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
;
176 if (event_class
->stream_class
) {
178 * We don't allow changing the id if the event class has already
179 * been added to a stream class.
185 obj
= bt_ctf_attributes_get_field_value(event_class
->attributes
,
186 BT_CTF_EVENT_CLASS_ATTR_ID_INDEX
);
191 if (bt_value_integer_set(obj
, id
)) {
201 int bt_ctf_event_class_set_attribute(
202 struct bt_ctf_event_class
*event_class
, const char *name
,
203 struct bt_value
*value
)
207 if (!event_class
|| !name
|| !value
|| event_class
->frozen
) {
212 if (!strcmp(name
, "id") || !strcmp(name
, "loglevel")) {
213 if (!bt_value_is_integer(value
)) {
217 } else if (!strcmp(name
, "name") || !strcmp(name
, "model.emf.uri")) {
218 if (!bt_value_is_string(value
)) {
223 /* unknown attribute */
228 /* "id" special case: >= 0 */
229 if (!strcmp(name
, "id")) {
232 ret
= bt_value_integer_get(value
, &val
);
244 ret
= bt_ctf_attributes_set_field_value(event_class
->attributes
,
251 int bt_ctf_event_class_get_attribute_count(
252 struct bt_ctf_event_class
*event_class
)
261 ret
= bt_ctf_attributes_get_count(event_class
->attributes
);
268 bt_ctf_event_class_get_attribute_name(
269 struct bt_ctf_event_class
*event_class
, int index
)
278 ret
= bt_ctf_attributes_get_field_name(event_class
->attributes
, index
);
285 bt_ctf_event_class_get_attribute_value(struct bt_ctf_event_class
*event_class
,
288 struct bt_value
*ret
;
295 ret
= bt_ctf_attributes_get_field_value(event_class
->attributes
, index
);
302 bt_ctf_event_class_get_attribute_value_by_name(
303 struct bt_ctf_event_class
*event_class
, const char *name
)
305 struct bt_value
*ret
;
307 if (!event_class
|| !name
) {
312 ret
= bt_ctf_attributes_get_field_value_by_name(event_class
->attributes
,
320 struct bt_ctf_stream_class
*bt_ctf_event_class_get_stream_class(
321 struct bt_ctf_event_class
*event_class
)
323 struct bt_ctf_stream_class
*stream_class
= NULL
;
329 stream_class
= event_class
->stream_class
;
330 bt_get(stream_class
);
335 struct bt_ctf_field_type
*bt_ctf_event_class_get_payload_type(
336 struct bt_ctf_event_class
*event_class
)
338 struct bt_ctf_field_type
*payload
= NULL
;
344 bt_get(event_class
->fields
);
345 payload
= event_class
->fields
;
350 int bt_ctf_event_class_set_payload_type(struct bt_ctf_event_class
*event_class
,
351 struct bt_ctf_field_type
*payload
)
355 if (!event_class
|| !payload
||
356 bt_ctf_field_type_get_type_id(payload
) != CTF_TYPE_STRUCT
) {
362 bt_put(event_class
->fields
);
363 event_class
->fields
= payload
;
368 int bt_ctf_event_class_add_field(struct bt_ctf_event_class
*event_class
,
369 struct bt_ctf_field_type
*type
,
374 if (!event_class
|| !type
|| bt_ctf_validate_identifier(name
) ||
375 event_class
->frozen
) {
380 if (bt_ctf_field_type_get_type_id(event_class
->fields
) !=
386 ret
= bt_ctf_field_type_structure_add_field(event_class
->fields
,
392 int bt_ctf_event_class_get_field_count(
393 struct bt_ctf_event_class
*event_class
)
402 if (bt_ctf_field_type_get_type_id(event_class
->fields
) !=
408 ret
= bt_ctf_field_type_structure_get_field_count(event_class
->fields
);
413 int bt_ctf_event_class_get_field(struct bt_ctf_event_class
*event_class
,
414 const char **field_name
, struct bt_ctf_field_type
**field_type
,
419 if (!event_class
|| index
< 0) {
424 if (bt_ctf_field_type_get_type_id(event_class
->fields
) !=
430 ret
= bt_ctf_field_type_structure_get_field(event_class
->fields
,
431 field_name
, field_type
, index
);
436 struct bt_ctf_field_type
*bt_ctf_event_class_get_field_by_name(
437 struct bt_ctf_event_class
*event_class
, const char *name
)
440 struct bt_ctf_field_type
*field_type
= NULL
;
442 if (!event_class
|| !name
) {
446 if (bt_ctf_field_type_get_type_id(event_class
->fields
) !=
451 name_quark
= g_quark_try_string(name
);
457 * No need to increment field_type's reference count since getting it
458 * from the structure already does.
460 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
461 event_class
->fields
, name
);
466 struct bt_ctf_field_type
*bt_ctf_event_class_get_context_type(
467 struct bt_ctf_event_class
*event_class
)
469 struct bt_ctf_field_type
*context_type
= NULL
;
471 if (!event_class
|| !event_class
->context
) {
475 bt_get(event_class
->context
);
476 context_type
= event_class
->context
;
481 int bt_ctf_event_class_set_context_type(
482 struct bt_ctf_event_class
*event_class
,
483 struct bt_ctf_field_type
*context
)
487 if (!event_class
|| !context
|| event_class
->frozen
) {
492 if (bt_ctf_field_type_get_type_id(context
) != CTF_TYPE_STRUCT
) {
498 bt_put(event_class
->context
);
499 event_class
->context
= context
;
505 void bt_ctf_event_class_get(struct bt_ctf_event_class
*event_class
)
510 void bt_ctf_event_class_put(struct bt_ctf_event_class
*event_class
)
516 int bt_ctf_event_class_set_stream_id(struct bt_ctf_event_class
*event_class
,
520 struct bt_value
*obj
;
522 obj
= bt_value_integer_create_init(stream_id
);
529 ret
= bt_ctf_attributes_set_field_value(event_class
->attributes
,
537 struct bt_ctf_event
*bt_ctf_event_create(struct bt_ctf_event_class
*event_class
)
539 struct bt_ctf_event
*event
= NULL
;
546 * The event class does not keep ownership of the stream class to
547 * which it as been added. Therefore, it can't assume it has been
548 * set. However, we disallow the creation of an event if its
549 * associated stream class has been reclaimed.
551 if (!event_class
->stream_class
) {
554 assert(event_class
->stream_class
->event_header_type
);
555 event
= g_new0(struct bt_ctf_event
, 1);
560 bt_object_init(event
, bt_ctf_event_destroy
);
562 bt_ctf_event_class_freeze(event_class
);
563 event
->event_class
= event_class
;
565 event
->event_header
= bt_ctf_field_create(
566 event_class
->stream_class
->event_header_type
);
567 if (!event
->event_header
) {
570 if (event_class
->context
) {
571 event
->context_payload
= bt_ctf_field_create(
572 event_class
->context
);
573 if (!event
->context_payload
) {
577 event
->fields_payload
= bt_ctf_field_create(event_class
->fields
);
578 if (!event
->fields_payload
) {
583 * Freeze the stream class since the event header must not be changed
586 bt_ctf_stream_class_freeze(event_class
->stream_class
);
594 struct bt_ctf_event_class
*bt_ctf_event_get_class(struct bt_ctf_event
*event
)
596 struct bt_ctf_event_class
*event_class
= NULL
;
602 event_class
= event
->event_class
;
608 struct bt_ctf_stream
*bt_ctf_event_get_stream(struct bt_ctf_event
*event
)
610 struct bt_ctf_stream
*stream
= NULL
;
616 stream
= event
->stream
;
622 struct bt_ctf_clock
*bt_ctf_event_get_clock(struct bt_ctf_event
*event
)
624 struct bt_ctf_clock
*clock
= NULL
;
625 struct bt_ctf_event_class
*event_class
;
626 struct bt_ctf_stream_class
*stream_class
;
632 event_class
= bt_ctf_event_get_class(event
);
637 stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
639 goto error_put_event_class
;
642 clock
= bt_ctf_stream_class_get_clock(stream_class
);
644 goto error_put_stream_class
;
647 error_put_stream_class
:
648 bt_put(stream_class
);
649 error_put_event_class
:
655 int bt_ctf_event_set_payload(struct bt_ctf_event
*event
,
657 struct bt_ctf_field
*payload
)
661 if (!event
|| !payload
) {
667 ret
= bt_ctf_field_structure_set_field(event
->fields_payload
,
670 struct bt_ctf_field_type
*payload_type
;
672 payload_type
= bt_ctf_field_get_type(payload
);
673 if (payload_type
== event
->event_class
->fields
) {
674 bt_put(event
->fields_payload
);
676 event
->fields_payload
= payload
;
681 bt_put(payload_type
);
687 struct bt_ctf_field
*bt_ctf_event_get_payload_field(struct bt_ctf_event
*event
)
689 struct bt_ctf_field
*payload
= NULL
;
691 if (!event
|| !event
->fields_payload
) {
695 payload
= event
->fields_payload
;
701 int bt_ctf_event_set_payload_field(struct bt_ctf_event
*event
,
702 struct bt_ctf_field
*payload
)
705 struct bt_ctf_field_type
*payload_type
= NULL
;
707 if (!event
|| !payload
) {
712 payload_type
= bt_ctf_field_get_type(payload
);
718 if (bt_ctf_field_type_get_type_id(payload_type
) != CTF_TYPE_STRUCT
) {
724 bt_put(event
->fields_payload
);
725 event
->fields_payload
= payload
;
728 bt_put(payload_type
);
732 struct bt_ctf_field
*bt_ctf_event_get_payload(struct bt_ctf_event
*event
,
735 struct bt_ctf_field
*field
= NULL
;
742 field
= bt_ctf_field_structure_get_field(event
->fields_payload
,
745 field
= event
->fields_payload
;
752 struct bt_ctf_field
*bt_ctf_event_get_payload_by_index(
753 struct bt_ctf_event
*event
, int index
)
755 struct bt_ctf_field
*field
= NULL
;
757 if (!event
|| index
< 0) {
761 field
= bt_ctf_field_structure_get_field_by_index(event
->fields_payload
,
767 struct bt_ctf_field
*bt_ctf_event_get_header(
768 struct bt_ctf_event
*event
)
770 struct bt_ctf_field
*header
= NULL
;
772 if (!event
|| !event
->event_header
) {
776 header
= event
->event_header
;
782 int bt_ctf_event_set_header(struct bt_ctf_event
*event
,
783 struct bt_ctf_field
*header
)
786 struct bt_ctf_field_type
*field_type
= NULL
;
788 if (!event
|| !header
) {
793 /* Could be NULL since an event class doesn't own a stream class */
794 if (!event
->event_class
->stream_class
) {
800 * Ensure the provided header's type matches the one registered to the
803 field_type
= bt_ctf_field_get_type(header
);
804 if (field_type
!= event
->event_class
->stream_class
->event_header_type
) {
810 bt_put(event
->event_header
);
811 event
->event_header
= header
;
817 struct bt_ctf_field
*bt_ctf_event_get_event_context(
818 struct bt_ctf_event
*event
)
820 struct bt_ctf_field
*context
= NULL
;
822 if (!event
|| !event
->context_payload
) {
826 context
= event
->context_payload
;
832 int bt_ctf_event_set_event_context(struct bt_ctf_event
*event
,
833 struct bt_ctf_field
*context
)
836 struct bt_ctf_field_type
*field_type
= NULL
;
838 if (!event
|| !context
) {
843 field_type
= bt_ctf_field_get_type(context
);
844 if (field_type
!= event
->event_class
->context
) {
850 bt_put(event
->context_payload
);
851 event
->context_payload
= context
;
857 void bt_ctf_event_get(struct bt_ctf_event
*event
)
862 void bt_ctf_event_put(struct bt_ctf_event
*event
)
868 void bt_ctf_event_class_destroy(struct bt_object
*obj
)
870 struct bt_ctf_event_class
*event_class
;
873 * Don't call put() on the stream class. See comment in
874 * bt_ctf_event_class_set_stream_class for explanation.
876 event_class
= container_of(obj
, struct bt_ctf_event_class
, base
);
877 bt_ctf_attributes_destroy(event_class
->attributes
);
878 bt_put(event_class
->context
);
879 bt_put(event_class
->fields
);
884 void bt_ctf_event_destroy(struct bt_object
*obj
)
886 struct bt_ctf_event
*event
;
888 event
= container_of(obj
, struct bt_ctf_event
, base
);
889 bt_put(event
->event_class
);
890 bt_put(event
->event_header
);
891 bt_put(event
->context_payload
);
892 bt_put(event
->fields_payload
);
897 int set_integer_field_value(struct bt_ctf_field
* field
, uint64_t value
)
900 struct bt_ctf_field_type
*field_type
= NULL
;
907 if (!bt_ctf_field_validate(field
)) {
908 /* Payload already set, skip! (not an error) */
912 field_type
= bt_ctf_field_get_type(field
);
915 if (bt_ctf_field_type_get_type_id(field_type
) != CTF_TYPE_INTEGER
) {
916 /* Not an integer and the value is unset, error. */
921 if (bt_ctf_field_type_integer_get_signed(field_type
)) {
922 ret
= bt_ctf_field_signed_integer_set_value(field
, (int64_t) value
);
924 /* Value is out of range, error. */
928 ret
= bt_ctf_field_unsigned_integer_set_value(field
, value
);
930 /* Value is out of range, error. */
940 void bt_ctf_event_class_freeze(struct bt_ctf_event_class
*event_class
)
943 event_class
->frozen
= 1;
944 bt_ctf_field_type_freeze(event_class
->context
);
945 bt_ctf_field_type_freeze(event_class
->fields
);
946 bt_ctf_attributes_freeze(event_class
->attributes
);
950 int bt_ctf_event_class_set_stream_class(struct bt_ctf_event_class
*event_class
,
951 struct bt_ctf_stream_class
*stream_class
)
960 /* Allow a NULL stream_class to unset the current stream_class */
961 if (stream_class
&& event_class
->stream_class
) {
966 event_class
->stream_class
= stream_class
;
968 * We don't get() the stream_class since doing so would introduce
969 * a circular ownership between event classes and stream classes.
971 * A stream class will always unset itself from its events before
972 * being destroyed. This ensures that a user won't get a pointer
973 * to a stale stream class instance from an event class.
980 int bt_ctf_event_class_serialize(struct bt_ctf_event_class
*event_class
,
981 struct metadata_context
*context
)
986 struct bt_value
*attr_value
= NULL
;
991 context
->current_indentation_level
= 1;
992 g_string_assign(context
->field_name
, "");
993 g_string_append(context
->string
, "event {\n");
994 count
= bt_ctf_event_class_get_attribute_count(event_class
);
1001 for (i
= 0; i
< count
; ++i
) {
1002 const char *attr_name
= NULL
;
1004 attr_name
= bt_ctf_event_class_get_attribute_name(
1006 attr_value
= bt_ctf_event_class_get_attribute_value(
1009 if (!attr_name
|| !attr_value
) {
1014 switch (bt_value_get_type(attr_value
)) {
1015 case BT_VALUE_TYPE_INTEGER
:
1019 ret
= bt_value_integer_get(attr_value
, &value
);
1025 g_string_append_printf(context
->string
,
1026 "\t%s = %" PRId64
";\n", attr_name
, value
);
1030 case BT_VALUE_TYPE_STRING
:
1034 ret
= bt_value_string_get(attr_value
, &value
);
1040 g_string_append_printf(context
->string
,
1041 "\t%s = \"%s\";\n", attr_name
, value
);
1046 /* should never happen */
1054 if (event_class
->context
) {
1055 g_string_append(context
->string
, "\tcontext := ");
1056 ret
= bt_ctf_field_type_serialize(event_class
->context
,
1061 g_string_append(context
->string
, ";\n");
1064 if (event_class
->fields
) {
1065 g_string_append(context
->string
, "\tfields := ");
1066 ret
= bt_ctf_field_type_serialize(event_class
->fields
, context
);
1070 g_string_append(context
->string
, ";\n");
1073 g_string_append(context
->string
, "};\n\n");
1075 context
->current_indentation_level
= 0;
1080 void bt_ctf_event_class_set_native_byte_order(
1081 struct bt_ctf_event_class
*event_class
,
1088 bt_ctf_field_type_set_native_byte_order(event_class
->context
,
1090 bt_ctf_field_type_set_native_byte_order(event_class
->fields
,
1095 int bt_ctf_event_validate(struct bt_ctf_event
*event
)
1097 /* Make sure each field's payload has been set */
1101 ret
= bt_ctf_field_validate(event
->event_header
);
1106 ret
= bt_ctf_field_validate(event
->fields_payload
);
1111 if (event
->event_class
->context
) {
1112 ret
= bt_ctf_field_validate(event
->context_payload
);
1119 int bt_ctf_event_serialize(struct bt_ctf_event
*event
,
1120 struct ctf_stream_pos
*pos
)
1126 if (event
->context_payload
) {
1127 ret
= bt_ctf_field_serialize(event
->context_payload
, pos
);
1133 if (event
->fields_payload
) {
1134 ret
= bt_ctf_field_serialize(event
->fields_payload
, pos
);
1144 int bt_ctf_event_populate_event_header(struct bt_ctf_event
*event
)
1147 struct bt_ctf_field
*id_field
= NULL
, *timestamp_field
= NULL
;
1154 id_field
= bt_ctf_field_structure_get_field(event
->event_header
, "id");
1156 ret
= set_integer_field_value(id_field
,
1157 (uint64_t) bt_ctf_event_class_get_id(
1158 event
->event_class
));
1164 timestamp_field
= bt_ctf_field_structure_get_field(event
->event_header
,
1166 if (timestamp_field
) {
1167 struct bt_ctf_field_type
*timestamp_field_type
=
1168 bt_ctf_field_get_type(timestamp_field
);
1169 struct bt_ctf_clock
*mapped_clock
;
1171 assert(timestamp_field_type
);
1172 mapped_clock
= bt_ctf_field_type_integer_get_mapped_clock(
1173 timestamp_field_type
);
1174 bt_put(timestamp_field_type
);
1176 uint64_t timestamp
= bt_ctf_clock_get_time(
1179 bt_put(mapped_clock
);
1180 if (timestamp
== (uint64_t) -1ULL) {
1184 ret
= set_integer_field_value(timestamp_field
,
1193 bt_put(timestamp_field
);
1198 int bt_ctf_event_set_stream(struct bt_ctf_event
*event
,
1199 struct bt_ctf_stream
*stream
)
1208 if (event
->stream
&& stream
) {
1209 /* Already attached to a stream */
1214 event
->stream
= stream
;
1219 struct bt_ctf_event
*bt_ctf_event_copy(struct bt_ctf_event
*event
)
1221 struct bt_ctf_event
*copy
= NULL
;
1227 copy
= g_new0(struct bt_ctf_event
, 1);
1232 bt_object_init(copy
, bt_ctf_event_destroy
);
1233 copy
->event_class
= event
->event_class
;
1234 bt_get(copy
->event_class
);
1235 copy
->stream
= event
->stream
;
1237 if (event
->event_header
) {
1238 copy
->event_header
= bt_ctf_field_copy(event
->event_header
);
1240 if (!copy
->event_header
) {
1245 if (event
->context_payload
) {
1246 copy
->context_payload
= bt_ctf_field_copy(
1247 event
->context_payload
);
1249 if (!copy
->context_payload
) {
1254 if (event
->fields_payload
) {
1255 copy
->fields_payload
= bt_ctf_field_copy(event
->fields_payload
);
1257 if (!copy
->fields_payload
) {