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 void bt_event_destroy(struct bt_object
*obj
);
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
)
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
;
80 struct bt_value
*environment
= NULL
;
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
);
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
;
93 packet_context_type
= bt_stream_class_common_get_packet_context_field_type(
95 event_header_type
= bt_stream_class_common_get_event_header_field_type(
97 stream_event_ctx_type
= bt_stream_class_common_get_event_context_field_type(
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
);
114 * This means something went wrong during the validation
115 * process, not that the objects are invalid.
117 BT_LOGE("Failed to validate event and parents: ret=%d", ret
);
121 if ((validation_output
->valid_flags
& 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
);
132 bt_validation_output_put_types(validation_output
);
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
);
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
)
156 if (validation_output
->event_header_type
) {
157 BT_LOGD("Creating initial event header field: ft-addr=%p",
158 validation_output
->event_header_type
);
160 create_field_func(validation_output
->event_header_type
);
161 if (!*header_field
) {
162 BT_LOGE_STR("Cannot create initial event header field object.");
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.");
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.");
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.");
203 BT_PUT(*header_field
);
204 BT_PUT(*stream_event_context_field
);
205 BT_PUT(*context_field
);
206 BT_PUT(*payload_field
);
214 int _bt_event_common_validate(struct bt_event_common
*event
)
217 struct bt_stream_class_common
*stream_class
;
220 if (event
->header_field
) {
221 ret
= bt_field_common_validate_recursive(event
->header_field
);
223 BT_ASSERT_PRE_MSG("Invalid event's header field: "
224 "%![event-]+_e, %![field-]+_f",
225 event
, event
->header_field
);
230 stream_class
= bt_event_class_common_borrow_stream_class(event
->class);
233 * We should not have been able to create the event without associating
234 * the event class to a stream class.
236 BT_ASSERT(stream_class
);
238 if (stream_class
->event_context_field_type
) {
239 ret
= bt_field_common_validate_recursive(
240 event
->stream_event_context_field
);
242 BT_ASSERT_PRE_MSG("Invalid event's stream event context field: "
243 "%![event-]+_e, %![field-]+_f",
244 event
, event
->stream_event_context_field
);
249 if (event
->class->context_field_type
) {
250 ret
= bt_field_common_validate_recursive(event
->context_field
);
252 BT_ASSERT_PRE_MSG("Invalid event's payload field: "
253 "%![event-]+_e, %![field-]+_f",
254 event
, event
->context_field
);
259 ret
= bt_field_common_validate_recursive(event
->payload_field
);
261 BT_ASSERT_PRE_MSG("Invalid event's payload field: "
262 "%![event-]+_e, %![field-]+_f",
263 event
, event
->payload_field
);
272 void _bt_event_common_freeze(struct bt_event_common
*event
)
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
);
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 *))
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
) :
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
));
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
);
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
);
333 if (must_be_in_trace
) {
335 "Event class's stream class is not part of a trace: "
336 "%![ec-]+_E, %![ec-]+_S", event_class
, stream_class
);
340 * This must be called before anything that can fail because on
341 * failure, the caller releases the reference to `event` to
344 bt_object_init(event
, release_func
);
346 if (!stream_class
->frozen
) {
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.
353 ret
= bt_stream_class_common_validate_single_clock_class(
354 stream_class
, &expected_clock_class
);
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 "
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\"",
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
) :
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
);
380 /* bt_event_common_validate_types_for_create() logs errors */
384 if (map_clock_classes_func
) {
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.
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.");
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
406 event
->class = bt_get(event_class
);
408 ret
= bt_event_common_create_fields(&validation_output
,
409 create_field_func
, &event_header
,
410 &stream_event_context
, &event_context
, &event_payload
);
412 /* bt_event_common_create_fields() logs errors */
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.
422 bt_validation_replace_types(trace
, stream_class
, event_class
,
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
);
431 * Put what was not moved in bt_validation_replace_types().
433 bt_validation_output_put_types(&validation_output
);
436 * Freeze the stream class since the event header must not be changed
439 bt_stream_class_common_freeze(stream_class
);
442 * It is safe to set the stream class's unique clock class
443 * now because the stream class is frozen.
445 if (expected_clock_class
) {
446 BT_MOVE(stream_class
->clock_class
, expected_clock_class
);
450 * Mark stream class, and event class as valid since
451 * they're all frozen now.
453 stream_class
->valid
= 1;
454 event_class
->valid
= 1;
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));
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
);
476 struct bt_event
*bt_event_create(struct bt_event_class
*event_class
)
479 struct bt_event
*event
= NULL
;
481 event
= g_new0(struct bt_event
, 1);
483 BT_LOGE_STR("Failed to allocate one event.");
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
);
492 /* bt_event_common_initialize() logs errors */
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
);
508 struct bt_event_class
*bt_event_get_class(struct bt_event
*event
)
510 BT_ASSERT_PRE_NON_NULL(event
, "Event");
511 return bt_get(bt_event_common_borrow_class(BT_TO_COMMON(event
)));
515 struct bt_stream
*bt_event_borrow_stream(struct bt_event
*event
)
517 struct bt_stream
*stream
= NULL
;
522 stream
= event
->packet
->stream
;
528 struct bt_stream
*bt_event_get_stream(struct bt_event
*event
)
530 BT_ASSERT_PRE_NON_NULL(event
, "Event");
531 return bt_get(bt_event_borrow_stream(event
));
534 struct bt_field
*bt_event_get_payload(struct bt_event
*event
)
536 return BT_FROM_COMMON(bt_event_common_get_payload(BT_TO_COMMON(event
)));
539 int bt_event_set_payload(struct bt_event
*event
, struct bt_field
*field
)
541 return bt_event_common_set_payload(BT_TO_COMMON(event
),
545 struct bt_field
*bt_event_get_header(struct bt_event
*event
)
547 return BT_FROM_COMMON(bt_event_common_get_header(BT_TO_COMMON(event
)));
550 int bt_event_set_header(struct bt_event
*event
, struct bt_field
*field
)
552 return bt_event_common_set_header(BT_TO_COMMON(event
), (void *) field
);
555 struct bt_field
*bt_event_get_context(struct bt_event
*event
)
557 return BT_FROM_COMMON(bt_event_common_get_context(BT_TO_COMMON(event
)));
560 int bt_event_set_context(struct bt_event
*event
, struct bt_field
*field
)
562 return bt_event_common_set_context(BT_TO_COMMON(event
), (void *) field
);
565 struct bt_field
*bt_event_get_stream_event_context(
566 struct bt_event
*event
)
568 return BT_FROM_COMMON(bt_event_common_get_stream_event_context(
569 BT_TO_COMMON(event
)));
572 int bt_event_set_stream_event_context(struct bt_event
*event
,
573 struct bt_field
*field
)
575 return bt_event_common_set_stream_event_context(
576 BT_TO_COMMON(event
), (void *) field
);
579 void bt_event_destroy(struct bt_object
*obj
)
581 struct bt_event
*event
= (void *) obj
;
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
);
590 struct bt_clock_value
*bt_event_get_clock_value(
591 struct bt_event
*event
, struct bt_clock_class
*clock_class
)
593 struct bt_clock_value
*clock_value
= NULL
;
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
);
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
));
615 int bt_event_set_clock_value(struct bt_event
*event
,
616 struct bt_clock_value
*value
)
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
;
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
);
633 BT_ASSERT_PRE(bt_trace_common_has_clock_class(BT_TO_COMMON(trace
),
635 "Clock class is not part of event's trace: "
636 "%![event-]+e, %![clock-class-]+K",
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
);
653 struct bt_packet
*bt_event_get_packet(struct bt_event
*event
)
655 struct bt_packet
*packet
= NULL
;
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));
666 packet
= bt_get(event
->packet
);
672 int bt_event_set_packet(struct bt_event
*event
,
673 struct bt_packet
*packet
)
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");
680 * Make sure the new packet was created by this event's
681 * stream, if it is set.
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",
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",
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
", "
702 event
, bt_event_class_common_get_name(event
->common
.class),
703 bt_event_class_common_get_id(event
->common
.class), packet
);
708 void _bt_event_freeze(struct bt_event
*event
)
710 _bt_event_common_freeze(BT_TO_COMMON(event
));
711 BT_LOGD_STR("Freezing event's packet.");
712 bt_packet_freeze(event
->packet
);