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