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