4 * Babeltrace CTF IR - Event
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
29 #define BT_LOG_TAG "EVENT"
30 #include <babeltrace/lib-logging-internal.h>
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>
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
)
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
;
77 struct bt_value
*environment
= NULL
;
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
);
83 BT_LOGD_STR("Event class is part of a trace.");
85 bt_trace_common_borrow_packet_header_field_type(trace
);
86 trace_valid
= trace
->valid
;
87 BT_ASSERT(trace_valid
);
88 environment
= trace
->environment
;
92 bt_stream_class_common_borrow_packet_context_field_type(
95 bt_stream_class_common_borrow_event_header_field_type(
97 stream_event_ctx_type
=
98 bt_stream_class_common_borrow_event_context_field_type(
101 bt_event_class_common_borrow_context_field_type(event_class
);
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
);
111 * This means something went wrong during the validation
112 * process, not that the objects are invalid.
114 BT_LOGE("Failed to validate event and parents: ret=%d", ret
);
118 if ((validation_output
->valid_flags
& 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
);
129 bt_validation_output_put_types(validation_output
);
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
)
151 if (validation_output
->event_header_type
) {
152 BT_LOGD("Creating initial event header field: ft-addr=%p",
153 validation_output
->event_header_type
);
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.");
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.");
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.");
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.");
200 release_header_field_func(*header_field
, stream_class
);
203 if (*stream_event_context_field
) {
204 release_field_func(*stream_event_context_field
);
207 if (*context_field
) {
208 release_field_func(*context_field
);
211 if (*payload_field
) {
212 release_field_func(*payload_field
);
222 int _bt_event_common_validate(struct bt_event_common
*event
)
225 struct bt_stream_class_common
*stream_class
;
228 if (event
->header_field
) {
229 ret
= bt_field_common_validate_recursive(
230 event
->header_field
->field
);
232 BT_ASSERT_PRE_MSG("Invalid event's header field: "
233 "%![event-]+_e, %![field-]+_f",
234 event
, event
->header_field
->field
);
239 stream_class
= bt_event_class_common_borrow_stream_class(event
->class);
242 * We should not have been able to create the event without associating
243 * the event class to a stream class.
245 BT_ASSERT(stream_class
);
247 if (stream_class
->event_context_field_type
) {
248 ret
= bt_field_common_validate_recursive(
249 event
->stream_event_context_field
);
251 BT_ASSERT_PRE_MSG("Invalid event's stream event context field: "
252 "%![event-]+_e, %![field-]+_f",
253 event
, event
->stream_event_context_field
);
258 if (event
->class->context_field_type
) {
259 ret
= bt_field_common_validate_recursive(event
->context_field
);
261 BT_ASSERT_PRE_MSG("Invalid event's payload field: "
262 "%![event-]+_e, %![field-]+_f",
263 event
, event
->context_field
);
268 ret
= bt_field_common_validate_recursive(event
->payload_field
);
270 BT_ASSERT_PRE_MSG("Invalid event's payload field: "
271 "%![event-]+_e, %![field-]+_f",
272 event
, event
->payload_field
);
281 void _bt_event_common_set_is_frozen(struct bt_event_common
*event
,
285 BT_LOGD("Freezing event: addr=%p, "
286 "event-class-name=\"%s\", event-class-id=%" PRId64
,
287 event
, bt_event_class_common_get_name(event
->class),
288 bt_event_class_common_get_id(event
->class));
290 if (event
->header_field
) {
291 BT_LOGD_STR("Freezing event's header field.");
292 bt_field_common_set_is_frozen_recursive(
293 event
->header_field
->field
, is_frozen
);
296 if (event
->stream_event_context_field
) {
297 BT_LOGD_STR("Freezing event's stream event context field.");
298 bt_field_common_set_is_frozen_recursive(
299 event
->stream_event_context_field
, is_frozen
);
302 if (event
->context_field
) {
303 BT_LOGD_STR("Freezing event's context field.");
304 bt_field_common_set_is_frozen_recursive(event
->context_field
,
308 if (event
->payload_field
) {
309 BT_LOGD_STR("Freezing event's payload field.");
310 bt_field_common_set_is_frozen_recursive(event
->payload_field
,
314 event
->frozen
= is_frozen
;
318 int bt_event_common_initialize(struct bt_event_common
*event
,
319 struct bt_event_class_common
*event_class
,
320 struct bt_clock_class
*init_expected_clock_class
,
321 bool is_shared_with_parent
, bt_object_release_func release_func
,
322 bt_validation_flag_copy_field_type_func field_type_copy_func
,
323 bool must_be_in_trace
,
324 int (*map_clock_classes_func
)(struct bt_stream_class_common
*stream_class
,
325 struct bt_field_type_common
*packet_context_field_type
,
326 struct bt_field_type_common
*event_header_field_type
),
327 create_field_func create_field_func
,
328 release_field_func release_field_func
,
329 create_header_field_func create_header_field_func
,
330 release_header_field_func release_header_field_func
)
333 struct bt_trace_common
*trace
= NULL
;
334 struct bt_stream_class_common
*stream_class
= NULL
;
335 struct bt_field_wrapper
*event_header
= NULL
;
336 struct bt_field_common
*stream_event_context
= NULL
;
337 struct bt_field_common
*event_context
= NULL
;
338 struct bt_field_common
*event_payload
= NULL
;
339 struct bt_validation_output validation_output
= { 0 };
340 struct bt_clock_class
*expected_clock_class
=
341 init_expected_clock_class
? bt_get(init_expected_clock_class
) :
344 BT_ASSERT_PRE_NON_NULL(event_class
, "Event class");
345 BT_LOGD("Initializing common event object: event-class-addr=%p, "
346 "event-class-name=\"%s\", event-class-id=%" PRId64
,
347 event_class
, bt_event_class_common_get_name(event_class
),
348 bt_event_class_common_get_id(event_class
));
350 stream_class
= bt_event_class_common_borrow_stream_class(event_class
);
351 BT_ASSERT_PRE(stream_class
,
352 "Event class is not part of a stream class: %!+_E", event_class
);
354 /* The event class was frozen when added to its stream class */
355 BT_ASSERT(event_class
->frozen
);
356 trace
= bt_stream_class_common_borrow_trace(stream_class
);
358 if (must_be_in_trace
) {
360 "Event class's stream class is not part of a trace: "
361 "%![ec-]+_E, %![ec-]+_S", event_class
, stream_class
);
365 * This must be called before anything that can fail because on
366 * failure, the caller releases the reference to `event` to
369 if (is_shared_with_parent
) {
370 bt_object_init_shared_with_parent(&event
->base
, release_func
);
372 bt_object_init_unique(&event
->base
);
375 if (!stream_class
->frozen
) {
377 * Because this function freezes the stream class,
378 * validate that this stream class contains at most a
379 * single clock class so that we set its expected clock
380 * class for future checks.
382 ret
= bt_stream_class_common_validate_single_clock_class(
383 stream_class
, &expected_clock_class
);
385 BT_LOGW("Event class's stream class or one of its event "
386 "classes contains a field type which is not "
387 "recursively mapped to the expected "
389 "stream-class-addr=%p, "
390 "stream-class-id=%" PRId64
", "
391 "stream-class-name=\"%s\", "
392 "expected-clock-class-addr=%p, "
393 "expected-clock-class-name=\"%s\"",
395 bt_stream_class_common_get_id(stream_class
),
396 bt_stream_class_common_get_name(stream_class
),
397 expected_clock_class
,
398 expected_clock_class
?
399 bt_clock_class_get_name(expected_clock_class
) :
405 /* Validate the trace, the stream class, and the event class */
406 ret
= bt_event_common_validate_types_for_create(
407 event_class
, &validation_output
, field_type_copy_func
);
409 /* bt_event_common_validate_types_for_create() logs errors */
413 if (map_clock_classes_func
) {
415 * Safe to automatically map selected fields to the
416 * stream's clock's class here because the stream class
417 * is about to be frozen.
419 if (map_clock_classes_func(stream_class
,
420 validation_output
.packet_context_type
,
421 validation_output
.event_header_type
)) {
422 BT_LOGW_STR("Cannot automatically map selected stream class's "
423 "field types to stream class's clock's class.");
429 * event does not share a common ancestor with the event class; it has
430 * to guarantee its existence by holding a reference. This reference
431 * shall be released once the event is associated to a stream since,
432 * from that point, the event and its class will share the same
435 event
->class = bt_get(event_class
);
437 ret
= bt_event_common_create_fields(stream_class
,
439 create_field_func
, release_field_func
,
440 create_header_field_func
, release_header_field_func
,
441 &event_header
, &stream_event_context
, &event_context
,
444 /* bt_event_common_create_fields() logs errors */
449 * At this point all the fields are created, potentially from
450 * validated copies of field types, so that the field types and
451 * fields can be replaced in the trace, stream class,
452 * event class, and created event.
454 bt_validation_replace_types(trace
, stream_class
, event_class
,
456 BT_VALIDATION_FLAG_STREAM
| BT_VALIDATION_FLAG_EVENT
);
457 event
->header_field
= event_header
;
459 event
->stream_event_context_field
= stream_event_context
;
460 stream_event_context
= NULL
;
461 event
->context_field
= event_context
;
462 event_context
= NULL
;
463 event
->payload_field
= event_payload
;
464 event_payload
= NULL
;
467 * Put what was not moved in bt_validation_replace_types().
469 bt_validation_output_put_types(&validation_output
);
472 * Freeze the stream class since the event header must not be changed
475 bt_stream_class_common_freeze(stream_class
);
478 * It is safe to set the stream class's unique clock class
479 * now because the stream class is frozen.
481 if (expected_clock_class
) {
482 BT_MOVE(stream_class
->clock_class
, expected_clock_class
);
486 * Mark stream class, and event class as valid since
487 * they're all frozen now.
489 stream_class
->valid
= 1;
490 event_class
->valid
= 1;
492 /* Put stuff we borrowed from the event class */
493 BT_LOGD("Initialized event object: addr=%p, event-class-name=\"%s\", "
494 "event-class-id=%" PRId64
,
495 event
, bt_event_class_common_get_name(event
->class),
496 bt_event_class_common_get_id(event
->class));
500 bt_validation_output_put_types(&validation_output
);
501 bt_put(expected_clock_class
);
504 release_header_field_func(event_header
, stream_class
);
507 if (stream_event_context
) {
508 release_field_func(stream_event_context
);
512 release_field_func(event_context
);
516 release_field_func(event_payload
);
526 void bt_event_header_field_recycle(struct bt_field_wrapper
*field_wrapper
,
527 struct bt_stream_class
*stream_class
)
529 BT_ASSERT(field_wrapper
);
530 BT_LIB_LOGD("Recycling event header field: "
531 "addr=%p, %![sc-]+S, %![field-]+f", field_wrapper
,
532 stream_class
, field_wrapper
->field
);
533 bt_object_pool_recycle_object(
534 &stream_class
->event_header_field_pool
,
539 struct bt_field_wrapper
*create_event_header_field(
540 struct bt_stream_class
*stream_class
,
541 struct bt_field_type_common
*ft
)
543 struct bt_field_wrapper
*field_wrapper
= NULL
;
545 field_wrapper
= bt_field_wrapper_create(
546 &stream_class
->event_header_field_pool
, (void *) ft
);
547 if (!field_wrapper
) {
555 bt_event_header_field_recycle(field_wrapper
, stream_class
);
556 field_wrapper
= NULL
;
560 return field_wrapper
;
564 struct bt_event
*bt_event_new(struct bt_event_class
*event_class
)
567 struct bt_event
*event
= NULL
;
568 struct bt_stream_class
*stream_class
;
570 event
= g_new0(struct bt_event
, 1);
572 BT_LOGE_STR("Failed to allocate one event.");
576 ret
= bt_event_common_initialize(BT_TO_COMMON(event
),
577 BT_TO_COMMON(event_class
), NULL
, false, NULL
,
578 (bt_validation_flag_copy_field_type_func
) bt_field_type_copy
,
580 (create_field_func
) bt_field_create_recursive
,
581 (release_field_func
) bt_field_destroy_recursive
,
582 (create_header_field_func
) create_event_header_field
,
583 (release_header_field_func
) bt_event_header_field_recycle
);
585 /* bt_event_common_initialize() logs errors */
589 stream_class
= bt_event_class_borrow_stream_class(event_class
);
590 BT_ASSERT(stream_class
);
591 ret
= bt_clock_value_set_initialize(&event
->cv_set
);
600 bt_event_destroy(event
);
608 struct bt_event_class
*bt_event_borrow_class(struct bt_event
*event
)
610 BT_ASSERT_PRE_NON_NULL(event
, "Event");
611 return BT_FROM_COMMON(
612 bt_event_common_borrow_class(BT_TO_COMMON(event
)));
615 struct bt_stream
*bt_event_borrow_stream(struct bt_event
*event
)
617 BT_ASSERT_PRE_NON_NULL(event
, "Event");
618 return event
->packet
? event
->packet
->stream
: NULL
;
621 struct bt_field
*bt_event_borrow_payload(struct bt_event
*event
)
623 return BT_FROM_COMMON(
624 bt_event_common_borrow_payload(BT_TO_COMMON(event
)));
627 struct bt_field
*bt_event_borrow_header(struct bt_event
*event
)
629 return BT_FROM_COMMON(
630 bt_event_common_borrow_header(BT_TO_COMMON(event
)));
633 struct bt_field
*bt_event_borrow_context(struct bt_event
*event
)
635 return BT_FROM_COMMON(
636 bt_event_common_borrow_context(BT_TO_COMMON(event
)));
639 struct bt_field
*bt_event_borrow_stream_event_context(
640 struct bt_event
*event
)
642 return BT_FROM_COMMON(bt_event_common_borrow_stream_event_context(
643 BT_TO_COMMON(event
)));
647 void release_event_header_field(struct bt_field_wrapper
*field_wrapper
,
648 struct bt_event_common
*event_common
)
650 struct bt_event
*event
= BT_FROM_COMMON(event_common
);
651 struct bt_event_class
*event_class
= bt_event_borrow_class(event
);
654 bt_field_wrapper_destroy(field_wrapper
);
656 struct bt_stream_class
*stream_class
=
657 bt_event_class_borrow_stream_class(event_class
);
659 BT_ASSERT(stream_class
);
660 bt_event_header_field_recycle(field_wrapper
, stream_class
);
665 void bt_event_destroy(struct bt_event
*event
)
668 bt_event_common_finalize((void *) event
,
669 (void *) bt_field_destroy_recursive
,
670 (void *) release_event_header_field
);
671 bt_clock_value_set_finalize(&event
->cv_set
);
672 BT_LOGD_STR("Putting event's packet.");
673 bt_put(event
->packet
);
677 int bt_event_set_clock_value(struct bt_event
*event
,
678 struct bt_clock_class
*clock_class
, uint64_t raw_value
,
681 BT_ASSERT_PRE_NON_NULL(event
, "Event");
682 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
683 BT_ASSERT_PRE_HOT(BT_TO_COMMON(event
), "Event", ": %!+e", event
);
684 BT_ASSERT_PRE(is_default
,
685 "You can only set a default clock value as of this version.");
686 return bt_clock_value_set_set_clock_value(&event
->cv_set
, clock_class
,
687 raw_value
, is_default
);
690 struct bt_clock_value
*bt_event_borrow_default_clock_value(
691 struct bt_event
*event
)
693 struct bt_clock_value
*clock_value
= NULL
;
695 BT_ASSERT_PRE_NON_NULL(event
, "Event");
696 clock_value
= event
->cv_set
.default_cv
;
698 BT_LIB_LOGV("No default clock value: %![event-]+e", event
);
704 struct bt_packet
*bt_event_borrow_packet(struct bt_event
*event
)
706 struct bt_packet
*packet
= NULL
;
708 BT_ASSERT_PRE_NON_NULL(event
, "Event");
709 if (!event
->packet
) {
710 BT_LOGV("Event has no current packet: addr=%p, "
711 "event-class-name=\"%s\", event-class-id=%" PRId64
,
712 event
, bt_event_class_common_get_name(event
->common
.class),
713 bt_event_class_common_get_id(event
->common
.class));
717 packet
= event
->packet
;
724 void _bt_event_set_is_frozen(struct bt_event
*event
, bool is_frozen
)
726 bt_event_common_set_is_frozen(BT_TO_COMMON(event
), is_frozen
);
727 BT_LOGD_STR("Freezing event's packet.");
728 bt_packet_set_is_frozen(event
->packet
, is_frozen
);
731 int bt_event_move_header(struct bt_event
*event
,
732 struct bt_event_header_field
*header_field
)
734 struct bt_stream_class
*stream_class
;
735 struct bt_field_wrapper
*field_wrapper
= (void *) header_field
;
737 BT_ASSERT_PRE_NON_NULL(event
, "Event");
738 BT_ASSERT_PRE_NON_NULL(field_wrapper
, "Header field");
739 BT_ASSERT_PRE_HOT(BT_TO_COMMON(event
), "Event", ": %!+e", event
);
740 stream_class
= bt_event_class_borrow_stream_class(
741 bt_event_borrow_class(event
));
742 BT_ASSERT_PRE(stream_class
->common
.event_header_field_type
,
743 "Stream class has no event header field type: %!+S",
746 /* Recycle current header field: always exists */
747 BT_ASSERT(event
->common
.header_field
);
748 bt_event_header_field_recycle(event
->common
.header_field
,
752 event
->common
.header_field
= (void *) field_wrapper
;