| 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 | void bt_event_destroy(struct bt_object *obj); |
| 60 | |
| 61 | static |
| 62 | int bt_event_common_validate_types_for_create( |
| 63 | struct bt_event_class_common *event_class, |
| 64 | struct bt_validation_output *validation_output, |
| 65 | bt_validation_flag_copy_field_type_func copy_field_type_func) |
| 66 | { |
| 67 | int ret; |
| 68 | enum bt_validation_flag validation_flags = |
| 69 | BT_VALIDATION_FLAG_STREAM | |
| 70 | BT_VALIDATION_FLAG_EVENT; |
| 71 | struct bt_trace_common *trace = NULL; |
| 72 | struct bt_stream_class_common *stream_class = NULL; |
| 73 | struct bt_field_type_common *packet_header_type = NULL; |
| 74 | struct bt_field_type_common *packet_context_type = NULL; |
| 75 | struct bt_field_type_common *event_header_type = NULL; |
| 76 | struct bt_field_type_common *stream_event_ctx_type = NULL; |
| 77 | struct bt_field_type_common *event_context_type = NULL; |
| 78 | struct bt_field_type_common *event_payload_type = NULL; |
| 79 | int trace_valid = 0; |
| 80 | struct bt_value *environment = NULL; |
| 81 | |
| 82 | stream_class = bt_event_class_common_borrow_stream_class(event_class); |
| 83 | BT_ASSERT(stream_class); |
| 84 | trace = bt_stream_class_common_borrow_trace(stream_class); |
| 85 | if (trace) { |
| 86 | BT_LOGD_STR("Event class is part of a trace."); |
| 87 | packet_header_type = |
| 88 | bt_trace_common_borrow_packet_header_field_type(trace); |
| 89 | trace_valid = trace->valid; |
| 90 | BT_ASSERT(trace_valid); |
| 91 | environment = trace->environment; |
| 92 | } |
| 93 | |
| 94 | packet_context_type = |
| 95 | bt_stream_class_common_borrow_packet_context_field_type( |
| 96 | stream_class); |
| 97 | event_header_type = |
| 98 | bt_stream_class_common_borrow_event_header_field_type( |
| 99 | stream_class); |
| 100 | stream_event_ctx_type = |
| 101 | bt_stream_class_common_borrow_event_context_field_type( |
| 102 | stream_class); |
| 103 | event_context_type = |
| 104 | bt_event_class_common_borrow_context_field_type(event_class); |
| 105 | event_payload_type = |
| 106 | bt_event_class_common_borrow_payload_field_type(event_class); |
| 107 | ret = bt_validate_class_types(environment, packet_header_type, |
| 108 | packet_context_type, event_header_type, stream_event_ctx_type, |
| 109 | event_context_type, event_payload_type, trace_valid, |
| 110 | stream_class->valid, event_class->valid, |
| 111 | validation_output, validation_flags, copy_field_type_func); |
| 112 | if (ret) { |
| 113 | /* |
| 114 | * This means something went wrong during the validation |
| 115 | * process, not that the objects are invalid. |
| 116 | */ |
| 117 | BT_LOGE("Failed to validate event and parents: ret=%d", ret); |
| 118 | goto error; |
| 119 | } |
| 120 | |
| 121 | if ((validation_output->valid_flags & validation_flags) != |
| 122 | validation_flags) { |
| 123 | /* Invalid trace/stream class/event class */ |
| 124 | BT_LOGW("Invalid trace, stream class, or event class: " |
| 125 | "valid-flags=0x%x", validation_output->valid_flags); |
| 126 | goto error; |
| 127 | } |
| 128 | |
| 129 | goto end; |
| 130 | |
| 131 | error: |
| 132 | bt_validation_output_put_types(validation_output); |
| 133 | ret = -1; |
| 134 | |
| 135 | end: |
| 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | static |
| 140 | int bt_event_common_create_fields( |
| 141 | struct bt_validation_output *validation_output, |
| 142 | void *(*create_field_func)(void *), |
| 143 | struct bt_field_common **header_field, |
| 144 | struct bt_field_common **stream_event_context_field, |
| 145 | struct bt_field_common **context_field, |
| 146 | struct bt_field_common **payload_field) |
| 147 | { |
| 148 | int ret = 0; |
| 149 | |
| 150 | if (validation_output->event_header_type) { |
| 151 | BT_LOGD("Creating initial event header field: ft-addr=%p", |
| 152 | validation_output->event_header_type); |
| 153 | *header_field = |
| 154 | create_field_func(validation_output->event_header_type); |
| 155 | if (!*header_field) { |
| 156 | BT_LOGE_STR("Cannot create initial event header field object."); |
| 157 | goto error; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | if (validation_output->stream_event_ctx_type) { |
| 162 | BT_LOGD("Creating initial stream event context field: ft-addr=%p", |
| 163 | validation_output->stream_event_ctx_type); |
| 164 | *stream_event_context_field = create_field_func( |
| 165 | validation_output->stream_event_ctx_type); |
| 166 | if (!*stream_event_context_field) { |
| 167 | BT_LOGE_STR("Cannot create initial stream event context field object."); |
| 168 | goto error; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | if (validation_output->event_context_type) { |
| 173 | BT_LOGD("Creating initial event context field: ft-addr=%p", |
| 174 | validation_output->event_context_type); |
| 175 | *context_field = create_field_func( |
| 176 | validation_output->event_context_type); |
| 177 | if (!*context_field) { |
| 178 | BT_LOGE_STR("Cannot create initial event context field object."); |
| 179 | goto error; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | if (validation_output->event_payload_type) { |
| 184 | BT_LOGD("Creating initial event payload field: ft-addr=%p", |
| 185 | validation_output->event_payload_type); |
| 186 | *payload_field = create_field_func( |
| 187 | validation_output->event_payload_type); |
| 188 | if (!*payload_field) { |
| 189 | BT_LOGE_STR("Cannot create initial event payload field object."); |
| 190 | goto error; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | goto end; |
| 195 | |
| 196 | error: |
| 197 | BT_PUT(*header_field); |
| 198 | BT_PUT(*stream_event_context_field); |
| 199 | BT_PUT(*context_field); |
| 200 | BT_PUT(*payload_field); |
| 201 | ret = -1; |
| 202 | |
| 203 | end: |
| 204 | return ret; |
| 205 | } |
| 206 | |
| 207 | BT_HIDDEN |
| 208 | int _bt_event_common_validate(struct bt_event_common *event) |
| 209 | { |
| 210 | int ret = 0; |
| 211 | struct bt_stream_class_common *stream_class; |
| 212 | |
| 213 | BT_ASSERT(event); |
| 214 | if (event->header_field) { |
| 215 | ret = bt_field_common_validate_recursive(event->header_field); |
| 216 | if (ret) { |
| 217 | BT_ASSERT_PRE_MSG("Invalid event's header field: " |
| 218 | "%![event-]+_e, %![field-]+_f", |
| 219 | event, event->header_field); |
| 220 | goto end; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | stream_class = bt_event_class_common_borrow_stream_class(event->class); |
| 225 | |
| 226 | /* |
| 227 | * We should not have been able to create the event without associating |
| 228 | * the event class to a stream class. |
| 229 | */ |
| 230 | BT_ASSERT(stream_class); |
| 231 | |
| 232 | if (stream_class->event_context_field_type) { |
| 233 | ret = bt_field_common_validate_recursive( |
| 234 | event->stream_event_context_field); |
| 235 | if (ret) { |
| 236 | BT_ASSERT_PRE_MSG("Invalid event's stream event context field: " |
| 237 | "%![event-]+_e, %![field-]+_f", |
| 238 | event, event->stream_event_context_field); |
| 239 | goto end; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | if (event->class->context_field_type) { |
| 244 | ret = bt_field_common_validate_recursive(event->context_field); |
| 245 | if (ret) { |
| 246 | BT_ASSERT_PRE_MSG("Invalid event's payload field: " |
| 247 | "%![event-]+_e, %![field-]+_f", |
| 248 | event, event->context_field); |
| 249 | goto end; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | ret = bt_field_common_validate_recursive(event->payload_field); |
| 254 | if (ret) { |
| 255 | BT_ASSERT_PRE_MSG("Invalid event's payload field: " |
| 256 | "%![event-]+_e, %![field-]+_f", |
| 257 | event, event->payload_field); |
| 258 | goto end; |
| 259 | } |
| 260 | |
| 261 | end: |
| 262 | return ret; |
| 263 | } |
| 264 | |
| 265 | BT_HIDDEN |
| 266 | void _bt_event_common_freeze(struct bt_event_common *event) |
| 267 | { |
| 268 | BT_ASSERT(event); |
| 269 | |
| 270 | if (event->frozen) { |
| 271 | return; |
| 272 | } |
| 273 | |
| 274 | BT_LOGD("Freezing event: addr=%p, " |
| 275 | "event-class-name=\"%s\", event-class-id=%" PRId64, |
| 276 | event, bt_event_class_common_get_name(event->class), |
| 277 | bt_event_class_common_get_id(event->class)); |
| 278 | BT_LOGD_STR("Freezing event's header field."); |
| 279 | bt_field_common_freeze_recursive(event->header_field); |
| 280 | BT_LOGD_STR("Freezing event's stream event context field."); |
| 281 | bt_field_common_freeze_recursive(event->stream_event_context_field); |
| 282 | BT_LOGD_STR("Freezing event's context field."); |
| 283 | bt_field_common_freeze_recursive(event->context_field); |
| 284 | BT_LOGD_STR("Freezing event's payload field."); |
| 285 | bt_field_common_freeze_recursive(event->payload_field); |
| 286 | event->frozen = 1; |
| 287 | } |
| 288 | |
| 289 | BT_HIDDEN |
| 290 | int bt_event_common_initialize(struct bt_event_common *event, |
| 291 | struct bt_event_class_common *event_class, |
| 292 | struct bt_clock_class *init_expected_clock_class, |
| 293 | bt_object_release_func release_func, |
| 294 | bt_validation_flag_copy_field_type_func field_type_copy_func, |
| 295 | bool must_be_in_trace, |
| 296 | int (*map_clock_classes_func)(struct bt_stream_class_common *stream_class, |
| 297 | struct bt_field_type_common *packet_context_field_type, |
| 298 | struct bt_field_type_common *event_header_field_type), |
| 299 | void *(*create_field_func)(void *)) |
| 300 | { |
| 301 | int ret; |
| 302 | struct bt_trace_common *trace = NULL; |
| 303 | struct bt_stream_class_common *stream_class = NULL; |
| 304 | struct bt_field_common *event_header = NULL; |
| 305 | struct bt_field_common *stream_event_context = NULL; |
| 306 | struct bt_field_common *event_context = NULL; |
| 307 | struct bt_field_common *event_payload = NULL; |
| 308 | struct bt_validation_output validation_output = { 0 }; |
| 309 | struct bt_clock_class *expected_clock_class = |
| 310 | init_expected_clock_class ? bt_get(init_expected_clock_class) : |
| 311 | NULL; |
| 312 | |
| 313 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
| 314 | BT_LOGD("Initializing common event object: event-class-addr=%p, " |
| 315 | "event-class-name=\"%s\", event-class-id=%" PRId64, |
| 316 | event_class, bt_event_class_common_get_name(event_class), |
| 317 | bt_event_class_common_get_id(event_class)); |
| 318 | |
| 319 | stream_class = bt_event_class_common_borrow_stream_class(event_class); |
| 320 | BT_ASSERT_PRE(stream_class, |
| 321 | "Event class is not part of a stream class: %!+_E", event_class); |
| 322 | |
| 323 | /* The event class was frozen when added to its stream class */ |
| 324 | BT_ASSERT(event_class->frozen); |
| 325 | trace = bt_stream_class_common_borrow_trace(stream_class); |
| 326 | |
| 327 | if (must_be_in_trace) { |
| 328 | BT_ASSERT_PRE(trace, |
| 329 | "Event class's stream class is not part of a trace: " |
| 330 | "%![ec-]+_E, %![ec-]+_S", event_class, stream_class); |
| 331 | } |
| 332 | |
| 333 | /* |
| 334 | * This must be called before anything that can fail because on |
| 335 | * failure, the caller releases the reference to `event` to |
| 336 | * destroy it. |
| 337 | */ |
| 338 | bt_object_init(event, release_func); |
| 339 | |
| 340 | if (!stream_class->frozen) { |
| 341 | /* |
| 342 | * Because this function freezes the stream class, |
| 343 | * validate that this stream class contains at most a |
| 344 | * single clock class so that we set its expected clock |
| 345 | * class for future checks. |
| 346 | */ |
| 347 | ret = bt_stream_class_common_validate_single_clock_class( |
| 348 | stream_class, &expected_clock_class); |
| 349 | if (ret) { |
| 350 | BT_LOGW("Event class's stream class or one of its event " |
| 351 | "classes contains a field type which is not " |
| 352 | "recursively mapped to the expected " |
| 353 | "clock class: " |
| 354 | "stream-class-addr=%p, " |
| 355 | "stream-class-id=%" PRId64 ", " |
| 356 | "stream-class-name=\"%s\", " |
| 357 | "expected-clock-class-addr=%p, " |
| 358 | "expected-clock-class-name=\"%s\"", |
| 359 | stream_class, |
| 360 | bt_stream_class_common_get_id(stream_class), |
| 361 | bt_stream_class_common_get_name(stream_class), |
| 362 | expected_clock_class, |
| 363 | expected_clock_class ? |
| 364 | bt_clock_class_get_name(expected_clock_class) : |
| 365 | NULL); |
| 366 | goto error; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | /* Validate the trace, the stream class, and the event class */ |
| 371 | ret = bt_event_common_validate_types_for_create( |
| 372 | event_class, &validation_output, field_type_copy_func); |
| 373 | if (ret) { |
| 374 | /* bt_event_common_validate_types_for_create() logs errors */ |
| 375 | goto error; |
| 376 | } |
| 377 | |
| 378 | if (map_clock_classes_func) { |
| 379 | /* |
| 380 | * Safe to automatically map selected fields to the |
| 381 | * stream's clock's class here because the stream class |
| 382 | * is about to be frozen. |
| 383 | */ |
| 384 | if (map_clock_classes_func(stream_class, |
| 385 | validation_output.packet_context_type, |
| 386 | validation_output.event_header_type)) { |
| 387 | BT_LOGW_STR("Cannot automatically map selected stream class's " |
| 388 | "field types to stream class's clock's class."); |
| 389 | goto error; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | /* |
| 394 | * event does not share a common ancestor with the event class; it has |
| 395 | * to guarantee its existence by holding a reference. This reference |
| 396 | * shall be released once the event is associated to a stream since, |
| 397 | * from that point, the event and its class will share the same |
| 398 | * lifetime. |
| 399 | */ |
| 400 | event->class = bt_get(event_class); |
| 401 | |
| 402 | ret = bt_event_common_create_fields(&validation_output, |
| 403 | create_field_func, &event_header, |
| 404 | &stream_event_context, &event_context, &event_payload); |
| 405 | if (ret) { |
| 406 | /* bt_event_common_create_fields() logs errors */ |
| 407 | goto error; |
| 408 | } |
| 409 | |
| 410 | /* |
| 411 | * At this point all the fields are created, potentially from |
| 412 | * validated copies of field types, so that the field types and |
| 413 | * fields can be replaced in the trace, stream class, |
| 414 | * event class, and created event. |
| 415 | */ |
| 416 | bt_validation_replace_types(trace, stream_class, event_class, |
| 417 | &validation_output, |
| 418 | BT_VALIDATION_FLAG_STREAM | BT_VALIDATION_FLAG_EVENT); |
| 419 | BT_MOVE(event->header_field, event_header); |
| 420 | BT_MOVE(event->stream_event_context_field, stream_event_context); |
| 421 | BT_MOVE(event->context_field, event_context); |
| 422 | BT_MOVE(event->payload_field, event_payload); |
| 423 | |
| 424 | /* |
| 425 | * Put what was not moved in bt_validation_replace_types(). |
| 426 | */ |
| 427 | bt_validation_output_put_types(&validation_output); |
| 428 | |
| 429 | /* |
| 430 | * Freeze the stream class since the event header must not be changed |
| 431 | * anymore. |
| 432 | */ |
| 433 | bt_stream_class_common_freeze(stream_class); |
| 434 | |
| 435 | /* |
| 436 | * It is safe to set the stream class's unique clock class |
| 437 | * now because the stream class is frozen. |
| 438 | */ |
| 439 | if (expected_clock_class) { |
| 440 | BT_MOVE(stream_class->clock_class, expected_clock_class); |
| 441 | } |
| 442 | |
| 443 | /* |
| 444 | * Mark stream class, and event class as valid since |
| 445 | * they're all frozen now. |
| 446 | */ |
| 447 | stream_class->valid = 1; |
| 448 | event_class->valid = 1; |
| 449 | |
| 450 | /* Put stuff we borrowed from the event class */ |
| 451 | BT_LOGD("Initialized event object: addr=%p, event-class-name=\"%s\", " |
| 452 | "event-class-id=%" PRId64, |
| 453 | event, bt_event_class_common_get_name(event->class), |
| 454 | bt_event_class_common_get_id(event->class)); |
| 455 | goto end; |
| 456 | |
| 457 | error: |
| 458 | bt_validation_output_put_types(&validation_output); |
| 459 | bt_put(expected_clock_class); |
| 460 | bt_put(event_header); |
| 461 | bt_put(stream_event_context); |
| 462 | bt_put(event_context); |
| 463 | bt_put(event_payload); |
| 464 | ret = -1; |
| 465 | |
| 466 | end: |
| 467 | return ret; |
| 468 | } |
| 469 | |
| 470 | struct bt_event *bt_event_create(struct bt_event_class *event_class) |
| 471 | { |
| 472 | int ret; |
| 473 | struct bt_event *event = NULL; |
| 474 | |
| 475 | event = g_new0(struct bt_event, 1); |
| 476 | if (!event) { |
| 477 | BT_LOGE_STR("Failed to allocate one event."); |
| 478 | goto error; |
| 479 | } |
| 480 | |
| 481 | ret = bt_event_common_initialize(BT_TO_COMMON(event), |
| 482 | BT_TO_COMMON(event_class), NULL, bt_event_destroy, |
| 483 | (bt_validation_flag_copy_field_type_func) bt_field_type_copy, |
| 484 | true, NULL, (void *) bt_field_create); |
| 485 | if (ret) { |
| 486 | /* bt_event_common_initialize() logs errors */ |
| 487 | goto error; |
| 488 | } |
| 489 | |
| 490 | event->clock_values = g_hash_table_new_full(g_direct_hash, |
| 491 | g_direct_equal, NULL, bt_put); |
| 492 | assert(event->clock_values); |
| 493 | goto end; |
| 494 | |
| 495 | error: |
| 496 | BT_PUT(event); |
| 497 | |
| 498 | end: |
| 499 | return event; |
| 500 | } |
| 501 | |
| 502 | struct bt_event_class *bt_event_borrow_class(struct bt_event *event) |
| 503 | { |
| 504 | BT_ASSERT_PRE_NON_NULL(event, "Event"); |
| 505 | return BT_FROM_COMMON( |
| 506 | bt_event_common_borrow_class(BT_TO_COMMON(event))); |
| 507 | } |
| 508 | |
| 509 | struct bt_stream *bt_event_borrow_stream(struct bt_event *event) |
| 510 | { |
| 511 | BT_ASSERT_PRE_NON_NULL(event, "Event"); |
| 512 | return event->packet ? event->packet->stream : NULL; |
| 513 | } |
| 514 | |
| 515 | struct bt_field *bt_event_borrow_payload(struct bt_event *event) |
| 516 | { |
| 517 | return BT_FROM_COMMON( |
| 518 | bt_event_common_borrow_payload(BT_TO_COMMON(event))); |
| 519 | } |
| 520 | |
| 521 | int bt_event_set_payload(struct bt_event *event, struct bt_field *field) |
| 522 | { |
| 523 | return bt_event_common_set_payload(BT_TO_COMMON(event), |
| 524 | (void *) field); |
| 525 | } |
| 526 | |
| 527 | struct bt_field *bt_event_borrow_header(struct bt_event *event) |
| 528 | { |
| 529 | return BT_FROM_COMMON( |
| 530 | bt_event_common_borrow_header(BT_TO_COMMON(event))); |
| 531 | } |
| 532 | |
| 533 | int bt_event_set_header(struct bt_event *event, struct bt_field *field) |
| 534 | { |
| 535 | return bt_event_common_set_header(BT_TO_COMMON(event), (void *) field); |
| 536 | } |
| 537 | |
| 538 | struct bt_field *bt_event_borrow_context(struct bt_event *event) |
| 539 | { |
| 540 | return BT_FROM_COMMON( |
| 541 | bt_event_common_borrow_context(BT_TO_COMMON(event))); |
| 542 | } |
| 543 | |
| 544 | int bt_event_set_context(struct bt_event *event, struct bt_field *field) |
| 545 | { |
| 546 | return bt_event_common_set_context(BT_TO_COMMON(event), (void *) field); |
| 547 | } |
| 548 | |
| 549 | struct bt_field *bt_event_borrow_stream_event_context( |
| 550 | struct bt_event *event) |
| 551 | { |
| 552 | return BT_FROM_COMMON(bt_event_common_borrow_stream_event_context( |
| 553 | BT_TO_COMMON(event))); |
| 554 | } |
| 555 | |
| 556 | int bt_event_set_stream_event_context(struct bt_event *event, |
| 557 | struct bt_field *field) |
| 558 | { |
| 559 | return bt_event_common_set_stream_event_context( |
| 560 | BT_TO_COMMON(event), (void *) field); |
| 561 | } |
| 562 | |
| 563 | void bt_event_destroy(struct bt_object *obj) |
| 564 | { |
| 565 | struct bt_event *event = (void *) obj; |
| 566 | |
| 567 | bt_event_common_finalize(obj); |
| 568 | g_hash_table_destroy(event->clock_values); |
| 569 | BT_LOGD_STR("Putting event's packet."); |
| 570 | bt_put(event->packet); |
| 571 | g_free(event); |
| 572 | } |
| 573 | |
| 574 | struct bt_clock_value *bt_event_borrow_clock_value( |
| 575 | struct bt_event *event, struct bt_clock_class *clock_class) |
| 576 | { |
| 577 | struct bt_clock_value *clock_value = NULL; |
| 578 | |
| 579 | BT_ASSERT_PRE_NON_NULL(event, "Event"); |
| 580 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
| 581 | clock_value = g_hash_table_lookup(event->clock_values, clock_class); |
| 582 | if (!clock_value) { |
| 583 | BT_LOGV("No clock value associated to the given clock class: " |
| 584 | "event-addr=%p, event-class-name=\"%s\", " |
| 585 | "event-class-id=%" PRId64 ", clock-class-addr=%p, " |
| 586 | "clock-class-name=\"%s\"", event, |
| 587 | bt_event_class_common_get_name(event->common.class), |
| 588 | bt_event_class_common_get_id(event->common.class), |
| 589 | clock_class, bt_clock_class_get_name(clock_class)); |
| 590 | goto end; |
| 591 | } |
| 592 | |
| 593 | end: |
| 594 | return clock_value; |
| 595 | } |
| 596 | |
| 597 | int bt_event_set_clock_value(struct bt_event *event, |
| 598 | struct bt_clock_value *value) |
| 599 | { |
| 600 | struct bt_trace *trace; |
| 601 | struct bt_stream_class *stream_class; |
| 602 | struct bt_event_class *event_class; |
| 603 | struct bt_clock_class *clock_class = NULL; |
| 604 | |
| 605 | BT_ASSERT_PRE_NON_NULL(event, "Event"); |
| 606 | BT_ASSERT_PRE_NON_NULL(value, "Clock value"); |
| 607 | BT_ASSERT_PRE_EVENT_COMMON_HOT(BT_TO_COMMON(event), "Event"); |
| 608 | clock_class = bt_clock_value_borrow_class(value); |
| 609 | event_class = BT_FROM_COMMON(event->common.class); |
| 610 | BT_ASSERT(event_class); |
| 611 | stream_class = bt_event_class_borrow_stream_class(event_class); |
| 612 | BT_ASSERT(stream_class); |
| 613 | trace = bt_stream_class_borrow_trace(stream_class); |
| 614 | BT_ASSERT(trace); |
| 615 | BT_ASSERT_PRE(bt_trace_common_has_clock_class(BT_TO_COMMON(trace), |
| 616 | clock_class), |
| 617 | "Clock class is not part of event's trace: " |
| 618 | "%![event-]+e, %![clock-class-]+K", |
| 619 | event, clock_class); |
| 620 | g_hash_table_insert(event->clock_values, clock_class, bt_get(value)); |
| 621 | BT_LOGV("Set event's clock value: " |
| 622 | "event-addr=%p, event-class-name=\"%s\", " |
| 623 | "event-class-id=%" PRId64 ", clock-class-addr=%p, " |
| 624 | "clock-class-name=\"%s\", clock-value-addr=%p, " |
| 625 | "clock-value-cycles=%" PRIu64, |
| 626 | event, bt_event_class_common_get_name(event->common.class), |
| 627 | bt_event_class_common_get_id(event->common.class), |
| 628 | clock_class, bt_clock_class_get_name(clock_class), |
| 629 | value, value->value); |
| 630 | return 0; |
| 631 | } |
| 632 | |
| 633 | struct bt_packet *bt_event_borrow_packet(struct bt_event *event) |
| 634 | { |
| 635 | struct bt_packet *packet = NULL; |
| 636 | |
| 637 | BT_ASSERT_PRE_NON_NULL(event, "Event"); |
| 638 | if (!event->packet) { |
| 639 | BT_LOGV("Event has no current packet: addr=%p, " |
| 640 | "event-class-name=\"%s\", event-class-id=%" PRId64, |
| 641 | event, bt_event_class_common_get_name(event->common.class), |
| 642 | bt_event_class_common_get_id(event->common.class)); |
| 643 | goto end; |
| 644 | } |
| 645 | |
| 646 | packet = event->packet; |
| 647 | |
| 648 | end: |
| 649 | return packet; |
| 650 | } |
| 651 | |
| 652 | int bt_event_set_packet(struct bt_event *event, |
| 653 | struct bt_packet *packet) |
| 654 | { |
| 655 | BT_ASSERT_PRE_NON_NULL(event, "Event"); |
| 656 | BT_ASSERT_PRE_NON_NULL(packet, "Packet"); |
| 657 | BT_ASSERT_PRE_EVENT_COMMON_HOT(BT_TO_COMMON(event), "Event"); |
| 658 | |
| 659 | /* |
| 660 | * Make sure the new packet was created by this event's |
| 661 | * stream, if it is set. |
| 662 | */ |
| 663 | if (bt_event_borrow_stream(event)) { |
| 664 | BT_ASSERT_PRE(packet->stream == bt_event_borrow_stream(event), |
| 665 | "Packet's stream and event's stream differ: " |
| 666 | "%![event-]+e, %![packet-]+a", |
| 667 | event, packet); |
| 668 | } else { |
| 669 | BT_ASSERT_PRE(bt_event_class_borrow_stream_class( |
| 670 | BT_FROM_COMMON(event->common.class)) == |
| 671 | BT_FROM_COMMON(packet->stream->common.stream_class), |
| 672 | "Packet's stream class and event's stream class differ: " |
| 673 | "%![event-]+e, %![packet-]+a", |
| 674 | event, packet); |
| 675 | } |
| 676 | |
| 677 | bt_get(packet); |
| 678 | BT_MOVE(event->packet, packet); |
| 679 | BT_LOGV("Set event's packet: event-addr=%p, " |
| 680 | "event-class-name=\"%s\", event-class-id=%" PRId64 ", " |
| 681 | "packet-addr=%p", |
| 682 | event, bt_event_class_common_get_name(event->common.class), |
| 683 | bt_event_class_common_get_id(event->common.class), packet); |
| 684 | return 0; |
| 685 | } |
| 686 | |
| 687 | BT_HIDDEN |
| 688 | void _bt_event_freeze(struct bt_event *event) |
| 689 | { |
| 690 | _bt_event_common_freeze(BT_TO_COMMON(event)); |
| 691 | BT_LOGD_STR("Freezing event's packet."); |
| 692 | bt_packet_freeze(event->packet); |
| 693 | } |