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_COMP_LOG_SELF_COMP (notit->self_comp)
27 #define BT_LOG_OUTPUT_LEVEL (notit->log_level)
28 #define BT_LOG_TAG "PLUGIN/CTF/MSG-ITER"
29 #include "plugins/comp-logging.h"
36 #include "common/assert.h"
38 #include <babeltrace2/babeltrace.h>
39 #include "common/common.h"
44 #include "../bfcr/bfcr.h"
48 /* A visit stack entry */
51 * Current base field, one of:
63 /* Index of next field to set */
71 struct bt_msg_iter
*notit
;
73 /* Entries (struct stack_entry) */
76 /* Number of active entries */
83 STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
,
84 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
85 STATE_AFTER_TRACE_PACKET_HEADER
,
86 STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
,
87 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
88 STATE_AFTER_STREAM_PACKET_CONTEXT
,
89 STATE_CHECK_EMIT_MSG_STREAM_BEGINNING
,
90 STATE_EMIT_MSG_STREAM_BEGINNING
,
91 STATE_EMIT_MSG_STREAM_ACTIVITY_BEGINNING
,
92 STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS
,
93 STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
,
94 STATE_EMIT_MSG_DISCARDED_EVENTS
,
95 STATE_EMIT_MSG_DISCARDED_PACKETS
,
96 STATE_EMIT_MSG_PACKET_BEGINNING
,
97 STATE_DSCOPE_EVENT_HEADER_BEGIN
,
98 STATE_DSCOPE_EVENT_HEADER_CONTINUE
,
99 STATE_AFTER_EVENT_HEADER
,
100 STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
,
101 STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
,
102 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
,
103 STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
,
104 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
105 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
106 STATE_EMIT_MSG_EVENT
,
107 STATE_SKIP_PACKET_PADDING
,
108 STATE_EMIT_MSG_PACKET_END_MULTI
,
109 STATE_EMIT_MSG_PACKET_END_SINGLE
,
110 STATE_CHECK_EMIT_MSG_STREAM_ACTIVITY_END
,
111 STATE_EMIT_MSG_STREAM_ACTIVITY_END
,
112 STATE_EMIT_MSG_STREAM_END
,
116 struct end_of_packet_snapshots
{
117 uint64_t discarded_events
;
119 uint64_t beginning_clock
;
123 /* CTF message iterator */
128 /* Current message iterator to create messages (weak) */
129 bt_self_message_iterator
*msg_iter
;
132 * True to emit stream beginning and stream activity beginning
135 bool emit_stream_begin_msg
;
137 /* True to emit stream end and stream activity end messages */
138 bool emit_stream_end_msg
;
140 /* True to set the stream */
144 * Current dynamic scope field pointer.
146 * This is set by read_dscope_begin_state() and contains the
147 * value of one of the pointers in `dscopes` below.
149 bt_field
*cur_dscope_field
;
152 * True if we're done filling a string field from a text
153 * array/sequence payload.
155 bool done_filling_string
;
157 /* Trace and classes */
158 /* True to set IR fields */
162 struct ctf_trace_class
*tc
;
163 struct ctf_stream_class
*sc
;
164 struct ctf_event_class
*ec
;
167 /* Current packet context field wrapper (NULL if not created yet) */
168 bt_packet_context_field
*packet_context_field
;
170 /* Current packet (NULL if not created yet) */
173 /* Current stream (NULL if not set yet) */
176 /* Current event (NULL if not created yet) */
179 /* Current event message (NULL if not created yet) */
180 bt_message
*event_msg
;
182 /* Database of current dynamic scopes */
184 bt_field
*stream_packet_context
;
185 bt_field
*event_common_context
;
186 bt_field
*event_spec_context
;
187 bt_field
*event_payload
;
193 /* Current medium buffer data */
195 /* Last address provided by medium */
198 /* Buffer size provided by medium (bytes) */
201 /* Offset within whole packet of addr (bits) */
202 size_t packet_offset
;
204 /* Current position from addr (bits) */
207 /* Position of the last event header from addr (bits) */
211 /* Binary type reader */
212 struct bt_bfcr
*bfcr
;
214 /* Current medium data */
216 struct bt_msg_iter_medium_ops medops
;
217 size_t max_request_sz
;
221 /* Current packet size (bits) (-1 if unknown) */
222 int64_t cur_exp_packet_total_size
;
224 /* Current content size (bits) (-1 if unknown) */
225 int64_t cur_exp_packet_content_size
;
227 /* Current stream class ID */
228 int64_t cur_stream_class_id
;
230 /* Current event class ID */
231 int64_t cur_event_class_id
;
233 /* Current data stream ID */
234 int64_t cur_data_stream_id
;
237 * Offset, in the underlying media, of the current packet's
238 * start (-1 if unknown).
240 off_t cur_packet_offset
;
242 /* Default clock's current value */
243 uint64_t default_clock_snapshot
;
245 /* End of current packet snapshots */
246 struct end_of_packet_snapshots snapshots
;
248 /* End of previous packet snapshots */
249 struct end_of_packet_snapshots prev_packet_snapshots
;
251 /* Stored values (for sequence lengths, variant tags) */
252 GArray
*stored_values
;
254 /* Iterator's current log level */
255 bt_logging_level log_level
;
257 /* Iterator's owning self component, or `NULL` if none (query) */
258 bt_self_component
*self_comp
;
262 const char *state_string(enum state state
)
267 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
268 return "STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN";
269 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
270 return "STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE";
271 case STATE_AFTER_TRACE_PACKET_HEADER
:
272 return "STATE_AFTER_TRACE_PACKET_HEADER";
273 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
274 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN";
275 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
276 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE";
277 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
278 return "STATE_AFTER_STREAM_PACKET_CONTEXT";
279 case STATE_EMIT_MSG_STREAM_BEGINNING
:
280 return "STATE_EMIT_MSG_STREAM_BEGINNING";
281 case STATE_EMIT_MSG_STREAM_ACTIVITY_BEGINNING
:
282 return "STATE_EMIT_MSG_STREAM_ACTIVITY_BEGINNING";
283 case STATE_EMIT_MSG_PACKET_BEGINNING
:
284 return "STATE_EMIT_MSG_PACKET_BEGINNING";
285 case STATE_EMIT_MSG_DISCARDED_EVENTS
:
286 return "STATE_EMIT_MSG_DISCARDED_EVENTS";
287 case STATE_EMIT_MSG_DISCARDED_PACKETS
:
288 return "STATE_EMIT_MSG_DISCARDED_PACKETS";
289 case STATE_DSCOPE_EVENT_HEADER_BEGIN
:
290 return "STATE_DSCOPE_EVENT_HEADER_BEGIN";
291 case STATE_DSCOPE_EVENT_HEADER_CONTINUE
:
292 return "STATE_DSCOPE_EVENT_HEADER_CONTINUE";
293 case STATE_AFTER_EVENT_HEADER
:
294 return "STATE_AFTER_EVENT_HEADER";
295 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
:
296 return "STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN";
297 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
:
298 return "STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE";
299 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
:
300 return "STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN";
301 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
:
302 return "STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE";
303 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
304 return "STATE_DSCOPE_EVENT_PAYLOAD_BEGIN";
305 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
306 return "STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE";
307 case STATE_EMIT_MSG_EVENT
:
308 return "STATE_EMIT_MSG_EVENT";
309 case STATE_SKIP_PACKET_PADDING
:
310 return "STATE_SKIP_PACKET_PADDING";
311 case STATE_EMIT_MSG_PACKET_END_MULTI
:
312 return "STATE_EMIT_MSG_PACKET_END_MULTI";
313 case STATE_EMIT_MSG_PACKET_END_SINGLE
:
314 return "STATE_EMIT_MSG_PACKET_END_SINGLE";
315 case STATE_EMIT_MSG_STREAM_ACTIVITY_END
:
316 return "STATE_EMIT_MSG_STREAM_ACTIVITY_END";
317 case STATE_EMIT_MSG_STREAM_END
:
318 return "STATE_EMIT_MSG_STREAM_END";
327 int bt_msg_iter_switch_packet(struct bt_msg_iter
*notit
);
330 struct stack
*stack_new(struct bt_msg_iter
*notit
)
332 struct stack
*stack
= NULL
;
334 stack
= g_new0(struct stack
, 1);
336 BT_COMP_LOGE_STR("Failed to allocate one stack.");
340 stack
->notit
= notit
;
341 stack
->entries
= g_array_new(FALSE
, TRUE
, sizeof(struct stack_entry
));
342 if (!stack
->entries
) {
343 BT_COMP_LOGE_STR("Failed to allocate a GArray.");
347 BT_COMP_LOGD("Created stack: notit-addr=%p, stack-addr=%p", notit
, stack
);
359 void stack_destroy(struct stack
*stack
)
361 struct bt_msg_iter
*notit
;
364 notit
= stack
->notit
;
365 BT_COMP_LOGD("Destroying stack: addr=%p", stack
);
367 if (stack
->entries
) {
368 g_array_free(stack
->entries
, TRUE
);
375 void stack_push(struct stack
*stack
, bt_field
*base
)
377 struct stack_entry
*entry
;
378 struct bt_msg_iter
*notit
;
381 notit
= stack
->notit
;
383 BT_COMP_LOGT("Pushing base field on stack: stack-addr=%p, "
384 "stack-size-before=%zu, stack-size-after=%zu",
385 stack
, stack
->size
, stack
->size
+ 1);
387 if (stack
->entries
->len
== stack
->size
) {
388 g_array_set_size(stack
->entries
, stack
->size
+ 1);
391 entry
= &g_array_index(stack
->entries
, struct stack_entry
, stack
->size
);
398 unsigned int stack_size(struct stack
*stack
)
405 void stack_pop(struct stack
*stack
)
407 struct bt_msg_iter
*notit
;
410 BT_ASSERT(stack_size(stack
));
411 notit
= stack
->notit
;
412 BT_COMP_LOGT("Popping from stack: "
413 "stack-addr=%p, stack-size-before=%zu, stack-size-after=%zu",
414 stack
, stack
->size
, stack
->size
- 1);
419 struct stack_entry
*stack_top(struct stack
*stack
)
422 BT_ASSERT(stack_size(stack
));
423 return &g_array_index(stack
->entries
, struct stack_entry
,
428 bool stack_empty(struct stack
*stack
)
430 return stack_size(stack
) == 0;
434 void stack_clear(struct stack
*stack
)
441 enum bt_msg_iter_status
msg_iter_status_from_m_status(
442 enum bt_msg_iter_medium_status m_status
)
444 /* They are the same */
445 return (int) m_status
;
449 size_t buf_size_bits(struct bt_msg_iter
*notit
)
451 return notit
->buf
.sz
* 8;
455 size_t buf_available_bits(struct bt_msg_iter
*notit
)
457 return buf_size_bits(notit
) - notit
->buf
.at
;
461 size_t packet_at(struct bt_msg_iter
*notit
)
463 return notit
->buf
.packet_offset
+ notit
->buf
.at
;
467 void buf_consume_bits(struct bt_msg_iter
*notit
, size_t incr
)
469 BT_COMP_LOGT("Advancing cursor: notit-addr=%p, cur-before=%zu, cur-after=%zu",
470 notit
, notit
->buf
.at
, notit
->buf
.at
+ incr
);
471 notit
->buf
.at
+= incr
;
475 enum bt_msg_iter_status
request_medium_bytes(
476 struct bt_msg_iter
*notit
)
478 uint8_t *buffer_addr
= NULL
;
479 size_t buffer_sz
= 0;
480 enum bt_msg_iter_medium_status m_status
;
482 BT_COMP_LOGD("Calling user function (request bytes): notit-addr=%p, "
483 "request-size=%zu", notit
, notit
->medium
.max_request_sz
);
484 m_status
= notit
->medium
.medops
.request_bytes(
485 notit
->medium
.max_request_sz
, &buffer_addr
,
486 &buffer_sz
, notit
->medium
.data
);
487 BT_COMP_LOGD("User function returned: status=%s, buf-addr=%p, buf-size=%zu",
488 bt_msg_iter_medium_status_string(m_status
),
489 buffer_addr
, buffer_sz
);
490 if (m_status
== BT_MSG_ITER_MEDIUM_STATUS_OK
) {
491 BT_ASSERT(buffer_sz
!= 0);
493 /* New packet offset is old one + old size (in bits) */
494 notit
->buf
.packet_offset
+= buf_size_bits(notit
);
496 /* Restart at the beginning of the new medium buffer */
498 notit
->buf
.last_eh_at
= SIZE_MAX
;
500 /* New medium buffer size */
501 notit
->buf
.sz
= buffer_sz
;
503 /* New medium buffer address */
504 notit
->buf
.addr
= buffer_addr
;
506 BT_COMP_LOGD("User function returned new bytes: "
507 "packet-offset=%zu, cur=%zu, size=%zu, addr=%p",
508 notit
->buf
.packet_offset
, notit
->buf
.at
,
509 notit
->buf
.sz
, notit
->buf
.addr
);
510 BT_COMP_LOGD_MEM(buffer_addr
, buffer_sz
, "Returned bytes at %p:",
512 } else if (m_status
== BT_MSG_ITER_MEDIUM_STATUS_EOF
) {
514 * User returned end of stream: validate that we're not
515 * in the middle of a packet header, packet context, or
518 if (notit
->cur_exp_packet_total_size
>= 0) {
519 if (packet_at(notit
) ==
520 notit
->cur_exp_packet_total_size
) {
524 if (packet_at(notit
) == 0) {
528 if (notit
->buf
.last_eh_at
!= SIZE_MAX
&&
529 notit
->buf
.at
== notit
->buf
.last_eh_at
) {
534 /* All other states are invalid */
535 BT_COMP_LOGW("User function returned %s, but message iterator is in an unexpected state: "
536 "state=%s, cur-packet-size=%" PRId64
", cur=%zu, "
537 "packet-cur=%zu, last-eh-at=%zu",
538 bt_msg_iter_medium_status_string(m_status
),
539 state_string(notit
->state
),
540 notit
->cur_exp_packet_total_size
,
541 notit
->buf
.at
, packet_at(notit
),
542 notit
->buf
.last_eh_at
);
543 m_status
= BT_MSG_ITER_MEDIUM_STATUS_ERROR
;
544 } else if (m_status
< 0) {
545 BT_COMP_LOGW("User function failed: status=%s",
546 bt_msg_iter_medium_status_string(m_status
));
550 return msg_iter_status_from_m_status(m_status
);
554 enum bt_msg_iter_status
buf_ensure_available_bits(
555 struct bt_msg_iter
*notit
)
557 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
559 if (G_UNLIKELY(buf_available_bits(notit
) == 0)) {
561 * This _cannot_ return BT_MSG_ITER_STATUS_OK
564 status
= request_medium_bytes(notit
);
571 enum bt_msg_iter_status
read_dscope_begin_state(
572 struct bt_msg_iter
*notit
,
573 struct ctf_field_class
*dscope_fc
,
574 enum state done_state
, enum state continue_state
,
575 bt_field
*dscope_field
)
577 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
578 enum bt_bfcr_status bfcr_status
;
579 size_t consumed_bits
;
581 notit
->cur_dscope_field
= dscope_field
;
582 BT_COMP_LOGT("Starting BFCR: notit-addr=%p, bfcr-addr=%p, fc-addr=%p",
583 notit
, notit
->bfcr
, dscope_fc
);
584 consumed_bits
= bt_bfcr_start(notit
->bfcr
, dscope_fc
,
585 notit
->buf
.addr
, notit
->buf
.at
, packet_at(notit
),
586 notit
->buf
.sz
, &bfcr_status
);
587 BT_COMP_LOGT("BFCR consumed bits: size=%zu", consumed_bits
);
589 switch (bfcr_status
) {
590 case BT_BFCR_STATUS_OK
:
591 /* Field class was read completely */
592 BT_COMP_LOGT_STR("Field was completely decoded.");
593 notit
->state
= done_state
;
595 case BT_BFCR_STATUS_EOF
:
596 BT_COMP_LOGT_STR("BFCR needs more data to decode field completely.");
597 notit
->state
= continue_state
;
600 BT_COMP_LOGW("BFCR failed to start: notit-addr=%p, bfcr-addr=%p, "
601 "status=%s", notit
, notit
->bfcr
,
602 bt_bfcr_status_string(bfcr_status
));
603 status
= BT_MSG_ITER_STATUS_ERROR
;
607 /* Consume bits now since we know we're not in an error state */
608 buf_consume_bits(notit
, consumed_bits
);
615 enum bt_msg_iter_status
read_dscope_continue_state(
616 struct bt_msg_iter
*notit
, enum state done_state
)
618 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
619 enum bt_bfcr_status bfcr_status
;
620 size_t consumed_bits
;
622 BT_COMP_LOGT("Continuing BFCR: notit-addr=%p, bfcr-addr=%p",
625 status
= buf_ensure_available_bits(notit
);
626 if (status
!= BT_MSG_ITER_STATUS_OK
) {
628 BT_COMP_LOGW("Cannot ensure that buffer has at least one byte: "
629 "msg-addr=%p, status=%s",
630 notit
, bt_msg_iter_status_string(status
));
632 BT_COMP_LOGT("Cannot ensure that buffer has at least one byte: "
633 "msg-addr=%p, status=%s",
634 notit
, bt_msg_iter_status_string(status
));
640 consumed_bits
= bt_bfcr_continue(notit
->bfcr
, notit
->buf
.addr
,
641 notit
->buf
.sz
, &bfcr_status
);
642 BT_COMP_LOGT("BFCR consumed bits: size=%zu", consumed_bits
);
644 switch (bfcr_status
) {
645 case BT_BFCR_STATUS_OK
:
646 /* Type was read completely. */
647 BT_COMP_LOGT_STR("Field was completely decoded.");
648 notit
->state
= done_state
;
650 case BT_BFCR_STATUS_EOF
:
651 /* Stay in this continue state. */
652 BT_COMP_LOGT_STR("BFCR needs more data to decode field completely.");
655 BT_COMP_LOGW("BFCR failed to continue: notit-addr=%p, bfcr-addr=%p, "
656 "status=%s", notit
, notit
->bfcr
,
657 bt_bfcr_status_string(bfcr_status
));
658 status
= BT_MSG_ITER_STATUS_ERROR
;
662 /* Consume bits now since we know we're not in an error state. */
663 buf_consume_bits(notit
, consumed_bits
);
669 void release_event_dscopes(struct bt_msg_iter
*notit
)
671 notit
->dscopes
.event_common_context
= NULL
;
672 notit
->dscopes
.event_spec_context
= NULL
;
673 notit
->dscopes
.event_payload
= NULL
;
677 void release_all_dscopes(struct bt_msg_iter
*notit
)
679 notit
->dscopes
.stream_packet_context
= NULL
;
681 if (notit
->packet_context_field
) {
682 bt_packet_context_field_release(notit
->packet_context_field
);
683 notit
->packet_context_field
= NULL
;
686 release_event_dscopes(notit
);
690 enum bt_msg_iter_status
read_packet_header_begin_state(
691 struct bt_msg_iter
*notit
)
693 struct ctf_field_class
*packet_header_fc
= NULL
;
694 enum bt_msg_iter_status ret
= BT_MSG_ITER_STATUS_OK
;
696 if (bt_msg_iter_switch_packet(notit
)) {
697 BT_COMP_LOGW("Cannot switch packet: notit-addr=%p", notit
);
698 ret
= BT_MSG_ITER_STATUS_ERROR
;
703 * Make sure at least one bit is available for this packet. An
704 * empty packet is impossible. If we reach the end of the medium
705 * at this point, then it's considered the end of the stream.
707 ret
= buf_ensure_available_bits(notit
);
709 case BT_MSG_ITER_STATUS_OK
:
711 case BT_MSG_ITER_STATUS_EOF
:
712 ret
= BT_MSG_ITER_STATUS_OK
;
713 notit
->state
= STATE_CHECK_EMIT_MSG_STREAM_ACTIVITY_END
;
719 /* Packet header class is common to the whole trace class. */
720 packet_header_fc
= notit
->meta
.tc
->packet_header_fc
;
721 if (!packet_header_fc
) {
722 notit
->state
= STATE_AFTER_TRACE_PACKET_HEADER
;
726 notit
->cur_stream_class_id
= -1;
727 notit
->cur_event_class_id
= -1;
728 notit
->cur_data_stream_id
= -1;
729 BT_COMP_LOGD("Decoding packet header field:"
730 "notit-addr=%p, trace-class-addr=%p, fc-addr=%p",
731 notit
, notit
->meta
.tc
, packet_header_fc
);
732 ret
= read_dscope_begin_state(notit
, packet_header_fc
,
733 STATE_AFTER_TRACE_PACKET_HEADER
,
734 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
, NULL
);
736 BT_COMP_LOGW("Cannot decode packet header field: "
737 "notit-addr=%p, trace-class-addr=%p, "
739 notit
, notit
->meta
.tc
, packet_header_fc
);
747 enum bt_msg_iter_status
read_packet_header_continue_state(
748 struct bt_msg_iter
*notit
)
750 return read_dscope_continue_state(notit
,
751 STATE_AFTER_TRACE_PACKET_HEADER
);
755 enum bt_msg_iter_status
set_current_stream_class(struct bt_msg_iter
*notit
)
757 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
758 struct ctf_stream_class
*new_stream_class
= NULL
;
760 if (notit
->cur_stream_class_id
== -1) {
762 * No current stream class ID field, therefore only one
765 if (notit
->meta
.tc
->stream_classes
->len
!= 1) {
766 BT_COMP_LOGW("Need exactly one stream class since there's "
767 "no stream class ID field: "
768 "notit-addr=%p", notit
);
769 status
= BT_MSG_ITER_STATUS_ERROR
;
773 new_stream_class
= notit
->meta
.tc
->stream_classes
->pdata
[0];
774 notit
->cur_stream_class_id
= new_stream_class
->id
;
777 new_stream_class
= ctf_trace_class_borrow_stream_class_by_id(
778 notit
->meta
.tc
, notit
->cur_stream_class_id
);
779 if (!new_stream_class
) {
780 BT_COMP_LOGW("No stream class with ID of stream class ID to use in trace class: "
781 "notit-addr=%p, stream-class-id=%" PRIu64
", "
782 "trace-class-addr=%p",
783 notit
, notit
->cur_stream_class_id
, notit
->meta
.tc
);
784 status
= BT_MSG_ITER_STATUS_ERROR
;
788 if (notit
->meta
.sc
) {
789 if (new_stream_class
!= notit
->meta
.sc
) {
790 BT_COMP_LOGW("Two packets refer to two different stream classes within the same packet sequence: "
791 "notit-addr=%p, prev-stream-class-addr=%p, "
792 "prev-stream-class-id=%" PRId64
", "
793 "next-stream-class-addr=%p, "
794 "next-stream-class-id=%" PRId64
", "
796 notit
, notit
->meta
.sc
,
799 new_stream_class
->id
,
801 status
= BT_MSG_ITER_STATUS_ERROR
;
805 notit
->meta
.sc
= new_stream_class
;
808 BT_COMP_LOGD("Set current stream class: "
809 "notit-addr=%p, stream-class-addr=%p, "
810 "stream-class-id=%" PRId64
,
811 notit
, notit
->meta
.sc
, notit
->meta
.sc
->id
);
818 enum bt_msg_iter_status
set_current_stream(struct bt_msg_iter
*notit
)
820 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
821 bt_stream
*stream
= NULL
;
823 BT_COMP_LOGD("Calling user function (get stream): notit-addr=%p, "
824 "stream-class-addr=%p, stream-class-id=%" PRId64
,
825 notit
, notit
->meta
.sc
,
827 stream
= notit
->medium
.medops
.borrow_stream(
828 notit
->meta
.sc
->ir_sc
, notit
->cur_data_stream_id
,
830 bt_stream_get_ref(stream
);
831 BT_COMP_LOGD("User function returned: stream-addr=%p", stream
);
833 BT_COMP_LOGW_STR("User function failed to return a stream object "
834 "for the given stream class.");
835 status
= BT_MSG_ITER_STATUS_ERROR
;
839 if (notit
->stream
&& stream
!= notit
->stream
) {
840 BT_COMP_LOGW("User function returned a different stream than the "
841 "previous one for the same sequence of packets.");
842 status
= BT_MSG_ITER_STATUS_ERROR
;
846 BT_STREAM_MOVE_REF(notit
->stream
, stream
);
849 bt_stream_put_ref(stream
);
854 enum bt_msg_iter_status
set_current_packet(struct bt_msg_iter
*notit
)
856 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
857 bt_packet
*packet
= NULL
;
859 BT_COMP_LOGD("Creating packet for packet message: "
860 "notit-addr=%p", notit
);
861 BT_COMP_LOGD("Creating packet from stream: "
862 "notit-addr=%p, stream-addr=%p, "
863 "stream-class-addr=%p, "
864 "stream-class-id=%" PRId64
,
865 notit
, notit
->stream
, notit
->meta
.sc
,
869 BT_ASSERT(notit
->stream
);
870 packet
= bt_packet_create(notit
->stream
);
872 BT_COMP_LOGE("Cannot create packet from stream: "
873 "notit-addr=%p, stream-addr=%p, "
874 "stream-class-addr=%p, "
875 "stream-class-id=%" PRId64
,
876 notit
, notit
->stream
, notit
->meta
.sc
,
884 BT_PACKET_PUT_REF_AND_RESET(packet
);
885 status
= BT_MSG_ITER_STATUS_ERROR
;
888 BT_PACKET_MOVE_REF(notit
->packet
, packet
);
893 enum bt_msg_iter_status
after_packet_header_state(
894 struct bt_msg_iter
*notit
)
896 enum bt_msg_iter_status status
;
898 status
= set_current_stream_class(notit
);
899 if (status
!= BT_MSG_ITER_STATUS_OK
) {
903 notit
->state
= STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
;
910 enum bt_msg_iter_status
read_packet_context_begin_state(
911 struct bt_msg_iter
*notit
)
913 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
914 struct ctf_field_class
*packet_context_fc
;
916 BT_ASSERT(notit
->meta
.sc
);
917 packet_context_fc
= notit
->meta
.sc
->packet_context_fc
;
918 if (!packet_context_fc
) {
919 BT_COMP_LOGD("No packet packet context field class in stream class: continuing: "
920 "notit-addr=%p, stream-class-addr=%p, "
921 "stream-class-id=%" PRId64
,
922 notit
, notit
->meta
.sc
,
924 notit
->state
= STATE_AFTER_STREAM_PACKET_CONTEXT
;
928 BT_ASSERT(!notit
->packet_context_field
);
930 if (packet_context_fc
->in_ir
) {
932 * Create free packet context field from stream class.
933 * This field is going to be moved to the packet once we
934 * create it. We cannot create the packet now because a
935 * packet is created from a stream, and this API must be
936 * able to return the packet context properties without
938 * (bt_msg_iter_get_packet_properties()).
940 notit
->packet_context_field
=
941 bt_packet_context_field_create(
942 notit
->meta
.sc
->ir_sc
);
943 if (!notit
->packet_context_field
) {
944 BT_COMP_LOGE_STR("Cannot create packet context field wrapper from stream class.");
945 status
= BT_MSG_ITER_STATUS_ERROR
;
949 notit
->dscopes
.stream_packet_context
=
950 bt_packet_context_field_borrow_field(
951 notit
->packet_context_field
);
952 BT_ASSERT(notit
->dscopes
.stream_packet_context
);
955 BT_COMP_LOGD("Decoding packet context field: "
956 "notit-addr=%p, stream-class-addr=%p, "
957 "stream-class-id=%" PRId64
", fc-addr=%p",
958 notit
, notit
->meta
.sc
,
959 notit
->meta
.sc
->id
, packet_context_fc
);
960 status
= read_dscope_begin_state(notit
, packet_context_fc
,
961 STATE_AFTER_STREAM_PACKET_CONTEXT
,
962 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
963 notit
->dscopes
.stream_packet_context
);
965 BT_COMP_LOGW("Cannot decode packet context field: "
966 "notit-addr=%p, stream-class-addr=%p, "
967 "stream-class-id=%" PRId64
", fc-addr=%p",
968 notit
, notit
->meta
.sc
,
978 enum bt_msg_iter_status
read_packet_context_continue_state(
979 struct bt_msg_iter
*notit
)
981 return read_dscope_continue_state(notit
,
982 STATE_AFTER_STREAM_PACKET_CONTEXT
);
986 enum bt_msg_iter_status
set_current_packet_content_sizes(
987 struct bt_msg_iter
*notit
)
989 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
991 if (notit
->cur_exp_packet_total_size
== -1) {
992 if (notit
->cur_exp_packet_content_size
!= -1) {
993 notit
->cur_exp_packet_total_size
=
994 notit
->cur_exp_packet_content_size
;
997 if (notit
->cur_exp_packet_content_size
== -1) {
998 notit
->cur_exp_packet_content_size
=
999 notit
->cur_exp_packet_total_size
;
1003 BT_ASSERT((notit
->cur_exp_packet_total_size
>= 0 &&
1004 notit
->cur_exp_packet_content_size
>= 0) ||
1005 (notit
->cur_exp_packet_total_size
< 0 &&
1006 notit
->cur_exp_packet_content_size
< 0));
1008 if (notit
->cur_exp_packet_content_size
>
1009 notit
->cur_exp_packet_total_size
) {
1010 BT_COMP_LOGW("Invalid packet or content size: "
1011 "content size is greater than packet size: "
1012 "notit-addr=%p, packet-context-field-addr=%p, "
1013 "packet-size=%" PRId64
", content-size=%" PRId64
,
1014 notit
, notit
->dscopes
.stream_packet_context
,
1015 notit
->cur_exp_packet_total_size
,
1016 notit
->cur_exp_packet_content_size
);
1017 status
= BT_MSG_ITER_STATUS_ERROR
;
1021 BT_COMP_LOGD("Set current packet and content sizes: "
1022 "notit-addr=%p, packet-size=%" PRIu64
", content-size=%" PRIu64
,
1023 notit
, notit
->cur_exp_packet_total_size
,
1024 notit
->cur_exp_packet_content_size
);
1031 enum bt_msg_iter_status
after_packet_context_state(struct bt_msg_iter
*notit
)
1033 enum bt_msg_iter_status status
;
1035 status
= set_current_packet_content_sizes(notit
);
1036 if (status
!= BT_MSG_ITER_STATUS_OK
) {
1040 if (notit
->stream
) {
1042 * Stream exists, which means we already emitted at
1043 * least one packet beginning message, so the initial
1044 * stream beginning message was also emitted.
1046 notit
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS
;
1048 notit
->state
= STATE_CHECK_EMIT_MSG_STREAM_BEGINNING
;
1056 enum bt_msg_iter_status
read_event_header_begin_state(struct bt_msg_iter
*notit
)
1058 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1059 struct ctf_field_class
*event_header_fc
= NULL
;
1061 /* Reset the position of the last event header */
1062 notit
->buf
.last_eh_at
= notit
->buf
.at
;
1063 notit
->cur_event_class_id
= -1;
1065 /* Check if we have some content left */
1066 if (notit
->cur_exp_packet_content_size
>= 0) {
1067 if (G_UNLIKELY(packet_at(notit
) ==
1068 notit
->cur_exp_packet_content_size
)) {
1069 /* No more events! */
1070 BT_COMP_LOGD("Reached end of packet: notit-addr=%p, "
1071 "cur=%zu", notit
, packet_at(notit
));
1072 notit
->state
= STATE_EMIT_MSG_PACKET_END_MULTI
;
1074 } else if (G_UNLIKELY(packet_at(notit
) >
1075 notit
->cur_exp_packet_content_size
)) {
1076 /* That's not supposed to happen */
1077 BT_COMP_LOGD("Before decoding event header field: cursor is passed the packet's content: "
1078 "notit-addr=%p, content-size=%" PRId64
", "
1080 notit
->cur_exp_packet_content_size
,
1082 status
= BT_MSG_ITER_STATUS_ERROR
;
1087 * "Infinite" content: we're done when the medium has
1088 * nothing else for us.
1090 status
= buf_ensure_available_bits(notit
);
1092 case BT_MSG_ITER_STATUS_OK
:
1094 case BT_MSG_ITER_STATUS_EOF
:
1095 status
= BT_MSG_ITER_STATUS_OK
;
1096 notit
->state
= STATE_EMIT_MSG_PACKET_END_SINGLE
;
1103 release_event_dscopes(notit
);
1104 BT_ASSERT(notit
->meta
.sc
);
1105 event_header_fc
= notit
->meta
.sc
->event_header_fc
;
1106 if (!event_header_fc
) {
1107 notit
->state
= STATE_AFTER_EVENT_HEADER
;
1111 BT_COMP_LOGD("Decoding event header field: "
1112 "notit-addr=%p, stream-class-addr=%p, "
1113 "stream-class-id=%" PRId64
", "
1115 notit
, notit
->meta
.sc
,
1118 status
= read_dscope_begin_state(notit
, event_header_fc
,
1119 STATE_AFTER_EVENT_HEADER
,
1120 STATE_DSCOPE_EVENT_HEADER_CONTINUE
, NULL
);
1122 BT_COMP_LOGW("Cannot decode event header field: "
1123 "notit-addr=%p, stream-class-addr=%p, "
1124 "stream-class-id=%" PRId64
", fc-addr=%p",
1125 notit
, notit
->meta
.sc
,
1135 enum bt_msg_iter_status
read_event_header_continue_state(
1136 struct bt_msg_iter
*notit
)
1138 return read_dscope_continue_state(notit
,
1139 STATE_AFTER_EVENT_HEADER
);
1143 enum bt_msg_iter_status
set_current_event_class(struct bt_msg_iter
*notit
)
1145 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1147 struct ctf_event_class
*new_event_class
= NULL
;
1149 if (notit
->cur_event_class_id
== -1) {
1151 * No current event class ID field, therefore only one
1154 if (notit
->meta
.sc
->event_classes
->len
!= 1) {
1155 BT_COMP_LOGW("Need exactly one event class since there's "
1156 "no event class ID field: "
1157 "notit-addr=%p", notit
);
1158 status
= BT_MSG_ITER_STATUS_ERROR
;
1162 new_event_class
= notit
->meta
.sc
->event_classes
->pdata
[0];
1163 notit
->cur_event_class_id
= new_event_class
->id
;
1166 new_event_class
= ctf_stream_class_borrow_event_class_by_id(
1167 notit
->meta
.sc
, notit
->cur_event_class_id
);
1168 if (!new_event_class
) {
1169 BT_COMP_LOGW("No event class with ID of event class ID to use in stream class: "
1170 "notit-addr=%p, stream-class-id=%" PRIu64
", "
1171 "event-class-id=%" PRIu64
", "
1172 "trace-class-addr=%p",
1173 notit
, notit
->meta
.sc
->id
, notit
->cur_event_class_id
,
1175 status
= BT_MSG_ITER_STATUS_ERROR
;
1179 notit
->meta
.ec
= new_event_class
;
1180 BT_COMP_LOGD("Set current event class: "
1181 "notit-addr=%p, event-class-addr=%p, "
1182 "event-class-id=%" PRId64
", "
1183 "event-class-name=\"%s\"",
1184 notit
, notit
->meta
.ec
, notit
->meta
.ec
->id
,
1185 notit
->meta
.ec
->name
->str
);
1192 enum bt_msg_iter_status
set_current_event_message(
1193 struct bt_msg_iter
*notit
)
1195 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1196 bt_message
*msg
= NULL
;
1198 BT_ASSERT(notit
->meta
.ec
);
1199 BT_ASSERT(notit
->packet
);
1200 BT_COMP_LOGD("Creating event message from event class and packet: "
1201 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", packet-addr=%p",
1202 notit
, notit
->meta
.ec
,
1203 notit
->meta
.ec
->name
->str
,
1205 BT_ASSERT(notit
->msg_iter
);
1206 BT_ASSERT(notit
->meta
.sc
);
1208 if (bt_stream_class_borrow_default_clock_class(notit
->meta
.sc
->ir_sc
)) {
1209 msg
= bt_message_event_create_with_default_clock_snapshot(
1210 notit
->msg_iter
, notit
->meta
.ec
->ir_ec
,
1211 notit
->packet
, notit
->default_clock_snapshot
);
1213 msg
= bt_message_event_create(notit
->msg_iter
,
1214 notit
->meta
.ec
->ir_ec
, notit
->packet
);
1218 BT_COMP_LOGE("Cannot create event message: "
1219 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", "
1221 notit
, notit
->meta
.ec
,
1222 notit
->meta
.ec
->name
->str
,
1230 BT_MESSAGE_PUT_REF_AND_RESET(msg
);
1231 status
= BT_MSG_ITER_STATUS_ERROR
;
1234 BT_MESSAGE_MOVE_REF(notit
->event_msg
, msg
);
1239 enum bt_msg_iter_status
after_event_header_state(
1240 struct bt_msg_iter
*notit
)
1242 enum bt_msg_iter_status status
;
1244 status
= set_current_event_class(notit
);
1245 if (status
!= BT_MSG_ITER_STATUS_OK
) {
1249 status
= set_current_event_message(notit
);
1250 if (status
!= BT_MSG_ITER_STATUS_OK
) {
1254 notit
->event
= bt_message_event_borrow_event(
1256 BT_ASSERT(notit
->event
);
1257 notit
->state
= STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
;
1264 enum bt_msg_iter_status
read_event_common_context_begin_state(
1265 struct bt_msg_iter
*notit
)
1267 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1268 struct ctf_field_class
*event_common_context_fc
;
1270 event_common_context_fc
= notit
->meta
.sc
->event_common_context_fc
;
1271 if (!event_common_context_fc
) {
1272 notit
->state
= STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
;
1276 if (event_common_context_fc
->in_ir
) {
1277 BT_ASSERT(!notit
->dscopes
.event_common_context
);
1278 notit
->dscopes
.event_common_context
=
1279 bt_event_borrow_common_context_field(
1281 BT_ASSERT(notit
->dscopes
.event_common_context
);
1284 BT_COMP_LOGT("Decoding event common context field: "
1285 "notit-addr=%p, stream-class-addr=%p, "
1286 "stream-class-id=%" PRId64
", "
1288 notit
, notit
->meta
.sc
,
1290 event_common_context_fc
);
1291 status
= read_dscope_begin_state(notit
, event_common_context_fc
,
1292 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
,
1293 STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
,
1294 notit
->dscopes
.event_common_context
);
1296 BT_COMP_LOGW("Cannot decode event common context field: "
1297 "notit-addr=%p, stream-class-addr=%p, "
1298 "stream-class-id=%" PRId64
", fc-addr=%p",
1299 notit
, notit
->meta
.sc
,
1301 event_common_context_fc
);
1309 enum bt_msg_iter_status
read_event_common_context_continue_state(
1310 struct bt_msg_iter
*notit
)
1312 return read_dscope_continue_state(notit
,
1313 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
);
1317 enum bt_msg_iter_status
read_event_spec_context_begin_state(
1318 struct bt_msg_iter
*notit
)
1320 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1321 struct ctf_field_class
*event_spec_context_fc
;
1323 event_spec_context_fc
= notit
->meta
.ec
->spec_context_fc
;
1324 if (!event_spec_context_fc
) {
1325 notit
->state
= STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
;
1329 if (event_spec_context_fc
->in_ir
) {
1330 BT_ASSERT(!notit
->dscopes
.event_spec_context
);
1331 notit
->dscopes
.event_spec_context
=
1332 bt_event_borrow_specific_context_field(
1334 BT_ASSERT(notit
->dscopes
.event_spec_context
);
1337 BT_COMP_LOGT("Decoding event specific context field: "
1338 "notit-addr=%p, event-class-addr=%p, "
1339 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1341 notit
, notit
->meta
.ec
,
1342 notit
->meta
.ec
->name
->str
,
1344 event_spec_context_fc
);
1345 status
= read_dscope_begin_state(notit
, event_spec_context_fc
,
1346 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
1347 STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
,
1348 notit
->dscopes
.event_spec_context
);
1350 BT_COMP_LOGW("Cannot decode event specific context field: "
1351 "notit-addr=%p, event-class-addr=%p, "
1352 "event-class-name=\"%s\", "
1353 "event-class-id=%" PRId64
", fc-addr=%p",
1354 notit
, notit
->meta
.ec
,
1355 notit
->meta
.ec
->name
->str
,
1357 event_spec_context_fc
);
1365 enum bt_msg_iter_status
read_event_spec_context_continue_state(
1366 struct bt_msg_iter
*notit
)
1368 return read_dscope_continue_state(notit
,
1369 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
);
1373 enum bt_msg_iter_status
read_event_payload_begin_state(
1374 struct bt_msg_iter
*notit
)
1376 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1377 struct ctf_field_class
*event_payload_fc
;
1379 event_payload_fc
= notit
->meta
.ec
->payload_fc
;
1380 if (!event_payload_fc
) {
1381 notit
->state
= STATE_EMIT_MSG_EVENT
;
1385 if (event_payload_fc
->in_ir
) {
1386 BT_ASSERT(!notit
->dscopes
.event_payload
);
1387 notit
->dscopes
.event_payload
=
1388 bt_event_borrow_payload_field(
1390 BT_ASSERT(notit
->dscopes
.event_payload
);
1393 BT_COMP_LOGT("Decoding event payload field: "
1394 "notit-addr=%p, event-class-addr=%p, "
1395 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1397 notit
, notit
->meta
.ec
,
1398 notit
->meta
.ec
->name
->str
,
1401 status
= read_dscope_begin_state(notit
, event_payload_fc
,
1402 STATE_EMIT_MSG_EVENT
,
1403 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
1404 notit
->dscopes
.event_payload
);
1406 BT_COMP_LOGW("Cannot decode event payload field: "
1407 "notit-addr=%p, event-class-addr=%p, "
1408 "event-class-name=\"%s\", "
1409 "event-class-id=%" PRId64
", fc-addr=%p",
1410 notit
, notit
->meta
.ec
,
1411 notit
->meta
.ec
->name
->str
,
1421 enum bt_msg_iter_status
read_event_payload_continue_state(
1422 struct bt_msg_iter
*notit
)
1424 return read_dscope_continue_state(notit
, STATE_EMIT_MSG_EVENT
);
1428 enum bt_msg_iter_status
skip_packet_padding_state(struct bt_msg_iter
*notit
)
1430 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1431 size_t bits_to_skip
;
1433 BT_ASSERT(notit
->cur_exp_packet_total_size
> 0);
1434 bits_to_skip
= notit
->cur_exp_packet_total_size
- packet_at(notit
);
1435 if (bits_to_skip
== 0) {
1436 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1439 size_t bits_to_consume
;
1441 BT_COMP_LOGD("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu",
1442 bits_to_skip
, notit
, bits_to_skip
);
1443 status
= buf_ensure_available_bits(notit
);
1444 if (status
!= BT_MSG_ITER_STATUS_OK
) {
1448 bits_to_consume
= MIN(buf_available_bits(notit
), bits_to_skip
);
1449 BT_COMP_LOGD("Skipping %zu bits of padding: notit-addr=%p, size=%zu",
1450 bits_to_consume
, notit
, bits_to_consume
);
1451 buf_consume_bits(notit
, bits_to_consume
);
1452 bits_to_skip
= notit
->cur_exp_packet_total_size
-
1454 if (bits_to_skip
== 0) {
1455 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1465 enum bt_msg_iter_status
check_emit_msg_stream_beginning_state(
1466 struct bt_msg_iter
*notit
)
1468 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1470 if (notit
->set_stream
) {
1471 status
= set_current_stream(notit
);
1472 if (status
!= BT_MSG_ITER_STATUS_OK
) {
1477 if (notit
->emit_stream_begin_msg
) {
1478 notit
->state
= STATE_EMIT_MSG_STREAM_BEGINNING
;
1480 /* Stream's first packet */
1481 notit
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS
;
1489 enum bt_msg_iter_status
check_emit_msg_discarded_events(
1490 struct bt_msg_iter
*notit
)
1492 notit
->state
= STATE_EMIT_MSG_DISCARDED_EVENTS
;
1494 if (!notit
->meta
.sc
->has_discarded_events
) {
1495 notit
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
;
1499 if (notit
->prev_packet_snapshots
.discarded_events
== UINT64_C(-1)) {
1500 if (notit
->snapshots
.discarded_events
== 0 ||
1501 notit
->snapshots
.discarded_events
== UINT64_C(-1)) {
1503 * Stream's first packet with no discarded
1504 * events or no information about discarded
1505 * events: do not emit.
1507 notit
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
;
1511 * If the previous packet has a value for this counter,
1512 * then this counter is defined for the whole stream.
1514 BT_ASSERT(notit
->snapshots
.discarded_events
!= UINT64_C(-1));
1516 if (notit
->snapshots
.discarded_events
-
1517 notit
->prev_packet_snapshots
.discarded_events
== 0) {
1519 * No discarded events since previous packet: do
1522 notit
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
;
1527 return BT_MSG_ITER_STATUS_OK
;
1531 enum bt_msg_iter_status
check_emit_msg_discarded_packets(
1532 struct bt_msg_iter
*notit
)
1534 notit
->state
= STATE_EMIT_MSG_DISCARDED_PACKETS
;
1536 if (!notit
->meta
.sc
->has_discarded_packets
) {
1537 notit
->state
= STATE_EMIT_MSG_PACKET_BEGINNING
;
1541 if (notit
->prev_packet_snapshots
.packets
== UINT64_C(-1)) {
1543 * Stream's first packet or no information about
1544 * discarded packets: do not emit. In other words, if
1545 * this is the first packet and its sequence number is
1546 * not 0, do not consider that packets were previously
1547 * lost: we might be reading a partial stream (LTTng
1548 * snapshot for example).
1550 notit
->state
= STATE_EMIT_MSG_PACKET_BEGINNING
;
1553 * If the previous packet has a value for this counter,
1554 * then this counter is defined for the whole stream.
1556 BT_ASSERT(notit
->snapshots
.packets
!= UINT64_C(-1));
1558 if (notit
->snapshots
.packets
-
1559 notit
->prev_packet_snapshots
.packets
<= 1) {
1561 * No discarded packets since previous packet:
1564 notit
->state
= STATE_EMIT_MSG_PACKET_BEGINNING
;
1569 return BT_MSG_ITER_STATUS_OK
;
1573 enum bt_msg_iter_status
check_emit_msg_stream_activity_end(
1574 struct bt_msg_iter
*notit
)
1576 if (notit
->emit_stream_end_msg
) {
1577 notit
->state
= STATE_EMIT_MSG_STREAM_ACTIVITY_END
;
1579 notit
->state
= STATE_DONE
;
1582 return BT_MSG_ITER_STATUS_OK
;
1586 enum bt_msg_iter_status
handle_state(struct bt_msg_iter
*notit
)
1588 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1589 const enum state state
= notit
->state
;
1591 BT_COMP_LOGT("Handling state: notit-addr=%p, state=%s",
1592 notit
, state_string(state
));
1594 // TODO: optimalize!
1597 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1599 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
1600 status
= read_packet_header_begin_state(notit
);
1602 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
1603 status
= read_packet_header_continue_state(notit
);
1605 case STATE_AFTER_TRACE_PACKET_HEADER
:
1606 status
= after_packet_header_state(notit
);
1608 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
1609 status
= read_packet_context_begin_state(notit
);
1611 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
1612 status
= read_packet_context_continue_state(notit
);
1614 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
1615 status
= after_packet_context_state(notit
);
1617 case STATE_CHECK_EMIT_MSG_STREAM_BEGINNING
:
1618 status
= check_emit_msg_stream_beginning_state(notit
);
1620 case STATE_EMIT_MSG_STREAM_BEGINNING
:
1621 notit
->state
= STATE_EMIT_MSG_STREAM_ACTIVITY_BEGINNING
;
1623 case STATE_EMIT_MSG_STREAM_ACTIVITY_BEGINNING
:
1624 notit
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS
;
1626 case STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS
:
1627 status
= check_emit_msg_discarded_events(notit
);
1629 case STATE_EMIT_MSG_DISCARDED_EVENTS
:
1630 notit
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
;
1632 case STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
:
1633 status
= check_emit_msg_discarded_packets(notit
);
1635 case STATE_EMIT_MSG_DISCARDED_PACKETS
:
1636 notit
->state
= STATE_EMIT_MSG_PACKET_BEGINNING
;
1638 case STATE_EMIT_MSG_PACKET_BEGINNING
:
1639 notit
->state
= STATE_DSCOPE_EVENT_HEADER_BEGIN
;
1641 case STATE_DSCOPE_EVENT_HEADER_BEGIN
:
1642 status
= read_event_header_begin_state(notit
);
1644 case STATE_DSCOPE_EVENT_HEADER_CONTINUE
:
1645 status
= read_event_header_continue_state(notit
);
1647 case STATE_AFTER_EVENT_HEADER
:
1648 status
= after_event_header_state(notit
);
1650 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
:
1651 status
= read_event_common_context_begin_state(notit
);
1653 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
:
1654 status
= read_event_common_context_continue_state(notit
);
1656 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
:
1657 status
= read_event_spec_context_begin_state(notit
);
1659 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
:
1660 status
= read_event_spec_context_continue_state(notit
);
1662 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
1663 status
= read_event_payload_begin_state(notit
);
1665 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
1666 status
= read_event_payload_continue_state(notit
);
1668 case STATE_EMIT_MSG_EVENT
:
1669 notit
->state
= STATE_DSCOPE_EVENT_HEADER_BEGIN
;
1671 case STATE_SKIP_PACKET_PADDING
:
1672 status
= skip_packet_padding_state(notit
);
1674 case STATE_EMIT_MSG_PACKET_END_MULTI
:
1675 notit
->state
= STATE_SKIP_PACKET_PADDING
;
1677 case STATE_EMIT_MSG_PACKET_END_SINGLE
:
1678 notit
->state
= STATE_CHECK_EMIT_MSG_STREAM_ACTIVITY_END
;
1680 case STATE_CHECK_EMIT_MSG_STREAM_ACTIVITY_END
:
1681 status
= check_emit_msg_stream_activity_end(notit
);
1683 case STATE_EMIT_MSG_STREAM_ACTIVITY_END
:
1684 notit
->state
= STATE_EMIT_MSG_STREAM_END
;
1686 case STATE_EMIT_MSG_STREAM_END
:
1687 notit
->state
= STATE_DONE
;
1692 BT_COMP_LOGF("Unknown CTF plugin message iterator state: "
1693 "notit-addr=%p, state=%d", notit
, notit
->state
);
1697 BT_COMP_LOGT("Handled state: notit-addr=%p, status=%s, "
1698 "prev-state=%s, cur-state=%s",
1699 notit
, bt_msg_iter_status_string(status
),
1700 state_string(state
), state_string(notit
->state
));
1705 void bt_msg_iter_reset_for_next_stream_file(struct bt_msg_iter
*notit
)
1708 BT_COMP_LOGD("Resetting message iterator: addr=%p", notit
);
1709 stack_clear(notit
->stack
);
1710 notit
->meta
.sc
= NULL
;
1711 notit
->meta
.ec
= NULL
;
1712 BT_PACKET_PUT_REF_AND_RESET(notit
->packet
);
1713 BT_STREAM_PUT_REF_AND_RESET(notit
->stream
);
1714 BT_MESSAGE_PUT_REF_AND_RESET(notit
->event_msg
);
1715 release_all_dscopes(notit
);
1716 notit
->cur_dscope_field
= NULL
;
1718 if (notit
->packet_context_field
) {
1719 bt_packet_context_field_release(notit
->packet_context_field
);
1720 notit
->packet_context_field
= NULL
;
1723 notit
->buf
.addr
= NULL
;
1726 notit
->buf
.last_eh_at
= SIZE_MAX
;
1727 notit
->buf
.packet_offset
= 0;
1728 notit
->state
= STATE_INIT
;
1729 notit
->cur_exp_packet_content_size
= -1;
1730 notit
->cur_exp_packet_total_size
= -1;
1731 notit
->cur_packet_offset
= -1;
1732 notit
->cur_event_class_id
= -1;
1733 notit
->snapshots
.beginning_clock
= UINT64_C(-1);
1734 notit
->snapshots
.end_clock
= UINT64_C(-1);
1738 * Resets the internal state of a CTF message iterator.
1741 void bt_msg_iter_reset(struct bt_msg_iter
*notit
)
1743 bt_msg_iter_reset_for_next_stream_file(notit
);
1744 notit
->cur_stream_class_id
= -1;
1745 notit
->cur_data_stream_id
= -1;
1746 notit
->emit_stream_begin_msg
= true;
1747 notit
->emit_stream_end_msg
= true;
1748 notit
->snapshots
.discarded_events
= UINT64_C(-1);
1749 notit
->snapshots
.packets
= UINT64_C(-1);
1750 notit
->prev_packet_snapshots
.discarded_events
= UINT64_C(-1);
1751 notit
->prev_packet_snapshots
.packets
= UINT64_C(-1);
1752 notit
->prev_packet_snapshots
.beginning_clock
= UINT64_C(-1);
1753 notit
->prev_packet_snapshots
.end_clock
= UINT64_C(-1);
1757 int bt_msg_iter_switch_packet(struct bt_msg_iter
*notit
)
1762 * We don't put the stream class here because we need to make
1763 * sure that all the packets processed by the same message
1764 * iterator refer to the same stream class (the first one).
1768 if (notit
->cur_exp_packet_total_size
!= -1) {
1769 notit
->cur_packet_offset
+= notit
->cur_exp_packet_total_size
;
1772 BT_COMP_LOGD("Switching packet: notit-addr=%p, cur=%zu, "
1773 "packet-offset=%" PRId64
, notit
, notit
->buf
.at
,
1774 notit
->cur_packet_offset
);
1775 stack_clear(notit
->stack
);
1776 notit
->meta
.ec
= NULL
;
1777 BT_PACKET_PUT_REF_AND_RESET(notit
->packet
);
1778 BT_MESSAGE_PUT_REF_AND_RESET(notit
->event_msg
);
1779 release_all_dscopes(notit
);
1780 notit
->cur_dscope_field
= NULL
;
1783 * Adjust current buffer so that addr points to the beginning of the new
1786 if (notit
->buf
.addr
) {
1787 size_t consumed_bytes
= (size_t) (notit
->buf
.at
/ CHAR_BIT
);
1789 /* Packets are assumed to start on a byte frontier. */
1790 if (notit
->buf
.at
% CHAR_BIT
) {
1791 BT_COMP_LOGW("Cannot switch packet: current position is not a multiple of 8: "
1792 "notit-addr=%p, cur=%zu", notit
, notit
->buf
.at
);
1797 notit
->buf
.addr
+= consumed_bytes
;
1798 notit
->buf
.sz
-= consumed_bytes
;
1800 notit
->buf
.packet_offset
= 0;
1801 BT_COMP_LOGD("Adjusted buffer: addr=%p, size=%zu",
1802 notit
->buf
.addr
, notit
->buf
.sz
);
1805 notit
->cur_exp_packet_content_size
= -1;
1806 notit
->cur_exp_packet_total_size
= -1;
1807 notit
->cur_stream_class_id
= -1;
1808 notit
->cur_event_class_id
= -1;
1809 notit
->cur_data_stream_id
= -1;
1810 notit
->prev_packet_snapshots
= notit
->snapshots
;
1811 notit
->snapshots
.discarded_events
= UINT64_C(-1);
1812 notit
->snapshots
.packets
= UINT64_C(-1);
1813 notit
->snapshots
.beginning_clock
= UINT64_C(-1);
1814 notit
->snapshots
.end_clock
= UINT64_C(-1);
1821 bt_field
*borrow_next_field(struct bt_msg_iter
*notit
)
1823 bt_field
*next_field
= NULL
;
1824 bt_field
*base_field
;
1825 const bt_field_class
*base_fc
;
1828 BT_ASSERT(!stack_empty(notit
->stack
));
1829 index
= stack_top(notit
->stack
)->index
;
1830 base_field
= stack_top(notit
->stack
)->base
;
1831 BT_ASSERT(base_field
);
1832 base_fc
= bt_field_borrow_class_const(base_field
);
1835 switch (bt_field_class_get_type(base_fc
)) {
1836 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
1839 bt_field_class_structure_get_member_count(
1840 bt_field_borrow_class_const(
1843 bt_field_structure_borrow_member_field_by_index(
1847 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
1848 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
1849 BT_ASSERT(index
< bt_field_array_get_length(base_field
));
1850 next_field
= bt_field_array_borrow_element_field_by_index(
1853 case BT_FIELD_CLASS_TYPE_VARIANT
:
1854 BT_ASSERT(index
== 0);
1855 next_field
= bt_field_variant_borrow_selected_option_field(
1862 BT_ASSERT(next_field
);
1867 void update_default_clock(struct bt_msg_iter
*notit
, uint64_t new_val
,
1868 uint64_t new_val_size
)
1870 uint64_t new_val_mask
;
1871 uint64_t cur_value_masked
;
1873 BT_ASSERT(new_val_size
> 0);
1876 * Special case for a 64-bit new value, which is the limit
1877 * of a clock value as of this version: overwrite the
1878 * current value directly.
1880 if (new_val_size
== 64) {
1881 notit
->default_clock_snapshot
= new_val
;
1885 new_val_mask
= (1ULL << new_val_size
) - 1;
1886 cur_value_masked
= notit
->default_clock_snapshot
& new_val_mask
;
1888 if (new_val
< cur_value_masked
) {
1890 * It looks like a wrap happened on the number of bits
1891 * of the requested new value. Assume that the clock
1892 * value wrapped only one time.
1894 notit
->default_clock_snapshot
+= new_val_mask
+ 1;
1897 /* Clear the low bits of the current clock value. */
1898 notit
->default_clock_snapshot
&= ~new_val_mask
;
1900 /* Set the low bits of the current clock value. */
1901 notit
->default_clock_snapshot
|= new_val
;
1904 BT_COMP_LOGT("Updated default clock's value from integer field's value: "
1905 "value=%" PRIu64
, notit
->default_clock_snapshot
);
1909 enum bt_bfcr_status
bfcr_unsigned_int_cb(uint64_t value
,
1910 struct ctf_field_class
*fc
, void *data
)
1912 struct bt_msg_iter
*notit
= data
;
1913 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
1914 bt_field
*field
= NULL
;
1915 struct ctf_field_class_int
*int_fc
= (void *) fc
;
1917 BT_COMP_LOGT("Unsigned integer function called from BFCR: "
1918 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1919 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64
,
1920 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
1922 if (G_LIKELY(int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
)) {
1923 goto update_def_clock
;
1926 switch (int_fc
->meaning
) {
1927 case CTF_FIELD_CLASS_MEANING_EVENT_CLASS_ID
:
1928 notit
->cur_event_class_id
= value
;
1930 case CTF_FIELD_CLASS_MEANING_DATA_STREAM_ID
:
1931 notit
->cur_data_stream_id
= value
;
1933 case CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME
:
1934 notit
->snapshots
.beginning_clock
= value
;
1936 case CTF_FIELD_CLASS_MEANING_PACKET_END_TIME
:
1937 notit
->snapshots
.end_clock
= value
;
1939 case CTF_FIELD_CLASS_MEANING_STREAM_CLASS_ID
:
1940 notit
->cur_stream_class_id
= value
;
1942 case CTF_FIELD_CLASS_MEANING_MAGIC
:
1943 if (value
!= 0xc1fc1fc1) {
1944 BT_COMP_LOGW("Invalid CTF magic number: notit-addr=%p, "
1945 "magic=%" PRIx64
, notit
, value
);
1946 status
= BT_BFCR_STATUS_ERROR
;
1951 case CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT
:
1952 notit
->snapshots
.packets
= value
;
1954 case CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT
:
1955 notit
->snapshots
.discarded_events
= value
;
1957 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_TOTAL_SIZE
:
1958 notit
->cur_exp_packet_total_size
= value
;
1960 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_CONTENT_SIZE
:
1961 notit
->cur_exp_packet_content_size
= value
;
1968 if (G_UNLIKELY(int_fc
->mapped_clock_class
)) {
1969 update_default_clock(notit
, value
, int_fc
->base
.size
);
1972 if (G_UNLIKELY(int_fc
->storing_index
>= 0)) {
1973 g_array_index(notit
->stored_values
, uint64_t,
1974 (uint64_t) int_fc
->storing_index
) = value
;
1977 if (G_UNLIKELY(!fc
->in_ir
)) {
1981 field
= borrow_next_field(notit
);
1983 BT_ASSERT(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
1984 BT_ASSERT(bt_field_get_class_type(field
) ==
1985 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
||
1986 bt_field_get_class_type(field
) ==
1987 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
);
1988 bt_field_unsigned_integer_set_value(field
, value
);
1989 stack_top(notit
->stack
)->index
++;
1996 enum bt_bfcr_status
bfcr_unsigned_int_char_cb(uint64_t value
,
1997 struct ctf_field_class
*fc
, void *data
)
2000 struct bt_msg_iter
*notit
= data
;
2001 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
2002 bt_field
*string_field
= NULL
;
2003 struct ctf_field_class_int
*int_fc
= (void *) fc
;
2004 char str
[2] = {'\0', '\0'};
2006 BT_COMP_LOGT("Unsigned integer character function called from BFCR: "
2007 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2008 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64
,
2009 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
2010 BT_ASSERT(int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
);
2011 BT_ASSERT(!int_fc
->mapped_clock_class
);
2012 BT_ASSERT(int_fc
->storing_index
< 0);
2014 if (G_UNLIKELY(!fc
->in_ir
)) {
2018 if (notit
->done_filling_string
) {
2023 notit
->done_filling_string
= true;
2027 string_field
= stack_top(notit
->stack
)->base
;
2028 BT_ASSERT(bt_field_get_class_type(string_field
) ==
2029 BT_FIELD_CLASS_TYPE_STRING
);
2031 /* Append character */
2032 str
[0] = (char) value
;
2033 ret
= bt_field_string_append_with_length(string_field
, str
, 1);
2035 BT_COMP_LOGE("Cannot append character to string field's value: "
2036 "notit-addr=%p, field-addr=%p, ret=%d",
2037 notit
, string_field
, ret
);
2038 status
= BT_BFCR_STATUS_ERROR
;
2047 enum bt_bfcr_status
bfcr_signed_int_cb(int64_t value
,
2048 struct ctf_field_class
*fc
, void *data
)
2050 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
2051 bt_field
*field
= NULL
;
2052 struct bt_msg_iter
*notit
= data
;
2053 struct ctf_field_class_int
*int_fc
= (void *) fc
;
2055 BT_COMP_LOGT("Signed integer function called from BFCR: "
2056 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2057 "fc-type=%d, fc-in-ir=%d, value=%" PRId64
,
2058 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
2059 BT_ASSERT(int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
);
2061 if (G_UNLIKELY(int_fc
->storing_index
>= 0)) {
2062 g_array_index(notit
->stored_values
, uint64_t,
2063 (uint64_t) int_fc
->storing_index
) = (uint64_t) value
;
2066 if (G_UNLIKELY(!fc
->in_ir
)) {
2070 field
= borrow_next_field(notit
);
2072 BT_ASSERT(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
2073 BT_ASSERT(bt_field_get_class_type(field
) ==
2074 BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
||
2075 bt_field_get_class_type(field
) ==
2076 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
);
2077 bt_field_signed_integer_set_value(field
, value
);
2078 stack_top(notit
->stack
)->index
++;
2085 enum bt_bfcr_status
bfcr_floating_point_cb(double value
,
2086 struct ctf_field_class
*fc
, void *data
)
2088 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
2089 bt_field
*field
= NULL
;
2090 struct bt_msg_iter
*notit
= data
;
2092 BT_COMP_LOGT("Floating point number function called from BFCR: "
2093 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2094 "fc-type=%d, fc-in-ir=%d, value=%f",
2095 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
2097 if (G_UNLIKELY(!fc
->in_ir
)) {
2101 field
= borrow_next_field(notit
);
2103 BT_ASSERT(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
2104 BT_ASSERT(bt_field_get_class_type(field
) ==
2105 BT_FIELD_CLASS_TYPE_REAL
);
2106 bt_field_real_set_value(field
, value
);
2107 stack_top(notit
->stack
)->index
++;
2114 enum bt_bfcr_status
bfcr_string_begin_cb(
2115 struct ctf_field_class
*fc
, void *data
)
2117 bt_field
*field
= NULL
;
2118 struct bt_msg_iter
*notit
= data
;
2120 BT_COMP_LOGT("String (beginning) function called from BFCR: "
2121 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2122 "fc-type=%d, fc-in-ir=%d",
2123 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
2125 if (G_UNLIKELY(!fc
->in_ir
)) {
2129 field
= borrow_next_field(notit
);
2131 BT_ASSERT(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
2132 BT_ASSERT(bt_field_get_class_type(field
) ==
2133 BT_FIELD_CLASS_TYPE_STRING
);
2134 bt_field_string_clear(field
);
2137 * Push on stack. Not a compound class per se, but we know that
2138 * only bfcr_string_cb() may be called between this call and a
2139 * subsequent call to bfcr_string_end_cb().
2141 stack_push(notit
->stack
, field
);
2144 return BT_BFCR_STATUS_OK
;
2148 enum bt_bfcr_status
bfcr_string_cb(const char *value
,
2149 size_t len
, struct ctf_field_class
*fc
, void *data
)
2151 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
2152 bt_field
*field
= NULL
;
2153 struct bt_msg_iter
*notit
= data
;
2156 BT_COMP_LOGT("String (substring) function called from BFCR: "
2157 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2158 "fc-type=%d, fc-in-ir=%d, string-length=%zu",
2159 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
,
2162 if (G_UNLIKELY(!fc
->in_ir
)) {
2166 field
= stack_top(notit
->stack
)->base
;
2169 /* Append current substring */
2170 ret
= bt_field_string_append_with_length(field
, value
, len
);
2172 BT_COMP_LOGE("Cannot append substring to string field's value: "
2173 "notit-addr=%p, field-addr=%p, string-length=%zu, "
2174 "ret=%d", notit
, field
, len
, ret
);
2175 status
= BT_BFCR_STATUS_ERROR
;
2184 enum bt_bfcr_status
bfcr_string_end_cb(
2185 struct ctf_field_class
*fc
, void *data
)
2187 struct bt_msg_iter
*notit
= data
;
2189 BT_COMP_LOGT("String (end) function called from BFCR: "
2190 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2191 "fc-type=%d, fc-in-ir=%d",
2192 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
2194 if (G_UNLIKELY(!fc
->in_ir
)) {
2198 /* Pop string field */
2199 stack_pop(notit
->stack
);
2201 /* Go to next field */
2202 stack_top(notit
->stack
)->index
++;
2205 return BT_BFCR_STATUS_OK
;
2208 enum bt_bfcr_status
bfcr_compound_begin_cb(
2209 struct ctf_field_class
*fc
, void *data
)
2211 struct bt_msg_iter
*notit
= data
;
2214 BT_COMP_LOGT("Compound (beginning) function called from BFCR: "
2215 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2216 "fc-type=%d, fc-in-ir=%d",
2217 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
2224 if (stack_empty(notit
->stack
)) {
2225 /* Root: already set by read_dscope_begin_state() */
2226 field
= notit
->cur_dscope_field
;
2228 field
= borrow_next_field(notit
);
2234 BT_ASSERT(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
2235 stack_push(notit
->stack
, field
);
2238 * Change BFCR "unsigned int" callback if it's a text
2241 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
||
2242 fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
2243 struct ctf_field_class_array_base
*array_fc
= (void *) fc
;
2245 if (array_fc
->is_text
) {
2246 BT_ASSERT(bt_field_get_class_type(field
) ==
2247 BT_FIELD_CLASS_TYPE_STRING
);
2248 notit
->done_filling_string
= false;
2249 bt_field_string_clear(field
);
2250 bt_bfcr_set_unsigned_int_cb(notit
->bfcr
,
2251 bfcr_unsigned_int_char_cb
);
2256 return BT_BFCR_STATUS_OK
;
2259 enum bt_bfcr_status
bfcr_compound_end_cb(
2260 struct ctf_field_class
*fc
, void *data
)
2262 struct bt_msg_iter
*notit
= data
;
2264 BT_COMP_LOGT("Compound (end) function called from BFCR: "
2265 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2266 "fc-type=%d, fc-in-ir=%d",
2267 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
2273 BT_ASSERT(!stack_empty(notit
->stack
));
2274 BT_ASSERT(bt_field_borrow_class_const(stack_top(notit
->stack
)->base
) ==
2278 * Reset BFCR "unsigned int" callback if it's a text
2281 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
||
2282 fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
2283 struct ctf_field_class_array_base
*array_fc
= (void *) fc
;
2285 if (array_fc
->is_text
) {
2286 BT_ASSERT(bt_field_get_class_type(
2287 stack_top(notit
->stack
)->base
) ==
2288 BT_FIELD_CLASS_TYPE_STRING
);
2289 bt_bfcr_set_unsigned_int_cb(notit
->bfcr
,
2290 bfcr_unsigned_int_cb
);
2295 stack_pop(notit
->stack
);
2297 /* If the stack is not empty, increment the base's index */
2298 if (!stack_empty(notit
->stack
)) {
2299 stack_top(notit
->stack
)->index
++;
2303 return BT_BFCR_STATUS_OK
;
2307 int64_t bfcr_get_sequence_length_cb(struct ctf_field_class
*fc
, void *data
)
2309 bt_field
*seq_field
;
2310 struct bt_msg_iter
*notit
= data
;
2311 struct ctf_field_class_sequence
*seq_fc
= (void *) fc
;
2312 int64_t length
= -1;
2315 length
= (uint64_t) g_array_index(notit
->stored_values
, uint64_t,
2316 seq_fc
->stored_length_index
);
2317 seq_field
= stack_top(notit
->stack
)->base
;
2318 BT_ASSERT(seq_field
);
2321 * bfcr_get_sequence_length_cb() also gets called back for a
2322 * text sequence, but the destination field is a string field.
2323 * Only set the field's sequence length if the destination field
2324 * is a sequence field.
2326 if (!seq_fc
->base
.is_text
) {
2327 BT_ASSERT(bt_field_get_class_type(seq_field
) ==
2328 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
);
2329 ret
= bt_field_dynamic_array_set_length(seq_field
,
2332 BT_COMP_LOGE("Cannot set dynamic array field's length field: "
2333 "notit-addr=%p, field-addr=%p, "
2334 "length=%" PRIu64
, notit
, seq_field
, length
);
2342 struct ctf_field_class
*bfcr_borrow_variant_selected_field_class_cb(
2343 struct ctf_field_class
*fc
, void *data
)
2347 int64_t option_index
= -1;
2348 struct bt_msg_iter
*notit
= data
;
2349 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
2350 struct ctf_named_field_class
*selected_option
= NULL
;
2351 struct ctf_field_class
*ret_fc
= NULL
;
2357 /* Get variant's tag */
2358 tag
.u
= g_array_index(notit
->stored_values
, uint64_t,
2359 var_fc
->stored_tag_index
);
2362 * Check each range to find the selected option's index.
2364 if (var_fc
->tag_fc
->base
.is_signed
) {
2365 for (i
= 0; i
< var_fc
->ranges
->len
; i
++) {
2366 struct ctf_field_class_variant_range
*range
=
2367 ctf_field_class_variant_borrow_range_by_index(
2370 if (tag
.i
>= range
->range
.lower
.i
&&
2371 tag
.i
<= range
->range
.upper
.i
) {
2372 option_index
= (int64_t) range
->option_index
;
2377 for (i
= 0; i
< var_fc
->ranges
->len
; i
++) {
2378 struct ctf_field_class_variant_range
*range
=
2379 ctf_field_class_variant_borrow_range_by_index(
2382 if (tag
.u
>= range
->range
.lower
.u
&&
2383 tag
.u
<= range
->range
.upper
.u
) {
2384 option_index
= (int64_t) range
->option_index
;
2390 if (option_index
< 0) {
2391 BT_COMP_LOGW("Cannot find variant field class's option: "
2392 "notit-addr=%p, var-fc-addr=%p, u-tag=%" PRIu64
", "
2393 "i-tag=%" PRId64
, notit
, var_fc
, tag
.u
, tag
.i
);
2397 selected_option
= ctf_field_class_variant_borrow_option_by_index(
2398 var_fc
, (uint64_t) option_index
);
2400 if (selected_option
->fc
->in_ir
) {
2401 bt_field
*var_field
= stack_top(notit
->stack
)->base
;
2403 ret
= bt_field_variant_select_option_field(
2404 var_field
, option_index
);
2406 BT_COMP_LOGW("Cannot select variant field's option field: "
2407 "notit-addr=%p, var-field-addr=%p, "
2408 "opt-index=%" PRId64
, notit
, var_field
,
2414 ret_fc
= selected_option
->fc
;
2421 void create_msg_stream_beginning(struct bt_msg_iter
*notit
,
2422 bt_message
**message
)
2424 bt_message
*ret
= NULL
;
2426 BT_ASSERT(notit
->stream
);
2427 BT_ASSERT(notit
->msg_iter
);
2428 ret
= bt_message_stream_beginning_create(notit
->msg_iter
,
2431 BT_COMP_LOGE("Cannot create stream beginning message: "
2432 "notit-addr=%p, stream-addr=%p",
2433 notit
, notit
->stream
);
2441 void create_msg_stream_activity_beginning(struct bt_msg_iter
*notit
,
2442 bt_message
**message
)
2444 bt_message
*ret
= NULL
;
2446 BT_ASSERT(notit
->stream
);
2447 BT_ASSERT(notit
->msg_iter
);
2448 ret
= bt_message_stream_activity_beginning_create(notit
->msg_iter
,
2451 BT_COMP_LOGE("Cannot create stream activity beginning message: "
2452 "notit-addr=%p, stream-addr=%p",
2453 notit
, notit
->stream
);
2461 void create_msg_stream_activity_end(struct bt_msg_iter
*notit
,
2462 bt_message
**message
)
2464 bt_message
*ret
= NULL
;
2466 if (!notit
->stream
) {
2467 BT_COMP_LOGE("Cannot create stream for stream message: "
2468 "notit-addr=%p", notit
);
2472 BT_ASSERT(notit
->stream
);
2473 BT_ASSERT(notit
->msg_iter
);
2474 ret
= bt_message_stream_activity_end_create(notit
->msg_iter
,
2477 BT_COMP_LOGE("Cannot create stream activity end message: "
2478 "notit-addr=%p, stream-addr=%p",
2479 notit
, notit
->stream
);
2487 void create_msg_stream_end(struct bt_msg_iter
*notit
, bt_message
**message
)
2491 if (!notit
->stream
) {
2492 BT_COMP_LOGE("Cannot create stream for stream message: "
2493 "notit-addr=%p", notit
);
2497 BT_ASSERT(notit
->msg_iter
);
2498 ret
= bt_message_stream_end_create(notit
->msg_iter
,
2501 BT_COMP_LOGE("Cannot create stream end message: "
2502 "notit-addr=%p, stream-addr=%p",
2503 notit
, notit
->stream
);
2511 void create_msg_packet_beginning(struct bt_msg_iter
*notit
,
2512 bt_message
**message
)
2515 enum bt_msg_iter_status status
;
2516 bt_message
*msg
= NULL
;
2517 const bt_stream_class
*sc
;
2519 status
= set_current_packet(notit
);
2520 if (status
!= BT_MSG_ITER_STATUS_OK
) {
2524 BT_ASSERT(notit
->packet
);
2525 sc
= notit
->meta
.sc
->ir_sc
;
2528 if (notit
->packet_context_field
) {
2529 ret
= bt_packet_move_context_field(
2530 notit
->packet
, notit
->packet_context_field
);
2535 notit
->packet_context_field
= NULL
;
2538 * At this point notit->dscopes.stream_packet_context
2539 * has the same value as the packet context field within
2542 BT_ASSERT(bt_packet_borrow_context_field(
2544 notit
->dscopes
.stream_packet_context
);
2547 BT_ASSERT(notit
->msg_iter
);
2549 if (notit
->meta
.sc
->packets_have_ts_begin
) {
2550 BT_ASSERT(notit
->snapshots
.beginning_clock
!= UINT64_C(-1));
2551 msg
= bt_message_packet_beginning_create_with_default_clock_snapshot(
2552 notit
->msg_iter
, notit
->packet
,
2553 notit
->snapshots
.beginning_clock
);
2555 msg
= bt_message_packet_beginning_create(notit
->msg_iter
,
2560 BT_COMP_LOGE("Cannot create packet beginning message: "
2561 "notit-addr=%p, packet-addr=%p",
2562 notit
, notit
->packet
);
2573 void create_msg_packet_end(struct bt_msg_iter
*notit
, bt_message
**message
)
2577 if (!notit
->packet
) {
2581 /* Update default clock from packet's end time */
2582 if (notit
->snapshots
.end_clock
!= UINT64_C(-1)) {
2583 notit
->default_clock_snapshot
= notit
->snapshots
.end_clock
;
2586 BT_ASSERT(notit
->msg_iter
);
2588 if (notit
->meta
.sc
->packets_have_ts_end
) {
2589 BT_ASSERT(notit
->snapshots
.end_clock
!= UINT64_C(-1));
2590 msg
= bt_message_packet_end_create_with_default_clock_snapshot(
2591 notit
->msg_iter
, notit
->packet
,
2592 notit
->snapshots
.end_clock
);
2594 msg
= bt_message_packet_end_create(notit
->msg_iter
,
2599 BT_COMP_LOGE("Cannot create packet end message: "
2600 "notit-addr=%p, packet-addr=%p",
2601 notit
, notit
->packet
);
2606 BT_PACKET_PUT_REF_AND_RESET(notit
->packet
);
2611 void create_msg_discarded_events(struct bt_msg_iter
*notit
,
2612 bt_message
**message
)
2615 uint64_t beginning_raw_value
= UINT64_C(-1);
2616 uint64_t end_raw_value
= UINT64_C(-1);
2618 BT_ASSERT(notit
->msg_iter
);
2619 BT_ASSERT(notit
->stream
);
2620 BT_ASSERT(notit
->meta
.sc
->has_discarded_events
);
2622 if (notit
->meta
.sc
->discarded_events_have_default_cs
) {
2623 if (notit
->prev_packet_snapshots
.discarded_events
== UINT64_C(-1)) {
2625 * We discarded events, but before (and possibly
2626 * including) the current packet: use this packet's time
2627 * range, and do not have a specific count.
2629 beginning_raw_value
= notit
->snapshots
.beginning_clock
;
2630 end_raw_value
= notit
->snapshots
.end_clock
;
2632 beginning_raw_value
= notit
->prev_packet_snapshots
.end_clock
;
2633 end_raw_value
= notit
->snapshots
.end_clock
;
2636 BT_ASSERT(beginning_raw_value
!= UINT64_C(-1));
2637 BT_ASSERT(end_raw_value
!= UINT64_C(-1));
2638 msg
= bt_message_discarded_events_create_with_default_clock_snapshots(
2639 notit
->msg_iter
, notit
->stream
, beginning_raw_value
,
2642 msg
= bt_message_discarded_events_create(notit
->msg_iter
,
2647 BT_COMP_LOGE("Cannot create discarded events message: "
2648 "notit-addr=%p, stream-addr=%p",
2649 notit
, notit
->stream
);
2653 if (notit
->prev_packet_snapshots
.discarded_events
!= UINT64_C(-1)) {
2654 bt_message_discarded_events_set_count(msg
,
2655 notit
->snapshots
.discarded_events
-
2656 notit
->prev_packet_snapshots
.discarded_events
);
2663 void create_msg_discarded_packets(struct bt_msg_iter
*notit
,
2664 bt_message
**message
)
2668 BT_ASSERT(notit
->msg_iter
);
2669 BT_ASSERT(notit
->stream
);
2670 BT_ASSERT(notit
->meta
.sc
->has_discarded_packets
);
2671 BT_ASSERT(notit
->prev_packet_snapshots
.packets
!=
2674 if (notit
->meta
.sc
->discarded_packets_have_default_cs
) {
2675 BT_ASSERT(notit
->prev_packet_snapshots
.end_clock
!= UINT64_C(-1));
2676 BT_ASSERT(notit
->snapshots
.beginning_clock
!= UINT64_C(-1));
2677 msg
= bt_message_discarded_packets_create_with_default_clock_snapshots(
2678 notit
->msg_iter
, notit
->stream
,
2679 notit
->prev_packet_snapshots
.end_clock
,
2680 notit
->snapshots
.beginning_clock
);
2682 msg
= bt_message_discarded_packets_create(notit
->msg_iter
,
2687 BT_COMP_LOGE("Cannot create discarded packets message: "
2688 "notit-addr=%p, stream-addr=%p",
2689 notit
, notit
->stream
);
2693 bt_message_discarded_packets_set_count(msg
,
2694 notit
->snapshots
.packets
-
2695 notit
->prev_packet_snapshots
.packets
- 1);
2700 struct bt_msg_iter
*bt_msg_iter_create(struct ctf_trace_class
*tc
,
2701 size_t max_request_sz
,
2702 struct bt_msg_iter_medium_ops medops
, void *data
,
2703 bt_logging_level log_level
, bt_self_component
*self_comp
)
2705 struct bt_msg_iter
*notit
= NULL
;
2706 struct bt_bfcr_cbs cbs
= {
2708 .signed_int
= bfcr_signed_int_cb
,
2709 .unsigned_int
= bfcr_unsigned_int_cb
,
2710 .floating_point
= bfcr_floating_point_cb
,
2711 .string_begin
= bfcr_string_begin_cb
,
2712 .string
= bfcr_string_cb
,
2713 .string_end
= bfcr_string_end_cb
,
2714 .compound_begin
= bfcr_compound_begin_cb
,
2715 .compound_end
= bfcr_compound_end_cb
,
2718 .get_sequence_length
= bfcr_get_sequence_length_cb
,
2719 .borrow_variant_selected_field_class
= bfcr_borrow_variant_selected_field_class_cb
,
2724 BT_ASSERT(medops
.request_bytes
);
2725 BT_ASSERT(medops
.borrow_stream
);
2726 BT_COMP_LOG_CUR_LVL(BT_LOG_DEBUG
, log_level
, self_comp
,
2727 "Creating CTF plugin message iterator: "
2728 "trace-addr=%p, max-request-size=%zu, "
2729 "data=%p, log-level=%s", tc
, max_request_sz
, data
,
2730 bt_common_logging_level_string(log_level
));
2731 notit
= g_new0(struct bt_msg_iter
, 1);
2733 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_comp
,
2734 "Failed to allocate one CTF plugin message iterator.");
2737 notit
->self_comp
= self_comp
;
2738 notit
->log_level
= log_level
;
2739 notit
->meta
.tc
= tc
;
2740 notit
->medium
.medops
= medops
;
2741 notit
->medium
.max_request_sz
= max_request_sz
;
2742 notit
->medium
.data
= data
;
2743 notit
->stack
= stack_new(notit
);
2744 notit
->stored_values
= g_array_new(FALSE
, TRUE
, sizeof(uint64_t));
2745 g_array_set_size(notit
->stored_values
, tc
->stored_value_count
);
2747 if (!notit
->stack
) {
2748 BT_COMP_LOGE_STR("Failed to create field stack.");
2752 notit
->bfcr
= bt_bfcr_create(cbs
, notit
, log_level
, NULL
);
2754 BT_COMP_LOGE_STR("Failed to create binary class reader (BFCR).");
2758 bt_msg_iter_reset(notit
);
2759 BT_COMP_LOGD("Created CTF plugin message iterator: "
2760 "trace-addr=%p, max-request-size=%zu, "
2761 "data=%p, notit-addr=%p, log-level=%s",
2762 tc
, max_request_sz
, data
, notit
,
2763 bt_common_logging_level_string(log_level
));
2764 notit
->cur_packet_offset
= 0;
2770 bt_msg_iter_destroy(notit
);
2775 void bt_msg_iter_destroy(struct bt_msg_iter
*notit
)
2777 BT_PACKET_PUT_REF_AND_RESET(notit
->packet
);
2778 BT_STREAM_PUT_REF_AND_RESET(notit
->stream
);
2779 release_all_dscopes(notit
);
2781 BT_COMP_LOGD("Destroying CTF plugin message iterator: addr=%p", notit
);
2784 BT_COMP_LOGD_STR("Destroying field stack.");
2785 stack_destroy(notit
->stack
);
2789 BT_COMP_LOGD("Destroying BFCR: bfcr-addr=%p", notit
->bfcr
);
2790 bt_bfcr_destroy(notit
->bfcr
);
2793 if (notit
->stored_values
) {
2794 g_array_free(notit
->stored_values
, TRUE
);
2800 enum bt_msg_iter_status
bt_msg_iter_get_next_message(
2801 struct bt_msg_iter
*notit
,
2802 bt_self_message_iterator
*msg_iter
, bt_message
**message
)
2804 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
2808 notit
->msg_iter
= msg_iter
;
2809 notit
->set_stream
= true;
2810 BT_COMP_LOGD("Getting next message: notit-addr=%p", notit
);
2813 status
= handle_state(notit
);
2814 if (G_UNLIKELY(status
== BT_MSG_ITER_STATUS_AGAIN
)) {
2815 BT_COMP_LOGD_STR("Medium returned BT_MSG_ITER_STATUS_AGAIN.");
2817 } else if (G_UNLIKELY(status
!= BT_MSG_ITER_STATUS_OK
)) {
2818 BT_COMP_LOGW("Cannot handle state: notit-addr=%p, state=%s",
2819 notit
, state_string(notit
->state
));
2823 switch (notit
->state
) {
2824 case STATE_EMIT_MSG_EVENT
:
2825 BT_ASSERT(notit
->event_msg
);
2826 *message
= notit
->event_msg
;
2827 notit
->event_msg
= NULL
;
2829 case STATE_EMIT_MSG_DISCARDED_EVENTS
:
2830 /* create_msg_discared_events() logs errors */
2831 create_msg_discarded_events(notit
, message
);
2834 status
= BT_MSG_ITER_STATUS_ERROR
;
2838 case STATE_EMIT_MSG_DISCARDED_PACKETS
:
2839 /* create_msg_discared_packets() logs errors */
2840 create_msg_discarded_packets(notit
, message
);
2843 status
= BT_MSG_ITER_STATUS_ERROR
;
2847 case STATE_EMIT_MSG_PACKET_BEGINNING
:
2848 /* create_msg_packet_beginning() logs errors */
2849 create_msg_packet_beginning(notit
, message
);
2852 status
= BT_MSG_ITER_STATUS_ERROR
;
2856 case STATE_EMIT_MSG_PACKET_END_SINGLE
:
2857 case STATE_EMIT_MSG_PACKET_END_MULTI
:
2858 /* create_msg_packet_end() logs errors */
2859 create_msg_packet_end(notit
, message
);
2862 status
= BT_MSG_ITER_STATUS_ERROR
;
2866 case STATE_EMIT_MSG_STREAM_ACTIVITY_BEGINNING
:
2867 /* create_msg_stream_activity_beginning() logs errors */
2868 create_msg_stream_activity_beginning(notit
, message
);
2871 status
= BT_MSG_ITER_STATUS_ERROR
;
2875 case STATE_EMIT_MSG_STREAM_ACTIVITY_END
:
2876 /* create_msg_stream_activity_end() logs errors */
2877 create_msg_stream_activity_end(notit
, message
);
2880 status
= BT_MSG_ITER_STATUS_ERROR
;
2884 case STATE_EMIT_MSG_STREAM_BEGINNING
:
2885 /* create_msg_stream_beginning() logs errors */
2886 create_msg_stream_beginning(notit
, message
);
2889 status
= BT_MSG_ITER_STATUS_ERROR
;
2893 case STATE_EMIT_MSG_STREAM_END
:
2894 /* create_msg_stream_end() logs errors */
2895 create_msg_stream_end(notit
, message
);
2898 status
= BT_MSG_ITER_STATUS_ERROR
;
2903 status
= BT_MSG_ITER_STATUS_EOF
;
2906 /* Non-emitting state: continue */
2916 enum bt_msg_iter_status
read_packet_header_context_fields(
2917 struct bt_msg_iter
*notit
)
2920 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
2923 notit
->set_stream
= false;
2925 if (notit
->state
== STATE_EMIT_MSG_PACKET_BEGINNING
) {
2926 /* We're already there */
2931 status
= handle_state(notit
);
2932 if (G_UNLIKELY(status
== BT_MSG_ITER_STATUS_AGAIN
)) {
2933 BT_COMP_LOGD_STR("Medium returned BT_MSG_ITER_STATUS_AGAIN.");
2935 } else if (G_UNLIKELY(status
!= BT_MSG_ITER_STATUS_OK
)) {
2936 BT_COMP_LOGW("Cannot handle state: notit-addr=%p, state=%s",
2937 notit
, state_string(notit
->state
));
2941 switch (notit
->state
) {
2942 case STATE_EMIT_MSG_PACKET_BEGINNING
:
2944 * Packet header and context fields are
2945 * potentially decoded (or they don't exist).
2949 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
2950 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
2951 case STATE_AFTER_TRACE_PACKET_HEADER
:
2952 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
2953 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
2954 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
2955 case STATE_CHECK_EMIT_MSG_STREAM_BEGINNING
:
2956 case STATE_EMIT_MSG_STREAM_BEGINNING
:
2957 case STATE_EMIT_MSG_STREAM_ACTIVITY_BEGINNING
:
2958 case STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS
:
2959 case STATE_EMIT_MSG_DISCARDED_EVENTS
:
2960 case STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
:
2961 case STATE_EMIT_MSG_DISCARDED_PACKETS
:
2962 /* Non-emitting state: continue */
2966 * We should never get past the
2967 * STATE_EMIT_MSG_PACKET_BEGINNING state.
2969 BT_COMP_LOGF("Unexpected state: notit-addr=%p, state=%s",
2970 notit
, state_string(notit
->state
));
2976 ret
= set_current_packet_content_sizes(notit
);
2978 status
= BT_MSG_ITER_STATUS_ERROR
;
2985 void bt_msg_iter_set_medops_data(struct bt_msg_iter
*notit
,
2989 notit
->medium
.data
= medops_data
;
2993 enum bt_msg_iter_status
bt_msg_iter_seek(struct bt_msg_iter
*notit
,
2996 enum bt_msg_iter_status ret
= BT_MSG_ITER_STATUS_OK
;
2997 enum bt_msg_iter_medium_status medium_status
;
3001 BT_COMP_LOGE("Cannot seek to negative offset: offset=%jd", offset
);
3002 ret
= BT_MSG_ITER_STATUS_INVAL
;
3006 if (!notit
->medium
.medops
.seek
) {
3007 ret
= BT_MSG_ITER_STATUS_UNSUPPORTED
;
3008 BT_COMP_LOGD("Aborting seek as the iterator's underlying media does not implement seek support.");
3012 medium_status
= notit
->medium
.medops
.seek(
3013 BT_MSG_ITER_SEEK_WHENCE_SET
, offset
, notit
->medium
.data
);
3014 if (medium_status
!= BT_MSG_ITER_MEDIUM_STATUS_OK
) {
3015 if (medium_status
== BT_MSG_ITER_MEDIUM_STATUS_EOF
) {
3016 ret
= BT_MSG_ITER_STATUS_EOF
;
3018 ret
= BT_MSG_ITER_STATUS_ERROR
;
3023 bt_msg_iter_reset(notit
);
3024 notit
->cur_packet_offset
= offset
;
3031 enum bt_msg_iter_status
bt_msg_iter_get_packet_properties(
3032 struct bt_msg_iter
*notit
,
3033 struct bt_msg_iter_packet_properties
*props
)
3035 enum bt_msg_iter_status status
;
3039 status
= read_packet_header_context_fields(notit
);
3040 if (status
!= BT_MSG_ITER_STATUS_OK
) {
3044 props
->exp_packet_total_size
= notit
->cur_exp_packet_total_size
;
3045 props
->exp_packet_content_size
= notit
->cur_exp_packet_content_size
;
3046 props
->stream_class_id
= (uint64_t) notit
->cur_stream_class_id
;
3047 props
->data_stream_id
= notit
->cur_data_stream_id
;
3048 props
->snapshots
.discarded_events
= notit
->snapshots
.discarded_events
;
3049 props
->snapshots
.packets
= notit
->snapshots
.packets
;
3050 props
->snapshots
.beginning_clock
= notit
->snapshots
.beginning_clock
;
3051 props
->snapshots
.end_clock
= notit
->snapshots
.end_clock
;
3058 void bt_msg_iter_set_emit_stream_beginning_message(struct bt_msg_iter
*notit
,
3061 notit
->emit_stream_begin_msg
= val
;
3065 void bt_msg_iter_set_emit_stream_end_message(struct bt_msg_iter
*notit
,
3068 notit
->emit_stream_end_msg
= val
;