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