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
{
146 /* Error stream (may be NULL) */
150 * Current dynamic scope field pointer.
152 * This is set when a dynamic scope field is first created by
153 * btr_compound_begin_cb(). It points to one of the fields in
156 struct bt_ctf_field
**cur_dscope_field
;
158 /* Trace and classes (owned by this) */
160 struct bt_ctf_trace
*trace
;
161 struct bt_ctf_stream_class
*stream_class
;
162 struct bt_ctf_event_class
*event_class
;
165 /* Current packet (NULL if not created yet) */
166 struct bt_ctf_packet
*packet
;
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
;
882 /* Clear the current stream class field path cache. */
883 notit
->cur_sc_field_path_cache
= NULL
;
885 /* Is there any "stream_id" field in the packet header? */
886 packet_header_type
= bt_ctf_trace_get_packet_header_type(
888 if (!packet_header_type
) {
890 * No packet header, therefore no `stream_id` field,
891 * therefore only one stream class.
893 goto single_stream_class
;
896 assert(is_struct_type(packet_header_type
));
899 stream_id_field_type
=
900 bt_ctf_field_type_structure_get_field_type_by_name(
901 packet_header_type
, "stream_id");
902 if (stream_id_field_type
) {
903 /* Find appropriate stream class using current stream ID */
905 struct bt_ctf_field
*stream_id_field
= NULL
;
907 assert(notit
->dscopes
.trace_packet_header
);
910 stream_id_field
= bt_ctf_field_structure_get_field(
911 notit
->dscopes
.trace_packet_header
, "stream_id");
912 assert(stream_id_field
);
913 ret
= bt_ctf_field_unsigned_integer_get_value(
914 stream_id_field
, &stream_id
);
916 BT_PUT(stream_id_field
);
919 /* Only one stream: pick the first stream class */
920 assert(bt_ctf_trace_get_stream_class_count(
921 notit
->meta
.trace
) == 1);
925 BT_LOGV("Found stream class ID to use: notit-addr=%p, "
926 "stream-class-id=%" PRIu64
", "
927 "trace-addr=%p, trace-name=\"%s\"",
928 notit
, stream_id
, notit
->meta
.trace
,
929 bt_ctf_trace_get_name(notit
->meta
.trace
));
931 BT_PUT(notit
->meta
.stream_class
);
932 notit
->meta
.stream_class
= bt_ctf_trace_get_stream_class_by_id(
933 notit
->meta
.trace
, stream_id
);
934 if (!notit
->meta
.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 BT_LOGV("Set current stream class: "
945 "notit-addr=%p, stream-class-addr=%p, "
946 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
947 notit
, notit
->meta
.stream_class
,
948 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
949 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
952 * Retrieve (or lazily create) the current stream class field path
955 notit
->cur_sc_field_path_cache
= get_stream_class_field_path_cache(
956 notit
, notit
->meta
.stream_class
);
957 if (!notit
->cur_sc_field_path_cache
) {
958 BT_LOGW("Cannot retrieve stream class field path from cache: "
959 "notit-addr=%p, stream-class-addr=%p, "
960 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
961 notit
, notit
->meta
.stream_class
,
962 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
963 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
964 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
968 BT_PUT(packet_header_type
);
969 BT_PUT(stream_id_field_type
);
975 enum bt_ctf_notif_iter_status
after_packet_header_state(
976 struct bt_ctf_notif_iter
*notit
)
978 enum bt_ctf_notif_iter_status status
;
980 status
= set_current_stream_class(notit
);
981 if (status
== BT_CTF_NOTIF_ITER_STATUS_OK
) {
982 notit
->state
= STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
;
989 enum bt_ctf_notif_iter_status
read_packet_context_begin_state(
990 struct bt_ctf_notif_iter
*notit
)
992 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
993 struct bt_ctf_field_type
*packet_context_type
;
995 assert(notit
->meta
.stream_class
);
996 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(
997 notit
->meta
.stream_class
);
998 if (!packet_context_type
) {
999 BT_LOGV("No packet packet context field type in stream class: continuing: "
1000 "notit-addr=%p, stream-class-addr=%p, "
1001 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
1002 notit
, notit
->meta
.stream_class
,
1003 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1004 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
1005 notit
->state
= STATE_AFTER_STREAM_PACKET_CONTEXT
;
1009 BT_LOGV("Decoding packet context field: "
1010 "notit-addr=%p, stream-class-addr=%p, "
1011 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1013 notit
, notit
->meta
.stream_class
,
1014 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1015 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1016 packet_context_type
);
1017 status
= read_dscope_begin_state(notit
, packet_context_type
,
1018 STATE_AFTER_STREAM_PACKET_CONTEXT
,
1019 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
,
1020 ¬it
->dscopes
.stream_packet_context
);
1022 BT_LOGW("Cannot decode packet context field: "
1023 "notit-addr=%p, stream-class-addr=%p, "
1024 "stream-class-name=\"%s\", "
1025 "stream-class-id=%" PRId64
", ft-addr=%p",
1026 notit
, notit
->meta
.stream_class
,
1027 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1028 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1029 packet_context_type
);
1033 BT_PUT(packet_context_type
);
1038 enum bt_ctf_notif_iter_status
read_packet_context_continue_state(
1039 struct bt_ctf_notif_iter
*notit
)
1041 return read_dscope_continue_state(notit
,
1042 STATE_AFTER_STREAM_PACKET_CONTEXT
);
1046 enum bt_ctf_notif_iter_status
set_current_packet_content_sizes(
1047 struct bt_ctf_notif_iter
*notit
)
1049 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1050 struct bt_ctf_field
*packet_size_field
= NULL
;
1051 struct bt_ctf_field
*content_size_field
= NULL
;
1052 uint64_t content_size
= -1, packet_size
= -1;
1054 assert(notit
->dscopes
.stream_packet_context
);
1056 packet_size_field
= bt_ctf_field_structure_get_field(
1057 notit
->dscopes
.stream_packet_context
, "packet_size");
1058 content_size_field
= bt_ctf_field_structure_get_field(
1059 notit
->dscopes
.stream_packet_context
, "content_size");
1060 if (packet_size_field
) {
1061 int ret
= bt_ctf_field_unsigned_integer_get_value(
1062 packet_size_field
, &packet_size
);
1065 if (packet_size
== 0) {
1066 BT_LOGW("Invalid packet size: packet context field indicates packet size is zero: "
1067 "notit-addr=%p, packet-context-field-addr=%p",
1068 notit
, notit
->dscopes
.stream_packet_context
);
1069 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1071 } else if ((packet_size
% 8) != 0) {
1072 BT_LOGW("Invalid packet size: packet context field indicates packet size is not a multiple of 8: "
1073 "notit-addr=%p, packet-context-field-addr=%p, "
1074 "packet-size=%" PRIu64
,
1075 notit
, notit
->dscopes
.stream_packet_context
,
1077 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1082 if (content_size_field
) {
1083 int ret
= bt_ctf_field_unsigned_integer_get_value(
1084 content_size_field
, &content_size
);
1088 content_size
= packet_size
;
1091 if (content_size
> packet_size
) {
1092 BT_LOGW("Invalid packet or content size: packet context field indicates content size is greater than packet size: "
1093 "notit-addr=%p, packet-context-field-addr=%p, "
1094 "packet-size=%" PRIu64
", content-size=%" PRIu64
,
1095 notit
, notit
->dscopes
.stream_packet_context
,
1096 packet_size
, content_size
);
1097 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1101 notit
->cur_packet_size
= packet_size
;
1102 notit
->cur_content_size
= content_size
;
1103 BT_LOGV("Set current packet and content sizes: "
1104 "notit-addr=%p, packet-size=%" PRIu64
", content-size=%" PRIu64
,
1105 notit
, packet_size
, content_size
);
1107 BT_PUT(packet_size_field
);
1108 BT_PUT(content_size_field
);
1113 enum bt_ctf_notif_iter_status
after_packet_context_state(
1114 struct bt_ctf_notif_iter
*notit
)
1116 enum bt_ctf_notif_iter_status status
;
1118 status
= set_current_packet_content_sizes(notit
);
1119 if (status
== BT_CTF_NOTIF_ITER_STATUS_OK
) {
1120 notit
->state
= STATE_EMIT_NOTIF_NEW_PACKET
;
1127 enum bt_ctf_notif_iter_status
read_event_header_begin_state(
1128 struct bt_ctf_notif_iter
*notit
)
1130 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1131 struct bt_ctf_field_type
*event_header_type
= NULL
;
1133 /* Check if we have some content left */
1134 if (notit
->cur_content_size
>= 0) {
1135 if (packet_at(notit
) == notit
->cur_content_size
) {
1136 /* No more events! */
1137 BT_LOGV("Reached end of packet: notit-addr=%p, "
1138 "cur=%zu", notit
, packet_at(notit
));
1139 notit
->state
= STATE_EMIT_NOTIF_END_OF_PACKET
;
1141 } else if (packet_at(notit
) > notit
->cur_content_size
) {
1142 /* That's not supposed to happen */
1143 BT_LOGV("Before decoding event header field: cursor is passed the packet's content: "
1144 "notit-addr=%p, content-size=%zu, "
1145 "cur=%zu", notit
, notit
->cur_content_size
,
1147 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1152 event_header_type
= bt_ctf_stream_class_get_event_header_type(
1153 notit
->meta
.stream_class
);
1154 if (!event_header_type
) {
1155 notit
->state
= STATE_AFTER_STREAM_EVENT_HEADER
;
1159 put_event_dscopes(notit
);
1160 BT_LOGV("Decoding event header field: "
1161 "notit-addr=%p, stream-class-addr=%p, "
1162 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1164 notit
, notit
->meta
.stream_class
,
1165 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1166 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1168 status
= read_dscope_begin_state(notit
, event_header_type
,
1169 STATE_AFTER_STREAM_EVENT_HEADER
,
1170 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
,
1171 ¬it
->dscopes
.stream_event_header
);
1173 BT_LOGW("Cannot decode event header field: "
1174 "notit-addr=%p, stream-class-addr=%p, "
1175 "stream-class-name=\"%s\", "
1176 "stream-class-id=%" PRId64
", ft-addr=%p",
1177 notit
, notit
->meta
.stream_class
,
1178 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1179 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1183 BT_PUT(event_header_type
);
1189 enum bt_ctf_notif_iter_status
read_event_header_continue_state(
1190 struct bt_ctf_notif_iter
*notit
)
1192 return read_dscope_continue_state(notit
,
1193 STATE_AFTER_STREAM_EVENT_HEADER
);
1197 enum bt_ctf_notif_iter_status
set_current_event_class(struct bt_ctf_notif_iter
*notit
)
1200 * The assert() calls in this function are okay because it is
1201 * assumed here that all the metadata objects have been
1202 * validated for CTF correctness before decoding actual streams.
1205 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1206 struct bt_ctf_field_type
*event_header_type
;
1207 struct bt_ctf_field_type
*id_field_type
= NULL
;
1208 struct bt_ctf_field_type
*v_field_type
= NULL
;
1209 uint64_t event_id
= -1ULL;
1212 event_header_type
= bt_ctf_stream_class_get_event_header_type(
1213 notit
->meta
.stream_class
);
1214 if (!event_header_type
) {
1216 * No event header, therefore no event class ID field,
1217 * therefore only one event class.
1219 goto single_event_class
;
1222 /* Is there any "id"/"v" field in the event header? */
1223 assert(is_struct_type(event_header_type
));
1224 id_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1225 event_header_type
, "id");
1226 v_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1227 event_header_type
, "v");
1228 assert(notit
->dscopes
.stream_event_header
);
1232 * | | |_ _|_ _| __ __ _
1233 * | | | | | || '_ \ / _` |
1234 * | |___| | | || | | | (_| | S P E C I A L
1235 * |_____|_| |_||_| |_|\__, | C A S E â„¢
1238 struct bt_ctf_field
*v_field
= NULL
;
1239 struct bt_ctf_field
*v_struct_field
= NULL
;
1240 struct bt_ctf_field
*v_struct_id_field
= NULL
;
1242 // TODO: optimalize!
1243 v_field
= bt_ctf_field_structure_get_field(
1244 notit
->dscopes
.stream_event_header
, "v");
1248 bt_ctf_field_variant_get_current_field(v_field
);
1249 if (!v_struct_field
) {
1250 goto end_v_field_type
;
1253 // TODO: optimalize!
1255 bt_ctf_field_structure_get_field(v_struct_field
, "id");
1256 if (!v_struct_id_field
) {
1257 goto end_v_field_type
;
1260 if (bt_ctf_field_is_integer(v_struct_id_field
)) {
1261 ret
= bt_ctf_field_unsigned_integer_get_value(
1262 v_struct_id_field
, &event_id
);
1264 BT_LOGV("Cannot get value of unsigned integer field (`id`): continuing: "
1265 "notit=%p, field-addr=%p",
1266 notit
, v_struct_id_field
);
1273 BT_PUT(v_struct_field
);
1274 BT_PUT(v_struct_id_field
);
1277 if (id_field_type
&& event_id
== -1ULL) {
1278 /* Check "id" field */
1279 struct bt_ctf_field
*id_field
= NULL
;
1282 // TODO: optimalize!
1283 id_field
= bt_ctf_field_structure_get_field(
1284 notit
->dscopes
.stream_event_header
, "id");
1286 goto check_event_id
;
1289 if (bt_ctf_field_is_integer(id_field
)) {
1290 ret
= bt_ctf_field_unsigned_integer_get_value(
1291 id_field
, &event_id
);
1292 } else if (bt_ctf_field_is_enumeration(id_field
)) {
1293 struct bt_ctf_field
*container
;
1295 container
= bt_ctf_field_enumeration_get_container(
1298 ret
= bt_ctf_field_unsigned_integer_get_value(
1299 container
, &event_id
);
1308 if (event_id
== -1ULL) {
1310 /* Event ID not found: single event? */
1311 assert(bt_ctf_stream_class_get_event_class_count(
1312 notit
->meta
.stream_class
) == 1);
1316 BT_LOGV("Found event class ID to use: notit-addr=%p, "
1317 "stream-class-addr=%p, stream-class-name=\"%s\", "
1318 "stream-class-id=%" PRId64
", "
1319 "event-class-id=%" PRIu64
,
1320 notit
, notit
->meta
.stream_class
,
1321 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1322 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1324 BT_PUT(notit
->meta
.event_class
);
1325 notit
->meta
.event_class
= bt_ctf_stream_class_get_event_class_by_id(
1326 notit
->meta
.stream_class
, event_id
);
1327 if (!notit
->meta
.event_class
) {
1328 BT_LOGW("No event class with ID of event class ID to use in stream class: "
1329 "notit-addr=%p, stream-class-addr=%p, "
1330 "stream-class-name=\"%s\", "
1331 "stream-class-id=%" PRId64
", "
1332 "event-class-id=%" PRIu64
,
1333 notit
, notit
->meta
.stream_class
,
1334 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1335 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1337 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
1341 BT_LOGV("Set current event class: "
1342 "notit-addr=%p, event-class-addr=%p, "
1343 "event-class-name=\"%s\", event-class-id=%" PRId64
,
1344 notit
, notit
->meta
.event_class
,
1345 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
1346 bt_ctf_event_class_get_id(notit
->meta
.event_class
));
1349 BT_PUT(event_header_type
);
1350 BT_PUT(id_field_type
);
1351 BT_PUT(v_field_type
);
1357 enum bt_ctf_notif_iter_status
after_event_header_state(
1358 struct bt_ctf_notif_iter
*notit
)
1360 enum bt_ctf_notif_iter_status status
;
1362 status
= set_current_event_class(notit
);
1363 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
1367 notit
->state
= STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
;
1374 enum bt_ctf_notif_iter_status
read_stream_event_context_begin_state(
1375 struct bt_ctf_notif_iter
*notit
)
1377 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1378 struct bt_ctf_field_type
*stream_event_context_type
;
1380 stream_event_context_type
= bt_ctf_stream_class_get_event_context_type(
1381 notit
->meta
.stream_class
);
1382 if (!stream_event_context_type
) {
1383 notit
->state
= STATE_DSCOPE_EVENT_CONTEXT_BEGIN
;
1387 BT_LOGV("Decoding stream event context field: "
1388 "notit-addr=%p, stream-class-addr=%p, "
1389 "stream-class-name=\"%s\", stream-class-id=%" PRId64
", "
1391 notit
, notit
->meta
.stream_class
,
1392 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1393 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1394 stream_event_context_type
);
1395 status
= read_dscope_begin_state(notit
, stream_event_context_type
,
1396 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
,
1397 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
,
1398 ¬it
->dscopes
.stream_event_context
);
1400 BT_LOGW("Cannot decode stream event context field: "
1401 "notit-addr=%p, stream-class-addr=%p, "
1402 "stream-class-name=\"%s\", "
1403 "stream-class-id=%" PRId64
", ft-addr=%p",
1404 notit
, notit
->meta
.stream_class
,
1405 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
1406 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
1407 stream_event_context_type
);
1411 BT_PUT(stream_event_context_type
);
1417 enum bt_ctf_notif_iter_status
read_stream_event_context_continue_state(
1418 struct bt_ctf_notif_iter
*notit
)
1420 return read_dscope_continue_state(notit
,
1421 STATE_DSCOPE_EVENT_CONTEXT_BEGIN
);
1425 enum bt_ctf_notif_iter_status
read_event_context_begin_state(
1426 struct bt_ctf_notif_iter
*notit
)
1428 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1429 struct bt_ctf_field_type
*event_context_type
;
1431 event_context_type
= bt_ctf_event_class_get_context_type(
1432 notit
->meta
.event_class
);
1433 if (!event_context_type
) {
1434 notit
->state
= STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
;
1438 BT_LOGV("Decoding event context field: "
1439 "notit-addr=%p, event-class-addr=%p, "
1440 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1442 notit
, notit
->meta
.event_class
,
1443 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
1444 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
1445 event_context_type
);
1446 status
= read_dscope_begin_state(notit
, event_context_type
,
1447 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
,
1448 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
,
1449 ¬it
->dscopes
.event_context
);
1451 BT_LOGW("Cannot decode event context field: "
1452 "notit-addr=%p, event-class-addr=%p, "
1453 "event-class-name=\"%s\", "
1454 "event-class-id=%" PRId64
", ft-addr=%p",
1455 notit
, notit
->meta
.event_class
,
1456 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
1457 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
1458 event_context_type
);
1462 BT_PUT(event_context_type
);
1468 enum bt_ctf_notif_iter_status
read_event_context_continue_state(
1469 struct bt_ctf_notif_iter
*notit
)
1471 return read_dscope_continue_state(notit
,
1472 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
);
1476 enum bt_ctf_notif_iter_status
read_event_payload_begin_state(
1477 struct bt_ctf_notif_iter
*notit
)
1479 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1480 struct bt_ctf_field_type
*event_payload_type
;
1482 event_payload_type
= bt_ctf_event_class_get_payload_type(
1483 notit
->meta
.event_class
);
1484 if (!event_payload_type
) {
1485 notit
->state
= STATE_EMIT_NOTIF_EVENT
;
1489 BT_LOGV("Decoding event payload field: "
1490 "notit-addr=%p, event-class-addr=%p, "
1491 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1493 notit
, notit
->meta
.event_class
,
1494 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
1495 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
1496 event_payload_type
);
1497 status
= read_dscope_begin_state(notit
, event_payload_type
,
1498 STATE_EMIT_NOTIF_EVENT
,
1499 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
,
1500 ¬it
->dscopes
.event_payload
);
1502 BT_LOGW("Cannot decode event payload field: "
1503 "notit-addr=%p, event-class-addr=%p, "
1504 "event-class-name=\"%s\", "
1505 "event-class-id=%" PRId64
", ft-addr=%p",
1506 notit
, notit
->meta
.event_class
,
1507 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
1508 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
1509 event_payload_type
);
1513 BT_PUT(event_payload_type
);
1519 enum bt_ctf_notif_iter_status
read_event_payload_continue_state(
1520 struct bt_ctf_notif_iter
*notit
)
1522 return read_dscope_continue_state(notit
, STATE_EMIT_NOTIF_EVENT
);
1526 enum bt_ctf_notif_iter_status
skip_packet_padding_state(
1527 struct bt_ctf_notif_iter
*notit
)
1529 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1530 size_t bits_to_skip
;
1532 assert(notit
->cur_packet_size
> 0);
1533 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1534 if (bits_to_skip
== 0) {
1535 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1538 size_t bits_to_consume
;
1540 BT_LOGV("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu",
1541 bits_to_skip
, notit
, bits_to_skip
);
1542 status
= buf_ensure_available_bits(notit
);
1543 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
1547 bits_to_consume
= MIN(buf_available_bits(notit
), bits_to_skip
);
1548 BT_LOGV("Skipping %zu bits of padding: notit-addr=%p, size=%zu",
1549 bits_to_consume
, notit
, bits_to_consume
);
1550 buf_consume_bits(notit
, bits_to_consume
);
1551 bits_to_skip
= notit
->cur_packet_size
- packet_at(notit
);
1552 if (bits_to_skip
== 0) {
1553 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1563 enum bt_ctf_notif_iter_status
handle_state(struct bt_ctf_notif_iter
*notit
)
1565 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
1566 const enum state state
= notit
->state
;
1568 BT_LOGV("Handling state: notit-addr=%p, state=%s",
1569 notit
, state_string(state
));
1571 // TODO: optimalize!
1574 notit
->state
= STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
;
1576 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
1577 status
= read_packet_header_begin_state(notit
);
1579 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
1580 status
= read_packet_header_continue_state(notit
);
1582 case STATE_AFTER_TRACE_PACKET_HEADER
:
1583 status
= after_packet_header_state(notit
);
1585 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
1586 status
= read_packet_context_begin_state(notit
);
1588 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
1589 status
= read_packet_context_continue_state(notit
);
1591 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
1592 status
= after_packet_context_state(notit
);
1594 case STATE_EMIT_NOTIF_NEW_PACKET
:
1595 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1597 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
:
1598 status
= read_event_header_begin_state(notit
);
1600 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE
:
1601 status
= read_event_header_continue_state(notit
);
1603 case STATE_AFTER_STREAM_EVENT_HEADER
:
1604 status
= after_event_header_state(notit
);
1606 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN
:
1607 status
= read_stream_event_context_begin_state(notit
);
1609 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE
:
1610 status
= read_stream_event_context_continue_state(notit
);
1612 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN
:
1613 status
= read_event_context_begin_state(notit
);
1615 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE
:
1616 status
= read_event_context_continue_state(notit
);
1618 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN
:
1619 status
= read_event_payload_begin_state(notit
);
1621 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE
:
1622 status
= read_event_payload_continue_state(notit
);
1624 case STATE_EMIT_NOTIF_EVENT
:
1625 notit
->state
= STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN
;
1627 case STATE_SKIP_PACKET_PADDING
:
1628 status
= skip_packet_padding_state(notit
);
1630 case STATE_EMIT_NOTIF_END_OF_PACKET
:
1631 notit
->state
= STATE_SKIP_PACKET_PADDING
;
1634 BT_LOGD("Unknown CTF plugin notification iterator state: "
1635 "notit-addr=%p, state=%d", notit
, notit
->state
);
1639 BT_LOGV("Handled state: notit-addr=%p, status=%s, "
1640 "prev-state=%s, cur-state=%s",
1641 notit
, bt_ctf_notif_iter_status_string(status
),
1642 state_string(state
), state_string(notit
->state
));
1647 * Resets the internal state of a CTF notification iterator.
1650 void bt_ctf_notif_iter_reset(struct bt_ctf_notif_iter
*notit
)
1653 BT_LOGD("Resetting notification iterator: addr=%p", notit
);
1654 stack_clear(notit
->stack
);
1655 BT_PUT(notit
->meta
.stream_class
);
1656 BT_PUT(notit
->meta
.event_class
);
1657 BT_PUT(notit
->packet
);
1658 put_all_dscopes(notit
);
1659 notit
->buf
.addr
= NULL
;
1662 notit
->buf
.packet_offset
= 0;
1663 notit
->state
= STATE_INIT
;
1664 notit
->cur_content_size
= -1;
1665 notit
->cur_packet_size
= -1;
1669 int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter
*notit
)
1674 BT_LOGV("Switching packet: notit-addr=%p, cur=%zu",
1675 notit
, notit
->buf
.at
);
1676 stack_clear(notit
->stack
);
1677 BT_PUT(notit
->meta
.stream_class
);
1678 BT_PUT(notit
->meta
.event_class
);
1679 BT_PUT(notit
->packet
);
1680 BT_PUT(notit
->cur_timestamp_end
);
1681 put_all_dscopes(notit
);
1684 * Adjust current buffer so that addr points to the beginning of the new
1687 if (notit
->buf
.addr
) {
1688 size_t consumed_bytes
= (size_t) (notit
->buf
.at
/ CHAR_BIT
);
1690 /* Packets are assumed to start on a byte frontier. */
1691 if (notit
->buf
.at
% CHAR_BIT
) {
1692 BT_LOGW("Cannot switch packet: current position is not a multiple of 8: "
1693 "notit-addr=%p, cur=%zu", notit
, notit
->buf
.at
);
1698 notit
->buf
.addr
+= consumed_bytes
;
1699 notit
->buf
.sz
-= consumed_bytes
;
1701 notit
->buf
.packet_offset
= 0;
1702 BT_LOGV("Adjusted buffer: addr=%p, size=%zu",
1703 notit
->buf
.addr
, notit
->buf
.sz
);
1706 notit
->cur_content_size
= -1;
1707 notit
->cur_packet_size
= -1;
1708 notit
->cur_sc_field_path_cache
= NULL
;
1715 struct bt_ctf_field
*get_next_field(struct bt_ctf_notif_iter
*notit
)
1717 struct bt_ctf_field
*next_field
= NULL
;
1718 struct bt_ctf_field
*base_field
;
1719 struct bt_ctf_field_type
*base_type
;
1722 assert(!stack_empty(notit
->stack
));
1723 index
= stack_top(notit
->stack
)->index
;
1724 base_field
= stack_top(notit
->stack
)->base
;
1726 base_type
= bt_ctf_field_get_type(base_field
);
1729 switch (bt_ctf_field_type_get_type_id(base_type
)) {
1730 case BT_CTF_FIELD_TYPE_ID_STRUCT
:
1731 next_field
= bt_ctf_field_structure_get_field_by_index(
1734 case BT_CTF_FIELD_TYPE_ID_ARRAY
:
1735 next_field
= bt_ctf_field_array_get_field(base_field
, index
);
1737 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
1738 next_field
= bt_ctf_field_sequence_get_field(base_field
, index
);
1740 case BT_CTF_FIELD_TYPE_ID_VARIANT
:
1741 next_field
= bt_ctf_field_variant_get_current_field(base_field
);
1744 BT_LOGF("Unknown base field type ID: "
1745 "notit-addr=%p, ft-addr=%p, ft-id=%s",
1747 bt_ctf_field_type_id_string(
1748 bt_ctf_field_type_get_type_id(base_type
)));
1757 void update_clock_state(uint64_t *state
,
1758 struct bt_ctf_field
*value_field
)
1760 struct bt_ctf_field_type
*value_type
= NULL
;
1761 uint64_t requested_new_value
;
1762 uint64_t requested_new_value_mask
;
1763 uint64_t cur_value_masked
;
1764 int requested_new_value_size
;
1767 value_type
= bt_ctf_field_get_type(value_field
);
1769 assert(bt_ctf_field_type_is_integer(value_type
));
1770 requested_new_value_size
=
1771 bt_ctf_field_type_integer_get_size(value_type
);
1772 assert(requested_new_value_size
> 0);
1773 ret
= bt_ctf_field_unsigned_integer_get_value(value_field
,
1774 &requested_new_value
);
1778 * Special case for a 64-bit new value, which is the limit
1779 * of a clock value as of this version: overwrite the
1780 * current value directly.
1782 if (requested_new_value_size
== 64) {
1783 *state
= requested_new_value
;
1787 requested_new_value_mask
= (1ULL << requested_new_value_size
) - 1;
1788 cur_value_masked
= *state
& requested_new_value_mask
;
1790 if (requested_new_value
< cur_value_masked
) {
1792 * It looks like a wrap happened on the number of bits
1793 * of the requested new value. Assume that the clock
1794 * value wrapped only one time.
1796 *state
+= requested_new_value_mask
+ 1;
1799 /* Clear the low bits of the current clock value. */
1800 *state
&= ~requested_new_value_mask
;
1802 /* Set the low bits of the current clock value. */
1803 *state
|= requested_new_value
;
1805 BT_LOGV("Updated clock's value from integer field's value: "
1806 "value=%" PRIu64
, *state
);
1811 enum bt_ctf_btr_status
update_clock(struct bt_ctf_notif_iter
*notit
,
1812 struct bt_ctf_field
*int_field
)
1814 gboolean clock_class_found
;
1815 uint64_t *clock_state
;
1816 struct bt_ctf_field_type
*int_field_type
= NULL
;
1817 enum bt_ctf_btr_status ret
= BT_CTF_BTR_STATUS_OK
;
1818 struct bt_ctf_clock_class
*clock_class
= NULL
;
1820 int_field_type
= bt_ctf_field_get_type(int_field
);
1821 assert(int_field_type
);
1822 clock_class
= bt_ctf_field_type_integer_get_mapped_clock_class(
1824 if (likely(!clock_class
)) {
1828 clock_class_found
= g_hash_table_lookup_extended(notit
->clock_states
,
1829 clock_class
, NULL
, (gpointer
) &clock_state
);
1830 if (unlikely(!clock_class_found
)) {
1831 ret
= BT_CTF_BTR_STATUS_ERROR
;
1835 if (unlikely(!clock_state
)) {
1836 clock_state
= g_new0(uint64_t, 1);
1838 BT_LOGE_STR("Failed to allocate a uint64_t.");
1839 ret
= BT_CTF_BTR_STATUS_ENOMEM
;
1843 g_hash_table_insert(notit
->clock_states
, bt_get(clock_class
),
1847 /* Update the clock's state. */
1848 BT_LOGV("Updating notification iterator's clock's value from integer field: "
1849 "notit-addr=%p, clock-class-addr=%p, "
1850 "clock-class-name=\"%s\", value=%" PRIu64
,
1852 bt_ctf_clock_class_get_name(clock_class
), *clock_state
);
1853 update_clock_state(clock_state
, int_field
);
1855 bt_put(int_field_type
);
1856 bt_put(clock_class
);
1861 enum bt_ctf_btr_status
btr_unsigned_int_common(uint64_t value
,
1862 struct bt_ctf_field_type
*type
, void *data
,
1863 struct bt_ctf_field
**out_int_field
)
1865 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1866 struct bt_ctf_field
*field
= NULL
;
1867 struct bt_ctf_field
*int_field
= NULL
;
1868 struct bt_ctf_notif_iter
*notit
= data
;
1871 BT_LOGV("Common unsigned integer function called from BTR: "
1872 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
1873 "ft-id=%s, value=%" PRIu64
,
1874 notit
, notit
->btr
, type
,
1875 bt_ctf_field_type_id_string(
1876 bt_ctf_field_type_get_type_id(type
)),
1879 /* Create next field */
1880 field
= get_next_field(notit
);
1882 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
1883 status
= BT_CTF_BTR_STATUS_ERROR
;
1887 switch(bt_ctf_field_type_get_type_id(type
)) {
1888 case BT_CTF_FIELD_TYPE_ID_INTEGER
:
1889 /* Integer field is created field */
1890 BT_MOVE(int_field
, field
);
1893 case BT_CTF_FIELD_TYPE_ID_ENUM
:
1894 int_field
= bt_ctf_field_enumeration_get_container(field
);
1896 type
= bt_ctf_field_get_type(int_field
);
1900 BT_LOGF("Unexpected field type ID: "
1901 "notit-addr=%p, ft-addr=%p, ft-id=%s",
1903 bt_ctf_field_type_id_string(
1904 bt_ctf_field_type_get_type_id(type
)));
1909 ret
= bt_ctf_field_unsigned_integer_set_value(int_field
, value
);
1911 stack_top(notit
->stack
)->index
++;
1912 *out_int_field
= int_field
;
1921 enum bt_ctf_btr_status
btr_timestamp_end_cb(void *value
,
1922 struct bt_ctf_field_type
*type
, void *data
)
1924 enum bt_ctf_btr_status status
;
1925 struct bt_ctf_field
*field
= NULL
;
1926 struct bt_ctf_notif_iter
*notit
= data
;
1928 BT_LOGV("`timestamp_end` unsigned integer function called from BTR: "
1929 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
1931 notit
, notit
->btr
, type
,
1932 bt_ctf_field_type_id_string(
1933 bt_ctf_field_type_get_type_id(type
)));
1934 status
= btr_unsigned_int_common(*((uint64_t *) value
), type
, data
,
1937 /* Set as the current packet's timestamp_end field. */
1938 BT_MOVE(notit
->cur_timestamp_end
, field
);
1943 enum bt_ctf_btr_status
btr_unsigned_int_cb(uint64_t value
,
1944 struct bt_ctf_field_type
*type
, void *data
)
1946 struct bt_ctf_notif_iter
*notit
= data
;
1947 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1948 struct bt_ctf_field
*field
= NULL
;
1949 struct field_cb_override
*override
;
1951 BT_LOGV("Unsigned integer function called from BTR: "
1952 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
1953 "ft-id=%s, value=%" PRIu64
,
1954 notit
, notit
->btr
, type
,
1955 bt_ctf_field_type_id_string(
1956 bt_ctf_field_type_get_type_id(type
)),
1958 override
= g_hash_table_lookup(notit
->field_overrides
, type
);
1959 if (unlikely(override
)) {
1960 /* Override function logs errors */
1961 status
= override
->func(&value
, type
, override
->data
);
1965 status
= btr_unsigned_int_common(value
, type
, data
, &field
);
1966 if (status
!= BT_CTF_BTR_STATUS_OK
) {
1967 /* btr_unsigned_int_common() logs errors */
1971 status
= update_clock(notit
, field
);
1978 enum bt_ctf_btr_status
btr_signed_int_cb(int64_t value
,
1979 struct bt_ctf_field_type
*type
, void *data
)
1981 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
1982 struct bt_ctf_field
*field
= NULL
;
1983 struct bt_ctf_field
*int_field
= NULL
;
1984 struct bt_ctf_notif_iter
*notit
= data
;
1987 BT_LOGV("Signed integer function called from BTR: "
1988 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
1989 "ft-id=%s, value=%" PRId64
,
1990 notit
, notit
->btr
, type
,
1991 bt_ctf_field_type_id_string(
1992 bt_ctf_field_type_get_type_id(type
)),
1995 /* create next field */
1996 field
= get_next_field(notit
);
1998 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
1999 status
= BT_CTF_BTR_STATUS_ERROR
;
2003 switch(bt_ctf_field_type_get_type_id(type
)) {
2004 case BT_CTF_FIELD_TYPE_ID_INTEGER
:
2005 /* Integer field is created field */
2006 BT_MOVE(int_field
, field
);
2009 case BT_CTF_FIELD_TYPE_ID_ENUM
:
2010 int_field
= bt_ctf_field_enumeration_get_container(field
);
2012 type
= bt_ctf_field_get_type(int_field
);
2016 BT_LOGF("Unexpected field type ID: "
2017 "notit-addr=%p, ft-addr=%p, ft-id=%s",
2019 bt_ctf_field_type_id_string(
2020 bt_ctf_field_type_get_type_id(type
)));
2025 ret
= bt_ctf_field_signed_integer_set_value(int_field
, value
);
2027 stack_top(notit
->stack
)->index
++;
2028 status
= update_clock(notit
, int_field
);
2038 enum bt_ctf_btr_status
btr_floating_point_cb(double value
,
2039 struct bt_ctf_field_type
*type
, void *data
)
2041 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2042 struct bt_ctf_field
*field
= NULL
;
2043 struct bt_ctf_notif_iter
*notit
= data
;
2046 BT_LOGV("Floating point number function called from BTR: "
2047 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2048 "ft-id=%s, value=%f",
2049 notit
, notit
->btr
, type
,
2050 bt_ctf_field_type_id_string(
2051 bt_ctf_field_type_get_type_id(type
)),
2054 /* Create next field */
2055 field
= get_next_field(notit
);
2057 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2058 status
= BT_CTF_BTR_STATUS_ERROR
;
2062 ret
= bt_ctf_field_floating_point_set_value(field
, value
);
2064 stack_top(notit
->stack
)->index
++;
2072 enum bt_ctf_btr_status
btr_string_begin_cb(
2073 struct bt_ctf_field_type
*type
, void *data
)
2075 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2076 struct bt_ctf_field
*field
= NULL
;
2077 struct bt_ctf_notif_iter
*notit
= data
;
2080 BT_LOGV("String (beginning) function called from BTR: "
2081 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2083 notit
, notit
->btr
, type
,
2084 bt_ctf_field_type_id_string(
2085 bt_ctf_field_type_get_type_id(type
)));
2087 /* Create next field */
2088 field
= get_next_field(notit
);
2090 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2091 status
= BT_CTF_BTR_STATUS_ERROR
;
2096 * Push on stack. Not a compound type per se, but we know that only
2097 * btr_string_cb() may be called between this call and a subsequent
2098 * call to btr_string_end_cb().
2100 ret
= stack_push(notit
->stack
, field
);
2102 BT_LOGE("Cannot push string field on stack: "
2103 "notit-addr=%p, field-addr=%p", notit
, field
);
2104 status
= BT_CTF_BTR_STATUS_ERROR
;
2109 * Initialize string field payload to an empty string since in the
2110 * case of a length 0 string the btr_string_cb won't be called and
2111 * we will end up with an unset string payload.
2113 ret
= bt_ctf_field_string_set_value(field
, "");
2115 BT_LOGE("Cannot initialize string field's value to an empty string: "
2116 "notit-addr=%p, field-addr=%p, ret=%d",
2118 status
= BT_CTF_BTR_STATUS_ERROR
;
2129 enum bt_ctf_btr_status
btr_string_cb(const char *value
,
2130 size_t len
, struct bt_ctf_field_type
*type
, void *data
)
2132 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2133 struct bt_ctf_field
*field
= NULL
;
2134 struct bt_ctf_notif_iter
*notit
= data
;
2137 BT_LOGV("String (substring) function called from BTR: "
2138 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2139 "ft-id=%s, string-length=%zu",
2140 notit
, notit
->btr
, type
,
2141 bt_ctf_field_type_id_string(
2142 bt_ctf_field_type_get_type_id(type
)),
2145 /* Get string field */
2146 field
= stack_top(notit
->stack
)->base
;
2149 /* Append current string */
2150 ret
= bt_ctf_field_string_append_len(field
, value
, len
);
2152 BT_LOGE("Cannot append substring to string field's value: "
2153 "notit-addr=%p, field-addr=%p, string-length=%zu, "
2154 "ret=%d", notit
, field
, len
, ret
);
2155 status
= BT_CTF_BTR_STATUS_ERROR
;
2164 enum bt_ctf_btr_status
btr_string_end_cb(
2165 struct bt_ctf_field_type
*type
, void *data
)
2167 struct bt_ctf_notif_iter
*notit
= data
;
2169 BT_LOGV("String (end) function called from BTR: "
2170 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2172 notit
, notit
->btr
, type
,
2173 bt_ctf_field_type_id_string(
2174 bt_ctf_field_type_get_type_id(type
)));
2176 /* Pop string field */
2177 stack_pop(notit
->stack
);
2179 /* Go to next field */
2180 stack_top(notit
->stack
)->index
++;
2181 return BT_CTF_BTR_STATUS_OK
;
2184 enum bt_ctf_btr_status
btr_compound_begin_cb(
2185 struct bt_ctf_field_type
*type
, void *data
)
2187 enum bt_ctf_btr_status status
= BT_CTF_BTR_STATUS_OK
;
2188 struct bt_ctf_notif_iter
*notit
= data
;
2189 struct bt_ctf_field
*field
;
2192 BT_LOGV("Compound (beginning) function called from BTR: "
2193 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2195 notit
, notit
->btr
, type
,
2196 bt_ctf_field_type_id_string(
2197 bt_ctf_field_type_get_type_id(type
)));
2200 if (stack_empty(notit
->stack
)) {
2201 /* Root: create dynamic scope field */
2202 *notit
->cur_dscope_field
= bt_ctf_field_create(type
);
2203 field
= *notit
->cur_dscope_field
;
2206 * Field will be put at the end of this function
2207 * (stack_push() will take one reference, but this
2208 * reference is lost upon the equivalent stack_pop()
2209 * later), so also get it for our context to own it.
2211 bt_get(*notit
->cur_dscope_field
);
2214 BT_LOGE("Cannot create compound field: "
2215 "notit-addr=%p, ft-addr=%p, ft-id=%s",
2217 bt_ctf_field_type_id_string(
2218 bt_ctf_field_type_get_type_id(type
)));
2219 status
= BT_CTF_BTR_STATUS_ERROR
;
2223 field
= get_next_field(notit
);
2225 BT_LOGW("Cannot get next field: notit-addr=%p", notit
);
2226 status
= BT_CTF_BTR_STATUS_ERROR
;
2233 ret
= stack_push(notit
->stack
, field
);
2235 BT_LOGE("Cannot push compound field onto the stack: "
2236 "notit-addr=%p, ft-addr=%p, ft-id=%s, ret=%d",
2238 bt_ctf_field_type_id_string(
2239 bt_ctf_field_type_get_type_id(type
)),
2241 status
= BT_CTF_BTR_STATUS_ERROR
;
2251 enum bt_ctf_btr_status
btr_compound_end_cb(
2252 struct bt_ctf_field_type
*type
, void *data
)
2254 struct bt_ctf_notif_iter
*notit
= data
;
2256 BT_LOGV("Compound (end) function called from BTR: "
2257 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2259 notit
, notit
->btr
, type
,
2260 bt_ctf_field_type_id_string(
2261 bt_ctf_field_type_get_type_id(type
)));
2262 assert(!stack_empty(notit
->stack
));
2265 stack_pop(notit
->stack
);
2267 /* If the stack is not empty, increment the base's index */
2268 if (!stack_empty(notit
->stack
)) {
2269 stack_top(notit
->stack
)->index
++;
2272 return BT_CTF_BTR_STATUS_OK
;
2276 struct bt_ctf_field
*resolve_field(struct bt_ctf_notif_iter
*notit
,
2277 struct bt_ctf_field_path
*path
)
2279 struct bt_ctf_field
*field
= NULL
;
2282 if (BT_LOG_ON_VERBOSE
) {
2283 GString
*gstr
= bt_ctf_field_path_string(path
);
2285 BT_LOGV("Resolving field path: notit-addr=%p, field-path=\"%s\"",
2286 notit
, gstr
? gstr
->str
: NULL
);
2289 g_string_free(gstr
, TRUE
);
2293 switch (bt_ctf_field_path_get_root_scope(path
)) {
2294 case BT_CTF_SCOPE_TRACE_PACKET_HEADER
:
2295 field
= notit
->dscopes
.trace_packet_header
;
2297 case BT_CTF_SCOPE_STREAM_PACKET_CONTEXT
:
2298 field
= notit
->dscopes
.stream_packet_context
;
2300 case BT_CTF_SCOPE_STREAM_EVENT_HEADER
:
2301 field
= notit
->dscopes
.stream_event_header
;
2303 case BT_CTF_SCOPE_STREAM_EVENT_CONTEXT
:
2304 field
= notit
->dscopes
.stream_event_context
;
2306 case BT_CTF_SCOPE_EVENT_CONTEXT
:
2307 field
= notit
->dscopes
.event_context
;
2309 case BT_CTF_SCOPE_EVENT_FIELDS
:
2310 field
= notit
->dscopes
.event_payload
;
2313 BT_LOGF("Cannot resolve field path: unknown scope: "
2314 "notit-addr=%p, root-scope=%s",
2315 notit
, bt_ctf_scope_string(
2316 bt_ctf_field_path_get_root_scope(path
)));
2321 BT_LOGW("Cannot resolve field path: root field not found: "
2322 "notit-addr=%p, root-scope=%s",
2323 notit
, bt_ctf_scope_string(
2324 bt_ctf_field_path_get_root_scope(path
)));
2330 for (i
= 0; i
< bt_ctf_field_path_get_index_count(path
); ++i
) {
2331 struct bt_ctf_field
*next_field
= NULL
;
2332 struct bt_ctf_field_type
*field_type
;
2333 int index
= bt_ctf_field_path_get_index(path
, i
);
2335 field_type
= bt_ctf_field_get_type(field
);
2338 if (is_struct_type(field_type
)) {
2339 next_field
= bt_ctf_field_structure_get_field_by_index(
2341 } else if (is_variant_type(field_type
)) {
2343 bt_ctf_field_variant_get_current_field(field
);
2350 BT_LOGW("Cannot find next field: "
2351 "notit-addr=%p, ft-addr=%p, ft-id=%s, index=%d",
2353 bt_ctf_field_type_id_string(
2354 bt_ctf_field_type_get_type_id(field_type
)),
2359 /* Move next field -> field */
2360 BT_MOVE(field
, next_field
);
2368 int64_t btr_get_sequence_length_cb(struct bt_ctf_field_type
*type
, void *data
)
2372 struct bt_ctf_field
*seq_field
;
2373 struct bt_ctf_field_path
*field_path
;
2374 struct bt_ctf_notif_iter
*notit
= data
;
2375 struct bt_ctf_field
*length_field
= NULL
;
2378 field_path
= bt_ctf_field_type_sequence_get_length_field_path(type
);
2380 length_field
= resolve_field(notit
, field_path
);
2381 if (!length_field
) {
2382 BT_LOGW("Cannot resolve sequence field type's length field path: "
2383 "notit-addr=%p, ft-addr=%p",
2388 iret
= bt_ctf_field_unsigned_integer_get_value(length_field
, &length
);
2390 BT_LOGE("Cannot get value of sequence length field: "
2391 "notit-addr=%p, field-addr=%p",
2392 notit
, length_field
);
2396 seq_field
= stack_top(notit
->stack
)->base
;
2397 iret
= bt_ctf_field_sequence_set_length(seq_field
, length_field
);
2399 BT_LOGE("Cannot set sequence field's length field: "
2400 "notit-addr=%p, seq-field-addr=%p, "
2401 "length-field-addr=%p, ",
2402 notit
, seq_field
, length_field
);
2406 ret
= (int64_t) length
;
2409 BT_PUT(length_field
);
2416 struct bt_ctf_field_type
*btr_get_variant_type_cb(
2417 struct bt_ctf_field_type
*type
, void *data
)
2419 struct bt_ctf_field_path
*path
;
2420 struct bt_ctf_notif_iter
*notit
= data
;
2421 struct bt_ctf_field
*var_field
;
2422 struct bt_ctf_field
*tag_field
= NULL
;
2423 struct bt_ctf_field
*selected_field
= NULL
;
2424 struct bt_ctf_field_type
*selected_field_type
= NULL
;
2426 path
= bt_ctf_field_type_variant_get_tag_field_path(type
);
2428 tag_field
= resolve_field(notit
, path
);
2430 BT_LOGW("Cannot resolve variant field type's tag field path: "
2431 "notit-addr=%p, ft-addr=%p",
2437 * We found the enumeration tag field instance which should be
2438 * able to select a current field for this variant. This
2439 * callback function we're in is called _after_
2440 * compound_begin(), so the current stack top's base field is
2441 * the variant field in question. We get the selected field here
2442 * thanks to this tag field (thus creating the selected field),
2443 * which will also provide us with its type. Then, this field
2444 * will remain the current selected one until the next callback
2445 * function call which is used to fill the current selected
2448 var_field
= stack_top(notit
->stack
)->base
;
2449 selected_field
= bt_ctf_field_variant_get_field(var_field
, tag_field
);
2450 if (!selected_field
) {
2451 BT_LOGW("Cannot get variant field's selection using tag field: "
2452 "notit-addr=%p, var-field-addr=%p, tag-field-addr=%p",
2453 notit
, var_field
, tag_field
);
2457 selected_field_type
= bt_ctf_field_get_type(selected_field
);
2461 BT_PUT(selected_field
);
2464 return selected_field_type
;
2468 int set_event_clocks(struct bt_ctf_event
*event
,
2469 struct bt_ctf_notif_iter
*notit
)
2472 GHashTableIter iter
;
2473 struct bt_ctf_clock_class
*clock_class
;
2474 uint64_t *clock_state
;
2476 g_hash_table_iter_init(&iter
, notit
->clock_states
);
2478 while (g_hash_table_iter_next(&iter
, (gpointer
) &clock_class
,
2479 (gpointer
) &clock_state
)) {
2480 struct bt_ctf_clock_value
*clock_value
;
2482 clock_value
= bt_ctf_clock_value_create(clock_class
,
2485 BT_LOGE("Cannot create clock value from clock class: "
2486 "notit-addr=%p, clock-class-addr=%p, "
2487 "clock-class-name=\"%s\"",
2489 bt_ctf_clock_class_get_name(clock_class
));
2493 ret
= bt_ctf_event_set_clock_value(event
, clock_value
);
2494 bt_put(clock_value
);
2496 struct bt_ctf_event_class
*event_class
=
2497 bt_ctf_event_get_class(event
);
2499 assert(event_class
);
2500 BT_LOGE("Cannot set event's clock value: "
2501 "notit-addr=%p, event-addr=%p, "
2502 "event-class-name=\"%s\", "
2503 "event-class-id=%" PRId64
", "
2504 "clock-class-addr=%p, "
2505 "clock-class-name=\"%s\", "
2506 "clock-value-addr=%p",
2508 bt_ctf_event_class_get_name(event_class
),
2509 bt_ctf_event_class_get_id(event_class
),
2511 bt_ctf_clock_class_get_name(clock_class
),
2513 bt_put(event_class
);
2524 struct bt_ctf_event
*create_event(struct bt_ctf_notif_iter
*notit
)
2527 struct bt_ctf_event
*event
;
2529 BT_LOGV("Creating event for event notification: "
2530 "notit-addr=%p, event-class-addr=%p, "
2531 "event-class-name=\"%s\", "
2532 "event-class-id=%" PRId64
,
2533 notit
, notit
->meta
.event_class
,
2534 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2535 bt_ctf_event_class_get_id(notit
->meta
.event_class
));
2537 /* Create event object. */
2538 event
= bt_ctf_event_create(notit
->meta
.event_class
);
2540 BT_LOGE("Cannot create event: "
2541 "notit-addr=%p, event-class-addr=%p, "
2542 "event-class-name=\"%s\", "
2543 "event-class-id=%" PRId64
,
2544 notit
, notit
->meta
.event_class
,
2545 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2546 bt_ctf_event_class_get_id(notit
->meta
.event_class
));
2550 /* Set header, stream event context, context, and payload fields. */
2551 ret
= bt_ctf_event_set_header(event
,
2552 notit
->dscopes
.stream_event_header
);
2554 BT_LOGE("Cannot set event's header field: "
2555 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2556 "event-class-name=\"%s\", "
2557 "event-class-id=%" PRId64
", field-addr=%p",
2558 notit
, event
, notit
->meta
.event_class
,
2559 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2560 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
2561 notit
->dscopes
.stream_event_header
);
2565 ret
= bt_ctf_event_set_stream_event_context(event
,
2566 notit
->dscopes
.stream_event_context
);
2568 BT_LOGE("Cannot set event's context field: "
2569 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2570 "event-class-name=\"%s\", "
2571 "event-class-id=%" PRId64
", field-addr=%p",
2572 notit
, event
, notit
->meta
.event_class
,
2573 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2574 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
2575 notit
->dscopes
.stream_event_context
);
2579 ret
= bt_ctf_event_set_event_context(event
,
2580 notit
->dscopes
.event_context
);
2582 BT_LOGE("Cannot set event's stream event context field: "
2583 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2584 "event-class-name=\"%s\", "
2585 "event-class-id=%" PRId64
", field-addr=%p",
2586 notit
, event
, notit
->meta
.event_class
,
2587 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2588 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
2589 notit
->dscopes
.event_context
);
2593 ret
= bt_ctf_event_set_event_payload(event
,
2594 notit
->dscopes
.event_payload
);
2596 BT_LOGE("Cannot set event's payload field: "
2597 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2598 "event-class-name=\"%s\", "
2599 "event-class-id=%" PRId64
", field-addr=%p",
2600 notit
, event
, notit
->meta
.event_class
,
2601 bt_ctf_event_class_get_name(notit
->meta
.event_class
),
2602 bt_ctf_event_class_get_id(notit
->meta
.event_class
),
2603 notit
->dscopes
.event_payload
);
2607 ret
= set_event_clocks(event
, notit
);
2609 BT_LOGE("Cannot set event's clock values: "
2610 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2611 "event-class-name=\"%s\", "
2612 "event-class-id=%" PRId64
,
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
));
2619 /* Associate with current packet. */
2620 assert(notit
->packet
);
2621 ret
= bt_ctf_event_set_packet(event
, notit
->packet
);
2623 BT_LOGE("Cannot set event's header field: "
2624 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2625 "event-class-name=\"%s\", "
2626 "event-class-id=%" PRId64
", packet-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
),
2644 void create_packet(struct bt_ctf_notif_iter
*notit
)
2647 struct bt_ctf_stream
*stream
= NULL
;
2648 struct bt_ctf_packet
*packet
= NULL
;
2650 BT_LOGV("Creating packet for packet notification: "
2651 "notit-addr=%p", notit
);
2653 /* Ask the user for the stream */
2654 BT_LOGV("Calling user function (get stream): notit-addr=%p, "
2655 "stream-class-addr=%p, stream-class-name=\"%s\", "
2656 "stream-class-id=%" PRId64
,
2657 notit
, notit
->meta
.stream_class
,
2658 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
2659 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
2660 stream
= notit
->medium
.medops
.get_stream(notit
->meta
.stream_class
,
2661 notit
->medium
.data
);
2662 BT_LOGV("User function returned: stream-addr=%p",
2665 BT_LOGW_STR("User function failed to return a stream object for the given stream class.");
2669 BT_LOGV("Creating packet from stream: "
2670 "notit-addr=%p, stream-addr=%p, "
2671 "stream-class-addr=%p, "
2672 "stream-class-name=\"%s\", "
2673 "stream-class-id=%" PRId64
,
2674 notit
, stream
, notit
->meta
.stream_class
,
2675 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
2676 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
2679 packet
= bt_ctf_packet_create(stream
);
2681 BT_LOGE("Cannot create packet from stream: "
2682 "notit-addr=%p, stream-addr=%p, "
2683 "stream-class-addr=%p, "
2684 "stream-class-name=\"%s\", "
2685 "stream-class-id=%" PRId64
,
2686 notit
, stream
, notit
->meta
.stream_class
,
2687 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
2688 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
));
2692 /* Set packet's context and header fields */
2693 if (notit
->dscopes
.trace_packet_header
) {
2694 ret
= bt_ctf_packet_set_header(packet
,
2695 notit
->dscopes
.trace_packet_header
);
2697 BT_LOGE("Cannot set packet's header field: "
2698 "notit-addr=%p, packet-addr=%p, "
2700 "stream-class-addr=%p, "
2701 "stream-class-name=\"%s\", "
2702 "stream-class-id=%" PRId64
", "
2704 notit
, packet
, stream
, notit
->meta
.stream_class
,
2705 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
2706 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
2707 notit
->dscopes
.trace_packet_header
);
2712 if (notit
->dscopes
.stream_packet_context
) {
2713 ret
= bt_ctf_packet_set_context(packet
,
2714 notit
->dscopes
.stream_packet_context
);
2716 BT_LOGE("Cannot set packet's context field: "
2717 "notit-addr=%p, packet-addr=%p, "
2719 "stream-class-addr=%p, "
2720 "stream-class-name=\"%s\", "
2721 "stream-class-id=%" PRId64
", "
2723 notit
, packet
, stream
, notit
->meta
.stream_class
,
2724 bt_ctf_stream_class_get_name(notit
->meta
.stream_class
),
2725 bt_ctf_stream_class_get_id(notit
->meta
.stream_class
),
2726 notit
->dscopes
.trace_packet_header
);
2737 BT_MOVE(notit
->packet
, packet
);
2741 void notify_new_packet(struct bt_ctf_notif_iter
*notit
,
2742 struct bt_notification
**notification
)
2744 struct bt_notification
*ret
;
2746 /* Initialize the iterator's current packet */
2747 create_packet(notit
);
2748 if (!notit
->packet
) {
2749 BT_LOGE("Cannot create packet for packet notification: "
2750 "notit-addr=%p", notit
);
2754 ret
= bt_notification_packet_begin_create(notit
->packet
);
2756 BT_LOGE("Cannot create packet beginning notification: "
2757 "notit-addr=%p, packet-addr=%p",
2758 notit
, notit
->packet
);
2761 *notification
= ret
;
2765 void notify_end_of_packet(struct bt_ctf_notif_iter
*notit
,
2766 struct bt_notification
**notification
)
2768 struct bt_notification
*ret
;
2770 if (!notit
->packet
) {
2774 ret
= bt_notification_packet_end_create(notit
->packet
);
2776 BT_LOGE("Cannot create packet end notification: "
2777 "notit-addr=%p, packet-addr=%p",
2778 notit
, notit
->packet
);
2781 BT_PUT(notit
->packet
);
2782 *notification
= ret
;
2786 void notify_event(struct bt_ctf_notif_iter
*notit
,
2787 struct bt_clock_class_priority_map
*cc_prio_map
,
2788 struct bt_notification
**notification
)
2790 struct bt_ctf_event
*event
;
2791 struct bt_notification
*ret
= NULL
;
2794 event
= create_event(notit
);
2796 BT_LOGE("Cannot create event for event notification: "
2797 "notit-addr=%p", notit
);
2801 ret
= bt_notification_event_create(event
, cc_prio_map
);
2803 BT_LOGE("Cannot create event notification: "
2804 "notit-addr=%p, event-addr=%p, "
2805 "cc-prio-map-addr=%p",
2806 notit
, event
, cc_prio_map
);
2809 *notification
= ret
;
2815 int init_clock_states(GHashTable
*clock_states
, struct bt_ctf_trace
*trace
)
2817 int clock_class_count
, i
, ret
= 0;
2820 clock_class_count
= bt_ctf_trace_get_clock_class_count(trace
);
2821 assert(clock_class_count
>= 0);
2823 for (i
= 0; i
< clock_class_count
; i
++) {
2824 struct bt_ctf_clock_class
*clock_class
;
2826 clock_class
= bt_ctf_trace_get_clock_class_by_index(trace
, i
);
2827 assert(clock_class
);
2828 g_hash_table_insert(clock_states
, bt_get(clock_class
), NULL
);
2829 bt_put(clock_class
);
2836 void init_trace_field_path_cache(struct bt_ctf_trace
*trace
,
2837 struct trace_field_path_cache
*trace_field_path_cache
)
2840 int stream_instance_id
= -1;
2842 struct bt_ctf_field_type
*packet_header
= NULL
;
2844 packet_header
= bt_ctf_trace_get_packet_header_type(trace
);
2845 if (!packet_header
) {
2849 if (!bt_ctf_field_type_is_structure(packet_header
)) {
2853 count
= bt_ctf_field_type_structure_get_field_count(packet_header
);
2856 for (i
= 0; (i
< count
&& (stream_id
== -1 || stream_instance_id
== -1)); i
++) {
2858 const char *field_name
;
2860 ret
= bt_ctf_field_type_structure_get_field(packet_header
,
2861 &field_name
, NULL
, i
);
2863 BT_LOGE("Cannot get structure field's field: "
2864 "field-addr=%p, index=%d",
2869 if (stream_id
== -1 && !strcmp(field_name
, "stream_id")) {
2871 } else if (stream_instance_id
== -1 &&
2872 !strcmp(field_name
, "stream_instance_id")) {
2873 stream_instance_id
= i
;
2878 trace_field_path_cache
->stream_id
= stream_id
;
2879 trace_field_path_cache
->stream_instance_id
= stream_instance_id
;
2880 BT_PUT(packet_header
);
2884 struct bt_ctf_notif_iter
*bt_ctf_notif_iter_create(struct bt_ctf_trace
*trace
,
2885 size_t max_request_sz
,
2886 struct bt_ctf_notif_iter_medium_ops medops
,
2887 void *data
, FILE *err_stream
)
2890 struct bt_ctf_notif_iter
*notit
= NULL
;
2891 struct bt_ctf_btr_cbs cbs
= {
2893 .signed_int
= btr_signed_int_cb
,
2894 .unsigned_int
= btr_unsigned_int_cb
,
2895 .floating_point
= btr_floating_point_cb
,
2896 .string_begin
= btr_string_begin_cb
,
2897 .string
= btr_string_cb
,
2898 .string_end
= btr_string_end_cb
,
2899 .compound_begin
= btr_compound_begin_cb
,
2900 .compound_end
= btr_compound_end_cb
,
2903 .get_sequence_length
= btr_get_sequence_length_cb
,
2904 .get_variant_type
= btr_get_variant_type_cb
,
2909 assert(medops
.request_bytes
);
2910 assert(medops
.get_stream
);
2911 BT_LOGD("Creating CTF plugin notification iterator: "
2912 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
2914 trace
, bt_ctf_trace_get_name(trace
), max_request_sz
, data
);
2915 notit
= g_new0(struct bt_ctf_notif_iter
, 1);
2917 BT_LOGE_STR("Failed to allocate one CTF plugin notification iterator.");
2920 notit
->clock_states
= g_hash_table_new_full(g_direct_hash
,
2921 g_direct_equal
, bt_put
, g_free
);
2922 if (!notit
->clock_states
) {
2923 BT_LOGE_STR("Failed to allocate a GHashTable.");
2926 ret
= init_clock_states(notit
->clock_states
, trace
);
2928 BT_LOGW("Cannot initialize clock values.");
2931 notit
->meta
.trace
= bt_get(trace
);
2932 notit
->medium
.medops
= medops
;
2933 notit
->medium
.max_request_sz
= max_request_sz
;
2934 notit
->medium
.data
= data
;
2935 notit
->err_stream
= err_stream
;
2936 notit
->stack
= stack_new(notit
);
2937 if (!notit
->stack
) {
2938 BT_LOGE_STR("Failed to create field stack.");
2942 notit
->btr
= bt_ctf_btr_create(cbs
, notit
, err_stream
);
2944 BT_LOGE_STR("Failed to create binary type reader (BTR).");
2948 bt_ctf_notif_iter_reset(notit
);
2949 init_trace_field_path_cache(trace
, ¬it
->trace_field_path_cache
);
2950 notit
->sc_field_path_caches
= g_hash_table_new_full(g_direct_hash
,
2951 g_direct_equal
, bt_put
, g_free
);
2952 if (!notit
->sc_field_path_caches
) {
2953 BT_LOGE_STR("Failed to allocate a GHashTable.");
2957 notit
->field_overrides
= g_hash_table_new_full(g_direct_hash
,
2958 g_direct_equal
, bt_put
, g_free
);
2959 if (!notit
->field_overrides
) {
2960 BT_LOGE_STR("Failed to allocate a GHashTable.");
2964 BT_LOGD("Created CTF plugin notification iterator: "
2965 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
2966 "data=%p, notit-addr=%p",
2967 trace
, bt_ctf_trace_get_name(trace
), max_request_sz
, data
,
2974 bt_ctf_notif_iter_destroy(notit
);
2979 void bt_ctf_notif_iter_destroy(struct bt_ctf_notif_iter
*notit
)
2981 BT_PUT(notit
->meta
.trace
);
2982 BT_PUT(notit
->meta
.stream_class
);
2983 BT_PUT(notit
->meta
.event_class
);
2984 BT_PUT(notit
->packet
);
2985 BT_PUT(notit
->cur_timestamp_end
);
2986 put_all_dscopes(notit
);
2988 BT_LOGD("Destroying CTF plugin notification iterator: addr=%p", notit
);
2991 BT_LOGD_STR("Destroying field stack.");
2992 stack_destroy(notit
->stack
);
2996 BT_LOGD("Destroying BTR: btr-addr=%p", notit
->btr
);
2997 bt_ctf_btr_destroy(notit
->btr
);
3000 if (notit
->clock_states
) {
3001 g_hash_table_destroy(notit
->clock_states
);
3004 if (notit
->sc_field_path_caches
) {
3005 g_hash_table_destroy(notit
->sc_field_path_caches
);
3008 if (notit
->field_overrides
) {
3009 g_hash_table_destroy(notit
->field_overrides
);
3015 enum bt_ctf_notif_iter_status
bt_ctf_notif_iter_get_next_notification(
3016 struct bt_ctf_notif_iter
*notit
,
3017 struct bt_clock_class_priority_map
*cc_prio_map
,
3018 struct bt_notification
**notification
)
3020 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
3023 assert(notification
);
3025 BT_LOGV("Getting next notification: notit-addr=%p, cc-prio-map-addr=%p",
3026 notit
, cc_prio_map
);
3029 status
= handle_state(notit
);
3030 if (status
== BT_CTF_NOTIF_ITER_STATUS_AGAIN
) {
3031 BT_LOGV_STR("Medium returned BT_CTF_NOTIF_ITER_STATUS_AGAIN.");
3034 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
3035 if (status
== BT_CTF_NOTIF_ITER_STATUS_EOF
) {
3036 BT_LOGV_STR("Medium returned BT_CTF_NOTIF_ITER_STATUS_EOF.");
3038 BT_LOGW("Cannot handle state: "
3039 "notit-addr=%p, state=%s",
3040 notit
, state_string(notit
->state
));
3045 switch (notit
->state
) {
3046 case STATE_EMIT_NOTIF_NEW_PACKET
:
3047 /* notify_new_packet() logs errors */
3048 notify_new_packet(notit
, notification
);
3049 if (!*notification
) {
3050 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
3053 case STATE_EMIT_NOTIF_EVENT
:
3054 /* notify_event() logs errors */
3055 notify_event(notit
, cc_prio_map
, notification
);
3056 if (!*notification
) {
3057 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
3060 case STATE_EMIT_NOTIF_END_OF_PACKET
:
3061 /* Update clock with timestamp_end field. */
3062 if (notit
->cur_timestamp_end
) {
3063 enum bt_ctf_btr_status btr_status
;
3064 struct bt_ctf_field_type
*field_type
=
3065 bt_ctf_field_get_type(
3066 notit
->cur_timestamp_end
);
3069 btr_status
= update_clock(notit
,
3070 notit
->cur_timestamp_end
);
3072 if (btr_status
!= BT_CTF_BTR_STATUS_OK
) {
3073 BT_LOGW("Cannot update stream's clock value: "
3074 "notit-addr=%p", notit
);
3075 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
3080 /* notify_end_of_packet() logs errors */
3081 notify_end_of_packet(notit
, notification
);
3082 if (!*notification
) {
3083 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
3087 /* Non-emitting state: continue */
3097 enum bt_ctf_notif_iter_status
bt_ctf_notif_iter_get_packet_header_context_fields(
3098 struct bt_ctf_notif_iter
*notit
,
3099 struct bt_ctf_field
**packet_header_field
,
3100 struct bt_ctf_field
**packet_context_field
)
3102 enum bt_ctf_notif_iter_status status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
3106 if (notit
->state
== STATE_EMIT_NOTIF_NEW_PACKET
) {
3107 /* We're already there */
3112 status
= handle_state(notit
);
3113 if (status
== BT_CTF_NOTIF_ITER_STATUS_AGAIN
) {
3114 BT_LOGV_STR("Medium returned BT_CTF_NOTIF_ITER_STATUS_AGAIN.");
3117 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
3118 if (status
== BT_CTF_NOTIF_ITER_STATUS_EOF
) {
3119 BT_LOGV_STR("Medium returned BT_CTF_NOTIF_ITER_STATUS_EOF.");
3121 BT_LOGW("Cannot handle state: "
3122 "notit-addr=%p, state=%s",
3123 notit
, state_string(notit
->state
));
3128 switch (notit
->state
) {
3129 case STATE_EMIT_NOTIF_NEW_PACKET
:
3131 * Packet header and context fields are
3132 * potentially decoded (or they don't exist).
3136 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN
:
3137 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE
:
3138 case STATE_AFTER_TRACE_PACKET_HEADER
:
3139 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN
:
3140 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE
:
3141 case STATE_AFTER_STREAM_PACKET_CONTEXT
:
3142 /* Non-emitting state: continue */
3146 * We should never get past the
3147 * STATE_EMIT_NOTIF_NEW_PACKET state.
3149 BT_LOGF("Unexpected state: notit-addr=%p, state=%s",
3150 notit
, state_string(notit
->state
));
3156 if (packet_header_field
) {
3157 *packet_header_field
= bt_get(notit
->dscopes
.trace_packet_header
);
3160 if (packet_context_field
) {
3161 *packet_context_field
= bt_get(notit
->dscopes
.stream_packet_context
);