2 * SPDX-License-Identifier: MIT
4 * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com>
7 * Babeltrace CTF writer - Attributes
10 #define BT_LOG_TAG "CTF-WRITER/ATTRS"
13 #include "attributes.h"
15 #include "common/assert.h"
16 #include "common/macros.h"
17 #include "compat/string.h"
18 #include <babeltrace2-ctf-writer/object.h>
23 #define BT_CTF_ATTR_NAME_INDEX 0
24 #define BT_CTF_ATTR_VALUE_INDEX 1
27 struct bt_ctf_private_value
*bt_ctf_attributes_create(void)
29 struct bt_ctf_private_value
*attr_obj
;
32 * Attributes: array value object of array value objects, each one
33 * containing two entries: a string value object (attributes
34 * field name), and a value object (attributes field value).
36 * Example (JSON representation):
39 * ["hostname", "eeppdesk"],
40 * ["sysname", "Linux"],
41 * ["tracer_major", 2],
45 BT_LOGD_STR("Creating attributes object.");
46 attr_obj
= bt_ctf_private_value_array_create();
48 BT_LOGE_STR("Failed to create array value.");
50 BT_LOGD("Created attributes object: addr=%p",
58 void bt_ctf_attributes_destroy(struct bt_ctf_private_value
*attr_obj
)
60 BT_LOGD("Destroying attributes object: addr=%p", attr_obj
);
61 bt_ctf_object_put_ref(attr_obj
);
65 int64_t bt_ctf_attributes_get_count(struct bt_ctf_private_value
*attr_obj
)
67 return bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj
));
71 const char *bt_ctf_attributes_get_field_name(struct bt_ctf_private_value
*attr_obj
,
74 const char *ret
= NULL
;
75 struct bt_ctf_private_value
*attr_field_obj
= NULL
;
76 struct bt_ctf_private_value
*attr_field_name_obj
= NULL
;
79 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
83 if (index
>= bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj
))) {
84 BT_LOGW("Invalid parameter: index is out of bounds: "
85 "index=%" PRIu64
", count=%" PRId64
,
86 index
, bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj
)));
90 attr_field_obj
= bt_ctf_private_value_array_borrow_element_by_index(
92 if (!attr_field_obj
) {
93 BT_LOGE("Cannot get attributes object's array value's element by index: "
94 "value-addr=%p, index=%" PRIu64
, attr_obj
, index
);
99 bt_ctf_private_value_array_borrow_element_by_index(
100 attr_field_obj
, BT_CTF_ATTR_NAME_INDEX
);
101 if (!attr_field_name_obj
) {
102 BT_LOGE("Cannot get attribute array value's element by index: "
103 "value-addr=%p, index=%" PRIu64
, attr_field_obj
,
104 (uint64_t) BT_CTF_ATTR_NAME_INDEX
);
108 ret
= bt_ctf_value_string_get(
109 bt_ctf_private_value_as_value(attr_field_name_obj
));
116 struct bt_ctf_private_value
*bt_ctf_attributes_borrow_field_value(struct bt_ctf_private_value
*attr_obj
,
119 struct bt_ctf_private_value
*value_obj
= NULL
;
120 struct bt_ctf_private_value
*attr_field_obj
= NULL
;
123 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
127 if (index
>= bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj
))) {
128 BT_LOGW("Invalid parameter: index is out of bounds: "
129 "index=%" PRIu64
", count=%" PRId64
,
130 index
, bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj
)));
134 attr_field_obj
= bt_ctf_private_value_array_borrow_element_by_index(
136 if (!attr_field_obj
) {
137 BT_LOGE("Cannot get attributes object's array value's element by index: "
138 "value-addr=%p, index=%" PRIu64
, attr_obj
, index
);
142 value_obj
= bt_ctf_private_value_array_borrow_element_by_index(attr_field_obj
,
143 BT_CTF_ATTR_VALUE_INDEX
);
145 BT_LOGE("Cannot get attribute array value's element by index: "
146 "value-addr=%p, index=%" PRIu64
, attr_field_obj
,
147 (uint64_t) BT_CTF_ATTR_VALUE_INDEX
);
155 struct bt_ctf_private_value
*bt_ctf_attributes_borrow_field_by_name(
156 struct bt_ctf_private_value
*attr_obj
, const char *name
)
160 struct bt_ctf_private_value
*value_obj
= NULL
;
161 struct bt_ctf_private_value
*attr_field_name_obj
= NULL
;
163 attr_size
= bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj
));
165 BT_LOGE("Cannot get array value's size: value-addr=%p",
170 for (i
= 0; i
< attr_size
; ++i
) {
171 const char *field_name
;
173 value_obj
= bt_ctf_private_value_array_borrow_element_by_index(attr_obj
, i
);
175 BT_LOGE("Cannot get attributes object's array value's element by index: "
176 "value-addr=%p, index=%" PRIu64
, attr_obj
, i
);
180 attr_field_name_obj
= bt_ctf_private_value_array_borrow_element_by_index(value_obj
,
181 BT_CTF_ATTR_NAME_INDEX
);
182 if (!attr_field_name_obj
) {
183 BT_LOGE("Cannot get attribute array value's element by index: "
184 "value-addr=%p, index=%" PRIu64
,
185 value_obj
, (int64_t) BT_CTF_ATTR_NAME_INDEX
);
189 field_name
= bt_ctf_value_string_get(
190 bt_ctf_private_value_as_value(attr_field_name_obj
));
192 if (strcmp(field_name
, name
) == 0) {
207 int bt_ctf_attributes_set_field_value(struct bt_ctf_private_value
*attr_obj
,
208 const char *name
, struct bt_ctf_private_value
*value_obj
)
211 struct bt_ctf_private_value
*attr_field_obj
= NULL
;
213 if (!attr_obj
|| !name
|| !value_obj
) {
214 BT_LOGW("Invalid parameter: attributes object, name, or value object is NULL: "
215 "attr-value-addr=%p, name-addr=%p, value-addr=%p",
216 attr_obj
, name
, value_obj
);
221 attr_field_obj
= bt_ctf_attributes_borrow_field_by_name(attr_obj
, name
);
222 if (attr_field_obj
) {
223 ret
= bt_ctf_private_value_array_set_element_by_index(
224 attr_field_obj
, BT_CTF_ATTR_VALUE_INDEX
,
225 bt_ctf_private_value_as_value(value_obj
));
226 attr_field_obj
= NULL
;
230 attr_field_obj
= bt_ctf_private_value_array_create();
231 if (!attr_field_obj
) {
232 BT_LOGE_STR("Failed to create empty array value.");
237 ret
= bt_ctf_private_value_array_append_string_element(attr_field_obj
, name
);
238 ret
|= bt_ctf_private_value_array_append_element(attr_field_obj
,
239 bt_ctf_private_value_as_value(value_obj
));
241 BT_LOGE("Cannot append elements to array value: addr=%p",
246 ret
= bt_ctf_private_value_array_append_element(attr_obj
,
247 bt_ctf_private_value_as_value(attr_field_obj
));
249 BT_LOGE("Cannot append element to array value: "
250 "array-value-addr=%p, element-value-addr=%p",
251 attr_obj
, attr_field_obj
);
255 bt_ctf_object_put_ref(attr_field_obj
);
260 struct bt_ctf_private_value
*bt_ctf_attributes_borrow_field_value_by_name(
261 struct bt_ctf_private_value
*attr_obj
, const char *name
)
263 struct bt_ctf_private_value
*value_obj
= NULL
;
264 struct bt_ctf_private_value
*attr_field_obj
= NULL
;
266 if (!attr_obj
|| !name
) {
267 BT_LOGW("Invalid parameter: attributes object or name is NULL: "
268 "value-addr=%p, name-addr=%p", attr_obj
, name
);
272 attr_field_obj
= bt_ctf_attributes_borrow_field_by_name(attr_obj
, name
);
273 if (!attr_field_obj
) {
274 BT_LOGD("Cannot find attributes object's field by name: "
275 "value-addr=%p, name=\"%s\"", attr_obj
, name
);
279 value_obj
= bt_ctf_private_value_array_borrow_element_by_index(attr_field_obj
,
280 BT_CTF_ATTR_VALUE_INDEX
);
282 BT_LOGE("Cannot get attribute array value's element by index: "
283 "value-addr=%p, index=%" PRIu64
, attr_field_obj
,
284 (uint64_t) BT_CTF_ATTR_VALUE_INDEX
);
292 int bt_ctf_attributes_freeze(struct bt_ctf_private_value
*attr_obj
)
299 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
304 BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj
);
305 count
= bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj
));
306 BT_ASSERT_DBG(count
>= 0);
309 * We do not freeze the array value object itself here, since
310 * internal stuff could need to modify/add attributes. Each
311 * attribute is frozen one by one.
313 for (i
= 0; i
< count
; ++i
) {
314 struct bt_ctf_private_value
*obj
= NULL
;
316 obj
= bt_ctf_attributes_borrow_field_value(attr_obj
, i
);
318 BT_LOGE("Cannot get attributes object's field value by index: "
319 "value-addr=%p, index=%" PRIu64
,
325 bt_ctf_value_freeze(bt_ctf_private_value_as_value(obj
));
This page took 0.035504 seconds and 4 git commands to generate.