c158c7dc42181b9d8de5d1438ee31b5146da7bf7
[babeltrace.git] / lib / trace-ir / attributes.c
1 /*
2 * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation
3 * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #define BT_LOG_TAG "ATTRS"
25 #include <babeltrace/lib-logging-internal.h>
26
27 #include <babeltrace/object.h>
28 #include <babeltrace/babeltrace-internal.h>
29 #include <babeltrace/values.h>
30 #include <babeltrace/values-const.h>
31 #include <babeltrace/values-internal.h>
32 #include <babeltrace/trace-ir/attributes-internal.h>
33 #include <inttypes.h>
34 #include <babeltrace/compat/string-internal.h>
35 #include <babeltrace/assert-internal.h>
36
37 #define BT_ATTR_NAME_INDEX 0
38 #define BT_ATTR_VALUE_INDEX 1
39
40 BT_HIDDEN
41 struct bt_value *bt_attributes_create(void)
42 {
43 struct bt_value *attr_obj;
44
45 /*
46 * Attributes: array value object of array value objects, each one
47 * containing two entries: a string value object (attributes
48 * field name), and a value object (attributes field value).
49 *
50 * Example (JSON representation):
51 *
52 * [
53 * ["hostname", "eeppdesk"],
54 * ["sysname", "Linux"],
55 * ["tracer_major", 2],
56 * ["tracer_minor", 5]
57 * ]
58 */
59 BT_LOGD_STR("Creating attributes object.");
60 attr_obj = bt_value_array_create();
61 if (!attr_obj) {
62 BT_LOGE_STR("Failed to create array value.");
63 } else {
64 BT_LOGD("Created attributes object: addr=%p",
65 attr_obj);
66 }
67
68 return attr_obj;
69 }
70
71 BT_HIDDEN
72 void bt_attributes_destroy(struct bt_value *attr_obj)
73 {
74 BT_LOGD("Destroying attributes object: addr=%p", attr_obj);
75 BT_OBJECT_PUT_REF_AND_RESET(attr_obj);
76 }
77
78 BT_HIDDEN
79 int64_t bt_attributes_get_count(const struct bt_value *attr_obj)
80 {
81 return bt_value_array_get_size(attr_obj);
82 }
83
84 BT_HIDDEN
85 const char *bt_attributes_get_field_name(const struct bt_value *attr_obj,
86 uint64_t index)
87 {
88 const char *ret = NULL;
89 const struct bt_value *attr_field_obj = NULL;
90 const struct bt_value *attr_field_name_obj = NULL;
91
92 if (!attr_obj) {
93 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
94 goto end;
95 }
96
97 if (index >= bt_value_array_get_size(attr_obj)) {
98 BT_LOGW("Invalid parameter: index is out of bounds: "
99 "index=%" PRIu64 ", count=%" PRId64,
100 index, bt_value_array_get_size(attr_obj));
101 goto end;
102 }
103
104 attr_field_obj = bt_value_array_borrow_element_by_index_const(
105 attr_obj, index);
106 if (!attr_field_obj) {
107 BT_LOGE("Cannot get attributes object's array value's element by index: "
108 "value-addr=%p, index=%" PRIu64, attr_obj, index);
109 goto end;
110 }
111
112 attr_field_name_obj =
113 bt_value_array_borrow_element_by_index_const(attr_field_obj,
114 BT_ATTR_NAME_INDEX);
115 if (!attr_field_name_obj) {
116 BT_LOGE("Cannot get attribute array value's element by index: "
117 "value-addr=%p, index=%" PRIu64, attr_field_obj,
118 (uint64_t) BT_ATTR_NAME_INDEX);
119 goto end;
120 }
121
122 ret = bt_value_string_get(attr_field_name_obj);
123
124 end:
125 return ret;
126 }
127
128 BT_HIDDEN
129 struct bt_value *bt_attributes_borrow_field_value(
130 struct bt_value *attr_obj, 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 if (index >= bt_value_array_get_size(attr_obj)) {
141 BT_LOGW("Invalid parameter: index is out of bounds: "
142 "index=%" PRIu64 ", count=%" PRId64,
143 index, bt_value_array_get_size(attr_obj));
144 goto end;
145 }
146
147 attr_field_obj =
148 bt_value_array_borrow_element_by_index(attr_obj, index);
149 if (!attr_field_obj) {
150 BT_LOGE("Cannot get attributes object's array value's element by index: "
151 "value-addr=%p, index=%" PRIu64, attr_obj, index);
152 goto end;
153 }
154
155 value_obj = bt_value_array_borrow_element_by_index(
156 attr_field_obj, BT_ATTR_VALUE_INDEX);
157 if (!value_obj) {
158 BT_LOGE("Cannot get attribute array value's element by index: "
159 "value-addr=%p, index=%" PRIu64, attr_field_obj,
160 (uint64_t) BT_ATTR_VALUE_INDEX);
161 }
162
163 end:
164 return value_obj;
165 }
166
167 static
168 struct bt_value *bt_attributes_borrow_field_by_name(
169 struct bt_value *attr_obj, const char *name)
170 {
171 uint64_t i;
172 int64_t attr_size;
173 struct bt_value *value_obj = NULL;
174 struct bt_value *attr_field_name_obj = NULL;
175
176 attr_size = bt_value_array_get_size(attr_obj);
177 if (attr_size < 0) {
178 BT_LOGE("Cannot get array value's size: value-addr=%p",
179 attr_obj);
180 goto error;
181 }
182
183 for (i = 0; i < attr_size; ++i) {
184 const char *field_name;
185
186 value_obj = bt_value_array_borrow_element_by_index(
187 attr_obj, i);
188 if (!value_obj) {
189 BT_LOGE("Cannot get attributes object's array value's element by index: "
190 "value-addr=%p, index=%" PRIu64, attr_obj, i);
191 goto error;
192 }
193
194 attr_field_name_obj =
195 bt_value_array_borrow_element_by_index(
196 value_obj, BT_ATTR_NAME_INDEX);
197 if (!attr_field_name_obj) {
198 BT_LOGE("Cannot get attribute array value's element by index: "
199 "value-addr=%p, index=%" PRIu64,
200 value_obj, (int64_t) BT_ATTR_NAME_INDEX);
201 goto error;
202 }
203
204 field_name = bt_value_string_get(attr_field_name_obj);
205
206 if (!strcmp(field_name, name)) {
207 break;
208 }
209
210 value_obj = NULL;
211 }
212
213 return value_obj;
214
215 error:
216 value_obj = NULL;
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_borrow_field_by_name(attr_obj, name);
236 if (attr_field_obj) {
237 ret = bt_value_array_set_element_by_index(
238 attr_field_obj, BT_ATTR_VALUE_INDEX,
239 value_obj);
240 attr_field_obj = NULL;
241 goto end;
242 }
243
244 attr_field_obj = bt_value_array_create();
245 if (!attr_field_obj) {
246 BT_LOGE_STR("Failed to create empty array value.");
247 ret = -1;
248 goto end;
249 }
250
251 ret = bt_value_array_append_string_element(attr_field_obj,
252 name);
253 ret |= bt_value_array_append_element(attr_field_obj,
254 value_obj);
255 if (ret) {
256 BT_LOGE("Cannot append elements to array value: addr=%p",
257 attr_field_obj);
258 goto end;
259 }
260
261 ret = bt_value_array_append_element(attr_obj,
262 attr_field_obj);
263 if (ret) {
264 BT_LOGE("Cannot append element to array value: "
265 "array-value-addr=%p, element-value-addr=%p",
266 attr_obj, attr_field_obj);
267 }
268
269 end:
270 bt_object_put_ref(attr_field_obj);
271 return ret;
272 }
273
274 BT_HIDDEN
275 struct bt_value *bt_attributes_borrow_field_value_by_name(
276 struct bt_value *attr_obj, const char *name)
277 {
278 struct bt_value *value_obj = NULL;
279 struct bt_value *attr_field_obj = NULL;
280
281 if (!attr_obj || !name) {
282 BT_LOGW("Invalid parameter: attributes object or name is NULL: "
283 "value-addr=%p, name-addr=%p", attr_obj, name);
284 goto end;
285 }
286
287 attr_field_obj = bt_attributes_borrow_field_by_name(attr_obj, name);
288 if (!attr_field_obj) {
289 BT_LOGD("Cannot find attributes object's field by name: "
290 "value-addr=%p, name=\"%s\"", attr_obj, name);
291 goto end;
292 }
293
294 value_obj = bt_value_array_borrow_element_by_index(
295 attr_field_obj, BT_ATTR_VALUE_INDEX);
296 if (!value_obj) {
297 BT_LOGE("Cannot get attribute array value's element by index: "
298 "value-addr=%p, index=%" PRIu64, attr_field_obj,
299 (uint64_t) BT_ATTR_VALUE_INDEX);
300 }
301
302 end:
303 return value_obj;
304 }
305
306 BT_HIDDEN
307 int bt_attributes_freeze(const struct bt_value *attr_obj)
308 {
309 uint64_t i;
310 int64_t count;
311 int ret = 0;
312
313 if (!attr_obj) {
314 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
315 ret = -1;
316 goto end;
317 }
318
319 BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj);
320 count = bt_value_array_get_size(attr_obj);
321 BT_ASSERT(count >= 0);
322
323 /*
324 * We do not freeze the array value object itself here, since
325 * internal stuff could need to modify/add attributes. Each
326 * attribute is frozen one by one.
327 */
328 for (i = 0; i < count; ++i) {
329 struct bt_value *obj = NULL;
330
331 obj = bt_attributes_borrow_field_value(
332 (void *) attr_obj, i);
333 if (!obj) {
334 BT_LOGE("Cannot get attributes object's field value by index: "
335 "value-addr=%p, index=%" PRIu64,
336 attr_obj, i);
337 ret = -1;
338 goto end;
339 }
340
341 bt_value_freeze(obj);
342 }
343
344 end:
345 return ret;
346 }
This page took 0.034584 seconds and 3 git commands to generate.