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_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_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_TYPE_ID_STRUCT
:
1416 next_field
= bt_ctf_field_structure_get_field_by_index(
1419 case BT_CTF_TYPE_ID_ARRAY
:
1420 next_field
= bt_ctf_field_array_get_field(base_field
, index
);
1422 case BT_CTF_TYPE_ID_SEQUENCE
:
1423 next_field
= bt_ctf_field_sequence_get_field(base_field
, index
);
1425 case BT_CTF_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_type
*int_field_type
,
1495 struct bt_ctf_field
*int_field
)
1497 gboolean clock_class_found
;
1498 uint64_t *clock_state
;
1499 enum bt_ctf_btr_status ret
= BT_CTF_BTR_STATUS_OK
;
1500 struct bt_ctf_clock_class
*clock_class
=
1501 bt_ctf_field_type_integer_get_mapped_clock_class(
1504 if (likely(!clock_class
)) {
1508 clock_class_found
= g_hash_table_lookup_extended(notit
->clock_states
,
1509 clock_class
, NULL
, (gpointer
) &clock_state
);
1510 if (unlikely(!clock_class_found
)) {
1511 const char *clock_class_name
=
1512 bt_ctf_clock_class_get_name(clock_class
);
1514 PERR("Unknown clock class %s mapped to integer encountered in stream\n",
1515 clock_class_name
? : "NULL");
1516 ret
= BT_CTF_BTR_STATUS_ERROR
;
1520 if (unlikely(!clock_state
)) {
1521 clock_state
= g_new0(uint64_t, 1);
1523 ret
= BT_CTF_BTR_STATUS_ENOMEM
;
1526 g_hash_table_insert(notit
->clock_states
, bt_get(clock_class
),
1530 /* Update the clock's state. */
1531 update_clock_state(clock_state
, int_field
);
1533 bt_put(clock_class
);
1539 enum bt_ctf_btr_status
btr_unsigned_int_common(uint64_t value
,
1540 struct bt_ctf_field_type
*type
, void *data
,
1541 struct bt_ctf_field
**_field
)
1543 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1544 struct bt_ctf_field
*field
= NULL
;
1545 struct bt_ctf_field
*int_field
= NULL
;
1546 struct bt_ctf_notif_iter
*notit
= data
;
1549 /* Create next field */
1550 field
= get_next_field(notit
);
1552 PERR("Failed to get next field (unsigned int)\n");
1553 status
= BT_CTF_BTR_STATUS_ERROR
;
1557 switch(bt_ctf_field_type_get_type_id(type
)) {
1558 case BT_CTF_TYPE_ID_INTEGER
:
1559 /* Integer field is created field */
1560 BT_MOVE(int_field
, field
);
1563 case BT_CTF_TYPE_ID_ENUM
:
1564 int_field
= bt_ctf_field_enumeration_get_container(field
);
1565 type
= bt_ctf_field_get_type(int_field
);
1574 PERR("Failed to get integer field\n");
1575 status
= BT_CTF_BTR_STATUS_ERROR
;
1579 ret
= bt_ctf_field_unsigned_integer_set_value(int_field
, value
);
1581 stack_top(notit
->stack
)->index
++;
1582 *_field
= int_field
;
1592 enum bt_ctf_btr_status
btr_timestamp_end_cb(void *value
,
1593 struct bt_ctf_field_type
*type
, void *data
)
1595 enum bt_ctf_btr_status status
;
1596 struct bt_ctf_field
*field
= NULL
;
1597 struct bt_ctf_notif_iter
*notit
= data
;
1599 status
= btr_unsigned_int_common(*((uint64_t *) value
), type
, data
,
1602 /* Set as the current packet's timestamp_end field. */
1603 BT_MOVE(notit
->cur_timestamp_end
, field
);
1608 enum bt_ctf_btr_status
btr_unsigned_int_cb(uint64_t value
,
1609 struct bt_ctf_field_type
*type
, void *data
)
1611 struct bt_ctf_notif_iter
*notit
= data
;
1612 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1613 struct bt_ctf_field
*field
= NULL
;
1614 struct field_cb_override
*override
;
1616 override
= g_hash_table_lookup(notit
->field_overrides
,
1618 if (unlikely(override
)) {
1619 status
= override
->func(&value
, type
, override
->data
);
1623 status
= btr_unsigned_int_common(value
, type
, data
, &field
);
1624 if (status
!= BT_CTF_BTR_STATUS_OK
) {
1628 status
= update_clock(notit
, type
, field
);
1635 enum bt_ctf_btr_status
btr_signed_int_cb(int64_t value
,
1636 struct bt_ctf_field_type
*type
, void *data
)
1638 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1639 struct bt_ctf_field
*field
= NULL
;
1640 struct bt_ctf_field
*int_field
= NULL
;
1641 struct bt_ctf_notif_iter
*notit
= data
;
1644 /* create next field */
1645 field
= get_next_field(notit
);
1647 PERR("Failed to get next field (signed int)\n");
1648 status
= BT_CTF_BTR_STATUS_ERROR
;
1652 switch(bt_ctf_field_type_get_type_id(type
)) {
1653 case BT_CTF_TYPE_ID_INTEGER
:
1654 /* Integer field is created field */
1655 BT_MOVE(int_field
, field
);
1658 case BT_CTF_TYPE_ID_ENUM
:
1659 int_field
= bt_ctf_field_enumeration_get_container(field
);
1660 type
= bt_ctf_field_get_type(int_field
);
1669 PERR("Failed to get integer field\n");
1670 status
= BT_CTF_BTR_STATUS_ERROR
;
1674 ret
= bt_ctf_field_signed_integer_set_value(int_field
, value
);
1676 stack_top(notit
->stack
)->index
++;
1677 status
= update_clock(notit
, type
, int_field
);
1687 enum bt_ctf_btr_status
btr_floating_point_cb(double value
,
1688 struct bt_ctf_field_type
*type
, void *data
)
1690 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1691 struct bt_ctf_field
*field
= NULL
;
1692 struct bt_ctf_notif_iter
*notit
= data
;
1695 /* Create next field */
1696 field
= get_next_field(notit
);
1698 PERR("Failed to get next field (floating point number)\n");
1699 status
= BT_CTF_BTR_STATUS_ERROR
;
1703 ret
= bt_ctf_field_floating_point_set_value(field
, value
);
1705 stack_top(notit
->stack
)->index
++;
1714 enum bt_ctf_btr_status
btr_string_begin_cb(
1715 struct bt_ctf_field_type
*type
, void *data
)
1717 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1718 struct bt_ctf_field
*field
= NULL
;
1719 struct bt_ctf_notif_iter
*notit
= data
;
1722 /* Create next field */
1723 field
= get_next_field(notit
);
1725 PERR("Failed to get next field (string)\n");
1726 status
= BT_CTF_BTR_STATUS_ERROR
;
1731 * Push on stack. Not a compound type per se, but we know that only
1732 * btr_string_cb() may be called between this call and a subsequent
1733 * call to btr_string_end_cb().
1735 ret
= stack_push(notit
->stack
, field
);
1737 PERR("Failed to push string field onto the stack\n");
1738 status
= BT_CTF_BTR_STATUS_ERROR
;
1743 * Initialize string field payload to an empty string since in the
1744 * case of a length 0 string the btr_string_cb won't be called and
1745 * we will end up with an unset string payload.
1747 ret
= bt_ctf_field_string_set_value(field
, "");
1749 PERR("Failed to initialize string field\n");
1750 status
= BT_CTF_BTR_STATUS_ERROR
;
1761 enum bt_ctf_btr_status
btr_string_cb(const char *value
,
1762 size_t len
, struct bt_ctf_field_type
*type
, void *data
)
1764 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1765 struct bt_ctf_field
*field
= NULL
;
1766 struct bt_ctf_notif_iter
*notit
= data
;
1769 /* Get string field */
1770 field
= stack_top(notit
->stack
)->base
;
1773 /* Append current string */
1774 ret
= bt_ctf_field_string_append_len(field
, value
, len
);
1776 PERR("Failed to append a string to a string field\n");
1777 status
= BT_CTF_BTR_STATUS_ERROR
;
1786 enum bt_ctf_btr_status
btr_string_end_cb(
1787 struct bt_ctf_field_type
*type
, void *data
)
1789 struct bt_ctf_notif_iter
*notit
= data
;
1791 /* Pop string field */
1792 stack_pop(notit
->stack
);
1794 /* Go to next field */
1795 stack_top(notit
->stack
)->index
++;
1797 return BT_CTF_BTR_STATUS_OK
;
1800 enum bt_ctf_btr_status
btr_compound_begin_cb(
1801 struct bt_ctf_field_type
*type
, void *data
)
1803 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1804 struct bt_ctf_notif_iter
*notit
= data
;
1805 struct bt_ctf_field
*field
;
1809 if (stack_empty(notit
->stack
)) {
1810 /* Root: create dynamic scope field */
1811 *notit
->cur_dscope_field
= bt_ctf_field_create(type
);
1812 field
= *notit
->cur_dscope_field
;
1815 * Field will be put at the end of this function
1816 * (stack_push() will take one reference, but this
1817 * reference is lost upon the equivalent stack_pop()
1818 * later), so also get it for our context to own it.
1820 bt_get(*notit
->cur_dscope_field
);
1822 field
= get_next_field(notit
);
1826 PERR("Failed to get next field or create dynamic scope field\n");
1827 status
= BT_CTF_BTR_STATUS_ERROR
;
1832 ret
= stack_push(notit
->stack
, field
);
1834 PERR("Failed to push compound field onto the stack\n");
1835 status
= BT_CTF_BTR_STATUS_ERROR
;
1845 enum bt_ctf_btr_status
btr_compound_end_cb(
1846 struct bt_ctf_field_type
*type
, void *data
)
1848 struct bt_ctf_notif_iter
*notit
= data
;
1850 assert(!stack_empty(notit
->stack
));
1853 stack_pop(notit
->stack
);
1855 /* If the stack is not empty, increment the base's index */
1856 if (!stack_empty(notit
->stack
)) {
1857 stack_top(notit
->stack
)->index
++;
1860 return BT_CTF_BTR_STATUS_OK
;
1864 struct bt_ctf_field
*resolve_field(struct bt_ctf_notif_iter
*notit
,
1865 struct bt_ctf_field_path
*path
)
1867 struct bt_ctf_field
*field
= NULL
;
1870 switch (bt_ctf_field_path_get_root_scope(path
)) {
1871 case BT_CTF_SCOPE_TRACE_PACKET_HEADER
:
1872 field
= notit
->dscopes
.trace_packet_header
;
1874 case BT_CTF_SCOPE_STREAM_PACKET_CONTEXT
:
1875 field
= notit
->dscopes
.stream_packet_context
;
1877 case BT_CTF_SCOPE_STREAM_EVENT_HEADER
:
1878 field
= notit
->dscopes
.stream_event_header
;
1880 case BT_CTF_SCOPE_STREAM_EVENT_CONTEXT
:
1881 field
= notit
->dscopes
.stream_event_context
;
1883 case BT_CTF_SCOPE_EVENT_CONTEXT
:
1884 field
= notit
->dscopes
.event_context
;
1886 case BT_CTF_SCOPE_EVENT_FIELDS
:
1887 field
= notit
->dscopes
.event_payload
;
1899 for (i
= 0; i
< bt_ctf_field_path_get_index_count(path
); ++i
) {
1900 struct bt_ctf_field
*next_field
= NULL
;
1901 struct bt_ctf_field_type
*field_type
;
1902 int index
= bt_ctf_field_path_get_index(path
, i
);
1904 field_type
= bt_ctf_field_get_type(field
);
1910 if (is_struct_type(field_type
)) {
1911 next_field
= bt_ctf_field_structure_get_field_by_index(
1913 } else if (is_variant_type(field_type
)) {
1915 bt_ctf_field_variant_get_current_field(field
);
1925 /* Move next field -> field */
1926 BT_MOVE(field
, next_field
);
1934 int64_t btr_get_sequence_length_cb(struct bt_ctf_field_type
*type
, void *data
)
1938 struct bt_ctf_field_path
*field_path
;
1939 struct bt_ctf_notif_iter
*notit
= data
;
1940 struct bt_ctf_field
*length_field
= NULL
;
1943 field_path
= bt_ctf_field_type_sequence_get_length_field_path(type
);
1948 length_field
= resolve_field(notit
, field_path
);
1949 if (!length_field
) {
1953 iret
= bt_ctf_field_unsigned_integer_get_value(length_field
, &length
);
1958 iret
= bt_ctf_field_sequence_set_length(stack_top(notit
->stack
)->base
,
1963 ret
= (int64_t) length
;
1966 BT_PUT(length_field
);
1973 struct bt_ctf_field_type
*btr_get_variant_type_cb(
1974 struct bt_ctf_field_type
*type
, void *data
)
1976 struct bt_ctf_field_path
*path
;
1977 struct bt_ctf_notif_iter
*notit
= data
;
1978 struct bt_ctf_field
*tag_field
= NULL
;
1979 struct bt_ctf_field
*selected_field
= NULL
;
1980 struct bt_ctf_field_type
*selected_field_type
= NULL
;
1982 path
= bt_ctf_field_type_variant_get_tag_field_path(type
);
1987 tag_field
= resolve_field(notit
, path
);
1993 * We found the enumeration tag field instance which should be
1994 * able to select a current field for this variant. This
1995 * callback function we're in is called _after_
1996 * compound_begin(), so the current stack top's base field is
1997 * the variant field in question. We get the selected field here
1998 * thanks to this tag field (thus creating the selected field),
1999 * which will also provide us with its type. Then, this field
2000 * will remain the current selected one until the next callback
2001 * function call which is used to fill the current selected
2004 selected_field
= bt_ctf_field_variant_get_field(
2005 stack_top(notit
->stack
)->base
, tag_field
);
2006 if (!selected_field
) {
2010 selected_field_type
= bt_ctf_field_get_type(selected_field
);
2014 BT_PUT(selected_field
);
2017 return selected_field_type
;
2021 int set_event_clocks(struct bt_ctf_event
*event
,
2022 struct bt_ctf_notif_iter
*notit
)
2025 GHashTableIter iter
;
2026 struct bt_ctf_clock_class
*clock_class
;
2027 uint64_t *clock_state
;
2029 g_hash_table_iter_init(&iter
, notit
->clock_states
);
2031 while (g_hash_table_iter_next(&iter
, (gpointer
) &clock_class
,
2032 (gpointer
) &clock_state
)) {
2033 struct bt_ctf_clock_value
*clock_value
;
2035 clock_value
= bt_ctf_clock_value_create(clock_class
,
2041 ret
= bt_ctf_event_set_clock_value(event
, clock_value
);
2042 bt_put(clock_value
);
2053 struct bt_ctf_event
*create_event(struct bt_ctf_notif_iter
*notit
)
2056 struct bt_ctf_event
*event
;
2058 /* Create event object. */
2059 event
= bt_ctf_event_create(notit
->meta
.event_class
);
2064 /* Set header, stream event context, context, and payload fields. */
2065 ret
= bt_ctf_event_set_header(event
,
2066 notit
->dscopes
.stream_event_header
);
2071 ret
= bt_ctf_event_set_stream_event_context(event
,
2072 notit
->dscopes
.stream_event_context
);
2077 ret
= bt_ctf_event_set_event_context(event
,
2078 notit
->dscopes
.event_context
);
2083 ret
= bt_ctf_event_set_payload_field(event
,
2084 notit
->dscopes
.event_payload
);
2089 ret
= set_event_clocks(event
, notit
);
2094 /* Associate with current packet. */
2095 assert(notit
->packet
);
2096 ret
= bt_ctf_event_set_packet(event
, notit
->packet
);
2109 void create_packet(struct bt_ctf_notif_iter
*notit
)
2112 struct bt_ctf_stream
*stream
= NULL
;
2113 struct bt_ctf_packet
*packet
= NULL
;
2115 /* Ask the user for the stream */
2116 stream
= notit
->medium
.medops
.get_stream(notit
->meta
.stream_class
,
2117 notit
->medium
.data
);
2123 packet
= bt_ctf_packet_create(stream
);
2128 /* Set packet's context and header fields */
2129 if (notit
->dscopes
.trace_packet_header
) {
2130 ret
= bt_ctf_packet_set_header(packet
,
2131 notit
->dscopes
.trace_packet_header
);
2137 if (notit
->dscopes
.stream_packet_context
) {
2138 ret
= bt_ctf_packet_set_context(packet
,
2139 notit
->dscopes
.stream_packet_context
);
2149 BT_MOVE(notit
->packet
, packet
);
2153 void notify_new_packet(struct bt_ctf_notif_iter
*notit
,
2154 struct bt_notification
**notification
)
2156 struct bt_notification
*ret
;
2158 /* Initialize the iterator's current packet */
2159 create_packet(notit
);
2160 if (!notit
->packet
) {
2164 ret
= bt_notification_packet_begin_create(notit
->packet
);
2168 *notification
= ret
;
2172 void notify_end_of_packet(struct bt_ctf_notif_iter
*notit
,
2173 struct bt_notification
**notification
)
2175 struct bt_notification
*ret
;
2177 if (!notit
->packet
) {
2181 ret
= bt_notification_packet_end_create(notit
->packet
);
2185 BT_PUT(notit
->packet
);
2186 *notification
= ret
;
2190 void notify_event(struct bt_ctf_notif_iter
*notit
,
2191 struct bt_notification
**notification
)
2193 struct bt_ctf_event
*event
;
2194 struct bt_notification
*ret
= NULL
;
2197 event
= create_event(notit
);
2202 ret
= bt_notification_event_create(event
, notit
->cc_prio_map
);
2206 *notification
= ret
;
2213 void notify_eos(struct bt_ctf_notif_iter
*notit
,
2214 struct bt_notification
**notification
)
2216 struct bt_ctf_stream
*stream
= NULL
;
2217 struct bt_notification
*ret
= NULL
;
2219 /* Ask the user for the stream */
2220 stream
= notit
->medium
.medops
.get_stream(notit
->meta
.stream_class
,
2221 notit
->medium
.data
);
2226 ret
= bt_notification_stream_end_create(stream
);
2230 *notification
= ret
;
2236 int init_clock_states(GHashTable
*clock_states
, struct bt_ctf_trace
*trace
)
2238 int clock_class_count
, i
, ret
= 0;
2240 clock_class_count
= bt_ctf_trace_get_clock_class_count(trace
);
2241 if (clock_class_count
<= 0) {
2246 for (i
= 0; i
< clock_class_count
; i
++) {
2247 struct bt_ctf_clock_class
*clock_class
;
2249 clock_class
= bt_ctf_trace_get_clock_class(trace
, i
);
2255 g_hash_table_insert(clock_states
, bt_get(clock_class
), NULL
);
2256 bt_put(clock_class
);
2263 void init_trace_field_path_cache(struct bt_ctf_trace
*trace
,
2264 struct trace_field_path_cache
*trace_field_path_cache
)
2267 int stream_instance_id
= -1;
2269 struct bt_ctf_field_type
*packet_header
= NULL
;
2271 packet_header
= bt_ctf_trace_get_packet_header_type(trace
);
2272 if (!packet_header
) {
2276 if (!bt_ctf_field_type_is_structure(packet_header
)) {
2280 count
= bt_ctf_field_type_structure_get_field_count(packet_header
);
2285 for (i
= 0; (i
< count
&& (stream_id
== -1 || stream_instance_id
== -1)); i
++) {
2287 const char *field_name
;
2289 ret
= bt_ctf_field_type_structure_get_field(packet_header
,
2290 &field_name
, NULL
, i
);
2295 if (stream_id
== -1 && !strcmp(field_name
, "stream_id")) {
2297 } else if (stream_instance_id
== -1 &&
2298 !strcmp(field_name
, "stream_instance_id")) {
2299 stream_instance_id
= i
;
2303 trace_field_path_cache
->stream_id
= stream_id
;
2304 trace_field_path_cache
->stream_instance_id
= stream_instance_id
;
2305 BT_PUT(packet_header
);
2309 struct bt_ctf_notif_iter
*bt_ctf_notif_iter_create(struct bt_ctf_trace
*trace
,
2310 struct bt_clock_class_priority_map
*cc_prio_map
,
2311 size_t max_request_sz
,
2312 struct bt_ctf_notif_iter_medium_ops medops
,
2313 void *data
, FILE *err_stream
)
2316 struct bt_ctf_notif_iter
*notit
= NULL
;
2317 struct bt_ctf_btr_cbs cbs
= {
2319 .signed_int
= btr_signed_int_cb
,
2320 .unsigned_int
= btr_unsigned_int_cb
,
2321 .floating_point
= btr_floating_point_cb
,
2322 .string_begin
= btr_string_begin_cb
,
2323 .string
= btr_string_cb
,
2324 .string_end
= btr_string_end_cb
,
2325 .compound_begin
= btr_compound_begin_cb
,
2326 .compound_end
= btr_compound_end_cb
,
2329 .get_sequence_length
= btr_get_sequence_length_cb
,
2330 .get_variant_type
= btr_get_variant_type_cb
,
2335 assert(cc_prio_map
);
2336 assert(medops
.request_bytes
);
2337 assert(medops
.get_stream
);
2338 notit
= g_new0(struct bt_ctf_notif_iter
, 1);
2340 PERR("Failed to allocate memory for CTF notification iterator\n");
2343 notit
->clock_states
= g_hash_table_new_full(g_direct_hash
,
2344 g_direct_equal
, bt_put
, g_free
);
2345 if (!notit
->clock_states
) {
2346 PERR("Failed to create hash table\n");
2349 ret
= init_clock_states(notit
->clock_states
, trace
);
2351 PERR("Failed to initialize stream clock states\n");
2354 notit
->cc_prio_map
= bt_get(cc_prio_map
);
2355 notit
->meta
.trace
= bt_get(trace
);
2356 notit
->medium
.medops
= medops
;
2357 notit
->medium
.max_request_sz
= max_request_sz
;
2358 notit
->medium
.data
= data
;
2359 notit
->err_stream
= err_stream
;
2360 notit
->stack
= stack_new(notit
);
2361 if (!notit
->stack
) {
2362 PERR("Failed to create stack\n");
2366 notit
->btr
= bt_ctf_btr_create(cbs
, notit
, err_stream
);
2368 PERR("Failed to create binary type reader\n");
2372 bt_ctf_notif_iter_reset(notit
);
2374 init_trace_field_path_cache(trace
, ¬it
->trace_field_path_cache
);
2375 notit
->sc_field_path_caches
= g_hash_table_new_full(g_direct_hash
,
2376 g_direct_equal
, bt_put
, g_free
);
2377 if (!notit
->sc_field_path_caches
) {
2378 PERR("Failed to create stream class field path caches\n");
2382 notit
->field_overrides
= g_hash_table_new_full(g_direct_hash
,
2383 g_direct_equal
, bt_put
, g_free
);
2384 if (!notit
->field_overrides
) {
2391 bt_ctf_notif_iter_destroy(notit
);
2396 void bt_ctf_notif_iter_destroy(struct bt_ctf_notif_iter
*notit
)
2398 BT_PUT(notit
->cc_prio_map
);
2399 BT_PUT(notit
->meta
.trace
);
2400 BT_PUT(notit
->meta
.stream_class
);
2401 BT_PUT(notit
->meta
.event_class
);
2402 BT_PUT(notit
->packet
);
2403 BT_PUT(notit
->cur_timestamp_end
);
2404 put_all_dscopes(notit
);
2407 stack_destroy(notit
->stack
);
2411 bt_ctf_btr_destroy(notit
->btr
);
2414 if (notit
->clock_states
) {
2415 g_hash_table_destroy(notit
->clock_states
);
2418 if (notit
->sc_field_path_caches
) {
2419 g_hash_table_destroy(notit
->sc_field_path_caches
);
2422 if (notit
->field_overrides
) {
2423 g_hash_table_destroy(notit
->field_overrides
);
2428 enum bt_ctf_notif_iter_status
bt_ctf_notif_iter_get_next_notification(
2429 struct bt_ctf_notif_iter
*notit
,
2430 struct bt_notification
**notification
)
2432 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
2435 assert(notification
);
2438 status
= handle_state(notit
);
2439 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
2440 if (status
== BT_CTF_NOTIF_ITER_STATUS_EOF
) {
2441 PDBG("Medium operation reported end of stream\n");
2443 PERR("Failed to handle state:\n");
2444 PERR("\tState: %d\n", notit
->state
);
2449 switch (notit
->state
) {
2450 case STATE_EMIT_NOTIF_NEW_PACKET
:
2451 PDBG("Emitting new packet notification\n");
2452 notify_new_packet(notit
, notification
);
2453 if (!*notification
) {
2454 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
2457 case STATE_EMIT_NOTIF_EVENT
:
2458 PDBG("Emitting event notification\n");
2459 notify_event(notit
, notification
);
2460 if (!*notification
) {
2461 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
2464 case STATE_EMIT_NOTIF_END_OF_PACKET
:
2465 /* Update clock with timestamp_end field. */
2466 if (notit
->cur_timestamp_end
) {
2467 enum bt_ctf_btr_status btr_status
;
2468 struct bt_ctf_field_type
*field_type
=
2469 bt_ctf_field_get_type(
2470 notit
->cur_timestamp_end
);
2472 btr_status
= update_clock(notit
, field_type
,
2473 notit
->cur_timestamp_end
);
2475 if (btr_status
!= BT_CTF_BTR_STATUS_OK
) {
2476 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
2481 PDBG("Emitting end of packet notification\n");
2482 notify_end_of_packet(notit
, notification
);
2483 if (!*notification
) {
2484 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
2488 /* Non-emitting state: continue */