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