2 * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation
3 * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com>
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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
24 #define BT_LOG_TAG "ATTRS"
25 #include <babeltrace/lib-logging-internal.h>
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>
34 #include <babeltrace/compat/string-internal.h>
35 #include <babeltrace/assert-internal.h>
37 #define BT_ATTR_NAME_INDEX 0
38 #define BT_ATTR_VALUE_INDEX 1
41 struct bt_value
*bt_attributes_create(void)
43 struct bt_value
*attr_obj
;
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).
50 * Example (JSON representation):
53 * ["hostname", "eeppdesk"],
54 * ["sysname", "Linux"],
55 * ["tracer_major", 2],
59 BT_LOGD_STR("Creating attributes object.");
60 attr_obj
= bt_value_array_create();
62 BT_LOGE_STR("Failed to create array value.");
64 BT_LOGD("Created attributes object: addr=%p",
72 void bt_attributes_destroy(struct bt_value
*attr_obj
)
74 BT_LOGD("Destroying attributes object: addr=%p", attr_obj
);
75 BT_OBJECT_PUT_REF_AND_RESET(attr_obj
);
79 int64_t bt_attributes_get_count(const struct bt_value
*attr_obj
)
81 return bt_value_array_get_size(attr_obj
);
85 const char *bt_attributes_get_field_name(const struct bt_value
*attr_obj
,
88 const char *ret
= NULL
;
89 const struct bt_value
*attr_field_obj
= NULL
;
90 const struct bt_value
*attr_field_name_obj
= NULL
;
93 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
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
));
104 attr_field_obj
= bt_value_array_borrow_element_by_index_const(
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
);
112 attr_field_name_obj
=
113 bt_value_array_borrow_element_by_index_const(attr_field_obj
,
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
);
122 ret
= bt_value_string_get(attr_field_name_obj
);
129 struct bt_value
*bt_attributes_borrow_field_value(
130 struct bt_value
*attr_obj
, uint64_t index
)
132 struct bt_value
*value_obj
= NULL
;
133 struct bt_value
*attr_field_obj
= NULL
;
136 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
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
));
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
);
155 value_obj
= bt_value_array_borrow_element_by_index(
156 attr_field_obj
, BT_ATTR_VALUE_INDEX
);
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
);
168 struct bt_value
*bt_attributes_borrow_field_by_name(
169 struct bt_value
*attr_obj
, const char *name
)
173 struct bt_value
*value_obj
= NULL
;
174 struct bt_value
*attr_field_name_obj
= NULL
;
176 attr_size
= bt_value_array_get_size(attr_obj
);
178 BT_LOGE("Cannot get array value's size: value-addr=%p",
183 for (i
= 0; i
< attr_size
; ++i
) {
184 const char *field_name
;
186 value_obj
= bt_value_array_borrow_element_by_index(
189 BT_LOGE("Cannot get attributes object's array value's element by index: "
190 "value-addr=%p, index=%" PRIu64
, attr_obj
, i
);
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
);
204 field_name
= bt_value_string_get(attr_field_name_obj
);
206 if (!strcmp(field_name
, name
)) {
221 int bt_attributes_set_field_value(struct bt_value
*attr_obj
,
222 const char *name
, struct bt_value
*value_obj
)
225 struct bt_value
*attr_field_obj
= NULL
;
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
);
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
,
240 attr_field_obj
= NULL
;
244 attr_field_obj
= bt_value_array_create();
245 if (!attr_field_obj
) {
246 BT_LOGE_STR("Failed to create empty array value.");
251 ret
= bt_value_array_append_string_element(attr_field_obj
,
253 ret
|= bt_value_array_append_element(attr_field_obj
,
256 BT_LOGE("Cannot append elements to array value: addr=%p",
261 ret
= bt_value_array_append_element(attr_obj
,
264 BT_LOGE("Cannot append element to array value: "
265 "array-value-addr=%p, element-value-addr=%p",
266 attr_obj
, attr_field_obj
);
270 bt_object_put_ref(attr_field_obj
);
275 struct bt_value
*bt_attributes_borrow_field_value_by_name(
276 struct bt_value
*attr_obj
, const char *name
)
278 struct bt_value
*value_obj
= NULL
;
279 struct bt_value
*attr_field_obj
= NULL
;
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
);
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
);
294 value_obj
= bt_value_array_borrow_element_by_index(
295 attr_field_obj
, BT_ATTR_VALUE_INDEX
);
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
);
307 int bt_attributes_freeze(const struct bt_value
*attr_obj
)
314 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
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);
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.
328 for (i
= 0; i
< count
; ++i
) {
329 struct bt_value
*obj
= NULL
;
331 obj
= bt_attributes_borrow_field_value(
332 (void *) attr_obj
, i
);
334 BT_LOGE("Cannot get attributes object's field value by index: "
335 "value-addr=%p, index=%" PRIu64
,
341 bt_value_freeze(obj
);
This page took 0.03579 seconds and 4 git commands to generate.