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