lib: make trace IR API const-correct
[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 "../bfcr/bfcr.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_self_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_private_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_bfcr *bfcr;
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_class *dscope_fc,
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_bfcr_status bfcr_status;
527 size_t consumed_bits;
528
529 notit->cur_dscope_field = dscope_field;
530 BT_LOGV("Starting BFCR: notit-addr=%p, bfcr-addr=%p, fc-addr=%p",
531 notit, notit->bfcr, dscope_fc);
532 consumed_bits = bt_bfcr_start(notit->bfcr, dscope_fc,
533 notit->buf.addr, notit->buf.at, packet_at(notit),
534 notit->buf.sz, &bfcr_status);
535 BT_LOGV("BFCR consumed bits: size=%zu", consumed_bits);
536
537 switch (bfcr_status) {
538 case BT_BFCR_STATUS_OK:
539 /* Field class was read completely */
540 BT_LOGV_STR("Field was completely decoded.");
541 notit->state = done_state;
542 break;
543 case BT_BFCR_STATUS_EOF:
544 BT_LOGV_STR("BFCR needs more data to decode field completely.");
545 notit->state = continue_state;
546 break;
547 default:
548 BT_LOGW("BFCR failed to start: notit-addr=%p, bfcr-addr=%p, "
549 "status=%s", notit, notit->bfcr,
550 bt_bfcr_status_string(bfcr_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_bfcr_status bfcr_status;
568 size_t consumed_bits;
569
570 BT_LOGV("Continuing BFCR: notit-addr=%p, bfcr-addr=%p",
571 notit, notit->bfcr);
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_bfcr_continue(notit->bfcr, notit->buf.addr,
589 notit->buf.sz, &bfcr_status);
590 BT_LOGV("BFCR consumed bits: size=%zu", consumed_bits);
591
592 switch (bfcr_status) {
593 case BT_BFCR_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_BFCR_STATUS_EOF:
599 /* Stay in this continue state. */
600 BT_LOGV_STR("BFCR needs more data to decode field completely.");
601 break;
602 default:
603 BT_LOGW("BFCR failed to continue: notit-addr=%p, bfcr-addr=%p, "
604 "status=%s", notit, notit->bfcr,
605 bt_bfcr_status_string(bfcr_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_class *packet_header_fc = 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 class is common to the whole trace. */
665 packet_header_fc = notit->meta.tc->packet_header_fc;
666 if (!packet_header_fc) {
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_fc->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 =
685 bt_packet_header_field_create(
686 notit->meta.tc->ir_tc);
687 if (!notit->packet_header_field) {
688 BT_LOGE_STR("Cannot create packet header field wrapper from trace.");
689 ret = BT_NOTIF_ITER_STATUS_ERROR;
690 goto end;
691 }
692
693 notit->dscopes.trace_packet_header =
694 bt_packet_header_field_borrow_field(
695 notit->packet_header_field);
696 BT_ASSERT(notit->dscopes.trace_packet_header);
697 }
698
699 notit->cur_stream_class_id = -1;
700 notit->cur_event_class_id = -1;
701 notit->cur_data_stream_id = -1;
702 BT_LOGV("Decoding packet header field:"
703 "notit-addr=%p, trace-addr=%p, trace-name=\"%s\", fc-addr=%p",
704 notit, notit->meta.tc,
705 notit->meta.tc->name->str, packet_header_fc);
706 ret = read_dscope_begin_state(notit, packet_header_fc,
707 STATE_AFTER_TRACE_PACKET_HEADER,
708 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE,
709 notit->dscopes.trace_packet_header);
710 if (ret < 0) {
711 BT_LOGW("Cannot decode packet header field: "
712 "notit-addr=%p, trace-addr=%p, "
713 "trace-name=\"%s\", fc-addr=%p",
714 notit, notit->meta.tc,
715 notit->meta.tc->name->str,
716 packet_header_fc);
717 }
718
719 end:
720 return ret;
721 }
722
723 static
724 enum bt_notif_iter_status read_packet_header_continue_state(
725 struct bt_notif_iter *notit)
726 {
727 return read_dscope_continue_state(notit,
728 STATE_AFTER_TRACE_PACKET_HEADER);
729 }
730
731 static inline
732 enum bt_notif_iter_status set_current_stream_class(struct bt_notif_iter *notit)
733 {
734 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
735 struct ctf_stream_class *new_stream_class = NULL;
736
737 if (notit->cur_stream_class_id == -1) {
738 /*
739 * No current stream class ID field, therefore only one
740 * stream class.
741 */
742 if (notit->meta.tc->stream_classes->len != 1) {
743 BT_LOGW("Need exactly one stream class since there's "
744 "no stream class ID field: "
745 "notit-addr=%p, trace-name=\"%s\"",
746 notit, notit->meta.tc->name->str);
747 status = BT_NOTIF_ITER_STATUS_ERROR;
748 goto end;
749 }
750
751 new_stream_class = notit->meta.tc->stream_classes->pdata[0];
752 notit->cur_stream_class_id = new_stream_class->id;
753 goto end;
754 }
755
756 new_stream_class = ctf_trace_class_borrow_stream_class_by_id(
757 notit->meta.tc, notit->cur_stream_class_id);
758 if (!new_stream_class) {
759 BT_LOGW("No stream class with ID of stream class ID to use in trace: "
760 "notit-addr=%p, stream-class-id=%" PRIu64 ", "
761 "trace-addr=%p, trace-name=\"%s\"",
762 notit, notit->cur_stream_class_id, notit->meta.tc,
763 notit->meta.tc->name->str);
764 status = BT_NOTIF_ITER_STATUS_ERROR;
765 goto end;
766 }
767
768 if (notit->meta.sc) {
769 if (new_stream_class != notit->meta.sc) {
770 BT_LOGW("Two packets refer to two different stream classes within the same packet sequence: "
771 "notit-addr=%p, prev-stream-class-addr=%p, "
772 "prev-stream-class-id=%" PRId64 ", "
773 "next-stream-class-addr=%p, "
774 "next-stream-class-id=%" PRId64 ", "
775 "trace-addr=%p, trace-name=\"%s\"",
776 notit, notit->meta.sc,
777 notit->meta.sc->id,
778 new_stream_class,
779 new_stream_class->id,
780 notit->meta.tc,
781 notit->meta.tc->name->str);
782 status = BT_NOTIF_ITER_STATUS_ERROR;
783 goto end;
784 }
785 } else {
786 notit->meta.sc = new_stream_class;
787 }
788
789 BT_LOGV("Set current stream class: "
790 "notit-addr=%p, stream-class-addr=%p, "
791 "stream-class-id=%" PRId64,
792 notit, notit->meta.sc, notit->meta.sc->id);
793
794 end:
795 return status;
796 }
797
798 static inline
799 enum bt_notif_iter_status set_current_stream(struct bt_notif_iter *notit)
800 {
801 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
802 struct bt_stream *stream = NULL;
803
804 BT_LOGV("Calling user function (get stream): notit-addr=%p, "
805 "stream-class-addr=%p, stream-class-id=%" PRId64,
806 notit, notit->meta.sc,
807 notit->meta.sc->id);
808 stream = notit->medium.medops.borrow_stream(
809 notit->meta.sc->ir_sc, notit->cur_data_stream_id,
810 notit->medium.data);
811 bt_object_get_ref(stream);
812 BT_LOGV("User function returned: stream-addr=%p", stream);
813 if (!stream) {
814 BT_LOGW_STR("User function failed to return a stream object "
815 "for the given stream class.");
816 status = BT_NOTIF_ITER_STATUS_ERROR;
817 goto end;
818 }
819
820 if (notit->stream && stream != notit->stream) {
821 BT_LOGW("User function returned a different stream than the "
822 "previous one for the same sequence of packets.");
823 status = BT_NOTIF_ITER_STATUS_ERROR;
824 goto end;
825 }
826
827 BT_OBJECT_MOVE_REF(notit->stream, stream);
828
829 end:
830 bt_object_put_ref(stream);
831 return status;
832 }
833
834 static inline
835 enum bt_notif_iter_status set_current_packet(struct bt_notif_iter *notit)
836 {
837 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
838 struct bt_packet *packet = NULL;
839
840 BT_LOGV("Creating packet for packet notification: "
841 "notit-addr=%p", notit);
842 BT_LOGV("Creating packet from stream: "
843 "notit-addr=%p, stream-addr=%p, "
844 "stream-class-addr=%p, "
845 "stream-class-id=%" PRId64,
846 notit, notit->stream, notit->meta.sc,
847 notit->meta.sc->id);
848
849 /* Create packet */
850 BT_ASSERT(notit->stream);
851 packet = bt_packet_create(notit->stream);
852 if (!packet) {
853 BT_LOGE("Cannot create packet from stream: "
854 "notit-addr=%p, stream-addr=%p, "
855 "stream-class-addr=%p, "
856 "stream-class-id=%" PRId64,
857 notit, notit->stream, notit->meta.sc,
858 notit->meta.sc->id);
859 goto error;
860 }
861
862 goto end;
863
864 error:
865 BT_OBJECT_PUT_REF_AND_RESET(packet);
866 status = BT_NOTIF_ITER_STATUS_ERROR;
867
868 end:
869 BT_OBJECT_MOVE_REF(notit->packet, packet);
870 return status;
871 }
872
873 static
874 enum bt_notif_iter_status after_packet_header_state(
875 struct bt_notif_iter *notit)
876 {
877 enum bt_notif_iter_status status;
878
879 status = set_current_stream_class(notit);
880 if (status != BT_NOTIF_ITER_STATUS_OK) {
881 goto end;
882 }
883
884 notit->state = STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN;
885
886 end:
887 return status;
888 }
889
890 static
891 enum bt_notif_iter_status read_packet_context_begin_state(
892 struct bt_notif_iter *notit)
893 {
894 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
895 struct ctf_field_class *packet_context_fc;
896
897 BT_ASSERT(notit->meta.sc);
898 packet_context_fc = notit->meta.sc->packet_context_fc;
899 if (!packet_context_fc) {
900 BT_LOGV("No packet packet context field class in stream class: continuing: "
901 "notit-addr=%p, stream-class-addr=%p, "
902 "stream-class-id=%" PRId64,
903 notit, notit->meta.sc,
904 notit->meta.sc->id);
905 notit->state = STATE_AFTER_STREAM_PACKET_CONTEXT;
906 goto end;
907 }
908
909 BT_ASSERT(!notit->packet_context_field);
910
911 if (packet_context_fc->in_ir) {
912 /*
913 * Create free packet context field from stream class.
914 * This field is going to be moved to the packet once we
915 * create it. We cannot create the packet now because a
916 * packet is created from a stream, and this API must be
917 * able to return the packet header and context fields
918 * without creating a stream
919 * (bt_notif_iter_borrow_packet_header_context_fields()).
920 */
921 notit->packet_context_field =
922 bt_packet_context_field_create(
923 notit->meta.sc->ir_sc);
924 if (!notit->packet_context_field) {
925 BT_LOGE_STR("Cannot create packet context field wrapper from stream class.");
926 status = BT_NOTIF_ITER_STATUS_ERROR;
927 goto end;
928 }
929
930 notit->dscopes.stream_packet_context =
931 bt_packet_context_field_borrow_field(
932 notit->packet_context_field);
933 BT_ASSERT(notit->dscopes.stream_packet_context);
934 }
935
936 BT_LOGV("Decoding packet context field: "
937 "notit-addr=%p, stream-class-addr=%p, "
938 "stream-class-id=%" PRId64 ", fc-addr=%p",
939 notit, notit->meta.sc,
940 notit->meta.sc->id, packet_context_fc);
941 status = read_dscope_begin_state(notit, packet_context_fc,
942 STATE_AFTER_STREAM_PACKET_CONTEXT,
943 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE,
944 notit->dscopes.stream_packet_context);
945 if (status < 0) {
946 BT_LOGW("Cannot decode packet context field: "
947 "notit-addr=%p, stream-class-addr=%p, "
948 "stream-class-id=%" PRId64 ", fc-addr=%p",
949 notit, notit->meta.sc,
950 notit->meta.sc->id,
951 packet_context_fc);
952 }
953
954 end:
955 return status;
956 }
957
958 static
959 enum bt_notif_iter_status read_packet_context_continue_state(
960 struct bt_notif_iter *notit)
961 {
962 return read_dscope_continue_state(notit,
963 STATE_AFTER_STREAM_PACKET_CONTEXT);
964 }
965
966 static
967 enum bt_notif_iter_status set_current_packet_content_sizes(
968 struct bt_notif_iter *notit)
969 {
970 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
971
972 if (notit->cur_exp_packet_total_size == -1) {
973 if (notit->cur_exp_packet_content_size != -1) {
974 BT_LOGW("Content size is set, but packet size is not: "
975 "notit-addr=%p, packet-context-field-addr=%p, "
976 "packet-size=%" PRId64 ", content-size=%" PRId64,
977 notit, notit->dscopes.stream_packet_context,
978 notit->cur_exp_packet_total_size,
979 notit->cur_exp_packet_content_size);
980 status = BT_NOTIF_ITER_STATUS_ERROR;
981 goto end;
982 }
983 } else {
984 if (notit->cur_exp_packet_content_size == -1) {
985 notit->cur_exp_packet_content_size =
986 notit->cur_exp_packet_total_size;
987 }
988 }
989
990 if (notit->cur_exp_packet_content_size >
991 notit->cur_exp_packet_total_size) {
992 BT_LOGW("Invalid packet or content size: "
993 "content size is greater than packet size: "
994 "notit-addr=%p, packet-context-field-addr=%p, "
995 "packet-size=%" PRId64 ", content-size=%" PRId64,
996 notit, notit->dscopes.stream_packet_context,
997 notit->cur_exp_packet_total_size,
998 notit->cur_exp_packet_content_size);
999 status = BT_NOTIF_ITER_STATUS_ERROR;
1000 goto end;
1001 }
1002
1003 BT_LOGV("Set current packet and content sizes: "
1004 "notit-addr=%p, packet-size=%" PRIu64 ", content-size=%" PRIu64,
1005 notit, notit->cur_exp_packet_total_size,
1006 notit->cur_exp_packet_content_size);
1007 end:
1008 return status;
1009 }
1010
1011 static
1012 enum bt_notif_iter_status after_packet_context_state(
1013 struct bt_notif_iter *notit)
1014 {
1015 enum bt_notif_iter_status status;
1016
1017 status = set_current_packet_content_sizes(notit);
1018 if (status != BT_NOTIF_ITER_STATUS_OK) {
1019 goto end;
1020 }
1021
1022 if (notit->stream_begin_emitted) {
1023 notit->state = STATE_EMIT_NOTIF_NEW_PACKET;
1024 } else {
1025 notit->state = STATE_EMIT_NOTIF_NEW_STREAM;
1026 }
1027
1028 end:
1029 return status;
1030 }
1031
1032 static
1033 enum bt_notif_iter_status read_event_header_begin_state(
1034 struct bt_notif_iter *notit)
1035 {
1036 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1037 struct ctf_field_class *event_header_fc = NULL;
1038
1039 /* Reset the position of the last event header */
1040 notit->buf.last_eh_at = notit->buf.at;
1041 notit->cur_event_class_id = -1;
1042
1043 /* Check if we have some content left */
1044 if (notit->cur_exp_packet_content_size >= 0) {
1045 if (unlikely(packet_at(notit) ==
1046 notit->cur_exp_packet_content_size)) {
1047 /* No more events! */
1048 BT_LOGV("Reached end of packet: notit-addr=%p, "
1049 "cur=%zu", notit, packet_at(notit));
1050 notit->state = STATE_EMIT_NOTIF_END_OF_PACKET;
1051 goto end;
1052 } else if (unlikely(packet_at(notit) >
1053 notit->cur_exp_packet_content_size)) {
1054 /* That's not supposed to happen */
1055 BT_LOGV("Before decoding event header field: cursor is passed the packet's content: "
1056 "notit-addr=%p, content-size=%" PRId64 ", "
1057 "cur=%zu", notit,
1058 notit->cur_exp_packet_content_size,
1059 packet_at(notit));
1060 status = BT_NOTIF_ITER_STATUS_ERROR;
1061 goto end;
1062 }
1063 } else {
1064 /*
1065 * "Infinite" content: we're done when the medium has
1066 * nothing else for us.
1067 */
1068 status = buf_ensure_available_bits(notit);
1069 if (status != BT_NOTIF_ITER_STATUS_OK) {
1070 /*
1071 * If this function returns
1072 * `BT_NOTIF_ITER_STATUS_EOF`:
1073 *
1074 * 1. bt_notif_iter_get_next_notification()
1075 * emits a "packet end" notification. This
1076 * resets the current packet. The state
1077 * remains unchanged otherwise.
1078 * 2. This function is called again. It returns
1079 * `BT_NOTIF_ITER_STATUS_EOF` again.
1080 * 3. bt_notif_iter_get_next_notification()
1081 * emits a "stream end" notification because
1082 * there's no current packet. It sets the
1083 * current state to `STATE_DONE`.
1084 */
1085 goto end;
1086 }
1087 }
1088
1089 release_event_dscopes(notit);
1090 BT_ASSERT(notit->meta.sc);
1091 event_header_fc = notit->meta.sc->event_header_fc;
1092 if (!event_header_fc) {
1093 notit->state = STATE_AFTER_EVENT_HEADER;
1094 goto end;
1095 }
1096
1097 if (event_header_fc->in_ir) {
1098 BT_ASSERT(!notit->event_header_field);
1099 notit->event_header_field =
1100 bt_event_header_field_create(
1101 notit->meta.sc->ir_sc);
1102 if (!notit->event_header_field) {
1103 BT_LOGE_STR("Cannot create event header field wrapper from trace.");
1104 status = BT_NOTIF_ITER_STATUS_ERROR;
1105 goto end;
1106 }
1107
1108 notit->dscopes.event_header =
1109 bt_event_header_field_borrow_field(
1110 notit->event_header_field);
1111 BT_ASSERT(notit->dscopes.event_header);
1112 }
1113
1114 BT_LOGV("Decoding event header field: "
1115 "notit-addr=%p, stream-class-addr=%p, "
1116 "stream-class-id=%" PRId64 ", "
1117 "fc-addr=%p",
1118 notit, notit->meta.sc,
1119 notit->meta.sc->id,
1120 event_header_fc);
1121 status = read_dscope_begin_state(notit, event_header_fc,
1122 STATE_AFTER_EVENT_HEADER,
1123 STATE_DSCOPE_EVENT_HEADER_CONTINUE,
1124 notit->dscopes.event_header);
1125 if (status < 0) {
1126 BT_LOGW("Cannot decode event header field: "
1127 "notit-addr=%p, stream-class-addr=%p, "
1128 "stream-class-id=%" PRId64 ", fc-addr=%p",
1129 notit, notit->meta.sc,
1130 notit->meta.sc->id,
1131 event_header_fc);
1132 }
1133
1134 end:
1135 return status;
1136 }
1137
1138 static
1139 enum bt_notif_iter_status read_event_header_continue_state(
1140 struct bt_notif_iter *notit)
1141 {
1142 return read_dscope_continue_state(notit,
1143 STATE_AFTER_EVENT_HEADER);
1144 }
1145
1146 static inline
1147 enum bt_notif_iter_status set_current_event_class(struct bt_notif_iter *notit)
1148 {
1149 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1150
1151 struct ctf_event_class *new_event_class = NULL;
1152
1153 if (notit->cur_event_class_id == -1) {
1154 /*
1155 * No current event class ID field, therefore only one
1156 * event class.
1157 */
1158 if (notit->meta.sc->event_classes->len != 1) {
1159 BT_LOGW("Need exactly one event class since there's "
1160 "no event class ID field: "
1161 "notit-addr=%p, trace-name=\"%s\"",
1162 notit, notit->meta.tc->name->str);
1163 status = BT_NOTIF_ITER_STATUS_ERROR;
1164 goto end;
1165 }
1166
1167 new_event_class = notit->meta.sc->event_classes->pdata[0];
1168 notit->cur_event_class_id = new_event_class->id;
1169 goto end;
1170 }
1171
1172 new_event_class = ctf_stream_class_borrow_event_class_by_id(
1173 notit->meta.sc, notit->cur_event_class_id);
1174 if (!new_event_class) {
1175 BT_LOGW("No event class with ID of event class ID to use in stream class: "
1176 "notit-addr=%p, stream-class-id=%" PRIu64 ", "
1177 "event-class-id=%" PRIu64 ", "
1178 "trace-addr=%p, trace-name=\"%s\"",
1179 notit, notit->meta.sc->id, notit->cur_event_class_id,
1180 notit->meta.tc, notit->meta.tc->name->str);
1181 status = BT_NOTIF_ITER_STATUS_ERROR;
1182 goto end;
1183 }
1184
1185 notit->meta.ec = new_event_class;
1186 BT_LOGV("Set current event class: "
1187 "notit-addr=%p, event-class-addr=%p, "
1188 "event-class-id=%" PRId64 ", "
1189 "event-class-name=\"%s\"",
1190 notit, notit->meta.ec, notit->meta.ec->id,
1191 notit->meta.ec->name->str);
1192
1193 end:
1194 return status;
1195 }
1196
1197 static inline
1198 enum bt_notif_iter_status set_current_event_notification(
1199 struct bt_notif_iter *notit)
1200 {
1201 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1202 struct bt_private_notification *notif = NULL;
1203
1204 BT_ASSERT(notit->meta.ec);
1205 BT_ASSERT(notit->packet);
1206 BT_LOGV("Creating event notification from event class and packet: "
1207 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", packet-addr=%p",
1208 notit, notit->meta.ec,
1209 notit->meta.ec->name->str,
1210 notit->packet);
1211 BT_ASSERT(notit->notif_iter);
1212 notif = bt_private_notification_event_create(notit->notif_iter,
1213 notit->meta.ec->ir_ec, notit->packet);
1214 if (!notif) {
1215 BT_LOGE("Cannot create event notification: "
1216 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", "
1217 "packet-addr=%p",
1218 notit, notit->meta.ec,
1219 notit->meta.ec->name->str,
1220 notit->packet);
1221 goto error;
1222 }
1223
1224 goto end;
1225
1226 error:
1227 BT_OBJECT_PUT_REF_AND_RESET(notif);
1228 status = BT_NOTIF_ITER_STATUS_ERROR;
1229
1230 end:
1231 BT_OBJECT_MOVE_REF(notit->event_notif, notif);
1232 return status;
1233 }
1234
1235 static
1236 enum bt_notif_iter_status after_event_header_state(
1237 struct bt_notif_iter *notit)
1238 {
1239 enum bt_notif_iter_status status;
1240
1241 status = set_current_event_class(notit);
1242 if (status != BT_NOTIF_ITER_STATUS_OK) {
1243 goto end;
1244 }
1245
1246 status = set_current_event_notification(notit);
1247 if (status != BT_NOTIF_ITER_STATUS_OK) {
1248 goto end;
1249 }
1250
1251 notit->event = bt_private_notification_event_borrow_event(
1252 notit->event_notif);
1253 BT_ASSERT(notit->event);
1254
1255 if (notit->event_header_field) {
1256 int ret;
1257
1258 BT_ASSERT(notit->event);
1259 ret = bt_event_move_header_field(notit->event,
1260 notit->event_header_field);
1261 if (ret) {
1262 status = BT_NOTIF_ITER_STATUS_ERROR;
1263 goto end;
1264 }
1265
1266 notit->event_header_field = NULL;
1267
1268 /*
1269 * At this point notit->dscopes.event_header has
1270 * the same value as the event header field within
1271 * notit->event.
1272 */
1273 BT_ASSERT(bt_event_borrow_header_field(
1274 notit->event) == notit->dscopes.event_header);
1275 }
1276
1277 notit->state = STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN;
1278
1279 end:
1280 return status;
1281 }
1282
1283 static
1284 enum bt_notif_iter_status read_event_common_context_begin_state(
1285 struct bt_notif_iter *notit)
1286 {
1287 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1288 struct ctf_field_class *event_common_context_fc;
1289
1290 event_common_context_fc = notit->meta.sc->event_common_context_fc;
1291 if (!event_common_context_fc) {
1292 notit->state = STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN;
1293 goto end;
1294 }
1295
1296 if (event_common_context_fc->in_ir) {
1297 BT_ASSERT(!notit->dscopes.event_common_context);
1298 notit->dscopes.event_common_context =
1299 bt_event_borrow_common_context_field(
1300 notit->event);
1301 BT_ASSERT(notit->dscopes.event_common_context);
1302 }
1303
1304 BT_LOGV("Decoding event common context field: "
1305 "notit-addr=%p, stream-class-addr=%p, "
1306 "stream-class-id=%" PRId64 ", "
1307 "fc-addr=%p",
1308 notit, notit->meta.sc,
1309 notit->meta.sc->id,
1310 event_common_context_fc);
1311 status = read_dscope_begin_state(notit, event_common_context_fc,
1312 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN,
1313 STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE,
1314 notit->dscopes.event_common_context);
1315 if (status < 0) {
1316 BT_LOGW("Cannot decode event common context field: "
1317 "notit-addr=%p, stream-class-addr=%p, "
1318 "stream-class-id=%" PRId64 ", fc-addr=%p",
1319 notit, notit->meta.sc,
1320 notit->meta.sc->id,
1321 event_common_context_fc);
1322 }
1323
1324 end:
1325 return status;
1326 }
1327
1328 static
1329 enum bt_notif_iter_status read_event_common_context_continue_state(
1330 struct bt_notif_iter *notit)
1331 {
1332 return read_dscope_continue_state(notit,
1333 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN);
1334 }
1335
1336 static
1337 enum bt_notif_iter_status read_event_spec_context_begin_state(
1338 struct bt_notif_iter *notit)
1339 {
1340 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1341 struct ctf_field_class *event_spec_context_fc;
1342
1343 event_spec_context_fc = notit->meta.ec->spec_context_fc;
1344 if (!event_spec_context_fc) {
1345 notit->state = STATE_DSCOPE_EVENT_PAYLOAD_BEGIN;
1346 goto end;
1347 }
1348
1349 if (event_spec_context_fc->in_ir) {
1350 BT_ASSERT(!notit->dscopes.event_spec_context);
1351 notit->dscopes.event_spec_context =
1352 bt_event_borrow_specific_context_field(
1353 notit->event);
1354 BT_ASSERT(notit->dscopes.event_spec_context);
1355 }
1356
1357 BT_LOGV("Decoding event specific context field: "
1358 "notit-addr=%p, event-class-addr=%p, "
1359 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
1360 "fc-addr=%p",
1361 notit, notit->meta.ec,
1362 notit->meta.ec->name->str,
1363 notit->meta.ec->id,
1364 event_spec_context_fc);
1365 status = read_dscope_begin_state(notit, event_spec_context_fc,
1366 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN,
1367 STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE,
1368 notit->dscopes.event_spec_context);
1369 if (status < 0) {
1370 BT_LOGW("Cannot decode event specific context field: "
1371 "notit-addr=%p, event-class-addr=%p, "
1372 "event-class-name=\"%s\", "
1373 "event-class-id=%" PRId64 ", fc-addr=%p",
1374 notit, notit->meta.ec,
1375 notit->meta.ec->name->str,
1376 notit->meta.ec->id,
1377 event_spec_context_fc);
1378 }
1379
1380 end:
1381 return status;
1382 }
1383
1384 static
1385 enum bt_notif_iter_status read_event_spec_context_continue_state(
1386 struct bt_notif_iter *notit)
1387 {
1388 return read_dscope_continue_state(notit,
1389 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN);
1390 }
1391
1392 static
1393 enum bt_notif_iter_status read_event_payload_begin_state(
1394 struct bt_notif_iter *notit)
1395 {
1396 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1397 struct ctf_field_class *event_payload_fc;
1398
1399 event_payload_fc = notit->meta.ec->payload_fc;
1400 if (!event_payload_fc) {
1401 notit->state = STATE_EMIT_NOTIF_EVENT;
1402 goto end;
1403 }
1404
1405 if (event_payload_fc->in_ir) {
1406 BT_ASSERT(!notit->dscopes.event_payload);
1407 notit->dscopes.event_payload =
1408 bt_event_borrow_payload_field(
1409 notit->event);
1410 BT_ASSERT(notit->dscopes.event_payload);
1411 }
1412
1413 BT_LOGV("Decoding event payload field: "
1414 "notit-addr=%p, event-class-addr=%p, "
1415 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
1416 "fc-addr=%p",
1417 notit, notit->meta.ec,
1418 notit->meta.ec->name->str,
1419 notit->meta.ec->id,
1420 event_payload_fc);
1421 status = read_dscope_begin_state(notit, event_payload_fc,
1422 STATE_EMIT_NOTIF_EVENT,
1423 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE,
1424 notit->dscopes.event_payload);
1425 if (status < 0) {
1426 BT_LOGW("Cannot decode event payload field: "
1427 "notit-addr=%p, event-class-addr=%p, "
1428 "event-class-name=\"%s\", "
1429 "event-class-id=%" PRId64 ", fc-addr=%p",
1430 notit, notit->meta.ec,
1431 notit->meta.ec->name->str,
1432 notit->meta.ec->id,
1433 event_payload_fc);
1434 }
1435
1436 end:
1437 return status;
1438 }
1439
1440 static
1441 enum bt_notif_iter_status read_event_payload_continue_state(
1442 struct bt_notif_iter *notit)
1443 {
1444 return read_dscope_continue_state(notit, STATE_EMIT_NOTIF_EVENT);
1445 }
1446
1447 static
1448 enum bt_notif_iter_status skip_packet_padding_state(
1449 struct bt_notif_iter *notit)
1450 {
1451 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1452 size_t bits_to_skip;
1453
1454 BT_ASSERT(notit->cur_exp_packet_total_size > 0);
1455 bits_to_skip = notit->cur_exp_packet_total_size - packet_at(notit);
1456 if (bits_to_skip == 0) {
1457 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1458 goto end;
1459 } else {
1460 size_t bits_to_consume;
1461
1462 BT_LOGV("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu",
1463 bits_to_skip, notit, bits_to_skip);
1464 status = buf_ensure_available_bits(notit);
1465 if (status != BT_NOTIF_ITER_STATUS_OK) {
1466 goto end;
1467 }
1468
1469 bits_to_consume = MIN(buf_available_bits(notit), bits_to_skip);
1470 BT_LOGV("Skipping %zu bits of padding: notit-addr=%p, size=%zu",
1471 bits_to_consume, notit, bits_to_consume);
1472 buf_consume_bits(notit, bits_to_consume);
1473 bits_to_skip = notit->cur_exp_packet_total_size -
1474 packet_at(notit);
1475 if (bits_to_skip == 0) {
1476 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1477 goto end;
1478 }
1479 }
1480
1481 end:
1482 return status;
1483 }
1484
1485 static inline
1486 enum bt_notif_iter_status handle_state(struct bt_notif_iter *notit)
1487 {
1488 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1489 const enum state state = notit->state;
1490
1491 BT_LOGV("Handling state: notit-addr=%p, state=%s",
1492 notit, state_string(state));
1493
1494 // TODO: optimalize!
1495 switch (state) {
1496 case STATE_INIT:
1497 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1498 break;
1499 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
1500 status = read_packet_header_begin_state(notit);
1501 break;
1502 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
1503 status = read_packet_header_continue_state(notit);
1504 break;
1505 case STATE_AFTER_TRACE_PACKET_HEADER:
1506 status = after_packet_header_state(notit);
1507 break;
1508 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN:
1509 status = read_packet_context_begin_state(notit);
1510 break;
1511 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE:
1512 status = read_packet_context_continue_state(notit);
1513 break;
1514 case STATE_AFTER_STREAM_PACKET_CONTEXT:
1515 status = after_packet_context_state(notit);
1516 break;
1517 case STATE_EMIT_NOTIF_NEW_STREAM:
1518 notit->state = STATE_EMIT_NOTIF_NEW_PACKET;
1519 break;
1520 case STATE_EMIT_NOTIF_NEW_PACKET:
1521 notit->state = STATE_DSCOPE_EVENT_HEADER_BEGIN;
1522 break;
1523 case STATE_DSCOPE_EVENT_HEADER_BEGIN:
1524 status = read_event_header_begin_state(notit);
1525 break;
1526 case STATE_DSCOPE_EVENT_HEADER_CONTINUE:
1527 status = read_event_header_continue_state(notit);
1528 break;
1529 case STATE_AFTER_EVENT_HEADER:
1530 status = after_event_header_state(notit);
1531 break;
1532 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN:
1533 status = read_event_common_context_begin_state(notit);
1534 break;
1535 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE:
1536 status = read_event_common_context_continue_state(notit);
1537 break;
1538 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN:
1539 status = read_event_spec_context_begin_state(notit);
1540 break;
1541 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE:
1542 status = read_event_spec_context_continue_state(notit);
1543 break;
1544 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN:
1545 status = read_event_payload_begin_state(notit);
1546 break;
1547 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE:
1548 status = read_event_payload_continue_state(notit);
1549 break;
1550 case STATE_EMIT_NOTIF_EVENT:
1551 notit->state = STATE_DSCOPE_EVENT_HEADER_BEGIN;
1552 break;
1553 case STATE_SKIP_PACKET_PADDING:
1554 status = skip_packet_padding_state(notit);
1555 break;
1556 case STATE_EMIT_NOTIF_END_OF_PACKET:
1557 notit->state = STATE_SKIP_PACKET_PADDING;
1558 break;
1559 default:
1560 BT_LOGD("Unknown CTF plugin notification iterator state: "
1561 "notit-addr=%p, state=%d", notit, notit->state);
1562 abort();
1563 }
1564
1565 BT_LOGV("Handled state: notit-addr=%p, status=%s, "
1566 "prev-state=%s, cur-state=%s",
1567 notit, bt_notif_iter_status_string(status),
1568 state_string(state), state_string(notit->state));
1569 return status;
1570 }
1571
1572 /**
1573 * Resets the internal state of a CTF notification iterator.
1574 */
1575 BT_HIDDEN
1576 void bt_notif_iter_reset(struct bt_notif_iter *notit)
1577 {
1578 BT_ASSERT(notit);
1579 BT_LOGD("Resetting notification iterator: addr=%p", notit);
1580 stack_clear(notit->stack);
1581 notit->meta.sc = NULL;
1582 notit->meta.ec = NULL;
1583 BT_OBJECT_PUT_REF_AND_RESET(notit->packet);
1584 BT_OBJECT_PUT_REF_AND_RESET(notit->stream);
1585 BT_OBJECT_PUT_REF_AND_RESET(notit->event_notif);
1586 release_all_dscopes(notit);
1587 notit->cur_dscope_field = NULL;
1588
1589 if (notit->packet_header_field) {
1590 bt_packet_header_field_release(notit->packet_header_field);
1591 notit->packet_header_field = NULL;
1592 }
1593
1594 if (notit->packet_context_field) {
1595 bt_packet_context_field_release(notit->packet_context_field);
1596 notit->packet_context_field = NULL;
1597 }
1598
1599 if (notit->event_header_field) {
1600 bt_event_header_field_release(notit->event_header_field);
1601 notit->event_header_field = NULL;
1602 }
1603
1604 notit->buf.addr = NULL;
1605 notit->buf.sz = 0;
1606 notit->buf.at = 0;
1607 notit->buf.last_eh_at = SIZE_MAX;
1608 notit->buf.packet_offset = 0;
1609 notit->state = STATE_INIT;
1610 notit->cur_exp_packet_content_size = -1;
1611 notit->cur_exp_packet_total_size = -1;
1612 notit->cur_packet_offset = -1;
1613 notit->cur_stream_class_id = -1;
1614 notit->cur_event_class_id = -1;
1615 notit->cur_data_stream_id = -1;
1616 notit->stream_begin_emitted = false;
1617 }
1618
1619 static
1620 int bt_notif_iter_switch_packet(struct bt_notif_iter *notit)
1621 {
1622 int ret = 0;
1623
1624 /*
1625 * We don't put the stream class here because we need to make
1626 * sure that all the packets processed by the same notification
1627 * iterator refer to the same stream class (the first one).
1628 */
1629 BT_ASSERT(notit);
1630
1631 if (notit->cur_exp_packet_total_size != -1) {
1632 notit->cur_packet_offset += notit->cur_exp_packet_total_size;
1633 }
1634
1635 BT_LOGV("Switching packet: notit-addr=%p, cur=%zu, "
1636 "packet-offset=%" PRId64, notit, notit->buf.at,
1637 notit->cur_packet_offset);
1638 stack_clear(notit->stack);
1639 notit->meta.ec = NULL;
1640 BT_OBJECT_PUT_REF_AND_RESET(notit->packet);
1641 BT_OBJECT_PUT_REF_AND_RESET(notit->event_notif);
1642 release_all_dscopes(notit);
1643 notit->cur_dscope_field = NULL;
1644
1645 /*
1646 * Adjust current buffer so that addr points to the beginning of the new
1647 * packet.
1648 */
1649 if (notit->buf.addr) {
1650 size_t consumed_bytes = (size_t) (notit->buf.at / CHAR_BIT);
1651
1652 /* Packets are assumed to start on a byte frontier. */
1653 if (notit->buf.at % CHAR_BIT) {
1654 BT_LOGW("Cannot switch packet: current position is not a multiple of 8: "
1655 "notit-addr=%p, cur=%zu", notit, notit->buf.at);
1656 ret = -1;
1657 goto end;
1658 }
1659
1660 notit->buf.addr += consumed_bytes;
1661 notit->buf.sz -= consumed_bytes;
1662 notit->buf.at = 0;
1663 notit->buf.packet_offset = 0;
1664 BT_LOGV("Adjusted buffer: addr=%p, size=%zu",
1665 notit->buf.addr, notit->buf.sz);
1666 }
1667
1668 notit->cur_exp_packet_content_size = -1;
1669 notit->cur_exp_packet_total_size = -1;
1670 notit->cur_stream_class_id = -1;
1671 notit->cur_event_class_id = -1;
1672 notit->cur_data_stream_id = -1;
1673 notit->snapshots.discarded_events = UINT64_C(-1);
1674 notit->snapshots.packets = UINT64_C(-1);
1675 notit->snapshots.beginning_clock = UINT64_C(-1);
1676 notit->snapshots.end_clock = UINT64_C(-1);
1677
1678 end:
1679 return ret;
1680 }
1681
1682 static
1683 struct bt_field *borrow_next_field(struct bt_notif_iter *notit)
1684 {
1685 struct bt_field *next_field = NULL;
1686 struct bt_field *base_field;
1687 const struct bt_field_class *base_fc;
1688 size_t index;
1689
1690 BT_ASSERT(!stack_empty(notit->stack));
1691 index = stack_top(notit->stack)->index;
1692 base_field = stack_top(notit->stack)->base;
1693 BT_ASSERT(base_field);
1694 base_fc = bt_field_borrow_class_const(base_field);
1695 BT_ASSERT(base_fc);
1696
1697 switch (bt_field_class_get_type(base_fc)) {
1698 case BT_FIELD_CLASS_TYPE_STRUCTURE:
1699 {
1700 BT_ASSERT(index <
1701 bt_field_class_structure_get_member_count(
1702 bt_field_borrow_class_const(
1703 base_field)));
1704 next_field =
1705 bt_field_structure_borrow_member_field_by_index(
1706 base_field, index);
1707 break;
1708 }
1709 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
1710 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
1711 BT_ASSERT(index < bt_field_array_get_length(base_field));
1712 next_field = bt_field_array_borrow_element_field_by_index(
1713 base_field, index);
1714 break;
1715 case BT_FIELD_CLASS_TYPE_VARIANT:
1716 BT_ASSERT(index == 0);
1717 next_field = bt_field_variant_borrow_selected_option_field(
1718 base_field);
1719 break;
1720 default:
1721 abort();
1722 }
1723
1724 BT_ASSERT(next_field);
1725 return next_field;
1726 }
1727
1728 static
1729 void update_default_clock(struct bt_notif_iter *notit, uint64_t new_val,
1730 uint64_t new_val_size)
1731 {
1732 uint64_t new_val_mask;
1733 uint64_t cur_value_masked;
1734
1735 BT_ASSERT(new_val_size > 0);
1736
1737 /*
1738 * Special case for a 64-bit new value, which is the limit
1739 * of a clock value as of this version: overwrite the
1740 * current value directly.
1741 */
1742 if (new_val_size == 64) {
1743 notit->default_clock_val = new_val;
1744 goto end;
1745 }
1746
1747 new_val_mask = (1ULL << new_val_size) - 1;
1748 cur_value_masked = notit->default_clock_val & new_val_mask;
1749
1750 if (new_val < cur_value_masked) {
1751 /*
1752 * It looks like a wrap happened on the number of bits
1753 * of the requested new value. Assume that the clock
1754 * value wrapped only one time.
1755 */
1756 notit->default_clock_val += new_val_mask + 1;
1757 }
1758
1759 /* Clear the low bits of the current clock value. */
1760 notit->default_clock_val &= ~new_val_mask;
1761
1762 /* Set the low bits of the current clock value. */
1763 notit->default_clock_val |= new_val;
1764
1765 end:
1766 BT_LOGV("Updated default clock's value from integer field's value: "
1767 "value=%" PRIu64, notit->default_clock_val);
1768 }
1769
1770 static
1771 enum bt_bfcr_status bfcr_unsigned_int_cb(uint64_t value,
1772 struct ctf_field_class *fc, void *data)
1773 {
1774 struct bt_notif_iter *notit = data;
1775 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
1776 struct bt_field *field = NULL;
1777 struct ctf_field_class_int *int_fc = (void *) fc;
1778
1779 BT_LOGV("Unsigned integer function called from BFCR: "
1780 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1781 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64,
1782 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
1783
1784 if (likely(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE)) {
1785 goto update_def_clock;
1786 }
1787
1788 switch (int_fc->meaning) {
1789 case CTF_FIELD_CLASS_MEANING_EVENT_CLASS_ID:
1790 notit->cur_event_class_id = value;
1791 break;
1792 case CTF_FIELD_CLASS_MEANING_DATA_STREAM_ID:
1793 notit->cur_data_stream_id = value;
1794 break;
1795 case CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME:
1796 notit->snapshots.beginning_clock = value;
1797 break;
1798 case CTF_FIELD_CLASS_MEANING_PACKET_END_TIME:
1799 notit->snapshots.end_clock = value;
1800 break;
1801 case CTF_FIELD_CLASS_MEANING_STREAM_CLASS_ID:
1802 notit->cur_stream_class_id = value;
1803 break;
1804 case CTF_FIELD_CLASS_MEANING_MAGIC:
1805 if (value != 0xc1fc1fc1) {
1806 BT_LOGW("Invalid CTF magic number: notit-addr=%p, "
1807 "magic=%" PRIx64, notit, value);
1808 status = BT_BFCR_STATUS_ERROR;
1809 goto end;
1810 }
1811
1812 break;
1813 case CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT:
1814 notit->snapshots.packets = value;
1815 break;
1816 case CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT:
1817 notit->snapshots.discarded_events = value;
1818 break;
1819 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_TOTAL_SIZE:
1820 notit->cur_exp_packet_total_size = value;
1821 break;
1822 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_CONTENT_SIZE:
1823 notit->cur_exp_packet_content_size = value;
1824 break;
1825 default:
1826 abort();
1827 }
1828
1829 update_def_clock:
1830 if (unlikely(int_fc->mapped_clock_class)) {
1831 update_default_clock(notit, value, int_fc->base.size);
1832 }
1833
1834 if (unlikely(int_fc->storing_index >= 0)) {
1835 g_array_index(notit->stored_values, uint64_t,
1836 (uint64_t) int_fc->storing_index) = value;
1837 }
1838
1839 if (unlikely(!fc->in_ir)) {
1840 goto end;
1841 }
1842
1843 field = borrow_next_field(notit);
1844 BT_ASSERT(field);
1845 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
1846 BT_ASSERT(bt_field_get_class_type(field) ==
1847 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER ||
1848 bt_field_get_class_type(field) ==
1849 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION);
1850 bt_field_unsigned_integer_set_value(field, value);
1851 stack_top(notit->stack)->index++;
1852
1853 end:
1854 return status;
1855 }
1856
1857 static
1858 enum bt_bfcr_status bfcr_unsigned_int_char_cb(uint64_t value,
1859 struct ctf_field_class *fc, void *data)
1860 {
1861 int ret;
1862 struct bt_notif_iter *notit = data;
1863 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
1864 struct bt_field *string_field = NULL;
1865 struct ctf_field_class_int *int_fc = (void *) fc;
1866 char str[2] = {'\0', '\0'};
1867
1868 BT_LOGV("Unsigned integer character function called from BFCR: "
1869 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1870 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64,
1871 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
1872 BT_ASSERT(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE);
1873 BT_ASSERT(!int_fc->mapped_clock_class);
1874 BT_ASSERT(int_fc->storing_index < 0);
1875
1876 if (unlikely(!fc->in_ir)) {
1877 goto end;
1878 }
1879
1880 if (notit->done_filling_string) {
1881 goto end;
1882 }
1883
1884 if (value == 0) {
1885 notit->done_filling_string = true;
1886 goto end;
1887 }
1888
1889 string_field = stack_top(notit->stack)->base;
1890 BT_ASSERT(bt_field_get_class_type(string_field) ==
1891 BT_FIELD_CLASS_TYPE_STRING);
1892
1893 /* Append character */
1894 str[0] = (char) value;
1895 ret = bt_field_string_append_with_length(string_field, str, 1);
1896 if (ret) {
1897 BT_LOGE("Cannot append character to string field's value: "
1898 "notit-addr=%p, field-addr=%p, ret=%d",
1899 notit, string_field, ret);
1900 status = BT_BFCR_STATUS_ERROR;
1901 goto end;
1902 }
1903
1904 end:
1905 return status;
1906 }
1907
1908 static
1909 enum bt_bfcr_status bfcr_signed_int_cb(int64_t value,
1910 struct ctf_field_class *fc, void *data)
1911 {
1912 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
1913 struct bt_field *field = NULL;
1914 struct bt_notif_iter *notit = data;
1915 struct ctf_field_class_int *int_fc = (void *) fc;
1916
1917 BT_LOGV("Signed integer function called from BFCR: "
1918 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1919 "fc-type=%d, fc-in-ir=%d, value=%" PRId64,
1920 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
1921 BT_ASSERT(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE);
1922
1923 if (unlikely(int_fc->storing_index >= 0)) {
1924 g_array_index(notit->stored_values, uint64_t,
1925 (uint64_t) int_fc->storing_index) = (uint64_t) value;
1926 }
1927
1928 if (unlikely(!fc->in_ir)) {
1929 goto end;
1930 }
1931
1932 field = borrow_next_field(notit);
1933 BT_ASSERT(field);
1934 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
1935 BT_ASSERT(bt_field_get_class_type(field) ==
1936 BT_FIELD_CLASS_TYPE_SIGNED_INTEGER ||
1937 bt_field_get_class_type(field) ==
1938 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION);
1939 bt_field_signed_integer_set_value(field, value);
1940 stack_top(notit->stack)->index++;
1941
1942 end:
1943 return status;
1944 }
1945
1946 static
1947 enum bt_bfcr_status bfcr_floating_point_cb(double value,
1948 struct ctf_field_class *fc, void *data)
1949 {
1950 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
1951 struct bt_field *field = NULL;
1952 struct bt_notif_iter *notit = data;
1953
1954 BT_LOGV("Floating point number function called from BFCR: "
1955 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1956 "fc-type=%d, fc-in-ir=%d, value=%f",
1957 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
1958 BT_ASSERT(fc->in_ir);
1959 field = borrow_next_field(notit);
1960 BT_ASSERT(field);
1961 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
1962 BT_ASSERT(bt_field_get_class_type(field) ==
1963 BT_FIELD_CLASS_TYPE_REAL);
1964 bt_field_real_set_value(field, value);
1965 stack_top(notit->stack)->index++;
1966 return status;
1967 }
1968
1969 static
1970 enum bt_bfcr_status bfcr_string_begin_cb(
1971 struct ctf_field_class *fc, void *data)
1972 {
1973 struct bt_field *field = NULL;
1974 struct bt_notif_iter *notit = data;
1975 int ret;
1976
1977 BT_LOGV("String (beginning) function called from BFCR: "
1978 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
1979 "fc-type=%d, fc-in-ir=%d",
1980 notit, notit->bfcr, fc, fc->type, fc->in_ir);
1981
1982 BT_ASSERT(fc->in_ir);
1983 field = borrow_next_field(notit);
1984 BT_ASSERT(field);
1985 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
1986 BT_ASSERT(bt_field_get_class_type(field) ==
1987 BT_FIELD_CLASS_TYPE_STRING);
1988 ret = bt_field_string_clear(field);
1989 BT_ASSERT(ret == 0);
1990
1991 /*
1992 * Push on stack. Not a compound class per se, but we know that
1993 * only bfcr_string_cb() may be called between this call and a
1994 * subsequent call to bfcr_string_end_cb().
1995 */
1996 stack_push(notit->stack, field);
1997 return BT_BFCR_STATUS_OK;
1998 }
1999
2000 static
2001 enum bt_bfcr_status bfcr_string_cb(const char *value,
2002 size_t len, struct ctf_field_class *fc, void *data)
2003 {
2004 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
2005 struct bt_field *field = NULL;
2006 struct bt_notif_iter *notit = data;
2007 int ret;
2008
2009 BT_LOGV("String (substring) function called from BFCR: "
2010 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2011 "fc-type=%d, fc-in-ir=%d, string-length=%zu",
2012 notit, notit->bfcr, fc, fc->type, fc->in_ir,
2013 len);
2014 BT_ASSERT(fc->in_ir);
2015 field = stack_top(notit->stack)->base;
2016 BT_ASSERT(field);
2017
2018 /* Append current substring */
2019 ret = bt_field_string_append_with_length(field, value, len);
2020 if (ret) {
2021 BT_LOGE("Cannot append substring to string field's value: "
2022 "notit-addr=%p, field-addr=%p, string-length=%zu, "
2023 "ret=%d", notit, field, len, ret);
2024 status = BT_BFCR_STATUS_ERROR;
2025 goto end;
2026 }
2027
2028 end:
2029 return status;
2030 }
2031
2032 static
2033 enum bt_bfcr_status bfcr_string_end_cb(
2034 struct ctf_field_class *fc, void *data)
2035 {
2036 struct bt_notif_iter *notit = data;
2037
2038 BT_LOGV("String (end) function called from BFCR: "
2039 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2040 "fc-type=%d, fc-in-ir=%d",
2041 notit, notit->bfcr, fc, fc->type, fc->in_ir);
2042 BT_ASSERT(fc->in_ir);
2043
2044 /* Pop string field */
2045 stack_pop(notit->stack);
2046
2047 /* Go to next field */
2048 stack_top(notit->stack)->index++;
2049 return BT_BFCR_STATUS_OK;
2050 }
2051
2052 enum bt_bfcr_status bfcr_compound_begin_cb(
2053 struct ctf_field_class *fc, void *data)
2054 {
2055 struct bt_notif_iter *notit = data;
2056 struct bt_field *field;
2057
2058 BT_LOGV("Compound (beginning) function called from BFCR: "
2059 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2060 "fc-type=%d, fc-in-ir=%d",
2061 notit, notit->bfcr, fc, fc->type, fc->in_ir);
2062
2063 if (!fc->in_ir) {
2064 goto end;
2065 }
2066
2067 /* Borrow field */
2068 if (stack_empty(notit->stack)) {
2069 /* Root: already set by read_dscope_begin_state() */
2070 field = notit->cur_dscope_field;
2071 } else {
2072 field = borrow_next_field(notit);
2073 BT_ASSERT(field);
2074 }
2075
2076 /* Push field */
2077 BT_ASSERT(field);
2078 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
2079 stack_push(notit->stack, field);
2080
2081 /*
2082 * Change BFCR "unsigned int" callback if it's a text
2083 * array/sequence.
2084 */
2085 if (fc->type == CTF_FIELD_CLASS_TYPE_ARRAY ||
2086 fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE) {
2087 struct ctf_field_class_array_base *array_fc = (void *) fc;
2088
2089 if (array_fc->is_text) {
2090 int ret;
2091
2092 BT_ASSERT(bt_field_get_class_type(field) ==
2093 BT_FIELD_CLASS_TYPE_STRING);
2094 notit->done_filling_string = false;
2095 ret = bt_field_string_clear(field);
2096 BT_ASSERT(ret == 0);
2097 bt_bfcr_set_unsigned_int_cb(notit->bfcr,
2098 bfcr_unsigned_int_char_cb);
2099 }
2100 }
2101
2102 end:
2103 return BT_BFCR_STATUS_OK;
2104 }
2105
2106 enum bt_bfcr_status bfcr_compound_end_cb(
2107 struct ctf_field_class *fc, void *data)
2108 {
2109 struct bt_notif_iter *notit = data;
2110
2111 BT_LOGV("Compound (end) function called from BFCR: "
2112 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
2113 "fc-type=%d, fc-in-ir=%d",
2114 notit, notit->bfcr, fc, fc->type, fc->in_ir);
2115
2116 if (!fc->in_ir) {
2117 goto end;
2118 }
2119
2120 BT_ASSERT(!stack_empty(notit->stack));
2121 BT_ASSERT(bt_field_borrow_class_const(stack_top(notit->stack)->base) ==
2122 fc->ir_fc);
2123
2124 /*
2125 * Reset BFCR "unsigned int" callback if it's a text
2126 * array/sequence.
2127 */
2128 if (fc->type == CTF_FIELD_CLASS_TYPE_ARRAY ||
2129 fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE) {
2130 struct ctf_field_class_array_base *array_fc = (void *) fc;
2131
2132 if (array_fc->is_text) {
2133 BT_ASSERT(bt_field_get_class_type(
2134 stack_top(notit->stack)->base) ==
2135 BT_FIELD_CLASS_TYPE_STRING);
2136 bt_bfcr_set_unsigned_int_cb(notit->bfcr,
2137 bfcr_unsigned_int_cb);
2138 }
2139 }
2140
2141 /* Pop stack */
2142 stack_pop(notit->stack);
2143
2144 /* If the stack is not empty, increment the base's index */
2145 if (!stack_empty(notit->stack)) {
2146 stack_top(notit->stack)->index++;
2147 }
2148
2149 end:
2150 return BT_BFCR_STATUS_OK;
2151 }
2152
2153 static
2154 int64_t bfcr_get_sequence_length_cb(struct ctf_field_class *fc, void *data)
2155 {
2156 struct bt_field *seq_field;
2157 struct bt_notif_iter *notit = data;
2158 struct ctf_field_class_sequence *seq_fc = (void *) fc;
2159 int64_t length = -1;
2160 int ret;
2161
2162 length = (uint64_t) g_array_index(notit->stored_values, uint64_t,
2163 seq_fc->stored_length_index);
2164 seq_field = stack_top(notit->stack)->base;
2165 BT_ASSERT(seq_field);
2166 ret = bt_field_dynamic_array_set_length(seq_field, (uint64_t) length);
2167 if (ret) {
2168 BT_LOGE("Cannot set dynamic array field's length field: "
2169 "notit-addr=%p, field-addr=%p, "
2170 "length=%" PRIu64, notit, seq_field, length);
2171 }
2172
2173 return length;
2174 }
2175
2176 static
2177 struct ctf_field_class *bfcr_borrow_variant_selected_field_class_cb(
2178 struct ctf_field_class *fc, void *data)
2179 {
2180 int ret;
2181 uint64_t i;
2182 int64_t option_index = -1;
2183 struct bt_notif_iter *notit = data;
2184 struct ctf_field_class_variant *var_fc = (void *) fc;
2185 struct ctf_named_field_class *selected_option = NULL;
2186 struct ctf_field_class *ret_fc = NULL;
2187 union {
2188 uint64_t u;
2189 int64_t i;
2190 } tag;
2191
2192 /* Get variant's tag */
2193 tag.u = g_array_index(notit->stored_values, uint64_t,
2194 var_fc->stored_tag_index);
2195
2196 /*
2197 * Check each range to find the selected option's index.
2198 */
2199 if (var_fc->tag_fc->base.is_signed) {
2200 for (i = 0; i < var_fc->ranges->len; i++) {
2201 struct ctf_field_class_variant_range *range =
2202 ctf_field_class_variant_borrow_range_by_index(
2203 var_fc, i);
2204
2205 if (tag.i >= range->range.lower.i &&
2206 tag.i <= range->range.upper.i) {
2207 option_index = (int64_t) range->option_index;
2208 break;
2209 }
2210 }
2211 } else {
2212 for (i = 0; i < var_fc->ranges->len; i++) {
2213 struct ctf_field_class_variant_range *range =
2214 ctf_field_class_variant_borrow_range_by_index(
2215 var_fc, i);
2216
2217 if (tag.u >= range->range.lower.u &&
2218 tag.u <= range->range.upper.u) {
2219 option_index = (int64_t) range->option_index;
2220 break;
2221 }
2222 }
2223 }
2224
2225 if (option_index < 0) {
2226 BT_LOGW("Cannot find variant field class's option: "
2227 "notit-addr=%p, var-fc-addr=%p, u-tag=%" PRIu64 ", "
2228 "i-tag=%" PRId64, notit, var_fc, tag.u, tag.i);
2229 goto end;
2230 }
2231
2232 selected_option = ctf_field_class_variant_borrow_option_by_index(
2233 var_fc, (uint64_t) option_index);
2234
2235 if (selected_option->fc->in_ir) {
2236 struct bt_field *var_field = stack_top(notit->stack)->base;
2237
2238 ret = bt_field_variant_select_option_field(
2239 var_field, option_index);
2240 if (ret) {
2241 BT_LOGW("Cannot select variant field's option field: "
2242 "notit-addr=%p, var-field-addr=%p, "
2243 "opt-index=%" PRId64, notit, var_field,
2244 option_index);
2245 goto end;
2246 }
2247 }
2248
2249 ret_fc = selected_option->fc;
2250
2251 end:
2252 return ret_fc;
2253 }
2254
2255 static
2256 void set_event_default_clock_value(struct bt_notif_iter *notit)
2257 {
2258 struct bt_event *event =
2259 bt_private_notification_event_borrow_event(
2260 notit->event_notif);
2261 struct bt_stream_class *sc = notit->meta.sc->ir_sc;
2262
2263 BT_ASSERT(event);
2264
2265 if (bt_stream_class_borrow_default_clock_class(sc)) {
2266 bt_event_set_default_clock_value(event,
2267 notit->default_clock_val);
2268 }
2269 }
2270
2271 static
2272 void notify_new_stream(struct bt_notif_iter *notit,
2273 struct bt_private_notification **notification)
2274 {
2275 enum bt_notif_iter_status status;
2276 struct bt_private_notification *ret = NULL;
2277
2278 status = set_current_stream(notit);
2279 if (status != BT_NOTIF_ITER_STATUS_OK) {
2280 BT_OBJECT_PUT_REF_AND_RESET(ret);
2281 goto end;
2282 }
2283
2284 BT_ASSERT(notit->stream);
2285 BT_ASSERT(notit->notif_iter);
2286 ret = bt_private_notification_stream_begin_create(notit->notif_iter,
2287 notit->stream);
2288 if (!ret) {
2289 BT_LOGE("Cannot create stream beginning notification: "
2290 "notit-addr=%p, stream-addr=%p",
2291 notit, notit->stream);
2292 return;
2293 }
2294
2295 end:
2296 *notification = ret;
2297 }
2298
2299 static
2300 void notify_end_of_stream(struct bt_notif_iter *notit,
2301 struct bt_private_notification **notification)
2302 {
2303 struct bt_private_notification *ret;
2304
2305 if (!notit->stream) {
2306 BT_LOGE("Cannot create stream for stream notification: "
2307 "notit-addr=%p", notit);
2308 return;
2309 }
2310
2311 BT_ASSERT(notit->notif_iter);
2312 ret = bt_private_notification_stream_end_create(notit->notif_iter,
2313 notit->stream);
2314 if (!ret) {
2315 BT_LOGE("Cannot create stream beginning notification: "
2316 "notit-addr=%p, stream-addr=%p",
2317 notit, notit->stream);
2318 return;
2319 }
2320 *notification = ret;
2321 }
2322
2323 static
2324 void notify_new_packet(struct bt_notif_iter *notit,
2325 struct bt_private_notification **notification)
2326 {
2327 int ret;
2328 enum bt_notif_iter_status status;
2329 struct bt_private_notification *notif = NULL;
2330 const struct bt_stream_class *sc;
2331
2332 status = set_current_packet(notit);
2333 if (status != BT_NOTIF_ITER_STATUS_OK) {
2334 goto end;
2335 }
2336
2337 BT_ASSERT(notit->packet);
2338 sc = notit->meta.sc->ir_sc;
2339 BT_ASSERT(sc);
2340
2341 if (bt_stream_class_packets_have_discarded_event_counter_snapshot(sc)) {
2342 BT_ASSERT(notit->snapshots.discarded_events != UINT64_C(-1));
2343 bt_packet_set_discarded_event_counter_snapshot(
2344 notit->packet, notit->snapshots.discarded_events);
2345 }
2346
2347 if (bt_stream_class_packets_have_packet_counter_snapshot(sc)) {
2348 BT_ASSERT(notit->snapshots.packets != UINT64_C(-1));
2349 bt_packet_set_packet_counter_snapshot(
2350 notit->packet, notit->snapshots.packets);
2351 }
2352
2353 if (bt_stream_class_packets_have_default_beginning_clock_value(sc)) {
2354 BT_ASSERT(notit->snapshots.beginning_clock != UINT64_C(-1));
2355 bt_packet_set_default_beginning_clock_value(
2356 notit->packet, notit->snapshots.beginning_clock);
2357 }
2358
2359 if (bt_stream_class_packets_have_default_end_clock_value(sc)) {
2360 BT_ASSERT(notit->snapshots.end_clock != UINT64_C(-1));
2361 bt_packet_set_default_end_clock_value(
2362 notit->packet, notit->snapshots.end_clock);
2363 }
2364
2365 if (notit->packet_header_field) {
2366 ret = bt_packet_move_header_field(
2367 notit->packet, notit->packet_header_field);
2368 if (ret) {
2369 goto end;
2370 }
2371
2372 notit->packet_header_field = NULL;
2373
2374 /*
2375 * At this point notit->dscopes.trace_packet_header has
2376 * the same value as the packet header field within
2377 * notit->packet.
2378 */
2379 BT_ASSERT(bt_packet_borrow_header_field(
2380 notit->packet) ==
2381 notit->dscopes.trace_packet_header);
2382 }
2383
2384 if (notit->packet_context_field) {
2385 ret = bt_packet_move_context_field(
2386 notit->packet, notit->packet_context_field);
2387 if (ret) {
2388 goto end;
2389 }
2390
2391 notit->packet_context_field = NULL;
2392
2393 /*
2394 * At this point notit->dscopes.trace_packet_header has
2395 * the same value as the packet header field within
2396 * notit->packet.
2397 */
2398 BT_ASSERT(bt_packet_borrow_context_field(
2399 notit->packet) ==
2400 notit->dscopes.stream_packet_context);
2401 }
2402
2403 BT_ASSERT(notit->notif_iter);
2404 notif = bt_private_notification_packet_begin_create(notit->notif_iter,
2405 notit->packet);
2406 if (!notif) {
2407 BT_LOGE("Cannot create packet beginning notification: "
2408 "notit-addr=%p, packet-addr=%p",
2409 notit, notit->packet);
2410 goto end;
2411 }
2412
2413 *notification = notif;
2414
2415 end:
2416 return;
2417 }
2418
2419 static
2420 void notify_end_of_packet(struct bt_notif_iter *notit,
2421 struct bt_private_notification **notification)
2422 {
2423 struct bt_private_notification *notif;
2424
2425 if (!notit->packet) {
2426 return;
2427 }
2428
2429 /* Update default clock from packet's end time */
2430 if (notit->snapshots.end_clock != UINT64_C(-1)) {
2431 notit->default_clock_val = notit->snapshots.end_clock;
2432 }
2433
2434 BT_ASSERT(notit->notif_iter);
2435 notif = bt_private_notification_packet_end_create(notit->notif_iter,
2436 notit->packet);
2437 if (!notif) {
2438 BT_LOGE("Cannot create packet end notification: "
2439 "notit-addr=%p, packet-addr=%p",
2440 notit, notit->packet);
2441 return;
2442
2443 }
2444
2445 BT_OBJECT_PUT_REF_AND_RESET(notit->packet);
2446 *notification = notif;
2447 }
2448
2449 BT_HIDDEN
2450 struct bt_notif_iter *bt_notif_iter_create(struct ctf_trace_class *tc,
2451 size_t max_request_sz,
2452 struct bt_notif_iter_medium_ops medops, void *data)
2453 {
2454 struct bt_notif_iter *notit = NULL;
2455 struct bt_bfcr_cbs cbs = {
2456 .classes = {
2457 .signed_int = bfcr_signed_int_cb,
2458 .unsigned_int = bfcr_unsigned_int_cb,
2459 .floating_point = bfcr_floating_point_cb,
2460 .string_begin = bfcr_string_begin_cb,
2461 .string = bfcr_string_cb,
2462 .string_end = bfcr_string_end_cb,
2463 .compound_begin = bfcr_compound_begin_cb,
2464 .compound_end = bfcr_compound_end_cb,
2465 },
2466 .query = {
2467 .get_sequence_length = bfcr_get_sequence_length_cb,
2468 .borrow_variant_selected_field_class = bfcr_borrow_variant_selected_field_class_cb,
2469 },
2470 };
2471
2472 BT_ASSERT(tc);
2473 BT_ASSERT(medops.request_bytes);
2474 BT_ASSERT(medops.borrow_stream);
2475 BT_LOGD("Creating CTF plugin notification iterator: "
2476 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
2477 "data=%p", tc, tc->name->str, max_request_sz, data);
2478 notit = g_new0(struct bt_notif_iter, 1);
2479 if (!notit) {
2480 BT_LOGE_STR("Failed to allocate one CTF plugin notification iterator.");
2481 goto end;
2482 }
2483 notit->meta.tc = tc;
2484 notit->medium.medops = medops;
2485 notit->medium.max_request_sz = max_request_sz;
2486 notit->medium.data = data;
2487 notit->stack = stack_new(notit);
2488 notit->stored_values = g_array_new(FALSE, TRUE, sizeof(uint64_t));
2489 g_array_set_size(notit->stored_values, tc->stored_value_count);
2490
2491 if (!notit->stack) {
2492 BT_LOGE_STR("Failed to create field stack.");
2493 goto error;
2494 }
2495
2496 notit->bfcr = bt_bfcr_create(cbs, notit);
2497 if (!notit->bfcr) {
2498 BT_LOGE_STR("Failed to create binary class reader (BFCR).");
2499 goto error;
2500 }
2501
2502 bt_notif_iter_reset(notit);
2503 BT_LOGD("Created CTF plugin notification iterator: "
2504 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
2505 "data=%p, notit-addr=%p",
2506 tc, tc->name->str, max_request_sz, data,
2507 notit);
2508 notit->cur_packet_offset = 0;
2509
2510 end:
2511 return notit;
2512
2513 error:
2514 bt_notif_iter_destroy(notit);
2515 notit = NULL;
2516 goto end;
2517 }
2518
2519 void bt_notif_iter_destroy(struct bt_notif_iter *notit)
2520 {
2521 BT_OBJECT_PUT_REF_AND_RESET(notit->packet);
2522 BT_OBJECT_PUT_REF_AND_RESET(notit->stream);
2523 release_all_dscopes(notit);
2524
2525 BT_LOGD("Destroying CTF plugin notification iterator: addr=%p", notit);
2526
2527 if (notit->stack) {
2528 BT_LOGD_STR("Destroying field stack.");
2529 stack_destroy(notit->stack);
2530 }
2531
2532 if (notit->bfcr) {
2533 BT_LOGD("Destroying BFCR: bfcr-addr=%p", notit->bfcr);
2534 bt_bfcr_destroy(notit->bfcr);
2535 }
2536
2537 if (notit->stored_values) {
2538 g_array_free(notit->stored_values, TRUE);
2539 }
2540
2541 g_free(notit);
2542 }
2543
2544 enum bt_notif_iter_status bt_notif_iter_get_next_notification(
2545 struct bt_notif_iter *notit,
2546 struct bt_self_notification_iterator *notif_iter,
2547 struct bt_private_notification **notification)
2548 {
2549 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
2550
2551 BT_ASSERT(notit);
2552 BT_ASSERT(notification);
2553
2554 if (notit->state == STATE_DONE) {
2555 status = BT_NOTIF_ITER_STATUS_EOF;
2556 goto end;
2557 }
2558
2559 notit->notif_iter = notif_iter;
2560
2561 BT_LOGV("Getting next notification: notit-addr=%p", notit);
2562
2563 while (true) {
2564 status = handle_state(notit);
2565 if (status == BT_NOTIF_ITER_STATUS_AGAIN) {
2566 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_AGAIN.");
2567 goto end;
2568 }
2569
2570 if (status != BT_NOTIF_ITER_STATUS_OK) {
2571 if (status == BT_NOTIF_ITER_STATUS_EOF) {
2572 enum state next_state = notit->state;
2573
2574 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_EOF.");
2575
2576 if (notit->packet) {
2577 notify_end_of_packet(notit,
2578 notification);
2579 } else {
2580 notify_end_of_stream(notit,
2581 notification);
2582 next_state = STATE_DONE;
2583 }
2584
2585 if (!*notification) {
2586 status = BT_NOTIF_ITER_STATUS_ERROR;
2587 goto end;
2588 }
2589
2590 status = BT_NOTIF_ITER_STATUS_OK;
2591 notit->state = next_state;
2592 } else {
2593 BT_LOGW("Cannot handle state: "
2594 "notit-addr=%p, state=%s",
2595 notit, state_string(notit->state));
2596 }
2597
2598 goto end;
2599 }
2600
2601 switch (notit->state) {
2602 case STATE_EMIT_NOTIF_NEW_STREAM:
2603 /* notify_new_stream() logs errors */
2604 notify_new_stream(notit, notification);
2605
2606 if (!*notification) {
2607 status = BT_NOTIF_ITER_STATUS_ERROR;
2608 }
2609
2610 notit->stream_begin_emitted = true;
2611 goto end;
2612 case STATE_EMIT_NOTIF_NEW_PACKET:
2613 /* notify_new_packet() logs errors */
2614 notify_new_packet(notit, notification);
2615
2616 if (!*notification) {
2617 status = BT_NOTIF_ITER_STATUS_ERROR;
2618 }
2619
2620 goto end;
2621 case STATE_EMIT_NOTIF_EVENT:
2622 BT_ASSERT(notit->event_notif);
2623 set_event_default_clock_value(notit);
2624 *notification = notit->event_notif;
2625 notit->event_notif = NULL;
2626 goto end;
2627 case STATE_EMIT_NOTIF_END_OF_PACKET:
2628 /* notify_end_of_packet() logs errors */
2629 notify_end_of_packet(notit, notification);
2630
2631 if (!*notification) {
2632 status = BT_NOTIF_ITER_STATUS_ERROR;
2633 }
2634
2635 goto end;
2636 default:
2637 /* Non-emitting state: continue */
2638 break;
2639 }
2640 }
2641
2642 end:
2643 return status;
2644 }
2645
2646 BT_HIDDEN
2647 enum bt_notif_iter_status bt_notif_iter_borrow_packet_header_context_fields(
2648 struct bt_notif_iter *notit,
2649 struct bt_field **packet_header_field,
2650 struct bt_field **packet_context_field)
2651 {
2652 int ret;
2653 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
2654
2655 BT_ASSERT(notit);
2656
2657 if (notit->state == STATE_EMIT_NOTIF_NEW_PACKET) {
2658 /* We're already there */
2659 goto set_fields;
2660 }
2661
2662 while (true) {
2663 status = handle_state(notit);
2664 if (status == BT_NOTIF_ITER_STATUS_AGAIN) {
2665 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_AGAIN.");
2666 goto end;
2667 }
2668 if (status != BT_NOTIF_ITER_STATUS_OK) {
2669 if (status == BT_NOTIF_ITER_STATUS_EOF) {
2670 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_EOF.");
2671 } else {
2672 BT_LOGW("Cannot handle state: "
2673 "notit-addr=%p, state=%s",
2674 notit, state_string(notit->state));
2675 }
2676 goto end;
2677 }
2678
2679 switch (notit->state) {
2680 case STATE_EMIT_NOTIF_NEW_PACKET:
2681 /*
2682 * Packet header and context fields are
2683 * potentially decoded (or they don't exist).
2684 */
2685 goto set_fields;
2686 case STATE_INIT:
2687 case STATE_EMIT_NOTIF_NEW_STREAM:
2688 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
2689 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
2690 case STATE_AFTER_TRACE_PACKET_HEADER:
2691 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN:
2692 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE:
2693 case STATE_AFTER_STREAM_PACKET_CONTEXT:
2694 /* Non-emitting state: continue */
2695 break;
2696 default:
2697 /*
2698 * We should never get past the
2699 * STATE_EMIT_NOTIF_NEW_PACKET state.
2700 */
2701 BT_LOGF("Unexpected state: notit-addr=%p, state=%s",
2702 notit, state_string(notit->state));
2703 abort();
2704 }
2705 }
2706
2707 set_fields:
2708 ret = set_current_packet_content_sizes(notit);
2709 if (ret) {
2710 status = BT_NOTIF_ITER_STATUS_ERROR;
2711 goto end;
2712 }
2713
2714 if (packet_header_field) {
2715 *packet_header_field = notit->dscopes.trace_packet_header;
2716 }
2717
2718 if (packet_context_field) {
2719 *packet_context_field = notit->dscopes.stream_packet_context;
2720 }
2721
2722 end:
2723 return status;
2724 }
2725
2726 BT_HIDDEN
2727 void bt_notif_iter_set_medops_data(struct bt_notif_iter *notit,
2728 void *medops_data)
2729 {
2730 BT_ASSERT(notit);
2731 notit->medium.data = medops_data;
2732 }
2733
2734 BT_HIDDEN
2735 enum bt_notif_iter_status bt_notif_iter_seek(
2736 struct bt_notif_iter *notit, off_t offset)
2737 {
2738 enum bt_notif_iter_status ret = BT_NOTIF_ITER_STATUS_OK;
2739 enum bt_notif_iter_medium_status medium_status;
2740
2741 BT_ASSERT(notit);
2742 if (offset < 0) {
2743 BT_LOGE("Cannot seek to negative offset: offset=%jd", offset);
2744 ret = BT_NOTIF_ITER_STATUS_INVAL;
2745 goto end;
2746 }
2747
2748 if (!notit->medium.medops.seek) {
2749 ret = BT_NOTIF_ITER_STATUS_UNSUPPORTED;
2750 BT_LOGD("Aborting seek as the iterator's underlying media does not implement seek support.");
2751 goto end;
2752 }
2753
2754 medium_status = notit->medium.medops.seek(
2755 BT_NOTIF_ITER_SEEK_WHENCE_SET, offset, notit->medium.data);
2756 if (medium_status != BT_NOTIF_ITER_MEDIUM_STATUS_OK) {
2757 if (medium_status == BT_NOTIF_ITER_MEDIUM_STATUS_EOF) {
2758 ret = BT_NOTIF_ITER_STATUS_EOF;
2759 } else {
2760 ret = BT_NOTIF_ITER_STATUS_ERROR;
2761 goto end;
2762 }
2763 }
2764
2765 bt_notif_iter_reset(notit);
2766 notit->cur_packet_offset = offset;
2767
2768 end:
2769 return ret;
2770 }
2771
2772 BT_HIDDEN
2773 off_t bt_notif_iter_get_current_packet_offset(struct bt_notif_iter *notit)
2774 {
2775 BT_ASSERT(notit);
2776 return notit->cur_packet_offset;
2777 }
2778
2779 BT_HIDDEN
2780 off_t bt_notif_iter_get_current_packet_size(
2781 struct bt_notif_iter *notit)
2782 {
2783 BT_ASSERT(notit);
2784 return notit->cur_exp_packet_total_size;
2785 }
2786
2787 BT_HIDDEN
2788 void bt_notif_trace_class_changed(struct bt_notif_iter *notit)
2789 {
2790 if (notit->meta.tc->stored_value_count > notit->stored_values->len) {
2791 g_array_set_size(notit->stored_values,
2792 notit->meta.tc->stored_value_count);
2793 }
2794 }
2795
2796 BT_HIDDEN
2797 enum bt_notif_iter_status bt_notif_iter_get_packet_properties(
2798 struct bt_notif_iter *notit,
2799 struct bt_notif_iter_packet_properties *props)
2800 {
2801 BT_ASSERT(notit);
2802 BT_ASSERT(props);
2803
2804 props->exp_packet_total_size =
2805 (uint64_t) notit->cur_exp_packet_total_size;
2806 props->exp_packet_content_size =
2807 (uint64_t) notit->cur_exp_packet_content_size;
2808 BT_ASSERT(props->stream_class_id >= 0);
2809 props->stream_class_id = (uint64_t) notit->cur_stream_class_id;
2810 props->data_stream_id = notit->cur_data_stream_id;
2811 props->snapshots.discarded_events = notit->snapshots.discarded_events;
2812 props->snapshots.packets = notit->snapshots.packets;
2813 props->snapshots.beginning_clock = notit->snapshots.beginning_clock;
2814 props->snapshots.end_clock = notit->snapshots.end_clock;
2815 return BT_NOTIF_ITER_STATUS_OK;
2816 }
This page took 0.101178 seconds and 4 git commands to generate.