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