include/babeltrace/graph: fix some include guards
[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>
f6ccaed9 34#include <babeltrace/values-internal.h>
561ad8c1 35#include <inttypes.h>
ee389f01 36#include <babeltrace/compat/string-internal.h>
f6ccaed9 37#include <babeltrace/assert-internal.h>
44e0a4f5 38
50842bdc
PP
39#define BT_ATTR_NAME_INDEX 0
40#define BT_ATTR_VALUE_INDEX 1
44e0a4f5
JG
41
42BT_HIDDEN
50842bdc 43struct bt_value *bt_attributes_create(void)
44e0a4f5 44{
561ad8c1
PP
45 struct bt_value *attr_obj;
46
44e0a4f5 47 /*
dac5c838
PP
48 * Attributes: array value object of array value objects, each one
49 * containing two entries: a string value object (attributes
50 * field name), and a value object (attributes field value).
44e0a4f5
JG
51 *
52 * Example (JSON representation):
53 *
54 * [
55 * ["hostname", "eeppdesk"],
56 * ["sysname", "Linux"],
57 * ["tracer_major", 2],
58 * ["tracer_minor", 5]
59 * ]
60 */
561ad8c1
PP
61 BT_LOGD_STR("Creating attributes object.");
62 attr_obj = bt_value_array_create();
63 if (!attr_obj) {
64 BT_LOGE_STR("Failed to create array value.");
65 } else {
66 BT_LOGD("Created attributes object: addr=%p",
67 attr_obj);
68 }
69
70 return attr_obj;
44e0a4f5
JG
71}
72
73BT_HIDDEN
50842bdc 74void bt_attributes_destroy(struct bt_value *attr_obj)
44e0a4f5 75{
561ad8c1 76 BT_LOGD("Destroying attributes object: addr=%p", attr_obj);
83509119 77 bt_put(attr_obj);
44e0a4f5
JG
78}
79
80BT_HIDDEN
50842bdc 81int64_t bt_attributes_get_count(struct bt_value *attr_obj)
44e0a4f5 82{
dac5c838 83 return bt_value_array_size(attr_obj);
44e0a4f5
JG
84}
85
86BT_HIDDEN
50842bdc 87const char *bt_attributes_get_field_name(struct bt_value *attr_obj,
dcf0cc71 88 uint64_t index)
44e0a4f5
JG
89{
90 int rc;
91 const char *ret = NULL;
dac5c838
PP
92 struct bt_value *attr_field_obj = NULL;
93 struct bt_value *attr_field_name_obj = NULL;
44e0a4f5 94
561ad8c1
PP
95 if (!attr_obj) {
96 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
44e0a4f5
JG
97 goto end;
98 }
99
f6ccaed9
PP
100 if (index >= bt_value_array_size(attr_obj)) {
101 BT_LOGW("Invalid parameter: index is out of bounds: "
102 "index=%" PRIu64 ", count=%" PRId64,
103 index, bt_value_array_size(attr_obj));
104 goto end;
105 }
106
dac5c838 107 attr_field_obj = bt_value_array_get(attr_obj, index);
44e0a4f5 108 if (!attr_field_obj) {
561ad8c1
PP
109 BT_LOGE("Cannot get attributes object's array value's element by index: "
110 "value-addr=%p, index=%" PRIu64, attr_obj, index);
44e0a4f5
JG
111 goto end;
112 }
113
dac5c838 114 attr_field_name_obj = bt_value_array_get(attr_field_obj,
50842bdc 115 BT_ATTR_NAME_INDEX);
44e0a4f5 116 if (!attr_field_name_obj) {
561ad8c1
PP
117 BT_LOGE("Cannot get attribute array value's element by index: "
118 "value-addr=%p, index=%" PRIu64, attr_field_obj,
50842bdc 119 (uint64_t) BT_ATTR_NAME_INDEX);
44e0a4f5
JG
120 goto end;
121 }
122
dac5c838 123 rc = bt_value_string_get(attr_field_name_obj, &ret);
44e0a4f5 124 if (rc) {
561ad8c1
PP
125 BT_LOGE("Cannot get raw value from string value: value-addr=%p",
126 attr_field_name_obj);
44e0a4f5
JG
127 ret = NULL;
128 }
129
130end:
83509119
JG
131 BT_PUT(attr_field_name_obj);
132 BT_PUT(attr_field_obj);
44e0a4f5
JG
133 return ret;
134}
135
136BT_HIDDEN
50842bdc 137struct bt_value *bt_attributes_get_field_value(struct bt_value *attr_obj,
dcf0cc71 138 uint64_t index)
44e0a4f5 139{
dac5c838
PP
140 struct bt_value *value_obj = NULL;
141 struct bt_value *attr_field_obj = NULL;
44e0a4f5 142
561ad8c1
PP
143 if (!attr_obj) {
144 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
44e0a4f5
JG
145 goto end;
146 }
147
f6ccaed9
PP
148 if (index >= bt_value_array_size(attr_obj)) {
149 BT_LOGW("Invalid parameter: index is out of bounds: "
150 "index=%" PRIu64 ", count=%" PRId64,
151 index, bt_value_array_size(attr_obj));
152 goto end;
153 }
154
dac5c838 155 attr_field_obj = bt_value_array_get(attr_obj, index);
44e0a4f5 156 if (!attr_field_obj) {
561ad8c1
PP
157 BT_LOGE("Cannot get attributes object's array value's element by index: "
158 "value-addr=%p, index=%" PRIu64, attr_obj, index);
44e0a4f5
JG
159 goto end;
160 }
161
dac5c838 162 value_obj = bt_value_array_get(attr_field_obj,
50842bdc 163 BT_ATTR_VALUE_INDEX);
561ad8c1
PP
164 if (!value_obj) {
165 BT_LOGE("Cannot get attribute array value's element by index: "
166 "value-addr=%p, index=%" PRIu64, attr_field_obj,
50842bdc 167 (uint64_t) BT_ATTR_VALUE_INDEX);
561ad8c1 168 }
44e0a4f5
JG
169
170end:
83509119 171 BT_PUT(attr_field_obj);
44e0a4f5
JG
172 return value_obj;
173}
174
175static
50842bdc 176struct bt_value *bt_attributes_get_field_by_name(
dac5c838 177 struct bt_value *attr_obj, const char *name)
44e0a4f5 178{
dcf0cc71
PP
179 uint64_t i;
180 int64_t attr_size;
dac5c838
PP
181 struct bt_value *value_obj = NULL;
182 struct bt_value *attr_field_name_obj = NULL;
44e0a4f5 183
dac5c838 184 attr_size = bt_value_array_size(attr_obj);
44e0a4f5 185 if (attr_size < 0) {
561ad8c1
PP
186 BT_LOGE("Cannot get array value's size: value-addr=%p",
187 attr_obj);
44e0a4f5
JG
188 goto error;
189 }
190
191 for (i = 0; i < attr_size; ++i) {
192 int ret;
193 const char *field_name;
194
dac5c838 195 value_obj = bt_value_array_get(attr_obj, i);
44e0a4f5 196 if (!value_obj) {
561ad8c1
PP
197 BT_LOGE("Cannot get attributes object's array value's element by index: "
198 "value-addr=%p, index=%" PRIu64, attr_obj, i);
44e0a4f5
JG
199 goto error;
200 }
201
dac5c838 202 attr_field_name_obj = bt_value_array_get(value_obj, 0);
44e0a4f5 203 if (!attr_field_name_obj) {
561ad8c1
PP
204 BT_LOGE("Cannot get attribute array value's element by index: "
205 "value-addr=%p, index=%" PRIu64,
32e87ceb 206 value_obj, (int64_t) 0);
44e0a4f5
JG
207 goto error;
208 }
209
dac5c838 210 ret = bt_value_string_get(attr_field_name_obj, &field_name);
44e0a4f5 211 if (ret) {
561ad8c1
PP
212 BT_LOGE("Cannot get raw value from string value: value-addr=%p",
213 attr_field_name_obj);
44e0a4f5
JG
214 goto error;
215 }
216
217 if (!strcmp(field_name, name)) {
83509119 218 BT_PUT(attr_field_name_obj);
44e0a4f5
JG
219 break;
220 }
221
83509119
JG
222 BT_PUT(attr_field_name_obj);
223 BT_PUT(value_obj);
44e0a4f5
JG
224 }
225
226 return value_obj;
227
228error:
83509119
JG
229 BT_PUT(attr_field_name_obj);
230 BT_PUT(value_obj);
44e0a4f5
JG
231
232 return value_obj;
233}
234
235BT_HIDDEN
50842bdc 236int bt_attributes_set_field_value(struct bt_value *attr_obj,
dac5c838 237 const char *name, struct bt_value *value_obj)
44e0a4f5
JG
238{
239 int ret = 0;
dac5c838 240 struct bt_value *attr_field_obj = NULL;
44e0a4f5
JG
241
242 if (!attr_obj || !name || !value_obj) {
561ad8c1
PP
243 BT_LOGW("Invalid parameter: attributes object, name, or value object is NULL: "
244 "attr-value-addr=%p, name-addr=%p, value-addr=%p",
245 attr_obj, name, value_obj);
44e0a4f5
JG
246 ret = -1;
247 goto end;
248 }
249
50842bdc 250 attr_field_obj = bt_attributes_get_field_by_name(attr_obj, name);
44e0a4f5 251 if (attr_field_obj) {
dac5c838 252 ret = bt_value_array_set(attr_field_obj,
50842bdc 253 BT_ATTR_VALUE_INDEX, value_obj);
44e0a4f5
JG
254 goto end;
255 }
256
dac5c838 257 attr_field_obj = bt_value_array_create();
44e0a4f5 258 if (!attr_field_obj) {
561ad8c1 259 BT_LOGE_STR("Failed to create empty array value.");
44e0a4f5
JG
260 ret = -1;
261 goto end;
262 }
263
dac5c838
PP
264 ret = bt_value_array_append_string(attr_field_obj, name);
265 ret |= bt_value_array_append(attr_field_obj, value_obj);
44e0a4f5 266 if (ret) {
561ad8c1
PP
267 BT_LOGE("Cannot append elements to array value: addr=%p",
268 attr_field_obj);
44e0a4f5
JG
269 goto end;
270 }
271
dac5c838 272 ret = bt_value_array_append(attr_obj, attr_field_obj);
561ad8c1
PP
273 if (ret) {
274 BT_LOGE("Cannot append element to array value: "
275 "array-value-addr=%p, element-value-addr=%p",
276 attr_obj, attr_field_obj);
277 }
44e0a4f5
JG
278
279end:
83509119 280 BT_PUT(attr_field_obj);
44e0a4f5
JG
281
282 return ret;
283}
284
285BT_HIDDEN
50842bdc 286struct bt_value *bt_attributes_get_field_value_by_name(
dac5c838 287 struct bt_value *attr_obj, const char *name)
44e0a4f5 288{
dac5c838
PP
289 struct bt_value *value_obj = NULL;
290 struct bt_value *attr_field_obj = NULL;
44e0a4f5
JG
291
292 if (!attr_obj || !name) {
561ad8c1
PP
293 BT_LOGW("Invalid parameter: attributes object or name is NULL: "
294 "value-addr=%p, name-addr=%p", attr_obj, name);
44e0a4f5
JG
295 goto end;
296 }
297
50842bdc 298 attr_field_obj = bt_attributes_get_field_by_name(attr_obj, name);
44e0a4f5 299 if (!attr_field_obj) {
561ad8c1
PP
300 BT_LOGD("Cannot find attributes object's field by name: "
301 "value-addr=%p, name=\"%s\"", attr_obj, name);
44e0a4f5
JG
302 goto end;
303 }
304
dac5c838 305 value_obj = bt_value_array_get(attr_field_obj,
50842bdc 306 BT_ATTR_VALUE_INDEX);
561ad8c1
PP
307 if (!value_obj) {
308 BT_LOGE("Cannot get attribute array value's element by index: "
309 "value-addr=%p, index=%" PRIu64, attr_field_obj,
50842bdc 310 (uint64_t) BT_ATTR_VALUE_INDEX);
561ad8c1 311 }
44e0a4f5
JG
312
313end:
83509119 314 BT_PUT(attr_field_obj);
44e0a4f5
JG
315
316 return value_obj;
317}
318
319BT_HIDDEN
50842bdc 320int bt_attributes_freeze(struct bt_value *attr_obj)
44e0a4f5 321{
dcf0cc71
PP
322 uint64_t i;
323 int64_t count;
44e0a4f5
JG
324 int ret = 0;
325
326 if (!attr_obj) {
561ad8c1 327 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
44e0a4f5
JG
328 ret = -1;
329 goto end;
330 }
331
561ad8c1 332 BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj);
dac5c838 333 count = bt_value_array_size(attr_obj);
f6ccaed9 334 BT_ASSERT(count >= 0);
44e0a4f5
JG
335
336 /*
dac5c838
PP
337 * We do not freeze the array value object itself here, since
338 * internal stuff could need to modify/add attributes. Each
339 * attribute is frozen one by one.
44e0a4f5
JG
340 */
341 for (i = 0; i < count; ++i) {
dac5c838 342 struct bt_value *obj = NULL;
44e0a4f5 343
50842bdc 344 obj = bt_attributes_get_field_value(attr_obj, i);
44e0a4f5 345 if (!obj) {
561ad8c1
PP
346 BT_LOGE("Cannot get attributes object's field value by index: "
347 "value-addr=%p, index=%" PRIu64,
348 attr_obj, i);
44e0a4f5
JG
349 ret = -1;
350 goto end;
351 }
352
dac5c838 353 bt_value_freeze(obj);
83509119 354 BT_PUT(obj);
44e0a4f5
JG
355 }
356
357end:
44e0a4f5
JG
358 return ret;
359}
This page took 0.056939 seconds and 4 git commands to generate.