Merge streams in ctf fs component
[babeltrace.git] / plugins / ctf / common / notif-iter / notif-iter.c
1 /*
2 * Babeltrace - CTF notification iterator
3 *
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>
41 #include <babeltrace/plugin/notification/packet.h>
42 #include <babeltrace/plugin/notification/event.h>
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
50 #include "notif-iter.h"
51 #include "../btr/btr.h"
52
53 #define BYTES_TO_BITS(x) ((x) * 8)
54
55 struct bt_ctf_notif_iter;
56
57 /* A visit stack entry */
58 struct 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 */
77 struct stack {
78 /* Entries (struct stack_entry *) (top is last element) */
79 GPtrArray *entries;
80 };
81
82 /* State */
83 enum 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 */
107 struct 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
146 /* Current medium buffer data */
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
164 /* Current medium data */
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) */
172 int64_t cur_packet_size;
173
174 /* Current content size (bits) (-1 if unknown) */
175 int64_t cur_content_size;
176 };
177
178 static
179 int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter *notit);
180
181 static
182 void 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
190 static
191 struct 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
205 return stack;
206 error:
207 g_free(stack);
208 return NULL;
209 }
210
211 static
212 void stack_destroy(struct stack *stack)
213 {
214 assert(stack);
215 g_ptr_array_free(stack->entries, TRUE);
216 g_free(stack);
217 }
218
219 static
220 int stack_push(struct stack *stack, struct bt_ctf_field *base)
221 {
222 int ret = 0;
223 struct stack_entry *entry;
224
225 assert(stack);
226 assert(base);
227 entry = g_new0(struct stack_entry, 1);
228 if (!entry) {
229 ret = -1;
230 goto end;
231 }
232
233 entry->base = bt_get(base);
234 g_ptr_array_add(stack->entries, entry);
235
236 end:
237 return ret;
238 }
239
240 static inline
241 unsigned int stack_size(struct stack *stack)
242 {
243 assert(stack);
244
245 return stack->entries->len;
246 }
247
248 static
249 void 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
256 static inline
257 struct 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
265 static inline
266 bool stack_empty(struct stack *stack)
267 {
268 return stack_size(stack) == 0;
269 }
270
271 static
272 void 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
283 static inline
284 enum 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
290 static inline
291 size_t buf_size_bits(struct bt_ctf_notif_iter *notit)
292 {
293 return BYTES_TO_BITS(notit->buf.sz);
294 }
295
296 static inline
297 size_t buf_available_bits(struct bt_ctf_notif_iter *notit)
298 {
299 return buf_size_bits(notit) - notit->buf.at;
300 }
301
302 static inline
303 size_t packet_at(struct bt_ctf_notif_iter *notit)
304 {
305 return notit->buf.packet_offset + notit->buf.at;
306 }
307
308 static inline
309 size_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
318 static inline
319 size_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
328 static inline
329 void buf_consume_bits(struct bt_ctf_notif_iter *notit, size_t incr)
330 {
331 notit->buf.at += incr;
332 }
333
334 static inline
335 bool buf_has_enough_bits(struct bt_ctf_notif_iter *notit, size_t sz)
336 {
337 return buf_available_bits(notit) >= sz;
338 }
339
340 static
341 enum bt_ctf_notif_iter_status request_medium_bytes(
342 struct bt_ctf_notif_iter *notit)
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
370 static inline
371 enum 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
387 static
388 enum 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
426 end:
427 return status;
428 }
429
430 static
431 enum 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,
444 notit->buf.sz, &btr_status);
445
446 switch (btr_status) {
447 case BT_CTF_BTR_STATUS_OK:
448 /* Type was read completely. */
449 notit->state = done_state;
450 break;
451 case BT_CTF_BTR_STATUS_EOF:
452 /* Stay in this continue state. */
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
460 /* Consume bits now since we know we're not in an error state. */
461 buf_consume_bits(notit, consumed_bits);
462 end:
463 return status;
464 }
465
466 static
467 void 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
475 static
476 void 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
483 static
484 enum bt_ctf_notif_iter_status read_packet_header_begin_state(
485 struct bt_ctf_notif_iter *notit)
486 {
487 struct bt_ctf_field_type *packet_header_type = NULL;
488 enum bt_ctf_notif_iter_status ret = BT_CTF_NOTIF_ITER_STATUS_OK;
489
490 if (bt_ctf_notif_iter_switch_packet(notit)) {
491 ret = BT_CTF_NOTIF_ITER_STATUS_ERROR;
492 goto end;
493 }
494
495 /* Packet header type is common to the whole trace. */
496 packet_header_type = bt_ctf_trace_get_packet_header_type(
497 notit->meta.trace);
498 if (!packet_header_type) {
499 PERR("Failed to retrieve trace's packet header type\n");
500 ret = BT_CTF_NOTIF_ITER_STATUS_ERROR;
501 goto end;
502 }
503
504 ret = read_dscope_begin_state(notit, packet_header_type,
505 STATE_AFTER_TRACE_PACKET_HEADER,
506 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE,
507 &notit->dscopes.trace_packet_header);
508 end:
509 BT_PUT(packet_header_type);
510 return ret;
511 }
512
513 static
514 enum 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,
518 STATE_AFTER_TRACE_PACKET_HEADER);
519 }
520
521 static inline
522 bool is_struct_type(struct bt_ctf_field_type *field_type)
523 {
524 return bt_ctf_field_type_get_type_id(field_type) ==
525 BT_CTF_TYPE_ID_STRUCT;
526 }
527
528 static inline
529 bool is_variant_type(struct bt_ctf_field_type *field_type)
530 {
531 return bt_ctf_field_type_get_type_id(field_type) ==
532 BT_CTF_TYPE_ID_VARIANT;
533 }
534
535 static inline
536 enum 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(
545 notit->meta.trace);
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 =
556 bt_ctf_field_type_structure_get_field_type_by_name(
557 packet_header_type, "stream_id");
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(
567 notit->dscopes.trace_packet_header, "stream_id");
568 assert(stream_id_field);
569 ret = bt_ctf_field_unsigned_integer_get_value(
570 stream_id_field, &stream_id);
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(
576 notit->meta.trace) == 1);
577 stream_id = 0;
578 }
579
580 BT_PUT(notit->meta.stream_class);
581
582 notit->meta.stream_class = bt_ctf_trace_get_stream_class_by_id(
583 notit->meta.trace, stream_id);
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
591 end:
592 BT_PUT(packet_header_type);
593 BT_PUT(stream_id_field_type);
594
595 return status;
596 }
597
598 static
599 enum 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
612 static
613 enum 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(
621 notit->meta.stream_class);
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,
629 STATE_AFTER_STREAM_PACKET_CONTEXT,
630 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE,
631 &notit->dscopes.stream_packet_context);
632
633 end:
634 BT_PUT(packet_context_type);
635 return status;
636 }
637
638 static
639 enum 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,
643 STATE_AFTER_STREAM_PACKET_CONTEXT);
644 }
645
646 static
647 enum 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
657 packet_size_field = bt_ctf_field_structure_get_field(
658 notit->dscopes.stream_packet_context, "packet_size");
659 content_size_field = bt_ctf_field_structure_get_field(
660 notit->dscopes.stream_packet_context, "content_size");
661 if (packet_size_field) {
662 int ret = bt_ctf_field_unsigned_integer_get_value(
663 packet_size_field, &packet_size);
664
665 assert(!ret);
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 }
676
677 if (content_size_field) {
678 int ret = bt_ctf_field_unsigned_integer_get_value(
679 content_size_field, &content_size);
680
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;
688 end:
689 BT_PUT(packet_size_field);
690 BT_PUT(content_size_field);
691 return status;
692 }
693
694 static
695 enum 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
708 static
709 enum 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");
724 PERR("\tDecoded content size: %zu\n",
725 notit->cur_content_size);
726 PERR("\tCursor position: %zu\n", packet_at(notit));
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);
745 end:
746 BT_PUT(event_header_type);
747
748 return status;
749 }
750
751 static
752 enum 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
759 static inline
760 enum 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
827 end_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
878 end:
879 BT_PUT(event_header_type);
880 BT_PUT(id_field_type);
881 BT_PUT(v_field_type);
882
883 return status;
884 }
885
886 static
887 enum 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
892 status = set_current_packet_content_sizes(notit);
893 if (status != BT_CTF_NOTIF_ITER_STATUS_OK) {
894 PERR("Failed to set current packet and content sizes\n");
895 goto end;
896 }
897
898 status = set_current_event_class(notit);
899 if (status != BT_CTF_NOTIF_ITER_STATUS_OK) {
900 PERR("Failed to set current event class\n");
901 goto end;
902 }
903
904 notit->state = STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN;
905
906 end:
907 return status;
908 }
909
910 static
911 enum bt_ctf_notif_iter_status read_stream_event_context_begin_state(
912 struct bt_ctf_notif_iter *notit)
913 {
914 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
915 struct bt_ctf_field_type *stream_event_context_type;
916
917 stream_event_context_type = bt_ctf_stream_class_get_event_context_type(
918 notit->meta.stream_class);
919 if (!stream_event_context_type) {
920 PERR("Failed to retrieve stream class's event context type\n");
921 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
922 goto end;
923 }
924
925 status = read_dscope_begin_state(notit, stream_event_context_type,
926 STATE_DSCOPE_EVENT_CONTEXT_BEGIN,
927 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE,
928 &notit->dscopes.stream_event_context);
929
930 end:
931 BT_PUT(stream_event_context_type);
932
933 return status;
934 }
935
936 static
937 enum bt_ctf_notif_iter_status read_stream_event_context_continue_state(
938 struct bt_ctf_notif_iter *notit)
939 {
940 return read_dscope_continue_state(notit,
941 STATE_DSCOPE_EVENT_CONTEXT_BEGIN);
942 }
943
944 static
945 enum bt_ctf_notif_iter_status read_event_context_begin_state(
946 struct bt_ctf_notif_iter *notit)
947 {
948 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
949 struct bt_ctf_field_type *event_context_type;
950
951 event_context_type = bt_ctf_event_class_get_context_type(
952 notit->meta.event_class);
953 if (!event_context_type) {
954 PERR("Failed to retrieve event class's context type\n");
955 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
956 goto end;
957 }
958
959 status = read_dscope_begin_state(notit, event_context_type,
960 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN,
961 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE,
962 &notit->dscopes.event_context);
963
964 end:
965 BT_PUT(event_context_type);
966
967 return status;
968 }
969
970 static
971 enum bt_ctf_notif_iter_status read_event_context_continue_state(
972 struct bt_ctf_notif_iter *notit)
973 {
974 return read_dscope_continue_state(notit,
975 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN);
976 }
977
978 static
979 enum bt_ctf_notif_iter_status read_event_payload_begin_state(
980 struct bt_ctf_notif_iter *notit)
981 {
982 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
983 struct bt_ctf_field_type *event_payload_type;
984
985 event_payload_type = bt_ctf_event_class_get_payload_type(
986 notit->meta.event_class);
987 if (!event_payload_type) {
988 PERR("Failed to retrieve event class's payload type\n");
989 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
990 goto end;
991 }
992
993 status = read_dscope_begin_state(notit, event_payload_type,
994 STATE_EMIT_NOTIF_EVENT,
995 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE,
996 &notit->dscopes.event_payload);
997
998 end:
999 BT_PUT(event_payload_type);
1000
1001 return status;
1002 }
1003
1004 static
1005 enum bt_ctf_notif_iter_status read_event_payload_continue_state(
1006 struct bt_ctf_notif_iter *notit)
1007 {
1008 return read_dscope_continue_state(notit, STATE_EMIT_NOTIF_EVENT);
1009 }
1010
1011 static
1012 enum bt_ctf_notif_iter_status skip_packet_padding_state(
1013 struct bt_ctf_notif_iter *notit)
1014 {
1015 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
1016 size_t bits_to_skip;
1017
1018 assert(notit->cur_packet_size > 0);
1019 bits_to_skip = notit->cur_packet_size - packet_at(notit);
1020 if (bits_to_skip == 0) {
1021 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1022 goto end;
1023 } else {
1024 size_t bits_to_consume;
1025 status = buf_ensure_available_bits(notit);
1026 if (status != BT_CTF_NOTIF_ITER_STATUS_OK) {
1027 goto end;
1028 }
1029
1030 bits_to_consume = MIN(buf_available_bits(notit), bits_to_skip);
1031 buf_consume_bits(notit, bits_to_consume);
1032 bits_to_skip = notit->cur_packet_size - packet_at(notit);
1033 if (bits_to_skip == 0) {
1034 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1035 goto end;
1036 }
1037 }
1038
1039 end:
1040 return status;
1041 }
1042
1043 static inline
1044 enum bt_ctf_notif_iter_status handle_state(struct bt_ctf_notif_iter *notit)
1045 {
1046 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
1047
1048 PDBG("Handling state %d\n", notit->state);
1049
1050 // TODO: optimalize!
1051 switch (notit->state) {
1052 case STATE_INIT:
1053 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1054 break;
1055 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
1056 status = read_packet_header_begin_state(notit);
1057 break;
1058 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
1059 status = read_packet_header_continue_state(notit);
1060 break;
1061 case STATE_AFTER_TRACE_PACKET_HEADER:
1062 status = after_packet_header_state(notit);
1063 break;
1064 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN:
1065 status = read_packet_context_begin_state(notit);
1066 break;
1067 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE:
1068 status = read_packet_context_continue_state(notit);
1069 break;
1070 case STATE_AFTER_STREAM_PACKET_CONTEXT:
1071 status = after_packet_context_state(notit);
1072 break;
1073 case STATE_EMIT_NOTIF_NEW_PACKET:
1074 notit->state = STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN;
1075 break;
1076 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN:
1077 status = read_event_header_begin_state(notit);
1078 break;
1079 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE:
1080 status = read_event_header_continue_state(notit);
1081 break;
1082 case STATE_AFTER_STREAM_EVENT_HEADER:
1083 status = after_event_header_state(notit);
1084 break;
1085 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN:
1086 status = read_stream_event_context_begin_state(notit);
1087 break;
1088 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE:
1089 status = read_stream_event_context_continue_state(notit);
1090 break;
1091 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN:
1092 status = read_event_context_begin_state(notit);
1093 break;
1094 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE:
1095 status = read_event_context_continue_state(notit);
1096 break;
1097 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN:
1098 status = read_event_payload_begin_state(notit);
1099 break;
1100 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE:
1101 status = read_event_payload_continue_state(notit);
1102 break;
1103 case STATE_EMIT_NOTIF_EVENT:
1104 notit->state = STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN;
1105 break;
1106 case STATE_SKIP_PACKET_PADDING:
1107 status = skip_packet_padding_state(notit);
1108 break;
1109 case STATE_EMIT_NOTIF_END_OF_PACKET:
1110 notit->state = STATE_SKIP_PACKET_PADDING;
1111 break;
1112 }
1113
1114 return status;
1115 }
1116
1117
1118 /**
1119 * Resets the internal state of a CTF notification iterator.
1120 *
1121 * This function can be used when it is desired to seek to the beginning
1122 * of another packet. It is expected that the next call to
1123 * bt_ctf_notif_iter_medium_ops::request_bytes() made by this
1124 * notification iterator will return the \em first bytes of a \em
1125 * packet.
1126 *
1127 * @param notif_iter CTF notification iterator
1128 */
1129 static
1130 void bt_ctf_notif_iter_reset(struct bt_ctf_notif_iter *notit)
1131 {
1132 assert(notit);
1133 stack_clear(notit->stack);
1134 BT_PUT(notit->meta.stream_class);
1135 BT_PUT(notit->meta.event_class);
1136 BT_PUT(notit->packet);
1137 put_all_dscopes(notit);
1138 notit->buf.addr = NULL;
1139 notit->buf.sz = 0;
1140 notit->buf.at = 0;
1141 notit->buf.packet_offset = 0;
1142 notit->state = STATE_INIT;
1143 notit->cur_content_size = -1;
1144 notit->cur_packet_size = -1;
1145 }
1146
1147 static
1148 int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter *notit)
1149 {
1150 int ret = 0;
1151
1152 assert(notit);
1153 stack_clear(notit->stack);
1154 BT_PUT(notit->meta.stream_class);
1155 BT_PUT(notit->meta.event_class);
1156 BT_PUT(notit->packet);
1157 put_all_dscopes(notit);
1158
1159 /*
1160 * Adjust current buffer so that addr points to the beginning of the new
1161 * packet.
1162 */
1163 if (notit->buf.addr) {
1164 size_t consumed_bytes = (size_t) (notit->buf.at / CHAR_BIT);
1165
1166 /* Packets are assumed to start on a byte frontier. */
1167 if (notit->buf.at % CHAR_BIT) {
1168 ret = -1;
1169 goto end;
1170 }
1171
1172 notit->buf.addr += consumed_bytes;
1173 notit->buf.sz -= consumed_bytes;
1174 notit->buf.at = 0;
1175 notit->buf.packet_offset = 0;
1176 }
1177
1178 notit->cur_content_size = -1;
1179 notit->cur_packet_size = -1;
1180 end:
1181 return ret;
1182 }
1183
1184 static
1185 struct bt_ctf_field *get_next_field(struct bt_ctf_notif_iter *notit)
1186 {
1187 struct bt_ctf_field *next_field = NULL;
1188 struct bt_ctf_field *base_field;
1189 struct bt_ctf_field_type *base_type;
1190 size_t index;
1191
1192 assert(!stack_empty(notit->stack));
1193 index = stack_top(notit->stack)->index;
1194 base_field = stack_top(notit->stack)->base;
1195 base_type = bt_ctf_field_get_type(base_field);
1196 if (!base_type) {
1197 PERR("Failed to get base field's type\n");
1198 goto end;
1199 }
1200
1201 switch (bt_ctf_field_type_get_type_id(base_type)) {
1202 case BT_CTF_TYPE_ID_STRUCT:
1203 next_field = bt_ctf_field_structure_get_field_by_index(
1204 base_field, index);
1205 break;
1206 case BT_CTF_TYPE_ID_ARRAY:
1207 next_field = bt_ctf_field_array_get_field(base_field, index);
1208 break;
1209 case BT_CTF_TYPE_ID_SEQUENCE:
1210 next_field = bt_ctf_field_sequence_get_field(base_field, index);
1211 break;
1212 case BT_CTF_TYPE_ID_VARIANT:
1213 next_field = bt_ctf_field_variant_get_current_field(base_field);
1214 break;
1215 default:
1216 assert(false);
1217 break;
1218 }
1219
1220 if (!next_field) {
1221 next_field = NULL;
1222 }
1223
1224 end:
1225 BT_PUT(base_type);
1226
1227 return next_field;
1228 }
1229
1230 static
1231 enum bt_ctf_btr_status btr_signed_int_cb(int64_t value,
1232 struct bt_ctf_field_type *type, void *data)
1233 {
1234 enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK;
1235 struct bt_ctf_field *field = NULL;
1236 struct bt_ctf_field *int_field = NULL;
1237 struct bt_ctf_notif_iter *notit = data;
1238 int ret;
1239
1240 /* create next field */
1241 field = get_next_field(notit);
1242 if (!field) {
1243 PERR("Failed to get next field (signed int)\n");
1244 status = BT_CTF_BTR_STATUS_ERROR;
1245 goto end;
1246 }
1247
1248 switch(bt_ctf_field_type_get_type_id(type)) {
1249 case BT_CTF_TYPE_ID_INTEGER:
1250 /* Integer field is created field */
1251 BT_MOVE(int_field, field);
1252 break;
1253 case BT_CTF_TYPE_ID_ENUM:
1254 int_field = bt_ctf_field_enumeration_get_container(field);
1255 break;
1256 default:
1257 break;
1258 }
1259
1260 if (!int_field) {
1261 PERR("Failed to get integer field\n");
1262 status = BT_CTF_BTR_STATUS_ERROR;
1263 goto end;
1264 }
1265
1266 ret = bt_ctf_field_signed_integer_set_value(int_field, value);
1267 assert(!ret);
1268 stack_top(notit->stack)->index++;
1269
1270 end:
1271 BT_PUT(field);
1272 BT_PUT(int_field);
1273
1274 return status;
1275 }
1276
1277 static
1278 enum bt_ctf_btr_status btr_unsigned_int_cb(uint64_t value,
1279 struct bt_ctf_field_type *type, void *data)
1280 {
1281 enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK;
1282 struct bt_ctf_field *field = NULL;
1283 struct bt_ctf_field *int_field = NULL;
1284 struct bt_ctf_notif_iter *notit = data;
1285 int ret;
1286
1287 /* Create next field */
1288 field = get_next_field(notit);
1289 if (!field) {
1290 PERR("Failed to get next field (unsigned int)\n");
1291 status = BT_CTF_BTR_STATUS_ERROR;
1292 goto end;
1293 }
1294
1295 switch(bt_ctf_field_type_get_type_id(type)) {
1296 case BT_CTF_TYPE_ID_INTEGER:
1297 /* Integer field is created field */
1298 BT_MOVE(int_field, field);
1299 break;
1300 case BT_CTF_TYPE_ID_ENUM:
1301 int_field = bt_ctf_field_enumeration_get_container(field);
1302 break;
1303 default:
1304 break;
1305 }
1306
1307 if (!int_field) {
1308 PERR("Failed to get integer field\n");
1309 status = BT_CTF_BTR_STATUS_ERROR;
1310 goto end;
1311 }
1312
1313 ret = bt_ctf_field_unsigned_integer_set_value(int_field, value);
1314 assert(!ret);
1315 stack_top(notit->stack)->index++;
1316
1317 end:
1318 BT_PUT(field);
1319 BT_PUT(int_field);
1320
1321 return status;
1322 }
1323
1324 static
1325 enum bt_ctf_btr_status btr_floating_point_cb(double value,
1326 struct bt_ctf_field_type *type, void *data)
1327 {
1328 enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK;
1329 struct bt_ctf_field *field = NULL;
1330 struct bt_ctf_notif_iter *notit = data;
1331 int ret;
1332
1333 /* Create next field */
1334 field = get_next_field(notit);
1335 if (!field) {
1336 PERR("Failed to get next field (floating point number)\n");
1337 status = BT_CTF_BTR_STATUS_ERROR;
1338 goto end;
1339 }
1340
1341 ret = bt_ctf_field_floating_point_set_value(field, value);
1342 assert(!ret);
1343 stack_top(notit->stack)->index++;
1344
1345 end:
1346 BT_PUT(field);
1347
1348 return status;
1349 }
1350
1351 static
1352 enum bt_ctf_btr_status btr_string_begin_cb(
1353 struct bt_ctf_field_type *type, void *data)
1354 {
1355 enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK;
1356 struct bt_ctf_field *field = NULL;
1357 struct bt_ctf_notif_iter *notit = data;
1358 int ret;
1359
1360 /* Create next field */
1361 field = get_next_field(notit);
1362 if (!field) {
1363 PERR("Failed to get next field (string)\n");
1364 status = BT_CTF_BTR_STATUS_ERROR;
1365 goto end;
1366 }
1367
1368 /*
1369 * Push on stack. Not a compound type per se, but we know that only
1370 * btr_string_cb() may be called between this call and a subsequent
1371 * call to btr_string_end_cb().
1372 */
1373 ret = stack_push(notit->stack, field);
1374 if (ret) {
1375 PERR("Failed to push string field onto the stack\n");
1376 status = BT_CTF_BTR_STATUS_ERROR;
1377 goto end;
1378 }
1379
1380 end:
1381 BT_PUT(field);
1382
1383 return status;
1384 }
1385
1386 static
1387 enum bt_ctf_btr_status btr_string_cb(const char *value,
1388 size_t len, struct bt_ctf_field_type *type, void *data)
1389 {
1390 enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK;
1391 struct bt_ctf_field *field = NULL;
1392 struct bt_ctf_notif_iter *notit = data;
1393 int ret;
1394
1395 /* Get string field */
1396 field = stack_top(notit->stack)->base;
1397 assert(field);
1398
1399 /* Append current string */
1400 ret = bt_ctf_field_string_append_len(field, value, len);
1401 if (ret) {
1402 PERR("Failed to append a string to a string field\n");
1403 status = BT_CTF_BTR_STATUS_ERROR;
1404 goto end;
1405 }
1406
1407 end:
1408 return status;
1409 }
1410
1411 static
1412 enum bt_ctf_btr_status btr_string_end_cb(
1413 struct bt_ctf_field_type *type, void *data)
1414 {
1415 struct bt_ctf_notif_iter *notit = data;
1416
1417 /* Pop string field */
1418 stack_pop(notit->stack);
1419
1420 /* Go to next field */
1421 stack_top(notit->stack)->index++;
1422
1423 return BT_CTF_BTR_STATUS_OK;
1424 }
1425
1426 enum bt_ctf_btr_status btr_compound_begin_cb(
1427 struct bt_ctf_field_type *type, void *data)
1428 {
1429 enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK;
1430 struct bt_ctf_notif_iter *notit = data;
1431 struct bt_ctf_field *field;
1432 int ret;
1433
1434 /* Create field */
1435 if (stack_empty(notit->stack)) {
1436 /* Root: create dynamic scope field */
1437 *notit->cur_dscope_field = bt_ctf_field_create(type);
1438 field = *notit->cur_dscope_field;
1439
1440 /*
1441 * Field will be put at the end of this function
1442 * (stack_push() will take one reference, but this
1443 * reference is lost upon the equivalent stack_pop()
1444 * later), so also get it for our context to own it.
1445 */
1446 bt_get(*notit->cur_dscope_field);
1447 } else {
1448 field = get_next_field(notit);
1449 }
1450
1451 if (!field) {
1452 PERR("Failed to get next field or create dynamic scope field\n");
1453 status = BT_CTF_BTR_STATUS_ERROR;
1454 goto end;
1455 }
1456
1457 /* Push field */
1458 ret = stack_push(notit->stack, field);
1459 if (ret) {
1460 PERR("Failed to push compound field onto the stack\n");
1461 status = BT_CTF_BTR_STATUS_ERROR;
1462 goto end;
1463 }
1464
1465 end:
1466 BT_PUT(field);
1467
1468 return status;
1469 }
1470
1471 enum bt_ctf_btr_status btr_compound_end_cb(
1472 struct bt_ctf_field_type *type, void *data)
1473 {
1474 struct bt_ctf_notif_iter *notit = data;
1475
1476 assert(!stack_empty(notit->stack));
1477
1478 /* Pop stack */
1479 stack_pop(notit->stack);
1480
1481 /* If the stack is not empty, increment the base's index */
1482 if (!stack_empty(notit->stack)) {
1483 stack_top(notit->stack)->index++;
1484 }
1485
1486 return BT_CTF_BTR_STATUS_OK;
1487 }
1488
1489 static
1490 struct bt_ctf_field *resolve_field(struct bt_ctf_notif_iter *notit,
1491 struct bt_ctf_field_path *path)
1492 {
1493 struct bt_ctf_field *field = NULL;
1494 unsigned int i;
1495
1496 switch (bt_ctf_field_path_get_root_scope(path)) {
1497 case BT_CTF_SCOPE_TRACE_PACKET_HEADER:
1498 field = notit->dscopes.trace_packet_header;
1499 break;
1500 case BT_CTF_SCOPE_STREAM_PACKET_CONTEXT:
1501 field = notit->dscopes.stream_packet_context;
1502 break;
1503 case BT_CTF_SCOPE_STREAM_EVENT_HEADER:
1504 field = notit->dscopes.stream_event_header;
1505 break;
1506 case BT_CTF_SCOPE_STREAM_EVENT_CONTEXT:
1507 field = notit->dscopes.stream_event_context;
1508 break;
1509 case BT_CTF_SCOPE_EVENT_CONTEXT:
1510 field = notit->dscopes.event_context;
1511 break;
1512 case BT_CTF_SCOPE_EVENT_FIELDS:
1513 field = notit->dscopes.event_payload;
1514 break;
1515 default:
1516 break;
1517 }
1518
1519 if (!field) {
1520 goto end;
1521 }
1522
1523 bt_get(field);
1524
1525 for (i = 0; i < bt_ctf_field_path_get_index_count(path); ++i) {
1526 struct bt_ctf_field *next_field = NULL;
1527 struct bt_ctf_field_type *field_type;
1528 int index = bt_ctf_field_path_get_index(path, i);
1529
1530 field_type = bt_ctf_field_get_type(field);
1531 if (!field_type) {
1532 BT_PUT(field);
1533 goto end;
1534 }
1535
1536 if (is_struct_type(field_type)) {
1537 next_field = bt_ctf_field_structure_get_field_by_index(
1538 field, index);
1539 } else if (is_variant_type(field_type)) {
1540 next_field =
1541 bt_ctf_field_variant_get_current_field(field);
1542 }
1543
1544 BT_PUT(field);
1545 BT_PUT(field_type);
1546
1547 if (!next_field) {
1548 goto end;
1549 }
1550
1551 /* Move next field -> field */
1552 BT_MOVE(field, next_field);
1553 }
1554
1555 end:
1556 return field;
1557 }
1558
1559 static
1560 int64_t btr_get_sequence_length_cb(struct bt_ctf_field_type *type, void *data)
1561 {
1562 int64_t ret = -1;
1563 int iret;
1564 struct bt_ctf_field_path *field_path;
1565 struct bt_ctf_notif_iter *notit = data;
1566 struct bt_ctf_field *length_field = NULL;
1567 uint64_t length;
1568
1569 field_path = bt_ctf_field_type_sequence_get_length_field_path(type);
1570 if (!field_path) {
1571 goto end;
1572 }
1573
1574 length_field = resolve_field(notit, field_path);
1575 if (!length_field) {
1576 goto end;
1577 }
1578
1579 iret = bt_ctf_field_unsigned_integer_get_value(length_field, &length);
1580 if (iret) {
1581 goto end;
1582 }
1583
1584 iret = bt_ctf_field_sequence_set_length(stack_top(notit->stack)->base,
1585 length_field);
1586 if (iret) {
1587 goto end;
1588 }
1589 ret = (int64_t) length;
1590
1591 end:
1592 BT_PUT(length_field);
1593 BT_PUT(field_path);
1594
1595 return ret;
1596 }
1597
1598 static
1599 struct bt_ctf_field_type *btr_get_variant_type_cb(
1600 struct bt_ctf_field_type *type, void *data)
1601 {
1602 struct bt_ctf_field_path *path;
1603 struct bt_ctf_notif_iter *notit = data;
1604 struct bt_ctf_field *tag_field = NULL;
1605 struct bt_ctf_field *selected_field = NULL;
1606 struct bt_ctf_field_type *selected_field_type = NULL;
1607
1608 path = bt_ctf_field_type_variant_get_tag_field_path(type);
1609 if (!path) {
1610 goto end;
1611 }
1612
1613 tag_field = resolve_field(notit, path);
1614 if (!tag_field) {
1615 goto end;
1616 }
1617
1618 /*
1619 * We found the enumeration tag field instance which should be
1620 * able to select a current field for this variant. This
1621 * callback function we're in is called _after_
1622 * compound_begin(), so the current stack top's base field is
1623 * the variant field in question. We get the selected field here
1624 * thanks to this tag field (thus creating the selected field),
1625 * which will also provide us with its type. Then, this field
1626 * will remain the current selected one until the next callback
1627 * function call which is used to fill the current selected
1628 * field.
1629 */
1630 selected_field = bt_ctf_field_variant_get_field(
1631 stack_top(notit->stack)->base, tag_field);
1632 if (!selected_field) {
1633 goto end;
1634 }
1635
1636 selected_field_type = bt_ctf_field_get_type(selected_field);
1637
1638 end:
1639 BT_PUT(tag_field);
1640 BT_PUT(selected_field);
1641
1642 return selected_field_type;
1643 }
1644
1645 static
1646 struct bt_ctf_event *create_event(struct bt_ctf_notif_iter *notit)
1647 {
1648 int ret;
1649 struct bt_ctf_event *event;
1650
1651 /* Create event object. */
1652 event = bt_ctf_event_create(notit->meta.event_class);
1653 if (!event) {
1654 goto error;
1655 }
1656
1657 /* Set header, stream event context, context, and payload fields. */
1658 ret = bt_ctf_event_set_header(event,
1659 notit->dscopes.stream_event_header);
1660 if (ret) {
1661 goto error;
1662 }
1663
1664 ret = bt_ctf_event_set_stream_event_context(event,
1665 notit->dscopes.stream_event_context);
1666 if (ret) {
1667 goto error;
1668 }
1669
1670 ret = bt_ctf_event_set_event_context(event,
1671 notit->dscopes.event_context);
1672 if (ret) {
1673 goto error;
1674 }
1675
1676 ret = bt_ctf_event_set_payload_field(event,
1677 notit->dscopes.event_payload);
1678 if (ret) {
1679 goto error;
1680 }
1681
1682 /* Associate with current packet. */
1683 assert(notit->packet);
1684 ret = bt_ctf_event_set_packet(event, notit->packet);
1685 if (ret) {
1686 goto error;
1687 }
1688
1689 goto end;
1690 error:
1691 BT_PUT(event);
1692 end:
1693 return event;
1694 }
1695
1696 static
1697 void create_packet(struct bt_ctf_notif_iter *notit)
1698 {
1699 int ret;
1700 struct bt_ctf_stream *stream = NULL;
1701 struct bt_ctf_packet *packet = NULL;
1702
1703 /* Ask the user for the stream */
1704 stream = notit->medium.medops.get_stream(notit->meta.stream_class,
1705 notit->medium.data);
1706 if (!stream) {
1707 goto error;
1708 }
1709
1710 /* Create packet */
1711 packet = bt_ctf_packet_create(stream);
1712 if (!packet) {
1713 goto error;
1714 }
1715
1716 /* Set packet's context and header fields */
1717 if (notit->dscopes.trace_packet_header) {
1718 ret = bt_ctf_packet_set_header(packet,
1719 notit->dscopes.trace_packet_header);
1720 if (ret) {
1721 goto error;
1722 }
1723 }
1724
1725 if (notit->dscopes.stream_packet_context) {
1726 ret = bt_ctf_packet_set_context(packet,
1727 notit->dscopes.stream_packet_context);
1728 if (ret) {
1729 goto error;
1730 }
1731 }
1732
1733 goto end;
1734 error:
1735 BT_PUT(packet);
1736 end:
1737 BT_MOVE(notit->packet, packet);
1738 }
1739
1740 static
1741 void notify_new_packet(struct bt_ctf_notif_iter *notit,
1742 struct bt_notification **notification)
1743 {
1744 struct bt_notification *ret;
1745
1746 /* Initialize the iterator's current packet */
1747 create_packet(notit);
1748 if (!notit->packet) {
1749 return;
1750 }
1751
1752 ret = bt_notification_packet_start_create(notit->packet);
1753 if (!ret) {
1754 return;
1755 }
1756 *notification = ret;
1757 }
1758
1759 static
1760 void notify_end_of_packet(struct bt_ctf_notif_iter *notit,
1761 struct bt_notification **notification)
1762 {
1763 struct bt_notification *ret;
1764
1765 if (!notit->packet) {
1766 return;
1767 }
1768
1769 ret = bt_notification_packet_end_create(notit->packet);
1770 if (!ret) {
1771 return;
1772 }
1773 BT_PUT(notit->packet);
1774 *notification = ret;
1775 }
1776
1777 static
1778 void notify_event(struct bt_ctf_notif_iter *notit,
1779 struct bt_notification **notification)
1780 {
1781 struct bt_ctf_event *event;
1782 struct bt_notification *ret = NULL;
1783
1784 /* Create event */
1785 event = create_event(notit);
1786 if (!event) {
1787 goto end;
1788 }
1789
1790 ret = bt_notification_event_create(event);
1791 if (!ret) {
1792 goto end;
1793 }
1794 *notification = ret;
1795 end:
1796 BT_PUT(event);
1797 }
1798
1799 static
1800 void notify_eos(struct bt_ctf_notif_iter *notit,
1801 struct bt_notification **notification)
1802 {
1803 struct bt_ctf_event *event;
1804 struct bt_notification *ret = NULL;
1805
1806 /* Create event */
1807 event = create_event(notit);
1808 if (!event) {
1809 goto end;
1810 }
1811
1812 ret = bt_notification_stream_end_create(event);
1813 if (!ret) {
1814 goto end;
1815 }
1816 *notification = ret;
1817 end:
1818 BT_PUT(event);
1819 }
1820
1821 struct bt_ctf_notif_iter *bt_ctf_notif_iter_create(struct bt_ctf_trace *trace,
1822 size_t max_request_sz,
1823 struct bt_ctf_notif_iter_medium_ops medops,
1824 void *data, FILE *err_stream)
1825 {
1826 struct bt_ctf_notif_iter *notit = NULL;
1827 struct bt_ctf_btr_cbs cbs = {
1828 .types = {
1829 .signed_int = btr_signed_int_cb,
1830 .unsigned_int = btr_unsigned_int_cb,
1831 .floating_point = btr_floating_point_cb,
1832 .string_begin = btr_string_begin_cb,
1833 .string = btr_string_cb,
1834 .string_end = btr_string_end_cb,
1835 .compound_begin = btr_compound_begin_cb,
1836 .compound_end = btr_compound_end_cb,
1837 },
1838 .query = {
1839 .get_sequence_length = btr_get_sequence_length_cb,
1840 .get_variant_type = btr_get_variant_type_cb,
1841 },
1842 };
1843
1844 assert(trace);
1845 assert(medops.request_bytes);
1846 notit = g_new0(struct bt_ctf_notif_iter, 1);
1847 if (!notit) {
1848 PERR("Failed to allocate memory for CTF notification iterator\n");
1849 goto end;
1850 }
1851
1852 notit->meta.trace = trace;
1853 bt_get(notit->meta.trace);
1854 notit->medium.medops = medops;
1855 notit->medium.max_request_sz = max_request_sz;
1856 notit->medium.data = data;
1857 notit->err_stream = err_stream;
1858 notit->stack = stack_new(notit);
1859 if (!notit->stack) {
1860 PERR("Failed to create stack\n");
1861 bt_ctf_notif_iter_destroy(notit);
1862 notit = NULL;
1863 goto end;
1864 }
1865
1866 notit->btr = bt_ctf_btr_create(cbs, notit, err_stream);
1867 if (!notit->btr) {
1868 PERR("Failed to create binary type reader\n");
1869 bt_ctf_notif_iter_destroy(notit);
1870 notit = NULL;
1871 goto end;
1872 }
1873
1874 bt_ctf_notif_iter_reset(notit);
1875
1876 end:
1877 return notit;
1878 }
1879
1880 void bt_ctf_notif_iter_destroy(struct bt_ctf_notif_iter *notit)
1881 {
1882 BT_PUT(notit->meta.trace);
1883 BT_PUT(notit->meta.stream_class);
1884 BT_PUT(notit->meta.event_class);
1885 BT_PUT(notit->packet);
1886 put_all_dscopes(notit);
1887
1888 if (notit->stack) {
1889 stack_destroy(notit->stack);
1890 }
1891
1892 if (notit->btr) {
1893 bt_ctf_btr_destroy(notit->btr);
1894 }
1895
1896 g_free(notit);
1897 }
1898
1899 enum bt_ctf_notif_iter_status bt_ctf_notif_iter_get_next_notification(
1900 struct bt_ctf_notif_iter *notit,
1901 struct bt_notification **notification)
1902 {
1903 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
1904
1905 assert(notit);
1906 assert(notification);
1907
1908 while (true) {
1909 status = handle_state(notit);
1910 if (status != BT_CTF_NOTIF_ITER_STATUS_OK) {
1911 if (status == BT_CTF_NOTIF_ITER_STATUS_EOF) {
1912 PDBG("Medium operation reported end of stream\n");
1913 } else {
1914 PERR("Failed to handle state:\n");
1915 PERR("\tState: %d\n", notit->state);
1916 }
1917 goto end;
1918 }
1919
1920 switch (notit->state) {
1921 case STATE_EMIT_NOTIF_NEW_PACKET:
1922 PDBG("Emitting new packet notification\n");
1923 notify_new_packet(notit, notification);
1924 if (!*notification) {
1925 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
1926 }
1927 goto end;
1928 case STATE_EMIT_NOTIF_EVENT:
1929 PDBG("Emitting event notification\n");
1930 notify_event(notit, notification);
1931 if (!*notification) {
1932 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
1933 }
1934 goto end;
1935 case STATE_EMIT_NOTIF_END_OF_PACKET:
1936 PDBG("Emitting end of packet notification\n");
1937 notify_end_of_packet(notit, notification);
1938 if (!*notification) {
1939 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
1940 }
1941 goto end;
1942 default:
1943 /* Non-emitting state: continue */
1944 break;
1945 }
1946 }
1947
1948 end:
1949 return status;
1950 }
This page took 0.067565 seconds and 5 git commands to generate.