4512880a5c6ec903f26e3f5af669a677a85dc653
[babeltrace.git] / src / lib / trace-ir / attributes.c
1 /*
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
24 #define BT_LOG_TAG "LIB/ATTRS"
25 #include "lib/logging.h"
26
27 #include "common/macros.h"
28 #include <babeltrace2/value.h>
29 #include "lib/assert-pre.h"
30 #include "lib/object.h"
31 #include <babeltrace2/value-const.h>
32 #include "lib/value.h"
33 #include "attributes.h"
34 #include <inttypes.h>
35 #include "compat/string.h"
36 #include "common/assert.h"
37
38 #define BT_ATTR_NAME_INDEX 0
39 #define BT_ATTR_VALUE_INDEX 1
40
41 BT_HIDDEN
42 struct bt_value *bt_attributes_create(void)
43 {
44 struct bt_value *attr_obj;
45
46 /*
47 * Attributes: array value object of array value objects, each one
48 * containing two entries: a string value object (attributes
49 * field name), and a value object (attributes field value).
50 *
51 * Example (JSON representation):
52 *
53 * [
54 * ["hostname", "eeppdesk"],
55 * ["sysname", "Linux"],
56 * ["tracer_major", 2],
57 * ["tracer_minor", 5]
58 * ]
59 */
60 BT_LOGD_STR("Creating attributes object.");
61 attr_obj = bt_value_array_create();
62 if (!attr_obj) {
63 BT_LIB_LOGE_APPEND_CAUSE("Failed to create array value.");
64 } else {
65 BT_LOGD("Created attributes object: addr=%p",
66 attr_obj);
67 }
68
69 return attr_obj;
70 }
71
72 BT_HIDDEN
73 void bt_attributes_destroy(struct bt_value *attr_obj)
74 {
75 BT_LOGD("Destroying attributes object: addr=%p", attr_obj);
76 BT_OBJECT_PUT_REF_AND_RESET(attr_obj);
77 }
78
79 BT_HIDDEN
80 uint64_t bt_attributes_get_count(const struct bt_value *attr_obj)
81 {
82 return bt_value_array_get_length(attr_obj);
83 }
84
85 BT_HIDDEN
86 const char *bt_attributes_get_field_name(const struct bt_value *attr_obj,
87 uint64_t index)
88 {
89 const char *ret = NULL;
90 const struct bt_value *attr_field_obj = NULL;
91 const struct bt_value *attr_field_name_obj = NULL;
92
93 BT_ASSERT_DBG(attr_obj);
94 BT_ASSERT_DBG(index < bt_value_array_get_length(attr_obj));
95 attr_field_obj = bt_value_array_borrow_element_by_index_const(
96 attr_obj, index);
97 if (!attr_field_obj) {
98 BT_LIB_LOGE_APPEND_CAUSE(
99 "Cannot borrow attributes object's array value's element by index: "
100 "%![value-]+v, index=%" PRIu64, attr_obj, index);
101 goto end;
102 }
103
104 attr_field_name_obj =
105 bt_value_array_borrow_element_by_index_const(attr_field_obj,
106 BT_ATTR_NAME_INDEX);
107 if (!attr_field_name_obj) {
108 BT_LIB_LOGE_APPEND_CAUSE(
109 "Cannot get attribute array value's element by index: "
110 "%![value-]+v, index=%" PRIu64, attr_field_obj,
111 (uint64_t) BT_ATTR_NAME_INDEX);
112 goto end;
113 }
114
115 ret = bt_value_string_get(attr_field_name_obj);
116
117 end:
118 return ret;
119 }
120
121 BT_HIDDEN
122 struct bt_value *bt_attributes_borrow_field_value(
123 struct bt_value *attr_obj, uint64_t index)
124 {
125 struct bt_value *value_obj = NULL;
126 struct bt_value *attr_field_obj = NULL;
127
128 BT_ASSERT_DBG(attr_obj);
129 BT_ASSERT_DBG(index < bt_value_array_get_length(attr_obj));
130 attr_field_obj =
131 bt_value_array_borrow_element_by_index(attr_obj, index);
132 if (!attr_field_obj) {
133 BT_LIB_LOGE_APPEND_CAUSE(
134 "Cannot get attributes object's array value's element by index: "
135 "%![value-]+v, index=%" PRIu64, attr_obj, index);
136 goto end;
137 }
138
139 value_obj = bt_value_array_borrow_element_by_index(
140 attr_field_obj, BT_ATTR_VALUE_INDEX);
141 if (!value_obj) {
142 BT_LIB_LOGE_APPEND_CAUSE(
143 "Cannot get attribute array value's element by index: "
144 "%![value-]+v, index=%" PRIu64, attr_field_obj,
145 (uint64_t) BT_ATTR_VALUE_INDEX);
146 }
147
148 end:
149 return value_obj;
150 }
151
152 static
153 struct bt_value *bt_attributes_borrow_field_by_name(
154 struct bt_value *attr_obj, const char *name)
155 {
156 uint64_t i, attr_size;
157 struct bt_value *value_obj = NULL;
158 struct bt_value *attr_field_name_obj = NULL;
159
160 attr_size = bt_value_array_get_length(attr_obj);
161 for (i = 0; i < attr_size; ++i) {
162 const char *field_name;
163
164 value_obj = bt_value_array_borrow_element_by_index(
165 attr_obj, i);
166 if (!value_obj) {
167 BT_LIB_LOGE_APPEND_CAUSE(
168 "Cannot get attributes object's array value's element by index: "
169 "%![value-]+v, index=%" PRIu64, attr_obj, i);
170 goto error;
171 }
172
173 attr_field_name_obj =
174 bt_value_array_borrow_element_by_index(
175 value_obj, BT_ATTR_NAME_INDEX);
176 if (!attr_field_name_obj) {
177 BT_LIB_LOGE_APPEND_CAUSE(
178 "Cannot get attribute array value's element by index: "
179 "%![value-]+v, index=%" PRIu64,
180 value_obj, (int64_t) BT_ATTR_NAME_INDEX);
181 goto error;
182 }
183
184 field_name = bt_value_string_get(attr_field_name_obj);
185
186 if (strcmp(field_name, name) == 0) {
187 break;
188 }
189
190 value_obj = NULL;
191 }
192
193 return value_obj;
194
195 error:
196 value_obj = NULL;
197 return value_obj;
198 }
199
200 BT_HIDDEN
201 int bt_attributes_set_field_value(struct bt_value *attr_obj,
202 const char *name, struct bt_value *value_obj)
203 {
204 int ret = 0;
205 struct bt_value *attr_field_obj = NULL;
206
207 BT_ASSERT(attr_obj);
208 BT_ASSERT(name);
209 BT_ASSERT(value_obj);
210 attr_field_obj = bt_attributes_borrow_field_by_name(attr_obj, name);
211 if (attr_field_obj) {
212 ret = bt_value_array_set_element_by_index(
213 attr_field_obj, BT_ATTR_VALUE_INDEX,
214 value_obj);
215 attr_field_obj = NULL;
216 goto end;
217 }
218
219 attr_field_obj = bt_value_array_create();
220 if (!attr_field_obj) {
221 BT_LIB_LOGE_APPEND_CAUSE("Failed to create empty array value.");
222 ret = -1;
223 goto end;
224 }
225
226 ret = bt_value_array_append_string_element(attr_field_obj,
227 name);
228 ret |= bt_value_array_append_element(attr_field_obj,
229 value_obj);
230 if (ret) {
231 BT_LIB_LOGE_APPEND_CAUSE(
232 "Cannot append elements to array value: %!+v",
233 attr_field_obj);
234 goto end;
235 }
236
237 ret = bt_value_array_append_element(attr_obj,
238 attr_field_obj);
239 if (ret) {
240 BT_LIB_LOGE_APPEND_CAUSE(
241 "Cannot append element to array value: "
242 "%![array-value-]+v, %![element-value-]+v",
243 attr_obj, attr_field_obj);
244 }
245
246 end:
247 bt_object_put_ref(attr_field_obj);
248 return ret;
249 }
250
251 BT_HIDDEN
252 struct bt_value *bt_attributes_borrow_field_value_by_name(
253 struct bt_value *attr_obj, const char *name)
254 {
255 struct bt_value *value_obj = NULL;
256 struct bt_value *attr_field_obj = NULL;
257
258 BT_ASSERT_DBG(attr_obj);
259 BT_ASSERT_DBG(name);
260 attr_field_obj = bt_attributes_borrow_field_by_name(attr_obj, name);
261 if (!attr_field_obj) {
262 BT_LOGD("Cannot find attributes object's field by name: "
263 "value-addr=%p, name=\"%s\"", attr_obj, name);
264 goto end;
265 }
266
267 value_obj = bt_value_array_borrow_element_by_index(
268 attr_field_obj, BT_ATTR_VALUE_INDEX);
269 if (!value_obj) {
270 BT_LIB_LOGE_APPEND_CAUSE(
271 "Cannot get attribute array value's element by index: "
272 "%![value-]+v, index=%" PRIu64, attr_field_obj,
273 (uint64_t) BT_ATTR_VALUE_INDEX);
274 }
275
276 end:
277 return value_obj;
278 }
279
280 BT_HIDDEN
281 int bt_attributes_freeze(const struct bt_value *attr_obj)
282 {
283 uint64_t i, count;
284 int ret = 0;
285
286 BT_ASSERT(attr_obj);
287 BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj);
288
289 count = bt_value_array_get_length(attr_obj);
290
291 /*
292 * We do not freeze the array value object itself here, since
293 * internal stuff could need to modify/add attributes. Each
294 * attribute is frozen one by one.
295 */
296 for (i = 0; i < count; ++i) {
297 struct bt_value *obj = NULL;
298
299 obj = bt_attributes_borrow_field_value(
300 (void *) attr_obj, i);
301 if (!obj) {
302 BT_LIB_LOGE_APPEND_CAUSE(
303 "Cannot get attributes object's field value by index: "
304 "%![value-]+v, index=%" PRIu64,
305 attr_obj, i);
306 ret = -1;
307 goto end;
308 }
309
310 bt_value_freeze(obj);
311 }
312
313 end:
314 return ret;
315 }
This page took 0.036199 seconds and 3 git commands to generate.