2 * Babeltrace - CTF notification iterator
4 * Copyright (c) 2015-2016 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2015-2016 Philippe Proulx <pproulx@efficios.com>
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <babeltrace/ctf-ir/field-types.h>
34 #include <babeltrace/ctf-ir/field-path.h>
35 #include <babeltrace/ctf-ir/fields.h>
36 #include <babeltrace/ctf-ir/stream-class.h>
37 #include <babeltrace/ctf-ir/packet.h>
38 #include <babeltrace/ctf-ir/stream.h>
39 #include <babeltrace/ctf-ir/clock.h>
40 #include <babeltrace/ctf-ir/event-class.h>
41 #include <babeltrace/plugin/notification/packet.h>
42 #include <babeltrace/plugin/notification/event.h>
43 #include <babeltrace/plugin/notification/stream.h>
44 #include <babeltrace/ref.h>
47 #define PRINT_ERR_STREAM notit->err_stream
48 #define PRINT_PREFIX "ctf-notif-iter"
51 #include "notif-iter.h"
52 #include "../btr/btr.h"
54 #define BYTES_TO_BITS(x) ((x) * 8)
56 struct bt_ctf_notif_iter
;
58 /* A visit stack entry */
61 * Current base field, one of:
69 * Field is owned by this.
71 struct bt_ctf_field
*base
;
73 /* index of next field to set */
79 /* Entries (struct stack_entry *) (top is last element) */
86 STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
,
87 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
88 STATE_AFTER_TRACE_PACKET_HEADER
,
89 STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
,
90 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
91 STATE_AFTER_STREAM_PACKET_CONTEXT
,
92 STATE_EMIT_NOTIF_NEW_PACKET
,
93 STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
,
94 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
,
95 STATE_AFTER_STREAM_EVENT_HEADER
,
96 STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
,
97 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
,
98 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
,
99 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
,
100 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
101 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
102 STATE_EMIT_NOTIF_EVENT
,
103 STATE_EMIT_NOTIF_END_OF_PACKET
,
104 STATE_SKIP_PACKET_PADDING
,
107 /* CTF notification iterator */
108 struct bt_ctf_notif_iter
{
112 /* Error stream (may be NULL) */
116 * Current dynamic scope field pointer.
118 * This is set when a dynamic scope field is first created by
119 * btr_compound_begin_cb(). It points to one of the fields in
122 struct bt_ctf_field
**cur_dscope_field
;
124 /* Trace and classes (owned by this) */
126 struct bt_ctf_trace
*trace
;
127 struct bt_ctf_stream_class
*stream_class
;
128 struct bt_ctf_event_class
*event_class
;
131 /* Current packet (NULL if not created yet) */
132 struct bt_ctf_packet
*packet
;
134 /* Database of current dynamic scopes (owned by this) */
136 struct bt_ctf_field
*trace_packet_header
;
137 struct bt_ctf_field
*stream_packet_context
;
138 struct bt_ctf_field
*stream_event_header
;
139 struct bt_ctf_field
*stream_event_context
;
140 struct bt_ctf_field
*event_context
;
141 struct bt_ctf_field
*event_payload
;
147 /* Current medium buffer data */
149 /* Last address provided by medium */
152 /* Buffer size provided by medium (bytes) */
155 /* Offset within whole packet of addr (bits) */
156 size_t packet_offset
;
158 /* Current position from addr (bits) */
162 /* Binary type reader */
163 struct bt_ctf_btr
*btr
;
165 /* Current medium data */
167 struct bt_ctf_notif_iter_medium_ops medops
;
168 size_t max_request_sz
;
172 /* Current packet size (bits) (-1 if unknown) */
173 int64_t cur_packet_size
;
175 /* Current content size (bits) (-1 if unknown) */
176 int64_t cur_content_size
;
178 /* bt_ctf_clock to uint64_t. */
179 GHashTable
*clock_states
;
183 int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter
*notit
);
186 void stack_entry_free_func(gpointer data
)
188 struct stack_entry
*entry
= data
;
195 struct stack
*stack_new(struct bt_ctf_notif_iter
*notit
)
197 struct stack
*stack
= NULL
;
199 stack
= g_new0(struct stack
, 1);
204 stack
->entries
= g_ptr_array_new_with_free_func(stack_entry_free_func
);
205 if (!stack
->entries
) {
216 void stack_destroy(struct stack
*stack
)
219 g_ptr_array_free(stack
->entries
, TRUE
);
224 int stack_push(struct stack
*stack
, struct bt_ctf_field
*base
)
227 struct stack_entry
*entry
;
231 entry
= g_new0(struct stack_entry
, 1);
237 entry
->base
= bt_get(base
);
238 g_ptr_array_add(stack
->entries
, entry
);
245 unsigned int stack_size(struct stack
*stack
)
249 return stack
->entries
->len
;
253 void stack_pop(struct stack
*stack
)
256 assert(stack_size(stack
));
257 g_ptr_array_remove_index(stack
->entries
, stack
->entries
->len
- 1);
261 struct stack_entry
*stack_top(struct stack
*stack
)
264 assert(stack_size(stack
));
266 return g_ptr_array_index(stack
->entries
, stack
->entries
->len
- 1);
270 bool stack_empty(struct stack
*stack
)
272 return stack_size(stack
) == 0;
276 void stack_clear(struct stack
*stack
)
280 if (!stack_empty(stack
)) {
281 g_ptr_array_remove_range(stack
->entries
, 0, stack_size(stack
));
284 assert(stack_empty(stack
));
288 enum bt_ctf_notif_iter_status
notif_iter_status_from_m_status(
289 enum bt_ctf_notif_iter_medium_status m_status
)
295 size_t buf_size_bits(struct bt_ctf_notif_iter
*notit
)
297 return BYTES_TO_BITS(notit
->buf
.sz
);
301 size_t buf_available_bits(struct bt_ctf_notif_iter
*notit
)
303 return buf_size_bits(notit
) - notit
->buf
.at
;
307 size_t packet_at(struct bt_ctf_notif_iter
*notit
)
309 return notit
->buf
.packet_offset
+ notit
->buf
.at
;
313 size_t remaining_content_bits(struct bt_ctf_notif_iter
*notit
)
315 if (notit
->cur_content_size
== -1) {
319 return notit
->cur_content_size
- packet_at(notit
);
323 size_t remaining_packet_bits(struct bt_ctf_notif_iter
*notit
)
325 if (notit
->cur_packet_size
== -1) {
329 return notit
->cur_packet_size
- packet_at(notit
);
333 void buf_consume_bits(struct bt_ctf_notif_iter
*notit
, size_t incr
)
335 notit
->buf
.at
+= incr
;
339 bool buf_has_enough_bits(struct bt_ctf_notif_iter
*notit
, size_t sz
)
341 return buf_available_bits(notit
) >= sz
;
345 enum bt_ctf_notif_iter_status
request_medium_bytes(
346 struct bt_ctf_notif_iter
*notit
)
348 uint8_t *buffer_addr
;
350 enum bt_ctf_notif_iter_medium_status m_status
;
352 m_status
= notit
->medium
.medops
.request_bytes(
353 notit
->medium
.max_request_sz
, &buffer_addr
,
354 &buffer_sz
, notit
->medium
.data
);
355 if (m_status
== BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK
) {
356 assert(buffer_sz
!= 0);
358 /* New packet offset is old one + old size (in bits) */
359 notit
->buf
.packet_offset
+= buf_size_bits(notit
);
361 /* Restart at the beginning of the new medium buffer */
364 /* New medium buffer size */
365 notit
->buf
.sz
= buffer_sz
;
367 /* New medium buffer address */
368 notit
->buf
.addr
= buffer_addr
;
371 return notif_iter_status_from_m_status(m_status
);
375 enum bt_ctf_notif_iter_status
buf_ensure_available_bits(
376 struct bt_ctf_notif_iter
*notit
)
378 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
380 if (buf_available_bits(notit
) == 0) {
382 * This _cannot_ return BT_CTF_NOTIF_ITER_STATUS_OK
385 status
= request_medium_bytes(notit
);
392 enum bt_ctf_notif_iter_status
read_dscope_begin_state(
393 struct bt_ctf_notif_iter
*notit
,
394 struct bt_ctf_field_type
*dscope_field_type
,
395 enum state done_state
, enum state continue_state
,
396 struct bt_ctf_field
**dscope_field
)
398 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
399 enum bt_ctf_btr_status btr_status
;
400 size_t consumed_bits
;
402 status
= buf_ensure_available_bits(notit
);
403 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
407 bt_put(*dscope_field
);
408 notit
->cur_dscope_field
= dscope_field
;
409 consumed_bits
= bt_ctf_btr_start(notit
->btr
, dscope_field_type
,
410 notit
->buf
.addr
, notit
->buf
.at
, packet_at(notit
),
411 notit
->buf
.sz
, &btr_status
);
413 switch (btr_status
) {
414 case BT_CTF_BTR_STATUS_OK
:
415 /* type was read completely */
416 notit
->state
= done_state
;
418 case BT_CTF_BTR_STATUS_EOF
:
419 notit
->state
= continue_state
;
422 PERR("Binary type reader failed to start\n");
423 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
427 /* Consume bits now since we know we're not in an error state */
428 buf_consume_bits(notit
, consumed_bits
);
435 enum bt_ctf_notif_iter_status
read_dscope_continue_state(
436 struct bt_ctf_notif_iter
*notit
, enum state done_state
)
438 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
439 enum bt_ctf_btr_status btr_status
;
440 size_t consumed_bits
;
442 status
= buf_ensure_available_bits(notit
);
443 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
447 consumed_bits
= bt_ctf_btr_continue(notit
->btr
, notit
->buf
.addr
,
448 notit
->buf
.sz
, &btr_status
);
450 switch (btr_status
) {
451 case BT_CTF_BTR_STATUS_OK
:
452 /* Type was read completely. */
453 notit
->state
= done_state
;
455 case BT_CTF_BTR_STATUS_EOF
:
456 /* Stay in this continue state. */
459 PERR("Binary type reader failed to continue\n");
460 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
464 /* Consume bits now since we know we're not in an error state. */
465 buf_consume_bits(notit
, consumed_bits
);
471 void put_event_dscopes(struct bt_ctf_notif_iter
*notit
)
473 BT_PUT(notit
->dscopes
.stream_event_header
);
474 BT_PUT(notit
->dscopes
.stream_event_context
);
475 BT_PUT(notit
->dscopes
.event_context
);
476 BT_PUT(notit
->dscopes
.event_payload
);
480 void put_all_dscopes(struct bt_ctf_notif_iter
*notit
)
482 BT_PUT(notit
->dscopes
.trace_packet_header
);
483 BT_PUT(notit
->dscopes
.stream_packet_context
);
484 put_event_dscopes(notit
);
488 enum bt_ctf_notif_iter_status
read_packet_header_begin_state(
489 struct bt_ctf_notif_iter
*notit
)
491 struct bt_ctf_field_type
*packet_header_type
= NULL
;
492 enum bt_ctf_notif_iter_status ret
= BT_CTF_NOTIF_ITER_STATUS_OK
;
494 if (bt_ctf_notif_iter_switch_packet(notit
)) {
495 ret
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
499 /* Packet header type is common to the whole trace. */
500 packet_header_type
= bt_ctf_trace_get_packet_header_type(
502 if (!packet_header_type
) {
503 notit
->state
= STATE_AFTER_TRACE_PACKET_HEADER
;
507 ret
= read_dscope_begin_state(notit
, packet_header_type
,
508 STATE_AFTER_TRACE_PACKET_HEADER
,
509 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
510 ¬it
->dscopes
.trace_packet_header
);
512 BT_PUT(packet_header_type
);
517 enum bt_ctf_notif_iter_status
read_packet_header_continue_state(
518 struct bt_ctf_notif_iter
*notit
)
520 return read_dscope_continue_state(notit
,
521 STATE_AFTER_TRACE_PACKET_HEADER
);
525 bool is_struct_type(struct bt_ctf_field_type
*field_type
)
527 return bt_ctf_field_type_get_type_id(field_type
) ==
528 BT_CTF_TYPE_ID_STRUCT
;
532 bool is_variant_type(struct bt_ctf_field_type
*field_type
)
534 return bt_ctf_field_type_get_type_id(field_type
) ==
535 BT_CTF_TYPE_ID_VARIANT
;
539 enum bt_ctf_notif_iter_status
set_current_stream_class(
540 struct bt_ctf_notif_iter
*notit
)
542 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
543 struct bt_ctf_field_type
*packet_header_type
;
544 struct bt_ctf_field_type
*stream_id_field_type
= NULL
;
547 /* Is there any "stream_id" field in the packet header? */
548 packet_header_type
= bt_ctf_trace_get_packet_header_type(
550 if (!packet_header_type
) {
551 PERR("Failed to retrieve trace's packet header type\n");
552 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
556 assert(is_struct_type(packet_header_type
));
559 stream_id_field_type
=
560 bt_ctf_field_type_structure_get_field_type_by_name(
561 packet_header_type
, "stream_id");
562 if (stream_id_field_type
) {
563 /* Find appropriate stream class using current stream ID */
564 struct bt_ctf_field
*stream_id_field
= NULL
;
567 assert(notit
->dscopes
.trace_packet_header
);
570 stream_id_field
= bt_ctf_field_structure_get_field(
571 notit
->dscopes
.trace_packet_header
, "stream_id");
572 assert(stream_id_field
);
573 ret
= bt_ctf_field_unsigned_integer_get_value(
574 stream_id_field
, &stream_id
);
576 BT_PUT(stream_id_field
);
578 /* Only one stream: pick the first stream class */
579 assert(bt_ctf_trace_get_stream_class_count(
580 notit
->meta
.trace
) == 1);
584 BT_PUT(notit
->meta
.stream_class
);
585 notit
->meta
.stream_class
= bt_ctf_trace_get_stream_class_by_id(
586 notit
->meta
.trace
, stream_id
);
587 if (!notit
->meta
.stream_class
) {
588 PERR("Cannot find stream class with ID %" PRIu64
"\n",
590 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
595 BT_PUT(packet_header_type
);
596 BT_PUT(stream_id_field_type
);
602 enum bt_ctf_notif_iter_status
after_packet_header_state(
603 struct bt_ctf_notif_iter
*notit
)
605 enum bt_ctf_notif_iter_status status
;
607 status
= set_current_stream_class(notit
);
608 if (status
== BT_CTF_NOTIF_ITER_STATUS_OK
) {
609 notit
->state
= STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
;
616 enum bt_ctf_notif_iter_status
read_packet_context_begin_state(
617 struct bt_ctf_notif_iter
*notit
)
619 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
620 struct bt_ctf_field_type
*packet_context_type
;
622 assert(notit
->meta
.stream_class
);
623 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(
624 notit
->meta
.stream_class
);
625 if (!packet_context_type
) {
626 notit
->state
= STATE_AFTER_STREAM_PACKET_CONTEXT
;
630 status
= read_dscope_begin_state(notit
, packet_context_type
,
631 STATE_AFTER_STREAM_PACKET_CONTEXT
,
632 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
633 ¬it
->dscopes
.stream_packet_context
);
636 BT_PUT(packet_context_type
);
641 enum bt_ctf_notif_iter_status
read_packet_context_continue_state(
642 struct bt_ctf_notif_iter
*notit
)
644 return read_dscope_continue_state(notit
,
645 STATE_AFTER_STREAM_PACKET_CONTEXT
);
649 enum bt_ctf_notif_iter_status
set_current_packet_content_sizes(
650 struct bt_ctf_notif_iter
*notit
)
652 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
653 struct bt_ctf_field
*packet_size_field
= NULL
;
654 struct bt_ctf_field
*content_size_field
= NULL
;
655 uint64_t content_size
= -1, packet_size
= -1;
657 assert(notit
->dscopes
.stream_packet_context
);
659 packet_size_field
= bt_ctf_field_structure_get_field(
660 notit
->dscopes
.stream_packet_context
, "packet_size");
661 content_size_field
= bt_ctf_field_structure_get_field(
662 notit
->dscopes
.stream_packet_context
, "content_size");
663 if (packet_size_field
) {
664 int ret
= bt_ctf_field_unsigned_integer_get_value(
665 packet_size_field
, &packet_size
);
668 if (packet_size
== 0) {
669 PERR("Decoded packet size is 0\n");
670 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
672 } else if ((packet_size
% 8) != 0) {
673 PERR("Decoded packet size is not a multiple of 8\n");
674 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
679 if (content_size_field
) {
680 int ret
= bt_ctf_field_unsigned_integer_get_value(
681 content_size_field
, &content_size
);
685 content_size
= packet_size
;
688 notit
->cur_packet_size
= packet_size
;
689 notit
->cur_content_size
= content_size
;
691 BT_PUT(packet_size_field
);
692 BT_PUT(content_size_field
);
697 enum bt_ctf_notif_iter_status
after_packet_context_state(
698 struct bt_ctf_notif_iter
*notit
)
700 enum bt_ctf_notif_iter_status status
;
702 status
= set_current_packet_content_sizes(notit
);
703 if (status
== BT_CTF_NOTIF_ITER_STATUS_OK
) {
704 notit
->state
= STATE_EMIT_NOTIF_NEW_PACKET
;
711 enum bt_ctf_notif_iter_status
read_event_header_begin_state(
712 struct bt_ctf_notif_iter
*notit
)
714 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
715 struct bt_ctf_field_type
*event_header_type
= NULL
;
717 /* Check if we have some content left */
718 if (notit
->cur_content_size
>= 0) {
719 if (packet_at(notit
) == notit
->cur_content_size
) {
720 /* No more events! */
721 notit
->state
= STATE_EMIT_NOTIF_END_OF_PACKET
;
723 } else if (packet_at(notit
) > notit
->cur_content_size
) {
724 /* That's not supposed to happen */
725 PERR("Cursor passed packet's content size:\n");
726 PERR("\tDecoded content size: %zu\n",
727 notit
->cur_content_size
);
728 PERR("\tCursor position: %zu\n", packet_at(notit
));
729 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
734 event_header_type
= bt_ctf_stream_class_get_event_header_type(
735 notit
->meta
.stream_class
);
736 if (!event_header_type
) {
737 notit
->state
= STATE_AFTER_STREAM_EVENT_HEADER
;
741 put_event_dscopes(notit
);
742 status
= read_dscope_begin_state(notit
, event_header_type
,
743 STATE_AFTER_STREAM_EVENT_HEADER
,
744 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
,
745 ¬it
->dscopes
.stream_event_header
);
747 BT_PUT(event_header_type
);
753 enum bt_ctf_notif_iter_status
read_event_header_continue_state(
754 struct bt_ctf_notif_iter
*notit
)
756 return read_dscope_continue_state(notit
,
757 STATE_AFTER_STREAM_EVENT_HEADER
);
761 enum bt_ctf_notif_iter_status
set_current_event_class(struct bt_ctf_notif_iter
*notit
)
764 * The assert() calls in this function are okay because it is
765 * assumed here that all the metadata objects have been
766 * validated for CTF correctness before decoding actual streams.
769 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
770 struct bt_ctf_field_type
*event_header_type
;
771 struct bt_ctf_field_type
*id_field_type
= NULL
;
772 struct bt_ctf_field_type
*v_field_type
= NULL
;
773 uint64_t event_id
= -1ULL;
776 event_header_type
= bt_ctf_stream_class_get_event_header_type(
777 notit
->meta
.stream_class
);
778 if (!event_header_type
) {
779 PERR("Failed to retrieve stream class's event header type\n");
780 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
784 /* Is there any "id"/"v" field in the event header? */
785 assert(is_struct_type(event_header_type
));
786 id_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
787 event_header_type
, "id");
788 v_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
789 event_header_type
, "v");
790 assert(notit
->dscopes
.stream_event_header
);
794 * | | |_ _|_ _| __ __ _
795 * | | | | | || '_ \ / _` |
796 * | |___| | | || | | | (_| | S P E C I A L
797 * |_____|_| |_||_| |_|\__, | C A S E â„¢
800 struct bt_ctf_field
*v_field
= NULL
;
801 struct bt_ctf_field
*v_struct_field
= NULL
;
802 struct bt_ctf_field
*v_struct_id_field
= NULL
;
805 v_field
= bt_ctf_field_structure_get_field(
806 notit
->dscopes
.stream_event_header
, "v");
810 bt_ctf_field_variant_get_current_field(v_field
);
811 if (!v_struct_field
) {
812 goto end_v_field_type
;
817 bt_ctf_field_structure_get_field(v_struct_field
, "id");
818 if (!v_struct_id_field
) {
819 goto end_v_field_type
;
822 ret
= bt_ctf_field_unsigned_integer_get_value(
823 v_struct_id_field
, &event_id
);
830 BT_PUT(v_struct_field
);
831 BT_PUT(v_struct_id_field
);
834 if (id_field_type
&& event_id
== -1ULL) {
835 /* Check "id" field */
836 struct bt_ctf_field
*id_field
= NULL
;
840 id_field
= bt_ctf_field_structure_get_field(
841 notit
->dscopes
.stream_event_header
, "id");
843 assert(bt_ctf_field_is_integer(id_field
) ||
844 bt_ctf_field_is_enumeration(id_field
));
846 if (bt_ctf_field_is_integer(id_field
)) {
847 ret
= bt_ctf_field_unsigned_integer_get_value(
848 id_field
, &event_id
);
850 struct bt_ctf_field
*container
;
852 container
= bt_ctf_field_enumeration_get_container(
855 ret
= bt_ctf_field_unsigned_integer_get_value(
856 container
, &event_id
);
863 if (event_id
== -1ULL) {
864 /* Event ID not found: single event? */
865 assert(bt_ctf_stream_class_get_event_class_count(
866 notit
->meta
.stream_class
) == 1);
870 BT_PUT(notit
->meta
.event_class
);
871 notit
->meta
.event_class
= bt_ctf_stream_class_get_event_class_by_id(
872 notit
->meta
.stream_class
, event_id
);
873 if (!notit
->meta
.event_class
) {
874 PERR("Cannot find event class with ID %" PRIu64
"\n", event_id
);
875 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
880 BT_PUT(event_header_type
);
881 BT_PUT(id_field_type
);
882 BT_PUT(v_field_type
);
888 enum bt_ctf_notif_iter_status
after_event_header_state(
889 struct bt_ctf_notif_iter
*notit
)
891 enum bt_ctf_notif_iter_status status
;
893 status
= set_current_event_class(notit
);
894 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
895 PERR("Failed to set current event class\n");
899 notit
->state
= STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
;
906 enum bt_ctf_notif_iter_status
read_stream_event_context_begin_state(
907 struct bt_ctf_notif_iter
*notit
)
909 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
910 struct bt_ctf_field_type
*stream_event_context_type
;
912 stream_event_context_type
= bt_ctf_stream_class_get_event_context_type(
913 notit
->meta
.stream_class
);
914 if (!stream_event_context_type
) {
915 notit
->state
= STATE_DSCOPE_EVENT_CONTEXT_BEGIN
;
919 status
= read_dscope_begin_state(notit
, stream_event_context_type
,
920 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
,
921 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
,
922 ¬it
->dscopes
.stream_event_context
);
925 BT_PUT(stream_event_context_type
);
931 enum bt_ctf_notif_iter_status
read_stream_event_context_continue_state(
932 struct bt_ctf_notif_iter
*notit
)
934 return read_dscope_continue_state(notit
,
935 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
);
939 enum bt_ctf_notif_iter_status
read_event_context_begin_state(
940 struct bt_ctf_notif_iter
*notit
)
942 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
943 struct bt_ctf_field_type
*event_context_type
;
945 event_context_type
= bt_ctf_event_class_get_context_type(
946 notit
->meta
.event_class
);
947 if (!event_context_type
) {
948 notit
->state
= STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
;
951 status
= read_dscope_begin_state(notit
, event_context_type
,
952 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
953 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
,
954 ¬it
->dscopes
.event_context
);
957 BT_PUT(event_context_type
);
963 enum bt_ctf_notif_iter_status
read_event_context_continue_state(
964 struct bt_ctf_notif_iter
*notit
)
966 return read_dscope_continue_state(notit
,
967 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
);
971 enum bt_ctf_notif_iter_status
read_event_payload_begin_state(
972 struct bt_ctf_notif_iter
*notit
)
974 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
975 struct bt_ctf_field_type
*event_payload_type
;
977 event_payload_type
= bt_ctf_event_class_get_payload_type(
978 notit
->meta
.event_class
);
979 if (!event_payload_type
) {
980 notit
->state
= STATE_EMIT_NOTIF_EVENT
;
984 status
= read_dscope_begin_state(notit
, event_payload_type
,
985 STATE_EMIT_NOTIF_EVENT
,
986 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
987 ¬it
->dscopes
.event_payload
);
990 BT_PUT(event_payload_type
);
996 enum bt_ctf_notif_iter_status
read_event_payload_continue_state(
997 struct bt_ctf_notif_iter
*notit
)
999 return read_dscope_continue_state(notit
, STATE_EMIT_NOTIF_EVENT
);
1003 enum bt_ctf_notif_iter_status
skip_packet_padding_state(
1004 struct bt_ctf_notif_iter
*notit
)
1006 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1007 size_t bits_to_skip
;
1009 assert(notit
->cur_packet_size
> 0);
1010 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1011 if (bits_to_skip
== 0) {
1012 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1015 size_t bits_to_consume
;
1016 status
= buf_ensure_available_bits(notit
);
1017 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
1021 bits_to_consume
= MIN(buf_available_bits(notit
), bits_to_skip
);
1022 buf_consume_bits(notit
, bits_to_consume
);
1023 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1024 if (bits_to_skip
== 0) {
1025 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1035 enum bt_ctf_notif_iter_status
handle_state(struct bt_ctf_notif_iter
*notit
)
1037 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1039 PDBG("Handling state %d\n", notit
->state
);
1041 // TODO: optimalize!
1042 switch (notit
->state
) {
1044 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1046 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
1047 status
= read_packet_header_begin_state(notit
);
1049 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
1050 status
= read_packet_header_continue_state(notit
);
1052 case STATE_AFTER_TRACE_PACKET_HEADER
:
1053 status
= after_packet_header_state(notit
);
1055 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
1056 status
= read_packet_context_begin_state(notit
);
1058 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
1059 status
= read_packet_context_continue_state(notit
);
1061 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
1062 status
= after_packet_context_state(notit
);
1064 case STATE_EMIT_NOTIF_NEW_PACKET
:
1065 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1067 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
:
1068 status
= read_event_header_begin_state(notit
);
1070 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
:
1071 status
= read_event_header_continue_state(notit
);
1073 case STATE_AFTER_STREAM_EVENT_HEADER
:
1074 status
= after_event_header_state(notit
);
1076 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
:
1077 status
= read_stream_event_context_begin_state(notit
);
1079 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
:
1080 status
= read_stream_event_context_continue_state(notit
);
1082 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN
:
1083 status
= read_event_context_begin_state(notit
);
1085 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
:
1086 status
= read_event_context_continue_state(notit
);
1088 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
1089 status
= read_event_payload_begin_state(notit
);
1091 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
1092 status
= read_event_payload_continue_state(notit
);
1094 case STATE_EMIT_NOTIF_EVENT
:
1095 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1097 case STATE_SKIP_PACKET_PADDING
:
1098 status
= skip_packet_padding_state(notit
);
1100 case STATE_EMIT_NOTIF_END_OF_PACKET
:
1101 notit
->state
= STATE_SKIP_PACKET_PADDING
;
1110 * Resets the internal state of a CTF notification iterator.
1112 * This function can be used when it is desired to seek to the beginning
1113 * of another packet. It is expected that the next call to
1114 * bt_ctf_notif_iter_medium_ops::request_bytes() made by this
1115 * notification iterator will return the \em first bytes of a \em
1118 * @param notif_iter CTF notification iterator
1121 void bt_ctf_notif_iter_reset(struct bt_ctf_notif_iter
*notit
)
1124 stack_clear(notit
->stack
);
1125 BT_PUT(notit
->meta
.stream_class
);
1126 BT_PUT(notit
->meta
.event_class
);
1127 BT_PUT(notit
->packet
);
1128 put_all_dscopes(notit
);
1129 notit
->buf
.addr
= NULL
;
1132 notit
->buf
.packet_offset
= 0;
1133 notit
->state
= STATE_INIT
;
1134 notit
->cur_content_size
= -1;
1135 notit
->cur_packet_size
= -1;
1139 int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter
*notit
)
1144 stack_clear(notit
->stack
);
1145 BT_PUT(notit
->meta
.stream_class
);
1146 BT_PUT(notit
->meta
.event_class
);
1147 BT_PUT(notit
->packet
);
1148 put_all_dscopes(notit
);
1151 * Adjust current buffer so that addr points to the beginning of the new
1154 if (notit
->buf
.addr
) {
1155 size_t consumed_bytes
= (size_t) (notit
->buf
.at
/ CHAR_BIT
);
1157 /* Packets are assumed to start on a byte frontier. */
1158 if (notit
->buf
.at
% CHAR_BIT
) {
1163 notit
->buf
.addr
+= consumed_bytes
;
1164 notit
->buf
.sz
-= consumed_bytes
;
1166 notit
->buf
.packet_offset
= 0;
1169 notit
->cur_content_size
= -1;
1170 notit
->cur_packet_size
= -1;
1176 struct bt_ctf_field
*get_next_field(struct bt_ctf_notif_iter
*notit
)
1178 struct bt_ctf_field
*next_field
= NULL
;
1179 struct bt_ctf_field
*base_field
;
1180 struct bt_ctf_field_type
*base_type
;
1183 assert(!stack_empty(notit
->stack
));
1184 index
= stack_top(notit
->stack
)->index
;
1185 base_field
= stack_top(notit
->stack
)->base
;
1186 base_type
= bt_ctf_field_get_type(base_field
);
1188 PERR("Failed to get base field's type\n");
1192 switch (bt_ctf_field_type_get_type_id(base_type
)) {
1193 case BT_CTF_TYPE_ID_STRUCT
:
1194 next_field
= bt_ctf_field_structure_get_field_by_index(
1197 case BT_CTF_TYPE_ID_ARRAY
:
1198 next_field
= bt_ctf_field_array_get_field(base_field
, index
);
1200 case BT_CTF_TYPE_ID_SEQUENCE
:
1201 next_field
= bt_ctf_field_sequence_get_field(base_field
, index
);
1203 case BT_CTF_TYPE_ID_VARIANT
:
1204 next_field
= bt_ctf_field_variant_get_current_field(base_field
);
1222 void update_clock_state(uint64_t *state
,
1223 struct bt_ctf_field
*value_field
)
1225 struct bt_ctf_field_type
*value_type
= NULL
;
1226 uint64_t requested_new_value
;
1227 uint64_t requested_new_value_mask
;
1228 uint64_t cur_value_masked
;
1229 int requested_new_value_size
;
1232 value_type
= bt_ctf_field_get_type(value_field
);
1235 requested_new_value_size
=
1236 bt_ctf_field_type_integer_get_size(value_type
);
1237 assert(requested_new_value_size
> 0);
1239 ret
= bt_ctf_field_unsigned_integer_get_value(value_field
,
1240 &requested_new_value
);
1244 * Special case for a 64-bit new value, which is the limit
1245 * of a clock value as of this version: overwrite the
1246 * current value directly.
1248 if (requested_new_value_size
== 64) {
1249 *state
= requested_new_value
;
1253 requested_new_value_mask
= (1ULL << requested_new_value_size
) - 1;
1254 cur_value_masked
= *state
& requested_new_value_mask
;
1256 if (requested_new_value
< cur_value_masked
) {
1258 * It looks like a wrap happened on the number of bits
1259 * of the requested new value. Assume that the clock
1260 * value wrapped only one time.
1262 *state
+= requested_new_value_mask
+ 1;
1265 /* Clear the low bits of the current clock value. */
1266 *state
&= ~requested_new_value_mask
;
1268 /* Set the low bits of the current clock value. */
1269 *state
|= requested_new_value
;
1275 enum bt_ctf_btr_status
update_clock(struct bt_ctf_notif_iter
*notit
,
1276 struct bt_ctf_field_type
*int_field_type
,
1277 struct bt_ctf_field
*int_field
)
1279 gboolean clock_found
;
1280 uint64_t *clock_state
;
1281 enum bt_ctf_btr_status ret
= BT_CTF_BTR_STATUS_OK
;
1282 struct bt_ctf_clock
*clock
= bt_ctf_field_type_integer_get_mapped_clock(
1285 if (likely(!clock
)) {
1289 clock_found
= g_hash_table_lookup_extended(notit
->clock_states
,
1290 clock
, NULL
, (gpointer
) &clock_state
);
1291 if (unlikely(!clock_found
)) {
1292 const char *clock_name
= bt_ctf_clock_get_name(clock
);
1294 PERR("Unknown clock %s mapped to integer encountered in stream\n",
1295 clock_name
? : "NULL");
1296 ret
= BT_CTF_BTR_STATUS_ERROR
;
1300 if (unlikely(!clock_state
)) {
1301 clock_state
= g_new0(uint64_t, 1);
1303 ret
= BT_CTF_BTR_STATUS_ENOMEM
;
1306 g_hash_table_insert(notit
->clock_states
, bt_get(clock
),
1310 /* Update the clock's state. */
1311 update_clock_state(clock_state
, int_field
);
1319 enum bt_ctf_btr_status
btr_signed_int_cb(int64_t value
,
1320 struct bt_ctf_field_type
*type
, void *data
)
1322 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1323 struct bt_ctf_field
*field
= NULL
;
1324 struct bt_ctf_field
*int_field
= NULL
;
1325 struct bt_ctf_notif_iter
*notit
= data
;
1328 /* create next field */
1329 field
= get_next_field(notit
);
1331 PERR("Failed to get next field (signed int)\n");
1332 status
= BT_CTF_BTR_STATUS_ERROR
;
1336 switch(bt_ctf_field_type_get_type_id(type
)) {
1337 case BT_CTF_TYPE_ID_INTEGER
:
1338 /* Integer field is created field */
1339 BT_MOVE(int_field
, field
);
1342 case BT_CTF_TYPE_ID_ENUM
:
1343 int_field
= bt_ctf_field_enumeration_get_container(field
);
1344 type
= bt_ctf_field_get_type(int_field
);
1353 PERR("Failed to get integer field\n");
1354 status
= BT_CTF_BTR_STATUS_ERROR
;
1358 ret
= bt_ctf_field_signed_integer_set_value(int_field
, value
);
1360 stack_top(notit
->stack
)->index
++;
1361 status
= update_clock(notit
, type
, int_field
);
1371 enum bt_ctf_btr_status
btr_unsigned_int_cb(uint64_t value
,
1372 struct bt_ctf_field_type
*type
, void *data
)
1374 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1375 struct bt_ctf_field
*field
= NULL
;
1376 struct bt_ctf_field
*int_field
= NULL
;
1377 struct bt_ctf_notif_iter
*notit
= data
;
1380 /* Create next field */
1381 field
= get_next_field(notit
);
1383 PERR("Failed to get next field (unsigned int)\n");
1384 status
= BT_CTF_BTR_STATUS_ERROR
;
1388 switch(bt_ctf_field_type_get_type_id(type
)) {
1389 case BT_CTF_TYPE_ID_INTEGER
:
1390 /* Integer field is created field */
1391 BT_MOVE(int_field
, field
);
1394 case BT_CTF_TYPE_ID_ENUM
:
1395 int_field
= bt_ctf_field_enumeration_get_container(field
);
1396 type
= bt_ctf_field_get_type(int_field
);
1405 PERR("Failed to get integer field\n");
1406 status
= BT_CTF_BTR_STATUS_ERROR
;
1410 ret
= bt_ctf_field_unsigned_integer_set_value(int_field
, value
);
1412 stack_top(notit
->stack
)->index
++;
1413 status
= update_clock(notit
, type
, int_field
);
1423 enum bt_ctf_btr_status
btr_floating_point_cb(double value
,
1424 struct bt_ctf_field_type
*type
, void *data
)
1426 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1427 struct bt_ctf_field
*field
= NULL
;
1428 struct bt_ctf_notif_iter
*notit
= data
;
1431 /* Create next field */
1432 field
= get_next_field(notit
);
1434 PERR("Failed to get next field (floating point number)\n");
1435 status
= BT_CTF_BTR_STATUS_ERROR
;
1439 ret
= bt_ctf_field_floating_point_set_value(field
, value
);
1441 stack_top(notit
->stack
)->index
++;
1450 enum bt_ctf_btr_status
btr_string_begin_cb(
1451 struct bt_ctf_field_type
*type
, void *data
)
1453 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1454 struct bt_ctf_field
*field
= NULL
;
1455 struct bt_ctf_notif_iter
*notit
= data
;
1458 /* Create next field */
1459 field
= get_next_field(notit
);
1461 PERR("Failed to get next field (string)\n");
1462 status
= BT_CTF_BTR_STATUS_ERROR
;
1467 * Push on stack. Not a compound type per se, but we know that only
1468 * btr_string_cb() may be called between this call and a subsequent
1469 * call to btr_string_end_cb().
1471 ret
= stack_push(notit
->stack
, field
);
1473 PERR("Failed to push string field onto the stack\n");
1474 status
= BT_CTF_BTR_STATUS_ERROR
;
1479 * Initialize string field payload to an empty string since in the
1480 * case of a length 0 string the btr_string_cb won't be called and
1481 * we will end up with an unset string payload.
1483 ret
= bt_ctf_field_string_set_value(field
, "");
1485 PERR("Failed to initialize string field\n");
1486 status
= BT_CTF_BTR_STATUS_ERROR
;
1497 enum bt_ctf_btr_status
btr_string_cb(const char *value
,
1498 size_t len
, struct bt_ctf_field_type
*type
, void *data
)
1500 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1501 struct bt_ctf_field
*field
= NULL
;
1502 struct bt_ctf_notif_iter
*notit
= data
;
1505 /* Get string field */
1506 field
= stack_top(notit
->stack
)->base
;
1509 /* Append current string */
1510 ret
= bt_ctf_field_string_append_len(field
, value
, len
);
1512 PERR("Failed to append a string to a string field\n");
1513 status
= BT_CTF_BTR_STATUS_ERROR
;
1522 enum bt_ctf_btr_status
btr_string_end_cb(
1523 struct bt_ctf_field_type
*type
, void *data
)
1525 struct bt_ctf_notif_iter
*notit
= data
;
1527 /* Pop string field */
1528 stack_pop(notit
->stack
);
1530 /* Go to next field */
1531 stack_top(notit
->stack
)->index
++;
1533 return BT_CTF_BTR_STATUS_OK
;
1536 enum bt_ctf_btr_status
btr_compound_begin_cb(
1537 struct bt_ctf_field_type
*type
, void *data
)
1539 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1540 struct bt_ctf_notif_iter
*notit
= data
;
1541 struct bt_ctf_field
*field
;
1545 if (stack_empty(notit
->stack
)) {
1546 /* Root: create dynamic scope field */
1547 *notit
->cur_dscope_field
= bt_ctf_field_create(type
);
1548 field
= *notit
->cur_dscope_field
;
1551 * Field will be put at the end of this function
1552 * (stack_push() will take one reference, but this
1553 * reference is lost upon the equivalent stack_pop()
1554 * later), so also get it for our context to own it.
1556 bt_get(*notit
->cur_dscope_field
);
1558 field
= get_next_field(notit
);
1562 PERR("Failed to get next field or create dynamic scope field\n");
1563 status
= BT_CTF_BTR_STATUS_ERROR
;
1568 ret
= stack_push(notit
->stack
, field
);
1570 PERR("Failed to push compound field onto the stack\n");
1571 status
= BT_CTF_BTR_STATUS_ERROR
;
1581 enum bt_ctf_btr_status
btr_compound_end_cb(
1582 struct bt_ctf_field_type
*type
, void *data
)
1584 struct bt_ctf_notif_iter
*notit
= data
;
1586 assert(!stack_empty(notit
->stack
));
1589 stack_pop(notit
->stack
);
1591 /* If the stack is not empty, increment the base's index */
1592 if (!stack_empty(notit
->stack
)) {
1593 stack_top(notit
->stack
)->index
++;
1596 return BT_CTF_BTR_STATUS_OK
;
1600 struct bt_ctf_field
*resolve_field(struct bt_ctf_notif_iter
*notit
,
1601 struct bt_ctf_field_path
*path
)
1603 struct bt_ctf_field
*field
= NULL
;
1606 switch (bt_ctf_field_path_get_root_scope(path
)) {
1607 case BT_CTF_SCOPE_TRACE_PACKET_HEADER
:
1608 field
= notit
->dscopes
.trace_packet_header
;
1610 case BT_CTF_SCOPE_STREAM_PACKET_CONTEXT
:
1611 field
= notit
->dscopes
.stream_packet_context
;
1613 case BT_CTF_SCOPE_STREAM_EVENT_HEADER
:
1614 field
= notit
->dscopes
.stream_event_header
;
1616 case BT_CTF_SCOPE_STREAM_EVENT_CONTEXT
:
1617 field
= notit
->dscopes
.stream_event_context
;
1619 case BT_CTF_SCOPE_EVENT_CONTEXT
:
1620 field
= notit
->dscopes
.event_context
;
1622 case BT_CTF_SCOPE_EVENT_FIELDS
:
1623 field
= notit
->dscopes
.event_payload
;
1635 for (i
= 0; i
< bt_ctf_field_path_get_index_count(path
); ++i
) {
1636 struct bt_ctf_field
*next_field
= NULL
;
1637 struct bt_ctf_field_type
*field_type
;
1638 int index
= bt_ctf_field_path_get_index(path
, i
);
1640 field_type
= bt_ctf_field_get_type(field
);
1646 if (is_struct_type(field_type
)) {
1647 next_field
= bt_ctf_field_structure_get_field_by_index(
1649 } else if (is_variant_type(field_type
)) {
1651 bt_ctf_field_variant_get_current_field(field
);
1661 /* Move next field -> field */
1662 BT_MOVE(field
, next_field
);
1670 int64_t btr_get_sequence_length_cb(struct bt_ctf_field_type
*type
, void *data
)
1674 struct bt_ctf_field_path
*field_path
;
1675 struct bt_ctf_notif_iter
*notit
= data
;
1676 struct bt_ctf_field
*length_field
= NULL
;
1679 field_path
= bt_ctf_field_type_sequence_get_length_field_path(type
);
1684 length_field
= resolve_field(notit
, field_path
);
1685 if (!length_field
) {
1689 iret
= bt_ctf_field_unsigned_integer_get_value(length_field
, &length
);
1694 iret
= bt_ctf_field_sequence_set_length(stack_top(notit
->stack
)->base
,
1699 ret
= (int64_t) length
;
1702 BT_PUT(length_field
);
1709 struct bt_ctf_field_type
*btr_get_variant_type_cb(
1710 struct bt_ctf_field_type
*type
, void *data
)
1712 struct bt_ctf_field_path
*path
;
1713 struct bt_ctf_notif_iter
*notit
= data
;
1714 struct bt_ctf_field
*tag_field
= NULL
;
1715 struct bt_ctf_field
*selected_field
= NULL
;
1716 struct bt_ctf_field_type
*selected_field_type
= NULL
;
1718 path
= bt_ctf_field_type_variant_get_tag_field_path(type
);
1723 tag_field
= resolve_field(notit
, path
);
1729 * We found the enumeration tag field instance which should be
1730 * able to select a current field for this variant. This
1731 * callback function we're in is called _after_
1732 * compound_begin(), so the current stack top's base field is
1733 * the variant field in question. We get the selected field here
1734 * thanks to this tag field (thus creating the selected field),
1735 * which will also provide us with its type. Then, this field
1736 * will remain the current selected one until the next callback
1737 * function call which is used to fill the current selected
1740 selected_field
= bt_ctf_field_variant_get_field(
1741 stack_top(notit
->stack
)->base
, tag_field
);
1742 if (!selected_field
) {
1746 selected_field_type
= bt_ctf_field_get_type(selected_field
);
1750 BT_PUT(selected_field
);
1753 return selected_field_type
;
1757 int set_event_clocks(struct bt_ctf_event
*event
,
1758 struct bt_ctf_notif_iter
*notit
)
1761 GHashTableIter iter
;
1762 struct bt_ctf_clock
*clock
;
1763 uint64_t *clock_state
;
1765 g_hash_table_iter_init(&iter
, notit
->clock_states
);
1767 while (g_hash_table_iter_next(&iter
, (gpointer
) &clock
,
1768 (gpointer
) &clock_state
)) {
1769 struct bt_ctf_clock_value
*clock_value
;
1771 clock_value
= bt_ctf_clock_value_create(clock
, *clock_state
);
1776 ret
= bt_ctf_event_set_clock_value(event
, clock
, clock_value
);
1777 bt_put(clock_value
);
1788 struct bt_ctf_event
*create_event(struct bt_ctf_notif_iter
*notit
)
1791 struct bt_ctf_event
*event
;
1793 /* Create event object. */
1794 event
= bt_ctf_event_create(notit
->meta
.event_class
);
1799 /* Set header, stream event context, context, and payload fields. */
1800 ret
= bt_ctf_event_set_header(event
,
1801 notit
->dscopes
.stream_event_header
);
1806 ret
= bt_ctf_event_set_stream_event_context(event
,
1807 notit
->dscopes
.stream_event_context
);
1812 ret
= bt_ctf_event_set_event_context(event
,
1813 notit
->dscopes
.event_context
);
1818 ret
= bt_ctf_event_set_payload_field(event
,
1819 notit
->dscopes
.event_payload
);
1824 ret
= set_event_clocks(event
, notit
);
1829 /* Associate with current packet. */
1830 assert(notit
->packet
);
1831 ret
= bt_ctf_event_set_packet(event
, notit
->packet
);
1844 void create_packet(struct bt_ctf_notif_iter
*notit
)
1847 struct bt_ctf_stream
*stream
= NULL
;
1848 struct bt_ctf_packet
*packet
= NULL
;
1850 /* Ask the user for the stream */
1851 stream
= notit
->medium
.medops
.get_stream(notit
->meta
.stream_class
,
1852 notit
->medium
.data
);
1858 packet
= bt_ctf_packet_create(stream
);
1863 /* Set packet's context and header fields */
1864 if (notit
->dscopes
.trace_packet_header
) {
1865 ret
= bt_ctf_packet_set_header(packet
,
1866 notit
->dscopes
.trace_packet_header
);
1872 if (notit
->dscopes
.stream_packet_context
) {
1873 ret
= bt_ctf_packet_set_context(packet
,
1874 notit
->dscopes
.stream_packet_context
);
1884 BT_MOVE(notit
->packet
, packet
);
1888 void notify_new_packet(struct bt_ctf_notif_iter
*notit
,
1889 struct bt_notification
**notification
)
1891 struct bt_notification
*ret
;
1893 /* Initialize the iterator's current packet */
1894 create_packet(notit
);
1895 if (!notit
->packet
) {
1899 ret
= bt_notification_packet_begin_create(notit
->packet
);
1903 *notification
= ret
;
1907 void notify_end_of_packet(struct bt_ctf_notif_iter
*notit
,
1908 struct bt_notification
**notification
)
1910 struct bt_notification
*ret
;
1912 if (!notit
->packet
) {
1916 ret
= bt_notification_packet_end_create(notit
->packet
);
1920 BT_PUT(notit
->packet
);
1921 *notification
= ret
;
1925 void notify_event(struct bt_ctf_notif_iter
*notit
,
1926 struct bt_notification
**notification
)
1928 struct bt_ctf_event
*event
;
1929 struct bt_notification
*ret
= NULL
;
1932 event
= create_event(notit
);
1937 ret
= bt_notification_event_create(event
);
1941 *notification
= ret
;
1948 void notify_eos(struct bt_ctf_notif_iter
*notit
,
1949 struct bt_notification
**notification
)
1951 struct bt_ctf_stream
*stream
= NULL
;
1952 struct bt_notification
*ret
= NULL
;
1954 /* Ask the user for the stream */
1955 stream
= notit
->medium
.medops
.get_stream(notit
->meta
.stream_class
,
1956 notit
->medium
.data
);
1961 ret
= bt_notification_stream_end_create(stream
);
1965 *notification
= ret
;
1971 int init_clock_states(GHashTable
*clock_states
, struct bt_ctf_trace
*trace
)
1973 int clock_count
, i
, ret
= 0;
1975 clock_count
= bt_ctf_trace_get_clock_count(trace
);
1976 if (clock_count
<= 0) {
1981 for (i
= 0; i
< clock_count
; i
++) {
1982 struct bt_ctf_clock
*clock
;
1984 clock
= bt_ctf_trace_get_clock(trace
, i
);
1990 g_hash_table_insert(clock_states
, bt_get(clock
), NULL
);
1998 struct bt_ctf_notif_iter
*bt_ctf_notif_iter_create(struct bt_ctf_trace
*trace
,
1999 size_t max_request_sz
,
2000 struct bt_ctf_notif_iter_medium_ops medops
,
2001 void *data
, FILE *err_stream
)
2004 struct bt_ctf_notif_iter
*notit
= NULL
;
2005 struct bt_ctf_btr_cbs cbs
= {
2007 .signed_int
= btr_signed_int_cb
,
2008 .unsigned_int
= btr_unsigned_int_cb
,
2009 .floating_point
= btr_floating_point_cb
,
2010 .string_begin
= btr_string_begin_cb
,
2011 .string
= btr_string_cb
,
2012 .string_end
= btr_string_end_cb
,
2013 .compound_begin
= btr_compound_begin_cb
,
2014 .compound_end
= btr_compound_end_cb
,
2017 .get_sequence_length
= btr_get_sequence_length_cb
,
2018 .get_variant_type
= btr_get_variant_type_cb
,
2023 assert(medops
.request_bytes
);
2024 notit
= g_new0(struct bt_ctf_notif_iter
, 1);
2026 PERR("Failed to allocate memory for CTF notification iterator\n");
2029 notit
->clock_states
= g_hash_table_new_full(g_direct_hash
,
2030 g_direct_equal
, bt_put
, g_free
);
2031 if (!notit
->clock_states
) {
2032 PERR("Failed to create hash table\n");
2035 ret
= init_clock_states(notit
->clock_states
, trace
);
2037 PERR("Failed to initialize stream clock states\n");
2040 notit
->meta
.trace
= bt_get(trace
);
2041 notit
->medium
.medops
= medops
;
2042 notit
->medium
.max_request_sz
= max_request_sz
;
2043 notit
->medium
.data
= data
;
2044 notit
->err_stream
= err_stream
;
2045 notit
->stack
= stack_new(notit
);
2046 if (!notit
->stack
) {
2047 PERR("Failed to create stack\n");
2051 notit
->btr
= bt_ctf_btr_create(cbs
, notit
, err_stream
);
2053 PERR("Failed to create binary type reader\n");
2057 bt_ctf_notif_iter_reset(notit
);
2062 bt_ctf_notif_iter_destroy(notit
);
2067 void bt_ctf_notif_iter_destroy(struct bt_ctf_notif_iter
*notit
)
2069 BT_PUT(notit
->meta
.trace
);
2070 BT_PUT(notit
->meta
.stream_class
);
2071 BT_PUT(notit
->meta
.event_class
);
2072 BT_PUT(notit
->packet
);
2073 put_all_dscopes(notit
);
2076 stack_destroy(notit
->stack
);
2080 bt_ctf_btr_destroy(notit
->btr
);
2083 if (notit
->clock_states
) {
2084 g_hash_table_destroy(notit
->clock_states
);
2089 enum bt_ctf_notif_iter_status
bt_ctf_notif_iter_get_next_notification(
2090 struct bt_ctf_notif_iter
*notit
,
2091 struct bt_notification
**notification
)
2093 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
2096 assert(notification
);
2099 status
= handle_state(notit
);
2100 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
2101 if (status
== BT_CTF_NOTIF_ITER_STATUS_EOF
) {
2102 PDBG("Medium operation reported end of stream\n");
2104 PERR("Failed to handle state:\n");
2105 PERR("\tState: %d\n", notit
->state
);
2110 switch (notit
->state
) {
2111 case STATE_EMIT_NOTIF_NEW_PACKET
:
2112 PDBG("Emitting new packet notification\n");
2113 notify_new_packet(notit
, notification
);
2114 if (!*notification
) {
2115 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
2118 case STATE_EMIT_NOTIF_EVENT
:
2119 PDBG("Emitting event notification\n");
2120 notify_event(notit
, notification
);
2121 if (!*notification
) {
2122 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
2125 case STATE_EMIT_NOTIF_END_OF_PACKET
:
2126 PDBG("Emitting end of packet notification\n");
2127 notify_end_of_packet(notit
, notification
);
2128 if (!*notification
) {
2129 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
2133 /* Non-emitting state: continue */