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-class.h>
40 #include <babeltrace/ctf-ir/event-class.h>
41 #include <babeltrace/graph/notification-packet.h>
42 #include <babeltrace/graph/notification-event.h>
43 #include <babeltrace/graph/notification-stream.h>
44 #include <babeltrace/graph/clock-class-priority-map.h>
45 #include <babeltrace/ref.h>
48 #define PRINT_ERR_STREAM notit->err_stream
49 #define PRINT_PREFIX "ctf-notif-iter"
52 #include "notif-iter.h"
53 #include "../btr/btr.h"
55 #define BYTES_TO_BITS(x) ((x) * 8)
57 struct bt_ctf_notif_iter
;
59 /* A visit stack entry */
62 * Current base field, one of:
70 * Field is owned by this.
72 struct bt_ctf_field
*base
;
74 /* index of next field to set */
80 /* Entries (struct stack_entry *) (top is last element) */
87 STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
,
88 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
89 STATE_AFTER_TRACE_PACKET_HEADER
,
90 STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
,
91 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
92 STATE_AFTER_STREAM_PACKET_CONTEXT
,
93 STATE_EMIT_NOTIF_NEW_PACKET
,
94 STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
,
95 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
,
96 STATE_AFTER_STREAM_EVENT_HEADER
,
97 STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
,
98 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
,
99 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
,
100 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
,
101 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
102 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
103 STATE_EMIT_NOTIF_EVENT
,
104 STATE_EMIT_NOTIF_END_OF_PACKET
,
105 STATE_SKIP_PACKET_PADDING
,
108 struct trace_field_path_cache
{
110 * Indexes of the stream_id and stream_instance_id field in the packet
111 * header structure, -1 if unset.
114 int stream_instance_id
;
117 struct stream_class_field_path_cache
{
119 * Indexes of the v and id fields in the stream event header structure,
126 * index of the timestamp_end, packet_size and content_size fields in
127 * the stream packet context structure. Set to -1 if the fields were
135 struct field_cb_override
{
136 enum bt_ctf_btr_status (* func
)(void *value
,
137 struct bt_ctf_field_type
*type
, void *data
);
141 /* CTF notification iterator */
142 struct bt_ctf_notif_iter
{
146 /* Error stream (may be NULL) */
150 * Current dynamic scope field pointer.
152 * This is set when a dynamic scope field is first created by
153 * btr_compound_begin_cb(). It points to one of the fields in
156 struct bt_ctf_field
**cur_dscope_field
;
158 /* Trace and classes (owned by this) */
160 struct bt_ctf_trace
*trace
;
161 struct bt_ctf_stream_class
*stream_class
;
162 struct bt_ctf_event_class
*event_class
;
165 /* Current packet (NULL if not created yet) */
166 struct bt_ctf_packet
*packet
;
169 * Current timestamp_end field (to consider before switching packets).
171 struct bt_ctf_field
*cur_timestamp_end
;
173 /* Database of current dynamic scopes (owned by this) */
175 struct bt_ctf_field
*trace_packet_header
;
176 struct bt_ctf_field
*stream_packet_context
;
177 struct bt_ctf_field
*stream_event_header
;
178 struct bt_ctf_field
*stream_event_context
;
179 struct bt_ctf_field
*event_context
;
180 struct bt_ctf_field
*event_payload
;
184 * Special field overrides.
186 * Overrides are used to implement the behaviours of special fields such
187 * as "timestamp_end" (which must be ignored until the end of the
188 * packet), "id" (event id) which can be present multiple times and must
189 * be updated multiple time.
191 * This should be used to implement the behaviour of integer fields
192 * mapped to clocks and other "tagged" fields (in CTF 2).
194 * bt_ctf_field_type to struct field_cb_override
196 GHashTable
*field_overrides
;
201 /* Current medium buffer data */
203 /* Last address provided by medium */
206 /* Buffer size provided by medium (bytes) */
209 /* Offset within whole packet of addr (bits) */
210 size_t packet_offset
;
212 /* Current position from addr (bits) */
216 /* Binary type reader */
217 struct bt_ctf_btr
*btr
;
219 /* Current medium data */
221 struct bt_ctf_notif_iter_medium_ops medops
;
222 size_t max_request_sz
;
226 /* Current packet size (bits) (-1 if unknown) */
227 int64_t cur_packet_size
;
229 /* Current content size (bits) (-1 if unknown) */
230 int64_t cur_content_size
;
232 /* bt_ctf_clock_class to uint64_t. */
233 GHashTable
*clock_states
;
236 * Cache of the trace-constant field paths (event header type)
237 * associated to the current trace.
239 struct trace_field_path_cache trace_field_path_cache
;
242 * Field path cache associated with the current stream class.
243 * Ownership of this structure belongs to the field_path_caches HT.
245 struct stream_class_field_path_cache
*cur_sc_field_path_cache
;
247 /* bt_ctf_stream_class to struct stream_class_field_path_cache. */
248 GHashTable
*sc_field_path_caches
;
252 int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter
*notit
);
255 enum bt_ctf_btr_status
btr_timestamp_end_cb(void *value
,
256 struct bt_ctf_field_type
*type
, void *data
);
259 void stack_entry_free_func(gpointer data
)
261 struct stack_entry
*entry
= data
;
268 struct stack
*stack_new(struct bt_ctf_notif_iter
*notit
)
270 struct stack
*stack
= NULL
;
272 stack
= g_new0(struct stack
, 1);
277 stack
->entries
= g_ptr_array_new_with_free_func(stack_entry_free_func
);
278 if (!stack
->entries
) {
289 void stack_destroy(struct stack
*stack
)
292 g_ptr_array_free(stack
->entries
, TRUE
);
297 int stack_push(struct stack
*stack
, struct bt_ctf_field
*base
)
300 struct stack_entry
*entry
;
304 entry
= g_new0(struct stack_entry
, 1);
310 entry
->base
= bt_get(base
);
311 g_ptr_array_add(stack
->entries
, entry
);
318 unsigned int stack_size(struct stack
*stack
)
322 return stack
->entries
->len
;
326 void stack_pop(struct stack
*stack
)
329 assert(stack_size(stack
));
330 g_ptr_array_remove_index(stack
->entries
, stack
->entries
->len
- 1);
334 struct stack_entry
*stack_top(struct stack
*stack
)
337 assert(stack_size(stack
));
339 return g_ptr_array_index(stack
->entries
, stack
->entries
->len
- 1);
343 bool stack_empty(struct stack
*stack
)
345 return stack_size(stack
) == 0;
349 void stack_clear(struct stack
*stack
)
353 if (!stack_empty(stack
)) {
354 g_ptr_array_remove_range(stack
->entries
, 0, stack_size(stack
));
357 assert(stack_empty(stack
));
361 enum bt_ctf_notif_iter_status
notif_iter_status_from_m_status(
362 enum bt_ctf_notif_iter_medium_status m_status
)
368 size_t buf_size_bits(struct bt_ctf_notif_iter
*notit
)
370 return BYTES_TO_BITS(notit
->buf
.sz
);
374 size_t buf_available_bits(struct bt_ctf_notif_iter
*notit
)
376 return buf_size_bits(notit
) - notit
->buf
.at
;
380 size_t packet_at(struct bt_ctf_notif_iter
*notit
)
382 return notit
->buf
.packet_offset
+ notit
->buf
.at
;
386 size_t remaining_content_bits(struct bt_ctf_notif_iter
*notit
)
388 if (notit
->cur_content_size
== -1) {
392 return notit
->cur_content_size
- packet_at(notit
);
396 size_t remaining_packet_bits(struct bt_ctf_notif_iter
*notit
)
398 if (notit
->cur_packet_size
== -1) {
402 return notit
->cur_packet_size
- packet_at(notit
);
406 void buf_consume_bits(struct bt_ctf_notif_iter
*notit
, size_t incr
)
408 notit
->buf
.at
+= incr
;
412 bool buf_has_enough_bits(struct bt_ctf_notif_iter
*notit
, size_t sz
)
414 return buf_available_bits(notit
) >= sz
;
418 enum bt_ctf_notif_iter_status
request_medium_bytes(
419 struct bt_ctf_notif_iter
*notit
)
421 uint8_t *buffer_addr
;
423 enum bt_ctf_notif_iter_medium_status m_status
;
425 m_status
= notit
->medium
.medops
.request_bytes(
426 notit
->medium
.max_request_sz
, &buffer_addr
,
427 &buffer_sz
, notit
->medium
.data
);
428 if (m_status
== BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK
) {
429 assert(buffer_sz
!= 0);
431 /* New packet offset is old one + old size (in bits) */
432 notit
->buf
.packet_offset
+= buf_size_bits(notit
);
434 /* Restart at the beginning of the new medium buffer */
437 /* New medium buffer size */
438 notit
->buf
.sz
= buffer_sz
;
440 /* New medium buffer address */
441 notit
->buf
.addr
= buffer_addr
;
444 return notif_iter_status_from_m_status(m_status
);
448 enum bt_ctf_notif_iter_status
buf_ensure_available_bits(
449 struct bt_ctf_notif_iter
*notit
)
451 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
453 if (buf_available_bits(notit
) == 0) {
455 * This _cannot_ return BT_CTF_NOTIF_ITER_STATUS_OK
458 status
= request_medium_bytes(notit
);
465 enum bt_ctf_notif_iter_status
read_dscope_begin_state(
466 struct bt_ctf_notif_iter
*notit
,
467 struct bt_ctf_field_type
*dscope_field_type
,
468 enum state done_state
, enum state continue_state
,
469 struct bt_ctf_field
**dscope_field
)
471 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
472 enum bt_ctf_btr_status btr_status
;
473 size_t consumed_bits
;
475 status
= buf_ensure_available_bits(notit
);
476 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
480 bt_put(*dscope_field
);
481 notit
->cur_dscope_field
= dscope_field
;
482 consumed_bits
= bt_ctf_btr_start(notit
->btr
, dscope_field_type
,
483 notit
->buf
.addr
, notit
->buf
.at
, packet_at(notit
),
484 notit
->buf
.sz
, &btr_status
);
486 switch (btr_status
) {
487 case BT_CTF_BTR_STATUS_OK
:
488 /* type was read completely */
489 notit
->state
= done_state
;
491 case BT_CTF_BTR_STATUS_EOF
:
492 notit
->state
= continue_state
;
495 PERR("Binary type reader failed to start\n");
496 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
500 /* Consume bits now since we know we're not in an error state */
501 buf_consume_bits(notit
, consumed_bits
);
508 enum bt_ctf_notif_iter_status
read_dscope_continue_state(
509 struct bt_ctf_notif_iter
*notit
, enum state done_state
)
511 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
512 enum bt_ctf_btr_status btr_status
;
513 size_t consumed_bits
;
515 status
= buf_ensure_available_bits(notit
);
516 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
520 consumed_bits
= bt_ctf_btr_continue(notit
->btr
, notit
->buf
.addr
,
521 notit
->buf
.sz
, &btr_status
);
523 switch (btr_status
) {
524 case BT_CTF_BTR_STATUS_OK
:
525 /* Type was read completely. */
526 notit
->state
= done_state
;
528 case BT_CTF_BTR_STATUS_EOF
:
529 /* Stay in this continue state. */
532 PERR("Binary type reader failed to continue\n");
533 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
537 /* Consume bits now since we know we're not in an error state. */
538 buf_consume_bits(notit
, consumed_bits
);
544 void put_event_dscopes(struct bt_ctf_notif_iter
*notit
)
546 BT_PUT(notit
->dscopes
.stream_event_header
);
547 BT_PUT(notit
->dscopes
.stream_event_context
);
548 BT_PUT(notit
->dscopes
.event_context
);
549 BT_PUT(notit
->dscopes
.event_payload
);
553 void put_all_dscopes(struct bt_ctf_notif_iter
*notit
)
555 BT_PUT(notit
->dscopes
.trace_packet_header
);
556 BT_PUT(notit
->dscopes
.stream_packet_context
);
557 put_event_dscopes(notit
);
561 enum bt_ctf_notif_iter_status
read_packet_header_begin_state(
562 struct bt_ctf_notif_iter
*notit
)
564 struct bt_ctf_field_type
*packet_header_type
= NULL
;
565 enum bt_ctf_notif_iter_status ret
= BT_CTF_NOTIF_ITER_STATUS_OK
;
567 if (bt_ctf_notif_iter_switch_packet(notit
)) {
568 ret
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
572 /* Packet header type is common to the whole trace. */
573 packet_header_type
= bt_ctf_trace_get_packet_header_type(
575 if (!packet_header_type
) {
576 notit
->state
= STATE_AFTER_TRACE_PACKET_HEADER
;
580 ret
= read_dscope_begin_state(notit
, packet_header_type
,
581 STATE_AFTER_TRACE_PACKET_HEADER
,
582 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
583 ¬it
->dscopes
.trace_packet_header
);
585 BT_PUT(packet_header_type
);
590 enum bt_ctf_notif_iter_status
read_packet_header_continue_state(
591 struct bt_ctf_notif_iter
*notit
)
593 return read_dscope_continue_state(notit
,
594 STATE_AFTER_TRACE_PACKET_HEADER
);
598 bool is_struct_type(struct bt_ctf_field_type
*field_type
)
600 return bt_ctf_field_type_get_type_id(field_type
) ==
601 BT_CTF_FIELD_TYPE_ID_STRUCT
;
605 bool is_variant_type(struct bt_ctf_field_type
*field_type
)
607 return bt_ctf_field_type_get_type_id(field_type
) ==
608 BT_CTF_FIELD_TYPE_ID_VARIANT
;
612 struct stream_class_field_path_cache
*
613 create_stream_class_field_path_cache_entry(
614 struct bt_ctf_notif_iter
*notit
,
615 struct bt_ctf_stream_class
*stream_class
)
619 int timestamp_end
= -1;
620 int packet_size
= -1;
621 int content_size
= -1;
622 struct stream_class_field_path_cache
*cache_entry
= g_new0(
623 struct stream_class_field_path_cache
, 1);
624 struct bt_ctf_field_type
*event_header
= NULL
, *packet_context
= NULL
;
630 event_header
= bt_ctf_stream_class_get_event_header_type(stream_class
);
631 if (event_header
&& bt_ctf_field_type_is_structure(event_header
)) {
634 count
= bt_ctf_field_type_structure_get_field_count(
639 for (i
= 0; i
< count
; i
++) {
643 ret
= bt_ctf_field_type_structure_get_field(
644 event_header
, &name
, NULL
, i
);
649 if (v
!= -1 && id
!= -1) {
652 if (v
== -1 && !strcmp(name
, "v")) {
654 } else if (id
== -1 && !strcmp(name
, "id")) {
660 packet_context
= bt_ctf_stream_class_get_packet_context_type(
662 if (packet_context
&& bt_ctf_field_type_is_structure(packet_context
)) {
665 count
= bt_ctf_field_type_structure_get_field_count(
670 for (i
= 0; i
< count
; i
++) {
673 struct bt_ctf_field_type
*field_type
;
675 if (timestamp_end
!= -1 && packet_size
!= -1 &&
676 content_size
!= -1) {
680 ret
= bt_ctf_field_type_structure_get_field(
681 packet_context
, &name
, &field_type
, i
);
686 if (timestamp_end
== -1 &&
687 !strcmp(name
, "timestamp_end")) {
688 struct field_cb_override
*override
= g_new0(
689 struct field_cb_override
, 1);
696 override
->func
= btr_timestamp_end_cb
;
697 override
->data
= notit
;
699 g_hash_table_insert(notit
->field_overrides
,
700 bt_get(field_type
), override
);
703 } else if (packet_size
== -1 &&
704 !strcmp(name
, "packet_size")) {
706 } else if (content_size
== -1 &&
707 !strcmp(name
, "content_size")) {
715 cache_entry
->id
= id
;
716 cache_entry
->timestamp_end
= timestamp_end
;
717 cache_entry
->packet_size
= packet_size
;
718 cache_entry
->content_size
= content_size
;
720 BT_PUT(event_header
);
721 BT_PUT(packet_context
);
730 struct stream_class_field_path_cache
*get_stream_class_field_path_cache(
731 struct bt_ctf_notif_iter
*notit
,
732 struct bt_ctf_stream_class
*stream_class
)
734 bool cache_entry_found
;
735 struct stream_class_field_path_cache
*cache_entry
;
737 cache_entry_found
= g_hash_table_lookup_extended(
738 notit
->sc_field_path_caches
,
739 stream_class
, NULL
, (gpointer
) &cache_entry
);
740 if (unlikely(!cache_entry_found
)) {
741 cache_entry
= create_stream_class_field_path_cache_entry(notit
,
743 g_hash_table_insert(notit
->sc_field_path_caches
,
744 bt_get(stream_class
), (gpointer
) cache_entry
);
751 enum bt_ctf_notif_iter_status
set_current_stream_class(
752 struct bt_ctf_notif_iter
*notit
)
754 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
755 struct bt_ctf_field_type
*packet_header_type
= NULL
;
756 struct bt_ctf_field_type
*stream_id_field_type
= NULL
;
759 /* Clear the current stream class field path cache. */
760 notit
->cur_sc_field_path_cache
= NULL
;
762 /* Is there any "stream_id" field in the packet header? */
763 packet_header_type
= bt_ctf_trace_get_packet_header_type(
765 if (!packet_header_type
) {
766 PERR("Failed to retrieve trace's packet header type\n");
767 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
771 assert(is_struct_type(packet_header_type
));
774 stream_id_field_type
=
775 bt_ctf_field_type_structure_get_field_type_by_name(
776 packet_header_type
, "stream_id");
777 if (stream_id_field_type
) {
778 /* Find appropriate stream class using current stream ID */
780 struct bt_ctf_field
*stream_id_field
= NULL
;
782 assert(notit
->dscopes
.trace_packet_header
);
785 stream_id_field
= bt_ctf_field_structure_get_field(
786 notit
->dscopes
.trace_packet_header
, "stream_id");
787 assert(stream_id_field
);
788 ret
= bt_ctf_field_unsigned_integer_get_value(
789 stream_id_field
, &stream_id
);
791 BT_PUT(stream_id_field
);
793 /* Only one stream: pick the first stream class */
794 assert(bt_ctf_trace_get_stream_class_count(
795 notit
->meta
.trace
) == 1);
799 BT_PUT(notit
->meta
.stream_class
);
800 notit
->meta
.stream_class
= bt_ctf_trace_get_stream_class_by_id(
801 notit
->meta
.trace
, stream_id
);
802 if (!notit
->meta
.stream_class
) {
803 PERR("Cannot find stream class with ID %" PRIu64
"\n",
805 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
810 * Retrieve (or lazily create) the current stream class field path
813 notit
->cur_sc_field_path_cache
= get_stream_class_field_path_cache(
814 notit
, notit
->meta
.stream_class
);
815 if (!notit
->cur_sc_field_path_cache
) {
816 PERR("Failed to retrieve stream class field path cache\n");
817 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
821 BT_PUT(packet_header_type
);
822 BT_PUT(stream_id_field_type
);
828 enum bt_ctf_notif_iter_status
after_packet_header_state(
829 struct bt_ctf_notif_iter
*notit
)
831 enum bt_ctf_notif_iter_status status
;
833 status
= set_current_stream_class(notit
);
834 if (status
== BT_CTF_NOTIF_ITER_STATUS_OK
) {
835 notit
->state
= STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
;
842 enum bt_ctf_notif_iter_status
read_packet_context_begin_state(
843 struct bt_ctf_notif_iter
*notit
)
845 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
846 struct bt_ctf_field_type
*packet_context_type
;
848 assert(notit
->meta
.stream_class
);
849 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(
850 notit
->meta
.stream_class
);
851 if (!packet_context_type
) {
852 notit
->state
= STATE_AFTER_STREAM_PACKET_CONTEXT
;
856 status
= read_dscope_begin_state(notit
, packet_context_type
,
857 STATE_AFTER_STREAM_PACKET_CONTEXT
,
858 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
859 ¬it
->dscopes
.stream_packet_context
);
862 BT_PUT(packet_context_type
);
867 enum bt_ctf_notif_iter_status
read_packet_context_continue_state(
868 struct bt_ctf_notif_iter
*notit
)
870 return read_dscope_continue_state(notit
,
871 STATE_AFTER_STREAM_PACKET_CONTEXT
);
875 enum bt_ctf_notif_iter_status
set_current_packet_content_sizes(
876 struct bt_ctf_notif_iter
*notit
)
878 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
879 struct bt_ctf_field
*packet_size_field
= NULL
;
880 struct bt_ctf_field
*content_size_field
= NULL
;
881 uint64_t content_size
= -1, packet_size
= -1;
883 assert(notit
->dscopes
.stream_packet_context
);
885 packet_size_field
= bt_ctf_field_structure_get_field(
886 notit
->dscopes
.stream_packet_context
, "packet_size");
887 content_size_field
= bt_ctf_field_structure_get_field(
888 notit
->dscopes
.stream_packet_context
, "content_size");
889 if (packet_size_field
) {
890 int ret
= bt_ctf_field_unsigned_integer_get_value(
891 packet_size_field
, &packet_size
);
894 if (packet_size
== 0) {
895 PERR("Decoded packet size is 0\n");
896 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
898 } else if ((packet_size
% 8) != 0) {
899 PERR("Decoded packet size is not a multiple of 8\n");
900 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
905 if (content_size_field
) {
906 int ret
= bt_ctf_field_unsigned_integer_get_value(
907 content_size_field
, &content_size
);
911 content_size
= packet_size
;
914 notit
->cur_packet_size
= packet_size
;
915 notit
->cur_content_size
= content_size
;
917 BT_PUT(packet_size_field
);
918 BT_PUT(content_size_field
);
923 enum bt_ctf_notif_iter_status
after_packet_context_state(
924 struct bt_ctf_notif_iter
*notit
)
926 enum bt_ctf_notif_iter_status status
;
928 status
= set_current_packet_content_sizes(notit
);
929 if (status
== BT_CTF_NOTIF_ITER_STATUS_OK
) {
930 notit
->state
= STATE_EMIT_NOTIF_NEW_PACKET
;
937 enum bt_ctf_notif_iter_status
read_event_header_begin_state(
938 struct bt_ctf_notif_iter
*notit
)
940 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
941 struct bt_ctf_field_type
*event_header_type
= NULL
;
943 /* Check if we have some content left */
944 if (notit
->cur_content_size
>= 0) {
945 if (packet_at(notit
) == notit
->cur_content_size
) {
946 /* No more events! */
947 notit
->state
= STATE_EMIT_NOTIF_END_OF_PACKET
;
949 } else if (packet_at(notit
) > notit
->cur_content_size
) {
950 /* That's not supposed to happen */
951 PERR("Cursor passed packet's content size:\n");
952 PERR("\tDecoded content size: %zu\n",
953 notit
->cur_content_size
);
954 PERR("\tCursor position: %zu\n", packet_at(notit
));
955 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
960 event_header_type
= bt_ctf_stream_class_get_event_header_type(
961 notit
->meta
.stream_class
);
962 if (!event_header_type
) {
963 notit
->state
= STATE_AFTER_STREAM_EVENT_HEADER
;
967 put_event_dscopes(notit
);
968 status
= read_dscope_begin_state(notit
, event_header_type
,
969 STATE_AFTER_STREAM_EVENT_HEADER
,
970 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
,
971 ¬it
->dscopes
.stream_event_header
);
973 BT_PUT(event_header_type
);
979 enum bt_ctf_notif_iter_status
read_event_header_continue_state(
980 struct bt_ctf_notif_iter
*notit
)
982 return read_dscope_continue_state(notit
,
983 STATE_AFTER_STREAM_EVENT_HEADER
);
987 enum bt_ctf_notif_iter_status
set_current_event_class(struct bt_ctf_notif_iter
*notit
)
990 * The assert() calls in this function are okay because it is
991 * assumed here that all the metadata objects have been
992 * validated for CTF correctness before decoding actual streams.
995 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
996 struct bt_ctf_field_type
*event_header_type
;
997 struct bt_ctf_field_type
*id_field_type
= NULL
;
998 struct bt_ctf_field_type
*v_field_type
= NULL
;
999 uint64_t event_id
= -1ULL;
1002 event_header_type
= bt_ctf_stream_class_get_event_header_type(
1003 notit
->meta
.stream_class
);
1004 if (!event_header_type
) {
1005 PERR("Failed to retrieve stream class's event header type\n");
1006 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1010 /* Is there any "id"/"v" field in the event header? */
1011 assert(is_struct_type(event_header_type
));
1012 id_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1013 event_header_type
, "id");
1014 v_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1015 event_header_type
, "v");
1016 assert(notit
->dscopes
.stream_event_header
);
1020 * | | |_ _|_ _| __ __ _
1021 * | | | | | || '_ \ / _` |
1022 * | |___| | | || | | | (_| | S P E C I A L
1023 * |_____|_| |_||_| |_|\__, | C A S E â„¢
1026 struct bt_ctf_field
*v_field
= NULL
;
1027 struct bt_ctf_field
*v_struct_field
= NULL
;
1028 struct bt_ctf_field
*v_struct_id_field
= NULL
;
1030 // TODO: optimalize!
1031 v_field
= bt_ctf_field_structure_get_field(
1032 notit
->dscopes
.stream_event_header
, "v");
1036 bt_ctf_field_variant_get_current_field(v_field
);
1037 if (!v_struct_field
) {
1038 goto end_v_field_type
;
1041 // TODO: optimalize!
1043 bt_ctf_field_structure_get_field(v_struct_field
, "id");
1044 if (!v_struct_id_field
) {
1045 goto end_v_field_type
;
1048 ret
= bt_ctf_field_unsigned_integer_get_value(
1049 v_struct_id_field
, &event_id
);
1056 BT_PUT(v_struct_field
);
1057 BT_PUT(v_struct_id_field
);
1060 if (id_field_type
&& event_id
== -1ULL) {
1061 /* Check "id" field */
1062 struct bt_ctf_field
*id_field
= NULL
;
1065 // TODO: optimalize!
1066 id_field
= bt_ctf_field_structure_get_field(
1067 notit
->dscopes
.stream_event_header
, "id");
1069 assert(bt_ctf_field_is_integer(id_field
) ||
1070 bt_ctf_field_is_enumeration(id_field
));
1072 if (bt_ctf_field_is_integer(id_field
)) {
1073 ret
= bt_ctf_field_unsigned_integer_get_value(
1074 id_field
, &event_id
);
1076 struct bt_ctf_field
*container
;
1078 container
= bt_ctf_field_enumeration_get_container(
1081 ret
= bt_ctf_field_unsigned_integer_get_value(
1082 container
, &event_id
);
1089 if (event_id
== -1ULL) {
1090 /* Event ID not found: single event? */
1091 assert(bt_ctf_stream_class_get_event_class_count(
1092 notit
->meta
.stream_class
) == 1);
1096 BT_PUT(notit
->meta
.event_class
);
1097 notit
->meta
.event_class
= bt_ctf_stream_class_get_event_class_by_id(
1098 notit
->meta
.stream_class
, event_id
);
1099 if (!notit
->meta
.event_class
) {
1100 PERR("Cannot find event class with ID %" PRIu64
"\n", event_id
);
1101 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1106 BT_PUT(event_header_type
);
1107 BT_PUT(id_field_type
);
1108 BT_PUT(v_field_type
);
1114 enum bt_ctf_notif_iter_status
after_event_header_state(
1115 struct bt_ctf_notif_iter
*notit
)
1117 enum bt_ctf_notif_iter_status status
;
1119 status
= set_current_event_class(notit
);
1120 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
1121 PERR("Failed to set current event class\n");
1125 notit
->state
= STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
;
1132 enum bt_ctf_notif_iter_status
read_stream_event_context_begin_state(
1133 struct bt_ctf_notif_iter
*notit
)
1135 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1136 struct bt_ctf_field_type
*stream_event_context_type
;
1138 stream_event_context_type
= bt_ctf_stream_class_get_event_context_type(
1139 notit
->meta
.stream_class
);
1140 if (!stream_event_context_type
) {
1141 notit
->state
= STATE_DSCOPE_EVENT_CONTEXT_BEGIN
;
1145 status
= read_dscope_begin_state(notit
, stream_event_context_type
,
1146 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
,
1147 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
,
1148 ¬it
->dscopes
.stream_event_context
);
1151 BT_PUT(stream_event_context_type
);
1157 enum bt_ctf_notif_iter_status
read_stream_event_context_continue_state(
1158 struct bt_ctf_notif_iter
*notit
)
1160 return read_dscope_continue_state(notit
,
1161 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
);
1165 enum bt_ctf_notif_iter_status
read_event_context_begin_state(
1166 struct bt_ctf_notif_iter
*notit
)
1168 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1169 struct bt_ctf_field_type
*event_context_type
;
1171 event_context_type
= bt_ctf_event_class_get_context_type(
1172 notit
->meta
.event_class
);
1173 if (!event_context_type
) {
1174 notit
->state
= STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
;
1177 status
= read_dscope_begin_state(notit
, event_context_type
,
1178 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
1179 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
,
1180 ¬it
->dscopes
.event_context
);
1183 BT_PUT(event_context_type
);
1189 enum bt_ctf_notif_iter_status
read_event_context_continue_state(
1190 struct bt_ctf_notif_iter
*notit
)
1192 return read_dscope_continue_state(notit
,
1193 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
);
1197 enum bt_ctf_notif_iter_status
read_event_payload_begin_state(
1198 struct bt_ctf_notif_iter
*notit
)
1200 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1201 struct bt_ctf_field_type
*event_payload_type
;
1203 event_payload_type
= bt_ctf_event_class_get_payload_type(
1204 notit
->meta
.event_class
);
1205 if (!event_payload_type
) {
1206 notit
->state
= STATE_EMIT_NOTIF_EVENT
;
1210 status
= read_dscope_begin_state(notit
, event_payload_type
,
1211 STATE_EMIT_NOTIF_EVENT
,
1212 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
1213 ¬it
->dscopes
.event_payload
);
1216 BT_PUT(event_payload_type
);
1222 enum bt_ctf_notif_iter_status
read_event_payload_continue_state(
1223 struct bt_ctf_notif_iter
*notit
)
1225 return read_dscope_continue_state(notit
, STATE_EMIT_NOTIF_EVENT
);
1229 enum bt_ctf_notif_iter_status
skip_packet_padding_state(
1230 struct bt_ctf_notif_iter
*notit
)
1232 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1233 size_t bits_to_skip
;
1235 assert(notit
->cur_packet_size
> 0);
1236 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1237 if (bits_to_skip
== 0) {
1238 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1241 size_t bits_to_consume
;
1242 status
= buf_ensure_available_bits(notit
);
1243 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
1247 bits_to_consume
= MIN(buf_available_bits(notit
), bits_to_skip
);
1248 buf_consume_bits(notit
, bits_to_consume
);
1249 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1250 if (bits_to_skip
== 0) {
1251 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1261 enum bt_ctf_notif_iter_status
handle_state(struct bt_ctf_notif_iter
*notit
)
1263 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1265 PDBG("Handling state %d\n", notit
->state
);
1267 // TODO: optimalize!
1268 switch (notit
->state
) {
1270 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1272 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
1273 status
= read_packet_header_begin_state(notit
);
1275 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
1276 status
= read_packet_header_continue_state(notit
);
1278 case STATE_AFTER_TRACE_PACKET_HEADER
:
1279 status
= after_packet_header_state(notit
);
1281 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
1282 status
= read_packet_context_begin_state(notit
);
1284 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
1285 status
= read_packet_context_continue_state(notit
);
1287 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
1288 status
= after_packet_context_state(notit
);
1290 case STATE_EMIT_NOTIF_NEW_PACKET
:
1291 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1293 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
:
1294 status
= read_event_header_begin_state(notit
);
1296 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
:
1297 status
= read_event_header_continue_state(notit
);
1299 case STATE_AFTER_STREAM_EVENT_HEADER
:
1300 status
= after_event_header_state(notit
);
1302 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
:
1303 status
= read_stream_event_context_begin_state(notit
);
1305 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
:
1306 status
= read_stream_event_context_continue_state(notit
);
1308 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN
:
1309 status
= read_event_context_begin_state(notit
);
1311 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
:
1312 status
= read_event_context_continue_state(notit
);
1314 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
1315 status
= read_event_payload_begin_state(notit
);
1317 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
1318 status
= read_event_payload_continue_state(notit
);
1320 case STATE_EMIT_NOTIF_EVENT
:
1321 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1323 case STATE_SKIP_PACKET_PADDING
:
1324 status
= skip_packet_padding_state(notit
);
1326 case STATE_EMIT_NOTIF_END_OF_PACKET
:
1327 notit
->state
= STATE_SKIP_PACKET_PADDING
;
1335 * Resets the internal state of a CTF notification iterator.
1338 void bt_ctf_notif_iter_reset(struct bt_ctf_notif_iter
*notit
)
1341 stack_clear(notit
->stack
);
1342 BT_PUT(notit
->meta
.stream_class
);
1343 BT_PUT(notit
->meta
.event_class
);
1344 BT_PUT(notit
->packet
);
1345 put_all_dscopes(notit
);
1346 notit
->buf
.addr
= NULL
;
1349 notit
->buf
.packet_offset
= 0;
1350 notit
->state
= STATE_INIT
;
1351 notit
->cur_content_size
= -1;
1352 notit
->cur_packet_size
= -1;
1356 int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter
*notit
)
1361 stack_clear(notit
->stack
);
1362 BT_PUT(notit
->meta
.stream_class
);
1363 BT_PUT(notit
->meta
.event_class
);
1364 BT_PUT(notit
->packet
);
1365 BT_PUT(notit
->cur_timestamp_end
);
1366 put_all_dscopes(notit
);
1369 * Adjust current buffer so that addr points to the beginning of the new
1372 if (notit
->buf
.addr
) {
1373 size_t consumed_bytes
= (size_t) (notit
->buf
.at
/ CHAR_BIT
);
1375 /* Packets are assumed to start on a byte frontier. */
1376 if (notit
->buf
.at
% CHAR_BIT
) {
1381 notit
->buf
.addr
+= consumed_bytes
;
1382 notit
->buf
.sz
-= consumed_bytes
;
1384 notit
->buf
.packet_offset
= 0;
1387 notit
->cur_content_size
= -1;
1388 notit
->cur_packet_size
= -1;
1389 notit
->cur_sc_field_path_cache
= NULL
;
1395 struct bt_ctf_field
*get_next_field(struct bt_ctf_notif_iter
*notit
)
1397 struct bt_ctf_field
*next_field
= NULL
;
1398 struct bt_ctf_field
*base_field
;
1399 struct bt_ctf_field_type
*base_type
;
1402 assert(!stack_empty(notit
->stack
));
1403 index
= stack_top(notit
->stack
)->index
;
1404 base_field
= stack_top(notit
->stack
)->base
;
1405 base_type
= bt_ctf_field_get_type(base_field
);
1407 PERR("Failed to get base field's type\n");
1411 switch (bt_ctf_field_type_get_type_id(base_type
)) {
1412 case BT_CTF_FIELD_TYPE_ID_STRUCT
:
1413 next_field
= bt_ctf_field_structure_get_field_by_index(
1416 case BT_CTF_FIELD_TYPE_ID_ARRAY
:
1417 next_field
= bt_ctf_field_array_get_field(base_field
, index
);
1419 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
1420 next_field
= bt_ctf_field_sequence_get_field(base_field
, index
);
1422 case BT_CTF_FIELD_TYPE_ID_VARIANT
:
1423 next_field
= bt_ctf_field_variant_get_current_field(base_field
);
1437 void update_clock_state(uint64_t *state
,
1438 struct bt_ctf_field
*value_field
)
1440 struct bt_ctf_field_type
*value_type
= NULL
;
1441 uint64_t requested_new_value
;
1442 uint64_t requested_new_value_mask
;
1443 uint64_t cur_value_masked
;
1444 int requested_new_value_size
;
1447 value_type
= bt_ctf_field_get_type(value_field
);
1450 requested_new_value_size
=
1451 bt_ctf_field_type_integer_get_size(value_type
);
1452 assert(requested_new_value_size
> 0);
1454 ret
= bt_ctf_field_unsigned_integer_get_value(value_field
,
1455 &requested_new_value
);
1459 * Special case for a 64-bit new value, which is the limit
1460 * of a clock value as of this version: overwrite the
1461 * current value directly.
1463 if (requested_new_value_size
== 64) {
1464 *state
= requested_new_value
;
1468 requested_new_value_mask
= (1ULL << requested_new_value_size
) - 1;
1469 cur_value_masked
= *state
& requested_new_value_mask
;
1471 if (requested_new_value
< cur_value_masked
) {
1473 * It looks like a wrap happened on the number of bits
1474 * of the requested new value. Assume that the clock
1475 * value wrapped only one time.
1477 *state
+= requested_new_value_mask
+ 1;
1480 /* Clear the low bits of the current clock value. */
1481 *state
&= ~requested_new_value_mask
;
1483 /* Set the low bits of the current clock value. */
1484 *state
|= requested_new_value
;
1490 enum bt_ctf_btr_status
update_clock(struct bt_ctf_notif_iter
*notit
,
1491 struct bt_ctf_field
*int_field
)
1493 gboolean clock_class_found
;
1494 uint64_t *clock_state
;
1495 struct bt_ctf_field_type
*int_field_type
= NULL
;
1496 enum bt_ctf_btr_status ret
= BT_CTF_BTR_STATUS_OK
;
1497 struct bt_ctf_clock_class
*clock_class
= NULL
;
1499 int_field_type
= bt_ctf_field_get_type(int_field
);
1500 if (unlikely(!int_field_type
)) {
1504 clock_class
= bt_ctf_field_type_integer_get_mapped_clock_class(
1506 if (likely(!clock_class
)) {
1510 clock_class_found
= g_hash_table_lookup_extended(notit
->clock_states
,
1511 clock_class
, NULL
, (gpointer
) &clock_state
);
1512 if (unlikely(!clock_class_found
)) {
1513 const char *clock_class_name
=
1514 bt_ctf_clock_class_get_name(clock_class
);
1516 PERR("Unknown clock class %s mapped to integer encountered in stream\n",
1517 clock_class_name
? : "NULL");
1518 ret
= BT_CTF_BTR_STATUS_ERROR
;
1522 if (unlikely(!clock_state
)) {
1523 clock_state
= g_new0(uint64_t, 1);
1525 ret
= BT_CTF_BTR_STATUS_ENOMEM
;
1528 g_hash_table_insert(notit
->clock_states
, bt_get(clock_class
),
1532 /* Update the clock's state. */
1533 update_clock_state(clock_state
, int_field
);
1535 bt_put(int_field_type
);
1536 bt_put(clock_class
);
1541 enum bt_ctf_btr_status
btr_unsigned_int_common(uint64_t value
,
1542 struct bt_ctf_field_type
*type
, void *data
,
1543 struct bt_ctf_field
**out_int_field
)
1545 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1546 struct bt_ctf_field
*field
= NULL
;
1547 struct bt_ctf_field
*int_field
= NULL
;
1548 struct bt_ctf_notif_iter
*notit
= data
;
1551 /* Create next field */
1552 field
= get_next_field(notit
);
1554 PERR("Failed to get next field (unsigned int)\n");
1555 status
= BT_CTF_BTR_STATUS_ERROR
;
1559 switch(bt_ctf_field_type_get_type_id(type
)) {
1560 case BT_CTF_FIELD_TYPE_ID_INTEGER
:
1561 /* Integer field is created field */
1562 BT_MOVE(int_field
, field
);
1565 case BT_CTF_FIELD_TYPE_ID_ENUM
:
1566 int_field
= bt_ctf_field_enumeration_get_container(field
);
1567 type
= bt_ctf_field_get_type(int_field
);
1576 PERR("Failed to get integer field\n");
1577 status
= BT_CTF_BTR_STATUS_ERROR
;
1581 ret
= bt_ctf_field_unsigned_integer_set_value(int_field
, value
);
1583 stack_top(notit
->stack
)->index
++;
1584 *out_int_field
= int_field
;
1594 enum bt_ctf_btr_status
btr_timestamp_end_cb(void *value
,
1595 struct bt_ctf_field_type
*type
, void *data
)
1597 enum bt_ctf_btr_status status
;
1598 struct bt_ctf_field
*field
= NULL
;
1599 struct bt_ctf_notif_iter
*notit
= data
;
1601 status
= btr_unsigned_int_common(*((uint64_t *) value
), type
, data
,
1604 /* Set as the current packet's timestamp_end field. */
1605 BT_MOVE(notit
->cur_timestamp_end
, field
);
1610 enum bt_ctf_btr_status
btr_unsigned_int_cb(uint64_t value
,
1611 struct bt_ctf_field_type
*type
, void *data
)
1613 struct bt_ctf_notif_iter
*notit
= data
;
1614 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1615 struct bt_ctf_field
*field
= NULL
;
1616 struct field_cb_override
*override
;
1618 override
= g_hash_table_lookup(notit
->field_overrides
,
1620 if (unlikely(override
)) {
1621 status
= override
->func(&value
, type
, override
->data
);
1625 status
= btr_unsigned_int_common(value
, type
, data
, &field
);
1626 if (status
!= BT_CTF_BTR_STATUS_OK
) {
1630 status
= update_clock(notit
, field
);
1637 enum bt_ctf_btr_status
btr_signed_int_cb(int64_t value
,
1638 struct bt_ctf_field_type
*type
, void *data
)
1640 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1641 struct bt_ctf_field
*field
= NULL
;
1642 struct bt_ctf_field
*int_field
= NULL
;
1643 struct bt_ctf_notif_iter
*notit
= data
;
1646 /* create next field */
1647 field
= get_next_field(notit
);
1649 PERR("Failed to get next field (signed int)\n");
1650 status
= BT_CTF_BTR_STATUS_ERROR
;
1654 switch(bt_ctf_field_type_get_type_id(type
)) {
1655 case BT_CTF_FIELD_TYPE_ID_INTEGER
:
1656 /* Integer field is created field */
1657 BT_MOVE(int_field
, field
);
1660 case BT_CTF_FIELD_TYPE_ID_ENUM
:
1661 int_field
= bt_ctf_field_enumeration_get_container(field
);
1662 type
= bt_ctf_field_get_type(int_field
);
1671 PERR("Failed to get integer field\n");
1672 status
= BT_CTF_BTR_STATUS_ERROR
;
1676 ret
= bt_ctf_field_signed_integer_set_value(int_field
, value
);
1678 stack_top(notit
->stack
)->index
++;
1679 status
= update_clock(notit
, int_field
);
1689 enum bt_ctf_btr_status
btr_floating_point_cb(double value
,
1690 struct bt_ctf_field_type
*type
, void *data
)
1692 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1693 struct bt_ctf_field
*field
= NULL
;
1694 struct bt_ctf_notif_iter
*notit
= data
;
1697 /* Create next field */
1698 field
= get_next_field(notit
);
1700 PERR("Failed to get next field (floating point number)\n");
1701 status
= BT_CTF_BTR_STATUS_ERROR
;
1705 ret
= bt_ctf_field_floating_point_set_value(field
, value
);
1707 stack_top(notit
->stack
)->index
++;
1716 enum bt_ctf_btr_status
btr_string_begin_cb(
1717 struct bt_ctf_field_type
*type
, void *data
)
1719 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1720 struct bt_ctf_field
*field
= NULL
;
1721 struct bt_ctf_notif_iter
*notit
= data
;
1724 /* Create next field */
1725 field
= get_next_field(notit
);
1727 PERR("Failed to get next field (string)\n");
1728 status
= BT_CTF_BTR_STATUS_ERROR
;
1733 * Push on stack. Not a compound type per se, but we know that only
1734 * btr_string_cb() may be called between this call and a subsequent
1735 * call to btr_string_end_cb().
1737 ret
= stack_push(notit
->stack
, field
);
1739 PERR("Failed to push string field onto the stack\n");
1740 status
= BT_CTF_BTR_STATUS_ERROR
;
1745 * Initialize string field payload to an empty string since in the
1746 * case of a length 0 string the btr_string_cb won't be called and
1747 * we will end up with an unset string payload.
1749 ret
= bt_ctf_field_string_set_value(field
, "");
1751 PERR("Failed to initialize string field\n");
1752 status
= BT_CTF_BTR_STATUS_ERROR
;
1763 enum bt_ctf_btr_status
btr_string_cb(const char *value
,
1764 size_t len
, struct bt_ctf_field_type
*type
, void *data
)
1766 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1767 struct bt_ctf_field
*field
= NULL
;
1768 struct bt_ctf_notif_iter
*notit
= data
;
1771 /* Get string field */
1772 field
= stack_top(notit
->stack
)->base
;
1775 /* Append current string */
1776 ret
= bt_ctf_field_string_append_len(field
, value
, len
);
1778 PERR("Failed to append a string to a string field\n");
1779 status
= BT_CTF_BTR_STATUS_ERROR
;
1788 enum bt_ctf_btr_status
btr_string_end_cb(
1789 struct bt_ctf_field_type
*type
, void *data
)
1791 struct bt_ctf_notif_iter
*notit
= data
;
1793 /* Pop string field */
1794 stack_pop(notit
->stack
);
1796 /* Go to next field */
1797 stack_top(notit
->stack
)->index
++;
1799 return BT_CTF_BTR_STATUS_OK
;
1802 enum bt_ctf_btr_status
btr_compound_begin_cb(
1803 struct bt_ctf_field_type
*type
, void *data
)
1805 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1806 struct bt_ctf_notif_iter
*notit
= data
;
1807 struct bt_ctf_field
*field
;
1811 if (stack_empty(notit
->stack
)) {
1812 /* Root: create dynamic scope field */
1813 *notit
->cur_dscope_field
= bt_ctf_field_create(type
);
1814 field
= *notit
->cur_dscope_field
;
1817 * Field will be put at the end of this function
1818 * (stack_push() will take one reference, but this
1819 * reference is lost upon the equivalent stack_pop()
1820 * later), so also get it for our context to own it.
1822 bt_get(*notit
->cur_dscope_field
);
1824 field
= get_next_field(notit
);
1828 PERR("Failed to get next field or create dynamic scope field\n");
1829 status
= BT_CTF_BTR_STATUS_ERROR
;
1834 ret
= stack_push(notit
->stack
, field
);
1836 PERR("Failed to push compound field onto the stack\n");
1837 status
= BT_CTF_BTR_STATUS_ERROR
;
1847 enum bt_ctf_btr_status
btr_compound_end_cb(
1848 struct bt_ctf_field_type
*type
, void *data
)
1850 struct bt_ctf_notif_iter
*notit
= data
;
1852 assert(!stack_empty(notit
->stack
));
1855 stack_pop(notit
->stack
);
1857 /* If the stack is not empty, increment the base's index */
1858 if (!stack_empty(notit
->stack
)) {
1859 stack_top(notit
->stack
)->index
++;
1862 return BT_CTF_BTR_STATUS_OK
;
1866 struct bt_ctf_field
*resolve_field(struct bt_ctf_notif_iter
*notit
,
1867 struct bt_ctf_field_path
*path
)
1869 struct bt_ctf_field
*field
= NULL
;
1872 switch (bt_ctf_field_path_get_root_scope(path
)) {
1873 case BT_CTF_SCOPE_TRACE_PACKET_HEADER
:
1874 field
= notit
->dscopes
.trace_packet_header
;
1876 case BT_CTF_SCOPE_STREAM_PACKET_CONTEXT
:
1877 field
= notit
->dscopes
.stream_packet_context
;
1879 case BT_CTF_SCOPE_STREAM_EVENT_HEADER
:
1880 field
= notit
->dscopes
.stream_event_header
;
1882 case BT_CTF_SCOPE_STREAM_EVENT_CONTEXT
:
1883 field
= notit
->dscopes
.stream_event_context
;
1885 case BT_CTF_SCOPE_EVENT_CONTEXT
:
1886 field
= notit
->dscopes
.event_context
;
1888 case BT_CTF_SCOPE_EVENT_FIELDS
:
1889 field
= notit
->dscopes
.event_payload
;
1901 for (i
= 0; i
< bt_ctf_field_path_get_index_count(path
); ++i
) {
1902 struct bt_ctf_field
*next_field
= NULL
;
1903 struct bt_ctf_field_type
*field_type
;
1904 int index
= bt_ctf_field_path_get_index(path
, i
);
1906 field_type
= bt_ctf_field_get_type(field
);
1912 if (is_struct_type(field_type
)) {
1913 next_field
= bt_ctf_field_structure_get_field_by_index(
1915 } else if (is_variant_type(field_type
)) {
1917 bt_ctf_field_variant_get_current_field(field
);
1927 /* Move next field -> field */
1928 BT_MOVE(field
, next_field
);
1936 int64_t btr_get_sequence_length_cb(struct bt_ctf_field_type
*type
, void *data
)
1940 struct bt_ctf_field_path
*field_path
;
1941 struct bt_ctf_notif_iter
*notit
= data
;
1942 struct bt_ctf_field
*length_field
= NULL
;
1945 field_path
= bt_ctf_field_type_sequence_get_length_field_path(type
);
1950 length_field
= resolve_field(notit
, field_path
);
1951 if (!length_field
) {
1955 iret
= bt_ctf_field_unsigned_integer_get_value(length_field
, &length
);
1960 iret
= bt_ctf_field_sequence_set_length(stack_top(notit
->stack
)->base
,
1965 ret
= (int64_t) length
;
1968 BT_PUT(length_field
);
1975 struct bt_ctf_field_type
*btr_get_variant_type_cb(
1976 struct bt_ctf_field_type
*type
, void *data
)
1978 struct bt_ctf_field_path
*path
;
1979 struct bt_ctf_notif_iter
*notit
= data
;
1980 struct bt_ctf_field
*tag_field
= NULL
;
1981 struct bt_ctf_field
*selected_field
= NULL
;
1982 struct bt_ctf_field_type
*selected_field_type
= NULL
;
1984 path
= bt_ctf_field_type_variant_get_tag_field_path(type
);
1989 tag_field
= resolve_field(notit
, path
);
1995 * We found the enumeration tag field instance which should be
1996 * able to select a current field for this variant. This
1997 * callback function we're in is called _after_
1998 * compound_begin(), so the current stack top's base field is
1999 * the variant field in question. We get the selected field here
2000 * thanks to this tag field (thus creating the selected field),
2001 * which will also provide us with its type. Then, this field
2002 * will remain the current selected one until the next callback
2003 * function call which is used to fill the current selected
2006 selected_field
= bt_ctf_field_variant_get_field(
2007 stack_top(notit
->stack
)->base
, tag_field
);
2008 if (!selected_field
) {
2012 selected_field_type
= bt_ctf_field_get_type(selected_field
);
2016 BT_PUT(selected_field
);
2019 return selected_field_type
;
2023 int set_event_clocks(struct bt_ctf_event
*event
,
2024 struct bt_ctf_notif_iter
*notit
)
2027 GHashTableIter iter
;
2028 struct bt_ctf_clock_class
*clock_class
;
2029 uint64_t *clock_state
;
2031 g_hash_table_iter_init(&iter
, notit
->clock_states
);
2033 while (g_hash_table_iter_next(&iter
, (gpointer
) &clock_class
,
2034 (gpointer
) &clock_state
)) {
2035 struct bt_ctf_clock_value
*clock_value
;
2037 clock_value
= bt_ctf_clock_value_create(clock_class
,
2043 ret
= bt_ctf_event_set_clock_value(event
, clock_value
);
2044 bt_put(clock_value
);
2055 struct bt_ctf_event
*create_event(struct bt_ctf_notif_iter
*notit
)
2058 struct bt_ctf_event
*event
;
2060 /* Create event object. */
2061 event
= bt_ctf_event_create(notit
->meta
.event_class
);
2066 /* Set header, stream event context, context, and payload fields. */
2067 ret
= bt_ctf_event_set_header(event
,
2068 notit
->dscopes
.stream_event_header
);
2073 ret
= bt_ctf_event_set_stream_event_context(event
,
2074 notit
->dscopes
.stream_event_context
);
2079 ret
= bt_ctf_event_set_event_context(event
,
2080 notit
->dscopes
.event_context
);
2085 ret
= bt_ctf_event_set_event_payload(event
,
2086 notit
->dscopes
.event_payload
);
2091 ret
= set_event_clocks(event
, notit
);
2096 /* Associate with current packet. */
2097 assert(notit
->packet
);
2098 ret
= bt_ctf_event_set_packet(event
, notit
->packet
);
2111 void create_packet(struct bt_ctf_notif_iter
*notit
)
2114 struct bt_ctf_stream
*stream
= NULL
;
2115 struct bt_ctf_packet
*packet
= NULL
;
2117 /* Ask the user for the stream */
2118 stream
= notit
->medium
.medops
.get_stream(notit
->meta
.stream_class
,
2119 notit
->medium
.data
);
2125 packet
= bt_ctf_packet_create(stream
);
2130 /* Set packet's context and header fields */
2131 if (notit
->dscopes
.trace_packet_header
) {
2132 ret
= bt_ctf_packet_set_header(packet
,
2133 notit
->dscopes
.trace_packet_header
);
2139 if (notit
->dscopes
.stream_packet_context
) {
2140 ret
= bt_ctf_packet_set_context(packet
,
2141 notit
->dscopes
.stream_packet_context
);
2151 BT_MOVE(notit
->packet
, packet
);
2155 void notify_new_packet(struct bt_ctf_notif_iter
*notit
,
2156 struct bt_notification
**notification
)
2158 struct bt_notification
*ret
;
2160 /* Initialize the iterator's current packet */
2161 create_packet(notit
);
2162 if (!notit
->packet
) {
2166 ret
= bt_notification_packet_begin_create(notit
->packet
);
2170 *notification
= ret
;
2174 void notify_end_of_packet(struct bt_ctf_notif_iter
*notit
,
2175 struct bt_notification
**notification
)
2177 struct bt_notification
*ret
;
2179 if (!notit
->packet
) {
2183 ret
= bt_notification_packet_end_create(notit
->packet
);
2187 BT_PUT(notit
->packet
);
2188 *notification
= ret
;
2192 void notify_event(struct bt_ctf_notif_iter
*notit
,
2193 struct bt_clock_class_priority_map
*cc_prio_map
,
2194 struct bt_notification
**notification
)
2196 struct bt_ctf_event
*event
;
2197 struct bt_notification
*ret
= NULL
;
2200 event
= create_event(notit
);
2205 ret
= bt_notification_event_create(event
, cc_prio_map
);
2209 *notification
= ret
;
2216 void notify_eos(struct bt_ctf_notif_iter
*notit
,
2217 struct bt_notification
**notification
)
2219 struct bt_ctf_stream
*stream
= NULL
;
2220 struct bt_notification
*ret
= NULL
;
2222 /* Ask the user for the stream */
2223 stream
= notit
->medium
.medops
.get_stream(notit
->meta
.stream_class
,
2224 notit
->medium
.data
);
2229 ret
= bt_notification_stream_end_create(stream
);
2233 *notification
= ret
;
2239 int init_clock_states(GHashTable
*clock_states
, struct bt_ctf_trace
*trace
)
2241 int clock_class_count
, i
, ret
= 0;
2243 clock_class_count
= bt_ctf_trace_get_clock_class_count(trace
);
2244 if (clock_class_count
<= 0) {
2249 for (i
= 0; i
< clock_class_count
; i
++) {
2250 struct bt_ctf_clock_class
*clock_class
;
2252 clock_class
= bt_ctf_trace_get_clock_class_by_index(trace
, i
);
2258 g_hash_table_insert(clock_states
, bt_get(clock_class
), NULL
);
2259 bt_put(clock_class
);
2266 void init_trace_field_path_cache(struct bt_ctf_trace
*trace
,
2267 struct trace_field_path_cache
*trace_field_path_cache
)
2270 int stream_instance_id
= -1;
2272 struct bt_ctf_field_type
*packet_header
= NULL
;
2274 packet_header
= bt_ctf_trace_get_packet_header_type(trace
);
2275 if (!packet_header
) {
2279 if (!bt_ctf_field_type_is_structure(packet_header
)) {
2283 count
= bt_ctf_field_type_structure_get_field_count(packet_header
);
2288 for (i
= 0; (i
< count
&& (stream_id
== -1 || stream_instance_id
== -1)); i
++) {
2290 const char *field_name
;
2292 ret
= bt_ctf_field_type_structure_get_field(packet_header
,
2293 &field_name
, NULL
, i
);
2298 if (stream_id
== -1 && !strcmp(field_name
, "stream_id")) {
2300 } else if (stream_instance_id
== -1 &&
2301 !strcmp(field_name
, "stream_instance_id")) {
2302 stream_instance_id
= i
;
2306 trace_field_path_cache
->stream_id
= stream_id
;
2307 trace_field_path_cache
->stream_instance_id
= stream_instance_id
;
2308 BT_PUT(packet_header
);
2312 struct bt_ctf_notif_iter
*bt_ctf_notif_iter_create(struct bt_ctf_trace
*trace
,
2313 size_t max_request_sz
,
2314 struct bt_ctf_notif_iter_medium_ops medops
,
2315 void *data
, FILE *err_stream
)
2318 struct bt_ctf_notif_iter
*notit
= NULL
;
2319 struct bt_ctf_btr_cbs cbs
= {
2321 .signed_int
= btr_signed_int_cb
,
2322 .unsigned_int
= btr_unsigned_int_cb
,
2323 .floating_point
= btr_floating_point_cb
,
2324 .string_begin
= btr_string_begin_cb
,
2325 .string
= btr_string_cb
,
2326 .string_end
= btr_string_end_cb
,
2327 .compound_begin
= btr_compound_begin_cb
,
2328 .compound_end
= btr_compound_end_cb
,
2331 .get_sequence_length
= btr_get_sequence_length_cb
,
2332 .get_variant_type
= btr_get_variant_type_cb
,
2337 assert(medops
.request_bytes
);
2338 assert(medops
.get_stream
);
2339 notit
= g_new0(struct bt_ctf_notif_iter
, 1);
2341 PERR("Failed to allocate memory for CTF notification iterator\n");
2344 notit
->clock_states
= g_hash_table_new_full(g_direct_hash
,
2345 g_direct_equal
, bt_put
, g_free
);
2346 if (!notit
->clock_states
) {
2347 PERR("Failed to create hash table\n");
2350 ret
= init_clock_states(notit
->clock_states
, trace
);
2352 PERR("Failed to initialize stream clock states\n");
2355 notit
->meta
.trace
= bt_get(trace
);
2356 notit
->medium
.medops
= medops
;
2357 notit
->medium
.max_request_sz
= max_request_sz
;
2358 notit
->medium
.data
= data
;
2359 notit
->err_stream
= err_stream
;
2360 notit
->stack
= stack_new(notit
);
2361 if (!notit
->stack
) {
2362 PERR("Failed to create stack\n");
2366 notit
->btr
= bt_ctf_btr_create(cbs
, notit
, err_stream
);
2368 PERR("Failed to create binary type reader\n");
2372 bt_ctf_notif_iter_reset(notit
);
2374 init_trace_field_path_cache(trace
, ¬it
->trace_field_path_cache
);
2375 notit
->sc_field_path_caches
= g_hash_table_new_full(g_direct_hash
,
2376 g_direct_equal
, bt_put
, g_free
);
2377 if (!notit
->sc_field_path_caches
) {
2378 PERR("Failed to create stream class field path caches\n");
2382 notit
->field_overrides
= g_hash_table_new_full(g_direct_hash
,
2383 g_direct_equal
, bt_put
, g_free
);
2384 if (!notit
->field_overrides
) {
2391 bt_ctf_notif_iter_destroy(notit
);
2396 void bt_ctf_notif_iter_destroy(struct bt_ctf_notif_iter
*notit
)
2398 BT_PUT(notit
->meta
.trace
);
2399 BT_PUT(notit
->meta
.stream_class
);
2400 BT_PUT(notit
->meta
.event_class
);
2401 BT_PUT(notit
->packet
);
2402 BT_PUT(notit
->cur_timestamp_end
);
2403 put_all_dscopes(notit
);
2406 stack_destroy(notit
->stack
);
2410 bt_ctf_btr_destroy(notit
->btr
);
2413 if (notit
->clock_states
) {
2414 g_hash_table_destroy(notit
->clock_states
);
2417 if (notit
->sc_field_path_caches
) {
2418 g_hash_table_destroy(notit
->sc_field_path_caches
);
2421 if (notit
->field_overrides
) {
2422 g_hash_table_destroy(notit
->field_overrides
);
2427 enum bt_ctf_notif_iter_status
bt_ctf_notif_iter_get_next_notification(
2428 struct bt_ctf_notif_iter
*notit
,
2429 struct bt_clock_class_priority_map
*cc_prio_map
,
2430 struct bt_notification
**notification
)
2432 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
2435 assert(notification
);
2438 status
= handle_state(notit
);
2439 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
2440 if (status
== BT_CTF_NOTIF_ITER_STATUS_EOF
) {
2441 PDBG("Medium operation reported end of stream\n");
2443 PERR("Failed to handle state:\n");
2444 PERR("\tState: %d\n", notit
->state
);
2449 switch (notit
->state
) {
2450 case STATE_EMIT_NOTIF_NEW_PACKET
:
2451 PDBG("Emitting new packet notification\n");
2452 notify_new_packet(notit
, notification
);
2453 if (!*notification
) {
2454 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
2457 case STATE_EMIT_NOTIF_EVENT
:
2458 PDBG("Emitting event notification\n");
2459 notify_event(notit
, cc_prio_map
, notification
);
2460 if (!*notification
) {
2461 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
2464 case STATE_EMIT_NOTIF_END_OF_PACKET
:
2465 /* Update clock with timestamp_end field. */
2466 if (notit
->cur_timestamp_end
) {
2467 enum bt_ctf_btr_status btr_status
;
2468 struct bt_ctf_field_type
*field_type
=
2469 bt_ctf_field_get_type(
2470 notit
->cur_timestamp_end
);
2472 btr_status
= update_clock(notit
,
2473 notit
->cur_timestamp_end
);
2475 if (btr_status
!= BT_CTF_BTR_STATUS_OK
) {
2476 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
2481 PDBG("Emitting end of packet notification\n");
2482 notify_end_of_packet(notit
, notification
);
2483 if (!*notification
) {
2484 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
2488 /* Non-emitting state: continue */