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