lib: make public reference count functions have strict types
[babeltrace.git] / plugins / ctf / common / notif-iter / notif-iter.c
CommitLineData
e98a2d6e
PP
1/*
2 * Babeltrace - CTF notification iterator
06a626b8 3 *
7b33a0e0
PP
4 * Copyright (c) 2015-2018 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2015-2018 Philippe Proulx <pproulx@efficios.com>
e98a2d6e
PP
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
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>
8b45963b 34#include <babeltrace/assert-internal.h>
e98a2d6e 35#include <string.h>
e0831b38 36#include <babeltrace/babeltrace.h>
8deee039 37#include <babeltrace/common-internal.h>
e98a2d6e 38#include <glib.h>
0fbb9a9f 39#include <stdlib.h>
e98a2d6e 40
06a626b8 41#include "notif-iter.h"
939190b3 42#include "../bfcr/bfcr.h"
e98a2d6e 43
839d52a5 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 *
a6918753 57 * Field is borrowed.
e98a2d6e 58 */
78cf9df6 59 struct bt_field *base;
e98a2d6e 60
7b33a0e0 61 /* Index of next field to set */
e98a2d6e
PP
62 size_t index;
63};
64
65/* Visit stack */
66struct stack {
87bdbbb0
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,
6ff151ad 83 STATE_EMIT_NOTIF_NEW_STREAM,
e98a2d6e 84 STATE_EMIT_NOTIF_NEW_PACKET,
7b33a0e0
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,
6ff151ad 96 STATE_DONE,
e98a2d6e
PP
97 STATE_SKIP_PACKET_PADDING,
98};
99
100/* CTF notification iterator */
839d52a5 101struct bt_notif_iter {
e98a2d6e
PP
102 /* Visit stack */
103 struct stack *stack;
104
03e18d5d 105 /* Current notification iterator to create notifications (weak) */
834e9996 106 struct bt_self_notification_iterator *notif_iter;
f7c3ac09 107
e98a2d6e
PP
108 /*
109 * Current dynamic scope field pointer.
110 *
a6918753
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 */
78cf9df6 114 struct bt_field *cur_dscope_field;
e98a2d6e 115
7b33a0e0
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 {
7b33a0e0
PP
124 struct ctf_trace_class *tc;
125 struct ctf_stream_class *sc;
126 struct ctf_event_class *ec;
e98a2d6e
PP
127 } meta;
128
a6918753 129 /* Current packet header field wrapper (NULL if not created yet) */
78cf9df6 130 struct bt_packet_header_field *packet_header_field;
a6918753
PP
131
132 /* Current packet header field wrapper (NULL if not created yet) */
78cf9df6 133 struct bt_packet_context_field *packet_context_field;
a6918753
PP
134
135 /* Current event header field (NULL if not created yet) */
78cf9df6 136 struct bt_event_header_field *event_header_field;
a6918753 137
e98a2d6e 138 /* Current packet (NULL if not created yet) */
78cf9df6 139 struct bt_packet *packet;
e98a2d6e 140
5184a1d6 141 /* Current stream (NULL if not set yet) */
78cf9df6 142 struct bt_stream *stream;
5184a1d6 143
a6918753 144 /* Current event (NULL if not created yet) */
78cf9df6 145 struct bt_event *event;
a6918753
PP
146
147 /* Current event notification (NULL if not created yet) */
7b53201c 148 struct bt_notification *event_notif;
a6918753 149
7b33a0e0 150 /* Database of current dynamic scopes */
e98a2d6e 151 struct {
78cf9df6
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;
9b876b7b
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 */
939190b3 182 struct bt_bfcr *bfcr;
e98a2d6e 183
2cf1d51e 184 /* Current medium data */
e98a2d6e 185 struct {
839d52a5 186 struct bt_notif_iter_medium_ops medops;
e98a2d6e
PP
187 size_t max_request_sz;
188 void *data;
189 } medium;
190
6ff151ad
PP
191 /* Stream beginning was emitted */
192 bool stream_begin_emitted;
193
e98a2d6e 194 /* Current packet size (bits) (-1 if unknown) */
7b33a0e0 195 int64_t cur_exp_packet_total_size;
e98a2d6e
PP
196
197 /* Current content size (bits) (-1 if unknown) */
7b33a0e0 198 int64_t cur_exp_packet_content_size;
c44c3e70 199
7b33a0e0
PP
200 /* Current stream class ID */
201 int64_t cur_stream_class_id;
bb230c9b 202
7b33a0e0
PP
203 /* Current event class ID */
204 int64_t cur_event_class_id;
5f870343 205
7b33a0e0
PP
206 /* Current data stream ID */
207 int64_t cur_data_stream_id;
5f870343
JG
208
209 /*
7b33a0e0
PP
210 * Offset, in the underlying media, of the current packet's
211 * start (-1 if unknown).
5f870343 212 */
7b33a0e0
PP
213 off_t cur_packet_offset;
214
215 /* Default clock's current value */
216 uint64_t default_clock_val;
5f870343 217
7b33a0e0
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";
6ff151ad
PP
250 case STATE_EMIT_NOTIF_NEW_STREAM:
251 return "STATE_EMIT_NOTIF_NEW_STREAM";
7b33a0e0
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";
6ff151ad
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
839d52a5 284int bt_notif_iter_switch_packet(struct bt_notif_iter *notit);
2cf1d51e 285
e98a2d6e 286static
839d52a5 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
87bdbbb0 297 stack->entries = g_array_new(FALSE, TRUE, sizeof(struct stack_entry));
e98a2d6e 298 if (!stack->entries) {
87bdbbb0 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);
7b33a0e0 304 goto end;
fdf0e7a0 305
e98a2d6e
PP
306error:
307 g_free(stack);
7b33a0e0
PP
308 stack = NULL;
309
310end:
311 return stack;
e98a2d6e
PP
312}
313
314static
315void stack_destroy(struct stack *stack)
316{
8b45963b 317 BT_ASSERT(stack);
fdf0e7a0 318 BT_LOGD("Destroying stack: addr=%p", stack);
87bdbbb0
PP
319
320 if (stack->entries) {
321 g_array_free(stack->entries, TRUE);
322 }
323
e98a2d6e
PP
324 g_free(stack);
325}
326
327static
78cf9df6 328void stack_push(struct stack *stack, struct bt_field *base)
e98a2d6e 329{
e98a2d6e 330 struct stack_entry *entry;
e98a2d6e 331
8b45963b
PP
332 BT_ASSERT(stack);
333 BT_ASSERT(base);
fdf0e7a0 334 BT_LOGV("Pushing base field on stack: stack-addr=%p, "
87bdbbb0
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
87bdbbb0 342 entry = &g_array_index(stack->entries, struct stack_entry, stack->size);
a6918753 343 entry->base = base;
87bdbbb0
PP
344 entry->index = 0;
345 stack->size++;
e98a2d6e
PP
346}
347
348static inline
349unsigned int stack_size(struct stack *stack)
350{
8b45963b 351 BT_ASSERT(stack);
87bdbbb0 352 return stack->size;
e98a2d6e
PP
353}
354
355static
356void stack_pop(struct stack *stack)
357{
8b45963b
PP
358 BT_ASSERT(stack);
359 BT_ASSERT(stack_size(stack));
fdf0e7a0 360 BT_LOGV("Popping from stack: "
87bdbbb0
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{
8b45963b
PP
369 BT_ASSERT(stack);
370 BT_ASSERT(stack_size(stack));
87bdbbb0
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{
8b45963b 384 BT_ASSERT(stack);
87bdbbb0 385 stack->size = 0;
e98a2d6e
PP
386}
387
388static inline
839d52a5
PP
389enum bt_notif_iter_status notif_iter_status_from_m_status(
390 enum bt_notif_iter_medium_status m_status)
e98a2d6e 391{
7b33a0e0 392 /* They are the same */
dc77b521 393 return (int) m_status;
e98a2d6e
PP
394}
395
396static inline
839d52a5 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
839d52a5 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
839d52a5 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
839d52a5 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
839d52a5
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;
839d52a5 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",
839d52a5 436 bt_notif_iter_medium_status_string(m_status),
fdf0e7a0 437 buffer_addr, buffer_sz);
839d52a5 438 if (m_status == BT_NOTIF_ITER_MEDIUM_STATUS_OK) {
8b45963b 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;
bb8744a7 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);
839d52a5 460 } else if (m_status == BT_NOTIF_ITER_MEDIUM_STATUS_EOF) {
7116f6c0
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 */
7b33a0e0
PP
466 if (notit->cur_exp_packet_total_size >= 0) {
467 if (packet_at(notit) ==
468 notit->cur_exp_packet_total_size) {
42ef5ec2 469 goto end;
7116f6c0 470 }
42ef5ec2
PP
471 } else {
472 if (packet_at(notit) == 0) {
473 goto end;
7116f6c0 474 }
7116f6c0 475
42ef5ec2
PP
476 if (notit->buf.last_eh_at != SIZE_MAX &&
477 notit->buf.at == notit->buf.last_eh_at) {
478 goto end;
7116f6c0
PP
479 }
480 }
481
7116f6c0
PP
482 /* All other states are invalid */
483 BT_LOGW("User function returned %s, but notification iterator is in an unexpected state: "
42ef5ec2
PP
484 "state=%s, cur-packet-size=%" PRId64 ", cur=%zu, "
485 "packet-cur=%zu, last-eh-at=%zu",
839d52a5 486 bt_notif_iter_medium_status_string(m_status),
42ef5ec2 487 state_string(notit->state),
7b33a0e0 488 notit->cur_exp_packet_total_size,
42ef5ec2
PP
489 notit->buf.at, packet_at(notit),
490 notit->buf.last_eh_at);
839d52a5 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",
839d52a5 494 bt_notif_iter_medium_status_string(m_status));
e98a2d6e
PP
495 }
496
42ef5ec2 497end:
e98a2d6e
PP
498 return notif_iter_status_from_m_status(m_status);
499}
500
501static inline
839d52a5
PP
502enum bt_notif_iter_status buf_ensure_available_bits(
503 struct bt_notif_iter *notit)
e98a2d6e 504{
839d52a5 505 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
e98a2d6e 506
7b33a0e0 507 if (unlikely(buf_available_bits(notit) == 0)) {
e98a2d6e 508 /*
839d52a5 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
839d52a5
PP
519enum bt_notif_iter_status read_dscope_begin_state(
520 struct bt_notif_iter *notit,
939190b3 521 struct ctf_field_class *dscope_fc,
e98a2d6e 522 enum state done_state, enum state continue_state,
78cf9df6 523 struct bt_field *dscope_field)
e98a2d6e 524{
839d52a5 525 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
939190b3 526 enum bt_bfcr_status bfcr_status;
e98a2d6e
PP
527 size_t consumed_bits;
528
e98a2d6e 529 notit->cur_dscope_field = dscope_field;
939190b3
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),
939190b3
PP
534 notit->buf.sz, &bfcr_status);
535 BT_LOGV("BFCR consumed bits: size=%zu", consumed_bits);
e98a2d6e 536
939190b3
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;
939190b3
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:
939190b3
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));
839d52a5 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
839d52a5
PP
563enum bt_notif_iter_status read_dscope_continue_state(
564 struct bt_notif_iter *notit, enum state done_state)
e98a2d6e 565{
839d52a5 566 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
939190b3 567 enum bt_bfcr_status bfcr_status;
e98a2d6e
PP
568 size_t consumed_bits;
569
939190b3
PP
570 BT_LOGV("Continuing BFCR: notit-addr=%p, bfcr-addr=%p",
571 notit, notit->bfcr);
42ef5ec2 572
e98a2d6e 573 status = buf_ensure_available_bits(notit);
839d52a5 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",
839d52a5 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",
839d52a5 582 notit, bt_notif_iter_status_string(status));
fdf0e7a0
PP
583 }
584
e98a2d6e
PP
585 goto end;
586 }
587
939190b3
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
939190b3
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;
939190b3 598 case BT_BFCR_STATUS_EOF:
78586d8a 599 /* Stay in this continue state. */
939190b3 600 BT_LOGV_STR("BFCR needs more data to decode field completely.");
e98a2d6e
PP
601 break;
602 default:
939190b3
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));
839d52a5 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
a6918753 617void release_event_dscopes(struct bt_notif_iter *notit)
e98a2d6e 618{
7b33a0e0 619 notit->dscopes.event_header = NULL;
a6918753
PP
620
621 if (notit->event_header_field) {
78cf9df6 622 bt_event_header_field_release(notit->event_header_field);
a6918753
PP
623 notit->event_header_field = NULL;
624 }
625
7b33a0e0
PP
626 notit->dscopes.event_common_context = NULL;
627 notit->dscopes.event_spec_context = NULL;
a6918753 628 notit->dscopes.event_payload = NULL;
e98a2d6e
PP
629}
630
631static
a6918753 632void release_all_dscopes(struct bt_notif_iter *notit)
e98a2d6e 633{
a6918753
PP
634 notit->dscopes.trace_packet_header = NULL;
635
636 if (notit->packet_header_field) {
78cf9df6 637 bt_packet_header_field_release(notit->packet_header_field);
a6918753
PP
638 notit->packet_header_field = NULL;
639 }
640
641 notit->dscopes.stream_packet_context = NULL;
642
643 if (notit->packet_context_field) {
78cf9df6 644 bt_packet_context_field_release(notit->packet_context_field);
a6918753
PP
645 notit->packet_context_field = NULL;
646 }
647
648 release_event_dscopes(notit);
e98a2d6e
PP
649}
650
651static
839d52a5
PP
652enum bt_notif_iter_status read_packet_header_begin_state(
653 struct bt_notif_iter *notit)
e98a2d6e 654{
939190b3 655 struct ctf_field_class *packet_header_fc = NULL;
839d52a5 656 enum bt_notif_iter_status ret = BT_NOTIF_ITER_STATUS_OK;
e98a2d6e 657
839d52a5 658 if (bt_notif_iter_switch_packet(notit)) {
fdf0e7a0 659 BT_LOGW("Cannot switch packet: notit-addr=%p", notit);
839d52a5 660 ret = BT_NOTIF_ITER_STATUS_ERROR;
2cf1d51e
JG
661 goto end;
662 }
e98a2d6e 663
10b7a2e4 664 /* Packet header class is common to the whole trace class. */
939190b3
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
a6918753 671 BT_ASSERT(!notit->packet_header_field);
7b33a0e0 672
939190b3 673 if (packet_header_fc->in_ir) {
7b33a0e0 674 /*
10b7a2e4
PP
675 * Create free packet header field from trace class.
676 * This field is going to be moved to the packet once we
7b33a0e0
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 */
9e550e5f 684 notit->packet_header_field =
78cf9df6 685 bt_packet_header_field_create(
9e550e5f 686 notit->meta.tc->ir_tc);
7b33a0e0 687 if (!notit->packet_header_field) {
10b7a2e4 688 BT_LOGE_STR("Cannot create packet header field wrapper from trace class.");
7b33a0e0
PP
689 ret = BT_NOTIF_ITER_STATUS_ERROR;
690 goto end;
691 }
692
693 notit->dscopes.trace_packet_header =
78cf9df6 694 bt_packet_header_field_borrow_field(
9e550e5f 695 notit->packet_header_field);
7b33a0e0 696 BT_ASSERT(notit->dscopes.trace_packet_header);
a6918753
PP
697 }
698
7b33a0e0
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:"
10b7a2e4
PP
703 "notit-addr=%p, trace-class-addr=%p, fc-addr=%p",
704 notit, notit->meta.tc, packet_header_fc);
939190b3 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,
a6918753 708 notit->dscopes.trace_packet_header);
fdf0e7a0
PP
709 if (ret < 0) {
710 BT_LOGW("Cannot decode packet header field: "
10b7a2e4
PP
711 "notit-addr=%p, trace-class-addr=%p, "
712 "fc-addr=%p",
713 notit, notit->meta.tc, packet_header_fc);
fdf0e7a0 714 }
b8418c69 715
e98a2d6e 716end:
2cf1d51e 717 return ret;
e98a2d6e
PP
718}
719
720static
839d52a5
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,
7b33a0e0 725 STATE_AFTER_TRACE_PACKET_HEADER);
5f870343
JG
726}
727
e98a2d6e 728static inline
7b33a0e0 729enum bt_notif_iter_status set_current_stream_class(struct bt_notif_iter *notit)
e98a2d6e 730{
839d52a5 731 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
7b33a0e0
PP
732 struct ctf_stream_class *new_stream_class = NULL;
733
734 if (notit->cur_stream_class_id == -1) {
fdf0e7a0 735 /*
7b33a0e0
PP
736 * No current stream class ID field, therefore only one
737 * stream class.
fdf0e7a0 738 */
7b33a0e0
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: "
10b7a2e4 742 "notit-addr=%p", notit);
7b33a0e0
PP
743 status = BT_NOTIF_ITER_STATUS_ERROR;
744 goto end;
745 }
e98a2d6e 746
7b33a0e0
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
7b33a0e0
PP
751 new_stream_class = ctf_trace_class_borrow_stream_class_by_id(
752 notit->meta.tc, notit->cur_stream_class_id);
c937c615 753 if (!new_stream_class) {
10b7a2e4 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 ", "
10b7a2e4
PP
756 "trace-class-addr=%p",
757 notit, notit->cur_stream_class_id, notit->meta.tc);
839d52a5 758 status = BT_NOTIF_ITER_STATUS_ERROR;
e98a2d6e
PP
759 goto end;
760 }
761
7b33a0e0
PP
762 if (notit->meta.sc) {
763 if (new_stream_class != notit->meta.sc) {
c937c615
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, "
c937c615
PP
766 "prev-stream-class-id=%" PRId64 ", "
767 "next-stream-class-addr=%p, "
c937c615 768 "next-stream-class-id=%" PRId64 ", "
10b7a2e4 769 "trace-addr=%p",
7b33a0e0
PP
770 notit, notit->meta.sc,
771 notit->meta.sc->id,
c937c615 772 new_stream_class,
7b33a0e0 773 new_stream_class->id,
10b7a2e4 774 notit->meta.tc);
839d52a5 775 status = BT_NOTIF_ITER_STATUS_ERROR;
c937c615
PP
776 goto end;
777 }
778 } else {
7b33a0e0 779 notit->meta.sc = new_stream_class;
c937c615
PP
780 }
781
fdf0e7a0
PP
782 BT_LOGV("Set current stream class: "
783 "notit-addr=%p, stream-class-addr=%p, "
7b33a0e0
PP
784 "stream-class-id=%" PRId64,
785 notit, notit->meta.sc, notit->meta.sc->id);
b8418c69 786
e98a2d6e 787end:
e98a2d6e
PP
788 return status;
789}
790
a6918753
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;
78cf9df6 795 struct bt_stream *stream = NULL;
a6918753
PP
796
797 BT_LOGV("Calling user function (get stream): notit-addr=%p, "
7b33a0e0
PP
798 "stream-class-addr=%p, stream-class-id=%" PRId64,
799 notit, notit->meta.sc,
800 notit->meta.sc->id);
4b70020d 801 stream = notit->medium.medops.borrow_stream(
7b33a0e0 802 notit->meta.sc->ir_sc, notit->cur_data_stream_id,
4b70020d 803 notit->medium.data);
8c6884d9 804 bt_stream_get_ref(stream);
a6918753
PP
805 BT_LOGV("User function returned: stream-addr=%p", stream);
806 if (!stream) {
7b33a0e0
PP
807 BT_LOGW_STR("User function failed to return a stream object "
808 "for the given stream class.");
a6918753
PP
809 status = BT_NOTIF_ITER_STATUS_ERROR;
810 goto end;
811 }
812
813 if (notit->stream && stream != notit->stream) {
7b33a0e0
PP
814 BT_LOGW("User function returned a different stream than the "
815 "previous one for the same sequence of packets.");
a6918753
PP
816 status = BT_NOTIF_ITER_STATUS_ERROR;
817 goto end;
818 }
819
8c6884d9 820 BT_STREAM_MOVE_REF(notit->stream, stream);
a6918753
PP
821
822end:
8c6884d9 823 bt_stream_put_ref(stream);
a6918753
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;
78cf9df6 831 struct bt_packet *packet = NULL;
a6918753
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, "
a6918753 838 "stream-class-id=%" PRId64,
7b33a0e0
PP
839 notit, notit->stream, notit->meta.sc,
840 notit->meta.sc->id);
a6918753
PP
841
842 /* Create packet */
843 BT_ASSERT(notit->stream);
78cf9df6 844 packet = bt_packet_create(notit->stream);
a6918753
PP
845 if (!packet) {
846 BT_LOGE("Cannot create packet from stream: "
847 "notit-addr=%p, stream-addr=%p, "
848 "stream-class-addr=%p, "
a6918753 849 "stream-class-id=%" PRId64,
7b33a0e0
PP
850 notit, notit->stream, notit->meta.sc,
851 notit->meta.sc->id);
a6918753
PP
852 goto error;
853 }
854
855 goto end;
856
857error:
8c6884d9 858 BT_PACKET_PUT_REF_AND_RESET(packet);
a6918753
PP
859 status = BT_NOTIF_ITER_STATUS_ERROR;
860
861end:
8c6884d9 862 BT_PACKET_MOVE_REF(notit->packet, packet);
a6918753
PP
863 return status;
864}
865
e98a2d6e 866static
839d52a5
PP
867enum bt_notif_iter_status after_packet_header_state(
868 struct bt_notif_iter *notit)
e98a2d6e 869{
839d52a5 870 enum bt_notif_iter_status status;
e98a2d6e
PP
871
872 status = set_current_stream_class(notit);
a6918753
PP
873 if (status != BT_NOTIF_ITER_STATUS_OK) {
874 goto end;
e98a2d6e
PP
875 }
876
a6918753
PP
877 notit->state = STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN;
878
879end:
e98a2d6e
PP
880 return status;
881}
882
883static
839d52a5
PP
884enum bt_notif_iter_status read_packet_context_begin_state(
885 struct bt_notif_iter *notit)
e98a2d6e 886{
839d52a5 887 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
939190b3 888 struct ctf_field_class *packet_context_fc;
e98a2d6e 889
7b33a0e0 890 BT_ASSERT(notit->meta.sc);
939190b3
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, "
7b33a0e0
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
a6918753 902 BT_ASSERT(!notit->packet_context_field);
7b33a0e0 903
939190b3 904 if (packet_context_fc->in_ir) {
7b33a0e0
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 =
78cf9df6 915 bt_packet_context_field_create(
9e550e5f 916 notit->meta.sc->ir_sc);
7b33a0e0
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 =
78cf9df6 924 bt_packet_context_field_borrow_field(
9e550e5f 925 notit->packet_context_field);
7b33a0e0 926 BT_ASSERT(notit->dscopes.stream_packet_context);
a6918753
PP
927 }
928
fdf0e7a0
PP
929 BT_LOGV("Decoding packet context field: "
930 "notit-addr=%p, stream-class-addr=%p, "
939190b3 931 "stream-class-id=%" PRId64 ", fc-addr=%p",
7b33a0e0 932 notit, notit->meta.sc,
939190b3
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,
a6918753 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, "
939190b3 941 "stream-class-id=%" PRId64 ", fc-addr=%p",
7b33a0e0
PP
942 notit, notit->meta.sc,
943 notit->meta.sc->id,
939190b3 944 packet_context_fc);
fdf0e7a0 945 }
e98a2d6e
PP
946
947end:
e98a2d6e
PP
948 return status;
949}
950
951static
839d52a5
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
839d52a5
PP
960enum bt_notif_iter_status set_current_packet_content_sizes(
961 struct bt_notif_iter *notit)
e98a2d6e 962{
839d52a5 963 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
e98a2d6e 964
7b33a0e0
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, "
7b33a0e0 969 "packet-size=%" PRId64 ", content-size=%" PRId64,
fdf0e7a0 970 notit, notit->dscopes.stream_packet_context,
7b33a0e0
PP
971 notit->cur_exp_packet_total_size,
972 notit->cur_exp_packet_content_size);
839d52a5 973 status = BT_NOTIF_ITER_STATUS_ERROR;
e98a2d6e
PP
974 goto end;
975 }
e98a2d6e 976 } else {
7b33a0e0
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
7b33a0e0
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, "
7b33a0e0 988 "packet-size=%" PRId64 ", content-size=%" PRId64,
fdf0e7a0 989 notit, notit->dscopes.stream_packet_context,
7b33a0e0
PP
990 notit->cur_exp_packet_total_size,
991 notit->cur_exp_packet_content_size);
839d52a5 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,
7b33a0e0
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
839d52a5
PP
1005enum bt_notif_iter_status after_packet_context_state(
1006 struct bt_notif_iter *notit)
e98a2d6e 1007{
839d52a5 1008 enum bt_notif_iter_status status;
e98a2d6e
PP
1009
1010 status = set_current_packet_content_sizes(notit);
8fc063a2
PP
1011 if (status != BT_NOTIF_ITER_STATUS_OK) {
1012 goto end;
1013 }
1014
8fc063a2
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
8fc063a2 1021end:
e98a2d6e
PP
1022 return status;
1023}
1024
1025static
839d52a5
PP
1026enum bt_notif_iter_status read_event_header_begin_state(
1027 struct bt_notif_iter *notit)
e98a2d6e 1028{
839d52a5 1029 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
939190b3 1030 struct ctf_field_class *event_header_fc = NULL;
e98a2d6e 1031
9b876b7b
PP
1032 /* Reset the position of the last event header */
1033 notit->buf.last_eh_at = notit->buf.at;
7b33a0e0 1034 notit->cur_event_class_id = -1;
9b876b7b 1035
e98a2d6e 1036 /* Check if we have some content left */
7b33a0e0
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;
7b33a0e0
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: "
41342c71 1049 "notit-addr=%p, content-size=%" PRId64 ", "
7b33a0e0
PP
1050 "cur=%zu", notit,
1051 notit->cur_exp_packet_content_size,
fdf0e7a0 1052 packet_at(notit));
839d52a5 1053 status = BT_NOTIF_ITER_STATUS_ERROR;
e98a2d6e
PP
1054 goto end;
1055 }
7b33a0e0
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
a6918753 1082 release_event_dscopes(notit);
7b33a0e0 1083 BT_ASSERT(notit->meta.sc);
939190b3
PP
1084 event_header_fc = notit->meta.sc->event_header_fc;
1085 if (!event_header_fc) {
7b33a0e0 1086 notit->state = STATE_AFTER_EVENT_HEADER;
e98a2d6e
PP
1087 goto end;
1088 }
1089
939190b3 1090 if (event_header_fc->in_ir) {
7b33a0e0 1091 BT_ASSERT(!notit->event_header_field);
9e550e5f 1092 notit->event_header_field =
78cf9df6 1093 bt_event_header_field_create(
9e550e5f 1094 notit->meta.sc->ir_sc);
7b33a0e0 1095 if (!notit->event_header_field) {
10b7a2e4 1096 BT_LOGE_STR("Cannot create event header field wrapper from trace class.");
7b33a0e0
PP
1097 status = BT_NOTIF_ITER_STATUS_ERROR;
1098 goto end;
1099 }
1100
1101 notit->dscopes.event_header =
78cf9df6 1102 bt_event_header_field_borrow_field(
9e550e5f 1103 notit->event_header_field);
7b33a0e0 1104 BT_ASSERT(notit->dscopes.event_header);
a6918753
PP
1105 }
1106
fdf0e7a0
PP
1107 BT_LOGV("Decoding event header field: "
1108 "notit-addr=%p, stream-class-addr=%p, "
7b33a0e0 1109 "stream-class-id=%" PRId64 ", "
939190b3 1110 "fc-addr=%p",
7b33a0e0
PP
1111 notit, notit->meta.sc,
1112 notit->meta.sc->id,
939190b3
PP
1113 event_header_fc);
1114 status = read_dscope_begin_state(notit, event_header_fc,
7b33a0e0
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, "
939190b3 1121 "stream-class-id=%" PRId64 ", fc-addr=%p",
7b33a0e0
PP
1122 notit, notit->meta.sc,
1123 notit->meta.sc->id,
939190b3 1124 event_header_fc);
fdf0e7a0 1125 }
e98a2d6e 1126
b8418c69 1127end:
e98a2d6e
PP
1128 return status;
1129}
1130
1131static
839d52a5
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,
7b33a0e0 1136 STATE_AFTER_EVENT_HEADER);
e98a2d6e
PP
1137}
1138
1139static inline
839d52a5 1140enum bt_notif_iter_status set_current_event_class(struct bt_notif_iter *notit)
e98a2d6e 1141{
839d52a5 1142 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
e98a2d6e 1143
7b33a0e0 1144 struct ctf_event_class *new_event_class = NULL;
e98a2d6e 1145
7b33a0e0 1146 if (notit->cur_event_class_id == -1) {
e98a2d6e 1147 /*
7b33a0e0
PP
1148 * No current event class ID field, therefore only one
1149 * event class.
e98a2d6e 1150 */
7b33a0e0
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: "
10b7a2e4 1154 "notit-addr=%p", notit);
7b33a0e0
PP
1155 status = BT_NOTIF_ITER_STATUS_ERROR;
1156 goto end;
fdf0e7a0 1157 }
e98a2d6e 1158
7b33a0e0
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
7b33a0e0
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: "
7b33a0e0
PP
1167 "notit-addr=%p, stream-class-id=%" PRIu64 ", "
1168 "event-class-id=%" PRIu64 ", "
10b7a2e4 1169 "trace-class-addr=%p",
7b33a0e0 1170 notit, notit->meta.sc->id, notit->cur_event_class_id,
10b7a2e4 1171 notit->meta.tc);
839d52a5 1172 status = BT_NOTIF_ITER_STATUS_ERROR;
e98a2d6e
PP
1173 goto end;
1174 }
1175
7b33a0e0 1176 notit->meta.ec = new_event_class;
fdf0e7a0
PP
1177 BT_LOGV("Set current event class: "
1178 "notit-addr=%p, event-class-addr=%p, "
7b33a0e0
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
a6918753
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;
7b53201c 1193 struct bt_notification *notif = NULL;
a6918753 1194
7b33a0e0 1195 BT_ASSERT(notit->meta.ec);
a6918753
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",
7b33a0e0
PP
1199 notit, notit->meta.ec,
1200 notit->meta.ec->name->str,
a6918753 1201 notit->packet);
03e18d5d 1202 BT_ASSERT(notit->notif_iter);
7b53201c 1203 notif = bt_notification_event_create(notit->notif_iter,
7b33a0e0 1204 notit->meta.ec->ir_ec, notit->packet);
a6918753
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",
7b33a0e0
PP
1209 notit, notit->meta.ec,
1210 notit->meta.ec->name->str,
a6918753
PP
1211 notit->packet);
1212 goto error;
1213 }
1214
1215 goto end;
1216
1217error:
8c6884d9 1218 BT_NOTIFICATION_PUT_REF_AND_RESET(notif);
a6918753
PP
1219 status = BT_NOTIF_ITER_STATUS_ERROR;
1220
1221end:
8c6884d9 1222 BT_NOTIFICATION_MOVE_REF(notit->event_notif, notif);
a6918753
PP
1223 return status;
1224}
1225
e98a2d6e 1226static
839d52a5
PP
1227enum bt_notif_iter_status after_event_header_state(
1228 struct bt_notif_iter *notit)
e98a2d6e 1229{
839d52a5 1230 enum bt_notif_iter_status status;
e98a2d6e 1231
e98a2d6e 1232 status = set_current_event_class(notit);
839d52a5 1233 if (status != BT_NOTIF_ITER_STATUS_OK) {
e98a2d6e
PP
1234 goto end;
1235 }
1236
a6918753
PP
1237 status = set_current_event_notification(notit);
1238 if (status != BT_NOTIF_ITER_STATUS_OK) {
1239 goto end;
1240 }
1241
7b53201c 1242 notit->event = bt_notification_event_borrow_event(
9e550e5f 1243 notit->event_notif);
a6918753
PP
1244 BT_ASSERT(notit->event);
1245
1246 if (notit->event_header_field) {
1247 int ret;
1248
1249 BT_ASSERT(notit->event);
78cf9df6 1250 ret = bt_event_move_header_field(notit->event,
a6918753 1251 notit->event_header_field);
a6918753
PP
1252 if (ret) {
1253 status = BT_NOTIF_ITER_STATUS_ERROR;
1254 goto end;
1255 }
1256
1257 notit->event_header_field = NULL;
1258
1259 /*
7b33a0e0 1260 * At this point notit->dscopes.event_header has
a6918753
PP
1261 * the same value as the event header field within
1262 * notit->event.
1263 */
78cf9df6 1264 BT_ASSERT(bt_event_borrow_header_field(
9e550e5f 1265 notit->event) == notit->dscopes.event_header);
a6918753
PP
1266 }
1267
7b33a0e0 1268 notit->state = STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN;
e98a2d6e
PP
1269
1270end:
1271 return status;
1272}
1273
1274static
7b33a0e0 1275enum bt_notif_iter_status read_event_common_context_begin_state(
839d52a5 1276 struct bt_notif_iter *notit)
e98a2d6e 1277{
839d52a5 1278 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
939190b3 1279 struct ctf_field_class *event_common_context_fc;
e98a2d6e 1280
939190b3
PP
1281 event_common_context_fc = notit->meta.sc->event_common_context_fc;
1282 if (!event_common_context_fc) {
7b33a0e0 1283 notit->state = STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN;
e98a2d6e
PP
1284 goto end;
1285 }
1286
939190b3 1287 if (event_common_context_fc->in_ir) {
7b33a0e0
PP
1288 BT_ASSERT(!notit->dscopes.event_common_context);
1289 notit->dscopes.event_common_context =
78cf9df6 1290 bt_event_borrow_common_context_field(
9e550e5f 1291 notit->event);
7b33a0e0
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, "
7b33a0e0 1297 "stream-class-id=%" PRId64 ", "
939190b3 1298 "fc-addr=%p",
7b33a0e0
PP
1299 notit, notit->meta.sc,
1300 notit->meta.sc->id,
939190b3
PP
1301 event_common_context_fc);
1302 status = read_dscope_begin_state(notit, event_common_context_fc,
7b33a0e0
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) {
7b33a0e0 1307 BT_LOGW("Cannot decode event common context field: "
fdf0e7a0 1308 "notit-addr=%p, stream-class-addr=%p, "
939190b3 1309 "stream-class-id=%" PRId64 ", fc-addr=%p",
7b33a0e0
PP
1310 notit, notit->meta.sc,
1311 notit->meta.sc->id,
939190b3 1312 event_common_context_fc);
fdf0e7a0 1313 }
e98a2d6e
PP
1314
1315end:
e98a2d6e
PP
1316 return status;
1317}
1318
1319static
7b33a0e0 1320enum bt_notif_iter_status read_event_common_context_continue_state(
839d52a5 1321 struct bt_notif_iter *notit)
e98a2d6e
PP
1322{
1323 return read_dscope_continue_state(notit,
7b33a0e0 1324 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN);
e98a2d6e
PP
1325}
1326
1327static
7b33a0e0 1328enum bt_notif_iter_status read_event_spec_context_begin_state(
839d52a5 1329 struct bt_notif_iter *notit)
e98a2d6e 1330{
839d52a5 1331 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
939190b3 1332 struct ctf_field_class *event_spec_context_fc;
e98a2d6e 1333
939190b3
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
939190b3 1340 if (event_spec_context_fc->in_ir) {
7b33a0e0 1341 BT_ASSERT(!notit->dscopes.event_spec_context);
9e550e5f 1342 notit->dscopes.event_spec_context =
78cf9df6 1343 bt_event_borrow_specific_context_field(
9e550e5f 1344 notit->event);
7b33a0e0
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 ", "
939190b3 1351 "fc-addr=%p",
7b33a0e0
PP
1352 notit, notit->meta.ec,
1353 notit->meta.ec->name->str,
1354 notit->meta.ec->id,
939190b3
PP
1355 event_spec_context_fc);
1356 status = read_dscope_begin_state(notit, event_spec_context_fc,
e98a2d6e 1357 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN,
7b33a0e0
PP
1358 STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE,
1359 notit->dscopes.event_spec_context);
fdf0e7a0 1360 if (status < 0) {
7b33a0e0 1361 BT_LOGW("Cannot decode event specific context field: "
fdf0e7a0
PP
1362 "notit-addr=%p, event-class-addr=%p, "
1363 "event-class-name=\"%s\", "
939190b3 1364 "event-class-id=%" PRId64 ", fc-addr=%p",
7b33a0e0
PP
1365 notit, notit->meta.ec,
1366 notit->meta.ec->name->str,
1367 notit->meta.ec->id,
939190b3 1368 event_spec_context_fc);
fdf0e7a0 1369 }
e98a2d6e
PP
1370
1371end:
e98a2d6e
PP
1372 return status;
1373}
1374
1375static
7b33a0e0 1376enum bt_notif_iter_status read_event_spec_context_continue_state(
839d52a5 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
839d52a5
PP
1384enum bt_notif_iter_status read_event_payload_begin_state(
1385 struct bt_notif_iter *notit)
e98a2d6e 1386{
839d52a5 1387 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
939190b3 1388 struct ctf_field_class *event_payload_fc;
e98a2d6e 1389
939190b3
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
939190b3 1396 if (event_payload_fc->in_ir) {
7b33a0e0 1397 BT_ASSERT(!notit->dscopes.event_payload);
9e550e5f 1398 notit->dscopes.event_payload =
78cf9df6 1399 bt_event_borrow_payload_field(
9e550e5f 1400 notit->event);
7b33a0e0
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 ", "
939190b3 1407 "fc-addr=%p",
7b33a0e0
PP
1408 notit, notit->meta.ec,
1409 notit->meta.ec->name->str,
1410 notit->meta.ec->id,
939190b3
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,
a6918753 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\", "
939190b3 1420 "event-class-id=%" PRId64 ", fc-addr=%p",
7b33a0e0
PP
1421 notit, notit->meta.ec,
1422 notit->meta.ec->name->str,
1423 notit->meta.ec->id,
939190b3 1424 event_payload_fc);
fdf0e7a0 1425 }
e98a2d6e
PP
1426
1427end:
e98a2d6e
PP
1428 return status;
1429}
1430
1431static
839d52a5
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
839d52a5
PP
1439enum bt_notif_iter_status skip_packet_padding_state(
1440 struct bt_notif_iter *notit)
e98a2d6e 1441{
839d52a5 1442 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
e98a2d6e
PP
1443 size_t bits_to_skip;
1444
7b33a0e0
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);
839d52a5 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);
7b33a0e0
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
839d52a5 1477enum bt_notif_iter_status handle_state(struct bt_notif_iter *notit)
e98a2d6e 1478{
839d52a5 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;
6ff151ad
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:
7b33a0e0 1512 notit->state = STATE_DSCOPE_EVENT_HEADER_BEGIN;
e98a2d6e 1513 break;
7b33a0e0 1514 case STATE_DSCOPE_EVENT_HEADER_BEGIN:
e98a2d6e
PP
1515 status = read_event_header_begin_state(notit);
1516 break;
7b33a0e0 1517 case STATE_DSCOPE_EVENT_HEADER_CONTINUE:
e98a2d6e
PP
1518 status = read_event_header_continue_state(notit);
1519 break;
7b33a0e0 1520 case STATE_AFTER_EVENT_HEADER:
e98a2d6e
PP
1521 status = after_event_header_state(notit);
1522 break;
7b33a0e0
PP
1523 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN:
1524 status = read_event_common_context_begin_state(notit);
e98a2d6e 1525 break;
7b33a0e0
PP
1526 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE:
1527 status = read_event_common_context_continue_state(notit);
e98a2d6e 1528 break;
7b33a0e0
PP
1529 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN:
1530 status = read_event_spec_context_begin_state(notit);
e98a2d6e 1531 break;
7b33a0e0
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:
7b33a0e0 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",
839d52a5 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 */
6ff151ad 1566BT_HIDDEN
839d52a5 1567void bt_notif_iter_reset(struct bt_notif_iter *notit)
e98a2d6e 1568{
8b45963b 1569 BT_ASSERT(notit);
fdf0e7a0 1570 BT_LOGD("Resetting notification iterator: addr=%p", notit);
e98a2d6e 1571 stack_clear(notit->stack);
7b33a0e0
PP
1572 notit->meta.sc = NULL;
1573 notit->meta.ec = NULL;
8c6884d9
PP
1574 BT_PACKET_PUT_REF_AND_RESET(notit->packet);
1575 BT_STREAM_PUT_REF_AND_RESET(notit->stream);
1576 BT_NOTIFICATION_PUT_REF_AND_RESET(notit->event_notif);
a6918753
PP
1577 release_all_dscopes(notit);
1578 notit->cur_dscope_field = NULL;
1579
1580 if (notit->packet_header_field) {
78cf9df6 1581 bt_packet_header_field_release(notit->packet_header_field);
a6918753
PP
1582 notit->packet_header_field = NULL;
1583 }
1584
1585 if (notit->packet_context_field) {
78cf9df6 1586 bt_packet_context_field_release(notit->packet_context_field);
a6918753
PP
1587 notit->packet_context_field = NULL;
1588 }
1589
1590 if (notit->event_header_field) {
78cf9df6 1591 bt_event_header_field_release(notit->event_header_field);
a6918753
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;
bb8744a7 1598 notit->buf.last_eh_at = SIZE_MAX;
e98a2d6e
PP
1599 notit->buf.packet_offset = 0;
1600 notit->state = STATE_INIT;
7b33a0e0
PP
1601 notit->cur_exp_packet_content_size = -1;
1602 notit->cur_exp_packet_total_size = -1;
bb230c9b 1603 notit->cur_packet_offset = -1;
7b33a0e0
PP
1604 notit->cur_stream_class_id = -1;
1605 notit->cur_event_class_id = -1;
1606 notit->cur_data_stream_id = -1;
6ff151ad 1607 notit->stream_begin_emitted = false;
e98a2d6e
PP
1608}
1609
2cf1d51e 1610static
839d52a5 1611int bt_notif_iter_switch_packet(struct bt_notif_iter *notit)
2cf1d51e
JG
1612{
1613 int ret = 0;
1614
c937c615
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 */
8b45963b 1620 BT_ASSERT(notit);
a6918753 1621
7b33a0e0
PP
1622 if (notit->cur_exp_packet_total_size != -1) {
1623 notit->cur_packet_offset += notit->cur_exp_packet_total_size;
bb230c9b 1624 }
a6918753 1625
bb230c9b
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);
7b33a0e0 1630 notit->meta.ec = NULL;
8c6884d9
PP
1631 BT_PACKET_PUT_REF_AND_RESET(notit->packet);
1632 BT_NOTIFICATION_PUT_REF_AND_RESET(notit->event_notif);
a6918753
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
7b33a0e0
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
78cf9df6 1674struct bt_field *borrow_next_field(struct bt_notif_iter *notit)
e98a2d6e 1675{
78cf9df6
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
8b45963b 1681 BT_ASSERT(!stack_empty(notit->stack));
e98a2d6e
PP
1682 index = stack_top(notit->stack)->index;
1683 base_field = stack_top(notit->stack)->base;
8b45963b 1684 BT_ASSERT(base_field);
78cf9df6 1685 base_fc = bt_field_borrow_class_const(base_field);
939190b3 1686 BT_ASSERT(base_fc);
e98a2d6e 1687
78cf9df6 1688 switch (bt_field_class_get_type(base_fc)) {
af0c18e3 1689 case BT_FIELD_CLASS_TYPE_STRUCTURE:
6ff151ad 1690 {
7b33a0e0 1691 BT_ASSERT(index <
939190b3 1692 bt_field_class_structure_get_member_count(
78cf9df6
PP
1693 bt_field_borrow_class_const(
1694 base_field)));
9e550e5f 1695 next_field =
78cf9df6 1696 bt_field_structure_borrow_member_field_by_index(
9e550e5f 1697 base_field, index);
e98a2d6e 1698 break;
6ff151ad 1699 }
af0c18e3
PP
1700 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
1701 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
78cf9df6
PP
1702 BT_ASSERT(index < bt_field_array_get_length(base_field));
1703 next_field = bt_field_array_borrow_element_field_by_index(
7b33a0e0 1704 base_field, index);
e98a2d6e 1705 break;
af0c18e3 1706 case BT_FIELD_CLASS_TYPE_VARIANT:
7b33a0e0 1707 BT_ASSERT(index == 0);
78cf9df6 1708 next_field = bt_field_variant_borrow_selected_option_field(
7b33a0e0 1709 base_field);
e98a2d6e
PP
1710 break;
1711 default:
0fbb9a9f 1712 abort();
e98a2d6e
PP
1713 }
1714
7b33a0e0 1715 BT_ASSERT(next_field);
e98a2d6e
PP
1716 return next_field;
1717}
1718
c44c3e70 1719static
7b33a0e0
PP
1720void update_default_clock(struct bt_notif_iter *notit, uint64_t new_val,
1721 uint64_t new_val_size)
c44c3e70 1722{
7b33a0e0 1723 uint64_t new_val_mask;
c44c3e70 1724 uint64_t cur_value_masked;
c44c3e70 1725
7b33a0e0 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 */
7b33a0e0
PP
1733 if (new_val_size == 64) {
1734 notit->default_clock_val = new_val;
c44c3e70
JG
1735 goto end;
1736 }
1737
7b33a0e0
PP
1738 new_val_mask = (1ULL << new_val_size) - 1;
1739 cur_value_masked = notit->default_clock_val & new_val_mask;
c44c3e70 1740
7b33a0e0 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 */
7b33a0e0 1747 notit->default_clock_val += new_val_mask + 1;
c44c3e70
JG
1748 }
1749
1750 /* Clear the low bits of the current clock value. */
7b33a0e0 1751 notit->default_clock_val &= ~new_val_mask;
c44c3e70
JG
1752
1753 /* Set the low bits of the current clock value. */
7b33a0e0 1754 notit->default_clock_val |= new_val;
b8418c69 1755
c44c3e70 1756end:
7b33a0e0
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
939190b3
PP
1762enum bt_bfcr_status bfcr_unsigned_int_cb(uint64_t value,
1763 struct ctf_field_class *fc, void *data)
c44c3e70 1764{
7b33a0e0 1765 struct bt_notif_iter *notit = data;
939190b3 1766 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
78cf9df6 1767 struct bt_field *field = NULL;
939190b3 1768 struct ctf_field_class_int *int_fc = (void *) fc;
c44c3e70 1769
939190b3
PP
1770 BT_LOGV("Unsigned integer function called from BFCR: "
1771 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
af0c18e3
PP
1772 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64,
1773 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
a6918753 1774
939190b3 1775 if (likely(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE)) {
7b33a0e0 1776 goto update_def_clock;
c44c3e70
JG
1777 }
1778
939190b3
PP
1779 switch (int_fc->meaning) {
1780 case CTF_FIELD_CLASS_MEANING_EVENT_CLASS_ID:
7b33a0e0
PP
1781 notit->cur_event_class_id = value;
1782 break;
939190b3 1783 case CTF_FIELD_CLASS_MEANING_DATA_STREAM_ID:
7b33a0e0
PP
1784 notit->cur_data_stream_id = value;
1785 break;
939190b3 1786 case CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME:
7b33a0e0
PP
1787 notit->snapshots.beginning_clock = value;
1788 break;
939190b3 1789 case CTF_FIELD_CLASS_MEANING_PACKET_END_TIME:
7b33a0e0
PP
1790 notit->snapshots.end_clock = value;
1791 break;
939190b3 1792 case CTF_FIELD_CLASS_MEANING_STREAM_CLASS_ID:
7b33a0e0
PP
1793 notit->cur_stream_class_id = value;
1794 break;
939190b3 1795 case CTF_FIELD_CLASS_MEANING_MAGIC:
7b33a0e0
PP
1796 if (value != 0xc1fc1fc1) {
1797 BT_LOGW("Invalid CTF magic number: notit-addr=%p, "
1798 "magic=%" PRIx64, notit, value);
939190b3 1799 status = BT_BFCR_STATUS_ERROR;
c44c3e70
JG
1800 goto end;
1801 }
fdf0e7a0 1802
7b33a0e0 1803 break;
939190b3 1804 case CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT:
7b33a0e0
PP
1805 notit->snapshots.packets = value;
1806 break;
939190b3 1807 case CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT:
7b33a0e0
PP
1808 notit->snapshots.discarded_events = value;
1809 break;
939190b3 1810 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_TOTAL_SIZE:
7b33a0e0
PP
1811 notit->cur_exp_packet_total_size = value;
1812 break;
939190b3 1813 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_CONTENT_SIZE:
7b33a0e0
PP
1814 notit->cur_exp_packet_content_size = value;
1815 break;
1816 default:
1817 abort();
c44c3e70
JG
1818 }
1819
7b33a0e0 1820update_def_clock:
939190b3
PP
1821 if (unlikely(int_fc->mapped_clock_class)) {
1822 update_default_clock(notit, value, int_fc->base.size);
7b33a0e0 1823 }
c44c3e70 1824
939190b3 1825 if (unlikely(int_fc->storing_index >= 0)) {
7b33a0e0 1826 g_array_index(notit->stored_values, uint64_t,
939190b3 1827 (uint64_t) int_fc->storing_index) = value;
7b33a0e0 1828 }
e98a2d6e 1829
939190b3 1830 if (unlikely(!fc->in_ir)) {
a6918753 1831 goto end;
e98a2d6e
PP
1832 }
1833
7b33a0e0
PP
1834 field = borrow_next_field(notit);
1835 BT_ASSERT(field);
78cf9df6
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
a6918753 1844end:
e98a2d6e
PP
1845 return status;
1846}
1847
5f870343 1848static
939190b3
PP
1849enum bt_bfcr_status bfcr_unsigned_int_char_cb(uint64_t value,
1850 struct ctf_field_class *fc, void *data)
5f870343 1851{
7b33a0e0 1852 int ret;
839d52a5 1853 struct bt_notif_iter *notit = data;
939190b3 1854 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
78cf9df6 1855 struct bt_field *string_field = NULL;
939190b3 1856 struct ctf_field_class_int *int_fc = (void *) fc;
7b33a0e0 1857 char str[2] = {'\0', '\0'};
5f870343 1858
939190b3
PP
1859 BT_LOGV("Unsigned integer character function called from BFCR: "
1860 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
af0c18e3
PP
1861 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64,
1862 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
939190b3
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);
a6918753 1866
939190b3 1867 if (unlikely(!fc->in_ir)) {
7b33a0e0
PP
1868 goto end;
1869 }
5f870343 1870
7b33a0e0 1871 if (notit->done_filling_string) {
5f870343
JG
1872 goto end;
1873 }
1874
7b33a0e0
PP
1875 if (value == 0) {
1876 notit->done_filling_string = true;
5f870343
JG
1877 goto end;
1878 }
1879
7b33a0e0 1880 string_field = stack_top(notit->stack)->base;
78cf9df6
PP
1881 BT_ASSERT(bt_field_get_class_type(string_field) ==
1882 BT_FIELD_CLASS_TYPE_STRING);
7b33a0e0
PP
1883
1884 /* Append character */
1885 str[0] = (char) value;
78cf9df6 1886 ret = bt_field_string_append_with_length(string_field, str, 1);
7b33a0e0
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);
939190b3 1891 status = BT_BFCR_STATUS_ERROR;
7b33a0e0
PP
1892 goto end;
1893 }
a6918753 1894
5f870343
JG
1895end:
1896 return status;
1897}
1898
1899static
939190b3
PP
1900enum bt_bfcr_status bfcr_signed_int_cb(int64_t value,
1901 struct ctf_field_class *fc, void *data)
e98a2d6e 1902{
939190b3 1903 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
78cf9df6 1904 struct bt_field *field = NULL;
839d52a5 1905 struct bt_notif_iter *notit = data;
939190b3 1906 struct ctf_field_class_int *int_fc = (void *) fc;
e98a2d6e 1907
939190b3
PP
1908 BT_LOGV("Signed integer function called from BFCR: "
1909 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
af0c18e3
PP
1910 "fc-type=%d, fc-in-ir=%d, value=%" PRId64,
1911 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
939190b3 1912 BT_ASSERT(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE);
7b33a0e0 1913
939190b3 1914 if (unlikely(int_fc->storing_index >= 0)) {
7b33a0e0 1915 g_array_index(notit->stored_values, uint64_t,
939190b3 1916 (uint64_t) int_fc->storing_index) = (uint64_t) value;
7b33a0e0
PP
1917 }
1918
939190b3 1919 if (unlikely(!fc->in_ir)) {
a6918753 1920 goto end;
e98a2d6e
PP
1921 }
1922
7b33a0e0
PP
1923 field = borrow_next_field(notit);
1924 BT_ASSERT(field);
78cf9df6
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
a6918753 1933end:
e98a2d6e
PP
1934 return status;
1935}
1936
1937static
939190b3
PP
1938enum bt_bfcr_status bfcr_floating_point_cb(double value,
1939 struct ctf_field_class *fc, void *data)
e98a2d6e 1940{
939190b3 1941 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
78cf9df6 1942 struct bt_field *field = NULL;
839d52a5 1943 struct bt_notif_iter *notit = data;
e98a2d6e 1944
939190b3
PP
1945 BT_LOGV("Floating point number function called from BFCR: "
1946 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
af0c18e3
PP
1947 "fc-type=%d, fc-in-ir=%d, value=%f",
1948 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
939190b3 1949 BT_ASSERT(fc->in_ir);
a6918753 1950 field = borrow_next_field(notit);
7b33a0e0 1951 BT_ASSERT(field);
78cf9df6
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
939190b3
PP
1961enum bt_bfcr_status bfcr_string_begin_cb(
1962 struct ctf_field_class *fc, void *data)
e98a2d6e 1963{
78cf9df6 1964 struct bt_field *field = NULL;
839d52a5 1965 struct bt_notif_iter *notit = data;
e98a2d6e
PP
1966 int ret;
1967
939190b3
PP
1968 BT_LOGV("String (beginning) function called from BFCR: "
1969 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
af0c18e3
PP
1970 "fc-type=%d, fc-in-ir=%d",
1971 notit, notit->bfcr, fc, fc->type, fc->in_ir);
e98a2d6e 1972
939190b3 1973 BT_ASSERT(fc->in_ir);
7b33a0e0
PP
1974 field = borrow_next_field(notit);
1975 BT_ASSERT(field);
78cf9df6
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);
a6918753
PP
1980 BT_ASSERT(ret == 0);
1981
e98a2d6e 1982 /*
939190b3
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 */
7b33a0e0 1987 stack_push(notit->stack, field);
939190b3 1988 return BT_BFCR_STATUS_OK;
e98a2d6e
PP
1989}
1990
1991static
939190b3
PP
1992enum bt_bfcr_status bfcr_string_cb(const char *value,
1993 size_t len, struct ctf_field_class *fc, void *data)
e98a2d6e 1994{
939190b3 1995 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
78cf9df6 1996 struct bt_field *field = NULL;
839d52a5 1997 struct bt_notif_iter *notit = data;
e98a2d6e
PP
1998 int ret;
1999
939190b3
PP
2000 BT_LOGV("String (substring) function called from BFCR: "
2001 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
af0c18e3
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);
939190b3 2005 BT_ASSERT(fc->in_ir);
e98a2d6e 2006 field = stack_top(notit->stack)->base;
8b45963b 2007 BT_ASSERT(field);
e98a2d6e 2008
a6918753 2009 /* Append current substring */
78cf9df6 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);
939190b3 2015 status = BT_BFCR_STATUS_ERROR;
e98a2d6e
PP
2016 goto end;
2017 }
2018
2019end:
2020 return status;
2021}
2022
2023static
939190b3
PP
2024enum bt_bfcr_status bfcr_string_end_cb(
2025 struct ctf_field_class *fc, void *data)
e98a2d6e 2026{
839d52a5 2027 struct bt_notif_iter *notit = data;
e98a2d6e 2028
939190b3
PP
2029 BT_LOGV("String (end) function called from BFCR: "
2030 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
af0c18e3
PP
2031 "fc-type=%d, fc-in-ir=%d",
2032 notit, notit->bfcr, fc, fc->type, fc->in_ir);
939190b3 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++;
939190b3 2040 return BT_BFCR_STATUS_OK;
e98a2d6e
PP
2041}
2042
939190b3
PP
2043enum bt_bfcr_status bfcr_compound_begin_cb(
2044 struct ctf_field_class *fc, void *data)
e98a2d6e 2045{
839d52a5 2046 struct bt_notif_iter *notit = data;
78cf9df6 2047 struct bt_field *field;
e98a2d6e 2048
939190b3
PP
2049 BT_LOGV("Compound (beginning) function called from BFCR: "
2050 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
af0c18e3
PP
2051 "fc-type=%d, fc-in-ir=%d",
2052 notit, notit->bfcr, fc, fc->type, fc->in_ir);
7b33a0e0 2053
939190b3 2054 if (!fc->in_ir) {
7b33a0e0
PP
2055 goto end;
2056 }
fdf0e7a0 2057
a6918753 2058 /* Borrow field */
e98a2d6e 2059 if (stack_empty(notit->stack)) {
a6918753
PP
2060 /* Root: already set by read_dscope_begin_state() */
2061 field = notit->cur_dscope_field;
e98a2d6e 2062 } else {
a6918753 2063 field = borrow_next_field(notit);
7b33a0e0 2064 BT_ASSERT(field);
e98a2d6e
PP
2065 }
2066
2067 /* Push field */
8b45963b 2068 BT_ASSERT(field);
78cf9df6 2069 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
7b33a0e0
PP
2070 stack_push(notit->stack, field);
2071
2072 /*
939190b3 2073 * Change BFCR "unsigned int" callback if it's a text
7b33a0e0
PP
2074 * array/sequence.
2075 */
af0c18e3
PP
2076 if (fc->type == CTF_FIELD_CLASS_TYPE_ARRAY ||
2077 fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE) {
939190b3 2078 struct ctf_field_class_array_base *array_fc = (void *) fc;
7b33a0e0 2079
939190b3 2080 if (array_fc->is_text) {
7b33a0e0
PP
2081 int ret;
2082
78cf9df6
PP
2083 BT_ASSERT(bt_field_get_class_type(field) ==
2084 BT_FIELD_CLASS_TYPE_STRING);
7b33a0e0 2085 notit->done_filling_string = false;
78cf9df6 2086 ret = bt_field_string_clear(field);
7b33a0e0 2087 BT_ASSERT(ret == 0);
939190b3
PP
2088 bt_bfcr_set_unsigned_int_cb(notit->bfcr,
2089 bfcr_unsigned_int_char_cb);
7b33a0e0 2090 }
e98a2d6e
PP
2091 }
2092
2093end:
939190b3 2094 return BT_BFCR_STATUS_OK;
e98a2d6e
PP
2095}
2096
939190b3
PP
2097enum bt_bfcr_status bfcr_compound_end_cb(
2098 struct ctf_field_class *fc, void *data)
e98a2d6e 2099{
839d52a5 2100 struct bt_notif_iter *notit = data;
e98a2d6e 2101
939190b3
PP
2102 BT_LOGV("Compound (end) function called from BFCR: "
2103 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
af0c18e3
PP
2104 "fc-type=%d, fc-in-ir=%d",
2105 notit, notit->bfcr, fc, fc->type, fc->in_ir);
e98a2d6e 2106
939190b3 2107 if (!fc->in_ir) {
e98a2d6e
PP
2108 goto end;
2109 }
2110
7b33a0e0 2111 BT_ASSERT(!stack_empty(notit->stack));
78cf9df6 2112 BT_ASSERT(bt_field_borrow_class_const(stack_top(notit->stack)->base) ==
939190b3 2113 fc->ir_fc);
e98a2d6e 2114
7b33a0e0 2115 /*
939190b3 2116 * Reset BFCR "unsigned int" callback if it's a text
7b33a0e0
PP
2117 * array/sequence.
2118 */
af0c18e3
PP
2119 if (fc->type == CTF_FIELD_CLASS_TYPE_ARRAY ||
2120 fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE) {
939190b3 2121 struct ctf_field_class_array_base *array_fc = (void *) fc;
7b33a0e0 2122
939190b3 2123 if (array_fc->is_text) {
af0c18e3 2124 BT_ASSERT(bt_field_get_class_type(
78cf9df6 2125 stack_top(notit->stack)->base) ==
9e550e5f 2126 BT_FIELD_CLASS_TYPE_STRING);
939190b3
PP
2127 bt_bfcr_set_unsigned_int_cb(notit->bfcr,
2128 bfcr_unsigned_int_cb);
e98a2d6e 2129 }
7b33a0e0 2130 }
e98a2d6e 2131
7b33a0e0
PP
2132 /* Pop stack */
2133 stack_pop(notit->stack);
e98a2d6e 2134
7b33a0e0
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:
939190b3 2141 return BT_BFCR_STATUS_OK;
e98a2d6e
PP
2142}
2143
2144static
939190b3 2145int64_t bfcr_get_sequence_length_cb(struct ctf_field_class *fc, void *data)
e98a2d6e 2146{
78cf9df6 2147 struct bt_field *seq_field;
839d52a5 2148 struct bt_notif_iter *notit = data;
939190b3 2149 struct ctf_field_class_sequence *seq_fc = (void *) fc;
7b33a0e0
PP
2150 int64_t length = -1;
2151 int ret;
e98a2d6e 2152
7b33a0e0 2153 length = (uint64_t) g_array_index(notit->stored_values, uint64_t,
939190b3 2154 seq_fc->stored_length_index);
fdf0e7a0 2155 seq_field = stack_top(notit->stack)->base;
7b33a0e0 2156 BT_ASSERT(seq_field);
78cf9df6 2157 ret = bt_field_dynamic_array_set_length(seq_field, (uint64_t) length);
7b33a0e0
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
7b33a0e0 2164 return length;
e98a2d6e
PP
2165}
2166
2167static
939190b3
PP
2168struct ctf_field_class *bfcr_borrow_variant_selected_field_class_cb(
2169 struct ctf_field_class *fc, void *data)
e98a2d6e 2170{
a6918753 2171 int ret;
7b33a0e0
PP
2172 uint64_t i;
2173 int64_t option_index = -1;
839d52a5 2174 struct bt_notif_iter *notit = data;
939190b3
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;
7b33a0e0
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,
939190b3 2185 var_fc->stored_tag_index);
e98a2d6e
PP
2186
2187 /*
7b33a0e0 2188 * Check each range to find the selected option's index.
e98a2d6e 2189 */
939190b3
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);
7b33a0e0
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 }
a6918753 2202 } else {
939190b3
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);
7b33a0e0
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 }
a6918753
PP
2214 }
2215
7b33a0e0 2216 if (option_index < 0) {
939190b3
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
939190b3
PP
2223 selected_option = ctf_field_class_variant_borrow_option_by_index(
2224 var_fc, (uint64_t) option_index);
e98a2d6e 2225
939190b3 2226 if (selected_option->fc->in_ir) {
78cf9df6 2227 struct bt_field *var_field = stack_top(notit->stack)->base;
1556a1af 2228
78cf9df6 2229 ret = bt_field_variant_select_option_field(
9e550e5f 2230 var_field, option_index);
8fc063a2 2231 if (ret) {
7b33a0e0
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 }
5184a1d6
PP
2238 }
2239
939190b3 2240 ret_fc = selected_option->fc;
8fc063a2 2241
5184a1d6 2242end:
939190b3 2243 return ret_fc;
7b33a0e0
PP
2244}
2245
2246static
2247void set_event_default_clock_value(struct bt_notif_iter *notit)
2248{
78cf9df6 2249 struct bt_event *event =
7b53201c 2250 bt_notification_event_borrow_event(
9e550e5f 2251 notit->event_notif);
78cf9df6 2252 struct bt_stream_class *sc = notit->meta.sc->ir_sc;
7b33a0e0
PP
2253
2254 BT_ASSERT(event);
2255
2256 if (bt_stream_class_borrow_default_clock_class(sc)) {
78cf9df6 2257 bt_event_set_default_clock_value(event,
7b33a0e0 2258 notit->default_clock_val);
7b33a0e0 2259 }
5184a1d6
PP
2260}
2261
6ff151ad
PP
2262static
2263void notify_new_stream(struct bt_notif_iter *notit,
7b53201c 2264 struct bt_notification **notification)
6ff151ad 2265{
a6918753 2266 enum bt_notif_iter_status status;
7b53201c 2267 struct bt_notification *ret = NULL;
6ff151ad 2268
a6918753
PP
2269 status = set_current_stream(notit);
2270 if (status != BT_NOTIF_ITER_STATUS_OK) {
8c6884d9 2271 BT_NOTIFICATION_PUT_REF_AND_RESET(ret);
6ff151ad
PP
2272 goto end;
2273 }
2274
2275 BT_ASSERT(notit->stream);
03e18d5d 2276 BT_ASSERT(notit->notif_iter);
e18f019b 2277 ret = bt_notification_stream_beginning_create(notit->notif_iter,
03e18d5d 2278 notit->stream);
6ff151ad
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,
7b53201c 2292 struct bt_notification **notification)
6ff151ad 2293{
7b53201c 2294 struct bt_notification *ret;
6ff151ad
PP
2295
2296 if (!notit->stream) {
2297 BT_LOGE("Cannot create stream for stream notification: "
2298 "notit-addr=%p", notit);
2299 return;
2300 }
2301
03e18d5d 2302 BT_ASSERT(notit->notif_iter);
7b53201c 2303 ret = bt_notification_stream_end_create(notit->notif_iter,
03e18d5d 2304 notit->stream);
6ff151ad
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
839d52a5 2315void notify_new_packet(struct bt_notif_iter *notit,
7b53201c 2316 struct bt_notification **notification)
e98a2d6e 2317{
a6918753
PP
2318 int ret;
2319 enum bt_notif_iter_status status;
7b53201c 2320 struct bt_notification *notif = NULL;
78cf9df6 2321 const struct bt_stream_class *sc;
e98a2d6e 2322
a6918753
PP
2323 status = set_current_packet(notit);
2324 if (status != BT_NOTIF_ITER_STATUS_OK) {
2325 goto end;
e98a2d6e
PP
2326 }
2327
a6918753 2328 BT_ASSERT(notit->packet);
78cf9df6 2329 sc = notit->meta.sc->ir_sc;
7b33a0e0
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));
78cf9df6 2334 bt_packet_set_discarded_event_counter_snapshot(
7b33a0e0 2335 notit->packet, notit->snapshots.discarded_events);
7b33a0e0
PP
2336 }
2337
2338 if (bt_stream_class_packets_have_packet_counter_snapshot(sc)) {
2339 BT_ASSERT(notit->snapshots.packets != UINT64_C(-1));
78cf9df6 2340 bt_packet_set_packet_counter_snapshot(
7b33a0e0 2341 notit->packet, notit->snapshots.packets);
7b33a0e0
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));
78cf9df6 2346 bt_packet_set_default_beginning_clock_value(
7b33a0e0 2347 notit->packet, notit->snapshots.beginning_clock);
7b33a0e0
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));
78cf9df6 2352 bt_packet_set_default_end_clock_value(
7b33a0e0 2353 notit->packet, notit->snapshots.end_clock);
7b33a0e0 2354 }
a6918753
PP
2355
2356 if (notit->packet_header_field) {
78cf9df6 2357 ret = bt_packet_move_header_field(
9e550e5f 2358 notit->packet, notit->packet_header_field);
a6918753
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 */
78cf9df6 2370 BT_ASSERT(bt_packet_borrow_header_field(
9e550e5f 2371 notit->packet) ==
a6918753
PP
2372 notit->dscopes.trace_packet_header);
2373 }
2374
2375 if (notit->packet_context_field) {
78cf9df6 2376 ret = bt_packet_move_context_field(
9e550e5f 2377 notit->packet, notit->packet_context_field);
a6918753
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 */
78cf9df6 2389 BT_ASSERT(bt_packet_borrow_context_field(
9e550e5f 2390 notit->packet) ==
a6918753
PP
2391 notit->dscopes.stream_packet_context);
2392 }
2393
03e18d5d 2394 BT_ASSERT(notit->notif_iter);
e18f019b 2395 notif = bt_notification_packet_beginning_create(notit->notif_iter,
f7c3ac09 2396 notit->packet);
a6918753 2397 if (!notif) {
fdf0e7a0
PP
2398 BT_LOGE("Cannot create packet beginning notification: "
2399 "notit-addr=%p, packet-addr=%p",
2400 notit, notit->packet);
8fc063a2 2401 goto end;
78586d8a 2402 }
a6918753 2403
a6918753 2404 *notification = notif;
8fc063a2
PP
2405
2406end:
2407 return;
e98a2d6e
PP
2408}
2409
78586d8a 2410static
839d52a5 2411void notify_end_of_packet(struct bt_notif_iter *notit,
7b53201c 2412 struct bt_notification **notification)
e98a2d6e 2413{
7b53201c 2414 struct bt_notification *notif;
e98a2d6e 2415
e98a2d6e 2416 if (!notit->packet) {
78586d8a 2417 return;
e98a2d6e
PP
2418 }
2419
7b33a0e0
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
03e18d5d 2425 BT_ASSERT(notit->notif_iter);
7b53201c 2426 notif = bt_notification_packet_end_create(notit->notif_iter,
03e18d5d 2427 notit->packet);
a6918753 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;
e5815ba2 2433
78586d8a 2434 }
e98a2d6e 2435
8c6884d9 2436 BT_PACKET_PUT_REF_AND_RESET(notit->packet);
a6918753 2437 *notification = notif;
e98a2d6e
PP
2438}
2439
c44c3e70 2440BT_HIDDEN
7b33a0e0 2441struct bt_notif_iter *bt_notif_iter_create(struct ctf_trace_class *tc,
e98a2d6e 2442 size_t max_request_sz,
839d52a5 2443 struct bt_notif_iter_medium_ops medops, void *data)
e98a2d6e 2444{
839d52a5 2445 struct bt_notif_iter *notit = NULL;
939190b3
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 = {
939190b3
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
7b33a0e0 2463 BT_ASSERT(tc);
8b45963b 2464 BT_ASSERT(medops.request_bytes);
a6918753 2465 BT_ASSERT(medops.borrow_stream);
fdf0e7a0 2466 BT_LOGD("Creating CTF plugin notification iterator: "
10b7a2e4
PP
2467 "trace-addr=%p, max-request-size=%zu, "
2468 "data=%p", tc, max_request_sz, data);
839d52a5 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 }
7b33a0e0 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);
7b33a0e0
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
939190b3
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
839d52a5 2493 bt_notif_iter_reset(notit);
fdf0e7a0 2494 BT_LOGD("Created CTF plugin notification iterator: "
10b7a2e4 2495 "trace-addr=%p, max-request-size=%zu, "
fdf0e7a0 2496 "data=%p, notit-addr=%p",
10b7a2e4 2497 tc, max_request_sz, data, notit);
bb230c9b 2498 notit->cur_packet_offset = 0;
fdf0e7a0 2499
e98a2d6e
PP
2500end:
2501 return notit;
fdf0e7a0 2502
c44c3e70 2503error:
839d52a5 2504 bt_notif_iter_destroy(notit);
c44c3e70
JG
2505 notit = NULL;
2506 goto end;
e98a2d6e
PP
2507}
2508
839d52a5 2509void bt_notif_iter_destroy(struct bt_notif_iter *notit)
e98a2d6e 2510{
8c6884d9
PP
2511 BT_PACKET_PUT_REF_AND_RESET(notit->packet);
2512 BT_STREAM_PUT_REF_AND_RESET(notit->stream);
a6918753 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
939190b3
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
7b33a0e0
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
839d52a5
PP
2534enum bt_notif_iter_status bt_notif_iter_get_next_notification(
2535 struct bt_notif_iter *notit,
834e9996 2536 struct bt_self_notification_iterator *notif_iter,
7b53201c 2537 struct bt_notification **notification)
e98a2d6e 2538{
839d52a5 2539 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
e98a2d6e 2540
8b45963b
PP
2541 BT_ASSERT(notit);
2542 BT_ASSERT(notification);
e98a2d6e 2543
f7c3ac09
PP
2544 if (notit->state == STATE_DONE) {
2545 status = BT_NOTIF_ITER_STATUS_EOF;
2546 goto end;
2547 }
2548
03e18d5d 2549 notit->notif_iter = notif_iter;
6ff151ad 2550
8fc063a2 2551 BT_LOGV("Getting next notification: notit-addr=%p", notit);
fdf0e7a0 2552
e98a2d6e
PP
2553 while (true) {
2554 status = handle_state(notit);
839d52a5
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 }
a6918753 2559
839d52a5
PP
2560 if (status != BT_NOTIF_ITER_STATUS_OK) {
2561 if (status == BT_NOTIF_ITER_STATUS_EOF) {
6ff151ad
PP
2562 enum state next_state = notit->state;
2563
839d52a5 2564 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_EOF.");
6ff151ad
PP
2565
2566 if (notit->packet) {
a6918753
PP
2567 notify_end_of_packet(notit,
2568 notification);
6ff151ad 2569 } else {
a6918753
PP
2570 notify_end_of_stream(notit,
2571 notification);
6ff151ad
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 }
a6918753 2587
e98a2d6e
PP
2588 goto end;
2589 }
2590
2591 switch (notit->state) {
6ff151ad
PP
2592 case STATE_EMIT_NOTIF_NEW_STREAM:
2593 /* notify_new_stream() logs errors */
2594 notify_new_stream(notit, notification);
7b33a0e0 2595
6ff151ad
PP
2596 if (!*notification) {
2597 status = BT_NOTIF_ITER_STATUS_ERROR;
2598 }
7b33a0e0 2599
6ff151ad
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);
7b33a0e0 2605
e98a2d6e 2606 if (!*notification) {
839d52a5 2607 status = BT_NOTIF_ITER_STATUS_ERROR;
e98a2d6e 2608 }
a6918753 2609
e98a2d6e
PP
2610 goto end;
2611 case STATE_EMIT_NOTIF_EVENT:
a6918753 2612 BT_ASSERT(notit->event_notif);
7b33a0e0 2613 set_event_default_clock_value(notit);
3fd7b79d
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);
7b33a0e0 2620
e98a2d6e 2621 if (!*notification) {
839d52a5 2622 status = BT_NOTIF_ITER_STATUS_ERROR;
e98a2d6e 2623 }
a6918753 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
a6918753 2637enum bt_notif_iter_status bt_notif_iter_borrow_packet_header_context_fields(
839d52a5 2638 struct bt_notif_iter *notit,
78cf9df6
PP
2639 struct bt_field **packet_header_field,
2640 struct bt_field **packet_context_field)
87187cbf 2641{
bb230c9b 2642 int ret;
839d52a5 2643 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
87187cbf 2644
8b45963b 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);
839d52a5
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 }
839d52a5
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:
6ff151ad 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:
a6918753
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) {
a6918753 2705 *packet_header_field = notit->dscopes.trace_packet_header;
87187cbf
PP
2706 }
2707
2708 if (packet_context_field) {
a6918753 2709 *packet_context_field = notit->dscopes.stream_packet_context;
87187cbf
PP
2710 }
2711
2712end:
2713 return status;
2714}
55fa6491
PP
2715
2716BT_HIDDEN
839d52a5 2717void bt_notif_iter_set_medops_data(struct bt_notif_iter *notit,
55fa6491
PP
2718 void *medops_data)
2719{
8b45963b 2720 BT_ASSERT(notit);
55fa6491
PP
2721 notit->medium.data = medops_data;
2722}
bb230c9b
JG
2723
2724BT_HIDDEN
839d52a5
PP
2725enum bt_notif_iter_status bt_notif_iter_seek(
2726 struct bt_notif_iter *notit, off_t offset)
bb230c9b 2727{
839d52a5
PP
2728 enum bt_notif_iter_status ret = BT_NOTIF_ITER_STATUS_OK;
2729 enum bt_notif_iter_medium_status medium_status;
bb230c9b 2730
8b45963b 2731 BT_ASSERT(notit);
bb230c9b
JG
2732 if (offset < 0) {
2733 BT_LOGE("Cannot seek to negative offset: offset=%jd", offset);
839d52a5 2734 ret = BT_NOTIF_ITER_STATUS_INVAL;
bb230c9b
JG
2735 goto end;
2736 }
2737
2738 if (!notit->medium.medops.seek) {
839d52a5 2739 ret = BT_NOTIF_ITER_STATUS_UNSUPPORTED;
bb230c9b
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(
7b33a0e0 2745 BT_NOTIF_ITER_SEEK_WHENCE_SET, offset, notit->medium.data);
839d52a5
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;
bb230c9b 2749 } else {
839d52a5 2750 ret = BT_NOTIF_ITER_STATUS_ERROR;
bb230c9b
JG
2751 goto end;
2752 }
2753 }
2754
839d52a5 2755 bt_notif_iter_reset(notit);
bb230c9b 2756 notit->cur_packet_offset = offset;
7b33a0e0 2757
bb230c9b
JG
2758end:
2759 return ret;
2760}
2761
2762BT_HIDDEN
7b33a0e0 2763off_t bt_notif_iter_get_current_packet_offset(struct bt_notif_iter *notit)
bb230c9b 2764{
8b45963b 2765 BT_ASSERT(notit);
bb230c9b
JG
2766 return notit->cur_packet_offset;
2767}
2768
2769BT_HIDDEN
839d52a5
PP
2770off_t bt_notif_iter_get_current_packet_size(
2771 struct bt_notif_iter *notit)
bb230c9b 2772{
8b45963b 2773 BT_ASSERT(notit);
7b33a0e0
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;
bb230c9b 2806}
This page took 0.203321 seconds and 4 git commands to generate.