2 * Babeltrace - CTF notification 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-NOTIF-ITER"
34 #include <babeltrace/assert-internal.h>
36 #include <babeltrace/babeltrace.h>
37 #include <babeltrace/common-internal.h>
41 #include "notif-iter.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_NOTIF_NEW_STREAM
,
84 STATE_EMIT_NOTIF_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
,
94 STATE_EMIT_NOTIF_EVENT
,
95 STATE_EMIT_NOTIF_END_OF_PACKET
,
97 STATE_SKIP_PACKET_PADDING
,
100 /* CTF notification iterator */
101 struct bt_notif_iter
{
105 /* Current notification iterator to create notifications (weak) */
106 bt_self_notification_iterator
*notif_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 */
124 struct ctf_trace_class
*tc
;
125 struct ctf_stream_class
*sc
;
126 struct ctf_event_class
*ec
;
129 /* Current packet header field wrapper (NULL if not created yet) */
130 bt_packet_header_field
*packet_header_field
;
132 /* Current packet header field wrapper (NULL if not created yet) */
133 bt_packet_context_field
*packet_context_field
;
135 /* Current event header field (NULL if not created yet) */
136 bt_event_header_field
*event_header_field
;
138 /* Current packet (NULL if not created yet) */
141 /* Current stream (NULL if not set yet) */
144 /* Current event (NULL if not created yet) */
147 /* Current event notification (NULL if not created yet) */
148 bt_notification
*event_notif
;
150 /* Database of current dynamic scopes */
152 bt_field
*trace_packet_header
;
153 bt_field
*stream_packet_context
;
154 bt_field
*event_header
;
155 bt_field
*event_common_context
;
156 bt_field
*event_spec_context
;
157 bt_field
*event_payload
;
163 /* Current medium buffer data */
165 /* Last address provided by medium */
168 /* Buffer size provided by medium (bytes) */
171 /* Offset within whole packet of addr (bits) */
172 size_t packet_offset
;
174 /* Current position from addr (bits) */
177 /* Position of the last event header from addr (bits) */
181 /* Binary type reader */
182 struct bt_bfcr
*bfcr
;
184 /* Current medium data */
186 struct bt_notif_iter_medium_ops medops
;
187 size_t max_request_sz
;
191 /* Stream beginning was emitted */
192 bool stream_begin_emitted
;
194 /* Current packet size (bits) (-1 if unknown) */
195 int64_t cur_exp_packet_total_size
;
197 /* Current content size (bits) (-1 if unknown) */
198 int64_t cur_exp_packet_content_size
;
200 /* Current stream class ID */
201 int64_t cur_stream_class_id
;
203 /* Current event class ID */
204 int64_t cur_event_class_id
;
206 /* Current data stream ID */
207 int64_t cur_data_stream_id
;
210 * Offset, in the underlying media, of the current packet's
211 * start (-1 if unknown).
213 off_t cur_packet_offset
;
215 /* Default clock's current value */
216 uint64_t default_clock_val
;
218 /* End of packet snapshots */
220 uint64_t discarded_events
;
222 uint64_t beginning_clock
;
226 /* Stored values (for sequence lengths, variant tags) */
227 GArray
*stored_values
;
231 const char *state_string(enum state state
)
236 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
237 return "STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN";
238 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
239 return "STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE";
240 case STATE_AFTER_TRACE_PACKET_HEADER
:
241 return "STATE_AFTER_TRACE_PACKET_HEADER";
242 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
243 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN";
244 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
245 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE";
246 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
247 return "STATE_AFTER_STREAM_PACKET_CONTEXT";
248 case STATE_EMIT_NOTIF_NEW_PACKET
:
249 return "STATE_EMIT_NOTIF_NEW_PACKET";
250 case STATE_EMIT_NOTIF_NEW_STREAM
:
251 return "STATE_EMIT_NOTIF_NEW_STREAM";
252 case STATE_DSCOPE_EVENT_HEADER_BEGIN
:
253 return "STATE_DSCOPE_EVENT_HEADER_BEGIN";
254 case STATE_DSCOPE_EVENT_HEADER_CONTINUE
:
255 return "STATE_DSCOPE_EVENT_HEADER_CONTINUE";
256 case STATE_AFTER_EVENT_HEADER
:
257 return "STATE_AFTER_EVENT_HEADER";
258 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
:
259 return "STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN";
260 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
:
261 return "STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE";
262 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
:
263 return "STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN";
264 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
:
265 return "STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE";
266 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
267 return "STATE_DSCOPE_EVENT_PAYLOAD_BEGIN";
268 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
269 return "STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE";
270 case STATE_EMIT_NOTIF_EVENT
:
271 return "STATE_EMIT_NOTIF_EVENT";
272 case STATE_EMIT_NOTIF_END_OF_PACKET
:
273 return "STATE_EMIT_NOTIF_END_OF_PACKET";
276 case STATE_SKIP_PACKET_PADDING
:
277 return "STATE_SKIP_PACKET_PADDING";
284 int bt_notif_iter_switch_packet(struct bt_notif_iter
*notit
);
287 struct stack
*stack_new(struct bt_notif_iter
*notit
)
289 struct stack
*stack
= NULL
;
291 stack
= g_new0(struct stack
, 1);
293 BT_LOGE_STR("Failed to allocate one stack.");
297 stack
->entries
= g_array_new(FALSE
, TRUE
, sizeof(struct stack_entry
));
298 if (!stack
->entries
) {
299 BT_LOGE_STR("Failed to allocate a GArray.");
303 BT_LOGD("Created stack: notit-addr=%p, stack-addr=%p", notit
, stack
);
315 void stack_destroy(struct stack
*stack
)
318 BT_LOGD("Destroying stack: addr=%p", stack
);
320 if (stack
->entries
) {
321 g_array_free(stack
->entries
, TRUE
);
328 void stack_push(struct stack
*stack
, bt_field
*base
)
330 struct stack_entry
*entry
;
334 BT_LOGV("Pushing base field on stack: stack-addr=%p, "
335 "stack-size-before=%zu, stack-size-after=%zu",
336 stack
, stack
->size
, stack
->size
+ 1);
338 if (stack
->entries
->len
== stack
->size
) {
339 g_array_set_size(stack
->entries
, stack
->size
+ 1);
342 entry
= &g_array_index(stack
->entries
, struct stack_entry
, stack
->size
);
349 unsigned int stack_size(struct stack
*stack
)
356 void stack_pop(struct stack
*stack
)
359 BT_ASSERT(stack_size(stack
));
360 BT_LOGV("Popping from stack: "
361 "stack-addr=%p, stack-size-before=%zu, stack-size-after=%zu",
362 stack
, stack
->size
, stack
->size
- 1);
367 struct stack_entry
*stack_top(struct stack
*stack
)
370 BT_ASSERT(stack_size(stack
));
371 return &g_array_index(stack
->entries
, struct stack_entry
,
376 bool stack_empty(struct stack
*stack
)
378 return stack_size(stack
) == 0;
382 void stack_clear(struct stack
*stack
)
389 enum bt_notif_iter_status
notif_iter_status_from_m_status(
390 enum bt_notif_iter_medium_status m_status
)
392 /* They are the same */
393 return (int) m_status
;
397 size_t buf_size_bits(struct bt_notif_iter
*notit
)
399 return notit
->buf
.sz
* 8;
403 size_t buf_available_bits(struct bt_notif_iter
*notit
)
405 return buf_size_bits(notit
) - notit
->buf
.at
;
409 size_t packet_at(struct bt_notif_iter
*notit
)
411 return notit
->buf
.packet_offset
+ notit
->buf
.at
;
415 void buf_consume_bits(struct bt_notif_iter
*notit
, size_t incr
)
417 BT_LOGV("Advancing cursor: notit-addr=%p, cur-before=%zu, cur-after=%zu",
418 notit
, notit
->buf
.at
, notit
->buf
.at
+ incr
);
419 notit
->buf
.at
+= incr
;
423 enum bt_notif_iter_status
request_medium_bytes(
424 struct bt_notif_iter
*notit
)
426 uint8_t *buffer_addr
= NULL
;
427 size_t buffer_sz
= 0;
428 enum bt_notif_iter_medium_status m_status
;
430 BT_LOGV("Calling user function (request bytes): notit-addr=%p, "
431 "request-size=%zu", notit
, notit
->medium
.max_request_sz
);
432 m_status
= notit
->medium
.medops
.request_bytes(
433 notit
->medium
.max_request_sz
, &buffer_addr
,
434 &buffer_sz
, notit
->medium
.data
);
435 BT_LOGV("User function returned: status=%s, buf-addr=%p, buf-size=%zu",
436 bt_notif_iter_medium_status_string(m_status
),
437 buffer_addr
, buffer_sz
);
438 if (m_status
== BT_NOTIF_ITER_MEDIUM_STATUS_OK
) {
439 BT_ASSERT(buffer_sz
!= 0);
441 /* New packet offset is old one + old size (in bits) */
442 notit
->buf
.packet_offset
+= buf_size_bits(notit
);
444 /* Restart at the beginning of the new medium buffer */
446 notit
->buf
.last_eh_at
= SIZE_MAX
;
448 /* New medium buffer size */
449 notit
->buf
.sz
= buffer_sz
;
451 /* New medium buffer address */
452 notit
->buf
.addr
= buffer_addr
;
454 BT_LOGV("User function returned new bytes: "
455 "packet-offset=%zu, cur=%zu, size=%zu, addr=%p",
456 notit
->buf
.packet_offset
, notit
->buf
.at
,
457 notit
->buf
.sz
, notit
->buf
.addr
);
458 BT_LOGV_MEM(buffer_addr
, buffer_sz
, "Returned bytes at %p:",
460 } else if (m_status
== BT_NOTIF_ITER_MEDIUM_STATUS_EOF
) {
462 * User returned end of stream: validate that we're not
463 * in the middle of a packet header, packet context, or
466 if (notit
->cur_exp_packet_total_size
>= 0) {
467 if (packet_at(notit
) ==
468 notit
->cur_exp_packet_total_size
) {
472 if (packet_at(notit
) == 0) {
476 if (notit
->buf
.last_eh_at
!= SIZE_MAX
&&
477 notit
->buf
.at
== notit
->buf
.last_eh_at
) {
482 /* All other states are invalid */
483 BT_LOGW("User function returned %s, but notification iterator is in an unexpected state: "
484 "state=%s, cur-packet-size=%" PRId64
", cur=%zu, "
485 "packet-cur=%zu, last-eh-at=%zu",
486 bt_notif_iter_medium_status_string(m_status
),
487 state_string(notit
->state
),
488 notit
->cur_exp_packet_total_size
,
489 notit
->buf
.at
, packet_at(notit
),
490 notit
->buf
.last_eh_at
);
491 m_status
= BT_NOTIF_ITER_MEDIUM_STATUS_ERROR
;
492 } else if (m_status
< 0) {
493 BT_LOGW("User function failed: status=%s",
494 bt_notif_iter_medium_status_string(m_status
));
498 return notif_iter_status_from_m_status(m_status
);
502 enum bt_notif_iter_status
buf_ensure_available_bits(
503 struct bt_notif_iter
*notit
)
505 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
507 if (unlikely(buf_available_bits(notit
) == 0)) {
509 * This _cannot_ return BT_NOTIF_ITER_STATUS_OK
512 status
= request_medium_bytes(notit
);
519 enum bt_notif_iter_status
read_dscope_begin_state(
520 struct bt_notif_iter
*notit
,
521 struct ctf_field_class
*dscope_fc
,
522 enum state done_state
, enum state continue_state
,
523 bt_field
*dscope_field
)
525 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
526 enum bt_bfcr_status bfcr_status
;
527 size_t consumed_bits
;
529 notit
->cur_dscope_field
= dscope_field
;
530 BT_LOGV("Starting BFCR: notit-addr=%p, bfcr-addr=%p, fc-addr=%p",
531 notit
, notit
->bfcr
, dscope_fc
);
532 consumed_bits
= bt_bfcr_start(notit
->bfcr
, dscope_fc
,
533 notit
->buf
.addr
, notit
->buf
.at
, packet_at(notit
),
534 notit
->buf
.sz
, &bfcr_status
);
535 BT_LOGV("BFCR consumed bits: size=%zu", consumed_bits
);
537 switch (bfcr_status
) {
538 case BT_BFCR_STATUS_OK
:
539 /* Field class was read completely */
540 BT_LOGV_STR("Field was completely decoded.");
541 notit
->state
= done_state
;
543 case BT_BFCR_STATUS_EOF
:
544 BT_LOGV_STR("BFCR needs more data to decode field completely.");
545 notit
->state
= continue_state
;
548 BT_LOGW("BFCR failed to start: notit-addr=%p, bfcr-addr=%p, "
549 "status=%s", notit
, notit
->bfcr
,
550 bt_bfcr_status_string(bfcr_status
));
551 status
= BT_NOTIF_ITER_STATUS_ERROR
;
555 /* Consume bits now since we know we're not in an error state */
556 buf_consume_bits(notit
, consumed_bits
);
563 enum bt_notif_iter_status
read_dscope_continue_state(
564 struct bt_notif_iter
*notit
, enum state done_state
)
566 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
567 enum bt_bfcr_status bfcr_status
;
568 size_t consumed_bits
;
570 BT_LOGV("Continuing BFCR: notit-addr=%p, bfcr-addr=%p",
573 status
= buf_ensure_available_bits(notit
);
574 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
576 BT_LOGW("Cannot ensure that buffer has at least one byte: "
577 "notif-addr=%p, status=%s",
578 notit
, bt_notif_iter_status_string(status
));
580 BT_LOGV("Cannot ensure that buffer has at least one byte: "
581 "notif-addr=%p, status=%s",
582 notit
, bt_notif_iter_status_string(status
));
588 consumed_bits
= bt_bfcr_continue(notit
->bfcr
, notit
->buf
.addr
,
589 notit
->buf
.sz
, &bfcr_status
);
590 BT_LOGV("BFCR consumed bits: size=%zu", consumed_bits
);
592 switch (bfcr_status
) {
593 case BT_BFCR_STATUS_OK
:
594 /* Type was read completely. */
595 BT_LOGV_STR("Field was completely decoded.");
596 notit
->state
= done_state
;
598 case BT_BFCR_STATUS_EOF
:
599 /* Stay in this continue state. */
600 BT_LOGV_STR("BFCR needs more data to decode field completely.");
603 BT_LOGW("BFCR failed to continue: notit-addr=%p, bfcr-addr=%p, "
604 "status=%s", notit
, notit
->bfcr
,
605 bt_bfcr_status_string(bfcr_status
));
606 status
= BT_NOTIF_ITER_STATUS_ERROR
;
610 /* Consume bits now since we know we're not in an error state. */
611 buf_consume_bits(notit
, consumed_bits
);
617 void release_event_dscopes(struct bt_notif_iter
*notit
)
619 notit
->dscopes
.event_header
= NULL
;
621 if (notit
->event_header_field
) {
622 bt_event_header_field_release(notit
->event_header_field
);
623 notit
->event_header_field
= NULL
;
626 notit
->dscopes
.event_common_context
= NULL
;
627 notit
->dscopes
.event_spec_context
= NULL
;
628 notit
->dscopes
.event_payload
= NULL
;
632 void release_all_dscopes(struct bt_notif_iter
*notit
)
634 notit
->dscopes
.trace_packet_header
= NULL
;
636 if (notit
->packet_header_field
) {
637 bt_packet_header_field_release(notit
->packet_header_field
);
638 notit
->packet_header_field
= NULL
;
641 notit
->dscopes
.stream_packet_context
= NULL
;
643 if (notit
->packet_context_field
) {
644 bt_packet_context_field_release(notit
->packet_context_field
);
645 notit
->packet_context_field
= NULL
;
648 release_event_dscopes(notit
);
652 enum bt_notif_iter_status
read_packet_header_begin_state(
653 struct bt_notif_iter
*notit
)
655 struct ctf_field_class
*packet_header_fc
= NULL
;
656 enum bt_notif_iter_status ret
= BT_NOTIF_ITER_STATUS_OK
;
658 if (bt_notif_iter_switch_packet(notit
)) {
659 BT_LOGW("Cannot switch packet: notit-addr=%p", notit
);
660 ret
= BT_NOTIF_ITER_STATUS_ERROR
;
664 /* Packet header class is common to the whole trace class. */
665 packet_header_fc
= notit
->meta
.tc
->packet_header_fc
;
666 if (!packet_header_fc
) {
667 notit
->state
= STATE_AFTER_TRACE_PACKET_HEADER
;
671 BT_ASSERT(!notit
->packet_header_field
);
673 if (packet_header_fc
->in_ir
) {
675 * Create free packet header field from trace class.
676 * This field is going to be moved to the packet once we
677 * create it. We cannot create the packet now because:
679 * 1. A packet is created from a stream.
680 * 2. A stream is created from a stream class.
681 * 3. We need the packet header field's content to know
682 * the ID of the stream class to select.
684 notit
->packet_header_field
=
685 bt_packet_header_field_create(
686 notit
->meta
.tc
->ir_tc
);
687 if (!notit
->packet_header_field
) {
688 BT_LOGE_STR("Cannot create packet header field wrapper from trace class.");
689 ret
= BT_NOTIF_ITER_STATUS_ERROR
;
693 notit
->dscopes
.trace_packet_header
=
694 bt_packet_header_field_borrow_field(
695 notit
->packet_header_field
);
696 BT_ASSERT(notit
->dscopes
.trace_packet_header
);
699 notit
->cur_stream_class_id
= -1;
700 notit
->cur_event_class_id
= -1;
701 notit
->cur_data_stream_id
= -1;
702 BT_LOGV("Decoding packet header field:"
703 "notit-addr=%p, trace-class-addr=%p, fc-addr=%p",
704 notit
, notit
->meta
.tc
, packet_header_fc
);
705 ret
= read_dscope_begin_state(notit
, packet_header_fc
,
706 STATE_AFTER_TRACE_PACKET_HEADER
,
707 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
708 notit
->dscopes
.trace_packet_header
);
710 BT_LOGW("Cannot decode packet header field: "
711 "notit-addr=%p, trace-class-addr=%p, "
713 notit
, notit
->meta
.tc
, packet_header_fc
);
721 enum bt_notif_iter_status
read_packet_header_continue_state(
722 struct bt_notif_iter
*notit
)
724 return read_dscope_continue_state(notit
,
725 STATE_AFTER_TRACE_PACKET_HEADER
);
729 enum bt_notif_iter_status
set_current_stream_class(struct bt_notif_iter
*notit
)
731 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
732 struct ctf_stream_class
*new_stream_class
= NULL
;
734 if (notit
->cur_stream_class_id
== -1) {
736 * No current stream class ID field, therefore only one
739 if (notit
->meta
.tc
->stream_classes
->len
!= 1) {
740 BT_LOGW("Need exactly one stream class since there's "
741 "no stream class ID field: "
742 "notit-addr=%p", notit
);
743 status
= BT_NOTIF_ITER_STATUS_ERROR
;
747 new_stream_class
= notit
->meta
.tc
->stream_classes
->pdata
[0];
748 notit
->cur_stream_class_id
= new_stream_class
->id
;
751 new_stream_class
= ctf_trace_class_borrow_stream_class_by_id(
752 notit
->meta
.tc
, notit
->cur_stream_class_id
);
753 if (!new_stream_class
) {
754 BT_LOGW("No stream class with ID of stream class ID to use in trace class: "
755 "notit-addr=%p, stream-class-id=%" PRIu64
", "
756 "trace-class-addr=%p",
757 notit
, notit
->cur_stream_class_id
, notit
->meta
.tc
);
758 status
= BT_NOTIF_ITER_STATUS_ERROR
;
762 if (notit
->meta
.sc
) {
763 if (new_stream_class
!= notit
->meta
.sc
) {
764 BT_LOGW("Two packets refer to two different stream classes within the same packet sequence: "
765 "notit-addr=%p, prev-stream-class-addr=%p, "
766 "prev-stream-class-id=%" PRId64
", "
767 "next-stream-class-addr=%p, "
768 "next-stream-class-id=%" PRId64
", "
770 notit
, notit
->meta
.sc
,
773 new_stream_class
->id
,
775 status
= BT_NOTIF_ITER_STATUS_ERROR
;
779 notit
->meta
.sc
= new_stream_class
;
782 BT_LOGV("Set current stream class: "
783 "notit-addr=%p, stream-class-addr=%p, "
784 "stream-class-id=%" PRId64
,
785 notit
, notit
->meta
.sc
, notit
->meta
.sc
->id
);
792 enum bt_notif_iter_status
set_current_stream(struct bt_notif_iter
*notit
)
794 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
795 bt_stream
*stream
= NULL
;
797 BT_LOGV("Calling user function (get stream): notit-addr=%p, "
798 "stream-class-addr=%p, stream-class-id=%" PRId64
,
799 notit
, notit
->meta
.sc
,
801 stream
= notit
->medium
.medops
.borrow_stream(
802 notit
->meta
.sc
->ir_sc
, notit
->cur_data_stream_id
,
804 bt_stream_get_ref(stream
);
805 BT_LOGV("User function returned: stream-addr=%p", stream
);
807 BT_LOGW_STR("User function failed to return a stream object "
808 "for the given stream class.");
809 status
= BT_NOTIF_ITER_STATUS_ERROR
;
813 if (notit
->stream
&& stream
!= notit
->stream
) {
814 BT_LOGW("User function returned a different stream than the "
815 "previous one for the same sequence of packets.");
816 status
= BT_NOTIF_ITER_STATUS_ERROR
;
820 BT_STREAM_MOVE_REF(notit
->stream
, stream
);
823 bt_stream_put_ref(stream
);
828 enum bt_notif_iter_status
set_current_packet(struct bt_notif_iter
*notit
)
830 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
831 bt_packet
*packet
= NULL
;
833 BT_LOGV("Creating packet for packet notification: "
834 "notit-addr=%p", notit
);
835 BT_LOGV("Creating 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
,
843 BT_ASSERT(notit
->stream
);
844 packet
= bt_packet_create(notit
->stream
);
846 BT_LOGE("Cannot create packet from stream: "
847 "notit-addr=%p, stream-addr=%p, "
848 "stream-class-addr=%p, "
849 "stream-class-id=%" PRId64
,
850 notit
, notit
->stream
, notit
->meta
.sc
,
858 BT_PACKET_PUT_REF_AND_RESET(packet
);
859 status
= BT_NOTIF_ITER_STATUS_ERROR
;
862 BT_PACKET_MOVE_REF(notit
->packet
, packet
);
867 enum bt_notif_iter_status
after_packet_header_state(
868 struct bt_notif_iter
*notit
)
870 enum bt_notif_iter_status status
;
872 status
= set_current_stream_class(notit
);
873 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
877 notit
->state
= STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
;
884 enum bt_notif_iter_status
read_packet_context_begin_state(
885 struct bt_notif_iter
*notit
)
887 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
888 struct ctf_field_class
*packet_context_fc
;
890 BT_ASSERT(notit
->meta
.sc
);
891 packet_context_fc
= notit
->meta
.sc
->packet_context_fc
;
892 if (!packet_context_fc
) {
893 BT_LOGV("No packet packet context field class in stream class: continuing: "
894 "notit-addr=%p, stream-class-addr=%p, "
895 "stream-class-id=%" PRId64
,
896 notit
, notit
->meta
.sc
,
898 notit
->state
= STATE_AFTER_STREAM_PACKET_CONTEXT
;
902 BT_ASSERT(!notit
->packet_context_field
);
904 if (packet_context_fc
->in_ir
) {
906 * Create free packet context field from stream class.
907 * This field is going to be moved to the packet once we
908 * create it. We cannot create the packet now because a
909 * packet is created from a stream, and this API must be
910 * able to return the packet header and context fields
911 * without creating a stream
912 * (bt_notif_iter_borrow_packet_header_context_fields()).
914 notit
->packet_context_field
=
915 bt_packet_context_field_create(
916 notit
->meta
.sc
->ir_sc
);
917 if (!notit
->packet_context_field
) {
918 BT_LOGE_STR("Cannot create packet context field wrapper from stream class.");
919 status
= BT_NOTIF_ITER_STATUS_ERROR
;
923 notit
->dscopes
.stream_packet_context
=
924 bt_packet_context_field_borrow_field(
925 notit
->packet_context_field
);
926 BT_ASSERT(notit
->dscopes
.stream_packet_context
);
929 BT_LOGV("Decoding packet context field: "
930 "notit-addr=%p, stream-class-addr=%p, "
931 "stream-class-id=%" PRId64
", fc-addr=%p",
932 notit
, notit
->meta
.sc
,
933 notit
->meta
.sc
->id
, packet_context_fc
);
934 status
= read_dscope_begin_state(notit
, packet_context_fc
,
935 STATE_AFTER_STREAM_PACKET_CONTEXT
,
936 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
937 notit
->dscopes
.stream_packet_context
);
939 BT_LOGW("Cannot decode packet context field: "
940 "notit-addr=%p, stream-class-addr=%p, "
941 "stream-class-id=%" PRId64
", fc-addr=%p",
942 notit
, notit
->meta
.sc
,
952 enum bt_notif_iter_status
read_packet_context_continue_state(
953 struct bt_notif_iter
*notit
)
955 return read_dscope_continue_state(notit
,
956 STATE_AFTER_STREAM_PACKET_CONTEXT
);
960 enum bt_notif_iter_status
set_current_packet_content_sizes(
961 struct bt_notif_iter
*notit
)
963 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
965 if (notit
->cur_exp_packet_total_size
== -1) {
966 if (notit
->cur_exp_packet_content_size
!= -1) {
967 BT_LOGW("Content size is set, but packet size is not: "
968 "notit-addr=%p, packet-context-field-addr=%p, "
969 "packet-size=%" PRId64
", content-size=%" PRId64
,
970 notit
, notit
->dscopes
.stream_packet_context
,
971 notit
->cur_exp_packet_total_size
,
972 notit
->cur_exp_packet_content_size
);
973 status
= BT_NOTIF_ITER_STATUS_ERROR
;
977 if (notit
->cur_exp_packet_content_size
== -1) {
978 notit
->cur_exp_packet_content_size
=
979 notit
->cur_exp_packet_total_size
;
983 if (notit
->cur_exp_packet_content_size
>
984 notit
->cur_exp_packet_total_size
) {
985 BT_LOGW("Invalid packet or content size: "
986 "content size is greater than packet size: "
987 "notit-addr=%p, packet-context-field-addr=%p, "
988 "packet-size=%" PRId64
", content-size=%" PRId64
,
989 notit
, notit
->dscopes
.stream_packet_context
,
990 notit
->cur_exp_packet_total_size
,
991 notit
->cur_exp_packet_content_size
);
992 status
= BT_NOTIF_ITER_STATUS_ERROR
;
996 BT_LOGV("Set current packet and content sizes: "
997 "notit-addr=%p, packet-size=%" PRIu64
", content-size=%" PRIu64
,
998 notit
, notit
->cur_exp_packet_total_size
,
999 notit
->cur_exp_packet_content_size
);
1005 enum bt_notif_iter_status
after_packet_context_state(
1006 struct bt_notif_iter
*notit
)
1008 enum bt_notif_iter_status status
;
1010 status
= set_current_packet_content_sizes(notit
);
1011 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
1015 if (notit
->stream_begin_emitted
) {
1016 notit
->state
= STATE_EMIT_NOTIF_NEW_PACKET
;
1018 notit
->state
= STATE_EMIT_NOTIF_NEW_STREAM
;
1026 enum bt_notif_iter_status
read_event_header_begin_state(
1027 struct bt_notif_iter
*notit
)
1029 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1030 struct ctf_field_class
*event_header_fc
= NULL
;
1032 /* Reset the position of the last event header */
1033 notit
->buf
.last_eh_at
= notit
->buf
.at
;
1034 notit
->cur_event_class_id
= -1;
1036 /* Check if we have some content left */
1037 if (notit
->cur_exp_packet_content_size
>= 0) {
1038 if (unlikely(packet_at(notit
) ==
1039 notit
->cur_exp_packet_content_size
)) {
1040 /* No more events! */
1041 BT_LOGV("Reached end of packet: notit-addr=%p, "
1042 "cur=%zu", notit
, packet_at(notit
));
1043 notit
->state
= STATE_EMIT_NOTIF_END_OF_PACKET
;
1045 } else if (unlikely(packet_at(notit
) >
1046 notit
->cur_exp_packet_content_size
)) {
1047 /* That's not supposed to happen */
1048 BT_LOGV("Before decoding event header field: cursor is passed the packet's content: "
1049 "notit-addr=%p, content-size=%" PRId64
", "
1051 notit
->cur_exp_packet_content_size
,
1053 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1058 * "Infinite" content: we're done when the medium has
1059 * nothing else for us.
1061 status
= buf_ensure_available_bits(notit
);
1062 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
1064 * If this function returns
1065 * `BT_NOTIF_ITER_STATUS_EOF`:
1067 * 1. bt_notif_iter_get_next_notification()
1068 * emits a "packet end" notification. This
1069 * resets the current packet. The state
1070 * remains unchanged otherwise.
1071 * 2. This function is called again. It returns
1072 * `BT_NOTIF_ITER_STATUS_EOF` again.
1073 * 3. bt_notif_iter_get_next_notification()
1074 * emits a "stream end" notification because
1075 * there's no current packet. It sets the
1076 * current state to `STATE_DONE`.
1082 release_event_dscopes(notit
);
1083 BT_ASSERT(notit
->meta
.sc
);
1084 event_header_fc
= notit
->meta
.sc
->event_header_fc
;
1085 if (!event_header_fc
) {
1086 notit
->state
= STATE_AFTER_EVENT_HEADER
;
1090 if (event_header_fc
->in_ir
) {
1091 BT_ASSERT(!notit
->event_header_field
);
1092 notit
->event_header_field
=
1093 bt_event_header_field_create(
1094 notit
->meta
.sc
->ir_sc
);
1095 if (!notit
->event_header_field
) {
1096 BT_LOGE_STR("Cannot create event header field wrapper from trace class.");
1097 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1101 notit
->dscopes
.event_header
=
1102 bt_event_header_field_borrow_field(
1103 notit
->event_header_field
);
1104 BT_ASSERT(notit
->dscopes
.event_header
);
1107 BT_LOGV("Decoding event header field: "
1108 "notit-addr=%p, stream-class-addr=%p, "
1109 "stream-class-id=%" PRId64
", "
1111 notit
, notit
->meta
.sc
,
1114 status
= read_dscope_begin_state(notit
, event_header_fc
,
1115 STATE_AFTER_EVENT_HEADER
,
1116 STATE_DSCOPE_EVENT_HEADER_CONTINUE
,
1117 notit
->dscopes
.event_header
);
1119 BT_LOGW("Cannot decode event header field: "
1120 "notit-addr=%p, stream-class-addr=%p, "
1121 "stream-class-id=%" PRId64
", fc-addr=%p",
1122 notit
, notit
->meta
.sc
,
1132 enum bt_notif_iter_status
read_event_header_continue_state(
1133 struct bt_notif_iter
*notit
)
1135 return read_dscope_continue_state(notit
,
1136 STATE_AFTER_EVENT_HEADER
);
1140 enum bt_notif_iter_status
set_current_event_class(struct bt_notif_iter
*notit
)
1142 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1144 struct ctf_event_class
*new_event_class
= NULL
;
1146 if (notit
->cur_event_class_id
== -1) {
1148 * No current event class ID field, therefore only one
1151 if (notit
->meta
.sc
->event_classes
->len
!= 1) {
1152 BT_LOGW("Need exactly one event class since there's "
1153 "no event class ID field: "
1154 "notit-addr=%p", notit
);
1155 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1159 new_event_class
= notit
->meta
.sc
->event_classes
->pdata
[0];
1160 notit
->cur_event_class_id
= new_event_class
->id
;
1163 new_event_class
= ctf_stream_class_borrow_event_class_by_id(
1164 notit
->meta
.sc
, notit
->cur_event_class_id
);
1165 if (!new_event_class
) {
1166 BT_LOGW("No event class with ID of event class ID to use in stream class: "
1167 "notit-addr=%p, stream-class-id=%" PRIu64
", "
1168 "event-class-id=%" PRIu64
", "
1169 "trace-class-addr=%p",
1170 notit
, notit
->meta
.sc
->id
, notit
->cur_event_class_id
,
1172 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1176 notit
->meta
.ec
= new_event_class
;
1177 BT_LOGV("Set current event class: "
1178 "notit-addr=%p, event-class-addr=%p, "
1179 "event-class-id=%" PRId64
", "
1180 "event-class-name=\"%s\"",
1181 notit
, notit
->meta
.ec
, notit
->meta
.ec
->id
,
1182 notit
->meta
.ec
->name
->str
);
1189 enum bt_notif_iter_status
set_current_event_notification(
1190 struct bt_notif_iter
*notit
)
1192 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1193 bt_notification
*notif
= NULL
;
1195 BT_ASSERT(notit
->meta
.ec
);
1196 BT_ASSERT(notit
->packet
);
1197 BT_LOGV("Creating event notification from event class and packet: "
1198 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", packet-addr=%p",
1199 notit
, notit
->meta
.ec
,
1200 notit
->meta
.ec
->name
->str
,
1202 BT_ASSERT(notit
->notif_iter
);
1203 notif
= bt_notification_event_create(notit
->notif_iter
,
1204 notit
->meta
.ec
->ir_ec
, notit
->packet
);
1206 BT_LOGE("Cannot create event notification: "
1207 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", "
1209 notit
, notit
->meta
.ec
,
1210 notit
->meta
.ec
->name
->str
,
1218 BT_NOTIFICATION_PUT_REF_AND_RESET(notif
);
1219 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1222 BT_NOTIFICATION_MOVE_REF(notit
->event_notif
, notif
);
1227 enum bt_notif_iter_status
after_event_header_state(
1228 struct bt_notif_iter
*notit
)
1230 enum bt_notif_iter_status status
;
1232 status
= set_current_event_class(notit
);
1233 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
1237 status
= set_current_event_notification(notit
);
1238 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
1242 notit
->event
= bt_notification_event_borrow_event(
1243 notit
->event_notif
);
1244 BT_ASSERT(notit
->event
);
1246 if (notit
->event_header_field
) {
1249 BT_ASSERT(notit
->event
);
1250 ret
= bt_event_move_header_field(notit
->event
,
1251 notit
->event_header_field
);
1253 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1257 notit
->event_header_field
= NULL
;
1260 * At this point notit->dscopes.event_header has
1261 * the same value as the event header field within
1264 BT_ASSERT(bt_event_borrow_header_field(
1265 notit
->event
) == notit
->dscopes
.event_header
);
1268 notit
->state
= STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
;
1275 enum bt_notif_iter_status
read_event_common_context_begin_state(
1276 struct bt_notif_iter
*notit
)
1278 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1279 struct ctf_field_class
*event_common_context_fc
;
1281 event_common_context_fc
= notit
->meta
.sc
->event_common_context_fc
;
1282 if (!event_common_context_fc
) {
1283 notit
->state
= STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
;
1287 if (event_common_context_fc
->in_ir
) {
1288 BT_ASSERT(!notit
->dscopes
.event_common_context
);
1289 notit
->dscopes
.event_common_context
=
1290 bt_event_borrow_common_context_field(
1292 BT_ASSERT(notit
->dscopes
.event_common_context
);
1295 BT_LOGV("Decoding event common context field: "
1296 "notit-addr=%p, stream-class-addr=%p, "
1297 "stream-class-id=%" PRId64
", "
1299 notit
, notit
->meta
.sc
,
1301 event_common_context_fc
);
1302 status
= read_dscope_begin_state(notit
, event_common_context_fc
,
1303 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
,
1304 STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
,
1305 notit
->dscopes
.event_common_context
);
1307 BT_LOGW("Cannot decode event common context field: "
1308 "notit-addr=%p, stream-class-addr=%p, "
1309 "stream-class-id=%" PRId64
", fc-addr=%p",
1310 notit
, notit
->meta
.sc
,
1312 event_common_context_fc
);
1320 enum bt_notif_iter_status
read_event_common_context_continue_state(
1321 struct bt_notif_iter
*notit
)
1323 return read_dscope_continue_state(notit
,
1324 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
);
1328 enum bt_notif_iter_status
read_event_spec_context_begin_state(
1329 struct bt_notif_iter
*notit
)
1331 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1332 struct ctf_field_class
*event_spec_context_fc
;
1334 event_spec_context_fc
= notit
->meta
.ec
->spec_context_fc
;
1335 if (!event_spec_context_fc
) {
1336 notit
->state
= STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
;
1340 if (event_spec_context_fc
->in_ir
) {
1341 BT_ASSERT(!notit
->dscopes
.event_spec_context
);
1342 notit
->dscopes
.event_spec_context
=
1343 bt_event_borrow_specific_context_field(
1345 BT_ASSERT(notit
->dscopes
.event_spec_context
);
1348 BT_LOGV("Decoding event specific context 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
,
1355 event_spec_context_fc
);
1356 status
= read_dscope_begin_state(notit
, event_spec_context_fc
,
1357 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
1358 STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
,
1359 notit
->dscopes
.event_spec_context
);
1361 BT_LOGW("Cannot decode event specific context 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
,
1368 event_spec_context_fc
);
1376 enum bt_notif_iter_status
read_event_spec_context_continue_state(
1377 struct bt_notif_iter
*notit
)
1379 return read_dscope_continue_state(notit
,
1380 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
);
1384 enum bt_notif_iter_status
read_event_payload_begin_state(
1385 struct bt_notif_iter
*notit
)
1387 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1388 struct ctf_field_class
*event_payload_fc
;
1390 event_payload_fc
= notit
->meta
.ec
->payload_fc
;
1391 if (!event_payload_fc
) {
1392 notit
->state
= STATE_EMIT_NOTIF_EVENT
;
1396 if (event_payload_fc
->in_ir
) {
1397 BT_ASSERT(!notit
->dscopes
.event_payload
);
1398 notit
->dscopes
.event_payload
=
1399 bt_event_borrow_payload_field(
1401 BT_ASSERT(notit
->dscopes
.event_payload
);
1404 BT_LOGV("Decoding event payload field: "
1405 "notit-addr=%p, event-class-addr=%p, "
1406 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1408 notit
, notit
->meta
.ec
,
1409 notit
->meta
.ec
->name
->str
,
1412 status
= read_dscope_begin_state(notit
, event_payload_fc
,
1413 STATE_EMIT_NOTIF_EVENT
,
1414 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
1415 notit
->dscopes
.event_payload
);
1417 BT_LOGW("Cannot decode event payload field: "
1418 "notit-addr=%p, event-class-addr=%p, "
1419 "event-class-name=\"%s\", "
1420 "event-class-id=%" PRId64
", fc-addr=%p",
1421 notit
, notit
->meta
.ec
,
1422 notit
->meta
.ec
->name
->str
,
1432 enum bt_notif_iter_status
read_event_payload_continue_state(
1433 struct bt_notif_iter
*notit
)
1435 return read_dscope_continue_state(notit
, STATE_EMIT_NOTIF_EVENT
);
1439 enum bt_notif_iter_status
skip_packet_padding_state(
1440 struct bt_notif_iter
*notit
)
1442 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1443 size_t bits_to_skip
;
1445 BT_ASSERT(notit
->cur_exp_packet_total_size
> 0);
1446 bits_to_skip
= notit
->cur_exp_packet_total_size
- packet_at(notit
);
1447 if (bits_to_skip
== 0) {
1448 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1451 size_t bits_to_consume
;
1453 BT_LOGV("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu",
1454 bits_to_skip
, notit
, bits_to_skip
);
1455 status
= buf_ensure_available_bits(notit
);
1456 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
1460 bits_to_consume
= MIN(buf_available_bits(notit
), bits_to_skip
);
1461 BT_LOGV("Skipping %zu bits of padding: notit-addr=%p, size=%zu",
1462 bits_to_consume
, notit
, bits_to_consume
);
1463 buf_consume_bits(notit
, bits_to_consume
);
1464 bits_to_skip
= notit
->cur_exp_packet_total_size
-
1466 if (bits_to_skip
== 0) {
1467 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1477 enum bt_notif_iter_status
handle_state(struct bt_notif_iter
*notit
)
1479 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1480 const enum state state
= notit
->state
;
1482 BT_LOGV("Handling state: notit-addr=%p, state=%s",
1483 notit
, state_string(state
));
1485 // TODO: optimalize!
1488 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1490 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
1491 status
= read_packet_header_begin_state(notit
);
1493 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
1494 status
= read_packet_header_continue_state(notit
);
1496 case STATE_AFTER_TRACE_PACKET_HEADER
:
1497 status
= after_packet_header_state(notit
);
1499 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
1500 status
= read_packet_context_begin_state(notit
);
1502 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
1503 status
= read_packet_context_continue_state(notit
);
1505 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
1506 status
= after_packet_context_state(notit
);
1508 case STATE_EMIT_NOTIF_NEW_STREAM
:
1509 notit
->state
= STATE_EMIT_NOTIF_NEW_PACKET
;
1511 case STATE_EMIT_NOTIF_NEW_PACKET
:
1512 notit
->state
= STATE_DSCOPE_EVENT_HEADER_BEGIN
;
1514 case STATE_DSCOPE_EVENT_HEADER_BEGIN
:
1515 status
= read_event_header_begin_state(notit
);
1517 case STATE_DSCOPE_EVENT_HEADER_CONTINUE
:
1518 status
= read_event_header_continue_state(notit
);
1520 case STATE_AFTER_EVENT_HEADER
:
1521 status
= after_event_header_state(notit
);
1523 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
:
1524 status
= read_event_common_context_begin_state(notit
);
1526 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
:
1527 status
= read_event_common_context_continue_state(notit
);
1529 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
:
1530 status
= read_event_spec_context_begin_state(notit
);
1532 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
:
1533 status
= read_event_spec_context_continue_state(notit
);
1535 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
1536 status
= read_event_payload_begin_state(notit
);
1538 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
1539 status
= read_event_payload_continue_state(notit
);
1541 case STATE_EMIT_NOTIF_EVENT
:
1542 notit
->state
= STATE_DSCOPE_EVENT_HEADER_BEGIN
;
1544 case STATE_SKIP_PACKET_PADDING
:
1545 status
= skip_packet_padding_state(notit
);
1547 case STATE_EMIT_NOTIF_END_OF_PACKET
:
1548 notit
->state
= STATE_SKIP_PACKET_PADDING
;
1551 BT_LOGD("Unknown CTF plugin notification iterator state: "
1552 "notit-addr=%p, state=%d", notit
, notit
->state
);
1556 BT_LOGV("Handled state: notit-addr=%p, status=%s, "
1557 "prev-state=%s, cur-state=%s",
1558 notit
, bt_notif_iter_status_string(status
),
1559 state_string(state
), state_string(notit
->state
));
1564 * Resets the internal state of a CTF notification iterator.
1567 void bt_notif_iter_reset(struct bt_notif_iter
*notit
)
1570 BT_LOGD("Resetting notification iterator: addr=%p", notit
);
1571 stack_clear(notit
->stack
);
1572 notit
->meta
.sc
= NULL
;
1573 notit
->meta
.ec
= NULL
;
1574 BT_PACKET_PUT_REF_AND_RESET(notit
->packet
);
1575 BT_STREAM_PUT_REF_AND_RESET(notit
->stream
);
1576 BT_NOTIFICATION_PUT_REF_AND_RESET(notit
->event_notif
);
1577 release_all_dscopes(notit
);
1578 notit
->cur_dscope_field
= NULL
;
1580 if (notit
->packet_header_field
) {
1581 bt_packet_header_field_release(notit
->packet_header_field
);
1582 notit
->packet_header_field
= NULL
;
1585 if (notit
->packet_context_field
) {
1586 bt_packet_context_field_release(notit
->packet_context_field
);
1587 notit
->packet_context_field
= NULL
;
1590 if (notit
->event_header_field
) {
1591 bt_event_header_field_release(notit
->event_header_field
);
1592 notit
->event_header_field
= NULL
;
1595 notit
->buf
.addr
= NULL
;
1598 notit
->buf
.last_eh_at
= SIZE_MAX
;
1599 notit
->buf
.packet_offset
= 0;
1600 notit
->state
= STATE_INIT
;
1601 notit
->cur_exp_packet_content_size
= -1;
1602 notit
->cur_exp_packet_total_size
= -1;
1603 notit
->cur_packet_offset
= -1;
1604 notit
->cur_stream_class_id
= -1;
1605 notit
->cur_event_class_id
= -1;
1606 notit
->cur_data_stream_id
= -1;
1607 notit
->stream_begin_emitted
= false;
1611 int bt_notif_iter_switch_packet(struct bt_notif_iter
*notit
)
1616 * We don't put the stream class here because we need to make
1617 * sure that all the packets processed by the same notification
1618 * iterator refer to the same stream class (the first one).
1622 if (notit
->cur_exp_packet_total_size
!= -1) {
1623 notit
->cur_packet_offset
+= notit
->cur_exp_packet_total_size
;
1626 BT_LOGV("Switching packet: notit-addr=%p, cur=%zu, "
1627 "packet-offset=%" PRId64
, notit
, notit
->buf
.at
,
1628 notit
->cur_packet_offset
);
1629 stack_clear(notit
->stack
);
1630 notit
->meta
.ec
= NULL
;
1631 BT_PACKET_PUT_REF_AND_RESET(notit
->packet
);
1632 BT_NOTIFICATION_PUT_REF_AND_RESET(notit
->event_notif
);
1633 release_all_dscopes(notit
);
1634 notit
->cur_dscope_field
= NULL
;
1637 * Adjust current buffer so that addr points to the beginning of the new
1640 if (notit
->buf
.addr
) {
1641 size_t consumed_bytes
= (size_t) (notit
->buf
.at
/ CHAR_BIT
);
1643 /* Packets are assumed to start on a byte frontier. */
1644 if (notit
->buf
.at
% CHAR_BIT
) {
1645 BT_LOGW("Cannot switch packet: current position is not a multiple of 8: "
1646 "notit-addr=%p, cur=%zu", notit
, notit
->buf
.at
);
1651 notit
->buf
.addr
+= consumed_bytes
;
1652 notit
->buf
.sz
-= consumed_bytes
;
1654 notit
->buf
.packet_offset
= 0;
1655 BT_LOGV("Adjusted buffer: addr=%p, size=%zu",
1656 notit
->buf
.addr
, notit
->buf
.sz
);
1659 notit
->cur_exp_packet_content_size
= -1;
1660 notit
->cur_exp_packet_total_size
= -1;
1661 notit
->cur_stream_class_id
= -1;
1662 notit
->cur_event_class_id
= -1;
1663 notit
->cur_data_stream_id
= -1;
1664 notit
->snapshots
.discarded_events
= UINT64_C(-1);
1665 notit
->snapshots
.packets
= UINT64_C(-1);
1666 notit
->snapshots
.beginning_clock
= UINT64_C(-1);
1667 notit
->snapshots
.end_clock
= UINT64_C(-1);
1674 bt_field
*borrow_next_field(struct bt_notif_iter
*notit
)
1676 bt_field
*next_field
= NULL
;
1677 bt_field
*base_field
;
1678 const bt_field_class
*base_fc
;
1681 BT_ASSERT(!stack_empty(notit
->stack
));
1682 index
= stack_top(notit
->stack
)->index
;
1683 base_field
= stack_top(notit
->stack
)->base
;
1684 BT_ASSERT(base_field
);
1685 base_fc
= bt_field_borrow_class_const(base_field
);
1688 switch (bt_field_class_get_type(base_fc
)) {
1689 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
1692 bt_field_class_structure_get_member_count(
1693 bt_field_borrow_class_const(
1696 bt_field_structure_borrow_member_field_by_index(
1700 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
1701 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
1702 BT_ASSERT(index
< bt_field_array_get_length(base_field
));
1703 next_field
= bt_field_array_borrow_element_field_by_index(
1706 case BT_FIELD_CLASS_TYPE_VARIANT
:
1707 BT_ASSERT(index
== 0);
1708 next_field
= bt_field_variant_borrow_selected_option_field(
1715 BT_ASSERT(next_field
);
1720 void update_default_clock(struct bt_notif_iter
*notit
, uint64_t new_val
,
1721 uint64_t new_val_size
)
1723 uint64_t new_val_mask
;
1724 uint64_t cur_value_masked
;
1726 BT_ASSERT(new_val_size
> 0);
1729 * Special case for a 64-bit new value, which is the limit
1730 * of a clock value as of this version: overwrite the
1731 * current value directly.
1733 if (new_val_size
== 64) {
1734 notit
->default_clock_val
= new_val
;
1738 new_val_mask
= (1ULL << new_val_size
) - 1;
1739 cur_value_masked
= notit
->default_clock_val
& new_val_mask
;
1741 if (new_val
< cur_value_masked
) {
1743 * It looks like a wrap happened on the number of bits
1744 * of the requested new value. Assume that the clock
1745 * value wrapped only one time.
1747 notit
->default_clock_val
+= new_val_mask
+ 1;
1750 /* Clear the low bits of the current clock value. */
1751 notit
->default_clock_val
&= ~new_val_mask
;
1753 /* Set the low bits of the current clock value. */
1754 notit
->default_clock_val
|= new_val
;
1757 BT_LOGV("Updated default clock's value from integer field's value: "
1758 "value=%" PRIu64
, notit
->default_clock_val
);
1762 enum bt_bfcr_status
bfcr_unsigned_int_cb(uint64_t value
,
1763 struct ctf_field_class
*fc
, void *data
)
1765 struct bt_notif_iter
*notit
= data
;
1766 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
1767 bt_field
*field
= NULL
;
1768 struct ctf_field_class_int
*int_fc
= (void *) fc
;
1770 BT_LOGV("Unsigned integer function called from BFCR: "
1771 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1772 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64
,
1773 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
1775 if (likely(int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
)) {
1776 goto update_def_clock
;
1779 switch (int_fc
->meaning
) {
1780 case CTF_FIELD_CLASS_MEANING_EVENT_CLASS_ID
:
1781 notit
->cur_event_class_id
= value
;
1783 case CTF_FIELD_CLASS_MEANING_DATA_STREAM_ID
:
1784 notit
->cur_data_stream_id
= value
;
1786 case CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME
:
1787 notit
->snapshots
.beginning_clock
= value
;
1789 case CTF_FIELD_CLASS_MEANING_PACKET_END_TIME
:
1790 notit
->snapshots
.end_clock
= value
;
1792 case CTF_FIELD_CLASS_MEANING_STREAM_CLASS_ID
:
1793 notit
->cur_stream_class_id
= value
;
1795 case CTF_FIELD_CLASS_MEANING_MAGIC
:
1796 if (value
!= 0xc1fc1fc1) {
1797 BT_LOGW("Invalid CTF magic number: notit-addr=%p, "
1798 "magic=%" PRIx64
, notit
, value
);
1799 status
= BT_BFCR_STATUS_ERROR
;
1804 case CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT
:
1805 notit
->snapshots
.packets
= value
;
1807 case CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT
:
1808 notit
->snapshots
.discarded_events
= value
;
1810 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_TOTAL_SIZE
:
1811 notit
->cur_exp_packet_total_size
= value
;
1813 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_CONTENT_SIZE
:
1814 notit
->cur_exp_packet_content_size
= value
;
1821 if (unlikely(int_fc
->mapped_clock_class
)) {
1822 update_default_clock(notit
, value
, int_fc
->base
.size
);
1825 if (unlikely(int_fc
->storing_index
>= 0)) {
1826 g_array_index(notit
->stored_values
, uint64_t,
1827 (uint64_t) int_fc
->storing_index
) = value
;
1830 if (unlikely(!fc
->in_ir
)) {
1834 field
= borrow_next_field(notit
);
1836 BT_ASSERT(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
1837 BT_ASSERT(bt_field_get_class_type(field
) ==
1838 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
||
1839 bt_field_get_class_type(field
) ==
1840 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
);
1841 bt_field_unsigned_integer_set_value(field
, value
);
1842 stack_top(notit
->stack
)->index
++;
1849 enum bt_bfcr_status
bfcr_unsigned_int_char_cb(uint64_t value
,
1850 struct ctf_field_class
*fc
, void *data
)
1853 struct bt_notif_iter
*notit
= data
;
1854 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
1855 bt_field
*string_field
= NULL
;
1856 struct ctf_field_class_int
*int_fc
= (void *) fc
;
1857 char str
[2] = {'\0', '\0'};
1859 BT_LOGV("Unsigned integer character function called from BFCR: "
1860 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1861 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64
,
1862 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
1863 BT_ASSERT(int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
);
1864 BT_ASSERT(!int_fc
->mapped_clock_class
);
1865 BT_ASSERT(int_fc
->storing_index
< 0);
1867 if (unlikely(!fc
->in_ir
)) {
1871 if (notit
->done_filling_string
) {
1876 notit
->done_filling_string
= true;
1880 string_field
= stack_top(notit
->stack
)->base
;
1881 BT_ASSERT(bt_field_get_class_type(string_field
) ==
1882 BT_FIELD_CLASS_TYPE_STRING
);
1884 /* Append character */
1885 str
[0] = (char) value
;
1886 ret
= bt_field_string_append_with_length(string_field
, str
, 1);
1888 BT_LOGE("Cannot append character to string field's value: "
1889 "notit-addr=%p, field-addr=%p, ret=%d",
1890 notit
, string_field
, ret
);
1891 status
= BT_BFCR_STATUS_ERROR
;
1900 enum bt_bfcr_status
bfcr_signed_int_cb(int64_t value
,
1901 struct ctf_field_class
*fc
, void *data
)
1903 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
1904 bt_field
*field
= NULL
;
1905 struct bt_notif_iter
*notit
= data
;
1906 struct ctf_field_class_int
*int_fc
= (void *) fc
;
1908 BT_LOGV("Signed integer function called from BFCR: "
1909 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1910 "fc-type=%d, fc-in-ir=%d, value=%" PRId64
,
1911 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
1912 BT_ASSERT(int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
);
1914 if (unlikely(int_fc
->storing_index
>= 0)) {
1915 g_array_index(notit
->stored_values
, uint64_t,
1916 (uint64_t) int_fc
->storing_index
) = (uint64_t) value
;
1919 if (unlikely(!fc
->in_ir
)) {
1923 field
= borrow_next_field(notit
);
1925 BT_ASSERT(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
1926 BT_ASSERT(bt_field_get_class_type(field
) ==
1927 BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
||
1928 bt_field_get_class_type(field
) ==
1929 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
);
1930 bt_field_signed_integer_set_value(field
, value
);
1931 stack_top(notit
->stack
)->index
++;
1938 enum bt_bfcr_status
bfcr_floating_point_cb(double value
,
1939 struct ctf_field_class
*fc
, void *data
)
1941 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
1942 bt_field
*field
= NULL
;
1943 struct bt_notif_iter
*notit
= data
;
1945 BT_LOGV("Floating point number function called from BFCR: "
1946 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1947 "fc-type=%d, fc-in-ir=%d, value=%f",
1948 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
, value
);
1949 BT_ASSERT(fc
->in_ir
);
1950 field
= borrow_next_field(notit
);
1952 BT_ASSERT(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
1953 BT_ASSERT(bt_field_get_class_type(field
) ==
1954 BT_FIELD_CLASS_TYPE_REAL
);
1955 bt_field_real_set_value(field
, value
);
1956 stack_top(notit
->stack
)->index
++;
1961 enum bt_bfcr_status
bfcr_string_begin_cb(
1962 struct ctf_field_class
*fc
, void *data
)
1964 bt_field
*field
= NULL
;
1965 struct bt_notif_iter
*notit
= data
;
1968 BT_LOGV("String (beginning) function called from BFCR: "
1969 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1970 "fc-type=%d, fc-in-ir=%d",
1971 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
1973 BT_ASSERT(fc
->in_ir
);
1974 field
= borrow_next_field(notit
);
1976 BT_ASSERT(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
1977 BT_ASSERT(bt_field_get_class_type(field
) ==
1978 BT_FIELD_CLASS_TYPE_STRING
);
1979 ret
= bt_field_string_clear(field
);
1980 BT_ASSERT(ret
== 0);
1983 * Push on stack. Not a compound class per se, but we know that
1984 * only bfcr_string_cb() may be called between this call and a
1985 * subsequent call to bfcr_string_end_cb().
1987 stack_push(notit
->stack
, field
);
1988 return BT_BFCR_STATUS_OK
;
1992 enum bt_bfcr_status
bfcr_string_cb(const char *value
,
1993 size_t len
, struct ctf_field_class
*fc
, void *data
)
1995 enum bt_bfcr_status status
= BT_BFCR_STATUS_OK
;
1996 bt_field
*field
= NULL
;
1997 struct bt_notif_iter
*notit
= data
;
2000 BT_LOGV("String (substring) function called from BFCR: "
2001 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2002 "fc-type=%d, fc-in-ir=%d, string-length=%zu",
2003 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
,
2005 BT_ASSERT(fc
->in_ir
);
2006 field
= stack_top(notit
->stack
)->base
;
2009 /* Append current substring */
2010 ret
= bt_field_string_append_with_length(field
, value
, len
);
2012 BT_LOGE("Cannot append substring to string field's value: "
2013 "notit-addr=%p, field-addr=%p, string-length=%zu, "
2014 "ret=%d", notit
, field
, len
, ret
);
2015 status
= BT_BFCR_STATUS_ERROR
;
2024 enum bt_bfcr_status
bfcr_string_end_cb(
2025 struct ctf_field_class
*fc
, void *data
)
2027 struct bt_notif_iter
*notit
= data
;
2029 BT_LOGV("String (end) function called from BFCR: "
2030 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2031 "fc-type=%d, fc-in-ir=%d",
2032 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
2033 BT_ASSERT(fc
->in_ir
);
2035 /* Pop string field */
2036 stack_pop(notit
->stack
);
2038 /* Go to next field */
2039 stack_top(notit
->stack
)->index
++;
2040 return BT_BFCR_STATUS_OK
;
2043 enum bt_bfcr_status
bfcr_compound_begin_cb(
2044 struct ctf_field_class
*fc
, void *data
)
2046 struct bt_notif_iter
*notit
= data
;
2049 BT_LOGV("Compound (beginning) function called from BFCR: "
2050 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2051 "fc-type=%d, fc-in-ir=%d",
2052 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
2059 if (stack_empty(notit
->stack
)) {
2060 /* Root: already set by read_dscope_begin_state() */
2061 field
= notit
->cur_dscope_field
;
2063 field
= borrow_next_field(notit
);
2069 BT_ASSERT(bt_field_borrow_class_const(field
) == fc
->ir_fc
);
2070 stack_push(notit
->stack
, field
);
2073 * Change BFCR "unsigned int" callback if it's a text
2076 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
||
2077 fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
2078 struct ctf_field_class_array_base
*array_fc
= (void *) fc
;
2080 if (array_fc
->is_text
) {
2083 BT_ASSERT(bt_field_get_class_type(field
) ==
2084 BT_FIELD_CLASS_TYPE_STRING
);
2085 notit
->done_filling_string
= false;
2086 ret
= bt_field_string_clear(field
);
2087 BT_ASSERT(ret
== 0);
2088 bt_bfcr_set_unsigned_int_cb(notit
->bfcr
,
2089 bfcr_unsigned_int_char_cb
);
2094 return BT_BFCR_STATUS_OK
;
2097 enum bt_bfcr_status
bfcr_compound_end_cb(
2098 struct ctf_field_class
*fc
, void *data
)
2100 struct bt_notif_iter
*notit
= data
;
2102 BT_LOGV("Compound (end) function called from BFCR: "
2103 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2104 "fc-type=%d, fc-in-ir=%d",
2105 notit
, notit
->bfcr
, fc
, fc
->type
, fc
->in_ir
);
2111 BT_ASSERT(!stack_empty(notit
->stack
));
2112 BT_ASSERT(bt_field_borrow_class_const(stack_top(notit
->stack
)->base
) ==
2116 * Reset BFCR "unsigned int" callback if it's a text
2119 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
||
2120 fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
2121 struct ctf_field_class_array_base
*array_fc
= (void *) fc
;
2123 if (array_fc
->is_text
) {
2124 BT_ASSERT(bt_field_get_class_type(
2125 stack_top(notit
->stack
)->base
) ==
2126 BT_FIELD_CLASS_TYPE_STRING
);
2127 bt_bfcr_set_unsigned_int_cb(notit
->bfcr
,
2128 bfcr_unsigned_int_cb
);
2133 stack_pop(notit
->stack
);
2135 /* If the stack is not empty, increment the base's index */
2136 if (!stack_empty(notit
->stack
)) {
2137 stack_top(notit
->stack
)->index
++;
2141 return BT_BFCR_STATUS_OK
;
2145 int64_t bfcr_get_sequence_length_cb(struct ctf_field_class
*fc
, void *data
)
2147 bt_field
*seq_field
;
2148 struct bt_notif_iter
*notit
= data
;
2149 struct ctf_field_class_sequence
*seq_fc
= (void *) fc
;
2150 int64_t length
= -1;
2153 length
= (uint64_t) g_array_index(notit
->stored_values
, uint64_t,
2154 seq_fc
->stored_length_index
);
2155 seq_field
= stack_top(notit
->stack
)->base
;
2156 BT_ASSERT(seq_field
);
2157 ret
= bt_field_dynamic_array_set_length(seq_field
, (uint64_t) length
);
2159 BT_LOGE("Cannot set dynamic array field's length field: "
2160 "notit-addr=%p, field-addr=%p, "
2161 "length=%" PRIu64
, notit
, seq_field
, length
);
2168 struct ctf_field_class
*bfcr_borrow_variant_selected_field_class_cb(
2169 struct ctf_field_class
*fc
, void *data
)
2173 int64_t option_index
= -1;
2174 struct bt_notif_iter
*notit
= data
;
2175 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
2176 struct ctf_named_field_class
*selected_option
= NULL
;
2177 struct ctf_field_class
*ret_fc
= NULL
;
2183 /* Get variant's tag */
2184 tag
.u
= g_array_index(notit
->stored_values
, uint64_t,
2185 var_fc
->stored_tag_index
);
2188 * Check each range to find the selected option's index.
2190 if (var_fc
->tag_fc
->base
.is_signed
) {
2191 for (i
= 0; i
< var_fc
->ranges
->len
; i
++) {
2192 struct ctf_field_class_variant_range
*range
=
2193 ctf_field_class_variant_borrow_range_by_index(
2196 if (tag
.i
>= range
->range
.lower
.i
&&
2197 tag
.i
<= range
->range
.upper
.i
) {
2198 option_index
= (int64_t) range
->option_index
;
2203 for (i
= 0; i
< var_fc
->ranges
->len
; i
++) {
2204 struct ctf_field_class_variant_range
*range
=
2205 ctf_field_class_variant_borrow_range_by_index(
2208 if (tag
.u
>= range
->range
.lower
.u
&&
2209 tag
.u
<= range
->range
.upper
.u
) {
2210 option_index
= (int64_t) range
->option_index
;
2216 if (option_index
< 0) {
2217 BT_LOGW("Cannot find variant field class's option: "
2218 "notit-addr=%p, var-fc-addr=%p, u-tag=%" PRIu64
", "
2219 "i-tag=%" PRId64
, notit
, var_fc
, tag
.u
, tag
.i
);
2223 selected_option
= ctf_field_class_variant_borrow_option_by_index(
2224 var_fc
, (uint64_t) option_index
);
2226 if (selected_option
->fc
->in_ir
) {
2227 bt_field
*var_field
= stack_top(notit
->stack
)->base
;
2229 ret
= bt_field_variant_select_option_field(
2230 var_field
, option_index
);
2232 BT_LOGW("Cannot select variant field's option field: "
2233 "notit-addr=%p, var-field-addr=%p, "
2234 "opt-index=%" PRId64
, notit
, var_field
,
2240 ret_fc
= selected_option
->fc
;
2247 void set_event_default_clock_value(struct bt_notif_iter
*notit
)
2250 bt_notification_event_borrow_event(
2251 notit
->event_notif
);
2252 bt_stream_class
*sc
= notit
->meta
.sc
->ir_sc
;
2256 if (bt_stream_class_borrow_default_clock_class(sc
)) {
2257 bt_event_set_default_clock_value(event
,
2258 notit
->default_clock_val
);
2263 void notify_new_stream(struct bt_notif_iter
*notit
,
2264 bt_notification
**notification
)
2266 enum bt_notif_iter_status status
;
2267 bt_notification
*ret
= NULL
;
2269 status
= set_current_stream(notit
);
2270 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
2271 BT_NOTIFICATION_PUT_REF_AND_RESET(ret
);
2275 BT_ASSERT(notit
->stream
);
2276 BT_ASSERT(notit
->notif_iter
);
2277 ret
= bt_notification_stream_beginning_create(notit
->notif_iter
,
2280 BT_LOGE("Cannot create stream beginning notification: "
2281 "notit-addr=%p, stream-addr=%p",
2282 notit
, notit
->stream
);
2287 *notification
= ret
;
2291 void notify_end_of_stream(struct bt_notif_iter
*notit
,
2292 bt_notification
**notification
)
2294 bt_notification
*ret
;
2296 if (!notit
->stream
) {
2297 BT_LOGE("Cannot create stream for stream notification: "
2298 "notit-addr=%p", notit
);
2302 BT_ASSERT(notit
->notif_iter
);
2303 ret
= bt_notification_stream_end_create(notit
->notif_iter
,
2306 BT_LOGE("Cannot create stream beginning notification: "
2307 "notit-addr=%p, stream-addr=%p",
2308 notit
, notit
->stream
);
2311 *notification
= ret
;
2315 void notify_new_packet(struct bt_notif_iter
*notit
,
2316 bt_notification
**notification
)
2319 enum bt_notif_iter_status status
;
2320 bt_notification
*notif
= NULL
;
2321 const bt_stream_class
*sc
;
2323 status
= set_current_packet(notit
);
2324 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
2328 BT_ASSERT(notit
->packet
);
2329 sc
= notit
->meta
.sc
->ir_sc
;
2332 if (bt_stream_class_packets_have_discarded_event_counter_snapshot(sc
)) {
2333 BT_ASSERT(notit
->snapshots
.discarded_events
!= UINT64_C(-1));
2334 bt_packet_set_discarded_event_counter_snapshot(
2335 notit
->packet
, notit
->snapshots
.discarded_events
);
2338 if (bt_stream_class_packets_have_packet_counter_snapshot(sc
)) {
2339 BT_ASSERT(notit
->snapshots
.packets
!= UINT64_C(-1));
2340 bt_packet_set_packet_counter_snapshot(
2341 notit
->packet
, notit
->snapshots
.packets
);
2344 if (bt_stream_class_packets_have_default_beginning_clock_value(sc
)) {
2345 BT_ASSERT(notit
->snapshots
.beginning_clock
!= UINT64_C(-1));
2346 bt_packet_set_default_beginning_clock_value(
2347 notit
->packet
, notit
->snapshots
.beginning_clock
);
2350 if (bt_stream_class_packets_have_default_end_clock_value(sc
)) {
2351 BT_ASSERT(notit
->snapshots
.end_clock
!= UINT64_C(-1));
2352 bt_packet_set_default_end_clock_value(
2353 notit
->packet
, notit
->snapshots
.end_clock
);
2356 if (notit
->packet_header_field
) {
2357 ret
= bt_packet_move_header_field(
2358 notit
->packet
, notit
->packet_header_field
);
2363 notit
->packet_header_field
= NULL
;
2366 * At this point notit->dscopes.trace_packet_header has
2367 * the same value as the packet header field within
2370 BT_ASSERT(bt_packet_borrow_header_field(
2372 notit
->dscopes
.trace_packet_header
);
2375 if (notit
->packet_context_field
) {
2376 ret
= bt_packet_move_context_field(
2377 notit
->packet
, notit
->packet_context_field
);
2382 notit
->packet_context_field
= NULL
;
2385 * At this point notit->dscopes.trace_packet_header has
2386 * the same value as the packet header field within
2389 BT_ASSERT(bt_packet_borrow_context_field(
2391 notit
->dscopes
.stream_packet_context
);
2394 BT_ASSERT(notit
->notif_iter
);
2395 notif
= bt_notification_packet_beginning_create(notit
->notif_iter
,
2398 BT_LOGE("Cannot create packet beginning notification: "
2399 "notit-addr=%p, packet-addr=%p",
2400 notit
, notit
->packet
);
2404 *notification
= notif
;
2411 void notify_end_of_packet(struct bt_notif_iter
*notit
,
2412 bt_notification
**notification
)
2414 bt_notification
*notif
;
2416 if (!notit
->packet
) {
2420 /* Update default clock from packet's end time */
2421 if (notit
->snapshots
.end_clock
!= UINT64_C(-1)) {
2422 notit
->default_clock_val
= notit
->snapshots
.end_clock
;
2425 BT_ASSERT(notit
->notif_iter
);
2426 notif
= bt_notification_packet_end_create(notit
->notif_iter
,
2429 BT_LOGE("Cannot create packet end notification: "
2430 "notit-addr=%p, packet-addr=%p",
2431 notit
, notit
->packet
);
2436 BT_PACKET_PUT_REF_AND_RESET(notit
->packet
);
2437 *notification
= notif
;
2441 struct bt_notif_iter
*bt_notif_iter_create(struct ctf_trace_class
*tc
,
2442 size_t max_request_sz
,
2443 struct bt_notif_iter_medium_ops medops
, void *data
)
2445 struct bt_notif_iter
*notit
= NULL
;
2446 struct bt_bfcr_cbs cbs
= {
2448 .signed_int
= bfcr_signed_int_cb
,
2449 .unsigned_int
= bfcr_unsigned_int_cb
,
2450 .floating_point
= bfcr_floating_point_cb
,
2451 .string_begin
= bfcr_string_begin_cb
,
2452 .string
= bfcr_string_cb
,
2453 .string_end
= bfcr_string_end_cb
,
2454 .compound_begin
= bfcr_compound_begin_cb
,
2455 .compound_end
= bfcr_compound_end_cb
,
2458 .get_sequence_length
= bfcr_get_sequence_length_cb
,
2459 .borrow_variant_selected_field_class
= bfcr_borrow_variant_selected_field_class_cb
,
2464 BT_ASSERT(medops
.request_bytes
);
2465 BT_ASSERT(medops
.borrow_stream
);
2466 BT_LOGD("Creating CTF plugin notification iterator: "
2467 "trace-addr=%p, max-request-size=%zu, "
2468 "data=%p", tc
, max_request_sz
, data
);
2469 notit
= g_new0(struct bt_notif_iter
, 1);
2471 BT_LOGE_STR("Failed to allocate one CTF plugin notification iterator.");
2474 notit
->meta
.tc
= tc
;
2475 notit
->medium
.medops
= medops
;
2476 notit
->medium
.max_request_sz
= max_request_sz
;
2477 notit
->medium
.data
= data
;
2478 notit
->stack
= stack_new(notit
);
2479 notit
->stored_values
= g_array_new(FALSE
, TRUE
, sizeof(uint64_t));
2480 g_array_set_size(notit
->stored_values
, tc
->stored_value_count
);
2482 if (!notit
->stack
) {
2483 BT_LOGE_STR("Failed to create field stack.");
2487 notit
->bfcr
= bt_bfcr_create(cbs
, notit
);
2489 BT_LOGE_STR("Failed to create binary class reader (BFCR).");
2493 bt_notif_iter_reset(notit
);
2494 BT_LOGD("Created CTF plugin notification iterator: "
2495 "trace-addr=%p, max-request-size=%zu, "
2496 "data=%p, notit-addr=%p",
2497 tc
, max_request_sz
, data
, notit
);
2498 notit
->cur_packet_offset
= 0;
2504 bt_notif_iter_destroy(notit
);
2509 void bt_notif_iter_destroy(struct bt_notif_iter
*notit
)
2511 BT_PACKET_PUT_REF_AND_RESET(notit
->packet
);
2512 BT_STREAM_PUT_REF_AND_RESET(notit
->stream
);
2513 release_all_dscopes(notit
);
2515 BT_LOGD("Destroying CTF plugin notification iterator: addr=%p", notit
);
2518 BT_LOGD_STR("Destroying field stack.");
2519 stack_destroy(notit
->stack
);
2523 BT_LOGD("Destroying BFCR: bfcr-addr=%p", notit
->bfcr
);
2524 bt_bfcr_destroy(notit
->bfcr
);
2527 if (notit
->stored_values
) {
2528 g_array_free(notit
->stored_values
, TRUE
);
2534 enum bt_notif_iter_status
bt_notif_iter_get_next_notification(
2535 struct bt_notif_iter
*notit
,
2536 bt_self_notification_iterator
*notif_iter
,
2537 bt_notification
**notification
)
2539 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
2542 BT_ASSERT(notification
);
2544 if (notit
->state
== STATE_DONE
) {
2545 status
= BT_NOTIF_ITER_STATUS_EOF
;
2549 notit
->notif_iter
= notif_iter
;
2551 BT_LOGV("Getting next notification: notit-addr=%p", notit
);
2554 status
= handle_state(notit
);
2555 if (status
== BT_NOTIF_ITER_STATUS_AGAIN
) {
2556 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_AGAIN.");
2560 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
2561 if (status
== BT_NOTIF_ITER_STATUS_EOF
) {
2562 enum state next_state
= notit
->state
;
2564 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_EOF.");
2566 if (notit
->packet
) {
2567 notify_end_of_packet(notit
,
2570 notify_end_of_stream(notit
,
2572 next_state
= STATE_DONE
;
2575 if (!*notification
) {
2576 status
= BT_NOTIF_ITER_STATUS_ERROR
;
2580 status
= BT_NOTIF_ITER_STATUS_OK
;
2581 notit
->state
= next_state
;
2583 BT_LOGW("Cannot handle state: "
2584 "notit-addr=%p, state=%s",
2585 notit
, state_string(notit
->state
));
2591 switch (notit
->state
) {
2592 case STATE_EMIT_NOTIF_NEW_STREAM
:
2593 /* notify_new_stream() logs errors */
2594 notify_new_stream(notit
, notification
);
2596 if (!*notification
) {
2597 status
= BT_NOTIF_ITER_STATUS_ERROR
;
2600 notit
->stream_begin_emitted
= true;
2602 case STATE_EMIT_NOTIF_NEW_PACKET
:
2603 /* notify_new_packet() logs errors */
2604 notify_new_packet(notit
, notification
);
2606 if (!*notification
) {
2607 status
= BT_NOTIF_ITER_STATUS_ERROR
;
2611 case STATE_EMIT_NOTIF_EVENT
:
2612 BT_ASSERT(notit
->event_notif
);
2613 set_event_default_clock_value(notit
);
2614 *notification
= notit
->event_notif
;
2615 notit
->event_notif
= NULL
;
2617 case STATE_EMIT_NOTIF_END_OF_PACKET
:
2618 /* notify_end_of_packet() logs errors */
2619 notify_end_of_packet(notit
, notification
);
2621 if (!*notification
) {
2622 status
= BT_NOTIF_ITER_STATUS_ERROR
;
2627 /* Non-emitting state: continue */
2637 enum bt_notif_iter_status
bt_notif_iter_borrow_packet_header_context_fields(
2638 struct bt_notif_iter
*notit
,
2639 bt_field
**packet_header_field
,
2640 bt_field
**packet_context_field
)
2643 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
2647 if (notit
->state
== STATE_EMIT_NOTIF_NEW_PACKET
) {
2648 /* We're already there */
2653 status
= handle_state(notit
);
2654 if (status
== BT_NOTIF_ITER_STATUS_AGAIN
) {
2655 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_AGAIN.");
2658 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
2659 if (status
== BT_NOTIF_ITER_STATUS_EOF
) {
2660 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_EOF.");
2662 BT_LOGW("Cannot handle state: "
2663 "notit-addr=%p, state=%s",
2664 notit
, state_string(notit
->state
));
2669 switch (notit
->state
) {
2670 case STATE_EMIT_NOTIF_NEW_PACKET
:
2672 * Packet header and context fields are
2673 * potentially decoded (or they don't exist).
2677 case STATE_EMIT_NOTIF_NEW_STREAM
:
2678 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
2679 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
2680 case STATE_AFTER_TRACE_PACKET_HEADER
:
2681 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
2682 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
2683 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
2684 /* Non-emitting state: continue */
2688 * We should never get past the
2689 * STATE_EMIT_NOTIF_NEW_PACKET state.
2691 BT_LOGF("Unexpected state: notit-addr=%p, state=%s",
2692 notit
, state_string(notit
->state
));
2698 ret
= set_current_packet_content_sizes(notit
);
2700 status
= BT_NOTIF_ITER_STATUS_ERROR
;
2704 if (packet_header_field
) {
2705 *packet_header_field
= notit
->dscopes
.trace_packet_header
;
2708 if (packet_context_field
) {
2709 *packet_context_field
= notit
->dscopes
.stream_packet_context
;
2717 void bt_notif_iter_set_medops_data(struct bt_notif_iter
*notit
,
2721 notit
->medium
.data
= medops_data
;
2725 enum bt_notif_iter_status
bt_notif_iter_seek(
2726 struct bt_notif_iter
*notit
, off_t offset
)
2728 enum bt_notif_iter_status ret
= BT_NOTIF_ITER_STATUS_OK
;
2729 enum bt_notif_iter_medium_status medium_status
;
2733 BT_LOGE("Cannot seek to negative offset: offset=%jd", offset
);
2734 ret
= BT_NOTIF_ITER_STATUS_INVAL
;
2738 if (!notit
->medium
.medops
.seek
) {
2739 ret
= BT_NOTIF_ITER_STATUS_UNSUPPORTED
;
2740 BT_LOGD("Aborting seek as the iterator's underlying media does not implement seek support.");
2744 medium_status
= notit
->medium
.medops
.seek(
2745 BT_NOTIF_ITER_SEEK_WHENCE_SET
, offset
, notit
->medium
.data
);
2746 if (medium_status
!= BT_NOTIF_ITER_MEDIUM_STATUS_OK
) {
2747 if (medium_status
== BT_NOTIF_ITER_MEDIUM_STATUS_EOF
) {
2748 ret
= BT_NOTIF_ITER_STATUS_EOF
;
2750 ret
= BT_NOTIF_ITER_STATUS_ERROR
;
2755 bt_notif_iter_reset(notit
);
2756 notit
->cur_packet_offset
= offset
;
2763 off_t
bt_notif_iter_get_current_packet_offset(struct bt_notif_iter
*notit
)
2766 return notit
->cur_packet_offset
;
2770 off_t
bt_notif_iter_get_current_packet_size(
2771 struct bt_notif_iter
*notit
)
2774 return notit
->cur_exp_packet_total_size
;
2778 void bt_notif_trace_class_changed(struct bt_notif_iter
*notit
)
2780 if (notit
->meta
.tc
->stored_value_count
> notit
->stored_values
->len
) {
2781 g_array_set_size(notit
->stored_values
,
2782 notit
->meta
.tc
->stored_value_count
);
2787 enum bt_notif_iter_status
bt_notif_iter_get_packet_properties(
2788 struct bt_notif_iter
*notit
,
2789 struct bt_notif_iter_packet_properties
*props
)
2794 props
->exp_packet_total_size
=
2795 (uint64_t) notit
->cur_exp_packet_total_size
;
2796 props
->exp_packet_content_size
=
2797 (uint64_t) notit
->cur_exp_packet_content_size
;
2798 BT_ASSERT(props
->stream_class_id
>= 0);
2799 props
->stream_class_id
= (uint64_t) notit
->cur_stream_class_id
;
2800 props
->data_stream_id
= notit
->cur_data_stream_id
;
2801 props
->snapshots
.discarded_events
= notit
->snapshots
.discarded_events
;
2802 props
->snapshots
.packets
= notit
->snapshots
.packets
;
2803 props
->snapshots
.beginning_clock
= notit
->snapshots
.beginning_clock
;
2804 props
->snapshots
.end_clock
= notit
->snapshots
.end_clock
;
2805 return BT_NOTIF_ITER_STATUS_OK
;