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) */
215 /* Position of the last event header from addr (bits) */
219 /* Binary type reader */
220 struct bt_ctf_btr
*btr
;
222 /* Current medium data */
224 struct bt_ctf_notif_iter_medium_ops medops
;
225 size_t max_request_sz
;
229 /* Current packet size (bits) (-1 if unknown) */
230 int64_t cur_packet_size
;
232 /* Current content size (bits) (-1 if unknown) */
233 int64_t cur_content_size
;
235 /* bt_ctf_clock_class to uint64_t. */
236 GHashTable
*clock_states
;
239 * Cache of the trace-constant field paths (event header type)
240 * associated to the current trace.
242 struct trace_field_path_cache trace_field_path_cache
;
245 * Field path cache associated with the current stream class.
246 * Ownership of this structure belongs to the field_path_caches HT.
248 struct stream_class_field_path_cache
*cur_sc_field_path_cache
;
250 /* bt_ctf_stream_class to struct stream_class_field_path_cache. */
251 GHashTable
*sc_field_path_caches
;
255 const char *state_string(enum state state
)
260 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
261 return "STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN";
262 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
263 return "STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE";
264 case STATE_AFTER_TRACE_PACKET_HEADER
:
265 return "STATE_AFTER_TRACE_PACKET_HEADER";
266 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
267 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN";
268 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
269 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE";
270 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
271 return "STATE_AFTER_STREAM_PACKET_CONTEXT";
272 case STATE_EMIT_NOTIF_NEW_PACKET
:
273 return "STATE_EMIT_NOTIF_NEW_PACKET";
274 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
:
275 return "STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN";
276 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
:
277 return "STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE";
278 case STATE_AFTER_STREAM_EVENT_HEADER
:
279 return "STATE_AFTER_STREAM_EVENT_HEADER";
280 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
:
281 return "STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN";
282 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
:
283 return "STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE";
284 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN
:
285 return "STATE_DSCOPE_EVENT_CONTEXT_BEGIN";
286 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
:
287 return "STATE_DSCOPE_EVENT_CONTEXT_CONTINUE";
288 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
289 return "STATE_DSCOPE_EVENT_PAYLOAD_BEGIN";
290 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
291 return "STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE";
292 case STATE_EMIT_NOTIF_EVENT
:
293 return "STATE_EMIT_NOTIF_EVENT";
294 case STATE_EMIT_NOTIF_END_OF_PACKET
:
295 return "STATE_EMIT_NOTIF_END_OF_PACKET";
296 case STATE_SKIP_PACKET_PADDING
:
297 return "STATE_SKIP_PACKET_PADDING";
304 int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter
*notit
);
307 enum bt_ctf_btr_status
btr_timestamp_end_cb(void *value
,
308 struct bt_ctf_field_type
*type
, void *data
);
311 void stack_entry_free_func(gpointer data
)
313 struct stack_entry
*entry
= data
;
320 struct stack
*stack_new(struct bt_ctf_notif_iter
*notit
)
322 struct stack
*stack
= NULL
;
324 stack
= g_new0(struct stack
, 1);
326 BT_LOGE_STR("Failed to allocate one stack.");
330 stack
->entries
= g_ptr_array_new_with_free_func(stack_entry_free_func
);
331 if (!stack
->entries
) {
332 BT_LOGE_STR("Failed to allocate a GPtrArray.");
336 BT_LOGD("Created stack: notit-addr=%p, stack-addr=%p", notit
, stack
);
345 void stack_destroy(struct stack
*stack
)
348 BT_LOGD("Destroying stack: addr=%p", stack
);
349 g_ptr_array_free(stack
->entries
, TRUE
);
354 int stack_push(struct stack
*stack
, struct bt_ctf_field
*base
)
357 struct stack_entry
*entry
;
361 BT_LOGV("Pushing base field on stack: stack-addr=%p, "
362 "stack-size-before=%u, stack-size-after=%u",
363 stack
, stack
->entries
->len
, stack
->entries
->len
+ 1);
364 entry
= g_new0(struct stack_entry
, 1);
366 BT_LOGE_STR("Failed to allocate one stack entry.");
371 entry
->base
= bt_get(base
);
372 g_ptr_array_add(stack
->entries
, entry
);
379 unsigned int stack_size(struct stack
*stack
)
383 return stack
->entries
->len
;
387 void stack_pop(struct stack
*stack
)
390 assert(stack_size(stack
));
391 BT_LOGV("Popping from stack: "
392 "stack-addr=%p, stack-size-before=%u, stack-size-after=%u",
393 stack
, stack
->entries
->len
, stack
->entries
->len
- 1);
394 g_ptr_array_remove_index(stack
->entries
, stack
->entries
->len
- 1);
398 struct stack_entry
*stack_top(struct stack
*stack
)
401 assert(stack_size(stack
));
403 return g_ptr_array_index(stack
->entries
, stack
->entries
->len
- 1);
407 bool stack_empty(struct stack
*stack
)
409 return stack_size(stack
) == 0;
413 void stack_clear(struct stack
*stack
)
417 if (!stack_empty(stack
)) {
418 BT_LOGV("Clearing stack: stack-addr=%p, stack-size=%u",
419 stack
, stack
->entries
->len
);
420 g_ptr_array_remove_range(stack
->entries
, 0, stack_size(stack
));
423 assert(stack_empty(stack
));
427 enum bt_ctf_notif_iter_status
notif_iter_status_from_m_status(
428 enum bt_ctf_notif_iter_medium_status m_status
)
430 return (int) m_status
;
434 size_t buf_size_bits(struct bt_ctf_notif_iter
*notit
)
436 return notit
->buf
.sz
* 8;
440 size_t buf_available_bits(struct bt_ctf_notif_iter
*notit
)
442 return buf_size_bits(notit
) - notit
->buf
.at
;
446 size_t packet_at(struct bt_ctf_notif_iter
*notit
)
448 return notit
->buf
.packet_offset
+ notit
->buf
.at
;
452 void buf_consume_bits(struct bt_ctf_notif_iter
*notit
, size_t incr
)
454 BT_LOGV("Advancing cursor: notit-addr=%p, cur-before=%zu, cur-after=%zu",
455 notit
, notit
->buf
.at
, notit
->buf
.at
+ incr
);
456 notit
->buf
.at
+= incr
;
460 enum bt_ctf_notif_iter_status
request_medium_bytes(
461 struct bt_ctf_notif_iter
*notit
)
463 uint8_t *buffer_addr
= NULL
;
464 size_t buffer_sz
= 0;
465 enum bt_ctf_notif_iter_medium_status m_status
;
467 BT_LOGV("Calling user function (request bytes): notit-addr=%p, "
468 "request-size=%zu", notit
, notit
->medium
.max_request_sz
);
469 m_status
= notit
->medium
.medops
.request_bytes(
470 notit
->medium
.max_request_sz
, &buffer_addr
,
471 &buffer_sz
, notit
->medium
.data
);
472 BT_LOGV("User function returned: status=%s, buf-addr=%p, buf-size=%zu",
473 bt_ctf_notif_iter_medium_status_string(m_status
),
474 buffer_addr
, buffer_sz
);
475 if (m_status
== BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK
) {
476 assert(buffer_sz
!= 0);
478 /* New packet offset is old one + old size (in bits) */
479 notit
->buf
.packet_offset
+= buf_size_bits(notit
);
481 /* Restart at the beginning of the new medium buffer */
483 notit
->buf
.last_eh_at
= -1ULL;
485 /* New medium buffer size */
486 notit
->buf
.sz
= buffer_sz
;
488 /* New medium buffer address */
489 notit
->buf
.addr
= buffer_addr
;
491 BT_LOGV("User function returned new bytes: "
492 "packet-offset=%zu, cur=%zu, size=%zu, addr=%p",
493 notit
->buf
.packet_offset
, notit
->buf
.at
,
494 notit
->buf
.sz
, notit
->buf
.addr
);
495 BT_LOGV_MEM(buffer_addr
, buffer_sz
, "Returned bytes at %p:",
497 } else if (m_status
== BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF
) {
498 struct bt_ctf_field_type
*eh_ft
= NULL
;
499 struct bt_ctf_field_type
*sec_ft
= NULL
;
500 struct bt_ctf_field_type
*ec_ft
= NULL
;
503 * User returned end of stream: validate that we're not
504 * in the middle of a packet header, packet context, or
507 if (notit
->state
== STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
) {
508 /* Beginning of packet: always valid */
512 if (!notit
->meta
.stream_class
) {
516 eh_ft
= bt_ctf_stream_class_get_event_header_type(
517 notit
->meta
.stream_class
);
518 sec_ft
= bt_ctf_stream_class_get_event_context_type(
519 notit
->meta
.stream_class
);
521 if (notit
->state
== STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
) {
523 * Beginning of event's header is only valid if
524 * the packet is not supposed to have a specific
525 * size (whole packet sequence has in fact only
528 if (notit
->cur_packet_size
== -1) {
533 if (notit
->state
== STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
) {
535 * Beginning of event's stream event context is
536 * only valid if the packet is not supposed to
537 * have a specific size (whole packet sequence
538 * has in fact only one packet), and there's no
541 if (notit
->cur_packet_size
== -1 && !eh_ft
) {
546 if (!notit
->meta
.event_class
) {
550 ec_ft
= bt_ctf_event_class_get_context_type(
551 notit
->meta
.event_class
);
553 if (notit
->state
== STATE_DSCOPE_EVENT_CONTEXT_BEGIN
) {
555 * Beginning of event's context is only valid if
556 * the packet is not supposed to have a specific
557 * size (whole packet sequence has in fact only
558 * one packet), and there's no event header and
559 * no stream event context.
561 if (notit
->cur_packet_size
== -1 && !eh_ft
&& !sec_ft
) {
566 if (notit
->state
== STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
) {
568 * Beginning of event's context is only valid if
569 * the packet is not supposed to have a specific
570 * size (whole packet sequence has in fact only
571 * one packet), and there's no event header, no
572 * stream event context, and no event context.
574 if (notit
->cur_packet_size
== -1 && !eh_ft
&& !sec_ft
&&
581 /* All other states are invalid */
582 BT_LOGW("User function returned %s, but notification iterator is in an unexpected state: "
584 bt_ctf_notif_iter_medium_status_string(m_status
),
585 state_string(notit
->state
));
586 m_status
= BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR
;
592 } else if (m_status
< 0) {
593 BT_LOGW("User function failed: status=%s",
594 bt_ctf_notif_iter_medium_status_string(m_status
));
597 return notif_iter_status_from_m_status(m_status
);
601 enum bt_ctf_notif_iter_status
buf_ensure_available_bits(
602 struct bt_ctf_notif_iter
*notit
)
604 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
606 if (buf_available_bits(notit
) == 0) {
608 * This _cannot_ return BT_CTF_NOTIF_ITER_STATUS_OK
611 status
= request_medium_bytes(notit
);
618 enum bt_ctf_notif_iter_status
read_dscope_begin_state(
619 struct bt_ctf_notif_iter
*notit
,
620 struct bt_ctf_field_type
*dscope_field_type
,
621 enum state done_state
, enum state continue_state
,
622 struct bt_ctf_field
**dscope_field
)
624 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
625 enum bt_ctf_btr_status btr_status
;
626 size_t consumed_bits
;
628 status
= buf_ensure_available_bits(notit
);
629 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
631 BT_LOGW("Cannot ensure that buffer has at least one byte: "
632 "notif-addr=%p, status=%s",
633 notit
, bt_ctf_notif_iter_status_string(status
));
635 BT_LOGV("Cannot ensure that buffer has at least one byte: "
636 "notif-addr=%p, status=%s",
637 notit
, bt_ctf_notif_iter_status_string(status
));
643 bt_put(*dscope_field
);
644 notit
->cur_dscope_field
= dscope_field
;
645 BT_LOGV("Starting BTR: notit-addr=%p, btr-addr=%p, ft-addr=%p",
646 notit
, notit
->btr
, dscope_field_type
);
647 consumed_bits
= bt_ctf_btr_start(notit
->btr
, dscope_field_type
,
648 notit
->buf
.addr
, notit
->buf
.at
, packet_at(notit
),
649 notit
->buf
.sz
, &btr_status
);
650 BT_LOGV("BTR consumed bits: size=%zu", consumed_bits
);
652 switch (btr_status
) {
653 case BT_CTF_BTR_STATUS_OK
:
654 /* type was read completely */
655 BT_LOGV_STR("Field was completely decoded.");
656 notit
->state
= done_state
;
658 case BT_CTF_BTR_STATUS_EOF
:
659 BT_LOGV_STR("BTR needs more data to decode field completely.");
660 notit
->state
= continue_state
;
663 BT_LOGW("BTR failed to start: notit-addr=%p, btr-addr=%p, "
664 "status=%s", notit
, notit
->btr
,
665 bt_ctf_btr_status_string(btr_status
));
666 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
670 /* Consume bits now since we know we're not in an error state */
671 buf_consume_bits(notit
, consumed_bits
);
678 enum bt_ctf_notif_iter_status
read_dscope_continue_state(
679 struct bt_ctf_notif_iter
*notit
, enum state done_state
)
681 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
682 enum bt_ctf_btr_status btr_status
;
683 size_t consumed_bits
;
685 status
= buf_ensure_available_bits(notit
);
686 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
688 BT_LOGW("Cannot ensure that buffer has at least one byte: "
689 "notif-addr=%p, status=%s",
690 notit
, bt_ctf_notif_iter_status_string(status
));
692 BT_LOGV("Cannot ensure that buffer has at least one byte: "
693 "notif-addr=%p, status=%s",
694 notit
, bt_ctf_notif_iter_status_string(status
));
700 BT_LOGV("Continuing BTR: notit-addr=%p, btr-addr=%p",
702 consumed_bits
= bt_ctf_btr_continue(notit
->btr
, notit
->buf
.addr
,
703 notit
->buf
.sz
, &btr_status
);
704 BT_LOGV("BTR consumed bits: size=%zu", consumed_bits
);
706 switch (btr_status
) {
707 case BT_CTF_BTR_STATUS_OK
:
708 /* Type was read completely. */
709 BT_LOGV_STR("Field was completely decoded.");
710 notit
->state
= done_state
;
712 case BT_CTF_BTR_STATUS_EOF
:
713 /* Stay in this continue state. */
714 BT_LOGV_STR("BTR needs more data to decode field completely.");
717 BT_LOGW("BTR failed to continue: notit-addr=%p, btr-addr=%p, "
718 "status=%s", notit
, notit
->btr
,
719 bt_ctf_btr_status_string(btr_status
));
720 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
724 /* Consume bits now since we know we're not in an error state. */
725 buf_consume_bits(notit
, consumed_bits
);
731 void put_event_dscopes(struct bt_ctf_notif_iter
*notit
)
733 BT_LOGV_STR("Putting event header field.");
734 BT_PUT(notit
->dscopes
.stream_event_header
);
735 BT_LOGV_STR("Putting stream event context field.");
736 BT_PUT(notit
->dscopes
.stream_event_context
);
737 BT_LOGV_STR("Putting event context field.");
738 BT_PUT(notit
->dscopes
.event_context
);
739 BT_LOGV_STR("Putting event payload field.");
740 BT_PUT(notit
->dscopes
.event_payload
);
744 void put_all_dscopes(struct bt_ctf_notif_iter
*notit
)
746 BT_LOGV_STR("Putting packet header field.");
747 BT_PUT(notit
->dscopes
.trace_packet_header
);
748 BT_LOGV_STR("Putting packet context field.");
749 BT_PUT(notit
->dscopes
.stream_packet_context
);
750 put_event_dscopes(notit
);
754 enum bt_ctf_notif_iter_status
read_packet_header_begin_state(
755 struct bt_ctf_notif_iter
*notit
)
757 struct bt_ctf_field_type
*packet_header_type
= NULL
;
758 enum bt_ctf_notif_iter_status ret
= BT_CTF_NOTIF_ITER_STATUS_OK
;
760 if (bt_ctf_notif_iter_switch_packet(notit
)) {
761 BT_LOGW("Cannot switch packet: notit-addr=%p", notit
);
762 ret
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
766 /* Packet header type is common to the whole trace. */
767 packet_header_type
= bt_ctf_trace_get_packet_header_type(
769 if (!packet_header_type
) {
770 notit
->state
= STATE_AFTER_TRACE_PACKET_HEADER
;
774 BT_LOGV("Decoding packet header field:"
775 "notit-addr=%p, trace-addr=%p, trace-name=\"%s\", ft-addr=%p",
776 notit
, notit
->meta
.trace
,
777 bt_ctf_trace_get_name(notit
->meta
.trace
), packet_header_type
);
778 ret
= read_dscope_begin_state(notit
, packet_header_type
,
779 STATE_AFTER_TRACE_PACKET_HEADER
,
780 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
781 ¬it
->dscopes
.trace_packet_header
);
783 BT_LOGW("Cannot decode packet header field: "
784 "notit-addr=%p, trace-addr=%p, "
785 "trace-name=\"%s\", ft-addr=%p",
786 notit
, notit
->meta
.trace
,
787 bt_ctf_trace_get_name(notit
->meta
.trace
),
791 BT_PUT(packet_header_type
);
796 enum bt_ctf_notif_iter_status
read_packet_header_continue_state(
797 struct bt_ctf_notif_iter
*notit
)
799 return read_dscope_continue_state(notit
,
800 STATE_AFTER_TRACE_PACKET_HEADER
);
804 bool is_struct_type(struct bt_ctf_field_type
*field_type
)
806 return bt_ctf_field_type_get_type_id(field_type
) ==
807 BT_CTF_FIELD_TYPE_ID_STRUCT
;
811 bool is_variant_type(struct bt_ctf_field_type
*field_type
)
813 return bt_ctf_field_type_get_type_id(field_type
) ==
814 BT_CTF_FIELD_TYPE_ID_VARIANT
;
818 struct stream_class_field_path_cache
*
819 create_stream_class_field_path_cache_entry(
820 struct bt_ctf_notif_iter
*notit
,
821 struct bt_ctf_stream_class
*stream_class
)
825 int timestamp_end
= -1;
826 int packet_size
= -1;
827 int content_size
= -1;
828 struct stream_class_field_path_cache
*cache_entry
= g_new0(
829 struct stream_class_field_path_cache
, 1);
830 struct bt_ctf_field_type
*event_header
= NULL
, *packet_context
= NULL
;
833 BT_LOGE_STR("Failed to allocate one stream class field path cache.");
837 event_header
= bt_ctf_stream_class_get_event_header_type(stream_class
);
838 if (event_header
&& bt_ctf_field_type_is_structure(event_header
)) {
841 count
= bt_ctf_field_type_structure_get_field_count(
845 for (i
= 0; i
< count
; i
++) {
849 ret
= bt_ctf_field_type_structure_get_field(
850 event_header
, &name
, NULL
, i
);
852 BT_LOGE("Cannot get event header structure field type's field: "
853 "notit-addr=%p, stream-class-addr=%p, "
854 "stream-class-name=\"%s\", "
855 "stream-class-id=%" PRId64
", "
856 "ft-addr=%p, index=%d",
858 bt_ctf_stream_class_get_name(stream_class
),
859 bt_ctf_stream_class_get_id(stream_class
),
864 if (v
!= -1 && id
!= -1) {
868 if (v
== -1 && strcmp(name
, "v") == 0) {
870 } else if (id
== -1 && !strcmp(name
, "id")) {
876 packet_context
= bt_ctf_stream_class_get_packet_context_type(
878 if (packet_context
&& bt_ctf_field_type_is_structure(packet_context
)) {
881 count
= bt_ctf_field_type_structure_get_field_count(
885 for (i
= 0; i
< count
; i
++) {
888 struct bt_ctf_field_type
*field_type
;
890 if (timestamp_end
!= -1 && packet_size
!= -1 &&
891 content_size
!= -1) {
895 ret
= bt_ctf_field_type_structure_get_field(
896 packet_context
, &name
, &field_type
, i
);
898 BT_LOGE("Cannot get packet context structure field type's field: "
899 "notit-addr=%p, stream-class-addr=%p, "
900 "stream-class-name=\"%s\", "
901 "stream-class-id=%" PRId64
", "
902 "ft-addr=%p, index=%d",
904 bt_ctf_stream_class_get_name(stream_class
),
905 bt_ctf_stream_class_get_id(stream_class
),
910 if (timestamp_end
== -1 &&
911 strcmp(name
, "timestamp_end") == 0) {
912 struct field_cb_override
*override
= g_new0(
913 struct field_cb_override
, 1);
920 override
->func
= btr_timestamp_end_cb
;
921 override
->data
= notit
;
922 g_hash_table_insert(notit
->field_overrides
,
923 bt_get(field_type
), override
);
925 } else if (packet_size
== -1 &&
926 !strcmp(name
, "packet_size")) {
928 } else if (content_size
== -1 &&
929 !strcmp(name
, "content_size")) {
937 cache_entry
->id
= id
;
938 cache_entry
->timestamp_end
= timestamp_end
;
939 cache_entry
->packet_size
= packet_size
;
940 cache_entry
->content_size
= content_size
;
942 BT_PUT(event_header
);
943 BT_PUT(packet_context
);
952 struct stream_class_field_path_cache
*get_stream_class_field_path_cache(
953 struct bt_ctf_notif_iter
*notit
,
954 struct bt_ctf_stream_class
*stream_class
)
956 bool cache_entry_found
;
957 struct stream_class_field_path_cache
*cache_entry
;
959 cache_entry_found
= g_hash_table_lookup_extended(
960 notit
->sc_field_path_caches
,
961 stream_class
, NULL
, (gpointer
) &cache_entry
);
962 if (unlikely(!cache_entry_found
)) {
963 cache_entry
= create_stream_class_field_path_cache_entry(notit
,
965 g_hash_table_insert(notit
->sc_field_path_caches
,
966 bt_get(stream_class
), (gpointer
) cache_entry
);
973 enum bt_ctf_notif_iter_status
set_current_stream_class(
974 struct bt_ctf_notif_iter
*notit
)
976 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
977 struct bt_ctf_field_type
*packet_header_type
= NULL
;
978 struct bt_ctf_field_type
*stream_id_field_type
= NULL
;
979 struct bt_ctf_stream_class
*new_stream_class
= NULL
;
982 /* Clear the current stream class field path cache. */
983 notit
->cur_sc_field_path_cache
= NULL
;
985 /* Is there any "stream_id" field in the packet header? */
986 packet_header_type
= bt_ctf_trace_get_packet_header_type(
988 if (!packet_header_type
) {
990 * No packet header, therefore no `stream_id` field,
991 * therefore only one stream class.
993 goto single_stream_class
;
996 assert(is_struct_type(packet_header_type
));
999 stream_id_field_type
=
1000 bt_ctf_field_type_structure_get_field_type_by_name(
1001 packet_header_type
, "stream_id");
1002 if (stream_id_field_type
) {
1003 /* Find appropriate stream class using current stream ID */
1005 struct bt_ctf_field
*stream_id_field
= NULL
;
1007 assert(notit
->dscopes
.trace_packet_header
);
1009 // TODO: optimalize!
1010 stream_id_field
= bt_ctf_field_structure_get_field(
1011 notit
->dscopes
.trace_packet_header
, "stream_id");
1012 assert(stream_id_field
);
1013 ret
= bt_ctf_field_unsigned_integer_get_value(
1014 stream_id_field
, &stream_id
);
1016 BT_PUT(stream_id_field
);
1018 single_stream_class
:
1019 /* Only one stream: pick the first stream class */
1020 assert(bt_ctf_trace_get_stream_class_count(
1021 notit
->meta
.trace
) == 1);
1025 BT_LOGV("Found stream class ID to use: notit-addr=%p, "
1026 "stream-class-id=%" PRIu64
", "
1027 "trace-addr=%p, trace-name=\"%s\"",
1028 notit
, stream_id
, notit
->meta
.trace
,
1029 bt_ctf_trace_get_name(notit
->meta
.trace
));
1031 new_stream_class
= bt_ctf_trace_get_stream_class_by_id(
1032 notit
->meta
.trace
, stream_id
);
1033 if (!new_stream_class
) {
1034 BT_LOGW("No stream class with ID of stream class ID to use in trace: "
1035 "notit-addr=%p, stream-class-id=%" PRIu64
", "
1036 "trace-addr=%p, trace-name=\"%s\"",
1037 notit
, stream_id
, notit
->meta
.trace
,
1038 bt_ctf_trace_get_name(notit
->meta
.trace
));
1039 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1043 if (notit
->meta
.stream_class
) {
1044 if (new_stream_class
!= notit
->meta
.stream_class
) {
1045 BT_LOGW("Two packets refer to two different stream classes within the same packet sequence: "
1046 "notit-addr=%p, prev-stream-class-addr=%p, "
1047 "prev-stream-class-name=\"%s\", "
1048 "prev-stream-class-id=%" PRId64
", "
1049 "next-stream-class-addr=%p, "
1050 "next-stream-class-name=\"%s\", "
1051 "next-stream-class-id=%" PRId64
", "
1052 "trace-addr=%p, trace-name=\"%s\"",
1053 notit
, notit
->meta
.stream_class
,
1054 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1055 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1057 bt_ctf_stream_class_get_name(new_stream_class
),
1058 bt_ctf_stream_class_get_id(new_stream_class
),
1060 bt_ctf_trace_get_name(notit
->meta
.trace
));
1061 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1065 BT_MOVE(notit
->meta
.stream_class
, new_stream_class
);
1068 BT_LOGV("Set current stream class: "
1069 "notit-addr=%p, stream-class-addr=%p, "
1070 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
1071 notit
, notit
->meta
.stream_class
,
1072 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1073 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
1076 * Retrieve (or lazily create) the current stream class field path
1079 notit
->cur_sc_field_path_cache
= get_stream_class_field_path_cache(
1080 notit
, notit
->meta
.stream_class
);
1081 if (!notit
->cur_sc_field_path_cache
) {
1082 BT_LOGW("Cannot retrieve stream class field path from cache: "
1083 "notit-addr=%p, stream-class-addr=%p, "
1084 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
1085 notit
, notit
->meta
.stream_class
,
1086 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1087 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
1088 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1092 BT_PUT(packet_header_type
);
1093 BT_PUT(stream_id_field_type
);
1094 bt_put(new_stream_class
);
1099 enum bt_ctf_notif_iter_status
after_packet_header_state(
1100 struct bt_ctf_notif_iter
*notit
)
1102 enum bt_ctf_notif_iter_status status
;
1104 status
= set_current_stream_class(notit
);
1105 if (status
== BT_CTF_NOTIF_ITER_STATUS_OK
) {
1106 notit
->state
= STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
;
1113 enum bt_ctf_notif_iter_status
read_packet_context_begin_state(
1114 struct bt_ctf_notif_iter
*notit
)
1116 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1117 struct bt_ctf_field_type
*packet_context_type
;
1119 assert(notit
->meta
.stream_class
);
1120 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(
1121 notit
->meta
.stream_class
);
1122 if (!packet_context_type
) {
1123 BT_LOGV("No packet packet context field type in stream class: continuing: "
1124 "notit-addr=%p, stream-class-addr=%p, "
1125 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
1126 notit
, notit
->meta
.stream_class
,
1127 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1128 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
1129 notit
->state
= STATE_AFTER_STREAM_PACKET_CONTEXT
;
1133 BT_LOGV("Decoding packet context field: "
1134 "notit-addr=%p, stream-class-addr=%p, "
1135 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1137 notit
, notit
->meta
.stream_class
,
1138 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1139 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1140 packet_context_type
);
1141 status
= read_dscope_begin_state(notit
, packet_context_type
,
1142 STATE_AFTER_STREAM_PACKET_CONTEXT
,
1143 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
1144 ¬it
->dscopes
.stream_packet_context
);
1146 BT_LOGW("Cannot decode packet context field: "
1147 "notit-addr=%p, stream-class-addr=%p, "
1148 "stream-class-name=\"%s\", "
1149 "stream-class-id=%" PRId64
", ft-addr=%p",
1150 notit
, notit
->meta
.stream_class
,
1151 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1152 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1153 packet_context_type
);
1157 BT_PUT(packet_context_type
);
1162 enum bt_ctf_notif_iter_status
read_packet_context_continue_state(
1163 struct bt_ctf_notif_iter
*notit
)
1165 return read_dscope_continue_state(notit
,
1166 STATE_AFTER_STREAM_PACKET_CONTEXT
);
1170 enum bt_ctf_notif_iter_status
set_current_packet_content_sizes(
1171 struct bt_ctf_notif_iter
*notit
)
1173 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1174 struct bt_ctf_field
*packet_size_field
= NULL
;
1175 struct bt_ctf_field
*content_size_field
= NULL
;
1176 uint64_t content_size
= -1, packet_size
= -1;
1178 if (!notit
->dscopes
.stream_packet_context
) {
1182 packet_size_field
= bt_ctf_field_structure_get_field(
1183 notit
->dscopes
.stream_packet_context
, "packet_size");
1184 content_size_field
= bt_ctf_field_structure_get_field(
1185 notit
->dscopes
.stream_packet_context
, "content_size");
1186 if (packet_size_field
) {
1187 int ret
= bt_ctf_field_unsigned_integer_get_value(
1188 packet_size_field
, &packet_size
);
1191 if (packet_size
== 0) {
1192 BT_LOGW("Invalid packet size: packet context field indicates packet size is zero: "
1193 "notit-addr=%p, packet-context-field-addr=%p",
1194 notit
, notit
->dscopes
.stream_packet_context
);
1195 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1197 } else if ((packet_size
% 8) != 0) {
1198 BT_LOGW("Invalid packet size: packet context field indicates packet size is not a multiple of 8: "
1199 "notit-addr=%p, packet-context-field-addr=%p, "
1200 "packet-size=%" PRIu64
,
1201 notit
, notit
->dscopes
.stream_packet_context
,
1203 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1208 if (content_size_field
) {
1209 int ret
= bt_ctf_field_unsigned_integer_get_value(
1210 content_size_field
, &content_size
);
1214 content_size
= packet_size
;
1217 if (content_size
> packet_size
) {
1218 BT_LOGW("Invalid packet or content size: packet context field indicates content size is greater than packet size: "
1219 "notit-addr=%p, packet-context-field-addr=%p, "
1220 "packet-size=%" PRIu64
", content-size=%" PRIu64
,
1221 notit
, notit
->dscopes
.stream_packet_context
,
1222 packet_size
, content_size
);
1223 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1227 notit
->cur_packet_size
= packet_size
;
1228 notit
->cur_content_size
= content_size
;
1229 BT_LOGV("Set current packet and content sizes: "
1230 "notit-addr=%p, packet-size=%" PRIu64
", content-size=%" PRIu64
,
1231 notit
, packet_size
, content_size
);
1233 BT_PUT(packet_size_field
);
1234 BT_PUT(content_size_field
);
1239 enum bt_ctf_notif_iter_status
after_packet_context_state(
1240 struct bt_ctf_notif_iter
*notit
)
1242 enum bt_ctf_notif_iter_status status
;
1244 status
= set_current_packet_content_sizes(notit
);
1245 if (status
== BT_CTF_NOTIF_ITER_STATUS_OK
) {
1246 notit
->state
= STATE_EMIT_NOTIF_NEW_PACKET
;
1253 enum bt_ctf_notif_iter_status
read_event_header_begin_state(
1254 struct bt_ctf_notif_iter
*notit
)
1256 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1257 struct bt_ctf_field_type
*event_header_type
= NULL
;
1259 /* Reset the position of the last event header */
1260 notit
->buf
.last_eh_at
= notit
->buf
.at
;
1262 /* Check if we have some content left */
1263 if (notit
->cur_content_size
>= 0) {
1264 if (packet_at(notit
) == notit
->cur_content_size
) {
1265 /* No more events! */
1266 BT_LOGV("Reached end of packet: notit-addr=%p, "
1267 "cur=%zu", notit
, packet_at(notit
));
1268 notit
->state
= STATE_EMIT_NOTIF_END_OF_PACKET
;
1270 } else if (packet_at(notit
) > notit
->cur_content_size
) {
1271 /* That's not supposed to happen */
1272 BT_LOGV("Before decoding event header field: cursor is passed the packet's content: "
1273 "notit-addr=%p, content-size=%zu, "
1274 "cur=%zu", notit
, notit
->cur_content_size
,
1276 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1281 event_header_type
= bt_ctf_stream_class_get_event_header_type(
1282 notit
->meta
.stream_class
);
1283 if (!event_header_type
) {
1284 notit
->state
= STATE_AFTER_STREAM_EVENT_HEADER
;
1288 put_event_dscopes(notit
);
1289 BT_LOGV("Decoding event header field: "
1290 "notit-addr=%p, stream-class-addr=%p, "
1291 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1293 notit
, notit
->meta
.stream_class
,
1294 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1295 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1297 status
= read_dscope_begin_state(notit
, event_header_type
,
1298 STATE_AFTER_STREAM_EVENT_HEADER
,
1299 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
,
1300 ¬it
->dscopes
.stream_event_header
);
1302 BT_LOGW("Cannot decode event header field: "
1303 "notit-addr=%p, stream-class-addr=%p, "
1304 "stream-class-name=\"%s\", "
1305 "stream-class-id=%" PRId64
", ft-addr=%p",
1306 notit
, notit
->meta
.stream_class
,
1307 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1308 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1312 BT_PUT(event_header_type
);
1318 enum bt_ctf_notif_iter_status
read_event_header_continue_state(
1319 struct bt_ctf_notif_iter
*notit
)
1321 return read_dscope_continue_state(notit
,
1322 STATE_AFTER_STREAM_EVENT_HEADER
);
1326 enum bt_ctf_notif_iter_status
set_current_event_class(struct bt_ctf_notif_iter
*notit
)
1329 * The assert() calls in this function are okay because it is
1330 * assumed here that all the metadata objects have been
1331 * validated for CTF correctness before decoding actual streams.
1334 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1335 struct bt_ctf_field_type
*event_header_type
;
1336 struct bt_ctf_field_type
*id_field_type
= NULL
;
1337 struct bt_ctf_field_type
*v_field_type
= NULL
;
1338 uint64_t event_id
= -1ULL;
1341 event_header_type
= bt_ctf_stream_class_get_event_header_type(
1342 notit
->meta
.stream_class
);
1343 if (!event_header_type
) {
1345 * No event header, therefore no event class ID field,
1346 * therefore only one event class.
1348 goto single_event_class
;
1351 /* Is there any "id"/"v" field in the event header? */
1352 assert(is_struct_type(event_header_type
));
1353 id_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1354 event_header_type
, "id");
1355 v_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1356 event_header_type
, "v");
1357 assert(notit
->dscopes
.stream_event_header
);
1361 * | | |_ _|_ _| __ __ _
1362 * | | | | | || '_ \ / _` |
1363 * | |___| | | || | | | (_| | S P E C I A L
1364 * |_____|_| |_||_| |_|\__, | C A S E â„¢
1367 struct bt_ctf_field
*v_field
= NULL
;
1368 struct bt_ctf_field
*v_struct_field
= NULL
;
1369 struct bt_ctf_field
*v_struct_id_field
= NULL
;
1371 // TODO: optimalize!
1372 v_field
= bt_ctf_field_structure_get_field(
1373 notit
->dscopes
.stream_event_header
, "v");
1377 bt_ctf_field_variant_get_current_field(v_field
);
1378 if (!v_struct_field
) {
1379 goto end_v_field_type
;
1382 // TODO: optimalize!
1384 bt_ctf_field_structure_get_field(v_struct_field
, "id");
1385 if (!v_struct_id_field
) {
1386 goto end_v_field_type
;
1389 if (bt_ctf_field_is_integer(v_struct_id_field
)) {
1390 ret
= bt_ctf_field_unsigned_integer_get_value(
1391 v_struct_id_field
, &event_id
);
1393 BT_LOGV("Cannot get value of unsigned integer field (`id`): continuing: "
1394 "notit=%p, field-addr=%p",
1395 notit
, v_struct_id_field
);
1402 BT_PUT(v_struct_field
);
1403 BT_PUT(v_struct_id_field
);
1406 if (id_field_type
&& event_id
== -1ULL) {
1407 /* Check "id" field */
1408 struct bt_ctf_field
*id_field
= NULL
;
1411 // TODO: optimalize!
1412 id_field
= bt_ctf_field_structure_get_field(
1413 notit
->dscopes
.stream_event_header
, "id");
1415 goto check_event_id
;
1418 if (bt_ctf_field_is_integer(id_field
)) {
1419 ret
= bt_ctf_field_unsigned_integer_get_value(
1420 id_field
, &event_id
);
1421 } else if (bt_ctf_field_is_enumeration(id_field
)) {
1422 struct bt_ctf_field
*container
;
1424 container
= bt_ctf_field_enumeration_get_container(
1427 ret
= bt_ctf_field_unsigned_integer_get_value(
1428 container
, &event_id
);
1437 if (event_id
== -1ULL) {
1439 /* Event ID not found: single event? */
1440 assert(bt_ctf_stream_class_get_event_class_count(
1441 notit
->meta
.stream_class
) == 1);
1445 BT_LOGV("Found event class ID to use: notit-addr=%p, "
1446 "stream-class-addr=%p, stream-class-name=\"%s\", "
1447 "stream-class-id=%" PRId64
", "
1448 "event-class-id=%" PRIu64
,
1449 notit
, notit
->meta
.stream_class
,
1450 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1451 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1453 BT_PUT(notit
->meta
.event_class
);
1454 notit
->meta
.event_class
= bt_ctf_stream_class_get_event_class_by_id(
1455 notit
->meta
.stream_class
, event_id
);
1456 if (!notit
->meta
.event_class
) {
1457 BT_LOGW("No event class with ID of event class ID to use in stream class: "
1458 "notit-addr=%p, stream-class-addr=%p, "
1459 "stream-class-name=\"%s\", "
1460 "stream-class-id=%" PRId64
", "
1461 "event-class-id=%" PRIu64
,
1462 notit
, notit
->meta
.stream_class
,
1463 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1464 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1466 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1470 BT_LOGV("Set current event class: "
1471 "notit-addr=%p, event-class-addr=%p, "
1472 "event-class-name=\"%s\", event-class-id=%" PRId64
,
1473 notit
, notit
->meta
.event_class
,
1474 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
1475 bt_ctf_event_class_get_id(notit
->meta
.event_class
));
1478 BT_PUT(event_header_type
);
1479 BT_PUT(id_field_type
);
1480 BT_PUT(v_field_type
);
1486 enum bt_ctf_notif_iter_status
after_event_header_state(
1487 struct bt_ctf_notif_iter
*notit
)
1489 enum bt_ctf_notif_iter_status status
;
1491 status
= set_current_event_class(notit
);
1492 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
1496 notit
->state
= STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
;
1503 enum bt_ctf_notif_iter_status
read_stream_event_context_begin_state(
1504 struct bt_ctf_notif_iter
*notit
)
1506 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1507 struct bt_ctf_field_type
*stream_event_context_type
;
1509 stream_event_context_type
= bt_ctf_stream_class_get_event_context_type(
1510 notit
->meta
.stream_class
);
1511 if (!stream_event_context_type
) {
1512 notit
->state
= STATE_DSCOPE_EVENT_CONTEXT_BEGIN
;
1516 BT_LOGV("Decoding stream event context field: "
1517 "notit-addr=%p, stream-class-addr=%p, "
1518 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1520 notit
, notit
->meta
.stream_class
,
1521 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1522 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1523 stream_event_context_type
);
1524 status
= read_dscope_begin_state(notit
, stream_event_context_type
,
1525 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
,
1526 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
,
1527 ¬it
->dscopes
.stream_event_context
);
1529 BT_LOGW("Cannot decode stream event context field: "
1530 "notit-addr=%p, stream-class-addr=%p, "
1531 "stream-class-name=\"%s\", "
1532 "stream-class-id=%" PRId64
", ft-addr=%p",
1533 notit
, notit
->meta
.stream_class
,
1534 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1535 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1536 stream_event_context_type
);
1540 BT_PUT(stream_event_context_type
);
1546 enum bt_ctf_notif_iter_status
read_stream_event_context_continue_state(
1547 struct bt_ctf_notif_iter
*notit
)
1549 return read_dscope_continue_state(notit
,
1550 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
);
1554 enum bt_ctf_notif_iter_status
read_event_context_begin_state(
1555 struct bt_ctf_notif_iter
*notit
)
1557 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1558 struct bt_ctf_field_type
*event_context_type
;
1560 event_context_type
= bt_ctf_event_class_get_context_type(
1561 notit
->meta
.event_class
);
1562 if (!event_context_type
) {
1563 notit
->state
= STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
;
1567 BT_LOGV("Decoding event context field: "
1568 "notit-addr=%p, event-class-addr=%p, "
1569 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1571 notit
, notit
->meta
.event_class
,
1572 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
1573 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
1574 event_context_type
);
1575 status
= read_dscope_begin_state(notit
, event_context_type
,
1576 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
1577 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
,
1578 ¬it
->dscopes
.event_context
);
1580 BT_LOGW("Cannot decode event context field: "
1581 "notit-addr=%p, event-class-addr=%p, "
1582 "event-class-name=\"%s\", "
1583 "event-class-id=%" PRId64
", ft-addr=%p",
1584 notit
, notit
->meta
.event_class
,
1585 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
1586 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
1587 event_context_type
);
1591 BT_PUT(event_context_type
);
1597 enum bt_ctf_notif_iter_status
read_event_context_continue_state(
1598 struct bt_ctf_notif_iter
*notit
)
1600 return read_dscope_continue_state(notit
,
1601 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
);
1605 enum bt_ctf_notif_iter_status
read_event_payload_begin_state(
1606 struct bt_ctf_notif_iter
*notit
)
1608 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1609 struct bt_ctf_field_type
*event_payload_type
;
1611 event_payload_type
= bt_ctf_event_class_get_payload_type(
1612 notit
->meta
.event_class
);
1613 if (!event_payload_type
) {
1614 notit
->state
= STATE_EMIT_NOTIF_EVENT
;
1618 BT_LOGV("Decoding event payload field: "
1619 "notit-addr=%p, event-class-addr=%p, "
1620 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1622 notit
, notit
->meta
.event_class
,
1623 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
1624 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
1625 event_payload_type
);
1626 status
= read_dscope_begin_state(notit
, event_payload_type
,
1627 STATE_EMIT_NOTIF_EVENT
,
1628 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
1629 ¬it
->dscopes
.event_payload
);
1631 BT_LOGW("Cannot decode event payload field: "
1632 "notit-addr=%p, event-class-addr=%p, "
1633 "event-class-name=\"%s\", "
1634 "event-class-id=%" PRId64
", ft-addr=%p",
1635 notit
, notit
->meta
.event_class
,
1636 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
1637 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
1638 event_payload_type
);
1642 BT_PUT(event_payload_type
);
1648 enum bt_ctf_notif_iter_status
read_event_payload_continue_state(
1649 struct bt_ctf_notif_iter
*notit
)
1651 return read_dscope_continue_state(notit
, STATE_EMIT_NOTIF_EVENT
);
1655 enum bt_ctf_notif_iter_status
skip_packet_padding_state(
1656 struct bt_ctf_notif_iter
*notit
)
1658 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1659 size_t bits_to_skip
;
1661 assert(notit
->cur_packet_size
> 0);
1662 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1663 if (bits_to_skip
== 0) {
1664 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1667 size_t bits_to_consume
;
1669 BT_LOGV("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu",
1670 bits_to_skip
, notit
, bits_to_skip
);
1671 status
= buf_ensure_available_bits(notit
);
1672 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
1676 bits_to_consume
= MIN(buf_available_bits(notit
), bits_to_skip
);
1677 BT_LOGV("Skipping %zu bits of padding: notit-addr=%p, size=%zu",
1678 bits_to_consume
, notit
, bits_to_consume
);
1679 buf_consume_bits(notit
, bits_to_consume
);
1680 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1681 if (bits_to_skip
== 0) {
1682 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1692 enum bt_ctf_notif_iter_status
handle_state(struct bt_ctf_notif_iter
*notit
)
1694 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1695 const enum state state
= notit
->state
;
1697 BT_LOGV("Handling state: notit-addr=%p, state=%s",
1698 notit
, state_string(state
));
1700 // TODO: optimalize!
1703 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1705 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
1706 status
= read_packet_header_begin_state(notit
);
1708 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
1709 status
= read_packet_header_continue_state(notit
);
1711 case STATE_AFTER_TRACE_PACKET_HEADER
:
1712 status
= after_packet_header_state(notit
);
1714 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
1715 status
= read_packet_context_begin_state(notit
);
1717 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
1718 status
= read_packet_context_continue_state(notit
);
1720 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
1721 status
= after_packet_context_state(notit
);
1723 case STATE_EMIT_NOTIF_NEW_PACKET
:
1724 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1726 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
:
1727 status
= read_event_header_begin_state(notit
);
1729 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
:
1730 status
= read_event_header_continue_state(notit
);
1732 case STATE_AFTER_STREAM_EVENT_HEADER
:
1733 status
= after_event_header_state(notit
);
1735 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
:
1736 status
= read_stream_event_context_begin_state(notit
);
1738 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
:
1739 status
= read_stream_event_context_continue_state(notit
);
1741 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN
:
1742 status
= read_event_context_begin_state(notit
);
1744 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
:
1745 status
= read_event_context_continue_state(notit
);
1747 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
1748 status
= read_event_payload_begin_state(notit
);
1750 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
1751 status
= read_event_payload_continue_state(notit
);
1753 case STATE_EMIT_NOTIF_EVENT
:
1754 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1756 case STATE_SKIP_PACKET_PADDING
:
1757 status
= skip_packet_padding_state(notit
);
1759 case STATE_EMIT_NOTIF_END_OF_PACKET
:
1760 notit
->state
= STATE_SKIP_PACKET_PADDING
;
1763 BT_LOGD("Unknown CTF plugin notification iterator state: "
1764 "notit-addr=%p, state=%d", notit
, notit
->state
);
1768 BT_LOGV("Handled state: notit-addr=%p, status=%s, "
1769 "prev-state=%s, cur-state=%s",
1770 notit
, bt_ctf_notif_iter_status_string(status
),
1771 state_string(state
), state_string(notit
->state
));
1776 * Resets the internal state of a CTF notification iterator.
1779 void bt_ctf_notif_iter_reset(struct bt_ctf_notif_iter
*notit
)
1782 BT_LOGD("Resetting notification iterator: addr=%p", notit
);
1783 stack_clear(notit
->stack
);
1784 BT_PUT(notit
->meta
.stream_class
);
1785 BT_PUT(notit
->meta
.event_class
);
1786 BT_PUT(notit
->packet
);
1787 BT_PUT(notit
->stream
);
1788 put_all_dscopes(notit
);
1789 notit
->buf
.addr
= NULL
;
1792 notit
->buf
.last_eh_at
= -1ULL;
1793 notit
->buf
.packet_offset
= 0;
1794 notit
->state
= STATE_INIT
;
1795 notit
->cur_content_size
= -1;
1796 notit
->cur_packet_size
= -1;
1800 int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter
*notit
)
1805 * We don't put the stream class here because we need to make
1806 * sure that all the packets processed by the same notification
1807 * iterator refer to the same stream class (the first one).
1810 BT_LOGV("Switching packet: notit-addr=%p, cur=%zu",
1811 notit
, notit
->buf
.at
);
1812 stack_clear(notit
->stack
);
1813 BT_PUT(notit
->meta
.event_class
);
1814 BT_PUT(notit
->packet
);
1815 BT_PUT(notit
->cur_timestamp_end
);
1816 put_all_dscopes(notit
);
1819 * Adjust current buffer so that addr points to the beginning of the new
1822 if (notit
->buf
.addr
) {
1823 size_t consumed_bytes
= (size_t) (notit
->buf
.at
/ CHAR_BIT
);
1825 /* Packets are assumed to start on a byte frontier. */
1826 if (notit
->buf
.at
% CHAR_BIT
) {
1827 BT_LOGW("Cannot switch packet: current position is not a multiple of 8: "
1828 "notit-addr=%p, cur=%zu", notit
, notit
->buf
.at
);
1833 notit
->buf
.addr
+= consumed_bytes
;
1834 notit
->buf
.sz
-= consumed_bytes
;
1836 notit
->buf
.packet_offset
= 0;
1837 BT_LOGV("Adjusted buffer: addr=%p, size=%zu",
1838 notit
->buf
.addr
, notit
->buf
.sz
);
1841 notit
->cur_content_size
= -1;
1842 notit
->cur_packet_size
= -1;
1843 notit
->cur_sc_field_path_cache
= NULL
;
1850 struct bt_ctf_field
*get_next_field(struct bt_ctf_notif_iter
*notit
)
1852 struct bt_ctf_field
*next_field
= NULL
;
1853 struct bt_ctf_field
*base_field
;
1854 struct bt_ctf_field_type
*base_type
;
1857 assert(!stack_empty(notit
->stack
));
1858 index
= stack_top(notit
->stack
)->index
;
1859 base_field
= stack_top(notit
->stack
)->base
;
1861 base_type
= bt_ctf_field_get_type(base_field
);
1864 switch (bt_ctf_field_type_get_type_id(base_type
)) {
1865 case BT_CTF_FIELD_TYPE_ID_STRUCT
:
1866 next_field
= bt_ctf_field_structure_get_field_by_index(
1869 case BT_CTF_FIELD_TYPE_ID_ARRAY
:
1870 next_field
= bt_ctf_field_array_get_field(base_field
, index
);
1872 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
1873 next_field
= bt_ctf_field_sequence_get_field(base_field
, index
);
1875 case BT_CTF_FIELD_TYPE_ID_VARIANT
:
1876 next_field
= bt_ctf_field_variant_get_current_field(base_field
);
1879 BT_LOGF("Unknown base field type ID: "
1880 "notit-addr=%p, ft-addr=%p, ft-id=%s",
1882 bt_ctf_field_type_id_string(
1883 bt_ctf_field_type_get_type_id(base_type
)));
1892 void update_clock_state(uint64_t *state
,
1893 struct bt_ctf_field
*value_field
)
1895 struct bt_ctf_field_type
*value_type
= NULL
;
1896 uint64_t requested_new_value
;
1897 uint64_t requested_new_value_mask
;
1898 uint64_t cur_value_masked
;
1899 int requested_new_value_size
;
1902 value_type
= bt_ctf_field_get_type(value_field
);
1904 assert(bt_ctf_field_type_is_integer(value_type
));
1905 requested_new_value_size
=
1906 bt_ctf_field_type_integer_get_size(value_type
);
1907 assert(requested_new_value_size
> 0);
1908 ret
= bt_ctf_field_unsigned_integer_get_value(value_field
,
1909 &requested_new_value
);
1913 * Special case for a 64-bit new value, which is the limit
1914 * of a clock value as of this version: overwrite the
1915 * current value directly.
1917 if (requested_new_value_size
== 64) {
1918 *state
= requested_new_value
;
1922 requested_new_value_mask
= (1ULL << requested_new_value_size
) - 1;
1923 cur_value_masked
= *state
& requested_new_value_mask
;
1925 if (requested_new_value
< cur_value_masked
) {
1927 * It looks like a wrap happened on the number of bits
1928 * of the requested new value. Assume that the clock
1929 * value wrapped only one time.
1931 *state
+= requested_new_value_mask
+ 1;
1934 /* Clear the low bits of the current clock value. */
1935 *state
&= ~requested_new_value_mask
;
1937 /* Set the low bits of the current clock value. */
1938 *state
|= requested_new_value
;
1940 BT_LOGV("Updated clock's value from integer field's value: "
1941 "value=%" PRIu64
, *state
);
1946 enum bt_ctf_btr_status
update_clock(struct bt_ctf_notif_iter
*notit
,
1947 struct bt_ctf_field
*int_field
)
1949 gboolean clock_class_found
;
1950 uint64_t *clock_state
= NULL
;
1951 struct bt_ctf_field_type
*int_field_type
= NULL
;
1952 enum bt_ctf_btr_status ret
= BT_CTF_BTR_STATUS_OK
;
1953 struct bt_ctf_clock_class
*clock_class
= NULL
;
1955 int_field_type
= bt_ctf_field_get_type(int_field
);
1956 assert(int_field_type
);
1957 clock_class
= bt_ctf_field_type_integer_get_mapped_clock_class(
1959 if (likely(!clock_class
)) {
1963 clock_class_found
= g_hash_table_lookup_extended(notit
->clock_states
,
1964 clock_class
, NULL
, (gpointer
) &clock_state
);
1965 if (!clock_class_found
) {
1966 clock_state
= g_new0(uint64_t, 1);
1968 BT_LOGE_STR("Failed to allocate a uint64_t.");
1969 ret
= BT_CTF_BTR_STATUS_ENOMEM
;
1973 g_hash_table_insert(notit
->clock_states
, bt_get(clock_class
),
1977 /* Update the clock's state. */
1978 BT_LOGV("Updating notification iterator's clock's value from integer field: "
1979 "notit-addr=%p, clock-class-addr=%p, "
1980 "clock-class-name=\"%s\", value=%" PRIu64
,
1982 bt_ctf_clock_class_get_name(clock_class
), *clock_state
);
1983 update_clock_state(clock_state
, int_field
);
1985 bt_put(int_field_type
);
1986 bt_put(clock_class
);
1991 enum bt_ctf_btr_status
btr_unsigned_int_common(uint64_t value
,
1992 struct bt_ctf_field_type
*type
, void *data
,
1993 struct bt_ctf_field
**out_int_field
)
1995 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1996 struct bt_ctf_field
*field
= NULL
;
1997 struct bt_ctf_field
*int_field
= NULL
;
1998 struct bt_ctf_notif_iter
*notit
= data
;
2001 BT_LOGV("Common unsigned integer function called from BTR: "
2002 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2003 "ft-id=%s, value=%" PRIu64
,
2004 notit
, notit
->btr
, type
,
2005 bt_ctf_field_type_id_string(
2006 bt_ctf_field_type_get_type_id(type
)),
2009 /* Create next field */
2010 field
= get_next_field(notit
);
2012 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2013 status
= BT_CTF_BTR_STATUS_ERROR
;
2017 switch(bt_ctf_field_type_get_type_id(type
)) {
2018 case BT_CTF_FIELD_TYPE_ID_INTEGER
:
2019 /* Integer field is created field */
2020 BT_MOVE(int_field
, field
);
2023 case BT_CTF_FIELD_TYPE_ID_ENUM
:
2024 int_field
= bt_ctf_field_enumeration_get_container(field
);
2026 type
= bt_ctf_field_get_type(int_field
);
2030 BT_LOGF("Unexpected field type ID: "
2031 "notit-addr=%p, ft-addr=%p, ft-id=%s",
2033 bt_ctf_field_type_id_string(
2034 bt_ctf_field_type_get_type_id(type
)));
2039 ret
= bt_ctf_field_unsigned_integer_set_value(int_field
, value
);
2041 stack_top(notit
->stack
)->index
++;
2042 *out_int_field
= int_field
;
2051 enum bt_ctf_btr_status
btr_timestamp_end_cb(void *value
,
2052 struct bt_ctf_field_type
*type
, void *data
)
2054 enum bt_ctf_btr_status status
;
2055 struct bt_ctf_field
*field
= NULL
;
2056 struct bt_ctf_notif_iter
*notit
= data
;
2058 BT_LOGV("`timestamp_end` unsigned integer function called from BTR: "
2059 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2061 notit
, notit
->btr
, type
,
2062 bt_ctf_field_type_id_string(
2063 bt_ctf_field_type_get_type_id(type
)));
2064 status
= btr_unsigned_int_common(*((uint64_t *) value
), type
, data
,
2067 /* Set as the current packet's timestamp_end field. */
2068 BT_MOVE(notit
->cur_timestamp_end
, field
);
2073 enum bt_ctf_btr_status
btr_unsigned_int_cb(uint64_t value
,
2074 struct bt_ctf_field_type
*type
, void *data
)
2076 struct bt_ctf_notif_iter
*notit
= data
;
2077 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2078 struct bt_ctf_field
*field
= NULL
;
2079 struct field_cb_override
*override
;
2081 BT_LOGV("Unsigned integer function called from BTR: "
2082 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2083 "ft-id=%s, value=%" PRIu64
,
2084 notit
, notit
->btr
, type
,
2085 bt_ctf_field_type_id_string(
2086 bt_ctf_field_type_get_type_id(type
)),
2088 override
= g_hash_table_lookup(notit
->field_overrides
, type
);
2089 if (unlikely(override
)) {
2090 /* Override function logs errors */
2091 status
= override
->func(&value
, type
, override
->data
);
2095 status
= btr_unsigned_int_common(value
, type
, data
, &field
);
2096 if (status
!= BT_CTF_BTR_STATUS_OK
) {
2097 /* btr_unsigned_int_common() logs errors */
2101 status
= update_clock(notit
, field
);
2108 enum bt_ctf_btr_status
btr_signed_int_cb(int64_t value
,
2109 struct bt_ctf_field_type
*type
, void *data
)
2111 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2112 struct bt_ctf_field
*field
= NULL
;
2113 struct bt_ctf_field
*int_field
= NULL
;
2114 struct bt_ctf_notif_iter
*notit
= data
;
2117 BT_LOGV("Signed integer function called from BTR: "
2118 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2119 "ft-id=%s, value=%" PRId64
,
2120 notit
, notit
->btr
, type
,
2121 bt_ctf_field_type_id_string(
2122 bt_ctf_field_type_get_type_id(type
)),
2125 /* create next field */
2126 field
= get_next_field(notit
);
2128 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2129 status
= BT_CTF_BTR_STATUS_ERROR
;
2133 switch(bt_ctf_field_type_get_type_id(type
)) {
2134 case BT_CTF_FIELD_TYPE_ID_INTEGER
:
2135 /* Integer field is created field */
2136 BT_MOVE(int_field
, field
);
2139 case BT_CTF_FIELD_TYPE_ID_ENUM
:
2140 int_field
= bt_ctf_field_enumeration_get_container(field
);
2142 type
= bt_ctf_field_get_type(int_field
);
2146 BT_LOGF("Unexpected field type ID: "
2147 "notit-addr=%p, ft-addr=%p, ft-id=%s",
2149 bt_ctf_field_type_id_string(
2150 bt_ctf_field_type_get_type_id(type
)));
2155 ret
= bt_ctf_field_signed_integer_set_value(int_field
, value
);
2157 stack_top(notit
->stack
)->index
++;
2158 status
= update_clock(notit
, int_field
);
2168 enum bt_ctf_btr_status
btr_floating_point_cb(double value
,
2169 struct bt_ctf_field_type
*type
, void *data
)
2171 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2172 struct bt_ctf_field
*field
= NULL
;
2173 struct bt_ctf_notif_iter
*notit
= data
;
2176 BT_LOGV("Floating point number function called from BTR: "
2177 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2178 "ft-id=%s, value=%f",
2179 notit
, notit
->btr
, type
,
2180 bt_ctf_field_type_id_string(
2181 bt_ctf_field_type_get_type_id(type
)),
2184 /* Create next field */
2185 field
= get_next_field(notit
);
2187 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2188 status
= BT_CTF_BTR_STATUS_ERROR
;
2192 ret
= bt_ctf_field_floating_point_set_value(field
, value
);
2194 stack_top(notit
->stack
)->index
++;
2202 enum bt_ctf_btr_status
btr_string_begin_cb(
2203 struct bt_ctf_field_type
*type
, void *data
)
2205 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2206 struct bt_ctf_field
*field
= NULL
;
2207 struct bt_ctf_notif_iter
*notit
= data
;
2210 BT_LOGV("String (beginning) function called from BTR: "
2211 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2213 notit
, notit
->btr
, type
,
2214 bt_ctf_field_type_id_string(
2215 bt_ctf_field_type_get_type_id(type
)));
2217 /* Create next field */
2218 field
= get_next_field(notit
);
2220 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2221 status
= BT_CTF_BTR_STATUS_ERROR
;
2226 * Push on stack. Not a compound type per se, but we know that only
2227 * btr_string_cb() may be called between this call and a subsequent
2228 * call to btr_string_end_cb().
2230 ret
= stack_push(notit
->stack
, field
);
2232 BT_LOGE("Cannot push string field on stack: "
2233 "notit-addr=%p, field-addr=%p", notit
, field
);
2234 status
= BT_CTF_BTR_STATUS_ERROR
;
2239 * Initialize string field payload to an empty string since in the
2240 * case of a length 0 string the btr_string_cb won't be called and
2241 * we will end up with an unset string payload.
2243 ret
= bt_ctf_field_string_set_value(field
, "");
2245 BT_LOGE("Cannot initialize string field's value to an empty string: "
2246 "notit-addr=%p, field-addr=%p, ret=%d",
2248 status
= BT_CTF_BTR_STATUS_ERROR
;
2259 enum bt_ctf_btr_status
btr_string_cb(const char *value
,
2260 size_t len
, struct bt_ctf_field_type
*type
, void *data
)
2262 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2263 struct bt_ctf_field
*field
= NULL
;
2264 struct bt_ctf_notif_iter
*notit
= data
;
2267 BT_LOGV("String (substring) function called from BTR: "
2268 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2269 "ft-id=%s, string-length=%zu",
2270 notit
, notit
->btr
, type
,
2271 bt_ctf_field_type_id_string(
2272 bt_ctf_field_type_get_type_id(type
)),
2275 /* Get string field */
2276 field
= stack_top(notit
->stack
)->base
;
2279 /* Append current string */
2280 ret
= bt_ctf_field_string_append_len(field
, value
, len
);
2282 BT_LOGE("Cannot append substring to string field's value: "
2283 "notit-addr=%p, field-addr=%p, string-length=%zu, "
2284 "ret=%d", notit
, field
, len
, ret
);
2285 status
= BT_CTF_BTR_STATUS_ERROR
;
2294 enum bt_ctf_btr_status
btr_string_end_cb(
2295 struct bt_ctf_field_type
*type
, void *data
)
2297 struct bt_ctf_notif_iter
*notit
= data
;
2299 BT_LOGV("String (end) function called from BTR: "
2300 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2302 notit
, notit
->btr
, type
,
2303 bt_ctf_field_type_id_string(
2304 bt_ctf_field_type_get_type_id(type
)));
2306 /* Pop string field */
2307 stack_pop(notit
->stack
);
2309 /* Go to next field */
2310 stack_top(notit
->stack
)->index
++;
2311 return BT_CTF_BTR_STATUS_OK
;
2314 enum bt_ctf_btr_status
btr_compound_begin_cb(
2315 struct bt_ctf_field_type
*type
, void *data
)
2317 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2318 struct bt_ctf_notif_iter
*notit
= data
;
2319 struct bt_ctf_field
*field
;
2322 BT_LOGV("Compound (beginning) function called from BTR: "
2323 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2325 notit
, notit
->btr
, type
,
2326 bt_ctf_field_type_id_string(
2327 bt_ctf_field_type_get_type_id(type
)));
2330 if (stack_empty(notit
->stack
)) {
2331 /* Root: create dynamic scope field */
2332 *notit
->cur_dscope_field
= bt_ctf_field_create(type
);
2333 field
= *notit
->cur_dscope_field
;
2336 * Field will be put at the end of this function
2337 * (stack_push() will take one reference, but this
2338 * reference is lost upon the equivalent stack_pop()
2339 * later), so also get it for our context to own it.
2341 bt_get(*notit
->cur_dscope_field
);
2344 BT_LOGE("Cannot create compound field: "
2345 "notit-addr=%p, ft-addr=%p, ft-id=%s",
2347 bt_ctf_field_type_id_string(
2348 bt_ctf_field_type_get_type_id(type
)));
2349 status
= BT_CTF_BTR_STATUS_ERROR
;
2353 field
= get_next_field(notit
);
2355 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2356 status
= BT_CTF_BTR_STATUS_ERROR
;
2363 ret
= stack_push(notit
->stack
, field
);
2365 BT_LOGE("Cannot push compound field onto the stack: "
2366 "notit-addr=%p, ft-addr=%p, ft-id=%s, ret=%d",
2368 bt_ctf_field_type_id_string(
2369 bt_ctf_field_type_get_type_id(type
)),
2371 status
= BT_CTF_BTR_STATUS_ERROR
;
2381 enum bt_ctf_btr_status
btr_compound_end_cb(
2382 struct bt_ctf_field_type
*type
, void *data
)
2384 struct bt_ctf_notif_iter
*notit
= data
;
2386 BT_LOGV("Compound (end) function called from BTR: "
2387 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2389 notit
, notit
->btr
, type
,
2390 bt_ctf_field_type_id_string(
2391 bt_ctf_field_type_get_type_id(type
)));
2392 assert(!stack_empty(notit
->stack
));
2395 stack_pop(notit
->stack
);
2397 /* If the stack is not empty, increment the base's index */
2398 if (!stack_empty(notit
->stack
)) {
2399 stack_top(notit
->stack
)->index
++;
2402 return BT_CTF_BTR_STATUS_OK
;
2406 struct bt_ctf_field
*resolve_field(struct bt_ctf_notif_iter
*notit
,
2407 struct bt_ctf_field_path
*path
)
2409 struct bt_ctf_field
*field
= NULL
;
2412 if (BT_LOG_ON_VERBOSE
) {
2413 GString
*gstr
= bt_ctf_field_path_string(path
);
2415 BT_LOGV("Resolving field path: notit-addr=%p, field-path=\"%s\"",
2416 notit
, gstr
? gstr
->str
: NULL
);
2419 g_string_free(gstr
, TRUE
);
2423 switch (bt_ctf_field_path_get_root_scope(path
)) {
2424 case BT_CTF_SCOPE_TRACE_PACKET_HEADER
:
2425 field
= notit
->dscopes
.trace_packet_header
;
2427 case BT_CTF_SCOPE_STREAM_PACKET_CONTEXT
:
2428 field
= notit
->dscopes
.stream_packet_context
;
2430 case BT_CTF_SCOPE_STREAM_EVENT_HEADER
:
2431 field
= notit
->dscopes
.stream_event_header
;
2433 case BT_CTF_SCOPE_STREAM_EVENT_CONTEXT
:
2434 field
= notit
->dscopes
.stream_event_context
;
2436 case BT_CTF_SCOPE_EVENT_CONTEXT
:
2437 field
= notit
->dscopes
.event_context
;
2439 case BT_CTF_SCOPE_EVENT_FIELDS
:
2440 field
= notit
->dscopes
.event_payload
;
2443 BT_LOGF("Cannot resolve field path: unknown scope: "
2444 "notit-addr=%p, root-scope=%s",
2445 notit
, bt_ctf_scope_string(
2446 bt_ctf_field_path_get_root_scope(path
)));
2451 BT_LOGW("Cannot resolve field path: root field not found: "
2452 "notit-addr=%p, root-scope=%s",
2453 notit
, bt_ctf_scope_string(
2454 bt_ctf_field_path_get_root_scope(path
)));
2460 for (i
= 0; i
< bt_ctf_field_path_get_index_count(path
); ++i
) {
2461 struct bt_ctf_field
*next_field
= NULL
;
2462 struct bt_ctf_field_type
*field_type
;
2463 int index
= bt_ctf_field_path_get_index(path
, i
);
2465 field_type
= bt_ctf_field_get_type(field
);
2468 if (is_struct_type(field_type
)) {
2469 next_field
= bt_ctf_field_structure_get_field_by_index(
2471 } else if (is_variant_type(field_type
)) {
2473 bt_ctf_field_variant_get_current_field(field
);
2480 BT_LOGW("Cannot find next field: "
2481 "notit-addr=%p, ft-addr=%p, ft-id=%s, index=%d",
2483 bt_ctf_field_type_id_string(
2484 bt_ctf_field_type_get_type_id(field_type
)),
2489 /* Move next field -> field */
2490 BT_MOVE(field
, next_field
);
2498 int64_t btr_get_sequence_length_cb(struct bt_ctf_field_type
*type
, void *data
)
2502 struct bt_ctf_field
*seq_field
;
2503 struct bt_ctf_field_path
*field_path
;
2504 struct bt_ctf_notif_iter
*notit
= data
;
2505 struct bt_ctf_field
*length_field
= NULL
;
2508 field_path
= bt_ctf_field_type_sequence_get_length_field_path(type
);
2510 length_field
= resolve_field(notit
, field_path
);
2511 if (!length_field
) {
2512 BT_LOGW("Cannot resolve sequence field type's length field path: "
2513 "notit-addr=%p, ft-addr=%p",
2518 iret
= bt_ctf_field_unsigned_integer_get_value(length_field
, &length
);
2520 BT_LOGE("Cannot get value of sequence length field: "
2521 "notit-addr=%p, field-addr=%p",
2522 notit
, length_field
);
2526 seq_field
= stack_top(notit
->stack
)->base
;
2527 iret
= bt_ctf_field_sequence_set_length(seq_field
, length_field
);
2529 BT_LOGE("Cannot set sequence field's length field: "
2530 "notit-addr=%p, seq-field-addr=%p, "
2531 "length-field-addr=%p, ",
2532 notit
, seq_field
, length_field
);
2536 ret
= (int64_t) length
;
2539 BT_PUT(length_field
);
2546 struct bt_ctf_field_type
*btr_get_variant_type_cb(
2547 struct bt_ctf_field_type
*type
, void *data
)
2549 struct bt_ctf_field_path
*path
;
2550 struct bt_ctf_notif_iter
*notit
= data
;
2551 struct bt_ctf_field
*var_field
;
2552 struct bt_ctf_field
*tag_field
= NULL
;
2553 struct bt_ctf_field
*selected_field
= NULL
;
2554 struct bt_ctf_field_type
*selected_field_type
= NULL
;
2556 path
= bt_ctf_field_type_variant_get_tag_field_path(type
);
2558 tag_field
= resolve_field(notit
, path
);
2560 BT_LOGW("Cannot resolve variant field type's tag field path: "
2561 "notit-addr=%p, ft-addr=%p",
2567 * We found the enumeration tag field instance which should be
2568 * able to select a current field for this variant. This
2569 * callback function we're in is called _after_
2570 * compound_begin(), so the current stack top's base field is
2571 * the variant field in question. We get the selected field here
2572 * thanks to this tag field (thus creating the selected field),
2573 * which will also provide us with its type. Then, this field
2574 * will remain the current selected one until the next callback
2575 * function call which is used to fill the current selected
2578 var_field
= stack_top(notit
->stack
)->base
;
2579 selected_field
= bt_ctf_field_variant_get_field(var_field
, tag_field
);
2580 if (!selected_field
) {
2581 BT_LOGW("Cannot get variant field's selection using tag field: "
2582 "notit-addr=%p, var-field-addr=%p, tag-field-addr=%p",
2583 notit
, var_field
, tag_field
);
2587 selected_field_type
= bt_ctf_field_get_type(selected_field
);
2591 BT_PUT(selected_field
);
2594 return selected_field_type
;
2598 int set_event_clocks(struct bt_ctf_event
*event
,
2599 struct bt_ctf_notif_iter
*notit
)
2602 GHashTableIter iter
;
2603 struct bt_ctf_clock_class
*clock_class
;
2604 uint64_t *clock_state
;
2606 g_hash_table_iter_init(&iter
, notit
->clock_states
);
2608 while (g_hash_table_iter_next(&iter
, (gpointer
) &clock_class
,
2609 (gpointer
) &clock_state
)) {
2610 struct bt_ctf_clock_value
*clock_value
;
2612 clock_value
= bt_ctf_clock_value_create(clock_class
,
2615 BT_LOGE("Cannot create clock value from clock class: "
2616 "notit-addr=%p, clock-class-addr=%p, "
2617 "clock-class-name=\"%s\"",
2619 bt_ctf_clock_class_get_name(clock_class
));
2623 ret
= bt_ctf_event_set_clock_value(event
, clock_value
);
2624 bt_put(clock_value
);
2626 struct bt_ctf_event_class
*event_class
=
2627 bt_ctf_event_get_class(event
);
2629 assert(event_class
);
2630 BT_LOGE("Cannot set event's clock value: "
2631 "notit-addr=%p, event-addr=%p, "
2632 "event-class-name=\"%s\", "
2633 "event-class-id=%" PRId64
", "
2634 "clock-class-addr=%p, "
2635 "clock-class-name=\"%s\", "
2636 "clock-value-addr=%p",
2638 bt_ctf_event_class_get_name(event_class
),
2639 bt_ctf_event_class_get_id(event_class
),
2641 bt_ctf_clock_class_get_name(clock_class
),
2643 bt_put(event_class
);
2654 struct bt_ctf_event
*create_event(struct bt_ctf_notif_iter
*notit
)
2657 struct bt_ctf_event
*event
;
2659 BT_LOGV("Creating event for event notification: "
2660 "notit-addr=%p, event-class-addr=%p, "
2661 "event-class-name=\"%s\", "
2662 "event-class-id=%" PRId64
,
2663 notit
, notit
->meta
.event_class
,
2664 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2665 bt_ctf_event_class_get_id(notit
->meta
.event_class
));
2667 /* Create event object. */
2668 event
= bt_ctf_event_create(notit
->meta
.event_class
);
2670 BT_LOGE("Cannot create event: "
2671 "notit-addr=%p, event-class-addr=%p, "
2672 "event-class-name=\"%s\", "
2673 "event-class-id=%" PRId64
,
2674 notit
, notit
->meta
.event_class
,
2675 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2676 bt_ctf_event_class_get_id(notit
->meta
.event_class
));
2680 /* Set header, stream event context, context, and payload fields. */
2681 ret
= bt_ctf_event_set_header(event
,
2682 notit
->dscopes
.stream_event_header
);
2684 BT_LOGE("Cannot set event's header field: "
2685 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2686 "event-class-name=\"%s\", "
2687 "event-class-id=%" PRId64
", field-addr=%p",
2688 notit
, event
, notit
->meta
.event_class
,
2689 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2690 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
2691 notit
->dscopes
.stream_event_header
);
2695 ret
= bt_ctf_event_set_stream_event_context(event
,
2696 notit
->dscopes
.stream_event_context
);
2698 BT_LOGE("Cannot set event's context field: "
2699 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2700 "event-class-name=\"%s\", "
2701 "event-class-id=%" PRId64
", field-addr=%p",
2702 notit
, event
, notit
->meta
.event_class
,
2703 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2704 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
2705 notit
->dscopes
.stream_event_context
);
2709 ret
= bt_ctf_event_set_event_context(event
,
2710 notit
->dscopes
.event_context
);
2712 BT_LOGE("Cannot set event's stream event context field: "
2713 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2714 "event-class-name=\"%s\", "
2715 "event-class-id=%" PRId64
", field-addr=%p",
2716 notit
, event
, notit
->meta
.event_class
,
2717 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2718 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
2719 notit
->dscopes
.event_context
);
2723 ret
= bt_ctf_event_set_event_payload(event
,
2724 notit
->dscopes
.event_payload
);
2726 BT_LOGE("Cannot set event's payload field: "
2727 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2728 "event-class-name=\"%s\", "
2729 "event-class-id=%" PRId64
", field-addr=%p",
2730 notit
, event
, notit
->meta
.event_class
,
2731 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2732 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
2733 notit
->dscopes
.event_payload
);
2737 ret
= set_event_clocks(event
, notit
);
2739 BT_LOGE("Cannot set event's clock values: "
2740 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2741 "event-class-name=\"%s\", "
2742 "event-class-id=%" PRId64
,
2743 notit
, event
, notit
->meta
.event_class
,
2744 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2745 bt_ctf_event_class_get_id(notit
->meta
.event_class
));
2749 /* Associate with current packet. */
2750 assert(notit
->packet
);
2751 ret
= bt_ctf_event_set_packet(event
, notit
->packet
);
2753 BT_LOGE("Cannot set event's header field: "
2754 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2755 "event-class-name=\"%s\", "
2756 "event-class-id=%" PRId64
", packet-addr=%p",
2757 notit
, event
, notit
->meta
.event_class
,
2758 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2759 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
2774 uint64_t get_cur_stream_instance_id(struct bt_ctf_notif_iter
*notit
)
2776 struct bt_ctf_field
*stream_instance_id_field
= NULL
;
2777 uint64_t stream_instance_id
= -1ULL;
2780 if (!notit
->dscopes
.trace_packet_header
) {
2784 stream_instance_id_field
= bt_ctf_field_structure_get_field_by_name(
2785 notit
->dscopes
.trace_packet_header
, "stream_instance_id");
2786 if (!stream_instance_id_field
) {
2790 ret
= bt_ctf_field_unsigned_integer_get_value(stream_instance_id_field
,
2791 &stream_instance_id
);
2793 stream_instance_id
= -1ULL;
2798 bt_put(stream_instance_id_field
);
2799 return stream_instance_id
;
2803 int set_stream(struct bt_ctf_notif_iter
*notit
)
2806 struct bt_ctf_stream
*stream
= NULL
;
2808 BT_LOGV("Calling user function (get stream): notit-addr=%p, "
2809 "stream-class-addr=%p, stream-class-name=\"%s\", "
2810 "stream-class-id=%" PRId64
,
2811 notit
, notit
->meta
.stream_class
,
2812 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
2813 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
2814 stream
= bt_get(notit
->medium
.medops
.get_stream(
2815 notit
->meta
.stream_class
, get_cur_stream_instance_id(notit
),
2816 notit
->medium
.data
));
2817 BT_LOGV("User function returned: stream-addr=%p", stream
);
2819 BT_LOGW_STR("User function failed to return a stream object for the given stream class.");
2824 if (notit
->stream
&& stream
!= notit
->stream
) {
2825 BT_LOGW("User function returned a different stream than the previous one for the same sequence of packets.");
2830 BT_MOVE(notit
->stream
, stream
);
2838 void create_packet(struct bt_ctf_notif_iter
*notit
)
2841 struct bt_ctf_packet
*packet
= NULL
;
2843 BT_LOGV("Creating packet for packet notification: "
2844 "notit-addr=%p", notit
);
2846 /* Ask the user for the stream */
2847 ret
= set_stream(notit
);
2852 BT_LOGV("Creating packet from stream: "
2853 "notit-addr=%p, stream-addr=%p, "
2854 "stream-class-addr=%p, "
2855 "stream-class-name=\"%s\", "
2856 "stream-class-id=%" PRId64
,
2857 notit
, notit
->stream
, notit
->meta
.stream_class
,
2858 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
2859 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
2862 packet
= bt_ctf_packet_create(notit
->stream
);
2864 BT_LOGE("Cannot create packet from stream: "
2865 "notit-addr=%p, stream-addr=%p, "
2866 "stream-class-addr=%p, "
2867 "stream-class-name=\"%s\", "
2868 "stream-class-id=%" PRId64
,
2869 notit
, notit
->stream
, notit
->meta
.stream_class
,
2870 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
2871 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
2875 /* Set packet's context and header fields */
2876 if (notit
->dscopes
.trace_packet_header
) {
2877 ret
= bt_ctf_packet_set_header(packet
,
2878 notit
->dscopes
.trace_packet_header
);
2880 BT_LOGE("Cannot set packet's header field: "
2881 "notit-addr=%p, packet-addr=%p, "
2883 "stream-class-addr=%p, "
2884 "stream-class-name=\"%s\", "
2885 "stream-class-id=%" PRId64
", "
2887 notit
, packet
, notit
->stream
, notit
->meta
.stream_class
,
2888 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
2889 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
2890 notit
->dscopes
.trace_packet_header
);
2895 if (notit
->dscopes
.stream_packet_context
) {
2896 ret
= bt_ctf_packet_set_context(packet
,
2897 notit
->dscopes
.stream_packet_context
);
2899 BT_LOGE("Cannot set packet's context field: "
2900 "notit-addr=%p, packet-addr=%p, "
2902 "stream-class-addr=%p, "
2903 "stream-class-name=\"%s\", "
2904 "stream-class-id=%" PRId64
", "
2906 notit
, packet
, notit
->stream
, notit
->meta
.stream_class
,
2907 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
2908 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
2909 notit
->dscopes
.trace_packet_header
);
2920 BT_MOVE(notit
->packet
, packet
);
2924 void notify_new_packet(struct bt_ctf_notif_iter
*notit
,
2925 struct bt_notification
**notification
)
2927 struct bt_notification
*ret
;
2929 /* Initialize the iterator's current packet */
2930 create_packet(notit
);
2931 if (!notit
->packet
) {
2932 BT_LOGE("Cannot create packet for packet notification: "
2933 "notit-addr=%p", notit
);
2937 ret
= bt_notification_packet_begin_create(notit
->packet
);
2939 BT_LOGE("Cannot create packet beginning notification: "
2940 "notit-addr=%p, packet-addr=%p",
2941 notit
, notit
->packet
);
2944 *notification
= ret
;
2948 void notify_end_of_packet(struct bt_ctf_notif_iter
*notit
,
2949 struct bt_notification
**notification
)
2951 struct bt_notification
*ret
;
2953 if (!notit
->packet
) {
2957 ret
= bt_notification_packet_end_create(notit
->packet
);
2959 BT_LOGE("Cannot create packet end notification: "
2960 "notit-addr=%p, packet-addr=%p",
2961 notit
, notit
->packet
);
2964 BT_PUT(notit
->packet
);
2965 *notification
= ret
;
2969 void notify_event(struct bt_ctf_notif_iter
*notit
,
2970 struct bt_clock_class_priority_map
*cc_prio_map
,
2971 struct bt_notification
**notification
)
2973 struct bt_ctf_event
*event
= NULL
;
2974 struct bt_notification
*ret
= NULL
;
2976 /* Make sure that the event contains at least one bit of data */
2977 if (notit
->buf
.at
== notit
->buf
.last_eh_at
) {
2978 BT_LOGE("Cannot create empty event with 0 bits of data: "
2979 "notit-addr=%p, packet-cur=%zu",
2980 notit
, packet_at(notit
));
2985 event
= create_event(notit
);
2987 BT_LOGE("Cannot create event for event notification: "
2988 "notit-addr=%p", notit
);
2992 ret
= bt_notification_event_create(event
, cc_prio_map
);
2994 BT_LOGE("Cannot create event notification: "
2995 "notit-addr=%p, event-addr=%p, "
2996 "cc-prio-map-addr=%p",
2997 notit
, event
, cc_prio_map
);
3000 *notification
= ret
;
3006 void init_trace_field_path_cache(struct bt_ctf_trace
*trace
,
3007 struct trace_field_path_cache
*trace_field_path_cache
)
3010 int stream_instance_id
= -1;
3012 struct bt_ctf_field_type
*packet_header
= NULL
;
3014 packet_header
= bt_ctf_trace_get_packet_header_type(trace
);
3015 if (!packet_header
) {
3019 if (!bt_ctf_field_type_is_structure(packet_header
)) {
3023 count
= bt_ctf_field_type_structure_get_field_count(packet_header
);
3026 for (i
= 0; (i
< count
&& (stream_id
== -1 || stream_instance_id
== -1)); i
++) {
3028 const char *field_name
;
3030 ret
= bt_ctf_field_type_structure_get_field(packet_header
,
3031 &field_name
, NULL
, i
);
3033 BT_LOGE("Cannot get structure field's field: "
3034 "field-addr=%p, index=%d",
3039 if (stream_id
== -1 && !strcmp(field_name
, "stream_id")) {
3041 } else if (stream_instance_id
== -1 &&
3042 !strcmp(field_name
, "stream_instance_id")) {
3043 stream_instance_id
= i
;
3048 trace_field_path_cache
->stream_id
= stream_id
;
3049 trace_field_path_cache
->stream_instance_id
= stream_instance_id
;
3050 BT_PUT(packet_header
);
3054 struct bt_ctf_notif_iter
*bt_ctf_notif_iter_create(struct bt_ctf_trace
*trace
,
3055 size_t max_request_sz
,
3056 struct bt_ctf_notif_iter_medium_ops medops
, void *data
)
3058 struct bt_ctf_notif_iter
*notit
= NULL
;
3059 struct bt_ctf_btr_cbs cbs
= {
3061 .signed_int
= btr_signed_int_cb
,
3062 .unsigned_int
= btr_unsigned_int_cb
,
3063 .floating_point
= btr_floating_point_cb
,
3064 .string_begin
= btr_string_begin_cb
,
3065 .string
= btr_string_cb
,
3066 .string_end
= btr_string_end_cb
,
3067 .compound_begin
= btr_compound_begin_cb
,
3068 .compound_end
= btr_compound_end_cb
,
3071 .get_sequence_length
= btr_get_sequence_length_cb
,
3072 .get_variant_type
= btr_get_variant_type_cb
,
3077 assert(medops
.request_bytes
);
3078 assert(medops
.get_stream
);
3079 BT_LOGD("Creating CTF plugin notification iterator: "
3080 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
3082 trace
, bt_ctf_trace_get_name(trace
), max_request_sz
, data
);
3083 notit
= g_new0(struct bt_ctf_notif_iter
, 1);
3085 BT_LOGE_STR("Failed to allocate one CTF plugin notification iterator.");
3088 notit
->clock_states
= g_hash_table_new_full(g_direct_hash
,
3089 g_direct_equal
, bt_put
, g_free
);
3090 if (!notit
->clock_states
) {
3091 BT_LOGE_STR("Failed to allocate a GHashTable.");
3094 notit
->meta
.trace
= bt_get(trace
);
3095 notit
->medium
.medops
= medops
;
3096 notit
->medium
.max_request_sz
= max_request_sz
;
3097 notit
->medium
.data
= data
;
3098 notit
->stack
= stack_new(notit
);
3099 if (!notit
->stack
) {
3100 BT_LOGE_STR("Failed to create field stack.");
3104 notit
->btr
= bt_ctf_btr_create(cbs
, notit
);
3106 BT_LOGE_STR("Failed to create binary type reader (BTR).");
3110 bt_ctf_notif_iter_reset(notit
);
3111 init_trace_field_path_cache(trace
, ¬it
->trace_field_path_cache
);
3112 notit
->sc_field_path_caches
= g_hash_table_new_full(g_direct_hash
,
3113 g_direct_equal
, bt_put
, g_free
);
3114 if (!notit
->sc_field_path_caches
) {
3115 BT_LOGE_STR("Failed to allocate a GHashTable.");
3119 notit
->field_overrides
= g_hash_table_new_full(g_direct_hash
,
3120 g_direct_equal
, bt_put
, g_free
);
3121 if (!notit
->field_overrides
) {
3122 BT_LOGE_STR("Failed to allocate a GHashTable.");
3126 BT_LOGD("Created CTF plugin notification iterator: "
3127 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
3128 "data=%p, notit-addr=%p",
3129 trace
, bt_ctf_trace_get_name(trace
), max_request_sz
, data
,
3136 bt_ctf_notif_iter_destroy(notit
);
3141 void bt_ctf_notif_iter_destroy(struct bt_ctf_notif_iter
*notit
)
3143 BT_PUT(notit
->meta
.trace
);
3144 BT_PUT(notit
->meta
.stream_class
);
3145 BT_PUT(notit
->meta
.event_class
);
3146 BT_PUT(notit
->packet
);
3147 BT_PUT(notit
->stream
);
3148 BT_PUT(notit
->cur_timestamp_end
);
3149 put_all_dscopes(notit
);
3151 BT_LOGD("Destroying CTF plugin notification iterator: addr=%p", notit
);
3154 BT_LOGD_STR("Destroying field stack.");
3155 stack_destroy(notit
->stack
);
3159 BT_LOGD("Destroying BTR: btr-addr=%p", notit
->btr
);
3160 bt_ctf_btr_destroy(notit
->btr
);
3163 if (notit
->clock_states
) {
3164 g_hash_table_destroy(notit
->clock_states
);
3167 if (notit
->sc_field_path_caches
) {
3168 g_hash_table_destroy(notit
->sc_field_path_caches
);
3171 if (notit
->field_overrides
) {
3172 g_hash_table_destroy(notit
->field_overrides
);
3178 enum bt_ctf_notif_iter_status
bt_ctf_notif_iter_get_next_notification(
3179 struct bt_ctf_notif_iter
*notit
,
3180 struct bt_clock_class_priority_map
*cc_prio_map
,
3181 struct bt_notification
**notification
)
3183 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
3186 assert(notification
);
3188 BT_LOGV("Getting next notification: notit-addr=%p, cc-prio-map-addr=%p",
3189 notit
, cc_prio_map
);
3192 status
= handle_state(notit
);
3193 if (status
== BT_CTF_NOTIF_ITER_STATUS_AGAIN
) {
3194 BT_LOGV_STR("Medium returned BT_CTF_NOTIF_ITER_STATUS_AGAIN.");
3197 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
3198 if (status
== BT_CTF_NOTIF_ITER_STATUS_EOF
) {
3199 BT_LOGV_STR("Medium returned BT_CTF_NOTIF_ITER_STATUS_EOF.");
3201 BT_LOGW("Cannot handle state: "
3202 "notit-addr=%p, state=%s",
3203 notit
, state_string(notit
->state
));
3208 switch (notit
->state
) {
3209 case STATE_EMIT_NOTIF_NEW_PACKET
:
3210 /* notify_new_packet() logs errors */
3211 notify_new_packet(notit
, notification
);
3212 if (!*notification
) {
3213 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
3216 case STATE_EMIT_NOTIF_EVENT
:
3217 /* notify_event() logs errors */
3218 notify_event(notit
, cc_prio_map
, notification
);
3219 if (!*notification
) {
3220 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
3223 case STATE_EMIT_NOTIF_END_OF_PACKET
:
3224 /* Update clock with timestamp_end field. */
3225 if (notit
->cur_timestamp_end
) {
3226 enum bt_ctf_btr_status btr_status
;
3227 struct bt_ctf_field_type
*field_type
=
3228 bt_ctf_field_get_type(
3229 notit
->cur_timestamp_end
);
3232 btr_status
= update_clock(notit
,
3233 notit
->cur_timestamp_end
);
3235 if (btr_status
!= BT_CTF_BTR_STATUS_OK
) {
3236 BT_LOGW("Cannot update stream's clock value: "
3237 "notit-addr=%p", notit
);
3238 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
3243 /* notify_end_of_packet() logs errors */
3244 notify_end_of_packet(notit
, notification
);
3245 if (!*notification
) {
3246 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
3250 /* Non-emitting state: continue */
3260 enum bt_ctf_notif_iter_status
bt_ctf_notif_iter_get_packet_header_context_fields(
3261 struct bt_ctf_notif_iter
*notit
,
3262 struct bt_ctf_field
**packet_header_field
,
3263 struct bt_ctf_field
**packet_context_field
)
3265 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
3269 if (notit
->state
== STATE_EMIT_NOTIF_NEW_PACKET
) {
3270 /* We're already there */
3275 status
= handle_state(notit
);
3276 if (status
== BT_CTF_NOTIF_ITER_STATUS_AGAIN
) {
3277 BT_LOGV_STR("Medium returned BT_CTF_NOTIF_ITER_STATUS_AGAIN.");
3280 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
3281 if (status
== BT_CTF_NOTIF_ITER_STATUS_EOF
) {
3282 BT_LOGV_STR("Medium returned BT_CTF_NOTIF_ITER_STATUS_EOF.");
3284 BT_LOGW("Cannot handle state: "
3285 "notit-addr=%p, state=%s",
3286 notit
, state_string(notit
->state
));
3291 switch (notit
->state
) {
3292 case STATE_EMIT_NOTIF_NEW_PACKET
:
3294 * Packet header and context fields are
3295 * potentially decoded (or they don't exist).
3299 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
3300 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
3301 case STATE_AFTER_TRACE_PACKET_HEADER
:
3302 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
3303 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
3304 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
3305 /* Non-emitting state: continue */
3309 * We should never get past the
3310 * STATE_EMIT_NOTIF_NEW_PACKET state.
3312 BT_LOGF("Unexpected state: notit-addr=%p, state=%s",
3313 notit
, state_string(notit
->state
));
3319 if (packet_header_field
) {
3320 *packet_header_field
= bt_get(notit
->dscopes
.trace_packet_header
);
3323 if (packet_context_field
) {
3324 *packet_context_field
= bt_get(notit
->dscopes
.stream_packet_context
);
3332 void bt_ctf_notif_iter_set_medops_data(struct bt_ctf_notif_iter
*notit
,
3336 notit
->medium
.data
= medops_data
;