2 * Babeltrace - CTF message iterator
4 * Copyright (c) 2015-2018 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2015-2018 Philippe Proulx <pproulx@efficios.com>
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 #define BT_COMP_LOG_SELF_COMP (notit->self_comp)
27 #define BT_LOG_OUTPUT_LEVEL (notit->log_level)
28 #define BT_LOG_TAG "PLUGIN/CTF/MSG-ITER"
29 #include "logging/comp-logging.h"
36 #include "common/assert.h"
38 #include <babeltrace2/babeltrace.h>
39 #include "common/common.h"
44 #include "../bfcr/bfcr.h"
48 /* A visit stack entry */
51 * Current base field, one of:
63 /* Index of next field to set */
71 struct bt_msg_iter
*notit
;
73 /* Entries (struct stack_entry) */
76 /* Number of active entries */
84 STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
,
85 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
86 STATE_AFTER_TRACE_PACKET_HEADER
,
87 STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
,
88 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
89 STATE_AFTER_STREAM_PACKET_CONTEXT
,
90 STATE_CHECK_EMIT_MSG_STREAM_BEGINNING
,
91 STATE_EMIT_MSG_STREAM_BEGINNING
,
92 STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS
,
93 STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
,
94 STATE_EMIT_MSG_DISCARDED_EVENTS
,
95 STATE_EMIT_MSG_DISCARDED_PACKETS
,
96 STATE_EMIT_MSG_PACKET_BEGINNING
,
97 STATE_DSCOPE_EVENT_HEADER_BEGIN
,
98 STATE_DSCOPE_EVENT_HEADER_CONTINUE
,
99 STATE_AFTER_EVENT_HEADER
,
100 STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
,
101 STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
,
102 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
,
103 STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
,
104 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
105 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
106 STATE_EMIT_MSG_EVENT
,
107 STATE_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 */
129 /* Current message iterator to create messages (weak) */
130 bt_self_message_iterator
*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 bt_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 bt_msg_iter
*notit
)
341 struct stack
*stack
= NULL
;
343 stack
= g_new0(struct stack
, 1);
345 BT_COMP_LOGE_STR("Failed to allocate one stack.");
349 stack
->notit
= notit
;
350 stack
->entries
= g_array_new(FALSE
, TRUE
, sizeof(struct stack_entry
));
351 if (!stack
->entries
) {
352 BT_COMP_LOGE_STR("Failed to allocate a GArray.");
356 BT_COMP_LOGD("Created stack: notit-addr=%p, stack-addr=%p", notit
, stack
);
368 void stack_destroy(struct stack
*stack
)
370 struct bt_msg_iter
*notit
;
372 BT_ASSERT_DBG(stack
);
373 notit
= stack
->notit
;
374 BT_COMP_LOGD("Destroying stack: addr=%p", stack
);
376 if (stack
->entries
) {
377 g_array_free(stack
->entries
, TRUE
);
384 void stack_push(struct stack
*stack
, bt_field
*base
)
386 struct stack_entry
*entry
;
387 struct bt_msg_iter
*notit
;
389 BT_ASSERT_DBG(stack
);
390 notit
= stack
->notit
;
392 BT_COMP_LOGT("Pushing base field on stack: stack-addr=%p, "
393 "stack-size-before=%zu, stack-size-after=%zu",
394 stack
, stack
->size
, stack
->size
+ 1);
396 if (stack
->entries
->len
== stack
->size
) {
397 g_array_set_size(stack
->entries
, stack
->size
+ 1);
400 entry
= &g_array_index(stack
->entries
, struct stack_entry
, stack
->size
);
407 unsigned int stack_size(struct stack
*stack
)
409 BT_ASSERT_DBG(stack
);
414 void stack_pop(struct stack
*stack
)
416 struct bt_msg_iter
*notit
;
418 BT_ASSERT_DBG(stack
);
419 BT_ASSERT_DBG(stack_size(stack
));
420 notit
= stack
->notit
;
421 BT_COMP_LOGT("Popping from stack: "
422 "stack-addr=%p, stack-size-before=%zu, stack-size-after=%zu",
423 stack
, stack
->size
, stack
->size
- 1);
428 struct stack_entry
*stack_top(struct stack
*stack
)
430 BT_ASSERT_DBG(stack
);
431 BT_ASSERT_DBG(stack_size(stack
));
432 return &g_array_index(stack
->entries
, struct stack_entry
,
437 bool stack_empty(struct stack
*stack
)
439 return stack_size(stack
) == 0;
443 void stack_clear(struct stack
*stack
)
445 BT_ASSERT_DBG(stack
);
450 enum bt_msg_iter_status
msg_iter_status_from_m_status(
451 enum bt_msg_iter_medium_status m_status
)
453 /* They are the same */
454 return (int) m_status
;
458 size_t buf_size_bits(struct bt_msg_iter
*notit
)
460 return notit
->buf
.sz
* 8;
464 size_t buf_available_bits(struct bt_msg_iter
*notit
)
466 return buf_size_bits(notit
) - notit
->buf
.at
;
470 size_t packet_at(struct bt_msg_iter
*notit
)
472 return notit
->buf
.packet_offset
+ notit
->buf
.at
;
476 void buf_consume_bits(struct bt_msg_iter
*notit
, size_t incr
)
478 BT_COMP_LOGT("Advancing cursor: notit-addr=%p, cur-before=%zu, cur-after=%zu",
479 notit
, notit
->buf
.at
, notit
->buf
.at
+ incr
);
480 notit
->buf
.at
+= incr
;
484 enum bt_msg_iter_status
request_medium_bytes(
485 struct bt_msg_iter
*notit
)
487 uint8_t *buffer_addr
= NULL
;
488 size_t buffer_sz
= 0;
489 enum bt_msg_iter_medium_status m_status
;
491 BT_COMP_LOGD("Calling user function (request bytes): notit-addr=%p, "
492 "request-size=%zu", notit
, notit
->medium
.max_request_sz
);
493 m_status
= notit
->medium
.medops
.request_bytes(
494 notit
->medium
.max_request_sz
, &buffer_addr
,
495 &buffer_sz
, notit
->medium
.data
);
496 BT_COMP_LOGD("User function returned: status=%s, buf-addr=%p, buf-size=%zu",
497 bt_msg_iter_medium_status_string(m_status
),
498 buffer_addr
, buffer_sz
);
499 if (m_status
== BT_MSG_ITER_MEDIUM_STATUS_OK
) {
500 BT_ASSERT(buffer_sz
!= 0);
502 /* New packet offset is old one + old size (in bits) */
503 notit
->buf
.packet_offset
+= buf_size_bits(notit
);
505 /* Restart at the beginning of the new medium buffer */
507 notit
->buf
.last_eh_at
= SIZE_MAX
;
509 /* New medium buffer size */
510 notit
->buf
.sz
= buffer_sz
;
512 /* New medium buffer address */
513 notit
->buf
.addr
= buffer_addr
;
515 BT_COMP_LOGD("User function returned new bytes: "
516 "packet-offset=%zu, cur=%zu, size=%zu, addr=%p",
517 notit
->buf
.packet_offset
, notit
->buf
.at
,
518 notit
->buf
.sz
, notit
->buf
.addr
);
519 BT_COMP_LOGD_MEM(buffer_addr
, buffer_sz
, "Returned bytes at %p:",
521 } else if (m_status
== BT_MSG_ITER_MEDIUM_STATUS_EOF
) {
523 * User returned end of stream: validate that we're not
524 * in the middle of a packet header, packet context, or
527 if (notit
->cur_exp_packet_total_size
>= 0) {
528 if (packet_at(notit
) ==
529 notit
->cur_exp_packet_total_size
) {
533 if (packet_at(notit
) == 0) {
537 if (notit
->buf
.last_eh_at
!= SIZE_MAX
&&
538 notit
->buf
.at
== notit
->buf
.last_eh_at
) {
543 /* All other states are invalid */
544 BT_COMP_LOGW("User function returned %s, but message iterator is in an unexpected state: "
545 "state=%s, cur-packet-size=%" PRId64
", cur=%zu, "
546 "packet-cur=%zu, last-eh-at=%zu",
547 bt_msg_iter_medium_status_string(m_status
),
548 state_string(notit
->state
),
549 notit
->cur_exp_packet_total_size
,
550 notit
->buf
.at
, packet_at(notit
),
551 notit
->buf
.last_eh_at
);
552 m_status
= BT_MSG_ITER_MEDIUM_STATUS_ERROR
;
553 } else if (m_status
< 0) {
554 BT_COMP_LOGW("User function failed: status=%s",
555 bt_msg_iter_medium_status_string(m_status
));
559 return msg_iter_status_from_m_status(m_status
);
563 enum bt_msg_iter_status
buf_ensure_available_bits(
564 struct bt_msg_iter
*notit
)
566 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
568 if (G_UNLIKELY(buf_available_bits(notit
) == 0)) {
570 * This _cannot_ return BT_MSG_ITER_STATUS_OK
573 status
= request_medium_bytes(notit
);
580 enum bt_msg_iter_status
read_dscope_begin_state(
581 struct bt_msg_iter
*notit
,
582 struct ctf_field_class
*dscope_fc
,
583 enum state done_state
, enum state continue_state
,
584 bt_field
*dscope_field
)
586 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
587 enum bt_bfcr_status bfcr_status
;
588 size_t consumed_bits
;
590 notit
->cur_dscope_field
= dscope_field
;
591 BT_COMP_LOGT("Starting BFCR: notit-addr=%p, bfcr-addr=%p, fc-addr=%p",
592 notit
, notit
->bfcr
, dscope_fc
);
593 consumed_bits
= bt_bfcr_start(notit
->bfcr
, dscope_fc
,
594 notit
->buf
.addr
, notit
->buf
.at
, packet_at(notit
),
595 notit
->buf
.sz
, &bfcr_status
);
596 BT_COMP_LOGT("BFCR consumed bits: size=%zu", consumed_bits
);
598 switch (bfcr_status
) {
599 case BT_BFCR_STATUS_OK
:
600 /* Field class was read completely */
601 BT_COMP_LOGT_STR("Field was completely decoded.");
602 notit
->state
= done_state
;
604 case BT_BFCR_STATUS_EOF
:
605 BT_COMP_LOGT_STR("BFCR needs more data to decode field completely.");
606 notit
->state
= continue_state
;
609 BT_COMP_LOGW("BFCR failed to start: notit-addr=%p, bfcr-addr=%p, "
610 "status=%s", notit
, notit
->bfcr
,
611 bt_bfcr_status_string(bfcr_status
));
612 status
= BT_MSG_ITER_STATUS_ERROR
;
616 /* Consume bits now since we know we're not in an error state */
617 buf_consume_bits(notit
, consumed_bits
);
624 enum bt_msg_iter_status
read_dscope_continue_state(
625 struct bt_msg_iter
*notit
, enum state done_state
)
627 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
628 enum bt_bfcr_status bfcr_status
;
629 size_t consumed_bits
;
631 BT_COMP_LOGT("Continuing BFCR: notit-addr=%p, bfcr-addr=%p",
634 status
= buf_ensure_available_bits(notit
);
635 if (status
!= BT_MSG_ITER_STATUS_OK
) {
637 BT_COMP_LOGW("Cannot ensure that buffer has at least one byte: "
638 "msg-addr=%p, status=%s",
639 notit
, bt_msg_iter_status_string(status
));
641 BT_COMP_LOGT("Cannot ensure that buffer has at least one byte: "
642 "msg-addr=%p, status=%s",
643 notit
, bt_msg_iter_status_string(status
));
649 consumed_bits
= bt_bfcr_continue(notit
->bfcr
, notit
->buf
.addr
,
650 notit
->buf
.sz
, &bfcr_status
);
651 BT_COMP_LOGT("BFCR consumed bits: size=%zu", consumed_bits
);
653 switch (bfcr_status
) {
654 case BT_BFCR_STATUS_OK
:
655 /* Type was read completely. */
656 BT_COMP_LOGT_STR("Field was completely decoded.");
657 notit
->state
= done_state
;
659 case BT_BFCR_STATUS_EOF
:
660 /* Stay in this continue state. */
661 BT_COMP_LOGT_STR("BFCR needs more data to decode field completely.");
664 BT_COMP_LOGW("BFCR failed to continue: notit-addr=%p, bfcr-addr=%p, "
665 "status=%s", notit
, notit
->bfcr
,
666 bt_bfcr_status_string(bfcr_status
));
667 status
= BT_MSG_ITER_STATUS_ERROR
;
671 /* Consume bits now since we know we're not in an error state. */
672 buf_consume_bits(notit
, consumed_bits
);
678 void release_event_dscopes(struct bt_msg_iter
*notit
)
680 notit
->dscopes
.event_common_context
= NULL
;
681 notit
->dscopes
.event_spec_context
= NULL
;
682 notit
->dscopes
.event_payload
= NULL
;
686 void release_all_dscopes(struct bt_msg_iter
*notit
)
688 notit
->dscopes
.stream_packet_context
= NULL
;
690 if (notit
->packet_context_field
) {
691 bt_packet_context_field_release(notit
->packet_context_field
);
692 notit
->packet_context_field
= NULL
;
695 release_event_dscopes(notit
);
699 enum bt_msg_iter_status
switch_packet_state(struct bt_msg_iter
*notit
)
701 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
704 * We don't put the stream class here because we need to make
705 * sure that all the packets processed by the same message
706 * iterator refer to the same stream class (the first one).
710 if (notit
->cur_exp_packet_total_size
!= -1) {
711 notit
->cur_packet_offset
+= notit
->cur_exp_packet_total_size
;
714 BT_COMP_LOGD("Switching packet: notit-addr=%p, cur=%zu, "
715 "packet-offset=%" PRId64
, notit
, notit
->buf
.at
,
716 notit
->cur_packet_offset
);
717 stack_clear(notit
->stack
);
718 notit
->meta
.ec
= NULL
;
719 BT_PACKET_PUT_REF_AND_RESET(notit
->packet
);
720 BT_MESSAGE_PUT_REF_AND_RESET(notit
->event_msg
);
721 release_all_dscopes(notit
);
722 notit
->cur_dscope_field
= NULL
;
725 * Adjust current buffer so that addr points to the beginning of the new
728 if (notit
->buf
.addr
) {
729 size_t consumed_bytes
= (size_t) (notit
->buf
.at
/ CHAR_BIT
);
731 /* Packets are assumed to start on a byte frontier. */
732 if (notit
->buf
.at
% CHAR_BIT
) {
733 BT_COMP_LOGW("Cannot switch packet: current position is not a multiple of 8: "
734 "notit-addr=%p, cur=%zu", notit
, notit
->buf
.at
);
735 status
= BT_MSG_ITER_STATUS_ERROR
;
739 notit
->buf
.addr
+= consumed_bytes
;
740 notit
->buf
.sz
-= consumed_bytes
;
742 notit
->buf
.packet_offset
= 0;
743 BT_COMP_LOGD("Adjusted buffer: addr=%p, size=%zu",
744 notit
->buf
.addr
, notit
->buf
.sz
);
747 notit
->cur_exp_packet_content_size
= -1;
748 notit
->cur_exp_packet_total_size
= -1;
749 notit
->cur_stream_class_id
= -1;
750 notit
->cur_event_class_id
= -1;
751 notit
->cur_data_stream_id
= -1;
752 notit
->prev_packet_snapshots
= notit
->snapshots
;
753 notit
->snapshots
.discarded_events
= UINT64_C(-1);
754 notit
->snapshots
.packets
= UINT64_C(-1);
755 notit
->snapshots
.beginning_clock
= UINT64_C(-1);
756 notit
->snapshots
.end_clock
= UINT64_C(-1);
757 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
764 enum bt_msg_iter_status
read_packet_header_begin_state(
765 struct bt_msg_iter
*notit
)
767 struct ctf_field_class
*packet_header_fc
= NULL
;
768 enum bt_msg_iter_status ret
= BT_MSG_ITER_STATUS_OK
;
771 * Make sure at least one bit is available for this packet. An
772 * empty packet is impossible. If we reach the end of the medium
773 * at this point, then it's considered the end of the stream.
775 ret
= buf_ensure_available_bits(notit
);
777 case BT_MSG_ITER_STATUS_OK
:
779 case BT_MSG_ITER_STATUS_EOF
:
780 ret
= BT_MSG_ITER_STATUS_OK
;
781 notit
->state
= STATE_CHECK_EMIT_MSG_STREAM_END
;
787 /* Packet header class is common to the whole trace class. */
788 packet_header_fc
= notit
->meta
.tc
->packet_header_fc
;
789 if (!packet_header_fc
) {
790 notit
->state
= STATE_AFTER_TRACE_PACKET_HEADER
;
794 notit
->cur_stream_class_id
= -1;
795 notit
->cur_event_class_id
= -1;
796 notit
->cur_data_stream_id
= -1;
797 BT_COMP_LOGD("Decoding packet header field:"
798 "notit-addr=%p, trace-class-addr=%p, fc-addr=%p",
799 notit
, notit
->meta
.tc
, packet_header_fc
);
800 ret
= read_dscope_begin_state(notit
, packet_header_fc
,
801 STATE_AFTER_TRACE_PACKET_HEADER
,
802 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
, NULL
);
804 BT_COMP_LOGW("Cannot decode packet header field: "
805 "notit-addr=%p, trace-class-addr=%p, "
807 notit
, notit
->meta
.tc
, packet_header_fc
);
815 enum bt_msg_iter_status
read_packet_header_continue_state(
816 struct bt_msg_iter
*notit
)
818 return read_dscope_continue_state(notit
,
819 STATE_AFTER_TRACE_PACKET_HEADER
);
823 enum bt_msg_iter_status
set_current_stream_class(struct bt_msg_iter
*notit
)
825 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
826 struct ctf_stream_class
*new_stream_class
= NULL
;
828 if (notit
->cur_stream_class_id
== -1) {
830 * No current stream class ID field, therefore only one
833 if (notit
->meta
.tc
->stream_classes
->len
!= 1) {
834 BT_COMP_LOGW("Need exactly one stream class since there's "
835 "no stream class ID field: "
836 "notit-addr=%p", notit
);
837 status
= BT_MSG_ITER_STATUS_ERROR
;
841 new_stream_class
= notit
->meta
.tc
->stream_classes
->pdata
[0];
842 notit
->cur_stream_class_id
= new_stream_class
->id
;
845 new_stream_class
= ctf_trace_class_borrow_stream_class_by_id(
846 notit
->meta
.tc
, notit
->cur_stream_class_id
);
847 if (!new_stream_class
) {
848 BT_COMP_LOGW("No stream class with ID of stream class ID to use in trace class: "
849 "notit-addr=%p, stream-class-id=%" PRIu64
", "
850 "trace-class-addr=%p",
851 notit
, notit
->cur_stream_class_id
, notit
->meta
.tc
);
852 status
= BT_MSG_ITER_STATUS_ERROR
;
856 if (notit
->meta
.sc
) {
857 if (new_stream_class
!= notit
->meta
.sc
) {
858 BT_COMP_LOGW("Two packets refer to two different stream classes within the same packet sequence: "
859 "notit-addr=%p, prev-stream-class-addr=%p, "
860 "prev-stream-class-id=%" PRId64
", "
861 "next-stream-class-addr=%p, "
862 "next-stream-class-id=%" PRId64
", "
864 notit
, notit
->meta
.sc
,
867 new_stream_class
->id
,
869 status
= BT_MSG_ITER_STATUS_ERROR
;
873 notit
->meta
.sc
= new_stream_class
;
876 BT_COMP_LOGD("Set current stream class: "
877 "notit-addr=%p, stream-class-addr=%p, "
878 "stream-class-id=%" PRId64
,
879 notit
, notit
->meta
.sc
, notit
->meta
.sc
->id
);
886 enum bt_msg_iter_status
set_current_stream(struct bt_msg_iter
*notit
)
888 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
889 bt_stream
*stream
= NULL
;
891 BT_COMP_LOGD("Calling user function (get stream): notit-addr=%p, "
892 "stream-class-addr=%p, stream-class-id=%" PRId64
,
893 notit
, notit
->meta
.sc
,
895 stream
= notit
->medium
.medops
.borrow_stream(
896 notit
->meta
.sc
->ir_sc
, notit
->cur_data_stream_id
,
898 bt_stream_get_ref(stream
);
899 BT_COMP_LOGD("User function returned: stream-addr=%p", stream
);
901 BT_COMP_LOGW_STR("User function failed to return a stream object "
902 "for the given stream class.");
903 status
= BT_MSG_ITER_STATUS_ERROR
;
907 if (notit
->stream
&& stream
!= notit
->stream
) {
908 BT_COMP_LOGW("User function returned a different stream than the "
909 "previous one for the same sequence of packets.");
910 status
= BT_MSG_ITER_STATUS_ERROR
;
914 BT_STREAM_MOVE_REF(notit
->stream
, stream
);
917 bt_stream_put_ref(stream
);
922 enum bt_msg_iter_status
set_current_packet(struct bt_msg_iter
*notit
)
924 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
925 bt_packet
*packet
= NULL
;
927 BT_COMP_LOGD("Creating packet for packet message: "
928 "notit-addr=%p", notit
);
929 BT_COMP_LOGD("Creating packet from stream: "
930 "notit-addr=%p, stream-addr=%p, "
931 "stream-class-addr=%p, "
932 "stream-class-id=%" PRId64
,
933 notit
, notit
->stream
, notit
->meta
.sc
,
937 BT_ASSERT(notit
->stream
);
938 packet
= bt_packet_create(notit
->stream
);
940 BT_COMP_LOGE("Cannot create packet from stream: "
941 "notit-addr=%p, stream-addr=%p, "
942 "stream-class-addr=%p, "
943 "stream-class-id=%" PRId64
,
944 notit
, notit
->stream
, notit
->meta
.sc
,
952 BT_PACKET_PUT_REF_AND_RESET(packet
);
953 status
= BT_MSG_ITER_STATUS_ERROR
;
956 BT_PACKET_MOVE_REF(notit
->packet
, packet
);
961 enum bt_msg_iter_status
after_packet_header_state(
962 struct bt_msg_iter
*notit
)
964 enum bt_msg_iter_status status
;
966 status
= set_current_stream_class(notit
);
967 if (status
!= BT_MSG_ITER_STATUS_OK
) {
971 notit
->state
= STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
;
978 enum bt_msg_iter_status
read_packet_context_begin_state(
979 struct bt_msg_iter
*notit
)
981 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
982 struct ctf_field_class
*packet_context_fc
;
984 BT_ASSERT(notit
->meta
.sc
);
985 packet_context_fc
= notit
->meta
.sc
->packet_context_fc
;
986 if (!packet_context_fc
) {
987 BT_COMP_LOGD("No packet packet context field class in stream class: continuing: "
988 "notit-addr=%p, stream-class-addr=%p, "
989 "stream-class-id=%" PRId64
,
990 notit
, notit
->meta
.sc
,
992 notit
->state
= STATE_AFTER_STREAM_PACKET_CONTEXT
;
996 BT_ASSERT(!notit
->packet_context_field
);
998 if (packet_context_fc
->in_ir
&& !notit
->dry_run
) {
1000 * Create free packet context field from stream class.
1001 * This field is going to be moved to the packet once we
1002 * create it. We cannot create the packet now because a
1003 * packet is created from a stream, and this API must be
1004 * able to return the packet context properties without
1006 * (bt_msg_iter_get_packet_properties()).
1008 notit
->packet_context_field
=
1009 bt_packet_context_field_create(
1010 notit
->meta
.sc
->ir_sc
);
1011 if (!notit
->packet_context_field
) {
1012 BT_COMP_LOGE_STR("Cannot create packet context field wrapper from stream class.");
1013 status
= BT_MSG_ITER_STATUS_ERROR
;
1017 notit
->dscopes
.stream_packet_context
=
1018 bt_packet_context_field_borrow_field(
1019 notit
->packet_context_field
);
1020 BT_ASSERT(notit
->dscopes
.stream_packet_context
);
1023 BT_COMP_LOGD("Decoding packet context field: "
1024 "notit-addr=%p, stream-class-addr=%p, "
1025 "stream-class-id=%" PRId64
", fc-addr=%p",
1026 notit
, notit
->meta
.sc
,
1027 notit
->meta
.sc
->id
, packet_context_fc
);
1028 status
= read_dscope_begin_state(notit
, packet_context_fc
,
1029 STATE_AFTER_STREAM_PACKET_CONTEXT
,
1030 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
1031 notit
->dscopes
.stream_packet_context
);
1033 BT_COMP_LOGW("Cannot decode packet context field: "
1034 "notit-addr=%p, stream-class-addr=%p, "
1035 "stream-class-id=%" PRId64
", fc-addr=%p",
1036 notit
, notit
->meta
.sc
,
1046 enum bt_msg_iter_status
read_packet_context_continue_state(
1047 struct bt_msg_iter
*notit
)
1049 return read_dscope_continue_state(notit
,
1050 STATE_AFTER_STREAM_PACKET_CONTEXT
);
1054 enum bt_msg_iter_status
set_current_packet_content_sizes(
1055 struct bt_msg_iter
*notit
)
1057 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1059 if (notit
->cur_exp_packet_total_size
== -1) {
1060 if (notit
->cur_exp_packet_content_size
!= -1) {
1061 notit
->cur_exp_packet_total_size
=
1062 notit
->cur_exp_packet_content_size
;
1065 if (notit
->cur_exp_packet_content_size
== -1) {
1066 notit
->cur_exp_packet_content_size
=
1067 notit
->cur_exp_packet_total_size
;
1071 BT_ASSERT((notit
->cur_exp_packet_total_size
>= 0 &&
1072 notit
->cur_exp_packet_content_size
>= 0) ||
1073 (notit
->cur_exp_packet_total_size
< 0 &&
1074 notit
->cur_exp_packet_content_size
< 0));
1076 if (notit
->cur_exp_packet_content_size
>
1077 notit
->cur_exp_packet_total_size
) {
1078 BT_COMP_LOGW("Invalid packet or content size: "
1079 "content size is greater than packet size: "
1080 "notit-addr=%p, packet-context-field-addr=%p, "
1081 "packet-size=%" PRId64
", content-size=%" PRId64
,
1082 notit
, notit
->dscopes
.stream_packet_context
,
1083 notit
->cur_exp_packet_total_size
,
1084 notit
->cur_exp_packet_content_size
);
1085 status
= BT_MSG_ITER_STATUS_ERROR
;
1089 BT_COMP_LOGD("Set current packet and content sizes: "
1090 "notit-addr=%p, packet-size=%" PRIu64
", content-size=%" PRIu64
,
1091 notit
, notit
->cur_exp_packet_total_size
,
1092 notit
->cur_exp_packet_content_size
);
1099 enum bt_msg_iter_status
after_packet_context_state(struct bt_msg_iter
*notit
)
1101 enum bt_msg_iter_status status
;
1103 status
= set_current_packet_content_sizes(notit
);
1104 if (status
!= BT_MSG_ITER_STATUS_OK
) {
1108 if (notit
->stream
) {
1110 * Stream exists, which means we already emitted at
1111 * least one packet beginning message, so the initial
1112 * stream beginning message was also emitted.
1114 notit
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS
;
1116 notit
->state
= STATE_CHECK_EMIT_MSG_STREAM_BEGINNING
;
1124 enum bt_msg_iter_status
read_event_header_begin_state(struct bt_msg_iter
*notit
)
1126 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1127 struct ctf_field_class
*event_header_fc
= NULL
;
1129 /* Reset the position of the last event header */
1130 notit
->buf
.last_eh_at
= notit
->buf
.at
;
1131 notit
->cur_event_class_id
= -1;
1133 /* Check if we have some content left */
1134 if (notit
->cur_exp_packet_content_size
>= 0) {
1135 if (G_UNLIKELY(packet_at(notit
) ==
1136 notit
->cur_exp_packet_content_size
)) {
1137 /* No more events! */
1138 BT_COMP_LOGD("Reached end of packet: notit-addr=%p, "
1139 "cur=%zu", notit
, packet_at(notit
));
1140 notit
->state
= STATE_EMIT_MSG_PACKET_END_MULTI
;
1142 } else if (G_UNLIKELY(packet_at(notit
) >
1143 notit
->cur_exp_packet_content_size
)) {
1144 /* That's not supposed to happen */
1145 BT_COMP_LOGD("Before decoding event header field: cursor is passed the packet's content: "
1146 "notit-addr=%p, content-size=%" PRId64
", "
1148 notit
->cur_exp_packet_content_size
,
1150 status
= BT_MSG_ITER_STATUS_ERROR
;
1155 * "Infinite" content: we're done when the medium has
1156 * nothing else for us.
1158 status
= buf_ensure_available_bits(notit
);
1160 case BT_MSG_ITER_STATUS_OK
:
1162 case BT_MSG_ITER_STATUS_EOF
:
1163 status
= BT_MSG_ITER_STATUS_OK
;
1164 notit
->state
= STATE_EMIT_MSG_PACKET_END_SINGLE
;
1171 release_event_dscopes(notit
);
1172 BT_ASSERT(notit
->meta
.sc
);
1173 event_header_fc
= notit
->meta
.sc
->event_header_fc
;
1174 if (!event_header_fc
) {
1175 notit
->state
= STATE_AFTER_EVENT_HEADER
;
1179 BT_COMP_LOGD("Decoding event header field: "
1180 "notit-addr=%p, stream-class-addr=%p, "
1181 "stream-class-id=%" PRId64
", "
1183 notit
, notit
->meta
.sc
,
1186 status
= read_dscope_begin_state(notit
, event_header_fc
,
1187 STATE_AFTER_EVENT_HEADER
,
1188 STATE_DSCOPE_EVENT_HEADER_CONTINUE
, NULL
);
1190 BT_COMP_LOGW("Cannot decode event header field: "
1191 "notit-addr=%p, stream-class-addr=%p, "
1192 "stream-class-id=%" PRId64
", fc-addr=%p",
1193 notit
, notit
->meta
.sc
,
1203 enum bt_msg_iter_status
read_event_header_continue_state(
1204 struct bt_msg_iter
*notit
)
1206 return read_dscope_continue_state(notit
,
1207 STATE_AFTER_EVENT_HEADER
);
1211 enum bt_msg_iter_status
set_current_event_class(struct bt_msg_iter
*notit
)
1213 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1215 struct ctf_event_class
*new_event_class
= NULL
;
1217 if (notit
->cur_event_class_id
== -1) {
1219 * No current event class ID field, therefore only one
1222 if (notit
->meta
.sc
->event_classes
->len
!= 1) {
1223 BT_COMP_LOGW("Need exactly one event class since there's "
1224 "no event class ID field: "
1225 "notit-addr=%p", notit
);
1226 status
= BT_MSG_ITER_STATUS_ERROR
;
1230 new_event_class
= notit
->meta
.sc
->event_classes
->pdata
[0];
1231 notit
->cur_event_class_id
= new_event_class
->id
;
1234 new_event_class
= ctf_stream_class_borrow_event_class_by_id(
1235 notit
->meta
.sc
, notit
->cur_event_class_id
);
1236 if (!new_event_class
) {
1237 BT_COMP_LOGW("No event class with ID of event class ID to use in stream class: "
1238 "notit-addr=%p, stream-class-id=%" PRIu64
", "
1239 "event-class-id=%" PRIu64
", "
1240 "trace-class-addr=%p",
1241 notit
, notit
->meta
.sc
->id
, notit
->cur_event_class_id
,
1243 status
= BT_MSG_ITER_STATUS_ERROR
;
1247 notit
->meta
.ec
= new_event_class
;
1248 BT_COMP_LOGD("Set current event class: "
1249 "notit-addr=%p, event-class-addr=%p, "
1250 "event-class-id=%" PRId64
", "
1251 "event-class-name=\"%s\"",
1252 notit
, notit
->meta
.ec
, notit
->meta
.ec
->id
,
1253 notit
->meta
.ec
->name
->str
);
1260 enum bt_msg_iter_status
set_current_event_message(
1261 struct bt_msg_iter
*notit
)
1263 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1264 bt_message
*msg
= NULL
;
1266 BT_ASSERT_DBG(notit
->meta
.ec
);
1267 BT_ASSERT_DBG(notit
->packet
);
1268 BT_COMP_LOGD("Creating event message from event class and packet: "
1269 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", packet-addr=%p",
1270 notit
, notit
->meta
.ec
,
1271 notit
->meta
.ec
->name
->str
,
1273 BT_ASSERT_DBG(notit
->msg_iter
);
1274 BT_ASSERT_DBG(notit
->meta
.sc
);
1276 if (bt_stream_class_borrow_default_clock_class(notit
->meta
.sc
->ir_sc
)) {
1277 msg
= bt_message_event_create_with_packet_and_default_clock_snapshot(
1278 notit
->msg_iter
, notit
->meta
.ec
->ir_ec
,
1279 notit
->packet
, notit
->default_clock_snapshot
);
1281 msg
= bt_message_event_create_with_packet(notit
->msg_iter
,
1282 notit
->meta
.ec
->ir_ec
, notit
->packet
);
1286 BT_COMP_LOGE("Cannot create event message: "
1287 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", "
1289 notit
, notit
->meta
.ec
,
1290 notit
->meta
.ec
->name
->str
,
1298 BT_MESSAGE_PUT_REF_AND_RESET(msg
);
1299 status
= BT_MSG_ITER_STATUS_ERROR
;
1302 BT_MESSAGE_MOVE_REF(notit
->event_msg
, msg
);
1307 enum bt_msg_iter_status
after_event_header_state(
1308 struct bt_msg_iter
*notit
)
1310 enum bt_msg_iter_status status
;
1312 status
= set_current_event_class(notit
);
1313 if (status
!= BT_MSG_ITER_STATUS_OK
) {
1317 if (G_UNLIKELY(notit
->dry_run
)) {
1321 status
= set_current_event_message(notit
);
1322 if (status
!= BT_MSG_ITER_STATUS_OK
) {
1326 notit
->event
= bt_message_event_borrow_event(
1328 BT_ASSERT_DBG(notit
->event
);
1331 notit
->state
= STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
;
1338 enum bt_msg_iter_status
read_event_common_context_begin_state(
1339 struct bt_msg_iter
*notit
)
1341 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1342 struct ctf_field_class
*event_common_context_fc
;
1344 event_common_context_fc
= notit
->meta
.sc
->event_common_context_fc
;
1345 if (!event_common_context_fc
) {
1346 notit
->state
= STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
;
1350 if (event_common_context_fc
->in_ir
&& !notit
->dry_run
) {
1351 BT_ASSERT_DBG(!notit
->dscopes
.event_common_context
);
1352 notit
->dscopes
.event_common_context
=
1353 bt_event_borrow_common_context_field(
1355 BT_ASSERT_DBG(notit
->dscopes
.event_common_context
);
1358 BT_COMP_LOGT("Decoding event common context field: "
1359 "notit-addr=%p, stream-class-addr=%p, "
1360 "stream-class-id=%" PRId64
", "
1362 notit
, notit
->meta
.sc
,
1364 event_common_context_fc
);
1365 status
= read_dscope_begin_state(notit
, event_common_context_fc
,
1366 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
,
1367 STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
,
1368 notit
->dscopes
.event_common_context
);
1370 BT_COMP_LOGW("Cannot decode event common context field: "
1371 "notit-addr=%p, stream-class-addr=%p, "
1372 "stream-class-id=%" PRId64
", fc-addr=%p",
1373 notit
, notit
->meta
.sc
,
1375 event_common_context_fc
);
1383 enum bt_msg_iter_status
read_event_common_context_continue_state(
1384 struct bt_msg_iter
*notit
)
1386 return read_dscope_continue_state(notit
,
1387 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
);
1391 enum bt_msg_iter_status
read_event_spec_context_begin_state(
1392 struct bt_msg_iter
*notit
)
1394 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1395 struct ctf_field_class
*event_spec_context_fc
;
1397 event_spec_context_fc
= notit
->meta
.ec
->spec_context_fc
;
1398 if (!event_spec_context_fc
) {
1399 notit
->state
= STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
;
1403 if (event_spec_context_fc
->in_ir
&& !notit
->dry_run
) {
1404 BT_ASSERT_DBG(!notit
->dscopes
.event_spec_context
);
1405 notit
->dscopes
.event_spec_context
=
1406 bt_event_borrow_specific_context_field(
1408 BT_ASSERT_DBG(notit
->dscopes
.event_spec_context
);
1411 BT_COMP_LOGT("Decoding event specific context field: "
1412 "notit-addr=%p, event-class-addr=%p, "
1413 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1415 notit
, notit
->meta
.ec
,
1416 notit
->meta
.ec
->name
->str
,
1418 event_spec_context_fc
);
1419 status
= read_dscope_begin_state(notit
, event_spec_context_fc
,
1420 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
1421 STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
,
1422 notit
->dscopes
.event_spec_context
);
1424 BT_COMP_LOGW("Cannot decode event specific context field: "
1425 "notit-addr=%p, event-class-addr=%p, "
1426 "event-class-name=\"%s\", "
1427 "event-class-id=%" PRId64
", fc-addr=%p",
1428 notit
, notit
->meta
.ec
,
1429 notit
->meta
.ec
->name
->str
,
1431 event_spec_context_fc
);
1439 enum bt_msg_iter_status
read_event_spec_context_continue_state(
1440 struct bt_msg_iter
*notit
)
1442 return read_dscope_continue_state(notit
,
1443 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
);
1447 enum bt_msg_iter_status
read_event_payload_begin_state(
1448 struct bt_msg_iter
*notit
)
1450 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1451 struct ctf_field_class
*event_payload_fc
;
1453 event_payload_fc
= notit
->meta
.ec
->payload_fc
;
1454 if (!event_payload_fc
) {
1455 notit
->state
= STATE_EMIT_MSG_EVENT
;
1459 if (event_payload_fc
->in_ir
&& !notit
->dry_run
) {
1460 BT_ASSERT_DBG(!notit
->dscopes
.event_payload
);
1461 notit
->dscopes
.event_payload
=
1462 bt_event_borrow_payload_field(
1464 BT_ASSERT_DBG(notit
->dscopes
.event_payload
);
1467 BT_COMP_LOGT("Decoding event payload field: "
1468 "notit-addr=%p, event-class-addr=%p, "
1469 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1471 notit
, notit
->meta
.ec
,
1472 notit
->meta
.ec
->name
->str
,
1475 status
= read_dscope_begin_state(notit
, event_payload_fc
,
1476 STATE_EMIT_MSG_EVENT
,
1477 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
1478 notit
->dscopes
.event_payload
);
1480 BT_COMP_LOGW("Cannot decode event payload field: "
1481 "notit-addr=%p, event-class-addr=%p, "
1482 "event-class-name=\"%s\", "
1483 "event-class-id=%" PRId64
", fc-addr=%p",
1484 notit
, notit
->meta
.ec
,
1485 notit
->meta
.ec
->name
->str
,
1495 enum bt_msg_iter_status
read_event_payload_continue_state(
1496 struct bt_msg_iter
*notit
)
1498 return read_dscope_continue_state(notit
, STATE_EMIT_MSG_EVENT
);
1502 enum bt_msg_iter_status
skip_packet_padding_state(struct bt_msg_iter
*notit
)
1504 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1505 size_t bits_to_skip
;
1506 const enum state next_state
= STATE_SWITCH_PACKET
;
1508 BT_ASSERT(notit
->cur_exp_packet_total_size
> 0);
1509 bits_to_skip
= notit
->cur_exp_packet_total_size
- packet_at(notit
);
1510 if (bits_to_skip
== 0) {
1511 notit
->state
= next_state
;
1514 size_t bits_to_consume
;
1516 BT_COMP_LOGD("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu",
1517 bits_to_skip
, notit
, bits_to_skip
);
1518 status
= buf_ensure_available_bits(notit
);
1519 if (status
!= BT_MSG_ITER_STATUS_OK
) {
1523 bits_to_consume
= MIN(buf_available_bits(notit
), bits_to_skip
);
1524 BT_COMP_LOGD("Skipping %zu bits of padding: notit-addr=%p, size=%zu",
1525 bits_to_consume
, notit
, bits_to_consume
);
1526 buf_consume_bits(notit
, bits_to_consume
);
1527 bits_to_skip
= notit
->cur_exp_packet_total_size
-
1529 if (bits_to_skip
== 0) {
1530 notit
->state
= next_state
;
1540 enum bt_msg_iter_status
check_emit_msg_stream_beginning_state(
1541 struct bt_msg_iter
*notit
)
1543 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1545 if (notit
->set_stream
) {
1546 status
= set_current_stream(notit
);
1547 if (status
!= BT_MSG_ITER_STATUS_OK
) {
1552 if (notit
->emit_stream_begin_msg
) {
1553 notit
->state
= STATE_EMIT_MSG_STREAM_BEGINNING
;
1555 /* Stream's first packet */
1556 notit
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS
;
1564 enum bt_msg_iter_status
check_emit_msg_discarded_events(
1565 struct bt_msg_iter
*notit
)
1567 notit
->state
= STATE_EMIT_MSG_DISCARDED_EVENTS
;
1569 if (!notit
->meta
.sc
->has_discarded_events
) {
1570 notit
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
;
1574 if (notit
->prev_packet_snapshots
.discarded_events
== UINT64_C(-1)) {
1575 if (notit
->snapshots
.discarded_events
== 0 ||
1576 notit
->snapshots
.discarded_events
== UINT64_C(-1)) {
1578 * Stream's first packet with no discarded
1579 * events or no information about discarded
1580 * events: do not emit.
1582 notit
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
;
1586 * If the previous packet has a value for this counter,
1587 * then this counter is defined for the whole stream.
1589 BT_ASSERT(notit
->snapshots
.discarded_events
!= UINT64_C(-1));
1591 if (notit
->snapshots
.discarded_events
-
1592 notit
->prev_packet_snapshots
.discarded_events
== 0) {
1594 * No discarded events since previous packet: do
1597 notit
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
;
1602 return BT_MSG_ITER_STATUS_OK
;
1606 enum bt_msg_iter_status
check_emit_msg_discarded_packets(
1607 struct bt_msg_iter
*notit
)
1609 notit
->state
= STATE_EMIT_MSG_DISCARDED_PACKETS
;
1611 if (!notit
->meta
.sc
->has_discarded_packets
) {
1612 notit
->state
= STATE_EMIT_MSG_PACKET_BEGINNING
;
1616 if (notit
->prev_packet_snapshots
.packets
== UINT64_C(-1)) {
1618 * Stream's first packet or no information about
1619 * discarded packets: do not emit. In other words, if
1620 * this is the first packet and its sequence number is
1621 * not 0, do not consider that packets were previously
1622 * lost: we might be reading a partial stream (LTTng
1623 * snapshot for example).
1625 notit
->state
= STATE_EMIT_MSG_PACKET_BEGINNING
;
1628 * If the previous packet has a value for this counter,
1629 * then this counter is defined for the whole stream.
1631 BT_ASSERT(notit
->snapshots
.packets
!= UINT64_C(-1));
1633 if (notit
->snapshots
.packets
-
1634 notit
->prev_packet_snapshots
.packets
<= 1) {
1636 * No discarded packets since previous packet:
1639 notit
->state
= STATE_EMIT_MSG_PACKET_BEGINNING
;
1644 return BT_MSG_ITER_STATUS_OK
;
1648 enum bt_msg_iter_status
check_emit_msg_stream_end(
1649 struct bt_msg_iter
*notit
)
1651 if (notit
->emit_stream_end_msg
) {
1652 notit
->state
= STATE_EMIT_MSG_STREAM_END
;
1654 notit
->state
= STATE_DONE
;
1657 return BT_MSG_ITER_STATUS_OK
;
1661 enum bt_msg_iter_status
handle_state(struct bt_msg_iter
*notit
)
1663 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1664 const enum state state
= notit
->state
;
1666 BT_COMP_LOGT("Handling state: notit-addr=%p, state=%s",
1667 notit
, state_string(state
));
1669 // TODO: optimalize!
1672 notit
->state
= STATE_SWITCH_PACKET
;
1674 case STATE_SWITCH_PACKET
:
1675 status
= switch_packet_state(notit
);
1677 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
1678 status
= read_packet_header_begin_state(notit
);
1680 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
1681 status
= read_packet_header_continue_state(notit
);
1683 case STATE_AFTER_TRACE_PACKET_HEADER
:
1684 status
= after_packet_header_state(notit
);
1686 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
1687 status
= read_packet_context_begin_state(notit
);
1689 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
1690 status
= read_packet_context_continue_state(notit
);
1692 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
1693 status
= after_packet_context_state(notit
);
1695 case STATE_CHECK_EMIT_MSG_STREAM_BEGINNING
:
1696 status
= check_emit_msg_stream_beginning_state(notit
);
1698 case STATE_EMIT_MSG_STREAM_BEGINNING
:
1699 notit
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS
;
1701 case STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS
:
1702 status
= check_emit_msg_discarded_events(notit
);
1704 case STATE_EMIT_MSG_DISCARDED_EVENTS
:
1705 notit
->state
= STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
;
1707 case STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
:
1708 status
= check_emit_msg_discarded_packets(notit
);
1710 case STATE_EMIT_MSG_DISCARDED_PACKETS
:
1711 notit
->state
= STATE_EMIT_MSG_PACKET_BEGINNING
;
1713 case STATE_EMIT_MSG_PACKET_BEGINNING
:
1714 notit
->state
= STATE_DSCOPE_EVENT_HEADER_BEGIN
;
1716 case STATE_DSCOPE_EVENT_HEADER_BEGIN
:
1717 status
= read_event_header_begin_state(notit
);
1719 case STATE_DSCOPE_EVENT_HEADER_CONTINUE
:
1720 status
= read_event_header_continue_state(notit
);
1722 case STATE_AFTER_EVENT_HEADER
:
1723 status
= after_event_header_state(notit
);
1725 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
:
1726 status
= read_event_common_context_begin_state(notit
);
1728 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
:
1729 status
= read_event_common_context_continue_state(notit
);
1731 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
:
1732 status
= read_event_spec_context_begin_state(notit
);
1734 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
:
1735 status
= read_event_spec_context_continue_state(notit
);
1737 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
1738 status
= read_event_payload_begin_state(notit
);
1740 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
1741 status
= read_event_payload_continue_state(notit
);
1743 case STATE_EMIT_MSG_EVENT
:
1744 notit
->state
= STATE_DSCOPE_EVENT_HEADER_BEGIN
;
1746 case STATE_EMIT_QUEUED_MSG_EVENT
:
1747 notit
->state
= STATE_EMIT_MSG_EVENT
;
1749 case STATE_SKIP_PACKET_PADDING
:
1750 status
= skip_packet_padding_state(notit
);
1752 case STATE_EMIT_MSG_PACKET_END_MULTI
:
1753 notit
->state
= STATE_SKIP_PACKET_PADDING
;
1755 case STATE_EMIT_MSG_PACKET_END_SINGLE
:
1756 notit
->state
= STATE_CHECK_EMIT_MSG_STREAM_END
;
1758 case STATE_CHECK_EMIT_MSG_STREAM_END
:
1759 status
= check_emit_msg_stream_end(notit
);
1761 case STATE_EMIT_QUEUED_MSG_PACKET_END
:
1762 notit
->state
= STATE_EMIT_MSG_PACKET_END_SINGLE
;
1764 case STATE_EMIT_MSG_STREAM_END
:
1765 notit
->state
= STATE_DONE
;
1770 BT_COMP_LOGF("Unknown CTF plugin message iterator state: "
1771 "notit-addr=%p, state=%d", notit
, notit
->state
);
1775 BT_COMP_LOGT("Handled state: notit-addr=%p, status=%s, "
1776 "prev-state=%s, cur-state=%s",
1777 notit
, bt_msg_iter_status_string(status
),
1778 state_string(state
), state_string(notit
->state
));
1783 void bt_msg_iter_reset_for_next_stream_file(struct bt_msg_iter
*notit
)
1786 BT_COMP_LOGD("Resetting message iterator: addr=%p", notit
);
1787 stack_clear(notit
->stack
);
1788 notit
->meta
.sc
= NULL
;
1789 notit
->meta
.ec
= NULL
;
1790 BT_PACKET_PUT_REF_AND_RESET(notit
->packet
);
1791 BT_STREAM_PUT_REF_AND_RESET(notit
->stream
);
1792 BT_MESSAGE_PUT_REF_AND_RESET(notit
->event_msg
);
1793 release_all_dscopes(notit
);
1794 notit
->cur_dscope_field
= NULL
;
1796 if (notit
->packet_context_field
) {
1797 bt_packet_context_field_release(notit
->packet_context_field
);
1798 notit
->packet_context_field
= NULL
;
1801 notit
->buf
.addr
= NULL
;
1804 notit
->buf
.last_eh_at
= SIZE_MAX
;
1805 notit
->buf
.packet_offset
= 0;
1806 notit
->state
= STATE_INIT
;
1807 notit
->cur_exp_packet_content_size
= -1;
1808 notit
->cur_exp_packet_total_size
= -1;
1809 notit
->cur_packet_offset
= -1;
1810 notit
->cur_event_class_id
= -1;
1811 notit
->snapshots
.beginning_clock
= UINT64_C(-1);
1812 notit
->snapshots
.end_clock
= UINT64_C(-1);
1816 * Resets the internal state of a CTF message iterator.
1819 void bt_msg_iter_reset(struct bt_msg_iter
*notit
)
1821 bt_msg_iter_reset_for_next_stream_file(notit
);
1822 notit
->cur_stream_class_id
= -1;
1823 notit
->cur_data_stream_id
= -1;
1824 notit
->snapshots
.discarded_events
= UINT64_C(-1);
1825 notit
->snapshots
.packets
= UINT64_C(-1);
1826 notit
->prev_packet_snapshots
.discarded_events
= UINT64_C(-1);
1827 notit
->prev_packet_snapshots
.packets
= UINT64_C(-1);
1828 notit
->prev_packet_snapshots
.beginning_clock
= UINT64_C(-1);
1829 notit
->prev_packet_snapshots
.end_clock
= UINT64_C(-1);
1833 bt_field
*borrow_next_field(struct bt_msg_iter
*notit
)
1835 bt_field
*next_field
= NULL
;
1836 bt_field
*base_field
;
1837 const bt_field_class
*base_fc
;
1838 bt_field_class_type base_fc_type
;
1841 BT_ASSERT_DBG(!stack_empty(notit
->stack
));
1842 index
= stack_top(notit
->stack
)->index
;
1843 base_field
= stack_top(notit
->stack
)->base
;
1844 BT_ASSERT_DBG(base_field
);
1845 base_fc
= bt_field_borrow_class_const(base_field
);
1846 BT_ASSERT_DBG(base_fc
);
1847 base_fc_type
= bt_field_class_get_type(base_fc
);
1849 if (base_fc_type
== BT_FIELD_CLASS_TYPE_STRUCTURE
) {
1850 BT_ASSERT_DBG(index
<
1851 bt_field_class_structure_get_member_count(
1852 bt_field_borrow_class_const(
1855 bt_field_structure_borrow_member_field_by_index(
1857 } else if (bt_field_class_type_is(base_fc_type
,
1858 BT_FIELD_CLASS_TYPE_ARRAY
)) {
1859 BT_ASSERT_DBG(index
< bt_field_array_get_length(base_field
));
1860 next_field
= bt_field_array_borrow_element_field_by_index(
1862 } else if (bt_field_class_type_is(base_fc_type
,
1863 BT_FIELD_CLASS_TYPE_VARIANT
)) {
1864 BT_ASSERT_DBG(index
== 0);
1865 next_field
= bt_field_variant_borrow_selected_option_field(
1871 BT_ASSERT_DBG(next_field
);
1876 void update_default_clock(struct bt_msg_iter
*notit
, uint64_t new_val
,
1877 uint64_t new_val_size
)
1879 uint64_t new_val_mask
;
1880 uint64_t cur_value_masked
;
1882 BT_ASSERT_DBG(new_val_size
> 0);
1885 * Special case for a 64-bit new value, which is the limit
1886 * of a clock value as of this version: overwrite the
1887 * current value directly.
1889 if (new_val_size
== 64) {
1890 notit
->default_clock_snapshot
= new_val
;
1894 new_val_mask
= (1ULL << new_val_size
) - 1;
1895 cur_value_masked
= notit
->default_clock_snapshot
& new_val_mask
;
1897 if (new_val
< cur_value_masked
) {
1899 * It looks like a wrap happened on the number of bits
1900 * of the requested new value. Assume that the clock
1901 * value wrapped only one time.
1903 notit
->default_clock_snapshot
+= new_val_mask
+ 1;
1906 /* Clear the low bits of the current clock value. */
1907 notit
->default_clock_snapshot
&= ~new_val_mask
;
1909 /* Set the low bits of the current clock value. */
1910 notit
->default_clock_snapshot
|= new_val
;
1913 BT_COMP_LOGT("Updated default clock's value from integer field's value: "
1914 "value=%" PRIu64
, notit
->default_clock_snapshot
);
1918 enum bt_bfcr_status
bfcr_unsigned_int_cb(uint64_t value
,
1919 struct ctf_field_class
*fc
, void *data
)
1921 struct bt_msg_iter
*notit
= data
;
1922 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
1923 bt_field
*field
= NULL
;
1924 struct ctf_field_class_int
*int_fc
= (void *) fc
;
1926 BT_COMP_LOGT("Unsigned integer function called from BFCR: "
1927 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1928 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64
,
1929 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
1931 if (G_LIKELY(int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
)) {
1932 goto update_def_clock
;
1935 switch (int_fc
->meaning
) {
1936 case CTF_FIELD_CLASS_MEANING_EVENT_CLASS_ID
:
1937 notit
->cur_event_class_id
= value
;
1939 case CTF_FIELD_CLASS_MEANING_DATA_STREAM_ID
:
1940 notit
->cur_data_stream_id
= value
;
1942 case CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME
:
1943 notit
->snapshots
.beginning_clock
= value
;
1945 case CTF_FIELD_CLASS_MEANING_PACKET_END_TIME
:
1946 notit
->snapshots
.end_clock
= value
;
1948 case CTF_FIELD_CLASS_MEANING_STREAM_CLASS_ID
:
1949 notit
->cur_stream_class_id
= value
;
1951 case CTF_FIELD_CLASS_MEANING_MAGIC
:
1952 if (value
!= 0xc1fc1fc1) {
1953 BT_COMP_LOGW("Invalid CTF magic number: notit-addr=%p, "
1954 "magic=%" PRIx64
, notit
, value
);
1955 status
= BT_BFCR_STATUS_ERROR
;
1960 case CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT
:
1961 notit
->snapshots
.packets
= value
;
1963 case CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT
:
1964 notit
->snapshots
.discarded_events
= value
;
1966 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_TOTAL_SIZE
:
1967 notit
->cur_exp_packet_total_size
= value
;
1969 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_CONTENT_SIZE
:
1970 notit
->cur_exp_packet_content_size
= value
;
1977 if (G_UNLIKELY(int_fc
->mapped_clock_class
)) {
1978 update_default_clock(notit
, value
, int_fc
->base
.size
);
1981 if (G_UNLIKELY(int_fc
->storing_index
>= 0)) {
1982 g_array_index(notit
->stored_values
, uint64_t,
1983 (uint64_t) int_fc
->storing_index
) = value
;
1986 if (G_UNLIKELY(!fc
->in_ir
|| notit
->dry_run
)) {
1990 field
= borrow_next_field(notit
);
1991 BT_ASSERT_DBG(field
);
1992 BT_ASSERT_DBG(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
1993 BT_ASSERT_DBG(bt_field_class_type_is(bt_field_get_class_type(field
),
1994 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
));
1995 bt_field_integer_unsigned_set_value(field
, value
);
1996 stack_top(notit
->stack
)->index
++;
2003 enum bt_bfcr_status
bfcr_unsigned_int_char_cb(uint64_t value
,
2004 struct ctf_field_class
*fc
, void *data
)
2007 struct bt_msg_iter
*notit
= data
;
2008 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
2009 bt_field
*string_field
= NULL
;
2010 struct ctf_field_class_int
*int_fc
= (void *) fc
;
2011 char str
[2] = {'\0', '\0'};
2013 BT_COMP_LOGT("Unsigned integer character function called from BFCR: "
2014 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2015 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64
,
2016 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
2017 BT_ASSERT_DBG(int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
);
2018 BT_ASSERT_DBG(!int_fc
->mapped_clock_class
);
2019 BT_ASSERT_DBG(int_fc
->storing_index
< 0);
2021 if (G_UNLIKELY(!fc
->in_ir
|| notit
->dry_run
)) {
2025 if (notit
->done_filling_string
) {
2030 notit
->done_filling_string
= true;
2034 string_field
= stack_top(notit
->stack
)->base
;
2035 BT_ASSERT_DBG(bt_field_get_class_type(string_field
) ==
2036 BT_FIELD_CLASS_TYPE_STRING
);
2038 /* Append character */
2039 str
[0] = (char) value
;
2040 ret
= bt_field_string_append_with_length(string_field
, str
, 1);
2042 BT_COMP_LOGE("Cannot append character to string field's value: "
2043 "notit-addr=%p, field-addr=%p, ret=%d",
2044 notit
, string_field
, ret
);
2045 status
= BT_BFCR_STATUS_ERROR
;
2054 enum bt_bfcr_status
bfcr_signed_int_cb(int64_t value
,
2055 struct ctf_field_class
*fc
, void *data
)
2057 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
2058 bt_field
*field
= NULL
;
2059 struct bt_msg_iter
*notit
= data
;
2060 struct ctf_field_class_int
*int_fc
= (void *) fc
;
2062 BT_COMP_LOGT("Signed integer function called from BFCR: "
2063 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2064 "fc-type=%d, fc-in-ir=%d, value=%" PRId64
,
2065 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
2066 BT_ASSERT_DBG(int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
);
2068 if (G_UNLIKELY(int_fc
->storing_index
>= 0)) {
2069 g_array_index(notit
->stored_values
, uint64_t,
2070 (uint64_t) int_fc
->storing_index
) = (uint64_t) value
;
2073 if (G_UNLIKELY(!fc
->in_ir
|| notit
->dry_run
)) {
2077 field
= borrow_next_field(notit
);
2078 BT_ASSERT_DBG(field
);
2079 BT_ASSERT_DBG(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
2080 BT_ASSERT_DBG(bt_field_class_type_is(bt_field_get_class_type(field
),
2081 BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
));
2082 bt_field_integer_signed_set_value(field
, value
);
2083 stack_top(notit
->stack
)->index
++;
2090 enum bt_bfcr_status
bfcr_floating_point_cb(double value
,
2091 struct ctf_field_class
*fc
, void *data
)
2093 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
2094 bt_field
*field
= NULL
;
2095 struct bt_msg_iter
*notit
= data
;
2096 bt_field_class_type type
;
2098 BT_COMP_LOGT("Floating point number function called from BFCR: "
2099 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2100 "fc-type=%d, fc-in-ir=%d, value=%f",
2101 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
2103 if (G_UNLIKELY(!fc
->in_ir
|| notit
->dry_run
)) {
2107 field
= borrow_next_field(notit
);
2108 type
= bt_field_get_class_type(field
);
2109 BT_ASSERT_DBG(field
);
2110 BT_ASSERT_DBG(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
2111 BT_ASSERT_DBG(bt_field_class_type_is(type
, BT_FIELD_CLASS_TYPE_REAL
));
2113 if (type
== BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL
) {
2114 bt_field_real_single_precision_set_value(field
, (float) value
);
2116 bt_field_real_double_precision_set_value(field
, value
);
2118 stack_top(notit
->stack
)->index
++;
2125 enum bt_bfcr_status
bfcr_string_begin_cb(
2126 struct ctf_field_class
*fc
, void *data
)
2128 bt_field
*field
= NULL
;
2129 struct bt_msg_iter
*notit
= data
;
2131 BT_COMP_LOGT("String (beginning) function called from BFCR: "
2132 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2133 "fc-type=%d, fc-in-ir=%d",
2134 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
2136 if (G_UNLIKELY(!fc
->in_ir
|| notit
->dry_run
)) {
2140 field
= borrow_next_field(notit
);
2141 BT_ASSERT_DBG(field
);
2142 BT_ASSERT_DBG(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
2143 BT_ASSERT_DBG(bt_field_get_class_type(field
) ==
2144 BT_FIELD_CLASS_TYPE_STRING
);
2145 bt_field_string_clear(field
);
2148 * Push on stack. Not a compound class per se, but we know that
2149 * only bfcr_string_cb() may be called between this call and a
2150 * subsequent call to bfcr_string_end_cb().
2152 stack_push(notit
->stack
, field
);
2155 return BT_BFCR_STATUS_OK
;
2159 enum bt_bfcr_status
bfcr_string_cb(const char *value
,
2160 size_t len
, struct ctf_field_class
*fc
, void *data
)
2162 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
2163 bt_field
*field
= NULL
;
2164 struct bt_msg_iter
*notit
= data
;
2167 BT_COMP_LOGT("String (substring) function called from BFCR: "
2168 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2169 "fc-type=%d, fc-in-ir=%d, string-length=%zu",
2170 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
,
2173 if (G_UNLIKELY(!fc
->in_ir
|| notit
->dry_run
)) {
2177 field
= stack_top(notit
->stack
)->base
;
2178 BT_ASSERT_DBG(field
);
2180 /* Append current substring */
2181 ret
= bt_field_string_append_with_length(field
, value
, len
);
2183 BT_COMP_LOGE("Cannot append substring to string field's value: "
2184 "notit-addr=%p, field-addr=%p, string-length=%zu, "
2185 "ret=%d", notit
, field
, len
, ret
);
2186 status
= BT_BFCR_STATUS_ERROR
;
2195 enum bt_bfcr_status
bfcr_string_end_cb(
2196 struct ctf_field_class
*fc
, void *data
)
2198 struct bt_msg_iter
*notit
= data
;
2200 BT_COMP_LOGT("String (end) function called from BFCR: "
2201 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2202 "fc-type=%d, fc-in-ir=%d",
2203 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
2205 if (G_UNLIKELY(!fc
->in_ir
|| notit
->dry_run
)) {
2209 /* Pop string field */
2210 stack_pop(notit
->stack
);
2212 /* Go to next field */
2213 stack_top(notit
->stack
)->index
++;
2216 return BT_BFCR_STATUS_OK
;
2219 enum bt_bfcr_status
bfcr_compound_begin_cb(
2220 struct ctf_field_class
*fc
, void *data
)
2222 struct bt_msg_iter
*notit
= data
;
2225 BT_COMP_LOGT("Compound (beginning) function called from BFCR: "
2226 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2227 "fc-type=%d, fc-in-ir=%d",
2228 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
2230 if (G_UNLIKELY(!fc
->in_ir
|| notit
->dry_run
)) {
2235 if (stack_empty(notit
->stack
)) {
2236 /* Root: already set by read_dscope_begin_state() */
2237 field
= notit
->cur_dscope_field
;
2239 field
= borrow_next_field(notit
);
2240 BT_ASSERT_DBG(field
);
2244 BT_ASSERT_DBG(field
);
2245 BT_ASSERT_DBG(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
2246 stack_push(notit
->stack
, field
);
2249 * Change BFCR "unsigned int" callback if it's a text
2252 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
||
2253 fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
2254 struct ctf_field_class_array_base
*array_fc
= (void *) fc
;
2256 if (array_fc
->is_text
) {
2257 BT_ASSERT_DBG(bt_field_get_class_type(field
) ==
2258 BT_FIELD_CLASS_TYPE_STRING
);
2259 notit
->done_filling_string
= false;
2260 bt_field_string_clear(field
);
2261 bt_bfcr_set_unsigned_int_cb(notit
->bfcr
,
2262 bfcr_unsigned_int_char_cb
);
2267 return BT_BFCR_STATUS_OK
;
2270 enum bt_bfcr_status
bfcr_compound_end_cb(
2271 struct ctf_field_class
*fc
, void *data
)
2273 struct bt_msg_iter
*notit
= data
;
2275 BT_COMP_LOGT("Compound (end) function called from BFCR: "
2276 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2277 "fc-type=%d, fc-in-ir=%d",
2278 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
2280 if (G_UNLIKELY(!fc
->in_ir
|| notit
->dry_run
)) {
2284 BT_ASSERT_DBG(!stack_empty(notit
->stack
));
2285 BT_ASSERT_DBG(bt_field_borrow_class_const(stack_top(notit
->stack
)->base
) ==
2289 * Reset BFCR "unsigned int" callback if it's a text
2292 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
||
2293 fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
2294 struct ctf_field_class_array_base
*array_fc
= (void *) fc
;
2296 if (array_fc
->is_text
) {
2297 BT_ASSERT_DBG(bt_field_get_class_type(
2298 stack_top(notit
->stack
)->base
) ==
2299 BT_FIELD_CLASS_TYPE_STRING
);
2300 bt_bfcr_set_unsigned_int_cb(notit
->bfcr
,
2301 bfcr_unsigned_int_cb
);
2306 stack_pop(notit
->stack
);
2308 /* If the stack is not empty, increment the base's index */
2309 if (!stack_empty(notit
->stack
)) {
2310 stack_top(notit
->stack
)->index
++;
2314 return BT_BFCR_STATUS_OK
;
2318 int64_t bfcr_get_sequence_length_cb(struct ctf_field_class
*fc
, void *data
)
2320 bt_field
*seq_field
;
2321 struct bt_msg_iter
*notit
= data
;
2322 struct ctf_field_class_sequence
*seq_fc
= (void *) fc
;
2323 int64_t length
= -1;
2326 length
= (uint64_t) g_array_index(notit
->stored_values
, uint64_t,
2327 seq_fc
->stored_length_index
);
2329 if (G_UNLIKELY(notit
->dry_run
)){
2333 seq_field
= stack_top(notit
->stack
)->base
;
2334 BT_ASSERT_DBG(seq_field
);
2337 * bfcr_get_sequence_length_cb() also gets called back for a
2338 * text sequence, but the destination field is a string field.
2339 * Only set the field's sequence length if the destination field
2340 * is a sequence field.
2342 if (!seq_fc
->base
.is_text
) {
2343 BT_ASSERT_DBG(bt_field_class_type_is(
2344 bt_field_get_class_type(seq_field
),
2345 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
));
2346 ret
= bt_field_array_dynamic_set_length(seq_field
,
2349 BT_COMP_LOGE("Cannot set dynamic array field's length field: "
2350 "notit-addr=%p, field-addr=%p, "
2351 "length=%" PRIu64
, notit
, seq_field
, length
);
2360 struct ctf_field_class
*bfcr_borrow_variant_selected_field_class_cb(
2361 struct ctf_field_class
*fc
, void *data
)
2365 int64_t option_index
= -1;
2366 struct bt_msg_iter
*notit
= data
;
2367 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
2368 struct ctf_named_field_class
*selected_option
= NULL
;
2369 struct ctf_field_class
*ret_fc
= NULL
;
2375 /* Get variant's tag */
2376 tag
.u
= g_array_index(notit
->stored_values
, uint64_t,
2377 var_fc
->stored_tag_index
);
2380 * Check each range to find the selected option's index.
2382 if (var_fc
->tag_fc
->base
.is_signed
) {
2383 for (i
= 0; i
< var_fc
->ranges
->len
; i
++) {
2384 struct ctf_field_class_variant_range
*range
=
2385 ctf_field_class_variant_borrow_range_by_index(
2388 if (tag
.i
>= range
->range
.lower
.i
&&
2389 tag
.i
<= range
->range
.upper
.i
) {
2390 option_index
= (int64_t) range
->option_index
;
2395 for (i
= 0; i
< var_fc
->ranges
->len
; i
++) {
2396 struct ctf_field_class_variant_range
*range
=
2397 ctf_field_class_variant_borrow_range_by_index(
2400 if (tag
.u
>= range
->range
.lower
.u
&&
2401 tag
.u
<= range
->range
.upper
.u
) {
2402 option_index
= (int64_t) range
->option_index
;
2408 if (option_index
< 0) {
2409 BT_COMP_LOGW("Cannot find variant field class's option: "
2410 "notit-addr=%p, var-fc-addr=%p, u-tag=%" PRIu64
", "
2411 "i-tag=%" PRId64
, notit
, var_fc
, tag
.u
, tag
.i
);
2415 selected_option
= ctf_field_class_variant_borrow_option_by_index(
2416 var_fc
, (uint64_t) option_index
);
2418 if (selected_option
->fc
->in_ir
&& !notit
->dry_run
) {
2419 bt_field
*var_field
= stack_top(notit
->stack
)->base
;
2421 ret
= bt_field_variant_select_option_field_by_index(
2422 var_field
, option_index
);
2424 BT_COMP_LOGW("Cannot select variant field's option field: "
2425 "notit-addr=%p, var-field-addr=%p, "
2426 "opt-index=%" PRId64
, notit
, var_field
,
2432 ret_fc
= selected_option
->fc
;
2439 void create_msg_stream_beginning(struct bt_msg_iter
*notit
,
2440 bt_message
**message
)
2442 bt_message
*ret
= NULL
;
2444 BT_ASSERT(notit
->stream
);
2445 BT_ASSERT(notit
->msg_iter
);
2446 ret
= bt_message_stream_beginning_create(notit
->msg_iter
,
2449 BT_COMP_LOGE("Cannot create stream beginning message: "
2450 "notit-addr=%p, stream-addr=%p",
2451 notit
, notit
->stream
);
2459 void create_msg_stream_end(struct bt_msg_iter
*notit
, bt_message
**message
)
2463 if (!notit
->stream
) {
2464 BT_COMP_LOGE("Cannot create stream for stream message: "
2465 "notit-addr=%p", notit
);
2469 BT_ASSERT(notit
->msg_iter
);
2470 ret
= bt_message_stream_end_create(notit
->msg_iter
,
2473 BT_COMP_LOGE("Cannot create stream end message: "
2474 "notit-addr=%p, stream-addr=%p",
2475 notit
, notit
->stream
);
2483 void create_msg_packet_beginning(struct bt_msg_iter
*notit
,
2484 bt_message
**message
, bool use_default_cs
)
2487 bt_message
*msg
= NULL
;
2488 const bt_stream_class
*sc
= notit
->meta
.sc
->ir_sc
;
2490 BT_ASSERT(notit
->packet
);
2493 if (notit
->packet_context_field
) {
2494 ret
= bt_packet_move_context_field(
2495 notit
->packet
, notit
->packet_context_field
);
2500 notit
->packet_context_field
= NULL
;
2503 * At this point notit->dscopes.stream_packet_context
2504 * has the same value as the packet context field within
2507 BT_ASSERT(bt_packet_borrow_context_field(
2509 notit
->dscopes
.stream_packet_context
);
2512 BT_ASSERT(notit
->msg_iter
);
2514 if (notit
->meta
.sc
->packets_have_ts_begin
) {
2515 BT_ASSERT(notit
->snapshots
.beginning_clock
!= UINT64_C(-1));
2516 uint64_t raw_cs_value
;
2519 * Either use the decoded packet `timestamp_begin` field or the
2520 * current stream's default clock_snapshot.
2522 if (use_default_cs
) {
2523 raw_cs_value
= notit
->default_clock_snapshot
;
2525 raw_cs_value
= notit
->snapshots
.beginning_clock
;
2528 msg
= bt_message_packet_beginning_create_with_default_clock_snapshot(
2529 notit
->msg_iter
, notit
->packet
,
2532 msg
= bt_message_packet_beginning_create(notit
->msg_iter
,
2537 BT_COMP_LOGE("Cannot create packet beginning message: "
2538 "notit-addr=%p, packet-addr=%p",
2539 notit
, notit
->packet
);
2550 void emit_delayed_packet_beg_msg(struct bt_msg_iter
*notit
,
2551 bt_message
**message
)
2553 bool packet_beg_ts_need_fix_up
;
2555 notit
->emit_delayed_packet_beginning_msg
= false;
2558 * Only fix the packet's timestamp_begin if it's larger than the first
2559 * event of the packet. If there was no event in the packet, the
2560 * `default_clock_snapshot` field will be either equal or greater than
2561 * `snapshots.beginning_clock` so there is not fix needed.
2563 packet_beg_ts_need_fix_up
=
2564 notit
->default_clock_snapshot
< notit
->snapshots
.beginning_clock
;
2566 /* create_msg_packet_beginning() logs errors */
2567 create_msg_packet_beginning(notit
, message
, packet_beg_ts_need_fix_up
);
2574 void create_msg_packet_end(struct bt_msg_iter
*notit
, bt_message
**message
)
2577 bool update_default_cs
= true;
2579 if (!notit
->packet
) {
2584 * Check if we need to emit the delayed packet
2585 * beginning message instead of the packet end message.
2587 if (G_UNLIKELY(notit
->emit_delayed_packet_beginning_msg
)) {
2588 emit_delayed_packet_beg_msg(notit
, message
);
2589 /* Don't forget to emit the packet end message. */
2590 notit
->state
= STATE_EMIT_QUEUED_MSG_PACKET_END
;
2594 /* Check if may be affected by lttng-crash timestamp_end quirk. */
2595 if (G_UNLIKELY(notit
->meta
.tc
->quirks
.lttng_crash
)) {
2597 * Check if the `timestamp_begin` field is non-zero but
2598 * `timestamp_end` is zero. It means the trace is affected by
2599 * the lttng-crash packet `timestamp_end` quirk and must be
2600 * fixed up by omitting to update the default clock snapshot to
2601 * the `timestamp_end` as is typically done.
2603 if (notit
->snapshots
.beginning_clock
!= 0 &&
2604 notit
->snapshots
.end_clock
== 0) {
2605 update_default_cs
= false;
2610 * Check if may be affected by lttng event-after-packet `timestamp_end`
2613 if (notit
->meta
.tc
->quirks
.lttng_event_after_packet
) {
2615 * Check if `timestamp_end` is smaller then the current
2616 * default_clock_snapshot (which is set to the last event
2617 * decoded). It means the trace is affected by the lttng
2618 * `event-after-packet` packet `timestamp_end` quirk and must
2619 * be fixed up by omitting to update the default clock snapshot
2620 * to the `timestamp_end` as is typically done.
2622 if (notit
->snapshots
.end_clock
< notit
->default_clock_snapshot
) {
2623 update_default_cs
= false;
2627 /* Update default clock from packet's end time. */
2628 if (notit
->snapshots
.end_clock
!= UINT64_C(-1) && update_default_cs
) {
2629 notit
->default_clock_snapshot
= notit
->snapshots
.end_clock
;
2632 BT_ASSERT(notit
->msg_iter
);
2634 if (notit
->meta
.sc
->packets_have_ts_end
) {
2635 BT_ASSERT(notit
->snapshots
.end_clock
!= UINT64_C(-1));
2636 msg
= bt_message_packet_end_create_with_default_clock_snapshot(
2637 notit
->msg_iter
, notit
->packet
,
2638 notit
->default_clock_snapshot
);
2640 msg
= bt_message_packet_end_create(notit
->msg_iter
,
2645 BT_COMP_LOGE("Cannot create packet end message: "
2646 "notit-addr=%p, packet-addr=%p",
2647 notit
, notit
->packet
);
2652 BT_PACKET_PUT_REF_AND_RESET(notit
->packet
);
2657 void create_msg_discarded_events(struct bt_msg_iter
*notit
,
2658 bt_message
**message
)
2661 uint64_t beginning_raw_value
= UINT64_C(-1);
2662 uint64_t end_raw_value
= UINT64_C(-1);
2664 BT_ASSERT(notit
->msg_iter
);
2665 BT_ASSERT(notit
->stream
);
2666 BT_ASSERT(notit
->meta
.sc
->has_discarded_events
);
2668 if (notit
->meta
.sc
->discarded_events_have_default_cs
) {
2669 if (notit
->prev_packet_snapshots
.discarded_events
== UINT64_C(-1)) {
2671 * We discarded events, but before (and possibly
2672 * including) the current packet: use this packet's time
2673 * range, and do not have a specific count.
2675 beginning_raw_value
= notit
->snapshots
.beginning_clock
;
2676 end_raw_value
= notit
->snapshots
.end_clock
;
2678 beginning_raw_value
= notit
->prev_packet_snapshots
.end_clock
;
2679 end_raw_value
= notit
->snapshots
.end_clock
;
2682 BT_ASSERT(beginning_raw_value
!= UINT64_C(-1));
2683 BT_ASSERT(end_raw_value
!= UINT64_C(-1));
2684 msg
= bt_message_discarded_events_create_with_default_clock_snapshots(
2685 notit
->msg_iter
, notit
->stream
, beginning_raw_value
,
2688 msg
= bt_message_discarded_events_create(notit
->msg_iter
,
2693 BT_COMP_LOGE("Cannot create discarded events message: "
2694 "notit-addr=%p, stream-addr=%p",
2695 notit
, notit
->stream
);
2699 if (notit
->prev_packet_snapshots
.discarded_events
!= UINT64_C(-1)) {
2700 bt_message_discarded_events_set_count(msg
,
2701 notit
->snapshots
.discarded_events
-
2702 notit
->prev_packet_snapshots
.discarded_events
);
2709 void create_msg_discarded_packets(struct bt_msg_iter
*notit
,
2710 bt_message
**message
)
2714 BT_ASSERT(notit
->msg_iter
);
2715 BT_ASSERT(notit
->stream
);
2716 BT_ASSERT(notit
->meta
.sc
->has_discarded_packets
);
2717 BT_ASSERT(notit
->prev_packet_snapshots
.packets
!=
2720 if (notit
->meta
.sc
->discarded_packets_have_default_cs
) {
2721 BT_ASSERT(notit
->prev_packet_snapshots
.end_clock
!= UINT64_C(-1));
2722 BT_ASSERT(notit
->snapshots
.beginning_clock
!= UINT64_C(-1));
2723 msg
= bt_message_discarded_packets_create_with_default_clock_snapshots(
2724 notit
->msg_iter
, notit
->stream
,
2725 notit
->prev_packet_snapshots
.end_clock
,
2726 notit
->snapshots
.beginning_clock
);
2728 msg
= bt_message_discarded_packets_create(notit
->msg_iter
,
2733 BT_COMP_LOGE("Cannot create discarded packets message: "
2734 "notit-addr=%p, stream-addr=%p",
2735 notit
, notit
->stream
);
2739 bt_message_discarded_packets_set_count(msg
,
2740 notit
->snapshots
.packets
-
2741 notit
->prev_packet_snapshots
.packets
- 1);
2746 struct bt_msg_iter
*bt_msg_iter_create(struct ctf_trace_class
*tc
,
2747 size_t max_request_sz
,
2748 struct bt_msg_iter_medium_ops medops
, void *data
,
2749 bt_logging_level log_level
, bt_self_component
*self_comp
)
2751 struct bt_msg_iter
*notit
= NULL
;
2752 struct bt_bfcr_cbs cbs
= {
2754 .signed_int
= bfcr_signed_int_cb
,
2755 .unsigned_int
= bfcr_unsigned_int_cb
,
2756 .floating_point
= bfcr_floating_point_cb
,
2757 .string_begin
= bfcr_string_begin_cb
,
2758 .string
= bfcr_string_cb
,
2759 .string_end
= bfcr_string_end_cb
,
2760 .compound_begin
= bfcr_compound_begin_cb
,
2761 .compound_end
= bfcr_compound_end_cb
,
2764 .get_sequence_length
= bfcr_get_sequence_length_cb
,
2765 .borrow_variant_selected_field_class
= bfcr_borrow_variant_selected_field_class_cb
,
2770 BT_ASSERT(medops
.request_bytes
);
2771 BT_ASSERT(medops
.borrow_stream
);
2772 BT_COMP_LOG_CUR_LVL(BT_LOG_DEBUG
, log_level
, self_comp
,
2773 "Creating CTF plugin message iterator: "
2774 "trace-addr=%p, max-request-size=%zu, "
2775 "data=%p, log-level=%s", tc
, max_request_sz
, data
,
2776 bt_common_logging_level_string(log_level
));
2777 notit
= g_new0(struct bt_msg_iter
, 1);
2779 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_comp
,
2780 "Failed to allocate one CTF plugin message iterator.");
2783 notit
->self_comp
= self_comp
;
2784 notit
->log_level
= log_level
;
2785 notit
->meta
.tc
= tc
;
2786 notit
->medium
.medops
= medops
;
2787 notit
->medium
.max_request_sz
= max_request_sz
;
2788 notit
->medium
.data
= data
;
2789 notit
->stack
= stack_new(notit
);
2790 notit
->stored_values
= g_array_new(FALSE
, TRUE
, sizeof(uint64_t));
2791 g_array_set_size(notit
->stored_values
, tc
->stored_value_count
);
2793 if (!notit
->stack
) {
2794 BT_COMP_LOGE_STR("Failed to create field stack.");
2798 notit
->bfcr
= bt_bfcr_create(cbs
, notit
, log_level
, NULL
);
2800 BT_COMP_LOGE_STR("Failed to create binary class reader (BFCR).");
2804 bt_msg_iter_reset(notit
);
2805 BT_COMP_LOGD("Created CTF plugin message iterator: "
2806 "trace-addr=%p, max-request-size=%zu, "
2807 "data=%p, notit-addr=%p, log-level=%s",
2808 tc
, max_request_sz
, data
, notit
,
2809 bt_common_logging_level_string(log_level
));
2810 notit
->cur_packet_offset
= 0;
2816 bt_msg_iter_destroy(notit
);
2821 void bt_msg_iter_destroy(struct bt_msg_iter
*notit
)
2823 BT_PACKET_PUT_REF_AND_RESET(notit
->packet
);
2824 BT_STREAM_PUT_REF_AND_RESET(notit
->stream
);
2825 release_all_dscopes(notit
);
2827 BT_COMP_LOGD("Destroying CTF plugin message iterator: addr=%p", notit
);
2830 BT_COMP_LOGD_STR("Destroying field stack.");
2831 stack_destroy(notit
->stack
);
2835 BT_COMP_LOGD("Destroying BFCR: bfcr-addr=%p", notit
->bfcr
);
2836 bt_bfcr_destroy(notit
->bfcr
);
2839 if (notit
->stored_values
) {
2840 g_array_free(notit
->stored_values
, TRUE
);
2846 enum bt_msg_iter_status
bt_msg_iter_get_next_message(
2847 struct bt_msg_iter
*notit
,
2848 bt_self_message_iterator
*msg_iter
, bt_message
**message
)
2850 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
2852 BT_ASSERT_DBG(notit
);
2853 BT_ASSERT_DBG(message
);
2854 notit
->msg_iter
= msg_iter
;
2855 notit
->set_stream
= true;
2856 BT_COMP_LOGD("Getting next message: notit-addr=%p", notit
);
2859 status
= handle_state(notit
);
2860 if (G_UNLIKELY(status
== BT_MSG_ITER_STATUS_AGAIN
)) {
2861 BT_COMP_LOGD_STR("Medium returned BT_MSG_ITER_STATUS_AGAIN.");
2863 } else if (G_UNLIKELY(status
!= BT_MSG_ITER_STATUS_OK
)) {
2864 BT_COMP_LOGW("Cannot handle state: notit-addr=%p, state=%s",
2865 notit
, state_string(notit
->state
));
2869 switch (notit
->state
) {
2870 case STATE_EMIT_MSG_EVENT
:
2871 BT_ASSERT_DBG(notit
->event_msg
);
2874 * Check if we need to emit the delayed packet
2875 * beginning message instead of the event message.
2877 if (G_UNLIKELY(notit
->emit_delayed_packet_beginning_msg
)) {
2878 emit_delayed_packet_beg_msg(notit
, message
);
2880 status
= BT_MSG_ITER_STATUS_ERROR
;
2884 * Don't forget to emit the event message of
2885 * the event record that was just decoded.
2887 notit
->state
= STATE_EMIT_QUEUED_MSG_EVENT
;
2890 *message
= notit
->event_msg
;
2891 notit
->event_msg
= NULL
;
2894 case STATE_EMIT_MSG_DISCARDED_EVENTS
:
2895 /* create_msg_discared_events() logs errors */
2896 create_msg_discarded_events(notit
, message
);
2899 status
= BT_MSG_ITER_STATUS_ERROR
;
2903 case STATE_EMIT_MSG_DISCARDED_PACKETS
:
2904 /* create_msg_discared_packets() logs errors */
2905 create_msg_discarded_packets(notit
, message
);
2908 status
= BT_MSG_ITER_STATUS_ERROR
;
2912 case STATE_EMIT_MSG_PACKET_BEGINNING
:
2913 status
= set_current_packet(notit
);
2914 if (status
!= BT_MSG_ITER_STATUS_OK
) {
2918 if (G_UNLIKELY(notit
->meta
.tc
->quirks
.barectf_event_before_packet
)) {
2919 notit
->emit_delayed_packet_beginning_msg
= true;
2921 * There is no message to return yet as this
2922 * packet beginning message is delayed until we
2923 * decode the first event message of the
2928 /* create_msg_packet_beginning() logs errors */
2929 create_msg_packet_beginning(notit
, message
, false);
2931 status
= BT_MSG_ITER_STATUS_ERROR
;
2936 case STATE_EMIT_MSG_PACKET_END_SINGLE
:
2937 case STATE_EMIT_MSG_PACKET_END_MULTI
:
2938 /* create_msg_packet_end() logs errors */
2939 create_msg_packet_end(notit
, message
);
2942 status
= BT_MSG_ITER_STATUS_ERROR
;
2946 case STATE_EMIT_MSG_STREAM_BEGINNING
:
2947 /* create_msg_stream_beginning() logs errors */
2948 create_msg_stream_beginning(notit
, message
);
2951 status
= BT_MSG_ITER_STATUS_ERROR
;
2955 case STATE_EMIT_MSG_STREAM_END
:
2956 /* create_msg_stream_end() logs errors */
2957 create_msg_stream_end(notit
, message
);
2960 status
= BT_MSG_ITER_STATUS_ERROR
;
2965 status
= BT_MSG_ITER_STATUS_EOF
;
2968 /* Non-emitting state: continue */
2978 enum bt_msg_iter_status
decode_until_state( struct bt_msg_iter
*notit
,
2979 enum state target_state_1
, enum state target_state_2
)
2981 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
2983 BT_ASSERT_DBG(notit
);
2984 notit
->set_stream
= false;
2988 * Check if we reached the state at which we want to stop
2991 if (notit
->state
== target_state_1
||
2992 notit
->state
== target_state_2
) {
2996 status
= handle_state(notit
);
2997 if (G_UNLIKELY(status
== BT_MSG_ITER_STATUS_AGAIN
)) {
2998 BT_COMP_LOGD_STR("Medium returned BT_MSG_ITER_STATUS_AGAIN.");
3000 } else if (G_UNLIKELY(status
!= BT_MSG_ITER_STATUS_OK
)) {
3001 BT_COMP_LOGW("Cannot handle state: notit-addr=%p, state=%s",
3002 notit
, state_string(notit
->state
));
3006 switch (notit
->state
) {
3008 case STATE_SWITCH_PACKET
:
3009 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
3010 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
3011 case STATE_AFTER_TRACE_PACKET_HEADER
:
3012 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
3013 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
3014 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
3015 case STATE_CHECK_EMIT_MSG_STREAM_BEGINNING
:
3016 case STATE_EMIT_MSG_STREAM_BEGINNING
:
3017 case STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS
:
3018 case STATE_EMIT_MSG_DISCARDED_EVENTS
:
3019 case STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS
:
3020 case STATE_EMIT_MSG_DISCARDED_PACKETS
:
3021 case STATE_EMIT_MSG_PACKET_BEGINNING
:
3022 case STATE_DSCOPE_EVENT_HEADER_BEGIN
:
3023 case STATE_DSCOPE_EVENT_HEADER_CONTINUE
:
3024 case STATE_AFTER_EVENT_HEADER
:
3025 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
:
3026 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
:
3027 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
:
3028 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
:
3029 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
3030 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
3031 case STATE_EMIT_MSG_EVENT
:
3032 case STATE_EMIT_QUEUED_MSG_EVENT
:
3033 case STATE_SKIP_PACKET_PADDING
:
3034 case STATE_EMIT_MSG_PACKET_END_MULTI
:
3035 case STATE_EMIT_MSG_PACKET_END_SINGLE
:
3036 case STATE_EMIT_QUEUED_MSG_PACKET_END
:
3037 case STATE_CHECK_EMIT_MSG_STREAM_END
:
3038 case STATE_EMIT_MSG_STREAM_END
:
3043 /* We should never get to the STATE_DONE state. */
3044 BT_COMP_LOGF("Unexpected state: notit-addr=%p, state=%s",
3045 notit
, state_string(notit
->state
));
3055 enum bt_msg_iter_status
read_packet_header_context_fields(
3056 struct bt_msg_iter
*notit
)
3059 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
3061 status
= decode_until_state(notit
, STATE_EMIT_MSG_PACKET_BEGINNING
, -1);
3062 if (status
!= BT_MSG_ITER_STATUS_OK
) {
3066 ret
= set_current_packet_content_sizes(notit
);
3068 status
= BT_MSG_ITER_STATUS_ERROR
;
3077 void bt_msg_iter_set_medops_data(struct bt_msg_iter
*notit
,
3081 notit
->medium
.data
= medops_data
;
3085 enum bt_msg_iter_status
bt_msg_iter_seek(struct bt_msg_iter
*notit
,
3088 enum bt_msg_iter_status ret
= BT_MSG_ITER_STATUS_OK
;
3089 enum bt_msg_iter_medium_status medium_status
;
3093 BT_COMP_LOGE("Cannot seek to negative offset: offset=%jd", (intmax_t) offset
);
3094 ret
= BT_MSG_ITER_STATUS_INVAL
;
3098 if (!notit
->medium
.medops
.seek
) {
3099 ret
= BT_MSG_ITER_STATUS_UNSUPPORTED
;
3100 BT_COMP_LOGD("Aborting seek as the iterator's underlying media does not implement seek support.");
3104 medium_status
= notit
->medium
.medops
.seek(
3105 BT_MSG_ITER_SEEK_WHENCE_SET
, offset
, notit
->medium
.data
);
3106 if (medium_status
!= BT_MSG_ITER_MEDIUM_STATUS_OK
) {
3107 if (medium_status
== BT_MSG_ITER_MEDIUM_STATUS_EOF
) {
3108 ret
= BT_MSG_ITER_STATUS_EOF
;
3110 ret
= BT_MSG_ITER_STATUS_ERROR
;
3115 bt_msg_iter_reset(notit
);
3116 notit
->cur_packet_offset
= offset
;
3123 enum bt_msg_iter_status
clock_snapshot_at_msg_iter_state(
3124 struct bt_msg_iter
*notit
, enum state target_state_1
,
3125 enum state target_state_2
, uint64_t *clock_snapshot
)
3127 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
3129 BT_ASSERT_DBG(notit
);
3130 BT_ASSERT_DBG(clock_snapshot
);
3131 status
= decode_until_state(notit
, target_state_1
, target_state_2
);
3132 if (status
!= BT_MSG_ITER_STATUS_OK
) {
3136 *clock_snapshot
= notit
->default_clock_snapshot
;
3142 enum bt_msg_iter_status
bt_msg_iter_curr_packet_first_event_clock_snapshot(
3143 struct bt_msg_iter
*notit
, uint64_t *first_clock_snapshot
)
3145 return clock_snapshot_at_msg_iter_state(notit
,
3146 STATE_AFTER_EVENT_HEADER
, -1, first_clock_snapshot
);
3150 enum bt_msg_iter_status
bt_msg_iter_curr_packet_last_event_clock_snapshot(
3151 struct bt_msg_iter
*notit
, uint64_t *last_clock_snapshot
)
3153 return clock_snapshot_at_msg_iter_state(notit
,
3154 STATE_EMIT_MSG_PACKET_END_SINGLE
,
3155 STATE_EMIT_MSG_PACKET_END_MULTI
, last_clock_snapshot
);
3159 enum bt_msg_iter_status
bt_msg_iter_get_packet_properties(
3160 struct bt_msg_iter
*notit
,
3161 struct bt_msg_iter_packet_properties
*props
)
3163 enum bt_msg_iter_status status
;
3165 BT_ASSERT_DBG(notit
);
3166 BT_ASSERT_DBG(props
);
3167 status
= read_packet_header_context_fields(notit
);
3168 if (status
!= BT_MSG_ITER_STATUS_OK
) {
3172 props
->exp_packet_total_size
= notit
->cur_exp_packet_total_size
;
3173 props
->exp_packet_content_size
= notit
->cur_exp_packet_content_size
;
3174 props
->stream_class_id
= (uint64_t) notit
->cur_stream_class_id
;
3175 props
->data_stream_id
= notit
->cur_data_stream_id
;
3176 props
->snapshots
.discarded_events
= notit
->snapshots
.discarded_events
;
3177 props
->snapshots
.packets
= notit
->snapshots
.packets
;
3178 props
->snapshots
.beginning_clock
= notit
->snapshots
.beginning_clock
;
3179 props
->snapshots
.end_clock
= notit
->snapshots
.end_clock
;
3186 void bt_msg_iter_set_emit_stream_beginning_message(struct bt_msg_iter
*notit
,
3189 notit
->emit_stream_begin_msg
= val
;
3193 void bt_msg_iter_set_emit_stream_end_message(struct bt_msg_iter
*notit
,
3196 notit
->emit_stream_end_msg
= val
;
3200 void bt_msg_iter_set_dry_run(struct bt_msg_iter
*notit
,
3203 notit
->dry_run
= val
;