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