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
)
50 struct bt_ctf_event_class
*event_class
= NULL
;
52 if (bt_ctf_validate_identifier(name
)) {
56 event_class
= g_new0(struct bt_ctf_event_class
, 1);
61 bt_ctf_ref_init(&event_class
->ref_count
);
62 event_class
->name
= g_quark_from_string(name
);
67 const char *bt_ctf_event_class_get_name(struct bt_ctf_event_class
*event_class
)
69 const char *name
= NULL
;
75 name
= g_quark_to_string(event_class
->name
);
80 int64_t bt_ctf_event_class_get_id(struct bt_ctf_event_class
*event_class
)
84 if (!event_class
|| !event_class
->id_set
) {
89 ret
= (int64_t) event_class
->id
;
94 int bt_ctf_event_class_set_id(struct bt_ctf_event_class
*event_class
,
104 if (event_class
->stream_class
) {
106 * We don't allow changing the id if the event class has already
107 * been added to a stream class.
113 event_class
->id
= id
;
114 event_class
->id_set
= 1;
119 struct bt_ctf_stream_class
*bt_ctf_event_class_get_stream_class(
120 struct bt_ctf_event_class
*event_class
)
122 struct bt_ctf_stream_class
*stream_class
= NULL
;
128 stream_class
= event_class
->stream_class
;
129 bt_ctf_stream_class_get(stream_class
);
134 int bt_ctf_event_class_add_field(struct bt_ctf_event_class
*event_class
,
135 struct bt_ctf_field_type
*type
,
140 if (!event_class
|| !type
|| bt_ctf_validate_identifier(name
) ||
141 event_class
->frozen
) {
146 if (!event_class
->fields
) {
147 event_class
->fields
= bt_ctf_field_type_structure_create();
148 if (!event_class
->fields
) {
154 ret
= bt_ctf_field_type_structure_add_field(event_class
->fields
,
160 int bt_ctf_event_class_get_field_count(
161 struct bt_ctf_event_class
*event_class
)
170 ret
= bt_ctf_field_type_structure_get_field_count(event_class
->fields
);
175 int bt_ctf_event_class_get_field(struct bt_ctf_event_class
*event_class
,
176 const char **field_name
, struct bt_ctf_field_type
**field_type
,
181 if (!event_class
|| index
< 0) {
186 ret
= bt_ctf_field_type_structure_get_field(event_class
->fields
,
187 field_name
, field_type
, index
);
192 struct bt_ctf_field_type
*bt_ctf_event_class_get_field_by_name(
193 struct bt_ctf_event_class
*event_class
, const char *name
)
196 struct bt_ctf_field_type
*field_type
= NULL
;
198 if (!event_class
|| !name
) {
202 name_quark
= g_quark_try_string(name
);
208 * No need to increment field_type's reference count since getting it
209 * from the structure already does.
211 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
212 event_class
->fields
, name
);
217 struct bt_ctf_field_type
*bt_ctf_event_class_get_context_type(
218 struct bt_ctf_event_class
*event_class
)
220 struct bt_ctf_field_type
*context_type
= NULL
;
222 if (!event_class
|| !event_class
->context
) {
226 bt_ctf_field_type_get(event_class
->context
);
227 context_type
= event_class
->context
;
232 int bt_ctf_event_class_set_context_type(
233 struct bt_ctf_event_class
*event_class
,
234 struct bt_ctf_field_type
*context
)
238 if (!event_class
|| !context
|| event_class
->frozen
) {
243 if (bt_ctf_field_type_get_type_id(context
) != CTF_TYPE_STRUCT
) {
248 bt_ctf_field_type_get(context
);
249 bt_ctf_field_type_put(event_class
->context
);
250 event_class
->context
= context
;
256 void bt_ctf_event_class_get(struct bt_ctf_event_class
*event_class
)
262 bt_ctf_ref_get(&event_class
->ref_count
);
265 void bt_ctf_event_class_put(struct bt_ctf_event_class
*event_class
)
271 bt_ctf_ref_put(&event_class
->ref_count
, bt_ctf_event_class_destroy
);
274 struct bt_ctf_event
*bt_ctf_event_create(struct bt_ctf_event_class
*event_class
)
276 struct bt_ctf_event
*event
= NULL
;
282 event
= g_new0(struct bt_ctf_event
, 1);
287 bt_ctf_ref_init(&event
->ref_count
);
288 bt_ctf_event_class_get(event_class
);
289 bt_ctf_event_class_freeze(event_class
);
290 event
->event_class
= event_class
;
293 * The event class does not keep ownership of the stream class to
294 * which it as been added. Therefore, it can't assume it has been
295 * set. However, we disallow the creation of an event if its
296 * associated stream class has been reclaimed.
298 if (!event_class
->stream_class
) {
301 assert(event_class
->stream_class
->event_header_type
);
303 event
->event_header
= bt_ctf_field_create(
304 event_class
->stream_class
->event_header_type
);
305 if (!event
->event_header
) {
308 if (event_class
->context
) {
309 event
->context_payload
= bt_ctf_field_create(
310 event_class
->context
);
311 if (!event
->context_payload
) {
315 event
->fields_payload
= bt_ctf_field_create(event_class
->fields
);
316 if (!event
->fields_payload
) {
321 * Freeze the stream class since the event header must not be changed
324 bt_ctf_stream_class_freeze(event_class
->stream_class
);
328 bt_ctf_event_destroy(&event
->ref_count
);
332 struct bt_ctf_event_class
*bt_ctf_event_get_class(struct bt_ctf_event
*event
)
334 struct bt_ctf_event_class
*event_class
= NULL
;
340 event_class
= event
->event_class
;
341 bt_ctf_event_class_get(event_class
);
346 struct bt_ctf_clock
*bt_ctf_event_get_clock(struct bt_ctf_event
*event
)
348 struct bt_ctf_clock
*clock
= NULL
;
349 struct bt_ctf_event_class
*event_class
;
350 struct bt_ctf_stream_class
*stream_class
;
356 event_class
= bt_ctf_event_get_class(event
);
361 stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
363 goto error_put_event_class
;
366 clock
= bt_ctf_stream_class_get_clock(stream_class
);
368 goto error_put_stream_class
;
371 error_put_stream_class
:
372 bt_ctf_stream_class_put(stream_class
);
373 error_put_event_class
:
374 bt_ctf_event_class_put(event_class
);
379 int bt_ctf_event_set_payload(struct bt_ctf_event
*event
,
381 struct bt_ctf_field
*value
)
385 if (!event
|| !value
|| bt_ctf_validate_identifier(name
)) {
390 ret
= bt_ctf_field_structure_set_field(event
->fields_payload
,
397 struct bt_ctf_field
*bt_ctf_event_get_payload(struct bt_ctf_event
*event
,
400 struct bt_ctf_field
*field
= NULL
;
402 if (!event
|| !name
) {
406 field
= bt_ctf_field_structure_get_field(event
->fields_payload
, name
);
411 struct bt_ctf_field
*bt_ctf_event_get_payload_by_index(
412 struct bt_ctf_event
*event
, int index
)
414 struct bt_ctf_field
*field
= NULL
;
416 if (!event
|| index
< 0) {
420 field
= bt_ctf_field_structure_get_field_by_index(event
->fields_payload
,
426 struct bt_ctf_field
*bt_ctf_event_get_event_header(
427 struct bt_ctf_event
*event
)
429 struct bt_ctf_field
*header
= NULL
;
431 if (!event
|| !event
->event_header
) {
435 header
= event
->event_header
;
436 bt_ctf_field_get(header
);
441 int bt_ctf_event_set_event_header(struct bt_ctf_event
*event
,
442 struct bt_ctf_field
*header
)
445 struct bt_ctf_field_type
*field_type
= NULL
;
447 if (!event
|| !header
) {
452 /* Could be NULL since an event class doesn't own a stream class */
453 if (!event
->event_class
->stream_class
) {
459 * Ensure the provided header's type matches the one registered to the
462 field_type
= bt_ctf_field_get_type(header
);
463 if (field_type
!= event
->event_class
->stream_class
->event_header_type
) {
468 bt_ctf_field_get(header
);
469 bt_ctf_field_put(event
->event_header
);
470 event
->event_header
= header
;
473 bt_ctf_field_type_put(field_type
);
478 struct bt_ctf_field
*bt_ctf_event_get_event_context(
479 struct bt_ctf_event
*event
)
481 struct bt_ctf_field
*context
= NULL
;
483 if (!event
|| !event
->context_payload
) {
487 context
= event
->context_payload
;
488 bt_ctf_field_get(context
);
493 int bt_ctf_event_set_event_context(struct bt_ctf_event
*event
,
494 struct bt_ctf_field
*context
)
497 struct bt_ctf_field_type
*field_type
= NULL
;
499 if (!event
|| !context
) {
504 field_type
= bt_ctf_field_get_type(context
);
505 if (field_type
!= event
->event_class
->context
) {
510 bt_ctf_field_get(context
);
511 bt_ctf_field_put(event
->context_payload
);
512 event
->context_payload
= context
;
515 bt_ctf_field_type_put(field_type
);
520 void bt_ctf_event_get(struct bt_ctf_event
*event
)
526 bt_ctf_ref_get(&event
->ref_count
);
529 void bt_ctf_event_put(struct bt_ctf_event
*event
)
535 bt_ctf_ref_put(&event
->ref_count
, bt_ctf_event_destroy
);
539 void bt_ctf_event_class_destroy(struct bt_ctf_ref
*ref
)
541 struct bt_ctf_event_class
*event_class
;
548 * Don't call put() on the stream class. See comment in
549 * bt_ctf_event_class_set_stream_class for explanation.
551 event_class
= container_of(ref
, struct bt_ctf_event_class
, ref_count
);
552 if (event_class
->context
) {
553 bt_ctf_field_type_put(event_class
->context
);
555 if (event_class
->fields
) {
556 bt_ctf_field_type_put(event_class
->fields
);
562 void bt_ctf_event_destroy(struct bt_ctf_ref
*ref
)
564 struct bt_ctf_event
*event
;
570 event
= container_of(ref
, struct bt_ctf_event
,
572 if (event
->event_class
) {
573 bt_ctf_event_class_put(event
->event_class
);
575 if (event
->event_header
) {
576 bt_ctf_field_put(event
->event_header
);
578 if (event
->context_payload
) {
579 bt_ctf_field_put(event
->context_payload
);
581 if (event
->fields_payload
) {
582 bt_ctf_field_put(event
->fields_payload
);
588 int set_integer_field_value(struct bt_ctf_field
* field
, uint64_t value
)
591 struct bt_ctf_field_type
*field_type
= NULL
;
598 if (!bt_ctf_field_validate(field
)) {
599 /* Payload already set, skip! (not an error) */
603 field_type
= bt_ctf_field_get_type(field
);
606 if (bt_ctf_field_type_get_type_id(field_type
) != CTF_TYPE_INTEGER
) {
607 /* Not an integer and the value is unset, error. */
612 if (bt_ctf_field_type_integer_get_signed(field_type
)) {
613 ret
= bt_ctf_field_signed_integer_set_value(field
, (int64_t) value
);
615 /* Value is out of range, error. */
619 ret
= bt_ctf_field_unsigned_integer_set_value(field
, value
);
621 /* Value is out of range, error. */
626 bt_ctf_field_type_put(field_type
);
631 void bt_ctf_event_class_freeze(struct bt_ctf_event_class
*event_class
)
634 event_class
->frozen
= 1;
635 bt_ctf_field_type_freeze(event_class
->context
);
636 bt_ctf_field_type_freeze(event_class
->fields
);
640 int bt_ctf_event_class_set_stream_class(struct bt_ctf_event_class
*event_class
,
641 struct bt_ctf_stream_class
*stream_class
)
650 /* Allow a NULL stream_class to unset the current stream_class */
651 if (stream_class
&& event_class
->stream_class
) {
656 event_class
->stream_class
= stream_class
;
658 * We don't get() the stream_class since doing so would introduce
659 * a circular ownership between event classes and stream classes.
661 * A stream class will always unset itself from its events before
662 * being destroyed. This ensures that a user won't get a pointer
663 * to a stale stream class instance from an event class.
670 int bt_ctf_event_class_serialize(struct bt_ctf_event_class
*event_class
,
671 struct metadata_context
*context
)
678 stream_id
= bt_ctf_stream_class_get_id(event_class
->stream_class
);
684 context
->current_indentation_level
= 1;
685 g_string_assign(context
->field_name
, "");
686 g_string_append_printf(context
->string
,
687 "event {\n\tname = \"%s\";\n\tid = %u;\n\tstream_id = %" PRId64
";\n",
688 g_quark_to_string(event_class
->name
),
692 if (event_class
->context
) {
693 g_string_append(context
->string
, "\tcontext := ");
694 ret
= bt_ctf_field_type_serialize(event_class
->context
,
699 g_string_append(context
->string
, ";\n");
702 if (event_class
->fields
) {
703 g_string_append(context
->string
, "\tfields := ");
704 ret
= bt_ctf_field_type_serialize(event_class
->fields
, context
);
708 g_string_append(context
->string
, ";\n");
711 g_string_append(context
->string
, "};\n\n");
713 context
->current_indentation_level
= 0;
717 void bt_ctf_event_class_set_native_byte_order(
718 struct bt_ctf_event_class
*event_class
,
725 bt_ctf_field_type_set_native_byte_order(event_class
->context
,
727 bt_ctf_field_type_set_native_byte_order(event_class
->fields
,
732 int bt_ctf_event_validate(struct bt_ctf_event
*event
)
734 /* Make sure each field's payload has been set */
738 ret
= bt_ctf_field_validate(event
->event_header
);
743 ret
= bt_ctf_field_validate(event
->fields_payload
);
748 if (event
->event_class
->context
) {
749 ret
= bt_ctf_field_validate(event
->context_payload
);
756 int bt_ctf_event_serialize(struct bt_ctf_event
*event
,
757 struct ctf_stream_pos
*pos
)
763 if (event
->context_payload
) {
764 ret
= bt_ctf_field_serialize(event
->context_payload
, pos
);
770 if (event
->fields_payload
) {
771 ret
= bt_ctf_field_serialize(event
->fields_payload
, pos
);
781 int bt_ctf_event_populate_event_header(struct bt_ctf_event
*event
)
784 struct bt_ctf_field
*id_field
= NULL
, *timestamp_field
= NULL
;
791 id_field
= bt_ctf_field_structure_get_field(event
->event_header
, "id");
793 ret
= set_integer_field_value(id_field
,
794 (uint64_t) event
->event_class
->id
);
800 timestamp_field
= bt_ctf_field_structure_get_field(event
->event_header
,
802 if (timestamp_field
) {
803 struct bt_ctf_field_type
*timestamp_field_type
=
804 bt_ctf_field_get_type(timestamp_field
);
805 struct bt_ctf_clock
*mapped_clock
;
807 assert(timestamp_field_type
);
808 mapped_clock
= bt_ctf_field_type_integer_get_mapped_clock(
809 timestamp_field_type
);
810 bt_ctf_field_type_put(timestamp_field_type
);
812 uint64_t timestamp
= bt_ctf_clock_get_time(
815 bt_ctf_clock_put(mapped_clock
);
816 if (timestamp
== (uint64_t) -1ULL) {
820 ret
= set_integer_field_value(timestamp_field
,
829 bt_ctf_field_put(id_field
);
831 if (timestamp_field
) {
832 bt_ctf_field_put(timestamp_field
);