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