ctf: decoding: accommodate LTTng `event-after-packet` timestamp quirk
[babeltrace.git] / src / plugins / ctf / common / msg-iter / msg-iter.c
CommitLineData
e98a2d6e 1/*
b09a5592 2 * Babeltrace - CTF message iterator
06a626b8 3 *
7b33a0e0
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
e310e6e2 26#define BT_COMP_LOG_SELF_COMP (notit->self_comp)
f8811e3b 27#define BT_LOG_OUTPUT_LEVEL (notit->log_level)
b03487ab 28#define BT_LOG_TAG "PLUGIN/CTF/MSG-ITER"
3fa1b6a3 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>
57952005 36#include "common/assert.h"
e98a2d6e 37#include <string.h>
71c5da58 38#include <babeltrace2/babeltrace.h>
57952005 39#include "common/common.h"
e98a2d6e 40#include <glib.h>
0fbb9a9f 41#include <stdlib.h>
e98a2d6e 42
b09a5592 43#include "msg-iter.h"
939190b3 44#include "../bfcr/bfcr.h"
e98a2d6e 45
b09a5592 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 *
a6918753 59 * Field is borrowed.
e98a2d6e 60 */
8eee8ea2 61 bt_field *base;
e98a2d6e 62
7b33a0e0 63 /* Index of next field to set */
e98a2d6e
PP
64 size_t index;
65};
66
f8811e3b
PP
67struct bt_msg_iter;
68
e98a2d6e
PP
69/* Visit stack */
70struct stack {
f8811e3b
PP
71 struct bt_msg_iter *notit;
72
87bdbbb0
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,
01ed875d 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,
989925d1 90 STATE_CHECK_EMIT_MSG_STREAM_BEGINNING,
bbbd35bf 91 STATE_EMIT_MSG_STREAM_BEGINNING,
989925d1
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,
bbbd35bf 96 STATE_EMIT_MSG_PACKET_BEGINNING,
7b33a0e0
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,
b09a5592 106 STATE_EMIT_MSG_EVENT,
e98a2d6e 107 STATE_SKIP_PACKET_PADDING,
bbbd35bf
PP
108 STATE_EMIT_MSG_PACKET_END_MULTI,
109 STATE_EMIT_MSG_PACKET_END_SINGLE,
b7cbc799 110 STATE_CHECK_EMIT_MSG_STREAM_END,
bbbd35bf
PP
111 STATE_EMIT_MSG_STREAM_END,
112 STATE_DONE,
e98a2d6e
PP
113};
114
989925d1
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
b09a5592
PP
122/* CTF message iterator */
123struct bt_msg_iter {
e98a2d6e
PP
124 /* Visit stack */
125 struct stack *stack;
126
b09a5592
PP
127 /* Current message iterator to create messages (weak) */
128 bt_self_message_iterator *msg_iter;
f7c3ac09 129
b7cbc799 130 /* True to emit a stream beginning message. */
bbbd35bf
PP
131 bool emit_stream_begin_msg;
132
b7cbc799 133 /* True to emit a stream end message. */
bbbd35bf
PP
134 bool emit_stream_end_msg;
135
8482a078
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
989925d1
PP
142 /* True to set the stream */
143 bool set_stream;
144
e98a2d6e
PP
145 /*
146 * Current dynamic scope field pointer.
147 *
a6918753
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 */
8eee8ea2 151 bt_field *cur_dscope_field;
e98a2d6e 152
7b33a0e0
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 */
e5e71bf8
PP
160 /* True to set IR fields */
161 bool set_ir_fields;
162
e98a2d6e 163 struct {
7b33a0e0
PP
164 struct ctf_trace_class *tc;
165 struct ctf_stream_class *sc;
166 struct ctf_event_class *ec;
e98a2d6e
PP
167 } meta;
168
0476f6f7 169 /* Current packet context field wrapper (NULL if not created yet) */
8eee8ea2 170 bt_packet_context_field *packet_context_field;
a6918753 171
e98a2d6e 172 /* Current packet (NULL if not created yet) */
8eee8ea2 173 bt_packet *packet;
e98a2d6e 174
5184a1d6 175 /* Current stream (NULL if not set yet) */
8eee8ea2 176 bt_stream *stream;
5184a1d6 177
a6918753 178 /* Current event (NULL if not created yet) */
8eee8ea2 179 bt_event *event;
a6918753 180
b09a5592
PP
181 /* Current event message (NULL if not created yet) */
182 bt_message *event_msg;
a6918753 183
7b33a0e0 184 /* Database of current dynamic scopes */
e98a2d6e 185 struct {
8eee8ea2 186 bt_field *stream_packet_context;
8eee8ea2
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;
9b876b7b
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 */
939190b3 214 struct bt_bfcr *bfcr;
e98a2d6e 215
2cf1d51e 216 /* Current medium data */
e98a2d6e 217 struct {
b09a5592 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) */
7b33a0e0 224 int64_t cur_exp_packet_total_size;
e98a2d6e
PP
225
226 /* Current content size (bits) (-1 if unknown) */
7b33a0e0 227 int64_t cur_exp_packet_content_size;
c44c3e70 228
7b33a0e0
PP
229 /* Current stream class ID */
230 int64_t cur_stream_class_id;
bb230c9b 231
7b33a0e0
PP
232 /* Current event class ID */
233 int64_t cur_event_class_id;
5f870343 234
7b33a0e0
PP
235 /* Current data stream ID */
236 int64_t cur_data_stream_id;
5f870343
JG
237
238 /*
7b33a0e0
PP
239 * Offset, in the underlying media, of the current packet's
240 * start (-1 if unknown).
5f870343 241 */
7b33a0e0
PP
242 off_t cur_packet_offset;
243
244 /* Default clock's current value */
ecbb78c0 245 uint64_t default_clock_snapshot;
5f870343 246
989925d1
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;
7b33a0e0
PP
252
253 /* Stored values (for sequence lengths, variant tags) */
254 GArray *stored_values;
f8811e3b
PP
255
256 /* Iterator's current log level */
257 bt_logging_level log_level;
e310e6e2
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:
e05f8464 268 return "INIT";
01ed875d
PP
269 case STATE_SWITCH_PACKET:
270 return "SWITCH_PACKET";
fdf0e7a0 271 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
e05f8464 272 return "DSCOPE_TRACE_PACKET_HEADER_BEGIN";
fdf0e7a0 273 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
e05f8464 274 return "DSCOPE_TRACE_PACKET_HEADER_CONTINUE";
fdf0e7a0 275 case STATE_AFTER_TRACE_PACKET_HEADER:
e05f8464 276 return "AFTER_TRACE_PACKET_HEADER";
fdf0e7a0 277 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN:
e05f8464 278 return "DSCOPE_STREAM_PACKET_CONTEXT_BEGIN";
fdf0e7a0 279 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE:
e05f8464 280 return "DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE";
fdf0e7a0 281 case STATE_AFTER_STREAM_PACKET_CONTEXT:
e05f8464 282 return "AFTER_STREAM_PACKET_CONTEXT";
bbbd35bf 283 case STATE_EMIT_MSG_STREAM_BEGINNING:
e05f8464 284 return "EMIT_MSG_STREAM_BEGINNING";
bbbd35bf 285 case STATE_EMIT_MSG_PACKET_BEGINNING:
e05f8464 286 return "EMIT_MSG_PACKET_BEGINNING";
989925d1 287 case STATE_EMIT_MSG_DISCARDED_EVENTS:
e05f8464 288 return "EMIT_MSG_DISCARDED_EVENTS";
989925d1 289 case STATE_EMIT_MSG_DISCARDED_PACKETS:
e05f8464 290 return "EMIT_MSG_DISCARDED_PACKETS";
7b33a0e0 291 case STATE_DSCOPE_EVENT_HEADER_BEGIN:
e05f8464 292 return "DSCOPE_EVENT_HEADER_BEGIN";
7b33a0e0 293 case STATE_DSCOPE_EVENT_HEADER_CONTINUE:
e05f8464 294 return "DSCOPE_EVENT_HEADER_CONTINUE";
7b33a0e0 295 case STATE_AFTER_EVENT_HEADER:
e05f8464 296 return "AFTER_EVENT_HEADER";
7b33a0e0 297 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN:
e05f8464 298 return "DSCOPE_EVENT_COMMON_CONTEXT_BEGIN";
7b33a0e0 299 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE:
e05f8464 300 return "DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE";
7b33a0e0 301 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN:
e05f8464 302 return "DSCOPE_EVENT_SPEC_CONTEXT_BEGIN";
7b33a0e0 303 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE:
e05f8464 304 return "DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE";
fdf0e7a0 305 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN:
e05f8464 306 return "DSCOPE_EVENT_PAYLOAD_BEGIN";
fdf0e7a0 307 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE:
e05f8464 308 return "DSCOPE_EVENT_PAYLOAD_CONTINUE";
b09a5592 309 case STATE_EMIT_MSG_EVENT:
e05f8464 310 return "EMIT_MSG_EVENT";
fdf0e7a0 311 case STATE_SKIP_PACKET_PADDING:
e05f8464 312 return "SKIP_PACKET_PADDING";
bbbd35bf 313 case STATE_EMIT_MSG_PACKET_END_MULTI:
e05f8464 314 return "EMIT_MSG_PACKET_END_MULTI";
bbbd35bf 315 case STATE_EMIT_MSG_PACKET_END_SINGLE:
e05f8464 316 return "EMIT_MSG_PACKET_END_SINGLE";
bbbd35bf 317 case STATE_EMIT_MSG_STREAM_END:
e05f8464 318 return "EMIT_MSG_STREAM_END";
bbbd35bf 319 case STATE_DONE:
e05f8464 320 return "DONE";
fdf0e7a0
PP
321 default:
322 return "(unknown)";
323 }
324}
325
e98a2d6e 326static
b09a5592 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) {
e310e6e2 333 BT_COMP_LOGE_STR("Failed to allocate one stack.");
e98a2d6e
PP
334 goto error;
335 }
336
f8811e3b 337 stack->notit = notit;
87bdbbb0 338 stack->entries = g_array_new(FALSE, TRUE, sizeof(struct stack_entry));
e98a2d6e 339 if (!stack->entries) {
e310e6e2 340 BT_COMP_LOGE_STR("Failed to allocate a GArray.");
e98a2d6e
PP
341 goto error;
342 }
343
e310e6e2 344 BT_COMP_LOGD("Created stack: notit-addr=%p, stack-addr=%p", notit, stack);
7b33a0e0 345 goto end;
fdf0e7a0 346
e98a2d6e
PP
347error:
348 g_free(stack);
7b33a0e0
PP
349 stack = NULL;
350
351end:
352 return stack;
e98a2d6e
PP
353}
354
355static
356void stack_destroy(struct stack *stack)
357{
f8811e3b
PP
358 struct bt_msg_iter *notit;
359
8b45963b 360 BT_ASSERT(stack);
f8811e3b 361 notit = stack->notit;
e310e6e2 362 BT_COMP_LOGD("Destroying stack: addr=%p", stack);
87bdbbb0
PP
363
364 if (stack->entries) {
365 g_array_free(stack->entries, TRUE);
366 }
367
e98a2d6e
PP
368 g_free(stack);
369}
370
371static
8eee8ea2 372void stack_push(struct stack *stack, bt_field *base)
e98a2d6e 373{
e98a2d6e 374 struct stack_entry *entry;
f8811e3b 375 struct bt_msg_iter *notit;
e98a2d6e 376
8b45963b 377 BT_ASSERT(stack);
f8811e3b 378 notit = stack->notit;
8b45963b 379 BT_ASSERT(base);
c9ecaa78 380 BT_COMP_LOGT("Pushing base field on stack: stack-addr=%p, "
87bdbbb0
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
87bdbbb0 388 entry = &g_array_index(stack->entries, struct stack_entry, stack->size);
a6918753 389 entry->base = base;
87bdbbb0
PP
390 entry->index = 0;
391 stack->size++;
e98a2d6e
PP
392}
393
394static inline
395unsigned int stack_size(struct stack *stack)
396{
8b45963b 397 BT_ASSERT(stack);
87bdbbb0 398 return stack->size;
e98a2d6e
PP
399}
400
401static
402void stack_pop(struct stack *stack)
403{
f8811e3b
PP
404 struct bt_msg_iter *notit;
405
8b45963b
PP
406 BT_ASSERT(stack);
407 BT_ASSERT(stack_size(stack));
f8811e3b 408 notit = stack->notit;
c9ecaa78 409 BT_COMP_LOGT("Popping from stack: "
87bdbbb0
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{
8b45963b
PP
418 BT_ASSERT(stack);
419 BT_ASSERT(stack_size(stack));
87bdbbb0
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{
8b45963b 433 BT_ASSERT(stack);
87bdbbb0 434 stack->size = 0;
e98a2d6e
PP
435}
436
437static inline
b09a5592
PP
438enum bt_msg_iter_status msg_iter_status_from_m_status(
439 enum bt_msg_iter_medium_status m_status)
e98a2d6e 440{
7b33a0e0 441 /* They are the same */
dc77b521 442 return (int) m_status;
e98a2d6e
PP
443}
444
445static inline
b09a5592 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
b09a5592 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
b09a5592 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
b09a5592 464void buf_consume_bits(struct bt_msg_iter *notit, size_t incr)
e98a2d6e 465{
c9ecaa78 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
b09a5592
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;
b09a5592 477 enum bt_msg_iter_medium_status m_status;
e98a2d6e 478
e310e6e2 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);
e310e6e2 484 BT_COMP_LOGD("User function returned: status=%s, buf-addr=%p, buf-size=%zu",
b09a5592 485 bt_msg_iter_medium_status_string(m_status),
fdf0e7a0 486 buffer_addr, buffer_sz);
b09a5592 487 if (m_status == BT_MSG_ITER_MEDIUM_STATUS_OK) {
8b45963b 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;
bb8744a7 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
e310e6e2 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);
e310e6e2 507 BT_COMP_LOGD_MEM(buffer_addr, buffer_sz, "Returned bytes at %p:",
fdf0e7a0 508 buffer_addr);
b09a5592 509 } else if (m_status == BT_MSG_ITER_MEDIUM_STATUS_EOF) {
7116f6c0
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 */
7b33a0e0
PP
515 if (notit->cur_exp_packet_total_size >= 0) {
516 if (packet_at(notit) ==
517 notit->cur_exp_packet_total_size) {
42ef5ec2 518 goto end;
7116f6c0 519 }
42ef5ec2
PP
520 } else {
521 if (packet_at(notit) == 0) {
522 goto end;
7116f6c0 523 }
7116f6c0 524
42ef5ec2
PP
525 if (notit->buf.last_eh_at != SIZE_MAX &&
526 notit->buf.at == notit->buf.last_eh_at) {
527 goto end;
7116f6c0
PP
528 }
529 }
530
7116f6c0 531 /* All other states are invalid */
e310e6e2 532 BT_COMP_LOGW("User function returned %s, but message iterator is in an unexpected state: "
42ef5ec2
PP
533 "state=%s, cur-packet-size=%" PRId64 ", cur=%zu, "
534 "packet-cur=%zu, last-eh-at=%zu",
b09a5592 535 bt_msg_iter_medium_status_string(m_status),
42ef5ec2 536 state_string(notit->state),
7b33a0e0 537 notit->cur_exp_packet_total_size,
42ef5ec2
PP
538 notit->buf.at, packet_at(notit),
539 notit->buf.last_eh_at);
b09a5592 540 m_status = BT_MSG_ITER_MEDIUM_STATUS_ERROR;
fdf0e7a0 541 } else if (m_status < 0) {
e310e6e2 542 BT_COMP_LOGW("User function failed: status=%s",
b09a5592 543 bt_msg_iter_medium_status_string(m_status));
e98a2d6e
PP
544 }
545
42ef5ec2 546end:
b09a5592 547 return msg_iter_status_from_m_status(m_status);
e98a2d6e
PP
548}
549
550static inline
b09a5592
PP
551enum bt_msg_iter_status buf_ensure_available_bits(
552 struct bt_msg_iter *notit)
e98a2d6e 553{
b09a5592 554 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
e98a2d6e 555
85e7137b 556 if (G_UNLIKELY(buf_available_bits(notit) == 0)) {
e98a2d6e 557 /*
b09a5592 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
b09a5592
PP
568enum bt_msg_iter_status read_dscope_begin_state(
569 struct bt_msg_iter *notit,
939190b3 570 struct ctf_field_class *dscope_fc,
e98a2d6e 571 enum state done_state, enum state continue_state,
8eee8ea2 572 bt_field *dscope_field)
e98a2d6e 573{
b09a5592 574 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
939190b3 575 enum bt_bfcr_status bfcr_status;
e98a2d6e
PP
576 size_t consumed_bits;
577
e98a2d6e 578 notit->cur_dscope_field = dscope_field;
c9ecaa78 579 BT_COMP_LOGT("Starting BFCR: notit-addr=%p, bfcr-addr=%p, fc-addr=%p",
939190b3
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),
939190b3 583 notit->buf.sz, &bfcr_status);
c9ecaa78 584 BT_COMP_LOGT("BFCR consumed bits: size=%zu", consumed_bits);
e98a2d6e 585
939190b3
PP
586 switch (bfcr_status) {
587 case BT_BFCR_STATUS_OK:
588 /* Field class was read completely */
c9ecaa78 589 BT_COMP_LOGT_STR("Field was completely decoded.");
e98a2d6e
PP
590 notit->state = done_state;
591 break;
939190b3 592 case BT_BFCR_STATUS_EOF:
c9ecaa78 593 BT_COMP_LOGT_STR("BFCR needs more data to decode field completely.");
e98a2d6e
PP
594 notit->state = continue_state;
595 break;
596 default:
e310e6e2 597 BT_COMP_LOGW("BFCR failed to start: notit-addr=%p, bfcr-addr=%p, "
939190b3
PP
598 "status=%s", notit, notit->bfcr,
599 bt_bfcr_status_string(bfcr_status));
b09a5592 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
b09a5592
PP
612enum bt_msg_iter_status read_dscope_continue_state(
613 struct bt_msg_iter *notit, enum state done_state)
e98a2d6e 614{
b09a5592 615 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
939190b3 616 enum bt_bfcr_status bfcr_status;
e98a2d6e
PP
617 size_t consumed_bits;
618
c9ecaa78 619 BT_COMP_LOGT("Continuing BFCR: notit-addr=%p, bfcr-addr=%p",
939190b3 620 notit, notit->bfcr);
42ef5ec2 621
e98a2d6e 622 status = buf_ensure_available_bits(notit);
b09a5592 623 if (status != BT_MSG_ITER_STATUS_OK) {
fdf0e7a0 624 if (status < 0) {
e310e6e2 625 BT_COMP_LOGW("Cannot ensure that buffer has at least one byte: "
b09a5592
PP
626 "msg-addr=%p, status=%s",
627 notit, bt_msg_iter_status_string(status));
fdf0e7a0 628 } else {
c9ecaa78 629 BT_COMP_LOGT("Cannot ensure that buffer has at least one byte: "
b09a5592
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
939190b3
PP
637 consumed_bits = bt_bfcr_continue(notit->bfcr, notit->buf.addr,
638 notit->buf.sz, &bfcr_status);
c9ecaa78 639 BT_COMP_LOGT("BFCR consumed bits: size=%zu", consumed_bits);
e98a2d6e 640
939190b3
PP
641 switch (bfcr_status) {
642 case BT_BFCR_STATUS_OK:
78586d8a 643 /* Type was read completely. */
c9ecaa78 644 BT_COMP_LOGT_STR("Field was completely decoded.");
e98a2d6e
PP
645 notit->state = done_state;
646 break;
939190b3 647 case BT_BFCR_STATUS_EOF:
78586d8a 648 /* Stay in this continue state. */
c9ecaa78 649 BT_COMP_LOGT_STR("BFCR needs more data to decode field completely.");
e98a2d6e
PP
650 break;
651 default:
e310e6e2 652 BT_COMP_LOGW("BFCR failed to continue: notit-addr=%p, bfcr-addr=%p, "
939190b3
PP
653 "status=%s", notit, notit->bfcr,
654 bt_bfcr_status_string(bfcr_status));
b09a5592 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
b09a5592 666void release_event_dscopes(struct bt_msg_iter *notit)
e98a2d6e 667{
7b33a0e0
PP
668 notit->dscopes.event_common_context = NULL;
669 notit->dscopes.event_spec_context = NULL;
a6918753 670 notit->dscopes.event_payload = NULL;
e98a2d6e
PP
671}
672
673static
b09a5592 674void release_all_dscopes(struct bt_msg_iter *notit)
e98a2d6e 675{
a6918753
PP
676 notit->dscopes.stream_packet_context = NULL;
677
678 if (notit->packet_context_field) {
78cf9df6 679 bt_packet_context_field_release(notit->packet_context_field);
a6918753
PP
680 notit->packet_context_field = NULL;
681 }
682
683 release_event_dscopes(notit);
e98a2d6e
PP
684}
685
01ed875d
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
b09a5592
PP
752enum bt_msg_iter_status read_packet_header_begin_state(
753 struct bt_msg_iter *notit)
e98a2d6e 754{
939190b3 755 struct ctf_field_class *packet_header_fc = NULL;
b09a5592 756 enum bt_msg_iter_status ret = BT_MSG_ITER_STATUS_OK;
e98a2d6e 757
bbbd35bf
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;
b7cbc799 769 notit->state = STATE_CHECK_EMIT_MSG_STREAM_END;
bbbd35bf
PP
770 goto end;
771 default:
772 goto end;
773 }
774
10b7a2e4 775 /* Packet header class is common to the whole trace class. */
939190b3
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
7b33a0e0
PP
782 notit->cur_stream_class_id = -1;
783 notit->cur_event_class_id = -1;
784 notit->cur_data_stream_id = -1;
e310e6e2 785 BT_COMP_LOGD("Decoding packet header field:"
10b7a2e4
PP
786 "notit-addr=%p, trace-class-addr=%p, fc-addr=%p",
787 notit, notit->meta.tc, packet_header_fc);
939190b3 788 ret = read_dscope_begin_state(notit, packet_header_fc,
fdf0e7a0 789 STATE_AFTER_TRACE_PACKET_HEADER,
0476f6f7 790 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE, NULL);
fdf0e7a0 791 if (ret < 0) {
e310e6e2 792 BT_COMP_LOGW("Cannot decode packet header field: "
10b7a2e4
PP
793 "notit-addr=%p, trace-class-addr=%p, "
794 "fc-addr=%p",
795 notit, notit->meta.tc, packet_header_fc);
fdf0e7a0 796 }
b8418c69 797
e98a2d6e 798end:
2cf1d51e 799 return ret;
e98a2d6e
PP
800}
801
802static
b09a5592
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,
7b33a0e0 807 STATE_AFTER_TRACE_PACKET_HEADER);
5f870343
JG
808}
809
e98a2d6e 810static inline
b09a5592 811enum bt_msg_iter_status set_current_stream_class(struct bt_msg_iter *notit)
e98a2d6e 812{
b09a5592 813 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
7b33a0e0
PP
814 struct ctf_stream_class *new_stream_class = NULL;
815
816 if (notit->cur_stream_class_id == -1) {
fdf0e7a0 817 /*
7b33a0e0
PP
818 * No current stream class ID field, therefore only one
819 * stream class.
fdf0e7a0 820 */
7b33a0e0 821 if (notit->meta.tc->stream_classes->len != 1) {
e310e6e2 822 BT_COMP_LOGW("Need exactly one stream class since there's "
7b33a0e0 823 "no stream class ID field: "
10b7a2e4 824 "notit-addr=%p", notit);
b09a5592 825 status = BT_MSG_ITER_STATUS_ERROR;
7b33a0e0
PP
826 goto end;
827 }
e98a2d6e 828
7b33a0e0
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
7b33a0e0
PP
833 new_stream_class = ctf_trace_class_borrow_stream_class_by_id(
834 notit->meta.tc, notit->cur_stream_class_id);
c937c615 835 if (!new_stream_class) {
e310e6e2 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 ", "
10b7a2e4
PP
838 "trace-class-addr=%p",
839 notit, notit->cur_stream_class_id, notit->meta.tc);
b09a5592 840 status = BT_MSG_ITER_STATUS_ERROR;
e98a2d6e
PP
841 goto end;
842 }
843
7b33a0e0
PP
844 if (notit->meta.sc) {
845 if (new_stream_class != notit->meta.sc) {
e310e6e2 846 BT_COMP_LOGW("Two packets refer to two different stream classes within the same packet sequence: "
c937c615 847 "notit-addr=%p, prev-stream-class-addr=%p, "
c937c615
PP
848 "prev-stream-class-id=%" PRId64 ", "
849 "next-stream-class-addr=%p, "
c937c615 850 "next-stream-class-id=%" PRId64 ", "
10b7a2e4 851 "trace-addr=%p",
7b33a0e0
PP
852 notit, notit->meta.sc,
853 notit->meta.sc->id,
c937c615 854 new_stream_class,
7b33a0e0 855 new_stream_class->id,
10b7a2e4 856 notit->meta.tc);
b09a5592 857 status = BT_MSG_ITER_STATUS_ERROR;
c937c615
PP
858 goto end;
859 }
860 } else {
7b33a0e0 861 notit->meta.sc = new_stream_class;
c937c615
PP
862 }
863
e310e6e2 864 BT_COMP_LOGD("Set current stream class: "
fdf0e7a0 865 "notit-addr=%p, stream-class-addr=%p, "
7b33a0e0
PP
866 "stream-class-id=%" PRId64,
867 notit, notit->meta.sc, notit->meta.sc->id);
b8418c69 868
e98a2d6e 869end:
e98a2d6e
PP
870 return status;
871}
872
a6918753 873static inline
b09a5592 874enum bt_msg_iter_status set_current_stream(struct bt_msg_iter *notit)
a6918753 875{
b09a5592 876 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
8eee8ea2 877 bt_stream *stream = NULL;
a6918753 878
e310e6e2 879 BT_COMP_LOGD("Calling user function (get stream): notit-addr=%p, "
7b33a0e0
PP
880 "stream-class-addr=%p, stream-class-id=%" PRId64,
881 notit, notit->meta.sc,
882 notit->meta.sc->id);
4b70020d 883 stream = notit->medium.medops.borrow_stream(
7b33a0e0 884 notit->meta.sc->ir_sc, notit->cur_data_stream_id,
4b70020d 885 notit->medium.data);
8c6884d9 886 bt_stream_get_ref(stream);
e310e6e2 887 BT_COMP_LOGD("User function returned: stream-addr=%p", stream);
a6918753 888 if (!stream) {
e310e6e2 889 BT_COMP_LOGW_STR("User function failed to return a stream object "
7b33a0e0 890 "for the given stream class.");
b09a5592 891 status = BT_MSG_ITER_STATUS_ERROR;
a6918753
PP
892 goto end;
893 }
894
895 if (notit->stream && stream != notit->stream) {
e310e6e2 896 BT_COMP_LOGW("User function returned a different stream than the "
7b33a0e0 897 "previous one for the same sequence of packets.");
b09a5592 898 status = BT_MSG_ITER_STATUS_ERROR;
a6918753
PP
899 goto end;
900 }
901
8c6884d9 902 BT_STREAM_MOVE_REF(notit->stream, stream);
a6918753
PP
903
904end:
8c6884d9 905 bt_stream_put_ref(stream);
a6918753
PP
906 return status;
907}
908
909static inline
b09a5592 910enum bt_msg_iter_status set_current_packet(struct bt_msg_iter *notit)
a6918753 911{
b09a5592 912 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
8eee8ea2 913 bt_packet *packet = NULL;
a6918753 914
e310e6e2 915 BT_COMP_LOGD("Creating packet for packet message: "
a6918753 916 "notit-addr=%p", notit);
e310e6e2 917 BT_COMP_LOGD("Creating packet from stream: "
a6918753
PP
918 "notit-addr=%p, stream-addr=%p, "
919 "stream-class-addr=%p, "
a6918753 920 "stream-class-id=%" PRId64,
7b33a0e0
PP
921 notit, notit->stream, notit->meta.sc,
922 notit->meta.sc->id);
a6918753
PP
923
924 /* Create packet */
925 BT_ASSERT(notit->stream);
78cf9df6 926 packet = bt_packet_create(notit->stream);
a6918753 927 if (!packet) {
e310e6e2 928 BT_COMP_LOGE("Cannot create packet from stream: "
a6918753
PP
929 "notit-addr=%p, stream-addr=%p, "
930 "stream-class-addr=%p, "
a6918753 931 "stream-class-id=%" PRId64,
7b33a0e0
PP
932 notit, notit->stream, notit->meta.sc,
933 notit->meta.sc->id);
a6918753
PP
934 goto error;
935 }
936
937 goto end;
938
939error:
8c6884d9 940 BT_PACKET_PUT_REF_AND_RESET(packet);
b09a5592 941 status = BT_MSG_ITER_STATUS_ERROR;
a6918753
PP
942
943end:
8c6884d9 944 BT_PACKET_MOVE_REF(notit->packet, packet);
a6918753
PP
945 return status;
946}
947
e98a2d6e 948static
b09a5592
PP
949enum bt_msg_iter_status after_packet_header_state(
950 struct bt_msg_iter *notit)
e98a2d6e 951{
b09a5592 952 enum bt_msg_iter_status status;
e98a2d6e
PP
953
954 status = set_current_stream_class(notit);
b09a5592 955 if (status != BT_MSG_ITER_STATUS_OK) {
a6918753 956 goto end;
e98a2d6e
PP
957 }
958
a6918753
PP
959 notit->state = STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN;
960
961end:
e98a2d6e
PP
962 return status;
963}
964
965static
b09a5592
PP
966enum bt_msg_iter_status read_packet_context_begin_state(
967 struct bt_msg_iter *notit)
e98a2d6e 968{
b09a5592 969 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
939190b3 970 struct ctf_field_class *packet_context_fc;
e98a2d6e 971
7b33a0e0 972 BT_ASSERT(notit->meta.sc);
939190b3
PP
973 packet_context_fc = notit->meta.sc->packet_context_fc;
974 if (!packet_context_fc) {
e310e6e2 975 BT_COMP_LOGD("No packet packet context field class in stream class: continuing: "
fdf0e7a0 976 "notit-addr=%p, stream-class-addr=%p, "
7b33a0e0
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
a6918753 984 BT_ASSERT(!notit->packet_context_field);
7b33a0e0 985
8482a078 986 if (packet_context_fc->in_ir && !notit->dry_run) {
7b33a0e0
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
0476f6f7
PP
992 * able to return the packet context properties without
993 * creating a stream
994 * (bt_msg_iter_get_packet_properties()).
7b33a0e0
PP
995 */
996 notit->packet_context_field =
78cf9df6 997 bt_packet_context_field_create(
9e550e5f 998 notit->meta.sc->ir_sc);
7b33a0e0 999 if (!notit->packet_context_field) {
e310e6e2 1000 BT_COMP_LOGE_STR("Cannot create packet context field wrapper from stream class.");
b09a5592 1001 status = BT_MSG_ITER_STATUS_ERROR;
7b33a0e0
PP
1002 goto end;
1003 }
1004
1005 notit->dscopes.stream_packet_context =
78cf9df6 1006 bt_packet_context_field_borrow_field(
9e550e5f 1007 notit->packet_context_field);
7b33a0e0 1008 BT_ASSERT(notit->dscopes.stream_packet_context);
a6918753
PP
1009 }
1010
e310e6e2 1011 BT_COMP_LOGD("Decoding packet context field: "
fdf0e7a0 1012 "notit-addr=%p, stream-class-addr=%p, "
939190b3 1013 "stream-class-id=%" PRId64 ", fc-addr=%p",
7b33a0e0 1014 notit, notit->meta.sc,
939190b3
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,
a6918753 1019 notit->dscopes.stream_packet_context);
fdf0e7a0 1020 if (status < 0) {
e310e6e2 1021 BT_COMP_LOGW("Cannot decode packet context field: "
fdf0e7a0 1022 "notit-addr=%p, stream-class-addr=%p, "
939190b3 1023 "stream-class-id=%" PRId64 ", fc-addr=%p",
7b33a0e0
PP
1024 notit, notit->meta.sc,
1025 notit->meta.sc->id,
939190b3 1026 packet_context_fc);
fdf0e7a0 1027 }
e98a2d6e
PP
1028
1029end:
e98a2d6e
PP
1030 return status;
1031}
1032
1033static
b09a5592
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
b09a5592
PP
1042enum bt_msg_iter_status set_current_packet_content_sizes(
1043 struct bt_msg_iter *notit)
e98a2d6e 1044{
b09a5592 1045 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
e98a2d6e 1046
7b33a0e0
PP
1047 if (notit->cur_exp_packet_total_size == -1) {
1048 if (notit->cur_exp_packet_content_size != -1) {
ab03d3fb
PP
1049 notit->cur_exp_packet_total_size =
1050 notit->cur_exp_packet_content_size;
e98a2d6e 1051 }
e98a2d6e 1052 } else {
7b33a0e0
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
cf19c159
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));
ab03d3fb 1063
7b33a0e0
PP
1064 if (notit->cur_exp_packet_content_size >
1065 notit->cur_exp_packet_total_size) {
e310e6e2 1066 BT_COMP_LOGW("Invalid packet or content size: "
7b33a0e0 1067 "content size is greater than packet size: "
fdf0e7a0 1068 "notit-addr=%p, packet-context-field-addr=%p, "
7b33a0e0 1069 "packet-size=%" PRId64 ", content-size=%" PRId64,
fdf0e7a0 1070 notit, notit->dscopes.stream_packet_context,
7b33a0e0
PP
1071 notit->cur_exp_packet_total_size,
1072 notit->cur_exp_packet_content_size);
b09a5592 1073 status = BT_MSG_ITER_STATUS_ERROR;
fdf0e7a0
PP
1074 goto end;
1075 }
1076
e310e6e2 1077 BT_COMP_LOGD("Set current packet and content sizes: "
fdf0e7a0 1078 "notit-addr=%p, packet-size=%" PRIu64 ", content-size=%" PRIu64,
7b33a0e0
PP
1079 notit, notit->cur_exp_packet_total_size,
1080 notit->cur_exp_packet_content_size);
bbbd35bf 1081
e98a2d6e 1082end:
e98a2d6e
PP
1083 return status;
1084}
1085
1086static
bbbd35bf 1087enum bt_msg_iter_status after_packet_context_state(struct bt_msg_iter *notit)
e98a2d6e 1088{
b09a5592 1089 enum bt_msg_iter_status status;
e98a2d6e
PP
1090
1091 status = set_current_packet_content_sizes(notit);
b09a5592 1092 if (status != BT_MSG_ITER_STATUS_OK) {
8fc063a2
PP
1093 goto end;
1094 }
1095
bbbd35bf
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 */
989925d1 1102 notit->state = STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS;
8fc063a2 1103 } else {
989925d1 1104 notit->state = STATE_CHECK_EMIT_MSG_STREAM_BEGINNING;
e98a2d6e
PP
1105 }
1106
8fc063a2 1107end:
e98a2d6e
PP
1108 return status;
1109}
1110
1111static
bbbd35bf 1112enum bt_msg_iter_status read_event_header_begin_state(struct bt_msg_iter *notit)
e98a2d6e 1113{
b09a5592 1114 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
939190b3 1115 struct ctf_field_class *event_header_fc = NULL;
e98a2d6e 1116
9b876b7b
PP
1117 /* Reset the position of the last event header */
1118 notit->buf.last_eh_at = notit->buf.at;
7b33a0e0 1119 notit->cur_event_class_id = -1;
9b876b7b 1120
e98a2d6e 1121 /* Check if we have some content left */
7b33a0e0 1122 if (notit->cur_exp_packet_content_size >= 0) {
85e7137b 1123 if (G_UNLIKELY(packet_at(notit) ==
7b33a0e0 1124 notit->cur_exp_packet_content_size)) {
e98a2d6e 1125 /* No more events! */
e310e6e2 1126 BT_COMP_LOGD("Reached end of packet: notit-addr=%p, "
fdf0e7a0 1127 "cur=%zu", notit, packet_at(notit));
bbbd35bf 1128 notit->state = STATE_EMIT_MSG_PACKET_END_MULTI;
e98a2d6e 1129 goto end;
85e7137b 1130 } else if (G_UNLIKELY(packet_at(notit) >
7b33a0e0 1131 notit->cur_exp_packet_content_size)) {
e98a2d6e 1132 /* That's not supposed to happen */
e310e6e2 1133 BT_COMP_LOGD("Before decoding event header field: cursor is passed the packet's content: "
41342c71 1134 "notit-addr=%p, content-size=%" PRId64 ", "
7b33a0e0
PP
1135 "cur=%zu", notit,
1136 notit->cur_exp_packet_content_size,
fdf0e7a0 1137 packet_at(notit));
b09a5592 1138 status = BT_MSG_ITER_STATUS_ERROR;
e98a2d6e
PP
1139 goto end;
1140 }
7b33a0e0
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);
bbbd35bf
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:
7b33a0e0
PP
1155 goto end;
1156 }
e98a2d6e
PP
1157 }
1158
a6918753 1159 release_event_dscopes(notit);
7b33a0e0 1160 BT_ASSERT(notit->meta.sc);
939190b3
PP
1161 event_header_fc = notit->meta.sc->event_header_fc;
1162 if (!event_header_fc) {
7b33a0e0 1163 notit->state = STATE_AFTER_EVENT_HEADER;
e98a2d6e
PP
1164 goto end;
1165 }
1166
e310e6e2 1167 BT_COMP_LOGD("Decoding event header field: "
fdf0e7a0 1168 "notit-addr=%p, stream-class-addr=%p, "
7b33a0e0 1169 "stream-class-id=%" PRId64 ", "
939190b3 1170 "fc-addr=%p",
7b33a0e0
PP
1171 notit, notit->meta.sc,
1172 notit->meta.sc->id,
939190b3
PP
1173 event_header_fc);
1174 status = read_dscope_begin_state(notit, event_header_fc,
7b33a0e0 1175 STATE_AFTER_EVENT_HEADER,
0476f6f7 1176 STATE_DSCOPE_EVENT_HEADER_CONTINUE, NULL);
fdf0e7a0 1177 if (status < 0) {
e310e6e2 1178 BT_COMP_LOGW("Cannot decode event header field: "
fdf0e7a0 1179 "notit-addr=%p, stream-class-addr=%p, "
939190b3 1180 "stream-class-id=%" PRId64 ", fc-addr=%p",
7b33a0e0
PP
1181 notit, notit->meta.sc,
1182 notit->meta.sc->id,
939190b3 1183 event_header_fc);
fdf0e7a0 1184 }
e98a2d6e 1185
b8418c69 1186end:
e98a2d6e
PP
1187 return status;
1188}
1189
1190static
b09a5592
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,
7b33a0e0 1195 STATE_AFTER_EVENT_HEADER);
e98a2d6e
PP
1196}
1197
1198static inline
b09a5592 1199enum bt_msg_iter_status set_current_event_class(struct bt_msg_iter *notit)
e98a2d6e 1200{
b09a5592 1201 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
e98a2d6e 1202
7b33a0e0 1203 struct ctf_event_class *new_event_class = NULL;
e98a2d6e 1204
7b33a0e0 1205 if (notit->cur_event_class_id == -1) {
e98a2d6e 1206 /*
7b33a0e0
PP
1207 * No current event class ID field, therefore only one
1208 * event class.
e98a2d6e 1209 */
7b33a0e0 1210 if (notit->meta.sc->event_classes->len != 1) {
e310e6e2 1211 BT_COMP_LOGW("Need exactly one event class since there's "
7b33a0e0 1212 "no event class ID field: "
10b7a2e4 1213 "notit-addr=%p", notit);
b09a5592 1214 status = BT_MSG_ITER_STATUS_ERROR;
7b33a0e0 1215 goto end;
fdf0e7a0 1216 }
e98a2d6e 1217
7b33a0e0
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
7b33a0e0
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) {
e310e6e2 1225 BT_COMP_LOGW("No event class with ID of event class ID to use in stream class: "
7b33a0e0
PP
1226 "notit-addr=%p, stream-class-id=%" PRIu64 ", "
1227 "event-class-id=%" PRIu64 ", "
10b7a2e4 1228 "trace-class-addr=%p",
7b33a0e0 1229 notit, notit->meta.sc->id, notit->cur_event_class_id,
10b7a2e4 1230 notit->meta.tc);
b09a5592 1231 status = BT_MSG_ITER_STATUS_ERROR;
e98a2d6e
PP
1232 goto end;
1233 }
1234
7b33a0e0 1235 notit->meta.ec = new_event_class;
e310e6e2 1236 BT_COMP_LOGD("Set current event class: "
fdf0e7a0 1237 "notit-addr=%p, event-class-addr=%p, "
7b33a0e0
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
a6918753 1247static inline
b09a5592
PP
1248enum bt_msg_iter_status set_current_event_message(
1249 struct bt_msg_iter *notit)
a6918753 1250{
b09a5592
PP
1251 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
1252 bt_message *msg = NULL;
a6918753 1253
7b33a0e0 1254 BT_ASSERT(notit->meta.ec);
a6918753 1255 BT_ASSERT(notit->packet);
e310e6e2 1256 BT_COMP_LOGD("Creating event message from event class and packet: "
a6918753 1257 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", packet-addr=%p",
7b33a0e0
PP
1258 notit, notit->meta.ec,
1259 notit->meta.ec->name->str,
a6918753 1260 notit->packet);
b09a5592 1261 BT_ASSERT(notit->msg_iter);
9c04b633
PP
1262 BT_ASSERT(notit->meta.sc);
1263
1264 if (bt_stream_class_borrow_default_clock_class(notit->meta.sc->ir_sc)) {
37a93d41 1265 msg = bt_message_event_create_with_packet_and_default_clock_snapshot(
9c04b633
PP
1266 notit->msg_iter, notit->meta.ec->ir_ec,
1267 notit->packet, notit->default_clock_snapshot);
1268 } else {
37a93d41 1269 msg = bt_message_event_create_with_packet(notit->msg_iter,
9c04b633
PP
1270 notit->meta.ec->ir_ec, notit->packet);
1271 }
1272
b09a5592 1273 if (!msg) {
e310e6e2 1274 BT_COMP_LOGE("Cannot create event message: "
a6918753
PP
1275 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", "
1276 "packet-addr=%p",
7b33a0e0
PP
1277 notit, notit->meta.ec,
1278 notit->meta.ec->name->str,
a6918753
PP
1279 notit->packet);
1280 goto error;
1281 }
1282
1283 goto end;
1284
1285error:
b09a5592
PP
1286 BT_MESSAGE_PUT_REF_AND_RESET(msg);
1287 status = BT_MSG_ITER_STATUS_ERROR;
a6918753
PP
1288
1289end:
b09a5592 1290 BT_MESSAGE_MOVE_REF(notit->event_msg, msg);
a6918753
PP
1291 return status;
1292}
1293
e98a2d6e 1294static
b09a5592
PP
1295enum bt_msg_iter_status after_event_header_state(
1296 struct bt_msg_iter *notit)
e98a2d6e 1297{
b09a5592 1298 enum bt_msg_iter_status status;
e98a2d6e 1299
e98a2d6e 1300 status = set_current_event_class(notit);
b09a5592 1301 if (status != BT_MSG_ITER_STATUS_OK) {
e98a2d6e
PP
1302 goto end;
1303 }
1304
8482a078
FD
1305 if (G_UNLIKELY(notit->dry_run)) {
1306 goto next_state;
1307 }
1308
b09a5592
PP
1309 status = set_current_event_message(notit);
1310 if (status != BT_MSG_ITER_STATUS_OK) {
a6918753
PP
1311 goto end;
1312 }
1313
b09a5592
PP
1314 notit->event = bt_message_event_borrow_event(
1315 notit->event_msg);
a6918753 1316 BT_ASSERT(notit->event);
8482a078
FD
1317
1318next_state:
7b33a0e0 1319 notit->state = STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN;
e98a2d6e
PP
1320
1321end:
1322 return status;
1323}
1324
1325static
b09a5592
PP
1326enum bt_msg_iter_status read_event_common_context_begin_state(
1327 struct bt_msg_iter *notit)
e98a2d6e 1328{
b09a5592 1329 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
939190b3 1330 struct ctf_field_class *event_common_context_fc;
e98a2d6e 1331
939190b3
PP
1332 event_common_context_fc = notit->meta.sc->event_common_context_fc;
1333 if (!event_common_context_fc) {
7b33a0e0 1334 notit->state = STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN;
e98a2d6e
PP
1335 goto end;
1336 }
1337
8482a078 1338 if (event_common_context_fc->in_ir && !notit->dry_run) {
7b33a0e0
PP
1339 BT_ASSERT(!notit->dscopes.event_common_context);
1340 notit->dscopes.event_common_context =
78cf9df6 1341 bt_event_borrow_common_context_field(
9e550e5f 1342 notit->event);
7b33a0e0
PP
1343 BT_ASSERT(notit->dscopes.event_common_context);
1344 }
1345
c9ecaa78 1346 BT_COMP_LOGT("Decoding event common context field: "
fdf0e7a0 1347 "notit-addr=%p, stream-class-addr=%p, "
7b33a0e0 1348 "stream-class-id=%" PRId64 ", "
939190b3 1349 "fc-addr=%p",
7b33a0e0
PP
1350 notit, notit->meta.sc,
1351 notit->meta.sc->id,
939190b3
PP
1352 event_common_context_fc);
1353 status = read_dscope_begin_state(notit, event_common_context_fc,
7b33a0e0
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) {
e310e6e2 1358 BT_COMP_LOGW("Cannot decode event common context field: "
fdf0e7a0 1359 "notit-addr=%p, stream-class-addr=%p, "
939190b3 1360 "stream-class-id=%" PRId64 ", fc-addr=%p",
7b33a0e0
PP
1361 notit, notit->meta.sc,
1362 notit->meta.sc->id,
939190b3 1363 event_common_context_fc);
fdf0e7a0 1364 }
e98a2d6e
PP
1365
1366end:
e98a2d6e
PP
1367 return status;
1368}
1369
1370static
b09a5592
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,
7b33a0e0 1375 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN);
e98a2d6e
PP
1376}
1377
1378static
b09a5592
PP
1379enum bt_msg_iter_status read_event_spec_context_begin_state(
1380 struct bt_msg_iter *notit)
e98a2d6e 1381{
b09a5592 1382 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
939190b3 1383 struct ctf_field_class *event_spec_context_fc;
e98a2d6e 1384
939190b3
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
8482a078 1391 if (event_spec_context_fc->in_ir && !notit->dry_run) {
7b33a0e0 1392 BT_ASSERT(!notit->dscopes.event_spec_context);
9e550e5f 1393 notit->dscopes.event_spec_context =
78cf9df6 1394 bt_event_borrow_specific_context_field(
9e550e5f 1395 notit->event);
7b33a0e0
PP
1396 BT_ASSERT(notit->dscopes.event_spec_context);
1397 }
1398
c9ecaa78 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 ", "
939190b3 1402 "fc-addr=%p",
7b33a0e0
PP
1403 notit, notit->meta.ec,
1404 notit->meta.ec->name->str,
1405 notit->meta.ec->id,
939190b3
PP
1406 event_spec_context_fc);
1407 status = read_dscope_begin_state(notit, event_spec_context_fc,
e98a2d6e 1408 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN,
7b33a0e0
PP
1409 STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE,
1410 notit->dscopes.event_spec_context);
fdf0e7a0 1411 if (status < 0) {
e310e6e2 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\", "
939190b3 1415 "event-class-id=%" PRId64 ", fc-addr=%p",
7b33a0e0
PP
1416 notit, notit->meta.ec,
1417 notit->meta.ec->name->str,
1418 notit->meta.ec->id,
939190b3 1419 event_spec_context_fc);
fdf0e7a0 1420 }
e98a2d6e
PP
1421
1422end:
e98a2d6e
PP
1423 return status;
1424}
1425
1426static
b09a5592
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
b09a5592
PP
1435enum bt_msg_iter_status read_event_payload_begin_state(
1436 struct bt_msg_iter *notit)
e98a2d6e 1437{
b09a5592 1438 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
939190b3 1439 struct ctf_field_class *event_payload_fc;
e98a2d6e 1440
939190b3
PP
1441 event_payload_fc = notit->meta.ec->payload_fc;
1442 if (!event_payload_fc) {
b09a5592 1443 notit->state = STATE_EMIT_MSG_EVENT;
e98a2d6e
PP
1444 goto end;
1445 }
1446
8482a078 1447 if (event_payload_fc->in_ir && !notit->dry_run) {
7b33a0e0 1448 BT_ASSERT(!notit->dscopes.event_payload);
9e550e5f 1449 notit->dscopes.event_payload =
78cf9df6 1450 bt_event_borrow_payload_field(
9e550e5f 1451 notit->event);
7b33a0e0
PP
1452 BT_ASSERT(notit->dscopes.event_payload);
1453 }
1454
c9ecaa78 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 ", "
939190b3 1458 "fc-addr=%p",
7b33a0e0
PP
1459 notit, notit->meta.ec,
1460 notit->meta.ec->name->str,
1461 notit->meta.ec->id,
939190b3
PP
1462 event_payload_fc);
1463 status = read_dscope_begin_state(notit, event_payload_fc,
b09a5592 1464 STATE_EMIT_MSG_EVENT,
e98a2d6e 1465 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE,
a6918753 1466 notit->dscopes.event_payload);
fdf0e7a0 1467 if (status < 0) {
e310e6e2 1468 BT_COMP_LOGW("Cannot decode event payload field: "
fdf0e7a0
PP
1469 "notit-addr=%p, event-class-addr=%p, "
1470 "event-class-name=\"%s\", "
939190b3 1471 "event-class-id=%" PRId64 ", fc-addr=%p",
7b33a0e0
PP
1472 notit, notit->meta.ec,
1473 notit->meta.ec->name->str,
1474 notit->meta.ec->id,
939190b3 1475 event_payload_fc);
fdf0e7a0 1476 }
e98a2d6e
PP
1477
1478end:
e98a2d6e
PP
1479 return status;
1480}
1481
1482static
b09a5592
PP
1483enum bt_msg_iter_status read_event_payload_continue_state(
1484 struct bt_msg_iter *notit)
e98a2d6e 1485{
b09a5592 1486 return read_dscope_continue_state(notit, STATE_EMIT_MSG_EVENT);
e98a2d6e
PP
1487}
1488
1489static
bbbd35bf 1490enum bt_msg_iter_status skip_packet_padding_state(struct bt_msg_iter *notit)
e98a2d6e 1491{
b09a5592 1492 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
e98a2d6e 1493 size_t bits_to_skip;
01ed875d 1494 const enum state next_state = STATE_SWITCH_PACKET;
e98a2d6e 1495
7b33a0e0
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) {
01ed875d 1499 notit->state = next_state;
e98a2d6e
PP
1500 goto end;
1501 } else {
1502 size_t bits_to_consume;
fdf0e7a0 1503
e310e6e2 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);
b09a5592 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);
e310e6e2 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);
7b33a0e0
PP
1515 bits_to_skip = notit->cur_exp_packet_total_size -
1516 packet_at(notit);
e98a2d6e 1517 if (bits_to_skip == 0) {
01ed875d 1518 notit->state = next_state;
e98a2d6e
PP
1519 goto end;
1520 }
1521 }
1522
1523end:
1524 return status;
1525}
1526
989925d1
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
32476570
PP
1557 if (!notit->meta.sc->has_discarded_events) {
1558 notit->state = STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS;
1559 goto end;
1560 }
1561
989925d1
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
32476570 1589end:
989925d1
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
32476570
PP
1599 if (!notit->meta.sc->has_discarded_packets) {
1600 notit->state = STATE_EMIT_MSG_PACKET_BEGINNING;
1601 goto end;
1602 }
1603
989925d1
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
32476570 1631end:
989925d1
PP
1632 return BT_MSG_ITER_STATUS_OK;
1633}
1634
1635static
b7cbc799 1636enum bt_msg_iter_status check_emit_msg_stream_end(
989925d1
PP
1637 struct bt_msg_iter *notit)
1638{
1639 if (notit->emit_stream_end_msg) {
b7cbc799 1640 notit->state = STATE_EMIT_MSG_STREAM_END;
989925d1
PP
1641 } else {
1642 notit->state = STATE_DONE;
1643 }
1644
1645 return BT_MSG_ITER_STATUS_OK;
1646}
1647
e98a2d6e 1648static inline
b09a5592 1649enum bt_msg_iter_status handle_state(struct bt_msg_iter *notit)
e98a2d6e 1650{
b09a5592 1651 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
fdf0e7a0 1652 const enum state state = notit->state;
e98a2d6e 1653
c9ecaa78 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:
01ed875d
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;
989925d1
PP
1683 case STATE_CHECK_EMIT_MSG_STREAM_BEGINNING:
1684 status = check_emit_msg_stream_beginning_state(notit);
1685 break;
bbbd35bf 1686 case STATE_EMIT_MSG_STREAM_BEGINNING:
989925d1
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:
bbbd35bf 1699 notit->state = STATE_EMIT_MSG_PACKET_BEGINNING;
6ff151ad 1700 break;
bbbd35bf 1701 case STATE_EMIT_MSG_PACKET_BEGINNING:
7b33a0e0 1702 notit->state = STATE_DSCOPE_EVENT_HEADER_BEGIN;
e98a2d6e 1703 break;
7b33a0e0 1704 case STATE_DSCOPE_EVENT_HEADER_BEGIN:
e98a2d6e
PP
1705 status = read_event_header_begin_state(notit);
1706 break;
7b33a0e0 1707 case STATE_DSCOPE_EVENT_HEADER_CONTINUE:
e98a2d6e
PP
1708 status = read_event_header_continue_state(notit);
1709 break;
7b33a0e0 1710 case STATE_AFTER_EVENT_HEADER:
e98a2d6e
PP
1711 status = after_event_header_state(notit);
1712 break;
7b33a0e0
PP
1713 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN:
1714 status = read_event_common_context_begin_state(notit);
e98a2d6e 1715 break;
7b33a0e0
PP
1716 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE:
1717 status = read_event_common_context_continue_state(notit);
e98a2d6e 1718 break;
7b33a0e0
PP
1719 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN:
1720 status = read_event_spec_context_begin_state(notit);
e98a2d6e 1721 break;
7b33a0e0
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;
b09a5592 1731 case STATE_EMIT_MSG_EVENT:
7b33a0e0 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;
bbbd35bf 1737 case STATE_EMIT_MSG_PACKET_END_MULTI:
e98a2d6e
PP
1738 notit->state = STATE_SKIP_PACKET_PADDING;
1739 break;
bbbd35bf 1740 case STATE_EMIT_MSG_PACKET_END_SINGLE:
b7cbc799 1741 notit->state = STATE_CHECK_EMIT_MSG_STREAM_END;
989925d1 1742 break;
b7cbc799
SM
1743 case STATE_CHECK_EMIT_MSG_STREAM_END:
1744 status = check_emit_msg_stream_end(notit);
bbbd35bf
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:
e310e6e2 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
c9ecaa78 1757 BT_COMP_LOGT("Handled state: notit-addr=%p, status=%s, "
fdf0e7a0 1758 "prev-state=%s, cur-state=%s",
b09a5592 1759 notit, bt_msg_iter_status_string(status),
fdf0e7a0 1760 state_string(state), state_string(notit->state));
e98a2d6e
PP
1761 return status;
1762}
1763
6ff151ad 1764BT_HIDDEN
989925d1 1765void bt_msg_iter_reset_for_next_stream_file(struct bt_msg_iter *notit)
e98a2d6e 1766{
8b45963b 1767 BT_ASSERT(notit);
e310e6e2 1768 BT_COMP_LOGD("Resetting message iterator: addr=%p", notit);
e98a2d6e 1769 stack_clear(notit->stack);
7b33a0e0
PP
1770 notit->meta.sc = NULL;
1771 notit->meta.ec = NULL;
8c6884d9
PP
1772 BT_PACKET_PUT_REF_AND_RESET(notit->packet);
1773 BT_STREAM_PUT_REF_AND_RESET(notit->stream);
b09a5592 1774 BT_MESSAGE_PUT_REF_AND_RESET(notit->event_msg);
a6918753
PP
1775 release_all_dscopes(notit);
1776 notit->cur_dscope_field = NULL;
1777
a6918753 1778 if (notit->packet_context_field) {
78cf9df6 1779 bt_packet_context_field_release(notit->packet_context_field);
a6918753
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;
bb8744a7 1786 notit->buf.last_eh_at = SIZE_MAX;
e98a2d6e
PP
1787 notit->buf.packet_offset = 0;
1788 notit->state = STATE_INIT;
7b33a0e0
PP
1789 notit->cur_exp_packet_content_size = -1;
1790 notit->cur_exp_packet_total_size = -1;
bb230c9b 1791 notit->cur_packet_offset = -1;
7b33a0e0 1792 notit->cur_event_class_id = -1;
989925d1
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;
7b33a0e0 1805 notit->cur_data_stream_id = -1;
989925d1
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
b09a5592 1815bt_field *borrow_next_field(struct bt_msg_iter *notit)
e98a2d6e 1816{
8eee8ea2
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
8b45963b 1822 BT_ASSERT(!stack_empty(notit->stack));
e98a2d6e
PP
1823 index = stack_top(notit->stack)->index;
1824 base_field = stack_top(notit->stack)->base;
8b45963b 1825 BT_ASSERT(base_field);
78cf9df6 1826 base_fc = bt_field_borrow_class_const(base_field);
939190b3 1827 BT_ASSERT(base_fc);
e98a2d6e 1828
78cf9df6 1829 switch (bt_field_class_get_type(base_fc)) {
af0c18e3 1830 case BT_FIELD_CLASS_TYPE_STRUCTURE:
6ff151ad 1831 {
7b33a0e0 1832 BT_ASSERT(index <
939190b3 1833 bt_field_class_structure_get_member_count(
78cf9df6
PP
1834 bt_field_borrow_class_const(
1835 base_field)));
9e550e5f 1836 next_field =
78cf9df6 1837 bt_field_structure_borrow_member_field_by_index(
9e550e5f 1838 base_field, index);
e98a2d6e 1839 break;
6ff151ad 1840 }
af0c18e3
PP
1841 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
1842 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
78cf9df6
PP
1843 BT_ASSERT(index < bt_field_array_get_length(base_field));
1844 next_field = bt_field_array_borrow_element_field_by_index(
7b33a0e0 1845 base_field, index);
e98a2d6e 1846 break;
02b61fe0
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:
7b33a0e0 1850 BT_ASSERT(index == 0);
78cf9df6 1851 next_field = bt_field_variant_borrow_selected_option_field(
7b33a0e0 1852 base_field);
e98a2d6e
PP
1853 break;
1854 default:
0fbb9a9f 1855 abort();
e98a2d6e
PP
1856 }
1857
7b33a0e0 1858 BT_ASSERT(next_field);
e98a2d6e
PP
1859 return next_field;
1860}
1861
c44c3e70 1862static
b09a5592 1863void update_default_clock(struct bt_msg_iter *notit, uint64_t new_val,
7b33a0e0 1864 uint64_t new_val_size)
c44c3e70 1865{
7b33a0e0 1866 uint64_t new_val_mask;
c44c3e70 1867 uint64_t cur_value_masked;
c44c3e70 1868
7b33a0e0 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 */
7b33a0e0 1876 if (new_val_size == 64) {
ecbb78c0 1877 notit->default_clock_snapshot = new_val;
c44c3e70
JG
1878 goto end;
1879 }
1880
7b33a0e0 1881 new_val_mask = (1ULL << new_val_size) - 1;
ecbb78c0 1882 cur_value_masked = notit->default_clock_snapshot & new_val_mask;
c44c3e70 1883
7b33a0e0 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 */
ecbb78c0 1890 notit->default_clock_snapshot += new_val_mask + 1;
c44c3e70
JG
1891 }
1892
1893 /* Clear the low bits of the current clock value. */
ecbb78c0 1894 notit->default_clock_snapshot &= ~new_val_mask;
c44c3e70
JG
1895
1896 /* Set the low bits of the current clock value. */
ecbb78c0 1897 notit->default_clock_snapshot |= new_val;
b8418c69 1898
c44c3e70 1899end:
c9ecaa78 1900 BT_COMP_LOGT("Updated default clock's value from integer field's value: "
ecbb78c0 1901 "value=%" PRIu64, notit->default_clock_snapshot);
c44c3e70
JG
1902}
1903
1904static
939190b3
PP
1905enum bt_bfcr_status bfcr_unsigned_int_cb(uint64_t value,
1906 struct ctf_field_class *fc, void *data)
c44c3e70 1907{
b09a5592 1908 struct bt_msg_iter *notit = data;
939190b3 1909 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
8eee8ea2 1910 bt_field *field = NULL;
939190b3 1911 struct ctf_field_class_int *int_fc = (void *) fc;
c44c3e70 1912
c9ecaa78 1913 BT_COMP_LOGT("Unsigned integer function called from BFCR: "
939190b3 1914 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
af0c18e3
PP
1915 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64,
1916 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
a6918753 1917
85e7137b 1918 if (G_LIKELY(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE)) {
7b33a0e0 1919 goto update_def_clock;
c44c3e70
JG
1920 }
1921
939190b3
PP
1922 switch (int_fc->meaning) {
1923 case CTF_FIELD_CLASS_MEANING_EVENT_CLASS_ID:
7b33a0e0
PP
1924 notit->cur_event_class_id = value;
1925 break;
939190b3 1926 case CTF_FIELD_CLASS_MEANING_DATA_STREAM_ID:
7b33a0e0
PP
1927 notit->cur_data_stream_id = value;
1928 break;
939190b3 1929 case CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME:
7b33a0e0
PP
1930 notit->snapshots.beginning_clock = value;
1931 break;
939190b3 1932 case CTF_FIELD_CLASS_MEANING_PACKET_END_TIME:
7b33a0e0
PP
1933 notit->snapshots.end_clock = value;
1934 break;
939190b3 1935 case CTF_FIELD_CLASS_MEANING_STREAM_CLASS_ID:
7b33a0e0
PP
1936 notit->cur_stream_class_id = value;
1937 break;
939190b3 1938 case CTF_FIELD_CLASS_MEANING_MAGIC:
7b33a0e0 1939 if (value != 0xc1fc1fc1) {
e310e6e2 1940 BT_COMP_LOGW("Invalid CTF magic number: notit-addr=%p, "
7b33a0e0 1941 "magic=%" PRIx64, notit, value);
939190b3 1942 status = BT_BFCR_STATUS_ERROR;
c44c3e70
JG
1943 goto end;
1944 }
fdf0e7a0 1945
7b33a0e0 1946 break;
939190b3 1947 case CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT:
7b33a0e0
PP
1948 notit->snapshots.packets = value;
1949 break;
939190b3 1950 case CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT:
7b33a0e0
PP
1951 notit->snapshots.discarded_events = value;
1952 break;
939190b3 1953 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_TOTAL_SIZE:
7b33a0e0
PP
1954 notit->cur_exp_packet_total_size = value;
1955 break;
939190b3 1956 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_CONTENT_SIZE:
7b33a0e0
PP
1957 notit->cur_exp_packet_content_size = value;
1958 break;
1959 default:
1960 abort();
c44c3e70
JG
1961 }
1962
7b33a0e0 1963update_def_clock:
85e7137b 1964 if (G_UNLIKELY(int_fc->mapped_clock_class)) {
939190b3 1965 update_default_clock(notit, value, int_fc->base.size);
7b33a0e0 1966 }
c44c3e70 1967
85e7137b 1968 if (G_UNLIKELY(int_fc->storing_index >= 0)) {
7b33a0e0 1969 g_array_index(notit->stored_values, uint64_t,
939190b3 1970 (uint64_t) int_fc->storing_index) = value;
7b33a0e0 1971 }
e98a2d6e 1972
8482a078 1973 if (G_UNLIKELY(!fc->in_ir || notit->dry_run)) {
a6918753 1974 goto end;
e98a2d6e
PP
1975 }
1976
7b33a0e0
PP
1977 field = borrow_next_field(notit);
1978 BT_ASSERT(field);
78cf9df6
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);
60bbfc7c 1984 bt_field_integer_unsigned_set_value(field, value);
e98a2d6e 1985 stack_top(notit->stack)->index++;
fdf0e7a0 1986
a6918753 1987end:
e98a2d6e
PP
1988 return status;
1989}
1990
5f870343 1991static
939190b3
PP
1992enum bt_bfcr_status bfcr_unsigned_int_char_cb(uint64_t value,
1993 struct ctf_field_class *fc, void *data)
5f870343 1994{
7b33a0e0 1995 int ret;
b09a5592 1996 struct bt_msg_iter *notit = data;
939190b3 1997 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
8eee8ea2 1998 bt_field *string_field = NULL;
939190b3 1999 struct ctf_field_class_int *int_fc = (void *) fc;
7b33a0e0 2000 char str[2] = {'\0', '\0'};
5f870343 2001
c9ecaa78 2002 BT_COMP_LOGT("Unsigned integer character function called from BFCR: "
939190b3 2003 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
af0c18e3
PP
2004 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64,
2005 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
939190b3
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);
a6918753 2009
8482a078 2010 if (G_UNLIKELY(!fc->in_ir || notit->dry_run)) {
7b33a0e0
PP
2011 goto end;
2012 }
5f870343 2013
7b33a0e0 2014 if (notit->done_filling_string) {
5f870343
JG
2015 goto end;
2016 }
2017
7b33a0e0
PP
2018 if (value == 0) {
2019 notit->done_filling_string = true;
5f870343
JG
2020 goto end;
2021 }
2022
7b33a0e0 2023 string_field = stack_top(notit->stack)->base;
78cf9df6
PP
2024 BT_ASSERT(bt_field_get_class_type(string_field) ==
2025 BT_FIELD_CLASS_TYPE_STRING);
7b33a0e0
PP
2026
2027 /* Append character */
2028 str[0] = (char) value;
78cf9df6 2029 ret = bt_field_string_append_with_length(string_field, str, 1);
7b33a0e0 2030 if (ret) {
e310e6e2 2031 BT_COMP_LOGE("Cannot append character to string field's value: "
7b33a0e0
PP
2032 "notit-addr=%p, field-addr=%p, ret=%d",
2033 notit, string_field, ret);
939190b3 2034 status = BT_BFCR_STATUS_ERROR;
7b33a0e0
PP
2035 goto end;
2036 }
a6918753 2037
5f870343
JG
2038end:
2039 return status;
2040}
2041
2042static
939190b3
PP
2043enum bt_bfcr_status bfcr_signed_int_cb(int64_t value,
2044 struct ctf_field_class *fc, void *data)
e98a2d6e 2045{
939190b3 2046 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
8eee8ea2 2047 bt_field *field = NULL;
b09a5592 2048 struct bt_msg_iter *notit = data;
939190b3 2049 struct ctf_field_class_int *int_fc = (void *) fc;
e98a2d6e 2050
c9ecaa78 2051 BT_COMP_LOGT("Signed integer function called from BFCR: "
939190b3 2052 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
af0c18e3
PP
2053 "fc-type=%d, fc-in-ir=%d, value=%" PRId64,
2054 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
939190b3 2055 BT_ASSERT(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE);
7b33a0e0 2056
85e7137b 2057 if (G_UNLIKELY(int_fc->storing_index >= 0)) {
7b33a0e0 2058 g_array_index(notit->stored_values, uint64_t,
939190b3 2059 (uint64_t) int_fc->storing_index) = (uint64_t) value;
7b33a0e0
PP
2060 }
2061
8482a078 2062 if (G_UNLIKELY(!fc->in_ir || notit->dry_run)) {
a6918753 2063 goto end;
e98a2d6e
PP
2064 }
2065
7b33a0e0
PP
2066 field = borrow_next_field(notit);
2067 BT_ASSERT(field);
78cf9df6
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);
60bbfc7c 2073 bt_field_integer_signed_set_value(field, value);
e98a2d6e 2074 stack_top(notit->stack)->index++;
fdf0e7a0 2075
a6918753 2076end:
e98a2d6e
PP
2077 return status;
2078}
2079
2080static
939190b3
PP
2081enum bt_bfcr_status bfcr_floating_point_cb(double value,
2082 struct ctf_field_class *fc, void *data)
e98a2d6e 2083{
939190b3 2084 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
8eee8ea2 2085 bt_field *field = NULL;
b09a5592 2086 struct bt_msg_iter *notit = data;
e98a2d6e 2087
c9ecaa78 2088 BT_COMP_LOGT("Floating point number function called from BFCR: "
939190b3 2089 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
af0c18e3
PP
2090 "fc-type=%d, fc-in-ir=%d, value=%f",
2091 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
e5e71bf8 2092
8482a078 2093 if (G_UNLIKELY(!fc->in_ir || notit->dry_run)) {
e5e71bf8
PP
2094 goto end;
2095 }
2096
a6918753 2097 field = borrow_next_field(notit);
7b33a0e0 2098 BT_ASSERT(field);
78cf9df6
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++;
e5e71bf8
PP
2104
2105end:
e98a2d6e
PP
2106 return status;
2107}
2108
2109static
939190b3
PP
2110enum bt_bfcr_status bfcr_string_begin_cb(
2111 struct ctf_field_class *fc, void *data)
e98a2d6e 2112{
8eee8ea2 2113 bt_field *field = NULL;
b09a5592 2114 struct bt_msg_iter *notit = data;
e98a2d6e 2115
c9ecaa78 2116 BT_COMP_LOGT("String (beginning) function called from BFCR: "
939190b3 2117 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
af0c18e3
PP
2118 "fc-type=%d, fc-in-ir=%d",
2119 notit, notit->bfcr, fc, fc->type, fc->in_ir);
e98a2d6e 2120
8482a078 2121 if (G_UNLIKELY(!fc->in_ir || notit->dry_run)) {
e5e71bf8
PP
2122 goto end;
2123 }
2124
7b33a0e0
PP
2125 field = borrow_next_field(notit);
2126 BT_ASSERT(field);
78cf9df6
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);
fb25b9e3 2130 bt_field_string_clear(field);
a6918753 2131
e98a2d6e 2132 /*
939190b3
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 */
7b33a0e0 2137 stack_push(notit->stack, field);
e5e71bf8
PP
2138
2139end:
939190b3 2140 return BT_BFCR_STATUS_OK;
e98a2d6e
PP
2141}
2142
2143static
939190b3
PP
2144enum bt_bfcr_status bfcr_string_cb(const char *value,
2145 size_t len, struct ctf_field_class *fc, void *data)
e98a2d6e 2146{
939190b3 2147 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
8eee8ea2 2148 bt_field *field = NULL;
b09a5592 2149 struct bt_msg_iter *notit = data;
e98a2d6e
PP
2150 int ret;
2151
c9ecaa78 2152 BT_COMP_LOGT("String (substring) function called from BFCR: "
939190b3 2153 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
af0c18e3
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);
e5e71bf8 2157
8482a078 2158 if (G_UNLIKELY(!fc->in_ir || notit->dry_run)) {
e5e71bf8
PP
2159 goto end;
2160 }
2161
e98a2d6e 2162 field = stack_top(notit->stack)->base;
8b45963b 2163 BT_ASSERT(field);
e98a2d6e 2164
a6918753 2165 /* Append current substring */
78cf9df6 2166 ret = bt_field_string_append_with_length(field, value, len);
e98a2d6e 2167 if (ret) {
e310e6e2 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);
939190b3 2171 status = BT_BFCR_STATUS_ERROR;
e98a2d6e
PP
2172 goto end;
2173 }
2174
2175end:
2176 return status;
2177}
2178
2179static
939190b3
PP
2180enum bt_bfcr_status bfcr_string_end_cb(
2181 struct ctf_field_class *fc, void *data)
e98a2d6e 2182{
b09a5592 2183 struct bt_msg_iter *notit = data;
e98a2d6e 2184
c9ecaa78 2185 BT_COMP_LOGT("String (end) function called from BFCR: "
939190b3 2186 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
af0c18e3
PP
2187 "fc-type=%d, fc-in-ir=%d",
2188 notit, notit->bfcr, fc, fc->type, fc->in_ir);
e5e71bf8 2189
8482a078 2190 if (G_UNLIKELY(!fc->in_ir || notit->dry_run)) {
e5e71bf8
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++;
e5e71bf8
PP
2199
2200end:
939190b3 2201 return BT_BFCR_STATUS_OK;
e98a2d6e
PP
2202}
2203
939190b3
PP
2204enum bt_bfcr_status bfcr_compound_begin_cb(
2205 struct ctf_field_class *fc, void *data)
e98a2d6e 2206{
b09a5592 2207 struct bt_msg_iter *notit = data;
8eee8ea2 2208 bt_field *field;
e98a2d6e 2209
c9ecaa78 2210 BT_COMP_LOGT("Compound (beginning) function called from BFCR: "
939190b3 2211 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
af0c18e3
PP
2212 "fc-type=%d, fc-in-ir=%d",
2213 notit, notit->bfcr, fc, fc->type, fc->in_ir);
7b33a0e0 2214
8482a078 2215 if (G_UNLIKELY(!fc->in_ir || notit->dry_run)) {
7b33a0e0
PP
2216 goto end;
2217 }
fdf0e7a0 2218
a6918753 2219 /* Borrow field */
e98a2d6e 2220 if (stack_empty(notit->stack)) {
a6918753
PP
2221 /* Root: already set by read_dscope_begin_state() */
2222 field = notit->cur_dscope_field;
e98a2d6e 2223 } else {
a6918753 2224 field = borrow_next_field(notit);
7b33a0e0 2225 BT_ASSERT(field);
e98a2d6e
PP
2226 }
2227
2228 /* Push field */
8b45963b 2229 BT_ASSERT(field);
78cf9df6 2230 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
7b33a0e0
PP
2231 stack_push(notit->stack, field);
2232
2233 /*
939190b3 2234 * Change BFCR "unsigned int" callback if it's a text
7b33a0e0
PP
2235 * array/sequence.
2236 */
af0c18e3
PP
2237 if (fc->type == CTF_FIELD_CLASS_TYPE_ARRAY ||
2238 fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE) {
939190b3 2239 struct ctf_field_class_array_base *array_fc = (void *) fc;
7b33a0e0 2240
939190b3 2241 if (array_fc->is_text) {
78cf9df6
PP
2242 BT_ASSERT(bt_field_get_class_type(field) ==
2243 BT_FIELD_CLASS_TYPE_STRING);
7b33a0e0 2244 notit->done_filling_string = false;
fb25b9e3 2245 bt_field_string_clear(field);
939190b3
PP
2246 bt_bfcr_set_unsigned_int_cb(notit->bfcr,
2247 bfcr_unsigned_int_char_cb);
7b33a0e0 2248 }
e98a2d6e
PP
2249 }
2250
2251end:
939190b3 2252 return BT_BFCR_STATUS_OK;
e98a2d6e
PP
2253}
2254
939190b3
PP
2255enum bt_bfcr_status bfcr_compound_end_cb(
2256 struct ctf_field_class *fc, void *data)
e98a2d6e 2257{
b09a5592 2258 struct bt_msg_iter *notit = data;
e98a2d6e 2259
c9ecaa78 2260 BT_COMP_LOGT("Compound (end) function called from BFCR: "
939190b3 2261 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
af0c18e3
PP
2262 "fc-type=%d, fc-in-ir=%d",
2263 notit, notit->bfcr, fc, fc->type, fc->in_ir);
e98a2d6e 2264
8482a078 2265 if (G_UNLIKELY(!fc->in_ir || notit->dry_run)) {
e98a2d6e
PP
2266 goto end;
2267 }
2268
7b33a0e0 2269 BT_ASSERT(!stack_empty(notit->stack));
78cf9df6 2270 BT_ASSERT(bt_field_borrow_class_const(stack_top(notit->stack)->base) ==
939190b3 2271 fc->ir_fc);
e98a2d6e 2272
7b33a0e0 2273 /*
939190b3 2274 * Reset BFCR "unsigned int" callback if it's a text
7b33a0e0
PP
2275 * array/sequence.
2276 */
af0c18e3
PP
2277 if (fc->type == CTF_FIELD_CLASS_TYPE_ARRAY ||
2278 fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE) {
939190b3 2279 struct ctf_field_class_array_base *array_fc = (void *) fc;
7b33a0e0 2280
939190b3 2281 if (array_fc->is_text) {
af0c18e3 2282 BT_ASSERT(bt_field_get_class_type(
78cf9df6 2283 stack_top(notit->stack)->base) ==
9e550e5f 2284 BT_FIELD_CLASS_TYPE_STRING);
939190b3
PP
2285 bt_bfcr_set_unsigned_int_cb(notit->bfcr,
2286 bfcr_unsigned_int_cb);
e98a2d6e 2287 }
7b33a0e0 2288 }
e98a2d6e 2289
7b33a0e0
PP
2290 /* Pop stack */
2291 stack_pop(notit->stack);
e98a2d6e 2292
7b33a0e0
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:
939190b3 2299 return BT_BFCR_STATUS_OK;
e98a2d6e
PP
2300}
2301
2302static
939190b3 2303int64_t bfcr_get_sequence_length_cb(struct ctf_field_class *fc, void *data)
e98a2d6e 2304{
8eee8ea2 2305 bt_field *seq_field;
b09a5592 2306 struct bt_msg_iter *notit = data;
939190b3 2307 struct ctf_field_class_sequence *seq_fc = (void *) fc;
7b33a0e0
PP
2308 int64_t length = -1;
2309 int ret;
e98a2d6e 2310
7b33a0e0 2311 length = (uint64_t) g_array_index(notit->stored_values, uint64_t,
939190b3 2312 seq_fc->stored_length_index);
8482a078
FD
2313
2314 if (G_UNLIKELY(notit->dry_run)){
2315 goto end;
2316 }
2317
fdf0e7a0 2318 seq_field = stack_top(notit->stack)->base;
7b33a0e0 2319 BT_ASSERT(seq_field);
c16748c7
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);
60bbfc7c 2330 ret = bt_field_array_dynamic_set_length(seq_field,
c16748c7
PP
2331 (uint64_t) length);
2332 if (ret) {
e310e6e2 2333 BT_COMP_LOGE("Cannot set dynamic array field's length field: "
c16748c7
PP
2334 "notit-addr=%p, field-addr=%p, "
2335 "length=%" PRIu64, notit, seq_field, length);
2336 }
2cf1d51e 2337 }
fdf0e7a0 2338
8482a078 2339end:
7b33a0e0 2340 return length;
e98a2d6e
PP
2341}
2342
2343static
939190b3
PP
2344struct ctf_field_class *bfcr_borrow_variant_selected_field_class_cb(
2345 struct ctf_field_class *fc, void *data)
e98a2d6e 2346{
a6918753 2347 int ret;
7b33a0e0
PP
2348 uint64_t i;
2349 int64_t option_index = -1;
b09a5592 2350 struct bt_msg_iter *notit = data;
939190b3
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;
7b33a0e0
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,
939190b3 2361 var_fc->stored_tag_index);
e98a2d6e
PP
2362
2363 /*
7b33a0e0 2364 * Check each range to find the selected option's index.
e98a2d6e 2365 */
939190b3
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);
7b33a0e0
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 }
a6918753 2378 } else {
939190b3
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);
7b33a0e0
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 }
a6918753
PP
2390 }
2391
7b33a0e0 2392 if (option_index < 0) {
e310e6e2 2393 BT_COMP_LOGW("Cannot find variant field class's option: "
939190b3
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
939190b3
PP
2399 selected_option = ctf_field_class_variant_borrow_option_by_index(
2400 var_fc, (uint64_t) option_index);
e98a2d6e 2401
8482a078 2402 if (selected_option->fc->in_ir && !notit->dry_run) {
8eee8ea2 2403 bt_field *var_field = stack_top(notit->stack)->base;
1556a1af 2404
02b61fe0 2405 ret = bt_field_variant_select_option_field_by_index(
9e550e5f 2406 var_field, option_index);
8fc063a2 2407 if (ret) {
e310e6e2 2408 BT_COMP_LOGW("Cannot select variant field's option field: "
7b33a0e0
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 }
5184a1d6
PP
2414 }
2415
939190b3 2416 ret_fc = selected_option->fc;
8fc063a2 2417
5184a1d6 2418end:
939190b3 2419 return ret_fc;
7b33a0e0
PP
2420}
2421
6ff151ad 2422static
bbbd35bf 2423void create_msg_stream_beginning(struct bt_msg_iter *notit,
b09a5592 2424 bt_message **message)
6ff151ad 2425{
b09a5592 2426 bt_message *ret = NULL;
6ff151ad 2427
6ff151ad 2428 BT_ASSERT(notit->stream);
b09a5592
PP
2429 BT_ASSERT(notit->msg_iter);
2430 ret = bt_message_stream_beginning_create(notit->msg_iter,
03e18d5d 2431 notit->stream);
6ff151ad 2432 if (!ret) {
e310e6e2 2433 BT_COMP_LOGE("Cannot create stream beginning message: "
6ff151ad
PP
2434 "notit-addr=%p, stream-addr=%p",
2435 notit, notit->stream);
2436 return;
2437 }
2438
b09a5592 2439 *message = ret;
6ff151ad
PP
2440}
2441
bbbd35bf
PP
2442static
2443void create_msg_stream_end(struct bt_msg_iter *notit, bt_message **message)
6ff151ad 2444{
b09a5592 2445 bt_message *ret;
6ff151ad
PP
2446
2447 if (!notit->stream) {
e310e6e2 2448 BT_COMP_LOGE("Cannot create stream for stream message: "
6ff151ad
PP
2449 "notit-addr=%p", notit);
2450 return;
2451 }
2452
b09a5592
PP
2453 BT_ASSERT(notit->msg_iter);
2454 ret = bt_message_stream_end_create(notit->msg_iter,
03e18d5d 2455 notit->stream);
6ff151ad 2456 if (!ret) {
e310e6e2 2457 BT_COMP_LOGE("Cannot create stream end message: "
6ff151ad
PP
2458 "notit-addr=%p, stream-addr=%p",
2459 notit, notit->stream);
2460 return;
2461 }
bbbd35bf 2462
b09a5592 2463 *message = ret;
6ff151ad
PP
2464}
2465
78586d8a 2466static
bbbd35bf 2467void create_msg_packet_beginning(struct bt_msg_iter *notit,
b09a5592 2468 bt_message **message)
e98a2d6e 2469{
a6918753 2470 int ret;
b09a5592
PP
2471 enum bt_msg_iter_status status;
2472 bt_message *msg = NULL;
8eee8ea2 2473 const bt_stream_class *sc;
e98a2d6e 2474
a6918753 2475 status = set_current_packet(notit);
b09a5592 2476 if (status != BT_MSG_ITER_STATUS_OK) {
a6918753 2477 goto end;
e98a2d6e
PP
2478 }
2479
a6918753 2480 BT_ASSERT(notit->packet);
78cf9df6 2481 sc = notit->meta.sc->ir_sc;
7b33a0e0
PP
2482 BT_ASSERT(sc);
2483
a6918753 2484 if (notit->packet_context_field) {
78cf9df6 2485 ret = bt_packet_move_context_field(
9e550e5f 2486 notit->packet, notit->packet_context_field);
a6918753
PP
2487 if (ret) {
2488 goto end;
2489 }
2490
2491 notit->packet_context_field = NULL;
2492
2493 /*
0476f6f7
PP
2494 * At this point notit->dscopes.stream_packet_context
2495 * has the same value as the packet context field within
a6918753
PP
2496 * notit->packet.
2497 */
78cf9df6 2498 BT_ASSERT(bt_packet_borrow_context_field(
9e550e5f 2499 notit->packet) ==
a6918753
PP
2500 notit->dscopes.stream_packet_context);
2501 }
2502
b09a5592 2503 BT_ASSERT(notit->msg_iter);
96a0a69d 2504
32476570
PP
2505 if (notit->meta.sc->packets_have_ts_begin) {
2506 BT_ASSERT(notit->snapshots.beginning_clock != UINT64_C(-1));
92f86a9e 2507 msg = bt_message_packet_beginning_create_with_default_clock_snapshot(
32476570
PP
2508 notit->msg_iter, notit->packet,
2509 notit->snapshots.beginning_clock);
92f86a9e 2510 } else {
96a0a69d
PP
2511 msg = bt_message_packet_beginning_create(notit->msg_iter,
2512 notit->packet);
96a0a69d
PP
2513 }
2514
b09a5592 2515 if (!msg) {
e310e6e2 2516 BT_COMP_LOGE("Cannot create packet beginning message: "
fdf0e7a0
PP
2517 "notit-addr=%p, packet-addr=%p",
2518 notit, notit->packet);
8fc063a2 2519 goto end;
78586d8a 2520 }
a6918753 2521
b09a5592 2522 *message = msg;
8fc063a2
PP
2523
2524end:
2525 return;
e98a2d6e
PP
2526}
2527
78586d8a 2528static
bbbd35bf 2529void create_msg_packet_end(struct bt_msg_iter *notit, bt_message **message)
e98a2d6e 2530{
b09a5592 2531 bt_message *msg;
6069b85e 2532 bool update_default_cs = true;
e98a2d6e 2533
e98a2d6e 2534 if (!notit->packet) {
78586d8a 2535 return;
e98a2d6e
PP
2536 }
2537
6069b85e
FD
2538 /* Check if may be affected by lttng-crash timestamp_end quirk. */
2539 if (G_UNLIKELY(notit->meta.tc->quirks.lttng_crash)) {
2540 /*
2541 * Check if the `timestamp_begin` field is non-zero but
2542 * `timestamp_end` is zero. It means the trace is affected by
2543 * the lttng-crash packet `timestamp_end` quirk and must be
2544 * fixed up by omitting to update the default clock snapshot to
2545 * the `timestamp_end` as is typically done.
2546 */
2547 if (notit->snapshots.beginning_clock != 0 &&
2548 notit->snapshots.end_clock == 0) {
2549 update_default_cs = false;
2550 }
2551 }
2552
1a8205fb
FD
2553 /*
2554 * Check if may be affected by lttng event-after-packet `timestamp_end`
2555 * quirk.
2556 */
2557 if (notit->meta.tc->quirks.lttng_event_after_packet) {
2558 /*
2559 * Check if `timestamp_end` is smaller then the current
2560 * default_clock_snapshot (which is set to the last event
2561 * decoded). It means the trace is affected by the lttng
2562 * `event-after-packet` packet `timestamp_end` quirk and must
2563 * be fixed up by omitting to update the default clock snapshot
2564 * to the `timestamp_end` as is typically done.
2565 */
2566 if (notit->snapshots.end_clock < notit->default_clock_snapshot) {
2567 update_default_cs = false;
2568 }
2569 }
2570
6069b85e
FD
2571 /* Update default clock from packet's end time. */
2572 if (notit->snapshots.end_clock != UINT64_C(-1) && update_default_cs) {
ecbb78c0 2573 notit->default_clock_snapshot = notit->snapshots.end_clock;
7b33a0e0
PP
2574 }
2575
b09a5592 2576 BT_ASSERT(notit->msg_iter);
96a0a69d 2577
32476570 2578 if (notit->meta.sc->packets_have_ts_end) {
92f86a9e 2579 BT_ASSERT(notit->snapshots.end_clock != UINT64_C(-1));
96a0a69d
PP
2580 msg = bt_message_packet_end_create_with_default_clock_snapshot(
2581 notit->msg_iter, notit->packet,
6069b85e 2582 notit->default_clock_snapshot);
92f86a9e
PP
2583 } else {
2584 msg = bt_message_packet_end_create(notit->msg_iter,
2585 notit->packet);
96a0a69d
PP
2586 }
2587
b09a5592 2588 if (!msg) {
e310e6e2 2589 BT_COMP_LOGE("Cannot create packet end message: "
fdf0e7a0
PP
2590 "notit-addr=%p, packet-addr=%p",
2591 notit, notit->packet);
78586d8a 2592 return;
e5815ba2 2593
78586d8a 2594 }
e98a2d6e 2595
8c6884d9 2596 BT_PACKET_PUT_REF_AND_RESET(notit->packet);
b09a5592 2597 *message = msg;
e98a2d6e
PP
2598}
2599
989925d1
PP
2600static
2601void create_msg_discarded_events(struct bt_msg_iter *notit,
2602 bt_message **message)
2603{
2604 bt_message *msg;
2605 uint64_t beginning_raw_value = UINT64_C(-1);
2606 uint64_t end_raw_value = UINT64_C(-1);
989925d1
PP
2607
2608 BT_ASSERT(notit->msg_iter);
2609 BT_ASSERT(notit->stream);
32476570 2610 BT_ASSERT(notit->meta.sc->has_discarded_events);
989925d1 2611
32476570
PP
2612 if (notit->meta.sc->discarded_events_have_default_cs) {
2613 if (notit->prev_packet_snapshots.discarded_events == UINT64_C(-1)) {
2614 /*
2615 * We discarded events, but before (and possibly
2616 * including) the current packet: use this packet's time
2617 * range, and do not have a specific count.
2618 */
2619 beginning_raw_value = notit->snapshots.beginning_clock;
2620 end_raw_value = notit->snapshots.end_clock;
2621 } else {
2622 beginning_raw_value = notit->prev_packet_snapshots.end_clock;
2623 end_raw_value = notit->snapshots.end_clock;
2624 }
989925d1 2625
32476570
PP
2626 BT_ASSERT(beginning_raw_value != UINT64_C(-1));
2627 BT_ASSERT(end_raw_value != UINT64_C(-1));
989925d1
PP
2628 msg = bt_message_discarded_events_create_with_default_clock_snapshots(
2629 notit->msg_iter, notit->stream, beginning_raw_value,
2630 end_raw_value);
2631 } else {
2632 msg = bt_message_discarded_events_create(notit->msg_iter,
2633 notit->stream);
2634 }
2635
2636 if (!msg) {
e310e6e2 2637 BT_COMP_LOGE("Cannot create discarded events message: "
989925d1
PP
2638 "notit-addr=%p, stream-addr=%p",
2639 notit, notit->stream);
2640 return;
2641 }
2642
32476570
PP
2643 if (notit->prev_packet_snapshots.discarded_events != UINT64_C(-1)) {
2644 bt_message_discarded_events_set_count(msg,
2645 notit->snapshots.discarded_events -
2646 notit->prev_packet_snapshots.discarded_events);
989925d1
PP
2647 }
2648
2649 *message = msg;
2650}
2651
2652static
2653void create_msg_discarded_packets(struct bt_msg_iter *notit,
2654 bt_message **message)
2655{
2656 bt_message *msg;
2657
2658 BT_ASSERT(notit->msg_iter);
2659 BT_ASSERT(notit->stream);
32476570 2660 BT_ASSERT(notit->meta.sc->has_discarded_packets);
989925d1
PP
2661 BT_ASSERT(notit->prev_packet_snapshots.packets !=
2662 UINT64_C(-1));
2663
32476570
PP
2664 if (notit->meta.sc->discarded_packets_have_default_cs) {
2665 BT_ASSERT(notit->prev_packet_snapshots.end_clock != UINT64_C(-1));
2666 BT_ASSERT(notit->snapshots.beginning_clock != UINT64_C(-1));
989925d1
PP
2667 msg = bt_message_discarded_packets_create_with_default_clock_snapshots(
2668 notit->msg_iter, notit->stream,
2669 notit->prev_packet_snapshots.end_clock,
2670 notit->snapshots.beginning_clock);
2671 } else {
2672 msg = bt_message_discarded_packets_create(notit->msg_iter,
2673 notit->stream);
2674 }
2675
2676 if (!msg) {
e310e6e2 2677 BT_COMP_LOGE("Cannot create discarded packets message: "
989925d1
PP
2678 "notit-addr=%p, stream-addr=%p",
2679 notit, notit->stream);
2680 return;
2681 }
2682
2683 bt_message_discarded_packets_set_count(msg,
2684 notit->snapshots.packets -
2685 notit->prev_packet_snapshots.packets - 1);
2686 *message = msg;
2687}
2688
c44c3e70 2689BT_HIDDEN
b09a5592 2690struct bt_msg_iter *bt_msg_iter_create(struct ctf_trace_class *tc,
e98a2d6e 2691 size_t max_request_sz,
f8811e3b 2692 struct bt_msg_iter_medium_ops medops, void *data,
e310e6e2 2693 bt_logging_level log_level, bt_self_component *self_comp)
e98a2d6e 2694{
b09a5592 2695 struct bt_msg_iter *notit = NULL;
939190b3
PP
2696 struct bt_bfcr_cbs cbs = {
2697 .classes = {
2698 .signed_int = bfcr_signed_int_cb,
2699 .unsigned_int = bfcr_unsigned_int_cb,
2700 .floating_point = bfcr_floating_point_cb,
2701 .string_begin = bfcr_string_begin_cb,
2702 .string = bfcr_string_cb,
2703 .string_end = bfcr_string_end_cb,
2704 .compound_begin = bfcr_compound_begin_cb,
2705 .compound_end = bfcr_compound_end_cb,
e98a2d6e
PP
2706 },
2707 .query = {
939190b3
PP
2708 .get_sequence_length = bfcr_get_sequence_length_cb,
2709 .borrow_variant_selected_field_class = bfcr_borrow_variant_selected_field_class_cb,
e98a2d6e
PP
2710 },
2711 };
2712
7b33a0e0 2713 BT_ASSERT(tc);
8b45963b 2714 BT_ASSERT(medops.request_bytes);
a6918753 2715 BT_ASSERT(medops.borrow_stream);
e310e6e2 2716 BT_COMP_LOG_CUR_LVL(BT_LOG_DEBUG, log_level, self_comp,
f8811e3b 2717 "Creating CTF plugin message iterator: "
10b7a2e4 2718 "trace-addr=%p, max-request-size=%zu, "
f8811e3b
PP
2719 "data=%p, log-level=%s", tc, max_request_sz, data,
2720 bt_common_logging_level_string(log_level));
b09a5592 2721 notit = g_new0(struct bt_msg_iter, 1);
e98a2d6e 2722 if (!notit) {
e310e6e2 2723 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp,
f8811e3b 2724 "Failed to allocate one CTF plugin message iterator.");
e98a2d6e
PP
2725 goto end;
2726 }
e310e6e2 2727 notit->self_comp = self_comp;
f8811e3b 2728 notit->log_level = log_level;
7b33a0e0 2729 notit->meta.tc = tc;
e98a2d6e
PP
2730 notit->medium.medops = medops;
2731 notit->medium.max_request_sz = max_request_sz;
2732 notit->medium.data = data;
e98a2d6e 2733 notit->stack = stack_new(notit);
7b33a0e0
PP
2734 notit->stored_values = g_array_new(FALSE, TRUE, sizeof(uint64_t));
2735 g_array_set_size(notit->stored_values, tc->stored_value_count);
2736
e98a2d6e 2737 if (!notit->stack) {
e310e6e2 2738 BT_COMP_LOGE_STR("Failed to create field stack.");
c44c3e70 2739 goto error;
e98a2d6e
PP
2740 }
2741
fd111402 2742 notit->bfcr = bt_bfcr_create(cbs, notit, log_level, NULL);
939190b3 2743 if (!notit->bfcr) {
e310e6e2 2744 BT_COMP_LOGE_STR("Failed to create binary class reader (BFCR).");
c44c3e70 2745 goto error;
e98a2d6e
PP
2746 }
2747
b09a5592 2748 bt_msg_iter_reset(notit);
e310e6e2 2749 BT_COMP_LOGD("Created CTF plugin message iterator: "
10b7a2e4 2750 "trace-addr=%p, max-request-size=%zu, "
f8811e3b
PP
2751 "data=%p, notit-addr=%p, log-level=%s",
2752 tc, max_request_sz, data, notit,
2753 bt_common_logging_level_string(log_level));
bb230c9b 2754 notit->cur_packet_offset = 0;
fdf0e7a0 2755
e98a2d6e
PP
2756end:
2757 return notit;
fdf0e7a0 2758
c44c3e70 2759error:
b09a5592 2760 bt_msg_iter_destroy(notit);
c44c3e70
JG
2761 notit = NULL;
2762 goto end;
e98a2d6e
PP
2763}
2764
b09a5592 2765void bt_msg_iter_destroy(struct bt_msg_iter *notit)
e98a2d6e 2766{
8c6884d9
PP
2767 BT_PACKET_PUT_REF_AND_RESET(notit->packet);
2768 BT_STREAM_PUT_REF_AND_RESET(notit->stream);
a6918753 2769 release_all_dscopes(notit);
e98a2d6e 2770
e310e6e2 2771 BT_COMP_LOGD("Destroying CTF plugin message iterator: addr=%p", notit);
fdf0e7a0 2772
e98a2d6e 2773 if (notit->stack) {
e310e6e2 2774 BT_COMP_LOGD_STR("Destroying field stack.");
e98a2d6e
PP
2775 stack_destroy(notit->stack);
2776 }
2777
939190b3 2778 if (notit->bfcr) {
e310e6e2 2779 BT_COMP_LOGD("Destroying BFCR: bfcr-addr=%p", notit->bfcr);
939190b3 2780 bt_bfcr_destroy(notit->bfcr);
e98a2d6e
PP
2781 }
2782
7b33a0e0
PP
2783 if (notit->stored_values) {
2784 g_array_free(notit->stored_values, TRUE);
5f870343 2785 }
fdf0e7a0 2786
e98a2d6e
PP
2787 g_free(notit);
2788}
2789
b09a5592
PP
2790enum bt_msg_iter_status bt_msg_iter_get_next_message(
2791 struct bt_msg_iter *notit,
bbbd35bf 2792 bt_self_message_iterator *msg_iter, bt_message **message)
e98a2d6e 2793{
b09a5592 2794 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
e98a2d6e 2795
8b45963b 2796 BT_ASSERT(notit);
b09a5592 2797 BT_ASSERT(message);
b09a5592 2798 notit->msg_iter = msg_iter;
989925d1 2799 notit->set_stream = true;
e310e6e2 2800 BT_COMP_LOGD("Getting next message: notit-addr=%p", notit);
fdf0e7a0 2801
e98a2d6e
PP
2802 while (true) {
2803 status = handle_state(notit);
85e7137b 2804 if (G_UNLIKELY(status == BT_MSG_ITER_STATUS_AGAIN)) {
e310e6e2 2805 BT_COMP_LOGD_STR("Medium returned BT_MSG_ITER_STATUS_AGAIN.");
7cdc2bab 2806 goto end;
85e7137b 2807 } else if (G_UNLIKELY(status != BT_MSG_ITER_STATUS_OK)) {
e310e6e2 2808 BT_COMP_LOGW("Cannot handle state: notit-addr=%p, state=%s",
bbbd35bf 2809 notit, state_string(notit->state));
e98a2d6e
PP
2810 goto end;
2811 }
2812
2813 switch (notit->state) {
bbbd35bf
PP
2814 case STATE_EMIT_MSG_EVENT:
2815 BT_ASSERT(notit->event_msg);
bbbd35bf
PP
2816 *message = notit->event_msg;
2817 notit->event_msg = NULL;
989925d1
PP
2818 goto end;
2819 case STATE_EMIT_MSG_DISCARDED_EVENTS:
2820 /* create_msg_discared_events() logs errors */
2821 create_msg_discarded_events(notit, message);
2822
2823 if (!*message) {
2824 status = BT_MSG_ITER_STATUS_ERROR;
2825 }
2826
2827 goto end;
2828 case STATE_EMIT_MSG_DISCARDED_PACKETS:
2829 /* create_msg_discared_packets() logs errors */
2830 create_msg_discarded_packets(notit, message);
2831
2832 if (!*message) {
2833 status = BT_MSG_ITER_STATUS_ERROR;
2834 }
2835
bbbd35bf
PP
2836 goto end;
2837 case STATE_EMIT_MSG_PACKET_BEGINNING:
2838 /* create_msg_packet_beginning() logs errors */
2839 create_msg_packet_beginning(notit, message);
7b33a0e0 2840
b09a5592
PP
2841 if (!*message) {
2842 status = BT_MSG_ITER_STATUS_ERROR;
6ff151ad 2843 }
7b33a0e0 2844
6ff151ad 2845 goto end;
bbbd35bf
PP
2846 case STATE_EMIT_MSG_PACKET_END_SINGLE:
2847 case STATE_EMIT_MSG_PACKET_END_MULTI:
2848 /* create_msg_packet_end() logs errors */
2849 create_msg_packet_end(notit, message);
7b33a0e0 2850
b09a5592
PP
2851 if (!*message) {
2852 status = BT_MSG_ITER_STATUS_ERROR;
e98a2d6e 2853 }
a6918753 2854
989925d1 2855 goto end;
bbbd35bf 2856 case STATE_EMIT_MSG_STREAM_BEGINNING:
989925d1
PP
2857 /* create_msg_stream_beginning() logs errors */
2858 create_msg_stream_beginning(notit, message);
bbbd35bf 2859
989925d1
PP
2860 if (!*message) {
2861 status = BT_MSG_ITER_STATUS_ERROR;
bbbd35bf
PP
2862 }
2863
989925d1 2864 goto end;
bbbd35bf 2865 case STATE_EMIT_MSG_STREAM_END:
989925d1
PP
2866 /* create_msg_stream_end() logs errors */
2867 create_msg_stream_end(notit, message);
bbbd35bf 2868
989925d1
PP
2869 if (!*message) {
2870 status = BT_MSG_ITER_STATUS_ERROR;
bbbd35bf
PP
2871 }
2872
989925d1 2873 goto end;
bbbd35bf
PP
2874 case STATE_DONE:
2875 status = BT_MSG_ITER_STATUS_EOF;
e98a2d6e
PP
2876 goto end;
2877 default:
2878 /* Non-emitting state: continue */
2879 break;
2880 }
2881 }
2882
2883end:
2884 return status;
2885}
87187cbf 2886
e5e71bf8 2887static
5ce43109
FD
2888enum bt_msg_iter_status decode_until_state( struct bt_msg_iter *notit,
2889 enum state target_state_1, enum state target_state_2)
87187cbf 2890{
b09a5592 2891 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
87187cbf 2892
8b45963b 2893 BT_ASSERT(notit);
989925d1 2894 notit->set_stream = false;
87187cbf 2895
d67906d7
FD
2896 do {
2897 /*
2898 * Check if we reached the state at which we want to stop
2899 * decoding.
2900 */
2901 if (notit->state == target_state_1 ||
2902 notit->state == target_state_2) {
2903 goto end;
2904 }
87187cbf 2905
87187cbf 2906 status = handle_state(notit);
85e7137b 2907 if (G_UNLIKELY(status == BT_MSG_ITER_STATUS_AGAIN)) {
e310e6e2 2908 BT_COMP_LOGD_STR("Medium returned BT_MSG_ITER_STATUS_AGAIN.");
87187cbf 2909 goto end;
85e7137b 2910 } else if (G_UNLIKELY(status != BT_MSG_ITER_STATUS_OK)) {
e310e6e2 2911 BT_COMP_LOGW("Cannot handle state: notit-addr=%p, state=%s",
bbbd35bf 2912 notit, state_string(notit->state));
87187cbf
PP
2913 goto end;
2914 }
2915
2916 switch (notit->state) {
87187cbf 2917 case STATE_INIT:
01ed875d 2918 case STATE_SWITCH_PACKET:
87187cbf
PP
2919 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
2920 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
2921 case STATE_AFTER_TRACE_PACKET_HEADER:
2922 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN:
2923 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE:
2924 case STATE_AFTER_STREAM_PACKET_CONTEXT:
989925d1 2925 case STATE_CHECK_EMIT_MSG_STREAM_BEGINNING:
bbbd35bf 2926 case STATE_EMIT_MSG_STREAM_BEGINNING:
989925d1
PP
2927 case STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS:
2928 case STATE_EMIT_MSG_DISCARDED_EVENTS:
2929 case STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS:
2930 case STATE_EMIT_MSG_DISCARDED_PACKETS:
d67906d7
FD
2931 case STATE_EMIT_MSG_PACKET_BEGINNING:
2932 case STATE_DSCOPE_EVENT_HEADER_BEGIN:
2933 case STATE_DSCOPE_EVENT_HEADER_CONTINUE:
2934 case STATE_AFTER_EVENT_HEADER:
2935 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN:
2936 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE:
2937 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN:
2938 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE:
2939 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN:
2940 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE:
2941 case STATE_EMIT_MSG_EVENT:
2942 case STATE_SKIP_PACKET_PADDING:
2943 case STATE_EMIT_MSG_PACKET_END_MULTI:
2944 case STATE_EMIT_MSG_PACKET_END_SINGLE:
2945 case STATE_CHECK_EMIT_MSG_STREAM_END:
2946 case STATE_EMIT_MSG_STREAM_END:
87187cbf 2947 break;
d67906d7
FD
2948 case STATE_DONE:
2949 /* fall-through */
87187cbf 2950 default:
d67906d7 2951 /* We should never get to the STATE_DONE state. */
e310e6e2 2952 BT_COMP_LOGF("Unexpected state: notit-addr=%p, state=%s",
fdf0e7a0 2953 notit, state_string(notit->state));
0fbb9a9f 2954 abort();
87187cbf 2955 }
d67906d7 2956 } while (true);
87187cbf 2957
e5e71bf8 2958end:
d67906d7
FD
2959 return status;
2960}
2961
2962static
2963enum bt_msg_iter_status read_packet_header_context_fields(
2964 struct bt_msg_iter *notit)
2965{
2966 int ret;
2967 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
2968
2969 status = decode_until_state(notit, STATE_EMIT_MSG_PACKET_BEGINNING, -1);
2970 if (status != BT_MSG_ITER_STATUS_OK) {
2971 goto end;
2972 }
2973
a6918753
PP
2974 ret = set_current_packet_content_sizes(notit);
2975 if (ret) {
b09a5592 2976 status = BT_MSG_ITER_STATUS_ERROR;
d67906d7 2977 goto end;
a6918753
PP
2978 }
2979
d67906d7 2980end:
87187cbf
PP
2981 return status;
2982}
55fa6491
PP
2983
2984BT_HIDDEN
b09a5592 2985void bt_msg_iter_set_medops_data(struct bt_msg_iter *notit,
55fa6491
PP
2986 void *medops_data)
2987{
8b45963b 2988 BT_ASSERT(notit);
55fa6491
PP
2989 notit->medium.data = medops_data;
2990}
bb230c9b
JG
2991
2992BT_HIDDEN
bbbd35bf
PP
2993enum bt_msg_iter_status bt_msg_iter_seek(struct bt_msg_iter *notit,
2994 off_t offset)
bb230c9b 2995{
b09a5592
PP
2996 enum bt_msg_iter_status ret = BT_MSG_ITER_STATUS_OK;
2997 enum bt_msg_iter_medium_status medium_status;
bb230c9b 2998
8b45963b 2999 BT_ASSERT(notit);
bb230c9b 3000 if (offset < 0) {
2655783b 3001 BT_COMP_LOGE("Cannot seek to negative offset: offset=%jd", (intmax_t) offset);
b09a5592 3002 ret = BT_MSG_ITER_STATUS_INVAL;
bb230c9b
JG
3003 goto end;
3004 }
3005
3006 if (!notit->medium.medops.seek) {
b09a5592 3007 ret = BT_MSG_ITER_STATUS_UNSUPPORTED;
e310e6e2 3008 BT_COMP_LOGD("Aborting seek as the iterator's underlying media does not implement seek support.");
bb230c9b
JG
3009 goto end;
3010 }
3011
3012 medium_status = notit->medium.medops.seek(
b09a5592
PP
3013 BT_MSG_ITER_SEEK_WHENCE_SET, offset, notit->medium.data);
3014 if (medium_status != BT_MSG_ITER_MEDIUM_STATUS_OK) {
3015 if (medium_status == BT_MSG_ITER_MEDIUM_STATUS_EOF) {
3016 ret = BT_MSG_ITER_STATUS_EOF;
bb230c9b 3017 } else {
b09a5592 3018 ret = BT_MSG_ITER_STATUS_ERROR;
bb230c9b
JG
3019 goto end;
3020 }
3021 }
3022
b09a5592 3023 bt_msg_iter_reset(notit);
bb230c9b 3024 notit->cur_packet_offset = offset;
7b33a0e0 3025
bb230c9b
JG
3026end:
3027 return ret;
3028}
3029
5ce43109
FD
3030static
3031enum bt_msg_iter_status clock_snapshot_at_msg_iter_state(
3032 struct bt_msg_iter *notit, enum state target_state_1,
3033 enum state target_state_2, uint64_t *clock_snapshot)
3034{
3035 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
3036
3037 BT_ASSERT(notit);
3038 BT_ASSERT(clock_snapshot);
3039 status = decode_until_state(notit, target_state_1, target_state_2);
3040 if (status != BT_MSG_ITER_STATUS_OK) {
3041 goto end;
3042 }
3043
3044 *clock_snapshot = notit->default_clock_snapshot;
3045end:
3046 return status;
3047}
3048
3049BT_HIDDEN
3050enum bt_msg_iter_status bt_msg_iter_curr_packet_first_event_clock_snapshot(
3051 struct bt_msg_iter *notit, uint64_t *first_clock_snapshot)
3052{
3053 return clock_snapshot_at_msg_iter_state(notit,
3054 STATE_AFTER_EVENT_HEADER, -1, first_clock_snapshot);
3055}
3056
3057BT_HIDDEN
3058enum bt_msg_iter_status bt_msg_iter_curr_packet_last_event_clock_snapshot(
3059 struct bt_msg_iter *notit, uint64_t *last_clock_snapshot)
3060{
3061 return clock_snapshot_at_msg_iter_state(notit,
3062 STATE_EMIT_MSG_PACKET_END_SINGLE,
3063 STATE_EMIT_MSG_PACKET_END_MULTI, last_clock_snapshot);
3064}
3065
7b33a0e0 3066BT_HIDDEN
b09a5592
PP
3067enum bt_msg_iter_status bt_msg_iter_get_packet_properties(
3068 struct bt_msg_iter *notit,
3069 struct bt_msg_iter_packet_properties *props)
7b33a0e0 3070{
e5e71bf8
PP
3071 enum bt_msg_iter_status status;
3072
7b33a0e0
PP
3073 BT_ASSERT(notit);
3074 BT_ASSERT(props);
e5e71bf8
PP
3075 status = read_packet_header_context_fields(notit);
3076 if (status != BT_MSG_ITER_STATUS_OK) {
3077 goto end;
3078 }
7b33a0e0 3079
bbbd35bf
PP
3080 props->exp_packet_total_size = notit->cur_exp_packet_total_size;
3081 props->exp_packet_content_size = notit->cur_exp_packet_content_size;
7b33a0e0
PP
3082 props->stream_class_id = (uint64_t) notit->cur_stream_class_id;
3083 props->data_stream_id = notit->cur_data_stream_id;
3084 props->snapshots.discarded_events = notit->snapshots.discarded_events;
3085 props->snapshots.packets = notit->snapshots.packets;
3086 props->snapshots.beginning_clock = notit->snapshots.beginning_clock;
3087 props->snapshots.end_clock = notit->snapshots.end_clock;
e5e71bf8
PP
3088
3089end:
3090 return status;
bb230c9b 3091}
bbbd35bf
PP
3092
3093BT_HIDDEN
3094void bt_msg_iter_set_emit_stream_beginning_message(struct bt_msg_iter *notit,
3095 bool val)
3096{
3097 notit->emit_stream_begin_msg = val;
3098}
3099
3100BT_HIDDEN
3101void bt_msg_iter_set_emit_stream_end_message(struct bt_msg_iter *notit,
3102 bool val)
3103{
3104 notit->emit_stream_end_msg = val;
3105}
8482a078
FD
3106
3107BT_HIDDEN
3108void bt_msg_iter_set_dry_run(struct bt_msg_iter *notit,
3109 bool val)
3110{
3111 notit->dry_run = val;
3112}
This page took 0.263795 seconds and 4 git commands to generate.