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
;
236 * Offset, in the underlying media, of the current packet's start
239 off_t cur_packet_offset
;
241 /* bt_ctf_clock_class to uint64_t. */
242 GHashTable
*clock_states
;
245 * Cache of the trace-constant field paths (event header type)
246 * associated to the current trace.
248 struct trace_field_path_cache trace_field_path_cache
;
251 * Field path cache associated with the current stream class.
252 * Ownership of this structure belongs to the field_path_caches HT.
254 struct stream_class_field_path_cache
*cur_sc_field_path_cache
;
256 /* bt_ctf_stream_class to struct stream_class_field_path_cache. */
257 GHashTable
*sc_field_path_caches
;
261 const char *state_string(enum state state
)
266 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
267 return "STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN";
268 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
269 return "STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE";
270 case STATE_AFTER_TRACE_PACKET_HEADER
:
271 return "STATE_AFTER_TRACE_PACKET_HEADER";
272 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
273 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN";
274 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
275 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE";
276 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
277 return "STATE_AFTER_STREAM_PACKET_CONTEXT";
278 case STATE_EMIT_NOTIF_NEW_PACKET
:
279 return "STATE_EMIT_NOTIF_NEW_PACKET";
280 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
:
281 return "STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN";
282 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
:
283 return "STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE";
284 case STATE_AFTER_STREAM_EVENT_HEADER
:
285 return "STATE_AFTER_STREAM_EVENT_HEADER";
286 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
:
287 return "STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN";
288 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
:
289 return "STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE";
290 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN
:
291 return "STATE_DSCOPE_EVENT_CONTEXT_BEGIN";
292 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
:
293 return "STATE_DSCOPE_EVENT_CONTEXT_CONTINUE";
294 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
295 return "STATE_DSCOPE_EVENT_PAYLOAD_BEGIN";
296 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
297 return "STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE";
298 case STATE_EMIT_NOTIF_EVENT
:
299 return "STATE_EMIT_NOTIF_EVENT";
300 case STATE_EMIT_NOTIF_END_OF_PACKET
:
301 return "STATE_EMIT_NOTIF_END_OF_PACKET";
302 case STATE_SKIP_PACKET_PADDING
:
303 return "STATE_SKIP_PACKET_PADDING";
310 int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter
*notit
);
313 enum bt_ctf_btr_status
btr_timestamp_end_cb(void *value
,
314 struct bt_ctf_field_type
*type
, void *data
);
317 void stack_entry_free_func(gpointer data
)
319 struct stack_entry
*entry
= data
;
326 struct stack
*stack_new(struct bt_ctf_notif_iter
*notit
)
328 struct stack
*stack
= NULL
;
330 stack
= g_new0(struct stack
, 1);
332 BT_LOGE_STR("Failed to allocate one stack.");
336 stack
->entries
= g_ptr_array_new_with_free_func(stack_entry_free_func
);
337 if (!stack
->entries
) {
338 BT_LOGE_STR("Failed to allocate a GPtrArray.");
342 BT_LOGD("Created stack: notit-addr=%p, stack-addr=%p", notit
, stack
);
351 void stack_destroy(struct stack
*stack
)
354 BT_LOGD("Destroying stack: addr=%p", stack
);
355 g_ptr_array_free(stack
->entries
, TRUE
);
360 int stack_push(struct stack
*stack
, struct bt_ctf_field
*base
)
363 struct stack_entry
*entry
;
367 BT_LOGV("Pushing base field on stack: stack-addr=%p, "
368 "stack-size-before=%u, stack-size-after=%u",
369 stack
, stack
->entries
->len
, stack
->entries
->len
+ 1);
370 entry
= g_new0(struct stack_entry
, 1);
372 BT_LOGE_STR("Failed to allocate one stack entry.");
377 entry
->base
= bt_get(base
);
378 g_ptr_array_add(stack
->entries
, entry
);
385 unsigned int stack_size(struct stack
*stack
)
389 return stack
->entries
->len
;
393 void stack_pop(struct stack
*stack
)
396 assert(stack_size(stack
));
397 BT_LOGV("Popping from stack: "
398 "stack-addr=%p, stack-size-before=%u, stack-size-after=%u",
399 stack
, stack
->entries
->len
, stack
->entries
->len
- 1);
400 g_ptr_array_remove_index(stack
->entries
, stack
->entries
->len
- 1);
404 struct stack_entry
*stack_top(struct stack
*stack
)
407 assert(stack_size(stack
));
409 return g_ptr_array_index(stack
->entries
, stack
->entries
->len
- 1);
413 bool stack_empty(struct stack
*stack
)
415 return stack_size(stack
) == 0;
419 void stack_clear(struct stack
*stack
)
423 if (!stack_empty(stack
)) {
424 BT_LOGV("Clearing stack: stack-addr=%p, stack-size=%u",
425 stack
, stack
->entries
->len
);
426 g_ptr_array_remove_range(stack
->entries
, 0, stack_size(stack
));
429 assert(stack_empty(stack
));
433 enum bt_ctf_notif_iter_status
notif_iter_status_from_m_status(
434 enum bt_ctf_notif_iter_medium_status m_status
)
436 return (int) m_status
;
440 size_t buf_size_bits(struct bt_ctf_notif_iter
*notit
)
442 return notit
->buf
.sz
* 8;
446 size_t buf_available_bits(struct bt_ctf_notif_iter
*notit
)
448 return buf_size_bits(notit
) - notit
->buf
.at
;
452 size_t packet_at(struct bt_ctf_notif_iter
*notit
)
454 return notit
->buf
.packet_offset
+ notit
->buf
.at
;
458 void buf_consume_bits(struct bt_ctf_notif_iter
*notit
, size_t incr
)
460 BT_LOGV("Advancing cursor: notit-addr=%p, cur-before=%zu, cur-after=%zu",
461 notit
, notit
->buf
.at
, notit
->buf
.at
+ incr
);
462 notit
->buf
.at
+= incr
;
466 enum bt_ctf_notif_iter_status
request_medium_bytes(
467 struct bt_ctf_notif_iter
*notit
)
469 uint8_t *buffer_addr
= NULL
;
470 size_t buffer_sz
= 0;
471 enum bt_ctf_notif_iter_medium_status m_status
;
473 BT_LOGV("Calling user function (request bytes): notit-addr=%p, "
474 "request-size=%zu", notit
, notit
->medium
.max_request_sz
);
475 m_status
= notit
->medium
.medops
.request_bytes(
476 notit
->medium
.max_request_sz
, &buffer_addr
,
477 &buffer_sz
, notit
->medium
.data
);
478 BT_LOGV("User function returned: status=%s, buf-addr=%p, buf-size=%zu",
479 bt_ctf_notif_iter_medium_status_string(m_status
),
480 buffer_addr
, buffer_sz
);
481 if (m_status
== BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK
) {
482 assert(buffer_sz
!= 0);
484 /* New packet offset is old one + old size (in bits) */
485 notit
->buf
.packet_offset
+= buf_size_bits(notit
);
487 /* Restart at the beginning of the new medium buffer */
489 notit
->buf
.last_eh_at
= SIZE_MAX
;
491 /* New medium buffer size */
492 notit
->buf
.sz
= buffer_sz
;
494 /* New medium buffer address */
495 notit
->buf
.addr
= buffer_addr
;
497 BT_LOGV("User function returned new bytes: "
498 "packet-offset=%zu, cur=%zu, size=%zu, addr=%p",
499 notit
->buf
.packet_offset
, notit
->buf
.at
,
500 notit
->buf
.sz
, notit
->buf
.addr
);
501 BT_LOGV_MEM(buffer_addr
, buffer_sz
, "Returned bytes at %p:",
503 } else if (m_status
== BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF
) {
504 struct bt_ctf_field_type
*eh_ft
= NULL
;
505 struct bt_ctf_field_type
*sec_ft
= NULL
;
506 struct bt_ctf_field_type
*ec_ft
= NULL
;
509 * User returned end of stream: validate that we're not
510 * in the middle of a packet header, packet context, or
513 if (notit
->state
== STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
) {
514 /* Beginning of packet: always valid */
518 if (!notit
->meta
.stream_class
) {
522 eh_ft
= bt_ctf_stream_class_get_event_header_type(
523 notit
->meta
.stream_class
);
524 sec_ft
= bt_ctf_stream_class_get_event_context_type(
525 notit
->meta
.stream_class
);
527 if (notit
->state
== STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
) {
529 * Beginning of event's header is only valid if
530 * the packet is not supposed to have a specific
531 * size (whole packet sequence has in fact only
534 if (notit
->cur_packet_size
== -1) {
539 if (notit
->state
== STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
) {
541 * Beginning of event's stream event context is
542 * only valid if the packet is not supposed to
543 * have a specific size (whole packet sequence
544 * has in fact only one packet), and there's no
547 if (notit
->cur_packet_size
== -1 && !eh_ft
) {
552 if (!notit
->meta
.event_class
) {
556 ec_ft
= bt_ctf_event_class_get_context_type(
557 notit
->meta
.event_class
);
559 if (notit
->state
== STATE_DSCOPE_EVENT_CONTEXT_BEGIN
) {
561 * Beginning of event's context is only valid if
562 * the packet is not supposed to have a specific
563 * size (whole packet sequence has in fact only
564 * one packet), and there's no event header and
565 * no stream event context.
567 if (notit
->cur_packet_size
== -1 && !eh_ft
&& !sec_ft
) {
572 if (notit
->state
== STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
) {
574 * Beginning of event's context is only valid if
575 * the packet is not supposed to have a specific
576 * size (whole packet sequence has in fact only
577 * one packet), and there's no event header, no
578 * stream event context, and no event context.
580 if (notit
->cur_packet_size
== -1 && !eh_ft
&& !sec_ft
&&
587 /* All other states are invalid */
588 BT_LOGW("User function returned %s, but notification iterator is in an unexpected state: "
590 bt_ctf_notif_iter_medium_status_string(m_status
),
591 state_string(notit
->state
));
592 m_status
= BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR
;
598 } else if (m_status
< 0) {
599 BT_LOGW("User function failed: status=%s",
600 bt_ctf_notif_iter_medium_status_string(m_status
));
603 return notif_iter_status_from_m_status(m_status
);
607 enum bt_ctf_notif_iter_status
buf_ensure_available_bits(
608 struct bt_ctf_notif_iter
*notit
)
610 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
612 if (buf_available_bits(notit
) == 0) {
614 * This _cannot_ return BT_CTF_NOTIF_ITER_STATUS_OK
617 status
= request_medium_bytes(notit
);
624 enum bt_ctf_notif_iter_status
read_dscope_begin_state(
625 struct bt_ctf_notif_iter
*notit
,
626 struct bt_ctf_field_type
*dscope_field_type
,
627 enum state done_state
, enum state continue_state
,
628 struct bt_ctf_field
**dscope_field
)
630 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
631 enum bt_ctf_btr_status btr_status
;
632 size_t consumed_bits
;
634 status
= buf_ensure_available_bits(notit
);
635 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
637 BT_LOGW("Cannot ensure that buffer has at least one byte: "
638 "notif-addr=%p, status=%s",
639 notit
, bt_ctf_notif_iter_status_string(status
));
641 BT_LOGV("Cannot ensure that buffer has at least one byte: "
642 "notif-addr=%p, status=%s",
643 notit
, bt_ctf_notif_iter_status_string(status
));
649 bt_put(*dscope_field
);
650 notit
->cur_dscope_field
= dscope_field
;
651 BT_LOGV("Starting BTR: notit-addr=%p, btr-addr=%p, ft-addr=%p",
652 notit
, notit
->btr
, dscope_field_type
);
653 consumed_bits
= bt_ctf_btr_start(notit
->btr
, dscope_field_type
,
654 notit
->buf
.addr
, notit
->buf
.at
, packet_at(notit
),
655 notit
->buf
.sz
, &btr_status
);
656 BT_LOGV("BTR consumed bits: size=%zu", consumed_bits
);
658 switch (btr_status
) {
659 case BT_CTF_BTR_STATUS_OK
:
660 /* type was read completely */
661 BT_LOGV_STR("Field was completely decoded.");
662 notit
->state
= done_state
;
664 case BT_CTF_BTR_STATUS_EOF
:
665 BT_LOGV_STR("BTR needs more data to decode field completely.");
666 notit
->state
= continue_state
;
669 BT_LOGW("BTR failed to start: notit-addr=%p, btr-addr=%p, "
670 "status=%s", notit
, notit
->btr
,
671 bt_ctf_btr_status_string(btr_status
));
672 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
676 /* Consume bits now since we know we're not in an error state */
677 buf_consume_bits(notit
, consumed_bits
);
684 enum bt_ctf_notif_iter_status
read_dscope_continue_state(
685 struct bt_ctf_notif_iter
*notit
, enum state done_state
)
687 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
688 enum bt_ctf_btr_status btr_status
;
689 size_t consumed_bits
;
691 status
= buf_ensure_available_bits(notit
);
692 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
694 BT_LOGW("Cannot ensure that buffer has at least one byte: "
695 "notif-addr=%p, status=%s",
696 notit
, bt_ctf_notif_iter_status_string(status
));
698 BT_LOGV("Cannot ensure that buffer has at least one byte: "
699 "notif-addr=%p, status=%s",
700 notit
, bt_ctf_notif_iter_status_string(status
));
706 BT_LOGV("Continuing BTR: notit-addr=%p, btr-addr=%p",
708 consumed_bits
= bt_ctf_btr_continue(notit
->btr
, notit
->buf
.addr
,
709 notit
->buf
.sz
, &btr_status
);
710 BT_LOGV("BTR consumed bits: size=%zu", consumed_bits
);
712 switch (btr_status
) {
713 case BT_CTF_BTR_STATUS_OK
:
714 /* Type was read completely. */
715 BT_LOGV_STR("Field was completely decoded.");
716 notit
->state
= done_state
;
718 case BT_CTF_BTR_STATUS_EOF
:
719 /* Stay in this continue state. */
720 BT_LOGV_STR("BTR needs more data to decode field completely.");
723 BT_LOGW("BTR failed to continue: notit-addr=%p, btr-addr=%p, "
724 "status=%s", notit
, notit
->btr
,
725 bt_ctf_btr_status_string(btr_status
));
726 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
730 /* Consume bits now since we know we're not in an error state. */
731 buf_consume_bits(notit
, consumed_bits
);
737 void put_event_dscopes(struct bt_ctf_notif_iter
*notit
)
739 BT_LOGV_STR("Putting event header field.");
740 BT_PUT(notit
->dscopes
.stream_event_header
);
741 BT_LOGV_STR("Putting stream event context field.");
742 BT_PUT(notit
->dscopes
.stream_event_context
);
743 BT_LOGV_STR("Putting event context field.");
744 BT_PUT(notit
->dscopes
.event_context
);
745 BT_LOGV_STR("Putting event payload field.");
746 BT_PUT(notit
->dscopes
.event_payload
);
750 void put_all_dscopes(struct bt_ctf_notif_iter
*notit
)
752 BT_LOGV_STR("Putting packet header field.");
753 BT_PUT(notit
->dscopes
.trace_packet_header
);
754 BT_LOGV_STR("Putting packet context field.");
755 BT_PUT(notit
->dscopes
.stream_packet_context
);
756 put_event_dscopes(notit
);
760 enum bt_ctf_notif_iter_status
read_packet_header_begin_state(
761 struct bt_ctf_notif_iter
*notit
)
763 struct bt_ctf_field_type
*packet_header_type
= NULL
;
764 enum bt_ctf_notif_iter_status ret
= BT_CTF_NOTIF_ITER_STATUS_OK
;
766 if (bt_ctf_notif_iter_switch_packet(notit
)) {
767 BT_LOGW("Cannot switch packet: notit-addr=%p", notit
);
768 ret
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
772 /* Packet header type is common to the whole trace. */
773 packet_header_type
= bt_ctf_trace_get_packet_header_type(
775 if (!packet_header_type
) {
776 notit
->state
= STATE_AFTER_TRACE_PACKET_HEADER
;
780 BT_LOGV("Decoding packet header field:"
781 "notit-addr=%p, trace-addr=%p, trace-name=\"%s\", ft-addr=%p",
782 notit
, notit
->meta
.trace
,
783 bt_ctf_trace_get_name(notit
->meta
.trace
), packet_header_type
);
784 ret
= read_dscope_begin_state(notit
, packet_header_type
,
785 STATE_AFTER_TRACE_PACKET_HEADER
,
786 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
787 ¬it
->dscopes
.trace_packet_header
);
789 BT_LOGW("Cannot decode packet header field: "
790 "notit-addr=%p, trace-addr=%p, "
791 "trace-name=\"%s\", ft-addr=%p",
792 notit
, notit
->meta
.trace
,
793 bt_ctf_trace_get_name(notit
->meta
.trace
),
797 BT_PUT(packet_header_type
);
802 enum bt_ctf_notif_iter_status
read_packet_header_continue_state(
803 struct bt_ctf_notif_iter
*notit
)
805 return read_dscope_continue_state(notit
,
806 STATE_AFTER_TRACE_PACKET_HEADER
);
810 bool is_struct_type(struct bt_ctf_field_type
*field_type
)
812 return bt_ctf_field_type_get_type_id(field_type
) ==
813 BT_CTF_FIELD_TYPE_ID_STRUCT
;
817 bool is_variant_type(struct bt_ctf_field_type
*field_type
)
819 return bt_ctf_field_type_get_type_id(field_type
) ==
820 BT_CTF_FIELD_TYPE_ID_VARIANT
;
824 struct stream_class_field_path_cache
*
825 create_stream_class_field_path_cache_entry(
826 struct bt_ctf_notif_iter
*notit
,
827 struct bt_ctf_stream_class
*stream_class
)
831 int timestamp_end
= -1;
832 int packet_size
= -1;
833 int content_size
= -1;
834 struct stream_class_field_path_cache
*cache_entry
= g_new0(
835 struct stream_class_field_path_cache
, 1);
836 struct bt_ctf_field_type
*event_header
= NULL
, *packet_context
= NULL
;
839 BT_LOGE_STR("Failed to allocate one stream class field path cache.");
843 event_header
= bt_ctf_stream_class_get_event_header_type(stream_class
);
844 if (event_header
&& bt_ctf_field_type_is_structure(event_header
)) {
847 count
= bt_ctf_field_type_structure_get_field_count(
851 for (i
= 0; i
< count
; i
++) {
855 ret
= bt_ctf_field_type_structure_get_field(
856 event_header
, &name
, NULL
, i
);
858 BT_LOGE("Cannot get event header structure field type's field: "
859 "notit-addr=%p, stream-class-addr=%p, "
860 "stream-class-name=\"%s\", "
861 "stream-class-id=%" PRId64
", "
862 "ft-addr=%p, index=%d",
864 bt_ctf_stream_class_get_name(stream_class
),
865 bt_ctf_stream_class_get_id(stream_class
),
870 if (v
!= -1 && id
!= -1) {
874 if (v
== -1 && strcmp(name
, "v") == 0) {
876 } else if (id
== -1 && !strcmp(name
, "id")) {
882 packet_context
= bt_ctf_stream_class_get_packet_context_type(
884 if (packet_context
&& bt_ctf_field_type_is_structure(packet_context
)) {
887 count
= bt_ctf_field_type_structure_get_field_count(
891 for (i
= 0; i
< count
; i
++) {
894 struct bt_ctf_field_type
*field_type
;
896 if (timestamp_end
!= -1 && packet_size
!= -1 &&
897 content_size
!= -1) {
901 ret
= bt_ctf_field_type_structure_get_field(
902 packet_context
, &name
, &field_type
, i
);
904 BT_LOGE("Cannot get packet context structure field type's field: "
905 "notit-addr=%p, stream-class-addr=%p, "
906 "stream-class-name=\"%s\", "
907 "stream-class-id=%" PRId64
", "
908 "ft-addr=%p, index=%d",
910 bt_ctf_stream_class_get_name(stream_class
),
911 bt_ctf_stream_class_get_id(stream_class
),
916 if (timestamp_end
== -1 &&
917 strcmp(name
, "timestamp_end") == 0) {
918 struct field_cb_override
*override
= g_new0(
919 struct field_cb_override
, 1);
926 override
->func
= btr_timestamp_end_cb
;
927 override
->data
= notit
;
928 g_hash_table_insert(notit
->field_overrides
,
929 bt_get(field_type
), override
);
931 } else if (packet_size
== -1 &&
932 !strcmp(name
, "packet_size")) {
934 } else if (content_size
== -1 &&
935 !strcmp(name
, "content_size")) {
943 cache_entry
->id
= id
;
944 cache_entry
->timestamp_end
= timestamp_end
;
945 cache_entry
->packet_size
= packet_size
;
946 cache_entry
->content_size
= content_size
;
948 BT_PUT(event_header
);
949 BT_PUT(packet_context
);
958 struct stream_class_field_path_cache
*get_stream_class_field_path_cache(
959 struct bt_ctf_notif_iter
*notit
,
960 struct bt_ctf_stream_class
*stream_class
)
962 bool cache_entry_found
;
963 struct stream_class_field_path_cache
*cache_entry
;
965 cache_entry_found
= g_hash_table_lookup_extended(
966 notit
->sc_field_path_caches
,
967 stream_class
, NULL
, (gpointer
) &cache_entry
);
968 if (unlikely(!cache_entry_found
)) {
969 cache_entry
= create_stream_class_field_path_cache_entry(notit
,
971 g_hash_table_insert(notit
->sc_field_path_caches
,
972 bt_get(stream_class
), (gpointer
) cache_entry
);
979 enum bt_ctf_notif_iter_status
set_current_stream_class(
980 struct bt_ctf_notif_iter
*notit
)
982 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
983 struct bt_ctf_field_type
*packet_header_type
= NULL
;
984 struct bt_ctf_field_type
*stream_id_field_type
= NULL
;
985 struct bt_ctf_stream_class
*new_stream_class
= NULL
;
988 /* Clear the current stream class field path cache. */
989 notit
->cur_sc_field_path_cache
= NULL
;
991 /* Is there any "stream_id" field in the packet header? */
992 packet_header_type
= bt_ctf_trace_get_packet_header_type(
994 if (!packet_header_type
) {
996 * No packet header, therefore no `stream_id` field,
997 * therefore only one stream class.
999 goto single_stream_class
;
1002 assert(is_struct_type(packet_header_type
));
1004 // TODO: optimalize!
1005 stream_id_field_type
=
1006 bt_ctf_field_type_structure_get_field_type_by_name(
1007 packet_header_type
, "stream_id");
1008 if (stream_id_field_type
) {
1009 /* Find appropriate stream class using current stream ID */
1011 struct bt_ctf_field
*stream_id_field
= NULL
;
1013 assert(notit
->dscopes
.trace_packet_header
);
1015 // TODO: optimalize!
1016 stream_id_field
= bt_ctf_field_structure_get_field(
1017 notit
->dscopes
.trace_packet_header
, "stream_id");
1018 assert(stream_id_field
);
1019 ret
= bt_ctf_field_unsigned_integer_get_value(
1020 stream_id_field
, &stream_id
);
1022 BT_PUT(stream_id_field
);
1024 single_stream_class
:
1025 /* Only one stream: pick the first stream class */
1026 assert(bt_ctf_trace_get_stream_class_count(
1027 notit
->meta
.trace
) == 1);
1031 BT_LOGV("Found stream class ID to use: notit-addr=%p, "
1032 "stream-class-id=%" PRIu64
", "
1033 "trace-addr=%p, trace-name=\"%s\"",
1034 notit
, stream_id
, notit
->meta
.trace
,
1035 bt_ctf_trace_get_name(notit
->meta
.trace
));
1037 new_stream_class
= bt_ctf_trace_get_stream_class_by_id(
1038 notit
->meta
.trace
, stream_id
);
1039 if (!new_stream_class
) {
1040 BT_LOGW("No stream class with ID of stream class ID to use in trace: "
1041 "notit-addr=%p, stream-class-id=%" PRIu64
", "
1042 "trace-addr=%p, trace-name=\"%s\"",
1043 notit
, stream_id
, notit
->meta
.trace
,
1044 bt_ctf_trace_get_name(notit
->meta
.trace
));
1045 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1049 if (notit
->meta
.stream_class
) {
1050 if (new_stream_class
!= notit
->meta
.stream_class
) {
1051 BT_LOGW("Two packets refer to two different stream classes within the same packet sequence: "
1052 "notit-addr=%p, prev-stream-class-addr=%p, "
1053 "prev-stream-class-name=\"%s\", "
1054 "prev-stream-class-id=%" PRId64
", "
1055 "next-stream-class-addr=%p, "
1056 "next-stream-class-name=\"%s\", "
1057 "next-stream-class-id=%" PRId64
", "
1058 "trace-addr=%p, trace-name=\"%s\"",
1059 notit
, notit
->meta
.stream_class
,
1060 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1061 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1063 bt_ctf_stream_class_get_name(new_stream_class
),
1064 bt_ctf_stream_class_get_id(new_stream_class
),
1066 bt_ctf_trace_get_name(notit
->meta
.trace
));
1067 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1071 BT_MOVE(notit
->meta
.stream_class
, new_stream_class
);
1074 BT_LOGV("Set current stream class: "
1075 "notit-addr=%p, stream-class-addr=%p, "
1076 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
1077 notit
, notit
->meta
.stream_class
,
1078 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1079 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
1082 * Retrieve (or lazily create) the current stream class field path
1085 notit
->cur_sc_field_path_cache
= get_stream_class_field_path_cache(
1086 notit
, notit
->meta
.stream_class
);
1087 if (!notit
->cur_sc_field_path_cache
) {
1088 BT_LOGW("Cannot retrieve stream class field path from cache: "
1089 "notit-addr=%p, stream-class-addr=%p, "
1090 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
1091 notit
, notit
->meta
.stream_class
,
1092 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1093 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
1094 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1098 BT_PUT(packet_header_type
);
1099 BT_PUT(stream_id_field_type
);
1100 bt_put(new_stream_class
);
1105 enum bt_ctf_notif_iter_status
after_packet_header_state(
1106 struct bt_ctf_notif_iter
*notit
)
1108 enum bt_ctf_notif_iter_status status
;
1110 status
= set_current_stream_class(notit
);
1111 if (status
== BT_CTF_NOTIF_ITER_STATUS_OK
) {
1112 notit
->state
= STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
;
1119 enum bt_ctf_notif_iter_status
read_packet_context_begin_state(
1120 struct bt_ctf_notif_iter
*notit
)
1122 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1123 struct bt_ctf_field_type
*packet_context_type
;
1125 assert(notit
->meta
.stream_class
);
1126 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(
1127 notit
->meta
.stream_class
);
1128 if (!packet_context_type
) {
1129 BT_LOGV("No packet packet context field type in stream class: continuing: "
1130 "notit-addr=%p, stream-class-addr=%p, "
1131 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
1132 notit
, notit
->meta
.stream_class
,
1133 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1134 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
1135 notit
->state
= STATE_AFTER_STREAM_PACKET_CONTEXT
;
1139 BT_LOGV("Decoding packet context field: "
1140 "notit-addr=%p, stream-class-addr=%p, "
1141 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1143 notit
, notit
->meta
.stream_class
,
1144 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1145 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1146 packet_context_type
);
1147 status
= read_dscope_begin_state(notit
, packet_context_type
,
1148 STATE_AFTER_STREAM_PACKET_CONTEXT
,
1149 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
1150 ¬it
->dscopes
.stream_packet_context
);
1152 BT_LOGW("Cannot decode packet context field: "
1153 "notit-addr=%p, stream-class-addr=%p, "
1154 "stream-class-name=\"%s\", "
1155 "stream-class-id=%" PRId64
", ft-addr=%p",
1156 notit
, notit
->meta
.stream_class
,
1157 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1158 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1159 packet_context_type
);
1163 BT_PUT(packet_context_type
);
1168 enum bt_ctf_notif_iter_status
read_packet_context_continue_state(
1169 struct bt_ctf_notif_iter
*notit
)
1171 return read_dscope_continue_state(notit
,
1172 STATE_AFTER_STREAM_PACKET_CONTEXT
);
1176 enum bt_ctf_notif_iter_status
set_current_packet_content_sizes(
1177 struct bt_ctf_notif_iter
*notit
)
1179 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1180 struct bt_ctf_field
*packet_size_field
= NULL
;
1181 struct bt_ctf_field
*content_size_field
= NULL
;
1182 uint64_t content_size
= -1ULL, packet_size
= -1ULL;
1184 if (!notit
->dscopes
.stream_packet_context
) {
1188 packet_size_field
= bt_ctf_field_structure_get_field(
1189 notit
->dscopes
.stream_packet_context
, "packet_size");
1190 content_size_field
= bt_ctf_field_structure_get_field(
1191 notit
->dscopes
.stream_packet_context
, "content_size");
1192 if (packet_size_field
) {
1193 int ret
= bt_ctf_field_unsigned_integer_get_value(
1194 packet_size_field
, &packet_size
);
1197 if (packet_size
== 0) {
1198 BT_LOGW("Invalid packet size: packet context field indicates packet size is zero: "
1199 "notit-addr=%p, packet-context-field-addr=%p",
1200 notit
, notit
->dscopes
.stream_packet_context
);
1201 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1203 } else if ((packet_size
% 8) != 0) {
1204 BT_LOGW("Invalid packet size: packet context field indicates packet size is not a multiple of 8: "
1205 "notit-addr=%p, packet-context-field-addr=%p, "
1206 "packet-size=%" PRIu64
,
1207 notit
, notit
->dscopes
.stream_packet_context
,
1209 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1214 if (content_size_field
) {
1215 int ret
= bt_ctf_field_unsigned_integer_get_value(
1216 content_size_field
, &content_size
);
1220 content_size
= packet_size
;
1223 if (content_size
> packet_size
) {
1224 BT_LOGW("Invalid packet or content size: packet context field indicates content size is greater than packet size: "
1225 "notit-addr=%p, packet-context-field-addr=%p, "
1226 "packet-size=%" PRIu64
", content-size=%" PRIu64
,
1227 notit
, notit
->dscopes
.stream_packet_context
,
1228 packet_size
, content_size
);
1229 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1233 if (packet_size
!= -1ULL) {
1234 notit
->cur_packet_size
= packet_size
;
1237 * Use the content size as packet size indicator if the
1238 * packet size field is missing. This means there is no
1239 * padding in this stream.
1241 notit
->cur_packet_size
= content_size
;
1243 notit
->cur_content_size
= content_size
;
1244 BT_LOGV("Set current packet and content sizes: "
1245 "notit-addr=%p, packet-size=%" PRIu64
", content-size=%" PRIu64
,
1246 notit
, packet_size
, content_size
);
1248 BT_PUT(packet_size_field
);
1249 BT_PUT(content_size_field
);
1254 enum bt_ctf_notif_iter_status
after_packet_context_state(
1255 struct bt_ctf_notif_iter
*notit
)
1257 enum bt_ctf_notif_iter_status status
;
1259 status
= set_current_packet_content_sizes(notit
);
1260 if (status
== BT_CTF_NOTIF_ITER_STATUS_OK
) {
1261 notit
->state
= STATE_EMIT_NOTIF_NEW_PACKET
;
1268 enum bt_ctf_notif_iter_status
read_event_header_begin_state(
1269 struct bt_ctf_notif_iter
*notit
)
1271 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1272 struct bt_ctf_field_type
*event_header_type
= NULL
;
1274 /* Reset the position of the last event header */
1275 notit
->buf
.last_eh_at
= notit
->buf
.at
;
1277 /* Check if we have some content left */
1278 if (notit
->cur_content_size
>= 0) {
1279 if (packet_at(notit
) == notit
->cur_content_size
) {
1280 /* No more events! */
1281 BT_LOGV("Reached end of packet: notit-addr=%p, "
1282 "cur=%zu", notit
, packet_at(notit
));
1283 notit
->state
= STATE_EMIT_NOTIF_END_OF_PACKET
;
1285 } else if (packet_at(notit
) > notit
->cur_content_size
) {
1286 /* That's not supposed to happen */
1287 BT_LOGV("Before decoding event header field: cursor is passed the packet's content: "
1288 "notit-addr=%p, content-size=%" PRId64
", "
1289 "cur=%zu", notit
, notit
->cur_content_size
,
1291 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1296 event_header_type
= bt_ctf_stream_class_get_event_header_type(
1297 notit
->meta
.stream_class
);
1298 if (!event_header_type
) {
1299 notit
->state
= STATE_AFTER_STREAM_EVENT_HEADER
;
1303 put_event_dscopes(notit
);
1304 BT_LOGV("Decoding event header field: "
1305 "notit-addr=%p, stream-class-addr=%p, "
1306 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1308 notit
, notit
->meta
.stream_class
,
1309 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1310 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1312 status
= read_dscope_begin_state(notit
, event_header_type
,
1313 STATE_AFTER_STREAM_EVENT_HEADER
,
1314 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
,
1315 ¬it
->dscopes
.stream_event_header
);
1317 BT_LOGW("Cannot decode event header field: "
1318 "notit-addr=%p, stream-class-addr=%p, "
1319 "stream-class-name=\"%s\", "
1320 "stream-class-id=%" PRId64
", ft-addr=%p",
1321 notit
, notit
->meta
.stream_class
,
1322 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1323 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1327 BT_PUT(event_header_type
);
1333 enum bt_ctf_notif_iter_status
read_event_header_continue_state(
1334 struct bt_ctf_notif_iter
*notit
)
1336 return read_dscope_continue_state(notit
,
1337 STATE_AFTER_STREAM_EVENT_HEADER
);
1341 enum bt_ctf_notif_iter_status
set_current_event_class(struct bt_ctf_notif_iter
*notit
)
1344 * The assert() calls in this function are okay because it is
1345 * assumed here that all the metadata objects have been
1346 * validated for CTF correctness before decoding actual streams.
1349 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1350 struct bt_ctf_field_type
*event_header_type
;
1351 struct bt_ctf_field_type
*id_field_type
= NULL
;
1352 struct bt_ctf_field_type
*v_field_type
= NULL
;
1353 uint64_t event_id
= -1ULL;
1356 event_header_type
= bt_ctf_stream_class_get_event_header_type(
1357 notit
->meta
.stream_class
);
1358 if (!event_header_type
) {
1360 * No event header, therefore no event class ID field,
1361 * therefore only one event class.
1363 goto single_event_class
;
1366 /* Is there any "id"/"v" field in the event header? */
1367 assert(is_struct_type(event_header_type
));
1368 id_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1369 event_header_type
, "id");
1370 v_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1371 event_header_type
, "v");
1372 assert(notit
->dscopes
.stream_event_header
);
1376 * | | |_ _|_ _| __ __ _
1377 * | | | | | || '_ \ / _` |
1378 * | |___| | | || | | | (_| | S P E C I A L
1379 * |_____|_| |_||_| |_|\__, | C A S E â„¢
1382 struct bt_ctf_field
*v_field
= NULL
;
1383 struct bt_ctf_field
*v_struct_field
= NULL
;
1384 struct bt_ctf_field
*v_struct_id_field
= NULL
;
1386 // TODO: optimalize!
1387 v_field
= bt_ctf_field_structure_get_field(
1388 notit
->dscopes
.stream_event_header
, "v");
1392 bt_ctf_field_variant_get_current_field(v_field
);
1393 if (!v_struct_field
) {
1394 goto end_v_field_type
;
1397 // TODO: optimalize!
1399 bt_ctf_field_structure_get_field(v_struct_field
, "id");
1400 if (!v_struct_id_field
) {
1401 goto end_v_field_type
;
1404 if (bt_ctf_field_is_integer(v_struct_id_field
)) {
1405 ret
= bt_ctf_field_unsigned_integer_get_value(
1406 v_struct_id_field
, &event_id
);
1408 BT_LOGV("Cannot get value of unsigned integer field (`id`): continuing: "
1409 "notit=%p, field-addr=%p",
1410 notit
, v_struct_id_field
);
1417 BT_PUT(v_struct_field
);
1418 BT_PUT(v_struct_id_field
);
1421 if (id_field_type
&& event_id
== -1ULL) {
1422 /* Check "id" field */
1423 struct bt_ctf_field
*id_field
= NULL
;
1426 // TODO: optimalize!
1427 id_field
= bt_ctf_field_structure_get_field(
1428 notit
->dscopes
.stream_event_header
, "id");
1430 goto check_event_id
;
1433 if (bt_ctf_field_is_integer(id_field
)) {
1434 ret
= bt_ctf_field_unsigned_integer_get_value(
1435 id_field
, &event_id
);
1436 } else if (bt_ctf_field_is_enumeration(id_field
)) {
1437 struct bt_ctf_field
*container
;
1439 container
= bt_ctf_field_enumeration_get_container(
1442 ret
= bt_ctf_field_unsigned_integer_get_value(
1443 container
, &event_id
);
1452 if (event_id
== -1ULL) {
1454 /* Event ID not found: single event? */
1455 assert(bt_ctf_stream_class_get_event_class_count(
1456 notit
->meta
.stream_class
) == 1);
1460 BT_LOGV("Found event class ID to use: notit-addr=%p, "
1461 "stream-class-addr=%p, stream-class-name=\"%s\", "
1462 "stream-class-id=%" PRId64
", "
1463 "event-class-id=%" PRIu64
,
1464 notit
, notit
->meta
.stream_class
,
1465 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1466 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1468 BT_PUT(notit
->meta
.event_class
);
1469 notit
->meta
.event_class
= bt_ctf_stream_class_get_event_class_by_id(
1470 notit
->meta
.stream_class
, event_id
);
1471 if (!notit
->meta
.event_class
) {
1472 BT_LOGW("No event class with ID of event class ID to use in stream class: "
1473 "notit-addr=%p, stream-class-addr=%p, "
1474 "stream-class-name=\"%s\", "
1475 "stream-class-id=%" PRId64
", "
1476 "event-class-id=%" PRIu64
,
1477 notit
, notit
->meta
.stream_class
,
1478 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1479 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1481 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1485 BT_LOGV("Set current event class: "
1486 "notit-addr=%p, event-class-addr=%p, "
1487 "event-class-name=\"%s\", event-class-id=%" PRId64
,
1488 notit
, notit
->meta
.event_class
,
1489 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
1490 bt_ctf_event_class_get_id(notit
->meta
.event_class
));
1493 BT_PUT(event_header_type
);
1494 BT_PUT(id_field_type
);
1495 BT_PUT(v_field_type
);
1501 enum bt_ctf_notif_iter_status
after_event_header_state(
1502 struct bt_ctf_notif_iter
*notit
)
1504 enum bt_ctf_notif_iter_status status
;
1506 status
= set_current_event_class(notit
);
1507 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
1511 notit
->state
= STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
;
1518 enum bt_ctf_notif_iter_status
read_stream_event_context_begin_state(
1519 struct bt_ctf_notif_iter
*notit
)
1521 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1522 struct bt_ctf_field_type
*stream_event_context_type
;
1524 stream_event_context_type
= bt_ctf_stream_class_get_event_context_type(
1525 notit
->meta
.stream_class
);
1526 if (!stream_event_context_type
) {
1527 notit
->state
= STATE_DSCOPE_EVENT_CONTEXT_BEGIN
;
1531 BT_LOGV("Decoding stream event context field: "
1532 "notit-addr=%p, stream-class-addr=%p, "
1533 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1535 notit
, notit
->meta
.stream_class
,
1536 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1537 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1538 stream_event_context_type
);
1539 status
= read_dscope_begin_state(notit
, stream_event_context_type
,
1540 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
,
1541 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
,
1542 ¬it
->dscopes
.stream_event_context
);
1544 BT_LOGW("Cannot decode stream event context field: "
1545 "notit-addr=%p, stream-class-addr=%p, "
1546 "stream-class-name=\"%s\", "
1547 "stream-class-id=%" PRId64
", ft-addr=%p",
1548 notit
, notit
->meta
.stream_class
,
1549 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1550 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1551 stream_event_context_type
);
1555 BT_PUT(stream_event_context_type
);
1561 enum bt_ctf_notif_iter_status
read_stream_event_context_continue_state(
1562 struct bt_ctf_notif_iter
*notit
)
1564 return read_dscope_continue_state(notit
,
1565 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
);
1569 enum bt_ctf_notif_iter_status
read_event_context_begin_state(
1570 struct bt_ctf_notif_iter
*notit
)
1572 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1573 struct bt_ctf_field_type
*event_context_type
;
1575 event_context_type
= bt_ctf_event_class_get_context_type(
1576 notit
->meta
.event_class
);
1577 if (!event_context_type
) {
1578 notit
->state
= STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
;
1582 BT_LOGV("Decoding event context field: "
1583 "notit-addr=%p, event-class-addr=%p, "
1584 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1586 notit
, notit
->meta
.event_class
,
1587 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
1588 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
1589 event_context_type
);
1590 status
= read_dscope_begin_state(notit
, event_context_type
,
1591 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
1592 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
,
1593 ¬it
->dscopes
.event_context
);
1595 BT_LOGW("Cannot decode event context field: "
1596 "notit-addr=%p, event-class-addr=%p, "
1597 "event-class-name=\"%s\", "
1598 "event-class-id=%" PRId64
", ft-addr=%p",
1599 notit
, notit
->meta
.event_class
,
1600 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
1601 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
1602 event_context_type
);
1606 BT_PUT(event_context_type
);
1612 enum bt_ctf_notif_iter_status
read_event_context_continue_state(
1613 struct bt_ctf_notif_iter
*notit
)
1615 return read_dscope_continue_state(notit
,
1616 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
);
1620 enum bt_ctf_notif_iter_status
read_event_payload_begin_state(
1621 struct bt_ctf_notif_iter
*notit
)
1623 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1624 struct bt_ctf_field_type
*event_payload_type
;
1626 event_payload_type
= bt_ctf_event_class_get_payload_type(
1627 notit
->meta
.event_class
);
1628 if (!event_payload_type
) {
1629 notit
->state
= STATE_EMIT_NOTIF_EVENT
;
1633 BT_LOGV("Decoding event payload field: "
1634 "notit-addr=%p, event-class-addr=%p, "
1635 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1637 notit
, notit
->meta
.event_class
,
1638 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
1639 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
1640 event_payload_type
);
1641 status
= read_dscope_begin_state(notit
, event_payload_type
,
1642 STATE_EMIT_NOTIF_EVENT
,
1643 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
1644 ¬it
->dscopes
.event_payload
);
1646 BT_LOGW("Cannot decode event payload field: "
1647 "notit-addr=%p, event-class-addr=%p, "
1648 "event-class-name=\"%s\", "
1649 "event-class-id=%" PRId64
", ft-addr=%p",
1650 notit
, notit
->meta
.event_class
,
1651 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
1652 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
1653 event_payload_type
);
1657 BT_PUT(event_payload_type
);
1663 enum bt_ctf_notif_iter_status
read_event_payload_continue_state(
1664 struct bt_ctf_notif_iter
*notit
)
1666 return read_dscope_continue_state(notit
, STATE_EMIT_NOTIF_EVENT
);
1670 enum bt_ctf_notif_iter_status
skip_packet_padding_state(
1671 struct bt_ctf_notif_iter
*notit
)
1673 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1674 size_t bits_to_skip
;
1676 assert(notit
->cur_packet_size
> 0);
1677 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1678 if (bits_to_skip
== 0) {
1679 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1682 size_t bits_to_consume
;
1684 BT_LOGV("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu",
1685 bits_to_skip
, notit
, bits_to_skip
);
1686 status
= buf_ensure_available_bits(notit
);
1687 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
1691 bits_to_consume
= MIN(buf_available_bits(notit
), bits_to_skip
);
1692 BT_LOGV("Skipping %zu bits of padding: notit-addr=%p, size=%zu",
1693 bits_to_consume
, notit
, bits_to_consume
);
1694 buf_consume_bits(notit
, bits_to_consume
);
1695 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1696 if (bits_to_skip
== 0) {
1697 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1707 enum bt_ctf_notif_iter_status
handle_state(struct bt_ctf_notif_iter
*notit
)
1709 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1710 const enum state state
= notit
->state
;
1712 BT_LOGV("Handling state: notit-addr=%p, state=%s",
1713 notit
, state_string(state
));
1715 // TODO: optimalize!
1718 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1720 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
1721 status
= read_packet_header_begin_state(notit
);
1723 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
1724 status
= read_packet_header_continue_state(notit
);
1726 case STATE_AFTER_TRACE_PACKET_HEADER
:
1727 status
= after_packet_header_state(notit
);
1729 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
1730 status
= read_packet_context_begin_state(notit
);
1732 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
1733 status
= read_packet_context_continue_state(notit
);
1735 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
1736 status
= after_packet_context_state(notit
);
1738 case STATE_EMIT_NOTIF_NEW_PACKET
:
1739 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1741 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
:
1742 status
= read_event_header_begin_state(notit
);
1744 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
:
1745 status
= read_event_header_continue_state(notit
);
1747 case STATE_AFTER_STREAM_EVENT_HEADER
:
1748 status
= after_event_header_state(notit
);
1750 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
:
1751 status
= read_stream_event_context_begin_state(notit
);
1753 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
:
1754 status
= read_stream_event_context_continue_state(notit
);
1756 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN
:
1757 status
= read_event_context_begin_state(notit
);
1759 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
:
1760 status
= read_event_context_continue_state(notit
);
1762 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
1763 status
= read_event_payload_begin_state(notit
);
1765 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
1766 status
= read_event_payload_continue_state(notit
);
1768 case STATE_EMIT_NOTIF_EVENT
:
1769 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1771 case STATE_SKIP_PACKET_PADDING
:
1772 status
= skip_packet_padding_state(notit
);
1774 case STATE_EMIT_NOTIF_END_OF_PACKET
:
1775 notit
->state
= STATE_SKIP_PACKET_PADDING
;
1778 BT_LOGD("Unknown CTF plugin notification iterator state: "
1779 "notit-addr=%p, state=%d", notit
, notit
->state
);
1783 BT_LOGV("Handled state: notit-addr=%p, status=%s, "
1784 "prev-state=%s, cur-state=%s",
1785 notit
, bt_ctf_notif_iter_status_string(status
),
1786 state_string(state
), state_string(notit
->state
));
1791 * Resets the internal state of a CTF notification iterator.
1794 void bt_ctf_notif_iter_reset(struct bt_ctf_notif_iter
*notit
)
1797 BT_LOGD("Resetting notification iterator: addr=%p", notit
);
1798 stack_clear(notit
->stack
);
1799 BT_PUT(notit
->meta
.stream_class
);
1800 BT_PUT(notit
->meta
.event_class
);
1801 BT_PUT(notit
->packet
);
1802 BT_PUT(notit
->stream
);
1803 put_all_dscopes(notit
);
1804 notit
->buf
.addr
= NULL
;
1807 notit
->buf
.last_eh_at
= SIZE_MAX
;
1808 notit
->buf
.packet_offset
= 0;
1809 notit
->state
= STATE_INIT
;
1810 notit
->cur_content_size
= -1;
1811 notit
->cur_packet_size
= -1;
1812 notit
->cur_packet_offset
= -1;
1816 int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter
*notit
)
1821 * We don't put the stream class here because we need to make
1822 * sure that all the packets processed by the same notification
1823 * iterator refer to the same stream class (the first one).
1826 if (notit
->cur_packet_size
!= -1) {
1827 notit
->cur_packet_offset
+= notit
->cur_packet_size
;
1829 BT_LOGV("Switching packet: notit-addr=%p, cur=%zu, "
1830 "packet-offset=%" PRId64
, notit
, notit
->buf
.at
,
1831 notit
->cur_packet_offset
);
1832 stack_clear(notit
->stack
);
1833 BT_PUT(notit
->meta
.event_class
);
1834 BT_PUT(notit
->packet
);
1835 BT_PUT(notit
->cur_timestamp_end
);
1836 put_all_dscopes(notit
);
1839 * Adjust current buffer so that addr points to the beginning of the new
1842 if (notit
->buf
.addr
) {
1843 size_t consumed_bytes
= (size_t) (notit
->buf
.at
/ CHAR_BIT
);
1845 /* Packets are assumed to start on a byte frontier. */
1846 if (notit
->buf
.at
% CHAR_BIT
) {
1847 BT_LOGW("Cannot switch packet: current position is not a multiple of 8: "
1848 "notit-addr=%p, cur=%zu", notit
, notit
->buf
.at
);
1853 notit
->buf
.addr
+= consumed_bytes
;
1854 notit
->buf
.sz
-= consumed_bytes
;
1856 notit
->buf
.packet_offset
= 0;
1857 BT_LOGV("Adjusted buffer: addr=%p, size=%zu",
1858 notit
->buf
.addr
, notit
->buf
.sz
);
1861 notit
->cur_content_size
= -1;
1862 notit
->cur_packet_size
= -1;
1863 notit
->cur_sc_field_path_cache
= NULL
;
1870 struct bt_ctf_field
*get_next_field(struct bt_ctf_notif_iter
*notit
)
1872 struct bt_ctf_field
*next_field
= NULL
;
1873 struct bt_ctf_field
*base_field
;
1874 struct bt_ctf_field_type
*base_type
;
1877 assert(!stack_empty(notit
->stack
));
1878 index
= stack_top(notit
->stack
)->index
;
1879 base_field
= stack_top(notit
->stack
)->base
;
1881 base_type
= bt_ctf_field_get_type(base_field
);
1884 switch (bt_ctf_field_type_get_type_id(base_type
)) {
1885 case BT_CTF_FIELD_TYPE_ID_STRUCT
:
1886 next_field
= bt_ctf_field_structure_get_field_by_index(
1889 case BT_CTF_FIELD_TYPE_ID_ARRAY
:
1890 next_field
= bt_ctf_field_array_get_field(base_field
, index
);
1892 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
1893 next_field
= bt_ctf_field_sequence_get_field(base_field
, index
);
1895 case BT_CTF_FIELD_TYPE_ID_VARIANT
:
1896 next_field
= bt_ctf_field_variant_get_current_field(base_field
);
1899 BT_LOGF("Unknown base field type ID: "
1900 "notit-addr=%p, ft-addr=%p, ft-id=%s",
1902 bt_ctf_field_type_id_string(
1903 bt_ctf_field_type_get_type_id(base_type
)));
1912 void update_clock_state(uint64_t *state
,
1913 struct bt_ctf_field
*value_field
)
1915 struct bt_ctf_field_type
*value_type
= NULL
;
1916 uint64_t requested_new_value
;
1917 uint64_t requested_new_value_mask
;
1918 uint64_t cur_value_masked
;
1919 int requested_new_value_size
;
1922 value_type
= bt_ctf_field_get_type(value_field
);
1924 assert(bt_ctf_field_type_is_integer(value_type
));
1925 requested_new_value_size
=
1926 bt_ctf_field_type_integer_get_size(value_type
);
1927 assert(requested_new_value_size
> 0);
1928 ret
= bt_ctf_field_unsigned_integer_get_value(value_field
,
1929 &requested_new_value
);
1933 * Special case for a 64-bit new value, which is the limit
1934 * of a clock value as of this version: overwrite the
1935 * current value directly.
1937 if (requested_new_value_size
== 64) {
1938 *state
= requested_new_value
;
1942 requested_new_value_mask
= (1ULL << requested_new_value_size
) - 1;
1943 cur_value_masked
= *state
& requested_new_value_mask
;
1945 if (requested_new_value
< cur_value_masked
) {
1947 * It looks like a wrap happened on the number of bits
1948 * of the requested new value. Assume that the clock
1949 * value wrapped only one time.
1951 *state
+= requested_new_value_mask
+ 1;
1954 /* Clear the low bits of the current clock value. */
1955 *state
&= ~requested_new_value_mask
;
1957 /* Set the low bits of the current clock value. */
1958 *state
|= requested_new_value
;
1960 BT_LOGV("Updated clock's value from integer field's value: "
1961 "value=%" PRIu64
, *state
);
1966 enum bt_ctf_btr_status
update_clock(struct bt_ctf_notif_iter
*notit
,
1967 struct bt_ctf_field
*int_field
)
1969 gboolean clock_class_found
;
1970 uint64_t *clock_state
= NULL
;
1971 struct bt_ctf_field_type
*int_field_type
= NULL
;
1972 enum bt_ctf_btr_status ret
= BT_CTF_BTR_STATUS_OK
;
1973 struct bt_ctf_clock_class
*clock_class
= NULL
;
1975 int_field_type
= bt_ctf_field_get_type(int_field
);
1976 assert(int_field_type
);
1977 clock_class
= bt_ctf_field_type_integer_get_mapped_clock_class(
1979 if (likely(!clock_class
)) {
1983 clock_class_found
= g_hash_table_lookup_extended(notit
->clock_states
,
1984 clock_class
, NULL
, (gpointer
) &clock_state
);
1985 if (!clock_class_found
) {
1986 clock_state
= g_new0(uint64_t, 1);
1988 BT_LOGE_STR("Failed to allocate a uint64_t.");
1989 ret
= BT_CTF_BTR_STATUS_ENOMEM
;
1993 g_hash_table_insert(notit
->clock_states
, bt_get(clock_class
),
1997 /* Update the clock's state. */
1998 BT_LOGV("Updating notification iterator's clock's value from integer field: "
1999 "notit-addr=%p, clock-class-addr=%p, "
2000 "clock-class-name=\"%s\", value=%" PRIu64
,
2002 bt_ctf_clock_class_get_name(clock_class
), *clock_state
);
2003 update_clock_state(clock_state
, int_field
);
2005 bt_put(int_field_type
);
2006 bt_put(clock_class
);
2011 enum bt_ctf_btr_status
btr_unsigned_int_common(uint64_t value
,
2012 struct bt_ctf_field_type
*type
, void *data
,
2013 struct bt_ctf_field
**out_int_field
)
2015 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2016 struct bt_ctf_field
*field
= NULL
;
2017 struct bt_ctf_field
*int_field
= NULL
;
2018 struct bt_ctf_notif_iter
*notit
= data
;
2021 BT_LOGV("Common unsigned integer function called from BTR: "
2022 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2023 "ft-id=%s, value=%" PRIu64
,
2024 notit
, notit
->btr
, type
,
2025 bt_ctf_field_type_id_string(
2026 bt_ctf_field_type_get_type_id(type
)),
2029 /* Create next field */
2030 field
= get_next_field(notit
);
2032 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2033 status
= BT_CTF_BTR_STATUS_ERROR
;
2037 switch(bt_ctf_field_type_get_type_id(type
)) {
2038 case BT_CTF_FIELD_TYPE_ID_INTEGER
:
2039 /* Integer field is created field */
2040 BT_MOVE(int_field
, field
);
2043 case BT_CTF_FIELD_TYPE_ID_ENUM
:
2044 int_field
= bt_ctf_field_enumeration_get_container(field
);
2046 type
= bt_ctf_field_get_type(int_field
);
2050 BT_LOGF("Unexpected field type ID: "
2051 "notit-addr=%p, ft-addr=%p, ft-id=%s",
2053 bt_ctf_field_type_id_string(
2054 bt_ctf_field_type_get_type_id(type
)));
2059 ret
= bt_ctf_field_unsigned_integer_set_value(int_field
, value
);
2061 stack_top(notit
->stack
)->index
++;
2062 *out_int_field
= int_field
;
2071 enum bt_ctf_btr_status
btr_timestamp_end_cb(void *value
,
2072 struct bt_ctf_field_type
*type
, void *data
)
2074 enum bt_ctf_btr_status status
;
2075 struct bt_ctf_field
*field
= NULL
;
2076 struct bt_ctf_notif_iter
*notit
= data
;
2078 BT_LOGV("`timestamp_end` unsigned integer function called from BTR: "
2079 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2081 notit
, notit
->btr
, type
,
2082 bt_ctf_field_type_id_string(
2083 bt_ctf_field_type_get_type_id(type
)));
2084 status
= btr_unsigned_int_common(*((uint64_t *) value
), type
, data
,
2087 /* Set as the current packet's timestamp_end field. */
2088 BT_MOVE(notit
->cur_timestamp_end
, field
);
2093 enum bt_ctf_btr_status
btr_unsigned_int_cb(uint64_t value
,
2094 struct bt_ctf_field_type
*type
, void *data
)
2096 struct bt_ctf_notif_iter
*notit
= data
;
2097 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2098 struct bt_ctf_field
*field
= NULL
;
2099 struct field_cb_override
*override
;
2101 BT_LOGV("Unsigned integer function called from BTR: "
2102 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2103 "ft-id=%s, value=%" PRIu64
,
2104 notit
, notit
->btr
, type
,
2105 bt_ctf_field_type_id_string(
2106 bt_ctf_field_type_get_type_id(type
)),
2108 override
= g_hash_table_lookup(notit
->field_overrides
, type
);
2109 if (unlikely(override
)) {
2110 /* Override function logs errors */
2111 status
= override
->func(&value
, type
, override
->data
);
2115 status
= btr_unsigned_int_common(value
, type
, data
, &field
);
2116 if (status
!= BT_CTF_BTR_STATUS_OK
) {
2117 /* btr_unsigned_int_common() logs errors */
2121 status
= update_clock(notit
, field
);
2128 enum bt_ctf_btr_status
btr_signed_int_cb(int64_t value
,
2129 struct bt_ctf_field_type
*type
, void *data
)
2131 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2132 struct bt_ctf_field
*field
= NULL
;
2133 struct bt_ctf_field
*int_field
= NULL
;
2134 struct bt_ctf_notif_iter
*notit
= data
;
2137 BT_LOGV("Signed integer function called from BTR: "
2138 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2139 "ft-id=%s, value=%" PRId64
,
2140 notit
, notit
->btr
, type
,
2141 bt_ctf_field_type_id_string(
2142 bt_ctf_field_type_get_type_id(type
)),
2145 /* create next field */
2146 field
= get_next_field(notit
);
2148 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2149 status
= BT_CTF_BTR_STATUS_ERROR
;
2153 switch(bt_ctf_field_type_get_type_id(type
)) {
2154 case BT_CTF_FIELD_TYPE_ID_INTEGER
:
2155 /* Integer field is created field */
2156 BT_MOVE(int_field
, field
);
2159 case BT_CTF_FIELD_TYPE_ID_ENUM
:
2160 int_field
= bt_ctf_field_enumeration_get_container(field
);
2162 type
= bt_ctf_field_get_type(int_field
);
2166 BT_LOGF("Unexpected field type ID: "
2167 "notit-addr=%p, ft-addr=%p, ft-id=%s",
2169 bt_ctf_field_type_id_string(
2170 bt_ctf_field_type_get_type_id(type
)));
2175 ret
= bt_ctf_field_signed_integer_set_value(int_field
, value
);
2177 stack_top(notit
->stack
)->index
++;
2178 status
= update_clock(notit
, int_field
);
2188 enum bt_ctf_btr_status
btr_floating_point_cb(double value
,
2189 struct bt_ctf_field_type
*type
, void *data
)
2191 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2192 struct bt_ctf_field
*field
= NULL
;
2193 struct bt_ctf_notif_iter
*notit
= data
;
2196 BT_LOGV("Floating point number function called from BTR: "
2197 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2198 "ft-id=%s, value=%f",
2199 notit
, notit
->btr
, type
,
2200 bt_ctf_field_type_id_string(
2201 bt_ctf_field_type_get_type_id(type
)),
2204 /* Create next field */
2205 field
= get_next_field(notit
);
2207 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2208 status
= BT_CTF_BTR_STATUS_ERROR
;
2212 ret
= bt_ctf_field_floating_point_set_value(field
, value
);
2214 stack_top(notit
->stack
)->index
++;
2222 enum bt_ctf_btr_status
btr_string_begin_cb(
2223 struct bt_ctf_field_type
*type
, void *data
)
2225 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2226 struct bt_ctf_field
*field
= NULL
;
2227 struct bt_ctf_notif_iter
*notit
= data
;
2230 BT_LOGV("String (beginning) function called from BTR: "
2231 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2233 notit
, notit
->btr
, type
,
2234 bt_ctf_field_type_id_string(
2235 bt_ctf_field_type_get_type_id(type
)));
2237 /* Create next field */
2238 field
= get_next_field(notit
);
2240 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2241 status
= BT_CTF_BTR_STATUS_ERROR
;
2246 * Push on stack. Not a compound type per se, but we know that only
2247 * btr_string_cb() may be called between this call and a subsequent
2248 * call to btr_string_end_cb().
2250 ret
= stack_push(notit
->stack
, field
);
2252 BT_LOGE("Cannot push string field on stack: "
2253 "notit-addr=%p, field-addr=%p", notit
, field
);
2254 status
= BT_CTF_BTR_STATUS_ERROR
;
2259 * Initialize string field payload to an empty string since in the
2260 * case of a length 0 string the btr_string_cb won't be called and
2261 * we will end up with an unset string payload.
2263 ret
= bt_ctf_field_string_set_value(field
, "");
2265 BT_LOGE("Cannot initialize string field's value to an empty string: "
2266 "notit-addr=%p, field-addr=%p, ret=%d",
2268 status
= BT_CTF_BTR_STATUS_ERROR
;
2279 enum bt_ctf_btr_status
btr_string_cb(const char *value
,
2280 size_t len
, struct bt_ctf_field_type
*type
, void *data
)
2282 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2283 struct bt_ctf_field
*field
= NULL
;
2284 struct bt_ctf_notif_iter
*notit
= data
;
2287 BT_LOGV("String (substring) function called from BTR: "
2288 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2289 "ft-id=%s, string-length=%zu",
2290 notit
, notit
->btr
, type
,
2291 bt_ctf_field_type_id_string(
2292 bt_ctf_field_type_get_type_id(type
)),
2295 /* Get string field */
2296 field
= stack_top(notit
->stack
)->base
;
2299 /* Append current string */
2300 ret
= bt_ctf_field_string_append_len(field
, value
, len
);
2302 BT_LOGE("Cannot append substring to string field's value: "
2303 "notit-addr=%p, field-addr=%p, string-length=%zu, "
2304 "ret=%d", notit
, field
, len
, ret
);
2305 status
= BT_CTF_BTR_STATUS_ERROR
;
2314 enum bt_ctf_btr_status
btr_string_end_cb(
2315 struct bt_ctf_field_type
*type
, void *data
)
2317 struct bt_ctf_notif_iter
*notit
= data
;
2319 BT_LOGV("String (end) function called from BTR: "
2320 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2322 notit
, notit
->btr
, type
,
2323 bt_ctf_field_type_id_string(
2324 bt_ctf_field_type_get_type_id(type
)));
2326 /* Pop string field */
2327 stack_pop(notit
->stack
);
2329 /* Go to next field */
2330 stack_top(notit
->stack
)->index
++;
2331 return BT_CTF_BTR_STATUS_OK
;
2334 enum bt_ctf_btr_status
btr_compound_begin_cb(
2335 struct bt_ctf_field_type
*type
, void *data
)
2337 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2338 struct bt_ctf_notif_iter
*notit
= data
;
2339 struct bt_ctf_field
*field
;
2342 BT_LOGV("Compound (beginning) function called from BTR: "
2343 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2345 notit
, notit
->btr
, type
,
2346 bt_ctf_field_type_id_string(
2347 bt_ctf_field_type_get_type_id(type
)));
2350 if (stack_empty(notit
->stack
)) {
2351 /* Root: create dynamic scope field */
2352 *notit
->cur_dscope_field
= bt_ctf_field_create(type
);
2353 field
= *notit
->cur_dscope_field
;
2356 * Field will be put at the end of this function
2357 * (stack_push() will take one reference, but this
2358 * reference is lost upon the equivalent stack_pop()
2359 * later), so also get it for our context to own it.
2361 bt_get(*notit
->cur_dscope_field
);
2364 BT_LOGE("Cannot create compound field: "
2365 "notit-addr=%p, ft-addr=%p, ft-id=%s",
2367 bt_ctf_field_type_id_string(
2368 bt_ctf_field_type_get_type_id(type
)));
2369 status
= BT_CTF_BTR_STATUS_ERROR
;
2373 field
= get_next_field(notit
);
2375 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2376 status
= BT_CTF_BTR_STATUS_ERROR
;
2383 ret
= stack_push(notit
->stack
, field
);
2385 BT_LOGE("Cannot push compound field onto the stack: "
2386 "notit-addr=%p, ft-addr=%p, ft-id=%s, ret=%d",
2388 bt_ctf_field_type_id_string(
2389 bt_ctf_field_type_get_type_id(type
)),
2391 status
= BT_CTF_BTR_STATUS_ERROR
;
2401 enum bt_ctf_btr_status
btr_compound_end_cb(
2402 struct bt_ctf_field_type
*type
, void *data
)
2404 struct bt_ctf_notif_iter
*notit
= data
;
2406 BT_LOGV("Compound (end) function called from BTR: "
2407 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2409 notit
, notit
->btr
, type
,
2410 bt_ctf_field_type_id_string(
2411 bt_ctf_field_type_get_type_id(type
)));
2412 assert(!stack_empty(notit
->stack
));
2415 stack_pop(notit
->stack
);
2417 /* If the stack is not empty, increment the base's index */
2418 if (!stack_empty(notit
->stack
)) {
2419 stack_top(notit
->stack
)->index
++;
2422 return BT_CTF_BTR_STATUS_OK
;
2426 struct bt_ctf_field
*resolve_field(struct bt_ctf_notif_iter
*notit
,
2427 struct bt_ctf_field_path
*path
)
2429 struct bt_ctf_field
*field
= NULL
;
2432 if (BT_LOG_ON_VERBOSE
) {
2433 GString
*gstr
= bt_ctf_field_path_string(path
);
2435 BT_LOGV("Resolving field path: notit-addr=%p, field-path=\"%s\"",
2436 notit
, gstr
? gstr
->str
: NULL
);
2439 g_string_free(gstr
, TRUE
);
2443 switch (bt_ctf_field_path_get_root_scope(path
)) {
2444 case BT_CTF_SCOPE_TRACE_PACKET_HEADER
:
2445 field
= notit
->dscopes
.trace_packet_header
;
2447 case BT_CTF_SCOPE_STREAM_PACKET_CONTEXT
:
2448 field
= notit
->dscopes
.stream_packet_context
;
2450 case BT_CTF_SCOPE_STREAM_EVENT_HEADER
:
2451 field
= notit
->dscopes
.stream_event_header
;
2453 case BT_CTF_SCOPE_STREAM_EVENT_CONTEXT
:
2454 field
= notit
->dscopes
.stream_event_context
;
2456 case BT_CTF_SCOPE_EVENT_CONTEXT
:
2457 field
= notit
->dscopes
.event_context
;
2459 case BT_CTF_SCOPE_EVENT_FIELDS
:
2460 field
= notit
->dscopes
.event_payload
;
2463 BT_LOGF("Cannot resolve field path: unknown scope: "
2464 "notit-addr=%p, root-scope=%s",
2465 notit
, bt_ctf_scope_string(
2466 bt_ctf_field_path_get_root_scope(path
)));
2471 BT_LOGW("Cannot resolve field path: root field not found: "
2472 "notit-addr=%p, root-scope=%s",
2473 notit
, bt_ctf_scope_string(
2474 bt_ctf_field_path_get_root_scope(path
)));
2480 for (i
= 0; i
< bt_ctf_field_path_get_index_count(path
); ++i
) {
2481 struct bt_ctf_field
*next_field
= NULL
;
2482 struct bt_ctf_field_type
*field_type
;
2483 int index
= bt_ctf_field_path_get_index(path
, i
);
2485 field_type
= bt_ctf_field_get_type(field
);
2488 if (is_struct_type(field_type
)) {
2489 next_field
= bt_ctf_field_structure_get_field_by_index(
2491 } else if (is_variant_type(field_type
)) {
2493 bt_ctf_field_variant_get_current_field(field
);
2500 BT_LOGW("Cannot find next field: "
2501 "notit-addr=%p, ft-addr=%p, ft-id=%s, index=%d",
2503 bt_ctf_field_type_id_string(
2504 bt_ctf_field_type_get_type_id(field_type
)),
2509 /* Move next field -> field */
2510 BT_MOVE(field
, next_field
);
2518 int64_t btr_get_sequence_length_cb(struct bt_ctf_field_type
*type
, void *data
)
2522 struct bt_ctf_field
*seq_field
;
2523 struct bt_ctf_field_path
*field_path
;
2524 struct bt_ctf_notif_iter
*notit
= data
;
2525 struct bt_ctf_field
*length_field
= NULL
;
2528 field_path
= bt_ctf_field_type_sequence_get_length_field_path(type
);
2530 length_field
= resolve_field(notit
, field_path
);
2531 if (!length_field
) {
2532 BT_LOGW("Cannot resolve sequence field type's length field path: "
2533 "notit-addr=%p, ft-addr=%p",
2538 iret
= bt_ctf_field_unsigned_integer_get_value(length_field
, &length
);
2540 BT_LOGE("Cannot get value of sequence length field: "
2541 "notit-addr=%p, field-addr=%p",
2542 notit
, length_field
);
2546 seq_field
= stack_top(notit
->stack
)->base
;
2547 iret
= bt_ctf_field_sequence_set_length(seq_field
, length_field
);
2549 BT_LOGE("Cannot set sequence field's length field: "
2550 "notit-addr=%p, seq-field-addr=%p, "
2551 "length-field-addr=%p, ",
2552 notit
, seq_field
, length_field
);
2556 ret
= (int64_t) length
;
2559 BT_PUT(length_field
);
2566 struct bt_ctf_field_type
*btr_get_variant_type_cb(
2567 struct bt_ctf_field_type
*type
, void *data
)
2569 struct bt_ctf_field_path
*path
;
2570 struct bt_ctf_notif_iter
*notit
= data
;
2571 struct bt_ctf_field
*var_field
;
2572 struct bt_ctf_field
*tag_field
= NULL
;
2573 struct bt_ctf_field
*selected_field
= NULL
;
2574 struct bt_ctf_field_type
*selected_field_type
= NULL
;
2576 path
= bt_ctf_field_type_variant_get_tag_field_path(type
);
2578 tag_field
= resolve_field(notit
, path
);
2580 BT_LOGW("Cannot resolve variant field type's tag field path: "
2581 "notit-addr=%p, ft-addr=%p",
2587 * We found the enumeration tag field instance which should be
2588 * able to select a current field for this variant. This
2589 * callback function we're in is called _after_
2590 * compound_begin(), so the current stack top's base field is
2591 * the variant field in question. We get the selected field here
2592 * thanks to this tag field (thus creating the selected field),
2593 * which will also provide us with its type. Then, this field
2594 * will remain the current selected one until the next callback
2595 * function call which is used to fill the current selected
2598 var_field
= stack_top(notit
->stack
)->base
;
2599 selected_field
= bt_ctf_field_variant_get_field(var_field
, tag_field
);
2600 if (!selected_field
) {
2601 BT_LOGW("Cannot get variant field's selection using tag field: "
2602 "notit-addr=%p, var-field-addr=%p, tag-field-addr=%p",
2603 notit
, var_field
, tag_field
);
2607 selected_field_type
= bt_ctf_field_get_type(selected_field
);
2611 BT_PUT(selected_field
);
2614 return selected_field_type
;
2618 int set_event_clocks(struct bt_ctf_event
*event
,
2619 struct bt_ctf_notif_iter
*notit
)
2622 GHashTableIter iter
;
2623 struct bt_ctf_clock_class
*clock_class
;
2624 uint64_t *clock_state
;
2626 g_hash_table_iter_init(&iter
, notit
->clock_states
);
2628 while (g_hash_table_iter_next(&iter
, (gpointer
) &clock_class
,
2629 (gpointer
) &clock_state
)) {
2630 struct bt_ctf_clock_value
*clock_value
;
2632 clock_value
= bt_ctf_clock_value_create(clock_class
,
2635 BT_LOGE("Cannot create clock value from clock class: "
2636 "notit-addr=%p, clock-class-addr=%p, "
2637 "clock-class-name=\"%s\"",
2639 bt_ctf_clock_class_get_name(clock_class
));
2643 ret
= bt_ctf_event_set_clock_value(event
, clock_value
);
2644 bt_put(clock_value
);
2646 struct bt_ctf_event_class
*event_class
=
2647 bt_ctf_event_get_class(event
);
2649 assert(event_class
);
2650 BT_LOGE("Cannot set event's clock value: "
2651 "notit-addr=%p, event-addr=%p, "
2652 "event-class-name=\"%s\", "
2653 "event-class-id=%" PRId64
", "
2654 "clock-class-addr=%p, "
2655 "clock-class-name=\"%s\", "
2656 "clock-value-addr=%p",
2658 bt_ctf_event_class_get_name(event_class
),
2659 bt_ctf_event_class_get_id(event_class
),
2661 bt_ctf_clock_class_get_name(clock_class
),
2663 bt_put(event_class
);
2674 struct bt_ctf_event
*create_event(struct bt_ctf_notif_iter
*notit
)
2677 struct bt_ctf_event
*event
;
2679 BT_LOGV("Creating event for event notification: "
2680 "notit-addr=%p, event-class-addr=%p, "
2681 "event-class-name=\"%s\", "
2682 "event-class-id=%" PRId64
,
2683 notit
, notit
->meta
.event_class
,
2684 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2685 bt_ctf_event_class_get_id(notit
->meta
.event_class
));
2687 /* Create event object. */
2688 event
= bt_ctf_event_create(notit
->meta
.event_class
);
2690 BT_LOGE("Cannot create event: "
2691 "notit-addr=%p, event-class-addr=%p, "
2692 "event-class-name=\"%s\", "
2693 "event-class-id=%" PRId64
,
2694 notit
, 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
));
2700 /* Set header, stream event context, context, and payload fields. */
2701 ret
= bt_ctf_event_set_header(event
,
2702 notit
->dscopes
.stream_event_header
);
2704 BT_LOGE("Cannot set event's header 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
.stream_event_header
);
2715 ret
= bt_ctf_event_set_stream_event_context(event
,
2716 notit
->dscopes
.stream_event_context
);
2718 BT_LOGE("Cannot set event's context 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
.stream_event_context
);
2729 ret
= bt_ctf_event_set_event_context(event
,
2730 notit
->dscopes
.event_context
);
2732 BT_LOGE("Cannot set event's stream event context field: "
2733 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2734 "event-class-name=\"%s\", "
2735 "event-class-id=%" PRId64
", field-addr=%p",
2736 notit
, event
, notit
->meta
.event_class
,
2737 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2738 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
2739 notit
->dscopes
.event_context
);
2743 ret
= bt_ctf_event_set_event_payload(event
,
2744 notit
->dscopes
.event_payload
);
2746 BT_LOGE("Cannot set event's payload field: "
2747 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2748 "event-class-name=\"%s\", "
2749 "event-class-id=%" PRId64
", field-addr=%p",
2750 notit
, event
, notit
->meta
.event_class
,
2751 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2752 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
2753 notit
->dscopes
.event_payload
);
2757 ret
= set_event_clocks(event
, notit
);
2759 BT_LOGE("Cannot set event's clock values: "
2760 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2761 "event-class-name=\"%s\", "
2762 "event-class-id=%" PRId64
,
2763 notit
, event
, notit
->meta
.event_class
,
2764 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2765 bt_ctf_event_class_get_id(notit
->meta
.event_class
));
2769 /* Associate with current packet. */
2770 assert(notit
->packet
);
2771 ret
= bt_ctf_event_set_packet(event
, notit
->packet
);
2773 BT_LOGE("Cannot set event's header field: "
2774 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2775 "event-class-name=\"%s\", "
2776 "event-class-id=%" PRId64
", packet-addr=%p",
2777 notit
, event
, notit
->meta
.event_class
,
2778 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2779 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
2794 uint64_t get_cur_stream_instance_id(struct bt_ctf_notif_iter
*notit
)
2796 struct bt_ctf_field
*stream_instance_id_field
= NULL
;
2797 uint64_t stream_instance_id
= -1ULL;
2800 if (!notit
->dscopes
.trace_packet_header
) {
2804 stream_instance_id_field
= bt_ctf_field_structure_get_field_by_name(
2805 notit
->dscopes
.trace_packet_header
, "stream_instance_id");
2806 if (!stream_instance_id_field
) {
2810 ret
= bt_ctf_field_unsigned_integer_get_value(stream_instance_id_field
,
2811 &stream_instance_id
);
2813 stream_instance_id
= -1ULL;
2818 bt_put(stream_instance_id_field
);
2819 return stream_instance_id
;
2823 int set_stream(struct bt_ctf_notif_iter
*notit
)
2826 struct bt_ctf_stream
*stream
= NULL
;
2828 BT_LOGV("Calling user function (get stream): notit-addr=%p, "
2829 "stream-class-addr=%p, stream-class-name=\"%s\", "
2830 "stream-class-id=%" PRId64
,
2831 notit
, notit
->meta
.stream_class
,
2832 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
2833 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
2834 stream
= bt_get(notit
->medium
.medops
.get_stream(
2835 notit
->meta
.stream_class
, get_cur_stream_instance_id(notit
),
2836 notit
->medium
.data
));
2837 BT_LOGV("User function returned: stream-addr=%p", stream
);
2839 BT_LOGW_STR("User function failed to return a stream object for the given stream class.");
2844 if (notit
->stream
&& stream
!= notit
->stream
) {
2845 BT_LOGW("User function returned a different stream than the previous one for the same sequence of packets.");
2850 BT_MOVE(notit
->stream
, stream
);
2858 void create_packet(struct bt_ctf_notif_iter
*notit
)
2861 struct bt_ctf_packet
*packet
= NULL
;
2863 BT_LOGV("Creating packet for packet notification: "
2864 "notit-addr=%p", notit
);
2866 /* Ask the user for the stream */
2867 ret
= set_stream(notit
);
2872 BT_LOGV("Creating packet from stream: "
2873 "notit-addr=%p, stream-addr=%p, "
2874 "stream-class-addr=%p, "
2875 "stream-class-name=\"%s\", "
2876 "stream-class-id=%" PRId64
,
2877 notit
, notit
->stream
, notit
->meta
.stream_class
,
2878 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
2879 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
2882 packet
= bt_ctf_packet_create(notit
->stream
);
2884 BT_LOGE("Cannot create packet from stream: "
2885 "notit-addr=%p, stream-addr=%p, "
2886 "stream-class-addr=%p, "
2887 "stream-class-name=\"%s\", "
2888 "stream-class-id=%" PRId64
,
2889 notit
, notit
->stream
, notit
->meta
.stream_class
,
2890 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
2891 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
2895 /* Set packet's context and header fields */
2896 if (notit
->dscopes
.trace_packet_header
) {
2897 ret
= bt_ctf_packet_set_header(packet
,
2898 notit
->dscopes
.trace_packet_header
);
2900 BT_LOGE("Cannot set packet's header field: "
2901 "notit-addr=%p, packet-addr=%p, "
2903 "stream-class-addr=%p, "
2904 "stream-class-name=\"%s\", "
2905 "stream-class-id=%" PRId64
", "
2907 notit
, packet
, notit
->stream
, notit
->meta
.stream_class
,
2908 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
2909 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
2910 notit
->dscopes
.trace_packet_header
);
2915 if (notit
->dscopes
.stream_packet_context
) {
2916 ret
= bt_ctf_packet_set_context(packet
,
2917 notit
->dscopes
.stream_packet_context
);
2919 BT_LOGE("Cannot set packet's context field: "
2920 "notit-addr=%p, packet-addr=%p, "
2922 "stream-class-addr=%p, "
2923 "stream-class-name=\"%s\", "
2924 "stream-class-id=%" PRId64
", "
2926 notit
, packet
, notit
->stream
, notit
->meta
.stream_class
,
2927 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
2928 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
2929 notit
->dscopes
.trace_packet_header
);
2940 BT_MOVE(notit
->packet
, packet
);
2944 void notify_new_packet(struct bt_ctf_notif_iter
*notit
,
2945 struct bt_notification
**notification
)
2947 struct bt_notification
*ret
;
2949 /* Initialize the iterator's current packet */
2950 create_packet(notit
);
2951 if (!notit
->packet
) {
2952 BT_LOGE("Cannot create packet for packet notification: "
2953 "notit-addr=%p", notit
);
2957 ret
= bt_notification_packet_begin_create(notit
->packet
);
2959 BT_LOGE("Cannot create packet beginning notification: "
2960 "notit-addr=%p, packet-addr=%p",
2961 notit
, notit
->packet
);
2964 *notification
= ret
;
2968 void notify_end_of_packet(struct bt_ctf_notif_iter
*notit
,
2969 struct bt_notification
**notification
)
2971 struct bt_notification
*ret
;
2973 if (!notit
->packet
) {
2977 ret
= bt_notification_packet_end_create(notit
->packet
);
2979 BT_LOGE("Cannot create packet end notification: "
2980 "notit-addr=%p, packet-addr=%p",
2981 notit
, notit
->packet
);
2984 BT_PUT(notit
->packet
);
2985 *notification
= ret
;
2989 void notify_event(struct bt_ctf_notif_iter
*notit
,
2990 struct bt_clock_class_priority_map
*cc_prio_map
,
2991 struct bt_notification
**notification
)
2993 struct bt_ctf_event
*event
= NULL
;
2994 struct bt_notification
*ret
= NULL
;
2996 /* Make sure that the event contains at least one bit of data */
2997 if (notit
->buf
.at
== notit
->buf
.last_eh_at
) {
2998 BT_LOGE("Cannot create empty event with 0 bits of data: "
2999 "notit-addr=%p, packet-cur=%zu",
3000 notit
, packet_at(notit
));
3005 event
= create_event(notit
);
3007 BT_LOGE("Cannot create event for event notification: "
3008 "notit-addr=%p", notit
);
3012 ret
= bt_notification_event_create(event
, cc_prio_map
);
3014 BT_LOGE("Cannot create event notification: "
3015 "notit-addr=%p, event-addr=%p, "
3016 "cc-prio-map-addr=%p",
3017 notit
, event
, cc_prio_map
);
3020 *notification
= ret
;
3026 void init_trace_field_path_cache(struct bt_ctf_trace
*trace
,
3027 struct trace_field_path_cache
*trace_field_path_cache
)
3030 int stream_instance_id
= -1;
3032 struct bt_ctf_field_type
*packet_header
= NULL
;
3034 packet_header
= bt_ctf_trace_get_packet_header_type(trace
);
3035 if (!packet_header
) {
3039 if (!bt_ctf_field_type_is_structure(packet_header
)) {
3043 count
= bt_ctf_field_type_structure_get_field_count(packet_header
);
3046 for (i
= 0; (i
< count
&& (stream_id
== -1 || stream_instance_id
== -1)); i
++) {
3048 const char *field_name
;
3050 ret
= bt_ctf_field_type_structure_get_field(packet_header
,
3051 &field_name
, NULL
, i
);
3053 BT_LOGE("Cannot get structure field's field: "
3054 "field-addr=%p, index=%d",
3059 if (stream_id
== -1 && !strcmp(field_name
, "stream_id")) {
3061 } else if (stream_instance_id
== -1 &&
3062 !strcmp(field_name
, "stream_instance_id")) {
3063 stream_instance_id
= i
;
3068 trace_field_path_cache
->stream_id
= stream_id
;
3069 trace_field_path_cache
->stream_instance_id
= stream_instance_id
;
3070 BT_PUT(packet_header
);
3074 struct bt_ctf_notif_iter
*bt_ctf_notif_iter_create(struct bt_ctf_trace
*trace
,
3075 size_t max_request_sz
,
3076 struct bt_ctf_notif_iter_medium_ops medops
, void *data
)
3078 struct bt_ctf_notif_iter
*notit
= NULL
;
3079 struct bt_ctf_btr_cbs cbs
= {
3081 .signed_int
= btr_signed_int_cb
,
3082 .unsigned_int
= btr_unsigned_int_cb
,
3083 .floating_point
= btr_floating_point_cb
,
3084 .string_begin
= btr_string_begin_cb
,
3085 .string
= btr_string_cb
,
3086 .string_end
= btr_string_end_cb
,
3087 .compound_begin
= btr_compound_begin_cb
,
3088 .compound_end
= btr_compound_end_cb
,
3091 .get_sequence_length
= btr_get_sequence_length_cb
,
3092 .get_variant_type
= btr_get_variant_type_cb
,
3097 assert(medops
.request_bytes
);
3098 assert(medops
.get_stream
);
3099 BT_LOGD("Creating CTF plugin notification iterator: "
3100 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
3102 trace
, bt_ctf_trace_get_name(trace
), max_request_sz
, data
);
3103 notit
= g_new0(struct bt_ctf_notif_iter
, 1);
3105 BT_LOGE_STR("Failed to allocate one CTF plugin notification iterator.");
3108 notit
->clock_states
= g_hash_table_new_full(g_direct_hash
,
3109 g_direct_equal
, bt_put
, g_free
);
3110 if (!notit
->clock_states
) {
3111 BT_LOGE_STR("Failed to allocate a GHashTable.");
3114 notit
->meta
.trace
= bt_get(trace
);
3115 notit
->medium
.medops
= medops
;
3116 notit
->medium
.max_request_sz
= max_request_sz
;
3117 notit
->medium
.data
= data
;
3118 notit
->stack
= stack_new(notit
);
3119 if (!notit
->stack
) {
3120 BT_LOGE_STR("Failed to create field stack.");
3124 notit
->btr
= bt_ctf_btr_create(cbs
, notit
);
3126 BT_LOGE_STR("Failed to create binary type reader (BTR).");
3130 bt_ctf_notif_iter_reset(notit
);
3131 init_trace_field_path_cache(trace
, ¬it
->trace_field_path_cache
);
3132 notit
->sc_field_path_caches
= g_hash_table_new_full(g_direct_hash
,
3133 g_direct_equal
, bt_put
, g_free
);
3134 if (!notit
->sc_field_path_caches
) {
3135 BT_LOGE_STR("Failed to allocate a GHashTable.");
3139 notit
->field_overrides
= g_hash_table_new_full(g_direct_hash
,
3140 g_direct_equal
, bt_put
, g_free
);
3141 if (!notit
->field_overrides
) {
3142 BT_LOGE_STR("Failed to allocate a GHashTable.");
3146 BT_LOGD("Created CTF plugin notification iterator: "
3147 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
3148 "data=%p, notit-addr=%p",
3149 trace
, bt_ctf_trace_get_name(trace
), max_request_sz
, data
,
3151 notit
->cur_packet_offset
= 0;
3157 bt_ctf_notif_iter_destroy(notit
);
3162 void bt_ctf_notif_iter_destroy(struct bt_ctf_notif_iter
*notit
)
3164 BT_PUT(notit
->meta
.trace
);
3165 BT_PUT(notit
->meta
.stream_class
);
3166 BT_PUT(notit
->meta
.event_class
);
3167 BT_PUT(notit
->packet
);
3168 BT_PUT(notit
->stream
);
3169 BT_PUT(notit
->cur_timestamp_end
);
3170 put_all_dscopes(notit
);
3172 BT_LOGD("Destroying CTF plugin notification iterator: addr=%p", notit
);
3175 BT_LOGD_STR("Destroying field stack.");
3176 stack_destroy(notit
->stack
);
3180 BT_LOGD("Destroying BTR: btr-addr=%p", notit
->btr
);
3181 bt_ctf_btr_destroy(notit
->btr
);
3184 if (notit
->clock_states
) {
3185 g_hash_table_destroy(notit
->clock_states
);
3188 if (notit
->sc_field_path_caches
) {
3189 g_hash_table_destroy(notit
->sc_field_path_caches
);
3192 if (notit
->field_overrides
) {
3193 g_hash_table_destroy(notit
->field_overrides
);
3199 enum bt_ctf_notif_iter_status
bt_ctf_notif_iter_get_next_notification(
3200 struct bt_ctf_notif_iter
*notit
,
3201 struct bt_clock_class_priority_map
*cc_prio_map
,
3202 struct bt_notification
**notification
)
3204 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
3207 assert(notification
);
3209 BT_LOGV("Getting next notification: notit-addr=%p, cc-prio-map-addr=%p",
3210 notit
, cc_prio_map
);
3213 status
= handle_state(notit
);
3214 if (status
== BT_CTF_NOTIF_ITER_STATUS_AGAIN
) {
3215 BT_LOGV_STR("Medium returned BT_CTF_NOTIF_ITER_STATUS_AGAIN.");
3218 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
3219 if (status
== BT_CTF_NOTIF_ITER_STATUS_EOF
) {
3220 BT_LOGV_STR("Medium returned BT_CTF_NOTIF_ITER_STATUS_EOF.");
3222 BT_LOGW("Cannot handle state: "
3223 "notit-addr=%p, state=%s",
3224 notit
, state_string(notit
->state
));
3229 switch (notit
->state
) {
3230 case STATE_EMIT_NOTIF_NEW_PACKET
:
3231 /* notify_new_packet() logs errors */
3232 notify_new_packet(notit
, notification
);
3233 if (!*notification
) {
3234 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
3237 case STATE_EMIT_NOTIF_EVENT
:
3238 /* notify_event() logs errors */
3239 notify_event(notit
, cc_prio_map
, notification
);
3240 if (!*notification
) {
3241 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
3244 case STATE_EMIT_NOTIF_END_OF_PACKET
:
3245 /* Update clock with timestamp_end field. */
3246 if (notit
->cur_timestamp_end
) {
3247 enum bt_ctf_btr_status btr_status
;
3248 struct bt_ctf_field_type
*field_type
=
3249 bt_ctf_field_get_type(
3250 notit
->cur_timestamp_end
);
3253 btr_status
= update_clock(notit
,
3254 notit
->cur_timestamp_end
);
3256 if (btr_status
!= BT_CTF_BTR_STATUS_OK
) {
3257 BT_LOGW("Cannot update stream's clock value: "
3258 "notit-addr=%p", notit
);
3259 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
3264 /* notify_end_of_packet() logs errors */
3265 notify_end_of_packet(notit
, notification
);
3266 if (!*notification
) {
3267 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
3271 /* Non-emitting state: continue */
3281 enum bt_ctf_notif_iter_status
bt_ctf_notif_iter_get_packet_header_context_fields(
3282 struct bt_ctf_notif_iter
*notit
,
3283 struct bt_ctf_field
**packet_header_field
,
3284 struct bt_ctf_field
**packet_context_field
)
3287 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
3291 if (notit
->state
== STATE_EMIT_NOTIF_NEW_PACKET
) {
3292 /* We're already there */
3297 status
= handle_state(notit
);
3298 if (status
== BT_CTF_NOTIF_ITER_STATUS_AGAIN
) {
3299 BT_LOGV_STR("Medium returned BT_CTF_NOTIF_ITER_STATUS_AGAIN.");
3302 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
3303 if (status
== BT_CTF_NOTIF_ITER_STATUS_EOF
) {
3304 BT_LOGV_STR("Medium returned BT_CTF_NOTIF_ITER_STATUS_EOF.");
3306 BT_LOGW("Cannot handle state: "
3307 "notit-addr=%p, state=%s",
3308 notit
, state_string(notit
->state
));
3313 switch (notit
->state
) {
3314 case STATE_EMIT_NOTIF_NEW_PACKET
:
3316 * Packet header and context fields are
3317 * potentially decoded (or they don't exist).
3321 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
3322 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
3323 case STATE_AFTER_TRACE_PACKET_HEADER
:
3324 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
3325 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
3326 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
3327 /* Non-emitting state: continue */
3331 * We should never get past the
3332 * STATE_EMIT_NOTIF_NEW_PACKET state.
3334 BT_LOGF("Unexpected state: notit-addr=%p, state=%s",
3335 notit
, state_string(notit
->state
));
3341 if (packet_header_field
) {
3342 *packet_header_field
= bt_get(notit
->dscopes
.trace_packet_header
);
3345 if (packet_context_field
) {
3346 *packet_context_field
= bt_get(notit
->dscopes
.stream_packet_context
);
3349 ret
= set_current_packet_content_sizes(notit
);
3351 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
3359 void bt_ctf_notif_iter_set_medops_data(struct bt_ctf_notif_iter
*notit
,
3363 notit
->medium
.data
= medops_data
;
3367 enum bt_ctf_notif_iter_status
bt_ctf_notif_iter_seek(
3368 struct bt_ctf_notif_iter
*notit
, off_t offset
)
3370 enum bt_ctf_notif_iter_status ret
= BT_CTF_NOTIF_ITER_STATUS_OK
;
3371 enum bt_ctf_notif_iter_medium_status medium_status
;
3375 BT_LOGE("Cannot seek to negative offset: offset=%jd", offset
);
3376 ret
= BT_CTF_NOTIF_ITER_STATUS_INVAL
;
3380 if (!notit
->medium
.medops
.seek
) {
3381 ret
= BT_CTF_NOTIF_ITER_STATUS_UNSUPPORTED
;
3382 BT_LOGD("Aborting seek as the iterator's underlying media does not implement seek support.");
3386 medium_status
= notit
->medium
.medops
.seek(
3387 BT_CTF_NOTIF_ITER_SEEK_WHENCE_SET
, offset
,
3388 notit
->medium
.data
);
3389 if (medium_status
!= BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK
) {
3390 if (medium_status
== BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF
) {
3391 ret
= BT_CTF_NOTIF_ITER_STATUS_EOF
;
3393 ret
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
3398 bt_ctf_notif_iter_reset(notit
);
3399 notit
->cur_packet_offset
= offset
;
3405 off_t
bt_ctf_notif_iter_get_current_packet_offset(
3406 struct bt_ctf_notif_iter
*notit
)
3409 return notit
->cur_packet_offset
;
3413 off_t
bt_ctf_notif_iter_get_current_packet_size(
3414 struct bt_ctf_notif_iter
*notit
)
3417 return notit
->cur_packet_size
;