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