b5fe54cd45ca61a034bd259e52ffd4c76ded11b6
[babeltrace.git] / lib / ctf-ir / attributes.c
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
28 #define BT_LOG_TAG "ATTRS"
29 #include <babeltrace/lib-logging-internal.h>
30
31 #include <babeltrace/ref.h>
32 #include <babeltrace/babeltrace-internal.h>
33 #include <babeltrace/values.h>
34 #include <babeltrace/values-internal.h>
35 #include <inttypes.h>
36 #include <babeltrace/compat/string-internal.h>
37 #include <babeltrace/assert-internal.h>
38
39 #define BT_ATTR_NAME_INDEX 0
40 #define BT_ATTR_VALUE_INDEX 1
41
42 BT_HIDDEN
43 struct bt_value *bt_attributes_create(void)
44 {
45 struct bt_value *attr_obj;
46
47 /*
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).
51 *
52 * Example (JSON representation):
53 *
54 * [
55 * ["hostname", "eeppdesk"],
56 * ["sysname", "Linux"],
57 * ["tracer_major", 2],
58 * ["tracer_minor", 5]
59 * ]
60 */
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;
71 }
72
73 BT_HIDDEN
74 void bt_attributes_destroy(struct bt_value *attr_obj)
75 {
76 BT_LOGD("Destroying attributes object: addr=%p", attr_obj);
77 bt_put(attr_obj);
78 }
79
80 BT_HIDDEN
81 int64_t bt_attributes_get_count(struct bt_value *attr_obj)
82 {
83 return bt_value_array_size(attr_obj);
84 }
85
86 BT_HIDDEN
87 const char *bt_attributes_get_field_name(struct bt_value *attr_obj,
88 uint64_t index)
89 {
90 int rc;
91 const char *ret = NULL;
92 struct bt_value *attr_field_obj = NULL;
93 struct bt_value *attr_field_name_obj = NULL;
94
95 if (!attr_obj) {
96 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
97 goto end;
98 }
99
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
107 attr_field_obj = bt_value_array_get(attr_obj, index);
108 if (!attr_field_obj) {
109 BT_LOGE("Cannot get attributes object's array value's element by index: "
110 "value-addr=%p, index=%" PRIu64, attr_obj, index);
111 goto end;
112 }
113
114 attr_field_name_obj = bt_value_array_get(attr_field_obj,
115 BT_ATTR_NAME_INDEX);
116 if (!attr_field_name_obj) {
117 BT_LOGE("Cannot get attribute array value's element by index: "
118 "value-addr=%p, index=%" PRIu64, attr_field_obj,
119 (uint64_t) BT_ATTR_NAME_INDEX);
120 goto end;
121 }
122
123 rc = bt_value_string_get(attr_field_name_obj, &ret);
124 if (rc) {
125 BT_LOGE("Cannot get raw value from string value: value-addr=%p",
126 attr_field_name_obj);
127 ret = NULL;
128 }
129
130 end:
131 BT_PUT(attr_field_name_obj);
132 BT_PUT(attr_field_obj);
133 return ret;
134 }
135
136 BT_HIDDEN
137 struct bt_value *bt_attributes_get_field_value(struct bt_value *attr_obj,
138 uint64_t index)
139 {
140 struct bt_value *value_obj = NULL;
141 struct bt_value *attr_field_obj = NULL;
142
143 if (!attr_obj) {
144 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
145 goto end;
146 }
147
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
155 attr_field_obj = bt_value_array_get(attr_obj, index);
156 if (!attr_field_obj) {
157 BT_LOGE("Cannot get attributes object's array value's element by index: "
158 "value-addr=%p, index=%" PRIu64, attr_obj, index);
159 goto end;
160 }
161
162 value_obj = bt_value_array_get(attr_field_obj,
163 BT_ATTR_VALUE_INDEX);
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,
167 (uint64_t) BT_ATTR_VALUE_INDEX);
168 }
169
170 end:
171 BT_PUT(attr_field_obj);
172 return value_obj;
173 }
174
175 static
176 struct bt_value *bt_attributes_get_field_by_name(
177 struct bt_value *attr_obj, const char *name)
178 {
179 uint64_t i;
180 int64_t attr_size;
181 struct bt_value *value_obj = NULL;
182 struct bt_value *attr_field_name_obj = NULL;
183
184 attr_size = bt_value_array_size(attr_obj);
185 if (attr_size < 0) {
186 BT_LOGE("Cannot get array value's size: value-addr=%p",
187 attr_obj);
188 goto error;
189 }
190
191 for (i = 0; i < attr_size; ++i) {
192 int ret;
193 const char *field_name;
194
195 value_obj = bt_value_array_get(attr_obj, i);
196 if (!value_obj) {
197 BT_LOGE("Cannot get attributes object's array value's element by index: "
198 "value-addr=%p, index=%" PRIu64, attr_obj, i);
199 goto error;
200 }
201
202 attr_field_name_obj = bt_value_array_get(value_obj, 0);
203 if (!attr_field_name_obj) {
204 BT_LOGE("Cannot get attribute array value's element by index: "
205 "value-addr=%p, index=%" PRIu64,
206 value_obj, (int64_t) 0);
207 goto error;
208 }
209
210 ret = bt_value_string_get(attr_field_name_obj, &field_name);
211 if (ret) {
212 BT_LOGE("Cannot get raw value from string value: value-addr=%p",
213 attr_field_name_obj);
214 goto error;
215 }
216
217 if (!strcmp(field_name, name)) {
218 BT_PUT(attr_field_name_obj);
219 break;
220 }
221
222 BT_PUT(attr_field_name_obj);
223 BT_PUT(value_obj);
224 }
225
226 return value_obj;
227
228 error:
229 BT_PUT(attr_field_name_obj);
230 BT_PUT(value_obj);
231
232 return value_obj;
233 }
234
235 BT_HIDDEN
236 int bt_attributes_set_field_value(struct bt_value *attr_obj,
237 const char *name, struct bt_value *value_obj)
238 {
239 int ret = 0;
240 struct bt_value *attr_field_obj = NULL;
241
242 if (!attr_obj || !name || !value_obj) {
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);
246 ret = -1;
247 goto end;
248 }
249
250 attr_field_obj = bt_attributes_get_field_by_name(attr_obj, name);
251 if (attr_field_obj) {
252 ret = bt_value_array_set(attr_field_obj,
253 BT_ATTR_VALUE_INDEX, value_obj);
254 goto end;
255 }
256
257 attr_field_obj = bt_value_array_create();
258 if (!attr_field_obj) {
259 BT_LOGE_STR("Failed to create empty array value.");
260 ret = -1;
261 goto end;
262 }
263
264 ret = bt_value_array_append_string(attr_field_obj, name);
265 ret |= bt_value_array_append(attr_field_obj, value_obj);
266 if (ret) {
267 BT_LOGE("Cannot append elements to array value: addr=%p",
268 attr_field_obj);
269 goto end;
270 }
271
272 ret = bt_value_array_append(attr_obj, attr_field_obj);
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 }
278
279 end:
280 BT_PUT(attr_field_obj);
281
282 return ret;
283 }
284
285 BT_HIDDEN
286 struct bt_value *bt_attributes_get_field_value_by_name(
287 struct bt_value *attr_obj, const char *name)
288 {
289 struct bt_value *value_obj = NULL;
290 struct bt_value *attr_field_obj = NULL;
291
292 if (!attr_obj || !name) {
293 BT_LOGW("Invalid parameter: attributes object or name is NULL: "
294 "value-addr=%p, name-addr=%p", attr_obj, name);
295 goto end;
296 }
297
298 attr_field_obj = bt_attributes_get_field_by_name(attr_obj, name);
299 if (!attr_field_obj) {
300 BT_LOGD("Cannot find attributes object's field by name: "
301 "value-addr=%p, name=\"%s\"", attr_obj, name);
302 goto end;
303 }
304
305 value_obj = bt_value_array_get(attr_field_obj,
306 BT_ATTR_VALUE_INDEX);
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,
310 (uint64_t) BT_ATTR_VALUE_INDEX);
311 }
312
313 end:
314 BT_PUT(attr_field_obj);
315
316 return value_obj;
317 }
318
319 BT_HIDDEN
320 int bt_attributes_freeze(struct bt_value *attr_obj)
321 {
322 uint64_t i;
323 int64_t count;
324 int ret = 0;
325
326 if (!attr_obj) {
327 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
328 ret = -1;
329 goto end;
330 }
331
332 BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj);
333 count = bt_value_array_size(attr_obj);
334 BT_ASSERT(count >= 0);
335
336 /*
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.
340 */
341 for (i = 0; i < count; ++i) {
342 struct bt_value *obj = NULL;
343
344 obj = bt_attributes_get_field_value(attr_obj, i);
345 if (!obj) {
346 BT_LOGE("Cannot get attributes object's field value by index: "
347 "value-addr=%p, index=%" PRIu64,
348 attr_obj, i);
349 ret = -1;
350 goto end;
351 }
352
353 bt_value_freeze(obj);
354 BT_PUT(obj);
355 }
356
357 end:
358 return ret;
359 }
This page took 0.036286 seconds and 3 git commands to generate.