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 "../btr/btr.h"
46 /* A visit stack entry */
49 * Current base field, one of:
59 struct bt_field
*base
;
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 struct bt_private_connection_private_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 struct 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 struct bt_packet_header_field
*packet_header_field
;
132 /* Current packet header field wrapper (NULL if not created yet) */
133 struct bt_packet_context_field
*packet_context_field
;
135 /* Current event header field (NULL if not created yet) */
136 struct bt_event_header_field
*event_header_field
;
138 /* Current packet (NULL if not created yet) */
139 struct bt_packet
*packet
;
141 /* Current stream (NULL if not set yet) */
142 struct bt_stream
*stream
;
144 /* Current event (NULL if not created yet) */
145 struct bt_event
*event
;
147 /* Current event notification (NULL if not created yet) */
148 struct bt_notification
*event_notif
;
150 /* Database of current dynamic scopes */
152 struct bt_field
*trace_packet_header
;
153 struct bt_field
*stream_packet_context
;
154 struct bt_field
*event_header
;
155 struct bt_field
*event_common_context
;
156 struct bt_field
*event_spec_context
;
157 struct 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 */
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
, struct 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_type
*dscope_ft
,
522 enum state done_state
, enum state continue_state
,
523 struct bt_field
*dscope_field
)
525 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
526 enum bt_btr_status btr_status
;
527 size_t consumed_bits
;
529 notit
->cur_dscope_field
= dscope_field
;
530 BT_LOGV("Starting BTR: notit-addr=%p, btr-addr=%p, ft-addr=%p",
531 notit
, notit
->btr
, dscope_ft
);
532 consumed_bits
= bt_btr_start(notit
->btr
, dscope_ft
,
533 notit
->buf
.addr
, notit
->buf
.at
, packet_at(notit
),
534 notit
->buf
.sz
, &btr_status
);
535 BT_LOGV("BTR consumed bits: size=%zu", consumed_bits
);
537 switch (btr_status
) {
538 case BT_BTR_STATUS_OK
:
539 /* type was read completely */
540 BT_LOGV_STR("Field was completely decoded.");
541 notit
->state
= done_state
;
543 case BT_BTR_STATUS_EOF
:
544 BT_LOGV_STR("BTR needs more data to decode field completely.");
545 notit
->state
= continue_state
;
548 BT_LOGW("BTR failed to start: notit-addr=%p, btr-addr=%p, "
549 "status=%s", notit
, notit
->btr
,
550 bt_btr_status_string(btr_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_btr_status btr_status
;
568 size_t consumed_bits
;
570 BT_LOGV("Continuing BTR: notit-addr=%p, btr-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_btr_continue(notit
->btr
, notit
->buf
.addr
,
589 notit
->buf
.sz
, &btr_status
);
590 BT_LOGV("BTR consumed bits: size=%zu", consumed_bits
);
592 switch (btr_status
) {
593 case BT_BTR_STATUS_OK
:
594 /* Type was read completely. */
595 BT_LOGV_STR("Field was completely decoded.");
596 notit
->state
= done_state
;
598 case BT_BTR_STATUS_EOF
:
599 /* Stay in this continue state. */
600 BT_LOGV_STR("BTR needs more data to decode field completely.");
603 BT_LOGW("BTR failed to continue: notit-addr=%p, btr-addr=%p, "
604 "status=%s", notit
, notit
->btr
,
605 bt_btr_status_string(btr_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_type
*packet_header_ft
= 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 type is common to the whole trace. */
665 packet_header_ft
= notit
->meta
.tc
->packet_header_ft
;
666 if (!packet_header_ft
) {
667 notit
->state
= STATE_AFTER_TRACE_PACKET_HEADER
;
671 BT_ASSERT(!notit
->packet_header_field
);
673 if (packet_header_ft
->in_ir
) {
675 * Create free packet header field from trace. This
676 * 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
= bt_packet_header_field_create(
685 notit
->meta
.tc
->ir_tc
);
686 if (!notit
->packet_header_field
) {
687 BT_LOGE_STR("Cannot create packet header field wrapper from trace.");
688 ret
= BT_NOTIF_ITER_STATUS_ERROR
;
692 notit
->dscopes
.trace_packet_header
=
693 bt_packet_header_field_borrow_field(notit
->packet_header_field
);
694 BT_ASSERT(notit
->dscopes
.trace_packet_header
);
697 notit
->cur_stream_class_id
= -1;
698 notit
->cur_event_class_id
= -1;
699 notit
->cur_data_stream_id
= -1;
700 BT_LOGV("Decoding packet header field:"
701 "notit-addr=%p, trace-addr=%p, trace-name=\"%s\", ft-addr=%p",
702 notit
, notit
->meta
.tc
,
703 notit
->meta
.tc
->name
->str
, packet_header_ft
);
704 ret
= read_dscope_begin_state(notit
, packet_header_ft
,
705 STATE_AFTER_TRACE_PACKET_HEADER
,
706 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
707 notit
->dscopes
.trace_packet_header
);
709 BT_LOGW("Cannot decode packet header field: "
710 "notit-addr=%p, trace-addr=%p, "
711 "trace-name=\"%s\", ft-addr=%p",
712 notit
, notit
->meta
.tc
,
713 notit
->meta
.tc
->name
->str
,
722 enum bt_notif_iter_status
read_packet_header_continue_state(
723 struct bt_notif_iter
*notit
)
725 return read_dscope_continue_state(notit
,
726 STATE_AFTER_TRACE_PACKET_HEADER
);
730 enum bt_notif_iter_status
set_current_stream_class(struct bt_notif_iter
*notit
)
732 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
733 struct ctf_stream_class
*new_stream_class
= NULL
;
735 if (notit
->cur_stream_class_id
== -1) {
737 * No current stream class ID field, therefore only one
740 if (notit
->meta
.tc
->stream_classes
->len
!= 1) {
741 BT_LOGW("Need exactly one stream class since there's "
742 "no stream class ID field: "
743 "notit-addr=%p, trace-name=\"%s\"",
744 notit
, notit
->meta
.tc
->name
->str
);
745 status
= BT_NOTIF_ITER_STATUS_ERROR
;
749 new_stream_class
= notit
->meta
.tc
->stream_classes
->pdata
[0];
750 notit
->cur_stream_class_id
= new_stream_class
->id
;
754 new_stream_class
= ctf_trace_class_borrow_stream_class_by_id(
755 notit
->meta
.tc
, notit
->cur_stream_class_id
);
756 if (!new_stream_class
) {
757 BT_LOGW("No stream class with ID of stream class ID to use in trace: "
758 "notit-addr=%p, stream-class-id=%" PRIu64
", "
759 "trace-addr=%p, trace-name=\"%s\"",
760 notit
, notit
->cur_stream_class_id
, notit
->meta
.tc
,
761 notit
->meta
.tc
->name
->str
);
762 status
= BT_NOTIF_ITER_STATUS_ERROR
;
766 if (notit
->meta
.sc
) {
767 if (new_stream_class
!= notit
->meta
.sc
) {
768 BT_LOGW("Two packets refer to two different stream classes within the same packet sequence: "
769 "notit-addr=%p, prev-stream-class-addr=%p, "
770 "prev-stream-class-id=%" PRId64
", "
771 "next-stream-class-addr=%p, "
772 "next-stream-class-id=%" PRId64
", "
773 "trace-addr=%p, trace-name=\"%s\"",
774 notit
, notit
->meta
.sc
,
777 new_stream_class
->id
,
779 notit
->meta
.tc
->name
->str
);
780 status
= BT_NOTIF_ITER_STATUS_ERROR
;
784 notit
->meta
.sc
= new_stream_class
;
787 BT_LOGV("Set current stream class: "
788 "notit-addr=%p, stream-class-addr=%p, "
789 "stream-class-id=%" PRId64
,
790 notit
, notit
->meta
.sc
, notit
->meta
.sc
->id
);
797 enum bt_notif_iter_status
set_current_stream(struct bt_notif_iter
*notit
)
799 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
800 struct bt_stream
*stream
= NULL
;
802 BT_LOGV("Calling user function (get stream): notit-addr=%p, "
803 "stream-class-addr=%p, stream-class-id=%" PRId64
,
804 notit
, notit
->meta
.sc
,
806 stream
= bt_get(notit
->medium
.medops
.borrow_stream(
807 notit
->meta
.sc
->ir_sc
, notit
->cur_data_stream_id
,
808 notit
->medium
.data
));
809 BT_LOGV("User function returned: stream-addr=%p", stream
);
811 BT_LOGW_STR("User function failed to return a stream object "
812 "for the given stream class.");
813 status
= BT_NOTIF_ITER_STATUS_ERROR
;
817 if (notit
->stream
&& stream
!= notit
->stream
) {
818 BT_LOGW("User function returned a different stream than the "
819 "previous one for the same sequence of packets.");
820 status
= BT_NOTIF_ITER_STATUS_ERROR
;
824 BT_MOVE(notit
->stream
, stream
);
832 enum bt_notif_iter_status
set_current_packet(struct bt_notif_iter
*notit
)
834 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
835 struct bt_packet
*packet
= NULL
;
837 BT_LOGV("Creating packet for packet notification: "
838 "notit-addr=%p", notit
);
839 BT_LOGV("Creating packet from stream: "
840 "notit-addr=%p, stream-addr=%p, "
841 "stream-class-addr=%p, "
842 "stream-class-id=%" PRId64
,
843 notit
, notit
->stream
, notit
->meta
.sc
,
847 BT_ASSERT(notit
->stream
);
848 packet
= bt_packet_create(notit
->stream
);
850 BT_LOGE("Cannot create packet from stream: "
851 "notit-addr=%p, stream-addr=%p, "
852 "stream-class-addr=%p, "
853 "stream-class-id=%" PRId64
,
854 notit
, notit
->stream
, notit
->meta
.sc
,
863 status
= BT_NOTIF_ITER_STATUS_ERROR
;
866 BT_MOVE(notit
->packet
, packet
);
871 enum bt_notif_iter_status
after_packet_header_state(
872 struct bt_notif_iter
*notit
)
874 enum bt_notif_iter_status status
;
876 status
= set_current_stream_class(notit
);
877 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
881 notit
->state
= STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
;
888 enum bt_notif_iter_status
read_packet_context_begin_state(
889 struct bt_notif_iter
*notit
)
891 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
892 struct ctf_field_type
*packet_context_ft
;
894 BT_ASSERT(notit
->meta
.sc
);
895 packet_context_ft
= notit
->meta
.sc
->packet_context_ft
;
896 if (!packet_context_ft
) {
897 BT_LOGV("No packet packet context field type in stream class: continuing: "
898 "notit-addr=%p, stream-class-addr=%p, "
899 "stream-class-id=%" PRId64
,
900 notit
, notit
->meta
.sc
,
902 notit
->state
= STATE_AFTER_STREAM_PACKET_CONTEXT
;
906 BT_ASSERT(!notit
->packet_context_field
);
908 if (packet_context_ft
->in_ir
) {
910 * Create free packet context field from stream class.
911 * This field is going to be moved to the packet once we
912 * create it. We cannot create the packet now because a
913 * packet is created from a stream, and this API must be
914 * able to return the packet header and context fields
915 * without creating a stream
916 * (bt_notif_iter_borrow_packet_header_context_fields()).
918 notit
->packet_context_field
=
919 bt_packet_context_field_create(notit
->meta
.sc
->ir_sc
);
920 if (!notit
->packet_context_field
) {
921 BT_LOGE_STR("Cannot create packet context field wrapper from stream class.");
922 status
= BT_NOTIF_ITER_STATUS_ERROR
;
926 notit
->dscopes
.stream_packet_context
=
927 bt_packet_context_field_borrow_field(notit
->packet_context_field
);
928 BT_ASSERT(notit
->dscopes
.stream_packet_context
);
931 BT_LOGV("Decoding packet context field: "
932 "notit-addr=%p, stream-class-addr=%p, "
933 "stream-class-id=%" PRId64
", ft-addr=%p",
934 notit
, notit
->meta
.sc
,
935 notit
->meta
.sc
->id
, packet_context_ft
);
936 status
= read_dscope_begin_state(notit
, packet_context_ft
,
937 STATE_AFTER_STREAM_PACKET_CONTEXT
,
938 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
939 notit
->dscopes
.stream_packet_context
);
941 BT_LOGW("Cannot decode packet context field: "
942 "notit-addr=%p, stream-class-addr=%p, "
943 "stream-class-id=%" PRId64
", ft-addr=%p",
944 notit
, notit
->meta
.sc
,
954 enum bt_notif_iter_status
read_packet_context_continue_state(
955 struct bt_notif_iter
*notit
)
957 return read_dscope_continue_state(notit
,
958 STATE_AFTER_STREAM_PACKET_CONTEXT
);
962 enum bt_notif_iter_status
set_current_packet_content_sizes(
963 struct bt_notif_iter
*notit
)
965 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
967 if (notit
->cur_exp_packet_total_size
== -1) {
968 if (notit
->cur_exp_packet_content_size
!= -1) {
969 BT_LOGW("Content size is set, but packet size is not: "
970 "notit-addr=%p, packet-context-field-addr=%p, "
971 "packet-size=%" PRId64
", content-size=%" PRId64
,
972 notit
, notit
->dscopes
.stream_packet_context
,
973 notit
->cur_exp_packet_total_size
,
974 notit
->cur_exp_packet_content_size
);
975 status
= BT_NOTIF_ITER_STATUS_ERROR
;
979 if (notit
->cur_exp_packet_content_size
== -1) {
980 notit
->cur_exp_packet_content_size
=
981 notit
->cur_exp_packet_total_size
;
985 if (notit
->cur_exp_packet_content_size
>
986 notit
->cur_exp_packet_total_size
) {
987 BT_LOGW("Invalid packet or content size: "
988 "content size is greater than packet size: "
989 "notit-addr=%p, packet-context-field-addr=%p, "
990 "packet-size=%" PRId64
", content-size=%" PRId64
,
991 notit
, notit
->dscopes
.stream_packet_context
,
992 notit
->cur_exp_packet_total_size
,
993 notit
->cur_exp_packet_content_size
);
994 status
= BT_NOTIF_ITER_STATUS_ERROR
;
998 BT_LOGV("Set current packet and content sizes: "
999 "notit-addr=%p, packet-size=%" PRIu64
", content-size=%" PRIu64
,
1000 notit
, notit
->cur_exp_packet_total_size
,
1001 notit
->cur_exp_packet_content_size
);
1007 enum bt_notif_iter_status
after_packet_context_state(
1008 struct bt_notif_iter
*notit
)
1010 enum bt_notif_iter_status status
;
1012 status
= set_current_packet_content_sizes(notit
);
1013 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
1017 if (notit
->stream_begin_emitted
) {
1018 notit
->state
= STATE_EMIT_NOTIF_NEW_PACKET
;
1020 notit
->state
= STATE_EMIT_NOTIF_NEW_STREAM
;
1028 enum bt_notif_iter_status
read_event_header_begin_state(
1029 struct bt_notif_iter
*notit
)
1031 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1032 struct ctf_field_type
*event_header_ft
= NULL
;
1034 /* Reset the position of the last event header */
1035 notit
->buf
.last_eh_at
= notit
->buf
.at
;
1036 notit
->cur_event_class_id
= -1;
1038 /* Check if we have some content left */
1039 if (notit
->cur_exp_packet_content_size
>= 0) {
1040 if (unlikely(packet_at(notit
) ==
1041 notit
->cur_exp_packet_content_size
)) {
1042 /* No more events! */
1043 BT_LOGV("Reached end of packet: notit-addr=%p, "
1044 "cur=%zu", notit
, packet_at(notit
));
1045 notit
->state
= STATE_EMIT_NOTIF_END_OF_PACKET
;
1047 } else if (unlikely(packet_at(notit
) >
1048 notit
->cur_exp_packet_content_size
)) {
1049 /* That's not supposed to happen */
1050 BT_LOGV("Before decoding event header field: cursor is passed the packet's content: "
1051 "notit-addr=%p, content-size=%" PRId64
", "
1053 notit
->cur_exp_packet_content_size
,
1055 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1060 * "Infinite" content: we're done when the medium has
1061 * nothing else for us.
1063 status
= buf_ensure_available_bits(notit
);
1064 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
1066 * If this function returns
1067 * `BT_NOTIF_ITER_STATUS_EOF`:
1069 * 1. bt_notif_iter_get_next_notification()
1070 * emits a "packet end" notification. This
1071 * resets the current packet. The state
1072 * remains unchanged otherwise.
1073 * 2. This function is called again. It returns
1074 * `BT_NOTIF_ITER_STATUS_EOF` again.
1075 * 3. bt_notif_iter_get_next_notification()
1076 * emits a "stream end" notification because
1077 * there's no current packet. It sets the
1078 * current state to `STATE_DONE`.
1084 release_event_dscopes(notit
);
1085 BT_ASSERT(notit
->meta
.sc
);
1086 event_header_ft
= notit
->meta
.sc
->event_header_ft
;
1087 if (!event_header_ft
) {
1088 notit
->state
= STATE_AFTER_EVENT_HEADER
;
1092 if (event_header_ft
->in_ir
) {
1093 BT_ASSERT(!notit
->event_header_field
);
1094 notit
->event_header_field
= bt_event_header_field_create(
1095 notit
->meta
.sc
->ir_sc
);
1096 if (!notit
->event_header_field
) {
1097 BT_LOGE_STR("Cannot create event header field wrapper from trace.");
1098 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1102 notit
->dscopes
.event_header
=
1103 bt_event_header_field_borrow_field(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_ft
,
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
", ft-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, trace-name=\"%s\"",
1155 notit
, notit
->meta
.tc
->name
->str
);
1156 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1160 new_event_class
= notit
->meta
.sc
->event_classes
->pdata
[0];
1161 notit
->cur_event_class_id
= new_event_class
->id
;
1165 new_event_class
= ctf_stream_class_borrow_event_class_by_id(
1166 notit
->meta
.sc
, notit
->cur_event_class_id
);
1167 if (!new_event_class
) {
1168 BT_LOGW("No event class with ID of event class ID to use in stream class: "
1169 "notit-addr=%p, stream-class-id=%" PRIu64
", "
1170 "event-class-id=%" PRIu64
", "
1171 "trace-addr=%p, trace-name=\"%s\"",
1172 notit
, notit
->meta
.sc
->id
, notit
->cur_event_class_id
,
1173 notit
->meta
.tc
, notit
->meta
.tc
->name
->str
);
1174 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1178 notit
->meta
.ec
= new_event_class
;
1179 BT_LOGV("Set current event class: "
1180 "notit-addr=%p, event-class-addr=%p, "
1181 "event-class-id=%" PRId64
", "
1182 "event-class-name=\"%s\"",
1183 notit
, notit
->meta
.ec
, notit
->meta
.ec
->id
,
1184 notit
->meta
.ec
->name
->str
);
1191 enum bt_notif_iter_status
set_current_event_notification(
1192 struct bt_notif_iter
*notit
)
1194 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1195 struct bt_notification
*notif
= NULL
;
1197 BT_ASSERT(notit
->meta
.ec
);
1198 BT_ASSERT(notit
->packet
);
1199 BT_LOGV("Creating event notification from event class and packet: "
1200 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", packet-addr=%p",
1201 notit
, notit
->meta
.ec
,
1202 notit
->meta
.ec
->name
->str
,
1204 BT_ASSERT(notit
->notif_iter
);
1205 notif
= bt_notification_event_create(notit
->notif_iter
,
1206 notit
->meta
.ec
->ir_ec
, notit
->packet
);
1208 BT_LOGE("Cannot create event notification: "
1209 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", "
1211 notit
, notit
->meta
.ec
,
1212 notit
->meta
.ec
->name
->str
,
1221 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1224 BT_MOVE(notit
->event_notif
, notif
);
1229 enum bt_notif_iter_status
after_event_header_state(
1230 struct bt_notif_iter
*notit
)
1232 enum bt_notif_iter_status status
;
1234 status
= set_current_event_class(notit
);
1235 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
1239 status
= set_current_event_notification(notit
);
1240 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
1244 notit
->event
= bt_notification_event_borrow_event(notit
->event_notif
);
1245 BT_ASSERT(notit
->event
);
1247 if (notit
->event_header_field
) {
1250 BT_ASSERT(notit
->event
);
1251 ret
= bt_event_move_header(notit
->event
,
1252 notit
->event_header_field
);
1254 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1258 notit
->event_header_field
= NULL
;
1261 * At this point notit->dscopes.event_header has
1262 * the same value as the event header field within
1265 BT_ASSERT(bt_event_borrow_header_field(notit
->event
) ==
1266 notit
->dscopes
.event_header
);
1269 notit
->state
= STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
;
1276 enum bt_notif_iter_status
read_event_common_context_begin_state(
1277 struct bt_notif_iter
*notit
)
1279 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1280 struct ctf_field_type
*event_common_context_ft
;
1282 event_common_context_ft
= notit
->meta
.sc
->event_common_context_ft
;
1283 if (!event_common_context_ft
) {
1284 notit
->state
= STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
;
1288 if (event_common_context_ft
->in_ir
) {
1289 BT_ASSERT(!notit
->dscopes
.event_common_context
);
1290 notit
->dscopes
.event_common_context
=
1291 bt_event_borrow_common_context_field(notit
->event
);
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_ft
);
1302 status
= read_dscope_begin_state(notit
, event_common_context_ft
,
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
", ft-addr=%p",
1310 notit
, notit
->meta
.sc
,
1312 event_common_context_ft
);
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_type
*event_spec_context_ft
;
1334 event_spec_context_ft
= notit
->meta
.ec
->spec_context_ft
;
1335 if (!event_spec_context_ft
) {
1336 notit
->state
= STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
;
1340 if (event_spec_context_ft
->in_ir
) {
1341 BT_ASSERT(!notit
->dscopes
.event_spec_context
);
1342 notit
->dscopes
.event_spec_context
= bt_event_borrow_specific_context_field(
1344 BT_ASSERT(notit
->dscopes
.event_spec_context
);
1347 BT_LOGV("Decoding event specific context field: "
1348 "notit-addr=%p, event-class-addr=%p, "
1349 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1351 notit
, notit
->meta
.ec
,
1352 notit
->meta
.ec
->name
->str
,
1354 event_spec_context_ft
);
1355 status
= read_dscope_begin_state(notit
, event_spec_context_ft
,
1356 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
1357 STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
,
1358 notit
->dscopes
.event_spec_context
);
1360 BT_LOGW("Cannot decode event specific context field: "
1361 "notit-addr=%p, event-class-addr=%p, "
1362 "event-class-name=\"%s\", "
1363 "event-class-id=%" PRId64
", ft-addr=%p",
1364 notit
, notit
->meta
.ec
,
1365 notit
->meta
.ec
->name
->str
,
1367 event_spec_context_ft
);
1375 enum bt_notif_iter_status
read_event_spec_context_continue_state(
1376 struct bt_notif_iter
*notit
)
1378 return read_dscope_continue_state(notit
,
1379 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
);
1383 enum bt_notif_iter_status
read_event_payload_begin_state(
1384 struct bt_notif_iter
*notit
)
1386 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1387 struct ctf_field_type
*event_payload_ft
;
1389 event_payload_ft
= notit
->meta
.ec
->payload_ft
;
1390 if (!event_payload_ft
) {
1391 notit
->state
= STATE_EMIT_NOTIF_EVENT
;
1395 if (event_payload_ft
->in_ir
) {
1396 BT_ASSERT(!notit
->dscopes
.event_payload
);
1397 notit
->dscopes
.event_payload
= bt_event_borrow_payload_field(
1399 BT_ASSERT(notit
->dscopes
.event_payload
);
1402 BT_LOGV("Decoding event payload field: "
1403 "notit-addr=%p, event-class-addr=%p, "
1404 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1406 notit
, notit
->meta
.ec
,
1407 notit
->meta
.ec
->name
->str
,
1410 status
= read_dscope_begin_state(notit
, event_payload_ft
,
1411 STATE_EMIT_NOTIF_EVENT
,
1412 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
1413 notit
->dscopes
.event_payload
);
1415 BT_LOGW("Cannot decode event payload field: "
1416 "notit-addr=%p, event-class-addr=%p, "
1417 "event-class-name=\"%s\", "
1418 "event-class-id=%" PRId64
", ft-addr=%p",
1419 notit
, notit
->meta
.ec
,
1420 notit
->meta
.ec
->name
->str
,
1430 enum bt_notif_iter_status
read_event_payload_continue_state(
1431 struct bt_notif_iter
*notit
)
1433 return read_dscope_continue_state(notit
, STATE_EMIT_NOTIF_EVENT
);
1437 enum bt_notif_iter_status
skip_packet_padding_state(
1438 struct bt_notif_iter
*notit
)
1440 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1441 size_t bits_to_skip
;
1443 BT_ASSERT(notit
->cur_exp_packet_total_size
> 0);
1444 bits_to_skip
= notit
->cur_exp_packet_total_size
- packet_at(notit
);
1445 if (bits_to_skip
== 0) {
1446 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1449 size_t bits_to_consume
;
1451 BT_LOGV("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu",
1452 bits_to_skip
, notit
, bits_to_skip
);
1453 status
= buf_ensure_available_bits(notit
);
1454 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
1458 bits_to_consume
= MIN(buf_available_bits(notit
), bits_to_skip
);
1459 BT_LOGV("Skipping %zu bits of padding: notit-addr=%p, size=%zu",
1460 bits_to_consume
, notit
, bits_to_consume
);
1461 buf_consume_bits(notit
, bits_to_consume
);
1462 bits_to_skip
= notit
->cur_exp_packet_total_size
-
1464 if (bits_to_skip
== 0) {
1465 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1475 enum bt_notif_iter_status
handle_state(struct bt_notif_iter
*notit
)
1477 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1478 const enum state state
= notit
->state
;
1480 BT_LOGV("Handling state: notit-addr=%p, state=%s",
1481 notit
, state_string(state
));
1483 // TODO: optimalize!
1486 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1488 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
1489 status
= read_packet_header_begin_state(notit
);
1491 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
1492 status
= read_packet_header_continue_state(notit
);
1494 case STATE_AFTER_TRACE_PACKET_HEADER
:
1495 status
= after_packet_header_state(notit
);
1497 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
1498 status
= read_packet_context_begin_state(notit
);
1500 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
1501 status
= read_packet_context_continue_state(notit
);
1503 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
1504 status
= after_packet_context_state(notit
);
1506 case STATE_EMIT_NOTIF_NEW_STREAM
:
1507 notit
->state
= STATE_EMIT_NOTIF_NEW_PACKET
;
1509 case STATE_EMIT_NOTIF_NEW_PACKET
:
1510 notit
->state
= STATE_DSCOPE_EVENT_HEADER_BEGIN
;
1512 case STATE_DSCOPE_EVENT_HEADER_BEGIN
:
1513 status
= read_event_header_begin_state(notit
);
1515 case STATE_DSCOPE_EVENT_HEADER_CONTINUE
:
1516 status
= read_event_header_continue_state(notit
);
1518 case STATE_AFTER_EVENT_HEADER
:
1519 status
= after_event_header_state(notit
);
1521 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN
:
1522 status
= read_event_common_context_begin_state(notit
);
1524 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE
:
1525 status
= read_event_common_context_continue_state(notit
);
1527 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN
:
1528 status
= read_event_spec_context_begin_state(notit
);
1530 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE
:
1531 status
= read_event_spec_context_continue_state(notit
);
1533 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
1534 status
= read_event_payload_begin_state(notit
);
1536 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
1537 status
= read_event_payload_continue_state(notit
);
1539 case STATE_EMIT_NOTIF_EVENT
:
1540 notit
->state
= STATE_DSCOPE_EVENT_HEADER_BEGIN
;
1542 case STATE_SKIP_PACKET_PADDING
:
1543 status
= skip_packet_padding_state(notit
);
1545 case STATE_EMIT_NOTIF_END_OF_PACKET
:
1546 notit
->state
= STATE_SKIP_PACKET_PADDING
;
1549 BT_LOGD("Unknown CTF plugin notification iterator state: "
1550 "notit-addr=%p, state=%d", notit
, notit
->state
);
1554 BT_LOGV("Handled state: notit-addr=%p, status=%s, "
1555 "prev-state=%s, cur-state=%s",
1556 notit
, bt_notif_iter_status_string(status
),
1557 state_string(state
), state_string(notit
->state
));
1562 * Resets the internal state of a CTF notification iterator.
1565 void bt_notif_iter_reset(struct bt_notif_iter
*notit
)
1568 BT_LOGD("Resetting notification iterator: addr=%p", notit
);
1569 stack_clear(notit
->stack
);
1570 notit
->meta
.sc
= NULL
;
1571 notit
->meta
.ec
= NULL
;
1572 BT_PUT(notit
->packet
);
1573 BT_PUT(notit
->stream
);
1574 BT_PUT(notit
->event_notif
);
1575 release_all_dscopes(notit
);
1576 notit
->cur_dscope_field
= NULL
;
1578 if (notit
->packet_header_field
) {
1579 bt_packet_header_field_release(notit
->packet_header_field
);
1580 notit
->packet_header_field
= NULL
;
1583 if (notit
->packet_context_field
) {
1584 bt_packet_context_field_release(notit
->packet_context_field
);
1585 notit
->packet_context_field
= NULL
;
1588 if (notit
->event_header_field
) {
1589 bt_event_header_field_release(notit
->event_header_field
);
1590 notit
->event_header_field
= NULL
;
1593 notit
->buf
.addr
= NULL
;
1596 notit
->buf
.last_eh_at
= SIZE_MAX
;
1597 notit
->buf
.packet_offset
= 0;
1598 notit
->state
= STATE_INIT
;
1599 notit
->cur_exp_packet_content_size
= -1;
1600 notit
->cur_exp_packet_total_size
= -1;
1601 notit
->cur_packet_offset
= -1;
1602 notit
->cur_stream_class_id
= -1;
1603 notit
->cur_event_class_id
= -1;
1604 notit
->cur_data_stream_id
= -1;
1605 notit
->stream_begin_emitted
= false;
1609 int bt_notif_iter_switch_packet(struct bt_notif_iter
*notit
)
1614 * We don't put the stream class here because we need to make
1615 * sure that all the packets processed by the same notification
1616 * iterator refer to the same stream class (the first one).
1620 if (notit
->cur_exp_packet_total_size
!= -1) {
1621 notit
->cur_packet_offset
+= notit
->cur_exp_packet_total_size
;
1624 BT_LOGV("Switching packet: notit-addr=%p, cur=%zu, "
1625 "packet-offset=%" PRId64
, notit
, notit
->buf
.at
,
1626 notit
->cur_packet_offset
);
1627 stack_clear(notit
->stack
);
1628 notit
->meta
.ec
= NULL
;
1629 BT_PUT(notit
->packet
);
1630 BT_PUT(notit
->event_notif
);
1631 release_all_dscopes(notit
);
1632 notit
->cur_dscope_field
= NULL
;
1635 * Adjust current buffer so that addr points to the beginning of the new
1638 if (notit
->buf
.addr
) {
1639 size_t consumed_bytes
= (size_t) (notit
->buf
.at
/ CHAR_BIT
);
1641 /* Packets are assumed to start on a byte frontier. */
1642 if (notit
->buf
.at
% CHAR_BIT
) {
1643 BT_LOGW("Cannot switch packet: current position is not a multiple of 8: "
1644 "notit-addr=%p, cur=%zu", notit
, notit
->buf
.at
);
1649 notit
->buf
.addr
+= consumed_bytes
;
1650 notit
->buf
.sz
-= consumed_bytes
;
1652 notit
->buf
.packet_offset
= 0;
1653 BT_LOGV("Adjusted buffer: addr=%p, size=%zu",
1654 notit
->buf
.addr
, notit
->buf
.sz
);
1657 notit
->cur_exp_packet_content_size
= -1;
1658 notit
->cur_exp_packet_total_size
= -1;
1659 notit
->cur_stream_class_id
= -1;
1660 notit
->cur_event_class_id
= -1;
1661 notit
->cur_data_stream_id
= -1;
1662 notit
->snapshots
.discarded_events
= UINT64_C(-1);
1663 notit
->snapshots
.packets
= UINT64_C(-1);
1664 notit
->snapshots
.beginning_clock
= UINT64_C(-1);
1665 notit
->snapshots
.end_clock
= UINT64_C(-1);
1672 struct bt_field
*borrow_next_field(struct bt_notif_iter
*notit
)
1674 struct bt_field
*next_field
= NULL
;
1675 struct bt_field
*base_field
;
1676 struct bt_field_type
*base_ft
;
1679 BT_ASSERT(!stack_empty(notit
->stack
));
1680 index
= stack_top(notit
->stack
)->index
;
1681 base_field
= stack_top(notit
->stack
)->base
;
1682 BT_ASSERT(base_field
);
1683 base_ft
= bt_field_borrow_type(base_field
);
1686 switch (bt_field_type_get_type_id(base_ft
)) {
1687 case BT_FIELD_TYPE_ID_STRUCTURE
:
1690 bt_field_type_structure_get_member_count(
1691 bt_field_borrow_type(base_field
)));
1692 next_field
= bt_field_structure_borrow_member_field_by_index(
1696 case BT_FIELD_TYPE_ID_STATIC_ARRAY
:
1697 case BT_FIELD_TYPE_ID_DYNAMIC_ARRAY
:
1698 BT_ASSERT(index
< bt_field_array_get_length(base_field
));
1699 next_field
= bt_field_array_borrow_element_field_by_index(
1702 case BT_FIELD_TYPE_ID_VARIANT
:
1703 BT_ASSERT(index
== 0);
1704 next_field
= bt_field_variant_borrow_selected_option_field(
1711 BT_ASSERT(next_field
);
1716 void update_default_clock(struct bt_notif_iter
*notit
, uint64_t new_val
,
1717 uint64_t new_val_size
)
1719 uint64_t new_val_mask
;
1720 uint64_t cur_value_masked
;
1722 BT_ASSERT(new_val_size
> 0);
1725 * Special case for a 64-bit new value, which is the limit
1726 * of a clock value as of this version: overwrite the
1727 * current value directly.
1729 if (new_val_size
== 64) {
1730 notit
->default_clock_val
= new_val
;
1734 new_val_mask
= (1ULL << new_val_size
) - 1;
1735 cur_value_masked
= notit
->default_clock_val
& new_val_mask
;
1737 if (new_val
< cur_value_masked
) {
1739 * It looks like a wrap happened on the number of bits
1740 * of the requested new value. Assume that the clock
1741 * value wrapped only one time.
1743 notit
->default_clock_val
+= new_val_mask
+ 1;
1746 /* Clear the low bits of the current clock value. */
1747 notit
->default_clock_val
&= ~new_val_mask
;
1749 /* Set the low bits of the current clock value. */
1750 notit
->default_clock_val
|= new_val
;
1753 BT_LOGV("Updated default clock's value from integer field's value: "
1754 "value=%" PRIu64
, notit
->default_clock_val
);
1758 enum bt_btr_status
btr_unsigned_int_cb(uint64_t value
,
1759 struct ctf_field_type
*ft
, void *data
)
1761 struct bt_notif_iter
*notit
= data
;
1762 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
1763 struct bt_field
*field
= NULL
;
1764 struct ctf_field_type_int
*int_ft
= (void *) ft
;
1766 BT_LOGV("Unsigned integer function called from BTR: "
1767 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
1768 "ft-id=%d, ft-in-ir=%d, value=%" PRIu64
,
1769 notit
, notit
->btr
, ft
, ft
->id
, ft
->in_ir
, value
);
1771 if (likely(int_ft
->meaning
== CTF_FIELD_TYPE_MEANING_NONE
)) {
1772 goto update_def_clock
;
1775 switch (int_ft
->meaning
) {
1776 case CTF_FIELD_TYPE_MEANING_EVENT_CLASS_ID
:
1777 notit
->cur_event_class_id
= value
;
1779 case CTF_FIELD_TYPE_MEANING_DATA_STREAM_ID
:
1780 notit
->cur_data_stream_id
= value
;
1782 case CTF_FIELD_TYPE_MEANING_PACKET_BEGINNING_TIME
:
1783 notit
->snapshots
.beginning_clock
= value
;
1785 case CTF_FIELD_TYPE_MEANING_PACKET_END_TIME
:
1786 notit
->snapshots
.end_clock
= value
;
1788 case CTF_FIELD_TYPE_MEANING_STREAM_CLASS_ID
:
1789 notit
->cur_stream_class_id
= value
;
1791 case CTF_FIELD_TYPE_MEANING_MAGIC
:
1792 if (value
!= 0xc1fc1fc1) {
1793 BT_LOGW("Invalid CTF magic number: notit-addr=%p, "
1794 "magic=%" PRIx64
, notit
, value
);
1795 status
= BT_BTR_STATUS_ERROR
;
1800 case CTF_FIELD_TYPE_MEANING_PACKET_COUNTER_SNAPSHOT
:
1801 notit
->snapshots
.packets
= value
;
1803 case CTF_FIELD_TYPE_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT
:
1804 notit
->snapshots
.discarded_events
= value
;
1806 case CTF_FIELD_TYPE_MEANING_EXP_PACKET_TOTAL_SIZE
:
1807 notit
->cur_exp_packet_total_size
= value
;
1809 case CTF_FIELD_TYPE_MEANING_EXP_PACKET_CONTENT_SIZE
:
1810 notit
->cur_exp_packet_content_size
= value
;
1817 if (unlikely(int_ft
->mapped_clock_class
)) {
1818 update_default_clock(notit
, value
, int_ft
->base
.size
);
1821 if (unlikely(int_ft
->storing_index
>= 0)) {
1822 g_array_index(notit
->stored_values
, uint64_t,
1823 (uint64_t) int_ft
->storing_index
) = value
;
1826 if (unlikely(!ft
->in_ir
)) {
1830 field
= borrow_next_field(notit
);
1832 BT_ASSERT(bt_field_borrow_type(field
) == ft
->ir_ft
);
1833 BT_ASSERT(bt_field_get_type_id(field
) == BT_FIELD_TYPE_ID_UNSIGNED_INTEGER
||
1834 bt_field_get_type_id(field
) == BT_FIELD_TYPE_ID_UNSIGNED_ENUMERATION
);
1835 bt_field_unsigned_integer_set_value(field
, value
);
1836 stack_top(notit
->stack
)->index
++;
1843 enum bt_btr_status
btr_unsigned_int_char_cb(uint64_t value
,
1844 struct ctf_field_type
*ft
, void *data
)
1847 struct bt_notif_iter
*notit
= data
;
1848 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
1849 struct bt_field
*string_field
= NULL
;
1850 struct ctf_field_type_int
*int_ft
= (void *) ft
;
1851 char str
[2] = {'\0', '\0'};
1853 BT_LOGV("Unsigned integer character function called from BTR: "
1854 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
1855 "ft-id=%d, ft-in-ir=%d, value=%" PRIu64
,
1856 notit
, notit
->btr
, ft
, ft
->id
, ft
->in_ir
, value
);
1857 BT_ASSERT(int_ft
->meaning
== CTF_FIELD_TYPE_MEANING_NONE
);
1858 BT_ASSERT(!int_ft
->mapped_clock_class
);
1859 BT_ASSERT(int_ft
->storing_index
< 0);
1861 if (unlikely(!ft
->in_ir
)) {
1865 if (notit
->done_filling_string
) {
1870 notit
->done_filling_string
= true;
1874 string_field
= stack_top(notit
->stack
)->base
;
1875 BT_ASSERT(bt_field_get_type_id(string_field
) == BT_FIELD_TYPE_ID_STRING
);
1877 /* Append character */
1878 str
[0] = (char) value
;
1879 ret
= bt_field_string_append_with_length(string_field
, str
, 1);
1881 BT_LOGE("Cannot append character to string field's value: "
1882 "notit-addr=%p, field-addr=%p, ret=%d",
1883 notit
, string_field
, ret
);
1884 status
= BT_BTR_STATUS_ERROR
;
1893 enum bt_btr_status
btr_signed_int_cb(int64_t value
,
1894 struct ctf_field_type
*ft
, void *data
)
1896 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
1897 struct bt_field
*field
= NULL
;
1898 struct bt_notif_iter
*notit
= data
;
1899 struct ctf_field_type_int
*int_ft
= (void *) ft
;
1901 BT_LOGV("Signed integer function called from BTR: "
1902 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
1903 "ft-id=%d, ft-in-ir=%d, value=%" PRId64
,
1904 notit
, notit
->btr
, ft
, ft
->id
, ft
->in_ir
, value
);
1905 BT_ASSERT(int_ft
->meaning
== CTF_FIELD_TYPE_MEANING_NONE
);
1907 if (unlikely(int_ft
->storing_index
>= 0)) {
1908 g_array_index(notit
->stored_values
, uint64_t,
1909 (uint64_t) int_ft
->storing_index
) = (uint64_t) value
;
1912 if (unlikely(!ft
->in_ir
)) {
1916 field
= borrow_next_field(notit
);
1918 BT_ASSERT(bt_field_borrow_type(field
) == ft
->ir_ft
);
1919 BT_ASSERT(bt_field_get_type_id(field
) == BT_FIELD_TYPE_ID_SIGNED_INTEGER
||
1920 bt_field_get_type_id(field
) == BT_FIELD_TYPE_ID_SIGNED_ENUMERATION
);
1921 bt_field_signed_integer_set_value(field
, value
);
1922 stack_top(notit
->stack
)->index
++;
1929 enum bt_btr_status
btr_floating_point_cb(double value
,
1930 struct ctf_field_type
*ft
, void *data
)
1932 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
1933 struct bt_field
*field
= NULL
;
1934 struct bt_notif_iter
*notit
= data
;
1936 BT_LOGV("Floating point number function called from BTR: "
1937 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
1938 "ft-id=%d, ft-in-ir=%d, value=%f",
1939 notit
, notit
->btr
, ft
, ft
->id
, ft
->in_ir
, value
);
1940 BT_ASSERT(ft
->in_ir
);
1941 field
= borrow_next_field(notit
);
1943 BT_ASSERT(bt_field_borrow_type(field
) == ft
->ir_ft
);
1944 BT_ASSERT(bt_field_get_type_id(field
) == BT_FIELD_TYPE_ID_REAL
);
1945 bt_field_real_set_value(field
, value
);
1946 stack_top(notit
->stack
)->index
++;
1951 enum bt_btr_status
btr_string_begin_cb(
1952 struct ctf_field_type
*ft
, void *data
)
1954 struct bt_field
*field
= NULL
;
1955 struct bt_notif_iter
*notit
= data
;
1958 BT_LOGV("String (beginning) function called from BTR: "
1959 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
1960 "ft-id=%d, ft-in-ir=%d",
1961 notit
, notit
->btr
, ft
, ft
->id
, ft
->in_ir
);
1963 BT_ASSERT(ft
->in_ir
);
1964 field
= borrow_next_field(notit
);
1966 BT_ASSERT(bt_field_borrow_type(field
) == ft
->ir_ft
);
1967 BT_ASSERT(bt_field_get_type_id(field
) == BT_FIELD_TYPE_ID_STRING
);
1968 ret
= bt_field_string_clear(field
);
1969 BT_ASSERT(ret
== 0);
1972 * Push on stack. Not a compound type per se, but we know that
1973 * only btr_string_cb() may be called between this call and a
1974 * subsequent call to btr_string_end_cb().
1976 stack_push(notit
->stack
, field
);
1977 return BT_BTR_STATUS_OK
;
1981 enum bt_btr_status
btr_string_cb(const char *value
,
1982 size_t len
, struct ctf_field_type
*ft
, void *data
)
1984 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
1985 struct bt_field
*field
= NULL
;
1986 struct bt_notif_iter
*notit
= data
;
1989 BT_LOGV("String (substring) function called from BTR: "
1990 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
1991 "ft-id=%d, ft-in-ir=%d, string-length=%zu",
1992 notit
, notit
->btr
, ft
, ft
->id
, ft
->in_ir
,
1994 BT_ASSERT(ft
->in_ir
);
1995 field
= stack_top(notit
->stack
)->base
;
1998 /* Append current substring */
1999 ret
= bt_field_string_append_with_length(field
, value
, len
);
2001 BT_LOGE("Cannot append substring to string field's value: "
2002 "notit-addr=%p, field-addr=%p, string-length=%zu, "
2003 "ret=%d", notit
, field
, len
, ret
);
2004 status
= BT_BTR_STATUS_ERROR
;
2013 enum bt_btr_status
btr_string_end_cb(
2014 struct ctf_field_type
*ft
, void *data
)
2016 struct bt_notif_iter
*notit
= data
;
2018 BT_LOGV("String (end) function called from BTR: "
2019 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2020 "ft-id=%d, ft-in-ir=%d",
2021 notit
, notit
->btr
, ft
, ft
->id
, ft
->in_ir
);
2022 BT_ASSERT(ft
->in_ir
);
2024 /* Pop string field */
2025 stack_pop(notit
->stack
);
2027 /* Go to next field */
2028 stack_top(notit
->stack
)->index
++;
2029 return BT_BTR_STATUS_OK
;
2032 enum bt_btr_status
btr_compound_begin_cb(
2033 struct ctf_field_type
*ft
, void *data
)
2035 struct bt_notif_iter
*notit
= data
;
2036 struct bt_field
*field
;
2038 BT_LOGV("Compound (beginning) function called from BTR: "
2039 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2040 "ft-id=%d, ft-in-ir=%d",
2041 notit
, notit
->btr
, ft
, ft
->id
, ft
->in_ir
);
2048 if (stack_empty(notit
->stack
)) {
2049 /* Root: already set by read_dscope_begin_state() */
2050 field
= notit
->cur_dscope_field
;
2052 field
= borrow_next_field(notit
);
2058 BT_ASSERT(bt_field_borrow_type(field
) == ft
->ir_ft
);
2059 stack_push(notit
->stack
, field
);
2062 * Change BTR "unsigned int" callback if it's a text
2065 if (ft
->id
== CTF_FIELD_TYPE_ID_ARRAY
||
2066 ft
->id
== CTF_FIELD_TYPE_ID_SEQUENCE
) {
2067 struct ctf_field_type_array_base
*array_ft
= (void *) ft
;
2069 if (array_ft
->is_text
) {
2072 BT_ASSERT(bt_field_get_type_id(field
) ==
2073 BT_FIELD_TYPE_ID_STRING
);
2074 notit
->done_filling_string
= false;
2075 ret
= bt_field_string_clear(field
);
2076 BT_ASSERT(ret
== 0);
2077 bt_btr_set_unsigned_int_cb(notit
->btr
,
2078 btr_unsigned_int_char_cb
);
2083 return BT_BTR_STATUS_OK
;
2086 enum bt_btr_status
btr_compound_end_cb(
2087 struct ctf_field_type
*ft
, void *data
)
2089 struct bt_notif_iter
*notit
= data
;
2091 BT_LOGV("Compound (end) function called from BTR: "
2092 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2093 "ft-id=%d, ft-in-ir=%d",
2094 notit
, notit
->btr
, ft
, ft
->id
, ft
->in_ir
);
2100 BT_ASSERT(!stack_empty(notit
->stack
));
2101 BT_ASSERT(bt_field_borrow_type(stack_top(notit
->stack
)->base
) ==
2105 * Reset BTR "unsigned int" callback if it's a text
2108 if (ft
->id
== CTF_FIELD_TYPE_ID_ARRAY
||
2109 ft
->id
== CTF_FIELD_TYPE_ID_SEQUENCE
) {
2110 struct ctf_field_type_array_base
*array_ft
= (void *) ft
;
2112 if (array_ft
->is_text
) {
2113 BT_ASSERT(bt_field_get_type_id(
2114 stack_top(notit
->stack
)->base
) ==
2115 BT_FIELD_TYPE_ID_STRING
);
2116 bt_btr_set_unsigned_int_cb(notit
->btr
,
2117 btr_unsigned_int_cb
);
2122 stack_pop(notit
->stack
);
2124 /* If the stack is not empty, increment the base's index */
2125 if (!stack_empty(notit
->stack
)) {
2126 stack_top(notit
->stack
)->index
++;
2130 return BT_BTR_STATUS_OK
;
2134 int64_t btr_get_sequence_length_cb(struct ctf_field_type
*ft
, void *data
)
2136 struct bt_field
*seq_field
;
2137 struct bt_notif_iter
*notit
= data
;
2138 struct ctf_field_type_sequence
*seq_ft
= (void *) ft
;
2139 int64_t length
= -1;
2142 length
= (uint64_t) g_array_index(notit
->stored_values
, uint64_t,
2143 seq_ft
->stored_length_index
);
2144 seq_field
= stack_top(notit
->stack
)->base
;
2145 BT_ASSERT(seq_field
);
2146 ret
= bt_field_dynamic_array_set_length(seq_field
, (uint64_t) length
);
2148 BT_LOGE("Cannot set dynamic array field's length field: "
2149 "notit-addr=%p, field-addr=%p, "
2150 "length=%" PRIu64
, notit
, seq_field
, length
);
2157 struct ctf_field_type
*btr_borrow_variant_selected_field_type_cb(
2158 struct ctf_field_type
*ft
, void *data
)
2162 int64_t option_index
= -1;
2163 struct bt_notif_iter
*notit
= data
;
2164 struct ctf_field_type_variant
*var_ft
= (void *) ft
;
2165 struct ctf_named_field_type
*selected_option
= NULL
;
2166 struct ctf_field_type
*ret_ft
= NULL
;
2172 /* Get variant's tag */
2173 tag
.u
= g_array_index(notit
->stored_values
, uint64_t,
2174 var_ft
->stored_tag_index
);
2177 * Check each range to find the selected option's index.
2179 if (var_ft
->tag_ft
->base
.is_signed
) {
2180 for (i
= 0; i
< var_ft
->ranges
->len
; i
++) {
2181 struct ctf_field_type_variant_range
*range
=
2182 ctf_field_type_variant_borrow_range_by_index(
2185 if (tag
.i
>= range
->range
.lower
.i
&&
2186 tag
.i
<= range
->range
.upper
.i
) {
2187 option_index
= (int64_t) range
->option_index
;
2192 for (i
= 0; i
< var_ft
->ranges
->len
; i
++) {
2193 struct ctf_field_type_variant_range
*range
=
2194 ctf_field_type_variant_borrow_range_by_index(
2197 if (tag
.u
>= range
->range
.lower
.u
&&
2198 tag
.u
<= range
->range
.upper
.u
) {
2199 option_index
= (int64_t) range
->option_index
;
2205 if (option_index
< 0) {
2206 BT_LOGW("Cannot find variant field type's option: "
2207 "notit-addr=%p, var-ft-addr=%p, u-tag=%" PRIu64
", "
2208 "i-tag=%" PRId64
, notit
, var_ft
, tag
.u
, tag
.i
);
2212 selected_option
= ctf_field_type_variant_borrow_option_by_index(
2213 var_ft
, (uint64_t) option_index
);
2215 if (selected_option
->ft
->in_ir
) {
2216 struct bt_field
*var_field
= stack_top(notit
->stack
)->base
;
2218 ret
= bt_field_variant_select_option_field(var_field
,
2221 BT_LOGW("Cannot select variant field's option field: "
2222 "notit-addr=%p, var-field-addr=%p, "
2223 "opt-index=%" PRId64
, notit
, var_field
,
2229 ret_ft
= selected_option
->ft
;
2236 void set_event_default_clock_value(struct bt_notif_iter
*notit
)
2238 struct bt_event
*event
= bt_notification_event_borrow_event(
2239 notit
->event_notif
);
2240 struct bt_stream_class
*sc
= notit
->meta
.sc
->ir_sc
;
2244 if (bt_stream_class_borrow_default_clock_class(sc
)) {
2245 int ret
= bt_event_set_default_clock_value(event
,
2246 notit
->default_clock_val
);
2248 BT_ASSERT(ret
== 0);
2253 void notify_new_stream(struct bt_notif_iter
*notit
,
2254 struct bt_notification
**notification
)
2256 enum bt_notif_iter_status status
;
2257 struct bt_notification
*ret
= NULL
;
2259 status
= set_current_stream(notit
);
2260 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
2265 BT_ASSERT(notit
->stream
);
2266 BT_ASSERT(notit
->notif_iter
);
2267 ret
= bt_notification_stream_begin_create(notit
->notif_iter
,
2270 BT_LOGE("Cannot create stream beginning notification: "
2271 "notit-addr=%p, stream-addr=%p",
2272 notit
, notit
->stream
);
2277 *notification
= ret
;
2281 void notify_end_of_stream(struct bt_notif_iter
*notit
,
2282 struct bt_notification
**notification
)
2284 struct bt_notification
*ret
;
2286 if (!notit
->stream
) {
2287 BT_LOGE("Cannot create stream for stream notification: "
2288 "notit-addr=%p", notit
);
2292 BT_ASSERT(notit
->notif_iter
);
2293 ret
= bt_notification_stream_end_create(notit
->notif_iter
,
2296 BT_LOGE("Cannot create stream beginning notification: "
2297 "notit-addr=%p, stream-addr=%p",
2298 notit
, notit
->stream
);
2301 *notification
= ret
;
2305 void notify_new_packet(struct bt_notif_iter
*notit
,
2306 struct bt_notification
**notification
)
2309 enum bt_notif_iter_status status
;
2310 struct bt_notification
*notif
= NULL
;
2311 struct bt_stream_class
*sc
;
2313 status
= set_current_packet(notit
);
2314 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
2318 BT_ASSERT(notit
->packet
);
2319 sc
= notit
->meta
.sc
->ir_sc
;
2322 if (bt_stream_class_packets_have_discarded_event_counter_snapshot(sc
)) {
2323 BT_ASSERT(notit
->snapshots
.discarded_events
!= UINT64_C(-1));
2324 ret
= bt_packet_set_discarded_event_counter_snapshot(
2325 notit
->packet
, notit
->snapshots
.discarded_events
);
2326 BT_ASSERT(ret
== 0);
2329 if (bt_stream_class_packets_have_packet_counter_snapshot(sc
)) {
2330 BT_ASSERT(notit
->snapshots
.packets
!= UINT64_C(-1));
2331 ret
= bt_packet_set_packet_counter_snapshot(
2332 notit
->packet
, notit
->snapshots
.packets
);
2333 BT_ASSERT(ret
== 0);
2336 if (bt_stream_class_packets_have_default_beginning_clock_value(sc
)) {
2337 BT_ASSERT(notit
->snapshots
.beginning_clock
!= UINT64_C(-1));
2338 ret
= bt_packet_set_default_beginning_clock_value(
2339 notit
->packet
, notit
->snapshots
.beginning_clock
);
2340 BT_ASSERT(ret
== 0);
2343 if (bt_stream_class_packets_have_default_end_clock_value(sc
)) {
2344 BT_ASSERT(notit
->snapshots
.end_clock
!= UINT64_C(-1));
2345 ret
= bt_packet_set_default_end_clock_value(
2346 notit
->packet
, notit
->snapshots
.end_clock
);
2347 BT_ASSERT(ret
== 0);
2350 if (notit
->packet_header_field
) {
2351 ret
= bt_packet_move_header_field(notit
->packet
,
2352 notit
->packet_header_field
);
2357 notit
->packet_header_field
= NULL
;
2360 * At this point notit->dscopes.trace_packet_header has
2361 * the same value as the packet header field within
2364 BT_ASSERT(bt_packet_borrow_header_field(notit
->packet
) ==
2365 notit
->dscopes
.trace_packet_header
);
2368 if (notit
->packet_context_field
) {
2369 ret
= bt_packet_move_context_field(notit
->packet
,
2370 notit
->packet_context_field
);
2375 notit
->packet_context_field
= NULL
;
2378 * At this point notit->dscopes.trace_packet_header has
2379 * the same value as the packet header field within
2382 BT_ASSERT(bt_packet_borrow_context_field(notit
->packet
) ==
2383 notit
->dscopes
.stream_packet_context
);
2386 BT_ASSERT(notit
->notif_iter
);
2387 notif
= bt_notification_packet_begin_create(notit
->notif_iter
,
2390 BT_LOGE("Cannot create packet beginning notification: "
2391 "notit-addr=%p, packet-addr=%p",
2392 notit
, notit
->packet
);
2396 *notification
= notif
;
2403 void notify_end_of_packet(struct bt_notif_iter
*notit
,
2404 struct bt_notification
**notification
)
2406 struct bt_notification
*notif
;
2408 if (!notit
->packet
) {
2412 /* Update default clock from packet's end time */
2413 if (notit
->snapshots
.end_clock
!= UINT64_C(-1)) {
2414 notit
->default_clock_val
= notit
->snapshots
.end_clock
;
2417 BT_ASSERT(notit
->notif_iter
);
2418 notif
= bt_notification_packet_end_create(notit
->notif_iter
,
2421 BT_LOGE("Cannot create packet end notification: "
2422 "notit-addr=%p, packet-addr=%p",
2423 notit
, notit
->packet
);
2428 BT_PUT(notit
->packet
);
2429 *notification
= notif
;
2433 struct bt_notif_iter
*bt_notif_iter_create(struct ctf_trace_class
*tc
,
2434 size_t max_request_sz
,
2435 struct bt_notif_iter_medium_ops medops
, void *data
)
2437 struct bt_notif_iter
*notit
= NULL
;
2438 struct bt_btr_cbs cbs
= {
2440 .signed_int
= btr_signed_int_cb
,
2441 .unsigned_int
= btr_unsigned_int_cb
,
2442 .floating_point
= btr_floating_point_cb
,
2443 .string_begin
= btr_string_begin_cb
,
2444 .string
= btr_string_cb
,
2445 .string_end
= btr_string_end_cb
,
2446 .compound_begin
= btr_compound_begin_cb
,
2447 .compound_end
= btr_compound_end_cb
,
2450 .get_sequence_length
= btr_get_sequence_length_cb
,
2451 .borrow_variant_selected_field_type
= btr_borrow_variant_selected_field_type_cb
,
2456 BT_ASSERT(medops
.request_bytes
);
2457 BT_ASSERT(medops
.borrow_stream
);
2458 BT_LOGD("Creating CTF plugin notification iterator: "
2459 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
2460 "data=%p", tc
, tc
->name
->str
, max_request_sz
, data
);
2461 notit
= g_new0(struct bt_notif_iter
, 1);
2463 BT_LOGE_STR("Failed to allocate one CTF plugin notification iterator.");
2466 notit
->meta
.tc
= tc
;
2467 notit
->medium
.medops
= medops
;
2468 notit
->medium
.max_request_sz
= max_request_sz
;
2469 notit
->medium
.data
= data
;
2470 notit
->stack
= stack_new(notit
);
2471 notit
->stored_values
= g_array_new(FALSE
, TRUE
, sizeof(uint64_t));
2472 g_array_set_size(notit
->stored_values
, tc
->stored_value_count
);
2474 if (!notit
->stack
) {
2475 BT_LOGE_STR("Failed to create field stack.");
2479 notit
->btr
= bt_btr_create(cbs
, notit
);
2481 BT_LOGE_STR("Failed to create binary type reader (BTR).");
2485 bt_notif_iter_reset(notit
);
2486 BT_LOGD("Created CTF plugin notification iterator: "
2487 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
2488 "data=%p, notit-addr=%p",
2489 tc
, tc
->name
->str
, max_request_sz
, data
,
2491 notit
->cur_packet_offset
= 0;
2497 bt_notif_iter_destroy(notit
);
2502 void bt_notif_iter_destroy(struct bt_notif_iter
*notit
)
2504 BT_PUT(notit
->packet
);
2505 BT_PUT(notit
->stream
);
2506 release_all_dscopes(notit
);
2508 BT_LOGD("Destroying CTF plugin notification iterator: addr=%p", notit
);
2511 BT_LOGD_STR("Destroying field stack.");
2512 stack_destroy(notit
->stack
);
2516 BT_LOGD("Destroying BTR: btr-addr=%p", notit
->btr
);
2517 bt_btr_destroy(notit
->btr
);
2520 if (notit
->stored_values
) {
2521 g_array_free(notit
->stored_values
, TRUE
);
2527 enum bt_notif_iter_status
bt_notif_iter_get_next_notification(
2528 struct bt_notif_iter
*notit
,
2529 struct bt_private_connection_private_notification_iterator
*notif_iter
,
2530 struct bt_notification
**notification
)
2532 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
2535 BT_ASSERT(notification
);
2537 if (notit
->state
== STATE_DONE
) {
2538 status
= BT_NOTIF_ITER_STATUS_EOF
;
2542 notit
->notif_iter
= notif_iter
;
2544 BT_LOGV("Getting next notification: notit-addr=%p", notit
);
2547 status
= handle_state(notit
);
2548 if (status
== BT_NOTIF_ITER_STATUS_AGAIN
) {
2549 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_AGAIN.");
2553 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
2554 if (status
== BT_NOTIF_ITER_STATUS_EOF
) {
2555 enum state next_state
= notit
->state
;
2557 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_EOF.");
2559 if (notit
->packet
) {
2560 notify_end_of_packet(notit
,
2563 notify_end_of_stream(notit
,
2565 next_state
= STATE_DONE
;
2568 if (!*notification
) {
2569 status
= BT_NOTIF_ITER_STATUS_ERROR
;
2573 status
= BT_NOTIF_ITER_STATUS_OK
;
2574 notit
->state
= next_state
;
2576 BT_LOGW("Cannot handle state: "
2577 "notit-addr=%p, state=%s",
2578 notit
, state_string(notit
->state
));
2584 switch (notit
->state
) {
2585 case STATE_EMIT_NOTIF_NEW_STREAM
:
2586 /* notify_new_stream() logs errors */
2587 notify_new_stream(notit
, notification
);
2589 if (!*notification
) {
2590 status
= BT_NOTIF_ITER_STATUS_ERROR
;
2593 notit
->stream_begin_emitted
= true;
2595 case STATE_EMIT_NOTIF_NEW_PACKET
:
2596 /* notify_new_packet() logs errors */
2597 notify_new_packet(notit
, notification
);
2599 if (!*notification
) {
2600 status
= BT_NOTIF_ITER_STATUS_ERROR
;
2604 case STATE_EMIT_NOTIF_EVENT
:
2605 BT_ASSERT(notit
->event_notif
);
2606 set_event_default_clock_value(notit
);
2607 *notification
= notit
->event_notif
;
2608 notit
->event_notif
= NULL
;
2610 case STATE_EMIT_NOTIF_END_OF_PACKET
:
2611 /* notify_end_of_packet() logs errors */
2612 notify_end_of_packet(notit
, notification
);
2614 if (!*notification
) {
2615 status
= BT_NOTIF_ITER_STATUS_ERROR
;
2620 /* Non-emitting state: continue */
2630 enum bt_notif_iter_status
bt_notif_iter_borrow_packet_header_context_fields(
2631 struct bt_notif_iter
*notit
,
2632 struct bt_field
**packet_header_field
,
2633 struct bt_field
**packet_context_field
)
2636 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
2640 if (notit
->state
== STATE_EMIT_NOTIF_NEW_PACKET
) {
2641 /* We're already there */
2646 status
= handle_state(notit
);
2647 if (status
== BT_NOTIF_ITER_STATUS_AGAIN
) {
2648 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_AGAIN.");
2651 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
2652 if (status
== BT_NOTIF_ITER_STATUS_EOF
) {
2653 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_EOF.");
2655 BT_LOGW("Cannot handle state: "
2656 "notit-addr=%p, state=%s",
2657 notit
, state_string(notit
->state
));
2662 switch (notit
->state
) {
2663 case STATE_EMIT_NOTIF_NEW_PACKET
:
2665 * Packet header and context fields are
2666 * potentially decoded (or they don't exist).
2670 case STATE_EMIT_NOTIF_NEW_STREAM
:
2671 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
2672 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
2673 case STATE_AFTER_TRACE_PACKET_HEADER
:
2674 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
2675 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
2676 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
2677 /* Non-emitting state: continue */
2681 * We should never get past the
2682 * STATE_EMIT_NOTIF_NEW_PACKET 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_NOTIF_ITER_STATUS_ERROR
;
2697 if (packet_header_field
) {
2698 *packet_header_field
= notit
->dscopes
.trace_packet_header
;
2701 if (packet_context_field
) {
2702 *packet_context_field
= notit
->dscopes
.stream_packet_context
;
2710 void bt_notif_iter_set_medops_data(struct bt_notif_iter
*notit
,
2714 notit
->medium
.data
= medops_data
;
2718 enum bt_notif_iter_status
bt_notif_iter_seek(
2719 struct bt_notif_iter
*notit
, off_t offset
)
2721 enum bt_notif_iter_status ret
= BT_NOTIF_ITER_STATUS_OK
;
2722 enum bt_notif_iter_medium_status medium_status
;
2726 BT_LOGE("Cannot seek to negative offset: offset=%jd", offset
);
2727 ret
= BT_NOTIF_ITER_STATUS_INVAL
;
2731 if (!notit
->medium
.medops
.seek
) {
2732 ret
= BT_NOTIF_ITER_STATUS_UNSUPPORTED
;
2733 BT_LOGD("Aborting seek as the iterator's underlying media does not implement seek support.");
2737 medium_status
= notit
->medium
.medops
.seek(
2738 BT_NOTIF_ITER_SEEK_WHENCE_SET
, offset
, notit
->medium
.data
);
2739 if (medium_status
!= BT_NOTIF_ITER_MEDIUM_STATUS_OK
) {
2740 if (medium_status
== BT_NOTIF_ITER_MEDIUM_STATUS_EOF
) {
2741 ret
= BT_NOTIF_ITER_STATUS_EOF
;
2743 ret
= BT_NOTIF_ITER_STATUS_ERROR
;
2748 bt_notif_iter_reset(notit
);
2749 notit
->cur_packet_offset
= offset
;
2756 off_t
bt_notif_iter_get_current_packet_offset(struct bt_notif_iter
*notit
)
2759 return notit
->cur_packet_offset
;
2763 off_t
bt_notif_iter_get_current_packet_size(
2764 struct bt_notif_iter
*notit
)
2767 return notit
->cur_exp_packet_total_size
;
2771 void bt_notif_trace_class_changed(struct bt_notif_iter
*notit
)
2773 if (notit
->meta
.tc
->stored_value_count
> notit
->stored_values
->len
) {
2774 g_array_set_size(notit
->stored_values
,
2775 notit
->meta
.tc
->stored_value_count
);
2780 enum bt_notif_iter_status
bt_notif_iter_get_packet_properties(
2781 struct bt_notif_iter
*notit
,
2782 struct bt_notif_iter_packet_properties
*props
)
2787 props
->exp_packet_total_size
=
2788 (uint64_t) notit
->cur_exp_packet_total_size
;
2789 props
->exp_packet_content_size
=
2790 (uint64_t) notit
->cur_exp_packet_content_size
;
2791 BT_ASSERT(props
->stream_class_id
>= 0);
2792 props
->stream_class_id
= (uint64_t) notit
->cur_stream_class_id
;
2793 props
->data_stream_id
= notit
->cur_data_stream_id
;
2794 props
->snapshots
.discarded_events
= notit
->snapshots
.discarded_events
;
2795 props
->snapshots
.packets
= notit
->snapshots
.packets
;
2796 props
->snapshots
.beginning_clock
= notit
->snapshots
.beginning_clock
;
2797 props
->snapshots
.end_clock
= notit
->snapshots
.end_clock
;
2798 return BT_NOTIF_ITER_STATUS_OK
;