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
||
162 bt_ctf_field_type_get_type_id(payload
) != CTF_TYPE_STRUCT
) {
167 bt_ctf_field_type_get(payload
);
168 bt_ctf_field_type_put(event_class
->fields
);
169 event_class
->fields
= payload
;
174 int bt_ctf_event_class_add_field(struct bt_ctf_event_class
*event_class
,
175 struct bt_ctf_field_type
*type
,
180 if (!event_class
|| !type
|| bt_ctf_validate_identifier(name
) ||
181 event_class
->frozen
) {
186 if (bt_ctf_field_type_get_type_id(event_class
->fields
) !=
192 ret
= bt_ctf_field_type_structure_add_field(event_class
->fields
,
198 int bt_ctf_event_class_get_field_count(
199 struct bt_ctf_event_class
*event_class
)
208 if (bt_ctf_field_type_get_type_id(event_class
->fields
) !=
214 ret
= bt_ctf_field_type_structure_get_field_count(event_class
->fields
);
219 int bt_ctf_event_class_get_field(struct bt_ctf_event_class
*event_class
,
220 const char **field_name
, struct bt_ctf_field_type
**field_type
,
225 if (!event_class
|| index
< 0) {
230 if (bt_ctf_field_type_get_type_id(event_class
->fields
) !=
236 ret
= bt_ctf_field_type_structure_get_field(event_class
->fields
,
237 field_name
, field_type
, index
);
242 struct bt_ctf_field_type
*bt_ctf_event_class_get_field_by_name(
243 struct bt_ctf_event_class
*event_class
, const char *name
)
246 struct bt_ctf_field_type
*field_type
= NULL
;
248 if (!event_class
|| !name
) {
252 if (bt_ctf_field_type_get_type_id(event_class
->fields
) !=
257 name_quark
= g_quark_try_string(name
);
263 * No need to increment field_type's reference count since getting it
264 * from the structure already does.
266 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
267 event_class
->fields
, name
);
272 struct bt_ctf_field_type
*bt_ctf_event_class_get_context_type(
273 struct bt_ctf_event_class
*event_class
)
275 struct bt_ctf_field_type
*context_type
= NULL
;
277 if (!event_class
|| !event_class
->context
) {
281 bt_ctf_field_type_get(event_class
->context
);
282 context_type
= event_class
->context
;
287 int bt_ctf_event_class_set_context_type(
288 struct bt_ctf_event_class
*event_class
,
289 struct bt_ctf_field_type
*context
)
293 if (!event_class
|| !context
|| event_class
->frozen
) {
298 if (bt_ctf_field_type_get_type_id(context
) != CTF_TYPE_STRUCT
) {
303 bt_ctf_field_type_get(context
);
304 bt_ctf_field_type_put(event_class
->context
);
305 event_class
->context
= context
;
311 void bt_ctf_event_class_get(struct bt_ctf_event_class
*event_class
)
317 bt_ctf_ref_get(&event_class
->ref_count
);
320 void bt_ctf_event_class_put(struct bt_ctf_event_class
*event_class
)
326 bt_ctf_ref_put(&event_class
->ref_count
, bt_ctf_event_class_destroy
);
329 struct bt_ctf_event
*bt_ctf_event_create(struct bt_ctf_event_class
*event_class
)
331 struct bt_ctf_event
*event
= NULL
;
337 event
= g_new0(struct bt_ctf_event
, 1);
342 bt_ctf_ref_init(&event
->ref_count
);
343 bt_ctf_event_class_get(event_class
);
344 bt_ctf_event_class_freeze(event_class
);
345 event
->event_class
= event_class
;
348 * The event class does not keep ownership of the stream class to
349 * which it as been added. Therefore, it can't assume it has been
350 * set. However, we disallow the creation of an event if its
351 * associated stream class has been reclaimed.
353 if (!event_class
->stream_class
) {
356 assert(event_class
->stream_class
->event_header_type
);
358 event
->event_header
= bt_ctf_field_create(
359 event_class
->stream_class
->event_header_type
);
360 if (!event
->event_header
) {
363 if (event_class
->context
) {
364 event
->context_payload
= bt_ctf_field_create(
365 event_class
->context
);
366 if (!event
->context_payload
) {
370 event
->fields_payload
= bt_ctf_field_create(event_class
->fields
);
371 if (!event
->fields_payload
) {
376 * Freeze the stream class since the event header must not be changed
379 bt_ctf_stream_class_freeze(event_class
->stream_class
);
383 bt_ctf_event_destroy(&event
->ref_count
);
387 struct bt_ctf_event_class
*bt_ctf_event_get_class(struct bt_ctf_event
*event
)
389 struct bt_ctf_event_class
*event_class
= NULL
;
395 event_class
= event
->event_class
;
396 bt_ctf_event_class_get(event_class
);
401 struct bt_ctf_clock
*bt_ctf_event_get_clock(struct bt_ctf_event
*event
)
403 struct bt_ctf_clock
*clock
= NULL
;
404 struct bt_ctf_event_class
*event_class
;
405 struct bt_ctf_stream_class
*stream_class
;
411 event_class
= bt_ctf_event_get_class(event
);
416 stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
418 goto error_put_event_class
;
421 clock
= bt_ctf_stream_class_get_clock(stream_class
);
423 goto error_put_stream_class
;
426 error_put_stream_class
:
427 bt_ctf_stream_class_put(stream_class
);
428 error_put_event_class
:
429 bt_ctf_event_class_put(event_class
);
434 int bt_ctf_event_set_payload(struct bt_ctf_event
*event
,
436 struct bt_ctf_field
*payload
)
440 if (!event
|| !payload
) {
446 ret
= bt_ctf_field_structure_set_field(event
->fields_payload
,
449 struct bt_ctf_field_type
*payload_type
;
451 payload_type
= bt_ctf_field_get_type(payload
);
452 if (payload_type
== event
->event_class
->fields
) {
453 bt_ctf_field_put(event
->fields_payload
);
454 bt_ctf_field_get(payload
);
455 event
->fields_payload
= payload
;
460 bt_ctf_field_type_put(payload_type
);
467 struct bt_ctf_field
*bt_ctf_event_get_payload(struct bt_ctf_event
*event
,
470 struct bt_ctf_field
*field
= NULL
;
477 field
= bt_ctf_field_structure_get_field(event
->fields_payload
,
480 field
= event
->fields_payload
;
481 bt_ctf_field_get(field
);
487 struct bt_ctf_field
*bt_ctf_event_get_payload_by_index(
488 struct bt_ctf_event
*event
, int index
)
490 struct bt_ctf_field
*field
= NULL
;
492 if (!event
|| index
< 0) {
496 field
= bt_ctf_field_structure_get_field_by_index(event
->fields_payload
,
502 struct bt_ctf_field
*bt_ctf_event_get_event_header(
503 struct bt_ctf_event
*event
)
505 struct bt_ctf_field
*header
= NULL
;
507 if (!event
|| !event
->event_header
) {
511 header
= event
->event_header
;
512 bt_ctf_field_get(header
);
517 int bt_ctf_event_set_event_header(struct bt_ctf_event
*event
,
518 struct bt_ctf_field
*header
)
521 struct bt_ctf_field_type
*field_type
= NULL
;
523 if (!event
|| !header
) {
528 /* Could be NULL since an event class doesn't own a stream class */
529 if (!event
->event_class
->stream_class
) {
535 * Ensure the provided header's type matches the one registered to the
538 field_type
= bt_ctf_field_get_type(header
);
539 if (field_type
!= event
->event_class
->stream_class
->event_header_type
) {
544 bt_ctf_field_get(header
);
545 bt_ctf_field_put(event
->event_header
);
546 event
->event_header
= header
;
549 bt_ctf_field_type_put(field_type
);
554 struct bt_ctf_field
*bt_ctf_event_get_event_context(
555 struct bt_ctf_event
*event
)
557 struct bt_ctf_field
*context
= NULL
;
559 if (!event
|| !event
->context_payload
) {
563 context
= event
->context_payload
;
564 bt_ctf_field_get(context
);
569 int bt_ctf_event_set_event_context(struct bt_ctf_event
*event
,
570 struct bt_ctf_field
*context
)
573 struct bt_ctf_field_type
*field_type
= NULL
;
575 if (!event
|| !context
) {
580 field_type
= bt_ctf_field_get_type(context
);
581 if (field_type
!= event
->event_class
->context
) {
586 bt_ctf_field_get(context
);
587 bt_ctf_field_put(event
->context_payload
);
588 event
->context_payload
= context
;
591 bt_ctf_field_type_put(field_type
);
596 void bt_ctf_event_get(struct bt_ctf_event
*event
)
602 bt_ctf_ref_get(&event
->ref_count
);
605 void bt_ctf_event_put(struct bt_ctf_event
*event
)
611 bt_ctf_ref_put(&event
->ref_count
, bt_ctf_event_destroy
);
615 void bt_ctf_event_class_destroy(struct bt_ctf_ref
*ref
)
617 struct bt_ctf_event_class
*event_class
;
624 * Don't call put() on the stream class. See comment in
625 * bt_ctf_event_class_set_stream_class for explanation.
627 event_class
= container_of(ref
, struct bt_ctf_event_class
, ref_count
);
628 if (event_class
->context
) {
629 bt_ctf_field_type_put(event_class
->context
);
631 if (event_class
->fields
) {
632 bt_ctf_field_type_put(event_class
->fields
);
638 void bt_ctf_event_destroy(struct bt_ctf_ref
*ref
)
640 struct bt_ctf_event
*event
;
646 event
= container_of(ref
, struct bt_ctf_event
,
648 if (event
->event_class
) {
649 bt_ctf_event_class_put(event
->event_class
);
651 if (event
->event_header
) {
652 bt_ctf_field_put(event
->event_header
);
654 if (event
->context_payload
) {
655 bt_ctf_field_put(event
->context_payload
);
657 if (event
->fields_payload
) {
658 bt_ctf_field_put(event
->fields_payload
);
664 int set_integer_field_value(struct bt_ctf_field
* field
, uint64_t value
)
667 struct bt_ctf_field_type
*field_type
= NULL
;
674 if (!bt_ctf_field_validate(field
)) {
675 /* Payload already set, skip! (not an error) */
679 field_type
= bt_ctf_field_get_type(field
);
682 if (bt_ctf_field_type_get_type_id(field_type
) != CTF_TYPE_INTEGER
) {
683 /* Not an integer and the value is unset, error. */
688 if (bt_ctf_field_type_integer_get_signed(field_type
)) {
689 ret
= bt_ctf_field_signed_integer_set_value(field
, (int64_t) value
);
691 /* Value is out of range, error. */
695 ret
= bt_ctf_field_unsigned_integer_set_value(field
, value
);
697 /* Value is out of range, error. */
702 bt_ctf_field_type_put(field_type
);
707 void bt_ctf_event_class_freeze(struct bt_ctf_event_class
*event_class
)
710 event_class
->frozen
= 1;
711 bt_ctf_field_type_freeze(event_class
->context
);
712 bt_ctf_field_type_freeze(event_class
->fields
);
716 int bt_ctf_event_class_set_stream_class(struct bt_ctf_event_class
*event_class
,
717 struct bt_ctf_stream_class
*stream_class
)
726 /* Allow a NULL stream_class to unset the current stream_class */
727 if (stream_class
&& event_class
->stream_class
) {
732 event_class
->stream_class
= stream_class
;
734 * We don't get() the stream_class since doing so would introduce
735 * a circular ownership between event classes and stream classes.
737 * A stream class will always unset itself from its events before
738 * being destroyed. This ensures that a user won't get a pointer
739 * to a stale stream class instance from an event class.
746 int bt_ctf_event_class_serialize(struct bt_ctf_event_class
*event_class
,
747 struct metadata_context
*context
)
754 stream_id
= bt_ctf_stream_class_get_id(event_class
->stream_class
);
760 context
->current_indentation_level
= 1;
761 g_string_assign(context
->field_name
, "");
762 g_string_append_printf(context
->string
,
763 "event {\n\tname = \"%s\";\n\tid = %u;\n\tstream_id = %" PRId64
";\n",
764 g_quark_to_string(event_class
->name
),
768 if (event_class
->context
) {
769 g_string_append(context
->string
, "\tcontext := ");
770 ret
= bt_ctf_field_type_serialize(event_class
->context
,
775 g_string_append(context
->string
, ";\n");
778 if (event_class
->fields
) {
779 g_string_append(context
->string
, "\tfields := ");
780 ret
= bt_ctf_field_type_serialize(event_class
->fields
, context
);
784 g_string_append(context
->string
, ";\n");
787 g_string_append(context
->string
, "};\n\n");
789 context
->current_indentation_level
= 0;
793 void bt_ctf_event_class_set_native_byte_order(
794 struct bt_ctf_event_class
*event_class
,
801 bt_ctf_field_type_set_native_byte_order(event_class
->context
,
803 bt_ctf_field_type_set_native_byte_order(event_class
->fields
,
808 int bt_ctf_event_validate(struct bt_ctf_event
*event
)
810 /* Make sure each field's payload has been set */
814 ret
= bt_ctf_field_validate(event
->event_header
);
819 ret
= bt_ctf_field_validate(event
->fields_payload
);
824 if (event
->event_class
->context
) {
825 ret
= bt_ctf_field_validate(event
->context_payload
);
832 int bt_ctf_event_serialize(struct bt_ctf_event
*event
,
833 struct ctf_stream_pos
*pos
)
839 if (event
->context_payload
) {
840 ret
= bt_ctf_field_serialize(event
->context_payload
, pos
);
846 if (event
->fields_payload
) {
847 ret
= bt_ctf_field_serialize(event
->fields_payload
, pos
);
857 int bt_ctf_event_populate_event_header(struct bt_ctf_event
*event
)
860 struct bt_ctf_field
*id_field
= NULL
, *timestamp_field
= NULL
;
867 id_field
= bt_ctf_field_structure_get_field(event
->event_header
, "id");
869 ret
= set_integer_field_value(id_field
,
870 (uint64_t) event
->event_class
->id
);
876 timestamp_field
= bt_ctf_field_structure_get_field(event
->event_header
,
878 if (timestamp_field
) {
879 struct bt_ctf_field_type
*timestamp_field_type
=
880 bt_ctf_field_get_type(timestamp_field
);
881 struct bt_ctf_clock
*mapped_clock
;
883 assert(timestamp_field_type
);
884 mapped_clock
= bt_ctf_field_type_integer_get_mapped_clock(
885 timestamp_field_type
);
886 bt_ctf_field_type_put(timestamp_field_type
);
888 uint64_t timestamp
= bt_ctf_clock_get_time(
891 bt_ctf_clock_put(mapped_clock
);
892 if (timestamp
== (uint64_t) -1ULL) {
896 ret
= set_integer_field_value(timestamp_field
,
905 bt_ctf_field_put(id_field
);
907 if (timestamp_field
) {
908 bt_ctf_field_put(timestamp_field
);