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 /* Clock value: clock class and raw value */
135 struct bt_clock_class
*clock_class
; /* Weak */
139 /* CTF notification iterator */
140 struct bt_notif_iter
{
144 /* Current notification iterator to create notifications (weak) */
145 struct bt_private_connection_private_notification_iterator
*notif_iter
;
148 * Current dynamic scope field pointer.
150 * This is set by read_dscope_begin_state() and contains the
151 * value of one of the pointers in `dscopes` below.
153 struct bt_field
*cur_dscope_field
;
155 /* Trace and classes (owned by this) */
157 struct bt_trace
*trace
;
158 struct bt_stream_class
*stream_class
;
159 struct bt_event_class
*event_class
;
162 /* Current packet header field wrapper (NULL if not created yet) */
163 struct bt_packet_header_field
*packet_header_field
;
165 /* Current packet header field wrapper (NULL if not created yet) */
166 struct bt_packet_context_field
*packet_context_field
;
168 /* Current event header field (NULL if not created yet) */
169 struct bt_event_header_field
*event_header_field
;
171 /* Current packet (NULL if not created yet) */
172 struct bt_packet
*packet
;
174 /* Current stream (NULL if not set yet) */
175 struct bt_stream
*stream
;
177 /* Current event (NULL if not created yet) */
178 struct bt_event
*event
;
180 /* Current event notification (NULL if not created yet) */
181 struct bt_notification
*event_notif
;
184 * Current `timestamp_end` field (to consider before switching
185 * packets). If it's set, it's a field which is within
186 * `dscopes.stream_packet_context` below, which is in `packet`
189 struct bt_field
*cur_timestamp_end
;
191 /* Database of current dynamic scopes (owned by this) */
193 struct bt_field
*trace_packet_header
;
194 struct bt_field
*stream_packet_context
;
195 struct bt_field
*stream_event_header
;
196 struct bt_field
*stream_event_context
;
197 struct bt_field
*event_context
;
198 struct bt_field
*event_payload
;
202 * Special field overrides.
204 * Overrides are used to implement the behaviours of special fields such
205 * as "timestamp_end" (which must be ignored until the end of the
206 * packet), "id" (event id) which can be present multiple times and must
207 * be updated multiple time.
209 * This should be used to implement the behaviour of integer fields
210 * mapped to clocks and other "tagged" fields (in CTF 2).
212 * bt_field_type to struct field_cb_override
214 GHashTable
*field_overrides
;
219 /* Current medium buffer data */
221 /* Last address provided by medium */
224 /* Buffer size provided by medium (bytes) */
227 /* Offset within whole packet of addr (bits) */
228 size_t packet_offset
;
230 /* Current position from addr (bits) */
233 /* Position of the last event header from addr (bits) */
237 /* Binary type reader */
240 /* Current medium data */
242 struct bt_notif_iter_medium_ops medops
;
243 size_t max_request_sz
;
247 /* Stream beginning was emitted */
248 bool stream_begin_emitted
;
250 /* Current packet size (bits) (-1 if unknown) */
251 int64_t cur_packet_size
;
253 /* Current content size (bits) (-1 if unknown) */
254 int64_t cur_content_size
;
256 /* Current packet default beginning and end clock values */
257 struct clock_value cur_packet_begin_cv
;
258 struct clock_value cur_packet_end_cv
;
261 * Offset, in the underlying media, of the current packet's start
264 off_t cur_packet_offset
;
266 /* bt_clock_class to uint64_t. */
267 GHashTable
*clock_states
;
270 * Cache of the trace-constant field paths (event header type)
271 * associated to the current trace.
273 struct trace_field_path_cache trace_field_path_cache
;
276 * Field path cache associated with the current stream class.
277 * Ownership of this structure belongs to the field_path_caches HT.
279 struct stream_class_field_path_cache
*cur_sc_field_path_cache
;
281 /* bt_stream_class to struct stream_class_field_path_cache. */
282 GHashTable
*sc_field_path_caches
;
286 const char *state_string(enum state state
)
291 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
292 return "STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN";
293 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
294 return "STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE";
295 case STATE_AFTER_TRACE_PACKET_HEADER
:
296 return "STATE_AFTER_TRACE_PACKET_HEADER";
297 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
298 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN";
299 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
300 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE";
301 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
302 return "STATE_AFTER_STREAM_PACKET_CONTEXT";
303 case STATE_EMIT_NOTIF_NEW_PACKET
:
304 return "STATE_EMIT_NOTIF_NEW_PACKET";
305 case STATE_EMIT_NOTIF_NEW_STREAM
:
306 return "STATE_EMIT_NOTIF_NEW_STREAM";
307 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
:
308 return "STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN";
309 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
:
310 return "STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE";
311 case STATE_AFTER_STREAM_EVENT_HEADER
:
312 return "STATE_AFTER_STREAM_EVENT_HEADER";
313 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
:
314 return "STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN";
315 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
:
316 return "STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE";
317 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN
:
318 return "STATE_DSCOPE_EVENT_CONTEXT_BEGIN";
319 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
:
320 return "STATE_DSCOPE_EVENT_CONTEXT_CONTINUE";
321 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
322 return "STATE_DSCOPE_EVENT_PAYLOAD_BEGIN";
323 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
324 return "STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE";
325 case STATE_EMIT_NOTIF_EVENT
:
326 return "STATE_EMIT_NOTIF_EVENT";
327 case STATE_EMIT_NOTIF_END_OF_PACKET
:
328 return "STATE_EMIT_NOTIF_END_OF_PACKET";
331 case STATE_SKIP_PACKET_PADDING
:
332 return "STATE_SKIP_PACKET_PADDING";
339 int bt_notif_iter_switch_packet(struct bt_notif_iter
*notit
);
342 enum bt_btr_status
btr_timestamp_end_cb(void *value
,
343 struct bt_field_type
*type
, void *data
);
346 struct stack
*stack_new(struct bt_notif_iter
*notit
)
348 struct stack
*stack
= NULL
;
350 stack
= g_new0(struct stack
, 1);
352 BT_LOGE_STR("Failed to allocate one stack.");
356 stack
->entries
= g_array_new(FALSE
, TRUE
, sizeof(struct stack_entry
));
357 if (!stack
->entries
) {
358 BT_LOGE_STR("Failed to allocate a GArray.");
362 BT_LOGD("Created stack: notit-addr=%p, stack-addr=%p", notit
, stack
);
371 void stack_destroy(struct stack
*stack
)
374 BT_LOGD("Destroying stack: addr=%p", stack
);
376 if (stack
->entries
) {
377 g_array_free(stack
->entries
, TRUE
);
384 int stack_push(struct stack
*stack
, struct bt_field
*base
)
386 struct stack_entry
*entry
;
390 BT_LOGV("Pushing base field on stack: stack-addr=%p, "
391 "stack-size-before=%zu, stack-size-after=%zu",
392 stack
, stack
->size
, stack
->size
+ 1);
394 if (stack
->entries
->len
== stack
->size
) {
395 g_array_set_size(stack
->entries
, stack
->size
+ 1);
398 entry
= &g_array_index(stack
->entries
, struct stack_entry
, stack
->size
);
406 unsigned int stack_size(struct stack
*stack
)
413 void stack_pop(struct stack
*stack
)
416 BT_ASSERT(stack_size(stack
));
417 BT_LOGV("Popping from stack: "
418 "stack-addr=%p, stack-size-before=%zu, stack-size-after=%zu",
419 stack
, stack
->size
, stack
->size
- 1);
424 struct stack_entry
*stack_top(struct stack
*stack
)
427 BT_ASSERT(stack_size(stack
));
428 return &g_array_index(stack
->entries
, struct stack_entry
,
433 bool stack_empty(struct stack
*stack
)
435 return stack_size(stack
) == 0;
439 void stack_clear(struct stack
*stack
)
446 enum bt_notif_iter_status
notif_iter_status_from_m_status(
447 enum bt_notif_iter_medium_status m_status
)
449 return (int) m_status
;
453 size_t buf_size_bits(struct bt_notif_iter
*notit
)
455 return notit
->buf
.sz
* 8;
459 size_t buf_available_bits(struct bt_notif_iter
*notit
)
461 return buf_size_bits(notit
) - notit
->buf
.at
;
465 size_t packet_at(struct bt_notif_iter
*notit
)
467 return notit
->buf
.packet_offset
+ notit
->buf
.at
;
471 void buf_consume_bits(struct bt_notif_iter
*notit
, size_t incr
)
473 BT_LOGV("Advancing cursor: notit-addr=%p, cur-before=%zu, cur-after=%zu",
474 notit
, notit
->buf
.at
, notit
->buf
.at
+ incr
);
475 notit
->buf
.at
+= incr
;
479 enum bt_notif_iter_status
request_medium_bytes(
480 struct bt_notif_iter
*notit
)
482 uint8_t *buffer_addr
= NULL
;
483 size_t buffer_sz
= 0;
484 enum bt_notif_iter_medium_status m_status
;
486 BT_LOGV("Calling user function (request bytes): notit-addr=%p, "
487 "request-size=%zu", notit
, notit
->medium
.max_request_sz
);
488 m_status
= notit
->medium
.medops
.request_bytes(
489 notit
->medium
.max_request_sz
, &buffer_addr
,
490 &buffer_sz
, notit
->medium
.data
);
491 BT_LOGV("User function returned: status=%s, buf-addr=%p, buf-size=%zu",
492 bt_notif_iter_medium_status_string(m_status
),
493 buffer_addr
, buffer_sz
);
494 if (m_status
== BT_NOTIF_ITER_MEDIUM_STATUS_OK
) {
495 BT_ASSERT(buffer_sz
!= 0);
497 /* New packet offset is old one + old size (in bits) */
498 notit
->buf
.packet_offset
+= buf_size_bits(notit
);
500 /* Restart at the beginning of the new medium buffer */
502 notit
->buf
.last_eh_at
= SIZE_MAX
;
504 /* New medium buffer size */
505 notit
->buf
.sz
= buffer_sz
;
507 /* New medium buffer address */
508 notit
->buf
.addr
= buffer_addr
;
510 BT_LOGV("User function returned new bytes: "
511 "packet-offset=%zu, cur=%zu, size=%zu, addr=%p",
512 notit
->buf
.packet_offset
, notit
->buf
.at
,
513 notit
->buf
.sz
, notit
->buf
.addr
);
514 BT_LOGV_MEM(buffer_addr
, buffer_sz
, "Returned bytes at %p:",
516 } else if (m_status
== BT_NOTIF_ITER_MEDIUM_STATUS_EOF
) {
518 * User returned end of stream: validate that we're not
519 * in the middle of a packet header, packet context, or
522 if (notit
->cur_packet_size
>= 0) {
523 if (packet_at(notit
) == notit
->cur_packet_size
) {
527 if (packet_at(notit
) == 0) {
531 if (notit
->buf
.last_eh_at
!= SIZE_MAX
&&
532 notit
->buf
.at
== notit
->buf
.last_eh_at
) {
537 /* All other states are invalid */
538 BT_LOGW("User function returned %s, but notification iterator is in an unexpected state: "
539 "state=%s, cur-packet-size=%" PRId64
", cur=%zu, "
540 "packet-cur=%zu, last-eh-at=%zu",
541 bt_notif_iter_medium_status_string(m_status
),
542 state_string(notit
->state
),
543 notit
->cur_packet_size
,
544 notit
->buf
.at
, packet_at(notit
),
545 notit
->buf
.last_eh_at
);
546 m_status
= BT_NOTIF_ITER_MEDIUM_STATUS_ERROR
;
547 } else if (m_status
< 0) {
548 BT_LOGW("User function failed: status=%s",
549 bt_notif_iter_medium_status_string(m_status
));
553 return notif_iter_status_from_m_status(m_status
);
557 enum bt_notif_iter_status
buf_ensure_available_bits(
558 struct bt_notif_iter
*notit
)
560 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
562 if (buf_available_bits(notit
) == 0) {
564 * This _cannot_ return BT_NOTIF_ITER_STATUS_OK
567 status
= request_medium_bytes(notit
);
574 void reset_clock_value(struct clock_value
*cv
)
577 cv
->clock_class
= NULL
;
578 cv
->raw_value
= UINT64_C(-1);
582 enum bt_notif_iter_status
read_dscope_begin_state(
583 struct bt_notif_iter
*notit
,
584 struct bt_field_type
*dscope_field_type
,
585 enum state done_state
, enum state continue_state
,
586 struct bt_field
*dscope_field
)
588 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
589 enum bt_btr_status btr_status
;
590 size_t consumed_bits
;
592 notit
->cur_dscope_field
= dscope_field
;
593 BT_LOGV("Starting BTR: notit-addr=%p, btr-addr=%p, ft-addr=%p",
594 notit
, notit
->btr
, dscope_field_type
);
595 consumed_bits
= bt_btr_start(notit
->btr
, dscope_field_type
,
596 notit
->buf
.addr
, notit
->buf
.at
, packet_at(notit
),
597 notit
->buf
.sz
, &btr_status
);
598 BT_LOGV("BTR consumed bits: size=%zu", consumed_bits
);
600 switch (btr_status
) {
601 case BT_BTR_STATUS_OK
:
602 /* type was read completely */
603 BT_LOGV_STR("Field was completely decoded.");
604 notit
->state
= done_state
;
606 case BT_BTR_STATUS_EOF
:
607 BT_LOGV_STR("BTR needs more data to decode field completely.");
608 notit
->state
= continue_state
;
611 BT_LOGW("BTR failed to start: notit-addr=%p, btr-addr=%p, "
612 "status=%s", notit
, notit
->btr
,
613 bt_btr_status_string(btr_status
));
614 status
= BT_NOTIF_ITER_STATUS_ERROR
;
618 /* Consume bits now since we know we're not in an error state */
619 buf_consume_bits(notit
, consumed_bits
);
626 enum bt_notif_iter_status
read_dscope_continue_state(
627 struct bt_notif_iter
*notit
, enum state done_state
)
629 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
630 enum bt_btr_status btr_status
;
631 size_t consumed_bits
;
633 BT_LOGV("Continuing BTR: notit-addr=%p, btr-addr=%p",
636 status
= buf_ensure_available_bits(notit
);
637 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
639 BT_LOGW("Cannot ensure that buffer has at least one byte: "
640 "notif-addr=%p, status=%s",
641 notit
, bt_notif_iter_status_string(status
));
643 BT_LOGV("Cannot ensure that buffer has at least one byte: "
644 "notif-addr=%p, status=%s",
645 notit
, bt_notif_iter_status_string(status
));
652 consumed_bits
= bt_btr_continue(notit
->btr
, notit
->buf
.addr
,
653 notit
->buf
.sz
, &btr_status
);
654 BT_LOGV("BTR consumed bits: size=%zu", consumed_bits
);
656 switch (btr_status
) {
657 case BT_BTR_STATUS_OK
:
658 /* Type was read completely. */
659 BT_LOGV_STR("Field was completely decoded.");
660 notit
->state
= done_state
;
662 case BT_BTR_STATUS_EOF
:
663 /* Stay in this continue state. */
664 BT_LOGV_STR("BTR needs more data to decode field completely.");
667 BT_LOGW("BTR failed to continue: notit-addr=%p, btr-addr=%p, "
668 "status=%s", notit
, notit
->btr
,
669 bt_btr_status_string(btr_status
));
670 status
= BT_NOTIF_ITER_STATUS_ERROR
;
674 /* Consume bits now since we know we're not in an error state. */
675 buf_consume_bits(notit
, consumed_bits
);
681 void release_event_dscopes(struct bt_notif_iter
*notit
)
683 notit
->dscopes
.stream_event_header
= NULL
;
685 if (notit
->event_header_field
) {
686 bt_event_header_field_release(notit
->event_header_field
);
687 notit
->event_header_field
= NULL
;
690 notit
->dscopes
.stream_event_context
= NULL
;
691 notit
->dscopes
.event_context
= NULL
;
692 notit
->dscopes
.event_payload
= NULL
;
696 void release_all_dscopes(struct bt_notif_iter
*notit
)
698 notit
->dscopes
.trace_packet_header
= NULL
;
700 if (notit
->packet_header_field
) {
701 bt_packet_header_field_release(notit
->packet_header_field
);
702 notit
->packet_header_field
= NULL
;
705 notit
->dscopes
.stream_packet_context
= NULL
;
707 if (notit
->packet_context_field
) {
708 bt_packet_context_field_release(notit
->packet_context_field
);
709 notit
->packet_context_field
= NULL
;
712 release_event_dscopes(notit
);
716 enum bt_notif_iter_status
read_packet_header_begin_state(
717 struct bt_notif_iter
*notit
)
719 struct bt_field_type
*packet_header_type
= NULL
;
720 enum bt_notif_iter_status ret
= BT_NOTIF_ITER_STATUS_OK
;
722 if (bt_notif_iter_switch_packet(notit
)) {
723 BT_LOGW("Cannot switch packet: notit-addr=%p", notit
);
724 ret
= BT_NOTIF_ITER_STATUS_ERROR
;
728 /* Packet header type is common to the whole trace. */
729 packet_header_type
= bt_trace_borrow_packet_header_field_type(
731 if (!packet_header_type
) {
732 notit
->state
= STATE_AFTER_TRACE_PACKET_HEADER
;
737 * Create free packet header field from trace. This field is
738 * going to be moved to the packet once we create it. We cannot
739 * create the packet now because:
741 * 1. A packet is created from a stream.
742 * 2. A stream is created from a stream class.
743 * 3. We need the packet header field's content to know the ID
744 * of the stream class to select.
746 BT_ASSERT(!notit
->packet_header_field
);
747 notit
->packet_header_field
= bt_trace_create_packet_header_field(
749 if (!notit
->packet_header_field
) {
750 BT_LOGE_STR("Cannot create packet header field wrapper from trace.");
751 ret
= BT_NOTIF_ITER_STATUS_ERROR
;
755 notit
->dscopes
.trace_packet_header
=
756 bt_packet_header_field_borrow_field(notit
->packet_header_field
);
757 BT_ASSERT(notit
->dscopes
.trace_packet_header
);
758 BT_LOGV("Decoding packet header field:"
759 "notit-addr=%p, trace-addr=%p, trace-name=\"%s\", ft-addr=%p",
760 notit
, notit
->meta
.trace
,
761 bt_trace_get_name(notit
->meta
.trace
), packet_header_type
);
762 ret
= read_dscope_begin_state(notit
, packet_header_type
,
763 STATE_AFTER_TRACE_PACKET_HEADER
,
764 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
765 notit
->dscopes
.trace_packet_header
);
767 BT_LOGW("Cannot decode packet header field: "
768 "notit-addr=%p, trace-addr=%p, "
769 "trace-name=\"%s\", ft-addr=%p",
770 notit
, notit
->meta
.trace
,
771 bt_trace_get_name(notit
->meta
.trace
),
780 enum bt_notif_iter_status
read_packet_header_continue_state(
781 struct bt_notif_iter
*notit
)
783 return read_dscope_continue_state(notit
,
784 STATE_AFTER_TRACE_PACKET_HEADER
);
788 struct stream_class_field_path_cache
*
789 create_stream_class_field_path_cache_entry(
790 struct bt_notif_iter
*notit
,
791 struct bt_stream_class
*stream_class
)
795 int timestamp_end
= -1;
796 int packet_size
= -1;
797 int content_size
= -1;
798 struct stream_class_field_path_cache
*cache_entry
= g_new0(
799 struct stream_class_field_path_cache
, 1);
800 struct bt_field_type
*event_header
= NULL
, *packet_context
= NULL
;
803 BT_LOGE_STR("Failed to allocate one stream class field path cache.");
807 event_header
= bt_stream_class_borrow_event_header_field_type(
809 if (event_header
&& bt_field_type_is_structure(event_header
)) {
812 count
= bt_field_type_structure_get_field_count(
814 BT_ASSERT(count
>= 0);
816 for (i
= 0; i
< count
; i
++) {
820 ret
= bt_field_type_structure_borrow_field_by_index(
821 event_header
, &name
, NULL
, i
);
823 BT_LOGE("Cannot get event header structure field type's field: "
824 "notit-addr=%p, stream-class-addr=%p, "
825 "stream-class-name=\"%s\", "
826 "stream-class-id=%" PRId64
", "
827 "ft-addr=%p, index=%d",
829 bt_stream_class_get_name(stream_class
),
830 bt_stream_class_get_id(stream_class
),
835 if (v
!= -1 && id
!= -1) {
839 if (v
== -1 && strcmp(name
, "v") == 0) {
841 } else if (id
== -1 && !strcmp(name
, "id")) {
847 packet_context
= bt_stream_class_borrow_packet_context_field_type(
849 if (packet_context
&& bt_field_type_is_structure(packet_context
)) {
852 count
= bt_field_type_structure_get_field_count(
854 BT_ASSERT(count
>= 0);
856 for (i
= 0; i
< count
; i
++) {
859 struct bt_field_type
*field_type
;
861 if (timestamp_end
!= -1 && packet_size
!= -1 &&
862 content_size
!= -1) {
866 ret
= bt_field_type_structure_borrow_field_by_index(
867 packet_context
, &name
, &field_type
, i
);
869 BT_LOGE("Cannot get packet context structure field type's field: "
870 "notit-addr=%p, stream-class-addr=%p, "
871 "stream-class-name=\"%s\", "
872 "stream-class-id=%" PRId64
", "
873 "ft-addr=%p, index=%d",
875 bt_stream_class_get_name(stream_class
),
876 bt_stream_class_get_id(stream_class
),
881 if (timestamp_end
== -1 &&
882 strcmp(name
, "timestamp_end") == 0) {
883 struct field_cb_override
*override
= g_new0(
884 struct field_cb_override
, 1);
890 override
->func
= btr_timestamp_end_cb
;
891 override
->data
= notit
;
892 g_hash_table_insert(notit
->field_overrides
,
893 field_type
, override
);
895 } else if (packet_size
== -1 &&
896 !strcmp(name
, "packet_size")) {
898 } else if (content_size
== -1 &&
899 !strcmp(name
, "content_size")) {
906 cache_entry
->id
= id
;
907 cache_entry
->timestamp_end
= timestamp_end
;
908 cache_entry
->packet_size
= packet_size
;
909 cache_entry
->content_size
= content_size
;
921 struct stream_class_field_path_cache
*get_stream_class_field_path_cache(
922 struct bt_notif_iter
*notit
,
923 struct bt_stream_class
*stream_class
)
925 bool cache_entry_found
;
926 struct stream_class_field_path_cache
*cache_entry
;
928 cache_entry_found
= g_hash_table_lookup_extended(
929 notit
->sc_field_path_caches
,
930 stream_class
, NULL
, (gpointer
) &cache_entry
);
931 if (unlikely(!cache_entry_found
)) {
932 cache_entry
= create_stream_class_field_path_cache_entry(notit
,
934 g_hash_table_insert(notit
->sc_field_path_caches
,
935 stream_class
, (gpointer
) cache_entry
);
942 enum bt_notif_iter_status
set_current_stream_class(
943 struct bt_notif_iter
*notit
)
945 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
946 struct bt_field_type
*packet_header_type
= NULL
;
947 struct bt_field_type
*stream_id_field_type
= NULL
;
948 struct bt_stream_class
*new_stream_class
= NULL
;
951 /* Clear the current stream class field path cache. */
952 notit
->cur_sc_field_path_cache
= NULL
;
954 /* Is there any "stream_id" field in the packet header? */
955 packet_header_type
= bt_trace_borrow_packet_header_field_type(
957 if (!packet_header_type
) {
959 * No packet header, therefore no `stream_id` field,
960 * therefore only one stream class.
962 goto single_stream_class
;
965 BT_ASSERT(bt_field_type_is_structure(packet_header_type
));
968 stream_id_field_type
=
969 bt_field_type_structure_borrow_field_type_by_name(
970 packet_header_type
, "stream_id");
971 if (stream_id_field_type
) {
972 /* Find appropriate stream class using current stream ID */
974 struct bt_field
*stream_id_field
= NULL
;
976 BT_ASSERT(notit
->dscopes
.trace_packet_header
);
979 stream_id_field
= bt_field_structure_borrow_field_by_name(
980 notit
->dscopes
.trace_packet_header
, "stream_id");
981 BT_ASSERT(stream_id_field
);
982 ret
= bt_field_integer_unsigned_get_value(
983 stream_id_field
, &stream_id
);
987 /* Only one stream: pick the first stream class */
988 BT_ASSERT(bt_trace_get_stream_class_count(
989 notit
->meta
.trace
) == 1);
993 BT_LOGV("Found stream class ID to use: notit-addr=%p, "
994 "stream-class-id=%" PRIu64
", "
995 "trace-addr=%p, trace-name=\"%s\"",
996 notit
, stream_id
, notit
->meta
.trace
,
997 bt_trace_get_name(notit
->meta
.trace
));
999 new_stream_class
= bt_trace_borrow_stream_class_by_id(
1000 notit
->meta
.trace
, stream_id
);
1001 if (!new_stream_class
) {
1002 BT_LOGW("No stream class with ID of stream class ID to use in trace: "
1003 "notit-addr=%p, stream-class-id=%" PRIu64
", "
1004 "trace-addr=%p, trace-name=\"%s\"",
1005 notit
, stream_id
, notit
->meta
.trace
,
1006 bt_trace_get_name(notit
->meta
.trace
));
1007 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1011 if (notit
->meta
.stream_class
) {
1012 if (new_stream_class
!= notit
->meta
.stream_class
) {
1013 BT_LOGW("Two packets refer to two different stream classes within the same packet sequence: "
1014 "notit-addr=%p, prev-stream-class-addr=%p, "
1015 "prev-stream-class-name=\"%s\", "
1016 "prev-stream-class-id=%" PRId64
", "
1017 "next-stream-class-addr=%p, "
1018 "next-stream-class-name=\"%s\", "
1019 "next-stream-class-id=%" PRId64
", "
1020 "trace-addr=%p, trace-name=\"%s\"",
1021 notit
, notit
->meta
.stream_class
,
1022 bt_stream_class_get_name(notit
->meta
.stream_class
),
1023 bt_stream_class_get_id(notit
->meta
.stream_class
),
1025 bt_stream_class_get_name(new_stream_class
),
1026 bt_stream_class_get_id(new_stream_class
),
1028 bt_trace_get_name(notit
->meta
.trace
));
1029 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1033 notit
->meta
.stream_class
= new_stream_class
;
1036 BT_LOGV("Set current stream class: "
1037 "notit-addr=%p, stream-class-addr=%p, "
1038 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
1039 notit
, notit
->meta
.stream_class
,
1040 bt_stream_class_get_name(notit
->meta
.stream_class
),
1041 bt_stream_class_get_id(notit
->meta
.stream_class
));
1044 * Retrieve (or lazily create) the current stream class field path
1047 notit
->cur_sc_field_path_cache
= get_stream_class_field_path_cache(
1048 notit
, notit
->meta
.stream_class
);
1049 if (!notit
->cur_sc_field_path_cache
) {
1050 BT_LOGW("Cannot retrieve stream class field path from cache: "
1051 "notit-addr=%p, stream-class-addr=%p, "
1052 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
1053 notit
, notit
->meta
.stream_class
,
1054 bt_stream_class_get_name(notit
->meta
.stream_class
),
1055 bt_stream_class_get_id(notit
->meta
.stream_class
));
1056 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1065 uint64_t get_cur_stream_instance_id(struct bt_notif_iter
*notit
)
1067 struct bt_field
*stream_instance_id_field
= NULL
;
1068 uint64_t stream_instance_id
= -1ULL;
1071 if (!notit
->dscopes
.trace_packet_header
) {
1075 stream_instance_id_field
= bt_field_structure_borrow_field_by_name(
1076 notit
->dscopes
.trace_packet_header
, "stream_instance_id");
1077 if (!stream_instance_id_field
) {
1081 ret
= bt_field_integer_unsigned_get_value(stream_instance_id_field
,
1082 &stream_instance_id
);
1084 stream_instance_id
= -1ULL;
1089 return stream_instance_id
;
1093 enum bt_notif_iter_status
set_current_stream(struct bt_notif_iter
*notit
)
1095 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1096 struct bt_stream
*stream
= NULL
;
1098 BT_LOGV("Calling user function (get stream): notit-addr=%p, "
1099 "stream-class-addr=%p, stream-class-name=\"%s\", "
1100 "stream-class-id=%" PRId64
,
1101 notit
, notit
->meta
.stream_class
,
1102 bt_stream_class_get_name(notit
->meta
.stream_class
),
1103 bt_stream_class_get_id(notit
->meta
.stream_class
));
1104 stream
= bt_get(notit
->medium
.medops
.borrow_stream(
1105 notit
->meta
.stream_class
, get_cur_stream_instance_id(notit
),
1106 notit
->medium
.data
));
1107 BT_LOGV("User function returned: stream-addr=%p", stream
);
1109 BT_LOGW_STR("User function failed to return a stream object for the given stream class.");
1110 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1114 if (notit
->stream
&& stream
!= notit
->stream
) {
1115 BT_LOGW("User function returned a different stream than the previous one for the same sequence of packets.");
1116 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1120 BT_MOVE(notit
->stream
, stream
);
1128 enum bt_notif_iter_status
set_current_packet(struct bt_notif_iter
*notit
)
1130 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1131 struct bt_packet
*packet
= NULL
;
1133 BT_LOGV("Creating packet for packet notification: "
1134 "notit-addr=%p", notit
);
1135 BT_LOGV("Creating packet from stream: "
1136 "notit-addr=%p, stream-addr=%p, "
1137 "stream-class-addr=%p, "
1138 "stream-class-name=\"%s\", "
1139 "stream-class-id=%" PRId64
,
1140 notit
, notit
->stream
, notit
->meta
.stream_class
,
1141 bt_stream_class_get_name(notit
->meta
.stream_class
),
1142 bt_stream_class_get_id(notit
->meta
.stream_class
));
1145 BT_ASSERT(notit
->stream
);
1146 packet
= bt_packet_create(notit
->stream
);
1148 BT_LOGE("Cannot create packet from stream: "
1149 "notit-addr=%p, stream-addr=%p, "
1150 "stream-class-addr=%p, "
1151 "stream-class-name=\"%s\", "
1152 "stream-class-id=%" PRId64
,
1153 notit
, notit
->stream
, notit
->meta
.stream_class
,
1154 bt_stream_class_get_name(notit
->meta
.stream_class
),
1155 bt_stream_class_get_id(notit
->meta
.stream_class
));
1163 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1166 BT_MOVE(notit
->packet
, packet
);
1171 enum bt_notif_iter_status
after_packet_header_state(
1172 struct bt_notif_iter
*notit
)
1174 enum bt_notif_iter_status status
;
1176 status
= set_current_stream_class(notit
);
1177 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
1181 notit
->state
= STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
;
1188 enum bt_notif_iter_status
read_packet_context_begin_state(
1189 struct bt_notif_iter
*notit
)
1191 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1192 struct bt_field_type
*packet_context_type
;
1194 BT_ASSERT(notit
->meta
.stream_class
);
1195 packet_context_type
= bt_stream_class_borrow_packet_context_field_type(
1196 notit
->meta
.stream_class
);
1197 if (!packet_context_type
) {
1198 BT_LOGV("No packet packet context field type in stream class: continuing: "
1199 "notit-addr=%p, stream-class-addr=%p, "
1200 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
1201 notit
, notit
->meta
.stream_class
,
1202 bt_stream_class_get_name(notit
->meta
.stream_class
),
1203 bt_stream_class_get_id(notit
->meta
.stream_class
));
1204 notit
->state
= STATE_AFTER_STREAM_PACKET_CONTEXT
;
1209 * Create free packet context field from stream class. This
1210 * field is going to be moved to the packet once we create it.
1211 * We cannot create the packet now because a packet is created
1212 * from a stream, and this API must be able to return the packet
1213 * header and context fields without creating a stream
1214 * (bt_notif_iter_get_packet_header_context_fields()).
1216 BT_ASSERT(!notit
->packet_context_field
);
1217 notit
->packet_context_field
=
1218 bt_stream_class_create_packet_context_field(
1219 notit
->meta
.stream_class
);
1220 if (!notit
->packet_context_field
) {
1221 BT_LOGE_STR("Cannot create packet context field wrapper from stream class.");
1222 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1226 notit
->dscopes
.stream_packet_context
=
1227 bt_packet_context_field_borrow_field(notit
->packet_context_field
);
1228 BT_ASSERT(notit
->dscopes
.stream_packet_context
);
1229 BT_LOGV("Decoding packet context field: "
1230 "notit-addr=%p, stream-class-addr=%p, "
1231 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1233 notit
, notit
->meta
.stream_class
,
1234 bt_stream_class_get_name(notit
->meta
.stream_class
),
1235 bt_stream_class_get_id(notit
->meta
.stream_class
),
1236 packet_context_type
);
1237 status
= read_dscope_begin_state(notit
, packet_context_type
,
1238 STATE_AFTER_STREAM_PACKET_CONTEXT
,
1239 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
1240 notit
->dscopes
.stream_packet_context
);
1242 BT_LOGW("Cannot decode packet context field: "
1243 "notit-addr=%p, stream-class-addr=%p, "
1244 "stream-class-name=\"%s\", "
1245 "stream-class-id=%" PRId64
", ft-addr=%p",
1246 notit
, notit
->meta
.stream_class
,
1247 bt_stream_class_get_name(notit
->meta
.stream_class
),
1248 bt_stream_class_get_id(notit
->meta
.stream_class
),
1249 packet_context_type
);
1257 enum bt_notif_iter_status
read_packet_context_continue_state(
1258 struct bt_notif_iter
*notit
)
1260 return read_dscope_continue_state(notit
,
1261 STATE_AFTER_STREAM_PACKET_CONTEXT
);
1265 uint64_t get_field_raw_clock_value(struct bt_field
*base_field
,
1266 const char *field_name
, struct bt_clock_class
**user_cc
)
1268 struct bt_field
*field
;
1269 struct bt_field_type
*ft
;
1270 struct bt_clock_class
*clock_class
= NULL
;
1271 uint64_t val
= UINT64_C(-1);
1274 field
= bt_field_structure_borrow_field_by_name(base_field
, field_name
);
1279 ft
= bt_field_borrow_type(field
);
1282 if (!bt_field_type_is_integer(ft
)) {
1286 clock_class
= bt_field_type_integer_borrow_mapped_clock_class(ft
);
1291 ret
= bt_field_integer_unsigned_get_value(field
, &val
);
1292 BT_ASSERT(ret
== 0);
1295 *user_cc
= clock_class
;
1300 void set_current_packet_begin_end(struct bt_notif_iter
*notit
)
1302 struct bt_clock_class
*clock_class
;
1305 if (!notit
->dscopes
.stream_packet_context
) {
1309 val
= get_field_raw_clock_value(notit
->dscopes
.stream_packet_context
,
1310 "timestamp_begin", &clock_class
);
1311 notit
->cur_packet_begin_cv
.clock_class
= clock_class
;
1312 notit
->cur_packet_begin_cv
.raw_value
= val
;
1313 val
= get_field_raw_clock_value(notit
->dscopes
.stream_packet_context
,
1314 "timestamp_end", &clock_class
);
1315 notit
->cur_packet_end_cv
.clock_class
= clock_class
;
1316 notit
->cur_packet_end_cv
.raw_value
= val
;
1323 enum bt_notif_iter_status
set_current_packet_content_sizes(
1324 struct bt_notif_iter
*notit
)
1326 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1327 struct bt_field
*packet_size_field
= NULL
;
1328 struct bt_field
*content_size_field
= NULL
;
1329 uint64_t content_size
= -1ULL, packet_size
= -1ULL;
1331 if (!notit
->dscopes
.stream_packet_context
) {
1335 packet_size_field
= bt_field_structure_borrow_field_by_name(
1336 notit
->dscopes
.stream_packet_context
, "packet_size");
1337 content_size_field
= bt_field_structure_borrow_field_by_name(
1338 notit
->dscopes
.stream_packet_context
, "content_size");
1339 if (packet_size_field
) {
1340 int ret
= bt_field_integer_unsigned_get_value(
1341 packet_size_field
, &packet_size
);
1343 BT_ASSERT(ret
== 0);
1344 if (packet_size
== 0) {
1345 BT_LOGW("Invalid packet size: packet context field indicates packet size is zero: "
1346 "notit-addr=%p, packet-context-field-addr=%p",
1347 notit
, notit
->dscopes
.stream_packet_context
);
1348 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1350 } else if ((packet_size
% 8) != 0) {
1351 BT_LOGW("Invalid packet size: packet context field indicates packet size is not a multiple of 8: "
1352 "notit-addr=%p, packet-context-field-addr=%p, "
1353 "packet-size=%" PRIu64
,
1354 notit
, notit
->dscopes
.stream_packet_context
,
1356 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1361 if (content_size_field
) {
1362 int ret
= bt_field_integer_unsigned_get_value(
1363 content_size_field
, &content_size
);
1365 BT_ASSERT(ret
== 0);
1367 content_size
= packet_size
;
1370 if (content_size
> packet_size
) {
1371 BT_LOGW("Invalid packet or content size: packet context field indicates content size is greater than packet size: "
1372 "notit-addr=%p, packet-context-field-addr=%p, "
1373 "packet-size=%" PRIu64
", content-size=%" PRIu64
,
1374 notit
, notit
->dscopes
.stream_packet_context
,
1375 packet_size
, content_size
);
1376 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1380 if (packet_size
!= -1ULL) {
1381 notit
->cur_packet_size
= packet_size
;
1384 * Use the content size as packet size indicator if the
1385 * packet size field is missing. This means there is no
1386 * padding in this stream.
1388 notit
->cur_packet_size
= content_size
;
1390 notit
->cur_content_size
= content_size
;
1391 BT_LOGV("Set current packet and content sizes: "
1392 "notit-addr=%p, packet-size=%" PRIu64
", content-size=%" PRIu64
,
1393 notit
, packet_size
, content_size
);
1399 enum bt_notif_iter_status
after_packet_context_state(
1400 struct bt_notif_iter
*notit
)
1402 enum bt_notif_iter_status status
;
1404 status
= set_current_packet_content_sizes(notit
);
1405 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
1409 set_current_packet_begin_end(notit
);
1411 if (notit
->stream_begin_emitted
) {
1412 notit
->state
= STATE_EMIT_NOTIF_NEW_PACKET
;
1414 notit
->state
= STATE_EMIT_NOTIF_NEW_STREAM
;
1422 enum bt_notif_iter_status
read_event_header_begin_state(
1423 struct bt_notif_iter
*notit
)
1425 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1426 struct bt_field_type
*event_header_type
= NULL
;
1428 /* Reset the position of the last event header */
1429 notit
->buf
.last_eh_at
= notit
->buf
.at
;
1431 /* Check if we have some content left */
1432 if (notit
->cur_content_size
>= 0) {
1433 if (packet_at(notit
) == notit
->cur_content_size
) {
1434 /* No more events! */
1435 BT_LOGV("Reached end of packet: notit-addr=%p, "
1436 "cur=%zu", notit
, packet_at(notit
));
1437 notit
->state
= STATE_EMIT_NOTIF_END_OF_PACKET
;
1439 } else if (packet_at(notit
) > notit
->cur_content_size
) {
1440 /* That's not supposed to happen */
1441 BT_LOGV("Before decoding event header field: cursor is passed the packet's content: "
1442 "notit-addr=%p, content-size=%" PRId64
", "
1443 "cur=%zu", notit
, notit
->cur_content_size
,
1445 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1450 release_event_dscopes(notit
);
1451 BT_ASSERT(notit
->meta
.stream_class
);
1452 event_header_type
= bt_stream_class_borrow_event_header_field_type(
1453 notit
->meta
.stream_class
);
1454 if (!event_header_type
) {
1455 notit
->state
= STATE_AFTER_STREAM_EVENT_HEADER
;
1459 BT_ASSERT(!notit
->event_header_field
);
1460 notit
->event_header_field
= bt_stream_class_create_event_header_field(
1461 notit
->meta
.stream_class
);
1462 if (!notit
->event_header_field
) {
1463 BT_LOGE_STR("Cannot create event header field wrapper from trace.");
1464 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1468 notit
->dscopes
.stream_event_header
=
1469 bt_event_header_field_borrow_field(notit
->event_header_field
);
1470 BT_ASSERT(notit
->dscopes
.stream_event_header
);
1471 BT_LOGV("Decoding event header field: "
1472 "notit-addr=%p, stream-class-addr=%p, "
1473 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1475 notit
, notit
->meta
.stream_class
,
1476 bt_stream_class_get_name(notit
->meta
.stream_class
),
1477 bt_stream_class_get_id(notit
->meta
.stream_class
),
1479 status
= read_dscope_begin_state(notit
, event_header_type
,
1480 STATE_AFTER_STREAM_EVENT_HEADER
,
1481 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
,
1482 notit
->dscopes
.stream_event_header
);
1484 BT_LOGW("Cannot decode event header field: "
1485 "notit-addr=%p, stream-class-addr=%p, "
1486 "stream-class-name=\"%s\", "
1487 "stream-class-id=%" PRId64
", ft-addr=%p",
1488 notit
, notit
->meta
.stream_class
,
1489 bt_stream_class_get_name(notit
->meta
.stream_class
),
1490 bt_stream_class_get_id(notit
->meta
.stream_class
),
1499 enum bt_notif_iter_status
read_event_header_continue_state(
1500 struct bt_notif_iter
*notit
)
1502 return read_dscope_continue_state(notit
,
1503 STATE_AFTER_STREAM_EVENT_HEADER
);
1507 enum bt_notif_iter_status
set_current_event_class(struct bt_notif_iter
*notit
)
1510 * The assert() calls in this function are okay because it is
1511 * assumed here that all the metadata objects have been
1512 * validated for CTF correctness before decoding actual streams.
1515 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1516 struct bt_field_type
*event_header_type
;
1517 struct bt_field_type
*id_field_type
= NULL
;
1518 struct bt_field_type
*v_field_type
= NULL
;
1519 uint64_t event_id
= -1ULL;
1522 event_header_type
= bt_stream_class_borrow_event_header_field_type(
1523 notit
->meta
.stream_class
);
1524 if (!event_header_type
) {
1526 * No event header, therefore no event class ID field,
1527 * therefore only one event class.
1529 goto single_event_class
;
1532 /* Is there any "id"/"v" field in the event header? */
1533 BT_ASSERT(bt_field_type_is_structure(event_header_type
));
1534 id_field_type
= bt_field_type_structure_borrow_field_type_by_name(
1535 event_header_type
, "id");
1536 v_field_type
= bt_field_type_structure_borrow_field_type_by_name(
1537 event_header_type
, "v");
1538 BT_ASSERT(notit
->dscopes
.stream_event_header
);
1542 * | | |_ _|_ _| __ __ _
1543 * | | | | | || '_ \ / _` |
1544 * | |___| | | || | | | (_| | S P E C I A L
1545 * |_____|_| |_||_| |_|\__, | C A S E â„¢
1548 struct bt_field
*v_field
= NULL
;
1549 struct bt_field
*v_struct_field
= NULL
;
1550 struct bt_field
*v_struct_id_field
= NULL
;
1552 // TODO: optimalize!
1553 v_field
= bt_field_structure_borrow_field_by_name(
1554 notit
->dscopes
.stream_event_header
, "v");
1558 bt_field_variant_borrow_current_field(v_field
);
1559 if (!v_struct_field
) {
1560 goto end_v_field_type
;
1563 // TODO: optimalize!
1564 v_struct_id_field
= bt_field_structure_borrow_field_by_name(
1565 v_struct_field
, "id");
1566 if (!v_struct_id_field
) {
1567 goto end_v_field_type
;
1570 if (bt_field_is_integer(v_struct_id_field
)) {
1571 ret
= bt_field_integer_unsigned_get_value(
1572 v_struct_id_field
, &event_id
);
1574 BT_LOGV("Cannot get value of unsigned integer field (`id`): continuing: "
1575 "notit=%p, field-addr=%p",
1576 notit
, v_struct_id_field
);
1583 if (id_field_type
&& event_id
== -1ULL) {
1584 /* Check "id" field */
1585 struct bt_field
*id_field
= NULL
;
1586 int ret_get_value
= 0;
1588 // TODO: optimalize!
1589 id_field
= bt_field_structure_borrow_field_by_name(
1590 notit
->dscopes
.stream_event_header
, "id");
1592 goto check_event_id
;
1595 ret_get_value
= bt_field_integer_unsigned_get_value(
1596 id_field
, &event_id
);
1597 BT_ASSERT(ret_get_value
== 0);
1601 if (event_id
== -1ULL) {
1603 /* Event ID not found: single event? */
1604 BT_ASSERT(bt_stream_class_get_event_class_count(
1605 notit
->meta
.stream_class
) == 1);
1609 BT_LOGV("Found event class ID to use: notit-addr=%p, "
1610 "stream-class-addr=%p, stream-class-name=\"%s\", "
1611 "stream-class-id=%" PRId64
", "
1612 "event-class-id=%" PRIu64
,
1613 notit
, notit
->meta
.stream_class
,
1614 bt_stream_class_get_name(notit
->meta
.stream_class
),
1615 bt_stream_class_get_id(notit
->meta
.stream_class
),
1617 notit
->meta
.event_class
= bt_stream_class_borrow_event_class_by_id(
1618 notit
->meta
.stream_class
, event_id
);
1619 if (!notit
->meta
.event_class
) {
1620 BT_LOGW("No event class with ID of event class ID to use in stream class: "
1621 "notit-addr=%p, stream-class-addr=%p, "
1622 "stream-class-name=\"%s\", "
1623 "stream-class-id=%" PRId64
", "
1624 "event-class-id=%" PRIu64
,
1625 notit
, notit
->meta
.stream_class
,
1626 bt_stream_class_get_name(notit
->meta
.stream_class
),
1627 bt_stream_class_get_id(notit
->meta
.stream_class
),
1629 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1633 BT_LOGV("Set current event class: "
1634 "notit-addr=%p, event-class-addr=%p, "
1635 "event-class-name=\"%s\", event-class-id=%" PRId64
,
1636 notit
, notit
->meta
.event_class
,
1637 bt_event_class_get_name(notit
->meta
.event_class
),
1638 bt_event_class_get_id(notit
->meta
.event_class
));
1645 enum bt_notif_iter_status
set_current_event_notification(
1646 struct bt_notif_iter
*notit
)
1648 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1649 struct bt_notification
*notif
= NULL
;
1651 BT_ASSERT(notit
->meta
.event_class
);
1652 BT_ASSERT(notit
->packet
);
1653 BT_LOGV("Creating event notification from event class and packet: "
1654 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", packet-addr=%p",
1655 notit
, notit
->meta
.event_class
,
1656 bt_event_class_get_name(notit
->meta
.event_class
),
1658 BT_ASSERT(notit
->notif_iter
);
1659 notif
= bt_notification_event_create(notit
->notif_iter
,
1660 notit
->meta
.event_class
, notit
->packet
);
1662 BT_LOGE("Cannot create event notification: "
1663 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", "
1665 notit
, notit
->meta
.event_class
,
1666 bt_event_class_get_name(notit
->meta
.event_class
),
1675 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1678 BT_MOVE(notit
->event_notif
, notif
);
1683 enum bt_notif_iter_status
after_event_header_state(
1684 struct bt_notif_iter
*notit
)
1686 enum bt_notif_iter_status status
;
1688 status
= set_current_event_class(notit
);
1689 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
1693 status
= set_current_event_notification(notit
);
1694 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
1698 notit
->event
= bt_notification_event_borrow_event(notit
->event_notif
);
1699 BT_ASSERT(notit
->event
);
1701 if (notit
->event_header_field
) {
1704 BT_ASSERT(notit
->event
);
1705 ret
= bt_event_move_header(notit
->event
,
1706 notit
->event_header_field
);
1709 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1713 notit
->event_header_field
= NULL
;
1716 * At this point notit->dscopes.stream_event_header has
1717 * the same value as the event header field within
1720 BT_ASSERT(bt_event_borrow_header(notit
->event
) ==
1721 notit
->dscopes
.stream_event_header
);
1724 notit
->state
= STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
;
1731 enum bt_notif_iter_status
read_stream_event_context_begin_state(
1732 struct bt_notif_iter
*notit
)
1734 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1735 struct bt_field_type
*stream_event_context_type
;
1737 stream_event_context_type
=
1738 bt_stream_class_borrow_event_context_field_type(
1739 notit
->meta
.stream_class
);
1740 if (!stream_event_context_type
) {
1741 notit
->state
= STATE_DSCOPE_EVENT_CONTEXT_BEGIN
;
1745 BT_ASSERT(!notit
->dscopes
.stream_event_context
);
1746 notit
->dscopes
.stream_event_context
=
1747 bt_event_borrow_stream_event_context(notit
->event
);
1748 BT_ASSERT(notit
->dscopes
.stream_event_context
);
1749 BT_LOGV("Decoding stream event context field: "
1750 "notit-addr=%p, stream-class-addr=%p, "
1751 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1753 notit
, notit
->meta
.stream_class
,
1754 bt_stream_class_get_name(notit
->meta
.stream_class
),
1755 bt_stream_class_get_id(notit
->meta
.stream_class
),
1756 stream_event_context_type
);
1757 status
= read_dscope_begin_state(notit
, stream_event_context_type
,
1758 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
,
1759 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
,
1760 notit
->dscopes
.stream_event_context
);
1762 BT_LOGW("Cannot decode stream event context field: "
1763 "notit-addr=%p, stream-class-addr=%p, "
1764 "stream-class-name=\"%s\", "
1765 "stream-class-id=%" PRId64
", ft-addr=%p",
1766 notit
, notit
->meta
.stream_class
,
1767 bt_stream_class_get_name(notit
->meta
.stream_class
),
1768 bt_stream_class_get_id(notit
->meta
.stream_class
),
1769 stream_event_context_type
);
1777 enum bt_notif_iter_status
read_stream_event_context_continue_state(
1778 struct bt_notif_iter
*notit
)
1780 return read_dscope_continue_state(notit
,
1781 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
);
1785 enum bt_notif_iter_status
read_event_context_begin_state(
1786 struct bt_notif_iter
*notit
)
1788 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1789 struct bt_field_type
*event_context_type
;
1791 event_context_type
= bt_event_class_borrow_context_field_type(
1792 notit
->meta
.event_class
);
1793 if (!event_context_type
) {
1794 notit
->state
= STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
;
1798 BT_ASSERT(!notit
->dscopes
.event_context
);
1799 notit
->dscopes
.event_context
= bt_event_borrow_context(notit
->event
);
1800 BT_ASSERT(notit
->dscopes
.event_context
);
1801 BT_LOGV("Decoding event context field: "
1802 "notit-addr=%p, event-class-addr=%p, "
1803 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1805 notit
, notit
->meta
.event_class
,
1806 bt_event_class_get_name(notit
->meta
.event_class
),
1807 bt_event_class_get_id(notit
->meta
.event_class
),
1808 event_context_type
);
1809 status
= read_dscope_begin_state(notit
, event_context_type
,
1810 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
1811 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
,
1812 notit
->dscopes
.event_context
);
1814 BT_LOGW("Cannot decode event context field: "
1815 "notit-addr=%p, event-class-addr=%p, "
1816 "event-class-name=\"%s\", "
1817 "event-class-id=%" PRId64
", ft-addr=%p",
1818 notit
, notit
->meta
.event_class
,
1819 bt_event_class_get_name(notit
->meta
.event_class
),
1820 bt_event_class_get_id(notit
->meta
.event_class
),
1821 event_context_type
);
1829 enum bt_notif_iter_status
read_event_context_continue_state(
1830 struct bt_notif_iter
*notit
)
1832 return read_dscope_continue_state(notit
,
1833 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
);
1837 enum bt_notif_iter_status
read_event_payload_begin_state(
1838 struct bt_notif_iter
*notit
)
1840 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1841 struct bt_field_type
*event_payload_type
;
1843 event_payload_type
= bt_event_class_borrow_payload_field_type(
1844 notit
->meta
.event_class
);
1845 if (!event_payload_type
) {
1846 notit
->state
= STATE_EMIT_NOTIF_EVENT
;
1850 BT_ASSERT(!notit
->dscopes
.event_payload
);
1851 notit
->dscopes
.event_payload
= bt_event_borrow_payload(notit
->event
);
1852 BT_ASSERT(notit
->dscopes
.event_payload
);
1853 BT_LOGV("Decoding event payload field: "
1854 "notit-addr=%p, event-class-addr=%p, "
1855 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1857 notit
, notit
->meta
.event_class
,
1858 bt_event_class_get_name(notit
->meta
.event_class
),
1859 bt_event_class_get_id(notit
->meta
.event_class
),
1860 event_payload_type
);
1861 status
= read_dscope_begin_state(notit
, event_payload_type
,
1862 STATE_EMIT_NOTIF_EVENT
,
1863 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
1864 notit
->dscopes
.event_payload
);
1866 BT_LOGW("Cannot decode event payload field: "
1867 "notit-addr=%p, event-class-addr=%p, "
1868 "event-class-name=\"%s\", "
1869 "event-class-id=%" PRId64
", ft-addr=%p",
1870 notit
, notit
->meta
.event_class
,
1871 bt_event_class_get_name(notit
->meta
.event_class
),
1872 bt_event_class_get_id(notit
->meta
.event_class
),
1873 event_payload_type
);
1881 enum bt_notif_iter_status
read_event_payload_continue_state(
1882 struct bt_notif_iter
*notit
)
1884 return read_dscope_continue_state(notit
, STATE_EMIT_NOTIF_EVENT
);
1888 enum bt_notif_iter_status
skip_packet_padding_state(
1889 struct bt_notif_iter
*notit
)
1891 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1892 size_t bits_to_skip
;
1894 BT_ASSERT(notit
->cur_packet_size
> 0);
1895 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1896 if (bits_to_skip
== 0) {
1897 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1900 size_t bits_to_consume
;
1902 BT_LOGV("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu",
1903 bits_to_skip
, notit
, bits_to_skip
);
1904 status
= buf_ensure_available_bits(notit
);
1905 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
1909 bits_to_consume
= MIN(buf_available_bits(notit
), bits_to_skip
);
1910 BT_LOGV("Skipping %zu bits of padding: notit-addr=%p, size=%zu",
1911 bits_to_consume
, notit
, bits_to_consume
);
1912 buf_consume_bits(notit
, bits_to_consume
);
1913 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1914 if (bits_to_skip
== 0) {
1915 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1925 enum bt_notif_iter_status
handle_state(struct bt_notif_iter
*notit
)
1927 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1928 const enum state state
= notit
->state
;
1930 BT_LOGV("Handling state: notit-addr=%p, state=%s",
1931 notit
, state_string(state
));
1933 // TODO: optimalize!
1936 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1938 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
1939 status
= read_packet_header_begin_state(notit
);
1941 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
1942 status
= read_packet_header_continue_state(notit
);
1944 case STATE_AFTER_TRACE_PACKET_HEADER
:
1945 status
= after_packet_header_state(notit
);
1947 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
1948 status
= read_packet_context_begin_state(notit
);
1950 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
1951 status
= read_packet_context_continue_state(notit
);
1953 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
1954 status
= after_packet_context_state(notit
);
1956 case STATE_EMIT_NOTIF_NEW_STREAM
:
1957 notit
->state
= STATE_EMIT_NOTIF_NEW_PACKET
;
1959 case STATE_EMIT_NOTIF_NEW_PACKET
:
1960 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1962 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
:
1963 status
= read_event_header_begin_state(notit
);
1965 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
:
1966 status
= read_event_header_continue_state(notit
);
1968 case STATE_AFTER_STREAM_EVENT_HEADER
:
1969 status
= after_event_header_state(notit
);
1971 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
:
1972 status
= read_stream_event_context_begin_state(notit
);
1974 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
:
1975 status
= read_stream_event_context_continue_state(notit
);
1977 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN
:
1978 status
= read_event_context_begin_state(notit
);
1980 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
:
1981 status
= read_event_context_continue_state(notit
);
1983 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
1984 status
= read_event_payload_begin_state(notit
);
1986 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
1987 status
= read_event_payload_continue_state(notit
);
1989 case STATE_EMIT_NOTIF_EVENT
:
1990 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1992 case STATE_SKIP_PACKET_PADDING
:
1993 status
= skip_packet_padding_state(notit
);
1995 case STATE_EMIT_NOTIF_END_OF_PACKET
:
1996 notit
->state
= STATE_SKIP_PACKET_PADDING
;
1999 BT_LOGD("Unknown CTF plugin notification iterator state: "
2000 "notit-addr=%p, state=%d", notit
, notit
->state
);
2004 BT_LOGV("Handled state: notit-addr=%p, status=%s, "
2005 "prev-state=%s, cur-state=%s",
2006 notit
, bt_notif_iter_status_string(status
),
2007 state_string(state
), state_string(notit
->state
));
2012 * Resets the internal state of a CTF notification iterator.
2015 void bt_notif_iter_reset(struct bt_notif_iter
*notit
)
2018 BT_LOGD("Resetting notification iterator: addr=%p", notit
);
2019 stack_clear(notit
->stack
);
2020 notit
->meta
.stream_class
= NULL
;
2021 notit
->meta
.event_class
= NULL
;
2022 BT_PUT(notit
->packet
);
2023 BT_PUT(notit
->stream
);
2024 BT_PUT(notit
->event_notif
);
2025 release_all_dscopes(notit
);
2026 notit
->cur_dscope_field
= NULL
;
2028 if (notit
->packet_header_field
) {
2029 bt_packet_header_field_release(notit
->packet_header_field
);
2030 notit
->packet_header_field
= NULL
;
2033 if (notit
->packet_context_field
) {
2034 bt_packet_context_field_release(notit
->packet_context_field
);
2035 notit
->packet_context_field
= NULL
;
2038 if (notit
->event_header_field
) {
2039 bt_event_header_field_release(notit
->event_header_field
);
2040 notit
->event_header_field
= NULL
;
2043 notit
->buf
.addr
= NULL
;
2046 notit
->buf
.last_eh_at
= SIZE_MAX
;
2047 notit
->buf
.packet_offset
= 0;
2048 notit
->state
= STATE_INIT
;
2049 notit
->cur_content_size
= -1;
2050 notit
->cur_packet_size
= -1;
2051 notit
->cur_packet_offset
= -1;
2052 reset_clock_value(¬it
->cur_packet_begin_cv
);
2053 reset_clock_value(¬it
->cur_packet_end_cv
);
2054 notit
->stream_begin_emitted
= false;
2055 notit
->cur_timestamp_end
= NULL
;
2059 int bt_notif_iter_switch_packet(struct bt_notif_iter
*notit
)
2064 * We don't put the stream class here because we need to make
2065 * sure that all the packets processed by the same notification
2066 * iterator refer to the same stream class (the first one).
2070 if (notit
->cur_packet_size
!= -1) {
2071 notit
->cur_packet_offset
+= notit
->cur_packet_size
;
2074 BT_LOGV("Switching packet: notit-addr=%p, cur=%zu, "
2075 "packet-offset=%" PRId64
, notit
, notit
->buf
.at
,
2076 notit
->cur_packet_offset
);
2077 stack_clear(notit
->stack
);
2078 notit
->meta
.event_class
= NULL
;
2079 BT_PUT(notit
->packet
);
2080 BT_PUT(notit
->event_notif
);
2081 notit
->cur_timestamp_end
= NULL
;
2082 release_all_dscopes(notit
);
2083 notit
->cur_dscope_field
= NULL
;
2086 * Adjust current buffer so that addr points to the beginning of the new
2089 if (notit
->buf
.addr
) {
2090 size_t consumed_bytes
= (size_t) (notit
->buf
.at
/ CHAR_BIT
);
2092 /* Packets are assumed to start on a byte frontier. */
2093 if (notit
->buf
.at
% CHAR_BIT
) {
2094 BT_LOGW("Cannot switch packet: current position is not a multiple of 8: "
2095 "notit-addr=%p, cur=%zu", notit
, notit
->buf
.at
);
2100 notit
->buf
.addr
+= consumed_bytes
;
2101 notit
->buf
.sz
-= consumed_bytes
;
2103 notit
->buf
.packet_offset
= 0;
2104 BT_LOGV("Adjusted buffer: addr=%p, size=%zu",
2105 notit
->buf
.addr
, notit
->buf
.sz
);
2108 notit
->cur_content_size
= -1;
2109 notit
->cur_packet_size
= -1;
2110 reset_clock_value(¬it
->cur_packet_begin_cv
);
2111 reset_clock_value(¬it
->cur_packet_end_cv
);
2112 notit
->cur_sc_field_path_cache
= NULL
;
2119 struct bt_field
*borrow_next_field(struct bt_notif_iter
*notit
)
2121 struct bt_field
*next_field
= NULL
;
2122 struct bt_field
*base_field
;
2123 struct bt_field_type
*base_type
;
2126 BT_ASSERT(!stack_empty(notit
->stack
));
2127 index
= stack_top(notit
->stack
)->index
;
2128 base_field
= stack_top(notit
->stack
)->base
;
2129 BT_ASSERT(base_field
);
2130 base_type
= bt_field_borrow_type(base_field
);
2131 BT_ASSERT(base_type
);
2133 switch (bt_field_type_get_type_id(base_type
)) {
2134 case BT_FIELD_TYPE_ID_STRUCT
:
2136 next_field
= bt_field_structure_borrow_field_by_index(
2140 case BT_FIELD_TYPE_ID_ARRAY
:
2141 next_field
= bt_field_array_borrow_field(base_field
, index
);
2143 case BT_FIELD_TYPE_ID_SEQUENCE
:
2144 next_field
= bt_field_sequence_borrow_field(base_field
, index
);
2146 case BT_FIELD_TYPE_ID_VARIANT
:
2147 next_field
= bt_field_variant_borrow_current_field(base_field
);
2150 BT_LOGF("Unknown base field type ID: "
2151 "notit-addr=%p, ft-addr=%p, ft-id=%s",
2153 bt_common_field_type_id_string(
2154 bt_field_type_get_type_id(base_type
)));
2162 void update_clock_state(uint64_t *state
, struct bt_field
*value_field
,
2163 struct bt_field_type
*value_type
)
2165 uint64_t requested_new_value
;
2166 uint64_t requested_new_value_mask
;
2167 uint64_t cur_value_masked
;
2168 int requested_new_value_size
;
2171 BT_ASSERT(value_type
);
2172 BT_ASSERT(bt_field_type_is_integer(value_type
));
2173 requested_new_value_size
=
2174 bt_field_type_integer_get_size(value_type
);
2175 BT_ASSERT(requested_new_value_size
> 0);
2176 ret
= bt_field_integer_unsigned_get_value(value_field
,
2177 &requested_new_value
);
2181 * Special case for a 64-bit new value, which is the limit
2182 * of a clock value as of this version: overwrite the
2183 * current value directly.
2185 if (requested_new_value_size
== 64) {
2186 *state
= requested_new_value
;
2190 requested_new_value_mask
= (1ULL << requested_new_value_size
) - 1;
2191 cur_value_masked
= *state
& requested_new_value_mask
;
2193 if (requested_new_value
< cur_value_masked
) {
2195 * It looks like a wrap happened on the number of bits
2196 * of the requested new value. Assume that the clock
2197 * value wrapped only one time.
2199 *state
+= requested_new_value_mask
+ 1;
2202 /* Clear the low bits of the current clock value. */
2203 *state
&= ~requested_new_value_mask
;
2205 /* Set the low bits of the current clock value. */
2206 *state
|= requested_new_value
;
2209 BT_LOGV("Updated clock's value from integer field's value: "
2210 "value=%" PRIu64
, *state
);
2214 enum bt_btr_status
update_clock(struct bt_notif_iter
*notit
,
2215 struct bt_field
*int_field
)
2217 gboolean clock_class_found
;
2218 uint64_t *clock_state
= NULL
;
2219 struct bt_field_type
*int_field_type
= NULL
;
2220 enum bt_btr_status ret
= BT_BTR_STATUS_OK
;
2221 struct bt_clock_class
*clock_class
= NULL
;
2223 int_field_type
= bt_field_borrow_type(int_field
);
2224 BT_ASSERT(int_field_type
);
2226 if (bt_field_type_is_enumeration(int_field_type
)) {
2228 bt_field_type_enumeration_borrow_container_field_type(
2232 clock_class
= bt_field_type_integer_borrow_mapped_clock_class(
2234 if (likely(!clock_class
)) {
2238 clock_class_found
= g_hash_table_lookup_extended(notit
->clock_states
,
2239 clock_class
, NULL
, (gpointer
) &clock_state
);
2240 if (!clock_class_found
) {
2241 clock_state
= g_new0(uint64_t, 1);
2243 BT_LOGE_STR("Failed to allocate a uint64_t.");
2244 ret
= BT_BTR_STATUS_ENOMEM
;
2248 g_hash_table_insert(notit
->clock_states
, clock_class
,
2252 /* Update the clock's state. */
2253 BT_LOGV("Updating notification iterator's clock's value from integer field: "
2254 "notit-addr=%p, clock-class-addr=%p, "
2255 "clock-class-name=\"%s\", value=%" PRIu64
,
2257 bt_clock_class_get_name(clock_class
), *clock_state
);
2258 update_clock_state(clock_state
, int_field
, int_field_type
);
2265 enum bt_btr_status
btr_unsigned_int_common(uint64_t value
,
2266 struct bt_field_type
*type
, void *data
,
2267 struct bt_field
**out_field
)
2269 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2270 struct bt_field
*field
= NULL
;
2271 struct bt_notif_iter
*notit
= data
;
2274 BT_LOGV("Common unsigned integer function called from BTR: "
2275 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2276 "ft-id=%s, value=%" PRIu64
,
2277 notit
, notit
->btr
, type
,
2278 bt_common_field_type_id_string(
2279 bt_field_type_get_type_id(type
)),
2281 field
= borrow_next_field(notit
);
2283 BT_LOGW("Cannot borrow next field: notit-addr=%p", notit
);
2284 status
= BT_BTR_STATUS_ERROR
;
2288 BT_ASSERT(bt_field_is_integer(field
) || bt_field_is_enumeration(field
));
2289 ret
= bt_field_integer_unsigned_set_value(field
, value
);
2290 BT_ASSERT(ret
== 0);
2291 stack_top(notit
->stack
)->index
++;
2299 enum bt_btr_status
btr_timestamp_end_cb(void *value
,
2300 struct bt_field_type
*type
, void *data
)
2302 enum bt_btr_status status
;
2303 struct bt_field
*field
= NULL
;
2304 struct bt_notif_iter
*notit
= data
;
2305 uint64_t uvalue
= *((uint64_t *) value
);
2307 BT_LOGV("`timestamp_end` unsigned integer function called from BTR: "
2308 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2310 notit
, notit
->btr
, type
,
2311 bt_common_field_type_id_string(
2312 bt_field_type_get_type_id(type
)));
2314 status
= btr_unsigned_int_common(uvalue
, type
, data
, &field
);
2316 /* Set as the current packet's end timestamp field */
2317 notit
->cur_timestamp_end
= field
;
2322 enum bt_btr_status
btr_unsigned_int_cb(uint64_t value
,
2323 struct bt_field_type
*type
, void *data
)
2325 struct bt_notif_iter
*notit
= data
;
2326 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2327 struct bt_field
*field
= NULL
;
2328 struct field_cb_override
*override
;
2330 BT_LOGV("Unsigned integer function called from BTR: "
2331 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2332 "ft-id=%s, value=%" PRIu64
,
2333 notit
, notit
->btr
, type
,
2334 bt_common_field_type_id_string(
2335 bt_field_type_get_type_id(type
)),
2337 override
= g_hash_table_lookup(notit
->field_overrides
, type
);
2338 if (unlikely(override
)) {
2339 /* Override function logs errors */
2340 status
= override
->func(&value
, type
, override
->data
);
2344 status
= btr_unsigned_int_common(value
, type
, data
, &field
);
2345 if (status
!= BT_BTR_STATUS_OK
) {
2346 /* btr_unsigned_int_common() logs errors */
2350 status
= update_clock(notit
, field
);
2357 enum bt_btr_status
btr_signed_int_cb(int64_t value
,
2358 struct bt_field_type
*type
, void *data
)
2360 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2361 struct bt_field
*field
= NULL
;
2362 struct bt_notif_iter
*notit
= data
;
2365 BT_LOGV("Signed integer function called from BTR: "
2366 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2367 "ft-id=%s, value=%" PRId64
,
2368 notit
, notit
->btr
, type
,
2369 bt_common_field_type_id_string(
2370 bt_field_type_get_type_id(type
)),
2372 field
= borrow_next_field(notit
);
2374 BT_LOGW("Cannot borrow next field: notit-addr=%p", notit
);
2375 status
= BT_BTR_STATUS_ERROR
;
2379 BT_ASSERT(bt_field_is_integer(field
) || bt_field_is_enumeration(field
));
2380 ret
= bt_field_integer_signed_set_value(field
, value
);
2381 BT_ASSERT(ret
== 0);
2382 stack_top(notit
->stack
)->index
++;
2389 enum bt_btr_status
btr_floating_point_cb(double value
,
2390 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("Floating point number function called from BTR: "
2398 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2399 "ft-id=%s, value=%f",
2400 notit
, notit
->btr
, type
,
2401 bt_common_field_type_id_string(
2402 bt_field_type_get_type_id(type
)),
2404 field
= borrow_next_field(notit
);
2406 BT_LOGW("Cannot borrow next field: notit-addr=%p", notit
);
2407 status
= BT_BTR_STATUS_ERROR
;
2411 ret
= bt_field_floating_point_set_value(field
, value
);
2413 stack_top(notit
->stack
)->index
++;
2420 enum bt_btr_status
btr_string_begin_cb(
2421 struct bt_field_type
*type
, void *data
)
2423 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2424 struct bt_field
*field
= NULL
;
2425 struct bt_notif_iter
*notit
= data
;
2428 BT_LOGV("String (beginning) function called from BTR: "
2429 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2431 notit
, notit
->btr
, type
,
2432 bt_common_field_type_id_string(
2433 bt_field_type_get_type_id(type
)));
2434 field
= borrow_next_field(notit
);
2436 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2437 status
= BT_BTR_STATUS_ERROR
;
2441 ret
= bt_field_string_clear(field
);
2442 BT_ASSERT(ret
== 0);
2445 * Push on stack. Not a compound type per se, but we know that only
2446 * btr_string_cb() may be called between this call and a subsequent
2447 * call to btr_string_end_cb().
2449 ret
= stack_push(notit
->stack
, field
);
2451 BT_LOGE("Cannot push string field on stack: "
2452 "notit-addr=%p, field-addr=%p", notit
, field
);
2453 status
= BT_BTR_STATUS_ERROR
;
2462 enum bt_btr_status
btr_string_cb(const char *value
,
2463 size_t len
, struct bt_field_type
*type
, void *data
)
2465 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2466 struct bt_field
*field
= NULL
;
2467 struct bt_notif_iter
*notit
= data
;
2470 BT_LOGV("String (substring) function called from BTR: "
2471 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2472 "ft-id=%s, string-length=%zu",
2473 notit
, notit
->btr
, type
,
2474 bt_common_field_type_id_string(
2475 bt_field_type_get_type_id(type
)),
2477 field
= stack_top(notit
->stack
)->base
;
2480 /* Append current substring */
2481 ret
= bt_field_string_append_len(field
, value
, len
);
2483 BT_LOGE("Cannot append substring to string field's value: "
2484 "notit-addr=%p, field-addr=%p, string-length=%zu, "
2485 "ret=%d", notit
, field
, len
, ret
);
2486 status
= BT_BTR_STATUS_ERROR
;
2495 enum bt_btr_status
btr_string_end_cb(
2496 struct bt_field_type
*type
, void *data
)
2498 struct bt_notif_iter
*notit
= data
;
2500 BT_LOGV("String (end) function called from BTR: "
2501 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2503 notit
, notit
->btr
, type
,
2504 bt_common_field_type_id_string(
2505 bt_field_type_get_type_id(type
)));
2507 /* Pop string field */
2508 stack_pop(notit
->stack
);
2510 /* Go to next field */
2511 stack_top(notit
->stack
)->index
++;
2512 return BT_BTR_STATUS_OK
;
2515 enum bt_btr_status
btr_compound_begin_cb(
2516 struct bt_field_type
*type
, void *data
)
2518 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2519 struct bt_notif_iter
*notit
= data
;
2520 struct bt_field
*field
;
2523 BT_LOGV("Compound (beginning) function called from BTR: "
2524 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2526 notit
, notit
->btr
, type
,
2527 bt_common_field_type_id_string(
2528 bt_field_type_get_type_id(type
)));
2531 if (stack_empty(notit
->stack
)) {
2532 /* Root: already set by read_dscope_begin_state() */
2533 field
= notit
->cur_dscope_field
;
2535 field
= borrow_next_field(notit
);
2537 BT_LOGW("Cannot borrow next field: notit-addr=%p", notit
);
2538 status
= BT_BTR_STATUS_ERROR
;
2545 ret
= stack_push(notit
->stack
, field
);
2547 BT_LOGE("Cannot push compound field onto the stack: "
2548 "notit-addr=%p, ft-addr=%p, ft-id=%s, ret=%d",
2550 bt_common_field_type_id_string(
2551 bt_field_type_get_type_id(type
)),
2553 status
= BT_BTR_STATUS_ERROR
;
2561 enum bt_btr_status
btr_compound_end_cb(
2562 struct bt_field_type
*type
, void *data
)
2564 struct bt_notif_iter
*notit
= data
;
2566 BT_LOGV("Compound (end) function called from BTR: "
2567 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2569 notit
, notit
->btr
, type
,
2570 bt_common_field_type_id_string(
2571 bt_field_type_get_type_id(type
)));
2572 BT_ASSERT(!stack_empty(notit
->stack
));
2575 stack_pop(notit
->stack
);
2577 /* If the stack is not empty, increment the base's index */
2578 if (!stack_empty(notit
->stack
)) {
2579 stack_top(notit
->stack
)->index
++;
2582 return BT_BTR_STATUS_OK
;
2586 struct bt_field
*resolve_field(struct bt_notif_iter
*notit
,
2587 struct bt_field_path
*path
)
2589 struct bt_field
*field
= NULL
;
2592 if (BT_LOG_ON_VERBOSE
) {
2593 GString
*gstr
= bt_field_path_string(path
);
2595 BT_LOGV("Resolving field path: notit-addr=%p, field-path=\"%s\"",
2596 notit
, gstr
? gstr
->str
: NULL
);
2599 g_string_free(gstr
, TRUE
);
2603 switch (bt_field_path_get_root_scope(path
)) {
2604 case BT_SCOPE_TRACE_PACKET_HEADER
:
2605 field
= notit
->dscopes
.trace_packet_header
;
2607 case BT_SCOPE_STREAM_PACKET_CONTEXT
:
2608 field
= notit
->dscopes
.stream_packet_context
;
2610 case BT_SCOPE_STREAM_EVENT_HEADER
:
2611 field
= notit
->dscopes
.stream_event_header
;
2613 case BT_SCOPE_STREAM_EVENT_CONTEXT
:
2614 field
= notit
->dscopes
.stream_event_context
;
2616 case BT_SCOPE_EVENT_CONTEXT
:
2617 field
= notit
->dscopes
.event_context
;
2619 case BT_SCOPE_EVENT_FIELDS
:
2620 field
= notit
->dscopes
.event_payload
;
2623 BT_LOGF("Cannot resolve field path: unknown scope: "
2624 "notit-addr=%p, root-scope=%s",
2625 notit
, bt_common_scope_string(
2626 bt_field_path_get_root_scope(path
)));
2631 BT_LOGW("Cannot resolve field path: root field not found: "
2632 "notit-addr=%p, root-scope=%s",
2633 notit
, bt_common_scope_string(
2634 bt_field_path_get_root_scope(path
)));
2638 for (i
= 0; i
< bt_field_path_get_index_count(path
); ++i
) {
2639 struct bt_field
*next_field
= NULL
;
2640 struct bt_field_type
*field_type
;
2641 int index
= bt_field_path_get_index(path
, i
);
2643 field_type
= bt_field_borrow_type(field
);
2644 BT_ASSERT(field_type
);
2646 if (bt_field_type_is_structure(field_type
)) {
2647 next_field
= bt_field_structure_borrow_field_by_index(
2649 } else if (bt_field_type_is_variant(field_type
)) {
2651 bt_field_variant_borrow_current_field(field
);
2657 BT_LOGW("Cannot find next field: "
2658 "notit-addr=%p, ft-addr=%p, ft-id=%s, index=%d",
2660 bt_common_field_type_id_string(
2661 bt_field_type_get_type_id(field_type
)),
2666 /* Move next field -> field */
2675 int64_t btr_get_sequence_length_cb(struct bt_field_type
*type
, void *data
)
2679 struct bt_field
*seq_field
;
2680 struct bt_field_path
*field_path
;
2681 struct bt_notif_iter
*notit
= data
;
2682 struct bt_field
*length_field
= NULL
;
2685 field_path
= bt_field_type_sequence_borrow_length_field_path(type
);
2686 BT_ASSERT(field_path
);
2687 length_field
= resolve_field(notit
, field_path
);
2688 if (!length_field
) {
2689 BT_LOGW("Cannot resolve sequence field type's length field path: "
2690 "notit-addr=%p, ft-addr=%p",
2695 iret
= bt_field_integer_unsigned_get_value(length_field
, &length
);
2697 BT_LOGE("Cannot get value of sequence length field: "
2698 "notit-addr=%p, field-addr=%p",
2699 notit
, length_field
);
2703 seq_field
= stack_top(notit
->stack
)->base
;
2704 iret
= bt_field_sequence_set_length(seq_field
, length
);
2706 BT_LOGE("Cannot set sequence field's length field: "
2707 "notit-addr=%p, seq-field-addr=%p, "
2709 notit
, seq_field
, length
);
2713 ret
= (int64_t) length
;
2720 struct bt_field_type
*btr_borrow_variant_field_type_cb(
2721 struct bt_field_type
*type
, void *data
)
2724 struct bt_field_path
*path
;
2725 struct bt_notif_iter
*notit
= data
;
2726 struct bt_field
*var_field
;
2727 struct bt_field
*tag_field
= NULL
;
2728 struct bt_field_type
*tag_ft
= NULL
;
2729 struct bt_field_type
*tag_int_ft
= NULL
;
2730 struct bt_field
*selected_field
= NULL
;
2731 struct bt_field_type
*selected_field_type
= NULL
;
2733 path
= bt_field_type_variant_borrow_tag_field_path(type
);
2735 tag_field
= resolve_field(notit
, path
);
2737 BT_LOGW("Cannot resolve variant field type's tag field path: "
2738 "notit-addr=%p, ft-addr=%p",
2744 * We found the enumeration tag field instance which should be
2745 * able to select a current field for this variant. This
2746 * callback function we're in is called _after_
2747 * compound_begin(), so the current stack top's base field is
2748 * the variant field in question. We set the variant field's tag
2749 * here and then get the current (selected) field thanks to this
2750 * tag field's value. This current field will also provide us
2751 * with its type. Then, this current field will remain the
2752 * current selected one until the next callback function call
2753 * which is used to fill it.
2755 var_field
= stack_top(notit
->stack
)->base
;
2756 tag_ft
= bt_field_borrow_type(tag_field
);
2757 tag_int_ft
= bt_field_type_enumeration_borrow_container_field_type(
2760 if (bt_field_type_integer_is_signed(tag_int_ft
)) {
2763 ret
= bt_field_integer_signed_get_value(tag_field
, &tag_value
);
2764 BT_ASSERT(ret
== 0);
2765 ret
= bt_field_variant_set_tag_signed(var_field
, tag_value
);
2766 BT_ASSERT(ret
== 0);
2770 ret
= bt_field_integer_unsigned_get_value(tag_field
,
2772 BT_ASSERT(ret
== 0);
2773 ret
= bt_field_variant_set_tag_unsigned(var_field
, tag_value
);
2774 BT_ASSERT(ret
== 0);
2777 selected_field
= bt_field_variant_borrow_current_field(var_field
);
2778 if (!selected_field
) {
2779 BT_LOGW("Cannot borrow variant field's current field: "
2780 "notit-addr=%p, var-field-addr=%p",
2785 selected_field_type
= bt_field_borrow_type(selected_field
);
2788 return selected_field_type
;
2792 int set_event_clocks(struct bt_notif_iter
*notit
)
2795 GHashTableIter iter
;
2796 struct bt_clock_class
*clock_class
;
2797 uint64_t *clock_state
;
2799 g_hash_table_iter_init(&iter
, notit
->clock_states
);
2801 while (g_hash_table_iter_next(&iter
, (gpointer
) &clock_class
,
2802 (gpointer
) &clock_state
)) {
2803 ret
= bt_event_set_clock_value(notit
->event
, clock_class
,
2804 *clock_state
, BT_TRUE
);
2806 BT_LOGE("Cannot set event's default clock value: "
2807 "notit-addr=%p, clock-class-addr=%p, "
2808 "clock-class-name=\"%s\"",
2810 bt_clock_class_get_name(clock_class
));
2823 void notify_new_stream(struct bt_notif_iter
*notit
,
2824 struct bt_notification
**notification
)
2826 enum bt_notif_iter_status status
;
2827 struct bt_notification
*ret
= NULL
;
2829 status
= set_current_stream(notit
);
2830 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
2835 BT_ASSERT(notit
->stream
);
2836 BT_ASSERT(notit
->notif_iter
);
2837 ret
= bt_notification_stream_begin_create(notit
->notif_iter
,
2840 BT_LOGE("Cannot create stream beginning notification: "
2841 "notit-addr=%p, stream-addr=%p",
2842 notit
, notit
->stream
);
2847 *notification
= ret
;
2851 void notify_end_of_stream(struct bt_notif_iter
*notit
,
2852 struct bt_notification
**notification
)
2854 struct bt_notification
*ret
;
2856 if (!notit
->stream
) {
2857 BT_LOGE("Cannot create stream for stream notification: "
2858 "notit-addr=%p", notit
);
2862 BT_ASSERT(notit
->notif_iter
);
2863 ret
= bt_notification_stream_end_create(notit
->notif_iter
,
2866 BT_LOGE("Cannot create stream beginning notification: "
2867 "notit-addr=%p, stream-addr=%p",
2868 notit
, notit
->stream
);
2871 *notification
= ret
;
2875 void notify_new_packet(struct bt_notif_iter
*notit
,
2876 struct bt_notification
**notification
)
2879 enum bt_notif_iter_status status
;
2880 struct bt_notification
*notif
= NULL
;
2882 status
= set_current_packet(notit
);
2883 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
2887 BT_ASSERT(notit
->packet
);
2889 if (notit
->packet_header_field
) {
2890 ret
= bt_packet_move_header(notit
->packet
,
2891 notit
->packet_header_field
);
2896 notit
->packet_header_field
= NULL
;
2899 * At this point notit->dscopes.trace_packet_header has
2900 * the same value as the packet header field within
2903 BT_ASSERT(bt_packet_borrow_header(notit
->packet
) ==
2904 notit
->dscopes
.trace_packet_header
);
2907 if (notit
->packet_context_field
) {
2908 ret
= bt_packet_move_context(notit
->packet
,
2909 notit
->packet_context_field
);
2914 notit
->packet_context_field
= NULL
;
2917 * At this point notit->dscopes.trace_packet_header has
2918 * the same value as the packet header field within
2921 BT_ASSERT(bt_packet_borrow_context(notit
->packet
) ==
2922 notit
->dscopes
.stream_packet_context
);
2925 if (notit
->cur_packet_begin_cv
.clock_class
) {
2926 ret
= bt_packet_set_beginning_clock_value(notit
->packet
,
2927 notit
->cur_packet_begin_cv
.clock_class
,
2928 notit
->cur_packet_begin_cv
.raw_value
, BT_TRUE
);
2930 BT_LOGE("Cannot set packet's default beginning clock value: "
2931 "notit-addr=%p, packet-addr=%p",
2932 notit
, notit
->packet
);
2937 if (notit
->cur_packet_end_cv
.clock_class
) {
2938 ret
= bt_packet_set_end_clock_value(notit
->packet
,
2939 notit
->cur_packet_end_cv
.clock_class
,
2940 notit
->cur_packet_end_cv
.raw_value
, BT_TRUE
);
2942 BT_LOGE("Cannot set packet's default end clock value: "
2943 "notit-addr=%p, packet-addr=%p",
2944 notit
, notit
->packet
);
2949 BT_ASSERT(notit
->notif_iter
);
2950 notif
= bt_notification_packet_begin_create(notit
->notif_iter
,
2953 BT_LOGE("Cannot create packet beginning notification: "
2954 "notit-addr=%p, packet-addr=%p",
2955 notit
, notit
->packet
);
2959 *notification
= notif
;
2966 void notify_end_of_packet(struct bt_notif_iter
*notit
,
2967 struct bt_notification
**notification
)
2969 struct bt_notification
*notif
;
2971 if (!notit
->packet
) {
2975 BT_ASSERT(notit
->notif_iter
);
2976 notif
= bt_notification_packet_end_create(notit
->notif_iter
,
2979 BT_LOGE("Cannot create packet end notification: "
2980 "notit-addr=%p, packet-addr=%p",
2981 notit
, notit
->packet
);
2985 BT_PUT(notit
->packet
);
2986 *notification
= notif
;
2990 void init_trace_field_path_cache(struct bt_trace
*trace
,
2991 struct trace_field_path_cache
*trace_field_path_cache
)
2994 int stream_instance_id
= -1;
2996 struct bt_field_type
*packet_header
= NULL
;
2998 packet_header
= bt_trace_borrow_packet_header_field_type(trace
);
2999 if (!packet_header
) {
3003 if (!bt_field_type_is_structure(packet_header
)) {
3007 count
= bt_field_type_structure_get_field_count(packet_header
);
3008 BT_ASSERT(count
>= 0);
3010 for (i
= 0; (i
< count
&& (stream_id
== -1 || stream_instance_id
== -1)); i
++) {
3012 const char *field_name
;
3014 ret
= bt_field_type_structure_borrow_field_by_index(
3015 packet_header
, &field_name
, NULL
, i
);
3017 BT_LOGE("Cannot get structure field's field: "
3018 "field-addr=%p, index=%d",
3023 if (stream_id
== -1 && !strcmp(field_name
, "stream_id")) {
3025 } else if (stream_instance_id
== -1 &&
3026 !strcmp(field_name
, "stream_instance_id")) {
3027 stream_instance_id
= i
;
3032 trace_field_path_cache
->stream_id
= stream_id
;
3033 trace_field_path_cache
->stream_instance_id
= stream_instance_id
;
3037 struct bt_notif_iter
*bt_notif_iter_create(struct bt_trace
*trace
,
3038 size_t max_request_sz
,
3039 struct bt_notif_iter_medium_ops medops
, void *data
)
3041 struct bt_notif_iter
*notit
= NULL
;
3042 struct bt_btr_cbs cbs
= {
3044 .signed_int
= btr_signed_int_cb
,
3045 .unsigned_int
= btr_unsigned_int_cb
,
3046 .floating_point
= btr_floating_point_cb
,
3047 .string_begin
= btr_string_begin_cb
,
3048 .string
= btr_string_cb
,
3049 .string_end
= btr_string_end_cb
,
3050 .compound_begin
= btr_compound_begin_cb
,
3051 .compound_end
= btr_compound_end_cb
,
3054 .get_sequence_length
= btr_get_sequence_length_cb
,
3055 .borrow_variant_field_type
= btr_borrow_variant_field_type_cb
,
3060 BT_ASSERT(medops
.request_bytes
);
3061 BT_ASSERT(medops
.borrow_stream
);
3062 BT_LOGD("Creating CTF plugin notification iterator: "
3063 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
3065 trace
, bt_trace_get_name(trace
), max_request_sz
, data
);
3066 notit
= g_new0(struct bt_notif_iter
, 1);
3068 BT_LOGE_STR("Failed to allocate one CTF plugin notification iterator.");
3071 notit
->clock_states
= g_hash_table_new_full(g_direct_hash
,
3072 g_direct_equal
, NULL
, g_free
);
3073 if (!notit
->clock_states
) {
3074 BT_LOGE_STR("Failed to allocate a GHashTable.");
3077 notit
->meta
.trace
= trace
;
3078 notit
->medium
.medops
= medops
;
3079 notit
->medium
.max_request_sz
= max_request_sz
;
3080 notit
->medium
.data
= data
;
3081 notit
->stack
= stack_new(notit
);
3082 if (!notit
->stack
) {
3083 BT_LOGE_STR("Failed to create field stack.");
3087 notit
->btr
= bt_btr_create(cbs
, notit
);
3089 BT_LOGE_STR("Failed to create binary type reader (BTR).");
3093 bt_notif_iter_reset(notit
);
3094 init_trace_field_path_cache(trace
, ¬it
->trace_field_path_cache
);
3095 notit
->sc_field_path_caches
= g_hash_table_new_full(g_direct_hash
,
3096 g_direct_equal
, NULL
, g_free
);
3097 if (!notit
->sc_field_path_caches
) {
3098 BT_LOGE_STR("Failed to allocate a GHashTable.");
3102 notit
->field_overrides
= g_hash_table_new_full(g_direct_hash
,
3103 g_direct_equal
, NULL
, g_free
);
3104 if (!notit
->field_overrides
) {
3105 BT_LOGE_STR("Failed to allocate a GHashTable.");
3109 BT_LOGD("Created CTF plugin notification iterator: "
3110 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
3111 "data=%p, notit-addr=%p",
3112 trace
, bt_trace_get_name(trace
), max_request_sz
, data
,
3114 notit
->cur_packet_offset
= 0;
3120 bt_notif_iter_destroy(notit
);
3125 void bt_notif_iter_destroy(struct bt_notif_iter
*notit
)
3127 BT_PUT(notit
->packet
);
3128 BT_PUT(notit
->stream
);
3129 release_all_dscopes(notit
);
3131 BT_LOGD("Destroying CTF plugin notification iterator: addr=%p", notit
);
3134 BT_LOGD_STR("Destroying field stack.");
3135 stack_destroy(notit
->stack
);
3139 BT_LOGD("Destroying BTR: btr-addr=%p", notit
->btr
);
3140 bt_btr_destroy(notit
->btr
);
3143 if (notit
->clock_states
) {
3144 g_hash_table_destroy(notit
->clock_states
);
3147 if (notit
->sc_field_path_caches
) {
3148 g_hash_table_destroy(notit
->sc_field_path_caches
);
3151 if (notit
->field_overrides
) {
3152 g_hash_table_destroy(notit
->field_overrides
);
3158 enum bt_notif_iter_status
bt_notif_iter_get_next_notification(
3159 struct bt_notif_iter
*notit
,
3160 struct bt_private_connection_private_notification_iterator
*notif_iter
,
3161 struct bt_notification
**notification
)
3164 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
3167 BT_ASSERT(notification
);
3169 if (notit
->state
== STATE_DONE
) {
3170 status
= BT_NOTIF_ITER_STATUS_EOF
;
3174 notit
->notif_iter
= notif_iter
;
3176 BT_LOGV("Getting next notification: notit-addr=%p", notit
);
3179 status
= handle_state(notit
);
3180 if (status
== BT_NOTIF_ITER_STATUS_AGAIN
) {
3181 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_AGAIN.");
3185 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
3186 if (status
== BT_NOTIF_ITER_STATUS_EOF
) {
3187 enum state next_state
= notit
->state
;
3189 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_EOF.");
3191 if (notit
->packet
) {
3192 notify_end_of_packet(notit
,
3195 notify_end_of_stream(notit
,
3197 next_state
= STATE_DONE
;
3200 if (!*notification
) {
3201 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3205 status
= BT_NOTIF_ITER_STATUS_OK
;
3206 notit
->state
= next_state
;
3208 BT_LOGW("Cannot handle state: "
3209 "notit-addr=%p, state=%s",
3210 notit
, state_string(notit
->state
));
3216 switch (notit
->state
) {
3217 case STATE_EMIT_NOTIF_NEW_STREAM
:
3218 /* notify_new_stream() logs errors */
3219 notify_new_stream(notit
, notification
);
3220 if (!*notification
) {
3221 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3223 notit
->stream_begin_emitted
= true;
3225 case STATE_EMIT_NOTIF_NEW_PACKET
:
3226 /* notify_new_packet() logs errors */
3227 notify_new_packet(notit
, notification
);
3228 if (!*notification
) {
3229 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3233 case STATE_EMIT_NOTIF_EVENT
:
3234 BT_ASSERT(notit
->event_notif
);
3235 ret
= set_event_clocks(notit
);
3237 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3241 *notification
= notit
->event_notif
;
3242 notit
->event_notif
= NULL
;
3244 case STATE_EMIT_NOTIF_END_OF_PACKET
:
3245 /* Update clock with timestamp_end field. */
3246 if (notit
->cur_timestamp_end
) {
3247 enum bt_btr_status btr_status
;
3249 btr_status
= update_clock(notit
,
3250 notit
->cur_timestamp_end
);
3251 if (btr_status
!= BT_BTR_STATUS_OK
) {
3252 BT_LOGW("Cannot update stream's clock value: "
3253 "notit-addr=%p", notit
);
3254 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3259 /* notify_end_of_packet() logs errors */
3260 notify_end_of_packet(notit
, notification
);
3261 if (!*notification
) {
3262 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3267 /* Non-emitting state: continue */
3277 enum bt_notif_iter_status
bt_notif_iter_borrow_packet_header_context_fields(
3278 struct bt_notif_iter
*notit
,
3279 struct bt_field
**packet_header_field
,
3280 struct bt_field
**packet_context_field
)
3283 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
3287 if (notit
->state
== STATE_EMIT_NOTIF_NEW_PACKET
) {
3288 /* We're already there */
3293 status
= handle_state(notit
);
3294 if (status
== BT_NOTIF_ITER_STATUS_AGAIN
) {
3295 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_AGAIN.");
3298 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
3299 if (status
== BT_NOTIF_ITER_STATUS_EOF
) {
3300 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_EOF.");
3302 BT_LOGW("Cannot handle state: "
3303 "notit-addr=%p, state=%s",
3304 notit
, state_string(notit
->state
));
3309 switch (notit
->state
) {
3310 case STATE_EMIT_NOTIF_NEW_PACKET
:
3312 * Packet header and context fields are
3313 * potentially decoded (or they don't exist).
3317 case STATE_EMIT_NOTIF_NEW_STREAM
:
3318 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
3319 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
3320 case STATE_AFTER_TRACE_PACKET_HEADER
:
3321 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
3322 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
3323 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
3324 /* Non-emitting state: continue */
3328 * We should never get past the
3329 * STATE_EMIT_NOTIF_NEW_PACKET state.
3331 BT_LOGF("Unexpected state: notit-addr=%p, state=%s",
3332 notit
, state_string(notit
->state
));
3338 ret
= set_current_packet_content_sizes(notit
);
3340 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3344 if (packet_header_field
) {
3345 *packet_header_field
= notit
->dscopes
.trace_packet_header
;
3348 if (packet_context_field
) {
3349 *packet_context_field
= notit
->dscopes
.stream_packet_context
;
3357 void bt_notif_iter_set_medops_data(struct bt_notif_iter
*notit
,
3361 notit
->medium
.data
= medops_data
;
3365 enum bt_notif_iter_status
bt_notif_iter_seek(
3366 struct bt_notif_iter
*notit
, off_t offset
)
3368 enum bt_notif_iter_status ret
= BT_NOTIF_ITER_STATUS_OK
;
3369 enum bt_notif_iter_medium_status medium_status
;
3373 BT_LOGE("Cannot seek to negative offset: offset=%jd", offset
);
3374 ret
= BT_NOTIF_ITER_STATUS_INVAL
;
3378 if (!notit
->medium
.medops
.seek
) {
3379 ret
= BT_NOTIF_ITER_STATUS_UNSUPPORTED
;
3380 BT_LOGD("Aborting seek as the iterator's underlying media does not implement seek support.");
3384 medium_status
= notit
->medium
.medops
.seek(
3385 BT_NOTIF_ITER_SEEK_WHENCE_SET
, offset
,
3386 notit
->medium
.data
);
3387 if (medium_status
!= BT_NOTIF_ITER_MEDIUM_STATUS_OK
) {
3388 if (medium_status
== BT_NOTIF_ITER_MEDIUM_STATUS_EOF
) {
3389 ret
= BT_NOTIF_ITER_STATUS_EOF
;
3391 ret
= BT_NOTIF_ITER_STATUS_ERROR
;
3396 bt_notif_iter_reset(notit
);
3397 notit
->cur_packet_offset
= offset
;
3403 off_t
bt_notif_iter_get_current_packet_offset(
3404 struct bt_notif_iter
*notit
)
3407 return notit
->cur_packet_offset
;
3411 off_t
bt_notif_iter_get_current_packet_size(
3412 struct bt_notif_iter
*notit
)
3415 return notit
->cur_packet_size
;