Visibility hidden by default
[babeltrace.git] / src / lib / trace-ir / attributes.c
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com>
6 */
7
8 #define BT_LOG_TAG "LIB/ATTRS"
9 #include "lib/logging.h"
10
11 #include "common/macros.h"
12 #include <babeltrace2/value.h>
13 #include "lib/assert-cond.h"
14 #include "lib/object.h"
15 #include <babeltrace2/value.h>
16 #include "lib/value.h"
17 #include "attributes.h"
18 #include <inttypes.h>
19 #include "compat/string.h"
20 #include "common/assert.h"
21
22 #define BT_ATTR_NAME_INDEX 0
23 #define BT_ATTR_VALUE_INDEX 1
24
25 struct bt_value *bt_attributes_create(void)
26 {
27 struct bt_value *attr_obj;
28
29 /*
30 * Attributes: array value object of array value objects, each one
31 * containing two entries: a string value object (attributes
32 * field name), and a value object (attributes field value).
33 *
34 * Example (JSON representation):
35 *
36 * [
37 * ["hostname", "eeppdesk"],
38 * ["sysname", "Linux"],
39 * ["tracer_major", 2],
40 * ["tracer_minor", 5]
41 * ]
42 */
43 BT_LOGD_STR("Creating attributes object.");
44 attr_obj = bt_value_array_create();
45 if (!attr_obj) {
46 BT_LIB_LOGE_APPEND_CAUSE("Failed to create array value.");
47 } else {
48 BT_LOGD("Created attributes object: addr=%p",
49 attr_obj);
50 }
51
52 return attr_obj;
53 }
54
55 void bt_attributes_destroy(struct bt_value *attr_obj)
56 {
57 BT_LOGD("Destroying attributes object: addr=%p", attr_obj);
58 BT_OBJECT_PUT_REF_AND_RESET(attr_obj);
59 }
60
61 uint64_t bt_attributes_get_count(const struct bt_value *attr_obj)
62 {
63 return bt_value_array_get_length(attr_obj);
64 }
65
66 const char *bt_attributes_get_field_name(const struct bt_value *attr_obj,
67 uint64_t index)
68 {
69 const struct bt_value *attr_field_obj = NULL;
70 const struct bt_value *attr_field_name_obj = NULL;
71
72 BT_ASSERT_DBG(attr_obj);
73 BT_ASSERT_DBG(index < bt_value_array_get_length(attr_obj));
74 attr_field_obj = bt_value_array_borrow_element_by_index_const(
75 attr_obj, index);
76
77 attr_field_name_obj =
78 bt_value_array_borrow_element_by_index_const(attr_field_obj,
79 BT_ATTR_NAME_INDEX);
80
81 return bt_value_string_get(attr_field_name_obj);
82 }
83
84 struct bt_value *bt_attributes_borrow_field_value(
85 struct bt_value *attr_obj, uint64_t index)
86 {
87 struct bt_value *attr_field_obj = NULL;
88
89 BT_ASSERT_DBG(attr_obj);
90 BT_ASSERT_DBG(index < bt_value_array_get_length(attr_obj));
91
92 attr_field_obj =
93 bt_value_array_borrow_element_by_index(attr_obj, index);
94
95 return bt_value_array_borrow_element_by_index( attr_field_obj,
96 BT_ATTR_VALUE_INDEX);
97 }
98
99 static
100 struct bt_value *bt_attributes_borrow_field_by_name(
101 struct bt_value *attr_obj, const char *name)
102 {
103 uint64_t i, attr_size;
104 struct bt_value *value_obj = NULL;
105 struct bt_value *attr_field_name_obj = NULL;
106
107 attr_size = bt_value_array_get_length(attr_obj);
108 for (i = 0; i < attr_size; ++i) {
109 const char *field_name;
110
111 value_obj = bt_value_array_borrow_element_by_index(
112 attr_obj, i);
113
114 attr_field_name_obj =
115 bt_value_array_borrow_element_by_index(
116 value_obj, BT_ATTR_NAME_INDEX);
117
118 field_name = bt_value_string_get(attr_field_name_obj);
119
120 if (strcmp(field_name, name) == 0) {
121 break;
122 }
123
124 value_obj = NULL;
125 }
126
127 return value_obj;
128 }
129
130 int bt_attributes_set_field_value(struct bt_value *attr_obj,
131 const char *name, struct bt_value *value_obj)
132 {
133 int ret = 0;
134 struct bt_value *attr_field_obj = NULL;
135
136 BT_ASSERT(attr_obj);
137 BT_ASSERT(name);
138 BT_ASSERT(value_obj);
139 attr_field_obj = bt_attributes_borrow_field_by_name(attr_obj, name);
140 if (attr_field_obj) {
141 ret = bt_value_array_set_element_by_index(
142 attr_field_obj, BT_ATTR_VALUE_INDEX,
143 value_obj);
144 attr_field_obj = NULL;
145 goto end;
146 }
147
148 attr_field_obj = bt_value_array_create();
149 if (!attr_field_obj) {
150 BT_LIB_LOGE_APPEND_CAUSE("Failed to create empty array value.");
151 ret = -1;
152 goto end;
153 }
154
155 ret = bt_value_array_append_string_element(attr_field_obj,
156 name);
157 ret |= bt_value_array_append_element(attr_field_obj,
158 value_obj);
159 if (ret) {
160 BT_LIB_LOGE_APPEND_CAUSE(
161 "Cannot append elements to array value: %!+v",
162 attr_field_obj);
163 goto end;
164 }
165
166 ret = bt_value_array_append_element(attr_obj,
167 attr_field_obj);
168 if (ret) {
169 BT_LIB_LOGE_APPEND_CAUSE(
170 "Cannot append element to array value: "
171 "%![array-value-]+v, %![element-value-]+v",
172 attr_obj, attr_field_obj);
173 }
174
175 end:
176 bt_object_put_ref(attr_field_obj);
177 return ret;
178 }
179
180 struct bt_value *bt_attributes_borrow_field_value_by_name(
181 struct bt_value *attr_obj, const char *name)
182 {
183 struct bt_value *value_obj = NULL;
184 struct bt_value *attr_field_obj = NULL;
185
186 BT_ASSERT_DBG(attr_obj);
187 BT_ASSERT_DBG(name);
188 attr_field_obj = bt_attributes_borrow_field_by_name(attr_obj, name);
189 if (!attr_field_obj) {
190 BT_LOGD("Cannot find attributes object's field by name: "
191 "value-addr=%p, name=\"%s\"", attr_obj, name);
192 goto end;
193 }
194
195 value_obj = bt_value_array_borrow_element_by_index(
196 attr_field_obj, BT_ATTR_VALUE_INDEX);
197
198 end:
199 return value_obj;
200 }
201
202 int bt_attributes_freeze(const struct bt_value *attr_obj)
203 {
204 uint64_t i, count;
205 int ret = 0;
206
207 BT_ASSERT(attr_obj);
208 BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj);
209
210 count = bt_value_array_get_length(attr_obj);
211
212 /*
213 * We do not freeze the array value object itself here, since
214 * internal stuff could need to modify/add attributes. Each
215 * attribute is frozen one by one.
216 */
217 for (i = 0; i < count; ++i) {
218 struct bt_value *obj = NULL;
219
220 obj = bt_attributes_borrow_field_value(
221 (void *) attr_obj, i);
222 if (!obj) {
223 BT_LIB_LOGE_APPEND_CAUSE(
224 "Cannot get attributes object's field value by index: "
225 "%![value-]+v, index=%" PRIu64,
226 attr_obj, i);
227 ret = -1;
228 goto end;
229 }
230
231 bt_value_freeze(obj);
232 }
233
234 end:
235 return ret;
236 }
This page took 0.037985 seconds and 4 git commands to generate.