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