ctf: only reset state of `bt_msg_iter` on _reset()
[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->snapshots.discarded_events = UINT64_C(-1);
1795 notit->snapshots.packets = UINT64_C(-1);
1796 notit->prev_packet_snapshots.discarded_events = UINT64_C(-1);
1797 notit->prev_packet_snapshots.packets = UINT64_C(-1);
1798 notit->prev_packet_snapshots.beginning_clock = UINT64_C(-1);
1799 notit->prev_packet_snapshots.end_clock = UINT64_C(-1);
1800 }
1801
1802 static
1803 bt_field *borrow_next_field(struct bt_msg_iter *notit)
1804 {
1805 bt_field *next_field = NULL;
1806 bt_field *base_field;
1807 const bt_field_class *base_fc;
1808 size_t index;
1809
1810 BT_ASSERT(!stack_empty(notit->stack));
1811 index = stack_top(notit->stack)->index;
1812 base_field = stack_top(notit->stack)->base;
1813 BT_ASSERT(base_field);
1814 base_fc = bt_field_borrow_class_const(base_field);
1815 BT_ASSERT(base_fc);
1816
1817 switch (bt_field_class_get_type(base_fc)) {
1818 case BT_FIELD_CLASS_TYPE_STRUCTURE:
1819 {
1820 BT_ASSERT(index <
1821 bt_field_class_structure_get_member_count(
1822 bt_field_borrow_class_const(
1823 base_field)));
1824 next_field =
1825 bt_field_structure_borrow_member_field_by_index(
1826 base_field, index);
1827 break;
1828 }
1829 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
1830 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
1831 BT_ASSERT(index < bt_field_array_get_length(base_field));
1832 next_field = bt_field_array_borrow_element_field_by_index(
1833 base_field, index);
1834 break;
1835 case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR:
1836 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR:
1837 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR:
1838 BT_ASSERT(index == 0);
1839 next_field = bt_field_variant_borrow_selected_option_field(
1840 base_field);
1841 break;
1842 default:
1843 abort();
1844 }
1845
1846 BT_ASSERT(next_field);
1847 return next_field;
1848 }
1849
1850 static
1851 void update_default_clock(struct bt_msg_iter *notit, uint64_t new_val,
1852 uint64_t new_val_size)
1853 {
1854 uint64_t new_val_mask;
1855 uint64_t cur_value_masked;
1856
1857 BT_ASSERT(new_val_size > 0);
1858
1859 /*
1860 * Special case for a 64-bit new value, which is the limit
1861 * of a clock value as of this version: overwrite the
1862 * current value directly.
1863 */
1864 if (new_val_size == 64) {
1865 notit->default_clock_snapshot = new_val;
1866 goto end;
1867 }
1868
1869 new_val_mask = (1ULL << new_val_size) - 1;
1870 cur_value_masked = notit->default_clock_snapshot & new_val_mask;
1871
1872 if (new_val < cur_value_masked) {
1873 /*
1874 * It looks like a wrap happened on the number of bits
1875 * of the requested new value. Assume that the clock
1876 * value wrapped only one time.
1877 */
1878 notit->default_clock_snapshot += new_val_mask + 1;
1879 }
1880
1881 /* Clear the low bits of the current clock value. */
1882 notit->default_clock_snapshot &= ~new_val_mask;
1883
1884 /* Set the low bits of the current clock value. */
1885 notit->default_clock_snapshot |= new_val;
1886
1887 end:
1888 BT_COMP_LOGT("Updated default clock's value from integer field's value: "
1889 "value=%" PRIu64, notit->default_clock_snapshot);
1890 }
1891
1892 static
1893 enum bt_bfcr_status bfcr_unsigned_int_cb(uint64_t value,
1894 struct ctf_field_class *fc, void *data)
1895 {
1896 struct bt_msg_iter *notit = data;
1897 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
1898 bt_field *field = NULL;
1899 struct ctf_field_class_int *int_fc = (void *) fc;
1900
1901 BT_COMP_LOGT("Unsigned integer function called from BFCR: "
1902 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1903 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64,
1904 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
1905
1906 if (G_LIKELY(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE)) {
1907 goto update_def_clock;
1908 }
1909
1910 switch (int_fc->meaning) {
1911 case CTF_FIELD_CLASS_MEANING_EVENT_CLASS_ID:
1912 notit->cur_event_class_id = value;
1913 break;
1914 case CTF_FIELD_CLASS_MEANING_DATA_STREAM_ID:
1915 notit->cur_data_stream_id = value;
1916 break;
1917 case CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME:
1918 notit->snapshots.beginning_clock = value;
1919 break;
1920 case CTF_FIELD_CLASS_MEANING_PACKET_END_TIME:
1921 notit->snapshots.end_clock = value;
1922 break;
1923 case CTF_FIELD_CLASS_MEANING_STREAM_CLASS_ID:
1924 notit->cur_stream_class_id = value;
1925 break;
1926 case CTF_FIELD_CLASS_MEANING_MAGIC:
1927 if (value != 0xc1fc1fc1) {
1928 BT_COMP_LOGW("Invalid CTF magic number: notit-addr=%p, "
1929 "magic=%" PRIx64, notit, value);
1930 status = BT_BFCR_STATUS_ERROR;
1931 goto end;
1932 }
1933
1934 break;
1935 case CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT:
1936 notit->snapshots.packets = value;
1937 break;
1938 case CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT:
1939 notit->snapshots.discarded_events = value;
1940 break;
1941 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_TOTAL_SIZE:
1942 notit->cur_exp_packet_total_size = value;
1943 break;
1944 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_CONTENT_SIZE:
1945 notit->cur_exp_packet_content_size = value;
1946 break;
1947 default:
1948 abort();
1949 }
1950
1951 update_def_clock:
1952 if (G_UNLIKELY(int_fc->mapped_clock_class)) {
1953 update_default_clock(notit, value, int_fc->base.size);
1954 }
1955
1956 if (G_UNLIKELY(int_fc->storing_index >= 0)) {
1957 g_array_index(notit->stored_values, uint64_t,
1958 (uint64_t) int_fc->storing_index) = value;
1959 }
1960
1961 if (G_UNLIKELY(!fc->in_ir)) {
1962 goto end;
1963 }
1964
1965 field = borrow_next_field(notit);
1966 BT_ASSERT(field);
1967 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
1968 BT_ASSERT(bt_field_get_class_type(field) ==
1969 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER ||
1970 bt_field_get_class_type(field) ==
1971 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION);
1972 bt_field_integer_unsigned_set_value(field, value);
1973 stack_top(notit->stack)->index++;
1974
1975 end:
1976 return status;
1977 }
1978
1979 static
1980 enum bt_bfcr_status bfcr_unsigned_int_char_cb(uint64_t value,
1981 struct ctf_field_class *fc, void *data)
1982 {
1983 int ret;
1984 struct bt_msg_iter *notit = data;
1985 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
1986 bt_field *string_field = NULL;
1987 struct ctf_field_class_int *int_fc = (void *) fc;
1988 char str[2] = {'\0', '\0'};
1989
1990 BT_COMP_LOGT("Unsigned integer character function called from BFCR: "
1991 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1992 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64,
1993 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
1994 BT_ASSERT(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE);
1995 BT_ASSERT(!int_fc->mapped_clock_class);
1996 BT_ASSERT(int_fc->storing_index < 0);
1997
1998 if (G_UNLIKELY(!fc->in_ir)) {
1999 goto end;
2000 }
2001
2002 if (notit->done_filling_string) {
2003 goto end;
2004 }
2005
2006 if (value == 0) {
2007 notit->done_filling_string = true;
2008 goto end;
2009 }
2010
2011 string_field = stack_top(notit->stack)->base;
2012 BT_ASSERT(bt_field_get_class_type(string_field) ==
2013 BT_FIELD_CLASS_TYPE_STRING);
2014
2015 /* Append character */
2016 str[0] = (char) value;
2017 ret = bt_field_string_append_with_length(string_field, str, 1);
2018 if (ret) {
2019 BT_COMP_LOGE("Cannot append character to string field's value: "
2020 "notit-addr=%p, field-addr=%p, ret=%d",
2021 notit, string_field, ret);
2022 status = BT_BFCR_STATUS_ERROR;
2023 goto end;
2024 }
2025
2026 end:
2027 return status;
2028 }
2029
2030 static
2031 enum bt_bfcr_status bfcr_signed_int_cb(int64_t value,
2032 struct ctf_field_class *fc, void *data)
2033 {
2034 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
2035 bt_field *field = NULL;
2036 struct bt_msg_iter *notit = data;
2037 struct ctf_field_class_int *int_fc = (void *) fc;
2038
2039 BT_COMP_LOGT("Signed integer function called from BFCR: "
2040 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2041 "fc-type=%d, fc-in-ir=%d, value=%" PRId64,
2042 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
2043 BT_ASSERT(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE);
2044
2045 if (G_UNLIKELY(int_fc->storing_index >= 0)) {
2046 g_array_index(notit->stored_values, uint64_t,
2047 (uint64_t) int_fc->storing_index) = (uint64_t) value;
2048 }
2049
2050 if (G_UNLIKELY(!fc->in_ir)) {
2051 goto end;
2052 }
2053
2054 field = borrow_next_field(notit);
2055 BT_ASSERT(field);
2056 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
2057 BT_ASSERT(bt_field_get_class_type(field) ==
2058 BT_FIELD_CLASS_TYPE_SIGNED_INTEGER ||
2059 bt_field_get_class_type(field) ==
2060 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION);
2061 bt_field_integer_signed_set_value(field, value);
2062 stack_top(notit->stack)->index++;
2063
2064 end:
2065 return status;
2066 }
2067
2068 static
2069 enum bt_bfcr_status bfcr_floating_point_cb(double value,
2070 struct ctf_field_class *fc, void *data)
2071 {
2072 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
2073 bt_field *field = NULL;
2074 struct bt_msg_iter *notit = data;
2075
2076 BT_COMP_LOGT("Floating point number function called from BFCR: "
2077 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2078 "fc-type=%d, fc-in-ir=%d, value=%f",
2079 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
2080
2081 if (G_UNLIKELY(!fc->in_ir)) {
2082 goto end;
2083 }
2084
2085 field = borrow_next_field(notit);
2086 BT_ASSERT(field);
2087 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
2088 BT_ASSERT(bt_field_get_class_type(field) ==
2089 BT_FIELD_CLASS_TYPE_REAL);
2090 bt_field_real_set_value(field, value);
2091 stack_top(notit->stack)->index++;
2092
2093 end:
2094 return status;
2095 }
2096
2097 static
2098 enum bt_bfcr_status bfcr_string_begin_cb(
2099 struct ctf_field_class *fc, void *data)
2100 {
2101 bt_field *field = NULL;
2102 struct bt_msg_iter *notit = data;
2103
2104 BT_COMP_LOGT("String (beginning) function called from BFCR: "
2105 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2106 "fc-type=%d, fc-in-ir=%d",
2107 notit, notit->bfcr, fc, fc->type, fc->in_ir);
2108
2109 if (G_UNLIKELY(!fc->in_ir)) {
2110 goto end;
2111 }
2112
2113 field = borrow_next_field(notit);
2114 BT_ASSERT(field);
2115 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
2116 BT_ASSERT(bt_field_get_class_type(field) ==
2117 BT_FIELD_CLASS_TYPE_STRING);
2118 bt_field_string_clear(field);
2119
2120 /*
2121 * Push on stack. Not a compound class per se, but we know that
2122 * only bfcr_string_cb() may be called between this call and a
2123 * subsequent call to bfcr_string_end_cb().
2124 */
2125 stack_push(notit->stack, field);
2126
2127 end:
2128 return BT_BFCR_STATUS_OK;
2129 }
2130
2131 static
2132 enum bt_bfcr_status bfcr_string_cb(const char *value,
2133 size_t len, struct ctf_field_class *fc, void *data)
2134 {
2135 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
2136 bt_field *field = NULL;
2137 struct bt_msg_iter *notit = data;
2138 int ret;
2139
2140 BT_COMP_LOGT("String (substring) function called from BFCR: "
2141 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2142 "fc-type=%d, fc-in-ir=%d, string-length=%zu",
2143 notit, notit->bfcr, fc, fc->type, fc->in_ir,
2144 len);
2145
2146 if (G_UNLIKELY(!fc->in_ir)) {
2147 goto end;
2148 }
2149
2150 field = stack_top(notit->stack)->base;
2151 BT_ASSERT(field);
2152
2153 /* Append current substring */
2154 ret = bt_field_string_append_with_length(field, value, len);
2155 if (ret) {
2156 BT_COMP_LOGE("Cannot append substring to string field's value: "
2157 "notit-addr=%p, field-addr=%p, string-length=%zu, "
2158 "ret=%d", notit, field, len, ret);
2159 status = BT_BFCR_STATUS_ERROR;
2160 goto end;
2161 }
2162
2163 end:
2164 return status;
2165 }
2166
2167 static
2168 enum bt_bfcr_status bfcr_string_end_cb(
2169 struct ctf_field_class *fc, void *data)
2170 {
2171 struct bt_msg_iter *notit = data;
2172
2173 BT_COMP_LOGT("String (end) function called from BFCR: "
2174 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2175 "fc-type=%d, fc-in-ir=%d",
2176 notit, notit->bfcr, fc, fc->type, fc->in_ir);
2177
2178 if (G_UNLIKELY(!fc->in_ir)) {
2179 goto end;
2180 }
2181
2182 /* Pop string field */
2183 stack_pop(notit->stack);
2184
2185 /* Go to next field */
2186 stack_top(notit->stack)->index++;
2187
2188 end:
2189 return BT_BFCR_STATUS_OK;
2190 }
2191
2192 enum bt_bfcr_status bfcr_compound_begin_cb(
2193 struct ctf_field_class *fc, void *data)
2194 {
2195 struct bt_msg_iter *notit = data;
2196 bt_field *field;
2197
2198 BT_COMP_LOGT("Compound (beginning) function called from BFCR: "
2199 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2200 "fc-type=%d, fc-in-ir=%d",
2201 notit, notit->bfcr, fc, fc->type, fc->in_ir);
2202
2203 if (!fc->in_ir) {
2204 goto end;
2205 }
2206
2207 /* Borrow field */
2208 if (stack_empty(notit->stack)) {
2209 /* Root: already set by read_dscope_begin_state() */
2210 field = notit->cur_dscope_field;
2211 } else {
2212 field = borrow_next_field(notit);
2213 BT_ASSERT(field);
2214 }
2215
2216 /* Push field */
2217 BT_ASSERT(field);
2218 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
2219 stack_push(notit->stack, field);
2220
2221 /*
2222 * Change BFCR "unsigned int" callback if it's a text
2223 * array/sequence.
2224 */
2225 if (fc->type == CTF_FIELD_CLASS_TYPE_ARRAY ||
2226 fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE) {
2227 struct ctf_field_class_array_base *array_fc = (void *) fc;
2228
2229 if (array_fc->is_text) {
2230 BT_ASSERT(bt_field_get_class_type(field) ==
2231 BT_FIELD_CLASS_TYPE_STRING);
2232 notit->done_filling_string = false;
2233 bt_field_string_clear(field);
2234 bt_bfcr_set_unsigned_int_cb(notit->bfcr,
2235 bfcr_unsigned_int_char_cb);
2236 }
2237 }
2238
2239 end:
2240 return BT_BFCR_STATUS_OK;
2241 }
2242
2243 enum bt_bfcr_status bfcr_compound_end_cb(
2244 struct ctf_field_class *fc, void *data)
2245 {
2246 struct bt_msg_iter *notit = data;
2247
2248 BT_COMP_LOGT("Compound (end) function called from BFCR: "
2249 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2250 "fc-type=%d, fc-in-ir=%d",
2251 notit, notit->bfcr, fc, fc->type, fc->in_ir);
2252
2253 if (!fc->in_ir) {
2254 goto end;
2255 }
2256
2257 BT_ASSERT(!stack_empty(notit->stack));
2258 BT_ASSERT(bt_field_borrow_class_const(stack_top(notit->stack)->base) ==
2259 fc->ir_fc);
2260
2261 /*
2262 * Reset BFCR "unsigned int" callback if it's a text
2263 * array/sequence.
2264 */
2265 if (fc->type == CTF_FIELD_CLASS_TYPE_ARRAY ||
2266 fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE) {
2267 struct ctf_field_class_array_base *array_fc = (void *) fc;
2268
2269 if (array_fc->is_text) {
2270 BT_ASSERT(bt_field_get_class_type(
2271 stack_top(notit->stack)->base) ==
2272 BT_FIELD_CLASS_TYPE_STRING);
2273 bt_bfcr_set_unsigned_int_cb(notit->bfcr,
2274 bfcr_unsigned_int_cb);
2275 }
2276 }
2277
2278 /* Pop stack */
2279 stack_pop(notit->stack);
2280
2281 /* If the stack is not empty, increment the base's index */
2282 if (!stack_empty(notit->stack)) {
2283 stack_top(notit->stack)->index++;
2284 }
2285
2286 end:
2287 return BT_BFCR_STATUS_OK;
2288 }
2289
2290 static
2291 int64_t bfcr_get_sequence_length_cb(struct ctf_field_class *fc, void *data)
2292 {
2293 bt_field *seq_field;
2294 struct bt_msg_iter *notit = data;
2295 struct ctf_field_class_sequence *seq_fc = (void *) fc;
2296 int64_t length = -1;
2297 int ret;
2298
2299 length = (uint64_t) g_array_index(notit->stored_values, uint64_t,
2300 seq_fc->stored_length_index);
2301 seq_field = stack_top(notit->stack)->base;
2302 BT_ASSERT(seq_field);
2303
2304 /*
2305 * bfcr_get_sequence_length_cb() also gets called back for a
2306 * text sequence, but the destination field is a string field.
2307 * Only set the field's sequence length if the destination field
2308 * is a sequence field.
2309 */
2310 if (!seq_fc->base.is_text) {
2311 BT_ASSERT(bt_field_get_class_type(seq_field) ==
2312 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY);
2313 ret = bt_field_array_dynamic_set_length(seq_field,
2314 (uint64_t) length);
2315 if (ret) {
2316 BT_COMP_LOGE("Cannot set dynamic array field's length field: "
2317 "notit-addr=%p, field-addr=%p, "
2318 "length=%" PRIu64, notit, seq_field, length);
2319 }
2320 }
2321
2322 return length;
2323 }
2324
2325 static
2326 struct ctf_field_class *bfcr_borrow_variant_selected_field_class_cb(
2327 struct ctf_field_class *fc, void *data)
2328 {
2329 int ret;
2330 uint64_t i;
2331 int64_t option_index = -1;
2332 struct bt_msg_iter *notit = data;
2333 struct ctf_field_class_variant *var_fc = (void *) fc;
2334 struct ctf_named_field_class *selected_option = NULL;
2335 struct ctf_field_class *ret_fc = NULL;
2336 union {
2337 uint64_t u;
2338 int64_t i;
2339 } tag;
2340
2341 /* Get variant's tag */
2342 tag.u = g_array_index(notit->stored_values, uint64_t,
2343 var_fc->stored_tag_index);
2344
2345 /*
2346 * Check each range to find the selected option's index.
2347 */
2348 if (var_fc->tag_fc->base.is_signed) {
2349 for (i = 0; i < var_fc->ranges->len; i++) {
2350 struct ctf_field_class_variant_range *range =
2351 ctf_field_class_variant_borrow_range_by_index(
2352 var_fc, i);
2353
2354 if (tag.i >= range->range.lower.i &&
2355 tag.i <= range->range.upper.i) {
2356 option_index = (int64_t) range->option_index;
2357 break;
2358 }
2359 }
2360 } else {
2361 for (i = 0; i < var_fc->ranges->len; i++) {
2362 struct ctf_field_class_variant_range *range =
2363 ctf_field_class_variant_borrow_range_by_index(
2364 var_fc, i);
2365
2366 if (tag.u >= range->range.lower.u &&
2367 tag.u <= range->range.upper.u) {
2368 option_index = (int64_t) range->option_index;
2369 break;
2370 }
2371 }
2372 }
2373
2374 if (option_index < 0) {
2375 BT_COMP_LOGW("Cannot find variant field class's option: "
2376 "notit-addr=%p, var-fc-addr=%p, u-tag=%" PRIu64 ", "
2377 "i-tag=%" PRId64, notit, var_fc, tag.u, tag.i);
2378 goto end;
2379 }
2380
2381 selected_option = ctf_field_class_variant_borrow_option_by_index(
2382 var_fc, (uint64_t) option_index);
2383
2384 if (selected_option->fc->in_ir) {
2385 bt_field *var_field = stack_top(notit->stack)->base;
2386
2387 ret = bt_field_variant_select_option_field_by_index(
2388 var_field, option_index);
2389 if (ret) {
2390 BT_COMP_LOGW("Cannot select variant field's option field: "
2391 "notit-addr=%p, var-field-addr=%p, "
2392 "opt-index=%" PRId64, notit, var_field,
2393 option_index);
2394 goto end;
2395 }
2396 }
2397
2398 ret_fc = selected_option->fc;
2399
2400 end:
2401 return ret_fc;
2402 }
2403
2404 static
2405 void create_msg_stream_beginning(struct bt_msg_iter *notit,
2406 bt_message **message)
2407 {
2408 bt_message *ret = NULL;
2409
2410 BT_ASSERT(notit->stream);
2411 BT_ASSERT(notit->msg_iter);
2412 ret = bt_message_stream_beginning_create(notit->msg_iter,
2413 notit->stream);
2414 if (!ret) {
2415 BT_COMP_LOGE("Cannot create stream beginning message: "
2416 "notit-addr=%p, stream-addr=%p",
2417 notit, notit->stream);
2418 return;
2419 }
2420
2421 *message = ret;
2422 }
2423
2424 static
2425 void create_msg_stream_end(struct bt_msg_iter *notit, bt_message **message)
2426 {
2427 bt_message *ret;
2428
2429 if (!notit->stream) {
2430 BT_COMP_LOGE("Cannot create stream for stream message: "
2431 "notit-addr=%p", notit);
2432 return;
2433 }
2434
2435 BT_ASSERT(notit->msg_iter);
2436 ret = bt_message_stream_end_create(notit->msg_iter,
2437 notit->stream);
2438 if (!ret) {
2439 BT_COMP_LOGE("Cannot create stream end message: "
2440 "notit-addr=%p, stream-addr=%p",
2441 notit, notit->stream);
2442 return;
2443 }
2444
2445 *message = ret;
2446 }
2447
2448 static
2449 void create_msg_packet_beginning(struct bt_msg_iter *notit,
2450 bt_message **message)
2451 {
2452 int ret;
2453 enum bt_msg_iter_status status;
2454 bt_message *msg = NULL;
2455 const bt_stream_class *sc;
2456
2457 status = set_current_packet(notit);
2458 if (status != BT_MSG_ITER_STATUS_OK) {
2459 goto end;
2460 }
2461
2462 BT_ASSERT(notit->packet);
2463 sc = notit->meta.sc->ir_sc;
2464 BT_ASSERT(sc);
2465
2466 if (notit->packet_context_field) {
2467 ret = bt_packet_move_context_field(
2468 notit->packet, notit->packet_context_field);
2469 if (ret) {
2470 goto end;
2471 }
2472
2473 notit->packet_context_field = NULL;
2474
2475 /*
2476 * At this point notit->dscopes.stream_packet_context
2477 * has the same value as the packet context field within
2478 * notit->packet.
2479 */
2480 BT_ASSERT(bt_packet_borrow_context_field(
2481 notit->packet) ==
2482 notit->dscopes.stream_packet_context);
2483 }
2484
2485 BT_ASSERT(notit->msg_iter);
2486
2487 if (notit->meta.sc->packets_have_ts_begin) {
2488 BT_ASSERT(notit->snapshots.beginning_clock != UINT64_C(-1));
2489 msg = bt_message_packet_beginning_create_with_default_clock_snapshot(
2490 notit->msg_iter, notit->packet,
2491 notit->snapshots.beginning_clock);
2492 } else {
2493 msg = bt_message_packet_beginning_create(notit->msg_iter,
2494 notit->packet);
2495 }
2496
2497 if (!msg) {
2498 BT_COMP_LOGE("Cannot create packet beginning message: "
2499 "notit-addr=%p, packet-addr=%p",
2500 notit, notit->packet);
2501 goto end;
2502 }
2503
2504 *message = msg;
2505
2506 end:
2507 return;
2508 }
2509
2510 static
2511 void create_msg_packet_end(struct bt_msg_iter *notit, bt_message **message)
2512 {
2513 bt_message *msg;
2514
2515 if (!notit->packet) {
2516 return;
2517 }
2518
2519 /* Update default clock from packet's end time */
2520 if (notit->snapshots.end_clock != UINT64_C(-1)) {
2521 notit->default_clock_snapshot = notit->snapshots.end_clock;
2522 }
2523
2524 BT_ASSERT(notit->msg_iter);
2525
2526 if (notit->meta.sc->packets_have_ts_end) {
2527 BT_ASSERT(notit->snapshots.end_clock != UINT64_C(-1));
2528 msg = bt_message_packet_end_create_with_default_clock_snapshot(
2529 notit->msg_iter, notit->packet,
2530 notit->snapshots.end_clock);
2531 } else {
2532 msg = bt_message_packet_end_create(notit->msg_iter,
2533 notit->packet);
2534 }
2535
2536 if (!msg) {
2537 BT_COMP_LOGE("Cannot create packet end message: "
2538 "notit-addr=%p, packet-addr=%p",
2539 notit, notit->packet);
2540 return;
2541
2542 }
2543
2544 BT_PACKET_PUT_REF_AND_RESET(notit->packet);
2545 *message = msg;
2546 }
2547
2548 static
2549 void create_msg_discarded_events(struct bt_msg_iter *notit,
2550 bt_message **message)
2551 {
2552 bt_message *msg;
2553 uint64_t beginning_raw_value = UINT64_C(-1);
2554 uint64_t end_raw_value = UINT64_C(-1);
2555
2556 BT_ASSERT(notit->msg_iter);
2557 BT_ASSERT(notit->stream);
2558 BT_ASSERT(notit->meta.sc->has_discarded_events);
2559
2560 if (notit->meta.sc->discarded_events_have_default_cs) {
2561 if (notit->prev_packet_snapshots.discarded_events == UINT64_C(-1)) {
2562 /*
2563 * We discarded events, but before (and possibly
2564 * including) the current packet: use this packet's time
2565 * range, and do not have a specific count.
2566 */
2567 beginning_raw_value = notit->snapshots.beginning_clock;
2568 end_raw_value = notit->snapshots.end_clock;
2569 } else {
2570 beginning_raw_value = notit->prev_packet_snapshots.end_clock;
2571 end_raw_value = notit->snapshots.end_clock;
2572 }
2573
2574 BT_ASSERT(beginning_raw_value != UINT64_C(-1));
2575 BT_ASSERT(end_raw_value != UINT64_C(-1));
2576 msg = bt_message_discarded_events_create_with_default_clock_snapshots(
2577 notit->msg_iter, notit->stream, beginning_raw_value,
2578 end_raw_value);
2579 } else {
2580 msg = bt_message_discarded_events_create(notit->msg_iter,
2581 notit->stream);
2582 }
2583
2584 if (!msg) {
2585 BT_COMP_LOGE("Cannot create discarded events message: "
2586 "notit-addr=%p, stream-addr=%p",
2587 notit, notit->stream);
2588 return;
2589 }
2590
2591 if (notit->prev_packet_snapshots.discarded_events != UINT64_C(-1)) {
2592 bt_message_discarded_events_set_count(msg,
2593 notit->snapshots.discarded_events -
2594 notit->prev_packet_snapshots.discarded_events);
2595 }
2596
2597 *message = msg;
2598 }
2599
2600 static
2601 void create_msg_discarded_packets(struct bt_msg_iter *notit,
2602 bt_message **message)
2603 {
2604 bt_message *msg;
2605
2606 BT_ASSERT(notit->msg_iter);
2607 BT_ASSERT(notit->stream);
2608 BT_ASSERT(notit->meta.sc->has_discarded_packets);
2609 BT_ASSERT(notit->prev_packet_snapshots.packets !=
2610 UINT64_C(-1));
2611
2612 if (notit->meta.sc->discarded_packets_have_default_cs) {
2613 BT_ASSERT(notit->prev_packet_snapshots.end_clock != UINT64_C(-1));
2614 BT_ASSERT(notit->snapshots.beginning_clock != UINT64_C(-1));
2615 msg = bt_message_discarded_packets_create_with_default_clock_snapshots(
2616 notit->msg_iter, notit->stream,
2617 notit->prev_packet_snapshots.end_clock,
2618 notit->snapshots.beginning_clock);
2619 } else {
2620 msg = bt_message_discarded_packets_create(notit->msg_iter,
2621 notit->stream);
2622 }
2623
2624 if (!msg) {
2625 BT_COMP_LOGE("Cannot create discarded packets message: "
2626 "notit-addr=%p, stream-addr=%p",
2627 notit, notit->stream);
2628 return;
2629 }
2630
2631 bt_message_discarded_packets_set_count(msg,
2632 notit->snapshots.packets -
2633 notit->prev_packet_snapshots.packets - 1);
2634 *message = msg;
2635 }
2636
2637 BT_HIDDEN
2638 struct bt_msg_iter *bt_msg_iter_create(struct ctf_trace_class *tc,
2639 size_t max_request_sz,
2640 struct bt_msg_iter_medium_ops medops, void *data,
2641 bt_logging_level log_level, bt_self_component *self_comp)
2642 {
2643 struct bt_msg_iter *notit = NULL;
2644 struct bt_bfcr_cbs cbs = {
2645 .classes = {
2646 .signed_int = bfcr_signed_int_cb,
2647 .unsigned_int = bfcr_unsigned_int_cb,
2648 .floating_point = bfcr_floating_point_cb,
2649 .string_begin = bfcr_string_begin_cb,
2650 .string = bfcr_string_cb,
2651 .string_end = bfcr_string_end_cb,
2652 .compound_begin = bfcr_compound_begin_cb,
2653 .compound_end = bfcr_compound_end_cb,
2654 },
2655 .query = {
2656 .get_sequence_length = bfcr_get_sequence_length_cb,
2657 .borrow_variant_selected_field_class = bfcr_borrow_variant_selected_field_class_cb,
2658 },
2659 };
2660
2661 BT_ASSERT(tc);
2662 BT_ASSERT(medops.request_bytes);
2663 BT_ASSERT(medops.borrow_stream);
2664 BT_COMP_LOG_CUR_LVL(BT_LOG_DEBUG, log_level, self_comp,
2665 "Creating CTF plugin message iterator: "
2666 "trace-addr=%p, max-request-size=%zu, "
2667 "data=%p, log-level=%s", tc, max_request_sz, data,
2668 bt_common_logging_level_string(log_level));
2669 notit = g_new0(struct bt_msg_iter, 1);
2670 if (!notit) {
2671 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp,
2672 "Failed to allocate one CTF plugin message iterator.");
2673 goto end;
2674 }
2675 notit->self_comp = self_comp;
2676 notit->log_level = log_level;
2677 notit->meta.tc = tc;
2678 notit->medium.medops = medops;
2679 notit->medium.max_request_sz = max_request_sz;
2680 notit->medium.data = data;
2681 notit->stack = stack_new(notit);
2682 notit->stored_values = g_array_new(FALSE, TRUE, sizeof(uint64_t));
2683 g_array_set_size(notit->stored_values, tc->stored_value_count);
2684
2685 if (!notit->stack) {
2686 BT_COMP_LOGE_STR("Failed to create field stack.");
2687 goto error;
2688 }
2689
2690 notit->bfcr = bt_bfcr_create(cbs, notit, log_level, NULL);
2691 if (!notit->bfcr) {
2692 BT_COMP_LOGE_STR("Failed to create binary class reader (BFCR).");
2693 goto error;
2694 }
2695
2696 bt_msg_iter_reset(notit);
2697 BT_COMP_LOGD("Created CTF plugin message iterator: "
2698 "trace-addr=%p, max-request-size=%zu, "
2699 "data=%p, notit-addr=%p, log-level=%s",
2700 tc, max_request_sz, data, notit,
2701 bt_common_logging_level_string(log_level));
2702 notit->cur_packet_offset = 0;
2703
2704 end:
2705 return notit;
2706
2707 error:
2708 bt_msg_iter_destroy(notit);
2709 notit = NULL;
2710 goto end;
2711 }
2712
2713 void bt_msg_iter_destroy(struct bt_msg_iter *notit)
2714 {
2715 BT_PACKET_PUT_REF_AND_RESET(notit->packet);
2716 BT_STREAM_PUT_REF_AND_RESET(notit->stream);
2717 release_all_dscopes(notit);
2718
2719 BT_COMP_LOGD("Destroying CTF plugin message iterator: addr=%p", notit);
2720
2721 if (notit->stack) {
2722 BT_COMP_LOGD_STR("Destroying field stack.");
2723 stack_destroy(notit->stack);
2724 }
2725
2726 if (notit->bfcr) {
2727 BT_COMP_LOGD("Destroying BFCR: bfcr-addr=%p", notit->bfcr);
2728 bt_bfcr_destroy(notit->bfcr);
2729 }
2730
2731 if (notit->stored_values) {
2732 g_array_free(notit->stored_values, TRUE);
2733 }
2734
2735 g_free(notit);
2736 }
2737
2738 enum bt_msg_iter_status bt_msg_iter_get_next_message(
2739 struct bt_msg_iter *notit,
2740 bt_self_message_iterator *msg_iter, bt_message **message)
2741 {
2742 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
2743
2744 BT_ASSERT(notit);
2745 BT_ASSERT(message);
2746 notit->msg_iter = msg_iter;
2747 notit->set_stream = true;
2748 BT_COMP_LOGD("Getting next message: notit-addr=%p", notit);
2749
2750 while (true) {
2751 status = handle_state(notit);
2752 if (G_UNLIKELY(status == BT_MSG_ITER_STATUS_AGAIN)) {
2753 BT_COMP_LOGD_STR("Medium returned BT_MSG_ITER_STATUS_AGAIN.");
2754 goto end;
2755 } else if (G_UNLIKELY(status != BT_MSG_ITER_STATUS_OK)) {
2756 BT_COMP_LOGW("Cannot handle state: notit-addr=%p, state=%s",
2757 notit, state_string(notit->state));
2758 goto end;
2759 }
2760
2761 switch (notit->state) {
2762 case STATE_EMIT_MSG_EVENT:
2763 BT_ASSERT(notit->event_msg);
2764 *message = notit->event_msg;
2765 notit->event_msg = NULL;
2766 goto end;
2767 case STATE_EMIT_MSG_DISCARDED_EVENTS:
2768 /* create_msg_discared_events() logs errors */
2769 create_msg_discarded_events(notit, message);
2770
2771 if (!*message) {
2772 status = BT_MSG_ITER_STATUS_ERROR;
2773 }
2774
2775 goto end;
2776 case STATE_EMIT_MSG_DISCARDED_PACKETS:
2777 /* create_msg_discared_packets() logs errors */
2778 create_msg_discarded_packets(notit, message);
2779
2780 if (!*message) {
2781 status = BT_MSG_ITER_STATUS_ERROR;
2782 }
2783
2784 goto end;
2785 case STATE_EMIT_MSG_PACKET_BEGINNING:
2786 /* create_msg_packet_beginning() logs errors */
2787 create_msg_packet_beginning(notit, message);
2788
2789 if (!*message) {
2790 status = BT_MSG_ITER_STATUS_ERROR;
2791 }
2792
2793 goto end;
2794 case STATE_EMIT_MSG_PACKET_END_SINGLE:
2795 case STATE_EMIT_MSG_PACKET_END_MULTI:
2796 /* create_msg_packet_end() logs errors */
2797 create_msg_packet_end(notit, message);
2798
2799 if (!*message) {
2800 status = BT_MSG_ITER_STATUS_ERROR;
2801 }
2802
2803 goto end;
2804 case STATE_EMIT_MSG_STREAM_BEGINNING:
2805 /* create_msg_stream_beginning() logs errors */
2806 create_msg_stream_beginning(notit, message);
2807
2808 if (!*message) {
2809 status = BT_MSG_ITER_STATUS_ERROR;
2810 }
2811
2812 goto end;
2813 case STATE_EMIT_MSG_STREAM_END:
2814 /* create_msg_stream_end() logs errors */
2815 create_msg_stream_end(notit, message);
2816
2817 if (!*message) {
2818 status = BT_MSG_ITER_STATUS_ERROR;
2819 }
2820
2821 goto end;
2822 case STATE_DONE:
2823 status = BT_MSG_ITER_STATUS_EOF;
2824 goto end;
2825 default:
2826 /* Non-emitting state: continue */
2827 break;
2828 }
2829 }
2830
2831 end:
2832 return status;
2833 }
2834
2835 static
2836 enum bt_msg_iter_status read_packet_header_context_fields(
2837 struct bt_msg_iter *notit)
2838 {
2839 int ret;
2840 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
2841
2842 BT_ASSERT(notit);
2843 notit->set_stream = false;
2844
2845 if (notit->state == STATE_EMIT_MSG_PACKET_BEGINNING) {
2846 /* We're already there */
2847 goto end;
2848 }
2849
2850 while (true) {
2851 status = handle_state(notit);
2852 if (G_UNLIKELY(status == BT_MSG_ITER_STATUS_AGAIN)) {
2853 BT_COMP_LOGD_STR("Medium returned BT_MSG_ITER_STATUS_AGAIN.");
2854 goto end;
2855 } else if (G_UNLIKELY(status != BT_MSG_ITER_STATUS_OK)) {
2856 BT_COMP_LOGW("Cannot handle state: notit-addr=%p, state=%s",
2857 notit, state_string(notit->state));
2858 goto end;
2859 }
2860
2861 switch (notit->state) {
2862 case STATE_EMIT_MSG_PACKET_BEGINNING:
2863 /*
2864 * Packet header and context fields are
2865 * potentially decoded (or they don't exist).
2866 */
2867 goto end;
2868 case STATE_INIT:
2869 case STATE_SWITCH_PACKET:
2870 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
2871 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
2872 case STATE_AFTER_TRACE_PACKET_HEADER:
2873 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN:
2874 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE:
2875 case STATE_AFTER_STREAM_PACKET_CONTEXT:
2876 case STATE_CHECK_EMIT_MSG_STREAM_BEGINNING:
2877 case STATE_EMIT_MSG_STREAM_BEGINNING:
2878 case STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS:
2879 case STATE_EMIT_MSG_DISCARDED_EVENTS:
2880 case STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS:
2881 case STATE_EMIT_MSG_DISCARDED_PACKETS:
2882 /* Non-emitting state: continue */
2883 break;
2884 default:
2885 /*
2886 * We should never get past the
2887 * STATE_EMIT_MSG_PACKET_BEGINNING state.
2888 */
2889 BT_COMP_LOGF("Unexpected state: notit-addr=%p, state=%s",
2890 notit, state_string(notit->state));
2891 abort();
2892 }
2893 }
2894
2895 end:
2896 ret = set_current_packet_content_sizes(notit);
2897 if (ret) {
2898 status = BT_MSG_ITER_STATUS_ERROR;
2899 }
2900
2901 return status;
2902 }
2903
2904 BT_HIDDEN
2905 void bt_msg_iter_set_medops_data(struct bt_msg_iter *notit,
2906 void *medops_data)
2907 {
2908 BT_ASSERT(notit);
2909 notit->medium.data = medops_data;
2910 }
2911
2912 BT_HIDDEN
2913 enum bt_msg_iter_status bt_msg_iter_seek(struct bt_msg_iter *notit,
2914 off_t offset)
2915 {
2916 enum bt_msg_iter_status ret = BT_MSG_ITER_STATUS_OK;
2917 enum bt_msg_iter_medium_status medium_status;
2918
2919 BT_ASSERT(notit);
2920 if (offset < 0) {
2921 BT_COMP_LOGE("Cannot seek to negative offset: offset=%jd", (intmax_t) offset);
2922 ret = BT_MSG_ITER_STATUS_INVAL;
2923 goto end;
2924 }
2925
2926 if (!notit->medium.medops.seek) {
2927 ret = BT_MSG_ITER_STATUS_UNSUPPORTED;
2928 BT_COMP_LOGD("Aborting seek as the iterator's underlying media does not implement seek support.");
2929 goto end;
2930 }
2931
2932 medium_status = notit->medium.medops.seek(
2933 BT_MSG_ITER_SEEK_WHENCE_SET, offset, notit->medium.data);
2934 if (medium_status != BT_MSG_ITER_MEDIUM_STATUS_OK) {
2935 if (medium_status == BT_MSG_ITER_MEDIUM_STATUS_EOF) {
2936 ret = BT_MSG_ITER_STATUS_EOF;
2937 } else {
2938 ret = BT_MSG_ITER_STATUS_ERROR;
2939 goto end;
2940 }
2941 }
2942
2943 bt_msg_iter_reset(notit);
2944 notit->cur_packet_offset = offset;
2945
2946 end:
2947 return ret;
2948 }
2949
2950 BT_HIDDEN
2951 enum bt_msg_iter_status bt_msg_iter_get_packet_properties(
2952 struct bt_msg_iter *notit,
2953 struct bt_msg_iter_packet_properties *props)
2954 {
2955 enum bt_msg_iter_status status;
2956
2957 BT_ASSERT(notit);
2958 BT_ASSERT(props);
2959 status = read_packet_header_context_fields(notit);
2960 if (status != BT_MSG_ITER_STATUS_OK) {
2961 goto end;
2962 }
2963
2964 props->exp_packet_total_size = notit->cur_exp_packet_total_size;
2965 props->exp_packet_content_size = notit->cur_exp_packet_content_size;
2966 props->stream_class_id = (uint64_t) notit->cur_stream_class_id;
2967 props->data_stream_id = notit->cur_data_stream_id;
2968 props->snapshots.discarded_events = notit->snapshots.discarded_events;
2969 props->snapshots.packets = notit->snapshots.packets;
2970 props->snapshots.beginning_clock = notit->snapshots.beginning_clock;
2971 props->snapshots.end_clock = notit->snapshots.end_clock;
2972
2973 end:
2974 return status;
2975 }
2976
2977 BT_HIDDEN
2978 void bt_msg_iter_set_emit_stream_beginning_message(struct bt_msg_iter *notit,
2979 bool val)
2980 {
2981 notit->emit_stream_begin_msg = val;
2982 }
2983
2984 BT_HIDDEN
2985 void bt_msg_iter_set_emit_stream_end_message(struct bt_msg_iter *notit,
2986 bool val)
2987 {
2988 notit->emit_stream_end_msg = val;
2989 }
This page took 0.122449 seconds and 5 git commands to generate.