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