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