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