lib: update copyrights
[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 */
40f4ba76 59 struct bt_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 */
40f4ba76 114 struct bt_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) */
40f4ba76 130 struct bt_packet_header_field *packet_header_field;
312c056a
PP
131
132 /* Current packet header field wrapper (NULL if not created yet) */
40f4ba76 133 struct bt_packet_context_field *packet_context_field;
312c056a
PP
134
135 /* Current event header field (NULL if not created yet) */
40f4ba76 136 struct bt_event_header_field *event_header_field;
312c056a 137
e98a2d6e 138 /* Current packet (NULL if not created yet) */
40f4ba76 139 struct bt_packet *packet;
e98a2d6e 140
af87daef 141 /* Current stream (NULL if not set yet) */
40f4ba76 142 struct bt_stream *stream;
af87daef 143
312c056a 144 /* Current event (NULL if not created yet) */
40f4ba76 145 struct bt_event *event;
312c056a
PP
146
147 /* Current event notification (NULL if not created yet) */
0d72b8c3 148 struct bt_notification *event_notif;
312c056a 149
44c440bc 150 /* Database of current dynamic scopes */
e98a2d6e 151 struct {
40f4ba76
PP
152 struct bt_field *trace_packet_header;
153 struct bt_field *stream_packet_context;
154 struct bt_field *event_header;
155 struct bt_field *event_common_context;
156 struct bt_field *event_spec_context;
157 struct bt_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
40f4ba76 328void stack_push(struct stack *stack, struct bt_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,
40f4ba76 523 struct bt_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) {
40f4ba76 622 bt_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) {
40f4ba76 637 bt_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) {
40f4ba76 644 bt_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
862ca4ed 664 /* Packet header class is common to the whole trace class. */
5cd6d0e5
PP
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 674 /*
862ca4ed
PP
675 * Create free packet header field from trace class.
676 * This field is going to be moved to the packet once we
44c440bc
PP
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 684 notit->packet_header_field =
40f4ba76 685 bt_packet_header_field_create(
e5be10ef 686 notit->meta.tc->ir_tc);
44c440bc 687 if (!notit->packet_header_field) {
862ca4ed 688 BT_LOGE_STR("Cannot create packet header field wrapper from trace class.");
44c440bc
PP
689 ret = BT_NOTIF_ITER_STATUS_ERROR;
690 goto end;
691 }
692
693 notit->dscopes.trace_packet_header =
40f4ba76 694 bt_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:"
862ca4ed
PP
703 "notit-addr=%p, trace-class-addr=%p, fc-addr=%p",
704 notit, notit->meta.tc, packet_header_fc);
5cd6d0e5 705 ret = read_dscope_begin_state(notit, packet_header_fc,
fdf0e7a0
PP
706 STATE_AFTER_TRACE_PACKET_HEADER,
707 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE,
312c056a 708 notit->dscopes.trace_packet_header);
fdf0e7a0
PP
709 if (ret < 0) {
710 BT_LOGW("Cannot decode packet header field: "
862ca4ed
PP
711 "notit-addr=%p, trace-class-addr=%p, "
712 "fc-addr=%p",
713 notit, notit->meta.tc, packet_header_fc);
fdf0e7a0 714 }
d1e46835 715
e98a2d6e 716end:
2cf1d51e 717 return ret;
e98a2d6e
PP
718}
719
720static
50842bdc
PP
721enum bt_notif_iter_status read_packet_header_continue_state(
722 struct bt_notif_iter *notit)
e98a2d6e
PP
723{
724 return read_dscope_continue_state(notit,
44c440bc 725 STATE_AFTER_TRACE_PACKET_HEADER);
5f870343
JG
726}
727
e98a2d6e 728static inline
44c440bc 729enum bt_notif_iter_status set_current_stream_class(struct bt_notif_iter *notit)
e98a2d6e 730{
50842bdc 731 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
44c440bc
PP
732 struct ctf_stream_class *new_stream_class = NULL;
733
734 if (notit->cur_stream_class_id == -1) {
fdf0e7a0 735 /*
44c440bc
PP
736 * No current stream class ID field, therefore only one
737 * stream class.
fdf0e7a0 738 */
44c440bc
PP
739 if (notit->meta.tc->stream_classes->len != 1) {
740 BT_LOGW("Need exactly one stream class since there's "
741 "no stream class ID field: "
862ca4ed 742 "notit-addr=%p", notit);
44c440bc
PP
743 status = BT_NOTIF_ITER_STATUS_ERROR;
744 goto end;
745 }
e98a2d6e 746
44c440bc
PP
747 new_stream_class = notit->meta.tc->stream_classes->pdata[0];
748 notit->cur_stream_class_id = new_stream_class->id;
e98a2d6e
PP
749 }
750
44c440bc
PP
751 new_stream_class = ctf_trace_class_borrow_stream_class_by_id(
752 notit->meta.tc, notit->cur_stream_class_id);
115de887 753 if (!new_stream_class) {
862ca4ed 754 BT_LOGW("No stream class with ID of stream class ID to use in trace class: "
fdf0e7a0 755 "notit-addr=%p, stream-class-id=%" PRIu64 ", "
862ca4ed
PP
756 "trace-class-addr=%p",
757 notit, notit->cur_stream_class_id, notit->meta.tc);
50842bdc 758 status = BT_NOTIF_ITER_STATUS_ERROR;
e98a2d6e
PP
759 goto end;
760 }
761
44c440bc
PP
762 if (notit->meta.sc) {
763 if (new_stream_class != notit->meta.sc) {
115de887
PP
764 BT_LOGW("Two packets refer to two different stream classes within the same packet sequence: "
765 "notit-addr=%p, prev-stream-class-addr=%p, "
115de887
PP
766 "prev-stream-class-id=%" PRId64 ", "
767 "next-stream-class-addr=%p, "
115de887 768 "next-stream-class-id=%" PRId64 ", "
862ca4ed 769 "trace-addr=%p",
44c440bc
PP
770 notit, notit->meta.sc,
771 notit->meta.sc->id,
115de887 772 new_stream_class,
44c440bc 773 new_stream_class->id,
862ca4ed 774 notit->meta.tc);
50842bdc 775 status = BT_NOTIF_ITER_STATUS_ERROR;
115de887
PP
776 goto end;
777 }
778 } else {
44c440bc 779 notit->meta.sc = new_stream_class;
115de887
PP
780 }
781
fdf0e7a0
PP
782 BT_LOGV("Set current stream class: "
783 "notit-addr=%p, stream-class-addr=%p, "
44c440bc
PP
784 "stream-class-id=%" PRId64,
785 notit, notit->meta.sc, notit->meta.sc->id);
d1e46835 786
e98a2d6e 787end:
e98a2d6e
PP
788 return status;
789}
790
312c056a
PP
791static inline
792enum bt_notif_iter_status set_current_stream(struct bt_notif_iter *notit)
793{
794 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
40f4ba76 795 struct bt_stream *stream = NULL;
312c056a
PP
796
797 BT_LOGV("Calling user function (get stream): notit-addr=%p, "
44c440bc
PP
798 "stream-class-addr=%p, stream-class-id=%" PRId64,
799 notit, notit->meta.sc,
800 notit->meta.sc->id);
398454ed 801 stream = notit->medium.medops.borrow_stream(
44c440bc 802 notit->meta.sc->ir_sc, notit->cur_data_stream_id,
398454ed
PP
803 notit->medium.data);
804 bt_object_get_ref(stream);
312c056a
PP
805 BT_LOGV("User function returned: stream-addr=%p", stream);
806 if (!stream) {
44c440bc
PP
807 BT_LOGW_STR("User function failed to return a stream object "
808 "for the given stream class.");
312c056a
PP
809 status = BT_NOTIF_ITER_STATUS_ERROR;
810 goto end;
811 }
812
813 if (notit->stream && stream != notit->stream) {
44c440bc
PP
814 BT_LOGW("User function returned a different stream than the "
815 "previous one for the same sequence of packets.");
312c056a
PP
816 status = BT_NOTIF_ITER_STATUS_ERROR;
817 goto end;
818 }
819
65300d60 820 BT_OBJECT_MOVE_REF(notit->stream, stream);
312c056a
PP
821
822end:
65300d60 823 bt_object_put_ref(stream);
312c056a
PP
824 return status;
825}
826
827static inline
828enum bt_notif_iter_status set_current_packet(struct bt_notif_iter *notit)
829{
830 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
40f4ba76 831 struct bt_packet *packet = NULL;
312c056a
PP
832
833 BT_LOGV("Creating packet for packet notification: "
834 "notit-addr=%p", notit);
835 BT_LOGV("Creating packet from stream: "
836 "notit-addr=%p, stream-addr=%p, "
837 "stream-class-addr=%p, "
312c056a 838 "stream-class-id=%" PRId64,
44c440bc
PP
839 notit, notit->stream, notit->meta.sc,
840 notit->meta.sc->id);
312c056a
PP
841
842 /* Create packet */
843 BT_ASSERT(notit->stream);
40f4ba76 844 packet = bt_packet_create(notit->stream);
312c056a
PP
845 if (!packet) {
846 BT_LOGE("Cannot create packet from stream: "
847 "notit-addr=%p, stream-addr=%p, "
848 "stream-class-addr=%p, "
312c056a 849 "stream-class-id=%" PRId64,
44c440bc
PP
850 notit, notit->stream, notit->meta.sc,
851 notit->meta.sc->id);
312c056a
PP
852 goto error;
853 }
854
855 goto end;
856
857error:
65300d60 858 BT_OBJECT_PUT_REF_AND_RESET(packet);
312c056a
PP
859 status = BT_NOTIF_ITER_STATUS_ERROR;
860
861end:
65300d60 862 BT_OBJECT_MOVE_REF(notit->packet, packet);
312c056a
PP
863 return status;
864}
865
e98a2d6e 866static
50842bdc
PP
867enum bt_notif_iter_status after_packet_header_state(
868 struct bt_notif_iter *notit)
e98a2d6e 869{
50842bdc 870 enum bt_notif_iter_status status;
e98a2d6e
PP
871
872 status = set_current_stream_class(notit);
312c056a
PP
873 if (status != BT_NOTIF_ITER_STATUS_OK) {
874 goto end;
e98a2d6e
PP
875 }
876
312c056a
PP
877 notit->state = STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN;
878
879end:
e98a2d6e
PP
880 return status;
881}
882
883static
50842bdc
PP
884enum bt_notif_iter_status read_packet_context_begin_state(
885 struct bt_notif_iter *notit)
e98a2d6e 886{
50842bdc 887 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
5cd6d0e5 888 struct ctf_field_class *packet_context_fc;
e98a2d6e 889
44c440bc 890 BT_ASSERT(notit->meta.sc);
5cd6d0e5
PP
891 packet_context_fc = notit->meta.sc->packet_context_fc;
892 if (!packet_context_fc) {
893 BT_LOGV("No packet packet context field class in stream class: continuing: "
fdf0e7a0 894 "notit-addr=%p, stream-class-addr=%p, "
44c440bc
PP
895 "stream-class-id=%" PRId64,
896 notit, notit->meta.sc,
897 notit->meta.sc->id);
835b2d10 898 notit->state = STATE_AFTER_STREAM_PACKET_CONTEXT;
e98a2d6e
PP
899 goto end;
900 }
901
312c056a 902 BT_ASSERT(!notit->packet_context_field);
44c440bc 903
5cd6d0e5 904 if (packet_context_fc->in_ir) {
44c440bc
PP
905 /*
906 * Create free packet context field from stream class.
907 * This field is going to be moved to the packet once we
908 * create it. We cannot create the packet now because a
909 * packet is created from a stream, and this API must be
910 * able to return the packet header and context fields
911 * without creating a stream
912 * (bt_notif_iter_borrow_packet_header_context_fields()).
913 */
914 notit->packet_context_field =
40f4ba76 915 bt_packet_context_field_create(
e5be10ef 916 notit->meta.sc->ir_sc);
44c440bc
PP
917 if (!notit->packet_context_field) {
918 BT_LOGE_STR("Cannot create packet context field wrapper from stream class.");
919 status = BT_NOTIF_ITER_STATUS_ERROR;
920 goto end;
921 }
922
923 notit->dscopes.stream_packet_context =
40f4ba76 924 bt_packet_context_field_borrow_field(
e5be10ef 925 notit->packet_context_field);
44c440bc 926 BT_ASSERT(notit->dscopes.stream_packet_context);
312c056a
PP
927 }
928
fdf0e7a0
PP
929 BT_LOGV("Decoding packet context field: "
930 "notit-addr=%p, stream-class-addr=%p, "
5cd6d0e5 931 "stream-class-id=%" PRId64 ", fc-addr=%p",
44c440bc 932 notit, notit->meta.sc,
5cd6d0e5
PP
933 notit->meta.sc->id, packet_context_fc);
934 status = read_dscope_begin_state(notit, packet_context_fc,
fdf0e7a0
PP
935 STATE_AFTER_STREAM_PACKET_CONTEXT,
936 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE,
312c056a 937 notit->dscopes.stream_packet_context);
fdf0e7a0
PP
938 if (status < 0) {
939 BT_LOGW("Cannot decode packet context field: "
940 "notit-addr=%p, stream-class-addr=%p, "
5cd6d0e5 941 "stream-class-id=%" PRId64 ", fc-addr=%p",
44c440bc
PP
942 notit, notit->meta.sc,
943 notit->meta.sc->id,
5cd6d0e5 944 packet_context_fc);
fdf0e7a0 945 }
e98a2d6e
PP
946
947end:
e98a2d6e
PP
948 return status;
949}
950
951static
50842bdc
PP
952enum bt_notif_iter_status read_packet_context_continue_state(
953 struct bt_notif_iter *notit)
e98a2d6e
PP
954{
955 return read_dscope_continue_state(notit,
78586d8a 956 STATE_AFTER_STREAM_PACKET_CONTEXT);
e98a2d6e
PP
957}
958
78586d8a 959static
50842bdc
PP
960enum bt_notif_iter_status set_current_packet_content_sizes(
961 struct bt_notif_iter *notit)
e98a2d6e 962{
50842bdc 963 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
e98a2d6e 964
44c440bc
PP
965 if (notit->cur_exp_packet_total_size == -1) {
966 if (notit->cur_exp_packet_content_size != -1) {
967 BT_LOGW("Content size is set, but packet size is not: "
fdf0e7a0 968 "notit-addr=%p, packet-context-field-addr=%p, "
44c440bc 969 "packet-size=%" PRId64 ", content-size=%" PRId64,
fdf0e7a0 970 notit, notit->dscopes.stream_packet_context,
44c440bc
PP
971 notit->cur_exp_packet_total_size,
972 notit->cur_exp_packet_content_size);
50842bdc 973 status = BT_NOTIF_ITER_STATUS_ERROR;
e98a2d6e
PP
974 goto end;
975 }
e98a2d6e 976 } else {
44c440bc
PP
977 if (notit->cur_exp_packet_content_size == -1) {
978 notit->cur_exp_packet_content_size =
979 notit->cur_exp_packet_total_size;
980 }
e98a2d6e
PP
981 }
982
44c440bc
PP
983 if (notit->cur_exp_packet_content_size >
984 notit->cur_exp_packet_total_size) {
985 BT_LOGW("Invalid packet or content size: "
986 "content size is greater than packet size: "
fdf0e7a0 987 "notit-addr=%p, packet-context-field-addr=%p, "
44c440bc 988 "packet-size=%" PRId64 ", content-size=%" PRId64,
fdf0e7a0 989 notit, notit->dscopes.stream_packet_context,
44c440bc
PP
990 notit->cur_exp_packet_total_size,
991 notit->cur_exp_packet_content_size);
50842bdc 992 status = BT_NOTIF_ITER_STATUS_ERROR;
fdf0e7a0
PP
993 goto end;
994 }
995
fdf0e7a0
PP
996 BT_LOGV("Set current packet and content sizes: "
997 "notit-addr=%p, packet-size=%" PRIu64 ", content-size=%" PRIu64,
44c440bc
PP
998 notit, notit->cur_exp_packet_total_size,
999 notit->cur_exp_packet_content_size);
e98a2d6e 1000end:
e98a2d6e
PP
1001 return status;
1002}
1003
1004static
50842bdc
PP
1005enum bt_notif_iter_status after_packet_context_state(
1006 struct bt_notif_iter *notit)
e98a2d6e 1007{
50842bdc 1008 enum bt_notif_iter_status status;
e98a2d6e
PP
1009
1010 status = set_current_packet_content_sizes(notit);
e22b45d0
PP
1011 if (status != BT_NOTIF_ITER_STATUS_OK) {
1012 goto end;
1013 }
1014
e22b45d0
PP
1015 if (notit->stream_begin_emitted) {
1016 notit->state = STATE_EMIT_NOTIF_NEW_PACKET;
1017 } else {
1018 notit->state = STATE_EMIT_NOTIF_NEW_STREAM;
e98a2d6e
PP
1019 }
1020
e22b45d0 1021end:
e98a2d6e
PP
1022 return status;
1023}
1024
1025static
50842bdc
PP
1026enum bt_notif_iter_status read_event_header_begin_state(
1027 struct bt_notif_iter *notit)
e98a2d6e 1028{
50842bdc 1029 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
5cd6d0e5 1030 struct ctf_field_class *event_header_fc = NULL;
e98a2d6e 1031
174e773b
PP
1032 /* Reset the position of the last event header */
1033 notit->buf.last_eh_at = notit->buf.at;
44c440bc 1034 notit->cur_event_class_id = -1;
174e773b 1035
e98a2d6e 1036 /* Check if we have some content left */
44c440bc
PP
1037 if (notit->cur_exp_packet_content_size >= 0) {
1038 if (unlikely(packet_at(notit) ==
1039 notit->cur_exp_packet_content_size)) {
e98a2d6e 1040 /* No more events! */
fdf0e7a0
PP
1041 BT_LOGV("Reached end of packet: notit-addr=%p, "
1042 "cur=%zu", notit, packet_at(notit));
e98a2d6e
PP
1043 notit->state = STATE_EMIT_NOTIF_END_OF_PACKET;
1044 goto end;
44c440bc
PP
1045 } else if (unlikely(packet_at(notit) >
1046 notit->cur_exp_packet_content_size)) {
e98a2d6e 1047 /* That's not supposed to happen */
fdf0e7a0 1048 BT_LOGV("Before decoding event header field: cursor is passed the packet's content: "
1974687e 1049 "notit-addr=%p, content-size=%" PRId64 ", "
44c440bc
PP
1050 "cur=%zu", notit,
1051 notit->cur_exp_packet_content_size,
fdf0e7a0 1052 packet_at(notit));
50842bdc 1053 status = BT_NOTIF_ITER_STATUS_ERROR;
e98a2d6e
PP
1054 goto end;
1055 }
44c440bc
PP
1056 } else {
1057 /*
1058 * "Infinite" content: we're done when the medium has
1059 * nothing else for us.
1060 */
1061 status = buf_ensure_available_bits(notit);
1062 if (status != BT_NOTIF_ITER_STATUS_OK) {
1063 /*
1064 * If this function returns
1065 * `BT_NOTIF_ITER_STATUS_EOF`:
1066 *
1067 * 1. bt_notif_iter_get_next_notification()
1068 * emits a "packet end" notification. This
1069 * resets the current packet. The state
1070 * remains unchanged otherwise.
1071 * 2. This function is called again. It returns
1072 * `BT_NOTIF_ITER_STATUS_EOF` again.
1073 * 3. bt_notif_iter_get_next_notification()
1074 * emits a "stream end" notification because
1075 * there's no current packet. It sets the
1076 * current state to `STATE_DONE`.
1077 */
1078 goto end;
1079 }
e98a2d6e
PP
1080 }
1081
312c056a 1082 release_event_dscopes(notit);
44c440bc 1083 BT_ASSERT(notit->meta.sc);
5cd6d0e5
PP
1084 event_header_fc = notit->meta.sc->event_header_fc;
1085 if (!event_header_fc) {
44c440bc 1086 notit->state = STATE_AFTER_EVENT_HEADER;
e98a2d6e
PP
1087 goto end;
1088 }
1089
5cd6d0e5 1090 if (event_header_fc->in_ir) {
44c440bc 1091 BT_ASSERT(!notit->event_header_field);
e5be10ef 1092 notit->event_header_field =
40f4ba76 1093 bt_event_header_field_create(
e5be10ef 1094 notit->meta.sc->ir_sc);
44c440bc 1095 if (!notit->event_header_field) {
862ca4ed 1096 BT_LOGE_STR("Cannot create event header field wrapper from trace class.");
44c440bc
PP
1097 status = BT_NOTIF_ITER_STATUS_ERROR;
1098 goto end;
1099 }
1100
1101 notit->dscopes.event_header =
40f4ba76 1102 bt_event_header_field_borrow_field(
e5be10ef 1103 notit->event_header_field);
44c440bc 1104 BT_ASSERT(notit->dscopes.event_header);
312c056a
PP
1105 }
1106
fdf0e7a0
PP
1107 BT_LOGV("Decoding event header field: "
1108 "notit-addr=%p, stream-class-addr=%p, "
44c440bc 1109 "stream-class-id=%" PRId64 ", "
5cd6d0e5 1110 "fc-addr=%p",
44c440bc
PP
1111 notit, notit->meta.sc,
1112 notit->meta.sc->id,
5cd6d0e5
PP
1113 event_header_fc);
1114 status = read_dscope_begin_state(notit, event_header_fc,
44c440bc
PP
1115 STATE_AFTER_EVENT_HEADER,
1116 STATE_DSCOPE_EVENT_HEADER_CONTINUE,
1117 notit->dscopes.event_header);
fdf0e7a0
PP
1118 if (status < 0) {
1119 BT_LOGW("Cannot decode event header field: "
1120 "notit-addr=%p, stream-class-addr=%p, "
5cd6d0e5 1121 "stream-class-id=%" PRId64 ", fc-addr=%p",
44c440bc
PP
1122 notit, notit->meta.sc,
1123 notit->meta.sc->id,
5cd6d0e5 1124 event_header_fc);
fdf0e7a0 1125 }
e98a2d6e 1126
d1e46835 1127end:
e98a2d6e
PP
1128 return status;
1129}
1130
1131static
50842bdc
PP
1132enum bt_notif_iter_status read_event_header_continue_state(
1133 struct bt_notif_iter *notit)
e98a2d6e
PP
1134{
1135 return read_dscope_continue_state(notit,
44c440bc 1136 STATE_AFTER_EVENT_HEADER);
e98a2d6e
PP
1137}
1138
1139static inline
50842bdc 1140enum bt_notif_iter_status set_current_event_class(struct bt_notif_iter *notit)
e98a2d6e 1141{
50842bdc 1142 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
e98a2d6e 1143
44c440bc 1144 struct ctf_event_class *new_event_class = NULL;
e98a2d6e 1145
44c440bc 1146 if (notit->cur_event_class_id == -1) {
e98a2d6e 1147 /*
44c440bc
PP
1148 * No current event class ID field, therefore only one
1149 * event class.
e98a2d6e 1150 */
44c440bc
PP
1151 if (notit->meta.sc->event_classes->len != 1) {
1152 BT_LOGW("Need exactly one event class since there's "
1153 "no event class ID field: "
862ca4ed 1154 "notit-addr=%p", notit);
44c440bc
PP
1155 status = BT_NOTIF_ITER_STATUS_ERROR;
1156 goto end;
fdf0e7a0 1157 }
e98a2d6e 1158
44c440bc
PP
1159 new_event_class = notit->meta.sc->event_classes->pdata[0];
1160 notit->cur_event_class_id = new_event_class->id;
e98a2d6e
PP
1161 }
1162
44c440bc
PP
1163 new_event_class = ctf_stream_class_borrow_event_class_by_id(
1164 notit->meta.sc, notit->cur_event_class_id);
1165 if (!new_event_class) {
fdf0e7a0 1166 BT_LOGW("No event class with ID of event class ID to use in stream class: "
44c440bc
PP
1167 "notit-addr=%p, stream-class-id=%" PRIu64 ", "
1168 "event-class-id=%" PRIu64 ", "
862ca4ed 1169 "trace-class-addr=%p",
44c440bc 1170 notit, notit->meta.sc->id, notit->cur_event_class_id,
862ca4ed 1171 notit->meta.tc);
50842bdc 1172 status = BT_NOTIF_ITER_STATUS_ERROR;
e98a2d6e
PP
1173 goto end;
1174 }
1175
44c440bc 1176 notit->meta.ec = new_event_class;
fdf0e7a0
PP
1177 BT_LOGV("Set current event class: "
1178 "notit-addr=%p, event-class-addr=%p, "
44c440bc
PP
1179 "event-class-id=%" PRId64 ", "
1180 "event-class-name=\"%s\"",
1181 notit, notit->meta.ec, notit->meta.ec->id,
1182 notit->meta.ec->name->str);
fdf0e7a0 1183
e98a2d6e 1184end:
e98a2d6e
PP
1185 return status;
1186}
1187
312c056a
PP
1188static inline
1189enum bt_notif_iter_status set_current_event_notification(
1190 struct bt_notif_iter *notit)
1191{
1192 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
0d72b8c3 1193 struct bt_notification *notif = NULL;
312c056a 1194
44c440bc 1195 BT_ASSERT(notit->meta.ec);
312c056a
PP
1196 BT_ASSERT(notit->packet);
1197 BT_LOGV("Creating event notification from event class and packet: "
1198 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", packet-addr=%p",
44c440bc
PP
1199 notit, notit->meta.ec,
1200 notit->meta.ec->name->str,
312c056a 1201 notit->packet);
f0010051 1202 BT_ASSERT(notit->notif_iter);
0d72b8c3 1203 notif = bt_notification_event_create(notit->notif_iter,
44c440bc 1204 notit->meta.ec->ir_ec, notit->packet);
312c056a
PP
1205 if (!notif) {
1206 BT_LOGE("Cannot create event notification: "
1207 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", "
1208 "packet-addr=%p",
44c440bc
PP
1209 notit, notit->meta.ec,
1210 notit->meta.ec->name->str,
312c056a
PP
1211 notit->packet);
1212 goto error;
1213 }
1214
1215 goto end;
1216
1217error:
65300d60 1218 BT_OBJECT_PUT_REF_AND_RESET(notif);
312c056a
PP
1219 status = BT_NOTIF_ITER_STATUS_ERROR;
1220
1221end:
65300d60 1222 BT_OBJECT_MOVE_REF(notit->event_notif, notif);
312c056a
PP
1223 return status;
1224}
1225
e98a2d6e 1226static
50842bdc
PP
1227enum bt_notif_iter_status after_event_header_state(
1228 struct bt_notif_iter *notit)
e98a2d6e 1229{
50842bdc 1230 enum bt_notif_iter_status status;
e98a2d6e 1231
e98a2d6e 1232 status = set_current_event_class(notit);
50842bdc 1233 if (status != BT_NOTIF_ITER_STATUS_OK) {
e98a2d6e
PP
1234 goto end;
1235 }
1236
312c056a
PP
1237 status = set_current_event_notification(notit);
1238 if (status != BT_NOTIF_ITER_STATUS_OK) {
1239 goto end;
1240 }
1241
0d72b8c3 1242 notit->event = bt_notification_event_borrow_event(
e5be10ef 1243 notit->event_notif);
312c056a
PP
1244 BT_ASSERT(notit->event);
1245
1246 if (notit->event_header_field) {
1247 int ret;
1248
1249 BT_ASSERT(notit->event);
40f4ba76 1250 ret = bt_event_move_header_field(notit->event,
312c056a 1251 notit->event_header_field);
312c056a
PP
1252 if (ret) {
1253 status = BT_NOTIF_ITER_STATUS_ERROR;
1254 goto end;
1255 }
1256
1257 notit->event_header_field = NULL;
1258
1259 /*
44c440bc 1260 * At this point notit->dscopes.event_header has
312c056a
PP
1261 * the same value as the event header field within
1262 * notit->event.
1263 */
40f4ba76 1264 BT_ASSERT(bt_event_borrow_header_field(
e5be10ef 1265 notit->event) == notit->dscopes.event_header);
312c056a
PP
1266 }
1267
44c440bc 1268 notit->state = STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN;
e98a2d6e
PP
1269
1270end:
1271 return status;
1272}
1273
1274static
44c440bc 1275enum bt_notif_iter_status read_event_common_context_begin_state(
50842bdc 1276 struct bt_notif_iter *notit)
e98a2d6e 1277{
50842bdc 1278 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
5cd6d0e5 1279 struct ctf_field_class *event_common_context_fc;
e98a2d6e 1280
5cd6d0e5
PP
1281 event_common_context_fc = notit->meta.sc->event_common_context_fc;
1282 if (!event_common_context_fc) {
44c440bc 1283 notit->state = STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN;
e98a2d6e
PP
1284 goto end;
1285 }
1286
5cd6d0e5 1287 if (event_common_context_fc->in_ir) {
44c440bc
PP
1288 BT_ASSERT(!notit->dscopes.event_common_context);
1289 notit->dscopes.event_common_context =
40f4ba76 1290 bt_event_borrow_common_context_field(
e5be10ef 1291 notit->event);
44c440bc
PP
1292 BT_ASSERT(notit->dscopes.event_common_context);
1293 }
1294
1295 BT_LOGV("Decoding event common context field: "
fdf0e7a0 1296 "notit-addr=%p, stream-class-addr=%p, "
44c440bc 1297 "stream-class-id=%" PRId64 ", "
5cd6d0e5 1298 "fc-addr=%p",
44c440bc
PP
1299 notit, notit->meta.sc,
1300 notit->meta.sc->id,
5cd6d0e5
PP
1301 event_common_context_fc);
1302 status = read_dscope_begin_state(notit, event_common_context_fc,
44c440bc
PP
1303 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN,
1304 STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE,
1305 notit->dscopes.event_common_context);
fdf0e7a0 1306 if (status < 0) {
44c440bc 1307 BT_LOGW("Cannot decode event common context field: "
fdf0e7a0 1308 "notit-addr=%p, stream-class-addr=%p, "
5cd6d0e5 1309 "stream-class-id=%" PRId64 ", fc-addr=%p",
44c440bc
PP
1310 notit, notit->meta.sc,
1311 notit->meta.sc->id,
5cd6d0e5 1312 event_common_context_fc);
fdf0e7a0 1313 }
e98a2d6e
PP
1314
1315end:
e98a2d6e
PP
1316 return status;
1317}
1318
1319static
44c440bc 1320enum bt_notif_iter_status read_event_common_context_continue_state(
50842bdc 1321 struct bt_notif_iter *notit)
e98a2d6e
PP
1322{
1323 return read_dscope_continue_state(notit,
44c440bc 1324 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN);
e98a2d6e
PP
1325}
1326
1327static
44c440bc 1328enum bt_notif_iter_status read_event_spec_context_begin_state(
50842bdc 1329 struct bt_notif_iter *notit)
e98a2d6e 1330{
50842bdc 1331 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
5cd6d0e5 1332 struct ctf_field_class *event_spec_context_fc;
e98a2d6e 1333
5cd6d0e5
PP
1334 event_spec_context_fc = notit->meta.ec->spec_context_fc;
1335 if (!event_spec_context_fc) {
835b2d10 1336 notit->state = STATE_DSCOPE_EVENT_PAYLOAD_BEGIN;
e98a2d6e
PP
1337 goto end;
1338 }
fdf0e7a0 1339
5cd6d0e5 1340 if (event_spec_context_fc->in_ir) {
44c440bc 1341 BT_ASSERT(!notit->dscopes.event_spec_context);
e5be10ef 1342 notit->dscopes.event_spec_context =
40f4ba76 1343 bt_event_borrow_specific_context_field(
e5be10ef 1344 notit->event);
44c440bc
PP
1345 BT_ASSERT(notit->dscopes.event_spec_context);
1346 }
1347
1348 BT_LOGV("Decoding event specific context field: "
fdf0e7a0
PP
1349 "notit-addr=%p, event-class-addr=%p, "
1350 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
5cd6d0e5 1351 "fc-addr=%p",
44c440bc
PP
1352 notit, notit->meta.ec,
1353 notit->meta.ec->name->str,
1354 notit->meta.ec->id,
5cd6d0e5
PP
1355 event_spec_context_fc);
1356 status = read_dscope_begin_state(notit, event_spec_context_fc,
e98a2d6e 1357 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN,
44c440bc
PP
1358 STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE,
1359 notit->dscopes.event_spec_context);
fdf0e7a0 1360 if (status < 0) {
44c440bc 1361 BT_LOGW("Cannot decode event specific context field: "
fdf0e7a0
PP
1362 "notit-addr=%p, event-class-addr=%p, "
1363 "event-class-name=\"%s\", "
5cd6d0e5 1364 "event-class-id=%" PRId64 ", fc-addr=%p",
44c440bc
PP
1365 notit, notit->meta.ec,
1366 notit->meta.ec->name->str,
1367 notit->meta.ec->id,
5cd6d0e5 1368 event_spec_context_fc);
fdf0e7a0 1369 }
e98a2d6e
PP
1370
1371end:
e98a2d6e
PP
1372 return status;
1373}
1374
1375static
44c440bc 1376enum bt_notif_iter_status read_event_spec_context_continue_state(
50842bdc 1377 struct bt_notif_iter *notit)
e98a2d6e
PP
1378{
1379 return read_dscope_continue_state(notit,
1380 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN);
1381}
1382
1383static
50842bdc
PP
1384enum bt_notif_iter_status read_event_payload_begin_state(
1385 struct bt_notif_iter *notit)
e98a2d6e 1386{
50842bdc 1387 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
5cd6d0e5 1388 struct ctf_field_class *event_payload_fc;
e98a2d6e 1389
5cd6d0e5
PP
1390 event_payload_fc = notit->meta.ec->payload_fc;
1391 if (!event_payload_fc) {
835b2d10 1392 notit->state = STATE_EMIT_NOTIF_EVENT;
e98a2d6e
PP
1393 goto end;
1394 }
1395
5cd6d0e5 1396 if (event_payload_fc->in_ir) {
44c440bc 1397 BT_ASSERT(!notit->dscopes.event_payload);
e5be10ef 1398 notit->dscopes.event_payload =
40f4ba76 1399 bt_event_borrow_payload_field(
e5be10ef 1400 notit->event);
44c440bc
PP
1401 BT_ASSERT(notit->dscopes.event_payload);
1402 }
1403
fdf0e7a0
PP
1404 BT_LOGV("Decoding event payload field: "
1405 "notit-addr=%p, event-class-addr=%p, "
1406 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
5cd6d0e5 1407 "fc-addr=%p",
44c440bc
PP
1408 notit, notit->meta.ec,
1409 notit->meta.ec->name->str,
1410 notit->meta.ec->id,
5cd6d0e5
PP
1411 event_payload_fc);
1412 status = read_dscope_begin_state(notit, event_payload_fc,
e98a2d6e
PP
1413 STATE_EMIT_NOTIF_EVENT,
1414 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE,
312c056a 1415 notit->dscopes.event_payload);
fdf0e7a0
PP
1416 if (status < 0) {
1417 BT_LOGW("Cannot decode event payload field: "
1418 "notit-addr=%p, event-class-addr=%p, "
1419 "event-class-name=\"%s\", "
5cd6d0e5 1420 "event-class-id=%" PRId64 ", fc-addr=%p",
44c440bc
PP
1421 notit, notit->meta.ec,
1422 notit->meta.ec->name->str,
1423 notit->meta.ec->id,
5cd6d0e5 1424 event_payload_fc);
fdf0e7a0 1425 }
e98a2d6e
PP
1426
1427end:
e98a2d6e
PP
1428 return status;
1429}
1430
1431static
50842bdc
PP
1432enum bt_notif_iter_status read_event_payload_continue_state(
1433 struct bt_notif_iter *notit)
e98a2d6e
PP
1434{
1435 return read_dscope_continue_state(notit, STATE_EMIT_NOTIF_EVENT);
1436}
1437
1438static
50842bdc
PP
1439enum bt_notif_iter_status skip_packet_padding_state(
1440 struct bt_notif_iter *notit)
e98a2d6e 1441{
50842bdc 1442 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
e98a2d6e
PP
1443 size_t bits_to_skip;
1444
44c440bc
PP
1445 BT_ASSERT(notit->cur_exp_packet_total_size > 0);
1446 bits_to_skip = notit->cur_exp_packet_total_size - packet_at(notit);
e98a2d6e
PP
1447 if (bits_to_skip == 0) {
1448 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1449 goto end;
1450 } else {
1451 size_t bits_to_consume;
fdf0e7a0
PP
1452
1453 BT_LOGV("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu",
1454 bits_to_skip, notit, bits_to_skip);
e98a2d6e 1455 status = buf_ensure_available_bits(notit);
50842bdc 1456 if (status != BT_NOTIF_ITER_STATUS_OK) {
e98a2d6e
PP
1457 goto end;
1458 }
1459
1460 bits_to_consume = MIN(buf_available_bits(notit), bits_to_skip);
fdf0e7a0
PP
1461 BT_LOGV("Skipping %zu bits of padding: notit-addr=%p, size=%zu",
1462 bits_to_consume, notit, bits_to_consume);
e98a2d6e 1463 buf_consume_bits(notit, bits_to_consume);
44c440bc
PP
1464 bits_to_skip = notit->cur_exp_packet_total_size -
1465 packet_at(notit);
e98a2d6e
PP
1466 if (bits_to_skip == 0) {
1467 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1468 goto end;
1469 }
1470 }
1471
1472end:
1473 return status;
1474}
1475
1476static inline
50842bdc 1477enum bt_notif_iter_status handle_state(struct bt_notif_iter *notit)
e98a2d6e 1478{
50842bdc 1479 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
fdf0e7a0 1480 const enum state state = notit->state;
e98a2d6e 1481
fdf0e7a0
PP
1482 BT_LOGV("Handling state: notit-addr=%p, state=%s",
1483 notit, state_string(state));
e98a2d6e
PP
1484
1485 // TODO: optimalize!
fdf0e7a0 1486 switch (state) {
e98a2d6e
PP
1487 case STATE_INIT:
1488 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1489 break;
1490 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
1491 status = read_packet_header_begin_state(notit);
1492 break;
1493 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
1494 status = read_packet_header_continue_state(notit);
1495 break;
1496 case STATE_AFTER_TRACE_PACKET_HEADER:
1497 status = after_packet_header_state(notit);
1498 break;
1499 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN:
1500 status = read_packet_context_begin_state(notit);
1501 break;
1502 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE:
1503 status = read_packet_context_continue_state(notit);
1504 break;
1505 case STATE_AFTER_STREAM_PACKET_CONTEXT:
1506 status = after_packet_context_state(notit);
1507 break;
f42867e2
PP
1508 case STATE_EMIT_NOTIF_NEW_STREAM:
1509 notit->state = STATE_EMIT_NOTIF_NEW_PACKET;
1510 break;
e98a2d6e 1511 case STATE_EMIT_NOTIF_NEW_PACKET:
44c440bc 1512 notit->state = STATE_DSCOPE_EVENT_HEADER_BEGIN;
e98a2d6e 1513 break;
44c440bc 1514 case STATE_DSCOPE_EVENT_HEADER_BEGIN:
e98a2d6e
PP
1515 status = read_event_header_begin_state(notit);
1516 break;
44c440bc 1517 case STATE_DSCOPE_EVENT_HEADER_CONTINUE:
e98a2d6e
PP
1518 status = read_event_header_continue_state(notit);
1519 break;
44c440bc 1520 case STATE_AFTER_EVENT_HEADER:
e98a2d6e
PP
1521 status = after_event_header_state(notit);
1522 break;
44c440bc
PP
1523 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN:
1524 status = read_event_common_context_begin_state(notit);
e98a2d6e 1525 break;
44c440bc
PP
1526 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE:
1527 status = read_event_common_context_continue_state(notit);
e98a2d6e 1528 break;
44c440bc
PP
1529 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN:
1530 status = read_event_spec_context_begin_state(notit);
e98a2d6e 1531 break;
44c440bc
PP
1532 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE:
1533 status = read_event_spec_context_continue_state(notit);
e98a2d6e
PP
1534 break;
1535 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN:
1536 status = read_event_payload_begin_state(notit);
1537 break;
1538 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE:
1539 status = read_event_payload_continue_state(notit);
1540 break;
1541 case STATE_EMIT_NOTIF_EVENT:
44c440bc 1542 notit->state = STATE_DSCOPE_EVENT_HEADER_BEGIN;
e98a2d6e
PP
1543 break;
1544 case STATE_SKIP_PACKET_PADDING:
1545 status = skip_packet_padding_state(notit);
1546 break;
1547 case STATE_EMIT_NOTIF_END_OF_PACKET:
1548 notit->state = STATE_SKIP_PACKET_PADDING;
1549 break;
fdf0e7a0
PP
1550 default:
1551 BT_LOGD("Unknown CTF plugin notification iterator state: "
1552 "notit-addr=%p, state=%d", notit, notit->state);
1553 abort();
e98a2d6e
PP
1554 }
1555
fdf0e7a0
PP
1556 BT_LOGV("Handled state: notit-addr=%p, status=%s, "
1557 "prev-state=%s, cur-state=%s",
50842bdc 1558 notit, bt_notif_iter_status_string(status),
fdf0e7a0 1559 state_string(state), state_string(notit->state));
e98a2d6e
PP
1560 return status;
1561}
1562
2cf1d51e
JG
1563/**
1564 * Resets the internal state of a CTF notification iterator.
2cf1d51e 1565 */
f42867e2 1566BT_HIDDEN
50842bdc 1567void bt_notif_iter_reset(struct bt_notif_iter *notit)
e98a2d6e 1568{
f6ccaed9 1569 BT_ASSERT(notit);
fdf0e7a0 1570 BT_LOGD("Resetting notification iterator: addr=%p", notit);
e98a2d6e 1571 stack_clear(notit->stack);
44c440bc
PP
1572 notit->meta.sc = NULL;
1573 notit->meta.ec = NULL;
65300d60
PP
1574 BT_OBJECT_PUT_REF_AND_RESET(notit->packet);
1575 BT_OBJECT_PUT_REF_AND_RESET(notit->stream);
1576 BT_OBJECT_PUT_REF_AND_RESET(notit->event_notif);
312c056a
PP
1577 release_all_dscopes(notit);
1578 notit->cur_dscope_field = NULL;
1579
1580 if (notit->packet_header_field) {
40f4ba76 1581 bt_packet_header_field_release(notit->packet_header_field);
312c056a
PP
1582 notit->packet_header_field = NULL;
1583 }
1584
1585 if (notit->packet_context_field) {
40f4ba76 1586 bt_packet_context_field_release(notit->packet_context_field);
312c056a
PP
1587 notit->packet_context_field = NULL;
1588 }
1589
1590 if (notit->event_header_field) {
40f4ba76 1591 bt_event_header_field_release(notit->event_header_field);
312c056a
PP
1592 notit->event_header_field = NULL;
1593 }
1594
e98a2d6e
PP
1595 notit->buf.addr = NULL;
1596 notit->buf.sz = 0;
1597 notit->buf.at = 0;
2b186c3e 1598 notit->buf.last_eh_at = SIZE_MAX;
e98a2d6e
PP
1599 notit->buf.packet_offset = 0;
1600 notit->state = STATE_INIT;
44c440bc
PP
1601 notit->cur_exp_packet_content_size = -1;
1602 notit->cur_exp_packet_total_size = -1;
9e0c8dbb 1603 notit->cur_packet_offset = -1;
44c440bc
PP
1604 notit->cur_stream_class_id = -1;
1605 notit->cur_event_class_id = -1;
1606 notit->cur_data_stream_id = -1;
f42867e2 1607 notit->stream_begin_emitted = false;
e98a2d6e
PP
1608}
1609
2cf1d51e 1610static
50842bdc 1611int bt_notif_iter_switch_packet(struct bt_notif_iter *notit)
2cf1d51e
JG
1612{
1613 int ret = 0;
1614
115de887
PP
1615 /*
1616 * We don't put the stream class here because we need to make
1617 * sure that all the packets processed by the same notification
1618 * iterator refer to the same stream class (the first one).
1619 */
f6ccaed9 1620 BT_ASSERT(notit);
312c056a 1621
44c440bc
PP
1622 if (notit->cur_exp_packet_total_size != -1) {
1623 notit->cur_packet_offset += notit->cur_exp_packet_total_size;
9e0c8dbb 1624 }
312c056a 1625
9e0c8dbb
JG
1626 BT_LOGV("Switching packet: notit-addr=%p, cur=%zu, "
1627 "packet-offset=%" PRId64, notit, notit->buf.at,
1628 notit->cur_packet_offset);
2cf1d51e 1629 stack_clear(notit->stack);
44c440bc 1630 notit->meta.ec = NULL;
65300d60
PP
1631 BT_OBJECT_PUT_REF_AND_RESET(notit->packet);
1632 BT_OBJECT_PUT_REF_AND_RESET(notit->event_notif);
312c056a
PP
1633 release_all_dscopes(notit);
1634 notit->cur_dscope_field = NULL;
2cf1d51e
JG
1635
1636 /*
1637 * Adjust current buffer so that addr points to the beginning of the new
1638 * packet.
1639 */
1640 if (notit->buf.addr) {
1641 size_t consumed_bytes = (size_t) (notit->buf.at / CHAR_BIT);
1642
1643 /* Packets are assumed to start on a byte frontier. */
1644 if (notit->buf.at % CHAR_BIT) {
fdf0e7a0
PP
1645 BT_LOGW("Cannot switch packet: current position is not a multiple of 8: "
1646 "notit-addr=%p, cur=%zu", notit, notit->buf.at);
2cf1d51e
JG
1647 ret = -1;
1648 goto end;
1649 }
1650
1651 notit->buf.addr += consumed_bytes;
1652 notit->buf.sz -= consumed_bytes;
1653 notit->buf.at = 0;
1654 notit->buf.packet_offset = 0;
fdf0e7a0
PP
1655 BT_LOGV("Adjusted buffer: addr=%p, size=%zu",
1656 notit->buf.addr, notit->buf.sz);
2cf1d51e
JG
1657 }
1658
44c440bc
PP
1659 notit->cur_exp_packet_content_size = -1;
1660 notit->cur_exp_packet_total_size = -1;
1661 notit->cur_stream_class_id = -1;
1662 notit->cur_event_class_id = -1;
1663 notit->cur_data_stream_id = -1;
1664 notit->snapshots.discarded_events = UINT64_C(-1);
1665 notit->snapshots.packets = UINT64_C(-1);
1666 notit->snapshots.beginning_clock = UINT64_C(-1);
1667 notit->snapshots.end_clock = UINT64_C(-1);
fdf0e7a0 1668
2cf1d51e
JG
1669end:
1670 return ret;
1671}
1672
e98a2d6e 1673static
40f4ba76 1674struct bt_field *borrow_next_field(struct bt_notif_iter *notit)
e98a2d6e 1675{
40f4ba76
PP
1676 struct bt_field *next_field = NULL;
1677 struct bt_field *base_field;
1678 const struct bt_field_class *base_fc;
e98a2d6e
PP
1679 size_t index;
1680
f6ccaed9 1681 BT_ASSERT(!stack_empty(notit->stack));
e98a2d6e
PP
1682 index = stack_top(notit->stack)->index;
1683 base_field = stack_top(notit->stack)->base;
f6ccaed9 1684 BT_ASSERT(base_field);
40f4ba76 1685 base_fc = bt_field_borrow_class_const(base_field);
5cd6d0e5 1686 BT_ASSERT(base_fc);
e98a2d6e 1687
40f4ba76 1688 switch (bt_field_class_get_type(base_fc)) {
864cad70 1689 case BT_FIELD_CLASS_TYPE_STRUCTURE:
f42867e2 1690 {
44c440bc 1691 BT_ASSERT(index <
5cd6d0e5 1692 bt_field_class_structure_get_member_count(
40f4ba76
PP
1693 bt_field_borrow_class_const(
1694 base_field)));
e5be10ef 1695 next_field =
40f4ba76 1696 bt_field_structure_borrow_member_field_by_index(
e5be10ef 1697 base_field, index);
e98a2d6e 1698 break;
f42867e2 1699 }
864cad70
PP
1700 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
1701 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
40f4ba76
PP
1702 BT_ASSERT(index < bt_field_array_get_length(base_field));
1703 next_field = bt_field_array_borrow_element_field_by_index(
44c440bc 1704 base_field, index);
e98a2d6e 1705 break;
864cad70 1706 case BT_FIELD_CLASS_TYPE_VARIANT:
44c440bc 1707 BT_ASSERT(index == 0);
40f4ba76 1708 next_field = bt_field_variant_borrow_selected_option_field(
44c440bc 1709 base_field);
e98a2d6e
PP
1710 break;
1711 default:
0fbb9a9f 1712 abort();
e98a2d6e
PP
1713 }
1714
44c440bc 1715 BT_ASSERT(next_field);
e98a2d6e
PP
1716 return next_field;
1717}
1718
c44c3e70 1719static
44c440bc
PP
1720void update_default_clock(struct bt_notif_iter *notit, uint64_t new_val,
1721 uint64_t new_val_size)
c44c3e70 1722{
44c440bc 1723 uint64_t new_val_mask;
c44c3e70 1724 uint64_t cur_value_masked;
c44c3e70 1725
44c440bc 1726 BT_ASSERT(new_val_size > 0);
c44c3e70
JG
1727
1728 /*
1729 * Special case for a 64-bit new value, which is the limit
1730 * of a clock value as of this version: overwrite the
1731 * current value directly.
1732 */
44c440bc
PP
1733 if (new_val_size == 64) {
1734 notit->default_clock_val = new_val;
c44c3e70
JG
1735 goto end;
1736 }
1737
44c440bc
PP
1738 new_val_mask = (1ULL << new_val_size) - 1;
1739 cur_value_masked = notit->default_clock_val & new_val_mask;
c44c3e70 1740
44c440bc 1741 if (new_val < cur_value_masked) {
c44c3e70
JG
1742 /*
1743 * It looks like a wrap happened on the number of bits
1744 * of the requested new value. Assume that the clock
1745 * value wrapped only one time.
1746 */
44c440bc 1747 notit->default_clock_val += new_val_mask + 1;
c44c3e70
JG
1748 }
1749
1750 /* Clear the low bits of the current clock value. */
44c440bc 1751 notit->default_clock_val &= ~new_val_mask;
c44c3e70
JG
1752
1753 /* Set the low bits of the current clock value. */
44c440bc 1754 notit->default_clock_val |= new_val;
d1e46835 1755
c44c3e70 1756end:
44c440bc
PP
1757 BT_LOGV("Updated default clock's value from integer field's value: "
1758 "value=%" PRIu64, notit->default_clock_val);
c44c3e70
JG
1759}
1760
1761static
5cd6d0e5
PP
1762enum bt_bfcr_status bfcr_unsigned_int_cb(uint64_t value,
1763 struct ctf_field_class *fc, void *data)
c44c3e70 1764{
44c440bc 1765 struct bt_notif_iter *notit = data;
5cd6d0e5 1766 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
40f4ba76 1767 struct bt_field *field = NULL;
5cd6d0e5 1768 struct ctf_field_class_int *int_fc = (void *) fc;
c44c3e70 1769
5cd6d0e5
PP
1770 BT_LOGV("Unsigned integer function called from BFCR: "
1771 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
864cad70
PP
1772 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64,
1773 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
312c056a 1774
5cd6d0e5 1775 if (likely(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE)) {
44c440bc 1776 goto update_def_clock;
c44c3e70
JG
1777 }
1778
5cd6d0e5
PP
1779 switch (int_fc->meaning) {
1780 case CTF_FIELD_CLASS_MEANING_EVENT_CLASS_ID:
44c440bc
PP
1781 notit->cur_event_class_id = value;
1782 break;
5cd6d0e5 1783 case CTF_FIELD_CLASS_MEANING_DATA_STREAM_ID:
44c440bc
PP
1784 notit->cur_data_stream_id = value;
1785 break;
5cd6d0e5 1786 case CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME:
44c440bc
PP
1787 notit->snapshots.beginning_clock = value;
1788 break;
5cd6d0e5 1789 case CTF_FIELD_CLASS_MEANING_PACKET_END_TIME:
44c440bc
PP
1790 notit->snapshots.end_clock = value;
1791 break;
5cd6d0e5 1792 case CTF_FIELD_CLASS_MEANING_STREAM_CLASS_ID:
44c440bc
PP
1793 notit->cur_stream_class_id = value;
1794 break;
5cd6d0e5 1795 case CTF_FIELD_CLASS_MEANING_MAGIC:
44c440bc
PP
1796 if (value != 0xc1fc1fc1) {
1797 BT_LOGW("Invalid CTF magic number: notit-addr=%p, "
1798 "magic=%" PRIx64, notit, value);
5cd6d0e5 1799 status = BT_BFCR_STATUS_ERROR;
c44c3e70
JG
1800 goto end;
1801 }
fdf0e7a0 1802
44c440bc 1803 break;
5cd6d0e5 1804 case CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT:
44c440bc
PP
1805 notit->snapshots.packets = value;
1806 break;
5cd6d0e5 1807 case CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT:
44c440bc
PP
1808 notit->snapshots.discarded_events = value;
1809 break;
5cd6d0e5 1810 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_TOTAL_SIZE:
44c440bc
PP
1811 notit->cur_exp_packet_total_size = value;
1812 break;
5cd6d0e5 1813 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_CONTENT_SIZE:
44c440bc
PP
1814 notit->cur_exp_packet_content_size = value;
1815 break;
1816 default:
1817 abort();
c44c3e70
JG
1818 }
1819
44c440bc 1820update_def_clock:
5cd6d0e5
PP
1821 if (unlikely(int_fc->mapped_clock_class)) {
1822 update_default_clock(notit, value, int_fc->base.size);
44c440bc 1823 }
c44c3e70 1824
5cd6d0e5 1825 if (unlikely(int_fc->storing_index >= 0)) {
44c440bc 1826 g_array_index(notit->stored_values, uint64_t,
5cd6d0e5 1827 (uint64_t) int_fc->storing_index) = value;
44c440bc 1828 }
e98a2d6e 1829
5cd6d0e5 1830 if (unlikely(!fc->in_ir)) {
312c056a 1831 goto end;
e98a2d6e
PP
1832 }
1833
44c440bc
PP
1834 field = borrow_next_field(notit);
1835 BT_ASSERT(field);
40f4ba76
PP
1836 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
1837 BT_ASSERT(bt_field_get_class_type(field) ==
1838 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER ||
1839 bt_field_get_class_type(field) ==
1840 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION);
1841 bt_field_unsigned_integer_set_value(field, value);
e98a2d6e 1842 stack_top(notit->stack)->index++;
fdf0e7a0 1843
312c056a 1844end:
e98a2d6e
PP
1845 return status;
1846}
1847
5f870343 1848static
5cd6d0e5
PP
1849enum bt_bfcr_status bfcr_unsigned_int_char_cb(uint64_t value,
1850 struct ctf_field_class *fc, void *data)
5f870343 1851{
44c440bc 1852 int ret;
50842bdc 1853 struct bt_notif_iter *notit = data;
5cd6d0e5 1854 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
40f4ba76 1855 struct bt_field *string_field = NULL;
5cd6d0e5 1856 struct ctf_field_class_int *int_fc = (void *) fc;
44c440bc 1857 char str[2] = {'\0', '\0'};
5f870343 1858
5cd6d0e5
PP
1859 BT_LOGV("Unsigned integer character function called from BFCR: "
1860 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
864cad70
PP
1861 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64,
1862 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
5cd6d0e5
PP
1863 BT_ASSERT(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE);
1864 BT_ASSERT(!int_fc->mapped_clock_class);
1865 BT_ASSERT(int_fc->storing_index < 0);
312c056a 1866
5cd6d0e5 1867 if (unlikely(!fc->in_ir)) {
44c440bc
PP
1868 goto end;
1869 }
5f870343 1870
44c440bc 1871 if (notit->done_filling_string) {
5f870343
JG
1872 goto end;
1873 }
1874
44c440bc
PP
1875 if (value == 0) {
1876 notit->done_filling_string = true;
5f870343
JG
1877 goto end;
1878 }
1879
44c440bc 1880 string_field = stack_top(notit->stack)->base;
40f4ba76
PP
1881 BT_ASSERT(bt_field_get_class_type(string_field) ==
1882 BT_FIELD_CLASS_TYPE_STRING);
44c440bc
PP
1883
1884 /* Append character */
1885 str[0] = (char) value;
40f4ba76 1886 ret = bt_field_string_append_with_length(string_field, str, 1);
44c440bc
PP
1887 if (ret) {
1888 BT_LOGE("Cannot append character to string field's value: "
1889 "notit-addr=%p, field-addr=%p, ret=%d",
1890 notit, string_field, ret);
5cd6d0e5 1891 status = BT_BFCR_STATUS_ERROR;
44c440bc
PP
1892 goto end;
1893 }
312c056a 1894
5f870343
JG
1895end:
1896 return status;
1897}
1898
1899static
5cd6d0e5
PP
1900enum bt_bfcr_status bfcr_signed_int_cb(int64_t value,
1901 struct ctf_field_class *fc, void *data)
e98a2d6e 1902{
5cd6d0e5 1903 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
40f4ba76 1904 struct bt_field *field = NULL;
50842bdc 1905 struct bt_notif_iter *notit = data;
5cd6d0e5 1906 struct ctf_field_class_int *int_fc = (void *) fc;
e98a2d6e 1907
5cd6d0e5
PP
1908 BT_LOGV("Signed integer function called from BFCR: "
1909 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
864cad70
PP
1910 "fc-type=%d, fc-in-ir=%d, value=%" PRId64,
1911 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
5cd6d0e5 1912 BT_ASSERT(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE);
44c440bc 1913
5cd6d0e5 1914 if (unlikely(int_fc->storing_index >= 0)) {
44c440bc 1915 g_array_index(notit->stored_values, uint64_t,
5cd6d0e5 1916 (uint64_t) int_fc->storing_index) = (uint64_t) value;
44c440bc
PP
1917 }
1918
5cd6d0e5 1919 if (unlikely(!fc->in_ir)) {
312c056a 1920 goto end;
e98a2d6e
PP
1921 }
1922
44c440bc
PP
1923 field = borrow_next_field(notit);
1924 BT_ASSERT(field);
40f4ba76
PP
1925 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
1926 BT_ASSERT(bt_field_get_class_type(field) ==
1927 BT_FIELD_CLASS_TYPE_SIGNED_INTEGER ||
1928 bt_field_get_class_type(field) ==
1929 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION);
1930 bt_field_signed_integer_set_value(field, value);
e98a2d6e 1931 stack_top(notit->stack)->index++;
fdf0e7a0 1932
312c056a 1933end:
e98a2d6e
PP
1934 return status;
1935}
1936
1937static
5cd6d0e5
PP
1938enum bt_bfcr_status bfcr_floating_point_cb(double value,
1939 struct ctf_field_class *fc, void *data)
e98a2d6e 1940{
5cd6d0e5 1941 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
40f4ba76 1942 struct bt_field *field = NULL;
50842bdc 1943 struct bt_notif_iter *notit = data;
e98a2d6e 1944
5cd6d0e5
PP
1945 BT_LOGV("Floating point number function called from BFCR: "
1946 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
864cad70
PP
1947 "fc-type=%d, fc-in-ir=%d, value=%f",
1948 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
5cd6d0e5 1949 BT_ASSERT(fc->in_ir);
312c056a 1950 field = borrow_next_field(notit);
44c440bc 1951 BT_ASSERT(field);
40f4ba76
PP
1952 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
1953 BT_ASSERT(bt_field_get_class_type(field) ==
1954 BT_FIELD_CLASS_TYPE_REAL);
1955 bt_field_real_set_value(field, value);
e98a2d6e 1956 stack_top(notit->stack)->index++;
e98a2d6e
PP
1957 return status;
1958}
1959
1960static
5cd6d0e5
PP
1961enum bt_bfcr_status bfcr_string_begin_cb(
1962 struct ctf_field_class *fc, void *data)
e98a2d6e 1963{
40f4ba76 1964 struct bt_field *field = NULL;
50842bdc 1965 struct bt_notif_iter *notit = data;
e98a2d6e
PP
1966 int ret;
1967
5cd6d0e5
PP
1968 BT_LOGV("String (beginning) function called from BFCR: "
1969 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
864cad70
PP
1970 "fc-type=%d, fc-in-ir=%d",
1971 notit, notit->bfcr, fc, fc->type, fc->in_ir);
e98a2d6e 1972
5cd6d0e5 1973 BT_ASSERT(fc->in_ir);
44c440bc
PP
1974 field = borrow_next_field(notit);
1975 BT_ASSERT(field);
40f4ba76
PP
1976 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
1977 BT_ASSERT(bt_field_get_class_type(field) ==
1978 BT_FIELD_CLASS_TYPE_STRING);
1979 ret = bt_field_string_clear(field);
312c056a
PP
1980 BT_ASSERT(ret == 0);
1981
e98a2d6e 1982 /*
5cd6d0e5
PP
1983 * Push on stack. Not a compound class per se, but we know that
1984 * only bfcr_string_cb() may be called between this call and a
1985 * subsequent call to bfcr_string_end_cb().
e98a2d6e 1986 */
44c440bc 1987 stack_push(notit->stack, field);
5cd6d0e5 1988 return BT_BFCR_STATUS_OK;
e98a2d6e
PP
1989}
1990
1991static
5cd6d0e5
PP
1992enum bt_bfcr_status bfcr_string_cb(const char *value,
1993 size_t len, struct ctf_field_class *fc, void *data)
e98a2d6e 1994{
5cd6d0e5 1995 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
40f4ba76 1996 struct bt_field *field = NULL;
50842bdc 1997 struct bt_notif_iter *notit = data;
e98a2d6e
PP
1998 int ret;
1999
5cd6d0e5
PP
2000 BT_LOGV("String (substring) function called from BFCR: "
2001 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
864cad70
PP
2002 "fc-type=%d, fc-in-ir=%d, string-length=%zu",
2003 notit, notit->bfcr, fc, fc->type, fc->in_ir,
fdf0e7a0 2004 len);
5cd6d0e5 2005 BT_ASSERT(fc->in_ir);
e98a2d6e 2006 field = stack_top(notit->stack)->base;
f6ccaed9 2007 BT_ASSERT(field);
e98a2d6e 2008
312c056a 2009 /* Append current substring */
40f4ba76 2010 ret = bt_field_string_append_with_length(field, value, len);
e98a2d6e 2011 if (ret) {
fdf0e7a0
PP
2012 BT_LOGE("Cannot append substring to string field's value: "
2013 "notit-addr=%p, field-addr=%p, string-length=%zu, "
2014 "ret=%d", notit, field, len, ret);
5cd6d0e5 2015 status = BT_BFCR_STATUS_ERROR;
e98a2d6e
PP
2016 goto end;
2017 }
2018
2019end:
2020 return status;
2021}
2022
2023static
5cd6d0e5
PP
2024enum bt_bfcr_status bfcr_string_end_cb(
2025 struct ctf_field_class *fc, void *data)
e98a2d6e 2026{
50842bdc 2027 struct bt_notif_iter *notit = data;
e98a2d6e 2028
5cd6d0e5
PP
2029 BT_LOGV("String (end) function called from BFCR: "
2030 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
864cad70
PP
2031 "fc-type=%d, fc-in-ir=%d",
2032 notit, notit->bfcr, fc, fc->type, fc->in_ir);
5cd6d0e5 2033 BT_ASSERT(fc->in_ir);
fdf0e7a0 2034
e98a2d6e
PP
2035 /* Pop string field */
2036 stack_pop(notit->stack);
2037
2038 /* Go to next field */
2039 stack_top(notit->stack)->index++;
5cd6d0e5 2040 return BT_BFCR_STATUS_OK;
e98a2d6e
PP
2041}
2042
5cd6d0e5
PP
2043enum bt_bfcr_status bfcr_compound_begin_cb(
2044 struct ctf_field_class *fc, void *data)
e98a2d6e 2045{
50842bdc 2046 struct bt_notif_iter *notit = data;
40f4ba76 2047 struct bt_field *field;
e98a2d6e 2048
5cd6d0e5
PP
2049 BT_LOGV("Compound (beginning) function called from BFCR: "
2050 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
864cad70
PP
2051 "fc-type=%d, fc-in-ir=%d",
2052 notit, notit->bfcr, fc, fc->type, fc->in_ir);
44c440bc 2053
5cd6d0e5 2054 if (!fc->in_ir) {
44c440bc
PP
2055 goto end;
2056 }
fdf0e7a0 2057
312c056a 2058 /* Borrow field */
e98a2d6e 2059 if (stack_empty(notit->stack)) {
312c056a
PP
2060 /* Root: already set by read_dscope_begin_state() */
2061 field = notit->cur_dscope_field;
e98a2d6e 2062 } else {
312c056a 2063 field = borrow_next_field(notit);
44c440bc 2064 BT_ASSERT(field);
e98a2d6e
PP
2065 }
2066
2067 /* Push field */
f6ccaed9 2068 BT_ASSERT(field);
40f4ba76 2069 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
44c440bc
PP
2070 stack_push(notit->stack, field);
2071
2072 /*
5cd6d0e5 2073 * Change BFCR "unsigned int" callback if it's a text
44c440bc
PP
2074 * array/sequence.
2075 */
864cad70
PP
2076 if (fc->type == CTF_FIELD_CLASS_TYPE_ARRAY ||
2077 fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE) {
5cd6d0e5 2078 struct ctf_field_class_array_base *array_fc = (void *) fc;
44c440bc 2079
5cd6d0e5 2080 if (array_fc->is_text) {
44c440bc
PP
2081 int ret;
2082
40f4ba76
PP
2083 BT_ASSERT(bt_field_get_class_type(field) ==
2084 BT_FIELD_CLASS_TYPE_STRING);
44c440bc 2085 notit->done_filling_string = false;
40f4ba76 2086 ret = bt_field_string_clear(field);
44c440bc 2087 BT_ASSERT(ret == 0);
5cd6d0e5
PP
2088 bt_bfcr_set_unsigned_int_cb(notit->bfcr,
2089 bfcr_unsigned_int_char_cb);
44c440bc 2090 }
e98a2d6e
PP
2091 }
2092
2093end:
5cd6d0e5 2094 return BT_BFCR_STATUS_OK;
e98a2d6e
PP
2095}
2096
5cd6d0e5
PP
2097enum bt_bfcr_status bfcr_compound_end_cb(
2098 struct ctf_field_class *fc, void *data)
e98a2d6e 2099{
50842bdc 2100 struct bt_notif_iter *notit = data;
e98a2d6e 2101
5cd6d0e5
PP
2102 BT_LOGV("Compound (end) function called from BFCR: "
2103 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
864cad70
PP
2104 "fc-type=%d, fc-in-ir=%d",
2105 notit, notit->bfcr, fc, fc->type, fc->in_ir);
e98a2d6e 2106
5cd6d0e5 2107 if (!fc->in_ir) {
e98a2d6e
PP
2108 goto end;
2109 }
2110
44c440bc 2111 BT_ASSERT(!stack_empty(notit->stack));
40f4ba76 2112 BT_ASSERT(bt_field_borrow_class_const(stack_top(notit->stack)->base) ==
5cd6d0e5 2113 fc->ir_fc);
e98a2d6e 2114
44c440bc 2115 /*
5cd6d0e5 2116 * Reset BFCR "unsigned int" callback if it's a text
44c440bc
PP
2117 * array/sequence.
2118 */
864cad70
PP
2119 if (fc->type == CTF_FIELD_CLASS_TYPE_ARRAY ||
2120 fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE) {
5cd6d0e5 2121 struct ctf_field_class_array_base *array_fc = (void *) fc;
44c440bc 2122
5cd6d0e5 2123 if (array_fc->is_text) {
864cad70 2124 BT_ASSERT(bt_field_get_class_type(
40f4ba76 2125 stack_top(notit->stack)->base) ==
e5be10ef 2126 BT_FIELD_CLASS_TYPE_STRING);
5cd6d0e5
PP
2127 bt_bfcr_set_unsigned_int_cb(notit->bfcr,
2128 bfcr_unsigned_int_cb);
e98a2d6e 2129 }
44c440bc 2130 }
e98a2d6e 2131
44c440bc
PP
2132 /* Pop stack */
2133 stack_pop(notit->stack);
e98a2d6e 2134
44c440bc
PP
2135 /* If the stack is not empty, increment the base's index */
2136 if (!stack_empty(notit->stack)) {
2137 stack_top(notit->stack)->index++;
e98a2d6e
PP
2138 }
2139
2140end:
5cd6d0e5 2141 return BT_BFCR_STATUS_OK;
e98a2d6e
PP
2142}
2143
2144static
5cd6d0e5 2145int64_t bfcr_get_sequence_length_cb(struct ctf_field_class *fc, void *data)
e98a2d6e 2146{
40f4ba76 2147 struct bt_field *seq_field;
50842bdc 2148 struct bt_notif_iter *notit = data;
5cd6d0e5 2149 struct ctf_field_class_sequence *seq_fc = (void *) fc;
44c440bc
PP
2150 int64_t length = -1;
2151 int ret;
e98a2d6e 2152
44c440bc 2153 length = (uint64_t) g_array_index(notit->stored_values, uint64_t,
5cd6d0e5 2154 seq_fc->stored_length_index);
fdf0e7a0 2155 seq_field = stack_top(notit->stack)->base;
44c440bc 2156 BT_ASSERT(seq_field);
40f4ba76 2157 ret = bt_field_dynamic_array_set_length(seq_field, (uint64_t) length);
44c440bc
PP
2158 if (ret) {
2159 BT_LOGE("Cannot set dynamic array field's length field: "
2160 "notit-addr=%p, field-addr=%p, "
2161 "length=%" PRIu64, notit, seq_field, length);
2cf1d51e 2162 }
fdf0e7a0 2163
44c440bc 2164 return length;
e98a2d6e
PP
2165}
2166
2167static
5cd6d0e5
PP
2168struct ctf_field_class *bfcr_borrow_variant_selected_field_class_cb(
2169 struct ctf_field_class *fc, void *data)
e98a2d6e 2170{
312c056a 2171 int ret;
44c440bc
PP
2172 uint64_t i;
2173 int64_t option_index = -1;
50842bdc 2174 struct bt_notif_iter *notit = data;
5cd6d0e5
PP
2175 struct ctf_field_class_variant *var_fc = (void *) fc;
2176 struct ctf_named_field_class *selected_option = NULL;
2177 struct ctf_field_class *ret_fc = NULL;
44c440bc
PP
2178 union {
2179 uint64_t u;
2180 int64_t i;
2181 } tag;
2182
2183 /* Get variant's tag */
2184 tag.u = g_array_index(notit->stored_values, uint64_t,
5cd6d0e5 2185 var_fc->stored_tag_index);
e98a2d6e
PP
2186
2187 /*
44c440bc 2188 * Check each range to find the selected option's index.
e98a2d6e 2189 */
5cd6d0e5
PP
2190 if (var_fc->tag_fc->base.is_signed) {
2191 for (i = 0; i < var_fc->ranges->len; i++) {
2192 struct ctf_field_class_variant_range *range =
2193 ctf_field_class_variant_borrow_range_by_index(
2194 var_fc, i);
44c440bc
PP
2195
2196 if (tag.i >= range->range.lower.i &&
2197 tag.i <= range->range.upper.i) {
2198 option_index = (int64_t) range->option_index;
2199 break;
2200 }
2201 }
312c056a 2202 } else {
5cd6d0e5
PP
2203 for (i = 0; i < var_fc->ranges->len; i++) {
2204 struct ctf_field_class_variant_range *range =
2205 ctf_field_class_variant_borrow_range_by_index(
2206 var_fc, i);
44c440bc
PP
2207
2208 if (tag.u >= range->range.lower.u &&
2209 tag.u <= range->range.upper.u) {
2210 option_index = (int64_t) range->option_index;
2211 break;
2212 }
2213 }
312c056a
PP
2214 }
2215
44c440bc 2216 if (option_index < 0) {
5cd6d0e5
PP
2217 BT_LOGW("Cannot find variant field class's option: "
2218 "notit-addr=%p, var-fc-addr=%p, u-tag=%" PRIu64 ", "
2219 "i-tag=%" PRId64, notit, var_fc, tag.u, tag.i);
e98a2d6e
PP
2220 goto end;
2221 }
2222
5cd6d0e5
PP
2223 selected_option = ctf_field_class_variant_borrow_option_by_index(
2224 var_fc, (uint64_t) option_index);
e98a2d6e 2225
5cd6d0e5 2226 if (selected_option->fc->in_ir) {
40f4ba76 2227 struct bt_field *var_field = stack_top(notit->stack)->base;
1556a1af 2228
40f4ba76 2229 ret = bt_field_variant_select_option_field(
e5be10ef 2230 var_field, option_index);
e22b45d0 2231 if (ret) {
44c440bc
PP
2232 BT_LOGW("Cannot select variant field's option field: "
2233 "notit-addr=%p, var-field-addr=%p, "
2234 "opt-index=%" PRId64, notit, var_field,
2235 option_index);
1556a1af
JG
2236 goto end;
2237 }
af87daef
PP
2238 }
2239
5cd6d0e5 2240 ret_fc = selected_option->fc;
e22b45d0 2241
af87daef 2242end:
5cd6d0e5 2243 return ret_fc;
44c440bc
PP
2244}
2245
2246static
2247void set_event_default_clock_value(struct bt_notif_iter *notit)
2248{
40f4ba76 2249 struct bt_event *event =
0d72b8c3 2250 bt_notification_event_borrow_event(
e5be10ef 2251 notit->event_notif);
40f4ba76 2252 struct bt_stream_class *sc = notit->meta.sc->ir_sc;
44c440bc
PP
2253
2254 BT_ASSERT(event);
2255
2256 if (bt_stream_class_borrow_default_clock_class(sc)) {
40f4ba76 2257 bt_event_set_default_clock_value(event,
44c440bc 2258 notit->default_clock_val);
44c440bc 2259 }
af87daef
PP
2260}
2261
f42867e2
PP
2262static
2263void notify_new_stream(struct bt_notif_iter *notit,
0d72b8c3 2264 struct bt_notification **notification)
f42867e2 2265{
312c056a 2266 enum bt_notif_iter_status status;
0d72b8c3 2267 struct bt_notification *ret = NULL;
f42867e2 2268
312c056a
PP
2269 status = set_current_stream(notit);
2270 if (status != BT_NOTIF_ITER_STATUS_OK) {
65300d60 2271 BT_OBJECT_PUT_REF_AND_RESET(ret);
f42867e2
PP
2272 goto end;
2273 }
2274
2275 BT_ASSERT(notit->stream);
f0010051 2276 BT_ASSERT(notit->notif_iter);
bb5bb160 2277 ret = bt_notification_stream_beginning_create(notit->notif_iter,
f0010051 2278 notit->stream);
f42867e2
PP
2279 if (!ret) {
2280 BT_LOGE("Cannot create stream beginning notification: "
2281 "notit-addr=%p, stream-addr=%p",
2282 notit, notit->stream);
2283 return;
2284 }
2285
2286end:
2287 *notification = ret;
2288}
2289
2290static
2291void notify_end_of_stream(struct bt_notif_iter *notit,
0d72b8c3 2292 struct bt_notification **notification)
f42867e2 2293{
0d72b8c3 2294 struct bt_notification *ret;
f42867e2
PP
2295
2296 if (!notit->stream) {
2297 BT_LOGE("Cannot create stream for stream notification: "
2298 "notit-addr=%p", notit);
2299 return;
2300 }
2301
f0010051 2302 BT_ASSERT(notit->notif_iter);
0d72b8c3 2303 ret = bt_notification_stream_end_create(notit->notif_iter,
f0010051 2304 notit->stream);
f42867e2
PP
2305 if (!ret) {
2306 BT_LOGE("Cannot create stream beginning notification: "
2307 "notit-addr=%p, stream-addr=%p",
2308 notit, notit->stream);
2309 return;
2310 }
2311 *notification = ret;
2312}
2313
78586d8a 2314static
50842bdc 2315void notify_new_packet(struct bt_notif_iter *notit,
0d72b8c3 2316 struct bt_notification **notification)
e98a2d6e 2317{
312c056a
PP
2318 int ret;
2319 enum bt_notif_iter_status status;
0d72b8c3 2320 struct bt_notification *notif = NULL;
40f4ba76 2321 const struct bt_stream_class *sc;
e98a2d6e 2322
312c056a
PP
2323 status = set_current_packet(notit);
2324 if (status != BT_NOTIF_ITER_STATUS_OK) {
2325 goto end;
e98a2d6e
PP
2326 }
2327
312c056a 2328 BT_ASSERT(notit->packet);
40f4ba76 2329 sc = notit->meta.sc->ir_sc;
44c440bc
PP
2330 BT_ASSERT(sc);
2331
2332 if (bt_stream_class_packets_have_discarded_event_counter_snapshot(sc)) {
2333 BT_ASSERT(notit->snapshots.discarded_events != UINT64_C(-1));
40f4ba76 2334 bt_packet_set_discarded_event_counter_snapshot(
44c440bc 2335 notit->packet, notit->snapshots.discarded_events);
44c440bc
PP
2336 }
2337
2338 if (bt_stream_class_packets_have_packet_counter_snapshot(sc)) {
2339 BT_ASSERT(notit->snapshots.packets != UINT64_C(-1));
40f4ba76 2340 bt_packet_set_packet_counter_snapshot(
44c440bc 2341 notit->packet, notit->snapshots.packets);
44c440bc
PP
2342 }
2343
2344 if (bt_stream_class_packets_have_default_beginning_clock_value(sc)) {
2345 BT_ASSERT(notit->snapshots.beginning_clock != UINT64_C(-1));
40f4ba76 2346 bt_packet_set_default_beginning_clock_value(
44c440bc 2347 notit->packet, notit->snapshots.beginning_clock);
44c440bc
PP
2348 }
2349
2350 if (bt_stream_class_packets_have_default_end_clock_value(sc)) {
2351 BT_ASSERT(notit->snapshots.end_clock != UINT64_C(-1));
40f4ba76 2352 bt_packet_set_default_end_clock_value(
44c440bc 2353 notit->packet, notit->snapshots.end_clock);
44c440bc 2354 }
312c056a
PP
2355
2356 if (notit->packet_header_field) {
40f4ba76 2357 ret = bt_packet_move_header_field(
e5be10ef 2358 notit->packet, notit->packet_header_field);
312c056a
PP
2359 if (ret) {
2360 goto end;
2361 }
2362
2363 notit->packet_header_field = NULL;
2364
2365 /*
2366 * At this point notit->dscopes.trace_packet_header has
2367 * the same value as the packet header field within
2368 * notit->packet.
2369 */
40f4ba76 2370 BT_ASSERT(bt_packet_borrow_header_field(
e5be10ef 2371 notit->packet) ==
312c056a
PP
2372 notit->dscopes.trace_packet_header);
2373 }
2374
2375 if (notit->packet_context_field) {
40f4ba76 2376 ret = bt_packet_move_context_field(
e5be10ef 2377 notit->packet, notit->packet_context_field);
312c056a
PP
2378 if (ret) {
2379 goto end;
2380 }
2381
2382 notit->packet_context_field = NULL;
2383
2384 /*
2385 * At this point notit->dscopes.trace_packet_header has
2386 * the same value as the packet header field within
2387 * notit->packet.
2388 */
40f4ba76 2389 BT_ASSERT(bt_packet_borrow_context_field(
e5be10ef 2390 notit->packet) ==
312c056a
PP
2391 notit->dscopes.stream_packet_context);
2392 }
2393
f0010051 2394 BT_ASSERT(notit->notif_iter);
bb5bb160 2395 notif = bt_notification_packet_beginning_create(notit->notif_iter,
5c563278 2396 notit->packet);
312c056a 2397 if (!notif) {
fdf0e7a0
PP
2398 BT_LOGE("Cannot create packet beginning notification: "
2399 "notit-addr=%p, packet-addr=%p",
2400 notit, notit->packet);
e22b45d0 2401 goto end;
78586d8a 2402 }
312c056a 2403
312c056a 2404 *notification = notif;
e22b45d0
PP
2405
2406end:
2407 return;
e98a2d6e
PP
2408}
2409
78586d8a 2410static
50842bdc 2411void notify_end_of_packet(struct bt_notif_iter *notit,
0d72b8c3 2412 struct bt_notification **notification)
e98a2d6e 2413{
0d72b8c3 2414 struct bt_notification *notif;
e98a2d6e 2415
e98a2d6e 2416 if (!notit->packet) {
78586d8a 2417 return;
e98a2d6e
PP
2418 }
2419
44c440bc
PP
2420 /* Update default clock from packet's end time */
2421 if (notit->snapshots.end_clock != UINT64_C(-1)) {
2422 notit->default_clock_val = notit->snapshots.end_clock;
2423 }
2424
f0010051 2425 BT_ASSERT(notit->notif_iter);
0d72b8c3 2426 notif = bt_notification_packet_end_create(notit->notif_iter,
f0010051 2427 notit->packet);
312c056a 2428 if (!notif) {
fdf0e7a0
PP
2429 BT_LOGE("Cannot create packet end notification: "
2430 "notit-addr=%p, packet-addr=%p",
2431 notit, notit->packet);
78586d8a 2432 return;
ccf82993 2433
78586d8a 2434 }
e98a2d6e 2435
65300d60 2436 BT_OBJECT_PUT_REF_AND_RESET(notit->packet);
312c056a 2437 *notification = notif;
e98a2d6e
PP
2438}
2439
c44c3e70 2440BT_HIDDEN
44c440bc 2441struct bt_notif_iter *bt_notif_iter_create(struct ctf_trace_class *tc,
e98a2d6e 2442 size_t max_request_sz,
50842bdc 2443 struct bt_notif_iter_medium_ops medops, void *data)
e98a2d6e 2444{
50842bdc 2445 struct bt_notif_iter *notit = NULL;
5cd6d0e5
PP
2446 struct bt_bfcr_cbs cbs = {
2447 .classes = {
2448 .signed_int = bfcr_signed_int_cb,
2449 .unsigned_int = bfcr_unsigned_int_cb,
2450 .floating_point = bfcr_floating_point_cb,
2451 .string_begin = bfcr_string_begin_cb,
2452 .string = bfcr_string_cb,
2453 .string_end = bfcr_string_end_cb,
2454 .compound_begin = bfcr_compound_begin_cb,
2455 .compound_end = bfcr_compound_end_cb,
e98a2d6e
PP
2456 },
2457 .query = {
5cd6d0e5
PP
2458 .get_sequence_length = bfcr_get_sequence_length_cb,
2459 .borrow_variant_selected_field_class = bfcr_borrow_variant_selected_field_class_cb,
e98a2d6e
PP
2460 },
2461 };
2462
44c440bc 2463 BT_ASSERT(tc);
f6ccaed9 2464 BT_ASSERT(medops.request_bytes);
312c056a 2465 BT_ASSERT(medops.borrow_stream);
fdf0e7a0 2466 BT_LOGD("Creating CTF plugin notification iterator: "
862ca4ed
PP
2467 "trace-addr=%p, max-request-size=%zu, "
2468 "data=%p", tc, max_request_sz, data);
50842bdc 2469 notit = g_new0(struct bt_notif_iter, 1);
e98a2d6e 2470 if (!notit) {
fdf0e7a0 2471 BT_LOGE_STR("Failed to allocate one CTF plugin notification iterator.");
e98a2d6e
PP
2472 goto end;
2473 }
44c440bc 2474 notit->meta.tc = tc;
e98a2d6e
PP
2475 notit->medium.medops = medops;
2476 notit->medium.max_request_sz = max_request_sz;
2477 notit->medium.data = data;
e98a2d6e 2478 notit->stack = stack_new(notit);
44c440bc
PP
2479 notit->stored_values = g_array_new(FALSE, TRUE, sizeof(uint64_t));
2480 g_array_set_size(notit->stored_values, tc->stored_value_count);
2481
e98a2d6e 2482 if (!notit->stack) {
fdf0e7a0 2483 BT_LOGE_STR("Failed to create field stack.");
c44c3e70 2484 goto error;
e98a2d6e
PP
2485 }
2486
5cd6d0e5
PP
2487 notit->bfcr = bt_bfcr_create(cbs, notit);
2488 if (!notit->bfcr) {
2489 BT_LOGE_STR("Failed to create binary class reader (BFCR).");
c44c3e70 2490 goto error;
e98a2d6e
PP
2491 }
2492
50842bdc 2493 bt_notif_iter_reset(notit);
fdf0e7a0 2494 BT_LOGD("Created CTF plugin notification iterator: "
862ca4ed 2495 "trace-addr=%p, max-request-size=%zu, "
fdf0e7a0 2496 "data=%p, notit-addr=%p",
862ca4ed 2497 tc, max_request_sz, data, notit);
9e0c8dbb 2498 notit->cur_packet_offset = 0;
fdf0e7a0 2499
e98a2d6e
PP
2500end:
2501 return notit;
fdf0e7a0 2502
c44c3e70 2503error:
50842bdc 2504 bt_notif_iter_destroy(notit);
c44c3e70
JG
2505 notit = NULL;
2506 goto end;
e98a2d6e
PP
2507}
2508
50842bdc 2509void bt_notif_iter_destroy(struct bt_notif_iter *notit)
e98a2d6e 2510{
65300d60
PP
2511 BT_OBJECT_PUT_REF_AND_RESET(notit->packet);
2512 BT_OBJECT_PUT_REF_AND_RESET(notit->stream);
312c056a 2513 release_all_dscopes(notit);
e98a2d6e 2514
fdf0e7a0
PP
2515 BT_LOGD("Destroying CTF plugin notification iterator: addr=%p", notit);
2516
e98a2d6e 2517 if (notit->stack) {
fdf0e7a0 2518 BT_LOGD_STR("Destroying field stack.");
e98a2d6e
PP
2519 stack_destroy(notit->stack);
2520 }
2521
5cd6d0e5
PP
2522 if (notit->bfcr) {
2523 BT_LOGD("Destroying BFCR: bfcr-addr=%p", notit->bfcr);
2524 bt_bfcr_destroy(notit->bfcr);
e98a2d6e
PP
2525 }
2526
44c440bc
PP
2527 if (notit->stored_values) {
2528 g_array_free(notit->stored_values, TRUE);
5f870343 2529 }
fdf0e7a0 2530
e98a2d6e
PP
2531 g_free(notit);
2532}
2533
50842bdc
PP
2534enum bt_notif_iter_status bt_notif_iter_get_next_notification(
2535 struct bt_notif_iter *notit,
d94d92ac 2536 struct bt_self_notification_iterator *notif_iter,
0d72b8c3 2537 struct bt_notification **notification)
e98a2d6e 2538{
50842bdc 2539 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
e98a2d6e 2540
f6ccaed9
PP
2541 BT_ASSERT(notit);
2542 BT_ASSERT(notification);
e98a2d6e 2543
5c563278
PP
2544 if (notit->state == STATE_DONE) {
2545 status = BT_NOTIF_ITER_STATUS_EOF;
2546 goto end;
2547 }
2548
f0010051 2549 notit->notif_iter = notif_iter;
f42867e2 2550
e22b45d0 2551 BT_LOGV("Getting next notification: notit-addr=%p", notit);
fdf0e7a0 2552
e98a2d6e
PP
2553 while (true) {
2554 status = handle_state(notit);
50842bdc
PP
2555 if (status == BT_NOTIF_ITER_STATUS_AGAIN) {
2556 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_AGAIN.");
7cdc2bab
MD
2557 goto end;
2558 }
312c056a 2559
50842bdc
PP
2560 if (status != BT_NOTIF_ITER_STATUS_OK) {
2561 if (status == BT_NOTIF_ITER_STATUS_EOF) {
f42867e2
PP
2562 enum state next_state = notit->state;
2563
50842bdc 2564 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_EOF.");
f42867e2
PP
2565
2566 if (notit->packet) {
312c056a
PP
2567 notify_end_of_packet(notit,
2568 notification);
f42867e2 2569 } else {
312c056a
PP
2570 notify_end_of_stream(notit,
2571 notification);
f42867e2
PP
2572 next_state = STATE_DONE;
2573 }
2574
2575 if (!*notification) {
2576 status = BT_NOTIF_ITER_STATUS_ERROR;
2577 goto end;
2578 }
2579
2580 status = BT_NOTIF_ITER_STATUS_OK;
2581 notit->state = next_state;
e98a2d6e 2582 } else {
fdf0e7a0
PP
2583 BT_LOGW("Cannot handle state: "
2584 "notit-addr=%p, state=%s",
2585 notit, state_string(notit->state));
e98a2d6e 2586 }
312c056a 2587
e98a2d6e
PP
2588 goto end;
2589 }
2590
2591 switch (notit->state) {
f42867e2
PP
2592 case STATE_EMIT_NOTIF_NEW_STREAM:
2593 /* notify_new_stream() logs errors */
2594 notify_new_stream(notit, notification);
44c440bc 2595
f42867e2
PP
2596 if (!*notification) {
2597 status = BT_NOTIF_ITER_STATUS_ERROR;
2598 }
44c440bc 2599
f42867e2
PP
2600 notit->stream_begin_emitted = true;
2601 goto end;
e98a2d6e 2602 case STATE_EMIT_NOTIF_NEW_PACKET:
fdf0e7a0 2603 /* notify_new_packet() logs errors */
e98a2d6e 2604 notify_new_packet(notit, notification);
44c440bc 2605
e98a2d6e 2606 if (!*notification) {
50842bdc 2607 status = BT_NOTIF_ITER_STATUS_ERROR;
e98a2d6e 2608 }
312c056a 2609
e98a2d6e
PP
2610 goto end;
2611 case STATE_EMIT_NOTIF_EVENT:
312c056a 2612 BT_ASSERT(notit->event_notif);
44c440bc 2613 set_event_default_clock_value(notit);
d4393e08
PP
2614 *notification = notit->event_notif;
2615 notit->event_notif = NULL;
e98a2d6e
PP
2616 goto end;
2617 case STATE_EMIT_NOTIF_END_OF_PACKET:
fdf0e7a0 2618 /* notify_end_of_packet() logs errors */
e98a2d6e 2619 notify_end_of_packet(notit, notification);
44c440bc 2620
e98a2d6e 2621 if (!*notification) {
50842bdc 2622 status = BT_NOTIF_ITER_STATUS_ERROR;
e98a2d6e 2623 }
312c056a 2624
e98a2d6e
PP
2625 goto end;
2626 default:
2627 /* Non-emitting state: continue */
2628 break;
2629 }
2630 }
2631
2632end:
2633 return status;
2634}
87187cbf
PP
2635
2636BT_HIDDEN
312c056a 2637enum bt_notif_iter_status bt_notif_iter_borrow_packet_header_context_fields(
50842bdc 2638 struct bt_notif_iter *notit,
40f4ba76
PP
2639 struct bt_field **packet_header_field,
2640 struct bt_field **packet_context_field)
87187cbf 2641{
9e0c8dbb 2642 int ret;
50842bdc 2643 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
87187cbf 2644
f6ccaed9 2645 BT_ASSERT(notit);
87187cbf
PP
2646
2647 if (notit->state == STATE_EMIT_NOTIF_NEW_PACKET) {
2648 /* We're already there */
2649 goto set_fields;
2650 }
2651
2652 while (true) {
2653 status = handle_state(notit);
50842bdc
PP
2654 if (status == BT_NOTIF_ITER_STATUS_AGAIN) {
2655 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_AGAIN.");
87187cbf
PP
2656 goto end;
2657 }
50842bdc
PP
2658 if (status != BT_NOTIF_ITER_STATUS_OK) {
2659 if (status == BT_NOTIF_ITER_STATUS_EOF) {
2660 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_EOF.");
87187cbf 2661 } else {
fdf0e7a0
PP
2662 BT_LOGW("Cannot handle state: "
2663 "notit-addr=%p, state=%s",
2664 notit, state_string(notit->state));
87187cbf
PP
2665 }
2666 goto end;
2667 }
2668
2669 switch (notit->state) {
2670 case STATE_EMIT_NOTIF_NEW_PACKET:
2671 /*
2672 * Packet header and context fields are
2673 * potentially decoded (or they don't exist).
2674 */
2675 goto set_fields;
2676 case STATE_INIT:
f42867e2 2677 case STATE_EMIT_NOTIF_NEW_STREAM:
87187cbf
PP
2678 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
2679 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
2680 case STATE_AFTER_TRACE_PACKET_HEADER:
2681 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN:
2682 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE:
2683 case STATE_AFTER_STREAM_PACKET_CONTEXT:
2684 /* Non-emitting state: continue */
2685 break;
2686 default:
2687 /*
2688 * We should never get past the
2689 * STATE_EMIT_NOTIF_NEW_PACKET state.
2690 */
fdf0e7a0
PP
2691 BT_LOGF("Unexpected state: notit-addr=%p, state=%s",
2692 notit, state_string(notit->state));
0fbb9a9f 2693 abort();
87187cbf
PP
2694 }
2695 }
2696
2697set_fields:
312c056a
PP
2698 ret = set_current_packet_content_sizes(notit);
2699 if (ret) {
2700 status = BT_NOTIF_ITER_STATUS_ERROR;
2701 goto end;
2702 }
2703
87187cbf 2704 if (packet_header_field) {
312c056a 2705 *packet_header_field = notit->dscopes.trace_packet_header;
87187cbf
PP
2706 }
2707
2708 if (packet_context_field) {
312c056a 2709 *packet_context_field = notit->dscopes.stream_packet_context;
87187cbf
PP
2710 }
2711
2712end:
2713 return status;
2714}
6de92955
PP
2715
2716BT_HIDDEN
50842bdc 2717void bt_notif_iter_set_medops_data(struct bt_notif_iter *notit,
6de92955
PP
2718 void *medops_data)
2719{
f6ccaed9 2720 BT_ASSERT(notit);
6de92955
PP
2721 notit->medium.data = medops_data;
2722}
9e0c8dbb
JG
2723
2724BT_HIDDEN
50842bdc
PP
2725enum bt_notif_iter_status bt_notif_iter_seek(
2726 struct bt_notif_iter *notit, off_t offset)
9e0c8dbb 2727{
50842bdc
PP
2728 enum bt_notif_iter_status ret = BT_NOTIF_ITER_STATUS_OK;
2729 enum bt_notif_iter_medium_status medium_status;
9e0c8dbb 2730
f6ccaed9 2731 BT_ASSERT(notit);
9e0c8dbb
JG
2732 if (offset < 0) {
2733 BT_LOGE("Cannot seek to negative offset: offset=%jd", offset);
50842bdc 2734 ret = BT_NOTIF_ITER_STATUS_INVAL;
9e0c8dbb
JG
2735 goto end;
2736 }
2737
2738 if (!notit->medium.medops.seek) {
50842bdc 2739 ret = BT_NOTIF_ITER_STATUS_UNSUPPORTED;
9e0c8dbb
JG
2740 BT_LOGD("Aborting seek as the iterator's underlying media does not implement seek support.");
2741 goto end;
2742 }
2743
2744 medium_status = notit->medium.medops.seek(
44c440bc 2745 BT_NOTIF_ITER_SEEK_WHENCE_SET, offset, notit->medium.data);
50842bdc
PP
2746 if (medium_status != BT_NOTIF_ITER_MEDIUM_STATUS_OK) {
2747 if (medium_status == BT_NOTIF_ITER_MEDIUM_STATUS_EOF) {
2748 ret = BT_NOTIF_ITER_STATUS_EOF;
9e0c8dbb 2749 } else {
50842bdc 2750 ret = BT_NOTIF_ITER_STATUS_ERROR;
9e0c8dbb
JG
2751 goto end;
2752 }
2753 }
2754
50842bdc 2755 bt_notif_iter_reset(notit);
9e0c8dbb 2756 notit->cur_packet_offset = offset;
44c440bc 2757
9e0c8dbb
JG
2758end:
2759 return ret;
2760}
2761
2762BT_HIDDEN
44c440bc 2763off_t bt_notif_iter_get_current_packet_offset(struct bt_notif_iter *notit)
9e0c8dbb 2764{
f6ccaed9 2765 BT_ASSERT(notit);
9e0c8dbb
JG
2766 return notit->cur_packet_offset;
2767}
2768
2769BT_HIDDEN
50842bdc
PP
2770off_t bt_notif_iter_get_current_packet_size(
2771 struct bt_notif_iter *notit)
9e0c8dbb 2772{
f6ccaed9 2773 BT_ASSERT(notit);
44c440bc
PP
2774 return notit->cur_exp_packet_total_size;
2775}
2776
2777BT_HIDDEN
2778void bt_notif_trace_class_changed(struct bt_notif_iter *notit)
2779{
2780 if (notit->meta.tc->stored_value_count > notit->stored_values->len) {
2781 g_array_set_size(notit->stored_values,
2782 notit->meta.tc->stored_value_count);
2783 }
2784}
2785
2786BT_HIDDEN
2787enum bt_notif_iter_status bt_notif_iter_get_packet_properties(
2788 struct bt_notif_iter *notit,
2789 struct bt_notif_iter_packet_properties *props)
2790{
2791 BT_ASSERT(notit);
2792 BT_ASSERT(props);
2793
2794 props->exp_packet_total_size =
2795 (uint64_t) notit->cur_exp_packet_total_size;
2796 props->exp_packet_content_size =
2797 (uint64_t) notit->cur_exp_packet_content_size;
2798 BT_ASSERT(props->stream_class_id >= 0);
2799 props->stream_class_id = (uint64_t) notit->cur_stream_class_id;
2800 props->data_stream_id = notit->cur_data_stream_id;
2801 props->snapshots.discarded_events = notit->snapshots.discarded_events;
2802 props->snapshots.packets = notit->snapshots.packets;
2803 props->snapshots.beginning_clock = notit->snapshots.beginning_clock;
2804 props->snapshots.end_clock = notit->snapshots.end_clock;
2805 return BT_NOTIF_ITER_STATUS_OK;
9e0c8dbb 2806}
This page took 0.255717 seconds and 4 git commands to generate.