2 * Babeltrace - CTF notification iterator
4 * Copyright (c) 2015-2016 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2015-2016 Philippe Proulx <pproulx@efficios.com>
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 #define BT_LOG_TAG "PLUGIN-CTF-NOTIF-ITER"
36 #include <babeltrace/ctf-ir/field-types.h>
37 #include <babeltrace/ctf-ir/field-types-internal.h>
38 #include <babeltrace/ctf-ir/field-path.h>
39 #include <babeltrace/ctf-ir/field-path-internal.h>
40 #include <babeltrace/ctf-ir/fields.h>
41 #include <babeltrace/ctf-ir/stream-class.h>
42 #include <babeltrace/ctf-ir/packet.h>
43 #include <babeltrace/ctf-ir/stream.h>
44 #include <babeltrace/ctf-ir/clock-class.h>
45 #include <babeltrace/ctf-ir/event-class.h>
46 #include <babeltrace/graph/notification-packet.h>
47 #include <babeltrace/graph/notification-event.h>
48 #include <babeltrace/graph/notification-stream.h>
49 #include <babeltrace/graph/clock-class-priority-map.h>
50 #include <babeltrace/ref.h>
54 #include "notif-iter.h"
55 #include "../btr/btr.h"
57 struct bt_ctf_notif_iter
;
59 /* A visit stack entry */
62 * Current base field, one of:
70 * Field is owned by this.
72 struct bt_ctf_field
*base
;
74 /* index of next field to set */
80 /* Entries (struct stack_entry *) (top is last element) */
87 STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
,
88 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
89 STATE_AFTER_TRACE_PACKET_HEADER
,
90 STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
,
91 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
92 STATE_AFTER_STREAM_PACKET_CONTEXT
,
93 STATE_EMIT_NOTIF_NEW_PACKET
,
94 STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
,
95 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
,
96 STATE_AFTER_STREAM_EVENT_HEADER
,
97 STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
,
98 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
,
99 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
,
100 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
,
101 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
102 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
103 STATE_EMIT_NOTIF_EVENT
,
104 STATE_EMIT_NOTIF_END_OF_PACKET
,
105 STATE_SKIP_PACKET_PADDING
,
108 struct trace_field_path_cache
{
110 * Indexes of the stream_id and stream_instance_id field in the packet
111 * header structure, -1 if unset.
114 int stream_instance_id
;
117 struct stream_class_field_path_cache
{
119 * Indexes of the v and id fields in the stream event header structure,
126 * index of the timestamp_end, packet_size and content_size fields in
127 * the stream packet context structure. Set to -1 if the fields were
135 struct field_cb_override
{
136 enum bt_ctf_btr_status (* func
)(void *value
,
137 struct bt_ctf_field_type
*type
, void *data
);
141 /* CTF notification iterator */
142 struct bt_ctf_notif_iter
{
147 * Current dynamic scope field pointer.
149 * This is set when a dynamic scope field is first created by
150 * btr_compound_begin_cb(). It points to one of the fields in
153 struct bt_ctf_field
**cur_dscope_field
;
155 /* Trace and classes (owned by this) */
157 struct bt_ctf_trace
*trace
;
158 struct bt_ctf_stream_class
*stream_class
;
159 struct bt_ctf_event_class
*event_class
;
162 /* Current packet (NULL if not created yet) */
163 struct bt_ctf_packet
*packet
;
165 /* Current stream (NULL if not set yet) */
166 struct bt_ctf_stream
*stream
;
169 * Current timestamp_end field (to consider before switching packets).
171 struct bt_ctf_field
*cur_timestamp_end
;
173 /* Database of current dynamic scopes (owned by this) */
175 struct bt_ctf_field
*trace_packet_header
;
176 struct bt_ctf_field
*stream_packet_context
;
177 struct bt_ctf_field
*stream_event_header
;
178 struct bt_ctf_field
*stream_event_context
;
179 struct bt_ctf_field
*event_context
;
180 struct bt_ctf_field
*event_payload
;
184 * Special field overrides.
186 * Overrides are used to implement the behaviours of special fields such
187 * as "timestamp_end" (which must be ignored until the end of the
188 * packet), "id" (event id) which can be present multiple times and must
189 * be updated multiple time.
191 * This should be used to implement the behaviour of integer fields
192 * mapped to clocks and other "tagged" fields (in CTF 2).
194 * bt_ctf_field_type to struct field_cb_override
196 GHashTable
*field_overrides
;
201 /* Current medium buffer data */
203 /* Last address provided by medium */
206 /* Buffer size provided by medium (bytes) */
209 /* Offset within whole packet of addr (bits) */
210 size_t packet_offset
;
212 /* Current position from addr (bits) */
216 /* Binary type reader */
217 struct bt_ctf_btr
*btr
;
219 /* Current medium data */
221 struct bt_ctf_notif_iter_medium_ops medops
;
222 size_t max_request_sz
;
226 /* Current packet size (bits) (-1 if unknown) */
227 int64_t cur_packet_size
;
229 /* Current content size (bits) (-1 if unknown) */
230 int64_t cur_content_size
;
232 /* bt_ctf_clock_class to uint64_t. */
233 GHashTable
*clock_states
;
236 * Cache of the trace-constant field paths (event header type)
237 * associated to the current trace.
239 struct trace_field_path_cache trace_field_path_cache
;
242 * Field path cache associated with the current stream class.
243 * Ownership of this structure belongs to the field_path_caches HT.
245 struct stream_class_field_path_cache
*cur_sc_field_path_cache
;
247 /* bt_ctf_stream_class to struct stream_class_field_path_cache. */
248 GHashTable
*sc_field_path_caches
;
252 const char *state_string(enum state state
)
257 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
258 return "STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN";
259 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
260 return "STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE";
261 case STATE_AFTER_TRACE_PACKET_HEADER
:
262 return "STATE_AFTER_TRACE_PACKET_HEADER";
263 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
264 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN";
265 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
266 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE";
267 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
268 return "STATE_AFTER_STREAM_PACKET_CONTEXT";
269 case STATE_EMIT_NOTIF_NEW_PACKET
:
270 return "STATE_EMIT_NOTIF_NEW_PACKET";
271 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
:
272 return "STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN";
273 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
:
274 return "STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE";
275 case STATE_AFTER_STREAM_EVENT_HEADER
:
276 return "STATE_AFTER_STREAM_EVENT_HEADER";
277 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
:
278 return "STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN";
279 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
:
280 return "STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE";
281 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN
:
282 return "STATE_DSCOPE_EVENT_CONTEXT_BEGIN";
283 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
:
284 return "STATE_DSCOPE_EVENT_CONTEXT_CONTINUE";
285 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
286 return "STATE_DSCOPE_EVENT_PAYLOAD_BEGIN";
287 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
288 return "STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE";
289 case STATE_EMIT_NOTIF_EVENT
:
290 return "STATE_EMIT_NOTIF_EVENT";
291 case STATE_EMIT_NOTIF_END_OF_PACKET
:
292 return "STATE_EMIT_NOTIF_END_OF_PACKET";
293 case STATE_SKIP_PACKET_PADDING
:
294 return "STATE_SKIP_PACKET_PADDING";
301 int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter
*notit
);
304 enum bt_ctf_btr_status
btr_timestamp_end_cb(void *value
,
305 struct bt_ctf_field_type
*type
, void *data
);
308 void stack_entry_free_func(gpointer data
)
310 struct stack_entry
*entry
= data
;
317 struct stack
*stack_new(struct bt_ctf_notif_iter
*notit
)
319 struct stack
*stack
= NULL
;
321 stack
= g_new0(struct stack
, 1);
323 BT_LOGE_STR("Failed to allocate one stack.");
327 stack
->entries
= g_ptr_array_new_with_free_func(stack_entry_free_func
);
328 if (!stack
->entries
) {
329 BT_LOGE_STR("Failed to allocate a GPtrArray.");
333 BT_LOGD("Created stack: notit-addr=%p, stack-addr=%p", notit
, stack
);
342 void stack_destroy(struct stack
*stack
)
345 BT_LOGD("Destroying stack: addr=%p", stack
);
346 g_ptr_array_free(stack
->entries
, TRUE
);
351 int stack_push(struct stack
*stack
, struct bt_ctf_field
*base
)
354 struct stack_entry
*entry
;
358 BT_LOGV("Pushing base field on stack: stack-addr=%p, "
359 "stack-size-before=%u, stack-size-after=%u",
360 stack
, stack
->entries
->len
, stack
->entries
->len
+ 1);
361 entry
= g_new0(struct stack_entry
, 1);
363 BT_LOGE_STR("Failed to allocate one stack entry.");
368 entry
->base
= bt_get(base
);
369 g_ptr_array_add(stack
->entries
, entry
);
376 unsigned int stack_size(struct stack
*stack
)
380 return stack
->entries
->len
;
384 void stack_pop(struct stack
*stack
)
387 assert(stack_size(stack
));
388 BT_LOGV("Popping from stack: "
389 "stack-addr=%p, stack-size-before=%u, stack-size-after=%u",
390 stack
, stack
->entries
->len
, stack
->entries
->len
- 1);
391 g_ptr_array_remove_index(stack
->entries
, stack
->entries
->len
- 1);
395 struct stack_entry
*stack_top(struct stack
*stack
)
398 assert(stack_size(stack
));
400 return g_ptr_array_index(stack
->entries
, stack
->entries
->len
- 1);
404 bool stack_empty(struct stack
*stack
)
406 return stack_size(stack
) == 0;
410 void stack_clear(struct stack
*stack
)
414 if (!stack_empty(stack
)) {
415 BT_LOGV("Clearing stack: stack-addr=%p, stack-size=%u",
416 stack
, stack
->entries
->len
);
417 g_ptr_array_remove_range(stack
->entries
, 0, stack_size(stack
));
420 assert(stack_empty(stack
));
424 enum bt_ctf_notif_iter_status
notif_iter_status_from_m_status(
425 enum bt_ctf_notif_iter_medium_status m_status
)
427 return (int) m_status
;
431 size_t buf_size_bits(struct bt_ctf_notif_iter
*notit
)
433 return notit
->buf
.sz
* 8;
437 size_t buf_available_bits(struct bt_ctf_notif_iter
*notit
)
439 return buf_size_bits(notit
) - notit
->buf
.at
;
443 size_t packet_at(struct bt_ctf_notif_iter
*notit
)
445 return notit
->buf
.packet_offset
+ notit
->buf
.at
;
449 void buf_consume_bits(struct bt_ctf_notif_iter
*notit
, size_t incr
)
451 BT_LOGV("Advancing cursor: notit-addr=%p, cur-before=%zu, cur-after=%zu",
452 notit
, notit
->buf
.at
, notit
->buf
.at
+ incr
);
453 notit
->buf
.at
+= incr
;
457 enum bt_ctf_notif_iter_status
request_medium_bytes(
458 struct bt_ctf_notif_iter
*notit
)
460 uint8_t *buffer_addr
= NULL
;
461 size_t buffer_sz
= 0;
462 enum bt_ctf_notif_iter_medium_status m_status
;
464 BT_LOGV("Calling user function (request bytes): notit-addr=%p, "
465 "request-size=%zu", notit
, notit
->medium
.max_request_sz
);
466 m_status
= notit
->medium
.medops
.request_bytes(
467 notit
->medium
.max_request_sz
, &buffer_addr
,
468 &buffer_sz
, notit
->medium
.data
);
469 BT_LOGV("User function returned: status=%s, buf-addr=%p, buf-size=%zu",
470 bt_ctf_notif_iter_medium_status_string(m_status
),
471 buffer_addr
, buffer_sz
);
472 if (m_status
== BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK
) {
473 assert(buffer_sz
!= 0);
475 /* New packet offset is old one + old size (in bits) */
476 notit
->buf
.packet_offset
+= buf_size_bits(notit
);
478 /* Restart at the beginning of the new medium buffer */
481 /* New medium buffer size */
482 notit
->buf
.sz
= buffer_sz
;
484 /* New medium buffer address */
485 notit
->buf
.addr
= buffer_addr
;
487 BT_LOGV("User function returned new bytes: "
488 "packet-offset=%zu, cur=%zu, size=%zu, addr=%p",
489 notit
->buf
.packet_offset
, notit
->buf
.at
,
490 notit
->buf
.sz
, notit
->buf
.addr
);
491 BT_LOGV_MEM(buffer_addr
, buffer_sz
, "Returned bytes at %p:",
493 } else if (m_status
< 0) {
494 BT_LOGW("User function failed: status=%s",
495 bt_ctf_notif_iter_medium_status_string(m_status
));
498 return notif_iter_status_from_m_status(m_status
);
502 enum bt_ctf_notif_iter_status
buf_ensure_available_bits(
503 struct bt_ctf_notif_iter
*notit
)
505 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
507 if (buf_available_bits(notit
) == 0) {
509 * This _cannot_ return BT_CTF_NOTIF_ITER_STATUS_OK
512 status
= request_medium_bytes(notit
);
519 enum bt_ctf_notif_iter_status
read_dscope_begin_state(
520 struct bt_ctf_notif_iter
*notit
,
521 struct bt_ctf_field_type
*dscope_field_type
,
522 enum state done_state
, enum state continue_state
,
523 struct bt_ctf_field
**dscope_field
)
525 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
526 enum bt_ctf_btr_status btr_status
;
527 size_t consumed_bits
;
529 status
= buf_ensure_available_bits(notit
);
530 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
532 BT_LOGW("Cannot ensure that buffer has at least one byte: "
533 "notif-addr=%p, status=%s",
534 notit
, bt_ctf_notif_iter_status_string(status
));
536 BT_LOGV("Cannot ensure that buffer has at least one byte: "
537 "notif-addr=%p, status=%s",
538 notit
, bt_ctf_notif_iter_status_string(status
));
544 bt_put(*dscope_field
);
545 notit
->cur_dscope_field
= dscope_field
;
546 BT_LOGV("Starting BTR: notit-addr=%p, btr-addr=%p, ft-addr=%p",
547 notit
, notit
->btr
, dscope_field_type
);
548 consumed_bits
= bt_ctf_btr_start(notit
->btr
, dscope_field_type
,
549 notit
->buf
.addr
, notit
->buf
.at
, packet_at(notit
),
550 notit
->buf
.sz
, &btr_status
);
551 BT_LOGV("BTR consumed bits: size=%zu", consumed_bits
);
553 switch (btr_status
) {
554 case BT_CTF_BTR_STATUS_OK
:
555 /* type was read completely */
556 BT_LOGV_STR("Field was completely decoded.");
557 notit
->state
= done_state
;
559 case BT_CTF_BTR_STATUS_EOF
:
560 BT_LOGV_STR("BTR needs more data to decode field completely.");
561 notit
->state
= continue_state
;
564 BT_LOGW("BTR failed to start: notit-addr=%p, btr-addr=%p, "
565 "status=%s", notit
, notit
->btr
,
566 bt_ctf_btr_status_string(btr_status
));
567 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
571 /* Consume bits now since we know we're not in an error state */
572 buf_consume_bits(notit
, consumed_bits
);
579 enum bt_ctf_notif_iter_status
read_dscope_continue_state(
580 struct bt_ctf_notif_iter
*notit
, enum state done_state
)
582 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
583 enum bt_ctf_btr_status btr_status
;
584 size_t consumed_bits
;
586 status
= buf_ensure_available_bits(notit
);
587 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
589 BT_LOGW("Cannot ensure that buffer has at least one byte: "
590 "notif-addr=%p, status=%s",
591 notit
, bt_ctf_notif_iter_status_string(status
));
593 BT_LOGV("Cannot ensure that buffer has at least one byte: "
594 "notif-addr=%p, status=%s",
595 notit
, bt_ctf_notif_iter_status_string(status
));
601 BT_LOGV("Continuing BTR: notit-addr=%p, btr-addr=%p",
603 consumed_bits
= bt_ctf_btr_continue(notit
->btr
, notit
->buf
.addr
,
604 notit
->buf
.sz
, &btr_status
);
605 BT_LOGV("BTR consumed bits: size=%zu", consumed_bits
);
607 switch (btr_status
) {
608 case BT_CTF_BTR_STATUS_OK
:
609 /* Type was read completely. */
610 BT_LOGV_STR("Field was completely decoded.");
611 notit
->state
= done_state
;
613 case BT_CTF_BTR_STATUS_EOF
:
614 /* Stay in this continue state. */
615 BT_LOGV_STR("BTR needs more data to decode field completely.");
618 BT_LOGW("BTR failed to continue: notit-addr=%p, btr-addr=%p, "
619 "status=%s", notit
, notit
->btr
,
620 bt_ctf_btr_status_string(btr_status
));
621 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
625 /* Consume bits now since we know we're not in an error state. */
626 buf_consume_bits(notit
, consumed_bits
);
632 void put_event_dscopes(struct bt_ctf_notif_iter
*notit
)
634 BT_LOGV_STR("Putting event header field.");
635 BT_PUT(notit
->dscopes
.stream_event_header
);
636 BT_LOGV_STR("Putting stream event context field.");
637 BT_PUT(notit
->dscopes
.stream_event_context
);
638 BT_LOGV_STR("Putting event context field.");
639 BT_PUT(notit
->dscopes
.event_context
);
640 BT_LOGV_STR("Putting event payload field.");
641 BT_PUT(notit
->dscopes
.event_payload
);
645 void put_all_dscopes(struct bt_ctf_notif_iter
*notit
)
647 BT_LOGV_STR("Putting packet header field.");
648 BT_PUT(notit
->dscopes
.trace_packet_header
);
649 BT_LOGV_STR("Putting packet context field.");
650 BT_PUT(notit
->dscopes
.stream_packet_context
);
651 put_event_dscopes(notit
);
655 enum bt_ctf_notif_iter_status
read_packet_header_begin_state(
656 struct bt_ctf_notif_iter
*notit
)
658 struct bt_ctf_field_type
*packet_header_type
= NULL
;
659 enum bt_ctf_notif_iter_status ret
= BT_CTF_NOTIF_ITER_STATUS_OK
;
661 if (bt_ctf_notif_iter_switch_packet(notit
)) {
662 BT_LOGW("Cannot switch packet: notit-addr=%p", notit
);
663 ret
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
667 /* Packet header type is common to the whole trace. */
668 packet_header_type
= bt_ctf_trace_get_packet_header_type(
670 if (!packet_header_type
) {
671 notit
->state
= STATE_AFTER_TRACE_PACKET_HEADER
;
675 BT_LOGV("Decoding packet header field:"
676 "notit-addr=%p, trace-addr=%p, trace-name=\"%s\", ft-addr=%p",
677 notit
, notit
->meta
.trace
,
678 bt_ctf_trace_get_name(notit
->meta
.trace
), packet_header_type
);
679 ret
= read_dscope_begin_state(notit
, packet_header_type
,
680 STATE_AFTER_TRACE_PACKET_HEADER
,
681 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
682 ¬it
->dscopes
.trace_packet_header
);
684 BT_LOGW("Cannot decode packet header field: "
685 "notit-addr=%p, trace-addr=%p, "
686 "trace-name=\"%s\", ft-addr=%p",
687 notit
, notit
->meta
.trace
,
688 bt_ctf_trace_get_name(notit
->meta
.trace
),
692 BT_PUT(packet_header_type
);
697 enum bt_ctf_notif_iter_status
read_packet_header_continue_state(
698 struct bt_ctf_notif_iter
*notit
)
700 return read_dscope_continue_state(notit
,
701 STATE_AFTER_TRACE_PACKET_HEADER
);
705 bool is_struct_type(struct bt_ctf_field_type
*field_type
)
707 return bt_ctf_field_type_get_type_id(field_type
) ==
708 BT_CTF_FIELD_TYPE_ID_STRUCT
;
712 bool is_variant_type(struct bt_ctf_field_type
*field_type
)
714 return bt_ctf_field_type_get_type_id(field_type
) ==
715 BT_CTF_FIELD_TYPE_ID_VARIANT
;
719 struct stream_class_field_path_cache
*
720 create_stream_class_field_path_cache_entry(
721 struct bt_ctf_notif_iter
*notit
,
722 struct bt_ctf_stream_class
*stream_class
)
726 int timestamp_end
= -1;
727 int packet_size
= -1;
728 int content_size
= -1;
729 struct stream_class_field_path_cache
*cache_entry
= g_new0(
730 struct stream_class_field_path_cache
, 1);
731 struct bt_ctf_field_type
*event_header
= NULL
, *packet_context
= NULL
;
734 BT_LOGE_STR("Failed to allocate one stream class field path cache.");
738 event_header
= bt_ctf_stream_class_get_event_header_type(stream_class
);
739 if (event_header
&& bt_ctf_field_type_is_structure(event_header
)) {
742 count
= bt_ctf_field_type_structure_get_field_count(
746 for (i
= 0; i
< count
; i
++) {
750 ret
= bt_ctf_field_type_structure_get_field(
751 event_header
, &name
, NULL
, i
);
753 BT_LOGE("Cannot get event header structure field type's field: "
754 "notit-addr=%p, stream-class-addr=%p, "
755 "stream-class-name=\"%s\", "
756 "stream-class-id=%" PRId64
", "
757 "ft-addr=%p, index=%d",
759 bt_ctf_stream_class_get_name(stream_class
),
760 bt_ctf_stream_class_get_id(stream_class
),
765 if (v
!= -1 && id
!= -1) {
769 if (v
== -1 && strcmp(name
, "v") == 0) {
771 } else if (id
== -1 && !strcmp(name
, "id")) {
777 packet_context
= bt_ctf_stream_class_get_packet_context_type(
779 if (packet_context
&& bt_ctf_field_type_is_structure(packet_context
)) {
782 count
= bt_ctf_field_type_structure_get_field_count(
786 for (i
= 0; i
< count
; i
++) {
789 struct bt_ctf_field_type
*field_type
;
791 if (timestamp_end
!= -1 && packet_size
!= -1 &&
792 content_size
!= -1) {
796 ret
= bt_ctf_field_type_structure_get_field(
797 packet_context
, &name
, &field_type
, i
);
799 BT_LOGE("Cannot get packet context structure field type's field: "
800 "notit-addr=%p, stream-class-addr=%p, "
801 "stream-class-name=\"%s\", "
802 "stream-class-id=%" PRId64
", "
803 "ft-addr=%p, index=%d",
805 bt_ctf_stream_class_get_name(stream_class
),
806 bt_ctf_stream_class_get_id(stream_class
),
811 if (timestamp_end
== -1 &&
812 strcmp(name
, "timestamp_end") == 0) {
813 struct field_cb_override
*override
= g_new0(
814 struct field_cb_override
, 1);
821 override
->func
= btr_timestamp_end_cb
;
822 override
->data
= notit
;
823 g_hash_table_insert(notit
->field_overrides
,
824 bt_get(field_type
), override
);
826 } else if (packet_size
== -1 &&
827 !strcmp(name
, "packet_size")) {
829 } else if (content_size
== -1 &&
830 !strcmp(name
, "content_size")) {
838 cache_entry
->id
= id
;
839 cache_entry
->timestamp_end
= timestamp_end
;
840 cache_entry
->packet_size
= packet_size
;
841 cache_entry
->content_size
= content_size
;
843 BT_PUT(event_header
);
844 BT_PUT(packet_context
);
853 struct stream_class_field_path_cache
*get_stream_class_field_path_cache(
854 struct bt_ctf_notif_iter
*notit
,
855 struct bt_ctf_stream_class
*stream_class
)
857 bool cache_entry_found
;
858 struct stream_class_field_path_cache
*cache_entry
;
860 cache_entry_found
= g_hash_table_lookup_extended(
861 notit
->sc_field_path_caches
,
862 stream_class
, NULL
, (gpointer
) &cache_entry
);
863 if (unlikely(!cache_entry_found
)) {
864 cache_entry
= create_stream_class_field_path_cache_entry(notit
,
866 g_hash_table_insert(notit
->sc_field_path_caches
,
867 bt_get(stream_class
), (gpointer
) cache_entry
);
874 enum bt_ctf_notif_iter_status
set_current_stream_class(
875 struct bt_ctf_notif_iter
*notit
)
877 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
878 struct bt_ctf_field_type
*packet_header_type
= NULL
;
879 struct bt_ctf_field_type
*stream_id_field_type
= NULL
;
880 struct bt_ctf_stream_class
*new_stream_class
= NULL
;
883 /* Clear the current stream class field path cache. */
884 notit
->cur_sc_field_path_cache
= NULL
;
886 /* Is there any "stream_id" field in the packet header? */
887 packet_header_type
= bt_ctf_trace_get_packet_header_type(
889 if (!packet_header_type
) {
891 * No packet header, therefore no `stream_id` field,
892 * therefore only one stream class.
894 goto single_stream_class
;
897 assert(is_struct_type(packet_header_type
));
900 stream_id_field_type
=
901 bt_ctf_field_type_structure_get_field_type_by_name(
902 packet_header_type
, "stream_id");
903 if (stream_id_field_type
) {
904 /* Find appropriate stream class using current stream ID */
906 struct bt_ctf_field
*stream_id_field
= NULL
;
908 assert(notit
->dscopes
.trace_packet_header
);
911 stream_id_field
= bt_ctf_field_structure_get_field(
912 notit
->dscopes
.trace_packet_header
, "stream_id");
913 assert(stream_id_field
);
914 ret
= bt_ctf_field_unsigned_integer_get_value(
915 stream_id_field
, &stream_id
);
917 BT_PUT(stream_id_field
);
920 /* Only one stream: pick the first stream class */
921 assert(bt_ctf_trace_get_stream_class_count(
922 notit
->meta
.trace
) == 1);
926 BT_LOGV("Found stream class ID to use: notit-addr=%p, "
927 "stream-class-id=%" PRIu64
", "
928 "trace-addr=%p, trace-name=\"%s\"",
929 notit
, stream_id
, notit
->meta
.trace
,
930 bt_ctf_trace_get_name(notit
->meta
.trace
));
932 new_stream_class
= bt_ctf_trace_get_stream_class_by_id(
933 notit
->meta
.trace
, stream_id
);
934 if (!new_stream_class
) {
935 BT_LOGW("No stream class with ID of stream class ID to use in trace: "
936 "notit-addr=%p, stream-class-id=%" PRIu64
", "
937 "trace-addr=%p, trace-name=\"%s\"",
938 notit
, stream_id
, notit
->meta
.trace
,
939 bt_ctf_trace_get_name(notit
->meta
.trace
));
940 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
944 if (notit
->meta
.stream_class
) {
945 if (new_stream_class
!= notit
->meta
.stream_class
) {
946 BT_LOGW("Two packets refer to two different stream classes within the same packet sequence: "
947 "notit-addr=%p, prev-stream-class-addr=%p, "
948 "prev-stream-class-name=\"%s\", "
949 "prev-stream-class-id=%" PRId64
", "
950 "next-stream-class-addr=%p, "
951 "next-stream-class-name=\"%s\", "
952 "next-stream-class-id=%" PRId64
", "
953 "trace-addr=%p, trace-name=\"%s\"",
954 notit
, notit
->meta
.stream_class
,
955 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
956 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
958 bt_ctf_stream_class_get_name(new_stream_class
),
959 bt_ctf_stream_class_get_id(new_stream_class
),
961 bt_ctf_trace_get_name(notit
->meta
.trace
));
962 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
966 BT_MOVE(notit
->meta
.stream_class
, new_stream_class
);
969 BT_LOGV("Set current stream class: "
970 "notit-addr=%p, stream-class-addr=%p, "
971 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
972 notit
, notit
->meta
.stream_class
,
973 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
974 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
977 * Retrieve (or lazily create) the current stream class field path
980 notit
->cur_sc_field_path_cache
= get_stream_class_field_path_cache(
981 notit
, notit
->meta
.stream_class
);
982 if (!notit
->cur_sc_field_path_cache
) {
983 BT_LOGW("Cannot retrieve stream class field path from cache: "
984 "notit-addr=%p, stream-class-addr=%p, "
985 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
986 notit
, notit
->meta
.stream_class
,
987 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
988 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
989 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
993 BT_PUT(packet_header_type
);
994 BT_PUT(stream_id_field_type
);
995 bt_put(new_stream_class
);
1000 enum bt_ctf_notif_iter_status
after_packet_header_state(
1001 struct bt_ctf_notif_iter
*notit
)
1003 enum bt_ctf_notif_iter_status status
;
1005 status
= set_current_stream_class(notit
);
1006 if (status
== BT_CTF_NOTIF_ITER_STATUS_OK
) {
1007 notit
->state
= STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
;
1014 enum bt_ctf_notif_iter_status
read_packet_context_begin_state(
1015 struct bt_ctf_notif_iter
*notit
)
1017 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1018 struct bt_ctf_field_type
*packet_context_type
;
1020 assert(notit
->meta
.stream_class
);
1021 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(
1022 notit
->meta
.stream_class
);
1023 if (!packet_context_type
) {
1024 BT_LOGV("No packet packet context field type in stream class: continuing: "
1025 "notit-addr=%p, stream-class-addr=%p, "
1026 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
1027 notit
, notit
->meta
.stream_class
,
1028 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1029 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
1030 notit
->state
= STATE_AFTER_STREAM_PACKET_CONTEXT
;
1034 BT_LOGV("Decoding packet context field: "
1035 "notit-addr=%p, stream-class-addr=%p, "
1036 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1038 notit
, notit
->meta
.stream_class
,
1039 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1040 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1041 packet_context_type
);
1042 status
= read_dscope_begin_state(notit
, packet_context_type
,
1043 STATE_AFTER_STREAM_PACKET_CONTEXT
,
1044 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
1045 ¬it
->dscopes
.stream_packet_context
);
1047 BT_LOGW("Cannot decode packet context field: "
1048 "notit-addr=%p, stream-class-addr=%p, "
1049 "stream-class-name=\"%s\", "
1050 "stream-class-id=%" PRId64
", ft-addr=%p",
1051 notit
, notit
->meta
.stream_class
,
1052 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1053 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1054 packet_context_type
);
1058 BT_PUT(packet_context_type
);
1063 enum bt_ctf_notif_iter_status
read_packet_context_continue_state(
1064 struct bt_ctf_notif_iter
*notit
)
1066 return read_dscope_continue_state(notit
,
1067 STATE_AFTER_STREAM_PACKET_CONTEXT
);
1071 enum bt_ctf_notif_iter_status
set_current_packet_content_sizes(
1072 struct bt_ctf_notif_iter
*notit
)
1074 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1075 struct bt_ctf_field
*packet_size_field
= NULL
;
1076 struct bt_ctf_field
*content_size_field
= NULL
;
1077 uint64_t content_size
= -1, packet_size
= -1;
1079 if (!notit
->dscopes
.stream_packet_context
) {
1083 packet_size_field
= bt_ctf_field_structure_get_field(
1084 notit
->dscopes
.stream_packet_context
, "packet_size");
1085 content_size_field
= bt_ctf_field_structure_get_field(
1086 notit
->dscopes
.stream_packet_context
, "content_size");
1087 if (packet_size_field
) {
1088 int ret
= bt_ctf_field_unsigned_integer_get_value(
1089 packet_size_field
, &packet_size
);
1092 if (packet_size
== 0) {
1093 BT_LOGW("Invalid packet size: packet context field indicates packet size is zero: "
1094 "notit-addr=%p, packet-context-field-addr=%p",
1095 notit
, notit
->dscopes
.stream_packet_context
);
1096 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1098 } else if ((packet_size
% 8) != 0) {
1099 BT_LOGW("Invalid packet size: packet context field indicates packet size is not a multiple of 8: "
1100 "notit-addr=%p, packet-context-field-addr=%p, "
1101 "packet-size=%" PRIu64
,
1102 notit
, notit
->dscopes
.stream_packet_context
,
1104 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1109 if (content_size_field
) {
1110 int ret
= bt_ctf_field_unsigned_integer_get_value(
1111 content_size_field
, &content_size
);
1115 content_size
= packet_size
;
1118 if (content_size
> packet_size
) {
1119 BT_LOGW("Invalid packet or content size: packet context field indicates content size is greater than packet size: "
1120 "notit-addr=%p, packet-context-field-addr=%p, "
1121 "packet-size=%" PRIu64
", content-size=%" PRIu64
,
1122 notit
, notit
->dscopes
.stream_packet_context
,
1123 packet_size
, content_size
);
1124 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1128 notit
->cur_packet_size
= packet_size
;
1129 notit
->cur_content_size
= content_size
;
1130 BT_LOGV("Set current packet and content sizes: "
1131 "notit-addr=%p, packet-size=%" PRIu64
", content-size=%" PRIu64
,
1132 notit
, packet_size
, content_size
);
1134 BT_PUT(packet_size_field
);
1135 BT_PUT(content_size_field
);
1140 enum bt_ctf_notif_iter_status
after_packet_context_state(
1141 struct bt_ctf_notif_iter
*notit
)
1143 enum bt_ctf_notif_iter_status status
;
1145 status
= set_current_packet_content_sizes(notit
);
1146 if (status
== BT_CTF_NOTIF_ITER_STATUS_OK
) {
1147 notit
->state
= STATE_EMIT_NOTIF_NEW_PACKET
;
1154 enum bt_ctf_notif_iter_status
read_event_header_begin_state(
1155 struct bt_ctf_notif_iter
*notit
)
1157 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1158 struct bt_ctf_field_type
*event_header_type
= NULL
;
1160 /* Check if we have some content left */
1161 if (notit
->cur_content_size
>= 0) {
1162 if (packet_at(notit
) == notit
->cur_content_size
) {
1163 /* No more events! */
1164 BT_LOGV("Reached end of packet: notit-addr=%p, "
1165 "cur=%zu", notit
, packet_at(notit
));
1166 notit
->state
= STATE_EMIT_NOTIF_END_OF_PACKET
;
1168 } else if (packet_at(notit
) > notit
->cur_content_size
) {
1169 /* That's not supposed to happen */
1170 BT_LOGV("Before decoding event header field: cursor is passed the packet's content: "
1171 "notit-addr=%p, content-size=%zu, "
1172 "cur=%zu", notit
, notit
->cur_content_size
,
1174 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1179 event_header_type
= bt_ctf_stream_class_get_event_header_type(
1180 notit
->meta
.stream_class
);
1181 if (!event_header_type
) {
1182 notit
->state
= STATE_AFTER_STREAM_EVENT_HEADER
;
1186 put_event_dscopes(notit
);
1187 BT_LOGV("Decoding event header field: "
1188 "notit-addr=%p, stream-class-addr=%p, "
1189 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1191 notit
, notit
->meta
.stream_class
,
1192 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1193 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1195 status
= read_dscope_begin_state(notit
, event_header_type
,
1196 STATE_AFTER_STREAM_EVENT_HEADER
,
1197 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
,
1198 ¬it
->dscopes
.stream_event_header
);
1200 BT_LOGW("Cannot decode event header field: "
1201 "notit-addr=%p, stream-class-addr=%p, "
1202 "stream-class-name=\"%s\", "
1203 "stream-class-id=%" PRId64
", ft-addr=%p",
1204 notit
, notit
->meta
.stream_class
,
1205 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1206 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1210 BT_PUT(event_header_type
);
1216 enum bt_ctf_notif_iter_status
read_event_header_continue_state(
1217 struct bt_ctf_notif_iter
*notit
)
1219 return read_dscope_continue_state(notit
,
1220 STATE_AFTER_STREAM_EVENT_HEADER
);
1224 enum bt_ctf_notif_iter_status
set_current_event_class(struct bt_ctf_notif_iter
*notit
)
1227 * The assert() calls in this function are okay because it is
1228 * assumed here that all the metadata objects have been
1229 * validated for CTF correctness before decoding actual streams.
1232 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1233 struct bt_ctf_field_type
*event_header_type
;
1234 struct bt_ctf_field_type
*id_field_type
= NULL
;
1235 struct bt_ctf_field_type
*v_field_type
= NULL
;
1236 uint64_t event_id
= -1ULL;
1239 event_header_type
= bt_ctf_stream_class_get_event_header_type(
1240 notit
->meta
.stream_class
);
1241 if (!event_header_type
) {
1243 * No event header, therefore no event class ID field,
1244 * therefore only one event class.
1246 goto single_event_class
;
1249 /* Is there any "id"/"v" field in the event header? */
1250 assert(is_struct_type(event_header_type
));
1251 id_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1252 event_header_type
, "id");
1253 v_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1254 event_header_type
, "v");
1255 assert(notit
->dscopes
.stream_event_header
);
1259 * | | |_ _|_ _| __ __ _
1260 * | | | | | || '_ \ / _` |
1261 * | |___| | | || | | | (_| | S P E C I A L
1262 * |_____|_| |_||_| |_|\__, | C A S E â„¢
1265 struct bt_ctf_field
*v_field
= NULL
;
1266 struct bt_ctf_field
*v_struct_field
= NULL
;
1267 struct bt_ctf_field
*v_struct_id_field
= NULL
;
1269 // TODO: optimalize!
1270 v_field
= bt_ctf_field_structure_get_field(
1271 notit
->dscopes
.stream_event_header
, "v");
1275 bt_ctf_field_variant_get_current_field(v_field
);
1276 if (!v_struct_field
) {
1277 goto end_v_field_type
;
1280 // TODO: optimalize!
1282 bt_ctf_field_structure_get_field(v_struct_field
, "id");
1283 if (!v_struct_id_field
) {
1284 goto end_v_field_type
;
1287 if (bt_ctf_field_is_integer(v_struct_id_field
)) {
1288 ret
= bt_ctf_field_unsigned_integer_get_value(
1289 v_struct_id_field
, &event_id
);
1291 BT_LOGV("Cannot get value of unsigned integer field (`id`): continuing: "
1292 "notit=%p, field-addr=%p",
1293 notit
, v_struct_id_field
);
1300 BT_PUT(v_struct_field
);
1301 BT_PUT(v_struct_id_field
);
1304 if (id_field_type
&& event_id
== -1ULL) {
1305 /* Check "id" field */
1306 struct bt_ctf_field
*id_field
= NULL
;
1309 // TODO: optimalize!
1310 id_field
= bt_ctf_field_structure_get_field(
1311 notit
->dscopes
.stream_event_header
, "id");
1313 goto check_event_id
;
1316 if (bt_ctf_field_is_integer(id_field
)) {
1317 ret
= bt_ctf_field_unsigned_integer_get_value(
1318 id_field
, &event_id
);
1319 } else if (bt_ctf_field_is_enumeration(id_field
)) {
1320 struct bt_ctf_field
*container
;
1322 container
= bt_ctf_field_enumeration_get_container(
1325 ret
= bt_ctf_field_unsigned_integer_get_value(
1326 container
, &event_id
);
1335 if (event_id
== -1ULL) {
1337 /* Event ID not found: single event? */
1338 assert(bt_ctf_stream_class_get_event_class_count(
1339 notit
->meta
.stream_class
) == 1);
1343 BT_LOGV("Found event class ID to use: notit-addr=%p, "
1344 "stream-class-addr=%p, stream-class-name=\"%s\", "
1345 "stream-class-id=%" PRId64
", "
1346 "event-class-id=%" PRIu64
,
1347 notit
, notit
->meta
.stream_class
,
1348 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1349 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1351 BT_PUT(notit
->meta
.event_class
);
1352 notit
->meta
.event_class
= bt_ctf_stream_class_get_event_class_by_id(
1353 notit
->meta
.stream_class
, event_id
);
1354 if (!notit
->meta
.event_class
) {
1355 BT_LOGW("No event class with ID of event class ID to use in stream class: "
1356 "notit-addr=%p, stream-class-addr=%p, "
1357 "stream-class-name=\"%s\", "
1358 "stream-class-id=%" PRId64
", "
1359 "event-class-id=%" PRIu64
,
1360 notit
, notit
->meta
.stream_class
,
1361 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1362 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1364 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1368 BT_LOGV("Set current event class: "
1369 "notit-addr=%p, event-class-addr=%p, "
1370 "event-class-name=\"%s\", event-class-id=%" PRId64
,
1371 notit
, notit
->meta
.event_class
,
1372 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
1373 bt_ctf_event_class_get_id(notit
->meta
.event_class
));
1376 BT_PUT(event_header_type
);
1377 BT_PUT(id_field_type
);
1378 BT_PUT(v_field_type
);
1384 enum bt_ctf_notif_iter_status
after_event_header_state(
1385 struct bt_ctf_notif_iter
*notit
)
1387 enum bt_ctf_notif_iter_status status
;
1389 status
= set_current_event_class(notit
);
1390 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
1394 notit
->state
= STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
;
1401 enum bt_ctf_notif_iter_status
read_stream_event_context_begin_state(
1402 struct bt_ctf_notif_iter
*notit
)
1404 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1405 struct bt_ctf_field_type
*stream_event_context_type
;
1407 stream_event_context_type
= bt_ctf_stream_class_get_event_context_type(
1408 notit
->meta
.stream_class
);
1409 if (!stream_event_context_type
) {
1410 notit
->state
= STATE_DSCOPE_EVENT_CONTEXT_BEGIN
;
1414 BT_LOGV("Decoding stream event context field: "
1415 "notit-addr=%p, stream-class-addr=%p, "
1416 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1418 notit
, notit
->meta
.stream_class
,
1419 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1420 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1421 stream_event_context_type
);
1422 status
= read_dscope_begin_state(notit
, stream_event_context_type
,
1423 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
,
1424 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
,
1425 ¬it
->dscopes
.stream_event_context
);
1427 BT_LOGW("Cannot decode stream event context field: "
1428 "notit-addr=%p, stream-class-addr=%p, "
1429 "stream-class-name=\"%s\", "
1430 "stream-class-id=%" PRId64
", ft-addr=%p",
1431 notit
, notit
->meta
.stream_class
,
1432 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1433 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1434 stream_event_context_type
);
1438 BT_PUT(stream_event_context_type
);
1444 enum bt_ctf_notif_iter_status
read_stream_event_context_continue_state(
1445 struct bt_ctf_notif_iter
*notit
)
1447 return read_dscope_continue_state(notit
,
1448 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
);
1452 enum bt_ctf_notif_iter_status
read_event_context_begin_state(
1453 struct bt_ctf_notif_iter
*notit
)
1455 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1456 struct bt_ctf_field_type
*event_context_type
;
1458 event_context_type
= bt_ctf_event_class_get_context_type(
1459 notit
->meta
.event_class
);
1460 if (!event_context_type
) {
1461 notit
->state
= STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
;
1465 BT_LOGV("Decoding event context field: "
1466 "notit-addr=%p, event-class-addr=%p, "
1467 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1469 notit
, notit
->meta
.event_class
,
1470 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
1471 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
1472 event_context_type
);
1473 status
= read_dscope_begin_state(notit
, event_context_type
,
1474 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
1475 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
,
1476 ¬it
->dscopes
.event_context
);
1478 BT_LOGW("Cannot decode event context field: "
1479 "notit-addr=%p, event-class-addr=%p, "
1480 "event-class-name=\"%s\", "
1481 "event-class-id=%" PRId64
", ft-addr=%p",
1482 notit
, notit
->meta
.event_class
,
1483 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
1484 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
1485 event_context_type
);
1489 BT_PUT(event_context_type
);
1495 enum bt_ctf_notif_iter_status
read_event_context_continue_state(
1496 struct bt_ctf_notif_iter
*notit
)
1498 return read_dscope_continue_state(notit
,
1499 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
);
1503 enum bt_ctf_notif_iter_status
read_event_payload_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
*event_payload_type
;
1509 event_payload_type
= bt_ctf_event_class_get_payload_type(
1510 notit
->meta
.event_class
);
1511 if (!event_payload_type
) {
1512 notit
->state
= STATE_EMIT_NOTIF_EVENT
;
1516 BT_LOGV("Decoding event payload field: "
1517 "notit-addr=%p, event-class-addr=%p, "
1518 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1520 notit
, notit
->meta
.event_class
,
1521 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
1522 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
1523 event_payload_type
);
1524 status
= read_dscope_begin_state(notit
, event_payload_type
,
1525 STATE_EMIT_NOTIF_EVENT
,
1526 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
1527 ¬it
->dscopes
.event_payload
);
1529 BT_LOGW("Cannot decode event payload field: "
1530 "notit-addr=%p, event-class-addr=%p, "
1531 "event-class-name=\"%s\", "
1532 "event-class-id=%" PRId64
", ft-addr=%p",
1533 notit
, notit
->meta
.event_class
,
1534 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
1535 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
1536 event_payload_type
);
1540 BT_PUT(event_payload_type
);
1546 enum bt_ctf_notif_iter_status
read_event_payload_continue_state(
1547 struct bt_ctf_notif_iter
*notit
)
1549 return read_dscope_continue_state(notit
, STATE_EMIT_NOTIF_EVENT
);
1553 enum bt_ctf_notif_iter_status
skip_packet_padding_state(
1554 struct bt_ctf_notif_iter
*notit
)
1556 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1557 size_t bits_to_skip
;
1559 assert(notit
->cur_packet_size
> 0);
1560 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1561 if (bits_to_skip
== 0) {
1562 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1565 size_t bits_to_consume
;
1567 BT_LOGV("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu",
1568 bits_to_skip
, notit
, bits_to_skip
);
1569 status
= buf_ensure_available_bits(notit
);
1570 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
1574 bits_to_consume
= MIN(buf_available_bits(notit
), bits_to_skip
);
1575 BT_LOGV("Skipping %zu bits of padding: notit-addr=%p, size=%zu",
1576 bits_to_consume
, notit
, bits_to_consume
);
1577 buf_consume_bits(notit
, bits_to_consume
);
1578 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1579 if (bits_to_skip
== 0) {
1580 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1590 enum bt_ctf_notif_iter_status
handle_state(struct bt_ctf_notif_iter
*notit
)
1592 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1593 const enum state state
= notit
->state
;
1595 BT_LOGV("Handling state: notit-addr=%p, state=%s",
1596 notit
, state_string(state
));
1598 // TODO: optimalize!
1601 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1603 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
1604 status
= read_packet_header_begin_state(notit
);
1606 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
1607 status
= read_packet_header_continue_state(notit
);
1609 case STATE_AFTER_TRACE_PACKET_HEADER
:
1610 status
= after_packet_header_state(notit
);
1612 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
1613 status
= read_packet_context_begin_state(notit
);
1615 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
1616 status
= read_packet_context_continue_state(notit
);
1618 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
1619 status
= after_packet_context_state(notit
);
1621 case STATE_EMIT_NOTIF_NEW_PACKET
:
1622 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1624 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
:
1625 status
= read_event_header_begin_state(notit
);
1627 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
:
1628 status
= read_event_header_continue_state(notit
);
1630 case STATE_AFTER_STREAM_EVENT_HEADER
:
1631 status
= after_event_header_state(notit
);
1633 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
:
1634 status
= read_stream_event_context_begin_state(notit
);
1636 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
:
1637 status
= read_stream_event_context_continue_state(notit
);
1639 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN
:
1640 status
= read_event_context_begin_state(notit
);
1642 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
:
1643 status
= read_event_context_continue_state(notit
);
1645 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
1646 status
= read_event_payload_begin_state(notit
);
1648 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
1649 status
= read_event_payload_continue_state(notit
);
1651 case STATE_EMIT_NOTIF_EVENT
:
1652 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1654 case STATE_SKIP_PACKET_PADDING
:
1655 status
= skip_packet_padding_state(notit
);
1657 case STATE_EMIT_NOTIF_END_OF_PACKET
:
1658 notit
->state
= STATE_SKIP_PACKET_PADDING
;
1661 BT_LOGD("Unknown CTF plugin notification iterator state: "
1662 "notit-addr=%p, state=%d", notit
, notit
->state
);
1666 BT_LOGV("Handled state: notit-addr=%p, status=%s, "
1667 "prev-state=%s, cur-state=%s",
1668 notit
, bt_ctf_notif_iter_status_string(status
),
1669 state_string(state
), state_string(notit
->state
));
1674 * Resets the internal state of a CTF notification iterator.
1677 void bt_ctf_notif_iter_reset(struct bt_ctf_notif_iter
*notit
)
1680 BT_LOGD("Resetting notification iterator: addr=%p", notit
);
1681 stack_clear(notit
->stack
);
1682 BT_PUT(notit
->meta
.stream_class
);
1683 BT_PUT(notit
->meta
.event_class
);
1684 BT_PUT(notit
->packet
);
1685 BT_PUT(notit
->stream
);
1686 put_all_dscopes(notit
);
1687 notit
->buf
.addr
= NULL
;
1690 notit
->buf
.packet_offset
= 0;
1691 notit
->state
= STATE_INIT
;
1692 notit
->cur_content_size
= -1;
1693 notit
->cur_packet_size
= -1;
1697 int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter
*notit
)
1702 * We don't put the stream class here because we need to make
1703 * sure that all the packets processed by the same notification
1704 * iterator refer to the same stream class (the first one).
1707 BT_LOGV("Switching packet: notit-addr=%p, cur=%zu",
1708 notit
, notit
->buf
.at
);
1709 stack_clear(notit
->stack
);
1710 BT_PUT(notit
->meta
.event_class
);
1711 BT_PUT(notit
->packet
);
1712 BT_PUT(notit
->cur_timestamp_end
);
1713 put_all_dscopes(notit
);
1716 * Adjust current buffer so that addr points to the beginning of the new
1719 if (notit
->buf
.addr
) {
1720 size_t consumed_bytes
= (size_t) (notit
->buf
.at
/ CHAR_BIT
);
1722 /* Packets are assumed to start on a byte frontier. */
1723 if (notit
->buf
.at
% CHAR_BIT
) {
1724 BT_LOGW("Cannot switch packet: current position is not a multiple of 8: "
1725 "notit-addr=%p, cur=%zu", notit
, notit
->buf
.at
);
1730 notit
->buf
.addr
+= consumed_bytes
;
1731 notit
->buf
.sz
-= consumed_bytes
;
1733 notit
->buf
.packet_offset
= 0;
1734 BT_LOGV("Adjusted buffer: addr=%p, size=%zu",
1735 notit
->buf
.addr
, notit
->buf
.sz
);
1738 notit
->cur_content_size
= -1;
1739 notit
->cur_packet_size
= -1;
1740 notit
->cur_sc_field_path_cache
= NULL
;
1747 struct bt_ctf_field
*get_next_field(struct bt_ctf_notif_iter
*notit
)
1749 struct bt_ctf_field
*next_field
= NULL
;
1750 struct bt_ctf_field
*base_field
;
1751 struct bt_ctf_field_type
*base_type
;
1754 assert(!stack_empty(notit
->stack
));
1755 index
= stack_top(notit
->stack
)->index
;
1756 base_field
= stack_top(notit
->stack
)->base
;
1758 base_type
= bt_ctf_field_get_type(base_field
);
1761 switch (bt_ctf_field_type_get_type_id(base_type
)) {
1762 case BT_CTF_FIELD_TYPE_ID_STRUCT
:
1763 next_field
= bt_ctf_field_structure_get_field_by_index(
1766 case BT_CTF_FIELD_TYPE_ID_ARRAY
:
1767 next_field
= bt_ctf_field_array_get_field(base_field
, index
);
1769 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
1770 next_field
= bt_ctf_field_sequence_get_field(base_field
, index
);
1772 case BT_CTF_FIELD_TYPE_ID_VARIANT
:
1773 next_field
= bt_ctf_field_variant_get_current_field(base_field
);
1776 BT_LOGF("Unknown base field type ID: "
1777 "notit-addr=%p, ft-addr=%p, ft-id=%s",
1779 bt_ctf_field_type_id_string(
1780 bt_ctf_field_type_get_type_id(base_type
)));
1789 void update_clock_state(uint64_t *state
,
1790 struct bt_ctf_field
*value_field
)
1792 struct bt_ctf_field_type
*value_type
= NULL
;
1793 uint64_t requested_new_value
;
1794 uint64_t requested_new_value_mask
;
1795 uint64_t cur_value_masked
;
1796 int requested_new_value_size
;
1799 value_type
= bt_ctf_field_get_type(value_field
);
1801 assert(bt_ctf_field_type_is_integer(value_type
));
1802 requested_new_value_size
=
1803 bt_ctf_field_type_integer_get_size(value_type
);
1804 assert(requested_new_value_size
> 0);
1805 ret
= bt_ctf_field_unsigned_integer_get_value(value_field
,
1806 &requested_new_value
);
1810 * Special case for a 64-bit new value, which is the limit
1811 * of a clock value as of this version: overwrite the
1812 * current value directly.
1814 if (requested_new_value_size
== 64) {
1815 *state
= requested_new_value
;
1819 requested_new_value_mask
= (1ULL << requested_new_value_size
) - 1;
1820 cur_value_masked
= *state
& requested_new_value_mask
;
1822 if (requested_new_value
< cur_value_masked
) {
1824 * It looks like a wrap happened on the number of bits
1825 * of the requested new value. Assume that the clock
1826 * value wrapped only one time.
1828 *state
+= requested_new_value_mask
+ 1;
1831 /* Clear the low bits of the current clock value. */
1832 *state
&= ~requested_new_value_mask
;
1834 /* Set the low bits of the current clock value. */
1835 *state
|= requested_new_value
;
1837 BT_LOGV("Updated clock's value from integer field's value: "
1838 "value=%" PRIu64
, *state
);
1843 enum bt_ctf_btr_status
update_clock(struct bt_ctf_notif_iter
*notit
,
1844 struct bt_ctf_field
*int_field
)
1846 gboolean clock_class_found
;
1847 uint64_t *clock_state
= NULL
;
1848 struct bt_ctf_field_type
*int_field_type
= NULL
;
1849 enum bt_ctf_btr_status ret
= BT_CTF_BTR_STATUS_OK
;
1850 struct bt_ctf_clock_class
*clock_class
= NULL
;
1852 int_field_type
= bt_ctf_field_get_type(int_field
);
1853 assert(int_field_type
);
1854 clock_class
= bt_ctf_field_type_integer_get_mapped_clock_class(
1856 if (likely(!clock_class
)) {
1860 clock_class_found
= g_hash_table_lookup_extended(notit
->clock_states
,
1861 clock_class
, NULL
, (gpointer
) &clock_state
);
1862 if (!clock_class_found
) {
1863 clock_state
= g_new0(uint64_t, 1);
1865 BT_LOGE_STR("Failed to allocate a uint64_t.");
1866 ret
= BT_CTF_BTR_STATUS_ENOMEM
;
1870 g_hash_table_insert(notit
->clock_states
, bt_get(clock_class
),
1874 /* Update the clock's state. */
1875 BT_LOGV("Updating notification iterator's clock's value from integer field: "
1876 "notit-addr=%p, clock-class-addr=%p, "
1877 "clock-class-name=\"%s\", value=%" PRIu64
,
1879 bt_ctf_clock_class_get_name(clock_class
), *clock_state
);
1880 update_clock_state(clock_state
, int_field
);
1882 bt_put(int_field_type
);
1883 bt_put(clock_class
);
1888 enum bt_ctf_btr_status
btr_unsigned_int_common(uint64_t value
,
1889 struct bt_ctf_field_type
*type
, void *data
,
1890 struct bt_ctf_field
**out_int_field
)
1892 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1893 struct bt_ctf_field
*field
= NULL
;
1894 struct bt_ctf_field
*int_field
= NULL
;
1895 struct bt_ctf_notif_iter
*notit
= data
;
1898 BT_LOGV("Common unsigned integer function called from BTR: "
1899 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
1900 "ft-id=%s, value=%" PRIu64
,
1901 notit
, notit
->btr
, type
,
1902 bt_ctf_field_type_id_string(
1903 bt_ctf_field_type_get_type_id(type
)),
1906 /* Create next field */
1907 field
= get_next_field(notit
);
1909 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
1910 status
= BT_CTF_BTR_STATUS_ERROR
;
1914 switch(bt_ctf_field_type_get_type_id(type
)) {
1915 case BT_CTF_FIELD_TYPE_ID_INTEGER
:
1916 /* Integer field is created field */
1917 BT_MOVE(int_field
, field
);
1920 case BT_CTF_FIELD_TYPE_ID_ENUM
:
1921 int_field
= bt_ctf_field_enumeration_get_container(field
);
1923 type
= bt_ctf_field_get_type(int_field
);
1927 BT_LOGF("Unexpected field type ID: "
1928 "notit-addr=%p, ft-addr=%p, ft-id=%s",
1930 bt_ctf_field_type_id_string(
1931 bt_ctf_field_type_get_type_id(type
)));
1936 ret
= bt_ctf_field_unsigned_integer_set_value(int_field
, value
);
1938 stack_top(notit
->stack
)->index
++;
1939 *out_int_field
= int_field
;
1948 enum bt_ctf_btr_status
btr_timestamp_end_cb(void *value
,
1949 struct bt_ctf_field_type
*type
, void *data
)
1951 enum bt_ctf_btr_status status
;
1952 struct bt_ctf_field
*field
= NULL
;
1953 struct bt_ctf_notif_iter
*notit
= data
;
1955 BT_LOGV("`timestamp_end` unsigned integer function called from BTR: "
1956 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
1958 notit
, notit
->btr
, type
,
1959 bt_ctf_field_type_id_string(
1960 bt_ctf_field_type_get_type_id(type
)));
1961 status
= btr_unsigned_int_common(*((uint64_t *) value
), type
, data
,
1964 /* Set as the current packet's timestamp_end field. */
1965 BT_MOVE(notit
->cur_timestamp_end
, field
);
1970 enum bt_ctf_btr_status
btr_unsigned_int_cb(uint64_t value
,
1971 struct bt_ctf_field_type
*type
, void *data
)
1973 struct bt_ctf_notif_iter
*notit
= data
;
1974 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1975 struct bt_ctf_field
*field
= NULL
;
1976 struct field_cb_override
*override
;
1978 BT_LOGV("Unsigned integer function called from BTR: "
1979 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
1980 "ft-id=%s, value=%" PRIu64
,
1981 notit
, notit
->btr
, type
,
1982 bt_ctf_field_type_id_string(
1983 bt_ctf_field_type_get_type_id(type
)),
1985 override
= g_hash_table_lookup(notit
->field_overrides
, type
);
1986 if (unlikely(override
)) {
1987 /* Override function logs errors */
1988 status
= override
->func(&value
, type
, override
->data
);
1992 status
= btr_unsigned_int_common(value
, type
, data
, &field
);
1993 if (status
!= BT_CTF_BTR_STATUS_OK
) {
1994 /* btr_unsigned_int_common() logs errors */
1998 status
= update_clock(notit
, field
);
2005 enum bt_ctf_btr_status
btr_signed_int_cb(int64_t value
,
2006 struct bt_ctf_field_type
*type
, void *data
)
2008 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2009 struct bt_ctf_field
*field
= NULL
;
2010 struct bt_ctf_field
*int_field
= NULL
;
2011 struct bt_ctf_notif_iter
*notit
= data
;
2014 BT_LOGV("Signed integer function called from BTR: "
2015 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2016 "ft-id=%s, value=%" PRId64
,
2017 notit
, notit
->btr
, type
,
2018 bt_ctf_field_type_id_string(
2019 bt_ctf_field_type_get_type_id(type
)),
2022 /* create next field */
2023 field
= get_next_field(notit
);
2025 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2026 status
= BT_CTF_BTR_STATUS_ERROR
;
2030 switch(bt_ctf_field_type_get_type_id(type
)) {
2031 case BT_CTF_FIELD_TYPE_ID_INTEGER
:
2032 /* Integer field is created field */
2033 BT_MOVE(int_field
, field
);
2036 case BT_CTF_FIELD_TYPE_ID_ENUM
:
2037 int_field
= bt_ctf_field_enumeration_get_container(field
);
2039 type
= bt_ctf_field_get_type(int_field
);
2043 BT_LOGF("Unexpected field type ID: "
2044 "notit-addr=%p, ft-addr=%p, ft-id=%s",
2046 bt_ctf_field_type_id_string(
2047 bt_ctf_field_type_get_type_id(type
)));
2052 ret
= bt_ctf_field_signed_integer_set_value(int_field
, value
);
2054 stack_top(notit
->stack
)->index
++;
2055 status
= update_clock(notit
, int_field
);
2065 enum bt_ctf_btr_status
btr_floating_point_cb(double value
,
2066 struct bt_ctf_field_type
*type
, void *data
)
2068 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2069 struct bt_ctf_field
*field
= NULL
;
2070 struct bt_ctf_notif_iter
*notit
= data
;
2073 BT_LOGV("Floating point number function called from BTR: "
2074 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2075 "ft-id=%s, value=%f",
2076 notit
, notit
->btr
, type
,
2077 bt_ctf_field_type_id_string(
2078 bt_ctf_field_type_get_type_id(type
)),
2081 /* Create next field */
2082 field
= get_next_field(notit
);
2084 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2085 status
= BT_CTF_BTR_STATUS_ERROR
;
2089 ret
= bt_ctf_field_floating_point_set_value(field
, value
);
2091 stack_top(notit
->stack
)->index
++;
2099 enum bt_ctf_btr_status
btr_string_begin_cb(
2100 struct bt_ctf_field_type
*type
, void *data
)
2102 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2103 struct bt_ctf_field
*field
= NULL
;
2104 struct bt_ctf_notif_iter
*notit
= data
;
2107 BT_LOGV("String (beginning) function called from BTR: "
2108 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2110 notit
, notit
->btr
, type
,
2111 bt_ctf_field_type_id_string(
2112 bt_ctf_field_type_get_type_id(type
)));
2114 /* Create next field */
2115 field
= get_next_field(notit
);
2117 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2118 status
= BT_CTF_BTR_STATUS_ERROR
;
2123 * Push on stack. Not a compound type per se, but we know that only
2124 * btr_string_cb() may be called between this call and a subsequent
2125 * call to btr_string_end_cb().
2127 ret
= stack_push(notit
->stack
, field
);
2129 BT_LOGE("Cannot push string field on stack: "
2130 "notit-addr=%p, field-addr=%p", notit
, field
);
2131 status
= BT_CTF_BTR_STATUS_ERROR
;
2136 * Initialize string field payload to an empty string since in the
2137 * case of a length 0 string the btr_string_cb won't be called and
2138 * we will end up with an unset string payload.
2140 ret
= bt_ctf_field_string_set_value(field
, "");
2142 BT_LOGE("Cannot initialize string field's value to an empty string: "
2143 "notit-addr=%p, field-addr=%p, ret=%d",
2145 status
= BT_CTF_BTR_STATUS_ERROR
;
2156 enum bt_ctf_btr_status
btr_string_cb(const char *value
,
2157 size_t len
, struct bt_ctf_field_type
*type
, void *data
)
2159 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2160 struct bt_ctf_field
*field
= NULL
;
2161 struct bt_ctf_notif_iter
*notit
= data
;
2164 BT_LOGV("String (substring) function called from BTR: "
2165 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2166 "ft-id=%s, string-length=%zu",
2167 notit
, notit
->btr
, type
,
2168 bt_ctf_field_type_id_string(
2169 bt_ctf_field_type_get_type_id(type
)),
2172 /* Get string field */
2173 field
= stack_top(notit
->stack
)->base
;
2176 /* Append current string */
2177 ret
= bt_ctf_field_string_append_len(field
, value
, len
);
2179 BT_LOGE("Cannot append substring to string field's value: "
2180 "notit-addr=%p, field-addr=%p, string-length=%zu, "
2181 "ret=%d", notit
, field
, len
, ret
);
2182 status
= BT_CTF_BTR_STATUS_ERROR
;
2191 enum bt_ctf_btr_status
btr_string_end_cb(
2192 struct bt_ctf_field_type
*type
, void *data
)
2194 struct bt_ctf_notif_iter
*notit
= data
;
2196 BT_LOGV("String (end) function called from BTR: "
2197 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2199 notit
, notit
->btr
, type
,
2200 bt_ctf_field_type_id_string(
2201 bt_ctf_field_type_get_type_id(type
)));
2203 /* Pop string field */
2204 stack_pop(notit
->stack
);
2206 /* Go to next field */
2207 stack_top(notit
->stack
)->index
++;
2208 return BT_CTF_BTR_STATUS_OK
;
2211 enum bt_ctf_btr_status
btr_compound_begin_cb(
2212 struct bt_ctf_field_type
*type
, void *data
)
2214 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2215 struct bt_ctf_notif_iter
*notit
= data
;
2216 struct bt_ctf_field
*field
;
2219 BT_LOGV("Compound (beginning) function called from BTR: "
2220 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2222 notit
, notit
->btr
, type
,
2223 bt_ctf_field_type_id_string(
2224 bt_ctf_field_type_get_type_id(type
)));
2227 if (stack_empty(notit
->stack
)) {
2228 /* Root: create dynamic scope field */
2229 *notit
->cur_dscope_field
= bt_ctf_field_create(type
);
2230 field
= *notit
->cur_dscope_field
;
2233 * Field will be put at the end of this function
2234 * (stack_push() will take one reference, but this
2235 * reference is lost upon the equivalent stack_pop()
2236 * later), so also get it for our context to own it.
2238 bt_get(*notit
->cur_dscope_field
);
2241 BT_LOGE("Cannot create compound field: "
2242 "notit-addr=%p, ft-addr=%p, ft-id=%s",
2244 bt_ctf_field_type_id_string(
2245 bt_ctf_field_type_get_type_id(type
)));
2246 status
= BT_CTF_BTR_STATUS_ERROR
;
2250 field
= get_next_field(notit
);
2252 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2253 status
= BT_CTF_BTR_STATUS_ERROR
;
2260 ret
= stack_push(notit
->stack
, field
);
2262 BT_LOGE("Cannot push compound field onto the stack: "
2263 "notit-addr=%p, ft-addr=%p, ft-id=%s, ret=%d",
2265 bt_ctf_field_type_id_string(
2266 bt_ctf_field_type_get_type_id(type
)),
2268 status
= BT_CTF_BTR_STATUS_ERROR
;
2278 enum bt_ctf_btr_status
btr_compound_end_cb(
2279 struct bt_ctf_field_type
*type
, void *data
)
2281 struct bt_ctf_notif_iter
*notit
= data
;
2283 BT_LOGV("Compound (end) function called from BTR: "
2284 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2286 notit
, notit
->btr
, type
,
2287 bt_ctf_field_type_id_string(
2288 bt_ctf_field_type_get_type_id(type
)));
2289 assert(!stack_empty(notit
->stack
));
2292 stack_pop(notit
->stack
);
2294 /* If the stack is not empty, increment the base's index */
2295 if (!stack_empty(notit
->stack
)) {
2296 stack_top(notit
->stack
)->index
++;
2299 return BT_CTF_BTR_STATUS_OK
;
2303 struct bt_ctf_field
*resolve_field(struct bt_ctf_notif_iter
*notit
,
2304 struct bt_ctf_field_path
*path
)
2306 struct bt_ctf_field
*field
= NULL
;
2309 if (BT_LOG_ON_VERBOSE
) {
2310 GString
*gstr
= bt_ctf_field_path_string(path
);
2312 BT_LOGV("Resolving field path: notit-addr=%p, field-path=\"%s\"",
2313 notit
, gstr
? gstr
->str
: NULL
);
2316 g_string_free(gstr
, TRUE
);
2320 switch (bt_ctf_field_path_get_root_scope(path
)) {
2321 case BT_CTF_SCOPE_TRACE_PACKET_HEADER
:
2322 field
= notit
->dscopes
.trace_packet_header
;
2324 case BT_CTF_SCOPE_STREAM_PACKET_CONTEXT
:
2325 field
= notit
->dscopes
.stream_packet_context
;
2327 case BT_CTF_SCOPE_STREAM_EVENT_HEADER
:
2328 field
= notit
->dscopes
.stream_event_header
;
2330 case BT_CTF_SCOPE_STREAM_EVENT_CONTEXT
:
2331 field
= notit
->dscopes
.stream_event_context
;
2333 case BT_CTF_SCOPE_EVENT_CONTEXT
:
2334 field
= notit
->dscopes
.event_context
;
2336 case BT_CTF_SCOPE_EVENT_FIELDS
:
2337 field
= notit
->dscopes
.event_payload
;
2340 BT_LOGF("Cannot resolve field path: unknown scope: "
2341 "notit-addr=%p, root-scope=%s",
2342 notit
, bt_ctf_scope_string(
2343 bt_ctf_field_path_get_root_scope(path
)));
2348 BT_LOGW("Cannot resolve field path: root field not found: "
2349 "notit-addr=%p, root-scope=%s",
2350 notit
, bt_ctf_scope_string(
2351 bt_ctf_field_path_get_root_scope(path
)));
2357 for (i
= 0; i
< bt_ctf_field_path_get_index_count(path
); ++i
) {
2358 struct bt_ctf_field
*next_field
= NULL
;
2359 struct bt_ctf_field_type
*field_type
;
2360 int index
= bt_ctf_field_path_get_index(path
, i
);
2362 field_type
= bt_ctf_field_get_type(field
);
2365 if (is_struct_type(field_type
)) {
2366 next_field
= bt_ctf_field_structure_get_field_by_index(
2368 } else if (is_variant_type(field_type
)) {
2370 bt_ctf_field_variant_get_current_field(field
);
2377 BT_LOGW("Cannot find next field: "
2378 "notit-addr=%p, ft-addr=%p, ft-id=%s, index=%d",
2380 bt_ctf_field_type_id_string(
2381 bt_ctf_field_type_get_type_id(field_type
)),
2386 /* Move next field -> field */
2387 BT_MOVE(field
, next_field
);
2395 int64_t btr_get_sequence_length_cb(struct bt_ctf_field_type
*type
, void *data
)
2399 struct bt_ctf_field
*seq_field
;
2400 struct bt_ctf_field_path
*field_path
;
2401 struct bt_ctf_notif_iter
*notit
= data
;
2402 struct bt_ctf_field
*length_field
= NULL
;
2405 field_path
= bt_ctf_field_type_sequence_get_length_field_path(type
);
2407 length_field
= resolve_field(notit
, field_path
);
2408 if (!length_field
) {
2409 BT_LOGW("Cannot resolve sequence field type's length field path: "
2410 "notit-addr=%p, ft-addr=%p",
2415 iret
= bt_ctf_field_unsigned_integer_get_value(length_field
, &length
);
2417 BT_LOGE("Cannot get value of sequence length field: "
2418 "notit-addr=%p, field-addr=%p",
2419 notit
, length_field
);
2423 seq_field
= stack_top(notit
->stack
)->base
;
2424 iret
= bt_ctf_field_sequence_set_length(seq_field
, length_field
);
2426 BT_LOGE("Cannot set sequence field's length field: "
2427 "notit-addr=%p, seq-field-addr=%p, "
2428 "length-field-addr=%p, ",
2429 notit
, seq_field
, length_field
);
2433 ret
= (int64_t) length
;
2436 BT_PUT(length_field
);
2443 struct bt_ctf_field_type
*btr_get_variant_type_cb(
2444 struct bt_ctf_field_type
*type
, void *data
)
2446 struct bt_ctf_field_path
*path
;
2447 struct bt_ctf_notif_iter
*notit
= data
;
2448 struct bt_ctf_field
*var_field
;
2449 struct bt_ctf_field
*tag_field
= NULL
;
2450 struct bt_ctf_field
*selected_field
= NULL
;
2451 struct bt_ctf_field_type
*selected_field_type
= NULL
;
2453 path
= bt_ctf_field_type_variant_get_tag_field_path(type
);
2455 tag_field
= resolve_field(notit
, path
);
2457 BT_LOGW("Cannot resolve variant field type's tag field path: "
2458 "notit-addr=%p, ft-addr=%p",
2464 * We found the enumeration tag field instance which should be
2465 * able to select a current field for this variant. This
2466 * callback function we're in is called _after_
2467 * compound_begin(), so the current stack top's base field is
2468 * the variant field in question. We get the selected field here
2469 * thanks to this tag field (thus creating the selected field),
2470 * which will also provide us with its type. Then, this field
2471 * will remain the current selected one until the next callback
2472 * function call which is used to fill the current selected
2475 var_field
= stack_top(notit
->stack
)->base
;
2476 selected_field
= bt_ctf_field_variant_get_field(var_field
, tag_field
);
2477 if (!selected_field
) {
2478 BT_LOGW("Cannot get variant field's selection using tag field: "
2479 "notit-addr=%p, var-field-addr=%p, tag-field-addr=%p",
2480 notit
, var_field
, tag_field
);
2484 selected_field_type
= bt_ctf_field_get_type(selected_field
);
2488 BT_PUT(selected_field
);
2491 return selected_field_type
;
2495 int set_event_clocks(struct bt_ctf_event
*event
,
2496 struct bt_ctf_notif_iter
*notit
)
2499 GHashTableIter iter
;
2500 struct bt_ctf_clock_class
*clock_class
;
2501 uint64_t *clock_state
;
2503 g_hash_table_iter_init(&iter
, notit
->clock_states
);
2505 while (g_hash_table_iter_next(&iter
, (gpointer
) &clock_class
,
2506 (gpointer
) &clock_state
)) {
2507 struct bt_ctf_clock_value
*clock_value
;
2509 clock_value
= bt_ctf_clock_value_create(clock_class
,
2512 BT_LOGE("Cannot create clock value from clock class: "
2513 "notit-addr=%p, clock-class-addr=%p, "
2514 "clock-class-name=\"%s\"",
2516 bt_ctf_clock_class_get_name(clock_class
));
2520 ret
= bt_ctf_event_set_clock_value(event
, clock_value
);
2521 bt_put(clock_value
);
2523 struct bt_ctf_event_class
*event_class
=
2524 bt_ctf_event_get_class(event
);
2526 assert(event_class
);
2527 BT_LOGE("Cannot set event's clock value: "
2528 "notit-addr=%p, event-addr=%p, "
2529 "event-class-name=\"%s\", "
2530 "event-class-id=%" PRId64
", "
2531 "clock-class-addr=%p, "
2532 "clock-class-name=\"%s\", "
2533 "clock-value-addr=%p",
2535 bt_ctf_event_class_get_name(event_class
),
2536 bt_ctf_event_class_get_id(event_class
),
2538 bt_ctf_clock_class_get_name(clock_class
),
2540 bt_put(event_class
);
2551 struct bt_ctf_event
*create_event(struct bt_ctf_notif_iter
*notit
)
2554 struct bt_ctf_event
*event
;
2556 BT_LOGV("Creating event for event notification: "
2557 "notit-addr=%p, event-class-addr=%p, "
2558 "event-class-name=\"%s\", "
2559 "event-class-id=%" PRId64
,
2560 notit
, notit
->meta
.event_class
,
2561 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2562 bt_ctf_event_class_get_id(notit
->meta
.event_class
));
2564 /* Create event object. */
2565 event
= bt_ctf_event_create(notit
->meta
.event_class
);
2567 BT_LOGE("Cannot create event: "
2568 "notit-addr=%p, event-class-addr=%p, "
2569 "event-class-name=\"%s\", "
2570 "event-class-id=%" PRId64
,
2571 notit
, notit
->meta
.event_class
,
2572 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2573 bt_ctf_event_class_get_id(notit
->meta
.event_class
));
2577 /* Set header, stream event context, context, and payload fields. */
2578 ret
= bt_ctf_event_set_header(event
,
2579 notit
->dscopes
.stream_event_header
);
2581 BT_LOGE("Cannot set event's header field: "
2582 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2583 "event-class-name=\"%s\", "
2584 "event-class-id=%" PRId64
", field-addr=%p",
2585 notit
, event
, notit
->meta
.event_class
,
2586 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2587 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
2588 notit
->dscopes
.stream_event_header
);
2592 ret
= bt_ctf_event_set_stream_event_context(event
,
2593 notit
->dscopes
.stream_event_context
);
2595 BT_LOGE("Cannot set event's context field: "
2596 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2597 "event-class-name=\"%s\", "
2598 "event-class-id=%" PRId64
", field-addr=%p",
2599 notit
, event
, notit
->meta
.event_class
,
2600 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2601 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
2602 notit
->dscopes
.stream_event_context
);
2606 ret
= bt_ctf_event_set_event_context(event
,
2607 notit
->dscopes
.event_context
);
2609 BT_LOGE("Cannot set event's stream event context field: "
2610 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2611 "event-class-name=\"%s\", "
2612 "event-class-id=%" PRId64
", field-addr=%p",
2613 notit
, event
, notit
->meta
.event_class
,
2614 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2615 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
2616 notit
->dscopes
.event_context
);
2620 ret
= bt_ctf_event_set_event_payload(event
,
2621 notit
->dscopes
.event_payload
);
2623 BT_LOGE("Cannot set event's payload field: "
2624 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2625 "event-class-name=\"%s\", "
2626 "event-class-id=%" PRId64
", field-addr=%p",
2627 notit
, event
, notit
->meta
.event_class
,
2628 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2629 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
2630 notit
->dscopes
.event_payload
);
2634 ret
= set_event_clocks(event
, notit
);
2636 BT_LOGE("Cannot set event's clock values: "
2637 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2638 "event-class-name=\"%s\", "
2639 "event-class-id=%" PRId64
,
2640 notit
, event
, notit
->meta
.event_class
,
2641 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2642 bt_ctf_event_class_get_id(notit
->meta
.event_class
));
2646 /* Associate with current packet. */
2647 assert(notit
->packet
);
2648 ret
= bt_ctf_event_set_packet(event
, notit
->packet
);
2650 BT_LOGE("Cannot set event's header field: "
2651 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2652 "event-class-name=\"%s\", "
2653 "event-class-id=%" PRId64
", packet-addr=%p",
2654 notit
, event
, notit
->meta
.event_class
,
2655 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2656 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
2671 int set_stream(struct bt_ctf_notif_iter
*notit
)
2674 struct bt_ctf_stream
*stream
= NULL
;
2676 BT_LOGV("Calling user function (get stream): notit-addr=%p, "
2677 "stream-class-addr=%p, stream-class-name=\"%s\", "
2678 "stream-class-id=%" PRId64
,
2679 notit
, notit
->meta
.stream_class
,
2680 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
2681 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
2682 stream
= bt_get(notit
->medium
.medops
.get_stream(
2683 notit
->meta
.stream_class
, notit
->medium
.data
));
2684 BT_LOGV("User function returned: stream-addr=%p", stream
);
2686 BT_LOGW_STR("User function failed to return a stream object for the given stream class.");
2691 if (notit
->stream
&& stream
!= notit
->stream
) {
2692 BT_LOGW("User function returned a different stream than the previous one for the same sequence of packets.");
2697 BT_MOVE(notit
->stream
, stream
);
2705 void create_packet(struct bt_ctf_notif_iter
*notit
)
2708 struct bt_ctf_packet
*packet
= NULL
;
2710 BT_LOGV("Creating packet for packet notification: "
2711 "notit-addr=%p", notit
);
2713 /* Ask the user for the stream */
2714 ret
= set_stream(notit
);
2719 BT_LOGV("Creating packet from stream: "
2720 "notit-addr=%p, stream-addr=%p, "
2721 "stream-class-addr=%p, "
2722 "stream-class-name=\"%s\", "
2723 "stream-class-id=%" PRId64
,
2724 notit
, notit
->stream
, notit
->meta
.stream_class
,
2725 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
2726 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
2729 packet
= bt_ctf_packet_create(notit
->stream
);
2731 BT_LOGE("Cannot create packet from stream: "
2732 "notit-addr=%p, stream-addr=%p, "
2733 "stream-class-addr=%p, "
2734 "stream-class-name=\"%s\", "
2735 "stream-class-id=%" PRId64
,
2736 notit
, notit
->stream
, notit
->meta
.stream_class
,
2737 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
2738 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
2742 /* Set packet's context and header fields */
2743 if (notit
->dscopes
.trace_packet_header
) {
2744 ret
= bt_ctf_packet_set_header(packet
,
2745 notit
->dscopes
.trace_packet_header
);
2747 BT_LOGE("Cannot set packet's header field: "
2748 "notit-addr=%p, packet-addr=%p, "
2750 "stream-class-addr=%p, "
2751 "stream-class-name=\"%s\", "
2752 "stream-class-id=%" PRId64
", "
2754 notit
, packet
, notit
->stream
, notit
->meta
.stream_class
,
2755 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
2756 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
2757 notit
->dscopes
.trace_packet_header
);
2762 if (notit
->dscopes
.stream_packet_context
) {
2763 ret
= bt_ctf_packet_set_context(packet
,
2764 notit
->dscopes
.stream_packet_context
);
2766 BT_LOGE("Cannot set packet's context field: "
2767 "notit-addr=%p, packet-addr=%p, "
2769 "stream-class-addr=%p, "
2770 "stream-class-name=\"%s\", "
2771 "stream-class-id=%" PRId64
", "
2773 notit
, packet
, notit
->stream
, notit
->meta
.stream_class
,
2774 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
2775 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
2776 notit
->dscopes
.trace_packet_header
);
2787 BT_MOVE(notit
->packet
, packet
);
2791 void notify_new_packet(struct bt_ctf_notif_iter
*notit
,
2792 struct bt_notification
**notification
)
2794 struct bt_notification
*ret
;
2796 /* Initialize the iterator's current packet */
2797 create_packet(notit
);
2798 if (!notit
->packet
) {
2799 BT_LOGE("Cannot create packet for packet notification: "
2800 "notit-addr=%p", notit
);
2804 ret
= bt_notification_packet_begin_create(notit
->packet
);
2806 BT_LOGE("Cannot create packet beginning notification: "
2807 "notit-addr=%p, packet-addr=%p",
2808 notit
, notit
->packet
);
2811 *notification
= ret
;
2815 void notify_end_of_packet(struct bt_ctf_notif_iter
*notit
,
2816 struct bt_notification
**notification
)
2818 struct bt_notification
*ret
;
2820 if (!notit
->packet
) {
2824 ret
= bt_notification_packet_end_create(notit
->packet
);
2826 BT_LOGE("Cannot create packet end notification: "
2827 "notit-addr=%p, packet-addr=%p",
2828 notit
, notit
->packet
);
2831 BT_PUT(notit
->packet
);
2832 *notification
= ret
;
2836 void notify_event(struct bt_ctf_notif_iter
*notit
,
2837 struct bt_clock_class_priority_map
*cc_prio_map
,
2838 struct bt_notification
**notification
)
2840 struct bt_ctf_event
*event
;
2841 struct bt_notification
*ret
= NULL
;
2844 event
= create_event(notit
);
2846 BT_LOGE("Cannot create event for event notification: "
2847 "notit-addr=%p", notit
);
2851 ret
= bt_notification_event_create(event
, cc_prio_map
);
2853 BT_LOGE("Cannot create event notification: "
2854 "notit-addr=%p, event-addr=%p, "
2855 "cc-prio-map-addr=%p",
2856 notit
, event
, cc_prio_map
);
2859 *notification
= ret
;
2865 void init_trace_field_path_cache(struct bt_ctf_trace
*trace
,
2866 struct trace_field_path_cache
*trace_field_path_cache
)
2869 int stream_instance_id
= -1;
2871 struct bt_ctf_field_type
*packet_header
= NULL
;
2873 packet_header
= bt_ctf_trace_get_packet_header_type(trace
);
2874 if (!packet_header
) {
2878 if (!bt_ctf_field_type_is_structure(packet_header
)) {
2882 count
= bt_ctf_field_type_structure_get_field_count(packet_header
);
2885 for (i
= 0; (i
< count
&& (stream_id
== -1 || stream_instance_id
== -1)); i
++) {
2887 const char *field_name
;
2889 ret
= bt_ctf_field_type_structure_get_field(packet_header
,
2890 &field_name
, NULL
, i
);
2892 BT_LOGE("Cannot get structure field's field: "
2893 "field-addr=%p, index=%d",
2898 if (stream_id
== -1 && !strcmp(field_name
, "stream_id")) {
2900 } else if (stream_instance_id
== -1 &&
2901 !strcmp(field_name
, "stream_instance_id")) {
2902 stream_instance_id
= i
;
2907 trace_field_path_cache
->stream_id
= stream_id
;
2908 trace_field_path_cache
->stream_instance_id
= stream_instance_id
;
2909 BT_PUT(packet_header
);
2913 struct bt_ctf_notif_iter
*bt_ctf_notif_iter_create(struct bt_ctf_trace
*trace
,
2914 size_t max_request_sz
,
2915 struct bt_ctf_notif_iter_medium_ops medops
, void *data
)
2917 struct bt_ctf_notif_iter
*notit
= NULL
;
2918 struct bt_ctf_btr_cbs cbs
= {
2920 .signed_int
= btr_signed_int_cb
,
2921 .unsigned_int
= btr_unsigned_int_cb
,
2922 .floating_point
= btr_floating_point_cb
,
2923 .string_begin
= btr_string_begin_cb
,
2924 .string
= btr_string_cb
,
2925 .string_end
= btr_string_end_cb
,
2926 .compound_begin
= btr_compound_begin_cb
,
2927 .compound_end
= btr_compound_end_cb
,
2930 .get_sequence_length
= btr_get_sequence_length_cb
,
2931 .get_variant_type
= btr_get_variant_type_cb
,
2936 assert(medops
.request_bytes
);
2937 assert(medops
.get_stream
);
2938 BT_LOGD("Creating CTF plugin notification iterator: "
2939 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
2941 trace
, bt_ctf_trace_get_name(trace
), max_request_sz
, data
);
2942 notit
= g_new0(struct bt_ctf_notif_iter
, 1);
2944 BT_LOGE_STR("Failed to allocate one CTF plugin notification iterator.");
2947 notit
->clock_states
= g_hash_table_new_full(g_direct_hash
,
2948 g_direct_equal
, bt_put
, g_free
);
2949 if (!notit
->clock_states
) {
2950 BT_LOGE_STR("Failed to allocate a GHashTable.");
2953 notit
->meta
.trace
= bt_get(trace
);
2954 notit
->medium
.medops
= medops
;
2955 notit
->medium
.max_request_sz
= max_request_sz
;
2956 notit
->medium
.data
= data
;
2957 notit
->stack
= stack_new(notit
);
2958 if (!notit
->stack
) {
2959 BT_LOGE_STR("Failed to create field stack.");
2963 notit
->btr
= bt_ctf_btr_create(cbs
, notit
);
2965 BT_LOGE_STR("Failed to create binary type reader (BTR).");
2969 bt_ctf_notif_iter_reset(notit
);
2970 init_trace_field_path_cache(trace
, ¬it
->trace_field_path_cache
);
2971 notit
->sc_field_path_caches
= g_hash_table_new_full(g_direct_hash
,
2972 g_direct_equal
, bt_put
, g_free
);
2973 if (!notit
->sc_field_path_caches
) {
2974 BT_LOGE_STR("Failed to allocate a GHashTable.");
2978 notit
->field_overrides
= g_hash_table_new_full(g_direct_hash
,
2979 g_direct_equal
, bt_put
, g_free
);
2980 if (!notit
->field_overrides
) {
2981 BT_LOGE_STR("Failed to allocate a GHashTable.");
2985 BT_LOGD("Created CTF plugin notification iterator: "
2986 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
2987 "data=%p, notit-addr=%p",
2988 trace
, bt_ctf_trace_get_name(trace
), max_request_sz
, data
,
2995 bt_ctf_notif_iter_destroy(notit
);
3000 void bt_ctf_notif_iter_destroy(struct bt_ctf_notif_iter
*notit
)
3002 BT_PUT(notit
->meta
.trace
);
3003 BT_PUT(notit
->meta
.stream_class
);
3004 BT_PUT(notit
->meta
.event_class
);
3005 BT_PUT(notit
->packet
);
3006 BT_PUT(notit
->stream
);
3007 BT_PUT(notit
->cur_timestamp_end
);
3008 put_all_dscopes(notit
);
3010 BT_LOGD("Destroying CTF plugin notification iterator: addr=%p", notit
);
3013 BT_LOGD_STR("Destroying field stack.");
3014 stack_destroy(notit
->stack
);
3018 BT_LOGD("Destroying BTR: btr-addr=%p", notit
->btr
);
3019 bt_ctf_btr_destroy(notit
->btr
);
3022 if (notit
->clock_states
) {
3023 g_hash_table_destroy(notit
->clock_states
);
3026 if (notit
->sc_field_path_caches
) {
3027 g_hash_table_destroy(notit
->sc_field_path_caches
);
3030 if (notit
->field_overrides
) {
3031 g_hash_table_destroy(notit
->field_overrides
);
3037 enum bt_ctf_notif_iter_status
bt_ctf_notif_iter_get_next_notification(
3038 struct bt_ctf_notif_iter
*notit
,
3039 struct bt_clock_class_priority_map
*cc_prio_map
,
3040 struct bt_notification
**notification
)
3042 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
3045 assert(notification
);
3047 BT_LOGV("Getting next notification: notit-addr=%p, cc-prio-map-addr=%p",
3048 notit
, cc_prio_map
);
3051 status
= handle_state(notit
);
3052 if (status
== BT_CTF_NOTIF_ITER_STATUS_AGAIN
) {
3053 BT_LOGV_STR("Medium returned BT_CTF_NOTIF_ITER_STATUS_AGAIN.");
3056 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
3057 if (status
== BT_CTF_NOTIF_ITER_STATUS_EOF
) {
3058 BT_LOGV_STR("Medium returned BT_CTF_NOTIF_ITER_STATUS_EOF.");
3060 BT_LOGW("Cannot handle state: "
3061 "notit-addr=%p, state=%s",
3062 notit
, state_string(notit
->state
));
3067 switch (notit
->state
) {
3068 case STATE_EMIT_NOTIF_NEW_PACKET
:
3069 /* notify_new_packet() logs errors */
3070 notify_new_packet(notit
, notification
);
3071 if (!*notification
) {
3072 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
3075 case STATE_EMIT_NOTIF_EVENT
:
3076 /* notify_event() logs errors */
3077 notify_event(notit
, cc_prio_map
, notification
);
3078 if (!*notification
) {
3079 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
3082 case STATE_EMIT_NOTIF_END_OF_PACKET
:
3083 /* Update clock with timestamp_end field. */
3084 if (notit
->cur_timestamp_end
) {
3085 enum bt_ctf_btr_status btr_status
;
3086 struct bt_ctf_field_type
*field_type
=
3087 bt_ctf_field_get_type(
3088 notit
->cur_timestamp_end
);
3091 btr_status
= update_clock(notit
,
3092 notit
->cur_timestamp_end
);
3094 if (btr_status
!= BT_CTF_BTR_STATUS_OK
) {
3095 BT_LOGW("Cannot update stream's clock value: "
3096 "notit-addr=%p", notit
);
3097 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
3102 /* notify_end_of_packet() logs errors */
3103 notify_end_of_packet(notit
, notification
);
3104 if (!*notification
) {
3105 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
3109 /* Non-emitting state: continue */
3119 enum bt_ctf_notif_iter_status
bt_ctf_notif_iter_get_packet_header_context_fields(
3120 struct bt_ctf_notif_iter
*notit
,
3121 struct bt_ctf_field
**packet_header_field
,
3122 struct bt_ctf_field
**packet_context_field
)
3124 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
3128 if (notit
->state
== STATE_EMIT_NOTIF_NEW_PACKET
) {
3129 /* We're already there */
3134 status
= handle_state(notit
);
3135 if (status
== BT_CTF_NOTIF_ITER_STATUS_AGAIN
) {
3136 BT_LOGV_STR("Medium returned BT_CTF_NOTIF_ITER_STATUS_AGAIN.");
3139 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
3140 if (status
== BT_CTF_NOTIF_ITER_STATUS_EOF
) {
3141 BT_LOGV_STR("Medium returned BT_CTF_NOTIF_ITER_STATUS_EOF.");
3143 BT_LOGW("Cannot handle state: "
3144 "notit-addr=%p, state=%s",
3145 notit
, state_string(notit
->state
));
3150 switch (notit
->state
) {
3151 case STATE_EMIT_NOTIF_NEW_PACKET
:
3153 * Packet header and context fields are
3154 * potentially decoded (or they don't exist).
3158 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
3159 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
3160 case STATE_AFTER_TRACE_PACKET_HEADER
:
3161 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
3162 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
3163 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
3164 /* Non-emitting state: continue */
3168 * We should never get past the
3169 * STATE_EMIT_NOTIF_NEW_PACKET state.
3171 BT_LOGF("Unexpected state: notit-addr=%p, state=%s",
3172 notit
, state_string(notit
->state
));
3178 if (packet_header_field
) {
3179 *packet_header_field
= bt_get(notit
->dscopes
.trace_packet_header
);
3182 if (packet_context_field
) {
3183 *packet_context_field
= bt_get(notit
->dscopes
.stream_packet_context
);
3191 void bt_ctf_notif_iter_set_medops_data(struct bt_ctf_notif_iter
*notit
,
3195 notit
->medium
.data
= medops_data
;