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/ctf-ir/field-types.h>
37 #include <babeltrace/ctf-ir/field-types-internal.h>
38 #include <babeltrace/ctf-ir/field-path.h>
39 #include <babeltrace/ctf-ir/field-path-internal.h>
40 #include <babeltrace/ctf-ir/fields.h>
41 #include <babeltrace/ctf-ir/stream-class.h>
42 #include <babeltrace/ctf-ir/packet.h>
43 #include <babeltrace/ctf-ir/stream.h>
44 #include <babeltrace/ctf-ir/clock-class.h>
45 #include <babeltrace/ctf-ir/event-class.h>
46 #include <babeltrace/graph/notification-packet.h>
47 #include <babeltrace/graph/notification-event.h>
48 #include <babeltrace/graph/notification-stream.h>
49 #include <babeltrace/graph/clock-class-priority-map.h>
50 #include <babeltrace/ref.h>
54 #include "notif-iter.h"
55 #include "../btr/btr.h"
57 struct bt_ctf_notif_iter
;
59 /* A visit stack entry */
62 * Current base field, one of:
70 * Field is owned by this.
72 struct bt_ctf_field
*base
;
74 /* index of next field to set */
80 /* Entries (struct stack_entry *) (top is last element) */
87 STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
,
88 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
89 STATE_AFTER_TRACE_PACKET_HEADER
,
90 STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
,
91 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
92 STATE_AFTER_STREAM_PACKET_CONTEXT
,
93 STATE_EMIT_NOTIF_NEW_PACKET
,
94 STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
,
95 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
,
96 STATE_AFTER_STREAM_EVENT_HEADER
,
97 STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
,
98 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
,
99 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
,
100 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
,
101 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
102 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
103 STATE_EMIT_NOTIF_EVENT
,
104 STATE_EMIT_NOTIF_END_OF_PACKET
,
105 STATE_SKIP_PACKET_PADDING
,
108 struct trace_field_path_cache
{
110 * Indexes of the stream_id and stream_instance_id field in the packet
111 * header structure, -1 if unset.
114 int stream_instance_id
;
117 struct stream_class_field_path_cache
{
119 * Indexes of the v and id fields in the stream event header structure,
126 * index of the timestamp_end, packet_size and content_size fields in
127 * the stream packet context structure. Set to -1 if the fields were
135 struct field_cb_override
{
136 enum bt_ctf_btr_status (* func
)(void *value
,
137 struct bt_ctf_field_type
*type
, void *data
);
141 /* CTF notification iterator */
142 struct bt_ctf_notif_iter
{
147 * Current dynamic scope field pointer.
149 * This is set when a dynamic scope field is first created by
150 * btr_compound_begin_cb(). It points to one of the fields in
153 struct bt_ctf_field
**cur_dscope_field
;
155 /* Trace and classes (owned by this) */
157 struct bt_ctf_trace
*trace
;
158 struct bt_ctf_stream_class
*stream_class
;
159 struct bt_ctf_event_class
*event_class
;
162 /* Current packet (NULL if not created yet) */
163 struct bt_ctf_packet
*packet
;
165 /* Current stream (NULL if not set yet) */
166 struct bt_ctf_stream
*stream
;
169 * Current timestamp_end field (to consider before switching packets).
171 struct bt_ctf_field
*cur_timestamp_end
;
173 /* Database of current dynamic scopes (owned by this) */
175 struct bt_ctf_field
*trace_packet_header
;
176 struct bt_ctf_field
*stream_packet_context
;
177 struct bt_ctf_field
*stream_event_header
;
178 struct bt_ctf_field
*stream_event_context
;
179 struct bt_ctf_field
*event_context
;
180 struct bt_ctf_field
*event_payload
;
184 * Special field overrides.
186 * Overrides are used to implement the behaviours of special fields such
187 * as "timestamp_end" (which must be ignored until the end of the
188 * packet), "id" (event id) which can be present multiple times and must
189 * be updated multiple time.
191 * This should be used to implement the behaviour of integer fields
192 * mapped to clocks and other "tagged" fields (in CTF 2).
194 * bt_ctf_field_type to struct field_cb_override
196 GHashTable
*field_overrides
;
201 /* Current medium buffer data */
203 /* Last address provided by medium */
206 /* Buffer size provided by medium (bytes) */
209 /* Offset within whole packet of addr (bits) */
210 size_t packet_offset
;
212 /* Current position from addr (bits) */
216 /* Binary type reader */
217 struct bt_ctf_btr
*btr
;
219 /* Current medium data */
221 struct bt_ctf_notif_iter_medium_ops medops
;
222 size_t max_request_sz
;
226 /* Current packet size (bits) (-1 if unknown) */
227 int64_t cur_packet_size
;
229 /* Current content size (bits) (-1 if unknown) */
230 int64_t cur_content_size
;
232 /* bt_ctf_clock_class to uint64_t. */
233 GHashTable
*clock_states
;
236 * Cache of the trace-constant field paths (event header type)
237 * associated to the current trace.
239 struct trace_field_path_cache trace_field_path_cache
;
242 * Field path cache associated with the current stream class.
243 * Ownership of this structure belongs to the field_path_caches HT.
245 struct stream_class_field_path_cache
*cur_sc_field_path_cache
;
247 /* bt_ctf_stream_class to struct stream_class_field_path_cache. */
248 GHashTable
*sc_field_path_caches
;
252 const char *state_string(enum state state
)
257 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
258 return "STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN";
259 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
260 return "STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE";
261 case STATE_AFTER_TRACE_PACKET_HEADER
:
262 return "STATE_AFTER_TRACE_PACKET_HEADER";
263 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
264 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN";
265 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
266 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE";
267 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
268 return "STATE_AFTER_STREAM_PACKET_CONTEXT";
269 case STATE_EMIT_NOTIF_NEW_PACKET
:
270 return "STATE_EMIT_NOTIF_NEW_PACKET";
271 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
:
272 return "STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN";
273 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
:
274 return "STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE";
275 case STATE_AFTER_STREAM_EVENT_HEADER
:
276 return "STATE_AFTER_STREAM_EVENT_HEADER";
277 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
:
278 return "STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN";
279 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
:
280 return "STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE";
281 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN
:
282 return "STATE_DSCOPE_EVENT_CONTEXT_BEGIN";
283 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
:
284 return "STATE_DSCOPE_EVENT_CONTEXT_CONTINUE";
285 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
286 return "STATE_DSCOPE_EVENT_PAYLOAD_BEGIN";
287 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
288 return "STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE";
289 case STATE_EMIT_NOTIF_EVENT
:
290 return "STATE_EMIT_NOTIF_EVENT";
291 case STATE_EMIT_NOTIF_END_OF_PACKET
:
292 return "STATE_EMIT_NOTIF_END_OF_PACKET";
293 case STATE_SKIP_PACKET_PADDING
:
294 return "STATE_SKIP_PACKET_PADDING";
301 int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter
*notit
);
304 enum bt_ctf_btr_status
btr_timestamp_end_cb(void *value
,
305 struct bt_ctf_field_type
*type
, void *data
);
308 void stack_entry_free_func(gpointer data
)
310 struct stack_entry
*entry
= data
;
317 struct stack
*stack_new(struct bt_ctf_notif_iter
*notit
)
319 struct stack
*stack
= NULL
;
321 stack
= g_new0(struct stack
, 1);
323 BT_LOGE_STR("Failed to allocate one stack.");
327 stack
->entries
= g_ptr_array_new_with_free_func(stack_entry_free_func
);
328 if (!stack
->entries
) {
329 BT_LOGE_STR("Failed to allocate a GPtrArray.");
333 BT_LOGD("Created stack: notit-addr=%p, stack-addr=%p", notit
, stack
);
342 void stack_destroy(struct stack
*stack
)
345 BT_LOGD("Destroying stack: addr=%p", stack
);
346 g_ptr_array_free(stack
->entries
, TRUE
);
351 int stack_push(struct stack
*stack
, struct bt_ctf_field
*base
)
354 struct stack_entry
*entry
;
358 BT_LOGV("Pushing base field on stack: stack-addr=%p, "
359 "stack-size-before=%u, stack-size-after=%u",
360 stack
, stack
->entries
->len
, stack
->entries
->len
+ 1);
361 entry
= g_new0(struct stack_entry
, 1);
363 BT_LOGE_STR("Failed to allocate one stack entry.");
368 entry
->base
= bt_get(base
);
369 g_ptr_array_add(stack
->entries
, entry
);
376 unsigned int stack_size(struct stack
*stack
)
380 return stack
->entries
->len
;
384 void stack_pop(struct stack
*stack
)
387 assert(stack_size(stack
));
388 BT_LOGV("Popping from stack: "
389 "stack-addr=%p, stack-size-before=%u, stack-size-after=%u",
390 stack
, stack
->entries
->len
, stack
->entries
->len
- 1);
391 g_ptr_array_remove_index(stack
->entries
, stack
->entries
->len
- 1);
395 struct stack_entry
*stack_top(struct stack
*stack
)
398 assert(stack_size(stack
));
400 return g_ptr_array_index(stack
->entries
, stack
->entries
->len
- 1);
404 bool stack_empty(struct stack
*stack
)
406 return stack_size(stack
) == 0;
410 void stack_clear(struct stack
*stack
)
414 if (!stack_empty(stack
)) {
415 BT_LOGV("Clearing stack: stack-addr=%p, stack-size=%u",
416 stack
, stack
->entries
->len
);
417 g_ptr_array_remove_range(stack
->entries
, 0, stack_size(stack
));
420 assert(stack_empty(stack
));
424 enum bt_ctf_notif_iter_status
notif_iter_status_from_m_status(
425 enum bt_ctf_notif_iter_medium_status m_status
)
427 return (int) m_status
;
431 size_t buf_size_bits(struct bt_ctf_notif_iter
*notit
)
433 return notit
->buf
.sz
* 8;
437 size_t buf_available_bits(struct bt_ctf_notif_iter
*notit
)
439 return buf_size_bits(notit
) - notit
->buf
.at
;
443 size_t packet_at(struct bt_ctf_notif_iter
*notit
)
445 return notit
->buf
.packet_offset
+ notit
->buf
.at
;
449 void buf_consume_bits(struct bt_ctf_notif_iter
*notit
, size_t incr
)
451 BT_LOGV("Advancing cursor: notit-addr=%p, cur-before=%zu, cur-after=%zu",
452 notit
, notit
->buf
.at
, notit
->buf
.at
+ incr
);
453 notit
->buf
.at
+= incr
;
457 enum bt_ctf_notif_iter_status
request_medium_bytes(
458 struct bt_ctf_notif_iter
*notit
)
460 uint8_t *buffer_addr
= NULL
;
461 size_t buffer_sz
= 0;
462 enum bt_ctf_notif_iter_medium_status m_status
;
464 BT_LOGV("Calling user function (request bytes): notit-addr=%p, "
465 "request-size=%zu", notit
, notit
->medium
.max_request_sz
);
466 m_status
= notit
->medium
.medops
.request_bytes(
467 notit
->medium
.max_request_sz
, &buffer_addr
,
468 &buffer_sz
, notit
->medium
.data
);
469 BT_LOGV("User function returned: status=%s, buf-addr=%p, buf-size=%zu",
470 bt_ctf_notif_iter_medium_status_string(m_status
),
471 buffer_addr
, buffer_sz
);
472 if (m_status
== BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK
) {
473 assert(buffer_sz
!= 0);
475 /* New packet offset is old one + old size (in bits) */
476 notit
->buf
.packet_offset
+= buf_size_bits(notit
);
478 /* Restart at the beginning of the new medium buffer */
481 /* New medium buffer size */
482 notit
->buf
.sz
= buffer_sz
;
484 /* New medium buffer address */
485 notit
->buf
.addr
= buffer_addr
;
487 BT_LOGV("User function returned new bytes: "
488 "packet-offset=%zu, cur=%zu, size=%zu, addr=%p",
489 notit
->buf
.packet_offset
, notit
->buf
.at
,
490 notit
->buf
.sz
, notit
->buf
.addr
);
491 BT_LOGV_MEM(buffer_addr
, buffer_sz
, "Returned bytes at %p:",
493 } else if (m_status
== BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF
) {
494 struct bt_ctf_field_type
*eh_ft
= NULL
;
495 struct bt_ctf_field_type
*sec_ft
= NULL
;
496 struct bt_ctf_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 eh_ft
= bt_ctf_stream_class_get_event_header_type(
513 notit
->meta
.stream_class
);
514 sec_ft
= bt_ctf_stream_class_get_event_context_type(
515 notit
->meta
.stream_class
);
517 if (notit
->state
== STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
) {
519 * Beginning of event's header is only valid if
520 * the packet is not supposed to have a specific
521 * size (whole packet sequence has in fact only
524 if (notit
->cur_packet_size
== -1) {
529 if (notit
->state
== STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
) {
531 * Beginning of event's stream event context is
532 * only valid if the packet is not supposed to
533 * have a specific size (whole packet sequence
534 * has in fact only one packet), and there's no
537 if (notit
->cur_packet_size
== -1 && !eh_ft
) {
542 if (!notit
->meta
.event_class
) {
546 ec_ft
= bt_ctf_event_class_get_context_type(
547 notit
->meta
.event_class
);
549 if (notit
->state
== STATE_DSCOPE_EVENT_CONTEXT_BEGIN
) {
551 * Beginning of event's context is only valid if
552 * the packet is not supposed to have a specific
553 * size (whole packet sequence has in fact only
554 * one packet), and there's no event header and
555 * no stream event context.
557 if (notit
->cur_packet_size
== -1 && !eh_ft
&& !sec_ft
) {
562 if (notit
->state
== STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
) {
564 * Beginning of event's context is only valid if
565 * the packet is not supposed to have a specific
566 * size (whole packet sequence has in fact only
567 * one packet), and there's no event header, no
568 * stream event context, and no event context.
570 if (notit
->cur_packet_size
== -1 && !eh_ft
&& !sec_ft
&&
577 /* All other states are invalid */
578 BT_LOGW("User function returned %s, but notification iterator is in an unexpected state: "
580 bt_ctf_notif_iter_medium_status_string(m_status
),
581 state_string(notit
->state
));
582 m_status
= BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR
;
588 } else if (m_status
< 0) {
589 BT_LOGW("User function failed: status=%s",
590 bt_ctf_notif_iter_medium_status_string(m_status
));
593 return notif_iter_status_from_m_status(m_status
);
597 enum bt_ctf_notif_iter_status
buf_ensure_available_bits(
598 struct bt_ctf_notif_iter
*notit
)
600 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
602 if (buf_available_bits(notit
) == 0) {
604 * This _cannot_ return BT_CTF_NOTIF_ITER_STATUS_OK
607 status
= request_medium_bytes(notit
);
614 enum bt_ctf_notif_iter_status
read_dscope_begin_state(
615 struct bt_ctf_notif_iter
*notit
,
616 struct bt_ctf_field_type
*dscope_field_type
,
617 enum state done_state
, enum state continue_state
,
618 struct bt_ctf_field
**dscope_field
)
620 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
621 enum bt_ctf_btr_status btr_status
;
622 size_t consumed_bits
;
624 status
= buf_ensure_available_bits(notit
);
625 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
627 BT_LOGW("Cannot ensure that buffer has at least one byte: "
628 "notif-addr=%p, status=%s",
629 notit
, bt_ctf_notif_iter_status_string(status
));
631 BT_LOGV("Cannot ensure that buffer has at least one byte: "
632 "notif-addr=%p, status=%s",
633 notit
, bt_ctf_notif_iter_status_string(status
));
639 bt_put(*dscope_field
);
640 notit
->cur_dscope_field
= dscope_field
;
641 BT_LOGV("Starting BTR: notit-addr=%p, btr-addr=%p, ft-addr=%p",
642 notit
, notit
->btr
, dscope_field_type
);
643 consumed_bits
= bt_ctf_btr_start(notit
->btr
, dscope_field_type
,
644 notit
->buf
.addr
, notit
->buf
.at
, packet_at(notit
),
645 notit
->buf
.sz
, &btr_status
);
646 BT_LOGV("BTR consumed bits: size=%zu", consumed_bits
);
648 switch (btr_status
) {
649 case BT_CTF_BTR_STATUS_OK
:
650 /* type was read completely */
651 BT_LOGV_STR("Field was completely decoded.");
652 notit
->state
= done_state
;
654 case BT_CTF_BTR_STATUS_EOF
:
655 BT_LOGV_STR("BTR needs more data to decode field completely.");
656 notit
->state
= continue_state
;
659 BT_LOGW("BTR failed to start: notit-addr=%p, btr-addr=%p, "
660 "status=%s", notit
, notit
->btr
,
661 bt_ctf_btr_status_string(btr_status
));
662 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
666 /* Consume bits now since we know we're not in an error state */
667 buf_consume_bits(notit
, consumed_bits
);
674 enum bt_ctf_notif_iter_status
read_dscope_continue_state(
675 struct bt_ctf_notif_iter
*notit
, enum state done_state
)
677 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
678 enum bt_ctf_btr_status btr_status
;
679 size_t consumed_bits
;
681 status
= buf_ensure_available_bits(notit
);
682 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
684 BT_LOGW("Cannot ensure that buffer has at least one byte: "
685 "notif-addr=%p, status=%s",
686 notit
, bt_ctf_notif_iter_status_string(status
));
688 BT_LOGV("Cannot ensure that buffer has at least one byte: "
689 "notif-addr=%p, status=%s",
690 notit
, bt_ctf_notif_iter_status_string(status
));
696 BT_LOGV("Continuing BTR: notit-addr=%p, btr-addr=%p",
698 consumed_bits
= bt_ctf_btr_continue(notit
->btr
, notit
->buf
.addr
,
699 notit
->buf
.sz
, &btr_status
);
700 BT_LOGV("BTR consumed bits: size=%zu", consumed_bits
);
702 switch (btr_status
) {
703 case BT_CTF_BTR_STATUS_OK
:
704 /* Type was read completely. */
705 BT_LOGV_STR("Field was completely decoded.");
706 notit
->state
= done_state
;
708 case BT_CTF_BTR_STATUS_EOF
:
709 /* Stay in this continue state. */
710 BT_LOGV_STR("BTR needs more data to decode field completely.");
713 BT_LOGW("BTR failed to continue: notit-addr=%p, btr-addr=%p, "
714 "status=%s", notit
, notit
->btr
,
715 bt_ctf_btr_status_string(btr_status
));
716 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
720 /* Consume bits now since we know we're not in an error state. */
721 buf_consume_bits(notit
, consumed_bits
);
727 void put_event_dscopes(struct bt_ctf_notif_iter
*notit
)
729 BT_LOGV_STR("Putting event header field.");
730 BT_PUT(notit
->dscopes
.stream_event_header
);
731 BT_LOGV_STR("Putting stream event context field.");
732 BT_PUT(notit
->dscopes
.stream_event_context
);
733 BT_LOGV_STR("Putting event context field.");
734 BT_PUT(notit
->dscopes
.event_context
);
735 BT_LOGV_STR("Putting event payload field.");
736 BT_PUT(notit
->dscopes
.event_payload
);
740 void put_all_dscopes(struct bt_ctf_notif_iter
*notit
)
742 BT_LOGV_STR("Putting packet header field.");
743 BT_PUT(notit
->dscopes
.trace_packet_header
);
744 BT_LOGV_STR("Putting packet context field.");
745 BT_PUT(notit
->dscopes
.stream_packet_context
);
746 put_event_dscopes(notit
);
750 enum bt_ctf_notif_iter_status
read_packet_header_begin_state(
751 struct bt_ctf_notif_iter
*notit
)
753 struct bt_ctf_field_type
*packet_header_type
= NULL
;
754 enum bt_ctf_notif_iter_status ret
= BT_CTF_NOTIF_ITER_STATUS_OK
;
756 if (bt_ctf_notif_iter_switch_packet(notit
)) {
757 BT_LOGW("Cannot switch packet: notit-addr=%p", notit
);
758 ret
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
762 /* Packet header type is common to the whole trace. */
763 packet_header_type
= bt_ctf_trace_get_packet_header_type(
765 if (!packet_header_type
) {
766 notit
->state
= STATE_AFTER_TRACE_PACKET_HEADER
;
770 BT_LOGV("Decoding packet header field:"
771 "notit-addr=%p, trace-addr=%p, trace-name=\"%s\", ft-addr=%p",
772 notit
, notit
->meta
.trace
,
773 bt_ctf_trace_get_name(notit
->meta
.trace
), packet_header_type
);
774 ret
= read_dscope_begin_state(notit
, packet_header_type
,
775 STATE_AFTER_TRACE_PACKET_HEADER
,
776 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
777 ¬it
->dscopes
.trace_packet_header
);
779 BT_LOGW("Cannot decode packet header field: "
780 "notit-addr=%p, trace-addr=%p, "
781 "trace-name=\"%s\", ft-addr=%p",
782 notit
, notit
->meta
.trace
,
783 bt_ctf_trace_get_name(notit
->meta
.trace
),
787 BT_PUT(packet_header_type
);
792 enum bt_ctf_notif_iter_status
read_packet_header_continue_state(
793 struct bt_ctf_notif_iter
*notit
)
795 return read_dscope_continue_state(notit
,
796 STATE_AFTER_TRACE_PACKET_HEADER
);
800 bool is_struct_type(struct bt_ctf_field_type
*field_type
)
802 return bt_ctf_field_type_get_type_id(field_type
) ==
803 BT_CTF_FIELD_TYPE_ID_STRUCT
;
807 bool is_variant_type(struct bt_ctf_field_type
*field_type
)
809 return bt_ctf_field_type_get_type_id(field_type
) ==
810 BT_CTF_FIELD_TYPE_ID_VARIANT
;
814 struct stream_class_field_path_cache
*
815 create_stream_class_field_path_cache_entry(
816 struct bt_ctf_notif_iter
*notit
,
817 struct bt_ctf_stream_class
*stream_class
)
821 int timestamp_end
= -1;
822 int packet_size
= -1;
823 int content_size
= -1;
824 struct stream_class_field_path_cache
*cache_entry
= g_new0(
825 struct stream_class_field_path_cache
, 1);
826 struct bt_ctf_field_type
*event_header
= NULL
, *packet_context
= NULL
;
829 BT_LOGE_STR("Failed to allocate one stream class field path cache.");
833 event_header
= bt_ctf_stream_class_get_event_header_type(stream_class
);
834 if (event_header
&& bt_ctf_field_type_is_structure(event_header
)) {
837 count
= bt_ctf_field_type_structure_get_field_count(
841 for (i
= 0; i
< count
; i
++) {
845 ret
= bt_ctf_field_type_structure_get_field(
846 event_header
, &name
, NULL
, i
);
848 BT_LOGE("Cannot get event header structure field type's field: "
849 "notit-addr=%p, stream-class-addr=%p, "
850 "stream-class-name=\"%s\", "
851 "stream-class-id=%" PRId64
", "
852 "ft-addr=%p, index=%d",
854 bt_ctf_stream_class_get_name(stream_class
),
855 bt_ctf_stream_class_get_id(stream_class
),
860 if (v
!= -1 && id
!= -1) {
864 if (v
== -1 && strcmp(name
, "v") == 0) {
866 } else if (id
== -1 && !strcmp(name
, "id")) {
872 packet_context
= bt_ctf_stream_class_get_packet_context_type(
874 if (packet_context
&& bt_ctf_field_type_is_structure(packet_context
)) {
877 count
= bt_ctf_field_type_structure_get_field_count(
881 for (i
= 0; i
< count
; i
++) {
884 struct bt_ctf_field_type
*field_type
;
886 if (timestamp_end
!= -1 && packet_size
!= -1 &&
887 content_size
!= -1) {
891 ret
= bt_ctf_field_type_structure_get_field(
892 packet_context
, &name
, &field_type
, i
);
894 BT_LOGE("Cannot get packet context structure field type's field: "
895 "notit-addr=%p, stream-class-addr=%p, "
896 "stream-class-name=\"%s\", "
897 "stream-class-id=%" PRId64
", "
898 "ft-addr=%p, index=%d",
900 bt_ctf_stream_class_get_name(stream_class
),
901 bt_ctf_stream_class_get_id(stream_class
),
906 if (timestamp_end
== -1 &&
907 strcmp(name
, "timestamp_end") == 0) {
908 struct field_cb_override
*override
= g_new0(
909 struct field_cb_override
, 1);
916 override
->func
= btr_timestamp_end_cb
;
917 override
->data
= notit
;
918 g_hash_table_insert(notit
->field_overrides
,
919 bt_get(field_type
), override
);
921 } else if (packet_size
== -1 &&
922 !strcmp(name
, "packet_size")) {
924 } else if (content_size
== -1 &&
925 !strcmp(name
, "content_size")) {
933 cache_entry
->id
= id
;
934 cache_entry
->timestamp_end
= timestamp_end
;
935 cache_entry
->packet_size
= packet_size
;
936 cache_entry
->content_size
= content_size
;
938 BT_PUT(event_header
);
939 BT_PUT(packet_context
);
948 struct stream_class_field_path_cache
*get_stream_class_field_path_cache(
949 struct bt_ctf_notif_iter
*notit
,
950 struct bt_ctf_stream_class
*stream_class
)
952 bool cache_entry_found
;
953 struct stream_class_field_path_cache
*cache_entry
;
955 cache_entry_found
= g_hash_table_lookup_extended(
956 notit
->sc_field_path_caches
,
957 stream_class
, NULL
, (gpointer
) &cache_entry
);
958 if (unlikely(!cache_entry_found
)) {
959 cache_entry
= create_stream_class_field_path_cache_entry(notit
,
961 g_hash_table_insert(notit
->sc_field_path_caches
,
962 bt_get(stream_class
), (gpointer
) cache_entry
);
969 enum bt_ctf_notif_iter_status
set_current_stream_class(
970 struct bt_ctf_notif_iter
*notit
)
972 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
973 struct bt_ctf_field_type
*packet_header_type
= NULL
;
974 struct bt_ctf_field_type
*stream_id_field_type
= NULL
;
975 struct bt_ctf_stream_class
*new_stream_class
= NULL
;
978 /* Clear the current stream class field path cache. */
979 notit
->cur_sc_field_path_cache
= NULL
;
981 /* Is there any "stream_id" field in the packet header? */
982 packet_header_type
= bt_ctf_trace_get_packet_header_type(
984 if (!packet_header_type
) {
986 * No packet header, therefore no `stream_id` field,
987 * therefore only one stream class.
989 goto single_stream_class
;
992 assert(is_struct_type(packet_header_type
));
995 stream_id_field_type
=
996 bt_ctf_field_type_structure_get_field_type_by_name(
997 packet_header_type
, "stream_id");
998 if (stream_id_field_type
) {
999 /* Find appropriate stream class using current stream ID */
1001 struct bt_ctf_field
*stream_id_field
= NULL
;
1003 assert(notit
->dscopes
.trace_packet_header
);
1005 // TODO: optimalize!
1006 stream_id_field
= bt_ctf_field_structure_get_field(
1007 notit
->dscopes
.trace_packet_header
, "stream_id");
1008 assert(stream_id_field
);
1009 ret
= bt_ctf_field_unsigned_integer_get_value(
1010 stream_id_field
, &stream_id
);
1012 BT_PUT(stream_id_field
);
1014 single_stream_class
:
1015 /* Only one stream: pick the first stream class */
1016 assert(bt_ctf_trace_get_stream_class_count(
1017 notit
->meta
.trace
) == 1);
1021 BT_LOGV("Found stream class ID to use: notit-addr=%p, "
1022 "stream-class-id=%" PRIu64
", "
1023 "trace-addr=%p, trace-name=\"%s\"",
1024 notit
, stream_id
, notit
->meta
.trace
,
1025 bt_ctf_trace_get_name(notit
->meta
.trace
));
1027 new_stream_class
= bt_ctf_trace_get_stream_class_by_id(
1028 notit
->meta
.trace
, stream_id
);
1029 if (!new_stream_class
) {
1030 BT_LOGW("No stream class with ID of stream class ID to use in trace: "
1031 "notit-addr=%p, stream-class-id=%" PRIu64
", "
1032 "trace-addr=%p, trace-name=\"%s\"",
1033 notit
, stream_id
, notit
->meta
.trace
,
1034 bt_ctf_trace_get_name(notit
->meta
.trace
));
1035 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1039 if (notit
->meta
.stream_class
) {
1040 if (new_stream_class
!= notit
->meta
.stream_class
) {
1041 BT_LOGW("Two packets refer to two different stream classes within the same packet sequence: "
1042 "notit-addr=%p, prev-stream-class-addr=%p, "
1043 "prev-stream-class-name=\"%s\", "
1044 "prev-stream-class-id=%" PRId64
", "
1045 "next-stream-class-addr=%p, "
1046 "next-stream-class-name=\"%s\", "
1047 "next-stream-class-id=%" PRId64
", "
1048 "trace-addr=%p, trace-name=\"%s\"",
1049 notit
, notit
->meta
.stream_class
,
1050 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1051 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1053 bt_ctf_stream_class_get_name(new_stream_class
),
1054 bt_ctf_stream_class_get_id(new_stream_class
),
1056 bt_ctf_trace_get_name(notit
->meta
.trace
));
1057 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1061 BT_MOVE(notit
->meta
.stream_class
, new_stream_class
);
1064 BT_LOGV("Set current stream class: "
1065 "notit-addr=%p, stream-class-addr=%p, "
1066 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
1067 notit
, notit
->meta
.stream_class
,
1068 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1069 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
1072 * Retrieve (or lazily create) the current stream class field path
1075 notit
->cur_sc_field_path_cache
= get_stream_class_field_path_cache(
1076 notit
, notit
->meta
.stream_class
);
1077 if (!notit
->cur_sc_field_path_cache
) {
1078 BT_LOGW("Cannot retrieve stream class field path from cache: "
1079 "notit-addr=%p, stream-class-addr=%p, "
1080 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
1081 notit
, notit
->meta
.stream_class
,
1082 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1083 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
1084 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1088 BT_PUT(packet_header_type
);
1089 BT_PUT(stream_id_field_type
);
1090 bt_put(new_stream_class
);
1095 enum bt_ctf_notif_iter_status
after_packet_header_state(
1096 struct bt_ctf_notif_iter
*notit
)
1098 enum bt_ctf_notif_iter_status status
;
1100 status
= set_current_stream_class(notit
);
1101 if (status
== BT_CTF_NOTIF_ITER_STATUS_OK
) {
1102 notit
->state
= STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
;
1109 enum bt_ctf_notif_iter_status
read_packet_context_begin_state(
1110 struct bt_ctf_notif_iter
*notit
)
1112 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1113 struct bt_ctf_field_type
*packet_context_type
;
1115 assert(notit
->meta
.stream_class
);
1116 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(
1117 notit
->meta
.stream_class
);
1118 if (!packet_context_type
) {
1119 BT_LOGV("No packet packet context field type in stream class: continuing: "
1120 "notit-addr=%p, stream-class-addr=%p, "
1121 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
1122 notit
, notit
->meta
.stream_class
,
1123 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1124 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
1125 notit
->state
= STATE_AFTER_STREAM_PACKET_CONTEXT
;
1129 BT_LOGV("Decoding packet context field: "
1130 "notit-addr=%p, stream-class-addr=%p, "
1131 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1133 notit
, notit
->meta
.stream_class
,
1134 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1135 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1136 packet_context_type
);
1137 status
= read_dscope_begin_state(notit
, packet_context_type
,
1138 STATE_AFTER_STREAM_PACKET_CONTEXT
,
1139 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
1140 ¬it
->dscopes
.stream_packet_context
);
1142 BT_LOGW("Cannot decode packet context field: "
1143 "notit-addr=%p, stream-class-addr=%p, "
1144 "stream-class-name=\"%s\", "
1145 "stream-class-id=%" PRId64
", ft-addr=%p",
1146 notit
, notit
->meta
.stream_class
,
1147 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1148 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1149 packet_context_type
);
1153 BT_PUT(packet_context_type
);
1158 enum bt_ctf_notif_iter_status
read_packet_context_continue_state(
1159 struct bt_ctf_notif_iter
*notit
)
1161 return read_dscope_continue_state(notit
,
1162 STATE_AFTER_STREAM_PACKET_CONTEXT
);
1166 enum bt_ctf_notif_iter_status
set_current_packet_content_sizes(
1167 struct bt_ctf_notif_iter
*notit
)
1169 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1170 struct bt_ctf_field
*packet_size_field
= NULL
;
1171 struct bt_ctf_field
*content_size_field
= NULL
;
1172 uint64_t content_size
= -1, packet_size
= -1;
1174 if (!notit
->dscopes
.stream_packet_context
) {
1178 packet_size_field
= bt_ctf_field_structure_get_field(
1179 notit
->dscopes
.stream_packet_context
, "packet_size");
1180 content_size_field
= bt_ctf_field_structure_get_field(
1181 notit
->dscopes
.stream_packet_context
, "content_size");
1182 if (packet_size_field
) {
1183 int ret
= bt_ctf_field_unsigned_integer_get_value(
1184 packet_size_field
, &packet_size
);
1187 if (packet_size
== 0) {
1188 BT_LOGW("Invalid packet size: packet context field indicates packet size is zero: "
1189 "notit-addr=%p, packet-context-field-addr=%p",
1190 notit
, notit
->dscopes
.stream_packet_context
);
1191 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1193 } else if ((packet_size
% 8) != 0) {
1194 BT_LOGW("Invalid packet size: packet context field indicates packet size is not a multiple of 8: "
1195 "notit-addr=%p, packet-context-field-addr=%p, "
1196 "packet-size=%" PRIu64
,
1197 notit
, notit
->dscopes
.stream_packet_context
,
1199 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1204 if (content_size_field
) {
1205 int ret
= bt_ctf_field_unsigned_integer_get_value(
1206 content_size_field
, &content_size
);
1210 content_size
= packet_size
;
1213 if (content_size
> packet_size
) {
1214 BT_LOGW("Invalid packet or content size: packet context field indicates content size is greater than packet size: "
1215 "notit-addr=%p, packet-context-field-addr=%p, "
1216 "packet-size=%" PRIu64
", content-size=%" PRIu64
,
1217 notit
, notit
->dscopes
.stream_packet_context
,
1218 packet_size
, content_size
);
1219 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1223 notit
->cur_packet_size
= packet_size
;
1224 notit
->cur_content_size
= content_size
;
1225 BT_LOGV("Set current packet and content sizes: "
1226 "notit-addr=%p, packet-size=%" PRIu64
", content-size=%" PRIu64
,
1227 notit
, packet_size
, content_size
);
1229 BT_PUT(packet_size_field
);
1230 BT_PUT(content_size_field
);
1235 enum bt_ctf_notif_iter_status
after_packet_context_state(
1236 struct bt_ctf_notif_iter
*notit
)
1238 enum bt_ctf_notif_iter_status status
;
1240 status
= set_current_packet_content_sizes(notit
);
1241 if (status
== BT_CTF_NOTIF_ITER_STATUS_OK
) {
1242 notit
->state
= STATE_EMIT_NOTIF_NEW_PACKET
;
1249 enum bt_ctf_notif_iter_status
read_event_header_begin_state(
1250 struct bt_ctf_notif_iter
*notit
)
1252 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1253 struct bt_ctf_field_type
*event_header_type
= NULL
;
1255 /* Check if we have some content left */
1256 if (notit
->cur_content_size
>= 0) {
1257 if (packet_at(notit
) == notit
->cur_content_size
) {
1258 /* No more events! */
1259 BT_LOGV("Reached end of packet: notit-addr=%p, "
1260 "cur=%zu", notit
, packet_at(notit
));
1261 notit
->state
= STATE_EMIT_NOTIF_END_OF_PACKET
;
1263 } else if (packet_at(notit
) > notit
->cur_content_size
) {
1264 /* That's not supposed to happen */
1265 BT_LOGV("Before decoding event header field: cursor is passed the packet's content: "
1266 "notit-addr=%p, content-size=%zu, "
1267 "cur=%zu", notit
, notit
->cur_content_size
,
1269 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1274 event_header_type
= bt_ctf_stream_class_get_event_header_type(
1275 notit
->meta
.stream_class
);
1276 if (!event_header_type
) {
1277 notit
->state
= STATE_AFTER_STREAM_EVENT_HEADER
;
1281 put_event_dscopes(notit
);
1282 BT_LOGV("Decoding event header field: "
1283 "notit-addr=%p, stream-class-addr=%p, "
1284 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1286 notit
, notit
->meta
.stream_class
,
1287 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1288 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1290 status
= read_dscope_begin_state(notit
, event_header_type
,
1291 STATE_AFTER_STREAM_EVENT_HEADER
,
1292 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
,
1293 ¬it
->dscopes
.stream_event_header
);
1295 BT_LOGW("Cannot decode event header field: "
1296 "notit-addr=%p, stream-class-addr=%p, "
1297 "stream-class-name=\"%s\", "
1298 "stream-class-id=%" PRId64
", ft-addr=%p",
1299 notit
, notit
->meta
.stream_class
,
1300 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1301 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1305 BT_PUT(event_header_type
);
1311 enum bt_ctf_notif_iter_status
read_event_header_continue_state(
1312 struct bt_ctf_notif_iter
*notit
)
1314 return read_dscope_continue_state(notit
,
1315 STATE_AFTER_STREAM_EVENT_HEADER
);
1319 enum bt_ctf_notif_iter_status
set_current_event_class(struct bt_ctf_notif_iter
*notit
)
1322 * The assert() calls in this function are okay because it is
1323 * assumed here that all the metadata objects have been
1324 * validated for CTF correctness before decoding actual streams.
1327 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1328 struct bt_ctf_field_type
*event_header_type
;
1329 struct bt_ctf_field_type
*id_field_type
= NULL
;
1330 struct bt_ctf_field_type
*v_field_type
= NULL
;
1331 uint64_t event_id
= -1ULL;
1334 event_header_type
= bt_ctf_stream_class_get_event_header_type(
1335 notit
->meta
.stream_class
);
1336 if (!event_header_type
) {
1338 * No event header, therefore no event class ID field,
1339 * therefore only one event class.
1341 goto single_event_class
;
1344 /* Is there any "id"/"v" field in the event header? */
1345 assert(is_struct_type(event_header_type
));
1346 id_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1347 event_header_type
, "id");
1348 v_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1349 event_header_type
, "v");
1350 assert(notit
->dscopes
.stream_event_header
);
1354 * | | |_ _|_ _| __ __ _
1355 * | | | | | || '_ \ / _` |
1356 * | |___| | | || | | | (_| | S P E C I A L
1357 * |_____|_| |_||_| |_|\__, | C A S E â„¢
1360 struct bt_ctf_field
*v_field
= NULL
;
1361 struct bt_ctf_field
*v_struct_field
= NULL
;
1362 struct bt_ctf_field
*v_struct_id_field
= NULL
;
1364 // TODO: optimalize!
1365 v_field
= bt_ctf_field_structure_get_field(
1366 notit
->dscopes
.stream_event_header
, "v");
1370 bt_ctf_field_variant_get_current_field(v_field
);
1371 if (!v_struct_field
) {
1372 goto end_v_field_type
;
1375 // TODO: optimalize!
1377 bt_ctf_field_structure_get_field(v_struct_field
, "id");
1378 if (!v_struct_id_field
) {
1379 goto end_v_field_type
;
1382 if (bt_ctf_field_is_integer(v_struct_id_field
)) {
1383 ret
= bt_ctf_field_unsigned_integer_get_value(
1384 v_struct_id_field
, &event_id
);
1386 BT_LOGV("Cannot get value of unsigned integer field (`id`): continuing: "
1387 "notit=%p, field-addr=%p",
1388 notit
, v_struct_id_field
);
1395 BT_PUT(v_struct_field
);
1396 BT_PUT(v_struct_id_field
);
1399 if (id_field_type
&& event_id
== -1ULL) {
1400 /* Check "id" field */
1401 struct bt_ctf_field
*id_field
= NULL
;
1404 // TODO: optimalize!
1405 id_field
= bt_ctf_field_structure_get_field(
1406 notit
->dscopes
.stream_event_header
, "id");
1408 goto check_event_id
;
1411 if (bt_ctf_field_is_integer(id_field
)) {
1412 ret
= bt_ctf_field_unsigned_integer_get_value(
1413 id_field
, &event_id
);
1414 } else if (bt_ctf_field_is_enumeration(id_field
)) {
1415 struct bt_ctf_field
*container
;
1417 container
= bt_ctf_field_enumeration_get_container(
1420 ret
= bt_ctf_field_unsigned_integer_get_value(
1421 container
, &event_id
);
1430 if (event_id
== -1ULL) {
1432 /* Event ID not found: single event? */
1433 assert(bt_ctf_stream_class_get_event_class_count(
1434 notit
->meta
.stream_class
) == 1);
1438 BT_LOGV("Found event class ID to use: notit-addr=%p, "
1439 "stream-class-addr=%p, stream-class-name=\"%s\", "
1440 "stream-class-id=%" PRId64
", "
1441 "event-class-id=%" PRIu64
,
1442 notit
, notit
->meta
.stream_class
,
1443 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1444 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1446 BT_PUT(notit
->meta
.event_class
);
1447 notit
->meta
.event_class
= bt_ctf_stream_class_get_event_class_by_id(
1448 notit
->meta
.stream_class
, event_id
);
1449 if (!notit
->meta
.event_class
) {
1450 BT_LOGW("No event class with ID of event class ID to use in stream class: "
1451 "notit-addr=%p, stream-class-addr=%p, "
1452 "stream-class-name=\"%s\", "
1453 "stream-class-id=%" PRId64
", "
1454 "event-class-id=%" PRIu64
,
1455 notit
, notit
->meta
.stream_class
,
1456 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1457 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1459 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1463 BT_LOGV("Set current event class: "
1464 "notit-addr=%p, event-class-addr=%p, "
1465 "event-class-name=\"%s\", event-class-id=%" PRId64
,
1466 notit
, notit
->meta
.event_class
,
1467 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
1468 bt_ctf_event_class_get_id(notit
->meta
.event_class
));
1471 BT_PUT(event_header_type
);
1472 BT_PUT(id_field_type
);
1473 BT_PUT(v_field_type
);
1479 enum bt_ctf_notif_iter_status
after_event_header_state(
1480 struct bt_ctf_notif_iter
*notit
)
1482 enum bt_ctf_notif_iter_status status
;
1484 status
= set_current_event_class(notit
);
1485 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
1489 notit
->state
= STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
;
1496 enum bt_ctf_notif_iter_status
read_stream_event_context_begin_state(
1497 struct bt_ctf_notif_iter
*notit
)
1499 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1500 struct bt_ctf_field_type
*stream_event_context_type
;
1502 stream_event_context_type
= bt_ctf_stream_class_get_event_context_type(
1503 notit
->meta
.stream_class
);
1504 if (!stream_event_context_type
) {
1505 notit
->state
= STATE_DSCOPE_EVENT_CONTEXT_BEGIN
;
1509 BT_LOGV("Decoding stream event context field: "
1510 "notit-addr=%p, stream-class-addr=%p, "
1511 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1513 notit
, notit
->meta
.stream_class
,
1514 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1515 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1516 stream_event_context_type
);
1517 status
= read_dscope_begin_state(notit
, stream_event_context_type
,
1518 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
,
1519 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
,
1520 ¬it
->dscopes
.stream_event_context
);
1522 BT_LOGW("Cannot decode stream event context field: "
1523 "notit-addr=%p, stream-class-addr=%p, "
1524 "stream-class-name=\"%s\", "
1525 "stream-class-id=%" PRId64
", ft-addr=%p",
1526 notit
, notit
->meta
.stream_class
,
1527 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1528 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1529 stream_event_context_type
);
1533 BT_PUT(stream_event_context_type
);
1539 enum bt_ctf_notif_iter_status
read_stream_event_context_continue_state(
1540 struct bt_ctf_notif_iter
*notit
)
1542 return read_dscope_continue_state(notit
,
1543 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
);
1547 enum bt_ctf_notif_iter_status
read_event_context_begin_state(
1548 struct bt_ctf_notif_iter
*notit
)
1550 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1551 struct bt_ctf_field_type
*event_context_type
;
1553 event_context_type
= bt_ctf_event_class_get_context_type(
1554 notit
->meta
.event_class
);
1555 if (!event_context_type
) {
1556 notit
->state
= STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
;
1560 BT_LOGV("Decoding event context field: "
1561 "notit-addr=%p, event-class-addr=%p, "
1562 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1564 notit
, notit
->meta
.event_class
,
1565 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
1566 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
1567 event_context_type
);
1568 status
= read_dscope_begin_state(notit
, event_context_type
,
1569 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
1570 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
,
1571 ¬it
->dscopes
.event_context
);
1573 BT_LOGW("Cannot decode event context field: "
1574 "notit-addr=%p, event-class-addr=%p, "
1575 "event-class-name=\"%s\", "
1576 "event-class-id=%" PRId64
", ft-addr=%p",
1577 notit
, notit
->meta
.event_class
,
1578 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
1579 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
1580 event_context_type
);
1584 BT_PUT(event_context_type
);
1590 enum bt_ctf_notif_iter_status
read_event_context_continue_state(
1591 struct bt_ctf_notif_iter
*notit
)
1593 return read_dscope_continue_state(notit
,
1594 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
);
1598 enum bt_ctf_notif_iter_status
read_event_payload_begin_state(
1599 struct bt_ctf_notif_iter
*notit
)
1601 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1602 struct bt_ctf_field_type
*event_payload_type
;
1604 event_payload_type
= bt_ctf_event_class_get_payload_type(
1605 notit
->meta
.event_class
);
1606 if (!event_payload_type
) {
1607 notit
->state
= STATE_EMIT_NOTIF_EVENT
;
1611 BT_LOGV("Decoding event payload field: "
1612 "notit-addr=%p, event-class-addr=%p, "
1613 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1615 notit
, notit
->meta
.event_class
,
1616 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
1617 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
1618 event_payload_type
);
1619 status
= read_dscope_begin_state(notit
, event_payload_type
,
1620 STATE_EMIT_NOTIF_EVENT
,
1621 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
1622 ¬it
->dscopes
.event_payload
);
1624 BT_LOGW("Cannot decode event payload field: "
1625 "notit-addr=%p, event-class-addr=%p, "
1626 "event-class-name=\"%s\", "
1627 "event-class-id=%" PRId64
", ft-addr=%p",
1628 notit
, notit
->meta
.event_class
,
1629 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
1630 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
1631 event_payload_type
);
1635 BT_PUT(event_payload_type
);
1641 enum bt_ctf_notif_iter_status
read_event_payload_continue_state(
1642 struct bt_ctf_notif_iter
*notit
)
1644 return read_dscope_continue_state(notit
, STATE_EMIT_NOTIF_EVENT
);
1648 enum bt_ctf_notif_iter_status
skip_packet_padding_state(
1649 struct bt_ctf_notif_iter
*notit
)
1651 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1652 size_t bits_to_skip
;
1654 assert(notit
->cur_packet_size
> 0);
1655 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1656 if (bits_to_skip
== 0) {
1657 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1660 size_t bits_to_consume
;
1662 BT_LOGV("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu",
1663 bits_to_skip
, notit
, bits_to_skip
);
1664 status
= buf_ensure_available_bits(notit
);
1665 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
1669 bits_to_consume
= MIN(buf_available_bits(notit
), bits_to_skip
);
1670 BT_LOGV("Skipping %zu bits of padding: notit-addr=%p, size=%zu",
1671 bits_to_consume
, notit
, bits_to_consume
);
1672 buf_consume_bits(notit
, bits_to_consume
);
1673 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1674 if (bits_to_skip
== 0) {
1675 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1685 enum bt_ctf_notif_iter_status
handle_state(struct bt_ctf_notif_iter
*notit
)
1687 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1688 const enum state state
= notit
->state
;
1690 BT_LOGV("Handling state: notit-addr=%p, state=%s",
1691 notit
, state_string(state
));
1693 // TODO: optimalize!
1696 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1698 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
1699 status
= read_packet_header_begin_state(notit
);
1701 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
1702 status
= read_packet_header_continue_state(notit
);
1704 case STATE_AFTER_TRACE_PACKET_HEADER
:
1705 status
= after_packet_header_state(notit
);
1707 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
1708 status
= read_packet_context_begin_state(notit
);
1710 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
1711 status
= read_packet_context_continue_state(notit
);
1713 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
1714 status
= after_packet_context_state(notit
);
1716 case STATE_EMIT_NOTIF_NEW_PACKET
:
1717 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1719 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
:
1720 status
= read_event_header_begin_state(notit
);
1722 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
:
1723 status
= read_event_header_continue_state(notit
);
1725 case STATE_AFTER_STREAM_EVENT_HEADER
:
1726 status
= after_event_header_state(notit
);
1728 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
:
1729 status
= read_stream_event_context_begin_state(notit
);
1731 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
:
1732 status
= read_stream_event_context_continue_state(notit
);
1734 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN
:
1735 status
= read_event_context_begin_state(notit
);
1737 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
:
1738 status
= read_event_context_continue_state(notit
);
1740 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
1741 status
= read_event_payload_begin_state(notit
);
1743 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
1744 status
= read_event_payload_continue_state(notit
);
1746 case STATE_EMIT_NOTIF_EVENT
:
1747 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1749 case STATE_SKIP_PACKET_PADDING
:
1750 status
= skip_packet_padding_state(notit
);
1752 case STATE_EMIT_NOTIF_END_OF_PACKET
:
1753 notit
->state
= STATE_SKIP_PACKET_PADDING
;
1756 BT_LOGD("Unknown CTF plugin notification iterator state: "
1757 "notit-addr=%p, state=%d", notit
, notit
->state
);
1761 BT_LOGV("Handled state: notit-addr=%p, status=%s, "
1762 "prev-state=%s, cur-state=%s",
1763 notit
, bt_ctf_notif_iter_status_string(status
),
1764 state_string(state
), state_string(notit
->state
));
1769 * Resets the internal state of a CTF notification iterator.
1772 void bt_ctf_notif_iter_reset(struct bt_ctf_notif_iter
*notit
)
1775 BT_LOGD("Resetting notification iterator: addr=%p", notit
);
1776 stack_clear(notit
->stack
);
1777 BT_PUT(notit
->meta
.stream_class
);
1778 BT_PUT(notit
->meta
.event_class
);
1779 BT_PUT(notit
->packet
);
1780 BT_PUT(notit
->stream
);
1781 put_all_dscopes(notit
);
1782 notit
->buf
.addr
= NULL
;
1785 notit
->buf
.packet_offset
= 0;
1786 notit
->state
= STATE_INIT
;
1787 notit
->cur_content_size
= -1;
1788 notit
->cur_packet_size
= -1;
1792 int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter
*notit
)
1797 * We don't put the stream class here because we need to make
1798 * sure that all the packets processed by the same notification
1799 * iterator refer to the same stream class (the first one).
1802 BT_LOGV("Switching packet: notit-addr=%p, cur=%zu",
1803 notit
, notit
->buf
.at
);
1804 stack_clear(notit
->stack
);
1805 BT_PUT(notit
->meta
.event_class
);
1806 BT_PUT(notit
->packet
);
1807 BT_PUT(notit
->cur_timestamp_end
);
1808 put_all_dscopes(notit
);
1811 * Adjust current buffer so that addr points to the beginning of the new
1814 if (notit
->buf
.addr
) {
1815 size_t consumed_bytes
= (size_t) (notit
->buf
.at
/ CHAR_BIT
);
1817 /* Packets are assumed to start on a byte frontier. */
1818 if (notit
->buf
.at
% CHAR_BIT
) {
1819 BT_LOGW("Cannot switch packet: current position is not a multiple of 8: "
1820 "notit-addr=%p, cur=%zu", notit
, notit
->buf
.at
);
1825 notit
->buf
.addr
+= consumed_bytes
;
1826 notit
->buf
.sz
-= consumed_bytes
;
1828 notit
->buf
.packet_offset
= 0;
1829 BT_LOGV("Adjusted buffer: addr=%p, size=%zu",
1830 notit
->buf
.addr
, notit
->buf
.sz
);
1833 notit
->cur_content_size
= -1;
1834 notit
->cur_packet_size
= -1;
1835 notit
->cur_sc_field_path_cache
= NULL
;
1842 struct bt_ctf_field
*get_next_field(struct bt_ctf_notif_iter
*notit
)
1844 struct bt_ctf_field
*next_field
= NULL
;
1845 struct bt_ctf_field
*base_field
;
1846 struct bt_ctf_field_type
*base_type
;
1849 assert(!stack_empty(notit
->stack
));
1850 index
= stack_top(notit
->stack
)->index
;
1851 base_field
= stack_top(notit
->stack
)->base
;
1853 base_type
= bt_ctf_field_get_type(base_field
);
1856 switch (bt_ctf_field_type_get_type_id(base_type
)) {
1857 case BT_CTF_FIELD_TYPE_ID_STRUCT
:
1858 next_field
= bt_ctf_field_structure_get_field_by_index(
1861 case BT_CTF_FIELD_TYPE_ID_ARRAY
:
1862 next_field
= bt_ctf_field_array_get_field(base_field
, index
);
1864 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
1865 next_field
= bt_ctf_field_sequence_get_field(base_field
, index
);
1867 case BT_CTF_FIELD_TYPE_ID_VARIANT
:
1868 next_field
= bt_ctf_field_variant_get_current_field(base_field
);
1871 BT_LOGF("Unknown base field type ID: "
1872 "notit-addr=%p, ft-addr=%p, ft-id=%s",
1874 bt_ctf_field_type_id_string(
1875 bt_ctf_field_type_get_type_id(base_type
)));
1884 void update_clock_state(uint64_t *state
,
1885 struct bt_ctf_field
*value_field
)
1887 struct bt_ctf_field_type
*value_type
= NULL
;
1888 uint64_t requested_new_value
;
1889 uint64_t requested_new_value_mask
;
1890 uint64_t cur_value_masked
;
1891 int requested_new_value_size
;
1894 value_type
= bt_ctf_field_get_type(value_field
);
1896 assert(bt_ctf_field_type_is_integer(value_type
));
1897 requested_new_value_size
=
1898 bt_ctf_field_type_integer_get_size(value_type
);
1899 assert(requested_new_value_size
> 0);
1900 ret
= bt_ctf_field_unsigned_integer_get_value(value_field
,
1901 &requested_new_value
);
1905 * Special case for a 64-bit new value, which is the limit
1906 * of a clock value as of this version: overwrite the
1907 * current value directly.
1909 if (requested_new_value_size
== 64) {
1910 *state
= requested_new_value
;
1914 requested_new_value_mask
= (1ULL << requested_new_value_size
) - 1;
1915 cur_value_masked
= *state
& requested_new_value_mask
;
1917 if (requested_new_value
< cur_value_masked
) {
1919 * It looks like a wrap happened on the number of bits
1920 * of the requested new value. Assume that the clock
1921 * value wrapped only one time.
1923 *state
+= requested_new_value_mask
+ 1;
1926 /* Clear the low bits of the current clock value. */
1927 *state
&= ~requested_new_value_mask
;
1929 /* Set the low bits of the current clock value. */
1930 *state
|= requested_new_value
;
1932 BT_LOGV("Updated clock's value from integer field's value: "
1933 "value=%" PRIu64
, *state
);
1938 enum bt_ctf_btr_status
update_clock(struct bt_ctf_notif_iter
*notit
,
1939 struct bt_ctf_field
*int_field
)
1941 gboolean clock_class_found
;
1942 uint64_t *clock_state
= NULL
;
1943 struct bt_ctf_field_type
*int_field_type
= NULL
;
1944 enum bt_ctf_btr_status ret
= BT_CTF_BTR_STATUS_OK
;
1945 struct bt_ctf_clock_class
*clock_class
= NULL
;
1947 int_field_type
= bt_ctf_field_get_type(int_field
);
1948 assert(int_field_type
);
1949 clock_class
= bt_ctf_field_type_integer_get_mapped_clock_class(
1951 if (likely(!clock_class
)) {
1955 clock_class_found
= g_hash_table_lookup_extended(notit
->clock_states
,
1956 clock_class
, NULL
, (gpointer
) &clock_state
);
1957 if (!clock_class_found
) {
1958 clock_state
= g_new0(uint64_t, 1);
1960 BT_LOGE_STR("Failed to allocate a uint64_t.");
1961 ret
= BT_CTF_BTR_STATUS_ENOMEM
;
1965 g_hash_table_insert(notit
->clock_states
, bt_get(clock_class
),
1969 /* Update the clock's state. */
1970 BT_LOGV("Updating notification iterator's clock's value from integer field: "
1971 "notit-addr=%p, clock-class-addr=%p, "
1972 "clock-class-name=\"%s\", value=%" PRIu64
,
1974 bt_ctf_clock_class_get_name(clock_class
), *clock_state
);
1975 update_clock_state(clock_state
, int_field
);
1977 bt_put(int_field_type
);
1978 bt_put(clock_class
);
1983 enum bt_ctf_btr_status
btr_unsigned_int_common(uint64_t value
,
1984 struct bt_ctf_field_type
*type
, void *data
,
1985 struct bt_ctf_field
**out_int_field
)
1987 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1988 struct bt_ctf_field
*field
= NULL
;
1989 struct bt_ctf_field
*int_field
= NULL
;
1990 struct bt_ctf_notif_iter
*notit
= data
;
1993 BT_LOGV("Common unsigned integer function called from BTR: "
1994 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
1995 "ft-id=%s, value=%" PRIu64
,
1996 notit
, notit
->btr
, type
,
1997 bt_ctf_field_type_id_string(
1998 bt_ctf_field_type_get_type_id(type
)),
2001 /* Create next field */
2002 field
= get_next_field(notit
);
2004 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2005 status
= BT_CTF_BTR_STATUS_ERROR
;
2009 switch(bt_ctf_field_type_get_type_id(type
)) {
2010 case BT_CTF_FIELD_TYPE_ID_INTEGER
:
2011 /* Integer field is created field */
2012 BT_MOVE(int_field
, field
);
2015 case BT_CTF_FIELD_TYPE_ID_ENUM
:
2016 int_field
= bt_ctf_field_enumeration_get_container(field
);
2018 type
= bt_ctf_field_get_type(int_field
);
2022 BT_LOGF("Unexpected field type ID: "
2023 "notit-addr=%p, ft-addr=%p, ft-id=%s",
2025 bt_ctf_field_type_id_string(
2026 bt_ctf_field_type_get_type_id(type
)));
2031 ret
= bt_ctf_field_unsigned_integer_set_value(int_field
, value
);
2033 stack_top(notit
->stack
)->index
++;
2034 *out_int_field
= int_field
;
2043 enum bt_ctf_btr_status
btr_timestamp_end_cb(void *value
,
2044 struct bt_ctf_field_type
*type
, void *data
)
2046 enum bt_ctf_btr_status status
;
2047 struct bt_ctf_field
*field
= NULL
;
2048 struct bt_ctf_notif_iter
*notit
= data
;
2050 BT_LOGV("`timestamp_end` unsigned integer function called from BTR: "
2051 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2053 notit
, notit
->btr
, type
,
2054 bt_ctf_field_type_id_string(
2055 bt_ctf_field_type_get_type_id(type
)));
2056 status
= btr_unsigned_int_common(*((uint64_t *) value
), type
, data
,
2059 /* Set as the current packet's timestamp_end field. */
2060 BT_MOVE(notit
->cur_timestamp_end
, field
);
2065 enum bt_ctf_btr_status
btr_unsigned_int_cb(uint64_t value
,
2066 struct bt_ctf_field_type
*type
, void *data
)
2068 struct bt_ctf_notif_iter
*notit
= data
;
2069 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2070 struct bt_ctf_field
*field
= NULL
;
2071 struct field_cb_override
*override
;
2073 BT_LOGV("Unsigned integer function called from BTR: "
2074 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2075 "ft-id=%s, value=%" PRIu64
,
2076 notit
, notit
->btr
, type
,
2077 bt_ctf_field_type_id_string(
2078 bt_ctf_field_type_get_type_id(type
)),
2080 override
= g_hash_table_lookup(notit
->field_overrides
, type
);
2081 if (unlikely(override
)) {
2082 /* Override function logs errors */
2083 status
= override
->func(&value
, type
, override
->data
);
2087 status
= btr_unsigned_int_common(value
, type
, data
, &field
);
2088 if (status
!= BT_CTF_BTR_STATUS_OK
) {
2089 /* btr_unsigned_int_common() logs errors */
2093 status
= update_clock(notit
, field
);
2100 enum bt_ctf_btr_status
btr_signed_int_cb(int64_t value
,
2101 struct bt_ctf_field_type
*type
, void *data
)
2103 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2104 struct bt_ctf_field
*field
= NULL
;
2105 struct bt_ctf_field
*int_field
= NULL
;
2106 struct bt_ctf_notif_iter
*notit
= data
;
2109 BT_LOGV("Signed integer function called from BTR: "
2110 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2111 "ft-id=%s, value=%" PRId64
,
2112 notit
, notit
->btr
, type
,
2113 bt_ctf_field_type_id_string(
2114 bt_ctf_field_type_get_type_id(type
)),
2117 /* create next field */
2118 field
= get_next_field(notit
);
2120 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2121 status
= BT_CTF_BTR_STATUS_ERROR
;
2125 switch(bt_ctf_field_type_get_type_id(type
)) {
2126 case BT_CTF_FIELD_TYPE_ID_INTEGER
:
2127 /* Integer field is created field */
2128 BT_MOVE(int_field
, field
);
2131 case BT_CTF_FIELD_TYPE_ID_ENUM
:
2132 int_field
= bt_ctf_field_enumeration_get_container(field
);
2134 type
= bt_ctf_field_get_type(int_field
);
2138 BT_LOGF("Unexpected field type ID: "
2139 "notit-addr=%p, ft-addr=%p, ft-id=%s",
2141 bt_ctf_field_type_id_string(
2142 bt_ctf_field_type_get_type_id(type
)));
2147 ret
= bt_ctf_field_signed_integer_set_value(int_field
, value
);
2149 stack_top(notit
->stack
)->index
++;
2150 status
= update_clock(notit
, int_field
);
2160 enum bt_ctf_btr_status
btr_floating_point_cb(double value
,
2161 struct bt_ctf_field_type
*type
, void *data
)
2163 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2164 struct bt_ctf_field
*field
= NULL
;
2165 struct bt_ctf_notif_iter
*notit
= data
;
2168 BT_LOGV("Floating point number function called from BTR: "
2169 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2170 "ft-id=%s, value=%f",
2171 notit
, notit
->btr
, type
,
2172 bt_ctf_field_type_id_string(
2173 bt_ctf_field_type_get_type_id(type
)),
2176 /* Create next field */
2177 field
= get_next_field(notit
);
2179 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2180 status
= BT_CTF_BTR_STATUS_ERROR
;
2184 ret
= bt_ctf_field_floating_point_set_value(field
, value
);
2186 stack_top(notit
->stack
)->index
++;
2194 enum bt_ctf_btr_status
btr_string_begin_cb(
2195 struct bt_ctf_field_type
*type
, void *data
)
2197 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2198 struct bt_ctf_field
*field
= NULL
;
2199 struct bt_ctf_notif_iter
*notit
= data
;
2202 BT_LOGV("String (beginning) function called from BTR: "
2203 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2205 notit
, notit
->btr
, type
,
2206 bt_ctf_field_type_id_string(
2207 bt_ctf_field_type_get_type_id(type
)));
2209 /* Create next field */
2210 field
= get_next_field(notit
);
2212 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2213 status
= BT_CTF_BTR_STATUS_ERROR
;
2218 * Push on stack. Not a compound type per se, but we know that only
2219 * btr_string_cb() may be called between this call and a subsequent
2220 * call to btr_string_end_cb().
2222 ret
= stack_push(notit
->stack
, field
);
2224 BT_LOGE("Cannot push string field on stack: "
2225 "notit-addr=%p, field-addr=%p", notit
, field
);
2226 status
= BT_CTF_BTR_STATUS_ERROR
;
2231 * Initialize string field payload to an empty string since in the
2232 * case of a length 0 string the btr_string_cb won't be called and
2233 * we will end up with an unset string payload.
2235 ret
= bt_ctf_field_string_set_value(field
, "");
2237 BT_LOGE("Cannot initialize string field's value to an empty string: "
2238 "notit-addr=%p, field-addr=%p, ret=%d",
2240 status
= BT_CTF_BTR_STATUS_ERROR
;
2251 enum bt_ctf_btr_status
btr_string_cb(const char *value
,
2252 size_t len
, struct bt_ctf_field_type
*type
, void *data
)
2254 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2255 struct bt_ctf_field
*field
= NULL
;
2256 struct bt_ctf_notif_iter
*notit
= data
;
2259 BT_LOGV("String (substring) function called from BTR: "
2260 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2261 "ft-id=%s, string-length=%zu",
2262 notit
, notit
->btr
, type
,
2263 bt_ctf_field_type_id_string(
2264 bt_ctf_field_type_get_type_id(type
)),
2267 /* Get string field */
2268 field
= stack_top(notit
->stack
)->base
;
2271 /* Append current string */
2272 ret
= bt_ctf_field_string_append_len(field
, value
, len
);
2274 BT_LOGE("Cannot append substring to string field's value: "
2275 "notit-addr=%p, field-addr=%p, string-length=%zu, "
2276 "ret=%d", notit
, field
, len
, ret
);
2277 status
= BT_CTF_BTR_STATUS_ERROR
;
2286 enum bt_ctf_btr_status
btr_string_end_cb(
2287 struct bt_ctf_field_type
*type
, void *data
)
2289 struct bt_ctf_notif_iter
*notit
= data
;
2291 BT_LOGV("String (end) function called from BTR: "
2292 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2294 notit
, notit
->btr
, type
,
2295 bt_ctf_field_type_id_string(
2296 bt_ctf_field_type_get_type_id(type
)));
2298 /* Pop string field */
2299 stack_pop(notit
->stack
);
2301 /* Go to next field */
2302 stack_top(notit
->stack
)->index
++;
2303 return BT_CTF_BTR_STATUS_OK
;
2306 enum bt_ctf_btr_status
btr_compound_begin_cb(
2307 struct bt_ctf_field_type
*type
, void *data
)
2309 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2310 struct bt_ctf_notif_iter
*notit
= data
;
2311 struct bt_ctf_field
*field
;
2314 BT_LOGV("Compound (beginning) function called from BTR: "
2315 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2317 notit
, notit
->btr
, type
,
2318 bt_ctf_field_type_id_string(
2319 bt_ctf_field_type_get_type_id(type
)));
2322 if (stack_empty(notit
->stack
)) {
2323 /* Root: create dynamic scope field */
2324 *notit
->cur_dscope_field
= bt_ctf_field_create(type
);
2325 field
= *notit
->cur_dscope_field
;
2328 * Field will be put at the end of this function
2329 * (stack_push() will take one reference, but this
2330 * reference is lost upon the equivalent stack_pop()
2331 * later), so also get it for our context to own it.
2333 bt_get(*notit
->cur_dscope_field
);
2336 BT_LOGE("Cannot create compound field: "
2337 "notit-addr=%p, ft-addr=%p, ft-id=%s",
2339 bt_ctf_field_type_id_string(
2340 bt_ctf_field_type_get_type_id(type
)));
2341 status
= BT_CTF_BTR_STATUS_ERROR
;
2345 field
= get_next_field(notit
);
2347 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2348 status
= BT_CTF_BTR_STATUS_ERROR
;
2355 ret
= stack_push(notit
->stack
, field
);
2357 BT_LOGE("Cannot push compound field onto the stack: "
2358 "notit-addr=%p, ft-addr=%p, ft-id=%s, ret=%d",
2360 bt_ctf_field_type_id_string(
2361 bt_ctf_field_type_get_type_id(type
)),
2363 status
= BT_CTF_BTR_STATUS_ERROR
;
2373 enum bt_ctf_btr_status
btr_compound_end_cb(
2374 struct bt_ctf_field_type
*type
, void *data
)
2376 struct bt_ctf_notif_iter
*notit
= data
;
2378 BT_LOGV("Compound (end) function called from BTR: "
2379 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2381 notit
, notit
->btr
, type
,
2382 bt_ctf_field_type_id_string(
2383 bt_ctf_field_type_get_type_id(type
)));
2384 assert(!stack_empty(notit
->stack
));
2387 stack_pop(notit
->stack
);
2389 /* If the stack is not empty, increment the base's index */
2390 if (!stack_empty(notit
->stack
)) {
2391 stack_top(notit
->stack
)->index
++;
2394 return BT_CTF_BTR_STATUS_OK
;
2398 struct bt_ctf_field
*resolve_field(struct bt_ctf_notif_iter
*notit
,
2399 struct bt_ctf_field_path
*path
)
2401 struct bt_ctf_field
*field
= NULL
;
2404 if (BT_LOG_ON_VERBOSE
) {
2405 GString
*gstr
= bt_ctf_field_path_string(path
);
2407 BT_LOGV("Resolving field path: notit-addr=%p, field-path=\"%s\"",
2408 notit
, gstr
? gstr
->str
: NULL
);
2411 g_string_free(gstr
, TRUE
);
2415 switch (bt_ctf_field_path_get_root_scope(path
)) {
2416 case BT_CTF_SCOPE_TRACE_PACKET_HEADER
:
2417 field
= notit
->dscopes
.trace_packet_header
;
2419 case BT_CTF_SCOPE_STREAM_PACKET_CONTEXT
:
2420 field
= notit
->dscopes
.stream_packet_context
;
2422 case BT_CTF_SCOPE_STREAM_EVENT_HEADER
:
2423 field
= notit
->dscopes
.stream_event_header
;
2425 case BT_CTF_SCOPE_STREAM_EVENT_CONTEXT
:
2426 field
= notit
->dscopes
.stream_event_context
;
2428 case BT_CTF_SCOPE_EVENT_CONTEXT
:
2429 field
= notit
->dscopes
.event_context
;
2431 case BT_CTF_SCOPE_EVENT_FIELDS
:
2432 field
= notit
->dscopes
.event_payload
;
2435 BT_LOGF("Cannot resolve field path: unknown scope: "
2436 "notit-addr=%p, root-scope=%s",
2437 notit
, bt_ctf_scope_string(
2438 bt_ctf_field_path_get_root_scope(path
)));
2443 BT_LOGW("Cannot resolve field path: root field not found: "
2444 "notit-addr=%p, root-scope=%s",
2445 notit
, bt_ctf_scope_string(
2446 bt_ctf_field_path_get_root_scope(path
)));
2452 for (i
= 0; i
< bt_ctf_field_path_get_index_count(path
); ++i
) {
2453 struct bt_ctf_field
*next_field
= NULL
;
2454 struct bt_ctf_field_type
*field_type
;
2455 int index
= bt_ctf_field_path_get_index(path
, i
);
2457 field_type
= bt_ctf_field_get_type(field
);
2460 if (is_struct_type(field_type
)) {
2461 next_field
= bt_ctf_field_structure_get_field_by_index(
2463 } else if (is_variant_type(field_type
)) {
2465 bt_ctf_field_variant_get_current_field(field
);
2472 BT_LOGW("Cannot find next field: "
2473 "notit-addr=%p, ft-addr=%p, ft-id=%s, index=%d",
2475 bt_ctf_field_type_id_string(
2476 bt_ctf_field_type_get_type_id(field_type
)),
2481 /* Move next field -> field */
2482 BT_MOVE(field
, next_field
);
2490 int64_t btr_get_sequence_length_cb(struct bt_ctf_field_type
*type
, void *data
)
2494 struct bt_ctf_field
*seq_field
;
2495 struct bt_ctf_field_path
*field_path
;
2496 struct bt_ctf_notif_iter
*notit
= data
;
2497 struct bt_ctf_field
*length_field
= NULL
;
2500 field_path
= bt_ctf_field_type_sequence_get_length_field_path(type
);
2502 length_field
= resolve_field(notit
, field_path
);
2503 if (!length_field
) {
2504 BT_LOGW("Cannot resolve sequence field type's length field path: "
2505 "notit-addr=%p, ft-addr=%p",
2510 iret
= bt_ctf_field_unsigned_integer_get_value(length_field
, &length
);
2512 BT_LOGE("Cannot get value of sequence length field: "
2513 "notit-addr=%p, field-addr=%p",
2514 notit
, length_field
);
2518 seq_field
= stack_top(notit
->stack
)->base
;
2519 iret
= bt_ctf_field_sequence_set_length(seq_field
, length_field
);
2521 BT_LOGE("Cannot set sequence field's length field: "
2522 "notit-addr=%p, seq-field-addr=%p, "
2523 "length-field-addr=%p, ",
2524 notit
, seq_field
, length_field
);
2528 ret
= (int64_t) length
;
2531 BT_PUT(length_field
);
2538 struct bt_ctf_field_type
*btr_get_variant_type_cb(
2539 struct bt_ctf_field_type
*type
, void *data
)
2541 struct bt_ctf_field_path
*path
;
2542 struct bt_ctf_notif_iter
*notit
= data
;
2543 struct bt_ctf_field
*var_field
;
2544 struct bt_ctf_field
*tag_field
= NULL
;
2545 struct bt_ctf_field
*selected_field
= NULL
;
2546 struct bt_ctf_field_type
*selected_field_type
= NULL
;
2548 path
= bt_ctf_field_type_variant_get_tag_field_path(type
);
2550 tag_field
= resolve_field(notit
, path
);
2552 BT_LOGW("Cannot resolve variant field type's tag field path: "
2553 "notit-addr=%p, ft-addr=%p",
2559 * We found the enumeration tag field instance which should be
2560 * able to select a current field for this variant. This
2561 * callback function we're in is called _after_
2562 * compound_begin(), so the current stack top's base field is
2563 * the variant field in question. We get the selected field here
2564 * thanks to this tag field (thus creating the selected field),
2565 * which will also provide us with its type. Then, this field
2566 * will remain the current selected one until the next callback
2567 * function call which is used to fill the current selected
2570 var_field
= stack_top(notit
->stack
)->base
;
2571 selected_field
= bt_ctf_field_variant_get_field(var_field
, tag_field
);
2572 if (!selected_field
) {
2573 BT_LOGW("Cannot get variant field's selection using tag field: "
2574 "notit-addr=%p, var-field-addr=%p, tag-field-addr=%p",
2575 notit
, var_field
, tag_field
);
2579 selected_field_type
= bt_ctf_field_get_type(selected_field
);
2583 BT_PUT(selected_field
);
2586 return selected_field_type
;
2590 int set_event_clocks(struct bt_ctf_event
*event
,
2591 struct bt_ctf_notif_iter
*notit
)
2594 GHashTableIter iter
;
2595 struct bt_ctf_clock_class
*clock_class
;
2596 uint64_t *clock_state
;
2598 g_hash_table_iter_init(&iter
, notit
->clock_states
);
2600 while (g_hash_table_iter_next(&iter
, (gpointer
) &clock_class
,
2601 (gpointer
) &clock_state
)) {
2602 struct bt_ctf_clock_value
*clock_value
;
2604 clock_value
= bt_ctf_clock_value_create(clock_class
,
2607 BT_LOGE("Cannot create clock value from clock class: "
2608 "notit-addr=%p, clock-class-addr=%p, "
2609 "clock-class-name=\"%s\"",
2611 bt_ctf_clock_class_get_name(clock_class
));
2615 ret
= bt_ctf_event_set_clock_value(event
, clock_value
);
2616 bt_put(clock_value
);
2618 struct bt_ctf_event_class
*event_class
=
2619 bt_ctf_event_get_class(event
);
2621 assert(event_class
);
2622 BT_LOGE("Cannot set event's clock value: "
2623 "notit-addr=%p, event-addr=%p, "
2624 "event-class-name=\"%s\", "
2625 "event-class-id=%" PRId64
", "
2626 "clock-class-addr=%p, "
2627 "clock-class-name=\"%s\", "
2628 "clock-value-addr=%p",
2630 bt_ctf_event_class_get_name(event_class
),
2631 bt_ctf_event_class_get_id(event_class
),
2633 bt_ctf_clock_class_get_name(clock_class
),
2635 bt_put(event_class
);
2646 struct bt_ctf_event
*create_event(struct bt_ctf_notif_iter
*notit
)
2649 struct bt_ctf_event
*event
;
2651 BT_LOGV("Creating event for event notification: "
2652 "notit-addr=%p, event-class-addr=%p, "
2653 "event-class-name=\"%s\", "
2654 "event-class-id=%" PRId64
,
2655 notit
, notit
->meta
.event_class
,
2656 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2657 bt_ctf_event_class_get_id(notit
->meta
.event_class
));
2659 /* Create event object. */
2660 event
= bt_ctf_event_create(notit
->meta
.event_class
);
2662 BT_LOGE("Cannot create event: "
2663 "notit-addr=%p, event-class-addr=%p, "
2664 "event-class-name=\"%s\", "
2665 "event-class-id=%" PRId64
,
2666 notit
, notit
->meta
.event_class
,
2667 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2668 bt_ctf_event_class_get_id(notit
->meta
.event_class
));
2672 /* Set header, stream event context, context, and payload fields. */
2673 ret
= bt_ctf_event_set_header(event
,
2674 notit
->dscopes
.stream_event_header
);
2676 BT_LOGE("Cannot set event's header field: "
2677 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2678 "event-class-name=\"%s\", "
2679 "event-class-id=%" PRId64
", field-addr=%p",
2680 notit
, event
, notit
->meta
.event_class
,
2681 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2682 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
2683 notit
->dscopes
.stream_event_header
);
2687 ret
= bt_ctf_event_set_stream_event_context(event
,
2688 notit
->dscopes
.stream_event_context
);
2690 BT_LOGE("Cannot set event's context field: "
2691 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2692 "event-class-name=\"%s\", "
2693 "event-class-id=%" PRId64
", field-addr=%p",
2694 notit
, event
, notit
->meta
.event_class
,
2695 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2696 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
2697 notit
->dscopes
.stream_event_context
);
2701 ret
= bt_ctf_event_set_event_context(event
,
2702 notit
->dscopes
.event_context
);
2704 BT_LOGE("Cannot set event's stream event context field: "
2705 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2706 "event-class-name=\"%s\", "
2707 "event-class-id=%" PRId64
", field-addr=%p",
2708 notit
, event
, notit
->meta
.event_class
,
2709 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2710 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
2711 notit
->dscopes
.event_context
);
2715 ret
= bt_ctf_event_set_event_payload(event
,
2716 notit
->dscopes
.event_payload
);
2718 BT_LOGE("Cannot set event's payload field: "
2719 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2720 "event-class-name=\"%s\", "
2721 "event-class-id=%" PRId64
", field-addr=%p",
2722 notit
, event
, notit
->meta
.event_class
,
2723 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2724 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
2725 notit
->dscopes
.event_payload
);
2729 ret
= set_event_clocks(event
, notit
);
2731 BT_LOGE("Cannot set event's clock values: "
2732 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2733 "event-class-name=\"%s\", "
2734 "event-class-id=%" PRId64
,
2735 notit
, event
, notit
->meta
.event_class
,
2736 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2737 bt_ctf_event_class_get_id(notit
->meta
.event_class
));
2741 /* Associate with current packet. */
2742 assert(notit
->packet
);
2743 ret
= bt_ctf_event_set_packet(event
, notit
->packet
);
2745 BT_LOGE("Cannot set event's header field: "
2746 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2747 "event-class-name=\"%s\", "
2748 "event-class-id=%" PRId64
", packet-addr=%p",
2749 notit
, event
, notit
->meta
.event_class
,
2750 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2751 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
2766 uint64_t get_cur_stream_instance_id(struct bt_ctf_notif_iter
*notit
)
2768 struct bt_ctf_field
*stream_instance_id_field
= NULL
;
2769 uint64_t stream_instance_id
= -1ULL;
2772 if (!notit
->dscopes
.trace_packet_header
) {
2776 stream_instance_id_field
= bt_ctf_field_structure_get_field_by_name(
2777 notit
->dscopes
.trace_packet_header
, "stream_instance_id");
2778 if (!stream_instance_id_field
) {
2782 ret
= bt_ctf_field_unsigned_integer_get_value(stream_instance_id_field
,
2783 &stream_instance_id
);
2785 stream_instance_id
= -1ULL;
2790 bt_put(stream_instance_id_field
);
2791 return stream_instance_id
;
2795 int set_stream(struct bt_ctf_notif_iter
*notit
)
2798 struct bt_ctf_stream
*stream
= NULL
;
2800 BT_LOGV("Calling user function (get stream): notit-addr=%p, "
2801 "stream-class-addr=%p, stream-class-name=\"%s\", "
2802 "stream-class-id=%" PRId64
,
2803 notit
, notit
->meta
.stream_class
,
2804 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
2805 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
2806 stream
= bt_get(notit
->medium
.medops
.get_stream(
2807 notit
->meta
.stream_class
, get_cur_stream_instance_id(notit
),
2808 notit
->medium
.data
));
2809 BT_LOGV("User function returned: stream-addr=%p", stream
);
2811 BT_LOGW_STR("User function failed to return a stream object for the given stream class.");
2816 if (notit
->stream
&& stream
!= notit
->stream
) {
2817 BT_LOGW("User function returned a different stream than the previous one for the same sequence of packets.");
2822 BT_MOVE(notit
->stream
, stream
);
2830 void create_packet(struct bt_ctf_notif_iter
*notit
)
2833 struct bt_ctf_packet
*packet
= NULL
;
2835 BT_LOGV("Creating packet for packet notification: "
2836 "notit-addr=%p", notit
);
2838 /* Ask the user for the stream */
2839 ret
= set_stream(notit
);
2844 BT_LOGV("Creating packet from stream: "
2845 "notit-addr=%p, stream-addr=%p, "
2846 "stream-class-addr=%p, "
2847 "stream-class-name=\"%s\", "
2848 "stream-class-id=%" PRId64
,
2849 notit
, notit
->stream
, notit
->meta
.stream_class
,
2850 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
2851 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
2854 packet
= bt_ctf_packet_create(notit
->stream
);
2856 BT_LOGE("Cannot create packet from stream: "
2857 "notit-addr=%p, stream-addr=%p, "
2858 "stream-class-addr=%p, "
2859 "stream-class-name=\"%s\", "
2860 "stream-class-id=%" PRId64
,
2861 notit
, notit
->stream
, notit
->meta
.stream_class
,
2862 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
2863 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
2867 /* Set packet's context and header fields */
2868 if (notit
->dscopes
.trace_packet_header
) {
2869 ret
= bt_ctf_packet_set_header(packet
,
2870 notit
->dscopes
.trace_packet_header
);
2872 BT_LOGE("Cannot set packet's header field: "
2873 "notit-addr=%p, packet-addr=%p, "
2875 "stream-class-addr=%p, "
2876 "stream-class-name=\"%s\", "
2877 "stream-class-id=%" PRId64
", "
2879 notit
, packet
, notit
->stream
, notit
->meta
.stream_class
,
2880 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
2881 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
2882 notit
->dscopes
.trace_packet_header
);
2887 if (notit
->dscopes
.stream_packet_context
) {
2888 ret
= bt_ctf_packet_set_context(packet
,
2889 notit
->dscopes
.stream_packet_context
);
2891 BT_LOGE("Cannot set packet's context field: "
2892 "notit-addr=%p, packet-addr=%p, "
2894 "stream-class-addr=%p, "
2895 "stream-class-name=\"%s\", "
2896 "stream-class-id=%" PRId64
", "
2898 notit
, packet
, notit
->stream
, notit
->meta
.stream_class
,
2899 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
2900 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
2901 notit
->dscopes
.trace_packet_header
);
2912 BT_MOVE(notit
->packet
, packet
);
2916 void notify_new_packet(struct bt_ctf_notif_iter
*notit
,
2917 struct bt_notification
**notification
)
2919 struct bt_notification
*ret
;
2921 /* Initialize the iterator's current packet */
2922 create_packet(notit
);
2923 if (!notit
->packet
) {
2924 BT_LOGE("Cannot create packet for packet notification: "
2925 "notit-addr=%p", notit
);
2929 ret
= bt_notification_packet_begin_create(notit
->packet
);
2931 BT_LOGE("Cannot create packet beginning notification: "
2932 "notit-addr=%p, packet-addr=%p",
2933 notit
, notit
->packet
);
2936 *notification
= ret
;
2940 void notify_end_of_packet(struct bt_ctf_notif_iter
*notit
,
2941 struct bt_notification
**notification
)
2943 struct bt_notification
*ret
;
2945 if (!notit
->packet
) {
2949 ret
= bt_notification_packet_end_create(notit
->packet
);
2951 BT_LOGE("Cannot create packet end notification: "
2952 "notit-addr=%p, packet-addr=%p",
2953 notit
, notit
->packet
);
2956 BT_PUT(notit
->packet
);
2957 *notification
= ret
;
2961 void notify_event(struct bt_ctf_notif_iter
*notit
,
2962 struct bt_clock_class_priority_map
*cc_prio_map
,
2963 struct bt_notification
**notification
)
2965 struct bt_ctf_event
*event
;
2966 struct bt_notification
*ret
= NULL
;
2969 event
= create_event(notit
);
2971 BT_LOGE("Cannot create event for event notification: "
2972 "notit-addr=%p", notit
);
2976 ret
= bt_notification_event_create(event
, cc_prio_map
);
2978 BT_LOGE("Cannot create event notification: "
2979 "notit-addr=%p, event-addr=%p, "
2980 "cc-prio-map-addr=%p",
2981 notit
, event
, cc_prio_map
);
2984 *notification
= ret
;
2990 void init_trace_field_path_cache(struct bt_ctf_trace
*trace
,
2991 struct trace_field_path_cache
*trace_field_path_cache
)
2994 int stream_instance_id
= -1;
2996 struct bt_ctf_field_type
*packet_header
= NULL
;
2998 packet_header
= bt_ctf_trace_get_packet_header_type(trace
);
2999 if (!packet_header
) {
3003 if (!bt_ctf_field_type_is_structure(packet_header
)) {
3007 count
= bt_ctf_field_type_structure_get_field_count(packet_header
);
3010 for (i
= 0; (i
< count
&& (stream_id
== -1 || stream_instance_id
== -1)); i
++) {
3012 const char *field_name
;
3014 ret
= bt_ctf_field_type_structure_get_field(packet_header
,
3015 &field_name
, NULL
, i
);
3017 BT_LOGE("Cannot get structure field's field: "
3018 "field-addr=%p, index=%d",
3023 if (stream_id
== -1 && !strcmp(field_name
, "stream_id")) {
3025 } else if (stream_instance_id
== -1 &&
3026 !strcmp(field_name
, "stream_instance_id")) {
3027 stream_instance_id
= i
;
3032 trace_field_path_cache
->stream_id
= stream_id
;
3033 trace_field_path_cache
->stream_instance_id
= stream_instance_id
;
3034 BT_PUT(packet_header
);
3038 struct bt_ctf_notif_iter
*bt_ctf_notif_iter_create(struct bt_ctf_trace
*trace
,
3039 size_t max_request_sz
,
3040 struct bt_ctf_notif_iter_medium_ops medops
, void *data
)
3042 struct bt_ctf_notif_iter
*notit
= NULL
;
3043 struct bt_ctf_btr_cbs cbs
= {
3045 .signed_int
= btr_signed_int_cb
,
3046 .unsigned_int
= btr_unsigned_int_cb
,
3047 .floating_point
= btr_floating_point_cb
,
3048 .string_begin
= btr_string_begin_cb
,
3049 .string
= btr_string_cb
,
3050 .string_end
= btr_string_end_cb
,
3051 .compound_begin
= btr_compound_begin_cb
,
3052 .compound_end
= btr_compound_end_cb
,
3055 .get_sequence_length
= btr_get_sequence_length_cb
,
3056 .get_variant_type
= btr_get_variant_type_cb
,
3061 assert(medops
.request_bytes
);
3062 assert(medops
.get_stream
);
3063 BT_LOGD("Creating CTF plugin notification iterator: "
3064 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
3066 trace
, bt_ctf_trace_get_name(trace
), max_request_sz
, data
);
3067 notit
= g_new0(struct bt_ctf_notif_iter
, 1);
3069 BT_LOGE_STR("Failed to allocate one CTF plugin notification iterator.");
3072 notit
->clock_states
= g_hash_table_new_full(g_direct_hash
,
3073 g_direct_equal
, bt_put
, g_free
);
3074 if (!notit
->clock_states
) {
3075 BT_LOGE_STR("Failed to allocate a GHashTable.");
3078 notit
->meta
.trace
= bt_get(trace
);
3079 notit
->medium
.medops
= medops
;
3080 notit
->medium
.max_request_sz
= max_request_sz
;
3081 notit
->medium
.data
= data
;
3082 notit
->stack
= stack_new(notit
);
3083 if (!notit
->stack
) {
3084 BT_LOGE_STR("Failed to create field stack.");
3088 notit
->btr
= bt_ctf_btr_create(cbs
, notit
);
3090 BT_LOGE_STR("Failed to create binary type reader (BTR).");
3094 bt_ctf_notif_iter_reset(notit
);
3095 init_trace_field_path_cache(trace
, ¬it
->trace_field_path_cache
);
3096 notit
->sc_field_path_caches
= g_hash_table_new_full(g_direct_hash
,
3097 g_direct_equal
, bt_put
, g_free
);
3098 if (!notit
->sc_field_path_caches
) {
3099 BT_LOGE_STR("Failed to allocate a GHashTable.");
3103 notit
->field_overrides
= g_hash_table_new_full(g_direct_hash
,
3104 g_direct_equal
, bt_put
, g_free
);
3105 if (!notit
->field_overrides
) {
3106 BT_LOGE_STR("Failed to allocate a GHashTable.");
3110 BT_LOGD("Created CTF plugin notification iterator: "
3111 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
3112 "data=%p, notit-addr=%p",
3113 trace
, bt_ctf_trace_get_name(trace
), max_request_sz
, data
,
3120 bt_ctf_notif_iter_destroy(notit
);
3125 void bt_ctf_notif_iter_destroy(struct bt_ctf_notif_iter
*notit
)
3127 BT_PUT(notit
->meta
.trace
);
3128 BT_PUT(notit
->meta
.stream_class
);
3129 BT_PUT(notit
->meta
.event_class
);
3130 BT_PUT(notit
->packet
);
3131 BT_PUT(notit
->stream
);
3132 BT_PUT(notit
->cur_timestamp_end
);
3133 put_all_dscopes(notit
);
3135 BT_LOGD("Destroying CTF plugin notification iterator: addr=%p", notit
);
3138 BT_LOGD_STR("Destroying field stack.");
3139 stack_destroy(notit
->stack
);
3143 BT_LOGD("Destroying BTR: btr-addr=%p", notit
->btr
);
3144 bt_ctf_btr_destroy(notit
->btr
);
3147 if (notit
->clock_states
) {
3148 g_hash_table_destroy(notit
->clock_states
);
3151 if (notit
->sc_field_path_caches
) {
3152 g_hash_table_destroy(notit
->sc_field_path_caches
);
3155 if (notit
->field_overrides
) {
3156 g_hash_table_destroy(notit
->field_overrides
);
3162 enum bt_ctf_notif_iter_status
bt_ctf_notif_iter_get_next_notification(
3163 struct bt_ctf_notif_iter
*notit
,
3164 struct bt_clock_class_priority_map
*cc_prio_map
,
3165 struct bt_notification
**notification
)
3167 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
3170 assert(notification
);
3172 BT_LOGV("Getting next notification: notit-addr=%p, cc-prio-map-addr=%p",
3173 notit
, cc_prio_map
);
3176 status
= handle_state(notit
);
3177 if (status
== BT_CTF_NOTIF_ITER_STATUS_AGAIN
) {
3178 BT_LOGV_STR("Medium returned BT_CTF_NOTIF_ITER_STATUS_AGAIN.");
3181 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
3182 if (status
== BT_CTF_NOTIF_ITER_STATUS_EOF
) {
3183 BT_LOGV_STR("Medium returned BT_CTF_NOTIF_ITER_STATUS_EOF.");
3185 BT_LOGW("Cannot handle state: "
3186 "notit-addr=%p, state=%s",
3187 notit
, state_string(notit
->state
));
3192 switch (notit
->state
) {
3193 case STATE_EMIT_NOTIF_NEW_PACKET
:
3194 /* notify_new_packet() logs errors */
3195 notify_new_packet(notit
, notification
);
3196 if (!*notification
) {
3197 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
3200 case STATE_EMIT_NOTIF_EVENT
:
3201 /* notify_event() logs errors */
3202 notify_event(notit
, cc_prio_map
, notification
);
3203 if (!*notification
) {
3204 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
3207 case STATE_EMIT_NOTIF_END_OF_PACKET
:
3208 /* Update clock with timestamp_end field. */
3209 if (notit
->cur_timestamp_end
) {
3210 enum bt_ctf_btr_status btr_status
;
3211 struct bt_ctf_field_type
*field_type
=
3212 bt_ctf_field_get_type(
3213 notit
->cur_timestamp_end
);
3216 btr_status
= update_clock(notit
,
3217 notit
->cur_timestamp_end
);
3219 if (btr_status
!= BT_CTF_BTR_STATUS_OK
) {
3220 BT_LOGW("Cannot update stream's clock value: "
3221 "notit-addr=%p", notit
);
3222 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
3227 /* notify_end_of_packet() logs errors */
3228 notify_end_of_packet(notit
, notification
);
3229 if (!*notification
) {
3230 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
3234 /* Non-emitting state: continue */
3244 enum bt_ctf_notif_iter_status
bt_ctf_notif_iter_get_packet_header_context_fields(
3245 struct bt_ctf_notif_iter
*notit
,
3246 struct bt_ctf_field
**packet_header_field
,
3247 struct bt_ctf_field
**packet_context_field
)
3249 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
3253 if (notit
->state
== STATE_EMIT_NOTIF_NEW_PACKET
) {
3254 /* We're already there */
3259 status
= handle_state(notit
);
3260 if (status
== BT_CTF_NOTIF_ITER_STATUS_AGAIN
) {
3261 BT_LOGV_STR("Medium returned BT_CTF_NOTIF_ITER_STATUS_AGAIN.");
3264 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
3265 if (status
== BT_CTF_NOTIF_ITER_STATUS_EOF
) {
3266 BT_LOGV_STR("Medium returned BT_CTF_NOTIF_ITER_STATUS_EOF.");
3268 BT_LOGW("Cannot handle state: "
3269 "notit-addr=%p, state=%s",
3270 notit
, state_string(notit
->state
));
3275 switch (notit
->state
) {
3276 case STATE_EMIT_NOTIF_NEW_PACKET
:
3278 * Packet header and context fields are
3279 * potentially decoded (or they don't exist).
3283 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
3284 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
3285 case STATE_AFTER_TRACE_PACKET_HEADER
:
3286 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
3287 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
3288 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
3289 /* Non-emitting state: continue */
3293 * We should never get past the
3294 * STATE_EMIT_NOTIF_NEW_PACKET state.
3296 BT_LOGF("Unexpected state: notit-addr=%p, state=%s",
3297 notit
, state_string(notit
->state
));
3303 if (packet_header_field
) {
3304 *packet_header_field
= bt_get(notit
->dscopes
.trace_packet_header
);
3307 if (packet_context_field
) {
3308 *packet_context_field
= bt_get(notit
->dscopes
.stream_packet_context
);
3316 void bt_ctf_notif_iter_set_medops_data(struct bt_ctf_notif_iter
*notit
,
3320 notit
->medium
.data
= medops_data
;