Logging: standardize logging tags
[babeltrace.git] / src / ctf-writer / attributes.c
CommitLineData
16ca5ff0
PP
1/*
2 * attributes.c
3 *
4 * Babeltrace CTF writer - Attributes
5 *
6 * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation
7 * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com>
8 *
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:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
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
25 * SOFTWARE.
26 */
27
350ad6c1 28#define BT_LOG_TAG "CTF-WRITER/ATTRS"
67d2ce02 29#include "logging.h"
16ca5ff0 30
578e048b 31#include "common/assert.h"
91d81473 32#include "common/macros.h"
578e048b 33#include "compat/string.h"
3fadfbc0 34#include <babeltrace2/ctf-writer/object.h>
16ca5ff0
PP
35#include <inttypes.h>
36
578e048b
MJ
37#include "values.h"
38
16ca5ff0
PP
39#define BT_CTF_ATTR_NAME_INDEX 0
40#define BT_CTF_ATTR_VALUE_INDEX 1
41
42BT_HIDDEN
e1e02a22 43struct bt_ctf_private_value *bt_ctf_attributes_create(void)
16ca5ff0 44{
e1e02a22 45 struct bt_ctf_private_value *attr_obj;
16ca5ff0
PP
46
47 /*
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).
51 *
52 * Example (JSON representation):
53 *
54 * [
55 * ["hostname", "eeppdesk"],
56 * ["sysname", "Linux"],
57 * ["tracer_major", 2],
58 * ["tracer_minor", 5]
59 * ]
60 */
61 BT_LOGD_STR("Creating attributes object.");
e1e02a22 62 attr_obj = bt_ctf_private_value_array_create();
16ca5ff0
PP
63 if (!attr_obj) {
64 BT_LOGE_STR("Failed to create array value.");
65 } else {
66 BT_LOGD("Created attributes object: addr=%p",
67 attr_obj);
68 }
69
70 return attr_obj;
71}
72
73BT_HIDDEN
e1e02a22 74void bt_ctf_attributes_destroy(struct bt_ctf_private_value *attr_obj)
16ca5ff0
PP
75{
76 BT_LOGD("Destroying attributes object: addr=%p", attr_obj);
e1e02a22 77 bt_ctf_object_put_ref(attr_obj);
16ca5ff0
PP
78}
79
80BT_HIDDEN
e1e02a22 81int64_t bt_ctf_attributes_get_count(struct bt_ctf_private_value *attr_obj)
16ca5ff0 82{
e1e02a22 83 return bt_ctf_value_array_get_size(bt_ctf_private_value_as_value(attr_obj));
16ca5ff0
PP
84}
85
86BT_HIDDEN
e1e02a22 87const char *bt_ctf_attributes_get_field_name(struct bt_ctf_private_value *attr_obj,
16ca5ff0
PP
88 uint64_t index)
89{
16ca5ff0 90 const char *ret = NULL;
e1e02a22
PP
91 struct bt_ctf_private_value *attr_field_obj = NULL;
92 struct bt_ctf_private_value *attr_field_name_obj = NULL;
16ca5ff0
PP
93
94 if (!attr_obj) {
95 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
96 goto end;
97 }
98
e1e02a22 99 if (index >= bt_ctf_value_array_get_size(bt_ctf_private_value_as_value(attr_obj))) {
16ca5ff0
PP
100 BT_LOGW("Invalid parameter: index is out of bounds: "
101 "index=%" PRIu64 ", count=%" PRId64,
e1e02a22 102 index, bt_ctf_value_array_get_size(bt_ctf_private_value_as_value(attr_obj)));
16ca5ff0
PP
103 goto end;
104 }
105
e1e02a22 106 attr_field_obj = bt_ctf_private_value_array_borrow_element_by_index(
da91b29a 107 attr_obj, index);
16ca5ff0
PP
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);
111 goto end;
112 }
113
da91b29a 114 attr_field_name_obj =
e1e02a22 115 bt_ctf_private_value_array_borrow_element_by_index(
da91b29a 116 attr_field_obj, BT_CTF_ATTR_NAME_INDEX);
16ca5ff0
PP
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);
121 goto end;
122 }
123
e1e02a22
PP
124 ret = bt_ctf_value_string_get(
125 bt_ctf_private_value_as_value(attr_field_name_obj));
16ca5ff0
PP
126
127end:
128 return ret;
129}
130
131BT_HIDDEN
e1e02a22 132struct bt_ctf_private_value *bt_ctf_attributes_borrow_field_value(struct bt_ctf_private_value *attr_obj,
16ca5ff0
PP
133 uint64_t index)
134{
e1e02a22
PP
135 struct bt_ctf_private_value *value_obj = NULL;
136 struct bt_ctf_private_value *attr_field_obj = NULL;
16ca5ff0
PP
137
138 if (!attr_obj) {
139 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
140 goto end;
141 }
142
e1e02a22 143 if (index >= bt_ctf_value_array_get_size(bt_ctf_private_value_as_value(attr_obj))) {
16ca5ff0
PP
144 BT_LOGW("Invalid parameter: index is out of bounds: "
145 "index=%" PRIu64 ", count=%" PRId64,
e1e02a22 146 index, bt_ctf_value_array_get_size(bt_ctf_private_value_as_value(attr_obj)));
16ca5ff0
PP
147 goto end;
148 }
149
e1e02a22 150 attr_field_obj = bt_ctf_private_value_array_borrow_element_by_index(
da91b29a 151 attr_obj, index);
16ca5ff0
PP
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);
155 goto end;
156 }
157
e1e02a22 158 value_obj = bt_ctf_private_value_array_borrow_element_by_index(attr_field_obj,
16ca5ff0
PP
159 BT_CTF_ATTR_VALUE_INDEX);
160 if (!value_obj) {
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);
164 }
165
166end:
167 return value_obj;
168}
169
170static
e1e02a22
PP
171struct bt_ctf_private_value *bt_ctf_attributes_borrow_field_by_name(
172 struct bt_ctf_private_value *attr_obj, const char *name)
16ca5ff0
PP
173{
174 uint64_t i;
175 int64_t attr_size;
e1e02a22
PP
176 struct bt_ctf_private_value *value_obj = NULL;
177 struct bt_ctf_private_value *attr_field_name_obj = NULL;
16ca5ff0 178
e1e02a22 179 attr_size = bt_ctf_value_array_get_size(bt_ctf_private_value_as_value(attr_obj));
16ca5ff0
PP
180 if (attr_size < 0) {
181 BT_LOGE("Cannot get array value's size: value-addr=%p",
182 attr_obj);
183 goto error;
184 }
185
186 for (i = 0; i < attr_size; ++i) {
16ca5ff0
PP
187 const char *field_name;
188
e1e02a22 189 value_obj = bt_ctf_private_value_array_borrow_element_by_index(attr_obj, i);
16ca5ff0
PP
190 if (!value_obj) {
191 BT_LOGE("Cannot get attributes object's array value's element by index: "
192 "value-addr=%p, index=%" PRIu64, attr_obj, i);
193 goto error;
194 }
195
e1e02a22 196 attr_field_name_obj = bt_ctf_private_value_array_borrow_element_by_index(value_obj,
16ca5ff0
PP
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);
202 goto error;
203 }
204
e1e02a22
PP
205 field_name = bt_ctf_value_string_get(
206 bt_ctf_private_value_as_value(attr_field_name_obj));
16ca5ff0
PP
207
208 if (!strcmp(field_name, name)) {
209 break;
210 }
211
212 value_obj = NULL;
213 }
214
215 return value_obj;
216
217error:
218 value_obj = NULL;
219 return value_obj;
220}
221
222BT_HIDDEN
e1e02a22
PP
223int bt_ctf_attributes_set_field_value(struct bt_ctf_private_value *attr_obj,
224 const char *name, struct bt_ctf_private_value *value_obj)
16ca5ff0
PP
225{
226 int ret = 0;
e1e02a22 227 struct bt_ctf_private_value *attr_field_obj = NULL;
16ca5ff0
PP
228
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);
233 ret = -1;
234 goto end;
235 }
236
237 attr_field_obj = bt_ctf_attributes_borrow_field_by_name(attr_obj, name);
238 if (attr_field_obj) {
e1e02a22 239 ret = bt_ctf_private_value_array_set_element_by_index(
da91b29a 240 attr_field_obj, BT_CTF_ATTR_VALUE_INDEX,
e1e02a22 241 bt_ctf_private_value_as_value(value_obj));
16ca5ff0
PP
242 attr_field_obj = NULL;
243 goto end;
244 }
245
e1e02a22 246 attr_field_obj = bt_ctf_private_value_array_create();
16ca5ff0
PP
247 if (!attr_field_obj) {
248 BT_LOGE_STR("Failed to create empty array value.");
249 ret = -1;
250 goto end;
251 }
252
e1e02a22
PP
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));
16ca5ff0
PP
256 if (ret) {
257 BT_LOGE("Cannot append elements to array value: addr=%p",
258 attr_field_obj);
259 goto end;
260 }
261
e1e02a22
PP
262 ret = bt_ctf_private_value_array_append_element(attr_obj,
263 bt_ctf_private_value_as_value(attr_field_obj));
16ca5ff0
PP
264 if (ret) {
265 BT_LOGE("Cannot append element to array value: "
266 "array-value-addr=%p, element-value-addr=%p",
267 attr_obj, attr_field_obj);
268 }
269
270end:
e1e02a22 271 bt_ctf_object_put_ref(attr_field_obj);
16ca5ff0
PP
272 return ret;
273}
274
275BT_HIDDEN
e1e02a22
PP
276struct bt_ctf_private_value *bt_ctf_attributes_borrow_field_value_by_name(
277 struct bt_ctf_private_value *attr_obj, const char *name)
16ca5ff0 278{
e1e02a22
PP
279 struct bt_ctf_private_value *value_obj = NULL;
280 struct bt_ctf_private_value *attr_field_obj = NULL;
16ca5ff0
PP
281
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);
285 goto end;
286 }
287
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);
292 goto end;
293 }
294
e1e02a22 295 value_obj = bt_ctf_private_value_array_borrow_element_by_index(attr_field_obj,
16ca5ff0
PP
296 BT_CTF_ATTR_VALUE_INDEX);
297 if (!value_obj) {
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);
301 }
302
303end:
304 return value_obj;
305}
306
307BT_HIDDEN
e1e02a22 308int bt_ctf_attributes_freeze(struct bt_ctf_private_value *attr_obj)
16ca5ff0
PP
309{
310 uint64_t i;
311 int64_t count;
312 int ret = 0;
313
314 if (!attr_obj) {
315 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
316 ret = -1;
317 goto end;
318 }
319
320 BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj);
e1e02a22 321 count = bt_ctf_value_array_get_size(bt_ctf_private_value_as_value(attr_obj));
16ca5ff0
PP
322 BT_ASSERT(count >= 0);
323
324 /*
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.
328 */
329 for (i = 0; i < count; ++i) {
e1e02a22 330 struct bt_ctf_private_value *obj = NULL;
16ca5ff0
PP
331
332 obj = bt_ctf_attributes_borrow_field_value(attr_obj, i);
333 if (!obj) {
334 BT_LOGE("Cannot get attributes object's field value by index: "
335 "value-addr=%p, index=%" PRIu64,
336 attr_obj, i);
337 ret = -1;
338 goto end;
339 }
340
e1e02a22 341 bt_ctf_value_freeze(bt_ctf_private_value_as_value(obj));
16ca5ff0
PP
342 }
343
344end:
345 return ret;
346}
This page took 0.053833 seconds and 4 git commands to generate.