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/ref.h>
40 #include <babeltrace/ctf-ir/attributes-internal.h>
41 #include <babeltrace/compiler.h>
44 void bt_ctf_event_class_destroy(struct bt_object
*obj
);
46 void bt_ctf_event_destroy(struct bt_object
*obj
);
48 int set_integer_field_value(struct bt_ctf_field
*field
, uint64_t value
);
50 struct bt_ctf_event_class
*bt_ctf_event_class_create(const char *name
)
53 struct bt_value
*obj
= NULL
;
54 struct bt_ctf_event_class
*event_class
= NULL
;
56 if (bt_ctf_validate_identifier(name
)) {
60 event_class
= g_new0(struct bt_ctf_event_class
, 1);
65 bt_object_init(event_class
, bt_ctf_event_class_destroy
);
66 event_class
->fields
= bt_ctf_field_type_structure_create();
67 if (!event_class
->fields
) {
71 event_class
->attributes
= bt_ctf_attributes_create();
72 if (!event_class
->attributes
) {
76 obj
= bt_value_integer_create_init(-1);
81 ret
= bt_ctf_attributes_set_field_value(event_class
->attributes
,
89 obj
= bt_value_string_create_init(name
);
94 ret
= bt_ctf_attributes_set_field_value(event_class
->attributes
,
110 const char *bt_ctf_event_class_get_name(struct bt_ctf_event_class
*event_class
)
112 struct bt_value
*obj
= NULL
;
113 const char *name
= NULL
;
119 obj
= bt_ctf_attributes_get_field_value(event_class
->attributes
,
120 BT_CTF_EVENT_CLASS_ATTR_NAME_INDEX
);
125 if (bt_value_string_get(obj
, &name
)) {
134 int64_t bt_ctf_event_class_get_id(struct bt_ctf_event_class
*event_class
)
136 struct bt_value
*obj
= NULL
;
144 obj
= bt_ctf_attributes_get_field_value(event_class
->attributes
,
145 BT_CTF_EVENT_CLASS_ATTR_ID_INDEX
);
150 if (bt_value_integer_get(obj
, &ret
)) {
155 /* means ID is not set */
165 int bt_ctf_event_class_set_id(struct bt_ctf_event_class
*event_class
,
169 struct bt_value
*obj
= NULL
;
170 struct bt_ctf_stream_class
*stream_class
= NULL
;
178 stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
181 * We don't allow changing the id if the event class has already
182 * been added to a stream class.
188 obj
= bt_ctf_attributes_get_field_value(event_class
->attributes
,
189 BT_CTF_EVENT_CLASS_ATTR_ID_INDEX
);
194 if (bt_value_integer_set(obj
, id
)) {
201 BT_PUT(stream_class
);
205 int bt_ctf_event_class_set_attribute(
206 struct bt_ctf_event_class
*event_class
, const char *name
,
207 struct bt_value
*value
)
211 if (!event_class
|| !name
|| !value
|| event_class
->frozen
) {
216 if (!strcmp(name
, "id") || !strcmp(name
, "loglevel")) {
217 if (!bt_value_is_integer(value
)) {
221 } else if (!strcmp(name
, "name") || !strcmp(name
, "model.emf.uri")) {
222 if (!bt_value_is_string(value
)) {
227 /* unknown attribute */
232 /* "id" special case: >= 0 */
233 if (!strcmp(name
, "id")) {
236 ret
= bt_value_integer_get(value
, &val
);
248 ret
= bt_ctf_attributes_set_field_value(event_class
->attributes
,
255 int bt_ctf_event_class_get_attribute_count(
256 struct bt_ctf_event_class
*event_class
)
265 ret
= bt_ctf_attributes_get_count(event_class
->attributes
);
272 bt_ctf_event_class_get_attribute_name(
273 struct bt_ctf_event_class
*event_class
, int index
)
282 ret
= bt_ctf_attributes_get_field_name(event_class
->attributes
, index
);
289 bt_ctf_event_class_get_attribute_value(struct bt_ctf_event_class
*event_class
,
292 struct bt_value
*ret
;
299 ret
= bt_ctf_attributes_get_field_value(event_class
->attributes
, index
);
306 bt_ctf_event_class_get_attribute_value_by_name(
307 struct bt_ctf_event_class
*event_class
, const char *name
)
309 struct bt_value
*ret
;
311 if (!event_class
|| !name
) {
316 ret
= bt_ctf_attributes_get_field_value_by_name(event_class
->attributes
,
324 struct bt_ctf_stream_class
*bt_ctf_event_class_get_stream_class(
325 struct bt_ctf_event_class
*event_class
)
327 return (struct bt_ctf_stream_class
*) bt_object_get_parent(event_class
);
330 struct bt_ctf_field_type
*bt_ctf_event_class_get_payload_type(
331 struct bt_ctf_event_class
*event_class
)
333 struct bt_ctf_field_type
*payload
= NULL
;
339 bt_get(event_class
->fields
);
340 payload
= event_class
->fields
;
345 int bt_ctf_event_class_set_payload_type(struct bt_ctf_event_class
*event_class
,
346 struct bt_ctf_field_type
*payload
)
350 if (!event_class
|| !payload
||
351 bt_ctf_field_type_get_type_id(payload
) != CTF_TYPE_STRUCT
) {
357 bt_put(event_class
->fields
);
358 event_class
->fields
= payload
;
363 int bt_ctf_event_class_add_field(struct bt_ctf_event_class
*event_class
,
364 struct bt_ctf_field_type
*type
,
369 if (!event_class
|| !type
|| bt_ctf_validate_identifier(name
) ||
370 event_class
->frozen
) {
375 if (bt_ctf_field_type_get_type_id(event_class
->fields
) !=
381 ret
= bt_ctf_field_type_structure_add_field(event_class
->fields
,
387 int bt_ctf_event_class_get_field_count(
388 struct bt_ctf_event_class
*event_class
)
397 if (bt_ctf_field_type_get_type_id(event_class
->fields
) !=
403 ret
= bt_ctf_field_type_structure_get_field_count(event_class
->fields
);
408 int bt_ctf_event_class_get_field(struct bt_ctf_event_class
*event_class
,
409 const char **field_name
, struct bt_ctf_field_type
**field_type
,
414 if (!event_class
|| index
< 0) {
419 if (bt_ctf_field_type_get_type_id(event_class
->fields
) !=
425 ret
= bt_ctf_field_type_structure_get_field(event_class
->fields
,
426 field_name
, field_type
, index
);
431 struct bt_ctf_field_type
*bt_ctf_event_class_get_field_by_name(
432 struct bt_ctf_event_class
*event_class
, const char *name
)
435 struct bt_ctf_field_type
*field_type
= NULL
;
437 if (!event_class
|| !name
) {
441 if (bt_ctf_field_type_get_type_id(event_class
->fields
) !=
446 name_quark
= g_quark_try_string(name
);
452 * No need to increment field_type's reference count since getting it
453 * from the structure already does.
455 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
456 event_class
->fields
, name
);
461 struct bt_ctf_field_type
*bt_ctf_event_class_get_context_type(
462 struct bt_ctf_event_class
*event_class
)
464 struct bt_ctf_field_type
*context_type
= NULL
;
466 if (!event_class
|| !event_class
->context
) {
470 bt_get(event_class
->context
);
471 context_type
= event_class
->context
;
476 int bt_ctf_event_class_set_context_type(
477 struct bt_ctf_event_class
*event_class
,
478 struct bt_ctf_field_type
*context
)
482 if (!event_class
|| !context
|| event_class
->frozen
) {
487 if (bt_ctf_field_type_get_type_id(context
) != CTF_TYPE_STRUCT
) {
493 bt_put(event_class
->context
);
494 event_class
->context
= context
;
500 void bt_ctf_event_class_get(struct bt_ctf_event_class
*event_class
)
505 void bt_ctf_event_class_put(struct bt_ctf_event_class
*event_class
)
511 int bt_ctf_event_class_set_stream_id(struct bt_ctf_event_class
*event_class
,
515 struct bt_value
*obj
;
517 obj
= bt_value_integer_create_init(stream_id
);
524 ret
= bt_ctf_attributes_set_field_value(event_class
->attributes
,
532 struct bt_ctf_event
*bt_ctf_event_create(struct bt_ctf_event_class
*event_class
)
534 struct bt_ctf_event
*event
= NULL
;
535 struct bt_ctf_stream_class
*stream_class
= NULL
;
541 stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
543 * We disallow the creation of an event if its event class has not been
544 * associated to a stream class.
549 assert(stream_class
->event_header_type
);
550 event
= g_new0(struct bt_ctf_event
, 1);
555 bt_object_init(event
, bt_ctf_event_destroy
);
556 bt_ctf_event_class_freeze(event_class
);
558 * event does not share a common ancestor with the event class; it has
559 * to guarantee its existence by holding a reference. This reference
560 * shall be released once the event is associated to a stream since,
561 * from that point, the event and its class will share the same
564 event
->event_class
= bt_get(event_class
);
566 event
->event_header
= bt_ctf_field_create(
567 stream_class
->event_header_type
);
568 if (!event
->event_header
) {
571 if (event_class
->context
) {
572 event
->context_payload
= bt_ctf_field_create(
573 event_class
->context
);
574 if (!event
->context_payload
) {
578 event
->fields_payload
= bt_ctf_field_create(event_class
->fields
);
579 if (!event
->fields_payload
) {
584 * Freeze the stream class since the event header must not be changed
587 bt_ctf_stream_class_freeze(stream_class
);
588 BT_PUT(stream_class
);
592 BT_PUT(stream_class
);
596 struct bt_ctf_event_class
*bt_ctf_event_get_class(struct bt_ctf_event
*event
)
598 struct bt_ctf_event_class
*event_class
= NULL
;
604 event_class
= event
->event_class
;
610 struct bt_ctf_stream
*bt_ctf_event_get_stream(struct bt_ctf_event
*event
)
612 return (struct bt_ctf_stream
*) bt_object_get_parent(event
);
615 struct bt_ctf_clock
*bt_ctf_event_get_clock(struct bt_ctf_event
*event
)
617 struct bt_ctf_clock
*clock
= NULL
;
618 struct bt_ctf_event_class
*event_class
;
619 struct bt_ctf_stream_class
*stream_class
;
625 event_class
= bt_ctf_event_get_class(event
);
630 stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
632 goto error_put_event_class
;
635 clock
= bt_ctf_stream_class_get_clock(stream_class
);
637 goto error_put_stream_class
;
640 error_put_stream_class
:
641 bt_put(stream_class
);
642 error_put_event_class
:
648 int bt_ctf_event_set_payload(struct bt_ctf_event
*event
,
650 struct bt_ctf_field
*payload
)
654 if (!event
|| !payload
) {
660 ret
= bt_ctf_field_structure_set_field(event
->fields_payload
,
663 struct bt_ctf_field_type
*payload_type
;
665 payload_type
= bt_ctf_field_get_type(payload
);
666 if (payload_type
== event
->event_class
->fields
) {
667 bt_put(event
->fields_payload
);
669 event
->fields_payload
= payload
;
674 bt_put(payload_type
);
680 struct bt_ctf_field
*bt_ctf_event_get_payload_field(struct bt_ctf_event
*event
)
682 struct bt_ctf_field
*payload
= NULL
;
684 if (!event
|| !event
->fields_payload
) {
688 payload
= event
->fields_payload
;
694 int bt_ctf_event_set_payload_field(struct bt_ctf_event
*event
,
695 struct bt_ctf_field
*payload
)
698 struct bt_ctf_field_type
*payload_type
= NULL
;
700 if (!event
|| !payload
) {
705 payload_type
= bt_ctf_field_get_type(payload
);
711 if (bt_ctf_field_type_get_type_id(payload_type
) != CTF_TYPE_STRUCT
) {
717 bt_put(event
->fields_payload
);
718 event
->fields_payload
= payload
;
721 bt_put(payload_type
);
725 struct bt_ctf_field
*bt_ctf_event_get_payload(struct bt_ctf_event
*event
,
728 struct bt_ctf_field
*field
= NULL
;
735 field
= bt_ctf_field_structure_get_field(event
->fields_payload
,
738 field
= event
->fields_payload
;
745 struct bt_ctf_field
*bt_ctf_event_get_payload_by_index(
746 struct bt_ctf_event
*event
, int index
)
748 struct bt_ctf_field
*field
= NULL
;
750 if (!event
|| index
< 0) {
754 field
= bt_ctf_field_structure_get_field_by_index(event
->fields_payload
,
760 struct bt_ctf_field
*bt_ctf_event_get_header(
761 struct bt_ctf_event
*event
)
763 struct bt_ctf_field
*header
= NULL
;
765 if (!event
|| !event
->event_header
) {
769 header
= event
->event_header
;
775 int bt_ctf_event_set_header(struct bt_ctf_event
*event
,
776 struct bt_ctf_field
*header
)
779 struct bt_ctf_field_type
*field_type
= NULL
;
780 struct bt_ctf_stream_class
*stream_class
= NULL
;
782 if (!event
|| !header
) {
787 stream_class
= (struct bt_ctf_stream_class
*) bt_object_get_parent(
790 * Ensure the provided header's type matches the one registered to the
793 field_type
= bt_ctf_field_get_type(header
);
794 if (field_type
!= stream_class
->event_header_type
) {
800 bt_put(event
->event_header
);
801 event
->event_header
= header
;
803 bt_put(stream_class
);
808 struct bt_ctf_field
*bt_ctf_event_get_event_context(
809 struct bt_ctf_event
*event
)
811 struct bt_ctf_field
*context
= NULL
;
813 if (!event
|| !event
->context_payload
) {
817 context
= event
->context_payload
;
823 int bt_ctf_event_set_event_context(struct bt_ctf_event
*event
,
824 struct bt_ctf_field
*context
)
827 struct bt_ctf_field_type
*field_type
= NULL
;
829 if (!event
|| !context
) {
834 field_type
= bt_ctf_field_get_type(context
);
835 if (field_type
!= event
->event_class
->context
) {
841 bt_put(event
->context_payload
);
842 event
->context_payload
= context
;
848 void bt_ctf_event_get(struct bt_ctf_event
*event
)
853 void bt_ctf_event_put(struct bt_ctf_event
*event
)
859 void bt_ctf_event_class_destroy(struct bt_object
*obj
)
861 struct bt_ctf_event_class
*event_class
;
863 event_class
= container_of(obj
, struct bt_ctf_event_class
, base
);
864 bt_ctf_attributes_destroy(event_class
->attributes
);
865 bt_put(event_class
->context
);
866 bt_put(event_class
->fields
);
871 void bt_ctf_event_destroy(struct bt_object
*obj
)
873 struct bt_ctf_event
*event
;
875 event
= container_of(obj
, struct bt_ctf_event
, base
);
876 if (!event
->base
.parent
) {
878 * Event was keeping a reference to its class since it shared no
879 * common ancestor with it to guarantee they would both have the
882 bt_put(event
->event_class
);
884 bt_put(event
->event_header
);
885 bt_put(event
->context_payload
);
886 bt_put(event
->fields_payload
);
891 int set_integer_field_value(struct bt_ctf_field
* field
, uint64_t value
)
894 struct bt_ctf_field_type
*field_type
= NULL
;
901 if (!bt_ctf_field_validate(field
)) {
902 /* Payload already set, skip! (not an error) */
906 field_type
= bt_ctf_field_get_type(field
);
909 if (bt_ctf_field_type_get_type_id(field_type
) != CTF_TYPE_INTEGER
) {
910 /* Not an integer and the value is unset, error. */
915 if (bt_ctf_field_type_integer_get_signed(field_type
)) {
916 ret
= bt_ctf_field_signed_integer_set_value(field
, (int64_t) value
);
918 /* Value is out of range, error. */
922 ret
= bt_ctf_field_unsigned_integer_set_value(field
, value
);
924 /* Value is out of range, error. */
934 void bt_ctf_event_class_freeze(struct bt_ctf_event_class
*event_class
)
937 event_class
->frozen
= 1;
938 bt_ctf_field_type_freeze(event_class
->context
);
939 bt_ctf_field_type_freeze(event_class
->fields
);
940 bt_ctf_attributes_freeze(event_class
->attributes
);
944 int bt_ctf_event_class_serialize(struct bt_ctf_event_class
*event_class
,
945 struct metadata_context
*context
)
950 struct bt_value
*attr_value
= NULL
;
955 context
->current_indentation_level
= 1;
956 g_string_assign(context
->field_name
, "");
957 g_string_append(context
->string
, "event {\n");
958 count
= bt_ctf_event_class_get_attribute_count(event_class
);
965 for (i
= 0; i
< count
; ++i
) {
966 const char *attr_name
= NULL
;
968 attr_name
= bt_ctf_event_class_get_attribute_name(
970 attr_value
= bt_ctf_event_class_get_attribute_value(
973 if (!attr_name
|| !attr_value
) {
978 switch (bt_value_get_type(attr_value
)) {
979 case BT_VALUE_TYPE_INTEGER
:
983 ret
= bt_value_integer_get(attr_value
, &value
);
989 g_string_append_printf(context
->string
,
990 "\t%s = %" PRId64
";\n", attr_name
, value
);
994 case BT_VALUE_TYPE_STRING
:
998 ret
= bt_value_string_get(attr_value
, &value
);
1004 g_string_append_printf(context
->string
,
1005 "\t%s = \"%s\";\n", attr_name
, value
);
1010 /* should never happen */
1018 if (event_class
->context
) {
1019 g_string_append(context
->string
, "\tcontext := ");
1020 ret
= bt_ctf_field_type_serialize(event_class
->context
,
1025 g_string_append(context
->string
, ";\n");
1028 if (event_class
->fields
) {
1029 g_string_append(context
->string
, "\tfields := ");
1030 ret
= bt_ctf_field_type_serialize(event_class
->fields
, context
);
1034 g_string_append(context
->string
, ";\n");
1037 g_string_append(context
->string
, "};\n\n");
1039 context
->current_indentation_level
= 0;
1044 void bt_ctf_event_class_set_native_byte_order(
1045 struct bt_ctf_event_class
*event_class
,
1052 assert(byte_order
== 0 || byte_order
== LITTLE_ENDIAN
||
1053 byte_order
== BIG_ENDIAN
);
1055 bt_ctf_field_type_set_native_byte_order(event_class
->context
,
1057 bt_ctf_field_type_set_native_byte_order(event_class
->fields
,
1062 int bt_ctf_event_validate(struct bt_ctf_event
*event
)
1064 /* Make sure each field's payload has been set */
1068 ret
= bt_ctf_field_validate(event
->event_header
);
1073 ret
= bt_ctf_field_validate(event
->fields_payload
);
1078 if (event
->event_class
->context
) {
1079 ret
= bt_ctf_field_validate(event
->context_payload
);
1086 int bt_ctf_event_serialize(struct bt_ctf_event
*event
,
1087 struct ctf_stream_pos
*pos
)
1093 if (event
->context_payload
) {
1094 ret
= bt_ctf_field_serialize(event
->context_payload
, pos
);
1100 if (event
->fields_payload
) {
1101 ret
= bt_ctf_field_serialize(event
->fields_payload
, pos
);
1111 int bt_ctf_event_populate_event_header(struct bt_ctf_event
*event
)
1114 struct bt_ctf_field
*id_field
= NULL
, *timestamp_field
= NULL
;
1121 id_field
= bt_ctf_field_structure_get_field(event
->event_header
, "id");
1123 ret
= set_integer_field_value(id_field
,
1124 (uint64_t) bt_ctf_event_class_get_id(
1125 event
->event_class
));
1131 timestamp_field
= bt_ctf_field_structure_get_field(event
->event_header
,
1133 if (timestamp_field
) {
1134 struct bt_ctf_field_type
*timestamp_field_type
=
1135 bt_ctf_field_get_type(timestamp_field
);
1136 struct bt_ctf_clock
*mapped_clock
;
1138 assert(timestamp_field_type
);
1139 mapped_clock
= bt_ctf_field_type_integer_get_mapped_clock(
1140 timestamp_field_type
);
1141 bt_put(timestamp_field_type
);
1145 ret
= bt_ctf_clock_get_time(mapped_clock
, ×tamp
);
1146 bt_put(mapped_clock
);
1151 ret
= set_integer_field_value(timestamp_field
,
1160 bt_put(timestamp_field
);
1164 struct bt_ctf_event
*bt_ctf_event_copy(struct bt_ctf_event
*event
)
1166 struct bt_ctf_event
*copy
= NULL
;
1172 copy
= g_new0(struct bt_ctf_event
, 1);
1177 bt_object_init(copy
, bt_ctf_event_destroy
);
1178 copy
->event_class
= bt_get(event
->event_class
);
1180 if (event
->event_header
) {
1181 copy
->event_header
= bt_ctf_field_copy(event
->event_header
);
1183 if (!copy
->event_header
) {
1188 if (event
->context_payload
) {
1189 copy
->context_payload
= bt_ctf_field_copy(
1190 event
->context_payload
);
1192 if (!copy
->context_payload
) {
1197 if (event
->fields_payload
) {
1198 copy
->fields_payload
= bt_ctf_field_copy(event
->fields_payload
);
1200 if (!copy
->fields_payload
) {