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
;
864 * Don't call put() on the stream class. See comment in
865 * bt_ctf_event_class_set_stream_class for explanation.
867 event_class
= container_of(obj
, struct bt_ctf_event_class
, base
);
868 bt_ctf_attributes_destroy(event_class
->attributes
);
869 bt_put(event_class
->context
);
870 bt_put(event_class
->fields
);
875 void bt_ctf_event_destroy(struct bt_object
*obj
)
877 struct bt_ctf_event
*event
;
879 event
= container_of(obj
, struct bt_ctf_event
, base
);
880 if (!event
->base
.parent
) {
882 * Event was keeping a reference to its class since it shared no
883 * common ancestor with it to guarantee they would both have the
886 bt_put(event
->event_class
);
888 bt_put(event
->event_header
);
889 bt_put(event
->context_payload
);
890 bt_put(event
->fields_payload
);
895 int set_integer_field_value(struct bt_ctf_field
* field
, uint64_t value
)
898 struct bt_ctf_field_type
*field_type
= NULL
;
905 if (!bt_ctf_field_validate(field
)) {
906 /* Payload already set, skip! (not an error) */
910 field_type
= bt_ctf_field_get_type(field
);
913 if (bt_ctf_field_type_get_type_id(field_type
) != CTF_TYPE_INTEGER
) {
914 /* Not an integer and the value is unset, error. */
919 if (bt_ctf_field_type_integer_get_signed(field_type
)) {
920 ret
= bt_ctf_field_signed_integer_set_value(field
, (int64_t) value
);
922 /* Value is out of range, error. */
926 ret
= bt_ctf_field_unsigned_integer_set_value(field
, value
);
928 /* Value is out of range, error. */
938 void bt_ctf_event_class_freeze(struct bt_ctf_event_class
*event_class
)
941 event_class
->frozen
= 1;
942 bt_ctf_field_type_freeze(event_class
->context
);
943 bt_ctf_field_type_freeze(event_class
->fields
);
944 bt_ctf_attributes_freeze(event_class
->attributes
);
948 int bt_ctf_event_class_serialize(struct bt_ctf_event_class
*event_class
,
949 struct metadata_context
*context
)
954 struct bt_value
*attr_value
= NULL
;
959 context
->current_indentation_level
= 1;
960 g_string_assign(context
->field_name
, "");
961 g_string_append(context
->string
, "event {\n");
962 count
= bt_ctf_event_class_get_attribute_count(event_class
);
969 for (i
= 0; i
< count
; ++i
) {
970 const char *attr_name
= NULL
;
972 attr_name
= bt_ctf_event_class_get_attribute_name(
974 attr_value
= bt_ctf_event_class_get_attribute_value(
977 if (!attr_name
|| !attr_value
) {
982 switch (bt_value_get_type(attr_value
)) {
983 case BT_VALUE_TYPE_INTEGER
:
987 ret
= bt_value_integer_get(attr_value
, &value
);
993 g_string_append_printf(context
->string
,
994 "\t%s = %" PRId64
";\n", attr_name
, value
);
998 case BT_VALUE_TYPE_STRING
:
1002 ret
= bt_value_string_get(attr_value
, &value
);
1008 g_string_append_printf(context
->string
,
1009 "\t%s = \"%s\";\n", attr_name
, value
);
1014 /* should never happen */
1022 if (event_class
->context
) {
1023 g_string_append(context
->string
, "\tcontext := ");
1024 ret
= bt_ctf_field_type_serialize(event_class
->context
,
1029 g_string_append(context
->string
, ";\n");
1032 if (event_class
->fields
) {
1033 g_string_append(context
->string
, "\tfields := ");
1034 ret
= bt_ctf_field_type_serialize(event_class
->fields
, context
);
1038 g_string_append(context
->string
, ";\n");
1041 g_string_append(context
->string
, "};\n\n");
1043 context
->current_indentation_level
= 0;
1048 void bt_ctf_event_class_set_native_byte_order(
1049 struct bt_ctf_event_class
*event_class
,
1056 bt_ctf_field_type_set_native_byte_order(event_class
->context
,
1058 bt_ctf_field_type_set_native_byte_order(event_class
->fields
,
1063 int bt_ctf_event_validate(struct bt_ctf_event
*event
)
1065 /* Make sure each field's payload has been set */
1069 ret
= bt_ctf_field_validate(event
->event_header
);
1074 ret
= bt_ctf_field_validate(event
->fields_payload
);
1079 if (event
->event_class
->context
) {
1080 ret
= bt_ctf_field_validate(event
->context_payload
);
1087 int bt_ctf_event_serialize(struct bt_ctf_event
*event
,
1088 struct ctf_stream_pos
*pos
)
1094 if (event
->context_payload
) {
1095 ret
= bt_ctf_field_serialize(event
->context_payload
, pos
);
1101 if (event
->fields_payload
) {
1102 ret
= bt_ctf_field_serialize(event
->fields_payload
, pos
);
1112 int bt_ctf_event_populate_event_header(struct bt_ctf_event
*event
)
1115 struct bt_ctf_field
*id_field
= NULL
, *timestamp_field
= NULL
;
1122 id_field
= bt_ctf_field_structure_get_field(event
->event_header
, "id");
1124 ret
= set_integer_field_value(id_field
,
1125 (uint64_t) bt_ctf_event_class_get_id(
1126 event
->event_class
));
1132 timestamp_field
= bt_ctf_field_structure_get_field(event
->event_header
,
1134 if (timestamp_field
) {
1135 struct bt_ctf_field_type
*timestamp_field_type
=
1136 bt_ctf_field_get_type(timestamp_field
);
1137 struct bt_ctf_clock
*mapped_clock
;
1139 assert(timestamp_field_type
);
1140 mapped_clock
= bt_ctf_field_type_integer_get_mapped_clock(
1141 timestamp_field_type
);
1142 bt_put(timestamp_field_type
);
1146 ret
= bt_ctf_clock_get_time(mapped_clock
, ×tamp
);
1147 bt_put(mapped_clock
);
1152 ret
= set_integer_field_value(timestamp_field
,
1161 bt_put(timestamp_field
);
1165 struct bt_ctf_event
*bt_ctf_event_copy(struct bt_ctf_event
*event
)
1167 struct bt_ctf_event
*copy
= NULL
;
1173 copy
= g_new0(struct bt_ctf_event
, 1);
1178 bt_object_init(copy
, bt_ctf_event_destroy
);
1179 copy
->event_class
= bt_get(event
->event_class
);
1181 if (event
->event_header
) {
1182 copy
->event_header
= bt_ctf_field_copy(event
->event_header
);
1184 if (!copy
->event_header
) {
1189 if (event
->context_payload
) {
1190 copy
->context_payload
= bt_ctf_field_copy(
1191 event
->context_payload
);
1193 if (!copy
->context_payload
) {
1198 if (event
->fields_payload
) {
1199 copy
->fields_payload
= bt_ctf_field_copy(event
->fields_payload
);
1201 if (!copy
->fields_payload
) {