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_clock
*bt_ctf_event_get_clock(struct bt_ctf_event
*event
)
621 struct bt_ctf_clock
*clock
= NULL
;
622 struct bt_ctf_event_class
*event_class
;
623 struct bt_ctf_stream_class
*stream_class
;
629 event_class
= bt_ctf_event_get_class(event
);
634 stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
636 goto error_put_event_class
;
639 clock
= bt_ctf_stream_class_get_clock(stream_class
);
641 goto error_put_stream_class
;
644 error_put_stream_class
:
645 bt_ctf_stream_class_put(stream_class
);
646 error_put_event_class
:
647 bt_ctf_event_class_put(event_class
);
652 int bt_ctf_event_set_payload(struct bt_ctf_event
*event
,
654 struct bt_ctf_field
*payload
)
658 if (!event
|| !payload
) {
664 ret
= bt_ctf_field_structure_set_field(event
->fields_payload
,
667 struct bt_ctf_field_type
*payload_type
;
669 payload_type
= bt_ctf_field_get_type(payload
);
670 if (payload_type
== event
->event_class
->fields
) {
671 bt_ctf_field_put(event
->fields_payload
);
672 bt_ctf_field_get(payload
);
673 event
->fields_payload
= payload
;
678 bt_ctf_field_type_put(payload_type
);
685 struct bt_ctf_field
*bt_ctf_event_get_payload(struct bt_ctf_event
*event
,
688 struct bt_ctf_field
*field
= NULL
;
695 field
= bt_ctf_field_structure_get_field(event
->fields_payload
,
698 field
= event
->fields_payload
;
699 bt_ctf_field_get(field
);
705 struct bt_ctf_field
*bt_ctf_event_get_payload_by_index(
706 struct bt_ctf_event
*event
, int index
)
708 struct bt_ctf_field
*field
= NULL
;
710 if (!event
|| index
< 0) {
714 field
= bt_ctf_field_structure_get_field_by_index(event
->fields_payload
,
720 struct bt_ctf_field
*bt_ctf_event_get_event_header(
721 struct bt_ctf_event
*event
)
723 struct bt_ctf_field
*header
= NULL
;
725 if (!event
|| !event
->event_header
) {
729 header
= event
->event_header
;
730 bt_ctf_field_get(header
);
735 int bt_ctf_event_set_event_header(struct bt_ctf_event
*event
,
736 struct bt_ctf_field
*header
)
739 struct bt_ctf_field_type
*field_type
= NULL
;
741 if (!event
|| !header
) {
746 /* Could be NULL since an event class doesn't own a stream class */
747 if (!event
->event_class
->stream_class
) {
753 * Ensure the provided header's type matches the one registered to the
756 field_type
= bt_ctf_field_get_type(header
);
757 if (field_type
!= event
->event_class
->stream_class
->event_header_type
) {
762 bt_ctf_field_get(header
);
763 bt_ctf_field_put(event
->event_header
);
764 event
->event_header
= header
;
767 bt_ctf_field_type_put(field_type
);
772 struct bt_ctf_field
*bt_ctf_event_get_event_context(
773 struct bt_ctf_event
*event
)
775 struct bt_ctf_field
*context
= NULL
;
777 if (!event
|| !event
->context_payload
) {
781 context
= event
->context_payload
;
782 bt_ctf_field_get(context
);
787 int bt_ctf_event_set_event_context(struct bt_ctf_event
*event
,
788 struct bt_ctf_field
*context
)
791 struct bt_ctf_field_type
*field_type
= NULL
;
793 if (!event
|| !context
) {
798 field_type
= bt_ctf_field_get_type(context
);
799 if (field_type
!= event
->event_class
->context
) {
804 bt_ctf_field_get(context
);
805 bt_ctf_field_put(event
->context_payload
);
806 event
->context_payload
= context
;
809 bt_ctf_field_type_put(field_type
);
814 void bt_ctf_event_get(struct bt_ctf_event
*event
)
820 bt_ctf_ref_get(&event
->ref_count
);
823 void bt_ctf_event_put(struct bt_ctf_event
*event
)
829 bt_ctf_ref_put(&event
->ref_count
, bt_ctf_event_destroy
);
833 void bt_ctf_event_class_destroy(struct bt_ctf_ref
*ref
)
835 struct bt_ctf_event_class
*event_class
;
842 * Don't call put() on the stream class. See comment in
843 * bt_ctf_event_class_set_stream_class for explanation.
845 event_class
= container_of(ref
, struct bt_ctf_event_class
, ref_count
);
846 if (event_class
->attributes
) {
847 bt_ctf_attributes_destroy(event_class
->attributes
);
849 if (event_class
->context
) {
850 bt_ctf_field_type_put(event_class
->context
);
852 if (event_class
->fields
) {
853 bt_ctf_field_type_put(event_class
->fields
);
859 void bt_ctf_event_destroy(struct bt_ctf_ref
*ref
)
861 struct bt_ctf_event
*event
;
867 event
= container_of(ref
, struct bt_ctf_event
,
869 if (event
->event_class
) {
870 bt_ctf_event_class_put(event
->event_class
);
872 if (event
->event_header
) {
873 bt_ctf_field_put(event
->event_header
);
875 if (event
->context_payload
) {
876 bt_ctf_field_put(event
->context_payload
);
878 if (event
->fields_payload
) {
879 bt_ctf_field_put(event
->fields_payload
);
885 int set_integer_field_value(struct bt_ctf_field
* field
, uint64_t value
)
888 struct bt_ctf_field_type
*field_type
= NULL
;
895 if (!bt_ctf_field_validate(field
)) {
896 /* Payload already set, skip! (not an error) */
900 field_type
= bt_ctf_field_get_type(field
);
903 if (bt_ctf_field_type_get_type_id(field_type
) != CTF_TYPE_INTEGER
) {
904 /* Not an integer and the value is unset, error. */
909 if (bt_ctf_field_type_integer_get_signed(field_type
)) {
910 ret
= bt_ctf_field_signed_integer_set_value(field
, (int64_t) value
);
912 /* Value is out of range, error. */
916 ret
= bt_ctf_field_unsigned_integer_set_value(field
, value
);
918 /* Value is out of range, error. */
923 bt_ctf_field_type_put(field_type
);
928 void bt_ctf_event_class_freeze(struct bt_ctf_event_class
*event_class
)
931 event_class
->frozen
= 1;
932 bt_ctf_field_type_freeze(event_class
->context
);
933 bt_ctf_field_type_freeze(event_class
->fields
);
934 bt_ctf_attributes_freeze(event_class
->attributes
);
938 int bt_ctf_event_class_set_stream_class(struct bt_ctf_event_class
*event_class
,
939 struct bt_ctf_stream_class
*stream_class
)
948 /* Allow a NULL stream_class to unset the current stream_class */
949 if (stream_class
&& event_class
->stream_class
) {
954 event_class
->stream_class
= stream_class
;
956 * We don't get() the stream_class since doing so would introduce
957 * a circular ownership between event classes and stream classes.
959 * A stream class will always unset itself from its events before
960 * being destroyed. This ensures that a user won't get a pointer
961 * to a stale stream class instance from an event class.
968 int bt_ctf_event_class_serialize(struct bt_ctf_event_class
*event_class
,
969 struct metadata_context
*context
)
974 struct bt_object
*attr_value
= NULL
;
979 context
->current_indentation_level
= 1;
980 g_string_assign(context
->field_name
, "");
981 g_string_append(context
->string
, "event {\n");
982 count
= bt_ctf_event_class_get_attribute_count(event_class
);
989 for (i
= 0; i
< count
; ++i
) {
990 const char *attr_name
= NULL
;
992 attr_name
= bt_ctf_event_class_get_attribute_name(
994 attr_value
= bt_ctf_event_class_get_attribute_value(
997 if (!attr_name
|| !attr_value
) {
1002 switch (bt_object_get_type(attr_value
)) {
1003 case BT_OBJECT_TYPE_INTEGER
:
1007 ret
= bt_object_integer_get(attr_value
, &value
);
1013 g_string_append_printf(context
->string
,
1014 "\t%s = %" PRId64
";\n", attr_name
, value
);
1018 case BT_OBJECT_TYPE_STRING
:
1022 ret
= bt_object_string_get(attr_value
, &value
);
1028 g_string_append_printf(context
->string
,
1029 "\t%s = \"%s\";\n", attr_name
, value
);
1034 /* should never happen */
1039 BT_OBJECT_PUT(attr_value
);
1042 if (event_class
->context
) {
1043 g_string_append(context
->string
, "\tcontext := ");
1044 ret
= bt_ctf_field_type_serialize(event_class
->context
,
1049 g_string_append(context
->string
, ";\n");
1052 if (event_class
->fields
) {
1053 g_string_append(context
->string
, "\tfields := ");
1054 ret
= bt_ctf_field_type_serialize(event_class
->fields
, context
);
1058 g_string_append(context
->string
, ";\n");
1061 g_string_append(context
->string
, "};\n\n");
1063 context
->current_indentation_level
= 0;
1064 BT_OBJECT_PUT(attr_value
);
1068 void bt_ctf_event_class_set_native_byte_order(
1069 struct bt_ctf_event_class
*event_class
,
1076 bt_ctf_field_type_set_native_byte_order(event_class
->context
,
1078 bt_ctf_field_type_set_native_byte_order(event_class
->fields
,
1083 int bt_ctf_event_validate(struct bt_ctf_event
*event
)
1085 /* Make sure each field's payload has been set */
1089 ret
= bt_ctf_field_validate(event
->event_header
);
1094 ret
= bt_ctf_field_validate(event
->fields_payload
);
1099 if (event
->event_class
->context
) {
1100 ret
= bt_ctf_field_validate(event
->context_payload
);
1107 int bt_ctf_event_serialize(struct bt_ctf_event
*event
,
1108 struct ctf_stream_pos
*pos
)
1114 if (event
->context_payload
) {
1115 ret
= bt_ctf_field_serialize(event
->context_payload
, pos
);
1121 if (event
->fields_payload
) {
1122 ret
= bt_ctf_field_serialize(event
->fields_payload
, pos
);
1132 int bt_ctf_event_populate_event_header(struct bt_ctf_event
*event
)
1135 struct bt_ctf_field
*id_field
= NULL
, *timestamp_field
= NULL
;
1142 id_field
= bt_ctf_field_structure_get_field(event
->event_header
, "id");
1144 ret
= set_integer_field_value(id_field
,
1145 (uint64_t) bt_ctf_event_class_get_id(
1146 event
->event_class
));
1152 timestamp_field
= bt_ctf_field_structure_get_field(event
->event_header
,
1154 if (timestamp_field
) {
1155 struct bt_ctf_field_type
*timestamp_field_type
=
1156 bt_ctf_field_get_type(timestamp_field
);
1157 struct bt_ctf_clock
*mapped_clock
;
1159 assert(timestamp_field_type
);
1160 mapped_clock
= bt_ctf_field_type_integer_get_mapped_clock(
1161 timestamp_field_type
);
1162 bt_ctf_field_type_put(timestamp_field_type
);
1164 uint64_t timestamp
= bt_ctf_clock_get_time(
1167 bt_ctf_clock_put(mapped_clock
);
1168 if (timestamp
== (uint64_t) -1ULL) {
1172 ret
= set_integer_field_value(timestamp_field
,
1181 bt_ctf_field_put(id_field
);
1183 if (timestamp_field
) {
1184 bt_ctf_field_put(timestamp_field
);