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 /* User buffer stuff */
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
;
166 struct bt_ctf_notif_iter_medium_ops medops
;
167 size_t max_request_sz
;
171 /* Current packet size (bits) (-1 if unknown) */
172 size_t cur_packet_size
;
174 /* Current content size (bits) (-1 if unknown) */
175 size_t cur_content_size
;
179 void stack_entry_free_func(gpointer data
)
181 struct stack_entry
*entry
= data
;
188 struct stack
*stack_new(struct bt_ctf_notif_iter
*notit
)
190 struct stack
*stack
= NULL
;
192 stack
= g_new0(struct stack
, 1);
197 stack
->entries
= g_ptr_array_new_with_free_func(stack_entry_free_func
);
198 if (!stack
->entries
) {
209 void stack_destroy(struct stack
*stack
)
212 g_ptr_array_free(stack
->entries
, TRUE
);
217 int stack_push(struct stack
*stack
, struct bt_ctf_field
*base
)
220 struct stack_entry
*entry
;
224 entry
= g_new0(struct stack_entry
, 1);
230 entry
->base
= bt_get(base
);
231 g_ptr_array_add(stack
->entries
, entry
);
238 unsigned int stack_size(struct stack
*stack
)
242 return stack
->entries
->len
;
246 void stack_pop(struct stack
*stack
)
249 assert(stack_size(stack
));
250 g_ptr_array_remove_index(stack
->entries
, stack
->entries
->len
- 1);
254 struct stack_entry
*stack_top(struct stack
*stack
)
257 assert(stack_size(stack
));
259 return g_ptr_array_index(stack
->entries
, stack
->entries
->len
- 1);
263 bool stack_empty(struct stack
*stack
)
265 return stack_size(stack
) == 0;
269 void stack_clear(struct stack
*stack
)
273 if (!stack_empty(stack
)) {
274 g_ptr_array_remove_range(stack
->entries
, 0, stack_size(stack
));
277 assert(stack_empty(stack
));
281 enum bt_ctf_notif_iter_status
notif_iter_status_from_m_status(
282 enum bt_ctf_notif_iter_medium_status m_status
)
288 size_t buf_size_bits(struct bt_ctf_notif_iter
*notit
)
290 return BYTES_TO_BITS(notit
->buf
.sz
);
294 size_t buf_available_bits(struct bt_ctf_notif_iter
*notit
)
296 return buf_size_bits(notit
) - notit
->buf
.at
;
300 size_t packet_at(struct bt_ctf_notif_iter
*notit
)
302 return notit
->buf
.packet_offset
+ notit
->buf
.at
;
306 size_t remaining_content_bits(struct bt_ctf_notif_iter
*notit
)
308 if (notit
->cur_content_size
== -1) {
312 return notit
->cur_content_size
- packet_at(notit
);
316 size_t remaining_packet_bits(struct bt_ctf_notif_iter
*notit
)
318 if (notit
->cur_packet_size
== -1) {
322 return notit
->cur_packet_size
- packet_at(notit
);
326 void buf_consume_bits(struct bt_ctf_notif_iter
*notit
, size_t incr
)
328 notit
->buf
.at
+= incr
;
332 bool buf_has_enough_bits(struct bt_ctf_notif_iter
*notit
, size_t sz
)
334 return buf_available_bits(notit
) >= sz
;
338 enum bt_ctf_notif_iter_status
request_medium_bytes(struct bt_ctf_notif_iter
*notit
)
340 uint8_t *buffer_addr
;
342 enum bt_ctf_notif_iter_medium_status m_status
;
344 m_status
= notit
->medium
.medops
.request_bytes(
345 notit
->medium
.max_request_sz
, &buffer_addr
,
346 &buffer_sz
, notit
->medium
.data
);
347 if (m_status
== BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK
) {
348 assert(buffer_sz
!= 0);
350 /* New packet offset is old one + old size (in bits) */
351 notit
->buf
.packet_offset
+= buf_size_bits(notit
);
353 /* Restart at the beginning of the new medium buffer */
356 /* New medium buffer size */
357 notit
->buf
.sz
= buffer_sz
;
359 /* New medium buffer address */
360 notit
->buf
.addr
= buffer_addr
;
363 return notif_iter_status_from_m_status(m_status
);
367 enum bt_ctf_notif_iter_status
buf_ensure_available_bits(
368 struct bt_ctf_notif_iter
*notit
)
370 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
372 if (buf_available_bits(notit
) == 0) {
374 * This _cannot_ return BT_CTF_NOTIF_ITER_STATUS_OK
377 status
= request_medium_bytes(notit
);
384 enum bt_ctf_notif_iter_status
read_dscope_begin_state(
385 struct bt_ctf_notif_iter
*notit
,
386 struct bt_ctf_field_type
*dscope_field_type
,
387 enum state done_state
, enum state continue_state
,
388 struct bt_ctf_field
**dscope_field
)
390 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
391 enum bt_ctf_btr_status btr_status
;
392 size_t consumed_bits
;
394 status
= buf_ensure_available_bits(notit
);
395 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
399 bt_put(*dscope_field
);
400 notit
->cur_dscope_field
= dscope_field
;
401 consumed_bits
= bt_ctf_btr_start(notit
->btr
, dscope_field_type
,
402 notit
->buf
.addr
, notit
->buf
.at
, packet_at(notit
),
403 notit
->buf
.sz
, &btr_status
);
405 switch (btr_status
) {
406 case BT_CTF_BTR_STATUS_OK
:
407 /* type was read completely */
408 notit
->state
= done_state
;
410 case BT_CTF_BTR_STATUS_EOF
:
411 notit
->state
= continue_state
;
414 PERR("Binary type reader failed to start\n");
415 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
419 /* Consume bits now since we know we're not in an error state */
420 buf_consume_bits(notit
, consumed_bits
);
427 enum bt_ctf_notif_iter_status
read_dscope_continue_state(
428 struct bt_ctf_notif_iter
*notit
, enum state done_state
)
430 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
431 enum bt_ctf_btr_status btr_status
;
432 size_t consumed_bits
;
434 status
= buf_ensure_available_bits(notit
);
435 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
439 consumed_bits
= bt_ctf_btr_continue(notit
->btr
, notit
->buf
.addr
,
440 notit
->buf
.sz
, &btr_status
);
442 switch (btr_status
) {
443 case BT_CTF_BTR_STATUS_OK
:
444 /* Type was read completely. */
445 notit
->state
= done_state
;
447 case BT_CTF_BTR_STATUS_EOF
:
448 /* Stay in this continue state. */
451 PERR("Binary type reader failed to continue\n");
452 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
456 /* Consume bits now since we know we're not in an error state. */
457 buf_consume_bits(notit
, consumed_bits
);
463 void put_event_dscopes(struct bt_ctf_notif_iter
*notit
)
465 BT_PUT(notit
->dscopes
.stream_event_header
);
466 BT_PUT(notit
->dscopes
.stream_event_context
);
467 BT_PUT(notit
->dscopes
.event_context
);
468 BT_PUT(notit
->dscopes
.event_payload
);
472 void put_all_dscopes(struct bt_ctf_notif_iter
*notit
)
474 BT_PUT(notit
->dscopes
.trace_packet_header
);
475 BT_PUT(notit
->dscopes
.stream_packet_context
);
476 put_event_dscopes(notit
);
480 enum bt_ctf_notif_iter_status
read_packet_header_begin_state(
481 struct bt_ctf_notif_iter
*notit
)
483 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
484 struct bt_ctf_field_type
*packet_header_type
;
486 /* Reset all dynamic scopes since we're reading a new packet */
487 put_all_dscopes(notit
);
488 BT_PUT(notit
->packet
);
489 BT_PUT(notit
->meta
.stream_class
);
490 BT_PUT(notit
->meta
.event_class
);
492 /* Packet header type is common to the whole trace */
493 packet_header_type
= bt_ctf_trace_get_packet_header_type(
495 if (!packet_header_type
) {
496 PERR("Failed to retrieve trace's packet header type\n");
497 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
501 status
= read_dscope_begin_state(notit
, packet_header_type
,
502 STATE_AFTER_TRACE_PACKET_HEADER
,
503 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
504 ¬it
->dscopes
.trace_packet_header
);
506 BT_PUT(packet_header_type
);
511 enum bt_ctf_notif_iter_status
read_packet_header_continue_state(
512 struct bt_ctf_notif_iter
*notit
)
514 return read_dscope_continue_state(notit
,
515 STATE_AFTER_TRACE_PACKET_HEADER
);
519 bool is_struct_type(struct bt_ctf_field_type
*field_type
)
521 return bt_ctf_field_type_get_type_id(field_type
) ==
522 BT_CTF_TYPE_ID_STRUCT
;
526 bool is_variant_type(struct bt_ctf_field_type
*field_type
)
528 return bt_ctf_field_type_get_type_id(field_type
) ==
529 BT_CTF_TYPE_ID_VARIANT
;
533 enum bt_ctf_notif_iter_status
set_current_stream_class(struct bt_ctf_notif_iter
*notit
)
535 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
536 struct bt_ctf_field_type
*packet_header_type
;
537 struct bt_ctf_field_type
*stream_id_field_type
= NULL
;
540 /* Is there any "stream_id" field in the packet header? */
541 packet_header_type
= bt_ctf_trace_get_packet_header_type(
543 if (!packet_header_type
) {
544 PERR("Failed to retrieve trace's packet header type\n");
545 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
549 assert(is_struct_type(packet_header_type
));
552 stream_id_field_type
=
553 bt_ctf_field_type_structure_get_field_type_by_name(
554 packet_header_type
, "stream_id");
555 if (stream_id_field_type
) {
556 /* Find appropriate stream class using current stream ID */
557 struct bt_ctf_field
*stream_id_field
= NULL
;
560 assert(notit
->dscopes
.trace_packet_header
);
563 stream_id_field
= bt_ctf_field_structure_get_field(
564 notit
->dscopes
.trace_packet_header
, "stream_id");
565 assert(stream_id_field
);
566 ret
= bt_ctf_field_unsigned_integer_get_value(
567 stream_id_field
, &stream_id
);
569 BT_PUT(stream_id_field
);
571 /* Only one stream: pick the first stream class */
572 assert(bt_ctf_trace_get_stream_class_count(
573 notit
->meta
.trace
) == 1);
577 BT_PUT(notit
->meta
.stream_class
);
580 notit
->meta
.stream_class
= bt_ctf_trace_get_stream_class(
581 notit
->meta
.trace
, stream_id
);
582 if (!notit
->meta
.stream_class
) {
583 PERR("Cannot find stream class with ID %" PRIu64
"\n",
585 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
590 BT_PUT(packet_header_type
);
591 BT_PUT(stream_id_field_type
);
597 enum bt_ctf_notif_iter_status
after_packet_header_state(
598 struct bt_ctf_notif_iter
*notit
)
600 enum bt_ctf_notif_iter_status status
;
602 status
= set_current_stream_class(notit
);
603 if (status
== BT_CTF_NOTIF_ITER_STATUS_OK
) {
604 notit
->state
= STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
;
611 enum bt_ctf_notif_iter_status
read_packet_context_begin_state(
612 struct bt_ctf_notif_iter
*notit
)
614 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
615 struct bt_ctf_field_type
*packet_context_type
;
617 assert(notit
->meta
.stream_class
);
618 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(
619 notit
->meta
.stream_class
);
620 if (!packet_context_type
) {
621 PERR("Failed to retrieve stream class's packet context\n");
622 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
626 status
= read_dscope_begin_state(notit
, packet_context_type
,
627 STATE_AFTER_STREAM_PACKET_CONTEXT
,
628 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
629 ¬it
->dscopes
.stream_packet_context
);
632 BT_PUT(packet_context_type
);
637 enum bt_ctf_notif_iter_status
read_packet_context_continue_state(
638 struct bt_ctf_notif_iter
*notit
)
640 return read_dscope_continue_state(notit
,
641 STATE_AFTER_STREAM_PACKET_CONTEXT
);
645 enum bt_ctf_notif_iter_status
set_current_packet_content_sizes(
646 struct bt_ctf_notif_iter
*notit
)
648 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
649 struct bt_ctf_field
*packet_size_field
= NULL
;
650 struct bt_ctf_field
*content_size_field
= NULL
;
651 uint64_t content_size
= -1, packet_size
= -1;
653 assert(notit
->dscopes
.stream_packet_context
);
655 packet_size_field
= bt_ctf_field_structure_get_field(
656 notit
->dscopes
.stream_packet_context
, "packet_size");
657 content_size_field
= bt_ctf_field_structure_get_field(
658 notit
->dscopes
.stream_packet_context
, "content_size");
659 if (packet_size_field
) {
660 int ret
= bt_ctf_field_unsigned_integer_get_value(
661 packet_size_field
, &packet_size
);
664 if (packet_size
== 0) {
665 PERR("Decoded packet size is 0\n");
666 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
668 } else if ((packet_size
% 8) != 0) {
669 PERR("Decoded packet size is not a multiple of 8\n");
670 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
675 if (content_size_field
) {
676 int ret
= bt_ctf_field_unsigned_integer_get_value(
677 content_size_field
, &content_size
);
681 content_size
= packet_size
;
684 notit
->cur_packet_size
= packet_size
;
685 notit
->cur_content_size
= content_size
;
687 BT_PUT(packet_size_field
);
688 BT_PUT(content_size_field
);
693 enum bt_ctf_notif_iter_status
after_packet_context_state(
694 struct bt_ctf_notif_iter
*notit
)
696 enum bt_ctf_notif_iter_status status
;
698 status
= set_current_packet_content_sizes(notit
);
699 if (status
== BT_CTF_NOTIF_ITER_STATUS_OK
) {
700 notit
->state
= STATE_EMIT_NOTIF_NEW_PACKET
;
707 enum bt_ctf_notif_iter_status
read_event_header_begin_state(
708 struct bt_ctf_notif_iter
*notit
)
710 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
711 struct bt_ctf_field_type
*event_header_type
= NULL
;
713 /* Check if we have some content left */
714 if (notit
->cur_content_size
>= 0) {
715 if (packet_at(notit
) == notit
->cur_content_size
) {
716 /* No more events! */
717 notit
->state
= STATE_EMIT_NOTIF_END_OF_PACKET
;
719 } else if (packet_at(notit
) > notit
->cur_content_size
) {
720 /* That's not supposed to happen */
721 PERR("Cursor passed packet's content size:\n");
722 PERR("\tDecoded content size: %zu\n",
723 notit
->cur_content_size
);
724 PERR("\tCursor position: %zu\n", packet_at(notit
));
725 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
730 event_header_type
= bt_ctf_stream_class_get_event_header_type(
731 notit
->meta
.stream_class
);
732 if (!event_header_type
) {
733 PERR("Failed to retrieve stream class's event header type\n");
734 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
738 put_event_dscopes(notit
);
739 status
= read_dscope_begin_state(notit
, event_header_type
,
740 STATE_AFTER_STREAM_EVENT_HEADER
,
741 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
,
742 ¬it
->dscopes
.stream_event_header
);
744 BT_PUT(event_header_type
);
750 enum bt_ctf_notif_iter_status
read_event_header_continue_state(
751 struct bt_ctf_notif_iter
*notit
)
753 return read_dscope_continue_state(notit
,
754 STATE_AFTER_STREAM_EVENT_HEADER
);
758 enum bt_ctf_notif_iter_status
set_current_event_class(struct bt_ctf_notif_iter
*notit
)
761 * The assert() calls in this function are okay because it is
762 * assumed here that all the metadata objects have been
763 * validated for CTF correctness before decoding actual streams.
766 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
767 struct bt_ctf_field_type
*event_header_type
;
768 struct bt_ctf_field_type
*id_field_type
= NULL
;
769 struct bt_ctf_field_type
*v_field_type
= NULL
;
770 uint64_t event_id
= -1ULL;
773 event_header_type
= bt_ctf_stream_class_get_event_header_type(
774 notit
->meta
.stream_class
);
775 if (!event_header_type
) {
776 PERR("Failed to retrieve stream class's event header type\n");
777 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
781 /* Is there any "id"/"v" field in the event header? */
782 assert(is_struct_type(event_header_type
));
783 id_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
784 event_header_type
, "id");
785 v_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
786 event_header_type
, "v");
787 assert(notit
->dscopes
.stream_event_header
);
791 * | | |_ _|_ _| __ __ _
792 * | | | | | || '_ \ / _` |
793 * | |___| | | || | | | (_| | S P E C I A L
794 * |_____|_| |_||_| |_|\__, | C A S E â„¢
797 struct bt_ctf_field
*v_field
= NULL
;
798 struct bt_ctf_field
*v_struct_field
= NULL
;
799 struct bt_ctf_field
*v_struct_id_field
= NULL
;
802 v_field
= bt_ctf_field_structure_get_field(
803 notit
->dscopes
.stream_event_header
, "v");
807 bt_ctf_field_variant_get_current_field(v_field
);
808 if (!v_struct_field
) {
809 goto end_v_field_type
;
814 bt_ctf_field_structure_get_field(v_struct_field
, "id");
815 if (!v_struct_id_field
) {
816 goto end_v_field_type
;
819 ret
= bt_ctf_field_unsigned_integer_get_value(
820 v_struct_id_field
, &event_id
);
827 BT_PUT(v_struct_field
);
828 BT_PUT(v_struct_id_field
);
831 if (id_field_type
&& event_id
== -1ULL) {
832 /* Check "id" field */
833 struct bt_ctf_field
*id_field
= NULL
;
837 id_field
= bt_ctf_field_structure_get_field(
838 notit
->dscopes
.stream_event_header
, "id");
840 assert(bt_ctf_field_is_integer(id_field
) ||
841 bt_ctf_field_is_enumeration(id_field
));
843 if (bt_ctf_field_is_integer(id_field
)) {
844 ret
= bt_ctf_field_unsigned_integer_get_value(
845 id_field
, &event_id
);
847 struct bt_ctf_field
*container
;
849 container
= bt_ctf_field_enumeration_get_container(
852 ret
= bt_ctf_field_unsigned_integer_get_value(
853 container
, &event_id
);
860 if (event_id
== -1ULL) {
861 /* Event ID not found: single event? */
862 assert(bt_ctf_stream_class_get_event_class_count(
863 notit
->meta
.stream_class
) == 1);
867 BT_PUT(notit
->meta
.event_class
);
868 notit
->meta
.event_class
= bt_ctf_stream_class_get_event_class_by_id(
869 notit
->meta
.stream_class
, event_id
);
870 if (!notit
->meta
.event_class
) {
871 PERR("Cannot find event class with ID %" PRIu64
"\n", event_id
);
872 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
877 BT_PUT(event_header_type
);
878 BT_PUT(id_field_type
);
879 BT_PUT(v_field_type
);
885 enum bt_ctf_notif_iter_status
after_event_header_state(
886 struct bt_ctf_notif_iter
*notit
)
888 enum bt_ctf_notif_iter_status status
;
890 status
= set_current_packet_content_sizes(notit
);
891 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
892 PERR("Failed to set current packet and content sizes\n");
896 status
= set_current_event_class(notit
);
897 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
898 PERR("Failed to set current event class\n");
902 notit
->state
= STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
;
909 enum bt_ctf_notif_iter_status
read_stream_event_context_begin_state(
910 struct bt_ctf_notif_iter
*notit
)
912 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
913 struct bt_ctf_field_type
*stream_event_context_type
;
915 stream_event_context_type
= bt_ctf_stream_class_get_event_context_type(
916 notit
->meta
.stream_class
);
917 if (!stream_event_context_type
) {
918 PERR("Failed to retrieve stream class's event context type\n");
919 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
923 status
= read_dscope_begin_state(notit
, stream_event_context_type
,
924 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
,
925 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
,
926 ¬it
->dscopes
.stream_event_context
);
929 BT_PUT(stream_event_context_type
);
935 enum bt_ctf_notif_iter_status
read_stream_event_context_continue_state(
936 struct bt_ctf_notif_iter
*notit
)
938 return read_dscope_continue_state(notit
,
939 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
);
943 enum bt_ctf_notif_iter_status
read_event_context_begin_state(
944 struct bt_ctf_notif_iter
*notit
)
946 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
947 struct bt_ctf_field_type
*event_context_type
;
949 event_context_type
= bt_ctf_event_class_get_context_type(
950 notit
->meta
.event_class
);
951 if (!event_context_type
) {
952 PERR("Failed to retrieve event class's context type\n");
953 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
957 status
= read_dscope_begin_state(notit
, event_context_type
,
958 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
959 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
,
960 ¬it
->dscopes
.event_context
);
963 BT_PUT(event_context_type
);
969 enum bt_ctf_notif_iter_status
read_event_context_continue_state(
970 struct bt_ctf_notif_iter
*notit
)
972 return read_dscope_continue_state(notit
,
973 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
);
977 enum bt_ctf_notif_iter_status
read_event_payload_begin_state(
978 struct bt_ctf_notif_iter
*notit
)
980 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
981 struct bt_ctf_field_type
*event_payload_type
;
983 event_payload_type
= bt_ctf_event_class_get_payload_type(
984 notit
->meta
.event_class
);
985 if (!event_payload_type
) {
986 PERR("Failed to retrieve event class's payload type\n");
987 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
991 status
= read_dscope_begin_state(notit
, event_payload_type
,
992 STATE_EMIT_NOTIF_EVENT
,
993 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
994 ¬it
->dscopes
.event_payload
);
997 BT_PUT(event_payload_type
);
1003 enum bt_ctf_notif_iter_status
read_event_payload_continue_state(
1004 struct bt_ctf_notif_iter
*notit
)
1006 return read_dscope_continue_state(notit
, STATE_EMIT_NOTIF_EVENT
);
1010 enum bt_ctf_notif_iter_status
skip_packet_padding_state(
1011 struct bt_ctf_notif_iter
*notit
)
1013 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1014 size_t bits_to_skip
;
1016 assert(notit
->cur_packet_size
> 0);
1017 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1018 if (bits_to_skip
== 0) {
1019 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1022 size_t bits_to_consume
;
1023 status
= buf_ensure_available_bits(notit
);
1024 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
1028 bits_to_consume
= MIN(buf_available_bits(notit
), bits_to_skip
);
1029 buf_consume_bits(notit
, bits_to_consume
);
1030 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1031 if (bits_to_skip
== 0) {
1032 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1042 enum bt_ctf_notif_iter_status
handle_state(struct bt_ctf_notif_iter
*notit
)
1044 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1046 PDBG("Handling state %d\n", notit
->state
);
1048 // TODO: optimalize!
1049 switch (notit
->state
) {
1051 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1053 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
1054 status
= read_packet_header_begin_state(notit
);
1056 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
1057 status
= read_packet_header_continue_state(notit
);
1059 case STATE_AFTER_TRACE_PACKET_HEADER
:
1060 status
= after_packet_header_state(notit
);
1062 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
1063 status
= read_packet_context_begin_state(notit
);
1065 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
1066 status
= read_packet_context_continue_state(notit
);
1068 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
1069 status
= after_packet_context_state(notit
);
1071 case STATE_EMIT_NOTIF_NEW_PACKET
:
1072 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1074 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
:
1075 status
= read_event_header_begin_state(notit
);
1077 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
:
1078 status
= read_event_header_continue_state(notit
);
1080 case STATE_AFTER_STREAM_EVENT_HEADER
:
1081 status
= after_event_header_state(notit
);
1083 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
:
1084 status
= read_stream_event_context_begin_state(notit
);
1086 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
:
1087 status
= read_stream_event_context_continue_state(notit
);
1089 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN
:
1090 status
= read_event_context_begin_state(notit
);
1092 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
:
1093 status
= read_event_context_continue_state(notit
);
1095 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
1096 status
= read_event_payload_begin_state(notit
);
1098 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
1099 status
= read_event_payload_continue_state(notit
);
1101 case STATE_EMIT_NOTIF_EVENT
:
1102 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1104 case STATE_SKIP_PACKET_PADDING
:
1105 status
= skip_packet_padding_state(notit
);
1107 case STATE_EMIT_NOTIF_END_OF_PACKET
:
1108 notit
->state
= STATE_SKIP_PACKET_PADDING
;
1115 void bt_ctf_notif_iter_reset(struct bt_ctf_notif_iter
*notit
)
1118 stack_clear(notit
->stack
);
1119 BT_PUT(notit
->meta
.stream_class
);
1120 BT_PUT(notit
->meta
.event_class
);
1121 BT_PUT(notit
->packet
);
1122 put_all_dscopes(notit
);
1123 notit
->buf
.addr
= NULL
;
1126 notit
->buf
.packet_offset
= 0;
1127 notit
->state
= STATE_INIT
;
1128 notit
->cur_content_size
= -1;
1129 notit
->cur_packet_size
= -1;
1133 struct bt_ctf_field
*get_next_field(struct bt_ctf_notif_iter
*notit
)
1135 struct bt_ctf_field
*next_field
= NULL
;
1136 struct bt_ctf_field
*base_field
;
1137 struct bt_ctf_field_type
*base_type
;
1140 assert(!stack_empty(notit
->stack
));
1141 index
= stack_top(notit
->stack
)->index
;
1142 base_field
= stack_top(notit
->stack
)->base
;
1143 base_type
= bt_ctf_field_get_type(base_field
);
1145 PERR("Failed to get base field's type\n");
1149 switch (bt_ctf_field_type_get_type_id(base_type
)) {
1150 case BT_CTF_TYPE_ID_STRUCT
:
1151 next_field
= bt_ctf_field_structure_get_field_by_index(
1154 case BT_CTF_TYPE_ID_ARRAY
:
1155 next_field
= bt_ctf_field_array_get_field(base_field
, index
);
1157 case BT_CTF_TYPE_ID_SEQUENCE
:
1158 next_field
= bt_ctf_field_sequence_get_field(base_field
, index
);
1160 case BT_CTF_TYPE_ID_VARIANT
:
1161 next_field
= bt_ctf_field_variant_get_current_field(base_field
);
1175 enum bt_ctf_btr_status
btr_signed_int_cb(int64_t value
,
1176 struct bt_ctf_field_type
*type
, void *data
)
1178 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1179 struct bt_ctf_field
*field
= NULL
;
1180 struct bt_ctf_field
*int_field
= NULL
;
1181 struct bt_ctf_notif_iter
*notit
= data
;
1184 /* create next field */
1185 field
= get_next_field(notit
);
1187 PERR("Failed to get next field (signed int)\n");
1188 status
= BT_CTF_BTR_STATUS_ERROR
;
1192 switch(bt_ctf_field_type_get_type_id(type
)) {
1193 case BT_CTF_TYPE_ID_INTEGER
:
1194 /* Integer field is created field */
1195 BT_MOVE(int_field
, field
);
1197 case BT_CTF_TYPE_ID_ENUM
:
1198 int_field
= bt_ctf_field_enumeration_get_container(field
);
1205 PERR("Failed to get integer field\n");
1206 status
= BT_CTF_BTR_STATUS_ERROR
;
1210 ret
= bt_ctf_field_signed_integer_set_value(int_field
, value
);
1212 stack_top(notit
->stack
)->index
++;
1222 enum bt_ctf_btr_status
btr_unsigned_int_cb(uint64_t value
,
1223 struct bt_ctf_field_type
*type
, void *data
)
1225 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1226 struct bt_ctf_field
*field
= NULL
;
1227 struct bt_ctf_field
*int_field
= NULL
;
1228 struct bt_ctf_notif_iter
*notit
= data
;
1231 /* Create next field */
1232 field
= get_next_field(notit
);
1234 PERR("Failed to get next field (unsigned int)\n");
1235 status
= BT_CTF_BTR_STATUS_ERROR
;
1239 switch(bt_ctf_field_type_get_type_id(type
)) {
1240 case BT_CTF_TYPE_ID_INTEGER
:
1241 /* Integer field is created field */
1242 BT_MOVE(int_field
, field
);
1244 case BT_CTF_TYPE_ID_ENUM
:
1245 int_field
= bt_ctf_field_enumeration_get_container(field
);
1252 PERR("Failed to get integer field\n");
1253 status
= BT_CTF_BTR_STATUS_ERROR
;
1257 ret
= bt_ctf_field_unsigned_integer_set_value(int_field
, value
);
1259 stack_top(notit
->stack
)->index
++;
1269 enum bt_ctf_btr_status
btr_floating_point_cb(double value
,
1270 struct bt_ctf_field_type
*type
, void *data
)
1272 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1273 struct bt_ctf_field
*field
= NULL
;
1274 struct bt_ctf_notif_iter
*notit
= data
;
1277 /* Create next field */
1278 field
= get_next_field(notit
);
1280 PERR("Failed to get next field (floating point number)\n");
1281 status
= BT_CTF_BTR_STATUS_ERROR
;
1285 ret
= bt_ctf_field_floating_point_set_value(field
, value
);
1287 stack_top(notit
->stack
)->index
++;
1296 enum bt_ctf_btr_status
btr_string_begin_cb(
1297 struct bt_ctf_field_type
*type
, void *data
)
1299 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1300 struct bt_ctf_field
*field
= NULL
;
1301 struct bt_ctf_notif_iter
*notit
= data
;
1304 /* Create next field */
1305 field
= get_next_field(notit
);
1307 PERR("Failed to get next field (string)\n");
1308 status
= BT_CTF_BTR_STATUS_ERROR
;
1313 * Push on stack. Not a compound type per se, but we know that only
1314 * btr_string_cb() may be called between this call and a subsequent
1315 * call to btr_string_end_cb().
1317 ret
= stack_push(notit
->stack
, field
);
1319 PERR("Failed to push string field onto the stack\n");
1320 status
= BT_CTF_BTR_STATUS_ERROR
;
1331 enum bt_ctf_btr_status
btr_string_cb(const char *value
,
1332 size_t len
, struct bt_ctf_field_type
*type
, void *data
)
1334 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1335 struct bt_ctf_field
*field
= NULL
;
1336 struct bt_ctf_notif_iter
*notit
= data
;
1339 /* Get string field */
1340 field
= stack_top(notit
->stack
)->base
;
1343 /* Append current string */
1344 ret
= bt_ctf_field_string_append_len(field
, value
, len
);
1346 PERR("Failed to append a string to a string field\n");
1347 status
= BT_CTF_BTR_STATUS_ERROR
;
1356 enum bt_ctf_btr_status
btr_string_end_cb(
1357 struct bt_ctf_field_type
*type
, void *data
)
1359 struct bt_ctf_notif_iter
*notit
= data
;
1361 /* Pop string field */
1362 stack_pop(notit
->stack
);
1364 /* Go to next field */
1365 stack_top(notit
->stack
)->index
++;
1367 return BT_CTF_BTR_STATUS_OK
;
1370 enum bt_ctf_btr_status
btr_compound_begin_cb(
1371 struct bt_ctf_field_type
*type
, void *data
)
1373 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1374 struct bt_ctf_notif_iter
*notit
= data
;
1375 struct bt_ctf_field
*field
;
1379 if (stack_empty(notit
->stack
)) {
1380 /* Root: create dynamic scope field */
1381 *notit
->cur_dscope_field
= bt_ctf_field_create(type
);
1382 field
= *notit
->cur_dscope_field
;
1385 * Field will be put at the end of this function
1386 * (stack_push() will take one reference, but this
1387 * reference is lost upon the equivalent stack_pop()
1388 * later), so also get it for our context to own it.
1390 bt_get(*notit
->cur_dscope_field
);
1392 field
= get_next_field(notit
);
1396 PERR("Failed to get next field or create dynamic scope field\n");
1397 status
= BT_CTF_BTR_STATUS_ERROR
;
1402 ret
= stack_push(notit
->stack
, field
);
1404 PERR("Failed to push compound field onto the stack\n");
1405 status
= BT_CTF_BTR_STATUS_ERROR
;
1415 enum bt_ctf_btr_status
btr_compound_end_cb(
1416 struct bt_ctf_field_type
*type
, void *data
)
1418 struct bt_ctf_notif_iter
*notit
= data
;
1420 assert(!stack_empty(notit
->stack
));
1423 stack_pop(notit
->stack
);
1425 /* If the stack is not empty, increment the base's index */
1426 if (!stack_empty(notit
->stack
)) {
1427 stack_top(notit
->stack
)->index
++;
1430 return BT_CTF_BTR_STATUS_OK
;
1434 struct bt_ctf_field
*resolve_field(struct bt_ctf_notif_iter
*notit
,
1435 struct bt_ctf_field_path
*path
)
1437 struct bt_ctf_field
*field
= NULL
;
1440 switch (bt_ctf_field_path_get_root_scope(path
)) {
1441 case BT_CTF_SCOPE_TRACE_PACKET_HEADER
:
1442 field
= notit
->dscopes
.trace_packet_header
;
1444 case BT_CTF_SCOPE_STREAM_PACKET_CONTEXT
:
1445 field
= notit
->dscopes
.stream_packet_context
;
1447 case BT_CTF_SCOPE_STREAM_EVENT_HEADER
:
1448 field
= notit
->dscopes
.stream_event_header
;
1450 case BT_CTF_SCOPE_STREAM_EVENT_CONTEXT
:
1451 field
= notit
->dscopes
.stream_event_context
;
1453 case BT_CTF_SCOPE_EVENT_CONTEXT
:
1454 field
= notit
->dscopes
.event_context
;
1456 case BT_CTF_SCOPE_EVENT_FIELDS
:
1457 field
= notit
->dscopes
.event_payload
;
1469 for (i
= 0; i
< bt_ctf_field_path_get_index_count(path
); ++i
) {
1470 struct bt_ctf_field
*next_field
= NULL
;
1471 struct bt_ctf_field_type
*field_type
;
1472 int index
= bt_ctf_field_path_get_index(path
, i
);
1474 field_type
= bt_ctf_field_get_type(field
);
1480 if (is_struct_type(field_type
)) {
1481 next_field
= bt_ctf_field_structure_get_field_by_index(
1483 } else if (is_variant_type(field_type
)) {
1485 bt_ctf_field_variant_get_current_field(field
);
1495 /* Move next field -> field */
1496 BT_MOVE(field
, next_field
);
1504 int64_t btr_get_sequence_length_cb(struct bt_ctf_field_type
*type
, void *data
)
1508 struct bt_ctf_field_path
*field_path
;
1509 struct bt_ctf_notif_iter
*notit
= data
;
1510 struct bt_ctf_field
*field
= NULL
;
1513 field_path
= bt_ctf_field_type_sequence_get_length_field_path(type
);
1518 field
= resolve_field(notit
, field_path
);
1523 iret
= bt_ctf_field_unsigned_integer_get_value(field
, &length
);
1528 ret
= (int64_t) length
;
1538 struct bt_ctf_field_type
*btr_get_variant_type_cb(
1539 struct bt_ctf_field_type
*type
, void *data
)
1541 struct bt_ctf_field_path
*path
;
1542 struct bt_ctf_notif_iter
*notit
= data
;
1543 struct bt_ctf_field
*tag_field
= NULL
;
1544 struct bt_ctf_field
*selected_field
= NULL
;
1545 struct bt_ctf_field_type
*selected_field_type
= NULL
;
1547 path
= bt_ctf_field_type_variant_get_tag_field_path(type
);
1552 tag_field
= resolve_field(notit
, path
);
1558 * We found the enumeration tag field instance which should be
1559 * able to select a current field for this variant. This
1560 * callback function we're in is called _after_
1561 * compound_begin(), so the current stack top's base field is
1562 * the variant field in question. We get the selected field here
1563 * thanks to this tag field (thus creating the selected field),
1564 * which will also provide us with its type. Then, this field
1565 * will remain the current selected one until the next callback
1566 * function call which is used to fill the current selected
1569 selected_field
= bt_ctf_field_variant_get_field(
1570 stack_top(notit
->stack
)->base
, tag_field
);
1571 if (!selected_field
) {
1575 selected_field_type
= bt_ctf_field_get_type(selected_field
);
1579 BT_PUT(selected_field
);
1581 return selected_field_type
;
1585 struct bt_ctf_event
*create_event(struct bt_ctf_notif_iter
*notit
)
1588 struct bt_ctf_event
*event
;
1590 /* Create event object. */
1591 event
= bt_ctf_event_create(notit
->meta
.event_class
);
1596 /* Set header, stream event context, context, and payload fields. */
1597 ret
= bt_ctf_event_set_header(event
,
1598 notit
->dscopes
.stream_event_header
);
1603 ret
= bt_ctf_event_set_stream_event_context(event
,
1604 notit
->dscopes
.stream_event_context
);
1609 ret
= bt_ctf_event_set_event_context(event
,
1610 notit
->dscopes
.event_context
);
1615 ret
= bt_ctf_event_set_payload_field(event
,
1616 notit
->dscopes
.event_payload
);
1621 /* Associate with current packet. */
1622 assert(notit
->packet
);
1623 ret
= bt_ctf_event_set_packet(event
, notit
->packet
);
1636 void create_packet(struct bt_ctf_notif_iter
*notit
)
1639 struct bt_ctf_stream
*stream
= NULL
;
1640 struct bt_ctf_packet
*packet
= NULL
;
1642 /* Ask the user for the stream */
1643 stream
= notit
->medium
.medops
.get_stream(notit
->meta
.stream_class
,
1644 notit
->medium
.data
);
1650 packet
= bt_ctf_packet_create(stream
);
1655 /* Set packet's context and header fields */
1656 if (notit
->dscopes
.trace_packet_header
) {
1657 ret
= bt_ctf_packet_set_header(packet
,
1658 notit
->dscopes
.trace_packet_header
);
1664 if (notit
->dscopes
.stream_packet_context
) {
1665 ret
= bt_ctf_packet_set_context(packet
,
1666 notit
->dscopes
.stream_packet_context
);
1676 BT_MOVE(notit
->packet
, packet
);
1680 void notify_new_packet(struct bt_ctf_notif_iter
*notit
,
1681 struct bt_notification
**notification
)
1683 struct bt_notification
*ret
;
1685 /* Initialize the iterator's current packet */
1686 create_packet(notit
);
1687 if (!notit
->packet
) {
1691 ret
= bt_notification_packet_start_create(notit
->packet
);
1695 *notification
= ret
;
1699 void notify_end_of_packet(struct bt_ctf_notif_iter
*notit
,
1700 struct bt_notification
**notification
)
1702 struct bt_notification
*ret
;
1704 if (!notit
->packet
) {
1708 ret
= bt_notification_packet_end_create(notit
->packet
);
1712 BT_PUT(notit
->packet
);
1713 *notification
= ret
;
1717 void notify_event(struct bt_ctf_notif_iter
*notit
,
1718 struct bt_notification
**notification
)
1720 struct bt_ctf_event
*event
;
1721 struct bt_notification
*ret
= NULL
;
1724 event
= create_event(notit
);
1729 ret
= bt_notification_event_create(event
);
1733 *notification
= ret
;
1738 struct bt_ctf_notif_iter
*bt_ctf_notif_iter_create(struct bt_ctf_trace
*trace
,
1739 size_t max_request_sz
,
1740 struct bt_ctf_notif_iter_medium_ops medops
,
1741 void *data
, FILE *err_stream
)
1743 struct bt_ctf_notif_iter
*notit
= NULL
;
1744 struct bt_ctf_btr_cbs cbs
= {
1746 .signed_int
= btr_signed_int_cb
,
1747 .unsigned_int
= btr_unsigned_int_cb
,
1748 .floating_point
= btr_floating_point_cb
,
1749 .string_begin
= btr_string_begin_cb
,
1750 .string
= btr_string_cb
,
1751 .string_end
= btr_string_end_cb
,
1752 .compound_begin
= btr_compound_begin_cb
,
1753 .compound_end
= btr_compound_end_cb
,
1756 .get_sequence_length
= btr_get_sequence_length_cb
,
1757 .get_variant_type
= btr_get_variant_type_cb
,
1762 assert(medops
.request_bytes
);
1763 notit
= g_new0(struct bt_ctf_notif_iter
, 1);
1765 PERR("Failed to allocate memory for CTF notification iterator\n");
1769 notit
->meta
.trace
= trace
;
1770 bt_get(notit
->meta
.trace
);
1771 notit
->medium
.medops
= medops
;
1772 notit
->medium
.max_request_sz
= max_request_sz
;
1773 notit
->medium
.data
= data
;
1774 notit
->err_stream
= err_stream
;
1775 notit
->stack
= stack_new(notit
);
1776 if (!notit
->stack
) {
1777 PERR("Failed to create stack\n");
1778 bt_ctf_notif_iter_destroy(notit
);
1783 notit
->btr
= bt_ctf_btr_create(cbs
, notit
, err_stream
);
1785 PERR("Failed to create binary type reader\n");
1786 bt_ctf_notif_iter_destroy(notit
);
1791 bt_ctf_notif_iter_reset(notit
);
1797 void bt_ctf_notif_iter_destroy(struct bt_ctf_notif_iter
*notit
)
1799 BT_PUT(notit
->meta
.trace
);
1800 BT_PUT(notit
->meta
.stream_class
);
1801 BT_PUT(notit
->meta
.event_class
);
1802 BT_PUT(notit
->packet
);
1803 put_all_dscopes(notit
);
1806 stack_destroy(notit
->stack
);
1810 bt_ctf_btr_destroy(notit
->btr
);
1816 enum bt_ctf_notif_iter_status
bt_ctf_notif_iter_get_next_notification(
1817 struct bt_ctf_notif_iter
*notit
,
1818 struct bt_notification
**notification
)
1820 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1823 assert(notification
);
1826 status
= handle_state(notit
);
1827 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
1828 if (status
== BT_CTF_NOTIF_ITER_STATUS_EOF
) {
1829 PDBG("Medium operation reported end of stream\n");
1831 PERR("Failed to handle state:\n");
1832 PERR("\tState: %d\n", notit
->state
);
1837 switch (notit
->state
) {
1838 case STATE_EMIT_NOTIF_NEW_PACKET
:
1839 PDBG("Emitting new packet notification\n");
1840 notify_new_packet(notit
, notification
);
1841 if (!*notification
) {
1842 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1845 case STATE_EMIT_NOTIF_EVENT
:
1846 PDBG("Emitting event notification\n");
1847 notify_event(notit
, notification
);
1848 if (!*notification
) {
1849 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1852 case STATE_EMIT_NOTIF_END_OF_PACKET
:
1853 PDBG("Emitting end of packet notification\n");
1854 notify_end_of_packet(notit
, notification
);
1855 if (!*notification
) {
1856 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1860 /* Non-emitting state: continue */