2 * SPDX-License-Identifier: MIT
4 * Copyright (c) 2015-2018 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2015-2018 Philippe Proulx <pproulx@efficios.com>
7 * Babeltrace - CTF message iterator
16 #include <babeltrace2/babeltrace.h>
18 #define BT_COMP_LOG_SELF_COMP (msg_it->self_comp)
19 #define BT_LOG_OUTPUT_LEVEL (msg_it->log_level)
20 #define BT_LOG_TAG "PLUGIN/CTF/MSG-ITER"
21 #include "logging/comp-logging.h"
23 #include "common/assert.h"
24 #include "common/common.h"
26 #include "../bfcr/bfcr.hpp"
27 #include "msg-iter.hpp"
28 #include "plugins/ctf/common/metadata/ctf-meta.hpp"
30 /* A visit stack entry */
34 * Current base field, one of:
46 /* Index of next field to set */
53 struct ctf_msg_iter
*msg_it
;
55 /* Entries (struct stack_entry) */
58 /* Number of active entries */
67 STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
,
68 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
69 STATE_AFTER_TRACE_PACKET_HEADER
,
70 STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
,
71 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
72 STATE_AFTER_STREAM_PACKET_CONTEXT
,
73 STATE_EMIT_MSG_STREAM_BEGINNING
,
74 STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS
,
75 STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
,
76 STATE_EMIT_MSG_DISCARDED_EVENTS
,
77 STATE_EMIT_MSG_DISCARDED_PACKETS
,
78 STATE_EMIT_MSG_PACKET_BEGINNING
,
79 STATE_DSCOPE_EVENT_HEADER_BEGIN
,
80 STATE_DSCOPE_EVENT_HEADER_CONTINUE
,
81 STATE_AFTER_EVENT_HEADER
,
82 STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
,
83 STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
,
84 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
,
85 STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
,
86 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
87 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
89 STATE_EMIT_QUEUED_MSG_EVENT
,
90 STATE_SKIP_PACKET_PADDING
,
91 STATE_EMIT_MSG_PACKET_END_MULTI
,
92 STATE_EMIT_MSG_PACKET_END_SINGLE
,
93 STATE_EMIT_QUEUED_MSG_PACKET_END
,
94 STATE_CHECK_EMIT_MSG_STREAM_END
,
95 STATE_EMIT_MSG_STREAM_END
,
99 struct end_of_packet_snapshots
101 uint64_t discarded_events
;
103 uint64_t beginning_clock
;
107 /* CTF message iterator */
113 /* Current message iterator to create messages (weak) */
114 bt_self_message_iterator
*self_msg_iter
;
117 * True if library objects are unavailable during the decoding and
118 * should not be created/used.
123 * Current dynamic scope field pointer.
125 * This is set by read_dscope_begin_state() and contains the
126 * value of one of the pointers in `dscopes` below.
128 bt_field
*cur_dscope_field
;
131 * True if we're done filling a string field from a text
132 * array/sequence payload.
134 bool done_filling_string
;
136 /* Trace and classes */
137 /* True to set IR fields */
142 struct ctf_trace_class
*tc
;
143 struct ctf_stream_class
*sc
;
144 struct ctf_event_class
*ec
;
147 /* Current packet (NULL if not created yet) */
150 /* Current stream (NULL if not set yet) */
153 /* Current event (NULL if not created yet) */
156 /* Current event message (NULL if not created yet) */
157 bt_message
*event_msg
;
160 * True if we need to emit a packet beginning message before we emit
161 * the next event message or the packet end message.
163 bool emit_delayed_packet_beginning_msg
;
166 * True if this is the first packet we are reading, and therefore if we
167 * should emit a stream beginning message.
169 bool emit_stream_beginning_message
;
172 * True if we need to emit a stream end message at the end of the
173 * current stream. A live stream may never receive any data and thus
174 * never send a stream beginning message which removes the need to emit
175 * a stream end message.
177 bool emit_stream_end_message
;
179 /* Database of current dynamic scopes */
182 bt_field
*stream_packet_context
;
183 bt_field
*event_common_context
;
184 bt_field
*event_spec_context
;
185 bt_field
*event_payload
;
191 /* Current medium buffer data */
194 /* Last address provided by medium */
197 /* Buffer size provided by medium (bytes) */
200 /* Offset within whole packet of addr (bits) */
201 size_t packet_offset
;
203 /* Current position from addr (bits) */
206 /* Position of the last event header from addr (bits) */
210 /* Binary type reader */
211 struct bt_bfcr
*bfcr
;
213 /* Current medium data */
216 struct ctf_msg_iter_medium_ops medops
;
217 size_t max_request_sz
;
221 /* Current packet size (bits) (-1 if unknown) */
222 int64_t cur_exp_packet_total_size
;
224 /* Current content size (bits) (-1 if unknown) */
225 int64_t cur_exp_packet_content_size
;
227 /* Current stream class ID */
228 int64_t cur_stream_class_id
;
230 /* Current event class ID */
231 int64_t cur_event_class_id
;
233 /* Current data stream ID */
234 int64_t cur_data_stream_id
;
237 * Offset, in the underlying media, of the current packet's
238 * start (-1 if unknown).
240 off_t cur_packet_offset
;
242 /* Default clock's current value */
243 uint64_t default_clock_snapshot
;
245 /* End of current packet snapshots */
246 struct end_of_packet_snapshots snapshots
;
248 /* End of previous packet snapshots */
249 struct end_of_packet_snapshots prev_packet_snapshots
;
251 /* Stored values (for sequence lengths, variant tags) */
252 GArray
*stored_values
;
254 /* Iterator's current log level */
255 bt_logging_level log_level
;
257 /* Iterator's owning self component, or `NULL` if none (query) */
258 bt_self_component
*self_comp
;
261 static inline const char *state_string(enum state state
)
266 case STATE_SWITCH_PACKET
:
267 return "SWITCH_PACKET";
268 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
269 return "DSCOPE_TRACE_PACKET_HEADER_BEGIN";
270 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
271 return "DSCOPE_TRACE_PACKET_HEADER_CONTINUE";
272 case STATE_AFTER_TRACE_PACKET_HEADER
:
273 return "AFTER_TRACE_PACKET_HEADER";
274 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
275 return "DSCOPE_STREAM_PACKET_CONTEXT_BEGIN";
276 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
277 return "DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE";
278 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
279 return "AFTER_STREAM_PACKET_CONTEXT";
280 case STATE_EMIT_MSG_STREAM_BEGINNING
:
281 return "EMIT_MSG_STREAM_BEGINNING";
282 case STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS
:
283 return "CHECK_EMIT_MSG_DISCARDED_EVENTS";
284 case STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
:
285 return "CHECK_EMIT_MSG_DISCARDED_PACKETS";
286 case STATE_EMIT_MSG_PACKET_BEGINNING
:
287 return "EMIT_MSG_PACKET_BEGINNING";
288 case STATE_EMIT_MSG_DISCARDED_EVENTS
:
289 return "EMIT_MSG_DISCARDED_EVENTS";
290 case STATE_EMIT_MSG_DISCARDED_PACKETS
:
291 return "EMIT_MSG_DISCARDED_PACKETS";
292 case STATE_DSCOPE_EVENT_HEADER_BEGIN
:
293 return "DSCOPE_EVENT_HEADER_BEGIN";
294 case STATE_DSCOPE_EVENT_HEADER_CONTINUE
:
295 return "DSCOPE_EVENT_HEADER_CONTINUE";
296 case STATE_AFTER_EVENT_HEADER
:
297 return "AFTER_EVENT_HEADER";
298 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
:
299 return "DSCOPE_EVENT_COMMON_CONTEXT_BEGIN";
300 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
:
301 return "DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE";
302 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
:
303 return "DSCOPE_EVENT_SPEC_CONTEXT_BEGIN";
304 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
:
305 return "DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE";
306 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
307 return "DSCOPE_EVENT_PAYLOAD_BEGIN";
308 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
309 return "DSCOPE_EVENT_PAYLOAD_CONTINUE";
310 case STATE_EMIT_MSG_EVENT
:
311 return "EMIT_MSG_EVENT";
312 case STATE_EMIT_QUEUED_MSG_EVENT
:
313 return "EMIT_QUEUED_MSG_EVENT";
314 case STATE_SKIP_PACKET_PADDING
:
315 return "SKIP_PACKET_PADDING";
316 case STATE_EMIT_MSG_PACKET_END_MULTI
:
317 return "EMIT_MSG_PACKET_END_MULTI";
318 case STATE_EMIT_MSG_PACKET_END_SINGLE
:
319 return "EMIT_MSG_PACKET_END_SINGLE";
320 case STATE_EMIT_QUEUED_MSG_PACKET_END
:
321 return "EMIT_QUEUED_MSG_PACKET_END";
322 case STATE_CHECK_EMIT_MSG_STREAM_END
:
323 return "CHECK_EMIT_MSG_STREAM_END";
324 case STATE_EMIT_MSG_STREAM_END
:
325 return "EMIT_MSG_STREAM_END";
333 static struct stack
*stack_new(struct ctf_msg_iter
*msg_it
)
335 bt_self_component
*self_comp
= msg_it
->self_comp
;
336 struct stack
*stack
= NULL
;
338 stack
= g_new0(struct stack
, 1);
340 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to allocate one stack.");
344 stack
->msg_it
= msg_it
;
345 stack
->entries
= g_array_new(FALSE
, TRUE
, sizeof(struct stack_entry
));
346 if (!stack
->entries
) {
347 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to allocate a GArray.");
351 BT_COMP_LOGD("Created stack: msg-it-addr=%p, stack-addr=%p", msg_it
, stack
);
362 static void stack_destroy(struct stack
*stack
)
364 struct ctf_msg_iter
*msg_it
;
366 BT_ASSERT_DBG(stack
);
367 msg_it
= stack
->msg_it
;
368 BT_COMP_LOGD("Destroying stack: addr=%p", stack
);
370 if (stack
->entries
) {
371 g_array_free(stack
->entries
, TRUE
);
377 static void stack_push(struct stack
*stack
, bt_field
*base
)
379 struct stack_entry
*entry
;
380 struct ctf_msg_iter
*msg_it
;
382 BT_ASSERT_DBG(stack
);
383 msg_it
= stack
->msg_it
;
385 BT_COMP_LOGT("Pushing base field on stack: stack-addr=%p, "
386 "stack-size-before=%zu, stack-size-after=%zu",
387 stack
, stack
->size
, stack
->size
+ 1);
389 if (stack
->entries
->len
== stack
->size
) {
390 g_array_set_size(stack
->entries
, stack
->size
+ 1);
393 entry
= &bt_g_array_index(stack
->entries
, struct stack_entry
, stack
->size
);
399 static inline unsigned int stack_size(struct stack
*stack
)
401 BT_ASSERT_DBG(stack
);
405 static void stack_pop(struct stack
*stack
)
407 struct ctf_msg_iter
*msg_it
;
409 BT_ASSERT_DBG(stack
);
410 BT_ASSERT_DBG(stack_size(stack
));
411 msg_it
= stack
->msg_it
;
412 BT_COMP_LOGT("Popping from stack: "
413 "stack-addr=%p, stack-size-before=%zu, stack-size-after=%zu",
414 stack
, stack
->size
, stack
->size
- 1);
418 static inline struct stack_entry
*stack_top(struct stack
*stack
)
420 BT_ASSERT_DBG(stack
);
421 BT_ASSERT_DBG(stack_size(stack
));
422 return &bt_g_array_index(stack
->entries
, struct stack_entry
, stack
->size
- 1);
425 static inline bool stack_empty(struct stack
*stack
)
427 return stack_size(stack
) == 0;
430 static void stack_clear(struct stack
*stack
)
432 BT_ASSERT_DBG(stack
);
436 static inline enum ctf_msg_iter_status
437 msg_iter_status_from_m_status(enum ctf_msg_iter_medium_status m_status
)
439 /* They are the same */
440 return (ctf_msg_iter_status
) m_status
;
443 static inline size_t buf_size_bits(struct ctf_msg_iter
*msg_it
)
445 return msg_it
->buf
.sz
* 8;
448 static inline size_t buf_available_bits(struct ctf_msg_iter
*msg_it
)
450 return buf_size_bits(msg_it
) - msg_it
->buf
.at
;
453 static inline size_t packet_at(struct ctf_msg_iter
*msg_it
)
455 return msg_it
->buf
.packet_offset
+ msg_it
->buf
.at
;
458 static inline void buf_consume_bits(struct ctf_msg_iter
*msg_it
, size_t incr
)
460 BT_COMP_LOGT("Advancing cursor: msg-it-addr=%p, cur-before=%zu, cur-after=%zu", msg_it
,
461 msg_it
->buf
.at
, msg_it
->buf
.at
+ incr
);
462 msg_it
->buf
.at
+= incr
;
465 static enum ctf_msg_iter_status
request_medium_bytes(struct ctf_msg_iter
*msg_it
)
467 bt_self_component
*self_comp
= msg_it
->self_comp
;
468 uint8_t *buffer_addr
= NULL
;
469 size_t buffer_sz
= 0;
470 enum ctf_msg_iter_medium_status m_status
;
472 BT_COMP_LOGD("Calling user function (request bytes): msg-it-addr=%p, "
474 msg_it
, msg_it
->medium
.max_request_sz
);
475 m_status
= msg_it
->medium
.medops
.request_bytes(msg_it
->medium
.max_request_sz
, &buffer_addr
,
476 &buffer_sz
, msg_it
->medium
.data
);
477 BT_COMP_LOGD("User function returned: status=%s, buf-addr=%p, buf-size=%zu",
478 ctf_msg_iter_medium_status_string(m_status
), buffer_addr
, buffer_sz
);
479 if (m_status
== CTF_MSG_ITER_MEDIUM_STATUS_OK
) {
480 BT_ASSERT(buffer_sz
!= 0);
482 /* New packet offset is old one + old size (in bits) */
483 msg_it
->buf
.packet_offset
+= buf_size_bits(msg_it
);
485 /* Restart at the beginning of the new medium buffer */
487 msg_it
->buf
.last_eh_at
= SIZE_MAX
;
489 /* New medium buffer size */
490 msg_it
->buf
.sz
= buffer_sz
;
492 /* New medium buffer address */
493 msg_it
->buf
.addr
= buffer_addr
;
495 BT_COMP_LOGD("User function returned new bytes: "
496 "packet-offset=%zu, cur=%zu, size=%zu, addr=%p",
497 msg_it
->buf
.packet_offset
, msg_it
->buf
.at
, msg_it
->buf
.sz
, msg_it
->buf
.addr
);
498 BT_COMP_LOGT_MEM(buffer_addr
, buffer_sz
, "Returned bytes at %p:", buffer_addr
);
499 } else if (m_status
== CTF_MSG_ITER_MEDIUM_STATUS_EOF
) {
501 * User returned end of stream: validate that we're not
502 * in the middle of a packet header, packet context, or
505 if (msg_it
->cur_exp_packet_total_size
>= 0) {
506 if (packet_at(msg_it
) == msg_it
->cur_exp_packet_total_size
) {
510 if (packet_at(msg_it
) == 0) {
514 if (msg_it
->buf
.last_eh_at
!= SIZE_MAX
&& msg_it
->buf
.at
== msg_it
->buf
.last_eh_at
) {
519 /* All other states are invalid */
520 BT_COMP_LOGE_APPEND_CAUSE(
522 "User function returned %s, but message iterator is in an unexpected state: "
523 "state=%s, cur-packet-size=%" PRId64
", cur=%zu, "
524 "packet-cur=%zu, last-eh-at=%zu",
525 ctf_msg_iter_medium_status_string(m_status
), state_string(msg_it
->state
),
526 msg_it
->cur_exp_packet_total_size
, msg_it
->buf
.at
, packet_at(msg_it
),
527 msg_it
->buf
.last_eh_at
);
528 m_status
= CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
529 } else if (m_status
< 0) {
530 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
531 "User function failed: "
533 ctf_msg_iter_medium_status_string(m_status
));
537 return msg_iter_status_from_m_status(m_status
);
540 static inline enum ctf_msg_iter_status
buf_ensure_available_bits(struct ctf_msg_iter
*msg_it
)
542 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
544 if (G_UNLIKELY(buf_available_bits(msg_it
) == 0)) {
546 * This _cannot_ return CTF_MSG_ITER_STATUS_OK
549 status
= request_medium_bytes(msg_it
);
555 static enum ctf_msg_iter_status
556 read_dscope_begin_state(struct ctf_msg_iter
*msg_it
, struct ctf_field_class
*dscope_fc
,
557 enum state done_state
, enum state continue_state
, bt_field
*dscope_field
)
559 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
560 bt_self_component
*self_comp
= msg_it
->self_comp
;
561 enum bt_bfcr_status bfcr_status
;
562 size_t consumed_bits
;
564 msg_it
->cur_dscope_field
= dscope_field
;
565 BT_COMP_LOGT("Starting BFCR: msg-it-addr=%p, bfcr-addr=%p, fc-addr=%p", msg_it
, msg_it
->bfcr
,
567 consumed_bits
= bt_bfcr_start(msg_it
->bfcr
, dscope_fc
, msg_it
->buf
.addr
, msg_it
->buf
.at
,
568 packet_at(msg_it
), msg_it
->buf
.sz
, &bfcr_status
);
569 BT_COMP_LOGT("BFCR consumed bits: size=%zu", consumed_bits
);
571 switch (bfcr_status
) {
572 case BT_BFCR_STATUS_OK
:
573 /* Field class was read completely */
574 BT_COMP_LOGT_STR("Field was completely decoded.");
575 msg_it
->state
= done_state
;
577 case BT_BFCR_STATUS_EOF
:
578 BT_COMP_LOGT_STR("BFCR needs more data to decode field completely.");
579 msg_it
->state
= continue_state
;
582 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
583 "BFCR failed to start: msg-it-addr=%p, bfcr-addr=%p, "
585 msg_it
, msg_it
->bfcr
, bt_bfcr_status_string(bfcr_status
));
586 status
= CTF_MSG_ITER_STATUS_ERROR
;
590 /* Consume bits now since we know we're not in an error state */
591 buf_consume_bits(msg_it
, consumed_bits
);
597 static enum ctf_msg_iter_status
read_dscope_continue_state(struct ctf_msg_iter
*msg_it
,
598 enum state done_state
)
600 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
601 bt_self_component
*self_comp
= msg_it
->self_comp
;
602 enum bt_bfcr_status bfcr_status
;
603 size_t consumed_bits
;
605 BT_COMP_LOGT("Continuing BFCR: msg-it-addr=%p, bfcr-addr=%p", msg_it
, msg_it
->bfcr
);
607 status
= buf_ensure_available_bits(msg_it
);
608 if (status
!= CTF_MSG_ITER_STATUS_OK
) {
610 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
611 "Cannot ensure that buffer has at least one byte: "
612 "msg-addr=%p, status=%s",
613 msg_it
, ctf_msg_iter_status_string(status
));
615 BT_COMP_LOGT("Cannot ensure that buffer has at least one byte: "
616 "msg-addr=%p, status=%s",
617 msg_it
, ctf_msg_iter_status_string(status
));
623 consumed_bits
= bt_bfcr_continue(msg_it
->bfcr
, msg_it
->buf
.addr
, msg_it
->buf
.sz
, &bfcr_status
);
624 BT_COMP_LOGT("BFCR consumed bits: size=%zu", consumed_bits
);
626 switch (bfcr_status
) {
627 case BT_BFCR_STATUS_OK
:
628 /* Type was read completely. */
629 BT_COMP_LOGT_STR("Field was completely decoded.");
630 msg_it
->state
= done_state
;
632 case BT_BFCR_STATUS_EOF
:
633 /* Stay in this continue state. */
634 BT_COMP_LOGT_STR("BFCR needs more data to decode field completely.");
637 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
638 "BFCR failed to continue: msg-it-addr=%p, bfcr-addr=%p, "
640 msg_it
, msg_it
->bfcr
, bt_bfcr_status_string(bfcr_status
));
641 status
= CTF_MSG_ITER_STATUS_ERROR
;
645 /* Consume bits now since we know we're not in an error state. */
646 buf_consume_bits(msg_it
, consumed_bits
);
651 static void release_event_dscopes(struct ctf_msg_iter
*msg_it
)
653 msg_it
->dscopes
.event_common_context
= NULL
;
654 msg_it
->dscopes
.event_spec_context
= NULL
;
655 msg_it
->dscopes
.event_payload
= NULL
;
658 static void release_all_dscopes(struct ctf_msg_iter
*msg_it
)
660 msg_it
->dscopes
.stream_packet_context
= NULL
;
662 release_event_dscopes(msg_it
);
665 static enum ctf_msg_iter_status
switch_packet_state(struct ctf_msg_iter
*msg_it
)
667 enum ctf_msg_iter_status status
;
668 bt_self_component
*self_comp
= msg_it
->self_comp
;
671 * We don't put the stream class here because we need to make
672 * sure that all the packets processed by the same message
673 * iterator refer to the same stream class (the first one).
677 if (msg_it
->cur_exp_packet_total_size
!= -1) {
678 msg_it
->cur_packet_offset
+= msg_it
->cur_exp_packet_total_size
;
681 BT_COMP_LOGD("Switching packet: msg-it-addr=%p, cur=%zu, "
682 "packet-offset=%" PRId64
,
683 msg_it
, msg_it
->buf
.at
, msg_it
->cur_packet_offset
);
684 stack_clear(msg_it
->stack
);
685 msg_it
->meta
.ec
= NULL
;
686 BT_PACKET_PUT_REF_AND_RESET(msg_it
->packet
);
687 BT_MESSAGE_PUT_REF_AND_RESET(msg_it
->event_msg
);
688 release_all_dscopes(msg_it
);
689 msg_it
->cur_dscope_field
= NULL
;
691 if (msg_it
->medium
.medops
.switch_packet
) {
692 enum ctf_msg_iter_medium_status medium_status
;
694 medium_status
= msg_it
->medium
.medops
.switch_packet(msg_it
->medium
.data
);
695 if (medium_status
== CTF_MSG_ITER_MEDIUM_STATUS_EOF
) {
696 /* No more packets. */
697 msg_it
->state
= STATE_CHECK_EMIT_MSG_STREAM_END
;
698 status
= CTF_MSG_ITER_STATUS_OK
;
700 } else if (medium_status
!= CTF_MSG_ITER_MEDIUM_STATUS_OK
) {
701 status
= msg_iter_status_from_m_status(medium_status
);
706 * After the packet switch, the medium might want to give us a
707 * different buffer for the new packet.
709 status
= request_medium_bytes(msg_it
);
710 if (status
!= CTF_MSG_ITER_STATUS_OK
) {
716 * Adjust current buffer so that addr points to the beginning of the new
719 if (msg_it
->buf
.addr
) {
720 size_t consumed_bytes
= (size_t) (msg_it
->buf
.at
/ CHAR_BIT
);
722 /* Packets are assumed to start on a byte frontier. */
723 if (msg_it
->buf
.at
% CHAR_BIT
) {
724 BT_COMP_LOGE_APPEND_CAUSE(
726 "Cannot switch packet: current position is not a multiple of 8: "
727 "msg-it-addr=%p, cur=%zu",
728 msg_it
, msg_it
->buf
.at
);
729 status
= CTF_MSG_ITER_STATUS_ERROR
;
733 msg_it
->buf
.addr
+= consumed_bytes
;
734 msg_it
->buf
.sz
-= consumed_bytes
;
736 msg_it
->buf
.packet_offset
= 0;
737 BT_COMP_LOGD("Adjusted buffer: addr=%p, size=%zu", msg_it
->buf
.addr
, msg_it
->buf
.sz
);
740 msg_it
->cur_exp_packet_content_size
= -1;
741 msg_it
->cur_exp_packet_total_size
= -1;
742 msg_it
->cur_stream_class_id
= -1;
743 msg_it
->cur_event_class_id
= -1;
744 msg_it
->cur_data_stream_id
= -1;
745 msg_it
->prev_packet_snapshots
= msg_it
->snapshots
;
746 msg_it
->snapshots
.discarded_events
= UINT64_C(-1);
747 msg_it
->snapshots
.packets
= UINT64_C(-1);
748 msg_it
->snapshots
.beginning_clock
= UINT64_C(-1);
749 msg_it
->snapshots
.end_clock
= UINT64_C(-1);
750 msg_it
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
752 status
= CTF_MSG_ITER_STATUS_OK
;
757 static enum ctf_msg_iter_status
read_packet_header_begin_state(struct ctf_msg_iter
*msg_it
)
759 struct ctf_field_class
*packet_header_fc
= NULL
;
760 bt_self_component
*self_comp
= msg_it
->self_comp
;
761 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
764 * Make sure at least one bit is available for this packet. An
765 * empty packet is impossible. If we reach the end of the medium
766 * at this point, then it's considered the end of the stream.
768 status
= buf_ensure_available_bits(msg_it
);
770 case CTF_MSG_ITER_STATUS_OK
:
772 case CTF_MSG_ITER_STATUS_EOF
:
773 status
= CTF_MSG_ITER_STATUS_OK
;
774 msg_it
->state
= STATE_CHECK_EMIT_MSG_STREAM_END
;
780 /* Packet header class is common to the whole trace class. */
781 packet_header_fc
= msg_it
->meta
.tc
->packet_header_fc
;
782 if (!packet_header_fc
) {
783 msg_it
->state
= STATE_AFTER_TRACE_PACKET_HEADER
;
787 msg_it
->cur_stream_class_id
= -1;
788 msg_it
->cur_event_class_id
= -1;
789 msg_it
->cur_data_stream_id
= -1;
790 BT_COMP_LOGD("Decoding packet header field: "
791 "msg-it-addr=%p, trace-class-addr=%p, fc-addr=%p",
792 msg_it
, msg_it
->meta
.tc
, packet_header_fc
);
793 status
= read_dscope_begin_state(msg_it
, packet_header_fc
, STATE_AFTER_TRACE_PACKET_HEADER
,
794 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
, NULL
);
796 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
797 "Cannot decode packet header field: "
798 "msg-it-addr=%p, trace-class-addr=%p, "
800 msg_it
, msg_it
->meta
.tc
, packet_header_fc
);
807 static enum ctf_msg_iter_status
read_packet_header_continue_state(struct ctf_msg_iter
*msg_it
)
809 return read_dscope_continue_state(msg_it
, STATE_AFTER_TRACE_PACKET_HEADER
);
812 static inline enum ctf_msg_iter_status
set_current_stream_class(struct ctf_msg_iter
*msg_it
)
814 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
815 bt_self_component
*self_comp
= msg_it
->self_comp
;
816 struct ctf_stream_class
*new_stream_class
= NULL
;
818 if (msg_it
->cur_stream_class_id
== -1) {
820 * No current stream class ID field, therefore only one
823 if (msg_it
->meta
.tc
->stream_classes
->len
!= 1) {
824 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
825 "Need exactly one stream class since there's "
826 "no stream class ID field: "
829 status
= CTF_MSG_ITER_STATUS_ERROR
;
833 new_stream_class
= (ctf_stream_class
*) msg_it
->meta
.tc
->stream_classes
->pdata
[0];
834 msg_it
->cur_stream_class_id
= new_stream_class
->id
;
838 ctf_trace_class_borrow_stream_class_by_id(msg_it
->meta
.tc
, msg_it
->cur_stream_class_id
);
839 if (!new_stream_class
) {
840 BT_COMP_LOGE_APPEND_CAUSE(
842 "No stream class with ID of stream class ID to use in trace class: "
843 "msg-it-addr=%p, stream-class-id=%" PRIu64
", "
844 "trace-class-addr=%p",
845 msg_it
, msg_it
->cur_stream_class_id
, msg_it
->meta
.tc
);
846 status
= CTF_MSG_ITER_STATUS_ERROR
;
850 if (msg_it
->meta
.sc
) {
851 if (new_stream_class
!= msg_it
->meta
.sc
) {
852 BT_COMP_LOGE_APPEND_CAUSE(
854 "Two packets refer to two different stream classes within the same packet sequence: "
855 "msg-it-addr=%p, prev-stream-class-addr=%p, "
856 "prev-stream-class-id=%" PRId64
", "
857 "next-stream-class-addr=%p, "
858 "next-stream-class-id=%" PRId64
", "
860 msg_it
, msg_it
->meta
.sc
, msg_it
->meta
.sc
->id
, new_stream_class
,
861 new_stream_class
->id
, msg_it
->meta
.tc
);
862 status
= CTF_MSG_ITER_STATUS_ERROR
;
866 msg_it
->meta
.sc
= new_stream_class
;
869 BT_COMP_LOGD("Set current stream class: "
870 "msg-it-addr=%p, stream-class-addr=%p, "
871 "stream-class-id=%" PRId64
,
872 msg_it
, msg_it
->meta
.sc
, msg_it
->meta
.sc
->id
);
878 static inline enum ctf_msg_iter_status
set_current_stream(struct ctf_msg_iter
*msg_it
)
880 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
881 bt_self_component
*self_comp
= msg_it
->self_comp
;
882 bt_stream
*stream
= NULL
;
884 BT_COMP_LOGD("Calling user function (get stream): msg-it-addr=%p, "
885 "stream-class-addr=%p, stream-class-id=%" PRId64
,
886 msg_it
, msg_it
->meta
.sc
, msg_it
->meta
.sc
->id
);
887 stream
= msg_it
->medium
.medops
.borrow_stream(msg_it
->meta
.sc
->ir_sc
, msg_it
->cur_data_stream_id
,
888 msg_it
->medium
.data
);
889 bt_stream_get_ref(stream
);
890 BT_COMP_LOGD("User function returned: stream-addr=%p", stream
);
892 BT_COMP_LOGE_APPEND_CAUSE(
894 "User function failed to return a stream object for the given stream class.");
895 status
= CTF_MSG_ITER_STATUS_ERROR
;
899 if (msg_it
->stream
&& stream
!= msg_it
->stream
) {
900 BT_COMP_LOGE_APPEND_CAUSE(
902 "User function returned a different stream than the previous one for the same sequence of packets.");
903 status
= CTF_MSG_ITER_STATUS_ERROR
;
907 BT_STREAM_MOVE_REF(msg_it
->stream
, stream
);
910 bt_stream_put_ref(stream
);
914 static inline enum ctf_msg_iter_status
set_current_packet(struct ctf_msg_iter
*msg_it
)
916 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
917 bt_self_component
*self_comp
= msg_it
->self_comp
;
918 bt_packet
*packet
= NULL
;
920 BT_COMP_LOGD("Creating packet from stream: "
921 "msg-it-addr=%p, stream-addr=%p, "
922 "stream-class-addr=%p, "
923 "stream-class-id=%" PRId64
,
924 msg_it
, msg_it
->stream
, msg_it
->meta
.sc
, msg_it
->meta
.sc
->id
);
927 BT_ASSERT(msg_it
->stream
);
928 packet
= bt_packet_create(msg_it
->stream
);
930 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
931 "Cannot create packet from stream: "
932 "msg-it-addr=%p, stream-addr=%p, "
933 "stream-class-addr=%p, "
934 "stream-class-id=%" PRId64
,
935 msg_it
, msg_it
->stream
, msg_it
->meta
.sc
, msg_it
->meta
.sc
->id
);
942 BT_PACKET_PUT_REF_AND_RESET(packet
);
943 status
= CTF_MSG_ITER_STATUS_ERROR
;
946 BT_PACKET_MOVE_REF(msg_it
->packet
, packet
);
950 static enum ctf_msg_iter_status
after_packet_header_state(struct ctf_msg_iter
*msg_it
)
952 enum ctf_msg_iter_status status
;
954 status
= set_current_stream_class(msg_it
);
955 if (status
!= CTF_MSG_ITER_STATUS_OK
) {
959 if (!msg_it
->dry_run
) {
960 status
= set_current_stream(msg_it
);
961 if (status
!= CTF_MSG_ITER_STATUS_OK
) {
965 status
= set_current_packet(msg_it
);
966 if (status
!= CTF_MSG_ITER_STATUS_OK
) {
971 msg_it
->state
= STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
;
973 status
= CTF_MSG_ITER_STATUS_OK
;
979 static enum ctf_msg_iter_status
read_packet_context_begin_state(struct ctf_msg_iter
*msg_it
)
981 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
982 bt_self_component
*self_comp
= msg_it
->self_comp
;
983 struct ctf_field_class
*packet_context_fc
;
985 BT_ASSERT(msg_it
->meta
.sc
);
986 packet_context_fc
= msg_it
->meta
.sc
->packet_context_fc
;
987 if (!packet_context_fc
) {
988 BT_COMP_LOGD("No packet packet context field class in stream class: continuing: "
989 "msg-it-addr=%p, stream-class-addr=%p, "
990 "stream-class-id=%" PRId64
,
991 msg_it
, msg_it
->meta
.sc
, msg_it
->meta
.sc
->id
);
992 msg_it
->state
= STATE_AFTER_STREAM_PACKET_CONTEXT
;
996 if (packet_context_fc
->in_ir
&& !msg_it
->dry_run
) {
997 BT_ASSERT(!msg_it
->dscopes
.stream_packet_context
);
998 BT_ASSERT(msg_it
->packet
);
999 msg_it
->dscopes
.stream_packet_context
= bt_packet_borrow_context_field(msg_it
->packet
);
1000 BT_ASSERT(msg_it
->dscopes
.stream_packet_context
);
1003 BT_COMP_LOGD("Decoding packet context field: "
1004 "msg-it-addr=%p, stream-class-addr=%p, "
1005 "stream-class-id=%" PRId64
", fc-addr=%p",
1006 msg_it
, msg_it
->meta
.sc
, msg_it
->meta
.sc
->id
, packet_context_fc
);
1007 status
= read_dscope_begin_state(msg_it
, packet_context_fc
, STATE_AFTER_STREAM_PACKET_CONTEXT
,
1008 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
1009 msg_it
->dscopes
.stream_packet_context
);
1011 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1012 "Cannot decode packet context field: "
1013 "msg-it-addr=%p, stream-class-addr=%p, "
1014 "stream-class-id=%" PRId64
", fc-addr=%p",
1015 msg_it
, msg_it
->meta
.sc
, msg_it
->meta
.sc
->id
, packet_context_fc
);
1022 static enum ctf_msg_iter_status
read_packet_context_continue_state(struct ctf_msg_iter
*msg_it
)
1024 return read_dscope_continue_state(msg_it
, STATE_AFTER_STREAM_PACKET_CONTEXT
);
1027 static enum ctf_msg_iter_status
set_current_packet_content_sizes(struct ctf_msg_iter
*msg_it
)
1029 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
1030 bt_self_component
*self_comp
= msg_it
->self_comp
;
1032 if (msg_it
->cur_exp_packet_total_size
== -1) {
1033 if (msg_it
->cur_exp_packet_content_size
!= -1) {
1034 msg_it
->cur_exp_packet_total_size
= msg_it
->cur_exp_packet_content_size
;
1037 if (msg_it
->cur_exp_packet_content_size
== -1) {
1038 msg_it
->cur_exp_packet_content_size
= msg_it
->cur_exp_packet_total_size
;
1043 (msg_it
->cur_exp_packet_total_size
>= 0 && msg_it
->cur_exp_packet_content_size
>= 0) ||
1044 (msg_it
->cur_exp_packet_total_size
< 0 && msg_it
->cur_exp_packet_content_size
< 0));
1046 if (msg_it
->cur_exp_packet_content_size
> msg_it
->cur_exp_packet_total_size
) {
1047 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1048 "Invalid packet or content size: "
1049 "content size is greater than packet size: "
1050 "msg-it-addr=%p, packet-context-field-addr=%p, "
1051 "packet-size=%" PRId64
", content-size=%" PRId64
,
1052 msg_it
, msg_it
->dscopes
.stream_packet_context
,
1053 msg_it
->cur_exp_packet_total_size
,
1054 msg_it
->cur_exp_packet_content_size
);
1055 status
= CTF_MSG_ITER_STATUS_ERROR
;
1059 BT_COMP_LOGD("Set current packet and content sizes: "
1060 "msg-it-addr=%p, packet-size=%" PRIu64
", content-size=%" PRIu64
,
1061 msg_it
, msg_it
->cur_exp_packet_total_size
, msg_it
->cur_exp_packet_content_size
);
1067 static enum ctf_msg_iter_status
after_packet_context_state(struct ctf_msg_iter
*msg_it
)
1069 enum ctf_msg_iter_status status
;
1071 status
= set_current_packet_content_sizes(msg_it
);
1072 if (status
!= CTF_MSG_ITER_STATUS_OK
) {
1076 if (msg_it
->emit_stream_beginning_message
) {
1077 msg_it
->state
= STATE_EMIT_MSG_STREAM_BEGINNING
;
1079 msg_it
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS
;
1086 static enum ctf_msg_iter_status
read_event_header_begin_state(struct ctf_msg_iter
*msg_it
)
1088 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
1089 bt_self_component
*self_comp
= msg_it
->self_comp
;
1090 struct ctf_field_class
*event_header_fc
= NULL
;
1092 /* Reset the position of the last event header */
1093 msg_it
->buf
.last_eh_at
= msg_it
->buf
.at
;
1094 msg_it
->cur_event_class_id
= -1;
1096 /* Check if we have some content left */
1097 if (msg_it
->cur_exp_packet_content_size
>= 0) {
1098 if (G_UNLIKELY(packet_at(msg_it
) == msg_it
->cur_exp_packet_content_size
)) {
1099 /* No more events! */
1100 BT_COMP_LOGD("Reached end of packet: msg-it-addr=%p, "
1102 msg_it
, packet_at(msg_it
));
1103 msg_it
->state
= STATE_EMIT_MSG_PACKET_END_MULTI
;
1105 } else if (G_UNLIKELY(packet_at(msg_it
) > msg_it
->cur_exp_packet_content_size
)) {
1106 /* That's not supposed to happen */
1108 "Before decoding event header field: cursor is passed the packet's content: "
1109 "msg-it-addr=%p, content-size=%" PRId64
", "
1111 msg_it
, msg_it
->cur_exp_packet_content_size
, packet_at(msg_it
));
1112 status
= CTF_MSG_ITER_STATUS_ERROR
;
1117 * "Infinite" content: we're done when the medium has
1118 * nothing else for us.
1120 status
= buf_ensure_available_bits(msg_it
);
1122 case CTF_MSG_ITER_STATUS_OK
:
1124 case CTF_MSG_ITER_STATUS_EOF
:
1125 status
= CTF_MSG_ITER_STATUS_OK
;
1126 msg_it
->state
= STATE_EMIT_MSG_PACKET_END_SINGLE
;
1133 release_event_dscopes(msg_it
);
1134 BT_ASSERT(msg_it
->meta
.sc
);
1135 event_header_fc
= msg_it
->meta
.sc
->event_header_fc
;
1136 if (!event_header_fc
) {
1137 msg_it
->state
= STATE_AFTER_EVENT_HEADER
;
1141 BT_COMP_LOGD("Decoding event header field: "
1142 "msg-it-addr=%p, stream-class-addr=%p, "
1143 "stream-class-id=%" PRId64
", "
1145 msg_it
, msg_it
->meta
.sc
, msg_it
->meta
.sc
->id
, event_header_fc
);
1146 status
= read_dscope_begin_state(msg_it
, event_header_fc
, STATE_AFTER_EVENT_HEADER
,
1147 STATE_DSCOPE_EVENT_HEADER_CONTINUE
, NULL
);
1149 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1150 "Cannot decode event header field: "
1151 "msg-it-addr=%p, stream-class-addr=%p, "
1152 "stream-class-id=%" PRId64
", fc-addr=%p",
1153 msg_it
, msg_it
->meta
.sc
, msg_it
->meta
.sc
->id
, event_header_fc
);
1160 static enum ctf_msg_iter_status
read_event_header_continue_state(struct ctf_msg_iter
*msg_it
)
1162 return read_dscope_continue_state(msg_it
, STATE_AFTER_EVENT_HEADER
);
1165 static inline enum ctf_msg_iter_status
set_current_event_class(struct ctf_msg_iter
*msg_it
)
1167 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
1168 bt_self_component
*self_comp
= msg_it
->self_comp
;
1170 struct ctf_event_class
*new_event_class
= NULL
;
1172 if (msg_it
->cur_event_class_id
== -1) {
1174 * No current event class ID field, therefore only one
1177 if (msg_it
->meta
.sc
->event_classes
->len
!= 1) {
1178 BT_COMP_LOGE_APPEND_CAUSE(
1180 "Need exactly one event class since there's no event class ID field: "
1183 status
= CTF_MSG_ITER_STATUS_ERROR
;
1187 new_event_class
= (ctf_event_class
*) msg_it
->meta
.sc
->event_classes
->pdata
[0];
1188 msg_it
->cur_event_class_id
= new_event_class
->id
;
1192 ctf_stream_class_borrow_event_class_by_id(msg_it
->meta
.sc
, msg_it
->cur_event_class_id
);
1193 if (!new_event_class
) {
1194 BT_COMP_LOGE_APPEND_CAUSE(
1196 "No event class with ID of event class ID to use in stream class: "
1197 "msg-it-addr=%p, stream-class-id=%" PRIu64
", "
1198 "event-class-id=%" PRIu64
", "
1199 "trace-class-addr=%p",
1200 msg_it
, msg_it
->meta
.sc
->id
, msg_it
->cur_event_class_id
, msg_it
->meta
.tc
);
1201 status
= CTF_MSG_ITER_STATUS_ERROR
;
1205 msg_it
->meta
.ec
= new_event_class
;
1206 BT_COMP_LOGD("Set current event class: "
1207 "msg-it-addr=%p, event-class-addr=%p, "
1208 "event-class-id=%" PRId64
", "
1209 "event-class-name=\"%s\"",
1210 msg_it
, msg_it
->meta
.ec
, msg_it
->meta
.ec
->id
, msg_it
->meta
.ec
->name
->str
);
1216 static inline enum ctf_msg_iter_status
set_current_event_message(struct ctf_msg_iter
*msg_it
)
1218 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
1219 bt_self_component
*self_comp
= msg_it
->self_comp
;
1220 bt_message
*msg
= NULL
;
1222 BT_ASSERT_DBG(msg_it
->meta
.ec
);
1223 BT_ASSERT_DBG(msg_it
->packet
);
1224 BT_COMP_LOGD("Creating event message from event class and packet: "
1225 "msg-it-addr=%p, ec-addr=%p, ec-name=\"%s\", packet-addr=%p",
1226 msg_it
, msg_it
->meta
.ec
, msg_it
->meta
.ec
->name
->str
, msg_it
->packet
);
1227 BT_ASSERT_DBG(msg_it
->self_msg_iter
);
1228 BT_ASSERT_DBG(msg_it
->meta
.sc
);
1230 if (bt_stream_class_borrow_default_clock_class(msg_it
->meta
.sc
->ir_sc
)) {
1231 msg
= bt_message_event_create_with_packet_and_default_clock_snapshot(
1232 msg_it
->self_msg_iter
, msg_it
->meta
.ec
->ir_ec
, msg_it
->packet
,
1233 msg_it
->default_clock_snapshot
);
1235 msg
= bt_message_event_create_with_packet(msg_it
->self_msg_iter
, msg_it
->meta
.ec
->ir_ec
,
1240 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1241 "Cannot create event message: "
1242 "msg-it-addr=%p, ec-addr=%p, ec-name=\"%s\", "
1244 msg_it
, msg_it
->meta
.ec
, msg_it
->meta
.ec
->name
->str
,
1252 BT_MESSAGE_PUT_REF_AND_RESET(msg
);
1253 status
= CTF_MSG_ITER_STATUS_ERROR
;
1256 BT_MESSAGE_MOVE_REF(msg_it
->event_msg
, msg
);
1260 static enum ctf_msg_iter_status
after_event_header_state(struct ctf_msg_iter
*msg_it
)
1262 enum ctf_msg_iter_status status
;
1264 status
= set_current_event_class(msg_it
);
1265 if (status
!= CTF_MSG_ITER_STATUS_OK
) {
1269 if (G_UNLIKELY(msg_it
->dry_run
)) {
1273 status
= set_current_event_message(msg_it
);
1274 if (status
!= CTF_MSG_ITER_STATUS_OK
) {
1278 msg_it
->event
= bt_message_event_borrow_event(msg_it
->event_msg
);
1279 BT_ASSERT_DBG(msg_it
->event
);
1282 msg_it
->state
= STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
;
1288 static enum ctf_msg_iter_status
read_event_common_context_begin_state(struct ctf_msg_iter
*msg_it
)
1290 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
1291 bt_self_component
*self_comp
= msg_it
->self_comp
;
1292 struct ctf_field_class
*event_common_context_fc
;
1294 event_common_context_fc
= msg_it
->meta
.sc
->event_common_context_fc
;
1295 if (!event_common_context_fc
) {
1296 msg_it
->state
= STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
;
1300 if (event_common_context_fc
->in_ir
&& !msg_it
->dry_run
) {
1301 BT_ASSERT_DBG(!msg_it
->dscopes
.event_common_context
);
1302 msg_it
->dscopes
.event_common_context
= bt_event_borrow_common_context_field(msg_it
->event
);
1303 BT_ASSERT_DBG(msg_it
->dscopes
.event_common_context
);
1306 BT_COMP_LOGT("Decoding event common context field: "
1307 "msg-it-addr=%p, stream-class-addr=%p, "
1308 "stream-class-id=%" PRId64
", "
1310 msg_it
, msg_it
->meta
.sc
, msg_it
->meta
.sc
->id
, event_common_context_fc
);
1311 status
= read_dscope_begin_state(
1312 msg_it
, event_common_context_fc
, STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
,
1313 STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
, msg_it
->dscopes
.event_common_context
);
1315 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1316 "Cannot decode event common context field: "
1317 "msg-it-addr=%p, stream-class-addr=%p, "
1318 "stream-class-id=%" PRId64
", fc-addr=%p",
1319 msg_it
, msg_it
->meta
.sc
, msg_it
->meta
.sc
->id
,
1320 event_common_context_fc
);
1327 static enum ctf_msg_iter_status
1328 read_event_common_context_continue_state(struct ctf_msg_iter
*msg_it
)
1330 return read_dscope_continue_state(msg_it
, STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
);
1333 static enum ctf_msg_iter_status
read_event_spec_context_begin_state(struct ctf_msg_iter
*msg_it
)
1335 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
1336 bt_self_component
*self_comp
= msg_it
->self_comp
;
1337 struct ctf_field_class
*event_spec_context_fc
;
1339 event_spec_context_fc
= msg_it
->meta
.ec
->spec_context_fc
;
1340 if (!event_spec_context_fc
) {
1341 msg_it
->state
= STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
;
1345 if (event_spec_context_fc
->in_ir
&& !msg_it
->dry_run
) {
1346 BT_ASSERT_DBG(!msg_it
->dscopes
.event_spec_context
);
1347 msg_it
->dscopes
.event_spec_context
= bt_event_borrow_specific_context_field(msg_it
->event
);
1348 BT_ASSERT_DBG(msg_it
->dscopes
.event_spec_context
);
1351 BT_COMP_LOGT("Decoding event specific context field: "
1352 "msg-it-addr=%p, event-class-addr=%p, "
1353 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1355 msg_it
, msg_it
->meta
.ec
, msg_it
->meta
.ec
->name
->str
, msg_it
->meta
.ec
->id
,
1356 event_spec_context_fc
);
1357 status
= read_dscope_begin_state(
1358 msg_it
, event_spec_context_fc
, STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
1359 STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
, msg_it
->dscopes
.event_spec_context
);
1361 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1362 "Cannot decode event specific context field: "
1363 "msg-it-addr=%p, event-class-addr=%p, "
1364 "event-class-name=\"%s\", "
1365 "event-class-id=%" PRId64
", fc-addr=%p",
1366 msg_it
, msg_it
->meta
.ec
, msg_it
->meta
.ec
->name
->str
,
1367 msg_it
->meta
.ec
->id
, event_spec_context_fc
);
1374 static enum ctf_msg_iter_status
read_event_spec_context_continue_state(struct ctf_msg_iter
*msg_it
)
1376 return read_dscope_continue_state(msg_it
, STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
);
1379 static enum ctf_msg_iter_status
read_event_payload_begin_state(struct ctf_msg_iter
*msg_it
)
1381 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
1382 bt_self_component
*self_comp
= msg_it
->self_comp
;
1383 struct ctf_field_class
*event_payload_fc
;
1385 event_payload_fc
= msg_it
->meta
.ec
->payload_fc
;
1386 if (!event_payload_fc
) {
1387 msg_it
->state
= STATE_EMIT_MSG_EVENT
;
1391 if (event_payload_fc
->in_ir
&& !msg_it
->dry_run
) {
1392 BT_ASSERT_DBG(!msg_it
->dscopes
.event_payload
);
1393 msg_it
->dscopes
.event_payload
= bt_event_borrow_payload_field(msg_it
->event
);
1394 BT_ASSERT_DBG(msg_it
->dscopes
.event_payload
);
1397 BT_COMP_LOGT("Decoding event payload field: "
1398 "msg-it-addr=%p, event-class-addr=%p, "
1399 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1401 msg_it
, msg_it
->meta
.ec
, msg_it
->meta
.ec
->name
->str
, msg_it
->meta
.ec
->id
,
1404 read_dscope_begin_state(msg_it
, event_payload_fc
, STATE_EMIT_MSG_EVENT
,
1405 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
, msg_it
->dscopes
.event_payload
);
1407 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1408 "Cannot decode event payload field: "
1409 "msg-it-addr=%p, event-class-addr=%p, "
1410 "event-class-name=\"%s\", "
1411 "event-class-id=%" PRId64
", fc-addr=%p",
1412 msg_it
, msg_it
->meta
.ec
, msg_it
->meta
.ec
->name
->str
,
1413 msg_it
->meta
.ec
->id
, event_payload_fc
);
1420 static enum ctf_msg_iter_status
read_event_payload_continue_state(struct ctf_msg_iter
*msg_it
)
1422 return read_dscope_continue_state(msg_it
, STATE_EMIT_MSG_EVENT
);
1425 static enum ctf_msg_iter_status
skip_packet_padding_state(struct ctf_msg_iter
*msg_it
)
1427 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
1428 size_t bits_to_skip
;
1429 const enum state next_state
= STATE_SWITCH_PACKET
;
1431 BT_ASSERT(msg_it
->cur_exp_packet_total_size
> 0);
1432 bits_to_skip
= msg_it
->cur_exp_packet_total_size
- packet_at(msg_it
);
1433 if (bits_to_skip
== 0) {
1434 msg_it
->state
= next_state
;
1437 size_t bits_to_consume
;
1439 BT_COMP_LOGD("Trying to skip %zu bits of padding: msg-it-addr=%p, size=%zu", bits_to_skip
,
1440 msg_it
, bits_to_skip
);
1441 status
= buf_ensure_available_bits(msg_it
);
1442 if (status
!= CTF_MSG_ITER_STATUS_OK
) {
1446 bits_to_consume
= MIN(buf_available_bits(msg_it
), bits_to_skip
);
1447 BT_COMP_LOGD("Skipping %zu bits of padding: msg-it-addr=%p, size=%zu", bits_to_consume
,
1448 msg_it
, bits_to_consume
);
1449 buf_consume_bits(msg_it
, bits_to_consume
);
1450 bits_to_skip
= msg_it
->cur_exp_packet_total_size
- packet_at(msg_it
);
1451 if (bits_to_skip
== 0) {
1452 msg_it
->state
= next_state
;
1461 static enum ctf_msg_iter_status
check_emit_msg_discarded_events(struct ctf_msg_iter
*msg_it
)
1463 msg_it
->state
= STATE_EMIT_MSG_DISCARDED_EVENTS
;
1465 if (!msg_it
->meta
.sc
->has_discarded_events
) {
1466 msg_it
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
;
1470 if (msg_it
->prev_packet_snapshots
.discarded_events
== UINT64_C(-1)) {
1471 if (msg_it
->snapshots
.discarded_events
== 0 ||
1472 msg_it
->snapshots
.discarded_events
== UINT64_C(-1)) {
1474 * Stream's first packet with no discarded
1475 * events or no information about discarded
1476 * events: do not emit.
1478 msg_it
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
;
1482 * If the previous packet has a value for this counter,
1483 * then this counter is defined for the whole stream.
1485 BT_ASSERT(msg_it
->snapshots
.discarded_events
!= UINT64_C(-1));
1487 if (msg_it
->snapshots
.discarded_events
- msg_it
->prev_packet_snapshots
.discarded_events
==
1490 * No discarded events since previous packet: do
1493 msg_it
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
;
1498 return CTF_MSG_ITER_STATUS_OK
;
1501 static enum ctf_msg_iter_status
check_emit_msg_discarded_packets(struct ctf_msg_iter
*msg_it
)
1503 msg_it
->state
= STATE_EMIT_MSG_DISCARDED_PACKETS
;
1505 if (!msg_it
->meta
.sc
->has_discarded_packets
) {
1506 msg_it
->state
= STATE_EMIT_MSG_PACKET_BEGINNING
;
1510 if (msg_it
->prev_packet_snapshots
.packets
== UINT64_C(-1)) {
1512 * Stream's first packet or no information about
1513 * discarded packets: do not emit. In other words, if
1514 * this is the first packet and its sequence number is
1515 * not 0, do not consider that packets were previously
1516 * lost: we might be reading a partial stream (LTTng
1517 * snapshot for example).
1519 msg_it
->state
= STATE_EMIT_MSG_PACKET_BEGINNING
;
1522 * If the previous packet has a value for this counter,
1523 * then this counter is defined for the whole stream.
1525 BT_ASSERT(msg_it
->snapshots
.packets
!= UINT64_C(-1));
1527 if (msg_it
->snapshots
.packets
- msg_it
->prev_packet_snapshots
.packets
<= 1) {
1529 * No discarded packets since previous packet:
1532 msg_it
->state
= STATE_EMIT_MSG_PACKET_BEGINNING
;
1537 return CTF_MSG_ITER_STATUS_OK
;
1540 static inline enum state
check_emit_msg_stream_end(struct ctf_msg_iter
*msg_it
)
1542 enum state next_state
;
1544 if (msg_it
->emit_stream_end_message
) {
1545 next_state
= STATE_EMIT_MSG_STREAM_END
;
1547 next_state
= STATE_DONE
;
1553 static inline enum ctf_msg_iter_status
handle_state(struct ctf_msg_iter
*msg_it
)
1555 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
1556 const enum state state
= msg_it
->state
;
1558 BT_COMP_LOGT("Handling state: msg-it-addr=%p, state=%s", msg_it
, state_string(state
));
1560 // TODO: optimalize!
1563 msg_it
->state
= STATE_SWITCH_PACKET
;
1565 case STATE_SWITCH_PACKET
:
1566 status
= switch_packet_state(msg_it
);
1568 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
1569 status
= read_packet_header_begin_state(msg_it
);
1571 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
1572 status
= read_packet_header_continue_state(msg_it
);
1574 case STATE_AFTER_TRACE_PACKET_HEADER
:
1575 status
= after_packet_header_state(msg_it
);
1577 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
1578 status
= read_packet_context_begin_state(msg_it
);
1580 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
1581 status
= read_packet_context_continue_state(msg_it
);
1583 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
1584 status
= after_packet_context_state(msg_it
);
1586 case STATE_EMIT_MSG_STREAM_BEGINNING
:
1587 msg_it
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS
;
1589 case STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS
:
1590 status
= check_emit_msg_discarded_events(msg_it
);
1592 case STATE_EMIT_MSG_DISCARDED_EVENTS
:
1593 msg_it
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
;
1595 case STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
:
1596 status
= check_emit_msg_discarded_packets(msg_it
);
1598 case STATE_EMIT_MSG_DISCARDED_PACKETS
:
1599 msg_it
->state
= STATE_EMIT_MSG_PACKET_BEGINNING
;
1601 case STATE_EMIT_MSG_PACKET_BEGINNING
:
1602 msg_it
->state
= STATE_DSCOPE_EVENT_HEADER_BEGIN
;
1604 case STATE_DSCOPE_EVENT_HEADER_BEGIN
:
1605 status
= read_event_header_begin_state(msg_it
);
1607 case STATE_DSCOPE_EVENT_HEADER_CONTINUE
:
1608 status
= read_event_header_continue_state(msg_it
);
1610 case STATE_AFTER_EVENT_HEADER
:
1611 status
= after_event_header_state(msg_it
);
1613 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
:
1614 status
= read_event_common_context_begin_state(msg_it
);
1616 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
:
1617 status
= read_event_common_context_continue_state(msg_it
);
1619 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
:
1620 status
= read_event_spec_context_begin_state(msg_it
);
1622 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
:
1623 status
= read_event_spec_context_continue_state(msg_it
);
1625 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
1626 status
= read_event_payload_begin_state(msg_it
);
1628 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
1629 status
= read_event_payload_continue_state(msg_it
);
1631 case STATE_EMIT_MSG_EVENT
:
1632 msg_it
->state
= STATE_DSCOPE_EVENT_HEADER_BEGIN
;
1634 case STATE_EMIT_QUEUED_MSG_EVENT
:
1635 msg_it
->state
= STATE_EMIT_MSG_EVENT
;
1637 case STATE_SKIP_PACKET_PADDING
:
1638 status
= skip_packet_padding_state(msg_it
);
1640 case STATE_EMIT_MSG_PACKET_END_MULTI
:
1641 msg_it
->state
= STATE_SKIP_PACKET_PADDING
;
1643 case STATE_EMIT_MSG_PACKET_END_SINGLE
:
1644 msg_it
->state
= STATE_EMIT_MSG_STREAM_END
;
1646 case STATE_EMIT_QUEUED_MSG_PACKET_END
:
1647 msg_it
->state
= STATE_EMIT_MSG_PACKET_END_SINGLE
;
1649 case STATE_CHECK_EMIT_MSG_STREAM_END
:
1650 msg_it
->state
= check_emit_msg_stream_end(msg_it
);
1652 case STATE_EMIT_MSG_STREAM_END
:
1653 msg_it
->state
= STATE_DONE
;
1658 BT_COMP_LOGF("Unknown CTF plugin message iterator state: "
1659 "msg-it-addr=%p, state=%d",
1660 msg_it
, msg_it
->state
);
1664 BT_COMP_LOGT("Handled state: msg-it-addr=%p, status=%s, "
1665 "prev-state=%s, cur-state=%s",
1666 msg_it
, ctf_msg_iter_status_string(status
), state_string(state
),
1667 state_string(msg_it
->state
));
1671 void ctf_msg_iter_reset_for_next_stream_file(struct ctf_msg_iter
*msg_it
)
1674 BT_COMP_LOGD("Resetting message iterator: addr=%p", msg_it
);
1675 stack_clear(msg_it
->stack
);
1676 msg_it
->meta
.sc
= NULL
;
1677 msg_it
->meta
.ec
= NULL
;
1678 BT_PACKET_PUT_REF_AND_RESET(msg_it
->packet
);
1679 BT_STREAM_PUT_REF_AND_RESET(msg_it
->stream
);
1680 BT_MESSAGE_PUT_REF_AND_RESET(msg_it
->event_msg
);
1681 release_all_dscopes(msg_it
);
1682 msg_it
->cur_dscope_field
= NULL
;
1684 msg_it
->buf
.addr
= NULL
;
1687 msg_it
->buf
.last_eh_at
= SIZE_MAX
;
1688 msg_it
->buf
.packet_offset
= 0;
1689 msg_it
->state
= STATE_INIT
;
1690 msg_it
->cur_exp_packet_content_size
= -1;
1691 msg_it
->cur_exp_packet_total_size
= -1;
1692 msg_it
->cur_packet_offset
= -1;
1693 msg_it
->cur_event_class_id
= -1;
1694 msg_it
->snapshots
.beginning_clock
= UINT64_C(-1);
1695 msg_it
->snapshots
.end_clock
= UINT64_C(-1);
1699 * Resets the internal state of a CTF message iterator.
1701 void ctf_msg_iter_reset(struct ctf_msg_iter
*msg_it
)
1703 ctf_msg_iter_reset_for_next_stream_file(msg_it
);
1704 msg_it
->cur_stream_class_id
= -1;
1705 msg_it
->cur_data_stream_id
= -1;
1706 msg_it
->snapshots
.discarded_events
= UINT64_C(-1);
1707 msg_it
->snapshots
.packets
= UINT64_C(-1);
1708 msg_it
->prev_packet_snapshots
.discarded_events
= UINT64_C(-1);
1709 msg_it
->prev_packet_snapshots
.packets
= UINT64_C(-1);
1710 msg_it
->prev_packet_snapshots
.beginning_clock
= UINT64_C(-1);
1711 msg_it
->prev_packet_snapshots
.end_clock
= UINT64_C(-1);
1712 msg_it
->emit_stream_beginning_message
= true;
1713 msg_it
->emit_stream_end_message
= false;
1716 static bt_field
*borrow_next_field(struct ctf_msg_iter
*msg_it
)
1718 bt_field
*next_field
= NULL
;
1719 bt_field
*base_field
;
1720 const bt_field_class
*base_fc
;
1721 bt_field_class_type base_fc_type
;
1724 BT_ASSERT_DBG(!stack_empty(msg_it
->stack
));
1725 index
= stack_top(msg_it
->stack
)->index
;
1726 base_field
= stack_top(msg_it
->stack
)->base
;
1727 BT_ASSERT_DBG(base_field
);
1728 base_fc
= bt_field_borrow_class_const(base_field
);
1729 BT_ASSERT_DBG(base_fc
);
1730 base_fc_type
= bt_field_class_get_type(base_fc
);
1732 if (base_fc_type
== BT_FIELD_CLASS_TYPE_STRUCTURE
) {
1733 BT_ASSERT_DBG(index
< bt_field_class_structure_get_member_count(
1734 bt_field_borrow_class_const(base_field
)));
1735 next_field
= bt_field_structure_borrow_member_field_by_index(base_field
, index
);
1736 } else if (bt_field_class_type_is(base_fc_type
, BT_FIELD_CLASS_TYPE_ARRAY
)) {
1737 BT_ASSERT_DBG(index
< bt_field_array_get_length(base_field
));
1738 next_field
= bt_field_array_borrow_element_field_by_index(base_field
, index
);
1739 } else if (bt_field_class_type_is(base_fc_type
, BT_FIELD_CLASS_TYPE_VARIANT
)) {
1740 BT_ASSERT_DBG(index
== 0);
1741 next_field
= bt_field_variant_borrow_selected_option_field(base_field
);
1746 BT_ASSERT_DBG(next_field
);
1750 static void update_default_clock(struct ctf_msg_iter
*msg_it
, uint64_t new_val
,
1751 uint64_t new_val_size
)
1753 uint64_t new_val_mask
;
1754 uint64_t cur_value_masked
;
1756 BT_ASSERT_DBG(new_val_size
> 0);
1759 * Special case for a 64-bit new value, which is the limit
1760 * of a clock value as of this version: overwrite the
1761 * current value directly.
1763 if (new_val_size
== 64) {
1764 msg_it
->default_clock_snapshot
= new_val
;
1768 new_val_mask
= (1ULL << new_val_size
) - 1;
1769 cur_value_masked
= msg_it
->default_clock_snapshot
& new_val_mask
;
1771 if (new_val
< cur_value_masked
) {
1773 * It looks like a wrap happened on the number of bits
1774 * of the requested new value. Assume that the clock
1775 * value wrapped only one time.
1777 msg_it
->default_clock_snapshot
+= new_val_mask
+ 1;
1780 /* Clear the low bits of the current clock value. */
1781 msg_it
->default_clock_snapshot
&= ~new_val_mask
;
1783 /* Set the low bits of the current clock value. */
1784 msg_it
->default_clock_snapshot
|= new_val
;
1787 BT_COMP_LOGT("Updated default clock's value from integer field's value: "
1789 msg_it
->default_clock_snapshot
);
1793 * Ensure the message iterator's `stored_values` array is large enough to
1794 * accomodate `storing_index`.
1796 * We may need more slots in the array than initially allocated if more
1797 * metadata arrives along the way.
1799 static void ensure_stored_values_size(ctf_msg_iter
*msg_it
, uint64_t storing_index
)
1801 if (G_UNLIKELY(storing_index
>= msg_it
->stored_values
->len
)) {
1802 g_array_set_size(msg_it
->stored_values
, msg_it
->meta
.tc
->stored_value_count
);
1806 static enum bt_bfcr_status
bfcr_unsigned_int_cb(uint64_t value
, struct ctf_field_class
*fc
,
1809 ctf_msg_iter
*msg_it
= (ctf_msg_iter
*) data
;
1810 bt_self_component
*self_comp
= msg_it
->self_comp
;
1811 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
1813 bt_field
*field
= NULL
;
1815 BT_COMP_LOGT("Unsigned integer function called from BFCR: "
1816 "msg-it-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1817 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64
,
1818 msg_it
, msg_it
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
1820 ctf_field_class_int
*int_fc
= ctf_field_class_as_int(fc
);
1822 if (G_LIKELY(int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
)) {
1823 goto update_def_clock
;
1826 switch (int_fc
->meaning
) {
1827 case CTF_FIELD_CLASS_MEANING_EVENT_CLASS_ID
:
1828 msg_it
->cur_event_class_id
= value
;
1830 case CTF_FIELD_CLASS_MEANING_DATA_STREAM_ID
:
1831 msg_it
->cur_data_stream_id
= value
;
1833 case CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME
:
1834 msg_it
->snapshots
.beginning_clock
= value
;
1836 case CTF_FIELD_CLASS_MEANING_PACKET_END_TIME
:
1837 msg_it
->snapshots
.end_clock
= value
;
1839 case CTF_FIELD_CLASS_MEANING_STREAM_CLASS_ID
:
1840 msg_it
->cur_stream_class_id
= value
;
1842 case CTF_FIELD_CLASS_MEANING_MAGIC
:
1843 if (value
!= 0xc1fc1fc1) {
1844 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1845 "Invalid CTF magic number: msg-it-addr=%p, "
1848 status
= BT_BFCR_STATUS_ERROR
;
1853 case CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT
:
1854 msg_it
->snapshots
.packets
= value
;
1856 case CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT
:
1857 msg_it
->snapshots
.discarded_events
= value
;
1859 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_TOTAL_SIZE
:
1860 msg_it
->cur_exp_packet_total_size
= value
;
1862 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_CONTENT_SIZE
:
1863 msg_it
->cur_exp_packet_content_size
= value
;
1870 if (G_UNLIKELY(int_fc
->mapped_clock_class
)) {
1871 update_default_clock(msg_it
, value
, int_fc
->base
.size
);
1874 if (G_UNLIKELY(int_fc
->storing_index
>= 0)) {
1875 ensure_stored_values_size(msg_it
, int_fc
->storing_index
);
1876 bt_g_array_index(msg_it
->stored_values
, uint64_t, (uint64_t) int_fc
->storing_index
) = value
;
1879 if (G_UNLIKELY(!fc
->in_ir
|| msg_it
->dry_run
)) {
1883 field
= borrow_next_field(msg_it
);
1884 BT_ASSERT_DBG(field
);
1885 BT_ASSERT_DBG(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
1886 BT_ASSERT_DBG(bt_field_class_type_is(bt_field_get_class_type(field
),
1887 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
));
1888 bt_field_integer_unsigned_set_value(field
, value
);
1889 stack_top(msg_it
->stack
)->index
++;
1895 static enum bt_bfcr_status
bfcr_unsigned_int_char_cb(uint64_t value
, struct ctf_field_class
*fc
,
1899 ctf_msg_iter
*msg_it
= (ctf_msg_iter
*) data
;
1900 bt_self_component
*self_comp
= msg_it
->self_comp
;
1901 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
1902 bt_field
*string_field
= NULL
;
1903 char str
[2] = {'\0', '\0'};
1905 BT_COMP_LOGT("Unsigned integer character function called from BFCR: "
1906 "msg-it-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1907 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64
,
1908 msg_it
, msg_it
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
1910 ctf_field_class_int
*int_fc
= ctf_field_class_as_int(fc
);
1911 BT_ASSERT_DBG(int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
);
1912 BT_ASSERT_DBG(!int_fc
->mapped_clock_class
);
1913 BT_ASSERT_DBG(int_fc
->storing_index
< 0);
1915 if (G_UNLIKELY(!fc
->in_ir
|| msg_it
->dry_run
)) {
1919 if (msg_it
->done_filling_string
) {
1924 msg_it
->done_filling_string
= true;
1928 string_field
= stack_top(msg_it
->stack
)->base
;
1929 BT_ASSERT_DBG(bt_field_get_class_type(string_field
) == BT_FIELD_CLASS_TYPE_STRING
);
1931 /* Append character */
1932 str
[0] = (char) value
;
1933 ret
= bt_field_string_append_with_length(string_field
, str
, 1);
1935 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1936 "Cannot append character to string field's value: "
1937 "msg-it-addr=%p, field-addr=%p, ret=%d",
1938 msg_it
, string_field
, ret
);
1939 status
= BT_BFCR_STATUS_ERROR
;
1947 static enum bt_bfcr_status
bfcr_signed_int_cb(int64_t value
, struct ctf_field_class
*fc
, void *data
)
1949 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
1950 bt_field
*field
= NULL
;
1951 ctf_msg_iter
*msg_it
= (ctf_msg_iter
*) data
;
1953 BT_COMP_LOGT("Signed integer function called from BFCR: "
1954 "msg-it-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1955 "fc-type=%d, fc-in-ir=%d, value=%" PRId64
,
1956 msg_it
, msg_it
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
1958 ctf_field_class_int
*int_fc
= ctf_field_class_as_int(fc
);
1959 BT_ASSERT_DBG(int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
);
1961 if (G_UNLIKELY(int_fc
->storing_index
>= 0)) {
1962 ensure_stored_values_size(msg_it
, int_fc
->storing_index
);
1963 bt_g_array_index(msg_it
->stored_values
, uint64_t, (uint64_t) int_fc
->storing_index
) =
1967 if (G_UNLIKELY(!fc
->in_ir
|| msg_it
->dry_run
)) {
1971 field
= borrow_next_field(msg_it
);
1972 BT_ASSERT_DBG(field
);
1973 BT_ASSERT_DBG(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
1975 bt_field_class_type_is(bt_field_get_class_type(field
), BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
));
1976 bt_field_integer_signed_set_value(field
, value
);
1977 stack_top(msg_it
->stack
)->index
++;
1983 static enum bt_bfcr_status
bfcr_floating_point_cb(double value
, struct ctf_field_class
*fc
,
1986 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
1987 bt_field
*field
= NULL
;
1988 ctf_msg_iter
*msg_it
= (ctf_msg_iter
*) data
;
1989 bt_field_class_type type
;
1991 BT_COMP_LOGT("Floating point number function called from BFCR: "
1992 "msg-it-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1993 "fc-type=%d, fc-in-ir=%d, value=%f",
1994 msg_it
, msg_it
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
1996 if (G_UNLIKELY(!fc
->in_ir
|| msg_it
->dry_run
)) {
2000 field
= borrow_next_field(msg_it
);
2001 type
= bt_field_get_class_type(field
);
2002 BT_ASSERT_DBG(field
);
2003 BT_ASSERT_DBG(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
2004 BT_ASSERT_DBG(bt_field_class_type_is(type
, BT_FIELD_CLASS_TYPE_REAL
));
2006 if (type
== BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL
) {
2007 bt_field_real_single_precision_set_value(field
, (float) value
);
2009 bt_field_real_double_precision_set_value(field
, value
);
2011 stack_top(msg_it
->stack
)->index
++;
2017 static enum bt_bfcr_status
bfcr_string_begin_cb(struct ctf_field_class
*fc
, void *data
)
2019 bt_field
*field
= NULL
;
2020 ctf_msg_iter
*msg_it
= (ctf_msg_iter
*) data
;
2022 BT_COMP_LOGT("String (beginning) function called from BFCR: "
2023 "msg-it-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2024 "fc-type=%d, fc-in-ir=%d",
2025 msg_it
, msg_it
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
2027 if (G_UNLIKELY(!fc
->in_ir
|| msg_it
->dry_run
)) {
2031 field
= borrow_next_field(msg_it
);
2032 BT_ASSERT_DBG(field
);
2033 BT_ASSERT_DBG(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
2034 BT_ASSERT_DBG(bt_field_get_class_type(field
) == BT_FIELD_CLASS_TYPE_STRING
);
2035 bt_field_string_clear(field
);
2038 * Push on stack. Not a compound class per se, but we know that
2039 * only bfcr_string_cb() may be called between this call and a
2040 * subsequent call to bfcr_string_end_cb().
2042 stack_push(msg_it
->stack
, field
);
2045 return BT_BFCR_STATUS_OK
;
2048 static enum bt_bfcr_status
bfcr_string_cb(const char *value
, size_t len
, struct ctf_field_class
*fc
,
2051 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
2052 bt_field
*field
= NULL
;
2053 ctf_msg_iter
*msg_it
= (ctf_msg_iter
*) data
;
2054 bt_self_component
*self_comp
= msg_it
->self_comp
;
2057 BT_COMP_LOGT("String (substring) function called from BFCR: "
2058 "msg-it-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2059 "fc-type=%d, fc-in-ir=%d, string-length=%zu",
2060 msg_it
, msg_it
->bfcr
, fc
, fc
->type
, fc
->in_ir
, len
);
2062 if (G_UNLIKELY(!fc
->in_ir
|| msg_it
->dry_run
)) {
2066 field
= stack_top(msg_it
->stack
)->base
;
2067 BT_ASSERT_DBG(field
);
2069 /* Append current substring */
2070 ret
= bt_field_string_append_with_length(field
, value
, len
);
2072 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
2073 "Cannot append substring to string field's value: "
2074 "msg-it-addr=%p, field-addr=%p, string-length=%zu, "
2076 msg_it
, field
, len
, ret
);
2077 status
= BT_BFCR_STATUS_ERROR
;
2085 static enum bt_bfcr_status
bfcr_string_end_cb(struct ctf_field_class
*fc
, void *data
)
2087 ctf_msg_iter
*msg_it
= (ctf_msg_iter
*) data
;
2089 BT_COMP_LOGT("String (end) function called from BFCR: "
2090 "msg-it-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2091 "fc-type=%d, fc-in-ir=%d",
2092 msg_it
, msg_it
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
2094 if (G_UNLIKELY(!fc
->in_ir
|| msg_it
->dry_run
)) {
2098 /* Pop string field */
2099 stack_pop(msg_it
->stack
);
2101 /* Go to next field */
2102 stack_top(msg_it
->stack
)->index
++;
2105 return BT_BFCR_STATUS_OK
;
2108 static enum bt_bfcr_status
bfcr_compound_begin_cb(struct ctf_field_class
*fc
, void *data
)
2110 ctf_msg_iter
*msg_it
= (ctf_msg_iter
*) data
;
2113 BT_COMP_LOGT("Compound (beginning) function called from BFCR: "
2114 "msg-it-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2115 "fc-type=%d, fc-in-ir=%d",
2116 msg_it
, msg_it
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
2118 if (G_UNLIKELY(!fc
->in_ir
|| msg_it
->dry_run
)) {
2123 if (stack_empty(msg_it
->stack
)) {
2124 /* Root: already set by read_dscope_begin_state() */
2125 field
= msg_it
->cur_dscope_field
;
2127 field
= borrow_next_field(msg_it
);
2128 BT_ASSERT_DBG(field
);
2132 BT_ASSERT_DBG(field
);
2133 BT_ASSERT_DBG(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
2134 stack_push(msg_it
->stack
, field
);
2137 * Change BFCR "unsigned int" callback if it's a text
2140 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
|| fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
2141 ctf_field_class_array_base
*array_fc
= ctf_field_class_as_array_base(fc
);
2143 if (array_fc
->is_text
) {
2144 BT_ASSERT_DBG(bt_field_get_class_type(field
) == BT_FIELD_CLASS_TYPE_STRING
);
2145 msg_it
->done_filling_string
= false;
2146 bt_field_string_clear(field
);
2147 bt_bfcr_set_unsigned_int_cb(msg_it
->bfcr
, bfcr_unsigned_int_char_cb
);
2152 return BT_BFCR_STATUS_OK
;
2155 static enum bt_bfcr_status
bfcr_compound_end_cb(struct ctf_field_class
*fc
, void *data
)
2157 ctf_msg_iter
*msg_it
= (ctf_msg_iter
*) data
;
2159 BT_COMP_LOGT("Compound (end) function called from BFCR: "
2160 "msg-it-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2161 "fc-type=%d, fc-in-ir=%d",
2162 msg_it
, msg_it
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
2164 if (G_UNLIKELY(!fc
->in_ir
|| msg_it
->dry_run
)) {
2168 BT_ASSERT_DBG(!stack_empty(msg_it
->stack
));
2169 BT_ASSERT_DBG(bt_field_borrow_class_const(stack_top(msg_it
->stack
)->base
) == fc
->ir_fc
);
2172 * Reset BFCR "unsigned int" callback if it's a text
2175 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
|| fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
2176 ctf_field_class_array_base
*array_fc
= ctf_field_class_as_array_base(fc
);
2178 if (array_fc
->is_text
) {
2179 BT_ASSERT_DBG(bt_field_get_class_type(stack_top(msg_it
->stack
)->base
) ==
2180 BT_FIELD_CLASS_TYPE_STRING
);
2181 bt_bfcr_set_unsigned_int_cb(msg_it
->bfcr
, bfcr_unsigned_int_cb
);
2186 stack_pop(msg_it
->stack
);
2188 /* If the stack is not empty, increment the base's index */
2189 if (!stack_empty(msg_it
->stack
)) {
2190 stack_top(msg_it
->stack
)->index
++;
2194 return BT_BFCR_STATUS_OK
;
2197 static int64_t bfcr_get_sequence_length_cb(struct ctf_field_class
*fc
, void *data
)
2199 bt_field
*seq_field
;
2200 ctf_msg_iter
*msg_it
= (ctf_msg_iter
*) data
;
2201 bt_self_component
*self_comp
= msg_it
->self_comp
;
2202 struct ctf_field_class_sequence
*seq_fc
= ctf_field_class_as_sequence(fc
);
2207 (uint64_t) bt_g_array_index(msg_it
->stored_values
, uint64_t, seq_fc
->stored_length_index
);
2209 if (G_UNLIKELY(msg_it
->dry_run
)) {
2213 seq_field
= stack_top(msg_it
->stack
)->base
;
2214 BT_ASSERT_DBG(seq_field
);
2217 * bfcr_get_sequence_length_cb() also gets called back for a
2218 * text sequence, but the destination field is a string field.
2219 * Only set the field's sequence length if the destination field
2220 * is a sequence field.
2222 if (!seq_fc
->base
.is_text
) {
2223 BT_ASSERT_DBG(bt_field_class_type_is(bt_field_get_class_type(seq_field
),
2224 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
));
2225 ret
= bt_field_array_dynamic_set_length(seq_field
, (uint64_t) length
);
2227 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
2228 "Cannot set dynamic array field's length field: "
2229 "msg-it-addr=%p, field-addr=%p, "
2231 msg_it
, seq_field
, length
);
2240 static struct ctf_field_class
*
2241 bfcr_borrow_variant_selected_field_class_cb(struct ctf_field_class
*fc
, void *data
)
2245 int64_t option_index
= -1;
2246 ctf_msg_iter
*msg_it
= (ctf_msg_iter
*) data
;
2247 ctf_field_class_variant
*var_fc
= ctf_field_class_as_variant(fc
);
2248 struct ctf_named_field_class
*selected_option
= NULL
;
2249 bt_self_component
*self_comp
= msg_it
->self_comp
;
2250 struct ctf_field_class
*ret_fc
= NULL
;
2257 /* Get variant's tag */
2258 tag
.u
= bt_g_array_index(msg_it
->stored_values
, uint64_t, var_fc
->stored_tag_index
);
2261 * Check each range to find the selected option's index.
2263 if (var_fc
->tag_fc
->base
.is_signed
) {
2264 for (i
= 0; i
< var_fc
->ranges
->len
; i
++) {
2265 struct ctf_field_class_variant_range
*range
=
2266 ctf_field_class_variant_borrow_range_by_index(var_fc
, i
);
2268 if (tag
.i
>= range
->range
.lower
.i
&& tag
.i
<= range
->range
.upper
.i
) {
2269 option_index
= (int64_t) range
->option_index
;
2274 for (i
= 0; i
< var_fc
->ranges
->len
; i
++) {
2275 struct ctf_field_class_variant_range
*range
=
2276 ctf_field_class_variant_borrow_range_by_index(var_fc
, i
);
2278 if (tag
.u
>= range
->range
.lower
.u
&& tag
.u
<= range
->range
.upper
.u
) {
2279 option_index
= (int64_t) range
->option_index
;
2285 if (option_index
< 0) {
2286 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
2287 "Cannot find variant field class's option: "
2288 "msg-it-addr=%p, var-fc-addr=%p, u-tag=%" PRIu64
", "
2290 msg_it
, var_fc
, tag
.u
, tag
.i
);
2296 ctf_field_class_variant_borrow_option_by_index(var_fc
, (uint64_t) option_index
);
2298 if (selected_option
->fc
->in_ir
&& !msg_it
->dry_run
) {
2299 bt_field
*var_field
= stack_top(msg_it
->stack
)->base
;
2301 ret
= bt_field_variant_select_option_by_index(var_field
, option_index
);
2303 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
2304 "Cannot select variant field's option field: "
2305 "msg-it-addr=%p, var-field-addr=%p, "
2306 "opt-index=%" PRId64
,
2307 msg_it
, var_field
, option_index
);
2313 ret_fc
= selected_option
->fc
;
2319 static bt_message
*create_msg_stream_beginning(struct ctf_msg_iter
*msg_it
)
2321 bt_self_component
*self_comp
= msg_it
->self_comp
;
2324 BT_ASSERT(msg_it
->stream
);
2325 BT_ASSERT(msg_it
->self_msg_iter
);
2326 msg
= bt_message_stream_beginning_create(msg_it
->self_msg_iter
, msg_it
->stream
);
2328 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
2329 "Cannot create stream beginning message: "
2330 "msg-it-addr=%p, stream-addr=%p",
2331 msg_it
, msg_it
->stream
);
2337 static bt_message
*create_msg_stream_end(struct ctf_msg_iter
*msg_it
)
2339 bt_self_component
*self_comp
= msg_it
->self_comp
;
2342 if (!msg_it
->stream
) {
2343 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
2344 "Cannot create stream end message because stream is NULL: "
2351 BT_ASSERT(msg_it
->self_msg_iter
);
2352 msg
= bt_message_stream_end_create(msg_it
->self_msg_iter
, msg_it
->stream
);
2354 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
2355 "Cannot create stream end message: "
2356 "msg-it-addr=%p, stream-addr=%p",
2357 msg_it
, msg_it
->stream
);
2364 static bt_message
*create_msg_packet_beginning(struct ctf_msg_iter
*msg_it
, bool use_default_cs
)
2366 bt_self_component
*self_comp
= msg_it
->self_comp
;
2368 const bt_stream_class
*sc
= msg_it
->meta
.sc
->ir_sc
;
2370 BT_ASSERT(msg_it
->packet
);
2372 BT_ASSERT(msg_it
->self_msg_iter
);
2374 if (msg_it
->meta
.sc
->packets_have_ts_begin
) {
2375 BT_ASSERT(msg_it
->snapshots
.beginning_clock
!= UINT64_C(-1));
2376 uint64_t raw_cs_value
;
2379 * Either use the decoded packet `timestamp_begin` field or the
2380 * current stream's default clock_snapshot.
2382 if (use_default_cs
) {
2383 raw_cs_value
= msg_it
->default_clock_snapshot
;
2385 raw_cs_value
= msg_it
->snapshots
.beginning_clock
;
2388 msg
= bt_message_packet_beginning_create_with_default_clock_snapshot(
2389 msg_it
->self_msg_iter
, msg_it
->packet
, raw_cs_value
);
2391 msg
= bt_message_packet_beginning_create(msg_it
->self_msg_iter
, msg_it
->packet
);
2395 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
2396 "Cannot create packet beginning message: "
2397 "msg-it-addr=%p, packet-addr=%p",
2398 msg_it
, msg_it
->packet
);
2406 static bt_message
*emit_delayed_packet_beg_msg(struct ctf_msg_iter
*msg_it
)
2408 bool packet_beg_ts_need_fix_up
;
2410 msg_it
->emit_delayed_packet_beginning_msg
= false;
2413 * Only fix the packet's timestamp_begin if it's larger than the first
2414 * event of the packet. If there was no event in the packet, the
2415 * `default_clock_snapshot` field will be either equal or greater than
2416 * `snapshots.beginning_clock` so there is not fix needed.
2418 packet_beg_ts_need_fix_up
= msg_it
->default_clock_snapshot
< msg_it
->snapshots
.beginning_clock
;
2420 /* create_msg_packet_beginning() logs errors */
2421 return create_msg_packet_beginning(msg_it
, packet_beg_ts_need_fix_up
);
2424 static bt_message
*create_msg_packet_end(struct ctf_msg_iter
*msg_it
)
2427 bool update_default_cs
= true;
2428 bt_self_component
*self_comp
= msg_it
->self_comp
;
2430 if (!msg_it
->packet
) {
2436 * Check if we need to emit the delayed packet
2437 * beginning message instead of the packet end message.
2439 if (G_UNLIKELY(msg_it
->emit_delayed_packet_beginning_msg
)) {
2440 msg
= emit_delayed_packet_beg_msg(msg_it
);
2441 /* Don't forget to emit the packet end message. */
2442 msg_it
->state
= STATE_EMIT_QUEUED_MSG_PACKET_END
;
2446 /* Check if may be affected by lttng-crash timestamp_end quirk. */
2447 if (G_UNLIKELY(msg_it
->meta
.tc
->quirks
.lttng_crash
)) {
2449 * Check if the `timestamp_begin` field is non-zero but
2450 * `timestamp_end` is zero. It means the trace is affected by
2451 * the lttng-crash packet `timestamp_end` quirk and must be
2452 * fixed up by omitting to update the default clock snapshot to
2453 * the `timestamp_end` as is typically done.
2455 if (msg_it
->snapshots
.beginning_clock
!= 0 && msg_it
->snapshots
.end_clock
== 0) {
2456 update_default_cs
= false;
2461 * Check if may be affected by lttng event-after-packet `timestamp_end`
2464 if (msg_it
->meta
.tc
->quirks
.lttng_event_after_packet
) {
2466 * Check if `timestamp_end` is smaller then the current
2467 * default_clock_snapshot (which is set to the last event
2468 * decoded). It means the trace is affected by the lttng
2469 * `event-after-packet` packet `timestamp_end` quirk and must
2470 * be fixed up by omitting to update the default clock snapshot
2471 * to the `timestamp_end` as is typically done.
2473 if (msg_it
->snapshots
.end_clock
< msg_it
->default_clock_snapshot
) {
2474 update_default_cs
= false;
2478 /* Update default clock from packet's end time. */
2479 if (msg_it
->snapshots
.end_clock
!= UINT64_C(-1) && update_default_cs
) {
2480 msg_it
->default_clock_snapshot
= msg_it
->snapshots
.end_clock
;
2483 BT_ASSERT(msg_it
->self_msg_iter
);
2485 if (msg_it
->meta
.sc
->packets_have_ts_end
) {
2486 BT_ASSERT(msg_it
->snapshots
.end_clock
!= UINT64_C(-1));
2487 msg
= bt_message_packet_end_create_with_default_clock_snapshot(
2488 msg_it
->self_msg_iter
, msg_it
->packet
, msg_it
->default_clock_snapshot
);
2490 msg
= bt_message_packet_end_create(msg_it
->self_msg_iter
, msg_it
->packet
);
2494 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
2495 "Cannot create packet end message: "
2496 "msg-it-addr=%p, packet-addr=%p",
2497 msg_it
, msg_it
->packet
);
2501 BT_PACKET_PUT_REF_AND_RESET(msg_it
->packet
);
2507 static bt_message
*create_msg_discarded_events(struct ctf_msg_iter
*msg_it
)
2510 bt_self_component
*self_comp
= msg_it
->self_comp
;
2511 uint64_t beginning_raw_value
= UINT64_C(-1);
2512 uint64_t end_raw_value
= UINT64_C(-1);
2514 BT_ASSERT(msg_it
->self_msg_iter
);
2515 BT_ASSERT(msg_it
->stream
);
2516 BT_ASSERT(msg_it
->meta
.sc
->has_discarded_events
);
2518 if (msg_it
->meta
.sc
->discarded_events_have_default_cs
) {
2519 if (msg_it
->prev_packet_snapshots
.discarded_events
== UINT64_C(-1)) {
2521 * We discarded events, but before (and possibly
2522 * including) the current packet: use this packet's time
2523 * range, and do not have a specific count.
2525 beginning_raw_value
= msg_it
->snapshots
.beginning_clock
;
2526 end_raw_value
= msg_it
->snapshots
.end_clock
;
2528 beginning_raw_value
= msg_it
->prev_packet_snapshots
.end_clock
;
2529 end_raw_value
= msg_it
->snapshots
.end_clock
;
2532 BT_ASSERT(beginning_raw_value
!= UINT64_C(-1));
2533 BT_ASSERT(end_raw_value
!= UINT64_C(-1));
2534 msg
= bt_message_discarded_events_create_with_default_clock_snapshots(
2535 msg_it
->self_msg_iter
, msg_it
->stream
, beginning_raw_value
, end_raw_value
);
2537 msg
= bt_message_discarded_events_create(msg_it
->self_msg_iter
, msg_it
->stream
);
2541 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
2542 "Cannot create discarded events message: "
2543 "msg-it-addr=%p, stream-addr=%p",
2544 msg_it
, msg_it
->stream
);
2548 if (msg_it
->prev_packet_snapshots
.discarded_events
!= UINT64_C(-1)) {
2549 bt_message_discarded_events_set_count(msg
,
2550 msg_it
->snapshots
.discarded_events
-
2551 msg_it
->prev_packet_snapshots
.discarded_events
);
2558 static bt_message
*create_msg_discarded_packets(struct ctf_msg_iter
*msg_it
)
2561 bt_self_component
*self_comp
= msg_it
->self_comp
;
2563 BT_ASSERT(msg_it
->self_msg_iter
);
2564 BT_ASSERT(msg_it
->stream
);
2565 BT_ASSERT(msg_it
->meta
.sc
->has_discarded_packets
);
2566 BT_ASSERT(msg_it
->prev_packet_snapshots
.packets
!= UINT64_C(-1));
2568 if (msg_it
->meta
.sc
->discarded_packets_have_default_cs
) {
2569 BT_ASSERT(msg_it
->prev_packet_snapshots
.end_clock
!= UINT64_C(-1));
2570 BT_ASSERT(msg_it
->snapshots
.beginning_clock
!= UINT64_C(-1));
2571 msg
= bt_message_discarded_packets_create_with_default_clock_snapshots(
2572 msg_it
->self_msg_iter
, msg_it
->stream
, msg_it
->prev_packet_snapshots
.end_clock
,
2573 msg_it
->snapshots
.beginning_clock
);
2575 msg
= bt_message_discarded_packets_create(msg_it
->self_msg_iter
, msg_it
->stream
);
2579 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
2580 "Cannot create discarded packets message: "
2581 "msg-it-addr=%p, stream-addr=%p",
2582 msg_it
, msg_it
->stream
);
2586 bt_message_discarded_packets_set_count(msg
, msg_it
->snapshots
.packets
-
2587 msg_it
->prev_packet_snapshots
.packets
- 1);
2593 struct ctf_msg_iter
*ctf_msg_iter_create(struct ctf_trace_class
*tc
, size_t max_request_sz
,
2594 struct ctf_msg_iter_medium_ops medops
, void *data
,
2595 bt_logging_level log_level
, bt_self_component
*self_comp
,
2596 bt_self_message_iterator
*self_msg_iter
)
2598 struct ctf_msg_iter
*msg_it
= NULL
;
2599 struct bt_bfcr_cbs cbs
= {
2602 .signed_int
= bfcr_signed_int_cb
,
2603 .unsigned_int
= bfcr_unsigned_int_cb
,
2604 .floating_point
= bfcr_floating_point_cb
,
2605 .string_begin
= bfcr_string_begin_cb
,
2606 .string
= bfcr_string_cb
,
2607 .string_end
= bfcr_string_end_cb
,
2608 .compound_begin
= bfcr_compound_begin_cb
,
2609 .compound_end
= bfcr_compound_end_cb
,
2613 .get_sequence_length
= bfcr_get_sequence_length_cb
,
2614 .borrow_variant_selected_field_class
= bfcr_borrow_variant_selected_field_class_cb
,
2619 BT_ASSERT(medops
.request_bytes
);
2620 BT_ASSERT(medops
.borrow_stream
);
2621 BT_ASSERT(max_request_sz
> 0);
2623 BT_COMP_LOG_CUR_LVL(BT_LOG_DEBUG
, log_level
, self_comp
,
2624 "Creating CTF plugin message iterator: "
2625 "trace-addr=%p, max-request-size=%zu, "
2626 "data=%p, log-level=%s",
2627 tc
, max_request_sz
, data
, bt_common_logging_level_string(log_level
));
2628 msg_it
= g_new0(struct ctf_msg_iter
, 1);
2630 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_comp
,
2631 "Failed to allocate one CTF plugin message iterator.");
2634 msg_it
->self_comp
= self_comp
;
2635 msg_it
->self_msg_iter
= self_msg_iter
;
2636 msg_it
->log_level
= log_level
;
2637 msg_it
->meta
.tc
= tc
;
2638 msg_it
->medium
.medops
= medops
;
2639 msg_it
->medium
.max_request_sz
= max_request_sz
;
2640 msg_it
->medium
.data
= data
;
2641 msg_it
->stack
= stack_new(msg_it
);
2642 msg_it
->stored_values
= g_array_new(FALSE
, TRUE
, sizeof(uint64_t));
2643 g_array_set_size(msg_it
->stored_values
, tc
->stored_value_count
);
2645 if (!msg_it
->stack
) {
2646 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to create field stack.");
2650 msg_it
->bfcr
= bt_bfcr_create(cbs
, msg_it
, log_level
, NULL
);
2651 if (!msg_it
->bfcr
) {
2652 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to create binary class reader (BFCR).");
2656 ctf_msg_iter_reset(msg_it
);
2657 BT_COMP_LOGD("Created CTF plugin message iterator: "
2658 "trace-addr=%p, max-request-size=%zu, "
2659 "data=%p, msg-it-addr=%p, log-level=%s",
2660 tc
, max_request_sz
, data
, msg_it
, bt_common_logging_level_string(log_level
));
2661 msg_it
->cur_packet_offset
= 0;
2667 ctf_msg_iter_destroy(msg_it
);
2672 void ctf_msg_iter_destroy(struct ctf_msg_iter
*msg_it
)
2674 BT_PACKET_PUT_REF_AND_RESET(msg_it
->packet
);
2675 BT_STREAM_PUT_REF_AND_RESET(msg_it
->stream
);
2676 release_all_dscopes(msg_it
);
2678 BT_COMP_LOGD("Destroying CTF plugin message iterator: addr=%p", msg_it
);
2680 if (msg_it
->stack
) {
2681 BT_COMP_LOGD_STR("Destroying field stack.");
2682 stack_destroy(msg_it
->stack
);
2686 BT_COMP_LOGD("Destroying BFCR: bfcr-addr=%p", msg_it
->bfcr
);
2687 bt_bfcr_destroy(msg_it
->bfcr
);
2690 if (msg_it
->stored_values
) {
2691 g_array_free(msg_it
->stored_values
, TRUE
);
2697 enum ctf_msg_iter_status
ctf_msg_iter_get_next_message(struct ctf_msg_iter
*msg_it
,
2698 const bt_message
**message
)
2700 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
2701 bt_self_component
*self_comp
= msg_it
->self_comp
;
2703 BT_ASSERT_DBG(msg_it
);
2704 BT_ASSERT_DBG(message
);
2705 BT_COMP_LOGD("Getting next message: msg-it-addr=%p", msg_it
);
2708 status
= handle_state(msg_it
);
2709 if (G_UNLIKELY(status
== CTF_MSG_ITER_STATUS_AGAIN
)) {
2710 BT_COMP_LOGD_STR("Medium returned CTF_MSG_ITER_STATUS_AGAIN.");
2712 } else if (G_UNLIKELY(status
!= CTF_MSG_ITER_STATUS_OK
)) {
2713 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Cannot handle state: msg-it-addr=%p, state=%s",
2714 msg_it
, state_string(msg_it
->state
));
2718 switch (msg_it
->state
) {
2719 case STATE_EMIT_MSG_EVENT
:
2720 BT_ASSERT_DBG(msg_it
->event_msg
);
2723 * Check if we need to emit the delayed packet
2724 * beginning message instead of the event message.
2726 if (G_UNLIKELY(msg_it
->emit_delayed_packet_beginning_msg
)) {
2727 *message
= emit_delayed_packet_beg_msg(msg_it
);
2729 status
= CTF_MSG_ITER_STATUS_ERROR
;
2733 * Don't forget to emit the event message of
2734 * the event record that was just decoded.
2736 msg_it
->state
= STATE_EMIT_QUEUED_MSG_EVENT
;
2739 *message
= msg_it
->event_msg
;
2740 msg_it
->event_msg
= NULL
;
2743 case STATE_EMIT_MSG_DISCARDED_EVENTS
:
2744 /* create_msg_discared_events() logs errors */
2745 *message
= create_msg_discarded_events(msg_it
);
2748 status
= CTF_MSG_ITER_STATUS_ERROR
;
2752 case STATE_EMIT_MSG_DISCARDED_PACKETS
:
2753 /* create_msg_discared_packets() logs errors */
2754 *message
= create_msg_discarded_packets(msg_it
);
2757 status
= CTF_MSG_ITER_STATUS_ERROR
;
2761 case STATE_EMIT_MSG_PACKET_BEGINNING
:
2762 if (G_UNLIKELY(msg_it
->meta
.tc
->quirks
.barectf_event_before_packet
)) {
2763 msg_it
->emit_delayed_packet_beginning_msg
= true;
2765 * There is no message to return yet as this
2766 * packet beginning message is delayed until we
2767 * decode the first event message of the
2772 /* create_msg_packet_beginning() logs errors */
2773 *message
= create_msg_packet_beginning(msg_it
, false);
2775 status
= CTF_MSG_ITER_STATUS_ERROR
;
2780 case STATE_EMIT_MSG_PACKET_END_SINGLE
:
2781 case STATE_EMIT_MSG_PACKET_END_MULTI
:
2782 /* create_msg_packet_end() logs errors */
2783 *message
= create_msg_packet_end(msg_it
);
2786 status
= CTF_MSG_ITER_STATUS_ERROR
;
2790 case STATE_EMIT_MSG_STREAM_BEGINNING
:
2791 /* create_msg_stream_beginning() logs errors */
2792 *message
= create_msg_stream_beginning(msg_it
);
2793 msg_it
->emit_stream_beginning_message
= false;
2794 msg_it
->emit_stream_end_message
= true;
2797 status
= CTF_MSG_ITER_STATUS_ERROR
;
2801 case STATE_EMIT_MSG_STREAM_END
:
2802 /* create_msg_stream_end() logs errors */
2803 *message
= create_msg_stream_end(msg_it
);
2804 msg_it
->emit_stream_end_message
= false;
2807 status
= CTF_MSG_ITER_STATUS_ERROR
;
2812 status
= CTF_MSG_ITER_STATUS_EOF
;
2815 /* Non-emitting state: continue */
2824 static enum ctf_msg_iter_status
decode_until_state(struct ctf_msg_iter
*msg_it
,
2825 enum state target_state_1
,
2826 enum state target_state_2
)
2828 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
2829 bt_self_component
*self_comp
= msg_it
->self_comp
;
2831 BT_ASSERT_DBG(msg_it
);
2835 * Check if we reached the state at which we want to stop
2838 if (msg_it
->state
== target_state_1
|| msg_it
->state
== target_state_2
) {
2842 status
= handle_state(msg_it
);
2843 if (G_UNLIKELY(status
== CTF_MSG_ITER_STATUS_AGAIN
)) {
2844 BT_COMP_LOGD_STR("Medium returned CTF_MSG_ITER_STATUS_AGAIN.");
2846 } else if (G_UNLIKELY(status
!= CTF_MSG_ITER_STATUS_OK
)) {
2847 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Cannot handle state: msg-it-addr=%p, state=%s",
2848 msg_it
, state_string(msg_it
->state
));
2852 switch (msg_it
->state
) {
2854 case STATE_SWITCH_PACKET
:
2855 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
2856 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
2857 case STATE_AFTER_TRACE_PACKET_HEADER
:
2858 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
2859 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
2860 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
2861 case STATE_EMIT_MSG_STREAM_BEGINNING
:
2862 case STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS
:
2863 case STATE_EMIT_MSG_DISCARDED_EVENTS
:
2864 case STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
:
2865 case STATE_EMIT_MSG_DISCARDED_PACKETS
:
2866 case STATE_EMIT_MSG_PACKET_BEGINNING
:
2867 case STATE_DSCOPE_EVENT_HEADER_BEGIN
:
2868 case STATE_DSCOPE_EVENT_HEADER_CONTINUE
:
2869 case STATE_AFTER_EVENT_HEADER
:
2870 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
:
2871 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
:
2872 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
:
2873 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
:
2874 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
2875 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
2876 case STATE_EMIT_MSG_EVENT
:
2877 case STATE_EMIT_QUEUED_MSG_EVENT
:
2878 case STATE_SKIP_PACKET_PADDING
:
2879 case STATE_EMIT_MSG_PACKET_END_MULTI
:
2880 case STATE_EMIT_MSG_PACKET_END_SINGLE
:
2881 case STATE_EMIT_QUEUED_MSG_PACKET_END
:
2882 case STATE_EMIT_MSG_STREAM_END
:
2887 /* We should never get to the STATE_DONE state. */
2888 BT_COMP_LOGF("Unexpected state: msg-it-addr=%p, state=%s", msg_it
,
2889 state_string(msg_it
->state
));
2898 static enum ctf_msg_iter_status
read_packet_header_context_fields(struct ctf_msg_iter
*msg_it
)
2901 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
2903 status
= decode_until_state(msg_it
, STATE_EMIT_MSG_PACKET_BEGINNING
, (state
) -1);
2904 if (status
!= CTF_MSG_ITER_STATUS_OK
) {
2908 ret
= set_current_packet_content_sizes(msg_it
);
2910 status
= CTF_MSG_ITER_STATUS_ERROR
;
2918 enum ctf_msg_iter_status
ctf_msg_iter_seek(struct ctf_msg_iter
*msg_it
, off_t offset
)
2920 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
2921 enum ctf_msg_iter_medium_status medium_status
;
2924 BT_ASSERT(offset
>= 0);
2925 BT_ASSERT(msg_it
->medium
.medops
.seek
);
2927 medium_status
= msg_it
->medium
.medops
.seek(offset
, msg_it
->medium
.data
);
2928 if (medium_status
!= CTF_MSG_ITER_MEDIUM_STATUS_OK
) {
2929 if (medium_status
== CTF_MSG_ITER_MEDIUM_STATUS_EOF
) {
2930 status
= CTF_MSG_ITER_STATUS_EOF
;
2932 status
= CTF_MSG_ITER_STATUS_ERROR
;
2937 ctf_msg_iter_reset(msg_it
);
2938 msg_it
->cur_packet_offset
= offset
;
2944 static enum ctf_msg_iter_status
clock_snapshot_at_msg_iter_state(struct ctf_msg_iter
*msg_it
,
2945 enum state target_state_1
,
2946 enum state target_state_2
,
2947 uint64_t *clock_snapshot
)
2949 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
2951 BT_ASSERT_DBG(msg_it
);
2952 BT_ASSERT_DBG(clock_snapshot
);
2953 status
= decode_until_state(msg_it
, target_state_1
, target_state_2
);
2954 if (status
!= CTF_MSG_ITER_STATUS_OK
) {
2958 *clock_snapshot
= msg_it
->default_clock_snapshot
;
2963 enum ctf_msg_iter_status
2964 ctf_msg_iter_curr_packet_first_event_clock_snapshot(struct ctf_msg_iter
*msg_it
,
2965 uint64_t *first_clock_snapshot
)
2967 return clock_snapshot_at_msg_iter_state(msg_it
, STATE_AFTER_EVENT_HEADER
, (state
) -1,
2968 first_clock_snapshot
);
2971 enum ctf_msg_iter_status
2972 ctf_msg_iter_curr_packet_last_event_clock_snapshot(struct ctf_msg_iter
*msg_it
,
2973 uint64_t *last_clock_snapshot
)
2975 return clock_snapshot_at_msg_iter_state(msg_it
, STATE_EMIT_MSG_PACKET_END_SINGLE
,
2976 STATE_EMIT_MSG_PACKET_END_MULTI
, last_clock_snapshot
);
2979 enum ctf_msg_iter_status
2980 ctf_msg_iter_get_packet_properties(struct ctf_msg_iter
*msg_it
,
2981 struct ctf_msg_iter_packet_properties
*props
)
2983 enum ctf_msg_iter_status status
;
2985 BT_ASSERT_DBG(msg_it
);
2986 BT_ASSERT_DBG(props
);
2987 status
= read_packet_header_context_fields(msg_it
);
2988 if (status
!= CTF_MSG_ITER_STATUS_OK
) {
2992 props
->exp_packet_total_size
= msg_it
->cur_exp_packet_total_size
;
2993 props
->exp_packet_content_size
= msg_it
->cur_exp_packet_content_size
;
2994 props
->stream_class_id
= (uint64_t) msg_it
->cur_stream_class_id
;
2995 props
->data_stream_id
= msg_it
->cur_data_stream_id
;
2996 props
->snapshots
.discarded_events
= msg_it
->snapshots
.discarded_events
;
2997 props
->snapshots
.packets
= msg_it
->snapshots
.packets
;
2998 props
->snapshots
.beginning_clock
= msg_it
->snapshots
.beginning_clock
;
2999 props
->snapshots
.end_clock
= msg_it
->snapshots
.end_clock
;
3005 void ctf_msg_iter_set_dry_run(struct ctf_msg_iter
*msg_it
, bool val
)
3007 msg_it
->dry_run
= val
;