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_LOG_TAG "PLUGIN-CTF-MSG-ITER"
34 #include <babeltrace/assert-internal.h>
36 #include <babeltrace/babeltrace.h>
37 #include <babeltrace/common-internal.h>
42 #include "../bfcr/bfcr.h"
46 /* A visit stack entry */
49 * Current base field, one of:
61 /* Index of next field to set */
67 /* Entries (struct stack_entry) */
70 /* Number of active entries */
77 STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
,
78 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
79 STATE_AFTER_TRACE_PACKET_HEADER
,
80 STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
,
81 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
82 STATE_AFTER_STREAM_PACKET_CONTEXT
,
83 STATE_EMIT_MSG_NEW_STREAM
,
84 STATE_EMIT_MSG_NEW_PACKET
,
85 STATE_DSCOPE_EVENT_HEADER_BEGIN
,
86 STATE_DSCOPE_EVENT_HEADER_CONTINUE
,
87 STATE_AFTER_EVENT_HEADER
,
88 STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
,
89 STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
,
90 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
,
91 STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
,
92 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
93 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
95 STATE_EMIT_MSG_END_OF_PACKET
,
97 STATE_SKIP_PACKET_PADDING
,
100 /* CTF message iterator */
105 /* Current message iterator to create messages (weak) */
106 bt_self_message_iterator
*msg_iter
;
109 * Current dynamic scope field pointer.
111 * This is set by read_dscope_begin_state() and contains the
112 * value of one of the pointers in `dscopes` below.
114 bt_field
*cur_dscope_field
;
117 * True if we're done filling a string field from a text
118 * array/sequence payload.
120 bool done_filling_string
;
122 /* Trace and classes */
123 /* True to set IR fields */
127 struct ctf_trace_class
*tc
;
128 struct ctf_stream_class
*sc
;
129 struct ctf_event_class
*ec
;
132 /* Current packet context field wrapper (NULL if not created yet) */
133 bt_packet_context_field
*packet_context_field
;
135 /* Current packet (NULL if not created yet) */
138 /* Current stream (NULL if not set yet) */
141 /* Current event (NULL if not created yet) */
144 /* Current event message (NULL if not created yet) */
145 bt_message
*event_msg
;
147 /* Database of current dynamic scopes */
149 bt_field
*stream_packet_context
;
150 bt_field
*event_common_context
;
151 bt_field
*event_spec_context
;
152 bt_field
*event_payload
;
158 /* Current medium buffer data */
160 /* Last address provided by medium */
163 /* Buffer size provided by medium (bytes) */
166 /* Offset within whole packet of addr (bits) */
167 size_t packet_offset
;
169 /* Current position from addr (bits) */
172 /* Position of the last event header from addr (bits) */
176 /* Binary type reader */
177 struct bt_bfcr
*bfcr
;
179 /* Current medium data */
181 struct bt_msg_iter_medium_ops medops
;
182 size_t max_request_sz
;
186 /* Stream beginning was emitted */
187 bool stream_begin_emitted
;
189 /* Current packet size (bits) (-1 if unknown) */
190 int64_t cur_exp_packet_total_size
;
192 /* Current content size (bits) (-1 if unknown) */
193 int64_t cur_exp_packet_content_size
;
195 /* Current stream class ID */
196 int64_t cur_stream_class_id
;
198 /* Current event class ID */
199 int64_t cur_event_class_id
;
201 /* Current data stream ID */
202 int64_t cur_data_stream_id
;
205 * Offset, in the underlying media, of the current packet's
206 * start (-1 if unknown).
208 off_t cur_packet_offset
;
210 /* Default clock's current value */
211 uint64_t default_clock_snapshot
;
213 /* End of packet snapshots */
215 uint64_t discarded_events
;
217 uint64_t beginning_clock
;
221 /* Stored values (for sequence lengths, variant tags) */
222 GArray
*stored_values
;
226 const char *state_string(enum state state
)
231 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
232 return "STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN";
233 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
234 return "STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE";
235 case STATE_AFTER_TRACE_PACKET_HEADER
:
236 return "STATE_AFTER_TRACE_PACKET_HEADER";
237 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
238 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN";
239 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
240 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE";
241 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
242 return "STATE_AFTER_STREAM_PACKET_CONTEXT";
243 case STATE_EMIT_MSG_NEW_PACKET
:
244 return "STATE_EMIT_MSG_NEW_PACKET";
245 case STATE_EMIT_MSG_NEW_STREAM
:
246 return "STATE_EMIT_MSG_NEW_STREAM";
247 case STATE_DSCOPE_EVENT_HEADER_BEGIN
:
248 return "STATE_DSCOPE_EVENT_HEADER_BEGIN";
249 case STATE_DSCOPE_EVENT_HEADER_CONTINUE
:
250 return "STATE_DSCOPE_EVENT_HEADER_CONTINUE";
251 case STATE_AFTER_EVENT_HEADER
:
252 return "STATE_AFTER_EVENT_HEADER";
253 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
:
254 return "STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN";
255 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
:
256 return "STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE";
257 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
:
258 return "STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN";
259 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
:
260 return "STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE";
261 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
262 return "STATE_DSCOPE_EVENT_PAYLOAD_BEGIN";
263 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
264 return "STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE";
265 case STATE_EMIT_MSG_EVENT
:
266 return "STATE_EMIT_MSG_EVENT";
267 case STATE_EMIT_MSG_END_OF_PACKET
:
268 return "STATE_EMIT_MSG_END_OF_PACKET";
271 case STATE_SKIP_PACKET_PADDING
:
272 return "STATE_SKIP_PACKET_PADDING";
279 int bt_msg_iter_switch_packet(struct bt_msg_iter
*notit
);
282 struct stack
*stack_new(struct bt_msg_iter
*notit
)
284 struct stack
*stack
= NULL
;
286 stack
= g_new0(struct stack
, 1);
288 BT_LOGE_STR("Failed to allocate one stack.");
292 stack
->entries
= g_array_new(FALSE
, TRUE
, sizeof(struct stack_entry
));
293 if (!stack
->entries
) {
294 BT_LOGE_STR("Failed to allocate a GArray.");
298 BT_LOGD("Created stack: notit-addr=%p, stack-addr=%p", notit
, stack
);
310 void stack_destroy(struct stack
*stack
)
313 BT_LOGD("Destroying stack: addr=%p", stack
);
315 if (stack
->entries
) {
316 g_array_free(stack
->entries
, TRUE
);
323 void stack_push(struct stack
*stack
, bt_field
*base
)
325 struct stack_entry
*entry
;
329 BT_LOGV("Pushing base field on stack: stack-addr=%p, "
330 "stack-size-before=%zu, stack-size-after=%zu",
331 stack
, stack
->size
, stack
->size
+ 1);
333 if (stack
->entries
->len
== stack
->size
) {
334 g_array_set_size(stack
->entries
, stack
->size
+ 1);
337 entry
= &g_array_index(stack
->entries
, struct stack_entry
, stack
->size
);
344 unsigned int stack_size(struct stack
*stack
)
351 void stack_pop(struct stack
*stack
)
354 BT_ASSERT(stack_size(stack
));
355 BT_LOGV("Popping from stack: "
356 "stack-addr=%p, stack-size-before=%zu, stack-size-after=%zu",
357 stack
, stack
->size
, stack
->size
- 1);
362 struct stack_entry
*stack_top(struct stack
*stack
)
365 BT_ASSERT(stack_size(stack
));
366 return &g_array_index(stack
->entries
, struct stack_entry
,
371 bool stack_empty(struct stack
*stack
)
373 return stack_size(stack
) == 0;
377 void stack_clear(struct stack
*stack
)
384 enum bt_msg_iter_status
msg_iter_status_from_m_status(
385 enum bt_msg_iter_medium_status m_status
)
387 /* They are the same */
388 return (int) m_status
;
392 size_t buf_size_bits(struct bt_msg_iter
*notit
)
394 return notit
->buf
.sz
* 8;
398 size_t buf_available_bits(struct bt_msg_iter
*notit
)
400 return buf_size_bits(notit
) - notit
->buf
.at
;
404 size_t packet_at(struct bt_msg_iter
*notit
)
406 return notit
->buf
.packet_offset
+ notit
->buf
.at
;
410 void buf_consume_bits(struct bt_msg_iter
*notit
, size_t incr
)
412 BT_LOGV("Advancing cursor: notit-addr=%p, cur-before=%zu, cur-after=%zu",
413 notit
, notit
->buf
.at
, notit
->buf
.at
+ incr
);
414 notit
->buf
.at
+= incr
;
418 enum bt_msg_iter_status
request_medium_bytes(
419 struct bt_msg_iter
*notit
)
421 uint8_t *buffer_addr
= NULL
;
422 size_t buffer_sz
= 0;
423 enum bt_msg_iter_medium_status m_status
;
425 BT_LOGV("Calling user function (request bytes): notit-addr=%p, "
426 "request-size=%zu", notit
, notit
->medium
.max_request_sz
);
427 m_status
= notit
->medium
.medops
.request_bytes(
428 notit
->medium
.max_request_sz
, &buffer_addr
,
429 &buffer_sz
, notit
->medium
.data
);
430 BT_LOGV("User function returned: status=%s, buf-addr=%p, buf-size=%zu",
431 bt_msg_iter_medium_status_string(m_status
),
432 buffer_addr
, buffer_sz
);
433 if (m_status
== BT_MSG_ITER_MEDIUM_STATUS_OK
) {
434 BT_ASSERT(buffer_sz
!= 0);
436 /* New packet offset is old one + old size (in bits) */
437 notit
->buf
.packet_offset
+= buf_size_bits(notit
);
439 /* Restart at the beginning of the new medium buffer */
441 notit
->buf
.last_eh_at
= SIZE_MAX
;
443 /* New medium buffer size */
444 notit
->buf
.sz
= buffer_sz
;
446 /* New medium buffer address */
447 notit
->buf
.addr
= buffer_addr
;
449 BT_LOGV("User function returned new bytes: "
450 "packet-offset=%zu, cur=%zu, size=%zu, addr=%p",
451 notit
->buf
.packet_offset
, notit
->buf
.at
,
452 notit
->buf
.sz
, notit
->buf
.addr
);
453 BT_LOGV_MEM(buffer_addr
, buffer_sz
, "Returned bytes at %p:",
455 } else if (m_status
== BT_MSG_ITER_MEDIUM_STATUS_EOF
) {
457 * User returned end of stream: validate that we're not
458 * in the middle of a packet header, packet context, or
461 if (notit
->cur_exp_packet_total_size
>= 0) {
462 if (packet_at(notit
) ==
463 notit
->cur_exp_packet_total_size
) {
467 if (packet_at(notit
) == 0) {
471 if (notit
->buf
.last_eh_at
!= SIZE_MAX
&&
472 notit
->buf
.at
== notit
->buf
.last_eh_at
) {
477 /* All other states are invalid */
478 BT_LOGW("User function returned %s, but message iterator is in an unexpected state: "
479 "state=%s, cur-packet-size=%" PRId64
", cur=%zu, "
480 "packet-cur=%zu, last-eh-at=%zu",
481 bt_msg_iter_medium_status_string(m_status
),
482 state_string(notit
->state
),
483 notit
->cur_exp_packet_total_size
,
484 notit
->buf
.at
, packet_at(notit
),
485 notit
->buf
.last_eh_at
);
486 m_status
= BT_MSG_ITER_MEDIUM_STATUS_ERROR
;
487 } else if (m_status
< 0) {
488 BT_LOGW("User function failed: status=%s",
489 bt_msg_iter_medium_status_string(m_status
));
493 return msg_iter_status_from_m_status(m_status
);
497 enum bt_msg_iter_status
buf_ensure_available_bits(
498 struct bt_msg_iter
*notit
)
500 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
502 if (unlikely(buf_available_bits(notit
) == 0)) {
504 * This _cannot_ return BT_MSG_ITER_STATUS_OK
507 status
= request_medium_bytes(notit
);
514 enum bt_msg_iter_status
read_dscope_begin_state(
515 struct bt_msg_iter
*notit
,
516 struct ctf_field_class
*dscope_fc
,
517 enum state done_state
, enum state continue_state
,
518 bt_field
*dscope_field
)
520 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
521 enum bt_bfcr_status bfcr_status
;
522 size_t consumed_bits
;
524 notit
->cur_dscope_field
= dscope_field
;
525 BT_LOGV("Starting BFCR: notit-addr=%p, bfcr-addr=%p, fc-addr=%p",
526 notit
, notit
->bfcr
, dscope_fc
);
527 consumed_bits
= bt_bfcr_start(notit
->bfcr
, dscope_fc
,
528 notit
->buf
.addr
, notit
->buf
.at
, packet_at(notit
),
529 notit
->buf
.sz
, &bfcr_status
);
530 BT_LOGV("BFCR consumed bits: size=%zu", consumed_bits
);
532 switch (bfcr_status
) {
533 case BT_BFCR_STATUS_OK
:
534 /* Field class was read completely */
535 BT_LOGV_STR("Field was completely decoded.");
536 notit
->state
= done_state
;
538 case BT_BFCR_STATUS_EOF
:
539 BT_LOGV_STR("BFCR needs more data to decode field completely.");
540 notit
->state
= continue_state
;
543 BT_LOGW("BFCR failed to start: notit-addr=%p, bfcr-addr=%p, "
544 "status=%s", notit
, notit
->bfcr
,
545 bt_bfcr_status_string(bfcr_status
));
546 status
= BT_MSG_ITER_STATUS_ERROR
;
550 /* Consume bits now since we know we're not in an error state */
551 buf_consume_bits(notit
, consumed_bits
);
558 enum bt_msg_iter_status
read_dscope_continue_state(
559 struct bt_msg_iter
*notit
, enum state done_state
)
561 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
562 enum bt_bfcr_status bfcr_status
;
563 size_t consumed_bits
;
565 BT_LOGV("Continuing BFCR: notit-addr=%p, bfcr-addr=%p",
568 status
= buf_ensure_available_bits(notit
);
569 if (status
!= BT_MSG_ITER_STATUS_OK
) {
571 BT_LOGW("Cannot ensure that buffer has at least one byte: "
572 "msg-addr=%p, status=%s",
573 notit
, bt_msg_iter_status_string(status
));
575 BT_LOGV("Cannot ensure that buffer has at least one byte: "
576 "msg-addr=%p, status=%s",
577 notit
, bt_msg_iter_status_string(status
));
583 consumed_bits
= bt_bfcr_continue(notit
->bfcr
, notit
->buf
.addr
,
584 notit
->buf
.sz
, &bfcr_status
);
585 BT_LOGV("BFCR consumed bits: size=%zu", consumed_bits
);
587 switch (bfcr_status
) {
588 case BT_BFCR_STATUS_OK
:
589 /* Type was read completely. */
590 BT_LOGV_STR("Field was completely decoded.");
591 notit
->state
= done_state
;
593 case BT_BFCR_STATUS_EOF
:
594 /* Stay in this continue state. */
595 BT_LOGV_STR("BFCR needs more data to decode field completely.");
598 BT_LOGW("BFCR failed to continue: notit-addr=%p, bfcr-addr=%p, "
599 "status=%s", notit
, notit
->bfcr
,
600 bt_bfcr_status_string(bfcr_status
));
601 status
= BT_MSG_ITER_STATUS_ERROR
;
605 /* Consume bits now since we know we're not in an error state. */
606 buf_consume_bits(notit
, consumed_bits
);
612 void release_event_dscopes(struct bt_msg_iter
*notit
)
614 notit
->dscopes
.event_common_context
= NULL
;
615 notit
->dscopes
.event_spec_context
= NULL
;
616 notit
->dscopes
.event_payload
= NULL
;
620 void release_all_dscopes(struct bt_msg_iter
*notit
)
622 notit
->dscopes
.stream_packet_context
= NULL
;
624 if (notit
->packet_context_field
) {
625 bt_packet_context_field_release(notit
->packet_context_field
);
626 notit
->packet_context_field
= NULL
;
629 release_event_dscopes(notit
);
633 enum bt_msg_iter_status
read_packet_header_begin_state(
634 struct bt_msg_iter
*notit
)
636 struct ctf_field_class
*packet_header_fc
= NULL
;
637 enum bt_msg_iter_status ret
= BT_MSG_ITER_STATUS_OK
;
639 if (bt_msg_iter_switch_packet(notit
)) {
640 BT_LOGW("Cannot switch packet: notit-addr=%p", notit
);
641 ret
= BT_MSG_ITER_STATUS_ERROR
;
645 /* Packet header class is common to the whole trace class. */
646 packet_header_fc
= notit
->meta
.tc
->packet_header_fc
;
647 if (!packet_header_fc
) {
648 notit
->state
= STATE_AFTER_TRACE_PACKET_HEADER
;
652 notit
->cur_stream_class_id
= -1;
653 notit
->cur_event_class_id
= -1;
654 notit
->cur_data_stream_id
= -1;
655 BT_LOGV("Decoding packet header field:"
656 "notit-addr=%p, trace-class-addr=%p, fc-addr=%p",
657 notit
, notit
->meta
.tc
, packet_header_fc
);
658 ret
= read_dscope_begin_state(notit
, packet_header_fc
,
659 STATE_AFTER_TRACE_PACKET_HEADER
,
660 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
, NULL
);
662 BT_LOGW("Cannot decode packet header field: "
663 "notit-addr=%p, trace-class-addr=%p, "
665 notit
, notit
->meta
.tc
, packet_header_fc
);
673 enum bt_msg_iter_status
read_packet_header_continue_state(
674 struct bt_msg_iter
*notit
)
676 return read_dscope_continue_state(notit
,
677 STATE_AFTER_TRACE_PACKET_HEADER
);
681 enum bt_msg_iter_status
set_current_stream_class(struct bt_msg_iter
*notit
)
683 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
684 struct ctf_stream_class
*new_stream_class
= NULL
;
686 if (notit
->cur_stream_class_id
== -1) {
688 * No current stream class ID field, therefore only one
691 if (notit
->meta
.tc
->stream_classes
->len
!= 1) {
692 BT_LOGW("Need exactly one stream class since there's "
693 "no stream class ID field: "
694 "notit-addr=%p", notit
);
695 status
= BT_MSG_ITER_STATUS_ERROR
;
699 new_stream_class
= notit
->meta
.tc
->stream_classes
->pdata
[0];
700 notit
->cur_stream_class_id
= new_stream_class
->id
;
703 new_stream_class
= ctf_trace_class_borrow_stream_class_by_id(
704 notit
->meta
.tc
, notit
->cur_stream_class_id
);
705 if (!new_stream_class
) {
706 BT_LOGW("No stream class with ID of stream class ID to use in trace class: "
707 "notit-addr=%p, stream-class-id=%" PRIu64
", "
708 "trace-class-addr=%p",
709 notit
, notit
->cur_stream_class_id
, notit
->meta
.tc
);
710 status
= BT_MSG_ITER_STATUS_ERROR
;
714 if (notit
->meta
.sc
) {
715 if (new_stream_class
!= notit
->meta
.sc
) {
716 BT_LOGW("Two packets refer to two different stream classes within the same packet sequence: "
717 "notit-addr=%p, prev-stream-class-addr=%p, "
718 "prev-stream-class-id=%" PRId64
", "
719 "next-stream-class-addr=%p, "
720 "next-stream-class-id=%" PRId64
", "
722 notit
, notit
->meta
.sc
,
725 new_stream_class
->id
,
727 status
= BT_MSG_ITER_STATUS_ERROR
;
731 notit
->meta
.sc
= new_stream_class
;
734 BT_LOGV("Set current stream class: "
735 "notit-addr=%p, stream-class-addr=%p, "
736 "stream-class-id=%" PRId64
,
737 notit
, notit
->meta
.sc
, notit
->meta
.sc
->id
);
744 enum bt_msg_iter_status
set_current_stream(struct bt_msg_iter
*notit
)
746 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
747 bt_stream
*stream
= NULL
;
749 BT_LOGV("Calling user function (get stream): notit-addr=%p, "
750 "stream-class-addr=%p, stream-class-id=%" PRId64
,
751 notit
, notit
->meta
.sc
,
753 stream
= notit
->medium
.medops
.borrow_stream(
754 notit
->meta
.sc
->ir_sc
, notit
->cur_data_stream_id
,
756 bt_stream_get_ref(stream
);
757 BT_LOGV("User function returned: stream-addr=%p", stream
);
759 BT_LOGW_STR("User function failed to return a stream object "
760 "for the given stream class.");
761 status
= BT_MSG_ITER_STATUS_ERROR
;
765 if (notit
->stream
&& stream
!= notit
->stream
) {
766 BT_LOGW("User function returned a different stream than the "
767 "previous one for the same sequence of packets.");
768 status
= BT_MSG_ITER_STATUS_ERROR
;
772 BT_STREAM_MOVE_REF(notit
->stream
, stream
);
775 bt_stream_put_ref(stream
);
780 enum bt_msg_iter_status
set_current_packet(struct bt_msg_iter
*notit
)
782 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
783 bt_packet
*packet
= NULL
;
785 BT_LOGV("Creating packet for packet message: "
786 "notit-addr=%p", notit
);
787 BT_LOGV("Creating packet from stream: "
788 "notit-addr=%p, stream-addr=%p, "
789 "stream-class-addr=%p, "
790 "stream-class-id=%" PRId64
,
791 notit
, notit
->stream
, notit
->meta
.sc
,
795 BT_ASSERT(notit
->stream
);
796 packet
= bt_packet_create(notit
->stream
);
798 BT_LOGE("Cannot create packet from stream: "
799 "notit-addr=%p, stream-addr=%p, "
800 "stream-class-addr=%p, "
801 "stream-class-id=%" PRId64
,
802 notit
, notit
->stream
, notit
->meta
.sc
,
810 BT_PACKET_PUT_REF_AND_RESET(packet
);
811 status
= BT_MSG_ITER_STATUS_ERROR
;
814 BT_PACKET_MOVE_REF(notit
->packet
, packet
);
819 enum bt_msg_iter_status
after_packet_header_state(
820 struct bt_msg_iter
*notit
)
822 enum bt_msg_iter_status status
;
824 status
= set_current_stream_class(notit
);
825 if (status
!= BT_MSG_ITER_STATUS_OK
) {
829 notit
->state
= STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
;
836 enum bt_msg_iter_status
read_packet_context_begin_state(
837 struct bt_msg_iter
*notit
)
839 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
840 struct ctf_field_class
*packet_context_fc
;
842 BT_ASSERT(notit
->meta
.sc
);
843 packet_context_fc
= notit
->meta
.sc
->packet_context_fc
;
844 if (!packet_context_fc
) {
845 BT_LOGV("No packet packet context field class in stream class: continuing: "
846 "notit-addr=%p, stream-class-addr=%p, "
847 "stream-class-id=%" PRId64
,
848 notit
, notit
->meta
.sc
,
850 notit
->state
= STATE_AFTER_STREAM_PACKET_CONTEXT
;
854 BT_ASSERT(!notit
->packet_context_field
);
856 if (packet_context_fc
->in_ir
) {
858 * Create free packet context field from stream class.
859 * This field is going to be moved to the packet once we
860 * create it. We cannot create the packet now because a
861 * packet is created from a stream, and this API must be
862 * able to return the packet context properties without
864 * (bt_msg_iter_get_packet_properties()).
866 notit
->packet_context_field
=
867 bt_packet_context_field_create(
868 notit
->meta
.sc
->ir_sc
);
869 if (!notit
->packet_context_field
) {
870 BT_LOGE_STR("Cannot create packet context field wrapper from stream class.");
871 status
= BT_MSG_ITER_STATUS_ERROR
;
875 notit
->dscopes
.stream_packet_context
=
876 bt_packet_context_field_borrow_field(
877 notit
->packet_context_field
);
878 BT_ASSERT(notit
->dscopes
.stream_packet_context
);
881 BT_LOGV("Decoding packet context field: "
882 "notit-addr=%p, stream-class-addr=%p, "
883 "stream-class-id=%" PRId64
", fc-addr=%p",
884 notit
, notit
->meta
.sc
,
885 notit
->meta
.sc
->id
, packet_context_fc
);
886 status
= read_dscope_begin_state(notit
, packet_context_fc
,
887 STATE_AFTER_STREAM_PACKET_CONTEXT
,
888 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
889 notit
->dscopes
.stream_packet_context
);
891 BT_LOGW("Cannot decode packet context field: "
892 "notit-addr=%p, stream-class-addr=%p, "
893 "stream-class-id=%" PRId64
", fc-addr=%p",
894 notit
, notit
->meta
.sc
,
904 enum bt_msg_iter_status
read_packet_context_continue_state(
905 struct bt_msg_iter
*notit
)
907 return read_dscope_continue_state(notit
,
908 STATE_AFTER_STREAM_PACKET_CONTEXT
);
912 enum bt_msg_iter_status
set_current_packet_content_sizes(
913 struct bt_msg_iter
*notit
)
915 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
917 if (notit
->cur_exp_packet_total_size
== -1) {
918 if (notit
->cur_exp_packet_content_size
!= -1) {
919 BT_LOGW("Content size is set, but packet size is not: "
920 "notit-addr=%p, packet-context-field-addr=%p, "
921 "packet-size=%" PRId64
", content-size=%" PRId64
,
922 notit
, notit
->dscopes
.stream_packet_context
,
923 notit
->cur_exp_packet_total_size
,
924 notit
->cur_exp_packet_content_size
);
925 status
= BT_MSG_ITER_STATUS_ERROR
;
929 if (notit
->cur_exp_packet_content_size
== -1) {
930 notit
->cur_exp_packet_content_size
=
931 notit
->cur_exp_packet_total_size
;
935 if (notit
->cur_exp_packet_content_size
>
936 notit
->cur_exp_packet_total_size
) {
937 BT_LOGW("Invalid packet or content size: "
938 "content size is greater than packet size: "
939 "notit-addr=%p, packet-context-field-addr=%p, "
940 "packet-size=%" PRId64
", content-size=%" PRId64
,
941 notit
, notit
->dscopes
.stream_packet_context
,
942 notit
->cur_exp_packet_total_size
,
943 notit
->cur_exp_packet_content_size
);
944 status
= BT_MSG_ITER_STATUS_ERROR
;
948 BT_LOGV("Set current packet and content sizes: "
949 "notit-addr=%p, packet-size=%" PRIu64
", content-size=%" PRIu64
,
950 notit
, notit
->cur_exp_packet_total_size
,
951 notit
->cur_exp_packet_content_size
);
957 enum bt_msg_iter_status
after_packet_context_state(
958 struct bt_msg_iter
*notit
)
960 enum bt_msg_iter_status status
;
962 status
= set_current_packet_content_sizes(notit
);
963 if (status
!= BT_MSG_ITER_STATUS_OK
) {
967 if (notit
->stream_begin_emitted
) {
968 notit
->state
= STATE_EMIT_MSG_NEW_PACKET
;
970 notit
->state
= STATE_EMIT_MSG_NEW_STREAM
;
978 enum bt_msg_iter_status
read_event_header_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
*event_header_fc
= NULL
;
984 /* Reset the position of the last event header */
985 notit
->buf
.last_eh_at
= notit
->buf
.at
;
986 notit
->cur_event_class_id
= -1;
988 /* Check if we have some content left */
989 if (notit
->cur_exp_packet_content_size
>= 0) {
990 if (unlikely(packet_at(notit
) ==
991 notit
->cur_exp_packet_content_size
)) {
992 /* No more events! */
993 BT_LOGV("Reached end of packet: notit-addr=%p, "
994 "cur=%zu", notit
, packet_at(notit
));
995 notit
->state
= STATE_EMIT_MSG_END_OF_PACKET
;
997 } else if (unlikely(packet_at(notit
) >
998 notit
->cur_exp_packet_content_size
)) {
999 /* That's not supposed to happen */
1000 BT_LOGV("Before decoding event header field: cursor is passed the packet's content: "
1001 "notit-addr=%p, content-size=%" PRId64
", "
1003 notit
->cur_exp_packet_content_size
,
1005 status
= BT_MSG_ITER_STATUS_ERROR
;
1010 * "Infinite" content: we're done when the medium has
1011 * nothing else for us.
1013 status
= buf_ensure_available_bits(notit
);
1014 if (status
!= BT_MSG_ITER_STATUS_OK
) {
1016 * If this function returns
1017 * `BT_MSG_ITER_STATUS_EOF`:
1019 * 1. bt_msg_iter_get_next_message()
1020 * emits a "packet end" message. This
1021 * resets the current packet. The state
1022 * remains unchanged otherwise.
1023 * 2. This function is called again. It returns
1024 * `BT_MSG_ITER_STATUS_EOF` again.
1025 * 3. bt_msg_iter_get_next_message()
1026 * emits a "stream end" message because
1027 * there's no current packet. It sets the
1028 * current state to `STATE_DONE`.
1034 release_event_dscopes(notit
);
1035 BT_ASSERT(notit
->meta
.sc
);
1036 event_header_fc
= notit
->meta
.sc
->event_header_fc
;
1037 if (!event_header_fc
) {
1038 notit
->state
= STATE_AFTER_EVENT_HEADER
;
1042 BT_LOGV("Decoding event header field: "
1043 "notit-addr=%p, stream-class-addr=%p, "
1044 "stream-class-id=%" PRId64
", "
1046 notit
, notit
->meta
.sc
,
1049 status
= read_dscope_begin_state(notit
, event_header_fc
,
1050 STATE_AFTER_EVENT_HEADER
,
1051 STATE_DSCOPE_EVENT_HEADER_CONTINUE
, NULL
);
1053 BT_LOGW("Cannot decode event header field: "
1054 "notit-addr=%p, stream-class-addr=%p, "
1055 "stream-class-id=%" PRId64
", fc-addr=%p",
1056 notit
, notit
->meta
.sc
,
1066 enum bt_msg_iter_status
read_event_header_continue_state(
1067 struct bt_msg_iter
*notit
)
1069 return read_dscope_continue_state(notit
,
1070 STATE_AFTER_EVENT_HEADER
);
1074 enum bt_msg_iter_status
set_current_event_class(struct bt_msg_iter
*notit
)
1076 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1078 struct ctf_event_class
*new_event_class
= NULL
;
1080 if (notit
->cur_event_class_id
== -1) {
1082 * No current event class ID field, therefore only one
1085 if (notit
->meta
.sc
->event_classes
->len
!= 1) {
1086 BT_LOGW("Need exactly one event class since there's "
1087 "no event class ID field: "
1088 "notit-addr=%p", notit
);
1089 status
= BT_MSG_ITER_STATUS_ERROR
;
1093 new_event_class
= notit
->meta
.sc
->event_classes
->pdata
[0];
1094 notit
->cur_event_class_id
= new_event_class
->id
;
1097 new_event_class
= ctf_stream_class_borrow_event_class_by_id(
1098 notit
->meta
.sc
, notit
->cur_event_class_id
);
1099 if (!new_event_class
) {
1100 BT_LOGW("No event class with ID of event class ID to use in stream class: "
1101 "notit-addr=%p, stream-class-id=%" PRIu64
", "
1102 "event-class-id=%" PRIu64
", "
1103 "trace-class-addr=%p",
1104 notit
, notit
->meta
.sc
->id
, notit
->cur_event_class_id
,
1106 status
= BT_MSG_ITER_STATUS_ERROR
;
1110 notit
->meta
.ec
= new_event_class
;
1111 BT_LOGV("Set current event class: "
1112 "notit-addr=%p, event-class-addr=%p, "
1113 "event-class-id=%" PRId64
", "
1114 "event-class-name=\"%s\"",
1115 notit
, notit
->meta
.ec
, notit
->meta
.ec
->id
,
1116 notit
->meta
.ec
->name
->str
);
1123 enum bt_msg_iter_status
set_current_event_message(
1124 struct bt_msg_iter
*notit
)
1126 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1127 bt_message
*msg
= NULL
;
1129 BT_ASSERT(notit
->meta
.ec
);
1130 BT_ASSERT(notit
->packet
);
1131 BT_LOGV("Creating event message from event class and packet: "
1132 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", packet-addr=%p",
1133 notit
, notit
->meta
.ec
,
1134 notit
->meta
.ec
->name
->str
,
1136 BT_ASSERT(notit
->msg_iter
);
1137 msg
= bt_message_event_create(notit
->msg_iter
,
1138 notit
->meta
.ec
->ir_ec
, notit
->packet
);
1140 BT_LOGE("Cannot create event message: "
1141 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", "
1143 notit
, notit
->meta
.ec
,
1144 notit
->meta
.ec
->name
->str
,
1152 BT_MESSAGE_PUT_REF_AND_RESET(msg
);
1153 status
= BT_MSG_ITER_STATUS_ERROR
;
1156 BT_MESSAGE_MOVE_REF(notit
->event_msg
, msg
);
1161 enum bt_msg_iter_status
after_event_header_state(
1162 struct bt_msg_iter
*notit
)
1164 enum bt_msg_iter_status status
;
1166 status
= set_current_event_class(notit
);
1167 if (status
!= BT_MSG_ITER_STATUS_OK
) {
1171 status
= set_current_event_message(notit
);
1172 if (status
!= BT_MSG_ITER_STATUS_OK
) {
1176 notit
->event
= bt_message_event_borrow_event(
1178 BT_ASSERT(notit
->event
);
1179 notit
->state
= STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
;
1186 enum bt_msg_iter_status
read_event_common_context_begin_state(
1187 struct bt_msg_iter
*notit
)
1189 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1190 struct ctf_field_class
*event_common_context_fc
;
1192 event_common_context_fc
= notit
->meta
.sc
->event_common_context_fc
;
1193 if (!event_common_context_fc
) {
1194 notit
->state
= STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
;
1198 if (event_common_context_fc
->in_ir
) {
1199 BT_ASSERT(!notit
->dscopes
.event_common_context
);
1200 notit
->dscopes
.event_common_context
=
1201 bt_event_borrow_common_context_field(
1203 BT_ASSERT(notit
->dscopes
.event_common_context
);
1206 BT_LOGV("Decoding event common context field: "
1207 "notit-addr=%p, stream-class-addr=%p, "
1208 "stream-class-id=%" PRId64
", "
1210 notit
, notit
->meta
.sc
,
1212 event_common_context_fc
);
1213 status
= read_dscope_begin_state(notit
, event_common_context_fc
,
1214 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
,
1215 STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
,
1216 notit
->dscopes
.event_common_context
);
1218 BT_LOGW("Cannot decode event common context field: "
1219 "notit-addr=%p, stream-class-addr=%p, "
1220 "stream-class-id=%" PRId64
", fc-addr=%p",
1221 notit
, notit
->meta
.sc
,
1223 event_common_context_fc
);
1231 enum bt_msg_iter_status
read_event_common_context_continue_state(
1232 struct bt_msg_iter
*notit
)
1234 return read_dscope_continue_state(notit
,
1235 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
);
1239 enum bt_msg_iter_status
read_event_spec_context_begin_state(
1240 struct bt_msg_iter
*notit
)
1242 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1243 struct ctf_field_class
*event_spec_context_fc
;
1245 event_spec_context_fc
= notit
->meta
.ec
->spec_context_fc
;
1246 if (!event_spec_context_fc
) {
1247 notit
->state
= STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
;
1251 if (event_spec_context_fc
->in_ir
) {
1252 BT_ASSERT(!notit
->dscopes
.event_spec_context
);
1253 notit
->dscopes
.event_spec_context
=
1254 bt_event_borrow_specific_context_field(
1256 BT_ASSERT(notit
->dscopes
.event_spec_context
);
1259 BT_LOGV("Decoding event specific context field: "
1260 "notit-addr=%p, event-class-addr=%p, "
1261 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1263 notit
, notit
->meta
.ec
,
1264 notit
->meta
.ec
->name
->str
,
1266 event_spec_context_fc
);
1267 status
= read_dscope_begin_state(notit
, event_spec_context_fc
,
1268 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
1269 STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
,
1270 notit
->dscopes
.event_spec_context
);
1272 BT_LOGW("Cannot decode event specific context field: "
1273 "notit-addr=%p, event-class-addr=%p, "
1274 "event-class-name=\"%s\", "
1275 "event-class-id=%" PRId64
", fc-addr=%p",
1276 notit
, notit
->meta
.ec
,
1277 notit
->meta
.ec
->name
->str
,
1279 event_spec_context_fc
);
1287 enum bt_msg_iter_status
read_event_spec_context_continue_state(
1288 struct bt_msg_iter
*notit
)
1290 return read_dscope_continue_state(notit
,
1291 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
);
1295 enum bt_msg_iter_status
read_event_payload_begin_state(
1296 struct bt_msg_iter
*notit
)
1298 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1299 struct ctf_field_class
*event_payload_fc
;
1301 event_payload_fc
= notit
->meta
.ec
->payload_fc
;
1302 if (!event_payload_fc
) {
1303 notit
->state
= STATE_EMIT_MSG_EVENT
;
1307 if (event_payload_fc
->in_ir
) {
1308 BT_ASSERT(!notit
->dscopes
.event_payload
);
1309 notit
->dscopes
.event_payload
=
1310 bt_event_borrow_payload_field(
1312 BT_ASSERT(notit
->dscopes
.event_payload
);
1315 BT_LOGV("Decoding event payload field: "
1316 "notit-addr=%p, event-class-addr=%p, "
1317 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1319 notit
, notit
->meta
.ec
,
1320 notit
->meta
.ec
->name
->str
,
1323 status
= read_dscope_begin_state(notit
, event_payload_fc
,
1324 STATE_EMIT_MSG_EVENT
,
1325 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
1326 notit
->dscopes
.event_payload
);
1328 BT_LOGW("Cannot decode event payload field: "
1329 "notit-addr=%p, event-class-addr=%p, "
1330 "event-class-name=\"%s\", "
1331 "event-class-id=%" PRId64
", fc-addr=%p",
1332 notit
, notit
->meta
.ec
,
1333 notit
->meta
.ec
->name
->str
,
1343 enum bt_msg_iter_status
read_event_payload_continue_state(
1344 struct bt_msg_iter
*notit
)
1346 return read_dscope_continue_state(notit
, STATE_EMIT_MSG_EVENT
);
1350 enum bt_msg_iter_status
skip_packet_padding_state(
1351 struct bt_msg_iter
*notit
)
1353 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1354 size_t bits_to_skip
;
1356 BT_ASSERT(notit
->cur_exp_packet_total_size
> 0);
1357 bits_to_skip
= notit
->cur_exp_packet_total_size
- packet_at(notit
);
1358 if (bits_to_skip
== 0) {
1359 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1362 size_t bits_to_consume
;
1364 BT_LOGV("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu",
1365 bits_to_skip
, notit
, bits_to_skip
);
1366 status
= buf_ensure_available_bits(notit
);
1367 if (status
!= BT_MSG_ITER_STATUS_OK
) {
1371 bits_to_consume
= MIN(buf_available_bits(notit
), bits_to_skip
);
1372 BT_LOGV("Skipping %zu bits of padding: notit-addr=%p, size=%zu",
1373 bits_to_consume
, notit
, bits_to_consume
);
1374 buf_consume_bits(notit
, bits_to_consume
);
1375 bits_to_skip
= notit
->cur_exp_packet_total_size
-
1377 if (bits_to_skip
== 0) {
1378 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1388 enum bt_msg_iter_status
handle_state(struct bt_msg_iter
*notit
)
1390 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1391 const enum state state
= notit
->state
;
1393 BT_LOGV("Handling state: notit-addr=%p, state=%s",
1394 notit
, state_string(state
));
1396 // TODO: optimalize!
1399 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1401 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
1402 status
= read_packet_header_begin_state(notit
);
1404 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
1405 status
= read_packet_header_continue_state(notit
);
1407 case STATE_AFTER_TRACE_PACKET_HEADER
:
1408 status
= after_packet_header_state(notit
);
1410 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
1411 status
= read_packet_context_begin_state(notit
);
1413 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
1414 status
= read_packet_context_continue_state(notit
);
1416 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
1417 status
= after_packet_context_state(notit
);
1419 case STATE_EMIT_MSG_NEW_STREAM
:
1420 notit
->state
= STATE_EMIT_MSG_NEW_PACKET
;
1422 case STATE_EMIT_MSG_NEW_PACKET
:
1423 notit
->state
= STATE_DSCOPE_EVENT_HEADER_BEGIN
;
1425 case STATE_DSCOPE_EVENT_HEADER_BEGIN
:
1426 status
= read_event_header_begin_state(notit
);
1428 case STATE_DSCOPE_EVENT_HEADER_CONTINUE
:
1429 status
= read_event_header_continue_state(notit
);
1431 case STATE_AFTER_EVENT_HEADER
:
1432 status
= after_event_header_state(notit
);
1434 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
:
1435 status
= read_event_common_context_begin_state(notit
);
1437 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
:
1438 status
= read_event_common_context_continue_state(notit
);
1440 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
:
1441 status
= read_event_spec_context_begin_state(notit
);
1443 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
:
1444 status
= read_event_spec_context_continue_state(notit
);
1446 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
1447 status
= read_event_payload_begin_state(notit
);
1449 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
1450 status
= read_event_payload_continue_state(notit
);
1452 case STATE_EMIT_MSG_EVENT
:
1453 notit
->state
= STATE_DSCOPE_EVENT_HEADER_BEGIN
;
1455 case STATE_SKIP_PACKET_PADDING
:
1456 status
= skip_packet_padding_state(notit
);
1458 case STATE_EMIT_MSG_END_OF_PACKET
:
1459 notit
->state
= STATE_SKIP_PACKET_PADDING
;
1462 BT_LOGD("Unknown CTF plugin message iterator state: "
1463 "notit-addr=%p, state=%d", notit
, notit
->state
);
1467 BT_LOGV("Handled state: notit-addr=%p, status=%s, "
1468 "prev-state=%s, cur-state=%s",
1469 notit
, bt_msg_iter_status_string(status
),
1470 state_string(state
), state_string(notit
->state
));
1475 * Resets the internal state of a CTF message iterator.
1478 void bt_msg_iter_reset(struct bt_msg_iter
*notit
)
1481 BT_LOGD("Resetting message iterator: addr=%p", notit
);
1482 stack_clear(notit
->stack
);
1483 notit
->meta
.sc
= NULL
;
1484 notit
->meta
.ec
= NULL
;
1485 BT_PACKET_PUT_REF_AND_RESET(notit
->packet
);
1486 BT_STREAM_PUT_REF_AND_RESET(notit
->stream
);
1487 BT_MESSAGE_PUT_REF_AND_RESET(notit
->event_msg
);
1488 release_all_dscopes(notit
);
1489 notit
->cur_dscope_field
= NULL
;
1491 if (notit
->packet_context_field
) {
1492 bt_packet_context_field_release(notit
->packet_context_field
);
1493 notit
->packet_context_field
= NULL
;
1496 notit
->buf
.addr
= NULL
;
1499 notit
->buf
.last_eh_at
= SIZE_MAX
;
1500 notit
->buf
.packet_offset
= 0;
1501 notit
->state
= STATE_INIT
;
1502 notit
->cur_exp_packet_content_size
= -1;
1503 notit
->cur_exp_packet_total_size
= -1;
1504 notit
->cur_packet_offset
= -1;
1505 notit
->cur_stream_class_id
= -1;
1506 notit
->cur_event_class_id
= -1;
1507 notit
->cur_data_stream_id
= -1;
1508 notit
->stream_begin_emitted
= false;
1512 int bt_msg_iter_switch_packet(struct bt_msg_iter
*notit
)
1517 * We don't put the stream class here because we need to make
1518 * sure that all the packets processed by the same message
1519 * iterator refer to the same stream class (the first one).
1523 if (notit
->cur_exp_packet_total_size
!= -1) {
1524 notit
->cur_packet_offset
+= notit
->cur_exp_packet_total_size
;
1527 BT_LOGV("Switching packet: notit-addr=%p, cur=%zu, "
1528 "packet-offset=%" PRId64
, notit
, notit
->buf
.at
,
1529 notit
->cur_packet_offset
);
1530 stack_clear(notit
->stack
);
1531 notit
->meta
.ec
= NULL
;
1532 BT_PACKET_PUT_REF_AND_RESET(notit
->packet
);
1533 BT_MESSAGE_PUT_REF_AND_RESET(notit
->event_msg
);
1534 release_all_dscopes(notit
);
1535 notit
->cur_dscope_field
= NULL
;
1538 * Adjust current buffer so that addr points to the beginning of the new
1541 if (notit
->buf
.addr
) {
1542 size_t consumed_bytes
= (size_t) (notit
->buf
.at
/ CHAR_BIT
);
1544 /* Packets are assumed to start on a byte frontier. */
1545 if (notit
->buf
.at
% CHAR_BIT
) {
1546 BT_LOGW("Cannot switch packet: current position is not a multiple of 8: "
1547 "notit-addr=%p, cur=%zu", notit
, notit
->buf
.at
);
1552 notit
->buf
.addr
+= consumed_bytes
;
1553 notit
->buf
.sz
-= consumed_bytes
;
1555 notit
->buf
.packet_offset
= 0;
1556 BT_LOGV("Adjusted buffer: addr=%p, size=%zu",
1557 notit
->buf
.addr
, notit
->buf
.sz
);
1560 notit
->cur_exp_packet_content_size
= -1;
1561 notit
->cur_exp_packet_total_size
= -1;
1562 notit
->cur_stream_class_id
= -1;
1563 notit
->cur_event_class_id
= -1;
1564 notit
->cur_data_stream_id
= -1;
1565 notit
->snapshots
.discarded_events
= UINT64_C(-1);
1566 notit
->snapshots
.packets
= UINT64_C(-1);
1567 notit
->snapshots
.beginning_clock
= UINT64_C(-1);
1568 notit
->snapshots
.end_clock
= UINT64_C(-1);
1575 bt_field
*borrow_next_field(struct bt_msg_iter
*notit
)
1577 bt_field
*next_field
= NULL
;
1578 bt_field
*base_field
;
1579 const bt_field_class
*base_fc
;
1582 BT_ASSERT(!stack_empty(notit
->stack
));
1583 index
= stack_top(notit
->stack
)->index
;
1584 base_field
= stack_top(notit
->stack
)->base
;
1585 BT_ASSERT(base_field
);
1586 base_fc
= bt_field_borrow_class_const(base_field
);
1589 switch (bt_field_class_get_type(base_fc
)) {
1590 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
1593 bt_field_class_structure_get_member_count(
1594 bt_field_borrow_class_const(
1597 bt_field_structure_borrow_member_field_by_index(
1601 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
1602 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
1603 BT_ASSERT(index
< bt_field_array_get_length(base_field
));
1604 next_field
= bt_field_array_borrow_element_field_by_index(
1607 case BT_FIELD_CLASS_TYPE_VARIANT
:
1608 BT_ASSERT(index
== 0);
1609 next_field
= bt_field_variant_borrow_selected_option_field(
1616 BT_ASSERT(next_field
);
1621 void update_default_clock(struct bt_msg_iter
*notit
, uint64_t new_val
,
1622 uint64_t new_val_size
)
1624 uint64_t new_val_mask
;
1625 uint64_t cur_value_masked
;
1627 BT_ASSERT(new_val_size
> 0);
1630 * Special case for a 64-bit new value, which is the limit
1631 * of a clock value as of this version: overwrite the
1632 * current value directly.
1634 if (new_val_size
== 64) {
1635 notit
->default_clock_snapshot
= new_val
;
1639 new_val_mask
= (1ULL << new_val_size
) - 1;
1640 cur_value_masked
= notit
->default_clock_snapshot
& new_val_mask
;
1642 if (new_val
< cur_value_masked
) {
1644 * It looks like a wrap happened on the number of bits
1645 * of the requested new value. Assume that the clock
1646 * value wrapped only one time.
1648 notit
->default_clock_snapshot
+= new_val_mask
+ 1;
1651 /* Clear the low bits of the current clock value. */
1652 notit
->default_clock_snapshot
&= ~new_val_mask
;
1654 /* Set the low bits of the current clock value. */
1655 notit
->default_clock_snapshot
|= new_val
;
1658 BT_LOGV("Updated default clock's value from integer field's value: "
1659 "value=%" PRIu64
, notit
->default_clock_snapshot
);
1663 enum bt_bfcr_status
bfcr_unsigned_int_cb(uint64_t value
,
1664 struct ctf_field_class
*fc
, void *data
)
1666 struct bt_msg_iter
*notit
= data
;
1667 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
1668 bt_field
*field
= NULL
;
1669 struct ctf_field_class_int
*int_fc
= (void *) fc
;
1671 BT_LOGV("Unsigned integer function called from BFCR: "
1672 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1673 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64
,
1674 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
1676 if (likely(int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
)) {
1677 goto update_def_clock
;
1680 switch (int_fc
->meaning
) {
1681 case CTF_FIELD_CLASS_MEANING_EVENT_CLASS_ID
:
1682 notit
->cur_event_class_id
= value
;
1684 case CTF_FIELD_CLASS_MEANING_DATA_STREAM_ID
:
1685 notit
->cur_data_stream_id
= value
;
1687 case CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME
:
1688 notit
->snapshots
.beginning_clock
= value
;
1690 case CTF_FIELD_CLASS_MEANING_PACKET_END_TIME
:
1691 notit
->snapshots
.end_clock
= value
;
1693 case CTF_FIELD_CLASS_MEANING_STREAM_CLASS_ID
:
1694 notit
->cur_stream_class_id
= value
;
1696 case CTF_FIELD_CLASS_MEANING_MAGIC
:
1697 if (value
!= 0xc1fc1fc1) {
1698 BT_LOGW("Invalid CTF magic number: notit-addr=%p, "
1699 "magic=%" PRIx64
, notit
, value
);
1700 status
= BT_BFCR_STATUS_ERROR
;
1705 case CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT
:
1706 notit
->snapshots
.packets
= value
;
1708 case CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT
:
1709 notit
->snapshots
.discarded_events
= value
;
1711 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_TOTAL_SIZE
:
1712 notit
->cur_exp_packet_total_size
= value
;
1714 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_CONTENT_SIZE
:
1715 notit
->cur_exp_packet_content_size
= value
;
1722 if (unlikely(int_fc
->mapped_clock_class
)) {
1723 update_default_clock(notit
, value
, int_fc
->base
.size
);
1726 if (unlikely(int_fc
->storing_index
>= 0)) {
1727 g_array_index(notit
->stored_values
, uint64_t,
1728 (uint64_t) int_fc
->storing_index
) = value
;
1731 if (unlikely(!fc
->in_ir
)) {
1735 field
= borrow_next_field(notit
);
1737 BT_ASSERT(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
1738 BT_ASSERT(bt_field_get_class_type(field
) ==
1739 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
||
1740 bt_field_get_class_type(field
) ==
1741 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
);
1742 bt_field_unsigned_integer_set_value(field
, value
);
1743 stack_top(notit
->stack
)->index
++;
1750 enum bt_bfcr_status
bfcr_unsigned_int_char_cb(uint64_t value
,
1751 struct ctf_field_class
*fc
, void *data
)
1754 struct bt_msg_iter
*notit
= data
;
1755 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
1756 bt_field
*string_field
= NULL
;
1757 struct ctf_field_class_int
*int_fc
= (void *) fc
;
1758 char str
[2] = {'\0', '\0'};
1760 BT_LOGV("Unsigned integer character function called from BFCR: "
1761 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1762 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64
,
1763 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
1764 BT_ASSERT(int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
);
1765 BT_ASSERT(!int_fc
->mapped_clock_class
);
1766 BT_ASSERT(int_fc
->storing_index
< 0);
1768 if (unlikely(!fc
->in_ir
)) {
1772 if (notit
->done_filling_string
) {
1777 notit
->done_filling_string
= true;
1781 string_field
= stack_top(notit
->stack
)->base
;
1782 BT_ASSERT(bt_field_get_class_type(string_field
) ==
1783 BT_FIELD_CLASS_TYPE_STRING
);
1785 /* Append character */
1786 str
[0] = (char) value
;
1787 ret
= bt_field_string_append_with_length(string_field
, str
, 1);
1789 BT_LOGE("Cannot append character to string field's value: "
1790 "notit-addr=%p, field-addr=%p, ret=%d",
1791 notit
, string_field
, ret
);
1792 status
= BT_BFCR_STATUS_ERROR
;
1801 enum bt_bfcr_status
bfcr_signed_int_cb(int64_t value
,
1802 struct ctf_field_class
*fc
, void *data
)
1804 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
1805 bt_field
*field
= NULL
;
1806 struct bt_msg_iter
*notit
= data
;
1807 struct ctf_field_class_int
*int_fc
= (void *) fc
;
1809 BT_LOGV("Signed integer function called from BFCR: "
1810 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1811 "fc-type=%d, fc-in-ir=%d, value=%" PRId64
,
1812 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
1813 BT_ASSERT(int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
);
1815 if (unlikely(int_fc
->storing_index
>= 0)) {
1816 g_array_index(notit
->stored_values
, uint64_t,
1817 (uint64_t) int_fc
->storing_index
) = (uint64_t) value
;
1820 if (unlikely(!fc
->in_ir
)) {
1824 field
= borrow_next_field(notit
);
1826 BT_ASSERT(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
1827 BT_ASSERT(bt_field_get_class_type(field
) ==
1828 BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
||
1829 bt_field_get_class_type(field
) ==
1830 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
);
1831 bt_field_signed_integer_set_value(field
, value
);
1832 stack_top(notit
->stack
)->index
++;
1839 enum bt_bfcr_status
bfcr_floating_point_cb(double value
,
1840 struct ctf_field_class
*fc
, void *data
)
1842 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
1843 bt_field
*field
= NULL
;
1844 struct bt_msg_iter
*notit
= data
;
1846 BT_LOGV("Floating point number function called from BFCR: "
1847 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1848 "fc-type=%d, fc-in-ir=%d, value=%f",
1849 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
1851 if (unlikely(!fc
->in_ir
)) {
1855 field
= borrow_next_field(notit
);
1857 BT_ASSERT(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
1858 BT_ASSERT(bt_field_get_class_type(field
) ==
1859 BT_FIELD_CLASS_TYPE_REAL
);
1860 bt_field_real_set_value(field
, value
);
1861 stack_top(notit
->stack
)->index
++;
1868 enum bt_bfcr_status
bfcr_string_begin_cb(
1869 struct ctf_field_class
*fc
, void *data
)
1871 bt_field
*field
= NULL
;
1872 struct bt_msg_iter
*notit
= data
;
1875 BT_LOGV("String (beginning) function called from BFCR: "
1876 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1877 "fc-type=%d, fc-in-ir=%d",
1878 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
1880 if (unlikely(!fc
->in_ir
)) {
1884 field
= borrow_next_field(notit
);
1886 BT_ASSERT(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
1887 BT_ASSERT(bt_field_get_class_type(field
) ==
1888 BT_FIELD_CLASS_TYPE_STRING
);
1889 ret
= bt_field_string_clear(field
);
1890 BT_ASSERT(ret
== 0);
1893 * Push on stack. Not a compound class per se, but we know that
1894 * only bfcr_string_cb() may be called between this call and a
1895 * subsequent call to bfcr_string_end_cb().
1897 stack_push(notit
->stack
, field
);
1900 return BT_BFCR_STATUS_OK
;
1904 enum bt_bfcr_status
bfcr_string_cb(const char *value
,
1905 size_t len
, struct ctf_field_class
*fc
, void *data
)
1907 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
1908 bt_field
*field
= NULL
;
1909 struct bt_msg_iter
*notit
= data
;
1912 BT_LOGV("String (substring) function called from BFCR: "
1913 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1914 "fc-type=%d, fc-in-ir=%d, string-length=%zu",
1915 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
,
1918 if (unlikely(!fc
->in_ir
)) {
1922 field
= stack_top(notit
->stack
)->base
;
1925 /* Append current substring */
1926 ret
= bt_field_string_append_with_length(field
, value
, len
);
1928 BT_LOGE("Cannot append substring to string field's value: "
1929 "notit-addr=%p, field-addr=%p, string-length=%zu, "
1930 "ret=%d", notit
, field
, len
, ret
);
1931 status
= BT_BFCR_STATUS_ERROR
;
1940 enum bt_bfcr_status
bfcr_string_end_cb(
1941 struct ctf_field_class
*fc
, void *data
)
1943 struct bt_msg_iter
*notit
= data
;
1945 BT_LOGV("String (end) function called from BFCR: "
1946 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1947 "fc-type=%d, fc-in-ir=%d",
1948 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
1950 if (unlikely(!fc
->in_ir
)) {
1954 /* Pop string field */
1955 stack_pop(notit
->stack
);
1957 /* Go to next field */
1958 stack_top(notit
->stack
)->index
++;
1961 return BT_BFCR_STATUS_OK
;
1964 enum bt_bfcr_status
bfcr_compound_begin_cb(
1965 struct ctf_field_class
*fc
, void *data
)
1967 struct bt_msg_iter
*notit
= data
;
1970 BT_LOGV("Compound (beginning) function called from BFCR: "
1971 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1972 "fc-type=%d, fc-in-ir=%d",
1973 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
1980 if (stack_empty(notit
->stack
)) {
1981 /* Root: already set by read_dscope_begin_state() */
1982 field
= notit
->cur_dscope_field
;
1984 field
= borrow_next_field(notit
);
1990 BT_ASSERT(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
1991 stack_push(notit
->stack
, field
);
1994 * Change BFCR "unsigned int" callback if it's a text
1997 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
||
1998 fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
1999 struct ctf_field_class_array_base
*array_fc
= (void *) fc
;
2001 if (array_fc
->is_text
) {
2004 BT_ASSERT(bt_field_get_class_type(field
) ==
2005 BT_FIELD_CLASS_TYPE_STRING
);
2006 notit
->done_filling_string
= false;
2007 ret
= bt_field_string_clear(field
);
2008 BT_ASSERT(ret
== 0);
2009 bt_bfcr_set_unsigned_int_cb(notit
->bfcr
,
2010 bfcr_unsigned_int_char_cb
);
2015 return BT_BFCR_STATUS_OK
;
2018 enum bt_bfcr_status
bfcr_compound_end_cb(
2019 struct ctf_field_class
*fc
, void *data
)
2021 struct bt_msg_iter
*notit
= data
;
2023 BT_LOGV("Compound (end) function called from BFCR: "
2024 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2025 "fc-type=%d, fc-in-ir=%d",
2026 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
2032 BT_ASSERT(!stack_empty(notit
->stack
));
2033 BT_ASSERT(bt_field_borrow_class_const(stack_top(notit
->stack
)->base
) ==
2037 * Reset BFCR "unsigned int" callback if it's a text
2040 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
||
2041 fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
2042 struct ctf_field_class_array_base
*array_fc
= (void *) fc
;
2044 if (array_fc
->is_text
) {
2045 BT_ASSERT(bt_field_get_class_type(
2046 stack_top(notit
->stack
)->base
) ==
2047 BT_FIELD_CLASS_TYPE_STRING
);
2048 bt_bfcr_set_unsigned_int_cb(notit
->bfcr
,
2049 bfcr_unsigned_int_cb
);
2054 stack_pop(notit
->stack
);
2056 /* If the stack is not empty, increment the base's index */
2057 if (!stack_empty(notit
->stack
)) {
2058 stack_top(notit
->stack
)->index
++;
2062 return BT_BFCR_STATUS_OK
;
2066 int64_t bfcr_get_sequence_length_cb(struct ctf_field_class
*fc
, void *data
)
2068 bt_field
*seq_field
;
2069 struct bt_msg_iter
*notit
= data
;
2070 struct ctf_field_class_sequence
*seq_fc
= (void *) fc
;
2071 int64_t length
= -1;
2074 length
= (uint64_t) g_array_index(notit
->stored_values
, uint64_t,
2075 seq_fc
->stored_length_index
);
2076 seq_field
= stack_top(notit
->stack
)->base
;
2077 BT_ASSERT(seq_field
);
2078 ret
= bt_field_dynamic_array_set_length(seq_field
, (uint64_t) length
);
2080 BT_LOGE("Cannot set dynamic array field's length field: "
2081 "notit-addr=%p, field-addr=%p, "
2082 "length=%" PRIu64
, notit
, seq_field
, length
);
2089 struct ctf_field_class
*bfcr_borrow_variant_selected_field_class_cb(
2090 struct ctf_field_class
*fc
, void *data
)
2094 int64_t option_index
= -1;
2095 struct bt_msg_iter
*notit
= data
;
2096 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
2097 struct ctf_named_field_class
*selected_option
= NULL
;
2098 struct ctf_field_class
*ret_fc
= NULL
;
2104 /* Get variant's tag */
2105 tag
.u
= g_array_index(notit
->stored_values
, uint64_t,
2106 var_fc
->stored_tag_index
);
2109 * Check each range to find the selected option's index.
2111 if (var_fc
->tag_fc
->base
.is_signed
) {
2112 for (i
= 0; i
< var_fc
->ranges
->len
; i
++) {
2113 struct ctf_field_class_variant_range
*range
=
2114 ctf_field_class_variant_borrow_range_by_index(
2117 if (tag
.i
>= range
->range
.lower
.i
&&
2118 tag
.i
<= range
->range
.upper
.i
) {
2119 option_index
= (int64_t) range
->option_index
;
2124 for (i
= 0; i
< var_fc
->ranges
->len
; i
++) {
2125 struct ctf_field_class_variant_range
*range
=
2126 ctf_field_class_variant_borrow_range_by_index(
2129 if (tag
.u
>= range
->range
.lower
.u
&&
2130 tag
.u
<= range
->range
.upper
.u
) {
2131 option_index
= (int64_t) range
->option_index
;
2137 if (option_index
< 0) {
2138 BT_LOGW("Cannot find variant field class's option: "
2139 "notit-addr=%p, var-fc-addr=%p, u-tag=%" PRIu64
", "
2140 "i-tag=%" PRId64
, notit
, var_fc
, tag
.u
, tag
.i
);
2144 selected_option
= ctf_field_class_variant_borrow_option_by_index(
2145 var_fc
, (uint64_t) option_index
);
2147 if (selected_option
->fc
->in_ir
) {
2148 bt_field
*var_field
= stack_top(notit
->stack
)->base
;
2150 ret
= bt_field_variant_select_option_field(
2151 var_field
, option_index
);
2153 BT_LOGW("Cannot select variant field's option field: "
2154 "notit-addr=%p, var-field-addr=%p, "
2155 "opt-index=%" PRId64
, notit
, var_field
,
2161 ret_fc
= selected_option
->fc
;
2168 void set_event_default_clock_snapshot(struct bt_msg_iter
*notit
)
2171 bt_message_event_borrow_event(
2173 bt_stream_class
*sc
= notit
->meta
.sc
->ir_sc
;
2177 if (bt_stream_class_borrow_default_clock_class(sc
)) {
2178 bt_event_set_default_clock_snapshot(event
,
2179 notit
->default_clock_snapshot
);
2184 void notify_new_stream(struct bt_msg_iter
*notit
,
2185 bt_message
**message
)
2187 enum bt_msg_iter_status status
;
2188 bt_message
*ret
= NULL
;
2190 status
= set_current_stream(notit
);
2191 if (status
!= BT_MSG_ITER_STATUS_OK
) {
2192 BT_MESSAGE_PUT_REF_AND_RESET(ret
);
2196 BT_ASSERT(notit
->stream
);
2197 BT_ASSERT(notit
->msg_iter
);
2198 ret
= bt_message_stream_beginning_create(notit
->msg_iter
,
2201 BT_LOGE("Cannot create stream beginning message: "
2202 "notit-addr=%p, stream-addr=%p",
2203 notit
, notit
->stream
);
2212 void notify_end_of_stream(struct bt_msg_iter
*notit
,
2213 bt_message
**message
)
2217 if (!notit
->stream
) {
2218 BT_LOGE("Cannot create stream for stream message: "
2219 "notit-addr=%p", notit
);
2223 BT_ASSERT(notit
->msg_iter
);
2224 ret
= bt_message_stream_end_create(notit
->msg_iter
,
2227 BT_LOGE("Cannot create stream beginning message: "
2228 "notit-addr=%p, stream-addr=%p",
2229 notit
, notit
->stream
);
2236 void notify_new_packet(struct bt_msg_iter
*notit
,
2237 bt_message
**message
)
2240 enum bt_msg_iter_status status
;
2241 bt_message
*msg
= NULL
;
2242 const bt_stream_class
*sc
;
2244 status
= set_current_packet(notit
);
2245 if (status
!= BT_MSG_ITER_STATUS_OK
) {
2249 BT_ASSERT(notit
->packet
);
2250 sc
= notit
->meta
.sc
->ir_sc
;
2253 if (bt_stream_class_packets_have_discarded_event_counter_snapshot(sc
)) {
2254 BT_ASSERT(notit
->snapshots
.discarded_events
!= UINT64_C(-1));
2255 bt_packet_set_discarded_event_counter_snapshot(
2256 notit
->packet
, notit
->snapshots
.discarded_events
);
2259 if (bt_stream_class_packets_have_packet_counter_snapshot(sc
)) {
2260 BT_ASSERT(notit
->snapshots
.packets
!= UINT64_C(-1));
2261 bt_packet_set_packet_counter_snapshot(
2262 notit
->packet
, notit
->snapshots
.packets
);
2265 if (bt_stream_class_packets_have_default_beginning_clock_snapshot(sc
)) {
2266 BT_ASSERT(notit
->snapshots
.beginning_clock
!= UINT64_C(-1));
2267 bt_packet_set_default_beginning_clock_snapshot(
2268 notit
->packet
, notit
->snapshots
.beginning_clock
);
2271 if (bt_stream_class_packets_have_default_end_clock_snapshot(sc
)) {
2272 BT_ASSERT(notit
->snapshots
.end_clock
!= UINT64_C(-1));
2273 bt_packet_set_default_end_clock_snapshot(
2274 notit
->packet
, notit
->snapshots
.end_clock
);
2277 if (notit
->packet_context_field
) {
2278 ret
= bt_packet_move_context_field(
2279 notit
->packet
, notit
->packet_context_field
);
2284 notit
->packet_context_field
= NULL
;
2287 * At this point notit->dscopes.stream_packet_context
2288 * has the same value as the packet context field within
2291 BT_ASSERT(bt_packet_borrow_context_field(
2293 notit
->dscopes
.stream_packet_context
);
2296 BT_ASSERT(notit
->msg_iter
);
2297 msg
= bt_message_packet_beginning_create(notit
->msg_iter
,
2300 BT_LOGE("Cannot create packet beginning message: "
2301 "notit-addr=%p, packet-addr=%p",
2302 notit
, notit
->packet
);
2313 void notify_end_of_packet(struct bt_msg_iter
*notit
,
2314 bt_message
**message
)
2318 if (!notit
->packet
) {
2322 /* Update default clock from packet's end time */
2323 if (notit
->snapshots
.end_clock
!= UINT64_C(-1)) {
2324 notit
->default_clock_snapshot
= notit
->snapshots
.end_clock
;
2327 BT_ASSERT(notit
->msg_iter
);
2328 msg
= bt_message_packet_end_create(notit
->msg_iter
,
2331 BT_LOGE("Cannot create packet end message: "
2332 "notit-addr=%p, packet-addr=%p",
2333 notit
, notit
->packet
);
2338 BT_PACKET_PUT_REF_AND_RESET(notit
->packet
);
2343 struct bt_msg_iter
*bt_msg_iter_create(struct ctf_trace_class
*tc
,
2344 size_t max_request_sz
,
2345 struct bt_msg_iter_medium_ops medops
, void *data
)
2347 struct bt_msg_iter
*notit
= NULL
;
2348 struct bt_bfcr_cbs cbs
= {
2350 .signed_int
= bfcr_signed_int_cb
,
2351 .unsigned_int
= bfcr_unsigned_int_cb
,
2352 .floating_point
= bfcr_floating_point_cb
,
2353 .string_begin
= bfcr_string_begin_cb
,
2354 .string
= bfcr_string_cb
,
2355 .string_end
= bfcr_string_end_cb
,
2356 .compound_begin
= bfcr_compound_begin_cb
,
2357 .compound_end
= bfcr_compound_end_cb
,
2360 .get_sequence_length
= bfcr_get_sequence_length_cb
,
2361 .borrow_variant_selected_field_class
= bfcr_borrow_variant_selected_field_class_cb
,
2366 BT_ASSERT(medops
.request_bytes
);
2367 BT_ASSERT(medops
.borrow_stream
);
2368 BT_LOGD("Creating CTF plugin message iterator: "
2369 "trace-addr=%p, max-request-size=%zu, "
2370 "data=%p", tc
, max_request_sz
, data
);
2371 notit
= g_new0(struct bt_msg_iter
, 1);
2373 BT_LOGE_STR("Failed to allocate one CTF plugin message iterator.");
2376 notit
->meta
.tc
= tc
;
2377 notit
->medium
.medops
= medops
;
2378 notit
->medium
.max_request_sz
= max_request_sz
;
2379 notit
->medium
.data
= data
;
2380 notit
->stack
= stack_new(notit
);
2381 notit
->stored_values
= g_array_new(FALSE
, TRUE
, sizeof(uint64_t));
2382 g_array_set_size(notit
->stored_values
, tc
->stored_value_count
);
2384 if (!notit
->stack
) {
2385 BT_LOGE_STR("Failed to create field stack.");
2389 notit
->bfcr
= bt_bfcr_create(cbs
, notit
);
2391 BT_LOGE_STR("Failed to create binary class reader (BFCR).");
2395 bt_msg_iter_reset(notit
);
2396 BT_LOGD("Created CTF plugin message iterator: "
2397 "trace-addr=%p, max-request-size=%zu, "
2398 "data=%p, notit-addr=%p",
2399 tc
, max_request_sz
, data
, notit
);
2400 notit
->cur_packet_offset
= 0;
2406 bt_msg_iter_destroy(notit
);
2411 void bt_msg_iter_destroy(struct bt_msg_iter
*notit
)
2413 BT_PACKET_PUT_REF_AND_RESET(notit
->packet
);
2414 BT_STREAM_PUT_REF_AND_RESET(notit
->stream
);
2415 release_all_dscopes(notit
);
2417 BT_LOGD("Destroying CTF plugin message iterator: addr=%p", notit
);
2420 BT_LOGD_STR("Destroying field stack.");
2421 stack_destroy(notit
->stack
);
2425 BT_LOGD("Destroying BFCR: bfcr-addr=%p", notit
->bfcr
);
2426 bt_bfcr_destroy(notit
->bfcr
);
2429 if (notit
->stored_values
) {
2430 g_array_free(notit
->stored_values
, TRUE
);
2436 enum bt_msg_iter_status
bt_msg_iter_get_next_message(
2437 struct bt_msg_iter
*notit
,
2438 bt_self_message_iterator
*msg_iter
,
2439 bt_message
**message
)
2441 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
2446 if (notit
->state
== STATE_DONE
) {
2447 status
= BT_MSG_ITER_STATUS_EOF
;
2451 notit
->msg_iter
= msg_iter
;
2453 BT_LOGV("Getting next message: notit-addr=%p", notit
);
2456 status
= handle_state(notit
);
2457 if (status
== BT_MSG_ITER_STATUS_AGAIN
) {
2458 BT_LOGV_STR("Medium returned BT_MSG_ITER_STATUS_AGAIN.");
2462 if (status
!= BT_MSG_ITER_STATUS_OK
) {
2463 if (status
== BT_MSG_ITER_STATUS_EOF
) {
2464 enum state next_state
= notit
->state
;
2466 BT_LOGV_STR("Medium returned BT_MSG_ITER_STATUS_EOF.");
2468 if (notit
->packet
) {
2469 notify_end_of_packet(notit
,
2472 notify_end_of_stream(notit
,
2474 next_state
= STATE_DONE
;
2478 status
= BT_MSG_ITER_STATUS_ERROR
;
2482 status
= BT_MSG_ITER_STATUS_OK
;
2483 notit
->state
= next_state
;
2485 BT_LOGW("Cannot handle state: "
2486 "notit-addr=%p, state=%s",
2487 notit
, state_string(notit
->state
));
2493 switch (notit
->state
) {
2494 case STATE_EMIT_MSG_NEW_STREAM
:
2495 /* notify_new_stream() logs errors */
2496 notify_new_stream(notit
, message
);
2499 status
= BT_MSG_ITER_STATUS_ERROR
;
2502 notit
->stream_begin_emitted
= true;
2504 case STATE_EMIT_MSG_NEW_PACKET
:
2505 /* notify_new_packet() logs errors */
2506 notify_new_packet(notit
, message
);
2509 status
= BT_MSG_ITER_STATUS_ERROR
;
2513 case STATE_EMIT_MSG_EVENT
:
2514 BT_ASSERT(notit
->event_msg
);
2515 set_event_default_clock_snapshot(notit
);
2516 *message
= notit
->event_msg
;
2517 notit
->event_msg
= NULL
;
2519 case STATE_EMIT_MSG_END_OF_PACKET
:
2520 /* notify_end_of_packet() logs errors */
2521 notify_end_of_packet(notit
, message
);
2524 status
= BT_MSG_ITER_STATUS_ERROR
;
2529 /* Non-emitting state: continue */
2539 enum bt_msg_iter_status
read_packet_header_context_fields(
2540 struct bt_msg_iter
*notit
)
2543 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
2547 if (notit
->state
== STATE_EMIT_MSG_NEW_PACKET
) {
2548 /* We're already there */
2553 status
= handle_state(notit
);
2554 if (status
== BT_MSG_ITER_STATUS_AGAIN
) {
2555 BT_LOGV_STR("Medium returned BT_MSG_ITER_STATUS_AGAIN.");
2558 if (status
!= BT_MSG_ITER_STATUS_OK
) {
2559 if (status
== BT_MSG_ITER_STATUS_EOF
) {
2560 BT_LOGV_STR("Medium returned BT_MSG_ITER_STATUS_EOF.");
2562 BT_LOGW("Cannot handle state: "
2563 "notit-addr=%p, state=%s",
2564 notit
, state_string(notit
->state
));
2569 switch (notit
->state
) {
2570 case STATE_EMIT_MSG_NEW_PACKET
:
2572 * Packet header and context fields are
2573 * potentially decoded (or they don't exist).
2577 case STATE_EMIT_MSG_NEW_STREAM
:
2578 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
2579 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
2580 case STATE_AFTER_TRACE_PACKET_HEADER
:
2581 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
2582 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
2583 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
2584 /* Non-emitting state: continue */
2588 * We should never get past the
2589 * STATE_EMIT_MSG_NEW_PACKET state.
2591 BT_LOGF("Unexpected state: notit-addr=%p, state=%s",
2592 notit
, state_string(notit
->state
));
2598 ret
= set_current_packet_content_sizes(notit
);
2600 status
= BT_MSG_ITER_STATUS_ERROR
;
2608 void bt_msg_iter_set_medops_data(struct bt_msg_iter
*notit
,
2612 notit
->medium
.data
= medops_data
;
2616 enum bt_msg_iter_status
bt_msg_iter_seek(
2617 struct bt_msg_iter
*notit
, off_t offset
)
2619 enum bt_msg_iter_status ret
= BT_MSG_ITER_STATUS_OK
;
2620 enum bt_msg_iter_medium_status medium_status
;
2624 BT_LOGE("Cannot seek to negative offset: offset=%jd", offset
);
2625 ret
= BT_MSG_ITER_STATUS_INVAL
;
2629 if (!notit
->medium
.medops
.seek
) {
2630 ret
= BT_MSG_ITER_STATUS_UNSUPPORTED
;
2631 BT_LOGD("Aborting seek as the iterator's underlying media does not implement seek support.");
2635 medium_status
= notit
->medium
.medops
.seek(
2636 BT_MSG_ITER_SEEK_WHENCE_SET
, offset
, notit
->medium
.data
);
2637 if (medium_status
!= BT_MSG_ITER_MEDIUM_STATUS_OK
) {
2638 if (medium_status
== BT_MSG_ITER_MEDIUM_STATUS_EOF
) {
2639 ret
= BT_MSG_ITER_STATUS_EOF
;
2641 ret
= BT_MSG_ITER_STATUS_ERROR
;
2646 bt_msg_iter_reset(notit
);
2647 notit
->cur_packet_offset
= offset
;
2654 off_t
bt_msg_iter_get_current_packet_offset(struct bt_msg_iter
*notit
)
2657 return notit
->cur_packet_offset
;
2661 off_t
bt_msg_iter_get_current_packet_size(
2662 struct bt_msg_iter
*notit
)
2665 return notit
->cur_exp_packet_total_size
;
2669 void bt_msg_trace_class_changed(struct bt_msg_iter
*notit
)
2671 if (notit
->meta
.tc
->stored_value_count
> notit
->stored_values
->len
) {
2672 g_array_set_size(notit
->stored_values
,
2673 notit
->meta
.tc
->stored_value_count
);
2678 enum bt_msg_iter_status
bt_msg_iter_get_packet_properties(
2679 struct bt_msg_iter
*notit
,
2680 struct bt_msg_iter_packet_properties
*props
)
2682 enum bt_msg_iter_status status
;
2686 status
= read_packet_header_context_fields(notit
);
2687 if (status
!= BT_MSG_ITER_STATUS_OK
) {
2691 props
->exp_packet_total_size
=
2692 (uint64_t) notit
->cur_exp_packet_total_size
;
2693 props
->exp_packet_content_size
=
2694 (uint64_t) notit
->cur_exp_packet_content_size
;
2695 BT_ASSERT(props
->stream_class_id
>= 0);
2696 props
->stream_class_id
= (uint64_t) notit
->cur_stream_class_id
;
2697 props
->data_stream_id
= notit
->cur_data_stream_id
;
2698 props
->snapshots
.discarded_events
= notit
->snapshots
.discarded_events
;
2699 props
->snapshots
.packets
= notit
->snapshots
.packets
;
2700 props
->snapshots
.beginning_clock
= notit
->snapshots
.beginning_clock
;
2701 props
->snapshots
.end_clock
= notit
->snapshots
.end_clock
;