49b84993bff6e4ee30785a463e61379ec68836ec
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"
29 #include <babeltrace/lib-logging-internal.h>
31 #include <babeltrace/assert-internal.h>
32 #include <babeltrace/babeltrace-internal.h>
33 #include <babeltrace/compat/string-internal.h>
34 #include <babeltrace/ctf-writer/object.h>
35 #include <babeltrace/ctf-writer/values-internal.h>
36 #include <babeltrace/ctf-writer/values-internal.h>
37 #include <babeltrace/ctf-writer/values-internal.h>
40 #define BT_CTF_ATTR_NAME_INDEX 0
41 #define BT_CTF_ATTR_VALUE_INDEX 1
44 struct bt_ctf_private_value
*bt_ctf_attributes_create(void)
46 struct bt_ctf_private_value
*attr_obj
;
49 * Attributes: array value object of array value objects, each one
50 * containing two entries: a string value object (attributes
51 * field name), and a value object (attributes field value).
53 * Example (JSON representation):
56 * ["hostname", "eeppdesk"],
57 * ["sysname", "Linux"],
58 * ["tracer_major", 2],
62 BT_LOGD_STR("Creating attributes object.");
63 attr_obj
= bt_ctf_private_value_array_create();
65 BT_LOGE_STR("Failed to create array value.");
67 BT_LOGD("Created attributes object: addr=%p",
75 void bt_ctf_attributes_destroy(struct bt_ctf_private_value
*attr_obj
)
77 BT_LOGD("Destroying attributes object: addr=%p", attr_obj
);
78 bt_ctf_object_put_ref(attr_obj
);
82 int64_t bt_ctf_attributes_get_count(struct bt_ctf_private_value
*attr_obj
)
84 return bt_ctf_value_array_get_size(bt_ctf_private_value_as_value(attr_obj
));
88 const char *bt_ctf_attributes_get_field_name(struct bt_ctf_private_value
*attr_obj
,
91 const char *ret
= NULL
;
92 struct bt_ctf_private_value
*attr_field_obj
= NULL
;
93 struct bt_ctf_private_value
*attr_field_name_obj
= NULL
;
96 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
100 if (index
>= bt_ctf_value_array_get_size(bt_ctf_private_value_as_value(attr_obj
))) {
101 BT_LOGW("Invalid parameter: index is out of bounds: "
102 "index=%" PRIu64
", count=%" PRId64
,
103 index
, bt_ctf_value_array_get_size(bt_ctf_private_value_as_value(attr_obj
)));
107 attr_field_obj
= bt_ctf_private_value_array_borrow_element_by_index(
109 if (!attr_field_obj
) {
110 BT_LOGE("Cannot get attributes object's array value's element by index: "
111 "value-addr=%p, index=%" PRIu64
, attr_obj
, index
);
115 attr_field_name_obj
=
116 bt_ctf_private_value_array_borrow_element_by_index(
117 attr_field_obj
, BT_CTF_ATTR_NAME_INDEX
);
118 if (!attr_field_name_obj
) {
119 BT_LOGE("Cannot get attribute array value's element by index: "
120 "value-addr=%p, index=%" PRIu64
, attr_field_obj
,
121 (uint64_t) BT_CTF_ATTR_NAME_INDEX
);
125 ret
= bt_ctf_value_string_get(
126 bt_ctf_private_value_as_value(attr_field_name_obj
));
133 struct bt_ctf_private_value
*bt_ctf_attributes_borrow_field_value(struct bt_ctf_private_value
*attr_obj
,
136 struct bt_ctf_private_value
*value_obj
= NULL
;
137 struct bt_ctf_private_value
*attr_field_obj
= NULL
;
140 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
144 if (index
>= bt_ctf_value_array_get_size(bt_ctf_private_value_as_value(attr_obj
))) {
145 BT_LOGW("Invalid parameter: index is out of bounds: "
146 "index=%" PRIu64
", count=%" PRId64
,
147 index
, bt_ctf_value_array_get_size(bt_ctf_private_value_as_value(attr_obj
)));
151 attr_field_obj
= bt_ctf_private_value_array_borrow_element_by_index(
153 if (!attr_field_obj
) {
154 BT_LOGE("Cannot get attributes object's array value's element by index: "
155 "value-addr=%p, index=%" PRIu64
, attr_obj
, index
);
159 value_obj
= bt_ctf_private_value_array_borrow_element_by_index(attr_field_obj
,
160 BT_CTF_ATTR_VALUE_INDEX
);
162 BT_LOGE("Cannot get attribute array value's element by index: "
163 "value-addr=%p, index=%" PRIu64
, attr_field_obj
,
164 (uint64_t) BT_CTF_ATTR_VALUE_INDEX
);
172 struct bt_ctf_private_value
*bt_ctf_attributes_borrow_field_by_name(
173 struct bt_ctf_private_value
*attr_obj
, const char *name
)
177 struct bt_ctf_private_value
*value_obj
= NULL
;
178 struct bt_ctf_private_value
*attr_field_name_obj
= NULL
;
180 attr_size
= bt_ctf_value_array_get_size(bt_ctf_private_value_as_value(attr_obj
));
182 BT_LOGE("Cannot get array value's size: value-addr=%p",
187 for (i
= 0; i
< attr_size
; ++i
) {
188 const char *field_name
;
190 value_obj
= bt_ctf_private_value_array_borrow_element_by_index(attr_obj
, i
);
192 BT_LOGE("Cannot get attributes object's array value's element by index: "
193 "value-addr=%p, index=%" PRIu64
, attr_obj
, i
);
197 attr_field_name_obj
= bt_ctf_private_value_array_borrow_element_by_index(value_obj
,
198 BT_CTF_ATTR_NAME_INDEX
);
199 if (!attr_field_name_obj
) {
200 BT_LOGE("Cannot get attribute array value's element by index: "
201 "value-addr=%p, index=%" PRIu64
,
202 value_obj
, (int64_t) BT_CTF_ATTR_NAME_INDEX
);
206 field_name
= bt_ctf_value_string_get(
207 bt_ctf_private_value_as_value(attr_field_name_obj
));
209 if (!strcmp(field_name
, name
)) {
224 int bt_ctf_attributes_set_field_value(struct bt_ctf_private_value
*attr_obj
,
225 const char *name
, struct bt_ctf_private_value
*value_obj
)
228 struct bt_ctf_private_value
*attr_field_obj
= NULL
;
230 if (!attr_obj
|| !name
|| !value_obj
) {
231 BT_LOGW("Invalid parameter: attributes object, name, or value object is NULL: "
232 "attr-value-addr=%p, name-addr=%p, value-addr=%p",
233 attr_obj
, name
, value_obj
);
238 attr_field_obj
= bt_ctf_attributes_borrow_field_by_name(attr_obj
, name
);
239 if (attr_field_obj
) {
240 ret
= bt_ctf_private_value_array_set_element_by_index(
241 attr_field_obj
, BT_CTF_ATTR_VALUE_INDEX
,
242 bt_ctf_private_value_as_value(value_obj
));
243 attr_field_obj
= NULL
;
247 attr_field_obj
= bt_ctf_private_value_array_create();
248 if (!attr_field_obj
) {
249 BT_LOGE_STR("Failed to create empty array value.");
254 ret
= bt_ctf_private_value_array_append_string_element(attr_field_obj
, name
);
255 ret
|= bt_ctf_private_value_array_append_element(attr_field_obj
,
256 bt_ctf_private_value_as_value(value_obj
));
258 BT_LOGE("Cannot append elements to array value: addr=%p",
263 ret
= bt_ctf_private_value_array_append_element(attr_obj
,
264 bt_ctf_private_value_as_value(attr_field_obj
));
266 BT_LOGE("Cannot append element to array value: "
267 "array-value-addr=%p, element-value-addr=%p",
268 attr_obj
, attr_field_obj
);
272 bt_ctf_object_put_ref(attr_field_obj
);
277 struct bt_ctf_private_value
*bt_ctf_attributes_borrow_field_value_by_name(
278 struct bt_ctf_private_value
*attr_obj
, const char *name
)
280 struct bt_ctf_private_value
*value_obj
= NULL
;
281 struct bt_ctf_private_value
*attr_field_obj
= NULL
;
283 if (!attr_obj
|| !name
) {
284 BT_LOGW("Invalid parameter: attributes object or name is NULL: "
285 "value-addr=%p, name-addr=%p", attr_obj
, name
);
289 attr_field_obj
= bt_ctf_attributes_borrow_field_by_name(attr_obj
, name
);
290 if (!attr_field_obj
) {
291 BT_LOGD("Cannot find attributes object's field by name: "
292 "value-addr=%p, name=\"%s\"", attr_obj
, name
);
296 value_obj
= bt_ctf_private_value_array_borrow_element_by_index(attr_field_obj
,
297 BT_CTF_ATTR_VALUE_INDEX
);
299 BT_LOGE("Cannot get attribute array value's element by index: "
300 "value-addr=%p, index=%" PRIu64
, attr_field_obj
,
301 (uint64_t) BT_CTF_ATTR_VALUE_INDEX
);
309 int bt_ctf_attributes_freeze(struct bt_ctf_private_value
*attr_obj
)
316 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
321 BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj
);
322 count
= bt_ctf_value_array_get_size(bt_ctf_private_value_as_value(attr_obj
));
323 BT_ASSERT(count
>= 0);
326 * We do not freeze the array value object itself here, since
327 * internal stuff could need to modify/add attributes. Each
328 * attribute is frozen one by one.
330 for (i
= 0; i
< count
; ++i
) {
331 struct bt_ctf_private_value
*obj
= NULL
;
333 obj
= bt_ctf_attributes_borrow_field_value(attr_obj
, i
);
335 BT_LOGE("Cannot get attributes object's field value by index: "
336 "value-addr=%p, index=%" PRIu64
,
342 bt_ctf_value_freeze(bt_ctf_private_value_as_value(obj
));
This page took 0.044509 seconds and 3 git commands to generate.