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