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"
36 #include <babeltrace/babeltrace.h>
37 #include <babeltrace/ctf-ir/field-types-internal.h>
38 #include <babeltrace/ctf-ir/field-path-internal.h>
42 #include "notif-iter.h"
43 #include "../btr/btr.h"
47 /* A visit stack entry */
50 * Current base field, one of:
58 * Field is owned by this.
60 struct bt_field
*base
;
62 /* index of next field to set */
68 /* Entries (struct stack_entry *) (top is last element) */
75 STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
,
76 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
77 STATE_AFTER_TRACE_PACKET_HEADER
,
78 STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
,
79 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
80 STATE_AFTER_STREAM_PACKET_CONTEXT
,
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
,
93 STATE_SKIP_PACKET_PADDING
,
96 struct trace_field_path_cache
{
98 * Indexes of the stream_id and stream_instance_id field in the packet
99 * header structure, -1 if unset.
102 int stream_instance_id
;
105 struct stream_class_field_path_cache
{
107 * Indexes of the v and id fields in the stream event header structure,
114 * index of the timestamp_end, packet_size and content_size fields in
115 * the stream packet context structure. Set to -1 if the fields were
123 struct field_cb_override
{
124 enum bt_btr_status (* func
)(void *value
,
125 struct bt_field_type
*type
, void *data
);
129 /* CTF notification iterator */
130 struct bt_notif_iter
{
135 * Current dynamic scope field pointer.
137 * This is set when a dynamic scope field is first created by
138 * btr_compound_begin_cb(). It points to one of the fields in
141 struct bt_field
**cur_dscope_field
;
143 /* Trace and classes (owned by this) */
145 struct bt_trace
*trace
;
146 struct bt_stream_class
*stream_class
;
147 struct bt_event_class
*event_class
;
150 /* Current packet (NULL if not created yet) */
151 struct bt_packet
*packet
;
153 /* Current stream (NULL if not set yet) */
154 struct bt_stream
*stream
;
157 * Current timestamp_end field (to consider before switching packets).
159 struct bt_field
*cur_timestamp_end
;
161 /* Database of current dynamic scopes (owned by this) */
163 struct bt_field
*trace_packet_header
;
164 struct bt_field
*stream_packet_context
;
165 struct bt_field
*stream_event_header
;
166 struct bt_field
*stream_event_context
;
167 struct bt_field
*event_context
;
168 struct bt_field
*event_payload
;
172 * Special field overrides.
174 * Overrides are used to implement the behaviours of special fields such
175 * as "timestamp_end" (which must be ignored until the end of the
176 * packet), "id" (event id) which can be present multiple times and must
177 * be updated multiple time.
179 * This should be used to implement the behaviour of integer fields
180 * mapped to clocks and other "tagged" fields (in CTF 2).
182 * bt_field_type to struct field_cb_override
184 GHashTable
*field_overrides
;
189 /* Current medium buffer data */
191 /* Last address provided by medium */
194 /* Buffer size provided by medium (bytes) */
197 /* Offset within whole packet of addr (bits) */
198 size_t packet_offset
;
200 /* Current position from addr (bits) */
203 /* Position of the last event header from addr (bits) */
207 /* Binary type reader */
210 /* Current medium data */
212 struct bt_notif_iter_medium_ops medops
;
213 size_t max_request_sz
;
217 /* Current packet size (bits) (-1 if unknown) */
218 int64_t cur_packet_size
;
220 /* Current content size (bits) (-1 if unknown) */
221 int64_t cur_content_size
;
224 * Offset, in the underlying media, of the current packet's start
227 off_t cur_packet_offset
;
229 /* bt_clock_class to uint64_t. */
230 GHashTable
*clock_states
;
233 * Cache of the trace-constant field paths (event header type)
234 * associated to the current trace.
236 struct trace_field_path_cache trace_field_path_cache
;
239 * Field path cache associated with the current stream class.
240 * Ownership of this structure belongs to the field_path_caches HT.
242 struct stream_class_field_path_cache
*cur_sc_field_path_cache
;
244 /* bt_stream_class to struct stream_class_field_path_cache. */
245 GHashTable
*sc_field_path_caches
;
249 const char *state_string(enum state state
)
254 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
255 return "STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN";
256 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
257 return "STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE";
258 case STATE_AFTER_TRACE_PACKET_HEADER
:
259 return "STATE_AFTER_TRACE_PACKET_HEADER";
260 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
261 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN";
262 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
263 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE";
264 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
265 return "STATE_AFTER_STREAM_PACKET_CONTEXT";
266 case STATE_EMIT_NOTIF_NEW_PACKET
:
267 return "STATE_EMIT_NOTIF_NEW_PACKET";
268 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
:
269 return "STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN";
270 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
:
271 return "STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE";
272 case STATE_AFTER_STREAM_EVENT_HEADER
:
273 return "STATE_AFTER_STREAM_EVENT_HEADER";
274 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
:
275 return "STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN";
276 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
:
277 return "STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE";
278 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN
:
279 return "STATE_DSCOPE_EVENT_CONTEXT_BEGIN";
280 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
:
281 return "STATE_DSCOPE_EVENT_CONTEXT_CONTINUE";
282 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
283 return "STATE_DSCOPE_EVENT_PAYLOAD_BEGIN";
284 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
285 return "STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE";
286 case STATE_EMIT_NOTIF_EVENT
:
287 return "STATE_EMIT_NOTIF_EVENT";
288 case STATE_EMIT_NOTIF_END_OF_PACKET
:
289 return "STATE_EMIT_NOTIF_END_OF_PACKET";
290 case STATE_SKIP_PACKET_PADDING
:
291 return "STATE_SKIP_PACKET_PADDING";
298 int bt_notif_iter_switch_packet(struct bt_notif_iter
*notit
);
301 enum bt_btr_status
btr_timestamp_end_cb(void *value
,
302 struct bt_field_type
*type
, void *data
);
305 void stack_entry_free_func(gpointer data
)
307 struct stack_entry
*entry
= data
;
314 struct stack
*stack_new(struct bt_notif_iter
*notit
)
316 struct stack
*stack
= NULL
;
318 stack
= g_new0(struct stack
, 1);
320 BT_LOGE_STR("Failed to allocate one stack.");
324 stack
->entries
= g_ptr_array_new_with_free_func(stack_entry_free_func
);
325 if (!stack
->entries
) {
326 BT_LOGE_STR("Failed to allocate a GPtrArray.");
330 BT_LOGD("Created stack: notit-addr=%p, stack-addr=%p", notit
, stack
);
339 void stack_destroy(struct stack
*stack
)
342 BT_LOGD("Destroying stack: addr=%p", stack
);
343 g_ptr_array_free(stack
->entries
, TRUE
);
348 int stack_push(struct stack
*stack
, struct bt_field
*base
)
351 struct stack_entry
*entry
;
355 BT_LOGV("Pushing base field on stack: stack-addr=%p, "
356 "stack-size-before=%u, stack-size-after=%u",
357 stack
, stack
->entries
->len
, stack
->entries
->len
+ 1);
358 entry
= g_new0(struct stack_entry
, 1);
360 BT_LOGE_STR("Failed to allocate one stack entry.");
365 entry
->base
= bt_get(base
);
366 g_ptr_array_add(stack
->entries
, entry
);
373 unsigned int stack_size(struct stack
*stack
)
377 return stack
->entries
->len
;
381 void stack_pop(struct stack
*stack
)
384 assert(stack_size(stack
));
385 BT_LOGV("Popping from stack: "
386 "stack-addr=%p, stack-size-before=%u, stack-size-after=%u",
387 stack
, stack
->entries
->len
, stack
->entries
->len
- 1);
388 g_ptr_array_remove_index(stack
->entries
, stack
->entries
->len
- 1);
392 struct stack_entry
*stack_top(struct stack
*stack
)
395 assert(stack_size(stack
));
397 return g_ptr_array_index(stack
->entries
, stack
->entries
->len
- 1);
401 bool stack_empty(struct stack
*stack
)
403 return stack_size(stack
) == 0;
407 void stack_clear(struct stack
*stack
)
411 if (!stack_empty(stack
)) {
412 BT_LOGV("Clearing stack: stack-addr=%p, stack-size=%u",
413 stack
, stack
->entries
->len
);
414 g_ptr_array_remove_range(stack
->entries
, 0, stack_size(stack
));
417 assert(stack_empty(stack
));
421 enum bt_notif_iter_status
notif_iter_status_from_m_status(
422 enum bt_notif_iter_medium_status m_status
)
424 return (int) m_status
;
428 size_t buf_size_bits(struct bt_notif_iter
*notit
)
430 return notit
->buf
.sz
* 8;
434 size_t buf_available_bits(struct bt_notif_iter
*notit
)
436 return buf_size_bits(notit
) - notit
->buf
.at
;
440 size_t packet_at(struct bt_notif_iter
*notit
)
442 return notit
->buf
.packet_offset
+ notit
->buf
.at
;
446 void buf_consume_bits(struct bt_notif_iter
*notit
, size_t incr
)
448 BT_LOGV("Advancing cursor: notit-addr=%p, cur-before=%zu, cur-after=%zu",
449 notit
, notit
->buf
.at
, notit
->buf
.at
+ incr
);
450 notit
->buf
.at
+= incr
;
454 enum bt_notif_iter_status
request_medium_bytes(
455 struct bt_notif_iter
*notit
)
457 uint8_t *buffer_addr
= NULL
;
458 size_t buffer_sz
= 0;
459 enum bt_notif_iter_medium_status m_status
;
461 BT_LOGV("Calling user function (request bytes): notit-addr=%p, "
462 "request-size=%zu", notit
, notit
->medium
.max_request_sz
);
463 m_status
= notit
->medium
.medops
.request_bytes(
464 notit
->medium
.max_request_sz
, &buffer_addr
,
465 &buffer_sz
, notit
->medium
.data
);
466 BT_LOGV("User function returned: status=%s, buf-addr=%p, buf-size=%zu",
467 bt_notif_iter_medium_status_string(m_status
),
468 buffer_addr
, buffer_sz
);
469 if (m_status
== BT_NOTIF_ITER_MEDIUM_STATUS_OK
) {
470 assert(buffer_sz
!= 0);
472 /* New packet offset is old one + old size (in bits) */
473 notit
->buf
.packet_offset
+= buf_size_bits(notit
);
475 /* Restart at the beginning of the new medium buffer */
477 notit
->buf
.last_eh_at
= SIZE_MAX
;
479 /* New medium buffer size */
480 notit
->buf
.sz
= buffer_sz
;
482 /* New medium buffer address */
483 notit
->buf
.addr
= buffer_addr
;
485 BT_LOGV("User function returned new bytes: "
486 "packet-offset=%zu, cur=%zu, size=%zu, addr=%p",
487 notit
->buf
.packet_offset
, notit
->buf
.at
,
488 notit
->buf
.sz
, notit
->buf
.addr
);
489 BT_LOGV_MEM(buffer_addr
, buffer_sz
, "Returned bytes at %p:",
491 } else if (m_status
== BT_NOTIF_ITER_MEDIUM_STATUS_EOF
) {
493 * User returned end of stream: validate that we're not
494 * in the middle of a packet header, packet context, or
497 if (notit
->cur_packet_size
>= 0) {
498 if (packet_at(notit
) == notit
->cur_packet_size
) {
502 if (packet_at(notit
) == 0) {
506 if (notit
->buf
.last_eh_at
!= SIZE_MAX
&&
507 notit
->buf
.at
== notit
->buf
.last_eh_at
) {
512 /* All other states are invalid */
513 BT_LOGW("User function returned %s, but notification iterator is in an unexpected state: "
514 "state=%s, cur-packet-size=%" PRId64
", cur=%zu, "
515 "packet-cur=%zu, last-eh-at=%zu",
516 bt_notif_iter_medium_status_string(m_status
),
517 state_string(notit
->state
),
518 notit
->cur_packet_size
,
519 notit
->buf
.at
, packet_at(notit
),
520 notit
->buf
.last_eh_at
);
521 m_status
= BT_NOTIF_ITER_MEDIUM_STATUS_ERROR
;
522 } else if (m_status
< 0) {
523 BT_LOGW("User function failed: status=%s",
524 bt_notif_iter_medium_status_string(m_status
));
528 return notif_iter_status_from_m_status(m_status
);
532 enum bt_notif_iter_status
buf_ensure_available_bits(
533 struct bt_notif_iter
*notit
)
535 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
537 if (buf_available_bits(notit
) == 0) {
539 * This _cannot_ return BT_NOTIF_ITER_STATUS_OK
542 status
= request_medium_bytes(notit
);
549 enum bt_notif_iter_status
read_dscope_begin_state(
550 struct bt_notif_iter
*notit
,
551 struct bt_field_type
*dscope_field_type
,
552 enum state done_state
, enum state continue_state
,
553 struct bt_field
**dscope_field
)
555 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
556 enum bt_btr_status btr_status
;
557 size_t consumed_bits
;
559 bt_put(*dscope_field
);
560 notit
->cur_dscope_field
= dscope_field
;
561 BT_LOGV("Starting BTR: notit-addr=%p, btr-addr=%p, ft-addr=%p",
562 notit
, notit
->btr
, dscope_field_type
);
563 consumed_bits
= bt_btr_start(notit
->btr
, dscope_field_type
,
564 notit
->buf
.addr
, notit
->buf
.at
, packet_at(notit
),
565 notit
->buf
.sz
, &btr_status
);
566 BT_LOGV("BTR consumed bits: size=%zu", consumed_bits
);
568 switch (btr_status
) {
569 case BT_BTR_STATUS_OK
:
570 /* type was read completely */
571 BT_LOGV_STR("Field was completely decoded.");
572 notit
->state
= done_state
;
574 case BT_BTR_STATUS_EOF
:
575 BT_LOGV_STR("BTR needs more data to decode field completely.");
576 notit
->state
= continue_state
;
579 BT_LOGW("BTR failed to start: notit-addr=%p, btr-addr=%p, "
580 "status=%s", notit
, notit
->btr
,
581 bt_btr_status_string(btr_status
));
582 status
= BT_NOTIF_ITER_STATUS_ERROR
;
586 /* Consume bits now since we know we're not in an error state */
587 buf_consume_bits(notit
, consumed_bits
);
594 enum bt_notif_iter_status
read_dscope_continue_state(
595 struct bt_notif_iter
*notit
, enum state done_state
)
597 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
598 enum bt_btr_status btr_status
;
599 size_t consumed_bits
;
601 BT_LOGV("Continuing BTR: notit-addr=%p, btr-addr=%p",
604 status
= buf_ensure_available_bits(notit
);
605 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
607 BT_LOGW("Cannot ensure that buffer has at least one byte: "
608 "notif-addr=%p, status=%s",
609 notit
, bt_notif_iter_status_string(status
));
611 BT_LOGV("Cannot ensure that buffer has at least one byte: "
612 "notif-addr=%p, status=%s",
613 notit
, bt_notif_iter_status_string(status
));
620 consumed_bits
= bt_btr_continue(notit
->btr
, notit
->buf
.addr
,
621 notit
->buf
.sz
, &btr_status
);
622 BT_LOGV("BTR consumed bits: size=%zu", consumed_bits
);
624 switch (btr_status
) {
625 case BT_BTR_STATUS_OK
:
626 /* Type was read completely. */
627 BT_LOGV_STR("Field was completely decoded.");
628 notit
->state
= done_state
;
630 case BT_BTR_STATUS_EOF
:
631 /* Stay in this continue state. */
632 BT_LOGV_STR("BTR needs more data to decode field completely.");
635 BT_LOGW("BTR failed to continue: notit-addr=%p, btr-addr=%p, "
636 "status=%s", notit
, notit
->btr
,
637 bt_btr_status_string(btr_status
));
638 status
= BT_NOTIF_ITER_STATUS_ERROR
;
642 /* Consume bits now since we know we're not in an error state. */
643 buf_consume_bits(notit
, consumed_bits
);
649 void put_event_dscopes(struct bt_notif_iter
*notit
)
651 BT_LOGV_STR("Putting event header field.");
652 BT_PUT(notit
->dscopes
.stream_event_header
);
653 BT_LOGV_STR("Putting stream event context field.");
654 BT_PUT(notit
->dscopes
.stream_event_context
);
655 BT_LOGV_STR("Putting event context field.");
656 BT_PUT(notit
->dscopes
.event_context
);
657 BT_LOGV_STR("Putting event payload field.");
658 BT_PUT(notit
->dscopes
.event_payload
);
662 void put_all_dscopes(struct bt_notif_iter
*notit
)
664 BT_LOGV_STR("Putting packet header field.");
665 BT_PUT(notit
->dscopes
.trace_packet_header
);
666 BT_LOGV_STR("Putting packet context field.");
667 BT_PUT(notit
->dscopes
.stream_packet_context
);
668 put_event_dscopes(notit
);
672 enum bt_notif_iter_status
read_packet_header_begin_state(
673 struct bt_notif_iter
*notit
)
675 struct bt_field_type
*packet_header_type
= NULL
;
676 enum bt_notif_iter_status ret
= BT_NOTIF_ITER_STATUS_OK
;
678 if (bt_notif_iter_switch_packet(notit
)) {
679 BT_LOGW("Cannot switch packet: notit-addr=%p", notit
);
680 ret
= BT_NOTIF_ITER_STATUS_ERROR
;
684 /* Packet header type is common to the whole trace. */
685 packet_header_type
= bt_trace_get_packet_header_type(
687 if (!packet_header_type
) {
688 notit
->state
= STATE_AFTER_TRACE_PACKET_HEADER
;
692 BT_LOGV("Decoding packet header field:"
693 "notit-addr=%p, trace-addr=%p, trace-name=\"%s\", ft-addr=%p",
694 notit
, notit
->meta
.trace
,
695 bt_trace_get_name(notit
->meta
.trace
), packet_header_type
);
696 ret
= read_dscope_begin_state(notit
, packet_header_type
,
697 STATE_AFTER_TRACE_PACKET_HEADER
,
698 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
699 ¬it
->dscopes
.trace_packet_header
);
701 BT_LOGW("Cannot decode packet header field: "
702 "notit-addr=%p, trace-addr=%p, "
703 "trace-name=\"%s\", ft-addr=%p",
704 notit
, notit
->meta
.trace
,
705 bt_trace_get_name(notit
->meta
.trace
),
709 BT_PUT(packet_header_type
);
714 enum bt_notif_iter_status
read_packet_header_continue_state(
715 struct bt_notif_iter
*notit
)
717 return read_dscope_continue_state(notit
,
718 STATE_AFTER_TRACE_PACKET_HEADER
);
722 bool is_struct_type(struct bt_field_type
*field_type
)
724 return bt_field_type_get_type_id(field_type
) ==
725 BT_FIELD_TYPE_ID_STRUCT
;
729 bool is_variant_type(struct bt_field_type
*field_type
)
731 return bt_field_type_get_type_id(field_type
) ==
732 BT_FIELD_TYPE_ID_VARIANT
;
736 struct stream_class_field_path_cache
*
737 create_stream_class_field_path_cache_entry(
738 struct bt_notif_iter
*notit
,
739 struct bt_stream_class
*stream_class
)
743 int timestamp_end
= -1;
744 int packet_size
= -1;
745 int content_size
= -1;
746 struct stream_class_field_path_cache
*cache_entry
= g_new0(
747 struct stream_class_field_path_cache
, 1);
748 struct bt_field_type
*event_header
= NULL
, *packet_context
= NULL
;
751 BT_LOGE_STR("Failed to allocate one stream class field path cache.");
755 event_header
= bt_stream_class_get_event_header_type(stream_class
);
756 if (event_header
&& bt_field_type_is_structure(event_header
)) {
759 count
= bt_field_type_structure_get_field_count(
763 for (i
= 0; i
< count
; i
++) {
767 ret
= bt_field_type_structure_get_field_by_index(
768 event_header
, &name
, NULL
, i
);
770 BT_LOGE("Cannot get event header structure field type's field: "
771 "notit-addr=%p, stream-class-addr=%p, "
772 "stream-class-name=\"%s\", "
773 "stream-class-id=%" PRId64
", "
774 "ft-addr=%p, index=%d",
776 bt_stream_class_get_name(stream_class
),
777 bt_stream_class_get_id(stream_class
),
782 if (v
!= -1 && id
!= -1) {
786 if (v
== -1 && strcmp(name
, "v") == 0) {
788 } else if (id
== -1 && !strcmp(name
, "id")) {
794 packet_context
= bt_stream_class_get_packet_context_type(
796 if (packet_context
&& bt_field_type_is_structure(packet_context
)) {
799 count
= bt_field_type_structure_get_field_count(
803 for (i
= 0; i
< count
; i
++) {
806 struct bt_field_type
*field_type
;
808 if (timestamp_end
!= -1 && packet_size
!= -1 &&
809 content_size
!= -1) {
813 ret
= bt_field_type_structure_get_field_by_index(
814 packet_context
, &name
, &field_type
, i
);
816 BT_LOGE("Cannot get packet context structure field type's field: "
817 "notit-addr=%p, stream-class-addr=%p, "
818 "stream-class-name=\"%s\", "
819 "stream-class-id=%" PRId64
", "
820 "ft-addr=%p, index=%d",
822 bt_stream_class_get_name(stream_class
),
823 bt_stream_class_get_id(stream_class
),
828 if (timestamp_end
== -1 &&
829 strcmp(name
, "timestamp_end") == 0) {
830 struct field_cb_override
*override
= g_new0(
831 struct field_cb_override
, 1);
838 override
->func
= btr_timestamp_end_cb
;
839 override
->data
= notit
;
840 g_hash_table_insert(notit
->field_overrides
,
841 bt_get(field_type
), override
);
843 } else if (packet_size
== -1 &&
844 !strcmp(name
, "packet_size")) {
846 } else if (content_size
== -1 &&
847 !strcmp(name
, "content_size")) {
855 cache_entry
->id
= id
;
856 cache_entry
->timestamp_end
= timestamp_end
;
857 cache_entry
->packet_size
= packet_size
;
858 cache_entry
->content_size
= content_size
;
860 BT_PUT(event_header
);
861 BT_PUT(packet_context
);
870 struct stream_class_field_path_cache
*get_stream_class_field_path_cache(
871 struct bt_notif_iter
*notit
,
872 struct bt_stream_class
*stream_class
)
874 bool cache_entry_found
;
875 struct stream_class_field_path_cache
*cache_entry
;
877 cache_entry_found
= g_hash_table_lookup_extended(
878 notit
->sc_field_path_caches
,
879 stream_class
, NULL
, (gpointer
) &cache_entry
);
880 if (unlikely(!cache_entry_found
)) {
881 cache_entry
= create_stream_class_field_path_cache_entry(notit
,
883 g_hash_table_insert(notit
->sc_field_path_caches
,
884 bt_get(stream_class
), (gpointer
) cache_entry
);
891 enum bt_notif_iter_status
set_current_stream_class(
892 struct bt_notif_iter
*notit
)
894 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
895 struct bt_field_type
*packet_header_type
= NULL
;
896 struct bt_field_type
*stream_id_field_type
= NULL
;
897 struct bt_stream_class
*new_stream_class
= NULL
;
900 /* Clear the current stream class field path cache. */
901 notit
->cur_sc_field_path_cache
= NULL
;
903 /* Is there any "stream_id" field in the packet header? */
904 packet_header_type
= bt_trace_get_packet_header_type(
906 if (!packet_header_type
) {
908 * No packet header, therefore no `stream_id` field,
909 * therefore only one stream class.
911 goto single_stream_class
;
914 assert(is_struct_type(packet_header_type
));
917 stream_id_field_type
=
918 bt_field_type_structure_get_field_type_by_name(
919 packet_header_type
, "stream_id");
920 if (stream_id_field_type
) {
921 /* Find appropriate stream class using current stream ID */
923 struct bt_field
*stream_id_field
= NULL
;
925 assert(notit
->dscopes
.trace_packet_header
);
928 stream_id_field
= bt_field_structure_get_field_by_name(
929 notit
->dscopes
.trace_packet_header
, "stream_id");
930 assert(stream_id_field
);
931 ret
= bt_field_unsigned_integer_get_value(
932 stream_id_field
, &stream_id
);
934 BT_PUT(stream_id_field
);
937 /* Only one stream: pick the first stream class */
938 assert(bt_trace_get_stream_class_count(
939 notit
->meta
.trace
) == 1);
943 BT_LOGV("Found stream class ID to use: notit-addr=%p, "
944 "stream-class-id=%" PRIu64
", "
945 "trace-addr=%p, trace-name=\"%s\"",
946 notit
, stream_id
, notit
->meta
.trace
,
947 bt_trace_get_name(notit
->meta
.trace
));
949 new_stream_class
= bt_trace_get_stream_class_by_id(
950 notit
->meta
.trace
, stream_id
);
951 if (!new_stream_class
) {
952 BT_LOGW("No stream class with ID of stream class ID to use in trace: "
953 "notit-addr=%p, stream-class-id=%" PRIu64
", "
954 "trace-addr=%p, trace-name=\"%s\"",
955 notit
, stream_id
, notit
->meta
.trace
,
956 bt_trace_get_name(notit
->meta
.trace
));
957 status
= BT_NOTIF_ITER_STATUS_ERROR
;
961 if (notit
->meta
.stream_class
) {
962 if (new_stream_class
!= notit
->meta
.stream_class
) {
963 BT_LOGW("Two packets refer to two different stream classes within the same packet sequence: "
964 "notit-addr=%p, prev-stream-class-addr=%p, "
965 "prev-stream-class-name=\"%s\", "
966 "prev-stream-class-id=%" PRId64
", "
967 "next-stream-class-addr=%p, "
968 "next-stream-class-name=\"%s\", "
969 "next-stream-class-id=%" PRId64
", "
970 "trace-addr=%p, trace-name=\"%s\"",
971 notit
, notit
->meta
.stream_class
,
972 bt_stream_class_get_name(notit
->meta
.stream_class
),
973 bt_stream_class_get_id(notit
->meta
.stream_class
),
975 bt_stream_class_get_name(new_stream_class
),
976 bt_stream_class_get_id(new_stream_class
),
978 bt_trace_get_name(notit
->meta
.trace
));
979 status
= BT_NOTIF_ITER_STATUS_ERROR
;
983 BT_MOVE(notit
->meta
.stream_class
, new_stream_class
);
986 BT_LOGV("Set current stream class: "
987 "notit-addr=%p, stream-class-addr=%p, "
988 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
989 notit
, notit
->meta
.stream_class
,
990 bt_stream_class_get_name(notit
->meta
.stream_class
),
991 bt_stream_class_get_id(notit
->meta
.stream_class
));
994 * Retrieve (or lazily create) the current stream class field path
997 notit
->cur_sc_field_path_cache
= get_stream_class_field_path_cache(
998 notit
, notit
->meta
.stream_class
);
999 if (!notit
->cur_sc_field_path_cache
) {
1000 BT_LOGW("Cannot retrieve stream class field path from cache: "
1001 "notit-addr=%p, stream-class-addr=%p, "
1002 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
1003 notit
, notit
->meta
.stream_class
,
1004 bt_stream_class_get_name(notit
->meta
.stream_class
),
1005 bt_stream_class_get_id(notit
->meta
.stream_class
));
1006 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1010 BT_PUT(packet_header_type
);
1011 BT_PUT(stream_id_field_type
);
1012 bt_put(new_stream_class
);
1017 enum bt_notif_iter_status
after_packet_header_state(
1018 struct bt_notif_iter
*notit
)
1020 enum bt_notif_iter_status status
;
1022 status
= set_current_stream_class(notit
);
1023 if (status
== BT_NOTIF_ITER_STATUS_OK
) {
1024 notit
->state
= STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
;
1031 enum bt_notif_iter_status
read_packet_context_begin_state(
1032 struct bt_notif_iter
*notit
)
1034 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1035 struct bt_field_type
*packet_context_type
;
1037 assert(notit
->meta
.stream_class
);
1038 packet_context_type
= bt_stream_class_get_packet_context_type(
1039 notit
->meta
.stream_class
);
1040 if (!packet_context_type
) {
1041 BT_LOGV("No packet packet context field type in stream class: continuing: "
1042 "notit-addr=%p, stream-class-addr=%p, "
1043 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
1044 notit
, notit
->meta
.stream_class
,
1045 bt_stream_class_get_name(notit
->meta
.stream_class
),
1046 bt_stream_class_get_id(notit
->meta
.stream_class
));
1047 notit
->state
= STATE_AFTER_STREAM_PACKET_CONTEXT
;
1051 BT_LOGV("Decoding packet context field: "
1052 "notit-addr=%p, stream-class-addr=%p, "
1053 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1055 notit
, notit
->meta
.stream_class
,
1056 bt_stream_class_get_name(notit
->meta
.stream_class
),
1057 bt_stream_class_get_id(notit
->meta
.stream_class
),
1058 packet_context_type
);
1059 status
= read_dscope_begin_state(notit
, packet_context_type
,
1060 STATE_AFTER_STREAM_PACKET_CONTEXT
,
1061 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
1062 ¬it
->dscopes
.stream_packet_context
);
1064 BT_LOGW("Cannot decode packet context field: "
1065 "notit-addr=%p, stream-class-addr=%p, "
1066 "stream-class-name=\"%s\", "
1067 "stream-class-id=%" PRId64
", ft-addr=%p",
1068 notit
, notit
->meta
.stream_class
,
1069 bt_stream_class_get_name(notit
->meta
.stream_class
),
1070 bt_stream_class_get_id(notit
->meta
.stream_class
),
1071 packet_context_type
);
1075 BT_PUT(packet_context_type
);
1080 enum bt_notif_iter_status
read_packet_context_continue_state(
1081 struct bt_notif_iter
*notit
)
1083 return read_dscope_continue_state(notit
,
1084 STATE_AFTER_STREAM_PACKET_CONTEXT
);
1088 enum bt_notif_iter_status
set_current_packet_content_sizes(
1089 struct bt_notif_iter
*notit
)
1091 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1092 struct bt_field
*packet_size_field
= NULL
;
1093 struct bt_field
*content_size_field
= NULL
;
1094 uint64_t content_size
= -1ULL, packet_size
= -1ULL;
1096 if (!notit
->dscopes
.stream_packet_context
) {
1100 packet_size_field
= bt_field_structure_get_field_by_name(
1101 notit
->dscopes
.stream_packet_context
, "packet_size");
1102 content_size_field
= bt_field_structure_get_field_by_name(
1103 notit
->dscopes
.stream_packet_context
, "content_size");
1104 if (packet_size_field
) {
1105 int ret
= bt_field_unsigned_integer_get_value(
1106 packet_size_field
, &packet_size
);
1109 if (packet_size
== 0) {
1110 BT_LOGW("Invalid packet size: packet context field indicates packet size is zero: "
1111 "notit-addr=%p, packet-context-field-addr=%p",
1112 notit
, notit
->dscopes
.stream_packet_context
);
1113 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1115 } else if ((packet_size
% 8) != 0) {
1116 BT_LOGW("Invalid packet size: packet context field indicates packet size is not a multiple of 8: "
1117 "notit-addr=%p, packet-context-field-addr=%p, "
1118 "packet-size=%" PRIu64
,
1119 notit
, notit
->dscopes
.stream_packet_context
,
1121 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1126 if (content_size_field
) {
1127 int ret
= bt_field_unsigned_integer_get_value(
1128 content_size_field
, &content_size
);
1132 content_size
= packet_size
;
1135 if (content_size
> packet_size
) {
1136 BT_LOGW("Invalid packet or content size: packet context field indicates content size is greater than packet size: "
1137 "notit-addr=%p, packet-context-field-addr=%p, "
1138 "packet-size=%" PRIu64
", content-size=%" PRIu64
,
1139 notit
, notit
->dscopes
.stream_packet_context
,
1140 packet_size
, content_size
);
1141 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1145 if (packet_size
!= -1ULL) {
1146 notit
->cur_packet_size
= packet_size
;
1149 * Use the content size as packet size indicator if the
1150 * packet size field is missing. This means there is no
1151 * padding in this stream.
1153 notit
->cur_packet_size
= content_size
;
1155 notit
->cur_content_size
= content_size
;
1156 BT_LOGV("Set current packet and content sizes: "
1157 "notit-addr=%p, packet-size=%" PRIu64
", content-size=%" PRIu64
,
1158 notit
, packet_size
, content_size
);
1160 BT_PUT(packet_size_field
);
1161 BT_PUT(content_size_field
);
1166 enum bt_notif_iter_status
after_packet_context_state(
1167 struct bt_notif_iter
*notit
)
1169 enum bt_notif_iter_status status
;
1171 status
= set_current_packet_content_sizes(notit
);
1172 if (status
== BT_NOTIF_ITER_STATUS_OK
) {
1173 notit
->state
= STATE_EMIT_NOTIF_NEW_PACKET
;
1180 enum bt_notif_iter_status
read_event_header_begin_state(
1181 struct bt_notif_iter
*notit
)
1183 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1184 struct bt_field_type
*event_header_type
= NULL
;
1186 /* Reset the position of the last event header */
1187 notit
->buf
.last_eh_at
= notit
->buf
.at
;
1189 /* Check if we have some content left */
1190 if (notit
->cur_content_size
>= 0) {
1191 if (packet_at(notit
) == notit
->cur_content_size
) {
1192 /* No more events! */
1193 BT_LOGV("Reached end of packet: notit-addr=%p, "
1194 "cur=%zu", notit
, packet_at(notit
));
1195 notit
->state
= STATE_EMIT_NOTIF_END_OF_PACKET
;
1197 } else if (packet_at(notit
) > notit
->cur_content_size
) {
1198 /* That's not supposed to happen */
1199 BT_LOGV("Before decoding event header field: cursor is passed the packet's content: "
1200 "notit-addr=%p, content-size=%" PRId64
", "
1201 "cur=%zu", notit
, notit
->cur_content_size
,
1203 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1208 event_header_type
= bt_stream_class_get_event_header_type(
1209 notit
->meta
.stream_class
);
1210 if (!event_header_type
) {
1211 notit
->state
= STATE_AFTER_STREAM_EVENT_HEADER
;
1215 put_event_dscopes(notit
);
1216 BT_LOGV("Decoding event header field: "
1217 "notit-addr=%p, stream-class-addr=%p, "
1218 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1220 notit
, notit
->meta
.stream_class
,
1221 bt_stream_class_get_name(notit
->meta
.stream_class
),
1222 bt_stream_class_get_id(notit
->meta
.stream_class
),
1224 status
= read_dscope_begin_state(notit
, event_header_type
,
1225 STATE_AFTER_STREAM_EVENT_HEADER
,
1226 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
,
1227 ¬it
->dscopes
.stream_event_header
);
1229 BT_LOGW("Cannot decode event header field: "
1230 "notit-addr=%p, stream-class-addr=%p, "
1231 "stream-class-name=\"%s\", "
1232 "stream-class-id=%" PRId64
", ft-addr=%p",
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
),
1239 BT_PUT(event_header_type
);
1245 enum bt_notif_iter_status
read_event_header_continue_state(
1246 struct bt_notif_iter
*notit
)
1248 return read_dscope_continue_state(notit
,
1249 STATE_AFTER_STREAM_EVENT_HEADER
);
1253 enum bt_notif_iter_status
set_current_event_class(struct bt_notif_iter
*notit
)
1256 * The assert() calls in this function are okay because it is
1257 * assumed here that all the metadata objects have been
1258 * validated for CTF correctness before decoding actual streams.
1261 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1262 struct bt_field_type
*event_header_type
;
1263 struct bt_field_type
*id_field_type
= NULL
;
1264 struct bt_field_type
*v_field_type
= NULL
;
1265 uint64_t event_id
= -1ULL;
1268 event_header_type
= bt_stream_class_get_event_header_type(
1269 notit
->meta
.stream_class
);
1270 if (!event_header_type
) {
1272 * No event header, therefore no event class ID field,
1273 * therefore only one event class.
1275 goto single_event_class
;
1278 /* Is there any "id"/"v" field in the event header? */
1279 assert(is_struct_type(event_header_type
));
1280 id_field_type
= bt_field_type_structure_get_field_type_by_name(
1281 event_header_type
, "id");
1282 v_field_type
= bt_field_type_structure_get_field_type_by_name(
1283 event_header_type
, "v");
1284 assert(notit
->dscopes
.stream_event_header
);
1288 * | | |_ _|_ _| __ __ _
1289 * | | | | | || '_ \ / _` |
1290 * | |___| | | || | | | (_| | S P E C I A L
1291 * |_____|_| |_||_| |_|\__, | C A S E â„¢
1294 struct bt_field
*v_field
= NULL
;
1295 struct bt_field
*v_struct_field
= NULL
;
1296 struct bt_field
*v_struct_id_field
= NULL
;
1298 // TODO: optimalize!
1299 v_field
= bt_field_structure_get_field_by_name(
1300 notit
->dscopes
.stream_event_header
, "v");
1304 bt_field_variant_get_current_field(v_field
);
1305 if (!v_struct_field
) {
1306 goto end_v_field_type
;
1309 // TODO: optimalize!
1311 bt_field_structure_get_field_by_name(v_struct_field
, "id");
1312 if (!v_struct_id_field
) {
1313 goto end_v_field_type
;
1316 if (bt_field_is_integer(v_struct_id_field
)) {
1317 ret
= bt_field_unsigned_integer_get_value(
1318 v_struct_id_field
, &event_id
);
1320 BT_LOGV("Cannot get value of unsigned integer field (`id`): continuing: "
1321 "notit=%p, field-addr=%p",
1322 notit
, v_struct_id_field
);
1329 BT_PUT(v_struct_field
);
1330 BT_PUT(v_struct_id_field
);
1333 if (id_field_type
&& event_id
== -1ULL) {
1334 /* Check "id" field */
1335 struct bt_field
*id_field
= NULL
;
1336 int ret_get_value
= 0;
1338 // TODO: optimalize!
1339 id_field
= bt_field_structure_get_field_by_name(
1340 notit
->dscopes
.stream_event_header
, "id");
1342 goto check_event_id
;
1345 if (bt_field_is_integer(id_field
)) {
1346 ret_get_value
= bt_field_unsigned_integer_get_value(
1347 id_field
, &event_id
);
1348 } else if (bt_field_is_enumeration(id_field
)) {
1349 struct bt_field
*container
;
1351 container
= bt_field_enumeration_get_container(
1354 ret_get_value
= bt_field_unsigned_integer_get_value(
1355 container
, &event_id
);
1359 assert(ret_get_value
== 0);
1364 if (event_id
== -1ULL) {
1366 /* Event ID not found: single event? */
1367 assert(bt_stream_class_get_event_class_count(
1368 notit
->meta
.stream_class
) == 1);
1372 BT_LOGV("Found event class ID to use: notit-addr=%p, "
1373 "stream-class-addr=%p, stream-class-name=\"%s\", "
1374 "stream-class-id=%" PRId64
", "
1375 "event-class-id=%" PRIu64
,
1376 notit
, notit
->meta
.stream_class
,
1377 bt_stream_class_get_name(notit
->meta
.stream_class
),
1378 bt_stream_class_get_id(notit
->meta
.stream_class
),
1380 BT_PUT(notit
->meta
.event_class
);
1381 notit
->meta
.event_class
= bt_stream_class_get_event_class_by_id(
1382 notit
->meta
.stream_class
, event_id
);
1383 if (!notit
->meta
.event_class
) {
1384 BT_LOGW("No event class with ID of event class ID to use in stream class: "
1385 "notit-addr=%p, stream-class-addr=%p, "
1386 "stream-class-name=\"%s\", "
1387 "stream-class-id=%" PRId64
", "
1388 "event-class-id=%" PRIu64
,
1389 notit
, notit
->meta
.stream_class
,
1390 bt_stream_class_get_name(notit
->meta
.stream_class
),
1391 bt_stream_class_get_id(notit
->meta
.stream_class
),
1393 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1397 BT_LOGV("Set current event class: "
1398 "notit-addr=%p, event-class-addr=%p, "
1399 "event-class-name=\"%s\", event-class-id=%" PRId64
,
1400 notit
, notit
->meta
.event_class
,
1401 bt_event_class_get_name(notit
->meta
.event_class
),
1402 bt_event_class_get_id(notit
->meta
.event_class
));
1405 BT_PUT(event_header_type
);
1406 BT_PUT(id_field_type
);
1407 BT_PUT(v_field_type
);
1413 enum bt_notif_iter_status
after_event_header_state(
1414 struct bt_notif_iter
*notit
)
1416 enum bt_notif_iter_status status
;
1418 status
= set_current_event_class(notit
);
1419 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
1423 notit
->state
= STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
;
1430 enum bt_notif_iter_status
read_stream_event_context_begin_state(
1431 struct bt_notif_iter
*notit
)
1433 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1434 struct bt_field_type
*stream_event_context_type
;
1436 stream_event_context_type
= bt_stream_class_get_event_context_type(
1437 notit
->meta
.stream_class
);
1438 if (!stream_event_context_type
) {
1439 notit
->state
= STATE_DSCOPE_EVENT_CONTEXT_BEGIN
;
1443 BT_LOGV("Decoding stream event context field: "
1444 "notit-addr=%p, stream-class-addr=%p, "
1445 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1447 notit
, notit
->meta
.stream_class
,
1448 bt_stream_class_get_name(notit
->meta
.stream_class
),
1449 bt_stream_class_get_id(notit
->meta
.stream_class
),
1450 stream_event_context_type
);
1451 status
= read_dscope_begin_state(notit
, stream_event_context_type
,
1452 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
,
1453 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
,
1454 ¬it
->dscopes
.stream_event_context
);
1456 BT_LOGW("Cannot decode stream event context field: "
1457 "notit-addr=%p, stream-class-addr=%p, "
1458 "stream-class-name=\"%s\", "
1459 "stream-class-id=%" PRId64
", ft-addr=%p",
1460 notit
, notit
->meta
.stream_class
,
1461 bt_stream_class_get_name(notit
->meta
.stream_class
),
1462 bt_stream_class_get_id(notit
->meta
.stream_class
),
1463 stream_event_context_type
);
1467 BT_PUT(stream_event_context_type
);
1473 enum bt_notif_iter_status
read_stream_event_context_continue_state(
1474 struct bt_notif_iter
*notit
)
1476 return read_dscope_continue_state(notit
,
1477 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
);
1481 enum bt_notif_iter_status
read_event_context_begin_state(
1482 struct bt_notif_iter
*notit
)
1484 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1485 struct bt_field_type
*event_context_type
;
1487 event_context_type
= bt_event_class_get_context_type(
1488 notit
->meta
.event_class
);
1489 if (!event_context_type
) {
1490 notit
->state
= STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
;
1494 BT_LOGV("Decoding event context field: "
1495 "notit-addr=%p, event-class-addr=%p, "
1496 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1498 notit
, notit
->meta
.event_class
,
1499 bt_event_class_get_name(notit
->meta
.event_class
),
1500 bt_event_class_get_id(notit
->meta
.event_class
),
1501 event_context_type
);
1502 status
= read_dscope_begin_state(notit
, event_context_type
,
1503 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
1504 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
,
1505 ¬it
->dscopes
.event_context
);
1507 BT_LOGW("Cannot decode event context field: "
1508 "notit-addr=%p, event-class-addr=%p, "
1509 "event-class-name=\"%s\", "
1510 "event-class-id=%" PRId64
", ft-addr=%p",
1511 notit
, notit
->meta
.event_class
,
1512 bt_event_class_get_name(notit
->meta
.event_class
),
1513 bt_event_class_get_id(notit
->meta
.event_class
),
1514 event_context_type
);
1518 BT_PUT(event_context_type
);
1524 enum bt_notif_iter_status
read_event_context_continue_state(
1525 struct bt_notif_iter
*notit
)
1527 return read_dscope_continue_state(notit
,
1528 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
);
1532 enum bt_notif_iter_status
read_event_payload_begin_state(
1533 struct bt_notif_iter
*notit
)
1535 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1536 struct bt_field_type
*event_payload_type
;
1538 event_payload_type
= bt_event_class_get_payload_type(
1539 notit
->meta
.event_class
);
1540 if (!event_payload_type
) {
1541 notit
->state
= STATE_EMIT_NOTIF_EVENT
;
1545 BT_LOGV("Decoding event payload field: "
1546 "notit-addr=%p, event-class-addr=%p, "
1547 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1549 notit
, notit
->meta
.event_class
,
1550 bt_event_class_get_name(notit
->meta
.event_class
),
1551 bt_event_class_get_id(notit
->meta
.event_class
),
1552 event_payload_type
);
1553 status
= read_dscope_begin_state(notit
, event_payload_type
,
1554 STATE_EMIT_NOTIF_EVENT
,
1555 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
1556 ¬it
->dscopes
.event_payload
);
1558 BT_LOGW("Cannot decode event payload field: "
1559 "notit-addr=%p, event-class-addr=%p, "
1560 "event-class-name=\"%s\", "
1561 "event-class-id=%" PRId64
", ft-addr=%p",
1562 notit
, notit
->meta
.event_class
,
1563 bt_event_class_get_name(notit
->meta
.event_class
),
1564 bt_event_class_get_id(notit
->meta
.event_class
),
1565 event_payload_type
);
1569 BT_PUT(event_payload_type
);
1575 enum bt_notif_iter_status
read_event_payload_continue_state(
1576 struct bt_notif_iter
*notit
)
1578 return read_dscope_continue_state(notit
, STATE_EMIT_NOTIF_EVENT
);
1582 enum bt_notif_iter_status
skip_packet_padding_state(
1583 struct bt_notif_iter
*notit
)
1585 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1586 size_t bits_to_skip
;
1588 assert(notit
->cur_packet_size
> 0);
1589 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1590 if (bits_to_skip
== 0) {
1591 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1594 size_t bits_to_consume
;
1596 BT_LOGV("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu",
1597 bits_to_skip
, notit
, bits_to_skip
);
1598 status
= buf_ensure_available_bits(notit
);
1599 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
1603 bits_to_consume
= MIN(buf_available_bits(notit
), bits_to_skip
);
1604 BT_LOGV("Skipping %zu bits of padding: notit-addr=%p, size=%zu",
1605 bits_to_consume
, notit
, bits_to_consume
);
1606 buf_consume_bits(notit
, bits_to_consume
);
1607 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1608 if (bits_to_skip
== 0) {
1609 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1619 enum bt_notif_iter_status
handle_state(struct bt_notif_iter
*notit
)
1621 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1622 const enum state state
= notit
->state
;
1624 BT_LOGV("Handling state: notit-addr=%p, state=%s",
1625 notit
, state_string(state
));
1627 // TODO: optimalize!
1630 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1632 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
1633 status
= read_packet_header_begin_state(notit
);
1635 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
1636 status
= read_packet_header_continue_state(notit
);
1638 case STATE_AFTER_TRACE_PACKET_HEADER
:
1639 status
= after_packet_header_state(notit
);
1641 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
1642 status
= read_packet_context_begin_state(notit
);
1644 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
1645 status
= read_packet_context_continue_state(notit
);
1647 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
1648 status
= after_packet_context_state(notit
);
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 BT_PUT(notit
->meta
.stream_class
);
1712 BT_PUT(notit
->meta
.event_class
);
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;
1728 int bt_notif_iter_switch_packet(struct bt_notif_iter
*notit
)
1733 * We don't put the stream class here because we need to make
1734 * sure that all the packets processed by the same notification
1735 * iterator refer to the same stream class (the first one).
1738 if (notit
->cur_packet_size
!= -1) {
1739 notit
->cur_packet_offset
+= notit
->cur_packet_size
;
1741 BT_LOGV("Switching packet: notit-addr=%p, cur=%zu, "
1742 "packet-offset=%" PRId64
, notit
, notit
->buf
.at
,
1743 notit
->cur_packet_offset
);
1744 stack_clear(notit
->stack
);
1745 BT_PUT(notit
->meta
.event_class
);
1746 BT_PUT(notit
->packet
);
1747 BT_PUT(notit
->cur_timestamp_end
);
1748 put_all_dscopes(notit
);
1751 * Adjust current buffer so that addr points to the beginning of the new
1754 if (notit
->buf
.addr
) {
1755 size_t consumed_bytes
= (size_t) (notit
->buf
.at
/ CHAR_BIT
);
1757 /* Packets are assumed to start on a byte frontier. */
1758 if (notit
->buf
.at
% CHAR_BIT
) {
1759 BT_LOGW("Cannot switch packet: current position is not a multiple of 8: "
1760 "notit-addr=%p, cur=%zu", notit
, notit
->buf
.at
);
1765 notit
->buf
.addr
+= consumed_bytes
;
1766 notit
->buf
.sz
-= consumed_bytes
;
1768 notit
->buf
.packet_offset
= 0;
1769 BT_LOGV("Adjusted buffer: addr=%p, size=%zu",
1770 notit
->buf
.addr
, notit
->buf
.sz
);
1773 notit
->cur_content_size
= -1;
1774 notit
->cur_packet_size
= -1;
1775 notit
->cur_sc_field_path_cache
= NULL
;
1782 struct bt_field
*get_next_field(struct bt_notif_iter
*notit
)
1784 struct bt_field
*next_field
= NULL
;
1785 struct bt_field
*base_field
;
1786 struct bt_field_type
*base_type
;
1789 assert(!stack_empty(notit
->stack
));
1790 index
= stack_top(notit
->stack
)->index
;
1791 base_field
= stack_top(notit
->stack
)->base
;
1793 base_type
= bt_field_get_type(base_field
);
1796 switch (bt_field_type_get_type_id(base_type
)) {
1797 case BT_FIELD_TYPE_ID_STRUCT
:
1798 next_field
= bt_field_structure_get_field_by_index(
1801 case BT_FIELD_TYPE_ID_ARRAY
:
1802 next_field
= bt_field_array_get_field(base_field
, index
);
1804 case BT_FIELD_TYPE_ID_SEQUENCE
:
1805 next_field
= bt_field_sequence_get_field(base_field
, index
);
1807 case BT_FIELD_TYPE_ID_VARIANT
:
1808 next_field
= bt_field_variant_get_current_field(base_field
);
1811 BT_LOGF("Unknown base field type ID: "
1812 "notit-addr=%p, ft-addr=%p, ft-id=%s",
1814 bt_field_type_id_string(
1815 bt_field_type_get_type_id(base_type
)));
1824 void update_clock_state(uint64_t *state
,
1825 struct bt_field
*value_field
)
1827 struct bt_field_type
*value_type
= NULL
;
1828 uint64_t requested_new_value
;
1829 uint64_t requested_new_value_mask
;
1830 uint64_t cur_value_masked
;
1831 int requested_new_value_size
;
1834 value_type
= bt_field_get_type(value_field
);
1836 assert(bt_field_type_is_integer(value_type
));
1837 requested_new_value_size
=
1838 bt_field_type_integer_get_size(value_type
);
1839 assert(requested_new_value_size
> 0);
1840 ret
= bt_field_unsigned_integer_get_value(value_field
,
1841 &requested_new_value
);
1845 * Special case for a 64-bit new value, which is the limit
1846 * of a clock value as of this version: overwrite the
1847 * current value directly.
1849 if (requested_new_value_size
== 64) {
1850 *state
= requested_new_value
;
1854 requested_new_value_mask
= (1ULL << requested_new_value_size
) - 1;
1855 cur_value_masked
= *state
& requested_new_value_mask
;
1857 if (requested_new_value
< cur_value_masked
) {
1859 * It looks like a wrap happened on the number of bits
1860 * of the requested new value. Assume that the clock
1861 * value wrapped only one time.
1863 *state
+= requested_new_value_mask
+ 1;
1866 /* Clear the low bits of the current clock value. */
1867 *state
&= ~requested_new_value_mask
;
1869 /* Set the low bits of the current clock value. */
1870 *state
|= requested_new_value
;
1872 BT_LOGV("Updated clock's value from integer field's value: "
1873 "value=%" PRIu64
, *state
);
1878 enum bt_btr_status
update_clock(struct bt_notif_iter
*notit
,
1879 struct bt_field
*int_field
)
1881 gboolean clock_class_found
;
1882 uint64_t *clock_state
= NULL
;
1883 struct bt_field_type
*int_field_type
= NULL
;
1884 enum bt_btr_status ret
= BT_BTR_STATUS_OK
;
1885 struct bt_clock_class
*clock_class
= NULL
;
1887 int_field_type
= bt_field_get_type(int_field
);
1888 assert(int_field_type
);
1889 clock_class
= bt_field_type_integer_get_mapped_clock_class(
1891 if (likely(!clock_class
)) {
1895 clock_class_found
= g_hash_table_lookup_extended(notit
->clock_states
,
1896 clock_class
, NULL
, (gpointer
) &clock_state
);
1897 if (!clock_class_found
) {
1898 clock_state
= g_new0(uint64_t, 1);
1900 BT_LOGE_STR("Failed to allocate a uint64_t.");
1901 ret
= BT_BTR_STATUS_ENOMEM
;
1905 g_hash_table_insert(notit
->clock_states
, bt_get(clock_class
),
1909 /* Update the clock's state. */
1910 BT_LOGV("Updating notification iterator's clock's value from integer field: "
1911 "notit-addr=%p, clock-class-addr=%p, "
1912 "clock-class-name=\"%s\", value=%" PRIu64
,
1914 bt_clock_class_get_name(clock_class
), *clock_state
);
1915 update_clock_state(clock_state
, int_field
);
1917 bt_put(int_field_type
);
1918 bt_put(clock_class
);
1923 enum bt_btr_status
btr_unsigned_int_common(uint64_t value
,
1924 struct bt_field_type
*type
, void *data
,
1925 struct bt_field
**out_int_field
)
1927 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
1928 struct bt_field
*field
= NULL
;
1929 struct bt_field
*int_field
= NULL
;
1930 struct bt_notif_iter
*notit
= data
;
1933 BT_LOGV("Common unsigned integer function called from BTR: "
1934 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
1935 "ft-id=%s, value=%" PRIu64
,
1936 notit
, notit
->btr
, type
,
1937 bt_field_type_id_string(
1938 bt_field_type_get_type_id(type
)),
1941 /* Create next field */
1942 field
= get_next_field(notit
);
1944 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
1945 status
= BT_BTR_STATUS_ERROR
;
1949 switch(bt_field_type_get_type_id(type
)) {
1950 case BT_FIELD_TYPE_ID_INTEGER
:
1951 /* Integer field is created field */
1952 BT_MOVE(int_field
, field
);
1955 case BT_FIELD_TYPE_ID_ENUM
:
1956 int_field
= bt_field_enumeration_get_container(field
);
1958 type
= bt_field_get_type(int_field
);
1962 BT_LOGF("Unexpected field type ID: "
1963 "notit-addr=%p, ft-addr=%p, ft-id=%s",
1965 bt_field_type_id_string(
1966 bt_field_type_get_type_id(type
)));
1971 ret
= bt_field_unsigned_integer_set_value(int_field
, value
);
1973 stack_top(notit
->stack
)->index
++;
1974 *out_int_field
= int_field
;
1983 enum bt_btr_status
btr_timestamp_end_cb(void *value
,
1984 struct bt_field_type
*type
, void *data
)
1986 enum bt_btr_status status
;
1987 struct bt_field
*field
= NULL
;
1988 struct bt_notif_iter
*notit
= data
;
1990 BT_LOGV("`timestamp_end` unsigned integer function called from BTR: "
1991 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
1993 notit
, notit
->btr
, type
,
1994 bt_field_type_id_string(
1995 bt_field_type_get_type_id(type
)));
1996 status
= btr_unsigned_int_common(*((uint64_t *) value
), type
, data
,
1999 /* Set as the current packet's timestamp_end field. */
2000 BT_MOVE(notit
->cur_timestamp_end
, field
);
2005 enum bt_btr_status
btr_unsigned_int_cb(uint64_t value
,
2006 struct bt_field_type
*type
, void *data
)
2008 struct bt_notif_iter
*notit
= data
;
2009 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2010 struct bt_field
*field
= NULL
;
2011 struct field_cb_override
*override
;
2013 BT_LOGV("Unsigned integer function called from BTR: "
2014 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2015 "ft-id=%s, value=%" PRIu64
,
2016 notit
, notit
->btr
, type
,
2017 bt_field_type_id_string(
2018 bt_field_type_get_type_id(type
)),
2020 override
= g_hash_table_lookup(notit
->field_overrides
, type
);
2021 if (unlikely(override
)) {
2022 /* Override function logs errors */
2023 status
= override
->func(&value
, type
, override
->data
);
2027 status
= btr_unsigned_int_common(value
, type
, data
, &field
);
2028 if (status
!= BT_BTR_STATUS_OK
) {
2029 /* btr_unsigned_int_common() logs errors */
2033 status
= update_clock(notit
, field
);
2040 enum bt_btr_status
btr_signed_int_cb(int64_t value
,
2041 struct bt_field_type
*type
, void *data
)
2043 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2044 struct bt_field
*field
= NULL
;
2045 struct bt_field
*int_field
= NULL
;
2046 struct bt_notif_iter
*notit
= data
;
2049 BT_LOGV("Signed integer function called from BTR: "
2050 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2051 "ft-id=%s, value=%" PRId64
,
2052 notit
, notit
->btr
, type
,
2053 bt_field_type_id_string(
2054 bt_field_type_get_type_id(type
)),
2057 /* create next field */
2058 field
= get_next_field(notit
);
2060 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2061 status
= BT_BTR_STATUS_ERROR
;
2065 switch(bt_field_type_get_type_id(type
)) {
2066 case BT_FIELD_TYPE_ID_INTEGER
:
2067 /* Integer field is created field */
2068 BT_MOVE(int_field
, field
);
2071 case BT_FIELD_TYPE_ID_ENUM
:
2072 int_field
= bt_field_enumeration_get_container(field
);
2074 type
= bt_field_get_type(int_field
);
2078 BT_LOGF("Unexpected field type ID: "
2079 "notit-addr=%p, ft-addr=%p, ft-id=%s",
2081 bt_field_type_id_string(
2082 bt_field_type_get_type_id(type
)));
2087 ret
= bt_field_signed_integer_set_value(int_field
, value
);
2089 stack_top(notit
->stack
)->index
++;
2090 status
= update_clock(notit
, int_field
);
2100 enum bt_btr_status
btr_floating_point_cb(double value
,
2101 struct bt_field_type
*type
, void *data
)
2103 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2104 struct bt_field
*field
= NULL
;
2105 struct bt_notif_iter
*notit
= data
;
2108 BT_LOGV("Floating point number function called from BTR: "
2109 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2110 "ft-id=%s, value=%f",
2111 notit
, notit
->btr
, type
,
2112 bt_field_type_id_string(
2113 bt_field_type_get_type_id(type
)),
2116 /* Create next field */
2117 field
= get_next_field(notit
);
2119 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2120 status
= BT_BTR_STATUS_ERROR
;
2124 ret
= bt_field_floating_point_set_value(field
, value
);
2126 stack_top(notit
->stack
)->index
++;
2134 enum bt_btr_status
btr_string_begin_cb(
2135 struct bt_field_type
*type
, void *data
)
2137 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2138 struct bt_field
*field
= NULL
;
2139 struct bt_notif_iter
*notit
= data
;
2142 BT_LOGV("String (beginning) function called from BTR: "
2143 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2145 notit
, notit
->btr
, type
,
2146 bt_field_type_id_string(
2147 bt_field_type_get_type_id(type
)));
2149 /* Create next field */
2150 field
= get_next_field(notit
);
2152 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2153 status
= BT_BTR_STATUS_ERROR
;
2158 * Push on stack. Not a compound type per se, but we know that only
2159 * btr_string_cb() may be called between this call and a subsequent
2160 * call to btr_string_end_cb().
2162 ret
= stack_push(notit
->stack
, field
);
2164 BT_LOGE("Cannot push string field on stack: "
2165 "notit-addr=%p, field-addr=%p", notit
, field
);
2166 status
= BT_BTR_STATUS_ERROR
;
2171 * Initialize string field payload to an empty string since in the
2172 * case of a length 0 string the btr_string_cb won't be called and
2173 * we will end up with an unset string payload.
2175 ret
= bt_field_string_set_value(field
, "");
2177 BT_LOGE("Cannot initialize string field's value to an empty string: "
2178 "notit-addr=%p, field-addr=%p, ret=%d",
2180 status
= BT_BTR_STATUS_ERROR
;
2191 enum bt_btr_status
btr_string_cb(const char *value
,
2192 size_t len
, struct bt_field_type
*type
, void *data
)
2194 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2195 struct bt_field
*field
= NULL
;
2196 struct bt_notif_iter
*notit
= data
;
2199 BT_LOGV("String (substring) function called from BTR: "
2200 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2201 "ft-id=%s, string-length=%zu",
2202 notit
, notit
->btr
, type
,
2203 bt_field_type_id_string(
2204 bt_field_type_get_type_id(type
)),
2207 /* Get string field */
2208 field
= stack_top(notit
->stack
)->base
;
2211 /* Append current string */
2212 ret
= bt_field_string_append_len(field
, value
, len
);
2214 BT_LOGE("Cannot append substring to string field's value: "
2215 "notit-addr=%p, field-addr=%p, string-length=%zu, "
2216 "ret=%d", notit
, field
, len
, ret
);
2217 status
= BT_BTR_STATUS_ERROR
;
2226 enum bt_btr_status
btr_string_end_cb(
2227 struct bt_field_type
*type
, void *data
)
2229 struct bt_notif_iter
*notit
= data
;
2231 BT_LOGV("String (end) function called from BTR: "
2232 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2234 notit
, notit
->btr
, type
,
2235 bt_field_type_id_string(
2236 bt_field_type_get_type_id(type
)));
2238 /* Pop string field */
2239 stack_pop(notit
->stack
);
2241 /* Go to next field */
2242 stack_top(notit
->stack
)->index
++;
2243 return BT_BTR_STATUS_OK
;
2246 enum bt_btr_status
btr_compound_begin_cb(
2247 struct bt_field_type
*type
, void *data
)
2249 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2250 struct bt_notif_iter
*notit
= data
;
2251 struct bt_field
*field
;
2254 BT_LOGV("Compound (beginning) function called from BTR: "
2255 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2257 notit
, notit
->btr
, type
,
2258 bt_field_type_id_string(
2259 bt_field_type_get_type_id(type
)));
2262 if (stack_empty(notit
->stack
)) {
2263 /* Root: create dynamic scope field */
2264 *notit
->cur_dscope_field
= bt_field_create(type
);
2265 field
= *notit
->cur_dscope_field
;
2268 * Field will be put at the end of this function
2269 * (stack_push() will take one reference, but this
2270 * reference is lost upon the equivalent stack_pop()
2271 * later), so also get it for our context to own it.
2273 bt_get(*notit
->cur_dscope_field
);
2276 BT_LOGE("Cannot create compound field: "
2277 "notit-addr=%p, ft-addr=%p, ft-id=%s",
2279 bt_field_type_id_string(
2280 bt_field_type_get_type_id(type
)));
2281 status
= BT_BTR_STATUS_ERROR
;
2285 field
= get_next_field(notit
);
2287 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2288 status
= BT_BTR_STATUS_ERROR
;
2295 ret
= stack_push(notit
->stack
, field
);
2297 BT_LOGE("Cannot push compound field onto the stack: "
2298 "notit-addr=%p, ft-addr=%p, ft-id=%s, ret=%d",
2300 bt_field_type_id_string(
2301 bt_field_type_get_type_id(type
)),
2303 status
= BT_BTR_STATUS_ERROR
;
2313 enum bt_btr_status
btr_compound_end_cb(
2314 struct bt_field_type
*type
, void *data
)
2316 struct bt_notif_iter
*notit
= data
;
2318 BT_LOGV("Compound (end) function called from BTR: "
2319 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2321 notit
, notit
->btr
, type
,
2322 bt_field_type_id_string(
2323 bt_field_type_get_type_id(type
)));
2324 assert(!stack_empty(notit
->stack
));
2327 stack_pop(notit
->stack
);
2329 /* If the stack is not empty, increment the base's index */
2330 if (!stack_empty(notit
->stack
)) {
2331 stack_top(notit
->stack
)->index
++;
2334 return BT_BTR_STATUS_OK
;
2338 struct bt_field
*resolve_field(struct bt_notif_iter
*notit
,
2339 struct bt_field_path
*path
)
2341 struct bt_field
*field
= NULL
;
2344 if (BT_LOG_ON_VERBOSE
) {
2345 GString
*gstr
= bt_field_path_string(path
);
2347 BT_LOGV("Resolving field path: notit-addr=%p, field-path=\"%s\"",
2348 notit
, gstr
? gstr
->str
: NULL
);
2351 g_string_free(gstr
, TRUE
);
2355 switch (bt_field_path_get_root_scope(path
)) {
2356 case BT_SCOPE_TRACE_PACKET_HEADER
:
2357 field
= notit
->dscopes
.trace_packet_header
;
2359 case BT_SCOPE_STREAM_PACKET_CONTEXT
:
2360 field
= notit
->dscopes
.stream_packet_context
;
2362 case BT_SCOPE_STREAM_EVENT_HEADER
:
2363 field
= notit
->dscopes
.stream_event_header
;
2365 case BT_SCOPE_STREAM_EVENT_CONTEXT
:
2366 field
= notit
->dscopes
.stream_event_context
;
2368 case BT_SCOPE_EVENT_CONTEXT
:
2369 field
= notit
->dscopes
.event_context
;
2371 case BT_SCOPE_EVENT_FIELDS
:
2372 field
= notit
->dscopes
.event_payload
;
2375 BT_LOGF("Cannot resolve field path: unknown scope: "
2376 "notit-addr=%p, root-scope=%s",
2377 notit
, bt_scope_string(
2378 bt_field_path_get_root_scope(path
)));
2383 BT_LOGW("Cannot resolve field path: root field not found: "
2384 "notit-addr=%p, root-scope=%s",
2385 notit
, bt_scope_string(
2386 bt_field_path_get_root_scope(path
)));
2392 for (i
= 0; i
< bt_field_path_get_index_count(path
); ++i
) {
2393 struct bt_field
*next_field
= NULL
;
2394 struct bt_field_type
*field_type
;
2395 int index
= bt_field_path_get_index(path
, i
);
2397 field_type
= bt_field_get_type(field
);
2400 if (is_struct_type(field_type
)) {
2401 next_field
= bt_field_structure_get_field_by_index(
2403 } else if (is_variant_type(field_type
)) {
2405 bt_field_variant_get_current_field(field
);
2412 BT_LOGW("Cannot find next field: "
2413 "notit-addr=%p, ft-addr=%p, ft-id=%s, index=%d",
2415 bt_field_type_id_string(
2416 bt_field_type_get_type_id(field_type
)),
2421 /* Move next field -> field */
2422 BT_MOVE(field
, next_field
);
2430 int64_t btr_get_sequence_length_cb(struct bt_field_type
*type
, void *data
)
2434 struct bt_field
*seq_field
;
2435 struct bt_field_path
*field_path
;
2436 struct bt_notif_iter
*notit
= data
;
2437 struct bt_field
*length_field
= NULL
;
2440 field_path
= bt_field_type_sequence_get_length_field_path(type
);
2442 length_field
= resolve_field(notit
, field_path
);
2443 if (!length_field
) {
2444 BT_LOGW("Cannot resolve sequence field type's length field path: "
2445 "notit-addr=%p, ft-addr=%p",
2450 iret
= bt_field_unsigned_integer_get_value(length_field
, &length
);
2452 BT_LOGE("Cannot get value of sequence length field: "
2453 "notit-addr=%p, field-addr=%p",
2454 notit
, length_field
);
2458 seq_field
= stack_top(notit
->stack
)->base
;
2459 iret
= bt_field_sequence_set_length(seq_field
, length_field
);
2461 BT_LOGE("Cannot set sequence field's length field: "
2462 "notit-addr=%p, seq-field-addr=%p, "
2463 "length-field-addr=%p, ",
2464 notit
, seq_field
, length_field
);
2468 ret
= (int64_t) length
;
2471 BT_PUT(length_field
);
2478 struct bt_field_type
*btr_get_variant_type_cb(
2479 struct bt_field_type
*type
, void *data
)
2481 struct bt_field_path
*path
;
2482 struct bt_notif_iter
*notit
= data
;
2483 struct bt_field
*var_field
;
2484 struct bt_field
*tag_field
= NULL
;
2485 struct bt_field
*selected_field
= NULL
;
2486 struct bt_field_type
*selected_field_type
= NULL
;
2488 path
= bt_field_type_variant_get_tag_field_path(type
);
2490 tag_field
= resolve_field(notit
, path
);
2492 BT_LOGW("Cannot resolve variant field type's tag field path: "
2493 "notit-addr=%p, ft-addr=%p",
2499 * We found the enumeration tag field instance which should be
2500 * able to select a current field for this variant. This
2501 * callback function we're in is called _after_
2502 * compound_begin(), so the current stack top's base field is
2503 * the variant field in question. We get the selected field here
2504 * thanks to this tag field (thus creating the selected field),
2505 * which will also provide us with its type. Then, this field
2506 * will remain the current selected one until the next callback
2507 * function call which is used to fill the current selected
2510 var_field
= stack_top(notit
->stack
)->base
;
2511 selected_field
= bt_field_variant_get_field(var_field
, tag_field
);
2512 if (!selected_field
) {
2513 BT_LOGW("Cannot get variant field's selection using tag field: "
2514 "notit-addr=%p, var-field-addr=%p, tag-field-addr=%p",
2515 notit
, var_field
, tag_field
);
2519 selected_field_type
= bt_field_get_type(selected_field
);
2523 BT_PUT(selected_field
);
2526 return selected_field_type
;
2530 int set_event_clocks(struct bt_event
*event
,
2531 struct bt_notif_iter
*notit
)
2534 GHashTableIter iter
;
2535 struct bt_clock_class
*clock_class
;
2536 uint64_t *clock_state
;
2538 g_hash_table_iter_init(&iter
, notit
->clock_states
);
2540 while (g_hash_table_iter_next(&iter
, (gpointer
) &clock_class
,
2541 (gpointer
) &clock_state
)) {
2542 struct bt_clock_value
*clock_value
;
2544 clock_value
= bt_clock_value_create(clock_class
,
2547 BT_LOGE("Cannot create clock value from clock class: "
2548 "notit-addr=%p, clock-class-addr=%p, "
2549 "clock-class-name=\"%s\"",
2551 bt_clock_class_get_name(clock_class
));
2555 ret
= bt_event_set_clock_value(event
, clock_value
);
2556 bt_put(clock_value
);
2558 struct bt_event_class
*event_class
=
2559 bt_event_get_class(event
);
2561 assert(event_class
);
2562 BT_LOGE("Cannot set event's clock value: "
2563 "notit-addr=%p, event-addr=%p, "
2564 "event-class-name=\"%s\", "
2565 "event-class-id=%" PRId64
", "
2566 "clock-class-addr=%p, "
2567 "clock-class-name=\"%s\", "
2568 "clock-value-addr=%p",
2570 bt_event_class_get_name(event_class
),
2571 bt_event_class_get_id(event_class
),
2573 bt_clock_class_get_name(clock_class
),
2575 bt_put(event_class
);
2586 struct bt_event
*create_event(struct bt_notif_iter
*notit
)
2589 struct bt_event
*event
;
2591 BT_LOGV("Creating event for event notification: "
2592 "notit-addr=%p, event-class-addr=%p, "
2593 "event-class-name=\"%s\", "
2594 "event-class-id=%" PRId64
,
2595 notit
, notit
->meta
.event_class
,
2596 bt_event_class_get_name(notit
->meta
.event_class
),
2597 bt_event_class_get_id(notit
->meta
.event_class
));
2599 /* Create event object. */
2600 event
= bt_event_create(notit
->meta
.event_class
);
2602 BT_LOGE("Cannot create event: "
2603 "notit-addr=%p, event-class-addr=%p, "
2604 "event-class-name=\"%s\", "
2605 "event-class-id=%" PRId64
,
2606 notit
, notit
->meta
.event_class
,
2607 bt_event_class_get_name(notit
->meta
.event_class
),
2608 bt_event_class_get_id(notit
->meta
.event_class
));
2612 /* Set header, stream event context, context, and payload fields. */
2613 ret
= bt_event_set_header(event
,
2614 notit
->dscopes
.stream_event_header
);
2616 BT_LOGE("Cannot set event's header field: "
2617 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2618 "event-class-name=\"%s\", "
2619 "event-class-id=%" PRId64
", field-addr=%p",
2620 notit
, event
, notit
->meta
.event_class
,
2621 bt_event_class_get_name(notit
->meta
.event_class
),
2622 bt_event_class_get_id(notit
->meta
.event_class
),
2623 notit
->dscopes
.stream_event_header
);
2627 ret
= bt_event_set_stream_event_context(event
,
2628 notit
->dscopes
.stream_event_context
);
2630 BT_LOGE("Cannot set event's context field: "
2631 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2632 "event-class-name=\"%s\", "
2633 "event-class-id=%" PRId64
", field-addr=%p",
2634 notit
, event
, notit
->meta
.event_class
,
2635 bt_event_class_get_name(notit
->meta
.event_class
),
2636 bt_event_class_get_id(notit
->meta
.event_class
),
2637 notit
->dscopes
.stream_event_context
);
2641 ret
= bt_event_set_event_context(event
,
2642 notit
->dscopes
.event_context
);
2644 BT_LOGE("Cannot set event's stream event context field: "
2645 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2646 "event-class-name=\"%s\", "
2647 "event-class-id=%" PRId64
", field-addr=%p",
2648 notit
, event
, notit
->meta
.event_class
,
2649 bt_event_class_get_name(notit
->meta
.event_class
),
2650 bt_event_class_get_id(notit
->meta
.event_class
),
2651 notit
->dscopes
.event_context
);
2655 ret
= bt_event_set_event_payload(event
,
2656 notit
->dscopes
.event_payload
);
2658 BT_LOGE("Cannot set event's payload field: "
2659 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2660 "event-class-name=\"%s\", "
2661 "event-class-id=%" PRId64
", field-addr=%p",
2662 notit
, event
, notit
->meta
.event_class
,
2663 bt_event_class_get_name(notit
->meta
.event_class
),
2664 bt_event_class_get_id(notit
->meta
.event_class
),
2665 notit
->dscopes
.event_payload
);
2669 ret
= set_event_clocks(event
, notit
);
2671 BT_LOGE("Cannot set event's clock values: "
2672 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2673 "event-class-name=\"%s\", "
2674 "event-class-id=%" PRId64
,
2675 notit
, event
, notit
->meta
.event_class
,
2676 bt_event_class_get_name(notit
->meta
.event_class
),
2677 bt_event_class_get_id(notit
->meta
.event_class
));
2681 /* Associate with current packet. */
2682 assert(notit
->packet
);
2683 ret
= bt_event_set_packet(event
, notit
->packet
);
2685 BT_LOGE("Cannot set event's header field: "
2686 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2687 "event-class-name=\"%s\", "
2688 "event-class-id=%" PRId64
", packet-addr=%p",
2689 notit
, event
, notit
->meta
.event_class
,
2690 bt_event_class_get_name(notit
->meta
.event_class
),
2691 bt_event_class_get_id(notit
->meta
.event_class
),
2706 uint64_t get_cur_stream_instance_id(struct bt_notif_iter
*notit
)
2708 struct bt_field
*stream_instance_id_field
= NULL
;
2709 uint64_t stream_instance_id
= -1ULL;
2712 if (!notit
->dscopes
.trace_packet_header
) {
2716 stream_instance_id_field
= bt_field_structure_get_field_by_name(
2717 notit
->dscopes
.trace_packet_header
, "stream_instance_id");
2718 if (!stream_instance_id_field
) {
2722 ret
= bt_field_unsigned_integer_get_value(stream_instance_id_field
,
2723 &stream_instance_id
);
2725 stream_instance_id
= -1ULL;
2730 bt_put(stream_instance_id_field
);
2731 return stream_instance_id
;
2735 int set_stream(struct bt_notif_iter
*notit
)
2738 struct bt_stream
*stream
= NULL
;
2740 BT_LOGV("Calling user function (get stream): notit-addr=%p, "
2741 "stream-class-addr=%p, stream-class-name=\"%s\", "
2742 "stream-class-id=%" PRId64
,
2743 notit
, notit
->meta
.stream_class
,
2744 bt_stream_class_get_name(notit
->meta
.stream_class
),
2745 bt_stream_class_get_id(notit
->meta
.stream_class
));
2746 stream
= bt_get(notit
->medium
.medops
.get_stream(
2747 notit
->meta
.stream_class
, get_cur_stream_instance_id(notit
),
2748 notit
->medium
.data
));
2749 BT_LOGV("User function returned: stream-addr=%p", stream
);
2751 BT_LOGW_STR("User function failed to return a stream object for the given stream class.");
2756 if (notit
->stream
&& stream
!= notit
->stream
) {
2757 BT_LOGW("User function returned a different stream than the previous one for the same sequence of packets.");
2762 BT_MOVE(notit
->stream
, stream
);
2770 void create_packet(struct bt_notif_iter
*notit
)
2773 struct bt_packet
*packet
= NULL
;
2775 BT_LOGV("Creating packet for packet notification: "
2776 "notit-addr=%p", notit
);
2778 /* Ask the user for the stream */
2779 ret
= set_stream(notit
);
2784 BT_LOGV("Creating packet from stream: "
2785 "notit-addr=%p, stream-addr=%p, "
2786 "stream-class-addr=%p, "
2787 "stream-class-name=\"%s\", "
2788 "stream-class-id=%" PRId64
,
2789 notit
, notit
->stream
, notit
->meta
.stream_class
,
2790 bt_stream_class_get_name(notit
->meta
.stream_class
),
2791 bt_stream_class_get_id(notit
->meta
.stream_class
));
2794 packet
= bt_packet_create(notit
->stream
);
2796 BT_LOGE("Cannot create packet from stream: "
2797 "notit-addr=%p, stream-addr=%p, "
2798 "stream-class-addr=%p, "
2799 "stream-class-name=\"%s\", "
2800 "stream-class-id=%" PRId64
,
2801 notit
, notit
->stream
, notit
->meta
.stream_class
,
2802 bt_stream_class_get_name(notit
->meta
.stream_class
),
2803 bt_stream_class_get_id(notit
->meta
.stream_class
));
2807 /* Set packet's context and header fields */
2808 if (notit
->dscopes
.trace_packet_header
) {
2809 ret
= bt_packet_set_header(packet
,
2810 notit
->dscopes
.trace_packet_header
);
2812 BT_LOGE("Cannot set packet's header field: "
2813 "notit-addr=%p, packet-addr=%p, "
2815 "stream-class-addr=%p, "
2816 "stream-class-name=\"%s\", "
2817 "stream-class-id=%" PRId64
", "
2819 notit
, packet
, notit
->stream
, notit
->meta
.stream_class
,
2820 bt_stream_class_get_name(notit
->meta
.stream_class
),
2821 bt_stream_class_get_id(notit
->meta
.stream_class
),
2822 notit
->dscopes
.trace_packet_header
);
2827 if (notit
->dscopes
.stream_packet_context
) {
2828 ret
= bt_packet_set_context(packet
,
2829 notit
->dscopes
.stream_packet_context
);
2831 BT_LOGE("Cannot set packet's context field: "
2832 "notit-addr=%p, packet-addr=%p, "
2834 "stream-class-addr=%p, "
2835 "stream-class-name=\"%s\", "
2836 "stream-class-id=%" PRId64
", "
2838 notit
, packet
, notit
->stream
, notit
->meta
.stream_class
,
2839 bt_stream_class_get_name(notit
->meta
.stream_class
),
2840 bt_stream_class_get_id(notit
->meta
.stream_class
),
2841 notit
->dscopes
.trace_packet_header
);
2852 BT_MOVE(notit
->packet
, packet
);
2856 void notify_new_packet(struct bt_notif_iter
*notit
,
2857 struct bt_notification
**notification
)
2859 struct bt_notification
*ret
;
2861 /* Initialize the iterator's current packet */
2862 create_packet(notit
);
2863 if (!notit
->packet
) {
2864 BT_LOGE("Cannot create packet for packet notification: "
2865 "notit-addr=%p", notit
);
2869 ret
= bt_notification_packet_begin_create(notit
->packet
);
2871 BT_LOGE("Cannot create packet beginning notification: "
2872 "notit-addr=%p, packet-addr=%p",
2873 notit
, notit
->packet
);
2876 *notification
= ret
;
2880 void notify_end_of_packet(struct bt_notif_iter
*notit
,
2881 struct bt_notification
**notification
)
2883 struct bt_notification
*ret
;
2885 if (!notit
->packet
) {
2889 ret
= bt_notification_packet_end_create(notit
->packet
);
2891 BT_LOGE("Cannot create packet end notification: "
2892 "notit-addr=%p, packet-addr=%p",
2893 notit
, notit
->packet
);
2896 BT_PUT(notit
->packet
);
2897 *notification
= ret
;
2901 void notify_event(struct bt_notif_iter
*notit
,
2902 struct bt_clock_class_priority_map
*cc_prio_map
,
2903 struct bt_notification
**notification
)
2905 struct bt_event
*event
= NULL
;
2906 struct bt_notification
*ret
= NULL
;
2908 /* Make sure that the event contains at least one bit of data */
2909 if (notit
->buf
.at
== notit
->buf
.last_eh_at
) {
2910 BT_LOGE("Cannot create empty event with 0 bits of data: "
2911 "notit-addr=%p, packet-cur=%zu",
2912 notit
, packet_at(notit
));
2917 event
= create_event(notit
);
2919 BT_LOGE("Cannot create event for event notification: "
2920 "notit-addr=%p", notit
);
2924 ret
= bt_notification_event_create(event
, cc_prio_map
);
2926 BT_LOGE("Cannot create event notification: "
2927 "notit-addr=%p, event-addr=%p, "
2928 "cc-prio-map-addr=%p",
2929 notit
, event
, cc_prio_map
);
2932 *notification
= ret
;
2938 void init_trace_field_path_cache(struct bt_trace
*trace
,
2939 struct trace_field_path_cache
*trace_field_path_cache
)
2942 int stream_instance_id
= -1;
2944 struct bt_field_type
*packet_header
= NULL
;
2946 packet_header
= bt_trace_get_packet_header_type(trace
);
2947 if (!packet_header
) {
2951 if (!bt_field_type_is_structure(packet_header
)) {
2955 count
= bt_field_type_structure_get_field_count(packet_header
);
2958 for (i
= 0; (i
< count
&& (stream_id
== -1 || stream_instance_id
== -1)); i
++) {
2960 const char *field_name
;
2962 ret
= bt_field_type_structure_get_field_by_index(packet_header
,
2963 &field_name
, NULL
, i
);
2965 BT_LOGE("Cannot get structure field's field: "
2966 "field-addr=%p, index=%d",
2971 if (stream_id
== -1 && !strcmp(field_name
, "stream_id")) {
2973 } else if (stream_instance_id
== -1 &&
2974 !strcmp(field_name
, "stream_instance_id")) {
2975 stream_instance_id
= i
;
2980 trace_field_path_cache
->stream_id
= stream_id
;
2981 trace_field_path_cache
->stream_instance_id
= stream_instance_id
;
2982 BT_PUT(packet_header
);
2986 struct bt_notif_iter
*bt_notif_iter_create(struct bt_trace
*trace
,
2987 size_t max_request_sz
,
2988 struct bt_notif_iter_medium_ops medops
, void *data
)
2990 struct bt_notif_iter
*notit
= NULL
;
2991 struct bt_btr_cbs cbs
= {
2993 .signed_int
= btr_signed_int_cb
,
2994 .unsigned_int
= btr_unsigned_int_cb
,
2995 .floating_point
= btr_floating_point_cb
,
2996 .string_begin
= btr_string_begin_cb
,
2997 .string
= btr_string_cb
,
2998 .string_end
= btr_string_end_cb
,
2999 .compound_begin
= btr_compound_begin_cb
,
3000 .compound_end
= btr_compound_end_cb
,
3003 .get_sequence_length
= btr_get_sequence_length_cb
,
3004 .get_variant_type
= btr_get_variant_type_cb
,
3009 assert(medops
.request_bytes
);
3010 assert(medops
.get_stream
);
3011 BT_LOGD("Creating CTF plugin notification iterator: "
3012 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
3014 trace
, bt_trace_get_name(trace
), max_request_sz
, data
);
3015 notit
= g_new0(struct bt_notif_iter
, 1);
3017 BT_LOGE_STR("Failed to allocate one CTF plugin notification iterator.");
3020 notit
->clock_states
= g_hash_table_new_full(g_direct_hash
,
3021 g_direct_equal
, bt_put
, g_free
);
3022 if (!notit
->clock_states
) {
3023 BT_LOGE_STR("Failed to allocate a GHashTable.");
3026 notit
->meta
.trace
= bt_get(trace
);
3027 notit
->medium
.medops
= medops
;
3028 notit
->medium
.max_request_sz
= max_request_sz
;
3029 notit
->medium
.data
= data
;
3030 notit
->stack
= stack_new(notit
);
3031 if (!notit
->stack
) {
3032 BT_LOGE_STR("Failed to create field stack.");
3036 notit
->btr
= bt_btr_create(cbs
, notit
);
3038 BT_LOGE_STR("Failed to create binary type reader (BTR).");
3042 bt_notif_iter_reset(notit
);
3043 init_trace_field_path_cache(trace
, ¬it
->trace_field_path_cache
);
3044 notit
->sc_field_path_caches
= g_hash_table_new_full(g_direct_hash
,
3045 g_direct_equal
, bt_put
, g_free
);
3046 if (!notit
->sc_field_path_caches
) {
3047 BT_LOGE_STR("Failed to allocate a GHashTable.");
3051 notit
->field_overrides
= g_hash_table_new_full(g_direct_hash
,
3052 g_direct_equal
, bt_put
, g_free
);
3053 if (!notit
->field_overrides
) {
3054 BT_LOGE_STR("Failed to allocate a GHashTable.");
3058 BT_LOGD("Created CTF plugin notification iterator: "
3059 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
3060 "data=%p, notit-addr=%p",
3061 trace
, bt_trace_get_name(trace
), max_request_sz
, data
,
3063 notit
->cur_packet_offset
= 0;
3069 bt_notif_iter_destroy(notit
);
3074 void bt_notif_iter_destroy(struct bt_notif_iter
*notit
)
3076 BT_PUT(notit
->meta
.trace
);
3077 BT_PUT(notit
->meta
.stream_class
);
3078 BT_PUT(notit
->meta
.event_class
);
3079 BT_PUT(notit
->packet
);
3080 BT_PUT(notit
->stream
);
3081 BT_PUT(notit
->cur_timestamp_end
);
3082 put_all_dscopes(notit
);
3084 BT_LOGD("Destroying CTF plugin notification iterator: addr=%p", notit
);
3087 BT_LOGD_STR("Destroying field stack.");
3088 stack_destroy(notit
->stack
);
3092 BT_LOGD("Destroying BTR: btr-addr=%p", notit
->btr
);
3093 bt_btr_destroy(notit
->btr
);
3096 if (notit
->clock_states
) {
3097 g_hash_table_destroy(notit
->clock_states
);
3100 if (notit
->sc_field_path_caches
) {
3101 g_hash_table_destroy(notit
->sc_field_path_caches
);
3104 if (notit
->field_overrides
) {
3105 g_hash_table_destroy(notit
->field_overrides
);
3111 enum bt_notif_iter_status
bt_notif_iter_get_next_notification(
3112 struct bt_notif_iter
*notit
,
3113 struct bt_clock_class_priority_map
*cc_prio_map
,
3114 struct bt_notification
**notification
)
3116 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
3119 assert(notification
);
3121 BT_LOGV("Getting next notification: notit-addr=%p, cc-prio-map-addr=%p",
3122 notit
, cc_prio_map
);
3125 status
= handle_state(notit
);
3126 if (status
== BT_NOTIF_ITER_STATUS_AGAIN
) {
3127 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_AGAIN.");
3130 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
3131 if (status
== BT_NOTIF_ITER_STATUS_EOF
) {
3132 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_EOF.");
3134 BT_LOGW("Cannot handle state: "
3135 "notit-addr=%p, state=%s",
3136 notit
, state_string(notit
->state
));
3141 switch (notit
->state
) {
3142 case STATE_EMIT_NOTIF_NEW_PACKET
:
3143 /* notify_new_packet() logs errors */
3144 notify_new_packet(notit
, notification
);
3145 if (!*notification
) {
3146 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3149 case STATE_EMIT_NOTIF_EVENT
:
3150 /* notify_event() logs errors */
3151 notify_event(notit
, cc_prio_map
, notification
);
3152 if (!*notification
) {
3153 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3156 case STATE_EMIT_NOTIF_END_OF_PACKET
:
3157 /* Update clock with timestamp_end field. */
3158 if (notit
->cur_timestamp_end
) {
3159 enum bt_btr_status btr_status
;
3160 struct bt_field_type
*field_type
=
3162 notit
->cur_timestamp_end
);
3165 btr_status
= update_clock(notit
,
3166 notit
->cur_timestamp_end
);
3168 if (btr_status
!= BT_BTR_STATUS_OK
) {
3169 BT_LOGW("Cannot update stream's clock value: "
3170 "notit-addr=%p", notit
);
3171 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3176 /* notify_end_of_packet() logs errors */
3177 notify_end_of_packet(notit
, notification
);
3178 if (!*notification
) {
3179 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3183 /* Non-emitting state: continue */
3193 enum bt_notif_iter_status
bt_notif_iter_get_packet_header_context_fields(
3194 struct bt_notif_iter
*notit
,
3195 struct bt_field
**packet_header_field
,
3196 struct bt_field
**packet_context_field
)
3199 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
3203 if (notit
->state
== STATE_EMIT_NOTIF_NEW_PACKET
) {
3204 /* We're already there */
3209 status
= handle_state(notit
);
3210 if (status
== BT_NOTIF_ITER_STATUS_AGAIN
) {
3211 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_AGAIN.");
3214 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
3215 if (status
== BT_NOTIF_ITER_STATUS_EOF
) {
3216 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_EOF.");
3218 BT_LOGW("Cannot handle state: "
3219 "notit-addr=%p, state=%s",
3220 notit
, state_string(notit
->state
));
3225 switch (notit
->state
) {
3226 case STATE_EMIT_NOTIF_NEW_PACKET
:
3228 * Packet header and context fields are
3229 * potentially decoded (or they don't exist).
3233 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
3234 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
3235 case STATE_AFTER_TRACE_PACKET_HEADER
:
3236 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
3237 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
3238 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
3239 /* Non-emitting state: continue */
3243 * We should never get past the
3244 * STATE_EMIT_NOTIF_NEW_PACKET state.
3246 BT_LOGF("Unexpected state: notit-addr=%p, state=%s",
3247 notit
, state_string(notit
->state
));
3253 if (packet_header_field
) {
3254 *packet_header_field
= bt_get(notit
->dscopes
.trace_packet_header
);
3257 if (packet_context_field
) {
3258 *packet_context_field
= bt_get(notit
->dscopes
.stream_packet_context
);
3261 ret
= set_current_packet_content_sizes(notit
);
3263 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3271 void bt_notif_iter_set_medops_data(struct bt_notif_iter
*notit
,
3275 notit
->medium
.data
= medops_data
;
3279 enum bt_notif_iter_status
bt_notif_iter_seek(
3280 struct bt_notif_iter
*notit
, off_t offset
)
3282 enum bt_notif_iter_status ret
= BT_NOTIF_ITER_STATUS_OK
;
3283 enum bt_notif_iter_medium_status medium_status
;
3287 BT_LOGE("Cannot seek to negative offset: offset=%jd", offset
);
3288 ret
= BT_NOTIF_ITER_STATUS_INVAL
;
3292 if (!notit
->medium
.medops
.seek
) {
3293 ret
= BT_NOTIF_ITER_STATUS_UNSUPPORTED
;
3294 BT_LOGD("Aborting seek as the iterator's underlying media does not implement seek support.");
3298 medium_status
= notit
->medium
.medops
.seek(
3299 BT_NOTIF_ITER_SEEK_WHENCE_SET
, offset
,
3300 notit
->medium
.data
);
3301 if (medium_status
!= BT_NOTIF_ITER_MEDIUM_STATUS_OK
) {
3302 if (medium_status
== BT_NOTIF_ITER_MEDIUM_STATUS_EOF
) {
3303 ret
= BT_NOTIF_ITER_STATUS_EOF
;
3305 ret
= BT_NOTIF_ITER_STATUS_ERROR
;
3310 bt_notif_iter_reset(notit
);
3311 notit
->cur_packet_offset
= offset
;
3317 off_t
bt_notif_iter_get_current_packet_offset(
3318 struct bt_notif_iter
*notit
)
3321 return notit
->cur_packet_offset
;
3325 off_t
bt_notif_iter_get_current_packet_size(
3326 struct bt_notif_iter
*notit
)
3329 return notit
->cur_packet_size
;