Handle empty strings in the notif-iter btr_string callbacks
[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 notit->meta.stream_class = bt_ctf_trace_get_stream_class_by_id(
582 notit->meta.trace, stream_id);
583 if (!notit->meta.stream_class) {
584 PERR("Cannot find stream class with ID %" PRIu64 "\n",
585 stream_id);
586 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
587 goto end;
588 }
589
590 end:
591 BT_PUT(packet_header_type);
592 BT_PUT(stream_id_field_type);
593
594 return status;
595 }
596
597 static
598 enum bt_ctf_notif_iter_status after_packet_header_state(
599 struct bt_ctf_notif_iter *notit)
600 {
601 enum bt_ctf_notif_iter_status status;
602
603 status = set_current_stream_class(notit);
604 if (status == BT_CTF_NOTIF_ITER_STATUS_OK) {
605 notit->state = STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN;
606 }
607
608 return status;
609 }
610
611 static
612 enum bt_ctf_notif_iter_status read_packet_context_begin_state(
613 struct bt_ctf_notif_iter *notit)
614 {
615 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
616 struct bt_ctf_field_type *packet_context_type;
617
618 assert(notit->meta.stream_class);
619 packet_context_type = bt_ctf_stream_class_get_packet_context_type(
620 notit->meta.stream_class);
621 if (!packet_context_type) {
622 PERR("Failed to retrieve stream class's packet context\n");
623 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
624 goto end;
625 }
626
627 status = read_dscope_begin_state(notit, packet_context_type,
628 STATE_AFTER_STREAM_PACKET_CONTEXT,
629 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE,
630 &notit->dscopes.stream_packet_context);
631
632 end:
633 BT_PUT(packet_context_type);
634 return status;
635 }
636
637 static
638 enum bt_ctf_notif_iter_status read_packet_context_continue_state(
639 struct bt_ctf_notif_iter *notit)
640 {
641 return read_dscope_continue_state(notit,
642 STATE_AFTER_STREAM_PACKET_CONTEXT);
643 }
644
645 static
646 enum bt_ctf_notif_iter_status set_current_packet_content_sizes(
647 struct bt_ctf_notif_iter *notit)
648 {
649 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
650 struct bt_ctf_field *packet_size_field = NULL;
651 struct bt_ctf_field *content_size_field = NULL;
652 uint64_t content_size = -1, packet_size = -1;
653
654 assert(notit->dscopes.stream_packet_context);
655
656 packet_size_field = bt_ctf_field_structure_get_field(
657 notit->dscopes.stream_packet_context, "packet_size");
658 content_size_field = bt_ctf_field_structure_get_field(
659 notit->dscopes.stream_packet_context, "content_size");
660 if (packet_size_field) {
661 int ret = bt_ctf_field_unsigned_integer_get_value(
662 packet_size_field, &packet_size);
663
664 assert(!ret);
665 if (packet_size == 0) {
666 PERR("Decoded packet size is 0\n");
667 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
668 goto end;
669 } else if ((packet_size % 8) != 0) {
670 PERR("Decoded packet size is not a multiple of 8\n");
671 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
672 goto end;
673 }
674 }
675
676 if (content_size_field) {
677 int ret = bt_ctf_field_unsigned_integer_get_value(
678 content_size_field, &content_size);
679
680 assert(!ret);
681 } else {
682 content_size = packet_size;
683 }
684
685 notit->cur_packet_size = packet_size;
686 notit->cur_content_size = content_size;
687 end:
688 BT_PUT(packet_size_field);
689 BT_PUT(content_size_field);
690 return status;
691 }
692
693 static
694 enum bt_ctf_notif_iter_status after_packet_context_state(
695 struct bt_ctf_notif_iter *notit)
696 {
697 enum bt_ctf_notif_iter_status status;
698
699 status = set_current_packet_content_sizes(notit);
700 if (status == BT_CTF_NOTIF_ITER_STATUS_OK) {
701 notit->state = STATE_EMIT_NOTIF_NEW_PACKET;
702 }
703
704 return status;
705 }
706
707 static
708 enum bt_ctf_notif_iter_status read_event_header_begin_state(
709 struct bt_ctf_notif_iter *notit)
710 {
711 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
712 struct bt_ctf_field_type *event_header_type = NULL;
713
714 /* Check if we have some content left */
715 if (notit->cur_content_size >= 0) {
716 if (packet_at(notit) == notit->cur_content_size) {
717 /* No more events! */
718 notit->state = STATE_EMIT_NOTIF_END_OF_PACKET;
719 goto end;
720 } else if (packet_at(notit) > notit->cur_content_size) {
721 /* That's not supposed to happen */
722 PERR("Cursor passed packet's content size:\n");
723 PERR("\tDecoded content size: %zu\n",
724 notit->cur_content_size);
725 PERR("\tCursor position: %zu\n", packet_at(notit));
726 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
727 goto end;
728 }
729 }
730
731 event_header_type = bt_ctf_stream_class_get_event_header_type(
732 notit->meta.stream_class);
733 if (!event_header_type) {
734 PERR("Failed to retrieve stream class's event header type\n");
735 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
736 goto end;
737 }
738
739 put_event_dscopes(notit);
740 status = read_dscope_begin_state(notit, event_header_type,
741 STATE_AFTER_STREAM_EVENT_HEADER,
742 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE,
743 &notit->dscopes.stream_event_header);
744 end:
745 BT_PUT(event_header_type);
746
747 return status;
748 }
749
750 static
751 enum bt_ctf_notif_iter_status read_event_header_continue_state(
752 struct bt_ctf_notif_iter *notit)
753 {
754 return read_dscope_continue_state(notit,
755 STATE_AFTER_STREAM_EVENT_HEADER);
756 }
757
758 static inline
759 enum bt_ctf_notif_iter_status set_current_event_class(struct bt_ctf_notif_iter *notit)
760 {
761 /*
762 * The assert() calls in this function are okay because it is
763 * assumed here that all the metadata objects have been
764 * validated for CTF correctness before decoding actual streams.
765 */
766
767 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
768 struct bt_ctf_field_type *event_header_type;
769 struct bt_ctf_field_type *id_field_type = NULL;
770 struct bt_ctf_field_type *v_field_type = NULL;
771 uint64_t event_id = -1ULL;
772 int ret;
773
774 event_header_type = bt_ctf_stream_class_get_event_header_type(
775 notit->meta.stream_class);
776 if (!event_header_type) {
777 PERR("Failed to retrieve stream class's event header type\n");
778 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
779 goto end;
780 }
781
782 /* Is there any "id"/"v" field in the event header? */
783 assert(is_struct_type(event_header_type));
784 id_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
785 event_header_type, "id");
786 v_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
787 event_header_type, "v");
788 assert(notit->dscopes.stream_event_header);
789 if (v_field_type) {
790 /*
791 * _ _____ _____
792 * | | |_ _|_ _| __ __ _
793 * | | | | | || '_ \ / _` |
794 * | |___| | | || | | | (_| | S P E C I A L
795 * |_____|_| |_||_| |_|\__, | C A S E â„¢
796 * |___/
797 */
798 struct bt_ctf_field *v_field = NULL;
799 struct bt_ctf_field *v_struct_field = NULL;
800 struct bt_ctf_field *v_struct_id_field = NULL;
801
802 // TODO: optimalize!
803 v_field = bt_ctf_field_structure_get_field(
804 notit->dscopes.stream_event_header, "v");
805 assert(v_field);
806
807 v_struct_field =
808 bt_ctf_field_variant_get_current_field(v_field);
809 if (!v_struct_field) {
810 goto end_v_field_type;
811 }
812
813 // TODO: optimalize!
814 v_struct_id_field =
815 bt_ctf_field_structure_get_field(v_struct_field, "id");
816 if (!v_struct_id_field) {
817 goto end_v_field_type;
818 }
819
820 ret = bt_ctf_field_unsigned_integer_get_value(
821 v_struct_id_field, &event_id);
822 if (ret) {
823 event_id = -1ULL;
824 }
825
826 end_v_field_type:
827 BT_PUT(v_field);
828 BT_PUT(v_struct_field);
829 BT_PUT(v_struct_id_field);
830 }
831
832 if (id_field_type && event_id == -1ULL) {
833 /* Check "id" field */
834 struct bt_ctf_field *id_field = NULL;
835 int ret;
836
837 // TODO: optimalize!
838 id_field = bt_ctf_field_structure_get_field(
839 notit->dscopes.stream_event_header, "id");
840 assert(id_field);
841 assert(bt_ctf_field_is_integer(id_field) ||
842 bt_ctf_field_is_enumeration(id_field));
843
844 if (bt_ctf_field_is_integer(id_field)) {
845 ret = bt_ctf_field_unsigned_integer_get_value(
846 id_field, &event_id);
847 } else {
848 struct bt_ctf_field *container;
849
850 container = bt_ctf_field_enumeration_get_container(
851 id_field);
852 assert(container);
853 ret = bt_ctf_field_unsigned_integer_get_value(
854 container, &event_id);
855 BT_PUT(container);
856 }
857 assert(!ret);
858 BT_PUT(id_field);
859 }
860
861 if (event_id == -1ULL) {
862 /* Event ID not found: single event? */
863 assert(bt_ctf_stream_class_get_event_class_count(
864 notit->meta.stream_class) == 1);
865 event_id = 0;
866 }
867
868 BT_PUT(notit->meta.event_class);
869 notit->meta.event_class = bt_ctf_stream_class_get_event_class_by_id(
870 notit->meta.stream_class, event_id);
871 if (!notit->meta.event_class) {
872 PERR("Cannot find event class with ID %" PRIu64 "\n", event_id);
873 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
874 goto end;
875 }
876
877 end:
878 BT_PUT(event_header_type);
879 BT_PUT(id_field_type);
880 BT_PUT(v_field_type);
881
882 return status;
883 }
884
885 static
886 enum bt_ctf_notif_iter_status after_event_header_state(
887 struct bt_ctf_notif_iter *notit)
888 {
889 enum bt_ctf_notif_iter_status status;
890
891 status = set_current_event_class(notit);
892 if (status != BT_CTF_NOTIF_ITER_STATUS_OK) {
893 PERR("Failed to set current event class\n");
894 goto end;
895 }
896
897 notit->state = STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN;
898
899 end:
900 return status;
901 }
902
903 static
904 enum bt_ctf_notif_iter_status read_stream_event_context_begin_state(
905 struct bt_ctf_notif_iter *notit)
906 {
907 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
908 struct bt_ctf_field_type *stream_event_context_type;
909
910 stream_event_context_type = bt_ctf_stream_class_get_event_context_type(
911 notit->meta.stream_class);
912 if (!stream_event_context_type) {
913 PERR("Failed to retrieve stream class's event context type\n");
914 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
915 goto end;
916 }
917
918 status = read_dscope_begin_state(notit, stream_event_context_type,
919 STATE_DSCOPE_EVENT_CONTEXT_BEGIN,
920 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE,
921 &notit->dscopes.stream_event_context);
922
923 end:
924 BT_PUT(stream_event_context_type);
925
926 return status;
927 }
928
929 static
930 enum bt_ctf_notif_iter_status read_stream_event_context_continue_state(
931 struct bt_ctf_notif_iter *notit)
932 {
933 return read_dscope_continue_state(notit,
934 STATE_DSCOPE_EVENT_CONTEXT_BEGIN);
935 }
936
937 static
938 enum bt_ctf_notif_iter_status read_event_context_begin_state(
939 struct bt_ctf_notif_iter *notit)
940 {
941 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
942 struct bt_ctf_field_type *event_context_type;
943
944 event_context_type = bt_ctf_event_class_get_context_type(
945 notit->meta.event_class);
946 if (!event_context_type) {
947 PERR("Failed to retrieve event class's context type\n");
948 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
949 goto end;
950 }
951
952 status = read_dscope_begin_state(notit, event_context_type,
953 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN,
954 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE,
955 &notit->dscopes.event_context);
956
957 end:
958 BT_PUT(event_context_type);
959
960 return status;
961 }
962
963 static
964 enum bt_ctf_notif_iter_status read_event_context_continue_state(
965 struct bt_ctf_notif_iter *notit)
966 {
967 return read_dscope_continue_state(notit,
968 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN);
969 }
970
971 static
972 enum bt_ctf_notif_iter_status read_event_payload_begin_state(
973 struct bt_ctf_notif_iter *notit)
974 {
975 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
976 struct bt_ctf_field_type *event_payload_type;
977
978 event_payload_type = bt_ctf_event_class_get_payload_type(
979 notit->meta.event_class);
980 if (!event_payload_type) {
981 PERR("Failed to retrieve event class's payload type\n");
982 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
983 goto end;
984 }
985
986 status = read_dscope_begin_state(notit, event_payload_type,
987 STATE_EMIT_NOTIF_EVENT,
988 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE,
989 &notit->dscopes.event_payload);
990
991 end:
992 BT_PUT(event_payload_type);
993
994 return status;
995 }
996
997 static
998 enum bt_ctf_notif_iter_status read_event_payload_continue_state(
999 struct bt_ctf_notif_iter *notit)
1000 {
1001 return read_dscope_continue_state(notit, STATE_EMIT_NOTIF_EVENT);
1002 }
1003
1004 static
1005 enum bt_ctf_notif_iter_status skip_packet_padding_state(
1006 struct bt_ctf_notif_iter *notit)
1007 {
1008 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
1009 size_t bits_to_skip;
1010
1011 assert(notit->cur_packet_size > 0);
1012 bits_to_skip = notit->cur_packet_size - packet_at(notit);
1013 if (bits_to_skip == 0) {
1014 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1015 goto end;
1016 } else {
1017 size_t bits_to_consume;
1018 status = buf_ensure_available_bits(notit);
1019 if (status != BT_CTF_NOTIF_ITER_STATUS_OK) {
1020 goto end;
1021 }
1022
1023 bits_to_consume = MIN(buf_available_bits(notit), bits_to_skip);
1024 buf_consume_bits(notit, bits_to_consume);
1025 bits_to_skip = notit->cur_packet_size - packet_at(notit);
1026 if (bits_to_skip == 0) {
1027 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1028 goto end;
1029 }
1030 }
1031
1032 end:
1033 return status;
1034 }
1035
1036 static inline
1037 enum bt_ctf_notif_iter_status handle_state(struct bt_ctf_notif_iter *notit)
1038 {
1039 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
1040
1041 PDBG("Handling state %d\n", notit->state);
1042
1043 // TODO: optimalize!
1044 switch (notit->state) {
1045 case STATE_INIT:
1046 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1047 break;
1048 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
1049 status = read_packet_header_begin_state(notit);
1050 break;
1051 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
1052 status = read_packet_header_continue_state(notit);
1053 break;
1054 case STATE_AFTER_TRACE_PACKET_HEADER:
1055 status = after_packet_header_state(notit);
1056 break;
1057 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN:
1058 status = read_packet_context_begin_state(notit);
1059 break;
1060 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE:
1061 status = read_packet_context_continue_state(notit);
1062 break;
1063 case STATE_AFTER_STREAM_PACKET_CONTEXT:
1064 status = after_packet_context_state(notit);
1065 break;
1066 case STATE_EMIT_NOTIF_NEW_PACKET:
1067 notit->state = STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN;
1068 break;
1069 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN:
1070 status = read_event_header_begin_state(notit);
1071 break;
1072 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE:
1073 status = read_event_header_continue_state(notit);
1074 break;
1075 case STATE_AFTER_STREAM_EVENT_HEADER:
1076 status = after_event_header_state(notit);
1077 break;
1078 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN:
1079 status = read_stream_event_context_begin_state(notit);
1080 break;
1081 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE:
1082 status = read_stream_event_context_continue_state(notit);
1083 break;
1084 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN:
1085 status = read_event_context_begin_state(notit);
1086 break;
1087 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE:
1088 status = read_event_context_continue_state(notit);
1089 break;
1090 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN:
1091 status = read_event_payload_begin_state(notit);
1092 break;
1093 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE:
1094 status = read_event_payload_continue_state(notit);
1095 break;
1096 case STATE_EMIT_NOTIF_EVENT:
1097 notit->state = STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN;
1098 break;
1099 case STATE_SKIP_PACKET_PADDING:
1100 status = skip_packet_padding_state(notit);
1101 break;
1102 case STATE_EMIT_NOTIF_END_OF_PACKET:
1103 notit->state = STATE_SKIP_PACKET_PADDING;
1104 break;
1105 }
1106
1107 return status;
1108 }
1109
1110
1111 /**
1112 * Resets the internal state of a CTF notification iterator.
1113 *
1114 * This function can be used when it is desired to seek to the beginning
1115 * of another packet. It is expected that the next call to
1116 * bt_ctf_notif_iter_medium_ops::request_bytes() made by this
1117 * notification iterator will return the \em first bytes of a \em
1118 * packet.
1119 *
1120 * @param notif_iter CTF notification iterator
1121 */
1122 static
1123 void bt_ctf_notif_iter_reset(struct bt_ctf_notif_iter *notit)
1124 {
1125 assert(notit);
1126 stack_clear(notit->stack);
1127 BT_PUT(notit->meta.stream_class);
1128 BT_PUT(notit->meta.event_class);
1129 BT_PUT(notit->packet);
1130 put_all_dscopes(notit);
1131 notit->buf.addr = NULL;
1132 notit->buf.sz = 0;
1133 notit->buf.at = 0;
1134 notit->buf.packet_offset = 0;
1135 notit->state = STATE_INIT;
1136 notit->cur_content_size = -1;
1137 notit->cur_packet_size = -1;
1138 }
1139
1140 static
1141 int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter *notit)
1142 {
1143 int ret = 0;
1144
1145 assert(notit);
1146 stack_clear(notit->stack);
1147 BT_PUT(notit->meta.stream_class);
1148 BT_PUT(notit->meta.event_class);
1149 BT_PUT(notit->packet);
1150 put_all_dscopes(notit);
1151
1152 /*
1153 * Adjust current buffer so that addr points to the beginning of the new
1154 * packet.
1155 */
1156 if (notit->buf.addr) {
1157 size_t consumed_bytes = (size_t) (notit->buf.at / CHAR_BIT);
1158
1159 /* Packets are assumed to start on a byte frontier. */
1160 if (notit->buf.at % CHAR_BIT) {
1161 ret = -1;
1162 goto end;
1163 }
1164
1165 notit->buf.addr += consumed_bytes;
1166 notit->buf.sz -= consumed_bytes;
1167 notit->buf.at = 0;
1168 notit->buf.packet_offset = 0;
1169 }
1170
1171 notit->cur_content_size = -1;
1172 notit->cur_packet_size = -1;
1173 end:
1174 return ret;
1175 }
1176
1177 static
1178 struct bt_ctf_field *get_next_field(struct bt_ctf_notif_iter *notit)
1179 {
1180 struct bt_ctf_field *next_field = NULL;
1181 struct bt_ctf_field *base_field;
1182 struct bt_ctf_field_type *base_type;
1183 size_t index;
1184
1185 assert(!stack_empty(notit->stack));
1186 index = stack_top(notit->stack)->index;
1187 base_field = stack_top(notit->stack)->base;
1188 base_type = bt_ctf_field_get_type(base_field);
1189 if (!base_type) {
1190 PERR("Failed to get base field's type\n");
1191 goto end;
1192 }
1193
1194 switch (bt_ctf_field_type_get_type_id(base_type)) {
1195 case BT_CTF_TYPE_ID_STRUCT:
1196 next_field = bt_ctf_field_structure_get_field_by_index(
1197 base_field, index);
1198 break;
1199 case BT_CTF_TYPE_ID_ARRAY:
1200 next_field = bt_ctf_field_array_get_field(base_field, index);
1201 break;
1202 case BT_CTF_TYPE_ID_SEQUENCE:
1203 next_field = bt_ctf_field_sequence_get_field(base_field, index);
1204 break;
1205 case BT_CTF_TYPE_ID_VARIANT:
1206 next_field = bt_ctf_field_variant_get_current_field(base_field);
1207 break;
1208 default:
1209 assert(false);
1210 break;
1211 }
1212
1213 if (!next_field) {
1214 next_field = NULL;
1215 }
1216
1217 end:
1218 BT_PUT(base_type);
1219
1220 return next_field;
1221 }
1222
1223 static
1224 enum bt_ctf_btr_status btr_signed_int_cb(int64_t value,
1225 struct bt_ctf_field_type *type, void *data)
1226 {
1227 enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK;
1228 struct bt_ctf_field *field = NULL;
1229 struct bt_ctf_field *int_field = NULL;
1230 struct bt_ctf_notif_iter *notit = data;
1231 int ret;
1232
1233 /* create next field */
1234 field = get_next_field(notit);
1235 if (!field) {
1236 PERR("Failed to get next field (signed int)\n");
1237 status = BT_CTF_BTR_STATUS_ERROR;
1238 goto end;
1239 }
1240
1241 switch(bt_ctf_field_type_get_type_id(type)) {
1242 case BT_CTF_TYPE_ID_INTEGER:
1243 /* Integer field is created field */
1244 BT_MOVE(int_field, field);
1245 break;
1246 case BT_CTF_TYPE_ID_ENUM:
1247 int_field = bt_ctf_field_enumeration_get_container(field);
1248 break;
1249 default:
1250 break;
1251 }
1252
1253 if (!int_field) {
1254 PERR("Failed to get integer field\n");
1255 status = BT_CTF_BTR_STATUS_ERROR;
1256 goto end;
1257 }
1258
1259 ret = bt_ctf_field_signed_integer_set_value(int_field, value);
1260 assert(!ret);
1261 stack_top(notit->stack)->index++;
1262
1263 end:
1264 BT_PUT(field);
1265 BT_PUT(int_field);
1266
1267 return status;
1268 }
1269
1270 static
1271 enum bt_ctf_btr_status btr_unsigned_int_cb(uint64_t value,
1272 struct bt_ctf_field_type *type, void *data)
1273 {
1274 enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK;
1275 struct bt_ctf_field *field = NULL;
1276 struct bt_ctf_field *int_field = NULL;
1277 struct bt_ctf_notif_iter *notit = data;
1278 int ret;
1279
1280 /* Create next field */
1281 field = get_next_field(notit);
1282 if (!field) {
1283 PERR("Failed to get next field (unsigned int)\n");
1284 status = BT_CTF_BTR_STATUS_ERROR;
1285 goto end;
1286 }
1287
1288 switch(bt_ctf_field_type_get_type_id(type)) {
1289 case BT_CTF_TYPE_ID_INTEGER:
1290 /* Integer field is created field */
1291 BT_MOVE(int_field, field);
1292 break;
1293 case BT_CTF_TYPE_ID_ENUM:
1294 int_field = bt_ctf_field_enumeration_get_container(field);
1295 break;
1296 default:
1297 break;
1298 }
1299
1300 if (!int_field) {
1301 PERR("Failed to get integer field\n");
1302 status = BT_CTF_BTR_STATUS_ERROR;
1303 goto end;
1304 }
1305
1306 ret = bt_ctf_field_unsigned_integer_set_value(int_field, value);
1307 assert(!ret);
1308 stack_top(notit->stack)->index++;
1309
1310 end:
1311 BT_PUT(field);
1312 BT_PUT(int_field);
1313
1314 return status;
1315 }
1316
1317 static
1318 enum bt_ctf_btr_status btr_floating_point_cb(double value,
1319 struct bt_ctf_field_type *type, void *data)
1320 {
1321 enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK;
1322 struct bt_ctf_field *field = NULL;
1323 struct bt_ctf_notif_iter *notit = data;
1324 int ret;
1325
1326 /* Create next field */
1327 field = get_next_field(notit);
1328 if (!field) {
1329 PERR("Failed to get next field (floating point number)\n");
1330 status = BT_CTF_BTR_STATUS_ERROR;
1331 goto end;
1332 }
1333
1334 ret = bt_ctf_field_floating_point_set_value(field, value);
1335 assert(!ret);
1336 stack_top(notit->stack)->index++;
1337
1338 end:
1339 BT_PUT(field);
1340
1341 return status;
1342 }
1343
1344 static
1345 enum bt_ctf_btr_status btr_string_begin_cb(
1346 struct bt_ctf_field_type *type, void *data)
1347 {
1348 enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK;
1349 struct bt_ctf_field *field = NULL;
1350 struct bt_ctf_notif_iter *notit = data;
1351 int ret;
1352
1353 /* Create next field */
1354 field = get_next_field(notit);
1355 if (!field) {
1356 PERR("Failed to get next field (string)\n");
1357 status = BT_CTF_BTR_STATUS_ERROR;
1358 goto end;
1359 }
1360
1361 /*
1362 * Push on stack. Not a compound type per se, but we know that only
1363 * btr_string_cb() may be called between this call and a subsequent
1364 * call to btr_string_end_cb().
1365 */
1366 ret = stack_push(notit->stack, field);
1367 if (ret) {
1368 PERR("Failed to push string field onto the stack\n");
1369 status = BT_CTF_BTR_STATUS_ERROR;
1370 goto end;
1371 }
1372
1373 /*
1374 * Initialize string field payload to an empty string since in the
1375 * case of a length 0 string the btr_string_cb won't be called and
1376 * we will end up with an unset string payload.
1377 */
1378 ret = bt_ctf_field_string_set_value(field, "");
1379 if (ret) {
1380 PERR("Failed to initialize string field\n");
1381 status = BT_CTF_BTR_STATUS_ERROR;
1382 goto end;
1383 }
1384
1385 end:
1386 BT_PUT(field);
1387
1388 return status;
1389 }
1390
1391 static
1392 enum bt_ctf_btr_status btr_string_cb(const char *value,
1393 size_t len, struct bt_ctf_field_type *type, void *data)
1394 {
1395 enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK;
1396 struct bt_ctf_field *field = NULL;
1397 struct bt_ctf_notif_iter *notit = data;
1398 int ret;
1399
1400 /* Get string field */
1401 field = stack_top(notit->stack)->base;
1402 assert(field);
1403
1404 /* Append current string */
1405 ret = bt_ctf_field_string_append_len(field, value, len);
1406 if (ret) {
1407 PERR("Failed to append a string to a string field\n");
1408 status = BT_CTF_BTR_STATUS_ERROR;
1409 goto end;
1410 }
1411
1412 end:
1413 return status;
1414 }
1415
1416 static
1417 enum bt_ctf_btr_status btr_string_end_cb(
1418 struct bt_ctf_field_type *type, void *data)
1419 {
1420 struct bt_ctf_notif_iter *notit = data;
1421
1422 /* Pop string field */
1423 stack_pop(notit->stack);
1424
1425 /* Go to next field */
1426 stack_top(notit->stack)->index++;
1427
1428 return BT_CTF_BTR_STATUS_OK;
1429 }
1430
1431 enum bt_ctf_btr_status btr_compound_begin_cb(
1432 struct bt_ctf_field_type *type, void *data)
1433 {
1434 enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK;
1435 struct bt_ctf_notif_iter *notit = data;
1436 struct bt_ctf_field *field;
1437 int ret;
1438
1439 /* Create field */
1440 if (stack_empty(notit->stack)) {
1441 /* Root: create dynamic scope field */
1442 *notit->cur_dscope_field = bt_ctf_field_create(type);
1443 field = *notit->cur_dscope_field;
1444
1445 /*
1446 * Field will be put at the end of this function
1447 * (stack_push() will take one reference, but this
1448 * reference is lost upon the equivalent stack_pop()
1449 * later), so also get it for our context to own it.
1450 */
1451 bt_get(*notit->cur_dscope_field);
1452 } else {
1453 field = get_next_field(notit);
1454 }
1455
1456 if (!field) {
1457 PERR("Failed to get next field or create dynamic scope field\n");
1458 status = BT_CTF_BTR_STATUS_ERROR;
1459 goto end;
1460 }
1461
1462 /* Push field */
1463 ret = stack_push(notit->stack, field);
1464 if (ret) {
1465 PERR("Failed to push compound field onto the stack\n");
1466 status = BT_CTF_BTR_STATUS_ERROR;
1467 goto end;
1468 }
1469
1470 end:
1471 BT_PUT(field);
1472
1473 return status;
1474 }
1475
1476 enum bt_ctf_btr_status btr_compound_end_cb(
1477 struct bt_ctf_field_type *type, void *data)
1478 {
1479 struct bt_ctf_notif_iter *notit = data;
1480
1481 assert(!stack_empty(notit->stack));
1482
1483 /* Pop stack */
1484 stack_pop(notit->stack);
1485
1486 /* If the stack is not empty, increment the base's index */
1487 if (!stack_empty(notit->stack)) {
1488 stack_top(notit->stack)->index++;
1489 }
1490
1491 return BT_CTF_BTR_STATUS_OK;
1492 }
1493
1494 static
1495 struct bt_ctf_field *resolve_field(struct bt_ctf_notif_iter *notit,
1496 struct bt_ctf_field_path *path)
1497 {
1498 struct bt_ctf_field *field = NULL;
1499 unsigned int i;
1500
1501 switch (bt_ctf_field_path_get_root_scope(path)) {
1502 case BT_CTF_SCOPE_TRACE_PACKET_HEADER:
1503 field = notit->dscopes.trace_packet_header;
1504 break;
1505 case BT_CTF_SCOPE_STREAM_PACKET_CONTEXT:
1506 field = notit->dscopes.stream_packet_context;
1507 break;
1508 case BT_CTF_SCOPE_STREAM_EVENT_HEADER:
1509 field = notit->dscopes.stream_event_header;
1510 break;
1511 case BT_CTF_SCOPE_STREAM_EVENT_CONTEXT:
1512 field = notit->dscopes.stream_event_context;
1513 break;
1514 case BT_CTF_SCOPE_EVENT_CONTEXT:
1515 field = notit->dscopes.event_context;
1516 break;
1517 case BT_CTF_SCOPE_EVENT_FIELDS:
1518 field = notit->dscopes.event_payload;
1519 break;
1520 default:
1521 break;
1522 }
1523
1524 if (!field) {
1525 goto end;
1526 }
1527
1528 bt_get(field);
1529
1530 for (i = 0; i < bt_ctf_field_path_get_index_count(path); ++i) {
1531 struct bt_ctf_field *next_field = NULL;
1532 struct bt_ctf_field_type *field_type;
1533 int index = bt_ctf_field_path_get_index(path, i);
1534
1535 field_type = bt_ctf_field_get_type(field);
1536 if (!field_type) {
1537 BT_PUT(field);
1538 goto end;
1539 }
1540
1541 if (is_struct_type(field_type)) {
1542 next_field = bt_ctf_field_structure_get_field_by_index(
1543 field, index);
1544 } else if (is_variant_type(field_type)) {
1545 next_field =
1546 bt_ctf_field_variant_get_current_field(field);
1547 }
1548
1549 BT_PUT(field);
1550 BT_PUT(field_type);
1551
1552 if (!next_field) {
1553 goto end;
1554 }
1555
1556 /* Move next field -> field */
1557 BT_MOVE(field, next_field);
1558 }
1559
1560 end:
1561 return field;
1562 }
1563
1564 static
1565 int64_t btr_get_sequence_length_cb(struct bt_ctf_field_type *type, void *data)
1566 {
1567 int64_t ret = -1;
1568 int iret;
1569 struct bt_ctf_field_path *field_path;
1570 struct bt_ctf_notif_iter *notit = data;
1571 struct bt_ctf_field *length_field = NULL;
1572 uint64_t length;
1573
1574 field_path = bt_ctf_field_type_sequence_get_length_field_path(type);
1575 if (!field_path) {
1576 goto end;
1577 }
1578
1579 length_field = resolve_field(notit, field_path);
1580 if (!length_field) {
1581 goto end;
1582 }
1583
1584 iret = bt_ctf_field_unsigned_integer_get_value(length_field, &length);
1585 if (iret) {
1586 goto end;
1587 }
1588
1589 iret = bt_ctf_field_sequence_set_length(stack_top(notit->stack)->base,
1590 length_field);
1591 if (iret) {
1592 goto end;
1593 }
1594 ret = (int64_t) length;
1595
1596 end:
1597 BT_PUT(length_field);
1598 BT_PUT(field_path);
1599
1600 return ret;
1601 }
1602
1603 static
1604 struct bt_ctf_field_type *btr_get_variant_type_cb(
1605 struct bt_ctf_field_type *type, void *data)
1606 {
1607 struct bt_ctf_field_path *path;
1608 struct bt_ctf_notif_iter *notit = data;
1609 struct bt_ctf_field *tag_field = NULL;
1610 struct bt_ctf_field *selected_field = NULL;
1611 struct bt_ctf_field_type *selected_field_type = NULL;
1612
1613 path = bt_ctf_field_type_variant_get_tag_field_path(type);
1614 if (!path) {
1615 goto end;
1616 }
1617
1618 tag_field = resolve_field(notit, path);
1619 if (!tag_field) {
1620 goto end;
1621 }
1622
1623 /*
1624 * We found the enumeration tag field instance which should be
1625 * able to select a current field for this variant. This
1626 * callback function we're in is called _after_
1627 * compound_begin(), so the current stack top's base field is
1628 * the variant field in question. We get the selected field here
1629 * thanks to this tag field (thus creating the selected field),
1630 * which will also provide us with its type. Then, this field
1631 * will remain the current selected one until the next callback
1632 * function call which is used to fill the current selected
1633 * field.
1634 */
1635 selected_field = bt_ctf_field_variant_get_field(
1636 stack_top(notit->stack)->base, tag_field);
1637 if (!selected_field) {
1638 goto end;
1639 }
1640
1641 selected_field_type = bt_ctf_field_get_type(selected_field);
1642
1643 end:
1644 BT_PUT(tag_field);
1645 BT_PUT(selected_field);
1646 BT_PUT(path);
1647
1648 return selected_field_type;
1649 }
1650
1651 static
1652 struct bt_ctf_event *create_event(struct bt_ctf_notif_iter *notit)
1653 {
1654 int ret;
1655 struct bt_ctf_event *event;
1656
1657 /* Create event object. */
1658 event = bt_ctf_event_create(notit->meta.event_class);
1659 if (!event) {
1660 goto error;
1661 }
1662
1663 /* Set header, stream event context, context, and payload fields. */
1664 ret = bt_ctf_event_set_header(event,
1665 notit->dscopes.stream_event_header);
1666 if (ret) {
1667 goto error;
1668 }
1669
1670 ret = bt_ctf_event_set_stream_event_context(event,
1671 notit->dscopes.stream_event_context);
1672 if (ret) {
1673 goto error;
1674 }
1675
1676 ret = bt_ctf_event_set_event_context(event,
1677 notit->dscopes.event_context);
1678 if (ret) {
1679 goto error;
1680 }
1681
1682 ret = bt_ctf_event_set_payload_field(event,
1683 notit->dscopes.event_payload);
1684 if (ret) {
1685 goto error;
1686 }
1687
1688 /* Associate with current packet. */
1689 assert(notit->packet);
1690 ret = bt_ctf_event_set_packet(event, notit->packet);
1691 if (ret) {
1692 goto error;
1693 }
1694
1695 goto end;
1696 error:
1697 BT_PUT(event);
1698 end:
1699 return event;
1700 }
1701
1702 static
1703 void create_packet(struct bt_ctf_notif_iter *notit)
1704 {
1705 int ret;
1706 struct bt_ctf_stream *stream = NULL;
1707 struct bt_ctf_packet *packet = NULL;
1708
1709 /* Ask the user for the stream */
1710 stream = notit->medium.medops.get_stream(notit->meta.stream_class,
1711 notit->medium.data);
1712 if (!stream) {
1713 goto error;
1714 }
1715
1716 /* Create packet */
1717 packet = bt_ctf_packet_create(stream);
1718 if (!packet) {
1719 goto error;
1720 }
1721
1722 /* Set packet's context and header fields */
1723 if (notit->dscopes.trace_packet_header) {
1724 ret = bt_ctf_packet_set_header(packet,
1725 notit->dscopes.trace_packet_header);
1726 if (ret) {
1727 goto error;
1728 }
1729 }
1730
1731 if (notit->dscopes.stream_packet_context) {
1732 ret = bt_ctf_packet_set_context(packet,
1733 notit->dscopes.stream_packet_context);
1734 if (ret) {
1735 goto error;
1736 }
1737 }
1738
1739 goto end;
1740 error:
1741 BT_PUT(packet);
1742 end:
1743 BT_MOVE(notit->packet, packet);
1744 }
1745
1746 static
1747 void notify_new_packet(struct bt_ctf_notif_iter *notit,
1748 struct bt_notification **notification)
1749 {
1750 struct bt_notification *ret;
1751
1752 /* Initialize the iterator's current packet */
1753 create_packet(notit);
1754 if (!notit->packet) {
1755 return;
1756 }
1757
1758 ret = bt_notification_packet_start_create(notit->packet);
1759 if (!ret) {
1760 return;
1761 }
1762 *notification = ret;
1763 }
1764
1765 static
1766 void notify_end_of_packet(struct bt_ctf_notif_iter *notit,
1767 struct bt_notification **notification)
1768 {
1769 struct bt_notification *ret;
1770
1771 if (!notit->packet) {
1772 return;
1773 }
1774
1775 ret = bt_notification_packet_end_create(notit->packet);
1776 if (!ret) {
1777 return;
1778 }
1779 BT_PUT(notit->packet);
1780 *notification = ret;
1781 }
1782
1783 static
1784 void notify_event(struct bt_ctf_notif_iter *notit,
1785 struct bt_notification **notification)
1786 {
1787 struct bt_ctf_event *event;
1788 struct bt_notification *ret = NULL;
1789
1790 /* Create event */
1791 event = create_event(notit);
1792 if (!event) {
1793 goto end;
1794 }
1795
1796 ret = bt_notification_event_create(event);
1797 if (!ret) {
1798 goto end;
1799 }
1800 *notification = ret;
1801 end:
1802 BT_PUT(event);
1803 }
1804
1805 static
1806 void notify_eos(struct bt_ctf_notif_iter *notit,
1807 struct bt_notification **notification)
1808 {
1809 struct bt_ctf_event *event;
1810 struct bt_notification *ret = NULL;
1811
1812 /* Create event */
1813 event = create_event(notit);
1814 if (!event) {
1815 goto end;
1816 }
1817
1818 ret = bt_notification_stream_end_create(event);
1819 if (!ret) {
1820 goto end;
1821 }
1822 *notification = ret;
1823 end:
1824 BT_PUT(event);
1825 }
1826
1827 struct bt_ctf_notif_iter *bt_ctf_notif_iter_create(struct bt_ctf_trace *trace,
1828 size_t max_request_sz,
1829 struct bt_ctf_notif_iter_medium_ops medops,
1830 void *data, FILE *err_stream)
1831 {
1832 struct bt_ctf_notif_iter *notit = NULL;
1833 struct bt_ctf_btr_cbs cbs = {
1834 .types = {
1835 .signed_int = btr_signed_int_cb,
1836 .unsigned_int = btr_unsigned_int_cb,
1837 .floating_point = btr_floating_point_cb,
1838 .string_begin = btr_string_begin_cb,
1839 .string = btr_string_cb,
1840 .string_end = btr_string_end_cb,
1841 .compound_begin = btr_compound_begin_cb,
1842 .compound_end = btr_compound_end_cb,
1843 },
1844 .query = {
1845 .get_sequence_length = btr_get_sequence_length_cb,
1846 .get_variant_type = btr_get_variant_type_cb,
1847 },
1848 };
1849
1850 assert(trace);
1851 assert(medops.request_bytes);
1852 notit = g_new0(struct bt_ctf_notif_iter, 1);
1853 if (!notit) {
1854 PERR("Failed to allocate memory for CTF notification iterator\n");
1855 goto end;
1856 }
1857
1858 notit->meta.trace = bt_get(trace);
1859 notit->medium.medops = medops;
1860 notit->medium.max_request_sz = max_request_sz;
1861 notit->medium.data = data;
1862 notit->err_stream = err_stream;
1863 notit->stack = stack_new(notit);
1864 if (!notit->stack) {
1865 PERR("Failed to create stack\n");
1866 bt_ctf_notif_iter_destroy(notit);
1867 notit = NULL;
1868 goto end;
1869 }
1870
1871 notit->btr = bt_ctf_btr_create(cbs, notit, err_stream);
1872 if (!notit->btr) {
1873 PERR("Failed to create binary type reader\n");
1874 bt_ctf_notif_iter_destroy(notit);
1875 notit = NULL;
1876 goto end;
1877 }
1878
1879 bt_ctf_notif_iter_reset(notit);
1880
1881 end:
1882 return notit;
1883 }
1884
1885 void bt_ctf_notif_iter_destroy(struct bt_ctf_notif_iter *notit)
1886 {
1887 BT_PUT(notit->meta.trace);
1888 BT_PUT(notit->meta.stream_class);
1889 BT_PUT(notit->meta.event_class);
1890 BT_PUT(notit->packet);
1891 put_all_dscopes(notit);
1892
1893 if (notit->stack) {
1894 stack_destroy(notit->stack);
1895 }
1896
1897 if (notit->btr) {
1898 bt_ctf_btr_destroy(notit->btr);
1899 }
1900
1901 g_free(notit);
1902 }
1903
1904 enum bt_ctf_notif_iter_status bt_ctf_notif_iter_get_next_notification(
1905 struct bt_ctf_notif_iter *notit,
1906 struct bt_notification **notification)
1907 {
1908 enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK;
1909
1910 assert(notit);
1911 assert(notification);
1912
1913 while (true) {
1914 status = handle_state(notit);
1915 if (status != BT_CTF_NOTIF_ITER_STATUS_OK) {
1916 if (status == BT_CTF_NOTIF_ITER_STATUS_EOF) {
1917 PDBG("Medium operation reported end of stream\n");
1918 } else {
1919 PERR("Failed to handle state:\n");
1920 PERR("\tState: %d\n", notit->state);
1921 }
1922 goto end;
1923 }
1924
1925 switch (notit->state) {
1926 case STATE_EMIT_NOTIF_NEW_PACKET:
1927 PDBG("Emitting new packet notification\n");
1928 notify_new_packet(notit, notification);
1929 if (!*notification) {
1930 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
1931 }
1932 goto end;
1933 case STATE_EMIT_NOTIF_EVENT:
1934 PDBG("Emitting event notification\n");
1935 notify_event(notit, notification);
1936 if (!*notification) {
1937 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
1938 }
1939 goto end;
1940 case STATE_EMIT_NOTIF_END_OF_PACKET:
1941 PDBG("Emitting end of packet notification\n");
1942 notify_end_of_packet(notit, notification);
1943 if (!*notification) {
1944 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
1945 }
1946 goto end;
1947 default:
1948 /* Non-emitting state: continue */
1949 break;
1950 }
1951 }
1952
1953 end:
1954 return status;
1955 }
This page took 0.067113 seconds and 5 git commands to generate.