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 header field wrapper (NULL if not created yet) */
133 bt_packet_header_field
*packet_header_field
;
135 /* Current packet header field wrapper (NULL if not created yet) */
136 bt_packet_context_field
*packet_context_field
;
138 /* Current event header field (NULL if not created yet) */
139 bt_event_header_field
*event_header_field
;
141 /* Current packet (NULL if not created yet) */
144 /* Current stream (NULL if not set yet) */
147 /* Current event (NULL if not created yet) */
150 /* Current event message (NULL if not created yet) */
151 bt_message
*event_msg
;
153 /* Database of current dynamic scopes */
155 bt_field
*trace_packet_header
;
156 bt_field
*stream_packet_context
;
157 bt_field
*event_header
;
158 bt_field
*event_common_context
;
159 bt_field
*event_spec_context
;
160 bt_field
*event_payload
;
166 /* Current medium buffer data */
168 /* Last address provided by medium */
171 /* Buffer size provided by medium (bytes) */
174 /* Offset within whole packet of addr (bits) */
175 size_t packet_offset
;
177 /* Current position from addr (bits) */
180 /* Position of the last event header from addr (bits) */
184 /* Binary type reader */
185 struct bt_bfcr
*bfcr
;
187 /* Current medium data */
189 struct bt_msg_iter_medium_ops medops
;
190 size_t max_request_sz
;
194 /* Stream beginning was emitted */
195 bool stream_begin_emitted
;
197 /* Current packet size (bits) (-1 if unknown) */
198 int64_t cur_exp_packet_total_size
;
200 /* Current content size (bits) (-1 if unknown) */
201 int64_t cur_exp_packet_content_size
;
203 /* Current stream class ID */
204 int64_t cur_stream_class_id
;
206 /* Current event class ID */
207 int64_t cur_event_class_id
;
209 /* Current data stream ID */
210 int64_t cur_data_stream_id
;
213 * Offset, in the underlying media, of the current packet's
214 * start (-1 if unknown).
216 off_t cur_packet_offset
;
218 /* Default clock's current value */
219 uint64_t default_clock_snapshot
;
221 /* End of packet snapshots */
223 uint64_t discarded_events
;
225 uint64_t beginning_clock
;
229 /* Stored values (for sequence lengths, variant tags) */
230 GArray
*stored_values
;
234 const char *state_string(enum state state
)
239 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
240 return "STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN";
241 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
242 return "STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE";
243 case STATE_AFTER_TRACE_PACKET_HEADER
:
244 return "STATE_AFTER_TRACE_PACKET_HEADER";
245 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
246 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN";
247 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
248 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE";
249 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
250 return "STATE_AFTER_STREAM_PACKET_CONTEXT";
251 case STATE_EMIT_MSG_NEW_PACKET
:
252 return "STATE_EMIT_MSG_NEW_PACKET";
253 case STATE_EMIT_MSG_NEW_STREAM
:
254 return "STATE_EMIT_MSG_NEW_STREAM";
255 case STATE_DSCOPE_EVENT_HEADER_BEGIN
:
256 return "STATE_DSCOPE_EVENT_HEADER_BEGIN";
257 case STATE_DSCOPE_EVENT_HEADER_CONTINUE
:
258 return "STATE_DSCOPE_EVENT_HEADER_CONTINUE";
259 case STATE_AFTER_EVENT_HEADER
:
260 return "STATE_AFTER_EVENT_HEADER";
261 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
:
262 return "STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN";
263 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
:
264 return "STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE";
265 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
:
266 return "STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN";
267 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
:
268 return "STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE";
269 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
270 return "STATE_DSCOPE_EVENT_PAYLOAD_BEGIN";
271 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
272 return "STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE";
273 case STATE_EMIT_MSG_EVENT
:
274 return "STATE_EMIT_MSG_EVENT";
275 case STATE_EMIT_MSG_END_OF_PACKET
:
276 return "STATE_EMIT_MSG_END_OF_PACKET";
279 case STATE_SKIP_PACKET_PADDING
:
280 return "STATE_SKIP_PACKET_PADDING";
287 int bt_msg_iter_switch_packet(struct bt_msg_iter
*notit
);
290 struct stack
*stack_new(struct bt_msg_iter
*notit
)
292 struct stack
*stack
= NULL
;
294 stack
= g_new0(struct stack
, 1);
296 BT_LOGE_STR("Failed to allocate one stack.");
300 stack
->entries
= g_array_new(FALSE
, TRUE
, sizeof(struct stack_entry
));
301 if (!stack
->entries
) {
302 BT_LOGE_STR("Failed to allocate a GArray.");
306 BT_LOGD("Created stack: notit-addr=%p, stack-addr=%p", notit
, stack
);
318 void stack_destroy(struct stack
*stack
)
321 BT_LOGD("Destroying stack: addr=%p", stack
);
323 if (stack
->entries
) {
324 g_array_free(stack
->entries
, TRUE
);
331 void stack_push(struct stack
*stack
, bt_field
*base
)
333 struct stack_entry
*entry
;
337 BT_LOGV("Pushing base field on stack: stack-addr=%p, "
338 "stack-size-before=%zu, stack-size-after=%zu",
339 stack
, stack
->size
, stack
->size
+ 1);
341 if (stack
->entries
->len
== stack
->size
) {
342 g_array_set_size(stack
->entries
, stack
->size
+ 1);
345 entry
= &g_array_index(stack
->entries
, struct stack_entry
, stack
->size
);
352 unsigned int stack_size(struct stack
*stack
)
359 void stack_pop(struct stack
*stack
)
362 BT_ASSERT(stack_size(stack
));
363 BT_LOGV("Popping from stack: "
364 "stack-addr=%p, stack-size-before=%zu, stack-size-after=%zu",
365 stack
, stack
->size
, stack
->size
- 1);
370 struct stack_entry
*stack_top(struct stack
*stack
)
373 BT_ASSERT(stack_size(stack
));
374 return &g_array_index(stack
->entries
, struct stack_entry
,
379 bool stack_empty(struct stack
*stack
)
381 return stack_size(stack
) == 0;
385 void stack_clear(struct stack
*stack
)
392 enum bt_msg_iter_status
msg_iter_status_from_m_status(
393 enum bt_msg_iter_medium_status m_status
)
395 /* They are the same */
396 return (int) m_status
;
400 size_t buf_size_bits(struct bt_msg_iter
*notit
)
402 return notit
->buf
.sz
* 8;
406 size_t buf_available_bits(struct bt_msg_iter
*notit
)
408 return buf_size_bits(notit
) - notit
->buf
.at
;
412 size_t packet_at(struct bt_msg_iter
*notit
)
414 return notit
->buf
.packet_offset
+ notit
->buf
.at
;
418 void buf_consume_bits(struct bt_msg_iter
*notit
, size_t incr
)
420 BT_LOGV("Advancing cursor: notit-addr=%p, cur-before=%zu, cur-after=%zu",
421 notit
, notit
->buf
.at
, notit
->buf
.at
+ incr
);
422 notit
->buf
.at
+= incr
;
426 enum bt_msg_iter_status
request_medium_bytes(
427 struct bt_msg_iter
*notit
)
429 uint8_t *buffer_addr
= NULL
;
430 size_t buffer_sz
= 0;
431 enum bt_msg_iter_medium_status m_status
;
433 BT_LOGV("Calling user function (request bytes): notit-addr=%p, "
434 "request-size=%zu", notit
, notit
->medium
.max_request_sz
);
435 m_status
= notit
->medium
.medops
.request_bytes(
436 notit
->medium
.max_request_sz
, &buffer_addr
,
437 &buffer_sz
, notit
->medium
.data
);
438 BT_LOGV("User function returned: status=%s, buf-addr=%p, buf-size=%zu",
439 bt_msg_iter_medium_status_string(m_status
),
440 buffer_addr
, buffer_sz
);
441 if (m_status
== BT_MSG_ITER_MEDIUM_STATUS_OK
) {
442 BT_ASSERT(buffer_sz
!= 0);
444 /* New packet offset is old one + old size (in bits) */
445 notit
->buf
.packet_offset
+= buf_size_bits(notit
);
447 /* Restart at the beginning of the new medium buffer */
449 notit
->buf
.last_eh_at
= SIZE_MAX
;
451 /* New medium buffer size */
452 notit
->buf
.sz
= buffer_sz
;
454 /* New medium buffer address */
455 notit
->buf
.addr
= buffer_addr
;
457 BT_LOGV("User function returned new bytes: "
458 "packet-offset=%zu, cur=%zu, size=%zu, addr=%p",
459 notit
->buf
.packet_offset
, notit
->buf
.at
,
460 notit
->buf
.sz
, notit
->buf
.addr
);
461 BT_LOGV_MEM(buffer_addr
, buffer_sz
, "Returned bytes at %p:",
463 } else if (m_status
== BT_MSG_ITER_MEDIUM_STATUS_EOF
) {
465 * User returned end of stream: validate that we're not
466 * in the middle of a packet header, packet context, or
469 if (notit
->cur_exp_packet_total_size
>= 0) {
470 if (packet_at(notit
) ==
471 notit
->cur_exp_packet_total_size
) {
475 if (packet_at(notit
) == 0) {
479 if (notit
->buf
.last_eh_at
!= SIZE_MAX
&&
480 notit
->buf
.at
== notit
->buf
.last_eh_at
) {
485 /* All other states are invalid */
486 BT_LOGW("User function returned %s, but message iterator is in an unexpected state: "
487 "state=%s, cur-packet-size=%" PRId64
", cur=%zu, "
488 "packet-cur=%zu, last-eh-at=%zu",
489 bt_msg_iter_medium_status_string(m_status
),
490 state_string(notit
->state
),
491 notit
->cur_exp_packet_total_size
,
492 notit
->buf
.at
, packet_at(notit
),
493 notit
->buf
.last_eh_at
);
494 m_status
= BT_MSG_ITER_MEDIUM_STATUS_ERROR
;
495 } else if (m_status
< 0) {
496 BT_LOGW("User function failed: status=%s",
497 bt_msg_iter_medium_status_string(m_status
));
501 return msg_iter_status_from_m_status(m_status
);
505 enum bt_msg_iter_status
buf_ensure_available_bits(
506 struct bt_msg_iter
*notit
)
508 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
510 if (unlikely(buf_available_bits(notit
) == 0)) {
512 * This _cannot_ return BT_MSG_ITER_STATUS_OK
515 status
= request_medium_bytes(notit
);
522 enum bt_msg_iter_status
read_dscope_begin_state(
523 struct bt_msg_iter
*notit
,
524 struct ctf_field_class
*dscope_fc
,
525 enum state done_state
, enum state continue_state
,
526 bt_field
*dscope_field
)
528 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
529 enum bt_bfcr_status bfcr_status
;
530 size_t consumed_bits
;
532 notit
->cur_dscope_field
= dscope_field
;
533 BT_LOGV("Starting BFCR: notit-addr=%p, bfcr-addr=%p, fc-addr=%p",
534 notit
, notit
->bfcr
, dscope_fc
);
535 consumed_bits
= bt_bfcr_start(notit
->bfcr
, dscope_fc
,
536 notit
->buf
.addr
, notit
->buf
.at
, packet_at(notit
),
537 notit
->buf
.sz
, &bfcr_status
);
538 BT_LOGV("BFCR consumed bits: size=%zu", consumed_bits
);
540 switch (bfcr_status
) {
541 case BT_BFCR_STATUS_OK
:
542 /* Field class was read completely */
543 BT_LOGV_STR("Field was completely decoded.");
544 notit
->state
= done_state
;
546 case BT_BFCR_STATUS_EOF
:
547 BT_LOGV_STR("BFCR needs more data to decode field completely.");
548 notit
->state
= continue_state
;
551 BT_LOGW("BFCR failed to start: notit-addr=%p, bfcr-addr=%p, "
552 "status=%s", notit
, notit
->bfcr
,
553 bt_bfcr_status_string(bfcr_status
));
554 status
= BT_MSG_ITER_STATUS_ERROR
;
558 /* Consume bits now since we know we're not in an error state */
559 buf_consume_bits(notit
, consumed_bits
);
566 enum bt_msg_iter_status
read_dscope_continue_state(
567 struct bt_msg_iter
*notit
, enum state done_state
)
569 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
570 enum bt_bfcr_status bfcr_status
;
571 size_t consumed_bits
;
573 BT_LOGV("Continuing BFCR: notit-addr=%p, bfcr-addr=%p",
576 status
= buf_ensure_available_bits(notit
);
577 if (status
!= BT_MSG_ITER_STATUS_OK
) {
579 BT_LOGW("Cannot ensure that buffer has at least one byte: "
580 "msg-addr=%p, status=%s",
581 notit
, bt_msg_iter_status_string(status
));
583 BT_LOGV("Cannot ensure that buffer has at least one byte: "
584 "msg-addr=%p, status=%s",
585 notit
, bt_msg_iter_status_string(status
));
591 consumed_bits
= bt_bfcr_continue(notit
->bfcr
, notit
->buf
.addr
,
592 notit
->buf
.sz
, &bfcr_status
);
593 BT_LOGV("BFCR consumed bits: size=%zu", consumed_bits
);
595 switch (bfcr_status
) {
596 case BT_BFCR_STATUS_OK
:
597 /* Type was read completely. */
598 BT_LOGV_STR("Field was completely decoded.");
599 notit
->state
= done_state
;
601 case BT_BFCR_STATUS_EOF
:
602 /* Stay in this continue state. */
603 BT_LOGV_STR("BFCR needs more data to decode field completely.");
606 BT_LOGW("BFCR failed to continue: notit-addr=%p, bfcr-addr=%p, "
607 "status=%s", notit
, notit
->bfcr
,
608 bt_bfcr_status_string(bfcr_status
));
609 status
= BT_MSG_ITER_STATUS_ERROR
;
613 /* Consume bits now since we know we're not in an error state. */
614 buf_consume_bits(notit
, consumed_bits
);
620 void release_event_dscopes(struct bt_msg_iter
*notit
)
622 notit
->dscopes
.event_header
= NULL
;
624 if (notit
->event_header_field
) {
625 bt_event_header_field_release(notit
->event_header_field
);
626 notit
->event_header_field
= NULL
;
629 notit
->dscopes
.event_common_context
= NULL
;
630 notit
->dscopes
.event_spec_context
= NULL
;
631 notit
->dscopes
.event_payload
= NULL
;
635 void release_all_dscopes(struct bt_msg_iter
*notit
)
637 notit
->dscopes
.trace_packet_header
= NULL
;
639 if (notit
->packet_header_field
) {
640 bt_packet_header_field_release(notit
->packet_header_field
);
641 notit
->packet_header_field
= NULL
;
644 notit
->dscopes
.stream_packet_context
= NULL
;
646 if (notit
->packet_context_field
) {
647 bt_packet_context_field_release(notit
->packet_context_field
);
648 notit
->packet_context_field
= NULL
;
651 release_event_dscopes(notit
);
655 enum bt_msg_iter_status
read_packet_header_begin_state(
656 struct bt_msg_iter
*notit
)
658 struct ctf_field_class
*packet_header_fc
= NULL
;
659 enum bt_msg_iter_status ret
= BT_MSG_ITER_STATUS_OK
;
661 if (bt_msg_iter_switch_packet(notit
)) {
662 BT_LOGW("Cannot switch packet: notit-addr=%p", notit
);
663 ret
= BT_MSG_ITER_STATUS_ERROR
;
667 /* Packet header class is common to the whole trace class. */
668 packet_header_fc
= notit
->meta
.tc
->packet_header_fc
;
669 if (!packet_header_fc
) {
670 notit
->state
= STATE_AFTER_TRACE_PACKET_HEADER
;
674 BT_ASSERT(!notit
->packet_header_field
);
676 if (packet_header_fc
->in_ir
) {
678 * Create free packet header field from trace class.
679 * This field is going to be moved to the packet once we
680 * create it. We cannot create the packet now because:
682 * 1. A packet is created from a stream.
683 * 2. A stream is created from a stream class.
684 * 3. We need the packet header field's content to know
685 * the ID of the stream class to select.
687 notit
->packet_header_field
=
688 bt_packet_header_field_create(
689 notit
->meta
.tc
->ir_tc
);
690 if (!notit
->packet_header_field
) {
691 BT_LOGE_STR("Cannot create packet header field wrapper from trace class.");
692 ret
= BT_MSG_ITER_STATUS_ERROR
;
696 notit
->dscopes
.trace_packet_header
=
697 bt_packet_header_field_borrow_field(
698 notit
->packet_header_field
);
699 BT_ASSERT(notit
->dscopes
.trace_packet_header
);
702 notit
->cur_stream_class_id
= -1;
703 notit
->cur_event_class_id
= -1;
704 notit
->cur_data_stream_id
= -1;
705 BT_LOGV("Decoding packet header field:"
706 "notit-addr=%p, trace-class-addr=%p, fc-addr=%p",
707 notit
, notit
->meta
.tc
, packet_header_fc
);
708 ret
= read_dscope_begin_state(notit
, packet_header_fc
,
709 STATE_AFTER_TRACE_PACKET_HEADER
,
710 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
711 notit
->dscopes
.trace_packet_header
);
713 BT_LOGW("Cannot decode packet header field: "
714 "notit-addr=%p, trace-class-addr=%p, "
716 notit
, notit
->meta
.tc
, packet_header_fc
);
724 enum bt_msg_iter_status
read_packet_header_continue_state(
725 struct bt_msg_iter
*notit
)
727 return read_dscope_continue_state(notit
,
728 STATE_AFTER_TRACE_PACKET_HEADER
);
732 enum bt_msg_iter_status
set_current_stream_class(struct bt_msg_iter
*notit
)
734 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
735 struct ctf_stream_class
*new_stream_class
= NULL
;
737 if (notit
->cur_stream_class_id
== -1) {
739 * No current stream class ID field, therefore only one
742 if (notit
->meta
.tc
->stream_classes
->len
!= 1) {
743 BT_LOGW("Need exactly one stream class since there's "
744 "no stream class ID field: "
745 "notit-addr=%p", notit
);
746 status
= BT_MSG_ITER_STATUS_ERROR
;
750 new_stream_class
= notit
->meta
.tc
->stream_classes
->pdata
[0];
751 notit
->cur_stream_class_id
= new_stream_class
->id
;
754 new_stream_class
= ctf_trace_class_borrow_stream_class_by_id(
755 notit
->meta
.tc
, notit
->cur_stream_class_id
);
756 if (!new_stream_class
) {
757 BT_LOGW("No stream class with ID of stream class ID to use in trace class: "
758 "notit-addr=%p, stream-class-id=%" PRIu64
", "
759 "trace-class-addr=%p",
760 notit
, notit
->cur_stream_class_id
, notit
->meta
.tc
);
761 status
= BT_MSG_ITER_STATUS_ERROR
;
765 if (notit
->meta
.sc
) {
766 if (new_stream_class
!= notit
->meta
.sc
) {
767 BT_LOGW("Two packets refer to two different stream classes within the same packet sequence: "
768 "notit-addr=%p, prev-stream-class-addr=%p, "
769 "prev-stream-class-id=%" PRId64
", "
770 "next-stream-class-addr=%p, "
771 "next-stream-class-id=%" PRId64
", "
773 notit
, notit
->meta
.sc
,
776 new_stream_class
->id
,
778 status
= BT_MSG_ITER_STATUS_ERROR
;
782 notit
->meta
.sc
= new_stream_class
;
785 BT_LOGV("Set current stream class: "
786 "notit-addr=%p, stream-class-addr=%p, "
787 "stream-class-id=%" PRId64
,
788 notit
, notit
->meta
.sc
, notit
->meta
.sc
->id
);
795 enum bt_msg_iter_status
set_current_stream(struct bt_msg_iter
*notit
)
797 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
798 bt_stream
*stream
= NULL
;
800 BT_LOGV("Calling user function (get stream): notit-addr=%p, "
801 "stream-class-addr=%p, stream-class-id=%" PRId64
,
802 notit
, notit
->meta
.sc
,
804 stream
= notit
->medium
.medops
.borrow_stream(
805 notit
->meta
.sc
->ir_sc
, notit
->cur_data_stream_id
,
807 bt_stream_get_ref(stream
);
808 BT_LOGV("User function returned: stream-addr=%p", stream
);
810 BT_LOGW_STR("User function failed to return a stream object "
811 "for the given stream class.");
812 status
= BT_MSG_ITER_STATUS_ERROR
;
816 if (notit
->stream
&& stream
!= notit
->stream
) {
817 BT_LOGW("User function returned a different stream than the "
818 "previous one for the same sequence of packets.");
819 status
= BT_MSG_ITER_STATUS_ERROR
;
823 BT_STREAM_MOVE_REF(notit
->stream
, stream
);
826 bt_stream_put_ref(stream
);
831 enum bt_msg_iter_status
set_current_packet(struct bt_msg_iter
*notit
)
833 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
834 bt_packet
*packet
= NULL
;
836 BT_LOGV("Creating packet for packet message: "
837 "notit-addr=%p", notit
);
838 BT_LOGV("Creating packet from stream: "
839 "notit-addr=%p, stream-addr=%p, "
840 "stream-class-addr=%p, "
841 "stream-class-id=%" PRId64
,
842 notit
, notit
->stream
, notit
->meta
.sc
,
846 BT_ASSERT(notit
->stream
);
847 packet
= bt_packet_create(notit
->stream
);
849 BT_LOGE("Cannot create packet from stream: "
850 "notit-addr=%p, stream-addr=%p, "
851 "stream-class-addr=%p, "
852 "stream-class-id=%" PRId64
,
853 notit
, notit
->stream
, notit
->meta
.sc
,
861 BT_PACKET_PUT_REF_AND_RESET(packet
);
862 status
= BT_MSG_ITER_STATUS_ERROR
;
865 BT_PACKET_MOVE_REF(notit
->packet
, packet
);
870 enum bt_msg_iter_status
after_packet_header_state(
871 struct bt_msg_iter
*notit
)
873 enum bt_msg_iter_status status
;
875 status
= set_current_stream_class(notit
);
876 if (status
!= BT_MSG_ITER_STATUS_OK
) {
880 notit
->state
= STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
;
887 enum bt_msg_iter_status
read_packet_context_begin_state(
888 struct bt_msg_iter
*notit
)
890 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
891 struct ctf_field_class
*packet_context_fc
;
893 BT_ASSERT(notit
->meta
.sc
);
894 packet_context_fc
= notit
->meta
.sc
->packet_context_fc
;
895 if (!packet_context_fc
) {
896 BT_LOGV("No packet packet context field class in stream class: continuing: "
897 "notit-addr=%p, stream-class-addr=%p, "
898 "stream-class-id=%" PRId64
,
899 notit
, notit
->meta
.sc
,
901 notit
->state
= STATE_AFTER_STREAM_PACKET_CONTEXT
;
905 BT_ASSERT(!notit
->packet_context_field
);
907 if (packet_context_fc
->in_ir
) {
909 * Create free packet context field from stream class.
910 * This field is going to be moved to the packet once we
911 * create it. We cannot create the packet now because a
912 * packet is created from a stream, and this API must be
913 * able to return the packet header and context fields
914 * without creating a stream
915 * (read_packet_header_context_fields()).
917 notit
->packet_context_field
=
918 bt_packet_context_field_create(
919 notit
->meta
.sc
->ir_sc
);
920 if (!notit
->packet_context_field
) {
921 BT_LOGE_STR("Cannot create packet context field wrapper from stream class.");
922 status
= BT_MSG_ITER_STATUS_ERROR
;
926 notit
->dscopes
.stream_packet_context
=
927 bt_packet_context_field_borrow_field(
928 notit
->packet_context_field
);
929 BT_ASSERT(notit
->dscopes
.stream_packet_context
);
932 BT_LOGV("Decoding packet context field: "
933 "notit-addr=%p, stream-class-addr=%p, "
934 "stream-class-id=%" PRId64
", fc-addr=%p",
935 notit
, notit
->meta
.sc
,
936 notit
->meta
.sc
->id
, packet_context_fc
);
937 status
= read_dscope_begin_state(notit
, packet_context_fc
,
938 STATE_AFTER_STREAM_PACKET_CONTEXT
,
939 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
940 notit
->dscopes
.stream_packet_context
);
942 BT_LOGW("Cannot decode packet context field: "
943 "notit-addr=%p, stream-class-addr=%p, "
944 "stream-class-id=%" PRId64
", fc-addr=%p",
945 notit
, notit
->meta
.sc
,
955 enum bt_msg_iter_status
read_packet_context_continue_state(
956 struct bt_msg_iter
*notit
)
958 return read_dscope_continue_state(notit
,
959 STATE_AFTER_STREAM_PACKET_CONTEXT
);
963 enum bt_msg_iter_status
set_current_packet_content_sizes(
964 struct bt_msg_iter
*notit
)
966 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
968 if (notit
->cur_exp_packet_total_size
== -1) {
969 if (notit
->cur_exp_packet_content_size
!= -1) {
970 BT_LOGW("Content size is set, but packet size is not: "
971 "notit-addr=%p, packet-context-field-addr=%p, "
972 "packet-size=%" PRId64
", content-size=%" PRId64
,
973 notit
, notit
->dscopes
.stream_packet_context
,
974 notit
->cur_exp_packet_total_size
,
975 notit
->cur_exp_packet_content_size
);
976 status
= BT_MSG_ITER_STATUS_ERROR
;
980 if (notit
->cur_exp_packet_content_size
== -1) {
981 notit
->cur_exp_packet_content_size
=
982 notit
->cur_exp_packet_total_size
;
986 if (notit
->cur_exp_packet_content_size
>
987 notit
->cur_exp_packet_total_size
) {
988 BT_LOGW("Invalid packet or content size: "
989 "content size is greater than packet size: "
990 "notit-addr=%p, packet-context-field-addr=%p, "
991 "packet-size=%" PRId64
", content-size=%" PRId64
,
992 notit
, notit
->dscopes
.stream_packet_context
,
993 notit
->cur_exp_packet_total_size
,
994 notit
->cur_exp_packet_content_size
);
995 status
= BT_MSG_ITER_STATUS_ERROR
;
999 BT_LOGV("Set current packet and content sizes: "
1000 "notit-addr=%p, packet-size=%" PRIu64
", content-size=%" PRIu64
,
1001 notit
, notit
->cur_exp_packet_total_size
,
1002 notit
->cur_exp_packet_content_size
);
1008 enum bt_msg_iter_status
after_packet_context_state(
1009 struct bt_msg_iter
*notit
)
1011 enum bt_msg_iter_status status
;
1013 status
= set_current_packet_content_sizes(notit
);
1014 if (status
!= BT_MSG_ITER_STATUS_OK
) {
1018 if (notit
->stream_begin_emitted
) {
1019 notit
->state
= STATE_EMIT_MSG_NEW_PACKET
;
1021 notit
->state
= STATE_EMIT_MSG_NEW_STREAM
;
1029 enum bt_msg_iter_status
read_event_header_begin_state(
1030 struct bt_msg_iter
*notit
)
1032 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1033 struct ctf_field_class
*event_header_fc
= NULL
;
1035 /* Reset the position of the last event header */
1036 notit
->buf
.last_eh_at
= notit
->buf
.at
;
1037 notit
->cur_event_class_id
= -1;
1039 /* Check if we have some content left */
1040 if (notit
->cur_exp_packet_content_size
>= 0) {
1041 if (unlikely(packet_at(notit
) ==
1042 notit
->cur_exp_packet_content_size
)) {
1043 /* No more events! */
1044 BT_LOGV("Reached end of packet: notit-addr=%p, "
1045 "cur=%zu", notit
, packet_at(notit
));
1046 notit
->state
= STATE_EMIT_MSG_END_OF_PACKET
;
1048 } else if (unlikely(packet_at(notit
) >
1049 notit
->cur_exp_packet_content_size
)) {
1050 /* That's not supposed to happen */
1051 BT_LOGV("Before decoding event header field: cursor is passed the packet's content: "
1052 "notit-addr=%p, content-size=%" PRId64
", "
1054 notit
->cur_exp_packet_content_size
,
1056 status
= BT_MSG_ITER_STATUS_ERROR
;
1061 * "Infinite" content: we're done when the medium has
1062 * nothing else for us.
1064 status
= buf_ensure_available_bits(notit
);
1065 if (status
!= BT_MSG_ITER_STATUS_OK
) {
1067 * If this function returns
1068 * `BT_MSG_ITER_STATUS_EOF`:
1070 * 1. bt_msg_iter_get_next_message()
1071 * emits a "packet end" message. This
1072 * resets the current packet. The state
1073 * remains unchanged otherwise.
1074 * 2. This function is called again. It returns
1075 * `BT_MSG_ITER_STATUS_EOF` again.
1076 * 3. bt_msg_iter_get_next_message()
1077 * emits a "stream end" message because
1078 * there's no current packet. It sets the
1079 * current state to `STATE_DONE`.
1085 release_event_dscopes(notit
);
1086 BT_ASSERT(notit
->meta
.sc
);
1087 event_header_fc
= notit
->meta
.sc
->event_header_fc
;
1088 if (!event_header_fc
) {
1089 notit
->state
= STATE_AFTER_EVENT_HEADER
;
1093 if (event_header_fc
->in_ir
) {
1094 BT_ASSERT(!notit
->event_header_field
);
1095 notit
->event_header_field
=
1096 bt_event_header_field_create(
1097 notit
->meta
.sc
->ir_sc
);
1098 if (!notit
->event_header_field
) {
1099 BT_LOGE_STR("Cannot create event header field wrapper from trace class.");
1100 status
= BT_MSG_ITER_STATUS_ERROR
;
1104 notit
->dscopes
.event_header
=
1105 bt_event_header_field_borrow_field(
1106 notit
->event_header_field
);
1107 BT_ASSERT(notit
->dscopes
.event_header
);
1110 BT_LOGV("Decoding event header field: "
1111 "notit-addr=%p, stream-class-addr=%p, "
1112 "stream-class-id=%" PRId64
", "
1114 notit
, notit
->meta
.sc
,
1117 status
= read_dscope_begin_state(notit
, event_header_fc
,
1118 STATE_AFTER_EVENT_HEADER
,
1119 STATE_DSCOPE_EVENT_HEADER_CONTINUE
,
1120 notit
->dscopes
.event_header
);
1122 BT_LOGW("Cannot decode event header field: "
1123 "notit-addr=%p, stream-class-addr=%p, "
1124 "stream-class-id=%" PRId64
", fc-addr=%p",
1125 notit
, notit
->meta
.sc
,
1135 enum bt_msg_iter_status
read_event_header_continue_state(
1136 struct bt_msg_iter
*notit
)
1138 return read_dscope_continue_state(notit
,
1139 STATE_AFTER_EVENT_HEADER
);
1143 enum bt_msg_iter_status
set_current_event_class(struct bt_msg_iter
*notit
)
1145 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1147 struct ctf_event_class
*new_event_class
= NULL
;
1149 if (notit
->cur_event_class_id
== -1) {
1151 * No current event class ID field, therefore only one
1154 if (notit
->meta
.sc
->event_classes
->len
!= 1) {
1155 BT_LOGW("Need exactly one event class since there's "
1156 "no event class ID field: "
1157 "notit-addr=%p", notit
);
1158 status
= BT_MSG_ITER_STATUS_ERROR
;
1162 new_event_class
= notit
->meta
.sc
->event_classes
->pdata
[0];
1163 notit
->cur_event_class_id
= new_event_class
->id
;
1166 new_event_class
= ctf_stream_class_borrow_event_class_by_id(
1167 notit
->meta
.sc
, notit
->cur_event_class_id
);
1168 if (!new_event_class
) {
1169 BT_LOGW("No event class with ID of event class ID to use in stream class: "
1170 "notit-addr=%p, stream-class-id=%" PRIu64
", "
1171 "event-class-id=%" PRIu64
", "
1172 "trace-class-addr=%p",
1173 notit
, notit
->meta
.sc
->id
, notit
->cur_event_class_id
,
1175 status
= BT_MSG_ITER_STATUS_ERROR
;
1179 notit
->meta
.ec
= new_event_class
;
1180 BT_LOGV("Set current event class: "
1181 "notit-addr=%p, event-class-addr=%p, "
1182 "event-class-id=%" PRId64
", "
1183 "event-class-name=\"%s\"",
1184 notit
, notit
->meta
.ec
, notit
->meta
.ec
->id
,
1185 notit
->meta
.ec
->name
->str
);
1192 enum bt_msg_iter_status
set_current_event_message(
1193 struct bt_msg_iter
*notit
)
1195 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1196 bt_message
*msg
= NULL
;
1198 BT_ASSERT(notit
->meta
.ec
);
1199 BT_ASSERT(notit
->packet
);
1200 BT_LOGV("Creating event message from event class and packet: "
1201 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", packet-addr=%p",
1202 notit
, notit
->meta
.ec
,
1203 notit
->meta
.ec
->name
->str
,
1205 BT_ASSERT(notit
->msg_iter
);
1206 msg
= bt_message_event_create(notit
->msg_iter
,
1207 notit
->meta
.ec
->ir_ec
, notit
->packet
);
1209 BT_LOGE("Cannot create event message: "
1210 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", "
1212 notit
, notit
->meta
.ec
,
1213 notit
->meta
.ec
->name
->str
,
1221 BT_MESSAGE_PUT_REF_AND_RESET(msg
);
1222 status
= BT_MSG_ITER_STATUS_ERROR
;
1225 BT_MESSAGE_MOVE_REF(notit
->event_msg
, msg
);
1230 enum bt_msg_iter_status
after_event_header_state(
1231 struct bt_msg_iter
*notit
)
1233 enum bt_msg_iter_status status
;
1235 status
= set_current_event_class(notit
);
1236 if (status
!= BT_MSG_ITER_STATUS_OK
) {
1240 status
= set_current_event_message(notit
);
1241 if (status
!= BT_MSG_ITER_STATUS_OK
) {
1245 notit
->event
= bt_message_event_borrow_event(
1247 BT_ASSERT(notit
->event
);
1249 if (notit
->event_header_field
) {
1252 BT_ASSERT(notit
->event
);
1253 ret
= bt_event_move_header_field(notit
->event
,
1254 notit
->event_header_field
);
1256 status
= BT_MSG_ITER_STATUS_ERROR
;
1260 notit
->event_header_field
= NULL
;
1263 * At this point notit->dscopes.event_header has
1264 * the same value as the event header field within
1267 BT_ASSERT(bt_event_borrow_header_field(
1268 notit
->event
) == notit
->dscopes
.event_header
);
1271 notit
->state
= STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
;
1278 enum bt_msg_iter_status
read_event_common_context_begin_state(
1279 struct bt_msg_iter
*notit
)
1281 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1282 struct ctf_field_class
*event_common_context_fc
;
1284 event_common_context_fc
= notit
->meta
.sc
->event_common_context_fc
;
1285 if (!event_common_context_fc
) {
1286 notit
->state
= STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
;
1290 if (event_common_context_fc
->in_ir
) {
1291 BT_ASSERT(!notit
->dscopes
.event_common_context
);
1292 notit
->dscopes
.event_common_context
=
1293 bt_event_borrow_common_context_field(
1295 BT_ASSERT(notit
->dscopes
.event_common_context
);
1298 BT_LOGV("Decoding event common context field: "
1299 "notit-addr=%p, stream-class-addr=%p, "
1300 "stream-class-id=%" PRId64
", "
1302 notit
, notit
->meta
.sc
,
1304 event_common_context_fc
);
1305 status
= read_dscope_begin_state(notit
, event_common_context_fc
,
1306 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
,
1307 STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
,
1308 notit
->dscopes
.event_common_context
);
1310 BT_LOGW("Cannot decode event common context field: "
1311 "notit-addr=%p, stream-class-addr=%p, "
1312 "stream-class-id=%" PRId64
", fc-addr=%p",
1313 notit
, notit
->meta
.sc
,
1315 event_common_context_fc
);
1323 enum bt_msg_iter_status
read_event_common_context_continue_state(
1324 struct bt_msg_iter
*notit
)
1326 return read_dscope_continue_state(notit
,
1327 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
);
1331 enum bt_msg_iter_status
read_event_spec_context_begin_state(
1332 struct bt_msg_iter
*notit
)
1334 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1335 struct ctf_field_class
*event_spec_context_fc
;
1337 event_spec_context_fc
= notit
->meta
.ec
->spec_context_fc
;
1338 if (!event_spec_context_fc
) {
1339 notit
->state
= STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
;
1343 if (event_spec_context_fc
->in_ir
) {
1344 BT_ASSERT(!notit
->dscopes
.event_spec_context
);
1345 notit
->dscopes
.event_spec_context
=
1346 bt_event_borrow_specific_context_field(
1348 BT_ASSERT(notit
->dscopes
.event_spec_context
);
1351 BT_LOGV("Decoding event specific context field: "
1352 "notit-addr=%p, event-class-addr=%p, "
1353 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1355 notit
, notit
->meta
.ec
,
1356 notit
->meta
.ec
->name
->str
,
1358 event_spec_context_fc
);
1359 status
= read_dscope_begin_state(notit
, event_spec_context_fc
,
1360 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
1361 STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
,
1362 notit
->dscopes
.event_spec_context
);
1364 BT_LOGW("Cannot decode event specific context field: "
1365 "notit-addr=%p, event-class-addr=%p, "
1366 "event-class-name=\"%s\", "
1367 "event-class-id=%" PRId64
", fc-addr=%p",
1368 notit
, notit
->meta
.ec
,
1369 notit
->meta
.ec
->name
->str
,
1371 event_spec_context_fc
);
1379 enum bt_msg_iter_status
read_event_spec_context_continue_state(
1380 struct bt_msg_iter
*notit
)
1382 return read_dscope_continue_state(notit
,
1383 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
);
1387 enum bt_msg_iter_status
read_event_payload_begin_state(
1388 struct bt_msg_iter
*notit
)
1390 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1391 struct ctf_field_class
*event_payload_fc
;
1393 event_payload_fc
= notit
->meta
.ec
->payload_fc
;
1394 if (!event_payload_fc
) {
1395 notit
->state
= STATE_EMIT_MSG_EVENT
;
1399 if (event_payload_fc
->in_ir
) {
1400 BT_ASSERT(!notit
->dscopes
.event_payload
);
1401 notit
->dscopes
.event_payload
=
1402 bt_event_borrow_payload_field(
1404 BT_ASSERT(notit
->dscopes
.event_payload
);
1407 BT_LOGV("Decoding event payload field: "
1408 "notit-addr=%p, event-class-addr=%p, "
1409 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1411 notit
, notit
->meta
.ec
,
1412 notit
->meta
.ec
->name
->str
,
1415 status
= read_dscope_begin_state(notit
, event_payload_fc
,
1416 STATE_EMIT_MSG_EVENT
,
1417 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
1418 notit
->dscopes
.event_payload
);
1420 BT_LOGW("Cannot decode event payload field: "
1421 "notit-addr=%p, event-class-addr=%p, "
1422 "event-class-name=\"%s\", "
1423 "event-class-id=%" PRId64
", fc-addr=%p",
1424 notit
, notit
->meta
.ec
,
1425 notit
->meta
.ec
->name
->str
,
1435 enum bt_msg_iter_status
read_event_payload_continue_state(
1436 struct bt_msg_iter
*notit
)
1438 return read_dscope_continue_state(notit
, STATE_EMIT_MSG_EVENT
);
1442 enum bt_msg_iter_status
skip_packet_padding_state(
1443 struct bt_msg_iter
*notit
)
1445 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1446 size_t bits_to_skip
;
1448 BT_ASSERT(notit
->cur_exp_packet_total_size
> 0);
1449 bits_to_skip
= notit
->cur_exp_packet_total_size
- packet_at(notit
);
1450 if (bits_to_skip
== 0) {
1451 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1454 size_t bits_to_consume
;
1456 BT_LOGV("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu",
1457 bits_to_skip
, notit
, bits_to_skip
);
1458 status
= buf_ensure_available_bits(notit
);
1459 if (status
!= BT_MSG_ITER_STATUS_OK
) {
1463 bits_to_consume
= MIN(buf_available_bits(notit
), bits_to_skip
);
1464 BT_LOGV("Skipping %zu bits of padding: notit-addr=%p, size=%zu",
1465 bits_to_consume
, notit
, bits_to_consume
);
1466 buf_consume_bits(notit
, bits_to_consume
);
1467 bits_to_skip
= notit
->cur_exp_packet_total_size
-
1469 if (bits_to_skip
== 0) {
1470 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1480 enum bt_msg_iter_status
handle_state(struct bt_msg_iter
*notit
)
1482 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1483 const enum state state
= notit
->state
;
1485 BT_LOGV("Handling state: notit-addr=%p, state=%s",
1486 notit
, state_string(state
));
1488 // TODO: optimalize!
1491 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1493 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
1494 status
= read_packet_header_begin_state(notit
);
1496 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
1497 status
= read_packet_header_continue_state(notit
);
1499 case STATE_AFTER_TRACE_PACKET_HEADER
:
1500 status
= after_packet_header_state(notit
);
1502 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
1503 status
= read_packet_context_begin_state(notit
);
1505 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
1506 status
= read_packet_context_continue_state(notit
);
1508 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
1509 status
= after_packet_context_state(notit
);
1511 case STATE_EMIT_MSG_NEW_STREAM
:
1512 notit
->state
= STATE_EMIT_MSG_NEW_PACKET
;
1514 case STATE_EMIT_MSG_NEW_PACKET
:
1515 notit
->state
= STATE_DSCOPE_EVENT_HEADER_BEGIN
;
1517 case STATE_DSCOPE_EVENT_HEADER_BEGIN
:
1518 status
= read_event_header_begin_state(notit
);
1520 case STATE_DSCOPE_EVENT_HEADER_CONTINUE
:
1521 status
= read_event_header_continue_state(notit
);
1523 case STATE_AFTER_EVENT_HEADER
:
1524 status
= after_event_header_state(notit
);
1526 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
:
1527 status
= read_event_common_context_begin_state(notit
);
1529 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
:
1530 status
= read_event_common_context_continue_state(notit
);
1532 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
:
1533 status
= read_event_spec_context_begin_state(notit
);
1535 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
:
1536 status
= read_event_spec_context_continue_state(notit
);
1538 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
1539 status
= read_event_payload_begin_state(notit
);
1541 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
1542 status
= read_event_payload_continue_state(notit
);
1544 case STATE_EMIT_MSG_EVENT
:
1545 notit
->state
= STATE_DSCOPE_EVENT_HEADER_BEGIN
;
1547 case STATE_SKIP_PACKET_PADDING
:
1548 status
= skip_packet_padding_state(notit
);
1550 case STATE_EMIT_MSG_END_OF_PACKET
:
1551 notit
->state
= STATE_SKIP_PACKET_PADDING
;
1554 BT_LOGD("Unknown CTF plugin message iterator state: "
1555 "notit-addr=%p, state=%d", notit
, notit
->state
);
1559 BT_LOGV("Handled state: notit-addr=%p, status=%s, "
1560 "prev-state=%s, cur-state=%s",
1561 notit
, bt_msg_iter_status_string(status
),
1562 state_string(state
), state_string(notit
->state
));
1567 * Resets the internal state of a CTF message iterator.
1570 void bt_msg_iter_reset(struct bt_msg_iter
*notit
)
1573 BT_LOGD("Resetting message iterator: addr=%p", notit
);
1574 stack_clear(notit
->stack
);
1575 notit
->meta
.sc
= NULL
;
1576 notit
->meta
.ec
= NULL
;
1577 BT_PACKET_PUT_REF_AND_RESET(notit
->packet
);
1578 BT_STREAM_PUT_REF_AND_RESET(notit
->stream
);
1579 BT_MESSAGE_PUT_REF_AND_RESET(notit
->event_msg
);
1580 release_all_dscopes(notit
);
1581 notit
->cur_dscope_field
= NULL
;
1583 if (notit
->packet_header_field
) {
1584 bt_packet_header_field_release(notit
->packet_header_field
);
1585 notit
->packet_header_field
= NULL
;
1588 if (notit
->packet_context_field
) {
1589 bt_packet_context_field_release(notit
->packet_context_field
);
1590 notit
->packet_context_field
= NULL
;
1593 if (notit
->event_header_field
) {
1594 bt_event_header_field_release(notit
->event_header_field
);
1595 notit
->event_header_field
= NULL
;
1598 notit
->buf
.addr
= NULL
;
1601 notit
->buf
.last_eh_at
= SIZE_MAX
;
1602 notit
->buf
.packet_offset
= 0;
1603 notit
->state
= STATE_INIT
;
1604 notit
->cur_exp_packet_content_size
= -1;
1605 notit
->cur_exp_packet_total_size
= -1;
1606 notit
->cur_packet_offset
= -1;
1607 notit
->cur_stream_class_id
= -1;
1608 notit
->cur_event_class_id
= -1;
1609 notit
->cur_data_stream_id
= -1;
1610 notit
->stream_begin_emitted
= false;
1614 int bt_msg_iter_switch_packet(struct bt_msg_iter
*notit
)
1619 * We don't put the stream class here because we need to make
1620 * sure that all the packets processed by the same message
1621 * iterator refer to the same stream class (the first one).
1625 if (notit
->cur_exp_packet_total_size
!= -1) {
1626 notit
->cur_packet_offset
+= notit
->cur_exp_packet_total_size
;
1629 BT_LOGV("Switching packet: notit-addr=%p, cur=%zu, "
1630 "packet-offset=%" PRId64
, notit
, notit
->buf
.at
,
1631 notit
->cur_packet_offset
);
1632 stack_clear(notit
->stack
);
1633 notit
->meta
.ec
= NULL
;
1634 BT_PACKET_PUT_REF_AND_RESET(notit
->packet
);
1635 BT_MESSAGE_PUT_REF_AND_RESET(notit
->event_msg
);
1636 release_all_dscopes(notit
);
1637 notit
->cur_dscope_field
= NULL
;
1640 * Adjust current buffer so that addr points to the beginning of the new
1643 if (notit
->buf
.addr
) {
1644 size_t consumed_bytes
= (size_t) (notit
->buf
.at
/ CHAR_BIT
);
1646 /* Packets are assumed to start on a byte frontier. */
1647 if (notit
->buf
.at
% CHAR_BIT
) {
1648 BT_LOGW("Cannot switch packet: current position is not a multiple of 8: "
1649 "notit-addr=%p, cur=%zu", notit
, notit
->buf
.at
);
1654 notit
->buf
.addr
+= consumed_bytes
;
1655 notit
->buf
.sz
-= consumed_bytes
;
1657 notit
->buf
.packet_offset
= 0;
1658 BT_LOGV("Adjusted buffer: addr=%p, size=%zu",
1659 notit
->buf
.addr
, notit
->buf
.sz
);
1662 notit
->cur_exp_packet_content_size
= -1;
1663 notit
->cur_exp_packet_total_size
= -1;
1664 notit
->cur_stream_class_id
= -1;
1665 notit
->cur_event_class_id
= -1;
1666 notit
->cur_data_stream_id
= -1;
1667 notit
->snapshots
.discarded_events
= UINT64_C(-1);
1668 notit
->snapshots
.packets
= UINT64_C(-1);
1669 notit
->snapshots
.beginning_clock
= UINT64_C(-1);
1670 notit
->snapshots
.end_clock
= UINT64_C(-1);
1677 bt_field
*borrow_next_field(struct bt_msg_iter
*notit
)
1679 bt_field
*next_field
= NULL
;
1680 bt_field
*base_field
;
1681 const bt_field_class
*base_fc
;
1684 BT_ASSERT(!stack_empty(notit
->stack
));
1685 index
= stack_top(notit
->stack
)->index
;
1686 base_field
= stack_top(notit
->stack
)->base
;
1687 BT_ASSERT(base_field
);
1688 base_fc
= bt_field_borrow_class_const(base_field
);
1691 switch (bt_field_class_get_type(base_fc
)) {
1692 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
1695 bt_field_class_structure_get_member_count(
1696 bt_field_borrow_class_const(
1699 bt_field_structure_borrow_member_field_by_index(
1703 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
1704 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
1705 BT_ASSERT(index
< bt_field_array_get_length(base_field
));
1706 next_field
= bt_field_array_borrow_element_field_by_index(
1709 case BT_FIELD_CLASS_TYPE_VARIANT
:
1710 BT_ASSERT(index
== 0);
1711 next_field
= bt_field_variant_borrow_selected_option_field(
1718 BT_ASSERT(next_field
);
1723 void update_default_clock(struct bt_msg_iter
*notit
, uint64_t new_val
,
1724 uint64_t new_val_size
)
1726 uint64_t new_val_mask
;
1727 uint64_t cur_value_masked
;
1729 BT_ASSERT(new_val_size
> 0);
1732 * Special case for a 64-bit new value, which is the limit
1733 * of a clock value as of this version: overwrite the
1734 * current value directly.
1736 if (new_val_size
== 64) {
1737 notit
->default_clock_snapshot
= new_val
;
1741 new_val_mask
= (1ULL << new_val_size
) - 1;
1742 cur_value_masked
= notit
->default_clock_snapshot
& new_val_mask
;
1744 if (new_val
< cur_value_masked
) {
1746 * It looks like a wrap happened on the number of bits
1747 * of the requested new value. Assume that the clock
1748 * value wrapped only one time.
1750 notit
->default_clock_snapshot
+= new_val_mask
+ 1;
1753 /* Clear the low bits of the current clock value. */
1754 notit
->default_clock_snapshot
&= ~new_val_mask
;
1756 /* Set the low bits of the current clock value. */
1757 notit
->default_clock_snapshot
|= new_val
;
1760 BT_LOGV("Updated default clock's value from integer field's value: "
1761 "value=%" PRIu64
, notit
->default_clock_snapshot
);
1765 enum bt_bfcr_status
bfcr_unsigned_int_cb(uint64_t value
,
1766 struct ctf_field_class
*fc
, void *data
)
1768 struct bt_msg_iter
*notit
= data
;
1769 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
1770 bt_field
*field
= NULL
;
1771 struct ctf_field_class_int
*int_fc
= (void *) fc
;
1773 BT_LOGV("Unsigned integer function called from BFCR: "
1774 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1775 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64
,
1776 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
1778 if (likely(int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
)) {
1779 goto update_def_clock
;
1782 switch (int_fc
->meaning
) {
1783 case CTF_FIELD_CLASS_MEANING_EVENT_CLASS_ID
:
1784 notit
->cur_event_class_id
= value
;
1786 case CTF_FIELD_CLASS_MEANING_DATA_STREAM_ID
:
1787 notit
->cur_data_stream_id
= value
;
1789 case CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME
:
1790 notit
->snapshots
.beginning_clock
= value
;
1792 case CTF_FIELD_CLASS_MEANING_PACKET_END_TIME
:
1793 notit
->snapshots
.end_clock
= value
;
1795 case CTF_FIELD_CLASS_MEANING_STREAM_CLASS_ID
:
1796 notit
->cur_stream_class_id
= value
;
1798 case CTF_FIELD_CLASS_MEANING_MAGIC
:
1799 if (value
!= 0xc1fc1fc1) {
1800 BT_LOGW("Invalid CTF magic number: notit-addr=%p, "
1801 "magic=%" PRIx64
, notit
, value
);
1802 status
= BT_BFCR_STATUS_ERROR
;
1807 case CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT
:
1808 notit
->snapshots
.packets
= value
;
1810 case CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT
:
1811 notit
->snapshots
.discarded_events
= value
;
1813 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_TOTAL_SIZE
:
1814 notit
->cur_exp_packet_total_size
= value
;
1816 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_CONTENT_SIZE
:
1817 notit
->cur_exp_packet_content_size
= value
;
1824 if (unlikely(int_fc
->mapped_clock_class
)) {
1825 update_default_clock(notit
, value
, int_fc
->base
.size
);
1828 if (unlikely(int_fc
->storing_index
>= 0)) {
1829 g_array_index(notit
->stored_values
, uint64_t,
1830 (uint64_t) int_fc
->storing_index
) = value
;
1833 if (unlikely(!fc
->in_ir
)) {
1837 field
= borrow_next_field(notit
);
1839 BT_ASSERT(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
1840 BT_ASSERT(bt_field_get_class_type(field
) ==
1841 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
||
1842 bt_field_get_class_type(field
) ==
1843 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
);
1844 bt_field_unsigned_integer_set_value(field
, value
);
1845 stack_top(notit
->stack
)->index
++;
1852 enum bt_bfcr_status
bfcr_unsigned_int_char_cb(uint64_t value
,
1853 struct ctf_field_class
*fc
, void *data
)
1856 struct bt_msg_iter
*notit
= data
;
1857 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
1858 bt_field
*string_field
= NULL
;
1859 struct ctf_field_class_int
*int_fc
= (void *) fc
;
1860 char str
[2] = {'\0', '\0'};
1862 BT_LOGV("Unsigned integer character function called from BFCR: "
1863 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1864 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64
,
1865 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
1866 BT_ASSERT(int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
);
1867 BT_ASSERT(!int_fc
->mapped_clock_class
);
1868 BT_ASSERT(int_fc
->storing_index
< 0);
1870 if (unlikely(!fc
->in_ir
)) {
1874 if (notit
->done_filling_string
) {
1879 notit
->done_filling_string
= true;
1883 string_field
= stack_top(notit
->stack
)->base
;
1884 BT_ASSERT(bt_field_get_class_type(string_field
) ==
1885 BT_FIELD_CLASS_TYPE_STRING
);
1887 /* Append character */
1888 str
[0] = (char) value
;
1889 ret
= bt_field_string_append_with_length(string_field
, str
, 1);
1891 BT_LOGE("Cannot append character to string field's value: "
1892 "notit-addr=%p, field-addr=%p, ret=%d",
1893 notit
, string_field
, ret
);
1894 status
= BT_BFCR_STATUS_ERROR
;
1903 enum bt_bfcr_status
bfcr_signed_int_cb(int64_t value
,
1904 struct ctf_field_class
*fc
, void *data
)
1906 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
1907 bt_field
*field
= NULL
;
1908 struct bt_msg_iter
*notit
= data
;
1909 struct ctf_field_class_int
*int_fc
= (void *) fc
;
1911 BT_LOGV("Signed integer function called from BFCR: "
1912 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1913 "fc-type=%d, fc-in-ir=%d, value=%" PRId64
,
1914 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
1915 BT_ASSERT(int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
);
1917 if (unlikely(int_fc
->storing_index
>= 0)) {
1918 g_array_index(notit
->stored_values
, uint64_t,
1919 (uint64_t) int_fc
->storing_index
) = (uint64_t) value
;
1922 if (unlikely(!fc
->in_ir
)) {
1926 field
= borrow_next_field(notit
);
1928 BT_ASSERT(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
1929 BT_ASSERT(bt_field_get_class_type(field
) ==
1930 BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
||
1931 bt_field_get_class_type(field
) ==
1932 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
);
1933 bt_field_signed_integer_set_value(field
, value
);
1934 stack_top(notit
->stack
)->index
++;
1941 enum bt_bfcr_status
bfcr_floating_point_cb(double value
,
1942 struct ctf_field_class
*fc
, void *data
)
1944 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
1945 bt_field
*field
= NULL
;
1946 struct bt_msg_iter
*notit
= data
;
1948 BT_LOGV("Floating point number function called from BFCR: "
1949 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1950 "fc-type=%d, fc-in-ir=%d, value=%f",
1951 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
1953 if (unlikely(!fc
->in_ir
)) {
1957 field
= borrow_next_field(notit
);
1959 BT_ASSERT(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
1960 BT_ASSERT(bt_field_get_class_type(field
) ==
1961 BT_FIELD_CLASS_TYPE_REAL
);
1962 bt_field_real_set_value(field
, value
);
1963 stack_top(notit
->stack
)->index
++;
1970 enum bt_bfcr_status
bfcr_string_begin_cb(
1971 struct ctf_field_class
*fc
, void *data
)
1973 bt_field
*field
= NULL
;
1974 struct bt_msg_iter
*notit
= data
;
1977 BT_LOGV("String (beginning) function called from BFCR: "
1978 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1979 "fc-type=%d, fc-in-ir=%d",
1980 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
1982 if (unlikely(!fc
->in_ir
)) {
1986 field
= borrow_next_field(notit
);
1988 BT_ASSERT(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
1989 BT_ASSERT(bt_field_get_class_type(field
) ==
1990 BT_FIELD_CLASS_TYPE_STRING
);
1991 ret
= bt_field_string_clear(field
);
1992 BT_ASSERT(ret
== 0);
1995 * Push on stack. Not a compound class per se, but we know that
1996 * only bfcr_string_cb() may be called between this call and a
1997 * subsequent call to bfcr_string_end_cb().
1999 stack_push(notit
->stack
, field
);
2002 return BT_BFCR_STATUS_OK
;
2006 enum bt_bfcr_status
bfcr_string_cb(const char *value
,
2007 size_t len
, struct ctf_field_class
*fc
, void *data
)
2009 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
2010 bt_field
*field
= NULL
;
2011 struct bt_msg_iter
*notit
= data
;
2014 BT_LOGV("String (substring) function called from BFCR: "
2015 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2016 "fc-type=%d, fc-in-ir=%d, string-length=%zu",
2017 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
,
2020 if (unlikely(!fc
->in_ir
)) {
2024 field
= stack_top(notit
->stack
)->base
;
2027 /* Append current substring */
2028 ret
= bt_field_string_append_with_length(field
, value
, len
);
2030 BT_LOGE("Cannot append substring to string field's value: "
2031 "notit-addr=%p, field-addr=%p, string-length=%zu, "
2032 "ret=%d", notit
, field
, len
, ret
);
2033 status
= BT_BFCR_STATUS_ERROR
;
2042 enum bt_bfcr_status
bfcr_string_end_cb(
2043 struct ctf_field_class
*fc
, void *data
)
2045 struct bt_msg_iter
*notit
= data
;
2047 BT_LOGV("String (end) function called from BFCR: "
2048 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2049 "fc-type=%d, fc-in-ir=%d",
2050 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
2052 if (unlikely(!fc
->in_ir
)) {
2056 /* Pop string field */
2057 stack_pop(notit
->stack
);
2059 /* Go to next field */
2060 stack_top(notit
->stack
)->index
++;
2063 return BT_BFCR_STATUS_OK
;
2066 enum bt_bfcr_status
bfcr_compound_begin_cb(
2067 struct ctf_field_class
*fc
, void *data
)
2069 struct bt_msg_iter
*notit
= data
;
2072 BT_LOGV("Compound (beginning) function called from BFCR: "
2073 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2074 "fc-type=%d, fc-in-ir=%d",
2075 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
2082 if (stack_empty(notit
->stack
)) {
2083 /* Root: already set by read_dscope_begin_state() */
2084 field
= notit
->cur_dscope_field
;
2086 field
= borrow_next_field(notit
);
2092 BT_ASSERT(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
2093 stack_push(notit
->stack
, field
);
2096 * Change BFCR "unsigned int" callback if it's a text
2099 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
||
2100 fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
2101 struct ctf_field_class_array_base
*array_fc
= (void *) fc
;
2103 if (array_fc
->is_text
) {
2106 BT_ASSERT(bt_field_get_class_type(field
) ==
2107 BT_FIELD_CLASS_TYPE_STRING
);
2108 notit
->done_filling_string
= false;
2109 ret
= bt_field_string_clear(field
);
2110 BT_ASSERT(ret
== 0);
2111 bt_bfcr_set_unsigned_int_cb(notit
->bfcr
,
2112 bfcr_unsigned_int_char_cb
);
2117 return BT_BFCR_STATUS_OK
;
2120 enum bt_bfcr_status
bfcr_compound_end_cb(
2121 struct ctf_field_class
*fc
, void *data
)
2123 struct bt_msg_iter
*notit
= data
;
2125 BT_LOGV("Compound (end) function called from BFCR: "
2126 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2127 "fc-type=%d, fc-in-ir=%d",
2128 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
2134 BT_ASSERT(!stack_empty(notit
->stack
));
2135 BT_ASSERT(bt_field_borrow_class_const(stack_top(notit
->stack
)->base
) ==
2139 * Reset BFCR "unsigned int" callback if it's a text
2142 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
||
2143 fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
2144 struct ctf_field_class_array_base
*array_fc
= (void *) fc
;
2146 if (array_fc
->is_text
) {
2147 BT_ASSERT(bt_field_get_class_type(
2148 stack_top(notit
->stack
)->base
) ==
2149 BT_FIELD_CLASS_TYPE_STRING
);
2150 bt_bfcr_set_unsigned_int_cb(notit
->bfcr
,
2151 bfcr_unsigned_int_cb
);
2156 stack_pop(notit
->stack
);
2158 /* If the stack is not empty, increment the base's index */
2159 if (!stack_empty(notit
->stack
)) {
2160 stack_top(notit
->stack
)->index
++;
2164 return BT_BFCR_STATUS_OK
;
2168 int64_t bfcr_get_sequence_length_cb(struct ctf_field_class
*fc
, void *data
)
2170 bt_field
*seq_field
;
2171 struct bt_msg_iter
*notit
= data
;
2172 struct ctf_field_class_sequence
*seq_fc
= (void *) fc
;
2173 int64_t length
= -1;
2176 length
= (uint64_t) g_array_index(notit
->stored_values
, uint64_t,
2177 seq_fc
->stored_length_index
);
2178 seq_field
= stack_top(notit
->stack
)->base
;
2179 BT_ASSERT(seq_field
);
2180 ret
= bt_field_dynamic_array_set_length(seq_field
, (uint64_t) length
);
2182 BT_LOGE("Cannot set dynamic array field's length field: "
2183 "notit-addr=%p, field-addr=%p, "
2184 "length=%" PRIu64
, notit
, seq_field
, length
);
2191 struct ctf_field_class
*bfcr_borrow_variant_selected_field_class_cb(
2192 struct ctf_field_class
*fc
, void *data
)
2196 int64_t option_index
= -1;
2197 struct bt_msg_iter
*notit
= data
;
2198 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
2199 struct ctf_named_field_class
*selected_option
= NULL
;
2200 struct ctf_field_class
*ret_fc
= NULL
;
2206 /* Get variant's tag */
2207 tag
.u
= g_array_index(notit
->stored_values
, uint64_t,
2208 var_fc
->stored_tag_index
);
2211 * Check each range to find the selected option's index.
2213 if (var_fc
->tag_fc
->base
.is_signed
) {
2214 for (i
= 0; i
< var_fc
->ranges
->len
; i
++) {
2215 struct ctf_field_class_variant_range
*range
=
2216 ctf_field_class_variant_borrow_range_by_index(
2219 if (tag
.i
>= range
->range
.lower
.i
&&
2220 tag
.i
<= range
->range
.upper
.i
) {
2221 option_index
= (int64_t) range
->option_index
;
2226 for (i
= 0; i
< var_fc
->ranges
->len
; i
++) {
2227 struct ctf_field_class_variant_range
*range
=
2228 ctf_field_class_variant_borrow_range_by_index(
2231 if (tag
.u
>= range
->range
.lower
.u
&&
2232 tag
.u
<= range
->range
.upper
.u
) {
2233 option_index
= (int64_t) range
->option_index
;
2239 if (option_index
< 0) {
2240 BT_LOGW("Cannot find variant field class's option: "
2241 "notit-addr=%p, var-fc-addr=%p, u-tag=%" PRIu64
", "
2242 "i-tag=%" PRId64
, notit
, var_fc
, tag
.u
, tag
.i
);
2246 selected_option
= ctf_field_class_variant_borrow_option_by_index(
2247 var_fc
, (uint64_t) option_index
);
2249 if (selected_option
->fc
->in_ir
) {
2250 bt_field
*var_field
= stack_top(notit
->stack
)->base
;
2252 ret
= bt_field_variant_select_option_field(
2253 var_field
, option_index
);
2255 BT_LOGW("Cannot select variant field's option field: "
2256 "notit-addr=%p, var-field-addr=%p, "
2257 "opt-index=%" PRId64
, notit
, var_field
,
2263 ret_fc
= selected_option
->fc
;
2270 void set_event_default_clock_snapshot(struct bt_msg_iter
*notit
)
2273 bt_message_event_borrow_event(
2275 bt_stream_class
*sc
= notit
->meta
.sc
->ir_sc
;
2279 if (bt_stream_class_borrow_default_clock_class(sc
)) {
2280 bt_event_set_default_clock_snapshot(event
,
2281 notit
->default_clock_snapshot
);
2286 void notify_new_stream(struct bt_msg_iter
*notit
,
2287 bt_message
**message
)
2289 enum bt_msg_iter_status status
;
2290 bt_message
*ret
= NULL
;
2292 status
= set_current_stream(notit
);
2293 if (status
!= BT_MSG_ITER_STATUS_OK
) {
2294 BT_MESSAGE_PUT_REF_AND_RESET(ret
);
2298 BT_ASSERT(notit
->stream
);
2299 BT_ASSERT(notit
->msg_iter
);
2300 ret
= bt_message_stream_beginning_create(notit
->msg_iter
,
2303 BT_LOGE("Cannot create stream beginning message: "
2304 "notit-addr=%p, stream-addr=%p",
2305 notit
, notit
->stream
);
2314 void notify_end_of_stream(struct bt_msg_iter
*notit
,
2315 bt_message
**message
)
2319 if (!notit
->stream
) {
2320 BT_LOGE("Cannot create stream for stream message: "
2321 "notit-addr=%p", notit
);
2325 BT_ASSERT(notit
->msg_iter
);
2326 ret
= bt_message_stream_end_create(notit
->msg_iter
,
2329 BT_LOGE("Cannot create stream beginning message: "
2330 "notit-addr=%p, stream-addr=%p",
2331 notit
, notit
->stream
);
2338 void notify_new_packet(struct bt_msg_iter
*notit
,
2339 bt_message
**message
)
2342 enum bt_msg_iter_status status
;
2343 bt_message
*msg
= NULL
;
2344 const bt_stream_class
*sc
;
2346 status
= set_current_packet(notit
);
2347 if (status
!= BT_MSG_ITER_STATUS_OK
) {
2351 BT_ASSERT(notit
->packet
);
2352 sc
= notit
->meta
.sc
->ir_sc
;
2355 if (bt_stream_class_packets_have_discarded_event_counter_snapshot(sc
)) {
2356 BT_ASSERT(notit
->snapshots
.discarded_events
!= UINT64_C(-1));
2357 bt_packet_set_discarded_event_counter_snapshot(
2358 notit
->packet
, notit
->snapshots
.discarded_events
);
2361 if (bt_stream_class_packets_have_packet_counter_snapshot(sc
)) {
2362 BT_ASSERT(notit
->snapshots
.packets
!= UINT64_C(-1));
2363 bt_packet_set_packet_counter_snapshot(
2364 notit
->packet
, notit
->snapshots
.packets
);
2367 if (bt_stream_class_packets_have_default_beginning_clock_snapshot(sc
)) {
2368 BT_ASSERT(notit
->snapshots
.beginning_clock
!= UINT64_C(-1));
2369 bt_packet_set_default_beginning_clock_snapshot(
2370 notit
->packet
, notit
->snapshots
.beginning_clock
);
2373 if (bt_stream_class_packets_have_default_end_clock_snapshot(sc
)) {
2374 BT_ASSERT(notit
->snapshots
.end_clock
!= UINT64_C(-1));
2375 bt_packet_set_default_end_clock_snapshot(
2376 notit
->packet
, notit
->snapshots
.end_clock
);
2379 if (notit
->packet_header_field
) {
2380 ret
= bt_packet_move_header_field(
2381 notit
->packet
, notit
->packet_header_field
);
2386 notit
->packet_header_field
= NULL
;
2389 * At this point notit->dscopes.trace_packet_header has
2390 * the same value as the packet header field within
2393 BT_ASSERT(bt_packet_borrow_header_field(
2395 notit
->dscopes
.trace_packet_header
);
2398 if (notit
->packet_context_field
) {
2399 ret
= bt_packet_move_context_field(
2400 notit
->packet
, notit
->packet_context_field
);
2405 notit
->packet_context_field
= NULL
;
2408 * At this point notit->dscopes.trace_packet_header has
2409 * the same value as the packet header field within
2412 BT_ASSERT(bt_packet_borrow_context_field(
2414 notit
->dscopes
.stream_packet_context
);
2417 BT_ASSERT(notit
->msg_iter
);
2418 msg
= bt_message_packet_beginning_create(notit
->msg_iter
,
2421 BT_LOGE("Cannot create packet beginning message: "
2422 "notit-addr=%p, packet-addr=%p",
2423 notit
, notit
->packet
);
2434 void notify_end_of_packet(struct bt_msg_iter
*notit
,
2435 bt_message
**message
)
2439 if (!notit
->packet
) {
2443 /* Update default clock from packet's end time */
2444 if (notit
->snapshots
.end_clock
!= UINT64_C(-1)) {
2445 notit
->default_clock_snapshot
= notit
->snapshots
.end_clock
;
2448 BT_ASSERT(notit
->msg_iter
);
2449 msg
= bt_message_packet_end_create(notit
->msg_iter
,
2452 BT_LOGE("Cannot create packet end message: "
2453 "notit-addr=%p, packet-addr=%p",
2454 notit
, notit
->packet
);
2459 BT_PACKET_PUT_REF_AND_RESET(notit
->packet
);
2464 struct bt_msg_iter
*bt_msg_iter_create(struct ctf_trace_class
*tc
,
2465 size_t max_request_sz
,
2466 struct bt_msg_iter_medium_ops medops
, void *data
)
2468 struct bt_msg_iter
*notit
= NULL
;
2469 struct bt_bfcr_cbs cbs
= {
2471 .signed_int
= bfcr_signed_int_cb
,
2472 .unsigned_int
= bfcr_unsigned_int_cb
,
2473 .floating_point
= bfcr_floating_point_cb
,
2474 .string_begin
= bfcr_string_begin_cb
,
2475 .string
= bfcr_string_cb
,
2476 .string_end
= bfcr_string_end_cb
,
2477 .compound_begin
= bfcr_compound_begin_cb
,
2478 .compound_end
= bfcr_compound_end_cb
,
2481 .get_sequence_length
= bfcr_get_sequence_length_cb
,
2482 .borrow_variant_selected_field_class
= bfcr_borrow_variant_selected_field_class_cb
,
2487 BT_ASSERT(medops
.request_bytes
);
2488 BT_ASSERT(medops
.borrow_stream
);
2489 BT_LOGD("Creating CTF plugin message iterator: "
2490 "trace-addr=%p, max-request-size=%zu, "
2491 "data=%p", tc
, max_request_sz
, data
);
2492 notit
= g_new0(struct bt_msg_iter
, 1);
2494 BT_LOGE_STR("Failed to allocate one CTF plugin message iterator.");
2497 notit
->meta
.tc
= tc
;
2498 notit
->medium
.medops
= medops
;
2499 notit
->medium
.max_request_sz
= max_request_sz
;
2500 notit
->medium
.data
= data
;
2501 notit
->stack
= stack_new(notit
);
2502 notit
->stored_values
= g_array_new(FALSE
, TRUE
, sizeof(uint64_t));
2503 g_array_set_size(notit
->stored_values
, tc
->stored_value_count
);
2505 if (!notit
->stack
) {
2506 BT_LOGE_STR("Failed to create field stack.");
2510 notit
->bfcr
= bt_bfcr_create(cbs
, notit
);
2512 BT_LOGE_STR("Failed to create binary class reader (BFCR).");
2516 bt_msg_iter_reset(notit
);
2517 BT_LOGD("Created CTF plugin message iterator: "
2518 "trace-addr=%p, max-request-size=%zu, "
2519 "data=%p, notit-addr=%p",
2520 tc
, max_request_sz
, data
, notit
);
2521 notit
->cur_packet_offset
= 0;
2527 bt_msg_iter_destroy(notit
);
2532 void bt_msg_iter_destroy(struct bt_msg_iter
*notit
)
2534 BT_PACKET_PUT_REF_AND_RESET(notit
->packet
);
2535 BT_STREAM_PUT_REF_AND_RESET(notit
->stream
);
2536 release_all_dscopes(notit
);
2538 BT_LOGD("Destroying CTF plugin message iterator: addr=%p", notit
);
2541 BT_LOGD_STR("Destroying field stack.");
2542 stack_destroy(notit
->stack
);
2546 BT_LOGD("Destroying BFCR: bfcr-addr=%p", notit
->bfcr
);
2547 bt_bfcr_destroy(notit
->bfcr
);
2550 if (notit
->stored_values
) {
2551 g_array_free(notit
->stored_values
, TRUE
);
2557 enum bt_msg_iter_status
bt_msg_iter_get_next_message(
2558 struct bt_msg_iter
*notit
,
2559 bt_self_message_iterator
*msg_iter
,
2560 bt_message
**message
)
2562 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
2567 if (notit
->state
== STATE_DONE
) {
2568 status
= BT_MSG_ITER_STATUS_EOF
;
2572 notit
->msg_iter
= msg_iter
;
2574 BT_LOGV("Getting next message: notit-addr=%p", notit
);
2577 status
= handle_state(notit
);
2578 if (status
== BT_MSG_ITER_STATUS_AGAIN
) {
2579 BT_LOGV_STR("Medium returned BT_MSG_ITER_STATUS_AGAIN.");
2583 if (status
!= BT_MSG_ITER_STATUS_OK
) {
2584 if (status
== BT_MSG_ITER_STATUS_EOF
) {
2585 enum state next_state
= notit
->state
;
2587 BT_LOGV_STR("Medium returned BT_MSG_ITER_STATUS_EOF.");
2589 if (notit
->packet
) {
2590 notify_end_of_packet(notit
,
2593 notify_end_of_stream(notit
,
2595 next_state
= STATE_DONE
;
2599 status
= BT_MSG_ITER_STATUS_ERROR
;
2603 status
= BT_MSG_ITER_STATUS_OK
;
2604 notit
->state
= next_state
;
2606 BT_LOGW("Cannot handle state: "
2607 "notit-addr=%p, state=%s",
2608 notit
, state_string(notit
->state
));
2614 switch (notit
->state
) {
2615 case STATE_EMIT_MSG_NEW_STREAM
:
2616 /* notify_new_stream() logs errors */
2617 notify_new_stream(notit
, message
);
2620 status
= BT_MSG_ITER_STATUS_ERROR
;
2623 notit
->stream_begin_emitted
= true;
2625 case STATE_EMIT_MSG_NEW_PACKET
:
2626 /* notify_new_packet() logs errors */
2627 notify_new_packet(notit
, message
);
2630 status
= BT_MSG_ITER_STATUS_ERROR
;
2634 case STATE_EMIT_MSG_EVENT
:
2635 BT_ASSERT(notit
->event_msg
);
2636 set_event_default_clock_snapshot(notit
);
2637 *message
= notit
->event_msg
;
2638 notit
->event_msg
= NULL
;
2640 case STATE_EMIT_MSG_END_OF_PACKET
:
2641 /* notify_end_of_packet() logs errors */
2642 notify_end_of_packet(notit
, message
);
2645 status
= BT_MSG_ITER_STATUS_ERROR
;
2650 /* Non-emitting state: continue */
2660 enum bt_msg_iter_status
read_packet_header_context_fields(
2661 struct bt_msg_iter
*notit
)
2664 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
2668 if (notit
->state
== STATE_EMIT_MSG_NEW_PACKET
) {
2669 /* We're already there */
2674 status
= handle_state(notit
);
2675 if (status
== BT_MSG_ITER_STATUS_AGAIN
) {
2676 BT_LOGV_STR("Medium returned BT_MSG_ITER_STATUS_AGAIN.");
2679 if (status
!= BT_MSG_ITER_STATUS_OK
) {
2680 if (status
== BT_MSG_ITER_STATUS_EOF
) {
2681 BT_LOGV_STR("Medium returned BT_MSG_ITER_STATUS_EOF.");
2683 BT_LOGW("Cannot handle state: "
2684 "notit-addr=%p, state=%s",
2685 notit
, state_string(notit
->state
));
2690 switch (notit
->state
) {
2691 case STATE_EMIT_MSG_NEW_PACKET
:
2693 * Packet header and context fields are
2694 * potentially decoded (or they don't exist).
2698 case STATE_EMIT_MSG_NEW_STREAM
:
2699 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
2700 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
2701 case STATE_AFTER_TRACE_PACKET_HEADER
:
2702 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
2703 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
2704 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
2705 /* Non-emitting state: continue */
2709 * We should never get past the
2710 * STATE_EMIT_MSG_NEW_PACKET state.
2712 BT_LOGF("Unexpected state: notit-addr=%p, state=%s",
2713 notit
, state_string(notit
->state
));
2719 ret
= set_current_packet_content_sizes(notit
);
2721 status
= BT_MSG_ITER_STATUS_ERROR
;
2729 void bt_msg_iter_set_medops_data(struct bt_msg_iter
*notit
,
2733 notit
->medium
.data
= medops_data
;
2737 enum bt_msg_iter_status
bt_msg_iter_seek(
2738 struct bt_msg_iter
*notit
, off_t offset
)
2740 enum bt_msg_iter_status ret
= BT_MSG_ITER_STATUS_OK
;
2741 enum bt_msg_iter_medium_status medium_status
;
2745 BT_LOGE("Cannot seek to negative offset: offset=%jd", offset
);
2746 ret
= BT_MSG_ITER_STATUS_INVAL
;
2750 if (!notit
->medium
.medops
.seek
) {
2751 ret
= BT_MSG_ITER_STATUS_UNSUPPORTED
;
2752 BT_LOGD("Aborting seek as the iterator's underlying media does not implement seek support.");
2756 medium_status
= notit
->medium
.medops
.seek(
2757 BT_MSG_ITER_SEEK_WHENCE_SET
, offset
, notit
->medium
.data
);
2758 if (medium_status
!= BT_MSG_ITER_MEDIUM_STATUS_OK
) {
2759 if (medium_status
== BT_MSG_ITER_MEDIUM_STATUS_EOF
) {
2760 ret
= BT_MSG_ITER_STATUS_EOF
;
2762 ret
= BT_MSG_ITER_STATUS_ERROR
;
2767 bt_msg_iter_reset(notit
);
2768 notit
->cur_packet_offset
= offset
;
2775 off_t
bt_msg_iter_get_current_packet_offset(struct bt_msg_iter
*notit
)
2778 return notit
->cur_packet_offset
;
2782 off_t
bt_msg_iter_get_current_packet_size(
2783 struct bt_msg_iter
*notit
)
2786 return notit
->cur_exp_packet_total_size
;
2790 void bt_msg_trace_class_changed(struct bt_msg_iter
*notit
)
2792 if (notit
->meta
.tc
->stored_value_count
> notit
->stored_values
->len
) {
2793 g_array_set_size(notit
->stored_values
,
2794 notit
->meta
.tc
->stored_value_count
);
2799 enum bt_msg_iter_status
bt_msg_iter_get_packet_properties(
2800 struct bt_msg_iter
*notit
,
2801 struct bt_msg_iter_packet_properties
*props
)
2803 enum bt_msg_iter_status status
;
2807 status
= read_packet_header_context_fields(notit
);
2808 if (status
!= BT_MSG_ITER_STATUS_OK
) {
2812 props
->exp_packet_total_size
=
2813 (uint64_t) notit
->cur_exp_packet_total_size
;
2814 props
->exp_packet_content_size
=
2815 (uint64_t) notit
->cur_exp_packet_content_size
;
2816 BT_ASSERT(props
->stream_class_id
>= 0);
2817 props
->stream_class_id
= (uint64_t) notit
->cur_stream_class_id
;
2818 props
->data_stream_id
= notit
->cur_data_stream_id
;
2819 props
->snapshots
.discarded_events
= notit
->snapshots
.discarded_events
;
2820 props
->snapshots
.packets
= notit
->snapshots
.packets
;
2821 props
->snapshots
.beginning_clock
= notit
->snapshots
.beginning_clock
;
2822 props
->snapshots
.end_clock
= notit
->snapshots
.end_clock
;