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
,
92 const char *ret
= NULL
;
93 struct bt_private_value
*attr_field_obj
= NULL
;
94 struct bt_private_value
*attr_field_name_obj
= NULL
;
97 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
101 if (index
>= bt_value_array_get_size(
102 bt_value_borrow_from_private(attr_obj
))) {
103 BT_LOGW("Invalid parameter: index is out of bounds: "
104 "index=%" PRIu64
", count=%" PRId64
,
105 index
, bt_value_array_get_size(
106 bt_value_borrow_from_private(attr_obj
)));
110 attr_field_obj
= bt_private_value_array_borrow_element_by_index(
112 if (!attr_field_obj
) {
113 BT_LOGE("Cannot get attributes object's array value's element by index: "
114 "value-addr=%p, index=%" PRIu64
, attr_obj
, index
);
118 attr_field_name_obj
=
119 bt_private_value_array_borrow_element_by_index(attr_field_obj
,
121 if (!attr_field_name_obj
) {
122 BT_LOGE("Cannot get attribute array value's element by index: "
123 "value-addr=%p, index=%" PRIu64
, attr_field_obj
,
124 (uint64_t) BT_ATTR_NAME_INDEX
);
128 rc
= bt_value_string_get(
129 bt_value_borrow_from_private(attr_field_name_obj
), &ret
);
131 BT_LOGE("Cannot get raw value from string value: value-addr=%p",
132 attr_field_name_obj
);
141 struct bt_private_value
*bt_attributes_borrow_field_value(
142 struct bt_private_value
*attr_obj
, uint64_t index
)
144 struct bt_private_value
*value_obj
= NULL
;
145 struct bt_private_value
*attr_field_obj
= NULL
;
148 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
152 if (index
>= bt_value_array_get_size(bt_value_borrow_from_private(attr_obj
))) {
153 BT_LOGW("Invalid parameter: index is out of bounds: "
154 "index=%" PRIu64
", count=%" PRId64
,
155 index
, bt_value_array_get_size(
156 bt_value_borrow_from_private(attr_obj
)));
161 bt_private_value_array_borrow_element_by_index(attr_obj
, index
);
162 if (!attr_field_obj
) {
163 BT_LOGE("Cannot get attributes object's array value's element by index: "
164 "value-addr=%p, index=%" PRIu64
, attr_obj
, index
);
168 value_obj
= bt_private_value_array_borrow_element_by_index(
169 attr_field_obj
, BT_ATTR_VALUE_INDEX
);
171 BT_LOGE("Cannot get attribute array value's element by index: "
172 "value-addr=%p, index=%" PRIu64
, attr_field_obj
,
173 (uint64_t) BT_ATTR_VALUE_INDEX
);
181 struct bt_private_value
*bt_attributes_borrow_field_by_name(
182 struct bt_private_value
*attr_obj
, const char *name
)
186 struct bt_private_value
*value_obj
= NULL
;
187 struct bt_private_value
*attr_field_name_obj
= NULL
;
189 attr_size
= bt_value_array_get_size(
190 bt_value_borrow_from_private(attr_obj
));
192 BT_LOGE("Cannot get array value's size: value-addr=%p",
197 for (i
= 0; i
< attr_size
; ++i
) {
199 const char *field_name
;
201 value_obj
= bt_private_value_array_borrow_element_by_index(
204 BT_LOGE("Cannot get attributes object's array value's element by index: "
205 "value-addr=%p, index=%" PRIu64
, attr_obj
, i
);
209 attr_field_name_obj
=
210 bt_private_value_array_borrow_element_by_index(
211 value_obj
, BT_ATTR_NAME_INDEX
);
212 if (!attr_field_name_obj
) {
213 BT_LOGE("Cannot get attribute array value's element by index: "
214 "value-addr=%p, index=%" PRIu64
,
215 value_obj
, (int64_t) BT_ATTR_NAME_INDEX
);
219 ret
= bt_value_string_get(
220 bt_value_borrow_from_private(attr_field_name_obj
),
223 BT_LOGE("Cannot get raw value from string value: value-addr=%p",
224 attr_field_name_obj
);
228 if (!strcmp(field_name
, name
)) {
243 int bt_attributes_set_field_value(struct bt_private_value
*attr_obj
,
244 const char *name
, struct bt_private_value
*value_obj
)
247 struct bt_private_value
*attr_field_obj
= NULL
;
249 if (!attr_obj
|| !name
|| !value_obj
) {
250 BT_LOGW("Invalid parameter: attributes object, name, or value object is NULL: "
251 "attr-value-addr=%p, name-addr=%p, value-addr=%p",
252 attr_obj
, name
, value_obj
);
257 attr_field_obj
= bt_attributes_borrow_field_by_name(attr_obj
, name
);
258 if (attr_field_obj
) {
259 ret
= bt_private_value_array_set_element_by_index(
260 attr_field_obj
, BT_ATTR_VALUE_INDEX
,
261 bt_value_borrow_from_private(value_obj
));
262 attr_field_obj
= NULL
;
266 attr_field_obj
= bt_private_value_array_create();
267 if (!attr_field_obj
) {
268 BT_LOGE_STR("Failed to create empty array value.");
273 ret
= bt_private_value_array_append_string_element(attr_field_obj
,
275 ret
|= bt_private_value_array_append_element(attr_field_obj
,
276 bt_value_borrow_from_private(value_obj
));
278 BT_LOGE("Cannot append elements to array value: addr=%p",
283 ret
= bt_private_value_array_append_element(attr_obj
,
284 bt_value_borrow_from_private(attr_field_obj
));
286 BT_LOGE("Cannot append element to array value: "
287 "array-value-addr=%p, element-value-addr=%p",
288 attr_obj
, attr_field_obj
);
292 bt_object_put_ref(attr_field_obj
);
297 struct bt_private_value
*bt_attributes_borrow_field_value_by_name(
298 struct bt_private_value
*attr_obj
, const char *name
)
300 struct bt_private_value
*value_obj
= NULL
;
301 struct bt_private_value
*attr_field_obj
= NULL
;
303 if (!attr_obj
|| !name
) {
304 BT_LOGW("Invalid parameter: attributes object or name is NULL: "
305 "value-addr=%p, name-addr=%p", attr_obj
, name
);
309 attr_field_obj
= bt_attributes_borrow_field_by_name(attr_obj
, name
);
310 if (!attr_field_obj
) {
311 BT_LOGD("Cannot find attributes object's field by name: "
312 "value-addr=%p, name=\"%s\"", attr_obj
, name
);
316 value_obj
= bt_private_value_array_borrow_element_by_index(
317 attr_field_obj
, BT_ATTR_VALUE_INDEX
);
319 BT_LOGE("Cannot get attribute array value's element by index: "
320 "value-addr=%p, index=%" PRIu64
, attr_field_obj
,
321 (uint64_t) BT_ATTR_VALUE_INDEX
);
329 int bt_attributes_freeze(struct bt_private_value
*attr_obj
)
336 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
341 BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj
);
342 count
= bt_value_array_get_size(bt_value_borrow_from_private(attr_obj
));
343 BT_ASSERT(count
>= 0);
346 * We do not freeze the array value object itself here, since
347 * internal stuff could need to modify/add attributes. Each
348 * attribute is frozen one by one.
350 for (i
= 0; i
< count
; ++i
) {
351 struct bt_private_value
*obj
= NULL
;
353 obj
= bt_attributes_borrow_field_value(attr_obj
, i
);
355 BT_LOGE("Cannot get attributes object's field value by index: "
356 "value-addr=%p, index=%" PRIu64
,
362 bt_value_freeze(bt_value_borrow_from_private(obj
));
This page took 0.042467 seconds and 4 git commands to generate.