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
;
177 /* bt_ctf_clock to uint64_t. */
178 GHashTable
*clock_states
;
182 int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter
*notit
);
185 void stack_entry_free_func(gpointer data
)
187 struct stack_entry
*entry
= data
;
194 struct stack
*stack_new(struct bt_ctf_notif_iter
*notit
)
196 struct stack
*stack
= NULL
;
198 stack
= g_new0(struct stack
, 1);
203 stack
->entries
= g_ptr_array_new_with_free_func(stack_entry_free_func
);
204 if (!stack
->entries
) {
215 void stack_destroy(struct stack
*stack
)
218 g_ptr_array_free(stack
->entries
, TRUE
);
223 int stack_push(struct stack
*stack
, struct bt_ctf_field
*base
)
226 struct stack_entry
*entry
;
230 entry
= g_new0(struct stack_entry
, 1);
236 entry
->base
= bt_get(base
);
237 g_ptr_array_add(stack
->entries
, entry
);
244 unsigned int stack_size(struct stack
*stack
)
248 return stack
->entries
->len
;
252 void stack_pop(struct stack
*stack
)
255 assert(stack_size(stack
));
256 g_ptr_array_remove_index(stack
->entries
, stack
->entries
->len
- 1);
260 struct stack_entry
*stack_top(struct stack
*stack
)
263 assert(stack_size(stack
));
265 return g_ptr_array_index(stack
->entries
, stack
->entries
->len
- 1);
269 bool stack_empty(struct stack
*stack
)
271 return stack_size(stack
) == 0;
275 void stack_clear(struct stack
*stack
)
279 if (!stack_empty(stack
)) {
280 g_ptr_array_remove_range(stack
->entries
, 0, stack_size(stack
));
283 assert(stack_empty(stack
));
287 enum bt_ctf_notif_iter_status
notif_iter_status_from_m_status(
288 enum bt_ctf_notif_iter_medium_status m_status
)
294 size_t buf_size_bits(struct bt_ctf_notif_iter
*notit
)
296 return BYTES_TO_BITS(notit
->buf
.sz
);
300 size_t buf_available_bits(struct bt_ctf_notif_iter
*notit
)
302 return buf_size_bits(notit
) - notit
->buf
.at
;
306 size_t packet_at(struct bt_ctf_notif_iter
*notit
)
308 return notit
->buf
.packet_offset
+ notit
->buf
.at
;
312 size_t remaining_content_bits(struct bt_ctf_notif_iter
*notit
)
314 if (notit
->cur_content_size
== -1) {
318 return notit
->cur_content_size
- packet_at(notit
);
322 size_t remaining_packet_bits(struct bt_ctf_notif_iter
*notit
)
324 if (notit
->cur_packet_size
== -1) {
328 return notit
->cur_packet_size
- packet_at(notit
);
332 void buf_consume_bits(struct bt_ctf_notif_iter
*notit
, size_t incr
)
334 notit
->buf
.at
+= incr
;
338 bool buf_has_enough_bits(struct bt_ctf_notif_iter
*notit
, size_t sz
)
340 return buf_available_bits(notit
) >= sz
;
344 enum bt_ctf_notif_iter_status
request_medium_bytes(
345 struct bt_ctf_notif_iter
*notit
)
347 uint8_t *buffer_addr
;
349 enum bt_ctf_notif_iter_medium_status m_status
;
351 m_status
= notit
->medium
.medops
.request_bytes(
352 notit
->medium
.max_request_sz
, &buffer_addr
,
353 &buffer_sz
, notit
->medium
.data
);
354 if (m_status
== BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK
) {
355 assert(buffer_sz
!= 0);
357 /* New packet offset is old one + old size (in bits) */
358 notit
->buf
.packet_offset
+= buf_size_bits(notit
);
360 /* Restart at the beginning of the new medium buffer */
363 /* New medium buffer size */
364 notit
->buf
.sz
= buffer_sz
;
366 /* New medium buffer address */
367 notit
->buf
.addr
= buffer_addr
;
370 return notif_iter_status_from_m_status(m_status
);
374 enum bt_ctf_notif_iter_status
buf_ensure_available_bits(
375 struct bt_ctf_notif_iter
*notit
)
377 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
379 if (buf_available_bits(notit
) == 0) {
381 * This _cannot_ return BT_CTF_NOTIF_ITER_STATUS_OK
384 status
= request_medium_bytes(notit
);
391 enum bt_ctf_notif_iter_status
read_dscope_begin_state(
392 struct bt_ctf_notif_iter
*notit
,
393 struct bt_ctf_field_type
*dscope_field_type
,
394 enum state done_state
, enum state continue_state
,
395 struct bt_ctf_field
**dscope_field
)
397 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
398 enum bt_ctf_btr_status btr_status
;
399 size_t consumed_bits
;
401 status
= buf_ensure_available_bits(notit
);
402 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
406 bt_put(*dscope_field
);
407 notit
->cur_dscope_field
= dscope_field
;
408 consumed_bits
= bt_ctf_btr_start(notit
->btr
, dscope_field_type
,
409 notit
->buf
.addr
, notit
->buf
.at
, packet_at(notit
),
410 notit
->buf
.sz
, &btr_status
);
412 switch (btr_status
) {
413 case BT_CTF_BTR_STATUS_OK
:
414 /* type was read completely */
415 notit
->state
= done_state
;
417 case BT_CTF_BTR_STATUS_EOF
:
418 notit
->state
= continue_state
;
421 PERR("Binary type reader failed to start\n");
422 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
426 /* Consume bits now since we know we're not in an error state */
427 buf_consume_bits(notit
, consumed_bits
);
434 enum bt_ctf_notif_iter_status
read_dscope_continue_state(
435 struct bt_ctf_notif_iter
*notit
, enum state done_state
)
437 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
438 enum bt_ctf_btr_status btr_status
;
439 size_t consumed_bits
;
441 status
= buf_ensure_available_bits(notit
);
442 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
446 consumed_bits
= bt_ctf_btr_continue(notit
->btr
, notit
->buf
.addr
,
447 notit
->buf
.sz
, &btr_status
);
449 switch (btr_status
) {
450 case BT_CTF_BTR_STATUS_OK
:
451 /* Type was read completely. */
452 notit
->state
= done_state
;
454 case BT_CTF_BTR_STATUS_EOF
:
455 /* Stay in this continue state. */
458 PERR("Binary type reader failed to continue\n");
459 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
463 /* Consume bits now since we know we're not in an error state. */
464 buf_consume_bits(notit
, consumed_bits
);
470 void put_event_dscopes(struct bt_ctf_notif_iter
*notit
)
472 BT_PUT(notit
->dscopes
.stream_event_header
);
473 BT_PUT(notit
->dscopes
.stream_event_context
);
474 BT_PUT(notit
->dscopes
.event_context
);
475 BT_PUT(notit
->dscopes
.event_payload
);
479 void put_all_dscopes(struct bt_ctf_notif_iter
*notit
)
481 BT_PUT(notit
->dscopes
.trace_packet_header
);
482 BT_PUT(notit
->dscopes
.stream_packet_context
);
483 put_event_dscopes(notit
);
487 enum bt_ctf_notif_iter_status
read_packet_header_begin_state(
488 struct bt_ctf_notif_iter
*notit
)
490 struct bt_ctf_field_type
*packet_header_type
= NULL
;
491 enum bt_ctf_notif_iter_status ret
= BT_CTF_NOTIF_ITER_STATUS_OK
;
493 if (bt_ctf_notif_iter_switch_packet(notit
)) {
494 ret
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
498 /* Packet header type is common to the whole trace. */
499 packet_header_type
= bt_ctf_trace_get_packet_header_type(
501 if (!packet_header_type
) {
502 PERR("Failed to retrieve trace's packet header type\n");
503 ret
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
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(struct bt_ctf_notif_iter
*notit
)
541 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
542 struct bt_ctf_field_type
*packet_header_type
;
543 struct bt_ctf_field_type
*stream_id_field_type
= NULL
;
546 /* Is there any "stream_id" field in the packet header? */
547 packet_header_type
= bt_ctf_trace_get_packet_header_type(
549 if (!packet_header_type
) {
550 PERR("Failed to retrieve trace's packet header type\n");
551 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
555 assert(is_struct_type(packet_header_type
));
558 stream_id_field_type
=
559 bt_ctf_field_type_structure_get_field_type_by_name(
560 packet_header_type
, "stream_id");
561 if (stream_id_field_type
) {
562 /* Find appropriate stream class using current stream ID */
563 struct bt_ctf_field
*stream_id_field
= NULL
;
566 assert(notit
->dscopes
.trace_packet_header
);
569 stream_id_field
= bt_ctf_field_structure_get_field(
570 notit
->dscopes
.trace_packet_header
, "stream_id");
571 assert(stream_id_field
);
572 ret
= bt_ctf_field_unsigned_integer_get_value(
573 stream_id_field
, &stream_id
);
575 BT_PUT(stream_id_field
);
577 /* Only one stream: pick the first stream class */
578 assert(bt_ctf_trace_get_stream_class_count(
579 notit
->meta
.trace
) == 1);
583 BT_PUT(notit
->meta
.stream_class
);
584 notit
->meta
.stream_class
= bt_ctf_trace_get_stream_class_by_id(
585 notit
->meta
.trace
, stream_id
);
586 if (!notit
->meta
.stream_class
) {
587 PERR("Cannot find stream class with ID %" PRIu64
"\n",
589 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
594 BT_PUT(packet_header_type
);
595 BT_PUT(stream_id_field_type
);
601 enum bt_ctf_notif_iter_status
after_packet_header_state(
602 struct bt_ctf_notif_iter
*notit
)
604 enum bt_ctf_notif_iter_status status
;
606 status
= set_current_stream_class(notit
);
607 if (status
== BT_CTF_NOTIF_ITER_STATUS_OK
) {
608 notit
->state
= STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
;
615 enum bt_ctf_notif_iter_status
read_packet_context_begin_state(
616 struct bt_ctf_notif_iter
*notit
)
618 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
619 struct bt_ctf_field_type
*packet_context_type
;
621 assert(notit
->meta
.stream_class
);
622 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(
623 notit
->meta
.stream_class
);
624 if (!packet_context_type
) {
625 PERR("Failed to retrieve stream class's packet context\n");
626 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
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 PERR("Failed to retrieve stream class's event header type\n");
738 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
742 put_event_dscopes(notit
);
743 status
= read_dscope_begin_state(notit
, event_header_type
,
744 STATE_AFTER_STREAM_EVENT_HEADER
,
745 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
,
746 ¬it
->dscopes
.stream_event_header
);
748 BT_PUT(event_header_type
);
754 enum bt_ctf_notif_iter_status
read_event_header_continue_state(
755 struct bt_ctf_notif_iter
*notit
)
757 return read_dscope_continue_state(notit
,
758 STATE_AFTER_STREAM_EVENT_HEADER
);
762 enum bt_ctf_notif_iter_status
set_current_event_class(struct bt_ctf_notif_iter
*notit
)
765 * The assert() calls in this function are okay because it is
766 * assumed here that all the metadata objects have been
767 * validated for CTF correctness before decoding actual streams.
770 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
771 struct bt_ctf_field_type
*event_header_type
;
772 struct bt_ctf_field_type
*id_field_type
= NULL
;
773 struct bt_ctf_field_type
*v_field_type
= NULL
;
774 uint64_t event_id
= -1ULL;
777 event_header_type
= bt_ctf_stream_class_get_event_header_type(
778 notit
->meta
.stream_class
);
779 if (!event_header_type
) {
780 PERR("Failed to retrieve stream class's event header type\n");
781 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
785 /* Is there any "id"/"v" field in the event header? */
786 assert(is_struct_type(event_header_type
));
787 id_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
788 event_header_type
, "id");
789 v_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
790 event_header_type
, "v");
791 assert(notit
->dscopes
.stream_event_header
);
795 * | | |_ _|_ _| __ __ _
796 * | | | | | || '_ \ / _` |
797 * | |___| | | || | | | (_| | S P E C I A L
798 * |_____|_| |_||_| |_|\__, | C A S E â„¢
801 struct bt_ctf_field
*v_field
= NULL
;
802 struct bt_ctf_field
*v_struct_field
= NULL
;
803 struct bt_ctf_field
*v_struct_id_field
= NULL
;
806 v_field
= bt_ctf_field_structure_get_field(
807 notit
->dscopes
.stream_event_header
, "v");
811 bt_ctf_field_variant_get_current_field(v_field
);
812 if (!v_struct_field
) {
813 goto end_v_field_type
;
818 bt_ctf_field_structure_get_field(v_struct_field
, "id");
819 if (!v_struct_id_field
) {
820 goto end_v_field_type
;
823 ret
= bt_ctf_field_unsigned_integer_get_value(
824 v_struct_id_field
, &event_id
);
831 BT_PUT(v_struct_field
);
832 BT_PUT(v_struct_id_field
);
835 if (id_field_type
&& event_id
== -1ULL) {
836 /* Check "id" field */
837 struct bt_ctf_field
*id_field
= NULL
;
841 id_field
= bt_ctf_field_structure_get_field(
842 notit
->dscopes
.stream_event_header
, "id");
844 assert(bt_ctf_field_is_integer(id_field
) ||
845 bt_ctf_field_is_enumeration(id_field
));
847 if (bt_ctf_field_is_integer(id_field
)) {
848 ret
= bt_ctf_field_unsigned_integer_get_value(
849 id_field
, &event_id
);
851 struct bt_ctf_field
*container
;
853 container
= bt_ctf_field_enumeration_get_container(
856 ret
= bt_ctf_field_unsigned_integer_get_value(
857 container
, &event_id
);
864 if (event_id
== -1ULL) {
865 /* Event ID not found: single event? */
866 assert(bt_ctf_stream_class_get_event_class_count(
867 notit
->meta
.stream_class
) == 1);
871 BT_PUT(notit
->meta
.event_class
);
872 notit
->meta
.event_class
= bt_ctf_stream_class_get_event_class_by_id(
873 notit
->meta
.stream_class
, event_id
);
874 if (!notit
->meta
.event_class
) {
875 PERR("Cannot find event class with ID %" PRIu64
"\n", event_id
);
876 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
881 BT_PUT(event_header_type
);
882 BT_PUT(id_field_type
);
883 BT_PUT(v_field_type
);
889 enum bt_ctf_notif_iter_status
after_event_header_state(
890 struct bt_ctf_notif_iter
*notit
)
892 enum bt_ctf_notif_iter_status status
;
894 status
= set_current_event_class(notit
);
895 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
896 PERR("Failed to set current event class\n");
900 notit
->state
= STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
;
907 enum bt_ctf_notif_iter_status
read_stream_event_context_begin_state(
908 struct bt_ctf_notif_iter
*notit
)
910 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
911 struct bt_ctf_field_type
*stream_event_context_type
;
913 stream_event_context_type
= bt_ctf_stream_class_get_event_context_type(
914 notit
->meta
.stream_class
);
915 if (!stream_event_context_type
) {
916 PERR("Failed to retrieve stream class's event context type\n");
917 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
921 status
= read_dscope_begin_state(notit
, stream_event_context_type
,
922 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
,
923 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
,
924 ¬it
->dscopes
.stream_event_context
);
927 BT_PUT(stream_event_context_type
);
933 enum bt_ctf_notif_iter_status
read_stream_event_context_continue_state(
934 struct bt_ctf_notif_iter
*notit
)
936 return read_dscope_continue_state(notit
,
937 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
);
941 enum bt_ctf_notif_iter_status
read_event_context_begin_state(
942 struct bt_ctf_notif_iter
*notit
)
944 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
945 struct bt_ctf_field_type
*event_context_type
;
947 event_context_type
= bt_ctf_event_class_get_context_type(
948 notit
->meta
.event_class
);
949 if (!event_context_type
) {
950 PERR("Failed to retrieve event class's context type\n");
951 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
955 status
= read_dscope_begin_state(notit
, event_context_type
,
956 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
957 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
,
958 ¬it
->dscopes
.event_context
);
961 BT_PUT(event_context_type
);
967 enum bt_ctf_notif_iter_status
read_event_context_continue_state(
968 struct bt_ctf_notif_iter
*notit
)
970 return read_dscope_continue_state(notit
,
971 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
);
975 enum bt_ctf_notif_iter_status
read_event_payload_begin_state(
976 struct bt_ctf_notif_iter
*notit
)
978 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
979 struct bt_ctf_field_type
*event_payload_type
;
981 event_payload_type
= bt_ctf_event_class_get_payload_type(
982 notit
->meta
.event_class
);
983 if (!event_payload_type
) {
984 PERR("Failed to retrieve event class's payload type\n");
985 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
989 status
= read_dscope_begin_state(notit
, event_payload_type
,
990 STATE_EMIT_NOTIF_EVENT
,
991 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
992 ¬it
->dscopes
.event_payload
);
995 BT_PUT(event_payload_type
);
1001 enum bt_ctf_notif_iter_status
read_event_payload_continue_state(
1002 struct bt_ctf_notif_iter
*notit
)
1004 return read_dscope_continue_state(notit
, STATE_EMIT_NOTIF_EVENT
);
1008 enum bt_ctf_notif_iter_status
skip_packet_padding_state(
1009 struct bt_ctf_notif_iter
*notit
)
1011 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1012 size_t bits_to_skip
;
1014 assert(notit
->cur_packet_size
> 0);
1015 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1016 if (bits_to_skip
== 0) {
1017 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1020 size_t bits_to_consume
;
1021 status
= buf_ensure_available_bits(notit
);
1022 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
1026 bits_to_consume
= MIN(buf_available_bits(notit
), bits_to_skip
);
1027 buf_consume_bits(notit
, bits_to_consume
);
1028 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1029 if (bits_to_skip
== 0) {
1030 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1040 enum bt_ctf_notif_iter_status
handle_state(struct bt_ctf_notif_iter
*notit
)
1042 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1044 PDBG("Handling state %d\n", notit
->state
);
1046 // TODO: optimalize!
1047 switch (notit
->state
) {
1049 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1051 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
1052 status
= read_packet_header_begin_state(notit
);
1054 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
1055 status
= read_packet_header_continue_state(notit
);
1057 case STATE_AFTER_TRACE_PACKET_HEADER
:
1058 status
= after_packet_header_state(notit
);
1060 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
1061 status
= read_packet_context_begin_state(notit
);
1063 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
1064 status
= read_packet_context_continue_state(notit
);
1066 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
1067 status
= after_packet_context_state(notit
);
1069 case STATE_EMIT_NOTIF_NEW_PACKET
:
1070 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1072 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
:
1073 status
= read_event_header_begin_state(notit
);
1075 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
:
1076 status
= read_event_header_continue_state(notit
);
1078 case STATE_AFTER_STREAM_EVENT_HEADER
:
1079 status
= after_event_header_state(notit
);
1081 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
:
1082 status
= read_stream_event_context_begin_state(notit
);
1084 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
:
1085 status
= read_stream_event_context_continue_state(notit
);
1087 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN
:
1088 status
= read_event_context_begin_state(notit
);
1090 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
:
1091 status
= read_event_context_continue_state(notit
);
1093 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
1094 status
= read_event_payload_begin_state(notit
);
1096 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
1097 status
= read_event_payload_continue_state(notit
);
1099 case STATE_EMIT_NOTIF_EVENT
:
1100 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1102 case STATE_SKIP_PACKET_PADDING
:
1103 status
= skip_packet_padding_state(notit
);
1105 case STATE_EMIT_NOTIF_END_OF_PACKET
:
1106 notit
->state
= STATE_SKIP_PACKET_PADDING
;
1115 * Resets the internal state of a CTF notification iterator.
1117 * This function can be used when it is desired to seek to the beginning
1118 * of another packet. It is expected that the next call to
1119 * bt_ctf_notif_iter_medium_ops::request_bytes() made by this
1120 * notification iterator will return the \em first bytes of a \em
1123 * @param notif_iter CTF notification iterator
1126 void bt_ctf_notif_iter_reset(struct bt_ctf_notif_iter
*notit
)
1129 stack_clear(notit
->stack
);
1130 BT_PUT(notit
->meta
.stream_class
);
1131 BT_PUT(notit
->meta
.event_class
);
1132 BT_PUT(notit
->packet
);
1133 put_all_dscopes(notit
);
1134 notit
->buf
.addr
= NULL
;
1137 notit
->buf
.packet_offset
= 0;
1138 notit
->state
= STATE_INIT
;
1139 notit
->cur_content_size
= -1;
1140 notit
->cur_packet_size
= -1;
1144 int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter
*notit
)
1149 stack_clear(notit
->stack
);
1150 BT_PUT(notit
->meta
.stream_class
);
1151 BT_PUT(notit
->meta
.event_class
);
1152 BT_PUT(notit
->packet
);
1153 put_all_dscopes(notit
);
1156 * Adjust current buffer so that addr points to the beginning of the new
1159 if (notit
->buf
.addr
) {
1160 size_t consumed_bytes
= (size_t) (notit
->buf
.at
/ CHAR_BIT
);
1162 /* Packets are assumed to start on a byte frontier. */
1163 if (notit
->buf
.at
% CHAR_BIT
) {
1168 notit
->buf
.addr
+= consumed_bytes
;
1169 notit
->buf
.sz
-= consumed_bytes
;
1171 notit
->buf
.packet_offset
= 0;
1174 notit
->cur_content_size
= -1;
1175 notit
->cur_packet_size
= -1;
1181 struct bt_ctf_field
*get_next_field(struct bt_ctf_notif_iter
*notit
)
1183 struct bt_ctf_field
*next_field
= NULL
;
1184 struct bt_ctf_field
*base_field
;
1185 struct bt_ctf_field_type
*base_type
;
1188 assert(!stack_empty(notit
->stack
));
1189 index
= stack_top(notit
->stack
)->index
;
1190 base_field
= stack_top(notit
->stack
)->base
;
1191 base_type
= bt_ctf_field_get_type(base_field
);
1193 PERR("Failed to get base field's type\n");
1197 switch (bt_ctf_field_type_get_type_id(base_type
)) {
1198 case BT_CTF_TYPE_ID_STRUCT
:
1199 next_field
= bt_ctf_field_structure_get_field_by_index(
1202 case BT_CTF_TYPE_ID_ARRAY
:
1203 next_field
= bt_ctf_field_array_get_field(base_field
, index
);
1205 case BT_CTF_TYPE_ID_SEQUENCE
:
1206 next_field
= bt_ctf_field_sequence_get_field(base_field
, index
);
1208 case BT_CTF_TYPE_ID_VARIANT
:
1209 next_field
= bt_ctf_field_variant_get_current_field(base_field
);
1227 void update_clock_state(uint64_t *state
,
1228 struct bt_ctf_field
*value_field
)
1230 struct bt_ctf_field_type
*value_type
= NULL
;
1231 uint64_t requested_new_value
;
1232 uint64_t requested_new_value_mask
;
1233 uint64_t cur_value_masked
;
1234 int requested_new_value_size
;
1237 value_type
= bt_ctf_field_get_type(value_field
);
1240 requested_new_value_size
=
1241 bt_ctf_field_type_integer_get_size(value_type
);
1242 assert(requested_new_value_size
> 0);
1244 ret
= bt_ctf_field_unsigned_integer_get_value(value_field
,
1245 &requested_new_value
);
1249 * Special case for a 64-bit new value, which is the limit
1250 * of a clock value as of this version: overwrite the
1251 * current value directly.
1253 if (requested_new_value_size
== 64) {
1254 *state
= requested_new_value
;
1258 requested_new_value_mask
= (1ULL << requested_new_value_size
) - 1;
1259 cur_value_masked
= *state
& requested_new_value_mask
;
1261 if (requested_new_value
< cur_value_masked
) {
1263 * It looks like a wrap happened on the number of bits
1264 * of the requested new value. Assume that the clock
1265 * value wrapped only one time.
1267 *state
+= requested_new_value_mask
+ 1;
1270 /* Clear the low bits of the current clock value. */
1271 *state
&= ~requested_new_value_mask
;
1273 /* Set the low bits of the current clock value. */
1274 *state
|= requested_new_value
;
1280 enum bt_ctf_btr_status
update_clock(struct bt_ctf_notif_iter
*notit
,
1281 struct bt_ctf_field_type
*int_field_type
,
1282 struct bt_ctf_field
*int_field
)
1284 gboolean clock_found
;
1285 uint64_t *clock_state
;
1286 enum bt_ctf_btr_status ret
= BT_CTF_BTR_STATUS_OK
;
1287 struct bt_ctf_clock
*clock
= bt_ctf_field_type_integer_get_mapped_clock(
1290 if (likely(!clock
)) {
1294 clock_found
= g_hash_table_lookup_extended(notit
->clock_states
,
1295 clock
, NULL
, (gpointer
) &clock_state
);
1296 if (unlikely(!clock_found
)) {
1297 const char *clock_name
= bt_ctf_clock_get_name(clock
);
1299 PERR("Unknown clock %s mapped to integer encountered in stream\n",
1300 clock_name
? : "NULL");
1301 ret
= BT_CTF_BTR_STATUS_ERROR
;
1305 if (unlikely(!clock_state
)) {
1306 clock_state
= g_new0(uint64_t, 1);
1308 ret
= BT_CTF_BTR_STATUS_ENOMEM
;
1311 g_hash_table_insert(notit
->clock_states
, bt_get(clock
),
1315 /* Update the clock's state. */
1316 update_clock_state(clock_state
, int_field
);
1324 enum bt_ctf_btr_status
btr_signed_int_cb(int64_t value
,
1325 struct bt_ctf_field_type
*type
, void *data
)
1327 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1328 struct bt_ctf_field
*field
= NULL
;
1329 struct bt_ctf_field
*int_field
= NULL
;
1330 struct bt_ctf_notif_iter
*notit
= data
;
1333 /* create next field */
1334 field
= get_next_field(notit
);
1336 PERR("Failed to get next field (signed int)\n");
1337 status
= BT_CTF_BTR_STATUS_ERROR
;
1341 switch(bt_ctf_field_type_get_type_id(type
)) {
1342 case BT_CTF_TYPE_ID_INTEGER
:
1343 /* Integer field is created field */
1344 BT_MOVE(int_field
, field
);
1347 case BT_CTF_TYPE_ID_ENUM
:
1348 int_field
= bt_ctf_field_enumeration_get_container(field
);
1349 type
= bt_ctf_field_get_type(int_field
);
1358 PERR("Failed to get integer field\n");
1359 status
= BT_CTF_BTR_STATUS_ERROR
;
1363 ret
= bt_ctf_field_signed_integer_set_value(int_field
, value
);
1365 stack_top(notit
->stack
)->index
++;
1366 status
= update_clock(notit
, type
, int_field
);
1376 enum bt_ctf_btr_status
btr_unsigned_int_cb(uint64_t value
,
1377 struct bt_ctf_field_type
*type
, void *data
)
1379 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1380 struct bt_ctf_field
*field
= NULL
;
1381 struct bt_ctf_field
*int_field
= NULL
;
1382 struct bt_ctf_notif_iter
*notit
= data
;
1385 /* Create next field */
1386 field
= get_next_field(notit
);
1388 PERR("Failed to get next field (unsigned int)\n");
1389 status
= BT_CTF_BTR_STATUS_ERROR
;
1393 switch(bt_ctf_field_type_get_type_id(type
)) {
1394 case BT_CTF_TYPE_ID_INTEGER
:
1395 /* Integer field is created field */
1396 BT_MOVE(int_field
, field
);
1399 case BT_CTF_TYPE_ID_ENUM
:
1400 int_field
= bt_ctf_field_enumeration_get_container(field
);
1401 type
= bt_ctf_field_get_type(int_field
);
1410 PERR("Failed to get integer field\n");
1411 status
= BT_CTF_BTR_STATUS_ERROR
;
1415 ret
= bt_ctf_field_unsigned_integer_set_value(int_field
, value
);
1417 stack_top(notit
->stack
)->index
++;
1418 status
= update_clock(notit
, type
, int_field
);
1428 enum bt_ctf_btr_status
btr_floating_point_cb(double value
,
1429 struct bt_ctf_field_type
*type
, void *data
)
1431 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1432 struct bt_ctf_field
*field
= NULL
;
1433 struct bt_ctf_notif_iter
*notit
= data
;
1436 /* Create next field */
1437 field
= get_next_field(notit
);
1439 PERR("Failed to get next field (floating point number)\n");
1440 status
= BT_CTF_BTR_STATUS_ERROR
;
1444 ret
= bt_ctf_field_floating_point_set_value(field
, value
);
1446 stack_top(notit
->stack
)->index
++;
1455 enum bt_ctf_btr_status
btr_string_begin_cb(
1456 struct bt_ctf_field_type
*type
, void *data
)
1458 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1459 struct bt_ctf_field
*field
= NULL
;
1460 struct bt_ctf_notif_iter
*notit
= data
;
1463 /* Create next field */
1464 field
= get_next_field(notit
);
1466 PERR("Failed to get next field (string)\n");
1467 status
= BT_CTF_BTR_STATUS_ERROR
;
1472 * Push on stack. Not a compound type per se, but we know that only
1473 * btr_string_cb() may be called between this call and a subsequent
1474 * call to btr_string_end_cb().
1476 ret
= stack_push(notit
->stack
, field
);
1478 PERR("Failed to push string field onto the stack\n");
1479 status
= BT_CTF_BTR_STATUS_ERROR
;
1484 * Initialize string field payload to an empty string since in the
1485 * case of a length 0 string the btr_string_cb won't be called and
1486 * we will end up with an unset string payload.
1488 ret
= bt_ctf_field_string_set_value(field
, "");
1490 PERR("Failed to initialize string field\n");
1491 status
= BT_CTF_BTR_STATUS_ERROR
;
1502 enum bt_ctf_btr_status
btr_string_cb(const char *value
,
1503 size_t len
, struct bt_ctf_field_type
*type
, void *data
)
1505 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1506 struct bt_ctf_field
*field
= NULL
;
1507 struct bt_ctf_notif_iter
*notit
= data
;
1510 /* Get string field */
1511 field
= stack_top(notit
->stack
)->base
;
1514 /* Append current string */
1515 ret
= bt_ctf_field_string_append_len(field
, value
, len
);
1517 PERR("Failed to append a string to a string field\n");
1518 status
= BT_CTF_BTR_STATUS_ERROR
;
1527 enum bt_ctf_btr_status
btr_string_end_cb(
1528 struct bt_ctf_field_type
*type
, void *data
)
1530 struct bt_ctf_notif_iter
*notit
= data
;
1532 /* Pop string field */
1533 stack_pop(notit
->stack
);
1535 /* Go to next field */
1536 stack_top(notit
->stack
)->index
++;
1538 return BT_CTF_BTR_STATUS_OK
;
1541 enum bt_ctf_btr_status
btr_compound_begin_cb(
1542 struct bt_ctf_field_type
*type
, void *data
)
1544 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1545 struct bt_ctf_notif_iter
*notit
= data
;
1546 struct bt_ctf_field
*field
;
1550 if (stack_empty(notit
->stack
)) {
1551 /* Root: create dynamic scope field */
1552 *notit
->cur_dscope_field
= bt_ctf_field_create(type
);
1553 field
= *notit
->cur_dscope_field
;
1556 * Field will be put at the end of this function
1557 * (stack_push() will take one reference, but this
1558 * reference is lost upon the equivalent stack_pop()
1559 * later), so also get it for our context to own it.
1561 bt_get(*notit
->cur_dscope_field
);
1563 field
= get_next_field(notit
);
1567 PERR("Failed to get next field or create dynamic scope field\n");
1568 status
= BT_CTF_BTR_STATUS_ERROR
;
1573 ret
= stack_push(notit
->stack
, field
);
1575 PERR("Failed to push compound field onto the stack\n");
1576 status
= BT_CTF_BTR_STATUS_ERROR
;
1586 enum bt_ctf_btr_status
btr_compound_end_cb(
1587 struct bt_ctf_field_type
*type
, void *data
)
1589 struct bt_ctf_notif_iter
*notit
= data
;
1591 assert(!stack_empty(notit
->stack
));
1594 stack_pop(notit
->stack
);
1596 /* If the stack is not empty, increment the base's index */
1597 if (!stack_empty(notit
->stack
)) {
1598 stack_top(notit
->stack
)->index
++;
1601 return BT_CTF_BTR_STATUS_OK
;
1605 struct bt_ctf_field
*resolve_field(struct bt_ctf_notif_iter
*notit
,
1606 struct bt_ctf_field_path
*path
)
1608 struct bt_ctf_field
*field
= NULL
;
1611 switch (bt_ctf_field_path_get_root_scope(path
)) {
1612 case BT_CTF_SCOPE_TRACE_PACKET_HEADER
:
1613 field
= notit
->dscopes
.trace_packet_header
;
1615 case BT_CTF_SCOPE_STREAM_PACKET_CONTEXT
:
1616 field
= notit
->dscopes
.stream_packet_context
;
1618 case BT_CTF_SCOPE_STREAM_EVENT_HEADER
:
1619 field
= notit
->dscopes
.stream_event_header
;
1621 case BT_CTF_SCOPE_STREAM_EVENT_CONTEXT
:
1622 field
= notit
->dscopes
.stream_event_context
;
1624 case BT_CTF_SCOPE_EVENT_CONTEXT
:
1625 field
= notit
->dscopes
.event_context
;
1627 case BT_CTF_SCOPE_EVENT_FIELDS
:
1628 field
= notit
->dscopes
.event_payload
;
1640 for (i
= 0; i
< bt_ctf_field_path_get_index_count(path
); ++i
) {
1641 struct bt_ctf_field
*next_field
= NULL
;
1642 struct bt_ctf_field_type
*field_type
;
1643 int index
= bt_ctf_field_path_get_index(path
, i
);
1645 field_type
= bt_ctf_field_get_type(field
);
1651 if (is_struct_type(field_type
)) {
1652 next_field
= bt_ctf_field_structure_get_field_by_index(
1654 } else if (is_variant_type(field_type
)) {
1656 bt_ctf_field_variant_get_current_field(field
);
1666 /* Move next field -> field */
1667 BT_MOVE(field
, next_field
);
1675 int64_t btr_get_sequence_length_cb(struct bt_ctf_field_type
*type
, void *data
)
1679 struct bt_ctf_field_path
*field_path
;
1680 struct bt_ctf_notif_iter
*notit
= data
;
1681 struct bt_ctf_field
*length_field
= NULL
;
1684 field_path
= bt_ctf_field_type_sequence_get_length_field_path(type
);
1689 length_field
= resolve_field(notit
, field_path
);
1690 if (!length_field
) {
1694 iret
= bt_ctf_field_unsigned_integer_get_value(length_field
, &length
);
1699 iret
= bt_ctf_field_sequence_set_length(stack_top(notit
->stack
)->base
,
1704 ret
= (int64_t) length
;
1707 BT_PUT(length_field
);
1714 struct bt_ctf_field_type
*btr_get_variant_type_cb(
1715 struct bt_ctf_field_type
*type
, void *data
)
1717 struct bt_ctf_field_path
*path
;
1718 struct bt_ctf_notif_iter
*notit
= data
;
1719 struct bt_ctf_field
*tag_field
= NULL
;
1720 struct bt_ctf_field
*selected_field
= NULL
;
1721 struct bt_ctf_field_type
*selected_field_type
= NULL
;
1723 path
= bt_ctf_field_type_variant_get_tag_field_path(type
);
1728 tag_field
= resolve_field(notit
, path
);
1734 * We found the enumeration tag field instance which should be
1735 * able to select a current field for this variant. This
1736 * callback function we're in is called _after_
1737 * compound_begin(), so the current stack top's base field is
1738 * the variant field in question. We get the selected field here
1739 * thanks to this tag field (thus creating the selected field),
1740 * which will also provide us with its type. Then, this field
1741 * will remain the current selected one until the next callback
1742 * function call which is used to fill the current selected
1745 selected_field
= bt_ctf_field_variant_get_field(
1746 stack_top(notit
->stack
)->base
, tag_field
);
1747 if (!selected_field
) {
1751 selected_field_type
= bt_ctf_field_get_type(selected_field
);
1755 BT_PUT(selected_field
);
1758 return selected_field_type
;
1762 struct bt_ctf_event
*create_event(struct bt_ctf_notif_iter
*notit
)
1765 struct bt_ctf_event
*event
;
1767 /* Create event object. */
1768 event
= bt_ctf_event_create(notit
->meta
.event_class
);
1773 /* Set header, stream event context, context, and payload fields. */
1774 ret
= bt_ctf_event_set_header(event
,
1775 notit
->dscopes
.stream_event_header
);
1780 ret
= bt_ctf_event_set_stream_event_context(event
,
1781 notit
->dscopes
.stream_event_context
);
1786 ret
= bt_ctf_event_set_event_context(event
,
1787 notit
->dscopes
.event_context
);
1792 ret
= bt_ctf_event_set_payload_field(event
,
1793 notit
->dscopes
.event_payload
);
1798 /* Associate with current packet. */
1799 assert(notit
->packet
);
1800 ret
= bt_ctf_event_set_packet(event
, notit
->packet
);
1813 void create_packet(struct bt_ctf_notif_iter
*notit
)
1816 struct bt_ctf_stream
*stream
= NULL
;
1817 struct bt_ctf_packet
*packet
= NULL
;
1819 /* Ask the user for the stream */
1820 stream
= notit
->medium
.medops
.get_stream(notit
->meta
.stream_class
,
1821 notit
->medium
.data
);
1827 packet
= bt_ctf_packet_create(stream
);
1832 /* Set packet's context and header fields */
1833 if (notit
->dscopes
.trace_packet_header
) {
1834 ret
= bt_ctf_packet_set_header(packet
,
1835 notit
->dscopes
.trace_packet_header
);
1841 if (notit
->dscopes
.stream_packet_context
) {
1842 ret
= bt_ctf_packet_set_context(packet
,
1843 notit
->dscopes
.stream_packet_context
);
1853 BT_MOVE(notit
->packet
, packet
);
1857 void notify_new_packet(struct bt_ctf_notif_iter
*notit
,
1858 struct bt_notification
**notification
)
1860 struct bt_notification
*ret
;
1862 /* Initialize the iterator's current packet */
1863 create_packet(notit
);
1864 if (!notit
->packet
) {
1868 ret
= bt_notification_packet_start_create(notit
->packet
);
1872 *notification
= ret
;
1876 void notify_end_of_packet(struct bt_ctf_notif_iter
*notit
,
1877 struct bt_notification
**notification
)
1879 struct bt_notification
*ret
;
1881 if (!notit
->packet
) {
1885 ret
= bt_notification_packet_end_create(notit
->packet
);
1889 BT_PUT(notit
->packet
);
1890 *notification
= ret
;
1894 void notify_event(struct bt_ctf_notif_iter
*notit
,
1895 struct bt_notification
**notification
)
1897 struct bt_ctf_event
*event
;
1898 struct bt_notification
*ret
= NULL
;
1901 event
= create_event(notit
);
1906 ret
= bt_notification_event_create(event
);
1910 *notification
= ret
;
1916 void notify_eos(struct bt_ctf_notif_iter
*notit
,
1917 struct bt_notification
**notification
)
1919 struct bt_ctf_event
*event
;
1920 struct bt_notification
*ret
= NULL
;
1923 event
= create_event(notit
);
1928 ret
= bt_notification_stream_end_create(event
);
1932 *notification
= ret
;
1938 int init_clock_states(GHashTable
*clock_states
, struct bt_ctf_trace
*trace
)
1940 int clock_count
, i
, ret
= 0;
1942 clock_count
= bt_ctf_trace_get_clock_count(trace
);
1943 if (clock_count
<= 0) {
1948 for (i
= 0; i
< clock_count
; i
++) {
1949 struct bt_ctf_clock
*clock
;
1951 clock
= bt_ctf_trace_get_clock(trace
, i
);
1957 g_hash_table_insert(clock_states
, bt_get(clock
), NULL
);
1965 struct bt_ctf_notif_iter
*bt_ctf_notif_iter_create(struct bt_ctf_trace
*trace
,
1966 size_t max_request_sz
,
1967 struct bt_ctf_notif_iter_medium_ops medops
,
1968 void *data
, FILE *err_stream
)
1971 struct bt_ctf_notif_iter
*notit
= NULL
;
1972 struct bt_ctf_btr_cbs cbs
= {
1974 .signed_int
= btr_signed_int_cb
,
1975 .unsigned_int
= btr_unsigned_int_cb
,
1976 .floating_point
= btr_floating_point_cb
,
1977 .string_begin
= btr_string_begin_cb
,
1978 .string
= btr_string_cb
,
1979 .string_end
= btr_string_end_cb
,
1980 .compound_begin
= btr_compound_begin_cb
,
1981 .compound_end
= btr_compound_end_cb
,
1984 .get_sequence_length
= btr_get_sequence_length_cb
,
1985 .get_variant_type
= btr_get_variant_type_cb
,
1990 assert(medops
.request_bytes
);
1991 notit
= g_new0(struct bt_ctf_notif_iter
, 1);
1993 PERR("Failed to allocate memory for CTF notification iterator\n");
1996 notit
->clock_states
= g_hash_table_new_full(g_direct_hash
,
1997 g_direct_equal
, bt_put
, g_free
);
1998 if (!notit
->clock_states
) {
1999 PERR("Failed to create hash table\n");
2002 ret
= init_clock_states(notit
->clock_states
, trace
);
2004 PERR("Failed to initialize stream clock states\n");
2007 notit
->meta
.trace
= bt_get(trace
);
2008 notit
->medium
.medops
= medops
;
2009 notit
->medium
.max_request_sz
= max_request_sz
;
2010 notit
->medium
.data
= data
;
2011 notit
->err_stream
= err_stream
;
2012 notit
->stack
= stack_new(notit
);
2013 if (!notit
->stack
) {
2014 PERR("Failed to create stack\n");
2018 notit
->btr
= bt_ctf_btr_create(cbs
, notit
, err_stream
);
2020 PERR("Failed to create binary type reader\n");
2024 bt_ctf_notif_iter_reset(notit
);
2029 bt_ctf_notif_iter_destroy(notit
);
2034 void bt_ctf_notif_iter_destroy(struct bt_ctf_notif_iter
*notit
)
2036 BT_PUT(notit
->meta
.trace
);
2037 BT_PUT(notit
->meta
.stream_class
);
2038 BT_PUT(notit
->meta
.event_class
);
2039 BT_PUT(notit
->packet
);
2040 put_all_dscopes(notit
);
2043 stack_destroy(notit
->stack
);
2047 bt_ctf_btr_destroy(notit
->btr
);
2050 if (notit
->clock_states
) {
2051 g_hash_table_destroy(notit
->clock_states
);
2056 enum bt_ctf_notif_iter_status
bt_ctf_notif_iter_get_next_notification(
2057 struct bt_ctf_notif_iter
*notit
,
2058 struct bt_notification
**notification
)
2060 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
2063 assert(notification
);
2066 status
= handle_state(notit
);
2067 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
2068 if (status
== BT_CTF_NOTIF_ITER_STATUS_EOF
) {
2069 PDBG("Medium operation reported end of stream\n");
2071 PERR("Failed to handle state:\n");
2072 PERR("\tState: %d\n", notit
->state
);
2077 switch (notit
->state
) {
2078 case STATE_EMIT_NOTIF_NEW_PACKET
:
2079 PDBG("Emitting new packet notification\n");
2080 notify_new_packet(notit
, notification
);
2081 if (!*notification
) {
2082 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
2085 case STATE_EMIT_NOTIF_EVENT
:
2086 PDBG("Emitting event notification\n");
2087 notify_event(notit
, notification
);
2088 if (!*notification
) {
2089 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
2092 case STATE_EMIT_NOTIF_END_OF_PACKET
:
2093 PDBG("Emitting end of packet notification\n");
2094 notify_end_of_packet(notit
, notification
);
2095 if (!*notification
) {
2096 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
2100 /* Non-emitting state: continue */