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