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