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
) {
492 struct bt_field_type
*ph_ft
=
493 bt_trace_get_packet_header_type(notit
->meta
.trace
);
494 struct bt_field_type
*eh_ft
= NULL
;
495 struct bt_field_type
*sec_ft
= NULL
;
496 struct bt_field_type
*ec_ft
= NULL
;
499 * User returned end of stream: validate that we're not
500 * in the middle of a packet header, packet context, or
503 if (notit
->state
== STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
) {
504 /* Beginning of packet: always valid */
508 if (!notit
->meta
.stream_class
) {
512 if (notit
->state
== STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
) {
514 * Beginning of packet context context is only
515 * valid if there's no packet header.
522 eh_ft
= bt_stream_class_get_event_header_type(
523 notit
->meta
.stream_class
);
524 sec_ft
= bt_stream_class_get_event_context_type(
525 notit
->meta
.stream_class
);
527 if (notit
->state
== STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
) {
529 * Beginning of event's header is only valid if
530 * the packet is not supposed to have a specific
531 * size (whole packet sequence has in fact only
534 if (notit
->cur_packet_size
== -1) {
539 if (notit
->state
== STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
) {
541 * Beginning of event's stream event context is
542 * only valid if the packet is not supposed to
543 * have a specific size (whole packet sequence
544 * has in fact only one packet), and there's no
547 if (notit
->cur_packet_size
== -1 && !eh_ft
) {
552 if (!notit
->meta
.event_class
) {
556 ec_ft
= bt_event_class_get_context_type(
557 notit
->meta
.event_class
);
559 if (notit
->state
== STATE_DSCOPE_EVENT_CONTEXT_BEGIN
) {
561 * Beginning of event's context is only valid if
562 * the packet is not supposed to have a specific
563 * size (whole packet sequence has in fact only
564 * one packet), and there's no event header and
565 * no stream event context.
567 if (notit
->cur_packet_size
== -1 && !eh_ft
&& !sec_ft
) {
572 if (notit
->state
== STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
) {
574 * Beginning of event's context is only valid if
575 * the packet is not supposed to have a specific
576 * size (whole packet sequence has in fact only
577 * one packet), and there's no event header, no
578 * stream event context, and no event context.
580 if (notit
->cur_packet_size
== -1 && !eh_ft
&& !sec_ft
&&
587 /* All other states are invalid */
588 BT_LOGW("User function returned %s, but notification iterator is in an unexpected state: "
590 bt_notif_iter_medium_status_string(m_status
),
591 state_string(notit
->state
));
592 m_status
= BT_NOTIF_ITER_MEDIUM_STATUS_ERROR
;
599 } else if (m_status
< 0) {
600 BT_LOGW("User function failed: status=%s",
601 bt_notif_iter_medium_status_string(m_status
));
604 return notif_iter_status_from_m_status(m_status
);
608 enum bt_notif_iter_status
buf_ensure_available_bits(
609 struct bt_notif_iter
*notit
)
611 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
613 if (buf_available_bits(notit
) == 0) {
615 * This _cannot_ return BT_NOTIF_ITER_STATUS_OK
618 status
= request_medium_bytes(notit
);
625 enum bt_notif_iter_status
read_dscope_begin_state(
626 struct bt_notif_iter
*notit
,
627 struct bt_field_type
*dscope_field_type
,
628 enum state done_state
, enum state continue_state
,
629 struct bt_field
**dscope_field
)
631 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
632 enum bt_btr_status btr_status
;
633 size_t consumed_bits
;
635 status
= buf_ensure_available_bits(notit
);
636 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
638 BT_LOGW("Cannot ensure that buffer has at least one byte: "
639 "notif-addr=%p, status=%s",
640 notit
, bt_notif_iter_status_string(status
));
642 BT_LOGV("Cannot ensure that buffer has at least one byte: "
643 "notif-addr=%p, status=%s",
644 notit
, bt_notif_iter_status_string(status
));
650 bt_put(*dscope_field
);
651 notit
->cur_dscope_field
= dscope_field
;
652 BT_LOGV("Starting BTR: notit-addr=%p, btr-addr=%p, ft-addr=%p",
653 notit
, notit
->btr
, dscope_field_type
);
654 consumed_bits
= bt_btr_start(notit
->btr
, dscope_field_type
,
655 notit
->buf
.addr
, notit
->buf
.at
, packet_at(notit
),
656 notit
->buf
.sz
, &btr_status
);
657 BT_LOGV("BTR consumed bits: size=%zu", consumed_bits
);
659 switch (btr_status
) {
660 case BT_BTR_STATUS_OK
:
661 /* type was read completely */
662 BT_LOGV_STR("Field was completely decoded.");
663 notit
->state
= done_state
;
665 case BT_BTR_STATUS_EOF
:
666 BT_LOGV_STR("BTR needs more data to decode field completely.");
667 notit
->state
= continue_state
;
670 BT_LOGW("BTR failed to start: notit-addr=%p, btr-addr=%p, "
671 "status=%s", notit
, notit
->btr
,
672 bt_btr_status_string(btr_status
));
673 status
= BT_NOTIF_ITER_STATUS_ERROR
;
677 /* Consume bits now since we know we're not in an error state */
678 buf_consume_bits(notit
, consumed_bits
);
685 enum bt_notif_iter_status
read_dscope_continue_state(
686 struct bt_notif_iter
*notit
, enum state done_state
)
688 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
689 enum bt_btr_status btr_status
;
690 size_t consumed_bits
;
692 status
= buf_ensure_available_bits(notit
);
693 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
695 BT_LOGW("Cannot ensure that buffer has at least one byte: "
696 "notif-addr=%p, status=%s",
697 notit
, bt_notif_iter_status_string(status
));
699 BT_LOGV("Cannot ensure that buffer has at least one byte: "
700 "notif-addr=%p, status=%s",
701 notit
, bt_notif_iter_status_string(status
));
707 BT_LOGV("Continuing BTR: notit-addr=%p, btr-addr=%p",
709 consumed_bits
= bt_btr_continue(notit
->btr
, notit
->buf
.addr
,
710 notit
->buf
.sz
, &btr_status
);
711 BT_LOGV("BTR consumed bits: size=%zu", consumed_bits
);
713 switch (btr_status
) {
714 case BT_BTR_STATUS_OK
:
715 /* Type was read completely. */
716 BT_LOGV_STR("Field was completely decoded.");
717 notit
->state
= done_state
;
719 case BT_BTR_STATUS_EOF
:
720 /* Stay in this continue state. */
721 BT_LOGV_STR("BTR needs more data to decode field completely.");
724 BT_LOGW("BTR failed to continue: notit-addr=%p, btr-addr=%p, "
725 "status=%s", notit
, notit
->btr
,
726 bt_btr_status_string(btr_status
));
727 status
= BT_NOTIF_ITER_STATUS_ERROR
;
731 /* Consume bits now since we know we're not in an error state. */
732 buf_consume_bits(notit
, consumed_bits
);
738 void put_event_dscopes(struct bt_notif_iter
*notit
)
740 BT_LOGV_STR("Putting event header field.");
741 BT_PUT(notit
->dscopes
.stream_event_header
);
742 BT_LOGV_STR("Putting stream event context field.");
743 BT_PUT(notit
->dscopes
.stream_event_context
);
744 BT_LOGV_STR("Putting event context field.");
745 BT_PUT(notit
->dscopes
.event_context
);
746 BT_LOGV_STR("Putting event payload field.");
747 BT_PUT(notit
->dscopes
.event_payload
);
751 void put_all_dscopes(struct bt_notif_iter
*notit
)
753 BT_LOGV_STR("Putting packet header field.");
754 BT_PUT(notit
->dscopes
.trace_packet_header
);
755 BT_LOGV_STR("Putting packet context field.");
756 BT_PUT(notit
->dscopes
.stream_packet_context
);
757 put_event_dscopes(notit
);
761 enum bt_notif_iter_status
read_packet_header_begin_state(
762 struct bt_notif_iter
*notit
)
764 struct bt_field_type
*packet_header_type
= NULL
;
765 enum bt_notif_iter_status ret
= BT_NOTIF_ITER_STATUS_OK
;
767 if (bt_notif_iter_switch_packet(notit
)) {
768 BT_LOGW("Cannot switch packet: notit-addr=%p", notit
);
769 ret
= BT_NOTIF_ITER_STATUS_ERROR
;
773 /* Packet header type is common to the whole trace. */
774 packet_header_type
= bt_trace_get_packet_header_type(
776 if (!packet_header_type
) {
777 notit
->state
= STATE_AFTER_TRACE_PACKET_HEADER
;
781 BT_LOGV("Decoding packet header field:"
782 "notit-addr=%p, trace-addr=%p, trace-name=\"%s\", ft-addr=%p",
783 notit
, notit
->meta
.trace
,
784 bt_trace_get_name(notit
->meta
.trace
), packet_header_type
);
785 ret
= read_dscope_begin_state(notit
, packet_header_type
,
786 STATE_AFTER_TRACE_PACKET_HEADER
,
787 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
788 ¬it
->dscopes
.trace_packet_header
);
790 BT_LOGW("Cannot decode packet header field: "
791 "notit-addr=%p, trace-addr=%p, "
792 "trace-name=\"%s\", ft-addr=%p",
793 notit
, notit
->meta
.trace
,
794 bt_trace_get_name(notit
->meta
.trace
),
798 BT_PUT(packet_header_type
);
803 enum bt_notif_iter_status
read_packet_header_continue_state(
804 struct bt_notif_iter
*notit
)
806 return read_dscope_continue_state(notit
,
807 STATE_AFTER_TRACE_PACKET_HEADER
);
811 bool is_struct_type(struct bt_field_type
*field_type
)
813 return bt_field_type_get_type_id(field_type
) ==
814 BT_FIELD_TYPE_ID_STRUCT
;
818 bool is_variant_type(struct bt_field_type
*field_type
)
820 return bt_field_type_get_type_id(field_type
) ==
821 BT_FIELD_TYPE_ID_VARIANT
;
825 struct stream_class_field_path_cache
*
826 create_stream_class_field_path_cache_entry(
827 struct bt_notif_iter
*notit
,
828 struct bt_stream_class
*stream_class
)
832 int timestamp_end
= -1;
833 int packet_size
= -1;
834 int content_size
= -1;
835 struct stream_class_field_path_cache
*cache_entry
= g_new0(
836 struct stream_class_field_path_cache
, 1);
837 struct bt_field_type
*event_header
= NULL
, *packet_context
= NULL
;
840 BT_LOGE_STR("Failed to allocate one stream class field path cache.");
844 event_header
= bt_stream_class_get_event_header_type(stream_class
);
845 if (event_header
&& bt_field_type_is_structure(event_header
)) {
848 count
= bt_field_type_structure_get_field_count(
852 for (i
= 0; i
< count
; i
++) {
856 ret
= bt_field_type_structure_get_field_by_index(
857 event_header
, &name
, NULL
, i
);
859 BT_LOGE("Cannot get event header structure field type's field: "
860 "notit-addr=%p, stream-class-addr=%p, "
861 "stream-class-name=\"%s\", "
862 "stream-class-id=%" PRId64
", "
863 "ft-addr=%p, index=%d",
865 bt_stream_class_get_name(stream_class
),
866 bt_stream_class_get_id(stream_class
),
871 if (v
!= -1 && id
!= -1) {
875 if (v
== -1 && strcmp(name
, "v") == 0) {
877 } else if (id
== -1 && !strcmp(name
, "id")) {
883 packet_context
= bt_stream_class_get_packet_context_type(
885 if (packet_context
&& bt_field_type_is_structure(packet_context
)) {
888 count
= bt_field_type_structure_get_field_count(
892 for (i
= 0; i
< count
; i
++) {
895 struct bt_field_type
*field_type
;
897 if (timestamp_end
!= -1 && packet_size
!= -1 &&
898 content_size
!= -1) {
902 ret
= bt_field_type_structure_get_field_by_index(
903 packet_context
, &name
, &field_type
, i
);
905 BT_LOGE("Cannot get packet context structure field type's field: "
906 "notit-addr=%p, stream-class-addr=%p, "
907 "stream-class-name=\"%s\", "
908 "stream-class-id=%" PRId64
", "
909 "ft-addr=%p, index=%d",
911 bt_stream_class_get_name(stream_class
),
912 bt_stream_class_get_id(stream_class
),
917 if (timestamp_end
== -1 &&
918 strcmp(name
, "timestamp_end") == 0) {
919 struct field_cb_override
*override
= g_new0(
920 struct field_cb_override
, 1);
927 override
->func
= btr_timestamp_end_cb
;
928 override
->data
= notit
;
929 g_hash_table_insert(notit
->field_overrides
,
930 bt_get(field_type
), override
);
932 } else if (packet_size
== -1 &&
933 !strcmp(name
, "packet_size")) {
935 } else if (content_size
== -1 &&
936 !strcmp(name
, "content_size")) {
944 cache_entry
->id
= id
;
945 cache_entry
->timestamp_end
= timestamp_end
;
946 cache_entry
->packet_size
= packet_size
;
947 cache_entry
->content_size
= content_size
;
949 BT_PUT(event_header
);
950 BT_PUT(packet_context
);
959 struct stream_class_field_path_cache
*get_stream_class_field_path_cache(
960 struct bt_notif_iter
*notit
,
961 struct bt_stream_class
*stream_class
)
963 bool cache_entry_found
;
964 struct stream_class_field_path_cache
*cache_entry
;
966 cache_entry_found
= g_hash_table_lookup_extended(
967 notit
->sc_field_path_caches
,
968 stream_class
, NULL
, (gpointer
) &cache_entry
);
969 if (unlikely(!cache_entry_found
)) {
970 cache_entry
= create_stream_class_field_path_cache_entry(notit
,
972 g_hash_table_insert(notit
->sc_field_path_caches
,
973 bt_get(stream_class
), (gpointer
) cache_entry
);
980 enum bt_notif_iter_status
set_current_stream_class(
981 struct bt_notif_iter
*notit
)
983 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
984 struct bt_field_type
*packet_header_type
= NULL
;
985 struct bt_field_type
*stream_id_field_type
= NULL
;
986 struct bt_stream_class
*new_stream_class
= NULL
;
989 /* Clear the current stream class field path cache. */
990 notit
->cur_sc_field_path_cache
= NULL
;
992 /* Is there any "stream_id" field in the packet header? */
993 packet_header_type
= bt_trace_get_packet_header_type(
995 if (!packet_header_type
) {
997 * No packet header, therefore no `stream_id` field,
998 * therefore only one stream class.
1000 goto single_stream_class
;
1003 assert(is_struct_type(packet_header_type
));
1005 // TODO: optimalize!
1006 stream_id_field_type
=
1007 bt_field_type_structure_get_field_type_by_name(
1008 packet_header_type
, "stream_id");
1009 if (stream_id_field_type
) {
1010 /* Find appropriate stream class using current stream ID */
1012 struct bt_field
*stream_id_field
= NULL
;
1014 assert(notit
->dscopes
.trace_packet_header
);
1016 // TODO: optimalize!
1017 stream_id_field
= bt_field_structure_get_field_by_name(
1018 notit
->dscopes
.trace_packet_header
, "stream_id");
1019 assert(stream_id_field
);
1020 ret
= bt_field_unsigned_integer_get_value(
1021 stream_id_field
, &stream_id
);
1023 BT_PUT(stream_id_field
);
1025 single_stream_class
:
1026 /* Only one stream: pick the first stream class */
1027 assert(bt_trace_get_stream_class_count(
1028 notit
->meta
.trace
) == 1);
1032 BT_LOGV("Found stream class ID to use: notit-addr=%p, "
1033 "stream-class-id=%" PRIu64
", "
1034 "trace-addr=%p, trace-name=\"%s\"",
1035 notit
, stream_id
, notit
->meta
.trace
,
1036 bt_trace_get_name(notit
->meta
.trace
));
1038 new_stream_class
= bt_trace_get_stream_class_by_id(
1039 notit
->meta
.trace
, stream_id
);
1040 if (!new_stream_class
) {
1041 BT_LOGW("No stream class with ID of stream class ID to use in trace: "
1042 "notit-addr=%p, stream-class-id=%" PRIu64
", "
1043 "trace-addr=%p, trace-name=\"%s\"",
1044 notit
, stream_id
, notit
->meta
.trace
,
1045 bt_trace_get_name(notit
->meta
.trace
));
1046 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1050 if (notit
->meta
.stream_class
) {
1051 if (new_stream_class
!= notit
->meta
.stream_class
) {
1052 BT_LOGW("Two packets refer to two different stream classes within the same packet sequence: "
1053 "notit-addr=%p, prev-stream-class-addr=%p, "
1054 "prev-stream-class-name=\"%s\", "
1055 "prev-stream-class-id=%" PRId64
", "
1056 "next-stream-class-addr=%p, "
1057 "next-stream-class-name=\"%s\", "
1058 "next-stream-class-id=%" PRId64
", "
1059 "trace-addr=%p, trace-name=\"%s\"",
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
),
1064 bt_stream_class_get_name(new_stream_class
),
1065 bt_stream_class_get_id(new_stream_class
),
1067 bt_trace_get_name(notit
->meta
.trace
));
1068 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1072 BT_MOVE(notit
->meta
.stream_class
, new_stream_class
);
1075 BT_LOGV("Set current stream class: "
1076 "notit-addr=%p, stream-class-addr=%p, "
1077 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
1078 notit
, notit
->meta
.stream_class
,
1079 bt_stream_class_get_name(notit
->meta
.stream_class
),
1080 bt_stream_class_get_id(notit
->meta
.stream_class
));
1083 * Retrieve (or lazily create) the current stream class field path
1086 notit
->cur_sc_field_path_cache
= get_stream_class_field_path_cache(
1087 notit
, notit
->meta
.stream_class
);
1088 if (!notit
->cur_sc_field_path_cache
) {
1089 BT_LOGW("Cannot retrieve stream class field path from cache: "
1090 "notit-addr=%p, stream-class-addr=%p, "
1091 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
1092 notit
, notit
->meta
.stream_class
,
1093 bt_stream_class_get_name(notit
->meta
.stream_class
),
1094 bt_stream_class_get_id(notit
->meta
.stream_class
));
1095 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1099 BT_PUT(packet_header_type
);
1100 BT_PUT(stream_id_field_type
);
1101 bt_put(new_stream_class
);
1106 enum bt_notif_iter_status
after_packet_header_state(
1107 struct bt_notif_iter
*notit
)
1109 enum bt_notif_iter_status status
;
1111 status
= set_current_stream_class(notit
);
1112 if (status
== BT_NOTIF_ITER_STATUS_OK
) {
1113 notit
->state
= STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
;
1120 enum bt_notif_iter_status
read_packet_context_begin_state(
1121 struct bt_notif_iter
*notit
)
1123 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1124 struct bt_field_type
*packet_context_type
;
1126 assert(notit
->meta
.stream_class
);
1127 packet_context_type
= bt_stream_class_get_packet_context_type(
1128 notit
->meta
.stream_class
);
1129 if (!packet_context_type
) {
1130 BT_LOGV("No packet packet context field type in stream class: continuing: "
1131 "notit-addr=%p, stream-class-addr=%p, "
1132 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
1133 notit
, notit
->meta
.stream_class
,
1134 bt_stream_class_get_name(notit
->meta
.stream_class
),
1135 bt_stream_class_get_id(notit
->meta
.stream_class
));
1136 notit
->state
= STATE_AFTER_STREAM_PACKET_CONTEXT
;
1140 BT_LOGV("Decoding packet context field: "
1141 "notit-addr=%p, stream-class-addr=%p, "
1142 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1144 notit
, notit
->meta
.stream_class
,
1145 bt_stream_class_get_name(notit
->meta
.stream_class
),
1146 bt_stream_class_get_id(notit
->meta
.stream_class
),
1147 packet_context_type
);
1148 status
= read_dscope_begin_state(notit
, packet_context_type
,
1149 STATE_AFTER_STREAM_PACKET_CONTEXT
,
1150 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
1151 ¬it
->dscopes
.stream_packet_context
);
1153 BT_LOGW("Cannot decode packet context field: "
1154 "notit-addr=%p, stream-class-addr=%p, "
1155 "stream-class-name=\"%s\", "
1156 "stream-class-id=%" PRId64
", ft-addr=%p",
1157 notit
, notit
->meta
.stream_class
,
1158 bt_stream_class_get_name(notit
->meta
.stream_class
),
1159 bt_stream_class_get_id(notit
->meta
.stream_class
),
1160 packet_context_type
);
1164 BT_PUT(packet_context_type
);
1169 enum bt_notif_iter_status
read_packet_context_continue_state(
1170 struct bt_notif_iter
*notit
)
1172 return read_dscope_continue_state(notit
,
1173 STATE_AFTER_STREAM_PACKET_CONTEXT
);
1177 enum bt_notif_iter_status
set_current_packet_content_sizes(
1178 struct bt_notif_iter
*notit
)
1180 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1181 struct bt_field
*packet_size_field
= NULL
;
1182 struct bt_field
*content_size_field
= NULL
;
1183 uint64_t content_size
= -1ULL, packet_size
= -1ULL;
1185 if (!notit
->dscopes
.stream_packet_context
) {
1189 packet_size_field
= bt_field_structure_get_field_by_name(
1190 notit
->dscopes
.stream_packet_context
, "packet_size");
1191 content_size_field
= bt_field_structure_get_field_by_name(
1192 notit
->dscopes
.stream_packet_context
, "content_size");
1193 if (packet_size_field
) {
1194 int ret
= bt_field_unsigned_integer_get_value(
1195 packet_size_field
, &packet_size
);
1198 if (packet_size
== 0) {
1199 BT_LOGW("Invalid packet size: packet context field indicates packet size is zero: "
1200 "notit-addr=%p, packet-context-field-addr=%p",
1201 notit
, notit
->dscopes
.stream_packet_context
);
1202 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1204 } else if ((packet_size
% 8) != 0) {
1205 BT_LOGW("Invalid packet size: packet context field indicates packet size is not a multiple of 8: "
1206 "notit-addr=%p, packet-context-field-addr=%p, "
1207 "packet-size=%" PRIu64
,
1208 notit
, notit
->dscopes
.stream_packet_context
,
1210 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1215 if (content_size_field
) {
1216 int ret
= bt_field_unsigned_integer_get_value(
1217 content_size_field
, &content_size
);
1221 content_size
= packet_size
;
1224 if (content_size
> packet_size
) {
1225 BT_LOGW("Invalid packet or content size: packet context field indicates content size is greater than packet size: "
1226 "notit-addr=%p, packet-context-field-addr=%p, "
1227 "packet-size=%" PRIu64
", content-size=%" PRIu64
,
1228 notit
, notit
->dscopes
.stream_packet_context
,
1229 packet_size
, content_size
);
1230 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1234 if (packet_size
!= -1ULL) {
1235 notit
->cur_packet_size
= packet_size
;
1238 * Use the content size as packet size indicator if the
1239 * packet size field is missing. This means there is no
1240 * padding in this stream.
1242 notit
->cur_packet_size
= content_size
;
1244 notit
->cur_content_size
= content_size
;
1245 BT_LOGV("Set current packet and content sizes: "
1246 "notit-addr=%p, packet-size=%" PRIu64
", content-size=%" PRIu64
,
1247 notit
, packet_size
, content_size
);
1249 BT_PUT(packet_size_field
);
1250 BT_PUT(content_size_field
);
1255 enum bt_notif_iter_status
after_packet_context_state(
1256 struct bt_notif_iter
*notit
)
1258 enum bt_notif_iter_status status
;
1260 status
= set_current_packet_content_sizes(notit
);
1261 if (status
== BT_NOTIF_ITER_STATUS_OK
) {
1262 notit
->state
= STATE_EMIT_NOTIF_NEW_PACKET
;
1269 enum bt_notif_iter_status
read_event_header_begin_state(
1270 struct bt_notif_iter
*notit
)
1272 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1273 struct bt_field_type
*event_header_type
= NULL
;
1275 /* Reset the position of the last event header */
1276 notit
->buf
.last_eh_at
= notit
->buf
.at
;
1278 /* Check if we have some content left */
1279 if (notit
->cur_content_size
>= 0) {
1280 if (packet_at(notit
) == notit
->cur_content_size
) {
1281 /* No more events! */
1282 BT_LOGV("Reached end of packet: notit-addr=%p, "
1283 "cur=%zu", notit
, packet_at(notit
));
1284 notit
->state
= STATE_EMIT_NOTIF_END_OF_PACKET
;
1286 } else if (packet_at(notit
) > notit
->cur_content_size
) {
1287 /* That's not supposed to happen */
1288 BT_LOGV("Before decoding event header field: cursor is passed the packet's content: "
1289 "notit-addr=%p, content-size=%" PRId64
", "
1290 "cur=%zu", notit
, notit
->cur_content_size
,
1292 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1297 event_header_type
= bt_stream_class_get_event_header_type(
1298 notit
->meta
.stream_class
);
1299 if (!event_header_type
) {
1300 notit
->state
= STATE_AFTER_STREAM_EVENT_HEADER
;
1304 put_event_dscopes(notit
);
1305 BT_LOGV("Decoding event header field: "
1306 "notit-addr=%p, stream-class-addr=%p, "
1307 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1309 notit
, notit
->meta
.stream_class
,
1310 bt_stream_class_get_name(notit
->meta
.stream_class
),
1311 bt_stream_class_get_id(notit
->meta
.stream_class
),
1313 status
= read_dscope_begin_state(notit
, event_header_type
,
1314 STATE_AFTER_STREAM_EVENT_HEADER
,
1315 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
,
1316 ¬it
->dscopes
.stream_event_header
);
1318 BT_LOGW("Cannot decode event header field: "
1319 "notit-addr=%p, stream-class-addr=%p, "
1320 "stream-class-name=\"%s\", "
1321 "stream-class-id=%" PRId64
", ft-addr=%p",
1322 notit
, notit
->meta
.stream_class
,
1323 bt_stream_class_get_name(notit
->meta
.stream_class
),
1324 bt_stream_class_get_id(notit
->meta
.stream_class
),
1328 BT_PUT(event_header_type
);
1334 enum bt_notif_iter_status
read_event_header_continue_state(
1335 struct bt_notif_iter
*notit
)
1337 return read_dscope_continue_state(notit
,
1338 STATE_AFTER_STREAM_EVENT_HEADER
);
1342 enum bt_notif_iter_status
set_current_event_class(struct bt_notif_iter
*notit
)
1345 * The assert() calls in this function are okay because it is
1346 * assumed here that all the metadata objects have been
1347 * validated for CTF correctness before decoding actual streams.
1350 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1351 struct bt_field_type
*event_header_type
;
1352 struct bt_field_type
*id_field_type
= NULL
;
1353 struct bt_field_type
*v_field_type
= NULL
;
1354 uint64_t event_id
= -1ULL;
1357 event_header_type
= bt_stream_class_get_event_header_type(
1358 notit
->meta
.stream_class
);
1359 if (!event_header_type
) {
1361 * No event header, therefore no event class ID field,
1362 * therefore only one event class.
1364 goto single_event_class
;
1367 /* Is there any "id"/"v" field in the event header? */
1368 assert(is_struct_type(event_header_type
));
1369 id_field_type
= bt_field_type_structure_get_field_type_by_name(
1370 event_header_type
, "id");
1371 v_field_type
= bt_field_type_structure_get_field_type_by_name(
1372 event_header_type
, "v");
1373 assert(notit
->dscopes
.stream_event_header
);
1377 * | | |_ _|_ _| __ __ _
1378 * | | | | | || '_ \ / _` |
1379 * | |___| | | || | | | (_| | S P E C I A L
1380 * |_____|_| |_||_| |_|\__, | C A S E â„¢
1383 struct bt_field
*v_field
= NULL
;
1384 struct bt_field
*v_struct_field
= NULL
;
1385 struct bt_field
*v_struct_id_field
= NULL
;
1387 // TODO: optimalize!
1388 v_field
= bt_field_structure_get_field_by_name(
1389 notit
->dscopes
.stream_event_header
, "v");
1393 bt_field_variant_get_current_field(v_field
);
1394 if (!v_struct_field
) {
1395 goto end_v_field_type
;
1398 // TODO: optimalize!
1400 bt_field_structure_get_field_by_name(v_struct_field
, "id");
1401 if (!v_struct_id_field
) {
1402 goto end_v_field_type
;
1405 if (bt_field_is_integer(v_struct_id_field
)) {
1406 ret
= bt_field_unsigned_integer_get_value(
1407 v_struct_id_field
, &event_id
);
1409 BT_LOGV("Cannot get value of unsigned integer field (`id`): continuing: "
1410 "notit=%p, field-addr=%p",
1411 notit
, v_struct_id_field
);
1418 BT_PUT(v_struct_field
);
1419 BT_PUT(v_struct_id_field
);
1422 if (id_field_type
&& event_id
== -1ULL) {
1423 /* Check "id" field */
1424 struct bt_field
*id_field
= NULL
;
1425 int ret_get_value
= 0;
1427 // TODO: optimalize!
1428 id_field
= bt_field_structure_get_field_by_name(
1429 notit
->dscopes
.stream_event_header
, "id");
1431 goto check_event_id
;
1434 if (bt_field_is_integer(id_field
)) {
1435 ret_get_value
= bt_field_unsigned_integer_get_value(
1436 id_field
, &event_id
);
1437 } else if (bt_field_is_enumeration(id_field
)) {
1438 struct bt_field
*container
;
1440 container
= bt_field_enumeration_get_container(
1443 ret_get_value
= bt_field_unsigned_integer_get_value(
1444 container
, &event_id
);
1448 assert(ret_get_value
== 0);
1453 if (event_id
== -1ULL) {
1455 /* Event ID not found: single event? */
1456 assert(bt_stream_class_get_event_class_count(
1457 notit
->meta
.stream_class
) == 1);
1461 BT_LOGV("Found event class ID to use: notit-addr=%p, "
1462 "stream-class-addr=%p, stream-class-name=\"%s\", "
1463 "stream-class-id=%" PRId64
", "
1464 "event-class-id=%" PRIu64
,
1465 notit
, notit
->meta
.stream_class
,
1466 bt_stream_class_get_name(notit
->meta
.stream_class
),
1467 bt_stream_class_get_id(notit
->meta
.stream_class
),
1469 BT_PUT(notit
->meta
.event_class
);
1470 notit
->meta
.event_class
= bt_stream_class_get_event_class_by_id(
1471 notit
->meta
.stream_class
, event_id
);
1472 if (!notit
->meta
.event_class
) {
1473 BT_LOGW("No event class with ID of event class ID to use in stream class: "
1474 "notit-addr=%p, stream-class-addr=%p, "
1475 "stream-class-name=\"%s\", "
1476 "stream-class-id=%" PRId64
", "
1477 "event-class-id=%" PRIu64
,
1478 notit
, notit
->meta
.stream_class
,
1479 bt_stream_class_get_name(notit
->meta
.stream_class
),
1480 bt_stream_class_get_id(notit
->meta
.stream_class
),
1482 status
= BT_NOTIF_ITER_STATUS_ERROR
;
1486 BT_LOGV("Set current event class: "
1487 "notit-addr=%p, event-class-addr=%p, "
1488 "event-class-name=\"%s\", event-class-id=%" PRId64
,
1489 notit
, notit
->meta
.event_class
,
1490 bt_event_class_get_name(notit
->meta
.event_class
),
1491 bt_event_class_get_id(notit
->meta
.event_class
));
1494 BT_PUT(event_header_type
);
1495 BT_PUT(id_field_type
);
1496 BT_PUT(v_field_type
);
1502 enum bt_notif_iter_status
after_event_header_state(
1503 struct bt_notif_iter
*notit
)
1505 enum bt_notif_iter_status status
;
1507 status
= set_current_event_class(notit
);
1508 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
1512 notit
->state
= STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
;
1519 enum bt_notif_iter_status
read_stream_event_context_begin_state(
1520 struct bt_notif_iter
*notit
)
1522 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1523 struct bt_field_type
*stream_event_context_type
;
1525 stream_event_context_type
= bt_stream_class_get_event_context_type(
1526 notit
->meta
.stream_class
);
1527 if (!stream_event_context_type
) {
1528 notit
->state
= STATE_DSCOPE_EVENT_CONTEXT_BEGIN
;
1532 BT_LOGV("Decoding stream event context field: "
1533 "notit-addr=%p, stream-class-addr=%p, "
1534 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1536 notit
, notit
->meta
.stream_class
,
1537 bt_stream_class_get_name(notit
->meta
.stream_class
),
1538 bt_stream_class_get_id(notit
->meta
.stream_class
),
1539 stream_event_context_type
);
1540 status
= read_dscope_begin_state(notit
, stream_event_context_type
,
1541 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
,
1542 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
,
1543 ¬it
->dscopes
.stream_event_context
);
1545 BT_LOGW("Cannot decode stream event context field: "
1546 "notit-addr=%p, stream-class-addr=%p, "
1547 "stream-class-name=\"%s\", "
1548 "stream-class-id=%" PRId64
", ft-addr=%p",
1549 notit
, notit
->meta
.stream_class
,
1550 bt_stream_class_get_name(notit
->meta
.stream_class
),
1551 bt_stream_class_get_id(notit
->meta
.stream_class
),
1552 stream_event_context_type
);
1556 BT_PUT(stream_event_context_type
);
1562 enum bt_notif_iter_status
read_stream_event_context_continue_state(
1563 struct bt_notif_iter
*notit
)
1565 return read_dscope_continue_state(notit
,
1566 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
);
1570 enum bt_notif_iter_status
read_event_context_begin_state(
1571 struct bt_notif_iter
*notit
)
1573 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1574 struct bt_field_type
*event_context_type
;
1576 event_context_type
= bt_event_class_get_context_type(
1577 notit
->meta
.event_class
);
1578 if (!event_context_type
) {
1579 notit
->state
= STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
;
1583 BT_LOGV("Decoding event context field: "
1584 "notit-addr=%p, event-class-addr=%p, "
1585 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1587 notit
, notit
->meta
.event_class
,
1588 bt_event_class_get_name(notit
->meta
.event_class
),
1589 bt_event_class_get_id(notit
->meta
.event_class
),
1590 event_context_type
);
1591 status
= read_dscope_begin_state(notit
, event_context_type
,
1592 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
1593 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
,
1594 ¬it
->dscopes
.event_context
);
1596 BT_LOGW("Cannot decode event context field: "
1597 "notit-addr=%p, event-class-addr=%p, "
1598 "event-class-name=\"%s\", "
1599 "event-class-id=%" PRId64
", ft-addr=%p",
1600 notit
, notit
->meta
.event_class
,
1601 bt_event_class_get_name(notit
->meta
.event_class
),
1602 bt_event_class_get_id(notit
->meta
.event_class
),
1603 event_context_type
);
1607 BT_PUT(event_context_type
);
1613 enum bt_notif_iter_status
read_event_context_continue_state(
1614 struct bt_notif_iter
*notit
)
1616 return read_dscope_continue_state(notit
,
1617 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
);
1621 enum bt_notif_iter_status
read_event_payload_begin_state(
1622 struct bt_notif_iter
*notit
)
1624 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1625 struct bt_field_type
*event_payload_type
;
1627 event_payload_type
= bt_event_class_get_payload_type(
1628 notit
->meta
.event_class
);
1629 if (!event_payload_type
) {
1630 notit
->state
= STATE_EMIT_NOTIF_EVENT
;
1634 BT_LOGV("Decoding event payload field: "
1635 "notit-addr=%p, event-class-addr=%p, "
1636 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1638 notit
, notit
->meta
.event_class
,
1639 bt_event_class_get_name(notit
->meta
.event_class
),
1640 bt_event_class_get_id(notit
->meta
.event_class
),
1641 event_payload_type
);
1642 status
= read_dscope_begin_state(notit
, event_payload_type
,
1643 STATE_EMIT_NOTIF_EVENT
,
1644 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
1645 ¬it
->dscopes
.event_payload
);
1647 BT_LOGW("Cannot decode event payload field: "
1648 "notit-addr=%p, event-class-addr=%p, "
1649 "event-class-name=\"%s\", "
1650 "event-class-id=%" PRId64
", ft-addr=%p",
1651 notit
, notit
->meta
.event_class
,
1652 bt_event_class_get_name(notit
->meta
.event_class
),
1653 bt_event_class_get_id(notit
->meta
.event_class
),
1654 event_payload_type
);
1658 BT_PUT(event_payload_type
);
1664 enum bt_notif_iter_status
read_event_payload_continue_state(
1665 struct bt_notif_iter
*notit
)
1667 return read_dscope_continue_state(notit
, STATE_EMIT_NOTIF_EVENT
);
1671 enum bt_notif_iter_status
skip_packet_padding_state(
1672 struct bt_notif_iter
*notit
)
1674 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1675 size_t bits_to_skip
;
1677 assert(notit
->cur_packet_size
> 0);
1678 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1679 if (bits_to_skip
== 0) {
1680 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1683 size_t bits_to_consume
;
1685 BT_LOGV("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu",
1686 bits_to_skip
, notit
, bits_to_skip
);
1687 status
= buf_ensure_available_bits(notit
);
1688 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
1692 bits_to_consume
= MIN(buf_available_bits(notit
), bits_to_skip
);
1693 BT_LOGV("Skipping %zu bits of padding: notit-addr=%p, size=%zu",
1694 bits_to_consume
, notit
, bits_to_consume
);
1695 buf_consume_bits(notit
, bits_to_consume
);
1696 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1697 if (bits_to_skip
== 0) {
1698 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1708 enum bt_notif_iter_status
handle_state(struct bt_notif_iter
*notit
)
1710 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
1711 const enum state state
= notit
->state
;
1713 BT_LOGV("Handling state: notit-addr=%p, state=%s",
1714 notit
, state_string(state
));
1716 // TODO: optimalize!
1719 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1721 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
1722 status
= read_packet_header_begin_state(notit
);
1724 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
1725 status
= read_packet_header_continue_state(notit
);
1727 case STATE_AFTER_TRACE_PACKET_HEADER
:
1728 status
= after_packet_header_state(notit
);
1730 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
1731 status
= read_packet_context_begin_state(notit
);
1733 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
1734 status
= read_packet_context_continue_state(notit
);
1736 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
1737 status
= after_packet_context_state(notit
);
1739 case STATE_EMIT_NOTIF_NEW_PACKET
:
1740 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1742 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
:
1743 status
= read_event_header_begin_state(notit
);
1745 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
:
1746 status
= read_event_header_continue_state(notit
);
1748 case STATE_AFTER_STREAM_EVENT_HEADER
:
1749 status
= after_event_header_state(notit
);
1751 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
:
1752 status
= read_stream_event_context_begin_state(notit
);
1754 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
:
1755 status
= read_stream_event_context_continue_state(notit
);
1757 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN
:
1758 status
= read_event_context_begin_state(notit
);
1760 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
:
1761 status
= read_event_context_continue_state(notit
);
1763 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
1764 status
= read_event_payload_begin_state(notit
);
1766 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
1767 status
= read_event_payload_continue_state(notit
);
1769 case STATE_EMIT_NOTIF_EVENT
:
1770 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1772 case STATE_SKIP_PACKET_PADDING
:
1773 status
= skip_packet_padding_state(notit
);
1775 case STATE_EMIT_NOTIF_END_OF_PACKET
:
1776 notit
->state
= STATE_SKIP_PACKET_PADDING
;
1779 BT_LOGD("Unknown CTF plugin notification iterator state: "
1780 "notit-addr=%p, state=%d", notit
, notit
->state
);
1784 BT_LOGV("Handled state: notit-addr=%p, status=%s, "
1785 "prev-state=%s, cur-state=%s",
1786 notit
, bt_notif_iter_status_string(status
),
1787 state_string(state
), state_string(notit
->state
));
1792 * Resets the internal state of a CTF notification iterator.
1795 void bt_notif_iter_reset(struct bt_notif_iter
*notit
)
1798 BT_LOGD("Resetting notification iterator: addr=%p", notit
);
1799 stack_clear(notit
->stack
);
1800 BT_PUT(notit
->meta
.stream_class
);
1801 BT_PUT(notit
->meta
.event_class
);
1802 BT_PUT(notit
->packet
);
1803 BT_PUT(notit
->stream
);
1804 put_all_dscopes(notit
);
1805 notit
->buf
.addr
= NULL
;
1808 notit
->buf
.last_eh_at
= SIZE_MAX
;
1809 notit
->buf
.packet_offset
= 0;
1810 notit
->state
= STATE_INIT
;
1811 notit
->cur_content_size
= -1;
1812 notit
->cur_packet_size
= -1;
1813 notit
->cur_packet_offset
= -1;
1817 int bt_notif_iter_switch_packet(struct bt_notif_iter
*notit
)
1822 * We don't put the stream class here because we need to make
1823 * sure that all the packets processed by the same notification
1824 * iterator refer to the same stream class (the first one).
1827 if (notit
->cur_packet_size
!= -1) {
1828 notit
->cur_packet_offset
+= notit
->cur_packet_size
;
1830 BT_LOGV("Switching packet: notit-addr=%p, cur=%zu, "
1831 "packet-offset=%" PRId64
, notit
, notit
->buf
.at
,
1832 notit
->cur_packet_offset
);
1833 stack_clear(notit
->stack
);
1834 BT_PUT(notit
->meta
.event_class
);
1835 BT_PUT(notit
->packet
);
1836 BT_PUT(notit
->cur_timestamp_end
);
1837 put_all_dscopes(notit
);
1840 * Adjust current buffer so that addr points to the beginning of the new
1843 if (notit
->buf
.addr
) {
1844 size_t consumed_bytes
= (size_t) (notit
->buf
.at
/ CHAR_BIT
);
1846 /* Packets are assumed to start on a byte frontier. */
1847 if (notit
->buf
.at
% CHAR_BIT
) {
1848 BT_LOGW("Cannot switch packet: current position is not a multiple of 8: "
1849 "notit-addr=%p, cur=%zu", notit
, notit
->buf
.at
);
1854 notit
->buf
.addr
+= consumed_bytes
;
1855 notit
->buf
.sz
-= consumed_bytes
;
1857 notit
->buf
.packet_offset
= 0;
1858 BT_LOGV("Adjusted buffer: addr=%p, size=%zu",
1859 notit
->buf
.addr
, notit
->buf
.sz
);
1862 notit
->cur_content_size
= -1;
1863 notit
->cur_packet_size
= -1;
1864 notit
->cur_sc_field_path_cache
= NULL
;
1871 struct bt_field
*get_next_field(struct bt_notif_iter
*notit
)
1873 struct bt_field
*next_field
= NULL
;
1874 struct bt_field
*base_field
;
1875 struct bt_field_type
*base_type
;
1878 assert(!stack_empty(notit
->stack
));
1879 index
= stack_top(notit
->stack
)->index
;
1880 base_field
= stack_top(notit
->stack
)->base
;
1882 base_type
= bt_field_get_type(base_field
);
1885 switch (bt_field_type_get_type_id(base_type
)) {
1886 case BT_FIELD_TYPE_ID_STRUCT
:
1887 next_field
= bt_field_structure_get_field_by_index(
1890 case BT_FIELD_TYPE_ID_ARRAY
:
1891 next_field
= bt_field_array_get_field(base_field
, index
);
1893 case BT_FIELD_TYPE_ID_SEQUENCE
:
1894 next_field
= bt_field_sequence_get_field(base_field
, index
);
1896 case BT_FIELD_TYPE_ID_VARIANT
:
1897 next_field
= bt_field_variant_get_current_field(base_field
);
1900 BT_LOGF("Unknown base field type ID: "
1901 "notit-addr=%p, ft-addr=%p, ft-id=%s",
1903 bt_field_type_id_string(
1904 bt_field_type_get_type_id(base_type
)));
1913 void update_clock_state(uint64_t *state
,
1914 struct bt_field
*value_field
)
1916 struct bt_field_type
*value_type
= NULL
;
1917 uint64_t requested_new_value
;
1918 uint64_t requested_new_value_mask
;
1919 uint64_t cur_value_masked
;
1920 int requested_new_value_size
;
1923 value_type
= bt_field_get_type(value_field
);
1925 assert(bt_field_type_is_integer(value_type
));
1926 requested_new_value_size
=
1927 bt_field_type_integer_get_size(value_type
);
1928 assert(requested_new_value_size
> 0);
1929 ret
= bt_field_unsigned_integer_get_value(value_field
,
1930 &requested_new_value
);
1934 * Special case for a 64-bit new value, which is the limit
1935 * of a clock value as of this version: overwrite the
1936 * current value directly.
1938 if (requested_new_value_size
== 64) {
1939 *state
= requested_new_value
;
1943 requested_new_value_mask
= (1ULL << requested_new_value_size
) - 1;
1944 cur_value_masked
= *state
& requested_new_value_mask
;
1946 if (requested_new_value
< cur_value_masked
) {
1948 * It looks like a wrap happened on the number of bits
1949 * of the requested new value. Assume that the clock
1950 * value wrapped only one time.
1952 *state
+= requested_new_value_mask
+ 1;
1955 /* Clear the low bits of the current clock value. */
1956 *state
&= ~requested_new_value_mask
;
1958 /* Set the low bits of the current clock value. */
1959 *state
|= requested_new_value
;
1961 BT_LOGV("Updated clock's value from integer field's value: "
1962 "value=%" PRIu64
, *state
);
1967 enum bt_btr_status
update_clock(struct bt_notif_iter
*notit
,
1968 struct bt_field
*int_field
)
1970 gboolean clock_class_found
;
1971 uint64_t *clock_state
= NULL
;
1972 struct bt_field_type
*int_field_type
= NULL
;
1973 enum bt_btr_status ret
= BT_BTR_STATUS_OK
;
1974 struct bt_clock_class
*clock_class
= NULL
;
1976 int_field_type
= bt_field_get_type(int_field
);
1977 assert(int_field_type
);
1978 clock_class
= bt_field_type_integer_get_mapped_clock_class(
1980 if (likely(!clock_class
)) {
1984 clock_class_found
= g_hash_table_lookup_extended(notit
->clock_states
,
1985 clock_class
, NULL
, (gpointer
) &clock_state
);
1986 if (!clock_class_found
) {
1987 clock_state
= g_new0(uint64_t, 1);
1989 BT_LOGE_STR("Failed to allocate a uint64_t.");
1990 ret
= BT_BTR_STATUS_ENOMEM
;
1994 g_hash_table_insert(notit
->clock_states
, bt_get(clock_class
),
1998 /* Update the clock's state. */
1999 BT_LOGV("Updating notification iterator's clock's value from integer field: "
2000 "notit-addr=%p, clock-class-addr=%p, "
2001 "clock-class-name=\"%s\", value=%" PRIu64
,
2003 bt_clock_class_get_name(clock_class
), *clock_state
);
2004 update_clock_state(clock_state
, int_field
);
2006 bt_put(int_field_type
);
2007 bt_put(clock_class
);
2012 enum bt_btr_status
btr_unsigned_int_common(uint64_t value
,
2013 struct bt_field_type
*type
, void *data
,
2014 struct bt_field
**out_int_field
)
2016 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2017 struct bt_field
*field
= NULL
;
2018 struct bt_field
*int_field
= NULL
;
2019 struct bt_notif_iter
*notit
= data
;
2022 BT_LOGV("Common unsigned integer function called from BTR: "
2023 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2024 "ft-id=%s, value=%" PRIu64
,
2025 notit
, notit
->btr
, type
,
2026 bt_field_type_id_string(
2027 bt_field_type_get_type_id(type
)),
2030 /* Create next field */
2031 field
= get_next_field(notit
);
2033 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2034 status
= BT_BTR_STATUS_ERROR
;
2038 switch(bt_field_type_get_type_id(type
)) {
2039 case BT_FIELD_TYPE_ID_INTEGER
:
2040 /* Integer field is created field */
2041 BT_MOVE(int_field
, field
);
2044 case BT_FIELD_TYPE_ID_ENUM
:
2045 int_field
= bt_field_enumeration_get_container(field
);
2047 type
= bt_field_get_type(int_field
);
2051 BT_LOGF("Unexpected field type ID: "
2052 "notit-addr=%p, ft-addr=%p, ft-id=%s",
2054 bt_field_type_id_string(
2055 bt_field_type_get_type_id(type
)));
2060 ret
= bt_field_unsigned_integer_set_value(int_field
, value
);
2062 stack_top(notit
->stack
)->index
++;
2063 *out_int_field
= int_field
;
2072 enum bt_btr_status
btr_timestamp_end_cb(void *value
,
2073 struct bt_field_type
*type
, void *data
)
2075 enum bt_btr_status status
;
2076 struct bt_field
*field
= NULL
;
2077 struct bt_notif_iter
*notit
= data
;
2079 BT_LOGV("`timestamp_end` unsigned integer function called from BTR: "
2080 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2082 notit
, notit
->btr
, type
,
2083 bt_field_type_id_string(
2084 bt_field_type_get_type_id(type
)));
2085 status
= btr_unsigned_int_common(*((uint64_t *) value
), type
, data
,
2088 /* Set as the current packet's timestamp_end field. */
2089 BT_MOVE(notit
->cur_timestamp_end
, field
);
2094 enum bt_btr_status
btr_unsigned_int_cb(uint64_t value
,
2095 struct bt_field_type
*type
, void *data
)
2097 struct bt_notif_iter
*notit
= data
;
2098 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2099 struct bt_field
*field
= NULL
;
2100 struct field_cb_override
*override
;
2102 BT_LOGV("Unsigned integer function called from BTR: "
2103 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2104 "ft-id=%s, value=%" PRIu64
,
2105 notit
, notit
->btr
, type
,
2106 bt_field_type_id_string(
2107 bt_field_type_get_type_id(type
)),
2109 override
= g_hash_table_lookup(notit
->field_overrides
, type
);
2110 if (unlikely(override
)) {
2111 /* Override function logs errors */
2112 status
= override
->func(&value
, type
, override
->data
);
2116 status
= btr_unsigned_int_common(value
, type
, data
, &field
);
2117 if (status
!= BT_BTR_STATUS_OK
) {
2118 /* btr_unsigned_int_common() logs errors */
2122 status
= update_clock(notit
, field
);
2129 enum bt_btr_status
btr_signed_int_cb(int64_t value
,
2130 struct bt_field_type
*type
, void *data
)
2132 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2133 struct bt_field
*field
= NULL
;
2134 struct bt_field
*int_field
= NULL
;
2135 struct bt_notif_iter
*notit
= data
;
2138 BT_LOGV("Signed integer function called from BTR: "
2139 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2140 "ft-id=%s, value=%" PRId64
,
2141 notit
, notit
->btr
, type
,
2142 bt_field_type_id_string(
2143 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
;
2154 switch(bt_field_type_get_type_id(type
)) {
2155 case BT_FIELD_TYPE_ID_INTEGER
:
2156 /* Integer field is created field */
2157 BT_MOVE(int_field
, field
);
2160 case BT_FIELD_TYPE_ID_ENUM
:
2161 int_field
= bt_field_enumeration_get_container(field
);
2163 type
= bt_field_get_type(int_field
);
2167 BT_LOGF("Unexpected field type ID: "
2168 "notit-addr=%p, ft-addr=%p, ft-id=%s",
2170 bt_field_type_id_string(
2171 bt_field_type_get_type_id(type
)));
2176 ret
= bt_field_signed_integer_set_value(int_field
, value
);
2178 stack_top(notit
->stack
)->index
++;
2179 status
= update_clock(notit
, int_field
);
2189 enum bt_btr_status
btr_floating_point_cb(double value
,
2190 struct bt_field_type
*type
, void *data
)
2192 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2193 struct bt_field
*field
= NULL
;
2194 struct bt_notif_iter
*notit
= data
;
2197 BT_LOGV("Floating point number function called from BTR: "
2198 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2199 "ft-id=%s, value=%f",
2200 notit
, notit
->btr
, type
,
2201 bt_field_type_id_string(
2202 bt_field_type_get_type_id(type
)),
2205 /* Create next field */
2206 field
= get_next_field(notit
);
2208 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2209 status
= BT_BTR_STATUS_ERROR
;
2213 ret
= bt_field_floating_point_set_value(field
, value
);
2215 stack_top(notit
->stack
)->index
++;
2223 enum bt_btr_status
btr_string_begin_cb(
2224 struct bt_field_type
*type
, void *data
)
2226 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2227 struct bt_field
*field
= NULL
;
2228 struct bt_notif_iter
*notit
= data
;
2231 BT_LOGV("String (beginning) 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 /* Create next field */
2239 field
= get_next_field(notit
);
2241 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2242 status
= BT_BTR_STATUS_ERROR
;
2247 * Push on stack. Not a compound type per se, but we know that only
2248 * btr_string_cb() may be called between this call and a subsequent
2249 * call to btr_string_end_cb().
2251 ret
= stack_push(notit
->stack
, field
);
2253 BT_LOGE("Cannot push string field on stack: "
2254 "notit-addr=%p, field-addr=%p", notit
, field
);
2255 status
= BT_BTR_STATUS_ERROR
;
2260 * Initialize string field payload to an empty string since in the
2261 * case of a length 0 string the btr_string_cb won't be called and
2262 * we will end up with an unset string payload.
2264 ret
= bt_field_string_set_value(field
, "");
2266 BT_LOGE("Cannot initialize string field's value to an empty string: "
2267 "notit-addr=%p, field-addr=%p, ret=%d",
2269 status
= BT_BTR_STATUS_ERROR
;
2280 enum bt_btr_status
btr_string_cb(const char *value
,
2281 size_t len
, struct bt_field_type
*type
, void *data
)
2283 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2284 struct bt_field
*field
= NULL
;
2285 struct bt_notif_iter
*notit
= data
;
2288 BT_LOGV("String (substring) function called from BTR: "
2289 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2290 "ft-id=%s, string-length=%zu",
2291 notit
, notit
->btr
, type
,
2292 bt_field_type_id_string(
2293 bt_field_type_get_type_id(type
)),
2296 /* Get string field */
2297 field
= stack_top(notit
->stack
)->base
;
2300 /* Append current string */
2301 ret
= bt_field_string_append_len(field
, value
, len
);
2303 BT_LOGE("Cannot append substring to string field's value: "
2304 "notit-addr=%p, field-addr=%p, string-length=%zu, "
2305 "ret=%d", notit
, field
, len
, ret
);
2306 status
= BT_BTR_STATUS_ERROR
;
2315 enum bt_btr_status
btr_string_end_cb(
2316 struct bt_field_type
*type
, void *data
)
2318 struct bt_notif_iter
*notit
= data
;
2320 BT_LOGV("String (end) function called from BTR: "
2321 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2323 notit
, notit
->btr
, type
,
2324 bt_field_type_id_string(
2325 bt_field_type_get_type_id(type
)));
2327 /* Pop string field */
2328 stack_pop(notit
->stack
);
2330 /* Go to next field */
2331 stack_top(notit
->stack
)->index
++;
2332 return BT_BTR_STATUS_OK
;
2335 enum bt_btr_status
btr_compound_begin_cb(
2336 struct bt_field_type
*type
, void *data
)
2338 enum bt_btr_status status
= BT_BTR_STATUS_OK
;
2339 struct bt_notif_iter
*notit
= data
;
2340 struct bt_field
*field
;
2343 BT_LOGV("Compound (beginning) function called from BTR: "
2344 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2346 notit
, notit
->btr
, type
,
2347 bt_field_type_id_string(
2348 bt_field_type_get_type_id(type
)));
2351 if (stack_empty(notit
->stack
)) {
2352 /* Root: create dynamic scope field */
2353 *notit
->cur_dscope_field
= bt_field_create(type
);
2354 field
= *notit
->cur_dscope_field
;
2357 * Field will be put at the end of this function
2358 * (stack_push() will take one reference, but this
2359 * reference is lost upon the equivalent stack_pop()
2360 * later), so also get it for our context to own it.
2362 bt_get(*notit
->cur_dscope_field
);
2365 BT_LOGE("Cannot create compound field: "
2366 "notit-addr=%p, ft-addr=%p, ft-id=%s",
2368 bt_field_type_id_string(
2369 bt_field_type_get_type_id(type
)));
2370 status
= BT_BTR_STATUS_ERROR
;
2374 field
= get_next_field(notit
);
2376 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2377 status
= BT_BTR_STATUS_ERROR
;
2384 ret
= stack_push(notit
->stack
, field
);
2386 BT_LOGE("Cannot push compound field onto the stack: "
2387 "notit-addr=%p, ft-addr=%p, ft-id=%s, ret=%d",
2389 bt_field_type_id_string(
2390 bt_field_type_get_type_id(type
)),
2392 status
= BT_BTR_STATUS_ERROR
;
2402 enum bt_btr_status
btr_compound_end_cb(
2403 struct bt_field_type
*type
, void *data
)
2405 struct bt_notif_iter
*notit
= data
;
2407 BT_LOGV("Compound (end) function called from BTR: "
2408 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2410 notit
, notit
->btr
, type
,
2411 bt_field_type_id_string(
2412 bt_field_type_get_type_id(type
)));
2413 assert(!stack_empty(notit
->stack
));
2416 stack_pop(notit
->stack
);
2418 /* If the stack is not empty, increment the base's index */
2419 if (!stack_empty(notit
->stack
)) {
2420 stack_top(notit
->stack
)->index
++;
2423 return BT_BTR_STATUS_OK
;
2427 struct bt_field
*resolve_field(struct bt_notif_iter
*notit
,
2428 struct bt_field_path
*path
)
2430 struct bt_field
*field
= NULL
;
2433 if (BT_LOG_ON_VERBOSE
) {
2434 GString
*gstr
= bt_field_path_string(path
);
2436 BT_LOGV("Resolving field path: notit-addr=%p, field-path=\"%s\"",
2437 notit
, gstr
? gstr
->str
: NULL
);
2440 g_string_free(gstr
, TRUE
);
2444 switch (bt_field_path_get_root_scope(path
)) {
2445 case BT_SCOPE_TRACE_PACKET_HEADER
:
2446 field
= notit
->dscopes
.trace_packet_header
;
2448 case BT_SCOPE_STREAM_PACKET_CONTEXT
:
2449 field
= notit
->dscopes
.stream_packet_context
;
2451 case BT_SCOPE_STREAM_EVENT_HEADER
:
2452 field
= notit
->dscopes
.stream_event_header
;
2454 case BT_SCOPE_STREAM_EVENT_CONTEXT
:
2455 field
= notit
->dscopes
.stream_event_context
;
2457 case BT_SCOPE_EVENT_CONTEXT
:
2458 field
= notit
->dscopes
.event_context
;
2460 case BT_SCOPE_EVENT_FIELDS
:
2461 field
= notit
->dscopes
.event_payload
;
2464 BT_LOGF("Cannot resolve field path: unknown scope: "
2465 "notit-addr=%p, root-scope=%s",
2466 notit
, bt_scope_string(
2467 bt_field_path_get_root_scope(path
)));
2472 BT_LOGW("Cannot resolve field path: root field not found: "
2473 "notit-addr=%p, root-scope=%s",
2474 notit
, bt_scope_string(
2475 bt_field_path_get_root_scope(path
)));
2481 for (i
= 0; i
< bt_field_path_get_index_count(path
); ++i
) {
2482 struct bt_field
*next_field
= NULL
;
2483 struct bt_field_type
*field_type
;
2484 int index
= bt_field_path_get_index(path
, i
);
2486 field_type
= bt_field_get_type(field
);
2489 if (is_struct_type(field_type
)) {
2490 next_field
= bt_field_structure_get_field_by_index(
2492 } else if (is_variant_type(field_type
)) {
2494 bt_field_variant_get_current_field(field
);
2501 BT_LOGW("Cannot find next field: "
2502 "notit-addr=%p, ft-addr=%p, ft-id=%s, index=%d",
2504 bt_field_type_id_string(
2505 bt_field_type_get_type_id(field_type
)),
2510 /* Move next field -> field */
2511 BT_MOVE(field
, next_field
);
2519 int64_t btr_get_sequence_length_cb(struct bt_field_type
*type
, void *data
)
2523 struct bt_field
*seq_field
;
2524 struct bt_field_path
*field_path
;
2525 struct bt_notif_iter
*notit
= data
;
2526 struct bt_field
*length_field
= NULL
;
2529 field_path
= bt_field_type_sequence_get_length_field_path(type
);
2531 length_field
= resolve_field(notit
, field_path
);
2532 if (!length_field
) {
2533 BT_LOGW("Cannot resolve sequence field type's length field path: "
2534 "notit-addr=%p, ft-addr=%p",
2539 iret
= bt_field_unsigned_integer_get_value(length_field
, &length
);
2541 BT_LOGE("Cannot get value of sequence length field: "
2542 "notit-addr=%p, field-addr=%p",
2543 notit
, length_field
);
2547 seq_field
= stack_top(notit
->stack
)->base
;
2548 iret
= bt_field_sequence_set_length(seq_field
, length_field
);
2550 BT_LOGE("Cannot set sequence field's length field: "
2551 "notit-addr=%p, seq-field-addr=%p, "
2552 "length-field-addr=%p, ",
2553 notit
, seq_field
, length_field
);
2557 ret
= (int64_t) length
;
2560 BT_PUT(length_field
);
2567 struct bt_field_type
*btr_get_variant_type_cb(
2568 struct bt_field_type
*type
, void *data
)
2570 struct bt_field_path
*path
;
2571 struct bt_notif_iter
*notit
= data
;
2572 struct bt_field
*var_field
;
2573 struct bt_field
*tag_field
= NULL
;
2574 struct bt_field
*selected_field
= NULL
;
2575 struct bt_field_type
*selected_field_type
= NULL
;
2577 path
= bt_field_type_variant_get_tag_field_path(type
);
2579 tag_field
= resolve_field(notit
, path
);
2581 BT_LOGW("Cannot resolve variant field type's tag field path: "
2582 "notit-addr=%p, ft-addr=%p",
2588 * We found the enumeration tag field instance which should be
2589 * able to select a current field for this variant. This
2590 * callback function we're in is called _after_
2591 * compound_begin(), so the current stack top's base field is
2592 * the variant field in question. We get the selected field here
2593 * thanks to this tag field (thus creating the selected field),
2594 * which will also provide us with its type. Then, this field
2595 * will remain the current selected one until the next callback
2596 * function call which is used to fill the current selected
2599 var_field
= stack_top(notit
->stack
)->base
;
2600 selected_field
= bt_field_variant_get_field(var_field
, tag_field
);
2601 if (!selected_field
) {
2602 BT_LOGW("Cannot get variant field's selection using tag field: "
2603 "notit-addr=%p, var-field-addr=%p, tag-field-addr=%p",
2604 notit
, var_field
, tag_field
);
2608 selected_field_type
= bt_field_get_type(selected_field
);
2612 BT_PUT(selected_field
);
2615 return selected_field_type
;
2619 int set_event_clocks(struct bt_event
*event
,
2620 struct bt_notif_iter
*notit
)
2623 GHashTableIter iter
;
2624 struct bt_clock_class
*clock_class
;
2625 uint64_t *clock_state
;
2627 g_hash_table_iter_init(&iter
, notit
->clock_states
);
2629 while (g_hash_table_iter_next(&iter
, (gpointer
) &clock_class
,
2630 (gpointer
) &clock_state
)) {
2631 struct bt_clock_value
*clock_value
;
2633 clock_value
= bt_clock_value_create(clock_class
,
2636 BT_LOGE("Cannot create clock value from clock class: "
2637 "notit-addr=%p, clock-class-addr=%p, "
2638 "clock-class-name=\"%s\"",
2640 bt_clock_class_get_name(clock_class
));
2644 ret
= bt_event_set_clock_value(event
, clock_value
);
2645 bt_put(clock_value
);
2647 struct bt_event_class
*event_class
=
2648 bt_event_get_class(event
);
2650 assert(event_class
);
2651 BT_LOGE("Cannot set event's clock value: "
2652 "notit-addr=%p, event-addr=%p, "
2653 "event-class-name=\"%s\", "
2654 "event-class-id=%" PRId64
", "
2655 "clock-class-addr=%p, "
2656 "clock-class-name=\"%s\", "
2657 "clock-value-addr=%p",
2659 bt_event_class_get_name(event_class
),
2660 bt_event_class_get_id(event_class
),
2662 bt_clock_class_get_name(clock_class
),
2664 bt_put(event_class
);
2675 struct bt_event
*create_event(struct bt_notif_iter
*notit
)
2678 struct bt_event
*event
;
2680 BT_LOGV("Creating event for event notification: "
2681 "notit-addr=%p, event-class-addr=%p, "
2682 "event-class-name=\"%s\", "
2683 "event-class-id=%" PRId64
,
2684 notit
, notit
->meta
.event_class
,
2685 bt_event_class_get_name(notit
->meta
.event_class
),
2686 bt_event_class_get_id(notit
->meta
.event_class
));
2688 /* Create event object. */
2689 event
= bt_event_create(notit
->meta
.event_class
);
2691 BT_LOGE("Cannot create event: "
2692 "notit-addr=%p, event-class-addr=%p, "
2693 "event-class-name=\"%s\", "
2694 "event-class-id=%" PRId64
,
2695 notit
, notit
->meta
.event_class
,
2696 bt_event_class_get_name(notit
->meta
.event_class
),
2697 bt_event_class_get_id(notit
->meta
.event_class
));
2701 /* Set header, stream event context, context, and payload fields. */
2702 ret
= bt_event_set_header(event
,
2703 notit
->dscopes
.stream_event_header
);
2705 BT_LOGE("Cannot set event's header field: "
2706 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2707 "event-class-name=\"%s\", "
2708 "event-class-id=%" PRId64
", field-addr=%p",
2709 notit
, event
, notit
->meta
.event_class
,
2710 bt_event_class_get_name(notit
->meta
.event_class
),
2711 bt_event_class_get_id(notit
->meta
.event_class
),
2712 notit
->dscopes
.stream_event_header
);
2716 ret
= bt_event_set_stream_event_context(event
,
2717 notit
->dscopes
.stream_event_context
);
2719 BT_LOGE("Cannot set event's context field: "
2720 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2721 "event-class-name=\"%s\", "
2722 "event-class-id=%" PRId64
", field-addr=%p",
2723 notit
, event
, notit
->meta
.event_class
,
2724 bt_event_class_get_name(notit
->meta
.event_class
),
2725 bt_event_class_get_id(notit
->meta
.event_class
),
2726 notit
->dscopes
.stream_event_context
);
2730 ret
= bt_event_set_event_context(event
,
2731 notit
->dscopes
.event_context
);
2733 BT_LOGE("Cannot set event's stream event context field: "
2734 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2735 "event-class-name=\"%s\", "
2736 "event-class-id=%" PRId64
", field-addr=%p",
2737 notit
, event
, notit
->meta
.event_class
,
2738 bt_event_class_get_name(notit
->meta
.event_class
),
2739 bt_event_class_get_id(notit
->meta
.event_class
),
2740 notit
->dscopes
.event_context
);
2744 ret
= bt_event_set_event_payload(event
,
2745 notit
->dscopes
.event_payload
);
2747 BT_LOGE("Cannot set event's payload field: "
2748 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2749 "event-class-name=\"%s\", "
2750 "event-class-id=%" PRId64
", field-addr=%p",
2751 notit
, event
, notit
->meta
.event_class
,
2752 bt_event_class_get_name(notit
->meta
.event_class
),
2753 bt_event_class_get_id(notit
->meta
.event_class
),
2754 notit
->dscopes
.event_payload
);
2758 ret
= set_event_clocks(event
, notit
);
2760 BT_LOGE("Cannot set event's clock values: "
2761 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2762 "event-class-name=\"%s\", "
2763 "event-class-id=%" PRId64
,
2764 notit
, event
, notit
->meta
.event_class
,
2765 bt_event_class_get_name(notit
->meta
.event_class
),
2766 bt_event_class_get_id(notit
->meta
.event_class
));
2770 /* Associate with current packet. */
2771 assert(notit
->packet
);
2772 ret
= bt_event_set_packet(event
, notit
->packet
);
2774 BT_LOGE("Cannot set event's header field: "
2775 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2776 "event-class-name=\"%s\", "
2777 "event-class-id=%" PRId64
", packet-addr=%p",
2778 notit
, event
, notit
->meta
.event_class
,
2779 bt_event_class_get_name(notit
->meta
.event_class
),
2780 bt_event_class_get_id(notit
->meta
.event_class
),
2795 uint64_t get_cur_stream_instance_id(struct bt_notif_iter
*notit
)
2797 struct bt_field
*stream_instance_id_field
= NULL
;
2798 uint64_t stream_instance_id
= -1ULL;
2801 if (!notit
->dscopes
.trace_packet_header
) {
2805 stream_instance_id_field
= bt_field_structure_get_field_by_name(
2806 notit
->dscopes
.trace_packet_header
, "stream_instance_id");
2807 if (!stream_instance_id_field
) {
2811 ret
= bt_field_unsigned_integer_get_value(stream_instance_id_field
,
2812 &stream_instance_id
);
2814 stream_instance_id
= -1ULL;
2819 bt_put(stream_instance_id_field
);
2820 return stream_instance_id
;
2824 int set_stream(struct bt_notif_iter
*notit
)
2827 struct bt_stream
*stream
= NULL
;
2829 BT_LOGV("Calling user function (get stream): notit-addr=%p, "
2830 "stream-class-addr=%p, stream-class-name=\"%s\", "
2831 "stream-class-id=%" PRId64
,
2832 notit
, notit
->meta
.stream_class
,
2833 bt_stream_class_get_name(notit
->meta
.stream_class
),
2834 bt_stream_class_get_id(notit
->meta
.stream_class
));
2835 stream
= bt_get(notit
->medium
.medops
.get_stream(
2836 notit
->meta
.stream_class
, get_cur_stream_instance_id(notit
),
2837 notit
->medium
.data
));
2838 BT_LOGV("User function returned: stream-addr=%p", stream
);
2840 BT_LOGW_STR("User function failed to return a stream object for the given stream class.");
2845 if (notit
->stream
&& stream
!= notit
->stream
) {
2846 BT_LOGW("User function returned a different stream than the previous one for the same sequence of packets.");
2851 BT_MOVE(notit
->stream
, stream
);
2859 void create_packet(struct bt_notif_iter
*notit
)
2862 struct bt_packet
*packet
= NULL
;
2864 BT_LOGV("Creating packet for packet notification: "
2865 "notit-addr=%p", notit
);
2867 /* Ask the user for the stream */
2868 ret
= set_stream(notit
);
2873 BT_LOGV("Creating packet from stream: "
2874 "notit-addr=%p, stream-addr=%p, "
2875 "stream-class-addr=%p, "
2876 "stream-class-name=\"%s\", "
2877 "stream-class-id=%" PRId64
,
2878 notit
, notit
->stream
, notit
->meta
.stream_class
,
2879 bt_stream_class_get_name(notit
->meta
.stream_class
),
2880 bt_stream_class_get_id(notit
->meta
.stream_class
));
2883 packet
= bt_packet_create(notit
->stream
);
2885 BT_LOGE("Cannot create packet from stream: "
2886 "notit-addr=%p, stream-addr=%p, "
2887 "stream-class-addr=%p, "
2888 "stream-class-name=\"%s\", "
2889 "stream-class-id=%" PRId64
,
2890 notit
, notit
->stream
, notit
->meta
.stream_class
,
2891 bt_stream_class_get_name(notit
->meta
.stream_class
),
2892 bt_stream_class_get_id(notit
->meta
.stream_class
));
2896 /* Set packet's context and header fields */
2897 if (notit
->dscopes
.trace_packet_header
) {
2898 ret
= bt_packet_set_header(packet
,
2899 notit
->dscopes
.trace_packet_header
);
2901 BT_LOGE("Cannot set packet's header field: "
2902 "notit-addr=%p, packet-addr=%p, "
2904 "stream-class-addr=%p, "
2905 "stream-class-name=\"%s\", "
2906 "stream-class-id=%" PRId64
", "
2908 notit
, packet
, notit
->stream
, notit
->meta
.stream_class
,
2909 bt_stream_class_get_name(notit
->meta
.stream_class
),
2910 bt_stream_class_get_id(notit
->meta
.stream_class
),
2911 notit
->dscopes
.trace_packet_header
);
2916 if (notit
->dscopes
.stream_packet_context
) {
2917 ret
= bt_packet_set_context(packet
,
2918 notit
->dscopes
.stream_packet_context
);
2920 BT_LOGE("Cannot set packet's context field: "
2921 "notit-addr=%p, packet-addr=%p, "
2923 "stream-class-addr=%p, "
2924 "stream-class-name=\"%s\", "
2925 "stream-class-id=%" PRId64
", "
2927 notit
, packet
, notit
->stream
, notit
->meta
.stream_class
,
2928 bt_stream_class_get_name(notit
->meta
.stream_class
),
2929 bt_stream_class_get_id(notit
->meta
.stream_class
),
2930 notit
->dscopes
.trace_packet_header
);
2941 BT_MOVE(notit
->packet
, packet
);
2945 void notify_new_packet(struct bt_notif_iter
*notit
,
2946 struct bt_notification
**notification
)
2948 struct bt_notification
*ret
;
2950 /* Initialize the iterator's current packet */
2951 create_packet(notit
);
2952 if (!notit
->packet
) {
2953 BT_LOGE("Cannot create packet for packet notification: "
2954 "notit-addr=%p", notit
);
2958 ret
= bt_notification_packet_begin_create(notit
->packet
);
2960 BT_LOGE("Cannot create packet beginning notification: "
2961 "notit-addr=%p, packet-addr=%p",
2962 notit
, notit
->packet
);
2965 *notification
= ret
;
2969 void notify_end_of_packet(struct bt_notif_iter
*notit
,
2970 struct bt_notification
**notification
)
2972 struct bt_notification
*ret
;
2974 if (!notit
->packet
) {
2978 ret
= bt_notification_packet_end_create(notit
->packet
);
2980 BT_LOGE("Cannot create packet end notification: "
2981 "notit-addr=%p, packet-addr=%p",
2982 notit
, notit
->packet
);
2985 BT_PUT(notit
->packet
);
2986 *notification
= ret
;
2990 void notify_event(struct bt_notif_iter
*notit
,
2991 struct bt_clock_class_priority_map
*cc_prio_map
,
2992 struct bt_notification
**notification
)
2994 struct bt_event
*event
= NULL
;
2995 struct bt_notification
*ret
= NULL
;
2997 /* Make sure that the event contains at least one bit of data */
2998 if (notit
->buf
.at
== notit
->buf
.last_eh_at
) {
2999 BT_LOGE("Cannot create empty event with 0 bits of data: "
3000 "notit-addr=%p, packet-cur=%zu",
3001 notit
, packet_at(notit
));
3006 event
= create_event(notit
);
3008 BT_LOGE("Cannot create event for event notification: "
3009 "notit-addr=%p", notit
);
3013 ret
= bt_notification_event_create(event
, cc_prio_map
);
3015 BT_LOGE("Cannot create event notification: "
3016 "notit-addr=%p, event-addr=%p, "
3017 "cc-prio-map-addr=%p",
3018 notit
, event
, cc_prio_map
);
3021 *notification
= ret
;
3027 void init_trace_field_path_cache(struct bt_trace
*trace
,
3028 struct trace_field_path_cache
*trace_field_path_cache
)
3031 int stream_instance_id
= -1;
3033 struct bt_field_type
*packet_header
= NULL
;
3035 packet_header
= bt_trace_get_packet_header_type(trace
);
3036 if (!packet_header
) {
3040 if (!bt_field_type_is_structure(packet_header
)) {
3044 count
= bt_field_type_structure_get_field_count(packet_header
);
3047 for (i
= 0; (i
< count
&& (stream_id
== -1 || stream_instance_id
== -1)); i
++) {
3049 const char *field_name
;
3051 ret
= bt_field_type_structure_get_field_by_index(packet_header
,
3052 &field_name
, NULL
, i
);
3054 BT_LOGE("Cannot get structure field's field: "
3055 "field-addr=%p, index=%d",
3060 if (stream_id
== -1 && !strcmp(field_name
, "stream_id")) {
3062 } else if (stream_instance_id
== -1 &&
3063 !strcmp(field_name
, "stream_instance_id")) {
3064 stream_instance_id
= i
;
3069 trace_field_path_cache
->stream_id
= stream_id
;
3070 trace_field_path_cache
->stream_instance_id
= stream_instance_id
;
3071 BT_PUT(packet_header
);
3075 struct bt_notif_iter
*bt_notif_iter_create(struct bt_trace
*trace
,
3076 size_t max_request_sz
,
3077 struct bt_notif_iter_medium_ops medops
, void *data
)
3079 struct bt_notif_iter
*notit
= NULL
;
3080 struct bt_btr_cbs cbs
= {
3082 .signed_int
= btr_signed_int_cb
,
3083 .unsigned_int
= btr_unsigned_int_cb
,
3084 .floating_point
= btr_floating_point_cb
,
3085 .string_begin
= btr_string_begin_cb
,
3086 .string
= btr_string_cb
,
3087 .string_end
= btr_string_end_cb
,
3088 .compound_begin
= btr_compound_begin_cb
,
3089 .compound_end
= btr_compound_end_cb
,
3092 .get_sequence_length
= btr_get_sequence_length_cb
,
3093 .get_variant_type
= btr_get_variant_type_cb
,
3098 assert(medops
.request_bytes
);
3099 assert(medops
.get_stream
);
3100 BT_LOGD("Creating CTF plugin notification iterator: "
3101 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
3103 trace
, bt_trace_get_name(trace
), max_request_sz
, data
);
3104 notit
= g_new0(struct bt_notif_iter
, 1);
3106 BT_LOGE_STR("Failed to allocate one CTF plugin notification iterator.");
3109 notit
->clock_states
= g_hash_table_new_full(g_direct_hash
,
3110 g_direct_equal
, bt_put
, g_free
);
3111 if (!notit
->clock_states
) {
3112 BT_LOGE_STR("Failed to allocate a GHashTable.");
3115 notit
->meta
.trace
= bt_get(trace
);
3116 notit
->medium
.medops
= medops
;
3117 notit
->medium
.max_request_sz
= max_request_sz
;
3118 notit
->medium
.data
= data
;
3119 notit
->stack
= stack_new(notit
);
3120 if (!notit
->stack
) {
3121 BT_LOGE_STR("Failed to create field stack.");
3125 notit
->btr
= bt_btr_create(cbs
, notit
);
3127 BT_LOGE_STR("Failed to create binary type reader (BTR).");
3131 bt_notif_iter_reset(notit
);
3132 init_trace_field_path_cache(trace
, ¬it
->trace_field_path_cache
);
3133 notit
->sc_field_path_caches
= g_hash_table_new_full(g_direct_hash
,
3134 g_direct_equal
, bt_put
, g_free
);
3135 if (!notit
->sc_field_path_caches
) {
3136 BT_LOGE_STR("Failed to allocate a GHashTable.");
3140 notit
->field_overrides
= g_hash_table_new_full(g_direct_hash
,
3141 g_direct_equal
, bt_put
, g_free
);
3142 if (!notit
->field_overrides
) {
3143 BT_LOGE_STR("Failed to allocate a GHashTable.");
3147 BT_LOGD("Created CTF plugin notification iterator: "
3148 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
3149 "data=%p, notit-addr=%p",
3150 trace
, bt_trace_get_name(trace
), max_request_sz
, data
,
3152 notit
->cur_packet_offset
= 0;
3158 bt_notif_iter_destroy(notit
);
3163 void bt_notif_iter_destroy(struct bt_notif_iter
*notit
)
3165 BT_PUT(notit
->meta
.trace
);
3166 BT_PUT(notit
->meta
.stream_class
);
3167 BT_PUT(notit
->meta
.event_class
);
3168 BT_PUT(notit
->packet
);
3169 BT_PUT(notit
->stream
);
3170 BT_PUT(notit
->cur_timestamp_end
);
3171 put_all_dscopes(notit
);
3173 BT_LOGD("Destroying CTF plugin notification iterator: addr=%p", notit
);
3176 BT_LOGD_STR("Destroying field stack.");
3177 stack_destroy(notit
->stack
);
3181 BT_LOGD("Destroying BTR: btr-addr=%p", notit
->btr
);
3182 bt_btr_destroy(notit
->btr
);
3185 if (notit
->clock_states
) {
3186 g_hash_table_destroy(notit
->clock_states
);
3189 if (notit
->sc_field_path_caches
) {
3190 g_hash_table_destroy(notit
->sc_field_path_caches
);
3193 if (notit
->field_overrides
) {
3194 g_hash_table_destroy(notit
->field_overrides
);
3200 enum bt_notif_iter_status
bt_notif_iter_get_next_notification(
3201 struct bt_notif_iter
*notit
,
3202 struct bt_clock_class_priority_map
*cc_prio_map
,
3203 struct bt_notification
**notification
)
3205 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
3208 assert(notification
);
3210 BT_LOGV("Getting next notification: notit-addr=%p, cc-prio-map-addr=%p",
3211 notit
, cc_prio_map
);
3214 status
= handle_state(notit
);
3215 if (status
== BT_NOTIF_ITER_STATUS_AGAIN
) {
3216 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_AGAIN.");
3219 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
3220 if (status
== BT_NOTIF_ITER_STATUS_EOF
) {
3221 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_EOF.");
3223 BT_LOGW("Cannot handle state: "
3224 "notit-addr=%p, state=%s",
3225 notit
, state_string(notit
->state
));
3230 switch (notit
->state
) {
3231 case STATE_EMIT_NOTIF_NEW_PACKET
:
3232 /* notify_new_packet() logs errors */
3233 notify_new_packet(notit
, notification
);
3234 if (!*notification
) {
3235 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3238 case STATE_EMIT_NOTIF_EVENT
:
3239 /* notify_event() logs errors */
3240 notify_event(notit
, cc_prio_map
, notification
);
3241 if (!*notification
) {
3242 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3245 case STATE_EMIT_NOTIF_END_OF_PACKET
:
3246 /* Update clock with timestamp_end field. */
3247 if (notit
->cur_timestamp_end
) {
3248 enum bt_btr_status btr_status
;
3249 struct bt_field_type
*field_type
=
3251 notit
->cur_timestamp_end
);
3254 btr_status
= update_clock(notit
,
3255 notit
->cur_timestamp_end
);
3257 if (btr_status
!= BT_BTR_STATUS_OK
) {
3258 BT_LOGW("Cannot update stream's clock value: "
3259 "notit-addr=%p", notit
);
3260 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3265 /* notify_end_of_packet() logs errors */
3266 notify_end_of_packet(notit
, notification
);
3267 if (!*notification
) {
3268 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3272 /* Non-emitting state: continue */
3282 enum bt_notif_iter_status
bt_notif_iter_get_packet_header_context_fields(
3283 struct bt_notif_iter
*notit
,
3284 struct bt_field
**packet_header_field
,
3285 struct bt_field
**packet_context_field
)
3288 enum bt_notif_iter_status status
= BT_NOTIF_ITER_STATUS_OK
;
3292 if (notit
->state
== STATE_EMIT_NOTIF_NEW_PACKET
) {
3293 /* We're already there */
3298 status
= handle_state(notit
);
3299 if (status
== BT_NOTIF_ITER_STATUS_AGAIN
) {
3300 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_AGAIN.");
3303 if (status
!= BT_NOTIF_ITER_STATUS_OK
) {
3304 if (status
== BT_NOTIF_ITER_STATUS_EOF
) {
3305 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_EOF.");
3307 BT_LOGW("Cannot handle state: "
3308 "notit-addr=%p, state=%s",
3309 notit
, state_string(notit
->state
));
3314 switch (notit
->state
) {
3315 case STATE_EMIT_NOTIF_NEW_PACKET
:
3317 * Packet header and context fields are
3318 * potentially decoded (or they don't exist).
3322 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
3323 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
3324 case STATE_AFTER_TRACE_PACKET_HEADER
:
3325 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
3326 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
3327 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
3328 /* Non-emitting state: continue */
3332 * We should never get past the
3333 * STATE_EMIT_NOTIF_NEW_PACKET state.
3335 BT_LOGF("Unexpected state: notit-addr=%p, state=%s",
3336 notit
, state_string(notit
->state
));
3342 if (packet_header_field
) {
3343 *packet_header_field
= bt_get(notit
->dscopes
.trace_packet_header
);
3346 if (packet_context_field
) {
3347 *packet_context_field
= bt_get(notit
->dscopes
.stream_packet_context
);
3350 ret
= set_current_packet_content_sizes(notit
);
3352 status
= BT_NOTIF_ITER_STATUS_ERROR
;
3360 void bt_notif_iter_set_medops_data(struct bt_notif_iter
*notit
,
3364 notit
->medium
.data
= medops_data
;
3368 enum bt_notif_iter_status
bt_notif_iter_seek(
3369 struct bt_notif_iter
*notit
, off_t offset
)
3371 enum bt_notif_iter_status ret
= BT_NOTIF_ITER_STATUS_OK
;
3372 enum bt_notif_iter_medium_status medium_status
;
3376 BT_LOGE("Cannot seek to negative offset: offset=%jd", offset
);
3377 ret
= BT_NOTIF_ITER_STATUS_INVAL
;
3381 if (!notit
->medium
.medops
.seek
) {
3382 ret
= BT_NOTIF_ITER_STATUS_UNSUPPORTED
;
3383 BT_LOGD("Aborting seek as the iterator's underlying media does not implement seek support.");
3387 medium_status
= notit
->medium
.medops
.seek(
3388 BT_NOTIF_ITER_SEEK_WHENCE_SET
, offset
,
3389 notit
->medium
.data
);
3390 if (medium_status
!= BT_NOTIF_ITER_MEDIUM_STATUS_OK
) {
3391 if (medium_status
== BT_NOTIF_ITER_MEDIUM_STATUS_EOF
) {
3392 ret
= BT_NOTIF_ITER_STATUS_EOF
;
3394 ret
= BT_NOTIF_ITER_STATUS_ERROR
;
3399 bt_notif_iter_reset(notit
);
3400 notit
->cur_packet_offset
= offset
;
3406 off_t
bt_notif_iter_get_current_packet_offset(
3407 struct bt_notif_iter
*notit
)
3410 return notit
->cur_packet_offset
;
3414 off_t
bt_notif_iter_get_current_packet_size(
3415 struct bt_notif_iter
*notit
)
3418 return notit
->cur_packet_size
;