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