79de791cfba17928d58ab1b3f7ea55cfd0b594ee
[babeltrace.git] / src / plugins / ctf / common / msg-iter / msg-iter.cpp
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright (c) 2015-2018 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2015-2018 Philippe Proulx <pproulx@efficios.com>
6 *
7 * Babeltrace - CTF message iterator
8 */
9
10 #include <glib.h>
11 #include <inttypes.h>
12 #include <stddef.h>
13 #include <stdint.h>
14 #include <stdio.h>
15
16 #include <babeltrace2/babeltrace.h>
17
18 #define BT_COMP_LOG_SELF_COMP (msg_it->self_comp)
19 #define BT_LOG_OUTPUT_LEVEL (msg_it->log_level)
20 #define BT_LOG_TAG "PLUGIN/CTF/MSG-ITER"
21 #include "logging/comp-logging.h"
22
23 #include "common/assert.h"
24 #include "common/common.h"
25
26 #include "../bfcr/bfcr.hpp"
27 #include "msg-iter.hpp"
28 #include "plugins/ctf/common/metadata/ctf-meta.hpp"
29
30 /* A visit stack entry */
31 struct stack_entry
32 {
33 /*
34 * Current base field, one of:
35 *
36 * * string
37 * * structure
38 * * array
39 * * sequence
40 * * variant
41 *
42 * Field is borrowed.
43 */
44 bt_field *base;
45
46 /* Index of next field to set */
47 size_t index;
48 };
49
50 /* Visit stack */
51 struct stack
52 {
53 struct ctf_msg_iter *msg_it;
54
55 /* Entries (struct stack_entry) */
56 GArray *entries;
57
58 /* Number of active entries */
59 size_t size;
60 };
61
62 /* State */
63 enum state
64 {
65 STATE_INIT,
66 STATE_SWITCH_PACKET,
67 STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN,
68 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE,
69 STATE_AFTER_TRACE_PACKET_HEADER,
70 STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN,
71 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE,
72 STATE_AFTER_STREAM_PACKET_CONTEXT,
73 STATE_EMIT_MSG_STREAM_BEGINNING,
74 STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS,
75 STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS,
76 STATE_EMIT_MSG_DISCARDED_EVENTS,
77 STATE_EMIT_MSG_DISCARDED_PACKETS,
78 STATE_EMIT_MSG_PACKET_BEGINNING,
79 STATE_DSCOPE_EVENT_HEADER_BEGIN,
80 STATE_DSCOPE_EVENT_HEADER_CONTINUE,
81 STATE_AFTER_EVENT_HEADER,
82 STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN,
83 STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE,
84 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN,
85 STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE,
86 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN,
87 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE,
88 STATE_EMIT_MSG_EVENT,
89 STATE_EMIT_QUEUED_MSG_EVENT,
90 STATE_SKIP_PACKET_PADDING,
91 STATE_EMIT_MSG_PACKET_END_MULTI,
92 STATE_EMIT_MSG_PACKET_END_SINGLE,
93 STATE_EMIT_QUEUED_MSG_PACKET_END,
94 STATE_CHECK_EMIT_MSG_STREAM_END,
95 STATE_EMIT_MSG_STREAM_END,
96 STATE_DONE,
97 };
98
99 struct end_of_packet_snapshots
100 {
101 uint64_t discarded_events;
102 uint64_t packets;
103 uint64_t beginning_clock;
104 uint64_t end_clock;
105 };
106
107 /* CTF message iterator */
108 struct ctf_msg_iter
109 {
110 /* Visit stack */
111 struct stack *stack;
112
113 /* Current message iterator to create messages (weak) */
114 bt_self_message_iterator *self_msg_iter;
115
116 /*
117 * True if library objects are unavailable during the decoding and
118 * should not be created/used.
119 */
120 bool dry_run;
121
122 /*
123 * Current dynamic scope field pointer.
124 *
125 * This is set by read_dscope_begin_state() and contains the
126 * value of one of the pointers in `dscopes` below.
127 */
128 bt_field *cur_dscope_field;
129
130 /*
131 * True if we're done filling a string field from a text
132 * array/sequence payload.
133 */
134 bool done_filling_string;
135
136 /* Trace and classes */
137 /* True to set IR fields */
138 bool set_ir_fields;
139
140 struct
141 {
142 struct ctf_trace_class *tc;
143 struct ctf_stream_class *sc;
144 struct ctf_event_class *ec;
145 } meta;
146
147 /* Current packet (NULL if not created yet) */
148 bt_packet *packet;
149
150 /* Current stream (NULL if not set yet) */
151 bt_stream *stream;
152
153 /* Current event (NULL if not created yet) */
154 bt_event *event;
155
156 /* Current event message (NULL if not created yet) */
157 bt_message *event_msg;
158
159 /*
160 * True if we need to emit a packet beginning message before we emit
161 * the next event message or the packet end message.
162 */
163 bool emit_delayed_packet_beginning_msg;
164
165 /*
166 * True if this is the first packet we are reading, and therefore if we
167 * should emit a stream beginning message.
168 */
169 bool emit_stream_beginning_message;
170
171 /*
172 * True if we need to emit a stream end message at the end of the
173 * current stream. A live stream may never receive any data and thus
174 * never send a stream beginning message which removes the need to emit
175 * a stream end message.
176 */
177 bool emit_stream_end_message;
178
179 /* Database of current dynamic scopes */
180 struct
181 {
182 bt_field *stream_packet_context;
183 bt_field *event_common_context;
184 bt_field *event_spec_context;
185 bt_field *event_payload;
186 } dscopes;
187
188 /* Current state */
189 enum state state;
190
191 /* Current medium buffer data */
192 struct
193 {
194 /* Last address provided by medium */
195 const uint8_t *addr;
196
197 /* Buffer size provided by medium (bytes) */
198 size_t sz;
199
200 /* Offset within whole packet of addr (bits) */
201 size_t packet_offset;
202
203 /* Current position from addr (bits) */
204 size_t at;
205
206 /* Position of the last event header from addr (bits) */
207 size_t last_eh_at;
208 } buf;
209
210 /* Binary type reader */
211 struct bt_bfcr *bfcr;
212
213 /* Current medium data */
214 struct
215 {
216 struct ctf_msg_iter_medium_ops medops;
217 size_t max_request_sz;
218 void *data;
219 } medium;
220
221 /* Current packet size (bits) (-1 if unknown) */
222 int64_t cur_exp_packet_total_size;
223
224 /* Current content size (bits) (-1 if unknown) */
225 int64_t cur_exp_packet_content_size;
226
227 /* Current stream class ID */
228 int64_t cur_stream_class_id;
229
230 /* Current event class ID */
231 int64_t cur_event_class_id;
232
233 /* Current data stream ID */
234 int64_t cur_data_stream_id;
235
236 /*
237 * Offset, in the underlying media, of the current packet's
238 * start (-1 if unknown).
239 */
240 off_t cur_packet_offset;
241
242 /* Default clock's current value */
243 uint64_t default_clock_snapshot;
244
245 /* End of current packet snapshots */
246 struct end_of_packet_snapshots snapshots;
247
248 /* End of previous packet snapshots */
249 struct end_of_packet_snapshots prev_packet_snapshots;
250
251 /* Stored values (for sequence lengths, variant tags) */
252 GArray *stored_values;
253
254 /* Iterator's current log level */
255 bt_logging_level log_level;
256
257 /* Iterator's owning self component, or `NULL` if none (query) */
258 bt_self_component *self_comp;
259 };
260
261 static inline const char *state_string(enum state state)
262 {
263 switch (state) {
264 case STATE_INIT:
265 return "INIT";
266 case STATE_SWITCH_PACKET:
267 return "SWITCH_PACKET";
268 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
269 return "DSCOPE_TRACE_PACKET_HEADER_BEGIN";
270 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
271 return "DSCOPE_TRACE_PACKET_HEADER_CONTINUE";
272 case STATE_AFTER_TRACE_PACKET_HEADER:
273 return "AFTER_TRACE_PACKET_HEADER";
274 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN:
275 return "DSCOPE_STREAM_PACKET_CONTEXT_BEGIN";
276 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE:
277 return "DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE";
278 case STATE_AFTER_STREAM_PACKET_CONTEXT:
279 return "AFTER_STREAM_PACKET_CONTEXT";
280 case STATE_EMIT_MSG_STREAM_BEGINNING:
281 return "EMIT_MSG_STREAM_BEGINNING";
282 case STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS:
283 return "CHECK_EMIT_MSG_DISCARDED_EVENTS";
284 case STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS:
285 return "CHECK_EMIT_MSG_DISCARDED_PACKETS";
286 case STATE_EMIT_MSG_PACKET_BEGINNING:
287 return "EMIT_MSG_PACKET_BEGINNING";
288 case STATE_EMIT_MSG_DISCARDED_EVENTS:
289 return "EMIT_MSG_DISCARDED_EVENTS";
290 case STATE_EMIT_MSG_DISCARDED_PACKETS:
291 return "EMIT_MSG_DISCARDED_PACKETS";
292 case STATE_DSCOPE_EVENT_HEADER_BEGIN:
293 return "DSCOPE_EVENT_HEADER_BEGIN";
294 case STATE_DSCOPE_EVENT_HEADER_CONTINUE:
295 return "DSCOPE_EVENT_HEADER_CONTINUE";
296 case STATE_AFTER_EVENT_HEADER:
297 return "AFTER_EVENT_HEADER";
298 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN:
299 return "DSCOPE_EVENT_COMMON_CONTEXT_BEGIN";
300 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE:
301 return "DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE";
302 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN:
303 return "DSCOPE_EVENT_SPEC_CONTEXT_BEGIN";
304 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE:
305 return "DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE";
306 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN:
307 return "DSCOPE_EVENT_PAYLOAD_BEGIN";
308 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE:
309 return "DSCOPE_EVENT_PAYLOAD_CONTINUE";
310 case STATE_EMIT_MSG_EVENT:
311 return "EMIT_MSG_EVENT";
312 case STATE_EMIT_QUEUED_MSG_EVENT:
313 return "EMIT_QUEUED_MSG_EVENT";
314 case STATE_SKIP_PACKET_PADDING:
315 return "SKIP_PACKET_PADDING";
316 case STATE_EMIT_MSG_PACKET_END_MULTI:
317 return "EMIT_MSG_PACKET_END_MULTI";
318 case STATE_EMIT_MSG_PACKET_END_SINGLE:
319 return "EMIT_MSG_PACKET_END_SINGLE";
320 case STATE_EMIT_QUEUED_MSG_PACKET_END:
321 return "EMIT_QUEUED_MSG_PACKET_END";
322 case STATE_CHECK_EMIT_MSG_STREAM_END:
323 return "CHECK_EMIT_MSG_STREAM_END";
324 case STATE_EMIT_MSG_STREAM_END:
325 return "EMIT_MSG_STREAM_END";
326 case STATE_DONE:
327 return "DONE";
328 }
329
330 bt_common_abort();
331 }
332
333 static struct stack *stack_new(struct ctf_msg_iter *msg_it)
334 {
335 bt_self_component *self_comp = msg_it->self_comp;
336 struct stack *stack = NULL;
337
338 stack = g_new0(struct stack, 1);
339 if (!stack) {
340 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Failed to allocate one stack.");
341 goto error;
342 }
343
344 stack->msg_it = msg_it;
345 stack->entries = g_array_new(FALSE, TRUE, sizeof(struct stack_entry));
346 if (!stack->entries) {
347 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Failed to allocate a GArray.");
348 goto error;
349 }
350
351 BT_COMP_LOGD("Created stack: msg-it-addr=%p, stack-addr=%p", msg_it, stack);
352 goto end;
353
354 error:
355 g_free(stack);
356 stack = NULL;
357
358 end:
359 return stack;
360 }
361
362 static void stack_destroy(struct stack *stack)
363 {
364 struct ctf_msg_iter *msg_it;
365
366 BT_ASSERT_DBG(stack);
367 msg_it = stack->msg_it;
368 BT_COMP_LOGD("Destroying stack: addr=%p", stack);
369
370 if (stack->entries) {
371 g_array_free(stack->entries, TRUE);
372 }
373
374 g_free(stack);
375 }
376
377 static void stack_push(struct stack *stack, bt_field *base)
378 {
379 struct stack_entry *entry;
380 struct ctf_msg_iter *msg_it;
381
382 BT_ASSERT_DBG(stack);
383 msg_it = stack->msg_it;
384 BT_ASSERT_DBG(base);
385 BT_COMP_LOGT("Pushing base field on stack: stack-addr=%p, "
386 "stack-size-before=%zu, stack-size-after=%zu",
387 stack, stack->size, stack->size + 1);
388
389 if (stack->entries->len == stack->size) {
390 g_array_set_size(stack->entries, stack->size + 1);
391 }
392
393 entry = &bt_g_array_index(stack->entries, struct stack_entry, stack->size);
394 entry->base = base;
395 entry->index = 0;
396 stack->size++;
397 }
398
399 static inline unsigned int stack_size(struct stack *stack)
400 {
401 BT_ASSERT_DBG(stack);
402 return stack->size;
403 }
404
405 static void stack_pop(struct stack *stack)
406 {
407 struct ctf_msg_iter *msg_it;
408
409 BT_ASSERT_DBG(stack);
410 BT_ASSERT_DBG(stack_size(stack));
411 msg_it = stack->msg_it;
412 BT_COMP_LOGT("Popping from stack: "
413 "stack-addr=%p, stack-size-before=%zu, stack-size-after=%zu",
414 stack, stack->size, stack->size - 1);
415 stack->size--;
416 }
417
418 static inline struct stack_entry *stack_top(struct stack *stack)
419 {
420 BT_ASSERT_DBG(stack);
421 BT_ASSERT_DBG(stack_size(stack));
422 return &bt_g_array_index(stack->entries, struct stack_entry, stack->size - 1);
423 }
424
425 static inline bool stack_empty(struct stack *stack)
426 {
427 return stack_size(stack) == 0;
428 }
429
430 static void stack_clear(struct stack *stack)
431 {
432 BT_ASSERT_DBG(stack);
433 stack->size = 0;
434 }
435
436 static inline enum ctf_msg_iter_status
437 msg_iter_status_from_m_status(enum ctf_msg_iter_medium_status m_status)
438 {
439 /* They are the same */
440 return (ctf_msg_iter_status) m_status;
441 }
442
443 static inline size_t buf_size_bits(struct ctf_msg_iter *msg_it)
444 {
445 return msg_it->buf.sz * 8;
446 }
447
448 static inline size_t buf_available_bits(struct ctf_msg_iter *msg_it)
449 {
450 return buf_size_bits(msg_it) - msg_it->buf.at;
451 }
452
453 static inline size_t packet_at(struct ctf_msg_iter *msg_it)
454 {
455 return msg_it->buf.packet_offset + msg_it->buf.at;
456 }
457
458 static inline void buf_consume_bits(struct ctf_msg_iter *msg_it, size_t incr)
459 {
460 BT_COMP_LOGT("Advancing cursor: msg-it-addr=%p, cur-before=%zu, cur-after=%zu", msg_it,
461 msg_it->buf.at, msg_it->buf.at + incr);
462 msg_it->buf.at += incr;
463 }
464
465 static enum ctf_msg_iter_status request_medium_bytes(struct ctf_msg_iter *msg_it)
466 {
467 bt_self_component *self_comp = msg_it->self_comp;
468 uint8_t *buffer_addr = NULL;
469 size_t buffer_sz = 0;
470 enum ctf_msg_iter_medium_status m_status;
471
472 BT_COMP_LOGD("Calling user function (request bytes): msg-it-addr=%p, "
473 "request-size=%zu",
474 msg_it, msg_it->medium.max_request_sz);
475 m_status = msg_it->medium.medops.request_bytes(msg_it->medium.max_request_sz, &buffer_addr,
476 &buffer_sz, msg_it->medium.data);
477 BT_COMP_LOGD("User function returned: status=%s, buf-addr=%p, buf-size=%zu",
478 ctf_msg_iter_medium_status_string(m_status), buffer_addr, buffer_sz);
479 if (m_status == CTF_MSG_ITER_MEDIUM_STATUS_OK) {
480 BT_ASSERT(buffer_sz != 0);
481
482 /* New packet offset is old one + old size (in bits) */
483 msg_it->buf.packet_offset += buf_size_bits(msg_it);
484
485 /* Restart at the beginning of the new medium buffer */
486 msg_it->buf.at = 0;
487 msg_it->buf.last_eh_at = SIZE_MAX;
488
489 /* New medium buffer size */
490 msg_it->buf.sz = buffer_sz;
491
492 /* New medium buffer address */
493 msg_it->buf.addr = buffer_addr;
494
495 BT_COMP_LOGD("User function returned new bytes: "
496 "packet-offset=%zu, cur=%zu, size=%zu, addr=%p",
497 msg_it->buf.packet_offset, msg_it->buf.at, msg_it->buf.sz, msg_it->buf.addr);
498 BT_COMP_LOGT_MEM(buffer_addr, buffer_sz, "Returned bytes at %p:", buffer_addr);
499 } else if (m_status == CTF_MSG_ITER_MEDIUM_STATUS_EOF) {
500 /*
501 * User returned end of stream: validate that we're not
502 * in the middle of a packet header, packet context, or
503 * event.
504 */
505 if (msg_it->cur_exp_packet_total_size >= 0) {
506 if (packet_at(msg_it) == msg_it->cur_exp_packet_total_size) {
507 goto end;
508 }
509 } else {
510 if (packet_at(msg_it) == 0) {
511 goto end;
512 }
513
514 if (msg_it->buf.last_eh_at != SIZE_MAX && msg_it->buf.at == msg_it->buf.last_eh_at) {
515 goto end;
516 }
517 }
518
519 /* All other states are invalid */
520 BT_COMP_LOGE_APPEND_CAUSE(
521 self_comp,
522 "User function returned %s, but message iterator is in an unexpected state: "
523 "state=%s, cur-packet-size=%" PRId64 ", cur=%zu, "
524 "packet-cur=%zu, last-eh-at=%zu",
525 ctf_msg_iter_medium_status_string(m_status), state_string(msg_it->state),
526 msg_it->cur_exp_packet_total_size, msg_it->buf.at, packet_at(msg_it),
527 msg_it->buf.last_eh_at);
528 m_status = CTF_MSG_ITER_MEDIUM_STATUS_ERROR;
529 } else if (m_status < 0) {
530 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
531 "User function failed: "
532 "status=%s",
533 ctf_msg_iter_medium_status_string(m_status));
534 }
535
536 end:
537 return msg_iter_status_from_m_status(m_status);
538 }
539
540 static inline enum ctf_msg_iter_status buf_ensure_available_bits(struct ctf_msg_iter *msg_it)
541 {
542 enum ctf_msg_iter_status status = CTF_MSG_ITER_STATUS_OK;
543
544 if (G_UNLIKELY(buf_available_bits(msg_it) == 0)) {
545 /*
546 * This _cannot_ return CTF_MSG_ITER_STATUS_OK
547 * _and_ no bits.
548 */
549 status = request_medium_bytes(msg_it);
550 }
551
552 return status;
553 }
554
555 static enum ctf_msg_iter_status
556 read_dscope_begin_state(struct ctf_msg_iter *msg_it, struct ctf_field_class *dscope_fc,
557 enum state done_state, enum state continue_state, bt_field *dscope_field)
558 {
559 enum ctf_msg_iter_status status = CTF_MSG_ITER_STATUS_OK;
560 bt_self_component *self_comp = msg_it->self_comp;
561 enum bt_bfcr_status bfcr_status;
562 size_t consumed_bits;
563
564 msg_it->cur_dscope_field = dscope_field;
565 BT_COMP_LOGT("Starting BFCR: msg-it-addr=%p, bfcr-addr=%p, fc-addr=%p", msg_it, msg_it->bfcr,
566 dscope_fc);
567 consumed_bits = bt_bfcr_start(msg_it->bfcr, dscope_fc, msg_it->buf.addr, msg_it->buf.at,
568 packet_at(msg_it), msg_it->buf.sz, &bfcr_status);
569 BT_COMP_LOGT("BFCR consumed bits: size=%zu", consumed_bits);
570
571 switch (bfcr_status) {
572 case BT_BFCR_STATUS_OK:
573 /* Field class was read completely */
574 BT_COMP_LOGT_STR("Field was completely decoded.");
575 msg_it->state = done_state;
576 break;
577 case BT_BFCR_STATUS_EOF:
578 BT_COMP_LOGT_STR("BFCR needs more data to decode field completely.");
579 msg_it->state = continue_state;
580 break;
581 default:
582 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
583 "BFCR failed to start: msg-it-addr=%p, bfcr-addr=%p, "
584 "status=%s",
585 msg_it, msg_it->bfcr, bt_bfcr_status_string(bfcr_status));
586 status = CTF_MSG_ITER_STATUS_ERROR;
587 goto end;
588 }
589
590 /* Consume bits now since we know we're not in an error state */
591 buf_consume_bits(msg_it, consumed_bits);
592
593 end:
594 return status;
595 }
596
597 static enum ctf_msg_iter_status read_dscope_continue_state(struct ctf_msg_iter *msg_it,
598 enum state done_state)
599 {
600 enum ctf_msg_iter_status status = CTF_MSG_ITER_STATUS_OK;
601 bt_self_component *self_comp = msg_it->self_comp;
602 enum bt_bfcr_status bfcr_status;
603 size_t consumed_bits;
604
605 BT_COMP_LOGT("Continuing BFCR: msg-it-addr=%p, bfcr-addr=%p", msg_it, msg_it->bfcr);
606
607 status = buf_ensure_available_bits(msg_it);
608 if (status != CTF_MSG_ITER_STATUS_OK) {
609 if (status < 0) {
610 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
611 "Cannot ensure that buffer has at least one byte: "
612 "msg-addr=%p, status=%s",
613 msg_it, ctf_msg_iter_status_string(status));
614 } else {
615 BT_COMP_LOGT("Cannot ensure that buffer has at least one byte: "
616 "msg-addr=%p, status=%s",
617 msg_it, ctf_msg_iter_status_string(status));
618 }
619
620 goto end;
621 }
622
623 consumed_bits = bt_bfcr_continue(msg_it->bfcr, msg_it->buf.addr, msg_it->buf.sz, &bfcr_status);
624 BT_COMP_LOGT("BFCR consumed bits: size=%zu", consumed_bits);
625
626 switch (bfcr_status) {
627 case BT_BFCR_STATUS_OK:
628 /* Type was read completely. */
629 BT_COMP_LOGT_STR("Field was completely decoded.");
630 msg_it->state = done_state;
631 break;
632 case BT_BFCR_STATUS_EOF:
633 /* Stay in this continue state. */
634 BT_COMP_LOGT_STR("BFCR needs more data to decode field completely.");
635 break;
636 default:
637 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
638 "BFCR failed to continue: msg-it-addr=%p, bfcr-addr=%p, "
639 "status=%s",
640 msg_it, msg_it->bfcr, bt_bfcr_status_string(bfcr_status));
641 status = CTF_MSG_ITER_STATUS_ERROR;
642 goto end;
643 }
644
645 /* Consume bits now since we know we're not in an error state. */
646 buf_consume_bits(msg_it, consumed_bits);
647 end:
648 return status;
649 }
650
651 static void release_event_dscopes(struct ctf_msg_iter *msg_it)
652 {
653 msg_it->dscopes.event_common_context = NULL;
654 msg_it->dscopes.event_spec_context = NULL;
655 msg_it->dscopes.event_payload = NULL;
656 }
657
658 static void release_all_dscopes(struct ctf_msg_iter *msg_it)
659 {
660 msg_it->dscopes.stream_packet_context = NULL;
661
662 release_event_dscopes(msg_it);
663 }
664
665 static enum ctf_msg_iter_status switch_packet_state(struct ctf_msg_iter *msg_it)
666 {
667 enum ctf_msg_iter_status status;
668 bt_self_component *self_comp = msg_it->self_comp;
669
670 /*
671 * We don't put the stream class here because we need to make
672 * sure that all the packets processed by the same message
673 * iterator refer to the same stream class (the first one).
674 */
675 BT_ASSERT(msg_it);
676
677 if (msg_it->cur_exp_packet_total_size != -1) {
678 msg_it->cur_packet_offset += msg_it->cur_exp_packet_total_size;
679 }
680
681 BT_COMP_LOGD("Switching packet: msg-it-addr=%p, cur=%zu, "
682 "packet-offset=%" PRId64,
683 msg_it, msg_it->buf.at, msg_it->cur_packet_offset);
684 stack_clear(msg_it->stack);
685 msg_it->meta.ec = NULL;
686 BT_PACKET_PUT_REF_AND_RESET(msg_it->packet);
687 BT_MESSAGE_PUT_REF_AND_RESET(msg_it->event_msg);
688 release_all_dscopes(msg_it);
689 msg_it->cur_dscope_field = NULL;
690
691 if (msg_it->medium.medops.switch_packet) {
692 enum ctf_msg_iter_medium_status medium_status;
693
694 medium_status = msg_it->medium.medops.switch_packet(msg_it->medium.data);
695 if (medium_status == CTF_MSG_ITER_MEDIUM_STATUS_EOF) {
696 /* No more packets. */
697 msg_it->state = STATE_CHECK_EMIT_MSG_STREAM_END;
698 status = CTF_MSG_ITER_STATUS_OK;
699 goto end;
700 } else if (medium_status != CTF_MSG_ITER_MEDIUM_STATUS_OK) {
701 status = msg_iter_status_from_m_status(medium_status);
702 goto end;
703 }
704
705 /*
706 * After the packet switch, the medium might want to give us a
707 * different buffer for the new packet.
708 */
709 status = request_medium_bytes(msg_it);
710 if (status != CTF_MSG_ITER_STATUS_OK) {
711 goto end;
712 }
713 }
714
715 /*
716 * Adjust current buffer so that addr points to the beginning of the new
717 * packet.
718 */
719 if (msg_it->buf.addr) {
720 size_t consumed_bytes = (size_t) (msg_it->buf.at / CHAR_BIT);
721
722 /* Packets are assumed to start on a byte frontier. */
723 if (msg_it->buf.at % CHAR_BIT) {
724 BT_COMP_LOGE_APPEND_CAUSE(
725 self_comp,
726 "Cannot switch packet: current position is not a multiple of 8: "
727 "msg-it-addr=%p, cur=%zu",
728 msg_it, msg_it->buf.at);
729 status = CTF_MSG_ITER_STATUS_ERROR;
730 goto end;
731 }
732
733 msg_it->buf.addr += consumed_bytes;
734 msg_it->buf.sz -= consumed_bytes;
735 msg_it->buf.at = 0;
736 msg_it->buf.packet_offset = 0;
737 BT_COMP_LOGD("Adjusted buffer: addr=%p, size=%zu", msg_it->buf.addr, msg_it->buf.sz);
738 }
739
740 msg_it->cur_exp_packet_content_size = -1;
741 msg_it->cur_exp_packet_total_size = -1;
742 msg_it->cur_stream_class_id = -1;
743 msg_it->cur_event_class_id = -1;
744 msg_it->cur_data_stream_id = -1;
745 msg_it->prev_packet_snapshots = msg_it->snapshots;
746 msg_it->snapshots.discarded_events = UINT64_C(-1);
747 msg_it->snapshots.packets = UINT64_C(-1);
748 msg_it->snapshots.beginning_clock = UINT64_C(-1);
749 msg_it->snapshots.end_clock = UINT64_C(-1);
750 msg_it->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
751
752 status = CTF_MSG_ITER_STATUS_OK;
753 end:
754 return status;
755 }
756
757 static enum ctf_msg_iter_status read_packet_header_begin_state(struct ctf_msg_iter *msg_it)
758 {
759 struct ctf_field_class *packet_header_fc = NULL;
760 bt_self_component *self_comp = msg_it->self_comp;
761 enum ctf_msg_iter_status status = CTF_MSG_ITER_STATUS_OK;
762
763 /*
764 * Make sure at least one bit is available for this packet. An
765 * empty packet is impossible. If we reach the end of the medium
766 * at this point, then it's considered the end of the stream.
767 */
768 status = buf_ensure_available_bits(msg_it);
769 switch (status) {
770 case CTF_MSG_ITER_STATUS_OK:
771 break;
772 case CTF_MSG_ITER_STATUS_EOF:
773 status = CTF_MSG_ITER_STATUS_OK;
774 msg_it->state = STATE_CHECK_EMIT_MSG_STREAM_END;
775 goto end;
776 default:
777 goto end;
778 }
779
780 /* Packet header class is common to the whole trace class. */
781 packet_header_fc = msg_it->meta.tc->packet_header_fc;
782 if (!packet_header_fc) {
783 msg_it->state = STATE_AFTER_TRACE_PACKET_HEADER;
784 goto end;
785 }
786
787 msg_it->cur_stream_class_id = -1;
788 msg_it->cur_event_class_id = -1;
789 msg_it->cur_data_stream_id = -1;
790 BT_COMP_LOGD("Decoding packet header field: "
791 "msg-it-addr=%p, trace-class-addr=%p, fc-addr=%p",
792 msg_it, msg_it->meta.tc, packet_header_fc);
793 status = read_dscope_begin_state(msg_it, packet_header_fc, STATE_AFTER_TRACE_PACKET_HEADER,
794 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE, NULL);
795 if (status < 0) {
796 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
797 "Cannot decode packet header field: "
798 "msg-it-addr=%p, trace-class-addr=%p, "
799 "fc-addr=%p",
800 msg_it, msg_it->meta.tc, packet_header_fc);
801 }
802
803 end:
804 return status;
805 }
806
807 static enum ctf_msg_iter_status read_packet_header_continue_state(struct ctf_msg_iter *msg_it)
808 {
809 return read_dscope_continue_state(msg_it, STATE_AFTER_TRACE_PACKET_HEADER);
810 }
811
812 static inline enum ctf_msg_iter_status set_current_stream_class(struct ctf_msg_iter *msg_it)
813 {
814 enum ctf_msg_iter_status status = CTF_MSG_ITER_STATUS_OK;
815 bt_self_component *self_comp = msg_it->self_comp;
816 struct ctf_stream_class *new_stream_class = NULL;
817
818 if (msg_it->cur_stream_class_id == -1) {
819 /*
820 * No current stream class ID field, therefore only one
821 * stream class.
822 */
823 if (msg_it->meta.tc->stream_classes->len != 1) {
824 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
825 "Need exactly one stream class since there's "
826 "no stream class ID field: "
827 "msg-it-addr=%p",
828 msg_it);
829 status = CTF_MSG_ITER_STATUS_ERROR;
830 goto end;
831 }
832
833 new_stream_class = (ctf_stream_class *) msg_it->meta.tc->stream_classes->pdata[0];
834 msg_it->cur_stream_class_id = new_stream_class->id;
835 }
836
837 new_stream_class =
838 ctf_trace_class_borrow_stream_class_by_id(msg_it->meta.tc, msg_it->cur_stream_class_id);
839 if (!new_stream_class) {
840 BT_COMP_LOGE_APPEND_CAUSE(
841 self_comp,
842 "No stream class with ID of stream class ID to use in trace class: "
843 "msg-it-addr=%p, stream-class-id=%" PRIu64 ", "
844 "trace-class-addr=%p",
845 msg_it, msg_it->cur_stream_class_id, msg_it->meta.tc);
846 status = CTF_MSG_ITER_STATUS_ERROR;
847 goto end;
848 }
849
850 if (msg_it->meta.sc) {
851 if (new_stream_class != msg_it->meta.sc) {
852 BT_COMP_LOGE_APPEND_CAUSE(
853 self_comp,
854 "Two packets refer to two different stream classes within the same packet sequence: "
855 "msg-it-addr=%p, prev-stream-class-addr=%p, "
856 "prev-stream-class-id=%" PRId64 ", "
857 "next-stream-class-addr=%p, "
858 "next-stream-class-id=%" PRId64 ", "
859 "trace-addr=%p",
860 msg_it, msg_it->meta.sc, msg_it->meta.sc->id, new_stream_class,
861 new_stream_class->id, msg_it->meta.tc);
862 status = CTF_MSG_ITER_STATUS_ERROR;
863 goto end;
864 }
865 } else {
866 msg_it->meta.sc = new_stream_class;
867 }
868
869 BT_COMP_LOGD("Set current stream class: "
870 "msg-it-addr=%p, stream-class-addr=%p, "
871 "stream-class-id=%" PRId64,
872 msg_it, msg_it->meta.sc, msg_it->meta.sc->id);
873
874 end:
875 return status;
876 }
877
878 static inline enum ctf_msg_iter_status set_current_stream(struct ctf_msg_iter *msg_it)
879 {
880 enum ctf_msg_iter_status status = CTF_MSG_ITER_STATUS_OK;
881 bt_self_component *self_comp = msg_it->self_comp;
882 bt_stream *stream = NULL;
883
884 BT_COMP_LOGD("Calling user function (get stream): msg-it-addr=%p, "
885 "stream-class-addr=%p, stream-class-id=%" PRId64,
886 msg_it, msg_it->meta.sc, msg_it->meta.sc->id);
887 stream = msg_it->medium.medops.borrow_stream(msg_it->meta.sc->ir_sc, msg_it->cur_data_stream_id,
888 msg_it->medium.data);
889 bt_stream_get_ref(stream);
890 BT_COMP_LOGD("User function returned: stream-addr=%p", stream);
891 if (!stream) {
892 BT_COMP_LOGE_APPEND_CAUSE(
893 self_comp,
894 "User function failed to return a stream object for the given stream class.");
895 status = CTF_MSG_ITER_STATUS_ERROR;
896 goto end;
897 }
898
899 if (msg_it->stream && stream != msg_it->stream) {
900 BT_COMP_LOGE_APPEND_CAUSE(
901 self_comp,
902 "User function returned a different stream than the previous one for the same sequence of packets.");
903 status = CTF_MSG_ITER_STATUS_ERROR;
904 goto end;
905 }
906
907 BT_STREAM_MOVE_REF(msg_it->stream, stream);
908
909 end:
910 bt_stream_put_ref(stream);
911 return status;
912 }
913
914 static inline enum ctf_msg_iter_status set_current_packet(struct ctf_msg_iter *msg_it)
915 {
916 enum ctf_msg_iter_status status = CTF_MSG_ITER_STATUS_OK;
917 bt_self_component *self_comp = msg_it->self_comp;
918 bt_packet *packet = NULL;
919
920 BT_COMP_LOGD("Creating packet from stream: "
921 "msg-it-addr=%p, stream-addr=%p, "
922 "stream-class-addr=%p, "
923 "stream-class-id=%" PRId64,
924 msg_it, msg_it->stream, msg_it->meta.sc, msg_it->meta.sc->id);
925
926 /* Create packet */
927 BT_ASSERT(msg_it->stream);
928 packet = bt_packet_create(msg_it->stream);
929 if (!packet) {
930 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
931 "Cannot create packet from stream: "
932 "msg-it-addr=%p, stream-addr=%p, "
933 "stream-class-addr=%p, "
934 "stream-class-id=%" PRId64,
935 msg_it, msg_it->stream, msg_it->meta.sc, msg_it->meta.sc->id);
936 goto error;
937 }
938
939 goto end;
940
941 error:
942 BT_PACKET_PUT_REF_AND_RESET(packet);
943 status = CTF_MSG_ITER_STATUS_ERROR;
944
945 end:
946 BT_PACKET_MOVE_REF(msg_it->packet, packet);
947 return status;
948 }
949
950 static enum ctf_msg_iter_status after_packet_header_state(struct ctf_msg_iter *msg_it)
951 {
952 enum ctf_msg_iter_status status;
953
954 status = set_current_stream_class(msg_it);
955 if (status != CTF_MSG_ITER_STATUS_OK) {
956 goto end;
957 }
958
959 if (!msg_it->dry_run) {
960 status = set_current_stream(msg_it);
961 if (status != CTF_MSG_ITER_STATUS_OK) {
962 goto end;
963 }
964
965 status = set_current_packet(msg_it);
966 if (status != CTF_MSG_ITER_STATUS_OK) {
967 goto end;
968 }
969 }
970
971 msg_it->state = STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN;
972
973 status = CTF_MSG_ITER_STATUS_OK;
974
975 end:
976 return status;
977 }
978
979 static enum ctf_msg_iter_status read_packet_context_begin_state(struct ctf_msg_iter *msg_it)
980 {
981 enum ctf_msg_iter_status status = CTF_MSG_ITER_STATUS_OK;
982 bt_self_component *self_comp = msg_it->self_comp;
983 struct ctf_field_class *packet_context_fc;
984
985 BT_ASSERT(msg_it->meta.sc);
986 packet_context_fc = msg_it->meta.sc->packet_context_fc;
987 if (!packet_context_fc) {
988 BT_COMP_LOGD("No packet packet context field class in stream class: continuing: "
989 "msg-it-addr=%p, stream-class-addr=%p, "
990 "stream-class-id=%" PRId64,
991 msg_it, msg_it->meta.sc, msg_it->meta.sc->id);
992 msg_it->state = STATE_AFTER_STREAM_PACKET_CONTEXT;
993 goto end;
994 }
995
996 if (packet_context_fc->in_ir && !msg_it->dry_run) {
997 BT_ASSERT(!msg_it->dscopes.stream_packet_context);
998 BT_ASSERT(msg_it->packet);
999 msg_it->dscopes.stream_packet_context = bt_packet_borrow_context_field(msg_it->packet);
1000 BT_ASSERT(msg_it->dscopes.stream_packet_context);
1001 }
1002
1003 BT_COMP_LOGD("Decoding packet context field: "
1004 "msg-it-addr=%p, stream-class-addr=%p, "
1005 "stream-class-id=%" PRId64 ", fc-addr=%p",
1006 msg_it, msg_it->meta.sc, msg_it->meta.sc->id, packet_context_fc);
1007 status = read_dscope_begin_state(msg_it, packet_context_fc, STATE_AFTER_STREAM_PACKET_CONTEXT,
1008 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE,
1009 msg_it->dscopes.stream_packet_context);
1010 if (status < 0) {
1011 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1012 "Cannot decode packet context field: "
1013 "msg-it-addr=%p, stream-class-addr=%p, "
1014 "stream-class-id=%" PRId64 ", fc-addr=%p",
1015 msg_it, msg_it->meta.sc, msg_it->meta.sc->id, packet_context_fc);
1016 }
1017
1018 end:
1019 return status;
1020 }
1021
1022 static enum ctf_msg_iter_status read_packet_context_continue_state(struct ctf_msg_iter *msg_it)
1023 {
1024 return read_dscope_continue_state(msg_it, STATE_AFTER_STREAM_PACKET_CONTEXT);
1025 }
1026
1027 static enum ctf_msg_iter_status set_current_packet_content_sizes(struct ctf_msg_iter *msg_it)
1028 {
1029 enum ctf_msg_iter_status status = CTF_MSG_ITER_STATUS_OK;
1030 bt_self_component *self_comp = msg_it->self_comp;
1031
1032 if (msg_it->cur_exp_packet_total_size == -1) {
1033 if (msg_it->cur_exp_packet_content_size != -1) {
1034 msg_it->cur_exp_packet_total_size = msg_it->cur_exp_packet_content_size;
1035 }
1036 } else {
1037 if (msg_it->cur_exp_packet_content_size == -1) {
1038 msg_it->cur_exp_packet_content_size = msg_it->cur_exp_packet_total_size;
1039 }
1040 }
1041
1042 BT_ASSERT(
1043 (msg_it->cur_exp_packet_total_size >= 0 && msg_it->cur_exp_packet_content_size >= 0) ||
1044 (msg_it->cur_exp_packet_total_size < 0 && msg_it->cur_exp_packet_content_size < 0));
1045
1046 if (msg_it->cur_exp_packet_content_size > msg_it->cur_exp_packet_total_size) {
1047 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1048 "Invalid packet or content size: "
1049 "content size is greater than packet size: "
1050 "msg-it-addr=%p, packet-context-field-addr=%p, "
1051 "packet-size=%" PRId64 ", content-size=%" PRId64,
1052 msg_it, msg_it->dscopes.stream_packet_context,
1053 msg_it->cur_exp_packet_total_size,
1054 msg_it->cur_exp_packet_content_size);
1055 status = CTF_MSG_ITER_STATUS_ERROR;
1056 goto end;
1057 }
1058
1059 BT_COMP_LOGD("Set current packet and content sizes: "
1060 "msg-it-addr=%p, packet-size=%" PRIu64 ", content-size=%" PRIu64,
1061 msg_it, msg_it->cur_exp_packet_total_size, msg_it->cur_exp_packet_content_size);
1062
1063 end:
1064 return status;
1065 }
1066
1067 static enum ctf_msg_iter_status after_packet_context_state(struct ctf_msg_iter *msg_it)
1068 {
1069 enum ctf_msg_iter_status status;
1070
1071 status = set_current_packet_content_sizes(msg_it);
1072 if (status != CTF_MSG_ITER_STATUS_OK) {
1073 goto end;
1074 }
1075
1076 if (msg_it->emit_stream_beginning_message) {
1077 msg_it->state = STATE_EMIT_MSG_STREAM_BEGINNING;
1078 } else {
1079 msg_it->state = STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS;
1080 }
1081
1082 end:
1083 return status;
1084 }
1085
1086 static enum ctf_msg_iter_status read_event_header_begin_state(struct ctf_msg_iter *msg_it)
1087 {
1088 enum ctf_msg_iter_status status = CTF_MSG_ITER_STATUS_OK;
1089 bt_self_component *self_comp = msg_it->self_comp;
1090 struct ctf_field_class *event_header_fc = NULL;
1091
1092 /* Reset the position of the last event header */
1093 msg_it->buf.last_eh_at = msg_it->buf.at;
1094 msg_it->cur_event_class_id = -1;
1095
1096 /* Check if we have some content left */
1097 if (msg_it->cur_exp_packet_content_size >= 0) {
1098 if (G_UNLIKELY(packet_at(msg_it) == msg_it->cur_exp_packet_content_size)) {
1099 /* No more events! */
1100 BT_COMP_LOGD("Reached end of packet: msg-it-addr=%p, "
1101 "cur=%zu",
1102 msg_it, packet_at(msg_it));
1103 msg_it->state = STATE_EMIT_MSG_PACKET_END_MULTI;
1104 goto end;
1105 } else if (G_UNLIKELY(packet_at(msg_it) > msg_it->cur_exp_packet_content_size)) {
1106 /* That's not supposed to happen */
1107 BT_COMP_LOGD(
1108 "Before decoding event header field: cursor is passed the packet's content: "
1109 "msg-it-addr=%p, content-size=%" PRId64 ", "
1110 "cur=%zu",
1111 msg_it, msg_it->cur_exp_packet_content_size, packet_at(msg_it));
1112 status = CTF_MSG_ITER_STATUS_ERROR;
1113 goto end;
1114 }
1115 } else {
1116 /*
1117 * "Infinite" content: we're done when the medium has
1118 * nothing else for us.
1119 */
1120 status = buf_ensure_available_bits(msg_it);
1121 switch (status) {
1122 case CTF_MSG_ITER_STATUS_OK:
1123 break;
1124 case CTF_MSG_ITER_STATUS_EOF:
1125 status = CTF_MSG_ITER_STATUS_OK;
1126 msg_it->state = STATE_EMIT_MSG_PACKET_END_SINGLE;
1127 goto end;
1128 default:
1129 goto end;
1130 }
1131 }
1132
1133 release_event_dscopes(msg_it);
1134 BT_ASSERT(msg_it->meta.sc);
1135 event_header_fc = msg_it->meta.sc->event_header_fc;
1136 if (!event_header_fc) {
1137 msg_it->state = STATE_AFTER_EVENT_HEADER;
1138 goto end;
1139 }
1140
1141 BT_COMP_LOGD("Decoding event header field: "
1142 "msg-it-addr=%p, stream-class-addr=%p, "
1143 "stream-class-id=%" PRId64 ", "
1144 "fc-addr=%p",
1145 msg_it, msg_it->meta.sc, msg_it->meta.sc->id, event_header_fc);
1146 status = read_dscope_begin_state(msg_it, event_header_fc, STATE_AFTER_EVENT_HEADER,
1147 STATE_DSCOPE_EVENT_HEADER_CONTINUE, NULL);
1148 if (status < 0) {
1149 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1150 "Cannot decode event header field: "
1151 "msg-it-addr=%p, stream-class-addr=%p, "
1152 "stream-class-id=%" PRId64 ", fc-addr=%p",
1153 msg_it, msg_it->meta.sc, msg_it->meta.sc->id, event_header_fc);
1154 }
1155
1156 end:
1157 return status;
1158 }
1159
1160 static enum ctf_msg_iter_status read_event_header_continue_state(struct ctf_msg_iter *msg_it)
1161 {
1162 return read_dscope_continue_state(msg_it, STATE_AFTER_EVENT_HEADER);
1163 }
1164
1165 static inline enum ctf_msg_iter_status set_current_event_class(struct ctf_msg_iter *msg_it)
1166 {
1167 enum ctf_msg_iter_status status = CTF_MSG_ITER_STATUS_OK;
1168 bt_self_component *self_comp = msg_it->self_comp;
1169
1170 struct ctf_event_class *new_event_class = NULL;
1171
1172 if (msg_it->cur_event_class_id == -1) {
1173 /*
1174 * No current event class ID field, therefore only one
1175 * event class.
1176 */
1177 if (msg_it->meta.sc->event_classes->len != 1) {
1178 BT_COMP_LOGE_APPEND_CAUSE(
1179 self_comp,
1180 "Need exactly one event class since there's no event class ID field: "
1181 "msg-it-addr=%p",
1182 msg_it);
1183 status = CTF_MSG_ITER_STATUS_ERROR;
1184 goto end;
1185 }
1186
1187 new_event_class = (ctf_event_class *) msg_it->meta.sc->event_classes->pdata[0];
1188 msg_it->cur_event_class_id = new_event_class->id;
1189 }
1190
1191 new_event_class =
1192 ctf_stream_class_borrow_event_class_by_id(msg_it->meta.sc, msg_it->cur_event_class_id);
1193 if (!new_event_class) {
1194 BT_COMP_LOGE_APPEND_CAUSE(
1195 self_comp,
1196 "No event class with ID of event class ID to use in stream class: "
1197 "msg-it-addr=%p, stream-class-id=%" PRIu64 ", "
1198 "event-class-id=%" PRIu64 ", "
1199 "trace-class-addr=%p",
1200 msg_it, msg_it->meta.sc->id, msg_it->cur_event_class_id, msg_it->meta.tc);
1201 status = CTF_MSG_ITER_STATUS_ERROR;
1202 goto end;
1203 }
1204
1205 msg_it->meta.ec = new_event_class;
1206 BT_COMP_LOGD("Set current event class: "
1207 "msg-it-addr=%p, event-class-addr=%p, "
1208 "event-class-id=%" PRId64 ", "
1209 "event-class-name=\"%s\"",
1210 msg_it, msg_it->meta.ec, msg_it->meta.ec->id, msg_it->meta.ec->name->str);
1211
1212 end:
1213 return status;
1214 }
1215
1216 static inline enum ctf_msg_iter_status set_current_event_message(struct ctf_msg_iter *msg_it)
1217 {
1218 enum ctf_msg_iter_status status = CTF_MSG_ITER_STATUS_OK;
1219 bt_self_component *self_comp = msg_it->self_comp;
1220 bt_message *msg = NULL;
1221
1222 BT_ASSERT_DBG(msg_it->meta.ec);
1223 BT_ASSERT_DBG(msg_it->packet);
1224 BT_COMP_LOGD("Creating event message from event class and packet: "
1225 "msg-it-addr=%p, ec-addr=%p, ec-name=\"%s\", packet-addr=%p",
1226 msg_it, msg_it->meta.ec, msg_it->meta.ec->name->str, msg_it->packet);
1227 BT_ASSERT_DBG(msg_it->self_msg_iter);
1228 BT_ASSERT_DBG(msg_it->meta.sc);
1229
1230 if (bt_stream_class_borrow_default_clock_class(msg_it->meta.sc->ir_sc)) {
1231 msg = bt_message_event_create_with_packet_and_default_clock_snapshot(
1232 msg_it->self_msg_iter, msg_it->meta.ec->ir_ec, msg_it->packet,
1233 msg_it->default_clock_snapshot);
1234 } else {
1235 msg = bt_message_event_create_with_packet(msg_it->self_msg_iter, msg_it->meta.ec->ir_ec,
1236 msg_it->packet);
1237 }
1238
1239 if (!msg) {
1240 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1241 "Cannot create event message: "
1242 "msg-it-addr=%p, ec-addr=%p, ec-name=\"%s\", "
1243 "packet-addr=%p",
1244 msg_it, msg_it->meta.ec, msg_it->meta.ec->name->str,
1245 msg_it->packet);
1246 goto error;
1247 }
1248
1249 goto end;
1250
1251 error:
1252 BT_MESSAGE_PUT_REF_AND_RESET(msg);
1253 status = CTF_MSG_ITER_STATUS_ERROR;
1254
1255 end:
1256 BT_MESSAGE_MOVE_REF(msg_it->event_msg, msg);
1257 return status;
1258 }
1259
1260 static enum ctf_msg_iter_status after_event_header_state(struct ctf_msg_iter *msg_it)
1261 {
1262 enum ctf_msg_iter_status status;
1263
1264 status = set_current_event_class(msg_it);
1265 if (status != CTF_MSG_ITER_STATUS_OK) {
1266 goto end;
1267 }
1268
1269 if (G_UNLIKELY(msg_it->dry_run)) {
1270 goto next_state;
1271 }
1272
1273 status = set_current_event_message(msg_it);
1274 if (status != CTF_MSG_ITER_STATUS_OK) {
1275 goto end;
1276 }
1277
1278 msg_it->event = bt_message_event_borrow_event(msg_it->event_msg);
1279 BT_ASSERT_DBG(msg_it->event);
1280
1281 next_state:
1282 msg_it->state = STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN;
1283
1284 end:
1285 return status;
1286 }
1287
1288 static enum ctf_msg_iter_status read_event_common_context_begin_state(struct ctf_msg_iter *msg_it)
1289 {
1290 enum ctf_msg_iter_status status = CTF_MSG_ITER_STATUS_OK;
1291 bt_self_component *self_comp = msg_it->self_comp;
1292 struct ctf_field_class *event_common_context_fc;
1293
1294 event_common_context_fc = msg_it->meta.sc->event_common_context_fc;
1295 if (!event_common_context_fc) {
1296 msg_it->state = STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN;
1297 goto end;
1298 }
1299
1300 if (event_common_context_fc->in_ir && !msg_it->dry_run) {
1301 BT_ASSERT_DBG(!msg_it->dscopes.event_common_context);
1302 msg_it->dscopes.event_common_context = bt_event_borrow_common_context_field(msg_it->event);
1303 BT_ASSERT_DBG(msg_it->dscopes.event_common_context);
1304 }
1305
1306 BT_COMP_LOGT("Decoding event common context field: "
1307 "msg-it-addr=%p, stream-class-addr=%p, "
1308 "stream-class-id=%" PRId64 ", "
1309 "fc-addr=%p",
1310 msg_it, msg_it->meta.sc, msg_it->meta.sc->id, event_common_context_fc);
1311 status = read_dscope_begin_state(
1312 msg_it, event_common_context_fc, STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN,
1313 STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE, msg_it->dscopes.event_common_context);
1314 if (status < 0) {
1315 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1316 "Cannot decode event common context field: "
1317 "msg-it-addr=%p, stream-class-addr=%p, "
1318 "stream-class-id=%" PRId64 ", fc-addr=%p",
1319 msg_it, msg_it->meta.sc, msg_it->meta.sc->id,
1320 event_common_context_fc);
1321 }
1322
1323 end:
1324 return status;
1325 }
1326
1327 static enum ctf_msg_iter_status
1328 read_event_common_context_continue_state(struct ctf_msg_iter *msg_it)
1329 {
1330 return read_dscope_continue_state(msg_it, STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN);
1331 }
1332
1333 static enum ctf_msg_iter_status read_event_spec_context_begin_state(struct ctf_msg_iter *msg_it)
1334 {
1335 enum ctf_msg_iter_status status = CTF_MSG_ITER_STATUS_OK;
1336 bt_self_component *self_comp = msg_it->self_comp;
1337 struct ctf_field_class *event_spec_context_fc;
1338
1339 event_spec_context_fc = msg_it->meta.ec->spec_context_fc;
1340 if (!event_spec_context_fc) {
1341 msg_it->state = STATE_DSCOPE_EVENT_PAYLOAD_BEGIN;
1342 goto end;
1343 }
1344
1345 if (event_spec_context_fc->in_ir && !msg_it->dry_run) {
1346 BT_ASSERT_DBG(!msg_it->dscopes.event_spec_context);
1347 msg_it->dscopes.event_spec_context = bt_event_borrow_specific_context_field(msg_it->event);
1348 BT_ASSERT_DBG(msg_it->dscopes.event_spec_context);
1349 }
1350
1351 BT_COMP_LOGT("Decoding event specific context field: "
1352 "msg-it-addr=%p, event-class-addr=%p, "
1353 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
1354 "fc-addr=%p",
1355 msg_it, msg_it->meta.ec, msg_it->meta.ec->name->str, msg_it->meta.ec->id,
1356 event_spec_context_fc);
1357 status = read_dscope_begin_state(
1358 msg_it, event_spec_context_fc, STATE_DSCOPE_EVENT_PAYLOAD_BEGIN,
1359 STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE, msg_it->dscopes.event_spec_context);
1360 if (status < 0) {
1361 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1362 "Cannot decode event specific context field: "
1363 "msg-it-addr=%p, event-class-addr=%p, "
1364 "event-class-name=\"%s\", "
1365 "event-class-id=%" PRId64 ", fc-addr=%p",
1366 msg_it, msg_it->meta.ec, msg_it->meta.ec->name->str,
1367 msg_it->meta.ec->id, event_spec_context_fc);
1368 }
1369
1370 end:
1371 return status;
1372 }
1373
1374 static enum ctf_msg_iter_status read_event_spec_context_continue_state(struct ctf_msg_iter *msg_it)
1375 {
1376 return read_dscope_continue_state(msg_it, STATE_DSCOPE_EVENT_PAYLOAD_BEGIN);
1377 }
1378
1379 static enum ctf_msg_iter_status read_event_payload_begin_state(struct ctf_msg_iter *msg_it)
1380 {
1381 enum ctf_msg_iter_status status = CTF_MSG_ITER_STATUS_OK;
1382 bt_self_component *self_comp = msg_it->self_comp;
1383 struct ctf_field_class *event_payload_fc;
1384
1385 event_payload_fc = msg_it->meta.ec->payload_fc;
1386 if (!event_payload_fc) {
1387 msg_it->state = STATE_EMIT_MSG_EVENT;
1388 goto end;
1389 }
1390
1391 if (event_payload_fc->in_ir && !msg_it->dry_run) {
1392 BT_ASSERT_DBG(!msg_it->dscopes.event_payload);
1393 msg_it->dscopes.event_payload = bt_event_borrow_payload_field(msg_it->event);
1394 BT_ASSERT_DBG(msg_it->dscopes.event_payload);
1395 }
1396
1397 BT_COMP_LOGT("Decoding event payload field: "
1398 "msg-it-addr=%p, event-class-addr=%p, "
1399 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
1400 "fc-addr=%p",
1401 msg_it, msg_it->meta.ec, msg_it->meta.ec->name->str, msg_it->meta.ec->id,
1402 event_payload_fc);
1403 status =
1404 read_dscope_begin_state(msg_it, event_payload_fc, STATE_EMIT_MSG_EVENT,
1405 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE, msg_it->dscopes.event_payload);
1406 if (status < 0) {
1407 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1408 "Cannot decode event payload field: "
1409 "msg-it-addr=%p, event-class-addr=%p, "
1410 "event-class-name=\"%s\", "
1411 "event-class-id=%" PRId64 ", fc-addr=%p",
1412 msg_it, msg_it->meta.ec, msg_it->meta.ec->name->str,
1413 msg_it->meta.ec->id, event_payload_fc);
1414 }
1415
1416 end:
1417 return status;
1418 }
1419
1420 static enum ctf_msg_iter_status read_event_payload_continue_state(struct ctf_msg_iter *msg_it)
1421 {
1422 return read_dscope_continue_state(msg_it, STATE_EMIT_MSG_EVENT);
1423 }
1424
1425 static enum ctf_msg_iter_status skip_packet_padding_state(struct ctf_msg_iter *msg_it)
1426 {
1427 enum ctf_msg_iter_status status = CTF_MSG_ITER_STATUS_OK;
1428 size_t bits_to_skip;
1429 const enum state next_state = STATE_SWITCH_PACKET;
1430
1431 BT_ASSERT(msg_it->cur_exp_packet_total_size > 0);
1432 bits_to_skip = msg_it->cur_exp_packet_total_size - packet_at(msg_it);
1433 if (bits_to_skip == 0) {
1434 msg_it->state = next_state;
1435 goto end;
1436 } else {
1437 size_t bits_to_consume;
1438
1439 BT_COMP_LOGD("Trying to skip %zu bits of padding: msg-it-addr=%p, size=%zu", bits_to_skip,
1440 msg_it, bits_to_skip);
1441 status = buf_ensure_available_bits(msg_it);
1442 if (status != CTF_MSG_ITER_STATUS_OK) {
1443 goto end;
1444 }
1445
1446 bits_to_consume = MIN(buf_available_bits(msg_it), bits_to_skip);
1447 BT_COMP_LOGD("Skipping %zu bits of padding: msg-it-addr=%p, size=%zu", bits_to_consume,
1448 msg_it, bits_to_consume);
1449 buf_consume_bits(msg_it, bits_to_consume);
1450 bits_to_skip = msg_it->cur_exp_packet_total_size - packet_at(msg_it);
1451 if (bits_to_skip == 0) {
1452 msg_it->state = next_state;
1453 goto end;
1454 }
1455 }
1456
1457 end:
1458 return status;
1459 }
1460
1461 static enum ctf_msg_iter_status check_emit_msg_discarded_events(struct ctf_msg_iter *msg_it)
1462 {
1463 msg_it->state = STATE_EMIT_MSG_DISCARDED_EVENTS;
1464
1465 if (!msg_it->meta.sc->has_discarded_events) {
1466 msg_it->state = STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS;
1467 goto end;
1468 }
1469
1470 if (msg_it->prev_packet_snapshots.discarded_events == UINT64_C(-1)) {
1471 if (msg_it->snapshots.discarded_events == 0 ||
1472 msg_it->snapshots.discarded_events == UINT64_C(-1)) {
1473 /*
1474 * Stream's first packet with no discarded
1475 * events or no information about discarded
1476 * events: do not emit.
1477 */
1478 msg_it->state = STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS;
1479 }
1480 } else {
1481 /*
1482 * If the previous packet has a value for this counter,
1483 * then this counter is defined for the whole stream.
1484 */
1485 BT_ASSERT(msg_it->snapshots.discarded_events != UINT64_C(-1));
1486
1487 if (msg_it->snapshots.discarded_events - msg_it->prev_packet_snapshots.discarded_events ==
1488 0) {
1489 /*
1490 * No discarded events since previous packet: do
1491 * not emit.
1492 */
1493 msg_it->state = STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS;
1494 }
1495 }
1496
1497 end:
1498 return CTF_MSG_ITER_STATUS_OK;
1499 }
1500
1501 static enum ctf_msg_iter_status check_emit_msg_discarded_packets(struct ctf_msg_iter *msg_it)
1502 {
1503 msg_it->state = STATE_EMIT_MSG_DISCARDED_PACKETS;
1504
1505 if (!msg_it->meta.sc->has_discarded_packets) {
1506 msg_it->state = STATE_EMIT_MSG_PACKET_BEGINNING;
1507 goto end;
1508 }
1509
1510 if (msg_it->prev_packet_snapshots.packets == UINT64_C(-1)) {
1511 /*
1512 * Stream's first packet or no information about
1513 * discarded packets: do not emit. In other words, if
1514 * this is the first packet and its sequence number is
1515 * not 0, do not consider that packets were previously
1516 * lost: we might be reading a partial stream (LTTng
1517 * snapshot for example).
1518 */
1519 msg_it->state = STATE_EMIT_MSG_PACKET_BEGINNING;
1520 } else {
1521 /*
1522 * If the previous packet has a value for this counter,
1523 * then this counter is defined for the whole stream.
1524 */
1525 BT_ASSERT(msg_it->snapshots.packets != UINT64_C(-1));
1526
1527 if (msg_it->snapshots.packets - msg_it->prev_packet_snapshots.packets <= 1) {
1528 /*
1529 * No discarded packets since previous packet:
1530 * do not emit.
1531 */
1532 msg_it->state = STATE_EMIT_MSG_PACKET_BEGINNING;
1533 }
1534 }
1535
1536 end:
1537 return CTF_MSG_ITER_STATUS_OK;
1538 }
1539
1540 static inline enum state check_emit_msg_stream_end(struct ctf_msg_iter *msg_it)
1541 {
1542 enum state next_state;
1543
1544 if (msg_it->emit_stream_end_message) {
1545 next_state = STATE_EMIT_MSG_STREAM_END;
1546 } else {
1547 next_state = STATE_DONE;
1548 }
1549
1550 return next_state;
1551 }
1552
1553 static inline enum ctf_msg_iter_status handle_state(struct ctf_msg_iter *msg_it)
1554 {
1555 enum ctf_msg_iter_status status = CTF_MSG_ITER_STATUS_OK;
1556 const enum state state = msg_it->state;
1557
1558 BT_COMP_LOGT("Handling state: msg-it-addr=%p, state=%s", msg_it, state_string(state));
1559
1560 // TODO: optimalize!
1561 switch (state) {
1562 case STATE_INIT:
1563 msg_it->state = STATE_SWITCH_PACKET;
1564 break;
1565 case STATE_SWITCH_PACKET:
1566 status = switch_packet_state(msg_it);
1567 break;
1568 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
1569 status = read_packet_header_begin_state(msg_it);
1570 break;
1571 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
1572 status = read_packet_header_continue_state(msg_it);
1573 break;
1574 case STATE_AFTER_TRACE_PACKET_HEADER:
1575 status = after_packet_header_state(msg_it);
1576 break;
1577 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN:
1578 status = read_packet_context_begin_state(msg_it);
1579 break;
1580 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE:
1581 status = read_packet_context_continue_state(msg_it);
1582 break;
1583 case STATE_AFTER_STREAM_PACKET_CONTEXT:
1584 status = after_packet_context_state(msg_it);
1585 break;
1586 case STATE_EMIT_MSG_STREAM_BEGINNING:
1587 msg_it->state = STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS;
1588 break;
1589 case STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS:
1590 status = check_emit_msg_discarded_events(msg_it);
1591 break;
1592 case STATE_EMIT_MSG_DISCARDED_EVENTS:
1593 msg_it->state = STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS;
1594 break;
1595 case STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS:
1596 status = check_emit_msg_discarded_packets(msg_it);
1597 break;
1598 case STATE_EMIT_MSG_DISCARDED_PACKETS:
1599 msg_it->state = STATE_EMIT_MSG_PACKET_BEGINNING;
1600 break;
1601 case STATE_EMIT_MSG_PACKET_BEGINNING:
1602 msg_it->state = STATE_DSCOPE_EVENT_HEADER_BEGIN;
1603 break;
1604 case STATE_DSCOPE_EVENT_HEADER_BEGIN:
1605 status = read_event_header_begin_state(msg_it);
1606 break;
1607 case STATE_DSCOPE_EVENT_HEADER_CONTINUE:
1608 status = read_event_header_continue_state(msg_it);
1609 break;
1610 case STATE_AFTER_EVENT_HEADER:
1611 status = after_event_header_state(msg_it);
1612 break;
1613 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN:
1614 status = read_event_common_context_begin_state(msg_it);
1615 break;
1616 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE:
1617 status = read_event_common_context_continue_state(msg_it);
1618 break;
1619 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN:
1620 status = read_event_spec_context_begin_state(msg_it);
1621 break;
1622 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE:
1623 status = read_event_spec_context_continue_state(msg_it);
1624 break;
1625 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN:
1626 status = read_event_payload_begin_state(msg_it);
1627 break;
1628 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE:
1629 status = read_event_payload_continue_state(msg_it);
1630 break;
1631 case STATE_EMIT_MSG_EVENT:
1632 msg_it->state = STATE_DSCOPE_EVENT_HEADER_BEGIN;
1633 break;
1634 case STATE_EMIT_QUEUED_MSG_EVENT:
1635 msg_it->state = STATE_EMIT_MSG_EVENT;
1636 break;
1637 case STATE_SKIP_PACKET_PADDING:
1638 status = skip_packet_padding_state(msg_it);
1639 break;
1640 case STATE_EMIT_MSG_PACKET_END_MULTI:
1641 msg_it->state = STATE_SKIP_PACKET_PADDING;
1642 break;
1643 case STATE_EMIT_MSG_PACKET_END_SINGLE:
1644 msg_it->state = STATE_EMIT_MSG_STREAM_END;
1645 break;
1646 case STATE_EMIT_QUEUED_MSG_PACKET_END:
1647 msg_it->state = STATE_EMIT_MSG_PACKET_END_SINGLE;
1648 break;
1649 case STATE_CHECK_EMIT_MSG_STREAM_END:
1650 msg_it->state = check_emit_msg_stream_end(msg_it);
1651 break;
1652 case STATE_EMIT_MSG_STREAM_END:
1653 msg_it->state = STATE_DONE;
1654 break;
1655 case STATE_DONE:
1656 break;
1657 default:
1658 BT_COMP_LOGF("Unknown CTF plugin message iterator state: "
1659 "msg-it-addr=%p, state=%d",
1660 msg_it, msg_it->state);
1661 bt_common_abort();
1662 }
1663
1664 BT_COMP_LOGT("Handled state: msg-it-addr=%p, status=%s, "
1665 "prev-state=%s, cur-state=%s",
1666 msg_it, ctf_msg_iter_status_string(status), state_string(state),
1667 state_string(msg_it->state));
1668 return status;
1669 }
1670
1671 void ctf_msg_iter_reset_for_next_stream_file(struct ctf_msg_iter *msg_it)
1672 {
1673 BT_ASSERT(msg_it);
1674 BT_COMP_LOGD("Resetting message iterator: addr=%p", msg_it);
1675 stack_clear(msg_it->stack);
1676 msg_it->meta.sc = NULL;
1677 msg_it->meta.ec = NULL;
1678 BT_PACKET_PUT_REF_AND_RESET(msg_it->packet);
1679 BT_STREAM_PUT_REF_AND_RESET(msg_it->stream);
1680 BT_MESSAGE_PUT_REF_AND_RESET(msg_it->event_msg);
1681 release_all_dscopes(msg_it);
1682 msg_it->cur_dscope_field = NULL;
1683
1684 msg_it->buf.addr = NULL;
1685 msg_it->buf.sz = 0;
1686 msg_it->buf.at = 0;
1687 msg_it->buf.last_eh_at = SIZE_MAX;
1688 msg_it->buf.packet_offset = 0;
1689 msg_it->state = STATE_INIT;
1690 msg_it->cur_exp_packet_content_size = -1;
1691 msg_it->cur_exp_packet_total_size = -1;
1692 msg_it->cur_packet_offset = -1;
1693 msg_it->cur_event_class_id = -1;
1694 msg_it->snapshots.beginning_clock = UINT64_C(-1);
1695 msg_it->snapshots.end_clock = UINT64_C(-1);
1696 }
1697
1698 /**
1699 * Resets the internal state of a CTF message iterator.
1700 */
1701 void ctf_msg_iter_reset(struct ctf_msg_iter *msg_it)
1702 {
1703 ctf_msg_iter_reset_for_next_stream_file(msg_it);
1704 msg_it->cur_stream_class_id = -1;
1705 msg_it->cur_data_stream_id = -1;
1706 msg_it->snapshots.discarded_events = UINT64_C(-1);
1707 msg_it->snapshots.packets = UINT64_C(-1);
1708 msg_it->prev_packet_snapshots.discarded_events = UINT64_C(-1);
1709 msg_it->prev_packet_snapshots.packets = UINT64_C(-1);
1710 msg_it->prev_packet_snapshots.beginning_clock = UINT64_C(-1);
1711 msg_it->prev_packet_snapshots.end_clock = UINT64_C(-1);
1712 msg_it->emit_stream_beginning_message = true;
1713 msg_it->emit_stream_end_message = false;
1714 }
1715
1716 static bt_field *borrow_next_field(struct ctf_msg_iter *msg_it)
1717 {
1718 bt_field *next_field = NULL;
1719 bt_field *base_field;
1720 const bt_field_class *base_fc;
1721 bt_field_class_type base_fc_type;
1722 size_t index;
1723
1724 BT_ASSERT_DBG(!stack_empty(msg_it->stack));
1725 index = stack_top(msg_it->stack)->index;
1726 base_field = stack_top(msg_it->stack)->base;
1727 BT_ASSERT_DBG(base_field);
1728 base_fc = bt_field_borrow_class_const(base_field);
1729 BT_ASSERT_DBG(base_fc);
1730 base_fc_type = bt_field_class_get_type(base_fc);
1731
1732 if (base_fc_type == BT_FIELD_CLASS_TYPE_STRUCTURE) {
1733 BT_ASSERT_DBG(index < bt_field_class_structure_get_member_count(
1734 bt_field_borrow_class_const(base_field)));
1735 next_field = bt_field_structure_borrow_member_field_by_index(base_field, index);
1736 } else if (bt_field_class_type_is(base_fc_type, BT_FIELD_CLASS_TYPE_ARRAY)) {
1737 BT_ASSERT_DBG(index < bt_field_array_get_length(base_field));
1738 next_field = bt_field_array_borrow_element_field_by_index(base_field, index);
1739 } else if (bt_field_class_type_is(base_fc_type, BT_FIELD_CLASS_TYPE_VARIANT)) {
1740 BT_ASSERT_DBG(index == 0);
1741 next_field = bt_field_variant_borrow_selected_option_field(base_field);
1742 } else {
1743 bt_common_abort();
1744 }
1745
1746 BT_ASSERT_DBG(next_field);
1747 return next_field;
1748 }
1749
1750 static void update_default_clock(struct ctf_msg_iter *msg_it, uint64_t new_val,
1751 uint64_t new_val_size)
1752 {
1753 uint64_t new_val_mask;
1754 uint64_t cur_value_masked;
1755
1756 BT_ASSERT_DBG(new_val_size > 0);
1757
1758 /*
1759 * Special case for a 64-bit new value, which is the limit
1760 * of a clock value as of this version: overwrite the
1761 * current value directly.
1762 */
1763 if (new_val_size == 64) {
1764 msg_it->default_clock_snapshot = new_val;
1765 goto end;
1766 }
1767
1768 new_val_mask = (1ULL << new_val_size) - 1;
1769 cur_value_masked = msg_it->default_clock_snapshot & new_val_mask;
1770
1771 if (new_val < cur_value_masked) {
1772 /*
1773 * It looks like a wrap happened on the number of bits
1774 * of the requested new value. Assume that the clock
1775 * value wrapped only one time.
1776 */
1777 msg_it->default_clock_snapshot += new_val_mask + 1;
1778 }
1779
1780 /* Clear the low bits of the current clock value. */
1781 msg_it->default_clock_snapshot &= ~new_val_mask;
1782
1783 /* Set the low bits of the current clock value. */
1784 msg_it->default_clock_snapshot |= new_val;
1785
1786 end:
1787 BT_COMP_LOGT("Updated default clock's value from integer field's value: "
1788 "value=%" PRIu64,
1789 msg_it->default_clock_snapshot);
1790 }
1791
1792 /*
1793 * Ensure the message iterator's `stored_values` array is large enough to
1794 * accomodate `storing_index`.
1795 *
1796 * We may need more slots in the array than initially allocated if more
1797 * metadata arrives along the way.
1798 */
1799 static void ensure_stored_values_size(ctf_msg_iter *msg_it, uint64_t storing_index)
1800 {
1801 if (G_UNLIKELY(storing_index >= msg_it->stored_values->len)) {
1802 g_array_set_size(msg_it->stored_values, msg_it->meta.tc->stored_value_count);
1803 }
1804 }
1805
1806 static enum bt_bfcr_status bfcr_unsigned_int_cb(uint64_t value, struct ctf_field_class *fc,
1807 void *data)
1808 {
1809 ctf_msg_iter *msg_it = (ctf_msg_iter *) data;
1810 bt_self_component *self_comp = msg_it->self_comp;
1811 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
1812
1813 bt_field *field = NULL;
1814
1815 BT_COMP_LOGT("Unsigned integer function called from BFCR: "
1816 "msg-it-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1817 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64,
1818 msg_it, msg_it->bfcr, fc, fc->type, fc->in_ir, value);
1819
1820 ctf_field_class_int *int_fc = ctf_field_class_as_int(fc);
1821
1822 if (G_LIKELY(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE)) {
1823 goto update_def_clock;
1824 }
1825
1826 switch (int_fc->meaning) {
1827 case CTF_FIELD_CLASS_MEANING_EVENT_CLASS_ID:
1828 msg_it->cur_event_class_id = value;
1829 break;
1830 case CTF_FIELD_CLASS_MEANING_DATA_STREAM_ID:
1831 msg_it->cur_data_stream_id = value;
1832 break;
1833 case CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME:
1834 msg_it->snapshots.beginning_clock = value;
1835 break;
1836 case CTF_FIELD_CLASS_MEANING_PACKET_END_TIME:
1837 msg_it->snapshots.end_clock = value;
1838 break;
1839 case CTF_FIELD_CLASS_MEANING_STREAM_CLASS_ID:
1840 msg_it->cur_stream_class_id = value;
1841 break;
1842 case CTF_FIELD_CLASS_MEANING_MAGIC:
1843 if (value != 0xc1fc1fc1) {
1844 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1845 "Invalid CTF magic number: msg-it-addr=%p, "
1846 "magic=%" PRIx64,
1847 msg_it, value);
1848 status = BT_BFCR_STATUS_ERROR;
1849 goto end;
1850 }
1851
1852 break;
1853 case CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT:
1854 msg_it->snapshots.packets = value;
1855 break;
1856 case CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT:
1857 msg_it->snapshots.discarded_events = value;
1858 break;
1859 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_TOTAL_SIZE:
1860 msg_it->cur_exp_packet_total_size = value;
1861 break;
1862 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_CONTENT_SIZE:
1863 msg_it->cur_exp_packet_content_size = value;
1864 break;
1865 default:
1866 bt_common_abort();
1867 }
1868
1869 update_def_clock:
1870 if (G_UNLIKELY(int_fc->mapped_clock_class)) {
1871 update_default_clock(msg_it, value, int_fc->base.size);
1872 }
1873
1874 if (G_UNLIKELY(int_fc->storing_index >= 0)) {
1875 ensure_stored_values_size(msg_it, int_fc->storing_index);
1876 bt_g_array_index(msg_it->stored_values, uint64_t, (uint64_t) int_fc->storing_index) = value;
1877 }
1878
1879 if (G_UNLIKELY(!fc->in_ir || msg_it->dry_run)) {
1880 goto end;
1881 }
1882
1883 field = borrow_next_field(msg_it);
1884 BT_ASSERT_DBG(field);
1885 BT_ASSERT_DBG(bt_field_borrow_class_const(field) == fc->ir_fc);
1886 BT_ASSERT_DBG(bt_field_class_type_is(bt_field_get_class_type(field),
1887 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER));
1888 bt_field_integer_unsigned_set_value(field, value);
1889 stack_top(msg_it->stack)->index++;
1890
1891 end:
1892 return status;
1893 }
1894
1895 static enum bt_bfcr_status bfcr_unsigned_int_char_cb(uint64_t value, struct ctf_field_class *fc,
1896 void *data)
1897 {
1898 int ret;
1899 ctf_msg_iter *msg_it = (ctf_msg_iter *) data;
1900 bt_self_component *self_comp = msg_it->self_comp;
1901 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
1902 bt_field *string_field = NULL;
1903 char str[2] = {'\0', '\0'};
1904
1905 BT_COMP_LOGT("Unsigned integer character function called from BFCR: "
1906 "msg-it-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1907 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64,
1908 msg_it, msg_it->bfcr, fc, fc->type, fc->in_ir, value);
1909
1910 ctf_field_class_int *int_fc = ctf_field_class_as_int(fc);
1911 BT_ASSERT_DBG(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE);
1912 BT_ASSERT_DBG(!int_fc->mapped_clock_class);
1913 BT_ASSERT_DBG(int_fc->storing_index < 0);
1914
1915 if (G_UNLIKELY(!fc->in_ir || msg_it->dry_run)) {
1916 goto end;
1917 }
1918
1919 if (msg_it->done_filling_string) {
1920 goto end;
1921 }
1922
1923 if (value == 0) {
1924 msg_it->done_filling_string = true;
1925 goto end;
1926 }
1927
1928 string_field = stack_top(msg_it->stack)->base;
1929 BT_ASSERT_DBG(bt_field_get_class_type(string_field) == BT_FIELD_CLASS_TYPE_STRING);
1930
1931 /* Append character */
1932 str[0] = (char) value;
1933 ret = bt_field_string_append_with_length(string_field, str, 1);
1934 if (ret) {
1935 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1936 "Cannot append character to string field's value: "
1937 "msg-it-addr=%p, field-addr=%p, ret=%d",
1938 msg_it, string_field, ret);
1939 status = BT_BFCR_STATUS_ERROR;
1940 goto end;
1941 }
1942
1943 end:
1944 return status;
1945 }
1946
1947 static enum bt_bfcr_status bfcr_signed_int_cb(int64_t value, struct ctf_field_class *fc, void *data)
1948 {
1949 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
1950 bt_field *field = NULL;
1951 ctf_msg_iter *msg_it = (ctf_msg_iter *) data;
1952
1953 BT_COMP_LOGT("Signed integer function called from BFCR: "
1954 "msg-it-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1955 "fc-type=%d, fc-in-ir=%d, value=%" PRId64,
1956 msg_it, msg_it->bfcr, fc, fc->type, fc->in_ir, value);
1957
1958 ctf_field_class_int *int_fc = ctf_field_class_as_int(fc);
1959 BT_ASSERT_DBG(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE);
1960
1961 if (G_UNLIKELY(int_fc->storing_index >= 0)) {
1962 ensure_stored_values_size(msg_it, int_fc->storing_index);
1963 bt_g_array_index(msg_it->stored_values, uint64_t, (uint64_t) int_fc->storing_index) =
1964 (uint64_t) value;
1965 }
1966
1967 if (G_UNLIKELY(!fc->in_ir || msg_it->dry_run)) {
1968 goto end;
1969 }
1970
1971 field = borrow_next_field(msg_it);
1972 BT_ASSERT_DBG(field);
1973 BT_ASSERT_DBG(bt_field_borrow_class_const(field) == fc->ir_fc);
1974 BT_ASSERT_DBG(
1975 bt_field_class_type_is(bt_field_get_class_type(field), BT_FIELD_CLASS_TYPE_SIGNED_INTEGER));
1976 bt_field_integer_signed_set_value(field, value);
1977 stack_top(msg_it->stack)->index++;
1978
1979 end:
1980 return status;
1981 }
1982
1983 static enum bt_bfcr_status bfcr_floating_point_cb(double value, struct ctf_field_class *fc,
1984 void *data)
1985 {
1986 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
1987 bt_field *field = NULL;
1988 ctf_msg_iter *msg_it = (ctf_msg_iter *) data;
1989 bt_field_class_type type;
1990
1991 BT_COMP_LOGT("Floating point number function called from BFCR: "
1992 "msg-it-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1993 "fc-type=%d, fc-in-ir=%d, value=%f",
1994 msg_it, msg_it->bfcr, fc, fc->type, fc->in_ir, value);
1995
1996 if (G_UNLIKELY(!fc->in_ir || msg_it->dry_run)) {
1997 goto end;
1998 }
1999
2000 field = borrow_next_field(msg_it);
2001 type = bt_field_get_class_type(field);
2002 BT_ASSERT_DBG(field);
2003 BT_ASSERT_DBG(bt_field_borrow_class_const(field) == fc->ir_fc);
2004 BT_ASSERT_DBG(bt_field_class_type_is(type, BT_FIELD_CLASS_TYPE_REAL));
2005
2006 if (type == BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL) {
2007 bt_field_real_single_precision_set_value(field, (float) value);
2008 } else {
2009 bt_field_real_double_precision_set_value(field, value);
2010 }
2011 stack_top(msg_it->stack)->index++;
2012
2013 end:
2014 return status;
2015 }
2016
2017 static enum bt_bfcr_status bfcr_string_begin_cb(struct ctf_field_class *fc, void *data)
2018 {
2019 bt_field *field = NULL;
2020 ctf_msg_iter *msg_it = (ctf_msg_iter *) data;
2021
2022 BT_COMP_LOGT("String (beginning) function called from BFCR: "
2023 "msg-it-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2024 "fc-type=%d, fc-in-ir=%d",
2025 msg_it, msg_it->bfcr, fc, fc->type, fc->in_ir);
2026
2027 if (G_UNLIKELY(!fc->in_ir || msg_it->dry_run)) {
2028 goto end;
2029 }
2030
2031 field = borrow_next_field(msg_it);
2032 BT_ASSERT_DBG(field);
2033 BT_ASSERT_DBG(bt_field_borrow_class_const(field) == fc->ir_fc);
2034 BT_ASSERT_DBG(bt_field_get_class_type(field) == BT_FIELD_CLASS_TYPE_STRING);
2035 bt_field_string_clear(field);
2036
2037 /*
2038 * Push on stack. Not a compound class per se, but we know that
2039 * only bfcr_string_cb() may be called between this call and a
2040 * subsequent call to bfcr_string_end_cb().
2041 */
2042 stack_push(msg_it->stack, field);
2043
2044 end:
2045 return BT_BFCR_STATUS_OK;
2046 }
2047
2048 static enum bt_bfcr_status bfcr_string_cb(const char *value, size_t len, struct ctf_field_class *fc,
2049 void *data)
2050 {
2051 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
2052 bt_field *field = NULL;
2053 ctf_msg_iter *msg_it = (ctf_msg_iter *) data;
2054 bt_self_component *self_comp = msg_it->self_comp;
2055 int ret;
2056
2057 BT_COMP_LOGT("String (substring) function called from BFCR: "
2058 "msg-it-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2059 "fc-type=%d, fc-in-ir=%d, string-length=%zu",
2060 msg_it, msg_it->bfcr, fc, fc->type, fc->in_ir, len);
2061
2062 if (G_UNLIKELY(!fc->in_ir || msg_it->dry_run)) {
2063 goto end;
2064 }
2065
2066 field = stack_top(msg_it->stack)->base;
2067 BT_ASSERT_DBG(field);
2068
2069 /* Append current substring */
2070 ret = bt_field_string_append_with_length(field, value, len);
2071 if (ret) {
2072 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
2073 "Cannot append substring to string field's value: "
2074 "msg-it-addr=%p, field-addr=%p, string-length=%zu, "
2075 "ret=%d",
2076 msg_it, field, len, ret);
2077 status = BT_BFCR_STATUS_ERROR;
2078 goto end;
2079 }
2080
2081 end:
2082 return status;
2083 }
2084
2085 static enum bt_bfcr_status bfcr_string_end_cb(struct ctf_field_class *fc, void *data)
2086 {
2087 ctf_msg_iter *msg_it = (ctf_msg_iter *) data;
2088
2089 BT_COMP_LOGT("String (end) function called from BFCR: "
2090 "msg-it-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2091 "fc-type=%d, fc-in-ir=%d",
2092 msg_it, msg_it->bfcr, fc, fc->type, fc->in_ir);
2093
2094 if (G_UNLIKELY(!fc->in_ir || msg_it->dry_run)) {
2095 goto end;
2096 }
2097
2098 /* Pop string field */
2099 stack_pop(msg_it->stack);
2100
2101 /* Go to next field */
2102 stack_top(msg_it->stack)->index++;
2103
2104 end:
2105 return BT_BFCR_STATUS_OK;
2106 }
2107
2108 static enum bt_bfcr_status bfcr_compound_begin_cb(struct ctf_field_class *fc, void *data)
2109 {
2110 ctf_msg_iter *msg_it = (ctf_msg_iter *) data;
2111 bt_field *field;
2112
2113 BT_COMP_LOGT("Compound (beginning) function called from BFCR: "
2114 "msg-it-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2115 "fc-type=%d, fc-in-ir=%d",
2116 msg_it, msg_it->bfcr, fc, fc->type, fc->in_ir);
2117
2118 if (G_UNLIKELY(!fc->in_ir || msg_it->dry_run)) {
2119 goto end;
2120 }
2121
2122 /* Borrow field */
2123 if (stack_empty(msg_it->stack)) {
2124 /* Root: already set by read_dscope_begin_state() */
2125 field = msg_it->cur_dscope_field;
2126 } else {
2127 field = borrow_next_field(msg_it);
2128 BT_ASSERT_DBG(field);
2129 }
2130
2131 /* Push field */
2132 BT_ASSERT_DBG(field);
2133 BT_ASSERT_DBG(bt_field_borrow_class_const(field) == fc->ir_fc);
2134 stack_push(msg_it->stack, field);
2135
2136 /*
2137 * Change BFCR "unsigned int" callback if it's a text
2138 * array/sequence.
2139 */
2140 if (fc->type == CTF_FIELD_CLASS_TYPE_ARRAY || fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE) {
2141 ctf_field_class_array_base *array_fc = ctf_field_class_as_array_base(fc);
2142
2143 if (array_fc->is_text) {
2144 BT_ASSERT_DBG(bt_field_get_class_type(field) == BT_FIELD_CLASS_TYPE_STRING);
2145 msg_it->done_filling_string = false;
2146 bt_field_string_clear(field);
2147 bt_bfcr_set_unsigned_int_cb(msg_it->bfcr, bfcr_unsigned_int_char_cb);
2148 }
2149 }
2150
2151 end:
2152 return BT_BFCR_STATUS_OK;
2153 }
2154
2155 static enum bt_bfcr_status bfcr_compound_end_cb(struct ctf_field_class *fc, void *data)
2156 {
2157 ctf_msg_iter *msg_it = (ctf_msg_iter *) data;
2158
2159 BT_COMP_LOGT("Compound (end) function called from BFCR: "
2160 "msg-it-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2161 "fc-type=%d, fc-in-ir=%d",
2162 msg_it, msg_it->bfcr, fc, fc->type, fc->in_ir);
2163
2164 if (G_UNLIKELY(!fc->in_ir || msg_it->dry_run)) {
2165 goto end;
2166 }
2167
2168 BT_ASSERT_DBG(!stack_empty(msg_it->stack));
2169 BT_ASSERT_DBG(bt_field_borrow_class_const(stack_top(msg_it->stack)->base) == fc->ir_fc);
2170
2171 /*
2172 * Reset BFCR "unsigned int" callback if it's a text
2173 * array/sequence.
2174 */
2175 if (fc->type == CTF_FIELD_CLASS_TYPE_ARRAY || fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE) {
2176 ctf_field_class_array_base *array_fc = ctf_field_class_as_array_base(fc);
2177
2178 if (array_fc->is_text) {
2179 BT_ASSERT_DBG(bt_field_get_class_type(stack_top(msg_it->stack)->base) ==
2180 BT_FIELD_CLASS_TYPE_STRING);
2181 bt_bfcr_set_unsigned_int_cb(msg_it->bfcr, bfcr_unsigned_int_cb);
2182 }
2183 }
2184
2185 /* Pop stack */
2186 stack_pop(msg_it->stack);
2187
2188 /* If the stack is not empty, increment the base's index */
2189 if (!stack_empty(msg_it->stack)) {
2190 stack_top(msg_it->stack)->index++;
2191 }
2192
2193 end:
2194 return BT_BFCR_STATUS_OK;
2195 }
2196
2197 static int64_t bfcr_get_sequence_length_cb(struct ctf_field_class *fc, void *data)
2198 {
2199 bt_field *seq_field;
2200 ctf_msg_iter *msg_it = (ctf_msg_iter *) data;
2201 bt_self_component *self_comp = msg_it->self_comp;
2202 struct ctf_field_class_sequence *seq_fc = ctf_field_class_as_sequence(fc);
2203 int64_t length;
2204 int ret;
2205
2206 length =
2207 (uint64_t) bt_g_array_index(msg_it->stored_values, uint64_t, seq_fc->stored_length_index);
2208
2209 if (G_UNLIKELY(msg_it->dry_run)) {
2210 goto end;
2211 }
2212
2213 seq_field = stack_top(msg_it->stack)->base;
2214 BT_ASSERT_DBG(seq_field);
2215
2216 /*
2217 * bfcr_get_sequence_length_cb() also gets called back for a
2218 * text sequence, but the destination field is a string field.
2219 * Only set the field's sequence length if the destination field
2220 * is a sequence field.
2221 */
2222 if (!seq_fc->base.is_text) {
2223 BT_ASSERT_DBG(bt_field_class_type_is(bt_field_get_class_type(seq_field),
2224 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY));
2225 ret = bt_field_array_dynamic_set_length(seq_field, (uint64_t) length);
2226 if (ret) {
2227 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
2228 "Cannot set dynamic array field's length field: "
2229 "msg-it-addr=%p, field-addr=%p, "
2230 "length=%" PRIu64,
2231 msg_it, seq_field, length);
2232 length = -1;
2233 }
2234 }
2235
2236 end:
2237 return length;
2238 }
2239
2240 static struct ctf_field_class *
2241 bfcr_borrow_variant_selected_field_class_cb(struct ctf_field_class *fc, void *data)
2242 {
2243 int ret;
2244 uint64_t i;
2245 int64_t option_index = -1;
2246 ctf_msg_iter *msg_it = (ctf_msg_iter *) data;
2247 ctf_field_class_variant *var_fc = ctf_field_class_as_variant(fc);
2248 struct ctf_named_field_class *selected_option = NULL;
2249 bt_self_component *self_comp = msg_it->self_comp;
2250 struct ctf_field_class *ret_fc = NULL;
2251 union
2252 {
2253 uint64_t u;
2254 int64_t i;
2255 } tag;
2256
2257 /* Get variant's tag */
2258 tag.u = bt_g_array_index(msg_it->stored_values, uint64_t, var_fc->stored_tag_index);
2259
2260 /*
2261 * Check each range to find the selected option's index.
2262 */
2263 if (var_fc->tag_fc->base.is_signed) {
2264 for (i = 0; i < var_fc->ranges->len; i++) {
2265 struct ctf_field_class_variant_range *range =
2266 ctf_field_class_variant_borrow_range_by_index(var_fc, i);
2267
2268 if (tag.i >= range->range.lower.i && tag.i <= range->range.upper.i) {
2269 option_index = (int64_t) range->option_index;
2270 break;
2271 }
2272 }
2273 } else {
2274 for (i = 0; i < var_fc->ranges->len; i++) {
2275 struct ctf_field_class_variant_range *range =
2276 ctf_field_class_variant_borrow_range_by_index(var_fc, i);
2277
2278 if (tag.u >= range->range.lower.u && tag.u <= range->range.upper.u) {
2279 option_index = (int64_t) range->option_index;
2280 break;
2281 }
2282 }
2283 }
2284
2285 if (option_index < 0) {
2286 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
2287 "Cannot find variant field class's option: "
2288 "msg-it-addr=%p, var-fc-addr=%p, u-tag=%" PRIu64 ", "
2289 "i-tag=%" PRId64,
2290 msg_it, var_fc, tag.u, tag.i);
2291 ret_fc = NULL;
2292 goto end;
2293 }
2294
2295 selected_option =
2296 ctf_field_class_variant_borrow_option_by_index(var_fc, (uint64_t) option_index);
2297
2298 if (selected_option->fc->in_ir && !msg_it->dry_run) {
2299 bt_field *var_field = stack_top(msg_it->stack)->base;
2300
2301 ret = bt_field_variant_select_option_by_index(var_field, option_index);
2302 if (ret) {
2303 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
2304 "Cannot select variant field's option field: "
2305 "msg-it-addr=%p, var-field-addr=%p, "
2306 "opt-index=%" PRId64,
2307 msg_it, var_field, option_index);
2308 ret_fc = NULL;
2309 goto end;
2310 }
2311 }
2312
2313 ret_fc = selected_option->fc;
2314
2315 end:
2316 return ret_fc;
2317 }
2318
2319 static bt_message *create_msg_stream_beginning(struct ctf_msg_iter *msg_it)
2320 {
2321 bt_self_component *self_comp = msg_it->self_comp;
2322 bt_message *msg;
2323
2324 BT_ASSERT(msg_it->stream);
2325 BT_ASSERT(msg_it->self_msg_iter);
2326 msg = bt_message_stream_beginning_create(msg_it->self_msg_iter, msg_it->stream);
2327 if (!msg) {
2328 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
2329 "Cannot create stream beginning message: "
2330 "msg-it-addr=%p, stream-addr=%p",
2331 msg_it, msg_it->stream);
2332 }
2333
2334 return msg;
2335 }
2336
2337 static bt_message *create_msg_stream_end(struct ctf_msg_iter *msg_it)
2338 {
2339 bt_self_component *self_comp = msg_it->self_comp;
2340 bt_message *msg;
2341
2342 if (!msg_it->stream) {
2343 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
2344 "Cannot create stream end message because stream is NULL: "
2345 "msg-it-addr=%p",
2346 msg_it);
2347 msg = NULL;
2348 goto end;
2349 }
2350
2351 BT_ASSERT(msg_it->self_msg_iter);
2352 msg = bt_message_stream_end_create(msg_it->self_msg_iter, msg_it->stream);
2353 if (!msg) {
2354 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
2355 "Cannot create stream end message: "
2356 "msg-it-addr=%p, stream-addr=%p",
2357 msg_it, msg_it->stream);
2358 }
2359
2360 end:
2361 return msg;
2362 }
2363
2364 static bt_message *create_msg_packet_beginning(struct ctf_msg_iter *msg_it, bool use_default_cs)
2365 {
2366 bt_self_component *self_comp = msg_it->self_comp;
2367 bt_message *msg;
2368 const bt_stream_class *sc = msg_it->meta.sc->ir_sc;
2369
2370 BT_ASSERT(msg_it->packet);
2371 BT_ASSERT(sc);
2372 BT_ASSERT(msg_it->self_msg_iter);
2373
2374 if (msg_it->meta.sc->packets_have_ts_begin) {
2375 BT_ASSERT(msg_it->snapshots.beginning_clock != UINT64_C(-1));
2376 uint64_t raw_cs_value;
2377
2378 /*
2379 * Either use the decoded packet `timestamp_begin` field or the
2380 * current stream's default clock_snapshot.
2381 */
2382 if (use_default_cs) {
2383 raw_cs_value = msg_it->default_clock_snapshot;
2384 } else {
2385 raw_cs_value = msg_it->snapshots.beginning_clock;
2386 }
2387
2388 msg = bt_message_packet_beginning_create_with_default_clock_snapshot(
2389 msg_it->self_msg_iter, msg_it->packet, raw_cs_value);
2390 } else {
2391 msg = bt_message_packet_beginning_create(msg_it->self_msg_iter, msg_it->packet);
2392 }
2393
2394 if (!msg) {
2395 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
2396 "Cannot create packet beginning message: "
2397 "msg-it-addr=%p, packet-addr=%p",
2398 msg_it, msg_it->packet);
2399 goto end;
2400 }
2401
2402 end:
2403 return msg;
2404 }
2405
2406 static bt_message *emit_delayed_packet_beg_msg(struct ctf_msg_iter *msg_it)
2407 {
2408 bool packet_beg_ts_need_fix_up;
2409
2410 msg_it->emit_delayed_packet_beginning_msg = false;
2411
2412 /*
2413 * Only fix the packet's timestamp_begin if it's larger than the first
2414 * event of the packet. If there was no event in the packet, the
2415 * `default_clock_snapshot` field will be either equal or greater than
2416 * `snapshots.beginning_clock` so there is not fix needed.
2417 */
2418 packet_beg_ts_need_fix_up = msg_it->default_clock_snapshot < msg_it->snapshots.beginning_clock;
2419
2420 /* create_msg_packet_beginning() logs errors */
2421 return create_msg_packet_beginning(msg_it, packet_beg_ts_need_fix_up);
2422 }
2423
2424 static bt_message *create_msg_packet_end(struct ctf_msg_iter *msg_it)
2425 {
2426 bt_message *msg;
2427 bool update_default_cs = true;
2428 bt_self_component *self_comp = msg_it->self_comp;
2429
2430 if (!msg_it->packet) {
2431 msg = NULL;
2432 goto end;
2433 }
2434
2435 /*
2436 * Check if we need to emit the delayed packet
2437 * beginning message instead of the packet end message.
2438 */
2439 if (G_UNLIKELY(msg_it->emit_delayed_packet_beginning_msg)) {
2440 msg = emit_delayed_packet_beg_msg(msg_it);
2441 /* Don't forget to emit the packet end message. */
2442 msg_it->state = STATE_EMIT_QUEUED_MSG_PACKET_END;
2443 goto end;
2444 }
2445
2446 /* Check if may be affected by lttng-crash timestamp_end quirk. */
2447 if (G_UNLIKELY(msg_it->meta.tc->quirks.lttng_crash)) {
2448 /*
2449 * Check if the `timestamp_begin` field is non-zero but
2450 * `timestamp_end` is zero. It means the trace is affected by
2451 * the lttng-crash packet `timestamp_end` quirk and must be
2452 * fixed up by omitting to update the default clock snapshot to
2453 * the `timestamp_end` as is typically done.
2454 */
2455 if (msg_it->snapshots.beginning_clock != 0 && msg_it->snapshots.end_clock == 0) {
2456 update_default_cs = false;
2457 }
2458 }
2459
2460 /*
2461 * Check if may be affected by lttng event-after-packet `timestamp_end`
2462 * quirk.
2463 */
2464 if (msg_it->meta.tc->quirks.lttng_event_after_packet) {
2465 /*
2466 * Check if `timestamp_end` is smaller then the current
2467 * default_clock_snapshot (which is set to the last event
2468 * decoded). It means the trace is affected by the lttng
2469 * `event-after-packet` packet `timestamp_end` quirk and must
2470 * be fixed up by omitting to update the default clock snapshot
2471 * to the `timestamp_end` as is typically done.
2472 */
2473 if (msg_it->snapshots.end_clock < msg_it->default_clock_snapshot) {
2474 update_default_cs = false;
2475 }
2476 }
2477
2478 /* Update default clock from packet's end time. */
2479 if (msg_it->snapshots.end_clock != UINT64_C(-1) && update_default_cs) {
2480 msg_it->default_clock_snapshot = msg_it->snapshots.end_clock;
2481 }
2482
2483 BT_ASSERT(msg_it->self_msg_iter);
2484
2485 if (msg_it->meta.sc->packets_have_ts_end) {
2486 BT_ASSERT(msg_it->snapshots.end_clock != UINT64_C(-1));
2487 msg = bt_message_packet_end_create_with_default_clock_snapshot(
2488 msg_it->self_msg_iter, msg_it->packet, msg_it->default_clock_snapshot);
2489 } else {
2490 msg = bt_message_packet_end_create(msg_it->self_msg_iter, msg_it->packet);
2491 }
2492
2493 if (!msg) {
2494 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
2495 "Cannot create packet end message: "
2496 "msg-it-addr=%p, packet-addr=%p",
2497 msg_it, msg_it->packet);
2498 goto end;
2499 }
2500
2501 BT_PACKET_PUT_REF_AND_RESET(msg_it->packet);
2502
2503 end:
2504 return msg;
2505 }
2506
2507 static bt_message *create_msg_discarded_events(struct ctf_msg_iter *msg_it)
2508 {
2509 bt_message *msg;
2510 bt_self_component *self_comp = msg_it->self_comp;
2511 uint64_t beginning_raw_value = UINT64_C(-1);
2512 uint64_t end_raw_value = UINT64_C(-1);
2513
2514 BT_ASSERT(msg_it->self_msg_iter);
2515 BT_ASSERT(msg_it->stream);
2516 BT_ASSERT(msg_it->meta.sc->has_discarded_events);
2517
2518 if (msg_it->meta.sc->discarded_events_have_default_cs) {
2519 if (msg_it->prev_packet_snapshots.discarded_events == UINT64_C(-1)) {
2520 /*
2521 * We discarded events, but before (and possibly
2522 * including) the current packet: use this packet's time
2523 * range, and do not have a specific count.
2524 */
2525 beginning_raw_value = msg_it->snapshots.beginning_clock;
2526 end_raw_value = msg_it->snapshots.end_clock;
2527 } else {
2528 beginning_raw_value = msg_it->prev_packet_snapshots.end_clock;
2529 end_raw_value = msg_it->snapshots.end_clock;
2530 }
2531
2532 BT_ASSERT(beginning_raw_value != UINT64_C(-1));
2533 BT_ASSERT(end_raw_value != UINT64_C(-1));
2534 msg = bt_message_discarded_events_create_with_default_clock_snapshots(
2535 msg_it->self_msg_iter, msg_it->stream, beginning_raw_value, end_raw_value);
2536 } else {
2537 msg = bt_message_discarded_events_create(msg_it->self_msg_iter, msg_it->stream);
2538 }
2539
2540 if (!msg) {
2541 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
2542 "Cannot create discarded events message: "
2543 "msg-it-addr=%p, stream-addr=%p",
2544 msg_it, msg_it->stream);
2545 goto end;
2546 }
2547
2548 if (msg_it->prev_packet_snapshots.discarded_events != UINT64_C(-1)) {
2549 bt_message_discarded_events_set_count(msg,
2550 msg_it->snapshots.discarded_events -
2551 msg_it->prev_packet_snapshots.discarded_events);
2552 }
2553
2554 end:
2555 return msg;
2556 }
2557
2558 static bt_message *create_msg_discarded_packets(struct ctf_msg_iter *msg_it)
2559 {
2560 bt_message *msg;
2561 bt_self_component *self_comp = msg_it->self_comp;
2562
2563 BT_ASSERT(msg_it->self_msg_iter);
2564 BT_ASSERT(msg_it->stream);
2565 BT_ASSERT(msg_it->meta.sc->has_discarded_packets);
2566 BT_ASSERT(msg_it->prev_packet_snapshots.packets != UINT64_C(-1));
2567
2568 if (msg_it->meta.sc->discarded_packets_have_default_cs) {
2569 BT_ASSERT(msg_it->prev_packet_snapshots.end_clock != UINT64_C(-1));
2570 BT_ASSERT(msg_it->snapshots.beginning_clock != UINT64_C(-1));
2571 msg = bt_message_discarded_packets_create_with_default_clock_snapshots(
2572 msg_it->self_msg_iter, msg_it->stream, msg_it->prev_packet_snapshots.end_clock,
2573 msg_it->snapshots.beginning_clock);
2574 } else {
2575 msg = bt_message_discarded_packets_create(msg_it->self_msg_iter, msg_it->stream);
2576 }
2577
2578 if (!msg) {
2579 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
2580 "Cannot create discarded packets message: "
2581 "msg-it-addr=%p, stream-addr=%p",
2582 msg_it, msg_it->stream);
2583 goto end;
2584 }
2585
2586 bt_message_discarded_packets_set_count(msg, msg_it->snapshots.packets -
2587 msg_it->prev_packet_snapshots.packets - 1);
2588
2589 end:
2590 return msg;
2591 }
2592
2593 struct ctf_msg_iter *ctf_msg_iter_create(struct ctf_trace_class *tc, size_t max_request_sz,
2594 struct ctf_msg_iter_medium_ops medops, void *data,
2595 bt_logging_level log_level, bt_self_component *self_comp,
2596 bt_self_message_iterator *self_msg_iter)
2597 {
2598 struct ctf_msg_iter *msg_it = NULL;
2599 struct bt_bfcr_cbs cbs = {
2600 .classes =
2601 {
2602 .signed_int = bfcr_signed_int_cb,
2603 .unsigned_int = bfcr_unsigned_int_cb,
2604 .floating_point = bfcr_floating_point_cb,
2605 .string_begin = bfcr_string_begin_cb,
2606 .string = bfcr_string_cb,
2607 .string_end = bfcr_string_end_cb,
2608 .compound_begin = bfcr_compound_begin_cb,
2609 .compound_end = bfcr_compound_end_cb,
2610 },
2611 .query =
2612 {
2613 .get_sequence_length = bfcr_get_sequence_length_cb,
2614 .borrow_variant_selected_field_class = bfcr_borrow_variant_selected_field_class_cb,
2615 },
2616 };
2617
2618 BT_ASSERT(tc);
2619 BT_ASSERT(medops.request_bytes);
2620 BT_ASSERT(medops.borrow_stream);
2621 BT_ASSERT(max_request_sz > 0);
2622
2623 BT_COMP_LOG_CUR_LVL(BT_LOG_DEBUG, log_level, self_comp,
2624 "Creating CTF plugin message iterator: "
2625 "trace-addr=%p, max-request-size=%zu, "
2626 "data=%p, log-level=%s",
2627 tc, max_request_sz, data, bt_common_logging_level_string(log_level));
2628 msg_it = g_new0(struct ctf_msg_iter, 1);
2629 if (!msg_it) {
2630 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp,
2631 "Failed to allocate one CTF plugin message iterator.");
2632 goto end;
2633 }
2634 msg_it->self_comp = self_comp;
2635 msg_it->self_msg_iter = self_msg_iter;
2636 msg_it->log_level = log_level;
2637 msg_it->meta.tc = tc;
2638 msg_it->medium.medops = medops;
2639 msg_it->medium.max_request_sz = max_request_sz;
2640 msg_it->medium.data = data;
2641 msg_it->stack = stack_new(msg_it);
2642 msg_it->stored_values = g_array_new(FALSE, TRUE, sizeof(uint64_t));
2643 g_array_set_size(msg_it->stored_values, tc->stored_value_count);
2644
2645 if (!msg_it->stack) {
2646 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Failed to create field stack.");
2647 goto error;
2648 }
2649
2650 msg_it->bfcr = bt_bfcr_create(cbs, msg_it, log_level, NULL);
2651 if (!msg_it->bfcr) {
2652 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Failed to create binary class reader (BFCR).");
2653 goto error;
2654 }
2655
2656 ctf_msg_iter_reset(msg_it);
2657 BT_COMP_LOGD("Created CTF plugin message iterator: "
2658 "trace-addr=%p, max-request-size=%zu, "
2659 "data=%p, msg-it-addr=%p, log-level=%s",
2660 tc, max_request_sz, data, msg_it, bt_common_logging_level_string(log_level));
2661 msg_it->cur_packet_offset = 0;
2662
2663 end:
2664 return msg_it;
2665
2666 error:
2667 ctf_msg_iter_destroy(msg_it);
2668 msg_it = NULL;
2669 goto end;
2670 }
2671
2672 void ctf_msg_iter_destroy(struct ctf_msg_iter *msg_it)
2673 {
2674 BT_PACKET_PUT_REF_AND_RESET(msg_it->packet);
2675 BT_STREAM_PUT_REF_AND_RESET(msg_it->stream);
2676 release_all_dscopes(msg_it);
2677
2678 BT_COMP_LOGD("Destroying CTF plugin message iterator: addr=%p", msg_it);
2679
2680 if (msg_it->stack) {
2681 BT_COMP_LOGD_STR("Destroying field stack.");
2682 stack_destroy(msg_it->stack);
2683 }
2684
2685 if (msg_it->bfcr) {
2686 BT_COMP_LOGD("Destroying BFCR: bfcr-addr=%p", msg_it->bfcr);
2687 bt_bfcr_destroy(msg_it->bfcr);
2688 }
2689
2690 if (msg_it->stored_values) {
2691 g_array_free(msg_it->stored_values, TRUE);
2692 }
2693
2694 g_free(msg_it);
2695 }
2696
2697 enum ctf_msg_iter_status ctf_msg_iter_get_next_message(struct ctf_msg_iter *msg_it,
2698 const bt_message **message)
2699 {
2700 enum ctf_msg_iter_status status = CTF_MSG_ITER_STATUS_OK;
2701 bt_self_component *self_comp = msg_it->self_comp;
2702
2703 BT_ASSERT_DBG(msg_it);
2704 BT_ASSERT_DBG(message);
2705 BT_COMP_LOGD("Getting next message: msg-it-addr=%p", msg_it);
2706
2707 while (true) {
2708 status = handle_state(msg_it);
2709 if (G_UNLIKELY(status == CTF_MSG_ITER_STATUS_AGAIN)) {
2710 BT_COMP_LOGD_STR("Medium returned CTF_MSG_ITER_STATUS_AGAIN.");
2711 goto end;
2712 } else if (G_UNLIKELY(status != CTF_MSG_ITER_STATUS_OK)) {
2713 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Cannot handle state: msg-it-addr=%p, state=%s",
2714 msg_it, state_string(msg_it->state));
2715 goto end;
2716 }
2717
2718 switch (msg_it->state) {
2719 case STATE_EMIT_MSG_EVENT:
2720 BT_ASSERT_DBG(msg_it->event_msg);
2721
2722 /*
2723 * Check if we need to emit the delayed packet
2724 * beginning message instead of the event message.
2725 */
2726 if (G_UNLIKELY(msg_it->emit_delayed_packet_beginning_msg)) {
2727 *message = emit_delayed_packet_beg_msg(msg_it);
2728 if (!*message) {
2729 status = CTF_MSG_ITER_STATUS_ERROR;
2730 }
2731
2732 /*
2733 * Don't forget to emit the event message of
2734 * the event record that was just decoded.
2735 */
2736 msg_it->state = STATE_EMIT_QUEUED_MSG_EVENT;
2737
2738 } else {
2739 *message = msg_it->event_msg;
2740 msg_it->event_msg = NULL;
2741 }
2742 goto end;
2743 case STATE_EMIT_MSG_DISCARDED_EVENTS:
2744 /* create_msg_discared_events() logs errors */
2745 *message = create_msg_discarded_events(msg_it);
2746
2747 if (!*message) {
2748 status = CTF_MSG_ITER_STATUS_ERROR;
2749 }
2750
2751 goto end;
2752 case STATE_EMIT_MSG_DISCARDED_PACKETS:
2753 /* create_msg_discared_packets() logs errors */
2754 *message = create_msg_discarded_packets(msg_it);
2755
2756 if (!*message) {
2757 status = CTF_MSG_ITER_STATUS_ERROR;
2758 }
2759
2760 goto end;
2761 case STATE_EMIT_MSG_PACKET_BEGINNING:
2762 if (G_UNLIKELY(msg_it->meta.tc->quirks.barectf_event_before_packet)) {
2763 msg_it->emit_delayed_packet_beginning_msg = true;
2764 /*
2765 * There is no message to return yet as this
2766 * packet beginning message is delayed until we
2767 * decode the first event message of the
2768 * packet.
2769 */
2770 break;
2771 } else {
2772 /* create_msg_packet_beginning() logs errors */
2773 *message = create_msg_packet_beginning(msg_it, false);
2774 if (!*message) {
2775 status = CTF_MSG_ITER_STATUS_ERROR;
2776 }
2777 }
2778
2779 goto end;
2780 case STATE_EMIT_MSG_PACKET_END_SINGLE:
2781 case STATE_EMIT_MSG_PACKET_END_MULTI:
2782 /* create_msg_packet_end() logs errors */
2783 *message = create_msg_packet_end(msg_it);
2784
2785 if (!*message) {
2786 status = CTF_MSG_ITER_STATUS_ERROR;
2787 }
2788
2789 goto end;
2790 case STATE_EMIT_MSG_STREAM_BEGINNING:
2791 /* create_msg_stream_beginning() logs errors */
2792 *message = create_msg_stream_beginning(msg_it);
2793 msg_it->emit_stream_beginning_message = false;
2794 msg_it->emit_stream_end_message = true;
2795
2796 if (!*message) {
2797 status = CTF_MSG_ITER_STATUS_ERROR;
2798 }
2799
2800 goto end;
2801 case STATE_EMIT_MSG_STREAM_END:
2802 /* create_msg_stream_end() logs errors */
2803 *message = create_msg_stream_end(msg_it);
2804 msg_it->emit_stream_end_message = false;
2805
2806 if (!*message) {
2807 status = CTF_MSG_ITER_STATUS_ERROR;
2808 }
2809
2810 goto end;
2811 case STATE_DONE:
2812 status = CTF_MSG_ITER_STATUS_EOF;
2813 goto end;
2814 default:
2815 /* Non-emitting state: continue */
2816 break;
2817 }
2818 }
2819
2820 end:
2821 return status;
2822 }
2823
2824 static enum ctf_msg_iter_status decode_until_state(struct ctf_msg_iter *msg_it,
2825 enum state target_state_1,
2826 enum state target_state_2)
2827 {
2828 enum ctf_msg_iter_status status = CTF_MSG_ITER_STATUS_OK;
2829 bt_self_component *self_comp = msg_it->self_comp;
2830
2831 BT_ASSERT_DBG(msg_it);
2832
2833 do {
2834 /*
2835 * Check if we reached the state at which we want to stop
2836 * decoding.
2837 */
2838 if (msg_it->state == target_state_1 || msg_it->state == target_state_2) {
2839 goto end;
2840 }
2841
2842 status = handle_state(msg_it);
2843 if (G_UNLIKELY(status == CTF_MSG_ITER_STATUS_AGAIN)) {
2844 BT_COMP_LOGD_STR("Medium returned CTF_MSG_ITER_STATUS_AGAIN.");
2845 goto end;
2846 } else if (G_UNLIKELY(status != CTF_MSG_ITER_STATUS_OK)) {
2847 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Cannot handle state: msg-it-addr=%p, state=%s",
2848 msg_it, state_string(msg_it->state));
2849 goto end;
2850 }
2851
2852 switch (msg_it->state) {
2853 case STATE_INIT:
2854 case STATE_SWITCH_PACKET:
2855 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
2856 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
2857 case STATE_AFTER_TRACE_PACKET_HEADER:
2858 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN:
2859 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE:
2860 case STATE_AFTER_STREAM_PACKET_CONTEXT:
2861 case STATE_EMIT_MSG_STREAM_BEGINNING:
2862 case STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS:
2863 case STATE_EMIT_MSG_DISCARDED_EVENTS:
2864 case STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS:
2865 case STATE_EMIT_MSG_DISCARDED_PACKETS:
2866 case STATE_EMIT_MSG_PACKET_BEGINNING:
2867 case STATE_DSCOPE_EVENT_HEADER_BEGIN:
2868 case STATE_DSCOPE_EVENT_HEADER_CONTINUE:
2869 case STATE_AFTER_EVENT_HEADER:
2870 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN:
2871 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE:
2872 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN:
2873 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE:
2874 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN:
2875 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE:
2876 case STATE_EMIT_MSG_EVENT:
2877 case STATE_EMIT_QUEUED_MSG_EVENT:
2878 case STATE_SKIP_PACKET_PADDING:
2879 case STATE_EMIT_MSG_PACKET_END_MULTI:
2880 case STATE_EMIT_MSG_PACKET_END_SINGLE:
2881 case STATE_EMIT_QUEUED_MSG_PACKET_END:
2882 case STATE_EMIT_MSG_STREAM_END:
2883 break;
2884 case STATE_DONE:
2885 /* fall-through */
2886 default:
2887 /* We should never get to the STATE_DONE state. */
2888 BT_COMP_LOGF("Unexpected state: msg-it-addr=%p, state=%s", msg_it,
2889 state_string(msg_it->state));
2890 bt_common_abort();
2891 }
2892 } while (true);
2893
2894 end:
2895 return status;
2896 }
2897
2898 static enum ctf_msg_iter_status read_packet_header_context_fields(struct ctf_msg_iter *msg_it)
2899 {
2900 int ret;
2901 enum ctf_msg_iter_status status = CTF_MSG_ITER_STATUS_OK;
2902
2903 status = decode_until_state(msg_it, STATE_EMIT_MSG_PACKET_BEGINNING, (state) -1);
2904 if (status != CTF_MSG_ITER_STATUS_OK) {
2905 goto end;
2906 }
2907
2908 ret = set_current_packet_content_sizes(msg_it);
2909 if (ret) {
2910 status = CTF_MSG_ITER_STATUS_ERROR;
2911 goto end;
2912 }
2913
2914 end:
2915 return status;
2916 }
2917
2918 enum ctf_msg_iter_status ctf_msg_iter_seek(struct ctf_msg_iter *msg_it, off_t offset)
2919 {
2920 enum ctf_msg_iter_status status = CTF_MSG_ITER_STATUS_OK;
2921 enum ctf_msg_iter_medium_status medium_status;
2922
2923 BT_ASSERT(msg_it);
2924 BT_ASSERT(offset >= 0);
2925 BT_ASSERT(msg_it->medium.medops.seek);
2926
2927 medium_status = msg_it->medium.medops.seek(offset, msg_it->medium.data);
2928 if (medium_status != CTF_MSG_ITER_MEDIUM_STATUS_OK) {
2929 if (medium_status == CTF_MSG_ITER_MEDIUM_STATUS_EOF) {
2930 status = CTF_MSG_ITER_STATUS_EOF;
2931 } else {
2932 status = CTF_MSG_ITER_STATUS_ERROR;
2933 goto end;
2934 }
2935 }
2936
2937 ctf_msg_iter_reset(msg_it);
2938 msg_it->cur_packet_offset = offset;
2939
2940 end:
2941 return status;
2942 }
2943
2944 static enum ctf_msg_iter_status clock_snapshot_at_msg_iter_state(struct ctf_msg_iter *msg_it,
2945 enum state target_state_1,
2946 enum state target_state_2,
2947 uint64_t *clock_snapshot)
2948 {
2949 enum ctf_msg_iter_status status = CTF_MSG_ITER_STATUS_OK;
2950
2951 BT_ASSERT_DBG(msg_it);
2952 BT_ASSERT_DBG(clock_snapshot);
2953 status = decode_until_state(msg_it, target_state_1, target_state_2);
2954 if (status != CTF_MSG_ITER_STATUS_OK) {
2955 goto end;
2956 }
2957
2958 *clock_snapshot = msg_it->default_clock_snapshot;
2959 end:
2960 return status;
2961 }
2962
2963 enum ctf_msg_iter_status
2964 ctf_msg_iter_curr_packet_first_event_clock_snapshot(struct ctf_msg_iter *msg_it,
2965 uint64_t *first_clock_snapshot)
2966 {
2967 return clock_snapshot_at_msg_iter_state(msg_it, STATE_AFTER_EVENT_HEADER, (state) -1,
2968 first_clock_snapshot);
2969 }
2970
2971 enum ctf_msg_iter_status
2972 ctf_msg_iter_curr_packet_last_event_clock_snapshot(struct ctf_msg_iter *msg_it,
2973 uint64_t *last_clock_snapshot)
2974 {
2975 return clock_snapshot_at_msg_iter_state(msg_it, STATE_EMIT_MSG_PACKET_END_SINGLE,
2976 STATE_EMIT_MSG_PACKET_END_MULTI, last_clock_snapshot);
2977 }
2978
2979 enum ctf_msg_iter_status
2980 ctf_msg_iter_get_packet_properties(struct ctf_msg_iter *msg_it,
2981 struct ctf_msg_iter_packet_properties *props)
2982 {
2983 enum ctf_msg_iter_status status;
2984
2985 BT_ASSERT_DBG(msg_it);
2986 BT_ASSERT_DBG(props);
2987 status = read_packet_header_context_fields(msg_it);
2988 if (status != CTF_MSG_ITER_STATUS_OK) {
2989 goto end;
2990 }
2991
2992 props->exp_packet_total_size = msg_it->cur_exp_packet_total_size;
2993 props->exp_packet_content_size = msg_it->cur_exp_packet_content_size;
2994 props->stream_class_id = (uint64_t) msg_it->cur_stream_class_id;
2995 props->data_stream_id = msg_it->cur_data_stream_id;
2996 props->snapshots.discarded_events = msg_it->snapshots.discarded_events;
2997 props->snapshots.packets = msg_it->snapshots.packets;
2998 props->snapshots.beginning_clock = msg_it->snapshots.beginning_clock;
2999 props->snapshots.end_clock = msg_it->snapshots.end_clock;
3000
3001 end:
3002 return status;
3003 }
3004
3005 void ctf_msg_iter_set_dry_run(struct ctf_msg_iter *msg_it, bool val)
3006 {
3007 msg_it->dry_run = val;
3008 }
This page took 0.087158 seconds and 3 git commands to generate.