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