Make API CTF-agnostic
[babeltrace.git] / plugins / ctf / common / notif-iter / notif-iter.c
1 /*
2 * Babeltrace - CTF notification iterator
3 *
4 * Copyright (c) 2015-2018 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2015-2018 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 #define BT_LOG_TAG "PLUGIN-CTF-NOTIF-ITER"
27 #include "logging.h"
28
29 #include <stdint.h>
30 #include <inttypes.h>
31 #include <stdio.h>
32 #include <stddef.h>
33 #include <stdbool.h>
34 #include <babeltrace/assert-internal.h>
35 #include <string.h>
36 #include <babeltrace/babeltrace.h>
37 #include <babeltrace/common-internal.h>
38 #include <glib.h>
39 #include <stdlib.h>
40
41 #include "notif-iter.h"
42 #include "../btr/btr.h"
43
44 struct bt_notif_iter;
45
46 /* A visit stack entry */
47 struct stack_entry {
48 /*
49 * Current base field, one of:
50 *
51 * * string
52 * * structure
53 * * array
54 * * sequence
55 * * variant
56 *
57 * Field is borrowed.
58 */
59 struct bt_field *base;
60
61 /* Index of next field to set */
62 size_t index;
63 };
64
65 /* Visit stack */
66 struct stack {
67 /* Entries (struct stack_entry) */
68 GArray *entries;
69
70 /* Number of active entries */
71 size_t size;
72 };
73
74 /* State */
75 enum state {
76 STATE_INIT,
77 STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN,
78 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE,
79 STATE_AFTER_TRACE_PACKET_HEADER,
80 STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN,
81 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE,
82 STATE_AFTER_STREAM_PACKET_CONTEXT,
83 STATE_EMIT_NOTIF_NEW_STREAM,
84 STATE_EMIT_NOTIF_NEW_PACKET,
85 STATE_DSCOPE_EVENT_HEADER_BEGIN,
86 STATE_DSCOPE_EVENT_HEADER_CONTINUE,
87 STATE_AFTER_EVENT_HEADER,
88 STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN,
89 STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE,
90 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN,
91 STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE,
92 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN,
93 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE,
94 STATE_EMIT_NOTIF_EVENT,
95 STATE_EMIT_NOTIF_END_OF_PACKET,
96 STATE_DONE,
97 STATE_SKIP_PACKET_PADDING,
98 };
99
100 /* CTF notification iterator */
101 struct bt_notif_iter {
102 /* Visit stack */
103 struct stack *stack;
104
105 /* Current notification iterator to create notifications (weak) */
106 struct bt_private_connection_private_notification_iterator *notif_iter;
107
108 /*
109 * Current dynamic scope field pointer.
110 *
111 * This is set by read_dscope_begin_state() and contains the
112 * value of one of the pointers in `dscopes` below.
113 */
114 struct bt_field *cur_dscope_field;
115
116 /*
117 * True if we're done filling a string field from a text
118 * array/sequence payload.
119 */
120 bool done_filling_string;
121
122 /* Trace and classes */
123 struct {
124 struct ctf_trace_class *tc;
125 struct ctf_stream_class *sc;
126 struct ctf_event_class *ec;
127 } meta;
128
129 /* Current packet header field wrapper (NULL if not created yet) */
130 struct bt_packet_header_field *packet_header_field;
131
132 /* Current packet header field wrapper (NULL if not created yet) */
133 struct bt_packet_context_field *packet_context_field;
134
135 /* Current event header field (NULL if not created yet) */
136 struct bt_event_header_field *event_header_field;
137
138 /* Current packet (NULL if not created yet) */
139 struct bt_packet *packet;
140
141 /* Current stream (NULL if not set yet) */
142 struct bt_stream *stream;
143
144 /* Current event (NULL if not created yet) */
145 struct bt_event *event;
146
147 /* Current event notification (NULL if not created yet) */
148 struct bt_notification *event_notif;
149
150 /* Database of current dynamic scopes */
151 struct {
152 struct bt_field *trace_packet_header;
153 struct bt_field *stream_packet_context;
154 struct bt_field *event_header;
155 struct bt_field *event_common_context;
156 struct bt_field *event_spec_context;
157 struct bt_field *event_payload;
158 } dscopes;
159
160 /* Current state */
161 enum state state;
162
163 /* Current medium buffer data */
164 struct {
165 /* Last address provided by medium */
166 const uint8_t *addr;
167
168 /* Buffer size provided by medium (bytes) */
169 size_t sz;
170
171 /* Offset within whole packet of addr (bits) */
172 size_t packet_offset;
173
174 /* Current position from addr (bits) */
175 size_t at;
176
177 /* Position of the last event header from addr (bits) */
178 size_t last_eh_at;
179 } buf;
180
181 /* Binary type reader */
182 struct bt_btr *btr;
183
184 /* Current medium data */
185 struct {
186 struct bt_notif_iter_medium_ops medops;
187 size_t max_request_sz;
188 void *data;
189 } medium;
190
191 /* Stream beginning was emitted */
192 bool stream_begin_emitted;
193
194 /* Current packet size (bits) (-1 if unknown) */
195 int64_t cur_exp_packet_total_size;
196
197 /* Current content size (bits) (-1 if unknown) */
198 int64_t cur_exp_packet_content_size;
199
200 /* Current stream class ID */
201 int64_t cur_stream_class_id;
202
203 /* Current event class ID */
204 int64_t cur_event_class_id;
205
206 /* Current data stream ID */
207 int64_t cur_data_stream_id;
208
209 /*
210 * Offset, in the underlying media, of the current packet's
211 * start (-1 if unknown).
212 */
213 off_t cur_packet_offset;
214
215 /* Default clock's current value */
216 uint64_t default_clock_val;
217
218 /* End of packet snapshots */
219 struct {
220 uint64_t discarded_events;
221 uint64_t packets;
222 uint64_t beginning_clock;
223 uint64_t end_clock;
224 } snapshots;
225
226 /* Stored values (for sequence lengths, variant tags) */
227 GArray *stored_values;
228 };
229
230 static inline
231 const char *state_string(enum state state)
232 {
233 switch (state) {
234 case STATE_INIT:
235 return "STATE_INIT";
236 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
237 return "STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN";
238 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
239 return "STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE";
240 case STATE_AFTER_TRACE_PACKET_HEADER:
241 return "STATE_AFTER_TRACE_PACKET_HEADER";
242 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN:
243 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN";
244 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE:
245 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE";
246 case STATE_AFTER_STREAM_PACKET_CONTEXT:
247 return "STATE_AFTER_STREAM_PACKET_CONTEXT";
248 case STATE_EMIT_NOTIF_NEW_PACKET:
249 return "STATE_EMIT_NOTIF_NEW_PACKET";
250 case STATE_EMIT_NOTIF_NEW_STREAM:
251 return "STATE_EMIT_NOTIF_NEW_STREAM";
252 case STATE_DSCOPE_EVENT_HEADER_BEGIN:
253 return "STATE_DSCOPE_EVENT_HEADER_BEGIN";
254 case STATE_DSCOPE_EVENT_HEADER_CONTINUE:
255 return "STATE_DSCOPE_EVENT_HEADER_CONTINUE";
256 case STATE_AFTER_EVENT_HEADER:
257 return "STATE_AFTER_EVENT_HEADER";
258 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN:
259 return "STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN";
260 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE:
261 return "STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE";
262 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN:
263 return "STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN";
264 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE:
265 return "STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE";
266 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN:
267 return "STATE_DSCOPE_EVENT_PAYLOAD_BEGIN";
268 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE:
269 return "STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE";
270 case STATE_EMIT_NOTIF_EVENT:
271 return "STATE_EMIT_NOTIF_EVENT";
272 case STATE_EMIT_NOTIF_END_OF_PACKET:
273 return "STATE_EMIT_NOTIF_END_OF_PACKET";
274 case STATE_DONE:
275 return "STATE_DONE";
276 case STATE_SKIP_PACKET_PADDING:
277 return "STATE_SKIP_PACKET_PADDING";
278 default:
279 return "(unknown)";
280 }
281 }
282
283 static
284 int bt_notif_iter_switch_packet(struct bt_notif_iter *notit);
285
286 static
287 struct stack *stack_new(struct bt_notif_iter *notit)
288 {
289 struct stack *stack = NULL;
290
291 stack = g_new0(struct stack, 1);
292 if (!stack) {
293 BT_LOGE_STR("Failed to allocate one stack.");
294 goto error;
295 }
296
297 stack->entries = g_array_new(FALSE, TRUE, sizeof(struct stack_entry));
298 if (!stack->entries) {
299 BT_LOGE_STR("Failed to allocate a GArray.");
300 goto error;
301 }
302
303 BT_LOGD("Created stack: notit-addr=%p, stack-addr=%p", notit, stack);
304 goto end;
305
306 error:
307 g_free(stack);
308 stack = NULL;
309
310 end:
311 return stack;
312 }
313
314 static
315 void stack_destroy(struct stack *stack)
316 {
317 BT_ASSERT(stack);
318 BT_LOGD("Destroying stack: addr=%p", stack);
319
320 if (stack->entries) {
321 g_array_free(stack->entries, TRUE);
322 }
323
324 g_free(stack);
325 }
326
327 static
328 void stack_push(struct stack *stack, struct bt_field *base)
329 {
330 struct stack_entry *entry;
331
332 BT_ASSERT(stack);
333 BT_ASSERT(base);
334 BT_LOGV("Pushing base field on stack: stack-addr=%p, "
335 "stack-size-before=%zu, stack-size-after=%zu",
336 stack, stack->size, stack->size + 1);
337
338 if (stack->entries->len == stack->size) {
339 g_array_set_size(stack->entries, stack->size + 1);
340 }
341
342 entry = &g_array_index(stack->entries, struct stack_entry, stack->size);
343 entry->base = base;
344 entry->index = 0;
345 stack->size++;
346 }
347
348 static inline
349 unsigned int stack_size(struct stack *stack)
350 {
351 BT_ASSERT(stack);
352 return stack->size;
353 }
354
355 static
356 void stack_pop(struct stack *stack)
357 {
358 BT_ASSERT(stack);
359 BT_ASSERT(stack_size(stack));
360 BT_LOGV("Popping from stack: "
361 "stack-addr=%p, stack-size-before=%zu, stack-size-after=%zu",
362 stack, stack->size, stack->size - 1);
363 stack->size--;
364 }
365
366 static inline
367 struct stack_entry *stack_top(struct stack *stack)
368 {
369 BT_ASSERT(stack);
370 BT_ASSERT(stack_size(stack));
371 return &g_array_index(stack->entries, struct stack_entry,
372 stack->size - 1);
373 }
374
375 static inline
376 bool stack_empty(struct stack *stack)
377 {
378 return stack_size(stack) == 0;
379 }
380
381 static
382 void stack_clear(struct stack *stack)
383 {
384 BT_ASSERT(stack);
385 stack->size = 0;
386 }
387
388 static inline
389 enum bt_notif_iter_status notif_iter_status_from_m_status(
390 enum bt_notif_iter_medium_status m_status)
391 {
392 /* They are the same */
393 return (int) m_status;
394 }
395
396 static inline
397 size_t buf_size_bits(struct bt_notif_iter *notit)
398 {
399 return notit->buf.sz * 8;
400 }
401
402 static inline
403 size_t buf_available_bits(struct bt_notif_iter *notit)
404 {
405 return buf_size_bits(notit) - notit->buf.at;
406 }
407
408 static inline
409 size_t packet_at(struct bt_notif_iter *notit)
410 {
411 return notit->buf.packet_offset + notit->buf.at;
412 }
413
414 static inline
415 void buf_consume_bits(struct bt_notif_iter *notit, size_t incr)
416 {
417 BT_LOGV("Advancing cursor: notit-addr=%p, cur-before=%zu, cur-after=%zu",
418 notit, notit->buf.at, notit->buf.at + incr);
419 notit->buf.at += incr;
420 }
421
422 static
423 enum bt_notif_iter_status request_medium_bytes(
424 struct bt_notif_iter *notit)
425 {
426 uint8_t *buffer_addr = NULL;
427 size_t buffer_sz = 0;
428 enum bt_notif_iter_medium_status m_status;
429
430 BT_LOGV("Calling user function (request bytes): notit-addr=%p, "
431 "request-size=%zu", notit, notit->medium.max_request_sz);
432 m_status = notit->medium.medops.request_bytes(
433 notit->medium.max_request_sz, &buffer_addr,
434 &buffer_sz, notit->medium.data);
435 BT_LOGV("User function returned: status=%s, buf-addr=%p, buf-size=%zu",
436 bt_notif_iter_medium_status_string(m_status),
437 buffer_addr, buffer_sz);
438 if (m_status == BT_NOTIF_ITER_MEDIUM_STATUS_OK) {
439 BT_ASSERT(buffer_sz != 0);
440
441 /* New packet offset is old one + old size (in bits) */
442 notit->buf.packet_offset += buf_size_bits(notit);
443
444 /* Restart at the beginning of the new medium buffer */
445 notit->buf.at = 0;
446 notit->buf.last_eh_at = SIZE_MAX;
447
448 /* New medium buffer size */
449 notit->buf.sz = buffer_sz;
450
451 /* New medium buffer address */
452 notit->buf.addr = buffer_addr;
453
454 BT_LOGV("User function returned new bytes: "
455 "packet-offset=%zu, cur=%zu, size=%zu, addr=%p",
456 notit->buf.packet_offset, notit->buf.at,
457 notit->buf.sz, notit->buf.addr);
458 BT_LOGV_MEM(buffer_addr, buffer_sz, "Returned bytes at %p:",
459 buffer_addr);
460 } else if (m_status == BT_NOTIF_ITER_MEDIUM_STATUS_EOF) {
461 /*
462 * User returned end of stream: validate that we're not
463 * in the middle of a packet header, packet context, or
464 * event.
465 */
466 if (notit->cur_exp_packet_total_size >= 0) {
467 if (packet_at(notit) ==
468 notit->cur_exp_packet_total_size) {
469 goto end;
470 }
471 } else {
472 if (packet_at(notit) == 0) {
473 goto end;
474 }
475
476 if (notit->buf.last_eh_at != SIZE_MAX &&
477 notit->buf.at == notit->buf.last_eh_at) {
478 goto end;
479 }
480 }
481
482 /* All other states are invalid */
483 BT_LOGW("User function returned %s, but notification iterator is in an unexpected state: "
484 "state=%s, cur-packet-size=%" PRId64 ", cur=%zu, "
485 "packet-cur=%zu, last-eh-at=%zu",
486 bt_notif_iter_medium_status_string(m_status),
487 state_string(notit->state),
488 notit->cur_exp_packet_total_size,
489 notit->buf.at, packet_at(notit),
490 notit->buf.last_eh_at);
491 m_status = BT_NOTIF_ITER_MEDIUM_STATUS_ERROR;
492 } else if (m_status < 0) {
493 BT_LOGW("User function failed: status=%s",
494 bt_notif_iter_medium_status_string(m_status));
495 }
496
497 end:
498 return notif_iter_status_from_m_status(m_status);
499 }
500
501 static inline
502 enum bt_notif_iter_status buf_ensure_available_bits(
503 struct bt_notif_iter *notit)
504 {
505 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
506
507 if (unlikely(buf_available_bits(notit) == 0)) {
508 /*
509 * This _cannot_ return BT_NOTIF_ITER_STATUS_OK
510 * _and_ no bits.
511 */
512 status = request_medium_bytes(notit);
513 }
514
515 return status;
516 }
517
518 static
519 enum bt_notif_iter_status read_dscope_begin_state(
520 struct bt_notif_iter *notit,
521 struct ctf_field_type *dscope_ft,
522 enum state done_state, enum state continue_state,
523 struct bt_field *dscope_field)
524 {
525 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
526 enum bt_btr_status btr_status;
527 size_t consumed_bits;
528
529 notit->cur_dscope_field = dscope_field;
530 BT_LOGV("Starting BTR: notit-addr=%p, btr-addr=%p, ft-addr=%p",
531 notit, notit->btr, dscope_ft);
532 consumed_bits = bt_btr_start(notit->btr, dscope_ft,
533 notit->buf.addr, notit->buf.at, packet_at(notit),
534 notit->buf.sz, &btr_status);
535 BT_LOGV("BTR consumed bits: size=%zu", consumed_bits);
536
537 switch (btr_status) {
538 case BT_BTR_STATUS_OK:
539 /* type was read completely */
540 BT_LOGV_STR("Field was completely decoded.");
541 notit->state = done_state;
542 break;
543 case BT_BTR_STATUS_EOF:
544 BT_LOGV_STR("BTR needs more data to decode field completely.");
545 notit->state = continue_state;
546 break;
547 default:
548 BT_LOGW("BTR failed to start: notit-addr=%p, btr-addr=%p, "
549 "status=%s", notit, notit->btr,
550 bt_btr_status_string(btr_status));
551 status = BT_NOTIF_ITER_STATUS_ERROR;
552 goto end;
553 }
554
555 /* Consume bits now since we know we're not in an error state */
556 buf_consume_bits(notit, consumed_bits);
557
558 end:
559 return status;
560 }
561
562 static
563 enum bt_notif_iter_status read_dscope_continue_state(
564 struct bt_notif_iter *notit, enum state done_state)
565 {
566 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
567 enum bt_btr_status btr_status;
568 size_t consumed_bits;
569
570 BT_LOGV("Continuing BTR: notit-addr=%p, btr-addr=%p",
571 notit, notit->btr);
572
573 status = buf_ensure_available_bits(notit);
574 if (status != BT_NOTIF_ITER_STATUS_OK) {
575 if (status < 0) {
576 BT_LOGW("Cannot ensure that buffer has at least one byte: "
577 "notif-addr=%p, status=%s",
578 notit, bt_notif_iter_status_string(status));
579 } else {
580 BT_LOGV("Cannot ensure that buffer has at least one byte: "
581 "notif-addr=%p, status=%s",
582 notit, bt_notif_iter_status_string(status));
583 }
584
585 goto end;
586 }
587
588 consumed_bits = bt_btr_continue(notit->btr, notit->buf.addr,
589 notit->buf.sz, &btr_status);
590 BT_LOGV("BTR consumed bits: size=%zu", consumed_bits);
591
592 switch (btr_status) {
593 case BT_BTR_STATUS_OK:
594 /* Type was read completely. */
595 BT_LOGV_STR("Field was completely decoded.");
596 notit->state = done_state;
597 break;
598 case BT_BTR_STATUS_EOF:
599 /* Stay in this continue state. */
600 BT_LOGV_STR("BTR needs more data to decode field completely.");
601 break;
602 default:
603 BT_LOGW("BTR failed to continue: notit-addr=%p, btr-addr=%p, "
604 "status=%s", notit, notit->btr,
605 bt_btr_status_string(btr_status));
606 status = BT_NOTIF_ITER_STATUS_ERROR;
607 goto end;
608 }
609
610 /* Consume bits now since we know we're not in an error state. */
611 buf_consume_bits(notit, consumed_bits);
612 end:
613 return status;
614 }
615
616 static
617 void release_event_dscopes(struct bt_notif_iter *notit)
618 {
619 notit->dscopes.event_header = NULL;
620
621 if (notit->event_header_field) {
622 bt_event_header_field_release(notit->event_header_field);
623 notit->event_header_field = NULL;
624 }
625
626 notit->dscopes.event_common_context = NULL;
627 notit->dscopes.event_spec_context = NULL;
628 notit->dscopes.event_payload = NULL;
629 }
630
631 static
632 void release_all_dscopes(struct bt_notif_iter *notit)
633 {
634 notit->dscopes.trace_packet_header = NULL;
635
636 if (notit->packet_header_field) {
637 bt_packet_header_field_release(notit->packet_header_field);
638 notit->packet_header_field = NULL;
639 }
640
641 notit->dscopes.stream_packet_context = NULL;
642
643 if (notit->packet_context_field) {
644 bt_packet_context_field_release(notit->packet_context_field);
645 notit->packet_context_field = NULL;
646 }
647
648 release_event_dscopes(notit);
649 }
650
651 static
652 enum bt_notif_iter_status read_packet_header_begin_state(
653 struct bt_notif_iter *notit)
654 {
655 struct ctf_field_type *packet_header_ft = NULL;
656 enum bt_notif_iter_status ret = BT_NOTIF_ITER_STATUS_OK;
657
658 if (bt_notif_iter_switch_packet(notit)) {
659 BT_LOGW("Cannot switch packet: notit-addr=%p", notit);
660 ret = BT_NOTIF_ITER_STATUS_ERROR;
661 goto end;
662 }
663
664 /* Packet header type is common to the whole trace. */
665 packet_header_ft = notit->meta.tc->packet_header_ft;
666 if (!packet_header_ft) {
667 notit->state = STATE_AFTER_TRACE_PACKET_HEADER;
668 goto end;
669 }
670
671 BT_ASSERT(!notit->packet_header_field);
672
673 if (packet_header_ft->in_ir) {
674 /*
675 * Create free packet header field from trace. This
676 * field is going to be moved to the packet once we
677 * create it. We cannot create the packet now because:
678 *
679 * 1. A packet is created from a stream.
680 * 2. A stream is created from a stream class.
681 * 3. We need the packet header field's content to know
682 * the ID of the stream class to select.
683 */
684 notit->packet_header_field = bt_packet_header_field_create(
685 notit->meta.tc->ir_tc);
686 if (!notit->packet_header_field) {
687 BT_LOGE_STR("Cannot create packet header field wrapper from trace.");
688 ret = BT_NOTIF_ITER_STATUS_ERROR;
689 goto end;
690 }
691
692 notit->dscopes.trace_packet_header =
693 bt_packet_header_field_borrow_field(notit->packet_header_field);
694 BT_ASSERT(notit->dscopes.trace_packet_header);
695 }
696
697 notit->cur_stream_class_id = -1;
698 notit->cur_event_class_id = -1;
699 notit->cur_data_stream_id = -1;
700 BT_LOGV("Decoding packet header field:"
701 "notit-addr=%p, trace-addr=%p, trace-name=\"%s\", ft-addr=%p",
702 notit, notit->meta.tc,
703 notit->meta.tc->name->str, packet_header_ft);
704 ret = read_dscope_begin_state(notit, packet_header_ft,
705 STATE_AFTER_TRACE_PACKET_HEADER,
706 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE,
707 notit->dscopes.trace_packet_header);
708 if (ret < 0) {
709 BT_LOGW("Cannot decode packet header field: "
710 "notit-addr=%p, trace-addr=%p, "
711 "trace-name=\"%s\", ft-addr=%p",
712 notit, notit->meta.tc,
713 notit->meta.tc->name->str,
714 packet_header_ft);
715 }
716
717 end:
718 return ret;
719 }
720
721 static
722 enum bt_notif_iter_status read_packet_header_continue_state(
723 struct bt_notif_iter *notit)
724 {
725 return read_dscope_continue_state(notit,
726 STATE_AFTER_TRACE_PACKET_HEADER);
727 }
728
729 static inline
730 enum bt_notif_iter_status set_current_stream_class(struct bt_notif_iter *notit)
731 {
732 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
733 struct ctf_stream_class *new_stream_class = NULL;
734
735 if (notit->cur_stream_class_id == -1) {
736 /*
737 * No current stream class ID field, therefore only one
738 * stream class.
739 */
740 if (notit->meta.tc->stream_classes->len != 1) {
741 BT_LOGW("Need exactly one stream class since there's "
742 "no stream class ID field: "
743 "notit-addr=%p, trace-name=\"%s\"",
744 notit, notit->meta.tc->name->str);
745 status = BT_NOTIF_ITER_STATUS_ERROR;
746 goto end;
747 }
748
749 new_stream_class = notit->meta.tc->stream_classes->pdata[0];
750 notit->cur_stream_class_id = new_stream_class->id;
751 goto end;
752 }
753
754 new_stream_class = ctf_trace_class_borrow_stream_class_by_id(
755 notit->meta.tc, notit->cur_stream_class_id);
756 if (!new_stream_class) {
757 BT_LOGW("No stream class with ID of stream class ID to use in trace: "
758 "notit-addr=%p, stream-class-id=%" PRIu64 ", "
759 "trace-addr=%p, trace-name=\"%s\"",
760 notit, notit->cur_stream_class_id, notit->meta.tc,
761 notit->meta.tc->name->str);
762 status = BT_NOTIF_ITER_STATUS_ERROR;
763 goto end;
764 }
765
766 if (notit->meta.sc) {
767 if (new_stream_class != notit->meta.sc) {
768 BT_LOGW("Two packets refer to two different stream classes within the same packet sequence: "
769 "notit-addr=%p, prev-stream-class-addr=%p, "
770 "prev-stream-class-id=%" PRId64 ", "
771 "next-stream-class-addr=%p, "
772 "next-stream-class-id=%" PRId64 ", "
773 "trace-addr=%p, trace-name=\"%s\"",
774 notit, notit->meta.sc,
775 notit->meta.sc->id,
776 new_stream_class,
777 new_stream_class->id,
778 notit->meta.tc,
779 notit->meta.tc->name->str);
780 status = BT_NOTIF_ITER_STATUS_ERROR;
781 goto end;
782 }
783 } else {
784 notit->meta.sc = new_stream_class;
785 }
786
787 BT_LOGV("Set current stream class: "
788 "notit-addr=%p, stream-class-addr=%p, "
789 "stream-class-id=%" PRId64,
790 notit, notit->meta.sc, notit->meta.sc->id);
791
792 end:
793 return status;
794 }
795
796 static inline
797 enum bt_notif_iter_status set_current_stream(struct bt_notif_iter *notit)
798 {
799 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
800 struct bt_stream *stream = NULL;
801
802 BT_LOGV("Calling user function (get stream): notit-addr=%p, "
803 "stream-class-addr=%p, stream-class-id=%" PRId64,
804 notit, notit->meta.sc,
805 notit->meta.sc->id);
806 stream = bt_get(notit->medium.medops.borrow_stream(
807 notit->meta.sc->ir_sc, notit->cur_data_stream_id,
808 notit->medium.data));
809 BT_LOGV("User function returned: stream-addr=%p", stream);
810 if (!stream) {
811 BT_LOGW_STR("User function failed to return a stream object "
812 "for the given stream class.");
813 status = BT_NOTIF_ITER_STATUS_ERROR;
814 goto end;
815 }
816
817 if (notit->stream && stream != notit->stream) {
818 BT_LOGW("User function returned a different stream than the "
819 "previous one for the same sequence of packets.");
820 status = BT_NOTIF_ITER_STATUS_ERROR;
821 goto end;
822 }
823
824 BT_MOVE(notit->stream, stream);
825
826 end:
827 bt_put(stream);
828 return status;
829 }
830
831 static inline
832 enum bt_notif_iter_status set_current_packet(struct bt_notif_iter *notit)
833 {
834 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
835 struct bt_packet *packet = NULL;
836
837 BT_LOGV("Creating packet for packet notification: "
838 "notit-addr=%p", notit);
839 BT_LOGV("Creating packet from stream: "
840 "notit-addr=%p, stream-addr=%p, "
841 "stream-class-addr=%p, "
842 "stream-class-id=%" PRId64,
843 notit, notit->stream, notit->meta.sc,
844 notit->meta.sc->id);
845
846 /* Create packet */
847 BT_ASSERT(notit->stream);
848 packet = bt_packet_create(notit->stream);
849 if (!packet) {
850 BT_LOGE("Cannot create packet from stream: "
851 "notit-addr=%p, stream-addr=%p, "
852 "stream-class-addr=%p, "
853 "stream-class-id=%" PRId64,
854 notit, notit->stream, notit->meta.sc,
855 notit->meta.sc->id);
856 goto error;
857 }
858
859 goto end;
860
861 error:
862 BT_PUT(packet);
863 status = BT_NOTIF_ITER_STATUS_ERROR;
864
865 end:
866 BT_MOVE(notit->packet, packet);
867 return status;
868 }
869
870 static
871 enum bt_notif_iter_status after_packet_header_state(
872 struct bt_notif_iter *notit)
873 {
874 enum bt_notif_iter_status status;
875
876 status = set_current_stream_class(notit);
877 if (status != BT_NOTIF_ITER_STATUS_OK) {
878 goto end;
879 }
880
881 notit->state = STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN;
882
883 end:
884 return status;
885 }
886
887 static
888 enum bt_notif_iter_status read_packet_context_begin_state(
889 struct bt_notif_iter *notit)
890 {
891 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
892 struct ctf_field_type *packet_context_ft;
893
894 BT_ASSERT(notit->meta.sc);
895 packet_context_ft = notit->meta.sc->packet_context_ft;
896 if (!packet_context_ft) {
897 BT_LOGV("No packet packet context field type in stream class: continuing: "
898 "notit-addr=%p, stream-class-addr=%p, "
899 "stream-class-id=%" PRId64,
900 notit, notit->meta.sc,
901 notit->meta.sc->id);
902 notit->state = STATE_AFTER_STREAM_PACKET_CONTEXT;
903 goto end;
904 }
905
906 BT_ASSERT(!notit->packet_context_field);
907
908 if (packet_context_ft->in_ir) {
909 /*
910 * Create free packet context field from stream class.
911 * This field is going to be moved to the packet once we
912 * create it. We cannot create the packet now because a
913 * packet is created from a stream, and this API must be
914 * able to return the packet header and context fields
915 * without creating a stream
916 * (bt_notif_iter_borrow_packet_header_context_fields()).
917 */
918 notit->packet_context_field =
919 bt_packet_context_field_create(notit->meta.sc->ir_sc);
920 if (!notit->packet_context_field) {
921 BT_LOGE_STR("Cannot create packet context field wrapper from stream class.");
922 status = BT_NOTIF_ITER_STATUS_ERROR;
923 goto end;
924 }
925
926 notit->dscopes.stream_packet_context =
927 bt_packet_context_field_borrow_field(notit->packet_context_field);
928 BT_ASSERT(notit->dscopes.stream_packet_context);
929 }
930
931 BT_LOGV("Decoding packet context field: "
932 "notit-addr=%p, stream-class-addr=%p, "
933 "stream-class-id=%" PRId64 ", ft-addr=%p",
934 notit, notit->meta.sc,
935 notit->meta.sc->id, packet_context_ft);
936 status = read_dscope_begin_state(notit, packet_context_ft,
937 STATE_AFTER_STREAM_PACKET_CONTEXT,
938 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE,
939 notit->dscopes.stream_packet_context);
940 if (status < 0) {
941 BT_LOGW("Cannot decode packet context field: "
942 "notit-addr=%p, stream-class-addr=%p, "
943 "stream-class-id=%" PRId64 ", ft-addr=%p",
944 notit, notit->meta.sc,
945 notit->meta.sc->id,
946 packet_context_ft);
947 }
948
949 end:
950 return status;
951 }
952
953 static
954 enum bt_notif_iter_status read_packet_context_continue_state(
955 struct bt_notif_iter *notit)
956 {
957 return read_dscope_continue_state(notit,
958 STATE_AFTER_STREAM_PACKET_CONTEXT);
959 }
960
961 static
962 enum bt_notif_iter_status set_current_packet_content_sizes(
963 struct bt_notif_iter *notit)
964 {
965 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
966
967 if (notit->cur_exp_packet_total_size == -1) {
968 if (notit->cur_exp_packet_content_size != -1) {
969 BT_LOGW("Content size is set, but packet size is not: "
970 "notit-addr=%p, packet-context-field-addr=%p, "
971 "packet-size=%" PRId64 ", content-size=%" PRId64,
972 notit, notit->dscopes.stream_packet_context,
973 notit->cur_exp_packet_total_size,
974 notit->cur_exp_packet_content_size);
975 status = BT_NOTIF_ITER_STATUS_ERROR;
976 goto end;
977 }
978 } else {
979 if (notit->cur_exp_packet_content_size == -1) {
980 notit->cur_exp_packet_content_size =
981 notit->cur_exp_packet_total_size;
982 }
983 }
984
985 if (notit->cur_exp_packet_content_size >
986 notit->cur_exp_packet_total_size) {
987 BT_LOGW("Invalid packet or content size: "
988 "content size is greater than packet size: "
989 "notit-addr=%p, packet-context-field-addr=%p, "
990 "packet-size=%" PRId64 ", content-size=%" PRId64,
991 notit, notit->dscopes.stream_packet_context,
992 notit->cur_exp_packet_total_size,
993 notit->cur_exp_packet_content_size);
994 status = BT_NOTIF_ITER_STATUS_ERROR;
995 goto end;
996 }
997
998 BT_LOGV("Set current packet and content sizes: "
999 "notit-addr=%p, packet-size=%" PRIu64 ", content-size=%" PRIu64,
1000 notit, notit->cur_exp_packet_total_size,
1001 notit->cur_exp_packet_content_size);
1002 end:
1003 return status;
1004 }
1005
1006 static
1007 enum bt_notif_iter_status after_packet_context_state(
1008 struct bt_notif_iter *notit)
1009 {
1010 enum bt_notif_iter_status status;
1011
1012 status = set_current_packet_content_sizes(notit);
1013 if (status != BT_NOTIF_ITER_STATUS_OK) {
1014 goto end;
1015 }
1016
1017 if (notit->stream_begin_emitted) {
1018 notit->state = STATE_EMIT_NOTIF_NEW_PACKET;
1019 } else {
1020 notit->state = STATE_EMIT_NOTIF_NEW_STREAM;
1021 }
1022
1023 end:
1024 return status;
1025 }
1026
1027 static
1028 enum bt_notif_iter_status read_event_header_begin_state(
1029 struct bt_notif_iter *notit)
1030 {
1031 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1032 struct ctf_field_type *event_header_ft = NULL;
1033
1034 /* Reset the position of the last event header */
1035 notit->buf.last_eh_at = notit->buf.at;
1036 notit->cur_event_class_id = -1;
1037
1038 /* Check if we have some content left */
1039 if (notit->cur_exp_packet_content_size >= 0) {
1040 if (unlikely(packet_at(notit) ==
1041 notit->cur_exp_packet_content_size)) {
1042 /* No more events! */
1043 BT_LOGV("Reached end of packet: notit-addr=%p, "
1044 "cur=%zu", notit, packet_at(notit));
1045 notit->state = STATE_EMIT_NOTIF_END_OF_PACKET;
1046 goto end;
1047 } else if (unlikely(packet_at(notit) >
1048 notit->cur_exp_packet_content_size)) {
1049 /* That's not supposed to happen */
1050 BT_LOGV("Before decoding event header field: cursor is passed the packet's content: "
1051 "notit-addr=%p, content-size=%" PRId64 ", "
1052 "cur=%zu", notit,
1053 notit->cur_exp_packet_content_size,
1054 packet_at(notit));
1055 status = BT_NOTIF_ITER_STATUS_ERROR;
1056 goto end;
1057 }
1058 } else {
1059 /*
1060 * "Infinite" content: we're done when the medium has
1061 * nothing else for us.
1062 */
1063 status = buf_ensure_available_bits(notit);
1064 if (status != BT_NOTIF_ITER_STATUS_OK) {
1065 /*
1066 * If this function returns
1067 * `BT_NOTIF_ITER_STATUS_EOF`:
1068 *
1069 * 1. bt_notif_iter_get_next_notification()
1070 * emits a "packet end" notification. This
1071 * resets the current packet. The state
1072 * remains unchanged otherwise.
1073 * 2. This function is called again. It returns
1074 * `BT_NOTIF_ITER_STATUS_EOF` again.
1075 * 3. bt_notif_iter_get_next_notification()
1076 * emits a "stream end" notification because
1077 * there's no current packet. It sets the
1078 * current state to `STATE_DONE`.
1079 */
1080 goto end;
1081 }
1082 }
1083
1084 release_event_dscopes(notit);
1085 BT_ASSERT(notit->meta.sc);
1086 event_header_ft = notit->meta.sc->event_header_ft;
1087 if (!event_header_ft) {
1088 notit->state = STATE_AFTER_EVENT_HEADER;
1089 goto end;
1090 }
1091
1092 if (event_header_ft->in_ir) {
1093 BT_ASSERT(!notit->event_header_field);
1094 notit->event_header_field = bt_event_header_field_create(
1095 notit->meta.sc->ir_sc);
1096 if (!notit->event_header_field) {
1097 BT_LOGE_STR("Cannot create event header field wrapper from trace.");
1098 status = BT_NOTIF_ITER_STATUS_ERROR;
1099 goto end;
1100 }
1101
1102 notit->dscopes.event_header =
1103 bt_event_header_field_borrow_field(notit->event_header_field);
1104 BT_ASSERT(notit->dscopes.event_header);
1105 }
1106
1107 BT_LOGV("Decoding event header field: "
1108 "notit-addr=%p, stream-class-addr=%p, "
1109 "stream-class-id=%" PRId64 ", "
1110 "ft-addr=%p",
1111 notit, notit->meta.sc,
1112 notit->meta.sc->id,
1113 event_header_ft);
1114 status = read_dscope_begin_state(notit, event_header_ft,
1115 STATE_AFTER_EVENT_HEADER,
1116 STATE_DSCOPE_EVENT_HEADER_CONTINUE,
1117 notit->dscopes.event_header);
1118 if (status < 0) {
1119 BT_LOGW("Cannot decode event header field: "
1120 "notit-addr=%p, stream-class-addr=%p, "
1121 "stream-class-id=%" PRId64 ", ft-addr=%p",
1122 notit, notit->meta.sc,
1123 notit->meta.sc->id,
1124 event_header_ft);
1125 }
1126
1127 end:
1128 return status;
1129 }
1130
1131 static
1132 enum bt_notif_iter_status read_event_header_continue_state(
1133 struct bt_notif_iter *notit)
1134 {
1135 return read_dscope_continue_state(notit,
1136 STATE_AFTER_EVENT_HEADER);
1137 }
1138
1139 static inline
1140 enum bt_notif_iter_status set_current_event_class(struct bt_notif_iter *notit)
1141 {
1142 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1143
1144 struct ctf_event_class *new_event_class = NULL;
1145
1146 if (notit->cur_event_class_id == -1) {
1147 /*
1148 * No current event class ID field, therefore only one
1149 * event class.
1150 */
1151 if (notit->meta.sc->event_classes->len != 1) {
1152 BT_LOGW("Need exactly one event class since there's "
1153 "no event class ID field: "
1154 "notit-addr=%p, trace-name=\"%s\"",
1155 notit, notit->meta.tc->name->str);
1156 status = BT_NOTIF_ITER_STATUS_ERROR;
1157 goto end;
1158 }
1159
1160 new_event_class = notit->meta.sc->event_classes->pdata[0];
1161 notit->cur_event_class_id = new_event_class->id;
1162 goto end;
1163 }
1164
1165 new_event_class = ctf_stream_class_borrow_event_class_by_id(
1166 notit->meta.sc, notit->cur_event_class_id);
1167 if (!new_event_class) {
1168 BT_LOGW("No event class with ID of event class ID to use in stream class: "
1169 "notit-addr=%p, stream-class-id=%" PRIu64 ", "
1170 "event-class-id=%" PRIu64 ", "
1171 "trace-addr=%p, trace-name=\"%s\"",
1172 notit, notit->meta.sc->id, notit->cur_event_class_id,
1173 notit->meta.tc, notit->meta.tc->name->str);
1174 status = BT_NOTIF_ITER_STATUS_ERROR;
1175 goto end;
1176 }
1177
1178 notit->meta.ec = new_event_class;
1179 BT_LOGV("Set current event class: "
1180 "notit-addr=%p, event-class-addr=%p, "
1181 "event-class-id=%" PRId64 ", "
1182 "event-class-name=\"%s\"",
1183 notit, notit->meta.ec, notit->meta.ec->id,
1184 notit->meta.ec->name->str);
1185
1186 end:
1187 return status;
1188 }
1189
1190 static inline
1191 enum bt_notif_iter_status set_current_event_notification(
1192 struct bt_notif_iter *notit)
1193 {
1194 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1195 struct bt_notification *notif = NULL;
1196
1197 BT_ASSERT(notit->meta.ec);
1198 BT_ASSERT(notit->packet);
1199 BT_LOGV("Creating event notification from event class and packet: "
1200 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", packet-addr=%p",
1201 notit, notit->meta.ec,
1202 notit->meta.ec->name->str,
1203 notit->packet);
1204 BT_ASSERT(notit->notif_iter);
1205 notif = bt_notification_event_create(notit->notif_iter,
1206 notit->meta.ec->ir_ec, notit->packet);
1207 if (!notif) {
1208 BT_LOGE("Cannot create event notification: "
1209 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", "
1210 "packet-addr=%p",
1211 notit, notit->meta.ec,
1212 notit->meta.ec->name->str,
1213 notit->packet);
1214 goto error;
1215 }
1216
1217 goto end;
1218
1219 error:
1220 BT_PUT(notif);
1221 status = BT_NOTIF_ITER_STATUS_ERROR;
1222
1223 end:
1224 BT_MOVE(notit->event_notif, notif);
1225 return status;
1226 }
1227
1228 static
1229 enum bt_notif_iter_status after_event_header_state(
1230 struct bt_notif_iter *notit)
1231 {
1232 enum bt_notif_iter_status status;
1233
1234 status = set_current_event_class(notit);
1235 if (status != BT_NOTIF_ITER_STATUS_OK) {
1236 goto end;
1237 }
1238
1239 status = set_current_event_notification(notit);
1240 if (status != BT_NOTIF_ITER_STATUS_OK) {
1241 goto end;
1242 }
1243
1244 notit->event = bt_notification_event_borrow_event(notit->event_notif);
1245 BT_ASSERT(notit->event);
1246
1247 if (notit->event_header_field) {
1248 int ret;
1249
1250 BT_ASSERT(notit->event);
1251 ret = bt_event_move_header(notit->event,
1252 notit->event_header_field);
1253 if (ret) {
1254 status = BT_NOTIF_ITER_STATUS_ERROR;
1255 goto end;
1256 }
1257
1258 notit->event_header_field = NULL;
1259
1260 /*
1261 * At this point notit->dscopes.event_header has
1262 * the same value as the event header field within
1263 * notit->event.
1264 */
1265 BT_ASSERT(bt_event_borrow_header_field(notit->event) ==
1266 notit->dscopes.event_header);
1267 }
1268
1269 notit->state = STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN;
1270
1271 end:
1272 return status;
1273 }
1274
1275 static
1276 enum bt_notif_iter_status read_event_common_context_begin_state(
1277 struct bt_notif_iter *notit)
1278 {
1279 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1280 struct ctf_field_type *event_common_context_ft;
1281
1282 event_common_context_ft = notit->meta.sc->event_common_context_ft;
1283 if (!event_common_context_ft) {
1284 notit->state = STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN;
1285 goto end;
1286 }
1287
1288 if (event_common_context_ft->in_ir) {
1289 BT_ASSERT(!notit->dscopes.event_common_context);
1290 notit->dscopes.event_common_context =
1291 bt_event_borrow_common_context_field(notit->event);
1292 BT_ASSERT(notit->dscopes.event_common_context);
1293 }
1294
1295 BT_LOGV("Decoding event common context field: "
1296 "notit-addr=%p, stream-class-addr=%p, "
1297 "stream-class-id=%" PRId64 ", "
1298 "ft-addr=%p",
1299 notit, notit->meta.sc,
1300 notit->meta.sc->id,
1301 event_common_context_ft);
1302 status = read_dscope_begin_state(notit, event_common_context_ft,
1303 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN,
1304 STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE,
1305 notit->dscopes.event_common_context);
1306 if (status < 0) {
1307 BT_LOGW("Cannot decode event common context field: "
1308 "notit-addr=%p, stream-class-addr=%p, "
1309 "stream-class-id=%" PRId64 ", ft-addr=%p",
1310 notit, notit->meta.sc,
1311 notit->meta.sc->id,
1312 event_common_context_ft);
1313 }
1314
1315 end:
1316 return status;
1317 }
1318
1319 static
1320 enum bt_notif_iter_status read_event_common_context_continue_state(
1321 struct bt_notif_iter *notit)
1322 {
1323 return read_dscope_continue_state(notit,
1324 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN);
1325 }
1326
1327 static
1328 enum bt_notif_iter_status read_event_spec_context_begin_state(
1329 struct bt_notif_iter *notit)
1330 {
1331 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1332 struct ctf_field_type *event_spec_context_ft;
1333
1334 event_spec_context_ft = notit->meta.ec->spec_context_ft;
1335 if (!event_spec_context_ft) {
1336 notit->state = STATE_DSCOPE_EVENT_PAYLOAD_BEGIN;
1337 goto end;
1338 }
1339
1340 if (event_spec_context_ft->in_ir) {
1341 BT_ASSERT(!notit->dscopes.event_spec_context);
1342 notit->dscopes.event_spec_context = bt_event_borrow_specific_context_field(
1343 notit->event);
1344 BT_ASSERT(notit->dscopes.event_spec_context);
1345 }
1346
1347 BT_LOGV("Decoding event specific context field: "
1348 "notit-addr=%p, event-class-addr=%p, "
1349 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
1350 "ft-addr=%p",
1351 notit, notit->meta.ec,
1352 notit->meta.ec->name->str,
1353 notit->meta.ec->id,
1354 event_spec_context_ft);
1355 status = read_dscope_begin_state(notit, event_spec_context_ft,
1356 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN,
1357 STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE,
1358 notit->dscopes.event_spec_context);
1359 if (status < 0) {
1360 BT_LOGW("Cannot decode event specific context field: "
1361 "notit-addr=%p, event-class-addr=%p, "
1362 "event-class-name=\"%s\", "
1363 "event-class-id=%" PRId64 ", ft-addr=%p",
1364 notit, notit->meta.ec,
1365 notit->meta.ec->name->str,
1366 notit->meta.ec->id,
1367 event_spec_context_ft);
1368 }
1369
1370 end:
1371 return status;
1372 }
1373
1374 static
1375 enum bt_notif_iter_status read_event_spec_context_continue_state(
1376 struct bt_notif_iter *notit)
1377 {
1378 return read_dscope_continue_state(notit,
1379 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN);
1380 }
1381
1382 static
1383 enum bt_notif_iter_status read_event_payload_begin_state(
1384 struct bt_notif_iter *notit)
1385 {
1386 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1387 struct ctf_field_type *event_payload_ft;
1388
1389 event_payload_ft = notit->meta.ec->payload_ft;
1390 if (!event_payload_ft) {
1391 notit->state = STATE_EMIT_NOTIF_EVENT;
1392 goto end;
1393 }
1394
1395 if (event_payload_ft->in_ir) {
1396 BT_ASSERT(!notit->dscopes.event_payload);
1397 notit->dscopes.event_payload = bt_event_borrow_payload_field(
1398 notit->event);
1399 BT_ASSERT(notit->dscopes.event_payload);
1400 }
1401
1402 BT_LOGV("Decoding event payload field: "
1403 "notit-addr=%p, event-class-addr=%p, "
1404 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
1405 "ft-addr=%p",
1406 notit, notit->meta.ec,
1407 notit->meta.ec->name->str,
1408 notit->meta.ec->id,
1409 event_payload_ft);
1410 status = read_dscope_begin_state(notit, event_payload_ft,
1411 STATE_EMIT_NOTIF_EVENT,
1412 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE,
1413 notit->dscopes.event_payload);
1414 if (status < 0) {
1415 BT_LOGW("Cannot decode event payload field: "
1416 "notit-addr=%p, event-class-addr=%p, "
1417 "event-class-name=\"%s\", "
1418 "event-class-id=%" PRId64 ", ft-addr=%p",
1419 notit, notit->meta.ec,
1420 notit->meta.ec->name->str,
1421 notit->meta.ec->id,
1422 event_payload_ft);
1423 }
1424
1425 end:
1426 return status;
1427 }
1428
1429 static
1430 enum bt_notif_iter_status read_event_payload_continue_state(
1431 struct bt_notif_iter *notit)
1432 {
1433 return read_dscope_continue_state(notit, STATE_EMIT_NOTIF_EVENT);
1434 }
1435
1436 static
1437 enum bt_notif_iter_status skip_packet_padding_state(
1438 struct bt_notif_iter *notit)
1439 {
1440 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1441 size_t bits_to_skip;
1442
1443 BT_ASSERT(notit->cur_exp_packet_total_size > 0);
1444 bits_to_skip = notit->cur_exp_packet_total_size - packet_at(notit);
1445 if (bits_to_skip == 0) {
1446 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1447 goto end;
1448 } else {
1449 size_t bits_to_consume;
1450
1451 BT_LOGV("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu",
1452 bits_to_skip, notit, bits_to_skip);
1453 status = buf_ensure_available_bits(notit);
1454 if (status != BT_NOTIF_ITER_STATUS_OK) {
1455 goto end;
1456 }
1457
1458 bits_to_consume = MIN(buf_available_bits(notit), bits_to_skip);
1459 BT_LOGV("Skipping %zu bits of padding: notit-addr=%p, size=%zu",
1460 bits_to_consume, notit, bits_to_consume);
1461 buf_consume_bits(notit, bits_to_consume);
1462 bits_to_skip = notit->cur_exp_packet_total_size -
1463 packet_at(notit);
1464 if (bits_to_skip == 0) {
1465 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1466 goto end;
1467 }
1468 }
1469
1470 end:
1471 return status;
1472 }
1473
1474 static inline
1475 enum bt_notif_iter_status handle_state(struct bt_notif_iter *notit)
1476 {
1477 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1478 const enum state state = notit->state;
1479
1480 BT_LOGV("Handling state: notit-addr=%p, state=%s",
1481 notit, state_string(state));
1482
1483 // TODO: optimalize!
1484 switch (state) {
1485 case STATE_INIT:
1486 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1487 break;
1488 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
1489 status = read_packet_header_begin_state(notit);
1490 break;
1491 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
1492 status = read_packet_header_continue_state(notit);
1493 break;
1494 case STATE_AFTER_TRACE_PACKET_HEADER:
1495 status = after_packet_header_state(notit);
1496 break;
1497 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN:
1498 status = read_packet_context_begin_state(notit);
1499 break;
1500 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE:
1501 status = read_packet_context_continue_state(notit);
1502 break;
1503 case STATE_AFTER_STREAM_PACKET_CONTEXT:
1504 status = after_packet_context_state(notit);
1505 break;
1506 case STATE_EMIT_NOTIF_NEW_STREAM:
1507 notit->state = STATE_EMIT_NOTIF_NEW_PACKET;
1508 break;
1509 case STATE_EMIT_NOTIF_NEW_PACKET:
1510 notit->state = STATE_DSCOPE_EVENT_HEADER_BEGIN;
1511 break;
1512 case STATE_DSCOPE_EVENT_HEADER_BEGIN:
1513 status = read_event_header_begin_state(notit);
1514 break;
1515 case STATE_DSCOPE_EVENT_HEADER_CONTINUE:
1516 status = read_event_header_continue_state(notit);
1517 break;
1518 case STATE_AFTER_EVENT_HEADER:
1519 status = after_event_header_state(notit);
1520 break;
1521 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN:
1522 status = read_event_common_context_begin_state(notit);
1523 break;
1524 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE:
1525 status = read_event_common_context_continue_state(notit);
1526 break;
1527 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN:
1528 status = read_event_spec_context_begin_state(notit);
1529 break;
1530 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE:
1531 status = read_event_spec_context_continue_state(notit);
1532 break;
1533 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN:
1534 status = read_event_payload_begin_state(notit);
1535 break;
1536 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE:
1537 status = read_event_payload_continue_state(notit);
1538 break;
1539 case STATE_EMIT_NOTIF_EVENT:
1540 notit->state = STATE_DSCOPE_EVENT_HEADER_BEGIN;
1541 break;
1542 case STATE_SKIP_PACKET_PADDING:
1543 status = skip_packet_padding_state(notit);
1544 break;
1545 case STATE_EMIT_NOTIF_END_OF_PACKET:
1546 notit->state = STATE_SKIP_PACKET_PADDING;
1547 break;
1548 default:
1549 BT_LOGD("Unknown CTF plugin notification iterator state: "
1550 "notit-addr=%p, state=%d", notit, notit->state);
1551 abort();
1552 }
1553
1554 BT_LOGV("Handled state: notit-addr=%p, status=%s, "
1555 "prev-state=%s, cur-state=%s",
1556 notit, bt_notif_iter_status_string(status),
1557 state_string(state), state_string(notit->state));
1558 return status;
1559 }
1560
1561 /**
1562 * Resets the internal state of a CTF notification iterator.
1563 */
1564 BT_HIDDEN
1565 void bt_notif_iter_reset(struct bt_notif_iter *notit)
1566 {
1567 BT_ASSERT(notit);
1568 BT_LOGD("Resetting notification iterator: addr=%p", notit);
1569 stack_clear(notit->stack);
1570 notit->meta.sc = NULL;
1571 notit->meta.ec = NULL;
1572 BT_PUT(notit->packet);
1573 BT_PUT(notit->stream);
1574 BT_PUT(notit->event_notif);
1575 release_all_dscopes(notit);
1576 notit->cur_dscope_field = NULL;
1577
1578 if (notit->packet_header_field) {
1579 bt_packet_header_field_release(notit->packet_header_field);
1580 notit->packet_header_field = NULL;
1581 }
1582
1583 if (notit->packet_context_field) {
1584 bt_packet_context_field_release(notit->packet_context_field);
1585 notit->packet_context_field = NULL;
1586 }
1587
1588 if (notit->event_header_field) {
1589 bt_event_header_field_release(notit->event_header_field);
1590 notit->event_header_field = NULL;
1591 }
1592
1593 notit->buf.addr = NULL;
1594 notit->buf.sz = 0;
1595 notit->buf.at = 0;
1596 notit->buf.last_eh_at = SIZE_MAX;
1597 notit->buf.packet_offset = 0;
1598 notit->state = STATE_INIT;
1599 notit->cur_exp_packet_content_size = -1;
1600 notit->cur_exp_packet_total_size = -1;
1601 notit->cur_packet_offset = -1;
1602 notit->cur_stream_class_id = -1;
1603 notit->cur_event_class_id = -1;
1604 notit->cur_data_stream_id = -1;
1605 notit->stream_begin_emitted = false;
1606 }
1607
1608 static
1609 int bt_notif_iter_switch_packet(struct bt_notif_iter *notit)
1610 {
1611 int ret = 0;
1612
1613 /*
1614 * We don't put the stream class here because we need to make
1615 * sure that all the packets processed by the same notification
1616 * iterator refer to the same stream class (the first one).
1617 */
1618 BT_ASSERT(notit);
1619
1620 if (notit->cur_exp_packet_total_size != -1) {
1621 notit->cur_packet_offset += notit->cur_exp_packet_total_size;
1622 }
1623
1624 BT_LOGV("Switching packet: notit-addr=%p, cur=%zu, "
1625 "packet-offset=%" PRId64, notit, notit->buf.at,
1626 notit->cur_packet_offset);
1627 stack_clear(notit->stack);
1628 notit->meta.ec = NULL;
1629 BT_PUT(notit->packet);
1630 BT_PUT(notit->event_notif);
1631 release_all_dscopes(notit);
1632 notit->cur_dscope_field = NULL;
1633
1634 /*
1635 * Adjust current buffer so that addr points to the beginning of the new
1636 * packet.
1637 */
1638 if (notit->buf.addr) {
1639 size_t consumed_bytes = (size_t) (notit->buf.at / CHAR_BIT);
1640
1641 /* Packets are assumed to start on a byte frontier. */
1642 if (notit->buf.at % CHAR_BIT) {
1643 BT_LOGW("Cannot switch packet: current position is not a multiple of 8: "
1644 "notit-addr=%p, cur=%zu", notit, notit->buf.at);
1645 ret = -1;
1646 goto end;
1647 }
1648
1649 notit->buf.addr += consumed_bytes;
1650 notit->buf.sz -= consumed_bytes;
1651 notit->buf.at = 0;
1652 notit->buf.packet_offset = 0;
1653 BT_LOGV("Adjusted buffer: addr=%p, size=%zu",
1654 notit->buf.addr, notit->buf.sz);
1655 }
1656
1657 notit->cur_exp_packet_content_size = -1;
1658 notit->cur_exp_packet_total_size = -1;
1659 notit->cur_stream_class_id = -1;
1660 notit->cur_event_class_id = -1;
1661 notit->cur_data_stream_id = -1;
1662 notit->snapshots.discarded_events = UINT64_C(-1);
1663 notit->snapshots.packets = UINT64_C(-1);
1664 notit->snapshots.beginning_clock = UINT64_C(-1);
1665 notit->snapshots.end_clock = UINT64_C(-1);
1666
1667 end:
1668 return ret;
1669 }
1670
1671 static
1672 struct bt_field *borrow_next_field(struct bt_notif_iter *notit)
1673 {
1674 struct bt_field *next_field = NULL;
1675 struct bt_field *base_field;
1676 struct bt_field_type *base_ft;
1677 size_t index;
1678
1679 BT_ASSERT(!stack_empty(notit->stack));
1680 index = stack_top(notit->stack)->index;
1681 base_field = stack_top(notit->stack)->base;
1682 BT_ASSERT(base_field);
1683 base_ft = bt_field_borrow_type(base_field);
1684 BT_ASSERT(base_ft);
1685
1686 switch (bt_field_type_get_type_id(base_ft)) {
1687 case BT_FIELD_TYPE_ID_STRUCTURE:
1688 {
1689 BT_ASSERT(index <
1690 bt_field_type_structure_get_member_count(
1691 bt_field_borrow_type(base_field)));
1692 next_field = bt_field_structure_borrow_member_field_by_index(
1693 base_field, index);
1694 break;
1695 }
1696 case BT_FIELD_TYPE_ID_STATIC_ARRAY:
1697 case BT_FIELD_TYPE_ID_DYNAMIC_ARRAY:
1698 BT_ASSERT(index < bt_field_array_get_length(base_field));
1699 next_field = bt_field_array_borrow_element_field_by_index(
1700 base_field, index);
1701 break;
1702 case BT_FIELD_TYPE_ID_VARIANT:
1703 BT_ASSERT(index == 0);
1704 next_field = bt_field_variant_borrow_selected_option_field(
1705 base_field);
1706 break;
1707 default:
1708 abort();
1709 }
1710
1711 BT_ASSERT(next_field);
1712 return next_field;
1713 }
1714
1715 static
1716 void update_default_clock(struct bt_notif_iter *notit, uint64_t new_val,
1717 uint64_t new_val_size)
1718 {
1719 uint64_t new_val_mask;
1720 uint64_t cur_value_masked;
1721
1722 BT_ASSERT(new_val_size > 0);
1723
1724 /*
1725 * Special case for a 64-bit new value, which is the limit
1726 * of a clock value as of this version: overwrite the
1727 * current value directly.
1728 */
1729 if (new_val_size == 64) {
1730 notit->default_clock_val = new_val;
1731 goto end;
1732 }
1733
1734 new_val_mask = (1ULL << new_val_size) - 1;
1735 cur_value_masked = notit->default_clock_val & new_val_mask;
1736
1737 if (new_val < cur_value_masked) {
1738 /*
1739 * It looks like a wrap happened on the number of bits
1740 * of the requested new value. Assume that the clock
1741 * value wrapped only one time.
1742 */
1743 notit->default_clock_val += new_val_mask + 1;
1744 }
1745
1746 /* Clear the low bits of the current clock value. */
1747 notit->default_clock_val &= ~new_val_mask;
1748
1749 /* Set the low bits of the current clock value. */
1750 notit->default_clock_val |= new_val;
1751
1752 end:
1753 BT_LOGV("Updated default clock's value from integer field's value: "
1754 "value=%" PRIu64, notit->default_clock_val);
1755 }
1756
1757 static
1758 enum bt_btr_status btr_unsigned_int_cb(uint64_t value,
1759 struct ctf_field_type *ft, void *data)
1760 {
1761 struct bt_notif_iter *notit = data;
1762 enum bt_btr_status status = BT_BTR_STATUS_OK;
1763 struct bt_field *field = NULL;
1764 struct ctf_field_type_int *int_ft = (void *) ft;
1765
1766 BT_LOGV("Unsigned integer function called from BTR: "
1767 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
1768 "ft-id=%d, ft-in-ir=%d, value=%" PRIu64,
1769 notit, notit->btr, ft, ft->id, ft->in_ir, value);
1770
1771 if (likely(int_ft->meaning == CTF_FIELD_TYPE_MEANING_NONE)) {
1772 goto update_def_clock;
1773 }
1774
1775 switch (int_ft->meaning) {
1776 case CTF_FIELD_TYPE_MEANING_EVENT_CLASS_ID:
1777 notit->cur_event_class_id = value;
1778 break;
1779 case CTF_FIELD_TYPE_MEANING_DATA_STREAM_ID:
1780 notit->cur_data_stream_id = value;
1781 break;
1782 case CTF_FIELD_TYPE_MEANING_PACKET_BEGINNING_TIME:
1783 notit->snapshots.beginning_clock = value;
1784 break;
1785 case CTF_FIELD_TYPE_MEANING_PACKET_END_TIME:
1786 notit->snapshots.end_clock = value;
1787 break;
1788 case CTF_FIELD_TYPE_MEANING_STREAM_CLASS_ID:
1789 notit->cur_stream_class_id = value;
1790 break;
1791 case CTF_FIELD_TYPE_MEANING_MAGIC:
1792 if (value != 0xc1fc1fc1) {
1793 BT_LOGW("Invalid CTF magic number: notit-addr=%p, "
1794 "magic=%" PRIx64, notit, value);
1795 status = BT_BTR_STATUS_ERROR;
1796 goto end;
1797 }
1798
1799 break;
1800 case CTF_FIELD_TYPE_MEANING_PACKET_COUNTER_SNAPSHOT:
1801 notit->snapshots.packets = value;
1802 break;
1803 case CTF_FIELD_TYPE_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT:
1804 notit->snapshots.discarded_events = value;
1805 break;
1806 case CTF_FIELD_TYPE_MEANING_EXP_PACKET_TOTAL_SIZE:
1807 notit->cur_exp_packet_total_size = value;
1808 break;
1809 case CTF_FIELD_TYPE_MEANING_EXP_PACKET_CONTENT_SIZE:
1810 notit->cur_exp_packet_content_size = value;
1811 break;
1812 default:
1813 abort();
1814 }
1815
1816 update_def_clock:
1817 if (unlikely(int_ft->mapped_clock_class)) {
1818 update_default_clock(notit, value, int_ft->base.size);
1819 }
1820
1821 if (unlikely(int_ft->storing_index >= 0)) {
1822 g_array_index(notit->stored_values, uint64_t,
1823 (uint64_t) int_ft->storing_index) = value;
1824 }
1825
1826 if (unlikely(!ft->in_ir)) {
1827 goto end;
1828 }
1829
1830 field = borrow_next_field(notit);
1831 BT_ASSERT(field);
1832 BT_ASSERT(bt_field_borrow_type(field) == ft->ir_ft);
1833 BT_ASSERT(bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_UNSIGNED_INTEGER ||
1834 bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_UNSIGNED_ENUMERATION);
1835 bt_field_unsigned_integer_set_value(field, value);
1836 stack_top(notit->stack)->index++;
1837
1838 end:
1839 return status;
1840 }
1841
1842 static
1843 enum bt_btr_status btr_unsigned_int_char_cb(uint64_t value,
1844 struct ctf_field_type *ft, void *data)
1845 {
1846 int ret;
1847 struct bt_notif_iter *notit = data;
1848 enum bt_btr_status status = BT_BTR_STATUS_OK;
1849 struct bt_field *string_field = NULL;
1850 struct ctf_field_type_int *int_ft = (void *) ft;
1851 char str[2] = {'\0', '\0'};
1852
1853 BT_LOGV("Unsigned integer character function called from BTR: "
1854 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
1855 "ft-id=%d, ft-in-ir=%d, value=%" PRIu64,
1856 notit, notit->btr, ft, ft->id, ft->in_ir, value);
1857 BT_ASSERT(int_ft->meaning == CTF_FIELD_TYPE_MEANING_NONE);
1858 BT_ASSERT(!int_ft->mapped_clock_class);
1859 BT_ASSERT(int_ft->storing_index < 0);
1860
1861 if (unlikely(!ft->in_ir)) {
1862 goto end;
1863 }
1864
1865 if (notit->done_filling_string) {
1866 goto end;
1867 }
1868
1869 if (value == 0) {
1870 notit->done_filling_string = true;
1871 goto end;
1872 }
1873
1874 string_field = stack_top(notit->stack)->base;
1875 BT_ASSERT(bt_field_get_type_id(string_field) == BT_FIELD_TYPE_ID_STRING);
1876
1877 /* Append character */
1878 str[0] = (char) value;
1879 ret = bt_field_string_append_with_length(string_field, str, 1);
1880 if (ret) {
1881 BT_LOGE("Cannot append character to string field's value: "
1882 "notit-addr=%p, field-addr=%p, ret=%d",
1883 notit, string_field, ret);
1884 status = BT_BTR_STATUS_ERROR;
1885 goto end;
1886 }
1887
1888 end:
1889 return status;
1890 }
1891
1892 static
1893 enum bt_btr_status btr_signed_int_cb(int64_t value,
1894 struct ctf_field_type *ft, void *data)
1895 {
1896 enum bt_btr_status status = BT_BTR_STATUS_OK;
1897 struct bt_field *field = NULL;
1898 struct bt_notif_iter *notit = data;
1899 struct ctf_field_type_int *int_ft = (void *) ft;
1900
1901 BT_LOGV("Signed integer function called from BTR: "
1902 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
1903 "ft-id=%d, ft-in-ir=%d, value=%" PRId64,
1904 notit, notit->btr, ft, ft->id, ft->in_ir, value);
1905 BT_ASSERT(int_ft->meaning == CTF_FIELD_TYPE_MEANING_NONE);
1906
1907 if (unlikely(int_ft->storing_index >= 0)) {
1908 g_array_index(notit->stored_values, uint64_t,
1909 (uint64_t) int_ft->storing_index) = (uint64_t) value;
1910 }
1911
1912 if (unlikely(!ft->in_ir)) {
1913 goto end;
1914 }
1915
1916 field = borrow_next_field(notit);
1917 BT_ASSERT(field);
1918 BT_ASSERT(bt_field_borrow_type(field) == ft->ir_ft);
1919 BT_ASSERT(bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_SIGNED_INTEGER ||
1920 bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_SIGNED_ENUMERATION);
1921 bt_field_signed_integer_set_value(field, value);
1922 stack_top(notit->stack)->index++;
1923
1924 end:
1925 return status;
1926 }
1927
1928 static
1929 enum bt_btr_status btr_floating_point_cb(double value,
1930 struct ctf_field_type *ft, void *data)
1931 {
1932 enum bt_btr_status status = BT_BTR_STATUS_OK;
1933 struct bt_field *field = NULL;
1934 struct bt_notif_iter *notit = data;
1935
1936 BT_LOGV("Floating point number function called from BTR: "
1937 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
1938 "ft-id=%d, ft-in-ir=%d, value=%f",
1939 notit, notit->btr, ft, ft->id, ft->in_ir, value);
1940 BT_ASSERT(ft->in_ir);
1941 field = borrow_next_field(notit);
1942 BT_ASSERT(field);
1943 BT_ASSERT(bt_field_borrow_type(field) == ft->ir_ft);
1944 BT_ASSERT(bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_REAL);
1945 bt_field_real_set_value(field, value);
1946 stack_top(notit->stack)->index++;
1947 return status;
1948 }
1949
1950 static
1951 enum bt_btr_status btr_string_begin_cb(
1952 struct ctf_field_type *ft, void *data)
1953 {
1954 struct bt_field *field = NULL;
1955 struct bt_notif_iter *notit = data;
1956 int ret;
1957
1958 BT_LOGV("String (beginning) function called from BTR: "
1959 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
1960 "ft-id=%d, ft-in-ir=%d",
1961 notit, notit->btr, ft, ft->id, ft->in_ir);
1962
1963 BT_ASSERT(ft->in_ir);
1964 field = borrow_next_field(notit);
1965 BT_ASSERT(field);
1966 BT_ASSERT(bt_field_borrow_type(field) == ft->ir_ft);
1967 BT_ASSERT(bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_STRING);
1968 ret = bt_field_string_clear(field);
1969 BT_ASSERT(ret == 0);
1970
1971 /*
1972 * Push on stack. Not a compound type per se, but we know that
1973 * only btr_string_cb() may be called between this call and a
1974 * subsequent call to btr_string_end_cb().
1975 */
1976 stack_push(notit->stack, field);
1977 return BT_BTR_STATUS_OK;
1978 }
1979
1980 static
1981 enum bt_btr_status btr_string_cb(const char *value,
1982 size_t len, struct ctf_field_type *ft, void *data)
1983 {
1984 enum bt_btr_status status = BT_BTR_STATUS_OK;
1985 struct bt_field *field = NULL;
1986 struct bt_notif_iter *notit = data;
1987 int ret;
1988
1989 BT_LOGV("String (substring) function called from BTR: "
1990 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
1991 "ft-id=%d, ft-in-ir=%d, string-length=%zu",
1992 notit, notit->btr, ft, ft->id, ft->in_ir,
1993 len);
1994 BT_ASSERT(ft->in_ir);
1995 field = stack_top(notit->stack)->base;
1996 BT_ASSERT(field);
1997
1998 /* Append current substring */
1999 ret = bt_field_string_append_with_length(field, value, len);
2000 if (ret) {
2001 BT_LOGE("Cannot append substring to string field's value: "
2002 "notit-addr=%p, field-addr=%p, string-length=%zu, "
2003 "ret=%d", notit, field, len, ret);
2004 status = BT_BTR_STATUS_ERROR;
2005 goto end;
2006 }
2007
2008 end:
2009 return status;
2010 }
2011
2012 static
2013 enum bt_btr_status btr_string_end_cb(
2014 struct ctf_field_type *ft, void *data)
2015 {
2016 struct bt_notif_iter *notit = data;
2017
2018 BT_LOGV("String (end) function called from BTR: "
2019 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2020 "ft-id=%d, ft-in-ir=%d",
2021 notit, notit->btr, ft, ft->id, ft->in_ir);
2022 BT_ASSERT(ft->in_ir);
2023
2024 /* Pop string field */
2025 stack_pop(notit->stack);
2026
2027 /* Go to next field */
2028 stack_top(notit->stack)->index++;
2029 return BT_BTR_STATUS_OK;
2030 }
2031
2032 enum bt_btr_status btr_compound_begin_cb(
2033 struct ctf_field_type *ft, void *data)
2034 {
2035 struct bt_notif_iter *notit = data;
2036 struct bt_field *field;
2037
2038 BT_LOGV("Compound (beginning) function called from BTR: "
2039 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2040 "ft-id=%d, ft-in-ir=%d",
2041 notit, notit->btr, ft, ft->id, ft->in_ir);
2042
2043 if (!ft->in_ir) {
2044 goto end;
2045 }
2046
2047 /* Borrow field */
2048 if (stack_empty(notit->stack)) {
2049 /* Root: already set by read_dscope_begin_state() */
2050 field = notit->cur_dscope_field;
2051 } else {
2052 field = borrow_next_field(notit);
2053 BT_ASSERT(field);
2054 }
2055
2056 /* Push field */
2057 BT_ASSERT(field);
2058 BT_ASSERT(bt_field_borrow_type(field) == ft->ir_ft);
2059 stack_push(notit->stack, field);
2060
2061 /*
2062 * Change BTR "unsigned int" callback if it's a text
2063 * array/sequence.
2064 */
2065 if (ft->id == CTF_FIELD_TYPE_ID_ARRAY ||
2066 ft->id == CTF_FIELD_TYPE_ID_SEQUENCE) {
2067 struct ctf_field_type_array_base *array_ft = (void *) ft;
2068
2069 if (array_ft->is_text) {
2070 int ret;
2071
2072 BT_ASSERT(bt_field_get_type_id(field) ==
2073 BT_FIELD_TYPE_ID_STRING);
2074 notit->done_filling_string = false;
2075 ret = bt_field_string_clear(field);
2076 BT_ASSERT(ret == 0);
2077 bt_btr_set_unsigned_int_cb(notit->btr,
2078 btr_unsigned_int_char_cb);
2079 }
2080 }
2081
2082 end:
2083 return BT_BTR_STATUS_OK;
2084 }
2085
2086 enum bt_btr_status btr_compound_end_cb(
2087 struct ctf_field_type *ft, void *data)
2088 {
2089 struct bt_notif_iter *notit = data;
2090
2091 BT_LOGV("Compound (end) function called from BTR: "
2092 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2093 "ft-id=%d, ft-in-ir=%d",
2094 notit, notit->btr, ft, ft->id, ft->in_ir);
2095
2096 if (!ft->in_ir) {
2097 goto end;
2098 }
2099
2100 BT_ASSERT(!stack_empty(notit->stack));
2101 BT_ASSERT(bt_field_borrow_type(stack_top(notit->stack)->base) ==
2102 ft->ir_ft);
2103
2104 /*
2105 * Reset BTR "unsigned int" callback if it's a text
2106 * array/sequence.
2107 */
2108 if (ft->id == CTF_FIELD_TYPE_ID_ARRAY ||
2109 ft->id == CTF_FIELD_TYPE_ID_SEQUENCE) {
2110 struct ctf_field_type_array_base *array_ft = (void *) ft;
2111
2112 if (array_ft->is_text) {
2113 BT_ASSERT(bt_field_get_type_id(
2114 stack_top(notit->stack)->base) ==
2115 BT_FIELD_TYPE_ID_STRING);
2116 bt_btr_set_unsigned_int_cb(notit->btr,
2117 btr_unsigned_int_cb);
2118 }
2119 }
2120
2121 /* Pop stack */
2122 stack_pop(notit->stack);
2123
2124 /* If the stack is not empty, increment the base's index */
2125 if (!stack_empty(notit->stack)) {
2126 stack_top(notit->stack)->index++;
2127 }
2128
2129 end:
2130 return BT_BTR_STATUS_OK;
2131 }
2132
2133 static
2134 int64_t btr_get_sequence_length_cb(struct ctf_field_type *ft, void *data)
2135 {
2136 struct bt_field *seq_field;
2137 struct bt_notif_iter *notit = data;
2138 struct ctf_field_type_sequence *seq_ft = (void *) ft;
2139 int64_t length = -1;
2140 int ret;
2141
2142 length = (uint64_t) g_array_index(notit->stored_values, uint64_t,
2143 seq_ft->stored_length_index);
2144 seq_field = stack_top(notit->stack)->base;
2145 BT_ASSERT(seq_field);
2146 ret = bt_field_dynamic_array_set_length(seq_field, (uint64_t) length);
2147 if (ret) {
2148 BT_LOGE("Cannot set dynamic array field's length field: "
2149 "notit-addr=%p, field-addr=%p, "
2150 "length=%" PRIu64, notit, seq_field, length);
2151 }
2152
2153 return length;
2154 }
2155
2156 static
2157 struct ctf_field_type *btr_borrow_variant_selected_field_type_cb(
2158 struct ctf_field_type *ft, void *data)
2159 {
2160 int ret;
2161 uint64_t i;
2162 int64_t option_index = -1;
2163 struct bt_notif_iter *notit = data;
2164 struct ctf_field_type_variant *var_ft = (void *) ft;
2165 struct ctf_named_field_type *selected_option = NULL;
2166 struct ctf_field_type *ret_ft = NULL;
2167 union {
2168 uint64_t u;
2169 int64_t i;
2170 } tag;
2171
2172 /* Get variant's tag */
2173 tag.u = g_array_index(notit->stored_values, uint64_t,
2174 var_ft->stored_tag_index);
2175
2176 /*
2177 * Check each range to find the selected option's index.
2178 */
2179 if (var_ft->tag_ft->base.is_signed) {
2180 for (i = 0; i < var_ft->ranges->len; i++) {
2181 struct ctf_field_type_variant_range *range =
2182 ctf_field_type_variant_borrow_range_by_index(
2183 var_ft, i);
2184
2185 if (tag.i >= range->range.lower.i &&
2186 tag.i <= range->range.upper.i) {
2187 option_index = (int64_t) range->option_index;
2188 break;
2189 }
2190 }
2191 } else {
2192 for (i = 0; i < var_ft->ranges->len; i++) {
2193 struct ctf_field_type_variant_range *range =
2194 ctf_field_type_variant_borrow_range_by_index(
2195 var_ft, i);
2196
2197 if (tag.u >= range->range.lower.u &&
2198 tag.u <= range->range.upper.u) {
2199 option_index = (int64_t) range->option_index;
2200 break;
2201 }
2202 }
2203 }
2204
2205 if (option_index < 0) {
2206 BT_LOGW("Cannot find variant field type's option: "
2207 "notit-addr=%p, var-ft-addr=%p, u-tag=%" PRIu64 ", "
2208 "i-tag=%" PRId64, notit, var_ft, tag.u, tag.i);
2209 goto end;
2210 }
2211
2212 selected_option = ctf_field_type_variant_borrow_option_by_index(
2213 var_ft, (uint64_t) option_index);
2214
2215 if (selected_option->ft->in_ir) {
2216 struct bt_field *var_field = stack_top(notit->stack)->base;
2217
2218 ret = bt_field_variant_select_option_field(var_field,
2219 option_index);
2220 if (ret) {
2221 BT_LOGW("Cannot select variant field's option field: "
2222 "notit-addr=%p, var-field-addr=%p, "
2223 "opt-index=%" PRId64, notit, var_field,
2224 option_index);
2225 goto end;
2226 }
2227 }
2228
2229 ret_ft = selected_option->ft;
2230
2231 end:
2232 return ret_ft;
2233 }
2234
2235 static
2236 void set_event_default_clock_value(struct bt_notif_iter *notit)
2237 {
2238 struct bt_event *event = bt_notification_event_borrow_event(
2239 notit->event_notif);
2240 struct bt_stream_class *sc = notit->meta.sc->ir_sc;
2241
2242 BT_ASSERT(event);
2243
2244 if (bt_stream_class_borrow_default_clock_class(sc)) {
2245 int ret = bt_event_set_default_clock_value(event,
2246 notit->default_clock_val);
2247
2248 BT_ASSERT(ret == 0);
2249 }
2250 }
2251
2252 static
2253 void notify_new_stream(struct bt_notif_iter *notit,
2254 struct bt_notification **notification)
2255 {
2256 enum bt_notif_iter_status status;
2257 struct bt_notification *ret = NULL;
2258
2259 status = set_current_stream(notit);
2260 if (status != BT_NOTIF_ITER_STATUS_OK) {
2261 BT_PUT(ret);
2262 goto end;
2263 }
2264
2265 BT_ASSERT(notit->stream);
2266 BT_ASSERT(notit->notif_iter);
2267 ret = bt_notification_stream_begin_create(notit->notif_iter,
2268 notit->stream);
2269 if (!ret) {
2270 BT_LOGE("Cannot create stream beginning notification: "
2271 "notit-addr=%p, stream-addr=%p",
2272 notit, notit->stream);
2273 return;
2274 }
2275
2276 end:
2277 *notification = ret;
2278 }
2279
2280 static
2281 void notify_end_of_stream(struct bt_notif_iter *notit,
2282 struct bt_notification **notification)
2283 {
2284 struct bt_notification *ret;
2285
2286 if (!notit->stream) {
2287 BT_LOGE("Cannot create stream for stream notification: "
2288 "notit-addr=%p", notit);
2289 return;
2290 }
2291
2292 BT_ASSERT(notit->notif_iter);
2293 ret = bt_notification_stream_end_create(notit->notif_iter,
2294 notit->stream);
2295 if (!ret) {
2296 BT_LOGE("Cannot create stream beginning notification: "
2297 "notit-addr=%p, stream-addr=%p",
2298 notit, notit->stream);
2299 return;
2300 }
2301 *notification = ret;
2302 }
2303
2304 static
2305 void notify_new_packet(struct bt_notif_iter *notit,
2306 struct bt_notification **notification)
2307 {
2308 int ret;
2309 enum bt_notif_iter_status status;
2310 struct bt_notification *notif = NULL;
2311 struct bt_stream_class *sc;
2312
2313 status = set_current_packet(notit);
2314 if (status != BT_NOTIF_ITER_STATUS_OK) {
2315 goto end;
2316 }
2317
2318 BT_ASSERT(notit->packet);
2319 sc = notit->meta.sc->ir_sc;
2320 BT_ASSERT(sc);
2321
2322 if (bt_stream_class_packets_have_discarded_event_counter_snapshot(sc)) {
2323 BT_ASSERT(notit->snapshots.discarded_events != UINT64_C(-1));
2324 ret = bt_packet_set_discarded_event_counter_snapshot(
2325 notit->packet, notit->snapshots.discarded_events);
2326 BT_ASSERT(ret == 0);
2327 }
2328
2329 if (bt_stream_class_packets_have_packet_counter_snapshot(sc)) {
2330 BT_ASSERT(notit->snapshots.packets != UINT64_C(-1));
2331 ret = bt_packet_set_packet_counter_snapshot(
2332 notit->packet, notit->snapshots.packets);
2333 BT_ASSERT(ret == 0);
2334 }
2335
2336 if (bt_stream_class_packets_have_default_beginning_clock_value(sc)) {
2337 BT_ASSERT(notit->snapshots.beginning_clock != UINT64_C(-1));
2338 ret = bt_packet_set_default_beginning_clock_value(
2339 notit->packet, notit->snapshots.beginning_clock);
2340 BT_ASSERT(ret == 0);
2341 }
2342
2343 if (bt_stream_class_packets_have_default_end_clock_value(sc)) {
2344 BT_ASSERT(notit->snapshots.end_clock != UINT64_C(-1));
2345 ret = bt_packet_set_default_end_clock_value(
2346 notit->packet, notit->snapshots.end_clock);
2347 BT_ASSERT(ret == 0);
2348 }
2349
2350 if (notit->packet_header_field) {
2351 ret = bt_packet_move_header_field(notit->packet,
2352 notit->packet_header_field);
2353 if (ret) {
2354 goto end;
2355 }
2356
2357 notit->packet_header_field = NULL;
2358
2359 /*
2360 * At this point notit->dscopes.trace_packet_header has
2361 * the same value as the packet header field within
2362 * notit->packet.
2363 */
2364 BT_ASSERT(bt_packet_borrow_header_field(notit->packet) ==
2365 notit->dscopes.trace_packet_header);
2366 }
2367
2368 if (notit->packet_context_field) {
2369 ret = bt_packet_move_context_field(notit->packet,
2370 notit->packet_context_field);
2371 if (ret) {
2372 goto end;
2373 }
2374
2375 notit->packet_context_field = NULL;
2376
2377 /*
2378 * At this point notit->dscopes.trace_packet_header has
2379 * the same value as the packet header field within
2380 * notit->packet.
2381 */
2382 BT_ASSERT(bt_packet_borrow_context_field(notit->packet) ==
2383 notit->dscopes.stream_packet_context);
2384 }
2385
2386 BT_ASSERT(notit->notif_iter);
2387 notif = bt_notification_packet_begin_create(notit->notif_iter,
2388 notit->packet);
2389 if (!notif) {
2390 BT_LOGE("Cannot create packet beginning notification: "
2391 "notit-addr=%p, packet-addr=%p",
2392 notit, notit->packet);
2393 goto end;
2394 }
2395
2396 *notification = notif;
2397
2398 end:
2399 return;
2400 }
2401
2402 static
2403 void notify_end_of_packet(struct bt_notif_iter *notit,
2404 struct bt_notification **notification)
2405 {
2406 struct bt_notification *notif;
2407
2408 if (!notit->packet) {
2409 return;
2410 }
2411
2412 /* Update default clock from packet's end time */
2413 if (notit->snapshots.end_clock != UINT64_C(-1)) {
2414 notit->default_clock_val = notit->snapshots.end_clock;
2415 }
2416
2417 BT_ASSERT(notit->notif_iter);
2418 notif = bt_notification_packet_end_create(notit->notif_iter,
2419 notit->packet);
2420 if (!notif) {
2421 BT_LOGE("Cannot create packet end notification: "
2422 "notit-addr=%p, packet-addr=%p",
2423 notit, notit->packet);
2424 return;
2425
2426 }
2427
2428 BT_PUT(notit->packet);
2429 *notification = notif;
2430 }
2431
2432 BT_HIDDEN
2433 struct bt_notif_iter *bt_notif_iter_create(struct ctf_trace_class *tc,
2434 size_t max_request_sz,
2435 struct bt_notif_iter_medium_ops medops, void *data)
2436 {
2437 struct bt_notif_iter *notit = NULL;
2438 struct bt_btr_cbs cbs = {
2439 .types = {
2440 .signed_int = btr_signed_int_cb,
2441 .unsigned_int = btr_unsigned_int_cb,
2442 .floating_point = btr_floating_point_cb,
2443 .string_begin = btr_string_begin_cb,
2444 .string = btr_string_cb,
2445 .string_end = btr_string_end_cb,
2446 .compound_begin = btr_compound_begin_cb,
2447 .compound_end = btr_compound_end_cb,
2448 },
2449 .query = {
2450 .get_sequence_length = btr_get_sequence_length_cb,
2451 .borrow_variant_selected_field_type = btr_borrow_variant_selected_field_type_cb,
2452 },
2453 };
2454
2455 BT_ASSERT(tc);
2456 BT_ASSERT(medops.request_bytes);
2457 BT_ASSERT(medops.borrow_stream);
2458 BT_LOGD("Creating CTF plugin notification iterator: "
2459 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
2460 "data=%p", tc, tc->name->str, max_request_sz, data);
2461 notit = g_new0(struct bt_notif_iter, 1);
2462 if (!notit) {
2463 BT_LOGE_STR("Failed to allocate one CTF plugin notification iterator.");
2464 goto end;
2465 }
2466 notit->meta.tc = tc;
2467 notit->medium.medops = medops;
2468 notit->medium.max_request_sz = max_request_sz;
2469 notit->medium.data = data;
2470 notit->stack = stack_new(notit);
2471 notit->stored_values = g_array_new(FALSE, TRUE, sizeof(uint64_t));
2472 g_array_set_size(notit->stored_values, tc->stored_value_count);
2473
2474 if (!notit->stack) {
2475 BT_LOGE_STR("Failed to create field stack.");
2476 goto error;
2477 }
2478
2479 notit->btr = bt_btr_create(cbs, notit);
2480 if (!notit->btr) {
2481 BT_LOGE_STR("Failed to create binary type reader (BTR).");
2482 goto error;
2483 }
2484
2485 bt_notif_iter_reset(notit);
2486 BT_LOGD("Created CTF plugin notification iterator: "
2487 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
2488 "data=%p, notit-addr=%p",
2489 tc, tc->name->str, max_request_sz, data,
2490 notit);
2491 notit->cur_packet_offset = 0;
2492
2493 end:
2494 return notit;
2495
2496 error:
2497 bt_notif_iter_destroy(notit);
2498 notit = NULL;
2499 goto end;
2500 }
2501
2502 void bt_notif_iter_destroy(struct bt_notif_iter *notit)
2503 {
2504 BT_PUT(notit->packet);
2505 BT_PUT(notit->stream);
2506 release_all_dscopes(notit);
2507
2508 BT_LOGD("Destroying CTF plugin notification iterator: addr=%p", notit);
2509
2510 if (notit->stack) {
2511 BT_LOGD_STR("Destroying field stack.");
2512 stack_destroy(notit->stack);
2513 }
2514
2515 if (notit->btr) {
2516 BT_LOGD("Destroying BTR: btr-addr=%p", notit->btr);
2517 bt_btr_destroy(notit->btr);
2518 }
2519
2520 if (notit->stored_values) {
2521 g_array_free(notit->stored_values, TRUE);
2522 }
2523
2524 g_free(notit);
2525 }
2526
2527 enum bt_notif_iter_status bt_notif_iter_get_next_notification(
2528 struct bt_notif_iter *notit,
2529 struct bt_private_connection_private_notification_iterator *notif_iter,
2530 struct bt_notification **notification)
2531 {
2532 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
2533
2534 BT_ASSERT(notit);
2535 BT_ASSERT(notification);
2536
2537 if (notit->state == STATE_DONE) {
2538 status = BT_NOTIF_ITER_STATUS_EOF;
2539 goto end;
2540 }
2541
2542 notit->notif_iter = notif_iter;
2543
2544 BT_LOGV("Getting next notification: notit-addr=%p", notit);
2545
2546 while (true) {
2547 status = handle_state(notit);
2548 if (status == BT_NOTIF_ITER_STATUS_AGAIN) {
2549 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_AGAIN.");
2550 goto end;
2551 }
2552
2553 if (status != BT_NOTIF_ITER_STATUS_OK) {
2554 if (status == BT_NOTIF_ITER_STATUS_EOF) {
2555 enum state next_state = notit->state;
2556
2557 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_EOF.");
2558
2559 if (notit->packet) {
2560 notify_end_of_packet(notit,
2561 notification);
2562 } else {
2563 notify_end_of_stream(notit,
2564 notification);
2565 next_state = STATE_DONE;
2566 }
2567
2568 if (!*notification) {
2569 status = BT_NOTIF_ITER_STATUS_ERROR;
2570 goto end;
2571 }
2572
2573 status = BT_NOTIF_ITER_STATUS_OK;
2574 notit->state = next_state;
2575 } else {
2576 BT_LOGW("Cannot handle state: "
2577 "notit-addr=%p, state=%s",
2578 notit, state_string(notit->state));
2579 }
2580
2581 goto end;
2582 }
2583
2584 switch (notit->state) {
2585 case STATE_EMIT_NOTIF_NEW_STREAM:
2586 /* notify_new_stream() logs errors */
2587 notify_new_stream(notit, notification);
2588
2589 if (!*notification) {
2590 status = BT_NOTIF_ITER_STATUS_ERROR;
2591 }
2592
2593 notit->stream_begin_emitted = true;
2594 goto end;
2595 case STATE_EMIT_NOTIF_NEW_PACKET:
2596 /* notify_new_packet() logs errors */
2597 notify_new_packet(notit, notification);
2598
2599 if (!*notification) {
2600 status = BT_NOTIF_ITER_STATUS_ERROR;
2601 }
2602
2603 goto end;
2604 case STATE_EMIT_NOTIF_EVENT:
2605 BT_ASSERT(notit->event_notif);
2606 set_event_default_clock_value(notit);
2607 *notification = notit->event_notif;
2608 notit->event_notif = NULL;
2609 goto end;
2610 case STATE_EMIT_NOTIF_END_OF_PACKET:
2611 /* notify_end_of_packet() logs errors */
2612 notify_end_of_packet(notit, notification);
2613
2614 if (!*notification) {
2615 status = BT_NOTIF_ITER_STATUS_ERROR;
2616 }
2617
2618 goto end;
2619 default:
2620 /* Non-emitting state: continue */
2621 break;
2622 }
2623 }
2624
2625 end:
2626 return status;
2627 }
2628
2629 BT_HIDDEN
2630 enum bt_notif_iter_status bt_notif_iter_borrow_packet_header_context_fields(
2631 struct bt_notif_iter *notit,
2632 struct bt_field **packet_header_field,
2633 struct bt_field **packet_context_field)
2634 {
2635 int ret;
2636 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
2637
2638 BT_ASSERT(notit);
2639
2640 if (notit->state == STATE_EMIT_NOTIF_NEW_PACKET) {
2641 /* We're already there */
2642 goto set_fields;
2643 }
2644
2645 while (true) {
2646 status = handle_state(notit);
2647 if (status == BT_NOTIF_ITER_STATUS_AGAIN) {
2648 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_AGAIN.");
2649 goto end;
2650 }
2651 if (status != BT_NOTIF_ITER_STATUS_OK) {
2652 if (status == BT_NOTIF_ITER_STATUS_EOF) {
2653 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_EOF.");
2654 } else {
2655 BT_LOGW("Cannot handle state: "
2656 "notit-addr=%p, state=%s",
2657 notit, state_string(notit->state));
2658 }
2659 goto end;
2660 }
2661
2662 switch (notit->state) {
2663 case STATE_EMIT_NOTIF_NEW_PACKET:
2664 /*
2665 * Packet header and context fields are
2666 * potentially decoded (or they don't exist).
2667 */
2668 goto set_fields;
2669 case STATE_INIT:
2670 case STATE_EMIT_NOTIF_NEW_STREAM:
2671 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
2672 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
2673 case STATE_AFTER_TRACE_PACKET_HEADER:
2674 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN:
2675 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE:
2676 case STATE_AFTER_STREAM_PACKET_CONTEXT:
2677 /* Non-emitting state: continue */
2678 break;
2679 default:
2680 /*
2681 * We should never get past the
2682 * STATE_EMIT_NOTIF_NEW_PACKET state.
2683 */
2684 BT_LOGF("Unexpected state: notit-addr=%p, state=%s",
2685 notit, state_string(notit->state));
2686 abort();
2687 }
2688 }
2689
2690 set_fields:
2691 ret = set_current_packet_content_sizes(notit);
2692 if (ret) {
2693 status = BT_NOTIF_ITER_STATUS_ERROR;
2694 goto end;
2695 }
2696
2697 if (packet_header_field) {
2698 *packet_header_field = notit->dscopes.trace_packet_header;
2699 }
2700
2701 if (packet_context_field) {
2702 *packet_context_field = notit->dscopes.stream_packet_context;
2703 }
2704
2705 end:
2706 return status;
2707 }
2708
2709 BT_HIDDEN
2710 void bt_notif_iter_set_medops_data(struct bt_notif_iter *notit,
2711 void *medops_data)
2712 {
2713 BT_ASSERT(notit);
2714 notit->medium.data = medops_data;
2715 }
2716
2717 BT_HIDDEN
2718 enum bt_notif_iter_status bt_notif_iter_seek(
2719 struct bt_notif_iter *notit, off_t offset)
2720 {
2721 enum bt_notif_iter_status ret = BT_NOTIF_ITER_STATUS_OK;
2722 enum bt_notif_iter_medium_status medium_status;
2723
2724 BT_ASSERT(notit);
2725 if (offset < 0) {
2726 BT_LOGE("Cannot seek to negative offset: offset=%jd", offset);
2727 ret = BT_NOTIF_ITER_STATUS_INVAL;
2728 goto end;
2729 }
2730
2731 if (!notit->medium.medops.seek) {
2732 ret = BT_NOTIF_ITER_STATUS_UNSUPPORTED;
2733 BT_LOGD("Aborting seek as the iterator's underlying media does not implement seek support.");
2734 goto end;
2735 }
2736
2737 medium_status = notit->medium.medops.seek(
2738 BT_NOTIF_ITER_SEEK_WHENCE_SET, offset, notit->medium.data);
2739 if (medium_status != BT_NOTIF_ITER_MEDIUM_STATUS_OK) {
2740 if (medium_status == BT_NOTIF_ITER_MEDIUM_STATUS_EOF) {
2741 ret = BT_NOTIF_ITER_STATUS_EOF;
2742 } else {
2743 ret = BT_NOTIF_ITER_STATUS_ERROR;
2744 goto end;
2745 }
2746 }
2747
2748 bt_notif_iter_reset(notit);
2749 notit->cur_packet_offset = offset;
2750
2751 end:
2752 return ret;
2753 }
2754
2755 BT_HIDDEN
2756 off_t bt_notif_iter_get_current_packet_offset(struct bt_notif_iter *notit)
2757 {
2758 BT_ASSERT(notit);
2759 return notit->cur_packet_offset;
2760 }
2761
2762 BT_HIDDEN
2763 off_t bt_notif_iter_get_current_packet_size(
2764 struct bt_notif_iter *notit)
2765 {
2766 BT_ASSERT(notit);
2767 return notit->cur_exp_packet_total_size;
2768 }
2769
2770 BT_HIDDEN
2771 void bt_notif_trace_class_changed(struct bt_notif_iter *notit)
2772 {
2773 if (notit->meta.tc->stored_value_count > notit->stored_values->len) {
2774 g_array_set_size(notit->stored_values,
2775 notit->meta.tc->stored_value_count);
2776 }
2777 }
2778
2779 BT_HIDDEN
2780 enum bt_notif_iter_status bt_notif_iter_get_packet_properties(
2781 struct bt_notif_iter *notit,
2782 struct bt_notif_iter_packet_properties *props)
2783 {
2784 BT_ASSERT(notit);
2785 BT_ASSERT(props);
2786
2787 props->exp_packet_total_size =
2788 (uint64_t) notit->cur_exp_packet_total_size;
2789 props->exp_packet_content_size =
2790 (uint64_t) notit->cur_exp_packet_content_size;
2791 BT_ASSERT(props->stream_class_id >= 0);
2792 props->stream_class_id = (uint64_t) notit->cur_stream_class_id;
2793 props->data_stream_id = notit->cur_data_stream_id;
2794 props->snapshots.discarded_events = notit->snapshots.discarded_events;
2795 props->snapshots.packets = notit->snapshots.packets;
2796 props->snapshots.beginning_clock = notit->snapshots.beginning_clock;
2797 props->snapshots.end_clock = notit->snapshots.end_clock;
2798 return BT_NOTIF_ITER_STATUS_OK;
2799 }
This page took 0.153254 seconds and 4 git commands to generate.