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