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