ctf: msg-iter.c: use `_APPEND_CAUSE` variants of logging macros
[babeltrace.git] / src / plugins / ctf / common / msg-iter / msg-iter.c
index 7c4756716d3321f22980cefef5e2aa9b58f42032..c24f5ba913ea1adbbffb5adc22305ddb727ba876 100644 (file)
@@ -80,6 +80,7 @@ struct stack {
 /* State */
 enum state {
        STATE_INIT,
+       STATE_SWITCH_PACKET,
        STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN,
        STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE,
        STATE_AFTER_TRACE_PACKET_HEADER,
@@ -103,10 +104,12 @@ enum state {
        STATE_DSCOPE_EVENT_PAYLOAD_BEGIN,
        STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE,
        STATE_EMIT_MSG_EVENT,
+       STATE_EMIT_QUEUED_MSG_EVENT,
        STATE_SKIP_PACKET_PADDING,
        STATE_EMIT_MSG_PACKET_END_MULTI,
        STATE_EMIT_MSG_PACKET_END_SINGLE,
        STATE_CHECK_EMIT_MSG_STREAM_END,
+       STATE_EMIT_QUEUED_MSG_PACKET_END,
        STATE_EMIT_MSG_STREAM_END,
        STATE_DONE,
 };
@@ -132,6 +135,12 @@ struct bt_msg_iter {
        /* True to emit a stream end message. */
        bool emit_stream_end_msg;
 
+       /*
+        * True if library objects are unavailable during the decoding and
+        * should not be created/used.
+        */
+       bool dry_run;
+
        /* True to set the stream */
        bool set_stream;
 
@@ -174,6 +183,12 @@ struct bt_msg_iter {
        /* Current event message (NULL if not created yet) */
        bt_message *event_msg;
 
+       /*
+        * True if we need to emit a packet beginning message before we emit
+        * the next event message or the packet end message.
+        */
+       bool emit_delayed_packet_beginning_msg;
+
        /* Database of current dynamic scopes */
        struct {
                bt_field *stream_packet_context;
@@ -259,6 +274,8 @@ const char *state_string(enum state state)
        switch (state) {
        case STATE_INIT:
                return "INIT";
+       case STATE_SWITCH_PACKET:
+               return "SWITCH_PACKET";
        case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
                return "DSCOPE_TRACE_PACKET_HEADER_BEGIN";
        case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
@@ -299,12 +316,16 @@ const char *state_string(enum state state)
                return "DSCOPE_EVENT_PAYLOAD_CONTINUE";
        case STATE_EMIT_MSG_EVENT:
                return "EMIT_MSG_EVENT";
+       case STATE_EMIT_QUEUED_MSG_EVENT:
+               return "EMIT_QUEUED_MSG_EVENT";
        case STATE_SKIP_PACKET_PADDING:
                return "SKIP_PACKET_PADDING";
        case STATE_EMIT_MSG_PACKET_END_MULTI:
                return "EMIT_MSG_PACKET_END_MULTI";
        case STATE_EMIT_MSG_PACKET_END_SINGLE:
                return "EMIT_MSG_PACKET_END_SINGLE";
+       case STATE_EMIT_QUEUED_MSG_PACKET_END:
+               return "EMIT_QUEUED_MSG_PACKET_END";
        case STATE_EMIT_MSG_STREAM_END:
                return "EMIT_MSG_STREAM_END";
        case STATE_DONE:
@@ -314,24 +335,24 @@ const char *state_string(enum state state)
        }
 }
 
-static
-int bt_msg_iter_switch_packet(struct bt_msg_iter *notit);
-
 static
 struct stack *stack_new(struct bt_msg_iter *notit)
 {
+       bt_self_component *self_comp = notit->self_comp;
        struct stack *stack = NULL;
 
        stack = g_new0(struct stack, 1);
        if (!stack) {
-               BT_COMP_LOGE_STR("Failed to allocate one stack.");
+               BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                       "Failed to allocate one stack.");
                goto error;
        }
 
        stack->notit = notit;
        stack->entries = g_array_new(FALSE, TRUE, sizeof(struct stack_entry));
        if (!stack->entries) {
-               BT_COMP_LOGE_STR("Failed to allocate a GArray.");
+               BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                       "Failed to allocate a GArray.");
                goto error;
        }
 
@@ -351,7 +372,7 @@ void stack_destroy(struct stack *stack)
 {
        struct bt_msg_iter *notit;
 
-       BT_ASSERT(stack);
+       BT_ASSERT_DBG(stack);
        notit = stack->notit;
        BT_COMP_LOGD("Destroying stack: addr=%p", stack);
 
@@ -368,9 +389,9 @@ void stack_push(struct stack *stack, bt_field *base)
        struct stack_entry *entry;
        struct bt_msg_iter *notit;
 
-       BT_ASSERT(stack);
+       BT_ASSERT_DBG(stack);
        notit = stack->notit;
-       BT_ASSERT(base);
+       BT_ASSERT_DBG(base);
        BT_COMP_LOGT("Pushing base field on stack: stack-addr=%p, "
                "stack-size-before=%zu, stack-size-after=%zu",
                stack, stack->size, stack->size + 1);
@@ -388,7 +409,7 @@ void stack_push(struct stack *stack, bt_field *base)
 static inline
 unsigned int stack_size(struct stack *stack)
 {
-       BT_ASSERT(stack);
+       BT_ASSERT_DBG(stack);
        return stack->size;
 }
 
@@ -397,8 +418,8 @@ void stack_pop(struct stack *stack)
 {
        struct bt_msg_iter *notit;
 
-       BT_ASSERT(stack);
-       BT_ASSERT(stack_size(stack));
+       BT_ASSERT_DBG(stack);
+       BT_ASSERT_DBG(stack_size(stack));
        notit = stack->notit;
        BT_COMP_LOGT("Popping from stack: "
                "stack-addr=%p, stack-size-before=%zu, stack-size-after=%zu",
@@ -409,8 +430,8 @@ void stack_pop(struct stack *stack)
 static inline
 struct stack_entry *stack_top(struct stack *stack)
 {
-       BT_ASSERT(stack);
-       BT_ASSERT(stack_size(stack));
+       BT_ASSERT_DBG(stack);
+       BT_ASSERT_DBG(stack_size(stack));
        return &g_array_index(stack->entries, struct stack_entry,
                stack->size - 1);
 }
@@ -424,7 +445,7 @@ bool stack_empty(struct stack *stack)
 static
 void stack_clear(struct stack *stack)
 {
-       BT_ASSERT(stack);
+       BT_ASSERT_DBG(stack);
        stack->size = 0;
 }
 
@@ -466,6 +487,7 @@ static
 enum bt_msg_iter_status request_medium_bytes(
                struct bt_msg_iter *notit)
 {
+       bt_self_component *self_comp = notit->self_comp;
        uint8_t *buffer_addr = NULL;
        size_t buffer_sz = 0;
        enum bt_msg_iter_medium_status m_status;
@@ -523,7 +545,8 @@ enum bt_msg_iter_status request_medium_bytes(
                }
 
                /* All other states are invalid */
-               BT_COMP_LOGW("User function returned %s, but message iterator is in an unexpected state: "
+               BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                       "User function returned %s, but message iterator is in an unexpected state: "
                        "state=%s, cur-packet-size=%" PRId64 ", cur=%zu, "
                        "packet-cur=%zu, last-eh-at=%zu",
                        bt_msg_iter_medium_status_string(m_status),
@@ -533,8 +556,8 @@ enum bt_msg_iter_status request_medium_bytes(
                        notit->buf.last_eh_at);
                m_status = BT_MSG_ITER_MEDIUM_STATUS_ERROR;
        } else if (m_status < 0) {
-               BT_COMP_LOGW("User function failed: status=%s",
-                       bt_msg_iter_medium_status_string(m_status));
+               BT_COMP_LOGE_APPEND_CAUSE(self_comp, "User function failed: "
+                       "status=%s", bt_msg_iter_medium_status_string(m_status));
        }
 
 end:
@@ -566,6 +589,7 @@ enum bt_msg_iter_status read_dscope_begin_state(
                bt_field *dscope_field)
 {
        enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
+       bt_self_component *self_comp = notit->self_comp;
        enum bt_bfcr_status bfcr_status;
        size_t consumed_bits;
 
@@ -588,7 +612,8 @@ enum bt_msg_iter_status read_dscope_begin_state(
                notit->state = continue_state;
                break;
        default:
-               BT_COMP_LOGW("BFCR failed to start: notit-addr=%p, bfcr-addr=%p, "
+               BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                       "BFCR failed to start: notit-addr=%p, bfcr-addr=%p, "
                        "status=%s", notit, notit->bfcr,
                        bt_bfcr_status_string(bfcr_status));
                status = BT_MSG_ITER_STATUS_ERROR;
@@ -607,6 +632,7 @@ enum bt_msg_iter_status read_dscope_continue_state(
                struct bt_msg_iter *notit, enum state done_state)
 {
        enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
+       bt_self_component *self_comp = notit->self_comp;
        enum bt_bfcr_status bfcr_status;
        size_t consumed_bits;
 
@@ -616,7 +642,8 @@ enum bt_msg_iter_status read_dscope_continue_state(
        status = buf_ensure_available_bits(notit);
        if (status != BT_MSG_ITER_STATUS_OK) {
                if (status < 0) {
-                       BT_COMP_LOGW("Cannot ensure that buffer has at least one byte: "
+                       BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                               "Cannot ensure that buffer has at least one byte: "
                                "msg-addr=%p, status=%s",
                                notit, bt_msg_iter_status_string(status));
                } else {
@@ -643,7 +670,8 @@ enum bt_msg_iter_status read_dscope_continue_state(
                BT_COMP_LOGT_STR("BFCR needs more data to decode field completely.");
                break;
        default:
-               BT_COMP_LOGW("BFCR failed to continue: notit-addr=%p, bfcr-addr=%p, "
+               BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                       "BFCR failed to continue: notit-addr=%p, bfcr-addr=%p, "
                        "status=%s", notit, notit->bfcr,
                        bt_bfcr_status_string(bfcr_status));
                status = BT_MSG_ITER_STATUS_ERROR;
@@ -677,30 +705,92 @@ void release_all_dscopes(struct bt_msg_iter *notit)
        release_event_dscopes(notit);
 }
 
+static
+enum bt_msg_iter_status switch_packet_state(struct bt_msg_iter *notit)
+{
+       enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
+       bt_self_component *self_comp = notit->self_comp;
+
+       /*
+        * We don't put the stream class here because we need to make
+        * sure that all the packets processed by the same message
+        * iterator refer to the same stream class (the first one).
+        */
+       BT_ASSERT(notit);
+
+       if (notit->cur_exp_packet_total_size != -1) {
+               notit->cur_packet_offset += notit->cur_exp_packet_total_size;
+       }
+
+       BT_COMP_LOGD("Switching packet: notit-addr=%p, cur=%zu, "
+               "packet-offset=%" PRId64, notit, notit->buf.at,
+               notit->cur_packet_offset);
+       stack_clear(notit->stack);
+       notit->meta.ec = NULL;
+       BT_PACKET_PUT_REF_AND_RESET(notit->packet);
+       BT_MESSAGE_PUT_REF_AND_RESET(notit->event_msg);
+       release_all_dscopes(notit);
+       notit->cur_dscope_field = NULL;
+
+       /*
+        * Adjust current buffer so that addr points to the beginning of the new
+        * packet.
+        */
+       if (notit->buf.addr) {
+               size_t consumed_bytes = (size_t) (notit->buf.at / CHAR_BIT);
+
+               /* Packets are assumed to start on a byte frontier. */
+               if (notit->buf.at % CHAR_BIT) {
+                       BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                               "Cannot switch packet: current position is not a multiple of 8: "
+                               "notit-addr=%p, cur=%zu", notit, notit->buf.at);
+                       status = BT_MSG_ITER_STATUS_ERROR;
+                       goto end;
+               }
+
+               notit->buf.addr += consumed_bytes;
+               notit->buf.sz -= consumed_bytes;
+               notit->buf.at = 0;
+               notit->buf.packet_offset = 0;
+               BT_COMP_LOGD("Adjusted buffer: addr=%p, size=%zu",
+                       notit->buf.addr, notit->buf.sz);
+       }
+
+       notit->cur_exp_packet_content_size = -1;
+       notit->cur_exp_packet_total_size = -1;
+       notit->cur_stream_class_id = -1;
+       notit->cur_event_class_id = -1;
+       notit->cur_data_stream_id = -1;
+       notit->prev_packet_snapshots = notit->snapshots;
+       notit->snapshots.discarded_events = UINT64_C(-1);
+       notit->snapshots.packets = UINT64_C(-1);
+       notit->snapshots.beginning_clock = UINT64_C(-1);
+       notit->snapshots.end_clock = UINT64_C(-1);
+       notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
+
+end:
+       return status;
+}
+
 static
 enum bt_msg_iter_status read_packet_header_begin_state(
                struct bt_msg_iter *notit)
 {
        struct ctf_field_class *packet_header_fc = NULL;
-       enum bt_msg_iter_status ret = BT_MSG_ITER_STATUS_OK;
-
-       if (bt_msg_iter_switch_packet(notit)) {
-               BT_COMP_LOGW("Cannot switch packet: notit-addr=%p", notit);
-               ret = BT_MSG_ITER_STATUS_ERROR;
-               goto end;
-       }
+       bt_self_component *self_comp = notit->self_comp;
+       enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
 
        /*
         * Make sure at least one bit is available for this packet. An
         * empty packet is impossible. If we reach the end of the medium
         * at this point, then it's considered the end of the stream.
         */
-       ret = buf_ensure_available_bits(notit);
-       switch (ret) {
+       status = buf_ensure_available_bits(notit);
+       switch (status) {
        case BT_MSG_ITER_STATUS_OK:
                break;
        case BT_MSG_ITER_STATUS_EOF:
-               ret = BT_MSG_ITER_STATUS_OK;
+               status = BT_MSG_ITER_STATUS_OK;
                notit->state = STATE_CHECK_EMIT_MSG_STREAM_END;
                goto end;
        default:
@@ -720,18 +810,19 @@ enum bt_msg_iter_status read_packet_header_begin_state(
        BT_COMP_LOGD("Decoding packet header field:"
                "notit-addr=%p, trace-class-addr=%p, fc-addr=%p",
                notit, notit->meta.tc, packet_header_fc);
-       ret = read_dscope_begin_state(notit, packet_header_fc,
+       status = read_dscope_begin_state(notit, packet_header_fc,
                STATE_AFTER_TRACE_PACKET_HEADER,
                STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE, NULL);
-       if (ret < 0) {
-               BT_COMP_LOGW("Cannot decode packet header field: "
+       if (status < 0) {
+               BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                       "Cannot decode packet header field: "
                        "notit-addr=%p, trace-class-addr=%p, "
                        "fc-addr=%p",
                        notit, notit->meta.tc, packet_header_fc);
        }
 
 end:
-       return ret;
+       return status;
 }
 
 static
@@ -746,6 +837,7 @@ static inline
 enum bt_msg_iter_status set_current_stream_class(struct bt_msg_iter *notit)
 {
        enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
+       bt_self_component *self_comp = notit->self_comp;
        struct ctf_stream_class *new_stream_class = NULL;
 
        if (notit->cur_stream_class_id == -1) {
@@ -754,7 +846,8 @@ enum bt_msg_iter_status set_current_stream_class(struct bt_msg_iter *notit)
                 * stream class.
                 */
                if (notit->meta.tc->stream_classes->len != 1) {
-                       BT_COMP_LOGW("Need exactly one stream class since there's "
+                       BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                               "Need exactly one stream class since there's "
                                "no stream class ID field: "
                                "notit-addr=%p", notit);
                        status = BT_MSG_ITER_STATUS_ERROR;
@@ -768,7 +861,8 @@ enum bt_msg_iter_status set_current_stream_class(struct bt_msg_iter *notit)
        new_stream_class = ctf_trace_class_borrow_stream_class_by_id(
                notit->meta.tc, notit->cur_stream_class_id);
        if (!new_stream_class) {
-               BT_COMP_LOGW("No stream class with ID of stream class ID to use in trace class: "
+               BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                       "No stream class with ID of stream class ID to use in trace class: "
                        "notit-addr=%p, stream-class-id=%" PRIu64 ", "
                        "trace-class-addr=%p",
                        notit, notit->cur_stream_class_id, notit->meta.tc);
@@ -778,7 +872,8 @@ enum bt_msg_iter_status set_current_stream_class(struct bt_msg_iter *notit)
 
        if (notit->meta.sc) {
                if (new_stream_class != notit->meta.sc) {
-                       BT_COMP_LOGW("Two packets refer to two different stream classes within the same packet sequence: "
+                       BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                               "Two packets refer to two different stream classes within the same packet sequence: "
                                "notit-addr=%p, prev-stream-class-addr=%p, "
                                "prev-stream-class-id=%" PRId64 ", "
                                "next-stream-class-addr=%p, "
@@ -809,6 +904,7 @@ static inline
 enum bt_msg_iter_status set_current_stream(struct bt_msg_iter *notit)
 {
        enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
+       bt_self_component *self_comp = notit->self_comp;
        bt_stream *stream = NULL;
 
        BT_COMP_LOGD("Calling user function (get stream): notit-addr=%p, "
@@ -821,15 +917,15 @@ enum bt_msg_iter_status set_current_stream(struct bt_msg_iter *notit)
        bt_stream_get_ref(stream);
        BT_COMP_LOGD("User function returned: stream-addr=%p", stream);
        if (!stream) {
-               BT_COMP_LOGW_STR("User function failed to return a stream object "
-                       "for the given stream class.");
+               BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                       "User function failed to return a stream object for the given stream class.");
                status = BT_MSG_ITER_STATUS_ERROR;
                goto end;
        }
 
        if (notit->stream && stream != notit->stream) {
-               BT_COMP_LOGW("User function returned a different stream than the "
-                       "previous one for the same sequence of packets.");
+               BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                       "User function returned a different stream than the previous one for the same sequence of packets.");
                status = BT_MSG_ITER_STATUS_ERROR;
                goto end;
        }
@@ -845,10 +941,9 @@ static inline
 enum bt_msg_iter_status set_current_packet(struct bt_msg_iter *notit)
 {
        enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
+       bt_self_component *self_comp = notit->self_comp;
        bt_packet *packet = NULL;
 
-       BT_COMP_LOGD("Creating packet for packet message: "
-               "notit-addr=%p", notit);
        BT_COMP_LOGD("Creating packet from stream: "
                "notit-addr=%p, stream-addr=%p, "
                "stream-class-addr=%p, "
@@ -860,7 +955,8 @@ enum bt_msg_iter_status set_current_packet(struct bt_msg_iter *notit)
        BT_ASSERT(notit->stream);
        packet = bt_packet_create(notit->stream);
        if (!packet) {
-               BT_COMP_LOGE("Cannot create packet from stream: "
+               BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                       "Cannot create packet from stream: "
                        "notit-addr=%p, stream-addr=%p, "
                        "stream-class-addr=%p, "
                        "stream-class-id=%" PRId64,
@@ -902,6 +998,7 @@ enum bt_msg_iter_status read_packet_context_begin_state(
                struct bt_msg_iter *notit)
 {
        enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
+       bt_self_component *self_comp = notit->self_comp;
        struct ctf_field_class *packet_context_fc;
 
        BT_ASSERT(notit->meta.sc);
@@ -918,7 +1015,7 @@ enum bt_msg_iter_status read_packet_context_begin_state(
 
        BT_ASSERT(!notit->packet_context_field);
 
-       if (packet_context_fc->in_ir) {
+       if (packet_context_fc->in_ir && !notit->dry_run) {
                /*
                 * Create free packet context field from stream class.
                 * This field is going to be moved to the packet once we
@@ -932,7 +1029,8 @@ enum bt_msg_iter_status read_packet_context_begin_state(
                        bt_packet_context_field_create(
                                notit->meta.sc->ir_sc);
                if (!notit->packet_context_field) {
-                       BT_COMP_LOGE_STR("Cannot create packet context field wrapper from stream class.");
+                       BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                               "Cannot create packet context field wrapper from stream class.");
                        status = BT_MSG_ITER_STATUS_ERROR;
                        goto end;
                }
@@ -953,7 +1051,8 @@ enum bt_msg_iter_status read_packet_context_begin_state(
                STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE,
                notit->dscopes.stream_packet_context);
        if (status < 0) {
-               BT_COMP_LOGW("Cannot decode packet context field: "
+               BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                       "Cannot decode packet context field: "
                        "notit-addr=%p, stream-class-addr=%p, "
                        "stream-class-id=%" PRId64 ", fc-addr=%p",
                        notit, notit->meta.sc,
@@ -978,6 +1077,7 @@ enum bt_msg_iter_status set_current_packet_content_sizes(
                struct bt_msg_iter *notit)
 {
        enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
+       bt_self_component *self_comp = notit->self_comp;
 
        if (notit->cur_exp_packet_total_size == -1) {
                if (notit->cur_exp_packet_content_size != -1) {
@@ -998,7 +1098,8 @@ enum bt_msg_iter_status set_current_packet_content_sizes(
 
        if (notit->cur_exp_packet_content_size >
                        notit->cur_exp_packet_total_size) {
-               BT_COMP_LOGW("Invalid packet or content size: "
+               BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                       "Invalid packet or content size: "
                        "content size is greater than packet size: "
                        "notit-addr=%p, packet-context-field-addr=%p, "
                        "packet-size=%" PRId64 ", content-size=%" PRId64,
@@ -1047,6 +1148,7 @@ static
 enum bt_msg_iter_status read_event_header_begin_state(struct bt_msg_iter *notit)
 {
        enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
+       bt_self_component *self_comp = notit->self_comp;
        struct ctf_field_class *event_header_fc = NULL;
 
        /* Reset the position of the last event header */
@@ -1110,7 +1212,8 @@ enum bt_msg_iter_status read_event_header_begin_state(struct bt_msg_iter *notit)
                STATE_AFTER_EVENT_HEADER,
                STATE_DSCOPE_EVENT_HEADER_CONTINUE, NULL);
        if (status < 0) {
-               BT_COMP_LOGW("Cannot decode event header field: "
+               BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                       "Cannot decode event header field: "
                        "notit-addr=%p, stream-class-addr=%p, "
                        "stream-class-id=%" PRId64 ", fc-addr=%p",
                        notit, notit->meta.sc,
@@ -1134,6 +1237,7 @@ static inline
 enum bt_msg_iter_status set_current_event_class(struct bt_msg_iter *notit)
 {
        enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
+       bt_self_component *self_comp = notit->self_comp;
 
        struct ctf_event_class *new_event_class = NULL;
 
@@ -1143,8 +1247,8 @@ enum bt_msg_iter_status set_current_event_class(struct bt_msg_iter *notit)
                 * event class.
                 */
                if (notit->meta.sc->event_classes->len != 1) {
-                       BT_COMP_LOGW("Need exactly one event class since there's "
-                               "no event class ID field: "
+                       BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                               "Need exactly one event class since there's no event class ID field: "
                                "notit-addr=%p", notit);
                        status = BT_MSG_ITER_STATUS_ERROR;
                        goto end;
@@ -1157,7 +1261,8 @@ enum bt_msg_iter_status set_current_event_class(struct bt_msg_iter *notit)
        new_event_class = ctf_stream_class_borrow_event_class_by_id(
                notit->meta.sc, notit->cur_event_class_id);
        if (!new_event_class) {
-               BT_COMP_LOGW("No event class with ID of event class ID to use in stream class: "
+               BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                       "No event class with ID of event class ID to use in stream class: "
                        "notit-addr=%p, stream-class-id=%" PRIu64 ", "
                        "event-class-id=%" PRIu64 ", "
                        "trace-class-addr=%p",
@@ -1184,17 +1289,18 @@ enum bt_msg_iter_status set_current_event_message(
                struct bt_msg_iter *notit)
 {
        enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
+       bt_self_component *self_comp = notit->self_comp;
        bt_message *msg = NULL;
 
-       BT_ASSERT(notit->meta.ec);
-       BT_ASSERT(notit->packet);
+       BT_ASSERT_DBG(notit->meta.ec);
+       BT_ASSERT_DBG(notit->packet);
        BT_COMP_LOGD("Creating event message from event class and packet: "
                "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", packet-addr=%p",
                notit, notit->meta.ec,
                notit->meta.ec->name->str,
                notit->packet);
-       BT_ASSERT(notit->msg_iter);
-       BT_ASSERT(notit->meta.sc);
+       BT_ASSERT_DBG(notit->msg_iter);
+       BT_ASSERT_DBG(notit->meta.sc);
 
        if (bt_stream_class_borrow_default_clock_class(notit->meta.sc->ir_sc)) {
                msg = bt_message_event_create_with_packet_and_default_clock_snapshot(
@@ -1206,7 +1312,8 @@ enum bt_msg_iter_status set_current_event_message(
        }
 
        if (!msg) {
-               BT_COMP_LOGE("Cannot create event message: "
+               BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                       "Cannot create event message: "
                        "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", "
                        "packet-addr=%p",
                        notit, notit->meta.ec,
@@ -1237,6 +1344,10 @@ enum bt_msg_iter_status after_event_header_state(
                goto end;
        }
 
+       if (G_UNLIKELY(notit->dry_run)) {
+               goto next_state;
+       }
+
        status = set_current_event_message(notit);
        if (status != BT_MSG_ITER_STATUS_OK) {
                goto end;
@@ -1244,7 +1355,9 @@ enum bt_msg_iter_status after_event_header_state(
 
        notit->event = bt_message_event_borrow_event(
                notit->event_msg);
-       BT_ASSERT(notit->event);
+       BT_ASSERT_DBG(notit->event);
+
+next_state:
        notit->state = STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN;
 
 end:
@@ -1256,6 +1369,7 @@ enum bt_msg_iter_status read_event_common_context_begin_state(
                struct bt_msg_iter *notit)
 {
        enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
+       bt_self_component *self_comp = notit->self_comp;
        struct ctf_field_class *event_common_context_fc;
 
        event_common_context_fc = notit->meta.sc->event_common_context_fc;
@@ -1264,12 +1378,12 @@ enum bt_msg_iter_status read_event_common_context_begin_state(
                goto end;
        }
 
-       if (event_common_context_fc->in_ir) {
-               BT_ASSERT(!notit->dscopes.event_common_context);
+       if (event_common_context_fc->in_ir && !notit->dry_run) {
+               BT_ASSERT_DBG(!notit->dscopes.event_common_context);
                notit->dscopes.event_common_context =
                        bt_event_borrow_common_context_field(
                                notit->event);
-               BT_ASSERT(notit->dscopes.event_common_context);
+               BT_ASSERT_DBG(notit->dscopes.event_common_context);
        }
 
        BT_COMP_LOGT("Decoding event common context field: "
@@ -1284,7 +1398,8 @@ enum bt_msg_iter_status read_event_common_context_begin_state(
                STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE,
                notit->dscopes.event_common_context);
        if (status < 0) {
-               BT_COMP_LOGW("Cannot decode event common context field: "
+               BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                       "Cannot decode event common context field: "
                        "notit-addr=%p, stream-class-addr=%p, "
                        "stream-class-id=%" PRId64 ", fc-addr=%p",
                        notit, notit->meta.sc,
@@ -1309,6 +1424,7 @@ enum bt_msg_iter_status read_event_spec_context_begin_state(
                struct bt_msg_iter *notit)
 {
        enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
+       bt_self_component *self_comp = notit->self_comp;
        struct ctf_field_class *event_spec_context_fc;
 
        event_spec_context_fc = notit->meta.ec->spec_context_fc;
@@ -1317,12 +1433,12 @@ enum bt_msg_iter_status read_event_spec_context_begin_state(
                goto end;
        }
 
-       if (event_spec_context_fc->in_ir) {
-               BT_ASSERT(!notit->dscopes.event_spec_context);
+       if (event_spec_context_fc->in_ir && !notit->dry_run) {
+               BT_ASSERT_DBG(!notit->dscopes.event_spec_context);
                notit->dscopes.event_spec_context =
                        bt_event_borrow_specific_context_field(
                                notit->event);
-               BT_ASSERT(notit->dscopes.event_spec_context);
+               BT_ASSERT_DBG(notit->dscopes.event_spec_context);
        }
 
        BT_COMP_LOGT("Decoding event specific context field: "
@@ -1338,7 +1454,8 @@ enum bt_msg_iter_status read_event_spec_context_begin_state(
                STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE,
                notit->dscopes.event_spec_context);
        if (status < 0) {
-               BT_COMP_LOGW("Cannot decode event specific context field: "
+               BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                       "Cannot decode event specific context field: "
                        "notit-addr=%p, event-class-addr=%p, "
                        "event-class-name=\"%s\", "
                        "event-class-id=%" PRId64 ", fc-addr=%p",
@@ -1365,6 +1482,7 @@ enum bt_msg_iter_status read_event_payload_begin_state(
                struct bt_msg_iter *notit)
 {
        enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
+       bt_self_component *self_comp = notit->self_comp;
        struct ctf_field_class *event_payload_fc;
 
        event_payload_fc = notit->meta.ec->payload_fc;
@@ -1373,12 +1491,12 @@ enum bt_msg_iter_status read_event_payload_begin_state(
                goto end;
        }
 
-       if (event_payload_fc->in_ir) {
-               BT_ASSERT(!notit->dscopes.event_payload);
+       if (event_payload_fc->in_ir && !notit->dry_run) {
+               BT_ASSERT_DBG(!notit->dscopes.event_payload);
                notit->dscopes.event_payload =
                        bt_event_borrow_payload_field(
                                notit->event);
-               BT_ASSERT(notit->dscopes.event_payload);
+               BT_ASSERT_DBG(notit->dscopes.event_payload);
        }
 
        BT_COMP_LOGT("Decoding event payload field: "
@@ -1394,7 +1512,8 @@ enum bt_msg_iter_status read_event_payload_begin_state(
                STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE,
                notit->dscopes.event_payload);
        if (status < 0) {
-               BT_COMP_LOGW("Cannot decode event payload field: "
+               BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                       "Cannot decode event payload field: "
                        "notit-addr=%p, event-class-addr=%p, "
                        "event-class-name=\"%s\", "
                        "event-class-id=%" PRId64 ", fc-addr=%p",
@@ -1420,11 +1539,12 @@ enum bt_msg_iter_status skip_packet_padding_state(struct bt_msg_iter *notit)
 {
        enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
        size_t bits_to_skip;
+       const enum state next_state = STATE_SWITCH_PACKET;
 
        BT_ASSERT(notit->cur_exp_packet_total_size > 0);
        bits_to_skip = notit->cur_exp_packet_total_size - packet_at(notit);
        if (bits_to_skip == 0) {
-               notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
+               notit->state = next_state;
                goto end;
        } else {
                size_t bits_to_consume;
@@ -1443,7 +1563,7 @@ enum bt_msg_iter_status skip_packet_padding_state(struct bt_msg_iter *notit)
                bits_to_skip = notit->cur_exp_packet_total_size -
                        packet_at(notit);
                if (bits_to_skip == 0) {
-                       notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
+                       notit->state = next_state;
                        goto end;
                }
        }
@@ -1585,7 +1705,10 @@ enum bt_msg_iter_status handle_state(struct bt_msg_iter *notit)
        // TODO: optimalize!
        switch (state) {
        case STATE_INIT:
-               notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
+               notit->state = STATE_SWITCH_PACKET;
+               break;
+       case STATE_SWITCH_PACKET:
+               status = switch_packet_state(notit);
                break;
        case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
                status = read_packet_header_begin_state(notit);
@@ -1656,6 +1779,9 @@ enum bt_msg_iter_status handle_state(struct bt_msg_iter *notit)
        case STATE_EMIT_MSG_EVENT:
                notit->state = STATE_DSCOPE_EVENT_HEADER_BEGIN;
                break;
+       case STATE_EMIT_QUEUED_MSG_EVENT:
+               notit->state = STATE_EMIT_MSG_EVENT;
+               break;
        case STATE_SKIP_PACKET_PADDING:
                status = skip_packet_padding_state(notit);
                break;
@@ -1668,6 +1794,9 @@ enum bt_msg_iter_status handle_state(struct bt_msg_iter *notit)
        case STATE_CHECK_EMIT_MSG_STREAM_END:
                status = check_emit_msg_stream_end(notit);
                break;
+       case STATE_EMIT_QUEUED_MSG_PACKET_END:
+               notit->state = STATE_EMIT_MSG_PACKET_END_SINGLE;
+               break;
        case STATE_EMIT_MSG_STREAM_END:
                notit->state = STATE_DONE;
                break;
@@ -1728,8 +1857,6 @@ void bt_msg_iter_reset(struct bt_msg_iter *notit)
        bt_msg_iter_reset_for_next_stream_file(notit);
        notit->cur_stream_class_id = -1;
        notit->cur_data_stream_id = -1;
-       notit->emit_stream_begin_msg = true;
-       notit->emit_stream_end_msg = true;
        notit->snapshots.discarded_events = UINT64_C(-1);
        notit->snapshots.packets = UINT64_C(-1);
        notit->prev_packet_snapshots.discarded_events = UINT64_C(-1);
@@ -1738,115 +1865,46 @@ void bt_msg_iter_reset(struct bt_msg_iter *notit)
        notit->prev_packet_snapshots.end_clock = UINT64_C(-1);
 }
 
-static
-int bt_msg_iter_switch_packet(struct bt_msg_iter *notit)
-{
-       int ret = 0;
-
-       /*
-        * We don't put the stream class here because we need to make
-        * sure that all the packets processed by the same message
-        * iterator refer to the same stream class (the first one).
-        */
-       BT_ASSERT(notit);
-
-       if (notit->cur_exp_packet_total_size != -1) {
-               notit->cur_packet_offset += notit->cur_exp_packet_total_size;
-       }
-
-       BT_COMP_LOGD("Switching packet: notit-addr=%p, cur=%zu, "
-               "packet-offset=%" PRId64, notit, notit->buf.at,
-               notit->cur_packet_offset);
-       stack_clear(notit->stack);
-       notit->meta.ec = NULL;
-       BT_PACKET_PUT_REF_AND_RESET(notit->packet);
-       BT_MESSAGE_PUT_REF_AND_RESET(notit->event_msg);
-       release_all_dscopes(notit);
-       notit->cur_dscope_field = NULL;
-
-       /*
-        * Adjust current buffer so that addr points to the beginning of the new
-        * packet.
-        */
-       if (notit->buf.addr) {
-               size_t consumed_bytes = (size_t) (notit->buf.at / CHAR_BIT);
-
-               /* Packets are assumed to start on a byte frontier. */
-               if (notit->buf.at % CHAR_BIT) {
-                       BT_COMP_LOGW("Cannot switch packet: current position is not a multiple of 8: "
-                               "notit-addr=%p, cur=%zu", notit, notit->buf.at);
-                       ret = -1;
-                       goto end;
-               }
-
-               notit->buf.addr += consumed_bytes;
-               notit->buf.sz -= consumed_bytes;
-               notit->buf.at = 0;
-               notit->buf.packet_offset = 0;
-               BT_COMP_LOGD("Adjusted buffer: addr=%p, size=%zu",
-                       notit->buf.addr, notit->buf.sz);
-       }
-
-       notit->cur_exp_packet_content_size = -1;
-       notit->cur_exp_packet_total_size = -1;
-       notit->cur_stream_class_id = -1;
-       notit->cur_event_class_id = -1;
-       notit->cur_data_stream_id = -1;
-       notit->prev_packet_snapshots = notit->snapshots;
-       notit->snapshots.discarded_events = UINT64_C(-1);
-       notit->snapshots.packets = UINT64_C(-1);
-       notit->snapshots.beginning_clock = UINT64_C(-1);
-       notit->snapshots.end_clock = UINT64_C(-1);
-
-end:
-       return ret;
-}
-
 static
 bt_field *borrow_next_field(struct bt_msg_iter *notit)
 {
        bt_field *next_field = NULL;
        bt_field *base_field;
        const bt_field_class *base_fc;
+       bt_field_class_type base_fc_type;
        size_t index;
 
-       BT_ASSERT(!stack_empty(notit->stack));
+       BT_ASSERT_DBG(!stack_empty(notit->stack));
        index = stack_top(notit->stack)->index;
        base_field = stack_top(notit->stack)->base;
-       BT_ASSERT(base_field);
+       BT_ASSERT_DBG(base_field);
        base_fc = bt_field_borrow_class_const(base_field);
-       BT_ASSERT(base_fc);
+       BT_ASSERT_DBG(base_fc);
+       base_fc_type = bt_field_class_get_type(base_fc);
 
-       switch (bt_field_class_get_type(base_fc)) {
-       case BT_FIELD_CLASS_TYPE_STRUCTURE:
-       {
-               BT_ASSERT(index <
+       if (base_fc_type == BT_FIELD_CLASS_TYPE_STRUCTURE) {
+               BT_ASSERT_DBG(index <
                        bt_field_class_structure_get_member_count(
                                bt_field_borrow_class_const(
                                                base_field)));
                next_field =
                        bt_field_structure_borrow_member_field_by_index(
                                base_field, index);
-               break;
-       }
-       case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
-       case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
-               BT_ASSERT(index < bt_field_array_get_length(base_field));
+       } else if (bt_field_class_type_is(base_fc_type,
+                       BT_FIELD_CLASS_TYPE_ARRAY)) {
+               BT_ASSERT_DBG(index < bt_field_array_get_length(base_field));
                next_field = bt_field_array_borrow_element_field_by_index(
                        base_field, index);
-               break;
-       case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR:
-       case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR:
-       case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR:
-               BT_ASSERT(index == 0);
+       } else if (bt_field_class_type_is(base_fc_type,
+                       BT_FIELD_CLASS_TYPE_VARIANT)) {
+               BT_ASSERT_DBG(index == 0);
                next_field = bt_field_variant_borrow_selected_option_field(
                        base_field);
-               break;
-       default:
+       } else {
                abort();
        }
 
-       BT_ASSERT(next_field);
+       BT_ASSERT_DBG(next_field);
        return next_field;
 }
 
@@ -1857,7 +1915,7 @@ void update_default_clock(struct bt_msg_iter *notit, uint64_t new_val,
        uint64_t new_val_mask;
        uint64_t cur_value_masked;
 
-       BT_ASSERT(new_val_size > 0);
+       BT_ASSERT_DBG(new_val_size > 0);
 
        /*
         * Special case for a 64-bit new value, which is the limit
@@ -1897,7 +1955,9 @@ enum bt_bfcr_status bfcr_unsigned_int_cb(uint64_t value,
                struct ctf_field_class *fc, void *data)
 {
        struct bt_msg_iter *notit = data;
+       bt_self_component *self_comp = notit->self_comp;
        enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
+
        bt_field *field = NULL;
        struct ctf_field_class_int *int_fc = (void *) fc;
 
@@ -1928,7 +1988,8 @@ enum bt_bfcr_status bfcr_unsigned_int_cb(uint64_t value,
                break;
        case CTF_FIELD_CLASS_MEANING_MAGIC:
                if (value != 0xc1fc1fc1) {
-                       BT_COMP_LOGW("Invalid CTF magic number: notit-addr=%p, "
+                       BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                               "Invalid CTF magic number: notit-addr=%p, "
                                "magic=%" PRIx64, notit, value);
                        status = BT_BFCR_STATUS_ERROR;
                        goto end;
@@ -1961,17 +2022,15 @@ update_def_clock:
                        (uint64_t) int_fc->storing_index) = value;
        }
 
-       if (G_UNLIKELY(!fc->in_ir)) {
+       if (G_UNLIKELY(!fc->in_ir || notit->dry_run)) {
                goto end;
        }
 
        field = borrow_next_field(notit);
-       BT_ASSERT(field);
-       BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
-       BT_ASSERT(bt_field_get_class_type(field) ==
-                 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER ||
-                 bt_field_get_class_type(field) ==
-                 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION);
+       BT_ASSERT_DBG(field);
+       BT_ASSERT_DBG(bt_field_borrow_class_const(field) == fc->ir_fc);
+       BT_ASSERT_DBG(bt_field_class_type_is(bt_field_get_class_type(field),
+               BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER));
        bt_field_integer_unsigned_set_value(field, value);
        stack_top(notit->stack)->index++;
 
@@ -1985,6 +2044,7 @@ enum bt_bfcr_status bfcr_unsigned_int_char_cb(uint64_t value,
 {
        int ret;
        struct bt_msg_iter *notit = data;
+       bt_self_component *self_comp = notit->self_comp;
        enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
        bt_field *string_field = NULL;
        struct ctf_field_class_int *int_fc = (void *) fc;
@@ -1994,11 +2054,11 @@ enum bt_bfcr_status bfcr_unsigned_int_char_cb(uint64_t value,
                "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
                "fc-type=%d, fc-in-ir=%d, value=%" PRIu64,
                notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
-       BT_ASSERT(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE);
-       BT_ASSERT(!int_fc->mapped_clock_class);
-       BT_ASSERT(int_fc->storing_index < 0);
+       BT_ASSERT_DBG(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE);
+       BT_ASSERT_DBG(!int_fc->mapped_clock_class);
+       BT_ASSERT_DBG(int_fc->storing_index < 0);
 
-       if (G_UNLIKELY(!fc->in_ir)) {
+       if (G_UNLIKELY(!fc->in_ir || notit->dry_run)) {
                goto end;
        }
 
@@ -2012,14 +2072,15 @@ enum bt_bfcr_status bfcr_unsigned_int_char_cb(uint64_t value,
        }
 
        string_field = stack_top(notit->stack)->base;
-       BT_ASSERT(bt_field_get_class_type(string_field) ==
-                 BT_FIELD_CLASS_TYPE_STRING);
+       BT_ASSERT_DBG(bt_field_get_class_type(string_field) ==
+               BT_FIELD_CLASS_TYPE_STRING);
 
        /* Append character */
        str[0] = (char) value;
        ret = bt_field_string_append_with_length(string_field, str, 1);
        if (ret) {
-               BT_COMP_LOGE("Cannot append character to string field's value: "
+               BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                       "Cannot append character to string field's value: "
                        "notit-addr=%p, field-addr=%p, ret=%d",
                        notit, string_field, ret);
                status = BT_BFCR_STATUS_ERROR;
@@ -2043,24 +2104,22 @@ enum bt_bfcr_status bfcr_signed_int_cb(int64_t value,
                "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
                "fc-type=%d, fc-in-ir=%d, value=%" PRId64,
                notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
-       BT_ASSERT(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE);
+       BT_ASSERT_DBG(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE);
 
        if (G_UNLIKELY(int_fc->storing_index >= 0)) {
                g_array_index(notit->stored_values, uint64_t,
                        (uint64_t) int_fc->storing_index) = (uint64_t) value;
        }
 
-       if (G_UNLIKELY(!fc->in_ir)) {
+       if (G_UNLIKELY(!fc->in_ir || notit->dry_run)) {
                goto end;
        }
 
        field = borrow_next_field(notit);
-       BT_ASSERT(field);
-       BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
-       BT_ASSERT(bt_field_get_class_type(field) ==
-                 BT_FIELD_CLASS_TYPE_SIGNED_INTEGER ||
-                 bt_field_get_class_type(field) ==
-                 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION);
+       BT_ASSERT_DBG(field);
+       BT_ASSERT_DBG(bt_field_borrow_class_const(field) == fc->ir_fc);
+       BT_ASSERT_DBG(bt_field_class_type_is(bt_field_get_class_type(field),
+               BT_FIELD_CLASS_TYPE_SIGNED_INTEGER));
        bt_field_integer_signed_set_value(field, value);
        stack_top(notit->stack)->index++;
 
@@ -2075,22 +2134,28 @@ enum bt_bfcr_status bfcr_floating_point_cb(double value,
        enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
        bt_field *field = NULL;
        struct bt_msg_iter *notit = data;
+       bt_field_class_type type;
 
        BT_COMP_LOGT("Floating point number function called from BFCR: "
                "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
                "fc-type=%d, fc-in-ir=%d, value=%f",
                notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
 
-       if (G_UNLIKELY(!fc->in_ir)) {
+       if (G_UNLIKELY(!fc->in_ir || notit->dry_run)) {
                goto end;
        }
 
        field = borrow_next_field(notit);
-       BT_ASSERT(field);
-       BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
-       BT_ASSERT(bt_field_get_class_type(field) ==
-                 BT_FIELD_CLASS_TYPE_REAL);
-       bt_field_real_set_value(field, value);
+       type = bt_field_get_class_type(field);
+       BT_ASSERT_DBG(field);
+       BT_ASSERT_DBG(bt_field_borrow_class_const(field) == fc->ir_fc);
+       BT_ASSERT_DBG(bt_field_class_type_is(type, BT_FIELD_CLASS_TYPE_REAL));
+
+       if (type == BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL) {
+               bt_field_real_single_precision_set_value(field, (float) value);
+       } else {
+               bt_field_real_double_precision_set_value(field, value);
+       }
        stack_top(notit->stack)->index++;
 
 end:
@@ -2109,14 +2174,14 @@ enum bt_bfcr_status bfcr_string_begin_cb(
                "fc-type=%d, fc-in-ir=%d",
                notit, notit->bfcr, fc, fc->type, fc->in_ir);
 
-       if (G_UNLIKELY(!fc->in_ir)) {
+       if (G_UNLIKELY(!fc->in_ir || notit->dry_run)) {
                goto end;
        }
 
        field = borrow_next_field(notit);
-       BT_ASSERT(field);
-       BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
-       BT_ASSERT(bt_field_get_class_type(field) ==
+       BT_ASSERT_DBG(field);
+       BT_ASSERT_DBG(bt_field_borrow_class_const(field) == fc->ir_fc);
+       BT_ASSERT_DBG(bt_field_get_class_type(field) ==
                  BT_FIELD_CLASS_TYPE_STRING);
        bt_field_string_clear(field);
 
@@ -2138,6 +2203,7 @@ enum bt_bfcr_status bfcr_string_cb(const char *value,
        enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
        bt_field *field = NULL;
        struct bt_msg_iter *notit = data;
+       bt_self_component *self_comp = notit->self_comp;
        int ret;
 
        BT_COMP_LOGT("String (substring) function called from BFCR: "
@@ -2146,17 +2212,18 @@ enum bt_bfcr_status bfcr_string_cb(const char *value,
                notit, notit->bfcr, fc, fc->type, fc->in_ir,
                len);
 
-       if (G_UNLIKELY(!fc->in_ir)) {
+       if (G_UNLIKELY(!fc->in_ir || notit->dry_run)) {
                goto end;
        }
 
        field = stack_top(notit->stack)->base;
-       BT_ASSERT(field);
+       BT_ASSERT_DBG(field);
 
        /* Append current substring */
        ret = bt_field_string_append_with_length(field, value, len);
        if (ret) {
-               BT_COMP_LOGE("Cannot append substring to string field's value: "
+               BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                       "Cannot append substring to string field's value: "
                        "notit-addr=%p, field-addr=%p, string-length=%zu, "
                        "ret=%d", notit, field, len, ret);
                status = BT_BFCR_STATUS_ERROR;
@@ -2178,7 +2245,7 @@ enum bt_bfcr_status bfcr_string_end_cb(
                "fc-type=%d, fc-in-ir=%d",
                notit, notit->bfcr, fc, fc->type, fc->in_ir);
 
-       if (G_UNLIKELY(!fc->in_ir)) {
+       if (G_UNLIKELY(!fc->in_ir || notit->dry_run)) {
                goto end;
        }
 
@@ -2192,6 +2259,7 @@ end:
        return BT_BFCR_STATUS_OK;
 }
 
+static
 enum bt_bfcr_status bfcr_compound_begin_cb(
                struct ctf_field_class *fc, void *data)
 {
@@ -2203,7 +2271,7 @@ enum bt_bfcr_status bfcr_compound_begin_cb(
                "fc-type=%d, fc-in-ir=%d",
                notit, notit->bfcr, fc, fc->type, fc->in_ir);
 
-       if (!fc->in_ir) {
+       if (G_UNLIKELY(!fc->in_ir || notit->dry_run)) {
                goto end;
        }
 
@@ -2213,12 +2281,12 @@ enum bt_bfcr_status bfcr_compound_begin_cb(
                field = notit->cur_dscope_field;
        } else {
                field = borrow_next_field(notit);
-               BT_ASSERT(field);
+               BT_ASSERT_DBG(field);
        }
 
        /* Push field */
-       BT_ASSERT(field);
-       BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
+       BT_ASSERT_DBG(field);
+       BT_ASSERT_DBG(bt_field_borrow_class_const(field) == fc->ir_fc);
        stack_push(notit->stack, field);
 
        /*
@@ -2230,7 +2298,7 @@ enum bt_bfcr_status bfcr_compound_begin_cb(
                struct ctf_field_class_array_base *array_fc = (void *) fc;
 
                if (array_fc->is_text) {
-                       BT_ASSERT(bt_field_get_class_type(field) ==
+                       BT_ASSERT_DBG(bt_field_get_class_type(field) ==
                                  BT_FIELD_CLASS_TYPE_STRING);
                        notit->done_filling_string = false;
                        bt_field_string_clear(field);
@@ -2243,6 +2311,7 @@ end:
        return BT_BFCR_STATUS_OK;
 }
 
+static
 enum bt_bfcr_status bfcr_compound_end_cb(
                struct ctf_field_class *fc, void *data)
 {
@@ -2253,12 +2322,12 @@ enum bt_bfcr_status bfcr_compound_end_cb(
                "fc-type=%d, fc-in-ir=%d",
                notit, notit->bfcr, fc, fc->type, fc->in_ir);
 
-       if (!fc->in_ir) {
+       if (G_UNLIKELY(!fc->in_ir || notit->dry_run)) {
                goto end;
        }
 
-       BT_ASSERT(!stack_empty(notit->stack));
-       BT_ASSERT(bt_field_borrow_class_const(stack_top(notit->stack)->base) ==
+       BT_ASSERT_DBG(!stack_empty(notit->stack));
+       BT_ASSERT_DBG(bt_field_borrow_class_const(stack_top(notit->stack)->base) ==
                fc->ir_fc);
 
        /*
@@ -2270,7 +2339,7 @@ enum bt_bfcr_status bfcr_compound_end_cb(
                struct ctf_field_class_array_base *array_fc = (void *) fc;
 
                if (array_fc->is_text) {
-                       BT_ASSERT(bt_field_get_class_type(
+                       BT_ASSERT_DBG(bt_field_get_class_type(
                                stack_top(notit->stack)->base) ==
                                BT_FIELD_CLASS_TYPE_STRING);
                        bt_bfcr_set_unsigned_int_cb(notit->bfcr,
@@ -2295,14 +2364,20 @@ int64_t bfcr_get_sequence_length_cb(struct ctf_field_class *fc, void *data)
 {
        bt_field *seq_field;
        struct bt_msg_iter *notit = data;
+       bt_self_component *self_comp = notit->self_comp;
        struct ctf_field_class_sequence *seq_fc = (void *) fc;
-       int64_t length = -1;
+       int64_t length;
        int ret;
 
        length = (uint64_t) g_array_index(notit->stored_values, uint64_t,
                seq_fc->stored_length_index);
+
+       if (G_UNLIKELY(notit->dry_run)){
+               goto end;
+       }
+
        seq_field = stack_top(notit->stack)->base;
-       BT_ASSERT(seq_field);
+       BT_ASSERT_DBG(seq_field);
 
        /*
         * bfcr_get_sequence_length_cb() also gets called back for a
@@ -2311,17 +2386,21 @@ int64_t bfcr_get_sequence_length_cb(struct ctf_field_class *fc, void *data)
         * is a sequence field.
         */
        if (!seq_fc->base.is_text) {
-               BT_ASSERT(bt_field_get_class_type(seq_field) ==
-                       BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY);
+               BT_ASSERT_DBG(bt_field_class_type_is(
+                       bt_field_get_class_type(seq_field),
+                               BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY));
                ret = bt_field_array_dynamic_set_length(seq_field,
                        (uint64_t) length);
                if (ret) {
-                       BT_COMP_LOGE("Cannot set dynamic array field's length field: "
+                       BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                               "Cannot set dynamic array field's length field: "
                                "notit-addr=%p, field-addr=%p, "
                                "length=%" PRIu64, notit, seq_field, length);
+                       length = -1;
                }
        }
 
+end:
        return length;
 }
 
@@ -2335,6 +2414,7 @@ struct ctf_field_class *bfcr_borrow_variant_selected_field_class_cb(
        struct bt_msg_iter *notit = data;
        struct ctf_field_class_variant *var_fc = (void *) fc;
        struct ctf_named_field_class *selected_option = NULL;
+       bt_self_component *self_comp = notit->self_comp;
        struct ctf_field_class *ret_fc = NULL;
        union {
                uint64_t u;
@@ -2375,25 +2455,29 @@ struct ctf_field_class *bfcr_borrow_variant_selected_field_class_cb(
        }
 
        if (option_index < 0) {
-               BT_COMP_LOGW("Cannot find variant field class's option: "
+               BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                       "Cannot find variant field class's option: "
                        "notit-addr=%p, var-fc-addr=%p, u-tag=%" PRIu64 ", "
                        "i-tag=%" PRId64, notit, var_fc, tag.u, tag.i);
+               ret_fc = NULL;
                goto end;
        }
 
        selected_option = ctf_field_class_variant_borrow_option_by_index(
                var_fc, (uint64_t) option_index);
 
-       if (selected_option->fc->in_ir) {
+       if (selected_option->fc->in_ir && !notit->dry_run) {
                bt_field *var_field = stack_top(notit->stack)->base;
 
                ret = bt_field_variant_select_option_field_by_index(
                        var_field, option_index);
                if (ret) {
-                       BT_COMP_LOGW("Cannot select variant field's option field: "
+                       BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                               "Cannot select variant field's option field: "
                                "notit-addr=%p, var-field-addr=%p, "
                                "opt-index=%" PRId64, notit, var_field,
                                option_index);
+                       ret_fc = NULL;
                        goto end;
                }
        }
@@ -2405,71 +2489,70 @@ end:
 }
 
 static
-void create_msg_stream_beginning(struct bt_msg_iter *notit,
-               bt_message **message)
+bt_message *create_msg_stream_beginning(struct bt_msg_iter *notit)
 {
-       bt_message *ret = NULL;
+       bt_self_component *self_comp = notit->self_comp;
+       bt_message *msg;
 
        BT_ASSERT(notit->stream);
        BT_ASSERT(notit->msg_iter);
-       ret = bt_message_stream_beginning_create(notit->msg_iter,
+       msg = bt_message_stream_beginning_create(notit->msg_iter,
                notit->stream);
-       if (!ret) {
-               BT_COMP_LOGE("Cannot create stream beginning message: "
+       if (!msg) {
+               BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                       "Cannot create stream beginning message: "
                        "notit-addr=%p, stream-addr=%p",
                        notit, notit->stream);
-               return;
        }
 
-       *message = ret;
+       return msg;
 }
 
 static
-void create_msg_stream_end(struct bt_msg_iter *notit, bt_message **message)
+bt_message *create_msg_stream_end(struct bt_msg_iter *notit)
 {
-       bt_message *ret;
+       bt_self_component *self_comp = notit->self_comp;
+       bt_message *msg;
 
        if (!notit->stream) {
-               BT_COMP_LOGE("Cannot create stream for stream message: "
+               BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                       "Cannot create stream end message because stream is NULL: "
                        "notit-addr=%p", notit);
-               return;
+               msg = NULL;
+               goto end;
        }
 
        BT_ASSERT(notit->msg_iter);
-       ret = bt_message_stream_end_create(notit->msg_iter,
+       msg = bt_message_stream_end_create(notit->msg_iter,
                notit->stream);
-       if (!ret) {
-               BT_COMP_LOGE("Cannot create stream end message: "
+       if (!msg) {
+               BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                       "Cannot create stream end message: "
                        "notit-addr=%p, stream-addr=%p",
                        notit, notit->stream);
-               return;
        }
 
-       *message = ret;
+end:
+       return msg;
 }
 
 static
-void create_msg_packet_beginning(struct bt_msg_iter *notit,
-               bt_message **message)
+bt_message *create_msg_packet_beginning(struct bt_msg_iter *notit,
+               bool use_default_cs)
 {
+       bt_self_component *self_comp = notit->self_comp;
        int ret;
-       enum bt_msg_iter_status status;
-       bt_message *msg = NULL;
-       const bt_stream_class *sc;
-
-       status = set_current_packet(notit);
-       if (status != BT_MSG_ITER_STATUS_OK) {
-               goto end;
-       }
+       bt_message *msg;
+       const bt_stream_class *sc = notit->meta.sc->ir_sc;
 
        BT_ASSERT(notit->packet);
-       sc = notit->meta.sc->ir_sc;
        BT_ASSERT(sc);
 
        if (notit->packet_context_field) {
                ret = bt_packet_move_context_field(
                        notit->packet, notit->packet_context_field);
                if (ret) {
+                       msg = NULL;
                        goto end;
                }
 
@@ -2489,38 +2572,117 @@ void create_msg_packet_beginning(struct bt_msg_iter *notit,
 
        if (notit->meta.sc->packets_have_ts_begin) {
                BT_ASSERT(notit->snapshots.beginning_clock != UINT64_C(-1));
+               uint64_t raw_cs_value;
+
+               /*
+                * Either use the decoded packet `timestamp_begin` field or the
+                * current stream's default clock_snapshot.
+                */
+               if (use_default_cs) {
+                       raw_cs_value = notit->default_clock_snapshot;
+               } else {
+                       raw_cs_value = notit->snapshots.beginning_clock;
+               }
+
                msg = bt_message_packet_beginning_create_with_default_clock_snapshot(
                        notit->msg_iter, notit->packet,
-                       notit->snapshots.beginning_clock);
+                       raw_cs_value);
        } else {
                msg = bt_message_packet_beginning_create(notit->msg_iter,
                        notit->packet);
        }
 
        if (!msg) {
-               BT_COMP_LOGE("Cannot create packet beginning message: "
+               BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                       "Cannot create packet beginning message: "
                        "notit-addr=%p, packet-addr=%p",
                        notit, notit->packet);
                goto end;
        }
 
-       *message = msg;
-
 end:
-       return;
+       return msg;
+}
+
+static
+bt_message *emit_delayed_packet_beg_msg(struct bt_msg_iter *notit)
+{
+       bool packet_beg_ts_need_fix_up;
+
+       notit->emit_delayed_packet_beginning_msg = false;
+
+       /*
+        * Only fix the packet's timestamp_begin if it's larger than the first
+        * event of the packet. If there was no event in the packet, the
+        * `default_clock_snapshot` field will be either equal or greater than
+        * `snapshots.beginning_clock` so there is not fix needed.
+        */
+       packet_beg_ts_need_fix_up =
+               notit->default_clock_snapshot < notit->snapshots.beginning_clock;
+
+       /* create_msg_packet_beginning() logs errors */
+       return create_msg_packet_beginning(notit, packet_beg_ts_need_fix_up);
 }
 
+
 static
-void create_msg_packet_end(struct bt_msg_iter *notit, bt_message **message)
+bt_message *create_msg_packet_end(struct bt_msg_iter *notit)
 {
        bt_message *msg;
+       bool update_default_cs = true;
+       bt_self_component *self_comp = notit->self_comp;
 
        if (!notit->packet) {
-               return;
+               msg = NULL;
+               goto end;
+       }
+
+       /*
+        * Check if we need to emit the delayed packet
+        * beginning message instead of the packet end message.
+        */
+       if (G_UNLIKELY(notit->emit_delayed_packet_beginning_msg)) {
+               msg = emit_delayed_packet_beg_msg(notit);
+               /* Don't forget to emit the packet end message. */
+               notit->state = STATE_EMIT_QUEUED_MSG_PACKET_END;
+               goto end;
+       }
+
+       /* Check if may be affected by lttng-crash timestamp_end quirk. */
+       if (G_UNLIKELY(notit->meta.tc->quirks.lttng_crash)) {
+               /*
+                * Check if the `timestamp_begin` field is non-zero but
+                * `timestamp_end` is zero. It means the trace is affected by
+                * the lttng-crash packet `timestamp_end` quirk and must be
+                * fixed up by omitting to update the default clock snapshot to
+                * the `timestamp_end` as is typically done.
+                */
+               if (notit->snapshots.beginning_clock != 0 &&
+                               notit->snapshots.end_clock == 0) {
+                       update_default_cs = false;
+               }
+       }
+
+       /*
+        * Check if may be affected by lttng event-after-packet `timestamp_end`
+        * quirk.
+        */
+       if (notit->meta.tc->quirks.lttng_event_after_packet) {
+               /*
+                * Check if `timestamp_end` is smaller then the current
+                * default_clock_snapshot (which is set to the last event
+                * decoded). It means the trace is affected by the lttng
+                * `event-after-packet` packet `timestamp_end` quirk and must
+                * be fixed up by omitting to update the default clock snapshot
+                * to the `timestamp_end` as is typically done.
+                */
+               if (notit->snapshots.end_clock < notit->default_clock_snapshot) {
+                       update_default_cs = false;
+               }
        }
 
-       /* Update default clock from packet's end time */
-       if (notit->snapshots.end_clock != UINT64_C(-1)) {
+       /* Update default clock from packet's end time. */
+       if (notit->snapshots.end_clock != UINT64_C(-1) && update_default_cs) {
                notit->default_clock_snapshot = notit->snapshots.end_clock;
        }
 
@@ -2530,29 +2692,32 @@ void create_msg_packet_end(struct bt_msg_iter *notit, bt_message **message)
                BT_ASSERT(notit->snapshots.end_clock != UINT64_C(-1));
                msg = bt_message_packet_end_create_with_default_clock_snapshot(
                        notit->msg_iter, notit->packet,
-                       notit->snapshots.end_clock);
+                       notit->default_clock_snapshot);
        } else {
                msg = bt_message_packet_end_create(notit->msg_iter,
                        notit->packet);
        }
 
        if (!msg) {
-               BT_COMP_LOGE("Cannot create packet end message: "
+               BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                       "Cannot create packet end message: "
                        "notit-addr=%p, packet-addr=%p",
                        notit, notit->packet);
-               return;
+               goto end;
 
        }
 
        BT_PACKET_PUT_REF_AND_RESET(notit->packet);
-       *message = msg;
+
+end:
+       return msg;
 }
 
 static
-void create_msg_discarded_events(struct bt_msg_iter *notit,
-               bt_message **message)
+bt_message *create_msg_discarded_events(struct bt_msg_iter *notit)
 {
        bt_message *msg;
+       bt_self_component *self_comp = notit->self_comp;
        uint64_t beginning_raw_value = UINT64_C(-1);
        uint64_t end_raw_value = UINT64_C(-1);
 
@@ -2585,10 +2750,11 @@ void create_msg_discarded_events(struct bt_msg_iter *notit,
        }
 
        if (!msg) {
-               BT_COMP_LOGE("Cannot create discarded events message: "
+               BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                       "Cannot create discarded events message: "
                        "notit-addr=%p, stream-addr=%p",
                        notit, notit->stream);
-               return;
+               goto end;
        }
 
        if (notit->prev_packet_snapshots.discarded_events != UINT64_C(-1)) {
@@ -2597,14 +2763,15 @@ void create_msg_discarded_events(struct bt_msg_iter *notit,
                        notit->prev_packet_snapshots.discarded_events);
        }
 
-       *message = msg;
+end:
+       return msg;
 }
 
 static
-void create_msg_discarded_packets(struct bt_msg_iter *notit,
-               bt_message **message)
+bt_message *create_msg_discarded_packets(struct bt_msg_iter *notit)
 {
        bt_message *msg;
+       bt_self_component *self_comp = notit->self_comp;
 
        BT_ASSERT(notit->msg_iter);
        BT_ASSERT(notit->stream);
@@ -2625,16 +2792,19 @@ void create_msg_discarded_packets(struct bt_msg_iter *notit,
        }
 
        if (!msg) {
-               BT_COMP_LOGE("Cannot create discarded packets message: "
+               BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                       "Cannot create discarded packets message: "
                        "notit-addr=%p, stream-addr=%p",
                        notit, notit->stream);
-               return;
+               goto end;
        }
 
        bt_message_discarded_packets_set_count(msg,
                notit->snapshots.packets -
                        notit->prev_packet_snapshots.packets - 1);
-       *message = msg;
+
+end:
+       return msg;
 }
 
 BT_HIDDEN
@@ -2686,13 +2856,15 @@ struct bt_msg_iter *bt_msg_iter_create(struct ctf_trace_class *tc,
        g_array_set_size(notit->stored_values, tc->stored_value_count);
 
        if (!notit->stack) {
-               BT_COMP_LOGE_STR("Failed to create field stack.");
+               BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                       "Failed to create field stack.");
                goto error;
        }
 
        notit->bfcr = bt_bfcr_create(cbs, notit, log_level, NULL);
        if (!notit->bfcr) {
-               BT_COMP_LOGE_STR("Failed to create binary class reader (BFCR).");
+               BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                       "Failed to create binary class reader (BFCR).");
                goto error;
        }
 
@@ -2743,9 +2915,10 @@ enum bt_msg_iter_status bt_msg_iter_get_next_message(
                bt_self_message_iterator *msg_iter, bt_message **message)
 {
        enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
+       bt_self_component *self_comp = notit->self_comp;
 
-       BT_ASSERT(notit);
-       BT_ASSERT(message);
+       BT_ASSERT_DBG(notit);
+       BT_ASSERT_DBG(message);
        notit->msg_iter = msg_iter;
        notit->set_stream = true;
        BT_COMP_LOGD("Getting next message: notit-addr=%p", notit);
@@ -2756,20 +2929,40 @@ enum bt_msg_iter_status bt_msg_iter_get_next_message(
                        BT_COMP_LOGD_STR("Medium returned BT_MSG_ITER_STATUS_AGAIN.");
                        goto end;
                } else if (G_UNLIKELY(status != BT_MSG_ITER_STATUS_OK)) {
-                       BT_COMP_LOGW("Cannot handle state: notit-addr=%p, state=%s",
+                       BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                               "Cannot handle state: notit-addr=%p, state=%s",
                                notit, state_string(notit->state));
                        goto end;
                }
 
                switch (notit->state) {
                case STATE_EMIT_MSG_EVENT:
-                       BT_ASSERT(notit->event_msg);
-                       *message = notit->event_msg;
-                       notit->event_msg = NULL;
+                       BT_ASSERT_DBG(notit->event_msg);
+
+                       /*
+                        * Check if we need to emit the delayed packet
+                        * beginning message instead of the event message.
+                        */
+                       if (G_UNLIKELY(notit->emit_delayed_packet_beginning_msg)) {
+                               *message = emit_delayed_packet_beg_msg(notit);
+                               if (!*message) {
+                                       status = BT_MSG_ITER_STATUS_ERROR;
+                               }
+
+                               /*
+                                * Don't forget to emit the event message of
+                                * the event record that was just decoded.
+                                */
+                               notit->state = STATE_EMIT_QUEUED_MSG_EVENT;
+
+                       } else {
+                               *message = notit->event_msg;
+                               notit->event_msg = NULL;
+                       }
                        goto end;
                case STATE_EMIT_MSG_DISCARDED_EVENTS:
                        /* create_msg_discared_events() logs errors */
-                       create_msg_discarded_events(notit, message);
+                       *message = create_msg_discarded_events(notit);
 
                        if (!*message) {
                                status = BT_MSG_ITER_STATUS_ERROR;
@@ -2778,7 +2971,7 @@ enum bt_msg_iter_status bt_msg_iter_get_next_message(
                        goto end;
                case STATE_EMIT_MSG_DISCARDED_PACKETS:
                        /* create_msg_discared_packets() logs errors */
-                       create_msg_discarded_packets(notit, message);
+                       *message = create_msg_discarded_packets(notit);
 
                        if (!*message) {
                                status = BT_MSG_ITER_STATUS_ERROR;
@@ -2786,18 +2979,33 @@ enum bt_msg_iter_status bt_msg_iter_get_next_message(
 
                        goto end;
                case STATE_EMIT_MSG_PACKET_BEGINNING:
-                       /* create_msg_packet_beginning() logs errors */
-                       create_msg_packet_beginning(notit, message);
+                       status = set_current_packet(notit);
+                       if (status != BT_MSG_ITER_STATUS_OK) {
+                               goto end;
+                       }
 
-                       if (!*message) {
-                               status = BT_MSG_ITER_STATUS_ERROR;
+                       if (G_UNLIKELY(notit->meta.tc->quirks.barectf_event_before_packet)) {
+                               notit->emit_delayed_packet_beginning_msg = true;
+                               /*
+                                * There is no message to return yet as this
+                                * packet beginning message is delayed until we
+                                * decode the first event message of the
+                                * packet.
+                                */
+                               break;
+                       } else {
+                               /* create_msg_packet_beginning() logs errors */
+                               *message = create_msg_packet_beginning(notit, false);
+                               if (!*message) {
+                                       status = BT_MSG_ITER_STATUS_ERROR;
+                               }
                        }
 
                        goto end;
                case STATE_EMIT_MSG_PACKET_END_SINGLE:
                case STATE_EMIT_MSG_PACKET_END_MULTI:
                        /* create_msg_packet_end() logs errors */
-                       create_msg_packet_end(notit, message);
+                       *message = create_msg_packet_end(notit);
 
                        if (!*message) {
                                status = BT_MSG_ITER_STATUS_ERROR;
@@ -2806,7 +3014,7 @@ enum bt_msg_iter_status bt_msg_iter_get_next_message(
                        goto end;
                case STATE_EMIT_MSG_STREAM_BEGINNING:
                        /* create_msg_stream_beginning() logs errors */
-                       create_msg_stream_beginning(notit, message);
+                       *message = create_msg_stream_beginning(notit);
 
                        if (!*message) {
                                status = BT_MSG_ITER_STATUS_ERROR;
@@ -2815,7 +3023,7 @@ enum bt_msg_iter_status bt_msg_iter_get_next_message(
                        goto end;
                case STATE_EMIT_MSG_STREAM_END:
                        /* create_msg_stream_end() logs errors */
-                       create_msg_stream_end(notit, message);
+                       *message = create_msg_stream_end(notit);
 
                        if (!*message) {
                                status = BT_MSG_ITER_STATUS_ERROR;
@@ -2836,39 +3044,39 @@ end:
 }
 
 static
-enum bt_msg_iter_status read_packet_header_context_fields(
-               struct bt_msg_iter *notit)
+enum bt_msg_iter_status decode_until_state( struct bt_msg_iter *notit,
+               enum state target_state_1, enum state target_state_2)
 {
-       int ret;
        enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
+       bt_self_component *self_comp = notit->self_comp;
 
-       BT_ASSERT(notit);
+       BT_ASSERT_DBG(notit);
        notit->set_stream = false;
 
-       if (notit->state == STATE_EMIT_MSG_PACKET_BEGINNING) {
-               /* We're already there */
-               goto end;
-       }
+       do {
+               /*
+                * Check if we reached the state at which we want to stop
+                * decoding.
+                */
+               if (notit->state == target_state_1 ||
+                               notit->state == target_state_2) {
+                       goto end;
+               }
 
-       while (true) {
                status = handle_state(notit);
                if (G_UNLIKELY(status == BT_MSG_ITER_STATUS_AGAIN)) {
                        BT_COMP_LOGD_STR("Medium returned BT_MSG_ITER_STATUS_AGAIN.");
                        goto end;
                } else if (G_UNLIKELY(status != BT_MSG_ITER_STATUS_OK)) {
-                       BT_COMP_LOGW("Cannot handle state: notit-addr=%p, state=%s",
+                       BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                               "Cannot handle state: notit-addr=%p, state=%s",
                                notit, state_string(notit->state));
                        goto end;
                }
 
                switch (notit->state) {
-               case STATE_EMIT_MSG_PACKET_BEGINNING:
-                       /*
-                        * Packet header and context fields are
-                        * potentially decoded (or they don't exist).
-                        */
-                       goto end;
                case STATE_INIT:
+               case STATE_SWITCH_PACKET:
                case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
                case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
                case STATE_AFTER_TRACE_PACKET_HEADER:
@@ -2881,25 +3089,58 @@ enum bt_msg_iter_status read_packet_header_context_fields(
                case STATE_EMIT_MSG_DISCARDED_EVENTS:
                case STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS:
                case STATE_EMIT_MSG_DISCARDED_PACKETS:
-                       /* Non-emitting state: continue */
+               case STATE_EMIT_MSG_PACKET_BEGINNING:
+               case STATE_DSCOPE_EVENT_HEADER_BEGIN:
+               case STATE_DSCOPE_EVENT_HEADER_CONTINUE:
+               case STATE_AFTER_EVENT_HEADER:
+               case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN:
+               case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE:
+               case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN:
+               case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE:
+               case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN:
+               case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE:
+               case STATE_EMIT_MSG_EVENT:
+               case STATE_EMIT_QUEUED_MSG_EVENT:
+               case STATE_SKIP_PACKET_PADDING:
+               case STATE_EMIT_MSG_PACKET_END_MULTI:
+               case STATE_EMIT_MSG_PACKET_END_SINGLE:
+               case STATE_EMIT_QUEUED_MSG_PACKET_END:
+               case STATE_CHECK_EMIT_MSG_STREAM_END:
+               case STATE_EMIT_MSG_STREAM_END:
                        break;
+               case STATE_DONE:
+                       /* fall-through */
                default:
-                       /*
-                        * We should never get past the
-                        * STATE_EMIT_MSG_PACKET_BEGINNING state.
-                        */
+                       /* We should never get to the STATE_DONE state. */
                        BT_COMP_LOGF("Unexpected state: notit-addr=%p, state=%s",
                                notit, state_string(notit->state));
                        abort();
                }
-       }
+       } while (true);
 
 end:
+       return status;
+}
+
+static
+enum bt_msg_iter_status read_packet_header_context_fields(
+               struct bt_msg_iter *notit)
+{
+       int ret;
+       enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
+
+       status = decode_until_state(notit, STATE_EMIT_MSG_PACKET_BEGINNING, -1);
+       if (status != BT_MSG_ITER_STATUS_OK) {
+               goto end;
+       }
+
        ret = set_current_packet_content_sizes(notit);
        if (ret) {
                status = BT_MSG_ITER_STATUS_ERROR;
+               goto end;
        }
 
+end:
        return status;
 }
 
@@ -2915,18 +3156,21 @@ BT_HIDDEN
 enum bt_msg_iter_status bt_msg_iter_seek(struct bt_msg_iter *notit,
                off_t offset)
 {
-       enum bt_msg_iter_status ret = BT_MSG_ITER_STATUS_OK;
+       enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
+       bt_self_component *self_comp = notit->self_comp;
        enum bt_msg_iter_medium_status medium_status;
 
        BT_ASSERT(notit);
        if (offset < 0) {
-               BT_COMP_LOGE("Cannot seek to negative offset: offset=%jd", (intmax_t) offset);
-               ret = BT_MSG_ITER_STATUS_INVAL;
+               BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                       "Cannot seek to negative offset: offset=%jd",
+                       (intmax_t) offset);
+               status = BT_MSG_ITER_STATUS_INVAL;
                goto end;
        }
 
        if (!notit->medium.medops.seek) {
-               ret = BT_MSG_ITER_STATUS_UNSUPPORTED;
+               status = BT_MSG_ITER_STATUS_UNSUPPORTED;
                BT_COMP_LOGD("Aborting seek as the iterator's underlying media does not implement seek support.");
                goto end;
        }
@@ -2935,9 +3179,9 @@ enum bt_msg_iter_status bt_msg_iter_seek(struct bt_msg_iter *notit,
                BT_MSG_ITER_SEEK_WHENCE_SET, offset, notit->medium.data);
        if (medium_status != BT_MSG_ITER_MEDIUM_STATUS_OK) {
                if (medium_status == BT_MSG_ITER_MEDIUM_STATUS_EOF) {
-                       ret = BT_MSG_ITER_STATUS_EOF;
+                       status = BT_MSG_ITER_STATUS_EOF;
                } else {
-                       ret = BT_MSG_ITER_STATUS_ERROR;
+                       status = BT_MSG_ITER_STATUS_ERROR;
                        goto end;
                }
        }
@@ -2946,7 +3190,43 @@ enum bt_msg_iter_status bt_msg_iter_seek(struct bt_msg_iter *notit,
        notit->cur_packet_offset = offset;
 
 end:
-       return ret;
+       return status;
+}
+
+static
+enum bt_msg_iter_status clock_snapshot_at_msg_iter_state(
+               struct bt_msg_iter *notit, enum state target_state_1,
+               enum state target_state_2, uint64_t *clock_snapshot)
+{
+       enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
+
+       BT_ASSERT_DBG(notit);
+       BT_ASSERT_DBG(clock_snapshot);
+       status = decode_until_state(notit, target_state_1, target_state_2);
+       if (status != BT_MSG_ITER_STATUS_OK) {
+               goto end;
+       }
+
+       *clock_snapshot = notit->default_clock_snapshot;
+end:
+       return status;
+}
+
+BT_HIDDEN
+enum bt_msg_iter_status bt_msg_iter_curr_packet_first_event_clock_snapshot(
+               struct bt_msg_iter *notit, uint64_t *first_clock_snapshot)
+{
+       return clock_snapshot_at_msg_iter_state(notit,
+               STATE_AFTER_EVENT_HEADER, -1, first_clock_snapshot);
+}
+
+BT_HIDDEN
+enum bt_msg_iter_status bt_msg_iter_curr_packet_last_event_clock_snapshot(
+               struct bt_msg_iter *notit, uint64_t *last_clock_snapshot)
+{
+       return clock_snapshot_at_msg_iter_state(notit,
+               STATE_EMIT_MSG_PACKET_END_SINGLE,
+               STATE_EMIT_MSG_PACKET_END_MULTI, last_clock_snapshot);
 }
 
 BT_HIDDEN
@@ -2956,8 +3236,8 @@ enum bt_msg_iter_status bt_msg_iter_get_packet_properties(
 {
        enum bt_msg_iter_status status;
 
-       BT_ASSERT(notit);
-       BT_ASSERT(props);
+       BT_ASSERT_DBG(notit);
+       BT_ASSERT_DBG(props);
        status = read_packet_header_context_fields(notit);
        if (status != BT_MSG_ITER_STATUS_OK) {
                goto end;
@@ -2989,3 +3269,10 @@ void bt_msg_iter_set_emit_stream_end_message(struct bt_msg_iter *notit,
 {
        notit->emit_stream_end_msg = val;
 }
+
+BT_HIDDEN
+void bt_msg_iter_set_dry_run(struct bt_msg_iter *notit,
+               bool val)
+{
+       notit->dry_run = val;
+}
This page took 0.049654 seconds and 4 git commands to generate.