Make API CTF-agnostic
[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
094ff7c0 107 attr_field_obj = bt_value_array_borrow(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
094ff7c0 114 attr_field_name_obj = bt_value_array_borrow(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:
44e0a4f5
JG
131 return ret;
132}
133
134BT_HIDDEN
094ff7c0 135struct bt_value *bt_attributes_borrow_field_value(struct bt_value *attr_obj,
dcf0cc71 136 uint64_t index)
44e0a4f5 137{
dac5c838
PP
138 struct bt_value *value_obj = NULL;
139 struct bt_value *attr_field_obj = NULL;
44e0a4f5 140
561ad8c1
PP
141 if (!attr_obj) {
142 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
44e0a4f5
JG
143 goto end;
144 }
145
f6ccaed9
PP
146 if (index >= bt_value_array_size(attr_obj)) {
147 BT_LOGW("Invalid parameter: index is out of bounds: "
148 "index=%" PRIu64 ", count=%" PRId64,
149 index, bt_value_array_size(attr_obj));
150 goto end;
151 }
152
094ff7c0 153 attr_field_obj = bt_value_array_borrow(attr_obj, index);
44e0a4f5 154 if (!attr_field_obj) {
561ad8c1
PP
155 BT_LOGE("Cannot get attributes object's array value's element by index: "
156 "value-addr=%p, index=%" PRIu64, attr_obj, index);
44e0a4f5
JG
157 goto end;
158 }
159
094ff7c0 160 value_obj = bt_value_array_borrow(attr_field_obj,
50842bdc 161 BT_ATTR_VALUE_INDEX);
561ad8c1
PP
162 if (!value_obj) {
163 BT_LOGE("Cannot get attribute array value's element by index: "
164 "value-addr=%p, index=%" PRIu64, attr_field_obj,
50842bdc 165 (uint64_t) BT_ATTR_VALUE_INDEX);
561ad8c1 166 }
44e0a4f5
JG
167
168end:
44e0a4f5
JG
169 return value_obj;
170}
171
172static
094ff7c0 173struct bt_value *bt_attributes_borrow_field_by_name(
dac5c838 174 struct bt_value *attr_obj, const char *name)
44e0a4f5 175{
dcf0cc71
PP
176 uint64_t i;
177 int64_t attr_size;
dac5c838
PP
178 struct bt_value *value_obj = NULL;
179 struct bt_value *attr_field_name_obj = NULL;
44e0a4f5 180
dac5c838 181 attr_size = bt_value_array_size(attr_obj);
44e0a4f5 182 if (attr_size < 0) {
561ad8c1
PP
183 BT_LOGE("Cannot get array value's size: value-addr=%p",
184 attr_obj);
44e0a4f5
JG
185 goto error;
186 }
187
188 for (i = 0; i < attr_size; ++i) {
189 int ret;
190 const char *field_name;
191
094ff7c0 192 value_obj = bt_value_array_borrow(attr_obj, i);
44e0a4f5 193 if (!value_obj) {
561ad8c1
PP
194 BT_LOGE("Cannot get attributes object's array value's element by index: "
195 "value-addr=%p, index=%" PRIu64, attr_obj, i);
44e0a4f5
JG
196 goto error;
197 }
198
094ff7c0
PP
199 attr_field_name_obj = bt_value_array_borrow(value_obj,
200 BT_ATTR_NAME_INDEX);
44e0a4f5 201 if (!attr_field_name_obj) {
561ad8c1
PP
202 BT_LOGE("Cannot get attribute array value's element by index: "
203 "value-addr=%p, index=%" PRIu64,
094ff7c0 204 value_obj, (int64_t) BT_ATTR_NAME_INDEX);
44e0a4f5
JG
205 goto error;
206 }
207
dac5c838 208 ret = bt_value_string_get(attr_field_name_obj, &field_name);
44e0a4f5 209 if (ret) {
561ad8c1
PP
210 BT_LOGE("Cannot get raw value from string value: value-addr=%p",
211 attr_field_name_obj);
44e0a4f5
JG
212 goto error;
213 }
214
215 if (!strcmp(field_name, name)) {
44e0a4f5
JG
216 break;
217 }
218
094ff7c0 219 value_obj = NULL;
44e0a4f5
JG
220 }
221
222 return value_obj;
223
224error:
094ff7c0 225 value_obj = NULL;
44e0a4f5
JG
226 return value_obj;
227}
228
229BT_HIDDEN
50842bdc 230int bt_attributes_set_field_value(struct bt_value *attr_obj,
dac5c838 231 const char *name, struct bt_value *value_obj)
44e0a4f5
JG
232{
233 int ret = 0;
dac5c838 234 struct bt_value *attr_field_obj = NULL;
44e0a4f5
JG
235
236 if (!attr_obj || !name || !value_obj) {
561ad8c1
PP
237 BT_LOGW("Invalid parameter: attributes object, name, or value object is NULL: "
238 "attr-value-addr=%p, name-addr=%p, value-addr=%p",
239 attr_obj, name, value_obj);
44e0a4f5
JG
240 ret = -1;
241 goto end;
242 }
243
094ff7c0 244 attr_field_obj = bt_attributes_borrow_field_by_name(attr_obj, name);
44e0a4f5 245 if (attr_field_obj) {
dac5c838 246 ret = bt_value_array_set(attr_field_obj,
50842bdc 247 BT_ATTR_VALUE_INDEX, value_obj);
094ff7c0 248 attr_field_obj = NULL;
44e0a4f5
JG
249 goto end;
250 }
251
dac5c838 252 attr_field_obj = bt_value_array_create();
44e0a4f5 253 if (!attr_field_obj) {
561ad8c1 254 BT_LOGE_STR("Failed to create empty array value.");
44e0a4f5
JG
255 ret = -1;
256 goto end;
257 }
258
dac5c838
PP
259 ret = bt_value_array_append_string(attr_field_obj, name);
260 ret |= bt_value_array_append(attr_field_obj, value_obj);
44e0a4f5 261 if (ret) {
561ad8c1
PP
262 BT_LOGE("Cannot append elements to array value: addr=%p",
263 attr_field_obj);
44e0a4f5
JG
264 goto end;
265 }
266
dac5c838 267 ret = bt_value_array_append(attr_obj, attr_field_obj);
561ad8c1
PP
268 if (ret) {
269 BT_LOGE("Cannot append element to array value: "
270 "array-value-addr=%p, element-value-addr=%p",
271 attr_obj, attr_field_obj);
272 }
44e0a4f5
JG
273
274end:
094ff7c0 275 bt_put(attr_field_obj);
44e0a4f5
JG
276 return ret;
277}
278
279BT_HIDDEN
094ff7c0 280struct bt_value *bt_attributes_borrow_field_value_by_name(
dac5c838 281 struct bt_value *attr_obj, const char *name)
44e0a4f5 282{
dac5c838
PP
283 struct bt_value *value_obj = NULL;
284 struct bt_value *attr_field_obj = NULL;
44e0a4f5
JG
285
286 if (!attr_obj || !name) {
561ad8c1
PP
287 BT_LOGW("Invalid parameter: attributes object or name is NULL: "
288 "value-addr=%p, name-addr=%p", attr_obj, name);
44e0a4f5
JG
289 goto end;
290 }
291
094ff7c0 292 attr_field_obj = bt_attributes_borrow_field_by_name(attr_obj, name);
44e0a4f5 293 if (!attr_field_obj) {
561ad8c1
PP
294 BT_LOGD("Cannot find attributes object's field by name: "
295 "value-addr=%p, name=\"%s\"", attr_obj, name);
44e0a4f5
JG
296 goto end;
297 }
298
094ff7c0 299 value_obj = bt_value_array_borrow(attr_field_obj,
50842bdc 300 BT_ATTR_VALUE_INDEX);
561ad8c1
PP
301 if (!value_obj) {
302 BT_LOGE("Cannot get attribute array value's element by index: "
303 "value-addr=%p, index=%" PRIu64, attr_field_obj,
50842bdc 304 (uint64_t) BT_ATTR_VALUE_INDEX);
561ad8c1 305 }
44e0a4f5
JG
306
307end:
44e0a4f5
JG
308 return value_obj;
309}
310
311BT_HIDDEN
50842bdc 312int bt_attributes_freeze(struct bt_value *attr_obj)
44e0a4f5 313{
dcf0cc71
PP
314 uint64_t i;
315 int64_t count;
44e0a4f5
JG
316 int ret = 0;
317
318 if (!attr_obj) {
561ad8c1 319 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
44e0a4f5
JG
320 ret = -1;
321 goto end;
322 }
323
561ad8c1 324 BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj);
dac5c838 325 count = bt_value_array_size(attr_obj);
f6ccaed9 326 BT_ASSERT(count >= 0);
44e0a4f5
JG
327
328 /*
dac5c838
PP
329 * We do not freeze the array value object itself here, since
330 * internal stuff could need to modify/add attributes. Each
331 * attribute is frozen one by one.
44e0a4f5
JG
332 */
333 for (i = 0; i < count; ++i) {
dac5c838 334 struct bt_value *obj = NULL;
44e0a4f5 335
094ff7c0 336 obj = bt_attributes_borrow_field_value(attr_obj, i);
44e0a4f5 337 if (!obj) {
561ad8c1
PP
338 BT_LOGE("Cannot get attributes object's field value by index: "
339 "value-addr=%p, index=%" PRIu64,
340 attr_obj, i);
44e0a4f5
JG
341 ret = -1;
342 goto end;
343 }
344
dac5c838 345 bt_value_freeze(obj);
44e0a4f5
JG
346 }
347
348end:
44e0a4f5
JG
349 return ret;
350}
This page took 0.057848 seconds and 4 git commands to generate.