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