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