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/compiler.h>
41 void bt_ctf_event_class_destroy(struct bt_ctf_ref
*ref
);
43 void bt_ctf_event_destroy(struct bt_ctf_ref
*ref
);
45 int set_integer_field_value(struct bt_ctf_field
*field
, uint64_t value
);
47 struct bt_ctf_event_class
*bt_ctf_event_class_create(const char *name
)
49 struct bt_ctf_event_class
*event_class
= NULL
;
51 if (validate_identifier(name
)) {
55 event_class
= g_new0(struct bt_ctf_event_class
, 1);
60 bt_ctf_ref_init(&event_class
->ref_count
);
61 event_class
->name
= g_quark_from_string(name
);
66 const char *bt_ctf_event_class_get_name(struct bt_ctf_event_class
*event_class
)
68 const char *name
= NULL
;
74 name
= g_quark_to_string(event_class
->name
);
79 int64_t bt_ctf_event_class_get_id(struct bt_ctf_event_class
*event_class
)
83 if (!event_class
|| !event_class
->id_set
) {
88 ret
= (int64_t) event_class
->id
;
93 int bt_ctf_event_class_set_id(struct bt_ctf_event_class
*event_class
,
103 if (event_class
->stream_class
) {
105 * We don't allow changing the id if the event class has already
106 * been added to a stream class.
112 event_class
->id
= id
;
113 event_class
->id_set
= 1;
118 struct bt_ctf_stream_class
*bt_ctf_event_class_get_stream_class(
119 struct bt_ctf_event_class
*event_class
)
121 struct bt_ctf_stream_class
*stream_class
= NULL
;
127 stream_class
= event_class
->stream_class
;
128 bt_ctf_stream_class_get(stream_class
);
133 int bt_ctf_event_class_add_field(struct bt_ctf_event_class
*event_class
,
134 struct bt_ctf_field_type
*type
,
139 if (!event_class
|| !type
|| validate_identifier(name
) ||
140 event_class
->frozen
) {
145 if (!event_class
->fields
) {
146 event_class
->fields
= bt_ctf_field_type_structure_create();
147 if (!event_class
->fields
) {
153 ret
= bt_ctf_field_type_structure_add_field(event_class
->fields
,
159 int bt_ctf_event_class_get_field_count(
160 struct bt_ctf_event_class
*event_class
)
169 ret
= bt_ctf_field_type_structure_get_field_count(event_class
->fields
);
174 int bt_ctf_event_class_get_field(struct bt_ctf_event_class
*event_class
,
175 const char **field_name
, struct bt_ctf_field_type
**field_type
,
180 if (!event_class
|| index
< 0) {
185 ret
= bt_ctf_field_type_structure_get_field(event_class
->fields
,
186 field_name
, field_type
, index
);
191 struct bt_ctf_field_type
*bt_ctf_event_class_get_field_by_name(
192 struct bt_ctf_event_class
*event_class
, const char *name
)
195 struct bt_ctf_field_type
*field_type
= NULL
;
197 if (!event_class
|| !name
) {
201 name_quark
= g_quark_try_string(name
);
207 * No need to increment field_type's reference count since getting it
208 * from the structure already does.
210 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
211 event_class
->fields
, name
);
216 struct bt_ctf_field_type
*bt_ctf_event_class_get_context_type(
217 struct bt_ctf_event_class
*event_class
)
219 struct bt_ctf_field_type
*context_type
= NULL
;
221 if (!event_class
|| !event_class
->context
) {
225 bt_ctf_field_type_get(event_class
->context
);
226 context_type
= event_class
->context
;
231 int bt_ctf_event_class_set_context_type(
232 struct bt_ctf_event_class
*event_class
,
233 struct bt_ctf_field_type
*context
)
237 if (!event_class
|| !context
|| event_class
->frozen
) {
242 if (bt_ctf_field_type_get_type_id(context
) != CTF_TYPE_STRUCT
) {
247 bt_ctf_field_type_get(context
);
248 bt_ctf_field_type_put(event_class
->context
);
249 event_class
->context
= context
;
255 void bt_ctf_event_class_get(struct bt_ctf_event_class
*event_class
)
261 bt_ctf_ref_get(&event_class
->ref_count
);
264 void bt_ctf_event_class_put(struct bt_ctf_event_class
*event_class
)
270 bt_ctf_ref_put(&event_class
->ref_count
, bt_ctf_event_class_destroy
);
273 struct bt_ctf_event
*bt_ctf_event_create(struct bt_ctf_event_class
*event_class
)
275 struct bt_ctf_event
*event
= NULL
;
281 event
= g_new0(struct bt_ctf_event
, 1);
286 bt_ctf_ref_init(&event
->ref_count
);
287 bt_ctf_event_class_get(event_class
);
288 bt_ctf_event_class_freeze(event_class
);
289 event
->event_class
= event_class
;
291 assert(event_class
->stream_class
);
292 assert(event_class
->stream_class
->event_header_type
);
294 event
->event_header
= bt_ctf_field_create(
295 event_class
->stream_class
->event_header_type
);
296 if (!event
->event_header
) {
299 if (event_class
->context
) {
300 event
->context_payload
= bt_ctf_field_create(
301 event_class
->context
);
302 if (!event
->context_payload
) {
306 event
->fields_payload
= bt_ctf_field_create(event_class
->fields
);
307 if (!event
->fields_payload
) {
312 * Freeze the stream class since the event header must not be changed
315 bt_ctf_stream_class_freeze(event_class
->stream_class
);
318 bt_ctf_event_destroy(&event
->ref_count
);
322 struct bt_ctf_event_class
*bt_ctf_event_get_class(struct bt_ctf_event
*event
)
324 struct bt_ctf_event_class
*event_class
= NULL
;
330 event_class
= event
->event_class
;
331 bt_ctf_event_class_get(event_class
);
336 struct bt_ctf_clock
*bt_ctf_event_get_clock(struct bt_ctf_event
*event
)
338 struct bt_ctf_clock
*clock
= NULL
;
339 struct bt_ctf_event_class
*event_class
;
340 struct bt_ctf_stream_class
*stream_class
;
346 event_class
= bt_ctf_event_get_class(event
);
351 stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
353 goto error_put_event_class
;
356 clock
= bt_ctf_stream_class_get_clock(stream_class
);
358 goto error_put_stream_class
;
361 error_put_stream_class
:
362 bt_ctf_stream_class_put(stream_class
);
363 error_put_event_class
:
364 bt_ctf_event_class_put(event_class
);
369 int bt_ctf_event_set_payload(struct bt_ctf_event
*event
,
371 struct bt_ctf_field
*value
)
375 if (!event
|| !value
|| validate_identifier(name
)) {
380 ret
= bt_ctf_field_structure_set_field(event
->fields_payload
,
387 struct bt_ctf_field
*bt_ctf_event_get_payload(struct bt_ctf_event
*event
,
390 struct bt_ctf_field
*field
= NULL
;
392 if (!event
|| !name
) {
396 field
= bt_ctf_field_structure_get_field(event
->fields_payload
, name
);
401 struct bt_ctf_field
*bt_ctf_event_get_payload_by_index(
402 struct bt_ctf_event
*event
, int index
)
404 struct bt_ctf_field
*field
= NULL
;
406 if (!event
|| index
< 0) {
410 field
= bt_ctf_field_structure_get_field_by_index(event
->fields_payload
,
416 struct bt_ctf_field
*bt_ctf_event_get_event_header(
417 struct bt_ctf_event
*event
)
419 struct bt_ctf_field
*header
= NULL
;
421 if (!event
|| !event
->event_header
) {
425 header
= event
->event_header
;
426 bt_ctf_field_get(header
);
431 int bt_ctf_event_set_event_header(struct bt_ctf_event
*event
,
432 struct bt_ctf_field
*header
)
435 struct bt_ctf_field_type
*field_type
= NULL
;
437 if (!event
|| !header
) {
442 /* Could be NULL since an event class doesn't own a stream class */
443 if (!event
->event_class
->stream_class
) {
449 * Ensure the provided header's type matches the one registered to the
452 field_type
= bt_ctf_field_get_type(header
);
453 if (field_type
!= event
->event_class
->stream_class
->event_header_type
) {
458 bt_ctf_field_get(header
);
459 bt_ctf_field_put(event
->event_header
);
460 event
->event_header
= header
;
463 bt_ctf_field_type_put(field_type
);
468 struct bt_ctf_field
*bt_ctf_event_get_event_context(
469 struct bt_ctf_event
*event
)
471 struct bt_ctf_field
*context
= NULL
;
473 if (!event
|| !event
->context_payload
) {
477 context
= event
->context_payload
;
478 bt_ctf_field_get(context
);
483 int bt_ctf_event_set_event_context(struct bt_ctf_event
*event
,
484 struct bt_ctf_field
*context
)
487 struct bt_ctf_field_type
*field_type
= NULL
;
489 if (!event
|| !context
) {
494 field_type
= bt_ctf_field_get_type(context
);
495 if (field_type
!= event
->event_class
->context
) {
500 bt_ctf_field_get(context
);
501 bt_ctf_field_put(event
->context_payload
);
502 event
->context_payload
= context
;
505 bt_ctf_field_type_put(field_type
);
510 void bt_ctf_event_get(struct bt_ctf_event
*event
)
516 bt_ctf_ref_get(&event
->ref_count
);
519 void bt_ctf_event_put(struct bt_ctf_event
*event
)
525 bt_ctf_ref_put(&event
->ref_count
, bt_ctf_event_destroy
);
529 void bt_ctf_event_class_destroy(struct bt_ctf_ref
*ref
)
531 struct bt_ctf_event_class
*event_class
;
538 * Don't call put() on the stream class. See comment in
539 * bt_ctf_event_class_set_stream_class for explanation.
541 event_class
= container_of(ref
, struct bt_ctf_event_class
, ref_count
);
542 if (event_class
->context
) {
543 bt_ctf_field_type_put(event_class
->context
);
545 if (event_class
->fields
) {
546 bt_ctf_field_type_put(event_class
->fields
);
552 void bt_ctf_event_destroy(struct bt_ctf_ref
*ref
)
554 struct bt_ctf_event
*event
;
560 event
= container_of(ref
, struct bt_ctf_event
,
562 if (event
->event_class
) {
563 bt_ctf_event_class_put(event
->event_class
);
565 if (event
->event_header
) {
566 bt_ctf_field_put(event
->event_header
);
568 if (event
->context_payload
) {
569 bt_ctf_field_put(event
->context_payload
);
571 if (event
->fields_payload
) {
572 bt_ctf_field_put(event
->fields_payload
);
578 int set_integer_field_value(struct bt_ctf_field
* field
, uint64_t value
)
581 struct bt_ctf_field_type
*field_type
= NULL
;
588 if (!bt_ctf_field_validate(field
)) {
589 /* Payload already set, skip! (not an error) */
593 field_type
= bt_ctf_field_get_type(field
);
596 if (bt_ctf_field_type_get_type_id(field_type
) != CTF_TYPE_INTEGER
) {
597 /* Not an integer and the value is unset, error. */
602 if (bt_ctf_field_type_integer_get_signed(field_type
)) {
603 ret
= bt_ctf_field_signed_integer_set_value(field
, (int64_t) value
);
605 /* Value is out of range, error. */
609 ret
= bt_ctf_field_unsigned_integer_set_value(field
, value
);
611 /* Value is out of range, error. */
616 bt_ctf_field_type_put(field_type
);
621 void bt_ctf_event_class_freeze(struct bt_ctf_event_class
*event_class
)
624 event_class
->frozen
= 1;
625 bt_ctf_field_type_freeze(event_class
->context
);
626 bt_ctf_field_type_freeze(event_class
->fields
);
630 int bt_ctf_event_class_set_stream_class(struct bt_ctf_event_class
*event_class
,
631 struct bt_ctf_stream_class
*stream_class
)
640 /* Allow a NULL stream_class to unset the current stream_class */
641 if (stream_class
&& event_class
->stream_class
) {
646 event_class
->stream_class
= stream_class
;
648 * We don't get() the stream_class since doing so would introduce
649 * a circular ownership between event classes and stream classes.
651 * A stream class will always unset itself from its events before
652 * being destroyed. This ensures that a user won't get a pointer
653 * to a stale stream class instance from an event class.
660 int bt_ctf_event_class_serialize(struct bt_ctf_event_class
*event_class
,
661 struct metadata_context
*context
)
668 stream_id
= bt_ctf_stream_class_get_id(event_class
->stream_class
);
674 context
->current_indentation_level
= 1;
675 g_string_assign(context
->field_name
, "");
676 g_string_append_printf(context
->string
,
677 "event {\n\tname = \"%s\";\n\tid = %u;\n\tstream_id = %" PRId64
";\n",
678 g_quark_to_string(event_class
->name
),
682 if (event_class
->context
) {
683 g_string_append(context
->string
, "\tcontext := ");
684 ret
= bt_ctf_field_type_serialize(event_class
->context
,
689 g_string_append(context
->string
, ";\n");
692 if (event_class
->fields
) {
693 g_string_append(context
->string
, "\tfields := ");
694 ret
= bt_ctf_field_type_serialize(event_class
->fields
, context
);
698 g_string_append(context
->string
, ";\n");
701 g_string_append(context
->string
, "};\n\n");
703 context
->current_indentation_level
= 0;
708 int bt_ctf_event_validate(struct bt_ctf_event
*event
)
710 /* Make sure each field's payload has been set */
714 ret
= bt_ctf_field_validate(event
->event_header
);
719 ret
= bt_ctf_field_validate(event
->fields_payload
);
724 if (event
->event_class
->context
) {
725 ret
= bt_ctf_field_validate(event
->context_payload
);
732 int bt_ctf_event_serialize(struct bt_ctf_event
*event
,
733 struct ctf_stream_pos
*pos
)
739 if (event
->context_payload
) {
740 ret
= bt_ctf_field_serialize(event
->context_payload
, pos
);
746 if (event
->fields_payload
) {
747 ret
= bt_ctf_field_serialize(event
->fields_payload
, pos
);
757 int bt_ctf_event_set_timestamp(struct bt_ctf_event
*event
,
763 if (event
->timestamp
) {
768 event
->timestamp
= timestamp
;
774 uint64_t bt_ctf_event_get_timestamp(struct bt_ctf_event
*event
)
777 return event
->timestamp
;
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 ret
= set_integer_field_value(timestamp_field
,
804 (uint64_t) event
->timestamp
);
811 bt_ctf_field_put(id_field
);
813 if (timestamp_field
) {
814 bt_ctf_field_put(timestamp_field
);