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/babeltrace-internal.h>
32 #include <babeltrace/values.h>
35 #define BT_CTF_ATTR_NAME_INDEX 0
36 #define BT_CTF_ATTR_VALUE_INDEX 1
39 struct bt_value
*bt_ctf_attributes_create(void)
41 struct bt_value
*attr_obj
;
44 * Attributes: array value object of array value objects, each one
45 * containing two entries: a string value object (attributes
46 * field name), and a value object (attributes field value).
48 * Example (JSON representation):
51 * ["hostname", "eeppdesk"],
52 * ["sysname", "Linux"],
53 * ["tracer_major", 2],
57 BT_LOGD_STR("Creating attributes object.");
58 attr_obj
= bt_value_array_create();
60 BT_LOGE_STR("Failed to create array value.");
62 BT_LOGD("Created attributes object: addr=%p",
70 void bt_ctf_attributes_destroy(struct bt_value
*attr_obj
)
72 BT_LOGD("Destroying attributes object: addr=%p", attr_obj
);
77 int64_t bt_ctf_attributes_get_count(struct bt_value
*attr_obj
)
79 return bt_value_array_size(attr_obj
);
83 const char *bt_ctf_attributes_get_field_name(struct bt_value
*attr_obj
,
87 const char *ret
= NULL
;
88 struct bt_value
*attr_field_obj
= NULL
;
89 struct bt_value
*attr_field_name_obj
= NULL
;
92 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
96 attr_field_obj
= bt_value_array_get(attr_obj
, index
);
97 if (!attr_field_obj
) {
98 BT_LOGE("Cannot get attributes object's array value's element by index: "
99 "value-addr=%p, index=%" PRIu64
, attr_obj
, index
);
103 attr_field_name_obj
= bt_value_array_get(attr_field_obj
,
104 BT_CTF_ATTR_NAME_INDEX
);
105 if (!attr_field_name_obj
) {
106 BT_LOGE("Cannot get attribute array value's element by index: "
107 "value-addr=%p, index=%" PRIu64
, attr_field_obj
,
108 (uint64_t) BT_CTF_ATTR_NAME_INDEX
);
112 rc
= bt_value_string_get(attr_field_name_obj
, &ret
);
114 BT_LOGE("Cannot get raw value from string value: value-addr=%p",
115 attr_field_name_obj
);
120 BT_PUT(attr_field_name_obj
);
121 BT_PUT(attr_field_obj
);
126 struct bt_value
*bt_ctf_attributes_get_field_value(struct bt_value
*attr_obj
,
129 struct bt_value
*value_obj
= NULL
;
130 struct bt_value
*attr_field_obj
= NULL
;
133 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
137 attr_field_obj
= bt_value_array_get(attr_obj
, index
);
138 if (!attr_field_obj
) {
139 BT_LOGE("Cannot get attributes object's array value's element by index: "
140 "value-addr=%p, index=%" PRIu64
, attr_obj
, index
);
144 value_obj
= bt_value_array_get(attr_field_obj
,
145 BT_CTF_ATTR_VALUE_INDEX
);
147 BT_LOGE("Cannot get attribute array value's element by index: "
148 "value-addr=%p, index=%" PRIu64
, attr_field_obj
,
149 (uint64_t) BT_CTF_ATTR_VALUE_INDEX
);
153 BT_PUT(attr_field_obj
);
158 struct bt_value
*bt_ctf_attributes_get_field_by_name(
159 struct bt_value
*attr_obj
, const char *name
)
163 struct bt_value
*value_obj
= NULL
;
164 struct bt_value
*attr_field_name_obj
= NULL
;
166 attr_size
= bt_value_array_size(attr_obj
);
168 BT_LOGE("Cannot get array value's size: value-addr=%p",
173 for (i
= 0; i
< attr_size
; ++i
) {
175 const char *field_name
;
177 value_obj
= bt_value_array_get(attr_obj
, i
);
179 BT_LOGE("Cannot get attributes object's array value's element by index: "
180 "value-addr=%p, index=%" PRIu64
, attr_obj
, i
);
184 attr_field_name_obj
= bt_value_array_get(value_obj
, 0);
185 if (!attr_field_name_obj
) {
186 BT_LOGE("Cannot get attribute array value's element by index: "
187 "value-addr=%p, index=%" PRIu64
,
192 ret
= bt_value_string_get(attr_field_name_obj
, &field_name
);
194 BT_LOGE("Cannot get raw value from string value: value-addr=%p",
195 attr_field_name_obj
);
199 if (!strcmp(field_name
, name
)) {
200 BT_PUT(attr_field_name_obj
);
204 BT_PUT(attr_field_name_obj
);
211 BT_PUT(attr_field_name_obj
);
218 int bt_ctf_attributes_set_field_value(struct bt_value
*attr_obj
,
219 const char *name
, struct bt_value
*value_obj
)
222 struct bt_value
*attr_field_obj
= NULL
;
224 if (!attr_obj
|| !name
|| !value_obj
) {
225 BT_LOGW("Invalid parameter: attributes object, name, or value object is NULL: "
226 "attr-value-addr=%p, name-addr=%p, value-addr=%p",
227 attr_obj
, name
, value_obj
);
232 attr_field_obj
= bt_ctf_attributes_get_field_by_name(attr_obj
, name
);
233 if (attr_field_obj
) {
234 ret
= bt_value_array_set(attr_field_obj
,
235 BT_CTF_ATTR_VALUE_INDEX
, value_obj
);
239 attr_field_obj
= bt_value_array_create();
240 if (!attr_field_obj
) {
241 BT_LOGE_STR("Failed to create empty array value.");
246 ret
= bt_value_array_append_string(attr_field_obj
, name
);
247 ret
|= bt_value_array_append(attr_field_obj
, value_obj
);
249 BT_LOGE("Cannot append elements to array value: addr=%p",
254 ret
= bt_value_array_append(attr_obj
, attr_field_obj
);
256 BT_LOGE("Cannot append element to array value: "
257 "array-value-addr=%p, element-value-addr=%p",
258 attr_obj
, attr_field_obj
);
262 BT_PUT(attr_field_obj
);
268 struct bt_value
*bt_ctf_attributes_get_field_value_by_name(
269 struct bt_value
*attr_obj
, const char *name
)
271 struct bt_value
*value_obj
= NULL
;
272 struct bt_value
*attr_field_obj
= NULL
;
274 if (!attr_obj
|| !name
) {
275 BT_LOGW("Invalid parameter: attributes object or name is NULL: "
276 "value-addr=%p, name-addr=%p", attr_obj
, name
);
280 attr_field_obj
= bt_ctf_attributes_get_field_by_name(attr_obj
, name
);
281 if (!attr_field_obj
) {
282 BT_LOGD("Cannot find attributes object's field by name: "
283 "value-addr=%p, name=\"%s\"", attr_obj
, name
);
287 value_obj
= bt_value_array_get(attr_field_obj
,
288 BT_CTF_ATTR_VALUE_INDEX
);
290 BT_LOGE("Cannot get attribute array value's element by index: "
291 "value-addr=%p, index=%" PRIu64
, attr_field_obj
,
292 (uint64_t) BT_CTF_ATTR_VALUE_INDEX
);
296 BT_PUT(attr_field_obj
);
302 int bt_ctf_attributes_freeze(struct bt_value
*attr_obj
)
309 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
314 BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj
);
315 count
= bt_value_array_size(attr_obj
);
317 BT_LOGE("Cannot get array value's size: value-addr=%p",
324 * We do not freeze the array value object itself here, since
325 * internal stuff could need to modify/add attributes. Each
326 * attribute is frozen one by one.
328 for (i
= 0; i
< count
; ++i
) {
329 struct bt_value
*obj
= NULL
;
331 obj
= bt_ctf_attributes_get_field_value(attr_obj
, i
);
333 BT_LOGE("Cannot get attributes object's field value by index: "
334 "value-addr=%p, index=%" PRIu64
,
340 bt_value_freeze(obj
);
This page took 0.045885 seconds and 4 git commands to generate.