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