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