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