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