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
)
51 struct bt_object
*obj
= NULL
;
52 struct bt_ctf_event_class
*event_class
= NULL
;
54 if (bt_ctf_validate_identifier(name
)) {
58 event_class
= g_new0(struct bt_ctf_event_class
, 1);
63 bt_ctf_ref_init(&event_class
->ref_count
);
64 event_class
->fields
= bt_ctf_field_type_structure_create();
65 if (!event_class
->fields
) {
69 event_class
->attributes
= bt_ctf_attributes_create();
70 if (!event_class
->attributes
) {
74 obj
= bt_object_integer_create_init(-1);
79 ret
= bt_ctf_attributes_set_field_value(event_class
->attributes
,
87 obj
= bt_object_string_create_init(name
);
92 ret
= bt_ctf_attributes_set_field_value(event_class
->attributes
,
104 bt_ctf_event_class_put(event_class
);
112 const char *bt_ctf_event_class_get_name(struct bt_ctf_event_class
*event_class
)
114 struct bt_object
*obj
= NULL
;
115 const char *name
= NULL
;
121 obj
= bt_ctf_attributes_get_field_value(event_class
->attributes
,
122 BT_CTF_EVENT_CLASS_ATTR_NAME_INDEX
);
127 if (bt_object_string_get(obj
, &name
)) {
137 int64_t bt_ctf_event_class_get_id(struct bt_ctf_event_class
*event_class
)
139 struct bt_object
*obj
= NULL
;
147 obj
= bt_ctf_attributes_get_field_value(event_class
->attributes
,
148 BT_CTF_EVENT_CLASS_ATTR_ID_INDEX
);
153 if (bt_object_integer_get(obj
, &ret
)) {
158 /* means ID is not set */
169 int bt_ctf_event_class_set_id(struct bt_ctf_event_class
*event_class
,
173 struct bt_object
*obj
= NULL
;
180 if (event_class
->stream_class
) {
182 * We don't allow changing the id if the event class has already
183 * been added to a stream class.
189 obj
= bt_ctf_attributes_get_field_value(event_class
->attributes
,
190 BT_CTF_EVENT_CLASS_ATTR_ID_INDEX
);
195 if (bt_object_integer_set(obj
, id
)) {
206 int bt_ctf_event_class_set_attribute(
207 struct bt_ctf_event_class
*event_class
, const char *name
,
208 struct bt_object
*value
)
212 if (!event_class
|| !name
|| !value
|| event_class
->frozen
) {
217 if (!strcmp(name
, "id") || !strcmp(name
, "loglevel")) {
218 if (!bt_object_is_integer(value
)) {
222 } else if (!strcmp(name
, "name") || !strcmp(name
, "model.emf.uri")) {
223 if (!bt_object_is_string(value
)) {
228 /* unknown attribute */
233 /* "id" special case: >= 0 */
234 if (!strcmp(name
, "id")) {
237 ret
= bt_object_integer_get(value
, &val
);
249 ret
= bt_ctf_attributes_set_field_value(event_class
->attributes
,
256 int bt_ctf_event_class_get_attribute_count(
257 struct bt_ctf_event_class
*event_class
)
266 ret
= bt_ctf_attributes_get_count(event_class
->attributes
);
273 bt_ctf_event_class_get_attribute_name(
274 struct bt_ctf_event_class
*event_class
, int index
)
283 ret
= bt_ctf_attributes_get_field_name(event_class
->attributes
, index
);
290 bt_ctf_event_class_get_attribute_value(struct bt_ctf_event_class
*event_class
,
293 struct bt_object
*ret
;
300 ret
= bt_ctf_attributes_get_field_value(event_class
->attributes
, index
);
307 bt_ctf_event_class_get_attribute_value_by_name(
308 struct bt_ctf_event_class
*event_class
, const char *name
)
310 struct bt_object
*ret
;
312 if (!event_class
|| !name
) {
317 ret
= bt_ctf_attributes_get_field_value_by_name(event_class
->attributes
,
325 struct bt_ctf_stream_class
*bt_ctf_event_class_get_stream_class(
326 struct bt_ctf_event_class
*event_class
)
328 struct bt_ctf_stream_class
*stream_class
= NULL
;
334 stream_class
= event_class
->stream_class
;
335 bt_ctf_stream_class_get(stream_class
);
340 struct bt_ctf_field_type
*bt_ctf_event_class_get_payload_type(
341 struct bt_ctf_event_class
*event_class
)
343 struct bt_ctf_field_type
*payload
= NULL
;
349 bt_ctf_field_type_get(event_class
->fields
);
350 payload
= event_class
->fields
;
355 int bt_ctf_event_class_set_payload_type(struct bt_ctf_event_class
*event_class
,
356 struct bt_ctf_field_type
*payload
)
360 if (!event_class
|| !payload
||
361 bt_ctf_field_type_get_type_id(payload
) != CTF_TYPE_STRUCT
) {
366 bt_ctf_field_type_get(payload
);
367 bt_ctf_field_type_put(event_class
->fields
);
368 event_class
->fields
= payload
;
373 int bt_ctf_event_class_add_field(struct bt_ctf_event_class
*event_class
,
374 struct bt_ctf_field_type
*type
,
379 if (!event_class
|| !type
|| bt_ctf_validate_identifier(name
) ||
380 event_class
->frozen
) {
385 if (bt_ctf_field_type_get_type_id(event_class
->fields
) !=
391 ret
= bt_ctf_field_type_structure_add_field(event_class
->fields
,
397 int bt_ctf_event_class_get_field_count(
398 struct bt_ctf_event_class
*event_class
)
407 if (bt_ctf_field_type_get_type_id(event_class
->fields
) !=
413 ret
= bt_ctf_field_type_structure_get_field_count(event_class
->fields
);
418 int bt_ctf_event_class_get_field(struct bt_ctf_event_class
*event_class
,
419 const char **field_name
, struct bt_ctf_field_type
**field_type
,
424 if (!event_class
|| index
< 0) {
429 if (bt_ctf_field_type_get_type_id(event_class
->fields
) !=
435 ret
= bt_ctf_field_type_structure_get_field(event_class
->fields
,
436 field_name
, field_type
, index
);
441 struct bt_ctf_field_type
*bt_ctf_event_class_get_field_by_name(
442 struct bt_ctf_event_class
*event_class
, const char *name
)
445 struct bt_ctf_field_type
*field_type
= NULL
;
447 if (!event_class
|| !name
) {
451 if (bt_ctf_field_type_get_type_id(event_class
->fields
) !=
456 name_quark
= g_quark_try_string(name
);
462 * No need to increment field_type's reference count since getting it
463 * from the structure already does.
465 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
466 event_class
->fields
, name
);
471 struct bt_ctf_field_type
*bt_ctf_event_class_get_context_type(
472 struct bt_ctf_event_class
*event_class
)
474 struct bt_ctf_field_type
*context_type
= NULL
;
476 if (!event_class
|| !event_class
->context
) {
480 bt_ctf_field_type_get(event_class
->context
);
481 context_type
= event_class
->context
;
486 int bt_ctf_event_class_set_context_type(
487 struct bt_ctf_event_class
*event_class
,
488 struct bt_ctf_field_type
*context
)
492 if (!event_class
|| !context
|| event_class
->frozen
) {
497 if (bt_ctf_field_type_get_type_id(context
) != CTF_TYPE_STRUCT
) {
502 bt_ctf_field_type_get(context
);
503 bt_ctf_field_type_put(event_class
->context
);
504 event_class
->context
= context
;
510 void bt_ctf_event_class_get(struct bt_ctf_event_class
*event_class
)
516 bt_ctf_ref_get(&event_class
->ref_count
);
519 void bt_ctf_event_class_put(struct bt_ctf_event_class
*event_class
)
525 bt_ctf_ref_put(&event_class
->ref_count
, bt_ctf_event_class_destroy
);
528 struct bt_ctf_event
*bt_ctf_event_create(struct bt_ctf_event_class
*event_class
)
531 struct bt_object
*obj
= NULL
;
532 struct bt_ctf_event
*event
= NULL
;
539 * The event class does not keep ownership of the stream class to
540 * which it as been added. Therefore, it can't assume it has been
541 * set. However, we disallow the creation of an event if its
542 * associated stream class has been reclaimed.
544 if (!event_class
->stream_class
) {
547 assert(event_class
->stream_class
->event_header_type
);
549 /* set "stream_id" attribute now that we know its value */
550 obj
= bt_object_integer_create_init(event_class
->stream_class
->id
);
555 ret
= bt_ctf_attributes_set_field_value(event_class
->attributes
,
563 event
= g_new0(struct bt_ctf_event
, 1);
568 bt_ctf_ref_init(&event
->ref_count
);
569 bt_ctf_event_class_get(event_class
);
570 bt_ctf_event_class_freeze(event_class
);
571 event
->event_class
= event_class
;
573 event
->event_header
= bt_ctf_field_create(
574 event_class
->stream_class
->event_header_type
);
575 if (!event
->event_header
) {
578 if (event_class
->context
) {
579 event
->context_payload
= bt_ctf_field_create(
580 event_class
->context
);
581 if (!event
->context_payload
) {
585 event
->fields_payload
= bt_ctf_field_create(event_class
->fields
);
586 if (!event
->fields_payload
) {
591 * Freeze the stream class since the event header must not be changed
594 bt_ctf_stream_class_freeze(event_class
->stream_class
);
599 bt_ctf_event_destroy(&event
->ref_count
);
605 struct bt_ctf_event_class
*bt_ctf_event_get_class(struct bt_ctf_event
*event
)
607 struct bt_ctf_event_class
*event_class
= NULL
;
613 event_class
= event
->event_class
;
614 bt_ctf_event_class_get(event_class
);
619 struct bt_ctf_stream
*bt_ctf_event_get_stream(struct bt_ctf_event
*event
)
621 struct bt_ctf_stream
*stream
= NULL
;
627 stream
= event
->stream
;
629 bt_ctf_stream_get(stream
);
635 struct bt_ctf_clock
*bt_ctf_event_get_clock(struct bt_ctf_event
*event
)
637 struct bt_ctf_clock
*clock
= NULL
;
638 struct bt_ctf_event_class
*event_class
;
639 struct bt_ctf_stream_class
*stream_class
;
645 event_class
= bt_ctf_event_get_class(event
);
650 stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
652 goto error_put_event_class
;
655 clock
= bt_ctf_stream_class_get_clock(stream_class
);
657 goto error_put_stream_class
;
660 error_put_stream_class
:
661 bt_ctf_stream_class_put(stream_class
);
662 error_put_event_class
:
663 bt_ctf_event_class_put(event_class
);
668 int bt_ctf_event_set_payload(struct bt_ctf_event
*event
,
670 struct bt_ctf_field
*payload
)
674 if (!event
|| !payload
) {
680 ret
= bt_ctf_field_structure_set_field(event
->fields_payload
,
683 struct bt_ctf_field_type
*payload_type
;
685 payload_type
= bt_ctf_field_get_type(payload
);
686 if (payload_type
== event
->event_class
->fields
) {
687 bt_ctf_field_put(event
->fields_payload
);
688 bt_ctf_field_get(payload
);
689 event
->fields_payload
= payload
;
694 bt_ctf_field_type_put(payload_type
);
701 struct bt_ctf_field
*bt_ctf_event_get_payload(struct bt_ctf_event
*event
,
704 struct bt_ctf_field
*field
= NULL
;
711 field
= bt_ctf_field_structure_get_field(event
->fields_payload
,
714 field
= event
->fields_payload
;
715 bt_ctf_field_get(field
);
721 struct bt_ctf_field
*bt_ctf_event_get_payload_by_index(
722 struct bt_ctf_event
*event
, int index
)
724 struct bt_ctf_field
*field
= NULL
;
726 if (!event
|| index
< 0) {
730 field
= bt_ctf_field_structure_get_field_by_index(event
->fields_payload
,
736 struct bt_ctf_field
*bt_ctf_event_get_event_header(
737 struct bt_ctf_event
*event
)
739 struct bt_ctf_field
*header
= NULL
;
741 if (!event
|| !event
->event_header
) {
745 header
= event
->event_header
;
746 bt_ctf_field_get(header
);
751 int bt_ctf_event_set_event_header(struct bt_ctf_event
*event
,
752 struct bt_ctf_field
*header
)
755 struct bt_ctf_field_type
*field_type
= NULL
;
757 if (!event
|| !header
) {
762 /* Could be NULL since an event class doesn't own a stream class */
763 if (!event
->event_class
->stream_class
) {
769 * Ensure the provided header's type matches the one registered to the
772 field_type
= bt_ctf_field_get_type(header
);
773 if (field_type
!= event
->event_class
->stream_class
->event_header_type
) {
778 bt_ctf_field_get(header
);
779 bt_ctf_field_put(event
->event_header
);
780 event
->event_header
= header
;
783 bt_ctf_field_type_put(field_type
);
788 struct bt_ctf_field
*bt_ctf_event_get_event_context(
789 struct bt_ctf_event
*event
)
791 struct bt_ctf_field
*context
= NULL
;
793 if (!event
|| !event
->context_payload
) {
797 context
= event
->context_payload
;
798 bt_ctf_field_get(context
);
803 int bt_ctf_event_set_event_context(struct bt_ctf_event
*event
,
804 struct bt_ctf_field
*context
)
807 struct bt_ctf_field_type
*field_type
= NULL
;
809 if (!event
|| !context
) {
814 field_type
= bt_ctf_field_get_type(context
);
815 if (field_type
!= event
->event_class
->context
) {
820 bt_ctf_field_get(context
);
821 bt_ctf_field_put(event
->context_payload
);
822 event
->context_payload
= context
;
825 bt_ctf_field_type_put(field_type
);
830 void bt_ctf_event_get(struct bt_ctf_event
*event
)
836 bt_ctf_ref_get(&event
->ref_count
);
839 void bt_ctf_event_put(struct bt_ctf_event
*event
)
845 bt_ctf_ref_put(&event
->ref_count
, bt_ctf_event_destroy
);
849 void bt_ctf_event_class_destroy(struct bt_ctf_ref
*ref
)
851 struct bt_ctf_event_class
*event_class
;
858 * Don't call put() on the stream class. See comment in
859 * bt_ctf_event_class_set_stream_class for explanation.
861 event_class
= container_of(ref
, struct bt_ctf_event_class
, ref_count
);
862 if (event_class
->attributes
) {
863 bt_ctf_attributes_destroy(event_class
->attributes
);
865 if (event_class
->context
) {
866 bt_ctf_field_type_put(event_class
->context
);
868 if (event_class
->fields
) {
869 bt_ctf_field_type_put(event_class
->fields
);
875 void bt_ctf_event_destroy(struct bt_ctf_ref
*ref
)
877 struct bt_ctf_event
*event
;
883 event
= container_of(ref
, struct bt_ctf_event
,
885 if (event
->event_class
) {
886 bt_ctf_event_class_put(event
->event_class
);
888 if (event
->event_header
) {
889 bt_ctf_field_put(event
->event_header
);
891 if (event
->context_payload
) {
892 bt_ctf_field_put(event
->context_payload
);
894 if (event
->fields_payload
) {
895 bt_ctf_field_put(event
->fields_payload
);
901 int set_integer_field_value(struct bt_ctf_field
* field
, uint64_t value
)
904 struct bt_ctf_field_type
*field_type
= NULL
;
911 if (!bt_ctf_field_validate(field
)) {
912 /* Payload already set, skip! (not an error) */
916 field_type
= bt_ctf_field_get_type(field
);
919 if (bt_ctf_field_type_get_type_id(field_type
) != CTF_TYPE_INTEGER
) {
920 /* Not an integer and the value is unset, error. */
925 if (bt_ctf_field_type_integer_get_signed(field_type
)) {
926 ret
= bt_ctf_field_signed_integer_set_value(field
, (int64_t) value
);
928 /* Value is out of range, error. */
932 ret
= bt_ctf_field_unsigned_integer_set_value(field
, value
);
934 /* Value is out of range, error. */
939 bt_ctf_field_type_put(field_type
);
944 void bt_ctf_event_class_freeze(struct bt_ctf_event_class
*event_class
)
947 event_class
->frozen
= 1;
948 bt_ctf_field_type_freeze(event_class
->context
);
949 bt_ctf_field_type_freeze(event_class
->fields
);
950 bt_ctf_attributes_freeze(event_class
->attributes
);
954 int bt_ctf_event_class_set_stream_class(struct bt_ctf_event_class
*event_class
,
955 struct bt_ctf_stream_class
*stream_class
)
964 /* Allow a NULL stream_class to unset the current stream_class */
965 if (stream_class
&& event_class
->stream_class
) {
970 event_class
->stream_class
= stream_class
;
972 * We don't get() the stream_class since doing so would introduce
973 * a circular ownership between event classes and stream classes.
975 * A stream class will always unset itself from its events before
976 * being destroyed. This ensures that a user won't get a pointer
977 * to a stale stream class instance from an event class.
984 int bt_ctf_event_class_serialize(struct bt_ctf_event_class
*event_class
,
985 struct metadata_context
*context
)
990 struct bt_object
*attr_value
= NULL
;
995 context
->current_indentation_level
= 1;
996 g_string_assign(context
->field_name
, "");
997 g_string_append(context
->string
, "event {\n");
998 count
= bt_ctf_event_class_get_attribute_count(event_class
);
1005 for (i
= 0; i
< count
; ++i
) {
1006 const char *attr_name
= NULL
;
1008 attr_name
= bt_ctf_event_class_get_attribute_name(
1010 attr_value
= bt_ctf_event_class_get_attribute_value(
1013 if (!attr_name
|| !attr_value
) {
1018 switch (bt_object_get_type(attr_value
)) {
1019 case BT_OBJECT_TYPE_INTEGER
:
1023 ret
= bt_object_integer_get(attr_value
, &value
);
1029 g_string_append_printf(context
->string
,
1030 "\t%s = %" PRId64
";\n", attr_name
, value
);
1034 case BT_OBJECT_TYPE_STRING
:
1038 ret
= bt_object_string_get(attr_value
, &value
);
1044 g_string_append_printf(context
->string
,
1045 "\t%s = \"%s\";\n", attr_name
, value
);
1050 /* should never happen */
1055 BT_OBJECT_PUT(attr_value
);
1058 if (event_class
->context
) {
1059 g_string_append(context
->string
, "\tcontext := ");
1060 ret
= bt_ctf_field_type_serialize(event_class
->context
,
1065 g_string_append(context
->string
, ";\n");
1068 if (event_class
->fields
) {
1069 g_string_append(context
->string
, "\tfields := ");
1070 ret
= bt_ctf_field_type_serialize(event_class
->fields
, context
);
1074 g_string_append(context
->string
, ";\n");
1077 g_string_append(context
->string
, "};\n\n");
1079 context
->current_indentation_level
= 0;
1080 BT_OBJECT_PUT(attr_value
);
1084 void bt_ctf_event_class_set_native_byte_order(
1085 struct bt_ctf_event_class
*event_class
,
1092 bt_ctf_field_type_set_native_byte_order(event_class
->context
,
1094 bt_ctf_field_type_set_native_byte_order(event_class
->fields
,
1099 int bt_ctf_event_validate(struct bt_ctf_event
*event
)
1101 /* Make sure each field's payload has been set */
1105 ret
= bt_ctf_field_validate(event
->event_header
);
1110 ret
= bt_ctf_field_validate(event
->fields_payload
);
1115 if (event
->event_class
->context
) {
1116 ret
= bt_ctf_field_validate(event
->context_payload
);
1123 int bt_ctf_event_serialize(struct bt_ctf_event
*event
,
1124 struct ctf_stream_pos
*pos
)
1130 if (event
->context_payload
) {
1131 ret
= bt_ctf_field_serialize(event
->context_payload
, pos
);
1137 if (event
->fields_payload
) {
1138 ret
= bt_ctf_field_serialize(event
->fields_payload
, pos
);
1148 int bt_ctf_event_populate_event_header(struct bt_ctf_event
*event
)
1151 struct bt_ctf_field
*id_field
= NULL
, *timestamp_field
= NULL
;
1158 id_field
= bt_ctf_field_structure_get_field(event
->event_header
, "id");
1160 ret
= set_integer_field_value(id_field
,
1161 (uint64_t) bt_ctf_event_class_get_id(
1162 event
->event_class
));
1168 timestamp_field
= bt_ctf_field_structure_get_field(event
->event_header
,
1170 if (timestamp_field
) {
1171 struct bt_ctf_field_type
*timestamp_field_type
=
1172 bt_ctf_field_get_type(timestamp_field
);
1173 struct bt_ctf_clock
*mapped_clock
;
1175 assert(timestamp_field_type
);
1176 mapped_clock
= bt_ctf_field_type_integer_get_mapped_clock(
1177 timestamp_field_type
);
1178 bt_ctf_field_type_put(timestamp_field_type
);
1180 uint64_t timestamp
= bt_ctf_clock_get_time(
1183 bt_ctf_clock_put(mapped_clock
);
1184 if (timestamp
== (uint64_t) -1ULL) {
1188 ret
= set_integer_field_value(timestamp_field
,
1197 bt_ctf_field_put(id_field
);
1199 if (timestamp_field
) {
1200 bt_ctf_field_put(timestamp_field
);
1206 int bt_ctf_event_set_stream(struct bt_ctf_event
*event
,
1207 struct bt_ctf_stream
*stream
)
1216 if (event
->stream
&& stream
) {
1217 /* Already attached to a stream */
1222 event
->stream
= stream
;