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