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 notit
->state
= STATE_AFTER_TRACE_PACKET_HEADER
;
506 ret
= read_dscope_begin_state(notit
, packet_header_type
,
507 STATE_AFTER_TRACE_PACKET_HEADER
,
508 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
509 ¬it
->dscopes
.trace_packet_header
);
511 BT_PUT(packet_header_type
);
516 enum bt_ctf_notif_iter_status
read_packet_header_continue_state(
517 struct bt_ctf_notif_iter
*notit
)
519 return read_dscope_continue_state(notit
,
520 STATE_AFTER_TRACE_PACKET_HEADER
);
524 bool is_struct_type(struct bt_ctf_field_type
*field_type
)
526 return bt_ctf_field_type_get_type_id(field_type
) ==
527 BT_CTF_TYPE_ID_STRUCT
;
531 bool is_variant_type(struct bt_ctf_field_type
*field_type
)
533 return bt_ctf_field_type_get_type_id(field_type
) ==
534 BT_CTF_TYPE_ID_VARIANT
;
538 enum bt_ctf_notif_iter_status
set_current_stream_class(
539 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 notit
->state
= STATE_AFTER_STREAM_PACKET_CONTEXT
;
629 status
= read_dscope_begin_state(notit
, packet_context_type
,
630 STATE_AFTER_STREAM_PACKET_CONTEXT
,
631 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
632 ¬it
->dscopes
.stream_packet_context
);
635 BT_PUT(packet_context_type
);
640 enum bt_ctf_notif_iter_status
read_packet_context_continue_state(
641 struct bt_ctf_notif_iter
*notit
)
643 return read_dscope_continue_state(notit
,
644 STATE_AFTER_STREAM_PACKET_CONTEXT
);
648 enum bt_ctf_notif_iter_status
set_current_packet_content_sizes(
649 struct bt_ctf_notif_iter
*notit
)
651 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
652 struct bt_ctf_field
*packet_size_field
= NULL
;
653 struct bt_ctf_field
*content_size_field
= NULL
;
654 uint64_t content_size
= -1, packet_size
= -1;
656 assert(notit
->dscopes
.stream_packet_context
);
658 packet_size_field
= bt_ctf_field_structure_get_field(
659 notit
->dscopes
.stream_packet_context
, "packet_size");
660 content_size_field
= bt_ctf_field_structure_get_field(
661 notit
->dscopes
.stream_packet_context
, "content_size");
662 if (packet_size_field
) {
663 int ret
= bt_ctf_field_unsigned_integer_get_value(
664 packet_size_field
, &packet_size
);
667 if (packet_size
== 0) {
668 PERR("Decoded packet size is 0\n");
669 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
671 } else if ((packet_size
% 8) != 0) {
672 PERR("Decoded packet size is not a multiple of 8\n");
673 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
678 if (content_size_field
) {
679 int ret
= bt_ctf_field_unsigned_integer_get_value(
680 content_size_field
, &content_size
);
684 content_size
= packet_size
;
687 notit
->cur_packet_size
= packet_size
;
688 notit
->cur_content_size
= content_size
;
690 BT_PUT(packet_size_field
);
691 BT_PUT(content_size_field
);
696 enum bt_ctf_notif_iter_status
after_packet_context_state(
697 struct bt_ctf_notif_iter
*notit
)
699 enum bt_ctf_notif_iter_status status
;
701 status
= set_current_packet_content_sizes(notit
);
702 if (status
== BT_CTF_NOTIF_ITER_STATUS_OK
) {
703 notit
->state
= STATE_EMIT_NOTIF_NEW_PACKET
;
710 enum bt_ctf_notif_iter_status
read_event_header_begin_state(
711 struct bt_ctf_notif_iter
*notit
)
713 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
714 struct bt_ctf_field_type
*event_header_type
= NULL
;
716 /* Check if we have some content left */
717 if (notit
->cur_content_size
>= 0) {
718 if (packet_at(notit
) == notit
->cur_content_size
) {
719 /* No more events! */
720 notit
->state
= STATE_EMIT_NOTIF_END_OF_PACKET
;
722 } else if (packet_at(notit
) > notit
->cur_content_size
) {
723 /* That's not supposed to happen */
724 PERR("Cursor passed packet's content size:\n");
725 PERR("\tDecoded content size: %zu\n",
726 notit
->cur_content_size
);
727 PERR("\tCursor position: %zu\n", packet_at(notit
));
728 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
733 event_header_type
= bt_ctf_stream_class_get_event_header_type(
734 notit
->meta
.stream_class
);
735 if (!event_header_type
) {
736 notit
->state
= STATE_AFTER_STREAM_EVENT_HEADER
;
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 notit
->state
= STATE_DSCOPE_EVENT_CONTEXT_BEGIN
;
918 status
= read_dscope_begin_state(notit
, stream_event_context_type
,
919 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
,
920 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
,
921 ¬it
->dscopes
.stream_event_context
);
924 BT_PUT(stream_event_context_type
);
930 enum bt_ctf_notif_iter_status
read_stream_event_context_continue_state(
931 struct bt_ctf_notif_iter
*notit
)
933 return read_dscope_continue_state(notit
,
934 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
);
938 enum bt_ctf_notif_iter_status
read_event_context_begin_state(
939 struct bt_ctf_notif_iter
*notit
)
941 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
942 struct bt_ctf_field_type
*event_context_type
;
944 event_context_type
= bt_ctf_event_class_get_context_type(
945 notit
->meta
.event_class
);
946 if (!event_context_type
) {
947 notit
->state
= STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
;
950 status
= read_dscope_begin_state(notit
, event_context_type
,
951 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
952 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
,
953 ¬it
->dscopes
.event_context
);
956 BT_PUT(event_context_type
);
962 enum bt_ctf_notif_iter_status
read_event_context_continue_state(
963 struct bt_ctf_notif_iter
*notit
)
965 return read_dscope_continue_state(notit
,
966 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
);
970 enum bt_ctf_notif_iter_status
read_event_payload_begin_state(
971 struct bt_ctf_notif_iter
*notit
)
973 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
974 struct bt_ctf_field_type
*event_payload_type
;
976 event_payload_type
= bt_ctf_event_class_get_payload_type(
977 notit
->meta
.event_class
);
978 if (!event_payload_type
) {
979 notit
->state
= STATE_EMIT_NOTIF_EVENT
;
983 status
= read_dscope_begin_state(notit
, event_payload_type
,
984 STATE_EMIT_NOTIF_EVENT
,
985 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
986 ¬it
->dscopes
.event_payload
);
989 BT_PUT(event_payload_type
);
995 enum bt_ctf_notif_iter_status
read_event_payload_continue_state(
996 struct bt_ctf_notif_iter
*notit
)
998 return read_dscope_continue_state(notit
, STATE_EMIT_NOTIF_EVENT
);
1002 enum bt_ctf_notif_iter_status
skip_packet_padding_state(
1003 struct bt_ctf_notif_iter
*notit
)
1005 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1006 size_t bits_to_skip
;
1008 assert(notit
->cur_packet_size
> 0);
1009 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1010 if (bits_to_skip
== 0) {
1011 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1014 size_t bits_to_consume
;
1015 status
= buf_ensure_available_bits(notit
);
1016 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
1020 bits_to_consume
= MIN(buf_available_bits(notit
), bits_to_skip
);
1021 buf_consume_bits(notit
, bits_to_consume
);
1022 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1023 if (bits_to_skip
== 0) {
1024 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1034 enum bt_ctf_notif_iter_status
handle_state(struct bt_ctf_notif_iter
*notit
)
1036 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1038 PDBG("Handling state %d\n", notit
->state
);
1040 // TODO: optimalize!
1041 switch (notit
->state
) {
1043 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1045 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
1046 status
= read_packet_header_begin_state(notit
);
1048 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
1049 status
= read_packet_header_continue_state(notit
);
1051 case STATE_AFTER_TRACE_PACKET_HEADER
:
1052 status
= after_packet_header_state(notit
);
1054 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
1055 status
= read_packet_context_begin_state(notit
);
1057 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
1058 status
= read_packet_context_continue_state(notit
);
1060 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
1061 status
= after_packet_context_state(notit
);
1063 case STATE_EMIT_NOTIF_NEW_PACKET
:
1064 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1066 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
:
1067 status
= read_event_header_begin_state(notit
);
1069 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
:
1070 status
= read_event_header_continue_state(notit
);
1072 case STATE_AFTER_STREAM_EVENT_HEADER
:
1073 status
= after_event_header_state(notit
);
1075 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
:
1076 status
= read_stream_event_context_begin_state(notit
);
1078 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
:
1079 status
= read_stream_event_context_continue_state(notit
);
1081 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN
:
1082 status
= read_event_context_begin_state(notit
);
1084 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
:
1085 status
= read_event_context_continue_state(notit
);
1087 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
1088 status
= read_event_payload_begin_state(notit
);
1090 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
1091 status
= read_event_payload_continue_state(notit
);
1093 case STATE_EMIT_NOTIF_EVENT
:
1094 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1096 case STATE_SKIP_PACKET_PADDING
:
1097 status
= skip_packet_padding_state(notit
);
1099 case STATE_EMIT_NOTIF_END_OF_PACKET
:
1100 notit
->state
= STATE_SKIP_PACKET_PADDING
;
1109 * Resets the internal state of a CTF notification iterator.
1111 * This function can be used when it is desired to seek to the beginning
1112 * of another packet. It is expected that the next call to
1113 * bt_ctf_notif_iter_medium_ops::request_bytes() made by this
1114 * notification iterator will return the \em first bytes of a \em
1117 * @param notif_iter CTF notification iterator
1120 void bt_ctf_notif_iter_reset(struct bt_ctf_notif_iter
*notit
)
1123 stack_clear(notit
->stack
);
1124 BT_PUT(notit
->meta
.stream_class
);
1125 BT_PUT(notit
->meta
.event_class
);
1126 BT_PUT(notit
->packet
);
1127 put_all_dscopes(notit
);
1128 notit
->buf
.addr
= NULL
;
1131 notit
->buf
.packet_offset
= 0;
1132 notit
->state
= STATE_INIT
;
1133 notit
->cur_content_size
= -1;
1134 notit
->cur_packet_size
= -1;
1138 int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter
*notit
)
1143 stack_clear(notit
->stack
);
1144 BT_PUT(notit
->meta
.stream_class
);
1145 BT_PUT(notit
->meta
.event_class
);
1146 BT_PUT(notit
->packet
);
1147 put_all_dscopes(notit
);
1150 * Adjust current buffer so that addr points to the beginning of the new
1153 if (notit
->buf
.addr
) {
1154 size_t consumed_bytes
= (size_t) (notit
->buf
.at
/ CHAR_BIT
);
1156 /* Packets are assumed to start on a byte frontier. */
1157 if (notit
->buf
.at
% CHAR_BIT
) {
1162 notit
->buf
.addr
+= consumed_bytes
;
1163 notit
->buf
.sz
-= consumed_bytes
;
1165 notit
->buf
.packet_offset
= 0;
1168 notit
->cur_content_size
= -1;
1169 notit
->cur_packet_size
= -1;
1175 struct bt_ctf_field
*get_next_field(struct bt_ctf_notif_iter
*notit
)
1177 struct bt_ctf_field
*next_field
= NULL
;
1178 struct bt_ctf_field
*base_field
;
1179 struct bt_ctf_field_type
*base_type
;
1182 assert(!stack_empty(notit
->stack
));
1183 index
= stack_top(notit
->stack
)->index
;
1184 base_field
= stack_top(notit
->stack
)->base
;
1185 base_type
= bt_ctf_field_get_type(base_field
);
1187 PERR("Failed to get base field's type\n");
1191 switch (bt_ctf_field_type_get_type_id(base_type
)) {
1192 case BT_CTF_TYPE_ID_STRUCT
:
1193 next_field
= bt_ctf_field_structure_get_field_by_index(
1196 case BT_CTF_TYPE_ID_ARRAY
:
1197 next_field
= bt_ctf_field_array_get_field(base_field
, index
);
1199 case BT_CTF_TYPE_ID_SEQUENCE
:
1200 next_field
= bt_ctf_field_sequence_get_field(base_field
, index
);
1202 case BT_CTF_TYPE_ID_VARIANT
:
1203 next_field
= bt_ctf_field_variant_get_current_field(base_field
);
1221 void update_clock_state(uint64_t *state
,
1222 struct bt_ctf_field
*value_field
)
1224 struct bt_ctf_field_type
*value_type
= NULL
;
1225 uint64_t requested_new_value
;
1226 uint64_t requested_new_value_mask
;
1227 uint64_t cur_value_masked
;
1228 int requested_new_value_size
;
1231 value_type
= bt_ctf_field_get_type(value_field
);
1234 requested_new_value_size
=
1235 bt_ctf_field_type_integer_get_size(value_type
);
1236 assert(requested_new_value_size
> 0);
1238 ret
= bt_ctf_field_unsigned_integer_get_value(value_field
,
1239 &requested_new_value
);
1243 * Special case for a 64-bit new value, which is the limit
1244 * of a clock value as of this version: overwrite the
1245 * current value directly.
1247 if (requested_new_value_size
== 64) {
1248 *state
= requested_new_value
;
1252 requested_new_value_mask
= (1ULL << requested_new_value_size
) - 1;
1253 cur_value_masked
= *state
& requested_new_value_mask
;
1255 if (requested_new_value
< cur_value_masked
) {
1257 * It looks like a wrap happened on the number of bits
1258 * of the requested new value. Assume that the clock
1259 * value wrapped only one time.
1261 *state
+= requested_new_value_mask
+ 1;
1264 /* Clear the low bits of the current clock value. */
1265 *state
&= ~requested_new_value_mask
;
1267 /* Set the low bits of the current clock value. */
1268 *state
|= requested_new_value
;
1274 enum bt_ctf_btr_status
update_clock(struct bt_ctf_notif_iter
*notit
,
1275 struct bt_ctf_field_type
*int_field_type
,
1276 struct bt_ctf_field
*int_field
)
1278 gboolean clock_found
;
1279 uint64_t *clock_state
;
1280 enum bt_ctf_btr_status ret
= BT_CTF_BTR_STATUS_OK
;
1281 struct bt_ctf_clock
*clock
= bt_ctf_field_type_integer_get_mapped_clock(
1284 if (likely(!clock
)) {
1288 clock_found
= g_hash_table_lookup_extended(notit
->clock_states
,
1289 clock
, NULL
, (gpointer
) &clock_state
);
1290 if (unlikely(!clock_found
)) {
1291 const char *clock_name
= bt_ctf_clock_get_name(clock
);
1293 PERR("Unknown clock %s mapped to integer encountered in stream\n",
1294 clock_name
? : "NULL");
1295 ret
= BT_CTF_BTR_STATUS_ERROR
;
1299 if (unlikely(!clock_state
)) {
1300 clock_state
= g_new0(uint64_t, 1);
1302 ret
= BT_CTF_BTR_STATUS_ENOMEM
;
1305 g_hash_table_insert(notit
->clock_states
, bt_get(clock
),
1309 /* Update the clock's state. */
1310 update_clock_state(clock_state
, int_field
);
1318 enum bt_ctf_btr_status
btr_signed_int_cb(int64_t value
,
1319 struct bt_ctf_field_type
*type
, void *data
)
1321 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1322 struct bt_ctf_field
*field
= NULL
;
1323 struct bt_ctf_field
*int_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 (signed int)\n");
1331 status
= BT_CTF_BTR_STATUS_ERROR
;
1335 switch(bt_ctf_field_type_get_type_id(type
)) {
1336 case BT_CTF_TYPE_ID_INTEGER
:
1337 /* Integer field is created field */
1338 BT_MOVE(int_field
, field
);
1341 case BT_CTF_TYPE_ID_ENUM
:
1342 int_field
= bt_ctf_field_enumeration_get_container(field
);
1343 type
= bt_ctf_field_get_type(int_field
);
1352 PERR("Failed to get integer field\n");
1353 status
= BT_CTF_BTR_STATUS_ERROR
;
1357 ret
= bt_ctf_field_signed_integer_set_value(int_field
, value
);
1359 stack_top(notit
->stack
)->index
++;
1360 status
= update_clock(notit
, type
, int_field
);
1370 enum bt_ctf_btr_status
btr_unsigned_int_cb(uint64_t value
,
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_field
*field
= NULL
;
1375 struct bt_ctf_field
*int_field
= NULL
;
1376 struct bt_ctf_notif_iter
*notit
= data
;
1379 /* Create next field */
1380 field
= get_next_field(notit
);
1382 PERR("Failed to get next field (unsigned int)\n");
1383 status
= BT_CTF_BTR_STATUS_ERROR
;
1387 switch(bt_ctf_field_type_get_type_id(type
)) {
1388 case BT_CTF_TYPE_ID_INTEGER
:
1389 /* Integer field is created field */
1390 BT_MOVE(int_field
, field
);
1393 case BT_CTF_TYPE_ID_ENUM
:
1394 int_field
= bt_ctf_field_enumeration_get_container(field
);
1395 type
= bt_ctf_field_get_type(int_field
);
1404 PERR("Failed to get integer field\n");
1405 status
= BT_CTF_BTR_STATUS_ERROR
;
1409 ret
= bt_ctf_field_unsigned_integer_set_value(int_field
, value
);
1411 stack_top(notit
->stack
)->index
++;
1412 status
= update_clock(notit
, type
, int_field
);
1422 enum bt_ctf_btr_status
btr_floating_point_cb(double value
,
1423 struct bt_ctf_field_type
*type
, void *data
)
1425 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1426 struct bt_ctf_field
*field
= NULL
;
1427 struct bt_ctf_notif_iter
*notit
= data
;
1430 /* Create next field */
1431 field
= get_next_field(notit
);
1433 PERR("Failed to get next field (floating point number)\n");
1434 status
= BT_CTF_BTR_STATUS_ERROR
;
1438 ret
= bt_ctf_field_floating_point_set_value(field
, value
);
1440 stack_top(notit
->stack
)->index
++;
1449 enum bt_ctf_btr_status
btr_string_begin_cb(
1450 struct bt_ctf_field_type
*type
, void *data
)
1452 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1453 struct bt_ctf_field
*field
= NULL
;
1454 struct bt_ctf_notif_iter
*notit
= data
;
1457 /* Create next field */
1458 field
= get_next_field(notit
);
1460 PERR("Failed to get next field (string)\n");
1461 status
= BT_CTF_BTR_STATUS_ERROR
;
1466 * Push on stack. Not a compound type per se, but we know that only
1467 * btr_string_cb() may be called between this call and a subsequent
1468 * call to btr_string_end_cb().
1470 ret
= stack_push(notit
->stack
, field
);
1472 PERR("Failed to push string field onto the stack\n");
1473 status
= BT_CTF_BTR_STATUS_ERROR
;
1478 * Initialize string field payload to an empty string since in the
1479 * case of a length 0 string the btr_string_cb won't be called and
1480 * we will end up with an unset string payload.
1482 ret
= bt_ctf_field_string_set_value(field
, "");
1484 PERR("Failed to initialize string field\n");
1485 status
= BT_CTF_BTR_STATUS_ERROR
;
1496 enum bt_ctf_btr_status
btr_string_cb(const char *value
,
1497 size_t len
, struct bt_ctf_field_type
*type
, void *data
)
1499 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1500 struct bt_ctf_field
*field
= NULL
;
1501 struct bt_ctf_notif_iter
*notit
= data
;
1504 /* Get string field */
1505 field
= stack_top(notit
->stack
)->base
;
1508 /* Append current string */
1509 ret
= bt_ctf_field_string_append_len(field
, value
, len
);
1511 PERR("Failed to append a string to a string field\n");
1512 status
= BT_CTF_BTR_STATUS_ERROR
;
1521 enum bt_ctf_btr_status
btr_string_end_cb(
1522 struct bt_ctf_field_type
*type
, void *data
)
1524 struct bt_ctf_notif_iter
*notit
= data
;
1526 /* Pop string field */
1527 stack_pop(notit
->stack
);
1529 /* Go to next field */
1530 stack_top(notit
->stack
)->index
++;
1532 return BT_CTF_BTR_STATUS_OK
;
1535 enum bt_ctf_btr_status
btr_compound_begin_cb(
1536 struct bt_ctf_field_type
*type
, void *data
)
1538 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1539 struct bt_ctf_notif_iter
*notit
= data
;
1540 struct bt_ctf_field
*field
;
1544 if (stack_empty(notit
->stack
)) {
1545 /* Root: create dynamic scope field */
1546 *notit
->cur_dscope_field
= bt_ctf_field_create(type
);
1547 field
= *notit
->cur_dscope_field
;
1550 * Field will be put at the end of this function
1551 * (stack_push() will take one reference, but this
1552 * reference is lost upon the equivalent stack_pop()
1553 * later), so also get it for our context to own it.
1555 bt_get(*notit
->cur_dscope_field
);
1557 field
= get_next_field(notit
);
1561 PERR("Failed to get next field or create dynamic scope field\n");
1562 status
= BT_CTF_BTR_STATUS_ERROR
;
1567 ret
= stack_push(notit
->stack
, field
);
1569 PERR("Failed to push compound field onto the stack\n");
1570 status
= BT_CTF_BTR_STATUS_ERROR
;
1580 enum bt_ctf_btr_status
btr_compound_end_cb(
1581 struct bt_ctf_field_type
*type
, void *data
)
1583 struct bt_ctf_notif_iter
*notit
= data
;
1585 assert(!stack_empty(notit
->stack
));
1588 stack_pop(notit
->stack
);
1590 /* If the stack is not empty, increment the base's index */
1591 if (!stack_empty(notit
->stack
)) {
1592 stack_top(notit
->stack
)->index
++;
1595 return BT_CTF_BTR_STATUS_OK
;
1599 struct bt_ctf_field
*resolve_field(struct bt_ctf_notif_iter
*notit
,
1600 struct bt_ctf_field_path
*path
)
1602 struct bt_ctf_field
*field
= NULL
;
1605 switch (bt_ctf_field_path_get_root_scope(path
)) {
1606 case BT_CTF_SCOPE_TRACE_PACKET_HEADER
:
1607 field
= notit
->dscopes
.trace_packet_header
;
1609 case BT_CTF_SCOPE_STREAM_PACKET_CONTEXT
:
1610 field
= notit
->dscopes
.stream_packet_context
;
1612 case BT_CTF_SCOPE_STREAM_EVENT_HEADER
:
1613 field
= notit
->dscopes
.stream_event_header
;
1615 case BT_CTF_SCOPE_STREAM_EVENT_CONTEXT
:
1616 field
= notit
->dscopes
.stream_event_context
;
1618 case BT_CTF_SCOPE_EVENT_CONTEXT
:
1619 field
= notit
->dscopes
.event_context
;
1621 case BT_CTF_SCOPE_EVENT_FIELDS
:
1622 field
= notit
->dscopes
.event_payload
;
1634 for (i
= 0; i
< bt_ctf_field_path_get_index_count(path
); ++i
) {
1635 struct bt_ctf_field
*next_field
= NULL
;
1636 struct bt_ctf_field_type
*field_type
;
1637 int index
= bt_ctf_field_path_get_index(path
, i
);
1639 field_type
= bt_ctf_field_get_type(field
);
1645 if (is_struct_type(field_type
)) {
1646 next_field
= bt_ctf_field_structure_get_field_by_index(
1648 } else if (is_variant_type(field_type
)) {
1650 bt_ctf_field_variant_get_current_field(field
);
1660 /* Move next field -> field */
1661 BT_MOVE(field
, next_field
);
1669 int64_t btr_get_sequence_length_cb(struct bt_ctf_field_type
*type
, void *data
)
1673 struct bt_ctf_field_path
*field_path
;
1674 struct bt_ctf_notif_iter
*notit
= data
;
1675 struct bt_ctf_field
*length_field
= NULL
;
1678 field_path
= bt_ctf_field_type_sequence_get_length_field_path(type
);
1683 length_field
= resolve_field(notit
, field_path
);
1684 if (!length_field
) {
1688 iret
= bt_ctf_field_unsigned_integer_get_value(length_field
, &length
);
1693 iret
= bt_ctf_field_sequence_set_length(stack_top(notit
->stack
)->base
,
1698 ret
= (int64_t) length
;
1701 BT_PUT(length_field
);
1708 struct bt_ctf_field_type
*btr_get_variant_type_cb(
1709 struct bt_ctf_field_type
*type
, void *data
)
1711 struct bt_ctf_field_path
*path
;
1712 struct bt_ctf_notif_iter
*notit
= data
;
1713 struct bt_ctf_field
*tag_field
= NULL
;
1714 struct bt_ctf_field
*selected_field
= NULL
;
1715 struct bt_ctf_field_type
*selected_field_type
= NULL
;
1717 path
= bt_ctf_field_type_variant_get_tag_field_path(type
);
1722 tag_field
= resolve_field(notit
, path
);
1728 * We found the enumeration tag field instance which should be
1729 * able to select a current field for this variant. This
1730 * callback function we're in is called _after_
1731 * compound_begin(), so the current stack top's base field is
1732 * the variant field in question. We get the selected field here
1733 * thanks to this tag field (thus creating the selected field),
1734 * which will also provide us with its type. Then, this field
1735 * will remain the current selected one until the next callback
1736 * function call which is used to fill the current selected
1739 selected_field
= bt_ctf_field_variant_get_field(
1740 stack_top(notit
->stack
)->base
, tag_field
);
1741 if (!selected_field
) {
1745 selected_field_type
= bt_ctf_field_get_type(selected_field
);
1749 BT_PUT(selected_field
);
1752 return selected_field_type
;
1756 int set_event_clocks(struct bt_ctf_event
*event
,
1757 struct bt_ctf_notif_iter
*notit
)
1760 GHashTableIter iter
;
1761 struct bt_ctf_clock
*clock
;
1762 uint64_t *clock_state
;
1764 g_hash_table_iter_init(&iter
, notit
->clock_states
);
1766 while (g_hash_table_iter_next(&iter
, (gpointer
) &clock
,
1767 (gpointer
) &clock_state
)) {
1768 struct bt_ctf_clock_value
*clock_value
;
1770 clock_value
= bt_ctf_clock_value_create(clock
, *clock_state
);
1775 ret
= bt_ctf_event_set_clock_value(event
, clock
, clock_value
);
1776 bt_put(clock_value
);
1787 struct bt_ctf_event
*create_event(struct bt_ctf_notif_iter
*notit
)
1790 struct bt_ctf_event
*event
;
1792 /* Create event object. */
1793 event
= bt_ctf_event_create(notit
->meta
.event_class
);
1798 /* Set header, stream event context, context, and payload fields. */
1799 ret
= bt_ctf_event_set_header(event
,
1800 notit
->dscopes
.stream_event_header
);
1805 ret
= bt_ctf_event_set_stream_event_context(event
,
1806 notit
->dscopes
.stream_event_context
);
1811 ret
= bt_ctf_event_set_event_context(event
,
1812 notit
->dscopes
.event_context
);
1817 ret
= bt_ctf_event_set_payload_field(event
,
1818 notit
->dscopes
.event_payload
);
1823 ret
= set_event_clocks(event
, notit
);
1828 /* Associate with current packet. */
1829 assert(notit
->packet
);
1830 ret
= bt_ctf_event_set_packet(event
, notit
->packet
);
1843 void create_packet(struct bt_ctf_notif_iter
*notit
)
1846 struct bt_ctf_stream
*stream
= NULL
;
1847 struct bt_ctf_packet
*packet
= NULL
;
1849 /* Ask the user for the stream */
1850 stream
= notit
->medium
.medops
.get_stream(notit
->meta
.stream_class
,
1851 notit
->medium
.data
);
1857 packet
= bt_ctf_packet_create(stream
);
1862 /* Set packet's context and header fields */
1863 if (notit
->dscopes
.trace_packet_header
) {
1864 ret
= bt_ctf_packet_set_header(packet
,
1865 notit
->dscopes
.trace_packet_header
);
1871 if (notit
->dscopes
.stream_packet_context
) {
1872 ret
= bt_ctf_packet_set_context(packet
,
1873 notit
->dscopes
.stream_packet_context
);
1883 BT_MOVE(notit
->packet
, packet
);
1887 void notify_new_packet(struct bt_ctf_notif_iter
*notit
,
1888 struct bt_notification
**notification
)
1890 struct bt_notification
*ret
;
1892 /* Initialize the iterator's current packet */
1893 create_packet(notit
);
1894 if (!notit
->packet
) {
1898 ret
= bt_notification_packet_start_create(notit
->packet
);
1902 *notification
= ret
;
1906 void notify_end_of_packet(struct bt_ctf_notif_iter
*notit
,
1907 struct bt_notification
**notification
)
1909 struct bt_notification
*ret
;
1911 if (!notit
->packet
) {
1915 ret
= bt_notification_packet_end_create(notit
->packet
);
1919 BT_PUT(notit
->packet
);
1920 *notification
= ret
;
1924 void notify_event(struct bt_ctf_notif_iter
*notit
,
1925 struct bt_notification
**notification
)
1927 struct bt_ctf_event
*event
;
1928 struct bt_notification
*ret
= NULL
;
1931 event
= create_event(notit
);
1936 ret
= bt_notification_event_create(event
);
1940 *notification
= ret
;
1946 void notify_eos(struct bt_ctf_notif_iter
*notit
,
1947 struct bt_notification
**notification
)
1949 struct bt_ctf_event
*event
;
1950 struct bt_notification
*ret
= NULL
;
1953 event
= create_event(notit
);
1958 ret
= bt_notification_stream_end_create(event
);
1962 *notification
= ret
;
1968 int init_clock_states(GHashTable
*clock_states
, struct bt_ctf_trace
*trace
)
1970 int clock_count
, i
, ret
= 0;
1972 clock_count
= bt_ctf_trace_get_clock_count(trace
);
1973 if (clock_count
<= 0) {
1978 for (i
= 0; i
< clock_count
; i
++) {
1979 struct bt_ctf_clock
*clock
;
1981 clock
= bt_ctf_trace_get_clock(trace
, i
);
1987 g_hash_table_insert(clock_states
, bt_get(clock
), NULL
);
1995 struct bt_ctf_notif_iter
*bt_ctf_notif_iter_create(struct bt_ctf_trace
*trace
,
1996 size_t max_request_sz
,
1997 struct bt_ctf_notif_iter_medium_ops medops
,
1998 void *data
, FILE *err_stream
)
2001 struct bt_ctf_notif_iter
*notit
= NULL
;
2002 struct bt_ctf_btr_cbs cbs
= {
2004 .signed_int
= btr_signed_int_cb
,
2005 .unsigned_int
= btr_unsigned_int_cb
,
2006 .floating_point
= btr_floating_point_cb
,
2007 .string_begin
= btr_string_begin_cb
,
2008 .string
= btr_string_cb
,
2009 .string_end
= btr_string_end_cb
,
2010 .compound_begin
= btr_compound_begin_cb
,
2011 .compound_end
= btr_compound_end_cb
,
2014 .get_sequence_length
= btr_get_sequence_length_cb
,
2015 .get_variant_type
= btr_get_variant_type_cb
,
2020 assert(medops
.request_bytes
);
2021 notit
= g_new0(struct bt_ctf_notif_iter
, 1);
2023 PERR("Failed to allocate memory for CTF notification iterator\n");
2026 notit
->clock_states
= g_hash_table_new_full(g_direct_hash
,
2027 g_direct_equal
, bt_put
, g_free
);
2028 if (!notit
->clock_states
) {
2029 PERR("Failed to create hash table\n");
2032 ret
= init_clock_states(notit
->clock_states
, trace
);
2034 PERR("Failed to initialize stream clock states\n");
2037 notit
->meta
.trace
= bt_get(trace
);
2038 notit
->medium
.medops
= medops
;
2039 notit
->medium
.max_request_sz
= max_request_sz
;
2040 notit
->medium
.data
= data
;
2041 notit
->err_stream
= err_stream
;
2042 notit
->stack
= stack_new(notit
);
2043 if (!notit
->stack
) {
2044 PERR("Failed to create stack\n");
2048 notit
->btr
= bt_ctf_btr_create(cbs
, notit
, err_stream
);
2050 PERR("Failed to create binary type reader\n");
2054 bt_ctf_notif_iter_reset(notit
);
2059 bt_ctf_notif_iter_destroy(notit
);
2064 void bt_ctf_notif_iter_destroy(struct bt_ctf_notif_iter
*notit
)
2066 BT_PUT(notit
->meta
.trace
);
2067 BT_PUT(notit
->meta
.stream_class
);
2068 BT_PUT(notit
->meta
.event_class
);
2069 BT_PUT(notit
->packet
);
2070 put_all_dscopes(notit
);
2073 stack_destroy(notit
->stack
);
2077 bt_ctf_btr_destroy(notit
->btr
);
2080 if (notit
->clock_states
) {
2081 g_hash_table_destroy(notit
->clock_states
);
2086 enum bt_ctf_notif_iter_status
bt_ctf_notif_iter_get_next_notification(
2087 struct bt_ctf_notif_iter
*notit
,
2088 struct bt_notification
**notification
)
2090 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
2093 assert(notification
);
2096 status
= handle_state(notit
);
2097 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
2098 if (status
== BT_CTF_NOTIF_ITER_STATUS_EOF
) {
2099 PDBG("Medium operation reported end of stream\n");
2101 PERR("Failed to handle state:\n");
2102 PERR("\tState: %d\n", notit
->state
);
2107 switch (notit
->state
) {
2108 case STATE_EMIT_NOTIF_NEW_PACKET
:
2109 PDBG("Emitting new packet notification\n");
2110 notify_new_packet(notit
, notification
);
2111 if (!*notification
) {
2112 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
2115 case STATE_EMIT_NOTIF_EVENT
:
2116 PDBG("Emitting event notification\n");
2117 notify_event(notit
, notification
);
2118 if (!*notification
) {
2119 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
2122 case STATE_EMIT_NOTIF_END_OF_PACKET
:
2123 PDBG("Emitting end of packet notification\n");
2124 notify_end_of_packet(notit
, notification
);
2125 if (!*notification
) {
2126 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
2130 /* Non-emitting state: continue */