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