2 * Babeltrace - CTF notification iterator
4 * Copyright (c) 2015-2016 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2015-2016 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 *) (top is last element) */
74 STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
,
75 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
76 STATE_AFTER_TRACE_PACKET_HEADER
,
77 STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
,
78 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
79 STATE_AFTER_STREAM_PACKET_CONTEXT
,
80 STATE_EMIT_NOTIF_NEW_STREAM
,
81 STATE_EMIT_NOTIF_NEW_PACKET
,
82 STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
,
83 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
,
84 STATE_AFTER_STREAM_EVENT_HEADER
,
85 STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
,
86 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
,
87 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
,
88 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
,
89 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
90 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
91 STATE_EMIT_NOTIF_EVENT
,
92 STATE_EMIT_NOTIF_END_OF_PACKET
,
94 STATE_SKIP_PACKET_PADDING
,
97 struct trace_field_path_cache
{
99 * Indexes of the stream_id and stream_instance_id field in the packet
100 * header structure, -1 if unset.
103 int stream_instance_id
;
106 struct stream_class_field_path_cache
{
108 * Indexes of the v and id fields in the stream event header structure,
115 * index of the timestamp_end, packet_size and content_size fields in
116 * the stream packet context structure. Set to -1 if the fields were
124 struct field_cb_override
{
125 enum bt_btr_status (* func
)(void *value
,
126 struct bt_field_type
*type
, void *data
);
130 /* CTF notification iterator */
131 struct bt_notif_iter
{
135 /* Current graph to create notifications (weak) */
136 struct bt_graph
*graph
;
139 * Current dynamic scope field pointer.
141 * This is set by read_dscope_begin_state() and contains the
142 * value of one of the pointers in `dscopes` below.
144 struct bt_field
*cur_dscope_field
;
146 /* Trace and classes (owned by this) */
148 struct bt_trace
*trace
;
149 struct bt_stream_class
*stream_class
;
150 struct bt_event_class
*event_class
;
151 struct bt_clock_class_priority_map
*cc_prio_map
;
154 /* Current packet header field wrapper (NULL if not created yet) */
155 struct bt_packet_header_field
*packet_header_field
;
157 /* Current packet header field wrapper (NULL if not created yet) */
158 struct bt_packet_context_field
*packet_context_field
;
160 /* Current event header field (NULL if not created yet) */
161 struct bt_event_header_field
*event_header_field
;
163 /* Current packet (NULL if not created yet) */
164 struct bt_packet
*packet
;
166 /* Current stream (NULL if not set yet) */
167 struct bt_stream
*stream
;
169 /* Current event (NULL if not created yet) */
170 struct bt_event
*event
;
172 /* Current event notification (NULL if not created yet) */
173 struct bt_notification
*event_notif
;
176 * Current `timestamp_end` field (to consider before switching
177 * packets). If it's set, it's a field which is within
178 * `dscopes.stream_packet_context` below, which is in `packet`
181 struct bt_field
*cur_timestamp_end
;
183 /* Database of current dynamic scopes (owned by this) */
185 struct bt_field
*trace_packet_header
;
186 struct bt_field
*stream_packet_context
;
187 struct bt_field
*stream_event_header
;
188 struct bt_field
*stream_event_context
;
189 struct bt_field
*event_context
;
190 struct bt_field
*event_payload
;
194 * Special field overrides.
196 * Overrides are used to implement the behaviours of special fields such
197 * as "timestamp_end" (which must be ignored until the end of the
198 * packet), "id" (event id) which can be present multiple times and must
199 * be updated multiple time.
201 * This should be used to implement the behaviour of integer fields
202 * mapped to clocks and other "tagged" fields (in CTF 2).
204 * bt_field_type to struct field_cb_override
206 GHashTable
*field_overrides
;
211 /* Current medium buffer data */
213 /* Last address provided by medium */
216 /* Buffer size provided by medium (bytes) */
219 /* Offset within whole packet of addr (bits) */
220 size_t packet_offset
;
222 /* Current position from addr (bits) */
225 /* Position of the last event header from addr (bits) */
229 /* Binary type reader */
232 /* Current medium data */
234 struct bt_notif_iter_medium_ops medops
;
235 size_t max_request_sz
;
239 /* Stream beginning was emitted */
240 bool stream_begin_emitted
;
242 /* Current packet size (bits) (-1 if unknown) */
243 int64_t cur_packet_size
;
245 /* Current content size (bits) (-1 if unknown) */
246 int64_t cur_content_size
;
249 * Offset, in the underlying media, of the current packet's start
252 off_t cur_packet_offset
;
254 /* bt_clock_class to uint64_t. */
255 GHashTable
*clock_states
;
258 * Cache of the trace-constant field paths (event header type)
259 * associated to the current trace.
261 struct trace_field_path_cache trace_field_path_cache
;
264 * Field path cache associated with the current stream class.
265 * Ownership of this structure belongs to the field_path_caches HT.
267 struct stream_class_field_path_cache
*cur_sc_field_path_cache
;
269 /* bt_stream_class to struct stream_class_field_path_cache. */
270 GHashTable
*sc_field_path_caches
;
274 const char *state_string(enum state state
)
279 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
280 return "STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN";
281 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
282 return "STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE";
283 case STATE_AFTER_TRACE_PACKET_HEADER
:
284 return "STATE_AFTER_TRACE_PACKET_HEADER";
285 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
286 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN";
287 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
288 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE";
289 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
290 return "STATE_AFTER_STREAM_PACKET_CONTEXT";
291 case STATE_EMIT_NOTIF_NEW_PACKET
:
292 return "STATE_EMIT_NOTIF_NEW_PACKET";
293 case STATE_EMIT_NOTIF_NEW_STREAM
:
294 return "STATE_EMIT_NOTIF_NEW_STREAM";
295 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
:
296 return "STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN";
297 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
:
298 return "STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE";
299 case STATE_AFTER_STREAM_EVENT_HEADER
:
300 return "STATE_AFTER_STREAM_EVENT_HEADER";
301 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
:
302 return "STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN";
303 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
:
304 return "STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE";
305 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN
:
306 return "STATE_DSCOPE_EVENT_CONTEXT_BEGIN";
307 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
:
308 return "STATE_DSCOPE_EVENT_CONTEXT_CONTINUE";
309 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
310 return "STATE_DSCOPE_EVENT_PAYLOAD_BEGIN";
311 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
312 return "STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE";
313 case STATE_EMIT_NOTIF_EVENT
:
314 return "STATE_EMIT_NOTIF_EVENT";
315 case STATE_EMIT_NOTIF_END_OF_PACKET
:
316 return "STATE_EMIT_NOTIF_END_OF_PACKET";
319 case STATE_SKIP_PACKET_PADDING
:
320 return "STATE_SKIP_PACKET_PADDING";
327 int bt_notif_iter_switch_packet(struct bt_notif_iter
*notit
);
330 enum bt_btr_status
btr_timestamp_end_cb(void *value
,
331 struct bt_field_type
*type
, void *data
);
334 void stack_entry_free_func(gpointer data
)
340 struct stack
*stack_new(struct bt_notif_iter
*notit
)
342 struct stack
*stack
= NULL
;
344 stack
= g_new0(struct stack
, 1);
346 BT_LOGE_STR("Failed to allocate one stack.");
350 stack
->entries
= g_ptr_array_new_with_free_func(stack_entry_free_func
);
351 if (!stack
->entries
) {
352 BT_LOGE_STR("Failed to allocate a GPtrArray.");
356 BT_LOGD("Created stack: notit-addr=%p, stack-addr=%p", notit
, stack
);
365 void stack_destroy(struct stack
*stack
)
368 BT_LOGD("Destroying stack: addr=%p", stack
);
369 g_ptr_array_free(stack
->entries
, TRUE
);
374 int stack_push(struct stack
*stack
, struct bt_field
*base
)
377 struct stack_entry
*entry
;
381 BT_LOGV("Pushing base field on stack: stack-addr=%p, "
382 "stack-size-before=%u, stack-size-after=%u",
383 stack
, stack
->entries
->len
, stack
->entries
->len
+ 1);
384 entry
= g_new0(struct stack_entry
, 1);
386 BT_LOGE_STR("Failed to allocate one stack entry.");
392 g_ptr_array_add(stack
->entries
, entry
);
399 unsigned int stack_size(struct stack
*stack
)
403 return stack
->entries
->len
;
407 void stack_pop(struct stack
*stack
)
410 BT_ASSERT(stack_size(stack
));
411 BT_LOGV("Popping from stack: "
412 "stack-addr=%p, stack-size-before=%u, stack-size-after=%u",
413 stack
, stack
->entries
->len
, stack
->entries
->len
- 1);
414 g_ptr_array_remove_index(stack
->entries
, stack
->entries
->len
- 1);
418 struct stack_entry
*stack_top(struct stack
*stack
)
421 BT_ASSERT(stack_size(stack
));
423 return g_ptr_array_index(stack
->entries
, stack
->entries
->len
- 1);
427 bool stack_empty(struct stack
*stack
)
429 return stack_size(stack
) == 0;
433 void stack_clear(struct stack
*stack
)
437 if (!stack_empty(stack
)) {
438 BT_LOGV("Clearing stack: stack-addr=%p, stack-size=%u",
439 stack
, stack
->entries
->len
);
440 g_ptr_array_remove_range(stack
->entries
, 0, stack_size(stack
));
443 BT_ASSERT(stack_empty(stack
));
447 enum bt_notif_iter_status
notif_iter_status_from_m_status(
448 enum bt_notif_iter_medium_status m_status
)
450 return (int) m_status
;
454 size_t buf_size_bits(struct bt_notif_iter
*notit
)
456 return notit
->buf
.sz
* 8;
460 size_t buf_available_bits(struct bt_notif_iter
*notit
)
462 return buf_size_bits(notit
) - notit
->buf
.at
;
466 size_t packet_at(struct bt_notif_iter
*notit
)
468 return notit
->buf
.packet_offset
+ notit
->buf
.at
;
472 void buf_consume_bits(struct bt_notif_iter
*notit
, size_t incr
)
474 BT_LOGV("Advancing cursor: notit-addr=%p, cur-before=%zu, cur-after=%zu",
475 notit
, notit
->buf
.at
, notit
->buf
.at
+ incr
);
476 notit
->buf
.at
+= incr
;
480 enum bt_notif_iter_status
request_medium_bytes(
481 struct bt_notif_iter
*notit
)
483 uint8_t *buffer_addr
= NULL
;
484 size_t buffer_sz
= 0;
485 enum bt_notif_iter_medium_status m_status
;
487 BT_LOGV("Calling user function (request bytes): notit-addr=%p, "
488 "request-size=%zu", notit
, notit
->medium
.max_request_sz
);
489 m_status
= notit
->medium
.medops
.request_bytes(
490 notit
->medium
.max_request_sz
, &buffer_addr
,
491 &buffer_sz
, notit
->medium
.data
);
492 BT_LOGV("User function returned: status=%s, buf-addr=%p, buf-size=%zu",
493 bt_notif_iter_medium_status_string(m_status
),
494 buffer_addr
, buffer_sz
);
495 if (m_status
== BT_NOTIF_ITER_MEDIUM_STATUS_OK
) {
496 BT_ASSERT(buffer_sz
!= 0);
498 /* New packet offset is old one + old size (in bits) */
499 notit
->buf
.packet_offset
+= buf_size_bits(notit
);
501 /* Restart at the beginning of the new medium buffer */
503 notit
->buf
.last_eh_at
= SIZE_MAX
;
505 /* New medium buffer size */
506 notit
->buf
.sz
= buffer_sz
;
508 /* New medium buffer address */
509 notit
->buf
.addr
= buffer_addr
;
511 BT_LOGV("User function returned new bytes: "
512 "packet-offset=%zu, cur=%zu, size=%zu, addr=%p",
513 notit
->buf
.packet_offset
, notit
->buf
.at
,
514 notit
->buf
.sz
, notit
->buf
.addr
);
515 BT_LOGV_MEM(buffer_addr
, buffer_sz
, "Returned bytes at %p:",
517 } else if (m_status
== BT_NOTIF_ITER_MEDIUM_STATUS_EOF
) {
519 * User returned end of stream: validate that we're not
520 * in the middle of a packet header, packet context, or
523 if (notit
->cur_packet_size
>= 0) {
524 if (packet_at(notit
) == notit
->cur_packet_size
) {
528 if (packet_at(notit
) == 0) {
532 if (notit
->buf
.last_eh_at
!= SIZE_MAX
&&
533 notit
->buf
.at
== notit
->buf
.last_eh_at
) {
538 /* All other states are invalid */
539 BT_LOGW("User function returned %s, but notification iterator is in an unexpected state: "
540 "state=%s, cur-packet-size=%" PRId64
", cur=%zu, "
541 "packet-cur=%zu, last-eh-at=%zu",
542 bt_notif_iter_medium_status_string(m_status
),
543 state_string(notit
->state
),
544 notit
->cur_packet_size
,
545 notit
->buf
.at
, packet_at(notit
),
546 notit
->buf
.last_eh_at
);
547 m_status
= BT_NOTIF_ITER_MEDIUM_STATUS_ERROR
;
548 } else if (m_status
< 0) {
549 BT_LOGW("User function failed: status=%s",
550 bt_notif_iter_medium_status_string(m_status
));
554 return notif_iter_status_from_m_status(m_status
);
558 enum bt_notif_iter_status
buf_ensure_available_bits(
559 struct bt_notif_iter
*notit
)
561 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
563 if (buf_available_bits(notit
) == 0) {
565 * This _cannot_ return BT_NOTIF_ITER_STATUS_OK
568 status
= request_medium_bytes(notit
);
575 enum bt_notif_iter_status
read_dscope_begin_state(
576 struct bt_notif_iter
*notit
,
577 struct bt_field_type
*dscope_field_type
,
578 enum state done_state
, enum state continue_state
,
579 struct bt_field
*dscope_field
)
581 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
582 enum bt_btr_status btr_status
;
583 size_t consumed_bits
;
585 notit
->cur_dscope_field
= dscope_field
;
586 BT_LOGV("Starting BTR: notit-addr=%p, btr-addr=%p, ft-addr=%p",
587 notit
, notit
->btr
, dscope_field_type
);
588 consumed_bits
= bt_btr_start(notit
->btr
, dscope_field_type
,
589 notit
->buf
.addr
, notit
->buf
.at
, packet_at(notit
),
590 notit
->buf
.sz
, &btr_status
);
591 BT_LOGV("BTR consumed bits: size=%zu", consumed_bits
);
593 switch (btr_status
) {
594 case BT_BTR_STATUS_OK
:
595 /* type was read completely */
596 BT_LOGV_STR("Field was completely decoded.");
597 notit
->state
= done_state
;
599 case BT_BTR_STATUS_EOF
:
600 BT_LOGV_STR("BTR needs more data to decode field completely.");
601 notit
->state
= continue_state
;
604 BT_LOGW("BTR failed to start: notit-addr=%p, btr-addr=%p, "
605 "status=%s", notit
, notit
->btr
,
606 bt_btr_status_string(btr_status
));
607 status
= BT_NOTIF_ITER_STATUS_ERROR
;
611 /* Consume bits now since we know we're not in an error state */
612 buf_consume_bits(notit
, consumed_bits
);
619 enum bt_notif_iter_status
read_dscope_continue_state(
620 struct bt_notif_iter
*notit
, enum state done_state
)
622 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
623 enum bt_btr_status btr_status
;
624 size_t consumed_bits
;
626 BT_LOGV("Continuing BTR: notit-addr=%p, btr-addr=%p",
629 status
= buf_ensure_available_bits(notit
);
630 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
632 BT_LOGW("Cannot ensure that buffer has at least one byte: "
633 "notif-addr=%p, status=%s",
634 notit
, bt_notif_iter_status_string(status
));
636 BT_LOGV("Cannot ensure that buffer has at least one byte: "
637 "notif-addr=%p, status=%s",
638 notit
, bt_notif_iter_status_string(status
));
645 consumed_bits
= bt_btr_continue(notit
->btr
, notit
->buf
.addr
,
646 notit
->buf
.sz
, &btr_status
);
647 BT_LOGV("BTR consumed bits: size=%zu", consumed_bits
);
649 switch (btr_status
) {
650 case BT_BTR_STATUS_OK
:
651 /* Type was read completely. */
652 BT_LOGV_STR("Field was completely decoded.");
653 notit
->state
= done_state
;
655 case BT_BTR_STATUS_EOF
:
656 /* Stay in this continue state. */
657 BT_LOGV_STR("BTR needs more data to decode field completely.");
660 BT_LOGW("BTR failed to continue: notit-addr=%p, btr-addr=%p, "
661 "status=%s", notit
, notit
->btr
,
662 bt_btr_status_string(btr_status
));
663 status
= BT_NOTIF_ITER_STATUS_ERROR
;
667 /* Consume bits now since we know we're not in an error state. */
668 buf_consume_bits(notit
, consumed_bits
);
674 void release_event_dscopes(struct bt_notif_iter
*notit
)
676 notit
->dscopes
.stream_event_header
= NULL
;
678 if (notit
->event_header_field
) {
679 bt_event_header_field_release(notit
->event_header_field
);
680 notit
->event_header_field
= NULL
;
683 notit
->dscopes
.stream_event_context
= NULL
;
684 notit
->dscopes
.event_context
= NULL
;
685 notit
->dscopes
.event_payload
= NULL
;
689 void release_all_dscopes(struct bt_notif_iter
*notit
)
691 notit
->dscopes
.trace_packet_header
= NULL
;
693 if (notit
->packet_header_field
) {
694 bt_packet_header_field_release(notit
->packet_header_field
);
695 notit
->packet_header_field
= NULL
;
698 notit
->dscopes
.stream_packet_context
= NULL
;
700 if (notit
->packet_context_field
) {
701 bt_packet_context_field_release(notit
->packet_context_field
);
702 notit
->packet_context_field
= NULL
;
705 release_event_dscopes(notit
);
709 enum bt_notif_iter_status
read_packet_header_begin_state(
710 struct bt_notif_iter
*notit
)
712 struct bt_field_type
*packet_header_type
= NULL
;
713 enum bt_notif_iter_status ret
= BT_NOTIF_ITER_STATUS_OK
;
715 if (bt_notif_iter_switch_packet(notit
)) {
716 BT_LOGW("Cannot switch packet: notit-addr=%p", notit
);
717 ret
= BT_NOTIF_ITER_STATUS_ERROR
;
721 /* Packet header type is common to the whole trace. */
722 packet_header_type
= bt_trace_borrow_packet_header_field_type(
724 if (!packet_header_type
) {
725 notit
->state
= STATE_AFTER_TRACE_PACKET_HEADER
;
730 * Create free packet header field from trace. This field is
731 * going to be moved to the packet once we create it. We cannot
732 * create the packet now because:
734 * 1. A packet is created from a stream.
735 * 2. A stream is created from a stream class.
736 * 3. We need the packet header field's content to know the ID
737 * of the stream class to select.
739 BT_ASSERT(!notit
->packet_header_field
);
740 notit
->packet_header_field
= bt_trace_create_packet_header_field(
742 if (!notit
->packet_header_field
) {
743 BT_LOGE_STR("Cannot create packet header field wrapper from trace.");
744 ret
= BT_NOTIF_ITER_STATUS_ERROR
;
748 notit
->dscopes
.trace_packet_header
=
749 bt_packet_header_field_borrow_field(notit
->packet_header_field
);
750 BT_ASSERT(notit
->dscopes
.trace_packet_header
);
751 BT_LOGV("Decoding packet header field:"
752 "notit-addr=%p, trace-addr=%p, trace-name=\"%s\", ft-addr=%p",
753 notit
, notit
->meta
.trace
,
754 bt_trace_get_name(notit
->meta
.trace
), packet_header_type
);
755 ret
= read_dscope_begin_state(notit
, packet_header_type
,
756 STATE_AFTER_TRACE_PACKET_HEADER
,
757 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
758 notit
->dscopes
.trace_packet_header
);
760 BT_LOGW("Cannot decode packet header field: "
761 "notit-addr=%p, trace-addr=%p, "
762 "trace-name=\"%s\", ft-addr=%p",
763 notit
, notit
->meta
.trace
,
764 bt_trace_get_name(notit
->meta
.trace
),
773 enum bt_notif_iter_status
read_packet_header_continue_state(
774 struct bt_notif_iter
*notit
)
776 return read_dscope_continue_state(notit
,
777 STATE_AFTER_TRACE_PACKET_HEADER
);
781 struct stream_class_field_path_cache
*
782 create_stream_class_field_path_cache_entry(
783 struct bt_notif_iter
*notit
,
784 struct bt_stream_class
*stream_class
)
788 int timestamp_end
= -1;
789 int packet_size
= -1;
790 int content_size
= -1;
791 struct stream_class_field_path_cache
*cache_entry
= g_new0(
792 struct stream_class_field_path_cache
, 1);
793 struct bt_field_type
*event_header
= NULL
, *packet_context
= NULL
;
796 BT_LOGE_STR("Failed to allocate one stream class field path cache.");
800 event_header
= bt_stream_class_borrow_event_header_field_type(
802 if (event_header
&& bt_field_type_is_structure(event_header
)) {
805 count
= bt_field_type_structure_get_field_count(
807 BT_ASSERT(count
>= 0);
809 for (i
= 0; i
< count
; i
++) {
813 ret
= bt_field_type_structure_borrow_field_by_index(
814 event_header
, &name
, NULL
, i
);
816 BT_LOGE("Cannot get event header structure field type's field: "
817 "notit-addr=%p, stream-class-addr=%p, "
818 "stream-class-name=\"%s\", "
819 "stream-class-id=%" PRId64
", "
820 "ft-addr=%p, index=%d",
822 bt_stream_class_get_name(stream_class
),
823 bt_stream_class_get_id(stream_class
),
828 if (v
!= -1 && id
!= -1) {
832 if (v
== -1 && strcmp(name
, "v") == 0) {
834 } else if (id
== -1 && !strcmp(name
, "id")) {
840 packet_context
= bt_stream_class_borrow_packet_context_field_type(
842 if (packet_context
&& bt_field_type_is_structure(packet_context
)) {
845 count
= bt_field_type_structure_get_field_count(
847 BT_ASSERT(count
>= 0);
849 for (i
= 0; i
< count
; i
++) {
852 struct bt_field_type
*field_type
;
854 if (timestamp_end
!= -1 && packet_size
!= -1 &&
855 content_size
!= -1) {
859 ret
= bt_field_type_structure_borrow_field_by_index(
860 packet_context
, &name
, &field_type
, i
);
862 BT_LOGE("Cannot get packet context structure field type's field: "
863 "notit-addr=%p, stream-class-addr=%p, "
864 "stream-class-name=\"%s\", "
865 "stream-class-id=%" PRId64
", "
866 "ft-addr=%p, index=%d",
868 bt_stream_class_get_name(stream_class
),
869 bt_stream_class_get_id(stream_class
),
874 if (timestamp_end
== -1 &&
875 strcmp(name
, "timestamp_end") == 0) {
876 struct field_cb_override
*override
= g_new0(
877 struct field_cb_override
, 1);
883 override
->func
= btr_timestamp_end_cb
;
884 override
->data
= notit
;
885 g_hash_table_insert(notit
->field_overrides
,
886 field_type
, override
);
888 } else if (packet_size
== -1 &&
889 !strcmp(name
, "packet_size")) {
891 } else if (content_size
== -1 &&
892 !strcmp(name
, "content_size")) {
899 cache_entry
->id
= id
;
900 cache_entry
->timestamp_end
= timestamp_end
;
901 cache_entry
->packet_size
= packet_size
;
902 cache_entry
->content_size
= content_size
;
914 struct stream_class_field_path_cache
*get_stream_class_field_path_cache(
915 struct bt_notif_iter
*notit
,
916 struct bt_stream_class
*stream_class
)
918 bool cache_entry_found
;
919 struct stream_class_field_path_cache
*cache_entry
;
921 cache_entry_found
= g_hash_table_lookup_extended(
922 notit
->sc_field_path_caches
,
923 stream_class
, NULL
, (gpointer
) &cache_entry
);
924 if (unlikely(!cache_entry_found
)) {
925 cache_entry
= create_stream_class_field_path_cache_entry(notit
,
927 g_hash_table_insert(notit
->sc_field_path_caches
,
928 stream_class
, (gpointer
) cache_entry
);
935 enum bt_notif_iter_status
set_current_stream_class(
936 struct bt_notif_iter
*notit
)
938 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
939 struct bt_field_type
*packet_header_type
= NULL
;
940 struct bt_field_type
*stream_id_field_type
= NULL
;
941 struct bt_stream_class
*new_stream_class
= NULL
;
944 /* Clear the current stream class field path cache. */
945 notit
->cur_sc_field_path_cache
= NULL
;
947 /* Is there any "stream_id" field in the packet header? */
948 packet_header_type
= bt_trace_borrow_packet_header_field_type(
950 if (!packet_header_type
) {
952 * No packet header, therefore no `stream_id` field,
953 * therefore only one stream class.
955 goto single_stream_class
;
958 BT_ASSERT(bt_field_type_is_structure(packet_header_type
));
961 stream_id_field_type
=
962 bt_field_type_structure_borrow_field_type_by_name(
963 packet_header_type
, "stream_id");
964 if (stream_id_field_type
) {
965 /* Find appropriate stream class using current stream ID */
967 struct bt_field
*stream_id_field
= NULL
;
969 BT_ASSERT(notit
->dscopes
.trace_packet_header
);
972 stream_id_field
= bt_field_structure_borrow_field_by_name(
973 notit
->dscopes
.trace_packet_header
, "stream_id");
974 BT_ASSERT(stream_id_field
);
975 ret
= bt_field_integer_unsigned_get_value(
976 stream_id_field
, &stream_id
);
980 /* Only one stream: pick the first stream class */
981 BT_ASSERT(bt_trace_get_stream_class_count(
982 notit
->meta
.trace
) == 1);
986 BT_LOGV("Found stream class ID to use: notit-addr=%p, "
987 "stream-class-id=%" PRIu64
", "
988 "trace-addr=%p, trace-name=\"%s\"",
989 notit
, stream_id
, notit
->meta
.trace
,
990 bt_trace_get_name(notit
->meta
.trace
));
992 new_stream_class
= bt_trace_borrow_stream_class_by_id(
993 notit
->meta
.trace
, stream_id
);
994 if (!new_stream_class
) {
995 BT_LOGW("No stream class with ID of stream class ID to use in trace: "
996 "notit-addr=%p, stream-class-id=%" PRIu64
", "
997 "trace-addr=%p, trace-name=\"%s\"",
998 notit
, stream_id
, notit
->meta
.trace
,
999 bt_trace_get_name(notit
->meta
.trace
));
1000 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1004 if (notit
->meta
.stream_class
) {
1005 if (new_stream_class
!= notit
->meta
.stream_class
) {
1006 BT_LOGW("Two packets refer to two different stream classes within the same packet sequence: "
1007 "notit-addr=%p, prev-stream-class-addr=%p, "
1008 "prev-stream-class-name=\"%s\", "
1009 "prev-stream-class-id=%" PRId64
", "
1010 "next-stream-class-addr=%p, "
1011 "next-stream-class-name=\"%s\", "
1012 "next-stream-class-id=%" PRId64
", "
1013 "trace-addr=%p, trace-name=\"%s\"",
1014 notit
, notit
->meta
.stream_class
,
1015 bt_stream_class_get_name(notit
->meta
.stream_class
),
1016 bt_stream_class_get_id(notit
->meta
.stream_class
),
1018 bt_stream_class_get_name(new_stream_class
),
1019 bt_stream_class_get_id(new_stream_class
),
1021 bt_trace_get_name(notit
->meta
.trace
));
1022 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1026 notit
->meta
.stream_class
= new_stream_class
;
1029 BT_LOGV("Set current stream class: "
1030 "notit-addr=%p, stream-class-addr=%p, "
1031 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
1032 notit
, notit
->meta
.stream_class
,
1033 bt_stream_class_get_name(notit
->meta
.stream_class
),
1034 bt_stream_class_get_id(notit
->meta
.stream_class
));
1037 * Retrieve (or lazily create) the current stream class field path
1040 notit
->cur_sc_field_path_cache
= get_stream_class_field_path_cache(
1041 notit
, notit
->meta
.stream_class
);
1042 if (!notit
->cur_sc_field_path_cache
) {
1043 BT_LOGW("Cannot retrieve stream class field path from cache: "
1044 "notit-addr=%p, stream-class-addr=%p, "
1045 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
1046 notit
, notit
->meta
.stream_class
,
1047 bt_stream_class_get_name(notit
->meta
.stream_class
),
1048 bt_stream_class_get_id(notit
->meta
.stream_class
));
1049 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1058 uint64_t get_cur_stream_instance_id(struct bt_notif_iter
*notit
)
1060 struct bt_field
*stream_instance_id_field
= NULL
;
1061 uint64_t stream_instance_id
= -1ULL;
1064 if (!notit
->dscopes
.trace_packet_header
) {
1068 stream_instance_id_field
= bt_field_structure_borrow_field_by_name(
1069 notit
->dscopes
.trace_packet_header
, "stream_instance_id");
1070 if (!stream_instance_id_field
) {
1074 ret
= bt_field_integer_unsigned_get_value(stream_instance_id_field
,
1075 &stream_instance_id
);
1077 stream_instance_id
= -1ULL;
1082 return stream_instance_id
;
1086 enum bt_notif_iter_status
set_current_stream(struct bt_notif_iter
*notit
)
1088 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1089 struct bt_stream
*stream
= NULL
;
1091 BT_LOGV("Calling user function (get stream): notit-addr=%p, "
1092 "stream-class-addr=%p, stream-class-name=\"%s\", "
1093 "stream-class-id=%" PRId64
,
1094 notit
, notit
->meta
.stream_class
,
1095 bt_stream_class_get_name(notit
->meta
.stream_class
),
1096 bt_stream_class_get_id(notit
->meta
.stream_class
));
1097 stream
= bt_get(notit
->medium
.medops
.borrow_stream(
1098 notit
->meta
.stream_class
, get_cur_stream_instance_id(notit
),
1099 notit
->medium
.data
));
1100 BT_LOGV("User function returned: stream-addr=%p", stream
);
1102 BT_LOGW_STR("User function failed to return a stream object for the given stream class.");
1103 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1107 if (notit
->stream
&& stream
!= notit
->stream
) {
1108 BT_LOGW("User function returned a different stream than the previous one for the same sequence of packets.");
1109 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1113 BT_MOVE(notit
->stream
, stream
);
1121 enum bt_notif_iter_status
set_current_packet(struct bt_notif_iter
*notit
)
1123 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1124 struct bt_packet
*packet
= NULL
;
1126 BT_LOGV("Creating packet for packet notification: "
1127 "notit-addr=%p", notit
);
1128 BT_LOGV("Creating packet from stream: "
1129 "notit-addr=%p, stream-addr=%p, "
1130 "stream-class-addr=%p, "
1131 "stream-class-name=\"%s\", "
1132 "stream-class-id=%" PRId64
,
1133 notit
, notit
->stream
, notit
->meta
.stream_class
,
1134 bt_stream_class_get_name(notit
->meta
.stream_class
),
1135 bt_stream_class_get_id(notit
->meta
.stream_class
));
1138 BT_ASSERT(notit
->stream
);
1139 packet
= bt_packet_create(notit
->stream
);
1141 BT_LOGE("Cannot create packet from stream: "
1142 "notit-addr=%p, stream-addr=%p, "
1143 "stream-class-addr=%p, "
1144 "stream-class-name=\"%s\", "
1145 "stream-class-id=%" PRId64
,
1146 notit
, notit
->stream
, notit
->meta
.stream_class
,
1147 bt_stream_class_get_name(notit
->meta
.stream_class
),
1148 bt_stream_class_get_id(notit
->meta
.stream_class
));
1156 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1159 BT_MOVE(notit
->packet
, packet
);
1164 enum bt_notif_iter_status
after_packet_header_state(
1165 struct bt_notif_iter
*notit
)
1167 enum bt_notif_iter_status status
;
1169 status
= set_current_stream_class(notit
);
1170 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
1174 notit
->state
= STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
;
1181 enum bt_notif_iter_status
read_packet_context_begin_state(
1182 struct bt_notif_iter
*notit
)
1184 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1185 struct bt_field_type
*packet_context_type
;
1187 BT_ASSERT(notit
->meta
.stream_class
);
1188 packet_context_type
= bt_stream_class_borrow_packet_context_field_type(
1189 notit
->meta
.stream_class
);
1190 if (!packet_context_type
) {
1191 BT_LOGV("No packet packet context field type in stream class: continuing: "
1192 "notit-addr=%p, stream-class-addr=%p, "
1193 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
1194 notit
, notit
->meta
.stream_class
,
1195 bt_stream_class_get_name(notit
->meta
.stream_class
),
1196 bt_stream_class_get_id(notit
->meta
.stream_class
));
1197 notit
->state
= STATE_AFTER_STREAM_PACKET_CONTEXT
;
1202 * Create free packet context field from stream class. This
1203 * field is going to be moved to the packet once we create it.
1204 * We cannot create the packet now because a packet is created
1205 * from a stream, and this API must be able to return the packet
1206 * header and context fields without creating a stream
1207 * (bt_notif_iter_get_packet_header_context_fields()).
1209 BT_ASSERT(!notit
->packet_context_field
);
1210 notit
->packet_context_field
=
1211 bt_stream_class_create_packet_context_field(
1212 notit
->meta
.stream_class
);
1213 if (!notit
->packet_context_field
) {
1214 BT_LOGE_STR("Cannot create packet context field wrapper from stream class.");
1215 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1219 notit
->dscopes
.stream_packet_context
=
1220 bt_packet_context_field_borrow_field(notit
->packet_context_field
);
1221 BT_ASSERT(notit
->dscopes
.stream_packet_context
);
1222 BT_LOGV("Decoding packet context field: "
1223 "notit-addr=%p, stream-class-addr=%p, "
1224 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1226 notit
, notit
->meta
.stream_class
,
1227 bt_stream_class_get_name(notit
->meta
.stream_class
),
1228 bt_stream_class_get_id(notit
->meta
.stream_class
),
1229 packet_context_type
);
1230 status
= read_dscope_begin_state(notit
, packet_context_type
,
1231 STATE_AFTER_STREAM_PACKET_CONTEXT
,
1232 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
1233 notit
->dscopes
.stream_packet_context
);
1235 BT_LOGW("Cannot decode packet context field: "
1236 "notit-addr=%p, stream-class-addr=%p, "
1237 "stream-class-name=\"%s\", "
1238 "stream-class-id=%" PRId64
", ft-addr=%p",
1239 notit
, notit
->meta
.stream_class
,
1240 bt_stream_class_get_name(notit
->meta
.stream_class
),
1241 bt_stream_class_get_id(notit
->meta
.stream_class
),
1242 packet_context_type
);
1250 enum bt_notif_iter_status
read_packet_context_continue_state(
1251 struct bt_notif_iter
*notit
)
1253 return read_dscope_continue_state(notit
,
1254 STATE_AFTER_STREAM_PACKET_CONTEXT
);
1258 enum bt_notif_iter_status
set_current_packet_content_sizes(
1259 struct bt_notif_iter
*notit
)
1261 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1262 struct bt_field
*packet_size_field
= NULL
;
1263 struct bt_field
*content_size_field
= NULL
;
1264 uint64_t content_size
= -1ULL, packet_size
= -1ULL;
1266 if (!notit
->dscopes
.stream_packet_context
) {
1270 packet_size_field
= bt_field_structure_borrow_field_by_name(
1271 notit
->dscopes
.stream_packet_context
, "packet_size");
1272 content_size_field
= bt_field_structure_borrow_field_by_name(
1273 notit
->dscopes
.stream_packet_context
, "content_size");
1274 if (packet_size_field
) {
1275 int ret
= bt_field_integer_unsigned_get_value(
1276 packet_size_field
, &packet_size
);
1278 BT_ASSERT(ret
== 0);
1279 if (packet_size
== 0) {
1280 BT_LOGW("Invalid packet size: packet context field indicates packet size is zero: "
1281 "notit-addr=%p, packet-context-field-addr=%p",
1282 notit
, notit
->dscopes
.stream_packet_context
);
1283 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1285 } else if ((packet_size
% 8) != 0) {
1286 BT_LOGW("Invalid packet size: packet context field indicates packet size is not a multiple of 8: "
1287 "notit-addr=%p, packet-context-field-addr=%p, "
1288 "packet-size=%" PRIu64
,
1289 notit
, notit
->dscopes
.stream_packet_context
,
1291 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1296 if (content_size_field
) {
1297 int ret
= bt_field_integer_unsigned_get_value(
1298 content_size_field
, &content_size
);
1300 BT_ASSERT(ret
== 0);
1302 content_size
= packet_size
;
1305 if (content_size
> packet_size
) {
1306 BT_LOGW("Invalid packet or content size: packet context field indicates content size is greater than packet size: "
1307 "notit-addr=%p, packet-context-field-addr=%p, "
1308 "packet-size=%" PRIu64
", content-size=%" PRIu64
,
1309 notit
, notit
->dscopes
.stream_packet_context
,
1310 packet_size
, content_size
);
1311 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1315 if (packet_size
!= -1ULL) {
1316 notit
->cur_packet_size
= packet_size
;
1319 * Use the content size as packet size indicator if the
1320 * packet size field is missing. This means there is no
1321 * padding in this stream.
1323 notit
->cur_packet_size
= content_size
;
1325 notit
->cur_content_size
= content_size
;
1326 BT_LOGV("Set current packet and content sizes: "
1327 "notit-addr=%p, packet-size=%" PRIu64
", content-size=%" PRIu64
,
1328 notit
, packet_size
, content_size
);
1334 enum bt_notif_iter_status
after_packet_context_state(
1335 struct bt_notif_iter
*notit
)
1337 enum bt_notif_iter_status status
;
1339 status
= set_current_packet_content_sizes(notit
);
1340 if (status
== BT_NOTIF_ITER_STATUS_OK
) {
1341 if (notit
->stream_begin_emitted
) {
1342 notit
->state
= STATE_EMIT_NOTIF_NEW_PACKET
;
1344 notit
->state
= STATE_EMIT_NOTIF_NEW_STREAM
;
1352 enum bt_notif_iter_status
read_event_header_begin_state(
1353 struct bt_notif_iter
*notit
)
1355 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1356 struct bt_field_type
*event_header_type
= NULL
;
1358 /* Reset the position of the last event header */
1359 notit
->buf
.last_eh_at
= notit
->buf
.at
;
1361 /* Check if we have some content left */
1362 if (notit
->cur_content_size
>= 0) {
1363 if (packet_at(notit
) == notit
->cur_content_size
) {
1364 /* No more events! */
1365 BT_LOGV("Reached end of packet: notit-addr=%p, "
1366 "cur=%zu", notit
, packet_at(notit
));
1367 notit
->state
= STATE_EMIT_NOTIF_END_OF_PACKET
;
1369 } else if (packet_at(notit
) > notit
->cur_content_size
) {
1370 /* That's not supposed to happen */
1371 BT_LOGV("Before decoding event header field: cursor is passed the packet's content: "
1372 "notit-addr=%p, content-size=%" PRId64
", "
1373 "cur=%zu", notit
, notit
->cur_content_size
,
1375 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1380 release_event_dscopes(notit
);
1381 BT_ASSERT(notit
->meta
.stream_class
);
1382 event_header_type
= bt_stream_class_borrow_event_header_field_type(
1383 notit
->meta
.stream_class
);
1384 if (!event_header_type
) {
1385 notit
->state
= STATE_AFTER_STREAM_EVENT_HEADER
;
1389 BT_ASSERT(!notit
->event_header_field
);
1390 notit
->event_header_field
= bt_stream_class_create_event_header_field(
1391 notit
->meta
.stream_class
);
1392 if (!notit
->event_header_field
) {
1393 BT_LOGE_STR("Cannot create event header field wrapper from trace.");
1394 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1398 notit
->dscopes
.stream_event_header
=
1399 bt_event_header_field_borrow_field(notit
->event_header_field
);
1400 BT_ASSERT(notit
->dscopes
.stream_event_header
);
1401 BT_LOGV("Decoding event header field: "
1402 "notit-addr=%p, stream-class-addr=%p, "
1403 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1405 notit
, notit
->meta
.stream_class
,
1406 bt_stream_class_get_name(notit
->meta
.stream_class
),
1407 bt_stream_class_get_id(notit
->meta
.stream_class
),
1409 status
= read_dscope_begin_state(notit
, event_header_type
,
1410 STATE_AFTER_STREAM_EVENT_HEADER
,
1411 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
,
1412 notit
->dscopes
.stream_event_header
);
1414 BT_LOGW("Cannot decode event header field: "
1415 "notit-addr=%p, stream-class-addr=%p, "
1416 "stream-class-name=\"%s\", "
1417 "stream-class-id=%" PRId64
", ft-addr=%p",
1418 notit
, notit
->meta
.stream_class
,
1419 bt_stream_class_get_name(notit
->meta
.stream_class
),
1420 bt_stream_class_get_id(notit
->meta
.stream_class
),
1429 enum bt_notif_iter_status
read_event_header_continue_state(
1430 struct bt_notif_iter
*notit
)
1432 return read_dscope_continue_state(notit
,
1433 STATE_AFTER_STREAM_EVENT_HEADER
);
1437 enum bt_notif_iter_status
set_current_event_class(struct bt_notif_iter
*notit
)
1440 * The assert() calls in this function are okay because it is
1441 * assumed here that all the metadata objects have been
1442 * validated for CTF correctness before decoding actual streams.
1445 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1446 struct bt_field_type
*event_header_type
;
1447 struct bt_field_type
*id_field_type
= NULL
;
1448 struct bt_field_type
*v_field_type
= NULL
;
1449 uint64_t event_id
= -1ULL;
1452 event_header_type
= bt_stream_class_borrow_event_header_field_type(
1453 notit
->meta
.stream_class
);
1454 if (!event_header_type
) {
1456 * No event header, therefore no event class ID field,
1457 * therefore only one event class.
1459 goto single_event_class
;
1462 /* Is there any "id"/"v" field in the event header? */
1463 BT_ASSERT(bt_field_type_is_structure(event_header_type
));
1464 id_field_type
= bt_field_type_structure_borrow_field_type_by_name(
1465 event_header_type
, "id");
1466 v_field_type
= bt_field_type_structure_borrow_field_type_by_name(
1467 event_header_type
, "v");
1468 BT_ASSERT(notit
->dscopes
.stream_event_header
);
1472 * | | |_ _|_ _| __ __ _
1473 * | | | | | || '_ \ / _` |
1474 * | |___| | | || | | | (_| | S P E C I A L
1475 * |_____|_| |_||_| |_|\__, | C A S E â„¢
1478 struct bt_field
*v_field
= NULL
;
1479 struct bt_field
*v_struct_field
= NULL
;
1480 struct bt_field
*v_struct_id_field
= NULL
;
1482 // TODO: optimalize!
1483 v_field
= bt_field_structure_borrow_field_by_name(
1484 notit
->dscopes
.stream_event_header
, "v");
1488 bt_field_variant_borrow_current_field(v_field
);
1489 if (!v_struct_field
) {
1490 goto end_v_field_type
;
1493 // TODO: optimalize!
1494 v_struct_id_field
= bt_field_structure_borrow_field_by_name(
1495 v_struct_field
, "id");
1496 if (!v_struct_id_field
) {
1497 goto end_v_field_type
;
1500 if (bt_field_is_integer(v_struct_id_field
)) {
1501 ret
= bt_field_integer_unsigned_get_value(
1502 v_struct_id_field
, &event_id
);
1504 BT_LOGV("Cannot get value of unsigned integer field (`id`): continuing: "
1505 "notit=%p, field-addr=%p",
1506 notit
, v_struct_id_field
);
1513 if (id_field_type
&& event_id
== -1ULL) {
1514 /* Check "id" field */
1515 struct bt_field
*id_field
= NULL
;
1516 int ret_get_value
= 0;
1518 // TODO: optimalize!
1519 id_field
= bt_field_structure_borrow_field_by_name(
1520 notit
->dscopes
.stream_event_header
, "id");
1522 goto check_event_id
;
1525 ret_get_value
= bt_field_integer_unsigned_get_value(
1526 id_field
, &event_id
);
1527 BT_ASSERT(ret_get_value
== 0);
1531 if (event_id
== -1ULL) {
1533 /* Event ID not found: single event? */
1534 BT_ASSERT(bt_stream_class_get_event_class_count(
1535 notit
->meta
.stream_class
) == 1);
1539 BT_LOGV("Found event class ID to use: notit-addr=%p, "
1540 "stream-class-addr=%p, stream-class-name=\"%s\", "
1541 "stream-class-id=%" PRId64
", "
1542 "event-class-id=%" PRIu64
,
1543 notit
, notit
->meta
.stream_class
,
1544 bt_stream_class_get_name(notit
->meta
.stream_class
),
1545 bt_stream_class_get_id(notit
->meta
.stream_class
),
1547 notit
->meta
.event_class
= bt_stream_class_borrow_event_class_by_id(
1548 notit
->meta
.stream_class
, event_id
);
1549 if (!notit
->meta
.event_class
) {
1550 BT_LOGW("No event class with ID of event class ID to use in stream class: "
1551 "notit-addr=%p, stream-class-addr=%p, "
1552 "stream-class-name=\"%s\", "
1553 "stream-class-id=%" PRId64
", "
1554 "event-class-id=%" PRIu64
,
1555 notit
, notit
->meta
.stream_class
,
1556 bt_stream_class_get_name(notit
->meta
.stream_class
),
1557 bt_stream_class_get_id(notit
->meta
.stream_class
),
1559 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1563 BT_LOGV("Set current event class: "
1564 "notit-addr=%p, event-class-addr=%p, "
1565 "event-class-name=\"%s\", event-class-id=%" PRId64
,
1566 notit
, notit
->meta
.event_class
,
1567 bt_event_class_get_name(notit
->meta
.event_class
),
1568 bt_event_class_get_id(notit
->meta
.event_class
));
1575 enum bt_notif_iter_status
set_current_event_notification(
1576 struct bt_notif_iter
*notit
)
1578 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1579 struct bt_notification
*notif
= NULL
;
1581 BT_ASSERT(notit
->meta
.event_class
);
1582 BT_ASSERT(notit
->packet
);
1583 BT_LOGV("Creating event notification from event class and packet: "
1584 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", packet-addr=%p",
1585 notit
, notit
->meta
.event_class
,
1586 bt_event_class_get_name(notit
->meta
.event_class
),
1588 BT_ASSERT(notit
->graph
);
1589 notif
= bt_notification_event_create(notit
->graph
,
1590 notit
->meta
.event_class
, notit
->packet
,
1591 notit
->meta
.cc_prio_map
);
1593 BT_LOGE("Cannot create event notification: "
1594 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", "
1596 notit
, notit
->meta
.event_class
,
1597 bt_event_class_get_name(notit
->meta
.event_class
),
1606 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1609 BT_MOVE(notit
->event_notif
, notif
);
1614 enum bt_notif_iter_status
after_event_header_state(
1615 struct bt_notif_iter
*notit
)
1617 enum bt_notif_iter_status status
;
1619 status
= set_current_event_class(notit
);
1620 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
1624 status
= set_current_event_notification(notit
);
1625 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
1629 notit
->event
= bt_notification_event_borrow_event(notit
->event_notif
);
1630 BT_ASSERT(notit
->event
);
1632 if (notit
->event_header_field
) {
1635 BT_ASSERT(notit
->event
);
1636 ret
= bt_event_move_header(notit
->event
,
1637 notit
->event_header_field
);
1640 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1644 notit
->event_header_field
= NULL
;
1647 * At this point notit->dscopes.stream_event_header has
1648 * the same value as the event header field within
1651 BT_ASSERT(bt_event_borrow_header(notit
->event
) ==
1652 notit
->dscopes
.stream_event_header
);
1655 notit
->state
= STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
;
1662 enum bt_notif_iter_status
read_stream_event_context_begin_state(
1663 struct bt_notif_iter
*notit
)
1665 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1666 struct bt_field_type
*stream_event_context_type
;
1668 stream_event_context_type
=
1669 bt_stream_class_borrow_event_context_field_type(
1670 notit
->meta
.stream_class
);
1671 if (!stream_event_context_type
) {
1672 notit
->state
= STATE_DSCOPE_EVENT_CONTEXT_BEGIN
;
1676 BT_ASSERT(!notit
->dscopes
.stream_event_context
);
1677 notit
->dscopes
.stream_event_context
=
1678 bt_event_borrow_stream_event_context(notit
->event
);
1679 BT_ASSERT(notit
->dscopes
.stream_event_context
);
1680 BT_LOGV("Decoding stream event context field: "
1681 "notit-addr=%p, stream-class-addr=%p, "
1682 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1684 notit
, notit
->meta
.stream_class
,
1685 bt_stream_class_get_name(notit
->meta
.stream_class
),
1686 bt_stream_class_get_id(notit
->meta
.stream_class
),
1687 stream_event_context_type
);
1688 status
= read_dscope_begin_state(notit
, stream_event_context_type
,
1689 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
,
1690 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
,
1691 notit
->dscopes
.stream_event_context
);
1693 BT_LOGW("Cannot decode stream event context field: "
1694 "notit-addr=%p, stream-class-addr=%p, "
1695 "stream-class-name=\"%s\", "
1696 "stream-class-id=%" PRId64
", ft-addr=%p",
1697 notit
, notit
->meta
.stream_class
,
1698 bt_stream_class_get_name(notit
->meta
.stream_class
),
1699 bt_stream_class_get_id(notit
->meta
.stream_class
),
1700 stream_event_context_type
);
1708 enum bt_notif_iter_status
read_stream_event_context_continue_state(
1709 struct bt_notif_iter
*notit
)
1711 return read_dscope_continue_state(notit
,
1712 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
);
1716 enum bt_notif_iter_status
read_event_context_begin_state(
1717 struct bt_notif_iter
*notit
)
1719 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1720 struct bt_field_type
*event_context_type
;
1722 event_context_type
= bt_event_class_borrow_context_field_type(
1723 notit
->meta
.event_class
);
1724 if (!event_context_type
) {
1725 notit
->state
= STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
;
1729 BT_ASSERT(!notit
->dscopes
.event_context
);
1730 notit
->dscopes
.event_context
= bt_event_borrow_context(notit
->event
);
1731 BT_ASSERT(notit
->dscopes
.event_context
);
1732 BT_LOGV("Decoding event context field: "
1733 "notit-addr=%p, event-class-addr=%p, "
1734 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1736 notit
, notit
->meta
.event_class
,
1737 bt_event_class_get_name(notit
->meta
.event_class
),
1738 bt_event_class_get_id(notit
->meta
.event_class
),
1739 event_context_type
);
1740 status
= read_dscope_begin_state(notit
, event_context_type
,
1741 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
1742 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
,
1743 notit
->dscopes
.event_context
);
1745 BT_LOGW("Cannot decode event context field: "
1746 "notit-addr=%p, event-class-addr=%p, "
1747 "event-class-name=\"%s\", "
1748 "event-class-id=%" PRId64
", ft-addr=%p",
1749 notit
, notit
->meta
.event_class
,
1750 bt_event_class_get_name(notit
->meta
.event_class
),
1751 bt_event_class_get_id(notit
->meta
.event_class
),
1752 event_context_type
);
1760 enum bt_notif_iter_status
read_event_context_continue_state(
1761 struct bt_notif_iter
*notit
)
1763 return read_dscope_continue_state(notit
,
1764 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
);
1768 enum bt_notif_iter_status
read_event_payload_begin_state(
1769 struct bt_notif_iter
*notit
)
1771 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1772 struct bt_field_type
*event_payload_type
;
1774 event_payload_type
= bt_event_class_borrow_payload_field_type(
1775 notit
->meta
.event_class
);
1776 if (!event_payload_type
) {
1777 notit
->state
= STATE_EMIT_NOTIF_EVENT
;
1781 BT_ASSERT(!notit
->dscopes
.event_payload
);
1782 notit
->dscopes
.event_payload
= bt_event_borrow_payload(notit
->event
);
1783 BT_ASSERT(notit
->dscopes
.event_payload
);
1784 BT_LOGV("Decoding event payload field: "
1785 "notit-addr=%p, event-class-addr=%p, "
1786 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1788 notit
, notit
->meta
.event_class
,
1789 bt_event_class_get_name(notit
->meta
.event_class
),
1790 bt_event_class_get_id(notit
->meta
.event_class
),
1791 event_payload_type
);
1792 status
= read_dscope_begin_state(notit
, event_payload_type
,
1793 STATE_EMIT_NOTIF_EVENT
,
1794 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
1795 notit
->dscopes
.event_payload
);
1797 BT_LOGW("Cannot decode event payload field: "
1798 "notit-addr=%p, event-class-addr=%p, "
1799 "event-class-name=\"%s\", "
1800 "event-class-id=%" PRId64
", ft-addr=%p",
1801 notit
, notit
->meta
.event_class
,
1802 bt_event_class_get_name(notit
->meta
.event_class
),
1803 bt_event_class_get_id(notit
->meta
.event_class
),
1804 event_payload_type
);
1812 enum bt_notif_iter_status
read_event_payload_continue_state(
1813 struct bt_notif_iter
*notit
)
1815 return read_dscope_continue_state(notit
, STATE_EMIT_NOTIF_EVENT
);
1819 enum bt_notif_iter_status
skip_packet_padding_state(
1820 struct bt_notif_iter
*notit
)
1822 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1823 size_t bits_to_skip
;
1825 BT_ASSERT(notit
->cur_packet_size
> 0);
1826 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1827 if (bits_to_skip
== 0) {
1828 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1831 size_t bits_to_consume
;
1833 BT_LOGV("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu",
1834 bits_to_skip
, notit
, bits_to_skip
);
1835 status
= buf_ensure_available_bits(notit
);
1836 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
1840 bits_to_consume
= MIN(buf_available_bits(notit
), bits_to_skip
);
1841 BT_LOGV("Skipping %zu bits of padding: notit-addr=%p, size=%zu",
1842 bits_to_consume
, notit
, bits_to_consume
);
1843 buf_consume_bits(notit
, bits_to_consume
);
1844 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1845 if (bits_to_skip
== 0) {
1846 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1856 enum bt_notif_iter_status
handle_state(struct bt_notif_iter
*notit
)
1858 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1859 const enum state state
= notit
->state
;
1861 BT_LOGV("Handling state: notit-addr=%p, state=%s",
1862 notit
, state_string(state
));
1864 // TODO: optimalize!
1867 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1869 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
1870 status
= read_packet_header_begin_state(notit
);
1872 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
1873 status
= read_packet_header_continue_state(notit
);
1875 case STATE_AFTER_TRACE_PACKET_HEADER
:
1876 status
= after_packet_header_state(notit
);
1878 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
1879 status
= read_packet_context_begin_state(notit
);
1881 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
1882 status
= read_packet_context_continue_state(notit
);
1884 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
1885 status
= after_packet_context_state(notit
);
1887 case STATE_EMIT_NOTIF_NEW_STREAM
:
1888 notit
->state
= STATE_EMIT_NOTIF_NEW_PACKET
;
1890 case STATE_EMIT_NOTIF_NEW_PACKET
:
1891 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1893 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
:
1894 status
= read_event_header_begin_state(notit
);
1896 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
:
1897 status
= read_event_header_continue_state(notit
);
1899 case STATE_AFTER_STREAM_EVENT_HEADER
:
1900 status
= after_event_header_state(notit
);
1902 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
:
1903 status
= read_stream_event_context_begin_state(notit
);
1905 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
:
1906 status
= read_stream_event_context_continue_state(notit
);
1908 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN
:
1909 status
= read_event_context_begin_state(notit
);
1911 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
:
1912 status
= read_event_context_continue_state(notit
);
1914 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
1915 status
= read_event_payload_begin_state(notit
);
1917 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
1918 status
= read_event_payload_continue_state(notit
);
1920 case STATE_EMIT_NOTIF_EVENT
:
1921 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1923 case STATE_SKIP_PACKET_PADDING
:
1924 status
= skip_packet_padding_state(notit
);
1926 case STATE_EMIT_NOTIF_END_OF_PACKET
:
1927 notit
->state
= STATE_SKIP_PACKET_PADDING
;
1930 BT_LOGD("Unknown CTF plugin notification iterator state: "
1931 "notit-addr=%p, state=%d", notit
, notit
->state
);
1935 BT_LOGV("Handled state: notit-addr=%p, status=%s, "
1936 "prev-state=%s, cur-state=%s",
1937 notit
, bt_notif_iter_status_string(status
),
1938 state_string(state
), state_string(notit
->state
));
1943 * Resets the internal state of a CTF notification iterator.
1946 void bt_notif_iter_reset(struct bt_notif_iter
*notit
)
1949 BT_LOGD("Resetting notification iterator: addr=%p", notit
);
1950 stack_clear(notit
->stack
);
1951 notit
->meta
.stream_class
= NULL
;
1952 notit
->meta
.event_class
= NULL
;
1953 BT_PUT(notit
->packet
);
1954 BT_PUT(notit
->stream
);
1955 BT_PUT(notit
->event_notif
);
1956 release_all_dscopes(notit
);
1957 notit
->cur_dscope_field
= NULL
;
1959 if (notit
->packet_header_field
) {
1960 bt_packet_header_field_release(notit
->packet_header_field
);
1961 notit
->packet_header_field
= NULL
;
1964 if (notit
->packet_context_field
) {
1965 bt_packet_context_field_release(notit
->packet_context_field
);
1966 notit
->packet_context_field
= NULL
;
1969 if (notit
->event_header_field
) {
1970 bt_event_header_field_release(notit
->event_header_field
);
1971 notit
->event_header_field
= NULL
;
1974 notit
->buf
.addr
= NULL
;
1977 notit
->buf
.last_eh_at
= SIZE_MAX
;
1978 notit
->buf
.packet_offset
= 0;
1979 notit
->state
= STATE_INIT
;
1980 notit
->cur_content_size
= -1;
1981 notit
->cur_packet_size
= -1;
1982 notit
->cur_packet_offset
= -1;
1983 notit
->stream_begin_emitted
= false;
1984 notit
->cur_timestamp_end
= NULL
;
1988 int bt_notif_iter_switch_packet(struct bt_notif_iter
*notit
)
1993 * We don't put the stream class here because we need to make
1994 * sure that all the packets processed by the same notification
1995 * iterator refer to the same stream class (the first one).
1999 if (notit
->cur_packet_size
!= -1) {
2000 notit
->cur_packet_offset
+= notit
->cur_packet_size
;
2003 BT_LOGV("Switching packet: notit-addr=%p, cur=%zu, "
2004 "packet-offset=%" PRId64
, notit
, notit
->buf
.at
,
2005 notit
->cur_packet_offset
);
2006 stack_clear(notit
->stack
);
2007 notit
->meta
.event_class
= NULL
;
2008 BT_PUT(notit
->packet
);
2009 BT_PUT(notit
->event_notif
);
2010 notit
->cur_timestamp_end
= NULL
;
2011 release_all_dscopes(notit
);
2012 notit
->cur_dscope_field
= NULL
;
2015 * Adjust current buffer so that addr points to the beginning of the new
2018 if (notit
->buf
.addr
) {
2019 size_t consumed_bytes
= (size_t) (notit
->buf
.at
/ CHAR_BIT
);
2021 /* Packets are assumed to start on a byte frontier. */
2022 if (notit
->buf
.at
% CHAR_BIT
) {
2023 BT_LOGW("Cannot switch packet: current position is not a multiple of 8: "
2024 "notit-addr=%p, cur=%zu", notit
, notit
->buf
.at
);
2029 notit
->buf
.addr
+= consumed_bytes
;
2030 notit
->buf
.sz
-= consumed_bytes
;
2032 notit
->buf
.packet_offset
= 0;
2033 BT_LOGV("Adjusted buffer: addr=%p, size=%zu",
2034 notit
->buf
.addr
, notit
->buf
.sz
);
2037 notit
->cur_content_size
= -1;
2038 notit
->cur_packet_size
= -1;
2039 notit
->cur_sc_field_path_cache
= NULL
;
2046 struct bt_field
*borrow_next_field(struct bt_notif_iter
*notit
)
2048 struct bt_field
*next_field
= NULL
;
2049 struct bt_field
*base_field
;
2050 struct bt_field_type
*base_type
;
2053 BT_ASSERT(!stack_empty(notit
->stack
));
2054 index
= stack_top(notit
->stack
)->index
;
2055 base_field
= stack_top(notit
->stack
)->base
;
2056 BT_ASSERT(base_field
);
2057 base_type
= bt_field_borrow_type(base_field
);
2058 BT_ASSERT(base_type
);
2060 switch (bt_field_type_get_type_id(base_type
)) {
2061 case BT_FIELD_TYPE_ID_STRUCT
:
2063 next_field
= bt_field_structure_borrow_field_by_index(
2067 case BT_FIELD_TYPE_ID_ARRAY
:
2068 next_field
= bt_field_array_borrow_field(base_field
, index
);
2070 case BT_FIELD_TYPE_ID_SEQUENCE
:
2071 next_field
= bt_field_sequence_borrow_field(base_field
, index
);
2073 case BT_FIELD_TYPE_ID_VARIANT
:
2074 next_field
= bt_field_variant_borrow_current_field(base_field
);
2077 BT_LOGF("Unknown base field type ID: "
2078 "notit-addr=%p, ft-addr=%p, ft-id=%s",
2080 bt_common_field_type_id_string(
2081 bt_field_type_get_type_id(base_type
)));
2089 void update_clock_state(uint64_t *state
, struct bt_field
*value_field
,
2090 struct bt_field_type
*value_type
)
2092 uint64_t requested_new_value
;
2093 uint64_t requested_new_value_mask
;
2094 uint64_t cur_value_masked
;
2095 int requested_new_value_size
;
2098 BT_ASSERT(value_type
);
2099 BT_ASSERT(bt_field_type_is_integer(value_type
));
2100 requested_new_value_size
=
2101 bt_field_type_integer_get_size(value_type
);
2102 BT_ASSERT(requested_new_value_size
> 0);
2103 ret
= bt_field_integer_unsigned_get_value(value_field
,
2104 &requested_new_value
);
2108 * Special case for a 64-bit new value, which is the limit
2109 * of a clock value as of this version: overwrite the
2110 * current value directly.
2112 if (requested_new_value_size
== 64) {
2113 *state
= requested_new_value
;
2117 requested_new_value_mask
= (1ULL << requested_new_value_size
) - 1;
2118 cur_value_masked
= *state
& requested_new_value_mask
;
2120 if (requested_new_value
< cur_value_masked
) {
2122 * It looks like a wrap happened on the number of bits
2123 * of the requested new value. Assume that the clock
2124 * value wrapped only one time.
2126 *state
+= requested_new_value_mask
+ 1;
2129 /* Clear the low bits of the current clock value. */
2130 *state
&= ~requested_new_value_mask
;
2132 /* Set the low bits of the current clock value. */
2133 *state
|= requested_new_value
;
2136 BT_LOGV("Updated clock's value from integer field's value: "
2137 "value=%" PRIu64
, *state
);
2141 enum bt_btr_status
update_clock(struct bt_notif_iter
*notit
,
2142 struct bt_field
*int_field
)
2144 gboolean clock_class_found
;
2145 uint64_t *clock_state
= NULL
;
2146 struct bt_field_type
*int_field_type
= NULL
;
2147 enum bt_btr_status ret
= BT_BTR_STATUS_OK
;
2148 struct bt_clock_class
*clock_class
= NULL
;
2150 int_field_type
= bt_field_borrow_type(int_field
);
2151 BT_ASSERT(int_field_type
);
2153 if (bt_field_type_is_enumeration(int_field_type
)) {
2155 bt_field_type_enumeration_borrow_container_field_type(
2159 clock_class
= bt_field_type_integer_borrow_mapped_clock_class(
2161 if (likely(!clock_class
)) {
2165 clock_class_found
= g_hash_table_lookup_extended(notit
->clock_states
,
2166 clock_class
, NULL
, (gpointer
) &clock_state
);
2167 if (!clock_class_found
) {
2168 clock_state
= g_new0(uint64_t, 1);
2170 BT_LOGE_STR("Failed to allocate a uint64_t.");
2171 ret
= BT_BTR_STATUS_ENOMEM
;
2175 g_hash_table_insert(notit
->clock_states
, clock_class
,
2179 /* Update the clock's state. */
2180 BT_LOGV("Updating notification iterator's clock's value from integer field: "
2181 "notit-addr=%p, clock-class-addr=%p, "
2182 "clock-class-name=\"%s\", value=%" PRIu64
,
2184 bt_clock_class_get_name(clock_class
), *clock_state
);
2185 update_clock_state(clock_state
, int_field
, int_field_type
);
2192 enum bt_btr_status
btr_unsigned_int_common(uint64_t value
,
2193 struct bt_field_type
*type
, void *data
,
2194 struct bt_field
**out_field
)
2196 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2197 struct bt_field
*field
= NULL
;
2198 struct bt_notif_iter
*notit
= data
;
2201 BT_LOGV("Common unsigned integer function called from BTR: "
2202 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2203 "ft-id=%s, value=%" PRIu64
,
2204 notit
, notit
->btr
, type
,
2205 bt_common_field_type_id_string(
2206 bt_field_type_get_type_id(type
)),
2208 field
= borrow_next_field(notit
);
2210 BT_LOGW("Cannot borrow next field: notit-addr=%p", notit
);
2211 status
= BT_BTR_STATUS_ERROR
;
2215 BT_ASSERT(bt_field_is_integer(field
) || bt_field_is_enumeration(field
));
2216 ret
= bt_field_integer_unsigned_set_value(field
, value
);
2217 BT_ASSERT(ret
== 0);
2218 stack_top(notit
->stack
)->index
++;
2226 enum bt_btr_status
btr_timestamp_end_cb(void *value
,
2227 struct bt_field_type
*type
, void *data
)
2229 enum bt_btr_status status
;
2230 struct bt_field
*field
= NULL
;
2231 struct bt_notif_iter
*notit
= data
;
2232 uint64_t uvalue
= *((uint64_t *) value
);
2234 BT_LOGV("`timestamp_end` unsigned integer function called from BTR: "
2235 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2237 notit
, notit
->btr
, type
,
2238 bt_common_field_type_id_string(
2239 bt_field_type_get_type_id(type
)));
2241 status
= btr_unsigned_int_common(uvalue
, type
, data
, &field
);
2243 /* Set as the current packet's end timestamp field */
2244 notit
->cur_timestamp_end
= field
;
2249 enum bt_btr_status
btr_unsigned_int_cb(uint64_t value
,
2250 struct bt_field_type
*type
, void *data
)
2252 struct bt_notif_iter
*notit
= data
;
2253 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2254 struct bt_field
*field
= NULL
;
2255 struct field_cb_override
*override
;
2257 BT_LOGV("Unsigned integer function called from BTR: "
2258 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2259 "ft-id=%s, value=%" PRIu64
,
2260 notit
, notit
->btr
, type
,
2261 bt_common_field_type_id_string(
2262 bt_field_type_get_type_id(type
)),
2264 override
= g_hash_table_lookup(notit
->field_overrides
, type
);
2265 if (unlikely(override
)) {
2266 /* Override function logs errors */
2267 status
= override
->func(&value
, type
, override
->data
);
2271 status
= btr_unsigned_int_common(value
, type
, data
, &field
);
2272 if (status
!= BT_BTR_STATUS_OK
) {
2273 /* btr_unsigned_int_common() logs errors */
2277 status
= update_clock(notit
, field
);
2284 enum bt_btr_status
btr_signed_int_cb(int64_t value
,
2285 struct bt_field_type
*type
, void *data
)
2287 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2288 struct bt_field
*field
= NULL
;
2289 struct bt_notif_iter
*notit
= data
;
2292 BT_LOGV("Signed integer function called from BTR: "
2293 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2294 "ft-id=%s, value=%" PRId64
,
2295 notit
, notit
->btr
, type
,
2296 bt_common_field_type_id_string(
2297 bt_field_type_get_type_id(type
)),
2299 field
= borrow_next_field(notit
);
2301 BT_LOGW("Cannot borrow next field: notit-addr=%p", notit
);
2302 status
= BT_BTR_STATUS_ERROR
;
2306 BT_ASSERT(bt_field_is_integer(field
) || bt_field_is_enumeration(field
));
2307 ret
= bt_field_integer_signed_set_value(field
, value
);
2308 BT_ASSERT(ret
== 0);
2309 stack_top(notit
->stack
)->index
++;
2316 enum bt_btr_status
btr_floating_point_cb(double value
,
2317 struct bt_field_type
*type
, void *data
)
2319 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2320 struct bt_field
*field
= NULL
;
2321 struct bt_notif_iter
*notit
= data
;
2324 BT_LOGV("Floating point number function called from BTR: "
2325 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2326 "ft-id=%s, value=%f",
2327 notit
, notit
->btr
, type
,
2328 bt_common_field_type_id_string(
2329 bt_field_type_get_type_id(type
)),
2331 field
= borrow_next_field(notit
);
2333 BT_LOGW("Cannot borrow next field: notit-addr=%p", notit
);
2334 status
= BT_BTR_STATUS_ERROR
;
2338 ret
= bt_field_floating_point_set_value(field
, value
);
2340 stack_top(notit
->stack
)->index
++;
2347 enum bt_btr_status
btr_string_begin_cb(
2348 struct bt_field_type
*type
, void *data
)
2350 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2351 struct bt_field
*field
= NULL
;
2352 struct bt_notif_iter
*notit
= data
;
2355 BT_LOGV("String (beginning) function called from BTR: "
2356 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2358 notit
, notit
->btr
, type
,
2359 bt_common_field_type_id_string(
2360 bt_field_type_get_type_id(type
)));
2361 field
= borrow_next_field(notit
);
2363 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2364 status
= BT_BTR_STATUS_ERROR
;
2368 ret
= bt_field_string_clear(field
);
2369 BT_ASSERT(ret
== 0);
2372 * Push on stack. Not a compound type per se, but we know that only
2373 * btr_string_cb() may be called between this call and a subsequent
2374 * call to btr_string_end_cb().
2376 ret
= stack_push(notit
->stack
, field
);
2378 BT_LOGE("Cannot push string field on stack: "
2379 "notit-addr=%p, field-addr=%p", notit
, field
);
2380 status
= BT_BTR_STATUS_ERROR
;
2389 enum bt_btr_status
btr_string_cb(const char *value
,
2390 size_t len
, struct bt_field_type
*type
, void *data
)
2392 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2393 struct bt_field
*field
= NULL
;
2394 struct bt_notif_iter
*notit
= data
;
2397 BT_LOGV("String (substring) function called from BTR: "
2398 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2399 "ft-id=%s, string-length=%zu",
2400 notit
, notit
->btr
, type
,
2401 bt_common_field_type_id_string(
2402 bt_field_type_get_type_id(type
)),
2404 field
= stack_top(notit
->stack
)->base
;
2407 /* Append current substring */
2408 ret
= bt_field_string_append_len(field
, value
, len
);
2410 BT_LOGE("Cannot append substring to string field's value: "
2411 "notit-addr=%p, field-addr=%p, string-length=%zu, "
2412 "ret=%d", notit
, field
, len
, ret
);
2413 status
= BT_BTR_STATUS_ERROR
;
2422 enum bt_btr_status
btr_string_end_cb(
2423 struct bt_field_type
*type
, void *data
)
2425 struct bt_notif_iter
*notit
= data
;
2427 BT_LOGV("String (end) function called from BTR: "
2428 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2430 notit
, notit
->btr
, type
,
2431 bt_common_field_type_id_string(
2432 bt_field_type_get_type_id(type
)));
2434 /* Pop string field */
2435 stack_pop(notit
->stack
);
2437 /* Go to next field */
2438 stack_top(notit
->stack
)->index
++;
2439 return BT_BTR_STATUS_OK
;
2442 enum bt_btr_status
btr_compound_begin_cb(
2443 struct bt_field_type
*type
, void *data
)
2445 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2446 struct bt_notif_iter
*notit
= data
;
2447 struct bt_field
*field
;
2450 BT_LOGV("Compound (beginning) function called from BTR: "
2451 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2453 notit
, notit
->btr
, type
,
2454 bt_common_field_type_id_string(
2455 bt_field_type_get_type_id(type
)));
2458 if (stack_empty(notit
->stack
)) {
2459 /* Root: already set by read_dscope_begin_state() */
2460 field
= notit
->cur_dscope_field
;
2462 field
= borrow_next_field(notit
);
2464 BT_LOGW("Cannot borrow next field: notit-addr=%p", notit
);
2465 status
= BT_BTR_STATUS_ERROR
;
2472 ret
= stack_push(notit
->stack
, field
);
2474 BT_LOGE("Cannot push compound field onto the stack: "
2475 "notit-addr=%p, ft-addr=%p, ft-id=%s, ret=%d",
2477 bt_common_field_type_id_string(
2478 bt_field_type_get_type_id(type
)),
2480 status
= BT_BTR_STATUS_ERROR
;
2488 enum bt_btr_status
btr_compound_end_cb(
2489 struct bt_field_type
*type
, void *data
)
2491 struct bt_notif_iter
*notit
= data
;
2493 BT_LOGV("Compound (end) function called from BTR: "
2494 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2496 notit
, notit
->btr
, type
,
2497 bt_common_field_type_id_string(
2498 bt_field_type_get_type_id(type
)));
2499 BT_ASSERT(!stack_empty(notit
->stack
));
2502 stack_pop(notit
->stack
);
2504 /* If the stack is not empty, increment the base's index */
2505 if (!stack_empty(notit
->stack
)) {
2506 stack_top(notit
->stack
)->index
++;
2509 return BT_BTR_STATUS_OK
;
2513 struct bt_field
*resolve_field(struct bt_notif_iter
*notit
,
2514 struct bt_field_path
*path
)
2516 struct bt_field
*field
= NULL
;
2519 if (BT_LOG_ON_VERBOSE
) {
2520 GString
*gstr
= bt_field_path_string(path
);
2522 BT_LOGV("Resolving field path: notit-addr=%p, field-path=\"%s\"",
2523 notit
, gstr
? gstr
->str
: NULL
);
2526 g_string_free(gstr
, TRUE
);
2530 switch (bt_field_path_get_root_scope(path
)) {
2531 case BT_SCOPE_TRACE_PACKET_HEADER
:
2532 field
= notit
->dscopes
.trace_packet_header
;
2534 case BT_SCOPE_STREAM_PACKET_CONTEXT
:
2535 field
= notit
->dscopes
.stream_packet_context
;
2537 case BT_SCOPE_STREAM_EVENT_HEADER
:
2538 field
= notit
->dscopes
.stream_event_header
;
2540 case BT_SCOPE_STREAM_EVENT_CONTEXT
:
2541 field
= notit
->dscopes
.stream_event_context
;
2543 case BT_SCOPE_EVENT_CONTEXT
:
2544 field
= notit
->dscopes
.event_context
;
2546 case BT_SCOPE_EVENT_FIELDS
:
2547 field
= notit
->dscopes
.event_payload
;
2550 BT_LOGF("Cannot resolve field path: unknown scope: "
2551 "notit-addr=%p, root-scope=%s",
2552 notit
, bt_common_scope_string(
2553 bt_field_path_get_root_scope(path
)));
2558 BT_LOGW("Cannot resolve field path: root field not found: "
2559 "notit-addr=%p, root-scope=%s",
2560 notit
, bt_common_scope_string(
2561 bt_field_path_get_root_scope(path
)));
2565 for (i
= 0; i
< bt_field_path_get_index_count(path
); ++i
) {
2566 struct bt_field
*next_field
= NULL
;
2567 struct bt_field_type
*field_type
;
2568 int index
= bt_field_path_get_index(path
, i
);
2570 field_type
= bt_field_borrow_type(field
);
2571 BT_ASSERT(field_type
);
2573 if (bt_field_type_is_structure(field_type
)) {
2574 next_field
= bt_field_structure_borrow_field_by_index(
2576 } else if (bt_field_type_is_variant(field_type
)) {
2578 bt_field_variant_borrow_current_field(field
);
2584 BT_LOGW("Cannot find next field: "
2585 "notit-addr=%p, ft-addr=%p, ft-id=%s, index=%d",
2587 bt_common_field_type_id_string(
2588 bt_field_type_get_type_id(field_type
)),
2593 /* Move next field -> field */
2602 int64_t btr_get_sequence_length_cb(struct bt_field_type
*type
, void *data
)
2606 struct bt_field
*seq_field
;
2607 struct bt_field_path
*field_path
;
2608 struct bt_notif_iter
*notit
= data
;
2609 struct bt_field
*length_field
= NULL
;
2612 field_path
= bt_field_type_sequence_borrow_length_field_path(type
);
2613 BT_ASSERT(field_path
);
2614 length_field
= resolve_field(notit
, field_path
);
2615 if (!length_field
) {
2616 BT_LOGW("Cannot resolve sequence field type's length field path: "
2617 "notit-addr=%p, ft-addr=%p",
2622 iret
= bt_field_integer_unsigned_get_value(length_field
, &length
);
2624 BT_LOGE("Cannot get value of sequence length field: "
2625 "notit-addr=%p, field-addr=%p",
2626 notit
, length_field
);
2630 seq_field
= stack_top(notit
->stack
)->base
;
2631 iret
= bt_field_sequence_set_length(seq_field
, length
);
2633 BT_LOGE("Cannot set sequence field's length field: "
2634 "notit-addr=%p, seq-field-addr=%p, "
2636 notit
, seq_field
, length
);
2640 ret
= (int64_t) length
;
2647 struct bt_field_type
*btr_borrow_variant_field_type_cb(
2648 struct bt_field_type
*type
, void *data
)
2651 struct bt_field_path
*path
;
2652 struct bt_notif_iter
*notit
= data
;
2653 struct bt_field
*var_field
;
2654 struct bt_field
*tag_field
= NULL
;
2655 struct bt_field_type
*tag_ft
= NULL
;
2656 struct bt_field_type
*tag_int_ft
= NULL
;
2657 struct bt_field
*selected_field
= NULL
;
2658 struct bt_field_type
*selected_field_type
= NULL
;
2660 path
= bt_field_type_variant_borrow_tag_field_path(type
);
2662 tag_field
= resolve_field(notit
, path
);
2664 BT_LOGW("Cannot resolve variant field type's tag field path: "
2665 "notit-addr=%p, ft-addr=%p",
2671 * We found the enumeration tag field instance which should be
2672 * able to select a current field for this variant. This
2673 * callback function we're in is called _after_
2674 * compound_begin(), so the current stack top's base field is
2675 * the variant field in question. We set the variant field's tag
2676 * here and then get the current (selected) field thanks to this
2677 * tag field's value. This current field will also provide us
2678 * with its type. Then, this current field will remain the
2679 * current selected one until the next callback function call
2680 * which is used to fill it.
2682 var_field
= stack_top(notit
->stack
)->base
;
2683 tag_ft
= bt_field_borrow_type(tag_field
);
2684 tag_int_ft
= bt_field_type_enumeration_borrow_container_field_type(
2687 if (bt_field_type_integer_is_signed(tag_int_ft
)) {
2690 ret
= bt_field_integer_signed_get_value(tag_field
, &tag_value
);
2691 BT_ASSERT(ret
== 0);
2692 ret
= bt_field_variant_set_tag_signed(var_field
, tag_value
);
2693 BT_ASSERT(ret
== 0);
2697 ret
= bt_field_integer_unsigned_get_value(tag_field
,
2699 BT_ASSERT(ret
== 0);
2700 ret
= bt_field_variant_set_tag_unsigned(var_field
, tag_value
);
2701 BT_ASSERT(ret
== 0);
2704 selected_field
= bt_field_variant_borrow_current_field(var_field
);
2705 if (!selected_field
) {
2706 BT_LOGW("Cannot borrow variant field's current field: "
2707 "notit-addr=%p, var-field-addr=%p",
2712 selected_field_type
= bt_field_borrow_type(selected_field
);
2715 return selected_field_type
;
2719 int set_event_clocks(struct bt_notif_iter
*notit
)
2722 GHashTableIter iter
;
2723 struct bt_clock_class
*clock_class
;
2724 uint64_t *clock_state
;
2726 g_hash_table_iter_init(&iter
, notit
->clock_states
);
2728 while (g_hash_table_iter_next(&iter
, (gpointer
) &clock_class
,
2729 (gpointer
) &clock_state
)) {
2730 struct bt_clock_value
*clock_value
;
2732 clock_value
= bt_event_borrow_clock_value(notit
->event
,
2735 BT_LOGE("Cannot borrow clock value from event with given clock class: "
2736 "notit-addr=%p, clock-class-addr=%p, "
2737 "clock-class-name=\"%s\"",
2739 bt_clock_class_get_name(clock_class
));
2744 ret
= bt_clock_value_set_value(clock_value
, *clock_state
);
2745 BT_ASSERT(ret
== 0);
2754 void notify_new_stream(struct bt_notif_iter
*notit
,
2755 struct bt_notification
**notification
)
2757 enum bt_notif_iter_status status
;
2758 struct bt_notification
*ret
= NULL
;
2760 status
= set_current_stream(notit
);
2761 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
2766 BT_ASSERT(notit
->stream
);
2767 BT_ASSERT(notit
->graph
);
2768 ret
= bt_notification_stream_begin_create(notit
->graph
, notit
->stream
);
2770 BT_LOGE("Cannot create stream beginning notification: "
2771 "notit-addr=%p, stream-addr=%p",
2772 notit
, notit
->stream
);
2777 *notification
= ret
;
2781 void notify_end_of_stream(struct bt_notif_iter
*notit
,
2782 struct bt_notification
**notification
)
2784 struct bt_notification
*ret
;
2786 if (!notit
->stream
) {
2787 BT_LOGE("Cannot create stream for stream notification: "
2788 "notit-addr=%p", notit
);
2792 BT_ASSERT(notit
->graph
);
2793 ret
= bt_notification_stream_end_create(notit
->graph
, notit
->stream
);
2795 BT_LOGE("Cannot create stream beginning notification: "
2796 "notit-addr=%p, stream-addr=%p",
2797 notit
, notit
->stream
);
2800 *notification
= ret
;
2804 void notify_new_packet(struct bt_notif_iter
*notit
,
2805 struct bt_notification
**notification
)
2808 enum bt_notif_iter_status status
;
2809 struct bt_notification
*notif
= NULL
;
2811 status
= set_current_packet(notit
);
2812 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
2816 BT_ASSERT(notit
->packet
);
2818 if (notit
->packet_header_field
) {
2819 ret
= bt_packet_move_header(notit
->packet
,
2820 notit
->packet_header_field
);
2825 notit
->packet_header_field
= NULL
;
2828 * At this point notit->dscopes.trace_packet_header has
2829 * the same value as the packet header field within
2832 BT_ASSERT(bt_packet_borrow_header(notit
->packet
) ==
2833 notit
->dscopes
.trace_packet_header
);
2836 if (notit
->packet_context_field
) {
2837 ret
= bt_packet_move_context(notit
->packet
,
2838 notit
->packet_context_field
);
2843 notit
->packet_context_field
= NULL
;
2846 * At this point notit->dscopes.trace_packet_header has
2847 * the same value as the packet header field within
2850 BT_ASSERT(bt_packet_borrow_context(notit
->packet
) ==
2851 notit
->dscopes
.stream_packet_context
);
2854 BT_ASSERT(notit
->graph
);
2855 notif
= bt_notification_packet_begin_create(notit
->graph
,
2858 BT_LOGE("Cannot create packet beginning notification: "
2859 "notit-addr=%p, packet-addr=%p",
2860 notit
, notit
->packet
);
2865 *notification
= notif
;
2869 void notify_end_of_packet(struct bt_notif_iter
*notit
,
2870 struct bt_notification
**notification
)
2872 struct bt_notification
*notif
;
2874 if (!notit
->packet
) {
2878 BT_ASSERT(notit
->graph
);
2879 notif
= bt_notification_packet_end_create(notit
->graph
, notit
->packet
);
2881 BT_LOGE("Cannot create packet end notification: "
2882 "notit-addr=%p, packet-addr=%p",
2883 notit
, notit
->packet
);
2887 BT_PUT(notit
->packet
);
2888 *notification
= notif
;
2892 void init_trace_field_path_cache(struct bt_trace
*trace
,
2893 struct trace_field_path_cache
*trace_field_path_cache
)
2896 int stream_instance_id
= -1;
2898 struct bt_field_type
*packet_header
= NULL
;
2900 packet_header
= bt_trace_borrow_packet_header_field_type(trace
);
2901 if (!packet_header
) {
2905 if (!bt_field_type_is_structure(packet_header
)) {
2909 count
= bt_field_type_structure_get_field_count(packet_header
);
2910 BT_ASSERT(count
>= 0);
2912 for (i
= 0; (i
< count
&& (stream_id
== -1 || stream_instance_id
== -1)); i
++) {
2914 const char *field_name
;
2916 ret
= bt_field_type_structure_borrow_field_by_index(
2917 packet_header
, &field_name
, NULL
, i
);
2919 BT_LOGE("Cannot get structure field's field: "
2920 "field-addr=%p, index=%d",
2925 if (stream_id
== -1 && !strcmp(field_name
, "stream_id")) {
2927 } else if (stream_instance_id
== -1 &&
2928 !strcmp(field_name
, "stream_instance_id")) {
2929 stream_instance_id
= i
;
2934 trace_field_path_cache
->stream_id
= stream_id
;
2935 trace_field_path_cache
->stream_instance_id
= stream_instance_id
;
2939 struct bt_notif_iter
*bt_notif_iter_create(struct bt_trace
*trace
,
2940 size_t max_request_sz
,
2941 struct bt_notif_iter_medium_ops medops
, void *data
)
2943 struct bt_notif_iter
*notit
= NULL
;
2944 struct bt_btr_cbs cbs
= {
2946 .signed_int
= btr_signed_int_cb
,
2947 .unsigned_int
= btr_unsigned_int_cb
,
2948 .floating_point
= btr_floating_point_cb
,
2949 .string_begin
= btr_string_begin_cb
,
2950 .string
= btr_string_cb
,
2951 .string_end
= btr_string_end_cb
,
2952 .compound_begin
= btr_compound_begin_cb
,
2953 .compound_end
= btr_compound_end_cb
,
2956 .get_sequence_length
= btr_get_sequence_length_cb
,
2957 .borrow_variant_field_type
= btr_borrow_variant_field_type_cb
,
2962 BT_ASSERT(medops
.request_bytes
);
2963 BT_ASSERT(medops
.borrow_stream
);
2964 BT_LOGD("Creating CTF plugin notification iterator: "
2965 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
2967 trace
, bt_trace_get_name(trace
), max_request_sz
, data
);
2968 notit
= g_new0(struct bt_notif_iter
, 1);
2970 BT_LOGE_STR("Failed to allocate one CTF plugin notification iterator.");
2973 notit
->clock_states
= g_hash_table_new_full(g_direct_hash
,
2974 g_direct_equal
, NULL
, g_free
);
2975 if (!notit
->clock_states
) {
2976 BT_LOGE_STR("Failed to allocate a GHashTable.");
2979 notit
->meta
.trace
= trace
;
2980 notit
->medium
.medops
= medops
;
2981 notit
->medium
.max_request_sz
= max_request_sz
;
2982 notit
->medium
.data
= data
;
2983 notit
->stack
= stack_new(notit
);
2984 if (!notit
->stack
) {
2985 BT_LOGE_STR("Failed to create field stack.");
2989 notit
->btr
= bt_btr_create(cbs
, notit
);
2991 BT_LOGE_STR("Failed to create binary type reader (BTR).");
2995 bt_notif_iter_reset(notit
);
2996 init_trace_field_path_cache(trace
, ¬it
->trace_field_path_cache
);
2997 notit
->sc_field_path_caches
= g_hash_table_new_full(g_direct_hash
,
2998 g_direct_equal
, NULL
, g_free
);
2999 if (!notit
->sc_field_path_caches
) {
3000 BT_LOGE_STR("Failed to allocate a GHashTable.");
3004 notit
->field_overrides
= g_hash_table_new_full(g_direct_hash
,
3005 g_direct_equal
, NULL
, g_free
);
3006 if (!notit
->field_overrides
) {
3007 BT_LOGE_STR("Failed to allocate a GHashTable.");
3011 BT_LOGD("Created CTF plugin notification iterator: "
3012 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
3013 "data=%p, notit-addr=%p",
3014 trace
, bt_trace_get_name(trace
), max_request_sz
, data
,
3016 notit
->cur_packet_offset
= 0;
3022 bt_notif_iter_destroy(notit
);
3027 void bt_notif_iter_destroy(struct bt_notif_iter
*notit
)
3029 BT_PUT(notit
->packet
);
3030 BT_PUT(notit
->stream
);
3031 BT_PUT(notit
->meta
.cc_prio_map
);
3032 release_all_dscopes(notit
);
3034 BT_LOGD("Destroying CTF plugin notification iterator: addr=%p", notit
);
3037 BT_LOGD_STR("Destroying field stack.");
3038 stack_destroy(notit
->stack
);
3042 BT_LOGD("Destroying BTR: btr-addr=%p", notit
->btr
);
3043 bt_btr_destroy(notit
->btr
);
3046 if (notit
->clock_states
) {
3047 g_hash_table_destroy(notit
->clock_states
);
3050 if (notit
->sc_field_path_caches
) {
3051 g_hash_table_destroy(notit
->sc_field_path_caches
);
3054 if (notit
->field_overrides
) {
3055 g_hash_table_destroy(notit
->field_overrides
);
3061 enum bt_notif_iter_status
bt_notif_iter_get_next_notification(
3062 struct bt_notif_iter
*notit
,
3063 struct bt_clock_class_priority_map
*cc_prio_map
,
3064 struct bt_graph
*graph
,
3065 struct bt_notification
**notification
)
3068 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
3071 BT_ASSERT(notification
);
3073 if (notit
->state
== STATE_DONE
) {
3074 status
= BT_NOTIF_ITER_STATUS_EOF
;
3078 if (cc_prio_map
!= notit
->meta
.cc_prio_map
) {
3079 bt_put(notit
->meta
.cc_prio_map
);
3080 notit
->meta
.cc_prio_map
= bt_get(cc_prio_map
);
3083 notit
->graph
= graph
;
3085 BT_LOGV("Getting next notification: notit-addr=%p, cc-prio-map-addr=%p",
3086 notit
, cc_prio_map
);
3089 status
= handle_state(notit
);
3090 if (status
== BT_NOTIF_ITER_STATUS_AGAIN
) {
3091 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_AGAIN.");
3095 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
3096 if (status
== BT_NOTIF_ITER_STATUS_EOF
) {
3097 enum state next_state
= notit
->state
;
3099 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_EOF.");
3101 if (notit
->packet
) {
3102 notify_end_of_packet(notit
,
3105 notify_end_of_stream(notit
,
3107 next_state
= STATE_DONE
;
3110 if (!*notification
) {
3111 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3115 status
= BT_NOTIF_ITER_STATUS_OK
;
3116 notit
->state
= next_state
;
3118 BT_LOGW("Cannot handle state: "
3119 "notit-addr=%p, state=%s",
3120 notit
, state_string(notit
->state
));
3126 switch (notit
->state
) {
3127 case STATE_EMIT_NOTIF_NEW_STREAM
:
3128 /* notify_new_stream() logs errors */
3129 notify_new_stream(notit
, notification
);
3130 if (!*notification
) {
3131 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3133 notit
->stream_begin_emitted
= true;
3135 case STATE_EMIT_NOTIF_NEW_PACKET
:
3136 /* notify_new_packet() logs errors */
3137 notify_new_packet(notit
, notification
);
3138 if (!*notification
) {
3139 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3143 case STATE_EMIT_NOTIF_EVENT
:
3144 BT_ASSERT(notit
->event_notif
);
3145 ret
= set_event_clocks(notit
);
3147 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3151 BT_MOVE(*notification
, notit
->event_notif
);
3153 case STATE_EMIT_NOTIF_END_OF_PACKET
:
3154 /* Update clock with timestamp_end field. */
3155 if (notit
->cur_timestamp_end
) {
3156 enum bt_btr_status btr_status
;
3158 btr_status
= update_clock(notit
,
3159 notit
->cur_timestamp_end
);
3160 if (btr_status
!= BT_BTR_STATUS_OK
) {
3161 BT_LOGW("Cannot update stream's clock value: "
3162 "notit-addr=%p", notit
);
3163 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3168 /* notify_end_of_packet() logs errors */
3169 notify_end_of_packet(notit
, notification
);
3170 if (!*notification
) {
3171 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3176 /* Non-emitting state: continue */
3186 enum bt_notif_iter_status
bt_notif_iter_borrow_packet_header_context_fields(
3187 struct bt_notif_iter
*notit
,
3188 struct bt_field
**packet_header_field
,
3189 struct bt_field
**packet_context_field
)
3192 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
3196 if (notit
->state
== STATE_EMIT_NOTIF_NEW_PACKET
) {
3197 /* We're already there */
3202 status
= handle_state(notit
);
3203 if (status
== BT_NOTIF_ITER_STATUS_AGAIN
) {
3204 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_AGAIN.");
3207 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
3208 if (status
== BT_NOTIF_ITER_STATUS_EOF
) {
3209 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_EOF.");
3211 BT_LOGW("Cannot handle state: "
3212 "notit-addr=%p, state=%s",
3213 notit
, state_string(notit
->state
));
3218 switch (notit
->state
) {
3219 case STATE_EMIT_NOTIF_NEW_PACKET
:
3221 * Packet header and context fields are
3222 * potentially decoded (or they don't exist).
3226 case STATE_EMIT_NOTIF_NEW_STREAM
:
3227 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
3228 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
3229 case STATE_AFTER_TRACE_PACKET_HEADER
:
3230 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
3231 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
3232 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
3233 /* Non-emitting state: continue */
3237 * We should never get past the
3238 * STATE_EMIT_NOTIF_NEW_PACKET state.
3240 BT_LOGF("Unexpected state: notit-addr=%p, state=%s",
3241 notit
, state_string(notit
->state
));
3247 ret
= set_current_packet_content_sizes(notit
);
3249 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3253 if (packet_header_field
) {
3254 *packet_header_field
= notit
->dscopes
.trace_packet_header
;
3257 if (packet_context_field
) {
3258 *packet_context_field
= notit
->dscopes
.stream_packet_context
;
3266 void bt_notif_iter_set_medops_data(struct bt_notif_iter
*notit
,
3270 notit
->medium
.data
= medops_data
;
3274 enum bt_notif_iter_status
bt_notif_iter_seek(
3275 struct bt_notif_iter
*notit
, off_t offset
)
3277 enum bt_notif_iter_status ret
= BT_NOTIF_ITER_STATUS_OK
;
3278 enum bt_notif_iter_medium_status medium_status
;
3282 BT_LOGE("Cannot seek to negative offset: offset=%jd", offset
);
3283 ret
= BT_NOTIF_ITER_STATUS_INVAL
;
3287 if (!notit
->medium
.medops
.seek
) {
3288 ret
= BT_NOTIF_ITER_STATUS_UNSUPPORTED
;
3289 BT_LOGD("Aborting seek as the iterator's underlying media does not implement seek support.");
3293 medium_status
= notit
->medium
.medops
.seek(
3294 BT_NOTIF_ITER_SEEK_WHENCE_SET
, offset
,
3295 notit
->medium
.data
);
3296 if (medium_status
!= BT_NOTIF_ITER_MEDIUM_STATUS_OK
) {
3297 if (medium_status
== BT_NOTIF_ITER_MEDIUM_STATUS_EOF
) {
3298 ret
= BT_NOTIF_ITER_STATUS_EOF
;
3300 ret
= BT_NOTIF_ITER_STATUS_ERROR
;
3305 bt_notif_iter_reset(notit
);
3306 notit
->cur_packet_offset
= offset
;
3312 off_t
bt_notif_iter_get_current_packet_offset(
3313 struct bt_notif_iter
*notit
)
3316 return notit
->cur_packet_offset
;
3320 off_t
bt_notif_iter_get_current_packet_size(
3321 struct bt_notif_iter
*notit
)
3324 return notit
->cur_packet_size
;