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-writer/event.h>
30 #include <babeltrace/ctf-writer/event-types.h>
31 #include <babeltrace/ctf-writer/event-fields.h>
32 #include <babeltrace/ctf-ir/event-fields-internal.h>
33 #include <babeltrace/ctf-ir/event-types-internal.h>
34 #include <babeltrace/ctf-ir/event-internal.h>
35 #include <babeltrace/ctf-ir/stream-class.h>
36 #include <babeltrace/ctf-ir/stream-class-internal.h>
37 #include <babeltrace/ctf-ir/trace-internal.h>
38 #include <babeltrace/ctf-ir/utils.h>
39 #include <babeltrace/compiler.h>
42 void bt_ctf_event_class_destroy(struct bt_ctf_ref
*ref
);
44 void bt_ctf_event_destroy(struct bt_ctf_ref
*ref
);
46 int set_integer_field_value(struct bt_ctf_field
*field
, uint64_t value
);
48 struct bt_ctf_event_class
*bt_ctf_event_class_create(const char *name
)
50 struct bt_ctf_event_class
*event_class
= NULL
;
52 if (bt_ctf_validate_identifier(name
)) {
56 event_class
= g_new0(struct bt_ctf_event_class
, 1);
61 bt_ctf_ref_init(&event_class
->ref_count
);
62 event_class
->fields
= bt_ctf_field_type_structure_create();
63 if (!event_class
->fields
) {
64 bt_ctf_event_class_put(event_class
);
69 event_class
->name
= g_quark_from_string(name
);
74 const char *bt_ctf_event_class_get_name(struct bt_ctf_event_class
*event_class
)
76 const char *name
= NULL
;
82 name
= g_quark_to_string(event_class
->name
);
87 int64_t bt_ctf_event_class_get_id(struct bt_ctf_event_class
*event_class
)
91 if (!event_class
|| !event_class
->id_set
) {
96 ret
= (int64_t) event_class
->id
;
101 int bt_ctf_event_class_set_id(struct bt_ctf_event_class
*event_class
,
111 if (event_class
->stream_class
) {
113 * We don't allow changing the id if the event class has already
114 * been added to a stream class.
120 event_class
->id
= id
;
121 event_class
->id_set
= 1;
126 struct bt_ctf_stream_class
*bt_ctf_event_class_get_stream_class(
127 struct bt_ctf_event_class
*event_class
)
129 struct bt_ctf_stream_class
*stream_class
= NULL
;
135 stream_class
= event_class
->stream_class
;
136 bt_ctf_stream_class_get(stream_class
);
141 struct bt_ctf_field_type
*bt_ctf_event_class_get_payload_type(
142 struct bt_ctf_event_class
*event_class
)
144 struct bt_ctf_field_type
*payload
= NULL
;
150 bt_ctf_field_type_get(event_class
->fields
);
151 payload
= event_class
->fields
;
156 int bt_ctf_event_class_set_payload_type(struct bt_ctf_event_class
*event_class
,
157 struct bt_ctf_field_type
*payload
)
161 if (!event_class
|| !payload
) {
166 bt_ctf_field_type_get(payload
);
167 bt_ctf_field_type_put(event_class
->fields
);
168 event_class
->fields
= payload
;
173 int bt_ctf_event_class_add_field(struct bt_ctf_event_class
*event_class
,
174 struct bt_ctf_field_type
*type
,
179 if (!event_class
|| !type
|| bt_ctf_validate_identifier(name
) ||
180 event_class
->frozen
) {
185 if (bt_ctf_field_type_get_type_id(event_class
->fields
) !=
191 ret
= bt_ctf_field_type_structure_add_field(event_class
->fields
,
197 int bt_ctf_event_class_get_field_count(
198 struct bt_ctf_event_class
*event_class
)
207 if (bt_ctf_field_type_get_type_id(event_class
->fields
) !=
213 ret
= bt_ctf_field_type_structure_get_field_count(event_class
->fields
);
218 int bt_ctf_event_class_get_field(struct bt_ctf_event_class
*event_class
,
219 const char **field_name
, struct bt_ctf_field_type
**field_type
,
224 if (!event_class
|| index
< 0) {
229 if (bt_ctf_field_type_get_type_id(event_class
->fields
) !=
235 ret
= bt_ctf_field_type_structure_get_field(event_class
->fields
,
236 field_name
, field_type
, index
);
241 struct bt_ctf_field_type
*bt_ctf_event_class_get_field_by_name(
242 struct bt_ctf_event_class
*event_class
, const char *name
)
245 struct bt_ctf_field_type
*field_type
= NULL
;
247 if (!event_class
|| !name
) {
251 if (bt_ctf_field_type_get_type_id(event_class
->fields
) !=
256 name_quark
= g_quark_try_string(name
);
262 * No need to increment field_type's reference count since getting it
263 * from the structure already does.
265 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
266 event_class
->fields
, name
);
271 struct bt_ctf_field_type
*bt_ctf_event_class_get_context_type(
272 struct bt_ctf_event_class
*event_class
)
274 struct bt_ctf_field_type
*context_type
= NULL
;
276 if (!event_class
|| !event_class
->context
) {
280 bt_ctf_field_type_get(event_class
->context
);
281 context_type
= event_class
->context
;
286 int bt_ctf_event_class_set_context_type(
287 struct bt_ctf_event_class
*event_class
,
288 struct bt_ctf_field_type
*context
)
292 if (!event_class
|| !context
|| event_class
->frozen
) {
297 if (bt_ctf_field_type_get_type_id(context
) != CTF_TYPE_STRUCT
) {
302 bt_ctf_field_type_get(context
);
303 bt_ctf_field_type_put(event_class
->context
);
304 event_class
->context
= context
;
310 void bt_ctf_event_class_get(struct bt_ctf_event_class
*event_class
)
316 bt_ctf_ref_get(&event_class
->ref_count
);
319 void bt_ctf_event_class_put(struct bt_ctf_event_class
*event_class
)
325 bt_ctf_ref_put(&event_class
->ref_count
, bt_ctf_event_class_destroy
);
328 struct bt_ctf_event
*bt_ctf_event_create(struct bt_ctf_event_class
*event_class
)
330 struct bt_ctf_event
*event
= NULL
;
336 event
= g_new0(struct bt_ctf_event
, 1);
341 bt_ctf_ref_init(&event
->ref_count
);
342 bt_ctf_event_class_get(event_class
);
343 bt_ctf_event_class_freeze(event_class
);
344 event
->event_class
= event_class
;
347 * The event class does not keep ownership of the stream class to
348 * which it as been added. Therefore, it can't assume it has been
349 * set. However, we disallow the creation of an event if its
350 * associated stream class has been reclaimed.
352 if (!event_class
->stream_class
) {
355 assert(event_class
->stream_class
->event_header_type
);
357 event
->event_header
= bt_ctf_field_create(
358 event_class
->stream_class
->event_header_type
);
359 if (!event
->event_header
) {
362 if (event_class
->context
) {
363 event
->context_payload
= bt_ctf_field_create(
364 event_class
->context
);
365 if (!event
->context_payload
) {
369 event
->fields_payload
= bt_ctf_field_create(event_class
->fields
);
370 if (!event
->fields_payload
) {
375 * Freeze the stream class since the event header must not be changed
378 bt_ctf_stream_class_freeze(event_class
->stream_class
);
382 bt_ctf_event_destroy(&event
->ref_count
);
386 struct bt_ctf_event_class
*bt_ctf_event_get_class(struct bt_ctf_event
*event
)
388 struct bt_ctf_event_class
*event_class
= NULL
;
394 event_class
= event
->event_class
;
395 bt_ctf_event_class_get(event_class
);
400 struct bt_ctf_clock
*bt_ctf_event_get_clock(struct bt_ctf_event
*event
)
402 struct bt_ctf_clock
*clock
= NULL
;
403 struct bt_ctf_event_class
*event_class
;
404 struct bt_ctf_stream_class
*stream_class
;
410 event_class
= bt_ctf_event_get_class(event
);
415 stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
417 goto error_put_event_class
;
420 clock
= bt_ctf_stream_class_get_clock(stream_class
);
422 goto error_put_stream_class
;
425 error_put_stream_class
:
426 bt_ctf_stream_class_put(stream_class
);
427 error_put_event_class
:
428 bt_ctf_event_class_put(event_class
);
433 int bt_ctf_event_set_payload(struct bt_ctf_event
*event
,
435 struct bt_ctf_field
*payload
)
439 if (!event
|| !payload
) {
445 ret
= bt_ctf_field_structure_set_field(event
->fields_payload
,
448 struct bt_ctf_field_type
*payload_type
;
450 payload_type
= bt_ctf_field_get_type(payload
);
451 if (payload_type
== event
->event_class
->fields
) {
452 bt_ctf_field_put(event
->fields_payload
);
453 bt_ctf_field_get(payload
);
454 event
->fields_payload
= payload
;
459 bt_ctf_field_type_put(payload_type
);
466 struct bt_ctf_field
*bt_ctf_event_get_payload(struct bt_ctf_event
*event
,
469 struct bt_ctf_field
*field
= NULL
;
476 field
= bt_ctf_field_structure_get_field(event
->fields_payload
,
479 field
= event
->fields_payload
;
480 bt_ctf_field_get(field
);
486 struct bt_ctf_field
*bt_ctf_event_get_payload_by_index(
487 struct bt_ctf_event
*event
, int index
)
489 struct bt_ctf_field
*field
= NULL
;
491 if (!event
|| index
< 0) {
495 field
= bt_ctf_field_structure_get_field_by_index(event
->fields_payload
,
501 struct bt_ctf_field
*bt_ctf_event_get_event_header(
502 struct bt_ctf_event
*event
)
504 struct bt_ctf_field
*header
= NULL
;
506 if (!event
|| !event
->event_header
) {
510 header
= event
->event_header
;
511 bt_ctf_field_get(header
);
516 int bt_ctf_event_set_event_header(struct bt_ctf_event
*event
,
517 struct bt_ctf_field
*header
)
520 struct bt_ctf_field_type
*field_type
= NULL
;
522 if (!event
|| !header
) {
527 /* Could be NULL since an event class doesn't own a stream class */
528 if (!event
->event_class
->stream_class
) {
534 * Ensure the provided header's type matches the one registered to the
537 field_type
= bt_ctf_field_get_type(header
);
538 if (field_type
!= event
->event_class
->stream_class
->event_header_type
) {
543 bt_ctf_field_get(header
);
544 bt_ctf_field_put(event
->event_header
);
545 event
->event_header
= header
;
548 bt_ctf_field_type_put(field_type
);
553 struct bt_ctf_field
*bt_ctf_event_get_event_context(
554 struct bt_ctf_event
*event
)
556 struct bt_ctf_field
*context
= NULL
;
558 if (!event
|| !event
->context_payload
) {
562 context
= event
->context_payload
;
563 bt_ctf_field_get(context
);
568 int bt_ctf_event_set_event_context(struct bt_ctf_event
*event
,
569 struct bt_ctf_field
*context
)
572 struct bt_ctf_field_type
*field_type
= NULL
;
574 if (!event
|| !context
) {
579 field_type
= bt_ctf_field_get_type(context
);
580 if (field_type
!= event
->event_class
->context
) {
585 bt_ctf_field_get(context
);
586 bt_ctf_field_put(event
->context_payload
);
587 event
->context_payload
= context
;
590 bt_ctf_field_type_put(field_type
);
595 void bt_ctf_event_get(struct bt_ctf_event
*event
)
601 bt_ctf_ref_get(&event
->ref_count
);
604 void bt_ctf_event_put(struct bt_ctf_event
*event
)
610 bt_ctf_ref_put(&event
->ref_count
, bt_ctf_event_destroy
);
614 void bt_ctf_event_class_destroy(struct bt_ctf_ref
*ref
)
616 struct bt_ctf_event_class
*event_class
;
623 * Don't call put() on the stream class. See comment in
624 * bt_ctf_event_class_set_stream_class for explanation.
626 event_class
= container_of(ref
, struct bt_ctf_event_class
, ref_count
);
627 if (event_class
->context
) {
628 bt_ctf_field_type_put(event_class
->context
);
630 if (event_class
->fields
) {
631 bt_ctf_field_type_put(event_class
->fields
);
637 void bt_ctf_event_destroy(struct bt_ctf_ref
*ref
)
639 struct bt_ctf_event
*event
;
645 event
= container_of(ref
, struct bt_ctf_event
,
647 if (event
->event_class
) {
648 bt_ctf_event_class_put(event
->event_class
);
650 if (event
->event_header
) {
651 bt_ctf_field_put(event
->event_header
);
653 if (event
->context_payload
) {
654 bt_ctf_field_put(event
->context_payload
);
656 if (event
->fields_payload
) {
657 bt_ctf_field_put(event
->fields_payload
);
663 int set_integer_field_value(struct bt_ctf_field
* field
, uint64_t value
)
666 struct bt_ctf_field_type
*field_type
= NULL
;
673 if (!bt_ctf_field_validate(field
)) {
674 /* Payload already set, skip! (not an error) */
678 field_type
= bt_ctf_field_get_type(field
);
681 if (bt_ctf_field_type_get_type_id(field_type
) != CTF_TYPE_INTEGER
) {
682 /* Not an integer and the value is unset, error. */
687 if (bt_ctf_field_type_integer_get_signed(field_type
)) {
688 ret
= bt_ctf_field_signed_integer_set_value(field
, (int64_t) value
);
690 /* Value is out of range, error. */
694 ret
= bt_ctf_field_unsigned_integer_set_value(field
, value
);
696 /* Value is out of range, error. */
701 bt_ctf_field_type_put(field_type
);
706 void bt_ctf_event_class_freeze(struct bt_ctf_event_class
*event_class
)
709 event_class
->frozen
= 1;
710 bt_ctf_field_type_freeze(event_class
->context
);
711 bt_ctf_field_type_freeze(event_class
->fields
);
715 int bt_ctf_event_class_set_stream_class(struct bt_ctf_event_class
*event_class
,
716 struct bt_ctf_stream_class
*stream_class
)
725 /* Allow a NULL stream_class to unset the current stream_class */
726 if (stream_class
&& event_class
->stream_class
) {
731 event_class
->stream_class
= stream_class
;
733 * We don't get() the stream_class since doing so would introduce
734 * a circular ownership between event classes and stream classes.
736 * A stream class will always unset itself from its events before
737 * being destroyed. This ensures that a user won't get a pointer
738 * to a stale stream class instance from an event class.
745 int bt_ctf_event_class_serialize(struct bt_ctf_event_class
*event_class
,
746 struct metadata_context
*context
)
753 stream_id
= bt_ctf_stream_class_get_id(event_class
->stream_class
);
759 context
->current_indentation_level
= 1;
760 g_string_assign(context
->field_name
, "");
761 g_string_append_printf(context
->string
,
762 "event {\n\tname = \"%s\";\n\tid = %u;\n\tstream_id = %" PRId64
";\n",
763 g_quark_to_string(event_class
->name
),
767 if (event_class
->context
) {
768 g_string_append(context
->string
, "\tcontext := ");
769 ret
= bt_ctf_field_type_serialize(event_class
->context
,
774 g_string_append(context
->string
, ";\n");
777 if (event_class
->fields
) {
778 g_string_append(context
->string
, "\tfields := ");
779 ret
= bt_ctf_field_type_serialize(event_class
->fields
, context
);
783 g_string_append(context
->string
, ";\n");
786 g_string_append(context
->string
, "};\n\n");
788 context
->current_indentation_level
= 0;
792 void bt_ctf_event_class_set_native_byte_order(
793 struct bt_ctf_event_class
*event_class
,
800 bt_ctf_field_type_set_native_byte_order(event_class
->context
,
802 bt_ctf_field_type_set_native_byte_order(event_class
->fields
,
807 int bt_ctf_event_validate(struct bt_ctf_event
*event
)
809 /* Make sure each field's payload has been set */
813 ret
= bt_ctf_field_validate(event
->event_header
);
818 ret
= bt_ctf_field_validate(event
->fields_payload
);
823 if (event
->event_class
->context
) {
824 ret
= bt_ctf_field_validate(event
->context_payload
);
831 int bt_ctf_event_serialize(struct bt_ctf_event
*event
,
832 struct ctf_stream_pos
*pos
)
838 if (event
->context_payload
) {
839 ret
= bt_ctf_field_serialize(event
->context_payload
, pos
);
845 if (event
->fields_payload
) {
846 ret
= bt_ctf_field_serialize(event
->fields_payload
, pos
);
856 int bt_ctf_event_populate_event_header(struct bt_ctf_event
*event
)
859 struct bt_ctf_field
*id_field
= NULL
, *timestamp_field
= NULL
;
866 id_field
= bt_ctf_field_structure_get_field(event
->event_header
, "id");
868 ret
= set_integer_field_value(id_field
,
869 (uint64_t) event
->event_class
->id
);
875 timestamp_field
= bt_ctf_field_structure_get_field(event
->event_header
,
877 if (timestamp_field
) {
878 struct bt_ctf_field_type
*timestamp_field_type
=
879 bt_ctf_field_get_type(timestamp_field
);
880 struct bt_ctf_clock
*mapped_clock
;
882 assert(timestamp_field_type
);
883 mapped_clock
= bt_ctf_field_type_integer_get_mapped_clock(
884 timestamp_field_type
);
885 bt_ctf_field_type_put(timestamp_field_type
);
887 uint64_t timestamp
= bt_ctf_clock_get_time(
890 bt_ctf_clock_put(mapped_clock
);
891 if (timestamp
== (uint64_t) -1ULL) {
895 ret
= set_integer_field_value(timestamp_field
,
904 bt_ctf_field_put(id_field
);
906 if (timestamp_field
) {
907 bt_ctf_field_put(timestamp_field
);