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