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 "common/assert.h"
32 #include "common/macros.h"
33 #include "compat/string.h"
34 #include <babeltrace2/ctf-writer/object.h>
39 #define BT_CTF_ATTR_NAME_INDEX 0
40 #define BT_CTF_ATTR_VALUE_INDEX 1
43 struct bt_ctf_private_value
*bt_ctf_attributes_create(void)
45 struct bt_ctf_private_value
*attr_obj
;
48 * Attributes: array value object of array value objects, each one
49 * containing two entries: a string value object (attributes
50 * field name), and a value object (attributes field value).
52 * Example (JSON representation):
55 * ["hostname", "eeppdesk"],
56 * ["sysname", "Linux"],
57 * ["tracer_major", 2],
61 BT_LOGD_STR("Creating attributes object.");
62 attr_obj
= bt_ctf_private_value_array_create();
64 BT_LOGE_STR("Failed to create array value.");
66 BT_LOGD("Created attributes object: addr=%p",
74 void bt_ctf_attributes_destroy(struct bt_ctf_private_value
*attr_obj
)
76 BT_LOGD("Destroying attributes object: addr=%p", attr_obj
);
77 bt_ctf_object_put_ref(attr_obj
);
81 int64_t bt_ctf_attributes_get_count(struct bt_ctf_private_value
*attr_obj
)
83 return bt_ctf_value_array_get_size(bt_ctf_private_value_as_value(attr_obj
));
87 const char *bt_ctf_attributes_get_field_name(struct bt_ctf_private_value
*attr_obj
,
90 const char *ret
= NULL
;
91 struct bt_ctf_private_value
*attr_field_obj
= NULL
;
92 struct bt_ctf_private_value
*attr_field_name_obj
= NULL
;
95 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
99 if (index
>= bt_ctf_value_array_get_size(bt_ctf_private_value_as_value(attr_obj
))) {
100 BT_LOGW("Invalid parameter: index is out of bounds: "
101 "index=%" PRIu64
", count=%" PRId64
,
102 index
, bt_ctf_value_array_get_size(bt_ctf_private_value_as_value(attr_obj
)));
106 attr_field_obj
= bt_ctf_private_value_array_borrow_element_by_index(
108 if (!attr_field_obj
) {
109 BT_LOGE("Cannot get attributes object's array value's element by index: "
110 "value-addr=%p, index=%" PRIu64
, attr_obj
, index
);
114 attr_field_name_obj
=
115 bt_ctf_private_value_array_borrow_element_by_index(
116 attr_field_obj
, BT_CTF_ATTR_NAME_INDEX
);
117 if (!attr_field_name_obj
) {
118 BT_LOGE("Cannot get attribute array value's element by index: "
119 "value-addr=%p, index=%" PRIu64
, attr_field_obj
,
120 (uint64_t) BT_CTF_ATTR_NAME_INDEX
);
124 ret
= bt_ctf_value_string_get(
125 bt_ctf_private_value_as_value(attr_field_name_obj
));
132 struct bt_ctf_private_value
*bt_ctf_attributes_borrow_field_value(struct bt_ctf_private_value
*attr_obj
,
135 struct bt_ctf_private_value
*value_obj
= NULL
;
136 struct bt_ctf_private_value
*attr_field_obj
= NULL
;
139 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
143 if (index
>= bt_ctf_value_array_get_size(bt_ctf_private_value_as_value(attr_obj
))) {
144 BT_LOGW("Invalid parameter: index is out of bounds: "
145 "index=%" PRIu64
", count=%" PRId64
,
146 index
, bt_ctf_value_array_get_size(bt_ctf_private_value_as_value(attr_obj
)));
150 attr_field_obj
= bt_ctf_private_value_array_borrow_element_by_index(
152 if (!attr_field_obj
) {
153 BT_LOGE("Cannot get attributes object's array value's element by index: "
154 "value-addr=%p, index=%" PRIu64
, attr_obj
, index
);
158 value_obj
= bt_ctf_private_value_array_borrow_element_by_index(attr_field_obj
,
159 BT_CTF_ATTR_VALUE_INDEX
);
161 BT_LOGE("Cannot get attribute array value's element by index: "
162 "value-addr=%p, index=%" PRIu64
, attr_field_obj
,
163 (uint64_t) BT_CTF_ATTR_VALUE_INDEX
);
171 struct bt_ctf_private_value
*bt_ctf_attributes_borrow_field_by_name(
172 struct bt_ctf_private_value
*attr_obj
, const char *name
)
176 struct bt_ctf_private_value
*value_obj
= NULL
;
177 struct bt_ctf_private_value
*attr_field_name_obj
= NULL
;
179 attr_size
= bt_ctf_value_array_get_size(bt_ctf_private_value_as_value(attr_obj
));
181 BT_LOGE("Cannot get array value's size: value-addr=%p",
186 for (i
= 0; i
< attr_size
; ++i
) {
187 const char *field_name
;
189 value_obj
= bt_ctf_private_value_array_borrow_element_by_index(attr_obj
, i
);
191 BT_LOGE("Cannot get attributes object's array value's element by index: "
192 "value-addr=%p, index=%" PRIu64
, attr_obj
, i
);
196 attr_field_name_obj
= bt_ctf_private_value_array_borrow_element_by_index(value_obj
,
197 BT_CTF_ATTR_NAME_INDEX
);
198 if (!attr_field_name_obj
) {
199 BT_LOGE("Cannot get attribute array value's element by index: "
200 "value-addr=%p, index=%" PRIu64
,
201 value_obj
, (int64_t) BT_CTF_ATTR_NAME_INDEX
);
205 field_name
= bt_ctf_value_string_get(
206 bt_ctf_private_value_as_value(attr_field_name_obj
));
208 if (!strcmp(field_name
, name
)) {
223 int bt_ctf_attributes_set_field_value(struct bt_ctf_private_value
*attr_obj
,
224 const char *name
, struct bt_ctf_private_value
*value_obj
)
227 struct bt_ctf_private_value
*attr_field_obj
= NULL
;
229 if (!attr_obj
|| !name
|| !value_obj
) {
230 BT_LOGW("Invalid parameter: attributes object, name, or value object is NULL: "
231 "attr-value-addr=%p, name-addr=%p, value-addr=%p",
232 attr_obj
, name
, value_obj
);
237 attr_field_obj
= bt_ctf_attributes_borrow_field_by_name(attr_obj
, name
);
238 if (attr_field_obj
) {
239 ret
= bt_ctf_private_value_array_set_element_by_index(
240 attr_field_obj
, BT_CTF_ATTR_VALUE_INDEX
,
241 bt_ctf_private_value_as_value(value_obj
));
242 attr_field_obj
= NULL
;
246 attr_field_obj
= bt_ctf_private_value_array_create();
247 if (!attr_field_obj
) {
248 BT_LOGE_STR("Failed to create empty array value.");
253 ret
= bt_ctf_private_value_array_append_string_element(attr_field_obj
, name
);
254 ret
|= bt_ctf_private_value_array_append_element(attr_field_obj
,
255 bt_ctf_private_value_as_value(value_obj
));
257 BT_LOGE("Cannot append elements to array value: addr=%p",
262 ret
= bt_ctf_private_value_array_append_element(attr_obj
,
263 bt_ctf_private_value_as_value(attr_field_obj
));
265 BT_LOGE("Cannot append element to array value: "
266 "array-value-addr=%p, element-value-addr=%p",
267 attr_obj
, attr_field_obj
);
271 bt_ctf_object_put_ref(attr_field_obj
);
276 struct bt_ctf_private_value
*bt_ctf_attributes_borrow_field_value_by_name(
277 struct bt_ctf_private_value
*attr_obj
, const char *name
)
279 struct bt_ctf_private_value
*value_obj
= NULL
;
280 struct bt_ctf_private_value
*attr_field_obj
= NULL
;
282 if (!attr_obj
|| !name
) {
283 BT_LOGW("Invalid parameter: attributes object or name is NULL: "
284 "value-addr=%p, name-addr=%p", attr_obj
, name
);
288 attr_field_obj
= bt_ctf_attributes_borrow_field_by_name(attr_obj
, name
);
289 if (!attr_field_obj
) {
290 BT_LOGD("Cannot find attributes object's field by name: "
291 "value-addr=%p, name=\"%s\"", attr_obj
, name
);
295 value_obj
= bt_ctf_private_value_array_borrow_element_by_index(attr_field_obj
,
296 BT_CTF_ATTR_VALUE_INDEX
);
298 BT_LOGE("Cannot get attribute array value's element by index: "
299 "value-addr=%p, index=%" PRIu64
, attr_field_obj
,
300 (uint64_t) BT_CTF_ATTR_VALUE_INDEX
);
308 int bt_ctf_attributes_freeze(struct bt_ctf_private_value
*attr_obj
)
315 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
320 BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj
);
321 count
= bt_ctf_value_array_get_size(bt_ctf_private_value_as_value(attr_obj
));
322 BT_ASSERT(count
>= 0);
325 * We do not freeze the array value object itself here, since
326 * internal stuff could need to modify/add attributes. Each
327 * attribute is frozen one by one.
329 for (i
= 0; i
< count
; ++i
) {
330 struct bt_ctf_private_value
*obj
= NULL
;
332 obj
= bt_ctf_attributes_borrow_field_value(attr_obj
, i
);
334 BT_LOGE("Cannot get attributes object's field value by index: "
335 "value-addr=%p, index=%" PRIu64
,
341 bt_ctf_value_freeze(bt_ctf_private_value_as_value(obj
));
This page took 0.036283 seconds and 4 git commands to generate.