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
;
166 * Current timestamp_end field (to consider before switching packets).
168 struct bt_ctf_field
*cur_timestamp_end
;
170 /* Database of current dynamic scopes (owned by this) */
172 struct bt_ctf_field
*trace_packet_header
;
173 struct bt_ctf_field
*stream_packet_context
;
174 struct bt_ctf_field
*stream_event_header
;
175 struct bt_ctf_field
*stream_event_context
;
176 struct bt_ctf_field
*event_context
;
177 struct bt_ctf_field
*event_payload
;
181 * Special field overrides.
183 * Overrides are used to implement the behaviours of special fields such
184 * as "timestamp_end" (which must be ignored until the end of the
185 * packet), "id" (event id) which can be present multiple times and must
186 * be updated multiple time.
188 * This should be used to implement the behaviour of integer fields
189 * mapped to clocks and other "tagged" fields (in CTF 2).
191 * bt_ctf_field_type to struct field_cb_override
193 GHashTable
*field_overrides
;
198 /* Current medium buffer data */
200 /* Last address provided by medium */
203 /* Buffer size provided by medium (bytes) */
206 /* Offset within whole packet of addr (bits) */
207 size_t packet_offset
;
209 /* Current position from addr (bits) */
213 /* Binary type reader */
214 struct bt_ctf_btr
*btr
;
216 /* Current medium data */
218 struct bt_ctf_notif_iter_medium_ops medops
;
219 size_t max_request_sz
;
223 /* Current packet size (bits) (-1 if unknown) */
224 int64_t cur_packet_size
;
226 /* Current content size (bits) (-1 if unknown) */
227 int64_t cur_content_size
;
229 /* bt_ctf_clock_class to uint64_t. */
230 GHashTable
*clock_states
;
233 * Cache of the trace-constant field paths (event header type)
234 * associated to the current trace.
236 struct trace_field_path_cache trace_field_path_cache
;
239 * Field path cache associated with the current stream class.
240 * Ownership of this structure belongs to the field_path_caches HT.
242 struct stream_class_field_path_cache
*cur_sc_field_path_cache
;
244 /* bt_ctf_stream_class to struct stream_class_field_path_cache. */
245 GHashTable
*sc_field_path_caches
;
249 const char *state_string(enum state state
)
254 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
255 return "STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN";
256 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
257 return "STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE";
258 case STATE_AFTER_TRACE_PACKET_HEADER
:
259 return "STATE_AFTER_TRACE_PACKET_HEADER";
260 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
261 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN";
262 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
263 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE";
264 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
265 return "STATE_AFTER_STREAM_PACKET_CONTEXT";
266 case STATE_EMIT_NOTIF_NEW_PACKET
:
267 return "STATE_EMIT_NOTIF_NEW_PACKET";
268 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
:
269 return "STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN";
270 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
:
271 return "STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE";
272 case STATE_AFTER_STREAM_EVENT_HEADER
:
273 return "STATE_AFTER_STREAM_EVENT_HEADER";
274 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
:
275 return "STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN";
276 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
:
277 return "STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE";
278 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN
:
279 return "STATE_DSCOPE_EVENT_CONTEXT_BEGIN";
280 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
:
281 return "STATE_DSCOPE_EVENT_CONTEXT_CONTINUE";
282 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
283 return "STATE_DSCOPE_EVENT_PAYLOAD_BEGIN";
284 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
285 return "STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE";
286 case STATE_EMIT_NOTIF_EVENT
:
287 return "STATE_EMIT_NOTIF_EVENT";
288 case STATE_EMIT_NOTIF_END_OF_PACKET
:
289 return "STATE_EMIT_NOTIF_END_OF_PACKET";
290 case STATE_SKIP_PACKET_PADDING
:
291 return "STATE_SKIP_PACKET_PADDING";
298 int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter
*notit
);
301 enum bt_ctf_btr_status
btr_timestamp_end_cb(void *value
,
302 struct bt_ctf_field_type
*type
, void *data
);
305 void stack_entry_free_func(gpointer data
)
307 struct stack_entry
*entry
= data
;
314 struct stack
*stack_new(struct bt_ctf_notif_iter
*notit
)
316 struct stack
*stack
= NULL
;
318 stack
= g_new0(struct stack
, 1);
320 BT_LOGE_STR("Failed to allocate one stack.");
324 stack
->entries
= g_ptr_array_new_with_free_func(stack_entry_free_func
);
325 if (!stack
->entries
) {
326 BT_LOGE_STR("Failed to allocate a GPtrArray.");
330 BT_LOGD("Created stack: notit-addr=%p, stack-addr=%p", notit
, stack
);
339 void stack_destroy(struct stack
*stack
)
342 BT_LOGD("Destroying stack: addr=%p", stack
);
343 g_ptr_array_free(stack
->entries
, TRUE
);
348 int stack_push(struct stack
*stack
, struct bt_ctf_field
*base
)
351 struct stack_entry
*entry
;
355 BT_LOGV("Pushing base field on stack: stack-addr=%p, "
356 "stack-size-before=%u, stack-size-after=%u",
357 stack
, stack
->entries
->len
, stack
->entries
->len
+ 1);
358 entry
= g_new0(struct stack_entry
, 1);
360 BT_LOGE_STR("Failed to allocate one stack entry.");
365 entry
->base
= bt_get(base
);
366 g_ptr_array_add(stack
->entries
, entry
);
373 unsigned int stack_size(struct stack
*stack
)
377 return stack
->entries
->len
;
381 void stack_pop(struct stack
*stack
)
384 assert(stack_size(stack
));
385 BT_LOGV("Popping from stack: "
386 "stack-addr=%p, stack-size-before=%u, stack-size-after=%u",
387 stack
, stack
->entries
->len
, stack
->entries
->len
- 1);
388 g_ptr_array_remove_index(stack
->entries
, stack
->entries
->len
- 1);
392 struct stack_entry
*stack_top(struct stack
*stack
)
395 assert(stack_size(stack
));
397 return g_ptr_array_index(stack
->entries
, stack
->entries
->len
- 1);
401 bool stack_empty(struct stack
*stack
)
403 return stack_size(stack
) == 0;
407 void stack_clear(struct stack
*stack
)
411 if (!stack_empty(stack
)) {
412 BT_LOGV("Clearing stack: stack-addr=%p, stack-size=%u",
413 stack
, stack
->entries
->len
);
414 g_ptr_array_remove_range(stack
->entries
, 0, stack_size(stack
));
417 assert(stack_empty(stack
));
421 enum bt_ctf_notif_iter_status
notif_iter_status_from_m_status(
422 enum bt_ctf_notif_iter_medium_status m_status
)
424 return (int) m_status
;
428 size_t buf_size_bits(struct bt_ctf_notif_iter
*notit
)
430 return notit
->buf
.sz
* 8;
434 size_t buf_available_bits(struct bt_ctf_notif_iter
*notit
)
436 return buf_size_bits(notit
) - notit
->buf
.at
;
440 size_t packet_at(struct bt_ctf_notif_iter
*notit
)
442 return notit
->buf
.packet_offset
+ notit
->buf
.at
;
446 void buf_consume_bits(struct bt_ctf_notif_iter
*notit
, size_t incr
)
448 BT_LOGV("Advancing cursor: notit-addr=%p, cur-before=%zu, cur-after=%zu",
449 notit
, notit
->buf
.at
, notit
->buf
.at
+ incr
);
450 notit
->buf
.at
+= incr
;
454 enum bt_ctf_notif_iter_status
request_medium_bytes(
455 struct bt_ctf_notif_iter
*notit
)
457 uint8_t *buffer_addr
= NULL
;
458 size_t buffer_sz
= 0;
459 enum bt_ctf_notif_iter_medium_status m_status
;
461 BT_LOGV("Calling user function (request bytes): notit-addr=%p, "
462 "request-size=%zu", notit
, notit
->medium
.max_request_sz
);
463 m_status
= notit
->medium
.medops
.request_bytes(
464 notit
->medium
.max_request_sz
, &buffer_addr
,
465 &buffer_sz
, notit
->medium
.data
);
466 BT_LOGV("User function returned: status=%s, buf-addr=%p, buf-size=%zu",
467 bt_ctf_notif_iter_medium_status_string(m_status
),
468 buffer_addr
, buffer_sz
);
469 if (m_status
== BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK
) {
470 assert(buffer_sz
!= 0);
472 /* New packet offset is old one + old size (in bits) */
473 notit
->buf
.packet_offset
+= buf_size_bits(notit
);
475 /* Restart at the beginning of the new medium buffer */
478 /* New medium buffer size */
479 notit
->buf
.sz
= buffer_sz
;
481 /* New medium buffer address */
482 notit
->buf
.addr
= buffer_addr
;
484 BT_LOGV("User function returned new bytes: "
485 "packet-offset=%zu, cur=%zu, size=%zu, addr=%p",
486 notit
->buf
.packet_offset
, notit
->buf
.at
,
487 notit
->buf
.sz
, notit
->buf
.addr
);
488 BT_LOGV_MEM(buffer_addr
, buffer_sz
, "Returned bytes at %p:",
490 } else if (m_status
< 0) {
491 BT_LOGW("User function failed: status=%s",
492 bt_ctf_notif_iter_medium_status_string(m_status
));
495 return notif_iter_status_from_m_status(m_status
);
499 enum bt_ctf_notif_iter_status
buf_ensure_available_bits(
500 struct bt_ctf_notif_iter
*notit
)
502 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
504 if (buf_available_bits(notit
) == 0) {
506 * This _cannot_ return BT_CTF_NOTIF_ITER_STATUS_OK
509 status
= request_medium_bytes(notit
);
516 enum bt_ctf_notif_iter_status
read_dscope_begin_state(
517 struct bt_ctf_notif_iter
*notit
,
518 struct bt_ctf_field_type
*dscope_field_type
,
519 enum state done_state
, enum state continue_state
,
520 struct bt_ctf_field
**dscope_field
)
522 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
523 enum bt_ctf_btr_status btr_status
;
524 size_t consumed_bits
;
526 status
= buf_ensure_available_bits(notit
);
527 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
529 BT_LOGW("Cannot ensure that buffer has at least one byte: "
530 "notif-addr=%p, status=%s",
531 notit
, bt_ctf_notif_iter_status_string(status
));
533 BT_LOGV("Cannot ensure that buffer has at least one byte: "
534 "notif-addr=%p, status=%s",
535 notit
, bt_ctf_notif_iter_status_string(status
));
541 bt_put(*dscope_field
);
542 notit
->cur_dscope_field
= dscope_field
;
543 BT_LOGV("Starting BTR: notit-addr=%p, btr-addr=%p, ft-addr=%p",
544 notit
, notit
->btr
, dscope_field_type
);
545 consumed_bits
= bt_ctf_btr_start(notit
->btr
, dscope_field_type
,
546 notit
->buf
.addr
, notit
->buf
.at
, packet_at(notit
),
547 notit
->buf
.sz
, &btr_status
);
548 BT_LOGV("BTR consumed bits: size=%zu", consumed_bits
);
550 switch (btr_status
) {
551 case BT_CTF_BTR_STATUS_OK
:
552 /* type was read completely */
553 BT_LOGV_STR("Field was completely decoded.");
554 notit
->state
= done_state
;
556 case BT_CTF_BTR_STATUS_EOF
:
557 BT_LOGV_STR("BTR needs more data to decode field completely.");
558 notit
->state
= continue_state
;
561 BT_LOGW("BTR failed to start: notit-addr=%p, btr-addr=%p, "
562 "status=%s", notit
, notit
->btr
,
563 bt_ctf_btr_status_string(btr_status
));
564 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
568 /* Consume bits now since we know we're not in an error state */
569 buf_consume_bits(notit
, consumed_bits
);
576 enum bt_ctf_notif_iter_status
read_dscope_continue_state(
577 struct bt_ctf_notif_iter
*notit
, enum state done_state
)
579 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
580 enum bt_ctf_btr_status btr_status
;
581 size_t consumed_bits
;
583 status
= buf_ensure_available_bits(notit
);
584 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
586 BT_LOGW("Cannot ensure that buffer has at least one byte: "
587 "notif-addr=%p, status=%s",
588 notit
, bt_ctf_notif_iter_status_string(status
));
590 BT_LOGV("Cannot ensure that buffer has at least one byte: "
591 "notif-addr=%p, status=%s",
592 notit
, bt_ctf_notif_iter_status_string(status
));
598 BT_LOGV("Continuing BTR: notit-addr=%p, btr-addr=%p",
600 consumed_bits
= bt_ctf_btr_continue(notit
->btr
, notit
->buf
.addr
,
601 notit
->buf
.sz
, &btr_status
);
602 BT_LOGV("BTR consumed bits: size=%zu", consumed_bits
);
604 switch (btr_status
) {
605 case BT_CTF_BTR_STATUS_OK
:
606 /* Type was read completely. */
607 BT_LOGV_STR("Field was completely decoded.");
608 notit
->state
= done_state
;
610 case BT_CTF_BTR_STATUS_EOF
:
611 /* Stay in this continue state. */
612 BT_LOGV_STR("BTR needs more data to decode field completely.");
615 BT_LOGW("BTR failed to continue: notit-addr=%p, btr-addr=%p, "
616 "status=%s", notit
, notit
->btr
,
617 bt_ctf_btr_status_string(btr_status
));
618 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
622 /* Consume bits now since we know we're not in an error state. */
623 buf_consume_bits(notit
, consumed_bits
);
629 void put_event_dscopes(struct bt_ctf_notif_iter
*notit
)
631 BT_LOGV_STR("Putting event header field.");
632 BT_PUT(notit
->dscopes
.stream_event_header
);
633 BT_LOGV_STR("Putting stream event context field.");
634 BT_PUT(notit
->dscopes
.stream_event_context
);
635 BT_LOGV_STR("Putting event context field.");
636 BT_PUT(notit
->dscopes
.event_context
);
637 BT_LOGV_STR("Putting event payload field.");
638 BT_PUT(notit
->dscopes
.event_payload
);
642 void put_all_dscopes(struct bt_ctf_notif_iter
*notit
)
644 BT_LOGV_STR("Putting packet header field.");
645 BT_PUT(notit
->dscopes
.trace_packet_header
);
646 BT_LOGV_STR("Putting packet context field.");
647 BT_PUT(notit
->dscopes
.stream_packet_context
);
648 put_event_dscopes(notit
);
652 enum bt_ctf_notif_iter_status
read_packet_header_begin_state(
653 struct bt_ctf_notif_iter
*notit
)
655 struct bt_ctf_field_type
*packet_header_type
= NULL
;
656 enum bt_ctf_notif_iter_status ret
= BT_CTF_NOTIF_ITER_STATUS_OK
;
658 if (bt_ctf_notif_iter_switch_packet(notit
)) {
659 BT_LOGW("Cannot switch packet: notit-addr=%p", notit
);
660 ret
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
664 /* Packet header type is common to the whole trace. */
665 packet_header_type
= bt_ctf_trace_get_packet_header_type(
667 if (!packet_header_type
) {
668 notit
->state
= STATE_AFTER_TRACE_PACKET_HEADER
;
672 BT_LOGV("Decoding packet header field:"
673 "notit-addr=%p, trace-addr=%p, trace-name=\"%s\", ft-addr=%p",
674 notit
, notit
->meta
.trace
,
675 bt_ctf_trace_get_name(notit
->meta
.trace
), packet_header_type
);
676 ret
= read_dscope_begin_state(notit
, packet_header_type
,
677 STATE_AFTER_TRACE_PACKET_HEADER
,
678 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
,
679 ¬it
->dscopes
.trace_packet_header
);
681 BT_LOGW("Cannot decode packet header field: "
682 "notit-addr=%p, trace-addr=%p, "
683 "trace-name=\"%s\", ft-addr=%p",
684 notit
, notit
->meta
.trace
,
685 bt_ctf_trace_get_name(notit
->meta
.trace
),
689 BT_PUT(packet_header_type
);
694 enum bt_ctf_notif_iter_status
read_packet_header_continue_state(
695 struct bt_ctf_notif_iter
*notit
)
697 return read_dscope_continue_state(notit
,
698 STATE_AFTER_TRACE_PACKET_HEADER
);
702 bool is_struct_type(struct bt_ctf_field_type
*field_type
)
704 return bt_ctf_field_type_get_type_id(field_type
) ==
705 BT_CTF_FIELD_TYPE_ID_STRUCT
;
709 bool is_variant_type(struct bt_ctf_field_type
*field_type
)
711 return bt_ctf_field_type_get_type_id(field_type
) ==
712 BT_CTF_FIELD_TYPE_ID_VARIANT
;
716 struct stream_class_field_path_cache
*
717 create_stream_class_field_path_cache_entry(
718 struct bt_ctf_notif_iter
*notit
,
719 struct bt_ctf_stream_class
*stream_class
)
723 int timestamp_end
= -1;
724 int packet_size
= -1;
725 int content_size
= -1;
726 struct stream_class_field_path_cache
*cache_entry
= g_new0(
727 struct stream_class_field_path_cache
, 1);
728 struct bt_ctf_field_type
*event_header
= NULL
, *packet_context
= NULL
;
731 BT_LOGE_STR("Failed to allocate one stream class field path cache.");
735 event_header
= bt_ctf_stream_class_get_event_header_type(stream_class
);
736 if (event_header
&& bt_ctf_field_type_is_structure(event_header
)) {
739 count
= bt_ctf_field_type_structure_get_field_count(
743 for (i
= 0; i
< count
; i
++) {
747 ret
= bt_ctf_field_type_structure_get_field(
748 event_header
, &name
, NULL
, i
);
750 BT_LOGE("Cannot get event header structure field type's field: "
751 "notit-addr=%p, stream-class-addr=%p, "
752 "stream-class-name=\"%s\", "
753 "stream-class-id=%" PRId64
", "
754 "ft-addr=%p, index=%d",
756 bt_ctf_stream_class_get_name(stream_class
),
757 bt_ctf_stream_class_get_id(stream_class
),
762 if (v
!= -1 && id
!= -1) {
766 if (v
== -1 && strcmp(name
, "v") == 0) {
768 } else if (id
== -1 && !strcmp(name
, "id")) {
774 packet_context
= bt_ctf_stream_class_get_packet_context_type(
776 if (packet_context
&& bt_ctf_field_type_is_structure(packet_context
)) {
779 count
= bt_ctf_field_type_structure_get_field_count(
783 for (i
= 0; i
< count
; i
++) {
786 struct bt_ctf_field_type
*field_type
;
788 if (timestamp_end
!= -1 && packet_size
!= -1 &&
789 content_size
!= -1) {
793 ret
= bt_ctf_field_type_structure_get_field(
794 packet_context
, &name
, &field_type
, i
);
796 BT_LOGE("Cannot get packet context structure field type's field: "
797 "notit-addr=%p, stream-class-addr=%p, "
798 "stream-class-name=\"%s\", "
799 "stream-class-id=%" PRId64
", "
800 "ft-addr=%p, index=%d",
802 bt_ctf_stream_class_get_name(stream_class
),
803 bt_ctf_stream_class_get_id(stream_class
),
808 if (timestamp_end
== -1 &&
809 strcmp(name
, "timestamp_end") == 0) {
810 struct field_cb_override
*override
= g_new0(
811 struct field_cb_override
, 1);
818 override
->func
= btr_timestamp_end_cb
;
819 override
->data
= notit
;
820 g_hash_table_insert(notit
->field_overrides
,
821 bt_get(field_type
), override
);
823 } else if (packet_size
== -1 &&
824 !strcmp(name
, "packet_size")) {
826 } else if (content_size
== -1 &&
827 !strcmp(name
, "content_size")) {
835 cache_entry
->id
= id
;
836 cache_entry
->timestamp_end
= timestamp_end
;
837 cache_entry
->packet_size
= packet_size
;
838 cache_entry
->content_size
= content_size
;
840 BT_PUT(event_header
);
841 BT_PUT(packet_context
);
850 struct stream_class_field_path_cache
*get_stream_class_field_path_cache(
851 struct bt_ctf_notif_iter
*notit
,
852 struct bt_ctf_stream_class
*stream_class
)
854 bool cache_entry_found
;
855 struct stream_class_field_path_cache
*cache_entry
;
857 cache_entry_found
= g_hash_table_lookup_extended(
858 notit
->sc_field_path_caches
,
859 stream_class
, NULL
, (gpointer
) &cache_entry
);
860 if (unlikely(!cache_entry_found
)) {
861 cache_entry
= create_stream_class_field_path_cache_entry(notit
,
863 g_hash_table_insert(notit
->sc_field_path_caches
,
864 bt_get(stream_class
), (gpointer
) cache_entry
);
871 enum bt_ctf_notif_iter_status
set_current_stream_class(
872 struct bt_ctf_notif_iter
*notit
)
874 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
875 struct bt_ctf_field_type
*packet_header_type
= NULL
;
876 struct bt_ctf_field_type
*stream_id_field_type
= NULL
;
879 /* Clear the current stream class field path cache. */
880 notit
->cur_sc_field_path_cache
= NULL
;
882 /* Is there any "stream_id" field in the packet header? */
883 packet_header_type
= bt_ctf_trace_get_packet_header_type(
885 if (!packet_header_type
) {
887 * No packet header, therefore no `stream_id` field,
888 * therefore only one stream class.
890 goto single_stream_class
;
893 assert(is_struct_type(packet_header_type
));
896 stream_id_field_type
=
897 bt_ctf_field_type_structure_get_field_type_by_name(
898 packet_header_type
, "stream_id");
899 if (stream_id_field_type
) {
900 /* Find appropriate stream class using current stream ID */
902 struct bt_ctf_field
*stream_id_field
= NULL
;
904 assert(notit
->dscopes
.trace_packet_header
);
907 stream_id_field
= bt_ctf_field_structure_get_field(
908 notit
->dscopes
.trace_packet_header
, "stream_id");
909 assert(stream_id_field
);
910 ret
= bt_ctf_field_unsigned_integer_get_value(
911 stream_id_field
, &stream_id
);
913 BT_PUT(stream_id_field
);
916 /* Only one stream: pick the first stream class */
917 assert(bt_ctf_trace_get_stream_class_count(
918 notit
->meta
.trace
) == 1);
922 BT_LOGV("Found stream class ID to use: notit-addr=%p, "
923 "stream-class-id=%" PRIu64
", "
924 "trace-addr=%p, trace-name=\"%s\"",
925 notit
, stream_id
, notit
->meta
.trace
,
926 bt_ctf_trace_get_name(notit
->meta
.trace
));
928 BT_PUT(notit
->meta
.stream_class
);
929 notit
->meta
.stream_class
= bt_ctf_trace_get_stream_class_by_id(
930 notit
->meta
.trace
, stream_id
);
931 if (!notit
->meta
.stream_class
) {
932 BT_LOGW("No stream class with ID of stream class ID to use in trace: "
933 "notit-addr=%p, stream-class-id=%" PRIu64
", "
934 "trace-addr=%p, trace-name=\"%s\"",
935 notit
, stream_id
, notit
->meta
.trace
,
936 bt_ctf_trace_get_name(notit
->meta
.trace
));
937 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
941 BT_LOGV("Set current stream class: "
942 "notit-addr=%p, stream-class-addr=%p, "
943 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
944 notit
, notit
->meta
.stream_class
,
945 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
946 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
949 * Retrieve (or lazily create) the current stream class field path
952 notit
->cur_sc_field_path_cache
= get_stream_class_field_path_cache(
953 notit
, notit
->meta
.stream_class
);
954 if (!notit
->cur_sc_field_path_cache
) {
955 BT_LOGW("Cannot retrieve stream class field path from cache: "
956 "notit-addr=%p, stream-class-addr=%p, "
957 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
958 notit
, notit
->meta
.stream_class
,
959 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
960 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
961 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
965 BT_PUT(packet_header_type
);
966 BT_PUT(stream_id_field_type
);
972 enum bt_ctf_notif_iter_status
after_packet_header_state(
973 struct bt_ctf_notif_iter
*notit
)
975 enum bt_ctf_notif_iter_status status
;
977 status
= set_current_stream_class(notit
);
978 if (status
== BT_CTF_NOTIF_ITER_STATUS_OK
) {
979 notit
->state
= STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
;
986 enum bt_ctf_notif_iter_status
read_packet_context_begin_state(
987 struct bt_ctf_notif_iter
*notit
)
989 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
990 struct bt_ctf_field_type
*packet_context_type
;
992 assert(notit
->meta
.stream_class
);
993 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(
994 notit
->meta
.stream_class
);
995 if (!packet_context_type
) {
996 BT_LOGV("No packet packet context field type in stream class: continuing: "
997 "notit-addr=%p, stream-class-addr=%p, "
998 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
999 notit
, notit
->meta
.stream_class
,
1000 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1001 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
1002 notit
->state
= STATE_AFTER_STREAM_PACKET_CONTEXT
;
1006 BT_LOGV("Decoding packet context field: "
1007 "notit-addr=%p, stream-class-addr=%p, "
1008 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1010 notit
, notit
->meta
.stream_class
,
1011 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1012 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1013 packet_context_type
);
1014 status
= read_dscope_begin_state(notit
, packet_context_type
,
1015 STATE_AFTER_STREAM_PACKET_CONTEXT
,
1016 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
1017 ¬it
->dscopes
.stream_packet_context
);
1019 BT_LOGW("Cannot decode packet context field: "
1020 "notit-addr=%p, stream-class-addr=%p, "
1021 "stream-class-name=\"%s\", "
1022 "stream-class-id=%" PRId64
", ft-addr=%p",
1023 notit
, notit
->meta
.stream_class
,
1024 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1025 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1026 packet_context_type
);
1030 BT_PUT(packet_context_type
);
1035 enum bt_ctf_notif_iter_status
read_packet_context_continue_state(
1036 struct bt_ctf_notif_iter
*notit
)
1038 return read_dscope_continue_state(notit
,
1039 STATE_AFTER_STREAM_PACKET_CONTEXT
);
1043 enum bt_ctf_notif_iter_status
set_current_packet_content_sizes(
1044 struct bt_ctf_notif_iter
*notit
)
1046 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1047 struct bt_ctf_field
*packet_size_field
= NULL
;
1048 struct bt_ctf_field
*content_size_field
= NULL
;
1049 uint64_t content_size
= -1, packet_size
= -1;
1051 if (!notit
->dscopes
.stream_packet_context
) {
1055 packet_size_field
= bt_ctf_field_structure_get_field(
1056 notit
->dscopes
.stream_packet_context
, "packet_size");
1057 content_size_field
= bt_ctf_field_structure_get_field(
1058 notit
->dscopes
.stream_packet_context
, "content_size");
1059 if (packet_size_field
) {
1060 int ret
= bt_ctf_field_unsigned_integer_get_value(
1061 packet_size_field
, &packet_size
);
1064 if (packet_size
== 0) {
1065 BT_LOGW("Invalid packet size: packet context field indicates packet size is zero: "
1066 "notit-addr=%p, packet-context-field-addr=%p",
1067 notit
, notit
->dscopes
.stream_packet_context
);
1068 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1070 } else if ((packet_size
% 8) != 0) {
1071 BT_LOGW("Invalid packet size: packet context field indicates packet size is not a multiple of 8: "
1072 "notit-addr=%p, packet-context-field-addr=%p, "
1073 "packet-size=%" PRIu64
,
1074 notit
, notit
->dscopes
.stream_packet_context
,
1076 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1081 if (content_size_field
) {
1082 int ret
= bt_ctf_field_unsigned_integer_get_value(
1083 content_size_field
, &content_size
);
1087 content_size
= packet_size
;
1090 if (content_size
> packet_size
) {
1091 BT_LOGW("Invalid packet or content size: packet context field indicates content size is greater than packet size: "
1092 "notit-addr=%p, packet-context-field-addr=%p, "
1093 "packet-size=%" PRIu64
", content-size=%" PRIu64
,
1094 notit
, notit
->dscopes
.stream_packet_context
,
1095 packet_size
, content_size
);
1096 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1100 notit
->cur_packet_size
= packet_size
;
1101 notit
->cur_content_size
= content_size
;
1102 BT_LOGV("Set current packet and content sizes: "
1103 "notit-addr=%p, packet-size=%" PRIu64
", content-size=%" PRIu64
,
1104 notit
, packet_size
, content_size
);
1106 BT_PUT(packet_size_field
);
1107 BT_PUT(content_size_field
);
1112 enum bt_ctf_notif_iter_status
after_packet_context_state(
1113 struct bt_ctf_notif_iter
*notit
)
1115 enum bt_ctf_notif_iter_status status
;
1117 status
= set_current_packet_content_sizes(notit
);
1118 if (status
== BT_CTF_NOTIF_ITER_STATUS_OK
) {
1119 notit
->state
= STATE_EMIT_NOTIF_NEW_PACKET
;
1126 enum bt_ctf_notif_iter_status
read_event_header_begin_state(
1127 struct bt_ctf_notif_iter
*notit
)
1129 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1130 struct bt_ctf_field_type
*event_header_type
= NULL
;
1132 /* Check if we have some content left */
1133 if (notit
->cur_content_size
>= 0) {
1134 if (packet_at(notit
) == notit
->cur_content_size
) {
1135 /* No more events! */
1136 BT_LOGV("Reached end of packet: notit-addr=%p, "
1137 "cur=%zu", notit
, packet_at(notit
));
1138 notit
->state
= STATE_EMIT_NOTIF_END_OF_PACKET
;
1140 } else if (packet_at(notit
) > notit
->cur_content_size
) {
1141 /* That's not supposed to happen */
1142 BT_LOGV("Before decoding event header field: cursor is passed the packet's content: "
1143 "notit-addr=%p, content-size=%zu, "
1144 "cur=%zu", notit
, notit
->cur_content_size
,
1146 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1151 event_header_type
= bt_ctf_stream_class_get_event_header_type(
1152 notit
->meta
.stream_class
);
1153 if (!event_header_type
) {
1154 notit
->state
= STATE_AFTER_STREAM_EVENT_HEADER
;
1158 put_event_dscopes(notit
);
1159 BT_LOGV("Decoding event header field: "
1160 "notit-addr=%p, stream-class-addr=%p, "
1161 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1163 notit
, notit
->meta
.stream_class
,
1164 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1165 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1167 status
= read_dscope_begin_state(notit
, event_header_type
,
1168 STATE_AFTER_STREAM_EVENT_HEADER
,
1169 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
,
1170 ¬it
->dscopes
.stream_event_header
);
1172 BT_LOGW("Cannot decode event header field: "
1173 "notit-addr=%p, stream-class-addr=%p, "
1174 "stream-class-name=\"%s\", "
1175 "stream-class-id=%" PRId64
", ft-addr=%p",
1176 notit
, notit
->meta
.stream_class
,
1177 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1178 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1182 BT_PUT(event_header_type
);
1188 enum bt_ctf_notif_iter_status
read_event_header_continue_state(
1189 struct bt_ctf_notif_iter
*notit
)
1191 return read_dscope_continue_state(notit
,
1192 STATE_AFTER_STREAM_EVENT_HEADER
);
1196 enum bt_ctf_notif_iter_status
set_current_event_class(struct bt_ctf_notif_iter
*notit
)
1199 * The assert() calls in this function are okay because it is
1200 * assumed here that all the metadata objects have been
1201 * validated for CTF correctness before decoding actual streams.
1204 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1205 struct bt_ctf_field_type
*event_header_type
;
1206 struct bt_ctf_field_type
*id_field_type
= NULL
;
1207 struct bt_ctf_field_type
*v_field_type
= NULL
;
1208 uint64_t event_id
= -1ULL;
1211 event_header_type
= bt_ctf_stream_class_get_event_header_type(
1212 notit
->meta
.stream_class
);
1213 if (!event_header_type
) {
1215 * No event header, therefore no event class ID field,
1216 * therefore only one event class.
1218 goto single_event_class
;
1221 /* Is there any "id"/"v" field in the event header? */
1222 assert(is_struct_type(event_header_type
));
1223 id_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1224 event_header_type
, "id");
1225 v_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1226 event_header_type
, "v");
1227 assert(notit
->dscopes
.stream_event_header
);
1231 * | | |_ _|_ _| __ __ _
1232 * | | | | | || '_ \ / _` |
1233 * | |___| | | || | | | (_| | S P E C I A L
1234 * |_____|_| |_||_| |_|\__, | C A S E â„¢
1237 struct bt_ctf_field
*v_field
= NULL
;
1238 struct bt_ctf_field
*v_struct_field
= NULL
;
1239 struct bt_ctf_field
*v_struct_id_field
= NULL
;
1241 // TODO: optimalize!
1242 v_field
= bt_ctf_field_structure_get_field(
1243 notit
->dscopes
.stream_event_header
, "v");
1247 bt_ctf_field_variant_get_current_field(v_field
);
1248 if (!v_struct_field
) {
1249 goto end_v_field_type
;
1252 // TODO: optimalize!
1254 bt_ctf_field_structure_get_field(v_struct_field
, "id");
1255 if (!v_struct_id_field
) {
1256 goto end_v_field_type
;
1259 if (bt_ctf_field_is_integer(v_struct_id_field
)) {
1260 ret
= bt_ctf_field_unsigned_integer_get_value(
1261 v_struct_id_field
, &event_id
);
1263 BT_LOGV("Cannot get value of unsigned integer field (`id`): continuing: "
1264 "notit=%p, field-addr=%p",
1265 notit
, v_struct_id_field
);
1272 BT_PUT(v_struct_field
);
1273 BT_PUT(v_struct_id_field
);
1276 if (id_field_type
&& event_id
== -1ULL) {
1277 /* Check "id" field */
1278 struct bt_ctf_field
*id_field
= NULL
;
1281 // TODO: optimalize!
1282 id_field
= bt_ctf_field_structure_get_field(
1283 notit
->dscopes
.stream_event_header
, "id");
1285 goto check_event_id
;
1288 if (bt_ctf_field_is_integer(id_field
)) {
1289 ret
= bt_ctf_field_unsigned_integer_get_value(
1290 id_field
, &event_id
);
1291 } else if (bt_ctf_field_is_enumeration(id_field
)) {
1292 struct bt_ctf_field
*container
;
1294 container
= bt_ctf_field_enumeration_get_container(
1297 ret
= bt_ctf_field_unsigned_integer_get_value(
1298 container
, &event_id
);
1307 if (event_id
== -1ULL) {
1309 /* Event ID not found: single event? */
1310 assert(bt_ctf_stream_class_get_event_class_count(
1311 notit
->meta
.stream_class
) == 1);
1315 BT_LOGV("Found event class ID to use: notit-addr=%p, "
1316 "stream-class-addr=%p, stream-class-name=\"%s\", "
1317 "stream-class-id=%" PRId64
", "
1318 "event-class-id=%" PRIu64
,
1319 notit
, notit
->meta
.stream_class
,
1320 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1321 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1323 BT_PUT(notit
->meta
.event_class
);
1324 notit
->meta
.event_class
= bt_ctf_stream_class_get_event_class_by_id(
1325 notit
->meta
.stream_class
, event_id
);
1326 if (!notit
->meta
.event_class
) {
1327 BT_LOGW("No event class with ID of event class ID to use in stream class: "
1328 "notit-addr=%p, stream-class-addr=%p, "
1329 "stream-class-name=\"%s\", "
1330 "stream-class-id=%" PRId64
", "
1331 "event-class-id=%" PRIu64
,
1332 notit
, notit
->meta
.stream_class
,
1333 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1334 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1336 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1340 BT_LOGV("Set current event class: "
1341 "notit-addr=%p, event-class-addr=%p, "
1342 "event-class-name=\"%s\", event-class-id=%" PRId64
,
1343 notit
, notit
->meta
.event_class
,
1344 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
1345 bt_ctf_event_class_get_id(notit
->meta
.event_class
));
1348 BT_PUT(event_header_type
);
1349 BT_PUT(id_field_type
);
1350 BT_PUT(v_field_type
);
1356 enum bt_ctf_notif_iter_status
after_event_header_state(
1357 struct bt_ctf_notif_iter
*notit
)
1359 enum bt_ctf_notif_iter_status status
;
1361 status
= set_current_event_class(notit
);
1362 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
1366 notit
->state
= STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
;
1373 enum bt_ctf_notif_iter_status
read_stream_event_context_begin_state(
1374 struct bt_ctf_notif_iter
*notit
)
1376 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1377 struct bt_ctf_field_type
*stream_event_context_type
;
1379 stream_event_context_type
= bt_ctf_stream_class_get_event_context_type(
1380 notit
->meta
.stream_class
);
1381 if (!stream_event_context_type
) {
1382 notit
->state
= STATE_DSCOPE_EVENT_CONTEXT_BEGIN
;
1386 BT_LOGV("Decoding stream event context field: "
1387 "notit-addr=%p, stream-class-addr=%p, "
1388 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1390 notit
, notit
->meta
.stream_class
,
1391 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1392 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1393 stream_event_context_type
);
1394 status
= read_dscope_begin_state(notit
, stream_event_context_type
,
1395 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
,
1396 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
,
1397 ¬it
->dscopes
.stream_event_context
);
1399 BT_LOGW("Cannot decode stream event context field: "
1400 "notit-addr=%p, stream-class-addr=%p, "
1401 "stream-class-name=\"%s\", "
1402 "stream-class-id=%" PRId64
", ft-addr=%p",
1403 notit
, notit
->meta
.stream_class
,
1404 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1405 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1406 stream_event_context_type
);
1410 BT_PUT(stream_event_context_type
);
1416 enum bt_ctf_notif_iter_status
read_stream_event_context_continue_state(
1417 struct bt_ctf_notif_iter
*notit
)
1419 return read_dscope_continue_state(notit
,
1420 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
);
1424 enum bt_ctf_notif_iter_status
read_event_context_begin_state(
1425 struct bt_ctf_notif_iter
*notit
)
1427 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1428 struct bt_ctf_field_type
*event_context_type
;
1430 event_context_type
= bt_ctf_event_class_get_context_type(
1431 notit
->meta
.event_class
);
1432 if (!event_context_type
) {
1433 notit
->state
= STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
;
1437 BT_LOGV("Decoding event context field: "
1438 "notit-addr=%p, event-class-addr=%p, "
1439 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1441 notit
, notit
->meta
.event_class
,
1442 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
1443 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
1444 event_context_type
);
1445 status
= read_dscope_begin_state(notit
, event_context_type
,
1446 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
1447 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
,
1448 ¬it
->dscopes
.event_context
);
1450 BT_LOGW("Cannot decode event context field: "
1451 "notit-addr=%p, event-class-addr=%p, "
1452 "event-class-name=\"%s\", "
1453 "event-class-id=%" PRId64
", ft-addr=%p",
1454 notit
, notit
->meta
.event_class
,
1455 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
1456 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
1457 event_context_type
);
1461 BT_PUT(event_context_type
);
1467 enum bt_ctf_notif_iter_status
read_event_context_continue_state(
1468 struct bt_ctf_notif_iter
*notit
)
1470 return read_dscope_continue_state(notit
,
1471 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
);
1475 enum bt_ctf_notif_iter_status
read_event_payload_begin_state(
1476 struct bt_ctf_notif_iter
*notit
)
1478 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1479 struct bt_ctf_field_type
*event_payload_type
;
1481 event_payload_type
= bt_ctf_event_class_get_payload_type(
1482 notit
->meta
.event_class
);
1483 if (!event_payload_type
) {
1484 notit
->state
= STATE_EMIT_NOTIF_EVENT
;
1488 BT_LOGV("Decoding event payload field: "
1489 "notit-addr=%p, event-class-addr=%p, "
1490 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1492 notit
, notit
->meta
.event_class
,
1493 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
1494 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
1495 event_payload_type
);
1496 status
= read_dscope_begin_state(notit
, event_payload_type
,
1497 STATE_EMIT_NOTIF_EVENT
,
1498 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
1499 ¬it
->dscopes
.event_payload
);
1501 BT_LOGW("Cannot decode event payload field: "
1502 "notit-addr=%p, event-class-addr=%p, "
1503 "event-class-name=\"%s\", "
1504 "event-class-id=%" PRId64
", ft-addr=%p",
1505 notit
, notit
->meta
.event_class
,
1506 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
1507 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
1508 event_payload_type
);
1512 BT_PUT(event_payload_type
);
1518 enum bt_ctf_notif_iter_status
read_event_payload_continue_state(
1519 struct bt_ctf_notif_iter
*notit
)
1521 return read_dscope_continue_state(notit
, STATE_EMIT_NOTIF_EVENT
);
1525 enum bt_ctf_notif_iter_status
skip_packet_padding_state(
1526 struct bt_ctf_notif_iter
*notit
)
1528 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1529 size_t bits_to_skip
;
1531 assert(notit
->cur_packet_size
> 0);
1532 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1533 if (bits_to_skip
== 0) {
1534 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1537 size_t bits_to_consume
;
1539 BT_LOGV("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu",
1540 bits_to_skip
, notit
, bits_to_skip
);
1541 status
= buf_ensure_available_bits(notit
);
1542 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
1546 bits_to_consume
= MIN(buf_available_bits(notit
), bits_to_skip
);
1547 BT_LOGV("Skipping %zu bits of padding: notit-addr=%p, size=%zu",
1548 bits_to_consume
, notit
, bits_to_consume
);
1549 buf_consume_bits(notit
, bits_to_consume
);
1550 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1551 if (bits_to_skip
== 0) {
1552 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1562 enum bt_ctf_notif_iter_status
handle_state(struct bt_ctf_notif_iter
*notit
)
1564 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1565 const enum state state
= notit
->state
;
1567 BT_LOGV("Handling state: notit-addr=%p, state=%s",
1568 notit
, state_string(state
));
1570 // TODO: optimalize!
1573 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1575 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
1576 status
= read_packet_header_begin_state(notit
);
1578 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
1579 status
= read_packet_header_continue_state(notit
);
1581 case STATE_AFTER_TRACE_PACKET_HEADER
:
1582 status
= after_packet_header_state(notit
);
1584 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
1585 status
= read_packet_context_begin_state(notit
);
1587 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
1588 status
= read_packet_context_continue_state(notit
);
1590 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
1591 status
= after_packet_context_state(notit
);
1593 case STATE_EMIT_NOTIF_NEW_PACKET
:
1594 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1596 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
:
1597 status
= read_event_header_begin_state(notit
);
1599 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
:
1600 status
= read_event_header_continue_state(notit
);
1602 case STATE_AFTER_STREAM_EVENT_HEADER
:
1603 status
= after_event_header_state(notit
);
1605 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
:
1606 status
= read_stream_event_context_begin_state(notit
);
1608 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
:
1609 status
= read_stream_event_context_continue_state(notit
);
1611 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN
:
1612 status
= read_event_context_begin_state(notit
);
1614 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
:
1615 status
= read_event_context_continue_state(notit
);
1617 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
1618 status
= read_event_payload_begin_state(notit
);
1620 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
1621 status
= read_event_payload_continue_state(notit
);
1623 case STATE_EMIT_NOTIF_EVENT
:
1624 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1626 case STATE_SKIP_PACKET_PADDING
:
1627 status
= skip_packet_padding_state(notit
);
1629 case STATE_EMIT_NOTIF_END_OF_PACKET
:
1630 notit
->state
= STATE_SKIP_PACKET_PADDING
;
1633 BT_LOGD("Unknown CTF plugin notification iterator state: "
1634 "notit-addr=%p, state=%d", notit
, notit
->state
);
1638 BT_LOGV("Handled state: notit-addr=%p, status=%s, "
1639 "prev-state=%s, cur-state=%s",
1640 notit
, bt_ctf_notif_iter_status_string(status
),
1641 state_string(state
), state_string(notit
->state
));
1646 * Resets the internal state of a CTF notification iterator.
1649 void bt_ctf_notif_iter_reset(struct bt_ctf_notif_iter
*notit
)
1652 BT_LOGD("Resetting notification iterator: addr=%p", notit
);
1653 stack_clear(notit
->stack
);
1654 BT_PUT(notit
->meta
.stream_class
);
1655 BT_PUT(notit
->meta
.event_class
);
1656 BT_PUT(notit
->packet
);
1657 put_all_dscopes(notit
);
1658 notit
->buf
.addr
= NULL
;
1661 notit
->buf
.packet_offset
= 0;
1662 notit
->state
= STATE_INIT
;
1663 notit
->cur_content_size
= -1;
1664 notit
->cur_packet_size
= -1;
1668 int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter
*notit
)
1673 BT_LOGV("Switching packet: notit-addr=%p, cur=%zu",
1674 notit
, notit
->buf
.at
);
1675 stack_clear(notit
->stack
);
1676 BT_PUT(notit
->meta
.stream_class
);
1677 BT_PUT(notit
->meta
.event_class
);
1678 BT_PUT(notit
->packet
);
1679 BT_PUT(notit
->cur_timestamp_end
);
1680 put_all_dscopes(notit
);
1683 * Adjust current buffer so that addr points to the beginning of the new
1686 if (notit
->buf
.addr
) {
1687 size_t consumed_bytes
= (size_t) (notit
->buf
.at
/ CHAR_BIT
);
1689 /* Packets are assumed to start on a byte frontier. */
1690 if (notit
->buf
.at
% CHAR_BIT
) {
1691 BT_LOGW("Cannot switch packet: current position is not a multiple of 8: "
1692 "notit-addr=%p, cur=%zu", notit
, notit
->buf
.at
);
1697 notit
->buf
.addr
+= consumed_bytes
;
1698 notit
->buf
.sz
-= consumed_bytes
;
1700 notit
->buf
.packet_offset
= 0;
1701 BT_LOGV("Adjusted buffer: addr=%p, size=%zu",
1702 notit
->buf
.addr
, notit
->buf
.sz
);
1705 notit
->cur_content_size
= -1;
1706 notit
->cur_packet_size
= -1;
1707 notit
->cur_sc_field_path_cache
= NULL
;
1714 struct bt_ctf_field
*get_next_field(struct bt_ctf_notif_iter
*notit
)
1716 struct bt_ctf_field
*next_field
= NULL
;
1717 struct bt_ctf_field
*base_field
;
1718 struct bt_ctf_field_type
*base_type
;
1721 assert(!stack_empty(notit
->stack
));
1722 index
= stack_top(notit
->stack
)->index
;
1723 base_field
= stack_top(notit
->stack
)->base
;
1725 base_type
= bt_ctf_field_get_type(base_field
);
1728 switch (bt_ctf_field_type_get_type_id(base_type
)) {
1729 case BT_CTF_FIELD_TYPE_ID_STRUCT
:
1730 next_field
= bt_ctf_field_structure_get_field_by_index(
1733 case BT_CTF_FIELD_TYPE_ID_ARRAY
:
1734 next_field
= bt_ctf_field_array_get_field(base_field
, index
);
1736 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
1737 next_field
= bt_ctf_field_sequence_get_field(base_field
, index
);
1739 case BT_CTF_FIELD_TYPE_ID_VARIANT
:
1740 next_field
= bt_ctf_field_variant_get_current_field(base_field
);
1743 BT_LOGF("Unknown base field type ID: "
1744 "notit-addr=%p, ft-addr=%p, ft-id=%s",
1746 bt_ctf_field_type_id_string(
1747 bt_ctf_field_type_get_type_id(base_type
)));
1756 void update_clock_state(uint64_t *state
,
1757 struct bt_ctf_field
*value_field
)
1759 struct bt_ctf_field_type
*value_type
= NULL
;
1760 uint64_t requested_new_value
;
1761 uint64_t requested_new_value_mask
;
1762 uint64_t cur_value_masked
;
1763 int requested_new_value_size
;
1766 value_type
= bt_ctf_field_get_type(value_field
);
1768 assert(bt_ctf_field_type_is_integer(value_type
));
1769 requested_new_value_size
=
1770 bt_ctf_field_type_integer_get_size(value_type
);
1771 assert(requested_new_value_size
> 0);
1772 ret
= bt_ctf_field_unsigned_integer_get_value(value_field
,
1773 &requested_new_value
);
1777 * Special case for a 64-bit new value, which is the limit
1778 * of a clock value as of this version: overwrite the
1779 * current value directly.
1781 if (requested_new_value_size
== 64) {
1782 *state
= requested_new_value
;
1786 requested_new_value_mask
= (1ULL << requested_new_value_size
) - 1;
1787 cur_value_masked
= *state
& requested_new_value_mask
;
1789 if (requested_new_value
< cur_value_masked
) {
1791 * It looks like a wrap happened on the number of bits
1792 * of the requested new value. Assume that the clock
1793 * value wrapped only one time.
1795 *state
+= requested_new_value_mask
+ 1;
1798 /* Clear the low bits of the current clock value. */
1799 *state
&= ~requested_new_value_mask
;
1801 /* Set the low bits of the current clock value. */
1802 *state
|= requested_new_value
;
1804 BT_LOGV("Updated clock's value from integer field's value: "
1805 "value=%" PRIu64
, *state
);
1810 enum bt_ctf_btr_status
update_clock(struct bt_ctf_notif_iter
*notit
,
1811 struct bt_ctf_field
*int_field
)
1813 gboolean clock_class_found
;
1814 uint64_t *clock_state
= NULL
;
1815 struct bt_ctf_field_type
*int_field_type
= NULL
;
1816 enum bt_ctf_btr_status ret
= BT_CTF_BTR_STATUS_OK
;
1817 struct bt_ctf_clock_class
*clock_class
= NULL
;
1819 int_field_type
= bt_ctf_field_get_type(int_field
);
1820 assert(int_field_type
);
1821 clock_class
= bt_ctf_field_type_integer_get_mapped_clock_class(
1823 if (likely(!clock_class
)) {
1827 clock_class_found
= g_hash_table_lookup_extended(notit
->clock_states
,
1828 clock_class
, NULL
, (gpointer
) &clock_state
);
1829 if (!clock_class_found
) {
1830 clock_state
= g_new0(uint64_t, 1);
1832 BT_LOGE_STR("Failed to allocate a uint64_t.");
1833 ret
= BT_CTF_BTR_STATUS_ENOMEM
;
1837 g_hash_table_insert(notit
->clock_states
, bt_get(clock_class
),
1841 /* Update the clock's state. */
1842 BT_LOGV("Updating notification iterator's clock's value from integer field: "
1843 "notit-addr=%p, clock-class-addr=%p, "
1844 "clock-class-name=\"%s\", value=%" PRIu64
,
1846 bt_ctf_clock_class_get_name(clock_class
), *clock_state
);
1847 update_clock_state(clock_state
, int_field
);
1849 bt_put(int_field_type
);
1850 bt_put(clock_class
);
1855 enum bt_ctf_btr_status
btr_unsigned_int_common(uint64_t value
,
1856 struct bt_ctf_field_type
*type
, void *data
,
1857 struct bt_ctf_field
**out_int_field
)
1859 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1860 struct bt_ctf_field
*field
= NULL
;
1861 struct bt_ctf_field
*int_field
= NULL
;
1862 struct bt_ctf_notif_iter
*notit
= data
;
1865 BT_LOGV("Common unsigned integer function called from BTR: "
1866 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
1867 "ft-id=%s, value=%" PRIu64
,
1868 notit
, notit
->btr
, type
,
1869 bt_ctf_field_type_id_string(
1870 bt_ctf_field_type_get_type_id(type
)),
1873 /* Create next field */
1874 field
= get_next_field(notit
);
1876 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
1877 status
= BT_CTF_BTR_STATUS_ERROR
;
1881 switch(bt_ctf_field_type_get_type_id(type
)) {
1882 case BT_CTF_FIELD_TYPE_ID_INTEGER
:
1883 /* Integer field is created field */
1884 BT_MOVE(int_field
, field
);
1887 case BT_CTF_FIELD_TYPE_ID_ENUM
:
1888 int_field
= bt_ctf_field_enumeration_get_container(field
);
1890 type
= bt_ctf_field_get_type(int_field
);
1894 BT_LOGF("Unexpected field type ID: "
1895 "notit-addr=%p, ft-addr=%p, ft-id=%s",
1897 bt_ctf_field_type_id_string(
1898 bt_ctf_field_type_get_type_id(type
)));
1903 ret
= bt_ctf_field_unsigned_integer_set_value(int_field
, value
);
1905 stack_top(notit
->stack
)->index
++;
1906 *out_int_field
= int_field
;
1915 enum bt_ctf_btr_status
btr_timestamp_end_cb(void *value
,
1916 struct bt_ctf_field_type
*type
, void *data
)
1918 enum bt_ctf_btr_status status
;
1919 struct bt_ctf_field
*field
= NULL
;
1920 struct bt_ctf_notif_iter
*notit
= data
;
1922 BT_LOGV("`timestamp_end` unsigned integer function called from BTR: "
1923 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
1925 notit
, notit
->btr
, type
,
1926 bt_ctf_field_type_id_string(
1927 bt_ctf_field_type_get_type_id(type
)));
1928 status
= btr_unsigned_int_common(*((uint64_t *) value
), type
, data
,
1931 /* Set as the current packet's timestamp_end field. */
1932 BT_MOVE(notit
->cur_timestamp_end
, field
);
1937 enum bt_ctf_btr_status
btr_unsigned_int_cb(uint64_t value
,
1938 struct bt_ctf_field_type
*type
, void *data
)
1940 struct bt_ctf_notif_iter
*notit
= data
;
1941 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1942 struct bt_ctf_field
*field
= NULL
;
1943 struct field_cb_override
*override
;
1945 BT_LOGV("Unsigned integer function called from BTR: "
1946 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
1947 "ft-id=%s, value=%" PRIu64
,
1948 notit
, notit
->btr
, type
,
1949 bt_ctf_field_type_id_string(
1950 bt_ctf_field_type_get_type_id(type
)),
1952 override
= g_hash_table_lookup(notit
->field_overrides
, type
);
1953 if (unlikely(override
)) {
1954 /* Override function logs errors */
1955 status
= override
->func(&value
, type
, override
->data
);
1959 status
= btr_unsigned_int_common(value
, type
, data
, &field
);
1960 if (status
!= BT_CTF_BTR_STATUS_OK
) {
1961 /* btr_unsigned_int_common() logs errors */
1965 status
= update_clock(notit
, field
);
1972 enum bt_ctf_btr_status
btr_signed_int_cb(int64_t value
,
1973 struct bt_ctf_field_type
*type
, void *data
)
1975 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1976 struct bt_ctf_field
*field
= NULL
;
1977 struct bt_ctf_field
*int_field
= NULL
;
1978 struct bt_ctf_notif_iter
*notit
= data
;
1981 BT_LOGV("Signed integer function called from BTR: "
1982 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
1983 "ft-id=%s, value=%" PRId64
,
1984 notit
, notit
->btr
, type
,
1985 bt_ctf_field_type_id_string(
1986 bt_ctf_field_type_get_type_id(type
)),
1989 /* create next field */
1990 field
= get_next_field(notit
);
1992 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
1993 status
= BT_CTF_BTR_STATUS_ERROR
;
1997 switch(bt_ctf_field_type_get_type_id(type
)) {
1998 case BT_CTF_FIELD_TYPE_ID_INTEGER
:
1999 /* Integer field is created field */
2000 BT_MOVE(int_field
, field
);
2003 case BT_CTF_FIELD_TYPE_ID_ENUM
:
2004 int_field
= bt_ctf_field_enumeration_get_container(field
);
2006 type
= bt_ctf_field_get_type(int_field
);
2010 BT_LOGF("Unexpected field type ID: "
2011 "notit-addr=%p, ft-addr=%p, ft-id=%s",
2013 bt_ctf_field_type_id_string(
2014 bt_ctf_field_type_get_type_id(type
)));
2019 ret
= bt_ctf_field_signed_integer_set_value(int_field
, value
);
2021 stack_top(notit
->stack
)->index
++;
2022 status
= update_clock(notit
, int_field
);
2032 enum bt_ctf_btr_status
btr_floating_point_cb(double value
,
2033 struct bt_ctf_field_type
*type
, void *data
)
2035 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2036 struct bt_ctf_field
*field
= NULL
;
2037 struct bt_ctf_notif_iter
*notit
= data
;
2040 BT_LOGV("Floating point number function called from BTR: "
2041 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2042 "ft-id=%s, value=%f",
2043 notit
, notit
->btr
, type
,
2044 bt_ctf_field_type_id_string(
2045 bt_ctf_field_type_get_type_id(type
)),
2048 /* Create next field */
2049 field
= get_next_field(notit
);
2051 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2052 status
= BT_CTF_BTR_STATUS_ERROR
;
2056 ret
= bt_ctf_field_floating_point_set_value(field
, value
);
2058 stack_top(notit
->stack
)->index
++;
2066 enum bt_ctf_btr_status
btr_string_begin_cb(
2067 struct bt_ctf_field_type
*type
, void *data
)
2069 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2070 struct bt_ctf_field
*field
= NULL
;
2071 struct bt_ctf_notif_iter
*notit
= data
;
2074 BT_LOGV("String (beginning) function called from BTR: "
2075 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2077 notit
, notit
->btr
, type
,
2078 bt_ctf_field_type_id_string(
2079 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
;
2090 * Push on stack. Not a compound type per se, but we know that only
2091 * btr_string_cb() may be called between this call and a subsequent
2092 * call to btr_string_end_cb().
2094 ret
= stack_push(notit
->stack
, field
);
2096 BT_LOGE("Cannot push string field on stack: "
2097 "notit-addr=%p, field-addr=%p", notit
, field
);
2098 status
= BT_CTF_BTR_STATUS_ERROR
;
2103 * Initialize string field payload to an empty string since in the
2104 * case of a length 0 string the btr_string_cb won't be called and
2105 * we will end up with an unset string payload.
2107 ret
= bt_ctf_field_string_set_value(field
, "");
2109 BT_LOGE("Cannot initialize string field's value to an empty string: "
2110 "notit-addr=%p, field-addr=%p, ret=%d",
2112 status
= BT_CTF_BTR_STATUS_ERROR
;
2123 enum bt_ctf_btr_status
btr_string_cb(const char *value
,
2124 size_t len
, struct bt_ctf_field_type
*type
, void *data
)
2126 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2127 struct bt_ctf_field
*field
= NULL
;
2128 struct bt_ctf_notif_iter
*notit
= data
;
2131 BT_LOGV("String (substring) function called from BTR: "
2132 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2133 "ft-id=%s, string-length=%zu",
2134 notit
, notit
->btr
, type
,
2135 bt_ctf_field_type_id_string(
2136 bt_ctf_field_type_get_type_id(type
)),
2139 /* Get string field */
2140 field
= stack_top(notit
->stack
)->base
;
2143 /* Append current string */
2144 ret
= bt_ctf_field_string_append_len(field
, value
, len
);
2146 BT_LOGE("Cannot append substring to string field's value: "
2147 "notit-addr=%p, field-addr=%p, string-length=%zu, "
2148 "ret=%d", notit
, field
, len
, ret
);
2149 status
= BT_CTF_BTR_STATUS_ERROR
;
2158 enum bt_ctf_btr_status
btr_string_end_cb(
2159 struct bt_ctf_field_type
*type
, void *data
)
2161 struct bt_ctf_notif_iter
*notit
= data
;
2163 BT_LOGV("String (end) function called from BTR: "
2164 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2166 notit
, notit
->btr
, type
,
2167 bt_ctf_field_type_id_string(
2168 bt_ctf_field_type_get_type_id(type
)));
2170 /* Pop string field */
2171 stack_pop(notit
->stack
);
2173 /* Go to next field */
2174 stack_top(notit
->stack
)->index
++;
2175 return BT_CTF_BTR_STATUS_OK
;
2178 enum bt_ctf_btr_status
btr_compound_begin_cb(
2179 struct bt_ctf_field_type
*type
, void *data
)
2181 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2182 struct bt_ctf_notif_iter
*notit
= data
;
2183 struct bt_ctf_field
*field
;
2186 BT_LOGV("Compound (beginning) function called from BTR: "
2187 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2189 notit
, notit
->btr
, type
,
2190 bt_ctf_field_type_id_string(
2191 bt_ctf_field_type_get_type_id(type
)));
2194 if (stack_empty(notit
->stack
)) {
2195 /* Root: create dynamic scope field */
2196 *notit
->cur_dscope_field
= bt_ctf_field_create(type
);
2197 field
= *notit
->cur_dscope_field
;
2200 * Field will be put at the end of this function
2201 * (stack_push() will take one reference, but this
2202 * reference is lost upon the equivalent stack_pop()
2203 * later), so also get it for our context to own it.
2205 bt_get(*notit
->cur_dscope_field
);
2208 BT_LOGE("Cannot create compound field: "
2209 "notit-addr=%p, ft-addr=%p, ft-id=%s",
2211 bt_ctf_field_type_id_string(
2212 bt_ctf_field_type_get_type_id(type
)));
2213 status
= BT_CTF_BTR_STATUS_ERROR
;
2217 field
= get_next_field(notit
);
2219 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2220 status
= BT_CTF_BTR_STATUS_ERROR
;
2227 ret
= stack_push(notit
->stack
, field
);
2229 BT_LOGE("Cannot push compound field onto the stack: "
2230 "notit-addr=%p, ft-addr=%p, ft-id=%s, ret=%d",
2232 bt_ctf_field_type_id_string(
2233 bt_ctf_field_type_get_type_id(type
)),
2235 status
= BT_CTF_BTR_STATUS_ERROR
;
2245 enum bt_ctf_btr_status
btr_compound_end_cb(
2246 struct bt_ctf_field_type
*type
, void *data
)
2248 struct bt_ctf_notif_iter
*notit
= data
;
2250 BT_LOGV("Compound (end) function called from BTR: "
2251 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2253 notit
, notit
->btr
, type
,
2254 bt_ctf_field_type_id_string(
2255 bt_ctf_field_type_get_type_id(type
)));
2256 assert(!stack_empty(notit
->stack
));
2259 stack_pop(notit
->stack
);
2261 /* If the stack is not empty, increment the base's index */
2262 if (!stack_empty(notit
->stack
)) {
2263 stack_top(notit
->stack
)->index
++;
2266 return BT_CTF_BTR_STATUS_OK
;
2270 struct bt_ctf_field
*resolve_field(struct bt_ctf_notif_iter
*notit
,
2271 struct bt_ctf_field_path
*path
)
2273 struct bt_ctf_field
*field
= NULL
;
2276 if (BT_LOG_ON_VERBOSE
) {
2277 GString
*gstr
= bt_ctf_field_path_string(path
);
2279 BT_LOGV("Resolving field path: notit-addr=%p, field-path=\"%s\"",
2280 notit
, gstr
? gstr
->str
: NULL
);
2283 g_string_free(gstr
, TRUE
);
2287 switch (bt_ctf_field_path_get_root_scope(path
)) {
2288 case BT_CTF_SCOPE_TRACE_PACKET_HEADER
:
2289 field
= notit
->dscopes
.trace_packet_header
;
2291 case BT_CTF_SCOPE_STREAM_PACKET_CONTEXT
:
2292 field
= notit
->dscopes
.stream_packet_context
;
2294 case BT_CTF_SCOPE_STREAM_EVENT_HEADER
:
2295 field
= notit
->dscopes
.stream_event_header
;
2297 case BT_CTF_SCOPE_STREAM_EVENT_CONTEXT
:
2298 field
= notit
->dscopes
.stream_event_context
;
2300 case BT_CTF_SCOPE_EVENT_CONTEXT
:
2301 field
= notit
->dscopes
.event_context
;
2303 case BT_CTF_SCOPE_EVENT_FIELDS
:
2304 field
= notit
->dscopes
.event_payload
;
2307 BT_LOGF("Cannot resolve field path: unknown scope: "
2308 "notit-addr=%p, root-scope=%s",
2309 notit
, bt_ctf_scope_string(
2310 bt_ctf_field_path_get_root_scope(path
)));
2315 BT_LOGW("Cannot resolve field path: root field not found: "
2316 "notit-addr=%p, root-scope=%s",
2317 notit
, bt_ctf_scope_string(
2318 bt_ctf_field_path_get_root_scope(path
)));
2324 for (i
= 0; i
< bt_ctf_field_path_get_index_count(path
); ++i
) {
2325 struct bt_ctf_field
*next_field
= NULL
;
2326 struct bt_ctf_field_type
*field_type
;
2327 int index
= bt_ctf_field_path_get_index(path
, i
);
2329 field_type
= bt_ctf_field_get_type(field
);
2332 if (is_struct_type(field_type
)) {
2333 next_field
= bt_ctf_field_structure_get_field_by_index(
2335 } else if (is_variant_type(field_type
)) {
2337 bt_ctf_field_variant_get_current_field(field
);
2344 BT_LOGW("Cannot find next field: "
2345 "notit-addr=%p, ft-addr=%p, ft-id=%s, index=%d",
2347 bt_ctf_field_type_id_string(
2348 bt_ctf_field_type_get_type_id(field_type
)),
2353 /* Move next field -> field */
2354 BT_MOVE(field
, next_field
);
2362 int64_t btr_get_sequence_length_cb(struct bt_ctf_field_type
*type
, void *data
)
2366 struct bt_ctf_field
*seq_field
;
2367 struct bt_ctf_field_path
*field_path
;
2368 struct bt_ctf_notif_iter
*notit
= data
;
2369 struct bt_ctf_field
*length_field
= NULL
;
2372 field_path
= bt_ctf_field_type_sequence_get_length_field_path(type
);
2374 length_field
= resolve_field(notit
, field_path
);
2375 if (!length_field
) {
2376 BT_LOGW("Cannot resolve sequence field type's length field path: "
2377 "notit-addr=%p, ft-addr=%p",
2382 iret
= bt_ctf_field_unsigned_integer_get_value(length_field
, &length
);
2384 BT_LOGE("Cannot get value of sequence length field: "
2385 "notit-addr=%p, field-addr=%p",
2386 notit
, length_field
);
2390 seq_field
= stack_top(notit
->stack
)->base
;
2391 iret
= bt_ctf_field_sequence_set_length(seq_field
, length_field
);
2393 BT_LOGE("Cannot set sequence field's length field: "
2394 "notit-addr=%p, seq-field-addr=%p, "
2395 "length-field-addr=%p, ",
2396 notit
, seq_field
, length_field
);
2400 ret
= (int64_t) length
;
2403 BT_PUT(length_field
);
2410 struct bt_ctf_field_type
*btr_get_variant_type_cb(
2411 struct bt_ctf_field_type
*type
, void *data
)
2413 struct bt_ctf_field_path
*path
;
2414 struct bt_ctf_notif_iter
*notit
= data
;
2415 struct bt_ctf_field
*var_field
;
2416 struct bt_ctf_field
*tag_field
= NULL
;
2417 struct bt_ctf_field
*selected_field
= NULL
;
2418 struct bt_ctf_field_type
*selected_field_type
= NULL
;
2420 path
= bt_ctf_field_type_variant_get_tag_field_path(type
);
2422 tag_field
= resolve_field(notit
, path
);
2424 BT_LOGW("Cannot resolve variant field type's tag field path: "
2425 "notit-addr=%p, ft-addr=%p",
2431 * We found the enumeration tag field instance which should be
2432 * able to select a current field for this variant. This
2433 * callback function we're in is called _after_
2434 * compound_begin(), so the current stack top's base field is
2435 * the variant field in question. We get the selected field here
2436 * thanks to this tag field (thus creating the selected field),
2437 * which will also provide us with its type. Then, this field
2438 * will remain the current selected one until the next callback
2439 * function call which is used to fill the current selected
2442 var_field
= stack_top(notit
->stack
)->base
;
2443 selected_field
= bt_ctf_field_variant_get_field(var_field
, tag_field
);
2444 if (!selected_field
) {
2445 BT_LOGW("Cannot get variant field's selection using tag field: "
2446 "notit-addr=%p, var-field-addr=%p, tag-field-addr=%p",
2447 notit
, var_field
, tag_field
);
2451 selected_field_type
= bt_ctf_field_get_type(selected_field
);
2455 BT_PUT(selected_field
);
2458 return selected_field_type
;
2462 int set_event_clocks(struct bt_ctf_event
*event
,
2463 struct bt_ctf_notif_iter
*notit
)
2466 GHashTableIter iter
;
2467 struct bt_ctf_clock_class
*clock_class
;
2468 uint64_t *clock_state
;
2470 g_hash_table_iter_init(&iter
, notit
->clock_states
);
2472 while (g_hash_table_iter_next(&iter
, (gpointer
) &clock_class
,
2473 (gpointer
) &clock_state
)) {
2474 struct bt_ctf_clock_value
*clock_value
;
2476 clock_value
= bt_ctf_clock_value_create(clock_class
,
2479 BT_LOGE("Cannot create clock value from clock class: "
2480 "notit-addr=%p, clock-class-addr=%p, "
2481 "clock-class-name=\"%s\"",
2483 bt_ctf_clock_class_get_name(clock_class
));
2487 ret
= bt_ctf_event_set_clock_value(event
, clock_value
);
2488 bt_put(clock_value
);
2490 struct bt_ctf_event_class
*event_class
=
2491 bt_ctf_event_get_class(event
);
2493 assert(event_class
);
2494 BT_LOGE("Cannot set event's clock value: "
2495 "notit-addr=%p, event-addr=%p, "
2496 "event-class-name=\"%s\", "
2497 "event-class-id=%" PRId64
", "
2498 "clock-class-addr=%p, "
2499 "clock-class-name=\"%s\", "
2500 "clock-value-addr=%p",
2502 bt_ctf_event_class_get_name(event_class
),
2503 bt_ctf_event_class_get_id(event_class
),
2505 bt_ctf_clock_class_get_name(clock_class
),
2507 bt_put(event_class
);
2518 struct bt_ctf_event
*create_event(struct bt_ctf_notif_iter
*notit
)
2521 struct bt_ctf_event
*event
;
2523 BT_LOGV("Creating event for event notification: "
2524 "notit-addr=%p, event-class-addr=%p, "
2525 "event-class-name=\"%s\", "
2526 "event-class-id=%" PRId64
,
2527 notit
, notit
->meta
.event_class
,
2528 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2529 bt_ctf_event_class_get_id(notit
->meta
.event_class
));
2531 /* Create event object. */
2532 event
= bt_ctf_event_create(notit
->meta
.event_class
);
2534 BT_LOGE("Cannot create event: "
2535 "notit-addr=%p, event-class-addr=%p, "
2536 "event-class-name=\"%s\", "
2537 "event-class-id=%" PRId64
,
2538 notit
, notit
->meta
.event_class
,
2539 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2540 bt_ctf_event_class_get_id(notit
->meta
.event_class
));
2544 /* Set header, stream event context, context, and payload fields. */
2545 ret
= bt_ctf_event_set_header(event
,
2546 notit
->dscopes
.stream_event_header
);
2548 BT_LOGE("Cannot set event's header field: "
2549 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2550 "event-class-name=\"%s\", "
2551 "event-class-id=%" PRId64
", field-addr=%p",
2552 notit
, event
, notit
->meta
.event_class
,
2553 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2554 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
2555 notit
->dscopes
.stream_event_header
);
2559 ret
= bt_ctf_event_set_stream_event_context(event
,
2560 notit
->dscopes
.stream_event_context
);
2562 BT_LOGE("Cannot set event's context field: "
2563 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2564 "event-class-name=\"%s\", "
2565 "event-class-id=%" PRId64
", field-addr=%p",
2566 notit
, event
, notit
->meta
.event_class
,
2567 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2568 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
2569 notit
->dscopes
.stream_event_context
);
2573 ret
= bt_ctf_event_set_event_context(event
,
2574 notit
->dscopes
.event_context
);
2576 BT_LOGE("Cannot set event's stream event context field: "
2577 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2578 "event-class-name=\"%s\", "
2579 "event-class-id=%" PRId64
", field-addr=%p",
2580 notit
, event
, notit
->meta
.event_class
,
2581 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2582 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
2583 notit
->dscopes
.event_context
);
2587 ret
= bt_ctf_event_set_event_payload(event
,
2588 notit
->dscopes
.event_payload
);
2590 BT_LOGE("Cannot set event's payload field: "
2591 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2592 "event-class-name=\"%s\", "
2593 "event-class-id=%" PRId64
", field-addr=%p",
2594 notit
, event
, notit
->meta
.event_class
,
2595 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2596 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
2597 notit
->dscopes
.event_payload
);
2601 ret
= set_event_clocks(event
, notit
);
2603 BT_LOGE("Cannot set event's clock values: "
2604 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2605 "event-class-name=\"%s\", "
2606 "event-class-id=%" PRId64
,
2607 notit
, event
, notit
->meta
.event_class
,
2608 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2609 bt_ctf_event_class_get_id(notit
->meta
.event_class
));
2613 /* Associate with current packet. */
2614 assert(notit
->packet
);
2615 ret
= bt_ctf_event_set_packet(event
, notit
->packet
);
2617 BT_LOGE("Cannot set event's header field: "
2618 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2619 "event-class-name=\"%s\", "
2620 "event-class-id=%" PRId64
", packet-addr=%p",
2621 notit
, event
, notit
->meta
.event_class
,
2622 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2623 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
2638 void create_packet(struct bt_ctf_notif_iter
*notit
)
2641 struct bt_ctf_stream
*stream
= NULL
;
2642 struct bt_ctf_packet
*packet
= NULL
;
2644 BT_LOGV("Creating packet for packet notification: "
2645 "notit-addr=%p", notit
);
2647 /* Ask the user for the stream */
2648 BT_LOGV("Calling user function (get stream): notit-addr=%p, "
2649 "stream-class-addr=%p, stream-class-name=\"%s\", "
2650 "stream-class-id=%" PRId64
,
2651 notit
, notit
->meta
.stream_class
,
2652 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
2653 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
2654 stream
= notit
->medium
.medops
.get_stream(notit
->meta
.stream_class
,
2655 notit
->medium
.data
);
2656 BT_LOGV("User function returned: stream-addr=%p",
2659 BT_LOGW_STR("User function failed to return a stream object for the given stream class.");
2663 BT_LOGV("Creating packet from stream: "
2664 "notit-addr=%p, stream-addr=%p, "
2665 "stream-class-addr=%p, "
2666 "stream-class-name=\"%s\", "
2667 "stream-class-id=%" PRId64
,
2668 notit
, stream
, notit
->meta
.stream_class
,
2669 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
2670 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
2673 packet
= bt_ctf_packet_create(stream
);
2675 BT_LOGE("Cannot create packet from stream: "
2676 "notit-addr=%p, stream-addr=%p, "
2677 "stream-class-addr=%p, "
2678 "stream-class-name=\"%s\", "
2679 "stream-class-id=%" PRId64
,
2680 notit
, stream
, notit
->meta
.stream_class
,
2681 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
2682 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
2686 /* Set packet's context and header fields */
2687 if (notit
->dscopes
.trace_packet_header
) {
2688 ret
= bt_ctf_packet_set_header(packet
,
2689 notit
->dscopes
.trace_packet_header
);
2691 BT_LOGE("Cannot set packet's header field: "
2692 "notit-addr=%p, packet-addr=%p, "
2694 "stream-class-addr=%p, "
2695 "stream-class-name=\"%s\", "
2696 "stream-class-id=%" PRId64
", "
2698 notit
, packet
, stream
, notit
->meta
.stream_class
,
2699 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
2700 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
2701 notit
->dscopes
.trace_packet_header
);
2706 if (notit
->dscopes
.stream_packet_context
) {
2707 ret
= bt_ctf_packet_set_context(packet
,
2708 notit
->dscopes
.stream_packet_context
);
2710 BT_LOGE("Cannot set packet's context field: "
2711 "notit-addr=%p, packet-addr=%p, "
2713 "stream-class-addr=%p, "
2714 "stream-class-name=\"%s\", "
2715 "stream-class-id=%" PRId64
", "
2717 notit
, packet
, stream
, notit
->meta
.stream_class
,
2718 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
2719 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
2720 notit
->dscopes
.trace_packet_header
);
2731 BT_MOVE(notit
->packet
, packet
);
2735 void notify_new_packet(struct bt_ctf_notif_iter
*notit
,
2736 struct bt_notification
**notification
)
2738 struct bt_notification
*ret
;
2740 /* Initialize the iterator's current packet */
2741 create_packet(notit
);
2742 if (!notit
->packet
) {
2743 BT_LOGE("Cannot create packet for packet notification: "
2744 "notit-addr=%p", notit
);
2748 ret
= bt_notification_packet_begin_create(notit
->packet
);
2750 BT_LOGE("Cannot create packet beginning notification: "
2751 "notit-addr=%p, packet-addr=%p",
2752 notit
, notit
->packet
);
2755 *notification
= ret
;
2759 void notify_end_of_packet(struct bt_ctf_notif_iter
*notit
,
2760 struct bt_notification
**notification
)
2762 struct bt_notification
*ret
;
2764 if (!notit
->packet
) {
2768 ret
= bt_notification_packet_end_create(notit
->packet
);
2770 BT_LOGE("Cannot create packet end notification: "
2771 "notit-addr=%p, packet-addr=%p",
2772 notit
, notit
->packet
);
2775 BT_PUT(notit
->packet
);
2776 *notification
= ret
;
2780 void notify_event(struct bt_ctf_notif_iter
*notit
,
2781 struct bt_clock_class_priority_map
*cc_prio_map
,
2782 struct bt_notification
**notification
)
2784 struct bt_ctf_event
*event
;
2785 struct bt_notification
*ret
= NULL
;
2788 event
= create_event(notit
);
2790 BT_LOGE("Cannot create event for event notification: "
2791 "notit-addr=%p", notit
);
2795 ret
= bt_notification_event_create(event
, cc_prio_map
);
2797 BT_LOGE("Cannot create event notification: "
2798 "notit-addr=%p, event-addr=%p, "
2799 "cc-prio-map-addr=%p",
2800 notit
, event
, cc_prio_map
);
2803 *notification
= ret
;
2809 void init_trace_field_path_cache(struct bt_ctf_trace
*trace
,
2810 struct trace_field_path_cache
*trace_field_path_cache
)
2813 int stream_instance_id
= -1;
2815 struct bt_ctf_field_type
*packet_header
= NULL
;
2817 packet_header
= bt_ctf_trace_get_packet_header_type(trace
);
2818 if (!packet_header
) {
2822 if (!bt_ctf_field_type_is_structure(packet_header
)) {
2826 count
= bt_ctf_field_type_structure_get_field_count(packet_header
);
2829 for (i
= 0; (i
< count
&& (stream_id
== -1 || stream_instance_id
== -1)); i
++) {
2831 const char *field_name
;
2833 ret
= bt_ctf_field_type_structure_get_field(packet_header
,
2834 &field_name
, NULL
, i
);
2836 BT_LOGE("Cannot get structure field's field: "
2837 "field-addr=%p, index=%d",
2842 if (stream_id
== -1 && !strcmp(field_name
, "stream_id")) {
2844 } else if (stream_instance_id
== -1 &&
2845 !strcmp(field_name
, "stream_instance_id")) {
2846 stream_instance_id
= i
;
2851 trace_field_path_cache
->stream_id
= stream_id
;
2852 trace_field_path_cache
->stream_instance_id
= stream_instance_id
;
2853 BT_PUT(packet_header
);
2857 struct bt_ctf_notif_iter
*bt_ctf_notif_iter_create(struct bt_ctf_trace
*trace
,
2858 size_t max_request_sz
,
2859 struct bt_ctf_notif_iter_medium_ops medops
, void *data
)
2861 struct bt_ctf_notif_iter
*notit
= NULL
;
2862 struct bt_ctf_btr_cbs cbs
= {
2864 .signed_int
= btr_signed_int_cb
,
2865 .unsigned_int
= btr_unsigned_int_cb
,
2866 .floating_point
= btr_floating_point_cb
,
2867 .string_begin
= btr_string_begin_cb
,
2868 .string
= btr_string_cb
,
2869 .string_end
= btr_string_end_cb
,
2870 .compound_begin
= btr_compound_begin_cb
,
2871 .compound_end
= btr_compound_end_cb
,
2874 .get_sequence_length
= btr_get_sequence_length_cb
,
2875 .get_variant_type
= btr_get_variant_type_cb
,
2880 assert(medops
.request_bytes
);
2881 assert(medops
.get_stream
);
2882 BT_LOGD("Creating CTF plugin notification iterator: "
2883 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
2885 trace
, bt_ctf_trace_get_name(trace
), max_request_sz
, data
);
2886 notit
= g_new0(struct bt_ctf_notif_iter
, 1);
2888 BT_LOGE_STR("Failed to allocate one CTF plugin notification iterator.");
2891 notit
->clock_states
= g_hash_table_new_full(g_direct_hash
,
2892 g_direct_equal
, bt_put
, g_free
);
2893 if (!notit
->clock_states
) {
2894 BT_LOGE_STR("Failed to allocate a GHashTable.");
2897 notit
->meta
.trace
= bt_get(trace
);
2898 notit
->medium
.medops
= medops
;
2899 notit
->medium
.max_request_sz
= max_request_sz
;
2900 notit
->medium
.data
= data
;
2901 notit
->stack
= stack_new(notit
);
2902 if (!notit
->stack
) {
2903 BT_LOGE_STR("Failed to create field stack.");
2907 notit
->btr
= bt_ctf_btr_create(cbs
, notit
);
2909 BT_LOGE_STR("Failed to create binary type reader (BTR).");
2913 bt_ctf_notif_iter_reset(notit
);
2914 init_trace_field_path_cache(trace
, ¬it
->trace_field_path_cache
);
2915 notit
->sc_field_path_caches
= g_hash_table_new_full(g_direct_hash
,
2916 g_direct_equal
, bt_put
, g_free
);
2917 if (!notit
->sc_field_path_caches
) {
2918 BT_LOGE_STR("Failed to allocate a GHashTable.");
2922 notit
->field_overrides
= g_hash_table_new_full(g_direct_hash
,
2923 g_direct_equal
, bt_put
, g_free
);
2924 if (!notit
->field_overrides
) {
2925 BT_LOGE_STR("Failed to allocate a GHashTable.");
2929 BT_LOGD("Created CTF plugin notification iterator: "
2930 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
2931 "data=%p, notit-addr=%p",
2932 trace
, bt_ctf_trace_get_name(trace
), max_request_sz
, data
,
2939 bt_ctf_notif_iter_destroy(notit
);
2944 void bt_ctf_notif_iter_destroy(struct bt_ctf_notif_iter
*notit
)
2946 BT_PUT(notit
->meta
.trace
);
2947 BT_PUT(notit
->meta
.stream_class
);
2948 BT_PUT(notit
->meta
.event_class
);
2949 BT_PUT(notit
->packet
);
2950 BT_PUT(notit
->cur_timestamp_end
);
2951 put_all_dscopes(notit
);
2953 BT_LOGD("Destroying CTF plugin notification iterator: addr=%p", notit
);
2956 BT_LOGD_STR("Destroying field stack.");
2957 stack_destroy(notit
->stack
);
2961 BT_LOGD("Destroying BTR: btr-addr=%p", notit
->btr
);
2962 bt_ctf_btr_destroy(notit
->btr
);
2965 if (notit
->clock_states
) {
2966 g_hash_table_destroy(notit
->clock_states
);
2969 if (notit
->sc_field_path_caches
) {
2970 g_hash_table_destroy(notit
->sc_field_path_caches
);
2973 if (notit
->field_overrides
) {
2974 g_hash_table_destroy(notit
->field_overrides
);
2980 enum bt_ctf_notif_iter_status
bt_ctf_notif_iter_get_next_notification(
2981 struct bt_ctf_notif_iter
*notit
,
2982 struct bt_clock_class_priority_map
*cc_prio_map
,
2983 struct bt_notification
**notification
)
2985 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
2988 assert(notification
);
2990 BT_LOGV("Getting next notification: notit-addr=%p, cc-prio-map-addr=%p",
2991 notit
, cc_prio_map
);
2994 status
= handle_state(notit
);
2995 if (status
== BT_CTF_NOTIF_ITER_STATUS_AGAIN
) {
2996 BT_LOGV_STR("Medium returned BT_CTF_NOTIF_ITER_STATUS_AGAIN.");
2999 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
3000 if (status
== BT_CTF_NOTIF_ITER_STATUS_EOF
) {
3001 BT_LOGV_STR("Medium returned BT_CTF_NOTIF_ITER_STATUS_EOF.");
3003 BT_LOGW("Cannot handle state: "
3004 "notit-addr=%p, state=%s",
3005 notit
, state_string(notit
->state
));
3010 switch (notit
->state
) {
3011 case STATE_EMIT_NOTIF_NEW_PACKET
:
3012 /* notify_new_packet() logs errors */
3013 notify_new_packet(notit
, notification
);
3014 if (!*notification
) {
3015 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
3018 case STATE_EMIT_NOTIF_EVENT
:
3019 /* notify_event() logs errors */
3020 notify_event(notit
, cc_prio_map
, notification
);
3021 if (!*notification
) {
3022 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
3025 case STATE_EMIT_NOTIF_END_OF_PACKET
:
3026 /* Update clock with timestamp_end field. */
3027 if (notit
->cur_timestamp_end
) {
3028 enum bt_ctf_btr_status btr_status
;
3029 struct bt_ctf_field_type
*field_type
=
3030 bt_ctf_field_get_type(
3031 notit
->cur_timestamp_end
);
3034 btr_status
= update_clock(notit
,
3035 notit
->cur_timestamp_end
);
3037 if (btr_status
!= BT_CTF_BTR_STATUS_OK
) {
3038 BT_LOGW("Cannot update stream's clock value: "
3039 "notit-addr=%p", notit
);
3040 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
3045 /* notify_end_of_packet() logs errors */
3046 notify_end_of_packet(notit
, notification
);
3047 if (!*notification
) {
3048 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
3052 /* Non-emitting state: continue */
3062 enum bt_ctf_notif_iter_status
bt_ctf_notif_iter_get_packet_header_context_fields(
3063 struct bt_ctf_notif_iter
*notit
,
3064 struct bt_ctf_field
**packet_header_field
,
3065 struct bt_ctf_field
**packet_context_field
)
3067 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
3071 if (notit
->state
== STATE_EMIT_NOTIF_NEW_PACKET
) {
3072 /* We're already there */
3077 status
= handle_state(notit
);
3078 if (status
== BT_CTF_NOTIF_ITER_STATUS_AGAIN
) {
3079 BT_LOGV_STR("Medium returned BT_CTF_NOTIF_ITER_STATUS_AGAIN.");
3082 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
3083 if (status
== BT_CTF_NOTIF_ITER_STATUS_EOF
) {
3084 BT_LOGV_STR("Medium returned BT_CTF_NOTIF_ITER_STATUS_EOF.");
3086 BT_LOGW("Cannot handle state: "
3087 "notit-addr=%p, state=%s",
3088 notit
, state_string(notit
->state
));
3093 switch (notit
->state
) {
3094 case STATE_EMIT_NOTIF_NEW_PACKET
:
3096 * Packet header and context fields are
3097 * potentially decoded (or they don't exist).
3101 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
3102 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
3103 case STATE_AFTER_TRACE_PACKET_HEADER
:
3104 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
3105 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
3106 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
3107 /* Non-emitting state: continue */
3111 * We should never get past the
3112 * STATE_EMIT_NOTIF_NEW_PACKET state.
3114 BT_LOGF("Unexpected state: notit-addr=%p, state=%s",
3115 notit
, state_string(notit
->state
));
3121 if (packet_header_field
) {
3122 *packet_header_field
= bt_get(notit
->dscopes
.trace_packet_header
);
3125 if (packet_context_field
) {
3126 *packet_context_field
= bt_get(notit
->dscopes
.stream_packet_context
);
3134 void bt_ctf_notif_iter_set_medops_data(struct bt_ctf_notif_iter
*notit
,
3138 notit
->medium
.data
= medops_data
;