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