Fix -Wmissing-prototypes/-Wmissing-declarations warnings
[babeltrace.git] / src / ctf-writer / attributes.c
CommitLineData
086dc475
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
b03487ab 28#define BT_LOG_TAG "CTF-WRITER/ATTRS"
bf436657 29#include "logging.h"
086dc475 30
f3847c75
SM
31#include "attributes.h"
32
57952005 33#include "common/assert.h"
85e7137b 34#include "common/macros.h"
57952005 35#include "compat/string.h"
3996bc53 36#include <babeltrace2-ctf-writer/object.h>
086dc475
PP
37#include <inttypes.h>
38
57952005
MJ
39#include "values.h"
40
086dc475
PP
41#define BT_CTF_ATTR_NAME_INDEX 0
42#define BT_CTF_ATTR_VALUE_INDEX 1
43
44BT_HIDDEN
c2606e2f 45struct bt_ctf_private_value *bt_ctf_attributes_create(void)
086dc475 46{
c2606e2f 47 struct bt_ctf_private_value *attr_obj;
086dc475
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.");
c2606e2f 64 attr_obj = bt_ctf_private_value_array_create();
086dc475
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
c2606e2f 76void bt_ctf_attributes_destroy(struct bt_ctf_private_value *attr_obj)
086dc475
PP
77{
78 BT_LOGD("Destroying attributes object: addr=%p", attr_obj);
c2606e2f 79 bt_ctf_object_put_ref(attr_obj);
086dc475
PP
80}
81
82BT_HIDDEN
c2606e2f 83int64_t bt_ctf_attributes_get_count(struct bt_ctf_private_value *attr_obj)
086dc475 84{
1270d0e9 85 return bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj));
086dc475
PP
86}
87
88BT_HIDDEN
c2606e2f 89const char *bt_ctf_attributes_get_field_name(struct bt_ctf_private_value *attr_obj,
086dc475
PP
90 uint64_t index)
91{
086dc475 92 const char *ret = NULL;
c2606e2f
PP
93 struct bt_ctf_private_value *attr_field_obj = NULL;
94 struct bt_ctf_private_value *attr_field_name_obj = NULL;
086dc475
PP
95
96 if (!attr_obj) {
97 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
98 goto end;
99 }
100
1270d0e9 101 if (index >= bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj))) {
086dc475
PP
102 BT_LOGW("Invalid parameter: index is out of bounds: "
103 "index=%" PRIu64 ", count=%" PRId64,
1270d0e9 104 index, bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj)));
086dc475
PP
105 goto end;
106 }
107
c2606e2f 108 attr_field_obj = bt_ctf_private_value_array_borrow_element_by_index(
17582c6d 109 attr_obj, index);
086dc475
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
17582c6d 116 attr_field_name_obj =
c2606e2f 117 bt_ctf_private_value_array_borrow_element_by_index(
17582c6d 118 attr_field_obj, BT_CTF_ATTR_NAME_INDEX);
086dc475
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
c2606e2f
PP
126 ret = bt_ctf_value_string_get(
127 bt_ctf_private_value_as_value(attr_field_name_obj));
086dc475
PP
128
129end:
130 return ret;
131}
132
133BT_HIDDEN
c2606e2f 134struct bt_ctf_private_value *bt_ctf_attributes_borrow_field_value(struct bt_ctf_private_value *attr_obj,
086dc475
PP
135 uint64_t index)
136{
c2606e2f
PP
137 struct bt_ctf_private_value *value_obj = NULL;
138 struct bt_ctf_private_value *attr_field_obj = NULL;
086dc475
PP
139
140 if (!attr_obj) {
141 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
142 goto end;
143 }
144
1270d0e9 145 if (index >= bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj))) {
086dc475
PP
146 BT_LOGW("Invalid parameter: index is out of bounds: "
147 "index=%" PRIu64 ", count=%" PRId64,
1270d0e9 148 index, bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj)));
086dc475
PP
149 goto end;
150 }
151
c2606e2f 152 attr_field_obj = bt_ctf_private_value_array_borrow_element_by_index(
17582c6d 153 attr_obj, index);
086dc475
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
c2606e2f 160 value_obj = bt_ctf_private_value_array_borrow_element_by_index(attr_field_obj,
086dc475
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
c2606e2f
PP
173struct bt_ctf_private_value *bt_ctf_attributes_borrow_field_by_name(
174 struct bt_ctf_private_value *attr_obj, const char *name)
086dc475
PP
175{
176 uint64_t i;
177 int64_t attr_size;
c2606e2f
PP
178 struct bt_ctf_private_value *value_obj = NULL;
179 struct bt_ctf_private_value *attr_field_name_obj = NULL;
086dc475 180
1270d0e9 181 attr_size = bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj));
086dc475
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) {
086dc475
PP
189 const char *field_name;
190
c2606e2f 191 value_obj = bt_ctf_private_value_array_borrow_element_by_index(attr_obj, i);
086dc475
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
c2606e2f 198 attr_field_name_obj = bt_ctf_private_value_array_borrow_element_by_index(value_obj,
086dc475
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
c2606e2f
PP
207 field_name = bt_ctf_value_string_get(
208 bt_ctf_private_value_as_value(attr_field_name_obj));
086dc475 209
acfa8112 210 if (strcmp(field_name, name) == 0) {
086dc475
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
c2606e2f
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)
086dc475
PP
227{
228 int ret = 0;
c2606e2f 229 struct bt_ctf_private_value *attr_field_obj = NULL;
086dc475
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) {
c2606e2f 241 ret = bt_ctf_private_value_array_set_element_by_index(
17582c6d 242 attr_field_obj, BT_CTF_ATTR_VALUE_INDEX,
c2606e2f 243 bt_ctf_private_value_as_value(value_obj));
086dc475
PP
244 attr_field_obj = NULL;
245 goto end;
246 }
247
c2606e2f 248 attr_field_obj = bt_ctf_private_value_array_create();
086dc475
PP
249 if (!attr_field_obj) {
250 BT_LOGE_STR("Failed to create empty array value.");
251 ret = -1;
252 goto end;
253 }
254
c2606e2f
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));
086dc475
PP
258 if (ret) {
259 BT_LOGE("Cannot append elements to array value: addr=%p",
260 attr_field_obj);
261 goto end;
262 }
263
c2606e2f
PP
264 ret = bt_ctf_private_value_array_append_element(attr_obj,
265 bt_ctf_private_value_as_value(attr_field_obj));
086dc475
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:
c2606e2f 273 bt_ctf_object_put_ref(attr_field_obj);
086dc475
PP
274 return ret;
275}
276
277BT_HIDDEN
c2606e2f
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)
086dc475 280{
c2606e2f
PP
281 struct bt_ctf_private_value *value_obj = NULL;
282 struct bt_ctf_private_value *attr_field_obj = NULL;
086dc475
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
c2606e2f 297 value_obj = bt_ctf_private_value_array_borrow_element_by_index(attr_field_obj,
086dc475
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
c2606e2f 310int bt_ctf_attributes_freeze(struct bt_ctf_private_value *attr_obj)
086dc475
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);
1270d0e9 323 count = bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj));
ec4a3354 324 BT_ASSERT_DBG(count >= 0);
086dc475
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) {
c2606e2f 332 struct bt_ctf_private_value *obj = NULL;
086dc475
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
c2606e2f 343 bt_ctf_value_freeze(bt_ctf_private_value_as_value(obj));
086dc475
PP
344 }
345
346end:
347 return ret;
348}
This page took 0.064007 seconds and 4 git commands to generate.