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:
57 * Field is owned by this.
59 struct bt_field
*base
;
61 /* index of next field to set */
67 /* Entries (struct stack_entry *) (top is last element) */
74 STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
,
75 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
76 STATE_AFTER_TRACE_PACKET_HEADER
,
77 STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
,
78 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
79 STATE_AFTER_STREAM_PACKET_CONTEXT
,
80 STATE_EMIT_NOTIF_NEW_STREAM
,
81 STATE_EMIT_NOTIF_NEW_PACKET
,
82 STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
,
83 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
,
84 STATE_AFTER_STREAM_EVENT_HEADER
,
85 STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
,
86 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
,
87 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
,
88 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
,
89 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
90 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
91 STATE_EMIT_NOTIF_EVENT
,
92 STATE_EMIT_NOTIF_END_OF_PACKET
,
94 STATE_SKIP_PACKET_PADDING
,
97 struct trace_field_path_cache
{
99 * Indexes of the stream_id and stream_instance_id field in the packet
100 * header structure, -1 if unset.
103 int stream_instance_id
;
106 struct stream_class_field_path_cache
{
108 * Indexes of the v and id fields in the stream event header structure,
115 * index of the timestamp_end, packet_size and content_size fields in
116 * the stream packet context structure. Set to -1 if the fields were
124 struct field_cb_override
{
125 enum bt_btr_status (* func
)(void *value
,
126 struct bt_field_type
*type
, void *data
);
130 /* CTF notification iterator */
131 struct bt_notif_iter
{
136 * Current dynamic scope field pointer.
138 * This is set when a dynamic scope field is first created by
139 * btr_compound_begin_cb(). It points to one of the fields in
142 struct bt_field
**cur_dscope_field
;
144 /* Trace and classes (owned by this) */
146 struct bt_trace
*trace
;
147 struct bt_stream_class
*stream_class
;
148 struct bt_event_class
*event_class
;
151 /* Current packet (NULL if not created yet) */
152 struct bt_packet
*packet
;
154 /* Current stream (NULL if not set yet) */
155 struct bt_stream
*stream
;
158 * Current timestamp_end field (to consider before switching packets).
160 struct bt_field
*cur_timestamp_end
;
162 /* Database of current dynamic scopes (owned by this) */
164 struct bt_field
*trace_packet_header
;
165 struct bt_field
*stream_packet_context
;
166 struct bt_field
*stream_event_header
;
167 struct bt_field
*stream_event_context
;
168 struct bt_field
*event_context
;
169 struct bt_field
*event_payload
;
173 * Special field overrides.
175 * Overrides are used to implement the behaviours of special fields such
176 * as "timestamp_end" (which must be ignored until the end of the
177 * packet), "id" (event id) which can be present multiple times and must
178 * be updated multiple time.
180 * This should be used to implement the behaviour of integer fields
181 * mapped to clocks and other "tagged" fields (in CTF 2).
183 * bt_field_type to struct field_cb_override
185 GHashTable
*field_overrides
;
190 /* Current medium buffer data */
192 /* Last address provided by medium */
195 /* Buffer size provided by medium (bytes) */
198 /* Offset within whole packet of addr (bits) */
199 size_t packet_offset
;
201 /* Current position from addr (bits) */
204 /* Position of the last event header from addr (bits) */
208 /* Binary type reader */
211 /* Current medium data */
213 struct bt_notif_iter_medium_ops medops
;
214 size_t max_request_sz
;
218 /* Stream beginning was emitted */
219 bool stream_begin_emitted
;
221 /* Current packet size (bits) (-1 if unknown) */
222 int64_t cur_packet_size
;
224 /* Current content size (bits) (-1 if unknown) */
225 int64_t cur_content_size
;
228 * Offset, in the underlying media, of the current packet's start
231 off_t cur_packet_offset
;
233 /* bt_clock_class to uint64_t. */
234 GHashTable
*clock_states
;
237 * Cache of the trace-constant field paths (event header type)
238 * associated to the current trace.
240 struct trace_field_path_cache trace_field_path_cache
;
243 * Field path cache associated with the current stream class.
244 * Ownership of this structure belongs to the field_path_caches HT.
246 struct stream_class_field_path_cache
*cur_sc_field_path_cache
;
248 /* bt_stream_class to struct stream_class_field_path_cache. */
249 GHashTable
*sc_field_path_caches
;
253 const char *state_string(enum state state
)
258 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
259 return "STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN";
260 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
261 return "STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE";
262 case STATE_AFTER_TRACE_PACKET_HEADER
:
263 return "STATE_AFTER_TRACE_PACKET_HEADER";
264 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
265 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN";
266 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
267 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE";
268 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
269 return "STATE_AFTER_STREAM_PACKET_CONTEXT";
270 case STATE_EMIT_NOTIF_NEW_PACKET
:
271 return "STATE_EMIT_NOTIF_NEW_PACKET";
272 case STATE_EMIT_NOTIF_NEW_STREAM
:
273 return "STATE_EMIT_NOTIF_NEW_STREAM";
274 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
:
275 return "STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN";
276 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
:
277 return "STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE";
278 case STATE_AFTER_STREAM_EVENT_HEADER
:
279 return "STATE_AFTER_STREAM_EVENT_HEADER";
280 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
:
281 return "STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN";
282 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
:
283 return "STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE";
284 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN
:
285 return "STATE_DSCOPE_EVENT_CONTEXT_BEGIN";
286 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
:
287 return "STATE_DSCOPE_EVENT_CONTEXT_CONTINUE";
288 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
289 return "STATE_DSCOPE_EVENT_PAYLOAD_BEGIN";
290 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
291 return "STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE";
292 case STATE_EMIT_NOTIF_EVENT
:
293 return "STATE_EMIT_NOTIF_EVENT";
294 case STATE_EMIT_NOTIF_END_OF_PACKET
:
295 return "STATE_EMIT_NOTIF_END_OF_PACKET";
298 case STATE_SKIP_PACKET_PADDING
:
299 return "STATE_SKIP_PACKET_PADDING";
306 int bt_notif_iter_switch_packet(struct bt_notif_iter
*notit
);
309 enum bt_btr_status
btr_timestamp_end_cb(void *value
,
310 struct bt_field_type
*type
, void *data
);
313 void stack_entry_free_func(gpointer data
)
315 struct stack_entry
*entry
= data
;
322 struct stack
*stack_new(struct bt_notif_iter
*notit
)
324 struct stack
*stack
= NULL
;
326 stack
= g_new0(struct stack
, 1);
328 BT_LOGE_STR("Failed to allocate one stack.");
332 stack
->entries
= g_ptr_array_new_with_free_func(stack_entry_free_func
);
333 if (!stack
->entries
) {
334 BT_LOGE_STR("Failed to allocate a GPtrArray.");
338 BT_LOGD("Created stack: notit-addr=%p, stack-addr=%p", notit
, stack
);
347 void stack_destroy(struct stack
*stack
)
350 BT_LOGD("Destroying stack: addr=%p", stack
);
351 g_ptr_array_free(stack
->entries
, TRUE
);
356 int stack_push(struct stack
*stack
, struct bt_field
*base
)
359 struct stack_entry
*entry
;
363 BT_LOGV("Pushing base field on stack: stack-addr=%p, "
364 "stack-size-before=%u, stack-size-after=%u",
365 stack
, stack
->entries
->len
, stack
->entries
->len
+ 1);
366 entry
= g_new0(struct stack_entry
, 1);
368 BT_LOGE_STR("Failed to allocate one stack entry.");
373 entry
->base
= bt_get(base
);
374 g_ptr_array_add(stack
->entries
, entry
);
381 unsigned int stack_size(struct stack
*stack
)
385 return stack
->entries
->len
;
389 void stack_pop(struct stack
*stack
)
392 BT_ASSERT(stack_size(stack
));
393 BT_LOGV("Popping from stack: "
394 "stack-addr=%p, stack-size-before=%u, stack-size-after=%u",
395 stack
, stack
->entries
->len
, stack
->entries
->len
- 1);
396 g_ptr_array_remove_index(stack
->entries
, stack
->entries
->len
- 1);
400 struct stack_entry
*stack_top(struct stack
*stack
)
403 BT_ASSERT(stack_size(stack
));
405 return g_ptr_array_index(stack
->entries
, stack
->entries
->len
- 1);
409 bool stack_empty(struct stack
*stack
)
411 return stack_size(stack
) == 0;
415 void stack_clear(struct stack
*stack
)
419 if (!stack_empty(stack
)) {
420 BT_LOGV("Clearing stack: stack-addr=%p, stack-size=%u",
421 stack
, stack
->entries
->len
);
422 g_ptr_array_remove_range(stack
->entries
, 0, stack_size(stack
));
425 BT_ASSERT(stack_empty(stack
));
429 enum bt_notif_iter_status
notif_iter_status_from_m_status(
430 enum bt_notif_iter_medium_status m_status
)
432 return (int) m_status
;
436 size_t buf_size_bits(struct bt_notif_iter
*notit
)
438 return notit
->buf
.sz
* 8;
442 size_t buf_available_bits(struct bt_notif_iter
*notit
)
444 return buf_size_bits(notit
) - notit
->buf
.at
;
448 size_t packet_at(struct bt_notif_iter
*notit
)
450 return notit
->buf
.packet_offset
+ notit
->buf
.at
;
454 void buf_consume_bits(struct bt_notif_iter
*notit
, size_t incr
)
456 BT_LOGV("Advancing cursor: notit-addr=%p, cur-before=%zu, cur-after=%zu",
457 notit
, notit
->buf
.at
, notit
->buf
.at
+ incr
);
458 notit
->buf
.at
+= incr
;
462 enum bt_notif_iter_status
request_medium_bytes(
463 struct bt_notif_iter
*notit
)
465 uint8_t *buffer_addr
= NULL
;
466 size_t buffer_sz
= 0;
467 enum bt_notif_iter_medium_status m_status
;
469 BT_LOGV("Calling user function (request bytes): notit-addr=%p, "
470 "request-size=%zu", notit
, notit
->medium
.max_request_sz
);
471 m_status
= notit
->medium
.medops
.request_bytes(
472 notit
->medium
.max_request_sz
, &buffer_addr
,
473 &buffer_sz
, notit
->medium
.data
);
474 BT_LOGV("User function returned: status=%s, buf-addr=%p, buf-size=%zu",
475 bt_notif_iter_medium_status_string(m_status
),
476 buffer_addr
, buffer_sz
);
477 if (m_status
== BT_NOTIF_ITER_MEDIUM_STATUS_OK
) {
478 BT_ASSERT(buffer_sz
!= 0);
480 /* New packet offset is old one + old size (in bits) */
481 notit
->buf
.packet_offset
+= buf_size_bits(notit
);
483 /* Restart at the beginning of the new medium buffer */
485 notit
->buf
.last_eh_at
= SIZE_MAX
;
487 /* New medium buffer size */
488 notit
->buf
.sz
= buffer_sz
;
490 /* New medium buffer address */
491 notit
->buf
.addr
= buffer_addr
;
493 BT_LOGV("User function returned new bytes: "
494 "packet-offset=%zu, cur=%zu, size=%zu, addr=%p",
495 notit
->buf
.packet_offset
, notit
->buf
.at
,
496 notit
->buf
.sz
, notit
->buf
.addr
);
497 BT_LOGV_MEM(buffer_addr
, buffer_sz
, "Returned bytes at %p:",
499 } else if (m_status
== BT_NOTIF_ITER_MEDIUM_STATUS_EOF
) {
501 * User returned end of stream: validate that we're not
502 * in the middle of a packet header, packet context, or
505 if (notit
->cur_packet_size
>= 0) {
506 if (packet_at(notit
) == notit
->cur_packet_size
) {
510 if (packet_at(notit
) == 0) {
514 if (notit
->buf
.last_eh_at
!= SIZE_MAX
&&
515 notit
->buf
.at
== notit
->buf
.last_eh_at
) {
520 /* All other states are invalid */
521 BT_LOGW("User function returned %s, but notification iterator is in an unexpected state: "
522 "state=%s, cur-packet-size=%" PRId64
", cur=%zu, "
523 "packet-cur=%zu, last-eh-at=%zu",
524 bt_notif_iter_medium_status_string(m_status
),
525 state_string(notit
->state
),
526 notit
->cur_packet_size
,
527 notit
->buf
.at
, packet_at(notit
),
528 notit
->buf
.last_eh_at
);
529 m_status
= BT_NOTIF_ITER_MEDIUM_STATUS_ERROR
;
530 } else if (m_status
< 0) {
531 BT_LOGW("User function failed: status=%s",
532 bt_notif_iter_medium_status_string(m_status
));
536 return notif_iter_status_from_m_status(m_status
);
540 enum bt_notif_iter_status
buf_ensure_available_bits(
541 struct bt_notif_iter
*notit
)
543 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
545 if (buf_available_bits(notit
) == 0) {
547 * This _cannot_ return BT_NOTIF_ITER_STATUS_OK
550 status
= request_medium_bytes(notit
);
557 enum bt_notif_iter_status
read_dscope_begin_state(
558 struct bt_notif_iter
*notit
,
559 struct bt_field_type
*dscope_field_type
,
560 enum state done_state
, enum state continue_state
,
561 struct bt_field
**dscope_field
)
563 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
564 enum bt_btr_status btr_status
;
565 size_t consumed_bits
;
567 bt_put(*dscope_field
);
568 notit
->cur_dscope_field
= dscope_field
;
569 BT_LOGV("Starting BTR: notit-addr=%p, btr-addr=%p, ft-addr=%p",
570 notit
, notit
->btr
, dscope_field_type
);
571 consumed_bits
= bt_btr_start(notit
->btr
, dscope_field_type
,
572 notit
->buf
.addr
, notit
->buf
.at
, packet_at(notit
),
573 notit
->buf
.sz
, &btr_status
);
574 BT_LOGV("BTR consumed bits: size=%zu", consumed_bits
);
576 switch (btr_status
) {
577 case BT_BTR_STATUS_OK
:
578 /* type was read completely */
579 BT_LOGV_STR("Field was completely decoded.");
580 notit
->state
= done_state
;
582 case BT_BTR_STATUS_EOF
:
583 BT_LOGV_STR("BTR needs more data to decode field completely.");
584 notit
->state
= continue_state
;
587 BT_LOGW("BTR failed to start: notit-addr=%p, btr-addr=%p, "
588 "status=%s", notit
, notit
->btr
,
589 bt_btr_status_string(btr_status
));
590 status
= BT_NOTIF_ITER_STATUS_ERROR
;
594 /* Consume bits now since we know we're not in an error state */
595 buf_consume_bits(notit
, consumed_bits
);
602 enum bt_notif_iter_status
read_dscope_continue_state(
603 struct bt_notif_iter
*notit
, enum state done_state
)
605 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
606 enum bt_btr_status btr_status
;
607 size_t consumed_bits
;
609 BT_LOGV("Continuing BTR: notit-addr=%p, btr-addr=%p",
612 status
= buf_ensure_available_bits(notit
);
613 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
615 BT_LOGW("Cannot ensure that buffer has at least one byte: "
616 "notif-addr=%p, status=%s",
617 notit
, bt_notif_iter_status_string(status
));
619 BT_LOGV("Cannot ensure that buffer has at least one byte: "
620 "notif-addr=%p, status=%s",
621 notit
, bt_notif_iter_status_string(status
));
628 consumed_bits
= bt_btr_continue(notit
->btr
, notit
->buf
.addr
,
629 notit
->buf
.sz
, &btr_status
);
630 BT_LOGV("BTR consumed bits: size=%zu", consumed_bits
);
632 switch (btr_status
) {
633 case BT_BTR_STATUS_OK
:
634 /* Type was read completely. */
635 BT_LOGV_STR("Field was completely decoded.");
636 notit
->state
= done_state
;
638 case BT_BTR_STATUS_EOF
:
639 /* Stay in this continue state. */
640 BT_LOGV_STR("BTR needs more data to decode field completely.");
643 BT_LOGW("BTR failed to continue: notit-addr=%p, btr-addr=%p, "
644 "status=%s", notit
, notit
->btr
,
645 bt_btr_status_string(btr_status
));
646 status
= BT_NOTIF_ITER_STATUS_ERROR
;
650 /* Consume bits now since we know we're not in an error state. */
651 buf_consume_bits(notit
, consumed_bits
);
657 void put_event_dscopes(struct bt_notif_iter
*notit
)
659 BT_LOGV_STR("Putting event header field.");
660 BT_PUT(notit
->dscopes
.stream_event_header
);
661 BT_LOGV_STR("Putting stream event context field.");
662 BT_PUT(notit
->dscopes
.stream_event_context
);
663 BT_LOGV_STR("Putting event context field.");
664 BT_PUT(notit
->dscopes
.event_context
);
665 BT_LOGV_STR("Putting event payload field.");
666 BT_PUT(notit
->dscopes
.event_payload
);
670 void put_all_dscopes(struct bt_notif_iter
*notit
)
672 BT_LOGV_STR("Putting packet header field.");
673 BT_PUT(notit
->dscopes
.trace_packet_header
);
674 BT_LOGV_STR("Putting packet context field.");
675 BT_PUT(notit
->dscopes
.stream_packet_context
);
676 put_event_dscopes(notit
);
680 enum bt_notif_iter_status
read_packet_header_begin_state(
681 struct bt_notif_iter
*notit
)
683 struct bt_field_type
*packet_header_type
= NULL
;
684 enum bt_notif_iter_status ret
= BT_NOTIF_ITER_STATUS_OK
;
686 if (bt_notif_iter_switch_packet(notit
)) {
687 BT_LOGW("Cannot switch packet: notit-addr=%p", notit
);
688 ret
= BT_NOTIF_ITER_STATUS_ERROR
;
692 /* Packet header type is common to the whole trace. */
693 packet_header_type
= bt_trace_borrow_packet_header_field_type(
695 if (!packet_header_type
) {
696 notit
->state
= STATE_AFTER_TRACE_PACKET_HEADER
;
700 BT_LOGV("Decoding packet header field:"
701 "notit-addr=%p, trace-addr=%p, trace-name=\"%s\", ft-addr=%p",
702 notit
, notit
->meta
.trace
,
703 bt_trace_get_name(notit
->meta
.trace
), packet_header_type
);
704 ret
= read_dscope_begin_state(notit
, packet_header_type
,
705 STATE_AFTER_TRACE_PACKET_HEADER
,
706 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
707 ¬it
->dscopes
.trace_packet_header
);
709 BT_LOGW("Cannot decode packet header field: "
710 "notit-addr=%p, trace-addr=%p, "
711 "trace-name=\"%s\", ft-addr=%p",
712 notit
, notit
->meta
.trace
,
713 bt_trace_get_name(notit
->meta
.trace
),
722 enum bt_notif_iter_status
read_packet_header_continue_state(
723 struct bt_notif_iter
*notit
)
725 return read_dscope_continue_state(notit
,
726 STATE_AFTER_TRACE_PACKET_HEADER
);
730 bool is_struct_type(struct bt_field_type
*field_type
)
732 return bt_field_type_get_type_id(field_type
) ==
733 BT_FIELD_TYPE_ID_STRUCT
;
737 bool is_variant_type(struct bt_field_type
*field_type
)
739 return bt_field_type_get_type_id(field_type
) ==
740 BT_FIELD_TYPE_ID_VARIANT
;
744 struct stream_class_field_path_cache
*
745 create_stream_class_field_path_cache_entry(
746 struct bt_notif_iter
*notit
,
747 struct bt_stream_class
*stream_class
)
751 int timestamp_end
= -1;
752 int packet_size
= -1;
753 int content_size
= -1;
754 struct stream_class_field_path_cache
*cache_entry
= g_new0(
755 struct stream_class_field_path_cache
, 1);
756 struct bt_field_type
*event_header
= NULL
, *packet_context
= NULL
;
759 BT_LOGE_STR("Failed to allocate one stream class field path cache.");
763 event_header
= bt_stream_class_borrow_event_header_field_type(
765 if (event_header
&& bt_field_type_is_structure(event_header
)) {
768 count
= bt_field_type_structure_get_field_count(
770 BT_ASSERT(count
>= 0);
772 for (i
= 0; i
< count
; i
++) {
776 ret
= bt_field_type_structure_borrow_field_by_index(
777 event_header
, &name
, NULL
, i
);
779 BT_LOGE("Cannot get event header structure field type's field: "
780 "notit-addr=%p, stream-class-addr=%p, "
781 "stream-class-name=\"%s\", "
782 "stream-class-id=%" PRId64
", "
783 "ft-addr=%p, index=%d",
785 bt_stream_class_get_name(stream_class
),
786 bt_stream_class_get_id(stream_class
),
791 if (v
!= -1 && id
!= -1) {
795 if (v
== -1 && strcmp(name
, "v") == 0) {
797 } else if (id
== -1 && !strcmp(name
, "id")) {
803 packet_context
= bt_stream_class_borrow_packet_context_field_type(
805 if (packet_context
&& bt_field_type_is_structure(packet_context
)) {
808 count
= bt_field_type_structure_get_field_count(
810 BT_ASSERT(count
>= 0);
812 for (i
= 0; i
< count
; i
++) {
815 struct bt_field_type
*field_type
;
817 if (timestamp_end
!= -1 && packet_size
!= -1 &&
818 content_size
!= -1) {
822 ret
= bt_field_type_structure_borrow_field_by_index(
823 packet_context
, &name
, &field_type
, i
);
825 BT_LOGE("Cannot get packet context structure field type's field: "
826 "notit-addr=%p, stream-class-addr=%p, "
827 "stream-class-name=\"%s\", "
828 "stream-class-id=%" PRId64
", "
829 "ft-addr=%p, index=%d",
831 bt_stream_class_get_name(stream_class
),
832 bt_stream_class_get_id(stream_class
),
837 if (timestamp_end
== -1 &&
838 strcmp(name
, "timestamp_end") == 0) {
839 struct field_cb_override
*override
= g_new0(
840 struct field_cb_override
, 1);
846 override
->func
= btr_timestamp_end_cb
;
847 override
->data
= notit
;
848 g_hash_table_insert(notit
->field_overrides
,
849 field_type
, override
);
851 } else if (packet_size
== -1 &&
852 !strcmp(name
, "packet_size")) {
854 } else if (content_size
== -1 &&
855 !strcmp(name
, "content_size")) {
862 cache_entry
->id
= id
;
863 cache_entry
->timestamp_end
= timestamp_end
;
864 cache_entry
->packet_size
= packet_size
;
865 cache_entry
->content_size
= content_size
;
877 struct stream_class_field_path_cache
*get_stream_class_field_path_cache(
878 struct bt_notif_iter
*notit
,
879 struct bt_stream_class
*stream_class
)
881 bool cache_entry_found
;
882 struct stream_class_field_path_cache
*cache_entry
;
884 cache_entry_found
= g_hash_table_lookup_extended(
885 notit
->sc_field_path_caches
,
886 stream_class
, NULL
, (gpointer
) &cache_entry
);
887 if (unlikely(!cache_entry_found
)) {
888 cache_entry
= create_stream_class_field_path_cache_entry(notit
,
890 g_hash_table_insert(notit
->sc_field_path_caches
,
891 stream_class
, (gpointer
) cache_entry
);
898 enum bt_notif_iter_status
set_current_stream_class(
899 struct bt_notif_iter
*notit
)
901 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
902 struct bt_field_type
*packet_header_type
= NULL
;
903 struct bt_field_type
*stream_id_field_type
= NULL
;
904 struct bt_stream_class
*new_stream_class
= NULL
;
907 /* Clear the current stream class field path cache. */
908 notit
->cur_sc_field_path_cache
= NULL
;
910 /* Is there any "stream_id" field in the packet header? */
911 packet_header_type
= bt_trace_borrow_packet_header_field_type(
913 if (!packet_header_type
) {
915 * No packet header, therefore no `stream_id` field,
916 * therefore only one stream class.
918 goto single_stream_class
;
921 BT_ASSERT(is_struct_type(packet_header_type
));
924 stream_id_field_type
=
925 bt_field_type_structure_borrow_field_type_by_name(
926 packet_header_type
, "stream_id");
927 if (stream_id_field_type
) {
928 /* Find appropriate stream class using current stream ID */
930 struct bt_field
*stream_id_field
= NULL
;
932 BT_ASSERT(notit
->dscopes
.trace_packet_header
);
935 stream_id_field
= bt_field_structure_get_field_by_name(
936 notit
->dscopes
.trace_packet_header
, "stream_id");
937 BT_ASSERT(stream_id_field
);
938 ret
= bt_field_integer_unsigned_get_value(
939 stream_id_field
, &stream_id
);
941 BT_PUT(stream_id_field
);
944 /* Only one stream: pick the first stream class */
945 BT_ASSERT(bt_trace_get_stream_class_count(
946 notit
->meta
.trace
) == 1);
950 BT_LOGV("Found stream class ID to use: notit-addr=%p, "
951 "stream-class-id=%" PRIu64
", "
952 "trace-addr=%p, trace-name=\"%s\"",
953 notit
, stream_id
, notit
->meta
.trace
,
954 bt_trace_get_name(notit
->meta
.trace
));
956 new_stream_class
= bt_trace_borrow_stream_class_by_id(
957 notit
->meta
.trace
, stream_id
);
958 if (!new_stream_class
) {
959 BT_LOGW("No stream class with ID of stream class ID to use in trace: "
960 "notit-addr=%p, stream-class-id=%" PRIu64
", "
961 "trace-addr=%p, trace-name=\"%s\"",
962 notit
, stream_id
, notit
->meta
.trace
,
963 bt_trace_get_name(notit
->meta
.trace
));
964 status
= BT_NOTIF_ITER_STATUS_ERROR
;
968 if (notit
->meta
.stream_class
) {
969 if (new_stream_class
!= notit
->meta
.stream_class
) {
970 BT_LOGW("Two packets refer to two different stream classes within the same packet sequence: "
971 "notit-addr=%p, prev-stream-class-addr=%p, "
972 "prev-stream-class-name=\"%s\", "
973 "prev-stream-class-id=%" PRId64
", "
974 "next-stream-class-addr=%p, "
975 "next-stream-class-name=\"%s\", "
976 "next-stream-class-id=%" PRId64
", "
977 "trace-addr=%p, trace-name=\"%s\"",
978 notit
, notit
->meta
.stream_class
,
979 bt_stream_class_get_name(notit
->meta
.stream_class
),
980 bt_stream_class_get_id(notit
->meta
.stream_class
),
982 bt_stream_class_get_name(new_stream_class
),
983 bt_stream_class_get_id(new_stream_class
),
985 bt_trace_get_name(notit
->meta
.trace
));
986 status
= BT_NOTIF_ITER_STATUS_ERROR
;
990 notit
->meta
.stream_class
= new_stream_class
;
993 BT_LOGV("Set current stream class: "
994 "notit-addr=%p, stream-class-addr=%p, "
995 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
996 notit
, notit
->meta
.stream_class
,
997 bt_stream_class_get_name(notit
->meta
.stream_class
),
998 bt_stream_class_get_id(notit
->meta
.stream_class
));
1001 * Retrieve (or lazily create) the current stream class field path
1004 notit
->cur_sc_field_path_cache
= get_stream_class_field_path_cache(
1005 notit
, notit
->meta
.stream_class
);
1006 if (!notit
->cur_sc_field_path_cache
) {
1007 BT_LOGW("Cannot retrieve stream class field path from cache: "
1008 "notit-addr=%p, stream-class-addr=%p, "
1009 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
1010 notit
, notit
->meta
.stream_class
,
1011 bt_stream_class_get_name(notit
->meta
.stream_class
),
1012 bt_stream_class_get_id(notit
->meta
.stream_class
));
1013 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1022 enum bt_notif_iter_status
after_packet_header_state(
1023 struct bt_notif_iter
*notit
)
1025 enum bt_notif_iter_status status
;
1027 status
= set_current_stream_class(notit
);
1028 if (status
== BT_NOTIF_ITER_STATUS_OK
) {
1029 notit
->state
= STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
;
1036 enum bt_notif_iter_status
read_packet_context_begin_state(
1037 struct bt_notif_iter
*notit
)
1039 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1040 struct bt_field_type
*packet_context_type
;
1042 BT_ASSERT(notit
->meta
.stream_class
);
1043 packet_context_type
= bt_stream_class_borrow_packet_context_field_type(
1044 notit
->meta
.stream_class
);
1045 if (!packet_context_type
) {
1046 BT_LOGV("No packet packet context field type in stream class: continuing: "
1047 "notit-addr=%p, stream-class-addr=%p, "
1048 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
1049 notit
, notit
->meta
.stream_class
,
1050 bt_stream_class_get_name(notit
->meta
.stream_class
),
1051 bt_stream_class_get_id(notit
->meta
.stream_class
));
1052 notit
->state
= STATE_AFTER_STREAM_PACKET_CONTEXT
;
1056 BT_LOGV("Decoding packet context field: "
1057 "notit-addr=%p, stream-class-addr=%p, "
1058 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1060 notit
, notit
->meta
.stream_class
,
1061 bt_stream_class_get_name(notit
->meta
.stream_class
),
1062 bt_stream_class_get_id(notit
->meta
.stream_class
),
1063 packet_context_type
);
1064 status
= read_dscope_begin_state(notit
, packet_context_type
,
1065 STATE_AFTER_STREAM_PACKET_CONTEXT
,
1066 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
1067 ¬it
->dscopes
.stream_packet_context
);
1069 BT_LOGW("Cannot decode packet context field: "
1070 "notit-addr=%p, stream-class-addr=%p, "
1071 "stream-class-name=\"%s\", "
1072 "stream-class-id=%" PRId64
", ft-addr=%p",
1073 notit
, notit
->meta
.stream_class
,
1074 bt_stream_class_get_name(notit
->meta
.stream_class
),
1075 bt_stream_class_get_id(notit
->meta
.stream_class
),
1076 packet_context_type
);
1084 enum bt_notif_iter_status
read_packet_context_continue_state(
1085 struct bt_notif_iter
*notit
)
1087 return read_dscope_continue_state(notit
,
1088 STATE_AFTER_STREAM_PACKET_CONTEXT
);
1092 enum bt_notif_iter_status
set_current_packet_content_sizes(
1093 struct bt_notif_iter
*notit
)
1095 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1096 struct bt_field
*packet_size_field
= NULL
;
1097 struct bt_field
*content_size_field
= NULL
;
1098 uint64_t content_size
= -1ULL, packet_size
= -1ULL;
1100 if (!notit
->dscopes
.stream_packet_context
) {
1104 packet_size_field
= bt_field_structure_get_field_by_name(
1105 notit
->dscopes
.stream_packet_context
, "packet_size");
1106 content_size_field
= bt_field_structure_get_field_by_name(
1107 notit
->dscopes
.stream_packet_context
, "content_size");
1108 if (packet_size_field
) {
1109 int ret
= bt_field_integer_unsigned_get_value(
1110 packet_size_field
, &packet_size
);
1112 BT_ASSERT(ret
== 0);
1113 if (packet_size
== 0) {
1114 BT_LOGW("Invalid packet size: packet context field indicates packet size is zero: "
1115 "notit-addr=%p, packet-context-field-addr=%p",
1116 notit
, notit
->dscopes
.stream_packet_context
);
1117 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1119 } else if ((packet_size
% 8) != 0) {
1120 BT_LOGW("Invalid packet size: packet context field indicates packet size is not a multiple of 8: "
1121 "notit-addr=%p, packet-context-field-addr=%p, "
1122 "packet-size=%" PRIu64
,
1123 notit
, notit
->dscopes
.stream_packet_context
,
1125 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1130 if (content_size_field
) {
1131 int ret
= bt_field_integer_unsigned_get_value(
1132 content_size_field
, &content_size
);
1134 BT_ASSERT(ret
== 0);
1136 content_size
= packet_size
;
1139 if (content_size
> packet_size
) {
1140 BT_LOGW("Invalid packet or content size: packet context field indicates content size is greater than packet size: "
1141 "notit-addr=%p, packet-context-field-addr=%p, "
1142 "packet-size=%" PRIu64
", content-size=%" PRIu64
,
1143 notit
, notit
->dscopes
.stream_packet_context
,
1144 packet_size
, content_size
);
1145 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1149 if (packet_size
!= -1ULL) {
1150 notit
->cur_packet_size
= packet_size
;
1153 * Use the content size as packet size indicator if the
1154 * packet size field is missing. This means there is no
1155 * padding in this stream.
1157 notit
->cur_packet_size
= content_size
;
1159 notit
->cur_content_size
= content_size
;
1160 BT_LOGV("Set current packet and content sizes: "
1161 "notit-addr=%p, packet-size=%" PRIu64
", content-size=%" PRIu64
,
1162 notit
, packet_size
, content_size
);
1164 BT_PUT(packet_size_field
);
1165 BT_PUT(content_size_field
);
1170 enum bt_notif_iter_status
after_packet_context_state(
1171 struct bt_notif_iter
*notit
)
1173 enum bt_notif_iter_status status
;
1175 status
= set_current_packet_content_sizes(notit
);
1176 if (status
== BT_NOTIF_ITER_STATUS_OK
) {
1177 if (notit
->stream_begin_emitted
) {
1178 notit
->state
= STATE_EMIT_NOTIF_NEW_PACKET
;
1180 notit
->state
= STATE_EMIT_NOTIF_NEW_STREAM
;
1188 enum bt_notif_iter_status
read_event_header_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
*event_header_type
= NULL
;
1194 /* Reset the position of the last event header */
1195 notit
->buf
.last_eh_at
= notit
->buf
.at
;
1197 /* Check if we have some content left */
1198 if (notit
->cur_content_size
>= 0) {
1199 if (packet_at(notit
) == notit
->cur_content_size
) {
1200 /* No more events! */
1201 BT_LOGV("Reached end of packet: notit-addr=%p, "
1202 "cur=%zu", notit
, packet_at(notit
));
1203 notit
->state
= STATE_EMIT_NOTIF_END_OF_PACKET
;
1205 } else if (packet_at(notit
) > notit
->cur_content_size
) {
1206 /* That's not supposed to happen */
1207 BT_LOGV("Before decoding event header field: cursor is passed the packet's content: "
1208 "notit-addr=%p, content-size=%" PRId64
", "
1209 "cur=%zu", notit
, notit
->cur_content_size
,
1211 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1216 event_header_type
= bt_stream_class_borrow_event_header_field_type(
1217 notit
->meta
.stream_class
);
1218 if (!event_header_type
) {
1219 notit
->state
= STATE_AFTER_STREAM_EVENT_HEADER
;
1223 put_event_dscopes(notit
);
1224 BT_LOGV("Decoding event header field: "
1225 "notit-addr=%p, stream-class-addr=%p, "
1226 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1228 notit
, notit
->meta
.stream_class
,
1229 bt_stream_class_get_name(notit
->meta
.stream_class
),
1230 bt_stream_class_get_id(notit
->meta
.stream_class
),
1232 status
= read_dscope_begin_state(notit
, event_header_type
,
1233 STATE_AFTER_STREAM_EVENT_HEADER
,
1234 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
,
1235 ¬it
->dscopes
.stream_event_header
);
1237 BT_LOGW("Cannot decode event header field: "
1238 "notit-addr=%p, stream-class-addr=%p, "
1239 "stream-class-name=\"%s\", "
1240 "stream-class-id=%" PRId64
", ft-addr=%p",
1241 notit
, notit
->meta
.stream_class
,
1242 bt_stream_class_get_name(notit
->meta
.stream_class
),
1243 bt_stream_class_get_id(notit
->meta
.stream_class
),
1252 enum bt_notif_iter_status
read_event_header_continue_state(
1253 struct bt_notif_iter
*notit
)
1255 return read_dscope_continue_state(notit
,
1256 STATE_AFTER_STREAM_EVENT_HEADER
);
1260 enum bt_notif_iter_status
set_current_event_class(struct bt_notif_iter
*notit
)
1263 * The assert() calls in this function are okay because it is
1264 * assumed here that all the metadata objects have been
1265 * validated for CTF correctness before decoding actual streams.
1268 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1269 struct bt_field_type
*event_header_type
;
1270 struct bt_field_type
*id_field_type
= NULL
;
1271 struct bt_field_type
*v_field_type
= NULL
;
1272 uint64_t event_id
= -1ULL;
1275 event_header_type
= bt_stream_class_borrow_event_header_field_type(
1276 notit
->meta
.stream_class
);
1277 if (!event_header_type
) {
1279 * No event header, therefore no event class ID field,
1280 * therefore only one event class.
1282 goto single_event_class
;
1285 /* Is there any "id"/"v" field in the event header? */
1286 BT_ASSERT(is_struct_type(event_header_type
));
1287 id_field_type
= bt_field_type_structure_borrow_field_type_by_name(
1288 event_header_type
, "id");
1289 v_field_type
= bt_field_type_structure_borrow_field_type_by_name(
1290 event_header_type
, "v");
1291 BT_ASSERT(notit
->dscopes
.stream_event_header
);
1295 * | | |_ _|_ _| __ __ _
1296 * | | | | | || '_ \ / _` |
1297 * | |___| | | || | | | (_| | S P E C I A L
1298 * |_____|_| |_||_| |_|\__, | C A S E â„¢
1301 struct bt_field
*v_field
= NULL
;
1302 struct bt_field
*v_struct_field
= NULL
;
1303 struct bt_field
*v_struct_id_field
= NULL
;
1305 // TODO: optimalize!
1306 v_field
= bt_field_structure_get_field_by_name(
1307 notit
->dscopes
.stream_event_header
, "v");
1311 bt_field_variant_get_current_field(v_field
);
1312 if (!v_struct_field
) {
1313 goto end_v_field_type
;
1316 // TODO: optimalize!
1318 bt_field_structure_get_field_by_name(v_struct_field
, "id");
1319 if (!v_struct_id_field
) {
1320 goto end_v_field_type
;
1323 if (bt_field_is_integer(v_struct_id_field
)) {
1324 ret
= bt_field_integer_unsigned_get_value(
1325 v_struct_id_field
, &event_id
);
1327 BT_LOGV("Cannot get value of unsigned integer field (`id`): continuing: "
1328 "notit=%p, field-addr=%p",
1329 notit
, v_struct_id_field
);
1336 BT_PUT(v_struct_field
);
1337 BT_PUT(v_struct_id_field
);
1340 if (id_field_type
&& event_id
== -1ULL) {
1341 /* Check "id" field */
1342 struct bt_field
*id_field
= NULL
;
1343 int ret_get_value
= 0;
1345 // TODO: optimalize!
1346 id_field
= bt_field_structure_get_field_by_name(
1347 notit
->dscopes
.stream_event_header
, "id");
1349 goto check_event_id
;
1352 if (bt_field_is_integer(id_field
)) {
1353 ret_get_value
= bt_field_integer_unsigned_get_value(
1354 id_field
, &event_id
);
1355 } else if (bt_field_is_enumeration(id_field
)) {
1356 struct bt_field
*container
;
1358 container
= bt_field_enumeration_get_container(
1360 BT_ASSERT(container
);
1361 ret_get_value
= bt_field_integer_unsigned_get_value(
1362 container
, &event_id
);
1366 BT_ASSERT(ret_get_value
== 0);
1371 if (event_id
== -1ULL) {
1373 /* Event ID not found: single event? */
1374 BT_ASSERT(bt_stream_class_get_event_class_count(
1375 notit
->meta
.stream_class
) == 1);
1379 BT_LOGV("Found event class ID to use: notit-addr=%p, "
1380 "stream-class-addr=%p, stream-class-name=\"%s\", "
1381 "stream-class-id=%" PRId64
", "
1382 "event-class-id=%" PRIu64
,
1383 notit
, notit
->meta
.stream_class
,
1384 bt_stream_class_get_name(notit
->meta
.stream_class
),
1385 bt_stream_class_get_id(notit
->meta
.stream_class
),
1387 notit
->meta
.event_class
= bt_stream_class_borrow_event_class_by_id(
1388 notit
->meta
.stream_class
, event_id
);
1389 if (!notit
->meta
.event_class
) {
1390 BT_LOGW("No event class with ID of event class ID to use in stream class: "
1391 "notit-addr=%p, stream-class-addr=%p, "
1392 "stream-class-name=\"%s\", "
1393 "stream-class-id=%" PRId64
", "
1394 "event-class-id=%" PRIu64
,
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
= BT_NOTIF_ITER_STATUS_ERROR
;
1403 BT_LOGV("Set current event class: "
1404 "notit-addr=%p, event-class-addr=%p, "
1405 "event-class-name=\"%s\", event-class-id=%" PRId64
,
1406 notit
, notit
->meta
.event_class
,
1407 bt_event_class_get_name(notit
->meta
.event_class
),
1408 bt_event_class_get_id(notit
->meta
.event_class
));
1415 enum bt_notif_iter_status
after_event_header_state(
1416 struct bt_notif_iter
*notit
)
1418 enum bt_notif_iter_status status
;
1420 status
= set_current_event_class(notit
);
1421 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
1425 notit
->state
= STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
;
1432 enum bt_notif_iter_status
read_stream_event_context_begin_state(
1433 struct bt_notif_iter
*notit
)
1435 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1436 struct bt_field_type
*stream_event_context_type
;
1438 stream_event_context_type
=
1439 bt_stream_class_borrow_event_context_field_type(
1440 notit
->meta
.stream_class
);
1441 if (!stream_event_context_type
) {
1442 notit
->state
= STATE_DSCOPE_EVENT_CONTEXT_BEGIN
;
1446 BT_LOGV("Decoding stream event context field: "
1447 "notit-addr=%p, stream-class-addr=%p, "
1448 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1450 notit
, notit
->meta
.stream_class
,
1451 bt_stream_class_get_name(notit
->meta
.stream_class
),
1452 bt_stream_class_get_id(notit
->meta
.stream_class
),
1453 stream_event_context_type
);
1454 status
= read_dscope_begin_state(notit
, stream_event_context_type
,
1455 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
,
1456 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
,
1457 ¬it
->dscopes
.stream_event_context
);
1459 BT_LOGW("Cannot decode stream event context field: "
1460 "notit-addr=%p, stream-class-addr=%p, "
1461 "stream-class-name=\"%s\", "
1462 "stream-class-id=%" PRId64
", ft-addr=%p",
1463 notit
, notit
->meta
.stream_class
,
1464 bt_stream_class_get_name(notit
->meta
.stream_class
),
1465 bt_stream_class_get_id(notit
->meta
.stream_class
),
1466 stream_event_context_type
);
1474 enum bt_notif_iter_status
read_stream_event_context_continue_state(
1475 struct bt_notif_iter
*notit
)
1477 return read_dscope_continue_state(notit
,
1478 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
);
1482 enum bt_notif_iter_status
read_event_context_begin_state(
1483 struct bt_notif_iter
*notit
)
1485 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1486 struct bt_field_type
*event_context_type
;
1488 event_context_type
= bt_event_class_borrow_context_field_type(
1489 notit
->meta
.event_class
);
1490 if (!event_context_type
) {
1491 notit
->state
= STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
;
1495 BT_LOGV("Decoding event context field: "
1496 "notit-addr=%p, event-class-addr=%p, "
1497 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1499 notit
, notit
->meta
.event_class
,
1500 bt_event_class_get_name(notit
->meta
.event_class
),
1501 bt_event_class_get_id(notit
->meta
.event_class
),
1502 event_context_type
);
1503 status
= read_dscope_begin_state(notit
, event_context_type
,
1504 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
1505 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
,
1506 ¬it
->dscopes
.event_context
);
1508 BT_LOGW("Cannot decode event context field: "
1509 "notit-addr=%p, event-class-addr=%p, "
1510 "event-class-name=\"%s\", "
1511 "event-class-id=%" PRId64
", ft-addr=%p",
1512 notit
, notit
->meta
.event_class
,
1513 bt_event_class_get_name(notit
->meta
.event_class
),
1514 bt_event_class_get_id(notit
->meta
.event_class
),
1515 event_context_type
);
1523 enum bt_notif_iter_status
read_event_context_continue_state(
1524 struct bt_notif_iter
*notit
)
1526 return read_dscope_continue_state(notit
,
1527 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
);
1531 enum bt_notif_iter_status
read_event_payload_begin_state(
1532 struct bt_notif_iter
*notit
)
1534 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1535 struct bt_field_type
*event_payload_type
;
1537 event_payload_type
= bt_event_class_borrow_payload_field_type(
1538 notit
->meta
.event_class
);
1539 if (!event_payload_type
) {
1540 notit
->state
= STATE_EMIT_NOTIF_EVENT
;
1544 BT_LOGV("Decoding event payload field: "
1545 "notit-addr=%p, event-class-addr=%p, "
1546 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1548 notit
, notit
->meta
.event_class
,
1549 bt_event_class_get_name(notit
->meta
.event_class
),
1550 bt_event_class_get_id(notit
->meta
.event_class
),
1551 event_payload_type
);
1552 status
= read_dscope_begin_state(notit
, event_payload_type
,
1553 STATE_EMIT_NOTIF_EVENT
,
1554 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
1555 ¬it
->dscopes
.event_payload
);
1557 BT_LOGW("Cannot decode event payload field: "
1558 "notit-addr=%p, event-class-addr=%p, "
1559 "event-class-name=\"%s\", "
1560 "event-class-id=%" PRId64
", ft-addr=%p",
1561 notit
, notit
->meta
.event_class
,
1562 bt_event_class_get_name(notit
->meta
.event_class
),
1563 bt_event_class_get_id(notit
->meta
.event_class
),
1564 event_payload_type
);
1572 enum bt_notif_iter_status
read_event_payload_continue_state(
1573 struct bt_notif_iter
*notit
)
1575 return read_dscope_continue_state(notit
, STATE_EMIT_NOTIF_EVENT
);
1579 enum bt_notif_iter_status
skip_packet_padding_state(
1580 struct bt_notif_iter
*notit
)
1582 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1583 size_t bits_to_skip
;
1585 BT_ASSERT(notit
->cur_packet_size
> 0);
1586 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1587 if (bits_to_skip
== 0) {
1588 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1591 size_t bits_to_consume
;
1593 BT_LOGV("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu",
1594 bits_to_skip
, notit
, bits_to_skip
);
1595 status
= buf_ensure_available_bits(notit
);
1596 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
1600 bits_to_consume
= MIN(buf_available_bits(notit
), bits_to_skip
);
1601 BT_LOGV("Skipping %zu bits of padding: notit-addr=%p, size=%zu",
1602 bits_to_consume
, notit
, bits_to_consume
);
1603 buf_consume_bits(notit
, bits_to_consume
);
1604 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1605 if (bits_to_skip
== 0) {
1606 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1616 enum bt_notif_iter_status
handle_state(struct bt_notif_iter
*notit
)
1618 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1619 const enum state state
= notit
->state
;
1621 BT_LOGV("Handling state: notit-addr=%p, state=%s",
1622 notit
, state_string(state
));
1624 // TODO: optimalize!
1627 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1629 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
1630 status
= read_packet_header_begin_state(notit
);
1632 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
1633 status
= read_packet_header_continue_state(notit
);
1635 case STATE_AFTER_TRACE_PACKET_HEADER
:
1636 status
= after_packet_header_state(notit
);
1638 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
1639 status
= read_packet_context_begin_state(notit
);
1641 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
1642 status
= read_packet_context_continue_state(notit
);
1644 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
1645 status
= after_packet_context_state(notit
);
1647 case STATE_EMIT_NOTIF_NEW_STREAM
:
1648 notit
->state
= STATE_EMIT_NOTIF_NEW_PACKET
;
1650 case STATE_EMIT_NOTIF_NEW_PACKET
:
1651 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1653 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
:
1654 status
= read_event_header_begin_state(notit
);
1656 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
:
1657 status
= read_event_header_continue_state(notit
);
1659 case STATE_AFTER_STREAM_EVENT_HEADER
:
1660 status
= after_event_header_state(notit
);
1662 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
:
1663 status
= read_stream_event_context_begin_state(notit
);
1665 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
:
1666 status
= read_stream_event_context_continue_state(notit
);
1668 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN
:
1669 status
= read_event_context_begin_state(notit
);
1671 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
:
1672 status
= read_event_context_continue_state(notit
);
1674 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
1675 status
= read_event_payload_begin_state(notit
);
1677 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
1678 status
= read_event_payload_continue_state(notit
);
1680 case STATE_EMIT_NOTIF_EVENT
:
1681 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1683 case STATE_SKIP_PACKET_PADDING
:
1684 status
= skip_packet_padding_state(notit
);
1686 case STATE_EMIT_NOTIF_END_OF_PACKET
:
1687 notit
->state
= STATE_SKIP_PACKET_PADDING
;
1690 BT_LOGD("Unknown CTF plugin notification iterator state: "
1691 "notit-addr=%p, state=%d", notit
, notit
->state
);
1695 BT_LOGV("Handled state: notit-addr=%p, status=%s, "
1696 "prev-state=%s, cur-state=%s",
1697 notit
, bt_notif_iter_status_string(status
),
1698 state_string(state
), state_string(notit
->state
));
1703 * Resets the internal state of a CTF notification iterator.
1706 void bt_notif_iter_reset(struct bt_notif_iter
*notit
)
1709 BT_LOGD("Resetting notification iterator: addr=%p", notit
);
1710 stack_clear(notit
->stack
);
1711 notit
->meta
.stream_class
= NULL
;
1712 notit
->meta
.event_class
= NULL
;
1713 BT_PUT(notit
->packet
);
1714 BT_PUT(notit
->stream
);
1715 put_all_dscopes(notit
);
1716 notit
->buf
.addr
= NULL
;
1719 notit
->buf
.last_eh_at
= SIZE_MAX
;
1720 notit
->buf
.packet_offset
= 0;
1721 notit
->state
= STATE_INIT
;
1722 notit
->cur_content_size
= -1;
1723 notit
->cur_packet_size
= -1;
1724 notit
->cur_packet_offset
= -1;
1725 notit
->stream_begin_emitted
= false;
1729 int bt_notif_iter_switch_packet(struct bt_notif_iter
*notit
)
1734 * We don't put the stream class here because we need to make
1735 * sure that all the packets processed by the same notification
1736 * iterator refer to the same stream class (the first one).
1739 if (notit
->cur_packet_size
!= -1) {
1740 notit
->cur_packet_offset
+= notit
->cur_packet_size
;
1742 BT_LOGV("Switching packet: notit-addr=%p, cur=%zu, "
1743 "packet-offset=%" PRId64
, notit
, notit
->buf
.at
,
1744 notit
->cur_packet_offset
);
1745 stack_clear(notit
->stack
);
1746 notit
->meta
.event_class
= NULL
;
1747 BT_PUT(notit
->packet
);
1748 BT_PUT(notit
->cur_timestamp_end
);
1749 put_all_dscopes(notit
);
1752 * Adjust current buffer so that addr points to the beginning of the new
1755 if (notit
->buf
.addr
) {
1756 size_t consumed_bytes
= (size_t) (notit
->buf
.at
/ CHAR_BIT
);
1758 /* Packets are assumed to start on a byte frontier. */
1759 if (notit
->buf
.at
% CHAR_BIT
) {
1760 BT_LOGW("Cannot switch packet: current position is not a multiple of 8: "
1761 "notit-addr=%p, cur=%zu", notit
, notit
->buf
.at
);
1766 notit
->buf
.addr
+= consumed_bytes
;
1767 notit
->buf
.sz
-= consumed_bytes
;
1769 notit
->buf
.packet_offset
= 0;
1770 BT_LOGV("Adjusted buffer: addr=%p, size=%zu",
1771 notit
->buf
.addr
, notit
->buf
.sz
);
1774 notit
->cur_content_size
= -1;
1775 notit
->cur_packet_size
= -1;
1776 notit
->cur_sc_field_path_cache
= NULL
;
1783 struct bt_field
*get_next_field(struct bt_notif_iter
*notit
)
1785 struct bt_field
*next_field
= NULL
;
1786 struct bt_field
*base_field
;
1787 struct bt_field_type
*base_type
;
1790 BT_ASSERT(!stack_empty(notit
->stack
));
1791 index
= stack_top(notit
->stack
)->index
;
1792 base_field
= stack_top(notit
->stack
)->base
;
1793 BT_ASSERT(base_field
);
1794 base_type
= bt_field_borrow_type(base_field
);
1795 BT_ASSERT(base_type
);
1797 switch (bt_field_type_get_type_id(base_type
)) {
1798 case BT_FIELD_TYPE_ID_STRUCT
:
1800 next_field
= bt_field_structure_get_field_by_index(
1804 case BT_FIELD_TYPE_ID_ARRAY
:
1805 next_field
= bt_field_array_get_field(base_field
, index
);
1807 case BT_FIELD_TYPE_ID_SEQUENCE
:
1808 next_field
= bt_field_sequence_get_field(base_field
, index
);
1810 case BT_FIELD_TYPE_ID_VARIANT
:
1811 next_field
= bt_field_variant_get_current_field(base_field
);
1814 BT_LOGF("Unknown base field type ID: "
1815 "notit-addr=%p, ft-addr=%p, ft-id=%s",
1817 bt_common_field_type_id_string(
1818 bt_field_type_get_type_id(base_type
)));
1826 void update_clock_state(uint64_t *state
,
1827 struct bt_field
*value_field
)
1829 struct bt_field_type
*value_type
= NULL
;
1830 uint64_t requested_new_value
;
1831 uint64_t requested_new_value_mask
;
1832 uint64_t cur_value_masked
;
1833 int requested_new_value_size
;
1836 value_type
= bt_field_borrow_type(value_field
);
1837 BT_ASSERT(value_type
);
1838 BT_ASSERT(bt_field_type_is_integer(value_type
));
1839 requested_new_value_size
=
1840 bt_field_type_integer_get_size(value_type
);
1841 BT_ASSERT(requested_new_value_size
> 0);
1842 ret
= bt_field_integer_unsigned_get_value(value_field
,
1843 &requested_new_value
);
1847 * Special case for a 64-bit new value, which is the limit
1848 * of a clock value as of this version: overwrite the
1849 * current value directly.
1851 if (requested_new_value_size
== 64) {
1852 *state
= requested_new_value
;
1856 requested_new_value_mask
= (1ULL << requested_new_value_size
) - 1;
1857 cur_value_masked
= *state
& requested_new_value_mask
;
1859 if (requested_new_value
< cur_value_masked
) {
1861 * It looks like a wrap happened on the number of bits
1862 * of the requested new value. Assume that the clock
1863 * value wrapped only one time.
1865 *state
+= requested_new_value_mask
+ 1;
1868 /* Clear the low bits of the current clock value. */
1869 *state
&= ~requested_new_value_mask
;
1871 /* Set the low bits of the current clock value. */
1872 *state
|= requested_new_value
;
1875 BT_LOGV("Updated clock's value from integer field's value: "
1876 "value=%" PRIu64
, *state
);
1880 enum bt_btr_status
update_clock(struct bt_notif_iter
*notit
,
1881 struct bt_field
*int_field
)
1883 gboolean clock_class_found
;
1884 uint64_t *clock_state
= NULL
;
1885 struct bt_field_type
*int_field_type
= NULL
;
1886 enum bt_btr_status ret
= BT_BTR_STATUS_OK
;
1887 struct bt_clock_class
*clock_class
= NULL
;
1889 int_field_type
= bt_field_borrow_type(int_field
);
1890 BT_ASSERT(int_field_type
);
1891 clock_class
= bt_field_type_integer_borrow_mapped_clock_class(
1893 if (likely(!clock_class
)) {
1897 clock_class_found
= g_hash_table_lookup_extended(notit
->clock_states
,
1898 clock_class
, NULL
, (gpointer
) &clock_state
);
1899 if (!clock_class_found
) {
1900 clock_state
= g_new0(uint64_t, 1);
1902 BT_LOGE_STR("Failed to allocate a uint64_t.");
1903 ret
= BT_BTR_STATUS_ENOMEM
;
1907 g_hash_table_insert(notit
->clock_states
, clock_class
,
1911 /* Update the clock's state. */
1912 BT_LOGV("Updating notification iterator's clock's value from integer field: "
1913 "notit-addr=%p, clock-class-addr=%p, "
1914 "clock-class-name=\"%s\", value=%" PRIu64
,
1916 bt_clock_class_get_name(clock_class
), *clock_state
);
1917 update_clock_state(clock_state
, int_field
);
1924 enum bt_btr_status
btr_unsigned_int_common(uint64_t value
,
1925 struct bt_field_type
*type
, void *data
,
1926 struct bt_field
**out_int_field
)
1928 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
1929 struct bt_field
*field
= NULL
;
1930 struct bt_field
*int_field
= NULL
;
1931 struct bt_notif_iter
*notit
= data
;
1934 BT_LOGV("Common unsigned integer function called from BTR: "
1935 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
1936 "ft-id=%s, value=%" PRIu64
,
1937 notit
, notit
->btr
, type
,
1938 bt_common_field_type_id_string(
1939 bt_field_type_get_type_id(type
)),
1942 /* Create next field */
1943 field
= get_next_field(notit
);
1945 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
1946 status
= BT_BTR_STATUS_ERROR
;
1950 switch(bt_field_type_get_type_id(type
)) {
1951 case BT_FIELD_TYPE_ID_INTEGER
:
1952 /* Integer field is created field */
1953 BT_MOVE(int_field
, field
);
1955 case BT_FIELD_TYPE_ID_ENUM
:
1956 int_field
= bt_field_enumeration_get_container(field
);
1957 BT_ASSERT(int_field
);
1958 type
= bt_field_borrow_type(int_field
);
1962 BT_LOGF("Unexpected field type ID: "
1963 "notit-addr=%p, ft-addr=%p, ft-id=%s",
1965 bt_common_field_type_id_string(
1966 bt_field_type_get_type_id(type
)));
1970 BT_ASSERT(int_field
);
1971 ret
= bt_field_integer_unsigned_set_value(int_field
, value
);
1972 BT_ASSERT(ret
== 0);
1973 stack_top(notit
->stack
)->index
++;
1974 *out_int_field
= int_field
;
1982 enum bt_btr_status
btr_timestamp_end_cb(void *value
,
1983 struct bt_field_type
*type
, void *data
)
1985 enum bt_btr_status status
;
1986 struct bt_field
*field
= NULL
;
1987 struct bt_notif_iter
*notit
= data
;
1989 BT_LOGV("`timestamp_end` unsigned integer function called from BTR: "
1990 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
1992 notit
, notit
->btr
, type
,
1993 bt_common_field_type_id_string(
1994 bt_field_type_get_type_id(type
)));
1995 status
= btr_unsigned_int_common(*((uint64_t *) value
), type
, data
,
1998 /* Set as the current packet's timestamp_end field. */
1999 BT_MOVE(notit
->cur_timestamp_end
, field
);
2004 enum bt_btr_status
btr_unsigned_int_cb(uint64_t value
,
2005 struct bt_field_type
*type
, void *data
)
2007 struct bt_notif_iter
*notit
= data
;
2008 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2009 struct bt_field
*field
= NULL
;
2010 struct field_cb_override
*override
;
2012 BT_LOGV("Unsigned integer function called from BTR: "
2013 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2014 "ft-id=%s, value=%" PRIu64
,
2015 notit
, notit
->btr
, type
,
2016 bt_common_field_type_id_string(
2017 bt_field_type_get_type_id(type
)),
2019 override
= g_hash_table_lookup(notit
->field_overrides
, type
);
2020 if (unlikely(override
)) {
2021 /* Override function logs errors */
2022 status
= override
->func(&value
, type
, override
->data
);
2026 status
= btr_unsigned_int_common(value
, type
, data
, &field
);
2027 if (status
!= BT_BTR_STATUS_OK
) {
2028 /* btr_unsigned_int_common() logs errors */
2032 status
= update_clock(notit
, field
);
2039 enum bt_btr_status
btr_signed_int_cb(int64_t value
,
2040 struct bt_field_type
*type
, void *data
)
2042 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2043 struct bt_field
*field
= NULL
;
2044 struct bt_field
*int_field
= NULL
;
2045 struct bt_notif_iter
*notit
= data
;
2048 BT_LOGV("Signed integer function called from BTR: "
2049 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2050 "ft-id=%s, value=%" PRId64
,
2051 notit
, notit
->btr
, type
,
2052 bt_common_field_type_id_string(
2053 bt_field_type_get_type_id(type
)),
2056 /* create next field */
2057 field
= get_next_field(notit
);
2059 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2060 status
= BT_BTR_STATUS_ERROR
;
2064 switch(bt_field_type_get_type_id(type
)) {
2065 case BT_FIELD_TYPE_ID_INTEGER
:
2066 /* Integer field is created field */
2067 BT_MOVE(int_field
, field
);
2069 case BT_FIELD_TYPE_ID_ENUM
:
2070 int_field
= bt_field_enumeration_get_container(field
);
2071 BT_ASSERT(int_field
);
2072 type
= bt_field_borrow_type(int_field
);
2076 BT_LOGF("Unexpected field type ID: "
2077 "notit-addr=%p, ft-addr=%p, ft-id=%s",
2079 bt_common_field_type_id_string(
2080 bt_field_type_get_type_id(type
)));
2084 BT_ASSERT(int_field
);
2085 ret
= bt_field_integer_signed_set_value(int_field
, value
);
2087 stack_top(notit
->stack
)->index
++;
2088 status
= update_clock(notit
, int_field
);
2097 enum bt_btr_status
btr_floating_point_cb(double value
,
2098 struct bt_field_type
*type
, void *data
)
2100 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2101 struct bt_field
*field
= NULL
;
2102 struct bt_notif_iter
*notit
= data
;
2105 BT_LOGV("Floating point number function called from BTR: "
2106 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2107 "ft-id=%s, value=%f",
2108 notit
, notit
->btr
, type
,
2109 bt_common_field_type_id_string(
2110 bt_field_type_get_type_id(type
)),
2113 /* Create next field */
2114 field
= get_next_field(notit
);
2116 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2117 status
= BT_BTR_STATUS_ERROR
;
2121 ret
= bt_field_floating_point_set_value(field
, value
);
2123 stack_top(notit
->stack
)->index
++;
2131 enum bt_btr_status
btr_string_begin_cb(
2132 struct bt_field_type
*type
, void *data
)
2134 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2135 struct bt_field
*field
= NULL
;
2136 struct bt_notif_iter
*notit
= data
;
2139 BT_LOGV("String (beginning) function called from BTR: "
2140 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2142 notit
, notit
->btr
, type
,
2143 bt_common_field_type_id_string(
2144 bt_field_type_get_type_id(type
)));
2146 /* Create next field */
2147 field
= get_next_field(notit
);
2149 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2150 status
= BT_BTR_STATUS_ERROR
;
2155 * Push on stack. Not a compound type per se, but we know that only
2156 * btr_string_cb() may be called between this call and a subsequent
2157 * call to btr_string_end_cb().
2159 ret
= stack_push(notit
->stack
, field
);
2161 BT_LOGE("Cannot push string field on stack: "
2162 "notit-addr=%p, field-addr=%p", notit
, field
);
2163 status
= BT_BTR_STATUS_ERROR
;
2168 * Initialize string field payload to an empty string since in the
2169 * case of a length 0 string the btr_string_cb won't be called and
2170 * we will end up with an unset string payload.
2172 ret
= bt_field_string_set_value(field
, "");
2174 BT_LOGE("Cannot initialize string field's value to an empty string: "
2175 "notit-addr=%p, field-addr=%p, ret=%d",
2177 status
= BT_BTR_STATUS_ERROR
;
2188 enum bt_btr_status
btr_string_cb(const char *value
,
2189 size_t len
, struct bt_field_type
*type
, void *data
)
2191 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2192 struct bt_field
*field
= NULL
;
2193 struct bt_notif_iter
*notit
= data
;
2196 BT_LOGV("String (substring) function called from BTR: "
2197 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2198 "ft-id=%s, string-length=%zu",
2199 notit
, notit
->btr
, type
,
2200 bt_common_field_type_id_string(
2201 bt_field_type_get_type_id(type
)),
2204 /* Get string field */
2205 field
= stack_top(notit
->stack
)->base
;
2208 /* Append current string */
2209 ret
= bt_field_string_append_len(field
, value
, len
);
2211 BT_LOGE("Cannot append substring to string field's value: "
2212 "notit-addr=%p, field-addr=%p, string-length=%zu, "
2213 "ret=%d", notit
, field
, len
, ret
);
2214 status
= BT_BTR_STATUS_ERROR
;
2223 enum bt_btr_status
btr_string_end_cb(
2224 struct bt_field_type
*type
, void *data
)
2226 struct bt_notif_iter
*notit
= data
;
2228 BT_LOGV("String (end) function called from BTR: "
2229 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2231 notit
, notit
->btr
, type
,
2232 bt_common_field_type_id_string(
2233 bt_field_type_get_type_id(type
)));
2235 /* Pop string field */
2236 stack_pop(notit
->stack
);
2238 /* Go to next field */
2239 stack_top(notit
->stack
)->index
++;
2240 return BT_BTR_STATUS_OK
;
2243 enum bt_btr_status
btr_compound_begin_cb(
2244 struct bt_field_type
*type
, void *data
)
2246 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2247 struct bt_notif_iter
*notit
= data
;
2248 struct bt_field
*field
;
2251 BT_LOGV("Compound (beginning) function called from BTR: "
2252 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2254 notit
, notit
->btr
, type
,
2255 bt_common_field_type_id_string(
2256 bt_field_type_get_type_id(type
)));
2259 if (stack_empty(notit
->stack
)) {
2260 /* Root: create dynamic scope field */
2261 *notit
->cur_dscope_field
= bt_field_create(type
);
2262 field
= *notit
->cur_dscope_field
;
2265 * Field will be put at the end of this function
2266 * (stack_push() will take one reference, but this
2267 * reference is lost upon the equivalent stack_pop()
2268 * later), so also get it for our context to own it.
2270 bt_get(*notit
->cur_dscope_field
);
2273 BT_LOGE("Cannot create compound field: "
2274 "notit-addr=%p, ft-addr=%p, ft-id=%s",
2276 bt_common_field_type_id_string(
2277 bt_field_type_get_type_id(type
)));
2278 status
= BT_BTR_STATUS_ERROR
;
2282 field
= get_next_field(notit
);
2284 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2285 status
= BT_BTR_STATUS_ERROR
;
2292 ret
= stack_push(notit
->stack
, field
);
2294 BT_LOGE("Cannot push compound field onto the stack: "
2295 "notit-addr=%p, ft-addr=%p, ft-id=%s, ret=%d",
2297 bt_common_field_type_id_string(
2298 bt_field_type_get_type_id(type
)),
2300 status
= BT_BTR_STATUS_ERROR
;
2309 enum bt_btr_status
btr_compound_end_cb(
2310 struct bt_field_type
*type
, void *data
)
2312 struct bt_notif_iter
*notit
= data
;
2314 BT_LOGV("Compound (end) function called from BTR: "
2315 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2317 notit
, notit
->btr
, type
,
2318 bt_common_field_type_id_string(
2319 bt_field_type_get_type_id(type
)));
2320 BT_ASSERT(!stack_empty(notit
->stack
));
2323 stack_pop(notit
->stack
);
2325 /* If the stack is not empty, increment the base's index */
2326 if (!stack_empty(notit
->stack
)) {
2327 stack_top(notit
->stack
)->index
++;
2330 return BT_BTR_STATUS_OK
;
2334 struct bt_field
*resolve_field(struct bt_notif_iter
*notit
,
2335 struct bt_field_path
*path
)
2337 struct bt_field
*field
= NULL
;
2340 if (BT_LOG_ON_VERBOSE
) {
2341 GString
*gstr
= bt_field_path_string(path
);
2343 BT_LOGV("Resolving field path: notit-addr=%p, field-path=\"%s\"",
2344 notit
, gstr
? gstr
->str
: NULL
);
2347 g_string_free(gstr
, TRUE
);
2351 switch (bt_field_path_get_root_scope(path
)) {
2352 case BT_SCOPE_TRACE_PACKET_HEADER
:
2353 field
= notit
->dscopes
.trace_packet_header
;
2355 case BT_SCOPE_STREAM_PACKET_CONTEXT
:
2356 field
= notit
->dscopes
.stream_packet_context
;
2358 case BT_SCOPE_STREAM_EVENT_HEADER
:
2359 field
= notit
->dscopes
.stream_event_header
;
2361 case BT_SCOPE_STREAM_EVENT_CONTEXT
:
2362 field
= notit
->dscopes
.stream_event_context
;
2364 case BT_SCOPE_EVENT_CONTEXT
:
2365 field
= notit
->dscopes
.event_context
;
2367 case BT_SCOPE_EVENT_FIELDS
:
2368 field
= notit
->dscopes
.event_payload
;
2371 BT_LOGF("Cannot resolve field path: unknown scope: "
2372 "notit-addr=%p, root-scope=%s",
2373 notit
, bt_common_scope_string(
2374 bt_field_path_get_root_scope(path
)));
2379 BT_LOGW("Cannot resolve field path: root field not found: "
2380 "notit-addr=%p, root-scope=%s",
2381 notit
, bt_common_scope_string(
2382 bt_field_path_get_root_scope(path
)));
2388 for (i
= 0; i
< bt_field_path_get_index_count(path
); ++i
) {
2389 struct bt_field
*next_field
= NULL
;
2390 struct bt_field_type
*field_type
;
2391 int index
= bt_field_path_get_index(path
, i
);
2393 field_type
= bt_field_borrow_type(field
);
2394 BT_ASSERT(field_type
);
2396 if (is_struct_type(field_type
)) {
2397 next_field
= bt_field_structure_get_field_by_index(
2399 } else if (is_variant_type(field_type
)) {
2401 bt_field_variant_get_current_field(field
);
2407 BT_LOGW("Cannot find next field: "
2408 "notit-addr=%p, ft-addr=%p, ft-id=%s, index=%d",
2410 bt_common_field_type_id_string(
2411 bt_field_type_get_type_id(field_type
)),
2416 /* Move next field -> field */
2417 BT_MOVE(field
, next_field
);
2425 int64_t btr_get_sequence_length_cb(struct bt_field_type
*type
, void *data
)
2429 struct bt_field
*seq_field
;
2430 struct bt_field_path
*field_path
;
2431 struct bt_notif_iter
*notit
= data
;
2432 struct bt_field
*length_field
= NULL
;
2435 field_path
= bt_field_type_sequence_borrow_length_field_path(type
);
2436 BT_ASSERT(field_path
);
2437 length_field
= resolve_field(notit
, field_path
);
2438 if (!length_field
) {
2439 BT_LOGW("Cannot resolve sequence field type's length field path: "
2440 "notit-addr=%p, ft-addr=%p",
2445 iret
= bt_field_integer_unsigned_get_value(length_field
, &length
);
2447 BT_LOGE("Cannot get value of sequence length field: "
2448 "notit-addr=%p, field-addr=%p",
2449 notit
, length_field
);
2453 seq_field
= stack_top(notit
->stack
)->base
;
2454 iret
= bt_field_sequence_set_length(seq_field
, length_field
);
2456 BT_LOGE("Cannot set sequence field's length field: "
2457 "notit-addr=%p, seq-field-addr=%p, "
2458 "length-field-addr=%p, ",
2459 notit
, seq_field
, length_field
);
2463 ret
= (int64_t) length
;
2466 BT_PUT(length_field
);
2471 struct bt_field_type
*btr_borrow_variant_field_type_cb(
2472 struct bt_field_type
*type
, void *data
)
2474 struct bt_field_path
*path
;
2475 struct bt_notif_iter
*notit
= data
;
2476 struct bt_field
*var_field
;
2477 struct bt_field
*tag_field
= NULL
;
2478 struct bt_field
*selected_field
= NULL
;
2479 struct bt_field_type
*selected_field_type
= NULL
;
2481 path
= bt_field_type_variant_borrow_tag_field_path(type
);
2483 tag_field
= resolve_field(notit
, path
);
2485 BT_LOGW("Cannot resolve variant field type's tag field path: "
2486 "notit-addr=%p, ft-addr=%p",
2492 * We found the enumeration tag field instance which should be
2493 * able to select a current field for this variant. This
2494 * callback function we're in is called _after_
2495 * compound_begin(), so the current stack top's base field is
2496 * the variant field in question. We get the selected field here
2497 * thanks to this tag field (thus creating the selected field),
2498 * which will also provide us with its type. Then, this field
2499 * will remain the current selected one until the next callback
2500 * function call which is used to fill the current selected
2503 var_field
= stack_top(notit
->stack
)->base
;
2504 selected_field
= bt_field_variant_get_field(var_field
, tag_field
);
2505 if (!selected_field
) {
2506 BT_LOGW("Cannot get variant field's selection using tag field: "
2507 "notit-addr=%p, var-field-addr=%p, tag-field-addr=%p",
2508 notit
, var_field
, tag_field
);
2512 selected_field_type
= bt_field_borrow_type(selected_field
);
2516 BT_PUT(selected_field
);
2517 return selected_field_type
;
2521 int set_event_clocks(struct bt_event
*event
,
2522 struct bt_notif_iter
*notit
)
2525 GHashTableIter iter
;
2526 struct bt_clock_class
*clock_class
;
2527 uint64_t *clock_state
;
2529 g_hash_table_iter_init(&iter
, notit
->clock_states
);
2531 while (g_hash_table_iter_next(&iter
, (gpointer
) &clock_class
,
2532 (gpointer
) &clock_state
)) {
2533 struct bt_clock_value
*clock_value
;
2535 clock_value
= bt_clock_value_create(clock_class
,
2538 BT_LOGE("Cannot create clock value from clock class: "
2539 "notit-addr=%p, clock-class-addr=%p, "
2540 "clock-class-name=\"%s\"",
2542 bt_clock_class_get_name(clock_class
));
2546 ret
= bt_event_set_clock_value(event
, clock_value
);
2547 bt_put(clock_value
);
2549 struct bt_event_class
*event_class
=
2550 bt_event_borrow_class(event
);
2552 BT_ASSERT(event_class
);
2553 BT_LOGE("Cannot set event's clock value: "
2554 "notit-addr=%p, event-addr=%p, "
2555 "event-class-name=\"%s\", "
2556 "event-class-id=%" PRId64
", "
2557 "clock-class-addr=%p, "
2558 "clock-class-name=\"%s\", "
2559 "clock-value-addr=%p",
2561 bt_event_class_get_name(event_class
),
2562 bt_event_class_get_id(event_class
),
2564 bt_clock_class_get_name(clock_class
),
2576 struct bt_event
*create_event(struct bt_notif_iter
*notit
)
2579 struct bt_event
*event
;
2581 BT_LOGV("Creating event for event notification: "
2582 "notit-addr=%p, event-class-addr=%p, "
2583 "event-class-name=\"%s\", "
2584 "event-class-id=%" PRId64
,
2585 notit
, notit
->meta
.event_class
,
2586 bt_event_class_get_name(notit
->meta
.event_class
),
2587 bt_event_class_get_id(notit
->meta
.event_class
));
2589 /* Create event object. */
2590 event
= bt_event_create(notit
->meta
.event_class
);
2592 BT_LOGE("Cannot create event: "
2593 "notit-addr=%p, event-class-addr=%p, "
2594 "event-class-name=\"%s\", "
2595 "event-class-id=%" PRId64
,
2596 notit
, notit
->meta
.event_class
,
2597 bt_event_class_get_name(notit
->meta
.event_class
),
2598 bt_event_class_get_id(notit
->meta
.event_class
));
2602 /* Set header, stream event context, context, and payload fields. */
2603 ret
= bt_event_set_header(event
,
2604 notit
->dscopes
.stream_event_header
);
2606 BT_LOGE("Cannot set event's header field: "
2607 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2608 "event-class-name=\"%s\", "
2609 "event-class-id=%" PRId64
", field-addr=%p",
2610 notit
, event
, notit
->meta
.event_class
,
2611 bt_event_class_get_name(notit
->meta
.event_class
),
2612 bt_event_class_get_id(notit
->meta
.event_class
),
2613 notit
->dscopes
.stream_event_header
);
2617 ret
= bt_event_set_stream_event_context(event
,
2618 notit
->dscopes
.stream_event_context
);
2620 BT_LOGE("Cannot set event's context field: "
2621 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2622 "event-class-name=\"%s\", "
2623 "event-class-id=%" PRId64
", field-addr=%p",
2624 notit
, event
, notit
->meta
.event_class
,
2625 bt_event_class_get_name(notit
->meta
.event_class
),
2626 bt_event_class_get_id(notit
->meta
.event_class
),
2627 notit
->dscopes
.stream_event_context
);
2631 ret
= bt_event_set_context(event
,
2632 notit
->dscopes
.event_context
);
2634 BT_LOGE("Cannot set event's stream event context field: "
2635 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2636 "event-class-name=\"%s\", "
2637 "event-class-id=%" PRId64
", field-addr=%p",
2638 notit
, event
, notit
->meta
.event_class
,
2639 bt_event_class_get_name(notit
->meta
.event_class
),
2640 bt_event_class_get_id(notit
->meta
.event_class
),
2641 notit
->dscopes
.event_context
);
2645 ret
= bt_event_set_payload(event
,
2646 notit
->dscopes
.event_payload
);
2648 BT_LOGE("Cannot set event's payload field: "
2649 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2650 "event-class-name=\"%s\", "
2651 "event-class-id=%" PRId64
", field-addr=%p",
2652 notit
, event
, notit
->meta
.event_class
,
2653 bt_event_class_get_name(notit
->meta
.event_class
),
2654 bt_event_class_get_id(notit
->meta
.event_class
),
2655 notit
->dscopes
.event_payload
);
2659 ret
= set_event_clocks(event
, notit
);
2661 BT_LOGE("Cannot set event's clock values: "
2662 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2663 "event-class-name=\"%s\", "
2664 "event-class-id=%" PRId64
,
2665 notit
, event
, notit
->meta
.event_class
,
2666 bt_event_class_get_name(notit
->meta
.event_class
),
2667 bt_event_class_get_id(notit
->meta
.event_class
));
2671 /* Associate with current packet. */
2672 BT_ASSERT(notit
->packet
);
2673 ret
= bt_event_set_packet(event
, notit
->packet
);
2675 BT_LOGE("Cannot set event's header field: "
2676 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2677 "event-class-name=\"%s\", "
2678 "event-class-id=%" PRId64
", packet-addr=%p",
2679 notit
, event
, notit
->meta
.event_class
,
2680 bt_event_class_get_name(notit
->meta
.event_class
),
2681 bt_event_class_get_id(notit
->meta
.event_class
),
2696 uint64_t get_cur_stream_instance_id(struct bt_notif_iter
*notit
)
2698 struct bt_field
*stream_instance_id_field
= NULL
;
2699 uint64_t stream_instance_id
= -1ULL;
2702 if (!notit
->dscopes
.trace_packet_header
) {
2706 stream_instance_id_field
= bt_field_structure_get_field_by_name(
2707 notit
->dscopes
.trace_packet_header
, "stream_instance_id");
2708 if (!stream_instance_id_field
) {
2712 ret
= bt_field_integer_unsigned_get_value(stream_instance_id_field
,
2713 &stream_instance_id
);
2715 stream_instance_id
= -1ULL;
2720 bt_put(stream_instance_id_field
);
2721 return stream_instance_id
;
2725 int set_stream(struct bt_notif_iter
*notit
)
2728 struct bt_stream
*stream
= NULL
;
2730 BT_LOGV("Calling user function (get stream): notit-addr=%p, "
2731 "stream-class-addr=%p, stream-class-name=\"%s\", "
2732 "stream-class-id=%" PRId64
,
2733 notit
, notit
->meta
.stream_class
,
2734 bt_stream_class_get_name(notit
->meta
.stream_class
),
2735 bt_stream_class_get_id(notit
->meta
.stream_class
));
2736 stream
= bt_get(notit
->medium
.medops
.get_stream(
2737 notit
->meta
.stream_class
, get_cur_stream_instance_id(notit
),
2738 notit
->medium
.data
));
2739 BT_LOGV("User function returned: stream-addr=%p", stream
);
2741 BT_LOGW_STR("User function failed to return a stream object for the given stream class.");
2746 if (notit
->stream
&& stream
!= notit
->stream
) {
2747 BT_LOGW("User function returned a different stream than the previous one for the same sequence of packets.");
2752 BT_MOVE(notit
->stream
, stream
);
2760 void create_packet(struct bt_notif_iter
*notit
)
2763 struct bt_packet
*packet
= NULL
;
2765 BT_LOGV("Creating packet for packet notification: "
2766 "notit-addr=%p", notit
);
2767 BT_LOGV("Creating packet from stream: "
2768 "notit-addr=%p, stream-addr=%p, "
2769 "stream-class-addr=%p, "
2770 "stream-class-name=\"%s\", "
2771 "stream-class-id=%" PRId64
,
2772 notit
, notit
->stream
, notit
->meta
.stream_class
,
2773 bt_stream_class_get_name(notit
->meta
.stream_class
),
2774 bt_stream_class_get_id(notit
->meta
.stream_class
));
2777 BT_ASSERT(notit
->stream
);
2778 packet
= bt_packet_create(notit
->stream
);
2780 BT_LOGE("Cannot create packet from stream: "
2781 "notit-addr=%p, stream-addr=%p, "
2782 "stream-class-addr=%p, "
2783 "stream-class-name=\"%s\", "
2784 "stream-class-id=%" PRId64
,
2785 notit
, notit
->stream
, notit
->meta
.stream_class
,
2786 bt_stream_class_get_name(notit
->meta
.stream_class
),
2787 bt_stream_class_get_id(notit
->meta
.stream_class
));
2791 /* Set packet's context and header fields */
2792 if (notit
->dscopes
.trace_packet_header
) {
2793 ret
= bt_packet_set_header(packet
,
2794 notit
->dscopes
.trace_packet_header
);
2796 BT_LOGE("Cannot set packet's header field: "
2797 "notit-addr=%p, packet-addr=%p, "
2799 "stream-class-addr=%p, "
2800 "stream-class-name=\"%s\", "
2801 "stream-class-id=%" PRId64
", "
2803 notit
, packet
, notit
->stream
, notit
->meta
.stream_class
,
2804 bt_stream_class_get_name(notit
->meta
.stream_class
),
2805 bt_stream_class_get_id(notit
->meta
.stream_class
),
2806 notit
->dscopes
.trace_packet_header
);
2811 if (notit
->dscopes
.stream_packet_context
) {
2812 ret
= bt_packet_set_context(packet
,
2813 notit
->dscopes
.stream_packet_context
);
2815 BT_LOGE("Cannot set packet's context field: "
2816 "notit-addr=%p, packet-addr=%p, "
2818 "stream-class-addr=%p, "
2819 "stream-class-name=\"%s\", "
2820 "stream-class-id=%" PRId64
", "
2822 notit
, packet
, notit
->stream
, notit
->meta
.stream_class
,
2823 bt_stream_class_get_name(notit
->meta
.stream_class
),
2824 bt_stream_class_get_id(notit
->meta
.stream_class
),
2825 notit
->dscopes
.trace_packet_header
);
2836 BT_MOVE(notit
->packet
, packet
);
2840 void notify_new_stream(struct bt_notif_iter
*notit
,
2841 struct bt_notification
**notification
)
2843 struct bt_notification
*ret
= NULL
;
2846 /* Ask the user for the stream */
2847 iret
= set_stream(notit
);
2852 BT_ASSERT(notit
->stream
);
2853 ret
= bt_notification_stream_begin_create(notit
->stream
);
2855 BT_LOGE("Cannot create stream beginning notification: "
2856 "notit-addr=%p, stream-addr=%p",
2857 notit
, notit
->stream
);
2862 *notification
= ret
;
2866 void notify_end_of_stream(struct bt_notif_iter
*notit
,
2867 struct bt_notification
**notification
)
2869 struct bt_notification
*ret
;
2871 if (!notit
->stream
) {
2872 BT_LOGE("Cannot create stream for stream notification: "
2873 "notit-addr=%p", notit
);
2877 ret
= bt_notification_stream_end_create(notit
->stream
);
2879 BT_LOGE("Cannot create stream beginning notification: "
2880 "notit-addr=%p, stream-addr=%p",
2881 notit
, notit
->stream
);
2884 *notification
= ret
;
2888 void notify_new_packet(struct bt_notif_iter
*notit
,
2889 struct bt_notification
**notification
)
2891 struct bt_notification
*ret
;
2893 /* Initialize the iterator's current packet */
2894 create_packet(notit
);
2895 if (!notit
->packet
) {
2896 BT_LOGE("Cannot create packet for packet notification: "
2897 "notit-addr=%p", notit
);
2901 ret
= bt_notification_packet_begin_create(notit
->packet
);
2903 BT_LOGE("Cannot create packet beginning notification: "
2904 "notit-addr=%p, packet-addr=%p",
2905 notit
, notit
->packet
);
2908 *notification
= ret
;
2912 void notify_end_of_packet(struct bt_notif_iter
*notit
,
2913 struct bt_notification
**notification
)
2915 struct bt_notification
*ret
;
2917 if (!notit
->packet
) {
2921 ret
= bt_notification_packet_end_create(notit
->packet
);
2923 BT_LOGE("Cannot create packet end notification: "
2924 "notit-addr=%p, packet-addr=%p",
2925 notit
, notit
->packet
);
2928 BT_PUT(notit
->packet
);
2929 *notification
= ret
;
2933 void notify_event(struct bt_notif_iter
*notit
,
2934 struct bt_clock_class_priority_map
*cc_prio_map
,
2935 struct bt_notification
**notification
)
2937 struct bt_event
*event
= NULL
;
2938 struct bt_notification
*ret
= NULL
;
2940 /* Make sure that the event contains at least one bit of data */
2941 if (notit
->buf
.at
== notit
->buf
.last_eh_at
) {
2942 BT_LOGE("Cannot create empty event with 0 bits of data: "
2943 "notit-addr=%p, packet-cur=%zu",
2944 notit
, packet_at(notit
));
2949 event
= create_event(notit
);
2951 BT_LOGE("Cannot create event for event notification: "
2952 "notit-addr=%p", notit
);
2956 ret
= bt_notification_event_create(event
, cc_prio_map
);
2958 BT_LOGE("Cannot create event notification: "
2959 "notit-addr=%p, event-addr=%p, "
2960 "cc-prio-map-addr=%p",
2961 notit
, event
, cc_prio_map
);
2964 *notification
= ret
;
2970 void init_trace_field_path_cache(struct bt_trace
*trace
,
2971 struct trace_field_path_cache
*trace_field_path_cache
)
2974 int stream_instance_id
= -1;
2976 struct bt_field_type
*packet_header
= NULL
;
2978 packet_header
= bt_trace_borrow_packet_header_field_type(trace
);
2979 if (!packet_header
) {
2983 if (!bt_field_type_is_structure(packet_header
)) {
2987 count
= bt_field_type_structure_get_field_count(packet_header
);
2988 BT_ASSERT(count
>= 0);
2990 for (i
= 0; (i
< count
&& (stream_id
== -1 || stream_instance_id
== -1)); i
++) {
2992 const char *field_name
;
2994 ret
= bt_field_type_structure_borrow_field_by_index(
2995 packet_header
, &field_name
, NULL
, i
);
2997 BT_LOGE("Cannot get structure field's field: "
2998 "field-addr=%p, index=%d",
3003 if (stream_id
== -1 && !strcmp(field_name
, "stream_id")) {
3005 } else if (stream_instance_id
== -1 &&
3006 !strcmp(field_name
, "stream_instance_id")) {
3007 stream_instance_id
= i
;
3012 trace_field_path_cache
->stream_id
= stream_id
;
3013 trace_field_path_cache
->stream_instance_id
= stream_instance_id
;
3017 struct bt_notif_iter
*bt_notif_iter_create(struct bt_trace
*trace
,
3018 size_t max_request_sz
,
3019 struct bt_notif_iter_medium_ops medops
, void *data
)
3021 struct bt_notif_iter
*notit
= NULL
;
3022 struct bt_btr_cbs cbs
= {
3024 .signed_int
= btr_signed_int_cb
,
3025 .unsigned_int
= btr_unsigned_int_cb
,
3026 .floating_point
= btr_floating_point_cb
,
3027 .string_begin
= btr_string_begin_cb
,
3028 .string
= btr_string_cb
,
3029 .string_end
= btr_string_end_cb
,
3030 .compound_begin
= btr_compound_begin_cb
,
3031 .compound_end
= btr_compound_end_cb
,
3034 .get_sequence_length
= btr_get_sequence_length_cb
,
3035 .borrow_variant_field_type
= btr_borrow_variant_field_type_cb
,
3040 BT_ASSERT(medops
.request_bytes
);
3041 BT_ASSERT(medops
.get_stream
);
3042 BT_LOGD("Creating CTF plugin notification iterator: "
3043 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
3045 trace
, bt_trace_get_name(trace
), max_request_sz
, data
);
3046 notit
= g_new0(struct bt_notif_iter
, 1);
3048 BT_LOGE_STR("Failed to allocate one CTF plugin notification iterator.");
3051 notit
->clock_states
= g_hash_table_new_full(g_direct_hash
,
3052 g_direct_equal
, NULL
, g_free
);
3053 if (!notit
->clock_states
) {
3054 BT_LOGE_STR("Failed to allocate a GHashTable.");
3057 notit
->meta
.trace
= trace
;
3058 notit
->medium
.medops
= medops
;
3059 notit
->medium
.max_request_sz
= max_request_sz
;
3060 notit
->medium
.data
= data
;
3061 notit
->stack
= stack_new(notit
);
3062 if (!notit
->stack
) {
3063 BT_LOGE_STR("Failed to create field stack.");
3067 notit
->btr
= bt_btr_create(cbs
, notit
);
3069 BT_LOGE_STR("Failed to create binary type reader (BTR).");
3073 bt_notif_iter_reset(notit
);
3074 init_trace_field_path_cache(trace
, ¬it
->trace_field_path_cache
);
3075 notit
->sc_field_path_caches
= g_hash_table_new_full(g_direct_hash
,
3076 g_direct_equal
, NULL
, g_free
);
3077 if (!notit
->sc_field_path_caches
) {
3078 BT_LOGE_STR("Failed to allocate a GHashTable.");
3082 notit
->field_overrides
= g_hash_table_new_full(g_direct_hash
,
3083 g_direct_equal
, NULL
, g_free
);
3084 if (!notit
->field_overrides
) {
3085 BT_LOGE_STR("Failed to allocate a GHashTable.");
3089 BT_LOGD("Created CTF plugin notification iterator: "
3090 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
3091 "data=%p, notit-addr=%p",
3092 trace
, bt_trace_get_name(trace
), max_request_sz
, data
,
3094 notit
->cur_packet_offset
= 0;
3100 bt_notif_iter_destroy(notit
);
3105 void bt_notif_iter_destroy(struct bt_notif_iter
*notit
)
3107 BT_PUT(notit
->packet
);
3108 BT_PUT(notit
->stream
);
3109 BT_PUT(notit
->cur_timestamp_end
);
3110 put_all_dscopes(notit
);
3112 BT_LOGD("Destroying CTF plugin notification iterator: addr=%p", notit
);
3115 BT_LOGD_STR("Destroying field stack.");
3116 stack_destroy(notit
->stack
);
3120 BT_LOGD("Destroying BTR: btr-addr=%p", notit
->btr
);
3121 bt_btr_destroy(notit
->btr
);
3124 if (notit
->clock_states
) {
3125 g_hash_table_destroy(notit
->clock_states
);
3128 if (notit
->sc_field_path_caches
) {
3129 g_hash_table_destroy(notit
->sc_field_path_caches
);
3132 if (notit
->field_overrides
) {
3133 g_hash_table_destroy(notit
->field_overrides
);
3139 enum bt_notif_iter_status
bt_notif_iter_get_next_notification(
3140 struct bt_notif_iter
*notit
,
3141 struct bt_clock_class_priority_map
*cc_prio_map
,
3142 struct bt_notification
**notification
)
3144 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
3147 BT_ASSERT(notification
);
3149 if (notit
->state
== STATE_DONE
) {
3150 status
= BT_NOTIF_ITER_STATUS_EOF
;
3154 BT_LOGV("Getting next notification: notit-addr=%p, cc-prio-map-addr=%p",
3155 notit
, cc_prio_map
);
3158 status
= handle_state(notit
);
3159 if (status
== BT_NOTIF_ITER_STATUS_AGAIN
) {
3160 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_AGAIN.");
3163 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
3164 if (status
== BT_NOTIF_ITER_STATUS_EOF
) {
3165 enum state next_state
= notit
->state
;
3167 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_EOF.");
3169 if (notit
->packet
) {
3170 notify_end_of_packet(notit
, notification
);
3172 notify_end_of_stream(notit
, notification
);
3173 next_state
= STATE_DONE
;
3176 if (!*notification
) {
3177 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3181 status
= BT_NOTIF_ITER_STATUS_OK
;
3182 notit
->state
= next_state
;
3185 BT_LOGW("Cannot handle state: "
3186 "notit-addr=%p, state=%s",
3187 notit
, state_string(notit
->state
));
3192 switch (notit
->state
) {
3193 case STATE_EMIT_NOTIF_NEW_STREAM
:
3194 /* notify_new_stream() logs errors */
3195 notify_new_stream(notit
, notification
);
3196 if (!*notification
) {
3197 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3199 notit
->stream_begin_emitted
= true;
3201 case STATE_EMIT_NOTIF_NEW_PACKET
:
3202 /* notify_new_packet() logs errors */
3203 notify_new_packet(notit
, notification
);
3204 if (!*notification
) {
3205 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3208 case STATE_EMIT_NOTIF_EVENT
:
3209 /* notify_event() logs errors */
3210 notify_event(notit
, cc_prio_map
, notification
);
3211 if (!*notification
) {
3212 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3215 case STATE_EMIT_NOTIF_END_OF_PACKET
:
3216 /* Update clock with timestamp_end field. */
3217 if (notit
->cur_timestamp_end
) {
3218 enum bt_btr_status btr_status
;
3219 struct bt_field_type
*field_type
=
3220 bt_field_borrow_type(
3221 notit
->cur_timestamp_end
);
3223 BT_ASSERT(field_type
);
3224 btr_status
= update_clock(notit
,
3225 notit
->cur_timestamp_end
);
3226 if (btr_status
!= BT_BTR_STATUS_OK
) {
3227 BT_LOGW("Cannot update stream's clock value: "
3228 "notit-addr=%p", notit
);
3229 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3234 /* notify_end_of_packet() logs errors */
3235 notify_end_of_packet(notit
, notification
);
3236 if (!*notification
) {
3237 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3241 /* Non-emitting state: continue */
3251 enum bt_notif_iter_status
bt_notif_iter_get_packet_header_context_fields(
3252 struct bt_notif_iter
*notit
,
3253 struct bt_field
**packet_header_field
,
3254 struct bt_field
**packet_context_field
)
3257 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
3261 if (notit
->state
== STATE_EMIT_NOTIF_NEW_PACKET
) {
3262 /* We're already there */
3267 status
= handle_state(notit
);
3268 if (status
== BT_NOTIF_ITER_STATUS_AGAIN
) {
3269 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_AGAIN.");
3272 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
3273 if (status
== BT_NOTIF_ITER_STATUS_EOF
) {
3274 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_EOF.");
3276 BT_LOGW("Cannot handle state: "
3277 "notit-addr=%p, state=%s",
3278 notit
, state_string(notit
->state
));
3283 switch (notit
->state
) {
3284 case STATE_EMIT_NOTIF_NEW_PACKET
:
3286 * Packet header and context fields are
3287 * potentially decoded (or they don't exist).
3291 case STATE_EMIT_NOTIF_NEW_STREAM
:
3292 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
3293 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
3294 case STATE_AFTER_TRACE_PACKET_HEADER
:
3295 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
3296 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
3297 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
3298 /* Non-emitting state: continue */
3302 * We should never get past the
3303 * STATE_EMIT_NOTIF_NEW_PACKET state.
3305 BT_LOGF("Unexpected state: notit-addr=%p, state=%s",
3306 notit
, state_string(notit
->state
));
3312 if (packet_header_field
) {
3313 *packet_header_field
= bt_get(notit
->dscopes
.trace_packet_header
);
3316 if (packet_context_field
) {
3317 *packet_context_field
= bt_get(notit
->dscopes
.stream_packet_context
);
3320 ret
= set_current_packet_content_sizes(notit
);
3322 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3330 void bt_notif_iter_set_medops_data(struct bt_notif_iter
*notit
,
3334 notit
->medium
.data
= medops_data
;
3338 enum bt_notif_iter_status
bt_notif_iter_seek(
3339 struct bt_notif_iter
*notit
, off_t offset
)
3341 enum bt_notif_iter_status ret
= BT_NOTIF_ITER_STATUS_OK
;
3342 enum bt_notif_iter_medium_status medium_status
;
3346 BT_LOGE("Cannot seek to negative offset: offset=%jd", offset
);
3347 ret
= BT_NOTIF_ITER_STATUS_INVAL
;
3351 if (!notit
->medium
.medops
.seek
) {
3352 ret
= BT_NOTIF_ITER_STATUS_UNSUPPORTED
;
3353 BT_LOGD("Aborting seek as the iterator's underlying media does not implement seek support.");
3357 medium_status
= notit
->medium
.medops
.seek(
3358 BT_NOTIF_ITER_SEEK_WHENCE_SET
, offset
,
3359 notit
->medium
.data
);
3360 if (medium_status
!= BT_NOTIF_ITER_MEDIUM_STATUS_OK
) {
3361 if (medium_status
== BT_NOTIF_ITER_MEDIUM_STATUS_EOF
) {
3362 ret
= BT_NOTIF_ITER_STATUS_EOF
;
3364 ret
= BT_NOTIF_ITER_STATUS_ERROR
;
3369 bt_notif_iter_reset(notit
);
3370 notit
->cur_packet_offset
= offset
;
3376 off_t
bt_notif_iter_get_current_packet_offset(
3377 struct bt_notif_iter
*notit
)
3380 return notit
->cur_packet_offset
;
3384 off_t
bt_notif_iter_get_current_packet_size(
3385 struct bt_notif_iter
*notit
)
3388 return notit
->cur_packet_size
;