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 #include <babeltrace/ctf-ir/fields-internal.h>
30 #include <babeltrace/ctf-ir/field-types-internal.h>
31 #include <babeltrace/ctf-ir/event-internal.h>
32 #include <babeltrace/ctf-ir/event-class.h>
33 #include <babeltrace/ctf-ir/event-class-internal.h>
34 #include <babeltrace/ctf-ir/stream-class.h>
35 #include <babeltrace/ctf-ir/stream-class-internal.h>
36 #include <babeltrace/ctf-ir/stream-internal.h>
37 #include <babeltrace/ctf-ir/packet.h>
38 #include <babeltrace/ctf-ir/packet-internal.h>
39 #include <babeltrace/ctf-ir/trace-internal.h>
40 #include <babeltrace/ctf-ir/validation-internal.h>
41 #include <babeltrace/ctf-ir/packet-internal.h>
42 #include <babeltrace/ctf-ir/utils.h>
43 #include <babeltrace/ref.h>
44 #include <babeltrace/ctf-ir/attributes-internal.h>
45 #include <babeltrace/compiler.h>
48 void bt_ctf_event_destroy(struct bt_object
*obj
);
50 int set_integer_field_value(struct bt_ctf_field
*field
, uint64_t value
);
52 struct bt_ctf_event
*bt_ctf_event_create(struct bt_ctf_event_class
*event_class
)
55 enum bt_ctf_validation_flag validation_flags
=
56 BT_CTF_VALIDATION_FLAG_STREAM
|
57 BT_CTF_VALIDATION_FLAG_EVENT
;
58 struct bt_ctf_event
*event
= NULL
;
59 struct bt_ctf_trace
*trace
= NULL
;
60 struct bt_ctf_stream_class
*stream_class
= NULL
;
61 struct bt_ctf_field_type
*packet_header_type
= NULL
;
62 struct bt_ctf_field_type
*packet_context_type
= NULL
;
63 struct bt_ctf_field_type
*event_header_type
= NULL
;
64 struct bt_ctf_field_type
*stream_event_ctx_type
= NULL
;
65 struct bt_ctf_field_type
*event_context_type
= NULL
;
66 struct bt_ctf_field_type
*event_payload_type
= NULL
;
67 struct bt_ctf_field
*event_header
= NULL
;
68 struct bt_ctf_field
*stream_event_context
= NULL
;
69 struct bt_ctf_field
*event_context
= NULL
;
70 struct bt_ctf_field
*event_payload
= NULL
;
71 struct bt_value
*environment
= NULL
;
72 struct bt_ctf_validation_output validation_output
= { 0 };
79 stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
82 * We disallow the creation of an event if its event class has not been
83 * associated to a stream class.
89 /* A stream class should always have an existing event header type */
90 assert(stream_class
->event_header_type
);
92 /* The event class was frozen when added to its stream class */
93 assert(event_class
->frozen
);
95 /* Validate the trace (if any), the stream class, and the event class */
96 trace
= bt_ctf_stream_class_get_trace(stream_class
);
98 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
99 trace_valid
= trace
->valid
;
101 environment
= trace
->environment
;
104 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(
106 event_header_type
= bt_ctf_stream_class_get_event_header_type(
108 stream_event_ctx_type
= bt_ctf_stream_class_get_event_context_type(
110 event_context_type
= bt_ctf_event_class_get_context_type(event_class
);
111 event_payload_type
= bt_ctf_event_class_get_payload_type(event_class
);
112 ret
= bt_ctf_validate_class_types(environment
, packet_header_type
,
113 packet_context_type
, event_header_type
, stream_event_ctx_type
,
114 event_context_type
, event_payload_type
, trace_valid
,
115 stream_class
->valid
, event_class
->valid
,
116 &validation_output
, validation_flags
);
117 BT_PUT(packet_header_type
);
118 BT_PUT(packet_context_type
);
119 BT_PUT(event_header_type
);
120 BT_PUT(stream_event_ctx_type
);
121 BT_PUT(event_context_type
);
122 BT_PUT(event_payload_type
);
125 * This means something went wrong during the validation
126 * process, not that the objects are invalid.
131 if ((validation_output
.valid_flags
& validation_flags
) !=
133 /* Invalid trace/stream class/event class */
138 * At this point we know the trace (if associated to the stream
139 * class), the stream class, and the event class, with their
140 * current types, are valid. We may proceed with creating
143 event
= g_new0(struct bt_ctf_event
, 1);
148 bt_object_init(event
, bt_ctf_event_destroy
);
151 * event does not share a common ancestor with the event class; it has
152 * to guarantee its existence by holding a reference. This reference
153 * shall be released once the event is associated to a stream since,
154 * from that point, the event and its class will share the same
157 event
->event_class
= bt_get(event_class
);
158 event
->clock_values
= g_hash_table_new_full(g_direct_hash
,
159 g_direct_equal
, bt_put
, bt_put
);
161 bt_ctf_field_create(validation_output
.event_header_type
);
166 if (validation_output
.stream_event_ctx_type
) {
167 stream_event_context
= bt_ctf_field_create(
168 validation_output
.stream_event_ctx_type
);
169 if (!stream_event_context
) {
174 if (validation_output
.event_context_type
) {
175 event_context
= bt_ctf_field_create(
176 validation_output
.event_context_type
);
177 if (!event_context
) {
182 if (validation_output
.event_payload_type
) {
183 event_payload
= bt_ctf_field_create(
184 validation_output
.event_payload_type
);
185 if (!event_payload
) {
191 * At this point all the fields are created, potentially from
192 * validated copies of field types, so that the field types and
193 * fields can be replaced in the trace, stream class,
194 * event class, and created event.
196 bt_ctf_validation_replace_types(trace
, stream_class
,
197 event_class
, &validation_output
, validation_flags
);
198 BT_MOVE(event
->event_header
, event_header
);
199 BT_MOVE(event
->stream_event_context
, stream_event_context
);
200 BT_MOVE(event
->context_payload
, event_context
);
201 BT_MOVE(event
->fields_payload
, event_payload
);
204 * Put what was not moved in bt_ctf_validation_replace_types().
206 bt_ctf_validation_output_put_types(&validation_output
);
209 * Freeze the stream class since the event header must not be changed
212 bt_ctf_stream_class_freeze(stream_class
);
215 * Mark stream class, and event class as valid since
216 * they're all frozen now.
218 stream_class
->valid
= 1;
219 event_class
->valid
= 1;
221 /* Put stuff we borrowed from the event class */
222 BT_PUT(stream_class
);
228 bt_ctf_validation_output_put_types(&validation_output
);
230 BT_PUT(stream_class
);
232 BT_PUT(event_header
);
233 BT_PUT(stream_event_context
);
234 BT_PUT(event_context
);
235 BT_PUT(event_payload
);
236 assert(!packet_header_type
);
237 assert(!packet_context_type
);
238 assert(!event_header_type
);
239 assert(!stream_event_ctx_type
);
240 assert(!event_context_type
);
241 assert(!event_payload_type
);
246 struct bt_ctf_event_class
*bt_ctf_event_get_class(struct bt_ctf_event
*event
)
248 struct bt_ctf_event_class
*event_class
= NULL
;
254 event_class
= event
->event_class
;
260 struct bt_ctf_stream
*bt_ctf_event_get_stream(struct bt_ctf_event
*event
)
262 struct bt_ctf_stream
*stream
= NULL
;
269 * If the event has a parent, then this is its (writer) stream.
270 * If the event has no parent, then if it has a packet, this
271 * is its (non-writer) stream.
273 if (event
->base
.parent
) {
274 stream
= (struct bt_ctf_stream
*) bt_object_get_parent(event
);
277 stream
= bt_get(event
->packet
->stream
);
285 int bt_ctf_event_set_payload(struct bt_ctf_event
*event
,
287 struct bt_ctf_field
*payload
)
291 if (!event
|| !payload
|| event
->frozen
) {
297 ret
= bt_ctf_field_structure_set_field(event
->fields_payload
,
300 struct bt_ctf_field_type
*payload_type
;
302 payload_type
= bt_ctf_field_get_type(payload
);
304 if (bt_ctf_field_type_compare(payload_type
,
305 event
->event_class
->fields
) == 0) {
306 bt_put(event
->fields_payload
);
308 event
->fields_payload
= payload
;
313 bt_put(payload_type
);
319 struct bt_ctf_field
*bt_ctf_event_get_payload_field(struct bt_ctf_event
*event
)
321 struct bt_ctf_field
*payload
= NULL
;
323 if (!event
|| !event
->fields_payload
) {
327 payload
= event
->fields_payload
;
333 int bt_ctf_event_set_payload_field(struct bt_ctf_event
*event
,
334 struct bt_ctf_field
*payload
)
337 struct bt_ctf_field_type
*payload_type
= NULL
;
339 if (!event
|| !payload
|| event
->frozen
) {
344 payload_type
= bt_ctf_field_get_type(payload
);
350 if (bt_ctf_field_type_get_type_id(payload_type
) !=
351 BT_CTF_TYPE_ID_STRUCT
) {
357 bt_put(event
->fields_payload
);
358 event
->fields_payload
= payload
;
361 bt_put(payload_type
);
365 struct bt_ctf_field
*bt_ctf_event_get_payload(struct bt_ctf_event
*event
,
368 struct bt_ctf_field
*field
= NULL
;
375 field
= bt_ctf_field_structure_get_field(event
->fields_payload
,
378 field
= event
->fields_payload
;
385 struct bt_ctf_field
*bt_ctf_event_get_payload_by_index(
386 struct bt_ctf_event
*event
, int index
)
388 struct bt_ctf_field
*field
= NULL
;
390 if (!event
|| index
< 0) {
394 field
= bt_ctf_field_structure_get_field_by_index(event
->fields_payload
,
400 struct bt_ctf_field
*bt_ctf_event_get_header(
401 struct bt_ctf_event
*event
)
403 struct bt_ctf_field
*header
= NULL
;
405 if (!event
|| !event
->event_header
) {
409 header
= event
->event_header
;
415 int bt_ctf_event_set_header(struct bt_ctf_event
*event
,
416 struct bt_ctf_field
*header
)
419 struct bt_ctf_field_type
*field_type
= NULL
;
420 struct bt_ctf_stream_class
*stream_class
= NULL
;
422 if (!event
|| !header
|| event
->frozen
) {
427 stream_class
= (struct bt_ctf_stream_class
*) bt_object_get_parent(
430 * Ensure the provided header's type matches the one registered to the
433 field_type
= bt_ctf_field_get_type(header
);
434 if (bt_ctf_field_type_compare(field_type
,
435 stream_class
->event_header_type
)) {
441 bt_put(event
->event_header
);
442 event
->event_header
= header
;
444 bt_put(stream_class
);
449 struct bt_ctf_field
*bt_ctf_event_get_event_context(
450 struct bt_ctf_event
*event
)
452 struct bt_ctf_field
*context
= NULL
;
454 if (!event
|| !event
->context_payload
) {
458 context
= event
->context_payload
;
464 int bt_ctf_event_set_event_context(struct bt_ctf_event
*event
,
465 struct bt_ctf_field
*context
)
468 struct bt_ctf_field_type
*field_type
= NULL
;
470 if (!event
|| !context
|| event
->frozen
) {
475 field_type
= bt_ctf_field_get_type(context
);
476 if (bt_ctf_field_type_compare(field_type
,
477 event
->event_class
->context
)) {
483 bt_put(event
->context_payload
);
484 event
->context_payload
= context
;
490 struct bt_ctf_field
*bt_ctf_event_get_stream_event_context(
491 struct bt_ctf_event
*event
)
493 struct bt_ctf_field
*stream_event_context
= NULL
;
495 if (!event
|| !event
->stream_event_context
) {
499 stream_event_context
= event
->stream_event_context
;
501 return bt_get(stream_event_context
);
504 int bt_ctf_event_set_stream_event_context(struct bt_ctf_event
*event
,
505 struct bt_ctf_field
*stream_event_context
)
508 struct bt_ctf_field_type
*field_type
= NULL
;
509 struct bt_ctf_stream_class
*stream_class
= NULL
;
511 if (!event
|| !stream_event_context
|| event
->frozen
) {
516 stream_class
= bt_ctf_event_class_get_stream_class(event
->event_class
);
518 * We should not have been able to create the event without associating
519 * the event class to a stream class.
521 assert(stream_class
);
523 field_type
= bt_ctf_field_get_type(stream_event_context
);
524 if (bt_ctf_field_type_compare(field_type
,
525 stream_class
->event_context_type
)) {
530 bt_get(stream_event_context
);
531 BT_MOVE(event
->stream_event_context
, stream_event_context
);
533 BT_PUT(stream_class
);
538 void bt_ctf_event_get(struct bt_ctf_event
*event
)
543 void bt_ctf_event_put(struct bt_ctf_event
*event
)
548 void bt_ctf_event_destroy(struct bt_object
*obj
)
550 struct bt_ctf_event
*event
;
552 event
= container_of(obj
, struct bt_ctf_event
, base
);
553 if (!event
->base
.parent
) {
555 * Event was keeping a reference to its class since it shared no
556 * common ancestor with it to guarantee they would both have the
559 bt_put(event
->event_class
);
561 g_hash_table_destroy(event
->clock_values
);
562 bt_put(event
->event_header
);
563 bt_put(event
->stream_event_context
);
564 bt_put(event
->context_payload
);
565 bt_put(event
->fields_payload
);
566 bt_put(event
->packet
);
570 struct bt_ctf_clock_value
*bt_ctf_event_get_clock_value(
571 struct bt_ctf_event
*event
, struct bt_ctf_clock
*clock
)
573 struct bt_ctf_clock_value
*clock_value
= NULL
;
575 if (!event
|| !clock
) {
579 clock_value
= g_hash_table_lookup(event
->clock_values
, clock
);
589 int bt_ctf_event_set_clock_value(struct bt_ctf_event
*event
,
590 struct bt_ctf_clock
*clock
, struct bt_ctf_clock_value
*value
)
594 if (!event
|| !clock
|| !value
|| event
->frozen
) {
599 g_hash_table_insert(event
->clock_values
, bt_get(clock
), bt_get(value
));
605 int set_integer_field_value(struct bt_ctf_field
* field
, uint64_t value
)
608 struct bt_ctf_field_type
*field_type
= NULL
;
615 if (!bt_ctf_field_validate(field
)) {
616 /* Payload already set, skip! (not an error) */
620 field_type
= bt_ctf_field_get_type(field
);
623 if (bt_ctf_field_type_get_type_id(field_type
) !=
624 BT_CTF_TYPE_ID_INTEGER
) {
625 /* Not an integer and the value is unset, error. */
630 if (bt_ctf_field_type_integer_get_signed(field_type
)) {
631 ret
= bt_ctf_field_signed_integer_set_value(field
, (int64_t) value
);
633 /* Value is out of range, error. */
637 ret
= bt_ctf_field_unsigned_integer_set_value(field
, value
);
639 /* Value is out of range, error. */
649 int bt_ctf_event_validate(struct bt_ctf_event
*event
)
651 /* Make sure each field's payload has been set */
653 struct bt_ctf_stream_class
*stream_class
= NULL
;
656 ret
= bt_ctf_field_validate(event
->event_header
);
661 stream_class
= bt_ctf_event_class_get_stream_class(event
->event_class
);
663 * We should not have been able to create the event without associating
664 * the event class to a stream class.
666 assert(stream_class
);
667 if (stream_class
->event_context_type
) {
668 ret
= bt_ctf_field_validate(event
->stream_event_context
);
674 ret
= bt_ctf_field_validate(event
->fields_payload
);
679 if (event
->event_class
->context
) {
680 ret
= bt_ctf_field_validate(event
->context_payload
);
683 bt_put(stream_class
);
688 int bt_ctf_event_serialize(struct bt_ctf_event
*event
,
689 struct ctf_stream_pos
*pos
)
695 if (event
->context_payload
) {
696 ret
= bt_ctf_field_serialize(event
->context_payload
, pos
);
702 if (event
->fields_payload
) {
703 ret
= bt_ctf_field_serialize(event
->fields_payload
, pos
);
713 int bt_ctf_event_populate_event_header(struct bt_ctf_event
*event
)
716 struct bt_ctf_field
*id_field
= NULL
, *timestamp_field
= NULL
;
718 if (!event
|| event
->frozen
) {
723 id_field
= bt_ctf_field_structure_get_field(event
->event_header
, "id");
725 ret
= set_integer_field_value(id_field
,
726 (uint64_t) bt_ctf_event_class_get_id(
727 event
->event_class
));
733 timestamp_field
= bt_ctf_field_structure_get_field(event
->event_header
,
735 if (timestamp_field
) {
736 struct bt_ctf_field_type
*timestamp_field_type
=
737 bt_ctf_field_get_type(timestamp_field
);
738 struct bt_ctf_clock
*mapped_clock
;
740 assert(timestamp_field_type
);
741 mapped_clock
= bt_ctf_field_type_integer_get_mapped_clock(
742 timestamp_field_type
);
743 bt_put(timestamp_field_type
);
745 int64_t timestamp
= 0;
747 // FIXME - Clock refactoring
749 ret = bt_ctf_clock_get_time(mapped_clock, ×tamp);
750 bt_put(mapped_clock);
756 ret
= set_integer_field_value(timestamp_field
,
765 bt_put(timestamp_field
);
769 struct bt_ctf_packet
*bt_ctf_event_get_packet(struct bt_ctf_event
*event
)
771 struct bt_ctf_packet
*packet
= NULL
;
773 if (!event
|| !event
->packet
) {
777 packet
= bt_get(event
->packet
);
782 int bt_ctf_event_set_packet(struct bt_ctf_event
*event
,
783 struct bt_ctf_packet
*packet
)
785 struct bt_ctf_stream_class
*event_stream_class
= NULL
;
786 struct bt_ctf_stream_class
*packet_stream_class
= NULL
;
787 struct bt_ctf_stream
*stream
= NULL
;
790 if (!event
|| !packet
|| event
->frozen
) {
796 * Make sure the new packet was created by this event's
797 * stream, if it is set.
799 stream
= bt_ctf_event_get_stream(event
);
801 if (packet
->stream
!= stream
) {
807 bt_ctf_event_class_get_stream_class(event
->event_class
);
808 packet_stream_class
=
809 bt_ctf_stream_get_class(packet
->stream
);
811 assert(event_stream_class
);
812 assert(packet_stream_class
);
814 if (event_stream_class
!= packet_stream_class
) {
821 BT_MOVE(event
->packet
, packet
);
825 BT_PUT(event_stream_class
);
826 BT_PUT(packet_stream_class
);
832 void bt_ctf_event_freeze(struct bt_ctf_event
*event
)
835 bt_ctf_packet_freeze(event
->packet
);
836 bt_ctf_field_freeze(event
->event_header
);
837 bt_ctf_field_freeze(event
->stream_event_context
);
838 bt_ctf_field_freeze(event
->context_payload
);
839 bt_ctf_field_freeze(event
->fields_payload
);