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_STREAM_BEGINNING
,
84 STATE_EMIT_MSG_STREAM_ACTIVITY_BEGINNING
,
85 STATE_EMIT_MSG_PACKET_BEGINNING
,
86 STATE_DSCOPE_EVENT_HEADER_BEGIN
,
87 STATE_DSCOPE_EVENT_HEADER_CONTINUE
,
88 STATE_AFTER_EVENT_HEADER
,
89 STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
,
90 STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
,
91 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
,
92 STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
,
93 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
94 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
96 STATE_SKIP_PACKET_PADDING
,
97 STATE_EMIT_MSG_PACKET_END_MULTI
,
98 STATE_EMIT_MSG_PACKET_END_SINGLE
,
99 STATE_EMIT_MSG_STREAM_ACTIVITY_END
,
100 STATE_EMIT_MSG_STREAM_END
,
104 /* CTF message iterator */
109 /* Current message iterator to create messages (weak) */
110 bt_self_message_iterator
*msg_iter
;
113 * True to emit stream beginning and stream activity beginning
116 bool emit_stream_begin_msg
;
119 * True to emit stream end and stream activity end messages.
121 bool emit_stream_end_msg
;
124 * Current dynamic scope field pointer.
126 * This is set by read_dscope_begin_state() and contains the
127 * value of one of the pointers in `dscopes` below.
129 bt_field
*cur_dscope_field
;
132 * True if we're done filling a string field from a text
133 * array/sequence payload.
135 bool done_filling_string
;
137 /* Trace and classes */
138 /* True to set IR fields */
142 struct ctf_trace_class
*tc
;
143 struct ctf_stream_class
*sc
;
144 struct ctf_event_class
*ec
;
147 /* Current packet context field wrapper (NULL if not created yet) */
148 bt_packet_context_field
*packet_context_field
;
150 /* Current packet (NULL if not created yet) */
153 /* Current stream (NULL if not set yet) */
156 /* Current event (NULL if not created yet) */
159 /* Current event message (NULL if not created yet) */
160 bt_message
*event_msg
;
162 /* Database of current dynamic scopes */
164 bt_field
*stream_packet_context
;
165 bt_field
*event_common_context
;
166 bt_field
*event_spec_context
;
167 bt_field
*event_payload
;
173 /* Current medium buffer data */
175 /* Last address provided by medium */
178 /* Buffer size provided by medium (bytes) */
181 /* Offset within whole packet of addr (bits) */
182 size_t packet_offset
;
184 /* Current position from addr (bits) */
187 /* Position of the last event header from addr (bits) */
191 /* Binary type reader */
192 struct bt_bfcr
*bfcr
;
194 /* Current medium data */
196 struct bt_msg_iter_medium_ops medops
;
197 size_t max_request_sz
;
201 /* Current packet size (bits) (-1 if unknown) */
202 int64_t cur_exp_packet_total_size
;
204 /* Current content size (bits) (-1 if unknown) */
205 int64_t cur_exp_packet_content_size
;
207 /* Current stream class ID */
208 int64_t cur_stream_class_id
;
210 /* Current event class ID */
211 int64_t cur_event_class_id
;
213 /* Current data stream ID */
214 int64_t cur_data_stream_id
;
217 * Offset, in the underlying media, of the current packet's
218 * start (-1 if unknown).
220 off_t cur_packet_offset
;
222 /* Default clock's current value */
223 uint64_t default_clock_snapshot
;
225 /* End of packet snapshots */
227 uint64_t discarded_events
;
229 uint64_t beginning_clock
;
233 /* Stored values (for sequence lengths, variant tags) */
234 GArray
*stored_values
;
238 const char *state_string(enum state state
)
243 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
244 return "STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN";
245 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
246 return "STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE";
247 case STATE_AFTER_TRACE_PACKET_HEADER
:
248 return "STATE_AFTER_TRACE_PACKET_HEADER";
249 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
250 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN";
251 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
252 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE";
253 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
254 return "STATE_AFTER_STREAM_PACKET_CONTEXT";
255 case STATE_EMIT_MSG_STREAM_BEGINNING
:
256 return "STATE_EMIT_MSG_STREAM_BEGINNING";
257 case STATE_EMIT_MSG_STREAM_ACTIVITY_BEGINNING
:
258 return "STATE_EMIT_MSG_STREAM_ACTIVITY_BEGINNING";
259 case STATE_EMIT_MSG_PACKET_BEGINNING
:
260 return "STATE_EMIT_MSG_PACKET_BEGINNING";
261 case STATE_DSCOPE_EVENT_HEADER_BEGIN
:
262 return "STATE_DSCOPE_EVENT_HEADER_BEGIN";
263 case STATE_DSCOPE_EVENT_HEADER_CONTINUE
:
264 return "STATE_DSCOPE_EVENT_HEADER_CONTINUE";
265 case STATE_AFTER_EVENT_HEADER
:
266 return "STATE_AFTER_EVENT_HEADER";
267 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
:
268 return "STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN";
269 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
:
270 return "STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE";
271 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
:
272 return "STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN";
273 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
:
274 return "STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE";
275 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
276 return "STATE_DSCOPE_EVENT_PAYLOAD_BEGIN";
277 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
278 return "STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE";
279 case STATE_EMIT_MSG_EVENT
:
280 return "STATE_EMIT_MSG_EVENT";
281 case STATE_SKIP_PACKET_PADDING
:
282 return "STATE_SKIP_PACKET_PADDING";
283 case STATE_EMIT_MSG_PACKET_END_MULTI
:
284 return "STATE_EMIT_MSG_PACKET_END_MULTI";
285 case STATE_EMIT_MSG_PACKET_END_SINGLE
:
286 return "STATE_EMIT_MSG_PACKET_END_SINGLE";
287 case STATE_EMIT_MSG_STREAM_ACTIVITY_END
:
288 return "STATE_EMIT_MSG_STREAM_ACTIVITY_END";
289 case STATE_EMIT_MSG_STREAM_END
:
290 return "STATE_EMIT_MSG_STREAM_END";
299 int bt_msg_iter_switch_packet(struct bt_msg_iter
*notit
);
302 struct stack
*stack_new(struct bt_msg_iter
*notit
)
304 struct stack
*stack
= NULL
;
306 stack
= g_new0(struct stack
, 1);
308 BT_LOGE_STR("Failed to allocate one stack.");
312 stack
->entries
= g_array_new(FALSE
, TRUE
, sizeof(struct stack_entry
));
313 if (!stack
->entries
) {
314 BT_LOGE_STR("Failed to allocate a GArray.");
318 BT_LOGD("Created stack: notit-addr=%p, stack-addr=%p", notit
, stack
);
330 void stack_destroy(struct stack
*stack
)
333 BT_LOGD("Destroying stack: addr=%p", stack
);
335 if (stack
->entries
) {
336 g_array_free(stack
->entries
, TRUE
);
343 void stack_push(struct stack
*stack
, bt_field
*base
)
345 struct stack_entry
*entry
;
349 BT_LOGV("Pushing base field on stack: stack-addr=%p, "
350 "stack-size-before=%zu, stack-size-after=%zu",
351 stack
, stack
->size
, stack
->size
+ 1);
353 if (stack
->entries
->len
== stack
->size
) {
354 g_array_set_size(stack
->entries
, stack
->size
+ 1);
357 entry
= &g_array_index(stack
->entries
, struct stack_entry
, stack
->size
);
364 unsigned int stack_size(struct stack
*stack
)
371 void stack_pop(struct stack
*stack
)
374 BT_ASSERT(stack_size(stack
));
375 BT_LOGV("Popping from stack: "
376 "stack-addr=%p, stack-size-before=%zu, stack-size-after=%zu",
377 stack
, stack
->size
, stack
->size
- 1);
382 struct stack_entry
*stack_top(struct stack
*stack
)
385 BT_ASSERT(stack_size(stack
));
386 return &g_array_index(stack
->entries
, struct stack_entry
,
391 bool stack_empty(struct stack
*stack
)
393 return stack_size(stack
) == 0;
397 void stack_clear(struct stack
*stack
)
404 enum bt_msg_iter_status
msg_iter_status_from_m_status(
405 enum bt_msg_iter_medium_status m_status
)
407 /* They are the same */
408 return (int) m_status
;
412 size_t buf_size_bits(struct bt_msg_iter
*notit
)
414 return notit
->buf
.sz
* 8;
418 size_t buf_available_bits(struct bt_msg_iter
*notit
)
420 return buf_size_bits(notit
) - notit
->buf
.at
;
424 size_t packet_at(struct bt_msg_iter
*notit
)
426 return notit
->buf
.packet_offset
+ notit
->buf
.at
;
430 void buf_consume_bits(struct bt_msg_iter
*notit
, size_t incr
)
432 BT_LOGV("Advancing cursor: notit-addr=%p, cur-before=%zu, cur-after=%zu",
433 notit
, notit
->buf
.at
, notit
->buf
.at
+ incr
);
434 notit
->buf
.at
+= incr
;
438 enum bt_msg_iter_status
request_medium_bytes(
439 struct bt_msg_iter
*notit
)
441 uint8_t *buffer_addr
= NULL
;
442 size_t buffer_sz
= 0;
443 enum bt_msg_iter_medium_status m_status
;
445 BT_LOGV("Calling user function (request bytes): notit-addr=%p, "
446 "request-size=%zu", notit
, notit
->medium
.max_request_sz
);
447 m_status
= notit
->medium
.medops
.request_bytes(
448 notit
->medium
.max_request_sz
, &buffer_addr
,
449 &buffer_sz
, notit
->medium
.data
);
450 BT_LOGV("User function returned: status=%s, buf-addr=%p, buf-size=%zu",
451 bt_msg_iter_medium_status_string(m_status
),
452 buffer_addr
, buffer_sz
);
453 if (m_status
== BT_MSG_ITER_MEDIUM_STATUS_OK
) {
454 BT_ASSERT(buffer_sz
!= 0);
456 /* New packet offset is old one + old size (in bits) */
457 notit
->buf
.packet_offset
+= buf_size_bits(notit
);
459 /* Restart at the beginning of the new medium buffer */
461 notit
->buf
.last_eh_at
= SIZE_MAX
;
463 /* New medium buffer size */
464 notit
->buf
.sz
= buffer_sz
;
466 /* New medium buffer address */
467 notit
->buf
.addr
= buffer_addr
;
469 BT_LOGV("User function returned new bytes: "
470 "packet-offset=%zu, cur=%zu, size=%zu, addr=%p",
471 notit
->buf
.packet_offset
, notit
->buf
.at
,
472 notit
->buf
.sz
, notit
->buf
.addr
);
473 BT_LOGV_MEM(buffer_addr
, buffer_sz
, "Returned bytes at %p:",
475 } else if (m_status
== BT_MSG_ITER_MEDIUM_STATUS_EOF
) {
477 * User returned end of stream: validate that we're not
478 * in the middle of a packet header, packet context, or
481 if (notit
->cur_exp_packet_total_size
>= 0) {
482 if (packet_at(notit
) ==
483 notit
->cur_exp_packet_total_size
) {
487 if (packet_at(notit
) == 0) {
491 if (notit
->buf
.last_eh_at
!= SIZE_MAX
&&
492 notit
->buf
.at
== notit
->buf
.last_eh_at
) {
497 /* All other states are invalid */
498 BT_LOGW("User function returned %s, but message iterator is in an unexpected state: "
499 "state=%s, cur-packet-size=%" PRId64
", cur=%zu, "
500 "packet-cur=%zu, last-eh-at=%zu",
501 bt_msg_iter_medium_status_string(m_status
),
502 state_string(notit
->state
),
503 notit
->cur_exp_packet_total_size
,
504 notit
->buf
.at
, packet_at(notit
),
505 notit
->buf
.last_eh_at
);
506 m_status
= BT_MSG_ITER_MEDIUM_STATUS_ERROR
;
507 } else if (m_status
< 0) {
508 BT_LOGW("User function failed: status=%s",
509 bt_msg_iter_medium_status_string(m_status
));
513 return msg_iter_status_from_m_status(m_status
);
517 enum bt_msg_iter_status
buf_ensure_available_bits(
518 struct bt_msg_iter
*notit
)
520 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
522 if (unlikely(buf_available_bits(notit
) == 0)) {
524 * This _cannot_ return BT_MSG_ITER_STATUS_OK
527 status
= request_medium_bytes(notit
);
534 enum bt_msg_iter_status
read_dscope_begin_state(
535 struct bt_msg_iter
*notit
,
536 struct ctf_field_class
*dscope_fc
,
537 enum state done_state
, enum state continue_state
,
538 bt_field
*dscope_field
)
540 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
541 enum bt_bfcr_status bfcr_status
;
542 size_t consumed_bits
;
544 notit
->cur_dscope_field
= dscope_field
;
545 BT_LOGV("Starting BFCR: notit-addr=%p, bfcr-addr=%p, fc-addr=%p",
546 notit
, notit
->bfcr
, dscope_fc
);
547 consumed_bits
= bt_bfcr_start(notit
->bfcr
, dscope_fc
,
548 notit
->buf
.addr
, notit
->buf
.at
, packet_at(notit
),
549 notit
->buf
.sz
, &bfcr_status
);
550 BT_LOGV("BFCR consumed bits: size=%zu", consumed_bits
);
552 switch (bfcr_status
) {
553 case BT_BFCR_STATUS_OK
:
554 /* Field class was read completely */
555 BT_LOGV_STR("Field was completely decoded.");
556 notit
->state
= done_state
;
558 case BT_BFCR_STATUS_EOF
:
559 BT_LOGV_STR("BFCR needs more data to decode field completely.");
560 notit
->state
= continue_state
;
563 BT_LOGW("BFCR failed to start: notit-addr=%p, bfcr-addr=%p, "
564 "status=%s", notit
, notit
->bfcr
,
565 bt_bfcr_status_string(bfcr_status
));
566 status
= BT_MSG_ITER_STATUS_ERROR
;
570 /* Consume bits now since we know we're not in an error state */
571 buf_consume_bits(notit
, consumed_bits
);
578 enum bt_msg_iter_status
read_dscope_continue_state(
579 struct bt_msg_iter
*notit
, enum state done_state
)
581 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
582 enum bt_bfcr_status bfcr_status
;
583 size_t consumed_bits
;
585 BT_LOGV("Continuing BFCR: notit-addr=%p, bfcr-addr=%p",
588 status
= buf_ensure_available_bits(notit
);
589 if (status
!= BT_MSG_ITER_STATUS_OK
) {
591 BT_LOGW("Cannot ensure that buffer has at least one byte: "
592 "msg-addr=%p, status=%s",
593 notit
, bt_msg_iter_status_string(status
));
595 BT_LOGV("Cannot ensure that buffer has at least one byte: "
596 "msg-addr=%p, status=%s",
597 notit
, bt_msg_iter_status_string(status
));
603 consumed_bits
= bt_bfcr_continue(notit
->bfcr
, notit
->buf
.addr
,
604 notit
->buf
.sz
, &bfcr_status
);
605 BT_LOGV("BFCR consumed bits: size=%zu", consumed_bits
);
607 switch (bfcr_status
) {
608 case BT_BFCR_STATUS_OK
:
609 /* Type was read completely. */
610 BT_LOGV_STR("Field was completely decoded.");
611 notit
->state
= done_state
;
613 case BT_BFCR_STATUS_EOF
:
614 /* Stay in this continue state. */
615 BT_LOGV_STR("BFCR needs more data to decode field completely.");
618 BT_LOGW("BFCR failed to continue: notit-addr=%p, bfcr-addr=%p, "
619 "status=%s", notit
, notit
->bfcr
,
620 bt_bfcr_status_string(bfcr_status
));
621 status
= BT_MSG_ITER_STATUS_ERROR
;
625 /* Consume bits now since we know we're not in an error state. */
626 buf_consume_bits(notit
, consumed_bits
);
632 void release_event_dscopes(struct bt_msg_iter
*notit
)
634 notit
->dscopes
.event_common_context
= NULL
;
635 notit
->dscopes
.event_spec_context
= NULL
;
636 notit
->dscopes
.event_payload
= NULL
;
640 void release_all_dscopes(struct bt_msg_iter
*notit
)
642 notit
->dscopes
.stream_packet_context
= NULL
;
644 if (notit
->packet_context_field
) {
645 bt_packet_context_field_release(notit
->packet_context_field
);
646 notit
->packet_context_field
= NULL
;
649 release_event_dscopes(notit
);
653 enum bt_msg_iter_status
read_packet_header_begin_state(
654 struct bt_msg_iter
*notit
)
656 struct ctf_field_class
*packet_header_fc
= NULL
;
657 enum bt_msg_iter_status ret
= BT_MSG_ITER_STATUS_OK
;
659 if (bt_msg_iter_switch_packet(notit
)) {
660 BT_LOGW("Cannot switch packet: notit-addr=%p", notit
);
661 ret
= BT_MSG_ITER_STATUS_ERROR
;
666 * Make sure at least one bit is available for this packet. An
667 * empty packet is impossible. If we reach the end of the medium
668 * at this point, then it's considered the end of the stream.
670 ret
= buf_ensure_available_bits(notit
);
672 case BT_MSG_ITER_STATUS_OK
:
674 case BT_MSG_ITER_STATUS_EOF
:
675 ret
= BT_MSG_ITER_STATUS_OK
;
676 notit
->state
= STATE_EMIT_MSG_STREAM_ACTIVITY_END
;
682 /* Packet header class is common to the whole trace class. */
683 packet_header_fc
= notit
->meta
.tc
->packet_header_fc
;
684 if (!packet_header_fc
) {
685 notit
->state
= STATE_AFTER_TRACE_PACKET_HEADER
;
689 notit
->cur_stream_class_id
= -1;
690 notit
->cur_event_class_id
= -1;
691 notit
->cur_data_stream_id
= -1;
692 BT_LOGV("Decoding packet header field:"
693 "notit-addr=%p, trace-class-addr=%p, fc-addr=%p",
694 notit
, notit
->meta
.tc
, packet_header_fc
);
695 ret
= read_dscope_begin_state(notit
, packet_header_fc
,
696 STATE_AFTER_TRACE_PACKET_HEADER
,
697 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
, NULL
);
699 BT_LOGW("Cannot decode packet header field: "
700 "notit-addr=%p, trace-class-addr=%p, "
702 notit
, notit
->meta
.tc
, packet_header_fc
);
710 enum bt_msg_iter_status
read_packet_header_continue_state(
711 struct bt_msg_iter
*notit
)
713 return read_dscope_continue_state(notit
,
714 STATE_AFTER_TRACE_PACKET_HEADER
);
718 enum bt_msg_iter_status
set_current_stream_class(struct bt_msg_iter
*notit
)
720 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
721 struct ctf_stream_class
*new_stream_class
= NULL
;
723 if (notit
->cur_stream_class_id
== -1) {
725 * No current stream class ID field, therefore only one
728 if (notit
->meta
.tc
->stream_classes
->len
!= 1) {
729 BT_LOGW("Need exactly one stream class since there's "
730 "no stream class ID field: "
731 "notit-addr=%p", notit
);
732 status
= BT_MSG_ITER_STATUS_ERROR
;
736 new_stream_class
= notit
->meta
.tc
->stream_classes
->pdata
[0];
737 notit
->cur_stream_class_id
= new_stream_class
->id
;
740 new_stream_class
= ctf_trace_class_borrow_stream_class_by_id(
741 notit
->meta
.tc
, notit
->cur_stream_class_id
);
742 if (!new_stream_class
) {
743 BT_LOGW("No stream class with ID of stream class ID to use in trace class: "
744 "notit-addr=%p, stream-class-id=%" PRIu64
", "
745 "trace-class-addr=%p",
746 notit
, notit
->cur_stream_class_id
, notit
->meta
.tc
);
747 status
= BT_MSG_ITER_STATUS_ERROR
;
751 if (notit
->meta
.sc
) {
752 if (new_stream_class
!= notit
->meta
.sc
) {
753 BT_LOGW("Two packets refer to two different stream classes within the same packet sequence: "
754 "notit-addr=%p, prev-stream-class-addr=%p, "
755 "prev-stream-class-id=%" PRId64
", "
756 "next-stream-class-addr=%p, "
757 "next-stream-class-id=%" PRId64
", "
759 notit
, notit
->meta
.sc
,
762 new_stream_class
->id
,
764 status
= BT_MSG_ITER_STATUS_ERROR
;
768 notit
->meta
.sc
= new_stream_class
;
771 BT_LOGV("Set current stream class: "
772 "notit-addr=%p, stream-class-addr=%p, "
773 "stream-class-id=%" PRId64
,
774 notit
, notit
->meta
.sc
, notit
->meta
.sc
->id
);
781 enum bt_msg_iter_status
set_current_stream(struct bt_msg_iter
*notit
)
783 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
784 bt_stream
*stream
= NULL
;
786 BT_LOGV("Calling user function (get stream): notit-addr=%p, "
787 "stream-class-addr=%p, stream-class-id=%" PRId64
,
788 notit
, notit
->meta
.sc
,
790 stream
= notit
->medium
.medops
.borrow_stream(
791 notit
->meta
.sc
->ir_sc
, notit
->cur_data_stream_id
,
793 bt_stream_get_ref(stream
);
794 BT_LOGV("User function returned: stream-addr=%p", stream
);
796 BT_LOGW_STR("User function failed to return a stream object "
797 "for the given stream class.");
798 status
= BT_MSG_ITER_STATUS_ERROR
;
802 if (notit
->stream
&& stream
!= notit
->stream
) {
803 BT_LOGW("User function returned a different stream than the "
804 "previous one for the same sequence of packets.");
805 status
= BT_MSG_ITER_STATUS_ERROR
;
809 BT_STREAM_MOVE_REF(notit
->stream
, stream
);
812 bt_stream_put_ref(stream
);
817 enum bt_msg_iter_status
set_current_packet(struct bt_msg_iter
*notit
)
819 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
820 bt_packet
*packet
= NULL
;
822 BT_LOGV("Creating packet for packet message: "
823 "notit-addr=%p", notit
);
824 BT_LOGV("Creating packet from stream: "
825 "notit-addr=%p, stream-addr=%p, "
826 "stream-class-addr=%p, "
827 "stream-class-id=%" PRId64
,
828 notit
, notit
->stream
, notit
->meta
.sc
,
832 BT_ASSERT(notit
->stream
);
833 packet
= bt_packet_create(notit
->stream
);
835 BT_LOGE("Cannot create packet from stream: "
836 "notit-addr=%p, stream-addr=%p, "
837 "stream-class-addr=%p, "
838 "stream-class-id=%" PRId64
,
839 notit
, notit
->stream
, notit
->meta
.sc
,
847 BT_PACKET_PUT_REF_AND_RESET(packet
);
848 status
= BT_MSG_ITER_STATUS_ERROR
;
851 BT_PACKET_MOVE_REF(notit
->packet
, packet
);
856 enum bt_msg_iter_status
after_packet_header_state(
857 struct bt_msg_iter
*notit
)
859 enum bt_msg_iter_status status
;
861 status
= set_current_stream_class(notit
);
862 if (status
!= BT_MSG_ITER_STATUS_OK
) {
866 notit
->state
= STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
;
873 enum bt_msg_iter_status
read_packet_context_begin_state(
874 struct bt_msg_iter
*notit
)
876 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
877 struct ctf_field_class
*packet_context_fc
;
879 BT_ASSERT(notit
->meta
.sc
);
880 packet_context_fc
= notit
->meta
.sc
->packet_context_fc
;
881 if (!packet_context_fc
) {
882 BT_LOGV("No packet packet context field class in stream class: continuing: "
883 "notit-addr=%p, stream-class-addr=%p, "
884 "stream-class-id=%" PRId64
,
885 notit
, notit
->meta
.sc
,
887 notit
->state
= STATE_AFTER_STREAM_PACKET_CONTEXT
;
891 BT_ASSERT(!notit
->packet_context_field
);
893 if (packet_context_fc
->in_ir
) {
895 * Create free packet context field from stream class.
896 * This field is going to be moved to the packet once we
897 * create it. We cannot create the packet now because a
898 * packet is created from a stream, and this API must be
899 * able to return the packet context properties without
901 * (bt_msg_iter_get_packet_properties()).
903 notit
->packet_context_field
=
904 bt_packet_context_field_create(
905 notit
->meta
.sc
->ir_sc
);
906 if (!notit
->packet_context_field
) {
907 BT_LOGE_STR("Cannot create packet context field wrapper from stream class.");
908 status
= BT_MSG_ITER_STATUS_ERROR
;
912 notit
->dscopes
.stream_packet_context
=
913 bt_packet_context_field_borrow_field(
914 notit
->packet_context_field
);
915 BT_ASSERT(notit
->dscopes
.stream_packet_context
);
918 BT_LOGV("Decoding packet context field: "
919 "notit-addr=%p, stream-class-addr=%p, "
920 "stream-class-id=%" PRId64
", fc-addr=%p",
921 notit
, notit
->meta
.sc
,
922 notit
->meta
.sc
->id
, packet_context_fc
);
923 status
= read_dscope_begin_state(notit
, packet_context_fc
,
924 STATE_AFTER_STREAM_PACKET_CONTEXT
,
925 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
926 notit
->dscopes
.stream_packet_context
);
928 BT_LOGW("Cannot decode packet context field: "
929 "notit-addr=%p, stream-class-addr=%p, "
930 "stream-class-id=%" PRId64
", fc-addr=%p",
931 notit
, notit
->meta
.sc
,
941 enum bt_msg_iter_status
read_packet_context_continue_state(
942 struct bt_msg_iter
*notit
)
944 return read_dscope_continue_state(notit
,
945 STATE_AFTER_STREAM_PACKET_CONTEXT
);
949 enum bt_msg_iter_status
set_current_packet_content_sizes(
950 struct bt_msg_iter
*notit
)
952 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
954 if (notit
->cur_exp_packet_total_size
== -1) {
955 if (notit
->cur_exp_packet_content_size
!= -1) {
956 BT_LOGW("Content size is set, but packet size is not: "
957 "notit-addr=%p, packet-context-field-addr=%p, "
958 "packet-size=%" PRId64
", content-size=%" PRId64
,
959 notit
, notit
->dscopes
.stream_packet_context
,
960 notit
->cur_exp_packet_total_size
,
961 notit
->cur_exp_packet_content_size
);
962 status
= BT_MSG_ITER_STATUS_ERROR
;
966 if (notit
->cur_exp_packet_content_size
== -1) {
967 notit
->cur_exp_packet_content_size
=
968 notit
->cur_exp_packet_total_size
;
972 if (notit
->cur_exp_packet_content_size
>
973 notit
->cur_exp_packet_total_size
) {
974 BT_LOGW("Invalid packet or content size: "
975 "content size is greater than packet size: "
976 "notit-addr=%p, packet-context-field-addr=%p, "
977 "packet-size=%" PRId64
", content-size=%" PRId64
,
978 notit
, notit
->dscopes
.stream_packet_context
,
979 notit
->cur_exp_packet_total_size
,
980 notit
->cur_exp_packet_content_size
);
981 status
= BT_MSG_ITER_STATUS_ERROR
;
985 BT_LOGV("Set current packet and content sizes: "
986 "notit-addr=%p, packet-size=%" PRIu64
", content-size=%" PRIu64
,
987 notit
, notit
->cur_exp_packet_total_size
,
988 notit
->cur_exp_packet_content_size
);
995 enum bt_msg_iter_status
after_packet_context_state(struct bt_msg_iter
*notit
)
997 enum bt_msg_iter_status status
;
999 status
= set_current_packet_content_sizes(notit
);
1000 if (status
!= BT_MSG_ITER_STATUS_OK
) {
1004 if (notit
->stream
) {
1006 * Stream exists, which means we already emitted at
1007 * least one packet beginning message, so the initial
1008 * stream beginning message was also emitted.
1010 notit
->state
= STATE_EMIT_MSG_PACKET_BEGINNING
;
1012 notit
->state
= STATE_EMIT_MSG_STREAM_BEGINNING
;
1020 enum bt_msg_iter_status
read_event_header_begin_state(struct bt_msg_iter
*notit
)
1022 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1023 struct ctf_field_class
*event_header_fc
= NULL
;
1025 /* Reset the position of the last event header */
1026 notit
->buf
.last_eh_at
= notit
->buf
.at
;
1027 notit
->cur_event_class_id
= -1;
1029 /* Check if we have some content left */
1030 if (notit
->cur_exp_packet_content_size
>= 0) {
1031 if (unlikely(packet_at(notit
) ==
1032 notit
->cur_exp_packet_content_size
)) {
1033 /* No more events! */
1034 BT_LOGV("Reached end of packet: notit-addr=%p, "
1035 "cur=%zu", notit
, packet_at(notit
));
1036 notit
->state
= STATE_EMIT_MSG_PACKET_END_MULTI
;
1038 } else if (unlikely(packet_at(notit
) >
1039 notit
->cur_exp_packet_content_size
)) {
1040 /* That's not supposed to happen */
1041 BT_LOGV("Before decoding event header field: cursor is passed the packet's content: "
1042 "notit-addr=%p, content-size=%" PRId64
", "
1044 notit
->cur_exp_packet_content_size
,
1046 status
= BT_MSG_ITER_STATUS_ERROR
;
1051 * "Infinite" content: we're done when the medium has
1052 * nothing else for us.
1054 status
= buf_ensure_available_bits(notit
);
1056 case BT_MSG_ITER_STATUS_OK
:
1058 case BT_MSG_ITER_STATUS_EOF
:
1059 status
= BT_MSG_ITER_STATUS_OK
;
1060 notit
->state
= STATE_EMIT_MSG_PACKET_END_SINGLE
;
1067 release_event_dscopes(notit
);
1068 BT_ASSERT(notit
->meta
.sc
);
1069 event_header_fc
= notit
->meta
.sc
->event_header_fc
;
1070 if (!event_header_fc
) {
1071 notit
->state
= STATE_AFTER_EVENT_HEADER
;
1075 BT_LOGV("Decoding event header field: "
1076 "notit-addr=%p, stream-class-addr=%p, "
1077 "stream-class-id=%" PRId64
", "
1079 notit
, notit
->meta
.sc
,
1082 status
= read_dscope_begin_state(notit
, event_header_fc
,
1083 STATE_AFTER_EVENT_HEADER
,
1084 STATE_DSCOPE_EVENT_HEADER_CONTINUE
, NULL
);
1086 BT_LOGW("Cannot decode event header field: "
1087 "notit-addr=%p, stream-class-addr=%p, "
1088 "stream-class-id=%" PRId64
", fc-addr=%p",
1089 notit
, notit
->meta
.sc
,
1099 enum bt_msg_iter_status
read_event_header_continue_state(
1100 struct bt_msg_iter
*notit
)
1102 return read_dscope_continue_state(notit
,
1103 STATE_AFTER_EVENT_HEADER
);
1107 enum bt_msg_iter_status
set_current_event_class(struct bt_msg_iter
*notit
)
1109 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1111 struct ctf_event_class
*new_event_class
= NULL
;
1113 if (notit
->cur_event_class_id
== -1) {
1115 * No current event class ID field, therefore only one
1118 if (notit
->meta
.sc
->event_classes
->len
!= 1) {
1119 BT_LOGW("Need exactly one event class since there's "
1120 "no event class ID field: "
1121 "notit-addr=%p", notit
);
1122 status
= BT_MSG_ITER_STATUS_ERROR
;
1126 new_event_class
= notit
->meta
.sc
->event_classes
->pdata
[0];
1127 notit
->cur_event_class_id
= new_event_class
->id
;
1130 new_event_class
= ctf_stream_class_borrow_event_class_by_id(
1131 notit
->meta
.sc
, notit
->cur_event_class_id
);
1132 if (!new_event_class
) {
1133 BT_LOGW("No event class with ID of event class ID to use in stream class: "
1134 "notit-addr=%p, stream-class-id=%" PRIu64
", "
1135 "event-class-id=%" PRIu64
", "
1136 "trace-class-addr=%p",
1137 notit
, notit
->meta
.sc
->id
, notit
->cur_event_class_id
,
1139 status
= BT_MSG_ITER_STATUS_ERROR
;
1143 notit
->meta
.ec
= new_event_class
;
1144 BT_LOGV("Set current event class: "
1145 "notit-addr=%p, event-class-addr=%p, "
1146 "event-class-id=%" PRId64
", "
1147 "event-class-name=\"%s\"",
1148 notit
, notit
->meta
.ec
, notit
->meta
.ec
->id
,
1149 notit
->meta
.ec
->name
->str
);
1156 enum bt_msg_iter_status
set_current_event_message(
1157 struct bt_msg_iter
*notit
)
1159 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1160 bt_message
*msg
= NULL
;
1162 BT_ASSERT(notit
->meta
.ec
);
1163 BT_ASSERT(notit
->packet
);
1164 BT_LOGV("Creating event message from event class and packet: "
1165 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", packet-addr=%p",
1166 notit
, notit
->meta
.ec
,
1167 notit
->meta
.ec
->name
->str
,
1169 BT_ASSERT(notit
->msg_iter
);
1170 msg
= bt_message_event_create(notit
->msg_iter
,
1171 notit
->meta
.ec
->ir_ec
, notit
->packet
);
1173 BT_LOGE("Cannot create event message: "
1174 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", "
1176 notit
, notit
->meta
.ec
,
1177 notit
->meta
.ec
->name
->str
,
1185 BT_MESSAGE_PUT_REF_AND_RESET(msg
);
1186 status
= BT_MSG_ITER_STATUS_ERROR
;
1189 BT_MESSAGE_MOVE_REF(notit
->event_msg
, msg
);
1194 enum bt_msg_iter_status
after_event_header_state(
1195 struct bt_msg_iter
*notit
)
1197 enum bt_msg_iter_status status
;
1199 status
= set_current_event_class(notit
);
1200 if (status
!= BT_MSG_ITER_STATUS_OK
) {
1204 status
= set_current_event_message(notit
);
1205 if (status
!= BT_MSG_ITER_STATUS_OK
) {
1209 notit
->event
= bt_message_event_borrow_event(
1211 BT_ASSERT(notit
->event
);
1212 notit
->state
= STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
;
1219 enum bt_msg_iter_status
read_event_common_context_begin_state(
1220 struct bt_msg_iter
*notit
)
1222 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1223 struct ctf_field_class
*event_common_context_fc
;
1225 event_common_context_fc
= notit
->meta
.sc
->event_common_context_fc
;
1226 if (!event_common_context_fc
) {
1227 notit
->state
= STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
;
1231 if (event_common_context_fc
->in_ir
) {
1232 BT_ASSERT(!notit
->dscopes
.event_common_context
);
1233 notit
->dscopes
.event_common_context
=
1234 bt_event_borrow_common_context_field(
1236 BT_ASSERT(notit
->dscopes
.event_common_context
);
1239 BT_LOGV("Decoding event common context field: "
1240 "notit-addr=%p, stream-class-addr=%p, "
1241 "stream-class-id=%" PRId64
", "
1243 notit
, notit
->meta
.sc
,
1245 event_common_context_fc
);
1246 status
= read_dscope_begin_state(notit
, event_common_context_fc
,
1247 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
,
1248 STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
,
1249 notit
->dscopes
.event_common_context
);
1251 BT_LOGW("Cannot decode event common context field: "
1252 "notit-addr=%p, stream-class-addr=%p, "
1253 "stream-class-id=%" PRId64
", fc-addr=%p",
1254 notit
, notit
->meta
.sc
,
1256 event_common_context_fc
);
1264 enum bt_msg_iter_status
read_event_common_context_continue_state(
1265 struct bt_msg_iter
*notit
)
1267 return read_dscope_continue_state(notit
,
1268 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
);
1272 enum bt_msg_iter_status
read_event_spec_context_begin_state(
1273 struct bt_msg_iter
*notit
)
1275 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1276 struct ctf_field_class
*event_spec_context_fc
;
1278 event_spec_context_fc
= notit
->meta
.ec
->spec_context_fc
;
1279 if (!event_spec_context_fc
) {
1280 notit
->state
= STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
;
1284 if (event_spec_context_fc
->in_ir
) {
1285 BT_ASSERT(!notit
->dscopes
.event_spec_context
);
1286 notit
->dscopes
.event_spec_context
=
1287 bt_event_borrow_specific_context_field(
1289 BT_ASSERT(notit
->dscopes
.event_spec_context
);
1292 BT_LOGV("Decoding event specific context field: "
1293 "notit-addr=%p, event-class-addr=%p, "
1294 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1296 notit
, notit
->meta
.ec
,
1297 notit
->meta
.ec
->name
->str
,
1299 event_spec_context_fc
);
1300 status
= read_dscope_begin_state(notit
, event_spec_context_fc
,
1301 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
1302 STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
,
1303 notit
->dscopes
.event_spec_context
);
1305 BT_LOGW("Cannot decode event specific context field: "
1306 "notit-addr=%p, event-class-addr=%p, "
1307 "event-class-name=\"%s\", "
1308 "event-class-id=%" PRId64
", fc-addr=%p",
1309 notit
, notit
->meta
.ec
,
1310 notit
->meta
.ec
->name
->str
,
1312 event_spec_context_fc
);
1320 enum bt_msg_iter_status
read_event_spec_context_continue_state(
1321 struct bt_msg_iter
*notit
)
1323 return read_dscope_continue_state(notit
,
1324 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
);
1328 enum bt_msg_iter_status
read_event_payload_begin_state(
1329 struct bt_msg_iter
*notit
)
1331 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1332 struct ctf_field_class
*event_payload_fc
;
1334 event_payload_fc
= notit
->meta
.ec
->payload_fc
;
1335 if (!event_payload_fc
) {
1336 notit
->state
= STATE_EMIT_MSG_EVENT
;
1340 if (event_payload_fc
->in_ir
) {
1341 BT_ASSERT(!notit
->dscopes
.event_payload
);
1342 notit
->dscopes
.event_payload
=
1343 bt_event_borrow_payload_field(
1345 BT_ASSERT(notit
->dscopes
.event_payload
);
1348 BT_LOGV("Decoding event payload field: "
1349 "notit-addr=%p, event-class-addr=%p, "
1350 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1352 notit
, notit
->meta
.ec
,
1353 notit
->meta
.ec
->name
->str
,
1356 status
= read_dscope_begin_state(notit
, event_payload_fc
,
1357 STATE_EMIT_MSG_EVENT
,
1358 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
1359 notit
->dscopes
.event_payload
);
1361 BT_LOGW("Cannot decode event payload field: "
1362 "notit-addr=%p, event-class-addr=%p, "
1363 "event-class-name=\"%s\", "
1364 "event-class-id=%" PRId64
", fc-addr=%p",
1365 notit
, notit
->meta
.ec
,
1366 notit
->meta
.ec
->name
->str
,
1376 enum bt_msg_iter_status
read_event_payload_continue_state(
1377 struct bt_msg_iter
*notit
)
1379 return read_dscope_continue_state(notit
, STATE_EMIT_MSG_EVENT
);
1383 enum bt_msg_iter_status
skip_packet_padding_state(struct bt_msg_iter
*notit
)
1385 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1386 size_t bits_to_skip
;
1388 BT_ASSERT(notit
->cur_exp_packet_total_size
> 0);
1389 bits_to_skip
= notit
->cur_exp_packet_total_size
- packet_at(notit
);
1390 if (bits_to_skip
== 0) {
1391 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1394 size_t bits_to_consume
;
1396 BT_LOGV("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu",
1397 bits_to_skip
, notit
, bits_to_skip
);
1398 status
= buf_ensure_available_bits(notit
);
1399 if (status
!= BT_MSG_ITER_STATUS_OK
) {
1403 bits_to_consume
= MIN(buf_available_bits(notit
), bits_to_skip
);
1404 BT_LOGV("Skipping %zu bits of padding: notit-addr=%p, size=%zu",
1405 bits_to_consume
, notit
, bits_to_consume
);
1406 buf_consume_bits(notit
, bits_to_consume
);
1407 bits_to_skip
= notit
->cur_exp_packet_total_size
-
1409 if (bits_to_skip
== 0) {
1410 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1420 enum bt_msg_iter_status
handle_state(struct bt_msg_iter
*notit
)
1422 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
1423 const enum state state
= notit
->state
;
1425 BT_LOGV("Handling state: notit-addr=%p, state=%s",
1426 notit
, state_string(state
));
1428 // TODO: optimalize!
1431 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1433 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
1434 status
= read_packet_header_begin_state(notit
);
1436 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
1437 status
= read_packet_header_continue_state(notit
);
1439 case STATE_AFTER_TRACE_PACKET_HEADER
:
1440 status
= after_packet_header_state(notit
);
1442 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
1443 status
= read_packet_context_begin_state(notit
);
1445 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
1446 status
= read_packet_context_continue_state(notit
);
1448 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
1449 status
= after_packet_context_state(notit
);
1451 case STATE_EMIT_MSG_STREAM_BEGINNING
:
1452 notit
->state
= STATE_EMIT_MSG_STREAM_ACTIVITY_BEGINNING
;
1454 case STATE_EMIT_MSG_STREAM_ACTIVITY_BEGINNING
:
1455 notit
->state
= STATE_EMIT_MSG_PACKET_BEGINNING
;
1457 case STATE_EMIT_MSG_PACKET_BEGINNING
:
1458 notit
->state
= STATE_DSCOPE_EVENT_HEADER_BEGIN
;
1460 case STATE_DSCOPE_EVENT_HEADER_BEGIN
:
1461 status
= read_event_header_begin_state(notit
);
1463 case STATE_DSCOPE_EVENT_HEADER_CONTINUE
:
1464 status
= read_event_header_continue_state(notit
);
1466 case STATE_AFTER_EVENT_HEADER
:
1467 status
= after_event_header_state(notit
);
1469 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
:
1470 status
= read_event_common_context_begin_state(notit
);
1472 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
:
1473 status
= read_event_common_context_continue_state(notit
);
1475 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
:
1476 status
= read_event_spec_context_begin_state(notit
);
1478 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
:
1479 status
= read_event_spec_context_continue_state(notit
);
1481 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
1482 status
= read_event_payload_begin_state(notit
);
1484 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
1485 status
= read_event_payload_continue_state(notit
);
1487 case STATE_EMIT_MSG_EVENT
:
1488 notit
->state
= STATE_DSCOPE_EVENT_HEADER_BEGIN
;
1490 case STATE_SKIP_PACKET_PADDING
:
1491 status
= skip_packet_padding_state(notit
);
1493 case STATE_EMIT_MSG_PACKET_END_MULTI
:
1494 notit
->state
= STATE_SKIP_PACKET_PADDING
;
1496 case STATE_EMIT_MSG_PACKET_END_SINGLE
:
1497 notit
->state
= STATE_EMIT_MSG_STREAM_ACTIVITY_END
;
1499 case STATE_EMIT_MSG_STREAM_ACTIVITY_END
:
1500 notit
->state
= STATE_EMIT_MSG_STREAM_END
;
1502 case STATE_EMIT_MSG_STREAM_END
:
1503 notit
->state
= STATE_DONE
;
1508 BT_LOGD("Unknown CTF plugin message iterator state: "
1509 "notit-addr=%p, state=%d", notit
, notit
->state
);
1513 BT_LOGV("Handled state: notit-addr=%p, status=%s, "
1514 "prev-state=%s, cur-state=%s",
1515 notit
, bt_msg_iter_status_string(status
),
1516 state_string(state
), state_string(notit
->state
));
1521 * Resets the internal state of a CTF message iterator.
1524 void bt_msg_iter_reset(struct bt_msg_iter
*notit
)
1527 BT_LOGD("Resetting message iterator: addr=%p", notit
);
1528 stack_clear(notit
->stack
);
1529 notit
->meta
.sc
= NULL
;
1530 notit
->meta
.ec
= NULL
;
1531 BT_PACKET_PUT_REF_AND_RESET(notit
->packet
);
1532 BT_STREAM_PUT_REF_AND_RESET(notit
->stream
);
1533 BT_MESSAGE_PUT_REF_AND_RESET(notit
->event_msg
);
1534 release_all_dscopes(notit
);
1535 notit
->cur_dscope_field
= NULL
;
1537 if (notit
->packet_context_field
) {
1538 bt_packet_context_field_release(notit
->packet_context_field
);
1539 notit
->packet_context_field
= NULL
;
1542 notit
->buf
.addr
= NULL
;
1545 notit
->buf
.last_eh_at
= SIZE_MAX
;
1546 notit
->buf
.packet_offset
= 0;
1547 notit
->state
= STATE_INIT
;
1548 notit
->cur_exp_packet_content_size
= -1;
1549 notit
->cur_exp_packet_total_size
= -1;
1550 notit
->cur_packet_offset
= -1;
1551 notit
->cur_stream_class_id
= -1;
1552 notit
->cur_event_class_id
= -1;
1553 notit
->cur_data_stream_id
= -1;
1554 notit
->emit_stream_begin_msg
= true;
1555 notit
->emit_stream_end_msg
= true;
1559 int bt_msg_iter_switch_packet(struct bt_msg_iter
*notit
)
1564 * We don't put the stream class here because we need to make
1565 * sure that all the packets processed by the same message
1566 * iterator refer to the same stream class (the first one).
1570 if (notit
->cur_exp_packet_total_size
!= -1) {
1571 notit
->cur_packet_offset
+= notit
->cur_exp_packet_total_size
;
1574 BT_LOGV("Switching packet: notit-addr=%p, cur=%zu, "
1575 "packet-offset=%" PRId64
, notit
, notit
->buf
.at
,
1576 notit
->cur_packet_offset
);
1577 stack_clear(notit
->stack
);
1578 notit
->meta
.ec
= NULL
;
1579 BT_PACKET_PUT_REF_AND_RESET(notit
->packet
);
1580 BT_MESSAGE_PUT_REF_AND_RESET(notit
->event_msg
);
1581 release_all_dscopes(notit
);
1582 notit
->cur_dscope_field
= NULL
;
1585 * Adjust current buffer so that addr points to the beginning of the new
1588 if (notit
->buf
.addr
) {
1589 size_t consumed_bytes
= (size_t) (notit
->buf
.at
/ CHAR_BIT
);
1591 /* Packets are assumed to start on a byte frontier. */
1592 if (notit
->buf
.at
% CHAR_BIT
) {
1593 BT_LOGW("Cannot switch packet: current position is not a multiple of 8: "
1594 "notit-addr=%p, cur=%zu", notit
, notit
->buf
.at
);
1599 notit
->buf
.addr
+= consumed_bytes
;
1600 notit
->buf
.sz
-= consumed_bytes
;
1602 notit
->buf
.packet_offset
= 0;
1603 BT_LOGV("Adjusted buffer: addr=%p, size=%zu",
1604 notit
->buf
.addr
, notit
->buf
.sz
);
1607 notit
->cur_exp_packet_content_size
= -1;
1608 notit
->cur_exp_packet_total_size
= -1;
1609 notit
->cur_stream_class_id
= -1;
1610 notit
->cur_event_class_id
= -1;
1611 notit
->cur_data_stream_id
= -1;
1612 notit
->snapshots
.discarded_events
= UINT64_C(-1);
1613 notit
->snapshots
.packets
= UINT64_C(-1);
1614 notit
->snapshots
.beginning_clock
= UINT64_C(-1);
1615 notit
->snapshots
.end_clock
= UINT64_C(-1);
1622 bt_field
*borrow_next_field(struct bt_msg_iter
*notit
)
1624 bt_field
*next_field
= NULL
;
1625 bt_field
*base_field
;
1626 const bt_field_class
*base_fc
;
1629 BT_ASSERT(!stack_empty(notit
->stack
));
1630 index
= stack_top(notit
->stack
)->index
;
1631 base_field
= stack_top(notit
->stack
)->base
;
1632 BT_ASSERT(base_field
);
1633 base_fc
= bt_field_borrow_class_const(base_field
);
1636 switch (bt_field_class_get_type(base_fc
)) {
1637 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
1640 bt_field_class_structure_get_member_count(
1641 bt_field_borrow_class_const(
1644 bt_field_structure_borrow_member_field_by_index(
1648 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
1649 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
1650 BT_ASSERT(index
< bt_field_array_get_length(base_field
));
1651 next_field
= bt_field_array_borrow_element_field_by_index(
1654 case BT_FIELD_CLASS_TYPE_VARIANT
:
1655 BT_ASSERT(index
== 0);
1656 next_field
= bt_field_variant_borrow_selected_option_field(
1663 BT_ASSERT(next_field
);
1668 void update_default_clock(struct bt_msg_iter
*notit
, uint64_t new_val
,
1669 uint64_t new_val_size
)
1671 uint64_t new_val_mask
;
1672 uint64_t cur_value_masked
;
1674 BT_ASSERT(new_val_size
> 0);
1677 * Special case for a 64-bit new value, which is the limit
1678 * of a clock value as of this version: overwrite the
1679 * current value directly.
1681 if (new_val_size
== 64) {
1682 notit
->default_clock_snapshot
= new_val
;
1686 new_val_mask
= (1ULL << new_val_size
) - 1;
1687 cur_value_masked
= notit
->default_clock_snapshot
& new_val_mask
;
1689 if (new_val
< cur_value_masked
) {
1691 * It looks like a wrap happened on the number of bits
1692 * of the requested new value. Assume that the clock
1693 * value wrapped only one time.
1695 notit
->default_clock_snapshot
+= new_val_mask
+ 1;
1698 /* Clear the low bits of the current clock value. */
1699 notit
->default_clock_snapshot
&= ~new_val_mask
;
1701 /* Set the low bits of the current clock value. */
1702 notit
->default_clock_snapshot
|= new_val
;
1705 BT_LOGV("Updated default clock's value from integer field's value: "
1706 "value=%" PRIu64
, notit
->default_clock_snapshot
);
1710 enum bt_bfcr_status
bfcr_unsigned_int_cb(uint64_t value
,
1711 struct ctf_field_class
*fc
, void *data
)
1713 struct bt_msg_iter
*notit
= data
;
1714 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
1715 bt_field
*field
= NULL
;
1716 struct ctf_field_class_int
*int_fc
= (void *) fc
;
1718 BT_LOGV("Unsigned integer function called from BFCR: "
1719 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1720 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64
,
1721 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
1723 if (likely(int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
)) {
1724 goto update_def_clock
;
1727 switch (int_fc
->meaning
) {
1728 case CTF_FIELD_CLASS_MEANING_EVENT_CLASS_ID
:
1729 notit
->cur_event_class_id
= value
;
1731 case CTF_FIELD_CLASS_MEANING_DATA_STREAM_ID
:
1732 notit
->cur_data_stream_id
= value
;
1734 case CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME
:
1735 notit
->snapshots
.beginning_clock
= value
;
1737 case CTF_FIELD_CLASS_MEANING_PACKET_END_TIME
:
1738 notit
->snapshots
.end_clock
= value
;
1740 case CTF_FIELD_CLASS_MEANING_STREAM_CLASS_ID
:
1741 notit
->cur_stream_class_id
= value
;
1743 case CTF_FIELD_CLASS_MEANING_MAGIC
:
1744 if (value
!= 0xc1fc1fc1) {
1745 BT_LOGW("Invalid CTF magic number: notit-addr=%p, "
1746 "magic=%" PRIx64
, notit
, value
);
1747 status
= BT_BFCR_STATUS_ERROR
;
1752 case CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT
:
1753 notit
->snapshots
.packets
= value
;
1755 case CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT
:
1756 notit
->snapshots
.discarded_events
= value
;
1758 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_TOTAL_SIZE
:
1759 notit
->cur_exp_packet_total_size
= value
;
1761 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_CONTENT_SIZE
:
1762 notit
->cur_exp_packet_content_size
= value
;
1769 if (unlikely(int_fc
->mapped_clock_class
)) {
1770 update_default_clock(notit
, value
, int_fc
->base
.size
);
1773 if (unlikely(int_fc
->storing_index
>= 0)) {
1774 g_array_index(notit
->stored_values
, uint64_t,
1775 (uint64_t) int_fc
->storing_index
) = value
;
1778 if (unlikely(!fc
->in_ir
)) {
1782 field
= borrow_next_field(notit
);
1784 BT_ASSERT(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
1785 BT_ASSERT(bt_field_get_class_type(field
) ==
1786 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
||
1787 bt_field_get_class_type(field
) ==
1788 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
);
1789 bt_field_unsigned_integer_set_value(field
, value
);
1790 stack_top(notit
->stack
)->index
++;
1797 enum bt_bfcr_status
bfcr_unsigned_int_char_cb(uint64_t value
,
1798 struct ctf_field_class
*fc
, void *data
)
1801 struct bt_msg_iter
*notit
= data
;
1802 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
1803 bt_field
*string_field
= NULL
;
1804 struct ctf_field_class_int
*int_fc
= (void *) fc
;
1805 char str
[2] = {'\0', '\0'};
1807 BT_LOGV("Unsigned integer character function called from BFCR: "
1808 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1809 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64
,
1810 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
1811 BT_ASSERT(int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
);
1812 BT_ASSERT(!int_fc
->mapped_clock_class
);
1813 BT_ASSERT(int_fc
->storing_index
< 0);
1815 if (unlikely(!fc
->in_ir
)) {
1819 if (notit
->done_filling_string
) {
1824 notit
->done_filling_string
= true;
1828 string_field
= stack_top(notit
->stack
)->base
;
1829 BT_ASSERT(bt_field_get_class_type(string_field
) ==
1830 BT_FIELD_CLASS_TYPE_STRING
);
1832 /* Append character */
1833 str
[0] = (char) value
;
1834 ret
= bt_field_string_append_with_length(string_field
, str
, 1);
1836 BT_LOGE("Cannot append character to string field's value: "
1837 "notit-addr=%p, field-addr=%p, ret=%d",
1838 notit
, string_field
, ret
);
1839 status
= BT_BFCR_STATUS_ERROR
;
1848 enum bt_bfcr_status
bfcr_signed_int_cb(int64_t value
,
1849 struct ctf_field_class
*fc
, void *data
)
1851 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
1852 bt_field
*field
= NULL
;
1853 struct bt_msg_iter
*notit
= data
;
1854 struct ctf_field_class_int
*int_fc
= (void *) fc
;
1856 BT_LOGV("Signed integer function called from BFCR: "
1857 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1858 "fc-type=%d, fc-in-ir=%d, value=%" PRId64
,
1859 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
1860 BT_ASSERT(int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
);
1862 if (unlikely(int_fc
->storing_index
>= 0)) {
1863 g_array_index(notit
->stored_values
, uint64_t,
1864 (uint64_t) int_fc
->storing_index
) = (uint64_t) value
;
1867 if (unlikely(!fc
->in_ir
)) {
1871 field
= borrow_next_field(notit
);
1873 BT_ASSERT(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
1874 BT_ASSERT(bt_field_get_class_type(field
) ==
1875 BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
||
1876 bt_field_get_class_type(field
) ==
1877 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
);
1878 bt_field_signed_integer_set_value(field
, value
);
1879 stack_top(notit
->stack
)->index
++;
1886 enum bt_bfcr_status
bfcr_floating_point_cb(double value
,
1887 struct ctf_field_class
*fc
, void *data
)
1889 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
1890 bt_field
*field
= NULL
;
1891 struct bt_msg_iter
*notit
= data
;
1893 BT_LOGV("Floating point number function called from BFCR: "
1894 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1895 "fc-type=%d, fc-in-ir=%d, value=%f",
1896 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
1898 if (unlikely(!fc
->in_ir
)) {
1902 field
= borrow_next_field(notit
);
1904 BT_ASSERT(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
1905 BT_ASSERT(bt_field_get_class_type(field
) ==
1906 BT_FIELD_CLASS_TYPE_REAL
);
1907 bt_field_real_set_value(field
, value
);
1908 stack_top(notit
->stack
)->index
++;
1915 enum bt_bfcr_status
bfcr_string_begin_cb(
1916 struct ctf_field_class
*fc
, void *data
)
1918 bt_field
*field
= NULL
;
1919 struct bt_msg_iter
*notit
= data
;
1922 BT_LOGV("String (beginning) function called from BFCR: "
1923 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1924 "fc-type=%d, fc-in-ir=%d",
1925 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
1927 if (unlikely(!fc
->in_ir
)) {
1931 field
= borrow_next_field(notit
);
1933 BT_ASSERT(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
1934 BT_ASSERT(bt_field_get_class_type(field
) ==
1935 BT_FIELD_CLASS_TYPE_STRING
);
1936 ret
= bt_field_string_clear(field
);
1937 BT_ASSERT(ret
== 0);
1940 * Push on stack. Not a compound class per se, but we know that
1941 * only bfcr_string_cb() may be called between this call and a
1942 * subsequent call to bfcr_string_end_cb().
1944 stack_push(notit
->stack
, field
);
1947 return BT_BFCR_STATUS_OK
;
1951 enum bt_bfcr_status
bfcr_string_cb(const char *value
,
1952 size_t len
, struct ctf_field_class
*fc
, void *data
)
1954 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
1955 bt_field
*field
= NULL
;
1956 struct bt_msg_iter
*notit
= data
;
1959 BT_LOGV("String (substring) function called from BFCR: "
1960 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1961 "fc-type=%d, fc-in-ir=%d, string-length=%zu",
1962 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
,
1965 if (unlikely(!fc
->in_ir
)) {
1969 field
= stack_top(notit
->stack
)->base
;
1972 /* Append current substring */
1973 ret
= bt_field_string_append_with_length(field
, value
, len
);
1975 BT_LOGE("Cannot append substring to string field's value: "
1976 "notit-addr=%p, field-addr=%p, string-length=%zu, "
1977 "ret=%d", notit
, field
, len
, ret
);
1978 status
= BT_BFCR_STATUS_ERROR
;
1987 enum bt_bfcr_status
bfcr_string_end_cb(
1988 struct ctf_field_class
*fc
, void *data
)
1990 struct bt_msg_iter
*notit
= data
;
1992 BT_LOGV("String (end) function called from BFCR: "
1993 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1994 "fc-type=%d, fc-in-ir=%d",
1995 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
1997 if (unlikely(!fc
->in_ir
)) {
2001 /* Pop string field */
2002 stack_pop(notit
->stack
);
2004 /* Go to next field */
2005 stack_top(notit
->stack
)->index
++;
2008 return BT_BFCR_STATUS_OK
;
2011 enum bt_bfcr_status
bfcr_compound_begin_cb(
2012 struct ctf_field_class
*fc
, void *data
)
2014 struct bt_msg_iter
*notit
= data
;
2017 BT_LOGV("Compound (beginning) function called from BFCR: "
2018 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2019 "fc-type=%d, fc-in-ir=%d",
2020 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
2027 if (stack_empty(notit
->stack
)) {
2028 /* Root: already set by read_dscope_begin_state() */
2029 field
= notit
->cur_dscope_field
;
2031 field
= borrow_next_field(notit
);
2037 BT_ASSERT(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
2038 stack_push(notit
->stack
, field
);
2041 * Change BFCR "unsigned int" callback if it's a text
2044 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
||
2045 fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
2046 struct ctf_field_class_array_base
*array_fc
= (void *) fc
;
2048 if (array_fc
->is_text
) {
2051 BT_ASSERT(bt_field_get_class_type(field
) ==
2052 BT_FIELD_CLASS_TYPE_STRING
);
2053 notit
->done_filling_string
= false;
2054 ret
= bt_field_string_clear(field
);
2055 BT_ASSERT(ret
== 0);
2056 bt_bfcr_set_unsigned_int_cb(notit
->bfcr
,
2057 bfcr_unsigned_int_char_cb
);
2062 return BT_BFCR_STATUS_OK
;
2065 enum bt_bfcr_status
bfcr_compound_end_cb(
2066 struct ctf_field_class
*fc
, void *data
)
2068 struct bt_msg_iter
*notit
= data
;
2070 BT_LOGV("Compound (end) function called from BFCR: "
2071 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2072 "fc-type=%d, fc-in-ir=%d",
2073 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
2079 BT_ASSERT(!stack_empty(notit
->stack
));
2080 BT_ASSERT(bt_field_borrow_class_const(stack_top(notit
->stack
)->base
) ==
2084 * Reset BFCR "unsigned int" callback if it's a text
2087 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
||
2088 fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
2089 struct ctf_field_class_array_base
*array_fc
= (void *) fc
;
2091 if (array_fc
->is_text
) {
2092 BT_ASSERT(bt_field_get_class_type(
2093 stack_top(notit
->stack
)->base
) ==
2094 BT_FIELD_CLASS_TYPE_STRING
);
2095 bt_bfcr_set_unsigned_int_cb(notit
->bfcr
,
2096 bfcr_unsigned_int_cb
);
2101 stack_pop(notit
->stack
);
2103 /* If the stack is not empty, increment the base's index */
2104 if (!stack_empty(notit
->stack
)) {
2105 stack_top(notit
->stack
)->index
++;
2109 return BT_BFCR_STATUS_OK
;
2113 int64_t bfcr_get_sequence_length_cb(struct ctf_field_class
*fc
, void *data
)
2115 bt_field
*seq_field
;
2116 struct bt_msg_iter
*notit
= data
;
2117 struct ctf_field_class_sequence
*seq_fc
= (void *) fc
;
2118 int64_t length
= -1;
2121 length
= (uint64_t) g_array_index(notit
->stored_values
, uint64_t,
2122 seq_fc
->stored_length_index
);
2123 seq_field
= stack_top(notit
->stack
)->base
;
2124 BT_ASSERT(seq_field
);
2125 ret
= bt_field_dynamic_array_set_length(seq_field
, (uint64_t) length
);
2127 BT_LOGE("Cannot set dynamic array field's length field: "
2128 "notit-addr=%p, field-addr=%p, "
2129 "length=%" PRIu64
, notit
, seq_field
, length
);
2136 struct ctf_field_class
*bfcr_borrow_variant_selected_field_class_cb(
2137 struct ctf_field_class
*fc
, void *data
)
2141 int64_t option_index
= -1;
2142 struct bt_msg_iter
*notit
= data
;
2143 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
2144 struct ctf_named_field_class
*selected_option
= NULL
;
2145 struct ctf_field_class
*ret_fc
= NULL
;
2151 /* Get variant's tag */
2152 tag
.u
= g_array_index(notit
->stored_values
, uint64_t,
2153 var_fc
->stored_tag_index
);
2156 * Check each range to find the selected option's index.
2158 if (var_fc
->tag_fc
->base
.is_signed
) {
2159 for (i
= 0; i
< var_fc
->ranges
->len
; i
++) {
2160 struct ctf_field_class_variant_range
*range
=
2161 ctf_field_class_variant_borrow_range_by_index(
2164 if (tag
.i
>= range
->range
.lower
.i
&&
2165 tag
.i
<= range
->range
.upper
.i
) {
2166 option_index
= (int64_t) range
->option_index
;
2171 for (i
= 0; i
< var_fc
->ranges
->len
; i
++) {
2172 struct ctf_field_class_variant_range
*range
=
2173 ctf_field_class_variant_borrow_range_by_index(
2176 if (tag
.u
>= range
->range
.lower
.u
&&
2177 tag
.u
<= range
->range
.upper
.u
) {
2178 option_index
= (int64_t) range
->option_index
;
2184 if (option_index
< 0) {
2185 BT_LOGW("Cannot find variant field class's option: "
2186 "notit-addr=%p, var-fc-addr=%p, u-tag=%" PRIu64
", "
2187 "i-tag=%" PRId64
, notit
, var_fc
, tag
.u
, tag
.i
);
2191 selected_option
= ctf_field_class_variant_borrow_option_by_index(
2192 var_fc
, (uint64_t) option_index
);
2194 if (selected_option
->fc
->in_ir
) {
2195 bt_field
*var_field
= stack_top(notit
->stack
)->base
;
2197 ret
= bt_field_variant_select_option_field(
2198 var_field
, option_index
);
2200 BT_LOGW("Cannot select variant field's option field: "
2201 "notit-addr=%p, var-field-addr=%p, "
2202 "opt-index=%" PRId64
, notit
, var_field
,
2208 ret_fc
= selected_option
->fc
;
2215 void set_event_default_clock_snapshot(struct bt_msg_iter
*notit
)
2217 bt_event
*event
= bt_message_event_borrow_event(notit
->event_msg
);
2218 bt_stream_class
*sc
= notit
->meta
.sc
->ir_sc
;
2222 if (bt_stream_class_borrow_default_clock_class(sc
)) {
2223 bt_event_set_default_clock_snapshot(event
,
2224 notit
->default_clock_snapshot
);
2229 void create_msg_stream_beginning(struct bt_msg_iter
*notit
,
2230 bt_message
**message
)
2232 bt_message
*ret
= NULL
;
2234 BT_ASSERT(notit
->stream
);
2235 BT_ASSERT(notit
->msg_iter
);
2236 ret
= bt_message_stream_beginning_create(notit
->msg_iter
,
2239 BT_LOGE("Cannot create stream beginning message: "
2240 "notit-addr=%p, stream-addr=%p",
2241 notit
, notit
->stream
);
2249 void create_msg_stream_activity_beginning(struct bt_msg_iter
*notit
,
2250 bt_message
**message
)
2252 bt_message
*ret
= NULL
;
2254 BT_ASSERT(notit
->stream
);
2255 BT_ASSERT(notit
->msg_iter
);
2256 ret
= bt_message_stream_activity_beginning_create(notit
->msg_iter
,
2259 BT_LOGE("Cannot create stream activity beginning message: "
2260 "notit-addr=%p, stream-addr=%p",
2261 notit
, notit
->stream
);
2269 void create_msg_stream_activity_end(struct bt_msg_iter
*notit
,
2270 bt_message
**message
)
2272 bt_message
*ret
= NULL
;
2274 if (!notit
->stream
) {
2275 BT_LOGE("Cannot create stream for stream message: "
2276 "notit-addr=%p", notit
);
2280 BT_ASSERT(notit
->stream
);
2281 BT_ASSERT(notit
->msg_iter
);
2282 ret
= bt_message_stream_activity_end_create(notit
->msg_iter
,
2285 BT_LOGE("Cannot create stream activity end message: "
2286 "notit-addr=%p, stream-addr=%p",
2287 notit
, notit
->stream
);
2295 void create_msg_stream_end(struct bt_msg_iter
*notit
, bt_message
**message
)
2299 if (!notit
->stream
) {
2300 BT_LOGE("Cannot create stream for stream message: "
2301 "notit-addr=%p", notit
);
2305 BT_ASSERT(notit
->msg_iter
);
2306 ret
= bt_message_stream_end_create(notit
->msg_iter
,
2309 BT_LOGE("Cannot create stream end message: "
2310 "notit-addr=%p, stream-addr=%p",
2311 notit
, notit
->stream
);
2319 void create_msg_packet_beginning(struct bt_msg_iter
*notit
,
2320 bt_message
**message
)
2323 enum bt_msg_iter_status status
;
2324 bt_message
*msg
= NULL
;
2325 const bt_stream_class
*sc
;
2327 status
= set_current_packet(notit
);
2328 if (status
!= BT_MSG_ITER_STATUS_OK
) {
2332 BT_ASSERT(notit
->packet
);
2333 sc
= notit
->meta
.sc
->ir_sc
;
2336 if (bt_stream_class_packets_have_discarded_event_counter_snapshot(sc
)) {
2337 BT_ASSERT(notit
->snapshots
.discarded_events
!= UINT64_C(-1));
2338 bt_packet_set_discarded_event_counter_snapshot(
2339 notit
->packet
, notit
->snapshots
.discarded_events
);
2342 if (bt_stream_class_packets_have_packet_counter_snapshot(sc
)) {
2343 BT_ASSERT(notit
->snapshots
.packets
!= UINT64_C(-1));
2344 bt_packet_set_packet_counter_snapshot(
2345 notit
->packet
, notit
->snapshots
.packets
);
2348 if (bt_stream_class_packets_have_default_beginning_clock_snapshot(sc
)) {
2349 BT_ASSERT(notit
->snapshots
.beginning_clock
!= UINT64_C(-1));
2350 bt_packet_set_default_beginning_clock_snapshot(
2351 notit
->packet
, notit
->snapshots
.beginning_clock
);
2354 if (bt_stream_class_packets_have_default_end_clock_snapshot(sc
)) {
2355 BT_ASSERT(notit
->snapshots
.end_clock
!= UINT64_C(-1));
2356 bt_packet_set_default_end_clock_snapshot(
2357 notit
->packet
, notit
->snapshots
.end_clock
);
2360 if (notit
->packet_context_field
) {
2361 ret
= bt_packet_move_context_field(
2362 notit
->packet
, notit
->packet_context_field
);
2367 notit
->packet_context_field
= NULL
;
2370 * At this point notit->dscopes.stream_packet_context
2371 * has the same value as the packet context field within
2374 BT_ASSERT(bt_packet_borrow_context_field(
2376 notit
->dscopes
.stream_packet_context
);
2379 BT_ASSERT(notit
->msg_iter
);
2380 msg
= bt_message_packet_beginning_create(notit
->msg_iter
,
2383 BT_LOGE("Cannot create packet beginning message: "
2384 "notit-addr=%p, packet-addr=%p",
2385 notit
, notit
->packet
);
2396 void create_msg_packet_end(struct bt_msg_iter
*notit
, bt_message
**message
)
2400 if (!notit
->packet
) {
2404 /* Update default clock from packet's end time */
2405 if (notit
->snapshots
.end_clock
!= UINT64_C(-1)) {
2406 notit
->default_clock_snapshot
= notit
->snapshots
.end_clock
;
2409 BT_ASSERT(notit
->msg_iter
);
2410 msg
= bt_message_packet_end_create(notit
->msg_iter
,
2413 BT_LOGE("Cannot create packet end message: "
2414 "notit-addr=%p, packet-addr=%p",
2415 notit
, notit
->packet
);
2420 BT_PACKET_PUT_REF_AND_RESET(notit
->packet
);
2425 struct bt_msg_iter
*bt_msg_iter_create(struct ctf_trace_class
*tc
,
2426 size_t max_request_sz
,
2427 struct bt_msg_iter_medium_ops medops
, void *data
)
2429 struct bt_msg_iter
*notit
= NULL
;
2430 struct bt_bfcr_cbs cbs
= {
2432 .signed_int
= bfcr_signed_int_cb
,
2433 .unsigned_int
= bfcr_unsigned_int_cb
,
2434 .floating_point
= bfcr_floating_point_cb
,
2435 .string_begin
= bfcr_string_begin_cb
,
2436 .string
= bfcr_string_cb
,
2437 .string_end
= bfcr_string_end_cb
,
2438 .compound_begin
= bfcr_compound_begin_cb
,
2439 .compound_end
= bfcr_compound_end_cb
,
2442 .get_sequence_length
= bfcr_get_sequence_length_cb
,
2443 .borrow_variant_selected_field_class
= bfcr_borrow_variant_selected_field_class_cb
,
2448 BT_ASSERT(medops
.request_bytes
);
2449 BT_ASSERT(medops
.borrow_stream
);
2450 BT_LOGD("Creating CTF plugin message iterator: "
2451 "trace-addr=%p, max-request-size=%zu, "
2452 "data=%p", tc
, max_request_sz
, data
);
2453 notit
= g_new0(struct bt_msg_iter
, 1);
2455 BT_LOGE_STR("Failed to allocate one CTF plugin message iterator.");
2458 notit
->meta
.tc
= tc
;
2459 notit
->medium
.medops
= medops
;
2460 notit
->medium
.max_request_sz
= max_request_sz
;
2461 notit
->medium
.data
= data
;
2462 notit
->stack
= stack_new(notit
);
2463 notit
->stored_values
= g_array_new(FALSE
, TRUE
, sizeof(uint64_t));
2464 g_array_set_size(notit
->stored_values
, tc
->stored_value_count
);
2466 if (!notit
->stack
) {
2467 BT_LOGE_STR("Failed to create field stack.");
2471 notit
->bfcr
= bt_bfcr_create(cbs
, notit
);
2473 BT_LOGE_STR("Failed to create binary class reader (BFCR).");
2477 bt_msg_iter_reset(notit
);
2478 BT_LOGD("Created CTF plugin message iterator: "
2479 "trace-addr=%p, max-request-size=%zu, "
2480 "data=%p, notit-addr=%p",
2481 tc
, max_request_sz
, data
, notit
);
2482 notit
->cur_packet_offset
= 0;
2488 bt_msg_iter_destroy(notit
);
2493 void bt_msg_iter_destroy(struct bt_msg_iter
*notit
)
2495 BT_PACKET_PUT_REF_AND_RESET(notit
->packet
);
2496 BT_STREAM_PUT_REF_AND_RESET(notit
->stream
);
2497 release_all_dscopes(notit
);
2499 BT_LOGD("Destroying CTF plugin message iterator: addr=%p", notit
);
2502 BT_LOGD_STR("Destroying field stack.");
2503 stack_destroy(notit
->stack
);
2507 BT_LOGD("Destroying BFCR: bfcr-addr=%p", notit
->bfcr
);
2508 bt_bfcr_destroy(notit
->bfcr
);
2511 if (notit
->stored_values
) {
2512 g_array_free(notit
->stored_values
, TRUE
);
2518 enum bt_msg_iter_status
bt_msg_iter_get_next_message(
2519 struct bt_msg_iter
*notit
,
2520 bt_self_message_iterator
*msg_iter
, bt_message
**message
)
2522 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
2526 notit
->msg_iter
= msg_iter
;
2527 BT_LOGV("Getting next message: notit-addr=%p", notit
);
2530 status
= handle_state(notit
);
2531 if (unlikely(status
== BT_MSG_ITER_STATUS_AGAIN
)) {
2532 BT_LOGV_STR("Medium returned BT_MSG_ITER_STATUS_AGAIN.");
2534 } else if (unlikely(status
!= BT_MSG_ITER_STATUS_OK
)) {
2535 BT_LOGW("Cannot handle state: notit-addr=%p, state=%s",
2536 notit
, state_string(notit
->state
));
2540 switch (notit
->state
) {
2541 case STATE_EMIT_MSG_EVENT
:
2542 BT_ASSERT(notit
->event_msg
);
2543 set_event_default_clock_snapshot(notit
);
2544 *message
= notit
->event_msg
;
2545 notit
->event_msg
= NULL
;
2547 case STATE_EMIT_MSG_PACKET_BEGINNING
:
2548 /* create_msg_packet_beginning() logs errors */
2549 create_msg_packet_beginning(notit
, message
);
2552 status
= BT_MSG_ITER_STATUS_ERROR
;
2556 case STATE_EMIT_MSG_PACKET_END_SINGLE
:
2557 case STATE_EMIT_MSG_PACKET_END_MULTI
:
2558 /* create_msg_packet_end() logs errors */
2559 create_msg_packet_end(notit
, message
);
2562 status
= BT_MSG_ITER_STATUS_ERROR
;
2566 case STATE_EMIT_MSG_STREAM_ACTIVITY_BEGINNING
:
2567 if (notit
->emit_stream_begin_msg
) {
2568 /* create_msg_stream_activity_beginning() logs errors */
2569 create_msg_stream_activity_beginning(notit
, message
);
2572 status
= BT_MSG_ITER_STATUS_ERROR
;
2579 case STATE_EMIT_MSG_STREAM_ACTIVITY_END
:
2580 if (notit
->emit_stream_end_msg
) {
2581 /* create_msg_stream_activity_end() logs errors */
2582 create_msg_stream_activity_end(notit
, message
);
2585 status
= BT_MSG_ITER_STATUS_ERROR
;
2592 case STATE_EMIT_MSG_STREAM_BEGINNING
:
2593 status
= set_current_stream(notit
);
2594 if (status
!= BT_MSG_ITER_STATUS_OK
) {
2598 if (notit
->emit_stream_begin_msg
) {
2599 /* create_msg_stream_beginning() logs errors */
2600 create_msg_stream_beginning(notit
, message
);
2603 status
= BT_MSG_ITER_STATUS_ERROR
;
2610 case STATE_EMIT_MSG_STREAM_END
:
2611 if (notit
->emit_stream_end_msg
) {
2612 /* create_msg_stream_end() logs errors */
2613 create_msg_stream_end(notit
, message
);
2616 status
= BT_MSG_ITER_STATUS_ERROR
;
2624 status
= BT_MSG_ITER_STATUS_EOF
;
2627 /* Non-emitting state: continue */
2637 enum bt_msg_iter_status
read_packet_header_context_fields(
2638 struct bt_msg_iter
*notit
)
2641 enum bt_msg_iter_status status
= BT_MSG_ITER_STATUS_OK
;
2645 if (notit
->state
== STATE_EMIT_MSG_PACKET_BEGINNING
) {
2646 /* We're already there */
2651 status
= handle_state(notit
);
2652 if (unlikely(status
== BT_MSG_ITER_STATUS_AGAIN
)) {
2653 BT_LOGV_STR("Medium returned BT_MSG_ITER_STATUS_AGAIN.");
2655 } else if (unlikely(status
!= BT_MSG_ITER_STATUS_OK
)) {
2656 BT_LOGW("Cannot handle state: notit-addr=%p, state=%s",
2657 notit
, state_string(notit
->state
));
2661 switch (notit
->state
) {
2662 case STATE_EMIT_MSG_PACKET_BEGINNING
:
2664 * Packet header and context fields are
2665 * potentially decoded (or they don't exist).
2669 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
2670 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
2671 case STATE_AFTER_TRACE_PACKET_HEADER
:
2672 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
2673 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
2674 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
2675 case STATE_EMIT_MSG_STREAM_BEGINNING
:
2676 case STATE_EMIT_MSG_STREAM_ACTIVITY_BEGINNING
:
2677 /* Non-emitting state: continue */
2681 * We should never get past the
2682 * STATE_EMIT_MSG_PACKET_BEGINNING state.
2684 BT_LOGF("Unexpected state: notit-addr=%p, state=%s",
2685 notit
, state_string(notit
->state
));
2691 ret
= set_current_packet_content_sizes(notit
);
2693 status
= BT_MSG_ITER_STATUS_ERROR
;
2701 void bt_msg_iter_set_medops_data(struct bt_msg_iter
*notit
,
2705 notit
->medium
.data
= medops_data
;
2709 enum bt_msg_iter_status
bt_msg_iter_seek(struct bt_msg_iter
*notit
,
2712 enum bt_msg_iter_status ret
= BT_MSG_ITER_STATUS_OK
;
2713 enum bt_msg_iter_medium_status medium_status
;
2717 BT_LOGE("Cannot seek to negative offset: offset=%jd", offset
);
2718 ret
= BT_MSG_ITER_STATUS_INVAL
;
2722 if (!notit
->medium
.medops
.seek
) {
2723 ret
= BT_MSG_ITER_STATUS_UNSUPPORTED
;
2724 BT_LOGD("Aborting seek as the iterator's underlying media does not implement seek support.");
2728 medium_status
= notit
->medium
.medops
.seek(
2729 BT_MSG_ITER_SEEK_WHENCE_SET
, offset
, notit
->medium
.data
);
2730 if (medium_status
!= BT_MSG_ITER_MEDIUM_STATUS_OK
) {
2731 if (medium_status
== BT_MSG_ITER_MEDIUM_STATUS_EOF
) {
2732 ret
= BT_MSG_ITER_STATUS_EOF
;
2734 ret
= BT_MSG_ITER_STATUS_ERROR
;
2739 bt_msg_iter_reset(notit
);
2740 notit
->cur_packet_offset
= offset
;
2747 enum bt_msg_iter_status
bt_msg_iter_get_packet_properties(
2748 struct bt_msg_iter
*notit
,
2749 struct bt_msg_iter_packet_properties
*props
)
2751 enum bt_msg_iter_status status
;
2755 status
= read_packet_header_context_fields(notit
);
2756 if (status
!= BT_MSG_ITER_STATUS_OK
) {
2760 props
->exp_packet_total_size
= notit
->cur_exp_packet_total_size
;
2761 props
->exp_packet_content_size
= notit
->cur_exp_packet_content_size
;
2762 BT_ASSERT(props
->stream_class_id
>= 0);
2763 props
->stream_class_id
= (uint64_t) notit
->cur_stream_class_id
;
2764 props
->data_stream_id
= notit
->cur_data_stream_id
;
2765 props
->snapshots
.discarded_events
= notit
->snapshots
.discarded_events
;
2766 props
->snapshots
.packets
= notit
->snapshots
.packets
;
2767 props
->snapshots
.beginning_clock
= notit
->snapshots
.beginning_clock
;
2768 props
->snapshots
.end_clock
= notit
->snapshots
.end_clock
;
2775 void bt_msg_iter_set_emit_stream_beginning_message(struct bt_msg_iter
*notit
,
2778 notit
->emit_stream_begin_msg
= val
;
2782 void bt_msg_iter_set_emit_stream_end_message(struct bt_msg_iter
*notit
,
2785 notit
->emit_stream_end_msg
= val
;