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