27b689e2e508b9f64c1c57610800c27731e8a987
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
26 struct bt_ctf_private_value
*bt_ctf_attributes_create(void)
28 struct bt_ctf_private_value
*attr_obj
;
31 * Attributes: array value object of array value objects, each one
32 * containing two entries: a string value object (attributes
33 * field name), and a value object (attributes field value).
35 * Example (JSON representation):
38 * ["hostname", "eeppdesk"],
39 * ["sysname", "Linux"],
40 * ["tracer_major", 2],
44 BT_LOGD_STR("Creating attributes object.");
45 attr_obj
= bt_ctf_private_value_array_create();
47 BT_LOGE_STR("Failed to create array value.");
49 BT_LOGD("Created attributes object: addr=%p",
56 void bt_ctf_attributes_destroy(struct bt_ctf_private_value
*attr_obj
)
58 BT_LOGD("Destroying attributes object: addr=%p", attr_obj
);
59 bt_ctf_object_put_ref(attr_obj
);
62 int64_t bt_ctf_attributes_get_count(struct bt_ctf_private_value
*attr_obj
)
64 return bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj
));
67 const char *bt_ctf_attributes_get_field_name(struct bt_ctf_private_value
*attr_obj
,
70 const char *ret
= NULL
;
71 struct bt_ctf_private_value
*attr_field_obj
= NULL
;
72 struct bt_ctf_private_value
*attr_field_name_obj
= NULL
;
75 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
79 if (index
>= bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj
))) {
80 BT_LOGW("Invalid parameter: index is out of bounds: "
81 "index=%" PRIu64
", count=%" PRId64
,
82 index
, bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj
)));
86 attr_field_obj
= bt_ctf_private_value_array_borrow_element_by_index(
88 if (!attr_field_obj
) {
89 BT_LOGE("Cannot get attributes object's array value's element by index: "
90 "value-addr=%p, index=%" PRIu64
, attr_obj
, index
);
95 bt_ctf_private_value_array_borrow_element_by_index(
96 attr_field_obj
, BT_CTF_ATTR_NAME_INDEX
);
97 if (!attr_field_name_obj
) {
98 BT_LOGE("Cannot get attribute array value's element by index: "
99 "value-addr=%p, index=%" PRIu64
, attr_field_obj
,
100 (uint64_t) BT_CTF_ATTR_NAME_INDEX
);
104 ret
= bt_ctf_value_string_get(
105 bt_ctf_private_value_as_value(attr_field_name_obj
));
111 struct bt_ctf_private_value
*bt_ctf_attributes_borrow_field_value(struct bt_ctf_private_value
*attr_obj
,
114 struct bt_ctf_private_value
*value_obj
= NULL
;
115 struct bt_ctf_private_value
*attr_field_obj
= NULL
;
118 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
122 if (index
>= bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj
))) {
123 BT_LOGW("Invalid parameter: index is out of bounds: "
124 "index=%" PRIu64
", count=%" PRId64
,
125 index
, bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj
)));
129 attr_field_obj
= bt_ctf_private_value_array_borrow_element_by_index(
131 if (!attr_field_obj
) {
132 BT_LOGE("Cannot get attributes object's array value's element by index: "
133 "value-addr=%p, index=%" PRIu64
, attr_obj
, index
);
137 value_obj
= bt_ctf_private_value_array_borrow_element_by_index(attr_field_obj
,
138 BT_CTF_ATTR_VALUE_INDEX
);
140 BT_LOGE("Cannot get attribute array value's element by index: "
141 "value-addr=%p, index=%" PRIu64
, attr_field_obj
,
142 (uint64_t) BT_CTF_ATTR_VALUE_INDEX
);
150 struct bt_ctf_private_value
*bt_ctf_attributes_borrow_field_by_name(
151 struct bt_ctf_private_value
*attr_obj
, const char *name
)
155 struct bt_ctf_private_value
*value_obj
= NULL
;
156 struct bt_ctf_private_value
*attr_field_name_obj
= NULL
;
158 attr_size
= bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj
));
160 BT_LOGE("Cannot get array value's size: value-addr=%p",
165 for (i
= 0; i
< attr_size
; ++i
) {
166 const char *field_name
;
168 value_obj
= bt_ctf_private_value_array_borrow_element_by_index(attr_obj
, i
);
170 BT_LOGE("Cannot get attributes object's array value's element by index: "
171 "value-addr=%p, index=%" PRIu64
, attr_obj
, i
);
175 attr_field_name_obj
= bt_ctf_private_value_array_borrow_element_by_index(value_obj
,
176 BT_CTF_ATTR_NAME_INDEX
);
177 if (!attr_field_name_obj
) {
178 BT_LOGE("Cannot get attribute array value's element by index: "
179 "value-addr=%p, index=%" PRIu64
,
180 value_obj
, (int64_t) BT_CTF_ATTR_NAME_INDEX
);
184 field_name
= bt_ctf_value_string_get(
185 bt_ctf_private_value_as_value(attr_field_name_obj
));
187 if (strcmp(field_name
, name
) == 0) {
201 int bt_ctf_attributes_set_field_value(struct bt_ctf_private_value
*attr_obj
,
202 const char *name
, struct bt_ctf_private_value
*value_obj
)
205 struct bt_ctf_private_value
*attr_field_obj
= NULL
;
207 if (!attr_obj
|| !name
|| !value_obj
) {
208 BT_LOGW("Invalid parameter: attributes object, name, or value object is NULL: "
209 "attr-value-addr=%p, name-addr=%p, value-addr=%p",
210 attr_obj
, name
, value_obj
);
215 attr_field_obj
= bt_ctf_attributes_borrow_field_by_name(attr_obj
, name
);
216 if (attr_field_obj
) {
217 ret
= bt_ctf_private_value_array_set_element_by_index(
218 attr_field_obj
, BT_CTF_ATTR_VALUE_INDEX
,
219 bt_ctf_private_value_as_value(value_obj
));
220 attr_field_obj
= NULL
;
224 attr_field_obj
= bt_ctf_private_value_array_create();
225 if (!attr_field_obj
) {
226 BT_LOGE_STR("Failed to create empty array value.");
231 ret
= bt_ctf_private_value_array_append_string_element(attr_field_obj
, name
);
232 ret
|= bt_ctf_private_value_array_append_element(attr_field_obj
,
233 bt_ctf_private_value_as_value(value_obj
));
235 BT_LOGE("Cannot append elements to array value: addr=%p",
240 ret
= bt_ctf_private_value_array_append_element(attr_obj
,
241 bt_ctf_private_value_as_value(attr_field_obj
));
243 BT_LOGE("Cannot append element to array value: "
244 "array-value-addr=%p, element-value-addr=%p",
245 attr_obj
, attr_field_obj
);
249 bt_ctf_object_put_ref(attr_field_obj
);
253 struct bt_ctf_private_value
*bt_ctf_attributes_borrow_field_value_by_name(
254 struct bt_ctf_private_value
*attr_obj
, const char *name
)
256 struct bt_ctf_private_value
*value_obj
= NULL
;
257 struct bt_ctf_private_value
*attr_field_obj
= NULL
;
259 if (!attr_obj
|| !name
) {
260 BT_LOGW("Invalid parameter: attributes object or name is NULL: "
261 "value-addr=%p, name-addr=%p", attr_obj
, name
);
265 attr_field_obj
= bt_ctf_attributes_borrow_field_by_name(attr_obj
, name
);
266 if (!attr_field_obj
) {
267 BT_LOGD("Cannot find attributes object's field by name: "
268 "value-addr=%p, name=\"%s\"", attr_obj
, name
);
272 value_obj
= bt_ctf_private_value_array_borrow_element_by_index(attr_field_obj
,
273 BT_CTF_ATTR_VALUE_INDEX
);
275 BT_LOGE("Cannot get attribute array value's element by index: "
276 "value-addr=%p, index=%" PRIu64
, attr_field_obj
,
277 (uint64_t) BT_CTF_ATTR_VALUE_INDEX
);
284 int bt_ctf_attributes_freeze(struct bt_ctf_private_value
*attr_obj
)
291 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
296 BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj
);
297 count
= bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj
));
298 BT_ASSERT_DBG(count
>= 0);
301 * We do not freeze the array value object itself here, since
302 * internal stuff could need to modify/add attributes. Each
303 * attribute is frozen one by one.
305 for (i
= 0; i
< count
; ++i
) {
306 struct bt_ctf_private_value
*obj
= NULL
;
308 obj
= bt_ctf_attributes_borrow_field_value(attr_obj
, i
);
310 BT_LOGE("Cannot get attributes object's field value by index: "
311 "value-addr=%p, index=%" PRIu64
,
317 bt_ctf_value_freeze(bt_ctf_private_value_as_value(obj
));
This page took 0.038083 seconds and 5 git commands to generate.