Fix type-limits warnings, uint can't be < 0
[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 notit->cur_exp_packet_total_size =
973 notit->cur_exp_packet_content_size;
974 }
975 } else {
976 if (notit->cur_exp_packet_content_size == -1) {
977 notit->cur_exp_packet_content_size =
978 notit->cur_exp_packet_total_size;
979 }
980 }
981
982 BT_ASSERT((notit->cur_exp_packet_total_size >= 0 &&
983 notit->cur_exp_packet_content_size >= 0) ||
984 (notit->cur_exp_packet_total_size < 0 &&
985 notit->cur_exp_packet_content_size < 0));
986
987 if (notit->cur_exp_packet_content_size >
988 notit->cur_exp_packet_total_size) {
989 BT_LOGW("Invalid packet or content size: "
990 "content size is greater than packet size: "
991 "notit-addr=%p, packet-context-field-addr=%p, "
992 "packet-size=%" PRId64 ", content-size=%" PRId64,
993 notit, notit->dscopes.stream_packet_context,
994 notit->cur_exp_packet_total_size,
995 notit->cur_exp_packet_content_size);
996 status = BT_MSG_ITER_STATUS_ERROR;
997 goto end;
998 }
999
1000 BT_LOGV("Set current packet and content sizes: "
1001 "notit-addr=%p, packet-size=%" PRIu64 ", content-size=%" PRIu64,
1002 notit, notit->cur_exp_packet_total_size,
1003 notit->cur_exp_packet_content_size);
1004
1005 end:
1006 return status;
1007 }
1008
1009 static
1010 enum bt_msg_iter_status after_packet_context_state(struct bt_msg_iter *notit)
1011 {
1012 enum bt_msg_iter_status status;
1013
1014 status = set_current_packet_content_sizes(notit);
1015 if (status != BT_MSG_ITER_STATUS_OK) {
1016 goto end;
1017 }
1018
1019 if (notit->stream) {
1020 /*
1021 * Stream exists, which means we already emitted at
1022 * least one packet beginning message, so the initial
1023 * stream beginning message was also emitted.
1024 */
1025 notit->state = STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS;
1026 } else {
1027 notit->state = STATE_CHECK_EMIT_MSG_STREAM_BEGINNING;
1028 }
1029
1030 end:
1031 return status;
1032 }
1033
1034 static
1035 enum bt_msg_iter_status read_event_header_begin_state(struct bt_msg_iter *notit)
1036 {
1037 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
1038 struct ctf_field_class *event_header_fc = NULL;
1039
1040 /* Reset the position of the last event header */
1041 notit->buf.last_eh_at = notit->buf.at;
1042 notit->cur_event_class_id = -1;
1043
1044 /* Check if we have some content left */
1045 if (notit->cur_exp_packet_content_size >= 0) {
1046 if (unlikely(packet_at(notit) ==
1047 notit->cur_exp_packet_content_size)) {
1048 /* No more events! */
1049 BT_LOGV("Reached end of packet: notit-addr=%p, "
1050 "cur=%zu", notit, packet_at(notit));
1051 notit->state = STATE_EMIT_MSG_PACKET_END_MULTI;
1052 goto end;
1053 } else if (unlikely(packet_at(notit) >
1054 notit->cur_exp_packet_content_size)) {
1055 /* That's not supposed to happen */
1056 BT_LOGV("Before decoding event header field: cursor is passed the packet's content: "
1057 "notit-addr=%p, content-size=%" PRId64 ", "
1058 "cur=%zu", notit,
1059 notit->cur_exp_packet_content_size,
1060 packet_at(notit));
1061 status = BT_MSG_ITER_STATUS_ERROR;
1062 goto end;
1063 }
1064 } else {
1065 /*
1066 * "Infinite" content: we're done when the medium has
1067 * nothing else for us.
1068 */
1069 status = buf_ensure_available_bits(notit);
1070 switch (status) {
1071 case BT_MSG_ITER_STATUS_OK:
1072 break;
1073 case BT_MSG_ITER_STATUS_EOF:
1074 status = BT_MSG_ITER_STATUS_OK;
1075 notit->state = STATE_EMIT_MSG_PACKET_END_SINGLE;
1076 goto end;
1077 default:
1078 goto end;
1079 }
1080 }
1081
1082 release_event_dscopes(notit);
1083 BT_ASSERT(notit->meta.sc);
1084 event_header_fc = notit->meta.sc->event_header_fc;
1085 if (!event_header_fc) {
1086 notit->state = STATE_AFTER_EVENT_HEADER;
1087 goto end;
1088 }
1089
1090 BT_LOGV("Decoding event header field: "
1091 "notit-addr=%p, stream-class-addr=%p, "
1092 "stream-class-id=%" PRId64 ", "
1093 "fc-addr=%p",
1094 notit, notit->meta.sc,
1095 notit->meta.sc->id,
1096 event_header_fc);
1097 status = read_dscope_begin_state(notit, event_header_fc,
1098 STATE_AFTER_EVENT_HEADER,
1099 STATE_DSCOPE_EVENT_HEADER_CONTINUE, NULL);
1100 if (status < 0) {
1101 BT_LOGW("Cannot decode event header field: "
1102 "notit-addr=%p, stream-class-addr=%p, "
1103 "stream-class-id=%" PRId64 ", fc-addr=%p",
1104 notit, notit->meta.sc,
1105 notit->meta.sc->id,
1106 event_header_fc);
1107 }
1108
1109 end:
1110 return status;
1111 }
1112
1113 static
1114 enum bt_msg_iter_status read_event_header_continue_state(
1115 struct bt_msg_iter *notit)
1116 {
1117 return read_dscope_continue_state(notit,
1118 STATE_AFTER_EVENT_HEADER);
1119 }
1120
1121 static inline
1122 enum bt_msg_iter_status set_current_event_class(struct bt_msg_iter *notit)
1123 {
1124 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
1125
1126 struct ctf_event_class *new_event_class = NULL;
1127
1128 if (notit->cur_event_class_id == -1) {
1129 /*
1130 * No current event class ID field, therefore only one
1131 * event class.
1132 */
1133 if (notit->meta.sc->event_classes->len != 1) {
1134 BT_LOGW("Need exactly one event class since there's "
1135 "no event class ID field: "
1136 "notit-addr=%p", notit);
1137 status = BT_MSG_ITER_STATUS_ERROR;
1138 goto end;
1139 }
1140
1141 new_event_class = notit->meta.sc->event_classes->pdata[0];
1142 notit->cur_event_class_id = new_event_class->id;
1143 }
1144
1145 new_event_class = ctf_stream_class_borrow_event_class_by_id(
1146 notit->meta.sc, notit->cur_event_class_id);
1147 if (!new_event_class) {
1148 BT_LOGW("No event class with ID of event class ID to use in stream class: "
1149 "notit-addr=%p, stream-class-id=%" PRIu64 ", "
1150 "event-class-id=%" PRIu64 ", "
1151 "trace-class-addr=%p",
1152 notit, notit->meta.sc->id, notit->cur_event_class_id,
1153 notit->meta.tc);
1154 status = BT_MSG_ITER_STATUS_ERROR;
1155 goto end;
1156 }
1157
1158 notit->meta.ec = new_event_class;
1159 BT_LOGV("Set current event class: "
1160 "notit-addr=%p, event-class-addr=%p, "
1161 "event-class-id=%" PRId64 ", "
1162 "event-class-name=\"%s\"",
1163 notit, notit->meta.ec, notit->meta.ec->id,
1164 notit->meta.ec->name->str);
1165
1166 end:
1167 return status;
1168 }
1169
1170 static inline
1171 enum bt_msg_iter_status set_current_event_message(
1172 struct bt_msg_iter *notit)
1173 {
1174 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
1175 bt_message *msg = NULL;
1176
1177 BT_ASSERT(notit->meta.ec);
1178 BT_ASSERT(notit->packet);
1179 BT_LOGV("Creating event message from event class and packet: "
1180 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", packet-addr=%p",
1181 notit, notit->meta.ec,
1182 notit->meta.ec->name->str,
1183 notit->packet);
1184 BT_ASSERT(notit->msg_iter);
1185 BT_ASSERT(notit->meta.sc);
1186
1187 if (bt_stream_class_borrow_default_clock_class(notit->meta.sc->ir_sc)) {
1188 msg = bt_message_event_create_with_default_clock_snapshot(
1189 notit->msg_iter, notit->meta.ec->ir_ec,
1190 notit->packet, notit->default_clock_snapshot);
1191 } else {
1192 msg = bt_message_event_create(notit->msg_iter,
1193 notit->meta.ec->ir_ec, notit->packet);
1194 }
1195
1196 if (!msg) {
1197 BT_LOGE("Cannot create event message: "
1198 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", "
1199 "packet-addr=%p",
1200 notit, notit->meta.ec,
1201 notit->meta.ec->name->str,
1202 notit->packet);
1203 goto error;
1204 }
1205
1206 goto end;
1207
1208 error:
1209 BT_MESSAGE_PUT_REF_AND_RESET(msg);
1210 status = BT_MSG_ITER_STATUS_ERROR;
1211
1212 end:
1213 BT_MESSAGE_MOVE_REF(notit->event_msg, msg);
1214 return status;
1215 }
1216
1217 static
1218 enum bt_msg_iter_status after_event_header_state(
1219 struct bt_msg_iter *notit)
1220 {
1221 enum bt_msg_iter_status status;
1222
1223 status = set_current_event_class(notit);
1224 if (status != BT_MSG_ITER_STATUS_OK) {
1225 goto end;
1226 }
1227
1228 status = set_current_event_message(notit);
1229 if (status != BT_MSG_ITER_STATUS_OK) {
1230 goto end;
1231 }
1232
1233 notit->event = bt_message_event_borrow_event(
1234 notit->event_msg);
1235 BT_ASSERT(notit->event);
1236 notit->state = STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN;
1237
1238 end:
1239 return status;
1240 }
1241
1242 static
1243 enum bt_msg_iter_status read_event_common_context_begin_state(
1244 struct bt_msg_iter *notit)
1245 {
1246 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
1247 struct ctf_field_class *event_common_context_fc;
1248
1249 event_common_context_fc = notit->meta.sc->event_common_context_fc;
1250 if (!event_common_context_fc) {
1251 notit->state = STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN;
1252 goto end;
1253 }
1254
1255 if (event_common_context_fc->in_ir) {
1256 BT_ASSERT(!notit->dscopes.event_common_context);
1257 notit->dscopes.event_common_context =
1258 bt_event_borrow_common_context_field(
1259 notit->event);
1260 BT_ASSERT(notit->dscopes.event_common_context);
1261 }
1262
1263 BT_LOGV("Decoding event common context field: "
1264 "notit-addr=%p, stream-class-addr=%p, "
1265 "stream-class-id=%" PRId64 ", "
1266 "fc-addr=%p",
1267 notit, notit->meta.sc,
1268 notit->meta.sc->id,
1269 event_common_context_fc);
1270 status = read_dscope_begin_state(notit, event_common_context_fc,
1271 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN,
1272 STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE,
1273 notit->dscopes.event_common_context);
1274 if (status < 0) {
1275 BT_LOGW("Cannot decode event common context field: "
1276 "notit-addr=%p, stream-class-addr=%p, "
1277 "stream-class-id=%" PRId64 ", fc-addr=%p",
1278 notit, notit->meta.sc,
1279 notit->meta.sc->id,
1280 event_common_context_fc);
1281 }
1282
1283 end:
1284 return status;
1285 }
1286
1287 static
1288 enum bt_msg_iter_status read_event_common_context_continue_state(
1289 struct bt_msg_iter *notit)
1290 {
1291 return read_dscope_continue_state(notit,
1292 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN);
1293 }
1294
1295 static
1296 enum bt_msg_iter_status read_event_spec_context_begin_state(
1297 struct bt_msg_iter *notit)
1298 {
1299 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
1300 struct ctf_field_class *event_spec_context_fc;
1301
1302 event_spec_context_fc = notit->meta.ec->spec_context_fc;
1303 if (!event_spec_context_fc) {
1304 notit->state = STATE_DSCOPE_EVENT_PAYLOAD_BEGIN;
1305 goto end;
1306 }
1307
1308 if (event_spec_context_fc->in_ir) {
1309 BT_ASSERT(!notit->dscopes.event_spec_context);
1310 notit->dscopes.event_spec_context =
1311 bt_event_borrow_specific_context_field(
1312 notit->event);
1313 BT_ASSERT(notit->dscopes.event_spec_context);
1314 }
1315
1316 BT_LOGV("Decoding event specific context field: "
1317 "notit-addr=%p, event-class-addr=%p, "
1318 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
1319 "fc-addr=%p",
1320 notit, notit->meta.ec,
1321 notit->meta.ec->name->str,
1322 notit->meta.ec->id,
1323 event_spec_context_fc);
1324 status = read_dscope_begin_state(notit, event_spec_context_fc,
1325 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN,
1326 STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE,
1327 notit->dscopes.event_spec_context);
1328 if (status < 0) {
1329 BT_LOGW("Cannot decode event specific context field: "
1330 "notit-addr=%p, event-class-addr=%p, "
1331 "event-class-name=\"%s\", "
1332 "event-class-id=%" PRId64 ", fc-addr=%p",
1333 notit, notit->meta.ec,
1334 notit->meta.ec->name->str,
1335 notit->meta.ec->id,
1336 event_spec_context_fc);
1337 }
1338
1339 end:
1340 return status;
1341 }
1342
1343 static
1344 enum bt_msg_iter_status read_event_spec_context_continue_state(
1345 struct bt_msg_iter *notit)
1346 {
1347 return read_dscope_continue_state(notit,
1348 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN);
1349 }
1350
1351 static
1352 enum bt_msg_iter_status read_event_payload_begin_state(
1353 struct bt_msg_iter *notit)
1354 {
1355 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
1356 struct ctf_field_class *event_payload_fc;
1357
1358 event_payload_fc = notit->meta.ec->payload_fc;
1359 if (!event_payload_fc) {
1360 notit->state = STATE_EMIT_MSG_EVENT;
1361 goto end;
1362 }
1363
1364 if (event_payload_fc->in_ir) {
1365 BT_ASSERT(!notit->dscopes.event_payload);
1366 notit->dscopes.event_payload =
1367 bt_event_borrow_payload_field(
1368 notit->event);
1369 BT_ASSERT(notit->dscopes.event_payload);
1370 }
1371
1372 BT_LOGV("Decoding event payload field: "
1373 "notit-addr=%p, event-class-addr=%p, "
1374 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
1375 "fc-addr=%p",
1376 notit, notit->meta.ec,
1377 notit->meta.ec->name->str,
1378 notit->meta.ec->id,
1379 event_payload_fc);
1380 status = read_dscope_begin_state(notit, event_payload_fc,
1381 STATE_EMIT_MSG_EVENT,
1382 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE,
1383 notit->dscopes.event_payload);
1384 if (status < 0) {
1385 BT_LOGW("Cannot decode event payload field: "
1386 "notit-addr=%p, event-class-addr=%p, "
1387 "event-class-name=\"%s\", "
1388 "event-class-id=%" PRId64 ", fc-addr=%p",
1389 notit, notit->meta.ec,
1390 notit->meta.ec->name->str,
1391 notit->meta.ec->id,
1392 event_payload_fc);
1393 }
1394
1395 end:
1396 return status;
1397 }
1398
1399 static
1400 enum bt_msg_iter_status read_event_payload_continue_state(
1401 struct bt_msg_iter *notit)
1402 {
1403 return read_dscope_continue_state(notit, STATE_EMIT_MSG_EVENT);
1404 }
1405
1406 static
1407 enum bt_msg_iter_status skip_packet_padding_state(struct bt_msg_iter *notit)
1408 {
1409 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
1410 size_t bits_to_skip;
1411
1412 BT_ASSERT(notit->cur_exp_packet_total_size > 0);
1413 bits_to_skip = notit->cur_exp_packet_total_size - packet_at(notit);
1414 if (bits_to_skip == 0) {
1415 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1416 goto end;
1417 } else {
1418 size_t bits_to_consume;
1419
1420 BT_LOGV("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu",
1421 bits_to_skip, notit, bits_to_skip);
1422 status = buf_ensure_available_bits(notit);
1423 if (status != BT_MSG_ITER_STATUS_OK) {
1424 goto end;
1425 }
1426
1427 bits_to_consume = MIN(buf_available_bits(notit), bits_to_skip);
1428 BT_LOGV("Skipping %zu bits of padding: notit-addr=%p, size=%zu",
1429 bits_to_consume, notit, bits_to_consume);
1430 buf_consume_bits(notit, bits_to_consume);
1431 bits_to_skip = notit->cur_exp_packet_total_size -
1432 packet_at(notit);
1433 if (bits_to_skip == 0) {
1434 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1435 goto end;
1436 }
1437 }
1438
1439 end:
1440 return status;
1441 }
1442
1443 static
1444 enum bt_msg_iter_status check_emit_msg_stream_beginning_state(
1445 struct bt_msg_iter *notit)
1446 {
1447 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
1448
1449 if (notit->set_stream) {
1450 status = set_current_stream(notit);
1451 if (status != BT_MSG_ITER_STATUS_OK) {
1452 goto end;
1453 }
1454 }
1455
1456 if (notit->emit_stream_begin_msg) {
1457 notit->state = STATE_EMIT_MSG_STREAM_BEGINNING;
1458 } else {
1459 /* Stream's first packet */
1460 notit->state = STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS;
1461 }
1462
1463 end:
1464 return status;
1465 }
1466
1467 static
1468 enum bt_msg_iter_status check_emit_msg_discarded_events(
1469 struct bt_msg_iter *notit)
1470 {
1471 notit->state = STATE_EMIT_MSG_DISCARDED_EVENTS;
1472
1473 if (notit->prev_packet_snapshots.discarded_events == UINT64_C(-1)) {
1474 if (notit->snapshots.discarded_events == 0 ||
1475 notit->snapshots.discarded_events == UINT64_C(-1)) {
1476 /*
1477 * Stream's first packet with no discarded
1478 * events or no information about discarded
1479 * events: do not emit.
1480 */
1481 notit->state = STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS;
1482 }
1483 } else {
1484 /*
1485 * If the previous packet has a value for this counter,
1486 * then this counter is defined for the whole stream.
1487 */
1488 BT_ASSERT(notit->snapshots.discarded_events != UINT64_C(-1));
1489
1490 if (notit->snapshots.discarded_events -
1491 notit->prev_packet_snapshots.discarded_events == 0) {
1492 /*
1493 * No discarded events since previous packet: do
1494 * not emit.
1495 */
1496 notit->state = STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS;
1497 }
1498 }
1499
1500 return BT_MSG_ITER_STATUS_OK;
1501 }
1502
1503 static
1504 enum bt_msg_iter_status check_emit_msg_discarded_packets(
1505 struct bt_msg_iter *notit)
1506 {
1507 notit->state = STATE_EMIT_MSG_DISCARDED_PACKETS;
1508
1509 if (notit->prev_packet_snapshots.packets == UINT64_C(-1)) {
1510 /*
1511 * Stream's first packet or no information about
1512 * discarded packets: do not emit. In other words, if
1513 * this is the first packet and its sequence number is
1514 * not 0, do not consider that packets were previously
1515 * lost: we might be reading a partial stream (LTTng
1516 * snapshot for example).
1517 */
1518 notit->state = STATE_EMIT_MSG_PACKET_BEGINNING;
1519 } else {
1520 /*
1521 * If the previous packet has a value for this counter,
1522 * then this counter is defined for the whole stream.
1523 */
1524 BT_ASSERT(notit->snapshots.packets != UINT64_C(-1));
1525
1526 if (notit->snapshots.packets -
1527 notit->prev_packet_snapshots.packets <= 1) {
1528 /*
1529 * No discarded packets since previous packet:
1530 * do not emit.
1531 */
1532 notit->state = STATE_EMIT_MSG_PACKET_BEGINNING;
1533 }
1534 }
1535
1536 return BT_MSG_ITER_STATUS_OK;
1537 }
1538
1539 static
1540 enum bt_msg_iter_status check_emit_msg_stream_activity_end(
1541 struct bt_msg_iter *notit)
1542 {
1543 if (notit->emit_stream_end_msg) {
1544 notit->state = STATE_EMIT_MSG_STREAM_ACTIVITY_END;
1545 } else {
1546 notit->state = STATE_DONE;
1547 }
1548
1549 return BT_MSG_ITER_STATUS_OK;
1550 }
1551
1552 static inline
1553 enum bt_msg_iter_status handle_state(struct bt_msg_iter *notit)
1554 {
1555 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
1556 const enum state state = notit->state;
1557
1558 BT_LOGV("Handling state: notit-addr=%p, state=%s",
1559 notit, state_string(state));
1560
1561 // TODO: optimalize!
1562 switch (state) {
1563 case STATE_INIT:
1564 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1565 break;
1566 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
1567 status = read_packet_header_begin_state(notit);
1568 break;
1569 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
1570 status = read_packet_header_continue_state(notit);
1571 break;
1572 case STATE_AFTER_TRACE_PACKET_HEADER:
1573 status = after_packet_header_state(notit);
1574 break;
1575 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN:
1576 status = read_packet_context_begin_state(notit);
1577 break;
1578 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE:
1579 status = read_packet_context_continue_state(notit);
1580 break;
1581 case STATE_AFTER_STREAM_PACKET_CONTEXT:
1582 status = after_packet_context_state(notit);
1583 break;
1584 case STATE_CHECK_EMIT_MSG_STREAM_BEGINNING:
1585 status = check_emit_msg_stream_beginning_state(notit);
1586 break;
1587 case STATE_EMIT_MSG_STREAM_BEGINNING:
1588 notit->state = STATE_EMIT_MSG_STREAM_ACTIVITY_BEGINNING;
1589 break;
1590 case STATE_EMIT_MSG_STREAM_ACTIVITY_BEGINNING:
1591 notit->state = STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS;
1592 break;
1593 case STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS:
1594 status = check_emit_msg_discarded_events(notit);
1595 break;
1596 case STATE_EMIT_MSG_DISCARDED_EVENTS:
1597 notit->state = STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS;
1598 break;
1599 case STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS:
1600 status = check_emit_msg_discarded_packets(notit);
1601 break;
1602 case STATE_EMIT_MSG_DISCARDED_PACKETS:
1603 notit->state = STATE_EMIT_MSG_PACKET_BEGINNING;
1604 break;
1605 case STATE_EMIT_MSG_PACKET_BEGINNING:
1606 notit->state = STATE_DSCOPE_EVENT_HEADER_BEGIN;
1607 break;
1608 case STATE_DSCOPE_EVENT_HEADER_BEGIN:
1609 status = read_event_header_begin_state(notit);
1610 break;
1611 case STATE_DSCOPE_EVENT_HEADER_CONTINUE:
1612 status = read_event_header_continue_state(notit);
1613 break;
1614 case STATE_AFTER_EVENT_HEADER:
1615 status = after_event_header_state(notit);
1616 break;
1617 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN:
1618 status = read_event_common_context_begin_state(notit);
1619 break;
1620 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE:
1621 status = read_event_common_context_continue_state(notit);
1622 break;
1623 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN:
1624 status = read_event_spec_context_begin_state(notit);
1625 break;
1626 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE:
1627 status = read_event_spec_context_continue_state(notit);
1628 break;
1629 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN:
1630 status = read_event_payload_begin_state(notit);
1631 break;
1632 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE:
1633 status = read_event_payload_continue_state(notit);
1634 break;
1635 case STATE_EMIT_MSG_EVENT:
1636 notit->state = STATE_DSCOPE_EVENT_HEADER_BEGIN;
1637 break;
1638 case STATE_SKIP_PACKET_PADDING:
1639 status = skip_packet_padding_state(notit);
1640 break;
1641 case STATE_EMIT_MSG_PACKET_END_MULTI:
1642 notit->state = STATE_SKIP_PACKET_PADDING;
1643 break;
1644 case STATE_EMIT_MSG_PACKET_END_SINGLE:
1645 notit->state = STATE_CHECK_EMIT_MSG_STREAM_ACTIVITY_END;
1646 break;
1647 case STATE_CHECK_EMIT_MSG_STREAM_ACTIVITY_END:
1648 status = check_emit_msg_stream_activity_end(notit);
1649 break;
1650 case STATE_EMIT_MSG_STREAM_ACTIVITY_END:
1651 notit->state = STATE_EMIT_MSG_STREAM_END;
1652 break;
1653 case STATE_EMIT_MSG_STREAM_END:
1654 notit->state = STATE_DONE;
1655 break;
1656 case STATE_DONE:
1657 break;
1658 default:
1659 BT_LOGD("Unknown CTF plugin message iterator state: "
1660 "notit-addr=%p, state=%d", notit, notit->state);
1661 abort();
1662 }
1663
1664 BT_LOGV("Handled state: notit-addr=%p, status=%s, "
1665 "prev-state=%s, cur-state=%s",
1666 notit, bt_msg_iter_status_string(status),
1667 state_string(state), state_string(notit->state));
1668 return status;
1669 }
1670
1671 BT_HIDDEN
1672 void bt_msg_iter_reset_for_next_stream_file(struct bt_msg_iter *notit)
1673 {
1674 BT_ASSERT(notit);
1675 BT_LOGD("Resetting message iterator: addr=%p", notit);
1676 stack_clear(notit->stack);
1677 notit->meta.sc = NULL;
1678 notit->meta.ec = NULL;
1679 BT_PACKET_PUT_REF_AND_RESET(notit->packet);
1680 BT_STREAM_PUT_REF_AND_RESET(notit->stream);
1681 BT_MESSAGE_PUT_REF_AND_RESET(notit->event_msg);
1682 release_all_dscopes(notit);
1683 notit->cur_dscope_field = NULL;
1684
1685 if (notit->packet_context_field) {
1686 bt_packet_context_field_release(notit->packet_context_field);
1687 notit->packet_context_field = NULL;
1688 }
1689
1690 notit->buf.addr = NULL;
1691 notit->buf.sz = 0;
1692 notit->buf.at = 0;
1693 notit->buf.last_eh_at = SIZE_MAX;
1694 notit->buf.packet_offset = 0;
1695 notit->state = STATE_INIT;
1696 notit->cur_exp_packet_content_size = -1;
1697 notit->cur_exp_packet_total_size = -1;
1698 notit->cur_packet_offset = -1;
1699 notit->cur_event_class_id = -1;
1700 notit->snapshots.beginning_clock = UINT64_C(-1);
1701 notit->snapshots.end_clock = UINT64_C(-1);
1702 }
1703
1704 /**
1705 * Resets the internal state of a CTF message iterator.
1706 */
1707 BT_HIDDEN
1708 void bt_msg_iter_reset(struct bt_msg_iter *notit)
1709 {
1710 bt_msg_iter_reset_for_next_stream_file(notit);
1711 notit->cur_stream_class_id = -1;
1712 notit->cur_data_stream_id = -1;
1713 notit->emit_stream_begin_msg = true;
1714 notit->emit_stream_end_msg = true;
1715 notit->snapshots.discarded_events = UINT64_C(-1);
1716 notit->snapshots.packets = UINT64_C(-1);
1717 notit->prev_packet_snapshots.discarded_events = UINT64_C(-1);
1718 notit->prev_packet_snapshots.packets = UINT64_C(-1);
1719 notit->prev_packet_snapshots.beginning_clock = UINT64_C(-1);
1720 notit->prev_packet_snapshots.end_clock = UINT64_C(-1);
1721 }
1722
1723 static
1724 int bt_msg_iter_switch_packet(struct bt_msg_iter *notit)
1725 {
1726 int ret = 0;
1727
1728 /*
1729 * We don't put the stream class here because we need to make
1730 * sure that all the packets processed by the same message
1731 * iterator refer to the same stream class (the first one).
1732 */
1733 BT_ASSERT(notit);
1734
1735 if (notit->cur_exp_packet_total_size != -1) {
1736 notit->cur_packet_offset += notit->cur_exp_packet_total_size;
1737 }
1738
1739 BT_LOGV("Switching packet: notit-addr=%p, cur=%zu, "
1740 "packet-offset=%" PRId64, notit, notit->buf.at,
1741 notit->cur_packet_offset);
1742 stack_clear(notit->stack);
1743 notit->meta.ec = NULL;
1744 BT_PACKET_PUT_REF_AND_RESET(notit->packet);
1745 BT_MESSAGE_PUT_REF_AND_RESET(notit->event_msg);
1746 release_all_dscopes(notit);
1747 notit->cur_dscope_field = NULL;
1748
1749 /*
1750 * Adjust current buffer so that addr points to the beginning of the new
1751 * packet.
1752 */
1753 if (notit->buf.addr) {
1754 size_t consumed_bytes = (size_t) (notit->buf.at / CHAR_BIT);
1755
1756 /* Packets are assumed to start on a byte frontier. */
1757 if (notit->buf.at % CHAR_BIT) {
1758 BT_LOGW("Cannot switch packet: current position is not a multiple of 8: "
1759 "notit-addr=%p, cur=%zu", notit, notit->buf.at);
1760 ret = -1;
1761 goto end;
1762 }
1763
1764 notit->buf.addr += consumed_bytes;
1765 notit->buf.sz -= consumed_bytes;
1766 notit->buf.at = 0;
1767 notit->buf.packet_offset = 0;
1768 BT_LOGV("Adjusted buffer: addr=%p, size=%zu",
1769 notit->buf.addr, notit->buf.sz);
1770 }
1771
1772 notit->cur_exp_packet_content_size = -1;
1773 notit->cur_exp_packet_total_size = -1;
1774 notit->cur_stream_class_id = -1;
1775 notit->cur_event_class_id = -1;
1776 notit->cur_data_stream_id = -1;
1777 notit->prev_packet_snapshots = notit->snapshots;
1778 notit->snapshots.discarded_events = UINT64_C(-1);
1779 notit->snapshots.packets = UINT64_C(-1);
1780 notit->snapshots.beginning_clock = UINT64_C(-1);
1781 notit->snapshots.end_clock = UINT64_C(-1);
1782
1783 end:
1784 return ret;
1785 }
1786
1787 static
1788 bt_field *borrow_next_field(struct bt_msg_iter *notit)
1789 {
1790 bt_field *next_field = NULL;
1791 bt_field *base_field;
1792 const bt_field_class *base_fc;
1793 size_t index;
1794
1795 BT_ASSERT(!stack_empty(notit->stack));
1796 index = stack_top(notit->stack)->index;
1797 base_field = stack_top(notit->stack)->base;
1798 BT_ASSERT(base_field);
1799 base_fc = bt_field_borrow_class_const(base_field);
1800 BT_ASSERT(base_fc);
1801
1802 switch (bt_field_class_get_type(base_fc)) {
1803 case BT_FIELD_CLASS_TYPE_STRUCTURE:
1804 {
1805 BT_ASSERT(index <
1806 bt_field_class_structure_get_member_count(
1807 bt_field_borrow_class_const(
1808 base_field)));
1809 next_field =
1810 bt_field_structure_borrow_member_field_by_index(
1811 base_field, index);
1812 break;
1813 }
1814 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
1815 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
1816 BT_ASSERT(index < bt_field_array_get_length(base_field));
1817 next_field = bt_field_array_borrow_element_field_by_index(
1818 base_field, index);
1819 break;
1820 case BT_FIELD_CLASS_TYPE_VARIANT:
1821 BT_ASSERT(index == 0);
1822 next_field = bt_field_variant_borrow_selected_option_field(
1823 base_field);
1824 break;
1825 default:
1826 abort();
1827 }
1828
1829 BT_ASSERT(next_field);
1830 return next_field;
1831 }
1832
1833 static
1834 void update_default_clock(struct bt_msg_iter *notit, uint64_t new_val,
1835 uint64_t new_val_size)
1836 {
1837 uint64_t new_val_mask;
1838 uint64_t cur_value_masked;
1839
1840 BT_ASSERT(new_val_size > 0);
1841
1842 /*
1843 * Special case for a 64-bit new value, which is the limit
1844 * of a clock value as of this version: overwrite the
1845 * current value directly.
1846 */
1847 if (new_val_size == 64) {
1848 notit->default_clock_snapshot = new_val;
1849 goto end;
1850 }
1851
1852 new_val_mask = (1ULL << new_val_size) - 1;
1853 cur_value_masked = notit->default_clock_snapshot & new_val_mask;
1854
1855 if (new_val < cur_value_masked) {
1856 /*
1857 * It looks like a wrap happened on the number of bits
1858 * of the requested new value. Assume that the clock
1859 * value wrapped only one time.
1860 */
1861 notit->default_clock_snapshot += new_val_mask + 1;
1862 }
1863
1864 /* Clear the low bits of the current clock value. */
1865 notit->default_clock_snapshot &= ~new_val_mask;
1866
1867 /* Set the low bits of the current clock value. */
1868 notit->default_clock_snapshot |= new_val;
1869
1870 end:
1871 BT_LOGV("Updated default clock's value from integer field's value: "
1872 "value=%" PRIu64, notit->default_clock_snapshot);
1873 }
1874
1875 static
1876 enum bt_bfcr_status bfcr_unsigned_int_cb(uint64_t value,
1877 struct ctf_field_class *fc, void *data)
1878 {
1879 struct bt_msg_iter *notit = data;
1880 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
1881 bt_field *field = NULL;
1882 struct ctf_field_class_int *int_fc = (void *) fc;
1883
1884 BT_LOGV("Unsigned integer function called from BFCR: "
1885 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1886 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64,
1887 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
1888
1889 if (likely(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE)) {
1890 goto update_def_clock;
1891 }
1892
1893 switch (int_fc->meaning) {
1894 case CTF_FIELD_CLASS_MEANING_EVENT_CLASS_ID:
1895 notit->cur_event_class_id = value;
1896 break;
1897 case CTF_FIELD_CLASS_MEANING_DATA_STREAM_ID:
1898 notit->cur_data_stream_id = value;
1899 break;
1900 case CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME:
1901 notit->snapshots.beginning_clock = value;
1902 break;
1903 case CTF_FIELD_CLASS_MEANING_PACKET_END_TIME:
1904 notit->snapshots.end_clock = value;
1905 break;
1906 case CTF_FIELD_CLASS_MEANING_STREAM_CLASS_ID:
1907 notit->cur_stream_class_id = value;
1908 break;
1909 case CTF_FIELD_CLASS_MEANING_MAGIC:
1910 if (value != 0xc1fc1fc1) {
1911 BT_LOGW("Invalid CTF magic number: notit-addr=%p, "
1912 "magic=%" PRIx64, notit, value);
1913 status = BT_BFCR_STATUS_ERROR;
1914 goto end;
1915 }
1916
1917 break;
1918 case CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT:
1919 notit->snapshots.packets = value;
1920 break;
1921 case CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT:
1922 notit->snapshots.discarded_events = value;
1923 break;
1924 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_TOTAL_SIZE:
1925 notit->cur_exp_packet_total_size = value;
1926 break;
1927 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_CONTENT_SIZE:
1928 notit->cur_exp_packet_content_size = value;
1929 break;
1930 default:
1931 abort();
1932 }
1933
1934 update_def_clock:
1935 if (unlikely(int_fc->mapped_clock_class)) {
1936 update_default_clock(notit, value, int_fc->base.size);
1937 }
1938
1939 if (unlikely(int_fc->storing_index >= 0)) {
1940 g_array_index(notit->stored_values, uint64_t,
1941 (uint64_t) int_fc->storing_index) = value;
1942 }
1943
1944 if (unlikely(!fc->in_ir)) {
1945 goto end;
1946 }
1947
1948 field = borrow_next_field(notit);
1949 BT_ASSERT(field);
1950 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
1951 BT_ASSERT(bt_field_get_class_type(field) ==
1952 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER ||
1953 bt_field_get_class_type(field) ==
1954 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION);
1955 bt_field_unsigned_integer_set_value(field, value);
1956 stack_top(notit->stack)->index++;
1957
1958 end:
1959 return status;
1960 }
1961
1962 static
1963 enum bt_bfcr_status bfcr_unsigned_int_char_cb(uint64_t value,
1964 struct ctf_field_class *fc, void *data)
1965 {
1966 int ret;
1967 struct bt_msg_iter *notit = data;
1968 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
1969 bt_field *string_field = NULL;
1970 struct ctf_field_class_int *int_fc = (void *) fc;
1971 char str[2] = {'\0', '\0'};
1972
1973 BT_LOGV("Unsigned integer character function called from BFCR: "
1974 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1975 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64,
1976 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
1977 BT_ASSERT(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE);
1978 BT_ASSERT(!int_fc->mapped_clock_class);
1979 BT_ASSERT(int_fc->storing_index < 0);
1980
1981 if (unlikely(!fc->in_ir)) {
1982 goto end;
1983 }
1984
1985 if (notit->done_filling_string) {
1986 goto end;
1987 }
1988
1989 if (value == 0) {
1990 notit->done_filling_string = true;
1991 goto end;
1992 }
1993
1994 string_field = stack_top(notit->stack)->base;
1995 BT_ASSERT(bt_field_get_class_type(string_field) ==
1996 BT_FIELD_CLASS_TYPE_STRING);
1997
1998 /* Append character */
1999 str[0] = (char) value;
2000 ret = bt_field_string_append_with_length(string_field, str, 1);
2001 if (ret) {
2002 BT_LOGE("Cannot append character to string field's value: "
2003 "notit-addr=%p, field-addr=%p, ret=%d",
2004 notit, string_field, ret);
2005 status = BT_BFCR_STATUS_ERROR;
2006 goto end;
2007 }
2008
2009 end:
2010 return status;
2011 }
2012
2013 static
2014 enum bt_bfcr_status bfcr_signed_int_cb(int64_t value,
2015 struct ctf_field_class *fc, void *data)
2016 {
2017 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
2018 bt_field *field = NULL;
2019 struct bt_msg_iter *notit = data;
2020 struct ctf_field_class_int *int_fc = (void *) fc;
2021
2022 BT_LOGV("Signed integer function called from BFCR: "
2023 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2024 "fc-type=%d, fc-in-ir=%d, value=%" PRId64,
2025 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
2026 BT_ASSERT(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE);
2027
2028 if (unlikely(int_fc->storing_index >= 0)) {
2029 g_array_index(notit->stored_values, uint64_t,
2030 (uint64_t) int_fc->storing_index) = (uint64_t) value;
2031 }
2032
2033 if (unlikely(!fc->in_ir)) {
2034 goto end;
2035 }
2036
2037 field = borrow_next_field(notit);
2038 BT_ASSERT(field);
2039 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
2040 BT_ASSERT(bt_field_get_class_type(field) ==
2041 BT_FIELD_CLASS_TYPE_SIGNED_INTEGER ||
2042 bt_field_get_class_type(field) ==
2043 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION);
2044 bt_field_signed_integer_set_value(field, value);
2045 stack_top(notit->stack)->index++;
2046
2047 end:
2048 return status;
2049 }
2050
2051 static
2052 enum bt_bfcr_status bfcr_floating_point_cb(double value,
2053 struct ctf_field_class *fc, void *data)
2054 {
2055 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
2056 bt_field *field = NULL;
2057 struct bt_msg_iter *notit = data;
2058
2059 BT_LOGV("Floating point number function called from BFCR: "
2060 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2061 "fc-type=%d, fc-in-ir=%d, value=%f",
2062 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
2063
2064 if (unlikely(!fc->in_ir)) {
2065 goto end;
2066 }
2067
2068 field = borrow_next_field(notit);
2069 BT_ASSERT(field);
2070 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
2071 BT_ASSERT(bt_field_get_class_type(field) ==
2072 BT_FIELD_CLASS_TYPE_REAL);
2073 bt_field_real_set_value(field, value);
2074 stack_top(notit->stack)->index++;
2075
2076 end:
2077 return status;
2078 }
2079
2080 static
2081 enum bt_bfcr_status bfcr_string_begin_cb(
2082 struct ctf_field_class *fc, void *data)
2083 {
2084 bt_field *field = NULL;
2085 struct bt_msg_iter *notit = data;
2086 int ret;
2087
2088 BT_LOGV("String (beginning) function called from BFCR: "
2089 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2090 "fc-type=%d, fc-in-ir=%d",
2091 notit, notit->bfcr, fc, fc->type, fc->in_ir);
2092
2093 if (unlikely(!fc->in_ir)) {
2094 goto end;
2095 }
2096
2097 field = borrow_next_field(notit);
2098 BT_ASSERT(field);
2099 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
2100 BT_ASSERT(bt_field_get_class_type(field) ==
2101 BT_FIELD_CLASS_TYPE_STRING);
2102 ret = bt_field_string_clear(field);
2103 BT_ASSERT(ret == 0);
2104
2105 /*
2106 * Push on stack. Not a compound class per se, but we know that
2107 * only bfcr_string_cb() may be called between this call and a
2108 * subsequent call to bfcr_string_end_cb().
2109 */
2110 stack_push(notit->stack, field);
2111
2112 end:
2113 return BT_BFCR_STATUS_OK;
2114 }
2115
2116 static
2117 enum bt_bfcr_status bfcr_string_cb(const char *value,
2118 size_t len, struct ctf_field_class *fc, void *data)
2119 {
2120 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
2121 bt_field *field = NULL;
2122 struct bt_msg_iter *notit = data;
2123 int ret;
2124
2125 BT_LOGV("String (substring) function called from BFCR: "
2126 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2127 "fc-type=%d, fc-in-ir=%d, string-length=%zu",
2128 notit, notit->bfcr, fc, fc->type, fc->in_ir,
2129 len);
2130
2131 if (unlikely(!fc->in_ir)) {
2132 goto end;
2133 }
2134
2135 field = stack_top(notit->stack)->base;
2136 BT_ASSERT(field);
2137
2138 /* Append current substring */
2139 ret = bt_field_string_append_with_length(field, value, len);
2140 if (ret) {
2141 BT_LOGE("Cannot append substring to string field's value: "
2142 "notit-addr=%p, field-addr=%p, string-length=%zu, "
2143 "ret=%d", notit, field, len, ret);
2144 status = BT_BFCR_STATUS_ERROR;
2145 goto end;
2146 }
2147
2148 end:
2149 return status;
2150 }
2151
2152 static
2153 enum bt_bfcr_status bfcr_string_end_cb(
2154 struct ctf_field_class *fc, void *data)
2155 {
2156 struct bt_msg_iter *notit = data;
2157
2158 BT_LOGV("String (end) function called from BFCR: "
2159 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2160 "fc-type=%d, fc-in-ir=%d",
2161 notit, notit->bfcr, fc, fc->type, fc->in_ir);
2162
2163 if (unlikely(!fc->in_ir)) {
2164 goto end;
2165 }
2166
2167 /* Pop string field */
2168 stack_pop(notit->stack);
2169
2170 /* Go to next field */
2171 stack_top(notit->stack)->index++;
2172
2173 end:
2174 return BT_BFCR_STATUS_OK;
2175 }
2176
2177 enum bt_bfcr_status bfcr_compound_begin_cb(
2178 struct ctf_field_class *fc, void *data)
2179 {
2180 struct bt_msg_iter *notit = data;
2181 bt_field *field;
2182
2183 BT_LOGV("Compound (beginning) function called from BFCR: "
2184 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2185 "fc-type=%d, fc-in-ir=%d",
2186 notit, notit->bfcr, fc, fc->type, fc->in_ir);
2187
2188 if (!fc->in_ir) {
2189 goto end;
2190 }
2191
2192 /* Borrow field */
2193 if (stack_empty(notit->stack)) {
2194 /* Root: already set by read_dscope_begin_state() */
2195 field = notit->cur_dscope_field;
2196 } else {
2197 field = borrow_next_field(notit);
2198 BT_ASSERT(field);
2199 }
2200
2201 /* Push field */
2202 BT_ASSERT(field);
2203 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
2204 stack_push(notit->stack, field);
2205
2206 /*
2207 * Change BFCR "unsigned int" callback if it's a text
2208 * array/sequence.
2209 */
2210 if (fc->type == CTF_FIELD_CLASS_TYPE_ARRAY ||
2211 fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE) {
2212 struct ctf_field_class_array_base *array_fc = (void *) fc;
2213
2214 if (array_fc->is_text) {
2215 int ret;
2216
2217 BT_ASSERT(bt_field_get_class_type(field) ==
2218 BT_FIELD_CLASS_TYPE_STRING);
2219 notit->done_filling_string = false;
2220 ret = bt_field_string_clear(field);
2221 BT_ASSERT(ret == 0);
2222 bt_bfcr_set_unsigned_int_cb(notit->bfcr,
2223 bfcr_unsigned_int_char_cb);
2224 }
2225 }
2226
2227 end:
2228 return BT_BFCR_STATUS_OK;
2229 }
2230
2231 enum bt_bfcr_status bfcr_compound_end_cb(
2232 struct ctf_field_class *fc, void *data)
2233 {
2234 struct bt_msg_iter *notit = data;
2235
2236 BT_LOGV("Compound (end) function called from BFCR: "
2237 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2238 "fc-type=%d, fc-in-ir=%d",
2239 notit, notit->bfcr, fc, fc->type, fc->in_ir);
2240
2241 if (!fc->in_ir) {
2242 goto end;
2243 }
2244
2245 BT_ASSERT(!stack_empty(notit->stack));
2246 BT_ASSERT(bt_field_borrow_class_const(stack_top(notit->stack)->base) ==
2247 fc->ir_fc);
2248
2249 /*
2250 * Reset BFCR "unsigned int" callback if it's a text
2251 * array/sequence.
2252 */
2253 if (fc->type == CTF_FIELD_CLASS_TYPE_ARRAY ||
2254 fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE) {
2255 struct ctf_field_class_array_base *array_fc = (void *) fc;
2256
2257 if (array_fc->is_text) {
2258 BT_ASSERT(bt_field_get_class_type(
2259 stack_top(notit->stack)->base) ==
2260 BT_FIELD_CLASS_TYPE_STRING);
2261 bt_bfcr_set_unsigned_int_cb(notit->bfcr,
2262 bfcr_unsigned_int_cb);
2263 }
2264 }
2265
2266 /* Pop stack */
2267 stack_pop(notit->stack);
2268
2269 /* If the stack is not empty, increment the base's index */
2270 if (!stack_empty(notit->stack)) {
2271 stack_top(notit->stack)->index++;
2272 }
2273
2274 end:
2275 return BT_BFCR_STATUS_OK;
2276 }
2277
2278 static
2279 int64_t bfcr_get_sequence_length_cb(struct ctf_field_class *fc, void *data)
2280 {
2281 bt_field *seq_field;
2282 struct bt_msg_iter *notit = data;
2283 struct ctf_field_class_sequence *seq_fc = (void *) fc;
2284 int64_t length = -1;
2285 int ret;
2286
2287 length = (uint64_t) g_array_index(notit->stored_values, uint64_t,
2288 seq_fc->stored_length_index);
2289 seq_field = stack_top(notit->stack)->base;
2290 BT_ASSERT(seq_field);
2291
2292 /*
2293 * bfcr_get_sequence_length_cb() also gets called back for a
2294 * text sequence, but the destination field is a string field.
2295 * Only set the field's sequence length if the destination field
2296 * is a sequence field.
2297 */
2298 if (!seq_fc->base.is_text) {
2299 BT_ASSERT(bt_field_get_class_type(seq_field) ==
2300 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY);
2301 ret = bt_field_dynamic_array_set_length(seq_field,
2302 (uint64_t) length);
2303 if (ret) {
2304 BT_LOGE("Cannot set dynamic array field's length field: "
2305 "notit-addr=%p, field-addr=%p, "
2306 "length=%" PRIu64, notit, seq_field, length);
2307 }
2308 }
2309
2310 return length;
2311 }
2312
2313 static
2314 struct ctf_field_class *bfcr_borrow_variant_selected_field_class_cb(
2315 struct ctf_field_class *fc, void *data)
2316 {
2317 int ret;
2318 uint64_t i;
2319 int64_t option_index = -1;
2320 struct bt_msg_iter *notit = data;
2321 struct ctf_field_class_variant *var_fc = (void *) fc;
2322 struct ctf_named_field_class *selected_option = NULL;
2323 struct ctf_field_class *ret_fc = NULL;
2324 union {
2325 uint64_t u;
2326 int64_t i;
2327 } tag;
2328
2329 /* Get variant's tag */
2330 tag.u = g_array_index(notit->stored_values, uint64_t,
2331 var_fc->stored_tag_index);
2332
2333 /*
2334 * Check each range to find the selected option's index.
2335 */
2336 if (var_fc->tag_fc->base.is_signed) {
2337 for (i = 0; i < var_fc->ranges->len; i++) {
2338 struct ctf_field_class_variant_range *range =
2339 ctf_field_class_variant_borrow_range_by_index(
2340 var_fc, i);
2341
2342 if (tag.i >= range->range.lower.i &&
2343 tag.i <= range->range.upper.i) {
2344 option_index = (int64_t) range->option_index;
2345 break;
2346 }
2347 }
2348 } else {
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.u >= range->range.lower.u &&
2355 tag.u <= range->range.upper.u) {
2356 option_index = (int64_t) range->option_index;
2357 break;
2358 }
2359 }
2360 }
2361
2362 if (option_index < 0) {
2363 BT_LOGW("Cannot find variant field class's option: "
2364 "notit-addr=%p, var-fc-addr=%p, u-tag=%" PRIu64 ", "
2365 "i-tag=%" PRId64, notit, var_fc, tag.u, tag.i);
2366 goto end;
2367 }
2368
2369 selected_option = ctf_field_class_variant_borrow_option_by_index(
2370 var_fc, (uint64_t) option_index);
2371
2372 if (selected_option->fc->in_ir) {
2373 bt_field *var_field = stack_top(notit->stack)->base;
2374
2375 ret = bt_field_variant_select_option_field(
2376 var_field, option_index);
2377 if (ret) {
2378 BT_LOGW("Cannot select variant field's option field: "
2379 "notit-addr=%p, var-field-addr=%p, "
2380 "opt-index=%" PRId64, notit, var_field,
2381 option_index);
2382 goto end;
2383 }
2384 }
2385
2386 ret_fc = selected_option->fc;
2387
2388 end:
2389 return ret_fc;
2390 }
2391
2392 static
2393 void create_msg_stream_beginning(struct bt_msg_iter *notit,
2394 bt_message **message)
2395 {
2396 bt_message *ret = NULL;
2397
2398 BT_ASSERT(notit->stream);
2399 BT_ASSERT(notit->msg_iter);
2400 ret = bt_message_stream_beginning_create(notit->msg_iter,
2401 notit->stream);
2402 if (!ret) {
2403 BT_LOGE("Cannot create stream beginning message: "
2404 "notit-addr=%p, stream-addr=%p",
2405 notit, notit->stream);
2406 return;
2407 }
2408
2409 *message = ret;
2410 }
2411
2412 static
2413 void create_msg_stream_activity_beginning(struct bt_msg_iter *notit,
2414 bt_message **message)
2415 {
2416 bt_message *ret = NULL;
2417
2418 BT_ASSERT(notit->stream);
2419 BT_ASSERT(notit->msg_iter);
2420 ret = bt_message_stream_activity_beginning_create(notit->msg_iter,
2421 notit->stream);
2422 if (!ret) {
2423 BT_LOGE("Cannot create stream activity beginning message: "
2424 "notit-addr=%p, stream-addr=%p",
2425 notit, notit->stream);
2426 return;
2427 }
2428
2429 *message = ret;
2430 }
2431
2432 static
2433 void create_msg_stream_activity_end(struct bt_msg_iter *notit,
2434 bt_message **message)
2435 {
2436 bt_message *ret = NULL;
2437
2438 if (!notit->stream) {
2439 BT_LOGE("Cannot create stream for stream message: "
2440 "notit-addr=%p", notit);
2441 return;
2442 }
2443
2444 BT_ASSERT(notit->stream);
2445 BT_ASSERT(notit->msg_iter);
2446 ret = bt_message_stream_activity_end_create(notit->msg_iter,
2447 notit->stream);
2448 if (!ret) {
2449 BT_LOGE("Cannot create stream activity end message: "
2450 "notit-addr=%p, stream-addr=%p",
2451 notit, notit->stream);
2452 return;
2453 }
2454
2455 *message = ret;
2456 }
2457
2458 static
2459 void create_msg_stream_end(struct bt_msg_iter *notit, bt_message **message)
2460 {
2461 bt_message *ret;
2462
2463 if (!notit->stream) {
2464 BT_LOGE("Cannot create stream for stream message: "
2465 "notit-addr=%p", notit);
2466 return;
2467 }
2468
2469 BT_ASSERT(notit->msg_iter);
2470 ret = bt_message_stream_end_create(notit->msg_iter,
2471 notit->stream);
2472 if (!ret) {
2473 BT_LOGE("Cannot create stream end message: "
2474 "notit-addr=%p, stream-addr=%p",
2475 notit, notit->stream);
2476 return;
2477 }
2478
2479 *message = ret;
2480 }
2481
2482 static
2483 void create_msg_packet_beginning(struct bt_msg_iter *notit,
2484 bt_message **message)
2485 {
2486 int ret;
2487 enum bt_msg_iter_status status;
2488 bt_message *msg = NULL;
2489 const bt_stream_class *sc;
2490
2491 status = set_current_packet(notit);
2492 if (status != BT_MSG_ITER_STATUS_OK) {
2493 goto end;
2494 }
2495
2496 BT_ASSERT(notit->packet);
2497 sc = notit->meta.sc->ir_sc;
2498 BT_ASSERT(sc);
2499
2500 if (notit->packet_context_field) {
2501 ret = bt_packet_move_context_field(
2502 notit->packet, notit->packet_context_field);
2503 if (ret) {
2504 goto end;
2505 }
2506
2507 notit->packet_context_field = NULL;
2508
2509 /*
2510 * At this point notit->dscopes.stream_packet_context
2511 * has the same value as the packet context field within
2512 * notit->packet.
2513 */
2514 BT_ASSERT(bt_packet_borrow_context_field(
2515 notit->packet) ==
2516 notit->dscopes.stream_packet_context);
2517 }
2518
2519 BT_ASSERT(notit->msg_iter);
2520
2521 if (bt_stream_class_borrow_default_clock_class(notit->meta.sc->ir_sc)) {
2522 uint64_t value = 0;
2523
2524 if (notit->snapshots.beginning_clock == UINT64_C(-1)) {
2525 if (notit->prev_packet_snapshots.end_clock != UINT64_C(-1)) {
2526 value = notit->prev_packet_snapshots.end_clock;
2527 }
2528 } else {
2529 value = notit->snapshots.beginning_clock;
2530 }
2531
2532
2533 msg = bt_message_packet_beginning_create_with_default_clock_snapshot(
2534 notit->msg_iter, notit->packet, value);
2535 } else {
2536 msg = bt_message_packet_beginning_create(notit->msg_iter,
2537 notit->packet);
2538 }
2539
2540 if (!msg) {
2541 BT_LOGE("Cannot create packet beginning message: "
2542 "notit-addr=%p, packet-addr=%p",
2543 notit, notit->packet);
2544 goto end;
2545 }
2546
2547 *message = msg;
2548
2549 end:
2550 return;
2551 }
2552
2553 static
2554 void create_msg_packet_end(struct bt_msg_iter *notit, bt_message **message)
2555 {
2556 bt_message *msg;
2557
2558 if (!notit->packet) {
2559 return;
2560 }
2561
2562 /* Update default clock from packet's end time */
2563 if (notit->snapshots.end_clock != UINT64_C(-1)) {
2564 notit->default_clock_snapshot = notit->snapshots.end_clock;
2565 }
2566
2567 BT_ASSERT(notit->msg_iter);
2568
2569 if (bt_stream_class_borrow_default_clock_class(notit->meta.sc->ir_sc)) {
2570 if (notit->snapshots.end_clock == UINT64_C(-1)) {
2571 notit->snapshots.end_clock =
2572 notit->default_clock_snapshot;
2573 }
2574
2575 BT_ASSERT(notit->snapshots.end_clock != UINT64_C(-1));
2576 msg = bt_message_packet_end_create_with_default_clock_snapshot(
2577 notit->msg_iter, notit->packet,
2578 notit->snapshots.end_clock);
2579 } else {
2580 msg = bt_message_packet_end_create(notit->msg_iter,
2581 notit->packet);
2582 }
2583
2584 if (!msg) {
2585 BT_LOGE("Cannot create packet end message: "
2586 "notit-addr=%p, packet-addr=%p",
2587 notit, notit->packet);
2588 return;
2589
2590 }
2591
2592 BT_PACKET_PUT_REF_AND_RESET(notit->packet);
2593 *message = msg;
2594 }
2595
2596 static
2597 void create_msg_discarded_events(struct bt_msg_iter *notit,
2598 bt_message **message)
2599 {
2600 bt_message *msg;
2601 uint64_t beginning_raw_value = UINT64_C(-1);
2602 uint64_t end_raw_value = UINT64_C(-1);
2603 uint64_t count = UINT64_C(-1);
2604
2605 BT_ASSERT(notit->msg_iter);
2606 BT_ASSERT(notit->stream);
2607
2608 if (notit->prev_packet_snapshots.discarded_events == UINT64_C(-1)) {
2609 /*
2610 * We discarded events, but before (and possibly
2611 * including) the current packet: use this packet's time
2612 * range, and do not have a specific count.
2613 */
2614 beginning_raw_value = notit->snapshots.beginning_clock;
2615 end_raw_value = notit->snapshots.end_clock;
2616 } else {
2617 count = notit->snapshots.discarded_events -
2618 notit->prev_packet_snapshots.discarded_events;
2619 BT_ASSERT(count > 0);
2620 beginning_raw_value = notit->prev_packet_snapshots.end_clock;
2621 end_raw_value = notit->snapshots.end_clock;
2622 }
2623
2624 if (beginning_raw_value != UINT64_C(-1) &&
2625 end_raw_value != UINT64_C(-1)) {
2626 msg = bt_message_discarded_events_create_with_default_clock_snapshots(
2627 notit->msg_iter, notit->stream, beginning_raw_value,
2628 end_raw_value);
2629 } else {
2630 msg = bt_message_discarded_events_create(notit->msg_iter,
2631 notit->stream);
2632 }
2633
2634 if (!msg) {
2635 BT_LOGE("Cannot create discarded events message: "
2636 "notit-addr=%p, stream-addr=%p",
2637 notit, notit->stream);
2638 return;
2639 }
2640
2641 if (count != UINT64_C(-1)) {
2642 bt_message_discarded_events_set_count(msg, count);
2643 }
2644
2645 *message = msg;
2646 }
2647
2648 static
2649 void create_msg_discarded_packets(struct bt_msg_iter *notit,
2650 bt_message **message)
2651 {
2652 bt_message *msg;
2653
2654 BT_ASSERT(notit->msg_iter);
2655 BT_ASSERT(notit->stream);
2656 BT_ASSERT(notit->prev_packet_snapshots.packets !=
2657 UINT64_C(-1));
2658
2659 if (notit->prev_packet_snapshots.end_clock != UINT64_C(-1) &&
2660 notit->snapshots.beginning_clock != UINT64_C(-1)) {
2661 msg = bt_message_discarded_packets_create_with_default_clock_snapshots(
2662 notit->msg_iter, notit->stream,
2663 notit->prev_packet_snapshots.end_clock,
2664 notit->snapshots.beginning_clock);
2665 } else {
2666 msg = bt_message_discarded_packets_create(notit->msg_iter,
2667 notit->stream);
2668 }
2669
2670 if (!msg) {
2671 BT_LOGE("Cannot create discarded packets message: "
2672 "notit-addr=%p, stream-addr=%p",
2673 notit, notit->stream);
2674 return;
2675 }
2676
2677 bt_message_discarded_packets_set_count(msg,
2678 notit->snapshots.packets -
2679 notit->prev_packet_snapshots.packets - 1);
2680 *message = msg;
2681 }
2682
2683 BT_HIDDEN
2684 struct bt_msg_iter *bt_msg_iter_create(struct ctf_trace_class *tc,
2685 size_t max_request_sz,
2686 struct bt_msg_iter_medium_ops medops, void *data)
2687 {
2688 struct bt_msg_iter *notit = NULL;
2689 struct bt_bfcr_cbs cbs = {
2690 .classes = {
2691 .signed_int = bfcr_signed_int_cb,
2692 .unsigned_int = bfcr_unsigned_int_cb,
2693 .floating_point = bfcr_floating_point_cb,
2694 .string_begin = bfcr_string_begin_cb,
2695 .string = bfcr_string_cb,
2696 .string_end = bfcr_string_end_cb,
2697 .compound_begin = bfcr_compound_begin_cb,
2698 .compound_end = bfcr_compound_end_cb,
2699 },
2700 .query = {
2701 .get_sequence_length = bfcr_get_sequence_length_cb,
2702 .borrow_variant_selected_field_class = bfcr_borrow_variant_selected_field_class_cb,
2703 },
2704 };
2705
2706 BT_ASSERT(tc);
2707 BT_ASSERT(medops.request_bytes);
2708 BT_ASSERT(medops.borrow_stream);
2709 BT_LOGD("Creating CTF plugin message iterator: "
2710 "trace-addr=%p, max-request-size=%zu, "
2711 "data=%p", tc, max_request_sz, data);
2712 notit = g_new0(struct bt_msg_iter, 1);
2713 if (!notit) {
2714 BT_LOGE_STR("Failed to allocate one CTF plugin message iterator.");
2715 goto end;
2716 }
2717 notit->meta.tc = tc;
2718 notit->medium.medops = medops;
2719 notit->medium.max_request_sz = max_request_sz;
2720 notit->medium.data = data;
2721 notit->stack = stack_new(notit);
2722 notit->stored_values = g_array_new(FALSE, TRUE, sizeof(uint64_t));
2723 g_array_set_size(notit->stored_values, tc->stored_value_count);
2724
2725 if (!notit->stack) {
2726 BT_LOGE_STR("Failed to create field stack.");
2727 goto error;
2728 }
2729
2730 notit->bfcr = bt_bfcr_create(cbs, notit);
2731 if (!notit->bfcr) {
2732 BT_LOGE_STR("Failed to create binary class reader (BFCR).");
2733 goto error;
2734 }
2735
2736 bt_msg_iter_reset(notit);
2737 BT_LOGD("Created CTF plugin message iterator: "
2738 "trace-addr=%p, max-request-size=%zu, "
2739 "data=%p, notit-addr=%p",
2740 tc, max_request_sz, data, notit);
2741 notit->cur_packet_offset = 0;
2742
2743 end:
2744 return notit;
2745
2746 error:
2747 bt_msg_iter_destroy(notit);
2748 notit = NULL;
2749 goto end;
2750 }
2751
2752 void bt_msg_iter_destroy(struct bt_msg_iter *notit)
2753 {
2754 BT_PACKET_PUT_REF_AND_RESET(notit->packet);
2755 BT_STREAM_PUT_REF_AND_RESET(notit->stream);
2756 release_all_dscopes(notit);
2757
2758 BT_LOGD("Destroying CTF plugin message iterator: addr=%p", notit);
2759
2760 if (notit->stack) {
2761 BT_LOGD_STR("Destroying field stack.");
2762 stack_destroy(notit->stack);
2763 }
2764
2765 if (notit->bfcr) {
2766 BT_LOGD("Destroying BFCR: bfcr-addr=%p", notit->bfcr);
2767 bt_bfcr_destroy(notit->bfcr);
2768 }
2769
2770 if (notit->stored_values) {
2771 g_array_free(notit->stored_values, TRUE);
2772 }
2773
2774 g_free(notit);
2775 }
2776
2777 enum bt_msg_iter_status bt_msg_iter_get_next_message(
2778 struct bt_msg_iter *notit,
2779 bt_self_message_iterator *msg_iter, bt_message **message)
2780 {
2781 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
2782
2783 BT_ASSERT(notit);
2784 BT_ASSERT(message);
2785 notit->msg_iter = msg_iter;
2786 notit->set_stream = true;
2787 BT_LOGV("Getting next message: notit-addr=%p", notit);
2788
2789 while (true) {
2790 status = handle_state(notit);
2791 if (unlikely(status == BT_MSG_ITER_STATUS_AGAIN)) {
2792 BT_LOGV_STR("Medium returned BT_MSG_ITER_STATUS_AGAIN.");
2793 goto end;
2794 } else if (unlikely(status != BT_MSG_ITER_STATUS_OK)) {
2795 BT_LOGW("Cannot handle state: notit-addr=%p, state=%s",
2796 notit, state_string(notit->state));
2797 goto end;
2798 }
2799
2800 switch (notit->state) {
2801 case STATE_EMIT_MSG_EVENT:
2802 BT_ASSERT(notit->event_msg);
2803 *message = notit->event_msg;
2804 notit->event_msg = NULL;
2805 goto end;
2806 case STATE_EMIT_MSG_DISCARDED_EVENTS:
2807 /* create_msg_discared_events() logs errors */
2808 create_msg_discarded_events(notit, message);
2809
2810 if (!*message) {
2811 status = BT_MSG_ITER_STATUS_ERROR;
2812 }
2813
2814 goto end;
2815 case STATE_EMIT_MSG_DISCARDED_PACKETS:
2816 /* create_msg_discared_packets() logs errors */
2817 create_msg_discarded_packets(notit, message);
2818
2819 if (!*message) {
2820 status = BT_MSG_ITER_STATUS_ERROR;
2821 }
2822
2823 goto end;
2824 case STATE_EMIT_MSG_PACKET_BEGINNING:
2825 /* create_msg_packet_beginning() logs errors */
2826 create_msg_packet_beginning(notit, message);
2827
2828 if (!*message) {
2829 status = BT_MSG_ITER_STATUS_ERROR;
2830 }
2831
2832 goto end;
2833 case STATE_EMIT_MSG_PACKET_END_SINGLE:
2834 case STATE_EMIT_MSG_PACKET_END_MULTI:
2835 /* create_msg_packet_end() logs errors */
2836 create_msg_packet_end(notit, message);
2837
2838 if (!*message) {
2839 status = BT_MSG_ITER_STATUS_ERROR;
2840 }
2841
2842 goto end;
2843 case STATE_EMIT_MSG_STREAM_ACTIVITY_BEGINNING:
2844 /* create_msg_stream_activity_beginning() logs errors */
2845 create_msg_stream_activity_beginning(notit, message);
2846
2847 if (!*message) {
2848 status = BT_MSG_ITER_STATUS_ERROR;
2849 }
2850
2851 goto end;
2852 case STATE_EMIT_MSG_STREAM_ACTIVITY_END:
2853 /* create_msg_stream_activity_end() logs errors */
2854 create_msg_stream_activity_end(notit, message);
2855
2856 if (!*message) {
2857 status = BT_MSG_ITER_STATUS_ERROR;
2858 }
2859
2860 goto end;
2861 case STATE_EMIT_MSG_STREAM_BEGINNING:
2862 /* create_msg_stream_beginning() logs errors */
2863 create_msg_stream_beginning(notit, message);
2864
2865 if (!*message) {
2866 status = BT_MSG_ITER_STATUS_ERROR;
2867 }
2868
2869 goto end;
2870 case STATE_EMIT_MSG_STREAM_END:
2871 /* create_msg_stream_end() logs errors */
2872 create_msg_stream_end(notit, message);
2873
2874 if (!*message) {
2875 status = BT_MSG_ITER_STATUS_ERROR;
2876 }
2877
2878 goto end;
2879 case STATE_DONE:
2880 status = BT_MSG_ITER_STATUS_EOF;
2881 goto end;
2882 default:
2883 /* Non-emitting state: continue */
2884 break;
2885 }
2886 }
2887
2888 end:
2889 return status;
2890 }
2891
2892 static
2893 enum bt_msg_iter_status read_packet_header_context_fields(
2894 struct bt_msg_iter *notit)
2895 {
2896 int ret;
2897 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
2898
2899 BT_ASSERT(notit);
2900 notit->set_stream = false;
2901
2902 if (notit->state == STATE_EMIT_MSG_PACKET_BEGINNING) {
2903 /* We're already there */
2904 goto end;
2905 }
2906
2907 while (true) {
2908 status = handle_state(notit);
2909 if (unlikely(status == BT_MSG_ITER_STATUS_AGAIN)) {
2910 BT_LOGV_STR("Medium returned BT_MSG_ITER_STATUS_AGAIN.");
2911 goto end;
2912 } else if (unlikely(status != BT_MSG_ITER_STATUS_OK)) {
2913 BT_LOGW("Cannot handle state: notit-addr=%p, state=%s",
2914 notit, state_string(notit->state));
2915 goto end;
2916 }
2917
2918 switch (notit->state) {
2919 case STATE_EMIT_MSG_PACKET_BEGINNING:
2920 /*
2921 * Packet header and context fields are
2922 * potentially decoded (or they don't exist).
2923 */
2924 goto end;
2925 case STATE_INIT:
2926 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
2927 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
2928 case STATE_AFTER_TRACE_PACKET_HEADER:
2929 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN:
2930 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE:
2931 case STATE_AFTER_STREAM_PACKET_CONTEXT:
2932 case STATE_CHECK_EMIT_MSG_STREAM_BEGINNING:
2933 case STATE_EMIT_MSG_STREAM_BEGINNING:
2934 case STATE_EMIT_MSG_STREAM_ACTIVITY_BEGINNING:
2935 case STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS:
2936 case STATE_EMIT_MSG_DISCARDED_EVENTS:
2937 case STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS:
2938 case STATE_EMIT_MSG_DISCARDED_PACKETS:
2939 /* Non-emitting state: continue */
2940 break;
2941 default:
2942 /*
2943 * We should never get past the
2944 * STATE_EMIT_MSG_PACKET_BEGINNING state.
2945 */
2946 BT_LOGF("Unexpected state: notit-addr=%p, state=%s",
2947 notit, state_string(notit->state));
2948 abort();
2949 }
2950 }
2951
2952 end:
2953 ret = set_current_packet_content_sizes(notit);
2954 if (ret) {
2955 status = BT_MSG_ITER_STATUS_ERROR;
2956 }
2957
2958 return status;
2959 }
2960
2961 BT_HIDDEN
2962 void bt_msg_iter_set_medops_data(struct bt_msg_iter *notit,
2963 void *medops_data)
2964 {
2965 BT_ASSERT(notit);
2966 notit->medium.data = medops_data;
2967 }
2968
2969 BT_HIDDEN
2970 enum bt_msg_iter_status bt_msg_iter_seek(struct bt_msg_iter *notit,
2971 off_t offset)
2972 {
2973 enum bt_msg_iter_status ret = BT_MSG_ITER_STATUS_OK;
2974 enum bt_msg_iter_medium_status medium_status;
2975
2976 BT_ASSERT(notit);
2977 if (offset < 0) {
2978 BT_LOGE("Cannot seek to negative offset: offset=%jd", offset);
2979 ret = BT_MSG_ITER_STATUS_INVAL;
2980 goto end;
2981 }
2982
2983 if (!notit->medium.medops.seek) {
2984 ret = BT_MSG_ITER_STATUS_UNSUPPORTED;
2985 BT_LOGD("Aborting seek as the iterator's underlying media does not implement seek support.");
2986 goto end;
2987 }
2988
2989 medium_status = notit->medium.medops.seek(
2990 BT_MSG_ITER_SEEK_WHENCE_SET, offset, notit->medium.data);
2991 if (medium_status != BT_MSG_ITER_MEDIUM_STATUS_OK) {
2992 if (medium_status == BT_MSG_ITER_MEDIUM_STATUS_EOF) {
2993 ret = BT_MSG_ITER_STATUS_EOF;
2994 } else {
2995 ret = BT_MSG_ITER_STATUS_ERROR;
2996 goto end;
2997 }
2998 }
2999
3000 bt_msg_iter_reset(notit);
3001 notit->cur_packet_offset = offset;
3002
3003 end:
3004 return ret;
3005 }
3006
3007 BT_HIDDEN
3008 enum bt_msg_iter_status bt_msg_iter_get_packet_properties(
3009 struct bt_msg_iter *notit,
3010 struct bt_msg_iter_packet_properties *props)
3011 {
3012 enum bt_msg_iter_status status;
3013
3014 BT_ASSERT(notit);
3015 BT_ASSERT(props);
3016 status = read_packet_header_context_fields(notit);
3017 if (status != BT_MSG_ITER_STATUS_OK) {
3018 goto end;
3019 }
3020
3021 props->exp_packet_total_size = notit->cur_exp_packet_total_size;
3022 props->exp_packet_content_size = notit->cur_exp_packet_content_size;
3023 props->stream_class_id = (uint64_t) notit->cur_stream_class_id;
3024 props->data_stream_id = notit->cur_data_stream_id;
3025 props->snapshots.discarded_events = notit->snapshots.discarded_events;
3026 props->snapshots.packets = notit->snapshots.packets;
3027 props->snapshots.beginning_clock = notit->snapshots.beginning_clock;
3028 props->snapshots.end_clock = notit->snapshots.end_clock;
3029
3030 end:
3031 return status;
3032 }
3033
3034 BT_HIDDEN
3035 void bt_msg_iter_set_emit_stream_beginning_message(struct bt_msg_iter *notit,
3036 bool val)
3037 {
3038 notit->emit_stream_begin_msg = val;
3039 }
3040
3041 BT_HIDDEN
3042 void bt_msg_iter_set_emit_stream_end_message(struct bt_msg_iter *notit,
3043 bool val)
3044 {
3045 notit->emit_stream_end_msg = val;
3046 }
This page took 0.135163 seconds and 4 git commands to generate.