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/private-values.h>
35 #include <babeltrace/values-internal.h>
37 #include <babeltrace/compat/string-internal.h>
38 #include <babeltrace/assert-internal.h>
40 #define BT_ATTR_NAME_INDEX 0
41 #define BT_ATTR_VALUE_INDEX 1
44 struct bt_private_value
*bt_attributes_create(void)
46 struct bt_private_value
*attr_obj
;
49 * Attributes: array value object of array value objects, each one
50 * containing two entries: a string value object (attributes
51 * field name), and a value object (attributes field value).
53 * Example (JSON representation):
56 * ["hostname", "eeppdesk"],
57 * ["sysname", "Linux"],
58 * ["tracer_major", 2],
62 BT_LOGD_STR("Creating attributes object.");
63 attr_obj
= bt_private_value_array_create();
65 BT_LOGE_STR("Failed to create array value.");
67 BT_LOGD("Created attributes object: addr=%p",
75 void bt_attributes_destroy(struct bt_private_value
*attr_obj
)
77 BT_LOGD("Destroying attributes object: addr=%p", attr_obj
);
78 bt_object_put_ref(attr_obj
);
82 int64_t bt_attributes_get_count(struct bt_private_value
*attr_obj
)
84 return bt_value_array_get_size(bt_value_borrow_from_private(attr_obj
));
88 const char *bt_attributes_get_field_name(struct bt_private_value
*attr_obj
,
91 const char *ret
= NULL
;
92 struct bt_private_value
*attr_field_obj
= NULL
;
93 struct bt_private_value
*attr_field_name_obj
= NULL
;
96 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
100 if (index
>= bt_value_array_get_size(
101 bt_value_borrow_from_private(attr_obj
))) {
102 BT_LOGW("Invalid parameter: index is out of bounds: "
103 "index=%" PRIu64
", count=%" PRId64
,
104 index
, bt_value_array_get_size(
105 bt_value_borrow_from_private(attr_obj
)));
109 attr_field_obj
= bt_private_value_array_borrow_element_by_index(
111 if (!attr_field_obj
) {
112 BT_LOGE("Cannot get attributes object's array value's element by index: "
113 "value-addr=%p, index=%" PRIu64
, attr_obj
, index
);
117 attr_field_name_obj
=
118 bt_private_value_array_borrow_element_by_index(attr_field_obj
,
120 if (!attr_field_name_obj
) {
121 BT_LOGE("Cannot get attribute array value's element by index: "
122 "value-addr=%p, index=%" PRIu64
, attr_field_obj
,
123 (uint64_t) BT_ATTR_NAME_INDEX
);
127 ret
= bt_value_string_get(
128 bt_value_borrow_from_private(attr_field_name_obj
));
135 struct bt_private_value
*bt_attributes_borrow_field_value(
136 struct bt_private_value
*attr_obj
, uint64_t index
)
138 struct bt_private_value
*value_obj
= NULL
;
139 struct bt_private_value
*attr_field_obj
= NULL
;
142 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
146 if (index
>= bt_value_array_get_size(bt_value_borrow_from_private(attr_obj
))) {
147 BT_LOGW("Invalid parameter: index is out of bounds: "
148 "index=%" PRIu64
", count=%" PRId64
,
149 index
, bt_value_array_get_size(
150 bt_value_borrow_from_private(attr_obj
)));
155 bt_private_value_array_borrow_element_by_index(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
);
162 value_obj
= bt_private_value_array_borrow_element_by_index(
163 attr_field_obj
, BT_ATTR_VALUE_INDEX
);
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
);
175 struct bt_private_value
*bt_attributes_borrow_field_by_name(
176 struct bt_private_value
*attr_obj
, const char *name
)
180 struct bt_private_value
*value_obj
= NULL
;
181 struct bt_private_value
*attr_field_name_obj
= NULL
;
183 attr_size
= bt_value_array_get_size(
184 bt_value_borrow_from_private(attr_obj
));
186 BT_LOGE("Cannot get array value's size: value-addr=%p",
191 for (i
= 0; i
< attr_size
; ++i
) {
192 const char *field_name
;
194 value_obj
= bt_private_value_array_borrow_element_by_index(
197 BT_LOGE("Cannot get attributes object's array value's element by index: "
198 "value-addr=%p, index=%" PRIu64
, attr_obj
, i
);
202 attr_field_name_obj
=
203 bt_private_value_array_borrow_element_by_index(
204 value_obj
, BT_ATTR_NAME_INDEX
);
205 if (!attr_field_name_obj
) {
206 BT_LOGE("Cannot get attribute array value's element by index: "
207 "value-addr=%p, index=%" PRIu64
,
208 value_obj
, (int64_t) BT_ATTR_NAME_INDEX
);
212 field_name
= bt_value_string_get(
213 bt_value_borrow_from_private(attr_field_name_obj
));
215 if (!strcmp(field_name
, name
)) {
230 int bt_attributes_set_field_value(struct bt_private_value
*attr_obj
,
231 const char *name
, struct bt_private_value
*value_obj
)
234 struct bt_private_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_private_value_array_set_element_by_index(
247 attr_field_obj
, BT_ATTR_VALUE_INDEX
,
248 bt_value_borrow_from_private(value_obj
));
249 attr_field_obj
= NULL
;
253 attr_field_obj
= bt_private_value_array_create();
254 if (!attr_field_obj
) {
255 BT_LOGE_STR("Failed to create empty array value.");
260 ret
= bt_private_value_array_append_string_element(attr_field_obj
,
262 ret
|= bt_private_value_array_append_element(attr_field_obj
,
263 bt_value_borrow_from_private(value_obj
));
265 BT_LOGE("Cannot append elements to array value: addr=%p",
270 ret
= bt_private_value_array_append_element(attr_obj
,
271 bt_value_borrow_from_private(attr_field_obj
));
273 BT_LOGE("Cannot append element to array value: "
274 "array-value-addr=%p, element-value-addr=%p",
275 attr_obj
, attr_field_obj
);
279 bt_object_put_ref(attr_field_obj
);
284 struct bt_private_value
*bt_attributes_borrow_field_value_by_name(
285 struct bt_private_value
*attr_obj
, const char *name
)
287 struct bt_private_value
*value_obj
= NULL
;
288 struct bt_private_value
*attr_field_obj
= NULL
;
290 if (!attr_obj
|| !name
) {
291 BT_LOGW("Invalid parameter: attributes object or name is NULL: "
292 "value-addr=%p, name-addr=%p", attr_obj
, name
);
296 attr_field_obj
= bt_attributes_borrow_field_by_name(attr_obj
, name
);
297 if (!attr_field_obj
) {
298 BT_LOGD("Cannot find attributes object's field by name: "
299 "value-addr=%p, name=\"%s\"", attr_obj
, name
);
303 value_obj
= bt_private_value_array_borrow_element_by_index(
304 attr_field_obj
, BT_ATTR_VALUE_INDEX
);
306 BT_LOGE("Cannot get attribute array value's element by index: "
307 "value-addr=%p, index=%" PRIu64
, attr_field_obj
,
308 (uint64_t) BT_ATTR_VALUE_INDEX
);
316 int bt_attributes_freeze(struct bt_private_value
*attr_obj
)
323 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
328 BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj
);
329 count
= bt_value_array_get_size(bt_value_borrow_from_private(attr_obj
));
330 BT_ASSERT(count
>= 0);
333 * We do not freeze the array value object itself here, since
334 * internal stuff could need to modify/add attributes. Each
335 * attribute is frozen one by one.
337 for (i
= 0; i
< count
; ++i
) {
338 struct bt_private_value
*obj
= NULL
;
340 obj
= bt_attributes_borrow_field_value(attr_obj
, i
);
342 BT_LOGE("Cannot get attributes object's field value by index: "
343 "value-addr=%p, index=%" PRIu64
,
349 bt_value_freeze(bt_value_borrow_from_private(obj
));
This page took 0.035992 seconds and 4 git commands to generate.