2 * Babeltrace - CTF message iterator
4 * Copyright (c) 2015-2018 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2015-2018 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
26 #define BT_LOG_TAG "PLUGIN-CTF-MSG-ITER"
34 #include "common/assert.h"
36 #include <babeltrace2/babeltrace.h>
37 #include "common/common.h"
42 #include "../bfcr/bfcr.h"
46 /* A visit stack entry */
49 * Current base field, one of:
61 /* Index of next field to set */
67 /* Entries (struct stack_entry) */
70 /* Number of active entries */
77 STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
,
78 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
79 STATE_AFTER_TRACE_PACKET_HEADER
,
80 STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
,
81 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
82 STATE_AFTER_STREAM_PACKET_CONTEXT
,
83 STATE_CHECK_EMIT_MSG_STREAM_BEGINNING
,
84 STATE_EMIT_MSG_STREAM_BEGINNING
,
85 STATE_EMIT_MSG_STREAM_ACTIVITY_BEGINNING
,
86 STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS
,
87 STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
,
88 STATE_EMIT_MSG_DISCARDED_EVENTS
,
89 STATE_EMIT_MSG_DISCARDED_PACKETS
,
90 STATE_EMIT_MSG_PACKET_BEGINNING
,
91 STATE_DSCOPE_EVENT_HEADER_BEGIN
,
92 STATE_DSCOPE_EVENT_HEADER_CONTINUE
,
93 STATE_AFTER_EVENT_HEADER
,
94 STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
,
95 STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
,
96 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
,
97 STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
,
98 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
99 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
100 STATE_EMIT_MSG_EVENT
,
101 STATE_SKIP_PACKET_PADDING
,
102 STATE_EMIT_MSG_PACKET_END_MULTI
,
103 STATE_EMIT_MSG_PACKET_END_SINGLE
,
104 STATE_CHECK_EMIT_MSG_STREAM_ACTIVITY_END
,
105 STATE_EMIT_MSG_STREAM_ACTIVITY_END
,
106 STATE_EMIT_MSG_STREAM_END
,
110 struct end_of_packet_snapshots
{
111 uint64_t discarded_events
;
113 uint64_t beginning_clock
;
117 /* CTF message iterator */
122 /* Current message iterator to create messages (weak) */
123 bt_self_message_iterator
*msg_iter
;
126 * True to emit stream beginning and stream activity beginning
129 bool emit_stream_begin_msg
;
131 /* True to emit stream end and stream activity end messages */
132 bool emit_stream_end_msg
;
134 /* True to set the stream */
138 * Current dynamic scope field pointer.
140 * This is set by read_dscope_begin_state() and contains the
141 * value of one of the pointers in `dscopes` below.
143 bt_field
*cur_dscope_field
;
146 * True if we're done filling a string field from a text
147 * array/sequence payload.
149 bool done_filling_string
;
151 /* Trace and classes */
152 /* True to set IR fields */
156 struct ctf_trace_class
*tc
;
157 struct ctf_stream_class
*sc
;
158 struct ctf_event_class
*ec
;
161 /* Current packet context field wrapper (NULL if not created yet) */
162 bt_packet_context_field
*packet_context_field
;
164 /* Current packet (NULL if not created yet) */
167 /* Current stream (NULL if not set yet) */
170 /* Current event (NULL if not created yet) */
173 /* Current event message (NULL if not created yet) */
174 bt_message
*event_msg
;
176 /* Database of current dynamic scopes */
178 bt_field
*stream_packet_context
;
179 bt_field
*event_common_context
;
180 bt_field
*event_spec_context
;
181 bt_field
*event_payload
;
187 /* Current medium buffer data */
189 /* Last address provided by medium */
192 /* Buffer size provided by medium (bytes) */
195 /* Offset within whole packet of addr (bits) */
196 size_t packet_offset
;
198 /* Current position from addr (bits) */
201 /* Position of the last event header from addr (bits) */
205 /* Binary type reader */
206 struct bt_bfcr
*bfcr
;
208 /* Current medium data */
210 struct bt_msg_iter_medium_ops medops
;
211 size_t max_request_sz
;
215 /* Current packet size (bits) (-1 if unknown) */
216 int64_t cur_exp_packet_total_size
;
218 /* Current content size (bits) (-1 if unknown) */
219 int64_t cur_exp_packet_content_size
;
221 /* Current stream class ID */
222 int64_t cur_stream_class_id
;
224 /* Current event class ID */
225 int64_t cur_event_class_id
;
227 /* Current data stream ID */
228 int64_t cur_data_stream_id
;
231 * Offset, in the underlying media, of the current packet's
232 * start (-1 if unknown).
234 off_t cur_packet_offset
;
236 /* Default clock's current value */
237 uint64_t default_clock_snapshot
;
239 /* End of current packet snapshots */
240 struct end_of_packet_snapshots snapshots
;
242 /* End of previous packet snapshots */
243 struct end_of_packet_snapshots prev_packet_snapshots
;
245 /* Stored values (for sequence lengths, variant tags) */
246 GArray
*stored_values
;
250 const char *state_string(enum state state
)
255 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
256 return "STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN";
257 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
258 return "STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE";
259 case STATE_AFTER_TRACE_PACKET_HEADER
:
260 return "STATE_AFTER_TRACE_PACKET_HEADER";
261 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
262 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN";
263 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
264 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE";
265 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
266 return "STATE_AFTER_STREAM_PACKET_CONTEXT";
267 case STATE_EMIT_MSG_STREAM_BEGINNING
:
268 return "STATE_EMIT_MSG_STREAM_BEGINNING";
269 case STATE_EMIT_MSG_STREAM_ACTIVITY_BEGINNING
:
270 return "STATE_EMIT_MSG_STREAM_ACTIVITY_BEGINNING";
271 case STATE_EMIT_MSG_PACKET_BEGINNING
:
272 return "STATE_EMIT_MSG_PACKET_BEGINNING";
273 case STATE_EMIT_MSG_DISCARDED_EVENTS
:
274 return "STATE_EMIT_MSG_DISCARDED_EVENTS";
275 case STATE_EMIT_MSG_DISCARDED_PACKETS
:
276 return "STATE_EMIT_MSG_DISCARDED_PACKETS";
277 case STATE_DSCOPE_EVENT_HEADER_BEGIN
:
278 return "STATE_DSCOPE_EVENT_HEADER_BEGIN";
279 case STATE_DSCOPE_EVENT_HEADER_CONTINUE
:
280 return "STATE_DSCOPE_EVENT_HEADER_CONTINUE";
281 case STATE_AFTER_EVENT_HEADER
:
282 return "STATE_AFTER_EVENT_HEADER";
283 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
:
284 return "STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN";
285 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
:
286 return "STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE";
287 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
:
288 return "STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN";
289 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
:
290 return "STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE";
291 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
292 return "STATE_DSCOPE_EVENT_PAYLOAD_BEGIN";
293 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
294 return "STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE";
295 case STATE_EMIT_MSG_EVENT
:
296 return "STATE_EMIT_MSG_EVENT";
297 case STATE_SKIP_PACKET_PADDING
:
298 return "STATE_SKIP_PACKET_PADDING";
299 case STATE_EMIT_MSG_PACKET_END_MULTI
:
300 return "STATE_EMIT_MSG_PACKET_END_MULTI";
301 case STATE_EMIT_MSG_PACKET_END_SINGLE
:
302 return "STATE_EMIT_MSG_PACKET_END_SINGLE";
303 case STATE_EMIT_MSG_STREAM_ACTIVITY_END
:
304 return "STATE_EMIT_MSG_STREAM_ACTIVITY_END";
305 case STATE_EMIT_MSG_STREAM_END
:
306 return "STATE_EMIT_MSG_STREAM_END";
315 int bt_msg_iter_switch_packet(struct bt_msg_iter
*notit
);
318 struct stack
*stack_new(struct bt_msg_iter
*notit
)
320 struct stack
*stack
= NULL
;
322 stack
= g_new0(struct stack
, 1);
324 BT_LOGE_STR("Failed to allocate one stack.");
328 stack
->entries
= g_array_new(FALSE
, TRUE
, sizeof(struct stack_entry
));
329 if (!stack
->entries
) {
330 BT_LOGE_STR("Failed to allocate a GArray.");
334 BT_LOGD("Created stack: notit-addr=%p, stack-addr=%p", notit
, stack
);
346 void stack_destroy(struct stack
*stack
)
349 BT_LOGD("Destroying stack: addr=%p", stack
);
351 if (stack
->entries
) {
352 g_array_free(stack
->entries
, TRUE
);
359 void stack_push(struct stack
*stack
, bt_field
*base
)
361 struct stack_entry
*entry
;
365 BT_LOGV("Pushing base field on stack: stack-addr=%p, "
366 "stack-size-before=%zu, stack-size-after=%zu",
367 stack
, stack
->size
, stack
->size
+ 1);
369 if (stack
->entries
->len
== stack
->size
) {
370 g_array_set_size(stack
->entries
, stack
->size
+ 1);
373 entry
= &g_array_index(stack
->entries
, struct stack_entry
, stack
->size
);
380 unsigned int stack_size(struct stack
*stack
)
387 void stack_pop(struct stack
*stack
)
390 BT_ASSERT(stack_size(stack
));
391 BT_LOGV("Popping from stack: "
392 "stack-addr=%p, stack-size-before=%zu, stack-size-after=%zu",
393 stack
, stack
->size
, stack
->size
- 1);
398 struct stack_entry
*stack_top(struct stack
*stack
)
401 BT_ASSERT(stack_size(stack
));
402 return &g_array_index(stack
->entries
, struct stack_entry
,
407 bool stack_empty(struct stack
*stack
)
409 return stack_size(stack
) == 0;
413 void stack_clear(struct stack
*stack
)
420 enum bt_msg_iter_status
msg_iter_status_from_m_status(
421 enum bt_msg_iter_medium_status m_status
)
423 /* They are the same */
424 return (int) m_status
;
428 size_t buf_size_bits(struct bt_msg_iter
*notit
)
430 return notit
->buf
.sz
* 8;
434 size_t buf_available_bits(struct bt_msg_iter
*notit
)
436 return buf_size_bits(notit
) - notit
->buf
.at
;
440 size_t packet_at(struct bt_msg_iter
*notit
)
442 return notit
->buf
.packet_offset
+ notit
->buf
.at
;
446 void buf_consume_bits(struct bt_msg_iter
*notit
, size_t incr
)
448 BT_LOGV("Advancing cursor: notit-addr=%p, cur-before=%zu, cur-after=%zu",
449 notit
, notit
->buf
.at
, notit
->buf
.at
+ incr
);
450 notit
->buf
.at
+= incr
;
454 enum bt_msg_iter_status
request_medium_bytes(
455 struct bt_msg_iter
*notit
)
457 uint8_t *buffer_addr
= NULL
;
458 size_t buffer_sz
= 0;
459 enum bt_msg_iter_medium_status m_status
;
461 BT_LOGV("Calling user function (request bytes): notit-addr=%p, "
462 "request-size=%zu", notit
, notit
->medium
.max_request_sz
);
463 m_status
= notit
->medium
.medops
.request_bytes(
464 notit
->medium
.max_request_sz
, &buffer_addr
,
465 &buffer_sz
, notit
->medium
.data
);
466 BT_LOGV("User function returned: status=%s, buf-addr=%p, buf-size=%zu",
467 bt_msg_iter_medium_status_string(m_status
),
468 buffer_addr
, buffer_sz
);
469 if (m_status
== BT_MSG_ITER_MEDIUM_STATUS_OK
) {
470 BT_ASSERT(buffer_sz
!= 0);
472 /* New packet offset is old one + old size (in bits) */
473 notit
->buf
.packet_offset
+= buf_size_bits(notit
);
475 /* Restart at the beginning of the new medium buffer */
477 notit
->buf
.last_eh_at
= SIZE_MAX
;
479 /* New medium buffer size */
480 notit
->buf
.sz
= buffer_sz
;
482 /* New medium buffer address */
483 notit
->buf
.addr
= buffer_addr
;
485 BT_LOGV("User function returned new bytes: "
486 "packet-offset=%zu, cur=%zu, size=%zu, addr=%p",
487 notit
->buf
.packet_offset
, notit
->buf
.at
,
488 notit
->buf
.sz
, notit
->buf
.addr
);
489 BT_LOGV_MEM(buffer_addr
, buffer_sz
, "Returned bytes at %p:",
491 } else if (m_status
== BT_MSG_ITER_MEDIUM_STATUS_EOF
) {
493 * User returned end of stream: validate that we're not
494 * in the middle of a packet header, packet context, or
497 if (notit
->cur_exp_packet_total_size
>= 0) {
498 if (packet_at(notit
) ==
499 notit
->cur_exp_packet_total_size
) {
503 if (packet_at(notit
) == 0) {
507 if (notit
->buf
.last_eh_at
!= SIZE_MAX
&&
508 notit
->buf
.at
== notit
->buf
.last_eh_at
) {
513 /* All other states are invalid */
514 BT_LOGW("User function returned %s, but message iterator is in an unexpected state: "
515 "state=%s, cur-packet-size=%" PRId64
", cur=%zu, "
516 "packet-cur=%zu, last-eh-at=%zu",
517 bt_msg_iter_medium_status_string(m_status
),
518 state_string(notit
->state
),
519 notit
->cur_exp_packet_total_size
,
520 notit
->buf
.at
, packet_at(notit
),
521 notit
->buf
.last_eh_at
);
522 m_status
= BT_MSG_ITER_MEDIUM_STATUS_ERROR
;
523 } else if (m_status
< 0) {
524 BT_LOGW("User function failed: status=%s",
525 bt_msg_iter_medium_status_string(m_status
));
529 return msg_iter_status_from_m_status(m_status
);
533 enum bt_msg_iter_status
buf_ensure_available_bits(
534 struct bt_msg_iter
*notit
)
536 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
538 if (G_UNLIKELY(buf_available_bits(notit
) == 0)) {
540 * This _cannot_ return BT_MSG_ITER_STATUS_OK
543 status
= request_medium_bytes(notit
);
550 enum bt_msg_iter_status
read_dscope_begin_state(
551 struct bt_msg_iter
*notit
,
552 struct ctf_field_class
*dscope_fc
,
553 enum state done_state
, enum state continue_state
,
554 bt_field
*dscope_field
)
556 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
557 enum bt_bfcr_status bfcr_status
;
558 size_t consumed_bits
;
560 notit
->cur_dscope_field
= dscope_field
;
561 BT_LOGV("Starting BFCR: notit-addr=%p, bfcr-addr=%p, fc-addr=%p",
562 notit
, notit
->bfcr
, dscope_fc
);
563 consumed_bits
= bt_bfcr_start(notit
->bfcr
, dscope_fc
,
564 notit
->buf
.addr
, notit
->buf
.at
, packet_at(notit
),
565 notit
->buf
.sz
, &bfcr_status
);
566 BT_LOGV("BFCR consumed bits: size=%zu", consumed_bits
);
568 switch (bfcr_status
) {
569 case BT_BFCR_STATUS_OK
:
570 /* Field class was read completely */
571 BT_LOGV_STR("Field was completely decoded.");
572 notit
->state
= done_state
;
574 case BT_BFCR_STATUS_EOF
:
575 BT_LOGV_STR("BFCR needs more data to decode field completely.");
576 notit
->state
= continue_state
;
579 BT_LOGW("BFCR failed to start: notit-addr=%p, bfcr-addr=%p, "
580 "status=%s", notit
, notit
->bfcr
,
581 bt_bfcr_status_string(bfcr_status
));
582 status
= BT_MSG_ITER_STATUS_ERROR
;
586 /* Consume bits now since we know we're not in an error state */
587 buf_consume_bits(notit
, consumed_bits
);
594 enum bt_msg_iter_status
read_dscope_continue_state(
595 struct bt_msg_iter
*notit
, enum state done_state
)
597 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
598 enum bt_bfcr_status bfcr_status
;
599 size_t consumed_bits
;
601 BT_LOGV("Continuing BFCR: notit-addr=%p, bfcr-addr=%p",
604 status
= buf_ensure_available_bits(notit
);
605 if (status
!= BT_MSG_ITER_STATUS_OK
) {
607 BT_LOGW("Cannot ensure that buffer has at least one byte: "
608 "msg-addr=%p, status=%s",
609 notit
, bt_msg_iter_status_string(status
));
611 BT_LOGV("Cannot ensure that buffer has at least one byte: "
612 "msg-addr=%p, status=%s",
613 notit
, bt_msg_iter_status_string(status
));
619 consumed_bits
= bt_bfcr_continue(notit
->bfcr
, notit
->buf
.addr
,
620 notit
->buf
.sz
, &bfcr_status
);
621 BT_LOGV("BFCR consumed bits: size=%zu", consumed_bits
);
623 switch (bfcr_status
) {
624 case BT_BFCR_STATUS_OK
:
625 /* Type was read completely. */
626 BT_LOGV_STR("Field was completely decoded.");
627 notit
->state
= done_state
;
629 case BT_BFCR_STATUS_EOF
:
630 /* Stay in this continue state. */
631 BT_LOGV_STR("BFCR needs more data to decode field completely.");
634 BT_LOGW("BFCR failed to continue: notit-addr=%p, bfcr-addr=%p, "
635 "status=%s", notit
, notit
->bfcr
,
636 bt_bfcr_status_string(bfcr_status
));
637 status
= BT_MSG_ITER_STATUS_ERROR
;
641 /* Consume bits now since we know we're not in an error state. */
642 buf_consume_bits(notit
, consumed_bits
);
648 void release_event_dscopes(struct bt_msg_iter
*notit
)
650 notit
->dscopes
.event_common_context
= NULL
;
651 notit
->dscopes
.event_spec_context
= NULL
;
652 notit
->dscopes
.event_payload
= NULL
;
656 void release_all_dscopes(struct bt_msg_iter
*notit
)
658 notit
->dscopes
.stream_packet_context
= NULL
;
660 if (notit
->packet_context_field
) {
661 bt_packet_context_field_release(notit
->packet_context_field
);
662 notit
->packet_context_field
= NULL
;
665 release_event_dscopes(notit
);
669 enum bt_msg_iter_status
read_packet_header_begin_state(
670 struct bt_msg_iter
*notit
)
672 struct ctf_field_class
*packet_header_fc
= NULL
;
673 enum bt_msg_iter_status ret
= BT_MSG_ITER_STATUS_OK
;
675 if (bt_msg_iter_switch_packet(notit
)) {
676 BT_LOGW("Cannot switch packet: notit-addr=%p", notit
);
677 ret
= BT_MSG_ITER_STATUS_ERROR
;
682 * Make sure at least one bit is available for this packet. An
683 * empty packet is impossible. If we reach the end of the medium
684 * at this point, then it's considered the end of the stream.
686 ret
= buf_ensure_available_bits(notit
);
688 case BT_MSG_ITER_STATUS_OK
:
690 case BT_MSG_ITER_STATUS_EOF
:
691 ret
= BT_MSG_ITER_STATUS_OK
;
692 notit
->state
= STATE_CHECK_EMIT_MSG_STREAM_ACTIVITY_END
;
698 /* Packet header class is common to the whole trace class. */
699 packet_header_fc
= notit
->meta
.tc
->packet_header_fc
;
700 if (!packet_header_fc
) {
701 notit
->state
= STATE_AFTER_TRACE_PACKET_HEADER
;
705 notit
->cur_stream_class_id
= -1;
706 notit
->cur_event_class_id
= -1;
707 notit
->cur_data_stream_id
= -1;
708 BT_LOGV("Decoding packet header field:"
709 "notit-addr=%p, trace-class-addr=%p, fc-addr=%p",
710 notit
, notit
->meta
.tc
, packet_header_fc
);
711 ret
= read_dscope_begin_state(notit
, packet_header_fc
,
712 STATE_AFTER_TRACE_PACKET_HEADER
,
713 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
, NULL
);
715 BT_LOGW("Cannot decode packet header field: "
716 "notit-addr=%p, trace-class-addr=%p, "
718 notit
, notit
->meta
.tc
, packet_header_fc
);
726 enum bt_msg_iter_status
read_packet_header_continue_state(
727 struct bt_msg_iter
*notit
)
729 return read_dscope_continue_state(notit
,
730 STATE_AFTER_TRACE_PACKET_HEADER
);
734 enum bt_msg_iter_status
set_current_stream_class(struct bt_msg_iter
*notit
)
736 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
737 struct ctf_stream_class
*new_stream_class
= NULL
;
739 if (notit
->cur_stream_class_id
== -1) {
741 * No current stream class ID field, therefore only one
744 if (notit
->meta
.tc
->stream_classes
->len
!= 1) {
745 BT_LOGW("Need exactly one stream class since there's "
746 "no stream class ID field: "
747 "notit-addr=%p", notit
);
748 status
= BT_MSG_ITER_STATUS_ERROR
;
752 new_stream_class
= notit
->meta
.tc
->stream_classes
->pdata
[0];
753 notit
->cur_stream_class_id
= new_stream_class
->id
;
756 new_stream_class
= ctf_trace_class_borrow_stream_class_by_id(
757 notit
->meta
.tc
, notit
->cur_stream_class_id
);
758 if (!new_stream_class
) {
759 BT_LOGW("No stream class with ID of stream class ID to use in trace class: "
760 "notit-addr=%p, stream-class-id=%" PRIu64
", "
761 "trace-class-addr=%p",
762 notit
, notit
->cur_stream_class_id
, notit
->meta
.tc
);
763 status
= BT_MSG_ITER_STATUS_ERROR
;
767 if (notit
->meta
.sc
) {
768 if (new_stream_class
!= notit
->meta
.sc
) {
769 BT_LOGW("Two packets refer to two different stream classes within the same packet sequence: "
770 "notit-addr=%p, prev-stream-class-addr=%p, "
771 "prev-stream-class-id=%" PRId64
", "
772 "next-stream-class-addr=%p, "
773 "next-stream-class-id=%" PRId64
", "
775 notit
, notit
->meta
.sc
,
778 new_stream_class
->id
,
780 status
= BT_MSG_ITER_STATUS_ERROR
;
784 notit
->meta
.sc
= new_stream_class
;
787 BT_LOGV("Set current stream class: "
788 "notit-addr=%p, stream-class-addr=%p, "
789 "stream-class-id=%" PRId64
,
790 notit
, notit
->meta
.sc
, notit
->meta
.sc
->id
);
797 enum bt_msg_iter_status
set_current_stream(struct bt_msg_iter
*notit
)
799 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
800 bt_stream
*stream
= NULL
;
802 BT_LOGV("Calling user function (get stream): notit-addr=%p, "
803 "stream-class-addr=%p, stream-class-id=%" PRId64
,
804 notit
, notit
->meta
.sc
,
806 stream
= notit
->medium
.medops
.borrow_stream(
807 notit
->meta
.sc
->ir_sc
, notit
->cur_data_stream_id
,
809 bt_stream_get_ref(stream
);
810 BT_LOGV("User function returned: stream-addr=%p", stream
);
812 BT_LOGW_STR("User function failed to return a stream object "
813 "for the given stream class.");
814 status
= BT_MSG_ITER_STATUS_ERROR
;
818 if (notit
->stream
&& stream
!= notit
->stream
) {
819 BT_LOGW("User function returned a different stream than the "
820 "previous one for the same sequence of packets.");
821 status
= BT_MSG_ITER_STATUS_ERROR
;
825 BT_STREAM_MOVE_REF(notit
->stream
, stream
);
828 bt_stream_put_ref(stream
);
833 enum bt_msg_iter_status
set_current_packet(struct bt_msg_iter
*notit
)
835 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
836 bt_packet
*packet
= NULL
;
838 BT_LOGV("Creating packet for packet message: "
839 "notit-addr=%p", notit
);
840 BT_LOGV("Creating packet from stream: "
841 "notit-addr=%p, stream-addr=%p, "
842 "stream-class-addr=%p, "
843 "stream-class-id=%" PRId64
,
844 notit
, notit
->stream
, notit
->meta
.sc
,
848 BT_ASSERT(notit
->stream
);
849 packet
= bt_packet_create(notit
->stream
);
851 BT_LOGE("Cannot create packet from stream: "
852 "notit-addr=%p, stream-addr=%p, "
853 "stream-class-addr=%p, "
854 "stream-class-id=%" PRId64
,
855 notit
, notit
->stream
, notit
->meta
.sc
,
863 BT_PACKET_PUT_REF_AND_RESET(packet
);
864 status
= BT_MSG_ITER_STATUS_ERROR
;
867 BT_PACKET_MOVE_REF(notit
->packet
, packet
);
872 enum bt_msg_iter_status
after_packet_header_state(
873 struct bt_msg_iter
*notit
)
875 enum bt_msg_iter_status status
;
877 status
= set_current_stream_class(notit
);
878 if (status
!= BT_MSG_ITER_STATUS_OK
) {
882 notit
->state
= STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
;
889 enum bt_msg_iter_status
read_packet_context_begin_state(
890 struct bt_msg_iter
*notit
)
892 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
893 struct ctf_field_class
*packet_context_fc
;
895 BT_ASSERT(notit
->meta
.sc
);
896 packet_context_fc
= notit
->meta
.sc
->packet_context_fc
;
897 if (!packet_context_fc
) {
898 BT_LOGV("No packet packet context field class in stream class: continuing: "
899 "notit-addr=%p, stream-class-addr=%p, "
900 "stream-class-id=%" PRId64
,
901 notit
, notit
->meta
.sc
,
903 notit
->state
= STATE_AFTER_STREAM_PACKET_CONTEXT
;
907 BT_ASSERT(!notit
->packet_context_field
);
909 if (packet_context_fc
->in_ir
) {
911 * Create free packet context field from stream class.
912 * This field is going to be moved to the packet once we
913 * create it. We cannot create the packet now because a
914 * packet is created from a stream, and this API must be
915 * able to return the packet context properties without
917 * (bt_msg_iter_get_packet_properties()).
919 notit
->packet_context_field
=
920 bt_packet_context_field_create(
921 notit
->meta
.sc
->ir_sc
);
922 if (!notit
->packet_context_field
) {
923 BT_LOGE_STR("Cannot create packet context field wrapper from stream class.");
924 status
= BT_MSG_ITER_STATUS_ERROR
;
928 notit
->dscopes
.stream_packet_context
=
929 bt_packet_context_field_borrow_field(
930 notit
->packet_context_field
);
931 BT_ASSERT(notit
->dscopes
.stream_packet_context
);
934 BT_LOGV("Decoding packet context field: "
935 "notit-addr=%p, stream-class-addr=%p, "
936 "stream-class-id=%" PRId64
", fc-addr=%p",
937 notit
, notit
->meta
.sc
,
938 notit
->meta
.sc
->id
, packet_context_fc
);
939 status
= read_dscope_begin_state(notit
, packet_context_fc
,
940 STATE_AFTER_STREAM_PACKET_CONTEXT
,
941 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
942 notit
->dscopes
.stream_packet_context
);
944 BT_LOGW("Cannot decode packet context field: "
945 "notit-addr=%p, stream-class-addr=%p, "
946 "stream-class-id=%" PRId64
", fc-addr=%p",
947 notit
, notit
->meta
.sc
,
957 enum bt_msg_iter_status
read_packet_context_continue_state(
958 struct bt_msg_iter
*notit
)
960 return read_dscope_continue_state(notit
,
961 STATE_AFTER_STREAM_PACKET_CONTEXT
);
965 enum bt_msg_iter_status
set_current_packet_content_sizes(
966 struct bt_msg_iter
*notit
)
968 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
970 if (notit
->cur_exp_packet_total_size
== -1) {
971 if (notit
->cur_exp_packet_content_size
!= -1) {
972 notit
->cur_exp_packet_total_size
=
973 notit
->cur_exp_packet_content_size
;
976 if (notit
->cur_exp_packet_content_size
== -1) {
977 notit
->cur_exp_packet_content_size
=
978 notit
->cur_exp_packet_total_size
;
982 BT_ASSERT((notit
->cur_exp_packet_total_size
>= 0 &&
983 notit
->cur_exp_packet_content_size
>= 0) ||
984 (notit
->cur_exp_packet_total_size
< 0 &&
985 notit
->cur_exp_packet_content_size
< 0));
987 if (notit
->cur_exp_packet_content_size
>
988 notit
->cur_exp_packet_total_size
) {
989 BT_LOGW("Invalid packet or content size: "
990 "content size is greater than packet size: "
991 "notit-addr=%p, packet-context-field-addr=%p, "
992 "packet-size=%" PRId64
", content-size=%" PRId64
,
993 notit
, notit
->dscopes
.stream_packet_context
,
994 notit
->cur_exp_packet_total_size
,
995 notit
->cur_exp_packet_content_size
);
996 status
= BT_MSG_ITER_STATUS_ERROR
;
1000 BT_LOGV("Set current packet and content sizes: "
1001 "notit-addr=%p, packet-size=%" PRIu64
", content-size=%" PRIu64
,
1002 notit
, notit
->cur_exp_packet_total_size
,
1003 notit
->cur_exp_packet_content_size
);
1010 enum bt_msg_iter_status
after_packet_context_state(struct bt_msg_iter
*notit
)
1012 enum bt_msg_iter_status status
;
1014 status
= set_current_packet_content_sizes(notit
);
1015 if (status
!= BT_MSG_ITER_STATUS_OK
) {
1019 if (notit
->stream
) {
1021 * Stream exists, which means we already emitted at
1022 * least one packet beginning message, so the initial
1023 * stream beginning message was also emitted.
1025 notit
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS
;
1027 notit
->state
= STATE_CHECK_EMIT_MSG_STREAM_BEGINNING
;
1035 enum bt_msg_iter_status
read_event_header_begin_state(struct bt_msg_iter
*notit
)
1037 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1038 struct ctf_field_class
*event_header_fc
= NULL
;
1040 /* Reset the position of the last event header */
1041 notit
->buf
.last_eh_at
= notit
->buf
.at
;
1042 notit
->cur_event_class_id
= -1;
1044 /* Check if we have some content left */
1045 if (notit
->cur_exp_packet_content_size
>= 0) {
1046 if (G_UNLIKELY(packet_at(notit
) ==
1047 notit
->cur_exp_packet_content_size
)) {
1048 /* No more events! */
1049 BT_LOGV("Reached end of packet: notit-addr=%p, "
1050 "cur=%zu", notit
, packet_at(notit
));
1051 notit
->state
= STATE_EMIT_MSG_PACKET_END_MULTI
;
1053 } else if (G_UNLIKELY(packet_at(notit
) >
1054 notit
->cur_exp_packet_content_size
)) {
1055 /* That's not supposed to happen */
1056 BT_LOGV("Before decoding event header field: cursor is passed the packet's content: "
1057 "notit-addr=%p, content-size=%" PRId64
", "
1059 notit
->cur_exp_packet_content_size
,
1061 status
= BT_MSG_ITER_STATUS_ERROR
;
1066 * "Infinite" content: we're done when the medium has
1067 * nothing else for us.
1069 status
= buf_ensure_available_bits(notit
);
1071 case BT_MSG_ITER_STATUS_OK
:
1073 case BT_MSG_ITER_STATUS_EOF
:
1074 status
= BT_MSG_ITER_STATUS_OK
;
1075 notit
->state
= STATE_EMIT_MSG_PACKET_END_SINGLE
;
1082 release_event_dscopes(notit
);
1083 BT_ASSERT(notit
->meta
.sc
);
1084 event_header_fc
= notit
->meta
.sc
->event_header_fc
;
1085 if (!event_header_fc
) {
1086 notit
->state
= STATE_AFTER_EVENT_HEADER
;
1090 BT_LOGV("Decoding event header field: "
1091 "notit-addr=%p, stream-class-addr=%p, "
1092 "stream-class-id=%" PRId64
", "
1094 notit
, notit
->meta
.sc
,
1097 status
= read_dscope_begin_state(notit
, event_header_fc
,
1098 STATE_AFTER_EVENT_HEADER
,
1099 STATE_DSCOPE_EVENT_HEADER_CONTINUE
, NULL
);
1101 BT_LOGW("Cannot decode event header field: "
1102 "notit-addr=%p, stream-class-addr=%p, "
1103 "stream-class-id=%" PRId64
", fc-addr=%p",
1104 notit
, notit
->meta
.sc
,
1114 enum bt_msg_iter_status
read_event_header_continue_state(
1115 struct bt_msg_iter
*notit
)
1117 return read_dscope_continue_state(notit
,
1118 STATE_AFTER_EVENT_HEADER
);
1122 enum bt_msg_iter_status
set_current_event_class(struct bt_msg_iter
*notit
)
1124 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1126 struct ctf_event_class
*new_event_class
= NULL
;
1128 if (notit
->cur_event_class_id
== -1) {
1130 * No current event class ID field, therefore only one
1133 if (notit
->meta
.sc
->event_classes
->len
!= 1) {
1134 BT_LOGW("Need exactly one event class since there's "
1135 "no event class ID field: "
1136 "notit-addr=%p", notit
);
1137 status
= BT_MSG_ITER_STATUS_ERROR
;
1141 new_event_class
= notit
->meta
.sc
->event_classes
->pdata
[0];
1142 notit
->cur_event_class_id
= new_event_class
->id
;
1145 new_event_class
= ctf_stream_class_borrow_event_class_by_id(
1146 notit
->meta
.sc
, notit
->cur_event_class_id
);
1147 if (!new_event_class
) {
1148 BT_LOGW("No event class with ID of event class ID to use in stream class: "
1149 "notit-addr=%p, stream-class-id=%" PRIu64
", "
1150 "event-class-id=%" PRIu64
", "
1151 "trace-class-addr=%p",
1152 notit
, notit
->meta
.sc
->id
, notit
->cur_event_class_id
,
1154 status
= BT_MSG_ITER_STATUS_ERROR
;
1158 notit
->meta
.ec
= new_event_class
;
1159 BT_LOGV("Set current event class: "
1160 "notit-addr=%p, event-class-addr=%p, "
1161 "event-class-id=%" PRId64
", "
1162 "event-class-name=\"%s\"",
1163 notit
, notit
->meta
.ec
, notit
->meta
.ec
->id
,
1164 notit
->meta
.ec
->name
->str
);
1171 enum bt_msg_iter_status
set_current_event_message(
1172 struct bt_msg_iter
*notit
)
1174 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1175 bt_message
*msg
= NULL
;
1177 BT_ASSERT(notit
->meta
.ec
);
1178 BT_ASSERT(notit
->packet
);
1179 BT_LOGV("Creating event message from event class and packet: "
1180 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", packet-addr=%p",
1181 notit
, notit
->meta
.ec
,
1182 notit
->meta
.ec
->name
->str
,
1184 BT_ASSERT(notit
->msg_iter
);
1185 BT_ASSERT(notit
->meta
.sc
);
1187 if (bt_stream_class_borrow_default_clock_class(notit
->meta
.sc
->ir_sc
)) {
1188 msg
= bt_message_event_create_with_default_clock_snapshot(
1189 notit
->msg_iter
, notit
->meta
.ec
->ir_ec
,
1190 notit
->packet
, notit
->default_clock_snapshot
);
1192 msg
= bt_message_event_create(notit
->msg_iter
,
1193 notit
->meta
.ec
->ir_ec
, notit
->packet
);
1197 BT_LOGE("Cannot create event message: "
1198 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", "
1200 notit
, notit
->meta
.ec
,
1201 notit
->meta
.ec
->name
->str
,
1209 BT_MESSAGE_PUT_REF_AND_RESET(msg
);
1210 status
= BT_MSG_ITER_STATUS_ERROR
;
1213 BT_MESSAGE_MOVE_REF(notit
->event_msg
, msg
);
1218 enum bt_msg_iter_status
after_event_header_state(
1219 struct bt_msg_iter
*notit
)
1221 enum bt_msg_iter_status status
;
1223 status
= set_current_event_class(notit
);
1224 if (status
!= BT_MSG_ITER_STATUS_OK
) {
1228 status
= set_current_event_message(notit
);
1229 if (status
!= BT_MSG_ITER_STATUS_OK
) {
1233 notit
->event
= bt_message_event_borrow_event(
1235 BT_ASSERT(notit
->event
);
1236 notit
->state
= STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
;
1243 enum bt_msg_iter_status
read_event_common_context_begin_state(
1244 struct bt_msg_iter
*notit
)
1246 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1247 struct ctf_field_class
*event_common_context_fc
;
1249 event_common_context_fc
= notit
->meta
.sc
->event_common_context_fc
;
1250 if (!event_common_context_fc
) {
1251 notit
->state
= STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
;
1255 if (event_common_context_fc
->in_ir
) {
1256 BT_ASSERT(!notit
->dscopes
.event_common_context
);
1257 notit
->dscopes
.event_common_context
=
1258 bt_event_borrow_common_context_field(
1260 BT_ASSERT(notit
->dscopes
.event_common_context
);
1263 BT_LOGV("Decoding event common context field: "
1264 "notit-addr=%p, stream-class-addr=%p, "
1265 "stream-class-id=%" PRId64
", "
1267 notit
, notit
->meta
.sc
,
1269 event_common_context_fc
);
1270 status
= read_dscope_begin_state(notit
, event_common_context_fc
,
1271 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
,
1272 STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
,
1273 notit
->dscopes
.event_common_context
);
1275 BT_LOGW("Cannot decode event common context field: "
1276 "notit-addr=%p, stream-class-addr=%p, "
1277 "stream-class-id=%" PRId64
", fc-addr=%p",
1278 notit
, notit
->meta
.sc
,
1280 event_common_context_fc
);
1288 enum bt_msg_iter_status
read_event_common_context_continue_state(
1289 struct bt_msg_iter
*notit
)
1291 return read_dscope_continue_state(notit
,
1292 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
);
1296 enum bt_msg_iter_status
read_event_spec_context_begin_state(
1297 struct bt_msg_iter
*notit
)
1299 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1300 struct ctf_field_class
*event_spec_context_fc
;
1302 event_spec_context_fc
= notit
->meta
.ec
->spec_context_fc
;
1303 if (!event_spec_context_fc
) {
1304 notit
->state
= STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
;
1308 if (event_spec_context_fc
->in_ir
) {
1309 BT_ASSERT(!notit
->dscopes
.event_spec_context
);
1310 notit
->dscopes
.event_spec_context
=
1311 bt_event_borrow_specific_context_field(
1313 BT_ASSERT(notit
->dscopes
.event_spec_context
);
1316 BT_LOGV("Decoding event specific context field: "
1317 "notit-addr=%p, event-class-addr=%p, "
1318 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1320 notit
, notit
->meta
.ec
,
1321 notit
->meta
.ec
->name
->str
,
1323 event_spec_context_fc
);
1324 status
= read_dscope_begin_state(notit
, event_spec_context_fc
,
1325 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
1326 STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
,
1327 notit
->dscopes
.event_spec_context
);
1329 BT_LOGW("Cannot decode event specific context field: "
1330 "notit-addr=%p, event-class-addr=%p, "
1331 "event-class-name=\"%s\", "
1332 "event-class-id=%" PRId64
", fc-addr=%p",
1333 notit
, notit
->meta
.ec
,
1334 notit
->meta
.ec
->name
->str
,
1336 event_spec_context_fc
);
1344 enum bt_msg_iter_status
read_event_spec_context_continue_state(
1345 struct bt_msg_iter
*notit
)
1347 return read_dscope_continue_state(notit
,
1348 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
);
1352 enum bt_msg_iter_status
read_event_payload_begin_state(
1353 struct bt_msg_iter
*notit
)
1355 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1356 struct ctf_field_class
*event_payload_fc
;
1358 event_payload_fc
= notit
->meta
.ec
->payload_fc
;
1359 if (!event_payload_fc
) {
1360 notit
->state
= STATE_EMIT_MSG_EVENT
;
1364 if (event_payload_fc
->in_ir
) {
1365 BT_ASSERT(!notit
->dscopes
.event_payload
);
1366 notit
->dscopes
.event_payload
=
1367 bt_event_borrow_payload_field(
1369 BT_ASSERT(notit
->dscopes
.event_payload
);
1372 BT_LOGV("Decoding event payload field: "
1373 "notit-addr=%p, event-class-addr=%p, "
1374 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1376 notit
, notit
->meta
.ec
,
1377 notit
->meta
.ec
->name
->str
,
1380 status
= read_dscope_begin_state(notit
, event_payload_fc
,
1381 STATE_EMIT_MSG_EVENT
,
1382 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
1383 notit
->dscopes
.event_payload
);
1385 BT_LOGW("Cannot decode event payload field: "
1386 "notit-addr=%p, event-class-addr=%p, "
1387 "event-class-name=\"%s\", "
1388 "event-class-id=%" PRId64
", fc-addr=%p",
1389 notit
, notit
->meta
.ec
,
1390 notit
->meta
.ec
->name
->str
,
1400 enum bt_msg_iter_status
read_event_payload_continue_state(
1401 struct bt_msg_iter
*notit
)
1403 return read_dscope_continue_state(notit
, STATE_EMIT_MSG_EVENT
);
1407 enum bt_msg_iter_status
skip_packet_padding_state(struct bt_msg_iter
*notit
)
1409 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1410 size_t bits_to_skip
;
1412 BT_ASSERT(notit
->cur_exp_packet_total_size
> 0);
1413 bits_to_skip
= notit
->cur_exp_packet_total_size
- packet_at(notit
);
1414 if (bits_to_skip
== 0) {
1415 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1418 size_t bits_to_consume
;
1420 BT_LOGV("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu",
1421 bits_to_skip
, notit
, bits_to_skip
);
1422 status
= buf_ensure_available_bits(notit
);
1423 if (status
!= BT_MSG_ITER_STATUS_OK
) {
1427 bits_to_consume
= MIN(buf_available_bits(notit
), bits_to_skip
);
1428 BT_LOGV("Skipping %zu bits of padding: notit-addr=%p, size=%zu",
1429 bits_to_consume
, notit
, bits_to_consume
);
1430 buf_consume_bits(notit
, bits_to_consume
);
1431 bits_to_skip
= notit
->cur_exp_packet_total_size
-
1433 if (bits_to_skip
== 0) {
1434 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1444 enum bt_msg_iter_status
check_emit_msg_stream_beginning_state(
1445 struct bt_msg_iter
*notit
)
1447 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1449 if (notit
->set_stream
) {
1450 status
= set_current_stream(notit
);
1451 if (status
!= BT_MSG_ITER_STATUS_OK
) {
1456 if (notit
->emit_stream_begin_msg
) {
1457 notit
->state
= STATE_EMIT_MSG_STREAM_BEGINNING
;
1459 /* Stream's first packet */
1460 notit
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS
;
1468 enum bt_msg_iter_status
check_emit_msg_discarded_events(
1469 struct bt_msg_iter
*notit
)
1471 notit
->state
= STATE_EMIT_MSG_DISCARDED_EVENTS
;
1473 if (!notit
->meta
.sc
->has_discarded_events
) {
1474 notit
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
;
1478 if (notit
->prev_packet_snapshots
.discarded_events
== UINT64_C(-1)) {
1479 if (notit
->snapshots
.discarded_events
== 0 ||
1480 notit
->snapshots
.discarded_events
== UINT64_C(-1)) {
1482 * Stream's first packet with no discarded
1483 * events or no information about discarded
1484 * events: do not emit.
1486 notit
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
;
1490 * If the previous packet has a value for this counter,
1491 * then this counter is defined for the whole stream.
1493 BT_ASSERT(notit
->snapshots
.discarded_events
!= UINT64_C(-1));
1495 if (notit
->snapshots
.discarded_events
-
1496 notit
->prev_packet_snapshots
.discarded_events
== 0) {
1498 * No discarded events since previous packet: do
1501 notit
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
;
1506 return BT_MSG_ITER_STATUS_OK
;
1510 enum bt_msg_iter_status
check_emit_msg_discarded_packets(
1511 struct bt_msg_iter
*notit
)
1513 notit
->state
= STATE_EMIT_MSG_DISCARDED_PACKETS
;
1515 if (!notit
->meta
.sc
->has_discarded_packets
) {
1516 notit
->state
= STATE_EMIT_MSG_PACKET_BEGINNING
;
1520 if (notit
->prev_packet_snapshots
.packets
== UINT64_C(-1)) {
1522 * Stream's first packet or no information about
1523 * discarded packets: do not emit. In other words, if
1524 * this is the first packet and its sequence number is
1525 * not 0, do not consider that packets were previously
1526 * lost: we might be reading a partial stream (LTTng
1527 * snapshot for example).
1529 notit
->state
= STATE_EMIT_MSG_PACKET_BEGINNING
;
1532 * If the previous packet has a value for this counter,
1533 * then this counter is defined for the whole stream.
1535 BT_ASSERT(notit
->snapshots
.packets
!= UINT64_C(-1));
1537 if (notit
->snapshots
.packets
-
1538 notit
->prev_packet_snapshots
.packets
<= 1) {
1540 * No discarded packets since previous packet:
1543 notit
->state
= STATE_EMIT_MSG_PACKET_BEGINNING
;
1548 return BT_MSG_ITER_STATUS_OK
;
1552 enum bt_msg_iter_status
check_emit_msg_stream_activity_end(
1553 struct bt_msg_iter
*notit
)
1555 if (notit
->emit_stream_end_msg
) {
1556 notit
->state
= STATE_EMIT_MSG_STREAM_ACTIVITY_END
;
1558 notit
->state
= STATE_DONE
;
1561 return BT_MSG_ITER_STATUS_OK
;
1565 enum bt_msg_iter_status
handle_state(struct bt_msg_iter
*notit
)
1567 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1568 const enum state state
= notit
->state
;
1570 BT_LOGV("Handling state: notit-addr=%p, state=%s",
1571 notit
, state_string(state
));
1573 // TODO: optimalize!
1576 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1578 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
1579 status
= read_packet_header_begin_state(notit
);
1581 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
1582 status
= read_packet_header_continue_state(notit
);
1584 case STATE_AFTER_TRACE_PACKET_HEADER
:
1585 status
= after_packet_header_state(notit
);
1587 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
1588 status
= read_packet_context_begin_state(notit
);
1590 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
1591 status
= read_packet_context_continue_state(notit
);
1593 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
1594 status
= after_packet_context_state(notit
);
1596 case STATE_CHECK_EMIT_MSG_STREAM_BEGINNING
:
1597 status
= check_emit_msg_stream_beginning_state(notit
);
1599 case STATE_EMIT_MSG_STREAM_BEGINNING
:
1600 notit
->state
= STATE_EMIT_MSG_STREAM_ACTIVITY_BEGINNING
;
1602 case STATE_EMIT_MSG_STREAM_ACTIVITY_BEGINNING
:
1603 notit
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS
;
1605 case STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS
:
1606 status
= check_emit_msg_discarded_events(notit
);
1608 case STATE_EMIT_MSG_DISCARDED_EVENTS
:
1609 notit
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
;
1611 case STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
:
1612 status
= check_emit_msg_discarded_packets(notit
);
1614 case STATE_EMIT_MSG_DISCARDED_PACKETS
:
1615 notit
->state
= STATE_EMIT_MSG_PACKET_BEGINNING
;
1617 case STATE_EMIT_MSG_PACKET_BEGINNING
:
1618 notit
->state
= STATE_DSCOPE_EVENT_HEADER_BEGIN
;
1620 case STATE_DSCOPE_EVENT_HEADER_BEGIN
:
1621 status
= read_event_header_begin_state(notit
);
1623 case STATE_DSCOPE_EVENT_HEADER_CONTINUE
:
1624 status
= read_event_header_continue_state(notit
);
1626 case STATE_AFTER_EVENT_HEADER
:
1627 status
= after_event_header_state(notit
);
1629 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
:
1630 status
= read_event_common_context_begin_state(notit
);
1632 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
:
1633 status
= read_event_common_context_continue_state(notit
);
1635 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
:
1636 status
= read_event_spec_context_begin_state(notit
);
1638 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
:
1639 status
= read_event_spec_context_continue_state(notit
);
1641 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
1642 status
= read_event_payload_begin_state(notit
);
1644 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
1645 status
= read_event_payload_continue_state(notit
);
1647 case STATE_EMIT_MSG_EVENT
:
1648 notit
->state
= STATE_DSCOPE_EVENT_HEADER_BEGIN
;
1650 case STATE_SKIP_PACKET_PADDING
:
1651 status
= skip_packet_padding_state(notit
);
1653 case STATE_EMIT_MSG_PACKET_END_MULTI
:
1654 notit
->state
= STATE_SKIP_PACKET_PADDING
;
1656 case STATE_EMIT_MSG_PACKET_END_SINGLE
:
1657 notit
->state
= STATE_CHECK_EMIT_MSG_STREAM_ACTIVITY_END
;
1659 case STATE_CHECK_EMIT_MSG_STREAM_ACTIVITY_END
:
1660 status
= check_emit_msg_stream_activity_end(notit
);
1662 case STATE_EMIT_MSG_STREAM_ACTIVITY_END
:
1663 notit
->state
= STATE_EMIT_MSG_STREAM_END
;
1665 case STATE_EMIT_MSG_STREAM_END
:
1666 notit
->state
= STATE_DONE
;
1671 BT_LOGD("Unknown CTF plugin message iterator state: "
1672 "notit-addr=%p, state=%d", notit
, notit
->state
);
1676 BT_LOGV("Handled state: notit-addr=%p, status=%s, "
1677 "prev-state=%s, cur-state=%s",
1678 notit
, bt_msg_iter_status_string(status
),
1679 state_string(state
), state_string(notit
->state
));
1684 void bt_msg_iter_reset_for_next_stream_file(struct bt_msg_iter
*notit
)
1687 BT_LOGD("Resetting message iterator: addr=%p", notit
);
1688 stack_clear(notit
->stack
);
1689 notit
->meta
.sc
= NULL
;
1690 notit
->meta
.ec
= NULL
;
1691 BT_PACKET_PUT_REF_AND_RESET(notit
->packet
);
1692 BT_STREAM_PUT_REF_AND_RESET(notit
->stream
);
1693 BT_MESSAGE_PUT_REF_AND_RESET(notit
->event_msg
);
1694 release_all_dscopes(notit
);
1695 notit
->cur_dscope_field
= NULL
;
1697 if (notit
->packet_context_field
) {
1698 bt_packet_context_field_release(notit
->packet_context_field
);
1699 notit
->packet_context_field
= NULL
;
1702 notit
->buf
.addr
= NULL
;
1705 notit
->buf
.last_eh_at
= SIZE_MAX
;
1706 notit
->buf
.packet_offset
= 0;
1707 notit
->state
= STATE_INIT
;
1708 notit
->cur_exp_packet_content_size
= -1;
1709 notit
->cur_exp_packet_total_size
= -1;
1710 notit
->cur_packet_offset
= -1;
1711 notit
->cur_event_class_id
= -1;
1712 notit
->snapshots
.beginning_clock
= UINT64_C(-1);
1713 notit
->snapshots
.end_clock
= UINT64_C(-1);
1717 * Resets the internal state of a CTF message iterator.
1720 void bt_msg_iter_reset(struct bt_msg_iter
*notit
)
1722 bt_msg_iter_reset_for_next_stream_file(notit
);
1723 notit
->cur_stream_class_id
= -1;
1724 notit
->cur_data_stream_id
= -1;
1725 notit
->emit_stream_begin_msg
= true;
1726 notit
->emit_stream_end_msg
= true;
1727 notit
->snapshots
.discarded_events
= UINT64_C(-1);
1728 notit
->snapshots
.packets
= UINT64_C(-1);
1729 notit
->prev_packet_snapshots
.discarded_events
= UINT64_C(-1);
1730 notit
->prev_packet_snapshots
.packets
= UINT64_C(-1);
1731 notit
->prev_packet_snapshots
.beginning_clock
= UINT64_C(-1);
1732 notit
->prev_packet_snapshots
.end_clock
= UINT64_C(-1);
1736 int bt_msg_iter_switch_packet(struct bt_msg_iter
*notit
)
1741 * We don't put the stream class here because we need to make
1742 * sure that all the packets processed by the same message
1743 * iterator refer to the same stream class (the first one).
1747 if (notit
->cur_exp_packet_total_size
!= -1) {
1748 notit
->cur_packet_offset
+= notit
->cur_exp_packet_total_size
;
1751 BT_LOGV("Switching packet: notit-addr=%p, cur=%zu, "
1752 "packet-offset=%" PRId64
, notit
, notit
->buf
.at
,
1753 notit
->cur_packet_offset
);
1754 stack_clear(notit
->stack
);
1755 notit
->meta
.ec
= NULL
;
1756 BT_PACKET_PUT_REF_AND_RESET(notit
->packet
);
1757 BT_MESSAGE_PUT_REF_AND_RESET(notit
->event_msg
);
1758 release_all_dscopes(notit
);
1759 notit
->cur_dscope_field
= NULL
;
1762 * Adjust current buffer so that addr points to the beginning of the new
1765 if (notit
->buf
.addr
) {
1766 size_t consumed_bytes
= (size_t) (notit
->buf
.at
/ CHAR_BIT
);
1768 /* Packets are assumed to start on a byte frontier. */
1769 if (notit
->buf
.at
% CHAR_BIT
) {
1770 BT_LOGW("Cannot switch packet: current position is not a multiple of 8: "
1771 "notit-addr=%p, cur=%zu", notit
, notit
->buf
.at
);
1776 notit
->buf
.addr
+= consumed_bytes
;
1777 notit
->buf
.sz
-= consumed_bytes
;
1779 notit
->buf
.packet_offset
= 0;
1780 BT_LOGV("Adjusted buffer: addr=%p, size=%zu",
1781 notit
->buf
.addr
, notit
->buf
.sz
);
1784 notit
->cur_exp_packet_content_size
= -1;
1785 notit
->cur_exp_packet_total_size
= -1;
1786 notit
->cur_stream_class_id
= -1;
1787 notit
->cur_event_class_id
= -1;
1788 notit
->cur_data_stream_id
= -1;
1789 notit
->prev_packet_snapshots
= notit
->snapshots
;
1790 notit
->snapshots
.discarded_events
= UINT64_C(-1);
1791 notit
->snapshots
.packets
= UINT64_C(-1);
1792 notit
->snapshots
.beginning_clock
= UINT64_C(-1);
1793 notit
->snapshots
.end_clock
= UINT64_C(-1);
1800 bt_field
*borrow_next_field(struct bt_msg_iter
*notit
)
1802 bt_field
*next_field
= NULL
;
1803 bt_field
*base_field
;
1804 const bt_field_class
*base_fc
;
1807 BT_ASSERT(!stack_empty(notit
->stack
));
1808 index
= stack_top(notit
->stack
)->index
;
1809 base_field
= stack_top(notit
->stack
)->base
;
1810 BT_ASSERT(base_field
);
1811 base_fc
= bt_field_borrow_class_const(base_field
);
1814 switch (bt_field_class_get_type(base_fc
)) {
1815 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
1818 bt_field_class_structure_get_member_count(
1819 bt_field_borrow_class_const(
1822 bt_field_structure_borrow_member_field_by_index(
1826 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
1827 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
1828 BT_ASSERT(index
< bt_field_array_get_length(base_field
));
1829 next_field
= bt_field_array_borrow_element_field_by_index(
1832 case BT_FIELD_CLASS_TYPE_VARIANT
:
1833 BT_ASSERT(index
== 0);
1834 next_field
= bt_field_variant_borrow_selected_option_field(
1841 BT_ASSERT(next_field
);
1846 void update_default_clock(struct bt_msg_iter
*notit
, uint64_t new_val
,
1847 uint64_t new_val_size
)
1849 uint64_t new_val_mask
;
1850 uint64_t cur_value_masked
;
1852 BT_ASSERT(new_val_size
> 0);
1855 * Special case for a 64-bit new value, which is the limit
1856 * of a clock value as of this version: overwrite the
1857 * current value directly.
1859 if (new_val_size
== 64) {
1860 notit
->default_clock_snapshot
= new_val
;
1864 new_val_mask
= (1ULL << new_val_size
) - 1;
1865 cur_value_masked
= notit
->default_clock_snapshot
& new_val_mask
;
1867 if (new_val
< cur_value_masked
) {
1869 * It looks like a wrap happened on the number of bits
1870 * of the requested new value. Assume that the clock
1871 * value wrapped only one time.
1873 notit
->default_clock_snapshot
+= new_val_mask
+ 1;
1876 /* Clear the low bits of the current clock value. */
1877 notit
->default_clock_snapshot
&= ~new_val_mask
;
1879 /* Set the low bits of the current clock value. */
1880 notit
->default_clock_snapshot
|= new_val
;
1883 BT_LOGV("Updated default clock's value from integer field's value: "
1884 "value=%" PRIu64
, notit
->default_clock_snapshot
);
1888 enum bt_bfcr_status
bfcr_unsigned_int_cb(uint64_t value
,
1889 struct ctf_field_class
*fc
, void *data
)
1891 struct bt_msg_iter
*notit
= data
;
1892 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
1893 bt_field
*field
= NULL
;
1894 struct ctf_field_class_int
*int_fc
= (void *) fc
;
1896 BT_LOGV("Unsigned integer function called from BFCR: "
1897 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1898 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64
,
1899 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
1901 if (G_LIKELY(int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
)) {
1902 goto update_def_clock
;
1905 switch (int_fc
->meaning
) {
1906 case CTF_FIELD_CLASS_MEANING_EVENT_CLASS_ID
:
1907 notit
->cur_event_class_id
= value
;
1909 case CTF_FIELD_CLASS_MEANING_DATA_STREAM_ID
:
1910 notit
->cur_data_stream_id
= value
;
1912 case CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME
:
1913 notit
->snapshots
.beginning_clock
= value
;
1915 case CTF_FIELD_CLASS_MEANING_PACKET_END_TIME
:
1916 notit
->snapshots
.end_clock
= value
;
1918 case CTF_FIELD_CLASS_MEANING_STREAM_CLASS_ID
:
1919 notit
->cur_stream_class_id
= value
;
1921 case CTF_FIELD_CLASS_MEANING_MAGIC
:
1922 if (value
!= 0xc1fc1fc1) {
1923 BT_LOGW("Invalid CTF magic number: notit-addr=%p, "
1924 "magic=%" PRIx64
, notit
, value
);
1925 status
= BT_BFCR_STATUS_ERROR
;
1930 case CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT
:
1931 notit
->snapshots
.packets
= value
;
1933 case CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT
:
1934 notit
->snapshots
.discarded_events
= value
;
1936 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_TOTAL_SIZE
:
1937 notit
->cur_exp_packet_total_size
= value
;
1939 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_CONTENT_SIZE
:
1940 notit
->cur_exp_packet_content_size
= value
;
1947 if (G_UNLIKELY(int_fc
->mapped_clock_class
)) {
1948 update_default_clock(notit
, value
, int_fc
->base
.size
);
1951 if (G_UNLIKELY(int_fc
->storing_index
>= 0)) {
1952 g_array_index(notit
->stored_values
, uint64_t,
1953 (uint64_t) int_fc
->storing_index
) = value
;
1956 if (G_UNLIKELY(!fc
->in_ir
)) {
1960 field
= borrow_next_field(notit
);
1962 BT_ASSERT(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
1963 BT_ASSERT(bt_field_get_class_type(field
) ==
1964 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
||
1965 bt_field_get_class_type(field
) ==
1966 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
);
1967 bt_field_unsigned_integer_set_value(field
, value
);
1968 stack_top(notit
->stack
)->index
++;
1975 enum bt_bfcr_status
bfcr_unsigned_int_char_cb(uint64_t value
,
1976 struct ctf_field_class
*fc
, void *data
)
1979 struct bt_msg_iter
*notit
= data
;
1980 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
1981 bt_field
*string_field
= NULL
;
1982 struct ctf_field_class_int
*int_fc
= (void *) fc
;
1983 char str
[2] = {'\0', '\0'};
1985 BT_LOGV("Unsigned integer character function called from BFCR: "
1986 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1987 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64
,
1988 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
1989 BT_ASSERT(int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
);
1990 BT_ASSERT(!int_fc
->mapped_clock_class
);
1991 BT_ASSERT(int_fc
->storing_index
< 0);
1993 if (G_UNLIKELY(!fc
->in_ir
)) {
1997 if (notit
->done_filling_string
) {
2002 notit
->done_filling_string
= true;
2006 string_field
= stack_top(notit
->stack
)->base
;
2007 BT_ASSERT(bt_field_get_class_type(string_field
) ==
2008 BT_FIELD_CLASS_TYPE_STRING
);
2010 /* Append character */
2011 str
[0] = (char) value
;
2012 ret
= bt_field_string_append_with_length(string_field
, str
, 1);
2014 BT_LOGE("Cannot append character to string field's value: "
2015 "notit-addr=%p, field-addr=%p, ret=%d",
2016 notit
, string_field
, ret
);
2017 status
= BT_BFCR_STATUS_ERROR
;
2026 enum bt_bfcr_status
bfcr_signed_int_cb(int64_t value
,
2027 struct ctf_field_class
*fc
, void *data
)
2029 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
2030 bt_field
*field
= NULL
;
2031 struct bt_msg_iter
*notit
= data
;
2032 struct ctf_field_class_int
*int_fc
= (void *) fc
;
2034 BT_LOGV("Signed integer function called from BFCR: "
2035 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2036 "fc-type=%d, fc-in-ir=%d, value=%" PRId64
,
2037 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
2038 BT_ASSERT(int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
);
2040 if (G_UNLIKELY(int_fc
->storing_index
>= 0)) {
2041 g_array_index(notit
->stored_values
, uint64_t,
2042 (uint64_t) int_fc
->storing_index
) = (uint64_t) value
;
2045 if (G_UNLIKELY(!fc
->in_ir
)) {
2049 field
= borrow_next_field(notit
);
2051 BT_ASSERT(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
2052 BT_ASSERT(bt_field_get_class_type(field
) ==
2053 BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
||
2054 bt_field_get_class_type(field
) ==
2055 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
);
2056 bt_field_signed_integer_set_value(field
, value
);
2057 stack_top(notit
->stack
)->index
++;
2064 enum bt_bfcr_status
bfcr_floating_point_cb(double value
,
2065 struct ctf_field_class
*fc
, void *data
)
2067 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
2068 bt_field
*field
= NULL
;
2069 struct bt_msg_iter
*notit
= data
;
2071 BT_LOGV("Floating point number function called from BFCR: "
2072 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2073 "fc-type=%d, fc-in-ir=%d, value=%f",
2074 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
2076 if (G_UNLIKELY(!fc
->in_ir
)) {
2080 field
= borrow_next_field(notit
);
2082 BT_ASSERT(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
2083 BT_ASSERT(bt_field_get_class_type(field
) ==
2084 BT_FIELD_CLASS_TYPE_REAL
);
2085 bt_field_real_set_value(field
, value
);
2086 stack_top(notit
->stack
)->index
++;
2093 enum bt_bfcr_status
bfcr_string_begin_cb(
2094 struct ctf_field_class
*fc
, void *data
)
2096 bt_field
*field
= NULL
;
2097 struct bt_msg_iter
*notit
= data
;
2100 BT_LOGV("String (beginning) function called from BFCR: "
2101 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2102 "fc-type=%d, fc-in-ir=%d",
2103 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
2105 if (G_UNLIKELY(!fc
->in_ir
)) {
2109 field
= borrow_next_field(notit
);
2111 BT_ASSERT(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
2112 BT_ASSERT(bt_field_get_class_type(field
) ==
2113 BT_FIELD_CLASS_TYPE_STRING
);
2114 ret
= bt_field_string_clear(field
);
2115 BT_ASSERT(ret
== 0);
2118 * Push on stack. Not a compound class per se, but we know that
2119 * only bfcr_string_cb() may be called between this call and a
2120 * subsequent call to bfcr_string_end_cb().
2122 stack_push(notit
->stack
, field
);
2125 return BT_BFCR_STATUS_OK
;
2129 enum bt_bfcr_status
bfcr_string_cb(const char *value
,
2130 size_t len
, struct ctf_field_class
*fc
, void *data
)
2132 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
2133 bt_field
*field
= NULL
;
2134 struct bt_msg_iter
*notit
= data
;
2137 BT_LOGV("String (substring) function called from BFCR: "
2138 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2139 "fc-type=%d, fc-in-ir=%d, string-length=%zu",
2140 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
,
2143 if (G_UNLIKELY(!fc
->in_ir
)) {
2147 field
= stack_top(notit
->stack
)->base
;
2150 /* Append current substring */
2151 ret
= bt_field_string_append_with_length(field
, value
, len
);
2153 BT_LOGE("Cannot append substring to string field's value: "
2154 "notit-addr=%p, field-addr=%p, string-length=%zu, "
2155 "ret=%d", notit
, field
, len
, ret
);
2156 status
= BT_BFCR_STATUS_ERROR
;
2165 enum bt_bfcr_status
bfcr_string_end_cb(
2166 struct ctf_field_class
*fc
, void *data
)
2168 struct bt_msg_iter
*notit
= data
;
2170 BT_LOGV("String (end) function called from BFCR: "
2171 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2172 "fc-type=%d, fc-in-ir=%d",
2173 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
2175 if (G_UNLIKELY(!fc
->in_ir
)) {
2179 /* Pop string field */
2180 stack_pop(notit
->stack
);
2182 /* Go to next field */
2183 stack_top(notit
->stack
)->index
++;
2186 return BT_BFCR_STATUS_OK
;
2189 enum bt_bfcr_status
bfcr_compound_begin_cb(
2190 struct ctf_field_class
*fc
, void *data
)
2192 struct bt_msg_iter
*notit
= data
;
2195 BT_LOGV("Compound (beginning) function called from BFCR: "
2196 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2197 "fc-type=%d, fc-in-ir=%d",
2198 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
2205 if (stack_empty(notit
->stack
)) {
2206 /* Root: already set by read_dscope_begin_state() */
2207 field
= notit
->cur_dscope_field
;
2209 field
= borrow_next_field(notit
);
2215 BT_ASSERT(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
2216 stack_push(notit
->stack
, field
);
2219 * Change BFCR "unsigned int" callback if it's a text
2222 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
||
2223 fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
2224 struct ctf_field_class_array_base
*array_fc
= (void *) fc
;
2226 if (array_fc
->is_text
) {
2229 BT_ASSERT(bt_field_get_class_type(field
) ==
2230 BT_FIELD_CLASS_TYPE_STRING
);
2231 notit
->done_filling_string
= false;
2232 ret
= bt_field_string_clear(field
);
2233 BT_ASSERT(ret
== 0);
2234 bt_bfcr_set_unsigned_int_cb(notit
->bfcr
,
2235 bfcr_unsigned_int_char_cb
);
2240 return BT_BFCR_STATUS_OK
;
2243 enum bt_bfcr_status
bfcr_compound_end_cb(
2244 struct ctf_field_class
*fc
, void *data
)
2246 struct bt_msg_iter
*notit
= data
;
2248 BT_LOGV("Compound (end) function called from BFCR: "
2249 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2250 "fc-type=%d, fc-in-ir=%d",
2251 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
2257 BT_ASSERT(!stack_empty(notit
->stack
));
2258 BT_ASSERT(bt_field_borrow_class_const(stack_top(notit
->stack
)->base
) ==
2262 * Reset BFCR "unsigned int" callback if it's a text
2265 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
||
2266 fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
2267 struct ctf_field_class_array_base
*array_fc
= (void *) fc
;
2269 if (array_fc
->is_text
) {
2270 BT_ASSERT(bt_field_get_class_type(
2271 stack_top(notit
->stack
)->base
) ==
2272 BT_FIELD_CLASS_TYPE_STRING
);
2273 bt_bfcr_set_unsigned_int_cb(notit
->bfcr
,
2274 bfcr_unsigned_int_cb
);
2279 stack_pop(notit
->stack
);
2281 /* If the stack is not empty, increment the base's index */
2282 if (!stack_empty(notit
->stack
)) {
2283 stack_top(notit
->stack
)->index
++;
2287 return BT_BFCR_STATUS_OK
;
2291 int64_t bfcr_get_sequence_length_cb(struct ctf_field_class
*fc
, void *data
)
2293 bt_field
*seq_field
;
2294 struct bt_msg_iter
*notit
= data
;
2295 struct ctf_field_class_sequence
*seq_fc
= (void *) fc
;
2296 int64_t length
= -1;
2299 length
= (uint64_t) g_array_index(notit
->stored_values
, uint64_t,
2300 seq_fc
->stored_length_index
);
2301 seq_field
= stack_top(notit
->stack
)->base
;
2302 BT_ASSERT(seq_field
);
2305 * bfcr_get_sequence_length_cb() also gets called back for a
2306 * text sequence, but the destination field is a string field.
2307 * Only set the field's sequence length if the destination field
2308 * is a sequence field.
2310 if (!seq_fc
->base
.is_text
) {
2311 BT_ASSERT(bt_field_get_class_type(seq_field
) ==
2312 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
);
2313 ret
= bt_field_dynamic_array_set_length(seq_field
,
2316 BT_LOGE("Cannot set dynamic array field's length field: "
2317 "notit-addr=%p, field-addr=%p, "
2318 "length=%" PRIu64
, notit
, seq_field
, length
);
2326 struct ctf_field_class
*bfcr_borrow_variant_selected_field_class_cb(
2327 struct ctf_field_class
*fc
, void *data
)
2331 int64_t option_index
= -1;
2332 struct bt_msg_iter
*notit
= data
;
2333 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
2334 struct ctf_named_field_class
*selected_option
= NULL
;
2335 struct ctf_field_class
*ret_fc
= NULL
;
2341 /* Get variant's tag */
2342 tag
.u
= g_array_index(notit
->stored_values
, uint64_t,
2343 var_fc
->stored_tag_index
);
2346 * Check each range to find the selected option's index.
2348 if (var_fc
->tag_fc
->base
.is_signed
) {
2349 for (i
= 0; i
< var_fc
->ranges
->len
; i
++) {
2350 struct ctf_field_class_variant_range
*range
=
2351 ctf_field_class_variant_borrow_range_by_index(
2354 if (tag
.i
>= range
->range
.lower
.i
&&
2355 tag
.i
<= range
->range
.upper
.i
) {
2356 option_index
= (int64_t) range
->option_index
;
2361 for (i
= 0; i
< var_fc
->ranges
->len
; i
++) {
2362 struct ctf_field_class_variant_range
*range
=
2363 ctf_field_class_variant_borrow_range_by_index(
2366 if (tag
.u
>= range
->range
.lower
.u
&&
2367 tag
.u
<= range
->range
.upper
.u
) {
2368 option_index
= (int64_t) range
->option_index
;
2374 if (option_index
< 0) {
2375 BT_LOGW("Cannot find variant field class's option: "
2376 "notit-addr=%p, var-fc-addr=%p, u-tag=%" PRIu64
", "
2377 "i-tag=%" PRId64
, notit
, var_fc
, tag
.u
, tag
.i
);
2381 selected_option
= ctf_field_class_variant_borrow_option_by_index(
2382 var_fc
, (uint64_t) option_index
);
2384 if (selected_option
->fc
->in_ir
) {
2385 bt_field
*var_field
= stack_top(notit
->stack
)->base
;
2387 ret
= bt_field_variant_select_option_field(
2388 var_field
, option_index
);
2390 BT_LOGW("Cannot select variant field's option field: "
2391 "notit-addr=%p, var-field-addr=%p, "
2392 "opt-index=%" PRId64
, notit
, var_field
,
2398 ret_fc
= selected_option
->fc
;
2405 void create_msg_stream_beginning(struct bt_msg_iter
*notit
,
2406 bt_message
**message
)
2408 bt_message
*ret
= NULL
;
2410 BT_ASSERT(notit
->stream
);
2411 BT_ASSERT(notit
->msg_iter
);
2412 ret
= bt_message_stream_beginning_create(notit
->msg_iter
,
2415 BT_LOGE("Cannot create stream beginning message: "
2416 "notit-addr=%p, stream-addr=%p",
2417 notit
, notit
->stream
);
2425 void create_msg_stream_activity_beginning(struct bt_msg_iter
*notit
,
2426 bt_message
**message
)
2428 bt_message
*ret
= NULL
;
2430 BT_ASSERT(notit
->stream
);
2431 BT_ASSERT(notit
->msg_iter
);
2432 ret
= bt_message_stream_activity_beginning_create(notit
->msg_iter
,
2435 BT_LOGE("Cannot create stream activity beginning message: "
2436 "notit-addr=%p, stream-addr=%p",
2437 notit
, notit
->stream
);
2445 void create_msg_stream_activity_end(struct bt_msg_iter
*notit
,
2446 bt_message
**message
)
2448 bt_message
*ret
= NULL
;
2450 if (!notit
->stream
) {
2451 BT_LOGE("Cannot create stream for stream message: "
2452 "notit-addr=%p", notit
);
2456 BT_ASSERT(notit
->stream
);
2457 BT_ASSERT(notit
->msg_iter
);
2458 ret
= bt_message_stream_activity_end_create(notit
->msg_iter
,
2461 BT_LOGE("Cannot create stream activity end message: "
2462 "notit-addr=%p, stream-addr=%p",
2463 notit
, notit
->stream
);
2471 void create_msg_stream_end(struct bt_msg_iter
*notit
, bt_message
**message
)
2475 if (!notit
->stream
) {
2476 BT_LOGE("Cannot create stream for stream message: "
2477 "notit-addr=%p", notit
);
2481 BT_ASSERT(notit
->msg_iter
);
2482 ret
= bt_message_stream_end_create(notit
->msg_iter
,
2485 BT_LOGE("Cannot create stream end message: "
2486 "notit-addr=%p, stream-addr=%p",
2487 notit
, notit
->stream
);
2495 void create_msg_packet_beginning(struct bt_msg_iter
*notit
,
2496 bt_message
**message
)
2499 enum bt_msg_iter_status status
;
2500 bt_message
*msg
= NULL
;
2501 const bt_stream_class
*sc
;
2503 status
= set_current_packet(notit
);
2504 if (status
!= BT_MSG_ITER_STATUS_OK
) {
2508 BT_ASSERT(notit
->packet
);
2509 sc
= notit
->meta
.sc
->ir_sc
;
2512 if (notit
->packet_context_field
) {
2513 ret
= bt_packet_move_context_field(
2514 notit
->packet
, notit
->packet_context_field
);
2519 notit
->packet_context_field
= NULL
;
2522 * At this point notit->dscopes.stream_packet_context
2523 * has the same value as the packet context field within
2526 BT_ASSERT(bt_packet_borrow_context_field(
2528 notit
->dscopes
.stream_packet_context
);
2531 BT_ASSERT(notit
->msg_iter
);
2533 if (notit
->meta
.sc
->packets_have_ts_begin
) {
2534 BT_ASSERT(notit
->snapshots
.beginning_clock
!= UINT64_C(-1));
2535 msg
= bt_message_packet_beginning_create_with_default_clock_snapshot(
2536 notit
->msg_iter
, notit
->packet
,
2537 notit
->snapshots
.beginning_clock
);
2539 msg
= bt_message_packet_beginning_create(notit
->msg_iter
,
2544 BT_LOGE("Cannot create packet beginning message: "
2545 "notit-addr=%p, packet-addr=%p",
2546 notit
, notit
->packet
);
2557 void create_msg_packet_end(struct bt_msg_iter
*notit
, bt_message
**message
)
2561 if (!notit
->packet
) {
2565 /* Update default clock from packet's end time */
2566 if (notit
->snapshots
.end_clock
!= UINT64_C(-1)) {
2567 notit
->default_clock_snapshot
= notit
->snapshots
.end_clock
;
2570 BT_ASSERT(notit
->msg_iter
);
2572 if (notit
->meta
.sc
->packets_have_ts_end
) {
2573 BT_ASSERT(notit
->snapshots
.end_clock
!= UINT64_C(-1));
2574 msg
= bt_message_packet_end_create_with_default_clock_snapshot(
2575 notit
->msg_iter
, notit
->packet
,
2576 notit
->snapshots
.end_clock
);
2578 msg
= bt_message_packet_end_create(notit
->msg_iter
,
2583 BT_LOGE("Cannot create packet end message: "
2584 "notit-addr=%p, packet-addr=%p",
2585 notit
, notit
->packet
);
2590 BT_PACKET_PUT_REF_AND_RESET(notit
->packet
);
2595 void create_msg_discarded_events(struct bt_msg_iter
*notit
,
2596 bt_message
**message
)
2599 uint64_t beginning_raw_value
= UINT64_C(-1);
2600 uint64_t end_raw_value
= UINT64_C(-1);
2602 BT_ASSERT(notit
->msg_iter
);
2603 BT_ASSERT(notit
->stream
);
2604 BT_ASSERT(notit
->meta
.sc
->has_discarded_events
);
2606 if (notit
->meta
.sc
->discarded_events_have_default_cs
) {
2607 if (notit
->prev_packet_snapshots
.discarded_events
== UINT64_C(-1)) {
2609 * We discarded events, but before (and possibly
2610 * including) the current packet: use this packet's time
2611 * range, and do not have a specific count.
2613 beginning_raw_value
= notit
->snapshots
.beginning_clock
;
2614 end_raw_value
= notit
->snapshots
.end_clock
;
2616 beginning_raw_value
= notit
->prev_packet_snapshots
.end_clock
;
2617 end_raw_value
= notit
->snapshots
.end_clock
;
2620 BT_ASSERT(beginning_raw_value
!= UINT64_C(-1));
2621 BT_ASSERT(end_raw_value
!= UINT64_C(-1));
2622 msg
= bt_message_discarded_events_create_with_default_clock_snapshots(
2623 notit
->msg_iter
, notit
->stream
, beginning_raw_value
,
2626 msg
= bt_message_discarded_events_create(notit
->msg_iter
,
2631 BT_LOGE("Cannot create discarded events message: "
2632 "notit-addr=%p, stream-addr=%p",
2633 notit
, notit
->stream
);
2637 if (notit
->prev_packet_snapshots
.discarded_events
!= UINT64_C(-1)) {
2638 bt_message_discarded_events_set_count(msg
,
2639 notit
->snapshots
.discarded_events
-
2640 notit
->prev_packet_snapshots
.discarded_events
);
2647 void create_msg_discarded_packets(struct bt_msg_iter
*notit
,
2648 bt_message
**message
)
2652 BT_ASSERT(notit
->msg_iter
);
2653 BT_ASSERT(notit
->stream
);
2654 BT_ASSERT(notit
->meta
.sc
->has_discarded_packets
);
2655 BT_ASSERT(notit
->prev_packet_snapshots
.packets
!=
2658 if (notit
->meta
.sc
->discarded_packets_have_default_cs
) {
2659 BT_ASSERT(notit
->prev_packet_snapshots
.end_clock
!= UINT64_C(-1));
2660 BT_ASSERT(notit
->snapshots
.beginning_clock
!= UINT64_C(-1));
2661 msg
= bt_message_discarded_packets_create_with_default_clock_snapshots(
2662 notit
->msg_iter
, notit
->stream
,
2663 notit
->prev_packet_snapshots
.end_clock
,
2664 notit
->snapshots
.beginning_clock
);
2666 msg
= bt_message_discarded_packets_create(notit
->msg_iter
,
2671 BT_LOGE("Cannot create discarded packets message: "
2672 "notit-addr=%p, stream-addr=%p",
2673 notit
, notit
->stream
);
2677 bt_message_discarded_packets_set_count(msg
,
2678 notit
->snapshots
.packets
-
2679 notit
->prev_packet_snapshots
.packets
- 1);
2684 struct bt_msg_iter
*bt_msg_iter_create(struct ctf_trace_class
*tc
,
2685 size_t max_request_sz
,
2686 struct bt_msg_iter_medium_ops medops
, void *data
)
2688 struct bt_msg_iter
*notit
= NULL
;
2689 struct bt_bfcr_cbs cbs
= {
2691 .signed_int
= bfcr_signed_int_cb
,
2692 .unsigned_int
= bfcr_unsigned_int_cb
,
2693 .floating_point
= bfcr_floating_point_cb
,
2694 .string_begin
= bfcr_string_begin_cb
,
2695 .string
= bfcr_string_cb
,
2696 .string_end
= bfcr_string_end_cb
,
2697 .compound_begin
= bfcr_compound_begin_cb
,
2698 .compound_end
= bfcr_compound_end_cb
,
2701 .get_sequence_length
= bfcr_get_sequence_length_cb
,
2702 .borrow_variant_selected_field_class
= bfcr_borrow_variant_selected_field_class_cb
,
2707 BT_ASSERT(medops
.request_bytes
);
2708 BT_ASSERT(medops
.borrow_stream
);
2709 BT_LOGD("Creating CTF plugin message iterator: "
2710 "trace-addr=%p, max-request-size=%zu, "
2711 "data=%p", tc
, max_request_sz
, data
);
2712 notit
= g_new0(struct bt_msg_iter
, 1);
2714 BT_LOGE_STR("Failed to allocate one CTF plugin message iterator.");
2717 notit
->meta
.tc
= tc
;
2718 notit
->medium
.medops
= medops
;
2719 notit
->medium
.max_request_sz
= max_request_sz
;
2720 notit
->medium
.data
= data
;
2721 notit
->stack
= stack_new(notit
);
2722 notit
->stored_values
= g_array_new(FALSE
, TRUE
, sizeof(uint64_t));
2723 g_array_set_size(notit
->stored_values
, tc
->stored_value_count
);
2725 if (!notit
->stack
) {
2726 BT_LOGE_STR("Failed to create field stack.");
2730 notit
->bfcr
= bt_bfcr_create(cbs
, notit
);
2732 BT_LOGE_STR("Failed to create binary class reader (BFCR).");
2736 bt_msg_iter_reset(notit
);
2737 BT_LOGD("Created CTF plugin message iterator: "
2738 "trace-addr=%p, max-request-size=%zu, "
2739 "data=%p, notit-addr=%p",
2740 tc
, max_request_sz
, data
, notit
);
2741 notit
->cur_packet_offset
= 0;
2747 bt_msg_iter_destroy(notit
);
2752 void bt_msg_iter_destroy(struct bt_msg_iter
*notit
)
2754 BT_PACKET_PUT_REF_AND_RESET(notit
->packet
);
2755 BT_STREAM_PUT_REF_AND_RESET(notit
->stream
);
2756 release_all_dscopes(notit
);
2758 BT_LOGD("Destroying CTF plugin message iterator: addr=%p", notit
);
2761 BT_LOGD_STR("Destroying field stack.");
2762 stack_destroy(notit
->stack
);
2766 BT_LOGD("Destroying BFCR: bfcr-addr=%p", notit
->bfcr
);
2767 bt_bfcr_destroy(notit
->bfcr
);
2770 if (notit
->stored_values
) {
2771 g_array_free(notit
->stored_values
, TRUE
);
2777 enum bt_msg_iter_status
bt_msg_iter_get_next_message(
2778 struct bt_msg_iter
*notit
,
2779 bt_self_message_iterator
*msg_iter
, bt_message
**message
)
2781 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
2785 notit
->msg_iter
= msg_iter
;
2786 notit
->set_stream
= true;
2787 BT_LOGV("Getting next message: notit-addr=%p", notit
);
2790 status
= handle_state(notit
);
2791 if (G_UNLIKELY(status
== BT_MSG_ITER_STATUS_AGAIN
)) {
2792 BT_LOGV_STR("Medium returned BT_MSG_ITER_STATUS_AGAIN.");
2794 } else if (G_UNLIKELY(status
!= BT_MSG_ITER_STATUS_OK
)) {
2795 BT_LOGW("Cannot handle state: notit-addr=%p, state=%s",
2796 notit
, state_string(notit
->state
));
2800 switch (notit
->state
) {
2801 case STATE_EMIT_MSG_EVENT
:
2802 BT_ASSERT(notit
->event_msg
);
2803 *message
= notit
->event_msg
;
2804 notit
->event_msg
= NULL
;
2806 case STATE_EMIT_MSG_DISCARDED_EVENTS
:
2807 /* create_msg_discared_events() logs errors */
2808 create_msg_discarded_events(notit
, message
);
2811 status
= BT_MSG_ITER_STATUS_ERROR
;
2815 case STATE_EMIT_MSG_DISCARDED_PACKETS
:
2816 /* create_msg_discared_packets() logs errors */
2817 create_msg_discarded_packets(notit
, message
);
2820 status
= BT_MSG_ITER_STATUS_ERROR
;
2824 case STATE_EMIT_MSG_PACKET_BEGINNING
:
2825 /* create_msg_packet_beginning() logs errors */
2826 create_msg_packet_beginning(notit
, message
);
2829 status
= BT_MSG_ITER_STATUS_ERROR
;
2833 case STATE_EMIT_MSG_PACKET_END_SINGLE
:
2834 case STATE_EMIT_MSG_PACKET_END_MULTI
:
2835 /* create_msg_packet_end() logs errors */
2836 create_msg_packet_end(notit
, message
);
2839 status
= BT_MSG_ITER_STATUS_ERROR
;
2843 case STATE_EMIT_MSG_STREAM_ACTIVITY_BEGINNING
:
2844 /* create_msg_stream_activity_beginning() logs errors */
2845 create_msg_stream_activity_beginning(notit
, message
);
2848 status
= BT_MSG_ITER_STATUS_ERROR
;
2852 case STATE_EMIT_MSG_STREAM_ACTIVITY_END
:
2853 /* create_msg_stream_activity_end() logs errors */
2854 create_msg_stream_activity_end(notit
, message
);
2857 status
= BT_MSG_ITER_STATUS_ERROR
;
2861 case STATE_EMIT_MSG_STREAM_BEGINNING
:
2862 /* create_msg_stream_beginning() logs errors */
2863 create_msg_stream_beginning(notit
, message
);
2866 status
= BT_MSG_ITER_STATUS_ERROR
;
2870 case STATE_EMIT_MSG_STREAM_END
:
2871 /* create_msg_stream_end() logs errors */
2872 create_msg_stream_end(notit
, message
);
2875 status
= BT_MSG_ITER_STATUS_ERROR
;
2880 status
= BT_MSG_ITER_STATUS_EOF
;
2883 /* Non-emitting state: continue */
2893 enum bt_msg_iter_status
read_packet_header_context_fields(
2894 struct bt_msg_iter
*notit
)
2897 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
2900 notit
->set_stream
= false;
2902 if (notit
->state
== STATE_EMIT_MSG_PACKET_BEGINNING
) {
2903 /* We're already there */
2908 status
= handle_state(notit
);
2909 if (G_UNLIKELY(status
== BT_MSG_ITER_STATUS_AGAIN
)) {
2910 BT_LOGV_STR("Medium returned BT_MSG_ITER_STATUS_AGAIN.");
2912 } else if (G_UNLIKELY(status
!= BT_MSG_ITER_STATUS_OK
)) {
2913 BT_LOGW("Cannot handle state: notit-addr=%p, state=%s",
2914 notit
, state_string(notit
->state
));
2918 switch (notit
->state
) {
2919 case STATE_EMIT_MSG_PACKET_BEGINNING
:
2921 * Packet header and context fields are
2922 * potentially decoded (or they don't exist).
2926 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
2927 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
2928 case STATE_AFTER_TRACE_PACKET_HEADER
:
2929 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
2930 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
2931 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
2932 case STATE_CHECK_EMIT_MSG_STREAM_BEGINNING
:
2933 case STATE_EMIT_MSG_STREAM_BEGINNING
:
2934 case STATE_EMIT_MSG_STREAM_ACTIVITY_BEGINNING
:
2935 case STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS
:
2936 case STATE_EMIT_MSG_DISCARDED_EVENTS
:
2937 case STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
:
2938 case STATE_EMIT_MSG_DISCARDED_PACKETS
:
2939 /* Non-emitting state: continue */
2943 * We should never get past the
2944 * STATE_EMIT_MSG_PACKET_BEGINNING state.
2946 BT_LOGF("Unexpected state: notit-addr=%p, state=%s",
2947 notit
, state_string(notit
->state
));
2953 ret
= set_current_packet_content_sizes(notit
);
2955 status
= BT_MSG_ITER_STATUS_ERROR
;
2962 void bt_msg_iter_set_medops_data(struct bt_msg_iter
*notit
,
2966 notit
->medium
.data
= medops_data
;
2970 enum bt_msg_iter_status
bt_msg_iter_seek(struct bt_msg_iter
*notit
,
2973 enum bt_msg_iter_status ret
= BT_MSG_ITER_STATUS_OK
;
2974 enum bt_msg_iter_medium_status medium_status
;
2978 BT_LOGE("Cannot seek to negative offset: offset=%jd", offset
);
2979 ret
= BT_MSG_ITER_STATUS_INVAL
;
2983 if (!notit
->medium
.medops
.seek
) {
2984 ret
= BT_MSG_ITER_STATUS_UNSUPPORTED
;
2985 BT_LOGD("Aborting seek as the iterator's underlying media does not implement seek support.");
2989 medium_status
= notit
->medium
.medops
.seek(
2990 BT_MSG_ITER_SEEK_WHENCE_SET
, offset
, notit
->medium
.data
);
2991 if (medium_status
!= BT_MSG_ITER_MEDIUM_STATUS_OK
) {
2992 if (medium_status
== BT_MSG_ITER_MEDIUM_STATUS_EOF
) {
2993 ret
= BT_MSG_ITER_STATUS_EOF
;
2995 ret
= BT_MSG_ITER_STATUS_ERROR
;
3000 bt_msg_iter_reset(notit
);
3001 notit
->cur_packet_offset
= offset
;
3008 enum bt_msg_iter_status
bt_msg_iter_get_packet_properties(
3009 struct bt_msg_iter
*notit
,
3010 struct bt_msg_iter_packet_properties
*props
)
3012 enum bt_msg_iter_status status
;
3016 status
= read_packet_header_context_fields(notit
);
3017 if (status
!= BT_MSG_ITER_STATUS_OK
) {
3021 props
->exp_packet_total_size
= notit
->cur_exp_packet_total_size
;
3022 props
->exp_packet_content_size
= notit
->cur_exp_packet_content_size
;
3023 props
->stream_class_id
= (uint64_t) notit
->cur_stream_class_id
;
3024 props
->data_stream_id
= notit
->cur_data_stream_id
;
3025 props
->snapshots
.discarded_events
= notit
->snapshots
.discarded_events
;
3026 props
->snapshots
.packets
= notit
->snapshots
.packets
;
3027 props
->snapshots
.beginning_clock
= notit
->snapshots
.beginning_clock
;
3028 props
->snapshots
.end_clock
= notit
->snapshots
.end_clock
;
3035 void bt_msg_iter_set_emit_stream_beginning_message(struct bt_msg_iter
*notit
,
3038 notit
->emit_stream_begin_msg
= val
;
3042 void bt_msg_iter_set_emit_stream_end_message(struct bt_msg_iter
*notit
,
3045 notit
->emit_stream_end_msg
= val
;