bt2: split clock value module from clock class module
[babeltrace.git] / lib / ctf-ir / attributes.c
CommitLineData
44e0a4f5
JG
1/*
2 * attributes.c
3 *
4 * Babeltrace CTF IR - 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
0f5e83e5 28#define BT_LOG_TAG "ATTRS"
40547d22 29#include <babeltrace/lib-logging-internal.h>
0f5e83e5 30
44e0a4f5 31#include <babeltrace/babeltrace-internal.h>
dac5c838 32#include <babeltrace/values.h>
561ad8c1 33#include <inttypes.h>
0e4802f6 34#include <assert.h>
ee389f01 35#include <babeltrace/compat/string-internal.h>
44e0a4f5
JG
36
37#define BT_CTF_ATTR_NAME_INDEX 0
83509119 38#define BT_CTF_ATTR_VALUE_INDEX 1
44e0a4f5
JG
39
40BT_HIDDEN
dac5c838 41struct bt_value *bt_ctf_attributes_create(void)
44e0a4f5 42{
561ad8c1
PP
43 struct bt_value *attr_obj;
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
PP
59 BT_LOGD_STR("Creating attributes object.");
60 attr_obj = bt_value_array_create();
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
71BT_HIDDEN
dac5c838 72void bt_ctf_attributes_destroy(struct bt_value *attr_obj)
44e0a4f5 73{
561ad8c1 74 BT_LOGD("Destroying attributes object: addr=%p", attr_obj);
83509119 75 bt_put(attr_obj);
44e0a4f5
JG
76}
77
78BT_HIDDEN
544d0515 79int64_t bt_ctf_attributes_get_count(struct bt_value *attr_obj)
44e0a4f5 80{
dac5c838 81 return bt_value_array_size(attr_obj);
44e0a4f5
JG
82}
83
84BT_HIDDEN
dac5c838 85const char *bt_ctf_attributes_get_field_name(struct bt_value *attr_obj,
dcf0cc71 86 uint64_t index)
44e0a4f5
JG
87{
88 int rc;
89 const char *ret = NULL;
dac5c838
PP
90 struct bt_value *attr_field_obj = NULL;
91 struct bt_value *attr_field_name_obj = NULL;
44e0a4f5 92
561ad8c1
PP
93 if (!attr_obj) {
94 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
44e0a4f5
JG
95 goto end;
96 }
97
dac5c838 98 attr_field_obj = bt_value_array_get(attr_obj, index);
44e0a4f5 99 if (!attr_field_obj) {
561ad8c1
PP
100 BT_LOGE("Cannot get attributes object's array value's element by index: "
101 "value-addr=%p, index=%" PRIu64, attr_obj, index);
44e0a4f5
JG
102 goto end;
103 }
104
dac5c838 105 attr_field_name_obj = bt_value_array_get(attr_field_obj,
44e0a4f5 106 BT_CTF_ATTR_NAME_INDEX);
44e0a4f5 107 if (!attr_field_name_obj) {
561ad8c1
PP
108 BT_LOGE("Cannot get attribute array value's element by index: "
109 "value-addr=%p, index=%" PRIu64, attr_field_obj,
110 (uint64_t) BT_CTF_ATTR_NAME_INDEX);
44e0a4f5
JG
111 goto end;
112 }
113
dac5c838 114 rc = bt_value_string_get(attr_field_name_obj, &ret);
44e0a4f5 115 if (rc) {
561ad8c1
PP
116 BT_LOGE("Cannot get raw value from string value: value-addr=%p",
117 attr_field_name_obj);
44e0a4f5
JG
118 ret = NULL;
119 }
120
121end:
83509119
JG
122 BT_PUT(attr_field_name_obj);
123 BT_PUT(attr_field_obj);
44e0a4f5
JG
124 return ret;
125}
126
127BT_HIDDEN
dac5c838 128struct bt_value *bt_ctf_attributes_get_field_value(struct bt_value *attr_obj,
dcf0cc71 129 uint64_t index)
44e0a4f5 130{
dac5c838
PP
131 struct bt_value *value_obj = NULL;
132 struct bt_value *attr_field_obj = NULL;
44e0a4f5 133
561ad8c1
PP
134 if (!attr_obj) {
135 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
44e0a4f5
JG
136 goto end;
137 }
138
dac5c838 139 attr_field_obj = bt_value_array_get(attr_obj, index);
44e0a4f5 140 if (!attr_field_obj) {
561ad8c1
PP
141 BT_LOGE("Cannot get attributes object's array value's element by index: "
142 "value-addr=%p, index=%" PRIu64, attr_obj, index);
44e0a4f5
JG
143 goto end;
144 }
145
dac5c838 146 value_obj = bt_value_array_get(attr_field_obj,
44e0a4f5 147 BT_CTF_ATTR_VALUE_INDEX);
561ad8c1
PP
148 if (!value_obj) {
149 BT_LOGE("Cannot get attribute array value's element by index: "
150 "value-addr=%p, index=%" PRIu64, attr_field_obj,
151 (uint64_t) BT_CTF_ATTR_VALUE_INDEX);
152 }
44e0a4f5
JG
153
154end:
83509119 155 BT_PUT(attr_field_obj);
44e0a4f5
JG
156 return value_obj;
157}
158
159static
dac5c838
PP
160struct bt_value *bt_ctf_attributes_get_field_by_name(
161 struct bt_value *attr_obj, const char *name)
44e0a4f5 162{
dcf0cc71
PP
163 uint64_t i;
164 int64_t attr_size;
dac5c838
PP
165 struct bt_value *value_obj = NULL;
166 struct bt_value *attr_field_name_obj = NULL;
44e0a4f5 167
dac5c838 168 attr_size = bt_value_array_size(attr_obj);
44e0a4f5 169 if (attr_size < 0) {
561ad8c1
PP
170 BT_LOGE("Cannot get array value's size: value-addr=%p",
171 attr_obj);
44e0a4f5
JG
172 goto error;
173 }
174
175 for (i = 0; i < attr_size; ++i) {
176 int ret;
177 const char *field_name;
178
dac5c838 179 value_obj = bt_value_array_get(attr_obj, i);
44e0a4f5 180 if (!value_obj) {
561ad8c1
PP
181 BT_LOGE("Cannot get attributes object's array value's element by index: "
182 "value-addr=%p, index=%" PRIu64, attr_obj, i);
44e0a4f5
JG
183 goto error;
184 }
185
dac5c838 186 attr_field_name_obj = bt_value_array_get(value_obj, 0);
44e0a4f5 187 if (!attr_field_name_obj) {
561ad8c1
PP
188 BT_LOGE("Cannot get attribute array value's element by index: "
189 "value-addr=%p, index=%" PRIu64,
32e87ceb 190 value_obj, (int64_t) 0);
44e0a4f5
JG
191 goto error;
192 }
193
dac5c838 194 ret = bt_value_string_get(attr_field_name_obj, &field_name);
44e0a4f5 195 if (ret) {
561ad8c1
PP
196 BT_LOGE("Cannot get raw value from string value: value-addr=%p",
197 attr_field_name_obj);
44e0a4f5
JG
198 goto error;
199 }
200
201 if (!strcmp(field_name, name)) {
83509119 202 BT_PUT(attr_field_name_obj);
44e0a4f5
JG
203 break;
204 }
205
83509119
JG
206 BT_PUT(attr_field_name_obj);
207 BT_PUT(value_obj);
44e0a4f5
JG
208 }
209
210 return value_obj;
211
212error:
83509119
JG
213 BT_PUT(attr_field_name_obj);
214 BT_PUT(value_obj);
44e0a4f5
JG
215
216 return value_obj;
217}
218
219BT_HIDDEN
dac5c838
PP
220int bt_ctf_attributes_set_field_value(struct bt_value *attr_obj,
221 const char *name, struct bt_value *value_obj)
44e0a4f5
JG
222{
223 int ret = 0;
dac5c838 224 struct bt_value *attr_field_obj = NULL;
44e0a4f5
JG
225
226 if (!attr_obj || !name || !value_obj) {
561ad8c1
PP
227 BT_LOGW("Invalid parameter: attributes object, name, or value object is NULL: "
228 "attr-value-addr=%p, name-addr=%p, value-addr=%p",
229 attr_obj, name, value_obj);
44e0a4f5
JG
230 ret = -1;
231 goto end;
232 }
233
234 attr_field_obj = bt_ctf_attributes_get_field_by_name(attr_obj, name);
44e0a4f5 235 if (attr_field_obj) {
dac5c838 236 ret = bt_value_array_set(attr_field_obj,
44e0a4f5
JG
237 BT_CTF_ATTR_VALUE_INDEX, value_obj);
238 goto end;
239 }
240
dac5c838 241 attr_field_obj = bt_value_array_create();
44e0a4f5 242 if (!attr_field_obj) {
561ad8c1 243 BT_LOGE_STR("Failed to create empty array value.");
44e0a4f5
JG
244 ret = -1;
245 goto end;
246 }
247
dac5c838
PP
248 ret = bt_value_array_append_string(attr_field_obj, name);
249 ret |= bt_value_array_append(attr_field_obj, value_obj);
44e0a4f5 250 if (ret) {
561ad8c1
PP
251 BT_LOGE("Cannot append elements to array value: addr=%p",
252 attr_field_obj);
44e0a4f5
JG
253 goto end;
254 }
255
dac5c838 256 ret = bt_value_array_append(attr_obj, attr_field_obj);
561ad8c1
PP
257 if (ret) {
258 BT_LOGE("Cannot append element to array value: "
259 "array-value-addr=%p, element-value-addr=%p",
260 attr_obj, attr_field_obj);
261 }
44e0a4f5
JG
262
263end:
83509119 264 BT_PUT(attr_field_obj);
44e0a4f5
JG
265
266 return ret;
267}
268
269BT_HIDDEN
dac5c838
PP
270struct bt_value *bt_ctf_attributes_get_field_value_by_name(
271 struct bt_value *attr_obj, const char *name)
44e0a4f5 272{
dac5c838
PP
273 struct bt_value *value_obj = NULL;
274 struct bt_value *attr_field_obj = NULL;
44e0a4f5
JG
275
276 if (!attr_obj || !name) {
561ad8c1
PP
277 BT_LOGW("Invalid parameter: attributes object or name is NULL: "
278 "value-addr=%p, name-addr=%p", attr_obj, name);
44e0a4f5
JG
279 goto end;
280 }
281
282 attr_field_obj = bt_ctf_attributes_get_field_by_name(attr_obj, name);
44e0a4f5 283 if (!attr_field_obj) {
561ad8c1
PP
284 BT_LOGD("Cannot find attributes object's field by name: "
285 "value-addr=%p, name=\"%s\"", attr_obj, name);
44e0a4f5
JG
286 goto end;
287 }
288
dac5c838 289 value_obj = bt_value_array_get(attr_field_obj,
44e0a4f5 290 BT_CTF_ATTR_VALUE_INDEX);
561ad8c1
PP
291 if (!value_obj) {
292 BT_LOGE("Cannot get attribute array value's element by index: "
293 "value-addr=%p, index=%" PRIu64, attr_field_obj,
294 (uint64_t) BT_CTF_ATTR_VALUE_INDEX);
295 }
44e0a4f5
JG
296
297end:
83509119 298 BT_PUT(attr_field_obj);
44e0a4f5
JG
299
300 return value_obj;
301}
302
303BT_HIDDEN
dac5c838 304int bt_ctf_attributes_freeze(struct bt_value *attr_obj)
44e0a4f5 305{
dcf0cc71
PP
306 uint64_t i;
307 int64_t count;
44e0a4f5
JG
308 int ret = 0;
309
310 if (!attr_obj) {
561ad8c1 311 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
44e0a4f5
JG
312 ret = -1;
313 goto end;
314 }
315
561ad8c1 316 BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj);
dac5c838 317 count = bt_value_array_size(attr_obj);
0e4802f6 318 assert(count >= 0);
44e0a4f5
JG
319
320 /*
dac5c838
PP
321 * We do not freeze the array value object itself here, since
322 * internal stuff could need to modify/add attributes. Each
323 * attribute is frozen one by one.
44e0a4f5
JG
324 */
325 for (i = 0; i < count; ++i) {
dac5c838 326 struct bt_value *obj = NULL;
44e0a4f5
JG
327
328 obj = bt_ctf_attributes_get_field_value(attr_obj, i);
44e0a4f5 329 if (!obj) {
561ad8c1
PP
330 BT_LOGE("Cannot get attributes object's field value by index: "
331 "value-addr=%p, index=%" PRIu64,
332 attr_obj, i);
44e0a4f5
JG
333 ret = -1;
334 goto end;
335 }
336
dac5c838 337 bt_value_freeze(obj);
83509119 338 BT_PUT(obj);
44e0a4f5
JG
339 }
340
341end:
44e0a4f5
JG
342 return ret;
343}
This page took 0.058687 seconds and 4 git commands to generate.