Cleanup: remove private babeltrace.h
[babeltrace.git] / src / 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>
57952005 34#include "common/assert.h"
e98a2d6e 35#include <string.h>
71c5da58 36#include <babeltrace2/babeltrace.h>
57952005 37#include "common/common.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
85e7137b 538 if (G_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
cf19c159
PP
982 BT_ASSERT((notit->cur_exp_packet_total_size >= 0 &&
983 notit->cur_exp_packet_content_size >= 0) ||
984 (notit->cur_exp_packet_total_size < 0 &&
985 notit->cur_exp_packet_content_size < 0));
ab03d3fb 986
7b33a0e0
PP
987 if (notit->cur_exp_packet_content_size >
988 notit->cur_exp_packet_total_size) {
989 BT_LOGW("Invalid packet or content size: "
990 "content size is greater than packet size: "
fdf0e7a0 991 "notit-addr=%p, packet-context-field-addr=%p, "
7b33a0e0 992 "packet-size=%" PRId64 ", content-size=%" PRId64,
fdf0e7a0 993 notit, notit->dscopes.stream_packet_context,
7b33a0e0
PP
994 notit->cur_exp_packet_total_size,
995 notit->cur_exp_packet_content_size);
b09a5592 996 status = BT_MSG_ITER_STATUS_ERROR;
fdf0e7a0
PP
997 goto end;
998 }
999
fdf0e7a0
PP
1000 BT_LOGV("Set current packet and content sizes: "
1001 "notit-addr=%p, packet-size=%" PRIu64 ", content-size=%" PRIu64,
7b33a0e0
PP
1002 notit, notit->cur_exp_packet_total_size,
1003 notit->cur_exp_packet_content_size);
bbbd35bf 1004
e98a2d6e 1005end:
e98a2d6e
PP
1006 return status;
1007}
1008
1009static
bbbd35bf 1010enum bt_msg_iter_status after_packet_context_state(struct bt_msg_iter *notit)
e98a2d6e 1011{
b09a5592 1012 enum bt_msg_iter_status status;
e98a2d6e
PP
1013
1014 status = set_current_packet_content_sizes(notit);
b09a5592 1015 if (status != BT_MSG_ITER_STATUS_OK) {
8fc063a2
PP
1016 goto end;
1017 }
1018
bbbd35bf
PP
1019 if (notit->stream) {
1020 /*
1021 * Stream exists, which means we already emitted at
1022 * least one packet beginning message, so the initial
1023 * stream beginning message was also emitted.
1024 */
989925d1 1025 notit->state = STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS;
8fc063a2 1026 } else {
989925d1 1027 notit->state = STATE_CHECK_EMIT_MSG_STREAM_BEGINNING;
e98a2d6e
PP
1028 }
1029
8fc063a2 1030end:
e98a2d6e
PP
1031 return status;
1032}
1033
1034static
bbbd35bf 1035enum bt_msg_iter_status read_event_header_begin_state(struct bt_msg_iter *notit)
e98a2d6e 1036{
b09a5592 1037 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
939190b3 1038 struct ctf_field_class *event_header_fc = NULL;
e98a2d6e 1039
9b876b7b
PP
1040 /* Reset the position of the last event header */
1041 notit->buf.last_eh_at = notit->buf.at;
7b33a0e0 1042 notit->cur_event_class_id = -1;
9b876b7b 1043
e98a2d6e 1044 /* Check if we have some content left */
7b33a0e0 1045 if (notit->cur_exp_packet_content_size >= 0) {
85e7137b 1046 if (G_UNLIKELY(packet_at(notit) ==
7b33a0e0 1047 notit->cur_exp_packet_content_size)) {
e98a2d6e 1048 /* No more events! */
fdf0e7a0
PP
1049 BT_LOGV("Reached end of packet: notit-addr=%p, "
1050 "cur=%zu", notit, packet_at(notit));
bbbd35bf 1051 notit->state = STATE_EMIT_MSG_PACKET_END_MULTI;
e98a2d6e 1052 goto end;
85e7137b 1053 } else if (G_UNLIKELY(packet_at(notit) >
7b33a0e0 1054 notit->cur_exp_packet_content_size)) {
e98a2d6e 1055 /* That's not supposed to happen */
fdf0e7a0 1056 BT_LOGV("Before decoding event header field: cursor is passed the packet's content: "
41342c71 1057 "notit-addr=%p, content-size=%" PRId64 ", "
7b33a0e0
PP
1058 "cur=%zu", notit,
1059 notit->cur_exp_packet_content_size,
fdf0e7a0 1060 packet_at(notit));
b09a5592 1061 status = BT_MSG_ITER_STATUS_ERROR;
e98a2d6e
PP
1062 goto end;
1063 }
7b33a0e0
PP
1064 } else {
1065 /*
1066 * "Infinite" content: we're done when the medium has
1067 * nothing else for us.
1068 */
1069 status = buf_ensure_available_bits(notit);
bbbd35bf
PP
1070 switch (status) {
1071 case BT_MSG_ITER_STATUS_OK:
1072 break;
1073 case BT_MSG_ITER_STATUS_EOF:
1074 status = BT_MSG_ITER_STATUS_OK;
1075 notit->state = STATE_EMIT_MSG_PACKET_END_SINGLE;
1076 goto end;
1077 default:
7b33a0e0
PP
1078 goto end;
1079 }
e98a2d6e
PP
1080 }
1081
a6918753 1082 release_event_dscopes(notit);
7b33a0e0 1083 BT_ASSERT(notit->meta.sc);
939190b3
PP
1084 event_header_fc = notit->meta.sc->event_header_fc;
1085 if (!event_header_fc) {
7b33a0e0 1086 notit->state = STATE_AFTER_EVENT_HEADER;
e98a2d6e
PP
1087 goto end;
1088 }
1089
fdf0e7a0
PP
1090 BT_LOGV("Decoding event header field: "
1091 "notit-addr=%p, stream-class-addr=%p, "
7b33a0e0 1092 "stream-class-id=%" PRId64 ", "
939190b3 1093 "fc-addr=%p",
7b33a0e0
PP
1094 notit, notit->meta.sc,
1095 notit->meta.sc->id,
939190b3
PP
1096 event_header_fc);
1097 status = read_dscope_begin_state(notit, event_header_fc,
7b33a0e0 1098 STATE_AFTER_EVENT_HEADER,
0476f6f7 1099 STATE_DSCOPE_EVENT_HEADER_CONTINUE, NULL);
fdf0e7a0
PP
1100 if (status < 0) {
1101 BT_LOGW("Cannot decode event header field: "
1102 "notit-addr=%p, stream-class-addr=%p, "
939190b3 1103 "stream-class-id=%" PRId64 ", fc-addr=%p",
7b33a0e0
PP
1104 notit, notit->meta.sc,
1105 notit->meta.sc->id,
939190b3 1106 event_header_fc);
fdf0e7a0 1107 }
e98a2d6e 1108
b8418c69 1109end:
e98a2d6e
PP
1110 return status;
1111}
1112
1113static
b09a5592
PP
1114enum bt_msg_iter_status read_event_header_continue_state(
1115 struct bt_msg_iter *notit)
e98a2d6e
PP
1116{
1117 return read_dscope_continue_state(notit,
7b33a0e0 1118 STATE_AFTER_EVENT_HEADER);
e98a2d6e
PP
1119}
1120
1121static inline
b09a5592 1122enum bt_msg_iter_status set_current_event_class(struct bt_msg_iter *notit)
e98a2d6e 1123{
b09a5592 1124 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
e98a2d6e 1125
7b33a0e0 1126 struct ctf_event_class *new_event_class = NULL;
e98a2d6e 1127
7b33a0e0 1128 if (notit->cur_event_class_id == -1) {
e98a2d6e 1129 /*
7b33a0e0
PP
1130 * No current event class ID field, therefore only one
1131 * event class.
e98a2d6e 1132 */
7b33a0e0
PP
1133 if (notit->meta.sc->event_classes->len != 1) {
1134 BT_LOGW("Need exactly one event class since there's "
1135 "no event class ID field: "
10b7a2e4 1136 "notit-addr=%p", notit);
b09a5592 1137 status = BT_MSG_ITER_STATUS_ERROR;
7b33a0e0 1138 goto end;
fdf0e7a0 1139 }
e98a2d6e 1140
7b33a0e0
PP
1141 new_event_class = notit->meta.sc->event_classes->pdata[0];
1142 notit->cur_event_class_id = new_event_class->id;
e98a2d6e
PP
1143 }
1144
7b33a0e0
PP
1145 new_event_class = ctf_stream_class_borrow_event_class_by_id(
1146 notit->meta.sc, notit->cur_event_class_id);
1147 if (!new_event_class) {
fdf0e7a0 1148 BT_LOGW("No event class with ID of event class ID to use in stream class: "
7b33a0e0
PP
1149 "notit-addr=%p, stream-class-id=%" PRIu64 ", "
1150 "event-class-id=%" PRIu64 ", "
10b7a2e4 1151 "trace-class-addr=%p",
7b33a0e0 1152 notit, notit->meta.sc->id, notit->cur_event_class_id,
10b7a2e4 1153 notit->meta.tc);
b09a5592 1154 status = BT_MSG_ITER_STATUS_ERROR;
e98a2d6e
PP
1155 goto end;
1156 }
1157
7b33a0e0 1158 notit->meta.ec = new_event_class;
fdf0e7a0
PP
1159 BT_LOGV("Set current event class: "
1160 "notit-addr=%p, event-class-addr=%p, "
7b33a0e0
PP
1161 "event-class-id=%" PRId64 ", "
1162 "event-class-name=\"%s\"",
1163 notit, notit->meta.ec, notit->meta.ec->id,
1164 notit->meta.ec->name->str);
fdf0e7a0 1165
e98a2d6e 1166end:
e98a2d6e
PP
1167 return status;
1168}
1169
a6918753 1170static inline
b09a5592
PP
1171enum bt_msg_iter_status set_current_event_message(
1172 struct bt_msg_iter *notit)
a6918753 1173{
b09a5592
PP
1174 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
1175 bt_message *msg = NULL;
a6918753 1176
7b33a0e0 1177 BT_ASSERT(notit->meta.ec);
a6918753 1178 BT_ASSERT(notit->packet);
b09a5592 1179 BT_LOGV("Creating event message from event class and packet: "
a6918753 1180 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", packet-addr=%p",
7b33a0e0
PP
1181 notit, notit->meta.ec,
1182 notit->meta.ec->name->str,
a6918753 1183 notit->packet);
b09a5592 1184 BT_ASSERT(notit->msg_iter);
9c04b633
PP
1185 BT_ASSERT(notit->meta.sc);
1186
1187 if (bt_stream_class_borrow_default_clock_class(notit->meta.sc->ir_sc)) {
1188 msg = bt_message_event_create_with_default_clock_snapshot(
1189 notit->msg_iter, notit->meta.ec->ir_ec,
1190 notit->packet, notit->default_clock_snapshot);
1191 } else {
1192 msg = bt_message_event_create(notit->msg_iter,
1193 notit->meta.ec->ir_ec, notit->packet);
1194 }
1195
b09a5592
PP
1196 if (!msg) {
1197 BT_LOGE("Cannot create event message: "
a6918753
PP
1198 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", "
1199 "packet-addr=%p",
7b33a0e0
PP
1200 notit, notit->meta.ec,
1201 notit->meta.ec->name->str,
a6918753
PP
1202 notit->packet);
1203 goto error;
1204 }
1205
1206 goto end;
1207
1208error:
b09a5592
PP
1209 BT_MESSAGE_PUT_REF_AND_RESET(msg);
1210 status = BT_MSG_ITER_STATUS_ERROR;
a6918753
PP
1211
1212end:
b09a5592 1213 BT_MESSAGE_MOVE_REF(notit->event_msg, msg);
a6918753
PP
1214 return status;
1215}
1216
e98a2d6e 1217static
b09a5592
PP
1218enum bt_msg_iter_status after_event_header_state(
1219 struct bt_msg_iter *notit)
e98a2d6e 1220{
b09a5592 1221 enum bt_msg_iter_status status;
e98a2d6e 1222
e98a2d6e 1223 status = set_current_event_class(notit);
b09a5592 1224 if (status != BT_MSG_ITER_STATUS_OK) {
e98a2d6e
PP
1225 goto end;
1226 }
1227
b09a5592
PP
1228 status = set_current_event_message(notit);
1229 if (status != BT_MSG_ITER_STATUS_OK) {
a6918753
PP
1230 goto end;
1231 }
1232
b09a5592
PP
1233 notit->event = bt_message_event_borrow_event(
1234 notit->event_msg);
a6918753 1235 BT_ASSERT(notit->event);
7b33a0e0 1236 notit->state = STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN;
e98a2d6e
PP
1237
1238end:
1239 return status;
1240}
1241
1242static
b09a5592
PP
1243enum bt_msg_iter_status read_event_common_context_begin_state(
1244 struct bt_msg_iter *notit)
e98a2d6e 1245{
b09a5592 1246 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
939190b3 1247 struct ctf_field_class *event_common_context_fc;
e98a2d6e 1248
939190b3
PP
1249 event_common_context_fc = notit->meta.sc->event_common_context_fc;
1250 if (!event_common_context_fc) {
7b33a0e0 1251 notit->state = STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN;
e98a2d6e
PP
1252 goto end;
1253 }
1254
939190b3 1255 if (event_common_context_fc->in_ir) {
7b33a0e0
PP
1256 BT_ASSERT(!notit->dscopes.event_common_context);
1257 notit->dscopes.event_common_context =
78cf9df6 1258 bt_event_borrow_common_context_field(
9e550e5f 1259 notit->event);
7b33a0e0
PP
1260 BT_ASSERT(notit->dscopes.event_common_context);
1261 }
1262
1263 BT_LOGV("Decoding event common context field: "
fdf0e7a0 1264 "notit-addr=%p, stream-class-addr=%p, "
7b33a0e0 1265 "stream-class-id=%" PRId64 ", "
939190b3 1266 "fc-addr=%p",
7b33a0e0
PP
1267 notit, notit->meta.sc,
1268 notit->meta.sc->id,
939190b3
PP
1269 event_common_context_fc);
1270 status = read_dscope_begin_state(notit, event_common_context_fc,
7b33a0e0
PP
1271 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN,
1272 STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE,
1273 notit->dscopes.event_common_context);
fdf0e7a0 1274 if (status < 0) {
7b33a0e0 1275 BT_LOGW("Cannot decode event common context field: "
fdf0e7a0 1276 "notit-addr=%p, stream-class-addr=%p, "
939190b3 1277 "stream-class-id=%" PRId64 ", fc-addr=%p",
7b33a0e0
PP
1278 notit, notit->meta.sc,
1279 notit->meta.sc->id,
939190b3 1280 event_common_context_fc);
fdf0e7a0 1281 }
e98a2d6e
PP
1282
1283end:
e98a2d6e
PP
1284 return status;
1285}
1286
1287static
b09a5592
PP
1288enum bt_msg_iter_status read_event_common_context_continue_state(
1289 struct bt_msg_iter *notit)
e98a2d6e
PP
1290{
1291 return read_dscope_continue_state(notit,
7b33a0e0 1292 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN);
e98a2d6e
PP
1293}
1294
1295static
b09a5592
PP
1296enum bt_msg_iter_status read_event_spec_context_begin_state(
1297 struct bt_msg_iter *notit)
e98a2d6e 1298{
b09a5592 1299 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
939190b3 1300 struct ctf_field_class *event_spec_context_fc;
e98a2d6e 1301
939190b3
PP
1302 event_spec_context_fc = notit->meta.ec->spec_context_fc;
1303 if (!event_spec_context_fc) {
835b2d10 1304 notit->state = STATE_DSCOPE_EVENT_PAYLOAD_BEGIN;
e98a2d6e
PP
1305 goto end;
1306 }
fdf0e7a0 1307
939190b3 1308 if (event_spec_context_fc->in_ir) {
7b33a0e0 1309 BT_ASSERT(!notit->dscopes.event_spec_context);
9e550e5f 1310 notit->dscopes.event_spec_context =
78cf9df6 1311 bt_event_borrow_specific_context_field(
9e550e5f 1312 notit->event);
7b33a0e0
PP
1313 BT_ASSERT(notit->dscopes.event_spec_context);
1314 }
1315
1316 BT_LOGV("Decoding event specific context field: "
fdf0e7a0
PP
1317 "notit-addr=%p, event-class-addr=%p, "
1318 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
939190b3 1319 "fc-addr=%p",
7b33a0e0
PP
1320 notit, notit->meta.ec,
1321 notit->meta.ec->name->str,
1322 notit->meta.ec->id,
939190b3
PP
1323 event_spec_context_fc);
1324 status = read_dscope_begin_state(notit, event_spec_context_fc,
e98a2d6e 1325 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN,
7b33a0e0
PP
1326 STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE,
1327 notit->dscopes.event_spec_context);
fdf0e7a0 1328 if (status < 0) {
7b33a0e0 1329 BT_LOGW("Cannot decode event specific context field: "
fdf0e7a0
PP
1330 "notit-addr=%p, event-class-addr=%p, "
1331 "event-class-name=\"%s\", "
939190b3 1332 "event-class-id=%" PRId64 ", fc-addr=%p",
7b33a0e0
PP
1333 notit, notit->meta.ec,
1334 notit->meta.ec->name->str,
1335 notit->meta.ec->id,
939190b3 1336 event_spec_context_fc);
fdf0e7a0 1337 }
e98a2d6e
PP
1338
1339end:
e98a2d6e
PP
1340 return status;
1341}
1342
1343static
b09a5592
PP
1344enum bt_msg_iter_status read_event_spec_context_continue_state(
1345 struct bt_msg_iter *notit)
e98a2d6e
PP
1346{
1347 return read_dscope_continue_state(notit,
1348 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN);
1349}
1350
1351static
b09a5592
PP
1352enum bt_msg_iter_status read_event_payload_begin_state(
1353 struct bt_msg_iter *notit)
e98a2d6e 1354{
b09a5592 1355 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
939190b3 1356 struct ctf_field_class *event_payload_fc;
e98a2d6e 1357
939190b3
PP
1358 event_payload_fc = notit->meta.ec->payload_fc;
1359 if (!event_payload_fc) {
b09a5592 1360 notit->state = STATE_EMIT_MSG_EVENT;
e98a2d6e
PP
1361 goto end;
1362 }
1363
939190b3 1364 if (event_payload_fc->in_ir) {
7b33a0e0 1365 BT_ASSERT(!notit->dscopes.event_payload);
9e550e5f 1366 notit->dscopes.event_payload =
78cf9df6 1367 bt_event_borrow_payload_field(
9e550e5f 1368 notit->event);
7b33a0e0
PP
1369 BT_ASSERT(notit->dscopes.event_payload);
1370 }
1371
fdf0e7a0
PP
1372 BT_LOGV("Decoding event payload field: "
1373 "notit-addr=%p, event-class-addr=%p, "
1374 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
939190b3 1375 "fc-addr=%p",
7b33a0e0
PP
1376 notit, notit->meta.ec,
1377 notit->meta.ec->name->str,
1378 notit->meta.ec->id,
939190b3
PP
1379 event_payload_fc);
1380 status = read_dscope_begin_state(notit, event_payload_fc,
b09a5592 1381 STATE_EMIT_MSG_EVENT,
e98a2d6e 1382 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE,
a6918753 1383 notit->dscopes.event_payload);
fdf0e7a0
PP
1384 if (status < 0) {
1385 BT_LOGW("Cannot decode event payload field: "
1386 "notit-addr=%p, event-class-addr=%p, "
1387 "event-class-name=\"%s\", "
939190b3 1388 "event-class-id=%" PRId64 ", fc-addr=%p",
7b33a0e0
PP
1389 notit, notit->meta.ec,
1390 notit->meta.ec->name->str,
1391 notit->meta.ec->id,
939190b3 1392 event_payload_fc);
fdf0e7a0 1393 }
e98a2d6e
PP
1394
1395end:
e98a2d6e
PP
1396 return status;
1397}
1398
1399static
b09a5592
PP
1400enum bt_msg_iter_status read_event_payload_continue_state(
1401 struct bt_msg_iter *notit)
e98a2d6e 1402{
b09a5592 1403 return read_dscope_continue_state(notit, STATE_EMIT_MSG_EVENT);
e98a2d6e
PP
1404}
1405
1406static
bbbd35bf 1407enum bt_msg_iter_status skip_packet_padding_state(struct bt_msg_iter *notit)
e98a2d6e 1408{
b09a5592 1409 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
e98a2d6e
PP
1410 size_t bits_to_skip;
1411
7b33a0e0
PP
1412 BT_ASSERT(notit->cur_exp_packet_total_size > 0);
1413 bits_to_skip = notit->cur_exp_packet_total_size - packet_at(notit);
e98a2d6e
PP
1414 if (bits_to_skip == 0) {
1415 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1416 goto end;
1417 } else {
1418 size_t bits_to_consume;
fdf0e7a0
PP
1419
1420 BT_LOGV("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu",
1421 bits_to_skip, notit, bits_to_skip);
e98a2d6e 1422 status = buf_ensure_available_bits(notit);
b09a5592 1423 if (status != BT_MSG_ITER_STATUS_OK) {
e98a2d6e
PP
1424 goto end;
1425 }
1426
1427 bits_to_consume = MIN(buf_available_bits(notit), bits_to_skip);
fdf0e7a0
PP
1428 BT_LOGV("Skipping %zu bits of padding: notit-addr=%p, size=%zu",
1429 bits_to_consume, notit, bits_to_consume);
e98a2d6e 1430 buf_consume_bits(notit, bits_to_consume);
7b33a0e0
PP
1431 bits_to_skip = notit->cur_exp_packet_total_size -
1432 packet_at(notit);
e98a2d6e
PP
1433 if (bits_to_skip == 0) {
1434 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1435 goto end;
1436 }
1437 }
1438
1439end:
1440 return status;
1441}
1442
989925d1
PP
1443static
1444enum bt_msg_iter_status check_emit_msg_stream_beginning_state(
1445 struct bt_msg_iter *notit)
1446{
1447 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
1448
1449 if (notit->set_stream) {
1450 status = set_current_stream(notit);
1451 if (status != BT_MSG_ITER_STATUS_OK) {
1452 goto end;
1453 }
1454 }
1455
1456 if (notit->emit_stream_begin_msg) {
1457 notit->state = STATE_EMIT_MSG_STREAM_BEGINNING;
1458 } else {
1459 /* Stream's first packet */
1460 notit->state = STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS;
1461 }
1462
1463end:
1464 return status;
1465}
1466
1467static
1468enum bt_msg_iter_status check_emit_msg_discarded_events(
1469 struct bt_msg_iter *notit)
1470{
1471 notit->state = STATE_EMIT_MSG_DISCARDED_EVENTS;
1472
32476570
PP
1473 if (!notit->meta.sc->has_discarded_events) {
1474 notit->state = STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS;
1475 goto end;
1476 }
1477
989925d1
PP
1478 if (notit->prev_packet_snapshots.discarded_events == UINT64_C(-1)) {
1479 if (notit->snapshots.discarded_events == 0 ||
1480 notit->snapshots.discarded_events == UINT64_C(-1)) {
1481 /*
1482 * Stream's first packet with no discarded
1483 * events or no information about discarded
1484 * events: do not emit.
1485 */
1486 notit->state = STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS;
1487 }
1488 } else {
1489 /*
1490 * If the previous packet has a value for this counter,
1491 * then this counter is defined for the whole stream.
1492 */
1493 BT_ASSERT(notit->snapshots.discarded_events != UINT64_C(-1));
1494
1495 if (notit->snapshots.discarded_events -
1496 notit->prev_packet_snapshots.discarded_events == 0) {
1497 /*
1498 * No discarded events since previous packet: do
1499 * not emit.
1500 */
1501 notit->state = STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS;
1502 }
1503 }
1504
32476570 1505end:
989925d1
PP
1506 return BT_MSG_ITER_STATUS_OK;
1507}
1508
1509static
1510enum bt_msg_iter_status check_emit_msg_discarded_packets(
1511 struct bt_msg_iter *notit)
1512{
1513 notit->state = STATE_EMIT_MSG_DISCARDED_PACKETS;
1514
32476570
PP
1515 if (!notit->meta.sc->has_discarded_packets) {
1516 notit->state = STATE_EMIT_MSG_PACKET_BEGINNING;
1517 goto end;
1518 }
1519
989925d1
PP
1520 if (notit->prev_packet_snapshots.packets == UINT64_C(-1)) {
1521 /*
1522 * Stream's first packet or no information about
1523 * discarded packets: do not emit. In other words, if
1524 * this is the first packet and its sequence number is
1525 * not 0, do not consider that packets were previously
1526 * lost: we might be reading a partial stream (LTTng
1527 * snapshot for example).
1528 */
1529 notit->state = STATE_EMIT_MSG_PACKET_BEGINNING;
1530 } else {
1531 /*
1532 * If the previous packet has a value for this counter,
1533 * then this counter is defined for the whole stream.
1534 */
1535 BT_ASSERT(notit->snapshots.packets != UINT64_C(-1));
1536
1537 if (notit->snapshots.packets -
1538 notit->prev_packet_snapshots.packets <= 1) {
1539 /*
1540 * No discarded packets since previous packet:
1541 * do not emit.
1542 */
1543 notit->state = STATE_EMIT_MSG_PACKET_BEGINNING;
1544 }
1545 }
1546
32476570 1547end:
989925d1
PP
1548 return BT_MSG_ITER_STATUS_OK;
1549}
1550
1551static
1552enum bt_msg_iter_status check_emit_msg_stream_activity_end(
1553 struct bt_msg_iter *notit)
1554{
1555 if (notit->emit_stream_end_msg) {
1556 notit->state = STATE_EMIT_MSG_STREAM_ACTIVITY_END;
1557 } else {
1558 notit->state = STATE_DONE;
1559 }
1560
1561 return BT_MSG_ITER_STATUS_OK;
1562}
1563
e98a2d6e 1564static inline
b09a5592 1565enum bt_msg_iter_status handle_state(struct bt_msg_iter *notit)
e98a2d6e 1566{
b09a5592 1567 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
fdf0e7a0 1568 const enum state state = notit->state;
e98a2d6e 1569
fdf0e7a0
PP
1570 BT_LOGV("Handling state: notit-addr=%p, state=%s",
1571 notit, state_string(state));
e98a2d6e
PP
1572
1573 // TODO: optimalize!
fdf0e7a0 1574 switch (state) {
e98a2d6e
PP
1575 case STATE_INIT:
1576 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1577 break;
1578 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
1579 status = read_packet_header_begin_state(notit);
1580 break;
1581 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
1582 status = read_packet_header_continue_state(notit);
1583 break;
1584 case STATE_AFTER_TRACE_PACKET_HEADER:
1585 status = after_packet_header_state(notit);
1586 break;
1587 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN:
1588 status = read_packet_context_begin_state(notit);
1589 break;
1590 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE:
1591 status = read_packet_context_continue_state(notit);
1592 break;
1593 case STATE_AFTER_STREAM_PACKET_CONTEXT:
1594 status = after_packet_context_state(notit);
1595 break;
989925d1
PP
1596 case STATE_CHECK_EMIT_MSG_STREAM_BEGINNING:
1597 status = check_emit_msg_stream_beginning_state(notit);
1598 break;
bbbd35bf
PP
1599 case STATE_EMIT_MSG_STREAM_BEGINNING:
1600 notit->state = STATE_EMIT_MSG_STREAM_ACTIVITY_BEGINNING;
1601 break;
1602 case STATE_EMIT_MSG_STREAM_ACTIVITY_BEGINNING:
989925d1
PP
1603 notit->state = STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS;
1604 break;
1605 case STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS:
1606 status = check_emit_msg_discarded_events(notit);
1607 break;
1608 case STATE_EMIT_MSG_DISCARDED_EVENTS:
1609 notit->state = STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS;
1610 break;
1611 case STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS:
1612 status = check_emit_msg_discarded_packets(notit);
1613 break;
1614 case STATE_EMIT_MSG_DISCARDED_PACKETS:
bbbd35bf 1615 notit->state = STATE_EMIT_MSG_PACKET_BEGINNING;
6ff151ad 1616 break;
bbbd35bf 1617 case STATE_EMIT_MSG_PACKET_BEGINNING:
7b33a0e0 1618 notit->state = STATE_DSCOPE_EVENT_HEADER_BEGIN;
e98a2d6e 1619 break;
7b33a0e0 1620 case STATE_DSCOPE_EVENT_HEADER_BEGIN:
e98a2d6e
PP
1621 status = read_event_header_begin_state(notit);
1622 break;
7b33a0e0 1623 case STATE_DSCOPE_EVENT_HEADER_CONTINUE:
e98a2d6e
PP
1624 status = read_event_header_continue_state(notit);
1625 break;
7b33a0e0 1626 case STATE_AFTER_EVENT_HEADER:
e98a2d6e
PP
1627 status = after_event_header_state(notit);
1628 break;
7b33a0e0
PP
1629 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN:
1630 status = read_event_common_context_begin_state(notit);
e98a2d6e 1631 break;
7b33a0e0
PP
1632 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE:
1633 status = read_event_common_context_continue_state(notit);
e98a2d6e 1634 break;
7b33a0e0
PP
1635 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN:
1636 status = read_event_spec_context_begin_state(notit);
e98a2d6e 1637 break;
7b33a0e0
PP
1638 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE:
1639 status = read_event_spec_context_continue_state(notit);
e98a2d6e
PP
1640 break;
1641 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN:
1642 status = read_event_payload_begin_state(notit);
1643 break;
1644 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE:
1645 status = read_event_payload_continue_state(notit);
1646 break;
b09a5592 1647 case STATE_EMIT_MSG_EVENT:
7b33a0e0 1648 notit->state = STATE_DSCOPE_EVENT_HEADER_BEGIN;
e98a2d6e
PP
1649 break;
1650 case STATE_SKIP_PACKET_PADDING:
1651 status = skip_packet_padding_state(notit);
1652 break;
bbbd35bf 1653 case STATE_EMIT_MSG_PACKET_END_MULTI:
e98a2d6e
PP
1654 notit->state = STATE_SKIP_PACKET_PADDING;
1655 break;
bbbd35bf 1656 case STATE_EMIT_MSG_PACKET_END_SINGLE:
989925d1
PP
1657 notit->state = STATE_CHECK_EMIT_MSG_STREAM_ACTIVITY_END;
1658 break;
1659 case STATE_CHECK_EMIT_MSG_STREAM_ACTIVITY_END:
1660 status = check_emit_msg_stream_activity_end(notit);
bbbd35bf
PP
1661 break;
1662 case STATE_EMIT_MSG_STREAM_ACTIVITY_END:
1663 notit->state = STATE_EMIT_MSG_STREAM_END;
1664 break;
1665 case STATE_EMIT_MSG_STREAM_END:
1666 notit->state = STATE_DONE;
1667 break;
1668 case STATE_DONE:
1669 break;
fdf0e7a0 1670 default:
b09a5592 1671 BT_LOGD("Unknown CTF plugin message iterator state: "
fdf0e7a0
PP
1672 "notit-addr=%p, state=%d", notit, notit->state);
1673 abort();
e98a2d6e
PP
1674 }
1675
fdf0e7a0
PP
1676 BT_LOGV("Handled state: notit-addr=%p, status=%s, "
1677 "prev-state=%s, cur-state=%s",
b09a5592 1678 notit, bt_msg_iter_status_string(status),
fdf0e7a0 1679 state_string(state), state_string(notit->state));
e98a2d6e
PP
1680 return status;
1681}
1682
6ff151ad 1683BT_HIDDEN
989925d1 1684void bt_msg_iter_reset_for_next_stream_file(struct bt_msg_iter *notit)
e98a2d6e 1685{
8b45963b 1686 BT_ASSERT(notit);
b09a5592 1687 BT_LOGD("Resetting message iterator: addr=%p", notit);
e98a2d6e 1688 stack_clear(notit->stack);
7b33a0e0
PP
1689 notit->meta.sc = NULL;
1690 notit->meta.ec = NULL;
8c6884d9
PP
1691 BT_PACKET_PUT_REF_AND_RESET(notit->packet);
1692 BT_STREAM_PUT_REF_AND_RESET(notit->stream);
b09a5592 1693 BT_MESSAGE_PUT_REF_AND_RESET(notit->event_msg);
a6918753
PP
1694 release_all_dscopes(notit);
1695 notit->cur_dscope_field = NULL;
1696
a6918753 1697 if (notit->packet_context_field) {
78cf9df6 1698 bt_packet_context_field_release(notit->packet_context_field);
a6918753
PP
1699 notit->packet_context_field = NULL;
1700 }
1701
e98a2d6e
PP
1702 notit->buf.addr = NULL;
1703 notit->buf.sz = 0;
1704 notit->buf.at = 0;
bb8744a7 1705 notit->buf.last_eh_at = SIZE_MAX;
e98a2d6e
PP
1706 notit->buf.packet_offset = 0;
1707 notit->state = STATE_INIT;
7b33a0e0
PP
1708 notit->cur_exp_packet_content_size = -1;
1709 notit->cur_exp_packet_total_size = -1;
bb230c9b 1710 notit->cur_packet_offset = -1;
7b33a0e0 1711 notit->cur_event_class_id = -1;
989925d1
PP
1712 notit->snapshots.beginning_clock = UINT64_C(-1);
1713 notit->snapshots.end_clock = UINT64_C(-1);
1714}
1715
1716/**
1717 * Resets the internal state of a CTF message iterator.
1718 */
1719BT_HIDDEN
1720void bt_msg_iter_reset(struct bt_msg_iter *notit)
1721{
1722 bt_msg_iter_reset_for_next_stream_file(notit);
1723 notit->cur_stream_class_id = -1;
7b33a0e0 1724 notit->cur_data_stream_id = -1;
bbbd35bf
PP
1725 notit->emit_stream_begin_msg = true;
1726 notit->emit_stream_end_msg = true;
989925d1
PP
1727 notit->snapshots.discarded_events = UINT64_C(-1);
1728 notit->snapshots.packets = UINT64_C(-1);
1729 notit->prev_packet_snapshots.discarded_events = UINT64_C(-1);
1730 notit->prev_packet_snapshots.packets = UINT64_C(-1);
1731 notit->prev_packet_snapshots.beginning_clock = UINT64_C(-1);
1732 notit->prev_packet_snapshots.end_clock = UINT64_C(-1);
e98a2d6e
PP
1733}
1734
2cf1d51e 1735static
b09a5592 1736int bt_msg_iter_switch_packet(struct bt_msg_iter *notit)
2cf1d51e
JG
1737{
1738 int ret = 0;
1739
c937c615
PP
1740 /*
1741 * We don't put the stream class here because we need to make
b09a5592 1742 * sure that all the packets processed by the same message
c937c615
PP
1743 * iterator refer to the same stream class (the first one).
1744 */
8b45963b 1745 BT_ASSERT(notit);
a6918753 1746
7b33a0e0
PP
1747 if (notit->cur_exp_packet_total_size != -1) {
1748 notit->cur_packet_offset += notit->cur_exp_packet_total_size;
bb230c9b 1749 }
a6918753 1750
bb230c9b
JG
1751 BT_LOGV("Switching packet: notit-addr=%p, cur=%zu, "
1752 "packet-offset=%" PRId64, notit, notit->buf.at,
1753 notit->cur_packet_offset);
2cf1d51e 1754 stack_clear(notit->stack);
7b33a0e0 1755 notit->meta.ec = NULL;
8c6884d9 1756 BT_PACKET_PUT_REF_AND_RESET(notit->packet);
b09a5592 1757 BT_MESSAGE_PUT_REF_AND_RESET(notit->event_msg);
a6918753
PP
1758 release_all_dscopes(notit);
1759 notit->cur_dscope_field = NULL;
2cf1d51e
JG
1760
1761 /*
1762 * Adjust current buffer so that addr points to the beginning of the new
1763 * packet.
1764 */
1765 if (notit->buf.addr) {
1766 size_t consumed_bytes = (size_t) (notit->buf.at / CHAR_BIT);
1767
1768 /* Packets are assumed to start on a byte frontier. */
1769 if (notit->buf.at % CHAR_BIT) {
fdf0e7a0
PP
1770 BT_LOGW("Cannot switch packet: current position is not a multiple of 8: "
1771 "notit-addr=%p, cur=%zu", notit, notit->buf.at);
2cf1d51e
JG
1772 ret = -1;
1773 goto end;
1774 }
1775
1776 notit->buf.addr += consumed_bytes;
1777 notit->buf.sz -= consumed_bytes;
1778 notit->buf.at = 0;
1779 notit->buf.packet_offset = 0;
fdf0e7a0
PP
1780 BT_LOGV("Adjusted buffer: addr=%p, size=%zu",
1781 notit->buf.addr, notit->buf.sz);
2cf1d51e
JG
1782 }
1783
7b33a0e0
PP
1784 notit->cur_exp_packet_content_size = -1;
1785 notit->cur_exp_packet_total_size = -1;
1786 notit->cur_stream_class_id = -1;
1787 notit->cur_event_class_id = -1;
1788 notit->cur_data_stream_id = -1;
989925d1 1789 notit->prev_packet_snapshots = notit->snapshots;
7b33a0e0
PP
1790 notit->snapshots.discarded_events = UINT64_C(-1);
1791 notit->snapshots.packets = UINT64_C(-1);
1792 notit->snapshots.beginning_clock = UINT64_C(-1);
1793 notit->snapshots.end_clock = UINT64_C(-1);
fdf0e7a0 1794
2cf1d51e
JG
1795end:
1796 return ret;
1797}
1798
e98a2d6e 1799static
b09a5592 1800bt_field *borrow_next_field(struct bt_msg_iter *notit)
e98a2d6e 1801{
8eee8ea2
PP
1802 bt_field *next_field = NULL;
1803 bt_field *base_field;
1804 const bt_field_class *base_fc;
e98a2d6e
PP
1805 size_t index;
1806
8b45963b 1807 BT_ASSERT(!stack_empty(notit->stack));
e98a2d6e
PP
1808 index = stack_top(notit->stack)->index;
1809 base_field = stack_top(notit->stack)->base;
8b45963b 1810 BT_ASSERT(base_field);
78cf9df6 1811 base_fc = bt_field_borrow_class_const(base_field);
939190b3 1812 BT_ASSERT(base_fc);
e98a2d6e 1813
78cf9df6 1814 switch (bt_field_class_get_type(base_fc)) {
af0c18e3 1815 case BT_FIELD_CLASS_TYPE_STRUCTURE:
6ff151ad 1816 {
7b33a0e0 1817 BT_ASSERT(index <
939190b3 1818 bt_field_class_structure_get_member_count(
78cf9df6
PP
1819 bt_field_borrow_class_const(
1820 base_field)));
9e550e5f 1821 next_field =
78cf9df6 1822 bt_field_structure_borrow_member_field_by_index(
9e550e5f 1823 base_field, index);
e98a2d6e 1824 break;
6ff151ad 1825 }
af0c18e3
PP
1826 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
1827 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
78cf9df6
PP
1828 BT_ASSERT(index < bt_field_array_get_length(base_field));
1829 next_field = bt_field_array_borrow_element_field_by_index(
7b33a0e0 1830 base_field, index);
e98a2d6e 1831 break;
af0c18e3 1832 case BT_FIELD_CLASS_TYPE_VARIANT:
7b33a0e0 1833 BT_ASSERT(index == 0);
78cf9df6 1834 next_field = bt_field_variant_borrow_selected_option_field(
7b33a0e0 1835 base_field);
e98a2d6e
PP
1836 break;
1837 default:
0fbb9a9f 1838 abort();
e98a2d6e
PP
1839 }
1840
7b33a0e0 1841 BT_ASSERT(next_field);
e98a2d6e
PP
1842 return next_field;
1843}
1844
c44c3e70 1845static
b09a5592 1846void update_default_clock(struct bt_msg_iter *notit, uint64_t new_val,
7b33a0e0 1847 uint64_t new_val_size)
c44c3e70 1848{
7b33a0e0 1849 uint64_t new_val_mask;
c44c3e70 1850 uint64_t cur_value_masked;
c44c3e70 1851
7b33a0e0 1852 BT_ASSERT(new_val_size > 0);
c44c3e70
JG
1853
1854 /*
1855 * Special case for a 64-bit new value, which is the limit
1856 * of a clock value as of this version: overwrite the
1857 * current value directly.
1858 */
7b33a0e0 1859 if (new_val_size == 64) {
ecbb78c0 1860 notit->default_clock_snapshot = new_val;
c44c3e70
JG
1861 goto end;
1862 }
1863
7b33a0e0 1864 new_val_mask = (1ULL << new_val_size) - 1;
ecbb78c0 1865 cur_value_masked = notit->default_clock_snapshot & new_val_mask;
c44c3e70 1866
7b33a0e0 1867 if (new_val < cur_value_masked) {
c44c3e70
JG
1868 /*
1869 * It looks like a wrap happened on the number of bits
1870 * of the requested new value. Assume that the clock
1871 * value wrapped only one time.
1872 */
ecbb78c0 1873 notit->default_clock_snapshot += new_val_mask + 1;
c44c3e70
JG
1874 }
1875
1876 /* Clear the low bits of the current clock value. */
ecbb78c0 1877 notit->default_clock_snapshot &= ~new_val_mask;
c44c3e70
JG
1878
1879 /* Set the low bits of the current clock value. */
ecbb78c0 1880 notit->default_clock_snapshot |= new_val;
b8418c69 1881
c44c3e70 1882end:
7b33a0e0 1883 BT_LOGV("Updated default clock's value from integer field's value: "
ecbb78c0 1884 "value=%" PRIu64, notit->default_clock_snapshot);
c44c3e70
JG
1885}
1886
1887static
939190b3
PP
1888enum bt_bfcr_status bfcr_unsigned_int_cb(uint64_t value,
1889 struct ctf_field_class *fc, void *data)
c44c3e70 1890{
b09a5592 1891 struct bt_msg_iter *notit = data;
939190b3 1892 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
8eee8ea2 1893 bt_field *field = NULL;
939190b3 1894 struct ctf_field_class_int *int_fc = (void *) fc;
c44c3e70 1895
939190b3
PP
1896 BT_LOGV("Unsigned integer function called from BFCR: "
1897 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
af0c18e3
PP
1898 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64,
1899 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
a6918753 1900
85e7137b 1901 if (G_LIKELY(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE)) {
7b33a0e0 1902 goto update_def_clock;
c44c3e70
JG
1903 }
1904
939190b3
PP
1905 switch (int_fc->meaning) {
1906 case CTF_FIELD_CLASS_MEANING_EVENT_CLASS_ID:
7b33a0e0
PP
1907 notit->cur_event_class_id = value;
1908 break;
939190b3 1909 case CTF_FIELD_CLASS_MEANING_DATA_STREAM_ID:
7b33a0e0
PP
1910 notit->cur_data_stream_id = value;
1911 break;
939190b3 1912 case CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME:
7b33a0e0
PP
1913 notit->snapshots.beginning_clock = value;
1914 break;
939190b3 1915 case CTF_FIELD_CLASS_MEANING_PACKET_END_TIME:
7b33a0e0
PP
1916 notit->snapshots.end_clock = value;
1917 break;
939190b3 1918 case CTF_FIELD_CLASS_MEANING_STREAM_CLASS_ID:
7b33a0e0
PP
1919 notit->cur_stream_class_id = value;
1920 break;
939190b3 1921 case CTF_FIELD_CLASS_MEANING_MAGIC:
7b33a0e0
PP
1922 if (value != 0xc1fc1fc1) {
1923 BT_LOGW("Invalid CTF magic number: notit-addr=%p, "
1924 "magic=%" PRIx64, notit, value);
939190b3 1925 status = BT_BFCR_STATUS_ERROR;
c44c3e70
JG
1926 goto end;
1927 }
fdf0e7a0 1928
7b33a0e0 1929 break;
939190b3 1930 case CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT:
7b33a0e0
PP
1931 notit->snapshots.packets = value;
1932 break;
939190b3 1933 case CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT:
7b33a0e0
PP
1934 notit->snapshots.discarded_events = value;
1935 break;
939190b3 1936 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_TOTAL_SIZE:
7b33a0e0
PP
1937 notit->cur_exp_packet_total_size = value;
1938 break;
939190b3 1939 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_CONTENT_SIZE:
7b33a0e0
PP
1940 notit->cur_exp_packet_content_size = value;
1941 break;
1942 default:
1943 abort();
c44c3e70
JG
1944 }
1945
7b33a0e0 1946update_def_clock:
85e7137b 1947 if (G_UNLIKELY(int_fc->mapped_clock_class)) {
939190b3 1948 update_default_clock(notit, value, int_fc->base.size);
7b33a0e0 1949 }
c44c3e70 1950
85e7137b 1951 if (G_UNLIKELY(int_fc->storing_index >= 0)) {
7b33a0e0 1952 g_array_index(notit->stored_values, uint64_t,
939190b3 1953 (uint64_t) int_fc->storing_index) = value;
7b33a0e0 1954 }
e98a2d6e 1955
85e7137b 1956 if (G_UNLIKELY(!fc->in_ir)) {
a6918753 1957 goto end;
e98a2d6e
PP
1958 }
1959
7b33a0e0
PP
1960 field = borrow_next_field(notit);
1961 BT_ASSERT(field);
78cf9df6
PP
1962 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
1963 BT_ASSERT(bt_field_get_class_type(field) ==
1964 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER ||
1965 bt_field_get_class_type(field) ==
1966 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION);
1967 bt_field_unsigned_integer_set_value(field, value);
e98a2d6e 1968 stack_top(notit->stack)->index++;
fdf0e7a0 1969
a6918753 1970end:
e98a2d6e
PP
1971 return status;
1972}
1973
5f870343 1974static
939190b3
PP
1975enum bt_bfcr_status bfcr_unsigned_int_char_cb(uint64_t value,
1976 struct ctf_field_class *fc, void *data)
5f870343 1977{
7b33a0e0 1978 int ret;
b09a5592 1979 struct bt_msg_iter *notit = data;
939190b3 1980 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
8eee8ea2 1981 bt_field *string_field = NULL;
939190b3 1982 struct ctf_field_class_int *int_fc = (void *) fc;
7b33a0e0 1983 char str[2] = {'\0', '\0'};
5f870343 1984
939190b3
PP
1985 BT_LOGV("Unsigned integer character function called from BFCR: "
1986 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
af0c18e3
PP
1987 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64,
1988 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
939190b3
PP
1989 BT_ASSERT(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE);
1990 BT_ASSERT(!int_fc->mapped_clock_class);
1991 BT_ASSERT(int_fc->storing_index < 0);
a6918753 1992
85e7137b 1993 if (G_UNLIKELY(!fc->in_ir)) {
7b33a0e0
PP
1994 goto end;
1995 }
5f870343 1996
7b33a0e0 1997 if (notit->done_filling_string) {
5f870343
JG
1998 goto end;
1999 }
2000
7b33a0e0
PP
2001 if (value == 0) {
2002 notit->done_filling_string = true;
5f870343
JG
2003 goto end;
2004 }
2005
7b33a0e0 2006 string_field = stack_top(notit->stack)->base;
78cf9df6
PP
2007 BT_ASSERT(bt_field_get_class_type(string_field) ==
2008 BT_FIELD_CLASS_TYPE_STRING);
7b33a0e0
PP
2009
2010 /* Append character */
2011 str[0] = (char) value;
78cf9df6 2012 ret = bt_field_string_append_with_length(string_field, str, 1);
7b33a0e0
PP
2013 if (ret) {
2014 BT_LOGE("Cannot append character to string field's value: "
2015 "notit-addr=%p, field-addr=%p, ret=%d",
2016 notit, string_field, ret);
939190b3 2017 status = BT_BFCR_STATUS_ERROR;
7b33a0e0
PP
2018 goto end;
2019 }
a6918753 2020
5f870343
JG
2021end:
2022 return status;
2023}
2024
2025static
939190b3
PP
2026enum bt_bfcr_status bfcr_signed_int_cb(int64_t value,
2027 struct ctf_field_class *fc, void *data)
e98a2d6e 2028{
939190b3 2029 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
8eee8ea2 2030 bt_field *field = NULL;
b09a5592 2031 struct bt_msg_iter *notit = data;
939190b3 2032 struct ctf_field_class_int *int_fc = (void *) fc;
e98a2d6e 2033
939190b3
PP
2034 BT_LOGV("Signed integer function called from BFCR: "
2035 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
af0c18e3
PP
2036 "fc-type=%d, fc-in-ir=%d, value=%" PRId64,
2037 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
939190b3 2038 BT_ASSERT(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE);
7b33a0e0 2039
85e7137b 2040 if (G_UNLIKELY(int_fc->storing_index >= 0)) {
7b33a0e0 2041 g_array_index(notit->stored_values, uint64_t,
939190b3 2042 (uint64_t) int_fc->storing_index) = (uint64_t) value;
7b33a0e0
PP
2043 }
2044
85e7137b 2045 if (G_UNLIKELY(!fc->in_ir)) {
a6918753 2046 goto end;
e98a2d6e
PP
2047 }
2048
7b33a0e0
PP
2049 field = borrow_next_field(notit);
2050 BT_ASSERT(field);
78cf9df6
PP
2051 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
2052 BT_ASSERT(bt_field_get_class_type(field) ==
2053 BT_FIELD_CLASS_TYPE_SIGNED_INTEGER ||
2054 bt_field_get_class_type(field) ==
2055 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION);
2056 bt_field_signed_integer_set_value(field, value);
e98a2d6e 2057 stack_top(notit->stack)->index++;
fdf0e7a0 2058
a6918753 2059end:
e98a2d6e
PP
2060 return status;
2061}
2062
2063static
939190b3
PP
2064enum bt_bfcr_status bfcr_floating_point_cb(double value,
2065 struct ctf_field_class *fc, void *data)
e98a2d6e 2066{
939190b3 2067 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
8eee8ea2 2068 bt_field *field = NULL;
b09a5592 2069 struct bt_msg_iter *notit = data;
e98a2d6e 2070
939190b3
PP
2071 BT_LOGV("Floating point number function called from BFCR: "
2072 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
af0c18e3
PP
2073 "fc-type=%d, fc-in-ir=%d, value=%f",
2074 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
e5e71bf8 2075
85e7137b 2076 if (G_UNLIKELY(!fc->in_ir)) {
e5e71bf8
PP
2077 goto end;
2078 }
2079
a6918753 2080 field = borrow_next_field(notit);
7b33a0e0 2081 BT_ASSERT(field);
78cf9df6
PP
2082 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
2083 BT_ASSERT(bt_field_get_class_type(field) ==
2084 BT_FIELD_CLASS_TYPE_REAL);
2085 bt_field_real_set_value(field, value);
e98a2d6e 2086 stack_top(notit->stack)->index++;
e5e71bf8
PP
2087
2088end:
e98a2d6e
PP
2089 return status;
2090}
2091
2092static
939190b3
PP
2093enum bt_bfcr_status bfcr_string_begin_cb(
2094 struct ctf_field_class *fc, void *data)
e98a2d6e 2095{
8eee8ea2 2096 bt_field *field = NULL;
b09a5592 2097 struct bt_msg_iter *notit = data;
e98a2d6e
PP
2098 int ret;
2099
939190b3
PP
2100 BT_LOGV("String (beginning) function called from BFCR: "
2101 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
af0c18e3
PP
2102 "fc-type=%d, fc-in-ir=%d",
2103 notit, notit->bfcr, fc, fc->type, fc->in_ir);
e98a2d6e 2104
85e7137b 2105 if (G_UNLIKELY(!fc->in_ir)) {
e5e71bf8
PP
2106 goto end;
2107 }
2108
7b33a0e0
PP
2109 field = borrow_next_field(notit);
2110 BT_ASSERT(field);
78cf9df6
PP
2111 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
2112 BT_ASSERT(bt_field_get_class_type(field) ==
2113 BT_FIELD_CLASS_TYPE_STRING);
2114 ret = bt_field_string_clear(field);
a6918753
PP
2115 BT_ASSERT(ret == 0);
2116
e98a2d6e 2117 /*
939190b3
PP
2118 * Push on stack. Not a compound class per se, but we know that
2119 * only bfcr_string_cb() may be called between this call and a
2120 * subsequent call to bfcr_string_end_cb().
e98a2d6e 2121 */
7b33a0e0 2122 stack_push(notit->stack, field);
e5e71bf8
PP
2123
2124end:
939190b3 2125 return BT_BFCR_STATUS_OK;
e98a2d6e
PP
2126}
2127
2128static
939190b3
PP
2129enum bt_bfcr_status bfcr_string_cb(const char *value,
2130 size_t len, struct ctf_field_class *fc, void *data)
e98a2d6e 2131{
939190b3 2132 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
8eee8ea2 2133 bt_field *field = NULL;
b09a5592 2134 struct bt_msg_iter *notit = data;
e98a2d6e
PP
2135 int ret;
2136
939190b3
PP
2137 BT_LOGV("String (substring) function called from BFCR: "
2138 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
af0c18e3
PP
2139 "fc-type=%d, fc-in-ir=%d, string-length=%zu",
2140 notit, notit->bfcr, fc, fc->type, fc->in_ir,
fdf0e7a0 2141 len);
e5e71bf8 2142
85e7137b 2143 if (G_UNLIKELY(!fc->in_ir)) {
e5e71bf8
PP
2144 goto end;
2145 }
2146
e98a2d6e 2147 field = stack_top(notit->stack)->base;
8b45963b 2148 BT_ASSERT(field);
e98a2d6e 2149
a6918753 2150 /* Append current substring */
78cf9df6 2151 ret = bt_field_string_append_with_length(field, value, len);
e98a2d6e 2152 if (ret) {
fdf0e7a0
PP
2153 BT_LOGE("Cannot append substring to string field's value: "
2154 "notit-addr=%p, field-addr=%p, string-length=%zu, "
2155 "ret=%d", notit, field, len, ret);
939190b3 2156 status = BT_BFCR_STATUS_ERROR;
e98a2d6e
PP
2157 goto end;
2158 }
2159
2160end:
2161 return status;
2162}
2163
2164static
939190b3
PP
2165enum bt_bfcr_status bfcr_string_end_cb(
2166 struct ctf_field_class *fc, void *data)
e98a2d6e 2167{
b09a5592 2168 struct bt_msg_iter *notit = data;
e98a2d6e 2169
939190b3
PP
2170 BT_LOGV("String (end) function called from BFCR: "
2171 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
af0c18e3
PP
2172 "fc-type=%d, fc-in-ir=%d",
2173 notit, notit->bfcr, fc, fc->type, fc->in_ir);
e5e71bf8 2174
85e7137b 2175 if (G_UNLIKELY(!fc->in_ir)) {
e5e71bf8
PP
2176 goto end;
2177 }
fdf0e7a0 2178
e98a2d6e
PP
2179 /* Pop string field */
2180 stack_pop(notit->stack);
2181
2182 /* Go to next field */
2183 stack_top(notit->stack)->index++;
e5e71bf8
PP
2184
2185end:
939190b3 2186 return BT_BFCR_STATUS_OK;
e98a2d6e
PP
2187}
2188
939190b3
PP
2189enum bt_bfcr_status bfcr_compound_begin_cb(
2190 struct ctf_field_class *fc, void *data)
e98a2d6e 2191{
b09a5592 2192 struct bt_msg_iter *notit = data;
8eee8ea2 2193 bt_field *field;
e98a2d6e 2194
939190b3
PP
2195 BT_LOGV("Compound (beginning) function called from BFCR: "
2196 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
af0c18e3
PP
2197 "fc-type=%d, fc-in-ir=%d",
2198 notit, notit->bfcr, fc, fc->type, fc->in_ir);
7b33a0e0 2199
939190b3 2200 if (!fc->in_ir) {
7b33a0e0
PP
2201 goto end;
2202 }
fdf0e7a0 2203
a6918753 2204 /* Borrow field */
e98a2d6e 2205 if (stack_empty(notit->stack)) {
a6918753
PP
2206 /* Root: already set by read_dscope_begin_state() */
2207 field = notit->cur_dscope_field;
e98a2d6e 2208 } else {
a6918753 2209 field = borrow_next_field(notit);
7b33a0e0 2210 BT_ASSERT(field);
e98a2d6e
PP
2211 }
2212
2213 /* Push field */
8b45963b 2214 BT_ASSERT(field);
78cf9df6 2215 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
7b33a0e0
PP
2216 stack_push(notit->stack, field);
2217
2218 /*
939190b3 2219 * Change BFCR "unsigned int" callback if it's a text
7b33a0e0
PP
2220 * array/sequence.
2221 */
af0c18e3
PP
2222 if (fc->type == CTF_FIELD_CLASS_TYPE_ARRAY ||
2223 fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE) {
939190b3 2224 struct ctf_field_class_array_base *array_fc = (void *) fc;
7b33a0e0 2225
939190b3 2226 if (array_fc->is_text) {
7b33a0e0
PP
2227 int ret;
2228
78cf9df6
PP
2229 BT_ASSERT(bt_field_get_class_type(field) ==
2230 BT_FIELD_CLASS_TYPE_STRING);
7b33a0e0 2231 notit->done_filling_string = false;
78cf9df6 2232 ret = bt_field_string_clear(field);
7b33a0e0 2233 BT_ASSERT(ret == 0);
939190b3
PP
2234 bt_bfcr_set_unsigned_int_cb(notit->bfcr,
2235 bfcr_unsigned_int_char_cb);
7b33a0e0 2236 }
e98a2d6e
PP
2237 }
2238
2239end:
939190b3 2240 return BT_BFCR_STATUS_OK;
e98a2d6e
PP
2241}
2242
939190b3
PP
2243enum bt_bfcr_status bfcr_compound_end_cb(
2244 struct ctf_field_class *fc, void *data)
e98a2d6e 2245{
b09a5592 2246 struct bt_msg_iter *notit = data;
e98a2d6e 2247
939190b3
PP
2248 BT_LOGV("Compound (end) function called from BFCR: "
2249 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
af0c18e3
PP
2250 "fc-type=%d, fc-in-ir=%d",
2251 notit, notit->bfcr, fc, fc->type, fc->in_ir);
e98a2d6e 2252
939190b3 2253 if (!fc->in_ir) {
e98a2d6e
PP
2254 goto end;
2255 }
2256
7b33a0e0 2257 BT_ASSERT(!stack_empty(notit->stack));
78cf9df6 2258 BT_ASSERT(bt_field_borrow_class_const(stack_top(notit->stack)->base) ==
939190b3 2259 fc->ir_fc);
e98a2d6e 2260
7b33a0e0 2261 /*
939190b3 2262 * Reset BFCR "unsigned int" callback if it's a text
7b33a0e0
PP
2263 * array/sequence.
2264 */
af0c18e3
PP
2265 if (fc->type == CTF_FIELD_CLASS_TYPE_ARRAY ||
2266 fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE) {
939190b3 2267 struct ctf_field_class_array_base *array_fc = (void *) fc;
7b33a0e0 2268
939190b3 2269 if (array_fc->is_text) {
af0c18e3 2270 BT_ASSERT(bt_field_get_class_type(
78cf9df6 2271 stack_top(notit->stack)->base) ==
9e550e5f 2272 BT_FIELD_CLASS_TYPE_STRING);
939190b3
PP
2273 bt_bfcr_set_unsigned_int_cb(notit->bfcr,
2274 bfcr_unsigned_int_cb);
e98a2d6e 2275 }
7b33a0e0 2276 }
e98a2d6e 2277
7b33a0e0
PP
2278 /* Pop stack */
2279 stack_pop(notit->stack);
e98a2d6e 2280
7b33a0e0
PP
2281 /* If the stack is not empty, increment the base's index */
2282 if (!stack_empty(notit->stack)) {
2283 stack_top(notit->stack)->index++;
e98a2d6e
PP
2284 }
2285
2286end:
939190b3 2287 return BT_BFCR_STATUS_OK;
e98a2d6e
PP
2288}
2289
2290static
939190b3 2291int64_t bfcr_get_sequence_length_cb(struct ctf_field_class *fc, void *data)
e98a2d6e 2292{
8eee8ea2 2293 bt_field *seq_field;
b09a5592 2294 struct bt_msg_iter *notit = data;
939190b3 2295 struct ctf_field_class_sequence *seq_fc = (void *) fc;
7b33a0e0
PP
2296 int64_t length = -1;
2297 int ret;
e98a2d6e 2298
7b33a0e0 2299 length = (uint64_t) g_array_index(notit->stored_values, uint64_t,
939190b3 2300 seq_fc->stored_length_index);
fdf0e7a0 2301 seq_field = stack_top(notit->stack)->base;
7b33a0e0 2302 BT_ASSERT(seq_field);
c16748c7
PP
2303
2304 /*
2305 * bfcr_get_sequence_length_cb() also gets called back for a
2306 * text sequence, but the destination field is a string field.
2307 * Only set the field's sequence length if the destination field
2308 * is a sequence field.
2309 */
2310 if (!seq_fc->base.is_text) {
2311 BT_ASSERT(bt_field_get_class_type(seq_field) ==
2312 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY);
2313 ret = bt_field_dynamic_array_set_length(seq_field,
2314 (uint64_t) length);
2315 if (ret) {
2316 BT_LOGE("Cannot set dynamic array field's length field: "
2317 "notit-addr=%p, field-addr=%p, "
2318 "length=%" PRIu64, notit, seq_field, length);
2319 }
2cf1d51e 2320 }
fdf0e7a0 2321
7b33a0e0 2322 return length;
e98a2d6e
PP
2323}
2324
2325static
939190b3
PP
2326struct ctf_field_class *bfcr_borrow_variant_selected_field_class_cb(
2327 struct ctf_field_class *fc, void *data)
e98a2d6e 2328{
a6918753 2329 int ret;
7b33a0e0
PP
2330 uint64_t i;
2331 int64_t option_index = -1;
b09a5592 2332 struct bt_msg_iter *notit = data;
939190b3
PP
2333 struct ctf_field_class_variant *var_fc = (void *) fc;
2334 struct ctf_named_field_class *selected_option = NULL;
2335 struct ctf_field_class *ret_fc = NULL;
7b33a0e0
PP
2336 union {
2337 uint64_t u;
2338 int64_t i;
2339 } tag;
2340
2341 /* Get variant's tag */
2342 tag.u = g_array_index(notit->stored_values, uint64_t,
939190b3 2343 var_fc->stored_tag_index);
e98a2d6e
PP
2344
2345 /*
7b33a0e0 2346 * Check each range to find the selected option's index.
e98a2d6e 2347 */
939190b3
PP
2348 if (var_fc->tag_fc->base.is_signed) {
2349 for (i = 0; i < var_fc->ranges->len; i++) {
2350 struct ctf_field_class_variant_range *range =
2351 ctf_field_class_variant_borrow_range_by_index(
2352 var_fc, i);
7b33a0e0
PP
2353
2354 if (tag.i >= range->range.lower.i &&
2355 tag.i <= range->range.upper.i) {
2356 option_index = (int64_t) range->option_index;
2357 break;
2358 }
2359 }
a6918753 2360 } else {
939190b3
PP
2361 for (i = 0; i < var_fc->ranges->len; i++) {
2362 struct ctf_field_class_variant_range *range =
2363 ctf_field_class_variant_borrow_range_by_index(
2364 var_fc, i);
7b33a0e0
PP
2365
2366 if (tag.u >= range->range.lower.u &&
2367 tag.u <= range->range.upper.u) {
2368 option_index = (int64_t) range->option_index;
2369 break;
2370 }
2371 }
a6918753
PP
2372 }
2373
7b33a0e0 2374 if (option_index < 0) {
939190b3
PP
2375 BT_LOGW("Cannot find variant field class's option: "
2376 "notit-addr=%p, var-fc-addr=%p, u-tag=%" PRIu64 ", "
2377 "i-tag=%" PRId64, notit, var_fc, tag.u, tag.i);
e98a2d6e
PP
2378 goto end;
2379 }
2380
939190b3
PP
2381 selected_option = ctf_field_class_variant_borrow_option_by_index(
2382 var_fc, (uint64_t) option_index);
e98a2d6e 2383
939190b3 2384 if (selected_option->fc->in_ir) {
8eee8ea2 2385 bt_field *var_field = stack_top(notit->stack)->base;
1556a1af 2386
78cf9df6 2387 ret = bt_field_variant_select_option_field(
9e550e5f 2388 var_field, option_index);
8fc063a2 2389 if (ret) {
7b33a0e0
PP
2390 BT_LOGW("Cannot select variant field's option field: "
2391 "notit-addr=%p, var-field-addr=%p, "
2392 "opt-index=%" PRId64, notit, var_field,
2393 option_index);
1556a1af
JG
2394 goto end;
2395 }
5184a1d6
PP
2396 }
2397
939190b3 2398 ret_fc = selected_option->fc;
8fc063a2 2399
5184a1d6 2400end:
939190b3 2401 return ret_fc;
7b33a0e0
PP
2402}
2403
6ff151ad 2404static
bbbd35bf 2405void create_msg_stream_beginning(struct bt_msg_iter *notit,
b09a5592 2406 bt_message **message)
6ff151ad 2407{
b09a5592 2408 bt_message *ret = NULL;
6ff151ad 2409
6ff151ad 2410 BT_ASSERT(notit->stream);
b09a5592
PP
2411 BT_ASSERT(notit->msg_iter);
2412 ret = bt_message_stream_beginning_create(notit->msg_iter,
03e18d5d 2413 notit->stream);
6ff151ad 2414 if (!ret) {
b09a5592 2415 BT_LOGE("Cannot create stream beginning message: "
6ff151ad
PP
2416 "notit-addr=%p, stream-addr=%p",
2417 notit, notit->stream);
2418 return;
2419 }
2420
b09a5592 2421 *message = ret;
6ff151ad
PP
2422}
2423
2424static
bbbd35bf
PP
2425void create_msg_stream_activity_beginning(struct bt_msg_iter *notit,
2426 bt_message **message)
2427{
2428 bt_message *ret = NULL;
2429
2430 BT_ASSERT(notit->stream);
2431 BT_ASSERT(notit->msg_iter);
2432 ret = bt_message_stream_activity_beginning_create(notit->msg_iter,
2433 notit->stream);
2434 if (!ret) {
2435 BT_LOGE("Cannot create stream activity beginning 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_activity_end(struct bt_msg_iter *notit,
b09a5592 2446 bt_message **message)
bbbd35bf
PP
2447{
2448 bt_message *ret = NULL;
2449
2450 if (!notit->stream) {
2451 BT_LOGE("Cannot create stream for stream message: "
2452 "notit-addr=%p", notit);
2453 return;
2454 }
2455
2456 BT_ASSERT(notit->stream);
2457 BT_ASSERT(notit->msg_iter);
2458 ret = bt_message_stream_activity_end_create(notit->msg_iter,
2459 notit->stream);
2460 if (!ret) {
2461 BT_LOGE("Cannot create stream activity end message: "
2462 "notit-addr=%p, stream-addr=%p",
2463 notit, notit->stream);
2464 return;
2465 }
2466
2467 *message = ret;
2468}
2469
2470static
2471void create_msg_stream_end(struct bt_msg_iter *notit, bt_message **message)
6ff151ad 2472{
b09a5592 2473 bt_message *ret;
6ff151ad
PP
2474
2475 if (!notit->stream) {
b09a5592 2476 BT_LOGE("Cannot create stream for stream message: "
6ff151ad
PP
2477 "notit-addr=%p", notit);
2478 return;
2479 }
2480
b09a5592
PP
2481 BT_ASSERT(notit->msg_iter);
2482 ret = bt_message_stream_end_create(notit->msg_iter,
03e18d5d 2483 notit->stream);
6ff151ad 2484 if (!ret) {
bbbd35bf 2485 BT_LOGE("Cannot create stream end message: "
6ff151ad
PP
2486 "notit-addr=%p, stream-addr=%p",
2487 notit, notit->stream);
2488 return;
2489 }
bbbd35bf 2490
b09a5592 2491 *message = ret;
6ff151ad
PP
2492}
2493
78586d8a 2494static
bbbd35bf 2495void create_msg_packet_beginning(struct bt_msg_iter *notit,
b09a5592 2496 bt_message **message)
e98a2d6e 2497{
a6918753 2498 int ret;
b09a5592
PP
2499 enum bt_msg_iter_status status;
2500 bt_message *msg = NULL;
8eee8ea2 2501 const bt_stream_class *sc;
e98a2d6e 2502
a6918753 2503 status = set_current_packet(notit);
b09a5592 2504 if (status != BT_MSG_ITER_STATUS_OK) {
a6918753 2505 goto end;
e98a2d6e
PP
2506 }
2507
a6918753 2508 BT_ASSERT(notit->packet);
78cf9df6 2509 sc = notit->meta.sc->ir_sc;
7b33a0e0
PP
2510 BT_ASSERT(sc);
2511
a6918753 2512 if (notit->packet_context_field) {
78cf9df6 2513 ret = bt_packet_move_context_field(
9e550e5f 2514 notit->packet, notit->packet_context_field);
a6918753
PP
2515 if (ret) {
2516 goto end;
2517 }
2518
2519 notit->packet_context_field = NULL;
2520
2521 /*
0476f6f7
PP
2522 * At this point notit->dscopes.stream_packet_context
2523 * has the same value as the packet context field within
a6918753
PP
2524 * notit->packet.
2525 */
78cf9df6 2526 BT_ASSERT(bt_packet_borrow_context_field(
9e550e5f 2527 notit->packet) ==
a6918753
PP
2528 notit->dscopes.stream_packet_context);
2529 }
2530
b09a5592 2531 BT_ASSERT(notit->msg_iter);
96a0a69d 2532
32476570
PP
2533 if (notit->meta.sc->packets_have_ts_begin) {
2534 BT_ASSERT(notit->snapshots.beginning_clock != UINT64_C(-1));
92f86a9e 2535 msg = bt_message_packet_beginning_create_with_default_clock_snapshot(
32476570
PP
2536 notit->msg_iter, notit->packet,
2537 notit->snapshots.beginning_clock);
92f86a9e 2538 } else {
96a0a69d
PP
2539 msg = bt_message_packet_beginning_create(notit->msg_iter,
2540 notit->packet);
96a0a69d
PP
2541 }
2542
b09a5592
PP
2543 if (!msg) {
2544 BT_LOGE("Cannot create packet beginning message: "
fdf0e7a0
PP
2545 "notit-addr=%p, packet-addr=%p",
2546 notit, notit->packet);
8fc063a2 2547 goto end;
78586d8a 2548 }
a6918753 2549
b09a5592 2550 *message = msg;
8fc063a2
PP
2551
2552end:
2553 return;
e98a2d6e
PP
2554}
2555
78586d8a 2556static
bbbd35bf 2557void create_msg_packet_end(struct bt_msg_iter *notit, bt_message **message)
e98a2d6e 2558{
b09a5592 2559 bt_message *msg;
e98a2d6e 2560
e98a2d6e 2561 if (!notit->packet) {
78586d8a 2562 return;
e98a2d6e
PP
2563 }
2564
7b33a0e0
PP
2565 /* Update default clock from packet's end time */
2566 if (notit->snapshots.end_clock != UINT64_C(-1)) {
ecbb78c0 2567 notit->default_clock_snapshot = notit->snapshots.end_clock;
7b33a0e0
PP
2568 }
2569
b09a5592 2570 BT_ASSERT(notit->msg_iter);
96a0a69d 2571
32476570 2572 if (notit->meta.sc->packets_have_ts_end) {
92f86a9e 2573 BT_ASSERT(notit->snapshots.end_clock != UINT64_C(-1));
96a0a69d
PP
2574 msg = bt_message_packet_end_create_with_default_clock_snapshot(
2575 notit->msg_iter, notit->packet,
2576 notit->snapshots.end_clock);
92f86a9e
PP
2577 } else {
2578 msg = bt_message_packet_end_create(notit->msg_iter,
2579 notit->packet);
96a0a69d
PP
2580 }
2581
b09a5592
PP
2582 if (!msg) {
2583 BT_LOGE("Cannot create packet end message: "
fdf0e7a0
PP
2584 "notit-addr=%p, packet-addr=%p",
2585 notit, notit->packet);
78586d8a 2586 return;
e5815ba2 2587
78586d8a 2588 }
e98a2d6e 2589
8c6884d9 2590 BT_PACKET_PUT_REF_AND_RESET(notit->packet);
b09a5592 2591 *message = msg;
e98a2d6e
PP
2592}
2593
989925d1
PP
2594static
2595void create_msg_discarded_events(struct bt_msg_iter *notit,
2596 bt_message **message)
2597{
2598 bt_message *msg;
2599 uint64_t beginning_raw_value = UINT64_C(-1);
2600 uint64_t end_raw_value = UINT64_C(-1);
989925d1
PP
2601
2602 BT_ASSERT(notit->msg_iter);
2603 BT_ASSERT(notit->stream);
32476570 2604 BT_ASSERT(notit->meta.sc->has_discarded_events);
989925d1 2605
32476570
PP
2606 if (notit->meta.sc->discarded_events_have_default_cs) {
2607 if (notit->prev_packet_snapshots.discarded_events == UINT64_C(-1)) {
2608 /*
2609 * We discarded events, but before (and possibly
2610 * including) the current packet: use this packet's time
2611 * range, and do not have a specific count.
2612 */
2613 beginning_raw_value = notit->snapshots.beginning_clock;
2614 end_raw_value = notit->snapshots.end_clock;
2615 } else {
2616 beginning_raw_value = notit->prev_packet_snapshots.end_clock;
2617 end_raw_value = notit->snapshots.end_clock;
2618 }
989925d1 2619
32476570
PP
2620 BT_ASSERT(beginning_raw_value != UINT64_C(-1));
2621 BT_ASSERT(end_raw_value != UINT64_C(-1));
989925d1
PP
2622 msg = bt_message_discarded_events_create_with_default_clock_snapshots(
2623 notit->msg_iter, notit->stream, beginning_raw_value,
2624 end_raw_value);
2625 } else {
2626 msg = bt_message_discarded_events_create(notit->msg_iter,
2627 notit->stream);
2628 }
2629
2630 if (!msg) {
2631 BT_LOGE("Cannot create discarded events message: "
2632 "notit-addr=%p, stream-addr=%p",
2633 notit, notit->stream);
2634 return;
2635 }
2636
32476570
PP
2637 if (notit->prev_packet_snapshots.discarded_events != UINT64_C(-1)) {
2638 bt_message_discarded_events_set_count(msg,
2639 notit->snapshots.discarded_events -
2640 notit->prev_packet_snapshots.discarded_events);
989925d1
PP
2641 }
2642
2643 *message = msg;
2644}
2645
2646static
2647void create_msg_discarded_packets(struct bt_msg_iter *notit,
2648 bt_message **message)
2649{
2650 bt_message *msg;
2651
2652 BT_ASSERT(notit->msg_iter);
2653 BT_ASSERT(notit->stream);
32476570 2654 BT_ASSERT(notit->meta.sc->has_discarded_packets);
989925d1
PP
2655 BT_ASSERT(notit->prev_packet_snapshots.packets !=
2656 UINT64_C(-1));
2657
32476570
PP
2658 if (notit->meta.sc->discarded_packets_have_default_cs) {
2659 BT_ASSERT(notit->prev_packet_snapshots.end_clock != UINT64_C(-1));
2660 BT_ASSERT(notit->snapshots.beginning_clock != UINT64_C(-1));
989925d1
PP
2661 msg = bt_message_discarded_packets_create_with_default_clock_snapshots(
2662 notit->msg_iter, notit->stream,
2663 notit->prev_packet_snapshots.end_clock,
2664 notit->snapshots.beginning_clock);
2665 } else {
2666 msg = bt_message_discarded_packets_create(notit->msg_iter,
2667 notit->stream);
2668 }
2669
2670 if (!msg) {
2671 BT_LOGE("Cannot create discarded packets message: "
2672 "notit-addr=%p, stream-addr=%p",
2673 notit, notit->stream);
2674 return;
2675 }
2676
2677 bt_message_discarded_packets_set_count(msg,
2678 notit->snapshots.packets -
2679 notit->prev_packet_snapshots.packets - 1);
2680 *message = msg;
2681}
2682
c44c3e70 2683BT_HIDDEN
b09a5592 2684struct bt_msg_iter *bt_msg_iter_create(struct ctf_trace_class *tc,
e98a2d6e 2685 size_t max_request_sz,
b09a5592 2686 struct bt_msg_iter_medium_ops medops, void *data)
e98a2d6e 2687{
b09a5592 2688 struct bt_msg_iter *notit = NULL;
939190b3
PP
2689 struct bt_bfcr_cbs cbs = {
2690 .classes = {
2691 .signed_int = bfcr_signed_int_cb,
2692 .unsigned_int = bfcr_unsigned_int_cb,
2693 .floating_point = bfcr_floating_point_cb,
2694 .string_begin = bfcr_string_begin_cb,
2695 .string = bfcr_string_cb,
2696 .string_end = bfcr_string_end_cb,
2697 .compound_begin = bfcr_compound_begin_cb,
2698 .compound_end = bfcr_compound_end_cb,
e98a2d6e
PP
2699 },
2700 .query = {
939190b3
PP
2701 .get_sequence_length = bfcr_get_sequence_length_cb,
2702 .borrow_variant_selected_field_class = bfcr_borrow_variant_selected_field_class_cb,
e98a2d6e
PP
2703 },
2704 };
2705
7b33a0e0 2706 BT_ASSERT(tc);
8b45963b 2707 BT_ASSERT(medops.request_bytes);
a6918753 2708 BT_ASSERT(medops.borrow_stream);
b09a5592 2709 BT_LOGD("Creating CTF plugin message iterator: "
10b7a2e4
PP
2710 "trace-addr=%p, max-request-size=%zu, "
2711 "data=%p", tc, max_request_sz, data);
b09a5592 2712 notit = g_new0(struct bt_msg_iter, 1);
e98a2d6e 2713 if (!notit) {
b09a5592 2714 BT_LOGE_STR("Failed to allocate one CTF plugin message iterator.");
e98a2d6e
PP
2715 goto end;
2716 }
7b33a0e0 2717 notit->meta.tc = tc;
e98a2d6e
PP
2718 notit->medium.medops = medops;
2719 notit->medium.max_request_sz = max_request_sz;
2720 notit->medium.data = data;
e98a2d6e 2721 notit->stack = stack_new(notit);
7b33a0e0
PP
2722 notit->stored_values = g_array_new(FALSE, TRUE, sizeof(uint64_t));
2723 g_array_set_size(notit->stored_values, tc->stored_value_count);
2724
e98a2d6e 2725 if (!notit->stack) {
fdf0e7a0 2726 BT_LOGE_STR("Failed to create field stack.");
c44c3e70 2727 goto error;
e98a2d6e
PP
2728 }
2729
939190b3
PP
2730 notit->bfcr = bt_bfcr_create(cbs, notit);
2731 if (!notit->bfcr) {
2732 BT_LOGE_STR("Failed to create binary class reader (BFCR).");
c44c3e70 2733 goto error;
e98a2d6e
PP
2734 }
2735
b09a5592
PP
2736 bt_msg_iter_reset(notit);
2737 BT_LOGD("Created CTF plugin message iterator: "
10b7a2e4 2738 "trace-addr=%p, max-request-size=%zu, "
fdf0e7a0 2739 "data=%p, notit-addr=%p",
10b7a2e4 2740 tc, max_request_sz, data, notit);
bb230c9b 2741 notit->cur_packet_offset = 0;
fdf0e7a0 2742
e98a2d6e
PP
2743end:
2744 return notit;
fdf0e7a0 2745
c44c3e70 2746error:
b09a5592 2747 bt_msg_iter_destroy(notit);
c44c3e70
JG
2748 notit = NULL;
2749 goto end;
e98a2d6e
PP
2750}
2751
b09a5592 2752void bt_msg_iter_destroy(struct bt_msg_iter *notit)
e98a2d6e 2753{
8c6884d9
PP
2754 BT_PACKET_PUT_REF_AND_RESET(notit->packet);
2755 BT_STREAM_PUT_REF_AND_RESET(notit->stream);
a6918753 2756 release_all_dscopes(notit);
e98a2d6e 2757
b09a5592 2758 BT_LOGD("Destroying CTF plugin message iterator: addr=%p", notit);
fdf0e7a0 2759
e98a2d6e 2760 if (notit->stack) {
fdf0e7a0 2761 BT_LOGD_STR("Destroying field stack.");
e98a2d6e
PP
2762 stack_destroy(notit->stack);
2763 }
2764
939190b3
PP
2765 if (notit->bfcr) {
2766 BT_LOGD("Destroying BFCR: bfcr-addr=%p", notit->bfcr);
2767 bt_bfcr_destroy(notit->bfcr);
e98a2d6e
PP
2768 }
2769
7b33a0e0
PP
2770 if (notit->stored_values) {
2771 g_array_free(notit->stored_values, TRUE);
5f870343 2772 }
fdf0e7a0 2773
e98a2d6e
PP
2774 g_free(notit);
2775}
2776
b09a5592
PP
2777enum bt_msg_iter_status bt_msg_iter_get_next_message(
2778 struct bt_msg_iter *notit,
bbbd35bf 2779 bt_self_message_iterator *msg_iter, bt_message **message)
e98a2d6e 2780{
b09a5592 2781 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
e98a2d6e 2782
8b45963b 2783 BT_ASSERT(notit);
b09a5592 2784 BT_ASSERT(message);
b09a5592 2785 notit->msg_iter = msg_iter;
989925d1 2786 notit->set_stream = true;
b09a5592 2787 BT_LOGV("Getting next message: notit-addr=%p", notit);
fdf0e7a0 2788
e98a2d6e
PP
2789 while (true) {
2790 status = handle_state(notit);
85e7137b 2791 if (G_UNLIKELY(status == BT_MSG_ITER_STATUS_AGAIN)) {
b09a5592 2792 BT_LOGV_STR("Medium returned BT_MSG_ITER_STATUS_AGAIN.");
7cdc2bab 2793 goto end;
85e7137b 2794 } else if (G_UNLIKELY(status != BT_MSG_ITER_STATUS_OK)) {
bbbd35bf
PP
2795 BT_LOGW("Cannot handle state: notit-addr=%p, state=%s",
2796 notit, state_string(notit->state));
e98a2d6e
PP
2797 goto end;
2798 }
2799
2800 switch (notit->state) {
bbbd35bf
PP
2801 case STATE_EMIT_MSG_EVENT:
2802 BT_ASSERT(notit->event_msg);
bbbd35bf
PP
2803 *message = notit->event_msg;
2804 notit->event_msg = NULL;
989925d1
PP
2805 goto end;
2806 case STATE_EMIT_MSG_DISCARDED_EVENTS:
2807 /* create_msg_discared_events() logs errors */
2808 create_msg_discarded_events(notit, message);
2809
2810 if (!*message) {
2811 status = BT_MSG_ITER_STATUS_ERROR;
2812 }
2813
2814 goto end;
2815 case STATE_EMIT_MSG_DISCARDED_PACKETS:
2816 /* create_msg_discared_packets() logs errors */
2817 create_msg_discarded_packets(notit, message);
2818
2819 if (!*message) {
2820 status = BT_MSG_ITER_STATUS_ERROR;
2821 }
2822
bbbd35bf
PP
2823 goto end;
2824 case STATE_EMIT_MSG_PACKET_BEGINNING:
2825 /* create_msg_packet_beginning() logs errors */
2826 create_msg_packet_beginning(notit, message);
7b33a0e0 2827
b09a5592
PP
2828 if (!*message) {
2829 status = BT_MSG_ITER_STATUS_ERROR;
6ff151ad 2830 }
7b33a0e0 2831
6ff151ad 2832 goto end;
bbbd35bf
PP
2833 case STATE_EMIT_MSG_PACKET_END_SINGLE:
2834 case STATE_EMIT_MSG_PACKET_END_MULTI:
2835 /* create_msg_packet_end() logs errors */
2836 create_msg_packet_end(notit, message);
7b33a0e0 2837
b09a5592
PP
2838 if (!*message) {
2839 status = BT_MSG_ITER_STATUS_ERROR;
e98a2d6e 2840 }
a6918753 2841
e98a2d6e 2842 goto end;
bbbd35bf 2843 case STATE_EMIT_MSG_STREAM_ACTIVITY_BEGINNING:
989925d1
PP
2844 /* create_msg_stream_activity_beginning() logs errors */
2845 create_msg_stream_activity_beginning(notit, message);
bbbd35bf 2846
989925d1
PP
2847 if (!*message) {
2848 status = BT_MSG_ITER_STATUS_ERROR;
e98a2d6e 2849 }
a6918753 2850
989925d1 2851 goto end;
bbbd35bf 2852 case STATE_EMIT_MSG_STREAM_ACTIVITY_END:
989925d1
PP
2853 /* create_msg_stream_activity_end() logs errors */
2854 create_msg_stream_activity_end(notit, message);
bbbd35bf 2855
989925d1
PP
2856 if (!*message) {
2857 status = BT_MSG_ITER_STATUS_ERROR;
bbbd35bf
PP
2858 }
2859
989925d1 2860 goto end;
bbbd35bf 2861 case STATE_EMIT_MSG_STREAM_BEGINNING:
989925d1
PP
2862 /* create_msg_stream_beginning() logs errors */
2863 create_msg_stream_beginning(notit, message);
bbbd35bf 2864
989925d1
PP
2865 if (!*message) {
2866 status = BT_MSG_ITER_STATUS_ERROR;
bbbd35bf
PP
2867 }
2868
989925d1 2869 goto end;
bbbd35bf 2870 case STATE_EMIT_MSG_STREAM_END:
989925d1
PP
2871 /* create_msg_stream_end() logs errors */
2872 create_msg_stream_end(notit, message);
bbbd35bf 2873
989925d1
PP
2874 if (!*message) {
2875 status = BT_MSG_ITER_STATUS_ERROR;
bbbd35bf
PP
2876 }
2877
989925d1 2878 goto end;
bbbd35bf
PP
2879 case STATE_DONE:
2880 status = BT_MSG_ITER_STATUS_EOF;
e98a2d6e
PP
2881 goto end;
2882 default:
2883 /* Non-emitting state: continue */
2884 break;
2885 }
2886 }
2887
2888end:
2889 return status;
2890}
87187cbf 2891
e5e71bf8
PP
2892static
2893enum bt_msg_iter_status read_packet_header_context_fields(
2894 struct bt_msg_iter *notit)
87187cbf 2895{
bb230c9b 2896 int ret;
b09a5592 2897 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
87187cbf 2898
8b45963b 2899 BT_ASSERT(notit);
989925d1 2900 notit->set_stream = false;
87187cbf 2901
bbbd35bf 2902 if (notit->state == STATE_EMIT_MSG_PACKET_BEGINNING) {
87187cbf 2903 /* We're already there */
e5e71bf8 2904 goto end;
87187cbf
PP
2905 }
2906
2907 while (true) {
2908 status = handle_state(notit);
85e7137b 2909 if (G_UNLIKELY(status == BT_MSG_ITER_STATUS_AGAIN)) {
b09a5592 2910 BT_LOGV_STR("Medium returned BT_MSG_ITER_STATUS_AGAIN.");
87187cbf 2911 goto end;
85e7137b 2912 } else if (G_UNLIKELY(status != BT_MSG_ITER_STATUS_OK)) {
bbbd35bf
PP
2913 BT_LOGW("Cannot handle state: notit-addr=%p, state=%s",
2914 notit, state_string(notit->state));
87187cbf
PP
2915 goto end;
2916 }
2917
2918 switch (notit->state) {
bbbd35bf 2919 case STATE_EMIT_MSG_PACKET_BEGINNING:
87187cbf
PP
2920 /*
2921 * Packet header and context fields are
2922 * potentially decoded (or they don't exist).
2923 */
e5e71bf8 2924 goto end;
87187cbf
PP
2925 case STATE_INIT:
2926 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
2927 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
2928 case STATE_AFTER_TRACE_PACKET_HEADER:
2929 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN:
2930 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE:
2931 case STATE_AFTER_STREAM_PACKET_CONTEXT:
989925d1 2932 case STATE_CHECK_EMIT_MSG_STREAM_BEGINNING:
bbbd35bf
PP
2933 case STATE_EMIT_MSG_STREAM_BEGINNING:
2934 case STATE_EMIT_MSG_STREAM_ACTIVITY_BEGINNING:
989925d1
PP
2935 case STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS:
2936 case STATE_EMIT_MSG_DISCARDED_EVENTS:
2937 case STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS:
2938 case STATE_EMIT_MSG_DISCARDED_PACKETS:
87187cbf
PP
2939 /* Non-emitting state: continue */
2940 break;
2941 default:
2942 /*
2943 * We should never get past the
bbbd35bf 2944 * STATE_EMIT_MSG_PACKET_BEGINNING state.
87187cbf 2945 */
fdf0e7a0
PP
2946 BT_LOGF("Unexpected state: notit-addr=%p, state=%s",
2947 notit, state_string(notit->state));
0fbb9a9f 2948 abort();
87187cbf
PP
2949 }
2950 }
2951
e5e71bf8 2952end:
a6918753
PP
2953 ret = set_current_packet_content_sizes(notit);
2954 if (ret) {
b09a5592 2955 status = BT_MSG_ITER_STATUS_ERROR;
a6918753
PP
2956 }
2957
87187cbf
PP
2958 return status;
2959}
55fa6491
PP
2960
2961BT_HIDDEN
b09a5592 2962void bt_msg_iter_set_medops_data(struct bt_msg_iter *notit,
55fa6491
PP
2963 void *medops_data)
2964{
8b45963b 2965 BT_ASSERT(notit);
55fa6491
PP
2966 notit->medium.data = medops_data;
2967}
bb230c9b
JG
2968
2969BT_HIDDEN
bbbd35bf
PP
2970enum bt_msg_iter_status bt_msg_iter_seek(struct bt_msg_iter *notit,
2971 off_t offset)
bb230c9b 2972{
b09a5592
PP
2973 enum bt_msg_iter_status ret = BT_MSG_ITER_STATUS_OK;
2974 enum bt_msg_iter_medium_status medium_status;
bb230c9b 2975
8b45963b 2976 BT_ASSERT(notit);
bb230c9b
JG
2977 if (offset < 0) {
2978 BT_LOGE("Cannot seek to negative offset: offset=%jd", offset);
b09a5592 2979 ret = BT_MSG_ITER_STATUS_INVAL;
bb230c9b
JG
2980 goto end;
2981 }
2982
2983 if (!notit->medium.medops.seek) {
b09a5592 2984 ret = BT_MSG_ITER_STATUS_UNSUPPORTED;
bb230c9b
JG
2985 BT_LOGD("Aborting seek as the iterator's underlying media does not implement seek support.");
2986 goto end;
2987 }
2988
2989 medium_status = notit->medium.medops.seek(
b09a5592
PP
2990 BT_MSG_ITER_SEEK_WHENCE_SET, offset, notit->medium.data);
2991 if (medium_status != BT_MSG_ITER_MEDIUM_STATUS_OK) {
2992 if (medium_status == BT_MSG_ITER_MEDIUM_STATUS_EOF) {
2993 ret = BT_MSG_ITER_STATUS_EOF;
bb230c9b 2994 } else {
b09a5592 2995 ret = BT_MSG_ITER_STATUS_ERROR;
bb230c9b
JG
2996 goto end;
2997 }
2998 }
2999
b09a5592 3000 bt_msg_iter_reset(notit);
bb230c9b 3001 notit->cur_packet_offset = offset;
7b33a0e0 3002
bb230c9b
JG
3003end:
3004 return ret;
3005}
3006
7b33a0e0 3007BT_HIDDEN
b09a5592
PP
3008enum bt_msg_iter_status bt_msg_iter_get_packet_properties(
3009 struct bt_msg_iter *notit,
3010 struct bt_msg_iter_packet_properties *props)
7b33a0e0 3011{
e5e71bf8
PP
3012 enum bt_msg_iter_status status;
3013
7b33a0e0
PP
3014 BT_ASSERT(notit);
3015 BT_ASSERT(props);
e5e71bf8
PP
3016 status = read_packet_header_context_fields(notit);
3017 if (status != BT_MSG_ITER_STATUS_OK) {
3018 goto end;
3019 }
7b33a0e0 3020
bbbd35bf
PP
3021 props->exp_packet_total_size = notit->cur_exp_packet_total_size;
3022 props->exp_packet_content_size = notit->cur_exp_packet_content_size;
7b33a0e0
PP
3023 props->stream_class_id = (uint64_t) notit->cur_stream_class_id;
3024 props->data_stream_id = notit->cur_data_stream_id;
3025 props->snapshots.discarded_events = notit->snapshots.discarded_events;
3026 props->snapshots.packets = notit->snapshots.packets;
3027 props->snapshots.beginning_clock = notit->snapshots.beginning_clock;
3028 props->snapshots.end_clock = notit->snapshots.end_clock;
e5e71bf8
PP
3029
3030end:
3031 return status;
bb230c9b 3032}
bbbd35bf
PP
3033
3034BT_HIDDEN
3035void bt_msg_iter_set_emit_stream_beginning_message(struct bt_msg_iter *notit,
3036 bool val)
3037{
3038 notit->emit_stream_begin_msg = val;
3039}
3040
3041BT_HIDDEN
3042void bt_msg_iter_set_emit_stream_end_message(struct bt_msg_iter *notit,
3043 bool val)
3044{
3045 notit->emit_stream_end_msg = val;
3046}
This page took 0.233448 seconds and 4 git commands to generate.