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