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 "logging/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 */
84 STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
,
85 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
86 STATE_AFTER_TRACE_PACKET_HEADER
,
87 STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
,
88 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
89 STATE_AFTER_STREAM_PACKET_CONTEXT
,
90 STATE_CHECK_EMIT_MSG_STREAM_BEGINNING
,
91 STATE_EMIT_MSG_STREAM_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_END
,
111 STATE_EMIT_MSG_STREAM_END
,
115 struct end_of_packet_snapshots
{
116 uint64_t discarded_events
;
118 uint64_t beginning_clock
;
122 /* CTF message iterator */
127 /* Current message iterator to create messages (weak) */
128 bt_self_message_iterator
*msg_iter
;
130 /* True to emit a stream beginning message. */
131 bool emit_stream_begin_msg
;
133 /* True to emit a stream end message. */
134 bool emit_stream_end_msg
;
137 * True if library objects are unavailable during the decoding and
138 * should not be created/used.
142 /* True to set the stream */
146 * Current dynamic scope field pointer.
148 * This is set by read_dscope_begin_state() and contains the
149 * value of one of the pointers in `dscopes` below.
151 bt_field
*cur_dscope_field
;
154 * True if we're done filling a string field from a text
155 * array/sequence payload.
157 bool done_filling_string
;
159 /* Trace and classes */
160 /* True to set IR fields */
164 struct ctf_trace_class
*tc
;
165 struct ctf_stream_class
*sc
;
166 struct ctf_event_class
*ec
;
169 /* Current packet context field wrapper (NULL if not created yet) */
170 bt_packet_context_field
*packet_context_field
;
172 /* Current packet (NULL if not created yet) */
175 /* Current stream (NULL if not set yet) */
178 /* Current event (NULL if not created yet) */
181 /* Current event message (NULL if not created yet) */
182 bt_message
*event_msg
;
184 /* Database of current dynamic scopes */
186 bt_field
*stream_packet_context
;
187 bt_field
*event_common_context
;
188 bt_field
*event_spec_context
;
189 bt_field
*event_payload
;
195 /* Current medium buffer data */
197 /* Last address provided by medium */
200 /* Buffer size provided by medium (bytes) */
203 /* Offset within whole packet of addr (bits) */
204 size_t packet_offset
;
206 /* Current position from addr (bits) */
209 /* Position of the last event header from addr (bits) */
213 /* Binary type reader */
214 struct bt_bfcr
*bfcr
;
216 /* Current medium data */
218 struct bt_msg_iter_medium_ops medops
;
219 size_t max_request_sz
;
223 /* Current packet size (bits) (-1 if unknown) */
224 int64_t cur_exp_packet_total_size
;
226 /* Current content size (bits) (-1 if unknown) */
227 int64_t cur_exp_packet_content_size
;
229 /* Current stream class ID */
230 int64_t cur_stream_class_id
;
232 /* Current event class ID */
233 int64_t cur_event_class_id
;
235 /* Current data stream ID */
236 int64_t cur_data_stream_id
;
239 * Offset, in the underlying media, of the current packet's
240 * start (-1 if unknown).
242 off_t cur_packet_offset
;
244 /* Default clock's current value */
245 uint64_t default_clock_snapshot
;
247 /* End of current packet snapshots */
248 struct end_of_packet_snapshots snapshots
;
250 /* End of previous packet snapshots */
251 struct end_of_packet_snapshots prev_packet_snapshots
;
253 /* Stored values (for sequence lengths, variant tags) */
254 GArray
*stored_values
;
256 /* Iterator's current log level */
257 bt_logging_level log_level
;
259 /* Iterator's owning self component, or `NULL` if none (query) */
260 bt_self_component
*self_comp
;
264 const char *state_string(enum state state
)
269 case STATE_SWITCH_PACKET
:
270 return "SWITCH_PACKET";
271 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
272 return "DSCOPE_TRACE_PACKET_HEADER_BEGIN";
273 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
274 return "DSCOPE_TRACE_PACKET_HEADER_CONTINUE";
275 case STATE_AFTER_TRACE_PACKET_HEADER
:
276 return "AFTER_TRACE_PACKET_HEADER";
277 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
278 return "DSCOPE_STREAM_PACKET_CONTEXT_BEGIN";
279 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
280 return "DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE";
281 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
282 return "AFTER_STREAM_PACKET_CONTEXT";
283 case STATE_EMIT_MSG_STREAM_BEGINNING
:
284 return "EMIT_MSG_STREAM_BEGINNING";
285 case STATE_EMIT_MSG_PACKET_BEGINNING
:
286 return "EMIT_MSG_PACKET_BEGINNING";
287 case STATE_EMIT_MSG_DISCARDED_EVENTS
:
288 return "EMIT_MSG_DISCARDED_EVENTS";
289 case STATE_EMIT_MSG_DISCARDED_PACKETS
:
290 return "EMIT_MSG_DISCARDED_PACKETS";
291 case STATE_DSCOPE_EVENT_HEADER_BEGIN
:
292 return "DSCOPE_EVENT_HEADER_BEGIN";
293 case STATE_DSCOPE_EVENT_HEADER_CONTINUE
:
294 return "DSCOPE_EVENT_HEADER_CONTINUE";
295 case STATE_AFTER_EVENT_HEADER
:
296 return "AFTER_EVENT_HEADER";
297 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
:
298 return "DSCOPE_EVENT_COMMON_CONTEXT_BEGIN";
299 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
:
300 return "DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE";
301 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
:
302 return "DSCOPE_EVENT_SPEC_CONTEXT_BEGIN";
303 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
:
304 return "DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE";
305 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
306 return "DSCOPE_EVENT_PAYLOAD_BEGIN";
307 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
308 return "DSCOPE_EVENT_PAYLOAD_CONTINUE";
309 case STATE_EMIT_MSG_EVENT
:
310 return "EMIT_MSG_EVENT";
311 case STATE_SKIP_PACKET_PADDING
:
312 return "SKIP_PACKET_PADDING";
313 case STATE_EMIT_MSG_PACKET_END_MULTI
:
314 return "EMIT_MSG_PACKET_END_MULTI";
315 case STATE_EMIT_MSG_PACKET_END_SINGLE
:
316 return "EMIT_MSG_PACKET_END_SINGLE";
317 case STATE_EMIT_MSG_STREAM_END
:
318 return "EMIT_MSG_STREAM_END";
327 struct stack
*stack_new(struct bt_msg_iter
*notit
)
329 struct stack
*stack
= NULL
;
331 stack
= g_new0(struct stack
, 1);
333 BT_COMP_LOGE_STR("Failed to allocate one stack.");
337 stack
->notit
= notit
;
338 stack
->entries
= g_array_new(FALSE
, TRUE
, sizeof(struct stack_entry
));
339 if (!stack
->entries
) {
340 BT_COMP_LOGE_STR("Failed to allocate a GArray.");
344 BT_COMP_LOGD("Created stack: notit-addr=%p, stack-addr=%p", notit
, stack
);
356 void stack_destroy(struct stack
*stack
)
358 struct bt_msg_iter
*notit
;
361 notit
= stack
->notit
;
362 BT_COMP_LOGD("Destroying stack: addr=%p", stack
);
364 if (stack
->entries
) {
365 g_array_free(stack
->entries
, TRUE
);
372 void stack_push(struct stack
*stack
, bt_field
*base
)
374 struct stack_entry
*entry
;
375 struct bt_msg_iter
*notit
;
378 notit
= stack
->notit
;
380 BT_COMP_LOGT("Pushing base field on stack: stack-addr=%p, "
381 "stack-size-before=%zu, stack-size-after=%zu",
382 stack
, stack
->size
, stack
->size
+ 1);
384 if (stack
->entries
->len
== stack
->size
) {
385 g_array_set_size(stack
->entries
, stack
->size
+ 1);
388 entry
= &g_array_index(stack
->entries
, struct stack_entry
, stack
->size
);
395 unsigned int stack_size(struct stack
*stack
)
402 void stack_pop(struct stack
*stack
)
404 struct bt_msg_iter
*notit
;
407 BT_ASSERT(stack_size(stack
));
408 notit
= stack
->notit
;
409 BT_COMP_LOGT("Popping from stack: "
410 "stack-addr=%p, stack-size-before=%zu, stack-size-after=%zu",
411 stack
, stack
->size
, stack
->size
- 1);
416 struct stack_entry
*stack_top(struct stack
*stack
)
419 BT_ASSERT(stack_size(stack
));
420 return &g_array_index(stack
->entries
, struct stack_entry
,
425 bool stack_empty(struct stack
*stack
)
427 return stack_size(stack
) == 0;
431 void stack_clear(struct stack
*stack
)
438 enum bt_msg_iter_status
msg_iter_status_from_m_status(
439 enum bt_msg_iter_medium_status m_status
)
441 /* They are the same */
442 return (int) m_status
;
446 size_t buf_size_bits(struct bt_msg_iter
*notit
)
448 return notit
->buf
.sz
* 8;
452 size_t buf_available_bits(struct bt_msg_iter
*notit
)
454 return buf_size_bits(notit
) - notit
->buf
.at
;
458 size_t packet_at(struct bt_msg_iter
*notit
)
460 return notit
->buf
.packet_offset
+ notit
->buf
.at
;
464 void buf_consume_bits(struct bt_msg_iter
*notit
, size_t incr
)
466 BT_COMP_LOGT("Advancing cursor: notit-addr=%p, cur-before=%zu, cur-after=%zu",
467 notit
, notit
->buf
.at
, notit
->buf
.at
+ incr
);
468 notit
->buf
.at
+= incr
;
472 enum bt_msg_iter_status
request_medium_bytes(
473 struct bt_msg_iter
*notit
)
475 uint8_t *buffer_addr
= NULL
;
476 size_t buffer_sz
= 0;
477 enum bt_msg_iter_medium_status m_status
;
479 BT_COMP_LOGD("Calling user function (request bytes): notit-addr=%p, "
480 "request-size=%zu", notit
, notit
->medium
.max_request_sz
);
481 m_status
= notit
->medium
.medops
.request_bytes(
482 notit
->medium
.max_request_sz
, &buffer_addr
,
483 &buffer_sz
, notit
->medium
.data
);
484 BT_COMP_LOGD("User function returned: status=%s, buf-addr=%p, buf-size=%zu",
485 bt_msg_iter_medium_status_string(m_status
),
486 buffer_addr
, buffer_sz
);
487 if (m_status
== BT_MSG_ITER_MEDIUM_STATUS_OK
) {
488 BT_ASSERT(buffer_sz
!= 0);
490 /* New packet offset is old one + old size (in bits) */
491 notit
->buf
.packet_offset
+= buf_size_bits(notit
);
493 /* Restart at the beginning of the new medium buffer */
495 notit
->buf
.last_eh_at
= SIZE_MAX
;
497 /* New medium buffer size */
498 notit
->buf
.sz
= buffer_sz
;
500 /* New medium buffer address */
501 notit
->buf
.addr
= buffer_addr
;
503 BT_COMP_LOGD("User function returned new bytes: "
504 "packet-offset=%zu, cur=%zu, size=%zu, addr=%p",
505 notit
->buf
.packet_offset
, notit
->buf
.at
,
506 notit
->buf
.sz
, notit
->buf
.addr
);
507 BT_COMP_LOGD_MEM(buffer_addr
, buffer_sz
, "Returned bytes at %p:",
509 } else if (m_status
== BT_MSG_ITER_MEDIUM_STATUS_EOF
) {
511 * User returned end of stream: validate that we're not
512 * in the middle of a packet header, packet context, or
515 if (notit
->cur_exp_packet_total_size
>= 0) {
516 if (packet_at(notit
) ==
517 notit
->cur_exp_packet_total_size
) {
521 if (packet_at(notit
) == 0) {
525 if (notit
->buf
.last_eh_at
!= SIZE_MAX
&&
526 notit
->buf
.at
== notit
->buf
.last_eh_at
) {
531 /* All other states are invalid */
532 BT_COMP_LOGW("User function returned %s, but message iterator is in an unexpected state: "
533 "state=%s, cur-packet-size=%" PRId64
", cur=%zu, "
534 "packet-cur=%zu, last-eh-at=%zu",
535 bt_msg_iter_medium_status_string(m_status
),
536 state_string(notit
->state
),
537 notit
->cur_exp_packet_total_size
,
538 notit
->buf
.at
, packet_at(notit
),
539 notit
->buf
.last_eh_at
);
540 m_status
= BT_MSG_ITER_MEDIUM_STATUS_ERROR
;
541 } else if (m_status
< 0) {
542 BT_COMP_LOGW("User function failed: status=%s",
543 bt_msg_iter_medium_status_string(m_status
));
547 return msg_iter_status_from_m_status(m_status
);
551 enum bt_msg_iter_status
buf_ensure_available_bits(
552 struct bt_msg_iter
*notit
)
554 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
556 if (G_UNLIKELY(buf_available_bits(notit
) == 0)) {
558 * This _cannot_ return BT_MSG_ITER_STATUS_OK
561 status
= request_medium_bytes(notit
);
568 enum bt_msg_iter_status
read_dscope_begin_state(
569 struct bt_msg_iter
*notit
,
570 struct ctf_field_class
*dscope_fc
,
571 enum state done_state
, enum state continue_state
,
572 bt_field
*dscope_field
)
574 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
575 enum bt_bfcr_status bfcr_status
;
576 size_t consumed_bits
;
578 notit
->cur_dscope_field
= dscope_field
;
579 BT_COMP_LOGT("Starting BFCR: notit-addr=%p, bfcr-addr=%p, fc-addr=%p",
580 notit
, notit
->bfcr
, dscope_fc
);
581 consumed_bits
= bt_bfcr_start(notit
->bfcr
, dscope_fc
,
582 notit
->buf
.addr
, notit
->buf
.at
, packet_at(notit
),
583 notit
->buf
.sz
, &bfcr_status
);
584 BT_COMP_LOGT("BFCR consumed bits: size=%zu", consumed_bits
);
586 switch (bfcr_status
) {
587 case BT_BFCR_STATUS_OK
:
588 /* Field class was read completely */
589 BT_COMP_LOGT_STR("Field was completely decoded.");
590 notit
->state
= done_state
;
592 case BT_BFCR_STATUS_EOF
:
593 BT_COMP_LOGT_STR("BFCR needs more data to decode field completely.");
594 notit
->state
= continue_state
;
597 BT_COMP_LOGW("BFCR failed to start: notit-addr=%p, bfcr-addr=%p, "
598 "status=%s", notit
, notit
->bfcr
,
599 bt_bfcr_status_string(bfcr_status
));
600 status
= BT_MSG_ITER_STATUS_ERROR
;
604 /* Consume bits now since we know we're not in an error state */
605 buf_consume_bits(notit
, consumed_bits
);
612 enum bt_msg_iter_status
read_dscope_continue_state(
613 struct bt_msg_iter
*notit
, enum state done_state
)
615 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
616 enum bt_bfcr_status bfcr_status
;
617 size_t consumed_bits
;
619 BT_COMP_LOGT("Continuing BFCR: notit-addr=%p, bfcr-addr=%p",
622 status
= buf_ensure_available_bits(notit
);
623 if (status
!= BT_MSG_ITER_STATUS_OK
) {
625 BT_COMP_LOGW("Cannot ensure that buffer has at least one byte: "
626 "msg-addr=%p, status=%s",
627 notit
, bt_msg_iter_status_string(status
));
629 BT_COMP_LOGT("Cannot ensure that buffer has at least one byte: "
630 "msg-addr=%p, status=%s",
631 notit
, bt_msg_iter_status_string(status
));
637 consumed_bits
= bt_bfcr_continue(notit
->bfcr
, notit
->buf
.addr
,
638 notit
->buf
.sz
, &bfcr_status
);
639 BT_COMP_LOGT("BFCR consumed bits: size=%zu", consumed_bits
);
641 switch (bfcr_status
) {
642 case BT_BFCR_STATUS_OK
:
643 /* Type was read completely. */
644 BT_COMP_LOGT_STR("Field was completely decoded.");
645 notit
->state
= done_state
;
647 case BT_BFCR_STATUS_EOF
:
648 /* Stay in this continue state. */
649 BT_COMP_LOGT_STR("BFCR needs more data to decode field completely.");
652 BT_COMP_LOGW("BFCR failed to continue: notit-addr=%p, bfcr-addr=%p, "
653 "status=%s", notit
, notit
->bfcr
,
654 bt_bfcr_status_string(bfcr_status
));
655 status
= BT_MSG_ITER_STATUS_ERROR
;
659 /* Consume bits now since we know we're not in an error state. */
660 buf_consume_bits(notit
, consumed_bits
);
666 void release_event_dscopes(struct bt_msg_iter
*notit
)
668 notit
->dscopes
.event_common_context
= NULL
;
669 notit
->dscopes
.event_spec_context
= NULL
;
670 notit
->dscopes
.event_payload
= NULL
;
674 void release_all_dscopes(struct bt_msg_iter
*notit
)
676 notit
->dscopes
.stream_packet_context
= NULL
;
678 if (notit
->packet_context_field
) {
679 bt_packet_context_field_release(notit
->packet_context_field
);
680 notit
->packet_context_field
= NULL
;
683 release_event_dscopes(notit
);
687 enum bt_msg_iter_status
switch_packet_state(struct bt_msg_iter
*notit
)
689 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
692 * We don't put the stream class here because we need to make
693 * sure that all the packets processed by the same message
694 * iterator refer to the same stream class (the first one).
698 if (notit
->cur_exp_packet_total_size
!= -1) {
699 notit
->cur_packet_offset
+= notit
->cur_exp_packet_total_size
;
702 BT_COMP_LOGD("Switching packet: notit-addr=%p, cur=%zu, "
703 "packet-offset=%" PRId64
, notit
, notit
->buf
.at
,
704 notit
->cur_packet_offset
);
705 stack_clear(notit
->stack
);
706 notit
->meta
.ec
= NULL
;
707 BT_PACKET_PUT_REF_AND_RESET(notit
->packet
);
708 BT_MESSAGE_PUT_REF_AND_RESET(notit
->event_msg
);
709 release_all_dscopes(notit
);
710 notit
->cur_dscope_field
= NULL
;
713 * Adjust current buffer so that addr points to the beginning of the new
716 if (notit
->buf
.addr
) {
717 size_t consumed_bytes
= (size_t) (notit
->buf
.at
/ CHAR_BIT
);
719 /* Packets are assumed to start on a byte frontier. */
720 if (notit
->buf
.at
% CHAR_BIT
) {
721 BT_COMP_LOGW("Cannot switch packet: current position is not a multiple of 8: "
722 "notit-addr=%p, cur=%zu", notit
, notit
->buf
.at
);
723 status
= BT_MSG_ITER_STATUS_ERROR
;
727 notit
->buf
.addr
+= consumed_bytes
;
728 notit
->buf
.sz
-= consumed_bytes
;
730 notit
->buf
.packet_offset
= 0;
731 BT_COMP_LOGD("Adjusted buffer: addr=%p, size=%zu",
732 notit
->buf
.addr
, notit
->buf
.sz
);
735 notit
->cur_exp_packet_content_size
= -1;
736 notit
->cur_exp_packet_total_size
= -1;
737 notit
->cur_stream_class_id
= -1;
738 notit
->cur_event_class_id
= -1;
739 notit
->cur_data_stream_id
= -1;
740 notit
->prev_packet_snapshots
= notit
->snapshots
;
741 notit
->snapshots
.discarded_events
= UINT64_C(-1);
742 notit
->snapshots
.packets
= UINT64_C(-1);
743 notit
->snapshots
.beginning_clock
= UINT64_C(-1);
744 notit
->snapshots
.end_clock
= UINT64_C(-1);
745 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
752 enum bt_msg_iter_status
read_packet_header_begin_state(
753 struct bt_msg_iter
*notit
)
755 struct ctf_field_class
*packet_header_fc
= NULL
;
756 enum bt_msg_iter_status ret
= BT_MSG_ITER_STATUS_OK
;
759 * Make sure at least one bit is available for this packet. An
760 * empty packet is impossible. If we reach the end of the medium
761 * at this point, then it's considered the end of the stream.
763 ret
= buf_ensure_available_bits(notit
);
765 case BT_MSG_ITER_STATUS_OK
:
767 case BT_MSG_ITER_STATUS_EOF
:
768 ret
= BT_MSG_ITER_STATUS_OK
;
769 notit
->state
= STATE_CHECK_EMIT_MSG_STREAM_END
;
775 /* Packet header class is common to the whole trace class. */
776 packet_header_fc
= notit
->meta
.tc
->packet_header_fc
;
777 if (!packet_header_fc
) {
778 notit
->state
= STATE_AFTER_TRACE_PACKET_HEADER
;
782 notit
->cur_stream_class_id
= -1;
783 notit
->cur_event_class_id
= -1;
784 notit
->cur_data_stream_id
= -1;
785 BT_COMP_LOGD("Decoding packet header field:"
786 "notit-addr=%p, trace-class-addr=%p, fc-addr=%p",
787 notit
, notit
->meta
.tc
, packet_header_fc
);
788 ret
= read_dscope_begin_state(notit
, packet_header_fc
,
789 STATE_AFTER_TRACE_PACKET_HEADER
,
790 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
, NULL
);
792 BT_COMP_LOGW("Cannot decode packet header field: "
793 "notit-addr=%p, trace-class-addr=%p, "
795 notit
, notit
->meta
.tc
, packet_header_fc
);
803 enum bt_msg_iter_status
read_packet_header_continue_state(
804 struct bt_msg_iter
*notit
)
806 return read_dscope_continue_state(notit
,
807 STATE_AFTER_TRACE_PACKET_HEADER
);
811 enum bt_msg_iter_status
set_current_stream_class(struct bt_msg_iter
*notit
)
813 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
814 struct ctf_stream_class
*new_stream_class
= NULL
;
816 if (notit
->cur_stream_class_id
== -1) {
818 * No current stream class ID field, therefore only one
821 if (notit
->meta
.tc
->stream_classes
->len
!= 1) {
822 BT_COMP_LOGW("Need exactly one stream class since there's "
823 "no stream class ID field: "
824 "notit-addr=%p", notit
);
825 status
= BT_MSG_ITER_STATUS_ERROR
;
829 new_stream_class
= notit
->meta
.tc
->stream_classes
->pdata
[0];
830 notit
->cur_stream_class_id
= new_stream_class
->id
;
833 new_stream_class
= ctf_trace_class_borrow_stream_class_by_id(
834 notit
->meta
.tc
, notit
->cur_stream_class_id
);
835 if (!new_stream_class
) {
836 BT_COMP_LOGW("No stream class with ID of stream class ID to use in trace class: "
837 "notit-addr=%p, stream-class-id=%" PRIu64
", "
838 "trace-class-addr=%p",
839 notit
, notit
->cur_stream_class_id
, notit
->meta
.tc
);
840 status
= BT_MSG_ITER_STATUS_ERROR
;
844 if (notit
->meta
.sc
) {
845 if (new_stream_class
!= notit
->meta
.sc
) {
846 BT_COMP_LOGW("Two packets refer to two different stream classes within the same packet sequence: "
847 "notit-addr=%p, prev-stream-class-addr=%p, "
848 "prev-stream-class-id=%" PRId64
", "
849 "next-stream-class-addr=%p, "
850 "next-stream-class-id=%" PRId64
", "
852 notit
, notit
->meta
.sc
,
855 new_stream_class
->id
,
857 status
= BT_MSG_ITER_STATUS_ERROR
;
861 notit
->meta
.sc
= new_stream_class
;
864 BT_COMP_LOGD("Set current stream class: "
865 "notit-addr=%p, stream-class-addr=%p, "
866 "stream-class-id=%" PRId64
,
867 notit
, notit
->meta
.sc
, notit
->meta
.sc
->id
);
874 enum bt_msg_iter_status
set_current_stream(struct bt_msg_iter
*notit
)
876 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
877 bt_stream
*stream
= NULL
;
879 BT_COMP_LOGD("Calling user function (get stream): notit-addr=%p, "
880 "stream-class-addr=%p, stream-class-id=%" PRId64
,
881 notit
, notit
->meta
.sc
,
883 stream
= notit
->medium
.medops
.borrow_stream(
884 notit
->meta
.sc
->ir_sc
, notit
->cur_data_stream_id
,
886 bt_stream_get_ref(stream
);
887 BT_COMP_LOGD("User function returned: stream-addr=%p", stream
);
889 BT_COMP_LOGW_STR("User function failed to return a stream object "
890 "for the given stream class.");
891 status
= BT_MSG_ITER_STATUS_ERROR
;
895 if (notit
->stream
&& stream
!= notit
->stream
) {
896 BT_COMP_LOGW("User function returned a different stream than the "
897 "previous one for the same sequence of packets.");
898 status
= BT_MSG_ITER_STATUS_ERROR
;
902 BT_STREAM_MOVE_REF(notit
->stream
, stream
);
905 bt_stream_put_ref(stream
);
910 enum bt_msg_iter_status
set_current_packet(struct bt_msg_iter
*notit
)
912 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
913 bt_packet
*packet
= NULL
;
915 BT_COMP_LOGD("Creating packet for packet message: "
916 "notit-addr=%p", notit
);
917 BT_COMP_LOGD("Creating packet from stream: "
918 "notit-addr=%p, stream-addr=%p, "
919 "stream-class-addr=%p, "
920 "stream-class-id=%" PRId64
,
921 notit
, notit
->stream
, notit
->meta
.sc
,
925 BT_ASSERT(notit
->stream
);
926 packet
= bt_packet_create(notit
->stream
);
928 BT_COMP_LOGE("Cannot create packet from stream: "
929 "notit-addr=%p, stream-addr=%p, "
930 "stream-class-addr=%p, "
931 "stream-class-id=%" PRId64
,
932 notit
, notit
->stream
, notit
->meta
.sc
,
940 BT_PACKET_PUT_REF_AND_RESET(packet
);
941 status
= BT_MSG_ITER_STATUS_ERROR
;
944 BT_PACKET_MOVE_REF(notit
->packet
, packet
);
949 enum bt_msg_iter_status
after_packet_header_state(
950 struct bt_msg_iter
*notit
)
952 enum bt_msg_iter_status status
;
954 status
= set_current_stream_class(notit
);
955 if (status
!= BT_MSG_ITER_STATUS_OK
) {
959 notit
->state
= STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
;
966 enum bt_msg_iter_status
read_packet_context_begin_state(
967 struct bt_msg_iter
*notit
)
969 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
970 struct ctf_field_class
*packet_context_fc
;
972 BT_ASSERT(notit
->meta
.sc
);
973 packet_context_fc
= notit
->meta
.sc
->packet_context_fc
;
974 if (!packet_context_fc
) {
975 BT_COMP_LOGD("No packet packet context field class in stream class: continuing: "
976 "notit-addr=%p, stream-class-addr=%p, "
977 "stream-class-id=%" PRId64
,
978 notit
, notit
->meta
.sc
,
980 notit
->state
= STATE_AFTER_STREAM_PACKET_CONTEXT
;
984 BT_ASSERT(!notit
->packet_context_field
);
986 if (packet_context_fc
->in_ir
&& !notit
->dry_run
) {
988 * Create free packet context field from stream class.
989 * This field is going to be moved to the packet once we
990 * create it. We cannot create the packet now because a
991 * packet is created from a stream, and this API must be
992 * able to return the packet context properties without
994 * (bt_msg_iter_get_packet_properties()).
996 notit
->packet_context_field
=
997 bt_packet_context_field_create(
998 notit
->meta
.sc
->ir_sc
);
999 if (!notit
->packet_context_field
) {
1000 BT_COMP_LOGE_STR("Cannot create packet context field wrapper from stream class.");
1001 status
= BT_MSG_ITER_STATUS_ERROR
;
1005 notit
->dscopes
.stream_packet_context
=
1006 bt_packet_context_field_borrow_field(
1007 notit
->packet_context_field
);
1008 BT_ASSERT(notit
->dscopes
.stream_packet_context
);
1011 BT_COMP_LOGD("Decoding packet context field: "
1012 "notit-addr=%p, stream-class-addr=%p, "
1013 "stream-class-id=%" PRId64
", fc-addr=%p",
1014 notit
, notit
->meta
.sc
,
1015 notit
->meta
.sc
->id
, packet_context_fc
);
1016 status
= read_dscope_begin_state(notit
, packet_context_fc
,
1017 STATE_AFTER_STREAM_PACKET_CONTEXT
,
1018 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
1019 notit
->dscopes
.stream_packet_context
);
1021 BT_COMP_LOGW("Cannot decode packet context field: "
1022 "notit-addr=%p, stream-class-addr=%p, "
1023 "stream-class-id=%" PRId64
", fc-addr=%p",
1024 notit
, notit
->meta
.sc
,
1034 enum bt_msg_iter_status
read_packet_context_continue_state(
1035 struct bt_msg_iter
*notit
)
1037 return read_dscope_continue_state(notit
,
1038 STATE_AFTER_STREAM_PACKET_CONTEXT
);
1042 enum bt_msg_iter_status
set_current_packet_content_sizes(
1043 struct bt_msg_iter
*notit
)
1045 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1047 if (notit
->cur_exp_packet_total_size
== -1) {
1048 if (notit
->cur_exp_packet_content_size
!= -1) {
1049 notit
->cur_exp_packet_total_size
=
1050 notit
->cur_exp_packet_content_size
;
1053 if (notit
->cur_exp_packet_content_size
== -1) {
1054 notit
->cur_exp_packet_content_size
=
1055 notit
->cur_exp_packet_total_size
;
1059 BT_ASSERT((notit
->cur_exp_packet_total_size
>= 0 &&
1060 notit
->cur_exp_packet_content_size
>= 0) ||
1061 (notit
->cur_exp_packet_total_size
< 0 &&
1062 notit
->cur_exp_packet_content_size
< 0));
1064 if (notit
->cur_exp_packet_content_size
>
1065 notit
->cur_exp_packet_total_size
) {
1066 BT_COMP_LOGW("Invalid packet or content size: "
1067 "content size is greater than packet size: "
1068 "notit-addr=%p, packet-context-field-addr=%p, "
1069 "packet-size=%" PRId64
", content-size=%" PRId64
,
1070 notit
, notit
->dscopes
.stream_packet_context
,
1071 notit
->cur_exp_packet_total_size
,
1072 notit
->cur_exp_packet_content_size
);
1073 status
= BT_MSG_ITER_STATUS_ERROR
;
1077 BT_COMP_LOGD("Set current packet and content sizes: "
1078 "notit-addr=%p, packet-size=%" PRIu64
", content-size=%" PRIu64
,
1079 notit
, notit
->cur_exp_packet_total_size
,
1080 notit
->cur_exp_packet_content_size
);
1087 enum bt_msg_iter_status
after_packet_context_state(struct bt_msg_iter
*notit
)
1089 enum bt_msg_iter_status status
;
1091 status
= set_current_packet_content_sizes(notit
);
1092 if (status
!= BT_MSG_ITER_STATUS_OK
) {
1096 if (notit
->stream
) {
1098 * Stream exists, which means we already emitted at
1099 * least one packet beginning message, so the initial
1100 * stream beginning message was also emitted.
1102 notit
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS
;
1104 notit
->state
= STATE_CHECK_EMIT_MSG_STREAM_BEGINNING
;
1112 enum bt_msg_iter_status
read_event_header_begin_state(struct bt_msg_iter
*notit
)
1114 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1115 struct ctf_field_class
*event_header_fc
= NULL
;
1117 /* Reset the position of the last event header */
1118 notit
->buf
.last_eh_at
= notit
->buf
.at
;
1119 notit
->cur_event_class_id
= -1;
1121 /* Check if we have some content left */
1122 if (notit
->cur_exp_packet_content_size
>= 0) {
1123 if (G_UNLIKELY(packet_at(notit
) ==
1124 notit
->cur_exp_packet_content_size
)) {
1125 /* No more events! */
1126 BT_COMP_LOGD("Reached end of packet: notit-addr=%p, "
1127 "cur=%zu", notit
, packet_at(notit
));
1128 notit
->state
= STATE_EMIT_MSG_PACKET_END_MULTI
;
1130 } else if (G_UNLIKELY(packet_at(notit
) >
1131 notit
->cur_exp_packet_content_size
)) {
1132 /* That's not supposed to happen */
1133 BT_COMP_LOGD("Before decoding event header field: cursor is passed the packet's content: "
1134 "notit-addr=%p, content-size=%" PRId64
", "
1136 notit
->cur_exp_packet_content_size
,
1138 status
= BT_MSG_ITER_STATUS_ERROR
;
1143 * "Infinite" content: we're done when the medium has
1144 * nothing else for us.
1146 status
= buf_ensure_available_bits(notit
);
1148 case BT_MSG_ITER_STATUS_OK
:
1150 case BT_MSG_ITER_STATUS_EOF
:
1151 status
= BT_MSG_ITER_STATUS_OK
;
1152 notit
->state
= STATE_EMIT_MSG_PACKET_END_SINGLE
;
1159 release_event_dscopes(notit
);
1160 BT_ASSERT(notit
->meta
.sc
);
1161 event_header_fc
= notit
->meta
.sc
->event_header_fc
;
1162 if (!event_header_fc
) {
1163 notit
->state
= STATE_AFTER_EVENT_HEADER
;
1167 BT_COMP_LOGD("Decoding event header field: "
1168 "notit-addr=%p, stream-class-addr=%p, "
1169 "stream-class-id=%" PRId64
", "
1171 notit
, notit
->meta
.sc
,
1174 status
= read_dscope_begin_state(notit
, event_header_fc
,
1175 STATE_AFTER_EVENT_HEADER
,
1176 STATE_DSCOPE_EVENT_HEADER_CONTINUE
, NULL
);
1178 BT_COMP_LOGW("Cannot decode event header field: "
1179 "notit-addr=%p, stream-class-addr=%p, "
1180 "stream-class-id=%" PRId64
", fc-addr=%p",
1181 notit
, notit
->meta
.sc
,
1191 enum bt_msg_iter_status
read_event_header_continue_state(
1192 struct bt_msg_iter
*notit
)
1194 return read_dscope_continue_state(notit
,
1195 STATE_AFTER_EVENT_HEADER
);
1199 enum bt_msg_iter_status
set_current_event_class(struct bt_msg_iter
*notit
)
1201 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1203 struct ctf_event_class
*new_event_class
= NULL
;
1205 if (notit
->cur_event_class_id
== -1) {
1207 * No current event class ID field, therefore only one
1210 if (notit
->meta
.sc
->event_classes
->len
!= 1) {
1211 BT_COMP_LOGW("Need exactly one event class since there's "
1212 "no event class ID field: "
1213 "notit-addr=%p", notit
);
1214 status
= BT_MSG_ITER_STATUS_ERROR
;
1218 new_event_class
= notit
->meta
.sc
->event_classes
->pdata
[0];
1219 notit
->cur_event_class_id
= new_event_class
->id
;
1222 new_event_class
= ctf_stream_class_borrow_event_class_by_id(
1223 notit
->meta
.sc
, notit
->cur_event_class_id
);
1224 if (!new_event_class
) {
1225 BT_COMP_LOGW("No event class with ID of event class ID to use in stream class: "
1226 "notit-addr=%p, stream-class-id=%" PRIu64
", "
1227 "event-class-id=%" PRIu64
", "
1228 "trace-class-addr=%p",
1229 notit
, notit
->meta
.sc
->id
, notit
->cur_event_class_id
,
1231 status
= BT_MSG_ITER_STATUS_ERROR
;
1235 notit
->meta
.ec
= new_event_class
;
1236 BT_COMP_LOGD("Set current event class: "
1237 "notit-addr=%p, event-class-addr=%p, "
1238 "event-class-id=%" PRId64
", "
1239 "event-class-name=\"%s\"",
1240 notit
, notit
->meta
.ec
, notit
->meta
.ec
->id
,
1241 notit
->meta
.ec
->name
->str
);
1248 enum bt_msg_iter_status
set_current_event_message(
1249 struct bt_msg_iter
*notit
)
1251 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1252 bt_message
*msg
= NULL
;
1254 BT_ASSERT(notit
->meta
.ec
);
1255 BT_ASSERT(notit
->packet
);
1256 BT_COMP_LOGD("Creating event message from event class and packet: "
1257 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", packet-addr=%p",
1258 notit
, notit
->meta
.ec
,
1259 notit
->meta
.ec
->name
->str
,
1261 BT_ASSERT(notit
->msg_iter
);
1262 BT_ASSERT(notit
->meta
.sc
);
1264 if (bt_stream_class_borrow_default_clock_class(notit
->meta
.sc
->ir_sc
)) {
1265 msg
= bt_message_event_create_with_packet_and_default_clock_snapshot(
1266 notit
->msg_iter
, notit
->meta
.ec
->ir_ec
,
1267 notit
->packet
, notit
->default_clock_snapshot
);
1269 msg
= bt_message_event_create_with_packet(notit
->msg_iter
,
1270 notit
->meta
.ec
->ir_ec
, notit
->packet
);
1274 BT_COMP_LOGE("Cannot create event message: "
1275 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", "
1277 notit
, notit
->meta
.ec
,
1278 notit
->meta
.ec
->name
->str
,
1286 BT_MESSAGE_PUT_REF_AND_RESET(msg
);
1287 status
= BT_MSG_ITER_STATUS_ERROR
;
1290 BT_MESSAGE_MOVE_REF(notit
->event_msg
, msg
);
1295 enum bt_msg_iter_status
after_event_header_state(
1296 struct bt_msg_iter
*notit
)
1298 enum bt_msg_iter_status status
;
1300 status
= set_current_event_class(notit
);
1301 if (status
!= BT_MSG_ITER_STATUS_OK
) {
1305 if (G_UNLIKELY(notit
->dry_run
)) {
1309 status
= set_current_event_message(notit
);
1310 if (status
!= BT_MSG_ITER_STATUS_OK
) {
1314 notit
->event
= bt_message_event_borrow_event(
1316 BT_ASSERT(notit
->event
);
1319 notit
->state
= STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
;
1326 enum bt_msg_iter_status
read_event_common_context_begin_state(
1327 struct bt_msg_iter
*notit
)
1329 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1330 struct ctf_field_class
*event_common_context_fc
;
1332 event_common_context_fc
= notit
->meta
.sc
->event_common_context_fc
;
1333 if (!event_common_context_fc
) {
1334 notit
->state
= STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
;
1338 if (event_common_context_fc
->in_ir
&& !notit
->dry_run
) {
1339 BT_ASSERT(!notit
->dscopes
.event_common_context
);
1340 notit
->dscopes
.event_common_context
=
1341 bt_event_borrow_common_context_field(
1343 BT_ASSERT(notit
->dscopes
.event_common_context
);
1346 BT_COMP_LOGT("Decoding event common context field: "
1347 "notit-addr=%p, stream-class-addr=%p, "
1348 "stream-class-id=%" PRId64
", "
1350 notit
, notit
->meta
.sc
,
1352 event_common_context_fc
);
1353 status
= read_dscope_begin_state(notit
, event_common_context_fc
,
1354 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
,
1355 STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
,
1356 notit
->dscopes
.event_common_context
);
1358 BT_COMP_LOGW("Cannot decode event common context field: "
1359 "notit-addr=%p, stream-class-addr=%p, "
1360 "stream-class-id=%" PRId64
", fc-addr=%p",
1361 notit
, notit
->meta
.sc
,
1363 event_common_context_fc
);
1371 enum bt_msg_iter_status
read_event_common_context_continue_state(
1372 struct bt_msg_iter
*notit
)
1374 return read_dscope_continue_state(notit
,
1375 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
);
1379 enum bt_msg_iter_status
read_event_spec_context_begin_state(
1380 struct bt_msg_iter
*notit
)
1382 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1383 struct ctf_field_class
*event_spec_context_fc
;
1385 event_spec_context_fc
= notit
->meta
.ec
->spec_context_fc
;
1386 if (!event_spec_context_fc
) {
1387 notit
->state
= STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
;
1391 if (event_spec_context_fc
->in_ir
&& !notit
->dry_run
) {
1392 BT_ASSERT(!notit
->dscopes
.event_spec_context
);
1393 notit
->dscopes
.event_spec_context
=
1394 bt_event_borrow_specific_context_field(
1396 BT_ASSERT(notit
->dscopes
.event_spec_context
);
1399 BT_COMP_LOGT("Decoding event specific context field: "
1400 "notit-addr=%p, event-class-addr=%p, "
1401 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1403 notit
, notit
->meta
.ec
,
1404 notit
->meta
.ec
->name
->str
,
1406 event_spec_context_fc
);
1407 status
= read_dscope_begin_state(notit
, event_spec_context_fc
,
1408 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
1409 STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
,
1410 notit
->dscopes
.event_spec_context
);
1412 BT_COMP_LOGW("Cannot decode event specific context field: "
1413 "notit-addr=%p, event-class-addr=%p, "
1414 "event-class-name=\"%s\", "
1415 "event-class-id=%" PRId64
", fc-addr=%p",
1416 notit
, notit
->meta
.ec
,
1417 notit
->meta
.ec
->name
->str
,
1419 event_spec_context_fc
);
1427 enum bt_msg_iter_status
read_event_spec_context_continue_state(
1428 struct bt_msg_iter
*notit
)
1430 return read_dscope_continue_state(notit
,
1431 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
);
1435 enum bt_msg_iter_status
read_event_payload_begin_state(
1436 struct bt_msg_iter
*notit
)
1438 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1439 struct ctf_field_class
*event_payload_fc
;
1441 event_payload_fc
= notit
->meta
.ec
->payload_fc
;
1442 if (!event_payload_fc
) {
1443 notit
->state
= STATE_EMIT_MSG_EVENT
;
1447 if (event_payload_fc
->in_ir
&& !notit
->dry_run
) {
1448 BT_ASSERT(!notit
->dscopes
.event_payload
);
1449 notit
->dscopes
.event_payload
=
1450 bt_event_borrow_payload_field(
1452 BT_ASSERT(notit
->dscopes
.event_payload
);
1455 BT_COMP_LOGT("Decoding event payload field: "
1456 "notit-addr=%p, event-class-addr=%p, "
1457 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1459 notit
, notit
->meta
.ec
,
1460 notit
->meta
.ec
->name
->str
,
1463 status
= read_dscope_begin_state(notit
, event_payload_fc
,
1464 STATE_EMIT_MSG_EVENT
,
1465 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
1466 notit
->dscopes
.event_payload
);
1468 BT_COMP_LOGW("Cannot decode event payload field: "
1469 "notit-addr=%p, event-class-addr=%p, "
1470 "event-class-name=\"%s\", "
1471 "event-class-id=%" PRId64
", fc-addr=%p",
1472 notit
, notit
->meta
.ec
,
1473 notit
->meta
.ec
->name
->str
,
1483 enum bt_msg_iter_status
read_event_payload_continue_state(
1484 struct bt_msg_iter
*notit
)
1486 return read_dscope_continue_state(notit
, STATE_EMIT_MSG_EVENT
);
1490 enum bt_msg_iter_status
skip_packet_padding_state(struct bt_msg_iter
*notit
)
1492 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1493 size_t bits_to_skip
;
1494 const enum state next_state
= STATE_SWITCH_PACKET
;
1496 BT_ASSERT(notit
->cur_exp_packet_total_size
> 0);
1497 bits_to_skip
= notit
->cur_exp_packet_total_size
- packet_at(notit
);
1498 if (bits_to_skip
== 0) {
1499 notit
->state
= next_state
;
1502 size_t bits_to_consume
;
1504 BT_COMP_LOGD("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu",
1505 bits_to_skip
, notit
, bits_to_skip
);
1506 status
= buf_ensure_available_bits(notit
);
1507 if (status
!= BT_MSG_ITER_STATUS_OK
) {
1511 bits_to_consume
= MIN(buf_available_bits(notit
), bits_to_skip
);
1512 BT_COMP_LOGD("Skipping %zu bits of padding: notit-addr=%p, size=%zu",
1513 bits_to_consume
, notit
, bits_to_consume
);
1514 buf_consume_bits(notit
, bits_to_consume
);
1515 bits_to_skip
= notit
->cur_exp_packet_total_size
-
1517 if (bits_to_skip
== 0) {
1518 notit
->state
= next_state
;
1528 enum bt_msg_iter_status
check_emit_msg_stream_beginning_state(
1529 struct bt_msg_iter
*notit
)
1531 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1533 if (notit
->set_stream
) {
1534 status
= set_current_stream(notit
);
1535 if (status
!= BT_MSG_ITER_STATUS_OK
) {
1540 if (notit
->emit_stream_begin_msg
) {
1541 notit
->state
= STATE_EMIT_MSG_STREAM_BEGINNING
;
1543 /* Stream's first packet */
1544 notit
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS
;
1552 enum bt_msg_iter_status
check_emit_msg_discarded_events(
1553 struct bt_msg_iter
*notit
)
1555 notit
->state
= STATE_EMIT_MSG_DISCARDED_EVENTS
;
1557 if (!notit
->meta
.sc
->has_discarded_events
) {
1558 notit
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
;
1562 if (notit
->prev_packet_snapshots
.discarded_events
== UINT64_C(-1)) {
1563 if (notit
->snapshots
.discarded_events
== 0 ||
1564 notit
->snapshots
.discarded_events
== UINT64_C(-1)) {
1566 * Stream's first packet with no discarded
1567 * events or no information about discarded
1568 * events: do not emit.
1570 notit
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
;
1574 * If the previous packet has a value for this counter,
1575 * then this counter is defined for the whole stream.
1577 BT_ASSERT(notit
->snapshots
.discarded_events
!= UINT64_C(-1));
1579 if (notit
->snapshots
.discarded_events
-
1580 notit
->prev_packet_snapshots
.discarded_events
== 0) {
1582 * No discarded events since previous packet: do
1585 notit
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
;
1590 return BT_MSG_ITER_STATUS_OK
;
1594 enum bt_msg_iter_status
check_emit_msg_discarded_packets(
1595 struct bt_msg_iter
*notit
)
1597 notit
->state
= STATE_EMIT_MSG_DISCARDED_PACKETS
;
1599 if (!notit
->meta
.sc
->has_discarded_packets
) {
1600 notit
->state
= STATE_EMIT_MSG_PACKET_BEGINNING
;
1604 if (notit
->prev_packet_snapshots
.packets
== UINT64_C(-1)) {
1606 * Stream's first packet or no information about
1607 * discarded packets: do not emit. In other words, if
1608 * this is the first packet and its sequence number is
1609 * not 0, do not consider that packets were previously
1610 * lost: we might be reading a partial stream (LTTng
1611 * snapshot for example).
1613 notit
->state
= STATE_EMIT_MSG_PACKET_BEGINNING
;
1616 * If the previous packet has a value for this counter,
1617 * then this counter is defined for the whole stream.
1619 BT_ASSERT(notit
->snapshots
.packets
!= UINT64_C(-1));
1621 if (notit
->snapshots
.packets
-
1622 notit
->prev_packet_snapshots
.packets
<= 1) {
1624 * No discarded packets since previous packet:
1627 notit
->state
= STATE_EMIT_MSG_PACKET_BEGINNING
;
1632 return BT_MSG_ITER_STATUS_OK
;
1636 enum bt_msg_iter_status
check_emit_msg_stream_end(
1637 struct bt_msg_iter
*notit
)
1639 if (notit
->emit_stream_end_msg
) {
1640 notit
->state
= STATE_EMIT_MSG_STREAM_END
;
1642 notit
->state
= STATE_DONE
;
1645 return BT_MSG_ITER_STATUS_OK
;
1649 enum bt_msg_iter_status
handle_state(struct bt_msg_iter
*notit
)
1651 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1652 const enum state state
= notit
->state
;
1654 BT_COMP_LOGT("Handling state: notit-addr=%p, state=%s",
1655 notit
, state_string(state
));
1657 // TODO: optimalize!
1660 notit
->state
= STATE_SWITCH_PACKET
;
1662 case STATE_SWITCH_PACKET
:
1663 status
= switch_packet_state(notit
);
1665 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
1666 status
= read_packet_header_begin_state(notit
);
1668 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
1669 status
= read_packet_header_continue_state(notit
);
1671 case STATE_AFTER_TRACE_PACKET_HEADER
:
1672 status
= after_packet_header_state(notit
);
1674 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
1675 status
= read_packet_context_begin_state(notit
);
1677 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
1678 status
= read_packet_context_continue_state(notit
);
1680 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
1681 status
= after_packet_context_state(notit
);
1683 case STATE_CHECK_EMIT_MSG_STREAM_BEGINNING
:
1684 status
= check_emit_msg_stream_beginning_state(notit
);
1686 case STATE_EMIT_MSG_STREAM_BEGINNING
:
1687 notit
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS
;
1689 case STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS
:
1690 status
= check_emit_msg_discarded_events(notit
);
1692 case STATE_EMIT_MSG_DISCARDED_EVENTS
:
1693 notit
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
;
1695 case STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
:
1696 status
= check_emit_msg_discarded_packets(notit
);
1698 case STATE_EMIT_MSG_DISCARDED_PACKETS
:
1699 notit
->state
= STATE_EMIT_MSG_PACKET_BEGINNING
;
1701 case STATE_EMIT_MSG_PACKET_BEGINNING
:
1702 notit
->state
= STATE_DSCOPE_EVENT_HEADER_BEGIN
;
1704 case STATE_DSCOPE_EVENT_HEADER_BEGIN
:
1705 status
= read_event_header_begin_state(notit
);
1707 case STATE_DSCOPE_EVENT_HEADER_CONTINUE
:
1708 status
= read_event_header_continue_state(notit
);
1710 case STATE_AFTER_EVENT_HEADER
:
1711 status
= after_event_header_state(notit
);
1713 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
:
1714 status
= read_event_common_context_begin_state(notit
);
1716 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
:
1717 status
= read_event_common_context_continue_state(notit
);
1719 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
:
1720 status
= read_event_spec_context_begin_state(notit
);
1722 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
:
1723 status
= read_event_spec_context_continue_state(notit
);
1725 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
1726 status
= read_event_payload_begin_state(notit
);
1728 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
1729 status
= read_event_payload_continue_state(notit
);
1731 case STATE_EMIT_MSG_EVENT
:
1732 notit
->state
= STATE_DSCOPE_EVENT_HEADER_BEGIN
;
1734 case STATE_SKIP_PACKET_PADDING
:
1735 status
= skip_packet_padding_state(notit
);
1737 case STATE_EMIT_MSG_PACKET_END_MULTI
:
1738 notit
->state
= STATE_SKIP_PACKET_PADDING
;
1740 case STATE_EMIT_MSG_PACKET_END_SINGLE
:
1741 notit
->state
= STATE_CHECK_EMIT_MSG_STREAM_END
;
1743 case STATE_CHECK_EMIT_MSG_STREAM_END
:
1744 status
= check_emit_msg_stream_end(notit
);
1746 case STATE_EMIT_MSG_STREAM_END
:
1747 notit
->state
= STATE_DONE
;
1752 BT_COMP_LOGF("Unknown CTF plugin message iterator state: "
1753 "notit-addr=%p, state=%d", notit
, notit
->state
);
1757 BT_COMP_LOGT("Handled state: notit-addr=%p, status=%s, "
1758 "prev-state=%s, cur-state=%s",
1759 notit
, bt_msg_iter_status_string(status
),
1760 state_string(state
), state_string(notit
->state
));
1765 void bt_msg_iter_reset_for_next_stream_file(struct bt_msg_iter
*notit
)
1768 BT_COMP_LOGD("Resetting message iterator: addr=%p", notit
);
1769 stack_clear(notit
->stack
);
1770 notit
->meta
.sc
= NULL
;
1771 notit
->meta
.ec
= NULL
;
1772 BT_PACKET_PUT_REF_AND_RESET(notit
->packet
);
1773 BT_STREAM_PUT_REF_AND_RESET(notit
->stream
);
1774 BT_MESSAGE_PUT_REF_AND_RESET(notit
->event_msg
);
1775 release_all_dscopes(notit
);
1776 notit
->cur_dscope_field
= NULL
;
1778 if (notit
->packet_context_field
) {
1779 bt_packet_context_field_release(notit
->packet_context_field
);
1780 notit
->packet_context_field
= NULL
;
1783 notit
->buf
.addr
= NULL
;
1786 notit
->buf
.last_eh_at
= SIZE_MAX
;
1787 notit
->buf
.packet_offset
= 0;
1788 notit
->state
= STATE_INIT
;
1789 notit
->cur_exp_packet_content_size
= -1;
1790 notit
->cur_exp_packet_total_size
= -1;
1791 notit
->cur_packet_offset
= -1;
1792 notit
->cur_event_class_id
= -1;
1793 notit
->snapshots
.beginning_clock
= UINT64_C(-1);
1794 notit
->snapshots
.end_clock
= UINT64_C(-1);
1798 * Resets the internal state of a CTF message iterator.
1801 void bt_msg_iter_reset(struct bt_msg_iter
*notit
)
1803 bt_msg_iter_reset_for_next_stream_file(notit
);
1804 notit
->cur_stream_class_id
= -1;
1805 notit
->cur_data_stream_id
= -1;
1806 notit
->snapshots
.discarded_events
= UINT64_C(-1);
1807 notit
->snapshots
.packets
= UINT64_C(-1);
1808 notit
->prev_packet_snapshots
.discarded_events
= UINT64_C(-1);
1809 notit
->prev_packet_snapshots
.packets
= UINT64_C(-1);
1810 notit
->prev_packet_snapshots
.beginning_clock
= UINT64_C(-1);
1811 notit
->prev_packet_snapshots
.end_clock
= UINT64_C(-1);
1815 bt_field
*borrow_next_field(struct bt_msg_iter
*notit
)
1817 bt_field
*next_field
= NULL
;
1818 bt_field
*base_field
;
1819 const bt_field_class
*base_fc
;
1822 BT_ASSERT(!stack_empty(notit
->stack
));
1823 index
= stack_top(notit
->stack
)->index
;
1824 base_field
= stack_top(notit
->stack
)->base
;
1825 BT_ASSERT(base_field
);
1826 base_fc
= bt_field_borrow_class_const(base_field
);
1829 switch (bt_field_class_get_type(base_fc
)) {
1830 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
1833 bt_field_class_structure_get_member_count(
1834 bt_field_borrow_class_const(
1837 bt_field_structure_borrow_member_field_by_index(
1841 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
1842 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
1843 BT_ASSERT(index
< bt_field_array_get_length(base_field
));
1844 next_field
= bt_field_array_borrow_element_field_by_index(
1847 case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR
:
1848 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR
:
1849 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR
:
1850 BT_ASSERT(index
== 0);
1851 next_field
= bt_field_variant_borrow_selected_option_field(
1858 BT_ASSERT(next_field
);
1863 void update_default_clock(struct bt_msg_iter
*notit
, uint64_t new_val
,
1864 uint64_t new_val_size
)
1866 uint64_t new_val_mask
;
1867 uint64_t cur_value_masked
;
1869 BT_ASSERT(new_val_size
> 0);
1872 * Special case for a 64-bit new value, which is the limit
1873 * of a clock value as of this version: overwrite the
1874 * current value directly.
1876 if (new_val_size
== 64) {
1877 notit
->default_clock_snapshot
= new_val
;
1881 new_val_mask
= (1ULL << new_val_size
) - 1;
1882 cur_value_masked
= notit
->default_clock_snapshot
& new_val_mask
;
1884 if (new_val
< cur_value_masked
) {
1886 * It looks like a wrap happened on the number of bits
1887 * of the requested new value. Assume that the clock
1888 * value wrapped only one time.
1890 notit
->default_clock_snapshot
+= new_val_mask
+ 1;
1893 /* Clear the low bits of the current clock value. */
1894 notit
->default_clock_snapshot
&= ~new_val_mask
;
1896 /* Set the low bits of the current clock value. */
1897 notit
->default_clock_snapshot
|= new_val
;
1900 BT_COMP_LOGT("Updated default clock's value from integer field's value: "
1901 "value=%" PRIu64
, notit
->default_clock_snapshot
);
1905 enum bt_bfcr_status
bfcr_unsigned_int_cb(uint64_t value
,
1906 struct ctf_field_class
*fc
, void *data
)
1908 struct bt_msg_iter
*notit
= data
;
1909 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
1910 bt_field
*field
= NULL
;
1911 struct ctf_field_class_int
*int_fc
= (void *) fc
;
1913 BT_COMP_LOGT("Unsigned integer function called from BFCR: "
1914 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1915 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64
,
1916 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
1918 if (G_LIKELY(int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
)) {
1919 goto update_def_clock
;
1922 switch (int_fc
->meaning
) {
1923 case CTF_FIELD_CLASS_MEANING_EVENT_CLASS_ID
:
1924 notit
->cur_event_class_id
= value
;
1926 case CTF_FIELD_CLASS_MEANING_DATA_STREAM_ID
:
1927 notit
->cur_data_stream_id
= value
;
1929 case CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME
:
1930 notit
->snapshots
.beginning_clock
= value
;
1932 case CTF_FIELD_CLASS_MEANING_PACKET_END_TIME
:
1933 notit
->snapshots
.end_clock
= value
;
1935 case CTF_FIELD_CLASS_MEANING_STREAM_CLASS_ID
:
1936 notit
->cur_stream_class_id
= value
;
1938 case CTF_FIELD_CLASS_MEANING_MAGIC
:
1939 if (value
!= 0xc1fc1fc1) {
1940 BT_COMP_LOGW("Invalid CTF magic number: notit-addr=%p, "
1941 "magic=%" PRIx64
, notit
, value
);
1942 status
= BT_BFCR_STATUS_ERROR
;
1947 case CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT
:
1948 notit
->snapshots
.packets
= value
;
1950 case CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT
:
1951 notit
->snapshots
.discarded_events
= value
;
1953 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_TOTAL_SIZE
:
1954 notit
->cur_exp_packet_total_size
= value
;
1956 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_CONTENT_SIZE
:
1957 notit
->cur_exp_packet_content_size
= value
;
1964 if (G_UNLIKELY(int_fc
->mapped_clock_class
)) {
1965 update_default_clock(notit
, value
, int_fc
->base
.size
);
1968 if (G_UNLIKELY(int_fc
->storing_index
>= 0)) {
1969 g_array_index(notit
->stored_values
, uint64_t,
1970 (uint64_t) int_fc
->storing_index
) = value
;
1973 if (G_UNLIKELY(!fc
->in_ir
|| notit
->dry_run
)) {
1977 field
= borrow_next_field(notit
);
1979 BT_ASSERT(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
1980 BT_ASSERT(bt_field_get_class_type(field
) ==
1981 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
||
1982 bt_field_get_class_type(field
) ==
1983 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
);
1984 bt_field_integer_unsigned_set_value(field
, value
);
1985 stack_top(notit
->stack
)->index
++;
1992 enum bt_bfcr_status
bfcr_unsigned_int_char_cb(uint64_t value
,
1993 struct ctf_field_class
*fc
, void *data
)
1996 struct bt_msg_iter
*notit
= data
;
1997 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
1998 bt_field
*string_field
= NULL
;
1999 struct ctf_field_class_int
*int_fc
= (void *) fc
;
2000 char str
[2] = {'\0', '\0'};
2002 BT_COMP_LOGT("Unsigned integer character function called from BFCR: "
2003 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2004 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64
,
2005 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
2006 BT_ASSERT(int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
);
2007 BT_ASSERT(!int_fc
->mapped_clock_class
);
2008 BT_ASSERT(int_fc
->storing_index
< 0);
2010 if (G_UNLIKELY(!fc
->in_ir
|| notit
->dry_run
)) {
2014 if (notit
->done_filling_string
) {
2019 notit
->done_filling_string
= true;
2023 string_field
= stack_top(notit
->stack
)->base
;
2024 BT_ASSERT(bt_field_get_class_type(string_field
) ==
2025 BT_FIELD_CLASS_TYPE_STRING
);
2027 /* Append character */
2028 str
[0] = (char) value
;
2029 ret
= bt_field_string_append_with_length(string_field
, str
, 1);
2031 BT_COMP_LOGE("Cannot append character to string field's value: "
2032 "notit-addr=%p, field-addr=%p, ret=%d",
2033 notit
, string_field
, ret
);
2034 status
= BT_BFCR_STATUS_ERROR
;
2043 enum bt_bfcr_status
bfcr_signed_int_cb(int64_t value
,
2044 struct ctf_field_class
*fc
, void *data
)
2046 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
2047 bt_field
*field
= NULL
;
2048 struct bt_msg_iter
*notit
= data
;
2049 struct ctf_field_class_int
*int_fc
= (void *) fc
;
2051 BT_COMP_LOGT("Signed integer function called from BFCR: "
2052 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2053 "fc-type=%d, fc-in-ir=%d, value=%" PRId64
,
2054 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
2055 BT_ASSERT(int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
);
2057 if (G_UNLIKELY(int_fc
->storing_index
>= 0)) {
2058 g_array_index(notit
->stored_values
, uint64_t,
2059 (uint64_t) int_fc
->storing_index
) = (uint64_t) value
;
2062 if (G_UNLIKELY(!fc
->in_ir
|| notit
->dry_run
)) {
2066 field
= borrow_next_field(notit
);
2068 BT_ASSERT(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
2069 BT_ASSERT(bt_field_get_class_type(field
) ==
2070 BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
||
2071 bt_field_get_class_type(field
) ==
2072 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
);
2073 bt_field_integer_signed_set_value(field
, value
);
2074 stack_top(notit
->stack
)->index
++;
2081 enum bt_bfcr_status
bfcr_floating_point_cb(double value
,
2082 struct ctf_field_class
*fc
, void *data
)
2084 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
2085 bt_field
*field
= NULL
;
2086 struct bt_msg_iter
*notit
= data
;
2088 BT_COMP_LOGT("Floating point number function called from BFCR: "
2089 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2090 "fc-type=%d, fc-in-ir=%d, value=%f",
2091 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
2093 if (G_UNLIKELY(!fc
->in_ir
|| notit
->dry_run
)) {
2097 field
= borrow_next_field(notit
);
2099 BT_ASSERT(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
2100 BT_ASSERT(bt_field_get_class_type(field
) ==
2101 BT_FIELD_CLASS_TYPE_REAL
);
2102 bt_field_real_set_value(field
, value
);
2103 stack_top(notit
->stack
)->index
++;
2110 enum bt_bfcr_status
bfcr_string_begin_cb(
2111 struct ctf_field_class
*fc
, void *data
)
2113 bt_field
*field
= NULL
;
2114 struct bt_msg_iter
*notit
= data
;
2116 BT_COMP_LOGT("String (beginning) function called from BFCR: "
2117 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2118 "fc-type=%d, fc-in-ir=%d",
2119 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
2121 if (G_UNLIKELY(!fc
->in_ir
|| notit
->dry_run
)) {
2125 field
= borrow_next_field(notit
);
2127 BT_ASSERT(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
2128 BT_ASSERT(bt_field_get_class_type(field
) ==
2129 BT_FIELD_CLASS_TYPE_STRING
);
2130 bt_field_string_clear(field
);
2133 * Push on stack. Not a compound class per se, but we know that
2134 * only bfcr_string_cb() may be called between this call and a
2135 * subsequent call to bfcr_string_end_cb().
2137 stack_push(notit
->stack
, field
);
2140 return BT_BFCR_STATUS_OK
;
2144 enum bt_bfcr_status
bfcr_string_cb(const char *value
,
2145 size_t len
, struct ctf_field_class
*fc
, void *data
)
2147 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
2148 bt_field
*field
= NULL
;
2149 struct bt_msg_iter
*notit
= data
;
2152 BT_COMP_LOGT("String (substring) function called from BFCR: "
2153 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2154 "fc-type=%d, fc-in-ir=%d, string-length=%zu",
2155 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
,
2158 if (G_UNLIKELY(!fc
->in_ir
|| notit
->dry_run
)) {
2162 field
= stack_top(notit
->stack
)->base
;
2165 /* Append current substring */
2166 ret
= bt_field_string_append_with_length(field
, value
, len
);
2168 BT_COMP_LOGE("Cannot append substring to string field's value: "
2169 "notit-addr=%p, field-addr=%p, string-length=%zu, "
2170 "ret=%d", notit
, field
, len
, ret
);
2171 status
= BT_BFCR_STATUS_ERROR
;
2180 enum bt_bfcr_status
bfcr_string_end_cb(
2181 struct ctf_field_class
*fc
, void *data
)
2183 struct bt_msg_iter
*notit
= data
;
2185 BT_COMP_LOGT("String (end) function called from BFCR: "
2186 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2187 "fc-type=%d, fc-in-ir=%d",
2188 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
2190 if (G_UNLIKELY(!fc
->in_ir
|| notit
->dry_run
)) {
2194 /* Pop string field */
2195 stack_pop(notit
->stack
);
2197 /* Go to next field */
2198 stack_top(notit
->stack
)->index
++;
2201 return BT_BFCR_STATUS_OK
;
2204 enum bt_bfcr_status
bfcr_compound_begin_cb(
2205 struct ctf_field_class
*fc
, void *data
)
2207 struct bt_msg_iter
*notit
= data
;
2210 BT_COMP_LOGT("Compound (beginning) function called from BFCR: "
2211 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2212 "fc-type=%d, fc-in-ir=%d",
2213 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
2215 if (G_UNLIKELY(!fc
->in_ir
|| notit
->dry_run
)) {
2220 if (stack_empty(notit
->stack
)) {
2221 /* Root: already set by read_dscope_begin_state() */
2222 field
= notit
->cur_dscope_field
;
2224 field
= borrow_next_field(notit
);
2230 BT_ASSERT(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
2231 stack_push(notit
->stack
, field
);
2234 * Change BFCR "unsigned int" callback if it's a text
2237 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
||
2238 fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
2239 struct ctf_field_class_array_base
*array_fc
= (void *) fc
;
2241 if (array_fc
->is_text
) {
2242 BT_ASSERT(bt_field_get_class_type(field
) ==
2243 BT_FIELD_CLASS_TYPE_STRING
);
2244 notit
->done_filling_string
= false;
2245 bt_field_string_clear(field
);
2246 bt_bfcr_set_unsigned_int_cb(notit
->bfcr
,
2247 bfcr_unsigned_int_char_cb
);
2252 return BT_BFCR_STATUS_OK
;
2255 enum bt_bfcr_status
bfcr_compound_end_cb(
2256 struct ctf_field_class
*fc
, void *data
)
2258 struct bt_msg_iter
*notit
= data
;
2260 BT_COMP_LOGT("Compound (end) function called from BFCR: "
2261 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2262 "fc-type=%d, fc-in-ir=%d",
2263 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
2265 if (G_UNLIKELY(!fc
->in_ir
|| notit
->dry_run
)) {
2269 BT_ASSERT(!stack_empty(notit
->stack
));
2270 BT_ASSERT(bt_field_borrow_class_const(stack_top(notit
->stack
)->base
) ==
2274 * Reset BFCR "unsigned int" callback if it's a text
2277 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
||
2278 fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
2279 struct ctf_field_class_array_base
*array_fc
= (void *) fc
;
2281 if (array_fc
->is_text
) {
2282 BT_ASSERT(bt_field_get_class_type(
2283 stack_top(notit
->stack
)->base
) ==
2284 BT_FIELD_CLASS_TYPE_STRING
);
2285 bt_bfcr_set_unsigned_int_cb(notit
->bfcr
,
2286 bfcr_unsigned_int_cb
);
2291 stack_pop(notit
->stack
);
2293 /* If the stack is not empty, increment the base's index */
2294 if (!stack_empty(notit
->stack
)) {
2295 stack_top(notit
->stack
)->index
++;
2299 return BT_BFCR_STATUS_OK
;
2303 int64_t bfcr_get_sequence_length_cb(struct ctf_field_class
*fc
, void *data
)
2305 bt_field
*seq_field
;
2306 struct bt_msg_iter
*notit
= data
;
2307 struct ctf_field_class_sequence
*seq_fc
= (void *) fc
;
2308 int64_t length
= -1;
2311 length
= (uint64_t) g_array_index(notit
->stored_values
, uint64_t,
2312 seq_fc
->stored_length_index
);
2314 if (G_UNLIKELY(notit
->dry_run
)){
2318 seq_field
= stack_top(notit
->stack
)->base
;
2319 BT_ASSERT(seq_field
);
2322 * bfcr_get_sequence_length_cb() also gets called back for a
2323 * text sequence, but the destination field is a string field.
2324 * Only set the field's sequence length if the destination field
2325 * is a sequence field.
2327 if (!seq_fc
->base
.is_text
) {
2328 BT_ASSERT(bt_field_get_class_type(seq_field
) ==
2329 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
);
2330 ret
= bt_field_array_dynamic_set_length(seq_field
,
2333 BT_COMP_LOGE("Cannot set dynamic array field's length field: "
2334 "notit-addr=%p, field-addr=%p, "
2335 "length=%" PRIu64
, notit
, seq_field
, length
);
2344 struct ctf_field_class
*bfcr_borrow_variant_selected_field_class_cb(
2345 struct ctf_field_class
*fc
, void *data
)
2349 int64_t option_index
= -1;
2350 struct bt_msg_iter
*notit
= data
;
2351 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
2352 struct ctf_named_field_class
*selected_option
= NULL
;
2353 struct ctf_field_class
*ret_fc
= NULL
;
2359 /* Get variant's tag */
2360 tag
.u
= g_array_index(notit
->stored_values
, uint64_t,
2361 var_fc
->stored_tag_index
);
2364 * Check each range to find the selected option's index.
2366 if (var_fc
->tag_fc
->base
.is_signed
) {
2367 for (i
= 0; i
< var_fc
->ranges
->len
; i
++) {
2368 struct ctf_field_class_variant_range
*range
=
2369 ctf_field_class_variant_borrow_range_by_index(
2372 if (tag
.i
>= range
->range
.lower
.i
&&
2373 tag
.i
<= range
->range
.upper
.i
) {
2374 option_index
= (int64_t) range
->option_index
;
2379 for (i
= 0; i
< var_fc
->ranges
->len
; i
++) {
2380 struct ctf_field_class_variant_range
*range
=
2381 ctf_field_class_variant_borrow_range_by_index(
2384 if (tag
.u
>= range
->range
.lower
.u
&&
2385 tag
.u
<= range
->range
.upper
.u
) {
2386 option_index
= (int64_t) range
->option_index
;
2392 if (option_index
< 0) {
2393 BT_COMP_LOGW("Cannot find variant field class's option: "
2394 "notit-addr=%p, var-fc-addr=%p, u-tag=%" PRIu64
", "
2395 "i-tag=%" PRId64
, notit
, var_fc
, tag
.u
, tag
.i
);
2399 selected_option
= ctf_field_class_variant_borrow_option_by_index(
2400 var_fc
, (uint64_t) option_index
);
2402 if (selected_option
->fc
->in_ir
&& !notit
->dry_run
) {
2403 bt_field
*var_field
= stack_top(notit
->stack
)->base
;
2405 ret
= bt_field_variant_select_option_field_by_index(
2406 var_field
, option_index
);
2408 BT_COMP_LOGW("Cannot select variant field's option field: "
2409 "notit-addr=%p, var-field-addr=%p, "
2410 "opt-index=%" PRId64
, notit
, var_field
,
2416 ret_fc
= selected_option
->fc
;
2423 void create_msg_stream_beginning(struct bt_msg_iter
*notit
,
2424 bt_message
**message
)
2426 bt_message
*ret
= NULL
;
2428 BT_ASSERT(notit
->stream
);
2429 BT_ASSERT(notit
->msg_iter
);
2430 ret
= bt_message_stream_beginning_create(notit
->msg_iter
,
2433 BT_COMP_LOGE("Cannot create stream beginning message: "
2434 "notit-addr=%p, stream-addr=%p",
2435 notit
, notit
->stream
);
2443 void create_msg_stream_end(struct bt_msg_iter
*notit
, bt_message
**message
)
2447 if (!notit
->stream
) {
2448 BT_COMP_LOGE("Cannot create stream for stream message: "
2449 "notit-addr=%p", notit
);
2453 BT_ASSERT(notit
->msg_iter
);
2454 ret
= bt_message_stream_end_create(notit
->msg_iter
,
2457 BT_COMP_LOGE("Cannot create stream end message: "
2458 "notit-addr=%p, stream-addr=%p",
2459 notit
, notit
->stream
);
2467 void create_msg_packet_beginning(struct bt_msg_iter
*notit
,
2468 bt_message
**message
)
2471 enum bt_msg_iter_status status
;
2472 bt_message
*msg
= NULL
;
2473 const bt_stream_class
*sc
;
2475 status
= set_current_packet(notit
);
2476 if (status
!= BT_MSG_ITER_STATUS_OK
) {
2480 BT_ASSERT(notit
->packet
);
2481 sc
= notit
->meta
.sc
->ir_sc
;
2484 if (notit
->packet_context_field
) {
2485 ret
= bt_packet_move_context_field(
2486 notit
->packet
, notit
->packet_context_field
);
2491 notit
->packet_context_field
= NULL
;
2494 * At this point notit->dscopes.stream_packet_context
2495 * has the same value as the packet context field within
2498 BT_ASSERT(bt_packet_borrow_context_field(
2500 notit
->dscopes
.stream_packet_context
);
2503 BT_ASSERT(notit
->msg_iter
);
2505 if (notit
->meta
.sc
->packets_have_ts_begin
) {
2506 BT_ASSERT(notit
->snapshots
.beginning_clock
!= UINT64_C(-1));
2507 msg
= bt_message_packet_beginning_create_with_default_clock_snapshot(
2508 notit
->msg_iter
, notit
->packet
,
2509 notit
->snapshots
.beginning_clock
);
2511 msg
= bt_message_packet_beginning_create(notit
->msg_iter
,
2516 BT_COMP_LOGE("Cannot create packet beginning message: "
2517 "notit-addr=%p, packet-addr=%p",
2518 notit
, notit
->packet
);
2529 void create_msg_packet_end(struct bt_msg_iter
*notit
, bt_message
**message
)
2532 bool update_default_cs
= true;
2534 if (!notit
->packet
) {
2538 /* Check if may be affected by lttng-crash timestamp_end quirk. */
2539 if (G_UNLIKELY(notit
->meta
.tc
->quirks
.lttng_crash
)) {
2541 * Check if the `timestamp_begin` field is non-zero but
2542 * `timestamp_end` is zero. It means the trace is affected by
2543 * the lttng-crash packet `timestamp_end` quirk and must be
2544 * fixed up by omitting to update the default clock snapshot to
2545 * the `timestamp_end` as is typically done.
2547 if (notit
->snapshots
.beginning_clock
!= 0 &&
2548 notit
->snapshots
.end_clock
== 0) {
2549 update_default_cs
= false;
2554 * Check if may be affected by lttng event-after-packet `timestamp_end`
2557 if (notit
->meta
.tc
->quirks
.lttng_event_after_packet
) {
2559 * Check if `timestamp_end` is smaller then the current
2560 * default_clock_snapshot (which is set to the last event
2561 * decoded). It means the trace is affected by the lttng
2562 * `event-after-packet` packet `timestamp_end` quirk and must
2563 * be fixed up by omitting to update the default clock snapshot
2564 * to the `timestamp_end` as is typically done.
2566 if (notit
->snapshots
.end_clock
< notit
->default_clock_snapshot
) {
2567 update_default_cs
= false;
2571 /* Update default clock from packet's end time. */
2572 if (notit
->snapshots
.end_clock
!= UINT64_C(-1) && update_default_cs
) {
2573 notit
->default_clock_snapshot
= notit
->snapshots
.end_clock
;
2576 BT_ASSERT(notit
->msg_iter
);
2578 if (notit
->meta
.sc
->packets_have_ts_end
) {
2579 BT_ASSERT(notit
->snapshots
.end_clock
!= UINT64_C(-1));
2580 msg
= bt_message_packet_end_create_with_default_clock_snapshot(
2581 notit
->msg_iter
, notit
->packet
,
2582 notit
->default_clock_snapshot
);
2584 msg
= bt_message_packet_end_create(notit
->msg_iter
,
2589 BT_COMP_LOGE("Cannot create packet end message: "
2590 "notit-addr=%p, packet-addr=%p",
2591 notit
, notit
->packet
);
2596 BT_PACKET_PUT_REF_AND_RESET(notit
->packet
);
2601 void create_msg_discarded_events(struct bt_msg_iter
*notit
,
2602 bt_message
**message
)
2605 uint64_t beginning_raw_value
= UINT64_C(-1);
2606 uint64_t end_raw_value
= UINT64_C(-1);
2608 BT_ASSERT(notit
->msg_iter
);
2609 BT_ASSERT(notit
->stream
);
2610 BT_ASSERT(notit
->meta
.sc
->has_discarded_events
);
2612 if (notit
->meta
.sc
->discarded_events_have_default_cs
) {
2613 if (notit
->prev_packet_snapshots
.discarded_events
== UINT64_C(-1)) {
2615 * We discarded events, but before (and possibly
2616 * including) the current packet: use this packet's time
2617 * range, and do not have a specific count.
2619 beginning_raw_value
= notit
->snapshots
.beginning_clock
;
2620 end_raw_value
= notit
->snapshots
.end_clock
;
2622 beginning_raw_value
= notit
->prev_packet_snapshots
.end_clock
;
2623 end_raw_value
= notit
->snapshots
.end_clock
;
2626 BT_ASSERT(beginning_raw_value
!= UINT64_C(-1));
2627 BT_ASSERT(end_raw_value
!= UINT64_C(-1));
2628 msg
= bt_message_discarded_events_create_with_default_clock_snapshots(
2629 notit
->msg_iter
, notit
->stream
, beginning_raw_value
,
2632 msg
= bt_message_discarded_events_create(notit
->msg_iter
,
2637 BT_COMP_LOGE("Cannot create discarded events message: "
2638 "notit-addr=%p, stream-addr=%p",
2639 notit
, notit
->stream
);
2643 if (notit
->prev_packet_snapshots
.discarded_events
!= UINT64_C(-1)) {
2644 bt_message_discarded_events_set_count(msg
,
2645 notit
->snapshots
.discarded_events
-
2646 notit
->prev_packet_snapshots
.discarded_events
);
2653 void create_msg_discarded_packets(struct bt_msg_iter
*notit
,
2654 bt_message
**message
)
2658 BT_ASSERT(notit
->msg_iter
);
2659 BT_ASSERT(notit
->stream
);
2660 BT_ASSERT(notit
->meta
.sc
->has_discarded_packets
);
2661 BT_ASSERT(notit
->prev_packet_snapshots
.packets
!=
2664 if (notit
->meta
.sc
->discarded_packets_have_default_cs
) {
2665 BT_ASSERT(notit
->prev_packet_snapshots
.end_clock
!= UINT64_C(-1));
2666 BT_ASSERT(notit
->snapshots
.beginning_clock
!= UINT64_C(-1));
2667 msg
= bt_message_discarded_packets_create_with_default_clock_snapshots(
2668 notit
->msg_iter
, notit
->stream
,
2669 notit
->prev_packet_snapshots
.end_clock
,
2670 notit
->snapshots
.beginning_clock
);
2672 msg
= bt_message_discarded_packets_create(notit
->msg_iter
,
2677 BT_COMP_LOGE("Cannot create discarded packets message: "
2678 "notit-addr=%p, stream-addr=%p",
2679 notit
, notit
->stream
);
2683 bt_message_discarded_packets_set_count(msg
,
2684 notit
->snapshots
.packets
-
2685 notit
->prev_packet_snapshots
.packets
- 1);
2690 struct bt_msg_iter
*bt_msg_iter_create(struct ctf_trace_class
*tc
,
2691 size_t max_request_sz
,
2692 struct bt_msg_iter_medium_ops medops
, void *data
,
2693 bt_logging_level log_level
, bt_self_component
*self_comp
)
2695 struct bt_msg_iter
*notit
= NULL
;
2696 struct bt_bfcr_cbs cbs
= {
2698 .signed_int
= bfcr_signed_int_cb
,
2699 .unsigned_int
= bfcr_unsigned_int_cb
,
2700 .floating_point
= bfcr_floating_point_cb
,
2701 .string_begin
= bfcr_string_begin_cb
,
2702 .string
= bfcr_string_cb
,
2703 .string_end
= bfcr_string_end_cb
,
2704 .compound_begin
= bfcr_compound_begin_cb
,
2705 .compound_end
= bfcr_compound_end_cb
,
2708 .get_sequence_length
= bfcr_get_sequence_length_cb
,
2709 .borrow_variant_selected_field_class
= bfcr_borrow_variant_selected_field_class_cb
,
2714 BT_ASSERT(medops
.request_bytes
);
2715 BT_ASSERT(medops
.borrow_stream
);
2716 BT_COMP_LOG_CUR_LVL(BT_LOG_DEBUG
, log_level
, self_comp
,
2717 "Creating CTF plugin message iterator: "
2718 "trace-addr=%p, max-request-size=%zu, "
2719 "data=%p, log-level=%s", tc
, max_request_sz
, data
,
2720 bt_common_logging_level_string(log_level
));
2721 notit
= g_new0(struct bt_msg_iter
, 1);
2723 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_comp
,
2724 "Failed to allocate one CTF plugin message iterator.");
2727 notit
->self_comp
= self_comp
;
2728 notit
->log_level
= log_level
;
2729 notit
->meta
.tc
= tc
;
2730 notit
->medium
.medops
= medops
;
2731 notit
->medium
.max_request_sz
= max_request_sz
;
2732 notit
->medium
.data
= data
;
2733 notit
->stack
= stack_new(notit
);
2734 notit
->stored_values
= g_array_new(FALSE
, TRUE
, sizeof(uint64_t));
2735 g_array_set_size(notit
->stored_values
, tc
->stored_value_count
);
2737 if (!notit
->stack
) {
2738 BT_COMP_LOGE_STR("Failed to create field stack.");
2742 notit
->bfcr
= bt_bfcr_create(cbs
, notit
, log_level
, NULL
);
2744 BT_COMP_LOGE_STR("Failed to create binary class reader (BFCR).");
2748 bt_msg_iter_reset(notit
);
2749 BT_COMP_LOGD("Created CTF plugin message iterator: "
2750 "trace-addr=%p, max-request-size=%zu, "
2751 "data=%p, notit-addr=%p, log-level=%s",
2752 tc
, max_request_sz
, data
, notit
,
2753 bt_common_logging_level_string(log_level
));
2754 notit
->cur_packet_offset
= 0;
2760 bt_msg_iter_destroy(notit
);
2765 void bt_msg_iter_destroy(struct bt_msg_iter
*notit
)
2767 BT_PACKET_PUT_REF_AND_RESET(notit
->packet
);
2768 BT_STREAM_PUT_REF_AND_RESET(notit
->stream
);
2769 release_all_dscopes(notit
);
2771 BT_COMP_LOGD("Destroying CTF plugin message iterator: addr=%p", notit
);
2774 BT_COMP_LOGD_STR("Destroying field stack.");
2775 stack_destroy(notit
->stack
);
2779 BT_COMP_LOGD("Destroying BFCR: bfcr-addr=%p", notit
->bfcr
);
2780 bt_bfcr_destroy(notit
->bfcr
);
2783 if (notit
->stored_values
) {
2784 g_array_free(notit
->stored_values
, TRUE
);
2790 enum bt_msg_iter_status
bt_msg_iter_get_next_message(
2791 struct bt_msg_iter
*notit
,
2792 bt_self_message_iterator
*msg_iter
, bt_message
**message
)
2794 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
2798 notit
->msg_iter
= msg_iter
;
2799 notit
->set_stream
= true;
2800 BT_COMP_LOGD("Getting next message: notit-addr=%p", notit
);
2803 status
= handle_state(notit
);
2804 if (G_UNLIKELY(status
== BT_MSG_ITER_STATUS_AGAIN
)) {
2805 BT_COMP_LOGD_STR("Medium returned BT_MSG_ITER_STATUS_AGAIN.");
2807 } else if (G_UNLIKELY(status
!= BT_MSG_ITER_STATUS_OK
)) {
2808 BT_COMP_LOGW("Cannot handle state: notit-addr=%p, state=%s",
2809 notit
, state_string(notit
->state
));
2813 switch (notit
->state
) {
2814 case STATE_EMIT_MSG_EVENT
:
2815 BT_ASSERT(notit
->event_msg
);
2816 *message
= notit
->event_msg
;
2817 notit
->event_msg
= NULL
;
2819 case STATE_EMIT_MSG_DISCARDED_EVENTS
:
2820 /* create_msg_discared_events() logs errors */
2821 create_msg_discarded_events(notit
, message
);
2824 status
= BT_MSG_ITER_STATUS_ERROR
;
2828 case STATE_EMIT_MSG_DISCARDED_PACKETS
:
2829 /* create_msg_discared_packets() logs errors */
2830 create_msg_discarded_packets(notit
, message
);
2833 status
= BT_MSG_ITER_STATUS_ERROR
;
2837 case STATE_EMIT_MSG_PACKET_BEGINNING
:
2838 /* create_msg_packet_beginning() logs errors */
2839 create_msg_packet_beginning(notit
, message
);
2842 status
= BT_MSG_ITER_STATUS_ERROR
;
2846 case STATE_EMIT_MSG_PACKET_END_SINGLE
:
2847 case STATE_EMIT_MSG_PACKET_END_MULTI
:
2848 /* create_msg_packet_end() logs errors */
2849 create_msg_packet_end(notit
, message
);
2852 status
= BT_MSG_ITER_STATUS_ERROR
;
2856 case STATE_EMIT_MSG_STREAM_BEGINNING
:
2857 /* create_msg_stream_beginning() logs errors */
2858 create_msg_stream_beginning(notit
, message
);
2861 status
= BT_MSG_ITER_STATUS_ERROR
;
2865 case STATE_EMIT_MSG_STREAM_END
:
2866 /* create_msg_stream_end() logs errors */
2867 create_msg_stream_end(notit
, message
);
2870 status
= BT_MSG_ITER_STATUS_ERROR
;
2875 status
= BT_MSG_ITER_STATUS_EOF
;
2878 /* Non-emitting state: continue */
2888 enum bt_msg_iter_status
decode_until_state( struct bt_msg_iter
*notit
,
2889 enum state target_state_1
, enum state target_state_2
)
2891 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
2894 notit
->set_stream
= false;
2898 * Check if we reached the state at which we want to stop
2901 if (notit
->state
== target_state_1
||
2902 notit
->state
== target_state_2
) {
2906 status
= handle_state(notit
);
2907 if (G_UNLIKELY(status
== BT_MSG_ITER_STATUS_AGAIN
)) {
2908 BT_COMP_LOGD_STR("Medium returned BT_MSG_ITER_STATUS_AGAIN.");
2910 } else if (G_UNLIKELY(status
!= BT_MSG_ITER_STATUS_OK
)) {
2911 BT_COMP_LOGW("Cannot handle state: notit-addr=%p, state=%s",
2912 notit
, state_string(notit
->state
));
2916 switch (notit
->state
) {
2918 case STATE_SWITCH_PACKET
:
2919 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
2920 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
2921 case STATE_AFTER_TRACE_PACKET_HEADER
:
2922 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
2923 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
2924 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
2925 case STATE_CHECK_EMIT_MSG_STREAM_BEGINNING
:
2926 case STATE_EMIT_MSG_STREAM_BEGINNING
:
2927 case STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS
:
2928 case STATE_EMIT_MSG_DISCARDED_EVENTS
:
2929 case STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
:
2930 case STATE_EMIT_MSG_DISCARDED_PACKETS
:
2931 case STATE_EMIT_MSG_PACKET_BEGINNING
:
2932 case STATE_DSCOPE_EVENT_HEADER_BEGIN
:
2933 case STATE_DSCOPE_EVENT_HEADER_CONTINUE
:
2934 case STATE_AFTER_EVENT_HEADER
:
2935 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
:
2936 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
:
2937 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
:
2938 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
:
2939 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
2940 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
2941 case STATE_EMIT_MSG_EVENT
:
2942 case STATE_SKIP_PACKET_PADDING
:
2943 case STATE_EMIT_MSG_PACKET_END_MULTI
:
2944 case STATE_EMIT_MSG_PACKET_END_SINGLE
:
2945 case STATE_CHECK_EMIT_MSG_STREAM_END
:
2946 case STATE_EMIT_MSG_STREAM_END
:
2951 /* We should never get to the STATE_DONE state. */
2952 BT_COMP_LOGF("Unexpected state: notit-addr=%p, state=%s",
2953 notit
, state_string(notit
->state
));
2963 enum bt_msg_iter_status
read_packet_header_context_fields(
2964 struct bt_msg_iter
*notit
)
2967 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
2969 status
= decode_until_state(notit
, STATE_EMIT_MSG_PACKET_BEGINNING
, -1);
2970 if (status
!= BT_MSG_ITER_STATUS_OK
) {
2974 ret
= set_current_packet_content_sizes(notit
);
2976 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", (intmax_t) 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
clock_snapshot_at_msg_iter_state(
3032 struct bt_msg_iter
*notit
, enum state target_state_1
,
3033 enum state target_state_2
, uint64_t *clock_snapshot
)
3035 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
3038 BT_ASSERT(clock_snapshot
);
3039 status
= decode_until_state(notit
, target_state_1
, target_state_2
);
3040 if (status
!= BT_MSG_ITER_STATUS_OK
) {
3044 *clock_snapshot
= notit
->default_clock_snapshot
;
3050 enum bt_msg_iter_status
bt_msg_iter_curr_packet_first_event_clock_snapshot(
3051 struct bt_msg_iter
*notit
, uint64_t *first_clock_snapshot
)
3053 return clock_snapshot_at_msg_iter_state(notit
,
3054 STATE_AFTER_EVENT_HEADER
, -1, first_clock_snapshot
);
3058 enum bt_msg_iter_status
bt_msg_iter_curr_packet_last_event_clock_snapshot(
3059 struct bt_msg_iter
*notit
, uint64_t *last_clock_snapshot
)
3061 return clock_snapshot_at_msg_iter_state(notit
,
3062 STATE_EMIT_MSG_PACKET_END_SINGLE
,
3063 STATE_EMIT_MSG_PACKET_END_MULTI
, last_clock_snapshot
);
3067 enum bt_msg_iter_status
bt_msg_iter_get_packet_properties(
3068 struct bt_msg_iter
*notit
,
3069 struct bt_msg_iter_packet_properties
*props
)
3071 enum bt_msg_iter_status status
;
3075 status
= read_packet_header_context_fields(notit
);
3076 if (status
!= BT_MSG_ITER_STATUS_OK
) {
3080 props
->exp_packet_total_size
= notit
->cur_exp_packet_total_size
;
3081 props
->exp_packet_content_size
= notit
->cur_exp_packet_content_size
;
3082 props
->stream_class_id
= (uint64_t) notit
->cur_stream_class_id
;
3083 props
->data_stream_id
= notit
->cur_data_stream_id
;
3084 props
->snapshots
.discarded_events
= notit
->snapshots
.discarded_events
;
3085 props
->snapshots
.packets
= notit
->snapshots
.packets
;
3086 props
->snapshots
.beginning_clock
= notit
->snapshots
.beginning_clock
;
3087 props
->snapshots
.end_clock
= notit
->snapshots
.end_clock
;
3094 void bt_msg_iter_set_emit_stream_beginning_message(struct bt_msg_iter
*notit
,
3097 notit
->emit_stream_begin_msg
= val
;
3101 void bt_msg_iter_set_emit_stream_end_message(struct bt_msg_iter
*notit
,
3104 notit
->emit_stream_end_msg
= val
;
3108 void bt_msg_iter_set_dry_run(struct bt_msg_iter
*notit
,
3111 notit
->dry_run
= val
;