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 /* Clock class priority map (owned by this) */
166 struct bt_clock_class_priority_map
*cc_prio_map
;
168 /* Current packet (NULL if not created yet) */
169 struct bt_ctf_packet
*packet
;
172 * Current timestamp_end field (to consider before switching packets).
174 struct bt_ctf_field
*cur_timestamp_end
;
176 /* Database of current dynamic scopes (owned by this) */
178 struct bt_ctf_field
*trace_packet_header
;
179 struct bt_ctf_field
*stream_packet_context
;
180 struct bt_ctf_field
*stream_event_header
;
181 struct bt_ctf_field
*stream_event_context
;
182 struct bt_ctf_field
*event_context
;
183 struct bt_ctf_field
*event_payload
;
187 * Special field overrides.
189 * Overrides are used to implement the behaviours of special fields such
190 * as "timestamp_end" (which must be ignored until the end of the
191 * packet), "id" (event id) which can be present multiple times and must
192 * be updated multiple time.
194 * This should be used to implement the behaviour of integer fields
195 * mapped to clocks and other "tagged" fields (in CTF 2).
197 * bt_ctf_field_type to struct field_cb_override
199 GHashTable
*field_overrides
;
204 /* Current medium buffer data */
206 /* Last address provided by medium */
209 /* Buffer size provided by medium (bytes) */
212 /* Offset within whole packet of addr (bits) */
213 size_t packet_offset
;
215 /* Current position from addr (bits) */
219 /* Binary type reader */
220 struct bt_ctf_btr
*btr
;
222 /* Current medium data */
224 struct bt_ctf_notif_iter_medium_ops medops
;
225 size_t max_request_sz
;
229 /* Current packet size (bits) (-1 if unknown) */
230 int64_t cur_packet_size
;
232 /* Current content size (bits) (-1 if unknown) */
233 int64_t cur_content_size
;
235 /* bt_ctf_clock_class to uint64_t. */
236 GHashTable
*clock_states
;
239 * Cache of the trace-constant field paths (event header type)
240 * associated to the current trace.
242 struct trace_field_path_cache trace_field_path_cache
;
245 * Field path cache associated with the current stream class.
246 * Ownership of this structure belongs to the field_path_caches HT.
248 struct stream_class_field_path_cache
*cur_sc_field_path_cache
;
250 /* bt_ctf_stream_class to struct stream_class_field_path_cache. */
251 GHashTable
*sc_field_path_caches
;
255 int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter
*notit
);
258 enum bt_ctf_btr_status
btr_timestamp_end_cb(void *value
,
259 struct bt_ctf_field_type
*type
, void *data
);
262 void stack_entry_free_func(gpointer data
)
264 struct stack_entry
*entry
= data
;
271 struct stack
*stack_new(struct bt_ctf_notif_iter
*notit
)
273 struct stack
*stack
= NULL
;
275 stack
= g_new0(struct stack
, 1);
280 stack
->entries
= g_ptr_array_new_with_free_func(stack_entry_free_func
);
281 if (!stack
->entries
) {
292 void stack_destroy(struct stack
*stack
)
295 g_ptr_array_free(stack
->entries
, TRUE
);
300 int stack_push(struct stack
*stack
, struct bt_ctf_field
*base
)
303 struct stack_entry
*entry
;
307 entry
= g_new0(struct stack_entry
, 1);
313 entry
->base
= bt_get(base
);
314 g_ptr_array_add(stack
->entries
, entry
);
321 unsigned int stack_size(struct stack
*stack
)
325 return stack
->entries
->len
;
329 void stack_pop(struct stack
*stack
)
332 assert(stack_size(stack
));
333 g_ptr_array_remove_index(stack
->entries
, stack
->entries
->len
- 1);
337 struct stack_entry
*stack_top(struct stack
*stack
)
340 assert(stack_size(stack
));
342 return g_ptr_array_index(stack
->entries
, stack
->entries
->len
- 1);
346 bool stack_empty(struct stack
*stack
)
348 return stack_size(stack
) == 0;
352 void stack_clear(struct stack
*stack
)
356 if (!stack_empty(stack
)) {
357 g_ptr_array_remove_range(stack
->entries
, 0, stack_size(stack
));
360 assert(stack_empty(stack
));
364 enum bt_ctf_notif_iter_status
notif_iter_status_from_m_status(
365 enum bt_ctf_notif_iter_medium_status m_status
)
371 size_t buf_size_bits(struct bt_ctf_notif_iter
*notit
)
373 return BYTES_TO_BITS(notit
->buf
.sz
);
377 size_t buf_available_bits(struct bt_ctf_notif_iter
*notit
)
379 return buf_size_bits(notit
) - notit
->buf
.at
;
383 size_t packet_at(struct bt_ctf_notif_iter
*notit
)
385 return notit
->buf
.packet_offset
+ notit
->buf
.at
;
389 size_t remaining_content_bits(struct bt_ctf_notif_iter
*notit
)
391 if (notit
->cur_content_size
== -1) {
395 return notit
->cur_content_size
- packet_at(notit
);
399 size_t remaining_packet_bits(struct bt_ctf_notif_iter
*notit
)
401 if (notit
->cur_packet_size
== -1) {
405 return notit
->cur_packet_size
- packet_at(notit
);
409 void buf_consume_bits(struct bt_ctf_notif_iter
*notit
, size_t incr
)
411 notit
->buf
.at
+= incr
;
415 bool buf_has_enough_bits(struct bt_ctf_notif_iter
*notit
, size_t sz
)
417 return buf_available_bits(notit
) >= sz
;
421 enum bt_ctf_notif_iter_status
request_medium_bytes(
422 struct bt_ctf_notif_iter
*notit
)
424 uint8_t *buffer_addr
;
426 enum bt_ctf_notif_iter_medium_status m_status
;
428 m_status
= notit
->medium
.medops
.request_bytes(
429 notit
->medium
.max_request_sz
, &buffer_addr
,
430 &buffer_sz
, notit
->medium
.data
);
431 if (m_status
== BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK
) {
432 assert(buffer_sz
!= 0);
434 /* New packet offset is old one + old size (in bits) */
435 notit
->buf
.packet_offset
+= buf_size_bits(notit
);
437 /* Restart at the beginning of the new medium buffer */
440 /* New medium buffer size */
441 notit
->buf
.sz
= buffer_sz
;
443 /* New medium buffer address */
444 notit
->buf
.addr
= buffer_addr
;
447 return notif_iter_status_from_m_status(m_status
);
451 enum bt_ctf_notif_iter_status
buf_ensure_available_bits(
452 struct bt_ctf_notif_iter
*notit
)
454 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
456 if (buf_available_bits(notit
) == 0) {
458 * This _cannot_ return BT_CTF_NOTIF_ITER_STATUS_OK
461 status
= request_medium_bytes(notit
);
468 enum bt_ctf_notif_iter_status
read_dscope_begin_state(
469 struct bt_ctf_notif_iter
*notit
,
470 struct bt_ctf_field_type
*dscope_field_type
,
471 enum state done_state
, enum state continue_state
,
472 struct bt_ctf_field
**dscope_field
)
474 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
475 enum bt_ctf_btr_status btr_status
;
476 size_t consumed_bits
;
478 status
= buf_ensure_available_bits(notit
);
479 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
483 bt_put(*dscope_field
);
484 notit
->cur_dscope_field
= dscope_field
;
485 consumed_bits
= bt_ctf_btr_start(notit
->btr
, dscope_field_type
,
486 notit
->buf
.addr
, notit
->buf
.at
, packet_at(notit
),
487 notit
->buf
.sz
, &btr_status
);
489 switch (btr_status
) {
490 case BT_CTF_BTR_STATUS_OK
:
491 /* type was read completely */
492 notit
->state
= done_state
;
494 case BT_CTF_BTR_STATUS_EOF
:
495 notit
->state
= continue_state
;
498 PERR("Binary type reader failed to start\n");
499 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
503 /* Consume bits now since we know we're not in an error state */
504 buf_consume_bits(notit
, consumed_bits
);
511 enum bt_ctf_notif_iter_status
read_dscope_continue_state(
512 struct bt_ctf_notif_iter
*notit
, enum state done_state
)
514 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
515 enum bt_ctf_btr_status btr_status
;
516 size_t consumed_bits
;
518 status
= buf_ensure_available_bits(notit
);
519 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
523 consumed_bits
= bt_ctf_btr_continue(notit
->btr
, notit
->buf
.addr
,
524 notit
->buf
.sz
, &btr_status
);
526 switch (btr_status
) {
527 case BT_CTF_BTR_STATUS_OK
:
528 /* Type was read completely. */
529 notit
->state
= done_state
;
531 case BT_CTF_BTR_STATUS_EOF
:
532 /* Stay in this continue state. */
535 PERR("Binary type reader failed to continue\n");
536 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
540 /* Consume bits now since we know we're not in an error state. */
541 buf_consume_bits(notit
, consumed_bits
);
547 void put_event_dscopes(struct bt_ctf_notif_iter
*notit
)
549 BT_PUT(notit
->dscopes
.stream_event_header
);
550 BT_PUT(notit
->dscopes
.stream_event_context
);
551 BT_PUT(notit
->dscopes
.event_context
);
552 BT_PUT(notit
->dscopes
.event_payload
);
556 void put_all_dscopes(struct bt_ctf_notif_iter
*notit
)
558 BT_PUT(notit
->dscopes
.trace_packet_header
);
559 BT_PUT(notit
->dscopes
.stream_packet_context
);
560 put_event_dscopes(notit
);
564 enum bt_ctf_notif_iter_status
read_packet_header_begin_state(
565 struct bt_ctf_notif_iter
*notit
)
567 struct bt_ctf_field_type
*packet_header_type
= NULL
;
568 enum bt_ctf_notif_iter_status ret
= BT_CTF_NOTIF_ITER_STATUS_OK
;
570 if (bt_ctf_notif_iter_switch_packet(notit
)) {
571 ret
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
575 /* Packet header type is common to the whole trace. */
576 packet_header_type
= bt_ctf_trace_get_packet_header_type(
578 if (!packet_header_type
) {
579 notit
->state
= STATE_AFTER_TRACE_PACKET_HEADER
;
583 ret
= read_dscope_begin_state(notit
, packet_header_type
,
584 STATE_AFTER_TRACE_PACKET_HEADER
,
585 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
586 ¬it
->dscopes
.trace_packet_header
);
588 BT_PUT(packet_header_type
);
593 enum bt_ctf_notif_iter_status
read_packet_header_continue_state(
594 struct bt_ctf_notif_iter
*notit
)
596 return read_dscope_continue_state(notit
,
597 STATE_AFTER_TRACE_PACKET_HEADER
);
601 bool is_struct_type(struct bt_ctf_field_type
*field_type
)
603 return bt_ctf_field_type_get_type_id(field_type
) ==
604 BT_CTF_FIELD_TYPE_ID_STRUCT
;
608 bool is_variant_type(struct bt_ctf_field_type
*field_type
)
610 return bt_ctf_field_type_get_type_id(field_type
) ==
611 BT_CTF_FIELD_TYPE_ID_VARIANT
;
615 struct stream_class_field_path_cache
*
616 create_stream_class_field_path_cache_entry(
617 struct bt_ctf_notif_iter
*notit
,
618 struct bt_ctf_stream_class
*stream_class
)
622 int timestamp_end
= -1;
623 int packet_size
= -1;
624 int content_size
= -1;
625 struct stream_class_field_path_cache
*cache_entry
= g_new0(
626 struct stream_class_field_path_cache
, 1);
627 struct bt_ctf_field_type
*event_header
= NULL
, *packet_context
= NULL
;
633 event_header
= bt_ctf_stream_class_get_event_header_type(stream_class
);
634 if (event_header
&& bt_ctf_field_type_is_structure(event_header
)) {
637 count
= bt_ctf_field_type_structure_get_field_count(
642 for (i
= 0; i
< count
; i
++) {
646 ret
= bt_ctf_field_type_structure_get_field(
647 event_header
, &name
, NULL
, i
);
652 if (v
!= -1 && id
!= -1) {
655 if (v
== -1 && !strcmp(name
, "v")) {
657 } else if (id
== -1 && !strcmp(name
, "id")) {
663 packet_context
= bt_ctf_stream_class_get_packet_context_type(
665 if (packet_context
&& bt_ctf_field_type_is_structure(packet_context
)) {
668 count
= bt_ctf_field_type_structure_get_field_count(
673 for (i
= 0; i
< count
; i
++) {
676 struct bt_ctf_field_type
*field_type
;
678 if (timestamp_end
!= -1 && packet_size
!= -1 &&
679 content_size
!= -1) {
683 ret
= bt_ctf_field_type_structure_get_field(
684 packet_context
, &name
, &field_type
, i
);
689 if (timestamp_end
== -1 &&
690 !strcmp(name
, "timestamp_end")) {
691 struct field_cb_override
*override
= g_new0(
692 struct field_cb_override
, 1);
699 override
->func
= btr_timestamp_end_cb
;
700 override
->data
= notit
;
702 g_hash_table_insert(notit
->field_overrides
,
703 bt_get(field_type
), override
);
706 } else if (packet_size
== -1 &&
707 !strcmp(name
, "packet_size")) {
709 } else if (content_size
== -1 &&
710 !strcmp(name
, "content_size")) {
718 cache_entry
->id
= id
;
719 cache_entry
->timestamp_end
= timestamp_end
;
720 cache_entry
->packet_size
= packet_size
;
721 cache_entry
->content_size
= content_size
;
723 BT_PUT(event_header
);
724 BT_PUT(packet_context
);
733 struct stream_class_field_path_cache
*get_stream_class_field_path_cache(
734 struct bt_ctf_notif_iter
*notit
,
735 struct bt_ctf_stream_class
*stream_class
)
737 bool cache_entry_found
;
738 struct stream_class_field_path_cache
*cache_entry
;
740 cache_entry_found
= g_hash_table_lookup_extended(
741 notit
->sc_field_path_caches
,
742 stream_class
, NULL
, (gpointer
) &cache_entry
);
743 if (unlikely(!cache_entry_found
)) {
744 cache_entry
= create_stream_class_field_path_cache_entry(notit
,
746 g_hash_table_insert(notit
->sc_field_path_caches
,
747 bt_get(stream_class
), (gpointer
) cache_entry
);
754 enum bt_ctf_notif_iter_status
set_current_stream_class(
755 struct bt_ctf_notif_iter
*notit
)
757 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
758 struct bt_ctf_field_type
*packet_header_type
= NULL
;
759 struct bt_ctf_field_type
*stream_id_field_type
= NULL
;
762 /* Clear the current stream class field path cache. */
763 notit
->cur_sc_field_path_cache
= NULL
;
765 /* Is there any "stream_id" field in the packet header? */
766 packet_header_type
= bt_ctf_trace_get_packet_header_type(
768 if (!packet_header_type
) {
769 PERR("Failed to retrieve trace's packet header type\n");
770 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
774 assert(is_struct_type(packet_header_type
));
777 stream_id_field_type
=
778 bt_ctf_field_type_structure_get_field_type_by_name(
779 packet_header_type
, "stream_id");
780 if (stream_id_field_type
) {
781 /* Find appropriate stream class using current stream ID */
783 struct bt_ctf_field
*stream_id_field
= NULL
;
785 assert(notit
->dscopes
.trace_packet_header
);
788 stream_id_field
= bt_ctf_field_structure_get_field(
789 notit
->dscopes
.trace_packet_header
, "stream_id");
790 assert(stream_id_field
);
791 ret
= bt_ctf_field_unsigned_integer_get_value(
792 stream_id_field
, &stream_id
);
794 BT_PUT(stream_id_field
);
796 /* Only one stream: pick the first stream class */
797 assert(bt_ctf_trace_get_stream_class_count(
798 notit
->meta
.trace
) == 1);
802 BT_PUT(notit
->meta
.stream_class
);
803 notit
->meta
.stream_class
= bt_ctf_trace_get_stream_class_by_id(
804 notit
->meta
.trace
, stream_id
);
805 if (!notit
->meta
.stream_class
) {
806 PERR("Cannot find stream class with ID %" PRIu64
"\n",
808 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
813 * Retrieve (or lazily create) the current stream class field path
816 notit
->cur_sc_field_path_cache
= get_stream_class_field_path_cache(
817 notit
, notit
->meta
.stream_class
);
818 if (!notit
->cur_sc_field_path_cache
) {
819 PERR("Failed to retrieve stream class field path cache\n");
820 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
824 BT_PUT(packet_header_type
);
825 BT_PUT(stream_id_field_type
);
831 enum bt_ctf_notif_iter_status
after_packet_header_state(
832 struct bt_ctf_notif_iter
*notit
)
834 enum bt_ctf_notif_iter_status status
;
836 status
= set_current_stream_class(notit
);
837 if (status
== BT_CTF_NOTIF_ITER_STATUS_OK
) {
838 notit
->state
= STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
;
845 enum bt_ctf_notif_iter_status
read_packet_context_begin_state(
846 struct bt_ctf_notif_iter
*notit
)
848 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
849 struct bt_ctf_field_type
*packet_context_type
;
851 assert(notit
->meta
.stream_class
);
852 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(
853 notit
->meta
.stream_class
);
854 if (!packet_context_type
) {
855 notit
->state
= STATE_AFTER_STREAM_PACKET_CONTEXT
;
859 status
= read_dscope_begin_state(notit
, packet_context_type
,
860 STATE_AFTER_STREAM_PACKET_CONTEXT
,
861 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
862 ¬it
->dscopes
.stream_packet_context
);
865 BT_PUT(packet_context_type
);
870 enum bt_ctf_notif_iter_status
read_packet_context_continue_state(
871 struct bt_ctf_notif_iter
*notit
)
873 return read_dscope_continue_state(notit
,
874 STATE_AFTER_STREAM_PACKET_CONTEXT
);
878 enum bt_ctf_notif_iter_status
set_current_packet_content_sizes(
879 struct bt_ctf_notif_iter
*notit
)
881 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
882 struct bt_ctf_field
*packet_size_field
= NULL
;
883 struct bt_ctf_field
*content_size_field
= NULL
;
884 uint64_t content_size
= -1, packet_size
= -1;
886 assert(notit
->dscopes
.stream_packet_context
);
888 packet_size_field
= bt_ctf_field_structure_get_field(
889 notit
->dscopes
.stream_packet_context
, "packet_size");
890 content_size_field
= bt_ctf_field_structure_get_field(
891 notit
->dscopes
.stream_packet_context
, "content_size");
892 if (packet_size_field
) {
893 int ret
= bt_ctf_field_unsigned_integer_get_value(
894 packet_size_field
, &packet_size
);
897 if (packet_size
== 0) {
898 PERR("Decoded packet size is 0\n");
899 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
901 } else if ((packet_size
% 8) != 0) {
902 PERR("Decoded packet size is not a multiple of 8\n");
903 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
908 if (content_size_field
) {
909 int ret
= bt_ctf_field_unsigned_integer_get_value(
910 content_size_field
, &content_size
);
914 content_size
= packet_size
;
917 notit
->cur_packet_size
= packet_size
;
918 notit
->cur_content_size
= content_size
;
920 BT_PUT(packet_size_field
);
921 BT_PUT(content_size_field
);
926 enum bt_ctf_notif_iter_status
after_packet_context_state(
927 struct bt_ctf_notif_iter
*notit
)
929 enum bt_ctf_notif_iter_status status
;
931 status
= set_current_packet_content_sizes(notit
);
932 if (status
== BT_CTF_NOTIF_ITER_STATUS_OK
) {
933 notit
->state
= STATE_EMIT_NOTIF_NEW_PACKET
;
940 enum bt_ctf_notif_iter_status
read_event_header_begin_state(
941 struct bt_ctf_notif_iter
*notit
)
943 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
944 struct bt_ctf_field_type
*event_header_type
= NULL
;
946 /* Check if we have some content left */
947 if (notit
->cur_content_size
>= 0) {
948 if (packet_at(notit
) == notit
->cur_content_size
) {
949 /* No more events! */
950 notit
->state
= STATE_EMIT_NOTIF_END_OF_PACKET
;
952 } else if (packet_at(notit
) > notit
->cur_content_size
) {
953 /* That's not supposed to happen */
954 PERR("Cursor passed packet's content size:\n");
955 PERR("\tDecoded content size: %zu\n",
956 notit
->cur_content_size
);
957 PERR("\tCursor position: %zu\n", packet_at(notit
));
958 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
963 event_header_type
= bt_ctf_stream_class_get_event_header_type(
964 notit
->meta
.stream_class
);
965 if (!event_header_type
) {
966 notit
->state
= STATE_AFTER_STREAM_EVENT_HEADER
;
970 put_event_dscopes(notit
);
971 status
= read_dscope_begin_state(notit
, event_header_type
,
972 STATE_AFTER_STREAM_EVENT_HEADER
,
973 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
,
974 ¬it
->dscopes
.stream_event_header
);
976 BT_PUT(event_header_type
);
982 enum bt_ctf_notif_iter_status
read_event_header_continue_state(
983 struct bt_ctf_notif_iter
*notit
)
985 return read_dscope_continue_state(notit
,
986 STATE_AFTER_STREAM_EVENT_HEADER
);
990 enum bt_ctf_notif_iter_status
set_current_event_class(struct bt_ctf_notif_iter
*notit
)
993 * The assert() calls in this function are okay because it is
994 * assumed here that all the metadata objects have been
995 * validated for CTF correctness before decoding actual streams.
998 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
999 struct bt_ctf_field_type
*event_header_type
;
1000 struct bt_ctf_field_type
*id_field_type
= NULL
;
1001 struct bt_ctf_field_type
*v_field_type
= NULL
;
1002 uint64_t event_id
= -1ULL;
1005 event_header_type
= bt_ctf_stream_class_get_event_header_type(
1006 notit
->meta
.stream_class
);
1007 if (!event_header_type
) {
1008 PERR("Failed to retrieve stream class's event header type\n");
1009 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1013 /* Is there any "id"/"v" field in the event header? */
1014 assert(is_struct_type(event_header_type
));
1015 id_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1016 event_header_type
, "id");
1017 v_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1018 event_header_type
, "v");
1019 assert(notit
->dscopes
.stream_event_header
);
1023 * | | |_ _|_ _| __ __ _
1024 * | | | | | || '_ \ / _` |
1025 * | |___| | | || | | | (_| | S P E C I A L
1026 * |_____|_| |_||_| |_|\__, | C A S E â„¢
1029 struct bt_ctf_field
*v_field
= NULL
;
1030 struct bt_ctf_field
*v_struct_field
= NULL
;
1031 struct bt_ctf_field
*v_struct_id_field
= NULL
;
1033 // TODO: optimalize!
1034 v_field
= bt_ctf_field_structure_get_field(
1035 notit
->dscopes
.stream_event_header
, "v");
1039 bt_ctf_field_variant_get_current_field(v_field
);
1040 if (!v_struct_field
) {
1041 goto end_v_field_type
;
1044 // TODO: optimalize!
1046 bt_ctf_field_structure_get_field(v_struct_field
, "id");
1047 if (!v_struct_id_field
) {
1048 goto end_v_field_type
;
1051 ret
= bt_ctf_field_unsigned_integer_get_value(
1052 v_struct_id_field
, &event_id
);
1059 BT_PUT(v_struct_field
);
1060 BT_PUT(v_struct_id_field
);
1063 if (id_field_type
&& event_id
== -1ULL) {
1064 /* Check "id" field */
1065 struct bt_ctf_field
*id_field
= NULL
;
1068 // TODO: optimalize!
1069 id_field
= bt_ctf_field_structure_get_field(
1070 notit
->dscopes
.stream_event_header
, "id");
1072 assert(bt_ctf_field_is_integer(id_field
) ||
1073 bt_ctf_field_is_enumeration(id_field
));
1075 if (bt_ctf_field_is_integer(id_field
)) {
1076 ret
= bt_ctf_field_unsigned_integer_get_value(
1077 id_field
, &event_id
);
1079 struct bt_ctf_field
*container
;
1081 container
= bt_ctf_field_enumeration_get_container(
1084 ret
= bt_ctf_field_unsigned_integer_get_value(
1085 container
, &event_id
);
1092 if (event_id
== -1ULL) {
1093 /* Event ID not found: single event? */
1094 assert(bt_ctf_stream_class_get_event_class_count(
1095 notit
->meta
.stream_class
) == 1);
1099 BT_PUT(notit
->meta
.event_class
);
1100 notit
->meta
.event_class
= bt_ctf_stream_class_get_event_class_by_id(
1101 notit
->meta
.stream_class
, event_id
);
1102 if (!notit
->meta
.event_class
) {
1103 PERR("Cannot find event class with ID %" PRIu64
"\n", event_id
);
1104 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1109 BT_PUT(event_header_type
);
1110 BT_PUT(id_field_type
);
1111 BT_PUT(v_field_type
);
1117 enum bt_ctf_notif_iter_status
after_event_header_state(
1118 struct bt_ctf_notif_iter
*notit
)
1120 enum bt_ctf_notif_iter_status status
;
1122 status
= set_current_event_class(notit
);
1123 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
1124 PERR("Failed to set current event class\n");
1128 notit
->state
= STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
;
1135 enum bt_ctf_notif_iter_status
read_stream_event_context_begin_state(
1136 struct bt_ctf_notif_iter
*notit
)
1138 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1139 struct bt_ctf_field_type
*stream_event_context_type
;
1141 stream_event_context_type
= bt_ctf_stream_class_get_event_context_type(
1142 notit
->meta
.stream_class
);
1143 if (!stream_event_context_type
) {
1144 notit
->state
= STATE_DSCOPE_EVENT_CONTEXT_BEGIN
;
1148 status
= read_dscope_begin_state(notit
, stream_event_context_type
,
1149 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
,
1150 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
,
1151 ¬it
->dscopes
.stream_event_context
);
1154 BT_PUT(stream_event_context_type
);
1160 enum bt_ctf_notif_iter_status
read_stream_event_context_continue_state(
1161 struct bt_ctf_notif_iter
*notit
)
1163 return read_dscope_continue_state(notit
,
1164 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
);
1168 enum bt_ctf_notif_iter_status
read_event_context_begin_state(
1169 struct bt_ctf_notif_iter
*notit
)
1171 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1172 struct bt_ctf_field_type
*event_context_type
;
1174 event_context_type
= bt_ctf_event_class_get_context_type(
1175 notit
->meta
.event_class
);
1176 if (!event_context_type
) {
1177 notit
->state
= STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
;
1180 status
= read_dscope_begin_state(notit
, event_context_type
,
1181 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
1182 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
,
1183 ¬it
->dscopes
.event_context
);
1186 BT_PUT(event_context_type
);
1192 enum bt_ctf_notif_iter_status
read_event_context_continue_state(
1193 struct bt_ctf_notif_iter
*notit
)
1195 return read_dscope_continue_state(notit
,
1196 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
);
1200 enum bt_ctf_notif_iter_status
read_event_payload_begin_state(
1201 struct bt_ctf_notif_iter
*notit
)
1203 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1204 struct bt_ctf_field_type
*event_payload_type
;
1206 event_payload_type
= bt_ctf_event_class_get_payload_type(
1207 notit
->meta
.event_class
);
1208 if (!event_payload_type
) {
1209 notit
->state
= STATE_EMIT_NOTIF_EVENT
;
1213 status
= read_dscope_begin_state(notit
, event_payload_type
,
1214 STATE_EMIT_NOTIF_EVENT
,
1215 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
1216 ¬it
->dscopes
.event_payload
);
1219 BT_PUT(event_payload_type
);
1225 enum bt_ctf_notif_iter_status
read_event_payload_continue_state(
1226 struct bt_ctf_notif_iter
*notit
)
1228 return read_dscope_continue_state(notit
, STATE_EMIT_NOTIF_EVENT
);
1232 enum bt_ctf_notif_iter_status
skip_packet_padding_state(
1233 struct bt_ctf_notif_iter
*notit
)
1235 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1236 size_t bits_to_skip
;
1238 assert(notit
->cur_packet_size
> 0);
1239 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1240 if (bits_to_skip
== 0) {
1241 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1244 size_t bits_to_consume
;
1245 status
= buf_ensure_available_bits(notit
);
1246 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
1250 bits_to_consume
= MIN(buf_available_bits(notit
), bits_to_skip
);
1251 buf_consume_bits(notit
, bits_to_consume
);
1252 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1253 if (bits_to_skip
== 0) {
1254 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1264 enum bt_ctf_notif_iter_status
handle_state(struct bt_ctf_notif_iter
*notit
)
1266 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1268 PDBG("Handling state %d\n", notit
->state
);
1270 // TODO: optimalize!
1271 switch (notit
->state
) {
1273 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1275 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
1276 status
= read_packet_header_begin_state(notit
);
1278 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
1279 status
= read_packet_header_continue_state(notit
);
1281 case STATE_AFTER_TRACE_PACKET_HEADER
:
1282 status
= after_packet_header_state(notit
);
1284 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
1285 status
= read_packet_context_begin_state(notit
);
1287 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
1288 status
= read_packet_context_continue_state(notit
);
1290 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
1291 status
= after_packet_context_state(notit
);
1293 case STATE_EMIT_NOTIF_NEW_PACKET
:
1294 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1296 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
:
1297 status
= read_event_header_begin_state(notit
);
1299 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
:
1300 status
= read_event_header_continue_state(notit
);
1302 case STATE_AFTER_STREAM_EVENT_HEADER
:
1303 status
= after_event_header_state(notit
);
1305 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
:
1306 status
= read_stream_event_context_begin_state(notit
);
1308 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
:
1309 status
= read_stream_event_context_continue_state(notit
);
1311 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN
:
1312 status
= read_event_context_begin_state(notit
);
1314 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
:
1315 status
= read_event_context_continue_state(notit
);
1317 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
1318 status
= read_event_payload_begin_state(notit
);
1320 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
1321 status
= read_event_payload_continue_state(notit
);
1323 case STATE_EMIT_NOTIF_EVENT
:
1324 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1326 case STATE_SKIP_PACKET_PADDING
:
1327 status
= skip_packet_padding_state(notit
);
1329 case STATE_EMIT_NOTIF_END_OF_PACKET
:
1330 notit
->state
= STATE_SKIP_PACKET_PADDING
;
1338 * Resets the internal state of a CTF notification iterator.
1341 void bt_ctf_notif_iter_reset(struct bt_ctf_notif_iter
*notit
)
1344 stack_clear(notit
->stack
);
1345 BT_PUT(notit
->meta
.stream_class
);
1346 BT_PUT(notit
->meta
.event_class
);
1347 BT_PUT(notit
->packet
);
1348 put_all_dscopes(notit
);
1349 notit
->buf
.addr
= NULL
;
1352 notit
->buf
.packet_offset
= 0;
1353 notit
->state
= STATE_INIT
;
1354 notit
->cur_content_size
= -1;
1355 notit
->cur_packet_size
= -1;
1359 int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter
*notit
)
1364 stack_clear(notit
->stack
);
1365 BT_PUT(notit
->meta
.stream_class
);
1366 BT_PUT(notit
->meta
.event_class
);
1367 BT_PUT(notit
->packet
);
1368 BT_PUT(notit
->cur_timestamp_end
);
1369 put_all_dscopes(notit
);
1372 * Adjust current buffer so that addr points to the beginning of the new
1375 if (notit
->buf
.addr
) {
1376 size_t consumed_bytes
= (size_t) (notit
->buf
.at
/ CHAR_BIT
);
1378 /* Packets are assumed to start on a byte frontier. */
1379 if (notit
->buf
.at
% CHAR_BIT
) {
1384 notit
->buf
.addr
+= consumed_bytes
;
1385 notit
->buf
.sz
-= consumed_bytes
;
1387 notit
->buf
.packet_offset
= 0;
1390 notit
->cur_content_size
= -1;
1391 notit
->cur_packet_size
= -1;
1392 notit
->cur_sc_field_path_cache
= NULL
;
1398 struct bt_ctf_field
*get_next_field(struct bt_ctf_notif_iter
*notit
)
1400 struct bt_ctf_field
*next_field
= NULL
;
1401 struct bt_ctf_field
*base_field
;
1402 struct bt_ctf_field_type
*base_type
;
1405 assert(!stack_empty(notit
->stack
));
1406 index
= stack_top(notit
->stack
)->index
;
1407 base_field
= stack_top(notit
->stack
)->base
;
1408 base_type
= bt_ctf_field_get_type(base_field
);
1410 PERR("Failed to get base field's type\n");
1414 switch (bt_ctf_field_type_get_type_id(base_type
)) {
1415 case BT_CTF_FIELD_TYPE_ID_STRUCT
:
1416 next_field
= bt_ctf_field_structure_get_field_by_index(
1419 case BT_CTF_FIELD_TYPE_ID_ARRAY
:
1420 next_field
= bt_ctf_field_array_get_field(base_field
, index
);
1422 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
1423 next_field
= bt_ctf_field_sequence_get_field(base_field
, index
);
1425 case BT_CTF_FIELD_TYPE_ID_VARIANT
:
1426 next_field
= bt_ctf_field_variant_get_current_field(base_field
);
1440 void update_clock_state(uint64_t *state
,
1441 struct bt_ctf_field
*value_field
)
1443 struct bt_ctf_field_type
*value_type
= NULL
;
1444 uint64_t requested_new_value
;
1445 uint64_t requested_new_value_mask
;
1446 uint64_t cur_value_masked
;
1447 int requested_new_value_size
;
1450 value_type
= bt_ctf_field_get_type(value_field
);
1453 requested_new_value_size
=
1454 bt_ctf_field_type_integer_get_size(value_type
);
1455 assert(requested_new_value_size
> 0);
1457 ret
= bt_ctf_field_unsigned_integer_get_value(value_field
,
1458 &requested_new_value
);
1462 * Special case for a 64-bit new value, which is the limit
1463 * of a clock value as of this version: overwrite the
1464 * current value directly.
1466 if (requested_new_value_size
== 64) {
1467 *state
= requested_new_value
;
1471 requested_new_value_mask
= (1ULL << requested_new_value_size
) - 1;
1472 cur_value_masked
= *state
& requested_new_value_mask
;
1474 if (requested_new_value
< cur_value_masked
) {
1476 * It looks like a wrap happened on the number of bits
1477 * of the requested new value. Assume that the clock
1478 * value wrapped only one time.
1480 *state
+= requested_new_value_mask
+ 1;
1483 /* Clear the low bits of the current clock value. */
1484 *state
&= ~requested_new_value_mask
;
1486 /* Set the low bits of the current clock value. */
1487 *state
|= requested_new_value
;
1493 enum bt_ctf_btr_status
update_clock(struct bt_ctf_notif_iter
*notit
,
1494 struct bt_ctf_field
*int_field
)
1496 gboolean clock_class_found
;
1497 uint64_t *clock_state
;
1498 struct bt_ctf_field_type
*int_field_type
= NULL
;
1499 enum bt_ctf_btr_status ret
= BT_CTF_BTR_STATUS_OK
;
1500 struct bt_ctf_clock_class
*clock_class
= NULL
;
1502 int_field_type
= bt_ctf_field_get_type(int_field
);
1503 if (unlikely(!int_field_type
)) {
1507 clock_class
= bt_ctf_field_type_integer_get_mapped_clock_class(
1509 if (likely(!clock_class
)) {
1513 clock_class_found
= g_hash_table_lookup_extended(notit
->clock_states
,
1514 clock_class
, NULL
, (gpointer
) &clock_state
);
1515 if (unlikely(!clock_class_found
)) {
1516 const char *clock_class_name
=
1517 bt_ctf_clock_class_get_name(clock_class
);
1519 PERR("Unknown clock class %s mapped to integer encountered in stream\n",
1520 clock_class_name
? : "NULL");
1521 ret
= BT_CTF_BTR_STATUS_ERROR
;
1525 if (unlikely(!clock_state
)) {
1526 clock_state
= g_new0(uint64_t, 1);
1528 ret
= BT_CTF_BTR_STATUS_ENOMEM
;
1531 g_hash_table_insert(notit
->clock_states
, bt_get(clock_class
),
1535 /* Update the clock's state. */
1536 update_clock_state(clock_state
, int_field
);
1538 bt_put(int_field_type
);
1539 bt_put(clock_class
);
1544 enum bt_ctf_btr_status
btr_unsigned_int_common(uint64_t value
,
1545 struct bt_ctf_field_type
*type
, void *data
,
1546 struct bt_ctf_field
**out_int_field
)
1548 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1549 struct bt_ctf_field
*field
= NULL
;
1550 struct bt_ctf_field
*int_field
= NULL
;
1551 struct bt_ctf_notif_iter
*notit
= data
;
1554 /* Create next field */
1555 field
= get_next_field(notit
);
1557 PERR("Failed to get next field (unsigned int)\n");
1558 status
= BT_CTF_BTR_STATUS_ERROR
;
1562 switch(bt_ctf_field_type_get_type_id(type
)) {
1563 case BT_CTF_FIELD_TYPE_ID_INTEGER
:
1564 /* Integer field is created field */
1565 BT_MOVE(int_field
, field
);
1568 case BT_CTF_FIELD_TYPE_ID_ENUM
:
1569 int_field
= bt_ctf_field_enumeration_get_container(field
);
1570 type
= bt_ctf_field_get_type(int_field
);
1579 PERR("Failed to get integer field\n");
1580 status
= BT_CTF_BTR_STATUS_ERROR
;
1584 ret
= bt_ctf_field_unsigned_integer_set_value(int_field
, value
);
1586 stack_top(notit
->stack
)->index
++;
1587 *out_int_field
= int_field
;
1597 enum bt_ctf_btr_status
btr_timestamp_end_cb(void *value
,
1598 struct bt_ctf_field_type
*type
, void *data
)
1600 enum bt_ctf_btr_status status
;
1601 struct bt_ctf_field
*field
= NULL
;
1602 struct bt_ctf_notif_iter
*notit
= data
;
1604 status
= btr_unsigned_int_common(*((uint64_t *) value
), type
, data
,
1607 /* Set as the current packet's timestamp_end field. */
1608 BT_MOVE(notit
->cur_timestamp_end
, field
);
1613 enum bt_ctf_btr_status
btr_unsigned_int_cb(uint64_t value
,
1614 struct bt_ctf_field_type
*type
, void *data
)
1616 struct bt_ctf_notif_iter
*notit
= data
;
1617 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1618 struct bt_ctf_field
*field
= NULL
;
1619 struct field_cb_override
*override
;
1621 override
= g_hash_table_lookup(notit
->field_overrides
,
1623 if (unlikely(override
)) {
1624 status
= override
->func(&value
, type
, override
->data
);
1628 status
= btr_unsigned_int_common(value
, type
, data
, &field
);
1629 if (status
!= BT_CTF_BTR_STATUS_OK
) {
1633 status
= update_clock(notit
, field
);
1640 enum bt_ctf_btr_status
btr_signed_int_cb(int64_t value
,
1641 struct bt_ctf_field_type
*type
, void *data
)
1643 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1644 struct bt_ctf_field
*field
= NULL
;
1645 struct bt_ctf_field
*int_field
= NULL
;
1646 struct bt_ctf_notif_iter
*notit
= data
;
1649 /* create next field */
1650 field
= get_next_field(notit
);
1652 PERR("Failed to get next field (signed int)\n");
1653 status
= BT_CTF_BTR_STATUS_ERROR
;
1657 switch(bt_ctf_field_type_get_type_id(type
)) {
1658 case BT_CTF_FIELD_TYPE_ID_INTEGER
:
1659 /* Integer field is created field */
1660 BT_MOVE(int_field
, field
);
1663 case BT_CTF_FIELD_TYPE_ID_ENUM
:
1664 int_field
= bt_ctf_field_enumeration_get_container(field
);
1665 type
= bt_ctf_field_get_type(int_field
);
1674 PERR("Failed to get integer field\n");
1675 status
= BT_CTF_BTR_STATUS_ERROR
;
1679 ret
= bt_ctf_field_signed_integer_set_value(int_field
, value
);
1681 stack_top(notit
->stack
)->index
++;
1682 status
= update_clock(notit
, int_field
);
1692 enum bt_ctf_btr_status
btr_floating_point_cb(double value
,
1693 struct bt_ctf_field_type
*type
, void *data
)
1695 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1696 struct bt_ctf_field
*field
= NULL
;
1697 struct bt_ctf_notif_iter
*notit
= data
;
1700 /* Create next field */
1701 field
= get_next_field(notit
);
1703 PERR("Failed to get next field (floating point number)\n");
1704 status
= BT_CTF_BTR_STATUS_ERROR
;
1708 ret
= bt_ctf_field_floating_point_set_value(field
, value
);
1710 stack_top(notit
->stack
)->index
++;
1719 enum bt_ctf_btr_status
btr_string_begin_cb(
1720 struct bt_ctf_field_type
*type
, void *data
)
1722 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1723 struct bt_ctf_field
*field
= NULL
;
1724 struct bt_ctf_notif_iter
*notit
= data
;
1727 /* Create next field */
1728 field
= get_next_field(notit
);
1730 PERR("Failed to get next field (string)\n");
1731 status
= BT_CTF_BTR_STATUS_ERROR
;
1736 * Push on stack. Not a compound type per se, but we know that only
1737 * btr_string_cb() may be called between this call and a subsequent
1738 * call to btr_string_end_cb().
1740 ret
= stack_push(notit
->stack
, field
);
1742 PERR("Failed to push string field onto the stack\n");
1743 status
= BT_CTF_BTR_STATUS_ERROR
;
1748 * Initialize string field payload to an empty string since in the
1749 * case of a length 0 string the btr_string_cb won't be called and
1750 * we will end up with an unset string payload.
1752 ret
= bt_ctf_field_string_set_value(field
, "");
1754 PERR("Failed to initialize string field\n");
1755 status
= BT_CTF_BTR_STATUS_ERROR
;
1766 enum bt_ctf_btr_status
btr_string_cb(const char *value
,
1767 size_t len
, struct bt_ctf_field_type
*type
, void *data
)
1769 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1770 struct bt_ctf_field
*field
= NULL
;
1771 struct bt_ctf_notif_iter
*notit
= data
;
1774 /* Get string field */
1775 field
= stack_top(notit
->stack
)->base
;
1778 /* Append current string */
1779 ret
= bt_ctf_field_string_append_len(field
, value
, len
);
1781 PERR("Failed to append a string to a string field\n");
1782 status
= BT_CTF_BTR_STATUS_ERROR
;
1791 enum bt_ctf_btr_status
btr_string_end_cb(
1792 struct bt_ctf_field_type
*type
, void *data
)
1794 struct bt_ctf_notif_iter
*notit
= data
;
1796 /* Pop string field */
1797 stack_pop(notit
->stack
);
1799 /* Go to next field */
1800 stack_top(notit
->stack
)->index
++;
1802 return BT_CTF_BTR_STATUS_OK
;
1805 enum bt_ctf_btr_status
btr_compound_begin_cb(
1806 struct bt_ctf_field_type
*type
, void *data
)
1808 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1809 struct bt_ctf_notif_iter
*notit
= data
;
1810 struct bt_ctf_field
*field
;
1814 if (stack_empty(notit
->stack
)) {
1815 /* Root: create dynamic scope field */
1816 *notit
->cur_dscope_field
= bt_ctf_field_create(type
);
1817 field
= *notit
->cur_dscope_field
;
1820 * Field will be put at the end of this function
1821 * (stack_push() will take one reference, but this
1822 * reference is lost upon the equivalent stack_pop()
1823 * later), so also get it for our context to own it.
1825 bt_get(*notit
->cur_dscope_field
);
1827 field
= get_next_field(notit
);
1831 PERR("Failed to get next field or create dynamic scope field\n");
1832 status
= BT_CTF_BTR_STATUS_ERROR
;
1837 ret
= stack_push(notit
->stack
, field
);
1839 PERR("Failed to push compound field onto the stack\n");
1840 status
= BT_CTF_BTR_STATUS_ERROR
;
1850 enum bt_ctf_btr_status
btr_compound_end_cb(
1851 struct bt_ctf_field_type
*type
, void *data
)
1853 struct bt_ctf_notif_iter
*notit
= data
;
1855 assert(!stack_empty(notit
->stack
));
1858 stack_pop(notit
->stack
);
1860 /* If the stack is not empty, increment the base's index */
1861 if (!stack_empty(notit
->stack
)) {
1862 stack_top(notit
->stack
)->index
++;
1865 return BT_CTF_BTR_STATUS_OK
;
1869 struct bt_ctf_field
*resolve_field(struct bt_ctf_notif_iter
*notit
,
1870 struct bt_ctf_field_path
*path
)
1872 struct bt_ctf_field
*field
= NULL
;
1875 switch (bt_ctf_field_path_get_root_scope(path
)) {
1876 case BT_CTF_SCOPE_TRACE_PACKET_HEADER
:
1877 field
= notit
->dscopes
.trace_packet_header
;
1879 case BT_CTF_SCOPE_STREAM_PACKET_CONTEXT
:
1880 field
= notit
->dscopes
.stream_packet_context
;
1882 case BT_CTF_SCOPE_STREAM_EVENT_HEADER
:
1883 field
= notit
->dscopes
.stream_event_header
;
1885 case BT_CTF_SCOPE_STREAM_EVENT_CONTEXT
:
1886 field
= notit
->dscopes
.stream_event_context
;
1888 case BT_CTF_SCOPE_EVENT_CONTEXT
:
1889 field
= notit
->dscopes
.event_context
;
1891 case BT_CTF_SCOPE_EVENT_FIELDS
:
1892 field
= notit
->dscopes
.event_payload
;
1904 for (i
= 0; i
< bt_ctf_field_path_get_index_count(path
); ++i
) {
1905 struct bt_ctf_field
*next_field
= NULL
;
1906 struct bt_ctf_field_type
*field_type
;
1907 int index
= bt_ctf_field_path_get_index(path
, i
);
1909 field_type
= bt_ctf_field_get_type(field
);
1915 if (is_struct_type(field_type
)) {
1916 next_field
= bt_ctf_field_structure_get_field_by_index(
1918 } else if (is_variant_type(field_type
)) {
1920 bt_ctf_field_variant_get_current_field(field
);
1930 /* Move next field -> field */
1931 BT_MOVE(field
, next_field
);
1939 int64_t btr_get_sequence_length_cb(struct bt_ctf_field_type
*type
, void *data
)
1943 struct bt_ctf_field_path
*field_path
;
1944 struct bt_ctf_notif_iter
*notit
= data
;
1945 struct bt_ctf_field
*length_field
= NULL
;
1948 field_path
= bt_ctf_field_type_sequence_get_length_field_path(type
);
1953 length_field
= resolve_field(notit
, field_path
);
1954 if (!length_field
) {
1958 iret
= bt_ctf_field_unsigned_integer_get_value(length_field
, &length
);
1963 iret
= bt_ctf_field_sequence_set_length(stack_top(notit
->stack
)->base
,
1968 ret
= (int64_t) length
;
1971 BT_PUT(length_field
);
1978 struct bt_ctf_field_type
*btr_get_variant_type_cb(
1979 struct bt_ctf_field_type
*type
, void *data
)
1981 struct bt_ctf_field_path
*path
;
1982 struct bt_ctf_notif_iter
*notit
= data
;
1983 struct bt_ctf_field
*tag_field
= NULL
;
1984 struct bt_ctf_field
*selected_field
= NULL
;
1985 struct bt_ctf_field_type
*selected_field_type
= NULL
;
1987 path
= bt_ctf_field_type_variant_get_tag_field_path(type
);
1992 tag_field
= resolve_field(notit
, path
);
1998 * We found the enumeration tag field instance which should be
1999 * able to select a current field for this variant. This
2000 * callback function we're in is called _after_
2001 * compound_begin(), so the current stack top's base field is
2002 * the variant field in question. We get the selected field here
2003 * thanks to this tag field (thus creating the selected field),
2004 * which will also provide us with its type. Then, this field
2005 * will remain the current selected one until the next callback
2006 * function call which is used to fill the current selected
2009 selected_field
= bt_ctf_field_variant_get_field(
2010 stack_top(notit
->stack
)->base
, tag_field
);
2011 if (!selected_field
) {
2015 selected_field_type
= bt_ctf_field_get_type(selected_field
);
2019 BT_PUT(selected_field
);
2022 return selected_field_type
;
2026 int set_event_clocks(struct bt_ctf_event
*event
,
2027 struct bt_ctf_notif_iter
*notit
)
2030 GHashTableIter iter
;
2031 struct bt_ctf_clock_class
*clock_class
;
2032 uint64_t *clock_state
;
2034 g_hash_table_iter_init(&iter
, notit
->clock_states
);
2036 while (g_hash_table_iter_next(&iter
, (gpointer
) &clock_class
,
2037 (gpointer
) &clock_state
)) {
2038 struct bt_ctf_clock_value
*clock_value
;
2040 clock_value
= bt_ctf_clock_value_create(clock_class
,
2046 ret
= bt_ctf_event_set_clock_value(event
, clock_value
);
2047 bt_put(clock_value
);
2058 struct bt_ctf_event
*create_event(struct bt_ctf_notif_iter
*notit
)
2061 struct bt_ctf_event
*event
;
2063 /* Create event object. */
2064 event
= bt_ctf_event_create(notit
->meta
.event_class
);
2069 /* Set header, stream event context, context, and payload fields. */
2070 ret
= bt_ctf_event_set_header(event
,
2071 notit
->dscopes
.stream_event_header
);
2076 ret
= bt_ctf_event_set_stream_event_context(event
,
2077 notit
->dscopes
.stream_event_context
);
2082 ret
= bt_ctf_event_set_event_context(event
,
2083 notit
->dscopes
.event_context
);
2088 ret
= bt_ctf_event_set_payload_field(event
,
2089 notit
->dscopes
.event_payload
);
2094 ret
= set_event_clocks(event
, notit
);
2099 /* Associate with current packet. */
2100 assert(notit
->packet
);
2101 ret
= bt_ctf_event_set_packet(event
, notit
->packet
);
2114 void create_packet(struct bt_ctf_notif_iter
*notit
)
2117 struct bt_ctf_stream
*stream
= NULL
;
2118 struct bt_ctf_packet
*packet
= NULL
;
2120 /* Ask the user for the stream */
2121 stream
= notit
->medium
.medops
.get_stream(notit
->meta
.stream_class
,
2122 notit
->medium
.data
);
2128 packet
= bt_ctf_packet_create(stream
);
2133 /* Set packet's context and header fields */
2134 if (notit
->dscopes
.trace_packet_header
) {
2135 ret
= bt_ctf_packet_set_header(packet
,
2136 notit
->dscopes
.trace_packet_header
);
2142 if (notit
->dscopes
.stream_packet_context
) {
2143 ret
= bt_ctf_packet_set_context(packet
,
2144 notit
->dscopes
.stream_packet_context
);
2154 BT_MOVE(notit
->packet
, packet
);
2158 void notify_new_packet(struct bt_ctf_notif_iter
*notit
,
2159 struct bt_notification
**notification
)
2161 struct bt_notification
*ret
;
2163 /* Initialize the iterator's current packet */
2164 create_packet(notit
);
2165 if (!notit
->packet
) {
2169 ret
= bt_notification_packet_begin_create(notit
->packet
);
2173 *notification
= ret
;
2177 void notify_end_of_packet(struct bt_ctf_notif_iter
*notit
,
2178 struct bt_notification
**notification
)
2180 struct bt_notification
*ret
;
2182 if (!notit
->packet
) {
2186 ret
= bt_notification_packet_end_create(notit
->packet
);
2190 BT_PUT(notit
->packet
);
2191 *notification
= ret
;
2195 void notify_event(struct bt_ctf_notif_iter
*notit
,
2196 struct bt_notification
**notification
)
2198 struct bt_ctf_event
*event
;
2199 struct bt_notification
*ret
= NULL
;
2202 event
= create_event(notit
);
2207 ret
= bt_notification_event_create(event
, notit
->cc_prio_map
);
2211 *notification
= ret
;
2218 void notify_eos(struct bt_ctf_notif_iter
*notit
,
2219 struct bt_notification
**notification
)
2221 struct bt_ctf_stream
*stream
= NULL
;
2222 struct bt_notification
*ret
= NULL
;
2224 /* Ask the user for the stream */
2225 stream
= notit
->medium
.medops
.get_stream(notit
->meta
.stream_class
,
2226 notit
->medium
.data
);
2231 ret
= bt_notification_stream_end_create(stream
);
2235 *notification
= ret
;
2241 int init_clock_states(GHashTable
*clock_states
, struct bt_ctf_trace
*trace
)
2243 int clock_class_count
, i
, ret
= 0;
2245 clock_class_count
= bt_ctf_trace_get_clock_class_count(trace
);
2246 if (clock_class_count
<= 0) {
2251 for (i
= 0; i
< clock_class_count
; i
++) {
2252 struct bt_ctf_clock_class
*clock_class
;
2254 clock_class
= bt_ctf_trace_get_clock_class(trace
, i
);
2260 g_hash_table_insert(clock_states
, bt_get(clock_class
), NULL
);
2261 bt_put(clock_class
);
2268 void init_trace_field_path_cache(struct bt_ctf_trace
*trace
,
2269 struct trace_field_path_cache
*trace_field_path_cache
)
2272 int stream_instance_id
= -1;
2274 struct bt_ctf_field_type
*packet_header
= NULL
;
2276 packet_header
= bt_ctf_trace_get_packet_header_type(trace
);
2277 if (!packet_header
) {
2281 if (!bt_ctf_field_type_is_structure(packet_header
)) {
2285 count
= bt_ctf_field_type_structure_get_field_count(packet_header
);
2290 for (i
= 0; (i
< count
&& (stream_id
== -1 || stream_instance_id
== -1)); i
++) {
2292 const char *field_name
;
2294 ret
= bt_ctf_field_type_structure_get_field(packet_header
,
2295 &field_name
, NULL
, i
);
2300 if (stream_id
== -1 && !strcmp(field_name
, "stream_id")) {
2302 } else if (stream_instance_id
== -1 &&
2303 !strcmp(field_name
, "stream_instance_id")) {
2304 stream_instance_id
= i
;
2308 trace_field_path_cache
->stream_id
= stream_id
;
2309 trace_field_path_cache
->stream_instance_id
= stream_instance_id
;
2310 BT_PUT(packet_header
);
2314 struct bt_ctf_notif_iter
*bt_ctf_notif_iter_create(struct bt_ctf_trace
*trace
,
2315 struct bt_clock_class_priority_map
*cc_prio_map
,
2316 size_t max_request_sz
,
2317 struct bt_ctf_notif_iter_medium_ops medops
,
2318 void *data
, FILE *err_stream
)
2321 struct bt_ctf_notif_iter
*notit
= NULL
;
2322 struct bt_ctf_btr_cbs cbs
= {
2324 .signed_int
= btr_signed_int_cb
,
2325 .unsigned_int
= btr_unsigned_int_cb
,
2326 .floating_point
= btr_floating_point_cb
,
2327 .string_begin
= btr_string_begin_cb
,
2328 .string
= btr_string_cb
,
2329 .string_end
= btr_string_end_cb
,
2330 .compound_begin
= btr_compound_begin_cb
,
2331 .compound_end
= btr_compound_end_cb
,
2334 .get_sequence_length
= btr_get_sequence_length_cb
,
2335 .get_variant_type
= btr_get_variant_type_cb
,
2340 assert(cc_prio_map
);
2341 assert(medops
.request_bytes
);
2342 assert(medops
.get_stream
);
2343 notit
= g_new0(struct bt_ctf_notif_iter
, 1);
2345 PERR("Failed to allocate memory for CTF notification iterator\n");
2348 notit
->clock_states
= g_hash_table_new_full(g_direct_hash
,
2349 g_direct_equal
, bt_put
, g_free
);
2350 if (!notit
->clock_states
) {
2351 PERR("Failed to create hash table\n");
2354 ret
= init_clock_states(notit
->clock_states
, trace
);
2356 PERR("Failed to initialize stream clock states\n");
2359 notit
->cc_prio_map
= bt_get(cc_prio_map
);
2360 notit
->meta
.trace
= bt_get(trace
);
2361 notit
->medium
.medops
= medops
;
2362 notit
->medium
.max_request_sz
= max_request_sz
;
2363 notit
->medium
.data
= data
;
2364 notit
->err_stream
= err_stream
;
2365 notit
->stack
= stack_new(notit
);
2366 if (!notit
->stack
) {
2367 PERR("Failed to create stack\n");
2371 notit
->btr
= bt_ctf_btr_create(cbs
, notit
, err_stream
);
2373 PERR("Failed to create binary type reader\n");
2377 bt_ctf_notif_iter_reset(notit
);
2379 init_trace_field_path_cache(trace
, ¬it
->trace_field_path_cache
);
2380 notit
->sc_field_path_caches
= g_hash_table_new_full(g_direct_hash
,
2381 g_direct_equal
, bt_put
, g_free
);
2382 if (!notit
->sc_field_path_caches
) {
2383 PERR("Failed to create stream class field path caches\n");
2387 notit
->field_overrides
= g_hash_table_new_full(g_direct_hash
,
2388 g_direct_equal
, bt_put
, g_free
);
2389 if (!notit
->field_overrides
) {
2396 bt_ctf_notif_iter_destroy(notit
);
2401 void bt_ctf_notif_iter_destroy(struct bt_ctf_notif_iter
*notit
)
2403 BT_PUT(notit
->cc_prio_map
);
2404 BT_PUT(notit
->meta
.trace
);
2405 BT_PUT(notit
->meta
.stream_class
);
2406 BT_PUT(notit
->meta
.event_class
);
2407 BT_PUT(notit
->packet
);
2408 BT_PUT(notit
->cur_timestamp_end
);
2409 put_all_dscopes(notit
);
2412 stack_destroy(notit
->stack
);
2416 bt_ctf_btr_destroy(notit
->btr
);
2419 if (notit
->clock_states
) {
2420 g_hash_table_destroy(notit
->clock_states
);
2423 if (notit
->sc_field_path_caches
) {
2424 g_hash_table_destroy(notit
->sc_field_path_caches
);
2427 if (notit
->field_overrides
) {
2428 g_hash_table_destroy(notit
->field_overrides
);
2433 enum bt_ctf_notif_iter_status
bt_ctf_notif_iter_get_next_notification(
2434 struct bt_ctf_notif_iter
*notit
,
2435 struct bt_notification
**notification
)
2437 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
2440 assert(notification
);
2443 status
= handle_state(notit
);
2444 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
2445 if (status
== BT_CTF_NOTIF_ITER_STATUS_EOF
) {
2446 PDBG("Medium operation reported end of stream\n");
2448 PERR("Failed to handle state:\n");
2449 PERR("\tState: %d\n", notit
->state
);
2454 switch (notit
->state
) {
2455 case STATE_EMIT_NOTIF_NEW_PACKET
:
2456 PDBG("Emitting new packet notification\n");
2457 notify_new_packet(notit
, notification
);
2458 if (!*notification
) {
2459 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
2462 case STATE_EMIT_NOTIF_EVENT
:
2463 PDBG("Emitting event notification\n");
2464 notify_event(notit
, notification
);
2465 if (!*notification
) {
2466 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
2469 case STATE_EMIT_NOTIF_END_OF_PACKET
:
2470 /* Update clock with timestamp_end field. */
2471 if (notit
->cur_timestamp_end
) {
2472 enum bt_ctf_btr_status btr_status
;
2473 struct bt_ctf_field_type
*field_type
=
2474 bt_ctf_field_get_type(
2475 notit
->cur_timestamp_end
);
2477 btr_status
= update_clock(notit
,
2478 notit
->cur_timestamp_end
);
2480 if (btr_status
!= BT_CTF_BTR_STATUS_OK
) {
2481 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
2486 PDBG("Emitting end of packet notification\n");
2487 notify_end_of_packet(notit
, notification
);
2488 if (!*notification
) {
2489 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
2493 /* Non-emitting state: continue */