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