4 * Babeltrace CTF IR - 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 "ATTRS"
29 #include <babeltrace/lib-logging-internal.h>
31 #include <babeltrace/ref.h>
32 #include <babeltrace/babeltrace-internal.h>
33 #include <babeltrace/values.h>
36 #include <babeltrace/compat/string-internal.h>
38 #define BT_CTF_ATTR_NAME_INDEX 0
39 #define BT_CTF_ATTR_VALUE_INDEX 1
42 struct bt_value
*bt_ctf_attributes_create(void)
44 struct bt_value
*attr_obj
;
47 * Attributes: array value object of array value objects, each one
48 * containing two entries: a string value object (attributes
49 * field name), and a value object (attributes field value).
51 * Example (JSON representation):
54 * ["hostname", "eeppdesk"],
55 * ["sysname", "Linux"],
56 * ["tracer_major", 2],
60 BT_LOGD_STR("Creating attributes object.");
61 attr_obj
= bt_value_array_create();
63 BT_LOGE_STR("Failed to create array value.");
65 BT_LOGD("Created attributes object: addr=%p",
73 void bt_ctf_attributes_destroy(struct bt_value
*attr_obj
)
75 BT_LOGD("Destroying attributes object: addr=%p", attr_obj
);
80 int64_t bt_ctf_attributes_get_count(struct bt_value
*attr_obj
)
82 return bt_value_array_size(attr_obj
);
86 const char *bt_ctf_attributes_get_field_name(struct bt_value
*attr_obj
,
90 const char *ret
= NULL
;
91 struct bt_value
*attr_field_obj
= NULL
;
92 struct bt_value
*attr_field_name_obj
= NULL
;
95 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
99 attr_field_obj
= bt_value_array_get(attr_obj
, index
);
100 if (!attr_field_obj
) {
101 BT_LOGE("Cannot get attributes object's array value's element by index: "
102 "value-addr=%p, index=%" PRIu64
, attr_obj
, index
);
106 attr_field_name_obj
= bt_value_array_get(attr_field_obj
,
107 BT_CTF_ATTR_NAME_INDEX
);
108 if (!attr_field_name_obj
) {
109 BT_LOGE("Cannot get attribute array value's element by index: "
110 "value-addr=%p, index=%" PRIu64
, attr_field_obj
,
111 (uint64_t) BT_CTF_ATTR_NAME_INDEX
);
115 rc
= bt_value_string_get(attr_field_name_obj
, &ret
);
117 BT_LOGE("Cannot get raw value from string value: value-addr=%p",
118 attr_field_name_obj
);
123 BT_PUT(attr_field_name_obj
);
124 BT_PUT(attr_field_obj
);
129 struct bt_value
*bt_ctf_attributes_get_field_value(struct bt_value
*attr_obj
,
132 struct bt_value
*value_obj
= NULL
;
133 struct bt_value
*attr_field_obj
= NULL
;
136 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
140 attr_field_obj
= bt_value_array_get(attr_obj
, index
);
141 if (!attr_field_obj
) {
142 BT_LOGE("Cannot get attributes object's array value's element by index: "
143 "value-addr=%p, index=%" PRIu64
, attr_obj
, index
);
147 value_obj
= bt_value_array_get(attr_field_obj
,
148 BT_CTF_ATTR_VALUE_INDEX
);
150 BT_LOGE("Cannot get attribute array value's element by index: "
151 "value-addr=%p, index=%" PRIu64
, attr_field_obj
,
152 (uint64_t) BT_CTF_ATTR_VALUE_INDEX
);
156 BT_PUT(attr_field_obj
);
161 struct bt_value
*bt_ctf_attributes_get_field_by_name(
162 struct bt_value
*attr_obj
, const char *name
)
166 struct bt_value
*value_obj
= NULL
;
167 struct bt_value
*attr_field_name_obj
= NULL
;
169 attr_size
= bt_value_array_size(attr_obj
);
171 BT_LOGE("Cannot get array value's size: value-addr=%p",
176 for (i
= 0; i
< attr_size
; ++i
) {
178 const char *field_name
;
180 value_obj
= bt_value_array_get(attr_obj
, i
);
182 BT_LOGE("Cannot get attributes object's array value's element by index: "
183 "value-addr=%p, index=%" PRIu64
, attr_obj
, i
);
187 attr_field_name_obj
= bt_value_array_get(value_obj
, 0);
188 if (!attr_field_name_obj
) {
189 BT_LOGE("Cannot get attribute array value's element by index: "
190 "value-addr=%p, index=%" PRIu64
,
191 value_obj
, (int64_t) 0);
195 ret
= bt_value_string_get(attr_field_name_obj
, &field_name
);
197 BT_LOGE("Cannot get raw value from string value: value-addr=%p",
198 attr_field_name_obj
);
202 if (!strcmp(field_name
, name
)) {
203 BT_PUT(attr_field_name_obj
);
207 BT_PUT(attr_field_name_obj
);
214 BT_PUT(attr_field_name_obj
);
221 int bt_ctf_attributes_set_field_value(struct bt_value
*attr_obj
,
222 const char *name
, struct bt_value
*value_obj
)
225 struct bt_value
*attr_field_obj
= NULL
;
227 if (!attr_obj
|| !name
|| !value_obj
) {
228 BT_LOGW("Invalid parameter: attributes object, name, or value object is NULL: "
229 "attr-value-addr=%p, name-addr=%p, value-addr=%p",
230 attr_obj
, name
, value_obj
);
235 attr_field_obj
= bt_ctf_attributes_get_field_by_name(attr_obj
, name
);
236 if (attr_field_obj
) {
237 ret
= bt_value_array_set(attr_field_obj
,
238 BT_CTF_ATTR_VALUE_INDEX
, value_obj
);
242 attr_field_obj
= bt_value_array_create();
243 if (!attr_field_obj
) {
244 BT_LOGE_STR("Failed to create empty array value.");
249 ret
= bt_value_array_append_string(attr_field_obj
, name
);
250 ret
|= bt_value_array_append(attr_field_obj
, value_obj
);
252 BT_LOGE("Cannot append elements to array value: addr=%p",
257 ret
= bt_value_array_append(attr_obj
, attr_field_obj
);
259 BT_LOGE("Cannot append element to array value: "
260 "array-value-addr=%p, element-value-addr=%p",
261 attr_obj
, attr_field_obj
);
265 BT_PUT(attr_field_obj
);
271 struct bt_value
*bt_ctf_attributes_get_field_value_by_name(
272 struct bt_value
*attr_obj
, const char *name
)
274 struct bt_value
*value_obj
= NULL
;
275 struct bt_value
*attr_field_obj
= NULL
;
277 if (!attr_obj
|| !name
) {
278 BT_LOGW("Invalid parameter: attributes object or name is NULL: "
279 "value-addr=%p, name-addr=%p", attr_obj
, name
);
283 attr_field_obj
= bt_ctf_attributes_get_field_by_name(attr_obj
, name
);
284 if (!attr_field_obj
) {
285 BT_LOGD("Cannot find attributes object's field by name: "
286 "value-addr=%p, name=\"%s\"", attr_obj
, name
);
290 value_obj
= bt_value_array_get(attr_field_obj
,
291 BT_CTF_ATTR_VALUE_INDEX
);
293 BT_LOGE("Cannot get attribute array value's element by index: "
294 "value-addr=%p, index=%" PRIu64
, attr_field_obj
,
295 (uint64_t) BT_CTF_ATTR_VALUE_INDEX
);
299 BT_PUT(attr_field_obj
);
305 int bt_ctf_attributes_freeze(struct bt_value
*attr_obj
)
312 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
317 BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj
);
318 count
= bt_value_array_size(attr_obj
);
322 * We do not freeze the array value object itself here, since
323 * internal stuff could need to modify/add attributes. Each
324 * attribute is frozen one by one.
326 for (i
= 0; i
< count
; ++i
) {
327 struct bt_value
*obj
= NULL
;
329 obj
= bt_ctf_attributes_get_field_value(attr_obj
, i
);
331 BT_LOGE("Cannot get attributes object's field value by index: "
332 "value-addr=%p, index=%" PRIu64
,
338 bt_value_freeze(obj
);
This page took 0.056582 seconds and 4 git commands to generate.