Commit | Line | Data |
---|---|---|
44e0a4f5 | 1 | /* |
44e0a4f5 JG |
2 | * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation |
3 | * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com> | |
4 | * | |
5 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
6 | * of this software and associated documentation files (the "Software"), to deal | |
7 | * in the Software without restriction, including without limitation the rights | |
8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
9 | * copies of the Software, and to permit persons to whom the Software is | |
10 | * furnished to do so, subject to the following conditions: | |
11 | * | |
12 | * The above copyright notice and this permission notice shall be included in | |
13 | * all copies or substantial portions of the Software. | |
14 | * | |
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
21 | * SOFTWARE. | |
22 | */ | |
23 | ||
0f5e83e5 | 24 | #define BT_LOG_TAG "ATTRS" |
40547d22 | 25 | #include <babeltrace/lib-logging-internal.h> |
0f5e83e5 | 26 | |
8138bfe1 | 27 | #include <babeltrace/object.h> |
44e0a4f5 | 28 | #include <babeltrace/babeltrace-internal.h> |
dac5c838 | 29 | #include <babeltrace/values.h> |
ce141536 | 30 | #include <babeltrace/values-const.h> |
8b45963b | 31 | #include <babeltrace/values-internal.h> |
ce141536 | 32 | #include <babeltrace/trace-ir/attributes-internal.h> |
561ad8c1 | 33 | #include <inttypes.h> |
ee389f01 | 34 | #include <babeltrace/compat/string-internal.h> |
8b45963b | 35 | #include <babeltrace/assert-internal.h> |
44e0a4f5 | 36 | |
839d52a5 PP |
37 | #define BT_ATTR_NAME_INDEX 0 |
38 | #define BT_ATTR_VALUE_INDEX 1 | |
44e0a4f5 JG |
39 | |
40 | BT_HIDDEN | |
ce141536 | 41 | struct bt_value *bt_attributes_create(void) |
44e0a4f5 | 42 | { |
ce141536 | 43 | struct bt_value *attr_obj; |
561ad8c1 | 44 | |
44e0a4f5 | 45 | /* |
dac5c838 PP |
46 | * Attributes: array value object of array value objects, each one |
47 | * containing two entries: a string value object (attributes | |
48 | * field name), and a value object (attributes field value). | |
44e0a4f5 JG |
49 | * |
50 | * Example (JSON representation): | |
51 | * | |
52 | * [ | |
53 | * ["hostname", "eeppdesk"], | |
54 | * ["sysname", "Linux"], | |
55 | * ["tracer_major", 2], | |
56 | * ["tracer_minor", 5] | |
57 | * ] | |
58 | */ | |
561ad8c1 | 59 | BT_LOGD_STR("Creating attributes object."); |
ce141536 | 60 | attr_obj = bt_value_array_create(); |
561ad8c1 PP |
61 | if (!attr_obj) { |
62 | BT_LOGE_STR("Failed to create array value."); | |
63 | } else { | |
64 | BT_LOGD("Created attributes object: addr=%p", | |
65 | attr_obj); | |
66 | } | |
67 | ||
68 | return attr_obj; | |
44e0a4f5 JG |
69 | } |
70 | ||
71 | BT_HIDDEN | |
ce141536 | 72 | void bt_attributes_destroy(struct bt_value *attr_obj) |
44e0a4f5 | 73 | { |
561ad8c1 | 74 | BT_LOGD("Destroying attributes object: addr=%p", attr_obj); |
1248f5ea | 75 | BT_OBJECT_PUT_REF_AND_RESET(attr_obj); |
44e0a4f5 JG |
76 | } |
77 | ||
78 | BT_HIDDEN | |
ce141536 | 79 | int64_t bt_attributes_get_count(const struct bt_value *attr_obj) |
44e0a4f5 | 80 | { |
ce141536 | 81 | return bt_value_array_get_size(attr_obj); |
44e0a4f5 JG |
82 | } |
83 | ||
84 | BT_HIDDEN | |
ce141536 | 85 | const char *bt_attributes_get_field_name(const struct bt_value *attr_obj, |
dcf0cc71 | 86 | uint64_t index) |
44e0a4f5 | 87 | { |
44e0a4f5 | 88 | const char *ret = NULL; |
ce141536 PP |
89 | const struct bt_value *attr_field_obj = NULL; |
90 | const struct bt_value *attr_field_name_obj = NULL; | |
44e0a4f5 | 91 | |
561ad8c1 PP |
92 | if (!attr_obj) { |
93 | BT_LOGW_STR("Invalid parameter: attributes object is NULL."); | |
44e0a4f5 JG |
94 | goto end; |
95 | } | |
96 | ||
ce141536 | 97 | if (index >= bt_value_array_get_size(attr_obj)) { |
8b45963b PP |
98 | BT_LOGW("Invalid parameter: index is out of bounds: " |
99 | "index=%" PRIu64 ", count=%" PRId64, | |
ce141536 | 100 | index, bt_value_array_get_size(attr_obj)); |
8b45963b PP |
101 | goto end; |
102 | } | |
103 | ||
ce141536 | 104 | attr_field_obj = bt_value_array_borrow_element_by_index_const( |
17582c6d | 105 | attr_obj, index); |
44e0a4f5 | 106 | if (!attr_field_obj) { |
561ad8c1 PP |
107 | BT_LOGE("Cannot get attributes object's array value's element by index: " |
108 | "value-addr=%p, index=%" PRIu64, attr_obj, index); | |
44e0a4f5 JG |
109 | goto end; |
110 | } | |
111 | ||
17582c6d | 112 | attr_field_name_obj = |
ce141536 | 113 | bt_value_array_borrow_element_by_index_const(attr_field_obj, |
17582c6d | 114 | BT_ATTR_NAME_INDEX); |
44e0a4f5 | 115 | if (!attr_field_name_obj) { |
561ad8c1 PP |
116 | BT_LOGE("Cannot get attribute array value's element by index: " |
117 | "value-addr=%p, index=%" PRIu64, attr_field_obj, | |
839d52a5 | 118 | (uint64_t) BT_ATTR_NAME_INDEX); |
44e0a4f5 JG |
119 | goto end; |
120 | } | |
121 | ||
ce141536 | 122 | ret = bt_value_string_get(attr_field_name_obj); |
44e0a4f5 JG |
123 | |
124 | end: | |
44e0a4f5 JG |
125 | return ret; |
126 | } | |
127 | ||
128 | BT_HIDDEN | |
ce141536 PP |
129 | struct bt_value *bt_attributes_borrow_field_value( |
130 | struct bt_value *attr_obj, uint64_t index) | |
44e0a4f5 | 131 | { |
ce141536 PP |
132 | struct bt_value *value_obj = NULL; |
133 | struct bt_value *attr_field_obj = NULL; | |
44e0a4f5 | 134 | |
561ad8c1 PP |
135 | if (!attr_obj) { |
136 | BT_LOGW_STR("Invalid parameter: attributes object is NULL."); | |
44e0a4f5 JG |
137 | goto end; |
138 | } | |
139 | ||
ce141536 | 140 | if (index >= bt_value_array_get_size(attr_obj)) { |
8b45963b PP |
141 | BT_LOGW("Invalid parameter: index is out of bounds: " |
142 | "index=%" PRIu64 ", count=%" PRId64, | |
ce141536 | 143 | index, bt_value_array_get_size(attr_obj)); |
8b45963b PP |
144 | goto end; |
145 | } | |
146 | ||
17582c6d | 147 | attr_field_obj = |
ce141536 | 148 | bt_value_array_borrow_element_by_index(attr_obj, index); |
44e0a4f5 | 149 | if (!attr_field_obj) { |
561ad8c1 PP |
150 | BT_LOGE("Cannot get attributes object's array value's element by index: " |
151 | "value-addr=%p, index=%" PRIu64, attr_obj, index); | |
44e0a4f5 JG |
152 | goto end; |
153 | } | |
154 | ||
ce141536 | 155 | value_obj = bt_value_array_borrow_element_by_index( |
17582c6d | 156 | attr_field_obj, BT_ATTR_VALUE_INDEX); |
561ad8c1 PP |
157 | if (!value_obj) { |
158 | BT_LOGE("Cannot get attribute array value's element by index: " | |
159 | "value-addr=%p, index=%" PRIu64, attr_field_obj, | |
839d52a5 | 160 | (uint64_t) BT_ATTR_VALUE_INDEX); |
561ad8c1 | 161 | } |
44e0a4f5 JG |
162 | |
163 | end: | |
44e0a4f5 JG |
164 | return value_obj; |
165 | } | |
166 | ||
167 | static | |
ce141536 PP |
168 | struct bt_value *bt_attributes_borrow_field_by_name( |
169 | struct bt_value *attr_obj, const char *name) | |
44e0a4f5 | 170 | { |
dcf0cc71 PP |
171 | uint64_t i; |
172 | int64_t attr_size; | |
ce141536 PP |
173 | struct bt_value *value_obj = NULL; |
174 | struct bt_value *attr_field_name_obj = NULL; | |
44e0a4f5 | 175 | |
ce141536 | 176 | attr_size = bt_value_array_get_size(attr_obj); |
44e0a4f5 | 177 | if (attr_size < 0) { |
561ad8c1 PP |
178 | BT_LOGE("Cannot get array value's size: value-addr=%p", |
179 | attr_obj); | |
44e0a4f5 JG |
180 | goto error; |
181 | } | |
182 | ||
183 | for (i = 0; i < attr_size; ++i) { | |
44e0a4f5 JG |
184 | const char *field_name; |
185 | ||
ce141536 | 186 | value_obj = bt_value_array_borrow_element_by_index( |
17582c6d | 187 | attr_obj, i); |
44e0a4f5 | 188 | if (!value_obj) { |
561ad8c1 PP |
189 | BT_LOGE("Cannot get attributes object's array value's element by index: " |
190 | "value-addr=%p, index=%" PRIu64, attr_obj, i); | |
44e0a4f5 JG |
191 | goto error; |
192 | } | |
193 | ||
17582c6d | 194 | attr_field_name_obj = |
ce141536 | 195 | bt_value_array_borrow_element_by_index( |
17582c6d | 196 | value_obj, BT_ATTR_NAME_INDEX); |
44e0a4f5 | 197 | if (!attr_field_name_obj) { |
561ad8c1 PP |
198 | BT_LOGE("Cannot get attribute array value's element by index: " |
199 | "value-addr=%p, index=%" PRIu64, | |
5fe68922 | 200 | value_obj, (int64_t) BT_ATTR_NAME_INDEX); |
44e0a4f5 JG |
201 | goto error; |
202 | } | |
203 | ||
ce141536 | 204 | field_name = bt_value_string_get(attr_field_name_obj); |
44e0a4f5 JG |
205 | |
206 | if (!strcmp(field_name, name)) { | |
44e0a4f5 JG |
207 | break; |
208 | } | |
209 | ||
5fe68922 | 210 | value_obj = NULL; |
44e0a4f5 JG |
211 | } |
212 | ||
213 | return value_obj; | |
214 | ||
215 | error: | |
5fe68922 | 216 | value_obj = NULL; |
44e0a4f5 JG |
217 | return value_obj; |
218 | } | |
219 | ||
220 | BT_HIDDEN | |
ce141536 PP |
221 | int bt_attributes_set_field_value(struct bt_value *attr_obj, |
222 | const char *name, struct bt_value *value_obj) | |
44e0a4f5 JG |
223 | { |
224 | int ret = 0; | |
ce141536 | 225 | struct bt_value *attr_field_obj = NULL; |
44e0a4f5 JG |
226 | |
227 | if (!attr_obj || !name || !value_obj) { | |
561ad8c1 PP |
228 | BT_LOGW("Invalid parameter: attributes object, name, or value object is NULL: " |
229 | "attr-value-addr=%p, name-addr=%p, value-addr=%p", | |
230 | attr_obj, name, value_obj); | |
44e0a4f5 JG |
231 | ret = -1; |
232 | goto end; | |
233 | } | |
234 | ||
5fe68922 | 235 | attr_field_obj = bt_attributes_borrow_field_by_name(attr_obj, name); |
44e0a4f5 | 236 | if (attr_field_obj) { |
ce141536 | 237 | ret = bt_value_array_set_element_by_index( |
17582c6d | 238 | attr_field_obj, BT_ATTR_VALUE_INDEX, |
ce141536 | 239 | value_obj); |
5fe68922 | 240 | attr_field_obj = NULL; |
44e0a4f5 JG |
241 | goto end; |
242 | } | |
243 | ||
ce141536 | 244 | attr_field_obj = bt_value_array_create(); |
44e0a4f5 | 245 | if (!attr_field_obj) { |
561ad8c1 | 246 | BT_LOGE_STR("Failed to create empty array value."); |
44e0a4f5 JG |
247 | ret = -1; |
248 | goto end; | |
249 | } | |
250 | ||
ce141536 | 251 | ret = bt_value_array_append_string_element(attr_field_obj, |
17582c6d | 252 | name); |
ce141536 PP |
253 | ret |= bt_value_array_append_element(attr_field_obj, |
254 | value_obj); | |
44e0a4f5 | 255 | if (ret) { |
561ad8c1 PP |
256 | BT_LOGE("Cannot append elements to array value: addr=%p", |
257 | attr_field_obj); | |
44e0a4f5 JG |
258 | goto end; |
259 | } | |
260 | ||
ce141536 PP |
261 | ret = bt_value_array_append_element(attr_obj, |
262 | attr_field_obj); | |
561ad8c1 PP |
263 | if (ret) { |
264 | BT_LOGE("Cannot append element to array value: " | |
265 | "array-value-addr=%p, element-value-addr=%p", | |
266 | attr_obj, attr_field_obj); | |
267 | } | |
44e0a4f5 JG |
268 | |
269 | end: | |
8138bfe1 | 270 | bt_object_put_ref(attr_field_obj); |
44e0a4f5 JG |
271 | return ret; |
272 | } | |
273 | ||
274 | BT_HIDDEN | |
ce141536 PP |
275 | struct bt_value *bt_attributes_borrow_field_value_by_name( |
276 | struct bt_value *attr_obj, const char *name) | |
44e0a4f5 | 277 | { |
ce141536 PP |
278 | struct bt_value *value_obj = NULL; |
279 | struct bt_value *attr_field_obj = NULL; | |
44e0a4f5 JG |
280 | |
281 | if (!attr_obj || !name) { | |
561ad8c1 PP |
282 | BT_LOGW("Invalid parameter: attributes object or name is NULL: " |
283 | "value-addr=%p, name-addr=%p", attr_obj, name); | |
44e0a4f5 JG |
284 | goto end; |
285 | } | |
286 | ||
5fe68922 | 287 | attr_field_obj = bt_attributes_borrow_field_by_name(attr_obj, name); |
44e0a4f5 | 288 | if (!attr_field_obj) { |
561ad8c1 PP |
289 | BT_LOGD("Cannot find attributes object's field by name: " |
290 | "value-addr=%p, name=\"%s\"", attr_obj, name); | |
44e0a4f5 JG |
291 | goto end; |
292 | } | |
293 | ||
ce141536 | 294 | value_obj = bt_value_array_borrow_element_by_index( |
17582c6d | 295 | attr_field_obj, BT_ATTR_VALUE_INDEX); |
561ad8c1 PP |
296 | if (!value_obj) { |
297 | BT_LOGE("Cannot get attribute array value's element by index: " | |
298 | "value-addr=%p, index=%" PRIu64, attr_field_obj, | |
839d52a5 | 299 | (uint64_t) BT_ATTR_VALUE_INDEX); |
561ad8c1 | 300 | } |
44e0a4f5 JG |
301 | |
302 | end: | |
44e0a4f5 JG |
303 | return value_obj; |
304 | } | |
305 | ||
306 | BT_HIDDEN | |
ce141536 | 307 | int bt_attributes_freeze(const struct bt_value *attr_obj) |
44e0a4f5 | 308 | { |
dcf0cc71 PP |
309 | uint64_t i; |
310 | int64_t count; | |
44e0a4f5 JG |
311 | int ret = 0; |
312 | ||
313 | if (!attr_obj) { | |
561ad8c1 | 314 | BT_LOGW_STR("Invalid parameter: attributes object is NULL."); |
44e0a4f5 JG |
315 | ret = -1; |
316 | goto end; | |
317 | } | |
318 | ||
561ad8c1 | 319 | BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj); |
ce141536 | 320 | count = bt_value_array_get_size(attr_obj); |
8b45963b | 321 | BT_ASSERT(count >= 0); |
44e0a4f5 JG |
322 | |
323 | /* | |
dac5c838 PP |
324 | * We do not freeze the array value object itself here, since |
325 | * internal stuff could need to modify/add attributes. Each | |
326 | * attribute is frozen one by one. | |
44e0a4f5 JG |
327 | */ |
328 | for (i = 0; i < count; ++i) { | |
ce141536 | 329 | struct bt_value *obj = NULL; |
44e0a4f5 | 330 | |
ce141536 PP |
331 | obj = bt_attributes_borrow_field_value( |
332 | (void *) attr_obj, i); | |
44e0a4f5 | 333 | if (!obj) { |
561ad8c1 PP |
334 | BT_LOGE("Cannot get attributes object's field value by index: " |
335 | "value-addr=%p, index=%" PRIu64, | |
336 | attr_obj, i); | |
44e0a4f5 JG |
337 | ret = -1; |
338 | goto end; | |
339 | } | |
340 | ||
ce141536 | 341 | bt_value_freeze(obj); |
44e0a4f5 JG |
342 | } |
343 | ||
344 | end: | |
44e0a4f5 JG |
345 | return ret; |
346 | } |