| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 5 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
| 6 | */ |
| 7 | |
| 8 | #define BT_LOG_TAG "CTF-WRITER/EVENT" |
| 9 | #include "logging.h" |
| 10 | |
| 11 | #include <inttypes.h> |
| 12 | #include <stdbool.h> |
| 13 | |
| 14 | #include <babeltrace2-ctf-writer/event.h> |
| 15 | #include <babeltrace2-ctf-writer/fields.h> |
| 16 | #include <babeltrace2-ctf-writer/field-types.h> |
| 17 | #include <babeltrace2-ctf-writer/object.h> |
| 18 | #include <babeltrace2-ctf-writer/stream-class.h> |
| 19 | #include <babeltrace2-ctf-writer/trace.h> |
| 20 | #include <babeltrace2-ctf-writer/utils.h> |
| 21 | |
| 22 | #include "common/assert.h" |
| 23 | #include "compat/compiler.h" |
| 24 | |
| 25 | #include "assert-pre.h" |
| 26 | #include "attributes.h" |
| 27 | #include "clock-class.h" |
| 28 | #include "clock.h" |
| 29 | #include "event-class.h" |
| 30 | #include "event.h" |
| 31 | #include "fields.h" |
| 32 | #include "field-types.h" |
| 33 | #include "stream-class.h" |
| 34 | #include "stream.h" |
| 35 | #include "trace.h" |
| 36 | #include "validation.h" |
| 37 | |
| 38 | static |
| 39 | int bt_ctf_event_common_validate_types_for_create( |
| 40 | struct bt_ctf_event_class_common *event_class, |
| 41 | struct bt_ctf_validation_output *validation_output, |
| 42 | bt_ctf_validation_flag_copy_field_type_func copy_field_type_func) |
| 43 | { |
| 44 | int ret; |
| 45 | enum bt_ctf_validation_flag validation_flags = |
| 46 | BT_CTF_VALIDATION_FLAG_STREAM | |
| 47 | BT_CTF_VALIDATION_FLAG_EVENT; |
| 48 | struct bt_ctf_trace_common *trace = NULL; |
| 49 | struct bt_ctf_stream_class_common *stream_class = NULL; |
| 50 | struct bt_ctf_field_type_common *packet_header_type = NULL; |
| 51 | struct bt_ctf_field_type_common *packet_context_type = NULL; |
| 52 | struct bt_ctf_field_type_common *event_header_type = NULL; |
| 53 | struct bt_ctf_field_type_common *stream_event_ctx_type = NULL; |
| 54 | struct bt_ctf_field_type_common *event_context_type = NULL; |
| 55 | struct bt_ctf_field_type_common *event_payload_type = NULL; |
| 56 | int trace_valid = 0; |
| 57 | struct bt_ctf_private_value *environment = NULL; |
| 58 | |
| 59 | stream_class = bt_ctf_event_class_common_borrow_stream_class(event_class); |
| 60 | BT_ASSERT_DBG(stream_class); |
| 61 | trace = bt_ctf_stream_class_common_borrow_trace(stream_class); |
| 62 | if (trace) { |
| 63 | BT_LOGD_STR("Event class is part of a trace."); |
| 64 | packet_header_type = |
| 65 | bt_ctf_trace_common_borrow_packet_header_field_type(trace); |
| 66 | trace_valid = trace->valid; |
| 67 | BT_ASSERT_DBG(trace_valid); |
| 68 | environment = trace->environment; |
| 69 | } |
| 70 | |
| 71 | packet_context_type = |
| 72 | bt_ctf_stream_class_common_borrow_packet_context_field_type( |
| 73 | stream_class); |
| 74 | event_header_type = |
| 75 | bt_ctf_stream_class_common_borrow_event_header_field_type( |
| 76 | stream_class); |
| 77 | stream_event_ctx_type = |
| 78 | bt_ctf_stream_class_common_borrow_event_context_field_type( |
| 79 | stream_class); |
| 80 | event_context_type = |
| 81 | bt_ctf_event_class_common_borrow_context_field_type(event_class); |
| 82 | event_payload_type = |
| 83 | bt_ctf_event_class_common_borrow_payload_field_type(event_class); |
| 84 | ret = bt_ctf_validate_class_types(environment, packet_header_type, |
| 85 | packet_context_type, event_header_type, stream_event_ctx_type, |
| 86 | event_context_type, event_payload_type, trace_valid, |
| 87 | stream_class->valid, event_class->valid, |
| 88 | validation_output, validation_flags, copy_field_type_func); |
| 89 | if (ret) { |
| 90 | /* |
| 91 | * This means something went wrong during the validation |
| 92 | * process, not that the objects are invalid. |
| 93 | */ |
| 94 | BT_LOGE("Failed to validate event and parents: ret=%d", ret); |
| 95 | goto error; |
| 96 | } |
| 97 | |
| 98 | if ((validation_output->valid_flags & validation_flags) != |
| 99 | validation_flags) { |
| 100 | /* Invalid trace/stream class/event class */ |
| 101 | BT_LOGW("Invalid trace, stream class, or event class: " |
| 102 | "valid-flags=0x%x", validation_output->valid_flags); |
| 103 | goto error; |
| 104 | } |
| 105 | |
| 106 | goto end; |
| 107 | |
| 108 | error: |
| 109 | bt_ctf_validation_output_put_types(validation_output); |
| 110 | ret = -1; |
| 111 | |
| 112 | end: |
| 113 | return ret; |
| 114 | } |
| 115 | |
| 116 | static |
| 117 | int bt_ctf_event_common_create_fields( |
| 118 | struct bt_ctf_stream_class_common *stream_class, |
| 119 | struct bt_ctf_validation_output *validation_output, |
| 120 | create_field_func_type create_field_func, |
| 121 | release_field_func_type release_field_func, |
| 122 | create_header_field_func_type create_header_field_func, |
| 123 | release_header_field_func_type release_header_field_func, |
| 124 | struct bt_ctf_field_wrapper **header_field, |
| 125 | struct bt_ctf_field_common **stream_event_context_field, |
| 126 | struct bt_ctf_field_common **context_field, |
| 127 | struct bt_ctf_field_common **payload_field) |
| 128 | { |
| 129 | int ret = 0; |
| 130 | |
| 131 | if (validation_output->event_header_type) { |
| 132 | BT_LOGD("Creating initial event header field: ft-addr=%p", |
| 133 | validation_output->event_header_type); |
| 134 | *header_field = |
| 135 | create_header_field_func(stream_class, |
| 136 | validation_output->event_header_type); |
| 137 | if (!*header_field) { |
| 138 | BT_LOGE_STR("Cannot create initial event header field object."); |
| 139 | goto error; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | if (validation_output->stream_event_ctx_type) { |
| 144 | BT_LOGD("Creating initial stream event context field: ft-addr=%p", |
| 145 | validation_output->stream_event_ctx_type); |
| 146 | *stream_event_context_field = create_field_func( |
| 147 | validation_output->stream_event_ctx_type); |
| 148 | if (!*stream_event_context_field) { |
| 149 | BT_LOGE_STR("Cannot create initial stream event context field object."); |
| 150 | goto error; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | if (validation_output->event_context_type) { |
| 155 | BT_LOGD("Creating initial event context field: ft-addr=%p", |
| 156 | validation_output->event_context_type); |
| 157 | *context_field = create_field_func( |
| 158 | validation_output->event_context_type); |
| 159 | if (!*context_field) { |
| 160 | BT_LOGE_STR("Cannot create initial event context field object."); |
| 161 | goto error; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | if (validation_output->event_payload_type) { |
| 166 | BT_LOGD("Creating initial event payload field: ft-addr=%p", |
| 167 | validation_output->event_payload_type); |
| 168 | *payload_field = create_field_func( |
| 169 | validation_output->event_payload_type); |
| 170 | if (!*payload_field) { |
| 171 | BT_LOGE_STR("Cannot create initial event payload field object."); |
| 172 | goto error; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | goto end; |
| 177 | |
| 178 | error: |
| 179 | if (*header_field) { |
| 180 | release_header_field_func(*header_field, stream_class); |
| 181 | } |
| 182 | |
| 183 | if (*stream_event_context_field) { |
| 184 | release_field_func(*stream_event_context_field); |
| 185 | } |
| 186 | |
| 187 | if (*context_field) { |
| 188 | release_field_func(*context_field); |
| 189 | } |
| 190 | |
| 191 | if (*payload_field) { |
| 192 | release_field_func(*payload_field); |
| 193 | } |
| 194 | |
| 195 | ret = -1; |
| 196 | |
| 197 | end: |
| 198 | return ret; |
| 199 | } |
| 200 | |
| 201 | int _bt_ctf_event_common_validate(struct bt_ctf_event_common *event) |
| 202 | { |
| 203 | int ret = 0; |
| 204 | struct bt_ctf_stream_class_common *stream_class; |
| 205 | |
| 206 | BT_ASSERT_DBG(event); |
| 207 | if (event->header_field) { |
| 208 | ret = bt_ctf_field_common_validate_recursive( |
| 209 | event->header_field->field); |
| 210 | if (ret) { |
| 211 | BT_CTF_ASSERT_PRE_MSG("Invalid event's header field: " |
| 212 | "event-addr=%p, field-addr=%p", |
| 213 | event, event->header_field->field); |
| 214 | goto end; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | stream_class = bt_ctf_event_class_common_borrow_stream_class(event->class); |
| 219 | |
| 220 | /* |
| 221 | * We should not have been able to create the event without associating |
| 222 | * the event class to a stream class. |
| 223 | */ |
| 224 | BT_ASSERT_DBG(stream_class); |
| 225 | |
| 226 | if (stream_class->event_context_field_type) { |
| 227 | ret = bt_ctf_field_common_validate_recursive( |
| 228 | event->stream_event_context_field); |
| 229 | if (ret) { |
| 230 | BT_CTF_ASSERT_PRE_MSG("Invalid event's stream event context field: " |
| 231 | "event-addr=%p, field-addr=%p", |
| 232 | event, event->stream_event_context_field); |
| 233 | goto end; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | if (event->class->context_field_type) { |
| 238 | ret = bt_ctf_field_common_validate_recursive(event->context_field); |
| 239 | if (ret) { |
| 240 | BT_CTF_ASSERT_PRE_MSG("Invalid event's payload field: " |
| 241 | "event-addr=%p, field-addr=%p", |
| 242 | event, event->context_field); |
| 243 | goto end; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | ret = bt_ctf_field_common_validate_recursive(event->payload_field); |
| 248 | if (ret) { |
| 249 | BT_CTF_ASSERT_PRE_MSG("Invalid event's payload field: " |
| 250 | "event-addr=%p, field-addr=%p", |
| 251 | event, event->payload_field); |
| 252 | goto end; |
| 253 | } |
| 254 | |
| 255 | end: |
| 256 | return ret; |
| 257 | } |
| 258 | |
| 259 | void _bt_ctf_event_common_set_is_frozen(struct bt_ctf_event_common *event, |
| 260 | bool is_frozen) |
| 261 | { |
| 262 | BT_ASSERT_DBG(event); |
| 263 | BT_LOGD("Freezing event: addr=%p, " |
| 264 | "event-class-name=\"%s\", event-class-id=%" PRId64, |
| 265 | event, bt_ctf_event_class_common_get_name(event->class), |
| 266 | bt_ctf_event_class_common_get_id(event->class)); |
| 267 | |
| 268 | if (event->header_field) { |
| 269 | BT_LOGD_STR("Freezing event's header field."); |
| 270 | bt_ctf_field_common_set_is_frozen_recursive( |
| 271 | event->header_field->field, is_frozen); |
| 272 | } |
| 273 | |
| 274 | if (event->stream_event_context_field) { |
| 275 | BT_LOGD_STR("Freezing event's stream event context field."); |
| 276 | bt_ctf_field_common_set_is_frozen_recursive( |
| 277 | event->stream_event_context_field, is_frozen); |
| 278 | } |
| 279 | |
| 280 | if (event->context_field) { |
| 281 | BT_LOGD_STR("Freezing event's context field."); |
| 282 | bt_ctf_field_common_set_is_frozen_recursive(event->context_field, |
| 283 | is_frozen); |
| 284 | } |
| 285 | |
| 286 | if (event->payload_field) { |
| 287 | BT_LOGD_STR("Freezing event's payload field."); |
| 288 | bt_ctf_field_common_set_is_frozen_recursive(event->payload_field, |
| 289 | is_frozen); |
| 290 | } |
| 291 | |
| 292 | event->frozen = is_frozen; |
| 293 | } |
| 294 | |
| 295 | int bt_ctf_event_common_initialize(struct bt_ctf_event_common *event, |
| 296 | struct bt_ctf_event_class_common *event_class, |
| 297 | struct bt_ctf_clock_class *init_expected_clock_class, |
| 298 | bool is_shared_with_parent, bt_ctf_object_release_func release_func, |
| 299 | bt_ctf_validation_flag_copy_field_type_func field_type_copy_func, |
| 300 | bool must_be_in_trace, |
| 301 | int (*map_clock_classes_func)(struct bt_ctf_stream_class_common *stream_class, |
| 302 | struct bt_ctf_field_type_common *packet_context_field_type, |
| 303 | struct bt_ctf_field_type_common *event_header_field_type), |
| 304 | create_field_func_type create_field_func, |
| 305 | release_field_func_type release_field_func, |
| 306 | create_header_field_func_type create_header_field_func, |
| 307 | release_header_field_func_type release_header_field_func) |
| 308 | { |
| 309 | int ret; |
| 310 | struct bt_ctf_trace_common *trace = NULL; |
| 311 | struct bt_ctf_stream_class_common *stream_class = NULL; |
| 312 | struct bt_ctf_field_wrapper *event_header = NULL; |
| 313 | struct bt_ctf_field_common *stream_event_context = NULL; |
| 314 | struct bt_ctf_field_common *event_context = NULL; |
| 315 | struct bt_ctf_field_common *event_payload = NULL; |
| 316 | struct bt_ctf_validation_output validation_output = { 0 }; |
| 317 | struct bt_ctf_clock_class *expected_clock_class = |
| 318 | init_expected_clock_class ? bt_ctf_object_get_ref(init_expected_clock_class) : |
| 319 | NULL; |
| 320 | |
| 321 | BT_CTF_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
| 322 | BT_LOGD("Initializing common event object: event-class-addr=%p, " |
| 323 | "event-class-name=\"%s\", event-class-id=%" PRId64, |
| 324 | event_class, bt_ctf_event_class_common_get_name(event_class), |
| 325 | bt_ctf_event_class_common_get_id(event_class)); |
| 326 | |
| 327 | stream_class = bt_ctf_event_class_common_borrow_stream_class(event_class); |
| 328 | BT_CTF_ASSERT_PRE(stream_class, |
| 329 | "Event class is not part of a stream class: event-class-addr=%p", |
| 330 | event_class); |
| 331 | |
| 332 | /* The event class was frozen when added to its stream class */ |
| 333 | BT_ASSERT_DBG(event_class->frozen); |
| 334 | trace = bt_ctf_stream_class_common_borrow_trace(stream_class); |
| 335 | |
| 336 | if (must_be_in_trace) { |
| 337 | BT_CTF_ASSERT_PRE(trace, |
| 338 | "Event class's stream class is not part of a trace: " |
| 339 | "ec-addr=%p, sc-addr=%p", event_class, stream_class); |
| 340 | } |
| 341 | |
| 342 | /* |
| 343 | * This must be called before anything that can fail because on |
| 344 | * failure, the caller releases the reference to `event` to |
| 345 | * destroy it. |
| 346 | */ |
| 347 | if (is_shared_with_parent) { |
| 348 | bt_ctf_object_init_shared_with_parent(&event->base, release_func); |
| 349 | } else { |
| 350 | bt_ctf_object_init_unique(&event->base); |
| 351 | } |
| 352 | |
| 353 | if (!stream_class->frozen) { |
| 354 | /* |
| 355 | * Because this function freezes the stream class, |
| 356 | * validate that this stream class contains at most a |
| 357 | * single clock class so that we set its expected clock |
| 358 | * class for future checks. |
| 359 | */ |
| 360 | ret = bt_ctf_stream_class_common_validate_single_clock_class( |
| 361 | stream_class, &expected_clock_class); |
| 362 | if (ret) { |
| 363 | BT_LOGW("Event class's stream class or one of its event " |
| 364 | "classes contains a field type which is not " |
| 365 | "recursively mapped to the expected " |
| 366 | "clock class: " |
| 367 | "stream-class-addr=%p, " |
| 368 | "stream-class-id=%" PRId64 ", " |
| 369 | "stream-class-name=\"%s\", " |
| 370 | "expected-clock-class-addr=%p, " |
| 371 | "expected-clock-class-name=\"%s\"", |
| 372 | stream_class, |
| 373 | bt_ctf_stream_class_common_get_id(stream_class), |
| 374 | bt_ctf_stream_class_common_get_name(stream_class), |
| 375 | expected_clock_class, |
| 376 | expected_clock_class ? |
| 377 | bt_ctf_clock_class_get_name(expected_clock_class) : |
| 378 | NULL); |
| 379 | goto error; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | /* Validate the trace, the stream class, and the event class */ |
| 384 | ret = bt_ctf_event_common_validate_types_for_create( |
| 385 | event_class, &validation_output, field_type_copy_func); |
| 386 | if (ret) { |
| 387 | /* bt_ctf_event_common_validate_types_for_create() logs errors */ |
| 388 | goto error; |
| 389 | } |
| 390 | |
| 391 | if (map_clock_classes_func) { |
| 392 | /* |
| 393 | * Safe to automatically map selected fields to the |
| 394 | * stream's clock's class here because the stream class |
| 395 | * is about to be frozen. |
| 396 | */ |
| 397 | if (map_clock_classes_func(stream_class, |
| 398 | validation_output.packet_context_type, |
| 399 | validation_output.event_header_type)) { |
| 400 | BT_LOGW_STR("Cannot automatically map selected stream class's " |
| 401 | "field types to stream class's clock's class."); |
| 402 | goto error; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | /* |
| 407 | * event does not share a common ancestor with the event class; it has |
| 408 | * to guarantee its existence by holding a reference. This reference |
| 409 | * shall be released once the event is associated to a stream since, |
| 410 | * from that point, the event and its class will share the same |
| 411 | * lifetime. |
| 412 | */ |
| 413 | event->class = bt_ctf_object_get_ref(event_class); |
| 414 | |
| 415 | ret = bt_ctf_event_common_create_fields(stream_class, |
| 416 | &validation_output, |
| 417 | create_field_func, release_field_func, |
| 418 | create_header_field_func, release_header_field_func, |
| 419 | &event_header, &stream_event_context, &event_context, |
| 420 | &event_payload); |
| 421 | if (ret) { |
| 422 | /* bt_ctf_event_common_create_fields() logs errors */ |
| 423 | goto error; |
| 424 | } |
| 425 | |
| 426 | /* |
| 427 | * At this point all the fields are created, potentially from |
| 428 | * validated copies of field types, so that the field types and |
| 429 | * fields can be replaced in the trace, stream class, |
| 430 | * event class, and created event. |
| 431 | */ |
| 432 | bt_ctf_validation_replace_types(trace, stream_class, event_class, |
| 433 | &validation_output, |
| 434 | BT_CTF_VALIDATION_FLAG_STREAM | BT_CTF_VALIDATION_FLAG_EVENT); |
| 435 | event->header_field = event_header; |
| 436 | event_header = NULL; |
| 437 | event->stream_event_context_field = stream_event_context; |
| 438 | stream_event_context = NULL; |
| 439 | event->context_field = event_context; |
| 440 | event_context = NULL; |
| 441 | event->payload_field = event_payload; |
| 442 | event_payload = NULL; |
| 443 | |
| 444 | /* |
| 445 | * Put what was not moved in bt_ctf_validation_replace_types(). |
| 446 | */ |
| 447 | bt_ctf_validation_output_put_types(&validation_output); |
| 448 | |
| 449 | /* |
| 450 | * Freeze the stream class since the event header must not be changed |
| 451 | * anymore. |
| 452 | */ |
| 453 | bt_ctf_stream_class_common_freeze(stream_class); |
| 454 | |
| 455 | /* |
| 456 | * It is safe to set the stream class's unique clock class |
| 457 | * now because the stream class is frozen. |
| 458 | */ |
| 459 | if (expected_clock_class) { |
| 460 | BT_CTF_OBJECT_MOVE_REF(stream_class->clock_class, expected_clock_class); |
| 461 | } |
| 462 | |
| 463 | /* |
| 464 | * Mark stream class, and event class as valid since |
| 465 | * they're all frozen now. |
| 466 | */ |
| 467 | stream_class->valid = 1; |
| 468 | event_class->valid = 1; |
| 469 | |
| 470 | /* Put stuff we borrowed from the event class */ |
| 471 | BT_LOGD("Initialized event object: addr=%p, event-class-name=\"%s\", " |
| 472 | "event-class-id=%" PRId64, |
| 473 | event, bt_ctf_event_class_common_get_name(event->class), |
| 474 | bt_ctf_event_class_common_get_id(event->class)); |
| 475 | goto end; |
| 476 | |
| 477 | error: |
| 478 | bt_ctf_validation_output_put_types(&validation_output); |
| 479 | bt_ctf_object_put_ref(expected_clock_class); |
| 480 | |
| 481 | if (event_header) { |
| 482 | release_header_field_func(event_header, stream_class); |
| 483 | } |
| 484 | |
| 485 | if (stream_event_context) { |
| 486 | release_field_func(stream_event_context); |
| 487 | } |
| 488 | |
| 489 | if (event_context) { |
| 490 | release_field_func(event_context); |
| 491 | } |
| 492 | |
| 493 | if (event_payload) { |
| 494 | release_field_func(event_payload); |
| 495 | } |
| 496 | |
| 497 | ret = -1; |
| 498 | |
| 499 | end: |
| 500 | return ret; |
| 501 | } |
| 502 | |
| 503 | static |
| 504 | int map_clock_classes_func(struct bt_ctf_stream_class_common *stream_class, |
| 505 | struct bt_ctf_field_type_common *packet_context_type, |
| 506 | struct bt_ctf_field_type_common *event_header_type) |
| 507 | { |
| 508 | int ret = bt_ctf_stream_class_map_clock_class( |
| 509 | BT_CTF_FROM_COMMON(stream_class), |
| 510 | BT_CTF_FROM_COMMON(packet_context_type), |
| 511 | BT_CTF_FROM_COMMON(event_header_type)); |
| 512 | |
| 513 | if (ret) { |
| 514 | BT_LOGW_STR("Cannot automatically map selected stream class's field types to stream class's clock's class."); |
| 515 | } |
| 516 | |
| 517 | return ret; |
| 518 | } |
| 519 | |
| 520 | static |
| 521 | void destroy_event_header_field(struct bt_ctf_field_wrapper *field_wrapper, |
| 522 | struct bt_ctf_stream_class *stream_class) |
| 523 | { |
| 524 | BT_ASSERT_DBG(field_wrapper); |
| 525 | bt_ctf_object_put_ref(field_wrapper->field); |
| 526 | bt_ctf_field_wrapper_destroy(field_wrapper); |
| 527 | } |
| 528 | |
| 529 | static |
| 530 | struct bt_ctf_field_wrapper *create_event_header_field( |
| 531 | struct bt_ctf_stream_class *stream_class, |
| 532 | struct bt_ctf_field_type_common *ft) |
| 533 | { |
| 534 | struct bt_ctf_field_wrapper *field_wrapper = NULL; |
| 535 | struct bt_ctf_field *field = bt_ctf_field_create((void *) ft); |
| 536 | |
| 537 | if (!field) { |
| 538 | goto error; |
| 539 | } |
| 540 | |
| 541 | field_wrapper = bt_ctf_field_wrapper_new(NULL); |
| 542 | if (!field_wrapper) { |
| 543 | goto error; |
| 544 | } |
| 545 | |
| 546 | field_wrapper->field = (void *) field; |
| 547 | field = NULL; |
| 548 | goto end; |
| 549 | |
| 550 | error: |
| 551 | bt_ctf_object_put_ref(field); |
| 552 | |
| 553 | if (field_wrapper) { |
| 554 | destroy_event_header_field(field_wrapper, stream_class); |
| 555 | field_wrapper = NULL; |
| 556 | } |
| 557 | |
| 558 | end: |
| 559 | return field_wrapper; |
| 560 | } |
| 561 | |
| 562 | static |
| 563 | void release_event_header_field(struct bt_ctf_field_wrapper *field_wrapper, |
| 564 | struct bt_ctf_event_common *event_common) |
| 565 | { |
| 566 | BT_ASSERT_DBG(field_wrapper); |
| 567 | BT_CTF_OBJECT_PUT_REF_AND_RESET(field_wrapper->field); |
| 568 | bt_ctf_field_wrapper_destroy(field_wrapper); |
| 569 | } |
| 570 | |
| 571 | static |
| 572 | void bt_ctf_event_destroy(struct bt_ctf_object *obj) |
| 573 | { |
| 574 | bt_ctf_event_common_finalize(obj, (void *) bt_ctf_object_put_ref, |
| 575 | (void *) release_event_header_field); |
| 576 | g_free(obj); |
| 577 | } |
| 578 | |
| 579 | BT_EXPORT |
| 580 | struct bt_ctf_event *bt_ctf_event_create(struct bt_ctf_event_class *event_class) |
| 581 | { |
| 582 | int ret; |
| 583 | struct bt_ctf_event *event = NULL; |
| 584 | struct bt_ctf_clock_class *expected_clock_class = NULL; |
| 585 | |
| 586 | event = g_new0(struct bt_ctf_event, 1); |
| 587 | if (!event) { |
| 588 | BT_LOGE_STR("Failed to allocate one CTF writer event."); |
| 589 | goto error; |
| 590 | } |
| 591 | |
| 592 | if (event_class) { |
| 593 | struct bt_ctf_stream_class *stream_class = |
| 594 | BT_CTF_FROM_COMMON(bt_ctf_event_class_common_borrow_stream_class( |
| 595 | BT_CTF_TO_COMMON(event_class))); |
| 596 | |
| 597 | if (stream_class && stream_class->clock) { |
| 598 | expected_clock_class = stream_class->clock->clock_class; |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | ret = bt_ctf_event_common_initialize(BT_CTF_TO_COMMON(event), |
| 603 | BT_CTF_TO_COMMON(event_class), expected_clock_class, |
| 604 | true, bt_ctf_event_destroy, |
| 605 | (bt_ctf_validation_flag_copy_field_type_func) |
| 606 | bt_ctf_field_type_copy, |
| 607 | false, map_clock_classes_func, |
| 608 | (create_field_func_type) bt_ctf_field_create, |
| 609 | (release_field_func_type) bt_ctf_object_put_ref, |
| 610 | (create_header_field_func_type) create_event_header_field, |
| 611 | (release_header_field_func_type) destroy_event_header_field); |
| 612 | if (ret) { |
| 613 | /* bt_ctf_event_common_initialize() logs errors */ |
| 614 | goto error; |
| 615 | } |
| 616 | |
| 617 | goto end; |
| 618 | |
| 619 | error: |
| 620 | BT_CTF_OBJECT_PUT_REF_AND_RESET(event); |
| 621 | |
| 622 | end: |
| 623 | return event; |
| 624 | } |
| 625 | |
| 626 | BT_EXPORT |
| 627 | struct bt_ctf_event_class *bt_ctf_event_get_class(struct bt_ctf_event *event) |
| 628 | { |
| 629 | BT_CTF_ASSERT_PRE_NON_NULL(event, "Event"); |
| 630 | return bt_ctf_object_get_ref(bt_ctf_event_common_borrow_class(BT_CTF_TO_COMMON(event))); |
| 631 | } |
| 632 | |
| 633 | static |
| 634 | struct bt_ctf_stream *bt_ctf_event_borrow_stream(struct bt_ctf_event *event) |
| 635 | { |
| 636 | BT_ASSERT_DBG(event); |
| 637 | return (struct bt_ctf_stream *) |
| 638 | bt_ctf_object_borrow_parent(&BT_CTF_TO_COMMON(event)->base); |
| 639 | } |
| 640 | |
| 641 | BT_EXPORT |
| 642 | struct bt_ctf_stream *bt_ctf_event_get_stream(struct bt_ctf_event *event) |
| 643 | { |
| 644 | BT_CTF_ASSERT_PRE_NON_NULL(event, "Event"); |
| 645 | return bt_ctf_object_get_ref(bt_ctf_event_borrow_stream(event)); |
| 646 | } |
| 647 | |
| 648 | BT_EXPORT |
| 649 | int bt_ctf_event_set_payload(struct bt_ctf_event *event, const char *name, |
| 650 | struct bt_ctf_field *field) |
| 651 | { |
| 652 | BT_CTF_ASSERT_PRE_NON_NULL(event, "Event"); |
| 653 | BT_CTF_ASSERT_PRE_NON_NULL(field, "Payload field"); |
| 654 | BT_CTF_ASSERT_PRE_EVENT_COMMON_HOT(BT_CTF_TO_COMMON(event), "Event"); |
| 655 | return bt_ctf_field_structure_set_field_by_name( |
| 656 | (void *) event->common.payload_field, name, field); |
| 657 | } |
| 658 | |
| 659 | BT_EXPORT |
| 660 | struct bt_ctf_field *bt_ctf_event_get_payload(struct bt_ctf_event *event, |
| 661 | const char *name) |
| 662 | { |
| 663 | struct bt_ctf_field *field = NULL; |
| 664 | |
| 665 | BT_CTF_ASSERT_PRE_NON_NULL(event, "Event"); |
| 666 | |
| 667 | if (name) { |
| 668 | field = bt_ctf_field_structure_get_field_by_name( |
| 669 | BT_CTF_FROM_COMMON(event->common.payload_field), name); |
| 670 | } else { |
| 671 | field = BT_CTF_FROM_COMMON(event->common.payload_field); |
| 672 | bt_ctf_object_get_ref(field); |
| 673 | } |
| 674 | |
| 675 | return field; |
| 676 | } |
| 677 | |
| 678 | BT_EXPORT |
| 679 | struct bt_ctf_field *bt_ctf_event_get_payload_field( |
| 680 | struct bt_ctf_event *event) |
| 681 | { |
| 682 | return bt_ctf_object_get_ref(bt_ctf_event_common_borrow_payload(BT_CTF_TO_COMMON(event))); |
| 683 | } |
| 684 | |
| 685 | BT_EXPORT |
| 686 | struct bt_ctf_field *bt_ctf_event_get_header(struct bt_ctf_event *event) |
| 687 | { |
| 688 | return bt_ctf_object_get_ref(bt_ctf_event_common_borrow_header(BT_CTF_TO_COMMON(event))); |
| 689 | } |
| 690 | |
| 691 | BT_EXPORT |
| 692 | struct bt_ctf_field *bt_ctf_event_get_context(struct bt_ctf_event *event) |
| 693 | { |
| 694 | return bt_ctf_object_get_ref(bt_ctf_event_common_borrow_context(BT_CTF_TO_COMMON(event))); |
| 695 | } |
| 696 | |
| 697 | BT_EXPORT |
| 698 | struct bt_ctf_field *bt_ctf_event_get_stream_event_context( |
| 699 | struct bt_ctf_event *event) |
| 700 | { |
| 701 | return bt_ctf_object_get_ref(bt_ctf_event_common_borrow_stream_event_context( |
| 702 | BT_CTF_TO_COMMON(event))); |
| 703 | } |
| 704 | |
| 705 | int bt_ctf_event_serialize(struct bt_ctf_event *event, |
| 706 | struct bt_ctfser *ctfser, |
| 707 | enum bt_ctf_byte_order native_byte_order) |
| 708 | { |
| 709 | int ret = 0; |
| 710 | |
| 711 | BT_ASSERT_DBG(event); |
| 712 | BT_ASSERT_DBG(ctfser); |
| 713 | |
| 714 | BT_LOGT_STR("Serializing event's context field."); |
| 715 | if (event->common.context_field) { |
| 716 | ret = bt_ctf_field_serialize_recursive( |
| 717 | (void *) event->common.context_field, ctfser, |
| 718 | native_byte_order); |
| 719 | if (ret) { |
| 720 | BT_LOGW("Cannot serialize event's context field: " |
| 721 | "event-addr=%p, event-class-name=\"%s\", " |
| 722 | "event-class-id=%" PRId64, |
| 723 | event, |
| 724 | bt_ctf_event_class_common_get_name(event->common.class), |
| 725 | bt_ctf_event_class_common_get_id(event->common.class)); |
| 726 | goto end; |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | BT_LOGT_STR("Serializing event's payload field."); |
| 731 | if (event->common.payload_field) { |
| 732 | ret = bt_ctf_field_serialize_recursive( |
| 733 | (void *) event->common.payload_field, ctfser, |
| 734 | native_byte_order); |
| 735 | if (ret) { |
| 736 | BT_LOGW("Cannot serialize event's payload field: " |
| 737 | "event-addr=%p, event-class-name=\"%s\", " |
| 738 | "event-class-id=%" PRId64, |
| 739 | event, |
| 740 | bt_ctf_event_class_common_get_name(event->common.class), |
| 741 | bt_ctf_event_class_common_get_id(event->common.class)); |
| 742 | goto end; |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | end: |
| 747 | return ret; |
| 748 | } |
| 749 | |
| 750 | BT_EXPORT |
| 751 | int bt_ctf_event_set_header(struct bt_ctf_event *event, |
| 752 | struct bt_ctf_field *header) |
| 753 | { |
| 754 | BT_CTF_ASSERT_PRE_NON_NULL(event, "Event"); |
| 755 | BT_CTF_ASSERT_PRE_EVENT_COMMON_HOT(BT_CTF_TO_COMMON(event), "Event"); |
| 756 | |
| 757 | /* |
| 758 | * Ensure the provided header's type matches the one registered to the |
| 759 | * stream class. |
| 760 | */ |
| 761 | if (header) { |
| 762 | BT_CTF_ASSERT_PRE(bt_ctf_field_type_common_compare( |
| 763 | ((struct bt_ctf_field_common *) header)->type, |
| 764 | bt_ctf_event_class_common_borrow_stream_class(event->common.class)->event_header_field_type) == 0, |
| 765 | "Header field's type is different from the " |
| 766 | "expected field type: event-addr=%p, ft-addr=%p, " |
| 767 | "expected-ft-addr=%p", |
| 768 | event, ((struct bt_ctf_field_common *) header)->type, |
| 769 | bt_ctf_event_class_common_borrow_stream_class(event->common.class)->event_header_field_type); |
| 770 | } else { |
| 771 | BT_CTF_ASSERT_PRE(!bt_ctf_event_class_common_borrow_stream_class(event->common.class)->event_header_field_type, |
| 772 | "Setting no event header field, " |
| 773 | "but event header field type is not NULL: " |
| 774 | "event-addr=%p, header-ft-addr=%p", |
| 775 | event, |
| 776 | bt_ctf_event_class_common_borrow_stream_class(event->common.class)->event_header_field_type); |
| 777 | } |
| 778 | |
| 779 | bt_ctf_object_put_ref(event->common.header_field->field); |
| 780 | event->common.header_field->field = bt_ctf_object_get_ref(header); |
| 781 | BT_LOGT("Set event's header field: event-addr=%p, " |
| 782 | "event-class-name=\"%s\", event-class-id=%" PRId64 ", " |
| 783 | "header-field-addr=%p", |
| 784 | event, bt_ctf_event_class_common_get_name(event->common.class), |
| 785 | bt_ctf_event_class_common_get_id(event->common.class), header); |
| 786 | return 0; |
| 787 | } |
| 788 | |
| 789 | int bt_ctf_event_common_set_payload(struct bt_ctf_event *event, |
| 790 | struct bt_ctf_field *payload) |
| 791 | { |
| 792 | BT_CTF_ASSERT_PRE_NON_NULL(event, "Event"); |
| 793 | BT_CTF_ASSERT_PRE_EVENT_COMMON_HOT(BT_CTF_TO_COMMON(event), "Event"); |
| 794 | |
| 795 | if (payload) { |
| 796 | BT_CTF_ASSERT_PRE(bt_ctf_field_type_common_compare( |
| 797 | ((struct bt_ctf_field_common *) payload)->type, |
| 798 | event->common.class->payload_field_type) == 0, |
| 799 | "Payload field's type is different from the " |
| 800 | "expected field type: event-addr=%p, ft-addr=%p, " |
| 801 | "expected-ft-addr=%p", |
| 802 | event, |
| 803 | ((struct bt_ctf_field_common *) payload)->type, |
| 804 | event->common.class->payload_field_type); |
| 805 | } else { |
| 806 | BT_CTF_ASSERT_PRE(!event->common.class->payload_field_type, |
| 807 | "Setting no event payload field, " |
| 808 | "but event payload field type is not NULL: " |
| 809 | "event-addr=%p, payload-ft-addr=%p", |
| 810 | event, event->common.class->payload_field_type); |
| 811 | } |
| 812 | |
| 813 | bt_ctf_object_put_ref(event->common.payload_field); |
| 814 | event->common.payload_field = bt_ctf_object_get_ref(payload); |
| 815 | BT_LOGT("Set event's payload field: event-addr=%p, " |
| 816 | "event-class-name=\"%s\", event-class-id=%" PRId64 ", " |
| 817 | "payload-field-addr=%p", |
| 818 | event, bt_ctf_event_class_common_get_name(event->common.class), |
| 819 | bt_ctf_event_class_common_get_id(event->common.class), payload); |
| 820 | return 0; |
| 821 | } |
| 822 | |
| 823 | BT_EXPORT |
| 824 | int bt_ctf_event_set_context(struct bt_ctf_event *event, |
| 825 | struct bt_ctf_field *context) |
| 826 | { |
| 827 | BT_CTF_ASSERT_PRE_NON_NULL(event, "Event"); |
| 828 | BT_CTF_ASSERT_PRE_EVENT_COMMON_HOT(BT_CTF_TO_COMMON(event), "Event"); |
| 829 | |
| 830 | if (context) { |
| 831 | BT_CTF_ASSERT_PRE(bt_ctf_field_type_common_compare( |
| 832 | ((struct bt_ctf_field_common *) context)->type, |
| 833 | event->common.class->context_field_type) == 0, |
| 834 | "Context field's type is different from the " |
| 835 | "expected field type: event-addr=%p, ft-addr=%p, " |
| 836 | "expected-ft-addr=%p", |
| 837 | event, ((struct bt_ctf_field_common *) context)->type, |
| 838 | event->common.class->context_field_type); |
| 839 | } else { |
| 840 | BT_CTF_ASSERT_PRE(!event->common.class->context_field_type, |
| 841 | "Setting no event context field, " |
| 842 | "but event context field type is not NULL: " |
| 843 | "event-addr=%p, context-ft-addr=%p", |
| 844 | event, event->common.class->context_field_type); |
| 845 | } |
| 846 | |
| 847 | bt_ctf_object_put_ref(event->common.context_field); |
| 848 | event->common.context_field = bt_ctf_object_get_ref(context); |
| 849 | BT_LOGT("Set event's context field: event-addr=%p, " |
| 850 | "event-class-name=\"%s\", event-class-id=%" PRId64 ", " |
| 851 | "context-field-addr=%p", |
| 852 | event, bt_ctf_event_class_common_get_name(event->common.class), |
| 853 | bt_ctf_event_class_common_get_id(event->common.class), context); |
| 854 | return 0; |
| 855 | } |
| 856 | |
| 857 | BT_EXPORT |
| 858 | int bt_ctf_event_set_stream_event_context(struct bt_ctf_event *event, |
| 859 | struct bt_ctf_field *stream_event_context) |
| 860 | { |
| 861 | BT_CTF_ASSERT_PRE_NON_NULL(event, "Event"); |
| 862 | BT_CTF_ASSERT_PRE_EVENT_COMMON_HOT(BT_CTF_TO_COMMON(event), "Event"); |
| 863 | |
| 864 | if (stream_event_context) { |
| 865 | BT_CTF_ASSERT_PRE(bt_ctf_field_type_common_compare( |
| 866 | ((struct bt_ctf_field_common *) stream_event_context)->type, |
| 867 | bt_ctf_event_class_common_borrow_stream_class(event->common.class)->event_context_field_type) == 0, |
| 868 | "Stream event context field's type is different from the " |
| 869 | "expected field type: event-addr=%p, ft-addr=%p, " |
| 870 | "expected-ft-addr=%p", |
| 871 | event, |
| 872 | ((struct bt_ctf_field_common *) stream_event_context)->type, |
| 873 | bt_ctf_event_class_common_borrow_stream_class(event->common.class)->event_context_field_type); |
| 874 | } else { |
| 875 | BT_CTF_ASSERT_PRE(!bt_ctf_event_class_common_borrow_stream_class(event->common.class)->event_context_field_type, |
| 876 | "Setting no stream event context field, " |
| 877 | "but stream event context field type is not NULL: " |
| 878 | "event-addr=%p, context-ft-addr=%p", |
| 879 | event, |
| 880 | bt_ctf_event_class_common_borrow_stream_class(event->common.class)->event_context_field_type); |
| 881 | } |
| 882 | |
| 883 | bt_ctf_object_put_ref(event->common.stream_event_context_field); |
| 884 | event->common.stream_event_context_field = bt_ctf_object_get_ref(stream_event_context); |
| 885 | BT_LOGT("Set event's stream event context field: event-addr=%p, " |
| 886 | "event-class-name=\"%s\", event-class-id=%" PRId64 ", " |
| 887 | "stream-event-context-field-addr=%p", |
| 888 | event, bt_ctf_event_class_common_get_name(event->common.class), |
| 889 | bt_ctf_event_class_common_get_id(event->common.class), |
| 890 | stream_event_context); |
| 891 | return 0; |
| 892 | } |