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/ref.h>
46 #define PRINT_ERR_STREAM notit->err_stream
47 #define PRINT_PREFIX "ctf-notif-iter"
50 #include "notif-iter.h"
51 #include "../btr/btr.h"
53 #define BYTES_TO_BITS(x) ((x) * 8)
55 struct bt_ctf_notif_iter
;
57 /* A visit stack entry */
60 * Current base field, one of:
68 * Field is owned by this.
70 struct bt_ctf_field
*base
;
72 /* index of next field to set */
78 /* Entries (struct stack_entry *) (top is last element) */
85 STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
,
86 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
87 STATE_AFTER_TRACE_PACKET_HEADER
,
88 STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
,
89 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
90 STATE_AFTER_STREAM_PACKET_CONTEXT
,
91 STATE_EMIT_NOTIF_NEW_PACKET
,
92 STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
,
93 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
,
94 STATE_AFTER_STREAM_EVENT_HEADER
,
95 STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
,
96 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
,
97 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
,
98 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
,
99 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
100 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
101 STATE_EMIT_NOTIF_EVENT
,
102 STATE_EMIT_NOTIF_END_OF_PACKET
,
103 STATE_SKIP_PACKET_PADDING
,
106 /* CTF notification iterator */
107 struct bt_ctf_notif_iter
{
111 /* Error stream (may be NULL) */
115 * Current dynamic scope field pointer.
117 * This is set when a dynamic scope field is first created by
118 * btr_compound_begin_cb(). It points to one of the fields in
121 struct bt_ctf_field
**cur_dscope_field
;
123 /* Trace and classes (owned by this) */
125 struct bt_ctf_trace
*trace
;
126 struct bt_ctf_stream_class
*stream_class
;
127 struct bt_ctf_event_class
*event_class
;
130 /* Current packet (NULL if not created yet) */
131 struct bt_ctf_packet
*packet
;
133 /* Database of current dynamic scopes (owned by this) */
135 struct bt_ctf_field
*trace_packet_header
;
136 struct bt_ctf_field
*stream_packet_context
;
137 struct bt_ctf_field
*stream_event_header
;
138 struct bt_ctf_field
*stream_event_context
;
139 struct bt_ctf_field
*event_context
;
140 struct bt_ctf_field
*event_payload
;
146 /* Current medium buffer data */
148 /* Last address provided by medium */
151 /* Buffer size provided by medium (bytes) */
154 /* Offset within whole packet of addr (bits) */
155 size_t packet_offset
;
157 /* Current position from addr (bits) */
161 /* Binary type reader */
162 struct bt_ctf_btr
*btr
;
164 /* Current medium data */
166 struct bt_ctf_notif_iter_medium_ops medops
;
167 size_t max_request_sz
;
171 /* Current packet size (bits) (-1 if unknown) */
172 int64_t cur_packet_size
;
174 /* Current content size (bits) (-1 if unknown) */
175 int64_t cur_content_size
;
179 int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter
*notit
);
182 void stack_entry_free_func(gpointer data
)
184 struct stack_entry
*entry
= data
;
191 struct stack
*stack_new(struct bt_ctf_notif_iter
*notit
)
193 struct stack
*stack
= NULL
;
195 stack
= g_new0(struct stack
, 1);
200 stack
->entries
= g_ptr_array_new_with_free_func(stack_entry_free_func
);
201 if (!stack
->entries
) {
212 void stack_destroy(struct stack
*stack
)
215 g_ptr_array_free(stack
->entries
, TRUE
);
220 int stack_push(struct stack
*stack
, struct bt_ctf_field
*base
)
223 struct stack_entry
*entry
;
227 entry
= g_new0(struct stack_entry
, 1);
233 entry
->base
= bt_get(base
);
234 g_ptr_array_add(stack
->entries
, entry
);
241 unsigned int stack_size(struct stack
*stack
)
245 return stack
->entries
->len
;
249 void stack_pop(struct stack
*stack
)
252 assert(stack_size(stack
));
253 g_ptr_array_remove_index(stack
->entries
, stack
->entries
->len
- 1);
257 struct stack_entry
*stack_top(struct stack
*stack
)
260 assert(stack_size(stack
));
262 return g_ptr_array_index(stack
->entries
, stack
->entries
->len
- 1);
266 bool stack_empty(struct stack
*stack
)
268 return stack_size(stack
) == 0;
272 void stack_clear(struct stack
*stack
)
276 if (!stack_empty(stack
)) {
277 g_ptr_array_remove_range(stack
->entries
, 0, stack_size(stack
));
280 assert(stack_empty(stack
));
284 enum bt_ctf_notif_iter_status
notif_iter_status_from_m_status(
285 enum bt_ctf_notif_iter_medium_status m_status
)
291 size_t buf_size_bits(struct bt_ctf_notif_iter
*notit
)
293 return BYTES_TO_BITS(notit
->buf
.sz
);
297 size_t buf_available_bits(struct bt_ctf_notif_iter
*notit
)
299 return buf_size_bits(notit
) - notit
->buf
.at
;
303 size_t packet_at(struct bt_ctf_notif_iter
*notit
)
305 return notit
->buf
.packet_offset
+ notit
->buf
.at
;
309 size_t remaining_content_bits(struct bt_ctf_notif_iter
*notit
)
311 if (notit
->cur_content_size
== -1) {
315 return notit
->cur_content_size
- packet_at(notit
);
319 size_t remaining_packet_bits(struct bt_ctf_notif_iter
*notit
)
321 if (notit
->cur_packet_size
== -1) {
325 return notit
->cur_packet_size
- packet_at(notit
);
329 void buf_consume_bits(struct bt_ctf_notif_iter
*notit
, size_t incr
)
331 notit
->buf
.at
+= incr
;
335 bool buf_has_enough_bits(struct bt_ctf_notif_iter
*notit
, size_t sz
)
337 return buf_available_bits(notit
) >= sz
;
341 enum bt_ctf_notif_iter_status
request_medium_bytes(
342 struct bt_ctf_notif_iter
*notit
)
344 uint8_t *buffer_addr
;
346 enum bt_ctf_notif_iter_medium_status m_status
;
348 m_status
= notit
->medium
.medops
.request_bytes(
349 notit
->medium
.max_request_sz
, &buffer_addr
,
350 &buffer_sz
, notit
->medium
.data
);
351 if (m_status
== BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK
) {
352 assert(buffer_sz
!= 0);
354 /* New packet offset is old one + old size (in bits) */
355 notit
->buf
.packet_offset
+= buf_size_bits(notit
);
357 /* Restart at the beginning of the new medium buffer */
360 /* New medium buffer size */
361 notit
->buf
.sz
= buffer_sz
;
363 /* New medium buffer address */
364 notit
->buf
.addr
= buffer_addr
;
367 return notif_iter_status_from_m_status(m_status
);
371 enum bt_ctf_notif_iter_status
buf_ensure_available_bits(
372 struct bt_ctf_notif_iter
*notit
)
374 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
376 if (buf_available_bits(notit
) == 0) {
378 * This _cannot_ return BT_CTF_NOTIF_ITER_STATUS_OK
381 status
= request_medium_bytes(notit
);
388 enum bt_ctf_notif_iter_status
read_dscope_begin_state(
389 struct bt_ctf_notif_iter
*notit
,
390 struct bt_ctf_field_type
*dscope_field_type
,
391 enum state done_state
, enum state continue_state
,
392 struct bt_ctf_field
**dscope_field
)
394 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
395 enum bt_ctf_btr_status btr_status
;
396 size_t consumed_bits
;
398 status
= buf_ensure_available_bits(notit
);
399 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
403 bt_put(*dscope_field
);
404 notit
->cur_dscope_field
= dscope_field
;
405 consumed_bits
= bt_ctf_btr_start(notit
->btr
, dscope_field_type
,
406 notit
->buf
.addr
, notit
->buf
.at
, packet_at(notit
),
407 notit
->buf
.sz
, &btr_status
);
409 switch (btr_status
) {
410 case BT_CTF_BTR_STATUS_OK
:
411 /* type was read completely */
412 notit
->state
= done_state
;
414 case BT_CTF_BTR_STATUS_EOF
:
415 notit
->state
= continue_state
;
418 PERR("Binary type reader failed to start\n");
419 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
423 /* Consume bits now since we know we're not in an error state */
424 buf_consume_bits(notit
, consumed_bits
);
431 enum bt_ctf_notif_iter_status
read_dscope_continue_state(
432 struct bt_ctf_notif_iter
*notit
, enum state done_state
)
434 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
435 enum bt_ctf_btr_status btr_status
;
436 size_t consumed_bits
;
438 status
= buf_ensure_available_bits(notit
);
439 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
443 consumed_bits
= bt_ctf_btr_continue(notit
->btr
, notit
->buf
.addr
,
444 notit
->buf
.sz
, &btr_status
);
446 switch (btr_status
) {
447 case BT_CTF_BTR_STATUS_OK
:
448 /* Type was read completely. */
449 notit
->state
= done_state
;
451 case BT_CTF_BTR_STATUS_EOF
:
452 /* Stay in this continue state. */
455 PERR("Binary type reader failed to continue\n");
456 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
460 /* Consume bits now since we know we're not in an error state. */
461 buf_consume_bits(notit
, consumed_bits
);
467 void put_event_dscopes(struct bt_ctf_notif_iter
*notit
)
469 BT_PUT(notit
->dscopes
.stream_event_header
);
470 BT_PUT(notit
->dscopes
.stream_event_context
);
471 BT_PUT(notit
->dscopes
.event_context
);
472 BT_PUT(notit
->dscopes
.event_payload
);
476 void put_all_dscopes(struct bt_ctf_notif_iter
*notit
)
478 BT_PUT(notit
->dscopes
.trace_packet_header
);
479 BT_PUT(notit
->dscopes
.stream_packet_context
);
480 put_event_dscopes(notit
);
484 enum bt_ctf_notif_iter_status
read_packet_header_begin_state(
485 struct bt_ctf_notif_iter
*notit
)
487 struct bt_ctf_field_type
*packet_header_type
= NULL
;
488 enum bt_ctf_notif_iter_status ret
= BT_CTF_NOTIF_ITER_STATUS_OK
;
490 if (bt_ctf_notif_iter_switch_packet(notit
)) {
491 ret
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
495 /* Packet header type is common to the whole trace. */
496 packet_header_type
= bt_ctf_trace_get_packet_header_type(
498 if (!packet_header_type
) {
499 PERR("Failed to retrieve trace's packet header type\n");
500 ret
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
504 ret
= read_dscope_begin_state(notit
, packet_header_type
,
505 STATE_AFTER_TRACE_PACKET_HEADER
,
506 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
507 ¬it
->dscopes
.trace_packet_header
);
509 BT_PUT(packet_header_type
);
514 enum bt_ctf_notif_iter_status
read_packet_header_continue_state(
515 struct bt_ctf_notif_iter
*notit
)
517 return read_dscope_continue_state(notit
,
518 STATE_AFTER_TRACE_PACKET_HEADER
);
522 bool is_struct_type(struct bt_ctf_field_type
*field_type
)
524 return bt_ctf_field_type_get_type_id(field_type
) ==
525 BT_CTF_TYPE_ID_STRUCT
;
529 bool is_variant_type(struct bt_ctf_field_type
*field_type
)
531 return bt_ctf_field_type_get_type_id(field_type
) ==
532 BT_CTF_TYPE_ID_VARIANT
;
536 enum bt_ctf_notif_iter_status
set_current_stream_class(struct bt_ctf_notif_iter
*notit
)
538 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
539 struct bt_ctf_field_type
*packet_header_type
;
540 struct bt_ctf_field_type
*stream_id_field_type
= NULL
;
543 /* Is there any "stream_id" field in the packet header? */
544 packet_header_type
= bt_ctf_trace_get_packet_header_type(
546 if (!packet_header_type
) {
547 PERR("Failed to retrieve trace's packet header type\n");
548 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
552 assert(is_struct_type(packet_header_type
));
555 stream_id_field_type
=
556 bt_ctf_field_type_structure_get_field_type_by_name(
557 packet_header_type
, "stream_id");
558 if (stream_id_field_type
) {
559 /* Find appropriate stream class using current stream ID */
560 struct bt_ctf_field
*stream_id_field
= NULL
;
563 assert(notit
->dscopes
.trace_packet_header
);
566 stream_id_field
= bt_ctf_field_structure_get_field(
567 notit
->dscopes
.trace_packet_header
, "stream_id");
568 assert(stream_id_field
);
569 ret
= bt_ctf_field_unsigned_integer_get_value(
570 stream_id_field
, &stream_id
);
572 BT_PUT(stream_id_field
);
574 /* Only one stream: pick the first stream class */
575 assert(bt_ctf_trace_get_stream_class_count(
576 notit
->meta
.trace
) == 1);
580 BT_PUT(notit
->meta
.stream_class
);
582 notit
->meta
.stream_class
= bt_ctf_trace_get_stream_class_by_id(
583 notit
->meta
.trace
, stream_id
);
584 if (!notit
->meta
.stream_class
) {
585 PERR("Cannot find stream class with ID %" PRIu64
"\n",
587 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
592 BT_PUT(packet_header_type
);
593 BT_PUT(stream_id_field_type
);
599 enum bt_ctf_notif_iter_status
after_packet_header_state(
600 struct bt_ctf_notif_iter
*notit
)
602 enum bt_ctf_notif_iter_status status
;
604 status
= set_current_stream_class(notit
);
605 if (status
== BT_CTF_NOTIF_ITER_STATUS_OK
) {
606 notit
->state
= STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
;
613 enum bt_ctf_notif_iter_status
read_packet_context_begin_state(
614 struct bt_ctf_notif_iter
*notit
)
616 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
617 struct bt_ctf_field_type
*packet_context_type
;
619 assert(notit
->meta
.stream_class
);
620 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(
621 notit
->meta
.stream_class
);
622 if (!packet_context_type
) {
623 PERR("Failed to retrieve stream class's packet context\n");
624 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
628 status
= read_dscope_begin_state(notit
, packet_context_type
,
629 STATE_AFTER_STREAM_PACKET_CONTEXT
,
630 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
631 ¬it
->dscopes
.stream_packet_context
);
634 BT_PUT(packet_context_type
);
639 enum bt_ctf_notif_iter_status
read_packet_context_continue_state(
640 struct bt_ctf_notif_iter
*notit
)
642 return read_dscope_continue_state(notit
,
643 STATE_AFTER_STREAM_PACKET_CONTEXT
);
647 enum bt_ctf_notif_iter_status
set_current_packet_content_sizes(
648 struct bt_ctf_notif_iter
*notit
)
650 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
651 struct bt_ctf_field
*packet_size_field
= NULL
;
652 struct bt_ctf_field
*content_size_field
= NULL
;
653 uint64_t content_size
= -1, packet_size
= -1;
655 assert(notit
->dscopes
.stream_packet_context
);
657 packet_size_field
= bt_ctf_field_structure_get_field(
658 notit
->dscopes
.stream_packet_context
, "packet_size");
659 content_size_field
= bt_ctf_field_structure_get_field(
660 notit
->dscopes
.stream_packet_context
, "content_size");
661 if (packet_size_field
) {
662 int ret
= bt_ctf_field_unsigned_integer_get_value(
663 packet_size_field
, &packet_size
);
666 if (packet_size
== 0) {
667 PERR("Decoded packet size is 0\n");
668 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
670 } else if ((packet_size
% 8) != 0) {
671 PERR("Decoded packet size is not a multiple of 8\n");
672 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
677 if (content_size_field
) {
678 int ret
= bt_ctf_field_unsigned_integer_get_value(
679 content_size_field
, &content_size
);
683 content_size
= packet_size
;
686 notit
->cur_packet_size
= packet_size
;
687 notit
->cur_content_size
= content_size
;
689 BT_PUT(packet_size_field
);
690 BT_PUT(content_size_field
);
695 enum bt_ctf_notif_iter_status
after_packet_context_state(
696 struct bt_ctf_notif_iter
*notit
)
698 enum bt_ctf_notif_iter_status status
;
700 status
= set_current_packet_content_sizes(notit
);
701 if (status
== BT_CTF_NOTIF_ITER_STATUS_OK
) {
702 notit
->state
= STATE_EMIT_NOTIF_NEW_PACKET
;
709 enum bt_ctf_notif_iter_status
read_event_header_begin_state(
710 struct bt_ctf_notif_iter
*notit
)
712 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
713 struct bt_ctf_field_type
*event_header_type
= NULL
;
715 /* Check if we have some content left */
716 if (notit
->cur_content_size
>= 0) {
717 if (packet_at(notit
) == notit
->cur_content_size
) {
718 /* No more events! */
719 notit
->state
= STATE_EMIT_NOTIF_END_OF_PACKET
;
721 } else if (packet_at(notit
) > notit
->cur_content_size
) {
722 /* That's not supposed to happen */
723 PERR("Cursor passed packet's content size:\n");
724 PERR("\tDecoded content size: %zu\n",
725 notit
->cur_content_size
);
726 PERR("\tCursor position: %zu\n", packet_at(notit
));
727 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
732 event_header_type
= bt_ctf_stream_class_get_event_header_type(
733 notit
->meta
.stream_class
);
734 if (!event_header_type
) {
735 PERR("Failed to retrieve stream class's event header type\n");
736 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
740 put_event_dscopes(notit
);
741 status
= read_dscope_begin_state(notit
, event_header_type
,
742 STATE_AFTER_STREAM_EVENT_HEADER
,
743 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
,
744 ¬it
->dscopes
.stream_event_header
);
746 BT_PUT(event_header_type
);
752 enum bt_ctf_notif_iter_status
read_event_header_continue_state(
753 struct bt_ctf_notif_iter
*notit
)
755 return read_dscope_continue_state(notit
,
756 STATE_AFTER_STREAM_EVENT_HEADER
);
760 enum bt_ctf_notif_iter_status
set_current_event_class(struct bt_ctf_notif_iter
*notit
)
763 * The assert() calls in this function are okay because it is
764 * assumed here that all the metadata objects have been
765 * validated for CTF correctness before decoding actual streams.
768 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
769 struct bt_ctf_field_type
*event_header_type
;
770 struct bt_ctf_field_type
*id_field_type
= NULL
;
771 struct bt_ctf_field_type
*v_field_type
= NULL
;
772 uint64_t event_id
= -1ULL;
775 event_header_type
= bt_ctf_stream_class_get_event_header_type(
776 notit
->meta
.stream_class
);
777 if (!event_header_type
) {
778 PERR("Failed to retrieve stream class's event header type\n");
779 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
783 /* Is there any "id"/"v" field in the event header? */
784 assert(is_struct_type(event_header_type
));
785 id_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
786 event_header_type
, "id");
787 v_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
788 event_header_type
, "v");
789 assert(notit
->dscopes
.stream_event_header
);
793 * | | |_ _|_ _| __ __ _
794 * | | | | | || '_ \ / _` |
795 * | |___| | | || | | | (_| | S P E C I A L
796 * |_____|_| |_||_| |_|\__, | C A S E â„¢
799 struct bt_ctf_field
*v_field
= NULL
;
800 struct bt_ctf_field
*v_struct_field
= NULL
;
801 struct bt_ctf_field
*v_struct_id_field
= NULL
;
804 v_field
= bt_ctf_field_structure_get_field(
805 notit
->dscopes
.stream_event_header
, "v");
809 bt_ctf_field_variant_get_current_field(v_field
);
810 if (!v_struct_field
) {
811 goto end_v_field_type
;
816 bt_ctf_field_structure_get_field(v_struct_field
, "id");
817 if (!v_struct_id_field
) {
818 goto end_v_field_type
;
821 ret
= bt_ctf_field_unsigned_integer_get_value(
822 v_struct_id_field
, &event_id
);
829 BT_PUT(v_struct_field
);
830 BT_PUT(v_struct_id_field
);
833 if (id_field_type
&& event_id
== -1ULL) {
834 /* Check "id" field */
835 struct bt_ctf_field
*id_field
= NULL
;
839 id_field
= bt_ctf_field_structure_get_field(
840 notit
->dscopes
.stream_event_header
, "id");
842 assert(bt_ctf_field_is_integer(id_field
) ||
843 bt_ctf_field_is_enumeration(id_field
));
845 if (bt_ctf_field_is_integer(id_field
)) {
846 ret
= bt_ctf_field_unsigned_integer_get_value(
847 id_field
, &event_id
);
849 struct bt_ctf_field
*container
;
851 container
= bt_ctf_field_enumeration_get_container(
854 ret
= bt_ctf_field_unsigned_integer_get_value(
855 container
, &event_id
);
862 if (event_id
== -1ULL) {
863 /* Event ID not found: single event? */
864 assert(bt_ctf_stream_class_get_event_class_count(
865 notit
->meta
.stream_class
) == 1);
869 BT_PUT(notit
->meta
.event_class
);
870 notit
->meta
.event_class
= bt_ctf_stream_class_get_event_class_by_id(
871 notit
->meta
.stream_class
, event_id
);
872 if (!notit
->meta
.event_class
) {
873 PERR("Cannot find event class with ID %" PRIu64
"\n", event_id
);
874 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
879 BT_PUT(event_header_type
);
880 BT_PUT(id_field_type
);
881 BT_PUT(v_field_type
);
887 enum bt_ctf_notif_iter_status
after_event_header_state(
888 struct bt_ctf_notif_iter
*notit
)
890 enum bt_ctf_notif_iter_status status
;
892 status
= set_current_event_class(notit
);
893 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
894 PERR("Failed to set current event class\n");
898 notit
->state
= STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
;
905 enum bt_ctf_notif_iter_status
read_stream_event_context_begin_state(
906 struct bt_ctf_notif_iter
*notit
)
908 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
909 struct bt_ctf_field_type
*stream_event_context_type
;
911 stream_event_context_type
= bt_ctf_stream_class_get_event_context_type(
912 notit
->meta
.stream_class
);
913 if (!stream_event_context_type
) {
914 PERR("Failed to retrieve stream class's event context type\n");
915 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
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 PERR("Failed to retrieve event class's context type\n");
949 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
953 status
= read_dscope_begin_state(notit
, event_context_type
,
954 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
955 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
,
956 ¬it
->dscopes
.event_context
);
959 BT_PUT(event_context_type
);
965 enum bt_ctf_notif_iter_status
read_event_context_continue_state(
966 struct bt_ctf_notif_iter
*notit
)
968 return read_dscope_continue_state(notit
,
969 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
);
973 enum bt_ctf_notif_iter_status
read_event_payload_begin_state(
974 struct bt_ctf_notif_iter
*notit
)
976 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
977 struct bt_ctf_field_type
*event_payload_type
;
979 event_payload_type
= bt_ctf_event_class_get_payload_type(
980 notit
->meta
.event_class
);
981 if (!event_payload_type
) {
982 PERR("Failed to retrieve event class's payload type\n");
983 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
987 status
= read_dscope_begin_state(notit
, event_payload_type
,
988 STATE_EMIT_NOTIF_EVENT
,
989 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
990 ¬it
->dscopes
.event_payload
);
993 BT_PUT(event_payload_type
);
999 enum bt_ctf_notif_iter_status
read_event_payload_continue_state(
1000 struct bt_ctf_notif_iter
*notit
)
1002 return read_dscope_continue_state(notit
, STATE_EMIT_NOTIF_EVENT
);
1006 enum bt_ctf_notif_iter_status
skip_packet_padding_state(
1007 struct bt_ctf_notif_iter
*notit
)
1009 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1010 size_t bits_to_skip
;
1012 assert(notit
->cur_packet_size
> 0);
1013 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1014 if (bits_to_skip
== 0) {
1015 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1018 size_t bits_to_consume
;
1019 status
= buf_ensure_available_bits(notit
);
1020 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
1024 bits_to_consume
= MIN(buf_available_bits(notit
), bits_to_skip
);
1025 buf_consume_bits(notit
, bits_to_consume
);
1026 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1027 if (bits_to_skip
== 0) {
1028 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1038 enum bt_ctf_notif_iter_status
handle_state(struct bt_ctf_notif_iter
*notit
)
1040 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1042 PDBG("Handling state %d\n", notit
->state
);
1044 // TODO: optimalize!
1045 switch (notit
->state
) {
1047 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1049 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
1050 status
= read_packet_header_begin_state(notit
);
1052 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
1053 status
= read_packet_header_continue_state(notit
);
1055 case STATE_AFTER_TRACE_PACKET_HEADER
:
1056 status
= after_packet_header_state(notit
);
1058 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
1059 status
= read_packet_context_begin_state(notit
);
1061 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
1062 status
= read_packet_context_continue_state(notit
);
1064 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
1065 status
= after_packet_context_state(notit
);
1067 case STATE_EMIT_NOTIF_NEW_PACKET
:
1068 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1070 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
:
1071 status
= read_event_header_begin_state(notit
);
1073 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
:
1074 status
= read_event_header_continue_state(notit
);
1076 case STATE_AFTER_STREAM_EVENT_HEADER
:
1077 status
= after_event_header_state(notit
);
1079 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
:
1080 status
= read_stream_event_context_begin_state(notit
);
1082 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
:
1083 status
= read_stream_event_context_continue_state(notit
);
1085 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN
:
1086 status
= read_event_context_begin_state(notit
);
1088 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
:
1089 status
= read_event_context_continue_state(notit
);
1091 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
1092 status
= read_event_payload_begin_state(notit
);
1094 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
1095 status
= read_event_payload_continue_state(notit
);
1097 case STATE_EMIT_NOTIF_EVENT
:
1098 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1100 case STATE_SKIP_PACKET_PADDING
:
1101 status
= skip_packet_padding_state(notit
);
1103 case STATE_EMIT_NOTIF_END_OF_PACKET
:
1104 notit
->state
= STATE_SKIP_PACKET_PADDING
;
1113 * Resets the internal state of a CTF notification iterator.
1115 * This function can be used when it is desired to seek to the beginning
1116 * of another packet. It is expected that the next call to
1117 * bt_ctf_notif_iter_medium_ops::request_bytes() made by this
1118 * notification iterator will return the \em first bytes of a \em
1121 * @param notif_iter CTF notification iterator
1124 void bt_ctf_notif_iter_reset(struct bt_ctf_notif_iter
*notit
)
1127 stack_clear(notit
->stack
);
1128 BT_PUT(notit
->meta
.stream_class
);
1129 BT_PUT(notit
->meta
.event_class
);
1130 BT_PUT(notit
->packet
);
1131 put_all_dscopes(notit
);
1132 notit
->buf
.addr
= NULL
;
1135 notit
->buf
.packet_offset
= 0;
1136 notit
->state
= STATE_INIT
;
1137 notit
->cur_content_size
= -1;
1138 notit
->cur_packet_size
= -1;
1142 int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter
*notit
)
1147 stack_clear(notit
->stack
);
1148 BT_PUT(notit
->meta
.stream_class
);
1149 BT_PUT(notit
->meta
.event_class
);
1150 BT_PUT(notit
->packet
);
1151 put_all_dscopes(notit
);
1154 * Adjust current buffer so that addr points to the beginning of the new
1157 if (notit
->buf
.addr
) {
1158 size_t consumed_bytes
= (size_t) (notit
->buf
.at
/ CHAR_BIT
);
1160 /* Packets are assumed to start on a byte frontier. */
1161 if (notit
->buf
.at
% CHAR_BIT
) {
1166 notit
->buf
.addr
+= consumed_bytes
;
1167 notit
->buf
.sz
-= consumed_bytes
;
1169 notit
->buf
.packet_offset
= 0;
1172 notit
->cur_content_size
= -1;
1173 notit
->cur_packet_size
= -1;
1179 struct bt_ctf_field
*get_next_field(struct bt_ctf_notif_iter
*notit
)
1181 struct bt_ctf_field
*next_field
= NULL
;
1182 struct bt_ctf_field
*base_field
;
1183 struct bt_ctf_field_type
*base_type
;
1186 assert(!stack_empty(notit
->stack
));
1187 index
= stack_top(notit
->stack
)->index
;
1188 base_field
= stack_top(notit
->stack
)->base
;
1189 base_type
= bt_ctf_field_get_type(base_field
);
1191 PERR("Failed to get base field's type\n");
1195 switch (bt_ctf_field_type_get_type_id(base_type
)) {
1196 case BT_CTF_TYPE_ID_STRUCT
:
1197 next_field
= bt_ctf_field_structure_get_field_by_index(
1200 case BT_CTF_TYPE_ID_ARRAY
:
1201 next_field
= bt_ctf_field_array_get_field(base_field
, index
);
1203 case BT_CTF_TYPE_ID_SEQUENCE
:
1204 next_field
= bt_ctf_field_sequence_get_field(base_field
, index
);
1206 case BT_CTF_TYPE_ID_VARIANT
:
1207 next_field
= bt_ctf_field_variant_get_current_field(base_field
);
1225 enum bt_ctf_btr_status
btr_signed_int_cb(int64_t value
,
1226 struct bt_ctf_field_type
*type
, void *data
)
1228 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1229 struct bt_ctf_field
*field
= NULL
;
1230 struct bt_ctf_field
*int_field
= NULL
;
1231 struct bt_ctf_notif_iter
*notit
= data
;
1234 /* create next field */
1235 field
= get_next_field(notit
);
1237 PERR("Failed to get next field (signed int)\n");
1238 status
= BT_CTF_BTR_STATUS_ERROR
;
1242 switch(bt_ctf_field_type_get_type_id(type
)) {
1243 case BT_CTF_TYPE_ID_INTEGER
:
1244 /* Integer field is created field */
1245 BT_MOVE(int_field
, field
);
1247 case BT_CTF_TYPE_ID_ENUM
:
1248 int_field
= bt_ctf_field_enumeration_get_container(field
);
1255 PERR("Failed to get integer field\n");
1256 status
= BT_CTF_BTR_STATUS_ERROR
;
1260 ret
= bt_ctf_field_signed_integer_set_value(int_field
, value
);
1262 stack_top(notit
->stack
)->index
++;
1272 enum bt_ctf_btr_status
btr_unsigned_int_cb(uint64_t value
,
1273 struct bt_ctf_field_type
*type
, void *data
)
1275 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1276 struct bt_ctf_field
*field
= NULL
;
1277 struct bt_ctf_field
*int_field
= NULL
;
1278 struct bt_ctf_notif_iter
*notit
= data
;
1281 /* Create next field */
1282 field
= get_next_field(notit
);
1284 PERR("Failed to get next field (unsigned int)\n");
1285 status
= BT_CTF_BTR_STATUS_ERROR
;
1289 switch(bt_ctf_field_type_get_type_id(type
)) {
1290 case BT_CTF_TYPE_ID_INTEGER
:
1291 /* Integer field is created field */
1292 BT_MOVE(int_field
, field
);
1294 case BT_CTF_TYPE_ID_ENUM
:
1295 int_field
= bt_ctf_field_enumeration_get_container(field
);
1302 PERR("Failed to get integer field\n");
1303 status
= BT_CTF_BTR_STATUS_ERROR
;
1307 ret
= bt_ctf_field_unsigned_integer_set_value(int_field
, value
);
1309 stack_top(notit
->stack
)->index
++;
1319 enum bt_ctf_btr_status
btr_floating_point_cb(double 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_notif_iter
*notit
= data
;
1327 /* Create next field */
1328 field
= get_next_field(notit
);
1330 PERR("Failed to get next field (floating point number)\n");
1331 status
= BT_CTF_BTR_STATUS_ERROR
;
1335 ret
= bt_ctf_field_floating_point_set_value(field
, value
);
1337 stack_top(notit
->stack
)->index
++;
1346 enum bt_ctf_btr_status
btr_string_begin_cb(
1347 struct bt_ctf_field_type
*type
, void *data
)
1349 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1350 struct bt_ctf_field
*field
= NULL
;
1351 struct bt_ctf_notif_iter
*notit
= data
;
1354 /* Create next field */
1355 field
= get_next_field(notit
);
1357 PERR("Failed to get next field (string)\n");
1358 status
= BT_CTF_BTR_STATUS_ERROR
;
1363 * Push on stack. Not a compound type per se, but we know that only
1364 * btr_string_cb() may be called between this call and a subsequent
1365 * call to btr_string_end_cb().
1367 ret
= stack_push(notit
->stack
, field
);
1369 PERR("Failed to push string field onto the stack\n");
1370 status
= BT_CTF_BTR_STATUS_ERROR
;
1381 enum bt_ctf_btr_status
btr_string_cb(const char *value
,
1382 size_t len
, struct bt_ctf_field_type
*type
, void *data
)
1384 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1385 struct bt_ctf_field
*field
= NULL
;
1386 struct bt_ctf_notif_iter
*notit
= data
;
1389 /* Get string field */
1390 field
= stack_top(notit
->stack
)->base
;
1393 /* Append current string */
1394 ret
= bt_ctf_field_string_append_len(field
, value
, len
);
1396 PERR("Failed to append a string to a string field\n");
1397 status
= BT_CTF_BTR_STATUS_ERROR
;
1406 enum bt_ctf_btr_status
btr_string_end_cb(
1407 struct bt_ctf_field_type
*type
, void *data
)
1409 struct bt_ctf_notif_iter
*notit
= data
;
1411 /* Pop string field */
1412 stack_pop(notit
->stack
);
1414 /* Go to next field */
1415 stack_top(notit
->stack
)->index
++;
1417 return BT_CTF_BTR_STATUS_OK
;
1420 enum bt_ctf_btr_status
btr_compound_begin_cb(
1421 struct bt_ctf_field_type
*type
, void *data
)
1423 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1424 struct bt_ctf_notif_iter
*notit
= data
;
1425 struct bt_ctf_field
*field
;
1429 if (stack_empty(notit
->stack
)) {
1430 /* Root: create dynamic scope field */
1431 *notit
->cur_dscope_field
= bt_ctf_field_create(type
);
1432 field
= *notit
->cur_dscope_field
;
1435 * Field will be put at the end of this function
1436 * (stack_push() will take one reference, but this
1437 * reference is lost upon the equivalent stack_pop()
1438 * later), so also get it for our context to own it.
1440 bt_get(*notit
->cur_dscope_field
);
1442 field
= get_next_field(notit
);
1446 PERR("Failed to get next field or create dynamic scope field\n");
1447 status
= BT_CTF_BTR_STATUS_ERROR
;
1452 ret
= stack_push(notit
->stack
, field
);
1454 PERR("Failed to push compound field onto the stack\n");
1455 status
= BT_CTF_BTR_STATUS_ERROR
;
1465 enum bt_ctf_btr_status
btr_compound_end_cb(
1466 struct bt_ctf_field_type
*type
, void *data
)
1468 struct bt_ctf_notif_iter
*notit
= data
;
1470 assert(!stack_empty(notit
->stack
));
1473 stack_pop(notit
->stack
);
1475 /* If the stack is not empty, increment the base's index */
1476 if (!stack_empty(notit
->stack
)) {
1477 stack_top(notit
->stack
)->index
++;
1480 return BT_CTF_BTR_STATUS_OK
;
1484 struct bt_ctf_field
*resolve_field(struct bt_ctf_notif_iter
*notit
,
1485 struct bt_ctf_field_path
*path
)
1487 struct bt_ctf_field
*field
= NULL
;
1490 switch (bt_ctf_field_path_get_root_scope(path
)) {
1491 case BT_CTF_SCOPE_TRACE_PACKET_HEADER
:
1492 field
= notit
->dscopes
.trace_packet_header
;
1494 case BT_CTF_SCOPE_STREAM_PACKET_CONTEXT
:
1495 field
= notit
->dscopes
.stream_packet_context
;
1497 case BT_CTF_SCOPE_STREAM_EVENT_HEADER
:
1498 field
= notit
->dscopes
.stream_event_header
;
1500 case BT_CTF_SCOPE_STREAM_EVENT_CONTEXT
:
1501 field
= notit
->dscopes
.stream_event_context
;
1503 case BT_CTF_SCOPE_EVENT_CONTEXT
:
1504 field
= notit
->dscopes
.event_context
;
1506 case BT_CTF_SCOPE_EVENT_FIELDS
:
1507 field
= notit
->dscopes
.event_payload
;
1519 for (i
= 0; i
< bt_ctf_field_path_get_index_count(path
); ++i
) {
1520 struct bt_ctf_field
*next_field
= NULL
;
1521 struct bt_ctf_field_type
*field_type
;
1522 int index
= bt_ctf_field_path_get_index(path
, i
);
1524 field_type
= bt_ctf_field_get_type(field
);
1530 if (is_struct_type(field_type
)) {
1531 next_field
= bt_ctf_field_structure_get_field_by_index(
1533 } else if (is_variant_type(field_type
)) {
1535 bt_ctf_field_variant_get_current_field(field
);
1545 /* Move next field -> field */
1546 BT_MOVE(field
, next_field
);
1554 int64_t btr_get_sequence_length_cb(struct bt_ctf_field_type
*type
, void *data
)
1558 struct bt_ctf_field_path
*field_path
;
1559 struct bt_ctf_notif_iter
*notit
= data
;
1560 struct bt_ctf_field
*length_field
= NULL
;
1563 field_path
= bt_ctf_field_type_sequence_get_length_field_path(type
);
1568 length_field
= resolve_field(notit
, field_path
);
1569 if (!length_field
) {
1573 iret
= bt_ctf_field_unsigned_integer_get_value(length_field
, &length
);
1578 iret
= bt_ctf_field_sequence_set_length(stack_top(notit
->stack
)->base
,
1583 ret
= (int64_t) length
;
1586 BT_PUT(length_field
);
1593 struct bt_ctf_field_type
*btr_get_variant_type_cb(
1594 struct bt_ctf_field_type
*type
, void *data
)
1596 struct bt_ctf_field_path
*path
;
1597 struct bt_ctf_notif_iter
*notit
= data
;
1598 struct bt_ctf_field
*tag_field
= NULL
;
1599 struct bt_ctf_field
*selected_field
= NULL
;
1600 struct bt_ctf_field_type
*selected_field_type
= NULL
;
1602 path
= bt_ctf_field_type_variant_get_tag_field_path(type
);
1607 tag_field
= resolve_field(notit
, path
);
1613 * We found the enumeration tag field instance which should be
1614 * able to select a current field for this variant. This
1615 * callback function we're in is called _after_
1616 * compound_begin(), so the current stack top's base field is
1617 * the variant field in question. We get the selected field here
1618 * thanks to this tag field (thus creating the selected field),
1619 * which will also provide us with its type. Then, this field
1620 * will remain the current selected one until the next callback
1621 * function call which is used to fill the current selected
1624 selected_field
= bt_ctf_field_variant_get_field(
1625 stack_top(notit
->stack
)->base
, tag_field
);
1626 if (!selected_field
) {
1630 selected_field_type
= bt_ctf_field_get_type(selected_field
);
1634 BT_PUT(selected_field
);
1637 return selected_field_type
;
1641 struct bt_ctf_event
*create_event(struct bt_ctf_notif_iter
*notit
)
1644 struct bt_ctf_event
*event
;
1646 /* Create event object. */
1647 event
= bt_ctf_event_create(notit
->meta
.event_class
);
1652 /* Set header, stream event context, context, and payload fields. */
1653 ret
= bt_ctf_event_set_header(event
,
1654 notit
->dscopes
.stream_event_header
);
1659 ret
= bt_ctf_event_set_stream_event_context(event
,
1660 notit
->dscopes
.stream_event_context
);
1665 ret
= bt_ctf_event_set_event_context(event
,
1666 notit
->dscopes
.event_context
);
1671 ret
= bt_ctf_event_set_payload_field(event
,
1672 notit
->dscopes
.event_payload
);
1677 /* Associate with current packet. */
1678 assert(notit
->packet
);
1679 ret
= bt_ctf_event_set_packet(event
, notit
->packet
);
1692 void create_packet(struct bt_ctf_notif_iter
*notit
)
1695 struct bt_ctf_stream
*stream
= NULL
;
1696 struct bt_ctf_packet
*packet
= NULL
;
1698 /* Ask the user for the stream */
1699 stream
= notit
->medium
.medops
.get_stream(notit
->meta
.stream_class
,
1700 notit
->medium
.data
);
1706 packet
= bt_ctf_packet_create(stream
);
1711 /* Set packet's context and header fields */
1712 if (notit
->dscopes
.trace_packet_header
) {
1713 ret
= bt_ctf_packet_set_header(packet
,
1714 notit
->dscopes
.trace_packet_header
);
1720 if (notit
->dscopes
.stream_packet_context
) {
1721 ret
= bt_ctf_packet_set_context(packet
,
1722 notit
->dscopes
.stream_packet_context
);
1732 BT_MOVE(notit
->packet
, packet
);
1736 void notify_new_packet(struct bt_ctf_notif_iter
*notit
,
1737 struct bt_notification
**notification
)
1739 struct bt_notification
*ret
;
1741 /* Initialize the iterator's current packet */
1742 create_packet(notit
);
1743 if (!notit
->packet
) {
1747 ret
= bt_notification_packet_start_create(notit
->packet
);
1751 *notification
= ret
;
1755 void notify_end_of_packet(struct bt_ctf_notif_iter
*notit
,
1756 struct bt_notification
**notification
)
1758 struct bt_notification
*ret
;
1760 if (!notit
->packet
) {
1764 ret
= bt_notification_packet_end_create(notit
->packet
);
1768 BT_PUT(notit
->packet
);
1769 *notification
= ret
;
1773 void notify_event(struct bt_ctf_notif_iter
*notit
,
1774 struct bt_notification
**notification
)
1776 struct bt_ctf_event
*event
;
1777 struct bt_notification
*ret
= NULL
;
1780 event
= create_event(notit
);
1785 ret
= bt_notification_event_create(event
);
1789 *notification
= ret
;
1795 void notify_eos(struct bt_ctf_notif_iter
*notit
,
1796 struct bt_notification
**notification
)
1798 struct bt_ctf_event
*event
;
1799 struct bt_notification
*ret
= NULL
;
1802 event
= create_event(notit
);
1807 ret
= bt_notification_stream_end_create(event
);
1811 *notification
= ret
;
1816 struct bt_ctf_notif_iter
*bt_ctf_notif_iter_create(struct bt_ctf_trace
*trace
,
1817 size_t max_request_sz
,
1818 struct bt_ctf_notif_iter_medium_ops medops
,
1819 void *data
, FILE *err_stream
)
1821 struct bt_ctf_notif_iter
*notit
= NULL
;
1822 struct bt_ctf_btr_cbs cbs
= {
1824 .signed_int
= btr_signed_int_cb
,
1825 .unsigned_int
= btr_unsigned_int_cb
,
1826 .floating_point
= btr_floating_point_cb
,
1827 .string_begin
= btr_string_begin_cb
,
1828 .string
= btr_string_cb
,
1829 .string_end
= btr_string_end_cb
,
1830 .compound_begin
= btr_compound_begin_cb
,
1831 .compound_end
= btr_compound_end_cb
,
1834 .get_sequence_length
= btr_get_sequence_length_cb
,
1835 .get_variant_type
= btr_get_variant_type_cb
,
1840 assert(medops
.request_bytes
);
1841 notit
= g_new0(struct bt_ctf_notif_iter
, 1);
1843 PERR("Failed to allocate memory for CTF notification iterator\n");
1847 notit
->meta
.trace
= trace
;
1848 bt_get(notit
->meta
.trace
);
1849 notit
->medium
.medops
= medops
;
1850 notit
->medium
.max_request_sz
= max_request_sz
;
1851 notit
->medium
.data
= data
;
1852 notit
->err_stream
= err_stream
;
1853 notit
->stack
= stack_new(notit
);
1854 if (!notit
->stack
) {
1855 PERR("Failed to create stack\n");
1856 bt_ctf_notif_iter_destroy(notit
);
1861 notit
->btr
= bt_ctf_btr_create(cbs
, notit
, err_stream
);
1863 PERR("Failed to create binary type reader\n");
1864 bt_ctf_notif_iter_destroy(notit
);
1869 bt_ctf_notif_iter_reset(notit
);
1875 void bt_ctf_notif_iter_destroy(struct bt_ctf_notif_iter
*notit
)
1877 BT_PUT(notit
->meta
.trace
);
1878 BT_PUT(notit
->meta
.stream_class
);
1879 BT_PUT(notit
->meta
.event_class
);
1880 BT_PUT(notit
->packet
);
1881 put_all_dscopes(notit
);
1884 stack_destroy(notit
->stack
);
1888 bt_ctf_btr_destroy(notit
->btr
);
1894 enum bt_ctf_notif_iter_status
bt_ctf_notif_iter_get_next_notification(
1895 struct bt_ctf_notif_iter
*notit
,
1896 struct bt_notification
**notification
)
1898 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1901 assert(notification
);
1904 status
= handle_state(notit
);
1905 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
1906 if (status
== BT_CTF_NOTIF_ITER_STATUS_EOF
) {
1907 PDBG("Medium operation reported end of stream\n");
1909 PERR("Failed to handle state:\n");
1910 PERR("\tState: %d\n", notit
->state
);
1915 switch (notit
->state
) {
1916 case STATE_EMIT_NOTIF_NEW_PACKET
:
1917 PDBG("Emitting new packet notification\n");
1918 notify_new_packet(notit
, notification
);
1919 if (!*notification
) {
1920 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1923 case STATE_EMIT_NOTIF_EVENT
:
1924 PDBG("Emitting event notification\n");
1925 notify_event(notit
, notification
);
1926 if (!*notification
) {
1927 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1930 case STATE_EMIT_NOTIF_END_OF_PACKET
:
1931 PDBG("Emitting end of packet notification\n");
1932 notify_end_of_packet(notit
, notification
);
1933 if (!*notification
) {
1934 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1938 /* Non-emitting state: continue */