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