4 * Babeltrace trace IR - Attributes
6 * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation
7 * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com>
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:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
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
28 #define BT_LOG_TAG "ATTRS"
29 #include <babeltrace/lib-logging-internal.h>
31 #include <babeltrace/object.h>
32 #include <babeltrace/babeltrace-internal.h>
33 #include <babeltrace/values.h>
34 #include <babeltrace/values-internal.h>
36 #include <babeltrace/compat/string-internal.h>
37 #include <babeltrace/assert-internal.h>
39 #define BT_ATTR_NAME_INDEX 0
40 #define BT_ATTR_VALUE_INDEX 1
43 struct bt_value
*bt_attributes_create(void)
45 struct bt_value
*attr_obj
;
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).
52 * Example (JSON representation):
55 * ["hostname", "eeppdesk"],
56 * ["sysname", "Linux"],
57 * ["tracer_major", 2],
61 BT_LOGD_STR("Creating attributes object.");
62 attr_obj
= bt_value_array_create();
64 BT_LOGE_STR("Failed to create array value.");
66 BT_LOGD("Created attributes object: addr=%p",
74 void bt_attributes_destroy(struct bt_value
*attr_obj
)
76 BT_LOGD("Destroying attributes object: addr=%p", attr_obj
);
77 bt_object_put_ref(attr_obj
);
81 int64_t bt_attributes_get_count(struct bt_value
*attr_obj
)
83 return bt_value_array_get_size(attr_obj
);
87 const char *bt_attributes_get_field_name(struct bt_value
*attr_obj
,
91 const char *ret
= NULL
;
92 struct bt_value
*attr_field_obj
= NULL
;
93 struct bt_value
*attr_field_name_obj
= NULL
;
96 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
100 if (index
>= bt_value_array_get_size(attr_obj
)) {
101 BT_LOGW("Invalid parameter: index is out of bounds: "
102 "index=%" PRIu64
", count=%" PRId64
,
103 index
, bt_value_array_get_size(attr_obj
));
107 attr_field_obj
= bt_value_array_borrow_element_by_index(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
);
114 attr_field_name_obj
= bt_value_array_borrow_element_by_index(attr_field_obj
,
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
);
123 rc
= bt_value_string_get(attr_field_name_obj
, &ret
);
125 BT_LOGE("Cannot get raw value from string value: value-addr=%p",
126 attr_field_name_obj
);
135 struct bt_value
*bt_attributes_borrow_field_value(struct bt_value
*attr_obj
,
138 struct bt_value
*value_obj
= NULL
;
139 struct bt_value
*attr_field_obj
= NULL
;
142 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
146 if (index
>= bt_value_array_get_size(attr_obj
)) {
147 BT_LOGW("Invalid parameter: index is out of bounds: "
148 "index=%" PRIu64
", count=%" PRId64
,
149 index
, bt_value_array_get_size(attr_obj
));
153 attr_field_obj
= bt_value_array_borrow_element_by_index(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
);
160 value_obj
= bt_value_array_borrow_element_by_index(attr_field_obj
,
161 BT_ATTR_VALUE_INDEX
);
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
);
173 struct bt_value
*bt_attributes_borrow_field_by_name(
174 struct bt_value
*attr_obj
, const char *name
)
178 struct bt_value
*value_obj
= NULL
;
179 struct bt_value
*attr_field_name_obj
= NULL
;
181 attr_size
= bt_value_array_get_size(attr_obj
);
183 BT_LOGE("Cannot get array value's size: value-addr=%p",
188 for (i
= 0; i
< attr_size
; ++i
) {
190 const char *field_name
;
192 value_obj
= bt_value_array_borrow_element_by_index(attr_obj
, i
);
194 BT_LOGE("Cannot get attributes object's array value's element by index: "
195 "value-addr=%p, index=%" PRIu64
, attr_obj
, i
);
199 attr_field_name_obj
= bt_value_array_borrow_element_by_index(value_obj
,
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
);
208 ret
= bt_value_string_get(attr_field_name_obj
, &field_name
);
210 BT_LOGE("Cannot get raw value from string value: value-addr=%p",
211 attr_field_name_obj
);
215 if (!strcmp(field_name
, name
)) {
230 int bt_attributes_set_field_value(struct bt_value
*attr_obj
,
231 const char *name
, struct bt_value
*value_obj
)
234 struct bt_value
*attr_field_obj
= NULL
;
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
);
244 attr_field_obj
= bt_attributes_borrow_field_by_name(attr_obj
, name
);
245 if (attr_field_obj
) {
246 ret
= bt_value_array_set_element_by_index(attr_field_obj
,
247 BT_ATTR_VALUE_INDEX
, value_obj
);
248 attr_field_obj
= NULL
;
252 attr_field_obj
= bt_value_array_create();
253 if (!attr_field_obj
) {
254 BT_LOGE_STR("Failed to create empty array value.");
259 ret
= bt_value_array_append_string_element(attr_field_obj
, name
);
260 ret
|= bt_value_array_append_element(attr_field_obj
, value_obj
);
262 BT_LOGE("Cannot append elements to array value: addr=%p",
267 ret
= bt_value_array_append_element(attr_obj
, attr_field_obj
);
269 BT_LOGE("Cannot append element to array value: "
270 "array-value-addr=%p, element-value-addr=%p",
271 attr_obj
, attr_field_obj
);
275 bt_object_put_ref(attr_field_obj
);
280 struct bt_value
*bt_attributes_borrow_field_value_by_name(
281 struct bt_value
*attr_obj
, const char *name
)
283 struct bt_value
*value_obj
= NULL
;
284 struct bt_value
*attr_field_obj
= NULL
;
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
);
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
);
299 value_obj
= bt_value_array_borrow_element_by_index(attr_field_obj
,
300 BT_ATTR_VALUE_INDEX
);
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
);
312 int bt_attributes_freeze(struct bt_value
*attr_obj
)
319 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
324 BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj
);
325 count
= bt_value_array_get_size(attr_obj
);
326 BT_ASSERT(count
>= 0);
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.
333 for (i
= 0; i
< count
; ++i
) {
334 struct bt_value
*obj
= NULL
;
336 obj
= bt_attributes_borrow_field_value(attr_obj
, i
);
338 BT_LOGE("Cannot get attributes object's field value by index: "
339 "value-addr=%p, index=%" PRIu64
,
345 bt_value_freeze(obj
);
This page took 0.03619 seconds and 4 git commands to generate.