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