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