4 * Babeltrace CTF writer - 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 "CTF-WRITER/ATTRS"
31 #include "attributes.h"
33 #include "common/assert.h"
34 #include "common/macros.h"
35 #include "compat/string.h"
36 #include <babeltrace2-ctf-writer/object.h>
41 #define BT_CTF_ATTR_NAME_INDEX 0
42 #define BT_CTF_ATTR_VALUE_INDEX 1
45 struct bt_ctf_private_value
*bt_ctf_attributes_create(void)
47 struct bt_ctf_private_value
*attr_obj
;
50 * Attributes: array value object of array value objects, each one
51 * containing two entries: a string value object (attributes
52 * field name), and a value object (attributes field value).
54 * Example (JSON representation):
57 * ["hostname", "eeppdesk"],
58 * ["sysname", "Linux"],
59 * ["tracer_major", 2],
63 BT_LOGD_STR("Creating attributes object.");
64 attr_obj
= bt_ctf_private_value_array_create();
66 BT_LOGE_STR("Failed to create array value.");
68 BT_LOGD("Created attributes object: addr=%p",
76 void bt_ctf_attributes_destroy(struct bt_ctf_private_value
*attr_obj
)
78 BT_LOGD("Destroying attributes object: addr=%p", attr_obj
);
79 bt_ctf_object_put_ref(attr_obj
);
83 int64_t bt_ctf_attributes_get_count(struct bt_ctf_private_value
*attr_obj
)
85 return bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj
));
89 const char *bt_ctf_attributes_get_field_name(struct bt_ctf_private_value
*attr_obj
,
92 const char *ret
= NULL
;
93 struct bt_ctf_private_value
*attr_field_obj
= NULL
;
94 struct bt_ctf_private_value
*attr_field_name_obj
= NULL
;
97 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
101 if (index
>= bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj
))) {
102 BT_LOGW("Invalid parameter: index is out of bounds: "
103 "index=%" PRIu64
", count=%" PRId64
,
104 index
, bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj
)));
108 attr_field_obj
= bt_ctf_private_value_array_borrow_element_by_index(
110 if (!attr_field_obj
) {
111 BT_LOGE("Cannot get attributes object's array value's element by index: "
112 "value-addr=%p, index=%" PRIu64
, attr_obj
, index
);
116 attr_field_name_obj
=
117 bt_ctf_private_value_array_borrow_element_by_index(
118 attr_field_obj
, BT_CTF_ATTR_NAME_INDEX
);
119 if (!attr_field_name_obj
) {
120 BT_LOGE("Cannot get attribute array value's element by index: "
121 "value-addr=%p, index=%" PRIu64
, attr_field_obj
,
122 (uint64_t) BT_CTF_ATTR_NAME_INDEX
);
126 ret
= bt_ctf_value_string_get(
127 bt_ctf_private_value_as_value(attr_field_name_obj
));
134 struct bt_ctf_private_value
*bt_ctf_attributes_borrow_field_value(struct bt_ctf_private_value
*attr_obj
,
137 struct bt_ctf_private_value
*value_obj
= NULL
;
138 struct bt_ctf_private_value
*attr_field_obj
= NULL
;
141 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
145 if (index
>= bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj
))) {
146 BT_LOGW("Invalid parameter: index is out of bounds: "
147 "index=%" PRIu64
", count=%" PRId64
,
148 index
, bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj
)));
152 attr_field_obj
= bt_ctf_private_value_array_borrow_element_by_index(
154 if (!attr_field_obj
) {
155 BT_LOGE("Cannot get attributes object's array value's element by index: "
156 "value-addr=%p, index=%" PRIu64
, attr_obj
, index
);
160 value_obj
= bt_ctf_private_value_array_borrow_element_by_index(attr_field_obj
,
161 BT_CTF_ATTR_VALUE_INDEX
);
163 BT_LOGE("Cannot get attribute array value's element by index: "
164 "value-addr=%p, index=%" PRIu64
, attr_field_obj
,
165 (uint64_t) BT_CTF_ATTR_VALUE_INDEX
);
173 struct bt_ctf_private_value
*bt_ctf_attributes_borrow_field_by_name(
174 struct bt_ctf_private_value
*attr_obj
, const char *name
)
178 struct bt_ctf_private_value
*value_obj
= NULL
;
179 struct bt_ctf_private_value
*attr_field_name_obj
= NULL
;
181 attr_size
= bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj
));
183 BT_LOGE("Cannot get array value's size: value-addr=%p",
188 for (i
= 0; i
< attr_size
; ++i
) {
189 const char *field_name
;
191 value_obj
= bt_ctf_private_value_array_borrow_element_by_index(attr_obj
, i
);
193 BT_LOGE("Cannot get attributes object's array value's element by index: "
194 "value-addr=%p, index=%" PRIu64
, attr_obj
, i
);
198 attr_field_name_obj
= bt_ctf_private_value_array_borrow_element_by_index(value_obj
,
199 BT_CTF_ATTR_NAME_INDEX
);
200 if (!attr_field_name_obj
) {
201 BT_LOGE("Cannot get attribute array value's element by index: "
202 "value-addr=%p, index=%" PRIu64
,
203 value_obj
, (int64_t) BT_CTF_ATTR_NAME_INDEX
);
207 field_name
= bt_ctf_value_string_get(
208 bt_ctf_private_value_as_value(attr_field_name_obj
));
210 if (strcmp(field_name
, name
) == 0) {
225 int bt_ctf_attributes_set_field_value(struct bt_ctf_private_value
*attr_obj
,
226 const char *name
, struct bt_ctf_private_value
*value_obj
)
229 struct bt_ctf_private_value
*attr_field_obj
= NULL
;
231 if (!attr_obj
|| !name
|| !value_obj
) {
232 BT_LOGW("Invalid parameter: attributes object, name, or value object is NULL: "
233 "attr-value-addr=%p, name-addr=%p, value-addr=%p",
234 attr_obj
, name
, value_obj
);
239 attr_field_obj
= bt_ctf_attributes_borrow_field_by_name(attr_obj
, name
);
240 if (attr_field_obj
) {
241 ret
= bt_ctf_private_value_array_set_element_by_index(
242 attr_field_obj
, BT_CTF_ATTR_VALUE_INDEX
,
243 bt_ctf_private_value_as_value(value_obj
));
244 attr_field_obj
= NULL
;
248 attr_field_obj
= bt_ctf_private_value_array_create();
249 if (!attr_field_obj
) {
250 BT_LOGE_STR("Failed to create empty array value.");
255 ret
= bt_ctf_private_value_array_append_string_element(attr_field_obj
, name
);
256 ret
|= bt_ctf_private_value_array_append_element(attr_field_obj
,
257 bt_ctf_private_value_as_value(value_obj
));
259 BT_LOGE("Cannot append elements to array value: addr=%p",
264 ret
= bt_ctf_private_value_array_append_element(attr_obj
,
265 bt_ctf_private_value_as_value(attr_field_obj
));
267 BT_LOGE("Cannot append element to array value: "
268 "array-value-addr=%p, element-value-addr=%p",
269 attr_obj
, attr_field_obj
);
273 bt_ctf_object_put_ref(attr_field_obj
);
278 struct bt_ctf_private_value
*bt_ctf_attributes_borrow_field_value_by_name(
279 struct bt_ctf_private_value
*attr_obj
, const char *name
)
281 struct bt_ctf_private_value
*value_obj
= NULL
;
282 struct bt_ctf_private_value
*attr_field_obj
= NULL
;
284 if (!attr_obj
|| !name
) {
285 BT_LOGW("Invalid parameter: attributes object or name is NULL: "
286 "value-addr=%p, name-addr=%p", attr_obj
, name
);
290 attr_field_obj
= bt_ctf_attributes_borrow_field_by_name(attr_obj
, name
);
291 if (!attr_field_obj
) {
292 BT_LOGD("Cannot find attributes object's field by name: "
293 "value-addr=%p, name=\"%s\"", attr_obj
, name
);
297 value_obj
= bt_ctf_private_value_array_borrow_element_by_index(attr_field_obj
,
298 BT_CTF_ATTR_VALUE_INDEX
);
300 BT_LOGE("Cannot get attribute array value's element by index: "
301 "value-addr=%p, index=%" PRIu64
, attr_field_obj
,
302 (uint64_t) BT_CTF_ATTR_VALUE_INDEX
);
310 int bt_ctf_attributes_freeze(struct bt_ctf_private_value
*attr_obj
)
317 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
322 BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj
);
323 count
= bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj
));
324 BT_ASSERT_DBG(count
>= 0);
327 * We do not freeze the array value object itself here, since
328 * internal stuff could need to modify/add attributes. Each
329 * attribute is frozen one by one.
331 for (i
= 0; i
< count
; ++i
) {
332 struct bt_ctf_private_value
*obj
= NULL
;
334 obj
= bt_ctf_attributes_borrow_field_value(attr_obj
, i
);
336 BT_LOGE("Cannot get attributes object's field value by index: "
337 "value-addr=%p, index=%" PRIu64
,
343 bt_ctf_value_freeze(bt_ctf_private_value_as_value(obj
));
This page took 0.03585 seconds and 4 git commands to generate.