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.");
88 bt_trace_common_borrow_packet_header_field_type(trace
);
89 trace_valid
= trace
->valid
;
90 BT_ASSERT(trace_valid
);
91 environment
= trace
->environment
;
95 bt_stream_class_common_borrow_packet_context_field_type(
98 bt_stream_class_common_borrow_event_header_field_type(
100 stream_event_ctx_type
=
101 bt_stream_class_common_borrow_event_context_field_type(
104 bt_event_class_common_borrow_context_field_type(event_class
);
106 bt_event_class_common_borrow_payload_field_type(event_class
);
107 ret
= bt_validate_class_types(environment
, packet_header_type
,
108 packet_context_type
, event_header_type
, stream_event_ctx_type
,
109 event_context_type
, event_payload_type
, trace_valid
,
110 stream_class
->valid
, event_class
->valid
,
111 validation_output
, validation_flags
, copy_field_type_func
);
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
);
140 int bt_event_common_create_fields(
141 struct bt_validation_output
*validation_output
,
142 void *(*create_field_func
)(void *),
143 struct bt_field_common
**header_field
,
144 struct bt_field_common
**stream_event_context_field
,
145 struct bt_field_common
**context_field
,
146 struct bt_field_common
**payload_field
)
150 if (validation_output
->event_header_type
) {
151 BT_LOGD("Creating initial event header field: ft-addr=%p",
152 validation_output
->event_header_type
);
154 create_field_func(validation_output
->event_header_type
);
155 if (!*header_field
) {
156 BT_LOGE_STR("Cannot create initial event header field object.");
161 if (validation_output
->stream_event_ctx_type
) {
162 BT_LOGD("Creating initial stream event context field: ft-addr=%p",
163 validation_output
->stream_event_ctx_type
);
164 *stream_event_context_field
= create_field_func(
165 validation_output
->stream_event_ctx_type
);
166 if (!*stream_event_context_field
) {
167 BT_LOGE_STR("Cannot create initial stream event context field object.");
172 if (validation_output
->event_context_type
) {
173 BT_LOGD("Creating initial event context field: ft-addr=%p",
174 validation_output
->event_context_type
);
175 *context_field
= create_field_func(
176 validation_output
->event_context_type
);
177 if (!*context_field
) {
178 BT_LOGE_STR("Cannot create initial event context field object.");
183 if (validation_output
->event_payload_type
) {
184 BT_LOGD("Creating initial event payload field: ft-addr=%p",
185 validation_output
->event_payload_type
);
186 *payload_field
= create_field_func(
187 validation_output
->event_payload_type
);
188 if (!*payload_field
) {
189 BT_LOGE_STR("Cannot create initial event payload field object.");
197 BT_PUT(*header_field
);
198 BT_PUT(*stream_event_context_field
);
199 BT_PUT(*context_field
);
200 BT_PUT(*payload_field
);
208 int _bt_event_common_validate(struct bt_event_common
*event
)
211 struct bt_stream_class_common
*stream_class
;
214 if (event
->header_field
) {
215 ret
= bt_field_common_validate_recursive(event
->header_field
);
217 BT_ASSERT_PRE_MSG("Invalid event's header field: "
218 "%![event-]+_e, %![field-]+_f",
219 event
, event
->header_field
);
224 stream_class
= bt_event_class_common_borrow_stream_class(event
->class);
227 * We should not have been able to create the event without associating
228 * the event class to a stream class.
230 BT_ASSERT(stream_class
);
232 if (stream_class
->event_context_field_type
) {
233 ret
= bt_field_common_validate_recursive(
234 event
->stream_event_context_field
);
236 BT_ASSERT_PRE_MSG("Invalid event's stream event context field: "
237 "%![event-]+_e, %![field-]+_f",
238 event
, event
->stream_event_context_field
);
243 if (event
->class->context_field_type
) {
244 ret
= bt_field_common_validate_recursive(event
->context_field
);
246 BT_ASSERT_PRE_MSG("Invalid event's payload field: "
247 "%![event-]+_e, %![field-]+_f",
248 event
, event
->context_field
);
253 ret
= bt_field_common_validate_recursive(event
->payload_field
);
255 BT_ASSERT_PRE_MSG("Invalid event's payload field: "
256 "%![event-]+_e, %![field-]+_f",
257 event
, event
->payload_field
);
266 void _bt_event_common_freeze(struct bt_event_common
*event
)
274 BT_LOGD("Freezing event: addr=%p, "
275 "event-class-name=\"%s\", event-class-id=%" PRId64
,
276 event
, bt_event_class_common_get_name(event
->class),
277 bt_event_class_common_get_id(event
->class));
278 BT_LOGD_STR("Freezing event's header field.");
279 bt_field_common_freeze_recursive(event
->header_field
);
280 BT_LOGD_STR("Freezing event's stream event context field.");
281 bt_field_common_freeze_recursive(event
->stream_event_context_field
);
282 BT_LOGD_STR("Freezing event's context field.");
283 bt_field_common_freeze_recursive(event
->context_field
);
284 BT_LOGD_STR("Freezing event's payload field.");
285 bt_field_common_freeze_recursive(event
->payload_field
);
290 int bt_event_common_initialize(struct bt_event_common
*event
,
291 struct bt_event_class_common
*event_class
,
292 struct bt_clock_class
*init_expected_clock_class
,
293 bt_object_release_func release_func
,
294 bt_validation_flag_copy_field_type_func field_type_copy_func
,
295 bool must_be_in_trace
,
296 int (*map_clock_classes_func
)(struct bt_stream_class_common
*stream_class
,
297 struct bt_field_type_common
*packet_context_field_type
,
298 struct bt_field_type_common
*event_header_field_type
),
299 void *(*create_field_func
)(void *))
302 struct bt_trace_common
*trace
= NULL
;
303 struct bt_stream_class_common
*stream_class
= NULL
;
304 struct bt_field_common
*event_header
= NULL
;
305 struct bt_field_common
*stream_event_context
= NULL
;
306 struct bt_field_common
*event_context
= NULL
;
307 struct bt_field_common
*event_payload
= NULL
;
308 struct bt_validation_output validation_output
= { 0 };
309 struct bt_clock_class
*expected_clock_class
=
310 init_expected_clock_class
? bt_get(init_expected_clock_class
) :
313 BT_ASSERT_PRE_NON_NULL(event_class
, "Event class");
314 BT_LOGD("Initializing common event object: event-class-addr=%p, "
315 "event-class-name=\"%s\", event-class-id=%" PRId64
,
316 event_class
, bt_event_class_common_get_name(event_class
),
317 bt_event_class_common_get_id(event_class
));
319 stream_class
= bt_event_class_common_borrow_stream_class(event_class
);
320 BT_ASSERT_PRE(stream_class
,
321 "Event class is not part of a stream class: %!+_E", event_class
);
323 /* The event class was frozen when added to its stream class */
324 BT_ASSERT(event_class
->frozen
);
325 trace
= bt_stream_class_common_borrow_trace(stream_class
);
327 if (must_be_in_trace
) {
329 "Event class's stream class is not part of a trace: "
330 "%![ec-]+_E, %![ec-]+_S", event_class
, stream_class
);
334 * This must be called before anything that can fail because on
335 * failure, the caller releases the reference to `event` to
338 bt_object_init(event
, release_func
);
340 if (!stream_class
->frozen
) {
342 * Because this function freezes the stream class,
343 * validate that this stream class contains at most a
344 * single clock class so that we set its expected clock
345 * class for future checks.
347 ret
= bt_stream_class_common_validate_single_clock_class(
348 stream_class
, &expected_clock_class
);
350 BT_LOGW("Event class's stream class or one of its event "
351 "classes contains a field type which is not "
352 "recursively mapped to the expected "
354 "stream-class-addr=%p, "
355 "stream-class-id=%" PRId64
", "
356 "stream-class-name=\"%s\", "
357 "expected-clock-class-addr=%p, "
358 "expected-clock-class-name=\"%s\"",
360 bt_stream_class_common_get_id(stream_class
),
361 bt_stream_class_common_get_name(stream_class
),
362 expected_clock_class
,
363 expected_clock_class
?
364 bt_clock_class_get_name(expected_clock_class
) :
370 /* Validate the trace, the stream class, and the event class */
371 ret
= bt_event_common_validate_types_for_create(
372 event_class
, &validation_output
, field_type_copy_func
);
374 /* bt_event_common_validate_types_for_create() logs errors */
378 if (map_clock_classes_func
) {
380 * Safe to automatically map selected fields to the
381 * stream's clock's class here because the stream class
382 * is about to be frozen.
384 if (map_clock_classes_func(stream_class
,
385 validation_output
.packet_context_type
,
386 validation_output
.event_header_type
)) {
387 BT_LOGW_STR("Cannot automatically map selected stream class's "
388 "field types to stream class's clock's class.");
394 * event does not share a common ancestor with the event class; it has
395 * to guarantee its existence by holding a reference. This reference
396 * shall be released once the event is associated to a stream since,
397 * from that point, the event and its class will share the same
400 event
->class = bt_get(event_class
);
402 ret
= bt_event_common_create_fields(&validation_output
,
403 create_field_func
, &event_header
,
404 &stream_event_context
, &event_context
, &event_payload
);
406 /* bt_event_common_create_fields() logs errors */
411 * At this point all the fields are created, potentially from
412 * validated copies of field types, so that the field types and
413 * fields can be replaced in the trace, stream class,
414 * event class, and created event.
416 bt_validation_replace_types(trace
, stream_class
, event_class
,
418 BT_VALIDATION_FLAG_STREAM
| BT_VALIDATION_FLAG_EVENT
);
419 BT_MOVE(event
->header_field
, event_header
);
420 BT_MOVE(event
->stream_event_context_field
, stream_event_context
);
421 BT_MOVE(event
->context_field
, event_context
);
422 BT_MOVE(event
->payload_field
, event_payload
);
425 * Put what was not moved in bt_validation_replace_types().
427 bt_validation_output_put_types(&validation_output
);
430 * Freeze the stream class since the event header must not be changed
433 bt_stream_class_common_freeze(stream_class
);
436 * It is safe to set the stream class's unique clock class
437 * now because the stream class is frozen.
439 if (expected_clock_class
) {
440 BT_MOVE(stream_class
->clock_class
, expected_clock_class
);
444 * Mark stream class, and event class as valid since
445 * they're all frozen now.
447 stream_class
->valid
= 1;
448 event_class
->valid
= 1;
450 /* Put stuff we borrowed from the event class */
451 BT_LOGD("Initialized event object: addr=%p, event-class-name=\"%s\", "
452 "event-class-id=%" PRId64
,
453 event
, bt_event_class_common_get_name(event
->class),
454 bt_event_class_common_get_id(event
->class));
458 bt_validation_output_put_types(&validation_output
);
459 bt_put(expected_clock_class
);
460 bt_put(event_header
);
461 bt_put(stream_event_context
);
462 bt_put(event_context
);
463 bt_put(event_payload
);
470 struct bt_event
*bt_event_create(struct bt_event_class
*event_class
)
473 struct bt_event
*event
= NULL
;
475 event
= g_new0(struct bt_event
, 1);
477 BT_LOGE_STR("Failed to allocate one event.");
481 ret
= bt_event_common_initialize(BT_TO_COMMON(event
),
482 BT_TO_COMMON(event_class
), NULL
, bt_event_destroy
,
483 (bt_validation_flag_copy_field_type_func
) bt_field_type_copy
,
484 true, NULL
, (void *) bt_field_create
);
486 /* bt_event_common_initialize() logs errors */
490 event
->clock_values
= g_hash_table_new_full(g_direct_hash
,
491 g_direct_equal
, NULL
, bt_put
);
492 assert(event
->clock_values
);
502 struct bt_event_class
*bt_event_borrow_class(struct bt_event
*event
)
504 BT_ASSERT_PRE_NON_NULL(event
, "Event");
505 return BT_FROM_COMMON(
506 bt_event_common_borrow_class(BT_TO_COMMON(event
)));
509 struct bt_stream
*bt_event_borrow_stream(struct bt_event
*event
)
511 BT_ASSERT_PRE_NON_NULL(event
, "Event");
512 return event
->packet
? event
->packet
->stream
: NULL
;
515 struct bt_field
*bt_event_borrow_payload(struct bt_event
*event
)
517 return BT_FROM_COMMON(
518 bt_event_common_borrow_payload(BT_TO_COMMON(event
)));
521 int bt_event_set_payload(struct bt_event
*event
, struct bt_field
*field
)
523 return bt_event_common_set_payload(BT_TO_COMMON(event
),
527 struct bt_field
*bt_event_borrow_header(struct bt_event
*event
)
529 return BT_FROM_COMMON(
530 bt_event_common_borrow_header(BT_TO_COMMON(event
)));
533 int bt_event_set_header(struct bt_event
*event
, struct bt_field
*field
)
535 return bt_event_common_set_header(BT_TO_COMMON(event
), (void *) field
);
538 struct bt_field
*bt_event_borrow_context(struct bt_event
*event
)
540 return BT_FROM_COMMON(
541 bt_event_common_borrow_context(BT_TO_COMMON(event
)));
544 int bt_event_set_context(struct bt_event
*event
, struct bt_field
*field
)
546 return bt_event_common_set_context(BT_TO_COMMON(event
), (void *) field
);
549 struct bt_field
*bt_event_borrow_stream_event_context(
550 struct bt_event
*event
)
552 return BT_FROM_COMMON(bt_event_common_borrow_stream_event_context(
553 BT_TO_COMMON(event
)));
556 int bt_event_set_stream_event_context(struct bt_event
*event
,
557 struct bt_field
*field
)
559 return bt_event_common_set_stream_event_context(
560 BT_TO_COMMON(event
), (void *) field
);
563 void bt_event_destroy(struct bt_object
*obj
)
565 struct bt_event
*event
= (void *) obj
;
567 bt_event_common_finalize(obj
);
568 g_hash_table_destroy(event
->clock_values
);
569 BT_LOGD_STR("Putting event's packet.");
570 bt_put(event
->packet
);
574 struct bt_clock_value
*bt_event_borrow_clock_value(
575 struct bt_event
*event
, struct bt_clock_class
*clock_class
)
577 struct bt_clock_value
*clock_value
= NULL
;
579 BT_ASSERT_PRE_NON_NULL(event
, "Event");
580 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
581 clock_value
= g_hash_table_lookup(event
->clock_values
, clock_class
);
583 BT_LOGV("No clock value associated to the given clock class: "
584 "event-addr=%p, event-class-name=\"%s\", "
585 "event-class-id=%" PRId64
", clock-class-addr=%p, "
586 "clock-class-name=\"%s\"", event
,
587 bt_event_class_common_get_name(event
->common
.class),
588 bt_event_class_common_get_id(event
->common
.class),
589 clock_class
, bt_clock_class_get_name(clock_class
));
597 int bt_event_set_clock_value(struct bt_event
*event
,
598 struct bt_clock_value
*value
)
600 struct bt_trace
*trace
;
601 struct bt_stream_class
*stream_class
;
602 struct bt_event_class
*event_class
;
603 struct bt_clock_class
*clock_class
= NULL
;
605 BT_ASSERT_PRE_NON_NULL(event
, "Event");
606 BT_ASSERT_PRE_NON_NULL(value
, "Clock value");
607 BT_ASSERT_PRE_EVENT_COMMON_HOT(BT_TO_COMMON(event
), "Event");
608 clock_class
= bt_clock_value_borrow_class(value
);
609 event_class
= BT_FROM_COMMON(event
->common
.class);
610 BT_ASSERT(event_class
);
611 stream_class
= bt_event_class_borrow_stream_class(event_class
);
612 BT_ASSERT(stream_class
);
613 trace
= bt_stream_class_borrow_trace(stream_class
);
615 BT_ASSERT_PRE(bt_trace_common_has_clock_class(BT_TO_COMMON(trace
),
617 "Clock class is not part of event's trace: "
618 "%![event-]+e, %![clock-class-]+K",
620 g_hash_table_insert(event
->clock_values
, clock_class
, bt_get(value
));
621 BT_LOGV("Set event's clock value: "
622 "event-addr=%p, event-class-name=\"%s\", "
623 "event-class-id=%" PRId64
", clock-class-addr=%p, "
624 "clock-class-name=\"%s\", clock-value-addr=%p, "
625 "clock-value-cycles=%" PRIu64
,
626 event
, bt_event_class_common_get_name(event
->common
.class),
627 bt_event_class_common_get_id(event
->common
.class),
628 clock_class
, bt_clock_class_get_name(clock_class
),
629 value
, value
->value
);
633 struct bt_packet
*bt_event_borrow_packet(struct bt_event
*event
)
635 struct bt_packet
*packet
= NULL
;
637 BT_ASSERT_PRE_NON_NULL(event
, "Event");
638 if (!event
->packet
) {
639 BT_LOGV("Event has no current packet: addr=%p, "
640 "event-class-name=\"%s\", event-class-id=%" PRId64
,
641 event
, bt_event_class_common_get_name(event
->common
.class),
642 bt_event_class_common_get_id(event
->common
.class));
646 packet
= event
->packet
;
652 int bt_event_set_packet(struct bt_event
*event
,
653 struct bt_packet
*packet
)
655 BT_ASSERT_PRE_NON_NULL(event
, "Event");
656 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
657 BT_ASSERT_PRE_EVENT_COMMON_HOT(BT_TO_COMMON(event
), "Event");
660 * Make sure the new packet was created by this event's
661 * stream, if it is set.
663 if (bt_event_borrow_stream(event
)) {
664 BT_ASSERT_PRE(packet
->stream
== bt_event_borrow_stream(event
),
665 "Packet's stream and event's stream differ: "
666 "%![event-]+e, %![packet-]+a",
669 BT_ASSERT_PRE(bt_event_class_borrow_stream_class(
670 BT_FROM_COMMON(event
->common
.class)) ==
671 BT_FROM_COMMON(packet
->stream
->common
.stream_class
),
672 "Packet's stream class and event's stream class differ: "
673 "%![event-]+e, %![packet-]+a",
678 BT_MOVE(event
->packet
, packet
);
679 BT_LOGV("Set event's packet: event-addr=%p, "
680 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
682 event
, bt_event_class_common_get_name(event
->common
.class),
683 bt_event_class_common_get_id(event
->common
.class), packet
);
688 void _bt_event_freeze(struct bt_event
*event
)
690 _bt_event_common_freeze(BT_TO_COMMON(event
));
691 BT_LOGD_STR("Freezing event's packet.");
692 bt_packet_freeze(event
->packet
);