Fix: variable declaration shadows previously declared variable
[babeltrace.git] / plugins / ctf / common / notif-iter / notif-iter.c
CommitLineData
e98a2d6e
PP
1/*
2 * Babeltrace - CTF notification iterator
06a626b8 3 *
e98a2d6e
PP
4 * Copyright (c) 2015-2016 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2015-2016 Philippe Proulx <pproulx@efficios.com>
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>
34#include <assert.h>
35#include <string.h>
e0831b38 36#include <babeltrace/babeltrace.h>
fdf0e7a0 37#include <babeltrace/ctf-ir/field-types-internal.h>
fdf0e7a0 38#include <babeltrace/ctf-ir/field-path-internal.h>
e98a2d6e 39#include <glib.h>
0fbb9a9f 40#include <stdlib.h>
e98a2d6e 41
06a626b8
JG
42#include "notif-iter.h"
43#include "../btr/btr.h"
e98a2d6e 44
e98a2d6e
PP
45struct bt_ctf_notif_iter;
46
47/* A visit stack entry */
48struct stack_entry {
49 /*
50 * Current base field, one of:
51 *
52 * * string
53 * * structure
54 * * array
55 * * sequence
56 * * variant
57 *
58 * Field is owned by this.
59 */
60 struct bt_ctf_field *base;
61
62 /* index of next field to set */
63 size_t index;
64};
65
66/* Visit stack */
67struct stack {
68 /* Entries (struct stack_entry *) (top is last element) */
69 GPtrArray *entries;
e98a2d6e
PP
70};
71
72/* State */
73enum state {
74 STATE_INIT,
75 STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN,
76 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE,
77 STATE_AFTER_TRACE_PACKET_HEADER,
78 STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN,
79 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE,
80 STATE_AFTER_STREAM_PACKET_CONTEXT,
81 STATE_EMIT_NOTIF_NEW_PACKET,
82 STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN,
83 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE,
84 STATE_AFTER_STREAM_EVENT_HEADER,
85 STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN,
86 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE,
87 STATE_DSCOPE_EVENT_CONTEXT_BEGIN,
88 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE,
89 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN,
90 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE,
91 STATE_EMIT_NOTIF_EVENT,
92 STATE_EMIT_NOTIF_END_OF_PACKET,
93 STATE_SKIP_PACKET_PADDING,
94};
95
5f870343
JG
96struct trace_field_path_cache {
97 /*
98 * Indexes of the stream_id and stream_instance_id field in the packet
99 * header structure, -1 if unset.
100 */
101 int stream_id;
102 int stream_instance_id;
103};
104
105struct stream_class_field_path_cache {
106 /*
107 * Indexes of the v and id fields in the stream event header structure,
108 * -1 if unset.
109 */
110 int v;
111 int id;
112
113 /*
114 * index of the timestamp_end, packet_size and content_size fields in
115 * the stream packet context structure. Set to -1 if the fields were
116 * not found.
117 */
118 int timestamp_end;
119 int packet_size;
120 int content_size;
121};
122
123struct field_cb_override {
124 enum bt_ctf_btr_status (* func)(void *value,
125 struct bt_ctf_field_type *type, void *data);
126 void *data;
127};
128
e98a2d6e
PP
129/* CTF notification iterator */
130struct bt_ctf_notif_iter {
131 /* Visit stack */
132 struct stack *stack;
133
e98a2d6e
PP
134 /*
135 * Current dynamic scope field pointer.
136 *
137 * This is set when a dynamic scope field is first created by
138 * btr_compound_begin_cb(). It points to one of the fields in
139 * dscopes below.
140 */
141 struct bt_ctf_field **cur_dscope_field;
142
143 /* Trace and classes (owned by this) */
144 struct {
145 struct bt_ctf_trace *trace;
146 struct bt_ctf_stream_class *stream_class;
147 struct bt_ctf_event_class *event_class;
148 } meta;
149
150 /* Current packet (NULL if not created yet) */
151 struct bt_ctf_packet *packet;
152
5184a1d6
PP
153 /* Current stream (NULL if not set yet) */
154 struct bt_ctf_stream *stream;
155
5f870343
JG
156 /*
157 * Current timestamp_end field (to consider before switching packets).
158 */
159 struct bt_ctf_field *cur_timestamp_end;
160
e98a2d6e
PP
161 /* Database of current dynamic scopes (owned by this) */
162 struct {
163 struct bt_ctf_field *trace_packet_header;
164 struct bt_ctf_field *stream_packet_context;
165 struct bt_ctf_field *stream_event_header;
166 struct bt_ctf_field *stream_event_context;
167 struct bt_ctf_field *event_context;
168 struct bt_ctf_field *event_payload;
169 } dscopes;
170
5f870343
JG
171 /*
172 * Special field overrides.
173 *
174 * Overrides are used to implement the behaviours of special fields such
175 * as "timestamp_end" (which must be ignored until the end of the
176 * packet), "id" (event id) which can be present multiple times and must
177 * be updated multiple time.
178 *
179 * This should be used to implement the behaviour of integer fields
180 * mapped to clocks and other "tagged" fields (in CTF 2).
181 *
182 * bt_ctf_field_type to struct field_cb_override
183 */
184 GHashTable *field_overrides;
185
e98a2d6e
PP
186 /* Current state */
187 enum state state;
188
2cf1d51e 189 /* Current medium buffer data */
e98a2d6e
PP
190 struct {
191 /* Last address provided by medium */
192 const uint8_t *addr;
193
194 /* Buffer size provided by medium (bytes) */
195 size_t sz;
196
197 /* Offset within whole packet of addr (bits) */
198 size_t packet_offset;
199
200 /* Current position from addr (bits) */
201 size_t at;
9b876b7b
PP
202
203 /* Position of the last event header from addr (bits) */
204 size_t last_eh_at;
e98a2d6e
PP
205 } buf;
206
207 /* Binary type reader */
208 struct bt_ctf_btr *btr;
209
2cf1d51e 210 /* Current medium data */
e98a2d6e
PP
211 struct {
212 struct bt_ctf_notif_iter_medium_ops medops;
213 size_t max_request_sz;
214 void *data;
215 } medium;
216
217 /* Current packet size (bits) (-1 if unknown) */
e7a4393b 218 int64_t cur_packet_size;
e98a2d6e
PP
219
220 /* Current content size (bits) (-1 if unknown) */
e7a4393b 221 int64_t cur_content_size;
c44c3e70 222
bb230c9b
JG
223 /*
224 * Offset, in the underlying media, of the current packet's start
225 * (-1 if unknown).
226 */
227 off_t cur_packet_offset;
228
ac0c6bdd 229 /* bt_ctf_clock_class to uint64_t. */
c44c3e70 230 GHashTable *clock_states;
5f870343
JG
231
232 /*
233 * Cache of the trace-constant field paths (event header type)
234 * associated to the current trace.
235 */
236 struct trace_field_path_cache trace_field_path_cache;
237
238 /*
239 * Field path cache associated with the current stream class.
240 * Ownership of this structure belongs to the field_path_caches HT.
241 */
242 struct stream_class_field_path_cache *cur_sc_field_path_cache;
243
244 /* bt_ctf_stream_class to struct stream_class_field_path_cache. */
245 GHashTable *sc_field_path_caches;
e98a2d6e
PP
246};
247
fdf0e7a0
PP
248static inline
249const char *state_string(enum state state)
250{
251 switch (state) {
252 case STATE_INIT:
253 return "STATE_INIT";
254 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
255 return "STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN";
256 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
257 return "STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE";
258 case STATE_AFTER_TRACE_PACKET_HEADER:
259 return "STATE_AFTER_TRACE_PACKET_HEADER";
260 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN:
261 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN";
262 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE:
263 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE";
264 case STATE_AFTER_STREAM_PACKET_CONTEXT:
265 return "STATE_AFTER_STREAM_PACKET_CONTEXT";
266 case STATE_EMIT_NOTIF_NEW_PACKET:
267 return "STATE_EMIT_NOTIF_NEW_PACKET";
268 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN:
269 return "STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN";
270 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE:
271 return "STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE";
272 case STATE_AFTER_STREAM_EVENT_HEADER:
273 return "STATE_AFTER_STREAM_EVENT_HEADER";
274 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN:
275 return "STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN";
276 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE:
277 return "STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE";
278 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN:
279 return "STATE_DSCOPE_EVENT_CONTEXT_BEGIN";
280 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE:
281 return "STATE_DSCOPE_EVENT_CONTEXT_CONTINUE";
282 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN:
283 return "STATE_DSCOPE_EVENT_PAYLOAD_BEGIN";
284 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE:
285 return "STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE";
286 case STATE_EMIT_NOTIF_EVENT:
287 return "STATE_EMIT_NOTIF_EVENT";
288 case STATE_EMIT_NOTIF_END_OF_PACKET:
289 return "STATE_EMIT_NOTIF_END_OF_PACKET";
290 case STATE_SKIP_PACKET_PADDING:
291 return "STATE_SKIP_PACKET_PADDING";
292 default:
293 return "(unknown)";
294 }
295}
296
2cf1d51e
JG
297static
298int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter *notit);
299
5f870343
JG
300static
301enum bt_ctf_btr_status btr_timestamp_end_cb(void *value,
302 struct bt_ctf_field_type *type, void *data);
303
e98a2d6e
PP
304static
305void stack_entry_free_func(gpointer data)
306{
307 struct stack_entry *entry = data;
308
309 bt_put(entry->base);
310 g_free(entry);
311}
312
313static
314struct stack *stack_new(struct bt_ctf_notif_iter *notit)
315{
316 struct stack *stack = NULL;
317
318 stack = g_new0(struct stack, 1);
319 if (!stack) {
fdf0e7a0 320 BT_LOGE_STR("Failed to allocate one stack.");
e98a2d6e
PP
321 goto error;
322 }
323
324 stack->entries = g_ptr_array_new_with_free_func(stack_entry_free_func);
325 if (!stack->entries) {
fdf0e7a0 326 BT_LOGE_STR("Failed to allocate a GPtrArray.");
e98a2d6e
PP
327 goto error;
328 }
329
fdf0e7a0 330 BT_LOGD("Created stack: notit-addr=%p, stack-addr=%p", notit, stack);
e98a2d6e 331 return stack;
fdf0e7a0 332
e98a2d6e
PP
333error:
334 g_free(stack);
e98a2d6e
PP
335 return NULL;
336}
337
338static
339void stack_destroy(struct stack *stack)
340{
341 assert(stack);
fdf0e7a0 342 BT_LOGD("Destroying stack: addr=%p", stack);
e98a2d6e
PP
343 g_ptr_array_free(stack->entries, TRUE);
344 g_free(stack);
345}
346
347static
348int stack_push(struct stack *stack, struct bt_ctf_field *base)
349{
350 int ret = 0;
351 struct stack_entry *entry;
e98a2d6e
PP
352
353 assert(stack);
354 assert(base);
fdf0e7a0
PP
355 BT_LOGV("Pushing base field on stack: stack-addr=%p, "
356 "stack-size-before=%u, stack-size-after=%u",
357 stack, stack->entries->len, stack->entries->len + 1);
e98a2d6e
PP
358 entry = g_new0(struct stack_entry, 1);
359 if (!entry) {
fdf0e7a0 360 BT_LOGE_STR("Failed to allocate one stack entry.");
e98a2d6e
PP
361 ret = -1;
362 goto end;
363 }
364
365 entry->base = bt_get(base);
366 g_ptr_array_add(stack->entries, entry);
367
368end:
369 return ret;
370}
371
372static inline
373unsigned int stack_size(struct stack *stack)
374{
375 assert(stack);
376
377 return stack->entries->len;
378}
379
380static
381void stack_pop(struct stack *stack)
382{
383 assert(stack);
384 assert(stack_size(stack));
fdf0e7a0
PP
385 BT_LOGV("Popping from stack: "
386 "stack-addr=%p, stack-size-before=%u, stack-size-after=%u",
387 stack, stack->entries->len, stack->entries->len - 1);
e98a2d6e
PP
388 g_ptr_array_remove_index(stack->entries, stack->entries->len - 1);
389}
390
391static inline
392struct stack_entry *stack_top(struct stack *stack)
393{
394 assert(stack);
395 assert(stack_size(stack));
396
397 return g_ptr_array_index(stack->entries, stack->entries->len - 1);
398}
399
400static inline
401bool stack_empty(struct stack *stack)
402{
403 return stack_size(stack) == 0;
404}
405
406static
407void stack_clear(struct stack *stack)
408{
409 assert(stack);
410
411 if (!stack_empty(stack)) {
fdf0e7a0
PP
412 BT_LOGV("Clearing stack: stack-addr=%p, stack-size=%u",
413 stack, stack->entries->len);
e98a2d6e
PP
414 g_ptr_array_remove_range(stack->entries, 0, stack_size(stack));
415 }
416
417 assert(stack_empty(stack));
418}
419
420static inline
421enum bt_ctf_notif_iter_status notif_iter_status_from_m_status(
422 enum bt_ctf_notif_iter_medium_status m_status)
423{
dc77b521 424 return (int) m_status;
e98a2d6e
PP
425}
426
427static inline
428size_t buf_size_bits(struct bt_ctf_notif_iter *notit)
429{
fdf0e7a0 430 return notit->buf.sz * 8;
e98a2d6e
PP
431}
432
433static inline
434size_t buf_available_bits(struct bt_ctf_notif_iter *notit)
435{
436 return buf_size_bits(notit) - notit->buf.at;
437}
438
439static inline
440size_t packet_at(struct bt_ctf_notif_iter *notit)
441{
442 return notit->buf.packet_offset + notit->buf.at;
443}
444
e98a2d6e
PP
445static inline
446void buf_consume_bits(struct bt_ctf_notif_iter *notit, size_t incr)
447{
fdf0e7a0
PP
448 BT_LOGV("Advancing cursor: notit-addr=%p, cur-before=%zu, cur-after=%zu",
449 notit, notit->buf.at, notit->buf.at + incr);
e98a2d6e
PP
450 notit->buf.at += incr;
451}
452
e98a2d6e 453static
2cf1d51e
JG
454enum bt_ctf_notif_iter_status request_medium_bytes(
455 struct bt_ctf_notif_iter *notit)
e98a2d6e 456{
fdf0e7a0
PP
457 uint8_t *buffer_addr = NULL;
458 size_t buffer_sz = 0;
e98a2d6e
PP
459 enum bt_ctf_notif_iter_medium_status m_status;
460
fdf0e7a0
PP
461 BT_LOGV("Calling user function (request bytes): notit-addr=%p, "
462 "request-size=%zu", notit, notit->medium.max_request_sz);
e98a2d6e
PP
463 m_status = notit->medium.medops.request_bytes(
464 notit->medium.max_request_sz, &buffer_addr,
465 &buffer_sz, notit->medium.data);
fdf0e7a0
PP
466 BT_LOGV("User function returned: status=%s, buf-addr=%p, buf-size=%zu",
467 bt_ctf_notif_iter_medium_status_string(m_status),
468 buffer_addr, buffer_sz);
e98a2d6e
PP
469 if (m_status == BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK) {
470 assert(buffer_sz != 0);
471
472 /* New packet offset is old one + old size (in bits) */
473 notit->buf.packet_offset += buf_size_bits(notit);
474
475 /* Restart at the beginning of the new medium buffer */
476 notit->buf.at = 0;
bb8744a7 477 notit->buf.last_eh_at = SIZE_MAX;
e98a2d6e
PP
478
479 /* New medium buffer size */
480 notit->buf.sz = buffer_sz;
481
482 /* New medium buffer address */
483 notit->buf.addr = buffer_addr;
fdf0e7a0
PP
484
485 BT_LOGV("User function returned new bytes: "
486 "packet-offset=%zu, cur=%zu, size=%zu, addr=%p",
487 notit->buf.packet_offset, notit->buf.at,
488 notit->buf.sz, notit->buf.addr);
489 BT_LOGV_MEM(buffer_addr, buffer_sz, "Returned bytes at %p:",
490 buffer_addr);
7116f6c0 491 } else if (m_status == BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF) {
0b7ec632
PP
492 struct bt_ctf_field_type *ph_ft =
493 bt_ctf_trace_get_packet_header_type(notit->meta.trace);
7116f6c0
PP
494 struct bt_ctf_field_type *eh_ft = NULL;
495 struct bt_ctf_field_type *sec_ft = NULL;
496 struct bt_ctf_field_type *ec_ft = NULL;
497
498 /*
499 * User returned end of stream: validate that we're not
500 * in the middle of a packet header, packet context, or
501 * event.
502 */
503 if (notit->state == STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN) {
504 /* Beginning of packet: always valid */
505 goto good_state;
506 }
507
508 if (!notit->meta.stream_class) {
509 goto bad_state;
510 }
511
0b7ec632
PP
512 if (notit->state == STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN) {
513 /*
514 * Beginning of packet context context is only
515 * valid if there's no packet header.
516 */
517 if (!ph_ft) {
518 goto good_state;
519 }
520 }
521
7116f6c0
PP
522 eh_ft = bt_ctf_stream_class_get_event_header_type(
523 notit->meta.stream_class);
524 sec_ft = bt_ctf_stream_class_get_event_context_type(
525 notit->meta.stream_class);
526
527 if (notit->state == STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN) {
528 /*
529 * Beginning of event's header is only valid if
530 * the packet is not supposed to have a specific
531 * size (whole packet sequence has in fact only
532 * one packet).
533 */
534 if (notit->cur_packet_size == -1) {
535 goto good_state;
536 }
537 }
538
539 if (notit->state == STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN) {
540 /*
541 * Beginning of event's stream event context is
542 * only valid if the packet is not supposed to
543 * have a specific size (whole packet sequence
544 * has in fact only one packet), and there's no
545 * event header.
546 */
547 if (notit->cur_packet_size == -1 && !eh_ft) {
548 goto good_state;
549 }
550 }
551
552 if (!notit->meta.event_class) {
553 goto bad_state;
554 }
555
556 ec_ft = bt_ctf_event_class_get_context_type(
557 notit->meta.event_class);
558
559 if (notit->state == STATE_DSCOPE_EVENT_CONTEXT_BEGIN) {
560 /*
561 * Beginning of event's context is only valid if
562 * the packet is not supposed to have a specific
563 * size (whole packet sequence has in fact only
564 * one packet), and there's no event header and
565 * no stream event context.
566 */
567 if (notit->cur_packet_size == -1 && !eh_ft && !sec_ft) {
568 goto good_state;
569 }
570 }
571
572 if (notit->state == STATE_DSCOPE_EVENT_PAYLOAD_BEGIN) {
573 /*
574 * Beginning of event's context is only valid if
575 * the packet is not supposed to have a specific
576 * size (whole packet sequence has in fact only
577 * one packet), and there's no event header, no
578 * stream event context, and no event context.
579 */
580 if (notit->cur_packet_size == -1 && !eh_ft && !sec_ft &&
581 !ec_ft) {
582 goto good_state;
583 }
584 }
585
586bad_state:
587 /* All other states are invalid */
588 BT_LOGW("User function returned %s, but notification iterator is in an unexpected state: "
589 "state=%s",
590 bt_ctf_notif_iter_medium_status_string(m_status),
591 state_string(notit->state));
592 m_status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR;
593
594good_state:
0b7ec632 595 bt_put(ph_ft);
7116f6c0
PP
596 bt_put(eh_ft);
597 bt_put(sec_ft);
598 bt_put(ec_ft);
fdf0e7a0
PP
599 } else if (m_status < 0) {
600 BT_LOGW("User function failed: status=%s",
601 bt_ctf_notif_iter_medium_status_string(m_status));
e98a2d6e
PP
602 }
603
604 return notif_iter_status_from_m_status(m_status);
605}
606
607static inline
608enum bt_ctf_notif_iter_status buf_ensure_available_bits(
609 struct bt_ctf_notif_iter *notit)
610{
611 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
612
613 if (buf_available_bits(notit) == 0) {
614 /*
615 * This _cannot_ return BT_CTF_NOTIF_ITER_STATUS_OK
616 * _and_ no bits.
617 */
618 status = request_medium_bytes(notit);
619 }
620
621 return status;
622}
623
624static
625enum bt_ctf_notif_iter_status read_dscope_begin_state(
626 struct bt_ctf_notif_iter *notit,
627 struct bt_ctf_field_type *dscope_field_type,
628 enum state done_state, enum state continue_state,
629 struct bt_ctf_field **dscope_field)
630{
631 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
632 enum bt_ctf_btr_status btr_status;
633 size_t consumed_bits;
634
635 status = buf_ensure_available_bits(notit);
636 if (status != BT_CTF_NOTIF_ITER_STATUS_OK) {
fdf0e7a0
PP
637 if (status < 0) {
638 BT_LOGW("Cannot ensure that buffer has at least one byte: "
639 "notif-addr=%p, status=%s",
640 notit, bt_ctf_notif_iter_status_string(status));
641 } else {
642 BT_LOGV("Cannot ensure that buffer has at least one byte: "
643 "notif-addr=%p, status=%s",
644 notit, bt_ctf_notif_iter_status_string(status));
645 }
646
e98a2d6e
PP
647 goto end;
648 }
649
650 bt_put(*dscope_field);
651 notit->cur_dscope_field = dscope_field;
fdf0e7a0
PP
652 BT_LOGV("Starting BTR: notit-addr=%p, btr-addr=%p, ft-addr=%p",
653 notit, notit->btr, dscope_field_type);
e98a2d6e
PP
654 consumed_bits = bt_ctf_btr_start(notit->btr, dscope_field_type,
655 notit->buf.addr, notit->buf.at, packet_at(notit),
656 notit->buf.sz, &btr_status);
fdf0e7a0 657 BT_LOGV("BTR consumed bits: size=%zu", consumed_bits);
e98a2d6e
PP
658
659 switch (btr_status) {
660 case BT_CTF_BTR_STATUS_OK:
661 /* type was read completely */
fdf0e7a0 662 BT_LOGV_STR("Field was completely decoded.");
e98a2d6e
PP
663 notit->state = done_state;
664 break;
665 case BT_CTF_BTR_STATUS_EOF:
fdf0e7a0 666 BT_LOGV_STR("BTR needs more data to decode field completely.");
e98a2d6e
PP
667 notit->state = continue_state;
668 break;
669 default:
fdf0e7a0
PP
670 BT_LOGW("BTR failed to start: notit-addr=%p, btr-addr=%p, "
671 "status=%s", notit, notit->btr,
672 bt_ctf_btr_status_string(btr_status));
e98a2d6e
PP
673 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
674 goto end;
675 }
676
677 /* Consume bits now since we know we're not in an error state */
678 buf_consume_bits(notit, consumed_bits);
679
680end:
681 return status;
682}
683
684static
685enum bt_ctf_notif_iter_status read_dscope_continue_state(
686 struct bt_ctf_notif_iter *notit, enum state done_state)
687{
688 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
689 enum bt_ctf_btr_status btr_status;
690 size_t consumed_bits;
691
692 status = buf_ensure_available_bits(notit);
693 if (status != BT_CTF_NOTIF_ITER_STATUS_OK) {
fdf0e7a0
PP
694 if (status < 0) {
695 BT_LOGW("Cannot ensure that buffer has at least one byte: "
696 "notif-addr=%p, status=%s",
697 notit, bt_ctf_notif_iter_status_string(status));
698 } else {
699 BT_LOGV("Cannot ensure that buffer has at least one byte: "
700 "notif-addr=%p, status=%s",
701 notit, bt_ctf_notif_iter_status_string(status));
702 }
703
e98a2d6e
PP
704 goto end;
705 }
706
fdf0e7a0
PP
707 BT_LOGV("Continuing BTR: notit-addr=%p, btr-addr=%p",
708 notit, notit->btr);
e98a2d6e 709 consumed_bits = bt_ctf_btr_continue(notit->btr, notit->buf.addr,
fdf0e7a0
PP
710 notit->buf.sz, &btr_status);
711 BT_LOGV("BTR consumed bits: size=%zu", consumed_bits);
e98a2d6e
PP
712
713 switch (btr_status) {
714 case BT_CTF_BTR_STATUS_OK:
78586d8a 715 /* Type was read completely. */
fdf0e7a0 716 BT_LOGV_STR("Field was completely decoded.");
e98a2d6e
PP
717 notit->state = done_state;
718 break;
719 case BT_CTF_BTR_STATUS_EOF:
78586d8a 720 /* Stay in this continue state. */
fdf0e7a0 721 BT_LOGV_STR("BTR needs more data to decode field completely.");
e98a2d6e
PP
722 break;
723 default:
fdf0e7a0
PP
724 BT_LOGW("BTR failed to continue: notit-addr=%p, btr-addr=%p, "
725 "status=%s", notit, notit->btr,
726 bt_ctf_btr_status_string(btr_status));
e98a2d6e
PP
727 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
728 goto end;
729 }
730
78586d8a 731 /* Consume bits now since we know we're not in an error state. */
e98a2d6e 732 buf_consume_bits(notit, consumed_bits);
e98a2d6e
PP
733end:
734 return status;
735}
736
737static
738void put_event_dscopes(struct bt_ctf_notif_iter *notit)
739{
fdf0e7a0 740 BT_LOGV_STR("Putting event header field.");
e98a2d6e 741 BT_PUT(notit->dscopes.stream_event_header);
fdf0e7a0 742 BT_LOGV_STR("Putting stream event context field.");
e98a2d6e 743 BT_PUT(notit->dscopes.stream_event_context);
fdf0e7a0 744 BT_LOGV_STR("Putting event context field.");
e98a2d6e 745 BT_PUT(notit->dscopes.event_context);
fdf0e7a0 746 BT_LOGV_STR("Putting event payload field.");
e98a2d6e
PP
747 BT_PUT(notit->dscopes.event_payload);
748}
749
750static
751void put_all_dscopes(struct bt_ctf_notif_iter *notit)
752{
fdf0e7a0 753 BT_LOGV_STR("Putting packet header field.");
e98a2d6e 754 BT_PUT(notit->dscopes.trace_packet_header);
fdf0e7a0 755 BT_LOGV_STR("Putting packet context field.");
e98a2d6e
PP
756 BT_PUT(notit->dscopes.stream_packet_context);
757 put_event_dscopes(notit);
758}
759
760static
761enum bt_ctf_notif_iter_status read_packet_header_begin_state(
762 struct bt_ctf_notif_iter *notit)
763{
2cf1d51e
JG
764 struct bt_ctf_field_type *packet_header_type = NULL;
765 enum bt_ctf_notif_iter_status ret = BT_CTF_NOTIF_ITER_STATUS_OK;
e98a2d6e 766
2cf1d51e 767 if (bt_ctf_notif_iter_switch_packet(notit)) {
fdf0e7a0 768 BT_LOGW("Cannot switch packet: notit-addr=%p", notit);
2cf1d51e
JG
769 ret = BT_CTF_NOTIF_ITER_STATUS_ERROR;
770 goto end;
771 }
e98a2d6e 772
2cf1d51e 773 /* Packet header type is common to the whole trace. */
e98a2d6e 774 packet_header_type = bt_ctf_trace_get_packet_header_type(
78586d8a 775 notit->meta.trace);
e98a2d6e 776 if (!packet_header_type) {
835b2d10 777 notit->state = STATE_AFTER_TRACE_PACKET_HEADER;
e98a2d6e
PP
778 goto end;
779 }
780
fdf0e7a0
PP
781 BT_LOGV("Decoding packet header field:"
782 "notit-addr=%p, trace-addr=%p, trace-name=\"%s\", ft-addr=%p",
783 notit, notit->meta.trace,
784 bt_ctf_trace_get_name(notit->meta.trace), packet_header_type);
2cf1d51e 785 ret = read_dscope_begin_state(notit, packet_header_type,
fdf0e7a0
PP
786 STATE_AFTER_TRACE_PACKET_HEADER,
787 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE,
788 &notit->dscopes.trace_packet_header);
789 if (ret < 0) {
790 BT_LOGW("Cannot decode packet header field: "
791 "notit-addr=%p, trace-addr=%p, "
792 "trace-name=\"%s\", ft-addr=%p",
793 notit, notit->meta.trace,
794 bt_ctf_trace_get_name(notit->meta.trace),
795 packet_header_type);
796 }
e98a2d6e
PP
797end:
798 BT_PUT(packet_header_type);
2cf1d51e 799 return ret;
e98a2d6e
PP
800}
801
802static
803enum bt_ctf_notif_iter_status read_packet_header_continue_state(
804 struct bt_ctf_notif_iter *notit)
805{
806 return read_dscope_continue_state(notit,
78586d8a 807 STATE_AFTER_TRACE_PACKET_HEADER);
e98a2d6e
PP
808}
809
810static inline
811bool is_struct_type(struct bt_ctf_field_type *field_type)
812{
78586d8a 813 return bt_ctf_field_type_get_type_id(field_type) ==
1487a16a 814 BT_CTF_FIELD_TYPE_ID_STRUCT;
e98a2d6e
PP
815}
816
817static inline
818bool is_variant_type(struct bt_ctf_field_type *field_type)
819{
78586d8a 820 return bt_ctf_field_type_get_type_id(field_type) ==
1487a16a 821 BT_CTF_FIELD_TYPE_ID_VARIANT;
e98a2d6e
PP
822}
823
5f870343
JG
824static
825struct stream_class_field_path_cache *
826create_stream_class_field_path_cache_entry(
827 struct bt_ctf_notif_iter *notit,
828 struct bt_ctf_stream_class *stream_class)
829{
830 int v = -1;
831 int id = -1;
832 int timestamp_end = -1;
833 int packet_size = -1;
834 int content_size = -1;
835 struct stream_class_field_path_cache *cache_entry = g_new0(
836 struct stream_class_field_path_cache, 1);
837 struct bt_ctf_field_type *event_header = NULL, *packet_context = NULL;
838
839 if (!cache_entry) {
fdf0e7a0 840 BT_LOGE_STR("Failed to allocate one stream class field path cache.");
5f870343
JG
841 goto end;
842 }
843
844 event_header = bt_ctf_stream_class_get_event_header_type(stream_class);
845 if (event_header && bt_ctf_field_type_is_structure(event_header)) {
846 int i, count;
847
848 count = bt_ctf_field_type_structure_get_field_count(
fdf0e7a0
PP
849 event_header);
850 assert(count >= 0);
851
5f870343
JG
852 for (i = 0; i < count; i++) {
853 int ret;
854 const char *name;
855
856 ret = bt_ctf_field_type_structure_get_field(
857 event_header, &name, NULL, i);
858 if (ret) {
fdf0e7a0
PP
859 BT_LOGE("Cannot get event header structure field type's field: "
860 "notit-addr=%p, stream-class-addr=%p, "
861 "stream-class-name=\"%s\", "
862 "stream-class-id=%" PRId64 ", "
863 "ft-addr=%p, index=%d",
864 notit, stream_class,
865 bt_ctf_stream_class_get_name(stream_class),
866 bt_ctf_stream_class_get_id(stream_class),
867 event_header, i);
5f870343
JG
868 goto error;
869 }
870
871 if (v != -1 && id != -1) {
872 break;
873 }
fdf0e7a0
PP
874
875 if (v == -1 && strcmp(name, "v") == 0) {
5f870343
JG
876 v = i;
877 } else if (id == -1 && !strcmp(name, "id")) {
878 id = i;
879 }
880 }
881 }
882
883 packet_context = bt_ctf_stream_class_get_packet_context_type(
884 stream_class);
885 if (packet_context && bt_ctf_field_type_is_structure(packet_context)) {
886 int i, count;
887
888 count = bt_ctf_field_type_structure_get_field_count(
fdf0e7a0
PP
889 packet_context);
890 assert(count >= 0);
891
5f870343
JG
892 for (i = 0; i < count; i++) {
893 int ret;
894 const char *name;
895 struct bt_ctf_field_type *field_type;
896
897 if (timestamp_end != -1 && packet_size != -1 &&
898 content_size != -1) {
899 break;
900 }
901
902 ret = bt_ctf_field_type_structure_get_field(
903 packet_context, &name, &field_type, i);
904 if (ret) {
fdf0e7a0
PP
905 BT_LOGE("Cannot get packet context structure field type's field: "
906 "notit-addr=%p, stream-class-addr=%p, "
907 "stream-class-name=\"%s\", "
908 "stream-class-id=%" PRId64 ", "
909 "ft-addr=%p, index=%d",
910 notit, stream_class,
911 bt_ctf_stream_class_get_name(stream_class),
912 bt_ctf_stream_class_get_id(stream_class),
913 event_header, i);
5f870343
JG
914 goto error;
915 }
916
917 if (timestamp_end == -1 &&
fdf0e7a0 918 strcmp(name, "timestamp_end") == 0) {
5f870343
JG
919 struct field_cb_override *override = g_new0(
920 struct field_cb_override, 1);
921
922 if (!override) {
923 BT_PUT(field_type);
924 goto error;
925 }
926
927 override->func = btr_timestamp_end_cb;
928 override->data = notit;
5f870343 929 g_hash_table_insert(notit->field_overrides,
fdf0e7a0 930 bt_get(field_type), override);
5f870343
JG
931 timestamp_end = i;
932 } else if (packet_size == -1 &&
933 !strcmp(name, "packet_size")) {
934 packet_size = i;
935 } else if (content_size == -1 &&
936 !strcmp(name, "content_size")) {
937 content_size = i;
938 }
939 BT_PUT(field_type);
940 }
941 }
942
943 cache_entry->v = v;
944 cache_entry->id = id;
945 cache_entry->timestamp_end = timestamp_end;
946 cache_entry->packet_size = packet_size;
947 cache_entry->content_size = content_size;
948end:
949 BT_PUT(event_header);
950 BT_PUT(packet_context);
951 return cache_entry;
952error:
953 g_free(cache_entry);
954 cache_entry = NULL;
955 goto end;
956}
957
958static
959struct stream_class_field_path_cache *get_stream_class_field_path_cache(
960 struct bt_ctf_notif_iter *notit,
961 struct bt_ctf_stream_class *stream_class)
962{
963 bool cache_entry_found;
964 struct stream_class_field_path_cache *cache_entry;
965
966 cache_entry_found = g_hash_table_lookup_extended(
967 notit->sc_field_path_caches,
968 stream_class, NULL, (gpointer) &cache_entry);
969 if (unlikely(!cache_entry_found)) {
970 cache_entry = create_stream_class_field_path_cache_entry(notit,
fdf0e7a0 971 stream_class);
5f870343 972 g_hash_table_insert(notit->sc_field_path_caches,
fdf0e7a0 973 bt_get(stream_class), (gpointer) cache_entry);
5f870343
JG
974 }
975
976 return cache_entry;
977}
978
e98a2d6e 979static inline
835b2d10
JG
980enum bt_ctf_notif_iter_status set_current_stream_class(
981 struct bt_ctf_notif_iter *notit)
e98a2d6e
PP
982{
983 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
5f870343 984 struct bt_ctf_field_type *packet_header_type = NULL;
e98a2d6e 985 struct bt_ctf_field_type *stream_id_field_type = NULL;
c937c615 986 struct bt_ctf_stream_class *new_stream_class = NULL;
e98a2d6e
PP
987 uint64_t stream_id;
988
5f870343
JG
989 /* Clear the current stream class field path cache. */
990 notit->cur_sc_field_path_cache = NULL;
991
e98a2d6e
PP
992 /* Is there any "stream_id" field in the packet header? */
993 packet_header_type = bt_ctf_trace_get_packet_header_type(
fdf0e7a0 994 notit->meta.trace);
e98a2d6e 995 if (!packet_header_type) {
fdf0e7a0
PP
996 /*
997 * No packet header, therefore no `stream_id` field,
998 * therefore only one stream class.
999 */
1000 goto single_stream_class;
e98a2d6e
PP
1001 }
1002
1003 assert(is_struct_type(packet_header_type));
1004
1005 // TODO: optimalize!
1006 stream_id_field_type =
fdf0e7a0
PP
1007 bt_ctf_field_type_structure_get_field_type_by_name(
1008 packet_header_type, "stream_id");
e98a2d6e
PP
1009 if (stream_id_field_type) {
1010 /* Find appropriate stream class using current stream ID */
e98a2d6e 1011 int ret;
5f870343 1012 struct bt_ctf_field *stream_id_field = NULL;
e98a2d6e
PP
1013
1014 assert(notit->dscopes.trace_packet_header);
1015
1016 // TODO: optimalize!
1017 stream_id_field = bt_ctf_field_structure_get_field(
78586d8a 1018 notit->dscopes.trace_packet_header, "stream_id");
e98a2d6e
PP
1019 assert(stream_id_field);
1020 ret = bt_ctf_field_unsigned_integer_get_value(
78586d8a 1021 stream_id_field, &stream_id);
e98a2d6e
PP
1022 assert(!ret);
1023 BT_PUT(stream_id_field);
1024 } else {
fdf0e7a0 1025single_stream_class:
e98a2d6e
PP
1026 /* Only one stream: pick the first stream class */
1027 assert(bt_ctf_trace_get_stream_class_count(
78586d8a 1028 notit->meta.trace) == 1);
e98a2d6e
PP
1029 stream_id = 0;
1030 }
1031
fdf0e7a0
PP
1032 BT_LOGV("Found stream class ID to use: notit-addr=%p, "
1033 "stream-class-id=%" PRIu64 ", "
1034 "trace-addr=%p, trace-name=\"%s\"",
1035 notit, stream_id, notit->meta.trace,
1036 bt_ctf_trace_get_name(notit->meta.trace));
1037
c937c615 1038 new_stream_class = bt_ctf_trace_get_stream_class_by_id(
fdf0e7a0 1039 notit->meta.trace, stream_id);
c937c615 1040 if (!new_stream_class) {
fdf0e7a0
PP
1041 BT_LOGW("No stream class with ID of stream class ID to use in trace: "
1042 "notit-addr=%p, stream-class-id=%" PRIu64 ", "
1043 "trace-addr=%p, trace-name=\"%s\"",
1044 notit, stream_id, notit->meta.trace,
1045 bt_ctf_trace_get_name(notit->meta.trace));
e98a2d6e
PP
1046 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
1047 goto end;
1048 }
1049
c937c615
PP
1050 if (notit->meta.stream_class) {
1051 if (new_stream_class != notit->meta.stream_class) {
1052 BT_LOGW("Two packets refer to two different stream classes within the same packet sequence: "
1053 "notit-addr=%p, prev-stream-class-addr=%p, "
1054 "prev-stream-class-name=\"%s\", "
1055 "prev-stream-class-id=%" PRId64 ", "
1056 "next-stream-class-addr=%p, "
1057 "next-stream-class-name=\"%s\", "
1058 "next-stream-class-id=%" PRId64 ", "
1059 "trace-addr=%p, trace-name=\"%s\"",
1060 notit, notit->meta.stream_class,
1061 bt_ctf_stream_class_get_name(notit->meta.stream_class),
1062 bt_ctf_stream_class_get_id(notit->meta.stream_class),
1063 new_stream_class,
1064 bt_ctf_stream_class_get_name(new_stream_class),
1065 bt_ctf_stream_class_get_id(new_stream_class),
1066 notit->meta.trace,
1067 bt_ctf_trace_get_name(notit->meta.trace));
1068 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
1069 goto end;
1070 }
1071 } else {
1072 BT_MOVE(notit->meta.stream_class, new_stream_class);
1073 }
1074
fdf0e7a0
PP
1075 BT_LOGV("Set current stream class: "
1076 "notit-addr=%p, stream-class-addr=%p, "
1077 "stream-class-name=\"%s\", stream-class-id=%" PRId64,
1078 notit, notit->meta.stream_class,
1079 bt_ctf_stream_class_get_name(notit->meta.stream_class),
1080 bt_ctf_stream_class_get_id(notit->meta.stream_class));
1081
5f870343
JG
1082 /*
1083 * Retrieve (or lazily create) the current stream class field path
1084 * cache.
1085 */
1086 notit->cur_sc_field_path_cache = get_stream_class_field_path_cache(
fdf0e7a0 1087 notit, notit->meta.stream_class);
5f870343 1088 if (!notit->cur_sc_field_path_cache) {
fdf0e7a0
PP
1089 BT_LOGW("Cannot retrieve stream class field path from cache: "
1090 "notit-addr=%p, stream-class-addr=%p, "
1091 "stream-class-name=\"%s\", stream-class-id=%" PRId64,
1092 notit, notit->meta.stream_class,
1093 bt_ctf_stream_class_get_name(notit->meta.stream_class),
1094 bt_ctf_stream_class_get_id(notit->meta.stream_class));
5f870343
JG
1095 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
1096 goto end;
1097 }
e98a2d6e
PP
1098end:
1099 BT_PUT(packet_header_type);
1100 BT_PUT(stream_id_field_type);
c937c615 1101 bt_put(new_stream_class);
e98a2d6e
PP
1102 return status;
1103}
1104
1105static
1106enum bt_ctf_notif_iter_status after_packet_header_state(
1107 struct bt_ctf_notif_iter *notit)
1108{
1109 enum bt_ctf_notif_iter_status status;
1110
1111 status = set_current_stream_class(notit);
1112 if (status == BT_CTF_NOTIF_ITER_STATUS_OK) {
1113 notit->state = STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN;
1114 }
1115
1116 return status;
1117}
1118
1119static
1120enum bt_ctf_notif_iter_status read_packet_context_begin_state(
1121 struct bt_ctf_notif_iter *notit)
1122{
1123 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
1124 struct bt_ctf_field_type *packet_context_type;
1125
1126 assert(notit->meta.stream_class);
1127 packet_context_type = bt_ctf_stream_class_get_packet_context_type(
fdf0e7a0 1128 notit->meta.stream_class);
e98a2d6e 1129 if (!packet_context_type) {
fdf0e7a0
PP
1130 BT_LOGV("No packet packet context field type in stream class: continuing: "
1131 "notit-addr=%p, stream-class-addr=%p, "
1132 "stream-class-name=\"%s\", stream-class-id=%" PRId64,
1133 notit, notit->meta.stream_class,
1134 bt_ctf_stream_class_get_name(notit->meta.stream_class),
1135 bt_ctf_stream_class_get_id(notit->meta.stream_class));
835b2d10 1136 notit->state = STATE_AFTER_STREAM_PACKET_CONTEXT;
e98a2d6e
PP
1137 goto end;
1138 }
1139
fdf0e7a0
PP
1140 BT_LOGV("Decoding packet context field: "
1141 "notit-addr=%p, stream-class-addr=%p, "
1142 "stream-class-name=\"%s\", stream-class-id=%" PRId64 ", "
1143 "ft-addr=%p",
1144 notit, notit->meta.stream_class,
1145 bt_ctf_stream_class_get_name(notit->meta.stream_class),
1146 bt_ctf_stream_class_get_id(notit->meta.stream_class),
1147 packet_context_type);
e98a2d6e 1148 status = read_dscope_begin_state(notit, packet_context_type,
fdf0e7a0
PP
1149 STATE_AFTER_STREAM_PACKET_CONTEXT,
1150 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE,
1151 &notit->dscopes.stream_packet_context);
1152 if (status < 0) {
1153 BT_LOGW("Cannot decode packet context field: "
1154 "notit-addr=%p, stream-class-addr=%p, "
1155 "stream-class-name=\"%s\", "
1156 "stream-class-id=%" PRId64 ", ft-addr=%p",
1157 notit, notit->meta.stream_class,
1158 bt_ctf_stream_class_get_name(notit->meta.stream_class),
1159 bt_ctf_stream_class_get_id(notit->meta.stream_class),
1160 packet_context_type);
1161 }
e98a2d6e
PP
1162
1163end:
1164 BT_PUT(packet_context_type);
e98a2d6e
PP
1165 return status;
1166}
1167
1168static
1169enum bt_ctf_notif_iter_status read_packet_context_continue_state(
1170 struct bt_ctf_notif_iter *notit)
1171{
1172 return read_dscope_continue_state(notit,
78586d8a 1173 STATE_AFTER_STREAM_PACKET_CONTEXT);
e98a2d6e
PP
1174}
1175
78586d8a 1176static
e98a2d6e
PP
1177enum bt_ctf_notif_iter_status set_current_packet_content_sizes(
1178 struct bt_ctf_notif_iter *notit)
1179{
1180 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
1181 struct bt_ctf_field *packet_size_field = NULL;
1182 struct bt_ctf_field *content_size_field = NULL;
a27839b0 1183 uint64_t content_size = -1ULL, packet_size = -1ULL;
e98a2d6e 1184
89b08333
MD
1185 if (!notit->dscopes.stream_packet_context) {
1186 goto end;
1187 }
e98a2d6e 1188
e98a2d6e 1189 packet_size_field = bt_ctf_field_structure_get_field(
fdf0e7a0 1190 notit->dscopes.stream_packet_context, "packet_size");
e98a2d6e 1191 content_size_field = bt_ctf_field_structure_get_field(
fdf0e7a0 1192 notit->dscopes.stream_packet_context, "content_size");
e98a2d6e
PP
1193 if (packet_size_field) {
1194 int ret = bt_ctf_field_unsigned_integer_get_value(
fdf0e7a0 1195 packet_size_field, &packet_size);
e98a2d6e 1196
fdf0e7a0 1197 assert(ret == 0);
e98a2d6e 1198 if (packet_size == 0) {
fdf0e7a0
PP
1199 BT_LOGW("Invalid packet size: packet context field indicates packet size is zero: "
1200 "notit-addr=%p, packet-context-field-addr=%p",
1201 notit, notit->dscopes.stream_packet_context);
e98a2d6e
PP
1202 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
1203 goto end;
1204 } else if ((packet_size % 8) != 0) {
fdf0e7a0
PP
1205 BT_LOGW("Invalid packet size: packet context field indicates packet size is not a multiple of 8: "
1206 "notit-addr=%p, packet-context-field-addr=%p, "
1207 "packet-size=%" PRIu64,
1208 notit, notit->dscopes.stream_packet_context,
1209 packet_size);
e98a2d6e
PP
1210 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
1211 goto end;
1212 }
1213 }
78586d8a 1214
e98a2d6e
PP
1215 if (content_size_field) {
1216 int ret = bt_ctf_field_unsigned_integer_get_value(
1217 content_size_field, &content_size);
78586d8a 1218
fdf0e7a0 1219 assert(ret == 0);
e98a2d6e
PP
1220 } else {
1221 content_size = packet_size;
1222 }
1223
fdf0e7a0
PP
1224 if (content_size > packet_size) {
1225 BT_LOGW("Invalid packet or content size: packet context field indicates content size is greater than packet size: "
1226 "notit-addr=%p, packet-context-field-addr=%p, "
1227 "packet-size=%" PRIu64 ", content-size=%" PRIu64,
1228 notit, notit->dscopes.stream_packet_context,
1229 packet_size, content_size);
1230 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
1231 goto end;
1232 }
1233
a27839b0
MD
1234 if (packet_size != -1ULL) {
1235 notit->cur_packet_size = packet_size;
1236 } else {
1237 /*
1238 * Use the content size as packet size indicator if the
1239 * packet size field is missing. This means there is no
1240 * padding in this stream.
1241 */
1242 notit->cur_packet_size = content_size;
1243 }
e98a2d6e 1244 notit->cur_content_size = content_size;
fdf0e7a0
PP
1245 BT_LOGV("Set current packet and content sizes: "
1246 "notit-addr=%p, packet-size=%" PRIu64 ", content-size=%" PRIu64,
1247 notit, packet_size, content_size);
e98a2d6e
PP
1248end:
1249 BT_PUT(packet_size_field);
1250 BT_PUT(content_size_field);
e98a2d6e
PP
1251 return status;
1252}
1253
1254static
1255enum bt_ctf_notif_iter_status after_packet_context_state(
1256 struct bt_ctf_notif_iter *notit)
1257{
1258 enum bt_ctf_notif_iter_status status;
1259
1260 status = set_current_packet_content_sizes(notit);
1261 if (status == BT_CTF_NOTIF_ITER_STATUS_OK) {
1262 notit->state = STATE_EMIT_NOTIF_NEW_PACKET;
1263 }
1264
1265 return status;
1266}
1267
1268static
1269enum bt_ctf_notif_iter_status read_event_header_begin_state(
1270 struct bt_ctf_notif_iter *notit)
1271{
1272 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
1273 struct bt_ctf_field_type *event_header_type = NULL;
1274
9b876b7b
PP
1275 /* Reset the position of the last event header */
1276 notit->buf.last_eh_at = notit->buf.at;
1277
e98a2d6e
PP
1278 /* Check if we have some content left */
1279 if (notit->cur_content_size >= 0) {
1280 if (packet_at(notit) == notit->cur_content_size) {
1281 /* No more events! */
fdf0e7a0
PP
1282 BT_LOGV("Reached end of packet: notit-addr=%p, "
1283 "cur=%zu", notit, packet_at(notit));
e98a2d6e
PP
1284 notit->state = STATE_EMIT_NOTIF_END_OF_PACKET;
1285 goto end;
1286 } else if (packet_at(notit) > notit->cur_content_size) {
1287 /* That's not supposed to happen */
fdf0e7a0 1288 BT_LOGV("Before decoding event header field: cursor is passed the packet's content: "
41342c71 1289 "notit-addr=%p, content-size=%" PRId64 ", "
fdf0e7a0
PP
1290 "cur=%zu", notit, notit->cur_content_size,
1291 packet_at(notit));
e98a2d6e
PP
1292 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
1293 goto end;
1294 }
1295 }
1296
1297 event_header_type = bt_ctf_stream_class_get_event_header_type(
1298 notit->meta.stream_class);
1299 if (!event_header_type) {
835b2d10 1300 notit->state = STATE_AFTER_STREAM_EVENT_HEADER;
e98a2d6e
PP
1301 goto end;
1302 }
1303
1304 put_event_dscopes(notit);
fdf0e7a0
PP
1305 BT_LOGV("Decoding event header field: "
1306 "notit-addr=%p, stream-class-addr=%p, "
1307 "stream-class-name=\"%s\", stream-class-id=%" PRId64 ", "
1308 "ft-addr=%p",
1309 notit, notit->meta.stream_class,
1310 bt_ctf_stream_class_get_name(notit->meta.stream_class),
1311 bt_ctf_stream_class_get_id(notit->meta.stream_class),
1312 event_header_type);
e98a2d6e
PP
1313 status = read_dscope_begin_state(notit, event_header_type,
1314 STATE_AFTER_STREAM_EVENT_HEADER,
1315 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE,
1316 &notit->dscopes.stream_event_header);
fdf0e7a0
PP
1317 if (status < 0) {
1318 BT_LOGW("Cannot decode event header field: "
1319 "notit-addr=%p, stream-class-addr=%p, "
1320 "stream-class-name=\"%s\", "
1321 "stream-class-id=%" PRId64 ", ft-addr=%p",
1322 notit, notit->meta.stream_class,
1323 bt_ctf_stream_class_get_name(notit->meta.stream_class),
1324 bt_ctf_stream_class_get_id(notit->meta.stream_class),
1325 event_header_type);
1326 }
e98a2d6e
PP
1327end:
1328 BT_PUT(event_header_type);
1329
1330 return status;
1331}
1332
1333static
1334enum bt_ctf_notif_iter_status read_event_header_continue_state(
1335 struct bt_ctf_notif_iter *notit)
1336{
1337 return read_dscope_continue_state(notit,
1338 STATE_AFTER_STREAM_EVENT_HEADER);
1339}
1340
1341static inline
1342enum bt_ctf_notif_iter_status set_current_event_class(struct bt_ctf_notif_iter *notit)
1343{
1344 /*
1345 * The assert() calls in this function are okay because it is
1346 * assumed here that all the metadata objects have been
1347 * validated for CTF correctness before decoding actual streams.
1348 */
1349
1350 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
1351 struct bt_ctf_field_type *event_header_type;
1352 struct bt_ctf_field_type *id_field_type = NULL;
1353 struct bt_ctf_field_type *v_field_type = NULL;
1354 uint64_t event_id = -1ULL;
1355 int ret;
1356
1357 event_header_type = bt_ctf_stream_class_get_event_header_type(
1358 notit->meta.stream_class);
1359 if (!event_header_type) {
fdf0e7a0
PP
1360 /*
1361 * No event header, therefore no event class ID field,
1362 * therefore only one event class.
1363 */
1364 goto single_event_class;
e98a2d6e
PP
1365 }
1366
1367 /* Is there any "id"/"v" field in the event header? */
1368 assert(is_struct_type(event_header_type));
1369 id_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
1370 event_header_type, "id");
1371 v_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
1372 event_header_type, "v");
1373 assert(notit->dscopes.stream_event_header);
1374 if (v_field_type) {
1375 /*
1376 * _ _____ _____
1377 * | | |_ _|_ _| __ __ _
1378 * | | | | | || '_ \ / _` |
1379 * | |___| | | || | | | (_| | S P E C I A L
1380 * |_____|_| |_||_| |_|\__, | C A S E ™
1381 * |___/
1382 */
1383 struct bt_ctf_field *v_field = NULL;
1384 struct bt_ctf_field *v_struct_field = NULL;
1385 struct bt_ctf_field *v_struct_id_field = NULL;
1386
1387 // TODO: optimalize!
1388 v_field = bt_ctf_field_structure_get_field(
1389 notit->dscopes.stream_event_header, "v");
1390 assert(v_field);
1391
1392 v_struct_field =
1393 bt_ctf_field_variant_get_current_field(v_field);
1394 if (!v_struct_field) {
1395 goto end_v_field_type;
1396 }
1397
1398 // TODO: optimalize!
1399 v_struct_id_field =
1400 bt_ctf_field_structure_get_field(v_struct_field, "id");
1401 if (!v_struct_id_field) {
1402 goto end_v_field_type;
1403 }
1404
fdf0e7a0
PP
1405 if (bt_ctf_field_is_integer(v_struct_id_field)) {
1406 ret = bt_ctf_field_unsigned_integer_get_value(
1407 v_struct_id_field, &event_id);
1408 if (ret) {
1409 BT_LOGV("Cannot get value of unsigned integer field (`id`): continuing: "
1410 "notit=%p, field-addr=%p",
1411 notit, v_struct_id_field);
1412 event_id = -1ULL;
1413 }
e98a2d6e
PP
1414 }
1415
1416end_v_field_type:
1417 BT_PUT(v_field);
1418 BT_PUT(v_struct_field);
1419 BT_PUT(v_struct_id_field);
1420 }
1421
1422 if (id_field_type && event_id == -1ULL) {
1423 /* Check "id" field */
1424 struct bt_ctf_field *id_field = NULL;
0b0bf682 1425 int ret_get_value = 0;
e98a2d6e
PP
1426
1427 // TODO: optimalize!
1428 id_field = bt_ctf_field_structure_get_field(
1429 notit->dscopes.stream_event_header, "id");
fdf0e7a0
PP
1430 if (!id_field) {
1431 goto check_event_id;
1432 }
e98a2d6e
PP
1433
1434 if (bt_ctf_field_is_integer(id_field)) {
0b0bf682 1435 ret_get_value = bt_ctf_field_unsigned_integer_get_value(
e98a2d6e 1436 id_field, &event_id);
fdf0e7a0 1437 } else if (bt_ctf_field_is_enumeration(id_field)) {
e98a2d6e
PP
1438 struct bt_ctf_field *container;
1439
1440 container = bt_ctf_field_enumeration_get_container(
1441 id_field);
1442 assert(container);
0b0bf682 1443 ret_get_value = bt_ctf_field_unsigned_integer_get_value(
e98a2d6e
PP
1444 container, &event_id);
1445 BT_PUT(container);
1446 }
fdf0e7a0 1447
0b0bf682 1448 assert(ret_get_value == 0);
e98a2d6e
PP
1449 BT_PUT(id_field);
1450 }
1451
fdf0e7a0 1452check_event_id:
e98a2d6e 1453 if (event_id == -1ULL) {
fdf0e7a0 1454single_event_class:
e98a2d6e
PP
1455 /* Event ID not found: single event? */
1456 assert(bt_ctf_stream_class_get_event_class_count(
1457 notit->meta.stream_class) == 1);
1458 event_id = 0;
1459 }
1460
fdf0e7a0
PP
1461 BT_LOGV("Found event class ID to use: notit-addr=%p, "
1462 "stream-class-addr=%p, stream-class-name=\"%s\", "
1463 "stream-class-id=%" PRId64 ", "
1464 "event-class-id=%" PRIu64,
1465 notit, notit->meta.stream_class,
1466 bt_ctf_stream_class_get_name(notit->meta.stream_class),
1467 bt_ctf_stream_class_get_id(notit->meta.stream_class),
1468 event_id);
e98a2d6e
PP
1469 BT_PUT(notit->meta.event_class);
1470 notit->meta.event_class = bt_ctf_stream_class_get_event_class_by_id(
1471 notit->meta.stream_class, event_id);
1472 if (!notit->meta.event_class) {
fdf0e7a0
PP
1473 BT_LOGW("No event class with ID of event class ID to use in stream class: "
1474 "notit-addr=%p, stream-class-addr=%p, "
1475 "stream-class-name=\"%s\", "
1476 "stream-class-id=%" PRId64 ", "
1477 "event-class-id=%" PRIu64,
1478 notit, notit->meta.stream_class,
1479 bt_ctf_stream_class_get_name(notit->meta.stream_class),
1480 bt_ctf_stream_class_get_id(notit->meta.stream_class),
1481 event_id);
e98a2d6e
PP
1482 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
1483 goto end;
1484 }
1485
fdf0e7a0
PP
1486 BT_LOGV("Set current event class: "
1487 "notit-addr=%p, event-class-addr=%p, "
1488 "event-class-name=\"%s\", event-class-id=%" PRId64,
1489 notit, notit->meta.event_class,
1490 bt_ctf_event_class_get_name(notit->meta.event_class),
1491 bt_ctf_event_class_get_id(notit->meta.event_class));
1492
e98a2d6e
PP
1493end:
1494 BT_PUT(event_header_type);
1495 BT_PUT(id_field_type);
1496 BT_PUT(v_field_type);
1497
1498 return status;
1499}
1500
1501static
1502enum bt_ctf_notif_iter_status after_event_header_state(
1503 struct bt_ctf_notif_iter *notit)
1504{
1505 enum bt_ctf_notif_iter_status status;
1506
e98a2d6e
PP
1507 status = set_current_event_class(notit);
1508 if (status != BT_CTF_NOTIF_ITER_STATUS_OK) {
e98a2d6e
PP
1509 goto end;
1510 }
1511
1512 notit->state = STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN;
1513
1514end:
1515 return status;
1516}
1517
1518static
1519enum bt_ctf_notif_iter_status read_stream_event_context_begin_state(
1520 struct bt_ctf_notif_iter *notit)
1521{
1522 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
1523 struct bt_ctf_field_type *stream_event_context_type;
1524
1525 stream_event_context_type = bt_ctf_stream_class_get_event_context_type(
1526 notit->meta.stream_class);
1527 if (!stream_event_context_type) {
835b2d10 1528 notit->state = STATE_DSCOPE_EVENT_CONTEXT_BEGIN;
e98a2d6e
PP
1529 goto end;
1530 }
1531
fdf0e7a0
PP
1532 BT_LOGV("Decoding stream event context field: "
1533 "notit-addr=%p, stream-class-addr=%p, "
1534 "stream-class-name=\"%s\", stream-class-id=%" PRId64 ", "
1535 "ft-addr=%p",
1536 notit, notit->meta.stream_class,
1537 bt_ctf_stream_class_get_name(notit->meta.stream_class),
1538 bt_ctf_stream_class_get_id(notit->meta.stream_class),
1539 stream_event_context_type);
e98a2d6e
PP
1540 status = read_dscope_begin_state(notit, stream_event_context_type,
1541 STATE_DSCOPE_EVENT_CONTEXT_BEGIN,
1542 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE,
1543 &notit->dscopes.stream_event_context);
fdf0e7a0
PP
1544 if (status < 0) {
1545 BT_LOGW("Cannot decode stream event context field: "
1546 "notit-addr=%p, stream-class-addr=%p, "
1547 "stream-class-name=\"%s\", "
1548 "stream-class-id=%" PRId64 ", ft-addr=%p",
1549 notit, notit->meta.stream_class,
1550 bt_ctf_stream_class_get_name(notit->meta.stream_class),
1551 bt_ctf_stream_class_get_id(notit->meta.stream_class),
1552 stream_event_context_type);
1553 }
e98a2d6e
PP
1554
1555end:
1556 BT_PUT(stream_event_context_type);
1557
1558 return status;
1559}
1560
1561static
1562enum bt_ctf_notif_iter_status read_stream_event_context_continue_state(
1563 struct bt_ctf_notif_iter *notit)
1564{
1565 return read_dscope_continue_state(notit,
1566 STATE_DSCOPE_EVENT_CONTEXT_BEGIN);
1567}
1568
1569static
1570enum bt_ctf_notif_iter_status read_event_context_begin_state(
1571 struct bt_ctf_notif_iter *notit)
1572{
1573 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
1574 struct bt_ctf_field_type *event_context_type;
1575
1576 event_context_type = bt_ctf_event_class_get_context_type(
1577 notit->meta.event_class);
1578 if (!event_context_type) {
835b2d10 1579 notit->state = STATE_DSCOPE_EVENT_PAYLOAD_BEGIN;
e98a2d6e
PP
1580 goto end;
1581 }
fdf0e7a0
PP
1582
1583 BT_LOGV("Decoding event context field: "
1584 "notit-addr=%p, event-class-addr=%p, "
1585 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
1586 "ft-addr=%p",
1587 notit, notit->meta.event_class,
1588 bt_ctf_event_class_get_name(notit->meta.event_class),
1589 bt_ctf_event_class_get_id(notit->meta.event_class),
1590 event_context_type);
e98a2d6e
PP
1591 status = read_dscope_begin_state(notit, event_context_type,
1592 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN,
1593 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE,
1594 &notit->dscopes.event_context);
fdf0e7a0
PP
1595 if (status < 0) {
1596 BT_LOGW("Cannot decode event context field: "
1597 "notit-addr=%p, event-class-addr=%p, "
1598 "event-class-name=\"%s\", "
1599 "event-class-id=%" PRId64 ", ft-addr=%p",
1600 notit, notit->meta.event_class,
1601 bt_ctf_event_class_get_name(notit->meta.event_class),
1602 bt_ctf_event_class_get_id(notit->meta.event_class),
1603 event_context_type);
1604 }
e98a2d6e
PP
1605
1606end:
1607 BT_PUT(event_context_type);
1608
1609 return status;
1610}
1611
1612static
1613enum bt_ctf_notif_iter_status read_event_context_continue_state(
1614 struct bt_ctf_notif_iter *notit)
1615{
1616 return read_dscope_continue_state(notit,
1617 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN);
1618}
1619
1620static
1621enum bt_ctf_notif_iter_status read_event_payload_begin_state(
1622 struct bt_ctf_notif_iter *notit)
1623{
1624 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
1625 struct bt_ctf_field_type *event_payload_type;
1626
1627 event_payload_type = bt_ctf_event_class_get_payload_type(
1628 notit->meta.event_class);
1629 if (!event_payload_type) {
835b2d10 1630 notit->state = STATE_EMIT_NOTIF_EVENT;
e98a2d6e
PP
1631 goto end;
1632 }
1633
fdf0e7a0
PP
1634 BT_LOGV("Decoding event payload field: "
1635 "notit-addr=%p, event-class-addr=%p, "
1636 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
1637 "ft-addr=%p",
1638 notit, notit->meta.event_class,
1639 bt_ctf_event_class_get_name(notit->meta.event_class),
1640 bt_ctf_event_class_get_id(notit->meta.event_class),
1641 event_payload_type);
e98a2d6e
PP
1642 status = read_dscope_begin_state(notit, event_payload_type,
1643 STATE_EMIT_NOTIF_EVENT,
1644 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE,
1645 &notit->dscopes.event_payload);
fdf0e7a0
PP
1646 if (status < 0) {
1647 BT_LOGW("Cannot decode event payload field: "
1648 "notit-addr=%p, event-class-addr=%p, "
1649 "event-class-name=\"%s\", "
1650 "event-class-id=%" PRId64 ", ft-addr=%p",
1651 notit, notit->meta.event_class,
1652 bt_ctf_event_class_get_name(notit->meta.event_class),
1653 bt_ctf_event_class_get_id(notit->meta.event_class),
1654 event_payload_type);
1655 }
e98a2d6e
PP
1656
1657end:
1658 BT_PUT(event_payload_type);
1659
1660 return status;
1661}
1662
1663static
1664enum bt_ctf_notif_iter_status read_event_payload_continue_state(
1665 struct bt_ctf_notif_iter *notit)
1666{
1667 return read_dscope_continue_state(notit, STATE_EMIT_NOTIF_EVENT);
1668}
1669
1670static
1671enum bt_ctf_notif_iter_status skip_packet_padding_state(
1672 struct bt_ctf_notif_iter *notit)
1673{
1674 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
1675 size_t bits_to_skip;
1676
1677 assert(notit->cur_packet_size > 0);
1678 bits_to_skip = notit->cur_packet_size - packet_at(notit);
1679 if (bits_to_skip == 0) {
1680 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1681 goto end;
1682 } else {
1683 size_t bits_to_consume;
fdf0e7a0
PP
1684
1685 BT_LOGV("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu",
1686 bits_to_skip, notit, bits_to_skip);
e98a2d6e
PP
1687 status = buf_ensure_available_bits(notit);
1688 if (status != BT_CTF_NOTIF_ITER_STATUS_OK) {
1689 goto end;
1690 }
1691
1692 bits_to_consume = MIN(buf_available_bits(notit), bits_to_skip);
fdf0e7a0
PP
1693 BT_LOGV("Skipping %zu bits of padding: notit-addr=%p, size=%zu",
1694 bits_to_consume, notit, bits_to_consume);
e98a2d6e
PP
1695 buf_consume_bits(notit, bits_to_consume);
1696 bits_to_skip = notit->cur_packet_size - packet_at(notit);
1697 if (bits_to_skip == 0) {
1698 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1699 goto end;
1700 }
1701 }
1702
1703end:
1704 return status;
1705}
1706
1707static inline
1708enum bt_ctf_notif_iter_status handle_state(struct bt_ctf_notif_iter *notit)
1709{
1710 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
fdf0e7a0 1711 const enum state state = notit->state;
e98a2d6e 1712
fdf0e7a0
PP
1713 BT_LOGV("Handling state: notit-addr=%p, state=%s",
1714 notit, state_string(state));
e98a2d6e
PP
1715
1716 // TODO: optimalize!
fdf0e7a0 1717 switch (state) {
e98a2d6e
PP
1718 case STATE_INIT:
1719 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1720 break;
1721 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
1722 status = read_packet_header_begin_state(notit);
1723 break;
1724 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
1725 status = read_packet_header_continue_state(notit);
1726 break;
1727 case STATE_AFTER_TRACE_PACKET_HEADER:
1728 status = after_packet_header_state(notit);
1729 break;
1730 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN:
1731 status = read_packet_context_begin_state(notit);
1732 break;
1733 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE:
1734 status = read_packet_context_continue_state(notit);
1735 break;
1736 case STATE_AFTER_STREAM_PACKET_CONTEXT:
1737 status = after_packet_context_state(notit);
1738 break;
1739 case STATE_EMIT_NOTIF_NEW_PACKET:
1740 notit->state = STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN;
1741 break;
1742 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN:
1743 status = read_event_header_begin_state(notit);
1744 break;
1745 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE:
1746 status = read_event_header_continue_state(notit);
1747 break;
1748 case STATE_AFTER_STREAM_EVENT_HEADER:
1749 status = after_event_header_state(notit);
1750 break;
1751 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN:
1752 status = read_stream_event_context_begin_state(notit);
1753 break;
1754 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE:
1755 status = read_stream_event_context_continue_state(notit);
1756 break;
1757 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN:
1758 status = read_event_context_begin_state(notit);
1759 break;
1760 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE:
1761 status = read_event_context_continue_state(notit);
1762 break;
1763 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN:
1764 status = read_event_payload_begin_state(notit);
1765 break;
1766 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE:
1767 status = read_event_payload_continue_state(notit);
1768 break;
1769 case STATE_EMIT_NOTIF_EVENT:
1770 notit->state = STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN;
1771 break;
1772 case STATE_SKIP_PACKET_PADDING:
1773 status = skip_packet_padding_state(notit);
1774 break;
1775 case STATE_EMIT_NOTIF_END_OF_PACKET:
1776 notit->state = STATE_SKIP_PACKET_PADDING;
1777 break;
fdf0e7a0
PP
1778 default:
1779 BT_LOGD("Unknown CTF plugin notification iterator state: "
1780 "notit-addr=%p, state=%d", notit, notit->state);
1781 abort();
e98a2d6e
PP
1782 }
1783
fdf0e7a0
PP
1784 BT_LOGV("Handled state: notit-addr=%p, status=%s, "
1785 "prev-state=%s, cur-state=%s",
1786 notit, bt_ctf_notif_iter_status_string(status),
1787 state_string(state), state_string(notit->state));
e98a2d6e
PP
1788 return status;
1789}
1790
2cf1d51e
JG
1791/**
1792 * Resets the internal state of a CTF notification iterator.
2cf1d51e
JG
1793 */
1794static
e98a2d6e
PP
1795void bt_ctf_notif_iter_reset(struct bt_ctf_notif_iter *notit)
1796{
1797 assert(notit);
fdf0e7a0 1798 BT_LOGD("Resetting notification iterator: addr=%p", notit);
e98a2d6e
PP
1799 stack_clear(notit->stack);
1800 BT_PUT(notit->meta.stream_class);
1801 BT_PUT(notit->meta.event_class);
1802 BT_PUT(notit->packet);
5184a1d6 1803 BT_PUT(notit->stream);
e98a2d6e
PP
1804 put_all_dscopes(notit);
1805 notit->buf.addr = NULL;
1806 notit->buf.sz = 0;
1807 notit->buf.at = 0;
bb8744a7 1808 notit->buf.last_eh_at = SIZE_MAX;
e98a2d6e
PP
1809 notit->buf.packet_offset = 0;
1810 notit->state = STATE_INIT;
1811 notit->cur_content_size = -1;
1812 notit->cur_packet_size = -1;
bb230c9b 1813 notit->cur_packet_offset = -1;
e98a2d6e
PP
1814}
1815
2cf1d51e
JG
1816static
1817int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter *notit)
1818{
1819 int ret = 0;
1820
c937c615
PP
1821 /*
1822 * We don't put the stream class here because we need to make
1823 * sure that all the packets processed by the same notification
1824 * iterator refer to the same stream class (the first one).
1825 */
2cf1d51e 1826 assert(notit);
bb230c9b
JG
1827 if (notit->cur_packet_size != -1) {
1828 notit->cur_packet_offset += notit->cur_packet_size;
1829 }
1830 BT_LOGV("Switching packet: notit-addr=%p, cur=%zu, "
1831 "packet-offset=%" PRId64, notit, notit->buf.at,
1832 notit->cur_packet_offset);
2cf1d51e 1833 stack_clear(notit->stack);
2cf1d51e
JG
1834 BT_PUT(notit->meta.event_class);
1835 BT_PUT(notit->packet);
5f870343 1836 BT_PUT(notit->cur_timestamp_end);
2cf1d51e
JG
1837 put_all_dscopes(notit);
1838
1839 /*
1840 * Adjust current buffer so that addr points to the beginning of the new
1841 * packet.
1842 */
1843 if (notit->buf.addr) {
1844 size_t consumed_bytes = (size_t) (notit->buf.at / CHAR_BIT);
1845
1846 /* Packets are assumed to start on a byte frontier. */
1847 if (notit->buf.at % CHAR_BIT) {
fdf0e7a0
PP
1848 BT_LOGW("Cannot switch packet: current position is not a multiple of 8: "
1849 "notit-addr=%p, cur=%zu", notit, notit->buf.at);
2cf1d51e
JG
1850 ret = -1;
1851 goto end;
1852 }
1853
1854 notit->buf.addr += consumed_bytes;
1855 notit->buf.sz -= consumed_bytes;
1856 notit->buf.at = 0;
1857 notit->buf.packet_offset = 0;
fdf0e7a0
PP
1858 BT_LOGV("Adjusted buffer: addr=%p, size=%zu",
1859 notit->buf.addr, notit->buf.sz);
2cf1d51e
JG
1860 }
1861
1862 notit->cur_content_size = -1;
1863 notit->cur_packet_size = -1;
5f870343 1864 notit->cur_sc_field_path_cache = NULL;
fdf0e7a0 1865
2cf1d51e
JG
1866end:
1867 return ret;
1868}
1869
e98a2d6e
PP
1870static
1871struct bt_ctf_field *get_next_field(struct bt_ctf_notif_iter *notit)
1872{
1873 struct bt_ctf_field *next_field = NULL;
1874 struct bt_ctf_field *base_field;
1875 struct bt_ctf_field_type *base_type;
1876 size_t index;
1877
1878 assert(!stack_empty(notit->stack));
1879 index = stack_top(notit->stack)->index;
1880 base_field = stack_top(notit->stack)->base;
fdf0e7a0 1881 assert(base_field);
e98a2d6e 1882 base_type = bt_ctf_field_get_type(base_field);
fdf0e7a0 1883 assert(base_type);
e98a2d6e
PP
1884
1885 switch (bt_ctf_field_type_get_type_id(base_type)) {
1487a16a 1886 case BT_CTF_FIELD_TYPE_ID_STRUCT:
e98a2d6e
PP
1887 next_field = bt_ctf_field_structure_get_field_by_index(
1888 base_field, index);
1889 break;
1487a16a 1890 case BT_CTF_FIELD_TYPE_ID_ARRAY:
e98a2d6e
PP
1891 next_field = bt_ctf_field_array_get_field(base_field, index);
1892 break;
1487a16a 1893 case BT_CTF_FIELD_TYPE_ID_SEQUENCE:
e98a2d6e
PP
1894 next_field = bt_ctf_field_sequence_get_field(base_field, index);
1895 break;
1487a16a 1896 case BT_CTF_FIELD_TYPE_ID_VARIANT:
e98a2d6e
PP
1897 next_field = bt_ctf_field_variant_get_current_field(base_field);
1898 break;
1899 default:
fdf0e7a0
PP
1900 BT_LOGF("Unknown base field type ID: "
1901 "notit-addr=%p, ft-addr=%p, ft-id=%s",
1902 notit, base_type,
1903 bt_ctf_field_type_id_string(
1904 bt_ctf_field_type_get_type_id(base_type)));
0fbb9a9f 1905 abort();
e98a2d6e
PP
1906 }
1907
e98a2d6e 1908 BT_PUT(base_type);
e98a2d6e
PP
1909 return next_field;
1910}
1911
c44c3e70
JG
1912static
1913void update_clock_state(uint64_t *state,
1914 struct bt_ctf_field *value_field)
1915{
1916 struct bt_ctf_field_type *value_type = NULL;
1917 uint64_t requested_new_value;
1918 uint64_t requested_new_value_mask;
1919 uint64_t cur_value_masked;
1920 int requested_new_value_size;
1921 int ret;
1922
1923 value_type = bt_ctf_field_get_type(value_field);
1924 assert(value_type);
fdf0e7a0 1925 assert(bt_ctf_field_type_is_integer(value_type));
c44c3e70
JG
1926 requested_new_value_size =
1927 bt_ctf_field_type_integer_get_size(value_type);
1928 assert(requested_new_value_size > 0);
c44c3e70
JG
1929 ret = bt_ctf_field_unsigned_integer_get_value(value_field,
1930 &requested_new_value);
1931 assert(!ret);
1932
1933 /*
1934 * Special case for a 64-bit new value, which is the limit
1935 * of a clock value as of this version: overwrite the
1936 * current value directly.
1937 */
1938 if (requested_new_value_size == 64) {
1939 *state = requested_new_value;
1940 goto end;
1941 }
1942
1943 requested_new_value_mask = (1ULL << requested_new_value_size) - 1;
1944 cur_value_masked = *state & requested_new_value_mask;
1945
1946 if (requested_new_value < cur_value_masked) {
1947 /*
1948 * It looks like a wrap happened on the number of bits
1949 * of the requested new value. Assume that the clock
1950 * value wrapped only one time.
1951 */
1952 *state += requested_new_value_mask + 1;
1953 }
1954
1955 /* Clear the low bits of the current clock value. */
1956 *state &= ~requested_new_value_mask;
1957
1958 /* Set the low bits of the current clock value. */
1959 *state |= requested_new_value;
1960end:
fdf0e7a0
PP
1961 BT_LOGV("Updated clock's value from integer field's value: "
1962 "value=%" PRIu64, *state);
c44c3e70
JG
1963 bt_put(value_type);
1964}
1965
1966static
1967enum bt_ctf_btr_status update_clock(struct bt_ctf_notif_iter *notit,
c44c3e70
JG
1968 struct bt_ctf_field *int_field)
1969{
ac0c6bdd 1970 gboolean clock_class_found;
501a8569 1971 uint64_t *clock_state = NULL;
f45bfe88 1972 struct bt_ctf_field_type *int_field_type = NULL;
c44c3e70 1973 enum bt_ctf_btr_status ret = BT_CTF_BTR_STATUS_OK;
f45bfe88 1974 struct bt_ctf_clock_class *clock_class = NULL;
c44c3e70 1975
f45bfe88 1976 int_field_type = bt_ctf_field_get_type(int_field);
fdf0e7a0 1977 assert(int_field_type);
f45bfe88
PP
1978 clock_class = bt_ctf_field_type_integer_get_mapped_clock_class(
1979 int_field_type);
ac0c6bdd 1980 if (likely(!clock_class)) {
f45bfe88 1981 goto end;
c44c3e70
JG
1982 }
1983
ac0c6bdd 1984 clock_class_found = g_hash_table_lookup_extended(notit->clock_states,
fdf0e7a0 1985 clock_class, NULL, (gpointer) &clock_state);
501a8569 1986 if (!clock_class_found) {
c44c3e70
JG
1987 clock_state = g_new0(uint64_t, 1);
1988 if (!clock_state) {
fdf0e7a0 1989 BT_LOGE_STR("Failed to allocate a uint64_t.");
c44c3e70
JG
1990 ret = BT_CTF_BTR_STATUS_ENOMEM;
1991 goto end;
1992 }
fdf0e7a0 1993
ac0c6bdd 1994 g_hash_table_insert(notit->clock_states, bt_get(clock_class),
fdf0e7a0 1995 clock_state);
c44c3e70
JG
1996 }
1997
1998 /* Update the clock's state. */
fdf0e7a0
PP
1999 BT_LOGV("Updating notification iterator's clock's value from integer field: "
2000 "notit-addr=%p, clock-class-addr=%p, "
2001 "clock-class-name=\"%s\", value=%" PRIu64,
2002 notit, clock_class,
2003 bt_ctf_clock_class_get_name(clock_class), *clock_state);
c44c3e70
JG
2004 update_clock_state(clock_state, int_field);
2005end:
f45bfe88 2006 bt_put(int_field_type);
ac0c6bdd 2007 bt_put(clock_class);
c44c3e70
JG
2008 return ret;
2009}
2010
e98a2d6e 2011static
5f870343
JG
2012enum bt_ctf_btr_status btr_unsigned_int_common(uint64_t value,
2013 struct bt_ctf_field_type *type, void *data,
f45bfe88 2014 struct bt_ctf_field **out_int_field)
e98a2d6e
PP
2015{
2016 enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK;
2017 struct bt_ctf_field *field = NULL;
2018 struct bt_ctf_field *int_field = NULL;
2019 struct bt_ctf_notif_iter *notit = data;
2020 int ret;
2021
fdf0e7a0
PP
2022 BT_LOGV("Common unsigned integer function called from BTR: "
2023 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2024 "ft-id=%s, value=%" PRIu64,
2025 notit, notit->btr, type,
2026 bt_ctf_field_type_id_string(
2027 bt_ctf_field_type_get_type_id(type)),
2028 value);
2029
5f870343 2030 /* Create next field */
e98a2d6e
PP
2031 field = get_next_field(notit);
2032 if (!field) {
fdf0e7a0 2033 BT_LOGW("Cannot get next field: notit-addr=%p", notit);
e98a2d6e 2034 status = BT_CTF_BTR_STATUS_ERROR;
c44c3e70 2035 goto end_no_put;
e98a2d6e
PP
2036 }
2037
2038 switch(bt_ctf_field_type_get_type_id(type)) {
1487a16a 2039 case BT_CTF_FIELD_TYPE_ID_INTEGER:
e98a2d6e
PP
2040 /* Integer field is created field */
2041 BT_MOVE(int_field, field);
c44c3e70 2042 bt_get(type);
e98a2d6e 2043 break;
1487a16a 2044 case BT_CTF_FIELD_TYPE_ID_ENUM:
e98a2d6e 2045 int_field = bt_ctf_field_enumeration_get_container(field);
fdf0e7a0 2046 assert(int_field);
c44c3e70 2047 type = bt_ctf_field_get_type(int_field);
fdf0e7a0 2048 assert(type);
e98a2d6e
PP
2049 break;
2050 default:
fdf0e7a0
PP
2051 BT_LOGF("Unexpected field type ID: "
2052 "notit-addr=%p, ft-addr=%p, ft-id=%s",
2053 notit, type,
2054 bt_ctf_field_type_id_string(
2055 bt_ctf_field_type_get_type_id(type)));
0fbb9a9f 2056 abort();
e98a2d6e
PP
2057 }
2058
fdf0e7a0 2059 assert(int_field);
5f870343 2060 ret = bt_ctf_field_unsigned_integer_set_value(int_field, value);
fdf0e7a0 2061 assert(ret == 0);
e98a2d6e 2062 stack_top(notit->stack)->index++;
f45bfe88 2063 *out_int_field = int_field;
e98a2d6e 2064 BT_PUT(field);
c44c3e70 2065 BT_PUT(type);
fdf0e7a0 2066
c44c3e70 2067end_no_put:
e98a2d6e
PP
2068 return status;
2069}
2070
5f870343
JG
2071static
2072enum bt_ctf_btr_status btr_timestamp_end_cb(void *value,
2073 struct bt_ctf_field_type *type, void *data)
2074{
2075 enum bt_ctf_btr_status status;
2076 struct bt_ctf_field *field = NULL;
2077 struct bt_ctf_notif_iter *notit = data;
2078
fdf0e7a0
PP
2079 BT_LOGV("`timestamp_end` unsigned integer function called from BTR: "
2080 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2081 "ft-id=%s",
2082 notit, notit->btr, type,
2083 bt_ctf_field_type_id_string(
2084 bt_ctf_field_type_get_type_id(type)));
5f870343
JG
2085 status = btr_unsigned_int_common(*((uint64_t *) value), type, data,
2086 &field);
2087
2088 /* Set as the current packet's timestamp_end field. */
2089 BT_MOVE(notit->cur_timestamp_end, field);
2090 return status;
2091}
2092
e98a2d6e
PP
2093static
2094enum bt_ctf_btr_status btr_unsigned_int_cb(uint64_t value,
2095 struct bt_ctf_field_type *type, void *data)
5f870343
JG
2096{
2097 struct bt_ctf_notif_iter *notit = data;
2098 enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK;
2099 struct bt_ctf_field *field = NULL;
2100 struct field_cb_override *override;
2101
fdf0e7a0
PP
2102 BT_LOGV("Unsigned integer function called from BTR: "
2103 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2104 "ft-id=%s, value=%" PRIu64,
2105 notit, notit->btr, type,
2106 bt_ctf_field_type_id_string(
2107 bt_ctf_field_type_get_type_id(type)),
2108 value);
2109 override = g_hash_table_lookup(notit->field_overrides, type);
5f870343 2110 if (unlikely(override)) {
fdf0e7a0 2111 /* Override function logs errors */
5f870343
JG
2112 status = override->func(&value, type, override->data);
2113 goto end;
2114 }
2115
2116 status = btr_unsigned_int_common(value, type, data, &field);
2117 if (status != BT_CTF_BTR_STATUS_OK) {
fdf0e7a0 2118 /* btr_unsigned_int_common() logs errors */
5f870343
JG
2119 goto end;
2120 }
2121
f45bfe88 2122 status = update_clock(notit, field);
5f870343
JG
2123 BT_PUT(field);
2124end:
2125 return status;
2126}
2127
2128static
2129enum bt_ctf_btr_status btr_signed_int_cb(int64_t value,
2130 struct bt_ctf_field_type *type, void *data)
e98a2d6e
PP
2131{
2132 enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK;
2133 struct bt_ctf_field *field = NULL;
2134 struct bt_ctf_field *int_field = NULL;
2135 struct bt_ctf_notif_iter *notit = data;
2136 int ret;
2137
fdf0e7a0
PP
2138 BT_LOGV("Signed integer function called from BTR: "
2139 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2140 "ft-id=%s, value=%" PRId64,
2141 notit, notit->btr, type,
2142 bt_ctf_field_type_id_string(
2143 bt_ctf_field_type_get_type_id(type)),
2144 value);
2145
5f870343 2146 /* create next field */
e98a2d6e
PP
2147 field = get_next_field(notit);
2148 if (!field) {
fdf0e7a0 2149 BT_LOGW("Cannot get next field: notit-addr=%p", notit);
e98a2d6e 2150 status = BT_CTF_BTR_STATUS_ERROR;
c44c3e70 2151 goto end_no_put;
e98a2d6e
PP
2152 }
2153
2154 switch(bt_ctf_field_type_get_type_id(type)) {
1487a16a 2155 case BT_CTF_FIELD_TYPE_ID_INTEGER:
e98a2d6e
PP
2156 /* Integer field is created field */
2157 BT_MOVE(int_field, field);
c44c3e70 2158 bt_get(type);
e98a2d6e 2159 break;
1487a16a 2160 case BT_CTF_FIELD_TYPE_ID_ENUM:
e98a2d6e 2161 int_field = bt_ctf_field_enumeration_get_container(field);
fdf0e7a0 2162 assert(int_field);
c44c3e70 2163 type = bt_ctf_field_get_type(int_field);
fdf0e7a0 2164 assert(type);
e98a2d6e
PP
2165 break;
2166 default:
fdf0e7a0
PP
2167 BT_LOGF("Unexpected field type ID: "
2168 "notit-addr=%p, ft-addr=%p, ft-id=%s",
2169 notit, type,
2170 bt_ctf_field_type_id_string(
2171 bt_ctf_field_type_get_type_id(type)));
0fbb9a9f 2172 abort();
e98a2d6e
PP
2173 }
2174
fdf0e7a0 2175 assert(int_field);
5f870343 2176 ret = bt_ctf_field_signed_integer_set_value(int_field, value);
e98a2d6e
PP
2177 assert(!ret);
2178 stack_top(notit->stack)->index++;
f45bfe88 2179 status = update_clock(notit, int_field);
e98a2d6e
PP
2180 BT_PUT(field);
2181 BT_PUT(int_field);
c44c3e70 2182 BT_PUT(type);
fdf0e7a0 2183
c44c3e70 2184end_no_put:
e98a2d6e
PP
2185 return status;
2186}
2187
2188static
2189enum bt_ctf_btr_status btr_floating_point_cb(double value,
2190 struct bt_ctf_field_type *type, void *data)
2191{
2192 enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK;
2193 struct bt_ctf_field *field = NULL;
2194 struct bt_ctf_notif_iter *notit = data;
2195 int ret;
2196
fdf0e7a0
PP
2197 BT_LOGV("Floating point number function called from BTR: "
2198 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2199 "ft-id=%s, value=%f",
2200 notit, notit->btr, type,
2201 bt_ctf_field_type_id_string(
2202 bt_ctf_field_type_get_type_id(type)),
2203 value);
2204
e98a2d6e
PP
2205 /* Create next field */
2206 field = get_next_field(notit);
2207 if (!field) {
fdf0e7a0 2208 BT_LOGW("Cannot get next field: notit-addr=%p", notit);
e98a2d6e
PP
2209 status = BT_CTF_BTR_STATUS_ERROR;
2210 goto end;
2211 }
2212
2213 ret = bt_ctf_field_floating_point_set_value(field, value);
2214 assert(!ret);
2215 stack_top(notit->stack)->index++;
2216
2217end:
2218 BT_PUT(field);
e98a2d6e
PP
2219 return status;
2220}
2221
2222static
2223enum bt_ctf_btr_status btr_string_begin_cb(
2224 struct bt_ctf_field_type *type, void *data)
2225{
2226 enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK;
2227 struct bt_ctf_field *field = NULL;
2228 struct bt_ctf_notif_iter *notit = data;
2229 int ret;
2230
fdf0e7a0
PP
2231 BT_LOGV("String (beginning) function called from BTR: "
2232 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2233 "ft-id=%s",
2234 notit, notit->btr, type,
2235 bt_ctf_field_type_id_string(
2236 bt_ctf_field_type_get_type_id(type)));
2237
e98a2d6e
PP
2238 /* Create next field */
2239 field = get_next_field(notit);
2240 if (!field) {
fdf0e7a0 2241 BT_LOGW("Cannot get next field: notit-addr=%p", notit);
e98a2d6e
PP
2242 status = BT_CTF_BTR_STATUS_ERROR;
2243 goto end;
2244 }
2245
2246 /*
2247 * Push on stack. Not a compound type per se, but we know that only
2248 * btr_string_cb() may be called between this call and a subsequent
2249 * call to btr_string_end_cb().
2250 */
2251 ret = stack_push(notit->stack, field);
2252 if (ret) {
fdf0e7a0
PP
2253 BT_LOGE("Cannot push string field on stack: "
2254 "notit-addr=%p, field-addr=%p", notit, field);
e98a2d6e
PP
2255 status = BT_CTF_BTR_STATUS_ERROR;
2256 goto end;
2257 }
2258
e5df2ae3
JG
2259 /*
2260 * Initialize string field payload to an empty string since in the
2261 * case of a length 0 string the btr_string_cb won't be called and
2262 * we will end up with an unset string payload.
2263 */
2264 ret = bt_ctf_field_string_set_value(field, "");
2265 if (ret) {
fdf0e7a0
PP
2266 BT_LOGE("Cannot initialize string field's value to an empty string: "
2267 "notit-addr=%p, field-addr=%p, ret=%d",
2268 notit, field, ret);
e5df2ae3
JG
2269 status = BT_CTF_BTR_STATUS_ERROR;
2270 goto end;
2271 }
2272
e98a2d6e
PP
2273end:
2274 BT_PUT(field);
2275
2276 return status;
2277}
2278
2279static
2280enum bt_ctf_btr_status btr_string_cb(const char *value,
2281 size_t len, struct bt_ctf_field_type *type, void *data)
2282{
2283 enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK;
2284 struct bt_ctf_field *field = NULL;
2285 struct bt_ctf_notif_iter *notit = data;
2286 int ret;
2287
fdf0e7a0
PP
2288 BT_LOGV("String (substring) function called from BTR: "
2289 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2290 "ft-id=%s, string-length=%zu",
2291 notit, notit->btr, type,
2292 bt_ctf_field_type_id_string(
2293 bt_ctf_field_type_get_type_id(type)),
2294 len);
2295
e98a2d6e
PP
2296 /* Get string field */
2297 field = stack_top(notit->stack)->base;
2298 assert(field);
2299
2300 /* Append current string */
2301 ret = bt_ctf_field_string_append_len(field, value, len);
2302 if (ret) {
fdf0e7a0
PP
2303 BT_LOGE("Cannot append substring to string field's value: "
2304 "notit-addr=%p, field-addr=%p, string-length=%zu, "
2305 "ret=%d", notit, field, len, ret);
e98a2d6e
PP
2306 status = BT_CTF_BTR_STATUS_ERROR;
2307 goto end;
2308 }
2309
2310end:
2311 return status;
2312}
2313
2314static
2315enum bt_ctf_btr_status btr_string_end_cb(
2316 struct bt_ctf_field_type *type, void *data)
2317{
2318 struct bt_ctf_notif_iter *notit = data;
2319
fdf0e7a0
PP
2320 BT_LOGV("String (end) function called from BTR: "
2321 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2322 "ft-id=%s",
2323 notit, notit->btr, type,
2324 bt_ctf_field_type_id_string(
2325 bt_ctf_field_type_get_type_id(type)));
2326
e98a2d6e
PP
2327 /* Pop string field */
2328 stack_pop(notit->stack);
2329
2330 /* Go to next field */
2331 stack_top(notit->stack)->index++;
e98a2d6e
PP
2332 return BT_CTF_BTR_STATUS_OK;
2333}
2334
2335enum bt_ctf_btr_status btr_compound_begin_cb(
2336 struct bt_ctf_field_type *type, void *data)
2337{
2338 enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK;
2339 struct bt_ctf_notif_iter *notit = data;
2340 struct bt_ctf_field *field;
2341 int ret;
2342
fdf0e7a0
PP
2343 BT_LOGV("Compound (beginning) function called from BTR: "
2344 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2345 "ft-id=%s",
2346 notit, notit->btr, type,
2347 bt_ctf_field_type_id_string(
2348 bt_ctf_field_type_get_type_id(type)));
2349
e98a2d6e
PP
2350 /* Create field */
2351 if (stack_empty(notit->stack)) {
2352 /* Root: create dynamic scope field */
2353 *notit->cur_dscope_field = bt_ctf_field_create(type);
2354 field = *notit->cur_dscope_field;
2355
2356 /*
2357 * Field will be put at the end of this function
2358 * (stack_push() will take one reference, but this
2359 * reference is lost upon the equivalent stack_pop()
2360 * later), so also get it for our context to own it.
2361 */
2362 bt_get(*notit->cur_dscope_field);
fdf0e7a0
PP
2363
2364 if (!field) {
2365 BT_LOGE("Cannot create compound field: "
2366 "notit-addr=%p, ft-addr=%p, ft-id=%s",
2367 notit, type,
2368 bt_ctf_field_type_id_string(
2369 bt_ctf_field_type_get_type_id(type)));
2370 status = BT_CTF_BTR_STATUS_ERROR;
2371 goto end;
2372 }
e98a2d6e
PP
2373 } else {
2374 field = get_next_field(notit);
fdf0e7a0
PP
2375 if (!field) {
2376 BT_LOGW("Cannot get next field: notit-addr=%p", notit);
2377 status = BT_CTF_BTR_STATUS_ERROR;
2378 goto end;
2379 }
e98a2d6e
PP
2380 }
2381
2382 /* Push field */
fdf0e7a0 2383 assert(field);
e98a2d6e
PP
2384 ret = stack_push(notit->stack, field);
2385 if (ret) {
fdf0e7a0
PP
2386 BT_LOGE("Cannot push compound field onto the stack: "
2387 "notit-addr=%p, ft-addr=%p, ft-id=%s, ret=%d",
2388 notit, type,
2389 bt_ctf_field_type_id_string(
2390 bt_ctf_field_type_get_type_id(type)),
2391 ret);
e98a2d6e
PP
2392 status = BT_CTF_BTR_STATUS_ERROR;
2393 goto end;
2394 }
2395
2396end:
2397 BT_PUT(field);
2398
2399 return status;
2400}
2401
2402enum bt_ctf_btr_status btr_compound_end_cb(
2403 struct bt_ctf_field_type *type, void *data)
2404{
2405 struct bt_ctf_notif_iter *notit = data;
2406
fdf0e7a0
PP
2407 BT_LOGV("Compound (end) function called from BTR: "
2408 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2409 "ft-id=%s",
2410 notit, notit->btr, type,
2411 bt_ctf_field_type_id_string(
2412 bt_ctf_field_type_get_type_id(type)));
e98a2d6e
PP
2413 assert(!stack_empty(notit->stack));
2414
2415 /* Pop stack */
2416 stack_pop(notit->stack);
2417
2418 /* If the stack is not empty, increment the base's index */
2419 if (!stack_empty(notit->stack)) {
2420 stack_top(notit->stack)->index++;
2421 }
2422
2423 return BT_CTF_BTR_STATUS_OK;
2424}
2425
2426static
2427struct bt_ctf_field *resolve_field(struct bt_ctf_notif_iter *notit,
2428 struct bt_ctf_field_path *path)
2429{
2430 struct bt_ctf_field *field = NULL;
2431 unsigned int i;
2432
fdf0e7a0
PP
2433 if (BT_LOG_ON_VERBOSE) {
2434 GString *gstr = bt_ctf_field_path_string(path);
2435
2436 BT_LOGV("Resolving field path: notit-addr=%p, field-path=\"%s\"",
2437 notit, gstr ? gstr->str : NULL);
2438
2439 if (gstr) {
2440 g_string_free(gstr, TRUE);
2441 }
2442 }
2443
e98a2d6e
PP
2444 switch (bt_ctf_field_path_get_root_scope(path)) {
2445 case BT_CTF_SCOPE_TRACE_PACKET_HEADER:
2446 field = notit->dscopes.trace_packet_header;
2447 break;
2448 case BT_CTF_SCOPE_STREAM_PACKET_CONTEXT:
2449 field = notit->dscopes.stream_packet_context;
2450 break;
2451 case BT_CTF_SCOPE_STREAM_EVENT_HEADER:
2452 field = notit->dscopes.stream_event_header;
2453 break;
2454 case BT_CTF_SCOPE_STREAM_EVENT_CONTEXT:
2455 field = notit->dscopes.stream_event_context;
2456 break;
2457 case BT_CTF_SCOPE_EVENT_CONTEXT:
2458 field = notit->dscopes.event_context;
2459 break;
2460 case BT_CTF_SCOPE_EVENT_FIELDS:
2461 field = notit->dscopes.event_payload;
2462 break;
2463 default:
fdf0e7a0
PP
2464 BT_LOGF("Cannot resolve field path: unknown scope: "
2465 "notit-addr=%p, root-scope=%s",
2466 notit, bt_ctf_scope_string(
2467 bt_ctf_field_path_get_root_scope(path)));
2468 abort();
e98a2d6e
PP
2469 }
2470
2471 if (!field) {
fdf0e7a0
PP
2472 BT_LOGW("Cannot resolve field path: root field not found: "
2473 "notit-addr=%p, root-scope=%s",
2474 notit, bt_ctf_scope_string(
2475 bt_ctf_field_path_get_root_scope(path)));
e98a2d6e
PP
2476 goto end;
2477 }
2478
2479 bt_get(field);
2480
2481 for (i = 0; i < bt_ctf_field_path_get_index_count(path); ++i) {
2482 struct bt_ctf_field *next_field = NULL;
2483 struct bt_ctf_field_type *field_type;
2484 int index = bt_ctf_field_path_get_index(path, i);
2485
2486 field_type = bt_ctf_field_get_type(field);
fdf0e7a0 2487 assert(field_type);
e98a2d6e
PP
2488
2489 if (is_struct_type(field_type)) {
2490 next_field = bt_ctf_field_structure_get_field_by_index(
2491 field, index);
2492 } else if (is_variant_type(field_type)) {
2493 next_field =
2494 bt_ctf_field_variant_get_current_field(field);
2495 }
2496
2497 BT_PUT(field);
2498 BT_PUT(field_type);
2499
2500 if (!next_field) {
fdf0e7a0
PP
2501 BT_LOGW("Cannot find next field: "
2502 "notit-addr=%p, ft-addr=%p, ft-id=%s, index=%d",
2503 notit, field_type,
2504 bt_ctf_field_type_id_string(
2505 bt_ctf_field_type_get_type_id(field_type)),
2506 index);
e98a2d6e
PP
2507 goto end;
2508 }
2509
2510 /* Move next field -> field */
2511 BT_MOVE(field, next_field);
2512 }
2513
2514end:
2515 return field;
2516}
2517
2518static
2519int64_t btr_get_sequence_length_cb(struct bt_ctf_field_type *type, void *data)
2520{
2521 int64_t ret = -1;
2522 int iret;
fdf0e7a0 2523 struct bt_ctf_field *seq_field;
e98a2d6e
PP
2524 struct bt_ctf_field_path *field_path;
2525 struct bt_ctf_notif_iter *notit = data;
2cf1d51e 2526 struct bt_ctf_field *length_field = NULL;
e98a2d6e
PP
2527 uint64_t length;
2528
2529 field_path = bt_ctf_field_type_sequence_get_length_field_path(type);
fdf0e7a0 2530 assert(field_path);
2cf1d51e
JG
2531 length_field = resolve_field(notit, field_path);
2532 if (!length_field) {
fdf0e7a0
PP
2533 BT_LOGW("Cannot resolve sequence field type's length field path: "
2534 "notit-addr=%p, ft-addr=%p",
2535 notit, type);
e98a2d6e
PP
2536 goto end;
2537 }
2538
2cf1d51e 2539 iret = bt_ctf_field_unsigned_integer_get_value(length_field, &length);
e98a2d6e 2540 if (iret) {
fdf0e7a0
PP
2541 BT_LOGE("Cannot get value of sequence length field: "
2542 "notit-addr=%p, field-addr=%p",
2543 notit, length_field);
e98a2d6e
PP
2544 goto end;
2545 }
2546
fdf0e7a0
PP
2547 seq_field = stack_top(notit->stack)->base;
2548 iret = bt_ctf_field_sequence_set_length(seq_field, length_field);
2cf1d51e 2549 if (iret) {
fdf0e7a0
PP
2550 BT_LOGE("Cannot set sequence field's length field: "
2551 "notit-addr=%p, seq-field-addr=%p, "
2552 "length-field-addr=%p, ",
2553 notit, seq_field, length_field);
2cf1d51e
JG
2554 goto end;
2555 }
fdf0e7a0 2556
e98a2d6e
PP
2557 ret = (int64_t) length;
2558
2559end:
2cf1d51e 2560 BT_PUT(length_field);
e98a2d6e
PP
2561 BT_PUT(field_path);
2562
2563 return ret;
2564}
2565
2566static
2567struct bt_ctf_field_type *btr_get_variant_type_cb(
2568 struct bt_ctf_field_type *type, void *data)
2569{
2570 struct bt_ctf_field_path *path;
2571 struct bt_ctf_notif_iter *notit = data;
fdf0e7a0 2572 struct bt_ctf_field *var_field;
e98a2d6e
PP
2573 struct bt_ctf_field *tag_field = NULL;
2574 struct bt_ctf_field *selected_field = NULL;
2575 struct bt_ctf_field_type *selected_field_type = NULL;
2576
2577 path = bt_ctf_field_type_variant_get_tag_field_path(type);
fdf0e7a0 2578 assert(path);
e98a2d6e
PP
2579 tag_field = resolve_field(notit, path);
2580 if (!tag_field) {
fdf0e7a0
PP
2581 BT_LOGW("Cannot resolve variant field type's tag field path: "
2582 "notit-addr=%p, ft-addr=%p",
2583 notit, type);
e98a2d6e
PP
2584 goto end;
2585 }
2586
2587 /*
2588 * We found the enumeration tag field instance which should be
2589 * able to select a current field for this variant. This
2590 * callback function we're in is called _after_
2591 * compound_begin(), so the current stack top's base field is
2592 * the variant field in question. We get the selected field here
2593 * thanks to this tag field (thus creating the selected field),
2594 * which will also provide us with its type. Then, this field
2595 * will remain the current selected one until the next callback
2596 * function call which is used to fill the current selected
2597 * field.
2598 */
fdf0e7a0
PP
2599 var_field = stack_top(notit->stack)->base;
2600 selected_field = bt_ctf_field_variant_get_field(var_field, tag_field);
e98a2d6e 2601 if (!selected_field) {
fdf0e7a0
PP
2602 BT_LOGW("Cannot get variant field's selection using tag field: "
2603 "notit-addr=%p, var-field-addr=%p, tag-field-addr=%p",
2604 notit, var_field, tag_field);
e98a2d6e
PP
2605 goto end;
2606 }
2607
2608 selected_field_type = bt_ctf_field_get_type(selected_field);
2609
2610end:
2611 BT_PUT(tag_field);
2612 BT_PUT(selected_field);
f77ae72a 2613 BT_PUT(path);
e98a2d6e
PP
2614
2615 return selected_field_type;
2616}
2617
1556a1af
JG
2618static
2619int set_event_clocks(struct bt_ctf_event *event,
2620 struct bt_ctf_notif_iter *notit)
2621{
2622 int ret;
2623 GHashTableIter iter;
ac0c6bdd 2624 struct bt_ctf_clock_class *clock_class;
1556a1af
JG
2625 uint64_t *clock_state;
2626
2627 g_hash_table_iter_init(&iter, notit->clock_states);
2628
ac0c6bdd 2629 while (g_hash_table_iter_next(&iter, (gpointer) &clock_class,
1556a1af
JG
2630 (gpointer) &clock_state)) {
2631 struct bt_ctf_clock_value *clock_value;
2632
ac0c6bdd
PP
2633 clock_value = bt_ctf_clock_value_create(clock_class,
2634 *clock_state);
1556a1af 2635 if (!clock_value) {
fdf0e7a0
PP
2636 BT_LOGE("Cannot create clock value from clock class: "
2637 "notit-addr=%p, clock-class-addr=%p, "
2638 "clock-class-name=\"%s\"",
2639 notit, clock_class,
2640 bt_ctf_clock_class_get_name(clock_class));
1556a1af
JG
2641 ret = -1;
2642 goto end;
2643 }
3f3c46b8 2644 ret = bt_ctf_event_set_clock_value(event, clock_value);
1556a1af
JG
2645 bt_put(clock_value);
2646 if (ret) {
fdf0e7a0
PP
2647 struct bt_ctf_event_class *event_class =
2648 bt_ctf_event_get_class(event);
2649
2650 assert(event_class);
2651 BT_LOGE("Cannot set event's clock value: "
2652 "notit-addr=%p, event-addr=%p, "
2653 "event-class-name=\"%s\", "
2654 "event-class-id=%" PRId64 ", "
2655 "clock-class-addr=%p, "
2656 "clock-class-name=\"%s\", "
2657 "clock-value-addr=%p",
2658 notit, event,
2659 bt_ctf_event_class_get_name(event_class),
2660 bt_ctf_event_class_get_id(event_class),
2661 clock_class,
2662 bt_ctf_clock_class_get_name(clock_class),
2663 clock_value);
2664 bt_put(event_class);
1556a1af
JG
2665 goto end;
2666 }
2667 }
fdf0e7a0 2668
1556a1af
JG
2669 ret = 0;
2670end:
2671 return ret;
2672}
2673
78586d8a
JG
2674static
2675struct bt_ctf_event *create_event(struct bt_ctf_notif_iter *notit)
e98a2d6e 2676{
e98a2d6e 2677 int ret;
78586d8a 2678 struct bt_ctf_event *event;
e98a2d6e 2679
fdf0e7a0
PP
2680 BT_LOGV("Creating event for event notification: "
2681 "notit-addr=%p, event-class-addr=%p, "
2682 "event-class-name=\"%s\", "
2683 "event-class-id=%" PRId64,
2684 notit, notit->meta.event_class,
2685 bt_ctf_event_class_get_name(notit->meta.event_class),
2686 bt_ctf_event_class_get_id(notit->meta.event_class));
2687
78586d8a 2688 /* Create event object. */
e98a2d6e
PP
2689 event = bt_ctf_event_create(notit->meta.event_class);
2690 if (!event) {
fdf0e7a0
PP
2691 BT_LOGE("Cannot create event: "
2692 "notit-addr=%p, event-class-addr=%p, "
2693 "event-class-name=\"%s\", "
2694 "event-class-id=%" PRId64,
2695 notit, notit->meta.event_class,
2696 bt_ctf_event_class_get_name(notit->meta.event_class),
2697 bt_ctf_event_class_get_id(notit->meta.event_class));
e98a2d6e
PP
2698 goto error;
2699 }
2700
78586d8a 2701 /* Set header, stream event context, context, and payload fields. */
e98a2d6e
PP
2702 ret = bt_ctf_event_set_header(event,
2703 notit->dscopes.stream_event_header);
2704 if (ret) {
fdf0e7a0
PP
2705 BT_LOGE("Cannot set event's header field: "
2706 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2707 "event-class-name=\"%s\", "
2708 "event-class-id=%" PRId64 ", field-addr=%p",
2709 notit, event, notit->meta.event_class,
2710 bt_ctf_event_class_get_name(notit->meta.event_class),
2711 bt_ctf_event_class_get_id(notit->meta.event_class),
2712 notit->dscopes.stream_event_header);
e98a2d6e
PP
2713 goto error;
2714 }
2715
2716 ret = bt_ctf_event_set_stream_event_context(event,
2717 notit->dscopes.stream_event_context);
2718 if (ret) {
fdf0e7a0
PP
2719 BT_LOGE("Cannot set event's context field: "
2720 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2721 "event-class-name=\"%s\", "
2722 "event-class-id=%" PRId64 ", field-addr=%p",
2723 notit, event, notit->meta.event_class,
2724 bt_ctf_event_class_get_name(notit->meta.event_class),
2725 bt_ctf_event_class_get_id(notit->meta.event_class),
2726 notit->dscopes.stream_event_context);
e98a2d6e
PP
2727 goto error;
2728 }
2729
2730 ret = bt_ctf_event_set_event_context(event,
2731 notit->dscopes.event_context);
2732 if (ret) {
fdf0e7a0
PP
2733 BT_LOGE("Cannot set event's stream event context field: "
2734 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2735 "event-class-name=\"%s\", "
2736 "event-class-id=%" PRId64 ", field-addr=%p",
2737 notit, event, notit->meta.event_class,
2738 bt_ctf_event_class_get_name(notit->meta.event_class),
2739 bt_ctf_event_class_get_id(notit->meta.event_class),
2740 notit->dscopes.event_context);
e98a2d6e
PP
2741 goto error;
2742 }
2743
9ac68eb1 2744 ret = bt_ctf_event_set_event_payload(event,
e98a2d6e
PP
2745 notit->dscopes.event_payload);
2746 if (ret) {
fdf0e7a0
PP
2747 BT_LOGE("Cannot set event's payload field: "
2748 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2749 "event-class-name=\"%s\", "
2750 "event-class-id=%" PRId64 ", field-addr=%p",
2751 notit, event, notit->meta.event_class,
2752 bt_ctf_event_class_get_name(notit->meta.event_class),
2753 bt_ctf_event_class_get_id(notit->meta.event_class),
2754 notit->dscopes.event_payload);
e98a2d6e
PP
2755 goto error;
2756 }
2757
1556a1af
JG
2758 ret = set_event_clocks(event, notit);
2759 if (ret) {
fdf0e7a0
PP
2760 BT_LOGE("Cannot set event's clock values: "
2761 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2762 "event-class-name=\"%s\", "
2763 "event-class-id=%" PRId64,
2764 notit, event, notit->meta.event_class,
2765 bt_ctf_event_class_get_name(notit->meta.event_class),
2766 bt_ctf_event_class_get_id(notit->meta.event_class));
1556a1af
JG
2767 goto error;
2768 }
2769
78586d8a 2770 /* Associate with current packet. */
e98a2d6e
PP
2771 assert(notit->packet);
2772 ret = bt_ctf_event_set_packet(event, notit->packet);
2773 if (ret) {
fdf0e7a0
PP
2774 BT_LOGE("Cannot set event's header field: "
2775 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2776 "event-class-name=\"%s\", "
2777 "event-class-id=%" PRId64 ", packet-addr=%p",
2778 notit, event, notit->meta.event_class,
2779 bt_ctf_event_class_get_name(notit->meta.event_class),
2780 bt_ctf_event_class_get_id(notit->meta.event_class),
2781 notit->packet);
e98a2d6e
PP
2782 goto error;
2783 }
2784
2785 goto end;
fdf0e7a0 2786
e98a2d6e
PP
2787error:
2788 BT_PUT(event);
fdf0e7a0 2789
e98a2d6e
PP
2790end:
2791 return event;
2792}
2793
0659c536
PP
2794static
2795uint64_t get_cur_stream_instance_id(struct bt_ctf_notif_iter *notit)
2796{
2797 struct bt_ctf_field *stream_instance_id_field = NULL;
2798 uint64_t stream_instance_id = -1ULL;
2799 int ret;
2800
2801 if (!notit->dscopes.trace_packet_header) {
2802 goto end;
2803 }
2804
2805 stream_instance_id_field = bt_ctf_field_structure_get_field_by_name(
2806 notit->dscopes.trace_packet_header, "stream_instance_id");
2807 if (!stream_instance_id_field) {
2808 goto end;
2809 }
2810
2811 ret = bt_ctf_field_unsigned_integer_get_value(stream_instance_id_field,
2812 &stream_instance_id);
2813 if (ret) {
2814 stream_instance_id = -1ULL;
2815 goto end;
2816 }
2817
2818end:
2819 bt_put(stream_instance_id_field);
2820 return stream_instance_id;
2821}
2822
78586d8a 2823static
5184a1d6 2824int set_stream(struct bt_ctf_notif_iter *notit)
e98a2d6e 2825{
5184a1d6 2826 int ret = 0;
e98a2d6e 2827 struct bt_ctf_stream *stream = NULL;
e98a2d6e 2828
fdf0e7a0
PP
2829 BT_LOGV("Calling user function (get stream): notit-addr=%p, "
2830 "stream-class-addr=%p, stream-class-name=\"%s\", "
2831 "stream-class-id=%" PRId64,
2832 notit, notit->meta.stream_class,
2833 bt_ctf_stream_class_get_name(notit->meta.stream_class),
2834 bt_ctf_stream_class_get_id(notit->meta.stream_class));
5184a1d6 2835 stream = bt_get(notit->medium.medops.get_stream(
0659c536
PP
2836 notit->meta.stream_class, get_cur_stream_instance_id(notit),
2837 notit->medium.data));
5184a1d6 2838 BT_LOGV("User function returned: stream-addr=%p", stream);
e98a2d6e 2839 if (!stream) {
fdf0e7a0 2840 BT_LOGW_STR("User function failed to return a stream object for the given stream class.");
5184a1d6
PP
2841 ret = -1;
2842 goto end;
2843 }
2844
2845 if (notit->stream && stream != notit->stream) {
2846 BT_LOGW("User function returned a different stream than the previous one for the same sequence of packets.");
2847 ret = -1;
2848 goto end;
2849 }
2850
2851 BT_MOVE(notit->stream, stream);
2852
2853end:
2854 bt_put(stream);
2855 return ret;
2856}
2857
2858static
2859void create_packet(struct bt_ctf_notif_iter *notit)
2860{
2861 int ret;
2862 struct bt_ctf_packet *packet = NULL;
2863
2864 BT_LOGV("Creating packet for packet notification: "
2865 "notit-addr=%p", notit);
2866
2867 /* Ask the user for the stream */
2868 ret = set_stream(notit);
2869 if (ret) {
e98a2d6e
PP
2870 goto error;
2871 }
2872
fdf0e7a0
PP
2873 BT_LOGV("Creating packet from stream: "
2874 "notit-addr=%p, stream-addr=%p, "
2875 "stream-class-addr=%p, "
2876 "stream-class-name=\"%s\", "
2877 "stream-class-id=%" PRId64,
5184a1d6 2878 notit, notit->stream, notit->meta.stream_class,
fdf0e7a0
PP
2879 bt_ctf_stream_class_get_name(notit->meta.stream_class),
2880 bt_ctf_stream_class_get_id(notit->meta.stream_class));
2881
e98a2d6e 2882 /* Create packet */
5184a1d6 2883 packet = bt_ctf_packet_create(notit->stream);
e98a2d6e 2884 if (!packet) {
fdf0e7a0
PP
2885 BT_LOGE("Cannot create packet from stream: "
2886 "notit-addr=%p, stream-addr=%p, "
2887 "stream-class-addr=%p, "
2888 "stream-class-name=\"%s\", "
2889 "stream-class-id=%" PRId64,
5184a1d6 2890 notit, notit->stream, notit->meta.stream_class,
fdf0e7a0
PP
2891 bt_ctf_stream_class_get_name(notit->meta.stream_class),
2892 bt_ctf_stream_class_get_id(notit->meta.stream_class));
e98a2d6e
PP
2893 goto error;
2894 }
2895
2896 /* Set packet's context and header fields */
2897 if (notit->dscopes.trace_packet_header) {
2898 ret = bt_ctf_packet_set_header(packet,
fdf0e7a0 2899 notit->dscopes.trace_packet_header);
e98a2d6e 2900 if (ret) {
fdf0e7a0
PP
2901 BT_LOGE("Cannot set packet's header field: "
2902 "notit-addr=%p, packet-addr=%p, "
2903 "stream-addr=%p, "
2904 "stream-class-addr=%p, "
2905 "stream-class-name=\"%s\", "
2906 "stream-class-id=%" PRId64 ", "
2907 "field-addr=%p",
5184a1d6 2908 notit, packet, notit->stream, notit->meta.stream_class,
fdf0e7a0
PP
2909 bt_ctf_stream_class_get_name(notit->meta.stream_class),
2910 bt_ctf_stream_class_get_id(notit->meta.stream_class),
2911 notit->dscopes.trace_packet_header);
e98a2d6e
PP
2912 goto error;
2913 }
2914 }
2915
2916 if (notit->dscopes.stream_packet_context) {
2917 ret = bt_ctf_packet_set_context(packet,
fdf0e7a0 2918 notit->dscopes.stream_packet_context);
e98a2d6e 2919 if (ret) {
fdf0e7a0
PP
2920 BT_LOGE("Cannot set packet's context field: "
2921 "notit-addr=%p, packet-addr=%p, "
2922 "stream-addr=%p, "
2923 "stream-class-addr=%p, "
2924 "stream-class-name=\"%s\", "
2925 "stream-class-id=%" PRId64 ", "
2926 "field-addr=%p",
5184a1d6 2927 notit, packet, notit->stream, notit->meta.stream_class,
fdf0e7a0
PP
2928 bt_ctf_stream_class_get_name(notit->meta.stream_class),
2929 bt_ctf_stream_class_get_id(notit->meta.stream_class),
2930 notit->dscopes.trace_packet_header);
e98a2d6e
PP
2931 goto error;
2932 }
2933 }
2934
2935 goto end;
fdf0e7a0 2936
e98a2d6e
PP
2937error:
2938 BT_PUT(packet);
fdf0e7a0 2939
e98a2d6e
PP
2940end:
2941 BT_MOVE(notit->packet, packet);
2942}
2943
78586d8a
JG
2944static
2945void notify_new_packet(struct bt_ctf_notif_iter *notit,
2946 struct bt_notification **notification)
e98a2d6e 2947{
78586d8a 2948 struct bt_notification *ret;
e98a2d6e 2949
78586d8a 2950 /* Initialize the iterator's current packet */
e98a2d6e
PP
2951 create_packet(notit);
2952 if (!notit->packet) {
fdf0e7a0
PP
2953 BT_LOGE("Cannot create packet for packet notification: "
2954 "notit-addr=%p", notit);
78586d8a 2955 return;
e98a2d6e
PP
2956 }
2957
ea0e619e 2958 ret = bt_notification_packet_begin_create(notit->packet);
78586d8a 2959 if (!ret) {
fdf0e7a0
PP
2960 BT_LOGE("Cannot create packet beginning notification: "
2961 "notit-addr=%p, packet-addr=%p",
2962 notit, notit->packet);
78586d8a
JG
2963 return;
2964 }
2965 *notification = ret;
e98a2d6e
PP
2966}
2967
78586d8a
JG
2968static
2969void notify_end_of_packet(struct bt_ctf_notif_iter *notit,
2970 struct bt_notification **notification)
e98a2d6e 2971{
78586d8a 2972 struct bt_notification *ret;
e98a2d6e 2973
e98a2d6e 2974 if (!notit->packet) {
78586d8a 2975 return;
e98a2d6e
PP
2976 }
2977
78586d8a
JG
2978 ret = bt_notification_packet_end_create(notit->packet);
2979 if (!ret) {
fdf0e7a0
PP
2980 BT_LOGE("Cannot create packet end notification: "
2981 "notit-addr=%p, packet-addr=%p",
2982 notit, notit->packet);
78586d8a
JG
2983 return;
2984 }
2985 BT_PUT(notit->packet);
2986 *notification = ret;
e98a2d6e
PP
2987}
2988
78586d8a
JG
2989static
2990void notify_event(struct bt_ctf_notif_iter *notit,
0982a26d 2991 struct bt_clock_class_priority_map *cc_prio_map,
78586d8a 2992 struct bt_notification **notification)
e98a2d6e 2993{
9b876b7b 2994 struct bt_ctf_event *event = NULL;
78586d8a 2995 struct bt_notification *ret = NULL;
e98a2d6e 2996
9b876b7b
PP
2997 /* Make sure that the event contains at least one bit of data */
2998 if (notit->buf.at == notit->buf.last_eh_at) {
2999 BT_LOGE("Cannot create empty event with 0 bits of data: "
3000 "notit-addr=%p, packet-cur=%zu",
3001 notit, packet_at(notit));
3002 goto end;
3003 }
3004
e98a2d6e
PP
3005 /* Create event */
3006 event = create_event(notit);
3007 if (!event) {
fdf0e7a0
PP
3008 BT_LOGE("Cannot create event for event notification: "
3009 "notit-addr=%p", notit);
78586d8a 3010 goto end;
e98a2d6e 3011 }
e98a2d6e 3012
0982a26d 3013 ret = bt_notification_event_create(event, cc_prio_map);
78586d8a 3014 if (!ret) {
fdf0e7a0
PP
3015 BT_LOGE("Cannot create event notification: "
3016 "notit-addr=%p, event-addr=%p, "
3017 "cc-prio-map-addr=%p",
3018 notit, event, cc_prio_map);
78586d8a 3019 goto end;
e98a2d6e 3020 }
78586d8a
JG
3021 *notification = ret;
3022end:
3023 BT_PUT(event);
e98a2d6e
PP
3024}
3025
5f870343
JG
3026static
3027void init_trace_field_path_cache(struct bt_ctf_trace *trace,
3028 struct trace_field_path_cache *trace_field_path_cache)
3029{
3030 int stream_id = -1;
3031 int stream_instance_id = -1;
3032 int i, count;
3033 struct bt_ctf_field_type *packet_header = NULL;
3034
3035 packet_header = bt_ctf_trace_get_packet_header_type(trace);
3036 if (!packet_header) {
3037 goto end;
3038 }
3039
3040 if (!bt_ctf_field_type_is_structure(packet_header)) {
3041 goto end;
3042 }
3043
3044 count = bt_ctf_field_type_structure_get_field_count(packet_header);
fdf0e7a0 3045 assert(count >= 0);
5f870343
JG
3046
3047 for (i = 0; (i < count && (stream_id == -1 || stream_instance_id == -1)); i++) {
3048 int ret;
3049 const char *field_name;
3050
3051 ret = bt_ctf_field_type_structure_get_field(packet_header,
3052 &field_name, NULL, i);
3053 if (ret) {
fdf0e7a0
PP
3054 BT_LOGE("Cannot get structure field's field: "
3055 "field-addr=%p, index=%d",
3056 packet_header, i);
5f870343
JG
3057 goto end;
3058 }
3059
3060 if (stream_id == -1 && !strcmp(field_name, "stream_id")) {
3061 stream_id = i;
3062 } else if (stream_instance_id == -1 &&
3063 !strcmp(field_name, "stream_instance_id")) {
3064 stream_instance_id = i;
3065 }
3066 }
fdf0e7a0 3067
5f870343
JG
3068end:
3069 trace_field_path_cache->stream_id = stream_id;
3070 trace_field_path_cache->stream_instance_id = stream_instance_id;
3071 BT_PUT(packet_header);
3072}
3073
c44c3e70 3074BT_HIDDEN
e98a2d6e
PP
3075struct bt_ctf_notif_iter *bt_ctf_notif_iter_create(struct bt_ctf_trace *trace,
3076 size_t max_request_sz,
55314f2a 3077 struct bt_ctf_notif_iter_medium_ops medops, void *data)
e98a2d6e
PP
3078{
3079 struct bt_ctf_notif_iter *notit = NULL;
3080 struct bt_ctf_btr_cbs cbs = {
3081 .types = {
3082 .signed_int = btr_signed_int_cb,
3083 .unsigned_int = btr_unsigned_int_cb,
3084 .floating_point = btr_floating_point_cb,
3085 .string_begin = btr_string_begin_cb,
3086 .string = btr_string_cb,
3087 .string_end = btr_string_end_cb,
3088 .compound_begin = btr_compound_begin_cb,
3089 .compound_end = btr_compound_end_cb,
3090 },
3091 .query = {
3092 .get_sequence_length = btr_get_sequence_length_cb,
3093 .get_variant_type = btr_get_variant_type_cb,
3094 },
3095 };
3096
3097 assert(trace);
3098 assert(medops.request_bytes);
f3985ab1 3099 assert(medops.get_stream);
fdf0e7a0
PP
3100 BT_LOGD("Creating CTF plugin notification iterator: "
3101 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
3102 "data=%p",
3103 trace, bt_ctf_trace_get_name(trace), max_request_sz, data);
e98a2d6e
PP
3104 notit = g_new0(struct bt_ctf_notif_iter, 1);
3105 if (!notit) {
fdf0e7a0 3106 BT_LOGE_STR("Failed to allocate one CTF plugin notification iterator.");
e98a2d6e
PP
3107 goto end;
3108 }
c44c3e70 3109 notit->clock_states = g_hash_table_new_full(g_direct_hash,
fdf0e7a0 3110 g_direct_equal, bt_put, g_free);
c44c3e70 3111 if (!notit->clock_states) {
fdf0e7a0 3112 BT_LOGE_STR("Failed to allocate a GHashTable.");
c44c3e70
JG
3113 goto error;
3114 }
35d47007 3115 notit->meta.trace = bt_get(trace);
e98a2d6e
PP
3116 notit->medium.medops = medops;
3117 notit->medium.max_request_sz = max_request_sz;
3118 notit->medium.data = data;
e98a2d6e
PP
3119 notit->stack = stack_new(notit);
3120 if (!notit->stack) {
fdf0e7a0 3121 BT_LOGE_STR("Failed to create field stack.");
c44c3e70 3122 goto error;
e98a2d6e
PP
3123 }
3124
55314f2a 3125 notit->btr = bt_ctf_btr_create(cbs, notit);
e98a2d6e 3126 if (!notit->btr) {
fdf0e7a0 3127 BT_LOGE_STR("Failed to create binary type reader (BTR).");
c44c3e70 3128 goto error;
e98a2d6e
PP
3129 }
3130
3131 bt_ctf_notif_iter_reset(notit);
5f870343
JG
3132 init_trace_field_path_cache(trace, &notit->trace_field_path_cache);
3133 notit->sc_field_path_caches = g_hash_table_new_full(g_direct_hash,
fdf0e7a0 3134 g_direct_equal, bt_put, g_free);
5f870343 3135 if (!notit->sc_field_path_caches) {
fdf0e7a0 3136 BT_LOGE_STR("Failed to allocate a GHashTable.");
5f870343
JG
3137 goto error;
3138 }
3139
3140 notit->field_overrides = g_hash_table_new_full(g_direct_hash,
fdf0e7a0 3141 g_direct_equal, bt_put, g_free);
5f870343 3142 if (!notit->field_overrides) {
fdf0e7a0 3143 BT_LOGE_STR("Failed to allocate a GHashTable.");
5f870343
JG
3144 goto error;
3145 }
3146
fdf0e7a0
PP
3147 BT_LOGD("Created CTF plugin notification iterator: "
3148 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
3149 "data=%p, notit-addr=%p",
3150 trace, bt_ctf_trace_get_name(trace), max_request_sz, data,
3151 notit);
bb230c9b 3152 notit->cur_packet_offset = 0;
fdf0e7a0 3153
e98a2d6e
PP
3154end:
3155 return notit;
fdf0e7a0 3156
c44c3e70
JG
3157error:
3158 bt_ctf_notif_iter_destroy(notit);
3159 notit = NULL;
3160 goto end;
e98a2d6e
PP
3161}
3162
3163void bt_ctf_notif_iter_destroy(struct bt_ctf_notif_iter *notit)
3164{
3165 BT_PUT(notit->meta.trace);
3166 BT_PUT(notit->meta.stream_class);
3167 BT_PUT(notit->meta.event_class);
3168 BT_PUT(notit->packet);
5184a1d6 3169 BT_PUT(notit->stream);
5f870343 3170 BT_PUT(notit->cur_timestamp_end);
e98a2d6e
PP
3171 put_all_dscopes(notit);
3172
fdf0e7a0
PP
3173 BT_LOGD("Destroying CTF plugin notification iterator: addr=%p", notit);
3174
e98a2d6e 3175 if (notit->stack) {
fdf0e7a0 3176 BT_LOGD_STR("Destroying field stack.");
e98a2d6e
PP
3177 stack_destroy(notit->stack);
3178 }
3179
3180 if (notit->btr) {
fdf0e7a0 3181 BT_LOGD("Destroying BTR: btr-addr=%p", notit->btr);
e98a2d6e
PP
3182 bt_ctf_btr_destroy(notit->btr);
3183 }
3184
c44c3e70
JG
3185 if (notit->clock_states) {
3186 g_hash_table_destroy(notit->clock_states);
3187 }
5f870343
JG
3188
3189 if (notit->sc_field_path_caches) {
3190 g_hash_table_destroy(notit->sc_field_path_caches);
3191 }
3192
3193 if (notit->field_overrides) {
3194 g_hash_table_destroy(notit->field_overrides);
3195 }
fdf0e7a0 3196
e98a2d6e
PP
3197 g_free(notit);
3198}
3199
3200enum bt_ctf_notif_iter_status bt_ctf_notif_iter_get_next_notification(
3201 struct bt_ctf_notif_iter *notit,
0982a26d 3202 struct bt_clock_class_priority_map *cc_prio_map,
78586d8a 3203 struct bt_notification **notification)
e98a2d6e
PP
3204{
3205 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
3206
3207 assert(notit);
3208 assert(notification);
3209
fdf0e7a0
PP
3210 BT_LOGV("Getting next notification: notit-addr=%p, cc-prio-map-addr=%p",
3211 notit, cc_prio_map);
3212
e98a2d6e
PP
3213 while (true) {
3214 status = handle_state(notit);
7cdc2bab 3215 if (status == BT_CTF_NOTIF_ITER_STATUS_AGAIN) {
fdf0e7a0 3216 BT_LOGV_STR("Medium returned BT_CTF_NOTIF_ITER_STATUS_AGAIN.");
7cdc2bab
MD
3217 goto end;
3218 }
e98a2d6e
PP
3219 if (status != BT_CTF_NOTIF_ITER_STATUS_OK) {
3220 if (status == BT_CTF_NOTIF_ITER_STATUS_EOF) {
fdf0e7a0 3221 BT_LOGV_STR("Medium returned BT_CTF_NOTIF_ITER_STATUS_EOF.");
e98a2d6e 3222 } else {
fdf0e7a0
PP
3223 BT_LOGW("Cannot handle state: "
3224 "notit-addr=%p, state=%s",
3225 notit, state_string(notit->state));
e98a2d6e
PP
3226 }
3227 goto end;
3228 }
3229
3230 switch (notit->state) {
3231 case STATE_EMIT_NOTIF_NEW_PACKET:
fdf0e7a0 3232 /* notify_new_packet() logs errors */
e98a2d6e
PP
3233 notify_new_packet(notit, notification);
3234 if (!*notification) {
3235 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
3236 }
3237 goto end;
3238 case STATE_EMIT_NOTIF_EVENT:
fdf0e7a0 3239 /* notify_event() logs errors */
0982a26d 3240 notify_event(notit, cc_prio_map, notification);
e98a2d6e
PP
3241 if (!*notification) {
3242 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
3243 }
3244 goto end;
3245 case STATE_EMIT_NOTIF_END_OF_PACKET:
5f870343
JG
3246 /* Update clock with timestamp_end field. */
3247 if (notit->cur_timestamp_end) {
3248 enum bt_ctf_btr_status btr_status;
3249 struct bt_ctf_field_type *field_type =
3250 bt_ctf_field_get_type(
3251 notit->cur_timestamp_end);
3252
fdf0e7a0 3253 assert(field_type);
f45bfe88
PP
3254 btr_status = update_clock(notit,
3255 notit->cur_timestamp_end);
5f870343
JG
3256 BT_PUT(field_type);
3257 if (btr_status != BT_CTF_BTR_STATUS_OK) {
fdf0e7a0
PP
3258 BT_LOGW("Cannot update stream's clock value: "
3259 "notit-addr=%p", notit);
5f870343
JG
3260 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
3261 goto end;
3262 }
3263 }
3264
fdf0e7a0 3265 /* notify_end_of_packet() logs errors */
e98a2d6e
PP
3266 notify_end_of_packet(notit, notification);
3267 if (!*notification) {
3268 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
3269 }
3270 goto end;
3271 default:
3272 /* Non-emitting state: continue */
3273 break;
3274 }
3275 }
3276
3277end:
3278 return status;
3279}
87187cbf
PP
3280
3281BT_HIDDEN
3282enum bt_ctf_notif_iter_status bt_ctf_notif_iter_get_packet_header_context_fields(
3283 struct bt_ctf_notif_iter *notit,
3284 struct bt_ctf_field **packet_header_field,
3285 struct bt_ctf_field **packet_context_field)
3286{
bb230c9b 3287 int ret;
87187cbf
PP
3288 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
3289
3290 assert(notit);
3291
3292 if (notit->state == STATE_EMIT_NOTIF_NEW_PACKET) {
3293 /* We're already there */
3294 goto set_fields;
3295 }
3296
3297 while (true) {
3298 status = handle_state(notit);
3299 if (status == BT_CTF_NOTIF_ITER_STATUS_AGAIN) {
fdf0e7a0 3300 BT_LOGV_STR("Medium returned BT_CTF_NOTIF_ITER_STATUS_AGAIN.");
87187cbf
PP
3301 goto end;
3302 }
3303 if (status != BT_CTF_NOTIF_ITER_STATUS_OK) {
3304 if (status == BT_CTF_NOTIF_ITER_STATUS_EOF) {
fdf0e7a0 3305 BT_LOGV_STR("Medium returned BT_CTF_NOTIF_ITER_STATUS_EOF.");
87187cbf 3306 } else {
fdf0e7a0
PP
3307 BT_LOGW("Cannot handle state: "
3308 "notit-addr=%p, state=%s",
3309 notit, state_string(notit->state));
87187cbf
PP
3310 }
3311 goto end;
3312 }
3313
3314 switch (notit->state) {
3315 case STATE_EMIT_NOTIF_NEW_PACKET:
3316 /*
3317 * Packet header and context fields are
3318 * potentially decoded (or they don't exist).
3319 */
3320 goto set_fields;
3321 case STATE_INIT:
3322 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
3323 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
3324 case STATE_AFTER_TRACE_PACKET_HEADER:
3325 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN:
3326 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE:
3327 case STATE_AFTER_STREAM_PACKET_CONTEXT:
3328 /* Non-emitting state: continue */
3329 break;
3330 default:
3331 /*
3332 * We should never get past the
3333 * STATE_EMIT_NOTIF_NEW_PACKET state.
3334 */
fdf0e7a0
PP
3335 BT_LOGF("Unexpected state: notit-addr=%p, state=%s",
3336 notit, state_string(notit->state));
0fbb9a9f 3337 abort();
87187cbf
PP
3338 }
3339 }
3340
3341set_fields:
3342 if (packet_header_field) {
3343 *packet_header_field = bt_get(notit->dscopes.trace_packet_header);
3344 }
3345
3346 if (packet_context_field) {
3347 *packet_context_field = bt_get(notit->dscopes.stream_packet_context);
3348 }
3349
bb230c9b
JG
3350 ret = set_current_packet_content_sizes(notit);
3351 if (ret) {
3352 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
3353 goto end;
3354 }
87187cbf
PP
3355end:
3356 return status;
3357}
55fa6491
PP
3358
3359BT_HIDDEN
3360void bt_ctf_notif_iter_set_medops_data(struct bt_ctf_notif_iter *notit,
3361 void *medops_data)
3362{
3363 assert(notit);
3364 notit->medium.data = medops_data;
3365}
bb230c9b
JG
3366
3367BT_HIDDEN
3368enum bt_ctf_notif_iter_status bt_ctf_notif_iter_seek(
3369 struct bt_ctf_notif_iter *notit, off_t offset)
3370{
3371 enum bt_ctf_notif_iter_status ret = BT_CTF_NOTIF_ITER_STATUS_OK;
3372 enum bt_ctf_notif_iter_medium_status medium_status;
3373
3374 assert(notit);
3375 if (offset < 0) {
3376 BT_LOGE("Cannot seek to negative offset: offset=%jd", offset);
3377 ret = BT_CTF_NOTIF_ITER_STATUS_INVAL;
3378 goto end;
3379 }
3380
3381 if (!notit->medium.medops.seek) {
3382 ret = BT_CTF_NOTIF_ITER_STATUS_UNSUPPORTED;
3383 BT_LOGD("Aborting seek as the iterator's underlying media does not implement seek support.");
3384 goto end;
3385 }
3386
3387 medium_status = notit->medium.medops.seek(
3388 BT_CTF_NOTIF_ITER_SEEK_WHENCE_SET, offset,
3389 notit->medium.data);
3390 if (medium_status != BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK) {
3391 if (medium_status == BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF) {
3392 ret = BT_CTF_NOTIF_ITER_STATUS_EOF;
3393 } else {
3394 ret = BT_CTF_NOTIF_ITER_STATUS_ERROR;
3395 goto end;
3396 }
3397 }
3398
3399 bt_ctf_notif_iter_reset(notit);
3400 notit->cur_packet_offset = offset;
3401end:
3402 return ret;
3403}
3404
3405BT_HIDDEN
3406off_t bt_ctf_notif_iter_get_current_packet_offset(
3407 struct bt_ctf_notif_iter *notit)
3408{
3409 assert(notit);
3410 return notit->cur_packet_offset;
3411}
3412
3413BT_HIDDEN
3414off_t bt_ctf_notif_iter_get_current_packet_size(
3415 struct bt_ctf_notif_iter *notit)
3416{
3417 assert(notit);
3418 return notit->cur_packet_size;
3419}
This page took 0.194633 seconds and 4 git commands to generate.