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/ctf-ir/attributes-internal.h>
40 #include <babeltrace/compiler.h>
43 void bt_ctf_event_class_destroy(struct bt_ctf_ref
*ref
);
45 void bt_ctf_event_destroy(struct bt_ctf_ref
*ref
);
47 int set_integer_field_value(struct bt_ctf_field
*field
, uint64_t value
);
49 struct bt_ctf_event_class
*bt_ctf_event_class_create(const char *name
)
52 struct bt_object
*obj
= NULL
;
53 struct bt_ctf_event_class
*event_class
= NULL
;
55 if (bt_ctf_validate_identifier(name
)) {
59 event_class
= g_new0(struct bt_ctf_event_class
, 1);
64 bt_ctf_ref_init(&event_class
->ref_count
);
65 event_class
->fields
= bt_ctf_field_type_structure_create();
66 if (!event_class
->fields
) {
70 event_class
->attributes
= bt_ctf_attributes_create();
71 if (!event_class
->attributes
) {
75 obj
= bt_object_integer_create_init(-1);
80 ret
= bt_ctf_attributes_set_field_value(event_class
->attributes
,
88 obj
= bt_object_string_create_init(name
);
93 ret
= bt_ctf_attributes_set_field_value(event_class
->attributes
,
105 bt_ctf_event_class_put(event_class
);
113 const char *bt_ctf_event_class_get_name(struct bt_ctf_event_class
*event_class
)
115 struct bt_object
*obj
= NULL
;
116 const char *name
= NULL
;
122 obj
= bt_ctf_attributes_get_field_value(event_class
->attributes
,
123 BT_CTF_EVENT_CLASS_ATTR_NAME_INDEX
);
128 if (bt_object_string_get(obj
, &name
)) {
138 int64_t bt_ctf_event_class_get_id(struct bt_ctf_event_class
*event_class
)
140 struct bt_object
*obj
= NULL
;
148 obj
= bt_ctf_attributes_get_field_value(event_class
->attributes
,
149 BT_CTF_EVENT_CLASS_ATTR_ID_INDEX
);
154 if (bt_object_integer_get(obj
, &ret
)) {
159 /* means ID is not set */
170 int bt_ctf_event_class_set_id(struct bt_ctf_event_class
*event_class
,
174 struct bt_object
*obj
= NULL
;
181 if (event_class
->stream_class
) {
183 * We don't allow changing the id if the event class has already
184 * been added to a stream class.
190 obj
= bt_ctf_attributes_get_field_value(event_class
->attributes
,
191 BT_CTF_EVENT_CLASS_ATTR_ID_INDEX
);
196 if (bt_object_integer_set(obj
, id
)) {
207 int bt_ctf_event_class_set_attribute(
208 struct bt_ctf_event_class
*event_class
, const char *name
,
209 struct bt_object
*value
)
213 if (!event_class
|| !name
|| !value
|| event_class
->frozen
) {
218 if (!strcmp(name
, "id") || !strcmp(name
, "loglevel")) {
219 if (!bt_object_is_integer(value
)) {
223 } else if (!strcmp(name
, "name") || !strcmp(name
, "model.emf.uri")) {
224 if (!bt_object_is_string(value
)) {
229 /* unknown attribute */
234 /* "id" special case: >= 0 */
235 if (!strcmp(name
, "id")) {
238 ret
= bt_object_integer_get(value
, &val
);
250 ret
= bt_ctf_attributes_set_field_value(event_class
->attributes
,
257 int bt_ctf_event_class_get_attribute_count(
258 struct bt_ctf_event_class
*event_class
)
267 ret
= bt_ctf_attributes_get_count(event_class
->attributes
);
274 bt_ctf_event_class_get_attribute_name(
275 struct bt_ctf_event_class
*event_class
, int index
)
284 ret
= bt_ctf_attributes_get_field_name(event_class
->attributes
, index
);
291 bt_ctf_event_class_get_attribute_value(struct bt_ctf_event_class
*event_class
,
294 struct bt_object
*ret
;
301 ret
= bt_ctf_attributes_get_field_value(event_class
->attributes
, index
);
308 bt_ctf_event_class_get_attribute_value_by_name(
309 struct bt_ctf_event_class
*event_class
, const char *name
)
311 struct bt_object
*ret
;
313 if (!event_class
|| !name
) {
318 ret
= bt_ctf_attributes_get_field_value_by_name(event_class
->attributes
,
326 struct bt_ctf_stream_class
*bt_ctf_event_class_get_stream_class(
327 struct bt_ctf_event_class
*event_class
)
329 struct bt_ctf_stream_class
*stream_class
= NULL
;
335 stream_class
= event_class
->stream_class
;
336 bt_ctf_stream_class_get(stream_class
);
341 struct bt_ctf_field_type
*bt_ctf_event_class_get_payload_type(
342 struct bt_ctf_event_class
*event_class
)
344 struct bt_ctf_field_type
*payload
= NULL
;
350 bt_ctf_field_type_get(event_class
->fields
);
351 payload
= event_class
->fields
;
356 int bt_ctf_event_class_set_payload_type(struct bt_ctf_event_class
*event_class
,
357 struct bt_ctf_field_type
*payload
)
361 if (!event_class
|| !payload
||
362 bt_ctf_field_type_get_type_id(payload
) != CTF_TYPE_STRUCT
) {
367 bt_ctf_field_type_get(payload
);
368 bt_ctf_field_type_put(event_class
->fields
);
369 event_class
->fields
= payload
;
374 int bt_ctf_event_class_add_field(struct bt_ctf_event_class
*event_class
,
375 struct bt_ctf_field_type
*type
,
380 if (!event_class
|| !type
|| bt_ctf_validate_identifier(name
) ||
381 event_class
->frozen
) {
386 if (bt_ctf_field_type_get_type_id(event_class
->fields
) !=
392 ret
= bt_ctf_field_type_structure_add_field(event_class
->fields
,
398 int bt_ctf_event_class_get_field_count(
399 struct bt_ctf_event_class
*event_class
)
408 if (bt_ctf_field_type_get_type_id(event_class
->fields
) !=
414 ret
= bt_ctf_field_type_structure_get_field_count(event_class
->fields
);
419 int bt_ctf_event_class_get_field(struct bt_ctf_event_class
*event_class
,
420 const char **field_name
, struct bt_ctf_field_type
**field_type
,
425 if (!event_class
|| index
< 0) {
430 if (bt_ctf_field_type_get_type_id(event_class
->fields
) !=
436 ret
= bt_ctf_field_type_structure_get_field(event_class
->fields
,
437 field_name
, field_type
, index
);
442 struct bt_ctf_field_type
*bt_ctf_event_class_get_field_by_name(
443 struct bt_ctf_event_class
*event_class
, const char *name
)
446 struct bt_ctf_field_type
*field_type
= NULL
;
448 if (!event_class
|| !name
) {
452 if (bt_ctf_field_type_get_type_id(event_class
->fields
) !=
457 name_quark
= g_quark_try_string(name
);
463 * No need to increment field_type's reference count since getting it
464 * from the structure already does.
466 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
467 event_class
->fields
, name
);
472 struct bt_ctf_field_type
*bt_ctf_event_class_get_context_type(
473 struct bt_ctf_event_class
*event_class
)
475 struct bt_ctf_field_type
*context_type
= NULL
;
477 if (!event_class
|| !event_class
->context
) {
481 bt_ctf_field_type_get(event_class
->context
);
482 context_type
= event_class
->context
;
487 int bt_ctf_event_class_set_context_type(
488 struct bt_ctf_event_class
*event_class
,
489 struct bt_ctf_field_type
*context
)
493 if (!event_class
|| !context
|| event_class
->frozen
) {
498 if (bt_ctf_field_type_get_type_id(context
) != CTF_TYPE_STRUCT
) {
503 bt_ctf_field_type_get(context
);
504 bt_ctf_field_type_put(event_class
->context
);
505 event_class
->context
= context
;
511 void bt_ctf_event_class_get(struct bt_ctf_event_class
*event_class
)
517 bt_ctf_ref_get(&event_class
->ref_count
);
520 void bt_ctf_event_class_put(struct bt_ctf_event_class
*event_class
)
526 bt_ctf_ref_put(&event_class
->ref_count
, bt_ctf_event_class_destroy
);
529 struct bt_ctf_event
*bt_ctf_event_create(struct bt_ctf_event_class
*event_class
)
532 struct bt_object
*obj
= NULL
;
533 struct bt_ctf_event
*event
= NULL
;
540 * The event class does not keep ownership of the stream class to
541 * which it as been added. Therefore, it can't assume it has been
542 * set. However, we disallow the creation of an event if its
543 * associated stream class has been reclaimed.
545 if (!event_class
->stream_class
) {
548 assert(event_class
->stream_class
->event_header_type
);
550 /* set "stream_id" attribute now that we know its value */
551 obj
= bt_object_integer_create_init(event_class
->stream_class
->id
);
556 ret
= bt_ctf_attributes_set_field_value(event_class
->attributes
,
564 event
= g_new0(struct bt_ctf_event
, 1);
569 bt_ctf_ref_init(&event
->ref_count
);
570 bt_ctf_event_class_get(event_class
);
571 bt_ctf_event_class_freeze(event_class
);
572 event
->event_class
= event_class
;
574 event
->event_header
= bt_ctf_field_create(
575 event_class
->stream_class
->event_header_type
);
576 if (!event
->event_header
) {
579 if (event_class
->context
) {
580 event
->context_payload
= bt_ctf_field_create(
581 event_class
->context
);
582 if (!event
->context_payload
) {
586 event
->fields_payload
= bt_ctf_field_create(event_class
->fields
);
587 if (!event
->fields_payload
) {
592 * Freeze the stream class since the event header must not be changed
595 bt_ctf_stream_class_freeze(event_class
->stream_class
);
600 bt_ctf_event_destroy(&event
->ref_count
);
606 struct bt_ctf_event_class
*bt_ctf_event_get_class(struct bt_ctf_event
*event
)
608 struct bt_ctf_event_class
*event_class
= NULL
;
614 event_class
= event
->event_class
;
615 bt_ctf_event_class_get(event_class
);
620 struct bt_ctf_stream
*bt_ctf_event_get_stream(struct bt_ctf_event
*event
)
622 struct bt_ctf_stream
*stream
= NULL
;
628 stream
= event
->stream
;
630 bt_ctf_stream_get(stream
);
636 struct bt_ctf_clock
*bt_ctf_event_get_clock(struct bt_ctf_event
*event
)
638 struct bt_ctf_clock
*clock
= NULL
;
639 struct bt_ctf_event_class
*event_class
;
640 struct bt_ctf_stream_class
*stream_class
;
646 event_class
= bt_ctf_event_get_class(event
);
651 stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
653 goto error_put_event_class
;
656 clock
= bt_ctf_stream_class_get_clock(stream_class
);
658 goto error_put_stream_class
;
661 error_put_stream_class
:
662 bt_ctf_stream_class_put(stream_class
);
663 error_put_event_class
:
664 bt_ctf_event_class_put(event_class
);
669 int bt_ctf_event_set_payload(struct bt_ctf_event
*event
,
671 struct bt_ctf_field
*payload
)
675 if (!event
|| !payload
) {
681 ret
= bt_ctf_field_structure_set_field(event
->fields_payload
,
684 struct bt_ctf_field_type
*payload_type
;
686 payload_type
= bt_ctf_field_get_type(payload
);
687 if (payload_type
== event
->event_class
->fields
) {
688 bt_ctf_field_put(event
->fields_payload
);
689 bt_ctf_field_get(payload
);
690 event
->fields_payload
= payload
;
695 bt_ctf_field_type_put(payload_type
);
702 struct bt_ctf_field
*bt_ctf_event_get_payload(struct bt_ctf_event
*event
,
705 struct bt_ctf_field
*field
= NULL
;
712 field
= bt_ctf_field_structure_get_field(event
->fields_payload
,
715 field
= event
->fields_payload
;
716 bt_ctf_field_get(field
);
722 struct bt_ctf_field
*bt_ctf_event_get_payload_by_index(
723 struct bt_ctf_event
*event
, int index
)
725 struct bt_ctf_field
*field
= NULL
;
727 if (!event
|| index
< 0) {
731 field
= bt_ctf_field_structure_get_field_by_index(event
->fields_payload
,
737 struct bt_ctf_field
*bt_ctf_event_get_event_header(
738 struct bt_ctf_event
*event
)
740 struct bt_ctf_field
*header
= NULL
;
742 if (!event
|| !event
->event_header
) {
746 header
= event
->event_header
;
747 bt_ctf_field_get(header
);
752 int bt_ctf_event_set_event_header(struct bt_ctf_event
*event
,
753 struct bt_ctf_field
*header
)
756 struct bt_ctf_field_type
*field_type
= NULL
;
758 if (!event
|| !header
) {
763 /* Could be NULL since an event class doesn't own a stream class */
764 if (!event
->event_class
->stream_class
) {
770 * Ensure the provided header's type matches the one registered to the
773 field_type
= bt_ctf_field_get_type(header
);
774 if (field_type
!= event
->event_class
->stream_class
->event_header_type
) {
779 bt_ctf_field_get(header
);
780 bt_ctf_field_put(event
->event_header
);
781 event
->event_header
= header
;
784 bt_ctf_field_type_put(field_type
);
789 struct bt_ctf_field
*bt_ctf_event_get_event_context(
790 struct bt_ctf_event
*event
)
792 struct bt_ctf_field
*context
= NULL
;
794 if (!event
|| !event
->context_payload
) {
798 context
= event
->context_payload
;
799 bt_ctf_field_get(context
);
804 int bt_ctf_event_set_event_context(struct bt_ctf_event
*event
,
805 struct bt_ctf_field
*context
)
808 struct bt_ctf_field_type
*field_type
= NULL
;
810 if (!event
|| !context
) {
815 field_type
= bt_ctf_field_get_type(context
);
816 if (field_type
!= event
->event_class
->context
) {
821 bt_ctf_field_get(context
);
822 bt_ctf_field_put(event
->context_payload
);
823 event
->context_payload
= context
;
826 bt_ctf_field_type_put(field_type
);
831 void bt_ctf_event_get(struct bt_ctf_event
*event
)
837 bt_ctf_ref_get(&event
->ref_count
);
840 void bt_ctf_event_put(struct bt_ctf_event
*event
)
846 bt_ctf_ref_put(&event
->ref_count
, bt_ctf_event_destroy
);
850 void bt_ctf_event_class_destroy(struct bt_ctf_ref
*ref
)
852 struct bt_ctf_event_class
*event_class
;
859 * Don't call put() on the stream class. See comment in
860 * bt_ctf_event_class_set_stream_class for explanation.
862 event_class
= container_of(ref
, struct bt_ctf_event_class
, ref_count
);
863 if (event_class
->attributes
) {
864 bt_ctf_attributes_destroy(event_class
->attributes
);
866 if (event_class
->context
) {
867 bt_ctf_field_type_put(event_class
->context
);
869 if (event_class
->fields
) {
870 bt_ctf_field_type_put(event_class
->fields
);
876 void bt_ctf_event_destroy(struct bt_ctf_ref
*ref
)
878 struct bt_ctf_event
*event
;
884 event
= container_of(ref
, struct bt_ctf_event
,
886 if (event
->event_class
) {
887 bt_ctf_event_class_put(event
->event_class
);
889 if (event
->event_header
) {
890 bt_ctf_field_put(event
->event_header
);
892 if (event
->context_payload
) {
893 bt_ctf_field_put(event
->context_payload
);
895 if (event
->fields_payload
) {
896 bt_ctf_field_put(event
->fields_payload
);
902 int set_integer_field_value(struct bt_ctf_field
* field
, uint64_t value
)
905 struct bt_ctf_field_type
*field_type
= NULL
;
912 if (!bt_ctf_field_validate(field
)) {
913 /* Payload already set, skip! (not an error) */
917 field_type
= bt_ctf_field_get_type(field
);
920 if (bt_ctf_field_type_get_type_id(field_type
) != CTF_TYPE_INTEGER
) {
921 /* Not an integer and the value is unset, error. */
926 if (bt_ctf_field_type_integer_get_signed(field_type
)) {
927 ret
= bt_ctf_field_signed_integer_set_value(field
, (int64_t) value
);
929 /* Value is out of range, error. */
933 ret
= bt_ctf_field_unsigned_integer_set_value(field
, value
);
935 /* Value is out of range, error. */
940 bt_ctf_field_type_put(field_type
);
945 void bt_ctf_event_class_freeze(struct bt_ctf_event_class
*event_class
)
948 event_class
->frozen
= 1;
949 bt_ctf_field_type_freeze(event_class
->context
);
950 bt_ctf_field_type_freeze(event_class
->fields
);
951 bt_ctf_attributes_freeze(event_class
->attributes
);
955 int bt_ctf_event_class_set_stream_class(struct bt_ctf_event_class
*event_class
,
956 struct bt_ctf_stream_class
*stream_class
)
965 /* Allow a NULL stream_class to unset the current stream_class */
966 if (stream_class
&& event_class
->stream_class
) {
971 event_class
->stream_class
= stream_class
;
973 * We don't get() the stream_class since doing so would introduce
974 * a circular ownership between event classes and stream classes.
976 * A stream class will always unset itself from its events before
977 * being destroyed. This ensures that a user won't get a pointer
978 * to a stale stream class instance from an event class.
985 int bt_ctf_event_class_serialize(struct bt_ctf_event_class
*event_class
,
986 struct metadata_context
*context
)
991 struct bt_object
*attr_value
= NULL
;
996 context
->current_indentation_level
= 1;
997 g_string_assign(context
->field_name
, "");
998 g_string_append(context
->string
, "event {\n");
999 count
= bt_ctf_event_class_get_attribute_count(event_class
);
1006 for (i
= 0; i
< count
; ++i
) {
1007 const char *attr_name
= NULL
;
1009 attr_name
= bt_ctf_event_class_get_attribute_name(
1011 attr_value
= bt_ctf_event_class_get_attribute_value(
1014 if (!attr_name
|| !attr_value
) {
1019 switch (bt_object_get_type(attr_value
)) {
1020 case BT_OBJECT_TYPE_INTEGER
:
1024 ret
= bt_object_integer_get(attr_value
, &value
);
1030 g_string_append_printf(context
->string
,
1031 "\t%s = %" PRId64
";\n", attr_name
, value
);
1035 case BT_OBJECT_TYPE_STRING
:
1039 ret
= bt_object_string_get(attr_value
, &value
);
1045 g_string_append_printf(context
->string
,
1046 "\t%s = \"%s\";\n", attr_name
, value
);
1051 /* should never happen */
1056 BT_OBJECT_PUT(attr_value
);
1059 if (event_class
->context
) {
1060 g_string_append(context
->string
, "\tcontext := ");
1061 ret
= bt_ctf_field_type_serialize(event_class
->context
,
1066 g_string_append(context
->string
, ";\n");
1069 if (event_class
->fields
) {
1070 g_string_append(context
->string
, "\tfields := ");
1071 ret
= bt_ctf_field_type_serialize(event_class
->fields
, context
);
1075 g_string_append(context
->string
, ";\n");
1078 g_string_append(context
->string
, "};\n\n");
1080 context
->current_indentation_level
= 0;
1081 BT_OBJECT_PUT(attr_value
);
1085 void bt_ctf_event_class_set_native_byte_order(
1086 struct bt_ctf_event_class
*event_class
,
1093 bt_ctf_field_type_set_native_byte_order(event_class
->context
,
1095 bt_ctf_field_type_set_native_byte_order(event_class
->fields
,
1100 int bt_ctf_event_validate(struct bt_ctf_event
*event
)
1102 /* Make sure each field's payload has been set */
1106 ret
= bt_ctf_field_validate(event
->event_header
);
1111 ret
= bt_ctf_field_validate(event
->fields_payload
);
1116 if (event
->event_class
->context
) {
1117 ret
= bt_ctf_field_validate(event
->context_payload
);
1124 int bt_ctf_event_serialize(struct bt_ctf_event
*event
,
1125 struct ctf_stream_pos
*pos
)
1131 if (event
->context_payload
) {
1132 ret
= bt_ctf_field_serialize(event
->context_payload
, pos
);
1138 if (event
->fields_payload
) {
1139 ret
= bt_ctf_field_serialize(event
->fields_payload
, pos
);
1149 int bt_ctf_event_populate_event_header(struct bt_ctf_event
*event
)
1152 struct bt_ctf_field
*id_field
= NULL
, *timestamp_field
= NULL
;
1159 id_field
= bt_ctf_field_structure_get_field(event
->event_header
, "id");
1161 ret
= set_integer_field_value(id_field
,
1162 (uint64_t) bt_ctf_event_class_get_id(
1163 event
->event_class
));
1169 timestamp_field
= bt_ctf_field_structure_get_field(event
->event_header
,
1171 if (timestamp_field
) {
1172 struct bt_ctf_field_type
*timestamp_field_type
=
1173 bt_ctf_field_get_type(timestamp_field
);
1174 struct bt_ctf_clock
*mapped_clock
;
1176 assert(timestamp_field_type
);
1177 mapped_clock
= bt_ctf_field_type_integer_get_mapped_clock(
1178 timestamp_field_type
);
1179 bt_ctf_field_type_put(timestamp_field_type
);
1181 uint64_t timestamp
= bt_ctf_clock_get_time(
1184 bt_ctf_clock_put(mapped_clock
);
1185 if (timestamp
== (uint64_t) -1ULL) {
1189 ret
= set_integer_field_value(timestamp_field
,
1198 bt_ctf_field_put(id_field
);
1200 if (timestamp_field
) {
1201 bt_ctf_field_put(timestamp_field
);
1207 int bt_ctf_event_set_stream(struct bt_ctf_event
*event
,
1208 struct bt_ctf_stream
*stream
)
1217 if (event
->stream
&& stream
) {
1218 /* Already attached to a stream */
1223 event
->stream
= stream
;