Fix: variable declaration shadows previously declared variable
[babeltrace.git] / lib / ctf-ir / attributes.c
CommitLineData
44e0a4f5
JG
1/*
2 * attributes.c
3 *
4 * Babeltrace CTF IR - Attributes
5 *
6 * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation
7 * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com>
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
0f5e83e5 28#define BT_LOG_TAG "ATTRS"
40547d22 29#include <babeltrace/lib-logging-internal.h>
0f5e83e5 30
9d408fca 31#include <babeltrace/ref.h>
44e0a4f5 32#include <babeltrace/babeltrace-internal.h>
dac5c838 33#include <babeltrace/values.h>
561ad8c1 34#include <inttypes.h>
0e4802f6 35#include <assert.h>
ee389f01 36#include <babeltrace/compat/string-internal.h>
44e0a4f5
JG
37
38#define BT_CTF_ATTR_NAME_INDEX 0
83509119 39#define BT_CTF_ATTR_VALUE_INDEX 1
44e0a4f5
JG
40
41BT_HIDDEN
dac5c838 42struct bt_value *bt_ctf_attributes_create(void)
44e0a4f5 43{
561ad8c1
PP
44 struct bt_value *attr_obj;
45
44e0a4f5 46 /*
dac5c838
PP
47 * Attributes: array value object of array value objects, each one
48 * containing two entries: a string value object (attributes
49 * field name), and a value object (attributes field value).
44e0a4f5
JG
50 *
51 * Example (JSON representation):
52 *
53 * [
54 * ["hostname", "eeppdesk"],
55 * ["sysname", "Linux"],
56 * ["tracer_major", 2],
57 * ["tracer_minor", 5]
58 * ]
59 */
561ad8c1
PP
60 BT_LOGD_STR("Creating attributes object.");
61 attr_obj = bt_value_array_create();
62 if (!attr_obj) {
63 BT_LOGE_STR("Failed to create array value.");
64 } else {
65 BT_LOGD("Created attributes object: addr=%p",
66 attr_obj);
67 }
68
69 return attr_obj;
44e0a4f5
JG
70}
71
72BT_HIDDEN
dac5c838 73void bt_ctf_attributes_destroy(struct bt_value *attr_obj)
44e0a4f5 74{
561ad8c1 75 BT_LOGD("Destroying attributes object: addr=%p", attr_obj);
83509119 76 bt_put(attr_obj);
44e0a4f5
JG
77}
78
79BT_HIDDEN
544d0515 80int64_t bt_ctf_attributes_get_count(struct bt_value *attr_obj)
44e0a4f5 81{
dac5c838 82 return bt_value_array_size(attr_obj);
44e0a4f5
JG
83}
84
85BT_HIDDEN
dac5c838 86const char *bt_ctf_attributes_get_field_name(struct bt_value *attr_obj,
dcf0cc71 87 uint64_t index)
44e0a4f5
JG
88{
89 int rc;
90 const char *ret = NULL;
dac5c838
PP
91 struct bt_value *attr_field_obj = NULL;
92 struct bt_value *attr_field_name_obj = NULL;
44e0a4f5 93
561ad8c1
PP
94 if (!attr_obj) {
95 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
44e0a4f5
JG
96 goto end;
97 }
98
dac5c838 99 attr_field_obj = bt_value_array_get(attr_obj, index);
44e0a4f5 100 if (!attr_field_obj) {
561ad8c1
PP
101 BT_LOGE("Cannot get attributes object's array value's element by index: "
102 "value-addr=%p, index=%" PRIu64, attr_obj, index);
44e0a4f5
JG
103 goto end;
104 }
105
dac5c838 106 attr_field_name_obj = bt_value_array_get(attr_field_obj,
44e0a4f5 107 BT_CTF_ATTR_NAME_INDEX);
44e0a4f5 108 if (!attr_field_name_obj) {
561ad8c1
PP
109 BT_LOGE("Cannot get attribute array value's element by index: "
110 "value-addr=%p, index=%" PRIu64, attr_field_obj,
111 (uint64_t) BT_CTF_ATTR_NAME_INDEX);
44e0a4f5
JG
112 goto end;
113 }
114
dac5c838 115 rc = bt_value_string_get(attr_field_name_obj, &ret);
44e0a4f5 116 if (rc) {
561ad8c1
PP
117 BT_LOGE("Cannot get raw value from string value: value-addr=%p",
118 attr_field_name_obj);
44e0a4f5
JG
119 ret = NULL;
120 }
121
122end:
83509119
JG
123 BT_PUT(attr_field_name_obj);
124 BT_PUT(attr_field_obj);
44e0a4f5
JG
125 return ret;
126}
127
128BT_HIDDEN
dac5c838 129struct bt_value *bt_ctf_attributes_get_field_value(struct bt_value *attr_obj,
dcf0cc71 130 uint64_t index)
44e0a4f5 131{
dac5c838
PP
132 struct bt_value *value_obj = NULL;
133 struct bt_value *attr_field_obj = NULL;
44e0a4f5 134
561ad8c1
PP
135 if (!attr_obj) {
136 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
44e0a4f5
JG
137 goto end;
138 }
139
dac5c838 140 attr_field_obj = bt_value_array_get(attr_obj, index);
44e0a4f5 141 if (!attr_field_obj) {
561ad8c1
PP
142 BT_LOGE("Cannot get attributes object's array value's element by index: "
143 "value-addr=%p, index=%" PRIu64, attr_obj, index);
44e0a4f5
JG
144 goto end;
145 }
146
dac5c838 147 value_obj = bt_value_array_get(attr_field_obj,
44e0a4f5 148 BT_CTF_ATTR_VALUE_INDEX);
561ad8c1
PP
149 if (!value_obj) {
150 BT_LOGE("Cannot get attribute array value's element by index: "
151 "value-addr=%p, index=%" PRIu64, attr_field_obj,
152 (uint64_t) BT_CTF_ATTR_VALUE_INDEX);
153 }
44e0a4f5
JG
154
155end:
83509119 156 BT_PUT(attr_field_obj);
44e0a4f5
JG
157 return value_obj;
158}
159
160static
dac5c838
PP
161struct bt_value *bt_ctf_attributes_get_field_by_name(
162 struct bt_value *attr_obj, const char *name)
44e0a4f5 163{
dcf0cc71
PP
164 uint64_t i;
165 int64_t attr_size;
dac5c838
PP
166 struct bt_value *value_obj = NULL;
167 struct bt_value *attr_field_name_obj = NULL;
44e0a4f5 168
dac5c838 169 attr_size = bt_value_array_size(attr_obj);
44e0a4f5 170 if (attr_size < 0) {
561ad8c1
PP
171 BT_LOGE("Cannot get array value's size: value-addr=%p",
172 attr_obj);
44e0a4f5
JG
173 goto error;
174 }
175
176 for (i = 0; i < attr_size; ++i) {
177 int ret;
178 const char *field_name;
179
dac5c838 180 value_obj = bt_value_array_get(attr_obj, i);
44e0a4f5 181 if (!value_obj) {
561ad8c1
PP
182 BT_LOGE("Cannot get attributes object's array value's element by index: "
183 "value-addr=%p, index=%" PRIu64, attr_obj, i);
44e0a4f5
JG
184 goto error;
185 }
186
dac5c838 187 attr_field_name_obj = bt_value_array_get(value_obj, 0);
44e0a4f5 188 if (!attr_field_name_obj) {
561ad8c1
PP
189 BT_LOGE("Cannot get attribute array value's element by index: "
190 "value-addr=%p, index=%" PRIu64,
32e87ceb 191 value_obj, (int64_t) 0);
44e0a4f5
JG
192 goto error;
193 }
194
dac5c838 195 ret = bt_value_string_get(attr_field_name_obj, &field_name);
44e0a4f5 196 if (ret) {
561ad8c1
PP
197 BT_LOGE("Cannot get raw value from string value: value-addr=%p",
198 attr_field_name_obj);
44e0a4f5
JG
199 goto error;
200 }
201
202 if (!strcmp(field_name, name)) {
83509119 203 BT_PUT(attr_field_name_obj);
44e0a4f5
JG
204 break;
205 }
206
83509119
JG
207 BT_PUT(attr_field_name_obj);
208 BT_PUT(value_obj);
44e0a4f5
JG
209 }
210
211 return value_obj;
212
213error:
83509119
JG
214 BT_PUT(attr_field_name_obj);
215 BT_PUT(value_obj);
44e0a4f5
JG
216
217 return value_obj;
218}
219
220BT_HIDDEN
dac5c838
PP
221int bt_ctf_attributes_set_field_value(struct bt_value *attr_obj,
222 const char *name, struct bt_value *value_obj)
44e0a4f5
JG
223{
224 int ret = 0;
dac5c838 225 struct bt_value *attr_field_obj = NULL;
44e0a4f5
JG
226
227 if (!attr_obj || !name || !value_obj) {
561ad8c1
PP
228 BT_LOGW("Invalid parameter: attributes object, name, or value object is NULL: "
229 "attr-value-addr=%p, name-addr=%p, value-addr=%p",
230 attr_obj, name, value_obj);
44e0a4f5
JG
231 ret = -1;
232 goto end;
233 }
234
235 attr_field_obj = bt_ctf_attributes_get_field_by_name(attr_obj, name);
44e0a4f5 236 if (attr_field_obj) {
dac5c838 237 ret = bt_value_array_set(attr_field_obj,
44e0a4f5
JG
238 BT_CTF_ATTR_VALUE_INDEX, value_obj);
239 goto end;
240 }
241
dac5c838 242 attr_field_obj = bt_value_array_create();
44e0a4f5 243 if (!attr_field_obj) {
561ad8c1 244 BT_LOGE_STR("Failed to create empty array value.");
44e0a4f5
JG
245 ret = -1;
246 goto end;
247 }
248
dac5c838
PP
249 ret = bt_value_array_append_string(attr_field_obj, name);
250 ret |= bt_value_array_append(attr_field_obj, value_obj);
44e0a4f5 251 if (ret) {
561ad8c1
PP
252 BT_LOGE("Cannot append elements to array value: addr=%p",
253 attr_field_obj);
44e0a4f5
JG
254 goto end;
255 }
256
dac5c838 257 ret = bt_value_array_append(attr_obj, attr_field_obj);
561ad8c1
PP
258 if (ret) {
259 BT_LOGE("Cannot append element to array value: "
260 "array-value-addr=%p, element-value-addr=%p",
261 attr_obj, attr_field_obj);
262 }
44e0a4f5
JG
263
264end:
83509119 265 BT_PUT(attr_field_obj);
44e0a4f5
JG
266
267 return ret;
268}
269
270BT_HIDDEN
dac5c838
PP
271struct bt_value *bt_ctf_attributes_get_field_value_by_name(
272 struct bt_value *attr_obj, const char *name)
44e0a4f5 273{
dac5c838
PP
274 struct bt_value *value_obj = NULL;
275 struct bt_value *attr_field_obj = NULL;
44e0a4f5
JG
276
277 if (!attr_obj || !name) {
561ad8c1
PP
278 BT_LOGW("Invalid parameter: attributes object or name is NULL: "
279 "value-addr=%p, name-addr=%p", attr_obj, name);
44e0a4f5
JG
280 goto end;
281 }
282
283 attr_field_obj = bt_ctf_attributes_get_field_by_name(attr_obj, name);
44e0a4f5 284 if (!attr_field_obj) {
561ad8c1
PP
285 BT_LOGD("Cannot find attributes object's field by name: "
286 "value-addr=%p, name=\"%s\"", attr_obj, name);
44e0a4f5
JG
287 goto end;
288 }
289
dac5c838 290 value_obj = bt_value_array_get(attr_field_obj,
44e0a4f5 291 BT_CTF_ATTR_VALUE_INDEX);
561ad8c1
PP
292 if (!value_obj) {
293 BT_LOGE("Cannot get attribute array value's element by index: "
294 "value-addr=%p, index=%" PRIu64, attr_field_obj,
295 (uint64_t) BT_CTF_ATTR_VALUE_INDEX);
296 }
44e0a4f5
JG
297
298end:
83509119 299 BT_PUT(attr_field_obj);
44e0a4f5
JG
300
301 return value_obj;
302}
303
304BT_HIDDEN
dac5c838 305int bt_ctf_attributes_freeze(struct bt_value *attr_obj)
44e0a4f5 306{
dcf0cc71
PP
307 uint64_t i;
308 int64_t count;
44e0a4f5
JG
309 int ret = 0;
310
311 if (!attr_obj) {
561ad8c1 312 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
44e0a4f5
JG
313 ret = -1;
314 goto end;
315 }
316
561ad8c1 317 BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj);
dac5c838 318 count = bt_value_array_size(attr_obj);
0e4802f6 319 assert(count >= 0);
44e0a4f5
JG
320
321 /*
dac5c838
PP
322 * We do not freeze the array value object itself here, since
323 * internal stuff could need to modify/add attributes. Each
324 * attribute is frozen one by one.
44e0a4f5
JG
325 */
326 for (i = 0; i < count; ++i) {
dac5c838 327 struct bt_value *obj = NULL;
44e0a4f5
JG
328
329 obj = bt_ctf_attributes_get_field_value(attr_obj, i);
44e0a4f5 330 if (!obj) {
561ad8c1
PP
331 BT_LOGE("Cannot get attributes object's field value by index: "
332 "value-addr=%p, index=%" PRIu64,
333 attr_obj, i);
44e0a4f5
JG
334 ret = -1;
335 goto end;
336 }
337
dac5c838 338 bt_value_freeze(obj);
83509119 339 BT_PUT(obj);
44e0a4f5
JG
340 }
341
342end:
44e0a4f5
JG
343 return ret;
344}
This page took 0.056347 seconds and 4 git commands to generate.