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