Fix: src.ctf.fs: initialize the other_entry variable
[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
7c7301d5
SM
31#include "attributes.h"
32
578e048b 33#include "common/assert.h"
91d81473 34#include "common/macros.h"
578e048b 35#include "compat/string.h"
217cf9d3 36#include <babeltrace2-ctf-writer/object.h>
16ca5ff0
PP
37#include <inttypes.h>
38
578e048b
MJ
39#include "values.h"
40
16ca5ff0
PP
41#define BT_CTF_ATTR_NAME_INDEX 0
42#define BT_CTF_ATTR_VALUE_INDEX 1
43
44BT_HIDDEN
e1e02a22 45struct bt_ctf_private_value *bt_ctf_attributes_create(void)
16ca5ff0 46{
e1e02a22 47 struct bt_ctf_private_value *attr_obj;
16ca5ff0
PP
48
49 /*
50 * Attributes: array value object of array value objects, each one
51 * containing two entries: a string value object (attributes
52 * field name), and a value object (attributes field value).
53 *
54 * Example (JSON representation):
55 *
56 * [
57 * ["hostname", "eeppdesk"],
58 * ["sysname", "Linux"],
59 * ["tracer_major", 2],
60 * ["tracer_minor", 5]
61 * ]
62 */
63 BT_LOGD_STR("Creating attributes object.");
e1e02a22 64 attr_obj = bt_ctf_private_value_array_create();
16ca5ff0
PP
65 if (!attr_obj) {
66 BT_LOGE_STR("Failed to create array value.");
67 } else {
68 BT_LOGD("Created attributes object: addr=%p",
69 attr_obj);
70 }
71
72 return attr_obj;
73}
74
75BT_HIDDEN
e1e02a22 76void bt_ctf_attributes_destroy(struct bt_ctf_private_value *attr_obj)
16ca5ff0
PP
77{
78 BT_LOGD("Destroying attributes object: addr=%p", attr_obj);
e1e02a22 79 bt_ctf_object_put_ref(attr_obj);
16ca5ff0
PP
80}
81
82BT_HIDDEN
e1e02a22 83int64_t bt_ctf_attributes_get_count(struct bt_ctf_private_value *attr_obj)
16ca5ff0 84{
393729a6 85 return bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj));
16ca5ff0
PP
86}
87
88BT_HIDDEN
e1e02a22 89const char *bt_ctf_attributes_get_field_name(struct bt_ctf_private_value *attr_obj,
16ca5ff0
PP
90 uint64_t index)
91{
16ca5ff0 92 const char *ret = NULL;
e1e02a22
PP
93 struct bt_ctf_private_value *attr_field_obj = NULL;
94 struct bt_ctf_private_value *attr_field_name_obj = NULL;
16ca5ff0
PP
95
96 if (!attr_obj) {
97 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
98 goto end;
99 }
100
393729a6 101 if (index >= bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj))) {
16ca5ff0
PP
102 BT_LOGW("Invalid parameter: index is out of bounds: "
103 "index=%" PRIu64 ", count=%" PRId64,
393729a6 104 index, bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj)));
16ca5ff0
PP
105 goto end;
106 }
107
e1e02a22 108 attr_field_obj = bt_ctf_private_value_array_borrow_element_by_index(
da91b29a 109 attr_obj, index);
16ca5ff0
PP
110 if (!attr_field_obj) {
111 BT_LOGE("Cannot get attributes object's array value's element by index: "
112 "value-addr=%p, index=%" PRIu64, attr_obj, index);
113 goto end;
114 }
115
da91b29a 116 attr_field_name_obj =
e1e02a22 117 bt_ctf_private_value_array_borrow_element_by_index(
da91b29a 118 attr_field_obj, BT_CTF_ATTR_NAME_INDEX);
16ca5ff0
PP
119 if (!attr_field_name_obj) {
120 BT_LOGE("Cannot get attribute array value's element by index: "
121 "value-addr=%p, index=%" PRIu64, attr_field_obj,
122 (uint64_t) BT_CTF_ATTR_NAME_INDEX);
123 goto end;
124 }
125
e1e02a22
PP
126 ret = bt_ctf_value_string_get(
127 bt_ctf_private_value_as_value(attr_field_name_obj));
16ca5ff0
PP
128
129end:
130 return ret;
131}
132
133BT_HIDDEN
e1e02a22 134struct bt_ctf_private_value *bt_ctf_attributes_borrow_field_value(struct bt_ctf_private_value *attr_obj,
16ca5ff0
PP
135 uint64_t index)
136{
e1e02a22
PP
137 struct bt_ctf_private_value *value_obj = NULL;
138 struct bt_ctf_private_value *attr_field_obj = NULL;
16ca5ff0
PP
139
140 if (!attr_obj) {
141 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
142 goto end;
143 }
144
393729a6 145 if (index >= bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj))) {
16ca5ff0
PP
146 BT_LOGW("Invalid parameter: index is out of bounds: "
147 "index=%" PRIu64 ", count=%" PRId64,
393729a6 148 index, bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj)));
16ca5ff0
PP
149 goto end;
150 }
151
e1e02a22 152 attr_field_obj = bt_ctf_private_value_array_borrow_element_by_index(
da91b29a 153 attr_obj, index);
16ca5ff0
PP
154 if (!attr_field_obj) {
155 BT_LOGE("Cannot get attributes object's array value's element by index: "
156 "value-addr=%p, index=%" PRIu64, attr_obj, index);
157 goto end;
158 }
159
e1e02a22 160 value_obj = bt_ctf_private_value_array_borrow_element_by_index(attr_field_obj,
16ca5ff0
PP
161 BT_CTF_ATTR_VALUE_INDEX);
162 if (!value_obj) {
163 BT_LOGE("Cannot get attribute array value's element by index: "
164 "value-addr=%p, index=%" PRIu64, attr_field_obj,
165 (uint64_t) BT_CTF_ATTR_VALUE_INDEX);
166 }
167
168end:
169 return value_obj;
170}
171
172static
e1e02a22
PP
173struct bt_ctf_private_value *bt_ctf_attributes_borrow_field_by_name(
174 struct bt_ctf_private_value *attr_obj, const char *name)
16ca5ff0
PP
175{
176 uint64_t i;
177 int64_t attr_size;
e1e02a22
PP
178 struct bt_ctf_private_value *value_obj = NULL;
179 struct bt_ctf_private_value *attr_field_name_obj = NULL;
16ca5ff0 180
393729a6 181 attr_size = bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj));
16ca5ff0
PP
182 if (attr_size < 0) {
183 BT_LOGE("Cannot get array value's size: value-addr=%p",
184 attr_obj);
185 goto error;
186 }
187
188 for (i = 0; i < attr_size; ++i) {
16ca5ff0
PP
189 const char *field_name;
190
e1e02a22 191 value_obj = bt_ctf_private_value_array_borrow_element_by_index(attr_obj, i);
16ca5ff0
PP
192 if (!value_obj) {
193 BT_LOGE("Cannot get attributes object's array value's element by index: "
194 "value-addr=%p, index=%" PRIu64, attr_obj, i);
195 goto error;
196 }
197
e1e02a22 198 attr_field_name_obj = bt_ctf_private_value_array_borrow_element_by_index(value_obj,
16ca5ff0
PP
199 BT_CTF_ATTR_NAME_INDEX);
200 if (!attr_field_name_obj) {
201 BT_LOGE("Cannot get attribute array value's element by index: "
202 "value-addr=%p, index=%" PRIu64,
203 value_obj, (int64_t) BT_CTF_ATTR_NAME_INDEX);
204 goto error;
205 }
206
e1e02a22
PP
207 field_name = bt_ctf_value_string_get(
208 bt_ctf_private_value_as_value(attr_field_name_obj));
16ca5ff0 209
2242b43d 210 if (strcmp(field_name, name) == 0) {
16ca5ff0
PP
211 break;
212 }
213
214 value_obj = NULL;
215 }
216
217 return value_obj;
218
219error:
220 value_obj = NULL;
221 return value_obj;
222}
223
224BT_HIDDEN
e1e02a22
PP
225int bt_ctf_attributes_set_field_value(struct bt_ctf_private_value *attr_obj,
226 const char *name, struct bt_ctf_private_value *value_obj)
16ca5ff0
PP
227{
228 int ret = 0;
e1e02a22 229 struct bt_ctf_private_value *attr_field_obj = NULL;
16ca5ff0
PP
230
231 if (!attr_obj || !name || !value_obj) {
232 BT_LOGW("Invalid parameter: attributes object, name, or value object is NULL: "
233 "attr-value-addr=%p, name-addr=%p, value-addr=%p",
234 attr_obj, name, value_obj);
235 ret = -1;
236 goto end;
237 }
238
239 attr_field_obj = bt_ctf_attributes_borrow_field_by_name(attr_obj, name);
240 if (attr_field_obj) {
e1e02a22 241 ret = bt_ctf_private_value_array_set_element_by_index(
da91b29a 242 attr_field_obj, BT_CTF_ATTR_VALUE_INDEX,
e1e02a22 243 bt_ctf_private_value_as_value(value_obj));
16ca5ff0
PP
244 attr_field_obj = NULL;
245 goto end;
246 }
247
e1e02a22 248 attr_field_obj = bt_ctf_private_value_array_create();
16ca5ff0
PP
249 if (!attr_field_obj) {
250 BT_LOGE_STR("Failed to create empty array value.");
251 ret = -1;
252 goto end;
253 }
254
e1e02a22
PP
255 ret = bt_ctf_private_value_array_append_string_element(attr_field_obj, name);
256 ret |= bt_ctf_private_value_array_append_element(attr_field_obj,
257 bt_ctf_private_value_as_value(value_obj));
16ca5ff0
PP
258 if (ret) {
259 BT_LOGE("Cannot append elements to array value: addr=%p",
260 attr_field_obj);
261 goto end;
262 }
263
e1e02a22
PP
264 ret = bt_ctf_private_value_array_append_element(attr_obj,
265 bt_ctf_private_value_as_value(attr_field_obj));
16ca5ff0
PP
266 if (ret) {
267 BT_LOGE("Cannot append element to array value: "
268 "array-value-addr=%p, element-value-addr=%p",
269 attr_obj, attr_field_obj);
270 }
271
272end:
e1e02a22 273 bt_ctf_object_put_ref(attr_field_obj);
16ca5ff0
PP
274 return ret;
275}
276
277BT_HIDDEN
e1e02a22
PP
278struct bt_ctf_private_value *bt_ctf_attributes_borrow_field_value_by_name(
279 struct bt_ctf_private_value *attr_obj, const char *name)
16ca5ff0 280{
e1e02a22
PP
281 struct bt_ctf_private_value *value_obj = NULL;
282 struct bt_ctf_private_value *attr_field_obj = NULL;
16ca5ff0
PP
283
284 if (!attr_obj || !name) {
285 BT_LOGW("Invalid parameter: attributes object or name is NULL: "
286 "value-addr=%p, name-addr=%p", attr_obj, name);
287 goto end;
288 }
289
290 attr_field_obj = bt_ctf_attributes_borrow_field_by_name(attr_obj, name);
291 if (!attr_field_obj) {
292 BT_LOGD("Cannot find attributes object's field by name: "
293 "value-addr=%p, name=\"%s\"", attr_obj, name);
294 goto end;
295 }
296
e1e02a22 297 value_obj = bt_ctf_private_value_array_borrow_element_by_index(attr_field_obj,
16ca5ff0
PP
298 BT_CTF_ATTR_VALUE_INDEX);
299 if (!value_obj) {
300 BT_LOGE("Cannot get attribute array value's element by index: "
301 "value-addr=%p, index=%" PRIu64, attr_field_obj,
302 (uint64_t) BT_CTF_ATTR_VALUE_INDEX);
303 }
304
305end:
306 return value_obj;
307}
308
309BT_HIDDEN
e1e02a22 310int bt_ctf_attributes_freeze(struct bt_ctf_private_value *attr_obj)
16ca5ff0
PP
311{
312 uint64_t i;
313 int64_t count;
314 int ret = 0;
315
316 if (!attr_obj) {
317 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
318 ret = -1;
319 goto end;
320 }
321
322 BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj);
393729a6 323 count = bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj));
98b15851 324 BT_ASSERT_DBG(count >= 0);
16ca5ff0
PP
325
326 /*
327 * We do not freeze the array value object itself here, since
328 * internal stuff could need to modify/add attributes. Each
329 * attribute is frozen one by one.
330 */
331 for (i = 0; i < count; ++i) {
e1e02a22 332 struct bt_ctf_private_value *obj = NULL;
16ca5ff0
PP
333
334 obj = bt_ctf_attributes_borrow_field_value(attr_obj, i);
335 if (!obj) {
336 BT_LOGE("Cannot get attributes object's field value by index: "
337 "value-addr=%p, index=%" PRIu64,
338 attr_obj, i);
339 ret = -1;
340 goto end;
341 }
342
e1e02a22 343 bt_ctf_value_freeze(bt_ctf_private_value_as_value(obj));
16ca5ff0
PP
344 }
345
346end:
347 return ret;
348}
This page took 0.07552 seconds and 4 git commands to generate.