| 1 | /* |
| 2 | * event.c |
| 3 | * |
| 4 | * Babeltrace CTF IR - Event |
| 5 | * |
| 6 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 7 | * |
| 8 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 9 | * |
| 10 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | * of this software and associated documentation files (the "Software"), to deal |
| 12 | * in the Software without restriction, including without limitation the rights |
| 13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | * copies of the Software, and to permit persons to whom the Software is |
| 15 | * furnished to do so, subject to the following conditions: |
| 16 | * |
| 17 | * The above copyright notice and this permission notice shall be included in |
| 18 | * all copies or substantial portions of the Software. |
| 19 | * |
| 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 26 | * SOFTWARE. |
| 27 | */ |
| 28 | |
| 29 | #define BT_LOG_TAG "EVENT" |
| 30 | #include <babeltrace/lib-logging-internal.h> |
| 31 | |
| 32 | #include <babeltrace/assert-pre-internal.h> |
| 33 | #include <babeltrace/ctf-ir/fields-internal.h> |
| 34 | #include <babeltrace/ctf-ir/field-types-internal.h> |
| 35 | #include <babeltrace/ctf-ir/clock-class.h> |
| 36 | #include <babeltrace/ctf-ir/clock-value.h> |
| 37 | #include <babeltrace/ctf-ir/clock-value-internal.h> |
| 38 | #include <babeltrace/ctf-ir/clock-class-internal.h> |
| 39 | #include <babeltrace/ctf-ir/event-internal.h> |
| 40 | #include <babeltrace/ctf-ir/event-class.h> |
| 41 | #include <babeltrace/ctf-ir/event-class-internal.h> |
| 42 | #include <babeltrace/ctf-ir/stream-class.h> |
| 43 | #include <babeltrace/ctf-ir/stream-class-internal.h> |
| 44 | #include <babeltrace/ctf-ir/stream-internal.h> |
| 45 | #include <babeltrace/ctf-ir/packet.h> |
| 46 | #include <babeltrace/ctf-ir/packet-internal.h> |
| 47 | #include <babeltrace/ctf-ir/trace.h> |
| 48 | #include <babeltrace/ctf-ir/trace-internal.h> |
| 49 | #include <babeltrace/ctf-ir/validation-internal.h> |
| 50 | #include <babeltrace/ctf-ir/packet-internal.h> |
| 51 | #include <babeltrace/ctf-ir/utils.h> |
| 52 | #include <babeltrace/ref.h> |
| 53 | #include <babeltrace/ctf-ir/attributes-internal.h> |
| 54 | #include <babeltrace/compiler-internal.h> |
| 55 | #include <babeltrace/assert-internal.h> |
| 56 | #include <inttypes.h> |
| 57 | |
| 58 | static |
| 59 | int bt_event_common_validate_types_for_create( |
| 60 | struct bt_event_class_common *event_class, |
| 61 | struct bt_validation_output *validation_output, |
| 62 | bt_validation_flag_copy_field_type_func copy_field_type_func) |
| 63 | { |
| 64 | int ret; |
| 65 | enum bt_validation_flag validation_flags = |
| 66 | BT_VALIDATION_FLAG_STREAM | |
| 67 | BT_VALIDATION_FLAG_EVENT; |
| 68 | struct bt_trace_common *trace = NULL; |
| 69 | struct bt_stream_class_common *stream_class = NULL; |
| 70 | struct bt_field_type_common *packet_header_type = NULL; |
| 71 | struct bt_field_type_common *packet_context_type = NULL; |
| 72 | struct bt_field_type_common *event_header_type = NULL; |
| 73 | struct bt_field_type_common *stream_event_ctx_type = NULL; |
| 74 | struct bt_field_type_common *event_context_type = NULL; |
| 75 | struct bt_field_type_common *event_payload_type = NULL; |
| 76 | int trace_valid = 0; |
| 77 | struct bt_value *environment = NULL; |
| 78 | |
| 79 | stream_class = bt_event_class_common_borrow_stream_class(event_class); |
| 80 | BT_ASSERT(stream_class); |
| 81 | trace = bt_stream_class_common_borrow_trace(stream_class); |
| 82 | if (trace) { |
| 83 | BT_LOGD_STR("Event class is part of a trace."); |
| 84 | packet_header_type = |
| 85 | bt_trace_common_borrow_packet_header_field_type(trace); |
| 86 | trace_valid = trace->valid; |
| 87 | BT_ASSERT(trace_valid); |
| 88 | environment = trace->environment; |
| 89 | } |
| 90 | |
| 91 | packet_context_type = |
| 92 | bt_stream_class_common_borrow_packet_context_field_type( |
| 93 | stream_class); |
| 94 | event_header_type = |
| 95 | bt_stream_class_common_borrow_event_header_field_type( |
| 96 | stream_class); |
| 97 | stream_event_ctx_type = |
| 98 | bt_stream_class_common_borrow_event_context_field_type( |
| 99 | stream_class); |
| 100 | event_context_type = |
| 101 | bt_event_class_common_borrow_context_field_type(event_class); |
| 102 | event_payload_type = |
| 103 | bt_event_class_common_borrow_payload_field_type(event_class); |
| 104 | ret = bt_validate_class_types(environment, packet_header_type, |
| 105 | packet_context_type, event_header_type, stream_event_ctx_type, |
| 106 | event_context_type, event_payload_type, trace_valid, |
| 107 | stream_class->valid, event_class->valid, |
| 108 | validation_output, validation_flags, copy_field_type_func); |
| 109 | if (ret) { |
| 110 | /* |
| 111 | * This means something went wrong during the validation |
| 112 | * process, not that the objects are invalid. |
| 113 | */ |
| 114 | BT_LOGE("Failed to validate event and parents: ret=%d", ret); |
| 115 | goto error; |
| 116 | } |
| 117 | |
| 118 | if ((validation_output->valid_flags & validation_flags) != |
| 119 | validation_flags) { |
| 120 | /* Invalid trace/stream class/event class */ |
| 121 | BT_LOGW("Invalid trace, stream class, or event class: " |
| 122 | "valid-flags=0x%x", validation_output->valid_flags); |
| 123 | goto error; |
| 124 | } |
| 125 | |
| 126 | goto end; |
| 127 | |
| 128 | error: |
| 129 | bt_validation_output_put_types(validation_output); |
| 130 | ret = -1; |
| 131 | |
| 132 | end: |
| 133 | return ret; |
| 134 | } |
| 135 | |
| 136 | static |
| 137 | int bt_event_common_create_fields( |
| 138 | struct bt_stream_class_common *stream_class, |
| 139 | struct bt_validation_output *validation_output, |
| 140 | create_field_func create_field_func, |
| 141 | release_field_func release_field_func, |
| 142 | create_header_field_func create_header_field_func, |
| 143 | release_header_field_func release_header_field_func, |
| 144 | struct bt_field_wrapper **header_field, |
| 145 | struct bt_field_common **stream_event_context_field, |
| 146 | struct bt_field_common **context_field, |
| 147 | struct bt_field_common **payload_field) |
| 148 | { |
| 149 | int ret = 0; |
| 150 | |
| 151 | if (validation_output->event_header_type) { |
| 152 | BT_LOGD("Creating initial event header field: ft-addr=%p", |
| 153 | validation_output->event_header_type); |
| 154 | *header_field = |
| 155 | create_header_field_func(stream_class, |
| 156 | validation_output->event_header_type); |
| 157 | if (!*header_field) { |
| 158 | BT_LOGE_STR("Cannot create initial event header field object."); |
| 159 | goto error; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | if (validation_output->stream_event_ctx_type) { |
| 164 | BT_LOGD("Creating initial stream event context field: ft-addr=%p", |
| 165 | validation_output->stream_event_ctx_type); |
| 166 | *stream_event_context_field = create_field_func( |
| 167 | validation_output->stream_event_ctx_type); |
| 168 | if (!*stream_event_context_field) { |
| 169 | BT_LOGE_STR("Cannot create initial stream event context field object."); |
| 170 | goto error; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | if (validation_output->event_context_type) { |
| 175 | BT_LOGD("Creating initial event context field: ft-addr=%p", |
| 176 | validation_output->event_context_type); |
| 177 | *context_field = create_field_func( |
| 178 | validation_output->event_context_type); |
| 179 | if (!*context_field) { |
| 180 | BT_LOGE_STR("Cannot create initial event context field object."); |
| 181 | goto error; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | if (validation_output->event_payload_type) { |
| 186 | BT_LOGD("Creating initial event payload field: ft-addr=%p", |
| 187 | validation_output->event_payload_type); |
| 188 | *payload_field = create_field_func( |
| 189 | validation_output->event_payload_type); |
| 190 | if (!*payload_field) { |
| 191 | BT_LOGE_STR("Cannot create initial event payload field object."); |
| 192 | goto error; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | goto end; |
| 197 | |
| 198 | error: |
| 199 | if (*header_field) { |
| 200 | release_header_field_func(*header_field, stream_class); |
| 201 | } |
| 202 | |
| 203 | if (*stream_event_context_field) { |
| 204 | release_field_func(*stream_event_context_field); |
| 205 | } |
| 206 | |
| 207 | if (*context_field) { |
| 208 | release_field_func(*context_field); |
| 209 | } |
| 210 | |
| 211 | if (*payload_field) { |
| 212 | release_field_func(*payload_field); |
| 213 | } |
| 214 | |
| 215 | ret = -1; |
| 216 | |
| 217 | end: |
| 218 | return ret; |
| 219 | } |
| 220 | |
| 221 | BT_HIDDEN |
| 222 | int _bt_event_common_validate(struct bt_event_common *event) |
| 223 | { |
| 224 | int ret = 0; |
| 225 | struct bt_stream_class_common *stream_class; |
| 226 | |
| 227 | BT_ASSERT(event); |
| 228 | if (event->header_field) { |
| 229 | ret = bt_field_common_validate_recursive( |
| 230 | event->header_field->field); |
| 231 | if (ret) { |
| 232 | BT_ASSERT_PRE_MSG("Invalid event's header field: " |
| 233 | "%![event-]+_e, %![field-]+_f", |
| 234 | event, event->header_field->field); |
| 235 | goto end; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | stream_class = bt_event_class_common_borrow_stream_class(event->class); |
| 240 | |
| 241 | /* |
| 242 | * We should not have been able to create the event without associating |
| 243 | * the event class to a stream class. |
| 244 | */ |
| 245 | BT_ASSERT(stream_class); |
| 246 | |
| 247 | if (stream_class->event_context_field_type) { |
| 248 | ret = bt_field_common_validate_recursive( |
| 249 | event->stream_event_context_field); |
| 250 | if (ret) { |
| 251 | BT_ASSERT_PRE_MSG("Invalid event's stream event context field: " |
| 252 | "%![event-]+_e, %![field-]+_f", |
| 253 | event, event->stream_event_context_field); |
| 254 | goto end; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | if (event->class->context_field_type) { |
| 259 | ret = bt_field_common_validate_recursive(event->context_field); |
| 260 | if (ret) { |
| 261 | BT_ASSERT_PRE_MSG("Invalid event's payload field: " |
| 262 | "%![event-]+_e, %![field-]+_f", |
| 263 | event, event->context_field); |
| 264 | goto end; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | ret = bt_field_common_validate_recursive(event->payload_field); |
| 269 | if (ret) { |
| 270 | BT_ASSERT_PRE_MSG("Invalid event's payload field: " |
| 271 | "%![event-]+_e, %![field-]+_f", |
| 272 | event, event->payload_field); |
| 273 | goto end; |
| 274 | } |
| 275 | |
| 276 | end: |
| 277 | return ret; |
| 278 | } |
| 279 | |
| 280 | BT_HIDDEN |
| 281 | void _bt_event_common_set_is_frozen(struct bt_event_common *event, |
| 282 | bool is_frozen) |
| 283 | { |
| 284 | BT_ASSERT(event); |
| 285 | |
| 286 | if (event->frozen) { |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | BT_LOGD("Freezing event: addr=%p, " |
| 291 | "event-class-name=\"%s\", event-class-id=%" PRId64, |
| 292 | event, bt_event_class_common_get_name(event->class), |
| 293 | bt_event_class_common_get_id(event->class)); |
| 294 | |
| 295 | if (event->header_field) { |
| 296 | BT_LOGD_STR("Freezing event's header field."); |
| 297 | bt_field_common_set_is_frozen_recursive( |
| 298 | event->header_field->field, is_frozen); |
| 299 | } |
| 300 | |
| 301 | if (event->stream_event_context_field) { |
| 302 | BT_LOGD_STR("Freezing event's stream event context field."); |
| 303 | bt_field_common_set_is_frozen_recursive( |
| 304 | event->stream_event_context_field, is_frozen); |
| 305 | } |
| 306 | |
| 307 | if (event->context_field) { |
| 308 | BT_LOGD_STR("Freezing event's context field."); |
| 309 | bt_field_common_set_is_frozen_recursive(event->context_field, |
| 310 | is_frozen); |
| 311 | } |
| 312 | |
| 313 | if (event->payload_field) { |
| 314 | BT_LOGD_STR("Freezing event's payload field."); |
| 315 | bt_field_common_set_is_frozen_recursive(event->payload_field, |
| 316 | is_frozen); |
| 317 | } |
| 318 | |
| 319 | event->frozen = is_frozen; |
| 320 | } |
| 321 | |
| 322 | BT_HIDDEN |
| 323 | int bt_event_common_initialize(struct bt_event_common *event, |
| 324 | struct bt_event_class_common *event_class, |
| 325 | struct bt_clock_class *init_expected_clock_class, |
| 326 | bool is_shared_with_parent, bt_object_release_func release_func, |
| 327 | bt_validation_flag_copy_field_type_func field_type_copy_func, |
| 328 | bool must_be_in_trace, |
| 329 | int (*map_clock_classes_func)(struct bt_stream_class_common *stream_class, |
| 330 | struct bt_field_type_common *packet_context_field_type, |
| 331 | struct bt_field_type_common *event_header_field_type), |
| 332 | create_field_func create_field_func, |
| 333 | release_field_func release_field_func, |
| 334 | create_header_field_func create_header_field_func, |
| 335 | release_header_field_func release_header_field_func) |
| 336 | { |
| 337 | int ret; |
| 338 | struct bt_trace_common *trace = NULL; |
| 339 | struct bt_stream_class_common *stream_class = NULL; |
| 340 | struct bt_field_wrapper *event_header = NULL; |
| 341 | struct bt_field_common *stream_event_context = NULL; |
| 342 | struct bt_field_common *event_context = NULL; |
| 343 | struct bt_field_common *event_payload = NULL; |
| 344 | struct bt_validation_output validation_output = { 0 }; |
| 345 | struct bt_clock_class *expected_clock_class = |
| 346 | init_expected_clock_class ? bt_get(init_expected_clock_class) : |
| 347 | NULL; |
| 348 | |
| 349 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
| 350 | BT_LOGD("Initializing common event object: event-class-addr=%p, " |
| 351 | "event-class-name=\"%s\", event-class-id=%" PRId64, |
| 352 | event_class, bt_event_class_common_get_name(event_class), |
| 353 | bt_event_class_common_get_id(event_class)); |
| 354 | |
| 355 | stream_class = bt_event_class_common_borrow_stream_class(event_class); |
| 356 | BT_ASSERT_PRE(stream_class, |
| 357 | "Event class is not part of a stream class: %!+_E", event_class); |
| 358 | |
| 359 | /* The event class was frozen when added to its stream class */ |
| 360 | BT_ASSERT(event_class->frozen); |
| 361 | trace = bt_stream_class_common_borrow_trace(stream_class); |
| 362 | |
| 363 | if (must_be_in_trace) { |
| 364 | BT_ASSERT_PRE(trace, |
| 365 | "Event class's stream class is not part of a trace: " |
| 366 | "%![ec-]+_E, %![ec-]+_S", event_class, stream_class); |
| 367 | } |
| 368 | |
| 369 | /* |
| 370 | * This must be called before anything that can fail because on |
| 371 | * failure, the caller releases the reference to `event` to |
| 372 | * destroy it. |
| 373 | */ |
| 374 | if (is_shared_with_parent) { |
| 375 | bt_object_init_shared_with_parent(&event->base, release_func); |
| 376 | } else { |
| 377 | bt_object_init_unique(&event->base); |
| 378 | } |
| 379 | |
| 380 | if (!stream_class->frozen) { |
| 381 | /* |
| 382 | * Because this function freezes the stream class, |
| 383 | * validate that this stream class contains at most a |
| 384 | * single clock class so that we set its expected clock |
| 385 | * class for future checks. |
| 386 | */ |
| 387 | ret = bt_stream_class_common_validate_single_clock_class( |
| 388 | stream_class, &expected_clock_class); |
| 389 | if (ret) { |
| 390 | BT_LOGW("Event class's stream class or one of its event " |
| 391 | "classes contains a field type which is not " |
| 392 | "recursively mapped to the expected " |
| 393 | "clock class: " |
| 394 | "stream-class-addr=%p, " |
| 395 | "stream-class-id=%" PRId64 ", " |
| 396 | "stream-class-name=\"%s\", " |
| 397 | "expected-clock-class-addr=%p, " |
| 398 | "expected-clock-class-name=\"%s\"", |
| 399 | stream_class, |
| 400 | bt_stream_class_common_get_id(stream_class), |
| 401 | bt_stream_class_common_get_name(stream_class), |
| 402 | expected_clock_class, |
| 403 | expected_clock_class ? |
| 404 | bt_clock_class_get_name(expected_clock_class) : |
| 405 | NULL); |
| 406 | goto error; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | /* Validate the trace, the stream class, and the event class */ |
| 411 | ret = bt_event_common_validate_types_for_create( |
| 412 | event_class, &validation_output, field_type_copy_func); |
| 413 | if (ret) { |
| 414 | /* bt_event_common_validate_types_for_create() logs errors */ |
| 415 | goto error; |
| 416 | } |
| 417 | |
| 418 | if (map_clock_classes_func) { |
| 419 | /* |
| 420 | * Safe to automatically map selected fields to the |
| 421 | * stream's clock's class here because the stream class |
| 422 | * is about to be frozen. |
| 423 | */ |
| 424 | if (map_clock_classes_func(stream_class, |
| 425 | validation_output.packet_context_type, |
| 426 | validation_output.event_header_type)) { |
| 427 | BT_LOGW_STR("Cannot automatically map selected stream class's " |
| 428 | "field types to stream class's clock's class."); |
| 429 | goto error; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | /* |
| 434 | * event does not share a common ancestor with the event class; it has |
| 435 | * to guarantee its existence by holding a reference. This reference |
| 436 | * shall be released once the event is associated to a stream since, |
| 437 | * from that point, the event and its class will share the same |
| 438 | * lifetime. |
| 439 | */ |
| 440 | event->class = bt_get(event_class); |
| 441 | |
| 442 | ret = bt_event_common_create_fields(stream_class, |
| 443 | &validation_output, |
| 444 | create_field_func, release_field_func, |
| 445 | create_header_field_func, release_header_field_func, |
| 446 | &event_header, &stream_event_context, &event_context, |
| 447 | &event_payload); |
| 448 | if (ret) { |
| 449 | /* bt_event_common_create_fields() logs errors */ |
| 450 | goto error; |
| 451 | } |
| 452 | |
| 453 | /* |
| 454 | * At this point all the fields are created, potentially from |
| 455 | * validated copies of field types, so that the field types and |
| 456 | * fields can be replaced in the trace, stream class, |
| 457 | * event class, and created event. |
| 458 | */ |
| 459 | bt_validation_replace_types(trace, stream_class, event_class, |
| 460 | &validation_output, |
| 461 | BT_VALIDATION_FLAG_STREAM | BT_VALIDATION_FLAG_EVENT); |
| 462 | event->header_field = event_header; |
| 463 | event_header = NULL; |
| 464 | event->stream_event_context_field = stream_event_context; |
| 465 | stream_event_context = NULL; |
| 466 | event->context_field = event_context; |
| 467 | event_context = NULL; |
| 468 | event->payload_field = event_payload; |
| 469 | event_payload = NULL; |
| 470 | |
| 471 | /* |
| 472 | * Put what was not moved in bt_validation_replace_types(). |
| 473 | */ |
| 474 | bt_validation_output_put_types(&validation_output); |
| 475 | |
| 476 | /* |
| 477 | * Freeze the stream class since the event header must not be changed |
| 478 | * anymore. |
| 479 | */ |
| 480 | bt_stream_class_common_freeze(stream_class); |
| 481 | |
| 482 | /* |
| 483 | * It is safe to set the stream class's unique clock class |
| 484 | * now because the stream class is frozen. |
| 485 | */ |
| 486 | if (expected_clock_class) { |
| 487 | BT_MOVE(stream_class->clock_class, expected_clock_class); |
| 488 | } |
| 489 | |
| 490 | /* |
| 491 | * Mark stream class, and event class as valid since |
| 492 | * they're all frozen now. |
| 493 | */ |
| 494 | stream_class->valid = 1; |
| 495 | event_class->valid = 1; |
| 496 | |
| 497 | /* Put stuff we borrowed from the event class */ |
| 498 | BT_LOGD("Initialized event object: addr=%p, event-class-name=\"%s\", " |
| 499 | "event-class-id=%" PRId64, |
| 500 | event, bt_event_class_common_get_name(event->class), |
| 501 | bt_event_class_common_get_id(event->class)); |
| 502 | goto end; |
| 503 | |
| 504 | error: |
| 505 | bt_validation_output_put_types(&validation_output); |
| 506 | bt_put(expected_clock_class); |
| 507 | |
| 508 | if (event_header) { |
| 509 | release_header_field_func(event_header, stream_class); |
| 510 | } |
| 511 | |
| 512 | if (stream_event_context) { |
| 513 | release_field_func(stream_event_context); |
| 514 | } |
| 515 | |
| 516 | if (event_context) { |
| 517 | release_field_func(event_context); |
| 518 | } |
| 519 | |
| 520 | if (event_payload) { |
| 521 | release_field_func(event_payload); |
| 522 | } |
| 523 | |
| 524 | ret = -1; |
| 525 | |
| 526 | end: |
| 527 | return ret; |
| 528 | } |
| 529 | |
| 530 | static |
| 531 | void bt_event_header_field_recycle(struct bt_field_wrapper *field_wrapper, |
| 532 | struct bt_stream_class *stream_class) |
| 533 | { |
| 534 | BT_ASSERT(field_wrapper); |
| 535 | BT_LIB_LOGD("Recycling event header field: " |
| 536 | "addr=%p, %![sc-]+S, %![field-]+f", field_wrapper, |
| 537 | stream_class, field_wrapper->field); |
| 538 | bt_object_pool_recycle_object( |
| 539 | &stream_class->event_header_field_pool, |
| 540 | field_wrapper); |
| 541 | } |
| 542 | |
| 543 | static |
| 544 | struct bt_field_wrapper *create_event_header_field( |
| 545 | struct bt_stream_class *stream_class, |
| 546 | struct bt_field_type_common *ft) |
| 547 | { |
| 548 | struct bt_field_wrapper *field_wrapper = NULL; |
| 549 | |
| 550 | field_wrapper = bt_field_wrapper_create( |
| 551 | &stream_class->event_header_field_pool, (void *) ft); |
| 552 | if (!field_wrapper) { |
| 553 | goto error; |
| 554 | } |
| 555 | |
| 556 | goto end; |
| 557 | |
| 558 | error: |
| 559 | if (field_wrapper) { |
| 560 | bt_event_header_field_recycle(field_wrapper, stream_class); |
| 561 | field_wrapper = NULL; |
| 562 | } |
| 563 | |
| 564 | end: |
| 565 | return field_wrapper; |
| 566 | } |
| 567 | |
| 568 | BT_HIDDEN |
| 569 | struct bt_event *bt_event_new(struct bt_event_class *event_class) |
| 570 | { |
| 571 | int ret; |
| 572 | struct bt_event *event = NULL; |
| 573 | struct bt_stream_class *stream_class; |
| 574 | |
| 575 | event = g_new0(struct bt_event, 1); |
| 576 | if (!event) { |
| 577 | BT_LOGE_STR("Failed to allocate one event."); |
| 578 | goto error; |
| 579 | } |
| 580 | |
| 581 | ret = bt_event_common_initialize(BT_TO_COMMON(event), |
| 582 | BT_TO_COMMON(event_class), NULL, false, NULL, |
| 583 | (bt_validation_flag_copy_field_type_func) bt_field_type_copy, |
| 584 | true, NULL, |
| 585 | (create_field_func) bt_field_create_recursive, |
| 586 | (release_field_func) bt_field_destroy_recursive, |
| 587 | (create_header_field_func) create_event_header_field, |
| 588 | (release_header_field_func) bt_event_header_field_recycle); |
| 589 | if (ret) { |
| 590 | /* bt_event_common_initialize() logs errors */ |
| 591 | goto error; |
| 592 | } |
| 593 | |
| 594 | stream_class = bt_event_class_borrow_stream_class(event_class); |
| 595 | BT_ASSERT(stream_class); |
| 596 | ret = bt_clock_value_set_initialize(&event->cv_set); |
| 597 | if (ret) { |
| 598 | goto error; |
| 599 | } |
| 600 | |
| 601 | goto end; |
| 602 | |
| 603 | error: |
| 604 | if (event) { |
| 605 | bt_event_destroy(event); |
| 606 | event = NULL; |
| 607 | } |
| 608 | |
| 609 | end: |
| 610 | return event; |
| 611 | } |
| 612 | |
| 613 | struct bt_event_class *bt_event_borrow_class(struct bt_event *event) |
| 614 | { |
| 615 | BT_ASSERT_PRE_NON_NULL(event, "Event"); |
| 616 | return BT_FROM_COMMON( |
| 617 | bt_event_common_borrow_class(BT_TO_COMMON(event))); |
| 618 | } |
| 619 | |
| 620 | struct bt_stream *bt_event_borrow_stream(struct bt_event *event) |
| 621 | { |
| 622 | BT_ASSERT_PRE_NON_NULL(event, "Event"); |
| 623 | return event->packet ? event->packet->stream : NULL; |
| 624 | } |
| 625 | |
| 626 | struct bt_field *bt_event_borrow_payload(struct bt_event *event) |
| 627 | { |
| 628 | return BT_FROM_COMMON( |
| 629 | bt_event_common_borrow_payload(BT_TO_COMMON(event))); |
| 630 | } |
| 631 | |
| 632 | struct bt_field *bt_event_borrow_header(struct bt_event *event) |
| 633 | { |
| 634 | return BT_FROM_COMMON( |
| 635 | bt_event_common_borrow_header(BT_TO_COMMON(event))); |
| 636 | } |
| 637 | |
| 638 | struct bt_field *bt_event_borrow_context(struct bt_event *event) |
| 639 | { |
| 640 | return BT_FROM_COMMON( |
| 641 | bt_event_common_borrow_context(BT_TO_COMMON(event))); |
| 642 | } |
| 643 | |
| 644 | struct bt_field *bt_event_borrow_stream_event_context( |
| 645 | struct bt_event *event) |
| 646 | { |
| 647 | return BT_FROM_COMMON(bt_event_common_borrow_stream_event_context( |
| 648 | BT_TO_COMMON(event))); |
| 649 | } |
| 650 | |
| 651 | static |
| 652 | void release_event_header_field(struct bt_field_wrapper *field_wrapper, |
| 653 | struct bt_event_common *event_common) |
| 654 | { |
| 655 | struct bt_event *event = BT_FROM_COMMON(event_common); |
| 656 | struct bt_event_class *event_class = bt_event_borrow_class(event); |
| 657 | |
| 658 | if (!event_class) { |
| 659 | bt_field_wrapper_destroy(field_wrapper); |
| 660 | } else { |
| 661 | struct bt_stream_class *stream_class = |
| 662 | bt_event_class_borrow_stream_class(event_class); |
| 663 | |
| 664 | BT_ASSERT(stream_class); |
| 665 | bt_event_header_field_recycle(field_wrapper, stream_class); |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | BT_HIDDEN |
| 670 | void bt_event_destroy(struct bt_event *event) |
| 671 | { |
| 672 | BT_ASSERT(event); |
| 673 | bt_event_common_finalize((void *) event, |
| 674 | (void *) bt_field_destroy_recursive, |
| 675 | (void *) release_event_header_field); |
| 676 | bt_clock_value_set_finalize(&event->cv_set); |
| 677 | BT_LOGD_STR("Putting event's packet."); |
| 678 | bt_put(event->packet); |
| 679 | g_free(event); |
| 680 | } |
| 681 | |
| 682 | int bt_event_set_clock_value(struct bt_event *event, |
| 683 | struct bt_clock_class *clock_class, uint64_t raw_value, |
| 684 | bt_bool is_default) |
| 685 | { |
| 686 | BT_ASSERT_PRE_NON_NULL(event, "Event"); |
| 687 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
| 688 | BT_ASSERT_PRE_HOT(BT_TO_COMMON(event), "Event", ": %!+e", event); |
| 689 | BT_ASSERT_PRE(is_default, |
| 690 | "You can only set a default clock value as of this version."); |
| 691 | return bt_clock_value_set_set_clock_value(&event->cv_set, clock_class, |
| 692 | raw_value, is_default); |
| 693 | } |
| 694 | |
| 695 | struct bt_clock_value *bt_event_borrow_default_clock_value( |
| 696 | struct bt_event *event) |
| 697 | { |
| 698 | struct bt_clock_value *clock_value = NULL; |
| 699 | |
| 700 | BT_ASSERT_PRE_NON_NULL(event, "Event"); |
| 701 | clock_value = event->cv_set.default_cv; |
| 702 | if (!clock_value) { |
| 703 | BT_LIB_LOGV("No default clock value: %![event-]+e", event); |
| 704 | } |
| 705 | |
| 706 | return clock_value; |
| 707 | } |
| 708 | |
| 709 | struct bt_packet *bt_event_borrow_packet(struct bt_event *event) |
| 710 | { |
| 711 | struct bt_packet *packet = NULL; |
| 712 | |
| 713 | BT_ASSERT_PRE_NON_NULL(event, "Event"); |
| 714 | if (!event->packet) { |
| 715 | BT_LOGV("Event has no current packet: addr=%p, " |
| 716 | "event-class-name=\"%s\", event-class-id=%" PRId64, |
| 717 | event, bt_event_class_common_get_name(event->common.class), |
| 718 | bt_event_class_common_get_id(event->common.class)); |
| 719 | goto end; |
| 720 | } |
| 721 | |
| 722 | packet = event->packet; |
| 723 | |
| 724 | end: |
| 725 | return packet; |
| 726 | } |
| 727 | |
| 728 | BT_HIDDEN |
| 729 | void _bt_event_set_is_frozen(struct bt_event *event, bool is_frozen) |
| 730 | { |
| 731 | bt_event_common_set_is_frozen(BT_TO_COMMON(event), is_frozen); |
| 732 | BT_LOGD_STR("Freezing event's packet."); |
| 733 | bt_packet_set_is_frozen(event->packet, is_frozen); |
| 734 | } |
| 735 | |
| 736 | int bt_event_move_header(struct bt_event *event, |
| 737 | struct bt_event_header_field *header_field) |
| 738 | { |
| 739 | struct bt_stream_class *stream_class; |
| 740 | struct bt_field_wrapper *field_wrapper = (void *) header_field; |
| 741 | |
| 742 | BT_ASSERT_PRE_NON_NULL(event, "Event"); |
| 743 | BT_ASSERT_PRE_NON_NULL(field_wrapper, "Header field"); |
| 744 | BT_ASSERT_PRE_HOT(BT_TO_COMMON(event), "Event", ": %!+e", event); |
| 745 | stream_class = bt_event_class_borrow_stream_class( |
| 746 | bt_event_borrow_class(event)); |
| 747 | BT_ASSERT_PRE(stream_class->common.event_header_field_type, |
| 748 | "Stream class has no event header field type: %!+S", |
| 749 | stream_class); |
| 750 | |
| 751 | /* Recycle current header field: always exists */ |
| 752 | BT_ASSERT(event->common.header_field); |
| 753 | bt_event_header_field_recycle(event->common.header_field, |
| 754 | stream_class); |
| 755 | |
| 756 | /* Move new field */ |
| 757 | event->common.header_field = (void *) field_wrapper; |
| 758 | return 0; |
| 759 | } |