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/plugin/notification/packet.h>
42 #include <babeltrace/plugin/notification/event.h>
43 #include <babeltrace/plugin/notification/stream.h>
44 #include <babeltrace/ref.h>
47 #define PRINT_ERR_STREAM notit->err_stream
48 #define PRINT_PREFIX "ctf-notif-iter"
51 #include "notif-iter.h"
52 #include "../btr/btr.h"
54 #define BYTES_TO_BITS(x) ((x) * 8)
56 struct bt_ctf_notif_iter
;
58 /* A visit stack entry */
61 * Current base field, one of:
69 * Field is owned by this.
71 struct bt_ctf_field
*base
;
73 /* index of next field to set */
79 /* Entries (struct stack_entry *) (top is last element) */
86 STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
,
87 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
88 STATE_AFTER_TRACE_PACKET_HEADER
,
89 STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
,
90 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
91 STATE_AFTER_STREAM_PACKET_CONTEXT
,
92 STATE_EMIT_NOTIF_NEW_PACKET
,
93 STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
,
94 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
,
95 STATE_AFTER_STREAM_EVENT_HEADER
,
96 STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
,
97 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
,
98 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
,
99 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
,
100 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
101 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
102 STATE_EMIT_NOTIF_EVENT
,
103 STATE_EMIT_NOTIF_END_OF_PACKET
,
104 STATE_SKIP_PACKET_PADDING
,
107 struct trace_field_path_cache
{
109 * Indexes of the stream_id and stream_instance_id field in the packet
110 * header structure, -1 if unset.
113 int stream_instance_id
;
116 struct stream_class_field_path_cache
{
118 * Indexes of the v and id fields in the stream event header structure,
125 * index of the timestamp_end, packet_size and content_size fields in
126 * the stream packet context structure. Set to -1 if the fields were
134 struct field_cb_override
{
135 enum bt_ctf_btr_status (* func
)(void *value
,
136 struct bt_ctf_field_type
*type
, void *data
);
140 /* CTF notification iterator */
141 struct bt_ctf_notif_iter
{
145 /* Error stream (may be NULL) */
149 * Current dynamic scope field pointer.
151 * This is set when a dynamic scope field is first created by
152 * btr_compound_begin_cb(). It points to one of the fields in
155 struct bt_ctf_field
**cur_dscope_field
;
157 /* Trace and classes (owned by this) */
159 struct bt_ctf_trace
*trace
;
160 struct bt_ctf_stream_class
*stream_class
;
161 struct bt_ctf_event_class
*event_class
;
164 /* Current packet (NULL if not created yet) */
165 struct bt_ctf_packet
*packet
;
168 * Current timestamp_end field (to consider before switching packets).
170 struct bt_ctf_field
*cur_timestamp_end
;
172 /* Database of current dynamic scopes (owned by this) */
174 struct bt_ctf_field
*trace_packet_header
;
175 struct bt_ctf_field
*stream_packet_context
;
176 struct bt_ctf_field
*stream_event_header
;
177 struct bt_ctf_field
*stream_event_context
;
178 struct bt_ctf_field
*event_context
;
179 struct bt_ctf_field
*event_payload
;
183 * Special field overrides.
185 * Overrides are used to implement the behaviours of special fields such
186 * as "timestamp_end" (which must be ignored until the end of the
187 * packet), "id" (event id) which can be present multiple times and must
188 * be updated multiple time.
190 * This should be used to implement the behaviour of integer fields
191 * mapped to clocks and other "tagged" fields (in CTF 2).
193 * bt_ctf_field_type to struct field_cb_override
195 GHashTable
*field_overrides
;
200 /* Current medium buffer data */
202 /* Last address provided by medium */
205 /* Buffer size provided by medium (bytes) */
208 /* Offset within whole packet of addr (bits) */
209 size_t packet_offset
;
211 /* Current position from addr (bits) */
215 /* Binary type reader */
216 struct bt_ctf_btr
*btr
;
218 /* Current medium data */
220 struct bt_ctf_notif_iter_medium_ops medops
;
221 size_t max_request_sz
;
225 /* Current packet size (bits) (-1 if unknown) */
226 int64_t cur_packet_size
;
228 /* Current content size (bits) (-1 if unknown) */
229 int64_t cur_content_size
;
231 /* bt_ctf_clock_class to uint64_t. */
232 GHashTable
*clock_states
;
235 * Cache of the trace-constant field paths (event header type)
236 * associated to the current trace.
238 struct trace_field_path_cache trace_field_path_cache
;
241 * Field path cache associated with the current stream class.
242 * Ownership of this structure belongs to the field_path_caches HT.
244 struct stream_class_field_path_cache
*cur_sc_field_path_cache
;
246 /* bt_ctf_stream_class to struct stream_class_field_path_cache. */
247 GHashTable
*sc_field_path_caches
;
251 int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter
*notit
);
254 enum bt_ctf_btr_status
btr_timestamp_end_cb(void *value
,
255 struct bt_ctf_field_type
*type
, void *data
);
258 void stack_entry_free_func(gpointer data
)
260 struct stack_entry
*entry
= data
;
267 struct stack
*stack_new(struct bt_ctf_notif_iter
*notit
)
269 struct stack
*stack
= NULL
;
271 stack
= g_new0(struct stack
, 1);
276 stack
->entries
= g_ptr_array_new_with_free_func(stack_entry_free_func
);
277 if (!stack
->entries
) {
288 void stack_destroy(struct stack
*stack
)
291 g_ptr_array_free(stack
->entries
, TRUE
);
296 int stack_push(struct stack
*stack
, struct bt_ctf_field
*base
)
299 struct stack_entry
*entry
;
303 entry
= g_new0(struct stack_entry
, 1);
309 entry
->base
= bt_get(base
);
310 g_ptr_array_add(stack
->entries
, entry
);
317 unsigned int stack_size(struct stack
*stack
)
321 return stack
->entries
->len
;
325 void stack_pop(struct stack
*stack
)
328 assert(stack_size(stack
));
329 g_ptr_array_remove_index(stack
->entries
, stack
->entries
->len
- 1);
333 struct stack_entry
*stack_top(struct stack
*stack
)
336 assert(stack_size(stack
));
338 return g_ptr_array_index(stack
->entries
, stack
->entries
->len
- 1);
342 bool stack_empty(struct stack
*stack
)
344 return stack_size(stack
) == 0;
348 void stack_clear(struct stack
*stack
)
352 if (!stack_empty(stack
)) {
353 g_ptr_array_remove_range(stack
->entries
, 0, stack_size(stack
));
356 assert(stack_empty(stack
));
360 enum bt_ctf_notif_iter_status
notif_iter_status_from_m_status(
361 enum bt_ctf_notif_iter_medium_status m_status
)
367 size_t buf_size_bits(struct bt_ctf_notif_iter
*notit
)
369 return BYTES_TO_BITS(notit
->buf
.sz
);
373 size_t buf_available_bits(struct bt_ctf_notif_iter
*notit
)
375 return buf_size_bits(notit
) - notit
->buf
.at
;
379 size_t packet_at(struct bt_ctf_notif_iter
*notit
)
381 return notit
->buf
.packet_offset
+ notit
->buf
.at
;
385 size_t remaining_content_bits(struct bt_ctf_notif_iter
*notit
)
387 if (notit
->cur_content_size
== -1) {
391 return notit
->cur_content_size
- packet_at(notit
);
395 size_t remaining_packet_bits(struct bt_ctf_notif_iter
*notit
)
397 if (notit
->cur_packet_size
== -1) {
401 return notit
->cur_packet_size
- packet_at(notit
);
405 void buf_consume_bits(struct bt_ctf_notif_iter
*notit
, size_t incr
)
407 notit
->buf
.at
+= incr
;
411 bool buf_has_enough_bits(struct bt_ctf_notif_iter
*notit
, size_t sz
)
413 return buf_available_bits(notit
) >= sz
;
417 enum bt_ctf_notif_iter_status
request_medium_bytes(
418 struct bt_ctf_notif_iter
*notit
)
420 uint8_t *buffer_addr
;
422 enum bt_ctf_notif_iter_medium_status m_status
;
424 m_status
= notit
->medium
.medops
.request_bytes(
425 notit
->medium
.max_request_sz
, &buffer_addr
,
426 &buffer_sz
, notit
->medium
.data
);
427 if (m_status
== BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK
) {
428 assert(buffer_sz
!= 0);
430 /* New packet offset is old one + old size (in bits) */
431 notit
->buf
.packet_offset
+= buf_size_bits(notit
);
433 /* Restart at the beginning of the new medium buffer */
436 /* New medium buffer size */
437 notit
->buf
.sz
= buffer_sz
;
439 /* New medium buffer address */
440 notit
->buf
.addr
= buffer_addr
;
443 return notif_iter_status_from_m_status(m_status
);
447 enum bt_ctf_notif_iter_status
buf_ensure_available_bits(
448 struct bt_ctf_notif_iter
*notit
)
450 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
452 if (buf_available_bits(notit
) == 0) {
454 * This _cannot_ return BT_CTF_NOTIF_ITER_STATUS_OK
457 status
= request_medium_bytes(notit
);
464 enum bt_ctf_notif_iter_status
read_dscope_begin_state(
465 struct bt_ctf_notif_iter
*notit
,
466 struct bt_ctf_field_type
*dscope_field_type
,
467 enum state done_state
, enum state continue_state
,
468 struct bt_ctf_field
**dscope_field
)
470 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
471 enum bt_ctf_btr_status btr_status
;
472 size_t consumed_bits
;
474 status
= buf_ensure_available_bits(notit
);
475 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
479 bt_put(*dscope_field
);
480 notit
->cur_dscope_field
= dscope_field
;
481 consumed_bits
= bt_ctf_btr_start(notit
->btr
, dscope_field_type
,
482 notit
->buf
.addr
, notit
->buf
.at
, packet_at(notit
),
483 notit
->buf
.sz
, &btr_status
);
485 switch (btr_status
) {
486 case BT_CTF_BTR_STATUS_OK
:
487 /* type was read completely */
488 notit
->state
= done_state
;
490 case BT_CTF_BTR_STATUS_EOF
:
491 notit
->state
= continue_state
;
494 PERR("Binary type reader failed to start\n");
495 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
499 /* Consume bits now since we know we're not in an error state */
500 buf_consume_bits(notit
, consumed_bits
);
507 enum bt_ctf_notif_iter_status
read_dscope_continue_state(
508 struct bt_ctf_notif_iter
*notit
, enum state done_state
)
510 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
511 enum bt_ctf_btr_status btr_status
;
512 size_t consumed_bits
;
514 status
= buf_ensure_available_bits(notit
);
515 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
519 consumed_bits
= bt_ctf_btr_continue(notit
->btr
, notit
->buf
.addr
,
520 notit
->buf
.sz
, &btr_status
);
522 switch (btr_status
) {
523 case BT_CTF_BTR_STATUS_OK
:
524 /* Type was read completely. */
525 notit
->state
= done_state
;
527 case BT_CTF_BTR_STATUS_EOF
:
528 /* Stay in this continue state. */
531 PERR("Binary type reader failed to continue\n");
532 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
536 /* Consume bits now since we know we're not in an error state. */
537 buf_consume_bits(notit
, consumed_bits
);
543 void put_event_dscopes(struct bt_ctf_notif_iter
*notit
)
545 BT_PUT(notit
->dscopes
.stream_event_header
);
546 BT_PUT(notit
->dscopes
.stream_event_context
);
547 BT_PUT(notit
->dscopes
.event_context
);
548 BT_PUT(notit
->dscopes
.event_payload
);
552 void put_all_dscopes(struct bt_ctf_notif_iter
*notit
)
554 BT_PUT(notit
->dscopes
.trace_packet_header
);
555 BT_PUT(notit
->dscopes
.stream_packet_context
);
556 put_event_dscopes(notit
);
560 enum bt_ctf_notif_iter_status
read_packet_header_begin_state(
561 struct bt_ctf_notif_iter
*notit
)
563 struct bt_ctf_field_type
*packet_header_type
= NULL
;
564 enum bt_ctf_notif_iter_status ret
= BT_CTF_NOTIF_ITER_STATUS_OK
;
566 if (bt_ctf_notif_iter_switch_packet(notit
)) {
567 ret
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
571 /* Packet header type is common to the whole trace. */
572 packet_header_type
= bt_ctf_trace_get_packet_header_type(
574 if (!packet_header_type
) {
575 notit
->state
= STATE_AFTER_TRACE_PACKET_HEADER
;
579 ret
= read_dscope_begin_state(notit
, packet_header_type
,
580 STATE_AFTER_TRACE_PACKET_HEADER
,
581 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
582 ¬it
->dscopes
.trace_packet_header
);
584 BT_PUT(packet_header_type
);
589 enum bt_ctf_notif_iter_status
read_packet_header_continue_state(
590 struct bt_ctf_notif_iter
*notit
)
592 return read_dscope_continue_state(notit
,
593 STATE_AFTER_TRACE_PACKET_HEADER
);
597 bool is_struct_type(struct bt_ctf_field_type
*field_type
)
599 return bt_ctf_field_type_get_type_id(field_type
) ==
600 BT_CTF_TYPE_ID_STRUCT
;
604 bool is_variant_type(struct bt_ctf_field_type
*field_type
)
606 return bt_ctf_field_type_get_type_id(field_type
) ==
607 BT_CTF_TYPE_ID_VARIANT
;
611 struct stream_class_field_path_cache
*
612 create_stream_class_field_path_cache_entry(
613 struct bt_ctf_notif_iter
*notit
,
614 struct bt_ctf_stream_class
*stream_class
)
618 int timestamp_end
= -1;
619 int packet_size
= -1;
620 int content_size
= -1;
621 struct stream_class_field_path_cache
*cache_entry
= g_new0(
622 struct stream_class_field_path_cache
, 1);
623 struct bt_ctf_field_type
*event_header
= NULL
, *packet_context
= NULL
;
629 event_header
= bt_ctf_stream_class_get_event_header_type(stream_class
);
630 if (event_header
&& bt_ctf_field_type_is_structure(event_header
)) {
633 count
= bt_ctf_field_type_structure_get_field_count(
638 for (i
= 0; i
< count
; i
++) {
642 ret
= bt_ctf_field_type_structure_get_field(
643 event_header
, &name
, NULL
, i
);
648 if (v
!= -1 && id
!= -1) {
651 if (v
== -1 && !strcmp(name
, "v")) {
653 } else if (id
== -1 && !strcmp(name
, "id")) {
659 packet_context
= bt_ctf_stream_class_get_packet_context_type(
661 if (packet_context
&& bt_ctf_field_type_is_structure(packet_context
)) {
664 count
= bt_ctf_field_type_structure_get_field_count(
669 for (i
= 0; i
< count
; i
++) {
672 struct bt_ctf_field_type
*field_type
;
674 if (timestamp_end
!= -1 && packet_size
!= -1 &&
675 content_size
!= -1) {
679 ret
= bt_ctf_field_type_structure_get_field(
680 packet_context
, &name
, &field_type
, i
);
685 if (timestamp_end
== -1 &&
686 !strcmp(name
, "timestamp_end")) {
687 struct field_cb_override
*override
= g_new0(
688 struct field_cb_override
, 1);
695 override
->func
= btr_timestamp_end_cb
;
696 override
->data
= notit
;
698 g_hash_table_insert(notit
->field_overrides
,
699 bt_get(field_type
), override
);
702 } else if (packet_size
== -1 &&
703 !strcmp(name
, "packet_size")) {
705 } else if (content_size
== -1 &&
706 !strcmp(name
, "content_size")) {
714 cache_entry
->id
= id
;
715 cache_entry
->timestamp_end
= timestamp_end
;
716 cache_entry
->packet_size
= packet_size
;
717 cache_entry
->content_size
= content_size
;
719 BT_PUT(event_header
);
720 BT_PUT(packet_context
);
729 struct stream_class_field_path_cache
*get_stream_class_field_path_cache(
730 struct bt_ctf_notif_iter
*notit
,
731 struct bt_ctf_stream_class
*stream_class
)
733 bool cache_entry_found
;
734 struct stream_class_field_path_cache
*cache_entry
;
736 cache_entry_found
= g_hash_table_lookup_extended(
737 notit
->sc_field_path_caches
,
738 stream_class
, NULL
, (gpointer
) &cache_entry
);
739 if (unlikely(!cache_entry_found
)) {
740 cache_entry
= create_stream_class_field_path_cache_entry(notit
,
742 g_hash_table_insert(notit
->sc_field_path_caches
,
743 bt_get(stream_class
), (gpointer
) cache_entry
);
750 enum bt_ctf_notif_iter_status
set_current_stream_class(
751 struct bt_ctf_notif_iter
*notit
)
753 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
754 struct bt_ctf_field_type
*packet_header_type
= NULL
;
755 struct bt_ctf_field_type
*stream_id_field_type
= NULL
;
758 /* Clear the current stream class field path cache. */
759 notit
->cur_sc_field_path_cache
= NULL
;
761 /* Is there any "stream_id" field in the packet header? */
762 packet_header_type
= bt_ctf_trace_get_packet_header_type(
764 if (!packet_header_type
) {
765 PERR("Failed to retrieve trace's packet header type\n");
766 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
770 assert(is_struct_type(packet_header_type
));
773 stream_id_field_type
=
774 bt_ctf_field_type_structure_get_field_type_by_name(
775 packet_header_type
, "stream_id");
776 if (stream_id_field_type
) {
777 /* Find appropriate stream class using current stream ID */
779 struct bt_ctf_field
*stream_id_field
= NULL
;
781 assert(notit
->dscopes
.trace_packet_header
);
784 stream_id_field
= bt_ctf_field_structure_get_field(
785 notit
->dscopes
.trace_packet_header
, "stream_id");
786 assert(stream_id_field
);
787 ret
= bt_ctf_field_unsigned_integer_get_value(
788 stream_id_field
, &stream_id
);
790 BT_PUT(stream_id_field
);
792 /* Only one stream: pick the first stream class */
793 assert(bt_ctf_trace_get_stream_class_count(
794 notit
->meta
.trace
) == 1);
798 BT_PUT(notit
->meta
.stream_class
);
799 notit
->meta
.stream_class
= bt_ctf_trace_get_stream_class_by_id(
800 notit
->meta
.trace
, stream_id
);
801 if (!notit
->meta
.stream_class
) {
802 PERR("Cannot find stream class with ID %" PRIu64
"\n",
804 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
809 * Retrieve (or lazily create) the current stream class field path
812 notit
->cur_sc_field_path_cache
= get_stream_class_field_path_cache(
813 notit
, notit
->meta
.stream_class
);
814 if (!notit
->cur_sc_field_path_cache
) {
815 PERR("Failed to retrieve stream class field path cache\n");
816 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
820 BT_PUT(packet_header_type
);
821 BT_PUT(stream_id_field_type
);
827 enum bt_ctf_notif_iter_status
after_packet_header_state(
828 struct bt_ctf_notif_iter
*notit
)
830 enum bt_ctf_notif_iter_status status
;
832 status
= set_current_stream_class(notit
);
833 if (status
== BT_CTF_NOTIF_ITER_STATUS_OK
) {
834 notit
->state
= STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
;
841 enum bt_ctf_notif_iter_status
read_packet_context_begin_state(
842 struct bt_ctf_notif_iter
*notit
)
844 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
845 struct bt_ctf_field_type
*packet_context_type
;
847 assert(notit
->meta
.stream_class
);
848 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(
849 notit
->meta
.stream_class
);
850 if (!packet_context_type
) {
851 notit
->state
= STATE_AFTER_STREAM_PACKET_CONTEXT
;
855 status
= read_dscope_begin_state(notit
, packet_context_type
,
856 STATE_AFTER_STREAM_PACKET_CONTEXT
,
857 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
858 ¬it
->dscopes
.stream_packet_context
);
861 BT_PUT(packet_context_type
);
866 enum bt_ctf_notif_iter_status
read_packet_context_continue_state(
867 struct bt_ctf_notif_iter
*notit
)
869 return read_dscope_continue_state(notit
,
870 STATE_AFTER_STREAM_PACKET_CONTEXT
);
874 enum bt_ctf_notif_iter_status
set_current_packet_content_sizes(
875 struct bt_ctf_notif_iter
*notit
)
877 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
878 struct bt_ctf_field
*packet_size_field
= NULL
;
879 struct bt_ctf_field
*content_size_field
= NULL
;
880 uint64_t content_size
= -1, packet_size
= -1;
882 assert(notit
->dscopes
.stream_packet_context
);
884 packet_size_field
= bt_ctf_field_structure_get_field(
885 notit
->dscopes
.stream_packet_context
, "packet_size");
886 content_size_field
= bt_ctf_field_structure_get_field(
887 notit
->dscopes
.stream_packet_context
, "content_size");
888 if (packet_size_field
) {
889 int ret
= bt_ctf_field_unsigned_integer_get_value(
890 packet_size_field
, &packet_size
);
893 if (packet_size
== 0) {
894 PERR("Decoded packet size is 0\n");
895 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
897 } else if ((packet_size
% 8) != 0) {
898 PERR("Decoded packet size is not a multiple of 8\n");
899 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
904 if (content_size_field
) {
905 int ret
= bt_ctf_field_unsigned_integer_get_value(
906 content_size_field
, &content_size
);
910 content_size
= packet_size
;
913 notit
->cur_packet_size
= packet_size
;
914 notit
->cur_content_size
= content_size
;
916 BT_PUT(packet_size_field
);
917 BT_PUT(content_size_field
);
922 enum bt_ctf_notif_iter_status
after_packet_context_state(
923 struct bt_ctf_notif_iter
*notit
)
925 enum bt_ctf_notif_iter_status status
;
927 status
= set_current_packet_content_sizes(notit
);
928 if (status
== BT_CTF_NOTIF_ITER_STATUS_OK
) {
929 notit
->state
= STATE_EMIT_NOTIF_NEW_PACKET
;
936 enum bt_ctf_notif_iter_status
read_event_header_begin_state(
937 struct bt_ctf_notif_iter
*notit
)
939 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
940 struct bt_ctf_field_type
*event_header_type
= NULL
;
942 /* Check if we have some content left */
943 if (notit
->cur_content_size
>= 0) {
944 if (packet_at(notit
) == notit
->cur_content_size
) {
945 /* No more events! */
946 notit
->state
= STATE_EMIT_NOTIF_END_OF_PACKET
;
948 } else if (packet_at(notit
) > notit
->cur_content_size
) {
949 /* That's not supposed to happen */
950 PERR("Cursor passed packet's content size:\n");
951 PERR("\tDecoded content size: %zu\n",
952 notit
->cur_content_size
);
953 PERR("\tCursor position: %zu\n", packet_at(notit
));
954 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
959 event_header_type
= bt_ctf_stream_class_get_event_header_type(
960 notit
->meta
.stream_class
);
961 if (!event_header_type
) {
962 notit
->state
= STATE_AFTER_STREAM_EVENT_HEADER
;
966 put_event_dscopes(notit
);
967 status
= read_dscope_begin_state(notit
, event_header_type
,
968 STATE_AFTER_STREAM_EVENT_HEADER
,
969 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
,
970 ¬it
->dscopes
.stream_event_header
);
972 BT_PUT(event_header_type
);
978 enum bt_ctf_notif_iter_status
read_event_header_continue_state(
979 struct bt_ctf_notif_iter
*notit
)
981 return read_dscope_continue_state(notit
,
982 STATE_AFTER_STREAM_EVENT_HEADER
);
986 enum bt_ctf_notif_iter_status
set_current_event_class(struct bt_ctf_notif_iter
*notit
)
989 * The assert() calls in this function are okay because it is
990 * assumed here that all the metadata objects have been
991 * validated for CTF correctness before decoding actual streams.
994 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
995 struct bt_ctf_field_type
*event_header_type
;
996 struct bt_ctf_field_type
*id_field_type
= NULL
;
997 struct bt_ctf_field_type
*v_field_type
= NULL
;
998 uint64_t event_id
= -1ULL;
1001 event_header_type
= bt_ctf_stream_class_get_event_header_type(
1002 notit
->meta
.stream_class
);
1003 if (!event_header_type
) {
1004 PERR("Failed to retrieve stream class's event header type\n");
1005 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1009 /* Is there any "id"/"v" field in the event header? */
1010 assert(is_struct_type(event_header_type
));
1011 id_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1012 event_header_type
, "id");
1013 v_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1014 event_header_type
, "v");
1015 assert(notit
->dscopes
.stream_event_header
);
1019 * | | |_ _|_ _| __ __ _
1020 * | | | | | || '_ \ / _` |
1021 * | |___| | | || | | | (_| | S P E C I A L
1022 * |_____|_| |_||_| |_|\__, | C A S E â„¢
1025 struct bt_ctf_field
*v_field
= NULL
;
1026 struct bt_ctf_field
*v_struct_field
= NULL
;
1027 struct bt_ctf_field
*v_struct_id_field
= NULL
;
1029 // TODO: optimalize!
1030 v_field
= bt_ctf_field_structure_get_field(
1031 notit
->dscopes
.stream_event_header
, "v");
1035 bt_ctf_field_variant_get_current_field(v_field
);
1036 if (!v_struct_field
) {
1037 goto end_v_field_type
;
1040 // TODO: optimalize!
1042 bt_ctf_field_structure_get_field(v_struct_field
, "id");
1043 if (!v_struct_id_field
) {
1044 goto end_v_field_type
;
1047 ret
= bt_ctf_field_unsigned_integer_get_value(
1048 v_struct_id_field
, &event_id
);
1055 BT_PUT(v_struct_field
);
1056 BT_PUT(v_struct_id_field
);
1059 if (id_field_type
&& event_id
== -1ULL) {
1060 /* Check "id" field */
1061 struct bt_ctf_field
*id_field
= NULL
;
1064 // TODO: optimalize!
1065 id_field
= bt_ctf_field_structure_get_field(
1066 notit
->dscopes
.stream_event_header
, "id");
1068 assert(bt_ctf_field_is_integer(id_field
) ||
1069 bt_ctf_field_is_enumeration(id_field
));
1071 if (bt_ctf_field_is_integer(id_field
)) {
1072 ret
= bt_ctf_field_unsigned_integer_get_value(
1073 id_field
, &event_id
);
1075 struct bt_ctf_field
*container
;
1077 container
= bt_ctf_field_enumeration_get_container(
1080 ret
= bt_ctf_field_unsigned_integer_get_value(
1081 container
, &event_id
);
1088 if (event_id
== -1ULL) {
1089 /* Event ID not found: single event? */
1090 assert(bt_ctf_stream_class_get_event_class_count(
1091 notit
->meta
.stream_class
) == 1);
1095 BT_PUT(notit
->meta
.event_class
);
1096 notit
->meta
.event_class
= bt_ctf_stream_class_get_event_class_by_id(
1097 notit
->meta
.stream_class
, event_id
);
1098 if (!notit
->meta
.event_class
) {
1099 PERR("Cannot find event class with ID %" PRIu64
"\n", event_id
);
1100 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1105 BT_PUT(event_header_type
);
1106 BT_PUT(id_field_type
);
1107 BT_PUT(v_field_type
);
1113 enum bt_ctf_notif_iter_status
after_event_header_state(
1114 struct bt_ctf_notif_iter
*notit
)
1116 enum bt_ctf_notif_iter_status status
;
1118 status
= set_current_event_class(notit
);
1119 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
1120 PERR("Failed to set current event class\n");
1124 notit
->state
= STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
;
1131 enum bt_ctf_notif_iter_status
read_stream_event_context_begin_state(
1132 struct bt_ctf_notif_iter
*notit
)
1134 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1135 struct bt_ctf_field_type
*stream_event_context_type
;
1137 stream_event_context_type
= bt_ctf_stream_class_get_event_context_type(
1138 notit
->meta
.stream_class
);
1139 if (!stream_event_context_type
) {
1140 notit
->state
= STATE_DSCOPE_EVENT_CONTEXT_BEGIN
;
1144 status
= read_dscope_begin_state(notit
, stream_event_context_type
,
1145 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
,
1146 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
,
1147 ¬it
->dscopes
.stream_event_context
);
1150 BT_PUT(stream_event_context_type
);
1156 enum bt_ctf_notif_iter_status
read_stream_event_context_continue_state(
1157 struct bt_ctf_notif_iter
*notit
)
1159 return read_dscope_continue_state(notit
,
1160 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
);
1164 enum bt_ctf_notif_iter_status
read_event_context_begin_state(
1165 struct bt_ctf_notif_iter
*notit
)
1167 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1168 struct bt_ctf_field_type
*event_context_type
;
1170 event_context_type
= bt_ctf_event_class_get_context_type(
1171 notit
->meta
.event_class
);
1172 if (!event_context_type
) {
1173 notit
->state
= STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
;
1176 status
= read_dscope_begin_state(notit
, event_context_type
,
1177 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
1178 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
,
1179 ¬it
->dscopes
.event_context
);
1182 BT_PUT(event_context_type
);
1188 enum bt_ctf_notif_iter_status
read_event_context_continue_state(
1189 struct bt_ctf_notif_iter
*notit
)
1191 return read_dscope_continue_state(notit
,
1192 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
);
1196 enum bt_ctf_notif_iter_status
read_event_payload_begin_state(
1197 struct bt_ctf_notif_iter
*notit
)
1199 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1200 struct bt_ctf_field_type
*event_payload_type
;
1202 event_payload_type
= bt_ctf_event_class_get_payload_type(
1203 notit
->meta
.event_class
);
1204 if (!event_payload_type
) {
1205 notit
->state
= STATE_EMIT_NOTIF_EVENT
;
1209 status
= read_dscope_begin_state(notit
, event_payload_type
,
1210 STATE_EMIT_NOTIF_EVENT
,
1211 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
1212 ¬it
->dscopes
.event_payload
);
1215 BT_PUT(event_payload_type
);
1221 enum bt_ctf_notif_iter_status
read_event_payload_continue_state(
1222 struct bt_ctf_notif_iter
*notit
)
1224 return read_dscope_continue_state(notit
, STATE_EMIT_NOTIF_EVENT
);
1228 enum bt_ctf_notif_iter_status
skip_packet_padding_state(
1229 struct bt_ctf_notif_iter
*notit
)
1231 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1232 size_t bits_to_skip
;
1234 assert(notit
->cur_packet_size
> 0);
1235 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1236 if (bits_to_skip
== 0) {
1237 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1240 size_t bits_to_consume
;
1241 status
= buf_ensure_available_bits(notit
);
1242 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
1246 bits_to_consume
= MIN(buf_available_bits(notit
), bits_to_skip
);
1247 buf_consume_bits(notit
, bits_to_consume
);
1248 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1249 if (bits_to_skip
== 0) {
1250 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1260 enum bt_ctf_notif_iter_status
handle_state(struct bt_ctf_notif_iter
*notit
)
1262 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1264 PDBG("Handling state %d\n", notit
->state
);
1266 // TODO: optimalize!
1267 switch (notit
->state
) {
1269 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1271 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
1272 status
= read_packet_header_begin_state(notit
);
1274 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
1275 status
= read_packet_header_continue_state(notit
);
1277 case STATE_AFTER_TRACE_PACKET_HEADER
:
1278 status
= after_packet_header_state(notit
);
1280 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
1281 status
= read_packet_context_begin_state(notit
);
1283 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
1284 status
= read_packet_context_continue_state(notit
);
1286 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
1287 status
= after_packet_context_state(notit
);
1289 case STATE_EMIT_NOTIF_NEW_PACKET
:
1290 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1292 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
:
1293 status
= read_event_header_begin_state(notit
);
1295 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
:
1296 status
= read_event_header_continue_state(notit
);
1298 case STATE_AFTER_STREAM_EVENT_HEADER
:
1299 status
= after_event_header_state(notit
);
1301 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
:
1302 status
= read_stream_event_context_begin_state(notit
);
1304 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
:
1305 status
= read_stream_event_context_continue_state(notit
);
1307 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN
:
1308 status
= read_event_context_begin_state(notit
);
1310 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
:
1311 status
= read_event_context_continue_state(notit
);
1313 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
1314 status
= read_event_payload_begin_state(notit
);
1316 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
1317 status
= read_event_payload_continue_state(notit
);
1319 case STATE_EMIT_NOTIF_EVENT
:
1320 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1322 case STATE_SKIP_PACKET_PADDING
:
1323 status
= skip_packet_padding_state(notit
);
1325 case STATE_EMIT_NOTIF_END_OF_PACKET
:
1326 notit
->state
= STATE_SKIP_PACKET_PADDING
;
1334 * Resets the internal state of a CTF notification iterator.
1337 void bt_ctf_notif_iter_reset(struct bt_ctf_notif_iter
*notit
)
1340 stack_clear(notit
->stack
);
1341 BT_PUT(notit
->meta
.stream_class
);
1342 BT_PUT(notit
->meta
.event_class
);
1343 BT_PUT(notit
->packet
);
1344 put_all_dscopes(notit
);
1345 notit
->buf
.addr
= NULL
;
1348 notit
->buf
.packet_offset
= 0;
1349 notit
->state
= STATE_INIT
;
1350 notit
->cur_content_size
= -1;
1351 notit
->cur_packet_size
= -1;
1355 int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter
*notit
)
1360 stack_clear(notit
->stack
);
1361 BT_PUT(notit
->meta
.stream_class
);
1362 BT_PUT(notit
->meta
.event_class
);
1363 BT_PUT(notit
->packet
);
1364 BT_PUT(notit
->cur_timestamp_end
);
1365 put_all_dscopes(notit
);
1368 * Adjust current buffer so that addr points to the beginning of the new
1371 if (notit
->buf
.addr
) {
1372 size_t consumed_bytes
= (size_t) (notit
->buf
.at
/ CHAR_BIT
);
1374 /* Packets are assumed to start on a byte frontier. */
1375 if (notit
->buf
.at
% CHAR_BIT
) {
1380 notit
->buf
.addr
+= consumed_bytes
;
1381 notit
->buf
.sz
-= consumed_bytes
;
1383 notit
->buf
.packet_offset
= 0;
1386 notit
->cur_content_size
= -1;
1387 notit
->cur_packet_size
= -1;
1388 notit
->cur_sc_field_path_cache
= NULL
;
1394 struct bt_ctf_field
*get_next_field(struct bt_ctf_notif_iter
*notit
)
1396 struct bt_ctf_field
*next_field
= NULL
;
1397 struct bt_ctf_field
*base_field
;
1398 struct bt_ctf_field_type
*base_type
;
1401 assert(!stack_empty(notit
->stack
));
1402 index
= stack_top(notit
->stack
)->index
;
1403 base_field
= stack_top(notit
->stack
)->base
;
1404 base_type
= bt_ctf_field_get_type(base_field
);
1406 PERR("Failed to get base field's type\n");
1410 switch (bt_ctf_field_type_get_type_id(base_type
)) {
1411 case BT_CTF_TYPE_ID_STRUCT
:
1412 next_field
= bt_ctf_field_structure_get_field_by_index(
1415 case BT_CTF_TYPE_ID_ARRAY
:
1416 next_field
= bt_ctf_field_array_get_field(base_field
, index
);
1418 case BT_CTF_TYPE_ID_SEQUENCE
:
1419 next_field
= bt_ctf_field_sequence_get_field(base_field
, index
);
1421 case BT_CTF_TYPE_ID_VARIANT
:
1422 next_field
= bt_ctf_field_variant_get_current_field(base_field
);
1436 void update_clock_state(uint64_t *state
,
1437 struct bt_ctf_field
*value_field
)
1439 struct bt_ctf_field_type
*value_type
= NULL
;
1440 uint64_t requested_new_value
;
1441 uint64_t requested_new_value_mask
;
1442 uint64_t cur_value_masked
;
1443 int requested_new_value_size
;
1446 value_type
= bt_ctf_field_get_type(value_field
);
1449 requested_new_value_size
=
1450 bt_ctf_field_type_integer_get_size(value_type
);
1451 assert(requested_new_value_size
> 0);
1453 ret
= bt_ctf_field_unsigned_integer_get_value(value_field
,
1454 &requested_new_value
);
1458 * Special case for a 64-bit new value, which is the limit
1459 * of a clock value as of this version: overwrite the
1460 * current value directly.
1462 if (requested_new_value_size
== 64) {
1463 *state
= requested_new_value
;
1467 requested_new_value_mask
= (1ULL << requested_new_value_size
) - 1;
1468 cur_value_masked
= *state
& requested_new_value_mask
;
1470 if (requested_new_value
< cur_value_masked
) {
1472 * It looks like a wrap happened on the number of bits
1473 * of the requested new value. Assume that the clock
1474 * value wrapped only one time.
1476 *state
+= requested_new_value_mask
+ 1;
1479 /* Clear the low bits of the current clock value. */
1480 *state
&= ~requested_new_value_mask
;
1482 /* Set the low bits of the current clock value. */
1483 *state
|= requested_new_value
;
1489 enum bt_ctf_btr_status
update_clock(struct bt_ctf_notif_iter
*notit
,
1490 struct bt_ctf_field_type
*int_field_type
,
1491 struct bt_ctf_field
*int_field
)
1493 gboolean clock_class_found
;
1494 uint64_t *clock_state
;
1495 enum bt_ctf_btr_status ret
= BT_CTF_BTR_STATUS_OK
;
1496 struct bt_ctf_clock_class
*clock_class
=
1497 bt_ctf_field_type_integer_get_mapped_clock_class(
1500 if (likely(!clock_class
)) {
1504 clock_class_found
= g_hash_table_lookup_extended(notit
->clock_states
,
1505 clock_class
, NULL
, (gpointer
) &clock_state
);
1506 if (unlikely(!clock_class_found
)) {
1507 const char *clock_class_name
=
1508 bt_ctf_clock_class_get_name(clock_class
);
1510 PERR("Unknown clock class %s mapped to integer encountered in stream\n",
1511 clock_class_name
? : "NULL");
1512 ret
= BT_CTF_BTR_STATUS_ERROR
;
1516 if (unlikely(!clock_state
)) {
1517 clock_state
= g_new0(uint64_t, 1);
1519 ret
= BT_CTF_BTR_STATUS_ENOMEM
;
1522 g_hash_table_insert(notit
->clock_states
, bt_get(clock_class
),
1526 /* Update the clock's state. */
1527 update_clock_state(clock_state
, int_field
);
1529 bt_put(clock_class
);
1535 enum bt_ctf_btr_status
btr_unsigned_int_common(uint64_t value
,
1536 struct bt_ctf_field_type
*type
, void *data
,
1537 struct bt_ctf_field
**_field
)
1539 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1540 struct bt_ctf_field
*field
= NULL
;
1541 struct bt_ctf_field
*int_field
= NULL
;
1542 struct bt_ctf_notif_iter
*notit
= data
;
1545 /* Create next field */
1546 field
= get_next_field(notit
);
1548 PERR("Failed to get next field (unsigned int)\n");
1549 status
= BT_CTF_BTR_STATUS_ERROR
;
1553 switch(bt_ctf_field_type_get_type_id(type
)) {
1554 case BT_CTF_TYPE_ID_INTEGER
:
1555 /* Integer field is created field */
1556 BT_MOVE(int_field
, field
);
1559 case BT_CTF_TYPE_ID_ENUM
:
1560 int_field
= bt_ctf_field_enumeration_get_container(field
);
1561 type
= bt_ctf_field_get_type(int_field
);
1570 PERR("Failed to get integer field\n");
1571 status
= BT_CTF_BTR_STATUS_ERROR
;
1575 ret
= bt_ctf_field_unsigned_integer_set_value(int_field
, value
);
1577 stack_top(notit
->stack
)->index
++;
1578 *_field
= int_field
;
1588 enum bt_ctf_btr_status
btr_timestamp_end_cb(void *value
,
1589 struct bt_ctf_field_type
*type
, void *data
)
1591 enum bt_ctf_btr_status status
;
1592 struct bt_ctf_field
*field
= NULL
;
1593 struct bt_ctf_notif_iter
*notit
= data
;
1595 status
= btr_unsigned_int_common(*((uint64_t *) value
), type
, data
,
1598 /* Set as the current packet's timestamp_end field. */
1599 BT_MOVE(notit
->cur_timestamp_end
, field
);
1604 enum bt_ctf_btr_status
btr_unsigned_int_cb(uint64_t value
,
1605 struct bt_ctf_field_type
*type
, void *data
)
1607 struct bt_ctf_notif_iter
*notit
= data
;
1608 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1609 struct bt_ctf_field
*field
= NULL
;
1610 struct field_cb_override
*override
;
1612 override
= g_hash_table_lookup(notit
->field_overrides
,
1614 if (unlikely(override
)) {
1615 status
= override
->func(&value
, type
, override
->data
);
1619 status
= btr_unsigned_int_common(value
, type
, data
, &field
);
1620 if (status
!= BT_CTF_BTR_STATUS_OK
) {
1624 status
= update_clock(notit
, type
, field
);
1631 enum bt_ctf_btr_status
btr_signed_int_cb(int64_t value
,
1632 struct bt_ctf_field_type
*type
, void *data
)
1634 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1635 struct bt_ctf_field
*field
= NULL
;
1636 struct bt_ctf_field
*int_field
= NULL
;
1637 struct bt_ctf_notif_iter
*notit
= data
;
1640 /* create next field */
1641 field
= get_next_field(notit
);
1643 PERR("Failed to get next field (signed int)\n");
1644 status
= BT_CTF_BTR_STATUS_ERROR
;
1648 switch(bt_ctf_field_type_get_type_id(type
)) {
1649 case BT_CTF_TYPE_ID_INTEGER
:
1650 /* Integer field is created field */
1651 BT_MOVE(int_field
, field
);
1654 case BT_CTF_TYPE_ID_ENUM
:
1655 int_field
= bt_ctf_field_enumeration_get_container(field
);
1656 type
= bt_ctf_field_get_type(int_field
);
1665 PERR("Failed to get integer field\n");
1666 status
= BT_CTF_BTR_STATUS_ERROR
;
1670 ret
= bt_ctf_field_signed_integer_set_value(int_field
, value
);
1672 stack_top(notit
->stack
)->index
++;
1673 status
= update_clock(notit
, type
, int_field
);
1683 enum bt_ctf_btr_status
btr_floating_point_cb(double value
,
1684 struct bt_ctf_field_type
*type
, void *data
)
1686 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1687 struct bt_ctf_field
*field
= NULL
;
1688 struct bt_ctf_notif_iter
*notit
= data
;
1691 /* Create next field */
1692 field
= get_next_field(notit
);
1694 PERR("Failed to get next field (floating point number)\n");
1695 status
= BT_CTF_BTR_STATUS_ERROR
;
1699 ret
= bt_ctf_field_floating_point_set_value(field
, value
);
1701 stack_top(notit
->stack
)->index
++;
1710 enum bt_ctf_btr_status
btr_string_begin_cb(
1711 struct bt_ctf_field_type
*type
, void *data
)
1713 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1714 struct bt_ctf_field
*field
= NULL
;
1715 struct bt_ctf_notif_iter
*notit
= data
;
1718 /* Create next field */
1719 field
= get_next_field(notit
);
1721 PERR("Failed to get next field (string)\n");
1722 status
= BT_CTF_BTR_STATUS_ERROR
;
1727 * Push on stack. Not a compound type per se, but we know that only
1728 * btr_string_cb() may be called between this call and a subsequent
1729 * call to btr_string_end_cb().
1731 ret
= stack_push(notit
->stack
, field
);
1733 PERR("Failed to push string field onto the stack\n");
1734 status
= BT_CTF_BTR_STATUS_ERROR
;
1739 * Initialize string field payload to an empty string since in the
1740 * case of a length 0 string the btr_string_cb won't be called and
1741 * we will end up with an unset string payload.
1743 ret
= bt_ctf_field_string_set_value(field
, "");
1745 PERR("Failed to initialize string field\n");
1746 status
= BT_CTF_BTR_STATUS_ERROR
;
1757 enum bt_ctf_btr_status
btr_string_cb(const char *value
,
1758 size_t len
, struct bt_ctf_field_type
*type
, void *data
)
1760 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1761 struct bt_ctf_field
*field
= NULL
;
1762 struct bt_ctf_notif_iter
*notit
= data
;
1765 /* Get string field */
1766 field
= stack_top(notit
->stack
)->base
;
1769 /* Append current string */
1770 ret
= bt_ctf_field_string_append_len(field
, value
, len
);
1772 PERR("Failed to append a string to a string field\n");
1773 status
= BT_CTF_BTR_STATUS_ERROR
;
1782 enum bt_ctf_btr_status
btr_string_end_cb(
1783 struct bt_ctf_field_type
*type
, void *data
)
1785 struct bt_ctf_notif_iter
*notit
= data
;
1787 /* Pop string field */
1788 stack_pop(notit
->stack
);
1790 /* Go to next field */
1791 stack_top(notit
->stack
)->index
++;
1793 return BT_CTF_BTR_STATUS_OK
;
1796 enum bt_ctf_btr_status
btr_compound_begin_cb(
1797 struct bt_ctf_field_type
*type
, void *data
)
1799 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1800 struct bt_ctf_notif_iter
*notit
= data
;
1801 struct bt_ctf_field
*field
;
1805 if (stack_empty(notit
->stack
)) {
1806 /* Root: create dynamic scope field */
1807 *notit
->cur_dscope_field
= bt_ctf_field_create(type
);
1808 field
= *notit
->cur_dscope_field
;
1811 * Field will be put at the end of this function
1812 * (stack_push() will take one reference, but this
1813 * reference is lost upon the equivalent stack_pop()
1814 * later), so also get it for our context to own it.
1816 bt_get(*notit
->cur_dscope_field
);
1818 field
= get_next_field(notit
);
1822 PERR("Failed to get next field or create dynamic scope field\n");
1823 status
= BT_CTF_BTR_STATUS_ERROR
;
1828 ret
= stack_push(notit
->stack
, field
);
1830 PERR("Failed to push compound field onto the stack\n");
1831 status
= BT_CTF_BTR_STATUS_ERROR
;
1841 enum bt_ctf_btr_status
btr_compound_end_cb(
1842 struct bt_ctf_field_type
*type
, void *data
)
1844 struct bt_ctf_notif_iter
*notit
= data
;
1846 assert(!stack_empty(notit
->stack
));
1849 stack_pop(notit
->stack
);
1851 /* If the stack is not empty, increment the base's index */
1852 if (!stack_empty(notit
->stack
)) {
1853 stack_top(notit
->stack
)->index
++;
1856 return BT_CTF_BTR_STATUS_OK
;
1860 struct bt_ctf_field
*resolve_field(struct bt_ctf_notif_iter
*notit
,
1861 struct bt_ctf_field_path
*path
)
1863 struct bt_ctf_field
*field
= NULL
;
1866 switch (bt_ctf_field_path_get_root_scope(path
)) {
1867 case BT_CTF_SCOPE_TRACE_PACKET_HEADER
:
1868 field
= notit
->dscopes
.trace_packet_header
;
1870 case BT_CTF_SCOPE_STREAM_PACKET_CONTEXT
:
1871 field
= notit
->dscopes
.stream_packet_context
;
1873 case BT_CTF_SCOPE_STREAM_EVENT_HEADER
:
1874 field
= notit
->dscopes
.stream_event_header
;
1876 case BT_CTF_SCOPE_STREAM_EVENT_CONTEXT
:
1877 field
= notit
->dscopes
.stream_event_context
;
1879 case BT_CTF_SCOPE_EVENT_CONTEXT
:
1880 field
= notit
->dscopes
.event_context
;
1882 case BT_CTF_SCOPE_EVENT_FIELDS
:
1883 field
= notit
->dscopes
.event_payload
;
1895 for (i
= 0; i
< bt_ctf_field_path_get_index_count(path
); ++i
) {
1896 struct bt_ctf_field
*next_field
= NULL
;
1897 struct bt_ctf_field_type
*field_type
;
1898 int index
= bt_ctf_field_path_get_index(path
, i
);
1900 field_type
= bt_ctf_field_get_type(field
);
1906 if (is_struct_type(field_type
)) {
1907 next_field
= bt_ctf_field_structure_get_field_by_index(
1909 } else if (is_variant_type(field_type
)) {
1911 bt_ctf_field_variant_get_current_field(field
);
1921 /* Move next field -> field */
1922 BT_MOVE(field
, next_field
);
1930 int64_t btr_get_sequence_length_cb(struct bt_ctf_field_type
*type
, void *data
)
1934 struct bt_ctf_field_path
*field_path
;
1935 struct bt_ctf_notif_iter
*notit
= data
;
1936 struct bt_ctf_field
*length_field
= NULL
;
1939 field_path
= bt_ctf_field_type_sequence_get_length_field_path(type
);
1944 length_field
= resolve_field(notit
, field_path
);
1945 if (!length_field
) {
1949 iret
= bt_ctf_field_unsigned_integer_get_value(length_field
, &length
);
1954 iret
= bt_ctf_field_sequence_set_length(stack_top(notit
->stack
)->base
,
1959 ret
= (int64_t) length
;
1962 BT_PUT(length_field
);
1969 struct bt_ctf_field_type
*btr_get_variant_type_cb(
1970 struct bt_ctf_field_type
*type
, void *data
)
1972 struct bt_ctf_field_path
*path
;
1973 struct bt_ctf_notif_iter
*notit
= data
;
1974 struct bt_ctf_field
*tag_field
= NULL
;
1975 struct bt_ctf_field
*selected_field
= NULL
;
1976 struct bt_ctf_field_type
*selected_field_type
= NULL
;
1978 path
= bt_ctf_field_type_variant_get_tag_field_path(type
);
1983 tag_field
= resolve_field(notit
, path
);
1989 * We found the enumeration tag field instance which should be
1990 * able to select a current field for this variant. This
1991 * callback function we're in is called _after_
1992 * compound_begin(), so the current stack top's base field is
1993 * the variant field in question. We get the selected field here
1994 * thanks to this tag field (thus creating the selected field),
1995 * which will also provide us with its type. Then, this field
1996 * will remain the current selected one until the next callback
1997 * function call which is used to fill the current selected
2000 selected_field
= bt_ctf_field_variant_get_field(
2001 stack_top(notit
->stack
)->base
, tag_field
);
2002 if (!selected_field
) {
2006 selected_field_type
= bt_ctf_field_get_type(selected_field
);
2010 BT_PUT(selected_field
);
2013 return selected_field_type
;
2017 int set_event_clocks(struct bt_ctf_event
*event
,
2018 struct bt_ctf_notif_iter
*notit
)
2021 GHashTableIter iter
;
2022 struct bt_ctf_clock_class
*clock_class
;
2023 uint64_t *clock_state
;
2025 g_hash_table_iter_init(&iter
, notit
->clock_states
);
2027 while (g_hash_table_iter_next(&iter
, (gpointer
) &clock_class
,
2028 (gpointer
) &clock_state
)) {
2029 struct bt_ctf_clock_value
*clock_value
;
2031 clock_value
= bt_ctf_clock_value_create(clock_class
,
2037 ret
= bt_ctf_event_set_clock_value(event
, clock_class
,
2039 bt_put(clock_value
);
2050 struct bt_ctf_event
*create_event(struct bt_ctf_notif_iter
*notit
)
2053 struct bt_ctf_event
*event
;
2055 /* Create event object. */
2056 event
= bt_ctf_event_create(notit
->meta
.event_class
);
2061 /* Set header, stream event context, context, and payload fields. */
2062 ret
= bt_ctf_event_set_header(event
,
2063 notit
->dscopes
.stream_event_header
);
2068 ret
= bt_ctf_event_set_stream_event_context(event
,
2069 notit
->dscopes
.stream_event_context
);
2074 ret
= bt_ctf_event_set_event_context(event
,
2075 notit
->dscopes
.event_context
);
2080 ret
= bt_ctf_event_set_payload_field(event
,
2081 notit
->dscopes
.event_payload
);
2086 ret
= set_event_clocks(event
, notit
);
2091 /* Associate with current packet. */
2092 assert(notit
->packet
);
2093 ret
= bt_ctf_event_set_packet(event
, notit
->packet
);
2106 void create_packet(struct bt_ctf_notif_iter
*notit
)
2109 struct bt_ctf_stream
*stream
= NULL
;
2110 struct bt_ctf_packet
*packet
= NULL
;
2112 /* Ask the user for the stream */
2113 stream
= notit
->medium
.medops
.get_stream(notit
->meta
.stream_class
,
2114 notit
->medium
.data
);
2120 packet
= bt_ctf_packet_create(stream
);
2125 /* Set packet's context and header fields */
2126 if (notit
->dscopes
.trace_packet_header
) {
2127 ret
= bt_ctf_packet_set_header(packet
,
2128 notit
->dscopes
.trace_packet_header
);
2134 if (notit
->dscopes
.stream_packet_context
) {
2135 ret
= bt_ctf_packet_set_context(packet
,
2136 notit
->dscopes
.stream_packet_context
);
2146 BT_MOVE(notit
->packet
, packet
);
2150 void notify_new_packet(struct bt_ctf_notif_iter
*notit
,
2151 struct bt_notification
**notification
)
2153 struct bt_notification
*ret
;
2155 /* Initialize the iterator's current packet */
2156 create_packet(notit
);
2157 if (!notit
->packet
) {
2161 ret
= bt_notification_packet_begin_create(notit
->packet
);
2165 *notification
= ret
;
2169 void notify_end_of_packet(struct bt_ctf_notif_iter
*notit
,
2170 struct bt_notification
**notification
)
2172 struct bt_notification
*ret
;
2174 if (!notit
->packet
) {
2178 ret
= bt_notification_packet_end_create(notit
->packet
);
2182 BT_PUT(notit
->packet
);
2183 *notification
= ret
;
2187 void notify_event(struct bt_ctf_notif_iter
*notit
,
2188 struct bt_notification
**notification
)
2190 struct bt_ctf_event
*event
;
2191 struct bt_notification
*ret
= NULL
;
2194 event
= create_event(notit
);
2199 ret
= bt_notification_event_create(event
);
2203 *notification
= ret
;
2210 void notify_eos(struct bt_ctf_notif_iter
*notit
,
2211 struct bt_notification
**notification
)
2213 struct bt_ctf_stream
*stream
= NULL
;
2214 struct bt_notification
*ret
= NULL
;
2216 /* Ask the user for the stream */
2217 stream
= notit
->medium
.medops
.get_stream(notit
->meta
.stream_class
,
2218 notit
->medium
.data
);
2223 ret
= bt_notification_stream_end_create(stream
);
2227 *notification
= ret
;
2233 int init_clock_states(GHashTable
*clock_states
, struct bt_ctf_trace
*trace
)
2235 int clock_class_count
, i
, ret
= 0;
2237 clock_class_count
= bt_ctf_trace_get_clock_class_count(trace
);
2238 if (clock_class_count
<= 0) {
2243 for (i
= 0; i
< clock_class_count
; i
++) {
2244 struct bt_ctf_clock_class
*clock_class
;
2246 clock_class
= bt_ctf_trace_get_clock_class(trace
, i
);
2252 g_hash_table_insert(clock_states
, bt_get(clock_class
), NULL
);
2253 bt_put(clock_class
);
2260 void init_trace_field_path_cache(struct bt_ctf_trace
*trace
,
2261 struct trace_field_path_cache
*trace_field_path_cache
)
2264 int stream_instance_id
= -1;
2266 struct bt_ctf_field_type
*packet_header
= NULL
;
2268 packet_header
= bt_ctf_trace_get_packet_header_type(trace
);
2269 if (!packet_header
) {
2273 if (!bt_ctf_field_type_is_structure(packet_header
)) {
2277 count
= bt_ctf_field_type_structure_get_field_count(packet_header
);
2282 for (i
= 0; (i
< count
&& (stream_id
== -1 || stream_instance_id
== -1)); i
++) {
2284 const char *field_name
;
2286 ret
= bt_ctf_field_type_structure_get_field(packet_header
,
2287 &field_name
, NULL
, i
);
2292 if (stream_id
== -1 && !strcmp(field_name
, "stream_id")) {
2294 } else if (stream_instance_id
== -1 &&
2295 !strcmp(field_name
, "stream_instance_id")) {
2296 stream_instance_id
= i
;
2300 trace_field_path_cache
->stream_id
= stream_id
;
2301 trace_field_path_cache
->stream_instance_id
= stream_instance_id
;
2302 BT_PUT(packet_header
);
2306 struct bt_ctf_notif_iter
*bt_ctf_notif_iter_create(struct bt_ctf_trace
*trace
,
2307 size_t max_request_sz
,
2308 struct bt_ctf_notif_iter_medium_ops medops
,
2309 void *data
, FILE *err_stream
)
2312 struct bt_ctf_notif_iter
*notit
= NULL
;
2313 struct bt_ctf_btr_cbs cbs
= {
2315 .signed_int
= btr_signed_int_cb
,
2316 .unsigned_int
= btr_unsigned_int_cb
,
2317 .floating_point
= btr_floating_point_cb
,
2318 .string_begin
= btr_string_begin_cb
,
2319 .string
= btr_string_cb
,
2320 .string_end
= btr_string_end_cb
,
2321 .compound_begin
= btr_compound_begin_cb
,
2322 .compound_end
= btr_compound_end_cb
,
2325 .get_sequence_length
= btr_get_sequence_length_cb
,
2326 .get_variant_type
= btr_get_variant_type_cb
,
2331 assert(medops
.request_bytes
);
2332 notit
= g_new0(struct bt_ctf_notif_iter
, 1);
2334 PERR("Failed to allocate memory for CTF notification iterator\n");
2337 notit
->clock_states
= g_hash_table_new_full(g_direct_hash
,
2338 g_direct_equal
, bt_put
, g_free
);
2339 if (!notit
->clock_states
) {
2340 PERR("Failed to create hash table\n");
2343 ret
= init_clock_states(notit
->clock_states
, trace
);
2345 PERR("Failed to initialize stream clock states\n");
2348 notit
->meta
.trace
= bt_get(trace
);
2349 notit
->medium
.medops
= medops
;
2350 notit
->medium
.max_request_sz
= max_request_sz
;
2351 notit
->medium
.data
= data
;
2352 notit
->err_stream
= err_stream
;
2353 notit
->stack
= stack_new(notit
);
2354 if (!notit
->stack
) {
2355 PERR("Failed to create stack\n");
2359 notit
->btr
= bt_ctf_btr_create(cbs
, notit
, err_stream
);
2361 PERR("Failed to create binary type reader\n");
2365 bt_ctf_notif_iter_reset(notit
);
2367 init_trace_field_path_cache(trace
, ¬it
->trace_field_path_cache
);
2368 notit
->sc_field_path_caches
= g_hash_table_new_full(g_direct_hash
,
2369 g_direct_equal
, bt_put
, g_free
);
2370 if (!notit
->sc_field_path_caches
) {
2371 PERR("Failed to create stream class field path caches\n");
2375 notit
->field_overrides
= g_hash_table_new_full(g_direct_hash
,
2376 g_direct_equal
, bt_put
, g_free
);
2377 if (!notit
->field_overrides
) {
2384 bt_ctf_notif_iter_destroy(notit
);
2389 void bt_ctf_notif_iter_destroy(struct bt_ctf_notif_iter
*notit
)
2391 BT_PUT(notit
->meta
.trace
);
2392 BT_PUT(notit
->meta
.stream_class
);
2393 BT_PUT(notit
->meta
.event_class
);
2394 BT_PUT(notit
->packet
);
2395 BT_PUT(notit
->cur_timestamp_end
);
2396 put_all_dscopes(notit
);
2399 stack_destroy(notit
->stack
);
2403 bt_ctf_btr_destroy(notit
->btr
);
2406 if (notit
->clock_states
) {
2407 g_hash_table_destroy(notit
->clock_states
);
2410 if (notit
->sc_field_path_caches
) {
2411 g_hash_table_destroy(notit
->sc_field_path_caches
);
2414 if (notit
->field_overrides
) {
2415 g_hash_table_destroy(notit
->field_overrides
);
2420 enum bt_ctf_notif_iter_status
bt_ctf_notif_iter_get_next_notification(
2421 struct bt_ctf_notif_iter
*notit
,
2422 struct bt_notification
**notification
)
2424 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
2427 assert(notification
);
2430 status
= handle_state(notit
);
2431 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
2432 if (status
== BT_CTF_NOTIF_ITER_STATUS_EOF
) {
2433 PDBG("Medium operation reported end of stream\n");
2435 PERR("Failed to handle state:\n");
2436 PERR("\tState: %d\n", notit
->state
);
2441 switch (notit
->state
) {
2442 case STATE_EMIT_NOTIF_NEW_PACKET
:
2443 PDBG("Emitting new packet notification\n");
2444 notify_new_packet(notit
, notification
);
2445 if (!*notification
) {
2446 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
2449 case STATE_EMIT_NOTIF_EVENT
:
2450 PDBG("Emitting event notification\n");
2451 notify_event(notit
, notification
);
2452 if (!*notification
) {
2453 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
2456 case STATE_EMIT_NOTIF_END_OF_PACKET
:
2457 /* Update clock with timestamp_end field. */
2458 if (notit
->cur_timestamp_end
) {
2459 enum bt_ctf_btr_status btr_status
;
2460 struct bt_ctf_field_type
*field_type
=
2461 bt_ctf_field_get_type(
2462 notit
->cur_timestamp_end
);
2464 btr_status
= update_clock(notit
, field_type
,
2465 notit
->cur_timestamp_end
);
2467 if (btr_status
!= BT_CTF_BTR_STATUS_OK
) {
2468 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
2473 PDBG("Emitting end of packet notification\n");
2474 notify_end_of_packet(notit
, notification
);
2475 if (!*notification
) {
2476 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
2480 /* Non-emitting state: continue */