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> | |
e1e02a22 PP |
34 | #include <babeltrace/ctf-writer/object.h> |
35 | #include <babeltrace/ctf-writer/values-internal.h> | |
36 | #include <babeltrace/ctf-writer/values-internal.h> | |
37 | #include <babeltrace/ctf-writer/values-internal.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 | |
e1e02a22 | 44 | struct bt_ctf_private_value *bt_ctf_attributes_create(void) |
16ca5ff0 | 45 | { |
e1e02a22 | 46 | struct bt_ctf_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."); | |
e1e02a22 | 63 | attr_obj = bt_ctf_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 | |
e1e02a22 | 75 | void bt_ctf_attributes_destroy(struct bt_ctf_private_value *attr_obj) |
16ca5ff0 PP |
76 | { |
77 | BT_LOGD("Destroying attributes object: addr=%p", attr_obj); | |
e1e02a22 | 78 | bt_ctf_object_put_ref(attr_obj); |
16ca5ff0 PP |
79 | } |
80 | ||
81 | BT_HIDDEN | |
e1e02a22 | 82 | int64_t bt_ctf_attributes_get_count(struct bt_ctf_private_value *attr_obj) |
16ca5ff0 | 83 | { |
e1e02a22 | 84 | return bt_ctf_value_array_get_size(bt_ctf_private_value_as_value(attr_obj)); |
16ca5ff0 PP |
85 | } |
86 | ||
87 | BT_HIDDEN | |
e1e02a22 | 88 | const char *bt_ctf_attributes_get_field_name(struct bt_ctf_private_value *attr_obj, |
16ca5ff0 PP |
89 | uint64_t index) |
90 | { | |
16ca5ff0 | 91 | const char *ret = NULL; |
e1e02a22 PP |
92 | struct bt_ctf_private_value *attr_field_obj = NULL; |
93 | struct bt_ctf_private_value *attr_field_name_obj = NULL; | |
16ca5ff0 PP |
94 | |
95 | if (!attr_obj) { | |
96 | BT_LOGW_STR("Invalid parameter: attributes object is NULL."); | |
97 | goto end; | |
98 | } | |
99 | ||
e1e02a22 | 100 | if (index >= bt_ctf_value_array_get_size(bt_ctf_private_value_as_value(attr_obj))) { |
16ca5ff0 PP |
101 | BT_LOGW("Invalid parameter: index is out of bounds: " |
102 | "index=%" PRIu64 ", count=%" PRId64, | |
e1e02a22 | 103 | index, bt_ctf_value_array_get_size(bt_ctf_private_value_as_value(attr_obj))); |
16ca5ff0 PP |
104 | goto end; |
105 | } | |
106 | ||
e1e02a22 | 107 | attr_field_obj = bt_ctf_private_value_array_borrow_element_by_index( |
da91b29a | 108 | attr_obj, index); |
16ca5ff0 PP |
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 | ||
da91b29a | 115 | attr_field_name_obj = |
e1e02a22 | 116 | bt_ctf_private_value_array_borrow_element_by_index( |
da91b29a | 117 | attr_field_obj, BT_CTF_ATTR_NAME_INDEX); |
16ca5ff0 PP |
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 | ||
e1e02a22 PP |
125 | ret = bt_ctf_value_string_get( |
126 | bt_ctf_private_value_as_value(attr_field_name_obj)); | |
16ca5ff0 PP |
127 | |
128 | end: | |
129 | return ret; | |
130 | } | |
131 | ||
132 | BT_HIDDEN | |
e1e02a22 | 133 | struct bt_ctf_private_value *bt_ctf_attributes_borrow_field_value(struct bt_ctf_private_value *attr_obj, |
16ca5ff0 PP |
134 | uint64_t index) |
135 | { | |
e1e02a22 PP |
136 | struct bt_ctf_private_value *value_obj = NULL; |
137 | struct bt_ctf_private_value *attr_field_obj = NULL; | |
16ca5ff0 PP |
138 | |
139 | if (!attr_obj) { | |
140 | BT_LOGW_STR("Invalid parameter: attributes object is NULL."); | |
141 | goto end; | |
142 | } | |
143 | ||
e1e02a22 | 144 | if (index >= bt_ctf_value_array_get_size(bt_ctf_private_value_as_value(attr_obj))) { |
16ca5ff0 PP |
145 | BT_LOGW("Invalid parameter: index is out of bounds: " |
146 | "index=%" PRIu64 ", count=%" PRId64, | |
e1e02a22 | 147 | index, bt_ctf_value_array_get_size(bt_ctf_private_value_as_value(attr_obj))); |
16ca5ff0 PP |
148 | goto end; |
149 | } | |
150 | ||
e1e02a22 | 151 | attr_field_obj = bt_ctf_private_value_array_borrow_element_by_index( |
da91b29a | 152 | attr_obj, index); |
16ca5ff0 PP |
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 | ||
e1e02a22 | 159 | value_obj = bt_ctf_private_value_array_borrow_element_by_index(attr_field_obj, |
16ca5ff0 PP |
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 | |
e1e02a22 PP |
172 | struct bt_ctf_private_value *bt_ctf_attributes_borrow_field_by_name( |
173 | struct bt_ctf_private_value *attr_obj, const char *name) | |
16ca5ff0 PP |
174 | { |
175 | uint64_t i; | |
176 | int64_t attr_size; | |
e1e02a22 PP |
177 | struct bt_ctf_private_value *value_obj = NULL; |
178 | struct bt_ctf_private_value *attr_field_name_obj = NULL; | |
16ca5ff0 | 179 | |
e1e02a22 | 180 | attr_size = bt_ctf_value_array_get_size(bt_ctf_private_value_as_value(attr_obj)); |
16ca5ff0 PP |
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) { | |
16ca5ff0 PP |
188 | const char *field_name; |
189 | ||
e1e02a22 | 190 | value_obj = bt_ctf_private_value_array_borrow_element_by_index(attr_obj, i); |
16ca5ff0 PP |
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 | ||
e1e02a22 | 197 | attr_field_name_obj = bt_ctf_private_value_array_borrow_element_by_index(value_obj, |
16ca5ff0 PP |
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 | ||
e1e02a22 PP |
206 | field_name = bt_ctf_value_string_get( |
207 | bt_ctf_private_value_as_value(attr_field_name_obj)); | |
16ca5ff0 PP |
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 | |
e1e02a22 PP |
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) | |
16ca5ff0 PP |
226 | { |
227 | int ret = 0; | |
e1e02a22 | 228 | struct bt_ctf_private_value *attr_field_obj = NULL; |
16ca5ff0 PP |
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) { | |
e1e02a22 | 240 | ret = bt_ctf_private_value_array_set_element_by_index( |
da91b29a | 241 | attr_field_obj, BT_CTF_ATTR_VALUE_INDEX, |
e1e02a22 | 242 | bt_ctf_private_value_as_value(value_obj)); |
16ca5ff0 PP |
243 | attr_field_obj = NULL; |
244 | goto end; | |
245 | } | |
246 | ||
e1e02a22 | 247 | attr_field_obj = bt_ctf_private_value_array_create(); |
16ca5ff0 PP |
248 | if (!attr_field_obj) { |
249 | BT_LOGE_STR("Failed to create empty array value."); | |
250 | ret = -1; | |
251 | goto end; | |
252 | } | |
253 | ||
e1e02a22 PP |
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)); | |
16ca5ff0 PP |
257 | if (ret) { |
258 | BT_LOGE("Cannot append elements to array value: addr=%p", | |
259 | attr_field_obj); | |
260 | goto end; | |
261 | } | |
262 | ||
e1e02a22 PP |
263 | ret = bt_ctf_private_value_array_append_element(attr_obj, |
264 | bt_ctf_private_value_as_value(attr_field_obj)); | |
16ca5ff0 PP |
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: | |
e1e02a22 | 272 | bt_ctf_object_put_ref(attr_field_obj); |
16ca5ff0 PP |
273 | return ret; |
274 | } | |
275 | ||
276 | BT_HIDDEN | |
e1e02a22 PP |
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) | |
16ca5ff0 | 279 | { |
e1e02a22 PP |
280 | struct bt_ctf_private_value *value_obj = NULL; |
281 | struct bt_ctf_private_value *attr_field_obj = NULL; | |
16ca5ff0 PP |
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 | ||
e1e02a22 | 296 | value_obj = bt_ctf_private_value_array_borrow_element_by_index(attr_field_obj, |
16ca5ff0 PP |
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 | |
e1e02a22 | 309 | int bt_ctf_attributes_freeze(struct bt_ctf_private_value *attr_obj) |
16ca5ff0 PP |
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); | |
e1e02a22 | 322 | count = bt_ctf_value_array_get_size(bt_ctf_private_value_as_value(attr_obj)); |
16ca5ff0 PP |
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) { | |
e1e02a22 | 331 | struct bt_ctf_private_value *obj = NULL; |
16ca5ff0 PP |
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 | ||
e1e02a22 | 342 | bt_ctf_value_freeze(bt_ctf_private_value_as_value(obj)); |
16ca5ff0 PP |
343 | } |
344 | ||
345 | end: | |
346 | return ret; | |
347 | } |