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