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