fix leak of variant field path
[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
26#include <stdint.h>
27#include <inttypes.h>
28#include <stdio.h>
29#include <stddef.h>
30#include <stdbool.h>
31#include <assert.h>
32#include <string.h>
33#include <babeltrace/ctf-ir/field-types.h>
34#include <babeltrace/ctf-ir/field-path.h>
35#include <babeltrace/ctf-ir/fields.h>
36#include <babeltrace/ctf-ir/stream-class.h>
37#include <babeltrace/ctf-ir/packet.h>
38#include <babeltrace/ctf-ir/stream.h>
39#include <babeltrace/ctf-ir/clock.h>
40#include <babeltrace/ctf-ir/event-class.h>
78586d8a
JG
41#include <babeltrace/plugin/notification/packet.h>
42#include <babeltrace/plugin/notification/event.h>
e98a2d6e
PP
43#include <babeltrace/ref.h>
44#include <glib.h>
45
46#define PRINT_ERR_STREAM notit->err_stream
47#define PRINT_PREFIX "ctf-notif-iter"
48#include "print.h"
49
06a626b8
JG
50#include "notif-iter.h"
51#include "../btr/btr.h"
e98a2d6e
PP
52
53#define BYTES_TO_BITS(x) ((x) * 8)
54
55struct bt_ctf_notif_iter;
56
57/* A visit stack entry */
58struct stack_entry {
59 /*
60 * Current base field, one of:
61 *
62 * * string
63 * * structure
64 * * array
65 * * sequence
66 * * variant
67 *
68 * Field is owned by this.
69 */
70 struct bt_ctf_field *base;
71
72 /* index of next field to set */
73 size_t index;
74};
75
76/* Visit stack */
77struct stack {
78 /* Entries (struct stack_entry *) (top is last element) */
79 GPtrArray *entries;
e98a2d6e
PP
80};
81
82/* State */
83enum state {
84 STATE_INIT,
85 STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN,
86 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE,
87 STATE_AFTER_TRACE_PACKET_HEADER,
88 STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN,
89 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE,
90 STATE_AFTER_STREAM_PACKET_CONTEXT,
91 STATE_EMIT_NOTIF_NEW_PACKET,
92 STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN,
93 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE,
94 STATE_AFTER_STREAM_EVENT_HEADER,
95 STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN,
96 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE,
97 STATE_DSCOPE_EVENT_CONTEXT_BEGIN,
98 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE,
99 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN,
100 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE,
101 STATE_EMIT_NOTIF_EVENT,
102 STATE_EMIT_NOTIF_END_OF_PACKET,
103 STATE_SKIP_PACKET_PADDING,
104};
105
106/* CTF notification iterator */
107struct bt_ctf_notif_iter {
108 /* Visit stack */
109 struct stack *stack;
110
111 /* Error stream (may be NULL) */
112 FILE *err_stream;
113
114 /*
115 * Current dynamic scope field pointer.
116 *
117 * This is set when a dynamic scope field is first created by
118 * btr_compound_begin_cb(). It points to one of the fields in
119 * dscopes below.
120 */
121 struct bt_ctf_field **cur_dscope_field;
122
123 /* Trace and classes (owned by this) */
124 struct {
125 struct bt_ctf_trace *trace;
126 struct bt_ctf_stream_class *stream_class;
127 struct bt_ctf_event_class *event_class;
128 } meta;
129
130 /* Current packet (NULL if not created yet) */
131 struct bt_ctf_packet *packet;
132
133 /* Database of current dynamic scopes (owned by this) */
134 struct {
135 struct bt_ctf_field *trace_packet_header;
136 struct bt_ctf_field *stream_packet_context;
137 struct bt_ctf_field *stream_event_header;
138 struct bt_ctf_field *stream_event_context;
139 struct bt_ctf_field *event_context;
140 struct bt_ctf_field *event_payload;
141 } dscopes;
142
143 /* Current state */
144 enum state state;
145
2cf1d51e 146 /* Current medium buffer data */
e98a2d6e
PP
147 struct {
148 /* Last address provided by medium */
149 const uint8_t *addr;
150
151 /* Buffer size provided by medium (bytes) */
152 size_t sz;
153
154 /* Offset within whole packet of addr (bits) */
155 size_t packet_offset;
156
157 /* Current position from addr (bits) */
158 size_t at;
159 } buf;
160
161 /* Binary type reader */
162 struct bt_ctf_btr *btr;
163
2cf1d51e 164 /* Current medium data */
e98a2d6e
PP
165 struct {
166 struct bt_ctf_notif_iter_medium_ops medops;
167 size_t max_request_sz;
168 void *data;
169 } medium;
170
171 /* Current packet size (bits) (-1 if unknown) */
e7a4393b 172 int64_t cur_packet_size;
e98a2d6e
PP
173
174 /* Current content size (bits) (-1 if unknown) */
e7a4393b 175 int64_t cur_content_size;
e98a2d6e
PP
176};
177
2cf1d51e
JG
178static
179int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter *notit);
180
e98a2d6e
PP
181static
182void stack_entry_free_func(gpointer data)
183{
184 struct stack_entry *entry = data;
185
186 bt_put(entry->base);
187 g_free(entry);
188}
189
190static
191struct stack *stack_new(struct bt_ctf_notif_iter *notit)
192{
193 struct stack *stack = NULL;
194
195 stack = g_new0(struct stack, 1);
196 if (!stack) {
197 goto error;
198 }
199
200 stack->entries = g_ptr_array_new_with_free_func(stack_entry_free_func);
201 if (!stack->entries) {
202 goto error;
203 }
204
e98a2d6e 205 return stack;
e98a2d6e
PP
206error:
207 g_free(stack);
e98a2d6e
PP
208 return NULL;
209}
210
211static
212void stack_destroy(struct stack *stack)
213{
214 assert(stack);
215 g_ptr_array_free(stack->entries, TRUE);
216 g_free(stack);
217}
218
219static
220int stack_push(struct stack *stack, struct bt_ctf_field *base)
221{
222 int ret = 0;
223 struct stack_entry *entry;
e98a2d6e
PP
224
225 assert(stack);
226 assert(base);
e98a2d6e
PP
227 entry = g_new0(struct stack_entry, 1);
228 if (!entry) {
e98a2d6e
PP
229 ret = -1;
230 goto end;
231 }
232
233 entry->base = bt_get(base);
234 g_ptr_array_add(stack->entries, entry);
235
236end:
237 return ret;
238}
239
240static inline
241unsigned int stack_size(struct stack *stack)
242{
243 assert(stack);
244
245 return stack->entries->len;
246}
247
248static
249void stack_pop(struct stack *stack)
250{
251 assert(stack);
252 assert(stack_size(stack));
253 g_ptr_array_remove_index(stack->entries, stack->entries->len - 1);
254}
255
256static inline
257struct stack_entry *stack_top(struct stack *stack)
258{
259 assert(stack);
260 assert(stack_size(stack));
261
262 return g_ptr_array_index(stack->entries, stack->entries->len - 1);
263}
264
265static inline
266bool stack_empty(struct stack *stack)
267{
268 return stack_size(stack) == 0;
269}
270
271static
272void stack_clear(struct stack *stack)
273{
274 assert(stack);
275
276 if (!stack_empty(stack)) {
277 g_ptr_array_remove_range(stack->entries, 0, stack_size(stack));
278 }
279
280 assert(stack_empty(stack));
281}
282
283static inline
284enum bt_ctf_notif_iter_status notif_iter_status_from_m_status(
285 enum bt_ctf_notif_iter_medium_status m_status)
286{
287 return m_status;
288}
289
290static inline
291size_t buf_size_bits(struct bt_ctf_notif_iter *notit)
292{
293 return BYTES_TO_BITS(notit->buf.sz);
294}
295
296static inline
297size_t buf_available_bits(struct bt_ctf_notif_iter *notit)
298{
299 return buf_size_bits(notit) - notit->buf.at;
300}
301
302static inline
303size_t packet_at(struct bt_ctf_notif_iter *notit)
304{
305 return notit->buf.packet_offset + notit->buf.at;
306}
307
308static inline
309size_t remaining_content_bits(struct bt_ctf_notif_iter *notit)
310{
311 if (notit->cur_content_size == -1) {
312 return -1;
313 }
314
315 return notit->cur_content_size - packet_at(notit);
316}
317
318static inline
319size_t remaining_packet_bits(struct bt_ctf_notif_iter *notit)
320{
321 if (notit->cur_packet_size == -1) {
322 return -1;
323 }
324
325 return notit->cur_packet_size - packet_at(notit);
326}
327
328static inline
329void buf_consume_bits(struct bt_ctf_notif_iter *notit, size_t incr)
330{
331 notit->buf.at += incr;
332}
333
334static inline
335bool buf_has_enough_bits(struct bt_ctf_notif_iter *notit, size_t sz)
336{
337 return buf_available_bits(notit) >= sz;
338}
339
340static
2cf1d51e
JG
341enum bt_ctf_notif_iter_status request_medium_bytes(
342 struct bt_ctf_notif_iter *notit)
e98a2d6e
PP
343{
344 uint8_t *buffer_addr;
345 size_t buffer_sz;
346 enum bt_ctf_notif_iter_medium_status m_status;
347
348 m_status = notit->medium.medops.request_bytes(
349 notit->medium.max_request_sz, &buffer_addr,
350 &buffer_sz, notit->medium.data);
351 if (m_status == BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK) {
352 assert(buffer_sz != 0);
353
354 /* New packet offset is old one + old size (in bits) */
355 notit->buf.packet_offset += buf_size_bits(notit);
356
357 /* Restart at the beginning of the new medium buffer */
358 notit->buf.at = 0;
359
360 /* New medium buffer size */
361 notit->buf.sz = buffer_sz;
362
363 /* New medium buffer address */
364 notit->buf.addr = buffer_addr;
365 }
366
367 return notif_iter_status_from_m_status(m_status);
368}
369
370static inline
371enum bt_ctf_notif_iter_status buf_ensure_available_bits(
372 struct bt_ctf_notif_iter *notit)
373{
374 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
375
376 if (buf_available_bits(notit) == 0) {
377 /*
378 * This _cannot_ return BT_CTF_NOTIF_ITER_STATUS_OK
379 * _and_ no bits.
380 */
381 status = request_medium_bytes(notit);
382 }
383
384 return status;
385}
386
387static
388enum bt_ctf_notif_iter_status read_dscope_begin_state(
389 struct bt_ctf_notif_iter *notit,
390 struct bt_ctf_field_type *dscope_field_type,
391 enum state done_state, enum state continue_state,
392 struct bt_ctf_field **dscope_field)
393{
394 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
395 enum bt_ctf_btr_status btr_status;
396 size_t consumed_bits;
397
398 status = buf_ensure_available_bits(notit);
399 if (status != BT_CTF_NOTIF_ITER_STATUS_OK) {
400 goto end;
401 }
402
403 bt_put(*dscope_field);
404 notit->cur_dscope_field = dscope_field;
405 consumed_bits = bt_ctf_btr_start(notit->btr, dscope_field_type,
406 notit->buf.addr, notit->buf.at, packet_at(notit),
407 notit->buf.sz, &btr_status);
408
409 switch (btr_status) {
410 case BT_CTF_BTR_STATUS_OK:
411 /* type was read completely */
412 notit->state = done_state;
413 break;
414 case BT_CTF_BTR_STATUS_EOF:
415 notit->state = continue_state;
416 break;
417 default:
418 PERR("Binary type reader failed to start\n");
419 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
420 goto end;
421 }
422
423 /* Consume bits now since we know we're not in an error state */
424 buf_consume_bits(notit, consumed_bits);
425
426end:
427 return status;
428}
429
430static
431enum bt_ctf_notif_iter_status read_dscope_continue_state(
432 struct bt_ctf_notif_iter *notit, enum state done_state)
433{
434 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
435 enum bt_ctf_btr_status btr_status;
436 size_t consumed_bits;
437
438 status = buf_ensure_available_bits(notit);
439 if (status != BT_CTF_NOTIF_ITER_STATUS_OK) {
440 goto end;
441 }
442
443 consumed_bits = bt_ctf_btr_continue(notit->btr, notit->buf.addr,
78586d8a 444 notit->buf.sz, &btr_status);
e98a2d6e
PP
445
446 switch (btr_status) {
447 case BT_CTF_BTR_STATUS_OK:
78586d8a 448 /* Type was read completely. */
e98a2d6e
PP
449 notit->state = done_state;
450 break;
451 case BT_CTF_BTR_STATUS_EOF:
78586d8a 452 /* Stay in this continue state. */
e98a2d6e
PP
453 break;
454 default:
455 PERR("Binary type reader failed to continue\n");
456 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
457 goto end;
458 }
459
78586d8a 460 /* Consume bits now since we know we're not in an error state. */
e98a2d6e 461 buf_consume_bits(notit, consumed_bits);
e98a2d6e
PP
462end:
463 return status;
464}
465
466static
467void put_event_dscopes(struct bt_ctf_notif_iter *notit)
468{
469 BT_PUT(notit->dscopes.stream_event_header);
470 BT_PUT(notit->dscopes.stream_event_context);
471 BT_PUT(notit->dscopes.event_context);
472 BT_PUT(notit->dscopes.event_payload);
473}
474
475static
476void put_all_dscopes(struct bt_ctf_notif_iter *notit)
477{
478 BT_PUT(notit->dscopes.trace_packet_header);
479 BT_PUT(notit->dscopes.stream_packet_context);
480 put_event_dscopes(notit);
481}
482
483static
484enum bt_ctf_notif_iter_status read_packet_header_begin_state(
485 struct bt_ctf_notif_iter *notit)
486{
2cf1d51e
JG
487 struct bt_ctf_field_type *packet_header_type = NULL;
488 enum bt_ctf_notif_iter_status ret = BT_CTF_NOTIF_ITER_STATUS_OK;
e98a2d6e 489
2cf1d51e
JG
490 if (bt_ctf_notif_iter_switch_packet(notit)) {
491 ret = BT_CTF_NOTIF_ITER_STATUS_ERROR;
492 goto end;
493 }
e98a2d6e 494
2cf1d51e 495 /* Packet header type is common to the whole trace. */
e98a2d6e 496 packet_header_type = bt_ctf_trace_get_packet_header_type(
78586d8a 497 notit->meta.trace);
e98a2d6e
PP
498 if (!packet_header_type) {
499 PERR("Failed to retrieve trace's packet header type\n");
2cf1d51e 500 ret = BT_CTF_NOTIF_ITER_STATUS_ERROR;
e98a2d6e
PP
501 goto end;
502 }
503
2cf1d51e 504 ret = read_dscope_begin_state(notit, packet_header_type,
78586d8a
JG
505 STATE_AFTER_TRACE_PACKET_HEADER,
506 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE,
507 &notit->dscopes.trace_packet_header);
e98a2d6e
PP
508end:
509 BT_PUT(packet_header_type);
2cf1d51e 510 return ret;
e98a2d6e
PP
511}
512
513static
514enum bt_ctf_notif_iter_status read_packet_header_continue_state(
515 struct bt_ctf_notif_iter *notit)
516{
517 return read_dscope_continue_state(notit,
78586d8a 518 STATE_AFTER_TRACE_PACKET_HEADER);
e98a2d6e
PP
519}
520
521static inline
522bool is_struct_type(struct bt_ctf_field_type *field_type)
523{
78586d8a
JG
524 return bt_ctf_field_type_get_type_id(field_type) ==
525 BT_CTF_TYPE_ID_STRUCT;
e98a2d6e
PP
526}
527
528static inline
529bool is_variant_type(struct bt_ctf_field_type *field_type)
530{
78586d8a
JG
531 return bt_ctf_field_type_get_type_id(field_type) ==
532 BT_CTF_TYPE_ID_VARIANT;
e98a2d6e
PP
533}
534
535static inline
536enum bt_ctf_notif_iter_status set_current_stream_class(struct bt_ctf_notif_iter *notit)
537{
538 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
539 struct bt_ctf_field_type *packet_header_type;
540 struct bt_ctf_field_type *stream_id_field_type = NULL;
541 uint64_t stream_id;
542
543 /* Is there any "stream_id" field in the packet header? */
544 packet_header_type = bt_ctf_trace_get_packet_header_type(
78586d8a 545 notit->meta.trace);
e98a2d6e
PP
546 if (!packet_header_type) {
547 PERR("Failed to retrieve trace's packet header type\n");
548 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
549 goto end;
550 }
551
552 assert(is_struct_type(packet_header_type));
553
554 // TODO: optimalize!
555 stream_id_field_type =
78586d8a
JG
556 bt_ctf_field_type_structure_get_field_type_by_name(
557 packet_header_type, "stream_id");
e98a2d6e
PP
558 if (stream_id_field_type) {
559 /* Find appropriate stream class using current stream ID */
560 struct bt_ctf_field *stream_id_field = NULL;
561 int ret;
562
563 assert(notit->dscopes.trace_packet_header);
564
565 // TODO: optimalize!
566 stream_id_field = bt_ctf_field_structure_get_field(
78586d8a 567 notit->dscopes.trace_packet_header, "stream_id");
e98a2d6e
PP
568 assert(stream_id_field);
569 ret = bt_ctf_field_unsigned_integer_get_value(
78586d8a 570 stream_id_field, &stream_id);
e98a2d6e
PP
571 assert(!ret);
572 BT_PUT(stream_id_field);
573 } else {
574 /* Only one stream: pick the first stream class */
575 assert(bt_ctf_trace_get_stream_class_count(
78586d8a 576 notit->meta.trace) == 1);
e98a2d6e
PP
577 stream_id = 0;
578 }
579
580 BT_PUT(notit->meta.stream_class);
581
5b29e799 582 notit->meta.stream_class = bt_ctf_trace_get_stream_class_by_id(
78586d8a 583 notit->meta.trace, stream_id);
e98a2d6e
PP
584 if (!notit->meta.stream_class) {
585 PERR("Cannot find stream class with ID %" PRIu64 "\n",
586 stream_id);
587 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
588 goto end;
589 }
590
591end:
592 BT_PUT(packet_header_type);
593 BT_PUT(stream_id_field_type);
594
595 return status;
596}
597
598static
599enum bt_ctf_notif_iter_status after_packet_header_state(
600 struct bt_ctf_notif_iter *notit)
601{
602 enum bt_ctf_notif_iter_status status;
603
604 status = set_current_stream_class(notit);
605 if (status == BT_CTF_NOTIF_ITER_STATUS_OK) {
606 notit->state = STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN;
607 }
608
609 return status;
610}
611
612static
613enum bt_ctf_notif_iter_status read_packet_context_begin_state(
614 struct bt_ctf_notif_iter *notit)
615{
616 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
617 struct bt_ctf_field_type *packet_context_type;
618
619 assert(notit->meta.stream_class);
620 packet_context_type = bt_ctf_stream_class_get_packet_context_type(
78586d8a 621 notit->meta.stream_class);
e98a2d6e
PP
622 if (!packet_context_type) {
623 PERR("Failed to retrieve stream class's packet context\n");
624 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
625 goto end;
626 }
627
628 status = read_dscope_begin_state(notit, packet_context_type,
78586d8a
JG
629 STATE_AFTER_STREAM_PACKET_CONTEXT,
630 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE,
631 &notit->dscopes.stream_packet_context);
e98a2d6e
PP
632
633end:
634 BT_PUT(packet_context_type);
e98a2d6e
PP
635 return status;
636}
637
638static
639enum bt_ctf_notif_iter_status read_packet_context_continue_state(
640 struct bt_ctf_notif_iter *notit)
641{
642 return read_dscope_continue_state(notit,
78586d8a 643 STATE_AFTER_STREAM_PACKET_CONTEXT);
e98a2d6e
PP
644}
645
78586d8a 646static
e98a2d6e
PP
647enum bt_ctf_notif_iter_status set_current_packet_content_sizes(
648 struct bt_ctf_notif_iter *notit)
649{
650 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
651 struct bt_ctf_field *packet_size_field = NULL;
652 struct bt_ctf_field *content_size_field = NULL;
653 uint64_t content_size = -1, packet_size = -1;
654
655 assert(notit->dscopes.stream_packet_context);
656
e98a2d6e 657 packet_size_field = bt_ctf_field_structure_get_field(
78586d8a 658 notit->dscopes.stream_packet_context, "packet_size");
e98a2d6e 659 content_size_field = bt_ctf_field_structure_get_field(
78586d8a 660 notit->dscopes.stream_packet_context, "content_size");
e98a2d6e
PP
661 if (packet_size_field) {
662 int ret = bt_ctf_field_unsigned_integer_get_value(
78586d8a 663 packet_size_field, &packet_size);
e98a2d6e 664
78586d8a 665 assert(!ret);
e98a2d6e
PP
666 if (packet_size == 0) {
667 PERR("Decoded packet size is 0\n");
668 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
669 goto end;
670 } else if ((packet_size % 8) != 0) {
671 PERR("Decoded packet size is not a multiple of 8\n");
672 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
673 goto end;
674 }
675 }
78586d8a 676
e98a2d6e
PP
677 if (content_size_field) {
678 int ret = bt_ctf_field_unsigned_integer_get_value(
679 content_size_field, &content_size);
78586d8a 680
e98a2d6e
PP
681 assert(!ret);
682 } else {
683 content_size = packet_size;
684 }
685
686 notit->cur_packet_size = packet_size;
687 notit->cur_content_size = content_size;
e98a2d6e
PP
688end:
689 BT_PUT(packet_size_field);
690 BT_PUT(content_size_field);
e98a2d6e
PP
691 return status;
692}
693
694static
695enum bt_ctf_notif_iter_status after_packet_context_state(
696 struct bt_ctf_notif_iter *notit)
697{
698 enum bt_ctf_notif_iter_status status;
699
700 status = set_current_packet_content_sizes(notit);
701 if (status == BT_CTF_NOTIF_ITER_STATUS_OK) {
702 notit->state = STATE_EMIT_NOTIF_NEW_PACKET;
703 }
704
705 return status;
706}
707
708static
709enum bt_ctf_notif_iter_status read_event_header_begin_state(
710 struct bt_ctf_notif_iter *notit)
711{
712 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
713 struct bt_ctf_field_type *event_header_type = NULL;
714
715 /* Check if we have some content left */
716 if (notit->cur_content_size >= 0) {
717 if (packet_at(notit) == notit->cur_content_size) {
718 /* No more events! */
719 notit->state = STATE_EMIT_NOTIF_END_OF_PACKET;
720 goto end;
721 } else if (packet_at(notit) > notit->cur_content_size) {
722 /* That's not supposed to happen */
723 PERR("Cursor passed packet's content size:\n");
78586d8a 724 PERR("\tDecoded content size: %zu\n",
e98a2d6e 725 notit->cur_content_size);
78586d8a 726 PERR("\tCursor position: %zu\n", packet_at(notit));
e98a2d6e
PP
727 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
728 goto end;
729 }
730 }
731
732 event_header_type = bt_ctf_stream_class_get_event_header_type(
733 notit->meta.stream_class);
734 if (!event_header_type) {
735 PERR("Failed to retrieve stream class's event header type\n");
736 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
737 goto end;
738 }
739
740 put_event_dscopes(notit);
741 status = read_dscope_begin_state(notit, event_header_type,
742 STATE_AFTER_STREAM_EVENT_HEADER,
743 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE,
744 &notit->dscopes.stream_event_header);
e98a2d6e
PP
745end:
746 BT_PUT(event_header_type);
747
748 return status;
749}
750
751static
752enum bt_ctf_notif_iter_status read_event_header_continue_state(
753 struct bt_ctf_notif_iter *notit)
754{
755 return read_dscope_continue_state(notit,
756 STATE_AFTER_STREAM_EVENT_HEADER);
757}
758
759static inline
760enum bt_ctf_notif_iter_status set_current_event_class(struct bt_ctf_notif_iter *notit)
761{
762 /*
763 * The assert() calls in this function are okay because it is
764 * assumed here that all the metadata objects have been
765 * validated for CTF correctness before decoding actual streams.
766 */
767
768 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
769 struct bt_ctf_field_type *event_header_type;
770 struct bt_ctf_field_type *id_field_type = NULL;
771 struct bt_ctf_field_type *v_field_type = NULL;
772 uint64_t event_id = -1ULL;
773 int ret;
774
775 event_header_type = bt_ctf_stream_class_get_event_header_type(
776 notit->meta.stream_class);
777 if (!event_header_type) {
778 PERR("Failed to retrieve stream class's event header type\n");
779 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
780 goto end;
781 }
782
783 /* Is there any "id"/"v" field in the event header? */
784 assert(is_struct_type(event_header_type));
785 id_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
786 event_header_type, "id");
787 v_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
788 event_header_type, "v");
789 assert(notit->dscopes.stream_event_header);
790 if (v_field_type) {
791 /*
792 * _ _____ _____
793 * | | |_ _|_ _| __ __ _
794 * | | | | | || '_ \ / _` |
795 * | |___| | | || | | | (_| | S P E C I A L
796 * |_____|_| |_||_| |_|\__, | C A S E ™
797 * |___/
798 */
799 struct bt_ctf_field *v_field = NULL;
800 struct bt_ctf_field *v_struct_field = NULL;
801 struct bt_ctf_field *v_struct_id_field = NULL;
802
803 // TODO: optimalize!
804 v_field = bt_ctf_field_structure_get_field(
805 notit->dscopes.stream_event_header, "v");
806 assert(v_field);
807
808 v_struct_field =
809 bt_ctf_field_variant_get_current_field(v_field);
810 if (!v_struct_field) {
811 goto end_v_field_type;
812 }
813
814 // TODO: optimalize!
815 v_struct_id_field =
816 bt_ctf_field_structure_get_field(v_struct_field, "id");
817 if (!v_struct_id_field) {
818 goto end_v_field_type;
819 }
820
821 ret = bt_ctf_field_unsigned_integer_get_value(
822 v_struct_id_field, &event_id);
823 if (ret) {
824 event_id = -1ULL;
825 }
826
827end_v_field_type:
828 BT_PUT(v_field);
829 BT_PUT(v_struct_field);
830 BT_PUT(v_struct_id_field);
831 }
832
833 if (id_field_type && event_id == -1ULL) {
834 /* Check "id" field */
835 struct bt_ctf_field *id_field = NULL;
836 int ret;
837
838 // TODO: optimalize!
839 id_field = bt_ctf_field_structure_get_field(
840 notit->dscopes.stream_event_header, "id");
841 assert(id_field);
842 assert(bt_ctf_field_is_integer(id_field) ||
843 bt_ctf_field_is_enumeration(id_field));
844
845 if (bt_ctf_field_is_integer(id_field)) {
846 ret = bt_ctf_field_unsigned_integer_get_value(
847 id_field, &event_id);
848 } else {
849 struct bt_ctf_field *container;
850
851 container = bt_ctf_field_enumeration_get_container(
852 id_field);
853 assert(container);
854 ret = bt_ctf_field_unsigned_integer_get_value(
855 container, &event_id);
856 BT_PUT(container);
857 }
858 assert(!ret);
859 BT_PUT(id_field);
860 }
861
862 if (event_id == -1ULL) {
863 /* Event ID not found: single event? */
864 assert(bt_ctf_stream_class_get_event_class_count(
865 notit->meta.stream_class) == 1);
866 event_id = 0;
867 }
868
869 BT_PUT(notit->meta.event_class);
870 notit->meta.event_class = bt_ctf_stream_class_get_event_class_by_id(
871 notit->meta.stream_class, event_id);
872 if (!notit->meta.event_class) {
873 PERR("Cannot find event class with ID %" PRIu64 "\n", event_id);
874 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
875 goto end;
876 }
877
878end:
879 BT_PUT(event_header_type);
880 BT_PUT(id_field_type);
881 BT_PUT(v_field_type);
882
883 return status;
884}
885
886static
887enum bt_ctf_notif_iter_status after_event_header_state(
888 struct bt_ctf_notif_iter *notit)
889{
890 enum bt_ctf_notif_iter_status status;
891
e98a2d6e
PP
892 status = set_current_event_class(notit);
893 if (status != BT_CTF_NOTIF_ITER_STATUS_OK) {
894 PERR("Failed to set current event class\n");
895 goto end;
896 }
897
898 notit->state = STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN;
899
900end:
901 return status;
902}
903
904static
905enum bt_ctf_notif_iter_status read_stream_event_context_begin_state(
906 struct bt_ctf_notif_iter *notit)
907{
908 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
909 struct bt_ctf_field_type *stream_event_context_type;
910
911 stream_event_context_type = bt_ctf_stream_class_get_event_context_type(
912 notit->meta.stream_class);
913 if (!stream_event_context_type) {
914 PERR("Failed to retrieve stream class's event context type\n");
915 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
916 goto end;
917 }
918
919 status = read_dscope_begin_state(notit, stream_event_context_type,
920 STATE_DSCOPE_EVENT_CONTEXT_BEGIN,
921 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE,
922 &notit->dscopes.stream_event_context);
923
924end:
925 BT_PUT(stream_event_context_type);
926
927 return status;
928}
929
930static
931enum bt_ctf_notif_iter_status read_stream_event_context_continue_state(
932 struct bt_ctf_notif_iter *notit)
933{
934 return read_dscope_continue_state(notit,
935 STATE_DSCOPE_EVENT_CONTEXT_BEGIN);
936}
937
938static
939enum bt_ctf_notif_iter_status read_event_context_begin_state(
940 struct bt_ctf_notif_iter *notit)
941{
942 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
943 struct bt_ctf_field_type *event_context_type;
944
945 event_context_type = bt_ctf_event_class_get_context_type(
946 notit->meta.event_class);
947 if (!event_context_type) {
948 PERR("Failed to retrieve event class's context type\n");
949 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
950 goto end;
951 }
952
953 status = read_dscope_begin_state(notit, event_context_type,
954 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN,
955 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE,
956 &notit->dscopes.event_context);
957
958end:
959 BT_PUT(event_context_type);
960
961 return status;
962}
963
964static
965enum bt_ctf_notif_iter_status read_event_context_continue_state(
966 struct bt_ctf_notif_iter *notit)
967{
968 return read_dscope_continue_state(notit,
969 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN);
970}
971
972static
973enum bt_ctf_notif_iter_status read_event_payload_begin_state(
974 struct bt_ctf_notif_iter *notit)
975{
976 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
977 struct bt_ctf_field_type *event_payload_type;
978
979 event_payload_type = bt_ctf_event_class_get_payload_type(
980 notit->meta.event_class);
981 if (!event_payload_type) {
982 PERR("Failed to retrieve event class's payload type\n");
983 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
984 goto end;
985 }
986
987 status = read_dscope_begin_state(notit, event_payload_type,
988 STATE_EMIT_NOTIF_EVENT,
989 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE,
990 &notit->dscopes.event_payload);
991
992end:
993 BT_PUT(event_payload_type);
994
995 return status;
996}
997
998static
999enum bt_ctf_notif_iter_status read_event_payload_continue_state(
1000 struct bt_ctf_notif_iter *notit)
1001{
1002 return read_dscope_continue_state(notit, STATE_EMIT_NOTIF_EVENT);
1003}
1004
1005static
1006enum bt_ctf_notif_iter_status skip_packet_padding_state(
1007 struct bt_ctf_notif_iter *notit)
1008{
1009 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
1010 size_t bits_to_skip;
1011
1012 assert(notit->cur_packet_size > 0);
1013 bits_to_skip = notit->cur_packet_size - packet_at(notit);
1014 if (bits_to_skip == 0) {
1015 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1016 goto end;
1017 } else {
1018 size_t bits_to_consume;
1019 status = buf_ensure_available_bits(notit);
1020 if (status != BT_CTF_NOTIF_ITER_STATUS_OK) {
1021 goto end;
1022 }
1023
1024 bits_to_consume = MIN(buf_available_bits(notit), bits_to_skip);
1025 buf_consume_bits(notit, bits_to_consume);
1026 bits_to_skip = notit->cur_packet_size - packet_at(notit);
1027 if (bits_to_skip == 0) {
1028 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1029 goto end;
1030 }
1031 }
1032
1033end:
1034 return status;
1035}
1036
1037static inline
1038enum bt_ctf_notif_iter_status handle_state(struct bt_ctf_notif_iter *notit)
1039{
1040 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
1041
1042 PDBG("Handling state %d\n", notit->state);
1043
1044 // TODO: optimalize!
1045 switch (notit->state) {
1046 case STATE_INIT:
1047 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1048 break;
1049 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
1050 status = read_packet_header_begin_state(notit);
1051 break;
1052 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
1053 status = read_packet_header_continue_state(notit);
1054 break;
1055 case STATE_AFTER_TRACE_PACKET_HEADER:
1056 status = after_packet_header_state(notit);
1057 break;
1058 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN:
1059 status = read_packet_context_begin_state(notit);
1060 break;
1061 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE:
1062 status = read_packet_context_continue_state(notit);
1063 break;
1064 case STATE_AFTER_STREAM_PACKET_CONTEXT:
1065 status = after_packet_context_state(notit);
1066 break;
1067 case STATE_EMIT_NOTIF_NEW_PACKET:
1068 notit->state = STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN;
1069 break;
1070 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN:
1071 status = read_event_header_begin_state(notit);
1072 break;
1073 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE:
1074 status = read_event_header_continue_state(notit);
1075 break;
1076 case STATE_AFTER_STREAM_EVENT_HEADER:
1077 status = after_event_header_state(notit);
1078 break;
1079 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN:
1080 status = read_stream_event_context_begin_state(notit);
1081 break;
1082 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE:
1083 status = read_stream_event_context_continue_state(notit);
1084 break;
1085 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN:
1086 status = read_event_context_begin_state(notit);
1087 break;
1088 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE:
1089 status = read_event_context_continue_state(notit);
1090 break;
1091 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN:
1092 status = read_event_payload_begin_state(notit);
1093 break;
1094 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE:
1095 status = read_event_payload_continue_state(notit);
1096 break;
1097 case STATE_EMIT_NOTIF_EVENT:
1098 notit->state = STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN;
1099 break;
1100 case STATE_SKIP_PACKET_PADDING:
1101 status = skip_packet_padding_state(notit);
1102 break;
1103 case STATE_EMIT_NOTIF_END_OF_PACKET:
1104 notit->state = STATE_SKIP_PACKET_PADDING;
1105 break;
1106 }
1107
1108 return status;
1109}
1110
2cf1d51e
JG
1111
1112/**
1113 * Resets the internal state of a CTF notification iterator.
1114 *
1115 * This function can be used when it is desired to seek to the beginning
1116 * of another packet. It is expected that the next call to
1117 * bt_ctf_notif_iter_medium_ops::request_bytes() made by this
1118 * notification iterator will return the \em first bytes of a \em
1119 * packet.
1120 *
1121 * @param notif_iter CTF notification iterator
1122 */
1123static
e98a2d6e
PP
1124void bt_ctf_notif_iter_reset(struct bt_ctf_notif_iter *notit)
1125{
1126 assert(notit);
1127 stack_clear(notit->stack);
1128 BT_PUT(notit->meta.stream_class);
1129 BT_PUT(notit->meta.event_class);
1130 BT_PUT(notit->packet);
1131 put_all_dscopes(notit);
1132 notit->buf.addr = NULL;
1133 notit->buf.sz = 0;
1134 notit->buf.at = 0;
1135 notit->buf.packet_offset = 0;
1136 notit->state = STATE_INIT;
1137 notit->cur_content_size = -1;
1138 notit->cur_packet_size = -1;
1139}
1140
2cf1d51e
JG
1141static
1142int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter *notit)
1143{
1144 int ret = 0;
1145
1146 assert(notit);
1147 stack_clear(notit->stack);
1148 BT_PUT(notit->meta.stream_class);
1149 BT_PUT(notit->meta.event_class);
1150 BT_PUT(notit->packet);
1151 put_all_dscopes(notit);
1152
1153 /*
1154 * Adjust current buffer so that addr points to the beginning of the new
1155 * packet.
1156 */
1157 if (notit->buf.addr) {
1158 size_t consumed_bytes = (size_t) (notit->buf.at / CHAR_BIT);
1159
1160 /* Packets are assumed to start on a byte frontier. */
1161 if (notit->buf.at % CHAR_BIT) {
1162 ret = -1;
1163 goto end;
1164 }
1165
1166 notit->buf.addr += consumed_bytes;
1167 notit->buf.sz -= consumed_bytes;
1168 notit->buf.at = 0;
1169 notit->buf.packet_offset = 0;
1170 }
1171
1172 notit->cur_content_size = -1;
1173 notit->cur_packet_size = -1;
1174end:
1175 return ret;
1176}
1177
e98a2d6e
PP
1178static
1179struct bt_ctf_field *get_next_field(struct bt_ctf_notif_iter *notit)
1180{
1181 struct bt_ctf_field *next_field = NULL;
1182 struct bt_ctf_field *base_field;
1183 struct bt_ctf_field_type *base_type;
1184 size_t index;
1185
1186 assert(!stack_empty(notit->stack));
1187 index = stack_top(notit->stack)->index;
1188 base_field = stack_top(notit->stack)->base;
1189 base_type = bt_ctf_field_get_type(base_field);
1190 if (!base_type) {
1191 PERR("Failed to get base field's type\n");
1192 goto end;
1193 }
1194
1195 switch (bt_ctf_field_type_get_type_id(base_type)) {
1196 case BT_CTF_TYPE_ID_STRUCT:
1197 next_field = bt_ctf_field_structure_get_field_by_index(
1198 base_field, index);
1199 break;
1200 case BT_CTF_TYPE_ID_ARRAY:
1201 next_field = bt_ctf_field_array_get_field(base_field, index);
1202 break;
1203 case BT_CTF_TYPE_ID_SEQUENCE:
1204 next_field = bt_ctf_field_sequence_get_field(base_field, index);
1205 break;
1206 case BT_CTF_TYPE_ID_VARIANT:
1207 next_field = bt_ctf_field_variant_get_current_field(base_field);
1208 break;
1209 default:
1210 assert(false);
1211 break;
1212 }
1213
2cf1d51e
JG
1214 if (!next_field) {
1215 next_field = NULL;
1216 }
1217
e98a2d6e
PP
1218end:
1219 BT_PUT(base_type);
1220
1221 return next_field;
1222}
1223
1224static
1225enum bt_ctf_btr_status btr_signed_int_cb(int64_t value,
1226 struct bt_ctf_field_type *type, void *data)
1227{
1228 enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK;
1229 struct bt_ctf_field *field = NULL;
1230 struct bt_ctf_field *int_field = NULL;
1231 struct bt_ctf_notif_iter *notit = data;
1232 int ret;
1233
1234 /* create next field */
1235 field = get_next_field(notit);
1236 if (!field) {
1237 PERR("Failed to get next field (signed int)\n");
1238 status = BT_CTF_BTR_STATUS_ERROR;
1239 goto end;
1240 }
1241
1242 switch(bt_ctf_field_type_get_type_id(type)) {
1243 case BT_CTF_TYPE_ID_INTEGER:
1244 /* Integer field is created field */
1245 BT_MOVE(int_field, field);
1246 break;
1247 case BT_CTF_TYPE_ID_ENUM:
1248 int_field = bt_ctf_field_enumeration_get_container(field);
1249 break;
1250 default:
1251 break;
1252 }
1253
1254 if (!int_field) {
1255 PERR("Failed to get integer field\n");
1256 status = BT_CTF_BTR_STATUS_ERROR;
1257 goto end;
1258 }
1259
1260 ret = bt_ctf_field_signed_integer_set_value(int_field, value);
1261 assert(!ret);
1262 stack_top(notit->stack)->index++;
1263
1264end:
1265 BT_PUT(field);
1266 BT_PUT(int_field);
1267
1268 return status;
1269}
1270
1271static
1272enum bt_ctf_btr_status btr_unsigned_int_cb(uint64_t value,
1273 struct bt_ctf_field_type *type, void *data)
1274{
1275 enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK;
1276 struct bt_ctf_field *field = NULL;
1277 struct bt_ctf_field *int_field = NULL;
1278 struct bt_ctf_notif_iter *notit = data;
1279 int ret;
1280
1281 /* Create next field */
1282 field = get_next_field(notit);
1283 if (!field) {
1284 PERR("Failed to get next field (unsigned int)\n");
1285 status = BT_CTF_BTR_STATUS_ERROR;
1286 goto end;
1287 }
1288
1289 switch(bt_ctf_field_type_get_type_id(type)) {
1290 case BT_CTF_TYPE_ID_INTEGER:
1291 /* Integer field is created field */
1292 BT_MOVE(int_field, field);
1293 break;
1294 case BT_CTF_TYPE_ID_ENUM:
1295 int_field = bt_ctf_field_enumeration_get_container(field);
1296 break;
1297 default:
1298 break;
1299 }
1300
1301 if (!int_field) {
1302 PERR("Failed to get integer field\n");
1303 status = BT_CTF_BTR_STATUS_ERROR;
1304 goto end;
1305 }
1306
1307 ret = bt_ctf_field_unsigned_integer_set_value(int_field, value);
1308 assert(!ret);
1309 stack_top(notit->stack)->index++;
1310
1311end:
1312 BT_PUT(field);
1313 BT_PUT(int_field);
1314
1315 return status;
1316}
1317
1318static
1319enum bt_ctf_btr_status btr_floating_point_cb(double value,
1320 struct bt_ctf_field_type *type, void *data)
1321{
1322 enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK;
1323 struct bt_ctf_field *field = NULL;
1324 struct bt_ctf_notif_iter *notit = data;
1325 int ret;
1326
1327 /* Create next field */
1328 field = get_next_field(notit);
1329 if (!field) {
1330 PERR("Failed to get next field (floating point number)\n");
1331 status = BT_CTF_BTR_STATUS_ERROR;
1332 goto end;
1333 }
1334
1335 ret = bt_ctf_field_floating_point_set_value(field, value);
1336 assert(!ret);
1337 stack_top(notit->stack)->index++;
1338
1339end:
1340 BT_PUT(field);
1341
1342 return status;
1343}
1344
1345static
1346enum bt_ctf_btr_status btr_string_begin_cb(
1347 struct bt_ctf_field_type *type, void *data)
1348{
1349 enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK;
1350 struct bt_ctf_field *field = NULL;
1351 struct bt_ctf_notif_iter *notit = data;
1352 int ret;
1353
1354 /* Create next field */
1355 field = get_next_field(notit);
1356 if (!field) {
1357 PERR("Failed to get next field (string)\n");
1358 status = BT_CTF_BTR_STATUS_ERROR;
1359 goto end;
1360 }
1361
1362 /*
1363 * Push on stack. Not a compound type per se, but we know that only
1364 * btr_string_cb() may be called between this call and a subsequent
1365 * call to btr_string_end_cb().
1366 */
1367 ret = stack_push(notit->stack, field);
1368 if (ret) {
1369 PERR("Failed to push string field onto the stack\n");
1370 status = BT_CTF_BTR_STATUS_ERROR;
1371 goto end;
1372 }
1373
1374end:
1375 BT_PUT(field);
1376
1377 return status;
1378}
1379
1380static
1381enum bt_ctf_btr_status btr_string_cb(const char *value,
1382 size_t len, struct bt_ctf_field_type *type, void *data)
1383{
1384 enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK;
1385 struct bt_ctf_field *field = NULL;
1386 struct bt_ctf_notif_iter *notit = data;
1387 int ret;
1388
1389 /* Get string field */
1390 field = stack_top(notit->stack)->base;
1391 assert(field);
1392
1393 /* Append current string */
1394 ret = bt_ctf_field_string_append_len(field, value, len);
1395 if (ret) {
1396 PERR("Failed to append a string to a string field\n");
1397 status = BT_CTF_BTR_STATUS_ERROR;
1398 goto end;
1399 }
1400
1401end:
1402 return status;
1403}
1404
1405static
1406enum bt_ctf_btr_status btr_string_end_cb(
1407 struct bt_ctf_field_type *type, void *data)
1408{
1409 struct bt_ctf_notif_iter *notit = data;
1410
1411 /* Pop string field */
1412 stack_pop(notit->stack);
1413
1414 /* Go to next field */
1415 stack_top(notit->stack)->index++;
1416
1417 return BT_CTF_BTR_STATUS_OK;
1418}
1419
1420enum bt_ctf_btr_status btr_compound_begin_cb(
1421 struct bt_ctf_field_type *type, void *data)
1422{
1423 enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK;
1424 struct bt_ctf_notif_iter *notit = data;
1425 struct bt_ctf_field *field;
1426 int ret;
1427
1428 /* Create field */
1429 if (stack_empty(notit->stack)) {
1430 /* Root: create dynamic scope field */
1431 *notit->cur_dscope_field = bt_ctf_field_create(type);
1432 field = *notit->cur_dscope_field;
1433
1434 /*
1435 * Field will be put at the end of this function
1436 * (stack_push() will take one reference, but this
1437 * reference is lost upon the equivalent stack_pop()
1438 * later), so also get it for our context to own it.
1439 */
1440 bt_get(*notit->cur_dscope_field);
1441 } else {
1442 field = get_next_field(notit);
1443 }
1444
1445 if (!field) {
1446 PERR("Failed to get next field or create dynamic scope field\n");
1447 status = BT_CTF_BTR_STATUS_ERROR;
1448 goto end;
1449 }
1450
1451 /* Push field */
1452 ret = stack_push(notit->stack, field);
1453 if (ret) {
1454 PERR("Failed to push compound field onto the stack\n");
1455 status = BT_CTF_BTR_STATUS_ERROR;
1456 goto end;
1457 }
1458
1459end:
1460 BT_PUT(field);
1461
1462 return status;
1463}
1464
1465enum bt_ctf_btr_status btr_compound_end_cb(
1466 struct bt_ctf_field_type *type, void *data)
1467{
1468 struct bt_ctf_notif_iter *notit = data;
1469
1470 assert(!stack_empty(notit->stack));
1471
1472 /* Pop stack */
1473 stack_pop(notit->stack);
1474
1475 /* If the stack is not empty, increment the base's index */
1476 if (!stack_empty(notit->stack)) {
1477 stack_top(notit->stack)->index++;
1478 }
1479
1480 return BT_CTF_BTR_STATUS_OK;
1481}
1482
1483static
1484struct bt_ctf_field *resolve_field(struct bt_ctf_notif_iter *notit,
1485 struct bt_ctf_field_path *path)
1486{
1487 struct bt_ctf_field *field = NULL;
1488 unsigned int i;
1489
1490 switch (bt_ctf_field_path_get_root_scope(path)) {
1491 case BT_CTF_SCOPE_TRACE_PACKET_HEADER:
1492 field = notit->dscopes.trace_packet_header;
1493 break;
1494 case BT_CTF_SCOPE_STREAM_PACKET_CONTEXT:
1495 field = notit->dscopes.stream_packet_context;
1496 break;
1497 case BT_CTF_SCOPE_STREAM_EVENT_HEADER:
1498 field = notit->dscopes.stream_event_header;
1499 break;
1500 case BT_CTF_SCOPE_STREAM_EVENT_CONTEXT:
1501 field = notit->dscopes.stream_event_context;
1502 break;
1503 case BT_CTF_SCOPE_EVENT_CONTEXT:
1504 field = notit->dscopes.event_context;
1505 break;
1506 case BT_CTF_SCOPE_EVENT_FIELDS:
1507 field = notit->dscopes.event_payload;
1508 break;
1509 default:
1510 break;
1511 }
1512
1513 if (!field) {
1514 goto end;
1515 }
1516
1517 bt_get(field);
1518
1519 for (i = 0; i < bt_ctf_field_path_get_index_count(path); ++i) {
1520 struct bt_ctf_field *next_field = NULL;
1521 struct bt_ctf_field_type *field_type;
1522 int index = bt_ctf_field_path_get_index(path, i);
1523
1524 field_type = bt_ctf_field_get_type(field);
1525 if (!field_type) {
1526 BT_PUT(field);
1527 goto end;
1528 }
1529
1530 if (is_struct_type(field_type)) {
1531 next_field = bt_ctf_field_structure_get_field_by_index(
1532 field, index);
1533 } else if (is_variant_type(field_type)) {
1534 next_field =
1535 bt_ctf_field_variant_get_current_field(field);
1536 }
1537
1538 BT_PUT(field);
1539 BT_PUT(field_type);
1540
1541 if (!next_field) {
1542 goto end;
1543 }
1544
1545 /* Move next field -> field */
1546 BT_MOVE(field, next_field);
1547 }
1548
1549end:
1550 return field;
1551}
1552
1553static
1554int64_t btr_get_sequence_length_cb(struct bt_ctf_field_type *type, void *data)
1555{
1556 int64_t ret = -1;
1557 int iret;
1558 struct bt_ctf_field_path *field_path;
1559 struct bt_ctf_notif_iter *notit = data;
2cf1d51e 1560 struct bt_ctf_field *length_field = NULL;
e98a2d6e
PP
1561 uint64_t length;
1562
1563 field_path = bt_ctf_field_type_sequence_get_length_field_path(type);
1564 if (!field_path) {
1565 goto end;
1566 }
1567
2cf1d51e
JG
1568 length_field = resolve_field(notit, field_path);
1569 if (!length_field) {
e98a2d6e
PP
1570 goto end;
1571 }
1572
2cf1d51e 1573 iret = bt_ctf_field_unsigned_integer_get_value(length_field, &length);
e98a2d6e
PP
1574 if (iret) {
1575 goto end;
1576 }
1577
2cf1d51e
JG
1578 iret = bt_ctf_field_sequence_set_length(stack_top(notit->stack)->base,
1579 length_field);
1580 if (iret) {
1581 goto end;
1582 }
e98a2d6e
PP
1583 ret = (int64_t) length;
1584
1585end:
2cf1d51e 1586 BT_PUT(length_field);
e98a2d6e
PP
1587 BT_PUT(field_path);
1588
1589 return ret;
1590}
1591
1592static
1593struct bt_ctf_field_type *btr_get_variant_type_cb(
1594 struct bt_ctf_field_type *type, void *data)
1595{
1596 struct bt_ctf_field_path *path;
1597 struct bt_ctf_notif_iter *notit = data;
1598 struct bt_ctf_field *tag_field = NULL;
1599 struct bt_ctf_field *selected_field = NULL;
1600 struct bt_ctf_field_type *selected_field_type = NULL;
1601
1602 path = bt_ctf_field_type_variant_get_tag_field_path(type);
1603 if (!path) {
1604 goto end;
1605 }
1606
1607 tag_field = resolve_field(notit, path);
1608 if (!tag_field) {
1609 goto end;
1610 }
1611
1612 /*
1613 * We found the enumeration tag field instance which should be
1614 * able to select a current field for this variant. This
1615 * callback function we're in is called _after_
1616 * compound_begin(), so the current stack top's base field is
1617 * the variant field in question. We get the selected field here
1618 * thanks to this tag field (thus creating the selected field),
1619 * which will also provide us with its type. Then, this field
1620 * will remain the current selected one until the next callback
1621 * function call which is used to fill the current selected
1622 * field.
1623 */
1624 selected_field = bt_ctf_field_variant_get_field(
1625 stack_top(notit->stack)->base, tag_field);
1626 if (!selected_field) {
1627 goto end;
1628 }
1629
1630 selected_field_type = bt_ctf_field_get_type(selected_field);
1631
1632end:
1633 BT_PUT(tag_field);
1634 BT_PUT(selected_field);
f77ae72a 1635 BT_PUT(path);
e98a2d6e
PP
1636
1637 return selected_field_type;
1638}
1639
78586d8a
JG
1640static
1641struct bt_ctf_event *create_event(struct bt_ctf_notif_iter *notit)
e98a2d6e 1642{
e98a2d6e 1643 int ret;
78586d8a 1644 struct bt_ctf_event *event;
e98a2d6e 1645
78586d8a 1646 /* Create event object. */
e98a2d6e
PP
1647 event = bt_ctf_event_create(notit->meta.event_class);
1648 if (!event) {
1649 goto error;
1650 }
1651
78586d8a 1652 /* Set header, stream event context, context, and payload fields. */
e98a2d6e
PP
1653 ret = bt_ctf_event_set_header(event,
1654 notit->dscopes.stream_event_header);
1655 if (ret) {
1656 goto error;
1657 }
1658
1659 ret = bt_ctf_event_set_stream_event_context(event,
1660 notit->dscopes.stream_event_context);
1661 if (ret) {
1662 goto error;
1663 }
1664
1665 ret = bt_ctf_event_set_event_context(event,
1666 notit->dscopes.event_context);
1667 if (ret) {
1668 goto error;
1669 }
1670
1671 ret = bt_ctf_event_set_payload_field(event,
1672 notit->dscopes.event_payload);
1673 if (ret) {
1674 goto error;
1675 }
1676
78586d8a 1677 /* Associate with current packet. */
e98a2d6e
PP
1678 assert(notit->packet);
1679 ret = bt_ctf_event_set_packet(event, notit->packet);
1680 if (ret) {
1681 goto error;
1682 }
1683
1684 goto end;
e98a2d6e
PP
1685error:
1686 BT_PUT(event);
e98a2d6e
PP
1687end:
1688 return event;
1689}
1690
78586d8a
JG
1691static
1692void create_packet(struct bt_ctf_notif_iter *notit)
e98a2d6e
PP
1693{
1694 int ret;
1695 struct bt_ctf_stream *stream = NULL;
1696 struct bt_ctf_packet *packet = NULL;
1697
1698 /* Ask the user for the stream */
1699 stream = notit->medium.medops.get_stream(notit->meta.stream_class,
78586d8a 1700 notit->medium.data);
e98a2d6e
PP
1701 if (!stream) {
1702 goto error;
1703 }
1704
1705 /* Create packet */
1706 packet = bt_ctf_packet_create(stream);
1707 if (!packet) {
1708 goto error;
1709 }
1710
1711 /* Set packet's context and header fields */
1712 if (notit->dscopes.trace_packet_header) {
1713 ret = bt_ctf_packet_set_header(packet,
78586d8a 1714 notit->dscopes.trace_packet_header);
e98a2d6e
PP
1715 if (ret) {
1716 goto error;
1717 }
1718 }
1719
1720 if (notit->dscopes.stream_packet_context) {
1721 ret = bt_ctf_packet_set_context(packet,
78586d8a 1722 notit->dscopes.stream_packet_context);
e98a2d6e
PP
1723 if (ret) {
1724 goto error;
1725 }
1726 }
1727
1728 goto end;
e98a2d6e
PP
1729error:
1730 BT_PUT(packet);
e98a2d6e
PP
1731end:
1732 BT_MOVE(notit->packet, packet);
1733}
1734
78586d8a
JG
1735static
1736void notify_new_packet(struct bt_ctf_notif_iter *notit,
1737 struct bt_notification **notification)
e98a2d6e 1738{
78586d8a 1739 struct bt_notification *ret;
e98a2d6e 1740
78586d8a 1741 /* Initialize the iterator's current packet */
e98a2d6e
PP
1742 create_packet(notit);
1743 if (!notit->packet) {
78586d8a 1744 return;
e98a2d6e
PP
1745 }
1746
78586d8a
JG
1747 ret = bt_notification_packet_start_create(notit->packet);
1748 if (!ret) {
1749 return;
1750 }
1751 *notification = ret;
e98a2d6e
PP
1752}
1753
78586d8a
JG
1754static
1755void notify_end_of_packet(struct bt_ctf_notif_iter *notit,
1756 struct bt_notification **notification)
e98a2d6e 1757{
78586d8a 1758 struct bt_notification *ret;
e98a2d6e 1759
e98a2d6e 1760 if (!notit->packet) {
78586d8a 1761 return;
e98a2d6e
PP
1762 }
1763
78586d8a
JG
1764 ret = bt_notification_packet_end_create(notit->packet);
1765 if (!ret) {
1766 return;
1767 }
1768 BT_PUT(notit->packet);
1769 *notification = ret;
e98a2d6e
PP
1770}
1771
78586d8a
JG
1772static
1773void notify_event(struct bt_ctf_notif_iter *notit,
1774 struct bt_notification **notification)
e98a2d6e 1775{
78586d8a
JG
1776 struct bt_ctf_event *event;
1777 struct bt_notification *ret = NULL;
e98a2d6e
PP
1778
1779 /* Create event */
1780 event = create_event(notit);
1781 if (!event) {
78586d8a 1782 goto end;
e98a2d6e 1783 }
e98a2d6e 1784
78586d8a
JG
1785 ret = bt_notification_event_create(event);
1786 if (!ret) {
1787 goto end;
e98a2d6e 1788 }
78586d8a
JG
1789 *notification = ret;
1790end:
1791 BT_PUT(event);
e98a2d6e
PP
1792}
1793
043e2020
JG
1794static
1795void notify_eos(struct bt_ctf_notif_iter *notit,
1796 struct bt_notification **notification)
1797{
1798 struct bt_ctf_event *event;
1799 struct bt_notification *ret = NULL;
1800
1801 /* Create event */
1802 event = create_event(notit);
1803 if (!event) {
1804 goto end;
1805 }
1806
1807 ret = bt_notification_stream_end_create(event);
1808 if (!ret) {
1809 goto end;
1810 }
1811 *notification = ret;
1812end:
1813 BT_PUT(event);
1814}
1815
e98a2d6e
PP
1816struct bt_ctf_notif_iter *bt_ctf_notif_iter_create(struct bt_ctf_trace *trace,
1817 size_t max_request_sz,
1818 struct bt_ctf_notif_iter_medium_ops medops,
1819 void *data, FILE *err_stream)
1820{
1821 struct bt_ctf_notif_iter *notit = NULL;
1822 struct bt_ctf_btr_cbs cbs = {
1823 .types = {
1824 .signed_int = btr_signed_int_cb,
1825 .unsigned_int = btr_unsigned_int_cb,
1826 .floating_point = btr_floating_point_cb,
1827 .string_begin = btr_string_begin_cb,
1828 .string = btr_string_cb,
1829 .string_end = btr_string_end_cb,
1830 .compound_begin = btr_compound_begin_cb,
1831 .compound_end = btr_compound_end_cb,
1832 },
1833 .query = {
1834 .get_sequence_length = btr_get_sequence_length_cb,
1835 .get_variant_type = btr_get_variant_type_cb,
1836 },
1837 };
1838
1839 assert(trace);
1840 assert(medops.request_bytes);
1841 notit = g_new0(struct bt_ctf_notif_iter, 1);
1842 if (!notit) {
1843 PERR("Failed to allocate memory for CTF notification iterator\n");
1844 goto end;
1845 }
1846
1847 notit->meta.trace = trace;
1848 bt_get(notit->meta.trace);
1849 notit->medium.medops = medops;
1850 notit->medium.max_request_sz = max_request_sz;
1851 notit->medium.data = data;
1852 notit->err_stream = err_stream;
1853 notit->stack = stack_new(notit);
1854 if (!notit->stack) {
1855 PERR("Failed to create stack\n");
1856 bt_ctf_notif_iter_destroy(notit);
1857 notit = NULL;
1858 goto end;
1859 }
1860
1861 notit->btr = bt_ctf_btr_create(cbs, notit, err_stream);
1862 if (!notit->btr) {
1863 PERR("Failed to create binary type reader\n");
1864 bt_ctf_notif_iter_destroy(notit);
1865 notit = NULL;
1866 goto end;
1867 }
1868
1869 bt_ctf_notif_iter_reset(notit);
1870
1871end:
1872 return notit;
1873}
1874
1875void bt_ctf_notif_iter_destroy(struct bt_ctf_notif_iter *notit)
1876{
1877 BT_PUT(notit->meta.trace);
1878 BT_PUT(notit->meta.stream_class);
1879 BT_PUT(notit->meta.event_class);
1880 BT_PUT(notit->packet);
1881 put_all_dscopes(notit);
1882
1883 if (notit->stack) {
1884 stack_destroy(notit->stack);
1885 }
1886
1887 if (notit->btr) {
1888 bt_ctf_btr_destroy(notit->btr);
1889 }
1890
1891 g_free(notit);
1892}
1893
1894enum bt_ctf_notif_iter_status bt_ctf_notif_iter_get_next_notification(
1895 struct bt_ctf_notif_iter *notit,
78586d8a 1896 struct bt_notification **notification)
e98a2d6e
PP
1897{
1898 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
1899
1900 assert(notit);
1901 assert(notification);
1902
1903 while (true) {
1904 status = handle_state(notit);
1905 if (status != BT_CTF_NOTIF_ITER_STATUS_OK) {
1906 if (status == BT_CTF_NOTIF_ITER_STATUS_EOF) {
78586d8a 1907 PDBG("Medium operation reported end of stream\n");
e98a2d6e
PP
1908 } else {
1909 PERR("Failed to handle state:\n");
78586d8a 1910 PERR("\tState: %d\n", notit->state);
e98a2d6e
PP
1911 }
1912 goto end;
1913 }
1914
1915 switch (notit->state) {
1916 case STATE_EMIT_NOTIF_NEW_PACKET:
1917 PDBG("Emitting new packet notification\n");
1918 notify_new_packet(notit, notification);
1919 if (!*notification) {
1920 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
1921 }
1922 goto end;
1923 case STATE_EMIT_NOTIF_EVENT:
1924 PDBG("Emitting event notification\n");
1925 notify_event(notit, notification);
1926 if (!*notification) {
1927 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
1928 }
1929 goto end;
1930 case STATE_EMIT_NOTIF_END_OF_PACKET:
1931 PDBG("Emitting end of packet notification\n");
1932 notify_end_of_packet(notit, notification);
1933 if (!*notification) {
1934 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
1935 }
1936 goto end;
1937 default:
1938 /* Non-emitting state: continue */
1939 break;
1940 }
1941 }
1942
1943end:
1944 return status;
1945}
This page took 0.100059 seconds and 4 git commands to generate.