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"
45 struct bt_ctf_notif_iter
;
47 /* A visit stack entry */
50 * Current base field, one of:
58 * Field is owned by this.
60 struct bt_ctf_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_ctf_btr_status (* func
)(void *value
,
125 struct bt_ctf_field_type
*type
, void *data
);
129 /* CTF notification iterator */
130 struct bt_ctf_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_ctf_field
**cur_dscope_field
;
143 /* Trace and classes (owned by this) */
145 struct bt_ctf_trace
*trace
;
146 struct bt_ctf_stream_class
*stream_class
;
147 struct bt_ctf_event_class
*event_class
;
150 /* Current packet (NULL if not created yet) */
151 struct bt_ctf_packet
*packet
;
153 /* Current stream (NULL if not set yet) */
154 struct bt_ctf_stream
*stream
;
157 * Current timestamp_end field (to consider before switching packets).
159 struct bt_ctf_field
*cur_timestamp_end
;
161 /* Database of current dynamic scopes (owned by this) */
163 struct bt_ctf_field
*trace_packet_header
;
164 struct bt_ctf_field
*stream_packet_context
;
165 struct bt_ctf_field
*stream_event_header
;
166 struct bt_ctf_field
*stream_event_context
;
167 struct bt_ctf_field
*event_context
;
168 struct bt_ctf_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_ctf_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 */
208 struct bt_ctf_btr
*btr
;
210 /* Current medium data */
212 struct bt_ctf_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_ctf_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_ctf_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_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter
*notit
);
301 enum bt_ctf_btr_status
btr_timestamp_end_cb(void *value
,
302 struct bt_ctf_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_ctf_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_ctf_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_ctf_notif_iter_status
notif_iter_status_from_m_status(
422 enum bt_ctf_notif_iter_medium_status m_status
)
424 return (int) m_status
;
428 size_t buf_size_bits(struct bt_ctf_notif_iter
*notit
)
430 return notit
->buf
.sz
* 8;
434 size_t buf_available_bits(struct bt_ctf_notif_iter
*notit
)
436 return buf_size_bits(notit
) - notit
->buf
.at
;
440 size_t packet_at(struct bt_ctf_notif_iter
*notit
)
442 return notit
->buf
.packet_offset
+ notit
->buf
.at
;
446 void buf_consume_bits(struct bt_ctf_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_ctf_notif_iter_status
request_medium_bytes(
455 struct bt_ctf_notif_iter
*notit
)
457 uint8_t *buffer_addr
= NULL
;
458 size_t buffer_sz
= 0;
459 enum bt_ctf_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_ctf_notif_iter_medium_status_string(m_status
),
468 buffer_addr
, buffer_sz
);
469 if (m_status
== BT_CTF_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_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF
) {
492 struct bt_ctf_field_type
*eh_ft
= NULL
;
493 struct bt_ctf_field_type
*sec_ft
= NULL
;
494 struct bt_ctf_field_type
*ec_ft
= NULL
;
497 * User returned end of stream: validate that we're not
498 * in the middle of a packet header, packet context, or
501 if (notit
->state
== STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
) {
502 /* Beginning of packet: always valid */
506 if (!notit
->meta
.stream_class
) {
510 eh_ft
= bt_ctf_stream_class_get_event_header_type(
511 notit
->meta
.stream_class
);
512 sec_ft
= bt_ctf_stream_class_get_event_context_type(
513 notit
->meta
.stream_class
);
515 if (notit
->state
== STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
) {
517 * Beginning of event's header is only valid if
518 * the packet is not supposed to have a specific
519 * size (whole packet sequence has in fact only
522 if (notit
->cur_packet_size
== -1) {
527 if (notit
->state
== STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
) {
529 * Beginning of event's stream event context is
530 * only valid if the packet is not supposed to
531 * have a specific size (whole packet sequence
532 * has in fact only one packet), and there's no
535 if (notit
->cur_packet_size
== -1 && !eh_ft
) {
540 if (!notit
->meta
.event_class
) {
544 ec_ft
= bt_ctf_event_class_get_context_type(
545 notit
->meta
.event_class
);
547 if (notit
->state
== STATE_DSCOPE_EVENT_CONTEXT_BEGIN
) {
549 * Beginning of event's context is only valid if
550 * the packet is not supposed to have a specific
551 * size (whole packet sequence has in fact only
552 * one packet), and there's no event header and
553 * no stream event context.
555 if (notit
->cur_packet_size
== -1 && !eh_ft
&& !sec_ft
) {
560 if (notit
->state
== STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
) {
562 * Beginning of event's context is only valid if
563 * the packet is not supposed to have a specific
564 * size (whole packet sequence has in fact only
565 * one packet), and there's no event header, no
566 * stream event context, and no event context.
568 if (notit
->cur_packet_size
== -1 && !eh_ft
&& !sec_ft
&&
575 /* All other states are invalid */
576 BT_LOGW("User function returned %s, but notification iterator is in an unexpected state: "
578 bt_ctf_notif_iter_medium_status_string(m_status
),
579 state_string(notit
->state
));
580 m_status
= BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR
;
586 } else if (m_status
< 0) {
587 BT_LOGW("User function failed: status=%s",
588 bt_ctf_notif_iter_medium_status_string(m_status
));
591 return notif_iter_status_from_m_status(m_status
);
595 enum bt_ctf_notif_iter_status
buf_ensure_available_bits(
596 struct bt_ctf_notif_iter
*notit
)
598 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
600 if (buf_available_bits(notit
) == 0) {
602 * This _cannot_ return BT_CTF_NOTIF_ITER_STATUS_OK
605 status
= request_medium_bytes(notit
);
612 enum bt_ctf_notif_iter_status
read_dscope_begin_state(
613 struct bt_ctf_notif_iter
*notit
,
614 struct bt_ctf_field_type
*dscope_field_type
,
615 enum state done_state
, enum state continue_state
,
616 struct bt_ctf_field
**dscope_field
)
618 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
619 enum bt_ctf_btr_status btr_status
;
620 size_t consumed_bits
;
622 status
= buf_ensure_available_bits(notit
);
623 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
625 BT_LOGW("Cannot ensure that buffer has at least one byte: "
626 "notif-addr=%p, status=%s",
627 notit
, bt_ctf_notif_iter_status_string(status
));
629 BT_LOGV("Cannot ensure that buffer has at least one byte: "
630 "notif-addr=%p, status=%s",
631 notit
, bt_ctf_notif_iter_status_string(status
));
637 bt_put(*dscope_field
);
638 notit
->cur_dscope_field
= dscope_field
;
639 BT_LOGV("Starting BTR: notit-addr=%p, btr-addr=%p, ft-addr=%p",
640 notit
, notit
->btr
, dscope_field_type
);
641 consumed_bits
= bt_ctf_btr_start(notit
->btr
, dscope_field_type
,
642 notit
->buf
.addr
, notit
->buf
.at
, packet_at(notit
),
643 notit
->buf
.sz
, &btr_status
);
644 BT_LOGV("BTR consumed bits: size=%zu", consumed_bits
);
646 switch (btr_status
) {
647 case BT_CTF_BTR_STATUS_OK
:
648 /* type was read completely */
649 BT_LOGV_STR("Field was completely decoded.");
650 notit
->state
= done_state
;
652 case BT_CTF_BTR_STATUS_EOF
:
653 BT_LOGV_STR("BTR needs more data to decode field completely.");
654 notit
->state
= continue_state
;
657 BT_LOGW("BTR failed to start: notit-addr=%p, btr-addr=%p, "
658 "status=%s", notit
, notit
->btr
,
659 bt_ctf_btr_status_string(btr_status
));
660 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
664 /* Consume bits now since we know we're not in an error state */
665 buf_consume_bits(notit
, consumed_bits
);
672 enum bt_ctf_notif_iter_status
read_dscope_continue_state(
673 struct bt_ctf_notif_iter
*notit
, enum state done_state
)
675 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
676 enum bt_ctf_btr_status btr_status
;
677 size_t consumed_bits
;
679 status
= buf_ensure_available_bits(notit
);
680 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
682 BT_LOGW("Cannot ensure that buffer has at least one byte: "
683 "notif-addr=%p, status=%s",
684 notit
, bt_ctf_notif_iter_status_string(status
));
686 BT_LOGV("Cannot ensure that buffer has at least one byte: "
687 "notif-addr=%p, status=%s",
688 notit
, bt_ctf_notif_iter_status_string(status
));
694 BT_LOGV("Continuing BTR: notit-addr=%p, btr-addr=%p",
696 consumed_bits
= bt_ctf_btr_continue(notit
->btr
, notit
->buf
.addr
,
697 notit
->buf
.sz
, &btr_status
);
698 BT_LOGV("BTR consumed bits: size=%zu", consumed_bits
);
700 switch (btr_status
) {
701 case BT_CTF_BTR_STATUS_OK
:
702 /* Type was read completely. */
703 BT_LOGV_STR("Field was completely decoded.");
704 notit
->state
= done_state
;
706 case BT_CTF_BTR_STATUS_EOF
:
707 /* Stay in this continue state. */
708 BT_LOGV_STR("BTR needs more data to decode field completely.");
711 BT_LOGW("BTR failed to continue: notit-addr=%p, btr-addr=%p, "
712 "status=%s", notit
, notit
->btr
,
713 bt_ctf_btr_status_string(btr_status
));
714 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
718 /* Consume bits now since we know we're not in an error state. */
719 buf_consume_bits(notit
, consumed_bits
);
725 void put_event_dscopes(struct bt_ctf_notif_iter
*notit
)
727 BT_LOGV_STR("Putting event header field.");
728 BT_PUT(notit
->dscopes
.stream_event_header
);
729 BT_LOGV_STR("Putting stream event context field.");
730 BT_PUT(notit
->dscopes
.stream_event_context
);
731 BT_LOGV_STR("Putting event context field.");
732 BT_PUT(notit
->dscopes
.event_context
);
733 BT_LOGV_STR("Putting event payload field.");
734 BT_PUT(notit
->dscopes
.event_payload
);
738 void put_all_dscopes(struct bt_ctf_notif_iter
*notit
)
740 BT_LOGV_STR("Putting packet header field.");
741 BT_PUT(notit
->dscopes
.trace_packet_header
);
742 BT_LOGV_STR("Putting packet context field.");
743 BT_PUT(notit
->dscopes
.stream_packet_context
);
744 put_event_dscopes(notit
);
748 enum bt_ctf_notif_iter_status
read_packet_header_begin_state(
749 struct bt_ctf_notif_iter
*notit
)
751 struct bt_ctf_field_type
*packet_header_type
= NULL
;
752 enum bt_ctf_notif_iter_status ret
= BT_CTF_NOTIF_ITER_STATUS_OK
;
754 if (bt_ctf_notif_iter_switch_packet(notit
)) {
755 BT_LOGW("Cannot switch packet: notit-addr=%p", notit
);
756 ret
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
760 /* Packet header type is common to the whole trace. */
761 packet_header_type
= bt_ctf_trace_get_packet_header_type(
763 if (!packet_header_type
) {
764 notit
->state
= STATE_AFTER_TRACE_PACKET_HEADER
;
768 BT_LOGV("Decoding packet header field:"
769 "notit-addr=%p, trace-addr=%p, trace-name=\"%s\", ft-addr=%p",
770 notit
, notit
->meta
.trace
,
771 bt_ctf_trace_get_name(notit
->meta
.trace
), packet_header_type
);
772 ret
= read_dscope_begin_state(notit
, packet_header_type
,
773 STATE_AFTER_TRACE_PACKET_HEADER
,
774 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
775 ¬it
->dscopes
.trace_packet_header
);
777 BT_LOGW("Cannot decode packet header field: "
778 "notit-addr=%p, trace-addr=%p, "
779 "trace-name=\"%s\", ft-addr=%p",
780 notit
, notit
->meta
.trace
,
781 bt_ctf_trace_get_name(notit
->meta
.trace
),
785 BT_PUT(packet_header_type
);
790 enum bt_ctf_notif_iter_status
read_packet_header_continue_state(
791 struct bt_ctf_notif_iter
*notit
)
793 return read_dscope_continue_state(notit
,
794 STATE_AFTER_TRACE_PACKET_HEADER
);
798 bool is_struct_type(struct bt_ctf_field_type
*field_type
)
800 return bt_ctf_field_type_get_type_id(field_type
) ==
801 BT_CTF_FIELD_TYPE_ID_STRUCT
;
805 bool is_variant_type(struct bt_ctf_field_type
*field_type
)
807 return bt_ctf_field_type_get_type_id(field_type
) ==
808 BT_CTF_FIELD_TYPE_ID_VARIANT
;
812 struct stream_class_field_path_cache
*
813 create_stream_class_field_path_cache_entry(
814 struct bt_ctf_notif_iter
*notit
,
815 struct bt_ctf_stream_class
*stream_class
)
819 int timestamp_end
= -1;
820 int packet_size
= -1;
821 int content_size
= -1;
822 struct stream_class_field_path_cache
*cache_entry
= g_new0(
823 struct stream_class_field_path_cache
, 1);
824 struct bt_ctf_field_type
*event_header
= NULL
, *packet_context
= NULL
;
827 BT_LOGE_STR("Failed to allocate one stream class field path cache.");
831 event_header
= bt_ctf_stream_class_get_event_header_type(stream_class
);
832 if (event_header
&& bt_ctf_field_type_is_structure(event_header
)) {
835 count
= bt_ctf_field_type_structure_get_field_count(
839 for (i
= 0; i
< count
; i
++) {
843 ret
= bt_ctf_field_type_structure_get_field(
844 event_header
, &name
, NULL
, i
);
846 BT_LOGE("Cannot get event header structure field type's field: "
847 "notit-addr=%p, stream-class-addr=%p, "
848 "stream-class-name=\"%s\", "
849 "stream-class-id=%" PRId64
", "
850 "ft-addr=%p, index=%d",
852 bt_ctf_stream_class_get_name(stream_class
),
853 bt_ctf_stream_class_get_id(stream_class
),
858 if (v
!= -1 && id
!= -1) {
862 if (v
== -1 && strcmp(name
, "v") == 0) {
864 } else if (id
== -1 && !strcmp(name
, "id")) {
870 packet_context
= bt_ctf_stream_class_get_packet_context_type(
872 if (packet_context
&& bt_ctf_field_type_is_structure(packet_context
)) {
875 count
= bt_ctf_field_type_structure_get_field_count(
879 for (i
= 0; i
< count
; i
++) {
882 struct bt_ctf_field_type
*field_type
;
884 if (timestamp_end
!= -1 && packet_size
!= -1 &&
885 content_size
!= -1) {
889 ret
= bt_ctf_field_type_structure_get_field(
890 packet_context
, &name
, &field_type
, i
);
892 BT_LOGE("Cannot get packet context structure field type's field: "
893 "notit-addr=%p, stream-class-addr=%p, "
894 "stream-class-name=\"%s\", "
895 "stream-class-id=%" PRId64
", "
896 "ft-addr=%p, index=%d",
898 bt_ctf_stream_class_get_name(stream_class
),
899 bt_ctf_stream_class_get_id(stream_class
),
904 if (timestamp_end
== -1 &&
905 strcmp(name
, "timestamp_end") == 0) {
906 struct field_cb_override
*override
= g_new0(
907 struct field_cb_override
, 1);
914 override
->func
= btr_timestamp_end_cb
;
915 override
->data
= notit
;
916 g_hash_table_insert(notit
->field_overrides
,
917 bt_get(field_type
), override
);
919 } else if (packet_size
== -1 &&
920 !strcmp(name
, "packet_size")) {
922 } else if (content_size
== -1 &&
923 !strcmp(name
, "content_size")) {
931 cache_entry
->id
= id
;
932 cache_entry
->timestamp_end
= timestamp_end
;
933 cache_entry
->packet_size
= packet_size
;
934 cache_entry
->content_size
= content_size
;
936 BT_PUT(event_header
);
937 BT_PUT(packet_context
);
946 struct stream_class_field_path_cache
*get_stream_class_field_path_cache(
947 struct bt_ctf_notif_iter
*notit
,
948 struct bt_ctf_stream_class
*stream_class
)
950 bool cache_entry_found
;
951 struct stream_class_field_path_cache
*cache_entry
;
953 cache_entry_found
= g_hash_table_lookup_extended(
954 notit
->sc_field_path_caches
,
955 stream_class
, NULL
, (gpointer
) &cache_entry
);
956 if (unlikely(!cache_entry_found
)) {
957 cache_entry
= create_stream_class_field_path_cache_entry(notit
,
959 g_hash_table_insert(notit
->sc_field_path_caches
,
960 bt_get(stream_class
), (gpointer
) cache_entry
);
967 enum bt_ctf_notif_iter_status
set_current_stream_class(
968 struct bt_ctf_notif_iter
*notit
)
970 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
971 struct bt_ctf_field_type
*packet_header_type
= NULL
;
972 struct bt_ctf_field_type
*stream_id_field_type
= NULL
;
973 struct bt_ctf_stream_class
*new_stream_class
= NULL
;
976 /* Clear the current stream class field path cache. */
977 notit
->cur_sc_field_path_cache
= NULL
;
979 /* Is there any "stream_id" field in the packet header? */
980 packet_header_type
= bt_ctf_trace_get_packet_header_type(
982 if (!packet_header_type
) {
984 * No packet header, therefore no `stream_id` field,
985 * therefore only one stream class.
987 goto single_stream_class
;
990 assert(is_struct_type(packet_header_type
));
993 stream_id_field_type
=
994 bt_ctf_field_type_structure_get_field_type_by_name(
995 packet_header_type
, "stream_id");
996 if (stream_id_field_type
) {
997 /* Find appropriate stream class using current stream ID */
999 struct bt_ctf_field
*stream_id_field
= NULL
;
1001 assert(notit
->dscopes
.trace_packet_header
);
1003 // TODO: optimalize!
1004 stream_id_field
= bt_ctf_field_structure_get_field(
1005 notit
->dscopes
.trace_packet_header
, "stream_id");
1006 assert(stream_id_field
);
1007 ret
= bt_ctf_field_unsigned_integer_get_value(
1008 stream_id_field
, &stream_id
);
1010 BT_PUT(stream_id_field
);
1012 single_stream_class
:
1013 /* Only one stream: pick the first stream class */
1014 assert(bt_ctf_trace_get_stream_class_count(
1015 notit
->meta
.trace
) == 1);
1019 BT_LOGV("Found stream class ID to use: notit-addr=%p, "
1020 "stream-class-id=%" PRIu64
", "
1021 "trace-addr=%p, trace-name=\"%s\"",
1022 notit
, stream_id
, notit
->meta
.trace
,
1023 bt_ctf_trace_get_name(notit
->meta
.trace
));
1025 new_stream_class
= bt_ctf_trace_get_stream_class_by_id(
1026 notit
->meta
.trace
, stream_id
);
1027 if (!new_stream_class
) {
1028 BT_LOGW("No stream class with ID of stream class ID to use in trace: "
1029 "notit-addr=%p, stream-class-id=%" PRIu64
", "
1030 "trace-addr=%p, trace-name=\"%s\"",
1031 notit
, stream_id
, notit
->meta
.trace
,
1032 bt_ctf_trace_get_name(notit
->meta
.trace
));
1033 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1037 if (notit
->meta
.stream_class
) {
1038 if (new_stream_class
!= notit
->meta
.stream_class
) {
1039 BT_LOGW("Two packets refer to two different stream classes within the same packet sequence: "
1040 "notit-addr=%p, prev-stream-class-addr=%p, "
1041 "prev-stream-class-name=\"%s\", "
1042 "prev-stream-class-id=%" PRId64
", "
1043 "next-stream-class-addr=%p, "
1044 "next-stream-class-name=\"%s\", "
1045 "next-stream-class-id=%" PRId64
", "
1046 "trace-addr=%p, trace-name=\"%s\"",
1047 notit
, notit
->meta
.stream_class
,
1048 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1049 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1051 bt_ctf_stream_class_get_name(new_stream_class
),
1052 bt_ctf_stream_class_get_id(new_stream_class
),
1054 bt_ctf_trace_get_name(notit
->meta
.trace
));
1055 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1059 BT_MOVE(notit
->meta
.stream_class
, new_stream_class
);
1062 BT_LOGV("Set current stream class: "
1063 "notit-addr=%p, stream-class-addr=%p, "
1064 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
1065 notit
, notit
->meta
.stream_class
,
1066 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1067 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
1070 * Retrieve (or lazily create) the current stream class field path
1073 notit
->cur_sc_field_path_cache
= get_stream_class_field_path_cache(
1074 notit
, notit
->meta
.stream_class
);
1075 if (!notit
->cur_sc_field_path_cache
) {
1076 BT_LOGW("Cannot retrieve stream class field path from cache: "
1077 "notit-addr=%p, stream-class-addr=%p, "
1078 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
1079 notit
, notit
->meta
.stream_class
,
1080 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1081 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
1082 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1086 BT_PUT(packet_header_type
);
1087 BT_PUT(stream_id_field_type
);
1088 bt_put(new_stream_class
);
1093 enum bt_ctf_notif_iter_status
after_packet_header_state(
1094 struct bt_ctf_notif_iter
*notit
)
1096 enum bt_ctf_notif_iter_status status
;
1098 status
= set_current_stream_class(notit
);
1099 if (status
== BT_CTF_NOTIF_ITER_STATUS_OK
) {
1100 notit
->state
= STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
;
1107 enum bt_ctf_notif_iter_status
read_packet_context_begin_state(
1108 struct bt_ctf_notif_iter
*notit
)
1110 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1111 struct bt_ctf_field_type
*packet_context_type
;
1113 assert(notit
->meta
.stream_class
);
1114 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(
1115 notit
->meta
.stream_class
);
1116 if (!packet_context_type
) {
1117 BT_LOGV("No packet packet context field type in stream class: continuing: "
1118 "notit-addr=%p, stream-class-addr=%p, "
1119 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
1120 notit
, notit
->meta
.stream_class
,
1121 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1122 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
1123 notit
->state
= STATE_AFTER_STREAM_PACKET_CONTEXT
;
1127 BT_LOGV("Decoding packet context field: "
1128 "notit-addr=%p, stream-class-addr=%p, "
1129 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1131 notit
, notit
->meta
.stream_class
,
1132 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1133 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1134 packet_context_type
);
1135 status
= read_dscope_begin_state(notit
, packet_context_type
,
1136 STATE_AFTER_STREAM_PACKET_CONTEXT
,
1137 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
1138 ¬it
->dscopes
.stream_packet_context
);
1140 BT_LOGW("Cannot decode packet context field: "
1141 "notit-addr=%p, stream-class-addr=%p, "
1142 "stream-class-name=\"%s\", "
1143 "stream-class-id=%" PRId64
", ft-addr=%p",
1144 notit
, notit
->meta
.stream_class
,
1145 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1146 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1147 packet_context_type
);
1151 BT_PUT(packet_context_type
);
1156 enum bt_ctf_notif_iter_status
read_packet_context_continue_state(
1157 struct bt_ctf_notif_iter
*notit
)
1159 return read_dscope_continue_state(notit
,
1160 STATE_AFTER_STREAM_PACKET_CONTEXT
);
1164 enum bt_ctf_notif_iter_status
set_current_packet_content_sizes(
1165 struct bt_ctf_notif_iter
*notit
)
1167 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1168 struct bt_ctf_field
*packet_size_field
= NULL
;
1169 struct bt_ctf_field
*content_size_field
= NULL
;
1170 uint64_t content_size
= -1ULL, packet_size
= -1ULL;
1172 if (!notit
->dscopes
.stream_packet_context
) {
1176 packet_size_field
= bt_ctf_field_structure_get_field(
1177 notit
->dscopes
.stream_packet_context
, "packet_size");
1178 content_size_field
= bt_ctf_field_structure_get_field(
1179 notit
->dscopes
.stream_packet_context
, "content_size");
1180 if (packet_size_field
) {
1181 int ret
= bt_ctf_field_unsigned_integer_get_value(
1182 packet_size_field
, &packet_size
);
1185 if (packet_size
== 0) {
1186 BT_LOGW("Invalid packet size: packet context field indicates packet size is zero: "
1187 "notit-addr=%p, packet-context-field-addr=%p",
1188 notit
, notit
->dscopes
.stream_packet_context
);
1189 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1191 } else if ((packet_size
% 8) != 0) {
1192 BT_LOGW("Invalid packet size: packet context field indicates packet size is not a multiple of 8: "
1193 "notit-addr=%p, packet-context-field-addr=%p, "
1194 "packet-size=%" PRIu64
,
1195 notit
, notit
->dscopes
.stream_packet_context
,
1197 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1202 if (content_size_field
) {
1203 int ret
= bt_ctf_field_unsigned_integer_get_value(
1204 content_size_field
, &content_size
);
1208 content_size
= packet_size
;
1211 if (content_size
> packet_size
) {
1212 BT_LOGW("Invalid packet or content size: packet context field indicates content size is greater than packet size: "
1213 "notit-addr=%p, packet-context-field-addr=%p, "
1214 "packet-size=%" PRIu64
", content-size=%" PRIu64
,
1215 notit
, notit
->dscopes
.stream_packet_context
,
1216 packet_size
, content_size
);
1217 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1221 if (packet_size
!= -1ULL) {
1222 notit
->cur_packet_size
= packet_size
;
1225 * Use the content size as packet size indicator if the
1226 * packet size field is missing. This means there is no
1227 * padding in this stream.
1229 notit
->cur_packet_size
= content_size
;
1231 notit
->cur_content_size
= content_size
;
1232 BT_LOGV("Set current packet and content sizes: "
1233 "notit-addr=%p, packet-size=%" PRIu64
", content-size=%" PRIu64
,
1234 notit
, packet_size
, content_size
);
1236 BT_PUT(packet_size_field
);
1237 BT_PUT(content_size_field
);
1242 enum bt_ctf_notif_iter_status
after_packet_context_state(
1243 struct bt_ctf_notif_iter
*notit
)
1245 enum bt_ctf_notif_iter_status status
;
1247 status
= set_current_packet_content_sizes(notit
);
1248 if (status
== BT_CTF_NOTIF_ITER_STATUS_OK
) {
1249 notit
->state
= STATE_EMIT_NOTIF_NEW_PACKET
;
1256 enum bt_ctf_notif_iter_status
read_event_header_begin_state(
1257 struct bt_ctf_notif_iter
*notit
)
1259 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1260 struct bt_ctf_field_type
*event_header_type
= NULL
;
1262 /* Reset the position of the last event header */
1263 notit
->buf
.last_eh_at
= notit
->buf
.at
;
1265 /* Check if we have some content left */
1266 if (notit
->cur_content_size
>= 0) {
1267 if (packet_at(notit
) == notit
->cur_content_size
) {
1268 /* No more events! */
1269 BT_LOGV("Reached end of packet: notit-addr=%p, "
1270 "cur=%zu", notit
, packet_at(notit
));
1271 notit
->state
= STATE_EMIT_NOTIF_END_OF_PACKET
;
1273 } else if (packet_at(notit
) > notit
->cur_content_size
) {
1274 /* That's not supposed to happen */
1275 BT_LOGV("Before decoding event header field: cursor is passed the packet's content: "
1276 "notit-addr=%p, content-size=%" PRId64
", "
1277 "cur=%zu", notit
, notit
->cur_content_size
,
1279 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1284 event_header_type
= bt_ctf_stream_class_get_event_header_type(
1285 notit
->meta
.stream_class
);
1286 if (!event_header_type
) {
1287 notit
->state
= STATE_AFTER_STREAM_EVENT_HEADER
;
1291 put_event_dscopes(notit
);
1292 BT_LOGV("Decoding event header field: "
1293 "notit-addr=%p, stream-class-addr=%p, "
1294 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1296 notit
, notit
->meta
.stream_class
,
1297 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1298 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1300 status
= read_dscope_begin_state(notit
, event_header_type
,
1301 STATE_AFTER_STREAM_EVENT_HEADER
,
1302 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
,
1303 ¬it
->dscopes
.stream_event_header
);
1305 BT_LOGW("Cannot decode event header field: "
1306 "notit-addr=%p, stream-class-addr=%p, "
1307 "stream-class-name=\"%s\", "
1308 "stream-class-id=%" PRId64
", ft-addr=%p",
1309 notit
, notit
->meta
.stream_class
,
1310 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1311 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1315 BT_PUT(event_header_type
);
1321 enum bt_ctf_notif_iter_status
read_event_header_continue_state(
1322 struct bt_ctf_notif_iter
*notit
)
1324 return read_dscope_continue_state(notit
,
1325 STATE_AFTER_STREAM_EVENT_HEADER
);
1329 enum bt_ctf_notif_iter_status
set_current_event_class(struct bt_ctf_notif_iter
*notit
)
1332 * The assert() calls in this function are okay because it is
1333 * assumed here that all the metadata objects have been
1334 * validated for CTF correctness before decoding actual streams.
1337 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1338 struct bt_ctf_field_type
*event_header_type
;
1339 struct bt_ctf_field_type
*id_field_type
= NULL
;
1340 struct bt_ctf_field_type
*v_field_type
= NULL
;
1341 uint64_t event_id
= -1ULL;
1344 event_header_type
= bt_ctf_stream_class_get_event_header_type(
1345 notit
->meta
.stream_class
);
1346 if (!event_header_type
) {
1348 * No event header, therefore no event class ID field,
1349 * therefore only one event class.
1351 goto single_event_class
;
1354 /* Is there any "id"/"v" field in the event header? */
1355 assert(is_struct_type(event_header_type
));
1356 id_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1357 event_header_type
, "id");
1358 v_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1359 event_header_type
, "v");
1360 assert(notit
->dscopes
.stream_event_header
);
1364 * | | |_ _|_ _| __ __ _
1365 * | | | | | || '_ \ / _` |
1366 * | |___| | | || | | | (_| | S P E C I A L
1367 * |_____|_| |_||_| |_|\__, | C A S E â„¢
1370 struct bt_ctf_field
*v_field
= NULL
;
1371 struct bt_ctf_field
*v_struct_field
= NULL
;
1372 struct bt_ctf_field
*v_struct_id_field
= NULL
;
1374 // TODO: optimalize!
1375 v_field
= bt_ctf_field_structure_get_field(
1376 notit
->dscopes
.stream_event_header
, "v");
1380 bt_ctf_field_variant_get_current_field(v_field
);
1381 if (!v_struct_field
) {
1382 goto end_v_field_type
;
1385 // TODO: optimalize!
1387 bt_ctf_field_structure_get_field(v_struct_field
, "id");
1388 if (!v_struct_id_field
) {
1389 goto end_v_field_type
;
1392 if (bt_ctf_field_is_integer(v_struct_id_field
)) {
1393 ret
= bt_ctf_field_unsigned_integer_get_value(
1394 v_struct_id_field
, &event_id
);
1396 BT_LOGV("Cannot get value of unsigned integer field (`id`): continuing: "
1397 "notit=%p, field-addr=%p",
1398 notit
, v_struct_id_field
);
1405 BT_PUT(v_struct_field
);
1406 BT_PUT(v_struct_id_field
);
1409 if (id_field_type
&& event_id
== -1ULL) {
1410 /* Check "id" field */
1411 struct bt_ctf_field
*id_field
= NULL
;
1414 // TODO: optimalize!
1415 id_field
= bt_ctf_field_structure_get_field(
1416 notit
->dscopes
.stream_event_header
, "id");
1418 goto check_event_id
;
1421 if (bt_ctf_field_is_integer(id_field
)) {
1422 ret
= bt_ctf_field_unsigned_integer_get_value(
1423 id_field
, &event_id
);
1424 } else if (bt_ctf_field_is_enumeration(id_field
)) {
1425 struct bt_ctf_field
*container
;
1427 container
= bt_ctf_field_enumeration_get_container(
1430 ret
= bt_ctf_field_unsigned_integer_get_value(
1431 container
, &event_id
);
1440 if (event_id
== -1ULL) {
1442 /* Event ID not found: single event? */
1443 assert(bt_ctf_stream_class_get_event_class_count(
1444 notit
->meta
.stream_class
) == 1);
1448 BT_LOGV("Found event class ID to use: notit-addr=%p, "
1449 "stream-class-addr=%p, stream-class-name=\"%s\", "
1450 "stream-class-id=%" PRId64
", "
1451 "event-class-id=%" PRIu64
,
1452 notit
, notit
->meta
.stream_class
,
1453 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1454 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1456 BT_PUT(notit
->meta
.event_class
);
1457 notit
->meta
.event_class
= bt_ctf_stream_class_get_event_class_by_id(
1458 notit
->meta
.stream_class
, event_id
);
1459 if (!notit
->meta
.event_class
) {
1460 BT_LOGW("No event class with ID of event class ID to use in stream class: "
1461 "notit-addr=%p, stream-class-addr=%p, "
1462 "stream-class-name=\"%s\", "
1463 "stream-class-id=%" PRId64
", "
1464 "event-class-id=%" PRIu64
,
1465 notit
, notit
->meta
.stream_class
,
1466 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1467 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1469 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1473 BT_LOGV("Set current event class: "
1474 "notit-addr=%p, event-class-addr=%p, "
1475 "event-class-name=\"%s\", event-class-id=%" PRId64
,
1476 notit
, notit
->meta
.event_class
,
1477 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
1478 bt_ctf_event_class_get_id(notit
->meta
.event_class
));
1481 BT_PUT(event_header_type
);
1482 BT_PUT(id_field_type
);
1483 BT_PUT(v_field_type
);
1489 enum bt_ctf_notif_iter_status
after_event_header_state(
1490 struct bt_ctf_notif_iter
*notit
)
1492 enum bt_ctf_notif_iter_status status
;
1494 status
= set_current_event_class(notit
);
1495 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
1499 notit
->state
= STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
;
1506 enum bt_ctf_notif_iter_status
read_stream_event_context_begin_state(
1507 struct bt_ctf_notif_iter
*notit
)
1509 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1510 struct bt_ctf_field_type
*stream_event_context_type
;
1512 stream_event_context_type
= bt_ctf_stream_class_get_event_context_type(
1513 notit
->meta
.stream_class
);
1514 if (!stream_event_context_type
) {
1515 notit
->state
= STATE_DSCOPE_EVENT_CONTEXT_BEGIN
;
1519 BT_LOGV("Decoding stream event context field: "
1520 "notit-addr=%p, stream-class-addr=%p, "
1521 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1523 notit
, notit
->meta
.stream_class
,
1524 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1525 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1526 stream_event_context_type
);
1527 status
= read_dscope_begin_state(notit
, stream_event_context_type
,
1528 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
,
1529 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
,
1530 ¬it
->dscopes
.stream_event_context
);
1532 BT_LOGW("Cannot decode stream event context field: "
1533 "notit-addr=%p, stream-class-addr=%p, "
1534 "stream-class-name=\"%s\", "
1535 "stream-class-id=%" PRId64
", ft-addr=%p",
1536 notit
, notit
->meta
.stream_class
,
1537 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1538 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1539 stream_event_context_type
);
1543 BT_PUT(stream_event_context_type
);
1549 enum bt_ctf_notif_iter_status
read_stream_event_context_continue_state(
1550 struct bt_ctf_notif_iter
*notit
)
1552 return read_dscope_continue_state(notit
,
1553 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
);
1557 enum bt_ctf_notif_iter_status
read_event_context_begin_state(
1558 struct bt_ctf_notif_iter
*notit
)
1560 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1561 struct bt_ctf_field_type
*event_context_type
;
1563 event_context_type
= bt_ctf_event_class_get_context_type(
1564 notit
->meta
.event_class
);
1565 if (!event_context_type
) {
1566 notit
->state
= STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
;
1570 BT_LOGV("Decoding event context field: "
1571 "notit-addr=%p, event-class-addr=%p, "
1572 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1574 notit
, notit
->meta
.event_class
,
1575 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
1576 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
1577 event_context_type
);
1578 status
= read_dscope_begin_state(notit
, event_context_type
,
1579 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
1580 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
,
1581 ¬it
->dscopes
.event_context
);
1583 BT_LOGW("Cannot decode event context field: "
1584 "notit-addr=%p, event-class-addr=%p, "
1585 "event-class-name=\"%s\", "
1586 "event-class-id=%" PRId64
", ft-addr=%p",
1587 notit
, notit
->meta
.event_class
,
1588 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
1589 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
1590 event_context_type
);
1594 BT_PUT(event_context_type
);
1600 enum bt_ctf_notif_iter_status
read_event_context_continue_state(
1601 struct bt_ctf_notif_iter
*notit
)
1603 return read_dscope_continue_state(notit
,
1604 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
);
1608 enum bt_ctf_notif_iter_status
read_event_payload_begin_state(
1609 struct bt_ctf_notif_iter
*notit
)
1611 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1612 struct bt_ctf_field_type
*event_payload_type
;
1614 event_payload_type
= bt_ctf_event_class_get_payload_type(
1615 notit
->meta
.event_class
);
1616 if (!event_payload_type
) {
1617 notit
->state
= STATE_EMIT_NOTIF_EVENT
;
1621 BT_LOGV("Decoding event payload field: "
1622 "notit-addr=%p, event-class-addr=%p, "
1623 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1625 notit
, notit
->meta
.event_class
,
1626 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
1627 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
1628 event_payload_type
);
1629 status
= read_dscope_begin_state(notit
, event_payload_type
,
1630 STATE_EMIT_NOTIF_EVENT
,
1631 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
1632 ¬it
->dscopes
.event_payload
);
1634 BT_LOGW("Cannot decode event payload field: "
1635 "notit-addr=%p, event-class-addr=%p, "
1636 "event-class-name=\"%s\", "
1637 "event-class-id=%" PRId64
", ft-addr=%p",
1638 notit
, notit
->meta
.event_class
,
1639 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
1640 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
1641 event_payload_type
);
1645 BT_PUT(event_payload_type
);
1651 enum bt_ctf_notif_iter_status
read_event_payload_continue_state(
1652 struct bt_ctf_notif_iter
*notit
)
1654 return read_dscope_continue_state(notit
, STATE_EMIT_NOTIF_EVENT
);
1658 enum bt_ctf_notif_iter_status
skip_packet_padding_state(
1659 struct bt_ctf_notif_iter
*notit
)
1661 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1662 size_t bits_to_skip
;
1664 assert(notit
->cur_packet_size
> 0);
1665 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1666 if (bits_to_skip
== 0) {
1667 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1670 size_t bits_to_consume
;
1672 BT_LOGV("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu",
1673 bits_to_skip
, notit
, bits_to_skip
);
1674 status
= buf_ensure_available_bits(notit
);
1675 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
1679 bits_to_consume
= MIN(buf_available_bits(notit
), bits_to_skip
);
1680 BT_LOGV("Skipping %zu bits of padding: notit-addr=%p, size=%zu",
1681 bits_to_consume
, notit
, bits_to_consume
);
1682 buf_consume_bits(notit
, bits_to_consume
);
1683 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1684 if (bits_to_skip
== 0) {
1685 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1695 enum bt_ctf_notif_iter_status
handle_state(struct bt_ctf_notif_iter
*notit
)
1697 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1698 const enum state state
= notit
->state
;
1700 BT_LOGV("Handling state: notit-addr=%p, state=%s",
1701 notit
, state_string(state
));
1703 // TODO: optimalize!
1706 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1708 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
1709 status
= read_packet_header_begin_state(notit
);
1711 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
1712 status
= read_packet_header_continue_state(notit
);
1714 case STATE_AFTER_TRACE_PACKET_HEADER
:
1715 status
= after_packet_header_state(notit
);
1717 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
1718 status
= read_packet_context_begin_state(notit
);
1720 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
1721 status
= read_packet_context_continue_state(notit
);
1723 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
1724 status
= after_packet_context_state(notit
);
1726 case STATE_EMIT_NOTIF_NEW_PACKET
:
1727 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1729 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
:
1730 status
= read_event_header_begin_state(notit
);
1732 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
:
1733 status
= read_event_header_continue_state(notit
);
1735 case STATE_AFTER_STREAM_EVENT_HEADER
:
1736 status
= after_event_header_state(notit
);
1738 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
:
1739 status
= read_stream_event_context_begin_state(notit
);
1741 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
:
1742 status
= read_stream_event_context_continue_state(notit
);
1744 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN
:
1745 status
= read_event_context_begin_state(notit
);
1747 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
:
1748 status
= read_event_context_continue_state(notit
);
1750 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
1751 status
= read_event_payload_begin_state(notit
);
1753 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
1754 status
= read_event_payload_continue_state(notit
);
1756 case STATE_EMIT_NOTIF_EVENT
:
1757 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1759 case STATE_SKIP_PACKET_PADDING
:
1760 status
= skip_packet_padding_state(notit
);
1762 case STATE_EMIT_NOTIF_END_OF_PACKET
:
1763 notit
->state
= STATE_SKIP_PACKET_PADDING
;
1766 BT_LOGD("Unknown CTF plugin notification iterator state: "
1767 "notit-addr=%p, state=%d", notit
, notit
->state
);
1771 BT_LOGV("Handled state: notit-addr=%p, status=%s, "
1772 "prev-state=%s, cur-state=%s",
1773 notit
, bt_ctf_notif_iter_status_string(status
),
1774 state_string(state
), state_string(notit
->state
));
1779 * Resets the internal state of a CTF notification iterator.
1782 void bt_ctf_notif_iter_reset(struct bt_ctf_notif_iter
*notit
)
1785 BT_LOGD("Resetting notification iterator: addr=%p", notit
);
1786 stack_clear(notit
->stack
);
1787 BT_PUT(notit
->meta
.stream_class
);
1788 BT_PUT(notit
->meta
.event_class
);
1789 BT_PUT(notit
->packet
);
1790 BT_PUT(notit
->stream
);
1791 put_all_dscopes(notit
);
1792 notit
->buf
.addr
= NULL
;
1795 notit
->buf
.last_eh_at
= SIZE_MAX
;
1796 notit
->buf
.packet_offset
= 0;
1797 notit
->state
= STATE_INIT
;
1798 notit
->cur_content_size
= -1;
1799 notit
->cur_packet_size
= -1;
1800 notit
->cur_packet_offset
= -1;
1804 int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter
*notit
)
1809 * We don't put the stream class here because we need to make
1810 * sure that all the packets processed by the same notification
1811 * iterator refer to the same stream class (the first one).
1814 if (notit
->cur_packet_size
!= -1) {
1815 notit
->cur_packet_offset
+= notit
->cur_packet_size
;
1817 BT_LOGV("Switching packet: notit-addr=%p, cur=%zu, "
1818 "packet-offset=%" PRId64
, notit
, notit
->buf
.at
,
1819 notit
->cur_packet_offset
);
1820 stack_clear(notit
->stack
);
1821 BT_PUT(notit
->meta
.event_class
);
1822 BT_PUT(notit
->packet
);
1823 BT_PUT(notit
->cur_timestamp_end
);
1824 put_all_dscopes(notit
);
1827 * Adjust current buffer so that addr points to the beginning of the new
1830 if (notit
->buf
.addr
) {
1831 size_t consumed_bytes
= (size_t) (notit
->buf
.at
/ CHAR_BIT
);
1833 /* Packets are assumed to start on a byte frontier. */
1834 if (notit
->buf
.at
% CHAR_BIT
) {
1835 BT_LOGW("Cannot switch packet: current position is not a multiple of 8: "
1836 "notit-addr=%p, cur=%zu", notit
, notit
->buf
.at
);
1841 notit
->buf
.addr
+= consumed_bytes
;
1842 notit
->buf
.sz
-= consumed_bytes
;
1844 notit
->buf
.packet_offset
= 0;
1845 BT_LOGV("Adjusted buffer: addr=%p, size=%zu",
1846 notit
->buf
.addr
, notit
->buf
.sz
);
1849 notit
->cur_content_size
= -1;
1850 notit
->cur_packet_size
= -1;
1851 notit
->cur_sc_field_path_cache
= NULL
;
1858 struct bt_ctf_field
*get_next_field(struct bt_ctf_notif_iter
*notit
)
1860 struct bt_ctf_field
*next_field
= NULL
;
1861 struct bt_ctf_field
*base_field
;
1862 struct bt_ctf_field_type
*base_type
;
1865 assert(!stack_empty(notit
->stack
));
1866 index
= stack_top(notit
->stack
)->index
;
1867 base_field
= stack_top(notit
->stack
)->base
;
1869 base_type
= bt_ctf_field_get_type(base_field
);
1872 switch (bt_ctf_field_type_get_type_id(base_type
)) {
1873 case BT_CTF_FIELD_TYPE_ID_STRUCT
:
1874 next_field
= bt_ctf_field_structure_get_field_by_index(
1877 case BT_CTF_FIELD_TYPE_ID_ARRAY
:
1878 next_field
= bt_ctf_field_array_get_field(base_field
, index
);
1880 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
1881 next_field
= bt_ctf_field_sequence_get_field(base_field
, index
);
1883 case BT_CTF_FIELD_TYPE_ID_VARIANT
:
1884 next_field
= bt_ctf_field_variant_get_current_field(base_field
);
1887 BT_LOGF("Unknown base field type ID: "
1888 "notit-addr=%p, ft-addr=%p, ft-id=%s",
1890 bt_ctf_field_type_id_string(
1891 bt_ctf_field_type_get_type_id(base_type
)));
1900 void update_clock_state(uint64_t *state
,
1901 struct bt_ctf_field
*value_field
)
1903 struct bt_ctf_field_type
*value_type
= NULL
;
1904 uint64_t requested_new_value
;
1905 uint64_t requested_new_value_mask
;
1906 uint64_t cur_value_masked
;
1907 int requested_new_value_size
;
1910 value_type
= bt_ctf_field_get_type(value_field
);
1912 assert(bt_ctf_field_type_is_integer(value_type
));
1913 requested_new_value_size
=
1914 bt_ctf_field_type_integer_get_size(value_type
);
1915 assert(requested_new_value_size
> 0);
1916 ret
= bt_ctf_field_unsigned_integer_get_value(value_field
,
1917 &requested_new_value
);
1921 * Special case for a 64-bit new value, which is the limit
1922 * of a clock value as of this version: overwrite the
1923 * current value directly.
1925 if (requested_new_value_size
== 64) {
1926 *state
= requested_new_value
;
1930 requested_new_value_mask
= (1ULL << requested_new_value_size
) - 1;
1931 cur_value_masked
= *state
& requested_new_value_mask
;
1933 if (requested_new_value
< cur_value_masked
) {
1935 * It looks like a wrap happened on the number of bits
1936 * of the requested new value. Assume that the clock
1937 * value wrapped only one time.
1939 *state
+= requested_new_value_mask
+ 1;
1942 /* Clear the low bits of the current clock value. */
1943 *state
&= ~requested_new_value_mask
;
1945 /* Set the low bits of the current clock value. */
1946 *state
|= requested_new_value
;
1948 BT_LOGV("Updated clock's value from integer field's value: "
1949 "value=%" PRIu64
, *state
);
1954 enum bt_ctf_btr_status
update_clock(struct bt_ctf_notif_iter
*notit
,
1955 struct bt_ctf_field
*int_field
)
1957 gboolean clock_class_found
;
1958 uint64_t *clock_state
= NULL
;
1959 struct bt_ctf_field_type
*int_field_type
= NULL
;
1960 enum bt_ctf_btr_status ret
= BT_CTF_BTR_STATUS_OK
;
1961 struct bt_ctf_clock_class
*clock_class
= NULL
;
1963 int_field_type
= bt_ctf_field_get_type(int_field
);
1964 assert(int_field_type
);
1965 clock_class
= bt_ctf_field_type_integer_get_mapped_clock_class(
1967 if (likely(!clock_class
)) {
1971 clock_class_found
= g_hash_table_lookup_extended(notit
->clock_states
,
1972 clock_class
, NULL
, (gpointer
) &clock_state
);
1973 if (!clock_class_found
) {
1974 clock_state
= g_new0(uint64_t, 1);
1976 BT_LOGE_STR("Failed to allocate a uint64_t.");
1977 ret
= BT_CTF_BTR_STATUS_ENOMEM
;
1981 g_hash_table_insert(notit
->clock_states
, bt_get(clock_class
),
1985 /* Update the clock's state. */
1986 BT_LOGV("Updating notification iterator's clock's value from integer field: "
1987 "notit-addr=%p, clock-class-addr=%p, "
1988 "clock-class-name=\"%s\", value=%" PRIu64
,
1990 bt_ctf_clock_class_get_name(clock_class
), *clock_state
);
1991 update_clock_state(clock_state
, int_field
);
1993 bt_put(int_field_type
);
1994 bt_put(clock_class
);
1999 enum bt_ctf_btr_status
btr_unsigned_int_common(uint64_t value
,
2000 struct bt_ctf_field_type
*type
, void *data
,
2001 struct bt_ctf_field
**out_int_field
)
2003 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2004 struct bt_ctf_field
*field
= NULL
;
2005 struct bt_ctf_field
*int_field
= NULL
;
2006 struct bt_ctf_notif_iter
*notit
= data
;
2009 BT_LOGV("Common unsigned integer function called from BTR: "
2010 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2011 "ft-id=%s, value=%" PRIu64
,
2012 notit
, notit
->btr
, type
,
2013 bt_ctf_field_type_id_string(
2014 bt_ctf_field_type_get_type_id(type
)),
2017 /* Create next field */
2018 field
= get_next_field(notit
);
2020 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2021 status
= BT_CTF_BTR_STATUS_ERROR
;
2025 switch(bt_ctf_field_type_get_type_id(type
)) {
2026 case BT_CTF_FIELD_TYPE_ID_INTEGER
:
2027 /* Integer field is created field */
2028 BT_MOVE(int_field
, field
);
2031 case BT_CTF_FIELD_TYPE_ID_ENUM
:
2032 int_field
= bt_ctf_field_enumeration_get_container(field
);
2034 type
= bt_ctf_field_get_type(int_field
);
2038 BT_LOGF("Unexpected field type ID: "
2039 "notit-addr=%p, ft-addr=%p, ft-id=%s",
2041 bt_ctf_field_type_id_string(
2042 bt_ctf_field_type_get_type_id(type
)));
2047 ret
= bt_ctf_field_unsigned_integer_set_value(int_field
, value
);
2049 stack_top(notit
->stack
)->index
++;
2050 *out_int_field
= int_field
;
2059 enum bt_ctf_btr_status
btr_timestamp_end_cb(void *value
,
2060 struct bt_ctf_field_type
*type
, void *data
)
2062 enum bt_ctf_btr_status status
;
2063 struct bt_ctf_field
*field
= NULL
;
2064 struct bt_ctf_notif_iter
*notit
= data
;
2066 BT_LOGV("`timestamp_end` unsigned integer function called from BTR: "
2067 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2069 notit
, notit
->btr
, type
,
2070 bt_ctf_field_type_id_string(
2071 bt_ctf_field_type_get_type_id(type
)));
2072 status
= btr_unsigned_int_common(*((uint64_t *) value
), type
, data
,
2075 /* Set as the current packet's timestamp_end field. */
2076 BT_MOVE(notit
->cur_timestamp_end
, field
);
2081 enum bt_ctf_btr_status
btr_unsigned_int_cb(uint64_t value
,
2082 struct bt_ctf_field_type
*type
, void *data
)
2084 struct bt_ctf_notif_iter
*notit
= data
;
2085 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2086 struct bt_ctf_field
*field
= NULL
;
2087 struct field_cb_override
*override
;
2089 BT_LOGV("Unsigned integer function called from BTR: "
2090 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2091 "ft-id=%s, value=%" PRIu64
,
2092 notit
, notit
->btr
, type
,
2093 bt_ctf_field_type_id_string(
2094 bt_ctf_field_type_get_type_id(type
)),
2096 override
= g_hash_table_lookup(notit
->field_overrides
, type
);
2097 if (unlikely(override
)) {
2098 /* Override function logs errors */
2099 status
= override
->func(&value
, type
, override
->data
);
2103 status
= btr_unsigned_int_common(value
, type
, data
, &field
);
2104 if (status
!= BT_CTF_BTR_STATUS_OK
) {
2105 /* btr_unsigned_int_common() logs errors */
2109 status
= update_clock(notit
, field
);
2116 enum bt_ctf_btr_status
btr_signed_int_cb(int64_t value
,
2117 struct bt_ctf_field_type
*type
, void *data
)
2119 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2120 struct bt_ctf_field
*field
= NULL
;
2121 struct bt_ctf_field
*int_field
= NULL
;
2122 struct bt_ctf_notif_iter
*notit
= data
;
2125 BT_LOGV("Signed integer function called from BTR: "
2126 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2127 "ft-id=%s, value=%" PRId64
,
2128 notit
, notit
->btr
, type
,
2129 bt_ctf_field_type_id_string(
2130 bt_ctf_field_type_get_type_id(type
)),
2133 /* create next field */
2134 field
= get_next_field(notit
);
2136 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2137 status
= BT_CTF_BTR_STATUS_ERROR
;
2141 switch(bt_ctf_field_type_get_type_id(type
)) {
2142 case BT_CTF_FIELD_TYPE_ID_INTEGER
:
2143 /* Integer field is created field */
2144 BT_MOVE(int_field
, field
);
2147 case BT_CTF_FIELD_TYPE_ID_ENUM
:
2148 int_field
= bt_ctf_field_enumeration_get_container(field
);
2150 type
= bt_ctf_field_get_type(int_field
);
2154 BT_LOGF("Unexpected field type ID: "
2155 "notit-addr=%p, ft-addr=%p, ft-id=%s",
2157 bt_ctf_field_type_id_string(
2158 bt_ctf_field_type_get_type_id(type
)));
2163 ret
= bt_ctf_field_signed_integer_set_value(int_field
, value
);
2165 stack_top(notit
->stack
)->index
++;
2166 status
= update_clock(notit
, int_field
);
2176 enum bt_ctf_btr_status
btr_floating_point_cb(double value
,
2177 struct bt_ctf_field_type
*type
, void *data
)
2179 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2180 struct bt_ctf_field
*field
= NULL
;
2181 struct bt_ctf_notif_iter
*notit
= data
;
2184 BT_LOGV("Floating point number function called from BTR: "
2185 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2186 "ft-id=%s, value=%f",
2187 notit
, notit
->btr
, type
,
2188 bt_ctf_field_type_id_string(
2189 bt_ctf_field_type_get_type_id(type
)),
2192 /* Create next field */
2193 field
= get_next_field(notit
);
2195 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2196 status
= BT_CTF_BTR_STATUS_ERROR
;
2200 ret
= bt_ctf_field_floating_point_set_value(field
, value
);
2202 stack_top(notit
->stack
)->index
++;
2210 enum bt_ctf_btr_status
btr_string_begin_cb(
2211 struct bt_ctf_field_type
*type
, void *data
)
2213 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2214 struct bt_ctf_field
*field
= NULL
;
2215 struct bt_ctf_notif_iter
*notit
= data
;
2218 BT_LOGV("String (beginning) function called from BTR: "
2219 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2221 notit
, notit
->btr
, type
,
2222 bt_ctf_field_type_id_string(
2223 bt_ctf_field_type_get_type_id(type
)));
2225 /* Create next field */
2226 field
= get_next_field(notit
);
2228 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2229 status
= BT_CTF_BTR_STATUS_ERROR
;
2234 * Push on stack. Not a compound type per se, but we know that only
2235 * btr_string_cb() may be called between this call and a subsequent
2236 * call to btr_string_end_cb().
2238 ret
= stack_push(notit
->stack
, field
);
2240 BT_LOGE("Cannot push string field on stack: "
2241 "notit-addr=%p, field-addr=%p", notit
, field
);
2242 status
= BT_CTF_BTR_STATUS_ERROR
;
2247 * Initialize string field payload to an empty string since in the
2248 * case of a length 0 string the btr_string_cb won't be called and
2249 * we will end up with an unset string payload.
2251 ret
= bt_ctf_field_string_set_value(field
, "");
2253 BT_LOGE("Cannot initialize string field's value to an empty string: "
2254 "notit-addr=%p, field-addr=%p, ret=%d",
2256 status
= BT_CTF_BTR_STATUS_ERROR
;
2267 enum bt_ctf_btr_status
btr_string_cb(const char *value
,
2268 size_t len
, struct bt_ctf_field_type
*type
, void *data
)
2270 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2271 struct bt_ctf_field
*field
= NULL
;
2272 struct bt_ctf_notif_iter
*notit
= data
;
2275 BT_LOGV("String (substring) function called from BTR: "
2276 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2277 "ft-id=%s, string-length=%zu",
2278 notit
, notit
->btr
, type
,
2279 bt_ctf_field_type_id_string(
2280 bt_ctf_field_type_get_type_id(type
)),
2283 /* Get string field */
2284 field
= stack_top(notit
->stack
)->base
;
2287 /* Append current string */
2288 ret
= bt_ctf_field_string_append_len(field
, value
, len
);
2290 BT_LOGE("Cannot append substring to string field's value: "
2291 "notit-addr=%p, field-addr=%p, string-length=%zu, "
2292 "ret=%d", notit
, field
, len
, ret
);
2293 status
= BT_CTF_BTR_STATUS_ERROR
;
2302 enum bt_ctf_btr_status
btr_string_end_cb(
2303 struct bt_ctf_field_type
*type
, void *data
)
2305 struct bt_ctf_notif_iter
*notit
= data
;
2307 BT_LOGV("String (end) function called from BTR: "
2308 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2310 notit
, notit
->btr
, type
,
2311 bt_ctf_field_type_id_string(
2312 bt_ctf_field_type_get_type_id(type
)));
2314 /* Pop string field */
2315 stack_pop(notit
->stack
);
2317 /* Go to next field */
2318 stack_top(notit
->stack
)->index
++;
2319 return BT_CTF_BTR_STATUS_OK
;
2322 enum bt_ctf_btr_status
btr_compound_begin_cb(
2323 struct bt_ctf_field_type
*type
, void *data
)
2325 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2326 struct bt_ctf_notif_iter
*notit
= data
;
2327 struct bt_ctf_field
*field
;
2330 BT_LOGV("Compound (beginning) function called from BTR: "
2331 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2333 notit
, notit
->btr
, type
,
2334 bt_ctf_field_type_id_string(
2335 bt_ctf_field_type_get_type_id(type
)));
2338 if (stack_empty(notit
->stack
)) {
2339 /* Root: create dynamic scope field */
2340 *notit
->cur_dscope_field
= bt_ctf_field_create(type
);
2341 field
= *notit
->cur_dscope_field
;
2344 * Field will be put at the end of this function
2345 * (stack_push() will take one reference, but this
2346 * reference is lost upon the equivalent stack_pop()
2347 * later), so also get it for our context to own it.
2349 bt_get(*notit
->cur_dscope_field
);
2352 BT_LOGE("Cannot create compound field: "
2353 "notit-addr=%p, ft-addr=%p, ft-id=%s",
2355 bt_ctf_field_type_id_string(
2356 bt_ctf_field_type_get_type_id(type
)));
2357 status
= BT_CTF_BTR_STATUS_ERROR
;
2361 field
= get_next_field(notit
);
2363 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2364 status
= BT_CTF_BTR_STATUS_ERROR
;
2371 ret
= stack_push(notit
->stack
, field
);
2373 BT_LOGE("Cannot push compound field onto the stack: "
2374 "notit-addr=%p, ft-addr=%p, ft-id=%s, ret=%d",
2376 bt_ctf_field_type_id_string(
2377 bt_ctf_field_type_get_type_id(type
)),
2379 status
= BT_CTF_BTR_STATUS_ERROR
;
2389 enum bt_ctf_btr_status
btr_compound_end_cb(
2390 struct bt_ctf_field_type
*type
, void *data
)
2392 struct bt_ctf_notif_iter
*notit
= data
;
2394 BT_LOGV("Compound (end) function called from BTR: "
2395 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2397 notit
, notit
->btr
, type
,
2398 bt_ctf_field_type_id_string(
2399 bt_ctf_field_type_get_type_id(type
)));
2400 assert(!stack_empty(notit
->stack
));
2403 stack_pop(notit
->stack
);
2405 /* If the stack is not empty, increment the base's index */
2406 if (!stack_empty(notit
->stack
)) {
2407 stack_top(notit
->stack
)->index
++;
2410 return BT_CTF_BTR_STATUS_OK
;
2414 struct bt_ctf_field
*resolve_field(struct bt_ctf_notif_iter
*notit
,
2415 struct bt_ctf_field_path
*path
)
2417 struct bt_ctf_field
*field
= NULL
;
2420 if (BT_LOG_ON_VERBOSE
) {
2421 GString
*gstr
= bt_ctf_field_path_string(path
);
2423 BT_LOGV("Resolving field path: notit-addr=%p, field-path=\"%s\"",
2424 notit
, gstr
? gstr
->str
: NULL
);
2427 g_string_free(gstr
, TRUE
);
2431 switch (bt_ctf_field_path_get_root_scope(path
)) {
2432 case BT_CTF_SCOPE_TRACE_PACKET_HEADER
:
2433 field
= notit
->dscopes
.trace_packet_header
;
2435 case BT_CTF_SCOPE_STREAM_PACKET_CONTEXT
:
2436 field
= notit
->dscopes
.stream_packet_context
;
2438 case BT_CTF_SCOPE_STREAM_EVENT_HEADER
:
2439 field
= notit
->dscopes
.stream_event_header
;
2441 case BT_CTF_SCOPE_STREAM_EVENT_CONTEXT
:
2442 field
= notit
->dscopes
.stream_event_context
;
2444 case BT_CTF_SCOPE_EVENT_CONTEXT
:
2445 field
= notit
->dscopes
.event_context
;
2447 case BT_CTF_SCOPE_EVENT_FIELDS
:
2448 field
= notit
->dscopes
.event_payload
;
2451 BT_LOGF("Cannot resolve field path: unknown scope: "
2452 "notit-addr=%p, root-scope=%s",
2453 notit
, bt_ctf_scope_string(
2454 bt_ctf_field_path_get_root_scope(path
)));
2459 BT_LOGW("Cannot resolve field path: root field not found: "
2460 "notit-addr=%p, root-scope=%s",
2461 notit
, bt_ctf_scope_string(
2462 bt_ctf_field_path_get_root_scope(path
)));
2468 for (i
= 0; i
< bt_ctf_field_path_get_index_count(path
); ++i
) {
2469 struct bt_ctf_field
*next_field
= NULL
;
2470 struct bt_ctf_field_type
*field_type
;
2471 int index
= bt_ctf_field_path_get_index(path
, i
);
2473 field_type
= bt_ctf_field_get_type(field
);
2476 if (is_struct_type(field_type
)) {
2477 next_field
= bt_ctf_field_structure_get_field_by_index(
2479 } else if (is_variant_type(field_type
)) {
2481 bt_ctf_field_variant_get_current_field(field
);
2488 BT_LOGW("Cannot find next field: "
2489 "notit-addr=%p, ft-addr=%p, ft-id=%s, index=%d",
2491 bt_ctf_field_type_id_string(
2492 bt_ctf_field_type_get_type_id(field_type
)),
2497 /* Move next field -> field */
2498 BT_MOVE(field
, next_field
);
2506 int64_t btr_get_sequence_length_cb(struct bt_ctf_field_type
*type
, void *data
)
2510 struct bt_ctf_field
*seq_field
;
2511 struct bt_ctf_field_path
*field_path
;
2512 struct bt_ctf_notif_iter
*notit
= data
;
2513 struct bt_ctf_field
*length_field
= NULL
;
2516 field_path
= bt_ctf_field_type_sequence_get_length_field_path(type
);
2518 length_field
= resolve_field(notit
, field_path
);
2519 if (!length_field
) {
2520 BT_LOGW("Cannot resolve sequence field type's length field path: "
2521 "notit-addr=%p, ft-addr=%p",
2526 iret
= bt_ctf_field_unsigned_integer_get_value(length_field
, &length
);
2528 BT_LOGE("Cannot get value of sequence length field: "
2529 "notit-addr=%p, field-addr=%p",
2530 notit
, length_field
);
2534 seq_field
= stack_top(notit
->stack
)->base
;
2535 iret
= bt_ctf_field_sequence_set_length(seq_field
, length_field
);
2537 BT_LOGE("Cannot set sequence field's length field: "
2538 "notit-addr=%p, seq-field-addr=%p, "
2539 "length-field-addr=%p, ",
2540 notit
, seq_field
, length_field
);
2544 ret
= (int64_t) length
;
2547 BT_PUT(length_field
);
2554 struct bt_ctf_field_type
*btr_get_variant_type_cb(
2555 struct bt_ctf_field_type
*type
, void *data
)
2557 struct bt_ctf_field_path
*path
;
2558 struct bt_ctf_notif_iter
*notit
= data
;
2559 struct bt_ctf_field
*var_field
;
2560 struct bt_ctf_field
*tag_field
= NULL
;
2561 struct bt_ctf_field
*selected_field
= NULL
;
2562 struct bt_ctf_field_type
*selected_field_type
= NULL
;
2564 path
= bt_ctf_field_type_variant_get_tag_field_path(type
);
2566 tag_field
= resolve_field(notit
, path
);
2568 BT_LOGW("Cannot resolve variant field type's tag field path: "
2569 "notit-addr=%p, ft-addr=%p",
2575 * We found the enumeration tag field instance which should be
2576 * able to select a current field for this variant. This
2577 * callback function we're in is called _after_
2578 * compound_begin(), so the current stack top's base field is
2579 * the variant field in question. We get the selected field here
2580 * thanks to this tag field (thus creating the selected field),
2581 * which will also provide us with its type. Then, this field
2582 * will remain the current selected one until the next callback
2583 * function call which is used to fill the current selected
2586 var_field
= stack_top(notit
->stack
)->base
;
2587 selected_field
= bt_ctf_field_variant_get_field(var_field
, tag_field
);
2588 if (!selected_field
) {
2589 BT_LOGW("Cannot get variant field's selection using tag field: "
2590 "notit-addr=%p, var-field-addr=%p, tag-field-addr=%p",
2591 notit
, var_field
, tag_field
);
2595 selected_field_type
= bt_ctf_field_get_type(selected_field
);
2599 BT_PUT(selected_field
);
2602 return selected_field_type
;
2606 int set_event_clocks(struct bt_ctf_event
*event
,
2607 struct bt_ctf_notif_iter
*notit
)
2610 GHashTableIter iter
;
2611 struct bt_ctf_clock_class
*clock_class
;
2612 uint64_t *clock_state
;
2614 g_hash_table_iter_init(&iter
, notit
->clock_states
);
2616 while (g_hash_table_iter_next(&iter
, (gpointer
) &clock_class
,
2617 (gpointer
) &clock_state
)) {
2618 struct bt_ctf_clock_value
*clock_value
;
2620 clock_value
= bt_ctf_clock_value_create(clock_class
,
2623 BT_LOGE("Cannot create clock value from clock class: "
2624 "notit-addr=%p, clock-class-addr=%p, "
2625 "clock-class-name=\"%s\"",
2627 bt_ctf_clock_class_get_name(clock_class
));
2631 ret
= bt_ctf_event_set_clock_value(event
, clock_value
);
2632 bt_put(clock_value
);
2634 struct bt_ctf_event_class
*event_class
=
2635 bt_ctf_event_get_class(event
);
2637 assert(event_class
);
2638 BT_LOGE("Cannot set event's clock value: "
2639 "notit-addr=%p, event-addr=%p, "
2640 "event-class-name=\"%s\", "
2641 "event-class-id=%" PRId64
", "
2642 "clock-class-addr=%p, "
2643 "clock-class-name=\"%s\", "
2644 "clock-value-addr=%p",
2646 bt_ctf_event_class_get_name(event_class
),
2647 bt_ctf_event_class_get_id(event_class
),
2649 bt_ctf_clock_class_get_name(clock_class
),
2651 bt_put(event_class
);
2662 struct bt_ctf_event
*create_event(struct bt_ctf_notif_iter
*notit
)
2665 struct bt_ctf_event
*event
;
2667 BT_LOGV("Creating event for event notification: "
2668 "notit-addr=%p, event-class-addr=%p, "
2669 "event-class-name=\"%s\", "
2670 "event-class-id=%" PRId64
,
2671 notit
, notit
->meta
.event_class
,
2672 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2673 bt_ctf_event_class_get_id(notit
->meta
.event_class
));
2675 /* Create event object. */
2676 event
= bt_ctf_event_create(notit
->meta
.event_class
);
2678 BT_LOGE("Cannot create event: "
2679 "notit-addr=%p, event-class-addr=%p, "
2680 "event-class-name=\"%s\", "
2681 "event-class-id=%" PRId64
,
2682 notit
, notit
->meta
.event_class
,
2683 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2684 bt_ctf_event_class_get_id(notit
->meta
.event_class
));
2688 /* Set header, stream event context, context, and payload fields. */
2689 ret
= bt_ctf_event_set_header(event
,
2690 notit
->dscopes
.stream_event_header
);
2692 BT_LOGE("Cannot set event's header field: "
2693 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2694 "event-class-name=\"%s\", "
2695 "event-class-id=%" PRId64
", field-addr=%p",
2696 notit
, event
, notit
->meta
.event_class
,
2697 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2698 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
2699 notit
->dscopes
.stream_event_header
);
2703 ret
= bt_ctf_event_set_stream_event_context(event
,
2704 notit
->dscopes
.stream_event_context
);
2706 BT_LOGE("Cannot set event's context field: "
2707 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2708 "event-class-name=\"%s\", "
2709 "event-class-id=%" PRId64
", field-addr=%p",
2710 notit
, event
, notit
->meta
.event_class
,
2711 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2712 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
2713 notit
->dscopes
.stream_event_context
);
2717 ret
= bt_ctf_event_set_event_context(event
,
2718 notit
->dscopes
.event_context
);
2720 BT_LOGE("Cannot set event's stream event context field: "
2721 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2722 "event-class-name=\"%s\", "
2723 "event-class-id=%" PRId64
", field-addr=%p",
2724 notit
, event
, notit
->meta
.event_class
,
2725 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2726 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
2727 notit
->dscopes
.event_context
);
2731 ret
= bt_ctf_event_set_event_payload(event
,
2732 notit
->dscopes
.event_payload
);
2734 BT_LOGE("Cannot set event's payload field: "
2735 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2736 "event-class-name=\"%s\", "
2737 "event-class-id=%" PRId64
", field-addr=%p",
2738 notit
, event
, notit
->meta
.event_class
,
2739 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2740 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
2741 notit
->dscopes
.event_payload
);
2745 ret
= set_event_clocks(event
, notit
);
2747 BT_LOGE("Cannot set event's clock values: "
2748 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2749 "event-class-name=\"%s\", "
2750 "event-class-id=%" PRId64
,
2751 notit
, event
, notit
->meta
.event_class
,
2752 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2753 bt_ctf_event_class_get_id(notit
->meta
.event_class
));
2757 /* Associate with current packet. */
2758 assert(notit
->packet
);
2759 ret
= bt_ctf_event_set_packet(event
, notit
->packet
);
2761 BT_LOGE("Cannot set event's header field: "
2762 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2763 "event-class-name=\"%s\", "
2764 "event-class-id=%" PRId64
", packet-addr=%p",
2765 notit
, event
, notit
->meta
.event_class
,
2766 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2767 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
2782 uint64_t get_cur_stream_instance_id(struct bt_ctf_notif_iter
*notit
)
2784 struct bt_ctf_field
*stream_instance_id_field
= NULL
;
2785 uint64_t stream_instance_id
= -1ULL;
2788 if (!notit
->dscopes
.trace_packet_header
) {
2792 stream_instance_id_field
= bt_ctf_field_structure_get_field_by_name(
2793 notit
->dscopes
.trace_packet_header
, "stream_instance_id");
2794 if (!stream_instance_id_field
) {
2798 ret
= bt_ctf_field_unsigned_integer_get_value(stream_instance_id_field
,
2799 &stream_instance_id
);
2801 stream_instance_id
= -1ULL;
2806 bt_put(stream_instance_id_field
);
2807 return stream_instance_id
;
2811 int set_stream(struct bt_ctf_notif_iter
*notit
)
2814 struct bt_ctf_stream
*stream
= NULL
;
2816 BT_LOGV("Calling user function (get stream): notit-addr=%p, "
2817 "stream-class-addr=%p, stream-class-name=\"%s\", "
2818 "stream-class-id=%" PRId64
,
2819 notit
, notit
->meta
.stream_class
,
2820 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
2821 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
2822 stream
= bt_get(notit
->medium
.medops
.get_stream(
2823 notit
->meta
.stream_class
, get_cur_stream_instance_id(notit
),
2824 notit
->medium
.data
));
2825 BT_LOGV("User function returned: stream-addr=%p", stream
);
2827 BT_LOGW_STR("User function failed to return a stream object for the given stream class.");
2832 if (notit
->stream
&& stream
!= notit
->stream
) {
2833 BT_LOGW("User function returned a different stream than the previous one for the same sequence of packets.");
2838 BT_MOVE(notit
->stream
, stream
);
2846 void create_packet(struct bt_ctf_notif_iter
*notit
)
2849 struct bt_ctf_packet
*packet
= NULL
;
2851 BT_LOGV("Creating packet for packet notification: "
2852 "notit-addr=%p", notit
);
2854 /* Ask the user for the stream */
2855 ret
= set_stream(notit
);
2860 BT_LOGV("Creating packet from stream: "
2861 "notit-addr=%p, stream-addr=%p, "
2862 "stream-class-addr=%p, "
2863 "stream-class-name=\"%s\", "
2864 "stream-class-id=%" PRId64
,
2865 notit
, notit
->stream
, notit
->meta
.stream_class
,
2866 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
2867 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
2870 packet
= bt_ctf_packet_create(notit
->stream
);
2872 BT_LOGE("Cannot create packet from stream: "
2873 "notit-addr=%p, stream-addr=%p, "
2874 "stream-class-addr=%p, "
2875 "stream-class-name=\"%s\", "
2876 "stream-class-id=%" PRId64
,
2877 notit
, notit
->stream
, notit
->meta
.stream_class
,
2878 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
2879 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
2883 /* Set packet's context and header fields */
2884 if (notit
->dscopes
.trace_packet_header
) {
2885 ret
= bt_ctf_packet_set_header(packet
,
2886 notit
->dscopes
.trace_packet_header
);
2888 BT_LOGE("Cannot set packet's header field: "
2889 "notit-addr=%p, packet-addr=%p, "
2891 "stream-class-addr=%p, "
2892 "stream-class-name=\"%s\", "
2893 "stream-class-id=%" PRId64
", "
2895 notit
, packet
, notit
->stream
, notit
->meta
.stream_class
,
2896 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
2897 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
2898 notit
->dscopes
.trace_packet_header
);
2903 if (notit
->dscopes
.stream_packet_context
) {
2904 ret
= bt_ctf_packet_set_context(packet
,
2905 notit
->dscopes
.stream_packet_context
);
2907 BT_LOGE("Cannot set packet's context field: "
2908 "notit-addr=%p, packet-addr=%p, "
2910 "stream-class-addr=%p, "
2911 "stream-class-name=\"%s\", "
2912 "stream-class-id=%" PRId64
", "
2914 notit
, packet
, notit
->stream
, notit
->meta
.stream_class
,
2915 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
2916 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
2917 notit
->dscopes
.trace_packet_header
);
2928 BT_MOVE(notit
->packet
, packet
);
2932 void notify_new_packet(struct bt_ctf_notif_iter
*notit
,
2933 struct bt_notification
**notification
)
2935 struct bt_notification
*ret
;
2937 /* Initialize the iterator's current packet */
2938 create_packet(notit
);
2939 if (!notit
->packet
) {
2940 BT_LOGE("Cannot create packet for packet notification: "
2941 "notit-addr=%p", notit
);
2945 ret
= bt_notification_packet_begin_create(notit
->packet
);
2947 BT_LOGE("Cannot create packet beginning notification: "
2948 "notit-addr=%p, packet-addr=%p",
2949 notit
, notit
->packet
);
2952 *notification
= ret
;
2956 void notify_end_of_packet(struct bt_ctf_notif_iter
*notit
,
2957 struct bt_notification
**notification
)
2959 struct bt_notification
*ret
;
2961 if (!notit
->packet
) {
2965 ret
= bt_notification_packet_end_create(notit
->packet
);
2967 BT_LOGE("Cannot create packet end notification: "
2968 "notit-addr=%p, packet-addr=%p",
2969 notit
, notit
->packet
);
2972 BT_PUT(notit
->packet
);
2973 *notification
= ret
;
2977 void notify_event(struct bt_ctf_notif_iter
*notit
,
2978 struct bt_clock_class_priority_map
*cc_prio_map
,
2979 struct bt_notification
**notification
)
2981 struct bt_ctf_event
*event
= NULL
;
2982 struct bt_notification
*ret
= NULL
;
2984 /* Make sure that the event contains at least one bit of data */
2985 if (notit
->buf
.at
== notit
->buf
.last_eh_at
) {
2986 BT_LOGE("Cannot create empty event with 0 bits of data: "
2987 "notit-addr=%p, packet-cur=%zu",
2988 notit
, packet_at(notit
));
2993 event
= create_event(notit
);
2995 BT_LOGE("Cannot create event for event notification: "
2996 "notit-addr=%p", notit
);
3000 ret
= bt_notification_event_create(event
, cc_prio_map
);
3002 BT_LOGE("Cannot create event notification: "
3003 "notit-addr=%p, event-addr=%p, "
3004 "cc-prio-map-addr=%p",
3005 notit
, event
, cc_prio_map
);
3008 *notification
= ret
;
3014 void init_trace_field_path_cache(struct bt_ctf_trace
*trace
,
3015 struct trace_field_path_cache
*trace_field_path_cache
)
3018 int stream_instance_id
= -1;
3020 struct bt_ctf_field_type
*packet_header
= NULL
;
3022 packet_header
= bt_ctf_trace_get_packet_header_type(trace
);
3023 if (!packet_header
) {
3027 if (!bt_ctf_field_type_is_structure(packet_header
)) {
3031 count
= bt_ctf_field_type_structure_get_field_count(packet_header
);
3034 for (i
= 0; (i
< count
&& (stream_id
== -1 || stream_instance_id
== -1)); i
++) {
3036 const char *field_name
;
3038 ret
= bt_ctf_field_type_structure_get_field(packet_header
,
3039 &field_name
, NULL
, i
);
3041 BT_LOGE("Cannot get structure field's field: "
3042 "field-addr=%p, index=%d",
3047 if (stream_id
== -1 && !strcmp(field_name
, "stream_id")) {
3049 } else if (stream_instance_id
== -1 &&
3050 !strcmp(field_name
, "stream_instance_id")) {
3051 stream_instance_id
= i
;
3056 trace_field_path_cache
->stream_id
= stream_id
;
3057 trace_field_path_cache
->stream_instance_id
= stream_instance_id
;
3058 BT_PUT(packet_header
);
3062 struct bt_ctf_notif_iter
*bt_ctf_notif_iter_create(struct bt_ctf_trace
*trace
,
3063 size_t max_request_sz
,
3064 struct bt_ctf_notif_iter_medium_ops medops
, void *data
)
3066 struct bt_ctf_notif_iter
*notit
= NULL
;
3067 struct bt_ctf_btr_cbs cbs
= {
3069 .signed_int
= btr_signed_int_cb
,
3070 .unsigned_int
= btr_unsigned_int_cb
,
3071 .floating_point
= btr_floating_point_cb
,
3072 .string_begin
= btr_string_begin_cb
,
3073 .string
= btr_string_cb
,
3074 .string_end
= btr_string_end_cb
,
3075 .compound_begin
= btr_compound_begin_cb
,
3076 .compound_end
= btr_compound_end_cb
,
3079 .get_sequence_length
= btr_get_sequence_length_cb
,
3080 .get_variant_type
= btr_get_variant_type_cb
,
3085 assert(medops
.request_bytes
);
3086 assert(medops
.get_stream
);
3087 BT_LOGD("Creating CTF plugin notification iterator: "
3088 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
3090 trace
, bt_ctf_trace_get_name(trace
), max_request_sz
, data
);
3091 notit
= g_new0(struct bt_ctf_notif_iter
, 1);
3093 BT_LOGE_STR("Failed to allocate one CTF plugin notification iterator.");
3096 notit
->clock_states
= g_hash_table_new_full(g_direct_hash
,
3097 g_direct_equal
, bt_put
, g_free
);
3098 if (!notit
->clock_states
) {
3099 BT_LOGE_STR("Failed to allocate a GHashTable.");
3102 notit
->meta
.trace
= bt_get(trace
);
3103 notit
->medium
.medops
= medops
;
3104 notit
->medium
.max_request_sz
= max_request_sz
;
3105 notit
->medium
.data
= data
;
3106 notit
->stack
= stack_new(notit
);
3107 if (!notit
->stack
) {
3108 BT_LOGE_STR("Failed to create field stack.");
3112 notit
->btr
= bt_ctf_btr_create(cbs
, notit
);
3114 BT_LOGE_STR("Failed to create binary type reader (BTR).");
3118 bt_ctf_notif_iter_reset(notit
);
3119 init_trace_field_path_cache(trace
, ¬it
->trace_field_path_cache
);
3120 notit
->sc_field_path_caches
= g_hash_table_new_full(g_direct_hash
,
3121 g_direct_equal
, bt_put
, g_free
);
3122 if (!notit
->sc_field_path_caches
) {
3123 BT_LOGE_STR("Failed to allocate a GHashTable.");
3127 notit
->field_overrides
= g_hash_table_new_full(g_direct_hash
,
3128 g_direct_equal
, bt_put
, g_free
);
3129 if (!notit
->field_overrides
) {
3130 BT_LOGE_STR("Failed to allocate a GHashTable.");
3134 BT_LOGD("Created CTF plugin notification iterator: "
3135 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
3136 "data=%p, notit-addr=%p",
3137 trace
, bt_ctf_trace_get_name(trace
), max_request_sz
, data
,
3139 notit
->cur_packet_offset
= 0;
3145 bt_ctf_notif_iter_destroy(notit
);
3150 void bt_ctf_notif_iter_destroy(struct bt_ctf_notif_iter
*notit
)
3152 BT_PUT(notit
->meta
.trace
);
3153 BT_PUT(notit
->meta
.stream_class
);
3154 BT_PUT(notit
->meta
.event_class
);
3155 BT_PUT(notit
->packet
);
3156 BT_PUT(notit
->stream
);
3157 BT_PUT(notit
->cur_timestamp_end
);
3158 put_all_dscopes(notit
);
3160 BT_LOGD("Destroying CTF plugin notification iterator: addr=%p", notit
);
3163 BT_LOGD_STR("Destroying field stack.");
3164 stack_destroy(notit
->stack
);
3168 BT_LOGD("Destroying BTR: btr-addr=%p", notit
->btr
);
3169 bt_ctf_btr_destroy(notit
->btr
);
3172 if (notit
->clock_states
) {
3173 g_hash_table_destroy(notit
->clock_states
);
3176 if (notit
->sc_field_path_caches
) {
3177 g_hash_table_destroy(notit
->sc_field_path_caches
);
3180 if (notit
->field_overrides
) {
3181 g_hash_table_destroy(notit
->field_overrides
);
3187 enum bt_ctf_notif_iter_status
bt_ctf_notif_iter_get_next_notification(
3188 struct bt_ctf_notif_iter
*notit
,
3189 struct bt_clock_class_priority_map
*cc_prio_map
,
3190 struct bt_notification
**notification
)
3192 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
3195 assert(notification
);
3197 BT_LOGV("Getting next notification: notit-addr=%p, cc-prio-map-addr=%p",
3198 notit
, cc_prio_map
);
3201 status
= handle_state(notit
);
3202 if (status
== BT_CTF_NOTIF_ITER_STATUS_AGAIN
) {
3203 BT_LOGV_STR("Medium returned BT_CTF_NOTIF_ITER_STATUS_AGAIN.");
3206 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
3207 if (status
== BT_CTF_NOTIF_ITER_STATUS_EOF
) {
3208 BT_LOGV_STR("Medium returned BT_CTF_NOTIF_ITER_STATUS_EOF.");
3210 BT_LOGW("Cannot handle state: "
3211 "notit-addr=%p, state=%s",
3212 notit
, state_string(notit
->state
));
3217 switch (notit
->state
) {
3218 case STATE_EMIT_NOTIF_NEW_PACKET
:
3219 /* notify_new_packet() logs errors */
3220 notify_new_packet(notit
, notification
);
3221 if (!*notification
) {
3222 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
3225 case STATE_EMIT_NOTIF_EVENT
:
3226 /* notify_event() logs errors */
3227 notify_event(notit
, cc_prio_map
, notification
);
3228 if (!*notification
) {
3229 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
3232 case STATE_EMIT_NOTIF_END_OF_PACKET
:
3233 /* Update clock with timestamp_end field. */
3234 if (notit
->cur_timestamp_end
) {
3235 enum bt_ctf_btr_status btr_status
;
3236 struct bt_ctf_field_type
*field_type
=
3237 bt_ctf_field_get_type(
3238 notit
->cur_timestamp_end
);
3241 btr_status
= update_clock(notit
,
3242 notit
->cur_timestamp_end
);
3244 if (btr_status
!= BT_CTF_BTR_STATUS_OK
) {
3245 BT_LOGW("Cannot update stream's clock value: "
3246 "notit-addr=%p", notit
);
3247 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
3252 /* notify_end_of_packet() logs errors */
3253 notify_end_of_packet(notit
, notification
);
3254 if (!*notification
) {
3255 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
3259 /* Non-emitting state: continue */
3269 enum bt_ctf_notif_iter_status
bt_ctf_notif_iter_get_packet_header_context_fields(
3270 struct bt_ctf_notif_iter
*notit
,
3271 struct bt_ctf_field
**packet_header_field
,
3272 struct bt_ctf_field
**packet_context_field
)
3275 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
3279 if (notit
->state
== STATE_EMIT_NOTIF_NEW_PACKET
) {
3280 /* We're already there */
3285 status
= handle_state(notit
);
3286 if (status
== BT_CTF_NOTIF_ITER_STATUS_AGAIN
) {
3287 BT_LOGV_STR("Medium returned BT_CTF_NOTIF_ITER_STATUS_AGAIN.");
3290 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
3291 if (status
== BT_CTF_NOTIF_ITER_STATUS_EOF
) {
3292 BT_LOGV_STR("Medium returned BT_CTF_NOTIF_ITER_STATUS_EOF.");
3294 BT_LOGW("Cannot handle state: "
3295 "notit-addr=%p, state=%s",
3296 notit
, state_string(notit
->state
));
3301 switch (notit
->state
) {
3302 case STATE_EMIT_NOTIF_NEW_PACKET
:
3304 * Packet header and context fields are
3305 * potentially decoded (or they don't exist).
3309 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
3310 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
3311 case STATE_AFTER_TRACE_PACKET_HEADER
:
3312 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
3313 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
3314 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
3315 /* Non-emitting state: continue */
3319 * We should never get past the
3320 * STATE_EMIT_NOTIF_NEW_PACKET state.
3322 BT_LOGF("Unexpected state: notit-addr=%p, state=%s",
3323 notit
, state_string(notit
->state
));
3329 if (packet_header_field
) {
3330 *packet_header_field
= bt_get(notit
->dscopes
.trace_packet_header
);
3333 if (packet_context_field
) {
3334 *packet_context_field
= bt_get(notit
->dscopes
.stream_packet_context
);
3337 ret
= set_current_packet_content_sizes(notit
);
3339 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
3347 void bt_ctf_notif_iter_set_medops_data(struct bt_ctf_notif_iter
*notit
,
3351 notit
->medium
.data
= medops_data
;
3355 enum bt_ctf_notif_iter_status
bt_ctf_notif_iter_seek(
3356 struct bt_ctf_notif_iter
*notit
, off_t offset
)
3358 enum bt_ctf_notif_iter_status ret
= BT_CTF_NOTIF_ITER_STATUS_OK
;
3359 enum bt_ctf_notif_iter_medium_status medium_status
;
3363 BT_LOGE("Cannot seek to negative offset: offset=%jd", offset
);
3364 ret
= BT_CTF_NOTIF_ITER_STATUS_INVAL
;
3368 if (!notit
->medium
.medops
.seek
) {
3369 ret
= BT_CTF_NOTIF_ITER_STATUS_UNSUPPORTED
;
3370 BT_LOGD("Aborting seek as the iterator's underlying media does not implement seek support.");
3374 medium_status
= notit
->medium
.medops
.seek(
3375 BT_CTF_NOTIF_ITER_SEEK_WHENCE_SET
, offset
,
3376 notit
->medium
.data
);
3377 if (medium_status
!= BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK
) {
3378 if (medium_status
== BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF
) {
3379 ret
= BT_CTF_NOTIF_ITER_STATUS_EOF
;
3381 ret
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
3386 bt_ctf_notif_iter_reset(notit
);
3387 notit
->cur_packet_offset
= offset
;
3393 off_t
bt_ctf_notif_iter_get_current_packet_offset(
3394 struct bt_ctf_notif_iter
*notit
)
3397 return notit
->cur_packet_offset
;
3401 off_t
bt_ctf_notif_iter_get_current_packet_size(
3402 struct bt_ctf_notif_iter
*notit
)
3405 return notit
->cur_packet_size
;