Commit | Line | Data |
---|---|---|
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 | ||
28 | #define BT_LOG_TAG "CTF-WRITER-ATTRS" | |
29 | #include <babeltrace/lib-logging-internal.h> | |
30 | ||
31 | #include <babeltrace/assert-internal.h> | |
32 | #include <babeltrace/babeltrace-internal.h> | |
33 | #include <babeltrace/compat/string-internal.h> | |
65300d60 | 34 | #include <babeltrace/object.h> |
16ca5ff0 PP |
35 | #include <babeltrace/values-internal.h> |
36 | #include <babeltrace/values.h> | |
da91b29a | 37 | #include <babeltrace/private-values.h> |
16ca5ff0 PP |
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 | |
da91b29a | 44 | struct bt_private_value *bt_ctf_attributes_create(void) |
16ca5ff0 | 45 | { |
da91b29a | 46 | struct bt_private_value *attr_obj; |
16ca5ff0 PP |
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."); | |
da91b29a | 63 | attr_obj = bt_private_value_array_create(); |
16ca5ff0 PP |
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 | |
da91b29a | 75 | void bt_ctf_attributes_destroy(struct bt_private_value *attr_obj) |
16ca5ff0 PP |
76 | { |
77 | BT_LOGD("Destroying attributes object: addr=%p", attr_obj); | |
65300d60 | 78 | bt_object_put_ref(attr_obj); |
16ca5ff0 PP |
79 | } |
80 | ||
81 | BT_HIDDEN | |
da91b29a | 82 | int64_t bt_ctf_attributes_get_count(struct bt_private_value *attr_obj) |
16ca5ff0 | 83 | { |
da91b29a | 84 | return bt_value_array_get_size(bt_value_borrow_from_private(attr_obj)); |
16ca5ff0 PP |
85 | } |
86 | ||
87 | BT_HIDDEN | |
da91b29a | 88 | const char *bt_ctf_attributes_get_field_name(struct bt_private_value *attr_obj, |
16ca5ff0 PP |
89 | uint64_t index) |
90 | { | |
91 | int rc; | |
92 | const char *ret = NULL; | |
da91b29a PP |
93 | struct bt_private_value *attr_field_obj = NULL; |
94 | struct bt_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 | ||
da91b29a | 101 | if (index >= bt_value_array_get_size(bt_value_borrow_from_private(attr_obj))) { |
16ca5ff0 PP |
102 | BT_LOGW("Invalid parameter: index is out of bounds: " |
103 | "index=%" PRIu64 ", count=%" PRId64, | |
da91b29a | 104 | index, bt_value_array_get_size(bt_value_borrow_from_private(attr_obj))); |
16ca5ff0 PP |
105 | goto end; |
106 | } | |
107 | ||
da91b29a PP |
108 | attr_field_obj = bt_private_value_array_borrow_element_by_index( |
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 PP |
116 | attr_field_name_obj = |
117 | bt_private_value_array_borrow_element_by_index( | |
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 | ||
da91b29a PP |
126 | rc = bt_value_string_get( |
127 | bt_value_borrow_from_private(attr_field_name_obj), &ret); | |
16ca5ff0 PP |
128 | if (rc) { |
129 | BT_LOGE("Cannot get raw value from string value: value-addr=%p", | |
130 | attr_field_name_obj); | |
131 | ret = NULL; | |
132 | } | |
133 | ||
134 | end: | |
135 | return ret; | |
136 | } | |
137 | ||
138 | BT_HIDDEN | |
da91b29a | 139 | struct bt_private_value *bt_ctf_attributes_borrow_field_value(struct bt_private_value *attr_obj, |
16ca5ff0 PP |
140 | uint64_t index) |
141 | { | |
da91b29a PP |
142 | struct bt_private_value *value_obj = NULL; |
143 | struct bt_private_value *attr_field_obj = NULL; | |
16ca5ff0 PP |
144 | |
145 | if (!attr_obj) { | |
146 | BT_LOGW_STR("Invalid parameter: attributes object is NULL."); | |
147 | goto end; | |
148 | } | |
149 | ||
da91b29a | 150 | if (index >= bt_value_array_get_size(bt_value_borrow_from_private(attr_obj))) { |
16ca5ff0 PP |
151 | BT_LOGW("Invalid parameter: index is out of bounds: " |
152 | "index=%" PRIu64 ", count=%" PRId64, | |
da91b29a | 153 | index, bt_value_array_get_size(bt_value_borrow_from_private(attr_obj))); |
16ca5ff0 PP |
154 | goto end; |
155 | } | |
156 | ||
da91b29a PP |
157 | attr_field_obj = bt_private_value_array_borrow_element_by_index( |
158 | attr_obj, index); | |
16ca5ff0 PP |
159 | if (!attr_field_obj) { |
160 | BT_LOGE("Cannot get attributes object's array value's element by index: " | |
161 | "value-addr=%p, index=%" PRIu64, attr_obj, index); | |
162 | goto end; | |
163 | } | |
164 | ||
da91b29a | 165 | value_obj = bt_private_value_array_borrow_element_by_index(attr_field_obj, |
16ca5ff0 PP |
166 | BT_CTF_ATTR_VALUE_INDEX); |
167 | if (!value_obj) { | |
168 | BT_LOGE("Cannot get attribute array value's element by index: " | |
169 | "value-addr=%p, index=%" PRIu64, attr_field_obj, | |
170 | (uint64_t) BT_CTF_ATTR_VALUE_INDEX); | |
171 | } | |
172 | ||
173 | end: | |
174 | return value_obj; | |
175 | } | |
176 | ||
177 | static | |
da91b29a PP |
178 | struct bt_private_value *bt_ctf_attributes_borrow_field_by_name( |
179 | struct bt_private_value *attr_obj, const char *name) | |
16ca5ff0 PP |
180 | { |
181 | uint64_t i; | |
182 | int64_t attr_size; | |
da91b29a PP |
183 | struct bt_private_value *value_obj = NULL; |
184 | struct bt_private_value *attr_field_name_obj = NULL; | |
16ca5ff0 | 185 | |
da91b29a | 186 | attr_size = bt_value_array_get_size(bt_value_borrow_from_private(attr_obj)); |
16ca5ff0 PP |
187 | if (attr_size < 0) { |
188 | BT_LOGE("Cannot get array value's size: value-addr=%p", | |
189 | attr_obj); | |
190 | goto error; | |
191 | } | |
192 | ||
193 | for (i = 0; i < attr_size; ++i) { | |
194 | int ret; | |
195 | const char *field_name; | |
196 | ||
da91b29a | 197 | value_obj = bt_private_value_array_borrow_element_by_index(attr_obj, i); |
16ca5ff0 PP |
198 | if (!value_obj) { |
199 | BT_LOGE("Cannot get attributes object's array value's element by index: " | |
200 | "value-addr=%p, index=%" PRIu64, attr_obj, i); | |
201 | goto error; | |
202 | } | |
203 | ||
da91b29a | 204 | attr_field_name_obj = bt_private_value_array_borrow_element_by_index(value_obj, |
16ca5ff0 PP |
205 | BT_CTF_ATTR_NAME_INDEX); |
206 | if (!attr_field_name_obj) { | |
207 | BT_LOGE("Cannot get attribute array value's element by index: " | |
208 | "value-addr=%p, index=%" PRIu64, | |
209 | value_obj, (int64_t) BT_CTF_ATTR_NAME_INDEX); | |
210 | goto error; | |
211 | } | |
212 | ||
da91b29a PP |
213 | ret = bt_value_string_get( |
214 | bt_value_borrow_from_private(attr_field_name_obj), | |
215 | &field_name); | |
16ca5ff0 PP |
216 | if (ret) { |
217 | BT_LOGE("Cannot get raw value from string value: value-addr=%p", | |
218 | attr_field_name_obj); | |
219 | goto error; | |
220 | } | |
221 | ||
222 | if (!strcmp(field_name, name)) { | |
223 | break; | |
224 | } | |
225 | ||
226 | value_obj = NULL; | |
227 | } | |
228 | ||
229 | return value_obj; | |
230 | ||
231 | error: | |
232 | value_obj = NULL; | |
233 | return value_obj; | |
234 | } | |
235 | ||
236 | BT_HIDDEN | |
da91b29a PP |
237 | int bt_ctf_attributes_set_field_value(struct bt_private_value *attr_obj, |
238 | const char *name, struct bt_private_value *value_obj) | |
16ca5ff0 PP |
239 | { |
240 | int ret = 0; | |
da91b29a | 241 | struct bt_private_value *attr_field_obj = NULL; |
16ca5ff0 PP |
242 | |
243 | if (!attr_obj || !name || !value_obj) { | |
244 | BT_LOGW("Invalid parameter: attributes object, name, or value object is NULL: " | |
245 | "attr-value-addr=%p, name-addr=%p, value-addr=%p", | |
246 | attr_obj, name, value_obj); | |
247 | ret = -1; | |
248 | goto end; | |
249 | } | |
250 | ||
251 | attr_field_obj = bt_ctf_attributes_borrow_field_by_name(attr_obj, name); | |
252 | if (attr_field_obj) { | |
da91b29a PP |
253 | ret = bt_private_value_array_set_element_by_index( |
254 | attr_field_obj, BT_CTF_ATTR_VALUE_INDEX, | |
255 | bt_value_borrow_from_private(value_obj)); | |
16ca5ff0 PP |
256 | attr_field_obj = NULL; |
257 | goto end; | |
258 | } | |
259 | ||
da91b29a | 260 | attr_field_obj = bt_private_value_array_create(); |
16ca5ff0 PP |
261 | if (!attr_field_obj) { |
262 | BT_LOGE_STR("Failed to create empty array value."); | |
263 | ret = -1; | |
264 | goto end; | |
265 | } | |
266 | ||
da91b29a PP |
267 | ret = bt_private_value_array_append_string_element(attr_field_obj, name); |
268 | ret |= bt_private_value_array_append_element(attr_field_obj, | |
269 | bt_value_borrow_from_private(value_obj)); | |
16ca5ff0 PP |
270 | if (ret) { |
271 | BT_LOGE("Cannot append elements to array value: addr=%p", | |
272 | attr_field_obj); | |
273 | goto end; | |
274 | } | |
275 | ||
da91b29a PP |
276 | ret = bt_private_value_array_append_element(attr_obj, |
277 | bt_value_borrow_from_private(attr_field_obj)); | |
16ca5ff0 PP |
278 | if (ret) { |
279 | BT_LOGE("Cannot append element to array value: " | |
280 | "array-value-addr=%p, element-value-addr=%p", | |
281 | attr_obj, attr_field_obj); | |
282 | } | |
283 | ||
284 | end: | |
65300d60 | 285 | bt_object_put_ref(attr_field_obj); |
16ca5ff0 PP |
286 | return ret; |
287 | } | |
288 | ||
289 | BT_HIDDEN | |
da91b29a PP |
290 | struct bt_private_value *bt_ctf_attributes_borrow_field_value_by_name( |
291 | struct bt_private_value *attr_obj, const char *name) | |
16ca5ff0 | 292 | { |
da91b29a PP |
293 | struct bt_private_value *value_obj = NULL; |
294 | struct bt_private_value *attr_field_obj = NULL; | |
16ca5ff0 PP |
295 | |
296 | if (!attr_obj || !name) { | |
297 | BT_LOGW("Invalid parameter: attributes object or name is NULL: " | |
298 | "value-addr=%p, name-addr=%p", attr_obj, name); | |
299 | goto end; | |
300 | } | |
301 | ||
302 | attr_field_obj = bt_ctf_attributes_borrow_field_by_name(attr_obj, name); | |
303 | if (!attr_field_obj) { | |
304 | BT_LOGD("Cannot find attributes object's field by name: " | |
305 | "value-addr=%p, name=\"%s\"", attr_obj, name); | |
306 | goto end; | |
307 | } | |
308 | ||
da91b29a | 309 | value_obj = bt_private_value_array_borrow_element_by_index(attr_field_obj, |
16ca5ff0 PP |
310 | BT_CTF_ATTR_VALUE_INDEX); |
311 | if (!value_obj) { | |
312 | BT_LOGE("Cannot get attribute array value's element by index: " | |
313 | "value-addr=%p, index=%" PRIu64, attr_field_obj, | |
314 | (uint64_t) BT_CTF_ATTR_VALUE_INDEX); | |
315 | } | |
316 | ||
317 | end: | |
318 | return value_obj; | |
319 | } | |
320 | ||
321 | BT_HIDDEN | |
da91b29a | 322 | int bt_ctf_attributes_freeze(struct bt_private_value *attr_obj) |
16ca5ff0 PP |
323 | { |
324 | uint64_t i; | |
325 | int64_t count; | |
326 | int ret = 0; | |
327 | ||
328 | if (!attr_obj) { | |
329 | BT_LOGW_STR("Invalid parameter: attributes object is NULL."); | |
330 | ret = -1; | |
331 | goto end; | |
332 | } | |
333 | ||
334 | BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj); | |
da91b29a | 335 | count = bt_value_array_get_size(bt_value_borrow_from_private(attr_obj)); |
16ca5ff0 PP |
336 | BT_ASSERT(count >= 0); |
337 | ||
338 | /* | |
339 | * We do not freeze the array value object itself here, since | |
340 | * internal stuff could need to modify/add attributes. Each | |
341 | * attribute is frozen one by one. | |
342 | */ | |
343 | for (i = 0; i < count; ++i) { | |
da91b29a | 344 | struct bt_private_value *obj = NULL; |
16ca5ff0 PP |
345 | |
346 | obj = bt_ctf_attributes_borrow_field_value(attr_obj, i); | |
347 | if (!obj) { | |
348 | BT_LOGE("Cannot get attributes object's field value by index: " | |
349 | "value-addr=%p, index=%" PRIu64, | |
350 | attr_obj, i); | |
351 | ret = -1; | |
352 | goto end; | |
353 | } | |
354 | ||
da91b29a | 355 | bt_value_freeze(bt_value_borrow_from_private(obj)); |
16ca5ff0 PP |
356 | } |
357 | ||
358 | end: | |
359 | return ret; | |
360 | } |