Commit | Line | Data |
---|---|---|
44e0a4f5 JG |
1 | /* |
2 | * attributes.c | |
3 | * | |
108b91d0 | 4 | * Babeltrace trace IR - Attributes |
44e0a4f5 JG |
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 | ||
0f5e83e5 | 28 | #define BT_LOG_TAG "ATTRS" |
40547d22 | 29 | #include <babeltrace/lib-logging-internal.h> |
0f5e83e5 | 30 | |
8138bfe1 | 31 | #include <babeltrace/object.h> |
44e0a4f5 | 32 | #include <babeltrace/babeltrace-internal.h> |
dac5c838 | 33 | #include <babeltrace/values.h> |
17582c6d | 34 | #include <babeltrace/private-values.h> |
8b45963b | 35 | #include <babeltrace/values-internal.h> |
561ad8c1 | 36 | #include <inttypes.h> |
ee389f01 | 37 | #include <babeltrace/compat/string-internal.h> |
8b45963b | 38 | #include <babeltrace/assert-internal.h> |
44e0a4f5 | 39 | |
839d52a5 PP |
40 | #define BT_ATTR_NAME_INDEX 0 |
41 | #define BT_ATTR_VALUE_INDEX 1 | |
44e0a4f5 JG |
42 | |
43 | BT_HIDDEN | |
17582c6d | 44 | struct bt_private_value *bt_attributes_create(void) |
44e0a4f5 | 45 | { |
17582c6d | 46 | struct bt_private_value *attr_obj; |
561ad8c1 | 47 | |
44e0a4f5 | 48 | /* |
dac5c838 PP |
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). | |
44e0a4f5 JG |
52 | * |
53 | * Example (JSON representation): | |
54 | * | |
55 | * [ | |
56 | * ["hostname", "eeppdesk"], | |
57 | * ["sysname", "Linux"], | |
58 | * ["tracer_major", 2], | |
59 | * ["tracer_minor", 5] | |
60 | * ] | |
61 | */ | |
561ad8c1 | 62 | BT_LOGD_STR("Creating attributes object."); |
17582c6d | 63 | attr_obj = bt_private_value_array_create(); |
561ad8c1 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; | |
44e0a4f5 JG |
72 | } |
73 | ||
74 | BT_HIDDEN | |
17582c6d | 75 | void bt_attributes_destroy(struct bt_private_value *attr_obj) |
44e0a4f5 | 76 | { |
561ad8c1 | 77 | BT_LOGD("Destroying attributes object: addr=%p", attr_obj); |
8138bfe1 | 78 | bt_object_put_ref(attr_obj); |
44e0a4f5 JG |
79 | } |
80 | ||
81 | BT_HIDDEN | |
17582c6d | 82 | int64_t bt_attributes_get_count(struct bt_private_value *attr_obj) |
44e0a4f5 | 83 | { |
17582c6d | 84 | return bt_value_array_get_size(bt_value_borrow_from_private(attr_obj)); |
44e0a4f5 JG |
85 | } |
86 | ||
87 | BT_HIDDEN | |
17582c6d | 88 | const char *bt_attributes_get_field_name(struct bt_private_value *attr_obj, |
dcf0cc71 | 89 | uint64_t index) |
44e0a4f5 JG |
90 | { |
91 | int rc; | |
92 | const char *ret = NULL; | |
17582c6d PP |
93 | struct bt_private_value *attr_field_obj = NULL; |
94 | struct bt_private_value *attr_field_name_obj = NULL; | |
44e0a4f5 | 95 | |
561ad8c1 PP |
96 | if (!attr_obj) { |
97 | BT_LOGW_STR("Invalid parameter: attributes object is NULL."); | |
44e0a4f5 JG |
98 | goto end; |
99 | } | |
100 | ||
17582c6d PP |
101 | if (index >= bt_value_array_get_size( |
102 | bt_value_borrow_from_private(attr_obj))) { | |
8b45963b PP |
103 | BT_LOGW("Invalid parameter: index is out of bounds: " |
104 | "index=%" PRIu64 ", count=%" PRId64, | |
17582c6d PP |
105 | index, bt_value_array_get_size( |
106 | bt_value_borrow_from_private(attr_obj))); | |
8b45963b PP |
107 | goto end; |
108 | } | |
109 | ||
17582c6d PP |
110 | attr_field_obj = bt_private_value_array_borrow_element_by_index( |
111 | attr_obj, index); | |
44e0a4f5 | 112 | if (!attr_field_obj) { |
561ad8c1 PP |
113 | BT_LOGE("Cannot get attributes object's array value's element by index: " |
114 | "value-addr=%p, index=%" PRIu64, attr_obj, index); | |
44e0a4f5 JG |
115 | goto end; |
116 | } | |
117 | ||
17582c6d PP |
118 | attr_field_name_obj = |
119 | bt_private_value_array_borrow_element_by_index(attr_field_obj, | |
120 | BT_ATTR_NAME_INDEX); | |
44e0a4f5 | 121 | if (!attr_field_name_obj) { |
561ad8c1 PP |
122 | BT_LOGE("Cannot get attribute array value's element by index: " |
123 | "value-addr=%p, index=%" PRIu64, attr_field_obj, | |
839d52a5 | 124 | (uint64_t) BT_ATTR_NAME_INDEX); |
44e0a4f5 JG |
125 | goto end; |
126 | } | |
127 | ||
17582c6d PP |
128 | rc = bt_value_string_get( |
129 | bt_value_borrow_from_private(attr_field_name_obj), &ret); | |
44e0a4f5 | 130 | if (rc) { |
561ad8c1 PP |
131 | BT_LOGE("Cannot get raw value from string value: value-addr=%p", |
132 | attr_field_name_obj); | |
44e0a4f5 JG |
133 | ret = NULL; |
134 | } | |
135 | ||
136 | end: | |
44e0a4f5 JG |
137 | return ret; |
138 | } | |
139 | ||
140 | BT_HIDDEN | |
17582c6d PP |
141 | struct bt_private_value *bt_attributes_borrow_field_value( |
142 | struct bt_private_value *attr_obj, uint64_t index) | |
44e0a4f5 | 143 | { |
17582c6d PP |
144 | struct bt_private_value *value_obj = NULL; |
145 | struct bt_private_value *attr_field_obj = NULL; | |
44e0a4f5 | 146 | |
561ad8c1 PP |
147 | if (!attr_obj) { |
148 | BT_LOGW_STR("Invalid parameter: attributes object is NULL."); | |
44e0a4f5 JG |
149 | goto end; |
150 | } | |
151 | ||
17582c6d | 152 | if (index >= bt_value_array_get_size(bt_value_borrow_from_private(attr_obj))) { |
8b45963b PP |
153 | BT_LOGW("Invalid parameter: index is out of bounds: " |
154 | "index=%" PRIu64 ", count=%" PRId64, | |
17582c6d PP |
155 | index, bt_value_array_get_size( |
156 | bt_value_borrow_from_private(attr_obj))); | |
8b45963b PP |
157 | goto end; |
158 | } | |
159 | ||
17582c6d PP |
160 | attr_field_obj = |
161 | bt_private_value_array_borrow_element_by_index(attr_obj, index); | |
44e0a4f5 | 162 | if (!attr_field_obj) { |
561ad8c1 PP |
163 | BT_LOGE("Cannot get attributes object's array value's element by index: " |
164 | "value-addr=%p, index=%" PRIu64, attr_obj, index); | |
44e0a4f5 JG |
165 | goto end; |
166 | } | |
167 | ||
17582c6d PP |
168 | value_obj = bt_private_value_array_borrow_element_by_index( |
169 | attr_field_obj, BT_ATTR_VALUE_INDEX); | |
561ad8c1 PP |
170 | if (!value_obj) { |
171 | BT_LOGE("Cannot get attribute array value's element by index: " | |
172 | "value-addr=%p, index=%" PRIu64, attr_field_obj, | |
839d52a5 | 173 | (uint64_t) BT_ATTR_VALUE_INDEX); |
561ad8c1 | 174 | } |
44e0a4f5 JG |
175 | |
176 | end: | |
44e0a4f5 JG |
177 | return value_obj; |
178 | } | |
179 | ||
180 | static | |
17582c6d PP |
181 | struct bt_private_value *bt_attributes_borrow_field_by_name( |
182 | struct bt_private_value *attr_obj, const char *name) | |
44e0a4f5 | 183 | { |
dcf0cc71 PP |
184 | uint64_t i; |
185 | int64_t attr_size; | |
17582c6d PP |
186 | struct bt_private_value *value_obj = NULL; |
187 | struct bt_private_value *attr_field_name_obj = NULL; | |
44e0a4f5 | 188 | |
17582c6d PP |
189 | attr_size = bt_value_array_get_size( |
190 | bt_value_borrow_from_private(attr_obj)); | |
44e0a4f5 | 191 | if (attr_size < 0) { |
561ad8c1 PP |
192 | BT_LOGE("Cannot get array value's size: value-addr=%p", |
193 | attr_obj); | |
44e0a4f5 JG |
194 | goto error; |
195 | } | |
196 | ||
197 | for (i = 0; i < attr_size; ++i) { | |
198 | int ret; | |
199 | const char *field_name; | |
200 | ||
17582c6d PP |
201 | value_obj = bt_private_value_array_borrow_element_by_index( |
202 | attr_obj, i); | |
44e0a4f5 | 203 | if (!value_obj) { |
561ad8c1 PP |
204 | BT_LOGE("Cannot get attributes object's array value's element by index: " |
205 | "value-addr=%p, index=%" PRIu64, attr_obj, i); | |
44e0a4f5 JG |
206 | goto error; |
207 | } | |
208 | ||
17582c6d PP |
209 | attr_field_name_obj = |
210 | bt_private_value_array_borrow_element_by_index( | |
211 | value_obj, BT_ATTR_NAME_INDEX); | |
44e0a4f5 | 212 | if (!attr_field_name_obj) { |
561ad8c1 PP |
213 | BT_LOGE("Cannot get attribute array value's element by index: " |
214 | "value-addr=%p, index=%" PRIu64, | |
5fe68922 | 215 | value_obj, (int64_t) BT_ATTR_NAME_INDEX); |
44e0a4f5 JG |
216 | goto error; |
217 | } | |
218 | ||
17582c6d PP |
219 | ret = bt_value_string_get( |
220 | bt_value_borrow_from_private(attr_field_name_obj), | |
221 | &field_name); | |
44e0a4f5 | 222 | if (ret) { |
561ad8c1 PP |
223 | BT_LOGE("Cannot get raw value from string value: value-addr=%p", |
224 | attr_field_name_obj); | |
44e0a4f5 JG |
225 | goto error; |
226 | } | |
227 | ||
228 | if (!strcmp(field_name, name)) { | |
44e0a4f5 JG |
229 | break; |
230 | } | |
231 | ||
5fe68922 | 232 | value_obj = NULL; |
44e0a4f5 JG |
233 | } |
234 | ||
235 | return value_obj; | |
236 | ||
237 | error: | |
5fe68922 | 238 | value_obj = NULL; |
44e0a4f5 JG |
239 | return value_obj; |
240 | } | |
241 | ||
242 | BT_HIDDEN | |
17582c6d PP |
243 | int bt_attributes_set_field_value(struct bt_private_value *attr_obj, |
244 | const char *name, struct bt_private_value *value_obj) | |
44e0a4f5 JG |
245 | { |
246 | int ret = 0; | |
17582c6d | 247 | struct bt_private_value *attr_field_obj = NULL; |
44e0a4f5 JG |
248 | |
249 | if (!attr_obj || !name || !value_obj) { | |
561ad8c1 PP |
250 | BT_LOGW("Invalid parameter: attributes object, name, or value object is NULL: " |
251 | "attr-value-addr=%p, name-addr=%p, value-addr=%p", | |
252 | attr_obj, name, value_obj); | |
44e0a4f5 JG |
253 | ret = -1; |
254 | goto end; | |
255 | } | |
256 | ||
5fe68922 | 257 | attr_field_obj = bt_attributes_borrow_field_by_name(attr_obj, name); |
44e0a4f5 | 258 | if (attr_field_obj) { |
17582c6d PP |
259 | ret = bt_private_value_array_set_element_by_index( |
260 | attr_field_obj, BT_ATTR_VALUE_INDEX, | |
261 | bt_value_borrow_from_private(value_obj)); | |
5fe68922 | 262 | attr_field_obj = NULL; |
44e0a4f5 JG |
263 | goto end; |
264 | } | |
265 | ||
17582c6d | 266 | attr_field_obj = bt_private_value_array_create(); |
44e0a4f5 | 267 | if (!attr_field_obj) { |
561ad8c1 | 268 | BT_LOGE_STR("Failed to create empty array value."); |
44e0a4f5 JG |
269 | ret = -1; |
270 | goto end; | |
271 | } | |
272 | ||
17582c6d PP |
273 | ret = bt_private_value_array_append_string_element(attr_field_obj, |
274 | name); | |
275 | ret |= bt_private_value_array_append_element(attr_field_obj, | |
276 | bt_value_borrow_from_private(value_obj)); | |
44e0a4f5 | 277 | if (ret) { |
561ad8c1 PP |
278 | BT_LOGE("Cannot append elements to array value: addr=%p", |
279 | attr_field_obj); | |
44e0a4f5 JG |
280 | goto end; |
281 | } | |
282 | ||
17582c6d PP |
283 | ret = bt_private_value_array_append_element(attr_obj, |
284 | bt_value_borrow_from_private(attr_field_obj)); | |
561ad8c1 PP |
285 | if (ret) { |
286 | BT_LOGE("Cannot append element to array value: " | |
287 | "array-value-addr=%p, element-value-addr=%p", | |
288 | attr_obj, attr_field_obj); | |
289 | } | |
44e0a4f5 JG |
290 | |
291 | end: | |
8138bfe1 | 292 | bt_object_put_ref(attr_field_obj); |
44e0a4f5 JG |
293 | return ret; |
294 | } | |
295 | ||
296 | BT_HIDDEN | |
17582c6d PP |
297 | struct bt_private_value *bt_attributes_borrow_field_value_by_name( |
298 | struct bt_private_value *attr_obj, const char *name) | |
44e0a4f5 | 299 | { |
17582c6d PP |
300 | struct bt_private_value *value_obj = NULL; |
301 | struct bt_private_value *attr_field_obj = NULL; | |
44e0a4f5 JG |
302 | |
303 | if (!attr_obj || !name) { | |
561ad8c1 PP |
304 | BT_LOGW("Invalid parameter: attributes object or name is NULL: " |
305 | "value-addr=%p, name-addr=%p", attr_obj, name); | |
44e0a4f5 JG |
306 | goto end; |
307 | } | |
308 | ||
5fe68922 | 309 | attr_field_obj = bt_attributes_borrow_field_by_name(attr_obj, name); |
44e0a4f5 | 310 | if (!attr_field_obj) { |
561ad8c1 PP |
311 | BT_LOGD("Cannot find attributes object's field by name: " |
312 | "value-addr=%p, name=\"%s\"", attr_obj, name); | |
44e0a4f5 JG |
313 | goto end; |
314 | } | |
315 | ||
17582c6d PP |
316 | value_obj = bt_private_value_array_borrow_element_by_index( |
317 | attr_field_obj, BT_ATTR_VALUE_INDEX); | |
561ad8c1 PP |
318 | if (!value_obj) { |
319 | BT_LOGE("Cannot get attribute array value's element by index: " | |
320 | "value-addr=%p, index=%" PRIu64, attr_field_obj, | |
839d52a5 | 321 | (uint64_t) BT_ATTR_VALUE_INDEX); |
561ad8c1 | 322 | } |
44e0a4f5 JG |
323 | |
324 | end: | |
44e0a4f5 JG |
325 | return value_obj; |
326 | } | |
327 | ||
328 | BT_HIDDEN | |
17582c6d | 329 | int bt_attributes_freeze(struct bt_private_value *attr_obj) |
44e0a4f5 | 330 | { |
dcf0cc71 PP |
331 | uint64_t i; |
332 | int64_t count; | |
44e0a4f5 JG |
333 | int ret = 0; |
334 | ||
335 | if (!attr_obj) { | |
561ad8c1 | 336 | BT_LOGW_STR("Invalid parameter: attributes object is NULL."); |
44e0a4f5 JG |
337 | ret = -1; |
338 | goto end; | |
339 | } | |
340 | ||
561ad8c1 | 341 | BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj); |
17582c6d | 342 | count = bt_value_array_get_size(bt_value_borrow_from_private(attr_obj)); |
8b45963b | 343 | BT_ASSERT(count >= 0); |
44e0a4f5 JG |
344 | |
345 | /* | |
dac5c838 PP |
346 | * We do not freeze the array value object itself here, since |
347 | * internal stuff could need to modify/add attributes. Each | |
348 | * attribute is frozen one by one. | |
44e0a4f5 JG |
349 | */ |
350 | for (i = 0; i < count; ++i) { | |
17582c6d | 351 | struct bt_private_value *obj = NULL; |
44e0a4f5 | 352 | |
5fe68922 | 353 | obj = bt_attributes_borrow_field_value(attr_obj, i); |
44e0a4f5 | 354 | if (!obj) { |
561ad8c1 PP |
355 | BT_LOGE("Cannot get attributes object's field value by index: " |
356 | "value-addr=%p, index=%" PRIu64, | |
357 | attr_obj, i); | |
44e0a4f5 JG |
358 | ret = -1; |
359 | goto end; | |
360 | } | |
361 | ||
17582c6d | 362 | bt_value_freeze(bt_value_borrow_from_private(obj)); |
44e0a4f5 JG |
363 | } |
364 | ||
365 | end: | |
44e0a4f5 JG |
366 | return ret; |
367 | } |