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) */
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_STREAM_EVENT_HEADER_BEGIN
,
86 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
,
87 STATE_AFTER_STREAM_EVENT_HEADER
,
88 STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
,
89 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
,
90 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
,
91 STATE_DSCOPE_EVENT_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 struct trace_field_path_cache
{
102 * Indexes of the stream_id and stream_instance_id field in the packet
103 * header structure, -1 if unset.
106 int stream_instance_id
;
109 struct stream_class_field_path_cache
{
111 * Indexes of the v and id fields in the stream event header structure,
118 * index of the timestamp_end, packet_size and content_size fields in
119 * the stream packet context structure. Set to -1 if the fields were
127 struct field_cb_override
{
128 enum bt_btr_status (* func
)(void *value
,
129 struct bt_field_type
*type
, void *data
);
133 /* CTF notification iterator */
134 struct bt_notif_iter
{
138 /* Current graph to create notifications (weak) */
139 struct bt_graph
*graph
;
142 * Current dynamic scope field pointer.
144 * This is set by read_dscope_begin_state() and contains the
145 * value of one of the pointers in `dscopes` below.
147 struct bt_field
*cur_dscope_field
;
149 /* Trace and classes (owned by this) */
151 struct bt_trace
*trace
;
152 struct bt_stream_class
*stream_class
;
153 struct bt_event_class
*event_class
;
154 struct bt_clock_class_priority_map
*cc_prio_map
;
157 /* Current packet header field wrapper (NULL if not created yet) */
158 struct bt_packet_header_field
*packet_header_field
;
160 /* Current packet header field wrapper (NULL if not created yet) */
161 struct bt_packet_context_field
*packet_context_field
;
163 /* Current event header field (NULL if not created yet) */
164 struct bt_event_header_field
*event_header_field
;
166 /* Current packet (NULL if not created yet) */
167 struct bt_packet
*packet
;
169 /* Current stream (NULL if not set yet) */
170 struct bt_stream
*stream
;
172 /* Current event (NULL if not created yet) */
173 struct bt_event
*event
;
175 /* Current event notification (NULL if not created yet) */
176 struct bt_notification
*event_notif
;
179 * Current `timestamp_end` field (to consider before switching
180 * packets). If it's set, it's a field which is within
181 * `dscopes.stream_packet_context` below, which is in `packet`
184 struct bt_field
*cur_timestamp_end
;
186 /* Database of current dynamic scopes (owned by this) */
188 struct bt_field
*trace_packet_header
;
189 struct bt_field
*stream_packet_context
;
190 struct bt_field
*stream_event_header
;
191 struct bt_field
*stream_event_context
;
192 struct bt_field
*event_context
;
193 struct bt_field
*event_payload
;
197 * Special field overrides.
199 * Overrides are used to implement the behaviours of special fields such
200 * as "timestamp_end" (which must be ignored until the end of the
201 * packet), "id" (event id) which can be present multiple times and must
202 * be updated multiple time.
204 * This should be used to implement the behaviour of integer fields
205 * mapped to clocks and other "tagged" fields (in CTF 2).
207 * bt_field_type to struct field_cb_override
209 GHashTable
*field_overrides
;
214 /* Current medium buffer data */
216 /* Last address provided by medium */
219 /* Buffer size provided by medium (bytes) */
222 /* Offset within whole packet of addr (bits) */
223 size_t packet_offset
;
225 /* Current position from addr (bits) */
228 /* Position of the last event header from addr (bits) */
232 /* Binary type reader */
235 /* Current medium data */
237 struct bt_notif_iter_medium_ops medops
;
238 size_t max_request_sz
;
242 /* Stream beginning was emitted */
243 bool stream_begin_emitted
;
245 /* Current packet size (bits) (-1 if unknown) */
246 int64_t cur_packet_size
;
248 /* Current content size (bits) (-1 if unknown) */
249 int64_t cur_content_size
;
252 * Offset, in the underlying media, of the current packet's start
255 off_t cur_packet_offset
;
257 /* bt_clock_class to uint64_t. */
258 GHashTable
*clock_states
;
261 * Cache of the trace-constant field paths (event header type)
262 * associated to the current trace.
264 struct trace_field_path_cache trace_field_path_cache
;
267 * Field path cache associated with the current stream class.
268 * Ownership of this structure belongs to the field_path_caches HT.
270 struct stream_class_field_path_cache
*cur_sc_field_path_cache
;
272 /* bt_stream_class to struct stream_class_field_path_cache. */
273 GHashTable
*sc_field_path_caches
;
277 const char *state_string(enum state state
)
282 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
283 return "STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN";
284 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
285 return "STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE";
286 case STATE_AFTER_TRACE_PACKET_HEADER
:
287 return "STATE_AFTER_TRACE_PACKET_HEADER";
288 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
289 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN";
290 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
291 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE";
292 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
293 return "STATE_AFTER_STREAM_PACKET_CONTEXT";
294 case STATE_EMIT_NOTIF_NEW_PACKET
:
295 return "STATE_EMIT_NOTIF_NEW_PACKET";
296 case STATE_EMIT_NOTIF_NEW_STREAM
:
297 return "STATE_EMIT_NOTIF_NEW_STREAM";
298 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
:
299 return "STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN";
300 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
:
301 return "STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE";
302 case STATE_AFTER_STREAM_EVENT_HEADER
:
303 return "STATE_AFTER_STREAM_EVENT_HEADER";
304 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
:
305 return "STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN";
306 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
:
307 return "STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE";
308 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN
:
309 return "STATE_DSCOPE_EVENT_CONTEXT_BEGIN";
310 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
:
311 return "STATE_DSCOPE_EVENT_CONTEXT_CONTINUE";
312 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
313 return "STATE_DSCOPE_EVENT_PAYLOAD_BEGIN";
314 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
315 return "STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE";
316 case STATE_EMIT_NOTIF_EVENT
:
317 return "STATE_EMIT_NOTIF_EVENT";
318 case STATE_EMIT_NOTIF_END_OF_PACKET
:
319 return "STATE_EMIT_NOTIF_END_OF_PACKET";
322 case STATE_SKIP_PACKET_PADDING
:
323 return "STATE_SKIP_PACKET_PADDING";
330 int bt_notif_iter_switch_packet(struct bt_notif_iter
*notit
);
333 enum bt_btr_status
btr_timestamp_end_cb(void *value
,
334 struct bt_field_type
*type
, void *data
);
337 struct stack
*stack_new(struct bt_notif_iter
*notit
)
339 struct stack
*stack
= NULL
;
341 stack
= g_new0(struct stack
, 1);
343 BT_LOGE_STR("Failed to allocate one stack.");
347 stack
->entries
= g_array_new(FALSE
, TRUE
, sizeof(struct stack_entry
));
348 if (!stack
->entries
) {
349 BT_LOGE_STR("Failed to allocate a GArray.");
353 BT_LOGD("Created stack: notit-addr=%p, stack-addr=%p", notit
, stack
);
362 void stack_destroy(struct stack
*stack
)
365 BT_LOGD("Destroying stack: addr=%p", stack
);
367 if (stack
->entries
) {
368 g_array_free(stack
->entries
, TRUE
);
375 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=%zu, stack-size-after=%zu",
383 stack
, stack
->size
, stack
->size
+ 1);
385 if (stack
->entries
->len
== stack
->size
) {
386 g_array_set_size(stack
->entries
, stack
->size
+ 1);
389 entry
= &g_array_index(stack
->entries
, struct stack_entry
, stack
->size
);
397 unsigned int stack_size(struct stack
*stack
)
404 void stack_pop(struct stack
*stack
)
407 BT_ASSERT(stack_size(stack
));
408 BT_LOGV("Popping from stack: "
409 "stack-addr=%p, stack-size-before=%zu, stack-size-after=%zu",
410 stack
, stack
->size
, stack
->size
- 1);
415 struct stack_entry
*stack_top(struct stack
*stack
)
418 BT_ASSERT(stack_size(stack
));
419 return &g_array_index(stack
->entries
, struct stack_entry
,
424 bool stack_empty(struct stack
*stack
)
426 return stack_size(stack
) == 0;
430 void stack_clear(struct stack
*stack
)
437 enum bt_notif_iter_status
notif_iter_status_from_m_status(
438 enum bt_notif_iter_medium_status m_status
)
440 return (int) m_status
;
444 size_t buf_size_bits(struct bt_notif_iter
*notit
)
446 return notit
->buf
.sz
* 8;
450 size_t buf_available_bits(struct bt_notif_iter
*notit
)
452 return buf_size_bits(notit
) - notit
->buf
.at
;
456 size_t packet_at(struct bt_notif_iter
*notit
)
458 return notit
->buf
.packet_offset
+ notit
->buf
.at
;
462 void buf_consume_bits(struct bt_notif_iter
*notit
, size_t incr
)
464 BT_LOGV("Advancing cursor: notit-addr=%p, cur-before=%zu, cur-after=%zu",
465 notit
, notit
->buf
.at
, notit
->buf
.at
+ incr
);
466 notit
->buf
.at
+= incr
;
470 enum bt_notif_iter_status
request_medium_bytes(
471 struct bt_notif_iter
*notit
)
473 uint8_t *buffer_addr
= NULL
;
474 size_t buffer_sz
= 0;
475 enum bt_notif_iter_medium_status m_status
;
477 BT_LOGV("Calling user function (request bytes): notit-addr=%p, "
478 "request-size=%zu", notit
, notit
->medium
.max_request_sz
);
479 m_status
= notit
->medium
.medops
.request_bytes(
480 notit
->medium
.max_request_sz
, &buffer_addr
,
481 &buffer_sz
, notit
->medium
.data
);
482 BT_LOGV("User function returned: status=%s, buf-addr=%p, buf-size=%zu",
483 bt_notif_iter_medium_status_string(m_status
),
484 buffer_addr
, buffer_sz
);
485 if (m_status
== BT_NOTIF_ITER_MEDIUM_STATUS_OK
) {
486 BT_ASSERT(buffer_sz
!= 0);
488 /* New packet offset is old one + old size (in bits) */
489 notit
->buf
.packet_offset
+= buf_size_bits(notit
);
491 /* Restart at the beginning of the new medium buffer */
493 notit
->buf
.last_eh_at
= SIZE_MAX
;
495 /* New medium buffer size */
496 notit
->buf
.sz
= buffer_sz
;
498 /* New medium buffer address */
499 notit
->buf
.addr
= buffer_addr
;
501 BT_LOGV("User function returned new bytes: "
502 "packet-offset=%zu, cur=%zu, size=%zu, addr=%p",
503 notit
->buf
.packet_offset
, notit
->buf
.at
,
504 notit
->buf
.sz
, notit
->buf
.addr
);
505 BT_LOGV_MEM(buffer_addr
, buffer_sz
, "Returned bytes at %p:",
507 } else if (m_status
== BT_NOTIF_ITER_MEDIUM_STATUS_EOF
) {
509 * User returned end of stream: validate that we're not
510 * in the middle of a packet header, packet context, or
513 if (notit
->cur_packet_size
>= 0) {
514 if (packet_at(notit
) == notit
->cur_packet_size
) {
518 if (packet_at(notit
) == 0) {
522 if (notit
->buf
.last_eh_at
!= SIZE_MAX
&&
523 notit
->buf
.at
== notit
->buf
.last_eh_at
) {
528 /* All other states are invalid */
529 BT_LOGW("User function returned %s, but notification iterator is in an unexpected state: "
530 "state=%s, cur-packet-size=%" PRId64
", cur=%zu, "
531 "packet-cur=%zu, last-eh-at=%zu",
532 bt_notif_iter_medium_status_string(m_status
),
533 state_string(notit
->state
),
534 notit
->cur_packet_size
,
535 notit
->buf
.at
, packet_at(notit
),
536 notit
->buf
.last_eh_at
);
537 m_status
= BT_NOTIF_ITER_MEDIUM_STATUS_ERROR
;
538 } else if (m_status
< 0) {
539 BT_LOGW("User function failed: status=%s",
540 bt_notif_iter_medium_status_string(m_status
));
544 return notif_iter_status_from_m_status(m_status
);
548 enum bt_notif_iter_status
buf_ensure_available_bits(
549 struct bt_notif_iter
*notit
)
551 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
553 if (buf_available_bits(notit
) == 0) {
555 * This _cannot_ return BT_NOTIF_ITER_STATUS_OK
558 status
= request_medium_bytes(notit
);
565 enum bt_notif_iter_status
read_dscope_begin_state(
566 struct bt_notif_iter
*notit
,
567 struct bt_field_type
*dscope_field_type
,
568 enum state done_state
, enum state continue_state
,
569 struct bt_field
*dscope_field
)
571 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
572 enum bt_btr_status btr_status
;
573 size_t consumed_bits
;
575 notit
->cur_dscope_field
= dscope_field
;
576 BT_LOGV("Starting BTR: notit-addr=%p, btr-addr=%p, ft-addr=%p",
577 notit
, notit
->btr
, dscope_field_type
);
578 consumed_bits
= bt_btr_start(notit
->btr
, dscope_field_type
,
579 notit
->buf
.addr
, notit
->buf
.at
, packet_at(notit
),
580 notit
->buf
.sz
, &btr_status
);
581 BT_LOGV("BTR consumed bits: size=%zu", consumed_bits
);
583 switch (btr_status
) {
584 case BT_BTR_STATUS_OK
:
585 /* type was read completely */
586 BT_LOGV_STR("Field was completely decoded.");
587 notit
->state
= done_state
;
589 case BT_BTR_STATUS_EOF
:
590 BT_LOGV_STR("BTR needs more data to decode field completely.");
591 notit
->state
= continue_state
;
594 BT_LOGW("BTR failed to start: notit-addr=%p, btr-addr=%p, "
595 "status=%s", notit
, notit
->btr
,
596 bt_btr_status_string(btr_status
));
597 status
= BT_NOTIF_ITER_STATUS_ERROR
;
601 /* Consume bits now since we know we're not in an error state */
602 buf_consume_bits(notit
, consumed_bits
);
609 enum bt_notif_iter_status
read_dscope_continue_state(
610 struct bt_notif_iter
*notit
, enum state done_state
)
612 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
613 enum bt_btr_status btr_status
;
614 size_t consumed_bits
;
616 BT_LOGV("Continuing BTR: notit-addr=%p, btr-addr=%p",
619 status
= buf_ensure_available_bits(notit
);
620 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
622 BT_LOGW("Cannot ensure that buffer has at least one byte: "
623 "notif-addr=%p, status=%s",
624 notit
, bt_notif_iter_status_string(status
));
626 BT_LOGV("Cannot ensure that buffer has at least one byte: "
627 "notif-addr=%p, status=%s",
628 notit
, bt_notif_iter_status_string(status
));
635 consumed_bits
= bt_btr_continue(notit
->btr
, notit
->buf
.addr
,
636 notit
->buf
.sz
, &btr_status
);
637 BT_LOGV("BTR consumed bits: size=%zu", consumed_bits
);
639 switch (btr_status
) {
640 case BT_BTR_STATUS_OK
:
641 /* Type was read completely. */
642 BT_LOGV_STR("Field was completely decoded.");
643 notit
->state
= done_state
;
645 case BT_BTR_STATUS_EOF
:
646 /* Stay in this continue state. */
647 BT_LOGV_STR("BTR needs more data to decode field completely.");
650 BT_LOGW("BTR failed to continue: notit-addr=%p, btr-addr=%p, "
651 "status=%s", notit
, notit
->btr
,
652 bt_btr_status_string(btr_status
));
653 status
= BT_NOTIF_ITER_STATUS_ERROR
;
657 /* Consume bits now since we know we're not in an error state. */
658 buf_consume_bits(notit
, consumed_bits
);
664 void release_event_dscopes(struct bt_notif_iter
*notit
)
666 notit
->dscopes
.stream_event_header
= NULL
;
668 if (notit
->event_header_field
) {
669 bt_event_header_field_release(notit
->event_header_field
);
670 notit
->event_header_field
= NULL
;
673 notit
->dscopes
.stream_event_context
= NULL
;
674 notit
->dscopes
.event_context
= NULL
;
675 notit
->dscopes
.event_payload
= NULL
;
679 void release_all_dscopes(struct bt_notif_iter
*notit
)
681 notit
->dscopes
.trace_packet_header
= NULL
;
683 if (notit
->packet_header_field
) {
684 bt_packet_header_field_release(notit
->packet_header_field
);
685 notit
->packet_header_field
= NULL
;
688 notit
->dscopes
.stream_packet_context
= NULL
;
690 if (notit
->packet_context_field
) {
691 bt_packet_context_field_release(notit
->packet_context_field
);
692 notit
->packet_context_field
= NULL
;
695 release_event_dscopes(notit
);
699 enum bt_notif_iter_status
read_packet_header_begin_state(
700 struct bt_notif_iter
*notit
)
702 struct bt_field_type
*packet_header_type
= NULL
;
703 enum bt_notif_iter_status ret
= BT_NOTIF_ITER_STATUS_OK
;
705 if (bt_notif_iter_switch_packet(notit
)) {
706 BT_LOGW("Cannot switch packet: notit-addr=%p", notit
);
707 ret
= BT_NOTIF_ITER_STATUS_ERROR
;
711 /* Packet header type is common to the whole trace. */
712 packet_header_type
= bt_trace_borrow_packet_header_field_type(
714 if (!packet_header_type
) {
715 notit
->state
= STATE_AFTER_TRACE_PACKET_HEADER
;
720 * Create free packet header field from trace. This field is
721 * going to be moved to the packet once we create it. We cannot
722 * create the packet now because:
724 * 1. A packet is created from a stream.
725 * 2. A stream is created from a stream class.
726 * 3. We need the packet header field's content to know the ID
727 * of the stream class to select.
729 BT_ASSERT(!notit
->packet_header_field
);
730 notit
->packet_header_field
= bt_trace_create_packet_header_field(
732 if (!notit
->packet_header_field
) {
733 BT_LOGE_STR("Cannot create packet header field wrapper from trace.");
734 ret
= BT_NOTIF_ITER_STATUS_ERROR
;
738 notit
->dscopes
.trace_packet_header
=
739 bt_packet_header_field_borrow_field(notit
->packet_header_field
);
740 BT_ASSERT(notit
->dscopes
.trace_packet_header
);
741 BT_LOGV("Decoding packet header field:"
742 "notit-addr=%p, trace-addr=%p, trace-name=\"%s\", ft-addr=%p",
743 notit
, notit
->meta
.trace
,
744 bt_trace_get_name(notit
->meta
.trace
), packet_header_type
);
745 ret
= read_dscope_begin_state(notit
, packet_header_type
,
746 STATE_AFTER_TRACE_PACKET_HEADER
,
747 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
748 notit
->dscopes
.trace_packet_header
);
750 BT_LOGW("Cannot decode packet header field: "
751 "notit-addr=%p, trace-addr=%p, "
752 "trace-name=\"%s\", ft-addr=%p",
753 notit
, notit
->meta
.trace
,
754 bt_trace_get_name(notit
->meta
.trace
),
763 enum bt_notif_iter_status
read_packet_header_continue_state(
764 struct bt_notif_iter
*notit
)
766 return read_dscope_continue_state(notit
,
767 STATE_AFTER_TRACE_PACKET_HEADER
);
771 struct stream_class_field_path_cache
*
772 create_stream_class_field_path_cache_entry(
773 struct bt_notif_iter
*notit
,
774 struct bt_stream_class
*stream_class
)
778 int timestamp_end
= -1;
779 int packet_size
= -1;
780 int content_size
= -1;
781 struct stream_class_field_path_cache
*cache_entry
= g_new0(
782 struct stream_class_field_path_cache
, 1);
783 struct bt_field_type
*event_header
= NULL
, *packet_context
= NULL
;
786 BT_LOGE_STR("Failed to allocate one stream class field path cache.");
790 event_header
= bt_stream_class_borrow_event_header_field_type(
792 if (event_header
&& bt_field_type_is_structure(event_header
)) {
795 count
= bt_field_type_structure_get_field_count(
797 BT_ASSERT(count
>= 0);
799 for (i
= 0; i
< count
; i
++) {
803 ret
= bt_field_type_structure_borrow_field_by_index(
804 event_header
, &name
, NULL
, i
);
806 BT_LOGE("Cannot get event header structure field type's field: "
807 "notit-addr=%p, stream-class-addr=%p, "
808 "stream-class-name=\"%s\", "
809 "stream-class-id=%" PRId64
", "
810 "ft-addr=%p, index=%d",
812 bt_stream_class_get_name(stream_class
),
813 bt_stream_class_get_id(stream_class
),
818 if (v
!= -1 && id
!= -1) {
822 if (v
== -1 && strcmp(name
, "v") == 0) {
824 } else if (id
== -1 && !strcmp(name
, "id")) {
830 packet_context
= bt_stream_class_borrow_packet_context_field_type(
832 if (packet_context
&& bt_field_type_is_structure(packet_context
)) {
835 count
= bt_field_type_structure_get_field_count(
837 BT_ASSERT(count
>= 0);
839 for (i
= 0; i
< count
; i
++) {
842 struct bt_field_type
*field_type
;
844 if (timestamp_end
!= -1 && packet_size
!= -1 &&
845 content_size
!= -1) {
849 ret
= bt_field_type_structure_borrow_field_by_index(
850 packet_context
, &name
, &field_type
, i
);
852 BT_LOGE("Cannot get packet context structure field type's field: "
853 "notit-addr=%p, stream-class-addr=%p, "
854 "stream-class-name=\"%s\", "
855 "stream-class-id=%" PRId64
", "
856 "ft-addr=%p, index=%d",
858 bt_stream_class_get_name(stream_class
),
859 bt_stream_class_get_id(stream_class
),
864 if (timestamp_end
== -1 &&
865 strcmp(name
, "timestamp_end") == 0) {
866 struct field_cb_override
*override
= g_new0(
867 struct field_cb_override
, 1);
873 override
->func
= btr_timestamp_end_cb
;
874 override
->data
= notit
;
875 g_hash_table_insert(notit
->field_overrides
,
876 field_type
, override
);
878 } else if (packet_size
== -1 &&
879 !strcmp(name
, "packet_size")) {
881 } else if (content_size
== -1 &&
882 !strcmp(name
, "content_size")) {
889 cache_entry
->id
= id
;
890 cache_entry
->timestamp_end
= timestamp_end
;
891 cache_entry
->packet_size
= packet_size
;
892 cache_entry
->content_size
= content_size
;
904 struct stream_class_field_path_cache
*get_stream_class_field_path_cache(
905 struct bt_notif_iter
*notit
,
906 struct bt_stream_class
*stream_class
)
908 bool cache_entry_found
;
909 struct stream_class_field_path_cache
*cache_entry
;
911 cache_entry_found
= g_hash_table_lookup_extended(
912 notit
->sc_field_path_caches
,
913 stream_class
, NULL
, (gpointer
) &cache_entry
);
914 if (unlikely(!cache_entry_found
)) {
915 cache_entry
= create_stream_class_field_path_cache_entry(notit
,
917 g_hash_table_insert(notit
->sc_field_path_caches
,
918 stream_class
, (gpointer
) cache_entry
);
925 enum bt_notif_iter_status
set_current_stream_class(
926 struct bt_notif_iter
*notit
)
928 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
929 struct bt_field_type
*packet_header_type
= NULL
;
930 struct bt_field_type
*stream_id_field_type
= NULL
;
931 struct bt_stream_class
*new_stream_class
= NULL
;
934 /* Clear the current stream class field path cache. */
935 notit
->cur_sc_field_path_cache
= NULL
;
937 /* Is there any "stream_id" field in the packet header? */
938 packet_header_type
= bt_trace_borrow_packet_header_field_type(
940 if (!packet_header_type
) {
942 * No packet header, therefore no `stream_id` field,
943 * therefore only one stream class.
945 goto single_stream_class
;
948 BT_ASSERT(bt_field_type_is_structure(packet_header_type
));
951 stream_id_field_type
=
952 bt_field_type_structure_borrow_field_type_by_name(
953 packet_header_type
, "stream_id");
954 if (stream_id_field_type
) {
955 /* Find appropriate stream class using current stream ID */
957 struct bt_field
*stream_id_field
= NULL
;
959 BT_ASSERT(notit
->dscopes
.trace_packet_header
);
962 stream_id_field
= bt_field_structure_borrow_field_by_name(
963 notit
->dscopes
.trace_packet_header
, "stream_id");
964 BT_ASSERT(stream_id_field
);
965 ret
= bt_field_integer_unsigned_get_value(
966 stream_id_field
, &stream_id
);
970 /* Only one stream: pick the first stream class */
971 BT_ASSERT(bt_trace_get_stream_class_count(
972 notit
->meta
.trace
) == 1);
976 BT_LOGV("Found stream class ID to use: notit-addr=%p, "
977 "stream-class-id=%" PRIu64
", "
978 "trace-addr=%p, trace-name=\"%s\"",
979 notit
, stream_id
, notit
->meta
.trace
,
980 bt_trace_get_name(notit
->meta
.trace
));
982 new_stream_class
= bt_trace_borrow_stream_class_by_id(
983 notit
->meta
.trace
, stream_id
);
984 if (!new_stream_class
) {
985 BT_LOGW("No stream class with ID of stream class ID to use in trace: "
986 "notit-addr=%p, stream-class-id=%" PRIu64
", "
987 "trace-addr=%p, trace-name=\"%s\"",
988 notit
, stream_id
, notit
->meta
.trace
,
989 bt_trace_get_name(notit
->meta
.trace
));
990 status
= BT_NOTIF_ITER_STATUS_ERROR
;
994 if (notit
->meta
.stream_class
) {
995 if (new_stream_class
!= notit
->meta
.stream_class
) {
996 BT_LOGW("Two packets refer to two different stream classes within the same packet sequence: "
997 "notit-addr=%p, prev-stream-class-addr=%p, "
998 "prev-stream-class-name=\"%s\", "
999 "prev-stream-class-id=%" PRId64
", "
1000 "next-stream-class-addr=%p, "
1001 "next-stream-class-name=\"%s\", "
1002 "next-stream-class-id=%" PRId64
", "
1003 "trace-addr=%p, trace-name=\"%s\"",
1004 notit
, notit
->meta
.stream_class
,
1005 bt_stream_class_get_name(notit
->meta
.stream_class
),
1006 bt_stream_class_get_id(notit
->meta
.stream_class
),
1008 bt_stream_class_get_name(new_stream_class
),
1009 bt_stream_class_get_id(new_stream_class
),
1011 bt_trace_get_name(notit
->meta
.trace
));
1012 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1016 notit
->meta
.stream_class
= new_stream_class
;
1019 BT_LOGV("Set current stream class: "
1020 "notit-addr=%p, stream-class-addr=%p, "
1021 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
1022 notit
, notit
->meta
.stream_class
,
1023 bt_stream_class_get_name(notit
->meta
.stream_class
),
1024 bt_stream_class_get_id(notit
->meta
.stream_class
));
1027 * Retrieve (or lazily create) the current stream class field path
1030 notit
->cur_sc_field_path_cache
= get_stream_class_field_path_cache(
1031 notit
, notit
->meta
.stream_class
);
1032 if (!notit
->cur_sc_field_path_cache
) {
1033 BT_LOGW("Cannot retrieve stream class field path from cache: "
1034 "notit-addr=%p, stream-class-addr=%p, "
1035 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
1036 notit
, notit
->meta
.stream_class
,
1037 bt_stream_class_get_name(notit
->meta
.stream_class
),
1038 bt_stream_class_get_id(notit
->meta
.stream_class
));
1039 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1048 uint64_t get_cur_stream_instance_id(struct bt_notif_iter
*notit
)
1050 struct bt_field
*stream_instance_id_field
= NULL
;
1051 uint64_t stream_instance_id
= -1ULL;
1054 if (!notit
->dscopes
.trace_packet_header
) {
1058 stream_instance_id_field
= bt_field_structure_borrow_field_by_name(
1059 notit
->dscopes
.trace_packet_header
, "stream_instance_id");
1060 if (!stream_instance_id_field
) {
1064 ret
= bt_field_integer_unsigned_get_value(stream_instance_id_field
,
1065 &stream_instance_id
);
1067 stream_instance_id
= -1ULL;
1072 return stream_instance_id
;
1076 enum bt_notif_iter_status
set_current_stream(struct bt_notif_iter
*notit
)
1078 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1079 struct bt_stream
*stream
= NULL
;
1081 BT_LOGV("Calling user function (get stream): notit-addr=%p, "
1082 "stream-class-addr=%p, stream-class-name=\"%s\", "
1083 "stream-class-id=%" PRId64
,
1084 notit
, notit
->meta
.stream_class
,
1085 bt_stream_class_get_name(notit
->meta
.stream_class
),
1086 bt_stream_class_get_id(notit
->meta
.stream_class
));
1087 stream
= bt_get(notit
->medium
.medops
.borrow_stream(
1088 notit
->meta
.stream_class
, get_cur_stream_instance_id(notit
),
1089 notit
->medium
.data
));
1090 BT_LOGV("User function returned: stream-addr=%p", stream
);
1092 BT_LOGW_STR("User function failed to return a stream object for the given stream class.");
1093 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1097 if (notit
->stream
&& stream
!= notit
->stream
) {
1098 BT_LOGW("User function returned a different stream than the previous one for the same sequence of packets.");
1099 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1103 BT_MOVE(notit
->stream
, stream
);
1111 enum bt_notif_iter_status
set_current_packet(struct bt_notif_iter
*notit
)
1113 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1114 struct bt_packet
*packet
= NULL
;
1116 BT_LOGV("Creating packet for packet notification: "
1117 "notit-addr=%p", notit
);
1118 BT_LOGV("Creating packet from stream: "
1119 "notit-addr=%p, stream-addr=%p, "
1120 "stream-class-addr=%p, "
1121 "stream-class-name=\"%s\", "
1122 "stream-class-id=%" PRId64
,
1123 notit
, notit
->stream
, notit
->meta
.stream_class
,
1124 bt_stream_class_get_name(notit
->meta
.stream_class
),
1125 bt_stream_class_get_id(notit
->meta
.stream_class
));
1128 BT_ASSERT(notit
->stream
);
1129 packet
= bt_packet_create(notit
->stream
);
1131 BT_LOGE("Cannot create packet from stream: "
1132 "notit-addr=%p, stream-addr=%p, "
1133 "stream-class-addr=%p, "
1134 "stream-class-name=\"%s\", "
1135 "stream-class-id=%" PRId64
,
1136 notit
, notit
->stream
, notit
->meta
.stream_class
,
1137 bt_stream_class_get_name(notit
->meta
.stream_class
),
1138 bt_stream_class_get_id(notit
->meta
.stream_class
));
1146 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1149 BT_MOVE(notit
->packet
, packet
);
1154 enum bt_notif_iter_status
after_packet_header_state(
1155 struct bt_notif_iter
*notit
)
1157 enum bt_notif_iter_status status
;
1159 status
= set_current_stream_class(notit
);
1160 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
1164 notit
->state
= STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
;
1171 enum bt_notif_iter_status
read_packet_context_begin_state(
1172 struct bt_notif_iter
*notit
)
1174 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1175 struct bt_field_type
*packet_context_type
;
1177 BT_ASSERT(notit
->meta
.stream_class
);
1178 packet_context_type
= bt_stream_class_borrow_packet_context_field_type(
1179 notit
->meta
.stream_class
);
1180 if (!packet_context_type
) {
1181 BT_LOGV("No packet packet context field type in stream class: continuing: "
1182 "notit-addr=%p, stream-class-addr=%p, "
1183 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
1184 notit
, notit
->meta
.stream_class
,
1185 bt_stream_class_get_name(notit
->meta
.stream_class
),
1186 bt_stream_class_get_id(notit
->meta
.stream_class
));
1187 notit
->state
= STATE_AFTER_STREAM_PACKET_CONTEXT
;
1192 * Create free packet context field from stream class. This
1193 * field is going to be moved to the packet once we create it.
1194 * We cannot create the packet now because a packet is created
1195 * from a stream, and this API must be able to return the packet
1196 * header and context fields without creating a stream
1197 * (bt_notif_iter_get_packet_header_context_fields()).
1199 BT_ASSERT(!notit
->packet_context_field
);
1200 notit
->packet_context_field
=
1201 bt_stream_class_create_packet_context_field(
1202 notit
->meta
.stream_class
);
1203 if (!notit
->packet_context_field
) {
1204 BT_LOGE_STR("Cannot create packet context field wrapper from stream class.");
1205 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1209 notit
->dscopes
.stream_packet_context
=
1210 bt_packet_context_field_borrow_field(notit
->packet_context_field
);
1211 BT_ASSERT(notit
->dscopes
.stream_packet_context
);
1212 BT_LOGV("Decoding packet context field: "
1213 "notit-addr=%p, stream-class-addr=%p, "
1214 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1216 notit
, notit
->meta
.stream_class
,
1217 bt_stream_class_get_name(notit
->meta
.stream_class
),
1218 bt_stream_class_get_id(notit
->meta
.stream_class
),
1219 packet_context_type
);
1220 status
= read_dscope_begin_state(notit
, packet_context_type
,
1221 STATE_AFTER_STREAM_PACKET_CONTEXT
,
1222 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
1223 notit
->dscopes
.stream_packet_context
);
1225 BT_LOGW("Cannot decode packet context field: "
1226 "notit-addr=%p, stream-class-addr=%p, "
1227 "stream-class-name=\"%s\", "
1228 "stream-class-id=%" PRId64
", ft-addr=%p",
1229 notit
, notit
->meta
.stream_class
,
1230 bt_stream_class_get_name(notit
->meta
.stream_class
),
1231 bt_stream_class_get_id(notit
->meta
.stream_class
),
1232 packet_context_type
);
1240 enum bt_notif_iter_status
read_packet_context_continue_state(
1241 struct bt_notif_iter
*notit
)
1243 return read_dscope_continue_state(notit
,
1244 STATE_AFTER_STREAM_PACKET_CONTEXT
);
1248 enum bt_notif_iter_status
set_current_packet_content_sizes(
1249 struct bt_notif_iter
*notit
)
1251 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1252 struct bt_field
*packet_size_field
= NULL
;
1253 struct bt_field
*content_size_field
= NULL
;
1254 uint64_t content_size
= -1ULL, packet_size
= -1ULL;
1256 if (!notit
->dscopes
.stream_packet_context
) {
1260 packet_size_field
= bt_field_structure_borrow_field_by_name(
1261 notit
->dscopes
.stream_packet_context
, "packet_size");
1262 content_size_field
= bt_field_structure_borrow_field_by_name(
1263 notit
->dscopes
.stream_packet_context
, "content_size");
1264 if (packet_size_field
) {
1265 int ret
= bt_field_integer_unsigned_get_value(
1266 packet_size_field
, &packet_size
);
1268 BT_ASSERT(ret
== 0);
1269 if (packet_size
== 0) {
1270 BT_LOGW("Invalid packet size: packet context field indicates packet size is zero: "
1271 "notit-addr=%p, packet-context-field-addr=%p",
1272 notit
, notit
->dscopes
.stream_packet_context
);
1273 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1275 } else if ((packet_size
% 8) != 0) {
1276 BT_LOGW("Invalid packet size: packet context field indicates packet size is not a multiple of 8: "
1277 "notit-addr=%p, packet-context-field-addr=%p, "
1278 "packet-size=%" PRIu64
,
1279 notit
, notit
->dscopes
.stream_packet_context
,
1281 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1286 if (content_size_field
) {
1287 int ret
= bt_field_integer_unsigned_get_value(
1288 content_size_field
, &content_size
);
1290 BT_ASSERT(ret
== 0);
1292 content_size
= packet_size
;
1295 if (content_size
> packet_size
) {
1296 BT_LOGW("Invalid packet or content size: packet context field indicates content size is greater than packet size: "
1297 "notit-addr=%p, packet-context-field-addr=%p, "
1298 "packet-size=%" PRIu64
", content-size=%" PRIu64
,
1299 notit
, notit
->dscopes
.stream_packet_context
,
1300 packet_size
, content_size
);
1301 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1305 if (packet_size
!= -1ULL) {
1306 notit
->cur_packet_size
= packet_size
;
1309 * Use the content size as packet size indicator if the
1310 * packet size field is missing. This means there is no
1311 * padding in this stream.
1313 notit
->cur_packet_size
= content_size
;
1315 notit
->cur_content_size
= content_size
;
1316 BT_LOGV("Set current packet and content sizes: "
1317 "notit-addr=%p, packet-size=%" PRIu64
", content-size=%" PRIu64
,
1318 notit
, packet_size
, content_size
);
1324 enum bt_notif_iter_status
after_packet_context_state(
1325 struct bt_notif_iter
*notit
)
1327 enum bt_notif_iter_status status
;
1329 status
= set_current_packet_content_sizes(notit
);
1330 if (status
== BT_NOTIF_ITER_STATUS_OK
) {
1331 if (notit
->stream_begin_emitted
) {
1332 notit
->state
= STATE_EMIT_NOTIF_NEW_PACKET
;
1334 notit
->state
= STATE_EMIT_NOTIF_NEW_STREAM
;
1342 enum bt_notif_iter_status
read_event_header_begin_state(
1343 struct bt_notif_iter
*notit
)
1345 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1346 struct bt_field_type
*event_header_type
= NULL
;
1348 /* Reset the position of the last event header */
1349 notit
->buf
.last_eh_at
= notit
->buf
.at
;
1351 /* Check if we have some content left */
1352 if (notit
->cur_content_size
>= 0) {
1353 if (packet_at(notit
) == notit
->cur_content_size
) {
1354 /* No more events! */
1355 BT_LOGV("Reached end of packet: notit-addr=%p, "
1356 "cur=%zu", notit
, packet_at(notit
));
1357 notit
->state
= STATE_EMIT_NOTIF_END_OF_PACKET
;
1359 } else if (packet_at(notit
) > notit
->cur_content_size
) {
1360 /* That's not supposed to happen */
1361 BT_LOGV("Before decoding event header field: cursor is passed the packet's content: "
1362 "notit-addr=%p, content-size=%" PRId64
", "
1363 "cur=%zu", notit
, notit
->cur_content_size
,
1365 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1370 release_event_dscopes(notit
);
1371 BT_ASSERT(notit
->meta
.stream_class
);
1372 event_header_type
= bt_stream_class_borrow_event_header_field_type(
1373 notit
->meta
.stream_class
);
1374 if (!event_header_type
) {
1375 notit
->state
= STATE_AFTER_STREAM_EVENT_HEADER
;
1379 BT_ASSERT(!notit
->event_header_field
);
1380 notit
->event_header_field
= bt_stream_class_create_event_header_field(
1381 notit
->meta
.stream_class
);
1382 if (!notit
->event_header_field
) {
1383 BT_LOGE_STR("Cannot create event header field wrapper from trace.");
1384 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1388 notit
->dscopes
.stream_event_header
=
1389 bt_event_header_field_borrow_field(notit
->event_header_field
);
1390 BT_ASSERT(notit
->dscopes
.stream_event_header
);
1391 BT_LOGV("Decoding event header field: "
1392 "notit-addr=%p, stream-class-addr=%p, "
1393 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1395 notit
, notit
->meta
.stream_class
,
1396 bt_stream_class_get_name(notit
->meta
.stream_class
),
1397 bt_stream_class_get_id(notit
->meta
.stream_class
),
1399 status
= read_dscope_begin_state(notit
, event_header_type
,
1400 STATE_AFTER_STREAM_EVENT_HEADER
,
1401 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
,
1402 notit
->dscopes
.stream_event_header
);
1404 BT_LOGW("Cannot decode event header field: "
1405 "notit-addr=%p, stream-class-addr=%p, "
1406 "stream-class-name=\"%s\", "
1407 "stream-class-id=%" PRId64
", ft-addr=%p",
1408 notit
, notit
->meta
.stream_class
,
1409 bt_stream_class_get_name(notit
->meta
.stream_class
),
1410 bt_stream_class_get_id(notit
->meta
.stream_class
),
1419 enum bt_notif_iter_status
read_event_header_continue_state(
1420 struct bt_notif_iter
*notit
)
1422 return read_dscope_continue_state(notit
,
1423 STATE_AFTER_STREAM_EVENT_HEADER
);
1427 enum bt_notif_iter_status
set_current_event_class(struct bt_notif_iter
*notit
)
1430 * The assert() calls in this function are okay because it is
1431 * assumed here that all the metadata objects have been
1432 * validated for CTF correctness before decoding actual streams.
1435 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1436 struct bt_field_type
*event_header_type
;
1437 struct bt_field_type
*id_field_type
= NULL
;
1438 struct bt_field_type
*v_field_type
= NULL
;
1439 uint64_t event_id
= -1ULL;
1442 event_header_type
= bt_stream_class_borrow_event_header_field_type(
1443 notit
->meta
.stream_class
);
1444 if (!event_header_type
) {
1446 * No event header, therefore no event class ID field,
1447 * therefore only one event class.
1449 goto single_event_class
;
1452 /* Is there any "id"/"v" field in the event header? */
1453 BT_ASSERT(bt_field_type_is_structure(event_header_type
));
1454 id_field_type
= bt_field_type_structure_borrow_field_type_by_name(
1455 event_header_type
, "id");
1456 v_field_type
= bt_field_type_structure_borrow_field_type_by_name(
1457 event_header_type
, "v");
1458 BT_ASSERT(notit
->dscopes
.stream_event_header
);
1462 * | | |_ _|_ _| __ __ _
1463 * | | | | | || '_ \ / _` |
1464 * | |___| | | || | | | (_| | S P E C I A L
1465 * |_____|_| |_||_| |_|\__, | C A S E â„¢
1468 struct bt_field
*v_field
= NULL
;
1469 struct bt_field
*v_struct_field
= NULL
;
1470 struct bt_field
*v_struct_id_field
= NULL
;
1472 // TODO: optimalize!
1473 v_field
= bt_field_structure_borrow_field_by_name(
1474 notit
->dscopes
.stream_event_header
, "v");
1478 bt_field_variant_borrow_current_field(v_field
);
1479 if (!v_struct_field
) {
1480 goto end_v_field_type
;
1483 // TODO: optimalize!
1484 v_struct_id_field
= bt_field_structure_borrow_field_by_name(
1485 v_struct_field
, "id");
1486 if (!v_struct_id_field
) {
1487 goto end_v_field_type
;
1490 if (bt_field_is_integer(v_struct_id_field
)) {
1491 ret
= bt_field_integer_unsigned_get_value(
1492 v_struct_id_field
, &event_id
);
1494 BT_LOGV("Cannot get value of unsigned integer field (`id`): continuing: "
1495 "notit=%p, field-addr=%p",
1496 notit
, v_struct_id_field
);
1503 if (id_field_type
&& event_id
== -1ULL) {
1504 /* Check "id" field */
1505 struct bt_field
*id_field
= NULL
;
1506 int ret_get_value
= 0;
1508 // TODO: optimalize!
1509 id_field
= bt_field_structure_borrow_field_by_name(
1510 notit
->dscopes
.stream_event_header
, "id");
1512 goto check_event_id
;
1515 ret_get_value
= bt_field_integer_unsigned_get_value(
1516 id_field
, &event_id
);
1517 BT_ASSERT(ret_get_value
== 0);
1521 if (event_id
== -1ULL) {
1523 /* Event ID not found: single event? */
1524 BT_ASSERT(bt_stream_class_get_event_class_count(
1525 notit
->meta
.stream_class
) == 1);
1529 BT_LOGV("Found event class ID to use: notit-addr=%p, "
1530 "stream-class-addr=%p, stream-class-name=\"%s\", "
1531 "stream-class-id=%" PRId64
", "
1532 "event-class-id=%" PRIu64
,
1533 notit
, notit
->meta
.stream_class
,
1534 bt_stream_class_get_name(notit
->meta
.stream_class
),
1535 bt_stream_class_get_id(notit
->meta
.stream_class
),
1537 notit
->meta
.event_class
= bt_stream_class_borrow_event_class_by_id(
1538 notit
->meta
.stream_class
, event_id
);
1539 if (!notit
->meta
.event_class
) {
1540 BT_LOGW("No event class with ID of event class ID to use in stream class: "
1541 "notit-addr=%p, stream-class-addr=%p, "
1542 "stream-class-name=\"%s\", "
1543 "stream-class-id=%" PRId64
", "
1544 "event-class-id=%" PRIu64
,
1545 notit
, notit
->meta
.stream_class
,
1546 bt_stream_class_get_name(notit
->meta
.stream_class
),
1547 bt_stream_class_get_id(notit
->meta
.stream_class
),
1549 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1553 BT_LOGV("Set current event class: "
1554 "notit-addr=%p, event-class-addr=%p, "
1555 "event-class-name=\"%s\", event-class-id=%" PRId64
,
1556 notit
, notit
->meta
.event_class
,
1557 bt_event_class_get_name(notit
->meta
.event_class
),
1558 bt_event_class_get_id(notit
->meta
.event_class
));
1565 enum bt_notif_iter_status
set_current_event_notification(
1566 struct bt_notif_iter
*notit
)
1568 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1569 struct bt_notification
*notif
= NULL
;
1571 BT_ASSERT(notit
->meta
.event_class
);
1572 BT_ASSERT(notit
->packet
);
1573 BT_LOGV("Creating event notification from event class and packet: "
1574 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", packet-addr=%p",
1575 notit
, notit
->meta
.event_class
,
1576 bt_event_class_get_name(notit
->meta
.event_class
),
1578 BT_ASSERT(notit
->graph
);
1579 notif
= bt_notification_event_create(notit
->graph
,
1580 notit
->meta
.event_class
, notit
->packet
,
1581 notit
->meta
.cc_prio_map
);
1583 BT_LOGE("Cannot create event notification: "
1584 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", "
1586 notit
, notit
->meta
.event_class
,
1587 bt_event_class_get_name(notit
->meta
.event_class
),
1596 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1599 BT_MOVE(notit
->event_notif
, notif
);
1604 enum bt_notif_iter_status
after_event_header_state(
1605 struct bt_notif_iter
*notit
)
1607 enum bt_notif_iter_status status
;
1609 status
= set_current_event_class(notit
);
1610 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
1614 status
= set_current_event_notification(notit
);
1615 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
1619 notit
->event
= bt_notification_event_borrow_event(notit
->event_notif
);
1620 BT_ASSERT(notit
->event
);
1622 if (notit
->event_header_field
) {
1625 BT_ASSERT(notit
->event
);
1626 ret
= bt_event_move_header(notit
->event
,
1627 notit
->event_header_field
);
1630 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1634 notit
->event_header_field
= NULL
;
1637 * At this point notit->dscopes.stream_event_header has
1638 * the same value as the event header field within
1641 BT_ASSERT(bt_event_borrow_header(notit
->event
) ==
1642 notit
->dscopes
.stream_event_header
);
1645 notit
->state
= STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
;
1652 enum bt_notif_iter_status
read_stream_event_context_begin_state(
1653 struct bt_notif_iter
*notit
)
1655 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1656 struct bt_field_type
*stream_event_context_type
;
1658 stream_event_context_type
=
1659 bt_stream_class_borrow_event_context_field_type(
1660 notit
->meta
.stream_class
);
1661 if (!stream_event_context_type
) {
1662 notit
->state
= STATE_DSCOPE_EVENT_CONTEXT_BEGIN
;
1666 BT_ASSERT(!notit
->dscopes
.stream_event_context
);
1667 notit
->dscopes
.stream_event_context
=
1668 bt_event_borrow_stream_event_context(notit
->event
);
1669 BT_ASSERT(notit
->dscopes
.stream_event_context
);
1670 BT_LOGV("Decoding stream event context field: "
1671 "notit-addr=%p, stream-class-addr=%p, "
1672 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1674 notit
, notit
->meta
.stream_class
,
1675 bt_stream_class_get_name(notit
->meta
.stream_class
),
1676 bt_stream_class_get_id(notit
->meta
.stream_class
),
1677 stream_event_context_type
);
1678 status
= read_dscope_begin_state(notit
, stream_event_context_type
,
1679 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
,
1680 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
,
1681 notit
->dscopes
.stream_event_context
);
1683 BT_LOGW("Cannot decode stream event context field: "
1684 "notit-addr=%p, stream-class-addr=%p, "
1685 "stream-class-name=\"%s\", "
1686 "stream-class-id=%" PRId64
", ft-addr=%p",
1687 notit
, notit
->meta
.stream_class
,
1688 bt_stream_class_get_name(notit
->meta
.stream_class
),
1689 bt_stream_class_get_id(notit
->meta
.stream_class
),
1690 stream_event_context_type
);
1698 enum bt_notif_iter_status
read_stream_event_context_continue_state(
1699 struct bt_notif_iter
*notit
)
1701 return read_dscope_continue_state(notit
,
1702 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
);
1706 enum bt_notif_iter_status
read_event_context_begin_state(
1707 struct bt_notif_iter
*notit
)
1709 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1710 struct bt_field_type
*event_context_type
;
1712 event_context_type
= bt_event_class_borrow_context_field_type(
1713 notit
->meta
.event_class
);
1714 if (!event_context_type
) {
1715 notit
->state
= STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
;
1719 BT_ASSERT(!notit
->dscopes
.event_context
);
1720 notit
->dscopes
.event_context
= bt_event_borrow_context(notit
->event
);
1721 BT_ASSERT(notit
->dscopes
.event_context
);
1722 BT_LOGV("Decoding event context field: "
1723 "notit-addr=%p, event-class-addr=%p, "
1724 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1726 notit
, notit
->meta
.event_class
,
1727 bt_event_class_get_name(notit
->meta
.event_class
),
1728 bt_event_class_get_id(notit
->meta
.event_class
),
1729 event_context_type
);
1730 status
= read_dscope_begin_state(notit
, event_context_type
,
1731 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
1732 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
,
1733 notit
->dscopes
.event_context
);
1735 BT_LOGW("Cannot decode event context field: "
1736 "notit-addr=%p, event-class-addr=%p, "
1737 "event-class-name=\"%s\", "
1738 "event-class-id=%" PRId64
", ft-addr=%p",
1739 notit
, notit
->meta
.event_class
,
1740 bt_event_class_get_name(notit
->meta
.event_class
),
1741 bt_event_class_get_id(notit
->meta
.event_class
),
1742 event_context_type
);
1750 enum bt_notif_iter_status
read_event_context_continue_state(
1751 struct bt_notif_iter
*notit
)
1753 return read_dscope_continue_state(notit
,
1754 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
);
1758 enum bt_notif_iter_status
read_event_payload_begin_state(
1759 struct bt_notif_iter
*notit
)
1761 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1762 struct bt_field_type
*event_payload_type
;
1764 event_payload_type
= bt_event_class_borrow_payload_field_type(
1765 notit
->meta
.event_class
);
1766 if (!event_payload_type
) {
1767 notit
->state
= STATE_EMIT_NOTIF_EVENT
;
1771 BT_ASSERT(!notit
->dscopes
.event_payload
);
1772 notit
->dscopes
.event_payload
= bt_event_borrow_payload(notit
->event
);
1773 BT_ASSERT(notit
->dscopes
.event_payload
);
1774 BT_LOGV("Decoding event payload field: "
1775 "notit-addr=%p, event-class-addr=%p, "
1776 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1778 notit
, notit
->meta
.event_class
,
1779 bt_event_class_get_name(notit
->meta
.event_class
),
1780 bt_event_class_get_id(notit
->meta
.event_class
),
1781 event_payload_type
);
1782 status
= read_dscope_begin_state(notit
, event_payload_type
,
1783 STATE_EMIT_NOTIF_EVENT
,
1784 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
1785 notit
->dscopes
.event_payload
);
1787 BT_LOGW("Cannot decode event payload field: "
1788 "notit-addr=%p, event-class-addr=%p, "
1789 "event-class-name=\"%s\", "
1790 "event-class-id=%" PRId64
", ft-addr=%p",
1791 notit
, notit
->meta
.event_class
,
1792 bt_event_class_get_name(notit
->meta
.event_class
),
1793 bt_event_class_get_id(notit
->meta
.event_class
),
1794 event_payload_type
);
1802 enum bt_notif_iter_status
read_event_payload_continue_state(
1803 struct bt_notif_iter
*notit
)
1805 return read_dscope_continue_state(notit
, STATE_EMIT_NOTIF_EVENT
);
1809 enum bt_notif_iter_status
skip_packet_padding_state(
1810 struct bt_notif_iter
*notit
)
1812 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1813 size_t bits_to_skip
;
1815 BT_ASSERT(notit
->cur_packet_size
> 0);
1816 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1817 if (bits_to_skip
== 0) {
1818 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1821 size_t bits_to_consume
;
1823 BT_LOGV("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu",
1824 bits_to_skip
, notit
, bits_to_skip
);
1825 status
= buf_ensure_available_bits(notit
);
1826 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
1830 bits_to_consume
= MIN(buf_available_bits(notit
), bits_to_skip
);
1831 BT_LOGV("Skipping %zu bits of padding: notit-addr=%p, size=%zu",
1832 bits_to_consume
, notit
, bits_to_consume
);
1833 buf_consume_bits(notit
, bits_to_consume
);
1834 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1835 if (bits_to_skip
== 0) {
1836 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1846 enum bt_notif_iter_status
handle_state(struct bt_notif_iter
*notit
)
1848 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1849 const enum state state
= notit
->state
;
1851 BT_LOGV("Handling state: notit-addr=%p, state=%s",
1852 notit
, state_string(state
));
1854 // TODO: optimalize!
1857 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1859 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
1860 status
= read_packet_header_begin_state(notit
);
1862 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
1863 status
= read_packet_header_continue_state(notit
);
1865 case STATE_AFTER_TRACE_PACKET_HEADER
:
1866 status
= after_packet_header_state(notit
);
1868 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
1869 status
= read_packet_context_begin_state(notit
);
1871 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
1872 status
= read_packet_context_continue_state(notit
);
1874 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
1875 status
= after_packet_context_state(notit
);
1877 case STATE_EMIT_NOTIF_NEW_STREAM
:
1878 notit
->state
= STATE_EMIT_NOTIF_NEW_PACKET
;
1880 case STATE_EMIT_NOTIF_NEW_PACKET
:
1881 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1883 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
:
1884 status
= read_event_header_begin_state(notit
);
1886 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
:
1887 status
= read_event_header_continue_state(notit
);
1889 case STATE_AFTER_STREAM_EVENT_HEADER
:
1890 status
= after_event_header_state(notit
);
1892 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
:
1893 status
= read_stream_event_context_begin_state(notit
);
1895 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
:
1896 status
= read_stream_event_context_continue_state(notit
);
1898 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN
:
1899 status
= read_event_context_begin_state(notit
);
1901 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
:
1902 status
= read_event_context_continue_state(notit
);
1904 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
1905 status
= read_event_payload_begin_state(notit
);
1907 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
1908 status
= read_event_payload_continue_state(notit
);
1910 case STATE_EMIT_NOTIF_EVENT
:
1911 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1913 case STATE_SKIP_PACKET_PADDING
:
1914 status
= skip_packet_padding_state(notit
);
1916 case STATE_EMIT_NOTIF_END_OF_PACKET
:
1917 notit
->state
= STATE_SKIP_PACKET_PADDING
;
1920 BT_LOGD("Unknown CTF plugin notification iterator state: "
1921 "notit-addr=%p, state=%d", notit
, notit
->state
);
1925 BT_LOGV("Handled state: notit-addr=%p, status=%s, "
1926 "prev-state=%s, cur-state=%s",
1927 notit
, bt_notif_iter_status_string(status
),
1928 state_string(state
), state_string(notit
->state
));
1933 * Resets the internal state of a CTF notification iterator.
1936 void bt_notif_iter_reset(struct bt_notif_iter
*notit
)
1939 BT_LOGD("Resetting notification iterator: addr=%p", notit
);
1940 stack_clear(notit
->stack
);
1941 notit
->meta
.stream_class
= NULL
;
1942 notit
->meta
.event_class
= NULL
;
1943 BT_PUT(notit
->packet
);
1944 BT_PUT(notit
->stream
);
1945 BT_PUT(notit
->event_notif
);
1946 release_all_dscopes(notit
);
1947 notit
->cur_dscope_field
= NULL
;
1949 if (notit
->packet_header_field
) {
1950 bt_packet_header_field_release(notit
->packet_header_field
);
1951 notit
->packet_header_field
= NULL
;
1954 if (notit
->packet_context_field
) {
1955 bt_packet_context_field_release(notit
->packet_context_field
);
1956 notit
->packet_context_field
= NULL
;
1959 if (notit
->event_header_field
) {
1960 bt_event_header_field_release(notit
->event_header_field
);
1961 notit
->event_header_field
= NULL
;
1964 notit
->buf
.addr
= NULL
;
1967 notit
->buf
.last_eh_at
= SIZE_MAX
;
1968 notit
->buf
.packet_offset
= 0;
1969 notit
->state
= STATE_INIT
;
1970 notit
->cur_content_size
= -1;
1971 notit
->cur_packet_size
= -1;
1972 notit
->cur_packet_offset
= -1;
1973 notit
->stream_begin_emitted
= false;
1974 notit
->cur_timestamp_end
= NULL
;
1978 int bt_notif_iter_switch_packet(struct bt_notif_iter
*notit
)
1983 * We don't put the stream class here because we need to make
1984 * sure that all the packets processed by the same notification
1985 * iterator refer to the same stream class (the first one).
1989 if (notit
->cur_packet_size
!= -1) {
1990 notit
->cur_packet_offset
+= notit
->cur_packet_size
;
1993 BT_LOGV("Switching packet: notit-addr=%p, cur=%zu, "
1994 "packet-offset=%" PRId64
, notit
, notit
->buf
.at
,
1995 notit
->cur_packet_offset
);
1996 stack_clear(notit
->stack
);
1997 notit
->meta
.event_class
= NULL
;
1998 BT_PUT(notit
->packet
);
1999 BT_PUT(notit
->event_notif
);
2000 notit
->cur_timestamp_end
= NULL
;
2001 release_all_dscopes(notit
);
2002 notit
->cur_dscope_field
= NULL
;
2005 * Adjust current buffer so that addr points to the beginning of the new
2008 if (notit
->buf
.addr
) {
2009 size_t consumed_bytes
= (size_t) (notit
->buf
.at
/ CHAR_BIT
);
2011 /* Packets are assumed to start on a byte frontier. */
2012 if (notit
->buf
.at
% CHAR_BIT
) {
2013 BT_LOGW("Cannot switch packet: current position is not a multiple of 8: "
2014 "notit-addr=%p, cur=%zu", notit
, notit
->buf
.at
);
2019 notit
->buf
.addr
+= consumed_bytes
;
2020 notit
->buf
.sz
-= consumed_bytes
;
2022 notit
->buf
.packet_offset
= 0;
2023 BT_LOGV("Adjusted buffer: addr=%p, size=%zu",
2024 notit
->buf
.addr
, notit
->buf
.sz
);
2027 notit
->cur_content_size
= -1;
2028 notit
->cur_packet_size
= -1;
2029 notit
->cur_sc_field_path_cache
= NULL
;
2036 struct bt_field
*borrow_next_field(struct bt_notif_iter
*notit
)
2038 struct bt_field
*next_field
= NULL
;
2039 struct bt_field
*base_field
;
2040 struct bt_field_type
*base_type
;
2043 BT_ASSERT(!stack_empty(notit
->stack
));
2044 index
= stack_top(notit
->stack
)->index
;
2045 base_field
= stack_top(notit
->stack
)->base
;
2046 BT_ASSERT(base_field
);
2047 base_type
= bt_field_borrow_type(base_field
);
2048 BT_ASSERT(base_type
);
2050 switch (bt_field_type_get_type_id(base_type
)) {
2051 case BT_FIELD_TYPE_ID_STRUCT
:
2053 next_field
= bt_field_structure_borrow_field_by_index(
2057 case BT_FIELD_TYPE_ID_ARRAY
:
2058 next_field
= bt_field_array_borrow_field(base_field
, index
);
2060 case BT_FIELD_TYPE_ID_SEQUENCE
:
2061 next_field
= bt_field_sequence_borrow_field(base_field
, index
);
2063 case BT_FIELD_TYPE_ID_VARIANT
:
2064 next_field
= bt_field_variant_borrow_current_field(base_field
);
2067 BT_LOGF("Unknown base field type ID: "
2068 "notit-addr=%p, ft-addr=%p, ft-id=%s",
2070 bt_common_field_type_id_string(
2071 bt_field_type_get_type_id(base_type
)));
2079 void update_clock_state(uint64_t *state
, struct bt_field
*value_field
,
2080 struct bt_field_type
*value_type
)
2082 uint64_t requested_new_value
;
2083 uint64_t requested_new_value_mask
;
2084 uint64_t cur_value_masked
;
2085 int requested_new_value_size
;
2088 BT_ASSERT(value_type
);
2089 BT_ASSERT(bt_field_type_is_integer(value_type
));
2090 requested_new_value_size
=
2091 bt_field_type_integer_get_size(value_type
);
2092 BT_ASSERT(requested_new_value_size
> 0);
2093 ret
= bt_field_integer_unsigned_get_value(value_field
,
2094 &requested_new_value
);
2098 * Special case for a 64-bit new value, which is the limit
2099 * of a clock value as of this version: overwrite the
2100 * current value directly.
2102 if (requested_new_value_size
== 64) {
2103 *state
= requested_new_value
;
2107 requested_new_value_mask
= (1ULL << requested_new_value_size
) - 1;
2108 cur_value_masked
= *state
& requested_new_value_mask
;
2110 if (requested_new_value
< cur_value_masked
) {
2112 * It looks like a wrap happened on the number of bits
2113 * of the requested new value. Assume that the clock
2114 * value wrapped only one time.
2116 *state
+= requested_new_value_mask
+ 1;
2119 /* Clear the low bits of the current clock value. */
2120 *state
&= ~requested_new_value_mask
;
2122 /* Set the low bits of the current clock value. */
2123 *state
|= requested_new_value
;
2126 BT_LOGV("Updated clock's value from integer field's value: "
2127 "value=%" PRIu64
, *state
);
2131 enum bt_btr_status
update_clock(struct bt_notif_iter
*notit
,
2132 struct bt_field
*int_field
)
2134 gboolean clock_class_found
;
2135 uint64_t *clock_state
= NULL
;
2136 struct bt_field_type
*int_field_type
= NULL
;
2137 enum bt_btr_status ret
= BT_BTR_STATUS_OK
;
2138 struct bt_clock_class
*clock_class
= NULL
;
2140 int_field_type
= bt_field_borrow_type(int_field
);
2141 BT_ASSERT(int_field_type
);
2143 if (bt_field_type_is_enumeration(int_field_type
)) {
2145 bt_field_type_enumeration_borrow_container_field_type(
2149 clock_class
= bt_field_type_integer_borrow_mapped_clock_class(
2151 if (likely(!clock_class
)) {
2155 clock_class_found
= g_hash_table_lookup_extended(notit
->clock_states
,
2156 clock_class
, NULL
, (gpointer
) &clock_state
);
2157 if (!clock_class_found
) {
2158 clock_state
= g_new0(uint64_t, 1);
2160 BT_LOGE_STR("Failed to allocate a uint64_t.");
2161 ret
= BT_BTR_STATUS_ENOMEM
;
2165 g_hash_table_insert(notit
->clock_states
, clock_class
,
2169 /* Update the clock's state. */
2170 BT_LOGV("Updating notification iterator's clock's value from integer field: "
2171 "notit-addr=%p, clock-class-addr=%p, "
2172 "clock-class-name=\"%s\", value=%" PRIu64
,
2174 bt_clock_class_get_name(clock_class
), *clock_state
);
2175 update_clock_state(clock_state
, int_field
, int_field_type
);
2182 enum bt_btr_status
btr_unsigned_int_common(uint64_t value
,
2183 struct bt_field_type
*type
, void *data
,
2184 struct bt_field
**out_field
)
2186 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2187 struct bt_field
*field
= NULL
;
2188 struct bt_notif_iter
*notit
= data
;
2191 BT_LOGV("Common unsigned integer function called from BTR: "
2192 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2193 "ft-id=%s, value=%" PRIu64
,
2194 notit
, notit
->btr
, type
,
2195 bt_common_field_type_id_string(
2196 bt_field_type_get_type_id(type
)),
2198 field
= borrow_next_field(notit
);
2200 BT_LOGW("Cannot borrow next field: notit-addr=%p", notit
);
2201 status
= BT_BTR_STATUS_ERROR
;
2205 BT_ASSERT(bt_field_is_integer(field
) || bt_field_is_enumeration(field
));
2206 ret
= bt_field_integer_unsigned_set_value(field
, value
);
2207 BT_ASSERT(ret
== 0);
2208 stack_top(notit
->stack
)->index
++;
2216 enum bt_btr_status
btr_timestamp_end_cb(void *value
,
2217 struct bt_field_type
*type
, void *data
)
2219 enum bt_btr_status status
;
2220 struct bt_field
*field
= NULL
;
2221 struct bt_notif_iter
*notit
= data
;
2222 uint64_t uvalue
= *((uint64_t *) value
);
2224 BT_LOGV("`timestamp_end` unsigned integer function called from BTR: "
2225 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2227 notit
, notit
->btr
, type
,
2228 bt_common_field_type_id_string(
2229 bt_field_type_get_type_id(type
)));
2231 status
= btr_unsigned_int_common(uvalue
, type
, data
, &field
);
2233 /* Set as the current packet's end timestamp field */
2234 notit
->cur_timestamp_end
= field
;
2239 enum bt_btr_status
btr_unsigned_int_cb(uint64_t value
,
2240 struct bt_field_type
*type
, void *data
)
2242 struct bt_notif_iter
*notit
= data
;
2243 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2244 struct bt_field
*field
= NULL
;
2245 struct field_cb_override
*override
;
2247 BT_LOGV("Unsigned integer function called from BTR: "
2248 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2249 "ft-id=%s, value=%" PRIu64
,
2250 notit
, notit
->btr
, type
,
2251 bt_common_field_type_id_string(
2252 bt_field_type_get_type_id(type
)),
2254 override
= g_hash_table_lookup(notit
->field_overrides
, type
);
2255 if (unlikely(override
)) {
2256 /* Override function logs errors */
2257 status
= override
->func(&value
, type
, override
->data
);
2261 status
= btr_unsigned_int_common(value
, type
, data
, &field
);
2262 if (status
!= BT_BTR_STATUS_OK
) {
2263 /* btr_unsigned_int_common() logs errors */
2267 status
= update_clock(notit
, field
);
2274 enum bt_btr_status
btr_signed_int_cb(int64_t value
,
2275 struct bt_field_type
*type
, void *data
)
2277 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2278 struct bt_field
*field
= NULL
;
2279 struct bt_notif_iter
*notit
= data
;
2282 BT_LOGV("Signed integer function called from BTR: "
2283 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2284 "ft-id=%s, value=%" PRId64
,
2285 notit
, notit
->btr
, type
,
2286 bt_common_field_type_id_string(
2287 bt_field_type_get_type_id(type
)),
2289 field
= borrow_next_field(notit
);
2291 BT_LOGW("Cannot borrow next field: notit-addr=%p", notit
);
2292 status
= BT_BTR_STATUS_ERROR
;
2296 BT_ASSERT(bt_field_is_integer(field
) || bt_field_is_enumeration(field
));
2297 ret
= bt_field_integer_signed_set_value(field
, value
);
2298 BT_ASSERT(ret
== 0);
2299 stack_top(notit
->stack
)->index
++;
2306 enum bt_btr_status
btr_floating_point_cb(double value
,
2307 struct bt_field_type
*type
, void *data
)
2309 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2310 struct bt_field
*field
= NULL
;
2311 struct bt_notif_iter
*notit
= data
;
2314 BT_LOGV("Floating point number function called from BTR: "
2315 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2316 "ft-id=%s, value=%f",
2317 notit
, notit
->btr
, type
,
2318 bt_common_field_type_id_string(
2319 bt_field_type_get_type_id(type
)),
2321 field
= borrow_next_field(notit
);
2323 BT_LOGW("Cannot borrow next field: notit-addr=%p", notit
);
2324 status
= BT_BTR_STATUS_ERROR
;
2328 ret
= bt_field_floating_point_set_value(field
, value
);
2330 stack_top(notit
->stack
)->index
++;
2337 enum bt_btr_status
btr_string_begin_cb(
2338 struct bt_field_type
*type
, void *data
)
2340 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2341 struct bt_field
*field
= NULL
;
2342 struct bt_notif_iter
*notit
= data
;
2345 BT_LOGV("String (beginning) function called from BTR: "
2346 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2348 notit
, notit
->btr
, type
,
2349 bt_common_field_type_id_string(
2350 bt_field_type_get_type_id(type
)));
2351 field
= borrow_next_field(notit
);
2353 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2354 status
= BT_BTR_STATUS_ERROR
;
2358 ret
= bt_field_string_clear(field
);
2359 BT_ASSERT(ret
== 0);
2362 * Push on stack. Not a compound type per se, but we know that only
2363 * btr_string_cb() may be called between this call and a subsequent
2364 * call to btr_string_end_cb().
2366 ret
= stack_push(notit
->stack
, field
);
2368 BT_LOGE("Cannot push string field on stack: "
2369 "notit-addr=%p, field-addr=%p", notit
, field
);
2370 status
= BT_BTR_STATUS_ERROR
;
2379 enum bt_btr_status
btr_string_cb(const char *value
,
2380 size_t len
, struct bt_field_type
*type
, void *data
)
2382 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2383 struct bt_field
*field
= NULL
;
2384 struct bt_notif_iter
*notit
= data
;
2387 BT_LOGV("String (substring) function called from BTR: "
2388 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2389 "ft-id=%s, string-length=%zu",
2390 notit
, notit
->btr
, type
,
2391 bt_common_field_type_id_string(
2392 bt_field_type_get_type_id(type
)),
2394 field
= stack_top(notit
->stack
)->base
;
2397 /* Append current substring */
2398 ret
= bt_field_string_append_len(field
, value
, len
);
2400 BT_LOGE("Cannot append substring to string field's value: "
2401 "notit-addr=%p, field-addr=%p, string-length=%zu, "
2402 "ret=%d", notit
, field
, len
, ret
);
2403 status
= BT_BTR_STATUS_ERROR
;
2412 enum bt_btr_status
btr_string_end_cb(
2413 struct bt_field_type
*type
, void *data
)
2415 struct bt_notif_iter
*notit
= data
;
2417 BT_LOGV("String (end) function called from BTR: "
2418 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2420 notit
, notit
->btr
, type
,
2421 bt_common_field_type_id_string(
2422 bt_field_type_get_type_id(type
)));
2424 /* Pop string field */
2425 stack_pop(notit
->stack
);
2427 /* Go to next field */
2428 stack_top(notit
->stack
)->index
++;
2429 return BT_BTR_STATUS_OK
;
2432 enum bt_btr_status
btr_compound_begin_cb(
2433 struct bt_field_type
*type
, void *data
)
2435 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2436 struct bt_notif_iter
*notit
= data
;
2437 struct bt_field
*field
;
2440 BT_LOGV("Compound (beginning) function called from BTR: "
2441 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2443 notit
, notit
->btr
, type
,
2444 bt_common_field_type_id_string(
2445 bt_field_type_get_type_id(type
)));
2448 if (stack_empty(notit
->stack
)) {
2449 /* Root: already set by read_dscope_begin_state() */
2450 field
= notit
->cur_dscope_field
;
2452 field
= borrow_next_field(notit
);
2454 BT_LOGW("Cannot borrow next field: notit-addr=%p", notit
);
2455 status
= BT_BTR_STATUS_ERROR
;
2462 ret
= stack_push(notit
->stack
, field
);
2464 BT_LOGE("Cannot push compound field onto the stack: "
2465 "notit-addr=%p, ft-addr=%p, ft-id=%s, ret=%d",
2467 bt_common_field_type_id_string(
2468 bt_field_type_get_type_id(type
)),
2470 status
= BT_BTR_STATUS_ERROR
;
2478 enum bt_btr_status
btr_compound_end_cb(
2479 struct bt_field_type
*type
, void *data
)
2481 struct bt_notif_iter
*notit
= data
;
2483 BT_LOGV("Compound (end) function called from BTR: "
2484 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2486 notit
, notit
->btr
, type
,
2487 bt_common_field_type_id_string(
2488 bt_field_type_get_type_id(type
)));
2489 BT_ASSERT(!stack_empty(notit
->stack
));
2492 stack_pop(notit
->stack
);
2494 /* If the stack is not empty, increment the base's index */
2495 if (!stack_empty(notit
->stack
)) {
2496 stack_top(notit
->stack
)->index
++;
2499 return BT_BTR_STATUS_OK
;
2503 struct bt_field
*resolve_field(struct bt_notif_iter
*notit
,
2504 struct bt_field_path
*path
)
2506 struct bt_field
*field
= NULL
;
2509 if (BT_LOG_ON_VERBOSE
) {
2510 GString
*gstr
= bt_field_path_string(path
);
2512 BT_LOGV("Resolving field path: notit-addr=%p, field-path=\"%s\"",
2513 notit
, gstr
? gstr
->str
: NULL
);
2516 g_string_free(gstr
, TRUE
);
2520 switch (bt_field_path_get_root_scope(path
)) {
2521 case BT_SCOPE_TRACE_PACKET_HEADER
:
2522 field
= notit
->dscopes
.trace_packet_header
;
2524 case BT_SCOPE_STREAM_PACKET_CONTEXT
:
2525 field
= notit
->dscopes
.stream_packet_context
;
2527 case BT_SCOPE_STREAM_EVENT_HEADER
:
2528 field
= notit
->dscopes
.stream_event_header
;
2530 case BT_SCOPE_STREAM_EVENT_CONTEXT
:
2531 field
= notit
->dscopes
.stream_event_context
;
2533 case BT_SCOPE_EVENT_CONTEXT
:
2534 field
= notit
->dscopes
.event_context
;
2536 case BT_SCOPE_EVENT_FIELDS
:
2537 field
= notit
->dscopes
.event_payload
;
2540 BT_LOGF("Cannot resolve field path: unknown scope: "
2541 "notit-addr=%p, root-scope=%s",
2542 notit
, bt_common_scope_string(
2543 bt_field_path_get_root_scope(path
)));
2548 BT_LOGW("Cannot resolve field path: root field not found: "
2549 "notit-addr=%p, root-scope=%s",
2550 notit
, bt_common_scope_string(
2551 bt_field_path_get_root_scope(path
)));
2555 for (i
= 0; i
< bt_field_path_get_index_count(path
); ++i
) {
2556 struct bt_field
*next_field
= NULL
;
2557 struct bt_field_type
*field_type
;
2558 int index
= bt_field_path_get_index(path
, i
);
2560 field_type
= bt_field_borrow_type(field
);
2561 BT_ASSERT(field_type
);
2563 if (bt_field_type_is_structure(field_type
)) {
2564 next_field
= bt_field_structure_borrow_field_by_index(
2566 } else if (bt_field_type_is_variant(field_type
)) {
2568 bt_field_variant_borrow_current_field(field
);
2574 BT_LOGW("Cannot find next field: "
2575 "notit-addr=%p, ft-addr=%p, ft-id=%s, index=%d",
2577 bt_common_field_type_id_string(
2578 bt_field_type_get_type_id(field_type
)),
2583 /* Move next field -> field */
2592 int64_t btr_get_sequence_length_cb(struct bt_field_type
*type
, void *data
)
2596 struct bt_field
*seq_field
;
2597 struct bt_field_path
*field_path
;
2598 struct bt_notif_iter
*notit
= data
;
2599 struct bt_field
*length_field
= NULL
;
2602 field_path
= bt_field_type_sequence_borrow_length_field_path(type
);
2603 BT_ASSERT(field_path
);
2604 length_field
= resolve_field(notit
, field_path
);
2605 if (!length_field
) {
2606 BT_LOGW("Cannot resolve sequence field type's length field path: "
2607 "notit-addr=%p, ft-addr=%p",
2612 iret
= bt_field_integer_unsigned_get_value(length_field
, &length
);
2614 BT_LOGE("Cannot get value of sequence length field: "
2615 "notit-addr=%p, field-addr=%p",
2616 notit
, length_field
);
2620 seq_field
= stack_top(notit
->stack
)->base
;
2621 iret
= bt_field_sequence_set_length(seq_field
, length
);
2623 BT_LOGE("Cannot set sequence field's length field: "
2624 "notit-addr=%p, seq-field-addr=%p, "
2626 notit
, seq_field
, length
);
2630 ret
= (int64_t) length
;
2637 struct bt_field_type
*btr_borrow_variant_field_type_cb(
2638 struct bt_field_type
*type
, void *data
)
2641 struct bt_field_path
*path
;
2642 struct bt_notif_iter
*notit
= data
;
2643 struct bt_field
*var_field
;
2644 struct bt_field
*tag_field
= NULL
;
2645 struct bt_field_type
*tag_ft
= NULL
;
2646 struct bt_field_type
*tag_int_ft
= NULL
;
2647 struct bt_field
*selected_field
= NULL
;
2648 struct bt_field_type
*selected_field_type
= NULL
;
2650 path
= bt_field_type_variant_borrow_tag_field_path(type
);
2652 tag_field
= resolve_field(notit
, path
);
2654 BT_LOGW("Cannot resolve variant field type's tag field path: "
2655 "notit-addr=%p, ft-addr=%p",
2661 * We found the enumeration tag field instance which should be
2662 * able to select a current field for this variant. This
2663 * callback function we're in is called _after_
2664 * compound_begin(), so the current stack top's base field is
2665 * the variant field in question. We set the variant field's tag
2666 * here and then get the current (selected) field thanks to this
2667 * tag field's value. This current field will also provide us
2668 * with its type. Then, this current field will remain the
2669 * current selected one until the next callback function call
2670 * which is used to fill it.
2672 var_field
= stack_top(notit
->stack
)->base
;
2673 tag_ft
= bt_field_borrow_type(tag_field
);
2674 tag_int_ft
= bt_field_type_enumeration_borrow_container_field_type(
2677 if (bt_field_type_integer_is_signed(tag_int_ft
)) {
2680 ret
= bt_field_integer_signed_get_value(tag_field
, &tag_value
);
2681 BT_ASSERT(ret
== 0);
2682 ret
= bt_field_variant_set_tag_signed(var_field
, tag_value
);
2683 BT_ASSERT(ret
== 0);
2687 ret
= bt_field_integer_unsigned_get_value(tag_field
,
2689 BT_ASSERT(ret
== 0);
2690 ret
= bt_field_variant_set_tag_unsigned(var_field
, tag_value
);
2691 BT_ASSERT(ret
== 0);
2694 selected_field
= bt_field_variant_borrow_current_field(var_field
);
2695 if (!selected_field
) {
2696 BT_LOGW("Cannot borrow variant field's current field: "
2697 "notit-addr=%p, var-field-addr=%p",
2702 selected_field_type
= bt_field_borrow_type(selected_field
);
2705 return selected_field_type
;
2709 int set_event_clocks(struct bt_notif_iter
*notit
)
2712 GHashTableIter iter
;
2713 struct bt_clock_class
*clock_class
;
2714 uint64_t *clock_state
;
2716 g_hash_table_iter_init(&iter
, notit
->clock_states
);
2718 while (g_hash_table_iter_next(&iter
, (gpointer
) &clock_class
,
2719 (gpointer
) &clock_state
)) {
2720 struct bt_clock_value
*clock_value
;
2722 clock_value
= bt_event_borrow_clock_value(notit
->event
,
2725 BT_LOGE("Cannot borrow clock value from event with given clock class: "
2726 "notit-addr=%p, clock-class-addr=%p, "
2727 "clock-class-name=\"%s\"",
2729 bt_clock_class_get_name(clock_class
));
2734 ret
= bt_clock_value_set_value(clock_value
, *clock_state
);
2735 BT_ASSERT(ret
== 0);
2744 void notify_new_stream(struct bt_notif_iter
*notit
,
2745 struct bt_notification
**notification
)
2747 enum bt_notif_iter_status status
;
2748 struct bt_notification
*ret
= NULL
;
2750 status
= set_current_stream(notit
);
2751 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
2756 BT_ASSERT(notit
->stream
);
2757 BT_ASSERT(notit
->graph
);
2758 ret
= bt_notification_stream_begin_create(notit
->graph
, notit
->stream
);
2760 BT_LOGE("Cannot create stream beginning notification: "
2761 "notit-addr=%p, stream-addr=%p",
2762 notit
, notit
->stream
);
2767 *notification
= ret
;
2771 void notify_end_of_stream(struct bt_notif_iter
*notit
,
2772 struct bt_notification
**notification
)
2774 struct bt_notification
*ret
;
2776 if (!notit
->stream
) {
2777 BT_LOGE("Cannot create stream for stream notification: "
2778 "notit-addr=%p", notit
);
2782 BT_ASSERT(notit
->graph
);
2783 ret
= bt_notification_stream_end_create(notit
->graph
, notit
->stream
);
2785 BT_LOGE("Cannot create stream beginning notification: "
2786 "notit-addr=%p, stream-addr=%p",
2787 notit
, notit
->stream
);
2790 *notification
= ret
;
2794 void notify_new_packet(struct bt_notif_iter
*notit
,
2795 struct bt_notification
**notification
)
2798 enum bt_notif_iter_status status
;
2799 struct bt_notification
*notif
= NULL
;
2801 status
= set_current_packet(notit
);
2802 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
2806 BT_ASSERT(notit
->packet
);
2808 if (notit
->packet_header_field
) {
2809 ret
= bt_packet_move_header(notit
->packet
,
2810 notit
->packet_header_field
);
2815 notit
->packet_header_field
= NULL
;
2818 * At this point notit->dscopes.trace_packet_header has
2819 * the same value as the packet header field within
2822 BT_ASSERT(bt_packet_borrow_header(notit
->packet
) ==
2823 notit
->dscopes
.trace_packet_header
);
2826 if (notit
->packet_context_field
) {
2827 ret
= bt_packet_move_context(notit
->packet
,
2828 notit
->packet_context_field
);
2833 notit
->packet_context_field
= NULL
;
2836 * At this point notit->dscopes.trace_packet_header has
2837 * the same value as the packet header field within
2840 BT_ASSERT(bt_packet_borrow_context(notit
->packet
) ==
2841 notit
->dscopes
.stream_packet_context
);
2844 BT_ASSERT(notit
->graph
);
2845 notif
= bt_notification_packet_begin_create(notit
->graph
,
2848 BT_LOGE("Cannot create packet beginning notification: "
2849 "notit-addr=%p, packet-addr=%p",
2850 notit
, notit
->packet
);
2855 *notification
= notif
;
2859 void notify_end_of_packet(struct bt_notif_iter
*notit
,
2860 struct bt_notification
**notification
)
2862 struct bt_notification
*notif
;
2864 if (!notit
->packet
) {
2868 BT_ASSERT(notit
->graph
);
2869 notif
= bt_notification_packet_end_create(notit
->graph
, notit
->packet
);
2871 BT_LOGE("Cannot create packet end notification: "
2872 "notit-addr=%p, packet-addr=%p",
2873 notit
, notit
->packet
);
2877 BT_PUT(notit
->packet
);
2878 *notification
= notif
;
2882 void init_trace_field_path_cache(struct bt_trace
*trace
,
2883 struct trace_field_path_cache
*trace_field_path_cache
)
2886 int stream_instance_id
= -1;
2888 struct bt_field_type
*packet_header
= NULL
;
2890 packet_header
= bt_trace_borrow_packet_header_field_type(trace
);
2891 if (!packet_header
) {
2895 if (!bt_field_type_is_structure(packet_header
)) {
2899 count
= bt_field_type_structure_get_field_count(packet_header
);
2900 BT_ASSERT(count
>= 0);
2902 for (i
= 0; (i
< count
&& (stream_id
== -1 || stream_instance_id
== -1)); i
++) {
2904 const char *field_name
;
2906 ret
= bt_field_type_structure_borrow_field_by_index(
2907 packet_header
, &field_name
, NULL
, i
);
2909 BT_LOGE("Cannot get structure field's field: "
2910 "field-addr=%p, index=%d",
2915 if (stream_id
== -1 && !strcmp(field_name
, "stream_id")) {
2917 } else if (stream_instance_id
== -1 &&
2918 !strcmp(field_name
, "stream_instance_id")) {
2919 stream_instance_id
= i
;
2924 trace_field_path_cache
->stream_id
= stream_id
;
2925 trace_field_path_cache
->stream_instance_id
= stream_instance_id
;
2929 struct bt_notif_iter
*bt_notif_iter_create(struct bt_trace
*trace
,
2930 size_t max_request_sz
,
2931 struct bt_notif_iter_medium_ops medops
, void *data
)
2933 struct bt_notif_iter
*notit
= NULL
;
2934 struct bt_btr_cbs cbs
= {
2936 .signed_int
= btr_signed_int_cb
,
2937 .unsigned_int
= btr_unsigned_int_cb
,
2938 .floating_point
= btr_floating_point_cb
,
2939 .string_begin
= btr_string_begin_cb
,
2940 .string
= btr_string_cb
,
2941 .string_end
= btr_string_end_cb
,
2942 .compound_begin
= btr_compound_begin_cb
,
2943 .compound_end
= btr_compound_end_cb
,
2946 .get_sequence_length
= btr_get_sequence_length_cb
,
2947 .borrow_variant_field_type
= btr_borrow_variant_field_type_cb
,
2952 BT_ASSERT(medops
.request_bytes
);
2953 BT_ASSERT(medops
.borrow_stream
);
2954 BT_LOGD("Creating CTF plugin notification iterator: "
2955 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
2957 trace
, bt_trace_get_name(trace
), max_request_sz
, data
);
2958 notit
= g_new0(struct bt_notif_iter
, 1);
2960 BT_LOGE_STR("Failed to allocate one CTF plugin notification iterator.");
2963 notit
->clock_states
= g_hash_table_new_full(g_direct_hash
,
2964 g_direct_equal
, NULL
, g_free
);
2965 if (!notit
->clock_states
) {
2966 BT_LOGE_STR("Failed to allocate a GHashTable.");
2969 notit
->meta
.trace
= trace
;
2970 notit
->medium
.medops
= medops
;
2971 notit
->medium
.max_request_sz
= max_request_sz
;
2972 notit
->medium
.data
= data
;
2973 notit
->stack
= stack_new(notit
);
2974 if (!notit
->stack
) {
2975 BT_LOGE_STR("Failed to create field stack.");
2979 notit
->btr
= bt_btr_create(cbs
, notit
);
2981 BT_LOGE_STR("Failed to create binary type reader (BTR).");
2985 bt_notif_iter_reset(notit
);
2986 init_trace_field_path_cache(trace
, ¬it
->trace_field_path_cache
);
2987 notit
->sc_field_path_caches
= g_hash_table_new_full(g_direct_hash
,
2988 g_direct_equal
, NULL
, g_free
);
2989 if (!notit
->sc_field_path_caches
) {
2990 BT_LOGE_STR("Failed to allocate a GHashTable.");
2994 notit
->field_overrides
= g_hash_table_new_full(g_direct_hash
,
2995 g_direct_equal
, NULL
, g_free
);
2996 if (!notit
->field_overrides
) {
2997 BT_LOGE_STR("Failed to allocate a GHashTable.");
3001 BT_LOGD("Created CTF plugin notification iterator: "
3002 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
3003 "data=%p, notit-addr=%p",
3004 trace
, bt_trace_get_name(trace
), max_request_sz
, data
,
3006 notit
->cur_packet_offset
= 0;
3012 bt_notif_iter_destroy(notit
);
3017 void bt_notif_iter_destroy(struct bt_notif_iter
*notit
)
3019 BT_PUT(notit
->packet
);
3020 BT_PUT(notit
->stream
);
3021 BT_PUT(notit
->meta
.cc_prio_map
);
3022 release_all_dscopes(notit
);
3024 BT_LOGD("Destroying CTF plugin notification iterator: addr=%p", notit
);
3027 BT_LOGD_STR("Destroying field stack.");
3028 stack_destroy(notit
->stack
);
3032 BT_LOGD("Destroying BTR: btr-addr=%p", notit
->btr
);
3033 bt_btr_destroy(notit
->btr
);
3036 if (notit
->clock_states
) {
3037 g_hash_table_destroy(notit
->clock_states
);
3040 if (notit
->sc_field_path_caches
) {
3041 g_hash_table_destroy(notit
->sc_field_path_caches
);
3044 if (notit
->field_overrides
) {
3045 g_hash_table_destroy(notit
->field_overrides
);
3051 enum bt_notif_iter_status
bt_notif_iter_get_next_notification(
3052 struct bt_notif_iter
*notit
,
3053 struct bt_clock_class_priority_map
*cc_prio_map
,
3054 struct bt_graph
*graph
,
3055 struct bt_notification
**notification
)
3058 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
3061 BT_ASSERT(notification
);
3063 if (notit
->state
== STATE_DONE
) {
3064 status
= BT_NOTIF_ITER_STATUS_EOF
;
3068 if (cc_prio_map
!= notit
->meta
.cc_prio_map
) {
3069 bt_put(notit
->meta
.cc_prio_map
);
3070 notit
->meta
.cc_prio_map
= bt_get(cc_prio_map
);
3073 notit
->graph
= graph
;
3075 BT_LOGV("Getting next notification: notit-addr=%p, cc-prio-map-addr=%p",
3076 notit
, cc_prio_map
);
3079 status
= handle_state(notit
);
3080 if (status
== BT_NOTIF_ITER_STATUS_AGAIN
) {
3081 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_AGAIN.");
3085 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
3086 if (status
== BT_NOTIF_ITER_STATUS_EOF
) {
3087 enum state next_state
= notit
->state
;
3089 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_EOF.");
3091 if (notit
->packet
) {
3092 notify_end_of_packet(notit
,
3095 notify_end_of_stream(notit
,
3097 next_state
= STATE_DONE
;
3100 if (!*notification
) {
3101 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3105 status
= BT_NOTIF_ITER_STATUS_OK
;
3106 notit
->state
= next_state
;
3108 BT_LOGW("Cannot handle state: "
3109 "notit-addr=%p, state=%s",
3110 notit
, state_string(notit
->state
));
3116 switch (notit
->state
) {
3117 case STATE_EMIT_NOTIF_NEW_STREAM
:
3118 /* notify_new_stream() logs errors */
3119 notify_new_stream(notit
, notification
);
3120 if (!*notification
) {
3121 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3123 notit
->stream_begin_emitted
= true;
3125 case STATE_EMIT_NOTIF_NEW_PACKET
:
3126 /* notify_new_packet() logs errors */
3127 notify_new_packet(notit
, notification
);
3128 if (!*notification
) {
3129 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3133 case STATE_EMIT_NOTIF_EVENT
:
3134 BT_ASSERT(notit
->event_notif
);
3135 ret
= set_event_clocks(notit
);
3137 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3141 *notification
= notit
->event_notif
;
3142 notit
->event_notif
= NULL
;
3144 case STATE_EMIT_NOTIF_END_OF_PACKET
:
3145 /* Update clock with timestamp_end field. */
3146 if (notit
->cur_timestamp_end
) {
3147 enum bt_btr_status btr_status
;
3149 btr_status
= update_clock(notit
,
3150 notit
->cur_timestamp_end
);
3151 if (btr_status
!= BT_BTR_STATUS_OK
) {
3152 BT_LOGW("Cannot update stream's clock value: "
3153 "notit-addr=%p", notit
);
3154 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3159 /* notify_end_of_packet() logs errors */
3160 notify_end_of_packet(notit
, notification
);
3161 if (!*notification
) {
3162 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3167 /* Non-emitting state: continue */
3177 enum bt_notif_iter_status
bt_notif_iter_borrow_packet_header_context_fields(
3178 struct bt_notif_iter
*notit
,
3179 struct bt_field
**packet_header_field
,
3180 struct bt_field
**packet_context_field
)
3183 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
3187 if (notit
->state
== STATE_EMIT_NOTIF_NEW_PACKET
) {
3188 /* We're already there */
3193 status
= handle_state(notit
);
3194 if (status
== BT_NOTIF_ITER_STATUS_AGAIN
) {
3195 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_AGAIN.");
3198 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
3199 if (status
== BT_NOTIF_ITER_STATUS_EOF
) {
3200 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_EOF.");
3202 BT_LOGW("Cannot handle state: "
3203 "notit-addr=%p, state=%s",
3204 notit
, state_string(notit
->state
));
3209 switch (notit
->state
) {
3210 case STATE_EMIT_NOTIF_NEW_PACKET
:
3212 * Packet header and context fields are
3213 * potentially decoded (or they don't exist).
3217 case STATE_EMIT_NOTIF_NEW_STREAM
:
3218 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
3219 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
3220 case STATE_AFTER_TRACE_PACKET_HEADER
:
3221 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
3222 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
3223 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
3224 /* Non-emitting state: continue */
3228 * We should never get past the
3229 * STATE_EMIT_NOTIF_NEW_PACKET state.
3231 BT_LOGF("Unexpected state: notit-addr=%p, state=%s",
3232 notit
, state_string(notit
->state
));
3238 ret
= set_current_packet_content_sizes(notit
);
3240 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3244 if (packet_header_field
) {
3245 *packet_header_field
= notit
->dscopes
.trace_packet_header
;
3248 if (packet_context_field
) {
3249 *packet_context_field
= notit
->dscopes
.stream_packet_context
;
3257 void bt_notif_iter_set_medops_data(struct bt_notif_iter
*notit
,
3261 notit
->medium
.data
= medops_data
;
3265 enum bt_notif_iter_status
bt_notif_iter_seek(
3266 struct bt_notif_iter
*notit
, off_t offset
)
3268 enum bt_notif_iter_status ret
= BT_NOTIF_ITER_STATUS_OK
;
3269 enum bt_notif_iter_medium_status medium_status
;
3273 BT_LOGE("Cannot seek to negative offset: offset=%jd", offset
);
3274 ret
= BT_NOTIF_ITER_STATUS_INVAL
;
3278 if (!notit
->medium
.medops
.seek
) {
3279 ret
= BT_NOTIF_ITER_STATUS_UNSUPPORTED
;
3280 BT_LOGD("Aborting seek as the iterator's underlying media does not implement seek support.");
3284 medium_status
= notit
->medium
.medops
.seek(
3285 BT_NOTIF_ITER_SEEK_WHENCE_SET
, offset
,
3286 notit
->medium
.data
);
3287 if (medium_status
!= BT_NOTIF_ITER_MEDIUM_STATUS_OK
) {
3288 if (medium_status
== BT_NOTIF_ITER_MEDIUM_STATUS_EOF
) {
3289 ret
= BT_NOTIF_ITER_STATUS_EOF
;
3291 ret
= BT_NOTIF_ITER_STATUS_ERROR
;
3296 bt_notif_iter_reset(notit
);
3297 notit
->cur_packet_offset
= offset
;
3303 off_t
bt_notif_iter_get_current_packet_offset(
3304 struct bt_notif_iter
*notit
)
3307 return notit
->cur_packet_offset
;
3311 off_t
bt_notif_iter_get_current_packet_size(
3312 struct bt_notif_iter
*notit
)
3315 return notit
->cur_packet_size
;