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