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