lib: iterator.c: auto-seek: handle new message types
[babeltrace.git] / plugins / ctf / common / msg-iter / msg-iter.c
CommitLineData
e98a2d6e 1/*
d6e69534 2 * Babeltrace - CTF message iterator
06a626b8 3 *
44c440bc
PP
4 * Copyright (c) 2015-2018 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2015-2018 Philippe Proulx <pproulx@efficios.com>
e98a2d6e
PP
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
d6e69534 26#define BT_LOG_TAG "PLUGIN-CTF-MSG-ITER"
fdf0e7a0
PP
27#include "logging.h"
28
e98a2d6e
PP
29#include <stdint.h>
30#include <inttypes.h>
31#include <stdio.h>
32#include <stddef.h>
33#include <stdbool.h>
f6ccaed9 34#include <babeltrace/assert-internal.h>
e98a2d6e 35#include <string.h>
9d408fca 36#include <babeltrace/babeltrace.h>
3dca2276 37#include <babeltrace/common-internal.h>
e98a2d6e 38#include <glib.h>
0fbb9a9f 39#include <stdlib.h>
e98a2d6e 40
d6e69534 41#include "msg-iter.h"
5cd6d0e5 42#include "../bfcr/bfcr.h"
e98a2d6e 43
d6e69534 44struct bt_msg_iter;
e98a2d6e
PP
45
46/* A visit stack entry */
47struct stack_entry {
48 /*
49 * Current base field, one of:
50 *
51 * * string
52 * * structure
53 * * array
54 * * sequence
55 * * variant
56 *
312c056a 57 * Field is borrowed.
e98a2d6e 58 */
b19ff26f 59 bt_field *base;
e98a2d6e 60
44c440bc 61 /* Index of next field to set */
e98a2d6e
PP
62 size_t index;
63};
64
65/* Visit stack */
66struct stack {
afe86a06
PP
67 /* Entries (struct stack_entry) */
68 GArray *entries;
69
70 /* Number of active entries */
71 size_t size;
e98a2d6e
PP
72};
73
74/* State */
75enum state {
76 STATE_INIT,
77 STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN,
78 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE,
79 STATE_AFTER_TRACE_PACKET_HEADER,
80 STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN,
81 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE,
82 STATE_AFTER_STREAM_PACKET_CONTEXT,
d6e69534
PP
83 STATE_EMIT_MSG_NEW_STREAM,
84 STATE_EMIT_MSG_NEW_PACKET,
44c440bc
PP
85 STATE_DSCOPE_EVENT_HEADER_BEGIN,
86 STATE_DSCOPE_EVENT_HEADER_CONTINUE,
87 STATE_AFTER_EVENT_HEADER,
88 STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN,
89 STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE,
90 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN,
91 STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE,
e98a2d6e
PP
92 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN,
93 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE,
d6e69534
PP
94 STATE_EMIT_MSG_EVENT,
95 STATE_EMIT_MSG_END_OF_PACKET,
f42867e2 96 STATE_DONE,
e98a2d6e
PP
97 STATE_SKIP_PACKET_PADDING,
98};
99
d6e69534
PP
100/* CTF message iterator */
101struct bt_msg_iter {
e98a2d6e
PP
102 /* Visit stack */
103 struct stack *stack;
104
d6e69534
PP
105 /* Current message iterator to create messages (weak) */
106 bt_self_message_iterator *msg_iter;
5c563278 107
e98a2d6e
PP
108 /*
109 * Current dynamic scope field pointer.
110 *
312c056a
PP
111 * This is set by read_dscope_begin_state() and contains the
112 * value of one of the pointers in `dscopes` below.
e98a2d6e 113 */
b19ff26f 114 bt_field *cur_dscope_field;
e98a2d6e 115
44c440bc
PP
116 /*
117 * True if we're done filling a string field from a text
118 * array/sequence payload.
119 */
120 bool done_filling_string;
121
122 /* Trace and classes */
41693723
PP
123 /* True to set IR fields */
124 bool set_ir_fields;
125
e98a2d6e 126 struct {
44c440bc
PP
127 struct ctf_trace_class *tc;
128 struct ctf_stream_class *sc;
129 struct ctf_event_class *ec;
e98a2d6e
PP
130 } meta;
131
83ebb7f1 132 /* Current packet context field wrapper (NULL if not created yet) */
b19ff26f 133 bt_packet_context_field *packet_context_field;
312c056a 134
e98a2d6e 135 /* Current packet (NULL if not created yet) */
b19ff26f 136 bt_packet *packet;
e98a2d6e 137
af87daef 138 /* Current stream (NULL if not set yet) */
b19ff26f 139 bt_stream *stream;
af87daef 140
312c056a 141 /* Current event (NULL if not created yet) */
b19ff26f 142 bt_event *event;
312c056a 143
d6e69534
PP
144 /* Current event message (NULL if not created yet) */
145 bt_message *event_msg;
312c056a 146
44c440bc 147 /* Database of current dynamic scopes */
e98a2d6e 148 struct {
b19ff26f 149 bt_field *stream_packet_context;
b19ff26f
PP
150 bt_field *event_common_context;
151 bt_field *event_spec_context;
152 bt_field *event_payload;
e98a2d6e
PP
153 } dscopes;
154
155 /* Current state */
156 enum state state;
157
2cf1d51e 158 /* Current medium buffer data */
e98a2d6e
PP
159 struct {
160 /* Last address provided by medium */
161 const uint8_t *addr;
162
163 /* Buffer size provided by medium (bytes) */
164 size_t sz;
165
166 /* Offset within whole packet of addr (bits) */
167 size_t packet_offset;
168
169 /* Current position from addr (bits) */
170 size_t at;
174e773b
PP
171
172 /* Position of the last event header from addr (bits) */
173 size_t last_eh_at;
e98a2d6e
PP
174 } buf;
175
176 /* Binary type reader */
5cd6d0e5 177 struct bt_bfcr *bfcr;
e98a2d6e 178
2cf1d51e 179 /* Current medium data */
e98a2d6e 180 struct {
d6e69534 181 struct bt_msg_iter_medium_ops medops;
e98a2d6e
PP
182 size_t max_request_sz;
183 void *data;
184 } medium;
185
f42867e2
PP
186 /* Stream beginning was emitted */
187 bool stream_begin_emitted;
188
e98a2d6e 189 /* Current packet size (bits) (-1 if unknown) */
44c440bc 190 int64_t cur_exp_packet_total_size;
e98a2d6e
PP
191
192 /* Current content size (bits) (-1 if unknown) */
44c440bc 193 int64_t cur_exp_packet_content_size;
c44c3e70 194
44c440bc
PP
195 /* Current stream class ID */
196 int64_t cur_stream_class_id;
9e0c8dbb 197
44c440bc
PP
198 /* Current event class ID */
199 int64_t cur_event_class_id;
5f870343 200
44c440bc
PP
201 /* Current data stream ID */
202 int64_t cur_data_stream_id;
5f870343
JG
203
204 /*
44c440bc
PP
205 * Offset, in the underlying media, of the current packet's
206 * start (-1 if unknown).
5f870343 207 */
44c440bc
PP
208 off_t cur_packet_offset;
209
210 /* Default clock's current value */
605e1019 211 uint64_t default_clock_snapshot;
5f870343 212
44c440bc
PP
213 /* End of packet snapshots */
214 struct {
215 uint64_t discarded_events;
216 uint64_t packets;
217 uint64_t beginning_clock;
218 uint64_t end_clock;
219 } snapshots;
220
221 /* Stored values (for sequence lengths, variant tags) */
222 GArray *stored_values;
e98a2d6e
PP
223};
224
fdf0e7a0
PP
225static inline
226const char *state_string(enum state state)
227{
228 switch (state) {
229 case STATE_INIT:
230 return "STATE_INIT";
231 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
232 return "STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN";
233 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
234 return "STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE";
235 case STATE_AFTER_TRACE_PACKET_HEADER:
236 return "STATE_AFTER_TRACE_PACKET_HEADER";
237 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN:
238 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN";
239 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE:
240 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE";
241 case STATE_AFTER_STREAM_PACKET_CONTEXT:
242 return "STATE_AFTER_STREAM_PACKET_CONTEXT";
d6e69534
PP
243 case STATE_EMIT_MSG_NEW_PACKET:
244 return "STATE_EMIT_MSG_NEW_PACKET";
245 case STATE_EMIT_MSG_NEW_STREAM:
246 return "STATE_EMIT_MSG_NEW_STREAM";
44c440bc
PP
247 case STATE_DSCOPE_EVENT_HEADER_BEGIN:
248 return "STATE_DSCOPE_EVENT_HEADER_BEGIN";
249 case STATE_DSCOPE_EVENT_HEADER_CONTINUE:
250 return "STATE_DSCOPE_EVENT_HEADER_CONTINUE";
251 case STATE_AFTER_EVENT_HEADER:
252 return "STATE_AFTER_EVENT_HEADER";
253 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN:
254 return "STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN";
255 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE:
256 return "STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE";
257 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN:
258 return "STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN";
259 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE:
260 return "STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE";
fdf0e7a0
PP
261 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN:
262 return "STATE_DSCOPE_EVENT_PAYLOAD_BEGIN";
263 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE:
264 return "STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE";
d6e69534
PP
265 case STATE_EMIT_MSG_EVENT:
266 return "STATE_EMIT_MSG_EVENT";
267 case STATE_EMIT_MSG_END_OF_PACKET:
268 return "STATE_EMIT_MSG_END_OF_PACKET";
f42867e2
PP
269 case STATE_DONE:
270 return "STATE_DONE";
fdf0e7a0
PP
271 case STATE_SKIP_PACKET_PADDING:
272 return "STATE_SKIP_PACKET_PADDING";
273 default:
274 return "(unknown)";
275 }
276}
277
2cf1d51e 278static
d6e69534 279int bt_msg_iter_switch_packet(struct bt_msg_iter *notit);
2cf1d51e 280
e98a2d6e 281static
d6e69534 282struct stack *stack_new(struct bt_msg_iter *notit)
e98a2d6e
PP
283{
284 struct stack *stack = NULL;
285
286 stack = g_new0(struct stack, 1);
287 if (!stack) {
fdf0e7a0 288 BT_LOGE_STR("Failed to allocate one stack.");
e98a2d6e
PP
289 goto error;
290 }
291
afe86a06 292 stack->entries = g_array_new(FALSE, TRUE, sizeof(struct stack_entry));
e98a2d6e 293 if (!stack->entries) {
afe86a06 294 BT_LOGE_STR("Failed to allocate a GArray.");
e98a2d6e
PP
295 goto error;
296 }
297
fdf0e7a0 298 BT_LOGD("Created stack: notit-addr=%p, stack-addr=%p", notit, stack);
44c440bc 299 goto end;
fdf0e7a0 300
e98a2d6e
PP
301error:
302 g_free(stack);
44c440bc
PP
303 stack = NULL;
304
305end:
306 return stack;
e98a2d6e
PP
307}
308
309static
310void stack_destroy(struct stack *stack)
311{
f6ccaed9 312 BT_ASSERT(stack);
fdf0e7a0 313 BT_LOGD("Destroying stack: addr=%p", stack);
afe86a06
PP
314
315 if (stack->entries) {
316 g_array_free(stack->entries, TRUE);
317 }
318
e98a2d6e
PP
319 g_free(stack);
320}
321
322static
b19ff26f 323void stack_push(struct stack *stack, bt_field *base)
e98a2d6e 324{
e98a2d6e 325 struct stack_entry *entry;
e98a2d6e 326
f6ccaed9
PP
327 BT_ASSERT(stack);
328 BT_ASSERT(base);
fdf0e7a0 329 BT_LOGV("Pushing base field on stack: stack-addr=%p, "
afe86a06
PP
330 "stack-size-before=%zu, stack-size-after=%zu",
331 stack, stack->size, stack->size + 1);
332
333 if (stack->entries->len == stack->size) {
334 g_array_set_size(stack->entries, stack->size + 1);
e98a2d6e
PP
335 }
336
afe86a06 337 entry = &g_array_index(stack->entries, struct stack_entry, stack->size);
312c056a 338 entry->base = base;
afe86a06
PP
339 entry->index = 0;
340 stack->size++;
e98a2d6e
PP
341}
342
343static inline
344unsigned int stack_size(struct stack *stack)
345{
f6ccaed9 346 BT_ASSERT(stack);
afe86a06 347 return stack->size;
e98a2d6e
PP
348}
349
350static
351void stack_pop(struct stack *stack)
352{
f6ccaed9
PP
353 BT_ASSERT(stack);
354 BT_ASSERT(stack_size(stack));
fdf0e7a0 355 BT_LOGV("Popping from stack: "
afe86a06
PP
356 "stack-addr=%p, stack-size-before=%zu, stack-size-after=%zu",
357 stack, stack->size, stack->size - 1);
358 stack->size--;
e98a2d6e
PP
359}
360
361static inline
362struct stack_entry *stack_top(struct stack *stack)
363{
f6ccaed9
PP
364 BT_ASSERT(stack);
365 BT_ASSERT(stack_size(stack));
afe86a06
PP
366 return &g_array_index(stack->entries, struct stack_entry,
367 stack->size - 1);
e98a2d6e
PP
368}
369
370static inline
371bool stack_empty(struct stack *stack)
372{
373 return stack_size(stack) == 0;
374}
375
376static
377void stack_clear(struct stack *stack)
378{
f6ccaed9 379 BT_ASSERT(stack);
afe86a06 380 stack->size = 0;
e98a2d6e
PP
381}
382
383static inline
d6e69534
PP
384enum bt_msg_iter_status msg_iter_status_from_m_status(
385 enum bt_msg_iter_medium_status m_status)
e98a2d6e 386{
44c440bc 387 /* They are the same */
dc77b521 388 return (int) m_status;
e98a2d6e
PP
389}
390
391static inline
d6e69534 392size_t buf_size_bits(struct bt_msg_iter *notit)
e98a2d6e 393{
fdf0e7a0 394 return notit->buf.sz * 8;
e98a2d6e
PP
395}
396
397static inline
d6e69534 398size_t buf_available_bits(struct bt_msg_iter *notit)
e98a2d6e
PP
399{
400 return buf_size_bits(notit) - notit->buf.at;
401}
402
403static inline
d6e69534 404size_t packet_at(struct bt_msg_iter *notit)
e98a2d6e
PP
405{
406 return notit->buf.packet_offset + notit->buf.at;
407}
408
e98a2d6e 409static inline
d6e69534 410void buf_consume_bits(struct bt_msg_iter *notit, size_t incr)
e98a2d6e 411{
fdf0e7a0
PP
412 BT_LOGV("Advancing cursor: notit-addr=%p, cur-before=%zu, cur-after=%zu",
413 notit, notit->buf.at, notit->buf.at + incr);
e98a2d6e
PP
414 notit->buf.at += incr;
415}
416
e98a2d6e 417static
d6e69534
PP
418enum bt_msg_iter_status request_medium_bytes(
419 struct bt_msg_iter *notit)
e98a2d6e 420{
fdf0e7a0
PP
421 uint8_t *buffer_addr = NULL;
422 size_t buffer_sz = 0;
d6e69534 423 enum bt_msg_iter_medium_status m_status;
e98a2d6e 424
fdf0e7a0
PP
425 BT_LOGV("Calling user function (request bytes): notit-addr=%p, "
426 "request-size=%zu", notit, notit->medium.max_request_sz);
e98a2d6e
PP
427 m_status = notit->medium.medops.request_bytes(
428 notit->medium.max_request_sz, &buffer_addr,
429 &buffer_sz, notit->medium.data);
fdf0e7a0 430 BT_LOGV("User function returned: status=%s, buf-addr=%p, buf-size=%zu",
d6e69534 431 bt_msg_iter_medium_status_string(m_status),
fdf0e7a0 432 buffer_addr, buffer_sz);
d6e69534 433 if (m_status == BT_MSG_ITER_MEDIUM_STATUS_OK) {
f6ccaed9 434 BT_ASSERT(buffer_sz != 0);
e98a2d6e
PP
435
436 /* New packet offset is old one + old size (in bits) */
437 notit->buf.packet_offset += buf_size_bits(notit);
438
439 /* Restart at the beginning of the new medium buffer */
440 notit->buf.at = 0;
2b186c3e 441 notit->buf.last_eh_at = SIZE_MAX;
e98a2d6e
PP
442
443 /* New medium buffer size */
444 notit->buf.sz = buffer_sz;
445
446 /* New medium buffer address */
447 notit->buf.addr = buffer_addr;
fdf0e7a0
PP
448
449 BT_LOGV("User function returned new bytes: "
450 "packet-offset=%zu, cur=%zu, size=%zu, addr=%p",
451 notit->buf.packet_offset, notit->buf.at,
452 notit->buf.sz, notit->buf.addr);
453 BT_LOGV_MEM(buffer_addr, buffer_sz, "Returned bytes at %p:",
454 buffer_addr);
d6e69534 455 } else if (m_status == BT_MSG_ITER_MEDIUM_STATUS_EOF) {
0684124b
PP
456 /*
457 * User returned end of stream: validate that we're not
458 * in the middle of a packet header, packet context, or
459 * event.
460 */
44c440bc
PP
461 if (notit->cur_exp_packet_total_size >= 0) {
462 if (packet_at(notit) ==
463 notit->cur_exp_packet_total_size) {
df0139b8 464 goto end;
0684124b 465 }
df0139b8
PP
466 } else {
467 if (packet_at(notit) == 0) {
468 goto end;
0684124b 469 }
0684124b 470
df0139b8
PP
471 if (notit->buf.last_eh_at != SIZE_MAX &&
472 notit->buf.at == notit->buf.last_eh_at) {
473 goto end;
0684124b
PP
474 }
475 }
476
0684124b 477 /* All other states are invalid */
d6e69534 478 BT_LOGW("User function returned %s, but message iterator is in an unexpected state: "
df0139b8
PP
479 "state=%s, cur-packet-size=%" PRId64 ", cur=%zu, "
480 "packet-cur=%zu, last-eh-at=%zu",
d6e69534 481 bt_msg_iter_medium_status_string(m_status),
df0139b8 482 state_string(notit->state),
44c440bc 483 notit->cur_exp_packet_total_size,
df0139b8
PP
484 notit->buf.at, packet_at(notit),
485 notit->buf.last_eh_at);
d6e69534 486 m_status = BT_MSG_ITER_MEDIUM_STATUS_ERROR;
fdf0e7a0
PP
487 } else if (m_status < 0) {
488 BT_LOGW("User function failed: status=%s",
d6e69534 489 bt_msg_iter_medium_status_string(m_status));
e98a2d6e
PP
490 }
491
df0139b8 492end:
d6e69534 493 return msg_iter_status_from_m_status(m_status);
e98a2d6e
PP
494}
495
496static inline
d6e69534
PP
497enum bt_msg_iter_status buf_ensure_available_bits(
498 struct bt_msg_iter *notit)
e98a2d6e 499{
d6e69534 500 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
e98a2d6e 501
44c440bc 502 if (unlikely(buf_available_bits(notit) == 0)) {
e98a2d6e 503 /*
d6e69534 504 * This _cannot_ return BT_MSG_ITER_STATUS_OK
e98a2d6e
PP
505 * _and_ no bits.
506 */
507 status = request_medium_bytes(notit);
508 }
509
510 return status;
511}
512
513static
d6e69534
PP
514enum bt_msg_iter_status read_dscope_begin_state(
515 struct bt_msg_iter *notit,
5cd6d0e5 516 struct ctf_field_class *dscope_fc,
e98a2d6e 517 enum state done_state, enum state continue_state,
b19ff26f 518 bt_field *dscope_field)
e98a2d6e 519{
d6e69534 520 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
5cd6d0e5 521 enum bt_bfcr_status bfcr_status;
e98a2d6e
PP
522 size_t consumed_bits;
523
e98a2d6e 524 notit->cur_dscope_field = dscope_field;
5cd6d0e5
PP
525 BT_LOGV("Starting BFCR: notit-addr=%p, bfcr-addr=%p, fc-addr=%p",
526 notit, notit->bfcr, dscope_fc);
527 consumed_bits = bt_bfcr_start(notit->bfcr, dscope_fc,
e98a2d6e 528 notit->buf.addr, notit->buf.at, packet_at(notit),
5cd6d0e5
PP
529 notit->buf.sz, &bfcr_status);
530 BT_LOGV("BFCR consumed bits: size=%zu", consumed_bits);
e98a2d6e 531
5cd6d0e5
PP
532 switch (bfcr_status) {
533 case BT_BFCR_STATUS_OK:
534 /* Field class was read completely */
fdf0e7a0 535 BT_LOGV_STR("Field was completely decoded.");
e98a2d6e
PP
536 notit->state = done_state;
537 break;
5cd6d0e5
PP
538 case BT_BFCR_STATUS_EOF:
539 BT_LOGV_STR("BFCR needs more data to decode field completely.");
e98a2d6e
PP
540 notit->state = continue_state;
541 break;
542 default:
5cd6d0e5
PP
543 BT_LOGW("BFCR failed to start: notit-addr=%p, bfcr-addr=%p, "
544 "status=%s", notit, notit->bfcr,
545 bt_bfcr_status_string(bfcr_status));
d6e69534 546 status = BT_MSG_ITER_STATUS_ERROR;
e98a2d6e
PP
547 goto end;
548 }
549
550 /* Consume bits now since we know we're not in an error state */
551 buf_consume_bits(notit, consumed_bits);
552
553end:
554 return status;
555}
556
557static
d6e69534
PP
558enum bt_msg_iter_status read_dscope_continue_state(
559 struct bt_msg_iter *notit, enum state done_state)
e98a2d6e 560{
d6e69534 561 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
5cd6d0e5 562 enum bt_bfcr_status bfcr_status;
e98a2d6e
PP
563 size_t consumed_bits;
564
5cd6d0e5
PP
565 BT_LOGV("Continuing BFCR: notit-addr=%p, bfcr-addr=%p",
566 notit, notit->bfcr);
df0139b8 567
e98a2d6e 568 status = buf_ensure_available_bits(notit);
d6e69534 569 if (status != BT_MSG_ITER_STATUS_OK) {
fdf0e7a0
PP
570 if (status < 0) {
571 BT_LOGW("Cannot ensure that buffer has at least one byte: "
d6e69534
PP
572 "msg-addr=%p, status=%s",
573 notit, bt_msg_iter_status_string(status));
fdf0e7a0
PP
574 } else {
575 BT_LOGV("Cannot ensure that buffer has at least one byte: "
d6e69534
PP
576 "msg-addr=%p, status=%s",
577 notit, bt_msg_iter_status_string(status));
fdf0e7a0
PP
578 }
579
e98a2d6e
PP
580 goto end;
581 }
582
5cd6d0e5
PP
583 consumed_bits = bt_bfcr_continue(notit->bfcr, notit->buf.addr,
584 notit->buf.sz, &bfcr_status);
585 BT_LOGV("BFCR consumed bits: size=%zu", consumed_bits);
e98a2d6e 586
5cd6d0e5
PP
587 switch (bfcr_status) {
588 case BT_BFCR_STATUS_OK:
78586d8a 589 /* Type was read completely. */
fdf0e7a0 590 BT_LOGV_STR("Field was completely decoded.");
e98a2d6e
PP
591 notit->state = done_state;
592 break;
5cd6d0e5 593 case BT_BFCR_STATUS_EOF:
78586d8a 594 /* Stay in this continue state. */
5cd6d0e5 595 BT_LOGV_STR("BFCR needs more data to decode field completely.");
e98a2d6e
PP
596 break;
597 default:
5cd6d0e5
PP
598 BT_LOGW("BFCR failed to continue: notit-addr=%p, bfcr-addr=%p, "
599 "status=%s", notit, notit->bfcr,
600 bt_bfcr_status_string(bfcr_status));
d6e69534 601 status = BT_MSG_ITER_STATUS_ERROR;
e98a2d6e
PP
602 goto end;
603 }
604
78586d8a 605 /* Consume bits now since we know we're not in an error state. */
e98a2d6e 606 buf_consume_bits(notit, consumed_bits);
e98a2d6e
PP
607end:
608 return status;
609}
610
611static
d6e69534 612void release_event_dscopes(struct bt_msg_iter *notit)
e98a2d6e 613{
44c440bc
PP
614 notit->dscopes.event_common_context = NULL;
615 notit->dscopes.event_spec_context = NULL;
312c056a 616 notit->dscopes.event_payload = NULL;
e98a2d6e
PP
617}
618
619static
d6e69534 620void release_all_dscopes(struct bt_msg_iter *notit)
e98a2d6e 621{
312c056a
PP
622 notit->dscopes.stream_packet_context = NULL;
623
624 if (notit->packet_context_field) {
40f4ba76 625 bt_packet_context_field_release(notit->packet_context_field);
312c056a
PP
626 notit->packet_context_field = NULL;
627 }
628
629 release_event_dscopes(notit);
e98a2d6e
PP
630}
631
632static
d6e69534
PP
633enum bt_msg_iter_status read_packet_header_begin_state(
634 struct bt_msg_iter *notit)
e98a2d6e 635{
5cd6d0e5 636 struct ctf_field_class *packet_header_fc = NULL;
d6e69534 637 enum bt_msg_iter_status ret = BT_MSG_ITER_STATUS_OK;
e98a2d6e 638
d6e69534 639 if (bt_msg_iter_switch_packet(notit)) {
fdf0e7a0 640 BT_LOGW("Cannot switch packet: notit-addr=%p", notit);
d6e69534 641 ret = BT_MSG_ITER_STATUS_ERROR;
2cf1d51e
JG
642 goto end;
643 }
e98a2d6e 644
862ca4ed 645 /* Packet header class is common to the whole trace class. */
5cd6d0e5
PP
646 packet_header_fc = notit->meta.tc->packet_header_fc;
647 if (!packet_header_fc) {
835b2d10 648 notit->state = STATE_AFTER_TRACE_PACKET_HEADER;
e98a2d6e
PP
649 goto end;
650 }
651
44c440bc
PP
652 notit->cur_stream_class_id = -1;
653 notit->cur_event_class_id = -1;
654 notit->cur_data_stream_id = -1;
fdf0e7a0 655 BT_LOGV("Decoding packet header field:"
862ca4ed
PP
656 "notit-addr=%p, trace-class-addr=%p, fc-addr=%p",
657 notit, notit->meta.tc, packet_header_fc);
5cd6d0e5 658 ret = read_dscope_begin_state(notit, packet_header_fc,
fdf0e7a0 659 STATE_AFTER_TRACE_PACKET_HEADER,
83ebb7f1 660 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE, NULL);
fdf0e7a0
PP
661 if (ret < 0) {
662 BT_LOGW("Cannot decode packet header field: "
862ca4ed
PP
663 "notit-addr=%p, trace-class-addr=%p, "
664 "fc-addr=%p",
665 notit, notit->meta.tc, packet_header_fc);
fdf0e7a0 666 }
d1e46835 667
e98a2d6e 668end:
2cf1d51e 669 return ret;
e98a2d6e
PP
670}
671
672static
d6e69534
PP
673enum bt_msg_iter_status read_packet_header_continue_state(
674 struct bt_msg_iter *notit)
e98a2d6e
PP
675{
676 return read_dscope_continue_state(notit,
44c440bc 677 STATE_AFTER_TRACE_PACKET_HEADER);
5f870343
JG
678}
679
e98a2d6e 680static inline
d6e69534 681enum bt_msg_iter_status set_current_stream_class(struct bt_msg_iter *notit)
e98a2d6e 682{
d6e69534 683 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
44c440bc
PP
684 struct ctf_stream_class *new_stream_class = NULL;
685
686 if (notit->cur_stream_class_id == -1) {
fdf0e7a0 687 /*
44c440bc
PP
688 * No current stream class ID field, therefore only one
689 * stream class.
fdf0e7a0 690 */
44c440bc
PP
691 if (notit->meta.tc->stream_classes->len != 1) {
692 BT_LOGW("Need exactly one stream class since there's "
693 "no stream class ID field: "
862ca4ed 694 "notit-addr=%p", notit);
d6e69534 695 status = BT_MSG_ITER_STATUS_ERROR;
44c440bc
PP
696 goto end;
697 }
e98a2d6e 698
44c440bc
PP
699 new_stream_class = notit->meta.tc->stream_classes->pdata[0];
700 notit->cur_stream_class_id = new_stream_class->id;
e98a2d6e
PP
701 }
702
44c440bc
PP
703 new_stream_class = ctf_trace_class_borrow_stream_class_by_id(
704 notit->meta.tc, notit->cur_stream_class_id);
115de887 705 if (!new_stream_class) {
862ca4ed 706 BT_LOGW("No stream class with ID of stream class ID to use in trace class: "
fdf0e7a0 707 "notit-addr=%p, stream-class-id=%" PRIu64 ", "
862ca4ed
PP
708 "trace-class-addr=%p",
709 notit, notit->cur_stream_class_id, notit->meta.tc);
d6e69534 710 status = BT_MSG_ITER_STATUS_ERROR;
e98a2d6e
PP
711 goto end;
712 }
713
44c440bc
PP
714 if (notit->meta.sc) {
715 if (new_stream_class != notit->meta.sc) {
115de887
PP
716 BT_LOGW("Two packets refer to two different stream classes within the same packet sequence: "
717 "notit-addr=%p, prev-stream-class-addr=%p, "
115de887
PP
718 "prev-stream-class-id=%" PRId64 ", "
719 "next-stream-class-addr=%p, "
115de887 720 "next-stream-class-id=%" PRId64 ", "
862ca4ed 721 "trace-addr=%p",
44c440bc
PP
722 notit, notit->meta.sc,
723 notit->meta.sc->id,
115de887 724 new_stream_class,
44c440bc 725 new_stream_class->id,
862ca4ed 726 notit->meta.tc);
d6e69534 727 status = BT_MSG_ITER_STATUS_ERROR;
115de887
PP
728 goto end;
729 }
730 } else {
44c440bc 731 notit->meta.sc = new_stream_class;
115de887
PP
732 }
733
fdf0e7a0
PP
734 BT_LOGV("Set current stream class: "
735 "notit-addr=%p, stream-class-addr=%p, "
44c440bc
PP
736 "stream-class-id=%" PRId64,
737 notit, notit->meta.sc, notit->meta.sc->id);
d1e46835 738
e98a2d6e 739end:
e98a2d6e
PP
740 return status;
741}
742
312c056a 743static inline
d6e69534 744enum bt_msg_iter_status set_current_stream(struct bt_msg_iter *notit)
312c056a 745{
d6e69534 746 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
b19ff26f 747 bt_stream *stream = NULL;
312c056a
PP
748
749 BT_LOGV("Calling user function (get stream): notit-addr=%p, "
44c440bc
PP
750 "stream-class-addr=%p, stream-class-id=%" PRId64,
751 notit, notit->meta.sc,
752 notit->meta.sc->id);
398454ed 753 stream = notit->medium.medops.borrow_stream(
44c440bc 754 notit->meta.sc->ir_sc, notit->cur_data_stream_id,
398454ed 755 notit->medium.data);
c5b9b441 756 bt_stream_get_ref(stream);
312c056a
PP
757 BT_LOGV("User function returned: stream-addr=%p", stream);
758 if (!stream) {
44c440bc
PP
759 BT_LOGW_STR("User function failed to return a stream object "
760 "for the given stream class.");
d6e69534 761 status = BT_MSG_ITER_STATUS_ERROR;
312c056a
PP
762 goto end;
763 }
764
765 if (notit->stream && stream != notit->stream) {
44c440bc
PP
766 BT_LOGW("User function returned a different stream than the "
767 "previous one for the same sequence of packets.");
d6e69534 768 status = BT_MSG_ITER_STATUS_ERROR;
312c056a
PP
769 goto end;
770 }
771
c5b9b441 772 BT_STREAM_MOVE_REF(notit->stream, stream);
312c056a
PP
773
774end:
c5b9b441 775 bt_stream_put_ref(stream);
312c056a
PP
776 return status;
777}
778
779static inline
d6e69534 780enum bt_msg_iter_status set_current_packet(struct bt_msg_iter *notit)
312c056a 781{
d6e69534 782 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
b19ff26f 783 bt_packet *packet = NULL;
312c056a 784
d6e69534 785 BT_LOGV("Creating packet for packet message: "
312c056a
PP
786 "notit-addr=%p", notit);
787 BT_LOGV("Creating packet from stream: "
788 "notit-addr=%p, stream-addr=%p, "
789 "stream-class-addr=%p, "
312c056a 790 "stream-class-id=%" PRId64,
44c440bc
PP
791 notit, notit->stream, notit->meta.sc,
792 notit->meta.sc->id);
312c056a
PP
793
794 /* Create packet */
795 BT_ASSERT(notit->stream);
40f4ba76 796 packet = bt_packet_create(notit->stream);
312c056a
PP
797 if (!packet) {
798 BT_LOGE("Cannot create packet from stream: "
799 "notit-addr=%p, stream-addr=%p, "
800 "stream-class-addr=%p, "
312c056a 801 "stream-class-id=%" PRId64,
44c440bc
PP
802 notit, notit->stream, notit->meta.sc,
803 notit->meta.sc->id);
312c056a
PP
804 goto error;
805 }
806
807 goto end;
808
809error:
c5b9b441 810 BT_PACKET_PUT_REF_AND_RESET(packet);
d6e69534 811 status = BT_MSG_ITER_STATUS_ERROR;
312c056a
PP
812
813end:
c5b9b441 814 BT_PACKET_MOVE_REF(notit->packet, packet);
312c056a
PP
815 return status;
816}
817
e98a2d6e 818static
d6e69534
PP
819enum bt_msg_iter_status after_packet_header_state(
820 struct bt_msg_iter *notit)
e98a2d6e 821{
d6e69534 822 enum bt_msg_iter_status status;
e98a2d6e
PP
823
824 status = set_current_stream_class(notit);
d6e69534 825 if (status != BT_MSG_ITER_STATUS_OK) {
312c056a 826 goto end;
e98a2d6e
PP
827 }
828
312c056a
PP
829 notit->state = STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN;
830
831end:
e98a2d6e
PP
832 return status;
833}
834
835static
d6e69534
PP
836enum bt_msg_iter_status read_packet_context_begin_state(
837 struct bt_msg_iter *notit)
e98a2d6e 838{
d6e69534 839 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
5cd6d0e5 840 struct ctf_field_class *packet_context_fc;
e98a2d6e 841
44c440bc 842 BT_ASSERT(notit->meta.sc);
5cd6d0e5
PP
843 packet_context_fc = notit->meta.sc->packet_context_fc;
844 if (!packet_context_fc) {
845 BT_LOGV("No packet packet context field class in stream class: continuing: "
fdf0e7a0 846 "notit-addr=%p, stream-class-addr=%p, "
44c440bc
PP
847 "stream-class-id=%" PRId64,
848 notit, notit->meta.sc,
849 notit->meta.sc->id);
835b2d10 850 notit->state = STATE_AFTER_STREAM_PACKET_CONTEXT;
e98a2d6e
PP
851 goto end;
852 }
853
312c056a 854 BT_ASSERT(!notit->packet_context_field);
44c440bc 855
5cd6d0e5 856 if (packet_context_fc->in_ir) {
44c440bc
PP
857 /*
858 * Create free packet context field from stream class.
859 * This field is going to be moved to the packet once we
860 * create it. We cannot create the packet now because a
861 * packet is created from a stream, and this API must be
83ebb7f1
PP
862 * able to return the packet context properties without
863 * creating a stream
864 * (bt_msg_iter_get_packet_properties()).
44c440bc
PP
865 */
866 notit->packet_context_field =
40f4ba76 867 bt_packet_context_field_create(
e5be10ef 868 notit->meta.sc->ir_sc);
44c440bc
PP
869 if (!notit->packet_context_field) {
870 BT_LOGE_STR("Cannot create packet context field wrapper from stream class.");
d6e69534 871 status = BT_MSG_ITER_STATUS_ERROR;
44c440bc
PP
872 goto end;
873 }
874
875 notit->dscopes.stream_packet_context =
40f4ba76 876 bt_packet_context_field_borrow_field(
e5be10ef 877 notit->packet_context_field);
44c440bc 878 BT_ASSERT(notit->dscopes.stream_packet_context);
312c056a
PP
879 }
880
fdf0e7a0
PP
881 BT_LOGV("Decoding packet context field: "
882 "notit-addr=%p, stream-class-addr=%p, "
5cd6d0e5 883 "stream-class-id=%" PRId64 ", fc-addr=%p",
44c440bc 884 notit, notit->meta.sc,
5cd6d0e5
PP
885 notit->meta.sc->id, packet_context_fc);
886 status = read_dscope_begin_state(notit, packet_context_fc,
fdf0e7a0
PP
887 STATE_AFTER_STREAM_PACKET_CONTEXT,
888 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE,
312c056a 889 notit->dscopes.stream_packet_context);
fdf0e7a0
PP
890 if (status < 0) {
891 BT_LOGW("Cannot decode packet context field: "
892 "notit-addr=%p, stream-class-addr=%p, "
5cd6d0e5 893 "stream-class-id=%" PRId64 ", fc-addr=%p",
44c440bc
PP
894 notit, notit->meta.sc,
895 notit->meta.sc->id,
5cd6d0e5 896 packet_context_fc);
fdf0e7a0 897 }
e98a2d6e
PP
898
899end:
e98a2d6e
PP
900 return status;
901}
902
903static
d6e69534
PP
904enum bt_msg_iter_status read_packet_context_continue_state(
905 struct bt_msg_iter *notit)
e98a2d6e
PP
906{
907 return read_dscope_continue_state(notit,
78586d8a 908 STATE_AFTER_STREAM_PACKET_CONTEXT);
e98a2d6e
PP
909}
910
78586d8a 911static
d6e69534
PP
912enum bt_msg_iter_status set_current_packet_content_sizes(
913 struct bt_msg_iter *notit)
e98a2d6e 914{
d6e69534 915 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
e98a2d6e 916
44c440bc
PP
917 if (notit->cur_exp_packet_total_size == -1) {
918 if (notit->cur_exp_packet_content_size != -1) {
919 BT_LOGW("Content size is set, but packet size is not: "
fdf0e7a0 920 "notit-addr=%p, packet-context-field-addr=%p, "
44c440bc 921 "packet-size=%" PRId64 ", content-size=%" PRId64,
fdf0e7a0 922 notit, notit->dscopes.stream_packet_context,
44c440bc
PP
923 notit->cur_exp_packet_total_size,
924 notit->cur_exp_packet_content_size);
d6e69534 925 status = BT_MSG_ITER_STATUS_ERROR;
e98a2d6e
PP
926 goto end;
927 }
e98a2d6e 928 } else {
44c440bc
PP
929 if (notit->cur_exp_packet_content_size == -1) {
930 notit->cur_exp_packet_content_size =
931 notit->cur_exp_packet_total_size;
932 }
e98a2d6e
PP
933 }
934
44c440bc
PP
935 if (notit->cur_exp_packet_content_size >
936 notit->cur_exp_packet_total_size) {
937 BT_LOGW("Invalid packet or content size: "
938 "content size is greater than packet size: "
fdf0e7a0 939 "notit-addr=%p, packet-context-field-addr=%p, "
44c440bc 940 "packet-size=%" PRId64 ", content-size=%" PRId64,
fdf0e7a0 941 notit, notit->dscopes.stream_packet_context,
44c440bc
PP
942 notit->cur_exp_packet_total_size,
943 notit->cur_exp_packet_content_size);
d6e69534 944 status = BT_MSG_ITER_STATUS_ERROR;
fdf0e7a0
PP
945 goto end;
946 }
947
fdf0e7a0
PP
948 BT_LOGV("Set current packet and content sizes: "
949 "notit-addr=%p, packet-size=%" PRIu64 ", content-size=%" PRIu64,
44c440bc
PP
950 notit, notit->cur_exp_packet_total_size,
951 notit->cur_exp_packet_content_size);
e98a2d6e 952end:
e98a2d6e
PP
953 return status;
954}
955
956static
d6e69534
PP
957enum bt_msg_iter_status after_packet_context_state(
958 struct bt_msg_iter *notit)
e98a2d6e 959{
d6e69534 960 enum bt_msg_iter_status status;
e98a2d6e
PP
961
962 status = set_current_packet_content_sizes(notit);
d6e69534 963 if (status != BT_MSG_ITER_STATUS_OK) {
e22b45d0
PP
964 goto end;
965 }
966
e22b45d0 967 if (notit->stream_begin_emitted) {
d6e69534 968 notit->state = STATE_EMIT_MSG_NEW_PACKET;
e22b45d0 969 } else {
d6e69534 970 notit->state = STATE_EMIT_MSG_NEW_STREAM;
e98a2d6e
PP
971 }
972
e22b45d0 973end:
e98a2d6e
PP
974 return status;
975}
976
977static
d6e69534
PP
978enum bt_msg_iter_status read_event_header_begin_state(
979 struct bt_msg_iter *notit)
e98a2d6e 980{
d6e69534 981 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
5cd6d0e5 982 struct ctf_field_class *event_header_fc = NULL;
e98a2d6e 983
174e773b
PP
984 /* Reset the position of the last event header */
985 notit->buf.last_eh_at = notit->buf.at;
44c440bc 986 notit->cur_event_class_id = -1;
174e773b 987
e98a2d6e 988 /* Check if we have some content left */
44c440bc
PP
989 if (notit->cur_exp_packet_content_size >= 0) {
990 if (unlikely(packet_at(notit) ==
991 notit->cur_exp_packet_content_size)) {
e98a2d6e 992 /* No more events! */
fdf0e7a0
PP
993 BT_LOGV("Reached end of packet: notit-addr=%p, "
994 "cur=%zu", notit, packet_at(notit));
d6e69534 995 notit->state = STATE_EMIT_MSG_END_OF_PACKET;
e98a2d6e 996 goto end;
44c440bc
PP
997 } else if (unlikely(packet_at(notit) >
998 notit->cur_exp_packet_content_size)) {
e98a2d6e 999 /* That's not supposed to happen */
fdf0e7a0 1000 BT_LOGV("Before decoding event header field: cursor is passed the packet's content: "
1974687e 1001 "notit-addr=%p, content-size=%" PRId64 ", "
44c440bc
PP
1002 "cur=%zu", notit,
1003 notit->cur_exp_packet_content_size,
fdf0e7a0 1004 packet_at(notit));
d6e69534 1005 status = BT_MSG_ITER_STATUS_ERROR;
e98a2d6e
PP
1006 goto end;
1007 }
44c440bc
PP
1008 } else {
1009 /*
1010 * "Infinite" content: we're done when the medium has
1011 * nothing else for us.
1012 */
1013 status = buf_ensure_available_bits(notit);
d6e69534 1014 if (status != BT_MSG_ITER_STATUS_OK) {
44c440bc
PP
1015 /*
1016 * If this function returns
d6e69534 1017 * `BT_MSG_ITER_STATUS_EOF`:
44c440bc 1018 *
d6e69534
PP
1019 * 1. bt_msg_iter_get_next_message()
1020 * emits a "packet end" message. This
44c440bc
PP
1021 * resets the current packet. The state
1022 * remains unchanged otherwise.
1023 * 2. This function is called again. It returns
d6e69534
PP
1024 * `BT_MSG_ITER_STATUS_EOF` again.
1025 * 3. bt_msg_iter_get_next_message()
1026 * emits a "stream end" message because
44c440bc
PP
1027 * there's no current packet. It sets the
1028 * current state to `STATE_DONE`.
1029 */
1030 goto end;
1031 }
e98a2d6e
PP
1032 }
1033
312c056a 1034 release_event_dscopes(notit);
44c440bc 1035 BT_ASSERT(notit->meta.sc);
5cd6d0e5
PP
1036 event_header_fc = notit->meta.sc->event_header_fc;
1037 if (!event_header_fc) {
44c440bc 1038 notit->state = STATE_AFTER_EVENT_HEADER;
e98a2d6e
PP
1039 goto end;
1040 }
1041
fdf0e7a0
PP
1042 BT_LOGV("Decoding event header field: "
1043 "notit-addr=%p, stream-class-addr=%p, "
44c440bc 1044 "stream-class-id=%" PRId64 ", "
5cd6d0e5 1045 "fc-addr=%p",
44c440bc
PP
1046 notit, notit->meta.sc,
1047 notit->meta.sc->id,
5cd6d0e5
PP
1048 event_header_fc);
1049 status = read_dscope_begin_state(notit, event_header_fc,
44c440bc 1050 STATE_AFTER_EVENT_HEADER,
83ebb7f1 1051 STATE_DSCOPE_EVENT_HEADER_CONTINUE, NULL);
fdf0e7a0
PP
1052 if (status < 0) {
1053 BT_LOGW("Cannot decode event header field: "
1054 "notit-addr=%p, stream-class-addr=%p, "
5cd6d0e5 1055 "stream-class-id=%" PRId64 ", fc-addr=%p",
44c440bc
PP
1056 notit, notit->meta.sc,
1057 notit->meta.sc->id,
5cd6d0e5 1058 event_header_fc);
fdf0e7a0 1059 }
e98a2d6e 1060
d1e46835 1061end:
e98a2d6e
PP
1062 return status;
1063}
1064
1065static
d6e69534
PP
1066enum bt_msg_iter_status read_event_header_continue_state(
1067 struct bt_msg_iter *notit)
e98a2d6e
PP
1068{
1069 return read_dscope_continue_state(notit,
44c440bc 1070 STATE_AFTER_EVENT_HEADER);
e98a2d6e
PP
1071}
1072
1073static inline
d6e69534 1074enum bt_msg_iter_status set_current_event_class(struct bt_msg_iter *notit)
e98a2d6e 1075{
d6e69534 1076 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
e98a2d6e 1077
44c440bc 1078 struct ctf_event_class *new_event_class = NULL;
e98a2d6e 1079
44c440bc 1080 if (notit->cur_event_class_id == -1) {
e98a2d6e 1081 /*
44c440bc
PP
1082 * No current event class ID field, therefore only one
1083 * event class.
e98a2d6e 1084 */
44c440bc
PP
1085 if (notit->meta.sc->event_classes->len != 1) {
1086 BT_LOGW("Need exactly one event class since there's "
1087 "no event class ID field: "
862ca4ed 1088 "notit-addr=%p", notit);
d6e69534 1089 status = BT_MSG_ITER_STATUS_ERROR;
44c440bc 1090 goto end;
fdf0e7a0 1091 }
e98a2d6e 1092
44c440bc
PP
1093 new_event_class = notit->meta.sc->event_classes->pdata[0];
1094 notit->cur_event_class_id = new_event_class->id;
e98a2d6e
PP
1095 }
1096
44c440bc
PP
1097 new_event_class = ctf_stream_class_borrow_event_class_by_id(
1098 notit->meta.sc, notit->cur_event_class_id);
1099 if (!new_event_class) {
fdf0e7a0 1100 BT_LOGW("No event class with ID of event class ID to use in stream class: "
44c440bc
PP
1101 "notit-addr=%p, stream-class-id=%" PRIu64 ", "
1102 "event-class-id=%" PRIu64 ", "
862ca4ed 1103 "trace-class-addr=%p",
44c440bc 1104 notit, notit->meta.sc->id, notit->cur_event_class_id,
862ca4ed 1105 notit->meta.tc);
d6e69534 1106 status = BT_MSG_ITER_STATUS_ERROR;
e98a2d6e
PP
1107 goto end;
1108 }
1109
44c440bc 1110 notit->meta.ec = new_event_class;
fdf0e7a0
PP
1111 BT_LOGV("Set current event class: "
1112 "notit-addr=%p, event-class-addr=%p, "
44c440bc
PP
1113 "event-class-id=%" PRId64 ", "
1114 "event-class-name=\"%s\"",
1115 notit, notit->meta.ec, notit->meta.ec->id,
1116 notit->meta.ec->name->str);
fdf0e7a0 1117
e98a2d6e 1118end:
e98a2d6e
PP
1119 return status;
1120}
1121
312c056a 1122static inline
d6e69534
PP
1123enum bt_msg_iter_status set_current_event_message(
1124 struct bt_msg_iter *notit)
312c056a 1125{
d6e69534
PP
1126 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
1127 bt_message *msg = NULL;
312c056a 1128
44c440bc 1129 BT_ASSERT(notit->meta.ec);
312c056a 1130 BT_ASSERT(notit->packet);
d6e69534 1131 BT_LOGV("Creating event message from event class and packet: "
312c056a 1132 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", packet-addr=%p",
44c440bc
PP
1133 notit, notit->meta.ec,
1134 notit->meta.ec->name->str,
312c056a 1135 notit->packet);
d6e69534
PP
1136 BT_ASSERT(notit->msg_iter);
1137 msg = bt_message_event_create(notit->msg_iter,
44c440bc 1138 notit->meta.ec->ir_ec, notit->packet);
d6e69534
PP
1139 if (!msg) {
1140 BT_LOGE("Cannot create event message: "
312c056a
PP
1141 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", "
1142 "packet-addr=%p",
44c440bc
PP
1143 notit, notit->meta.ec,
1144 notit->meta.ec->name->str,
312c056a
PP
1145 notit->packet);
1146 goto error;
1147 }
1148
1149 goto end;
1150
1151error:
d6e69534
PP
1152 BT_MESSAGE_PUT_REF_AND_RESET(msg);
1153 status = BT_MSG_ITER_STATUS_ERROR;
312c056a
PP
1154
1155end:
d6e69534 1156 BT_MESSAGE_MOVE_REF(notit->event_msg, msg);
312c056a
PP
1157 return status;
1158}
1159
e98a2d6e 1160static
d6e69534
PP
1161enum bt_msg_iter_status after_event_header_state(
1162 struct bt_msg_iter *notit)
e98a2d6e 1163{
d6e69534 1164 enum bt_msg_iter_status status;
e98a2d6e 1165
e98a2d6e 1166 status = set_current_event_class(notit);
d6e69534 1167 if (status != BT_MSG_ITER_STATUS_OK) {
e98a2d6e
PP
1168 goto end;
1169 }
1170
d6e69534
PP
1171 status = set_current_event_message(notit);
1172 if (status != BT_MSG_ITER_STATUS_OK) {
312c056a
PP
1173 goto end;
1174 }
1175
d6e69534
PP
1176 notit->event = bt_message_event_borrow_event(
1177 notit->event_msg);
312c056a 1178 BT_ASSERT(notit->event);
44c440bc 1179 notit->state = STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN;
e98a2d6e
PP
1180
1181end:
1182 return status;
1183}
1184
1185static
d6e69534
PP
1186enum bt_msg_iter_status read_event_common_context_begin_state(
1187 struct bt_msg_iter *notit)
e98a2d6e 1188{
d6e69534 1189 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
5cd6d0e5 1190 struct ctf_field_class *event_common_context_fc;
e98a2d6e 1191
5cd6d0e5
PP
1192 event_common_context_fc = notit->meta.sc->event_common_context_fc;
1193 if (!event_common_context_fc) {
44c440bc 1194 notit->state = STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN;
e98a2d6e
PP
1195 goto end;
1196 }
1197
5cd6d0e5 1198 if (event_common_context_fc->in_ir) {
44c440bc
PP
1199 BT_ASSERT(!notit->dscopes.event_common_context);
1200 notit->dscopes.event_common_context =
40f4ba76 1201 bt_event_borrow_common_context_field(
e5be10ef 1202 notit->event);
44c440bc
PP
1203 BT_ASSERT(notit->dscopes.event_common_context);
1204 }
1205
1206 BT_LOGV("Decoding event common context field: "
fdf0e7a0 1207 "notit-addr=%p, stream-class-addr=%p, "
44c440bc 1208 "stream-class-id=%" PRId64 ", "
5cd6d0e5 1209 "fc-addr=%p",
44c440bc
PP
1210 notit, notit->meta.sc,
1211 notit->meta.sc->id,
5cd6d0e5
PP
1212 event_common_context_fc);
1213 status = read_dscope_begin_state(notit, event_common_context_fc,
44c440bc
PP
1214 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN,
1215 STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE,
1216 notit->dscopes.event_common_context);
fdf0e7a0 1217 if (status < 0) {
44c440bc 1218 BT_LOGW("Cannot decode event common context field: "
fdf0e7a0 1219 "notit-addr=%p, stream-class-addr=%p, "
5cd6d0e5 1220 "stream-class-id=%" PRId64 ", fc-addr=%p",
44c440bc
PP
1221 notit, notit->meta.sc,
1222 notit->meta.sc->id,
5cd6d0e5 1223 event_common_context_fc);
fdf0e7a0 1224 }
e98a2d6e
PP
1225
1226end:
e98a2d6e
PP
1227 return status;
1228}
1229
1230static
d6e69534
PP
1231enum bt_msg_iter_status read_event_common_context_continue_state(
1232 struct bt_msg_iter *notit)
e98a2d6e
PP
1233{
1234 return read_dscope_continue_state(notit,
44c440bc 1235 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN);
e98a2d6e
PP
1236}
1237
1238static
d6e69534
PP
1239enum bt_msg_iter_status read_event_spec_context_begin_state(
1240 struct bt_msg_iter *notit)
e98a2d6e 1241{
d6e69534 1242 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
5cd6d0e5 1243 struct ctf_field_class *event_spec_context_fc;
e98a2d6e 1244
5cd6d0e5
PP
1245 event_spec_context_fc = notit->meta.ec->spec_context_fc;
1246 if (!event_spec_context_fc) {
835b2d10 1247 notit->state = STATE_DSCOPE_EVENT_PAYLOAD_BEGIN;
e98a2d6e
PP
1248 goto end;
1249 }
fdf0e7a0 1250
5cd6d0e5 1251 if (event_spec_context_fc->in_ir) {
44c440bc 1252 BT_ASSERT(!notit->dscopes.event_spec_context);
e5be10ef 1253 notit->dscopes.event_spec_context =
40f4ba76 1254 bt_event_borrow_specific_context_field(
e5be10ef 1255 notit->event);
44c440bc
PP
1256 BT_ASSERT(notit->dscopes.event_spec_context);
1257 }
1258
1259 BT_LOGV("Decoding event specific context field: "
fdf0e7a0
PP
1260 "notit-addr=%p, event-class-addr=%p, "
1261 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
5cd6d0e5 1262 "fc-addr=%p",
44c440bc
PP
1263 notit, notit->meta.ec,
1264 notit->meta.ec->name->str,
1265 notit->meta.ec->id,
5cd6d0e5
PP
1266 event_spec_context_fc);
1267 status = read_dscope_begin_state(notit, event_spec_context_fc,
e98a2d6e 1268 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN,
44c440bc
PP
1269 STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE,
1270 notit->dscopes.event_spec_context);
fdf0e7a0 1271 if (status < 0) {
44c440bc 1272 BT_LOGW("Cannot decode event specific context field: "
fdf0e7a0
PP
1273 "notit-addr=%p, event-class-addr=%p, "
1274 "event-class-name=\"%s\", "
5cd6d0e5 1275 "event-class-id=%" PRId64 ", fc-addr=%p",
44c440bc
PP
1276 notit, notit->meta.ec,
1277 notit->meta.ec->name->str,
1278 notit->meta.ec->id,
5cd6d0e5 1279 event_spec_context_fc);
fdf0e7a0 1280 }
e98a2d6e
PP
1281
1282end:
e98a2d6e
PP
1283 return status;
1284}
1285
1286static
d6e69534
PP
1287enum bt_msg_iter_status read_event_spec_context_continue_state(
1288 struct bt_msg_iter *notit)
e98a2d6e
PP
1289{
1290 return read_dscope_continue_state(notit,
1291 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN);
1292}
1293
1294static
d6e69534
PP
1295enum bt_msg_iter_status read_event_payload_begin_state(
1296 struct bt_msg_iter *notit)
e98a2d6e 1297{
d6e69534 1298 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
5cd6d0e5 1299 struct ctf_field_class *event_payload_fc;
e98a2d6e 1300
5cd6d0e5
PP
1301 event_payload_fc = notit->meta.ec->payload_fc;
1302 if (!event_payload_fc) {
d6e69534 1303 notit->state = STATE_EMIT_MSG_EVENT;
e98a2d6e
PP
1304 goto end;
1305 }
1306
5cd6d0e5 1307 if (event_payload_fc->in_ir) {
44c440bc 1308 BT_ASSERT(!notit->dscopes.event_payload);
e5be10ef 1309 notit->dscopes.event_payload =
40f4ba76 1310 bt_event_borrow_payload_field(
e5be10ef 1311 notit->event);
44c440bc
PP
1312 BT_ASSERT(notit->dscopes.event_payload);
1313 }
1314
fdf0e7a0
PP
1315 BT_LOGV("Decoding event payload field: "
1316 "notit-addr=%p, event-class-addr=%p, "
1317 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
5cd6d0e5 1318 "fc-addr=%p",
44c440bc
PP
1319 notit, notit->meta.ec,
1320 notit->meta.ec->name->str,
1321 notit->meta.ec->id,
5cd6d0e5
PP
1322 event_payload_fc);
1323 status = read_dscope_begin_state(notit, event_payload_fc,
d6e69534 1324 STATE_EMIT_MSG_EVENT,
e98a2d6e 1325 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE,
312c056a 1326 notit->dscopes.event_payload);
fdf0e7a0
PP
1327 if (status < 0) {
1328 BT_LOGW("Cannot decode event payload field: "
1329 "notit-addr=%p, event-class-addr=%p, "
1330 "event-class-name=\"%s\", "
5cd6d0e5 1331 "event-class-id=%" PRId64 ", fc-addr=%p",
44c440bc
PP
1332 notit, notit->meta.ec,
1333 notit->meta.ec->name->str,
1334 notit->meta.ec->id,
5cd6d0e5 1335 event_payload_fc);
fdf0e7a0 1336 }
e98a2d6e
PP
1337
1338end:
e98a2d6e
PP
1339 return status;
1340}
1341
1342static
d6e69534
PP
1343enum bt_msg_iter_status read_event_payload_continue_state(
1344 struct bt_msg_iter *notit)
e98a2d6e 1345{
d6e69534 1346 return read_dscope_continue_state(notit, STATE_EMIT_MSG_EVENT);
e98a2d6e
PP
1347}
1348
1349static
d6e69534
PP
1350enum bt_msg_iter_status skip_packet_padding_state(
1351 struct bt_msg_iter *notit)
e98a2d6e 1352{
d6e69534 1353 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
e98a2d6e
PP
1354 size_t bits_to_skip;
1355
44c440bc
PP
1356 BT_ASSERT(notit->cur_exp_packet_total_size > 0);
1357 bits_to_skip = notit->cur_exp_packet_total_size - packet_at(notit);
e98a2d6e
PP
1358 if (bits_to_skip == 0) {
1359 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1360 goto end;
1361 } else {
1362 size_t bits_to_consume;
fdf0e7a0
PP
1363
1364 BT_LOGV("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu",
1365 bits_to_skip, notit, bits_to_skip);
e98a2d6e 1366 status = buf_ensure_available_bits(notit);
d6e69534 1367 if (status != BT_MSG_ITER_STATUS_OK) {
e98a2d6e
PP
1368 goto end;
1369 }
1370
1371 bits_to_consume = MIN(buf_available_bits(notit), bits_to_skip);
fdf0e7a0
PP
1372 BT_LOGV("Skipping %zu bits of padding: notit-addr=%p, size=%zu",
1373 bits_to_consume, notit, bits_to_consume);
e98a2d6e 1374 buf_consume_bits(notit, bits_to_consume);
44c440bc
PP
1375 bits_to_skip = notit->cur_exp_packet_total_size -
1376 packet_at(notit);
e98a2d6e
PP
1377 if (bits_to_skip == 0) {
1378 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1379 goto end;
1380 }
1381 }
1382
1383end:
1384 return status;
1385}
1386
1387static inline
d6e69534 1388enum bt_msg_iter_status handle_state(struct bt_msg_iter *notit)
e98a2d6e 1389{
d6e69534 1390 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
fdf0e7a0 1391 const enum state state = notit->state;
e98a2d6e 1392
fdf0e7a0
PP
1393 BT_LOGV("Handling state: notit-addr=%p, state=%s",
1394 notit, state_string(state));
e98a2d6e
PP
1395
1396 // TODO: optimalize!
fdf0e7a0 1397 switch (state) {
e98a2d6e
PP
1398 case STATE_INIT:
1399 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1400 break;
1401 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
1402 status = read_packet_header_begin_state(notit);
1403 break;
1404 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
1405 status = read_packet_header_continue_state(notit);
1406 break;
1407 case STATE_AFTER_TRACE_PACKET_HEADER:
1408 status = after_packet_header_state(notit);
1409 break;
1410 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN:
1411 status = read_packet_context_begin_state(notit);
1412 break;
1413 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE:
1414 status = read_packet_context_continue_state(notit);
1415 break;
1416 case STATE_AFTER_STREAM_PACKET_CONTEXT:
1417 status = after_packet_context_state(notit);
1418 break;
d6e69534
PP
1419 case STATE_EMIT_MSG_NEW_STREAM:
1420 notit->state = STATE_EMIT_MSG_NEW_PACKET;
f42867e2 1421 break;
d6e69534 1422 case STATE_EMIT_MSG_NEW_PACKET:
44c440bc 1423 notit->state = STATE_DSCOPE_EVENT_HEADER_BEGIN;
e98a2d6e 1424 break;
44c440bc 1425 case STATE_DSCOPE_EVENT_HEADER_BEGIN:
e98a2d6e
PP
1426 status = read_event_header_begin_state(notit);
1427 break;
44c440bc 1428 case STATE_DSCOPE_EVENT_HEADER_CONTINUE:
e98a2d6e
PP
1429 status = read_event_header_continue_state(notit);
1430 break;
44c440bc 1431 case STATE_AFTER_EVENT_HEADER:
e98a2d6e
PP
1432 status = after_event_header_state(notit);
1433 break;
44c440bc
PP
1434 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN:
1435 status = read_event_common_context_begin_state(notit);
e98a2d6e 1436 break;
44c440bc
PP
1437 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE:
1438 status = read_event_common_context_continue_state(notit);
e98a2d6e 1439 break;
44c440bc
PP
1440 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN:
1441 status = read_event_spec_context_begin_state(notit);
e98a2d6e 1442 break;
44c440bc
PP
1443 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE:
1444 status = read_event_spec_context_continue_state(notit);
e98a2d6e
PP
1445 break;
1446 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN:
1447 status = read_event_payload_begin_state(notit);
1448 break;
1449 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE:
1450 status = read_event_payload_continue_state(notit);
1451 break;
d6e69534 1452 case STATE_EMIT_MSG_EVENT:
44c440bc 1453 notit->state = STATE_DSCOPE_EVENT_HEADER_BEGIN;
e98a2d6e
PP
1454 break;
1455 case STATE_SKIP_PACKET_PADDING:
1456 status = skip_packet_padding_state(notit);
1457 break;
d6e69534 1458 case STATE_EMIT_MSG_END_OF_PACKET:
e98a2d6e
PP
1459 notit->state = STATE_SKIP_PACKET_PADDING;
1460 break;
fdf0e7a0 1461 default:
d6e69534 1462 BT_LOGD("Unknown CTF plugin message iterator state: "
fdf0e7a0
PP
1463 "notit-addr=%p, state=%d", notit, notit->state);
1464 abort();
e98a2d6e
PP
1465 }
1466
fdf0e7a0
PP
1467 BT_LOGV("Handled state: notit-addr=%p, status=%s, "
1468 "prev-state=%s, cur-state=%s",
d6e69534 1469 notit, bt_msg_iter_status_string(status),
fdf0e7a0 1470 state_string(state), state_string(notit->state));
e98a2d6e
PP
1471 return status;
1472}
1473
2cf1d51e 1474/**
d6e69534 1475 * Resets the internal state of a CTF message iterator.
2cf1d51e 1476 */
f42867e2 1477BT_HIDDEN
d6e69534 1478void bt_msg_iter_reset(struct bt_msg_iter *notit)
e98a2d6e 1479{
f6ccaed9 1480 BT_ASSERT(notit);
d6e69534 1481 BT_LOGD("Resetting message iterator: addr=%p", notit);
e98a2d6e 1482 stack_clear(notit->stack);
44c440bc
PP
1483 notit->meta.sc = NULL;
1484 notit->meta.ec = NULL;
c5b9b441
PP
1485 BT_PACKET_PUT_REF_AND_RESET(notit->packet);
1486 BT_STREAM_PUT_REF_AND_RESET(notit->stream);
d6e69534 1487 BT_MESSAGE_PUT_REF_AND_RESET(notit->event_msg);
312c056a
PP
1488 release_all_dscopes(notit);
1489 notit->cur_dscope_field = NULL;
1490
312c056a 1491 if (notit->packet_context_field) {
40f4ba76 1492 bt_packet_context_field_release(notit->packet_context_field);
312c056a
PP
1493 notit->packet_context_field = NULL;
1494 }
1495
e98a2d6e
PP
1496 notit->buf.addr = NULL;
1497 notit->buf.sz = 0;
1498 notit->buf.at = 0;
2b186c3e 1499 notit->buf.last_eh_at = SIZE_MAX;
e98a2d6e
PP
1500 notit->buf.packet_offset = 0;
1501 notit->state = STATE_INIT;
44c440bc
PP
1502 notit->cur_exp_packet_content_size = -1;
1503 notit->cur_exp_packet_total_size = -1;
9e0c8dbb 1504 notit->cur_packet_offset = -1;
44c440bc
PP
1505 notit->cur_stream_class_id = -1;
1506 notit->cur_event_class_id = -1;
1507 notit->cur_data_stream_id = -1;
f42867e2 1508 notit->stream_begin_emitted = false;
e98a2d6e
PP
1509}
1510
2cf1d51e 1511static
d6e69534 1512int bt_msg_iter_switch_packet(struct bt_msg_iter *notit)
2cf1d51e
JG
1513{
1514 int ret = 0;
1515
115de887
PP
1516 /*
1517 * We don't put the stream class here because we need to make
d6e69534 1518 * sure that all the packets processed by the same message
115de887
PP
1519 * iterator refer to the same stream class (the first one).
1520 */
f6ccaed9 1521 BT_ASSERT(notit);
312c056a 1522
44c440bc
PP
1523 if (notit->cur_exp_packet_total_size != -1) {
1524 notit->cur_packet_offset += notit->cur_exp_packet_total_size;
9e0c8dbb 1525 }
312c056a 1526
9e0c8dbb
JG
1527 BT_LOGV("Switching packet: notit-addr=%p, cur=%zu, "
1528 "packet-offset=%" PRId64, notit, notit->buf.at,
1529 notit->cur_packet_offset);
2cf1d51e 1530 stack_clear(notit->stack);
44c440bc 1531 notit->meta.ec = NULL;
c5b9b441 1532 BT_PACKET_PUT_REF_AND_RESET(notit->packet);
d6e69534 1533 BT_MESSAGE_PUT_REF_AND_RESET(notit->event_msg);
312c056a
PP
1534 release_all_dscopes(notit);
1535 notit->cur_dscope_field = NULL;
2cf1d51e
JG
1536
1537 /*
1538 * Adjust current buffer so that addr points to the beginning of the new
1539 * packet.
1540 */
1541 if (notit->buf.addr) {
1542 size_t consumed_bytes = (size_t) (notit->buf.at / CHAR_BIT);
1543
1544 /* Packets are assumed to start on a byte frontier. */
1545 if (notit->buf.at % CHAR_BIT) {
fdf0e7a0
PP
1546 BT_LOGW("Cannot switch packet: current position is not a multiple of 8: "
1547 "notit-addr=%p, cur=%zu", notit, notit->buf.at);
2cf1d51e
JG
1548 ret = -1;
1549 goto end;
1550 }
1551
1552 notit->buf.addr += consumed_bytes;
1553 notit->buf.sz -= consumed_bytes;
1554 notit->buf.at = 0;
1555 notit->buf.packet_offset = 0;
fdf0e7a0
PP
1556 BT_LOGV("Adjusted buffer: addr=%p, size=%zu",
1557 notit->buf.addr, notit->buf.sz);
2cf1d51e
JG
1558 }
1559
44c440bc
PP
1560 notit->cur_exp_packet_content_size = -1;
1561 notit->cur_exp_packet_total_size = -1;
1562 notit->cur_stream_class_id = -1;
1563 notit->cur_event_class_id = -1;
1564 notit->cur_data_stream_id = -1;
1565 notit->snapshots.discarded_events = UINT64_C(-1);
1566 notit->snapshots.packets = UINT64_C(-1);
1567 notit->snapshots.beginning_clock = UINT64_C(-1);
1568 notit->snapshots.end_clock = UINT64_C(-1);
fdf0e7a0 1569
2cf1d51e
JG
1570end:
1571 return ret;
1572}
1573
e98a2d6e 1574static
d6e69534 1575bt_field *borrow_next_field(struct bt_msg_iter *notit)
e98a2d6e 1576{
b19ff26f
PP
1577 bt_field *next_field = NULL;
1578 bt_field *base_field;
1579 const bt_field_class *base_fc;
e98a2d6e
PP
1580 size_t index;
1581
f6ccaed9 1582 BT_ASSERT(!stack_empty(notit->stack));
e98a2d6e
PP
1583 index = stack_top(notit->stack)->index;
1584 base_field = stack_top(notit->stack)->base;
f6ccaed9 1585 BT_ASSERT(base_field);
40f4ba76 1586 base_fc = bt_field_borrow_class_const(base_field);
5cd6d0e5 1587 BT_ASSERT(base_fc);
e98a2d6e 1588
40f4ba76 1589 switch (bt_field_class_get_type(base_fc)) {
864cad70 1590 case BT_FIELD_CLASS_TYPE_STRUCTURE:
f42867e2 1591 {
44c440bc 1592 BT_ASSERT(index <
5cd6d0e5 1593 bt_field_class_structure_get_member_count(
40f4ba76
PP
1594 bt_field_borrow_class_const(
1595 base_field)));
e5be10ef 1596 next_field =
40f4ba76 1597 bt_field_structure_borrow_member_field_by_index(
e5be10ef 1598 base_field, index);
e98a2d6e 1599 break;
f42867e2 1600 }
864cad70
PP
1601 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
1602 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
40f4ba76
PP
1603 BT_ASSERT(index < bt_field_array_get_length(base_field));
1604 next_field = bt_field_array_borrow_element_field_by_index(
44c440bc 1605 base_field, index);
e98a2d6e 1606 break;
864cad70 1607 case BT_FIELD_CLASS_TYPE_VARIANT:
44c440bc 1608 BT_ASSERT(index == 0);
40f4ba76 1609 next_field = bt_field_variant_borrow_selected_option_field(
44c440bc 1610 base_field);
e98a2d6e
PP
1611 break;
1612 default:
0fbb9a9f 1613 abort();
e98a2d6e
PP
1614 }
1615
44c440bc 1616 BT_ASSERT(next_field);
e98a2d6e
PP
1617 return next_field;
1618}
1619
c44c3e70 1620static
d6e69534 1621void update_default_clock(struct bt_msg_iter *notit, uint64_t new_val,
44c440bc 1622 uint64_t new_val_size)
c44c3e70 1623{
44c440bc 1624 uint64_t new_val_mask;
c44c3e70 1625 uint64_t cur_value_masked;
c44c3e70 1626
44c440bc 1627 BT_ASSERT(new_val_size > 0);
c44c3e70
JG
1628
1629 /*
1630 * Special case for a 64-bit new value, which is the limit
1631 * of a clock value as of this version: overwrite the
1632 * current value directly.
1633 */
44c440bc 1634 if (new_val_size == 64) {
605e1019 1635 notit->default_clock_snapshot = new_val;
c44c3e70
JG
1636 goto end;
1637 }
1638
44c440bc 1639 new_val_mask = (1ULL << new_val_size) - 1;
605e1019 1640 cur_value_masked = notit->default_clock_snapshot & new_val_mask;
c44c3e70 1641
44c440bc 1642 if (new_val < cur_value_masked) {
c44c3e70
JG
1643 /*
1644 * It looks like a wrap happened on the number of bits
1645 * of the requested new value. Assume that the clock
1646 * value wrapped only one time.
1647 */
605e1019 1648 notit->default_clock_snapshot += new_val_mask + 1;
c44c3e70
JG
1649 }
1650
1651 /* Clear the low bits of the current clock value. */
605e1019 1652 notit->default_clock_snapshot &= ~new_val_mask;
c44c3e70
JG
1653
1654 /* Set the low bits of the current clock value. */
605e1019 1655 notit->default_clock_snapshot |= new_val;
d1e46835 1656
c44c3e70 1657end:
44c440bc 1658 BT_LOGV("Updated default clock's value from integer field's value: "
605e1019 1659 "value=%" PRIu64, notit->default_clock_snapshot);
c44c3e70
JG
1660}
1661
1662static
5cd6d0e5
PP
1663enum bt_bfcr_status bfcr_unsigned_int_cb(uint64_t value,
1664 struct ctf_field_class *fc, void *data)
c44c3e70 1665{
d6e69534 1666 struct bt_msg_iter *notit = data;
5cd6d0e5 1667 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
b19ff26f 1668 bt_field *field = NULL;
5cd6d0e5 1669 struct ctf_field_class_int *int_fc = (void *) fc;
c44c3e70 1670
5cd6d0e5
PP
1671 BT_LOGV("Unsigned integer function called from BFCR: "
1672 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
864cad70
PP
1673 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64,
1674 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
312c056a 1675
5cd6d0e5 1676 if (likely(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE)) {
44c440bc 1677 goto update_def_clock;
c44c3e70
JG
1678 }
1679
5cd6d0e5
PP
1680 switch (int_fc->meaning) {
1681 case CTF_FIELD_CLASS_MEANING_EVENT_CLASS_ID:
44c440bc
PP
1682 notit->cur_event_class_id = value;
1683 break;
5cd6d0e5 1684 case CTF_FIELD_CLASS_MEANING_DATA_STREAM_ID:
44c440bc
PP
1685 notit->cur_data_stream_id = value;
1686 break;
5cd6d0e5 1687 case CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME:
44c440bc
PP
1688 notit->snapshots.beginning_clock = value;
1689 break;
5cd6d0e5 1690 case CTF_FIELD_CLASS_MEANING_PACKET_END_TIME:
44c440bc
PP
1691 notit->snapshots.end_clock = value;
1692 break;
5cd6d0e5 1693 case CTF_FIELD_CLASS_MEANING_STREAM_CLASS_ID:
44c440bc
PP
1694 notit->cur_stream_class_id = value;
1695 break;
5cd6d0e5 1696 case CTF_FIELD_CLASS_MEANING_MAGIC:
44c440bc
PP
1697 if (value != 0xc1fc1fc1) {
1698 BT_LOGW("Invalid CTF magic number: notit-addr=%p, "
1699 "magic=%" PRIx64, notit, value);
5cd6d0e5 1700 status = BT_BFCR_STATUS_ERROR;
c44c3e70
JG
1701 goto end;
1702 }
fdf0e7a0 1703
44c440bc 1704 break;
5cd6d0e5 1705 case CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT:
44c440bc
PP
1706 notit->snapshots.packets = value;
1707 break;
5cd6d0e5 1708 case CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT:
44c440bc
PP
1709 notit->snapshots.discarded_events = value;
1710 break;
5cd6d0e5 1711 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_TOTAL_SIZE:
44c440bc
PP
1712 notit->cur_exp_packet_total_size = value;
1713 break;
5cd6d0e5 1714 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_CONTENT_SIZE:
44c440bc
PP
1715 notit->cur_exp_packet_content_size = value;
1716 break;
1717 default:
1718 abort();
c44c3e70
JG
1719 }
1720
44c440bc 1721update_def_clock:
5cd6d0e5
PP
1722 if (unlikely(int_fc->mapped_clock_class)) {
1723 update_default_clock(notit, value, int_fc->base.size);
44c440bc 1724 }
c44c3e70 1725
5cd6d0e5 1726 if (unlikely(int_fc->storing_index >= 0)) {
44c440bc 1727 g_array_index(notit->stored_values, uint64_t,
5cd6d0e5 1728 (uint64_t) int_fc->storing_index) = value;
44c440bc 1729 }
e98a2d6e 1730
5cd6d0e5 1731 if (unlikely(!fc->in_ir)) {
312c056a 1732 goto end;
e98a2d6e
PP
1733 }
1734
44c440bc
PP
1735 field = borrow_next_field(notit);
1736 BT_ASSERT(field);
40f4ba76
PP
1737 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
1738 BT_ASSERT(bt_field_get_class_type(field) ==
1739 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER ||
1740 bt_field_get_class_type(field) ==
1741 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION);
1742 bt_field_unsigned_integer_set_value(field, value);
e98a2d6e 1743 stack_top(notit->stack)->index++;
fdf0e7a0 1744
312c056a 1745end:
e98a2d6e
PP
1746 return status;
1747}
1748
5f870343 1749static
5cd6d0e5
PP
1750enum bt_bfcr_status bfcr_unsigned_int_char_cb(uint64_t value,
1751 struct ctf_field_class *fc, void *data)
5f870343 1752{
44c440bc 1753 int ret;
d6e69534 1754 struct bt_msg_iter *notit = data;
5cd6d0e5 1755 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
b19ff26f 1756 bt_field *string_field = NULL;
5cd6d0e5 1757 struct ctf_field_class_int *int_fc = (void *) fc;
44c440bc 1758 char str[2] = {'\0', '\0'};
5f870343 1759
5cd6d0e5
PP
1760 BT_LOGV("Unsigned integer character function called from BFCR: "
1761 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
864cad70
PP
1762 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64,
1763 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
5cd6d0e5
PP
1764 BT_ASSERT(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE);
1765 BT_ASSERT(!int_fc->mapped_clock_class);
1766 BT_ASSERT(int_fc->storing_index < 0);
312c056a 1767
5cd6d0e5 1768 if (unlikely(!fc->in_ir)) {
44c440bc
PP
1769 goto end;
1770 }
5f870343 1771
44c440bc 1772 if (notit->done_filling_string) {
5f870343
JG
1773 goto end;
1774 }
1775
44c440bc
PP
1776 if (value == 0) {
1777 notit->done_filling_string = true;
5f870343
JG
1778 goto end;
1779 }
1780
44c440bc 1781 string_field = stack_top(notit->stack)->base;
40f4ba76
PP
1782 BT_ASSERT(bt_field_get_class_type(string_field) ==
1783 BT_FIELD_CLASS_TYPE_STRING);
44c440bc
PP
1784
1785 /* Append character */
1786 str[0] = (char) value;
40f4ba76 1787 ret = bt_field_string_append_with_length(string_field, str, 1);
44c440bc
PP
1788 if (ret) {
1789 BT_LOGE("Cannot append character to string field's value: "
1790 "notit-addr=%p, field-addr=%p, ret=%d",
1791 notit, string_field, ret);
5cd6d0e5 1792 status = BT_BFCR_STATUS_ERROR;
44c440bc
PP
1793 goto end;
1794 }
312c056a 1795
5f870343
JG
1796end:
1797 return status;
1798}
1799
1800static
5cd6d0e5
PP
1801enum bt_bfcr_status bfcr_signed_int_cb(int64_t value,
1802 struct ctf_field_class *fc, void *data)
e98a2d6e 1803{
5cd6d0e5 1804 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
b19ff26f 1805 bt_field *field = NULL;
d6e69534 1806 struct bt_msg_iter *notit = data;
5cd6d0e5 1807 struct ctf_field_class_int *int_fc = (void *) fc;
e98a2d6e 1808
5cd6d0e5
PP
1809 BT_LOGV("Signed integer function called from BFCR: "
1810 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
864cad70
PP
1811 "fc-type=%d, fc-in-ir=%d, value=%" PRId64,
1812 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
5cd6d0e5 1813 BT_ASSERT(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE);
44c440bc 1814
5cd6d0e5 1815 if (unlikely(int_fc->storing_index >= 0)) {
44c440bc 1816 g_array_index(notit->stored_values, uint64_t,
5cd6d0e5 1817 (uint64_t) int_fc->storing_index) = (uint64_t) value;
44c440bc
PP
1818 }
1819
5cd6d0e5 1820 if (unlikely(!fc->in_ir)) {
312c056a 1821 goto end;
e98a2d6e
PP
1822 }
1823
44c440bc
PP
1824 field = borrow_next_field(notit);
1825 BT_ASSERT(field);
40f4ba76
PP
1826 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
1827 BT_ASSERT(bt_field_get_class_type(field) ==
1828 BT_FIELD_CLASS_TYPE_SIGNED_INTEGER ||
1829 bt_field_get_class_type(field) ==
1830 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION);
1831 bt_field_signed_integer_set_value(field, value);
e98a2d6e 1832 stack_top(notit->stack)->index++;
fdf0e7a0 1833
312c056a 1834end:
e98a2d6e
PP
1835 return status;
1836}
1837
1838static
5cd6d0e5
PP
1839enum bt_bfcr_status bfcr_floating_point_cb(double value,
1840 struct ctf_field_class *fc, void *data)
e98a2d6e 1841{
5cd6d0e5 1842 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
b19ff26f 1843 bt_field *field = NULL;
d6e69534 1844 struct bt_msg_iter *notit = data;
e98a2d6e 1845
5cd6d0e5
PP
1846 BT_LOGV("Floating point number function called from BFCR: "
1847 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
864cad70
PP
1848 "fc-type=%d, fc-in-ir=%d, value=%f",
1849 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
41693723
PP
1850
1851 if (unlikely(!fc->in_ir)) {
1852 goto end;
1853 }
1854
312c056a 1855 field = borrow_next_field(notit);
44c440bc 1856 BT_ASSERT(field);
40f4ba76
PP
1857 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
1858 BT_ASSERT(bt_field_get_class_type(field) ==
1859 BT_FIELD_CLASS_TYPE_REAL);
1860 bt_field_real_set_value(field, value);
e98a2d6e 1861 stack_top(notit->stack)->index++;
41693723
PP
1862
1863end:
e98a2d6e
PP
1864 return status;
1865}
1866
1867static
5cd6d0e5
PP
1868enum bt_bfcr_status bfcr_string_begin_cb(
1869 struct ctf_field_class *fc, void *data)
e98a2d6e 1870{
b19ff26f 1871 bt_field *field = NULL;
d6e69534 1872 struct bt_msg_iter *notit = data;
e98a2d6e
PP
1873 int ret;
1874
5cd6d0e5
PP
1875 BT_LOGV("String (beginning) function called from BFCR: "
1876 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
864cad70
PP
1877 "fc-type=%d, fc-in-ir=%d",
1878 notit, notit->bfcr, fc, fc->type, fc->in_ir);
e98a2d6e 1879
41693723
PP
1880 if (unlikely(!fc->in_ir)) {
1881 goto end;
1882 }
1883
44c440bc
PP
1884 field = borrow_next_field(notit);
1885 BT_ASSERT(field);
40f4ba76
PP
1886 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
1887 BT_ASSERT(bt_field_get_class_type(field) ==
1888 BT_FIELD_CLASS_TYPE_STRING);
1889 ret = bt_field_string_clear(field);
312c056a
PP
1890 BT_ASSERT(ret == 0);
1891
e98a2d6e 1892 /*
5cd6d0e5
PP
1893 * Push on stack. Not a compound class per se, but we know that
1894 * only bfcr_string_cb() may be called between this call and a
1895 * subsequent call to bfcr_string_end_cb().
e98a2d6e 1896 */
44c440bc 1897 stack_push(notit->stack, field);
41693723
PP
1898
1899end:
5cd6d0e5 1900 return BT_BFCR_STATUS_OK;
e98a2d6e
PP
1901}
1902
1903static
5cd6d0e5
PP
1904enum bt_bfcr_status bfcr_string_cb(const char *value,
1905 size_t len, struct ctf_field_class *fc, void *data)
e98a2d6e 1906{
5cd6d0e5 1907 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
b19ff26f 1908 bt_field *field = NULL;
d6e69534 1909 struct bt_msg_iter *notit = data;
e98a2d6e
PP
1910 int ret;
1911
5cd6d0e5
PP
1912 BT_LOGV("String (substring) function called from BFCR: "
1913 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
864cad70
PP
1914 "fc-type=%d, fc-in-ir=%d, string-length=%zu",
1915 notit, notit->bfcr, fc, fc->type, fc->in_ir,
fdf0e7a0 1916 len);
41693723
PP
1917
1918 if (unlikely(!fc->in_ir)) {
1919 goto end;
1920 }
1921
e98a2d6e 1922 field = stack_top(notit->stack)->base;
f6ccaed9 1923 BT_ASSERT(field);
e98a2d6e 1924
312c056a 1925 /* Append current substring */
40f4ba76 1926 ret = bt_field_string_append_with_length(field, value, len);
e98a2d6e 1927 if (ret) {
fdf0e7a0
PP
1928 BT_LOGE("Cannot append substring to string field's value: "
1929 "notit-addr=%p, field-addr=%p, string-length=%zu, "
1930 "ret=%d", notit, field, len, ret);
5cd6d0e5 1931 status = BT_BFCR_STATUS_ERROR;
e98a2d6e
PP
1932 goto end;
1933 }
1934
1935end:
1936 return status;
1937}
1938
1939static
5cd6d0e5
PP
1940enum bt_bfcr_status bfcr_string_end_cb(
1941 struct ctf_field_class *fc, void *data)
e98a2d6e 1942{
d6e69534 1943 struct bt_msg_iter *notit = data;
e98a2d6e 1944
5cd6d0e5
PP
1945 BT_LOGV("String (end) function called from BFCR: "
1946 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
864cad70
PP
1947 "fc-type=%d, fc-in-ir=%d",
1948 notit, notit->bfcr, fc, fc->type, fc->in_ir);
41693723
PP
1949
1950 if (unlikely(!fc->in_ir)) {
1951 goto end;
1952 }
fdf0e7a0 1953
e98a2d6e
PP
1954 /* Pop string field */
1955 stack_pop(notit->stack);
1956
1957 /* Go to next field */
1958 stack_top(notit->stack)->index++;
41693723
PP
1959
1960end:
5cd6d0e5 1961 return BT_BFCR_STATUS_OK;
e98a2d6e
PP
1962}
1963
5cd6d0e5
PP
1964enum bt_bfcr_status bfcr_compound_begin_cb(
1965 struct ctf_field_class *fc, void *data)
e98a2d6e 1966{
d6e69534 1967 struct bt_msg_iter *notit = data;
b19ff26f 1968 bt_field *field;
e98a2d6e 1969
5cd6d0e5
PP
1970 BT_LOGV("Compound (beginning) function called from BFCR: "
1971 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
864cad70
PP
1972 "fc-type=%d, fc-in-ir=%d",
1973 notit, notit->bfcr, fc, fc->type, fc->in_ir);
44c440bc 1974
5cd6d0e5 1975 if (!fc->in_ir) {
44c440bc
PP
1976 goto end;
1977 }
fdf0e7a0 1978
312c056a 1979 /* Borrow field */
e98a2d6e 1980 if (stack_empty(notit->stack)) {
312c056a
PP
1981 /* Root: already set by read_dscope_begin_state() */
1982 field = notit->cur_dscope_field;
e98a2d6e 1983 } else {
312c056a 1984 field = borrow_next_field(notit);
44c440bc 1985 BT_ASSERT(field);
e98a2d6e
PP
1986 }
1987
1988 /* Push field */
f6ccaed9 1989 BT_ASSERT(field);
40f4ba76 1990 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
44c440bc
PP
1991 stack_push(notit->stack, field);
1992
1993 /*
5cd6d0e5 1994 * Change BFCR "unsigned int" callback if it's a text
44c440bc
PP
1995 * array/sequence.
1996 */
864cad70
PP
1997 if (fc->type == CTF_FIELD_CLASS_TYPE_ARRAY ||
1998 fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE) {
5cd6d0e5 1999 struct ctf_field_class_array_base *array_fc = (void *) fc;
44c440bc 2000
5cd6d0e5 2001 if (array_fc->is_text) {
44c440bc
PP
2002 int ret;
2003
40f4ba76
PP
2004 BT_ASSERT(bt_field_get_class_type(field) ==
2005 BT_FIELD_CLASS_TYPE_STRING);
44c440bc 2006 notit->done_filling_string = false;
40f4ba76 2007 ret = bt_field_string_clear(field);
44c440bc 2008 BT_ASSERT(ret == 0);
5cd6d0e5
PP
2009 bt_bfcr_set_unsigned_int_cb(notit->bfcr,
2010 bfcr_unsigned_int_char_cb);
44c440bc 2011 }
e98a2d6e
PP
2012 }
2013
2014end:
5cd6d0e5 2015 return BT_BFCR_STATUS_OK;
e98a2d6e
PP
2016}
2017
5cd6d0e5
PP
2018enum bt_bfcr_status bfcr_compound_end_cb(
2019 struct ctf_field_class *fc, void *data)
e98a2d6e 2020{
d6e69534 2021 struct bt_msg_iter *notit = data;
e98a2d6e 2022
5cd6d0e5
PP
2023 BT_LOGV("Compound (end) function called from BFCR: "
2024 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
864cad70
PP
2025 "fc-type=%d, fc-in-ir=%d",
2026 notit, notit->bfcr, fc, fc->type, fc->in_ir);
e98a2d6e 2027
5cd6d0e5 2028 if (!fc->in_ir) {
e98a2d6e
PP
2029 goto end;
2030 }
2031
44c440bc 2032 BT_ASSERT(!stack_empty(notit->stack));
40f4ba76 2033 BT_ASSERT(bt_field_borrow_class_const(stack_top(notit->stack)->base) ==
5cd6d0e5 2034 fc->ir_fc);
e98a2d6e 2035
44c440bc 2036 /*
5cd6d0e5 2037 * Reset BFCR "unsigned int" callback if it's a text
44c440bc
PP
2038 * array/sequence.
2039 */
864cad70
PP
2040 if (fc->type == CTF_FIELD_CLASS_TYPE_ARRAY ||
2041 fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE) {
5cd6d0e5 2042 struct ctf_field_class_array_base *array_fc = (void *) fc;
44c440bc 2043
5cd6d0e5 2044 if (array_fc->is_text) {
864cad70 2045 BT_ASSERT(bt_field_get_class_type(
40f4ba76 2046 stack_top(notit->stack)->base) ==
e5be10ef 2047 BT_FIELD_CLASS_TYPE_STRING);
5cd6d0e5
PP
2048 bt_bfcr_set_unsigned_int_cb(notit->bfcr,
2049 bfcr_unsigned_int_cb);
e98a2d6e 2050 }
44c440bc 2051 }
e98a2d6e 2052
44c440bc
PP
2053 /* Pop stack */
2054 stack_pop(notit->stack);
e98a2d6e 2055
44c440bc
PP
2056 /* If the stack is not empty, increment the base's index */
2057 if (!stack_empty(notit->stack)) {
2058 stack_top(notit->stack)->index++;
e98a2d6e
PP
2059 }
2060
2061end:
5cd6d0e5 2062 return BT_BFCR_STATUS_OK;
e98a2d6e
PP
2063}
2064
2065static
5cd6d0e5 2066int64_t bfcr_get_sequence_length_cb(struct ctf_field_class *fc, void *data)
e98a2d6e 2067{
b19ff26f 2068 bt_field *seq_field;
d6e69534 2069 struct bt_msg_iter *notit = data;
5cd6d0e5 2070 struct ctf_field_class_sequence *seq_fc = (void *) fc;
44c440bc
PP
2071 int64_t length = -1;
2072 int ret;
e98a2d6e 2073
44c440bc 2074 length = (uint64_t) g_array_index(notit->stored_values, uint64_t,
5cd6d0e5 2075 seq_fc->stored_length_index);
fdf0e7a0 2076 seq_field = stack_top(notit->stack)->base;
44c440bc 2077 BT_ASSERT(seq_field);
40f4ba76 2078 ret = bt_field_dynamic_array_set_length(seq_field, (uint64_t) length);
44c440bc
PP
2079 if (ret) {
2080 BT_LOGE("Cannot set dynamic array field's length field: "
2081 "notit-addr=%p, field-addr=%p, "
2082 "length=%" PRIu64, notit, seq_field, length);
2cf1d51e 2083 }
fdf0e7a0 2084
44c440bc 2085 return length;
e98a2d6e
PP
2086}
2087
2088static
5cd6d0e5
PP
2089struct ctf_field_class *bfcr_borrow_variant_selected_field_class_cb(
2090 struct ctf_field_class *fc, void *data)
e98a2d6e 2091{
312c056a 2092 int ret;
44c440bc
PP
2093 uint64_t i;
2094 int64_t option_index = -1;
d6e69534 2095 struct bt_msg_iter *notit = data;
5cd6d0e5
PP
2096 struct ctf_field_class_variant *var_fc = (void *) fc;
2097 struct ctf_named_field_class *selected_option = NULL;
2098 struct ctf_field_class *ret_fc = NULL;
44c440bc
PP
2099 union {
2100 uint64_t u;
2101 int64_t i;
2102 } tag;
2103
2104 /* Get variant's tag */
2105 tag.u = g_array_index(notit->stored_values, uint64_t,
5cd6d0e5 2106 var_fc->stored_tag_index);
e98a2d6e
PP
2107
2108 /*
44c440bc 2109 * Check each range to find the selected option's index.
e98a2d6e 2110 */
5cd6d0e5
PP
2111 if (var_fc->tag_fc->base.is_signed) {
2112 for (i = 0; i < var_fc->ranges->len; i++) {
2113 struct ctf_field_class_variant_range *range =
2114 ctf_field_class_variant_borrow_range_by_index(
2115 var_fc, i);
44c440bc
PP
2116
2117 if (tag.i >= range->range.lower.i &&
2118 tag.i <= range->range.upper.i) {
2119 option_index = (int64_t) range->option_index;
2120 break;
2121 }
2122 }
312c056a 2123 } else {
5cd6d0e5
PP
2124 for (i = 0; i < var_fc->ranges->len; i++) {
2125 struct ctf_field_class_variant_range *range =
2126 ctf_field_class_variant_borrow_range_by_index(
2127 var_fc, i);
44c440bc
PP
2128
2129 if (tag.u >= range->range.lower.u &&
2130 tag.u <= range->range.upper.u) {
2131 option_index = (int64_t) range->option_index;
2132 break;
2133 }
2134 }
312c056a
PP
2135 }
2136
44c440bc 2137 if (option_index < 0) {
5cd6d0e5
PP
2138 BT_LOGW("Cannot find variant field class's option: "
2139 "notit-addr=%p, var-fc-addr=%p, u-tag=%" PRIu64 ", "
2140 "i-tag=%" PRId64, notit, var_fc, tag.u, tag.i);
e98a2d6e
PP
2141 goto end;
2142 }
2143
5cd6d0e5
PP
2144 selected_option = ctf_field_class_variant_borrow_option_by_index(
2145 var_fc, (uint64_t) option_index);
e98a2d6e 2146
5cd6d0e5 2147 if (selected_option->fc->in_ir) {
b19ff26f 2148 bt_field *var_field = stack_top(notit->stack)->base;
1556a1af 2149
40f4ba76 2150 ret = bt_field_variant_select_option_field(
e5be10ef 2151 var_field, option_index);
e22b45d0 2152 if (ret) {
44c440bc
PP
2153 BT_LOGW("Cannot select variant field's option field: "
2154 "notit-addr=%p, var-field-addr=%p, "
2155 "opt-index=%" PRId64, notit, var_field,
2156 option_index);
1556a1af
JG
2157 goto end;
2158 }
af87daef
PP
2159 }
2160
5cd6d0e5 2161 ret_fc = selected_option->fc;
e22b45d0 2162
af87daef 2163end:
5cd6d0e5 2164 return ret_fc;
44c440bc
PP
2165}
2166
2167static
605e1019 2168void set_event_default_clock_snapshot(struct bt_msg_iter *notit)
44c440bc 2169{
b19ff26f 2170 bt_event *event =
d6e69534
PP
2171 bt_message_event_borrow_event(
2172 notit->event_msg);
b19ff26f 2173 bt_stream_class *sc = notit->meta.sc->ir_sc;
44c440bc
PP
2174
2175 BT_ASSERT(event);
2176
2177 if (bt_stream_class_borrow_default_clock_class(sc)) {
605e1019
PP
2178 bt_event_set_default_clock_snapshot(event,
2179 notit->default_clock_snapshot);
44c440bc 2180 }
af87daef
PP
2181}
2182
f42867e2 2183static
d6e69534
PP
2184void notify_new_stream(struct bt_msg_iter *notit,
2185 bt_message **message)
f42867e2 2186{
d6e69534
PP
2187 enum bt_msg_iter_status status;
2188 bt_message *ret = NULL;
f42867e2 2189
312c056a 2190 status = set_current_stream(notit);
d6e69534
PP
2191 if (status != BT_MSG_ITER_STATUS_OK) {
2192 BT_MESSAGE_PUT_REF_AND_RESET(ret);
f42867e2
PP
2193 goto end;
2194 }
2195
2196 BT_ASSERT(notit->stream);
d6e69534
PP
2197 BT_ASSERT(notit->msg_iter);
2198 ret = bt_message_stream_beginning_create(notit->msg_iter,
f0010051 2199 notit->stream);
f42867e2 2200 if (!ret) {
d6e69534 2201 BT_LOGE("Cannot create stream beginning message: "
f42867e2
PP
2202 "notit-addr=%p, stream-addr=%p",
2203 notit, notit->stream);
2204 return;
2205 }
2206
2207end:
d6e69534 2208 *message = ret;
f42867e2
PP
2209}
2210
2211static
d6e69534
PP
2212void notify_end_of_stream(struct bt_msg_iter *notit,
2213 bt_message **message)
f42867e2 2214{
d6e69534 2215 bt_message *ret;
f42867e2
PP
2216
2217 if (!notit->stream) {
d6e69534 2218 BT_LOGE("Cannot create stream for stream message: "
f42867e2
PP
2219 "notit-addr=%p", notit);
2220 return;
2221 }
2222
d6e69534
PP
2223 BT_ASSERT(notit->msg_iter);
2224 ret = bt_message_stream_end_create(notit->msg_iter,
f0010051 2225 notit->stream);
f42867e2 2226 if (!ret) {
d6e69534 2227 BT_LOGE("Cannot create stream beginning message: "
f42867e2
PP
2228 "notit-addr=%p, stream-addr=%p",
2229 notit, notit->stream);
2230 return;
2231 }
d6e69534 2232 *message = ret;
f42867e2
PP
2233}
2234
78586d8a 2235static
d6e69534
PP
2236void notify_new_packet(struct bt_msg_iter *notit,
2237 bt_message **message)
e98a2d6e 2238{
312c056a 2239 int ret;
d6e69534
PP
2240 enum bt_msg_iter_status status;
2241 bt_message *msg = NULL;
b19ff26f 2242 const bt_stream_class *sc;
e98a2d6e 2243
312c056a 2244 status = set_current_packet(notit);
d6e69534 2245 if (status != BT_MSG_ITER_STATUS_OK) {
312c056a 2246 goto end;
e98a2d6e
PP
2247 }
2248
312c056a 2249 BT_ASSERT(notit->packet);
40f4ba76 2250 sc = notit->meta.sc->ir_sc;
44c440bc
PP
2251 BT_ASSERT(sc);
2252
2253 if (bt_stream_class_packets_have_discarded_event_counter_snapshot(sc)) {
2254 BT_ASSERT(notit->snapshots.discarded_events != UINT64_C(-1));
40f4ba76 2255 bt_packet_set_discarded_event_counter_snapshot(
44c440bc 2256 notit->packet, notit->snapshots.discarded_events);
44c440bc
PP
2257 }
2258
2259 if (bt_stream_class_packets_have_packet_counter_snapshot(sc)) {
2260 BT_ASSERT(notit->snapshots.packets != UINT64_C(-1));
40f4ba76 2261 bt_packet_set_packet_counter_snapshot(
44c440bc 2262 notit->packet, notit->snapshots.packets);
44c440bc
PP
2263 }
2264
605e1019 2265 if (bt_stream_class_packets_have_default_beginning_clock_snapshot(sc)) {
44c440bc 2266 BT_ASSERT(notit->snapshots.beginning_clock != UINT64_C(-1));
605e1019 2267 bt_packet_set_default_beginning_clock_snapshot(
44c440bc 2268 notit->packet, notit->snapshots.beginning_clock);
44c440bc
PP
2269 }
2270
605e1019 2271 if (bt_stream_class_packets_have_default_end_clock_snapshot(sc)) {
44c440bc 2272 BT_ASSERT(notit->snapshots.end_clock != UINT64_C(-1));
605e1019 2273 bt_packet_set_default_end_clock_snapshot(
44c440bc 2274 notit->packet, notit->snapshots.end_clock);
44c440bc 2275 }
312c056a 2276
312c056a 2277 if (notit->packet_context_field) {
40f4ba76 2278 ret = bt_packet_move_context_field(
e5be10ef 2279 notit->packet, notit->packet_context_field);
312c056a
PP
2280 if (ret) {
2281 goto end;
2282 }
2283
2284 notit->packet_context_field = NULL;
2285
2286 /*
83ebb7f1
PP
2287 * At this point notit->dscopes.stream_packet_context
2288 * has the same value as the packet context field within
312c056a
PP
2289 * notit->packet.
2290 */
40f4ba76 2291 BT_ASSERT(bt_packet_borrow_context_field(
e5be10ef 2292 notit->packet) ==
312c056a
PP
2293 notit->dscopes.stream_packet_context);
2294 }
2295
d6e69534
PP
2296 BT_ASSERT(notit->msg_iter);
2297 msg = bt_message_packet_beginning_create(notit->msg_iter,
5c563278 2298 notit->packet);
d6e69534
PP
2299 if (!msg) {
2300 BT_LOGE("Cannot create packet beginning message: "
fdf0e7a0
PP
2301 "notit-addr=%p, packet-addr=%p",
2302 notit, notit->packet);
e22b45d0 2303 goto end;
78586d8a 2304 }
312c056a 2305
d6e69534 2306 *message = msg;
e22b45d0
PP
2307
2308end:
2309 return;
e98a2d6e
PP
2310}
2311
78586d8a 2312static
d6e69534
PP
2313void notify_end_of_packet(struct bt_msg_iter *notit,
2314 bt_message **message)
e98a2d6e 2315{
d6e69534 2316 bt_message *msg;
e98a2d6e 2317
e98a2d6e 2318 if (!notit->packet) {
78586d8a 2319 return;
e98a2d6e
PP
2320 }
2321
44c440bc
PP
2322 /* Update default clock from packet's end time */
2323 if (notit->snapshots.end_clock != UINT64_C(-1)) {
605e1019 2324 notit->default_clock_snapshot = notit->snapshots.end_clock;
44c440bc
PP
2325 }
2326
d6e69534
PP
2327 BT_ASSERT(notit->msg_iter);
2328 msg = bt_message_packet_end_create(notit->msg_iter,
f0010051 2329 notit->packet);
d6e69534
PP
2330 if (!msg) {
2331 BT_LOGE("Cannot create packet end message: "
fdf0e7a0
PP
2332 "notit-addr=%p, packet-addr=%p",
2333 notit, notit->packet);
78586d8a 2334 return;
ccf82993 2335
78586d8a 2336 }
e98a2d6e 2337
c5b9b441 2338 BT_PACKET_PUT_REF_AND_RESET(notit->packet);
d6e69534 2339 *message = msg;
e98a2d6e
PP
2340}
2341
c44c3e70 2342BT_HIDDEN
d6e69534 2343struct bt_msg_iter *bt_msg_iter_create(struct ctf_trace_class *tc,
e98a2d6e 2344 size_t max_request_sz,
d6e69534 2345 struct bt_msg_iter_medium_ops medops, void *data)
e98a2d6e 2346{
d6e69534 2347 struct bt_msg_iter *notit = NULL;
5cd6d0e5
PP
2348 struct bt_bfcr_cbs cbs = {
2349 .classes = {
2350 .signed_int = bfcr_signed_int_cb,
2351 .unsigned_int = bfcr_unsigned_int_cb,
2352 .floating_point = bfcr_floating_point_cb,
2353 .string_begin = bfcr_string_begin_cb,
2354 .string = bfcr_string_cb,
2355 .string_end = bfcr_string_end_cb,
2356 .compound_begin = bfcr_compound_begin_cb,
2357 .compound_end = bfcr_compound_end_cb,
e98a2d6e
PP
2358 },
2359 .query = {
5cd6d0e5
PP
2360 .get_sequence_length = bfcr_get_sequence_length_cb,
2361 .borrow_variant_selected_field_class = bfcr_borrow_variant_selected_field_class_cb,
e98a2d6e
PP
2362 },
2363 };
2364
44c440bc 2365 BT_ASSERT(tc);
f6ccaed9 2366 BT_ASSERT(medops.request_bytes);
312c056a 2367 BT_ASSERT(medops.borrow_stream);
d6e69534 2368 BT_LOGD("Creating CTF plugin message iterator: "
862ca4ed
PP
2369 "trace-addr=%p, max-request-size=%zu, "
2370 "data=%p", tc, max_request_sz, data);
d6e69534 2371 notit = g_new0(struct bt_msg_iter, 1);
e98a2d6e 2372 if (!notit) {
d6e69534 2373 BT_LOGE_STR("Failed to allocate one CTF plugin message iterator.");
e98a2d6e
PP
2374 goto end;
2375 }
44c440bc 2376 notit->meta.tc = tc;
e98a2d6e
PP
2377 notit->medium.medops = medops;
2378 notit->medium.max_request_sz = max_request_sz;
2379 notit->medium.data = data;
e98a2d6e 2380 notit->stack = stack_new(notit);
44c440bc
PP
2381 notit->stored_values = g_array_new(FALSE, TRUE, sizeof(uint64_t));
2382 g_array_set_size(notit->stored_values, tc->stored_value_count);
2383
e98a2d6e 2384 if (!notit->stack) {
fdf0e7a0 2385 BT_LOGE_STR("Failed to create field stack.");
c44c3e70 2386 goto error;
e98a2d6e
PP
2387 }
2388
5cd6d0e5
PP
2389 notit->bfcr = bt_bfcr_create(cbs, notit);
2390 if (!notit->bfcr) {
2391 BT_LOGE_STR("Failed to create binary class reader (BFCR).");
c44c3e70 2392 goto error;
e98a2d6e
PP
2393 }
2394
d6e69534
PP
2395 bt_msg_iter_reset(notit);
2396 BT_LOGD("Created CTF plugin message iterator: "
862ca4ed 2397 "trace-addr=%p, max-request-size=%zu, "
fdf0e7a0 2398 "data=%p, notit-addr=%p",
862ca4ed 2399 tc, max_request_sz, data, notit);
9e0c8dbb 2400 notit->cur_packet_offset = 0;
fdf0e7a0 2401
e98a2d6e
PP
2402end:
2403 return notit;
fdf0e7a0 2404
c44c3e70 2405error:
d6e69534 2406 bt_msg_iter_destroy(notit);
c44c3e70
JG
2407 notit = NULL;
2408 goto end;
e98a2d6e
PP
2409}
2410
d6e69534 2411void bt_msg_iter_destroy(struct bt_msg_iter *notit)
e98a2d6e 2412{
c5b9b441
PP
2413 BT_PACKET_PUT_REF_AND_RESET(notit->packet);
2414 BT_STREAM_PUT_REF_AND_RESET(notit->stream);
312c056a 2415 release_all_dscopes(notit);
e98a2d6e 2416
d6e69534 2417 BT_LOGD("Destroying CTF plugin message iterator: addr=%p", notit);
fdf0e7a0 2418
e98a2d6e 2419 if (notit->stack) {
fdf0e7a0 2420 BT_LOGD_STR("Destroying field stack.");
e98a2d6e
PP
2421 stack_destroy(notit->stack);
2422 }
2423
5cd6d0e5
PP
2424 if (notit->bfcr) {
2425 BT_LOGD("Destroying BFCR: bfcr-addr=%p", notit->bfcr);
2426 bt_bfcr_destroy(notit->bfcr);
e98a2d6e
PP
2427 }
2428
44c440bc
PP
2429 if (notit->stored_values) {
2430 g_array_free(notit->stored_values, TRUE);
5f870343 2431 }
fdf0e7a0 2432
e98a2d6e
PP
2433 g_free(notit);
2434}
2435
d6e69534
PP
2436enum bt_msg_iter_status bt_msg_iter_get_next_message(
2437 struct bt_msg_iter *notit,
2438 bt_self_message_iterator *msg_iter,
2439 bt_message **message)
e98a2d6e 2440{
d6e69534 2441 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
e98a2d6e 2442
f6ccaed9 2443 BT_ASSERT(notit);
d6e69534 2444 BT_ASSERT(message);
e98a2d6e 2445
5c563278 2446 if (notit->state == STATE_DONE) {
d6e69534 2447 status = BT_MSG_ITER_STATUS_EOF;
5c563278
PP
2448 goto end;
2449 }
2450
d6e69534 2451 notit->msg_iter = msg_iter;
f42867e2 2452
d6e69534 2453 BT_LOGV("Getting next message: notit-addr=%p", notit);
fdf0e7a0 2454
e98a2d6e
PP
2455 while (true) {
2456 status = handle_state(notit);
d6e69534
PP
2457 if (status == BT_MSG_ITER_STATUS_AGAIN) {
2458 BT_LOGV_STR("Medium returned BT_MSG_ITER_STATUS_AGAIN.");
7cdc2bab
MD
2459 goto end;
2460 }
312c056a 2461
d6e69534
PP
2462 if (status != BT_MSG_ITER_STATUS_OK) {
2463 if (status == BT_MSG_ITER_STATUS_EOF) {
f42867e2
PP
2464 enum state next_state = notit->state;
2465
d6e69534 2466 BT_LOGV_STR("Medium returned BT_MSG_ITER_STATUS_EOF.");
f42867e2
PP
2467
2468 if (notit->packet) {
312c056a 2469 notify_end_of_packet(notit,
d6e69534 2470 message);
f42867e2 2471 } else {
312c056a 2472 notify_end_of_stream(notit,
d6e69534 2473 message);
f42867e2
PP
2474 next_state = STATE_DONE;
2475 }
2476
d6e69534
PP
2477 if (!*message) {
2478 status = BT_MSG_ITER_STATUS_ERROR;
f42867e2
PP
2479 goto end;
2480 }
2481
d6e69534 2482 status = BT_MSG_ITER_STATUS_OK;
f42867e2 2483 notit->state = next_state;
e98a2d6e 2484 } else {
fdf0e7a0
PP
2485 BT_LOGW("Cannot handle state: "
2486 "notit-addr=%p, state=%s",
2487 notit, state_string(notit->state));
e98a2d6e 2488 }
312c056a 2489
e98a2d6e
PP
2490 goto end;
2491 }
2492
2493 switch (notit->state) {
d6e69534 2494 case STATE_EMIT_MSG_NEW_STREAM:
f42867e2 2495 /* notify_new_stream() logs errors */
d6e69534 2496 notify_new_stream(notit, message);
44c440bc 2497
d6e69534
PP
2498 if (!*message) {
2499 status = BT_MSG_ITER_STATUS_ERROR;
f42867e2 2500 }
44c440bc 2501
f42867e2
PP
2502 notit->stream_begin_emitted = true;
2503 goto end;
d6e69534 2504 case STATE_EMIT_MSG_NEW_PACKET:
fdf0e7a0 2505 /* notify_new_packet() logs errors */
d6e69534 2506 notify_new_packet(notit, message);
44c440bc 2507
d6e69534
PP
2508 if (!*message) {
2509 status = BT_MSG_ITER_STATUS_ERROR;
e98a2d6e 2510 }
312c056a 2511
e98a2d6e 2512 goto end;
d6e69534
PP
2513 case STATE_EMIT_MSG_EVENT:
2514 BT_ASSERT(notit->event_msg);
605e1019 2515 set_event_default_clock_snapshot(notit);
d6e69534
PP
2516 *message = notit->event_msg;
2517 notit->event_msg = NULL;
e98a2d6e 2518 goto end;
d6e69534 2519 case STATE_EMIT_MSG_END_OF_PACKET:
fdf0e7a0 2520 /* notify_end_of_packet() logs errors */
d6e69534 2521 notify_end_of_packet(notit, message);
44c440bc 2522
d6e69534
PP
2523 if (!*message) {
2524 status = BT_MSG_ITER_STATUS_ERROR;
e98a2d6e 2525 }
312c056a 2526
e98a2d6e
PP
2527 goto end;
2528 default:
2529 /* Non-emitting state: continue */
2530 break;
2531 }
2532 }
2533
2534end:
2535 return status;
2536}
87187cbf 2537
41693723
PP
2538static
2539enum bt_msg_iter_status read_packet_header_context_fields(
2540 struct bt_msg_iter *notit)
87187cbf 2541{
9e0c8dbb 2542 int ret;
d6e69534 2543 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
87187cbf 2544
f6ccaed9 2545 BT_ASSERT(notit);
87187cbf 2546
d6e69534 2547 if (notit->state == STATE_EMIT_MSG_NEW_PACKET) {
87187cbf 2548 /* We're already there */
41693723 2549 goto end;
87187cbf
PP
2550 }
2551
2552 while (true) {
2553 status = handle_state(notit);
d6e69534
PP
2554 if (status == BT_MSG_ITER_STATUS_AGAIN) {
2555 BT_LOGV_STR("Medium returned BT_MSG_ITER_STATUS_AGAIN.");
87187cbf
PP
2556 goto end;
2557 }
d6e69534
PP
2558 if (status != BT_MSG_ITER_STATUS_OK) {
2559 if (status == BT_MSG_ITER_STATUS_EOF) {
2560 BT_LOGV_STR("Medium returned BT_MSG_ITER_STATUS_EOF.");
87187cbf 2561 } else {
fdf0e7a0
PP
2562 BT_LOGW("Cannot handle state: "
2563 "notit-addr=%p, state=%s",
2564 notit, state_string(notit->state));
87187cbf
PP
2565 }
2566 goto end;
2567 }
2568
2569 switch (notit->state) {
d6e69534 2570 case STATE_EMIT_MSG_NEW_PACKET:
87187cbf
PP
2571 /*
2572 * Packet header and context fields are
2573 * potentially decoded (or they don't exist).
2574 */
41693723 2575 goto end;
87187cbf 2576 case STATE_INIT:
d6e69534 2577 case STATE_EMIT_MSG_NEW_STREAM:
87187cbf
PP
2578 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
2579 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
2580 case STATE_AFTER_TRACE_PACKET_HEADER:
2581 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN:
2582 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE:
2583 case STATE_AFTER_STREAM_PACKET_CONTEXT:
2584 /* Non-emitting state: continue */
2585 break;
2586 default:
2587 /*
2588 * We should never get past the
d6e69534 2589 * STATE_EMIT_MSG_NEW_PACKET state.
87187cbf 2590 */
fdf0e7a0
PP
2591 BT_LOGF("Unexpected state: notit-addr=%p, state=%s",
2592 notit, state_string(notit->state));
0fbb9a9f 2593 abort();
87187cbf
PP
2594 }
2595 }
2596
41693723 2597end:
312c056a
PP
2598 ret = set_current_packet_content_sizes(notit);
2599 if (ret) {
d6e69534 2600 status = BT_MSG_ITER_STATUS_ERROR;
312c056a
PP
2601 goto end;
2602 }
2603
87187cbf
PP
2604 return status;
2605}
6de92955
PP
2606
2607BT_HIDDEN
d6e69534 2608void bt_msg_iter_set_medops_data(struct bt_msg_iter *notit,
6de92955
PP
2609 void *medops_data)
2610{
f6ccaed9 2611 BT_ASSERT(notit);
6de92955
PP
2612 notit->medium.data = medops_data;
2613}
9e0c8dbb
JG
2614
2615BT_HIDDEN
d6e69534
PP
2616enum bt_msg_iter_status bt_msg_iter_seek(
2617 struct bt_msg_iter *notit, off_t offset)
9e0c8dbb 2618{
d6e69534
PP
2619 enum bt_msg_iter_status ret = BT_MSG_ITER_STATUS_OK;
2620 enum bt_msg_iter_medium_status medium_status;
9e0c8dbb 2621
f6ccaed9 2622 BT_ASSERT(notit);
9e0c8dbb
JG
2623 if (offset < 0) {
2624 BT_LOGE("Cannot seek to negative offset: offset=%jd", offset);
d6e69534 2625 ret = BT_MSG_ITER_STATUS_INVAL;
9e0c8dbb
JG
2626 goto end;
2627 }
2628
2629 if (!notit->medium.medops.seek) {
d6e69534 2630 ret = BT_MSG_ITER_STATUS_UNSUPPORTED;
9e0c8dbb
JG
2631 BT_LOGD("Aborting seek as the iterator's underlying media does not implement seek support.");
2632 goto end;
2633 }
2634
2635 medium_status = notit->medium.medops.seek(
d6e69534
PP
2636 BT_MSG_ITER_SEEK_WHENCE_SET, offset, notit->medium.data);
2637 if (medium_status != BT_MSG_ITER_MEDIUM_STATUS_OK) {
2638 if (medium_status == BT_MSG_ITER_MEDIUM_STATUS_EOF) {
2639 ret = BT_MSG_ITER_STATUS_EOF;
9e0c8dbb 2640 } else {
d6e69534 2641 ret = BT_MSG_ITER_STATUS_ERROR;
9e0c8dbb
JG
2642 goto end;
2643 }
2644 }
2645
d6e69534 2646 bt_msg_iter_reset(notit);
9e0c8dbb 2647 notit->cur_packet_offset = offset;
44c440bc 2648
9e0c8dbb
JG
2649end:
2650 return ret;
2651}
2652
2653BT_HIDDEN
d6e69534 2654off_t bt_msg_iter_get_current_packet_offset(struct bt_msg_iter *notit)
9e0c8dbb 2655{
f6ccaed9 2656 BT_ASSERT(notit);
9e0c8dbb
JG
2657 return notit->cur_packet_offset;
2658}
2659
2660BT_HIDDEN
d6e69534
PP
2661off_t bt_msg_iter_get_current_packet_size(
2662 struct bt_msg_iter *notit)
9e0c8dbb 2663{
f6ccaed9 2664 BT_ASSERT(notit);
44c440bc
PP
2665 return notit->cur_exp_packet_total_size;
2666}
2667
2668BT_HIDDEN
d6e69534 2669void bt_msg_trace_class_changed(struct bt_msg_iter *notit)
44c440bc
PP
2670{
2671 if (notit->meta.tc->stored_value_count > notit->stored_values->len) {
2672 g_array_set_size(notit->stored_values,
2673 notit->meta.tc->stored_value_count);
2674 }
2675}
2676
2677BT_HIDDEN
d6e69534
PP
2678enum bt_msg_iter_status bt_msg_iter_get_packet_properties(
2679 struct bt_msg_iter *notit,
2680 struct bt_msg_iter_packet_properties *props)
44c440bc 2681{
41693723
PP
2682 enum bt_msg_iter_status status;
2683
44c440bc
PP
2684 BT_ASSERT(notit);
2685 BT_ASSERT(props);
41693723
PP
2686 status = read_packet_header_context_fields(notit);
2687 if (status != BT_MSG_ITER_STATUS_OK) {
2688 goto end;
2689 }
44c440bc
PP
2690
2691 props->exp_packet_total_size =
2692 (uint64_t) notit->cur_exp_packet_total_size;
2693 props->exp_packet_content_size =
2694 (uint64_t) notit->cur_exp_packet_content_size;
2695 BT_ASSERT(props->stream_class_id >= 0);
2696 props->stream_class_id = (uint64_t) notit->cur_stream_class_id;
2697 props->data_stream_id = notit->cur_data_stream_id;
2698 props->snapshots.discarded_events = notit->snapshots.discarded_events;
2699 props->snapshots.packets = notit->snapshots.packets;
2700 props->snapshots.beginning_clock = notit->snapshots.beginning_clock;
2701 props->snapshots.end_clock = notit->snapshots.end_clock;
41693723
PP
2702
2703end:
2704 return status;
9e0c8dbb 2705}
This page took 0.203836 seconds and 4 git commands to generate.