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