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