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