| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
| 5 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 6 | */ |
| 7 | |
| 8 | #define BT_LOG_TAG "LIB/EVENT-CLASS" |
| 9 | #include "lib/logging.h" |
| 10 | |
| 11 | #include "lib/assert-cond.h" |
| 12 | #include <babeltrace2/trace-ir/field-class.h> |
| 13 | #include <babeltrace2/trace-ir/event-class.h> |
| 14 | #include <babeltrace2/trace-ir/stream-class.h> |
| 15 | #include "compat/compiler.h" |
| 16 | #include "compat/endian.h" |
| 17 | #include <babeltrace2/types.h> |
| 18 | #include "lib/value.h" |
| 19 | #include "common/assert.h" |
| 20 | #include <inttypes.h> |
| 21 | #include <stdbool.h> |
| 22 | #include <stdlib.h> |
| 23 | |
| 24 | #include "event-class.h" |
| 25 | #include "event.h" |
| 26 | #include "field-class.h" |
| 27 | #include "resolve-field-path.h" |
| 28 | #include "stream-class.h" |
| 29 | #include "lib/func-status.h" |
| 30 | |
| 31 | #define BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(_ec) \ |
| 32 | BT_ASSERT_PRE_DEV_HOT("event-class", \ |
| 33 | ((const struct bt_event_class *) (_ec)), \ |
| 34 | "Event class", ": %!+E", (_ec)) |
| 35 | |
| 36 | static |
| 37 | void destroy_event_class(struct bt_object *obj) |
| 38 | { |
| 39 | struct bt_event_class *event_class = (void *) obj; |
| 40 | |
| 41 | BT_LIB_LOGD("Destroying event class: %!+E", event_class); |
| 42 | BT_OBJECT_PUT_REF_AND_RESET(event_class->user_attributes); |
| 43 | |
| 44 | if (event_class->name.str) { |
| 45 | g_string_free(event_class->name.str, TRUE); |
| 46 | event_class->name.str = NULL; |
| 47 | } |
| 48 | |
| 49 | if (event_class->emf_uri.str) { |
| 50 | g_string_free(event_class->emf_uri.str, TRUE); |
| 51 | event_class->emf_uri.str = NULL; |
| 52 | } |
| 53 | |
| 54 | BT_LOGD_STR("Putting context field class."); |
| 55 | BT_OBJECT_PUT_REF_AND_RESET(event_class->specific_context_fc); |
| 56 | BT_LOGD_STR("Putting payload field class."); |
| 57 | BT_OBJECT_PUT_REF_AND_RESET(event_class->payload_fc); |
| 58 | bt_object_pool_finalize(&event_class->event_pool); |
| 59 | g_free(obj); |
| 60 | } |
| 61 | |
| 62 | static |
| 63 | void free_event(struct bt_event *event, |
| 64 | struct bt_event_class *event_class __attribute__((unused))) |
| 65 | { |
| 66 | bt_event_destroy(event); |
| 67 | } |
| 68 | |
| 69 | static |
| 70 | bool event_class_id_is_unique(const struct bt_stream_class *stream_class, |
| 71 | uint64_t id) |
| 72 | { |
| 73 | uint64_t i; |
| 74 | bool is_unique = true; |
| 75 | |
| 76 | for (i = 0; i < stream_class->event_classes->len; i++) { |
| 77 | const struct bt_event_class *ec = |
| 78 | stream_class->event_classes->pdata[i]; |
| 79 | |
| 80 | if (ec->id == id) { |
| 81 | is_unique = false; |
| 82 | goto end; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | end: |
| 87 | return is_unique; |
| 88 | } |
| 89 | |
| 90 | static |
| 91 | struct bt_event_class *create_event_class_with_id( |
| 92 | struct bt_stream_class *stream_class, uint64_t id) |
| 93 | { |
| 94 | int ret; |
| 95 | struct bt_event_class *event_class; |
| 96 | |
| 97 | BT_ASSERT(stream_class); |
| 98 | BT_ASSERT_PRE("event-class-id-is-unique", |
| 99 | event_class_id_is_unique(stream_class, id), |
| 100 | "Duplicate event class ID: %![sc-]+S, id=%" PRIu64, |
| 101 | stream_class, id); |
| 102 | BT_LIB_LOGD("Creating event class object: %![sc-]+S, id=%" PRIu64, |
| 103 | stream_class, id); |
| 104 | event_class = g_new0(struct bt_event_class, 1); |
| 105 | if (!event_class) { |
| 106 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one event class."); |
| 107 | goto error; |
| 108 | } |
| 109 | |
| 110 | bt_object_init_shared_with_parent(&event_class->base, |
| 111 | destroy_event_class); |
| 112 | event_class->user_attributes = bt_value_map_create(); |
| 113 | if (!event_class->user_attributes) { |
| 114 | BT_LIB_LOGE_APPEND_CAUSE( |
| 115 | "Failed to create a map value object."); |
| 116 | goto error; |
| 117 | } |
| 118 | |
| 119 | event_class->id = id; |
| 120 | bt_property_uint_init(&event_class->log_level, |
| 121 | BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE, 0); |
| 122 | event_class->name.str = g_string_new(NULL); |
| 123 | if (!event_class->name.str) { |
| 124 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString."); |
| 125 | goto error; |
| 126 | } |
| 127 | |
| 128 | event_class->emf_uri.str = g_string_new(NULL); |
| 129 | if (!event_class->emf_uri.str) { |
| 130 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString."); |
| 131 | goto error; |
| 132 | } |
| 133 | |
| 134 | ret = bt_object_pool_initialize(&event_class->event_pool, |
| 135 | (bt_object_pool_new_object_func) bt_event_new, |
| 136 | (bt_object_pool_destroy_object_func) free_event, |
| 137 | event_class); |
| 138 | if (ret) { |
| 139 | BT_LIB_LOGE_APPEND_CAUSE( |
| 140 | "Failed to initialize event pool: ret=%d", |
| 141 | ret); |
| 142 | goto error; |
| 143 | } |
| 144 | |
| 145 | bt_object_set_parent(&event_class->base, &stream_class->base); |
| 146 | g_ptr_array_add(stream_class->event_classes, event_class); |
| 147 | bt_stream_class_freeze(stream_class); |
| 148 | BT_LIB_LOGD("Created event class object: %!+E", event_class); |
| 149 | goto end; |
| 150 | |
| 151 | error: |
| 152 | BT_OBJECT_PUT_REF_AND_RESET(event_class); |
| 153 | |
| 154 | end: |
| 155 | return event_class; |
| 156 | } |
| 157 | |
| 158 | BT_EXPORT |
| 159 | struct bt_event_class *bt_event_class_create( |
| 160 | struct bt_stream_class *stream_class) |
| 161 | { |
| 162 | BT_ASSERT_PRE_NO_ERROR(); |
| 163 | BT_ASSERT_PRE_SC_NON_NULL(stream_class); |
| 164 | BT_ASSERT_PRE( |
| 165 | "stream-class-automatically-assigns-event-class-ids", |
| 166 | stream_class->assigns_automatic_event_class_id, |
| 167 | "Stream class does not automatically assigns event class IDs: " |
| 168 | "%![sc-]+S", stream_class); |
| 169 | return create_event_class_with_id(stream_class, |
| 170 | (uint64_t) stream_class->event_classes->len); |
| 171 | } |
| 172 | |
| 173 | BT_EXPORT |
| 174 | struct bt_event_class *bt_event_class_create_with_id( |
| 175 | struct bt_stream_class *stream_class, uint64_t id) |
| 176 | { |
| 177 | BT_ASSERT_PRE_NO_ERROR(); |
| 178 | BT_ASSERT_PRE( |
| 179 | "stream-class-does-not-automatically-assigns-event-class-ids", |
| 180 | !stream_class->assigns_automatic_event_class_id, |
| 181 | "Stream class automatically assigns event class IDs: " |
| 182 | "%![sc-]+S", stream_class); |
| 183 | return create_event_class_with_id(stream_class, id); |
| 184 | } |
| 185 | |
| 186 | BT_EXPORT |
| 187 | const char *bt_event_class_get_name(const struct bt_event_class *event_class) |
| 188 | { |
| 189 | BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class); |
| 190 | return event_class->name.value; |
| 191 | } |
| 192 | |
| 193 | BT_EXPORT |
| 194 | enum bt_event_class_set_name_status bt_event_class_set_name( |
| 195 | struct bt_event_class *event_class, const char *name) |
| 196 | { |
| 197 | BT_ASSERT_PRE_NO_ERROR(); |
| 198 | BT_ASSERT_PRE_EC_NON_NULL(event_class); |
| 199 | BT_ASSERT_PRE_NAME_NON_NULL(name); |
| 200 | BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class); |
| 201 | g_string_assign(event_class->name.str, name); |
| 202 | event_class->name.value = event_class->name.str->str; |
| 203 | BT_LIB_LOGD("Set event class's name: %!+E", event_class); |
| 204 | return BT_FUNC_STATUS_OK; |
| 205 | } |
| 206 | |
| 207 | BT_EXPORT |
| 208 | uint64_t bt_event_class_get_id(const struct bt_event_class *event_class) |
| 209 | { |
| 210 | BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class); |
| 211 | return event_class->id; |
| 212 | } |
| 213 | |
| 214 | BT_EXPORT |
| 215 | enum bt_property_availability bt_event_class_get_log_level( |
| 216 | const struct bt_event_class *event_class, |
| 217 | enum bt_event_class_log_level *log_level) |
| 218 | { |
| 219 | BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class); |
| 220 | BT_ASSERT_PRE_DEV_NON_NULL("log-level-output", log_level, |
| 221 | "Log level (output)"); |
| 222 | *log_level = (enum bt_event_class_log_level) |
| 223 | event_class->log_level.value; |
| 224 | return event_class->log_level.base.avail; |
| 225 | } |
| 226 | |
| 227 | BT_EXPORT |
| 228 | void bt_event_class_set_log_level( |
| 229 | struct bt_event_class *event_class, |
| 230 | enum bt_event_class_log_level log_level) |
| 231 | { |
| 232 | BT_ASSERT_PRE_EC_NON_NULL(event_class); |
| 233 | BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class); |
| 234 | bt_property_uint_set(&event_class->log_level, |
| 235 | (uint64_t) log_level); |
| 236 | BT_LIB_LOGD("Set event class's log level: %!+E", event_class); |
| 237 | } |
| 238 | |
| 239 | BT_EXPORT |
| 240 | const char *bt_event_class_get_emf_uri(const struct bt_event_class *event_class) |
| 241 | { |
| 242 | BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class); |
| 243 | return event_class->emf_uri.value; |
| 244 | } |
| 245 | |
| 246 | BT_EXPORT |
| 247 | enum bt_event_class_set_emf_uri_status bt_event_class_set_emf_uri( |
| 248 | struct bt_event_class *event_class, |
| 249 | const char *emf_uri) |
| 250 | { |
| 251 | BT_ASSERT_PRE_NO_ERROR(); |
| 252 | BT_ASSERT_PRE_EC_NON_NULL(event_class); |
| 253 | BT_ASSERT_PRE_NON_NULL("emf-uri", emf_uri, "EMF URI"); |
| 254 | BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class); |
| 255 | g_string_assign(event_class->emf_uri.str, emf_uri); |
| 256 | event_class->emf_uri.value = event_class->emf_uri.str->str; |
| 257 | BT_LIB_LOGD("Set event class's EMF URI: %!+E", event_class); |
| 258 | return BT_FUNC_STATUS_OK; |
| 259 | } |
| 260 | |
| 261 | BT_EXPORT |
| 262 | struct bt_stream_class *bt_event_class_borrow_stream_class( |
| 263 | struct bt_event_class *event_class) |
| 264 | { |
| 265 | BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class); |
| 266 | return bt_event_class_borrow_stream_class_inline(event_class); |
| 267 | } |
| 268 | |
| 269 | BT_EXPORT |
| 270 | const struct bt_stream_class * |
| 271 | bt_event_class_borrow_stream_class_const( |
| 272 | const struct bt_event_class *event_class) |
| 273 | { |
| 274 | return bt_event_class_borrow_stream_class((void *) event_class); |
| 275 | } |
| 276 | |
| 277 | BT_EXPORT |
| 278 | const struct bt_field_class * |
| 279 | bt_event_class_borrow_specific_context_field_class_const( |
| 280 | const struct bt_event_class *event_class) |
| 281 | { |
| 282 | BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class); |
| 283 | return event_class->specific_context_fc; |
| 284 | } |
| 285 | |
| 286 | BT_EXPORT |
| 287 | struct bt_field_class * |
| 288 | bt_event_class_borrow_specific_context_field_class( |
| 289 | struct bt_event_class *event_class) |
| 290 | { |
| 291 | BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class); |
| 292 | return event_class->specific_context_fc; |
| 293 | } |
| 294 | |
| 295 | BT_EXPORT |
| 296 | enum bt_event_class_set_field_class_status |
| 297 | bt_event_class_set_specific_context_field_class( |
| 298 | struct bt_event_class *event_class, |
| 299 | struct bt_field_class *field_class) |
| 300 | { |
| 301 | int ret; |
| 302 | struct bt_stream_class *stream_class; |
| 303 | struct bt_resolve_field_path_context resolve_ctx = { |
| 304 | .packet_context = NULL, |
| 305 | .event_common_context = NULL, |
| 306 | .event_specific_context = field_class, |
| 307 | .event_payload = NULL, |
| 308 | }; |
| 309 | |
| 310 | BT_ASSERT_PRE_NO_ERROR(); |
| 311 | BT_ASSERT_PRE_EC_NON_NULL(event_class); |
| 312 | BT_ASSERT_PRE_FC_NON_NULL(field_class); |
| 313 | BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class); |
| 314 | BT_ASSERT_PRE_FC_IS_STRUCT("specific-context", field_class, |
| 315 | "Specific context field class"); |
| 316 | stream_class = bt_event_class_borrow_stream_class_inline( |
| 317 | event_class); |
| 318 | resolve_ctx.packet_context = stream_class->packet_context_fc; |
| 319 | resolve_ctx.event_common_context = |
| 320 | stream_class->event_common_context_fc; |
| 321 | |
| 322 | ret = bt_resolve_field_paths(field_class, &resolve_ctx, __func__); |
| 323 | if (ret) { |
| 324 | /* |
| 325 | * This is the only reason for which |
| 326 | * bt_resolve_field_paths() can fail: anything else |
| 327 | * would be because a precondition is not satisfied. |
| 328 | */ |
| 329 | ret = BT_FUNC_STATUS_MEMORY_ERROR; |
| 330 | goto end; |
| 331 | } |
| 332 | |
| 333 | bt_field_class_make_part_of_trace_class(field_class); |
| 334 | bt_object_put_ref(event_class->specific_context_fc); |
| 335 | event_class->specific_context_fc = field_class; |
| 336 | bt_object_get_ref_no_null_check(event_class->specific_context_fc); |
| 337 | bt_field_class_freeze(field_class); |
| 338 | BT_LIB_LOGD("Set event class's specific context field class: %!+E", |
| 339 | event_class); |
| 340 | |
| 341 | end: |
| 342 | return ret; |
| 343 | } |
| 344 | |
| 345 | BT_EXPORT |
| 346 | const struct bt_field_class *bt_event_class_borrow_payload_field_class_const( |
| 347 | const struct bt_event_class *event_class) |
| 348 | { |
| 349 | BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class); |
| 350 | return event_class->payload_fc; |
| 351 | } |
| 352 | |
| 353 | BT_EXPORT |
| 354 | struct bt_field_class *bt_event_class_borrow_payload_field_class( |
| 355 | struct bt_event_class *event_class) |
| 356 | { |
| 357 | BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class); |
| 358 | return event_class->payload_fc; |
| 359 | } |
| 360 | |
| 361 | BT_EXPORT |
| 362 | enum bt_event_class_set_field_class_status |
| 363 | bt_event_class_set_payload_field_class( |
| 364 | struct bt_event_class *event_class, |
| 365 | struct bt_field_class *field_class) |
| 366 | { |
| 367 | int ret; |
| 368 | struct bt_stream_class *stream_class; |
| 369 | struct bt_resolve_field_path_context resolve_ctx = { |
| 370 | .packet_context = NULL, |
| 371 | .event_common_context = NULL, |
| 372 | .event_specific_context = NULL, |
| 373 | .event_payload = field_class, |
| 374 | }; |
| 375 | |
| 376 | BT_ASSERT_PRE_NO_ERROR(); |
| 377 | BT_ASSERT_PRE_EC_NON_NULL(event_class); |
| 378 | BT_ASSERT_PRE_FC_NON_NULL(field_class); |
| 379 | BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class); |
| 380 | BT_ASSERT_PRE_FC_IS_STRUCT("payload", field_class, "Payload field class"); |
| 381 | stream_class = bt_event_class_borrow_stream_class_inline( |
| 382 | event_class); |
| 383 | resolve_ctx.packet_context = stream_class->packet_context_fc; |
| 384 | resolve_ctx.event_common_context = |
| 385 | stream_class->event_common_context_fc; |
| 386 | resolve_ctx.event_specific_context = event_class->specific_context_fc; |
| 387 | |
| 388 | ret = bt_resolve_field_paths(field_class, &resolve_ctx, __func__); |
| 389 | if (ret) { |
| 390 | /* |
| 391 | * This is the only reason for which |
| 392 | * bt_resolve_field_paths() can fail: anything else |
| 393 | * would be because a precondition is not satisfied. |
| 394 | */ |
| 395 | ret = BT_FUNC_STATUS_MEMORY_ERROR; |
| 396 | goto end; |
| 397 | } |
| 398 | |
| 399 | bt_field_class_make_part_of_trace_class(field_class); |
| 400 | bt_object_put_ref(event_class->payload_fc); |
| 401 | event_class->payload_fc = field_class; |
| 402 | bt_object_get_ref_no_null_check(event_class->payload_fc); |
| 403 | bt_field_class_freeze(field_class); |
| 404 | BT_LIB_LOGD("Set event class's payload field class: %!+E", event_class); |
| 405 | |
| 406 | end: |
| 407 | return ret; |
| 408 | } |
| 409 | |
| 410 | void _bt_event_class_freeze(const struct bt_event_class *event_class) |
| 411 | { |
| 412 | /* The field classes are already frozen */ |
| 413 | BT_ASSERT(event_class); |
| 414 | BT_LIB_LOGD("Freezing event class's user attributes: %!+v", |
| 415 | event_class->user_attributes); |
| 416 | bt_value_freeze(event_class->user_attributes); |
| 417 | BT_LIB_LOGD("Freezing event class: %!+E", event_class); |
| 418 | ((struct bt_event_class *) event_class)->frozen = true; |
| 419 | } |
| 420 | |
| 421 | BT_EXPORT |
| 422 | const struct bt_value *bt_event_class_borrow_user_attributes_const( |
| 423 | const struct bt_event_class *event_class) |
| 424 | { |
| 425 | BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class); |
| 426 | return event_class->user_attributes; |
| 427 | } |
| 428 | |
| 429 | BT_EXPORT |
| 430 | struct bt_value *bt_event_class_borrow_user_attributes( |
| 431 | struct bt_event_class *event_class) |
| 432 | { |
| 433 | return (void *) bt_event_class_borrow_user_attributes_const( |
| 434 | (void *) event_class); |
| 435 | } |
| 436 | |
| 437 | BT_EXPORT |
| 438 | void bt_event_class_set_user_attributes( |
| 439 | struct bt_event_class *event_class, |
| 440 | const struct bt_value *user_attributes) |
| 441 | { |
| 442 | BT_ASSERT_PRE_EC_NON_NULL(event_class); |
| 443 | BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class); |
| 444 | BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes); |
| 445 | BT_ASSERT_PRE_USER_ATTRS_IS_MAP(user_attributes); |
| 446 | bt_object_put_ref_no_null_check(event_class->user_attributes); |
| 447 | event_class->user_attributes = (void *) user_attributes; |
| 448 | bt_object_get_ref_no_null_check(event_class->user_attributes); |
| 449 | } |
| 450 | |
| 451 | BT_EXPORT |
| 452 | void bt_event_class_get_ref(const struct bt_event_class *event_class) |
| 453 | { |
| 454 | bt_object_get_ref(event_class); |
| 455 | } |
| 456 | |
| 457 | BT_EXPORT |
| 458 | void bt_event_class_put_ref(const struct bt_event_class *event_class) |
| 459 | { |
| 460 | bt_object_put_ref(event_class); |
| 461 | } |