2 * Babeltrace - CTF message iterator
4 * Copyright (c) 2015-2018 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2015-2018 Philippe Proulx <pproulx@efficios.com>
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 #define BT_COMP_LOG_SELF_COMP (msg_it->self_comp)
27 #define BT_LOG_OUTPUT_LEVEL (msg_it->log_level)
28 #define BT_LOG_TAG "PLUGIN/CTF/MSG-ITER"
29 #include "logging/comp-logging.h"
36 #include "common/assert.h"
38 #include <babeltrace2/babeltrace.h>
39 #include "common/common.h"
44 #include "../bfcr/bfcr.h"
48 /* A visit stack entry */
51 * Current base field, one of:
63 /* Index of next field to set */
71 struct ctf_msg_iter
*msg_it
;
73 /* Entries (struct stack_entry) */
76 /* Number of active entries */
84 STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
,
85 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
86 STATE_AFTER_TRACE_PACKET_HEADER
,
87 STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
,
88 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
89 STATE_AFTER_STREAM_PACKET_CONTEXT
,
90 STATE_CHECK_EMIT_MSG_STREAM_BEGINNING
,
91 STATE_EMIT_MSG_STREAM_BEGINNING
,
92 STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS
,
93 STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
,
94 STATE_EMIT_MSG_DISCARDED_EVENTS
,
95 STATE_EMIT_MSG_DISCARDED_PACKETS
,
96 STATE_EMIT_MSG_PACKET_BEGINNING
,
97 STATE_DSCOPE_EVENT_HEADER_BEGIN
,
98 STATE_DSCOPE_EVENT_HEADER_CONTINUE
,
99 STATE_AFTER_EVENT_HEADER
,
100 STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
,
101 STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
,
102 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
,
103 STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
,
104 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
105 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
106 STATE_EMIT_MSG_EVENT
,
107 STATE_EMIT_QUEUED_MSG_EVENT
,
108 STATE_SKIP_PACKET_PADDING
,
109 STATE_EMIT_MSG_PACKET_END_MULTI
,
110 STATE_EMIT_MSG_PACKET_END_SINGLE
,
111 STATE_CHECK_EMIT_MSG_STREAM_END
,
112 STATE_EMIT_QUEUED_MSG_PACKET_END
,
113 STATE_EMIT_MSG_STREAM_END
,
117 struct end_of_packet_snapshots
{
118 uint64_t discarded_events
;
120 uint64_t beginning_clock
;
124 /* CTF message iterator */
125 struct ctf_msg_iter
{
129 /* Current message iterator to create messages (weak) */
130 bt_self_message_iterator
*self_msg_iter
;
132 /* True to emit a stream beginning message. */
133 bool emit_stream_begin_msg
;
135 /* True to emit a stream end message. */
136 bool emit_stream_end_msg
;
139 * True if library objects are unavailable during the decoding and
140 * should not be created/used.
144 /* True to set the stream */
148 * Current dynamic scope field pointer.
150 * This is set by read_dscope_begin_state() and contains the
151 * value of one of the pointers in `dscopes` below.
153 bt_field
*cur_dscope_field
;
156 * True if we're done filling a string field from a text
157 * array/sequence payload.
159 bool done_filling_string
;
161 /* Trace and classes */
162 /* True to set IR fields */
166 struct ctf_trace_class
*tc
;
167 struct ctf_stream_class
*sc
;
168 struct ctf_event_class
*ec
;
171 /* Current packet context field wrapper (NULL if not created yet) */
172 bt_packet_context_field
*packet_context_field
;
174 /* Current packet (NULL if not created yet) */
177 /* Current stream (NULL if not set yet) */
180 /* Current event (NULL if not created yet) */
183 /* Current event message (NULL if not created yet) */
184 bt_message
*event_msg
;
187 * True if we need to emit a packet beginning message before we emit
188 * the next event message or the packet end message.
190 bool emit_delayed_packet_beginning_msg
;
192 /* Database of current dynamic scopes */
194 bt_field
*stream_packet_context
;
195 bt_field
*event_common_context
;
196 bt_field
*event_spec_context
;
197 bt_field
*event_payload
;
203 /* Current medium buffer data */
205 /* Last address provided by medium */
208 /* Buffer size provided by medium (bytes) */
211 /* Offset within whole packet of addr (bits) */
212 size_t packet_offset
;
214 /* Current position from addr (bits) */
217 /* Position of the last event header from addr (bits) */
221 /* Binary type reader */
222 struct bt_bfcr
*bfcr
;
224 /* Current medium data */
226 struct ctf_msg_iter_medium_ops medops
;
227 size_t max_request_sz
;
231 /* Current packet size (bits) (-1 if unknown) */
232 int64_t cur_exp_packet_total_size
;
234 /* Current content size (bits) (-1 if unknown) */
235 int64_t cur_exp_packet_content_size
;
237 /* Current stream class ID */
238 int64_t cur_stream_class_id
;
240 /* Current event class ID */
241 int64_t cur_event_class_id
;
243 /* Current data stream ID */
244 int64_t cur_data_stream_id
;
247 * Offset, in the underlying media, of the current packet's
248 * start (-1 if unknown).
250 off_t cur_packet_offset
;
252 /* Default clock's current value */
253 uint64_t default_clock_snapshot
;
255 /* End of current packet snapshots */
256 struct end_of_packet_snapshots snapshots
;
258 /* End of previous packet snapshots */
259 struct end_of_packet_snapshots prev_packet_snapshots
;
261 /* Stored values (for sequence lengths, variant tags) */
262 GArray
*stored_values
;
264 /* Iterator's current log level */
265 bt_logging_level log_level
;
267 /* Iterator's owning self component, or `NULL` if none (query) */
268 bt_self_component
*self_comp
;
272 const char *state_string(enum state state
)
277 case STATE_SWITCH_PACKET
:
278 return "SWITCH_PACKET";
279 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
280 return "DSCOPE_TRACE_PACKET_HEADER_BEGIN";
281 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
282 return "DSCOPE_TRACE_PACKET_HEADER_CONTINUE";
283 case STATE_AFTER_TRACE_PACKET_HEADER
:
284 return "AFTER_TRACE_PACKET_HEADER";
285 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
286 return "DSCOPE_STREAM_PACKET_CONTEXT_BEGIN";
287 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
288 return "DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE";
289 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
290 return "AFTER_STREAM_PACKET_CONTEXT";
291 case STATE_EMIT_MSG_STREAM_BEGINNING
:
292 return "EMIT_MSG_STREAM_BEGINNING";
293 case STATE_EMIT_MSG_PACKET_BEGINNING
:
294 return "EMIT_MSG_PACKET_BEGINNING";
295 case STATE_EMIT_MSG_DISCARDED_EVENTS
:
296 return "EMIT_MSG_DISCARDED_EVENTS";
297 case STATE_EMIT_MSG_DISCARDED_PACKETS
:
298 return "EMIT_MSG_DISCARDED_PACKETS";
299 case STATE_DSCOPE_EVENT_HEADER_BEGIN
:
300 return "DSCOPE_EVENT_HEADER_BEGIN";
301 case STATE_DSCOPE_EVENT_HEADER_CONTINUE
:
302 return "DSCOPE_EVENT_HEADER_CONTINUE";
303 case STATE_AFTER_EVENT_HEADER
:
304 return "AFTER_EVENT_HEADER";
305 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
:
306 return "DSCOPE_EVENT_COMMON_CONTEXT_BEGIN";
307 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
:
308 return "DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE";
309 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
:
310 return "DSCOPE_EVENT_SPEC_CONTEXT_BEGIN";
311 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
:
312 return "DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE";
313 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
314 return "DSCOPE_EVENT_PAYLOAD_BEGIN";
315 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
316 return "DSCOPE_EVENT_PAYLOAD_CONTINUE";
317 case STATE_EMIT_MSG_EVENT
:
318 return "EMIT_MSG_EVENT";
319 case STATE_EMIT_QUEUED_MSG_EVENT
:
320 return "EMIT_QUEUED_MSG_EVENT";
321 case STATE_SKIP_PACKET_PADDING
:
322 return "SKIP_PACKET_PADDING";
323 case STATE_EMIT_MSG_PACKET_END_MULTI
:
324 return "EMIT_MSG_PACKET_END_MULTI";
325 case STATE_EMIT_MSG_PACKET_END_SINGLE
:
326 return "EMIT_MSG_PACKET_END_SINGLE";
327 case STATE_EMIT_QUEUED_MSG_PACKET_END
:
328 return "EMIT_QUEUED_MSG_PACKET_END";
329 case STATE_EMIT_MSG_STREAM_END
:
330 return "EMIT_MSG_STREAM_END";
339 struct stack
*stack_new(struct ctf_msg_iter
*msg_it
)
341 bt_self_component
*self_comp
= msg_it
->self_comp
;
342 struct stack
*stack
= NULL
;
344 stack
= g_new0(struct stack
, 1);
346 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
347 "Failed to allocate one stack.");
351 stack
->msg_it
= msg_it
;
352 stack
->entries
= g_array_new(FALSE
, TRUE
, sizeof(struct stack_entry
));
353 if (!stack
->entries
) {
354 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
355 "Failed to allocate a GArray.");
359 BT_COMP_LOGD("Created stack: msg-it-addr=%p, stack-addr=%p", msg_it
, stack
);
371 void stack_destroy(struct stack
*stack
)
373 struct ctf_msg_iter
*msg_it
;
375 BT_ASSERT_DBG(stack
);
376 msg_it
= stack
->msg_it
;
377 BT_COMP_LOGD("Destroying stack: addr=%p", stack
);
379 if (stack
->entries
) {
380 g_array_free(stack
->entries
, TRUE
);
387 void stack_push(struct stack
*stack
, bt_field
*base
)
389 struct stack_entry
*entry
;
390 struct ctf_msg_iter
*msg_it
;
392 BT_ASSERT_DBG(stack
);
393 msg_it
= stack
->msg_it
;
395 BT_COMP_LOGT("Pushing base field on stack: stack-addr=%p, "
396 "stack-size-before=%zu, stack-size-after=%zu",
397 stack
, stack
->size
, stack
->size
+ 1);
399 if (stack
->entries
->len
== stack
->size
) {
400 g_array_set_size(stack
->entries
, stack
->size
+ 1);
403 entry
= &g_array_index(stack
->entries
, struct stack_entry
, stack
->size
);
410 unsigned int stack_size(struct stack
*stack
)
412 BT_ASSERT_DBG(stack
);
417 void stack_pop(struct stack
*stack
)
419 struct ctf_msg_iter
*msg_it
;
421 BT_ASSERT_DBG(stack
);
422 BT_ASSERT_DBG(stack_size(stack
));
423 msg_it
= stack
->msg_it
;
424 BT_COMP_LOGT("Popping from stack: "
425 "stack-addr=%p, stack-size-before=%zu, stack-size-after=%zu",
426 stack
, stack
->size
, stack
->size
- 1);
431 struct stack_entry
*stack_top(struct stack
*stack
)
433 BT_ASSERT_DBG(stack
);
434 BT_ASSERT_DBG(stack_size(stack
));
435 return &g_array_index(stack
->entries
, struct stack_entry
,
440 bool stack_empty(struct stack
*stack
)
442 return stack_size(stack
) == 0;
446 void stack_clear(struct stack
*stack
)
448 BT_ASSERT_DBG(stack
);
453 enum ctf_msg_iter_status
msg_iter_status_from_m_status(
454 enum ctf_msg_iter_medium_status m_status
)
456 /* They are the same */
457 return (int) m_status
;
461 size_t buf_size_bits(struct ctf_msg_iter
*msg_it
)
463 return msg_it
->buf
.sz
* 8;
467 size_t buf_available_bits(struct ctf_msg_iter
*msg_it
)
469 return buf_size_bits(msg_it
) - msg_it
->buf
.at
;
473 size_t packet_at(struct ctf_msg_iter
*msg_it
)
475 return msg_it
->buf
.packet_offset
+ msg_it
->buf
.at
;
479 void buf_consume_bits(struct ctf_msg_iter
*msg_it
, size_t incr
)
481 BT_COMP_LOGT("Advancing cursor: msg-it-addr=%p, cur-before=%zu, cur-after=%zu",
482 msg_it
, msg_it
->buf
.at
, msg_it
->buf
.at
+ incr
);
483 msg_it
->buf
.at
+= incr
;
487 enum ctf_msg_iter_status
request_medium_bytes(
488 struct ctf_msg_iter
*msg_it
)
490 bt_self_component
*self_comp
= msg_it
->self_comp
;
491 uint8_t *buffer_addr
= NULL
;
492 size_t buffer_sz
= 0;
493 enum ctf_msg_iter_medium_status m_status
;
495 BT_COMP_LOGD("Calling user function (request bytes): msg-it-addr=%p, "
496 "request-size=%zu", msg_it
, msg_it
->medium
.max_request_sz
);
497 m_status
= msg_it
->medium
.medops
.request_bytes(
498 msg_it
->medium
.max_request_sz
, &buffer_addr
,
499 &buffer_sz
, msg_it
->medium
.data
);
500 BT_COMP_LOGD("User function returned: status=%s, buf-addr=%p, buf-size=%zu",
501 ctf_msg_iter_medium_status_string(m_status
),
502 buffer_addr
, buffer_sz
);
503 if (m_status
== CTF_MSG_ITER_MEDIUM_STATUS_OK
) {
504 BT_ASSERT(buffer_sz
!= 0);
506 /* New packet offset is old one + old size (in bits) */
507 msg_it
->buf
.packet_offset
+= buf_size_bits(msg_it
);
509 /* Restart at the beginning of the new medium buffer */
511 msg_it
->buf
.last_eh_at
= SIZE_MAX
;
513 /* New medium buffer size */
514 msg_it
->buf
.sz
= buffer_sz
;
516 /* New medium buffer address */
517 msg_it
->buf
.addr
= buffer_addr
;
519 BT_COMP_LOGD("User function returned new bytes: "
520 "packet-offset=%zu, cur=%zu, size=%zu, addr=%p",
521 msg_it
->buf
.packet_offset
, msg_it
->buf
.at
,
522 msg_it
->buf
.sz
, msg_it
->buf
.addr
);
523 BT_COMP_LOGD_MEM(buffer_addr
, buffer_sz
, "Returned bytes at %p:",
525 } else if (m_status
== CTF_MSG_ITER_MEDIUM_STATUS_EOF
) {
527 * User returned end of stream: validate that we're not
528 * in the middle of a packet header, packet context, or
531 if (msg_it
->cur_exp_packet_total_size
>= 0) {
532 if (packet_at(msg_it
) ==
533 msg_it
->cur_exp_packet_total_size
) {
537 if (packet_at(msg_it
) == 0) {
541 if (msg_it
->buf
.last_eh_at
!= SIZE_MAX
&&
542 msg_it
->buf
.at
== msg_it
->buf
.last_eh_at
) {
547 /* All other states are invalid */
548 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
549 "User function returned %s, but message iterator is in an unexpected state: "
550 "state=%s, cur-packet-size=%" PRId64
", cur=%zu, "
551 "packet-cur=%zu, last-eh-at=%zu",
552 ctf_msg_iter_medium_status_string(m_status
),
553 state_string(msg_it
->state
),
554 msg_it
->cur_exp_packet_total_size
,
555 msg_it
->buf
.at
, packet_at(msg_it
),
556 msg_it
->buf
.last_eh_at
);
557 m_status
= CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
558 } else if (m_status
< 0) {
559 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "User function failed: "
560 "status=%s", ctf_msg_iter_medium_status_string(m_status
));
564 return msg_iter_status_from_m_status(m_status
);
568 enum ctf_msg_iter_status
buf_ensure_available_bits(
569 struct ctf_msg_iter
*msg_it
)
571 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
573 if (G_UNLIKELY(buf_available_bits(msg_it
) == 0)) {
575 * This _cannot_ return CTF_MSG_ITER_STATUS_OK
578 status
= request_medium_bytes(msg_it
);
585 enum ctf_msg_iter_status
read_dscope_begin_state(
586 struct ctf_msg_iter
*msg_it
,
587 struct ctf_field_class
*dscope_fc
,
588 enum state done_state
, enum state continue_state
,
589 bt_field
*dscope_field
)
591 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
592 bt_self_component
*self_comp
= msg_it
->self_comp
;
593 enum bt_bfcr_status bfcr_status
;
594 size_t consumed_bits
;
596 msg_it
->cur_dscope_field
= dscope_field
;
597 BT_COMP_LOGT("Starting BFCR: msg-it-addr=%p, bfcr-addr=%p, fc-addr=%p",
598 msg_it
, msg_it
->bfcr
, dscope_fc
);
599 consumed_bits
= bt_bfcr_start(msg_it
->bfcr
, dscope_fc
,
600 msg_it
->buf
.addr
, msg_it
->buf
.at
, packet_at(msg_it
),
601 msg_it
->buf
.sz
, &bfcr_status
);
602 BT_COMP_LOGT("BFCR consumed bits: size=%zu", consumed_bits
);
604 switch (bfcr_status
) {
605 case BT_BFCR_STATUS_OK
:
606 /* Field class was read completely */
607 BT_COMP_LOGT_STR("Field was completely decoded.");
608 msg_it
->state
= done_state
;
610 case BT_BFCR_STATUS_EOF
:
611 BT_COMP_LOGT_STR("BFCR needs more data to decode field completely.");
612 msg_it
->state
= continue_state
;
615 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
616 "BFCR failed to start: msg-it-addr=%p, bfcr-addr=%p, "
617 "status=%s", msg_it
, msg_it
->bfcr
,
618 bt_bfcr_status_string(bfcr_status
));
619 status
= CTF_MSG_ITER_STATUS_ERROR
;
623 /* Consume bits now since we know we're not in an error state */
624 buf_consume_bits(msg_it
, consumed_bits
);
631 enum ctf_msg_iter_status
read_dscope_continue_state(
632 struct ctf_msg_iter
*msg_it
, enum state done_state
)
634 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
635 bt_self_component
*self_comp
= msg_it
->self_comp
;
636 enum bt_bfcr_status bfcr_status
;
637 size_t consumed_bits
;
639 BT_COMP_LOGT("Continuing BFCR: msg-it-addr=%p, bfcr-addr=%p",
640 msg_it
, msg_it
->bfcr
);
642 status
= buf_ensure_available_bits(msg_it
);
643 if (status
!= CTF_MSG_ITER_STATUS_OK
) {
645 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
646 "Cannot ensure that buffer has at least one byte: "
647 "msg-addr=%p, status=%s",
648 msg_it
, ctf_msg_iter_status_string(status
));
650 BT_COMP_LOGT("Cannot ensure that buffer has at least one byte: "
651 "msg-addr=%p, status=%s",
652 msg_it
, ctf_msg_iter_status_string(status
));
658 consumed_bits
= bt_bfcr_continue(msg_it
->bfcr
, msg_it
->buf
.addr
,
659 msg_it
->buf
.sz
, &bfcr_status
);
660 BT_COMP_LOGT("BFCR consumed bits: size=%zu", consumed_bits
);
662 switch (bfcr_status
) {
663 case BT_BFCR_STATUS_OK
:
664 /* Type was read completely. */
665 BT_COMP_LOGT_STR("Field was completely decoded.");
666 msg_it
->state
= done_state
;
668 case BT_BFCR_STATUS_EOF
:
669 /* Stay in this continue state. */
670 BT_COMP_LOGT_STR("BFCR needs more data to decode field completely.");
673 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
674 "BFCR failed to continue: msg-it-addr=%p, bfcr-addr=%p, "
675 "status=%s", msg_it
, msg_it
->bfcr
,
676 bt_bfcr_status_string(bfcr_status
));
677 status
= CTF_MSG_ITER_STATUS_ERROR
;
681 /* Consume bits now since we know we're not in an error state. */
682 buf_consume_bits(msg_it
, consumed_bits
);
688 void release_event_dscopes(struct ctf_msg_iter
*msg_it
)
690 msg_it
->dscopes
.event_common_context
= NULL
;
691 msg_it
->dscopes
.event_spec_context
= NULL
;
692 msg_it
->dscopes
.event_payload
= NULL
;
696 void release_all_dscopes(struct ctf_msg_iter
*msg_it
)
698 msg_it
->dscopes
.stream_packet_context
= NULL
;
700 if (msg_it
->packet_context_field
) {
701 bt_packet_context_field_release(msg_it
->packet_context_field
);
702 msg_it
->packet_context_field
= NULL
;
705 release_event_dscopes(msg_it
);
709 enum ctf_msg_iter_status
switch_packet_state(struct ctf_msg_iter
*msg_it
)
711 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
712 bt_self_component
*self_comp
= msg_it
->self_comp
;
715 * We don't put the stream class here because we need to make
716 * sure that all the packets processed by the same message
717 * iterator refer to the same stream class (the first one).
721 if (msg_it
->cur_exp_packet_total_size
!= -1) {
722 msg_it
->cur_packet_offset
+= msg_it
->cur_exp_packet_total_size
;
725 BT_COMP_LOGD("Switching packet: msg-it-addr=%p, cur=%zu, "
726 "packet-offset=%" PRId64
, msg_it
, msg_it
->buf
.at
,
727 msg_it
->cur_packet_offset
);
728 stack_clear(msg_it
->stack
);
729 msg_it
->meta
.ec
= NULL
;
730 BT_PACKET_PUT_REF_AND_RESET(msg_it
->packet
);
731 BT_MESSAGE_PUT_REF_AND_RESET(msg_it
->event_msg
);
732 release_all_dscopes(msg_it
);
733 msg_it
->cur_dscope_field
= NULL
;
736 * Adjust current buffer so that addr points to the beginning of the new
739 if (msg_it
->buf
.addr
) {
740 size_t consumed_bytes
= (size_t) (msg_it
->buf
.at
/ CHAR_BIT
);
742 /* Packets are assumed to start on a byte frontier. */
743 if (msg_it
->buf
.at
% CHAR_BIT
) {
744 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
745 "Cannot switch packet: current position is not a multiple of 8: "
746 "msg-it-addr=%p, cur=%zu", msg_it
, msg_it
->buf
.at
);
747 status
= CTF_MSG_ITER_STATUS_ERROR
;
751 msg_it
->buf
.addr
+= consumed_bytes
;
752 msg_it
->buf
.sz
-= consumed_bytes
;
754 msg_it
->buf
.packet_offset
= 0;
755 BT_COMP_LOGD("Adjusted buffer: addr=%p, size=%zu",
756 msg_it
->buf
.addr
, msg_it
->buf
.sz
);
759 msg_it
->cur_exp_packet_content_size
= -1;
760 msg_it
->cur_exp_packet_total_size
= -1;
761 msg_it
->cur_stream_class_id
= -1;
762 msg_it
->cur_event_class_id
= -1;
763 msg_it
->cur_data_stream_id
= -1;
764 msg_it
->prev_packet_snapshots
= msg_it
->snapshots
;
765 msg_it
->snapshots
.discarded_events
= UINT64_C(-1);
766 msg_it
->snapshots
.packets
= UINT64_C(-1);
767 msg_it
->snapshots
.beginning_clock
= UINT64_C(-1);
768 msg_it
->snapshots
.end_clock
= UINT64_C(-1);
769 msg_it
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
776 enum ctf_msg_iter_status
read_packet_header_begin_state(
777 struct ctf_msg_iter
*msg_it
)
779 struct ctf_field_class
*packet_header_fc
= NULL
;
780 bt_self_component
*self_comp
= msg_it
->self_comp
;
781 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
784 * Make sure at least one bit is available for this packet. An
785 * empty packet is impossible. If we reach the end of the medium
786 * at this point, then it's considered the end of the stream.
788 status
= buf_ensure_available_bits(msg_it
);
790 case CTF_MSG_ITER_STATUS_OK
:
792 case CTF_MSG_ITER_STATUS_EOF
:
793 status
= CTF_MSG_ITER_STATUS_OK
;
794 msg_it
->state
= STATE_CHECK_EMIT_MSG_STREAM_END
;
800 /* Packet header class is common to the whole trace class. */
801 packet_header_fc
= msg_it
->meta
.tc
->packet_header_fc
;
802 if (!packet_header_fc
) {
803 msg_it
->state
= STATE_AFTER_TRACE_PACKET_HEADER
;
807 msg_it
->cur_stream_class_id
= -1;
808 msg_it
->cur_event_class_id
= -1;
809 msg_it
->cur_data_stream_id
= -1;
810 BT_COMP_LOGD("Decoding packet header field:"
811 "msg-it-addr=%p, trace-class-addr=%p, fc-addr=%p",
812 msg_it
, msg_it
->meta
.tc
, packet_header_fc
);
813 status
= read_dscope_begin_state(msg_it
, packet_header_fc
,
814 STATE_AFTER_TRACE_PACKET_HEADER
,
815 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
, NULL
);
817 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
818 "Cannot decode packet header field: "
819 "msg-it-addr=%p, trace-class-addr=%p, "
821 msg_it
, msg_it
->meta
.tc
, packet_header_fc
);
829 enum ctf_msg_iter_status
read_packet_header_continue_state(
830 struct ctf_msg_iter
*msg_it
)
832 return read_dscope_continue_state(msg_it
,
833 STATE_AFTER_TRACE_PACKET_HEADER
);
837 enum ctf_msg_iter_status
set_current_stream_class(struct ctf_msg_iter
*msg_it
)
839 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
840 bt_self_component
*self_comp
= msg_it
->self_comp
;
841 struct ctf_stream_class
*new_stream_class
= NULL
;
843 if (msg_it
->cur_stream_class_id
== -1) {
845 * No current stream class ID field, therefore only one
848 if (msg_it
->meta
.tc
->stream_classes
->len
!= 1) {
849 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
850 "Need exactly one stream class since there's "
851 "no stream class ID field: "
852 "msg-it-addr=%p", msg_it
);
853 status
= CTF_MSG_ITER_STATUS_ERROR
;
857 new_stream_class
= msg_it
->meta
.tc
->stream_classes
->pdata
[0];
858 msg_it
->cur_stream_class_id
= new_stream_class
->id
;
861 new_stream_class
= ctf_trace_class_borrow_stream_class_by_id(
862 msg_it
->meta
.tc
, msg_it
->cur_stream_class_id
);
863 if (!new_stream_class
) {
864 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
865 "No stream class with ID of stream class ID to use in trace class: "
866 "msg-it-addr=%p, stream-class-id=%" PRIu64
", "
867 "trace-class-addr=%p",
868 msg_it
, msg_it
->cur_stream_class_id
, msg_it
->meta
.tc
);
869 status
= CTF_MSG_ITER_STATUS_ERROR
;
873 if (msg_it
->meta
.sc
) {
874 if (new_stream_class
!= msg_it
->meta
.sc
) {
875 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
876 "Two packets refer to two different stream classes within the same packet sequence: "
877 "msg-it-addr=%p, prev-stream-class-addr=%p, "
878 "prev-stream-class-id=%" PRId64
", "
879 "next-stream-class-addr=%p, "
880 "next-stream-class-id=%" PRId64
", "
882 msg_it
, msg_it
->meta
.sc
,
885 new_stream_class
->id
,
887 status
= CTF_MSG_ITER_STATUS_ERROR
;
891 msg_it
->meta
.sc
= new_stream_class
;
894 BT_COMP_LOGD("Set current stream class: "
895 "msg-it-addr=%p, stream-class-addr=%p, "
896 "stream-class-id=%" PRId64
,
897 msg_it
, msg_it
->meta
.sc
, msg_it
->meta
.sc
->id
);
904 enum ctf_msg_iter_status
set_current_stream(struct ctf_msg_iter
*msg_it
)
906 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
907 bt_self_component
*self_comp
= msg_it
->self_comp
;
908 bt_stream
*stream
= NULL
;
910 BT_COMP_LOGD("Calling user function (get stream): msg-it-addr=%p, "
911 "stream-class-addr=%p, stream-class-id=%" PRId64
,
912 msg_it
, msg_it
->meta
.sc
,
913 msg_it
->meta
.sc
->id
);
914 stream
= msg_it
->medium
.medops
.borrow_stream(
915 msg_it
->meta
.sc
->ir_sc
, msg_it
->cur_data_stream_id
,
916 msg_it
->medium
.data
);
917 bt_stream_get_ref(stream
);
918 BT_COMP_LOGD("User function returned: stream-addr=%p", stream
);
920 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
921 "User function failed to return a stream object for the given stream class.");
922 status
= CTF_MSG_ITER_STATUS_ERROR
;
926 if (msg_it
->stream
&& stream
!= msg_it
->stream
) {
927 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
928 "User function returned a different stream than the previous one for the same sequence of packets.");
929 status
= CTF_MSG_ITER_STATUS_ERROR
;
933 BT_STREAM_MOVE_REF(msg_it
->stream
, stream
);
936 bt_stream_put_ref(stream
);
941 enum ctf_msg_iter_status
set_current_packet(struct ctf_msg_iter
*msg_it
)
943 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
944 bt_self_component
*self_comp
= msg_it
->self_comp
;
945 bt_packet
*packet
= NULL
;
947 BT_COMP_LOGD("Creating packet from stream: "
948 "msg-it-addr=%p, stream-addr=%p, "
949 "stream-class-addr=%p, "
950 "stream-class-id=%" PRId64
,
951 msg_it
, msg_it
->stream
, msg_it
->meta
.sc
,
952 msg_it
->meta
.sc
->id
);
955 BT_ASSERT(msg_it
->stream
);
956 packet
= bt_packet_create(msg_it
->stream
);
958 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
959 "Cannot create packet from stream: "
960 "msg-it-addr=%p, stream-addr=%p, "
961 "stream-class-addr=%p, "
962 "stream-class-id=%" PRId64
,
963 msg_it
, msg_it
->stream
, msg_it
->meta
.sc
,
964 msg_it
->meta
.sc
->id
);
971 BT_PACKET_PUT_REF_AND_RESET(packet
);
972 status
= CTF_MSG_ITER_STATUS_ERROR
;
975 BT_PACKET_MOVE_REF(msg_it
->packet
, packet
);
980 enum ctf_msg_iter_status
after_packet_header_state(
981 struct ctf_msg_iter
*msg_it
)
983 enum ctf_msg_iter_status status
;
985 status
= set_current_stream_class(msg_it
);
986 if (status
!= CTF_MSG_ITER_STATUS_OK
) {
990 msg_it
->state
= STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
;
997 enum ctf_msg_iter_status
read_packet_context_begin_state(
998 struct ctf_msg_iter
*msg_it
)
1000 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
1001 bt_self_component
*self_comp
= msg_it
->self_comp
;
1002 struct ctf_field_class
*packet_context_fc
;
1004 BT_ASSERT(msg_it
->meta
.sc
);
1005 packet_context_fc
= msg_it
->meta
.sc
->packet_context_fc
;
1006 if (!packet_context_fc
) {
1007 BT_COMP_LOGD("No packet packet context field class in stream class: continuing: "
1008 "msg-it-addr=%p, stream-class-addr=%p, "
1009 "stream-class-id=%" PRId64
,
1010 msg_it
, msg_it
->meta
.sc
,
1011 msg_it
->meta
.sc
->id
);
1012 msg_it
->state
= STATE_AFTER_STREAM_PACKET_CONTEXT
;
1016 BT_ASSERT(!msg_it
->packet_context_field
);
1018 if (packet_context_fc
->in_ir
&& !msg_it
->dry_run
) {
1020 * Create free packet context field from stream class.
1021 * This field is going to be moved to the packet once we
1022 * create it. We cannot create the packet now because a
1023 * packet is created from a stream, and this API must be
1024 * able to return the packet context properties without
1026 * (ctf_msg_iter_get_packet_properties()).
1028 msg_it
->packet_context_field
=
1029 bt_packet_context_field_create(
1030 msg_it
->meta
.sc
->ir_sc
);
1031 if (!msg_it
->packet_context_field
) {
1032 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1033 "Cannot create packet context field wrapper from stream class.");
1034 status
= CTF_MSG_ITER_STATUS_ERROR
;
1038 msg_it
->dscopes
.stream_packet_context
=
1039 bt_packet_context_field_borrow_field(
1040 msg_it
->packet_context_field
);
1041 BT_ASSERT(msg_it
->dscopes
.stream_packet_context
);
1044 BT_COMP_LOGD("Decoding packet context field: "
1045 "msg-it-addr=%p, stream-class-addr=%p, "
1046 "stream-class-id=%" PRId64
", fc-addr=%p",
1047 msg_it
, msg_it
->meta
.sc
,
1048 msg_it
->meta
.sc
->id
, packet_context_fc
);
1049 status
= read_dscope_begin_state(msg_it
, packet_context_fc
,
1050 STATE_AFTER_STREAM_PACKET_CONTEXT
,
1051 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
1052 msg_it
->dscopes
.stream_packet_context
);
1054 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1055 "Cannot decode packet context field: "
1056 "msg-it-addr=%p, stream-class-addr=%p, "
1057 "stream-class-id=%" PRId64
", fc-addr=%p",
1058 msg_it
, msg_it
->meta
.sc
,
1059 msg_it
->meta
.sc
->id
,
1068 enum ctf_msg_iter_status
read_packet_context_continue_state(
1069 struct ctf_msg_iter
*msg_it
)
1071 return read_dscope_continue_state(msg_it
,
1072 STATE_AFTER_STREAM_PACKET_CONTEXT
);
1076 enum ctf_msg_iter_status
set_current_packet_content_sizes(
1077 struct ctf_msg_iter
*msg_it
)
1079 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
1080 bt_self_component
*self_comp
= msg_it
->self_comp
;
1082 if (msg_it
->cur_exp_packet_total_size
== -1) {
1083 if (msg_it
->cur_exp_packet_content_size
!= -1) {
1084 msg_it
->cur_exp_packet_total_size
=
1085 msg_it
->cur_exp_packet_content_size
;
1088 if (msg_it
->cur_exp_packet_content_size
== -1) {
1089 msg_it
->cur_exp_packet_content_size
=
1090 msg_it
->cur_exp_packet_total_size
;
1094 BT_ASSERT((msg_it
->cur_exp_packet_total_size
>= 0 &&
1095 msg_it
->cur_exp_packet_content_size
>= 0) ||
1096 (msg_it
->cur_exp_packet_total_size
< 0 &&
1097 msg_it
->cur_exp_packet_content_size
< 0));
1099 if (msg_it
->cur_exp_packet_content_size
>
1100 msg_it
->cur_exp_packet_total_size
) {
1101 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1102 "Invalid packet or content size: "
1103 "content size is greater than packet size: "
1104 "msg-it-addr=%p, packet-context-field-addr=%p, "
1105 "packet-size=%" PRId64
", content-size=%" PRId64
,
1106 msg_it
, msg_it
->dscopes
.stream_packet_context
,
1107 msg_it
->cur_exp_packet_total_size
,
1108 msg_it
->cur_exp_packet_content_size
);
1109 status
= CTF_MSG_ITER_STATUS_ERROR
;
1113 BT_COMP_LOGD("Set current packet and content sizes: "
1114 "msg-it-addr=%p, packet-size=%" PRIu64
", content-size=%" PRIu64
,
1115 msg_it
, msg_it
->cur_exp_packet_total_size
,
1116 msg_it
->cur_exp_packet_content_size
);
1123 enum ctf_msg_iter_status
after_packet_context_state(struct ctf_msg_iter
*msg_it
)
1125 enum ctf_msg_iter_status status
;
1127 status
= set_current_packet_content_sizes(msg_it
);
1128 if (status
!= CTF_MSG_ITER_STATUS_OK
) {
1132 if (msg_it
->stream
) {
1134 * Stream exists, which means we already emitted at
1135 * least one packet beginning message, so the initial
1136 * stream beginning message was also emitted.
1138 msg_it
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS
;
1140 msg_it
->state
= STATE_CHECK_EMIT_MSG_STREAM_BEGINNING
;
1148 enum ctf_msg_iter_status
read_event_header_begin_state(struct ctf_msg_iter
*msg_it
)
1150 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
1151 bt_self_component
*self_comp
= msg_it
->self_comp
;
1152 struct ctf_field_class
*event_header_fc
= NULL
;
1154 /* Reset the position of the last event header */
1155 msg_it
->buf
.last_eh_at
= msg_it
->buf
.at
;
1156 msg_it
->cur_event_class_id
= -1;
1158 /* Check if we have some content left */
1159 if (msg_it
->cur_exp_packet_content_size
>= 0) {
1160 if (G_UNLIKELY(packet_at(msg_it
) ==
1161 msg_it
->cur_exp_packet_content_size
)) {
1162 /* No more events! */
1163 BT_COMP_LOGD("Reached end of packet: msg-it-addr=%p, "
1164 "cur=%zu", msg_it
, packet_at(msg_it
));
1165 msg_it
->state
= STATE_EMIT_MSG_PACKET_END_MULTI
;
1167 } else if (G_UNLIKELY(packet_at(msg_it
) >
1168 msg_it
->cur_exp_packet_content_size
)) {
1169 /* That's not supposed to happen */
1170 BT_COMP_LOGD("Before decoding event header field: cursor is passed the packet's content: "
1171 "msg-it-addr=%p, content-size=%" PRId64
", "
1173 msg_it
->cur_exp_packet_content_size
,
1175 status
= CTF_MSG_ITER_STATUS_ERROR
;
1180 * "Infinite" content: we're done when the medium has
1181 * nothing else for us.
1183 status
= buf_ensure_available_bits(msg_it
);
1185 case CTF_MSG_ITER_STATUS_OK
:
1187 case CTF_MSG_ITER_STATUS_EOF
:
1188 status
= CTF_MSG_ITER_STATUS_OK
;
1189 msg_it
->state
= STATE_EMIT_MSG_PACKET_END_SINGLE
;
1196 release_event_dscopes(msg_it
);
1197 BT_ASSERT(msg_it
->meta
.sc
);
1198 event_header_fc
= msg_it
->meta
.sc
->event_header_fc
;
1199 if (!event_header_fc
) {
1200 msg_it
->state
= STATE_AFTER_EVENT_HEADER
;
1204 BT_COMP_LOGD("Decoding event header field: "
1205 "msg-it-addr=%p, stream-class-addr=%p, "
1206 "stream-class-id=%" PRId64
", "
1208 msg_it
, msg_it
->meta
.sc
,
1209 msg_it
->meta
.sc
->id
,
1211 status
= read_dscope_begin_state(msg_it
, event_header_fc
,
1212 STATE_AFTER_EVENT_HEADER
,
1213 STATE_DSCOPE_EVENT_HEADER_CONTINUE
, NULL
);
1215 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1216 "Cannot decode event header field: "
1217 "msg-it-addr=%p, stream-class-addr=%p, "
1218 "stream-class-id=%" PRId64
", fc-addr=%p",
1219 msg_it
, msg_it
->meta
.sc
,
1220 msg_it
->meta
.sc
->id
,
1229 enum ctf_msg_iter_status
read_event_header_continue_state(
1230 struct ctf_msg_iter
*msg_it
)
1232 return read_dscope_continue_state(msg_it
,
1233 STATE_AFTER_EVENT_HEADER
);
1237 enum ctf_msg_iter_status
set_current_event_class(struct ctf_msg_iter
*msg_it
)
1239 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
1240 bt_self_component
*self_comp
= msg_it
->self_comp
;
1242 struct ctf_event_class
*new_event_class
= NULL
;
1244 if (msg_it
->cur_event_class_id
== -1) {
1246 * No current event class ID field, therefore only one
1249 if (msg_it
->meta
.sc
->event_classes
->len
!= 1) {
1250 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1251 "Need exactly one event class since there's no event class ID field: "
1252 "msg-it-addr=%p", msg_it
);
1253 status
= CTF_MSG_ITER_STATUS_ERROR
;
1257 new_event_class
= msg_it
->meta
.sc
->event_classes
->pdata
[0];
1258 msg_it
->cur_event_class_id
= new_event_class
->id
;
1261 new_event_class
= ctf_stream_class_borrow_event_class_by_id(
1262 msg_it
->meta
.sc
, msg_it
->cur_event_class_id
);
1263 if (!new_event_class
) {
1264 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1265 "No event class with ID of event class ID to use in stream class: "
1266 "msg-it-addr=%p, stream-class-id=%" PRIu64
", "
1267 "event-class-id=%" PRIu64
", "
1268 "trace-class-addr=%p",
1269 msg_it
, msg_it
->meta
.sc
->id
, msg_it
->cur_event_class_id
,
1271 status
= CTF_MSG_ITER_STATUS_ERROR
;
1275 msg_it
->meta
.ec
= new_event_class
;
1276 BT_COMP_LOGD("Set current event class: "
1277 "msg-it-addr=%p, event-class-addr=%p, "
1278 "event-class-id=%" PRId64
", "
1279 "event-class-name=\"%s\"",
1280 msg_it
, msg_it
->meta
.ec
, msg_it
->meta
.ec
->id
,
1281 msg_it
->meta
.ec
->name
->str
);
1288 enum ctf_msg_iter_status
set_current_event_message(
1289 struct ctf_msg_iter
*msg_it
)
1291 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
1292 bt_self_component
*self_comp
= msg_it
->self_comp
;
1293 bt_message
*msg
= NULL
;
1295 BT_ASSERT_DBG(msg_it
->meta
.ec
);
1296 BT_ASSERT_DBG(msg_it
->packet
);
1297 BT_COMP_LOGD("Creating event message from event class and packet: "
1298 "msg-it-addr=%p, ec-addr=%p, ec-name=\"%s\", packet-addr=%p",
1299 msg_it
, msg_it
->meta
.ec
,
1300 msg_it
->meta
.ec
->name
->str
,
1302 BT_ASSERT_DBG(msg_it
->self_msg_iter
);
1303 BT_ASSERT_DBG(msg_it
->meta
.sc
);
1305 if (bt_stream_class_borrow_default_clock_class(msg_it
->meta
.sc
->ir_sc
)) {
1306 msg
= bt_message_event_create_with_packet_and_default_clock_snapshot(
1307 msg_it
->self_msg_iter
, msg_it
->meta
.ec
->ir_ec
,
1308 msg_it
->packet
, msg_it
->default_clock_snapshot
);
1310 msg
= bt_message_event_create_with_packet(msg_it
->self_msg_iter
,
1311 msg_it
->meta
.ec
->ir_ec
, msg_it
->packet
);
1315 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1316 "Cannot create event message: "
1317 "msg-it-addr=%p, ec-addr=%p, ec-name=\"%s\", "
1319 msg_it
, msg_it
->meta
.ec
,
1320 msg_it
->meta
.ec
->name
->str
,
1328 BT_MESSAGE_PUT_REF_AND_RESET(msg
);
1329 status
= CTF_MSG_ITER_STATUS_ERROR
;
1332 BT_MESSAGE_MOVE_REF(msg_it
->event_msg
, msg
);
1337 enum ctf_msg_iter_status
after_event_header_state(
1338 struct ctf_msg_iter
*msg_it
)
1340 enum ctf_msg_iter_status status
;
1342 status
= set_current_event_class(msg_it
);
1343 if (status
!= CTF_MSG_ITER_STATUS_OK
) {
1347 if (G_UNLIKELY(msg_it
->dry_run
)) {
1351 status
= set_current_event_message(msg_it
);
1352 if (status
!= CTF_MSG_ITER_STATUS_OK
) {
1356 msg_it
->event
= bt_message_event_borrow_event(
1358 BT_ASSERT_DBG(msg_it
->event
);
1361 msg_it
->state
= STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
;
1368 enum ctf_msg_iter_status
read_event_common_context_begin_state(
1369 struct ctf_msg_iter
*msg_it
)
1371 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
1372 bt_self_component
*self_comp
= msg_it
->self_comp
;
1373 struct ctf_field_class
*event_common_context_fc
;
1375 event_common_context_fc
= msg_it
->meta
.sc
->event_common_context_fc
;
1376 if (!event_common_context_fc
) {
1377 msg_it
->state
= STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
;
1381 if (event_common_context_fc
->in_ir
&& !msg_it
->dry_run
) {
1382 BT_ASSERT_DBG(!msg_it
->dscopes
.event_common_context
);
1383 msg_it
->dscopes
.event_common_context
=
1384 bt_event_borrow_common_context_field(
1386 BT_ASSERT_DBG(msg_it
->dscopes
.event_common_context
);
1389 BT_COMP_LOGT("Decoding event common context field: "
1390 "msg-it-addr=%p, stream-class-addr=%p, "
1391 "stream-class-id=%" PRId64
", "
1393 msg_it
, msg_it
->meta
.sc
,
1394 msg_it
->meta
.sc
->id
,
1395 event_common_context_fc
);
1396 status
= read_dscope_begin_state(msg_it
, event_common_context_fc
,
1397 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
,
1398 STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
,
1399 msg_it
->dscopes
.event_common_context
);
1401 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1402 "Cannot decode event common context field: "
1403 "msg-it-addr=%p, stream-class-addr=%p, "
1404 "stream-class-id=%" PRId64
", fc-addr=%p",
1405 msg_it
, msg_it
->meta
.sc
,
1406 msg_it
->meta
.sc
->id
,
1407 event_common_context_fc
);
1415 enum ctf_msg_iter_status
read_event_common_context_continue_state(
1416 struct ctf_msg_iter
*msg_it
)
1418 return read_dscope_continue_state(msg_it
,
1419 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
);
1423 enum ctf_msg_iter_status
read_event_spec_context_begin_state(
1424 struct ctf_msg_iter
*msg_it
)
1426 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
1427 bt_self_component
*self_comp
= msg_it
->self_comp
;
1428 struct ctf_field_class
*event_spec_context_fc
;
1430 event_spec_context_fc
= msg_it
->meta
.ec
->spec_context_fc
;
1431 if (!event_spec_context_fc
) {
1432 msg_it
->state
= STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
;
1436 if (event_spec_context_fc
->in_ir
&& !msg_it
->dry_run
) {
1437 BT_ASSERT_DBG(!msg_it
->dscopes
.event_spec_context
);
1438 msg_it
->dscopes
.event_spec_context
=
1439 bt_event_borrow_specific_context_field(
1441 BT_ASSERT_DBG(msg_it
->dscopes
.event_spec_context
);
1444 BT_COMP_LOGT("Decoding event specific context field: "
1445 "msg-it-addr=%p, event-class-addr=%p, "
1446 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1448 msg_it
, msg_it
->meta
.ec
,
1449 msg_it
->meta
.ec
->name
->str
,
1450 msg_it
->meta
.ec
->id
,
1451 event_spec_context_fc
);
1452 status
= read_dscope_begin_state(msg_it
, event_spec_context_fc
,
1453 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
1454 STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
,
1455 msg_it
->dscopes
.event_spec_context
);
1457 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1458 "Cannot decode event specific context field: "
1459 "msg-it-addr=%p, event-class-addr=%p, "
1460 "event-class-name=\"%s\", "
1461 "event-class-id=%" PRId64
", fc-addr=%p",
1462 msg_it
, msg_it
->meta
.ec
,
1463 msg_it
->meta
.ec
->name
->str
,
1464 msg_it
->meta
.ec
->id
,
1465 event_spec_context_fc
);
1473 enum ctf_msg_iter_status
read_event_spec_context_continue_state(
1474 struct ctf_msg_iter
*msg_it
)
1476 return read_dscope_continue_state(msg_it
,
1477 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
);
1481 enum ctf_msg_iter_status
read_event_payload_begin_state(
1482 struct ctf_msg_iter
*msg_it
)
1484 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
1485 bt_self_component
*self_comp
= msg_it
->self_comp
;
1486 struct ctf_field_class
*event_payload_fc
;
1488 event_payload_fc
= msg_it
->meta
.ec
->payload_fc
;
1489 if (!event_payload_fc
) {
1490 msg_it
->state
= STATE_EMIT_MSG_EVENT
;
1494 if (event_payload_fc
->in_ir
&& !msg_it
->dry_run
) {
1495 BT_ASSERT_DBG(!msg_it
->dscopes
.event_payload
);
1496 msg_it
->dscopes
.event_payload
=
1497 bt_event_borrow_payload_field(
1499 BT_ASSERT_DBG(msg_it
->dscopes
.event_payload
);
1502 BT_COMP_LOGT("Decoding event payload field: "
1503 "msg-it-addr=%p, event-class-addr=%p, "
1504 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1506 msg_it
, msg_it
->meta
.ec
,
1507 msg_it
->meta
.ec
->name
->str
,
1508 msg_it
->meta
.ec
->id
,
1510 status
= read_dscope_begin_state(msg_it
, event_payload_fc
,
1511 STATE_EMIT_MSG_EVENT
,
1512 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
1513 msg_it
->dscopes
.event_payload
);
1515 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1516 "Cannot decode event payload field: "
1517 "msg-it-addr=%p, event-class-addr=%p, "
1518 "event-class-name=\"%s\", "
1519 "event-class-id=%" PRId64
", fc-addr=%p",
1520 msg_it
, msg_it
->meta
.ec
,
1521 msg_it
->meta
.ec
->name
->str
,
1522 msg_it
->meta
.ec
->id
,
1531 enum ctf_msg_iter_status
read_event_payload_continue_state(
1532 struct ctf_msg_iter
*msg_it
)
1534 return read_dscope_continue_state(msg_it
, STATE_EMIT_MSG_EVENT
);
1538 enum ctf_msg_iter_status
skip_packet_padding_state(struct ctf_msg_iter
*msg_it
)
1540 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
1541 size_t bits_to_skip
;
1542 const enum state next_state
= STATE_SWITCH_PACKET
;
1544 BT_ASSERT(msg_it
->cur_exp_packet_total_size
> 0);
1545 bits_to_skip
= msg_it
->cur_exp_packet_total_size
- packet_at(msg_it
);
1546 if (bits_to_skip
== 0) {
1547 msg_it
->state
= next_state
;
1550 size_t bits_to_consume
;
1552 BT_COMP_LOGD("Trying to skip %zu bits of padding: msg-it-addr=%p, size=%zu",
1553 bits_to_skip
, msg_it
, bits_to_skip
);
1554 status
= buf_ensure_available_bits(msg_it
);
1555 if (status
!= CTF_MSG_ITER_STATUS_OK
) {
1559 bits_to_consume
= MIN(buf_available_bits(msg_it
), bits_to_skip
);
1560 BT_COMP_LOGD("Skipping %zu bits of padding: msg-it-addr=%p, size=%zu",
1561 bits_to_consume
, msg_it
, bits_to_consume
);
1562 buf_consume_bits(msg_it
, bits_to_consume
);
1563 bits_to_skip
= msg_it
->cur_exp_packet_total_size
-
1565 if (bits_to_skip
== 0) {
1566 msg_it
->state
= next_state
;
1576 enum ctf_msg_iter_status
check_emit_msg_stream_beginning_state(
1577 struct ctf_msg_iter
*msg_it
)
1579 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
1581 if (msg_it
->set_stream
) {
1582 status
= set_current_stream(msg_it
);
1583 if (status
!= CTF_MSG_ITER_STATUS_OK
) {
1588 if (msg_it
->emit_stream_begin_msg
) {
1589 msg_it
->state
= STATE_EMIT_MSG_STREAM_BEGINNING
;
1591 /* Stream's first packet */
1592 msg_it
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS
;
1600 enum ctf_msg_iter_status
check_emit_msg_discarded_events(
1601 struct ctf_msg_iter
*msg_it
)
1603 msg_it
->state
= STATE_EMIT_MSG_DISCARDED_EVENTS
;
1605 if (!msg_it
->meta
.sc
->has_discarded_events
) {
1606 msg_it
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
;
1610 if (msg_it
->prev_packet_snapshots
.discarded_events
== UINT64_C(-1)) {
1611 if (msg_it
->snapshots
.discarded_events
== 0 ||
1612 msg_it
->snapshots
.discarded_events
== UINT64_C(-1)) {
1614 * Stream's first packet with no discarded
1615 * events or no information about discarded
1616 * events: do not emit.
1618 msg_it
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
;
1622 * If the previous packet has a value for this counter,
1623 * then this counter is defined for the whole stream.
1625 BT_ASSERT(msg_it
->snapshots
.discarded_events
!= UINT64_C(-1));
1627 if (msg_it
->snapshots
.discarded_events
-
1628 msg_it
->prev_packet_snapshots
.discarded_events
== 0) {
1630 * No discarded events since previous packet: do
1633 msg_it
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
;
1638 return CTF_MSG_ITER_STATUS_OK
;
1642 enum ctf_msg_iter_status
check_emit_msg_discarded_packets(
1643 struct ctf_msg_iter
*msg_it
)
1645 msg_it
->state
= STATE_EMIT_MSG_DISCARDED_PACKETS
;
1647 if (!msg_it
->meta
.sc
->has_discarded_packets
) {
1648 msg_it
->state
= STATE_EMIT_MSG_PACKET_BEGINNING
;
1652 if (msg_it
->prev_packet_snapshots
.packets
== UINT64_C(-1)) {
1654 * Stream's first packet or no information about
1655 * discarded packets: do not emit. In other words, if
1656 * this is the first packet and its sequence number is
1657 * not 0, do not consider that packets were previously
1658 * lost: we might be reading a partial stream (LTTng
1659 * snapshot for example).
1661 msg_it
->state
= STATE_EMIT_MSG_PACKET_BEGINNING
;
1664 * If the previous packet has a value for this counter,
1665 * then this counter is defined for the whole stream.
1667 BT_ASSERT(msg_it
->snapshots
.packets
!= UINT64_C(-1));
1669 if (msg_it
->snapshots
.packets
-
1670 msg_it
->prev_packet_snapshots
.packets
<= 1) {
1672 * No discarded packets since previous packet:
1675 msg_it
->state
= STATE_EMIT_MSG_PACKET_BEGINNING
;
1680 return CTF_MSG_ITER_STATUS_OK
;
1684 enum ctf_msg_iter_status
check_emit_msg_stream_end(
1685 struct ctf_msg_iter
*msg_it
)
1687 if (msg_it
->emit_stream_end_msg
) {
1688 msg_it
->state
= STATE_EMIT_MSG_STREAM_END
;
1690 msg_it
->state
= STATE_DONE
;
1693 return CTF_MSG_ITER_STATUS_OK
;
1697 enum ctf_msg_iter_status
handle_state(struct ctf_msg_iter
*msg_it
)
1699 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
1700 const enum state state
= msg_it
->state
;
1702 BT_COMP_LOGT("Handling state: msg-it-addr=%p, state=%s",
1703 msg_it
, state_string(state
));
1705 // TODO: optimalize!
1708 msg_it
->state
= STATE_SWITCH_PACKET
;
1710 case STATE_SWITCH_PACKET
:
1711 status
= switch_packet_state(msg_it
);
1713 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
1714 status
= read_packet_header_begin_state(msg_it
);
1716 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
1717 status
= read_packet_header_continue_state(msg_it
);
1719 case STATE_AFTER_TRACE_PACKET_HEADER
:
1720 status
= after_packet_header_state(msg_it
);
1722 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
1723 status
= read_packet_context_begin_state(msg_it
);
1725 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
1726 status
= read_packet_context_continue_state(msg_it
);
1728 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
1729 status
= after_packet_context_state(msg_it
);
1731 case STATE_CHECK_EMIT_MSG_STREAM_BEGINNING
:
1732 status
= check_emit_msg_stream_beginning_state(msg_it
);
1734 case STATE_EMIT_MSG_STREAM_BEGINNING
:
1735 msg_it
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS
;
1737 case STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS
:
1738 status
= check_emit_msg_discarded_events(msg_it
);
1740 case STATE_EMIT_MSG_DISCARDED_EVENTS
:
1741 msg_it
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
;
1743 case STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
:
1744 status
= check_emit_msg_discarded_packets(msg_it
);
1746 case STATE_EMIT_MSG_DISCARDED_PACKETS
:
1747 msg_it
->state
= STATE_EMIT_MSG_PACKET_BEGINNING
;
1749 case STATE_EMIT_MSG_PACKET_BEGINNING
:
1750 msg_it
->state
= STATE_DSCOPE_EVENT_HEADER_BEGIN
;
1752 case STATE_DSCOPE_EVENT_HEADER_BEGIN
:
1753 status
= read_event_header_begin_state(msg_it
);
1755 case STATE_DSCOPE_EVENT_HEADER_CONTINUE
:
1756 status
= read_event_header_continue_state(msg_it
);
1758 case STATE_AFTER_EVENT_HEADER
:
1759 status
= after_event_header_state(msg_it
);
1761 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
:
1762 status
= read_event_common_context_begin_state(msg_it
);
1764 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
:
1765 status
= read_event_common_context_continue_state(msg_it
);
1767 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
:
1768 status
= read_event_spec_context_begin_state(msg_it
);
1770 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
:
1771 status
= read_event_spec_context_continue_state(msg_it
);
1773 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
1774 status
= read_event_payload_begin_state(msg_it
);
1776 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
1777 status
= read_event_payload_continue_state(msg_it
);
1779 case STATE_EMIT_MSG_EVENT
:
1780 msg_it
->state
= STATE_DSCOPE_EVENT_HEADER_BEGIN
;
1782 case STATE_EMIT_QUEUED_MSG_EVENT
:
1783 msg_it
->state
= STATE_EMIT_MSG_EVENT
;
1785 case STATE_SKIP_PACKET_PADDING
:
1786 status
= skip_packet_padding_state(msg_it
);
1788 case STATE_EMIT_MSG_PACKET_END_MULTI
:
1789 msg_it
->state
= STATE_SKIP_PACKET_PADDING
;
1791 case STATE_EMIT_MSG_PACKET_END_SINGLE
:
1792 msg_it
->state
= STATE_CHECK_EMIT_MSG_STREAM_END
;
1794 case STATE_CHECK_EMIT_MSG_STREAM_END
:
1795 status
= check_emit_msg_stream_end(msg_it
);
1797 case STATE_EMIT_QUEUED_MSG_PACKET_END
:
1798 msg_it
->state
= STATE_EMIT_MSG_PACKET_END_SINGLE
;
1800 case STATE_EMIT_MSG_STREAM_END
:
1801 msg_it
->state
= STATE_DONE
;
1806 BT_COMP_LOGF("Unknown CTF plugin message iterator state: "
1807 "msg-it-addr=%p, state=%d", msg_it
, msg_it
->state
);
1811 BT_COMP_LOGT("Handled state: msg-it-addr=%p, status=%s, "
1812 "prev-state=%s, cur-state=%s",
1813 msg_it
, ctf_msg_iter_status_string(status
),
1814 state_string(state
), state_string(msg_it
->state
));
1819 void ctf_msg_iter_reset_for_next_stream_file(struct ctf_msg_iter
*msg_it
)
1822 BT_COMP_LOGD("Resetting message iterator: addr=%p", msg_it
);
1823 stack_clear(msg_it
->stack
);
1824 msg_it
->meta
.sc
= NULL
;
1825 msg_it
->meta
.ec
= NULL
;
1826 BT_PACKET_PUT_REF_AND_RESET(msg_it
->packet
);
1827 BT_STREAM_PUT_REF_AND_RESET(msg_it
->stream
);
1828 BT_MESSAGE_PUT_REF_AND_RESET(msg_it
->event_msg
);
1829 release_all_dscopes(msg_it
);
1830 msg_it
->cur_dscope_field
= NULL
;
1832 if (msg_it
->packet_context_field
) {
1833 bt_packet_context_field_release(msg_it
->packet_context_field
);
1834 msg_it
->packet_context_field
= NULL
;
1837 msg_it
->buf
.addr
= NULL
;
1840 msg_it
->buf
.last_eh_at
= SIZE_MAX
;
1841 msg_it
->buf
.packet_offset
= 0;
1842 msg_it
->state
= STATE_INIT
;
1843 msg_it
->cur_exp_packet_content_size
= -1;
1844 msg_it
->cur_exp_packet_total_size
= -1;
1845 msg_it
->cur_packet_offset
= -1;
1846 msg_it
->cur_event_class_id
= -1;
1847 msg_it
->snapshots
.beginning_clock
= UINT64_C(-1);
1848 msg_it
->snapshots
.end_clock
= UINT64_C(-1);
1852 * Resets the internal state of a CTF message iterator.
1855 void ctf_msg_iter_reset(struct ctf_msg_iter
*msg_it
)
1857 ctf_msg_iter_reset_for_next_stream_file(msg_it
);
1858 msg_it
->cur_stream_class_id
= -1;
1859 msg_it
->cur_data_stream_id
= -1;
1860 msg_it
->snapshots
.discarded_events
= UINT64_C(-1);
1861 msg_it
->snapshots
.packets
= UINT64_C(-1);
1862 msg_it
->prev_packet_snapshots
.discarded_events
= UINT64_C(-1);
1863 msg_it
->prev_packet_snapshots
.packets
= UINT64_C(-1);
1864 msg_it
->prev_packet_snapshots
.beginning_clock
= UINT64_C(-1);
1865 msg_it
->prev_packet_snapshots
.end_clock
= UINT64_C(-1);
1869 bt_field
*borrow_next_field(struct ctf_msg_iter
*msg_it
)
1871 bt_field
*next_field
= NULL
;
1872 bt_field
*base_field
;
1873 const bt_field_class
*base_fc
;
1874 bt_field_class_type base_fc_type
;
1877 BT_ASSERT_DBG(!stack_empty(msg_it
->stack
));
1878 index
= stack_top(msg_it
->stack
)->index
;
1879 base_field
= stack_top(msg_it
->stack
)->base
;
1880 BT_ASSERT_DBG(base_field
);
1881 base_fc
= bt_field_borrow_class_const(base_field
);
1882 BT_ASSERT_DBG(base_fc
);
1883 base_fc_type
= bt_field_class_get_type(base_fc
);
1885 if (base_fc_type
== BT_FIELD_CLASS_TYPE_STRUCTURE
) {
1886 BT_ASSERT_DBG(index
<
1887 bt_field_class_structure_get_member_count(
1888 bt_field_borrow_class_const(
1891 bt_field_structure_borrow_member_field_by_index(
1893 } else if (bt_field_class_type_is(base_fc_type
,
1894 BT_FIELD_CLASS_TYPE_ARRAY
)) {
1895 BT_ASSERT_DBG(index
< bt_field_array_get_length(base_field
));
1896 next_field
= bt_field_array_borrow_element_field_by_index(
1898 } else if (bt_field_class_type_is(base_fc_type
,
1899 BT_FIELD_CLASS_TYPE_VARIANT
)) {
1900 BT_ASSERT_DBG(index
== 0);
1901 next_field
= bt_field_variant_borrow_selected_option_field(
1907 BT_ASSERT_DBG(next_field
);
1912 void update_default_clock(struct ctf_msg_iter
*msg_it
, uint64_t new_val
,
1913 uint64_t new_val_size
)
1915 uint64_t new_val_mask
;
1916 uint64_t cur_value_masked
;
1918 BT_ASSERT_DBG(new_val_size
> 0);
1921 * Special case for a 64-bit new value, which is the limit
1922 * of a clock value as of this version: overwrite the
1923 * current value directly.
1925 if (new_val_size
== 64) {
1926 msg_it
->default_clock_snapshot
= new_val
;
1930 new_val_mask
= (1ULL << new_val_size
) - 1;
1931 cur_value_masked
= msg_it
->default_clock_snapshot
& new_val_mask
;
1933 if (new_val
< cur_value_masked
) {
1935 * It looks like a wrap happened on the number of bits
1936 * of the requested new value. Assume that the clock
1937 * value wrapped only one time.
1939 msg_it
->default_clock_snapshot
+= new_val_mask
+ 1;
1942 /* Clear the low bits of the current clock value. */
1943 msg_it
->default_clock_snapshot
&= ~new_val_mask
;
1945 /* Set the low bits of the current clock value. */
1946 msg_it
->default_clock_snapshot
|= new_val
;
1949 BT_COMP_LOGT("Updated default clock's value from integer field's value: "
1950 "value=%" PRIu64
, msg_it
->default_clock_snapshot
);
1954 enum bt_bfcr_status
bfcr_unsigned_int_cb(uint64_t value
,
1955 struct ctf_field_class
*fc
, void *data
)
1957 struct ctf_msg_iter
*msg_it
= data
;
1958 bt_self_component
*self_comp
= msg_it
->self_comp
;
1959 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
1961 bt_field
*field
= NULL
;
1962 struct ctf_field_class_int
*int_fc
= (void *) fc
;
1964 BT_COMP_LOGT("Unsigned integer function called from BFCR: "
1965 "msg-it-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1966 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64
,
1967 msg_it
, msg_it
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
1969 if (G_LIKELY(int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
)) {
1970 goto update_def_clock
;
1973 switch (int_fc
->meaning
) {
1974 case CTF_FIELD_CLASS_MEANING_EVENT_CLASS_ID
:
1975 msg_it
->cur_event_class_id
= value
;
1977 case CTF_FIELD_CLASS_MEANING_DATA_STREAM_ID
:
1978 msg_it
->cur_data_stream_id
= value
;
1980 case CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME
:
1981 msg_it
->snapshots
.beginning_clock
= value
;
1983 case CTF_FIELD_CLASS_MEANING_PACKET_END_TIME
:
1984 msg_it
->snapshots
.end_clock
= value
;
1986 case CTF_FIELD_CLASS_MEANING_STREAM_CLASS_ID
:
1987 msg_it
->cur_stream_class_id
= value
;
1989 case CTF_FIELD_CLASS_MEANING_MAGIC
:
1990 if (value
!= 0xc1fc1fc1) {
1991 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1992 "Invalid CTF magic number: msg-it-addr=%p, "
1993 "magic=%" PRIx64
, msg_it
, value
);
1994 status
= BT_BFCR_STATUS_ERROR
;
1999 case CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT
:
2000 msg_it
->snapshots
.packets
= value
;
2002 case CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT
:
2003 msg_it
->snapshots
.discarded_events
= value
;
2005 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_TOTAL_SIZE
:
2006 msg_it
->cur_exp_packet_total_size
= value
;
2008 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_CONTENT_SIZE
:
2009 msg_it
->cur_exp_packet_content_size
= value
;
2016 if (G_UNLIKELY(int_fc
->mapped_clock_class
)) {
2017 update_default_clock(msg_it
, value
, int_fc
->base
.size
);
2020 if (G_UNLIKELY(int_fc
->storing_index
>= 0)) {
2021 g_array_index(msg_it
->stored_values
, uint64_t,
2022 (uint64_t) int_fc
->storing_index
) = value
;
2025 if (G_UNLIKELY(!fc
->in_ir
|| msg_it
->dry_run
)) {
2029 field
= borrow_next_field(msg_it
);
2030 BT_ASSERT_DBG(field
);
2031 BT_ASSERT_DBG(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
2032 BT_ASSERT_DBG(bt_field_class_type_is(bt_field_get_class_type(field
),
2033 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
));
2034 bt_field_integer_unsigned_set_value(field
, value
);
2035 stack_top(msg_it
->stack
)->index
++;
2042 enum bt_bfcr_status
bfcr_unsigned_int_char_cb(uint64_t value
,
2043 struct ctf_field_class
*fc
, void *data
)
2046 struct ctf_msg_iter
*msg_it
= data
;
2047 bt_self_component
*self_comp
= msg_it
->self_comp
;
2048 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
2049 bt_field
*string_field
= NULL
;
2050 struct ctf_field_class_int
*int_fc
= (void *) fc
;
2051 char str
[2] = {'\0', '\0'};
2053 BT_COMP_LOGT("Unsigned integer character function called from BFCR: "
2054 "msg-it-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2055 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64
,
2056 msg_it
, msg_it
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
2057 BT_ASSERT_DBG(int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
);
2058 BT_ASSERT_DBG(!int_fc
->mapped_clock_class
);
2059 BT_ASSERT_DBG(int_fc
->storing_index
< 0);
2061 if (G_UNLIKELY(!fc
->in_ir
|| msg_it
->dry_run
)) {
2065 if (msg_it
->done_filling_string
) {
2070 msg_it
->done_filling_string
= true;
2074 string_field
= stack_top(msg_it
->stack
)->base
;
2075 BT_ASSERT_DBG(bt_field_get_class_type(string_field
) ==
2076 BT_FIELD_CLASS_TYPE_STRING
);
2078 /* Append character */
2079 str
[0] = (char) value
;
2080 ret
= bt_field_string_append_with_length(string_field
, str
, 1);
2082 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
2083 "Cannot append character to string field's value: "
2084 "msg-it-addr=%p, field-addr=%p, ret=%d",
2085 msg_it
, string_field
, ret
);
2086 status
= BT_BFCR_STATUS_ERROR
;
2095 enum bt_bfcr_status
bfcr_signed_int_cb(int64_t value
,
2096 struct ctf_field_class
*fc
, void *data
)
2098 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
2099 bt_field
*field
= NULL
;
2100 struct ctf_msg_iter
*msg_it
= data
;
2101 struct ctf_field_class_int
*int_fc
= (void *) fc
;
2103 BT_COMP_LOGT("Signed integer function called from BFCR: "
2104 "msg-it-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2105 "fc-type=%d, fc-in-ir=%d, value=%" PRId64
,
2106 msg_it
, msg_it
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
2107 BT_ASSERT_DBG(int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
);
2109 if (G_UNLIKELY(int_fc
->storing_index
>= 0)) {
2110 g_array_index(msg_it
->stored_values
, uint64_t,
2111 (uint64_t) int_fc
->storing_index
) = (uint64_t) value
;
2114 if (G_UNLIKELY(!fc
->in_ir
|| msg_it
->dry_run
)) {
2118 field
= borrow_next_field(msg_it
);
2119 BT_ASSERT_DBG(field
);
2120 BT_ASSERT_DBG(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
2121 BT_ASSERT_DBG(bt_field_class_type_is(bt_field_get_class_type(field
),
2122 BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
));
2123 bt_field_integer_signed_set_value(field
, value
);
2124 stack_top(msg_it
->stack
)->index
++;
2131 enum bt_bfcr_status
bfcr_floating_point_cb(double value
,
2132 struct ctf_field_class
*fc
, void *data
)
2134 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
2135 bt_field
*field
= NULL
;
2136 struct ctf_msg_iter
*msg_it
= data
;
2137 bt_field_class_type type
;
2139 BT_COMP_LOGT("Floating point number function called from BFCR: "
2140 "msg-it-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2141 "fc-type=%d, fc-in-ir=%d, value=%f",
2142 msg_it
, msg_it
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
2144 if (G_UNLIKELY(!fc
->in_ir
|| msg_it
->dry_run
)) {
2148 field
= borrow_next_field(msg_it
);
2149 type
= bt_field_get_class_type(field
);
2150 BT_ASSERT_DBG(field
);
2151 BT_ASSERT_DBG(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
2152 BT_ASSERT_DBG(bt_field_class_type_is(type
, BT_FIELD_CLASS_TYPE_REAL
));
2154 if (type
== BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL
) {
2155 bt_field_real_single_precision_set_value(field
, (float) value
);
2157 bt_field_real_double_precision_set_value(field
, value
);
2159 stack_top(msg_it
->stack
)->index
++;
2166 enum bt_bfcr_status
bfcr_string_begin_cb(
2167 struct ctf_field_class
*fc
, void *data
)
2169 bt_field
*field
= NULL
;
2170 struct ctf_msg_iter
*msg_it
= data
;
2172 BT_COMP_LOGT("String (beginning) function called from BFCR: "
2173 "msg-it-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2174 "fc-type=%d, fc-in-ir=%d",
2175 msg_it
, msg_it
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
2177 if (G_UNLIKELY(!fc
->in_ir
|| msg_it
->dry_run
)) {
2181 field
= borrow_next_field(msg_it
);
2182 BT_ASSERT_DBG(field
);
2183 BT_ASSERT_DBG(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
2184 BT_ASSERT_DBG(bt_field_get_class_type(field
) ==
2185 BT_FIELD_CLASS_TYPE_STRING
);
2186 bt_field_string_clear(field
);
2189 * Push on stack. Not a compound class per se, but we know that
2190 * only bfcr_string_cb() may be called between this call and a
2191 * subsequent call to bfcr_string_end_cb().
2193 stack_push(msg_it
->stack
, field
);
2196 return BT_BFCR_STATUS_OK
;
2200 enum bt_bfcr_status
bfcr_string_cb(const char *value
,
2201 size_t len
, struct ctf_field_class
*fc
, void *data
)
2203 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
2204 bt_field
*field
= NULL
;
2205 struct ctf_msg_iter
*msg_it
= data
;
2206 bt_self_component
*self_comp
= msg_it
->self_comp
;
2209 BT_COMP_LOGT("String (substring) function called from BFCR: "
2210 "msg-it-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2211 "fc-type=%d, fc-in-ir=%d, string-length=%zu",
2212 msg_it
, msg_it
->bfcr
, fc
, fc
->type
, fc
->in_ir
,
2215 if (G_UNLIKELY(!fc
->in_ir
|| msg_it
->dry_run
)) {
2219 field
= stack_top(msg_it
->stack
)->base
;
2220 BT_ASSERT_DBG(field
);
2222 /* Append current substring */
2223 ret
= bt_field_string_append_with_length(field
, value
, len
);
2225 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
2226 "Cannot append substring to string field's value: "
2227 "msg-it-addr=%p, field-addr=%p, string-length=%zu, "
2228 "ret=%d", msg_it
, field
, len
, ret
);
2229 status
= BT_BFCR_STATUS_ERROR
;
2238 enum bt_bfcr_status
bfcr_string_end_cb(
2239 struct ctf_field_class
*fc
, void *data
)
2241 struct ctf_msg_iter
*msg_it
= data
;
2243 BT_COMP_LOGT("String (end) function called from BFCR: "
2244 "msg-it-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2245 "fc-type=%d, fc-in-ir=%d",
2246 msg_it
, msg_it
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
2248 if (G_UNLIKELY(!fc
->in_ir
|| msg_it
->dry_run
)) {
2252 /* Pop string field */
2253 stack_pop(msg_it
->stack
);
2255 /* Go to next field */
2256 stack_top(msg_it
->stack
)->index
++;
2259 return BT_BFCR_STATUS_OK
;
2263 enum bt_bfcr_status
bfcr_compound_begin_cb(
2264 struct ctf_field_class
*fc
, void *data
)
2266 struct ctf_msg_iter
*msg_it
= data
;
2269 BT_COMP_LOGT("Compound (beginning) function called from BFCR: "
2270 "msg-it-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2271 "fc-type=%d, fc-in-ir=%d",
2272 msg_it
, msg_it
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
2274 if (G_UNLIKELY(!fc
->in_ir
|| msg_it
->dry_run
)) {
2279 if (stack_empty(msg_it
->stack
)) {
2280 /* Root: already set by read_dscope_begin_state() */
2281 field
= msg_it
->cur_dscope_field
;
2283 field
= borrow_next_field(msg_it
);
2284 BT_ASSERT_DBG(field
);
2288 BT_ASSERT_DBG(field
);
2289 BT_ASSERT_DBG(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
2290 stack_push(msg_it
->stack
, field
);
2293 * Change BFCR "unsigned int" callback if it's a text
2296 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
||
2297 fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
2298 struct ctf_field_class_array_base
*array_fc
= (void *) fc
;
2300 if (array_fc
->is_text
) {
2301 BT_ASSERT_DBG(bt_field_get_class_type(field
) ==
2302 BT_FIELD_CLASS_TYPE_STRING
);
2303 msg_it
->done_filling_string
= false;
2304 bt_field_string_clear(field
);
2305 bt_bfcr_set_unsigned_int_cb(msg_it
->bfcr
,
2306 bfcr_unsigned_int_char_cb
);
2311 return BT_BFCR_STATUS_OK
;
2315 enum bt_bfcr_status
bfcr_compound_end_cb(
2316 struct ctf_field_class
*fc
, void *data
)
2318 struct ctf_msg_iter
*msg_it
= data
;
2320 BT_COMP_LOGT("Compound (end) function called from BFCR: "
2321 "msg-it-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2322 "fc-type=%d, fc-in-ir=%d",
2323 msg_it
, msg_it
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
2325 if (G_UNLIKELY(!fc
->in_ir
|| msg_it
->dry_run
)) {
2329 BT_ASSERT_DBG(!stack_empty(msg_it
->stack
));
2330 BT_ASSERT_DBG(bt_field_borrow_class_const(stack_top(msg_it
->stack
)->base
) ==
2334 * Reset BFCR "unsigned int" callback if it's a text
2337 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
||
2338 fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
2339 struct ctf_field_class_array_base
*array_fc
= (void *) fc
;
2341 if (array_fc
->is_text
) {
2342 BT_ASSERT_DBG(bt_field_get_class_type(
2343 stack_top(msg_it
->stack
)->base
) ==
2344 BT_FIELD_CLASS_TYPE_STRING
);
2345 bt_bfcr_set_unsigned_int_cb(msg_it
->bfcr
,
2346 bfcr_unsigned_int_cb
);
2351 stack_pop(msg_it
->stack
);
2353 /* If the stack is not empty, increment the base's index */
2354 if (!stack_empty(msg_it
->stack
)) {
2355 stack_top(msg_it
->stack
)->index
++;
2359 return BT_BFCR_STATUS_OK
;
2363 int64_t bfcr_get_sequence_length_cb(struct ctf_field_class
*fc
, void *data
)
2365 bt_field
*seq_field
;
2366 struct ctf_msg_iter
*msg_it
= data
;
2367 bt_self_component
*self_comp
= msg_it
->self_comp
;
2368 struct ctf_field_class_sequence
*seq_fc
= (void *) fc
;
2372 length
= (uint64_t) g_array_index(msg_it
->stored_values
, uint64_t,
2373 seq_fc
->stored_length_index
);
2375 if (G_UNLIKELY(msg_it
->dry_run
)){
2379 seq_field
= stack_top(msg_it
->stack
)->base
;
2380 BT_ASSERT_DBG(seq_field
);
2383 * bfcr_get_sequence_length_cb() also gets called back for a
2384 * text sequence, but the destination field is a string field.
2385 * Only set the field's sequence length if the destination field
2386 * is a sequence field.
2388 if (!seq_fc
->base
.is_text
) {
2389 BT_ASSERT_DBG(bt_field_class_type_is(
2390 bt_field_get_class_type(seq_field
),
2391 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
));
2392 ret
= bt_field_array_dynamic_set_length(seq_field
,
2395 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
2396 "Cannot set dynamic array field's length field: "
2397 "msg-it-addr=%p, field-addr=%p, "
2398 "length=%" PRIu64
, msg_it
, seq_field
, length
);
2408 struct ctf_field_class
*bfcr_borrow_variant_selected_field_class_cb(
2409 struct ctf_field_class
*fc
, void *data
)
2413 int64_t option_index
= -1;
2414 struct ctf_msg_iter
*msg_it
= data
;
2415 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
2416 struct ctf_named_field_class
*selected_option
= NULL
;
2417 bt_self_component
*self_comp
= msg_it
->self_comp
;
2418 struct ctf_field_class
*ret_fc
= NULL
;
2424 /* Get variant's tag */
2425 tag
.u
= g_array_index(msg_it
->stored_values
, uint64_t,
2426 var_fc
->stored_tag_index
);
2429 * Check each range to find the selected option's index.
2431 if (var_fc
->tag_fc
->base
.is_signed
) {
2432 for (i
= 0; i
< var_fc
->ranges
->len
; i
++) {
2433 struct ctf_field_class_variant_range
*range
=
2434 ctf_field_class_variant_borrow_range_by_index(
2437 if (tag
.i
>= range
->range
.lower
.i
&&
2438 tag
.i
<= range
->range
.upper
.i
) {
2439 option_index
= (int64_t) range
->option_index
;
2444 for (i
= 0; i
< var_fc
->ranges
->len
; i
++) {
2445 struct ctf_field_class_variant_range
*range
=
2446 ctf_field_class_variant_borrow_range_by_index(
2449 if (tag
.u
>= range
->range
.lower
.u
&&
2450 tag
.u
<= range
->range
.upper
.u
) {
2451 option_index
= (int64_t) range
->option_index
;
2457 if (option_index
< 0) {
2458 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
2459 "Cannot find variant field class's option: "
2460 "msg-it-addr=%p, var-fc-addr=%p, u-tag=%" PRIu64
", "
2461 "i-tag=%" PRId64
, msg_it
, var_fc
, tag
.u
, tag
.i
);
2466 selected_option
= ctf_field_class_variant_borrow_option_by_index(
2467 var_fc
, (uint64_t) option_index
);
2469 if (selected_option
->fc
->in_ir
&& !msg_it
->dry_run
) {
2470 bt_field
*var_field
= stack_top(msg_it
->stack
)->base
;
2472 ret
= bt_field_variant_select_option_field_by_index(
2473 var_field
, option_index
);
2475 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
2476 "Cannot select variant field's option field: "
2477 "msg-it-addr=%p, var-field-addr=%p, "
2478 "opt-index=%" PRId64
, msg_it
, var_field
,
2485 ret_fc
= selected_option
->fc
;
2492 bt_message
*create_msg_stream_beginning(struct ctf_msg_iter
*msg_it
)
2494 bt_self_component
*self_comp
= msg_it
->self_comp
;
2497 BT_ASSERT(msg_it
->stream
);
2498 BT_ASSERT(msg_it
->self_msg_iter
);
2499 msg
= bt_message_stream_beginning_create(msg_it
->self_msg_iter
,
2502 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
2503 "Cannot create stream beginning message: "
2504 "msg-it-addr=%p, stream-addr=%p",
2505 msg_it
, msg_it
->stream
);
2512 bt_message
*create_msg_stream_end(struct ctf_msg_iter
*msg_it
)
2514 bt_self_component
*self_comp
= msg_it
->self_comp
;
2517 if (!msg_it
->stream
) {
2518 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
2519 "Cannot create stream end message because stream is NULL: "
2520 "msg-it-addr=%p", msg_it
);
2525 BT_ASSERT(msg_it
->self_msg_iter
);
2526 msg
= bt_message_stream_end_create(msg_it
->self_msg_iter
,
2529 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
2530 "Cannot create stream end message: "
2531 "msg-it-addr=%p, stream-addr=%p",
2532 msg_it
, msg_it
->stream
);
2540 bt_message
*create_msg_packet_beginning(struct ctf_msg_iter
*msg_it
,
2541 bool use_default_cs
)
2543 bt_self_component
*self_comp
= msg_it
->self_comp
;
2546 const bt_stream_class
*sc
= msg_it
->meta
.sc
->ir_sc
;
2548 BT_ASSERT(msg_it
->packet
);
2551 if (msg_it
->packet_context_field
) {
2552 ret
= bt_packet_move_context_field(
2553 msg_it
->packet
, msg_it
->packet_context_field
);
2559 msg_it
->packet_context_field
= NULL
;
2562 * At this point msg_it->dscopes.stream_packet_context
2563 * has the same value as the packet context field within
2566 BT_ASSERT(bt_packet_borrow_context_field(
2568 msg_it
->dscopes
.stream_packet_context
);
2571 BT_ASSERT(msg_it
->self_msg_iter
);
2573 if (msg_it
->meta
.sc
->packets_have_ts_begin
) {
2574 BT_ASSERT(msg_it
->snapshots
.beginning_clock
!= UINT64_C(-1));
2575 uint64_t raw_cs_value
;
2578 * Either use the decoded packet `timestamp_begin` field or the
2579 * current stream's default clock_snapshot.
2581 if (use_default_cs
) {
2582 raw_cs_value
= msg_it
->default_clock_snapshot
;
2584 raw_cs_value
= msg_it
->snapshots
.beginning_clock
;
2587 msg
= bt_message_packet_beginning_create_with_default_clock_snapshot(
2588 msg_it
->self_msg_iter
, msg_it
->packet
,
2591 msg
= bt_message_packet_beginning_create(msg_it
->self_msg_iter
,
2596 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
2597 "Cannot create packet beginning message: "
2598 "msg-it-addr=%p, packet-addr=%p",
2599 msg_it
, msg_it
->packet
);
2608 bt_message
*emit_delayed_packet_beg_msg(struct ctf_msg_iter
*msg_it
)
2610 bool packet_beg_ts_need_fix_up
;
2612 msg_it
->emit_delayed_packet_beginning_msg
= false;
2615 * Only fix the packet's timestamp_begin if it's larger than the first
2616 * event of the packet. If there was no event in the packet, the
2617 * `default_clock_snapshot` field will be either equal or greater than
2618 * `snapshots.beginning_clock` so there is not fix needed.
2620 packet_beg_ts_need_fix_up
=
2621 msg_it
->default_clock_snapshot
< msg_it
->snapshots
.beginning_clock
;
2623 /* create_msg_packet_beginning() logs errors */
2624 return create_msg_packet_beginning(msg_it
, packet_beg_ts_need_fix_up
);
2629 bt_message
*create_msg_packet_end(struct ctf_msg_iter
*msg_it
)
2632 bool update_default_cs
= true;
2633 bt_self_component
*self_comp
= msg_it
->self_comp
;
2635 if (!msg_it
->packet
) {
2641 * Check if we need to emit the delayed packet
2642 * beginning message instead of the packet end message.
2644 if (G_UNLIKELY(msg_it
->emit_delayed_packet_beginning_msg
)) {
2645 msg
= emit_delayed_packet_beg_msg(msg_it
);
2646 /* Don't forget to emit the packet end message. */
2647 msg_it
->state
= STATE_EMIT_QUEUED_MSG_PACKET_END
;
2651 /* Check if may be affected by lttng-crash timestamp_end quirk. */
2652 if (G_UNLIKELY(msg_it
->meta
.tc
->quirks
.lttng_crash
)) {
2654 * Check if the `timestamp_begin` field is non-zero but
2655 * `timestamp_end` is zero. It means the trace is affected by
2656 * the lttng-crash packet `timestamp_end` quirk and must be
2657 * fixed up by omitting to update the default clock snapshot to
2658 * the `timestamp_end` as is typically done.
2660 if (msg_it
->snapshots
.beginning_clock
!= 0 &&
2661 msg_it
->snapshots
.end_clock
== 0) {
2662 update_default_cs
= false;
2667 * Check if may be affected by lttng event-after-packet `timestamp_end`
2670 if (msg_it
->meta
.tc
->quirks
.lttng_event_after_packet
) {
2672 * Check if `timestamp_end` is smaller then the current
2673 * default_clock_snapshot (which is set to the last event
2674 * decoded). It means the trace is affected by the lttng
2675 * `event-after-packet` packet `timestamp_end` quirk and must
2676 * be fixed up by omitting to update the default clock snapshot
2677 * to the `timestamp_end` as is typically done.
2679 if (msg_it
->snapshots
.end_clock
< msg_it
->default_clock_snapshot
) {
2680 update_default_cs
= false;
2684 /* Update default clock from packet's end time. */
2685 if (msg_it
->snapshots
.end_clock
!= UINT64_C(-1) && update_default_cs
) {
2686 msg_it
->default_clock_snapshot
= msg_it
->snapshots
.end_clock
;
2689 BT_ASSERT(msg_it
->self_msg_iter
);
2691 if (msg_it
->meta
.sc
->packets_have_ts_end
) {
2692 BT_ASSERT(msg_it
->snapshots
.end_clock
!= UINT64_C(-1));
2693 msg
= bt_message_packet_end_create_with_default_clock_snapshot(
2694 msg_it
->self_msg_iter
, msg_it
->packet
,
2695 msg_it
->default_clock_snapshot
);
2697 msg
= bt_message_packet_end_create(msg_it
->self_msg_iter
,
2702 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
2703 "Cannot create packet end message: "
2704 "msg-it-addr=%p, packet-addr=%p",
2705 msg_it
, msg_it
->packet
);
2710 BT_PACKET_PUT_REF_AND_RESET(msg_it
->packet
);
2717 bt_message
*create_msg_discarded_events(struct ctf_msg_iter
*msg_it
)
2720 bt_self_component
*self_comp
= msg_it
->self_comp
;
2721 uint64_t beginning_raw_value
= UINT64_C(-1);
2722 uint64_t end_raw_value
= UINT64_C(-1);
2724 BT_ASSERT(msg_it
->self_msg_iter
);
2725 BT_ASSERT(msg_it
->stream
);
2726 BT_ASSERT(msg_it
->meta
.sc
->has_discarded_events
);
2728 if (msg_it
->meta
.sc
->discarded_events_have_default_cs
) {
2729 if (msg_it
->prev_packet_snapshots
.discarded_events
== UINT64_C(-1)) {
2731 * We discarded events, but before (and possibly
2732 * including) the current packet: use this packet's time
2733 * range, and do not have a specific count.
2735 beginning_raw_value
= msg_it
->snapshots
.beginning_clock
;
2736 end_raw_value
= msg_it
->snapshots
.end_clock
;
2738 beginning_raw_value
= msg_it
->prev_packet_snapshots
.end_clock
;
2739 end_raw_value
= msg_it
->snapshots
.end_clock
;
2742 BT_ASSERT(beginning_raw_value
!= UINT64_C(-1));
2743 BT_ASSERT(end_raw_value
!= UINT64_C(-1));
2744 msg
= bt_message_discarded_events_create_with_default_clock_snapshots(
2745 msg_it
->self_msg_iter
, msg_it
->stream
, beginning_raw_value
,
2748 msg
= bt_message_discarded_events_create(msg_it
->self_msg_iter
,
2753 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
2754 "Cannot create discarded events message: "
2755 "msg-it-addr=%p, stream-addr=%p",
2756 msg_it
, msg_it
->stream
);
2760 if (msg_it
->prev_packet_snapshots
.discarded_events
!= UINT64_C(-1)) {
2761 bt_message_discarded_events_set_count(msg
,
2762 msg_it
->snapshots
.discarded_events
-
2763 msg_it
->prev_packet_snapshots
.discarded_events
);
2771 bt_message
*create_msg_discarded_packets(struct ctf_msg_iter
*msg_it
)
2774 bt_self_component
*self_comp
= msg_it
->self_comp
;
2776 BT_ASSERT(msg_it
->self_msg_iter
);
2777 BT_ASSERT(msg_it
->stream
);
2778 BT_ASSERT(msg_it
->meta
.sc
->has_discarded_packets
);
2779 BT_ASSERT(msg_it
->prev_packet_snapshots
.packets
!=
2782 if (msg_it
->meta
.sc
->discarded_packets_have_default_cs
) {
2783 BT_ASSERT(msg_it
->prev_packet_snapshots
.end_clock
!= UINT64_C(-1));
2784 BT_ASSERT(msg_it
->snapshots
.beginning_clock
!= UINT64_C(-1));
2785 msg
= bt_message_discarded_packets_create_with_default_clock_snapshots(
2786 msg_it
->self_msg_iter
, msg_it
->stream
,
2787 msg_it
->prev_packet_snapshots
.end_clock
,
2788 msg_it
->snapshots
.beginning_clock
);
2790 msg
= bt_message_discarded_packets_create(msg_it
->self_msg_iter
,
2795 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
2796 "Cannot create discarded packets message: "
2797 "msg-it-addr=%p, stream-addr=%p",
2798 msg_it
, msg_it
->stream
);
2802 bt_message_discarded_packets_set_count(msg
,
2803 msg_it
->snapshots
.packets
-
2804 msg_it
->prev_packet_snapshots
.packets
- 1);
2811 struct ctf_msg_iter
*ctf_msg_iter_create(
2812 struct ctf_trace_class
*tc
,
2813 size_t max_request_sz
,
2814 struct ctf_msg_iter_medium_ops medops
, void *data
,
2815 bt_logging_level log_level
,
2816 bt_self_component
*self_comp
,
2817 bt_self_message_iterator
*self_msg_iter
)
2819 struct ctf_msg_iter
*msg_it
= NULL
;
2820 struct bt_bfcr_cbs cbs
= {
2822 .signed_int
= bfcr_signed_int_cb
,
2823 .unsigned_int
= bfcr_unsigned_int_cb
,
2824 .floating_point
= bfcr_floating_point_cb
,
2825 .string_begin
= bfcr_string_begin_cb
,
2826 .string
= bfcr_string_cb
,
2827 .string_end
= bfcr_string_end_cb
,
2828 .compound_begin
= bfcr_compound_begin_cb
,
2829 .compound_end
= bfcr_compound_end_cb
,
2832 .get_sequence_length
= bfcr_get_sequence_length_cb
,
2833 .borrow_variant_selected_field_class
= bfcr_borrow_variant_selected_field_class_cb
,
2838 BT_ASSERT(medops
.request_bytes
);
2839 BT_ASSERT(medops
.borrow_stream
);
2840 BT_COMP_LOG_CUR_LVL(BT_LOG_DEBUG
, log_level
, self_comp
,
2841 "Creating CTF plugin message iterator: "
2842 "trace-addr=%p, max-request-size=%zu, "
2843 "data=%p, log-level=%s", tc
, max_request_sz
, data
,
2844 bt_common_logging_level_string(log_level
));
2845 msg_it
= g_new0(struct ctf_msg_iter
, 1);
2847 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_comp
,
2848 "Failed to allocate one CTF plugin message iterator.");
2851 msg_it
->self_comp
= self_comp
;
2852 msg_it
->self_msg_iter
= self_msg_iter
;
2853 msg_it
->log_level
= log_level
;
2854 msg_it
->meta
.tc
= tc
;
2855 msg_it
->medium
.medops
= medops
;
2856 msg_it
->medium
.max_request_sz
= max_request_sz
;
2857 msg_it
->medium
.data
= data
;
2858 msg_it
->stack
= stack_new(msg_it
);
2859 msg_it
->stored_values
= g_array_new(FALSE
, TRUE
, sizeof(uint64_t));
2860 g_array_set_size(msg_it
->stored_values
, tc
->stored_value_count
);
2862 if (!msg_it
->stack
) {
2863 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
2864 "Failed to create field stack.");
2868 msg_it
->bfcr
= bt_bfcr_create(cbs
, msg_it
, log_level
, NULL
);
2869 if (!msg_it
->bfcr
) {
2870 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
2871 "Failed to create binary class reader (BFCR).");
2875 ctf_msg_iter_reset(msg_it
);
2876 BT_COMP_LOGD("Created CTF plugin message iterator: "
2877 "trace-addr=%p, max-request-size=%zu, "
2878 "data=%p, msg-it-addr=%p, log-level=%s",
2879 tc
, max_request_sz
, data
, msg_it
,
2880 bt_common_logging_level_string(log_level
));
2881 msg_it
->cur_packet_offset
= 0;
2887 ctf_msg_iter_destroy(msg_it
);
2892 void ctf_msg_iter_destroy(struct ctf_msg_iter
*msg_it
)
2894 BT_PACKET_PUT_REF_AND_RESET(msg_it
->packet
);
2895 BT_STREAM_PUT_REF_AND_RESET(msg_it
->stream
);
2896 release_all_dscopes(msg_it
);
2898 BT_COMP_LOGD("Destroying CTF plugin message iterator: addr=%p", msg_it
);
2900 if (msg_it
->stack
) {
2901 BT_COMP_LOGD_STR("Destroying field stack.");
2902 stack_destroy(msg_it
->stack
);
2906 BT_COMP_LOGD("Destroying BFCR: bfcr-addr=%p", msg_it
->bfcr
);
2907 bt_bfcr_destroy(msg_it
->bfcr
);
2910 if (msg_it
->stored_values
) {
2911 g_array_free(msg_it
->stored_values
, TRUE
);
2917 enum ctf_msg_iter_status
ctf_msg_iter_get_next_message(
2918 struct ctf_msg_iter
*msg_it
,
2919 bt_message
**message
)
2921 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
2922 bt_self_component
*self_comp
= msg_it
->self_comp
;
2924 BT_ASSERT_DBG(msg_it
);
2925 BT_ASSERT_DBG(message
);
2926 msg_it
->set_stream
= true;
2927 BT_COMP_LOGD("Getting next message: msg-it-addr=%p", msg_it
);
2930 status
= handle_state(msg_it
);
2931 if (G_UNLIKELY(status
== CTF_MSG_ITER_STATUS_AGAIN
)) {
2932 BT_COMP_LOGD_STR("Medium returned CTF_MSG_ITER_STATUS_AGAIN.");
2934 } else if (G_UNLIKELY(status
!= CTF_MSG_ITER_STATUS_OK
)) {
2935 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
2936 "Cannot handle state: msg-it-addr=%p, state=%s",
2937 msg_it
, state_string(msg_it
->state
));
2941 switch (msg_it
->state
) {
2942 case STATE_EMIT_MSG_EVENT
:
2943 BT_ASSERT_DBG(msg_it
->event_msg
);
2946 * Check if we need to emit the delayed packet
2947 * beginning message instead of the event message.
2949 if (G_UNLIKELY(msg_it
->emit_delayed_packet_beginning_msg
)) {
2950 *message
= emit_delayed_packet_beg_msg(msg_it
);
2952 status
= CTF_MSG_ITER_STATUS_ERROR
;
2956 * Don't forget to emit the event message of
2957 * the event record that was just decoded.
2959 msg_it
->state
= STATE_EMIT_QUEUED_MSG_EVENT
;
2962 *message
= msg_it
->event_msg
;
2963 msg_it
->event_msg
= NULL
;
2966 case STATE_EMIT_MSG_DISCARDED_EVENTS
:
2967 /* create_msg_discared_events() logs errors */
2968 *message
= create_msg_discarded_events(msg_it
);
2971 status
= CTF_MSG_ITER_STATUS_ERROR
;
2975 case STATE_EMIT_MSG_DISCARDED_PACKETS
:
2976 /* create_msg_discared_packets() logs errors */
2977 *message
= create_msg_discarded_packets(msg_it
);
2980 status
= CTF_MSG_ITER_STATUS_ERROR
;
2984 case STATE_EMIT_MSG_PACKET_BEGINNING
:
2985 status
= set_current_packet(msg_it
);
2986 if (status
!= CTF_MSG_ITER_STATUS_OK
) {
2990 if (G_UNLIKELY(msg_it
->meta
.tc
->quirks
.barectf_event_before_packet
)) {
2991 msg_it
->emit_delayed_packet_beginning_msg
= true;
2993 * There is no message to return yet as this
2994 * packet beginning message is delayed until we
2995 * decode the first event message of the
3000 /* create_msg_packet_beginning() logs errors */
3001 *message
= create_msg_packet_beginning(msg_it
, false);
3003 status
= CTF_MSG_ITER_STATUS_ERROR
;
3008 case STATE_EMIT_MSG_PACKET_END_SINGLE
:
3009 case STATE_EMIT_MSG_PACKET_END_MULTI
:
3010 /* create_msg_packet_end() logs errors */
3011 *message
= create_msg_packet_end(msg_it
);
3014 status
= CTF_MSG_ITER_STATUS_ERROR
;
3018 case STATE_EMIT_MSG_STREAM_BEGINNING
:
3019 /* create_msg_stream_beginning() logs errors */
3020 *message
= create_msg_stream_beginning(msg_it
);
3023 status
= CTF_MSG_ITER_STATUS_ERROR
;
3027 case STATE_EMIT_MSG_STREAM_END
:
3028 /* create_msg_stream_end() logs errors */
3029 *message
= create_msg_stream_end(msg_it
);
3032 status
= CTF_MSG_ITER_STATUS_ERROR
;
3037 status
= CTF_MSG_ITER_STATUS_EOF
;
3040 /* Non-emitting state: continue */
3050 enum ctf_msg_iter_status
decode_until_state( struct ctf_msg_iter
*msg_it
,
3051 enum state target_state_1
, enum state target_state_2
)
3053 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
3054 bt_self_component
*self_comp
= msg_it
->self_comp
;
3056 BT_ASSERT_DBG(msg_it
);
3057 msg_it
->set_stream
= false;
3061 * Check if we reached the state at which we want to stop
3064 if (msg_it
->state
== target_state_1
||
3065 msg_it
->state
== target_state_2
) {
3069 status
= handle_state(msg_it
);
3070 if (G_UNLIKELY(status
== CTF_MSG_ITER_STATUS_AGAIN
)) {
3071 BT_COMP_LOGD_STR("Medium returned CTF_MSG_ITER_STATUS_AGAIN.");
3073 } else if (G_UNLIKELY(status
!= CTF_MSG_ITER_STATUS_OK
)) {
3074 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
3075 "Cannot handle state: msg-it-addr=%p, state=%s",
3076 msg_it
, state_string(msg_it
->state
));
3080 switch (msg_it
->state
) {
3082 case STATE_SWITCH_PACKET
:
3083 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
3084 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
3085 case STATE_AFTER_TRACE_PACKET_HEADER
:
3086 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
3087 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
3088 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
3089 case STATE_CHECK_EMIT_MSG_STREAM_BEGINNING
:
3090 case STATE_EMIT_MSG_STREAM_BEGINNING
:
3091 case STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS
:
3092 case STATE_EMIT_MSG_DISCARDED_EVENTS
:
3093 case STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
:
3094 case STATE_EMIT_MSG_DISCARDED_PACKETS
:
3095 case STATE_EMIT_MSG_PACKET_BEGINNING
:
3096 case STATE_DSCOPE_EVENT_HEADER_BEGIN
:
3097 case STATE_DSCOPE_EVENT_HEADER_CONTINUE
:
3098 case STATE_AFTER_EVENT_HEADER
:
3099 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
:
3100 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
:
3101 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
:
3102 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
:
3103 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
3104 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
3105 case STATE_EMIT_MSG_EVENT
:
3106 case STATE_EMIT_QUEUED_MSG_EVENT
:
3107 case STATE_SKIP_PACKET_PADDING
:
3108 case STATE_EMIT_MSG_PACKET_END_MULTI
:
3109 case STATE_EMIT_MSG_PACKET_END_SINGLE
:
3110 case STATE_EMIT_QUEUED_MSG_PACKET_END
:
3111 case STATE_CHECK_EMIT_MSG_STREAM_END
:
3112 case STATE_EMIT_MSG_STREAM_END
:
3117 /* We should never get to the STATE_DONE state. */
3118 BT_COMP_LOGF("Unexpected state: msg-it-addr=%p, state=%s",
3119 msg_it
, state_string(msg_it
->state
));
3129 enum ctf_msg_iter_status
read_packet_header_context_fields(
3130 struct ctf_msg_iter
*msg_it
)
3133 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
3135 status
= decode_until_state(msg_it
, STATE_EMIT_MSG_PACKET_BEGINNING
, -1);
3136 if (status
!= CTF_MSG_ITER_STATUS_OK
) {
3140 ret
= set_current_packet_content_sizes(msg_it
);
3142 status
= CTF_MSG_ITER_STATUS_ERROR
;
3151 void ctf_msg_iter_set_medops_data(struct ctf_msg_iter
*msg_it
,
3155 msg_it
->medium
.data
= medops_data
;
3159 enum ctf_msg_iter_status
ctf_msg_iter_seek(struct ctf_msg_iter
*msg_it
,
3162 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
3163 bt_self_component
*self_comp
= msg_it
->self_comp
;
3164 enum ctf_msg_iter_medium_status medium_status
;
3168 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
3169 "Cannot seek to negative offset: offset=%jd",
3171 status
= CTF_MSG_ITER_STATUS_INVAL
;
3175 if (!msg_it
->medium
.medops
.seek
) {
3176 status
= CTF_MSG_ITER_STATUS_UNSUPPORTED
;
3177 BT_COMP_LOGD("Aborting seek as the iterator's underlying media does not implement seek support.");
3181 medium_status
= msg_it
->medium
.medops
.seek(
3182 CTF_MSG_ITER_SEEK_WHENCE_SET
, offset
, msg_it
->medium
.data
);
3183 if (medium_status
!= CTF_MSG_ITER_MEDIUM_STATUS_OK
) {
3184 if (medium_status
== CTF_MSG_ITER_MEDIUM_STATUS_EOF
) {
3185 status
= CTF_MSG_ITER_STATUS_EOF
;
3187 status
= CTF_MSG_ITER_STATUS_ERROR
;
3192 ctf_msg_iter_reset(msg_it
);
3193 msg_it
->cur_packet_offset
= offset
;
3200 enum ctf_msg_iter_status
clock_snapshot_at_msg_iter_state(
3201 struct ctf_msg_iter
*msg_it
, enum state target_state_1
,
3202 enum state target_state_2
, uint64_t *clock_snapshot
)
3204 enum ctf_msg_iter_status status
= CTF_MSG_ITER_STATUS_OK
;
3206 BT_ASSERT_DBG(msg_it
);
3207 BT_ASSERT_DBG(clock_snapshot
);
3208 status
= decode_until_state(msg_it
, target_state_1
, target_state_2
);
3209 if (status
!= CTF_MSG_ITER_STATUS_OK
) {
3213 *clock_snapshot
= msg_it
->default_clock_snapshot
;
3219 enum ctf_msg_iter_status
ctf_msg_iter_curr_packet_first_event_clock_snapshot(
3220 struct ctf_msg_iter
*msg_it
, uint64_t *first_clock_snapshot
)
3222 return clock_snapshot_at_msg_iter_state(msg_it
,
3223 STATE_AFTER_EVENT_HEADER
, -1, first_clock_snapshot
);
3227 enum ctf_msg_iter_status
ctf_msg_iter_curr_packet_last_event_clock_snapshot(
3228 struct ctf_msg_iter
*msg_it
, uint64_t *last_clock_snapshot
)
3230 return clock_snapshot_at_msg_iter_state(msg_it
,
3231 STATE_EMIT_MSG_PACKET_END_SINGLE
,
3232 STATE_EMIT_MSG_PACKET_END_MULTI
, last_clock_snapshot
);
3236 enum ctf_msg_iter_status
ctf_msg_iter_get_packet_properties(
3237 struct ctf_msg_iter
*msg_it
,
3238 struct ctf_msg_iter_packet_properties
*props
)
3240 enum ctf_msg_iter_status status
;
3242 BT_ASSERT_DBG(msg_it
);
3243 BT_ASSERT_DBG(props
);
3244 status
= read_packet_header_context_fields(msg_it
);
3245 if (status
!= CTF_MSG_ITER_STATUS_OK
) {
3249 props
->exp_packet_total_size
= msg_it
->cur_exp_packet_total_size
;
3250 props
->exp_packet_content_size
= msg_it
->cur_exp_packet_content_size
;
3251 props
->stream_class_id
= (uint64_t) msg_it
->cur_stream_class_id
;
3252 props
->data_stream_id
= msg_it
->cur_data_stream_id
;
3253 props
->snapshots
.discarded_events
= msg_it
->snapshots
.discarded_events
;
3254 props
->snapshots
.packets
= msg_it
->snapshots
.packets
;
3255 props
->snapshots
.beginning_clock
= msg_it
->snapshots
.beginning_clock
;
3256 props
->snapshots
.end_clock
= msg_it
->snapshots
.end_clock
;
3263 void ctf_msg_iter_set_emit_stream_beginning_message(struct ctf_msg_iter
*msg_it
,
3266 msg_it
->emit_stream_begin_msg
= val
;
3270 void ctf_msg_iter_set_emit_stream_end_message(struct ctf_msg_iter
*msg_it
,
3273 msg_it
->emit_stream_end_msg
= val
;
3277 void ctf_msg_iter_set_dry_run(struct ctf_msg_iter
*msg_it
,
3280 msg_it
->dry_run
= val
;