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