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