lib: use priv connection priv notif iterator to create notif, not graph
[babeltrace.git] / plugins / ctf / common / notif-iter / notif-iter.c
1 /*
2 * Babeltrace - CTF notification iterator
3 *
4 * Copyright (c) 2015-2016 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2015-2016 Philippe Proulx <pproulx@efficios.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26 #define BT_LOG_TAG "PLUGIN-CTF-NOTIF-ITER"
27 #include "logging.h"
28
29 #include <stdint.h>
30 #include <inttypes.h>
31 #include <stdio.h>
32 #include <stddef.h>
33 #include <stdbool.h>
34 #include <babeltrace/assert-internal.h>
35 #include <string.h>
36 #include <babeltrace/babeltrace.h>
37 #include <babeltrace/common-internal.h>
38 #include <glib.h>
39 #include <stdlib.h>
40
41 #include "notif-iter.h"
42 #include "../btr/btr.h"
43
44 struct bt_notif_iter;
45
46 /* A visit stack entry */
47 struct stack_entry {
48 /*
49 * Current base field, one of:
50 *
51 * * string
52 * * structure
53 * * array
54 * * sequence
55 * * variant
56 *
57 * Field is borrowed.
58 */
59 struct bt_field *base;
60
61 /* index of next field to set */
62 size_t index;
63 };
64
65 /* Visit stack */
66 struct stack {
67 /* Entries (struct stack_entry) */
68 GArray *entries;
69
70 /* Number of active entries */
71 size_t size;
72 };
73
74 /* State */
75 enum state {
76 STATE_INIT,
77 STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN,
78 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE,
79 STATE_AFTER_TRACE_PACKET_HEADER,
80 STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN,
81 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE,
82 STATE_AFTER_STREAM_PACKET_CONTEXT,
83 STATE_EMIT_NOTIF_NEW_STREAM,
84 STATE_EMIT_NOTIF_NEW_PACKET,
85 STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN,
86 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE,
87 STATE_AFTER_STREAM_EVENT_HEADER,
88 STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN,
89 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE,
90 STATE_DSCOPE_EVENT_CONTEXT_BEGIN,
91 STATE_DSCOPE_EVENT_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 struct trace_field_path_cache {
101 /*
102 * Indexes of the stream_id and stream_instance_id field in the packet
103 * header structure, -1 if unset.
104 */
105 int stream_id;
106 int stream_instance_id;
107 };
108
109 struct stream_class_field_path_cache {
110 /*
111 * Indexes of the v and id fields in the stream event header structure,
112 * -1 if unset.
113 */
114 int v;
115 int id;
116
117 /*
118 * index of the timestamp_end, packet_size and content_size fields in
119 * the stream packet context structure. Set to -1 if the fields were
120 * not found.
121 */
122 int timestamp_end;
123 int packet_size;
124 int content_size;
125 };
126
127 struct field_cb_override {
128 enum bt_btr_status (* func)(void *value,
129 struct bt_field_type *type, void *data);
130 void *data;
131 };
132
133 /* Clock value: clock class and raw value */
134 struct clock_value {
135 struct bt_clock_class *clock_class; /* Weak */
136 uint64_t raw_value;
137 };
138
139 /* CTF notification iterator */
140 struct bt_notif_iter {
141 /* Visit stack */
142 struct stack *stack;
143
144 /* Current notification iterator to create notifications (weak) */
145 struct bt_private_connection_private_notification_iterator *notif_iter;
146
147 /*
148 * Current dynamic scope field pointer.
149 *
150 * This is set by read_dscope_begin_state() and contains the
151 * value of one of the pointers in `dscopes` below.
152 */
153 struct bt_field *cur_dscope_field;
154
155 /* Trace and classes (owned by this) */
156 struct {
157 struct bt_trace *trace;
158 struct bt_stream_class *stream_class;
159 struct bt_event_class *event_class;
160 } meta;
161
162 /* Current packet header field wrapper (NULL if not created yet) */
163 struct bt_packet_header_field *packet_header_field;
164
165 /* Current packet header field wrapper (NULL if not created yet) */
166 struct bt_packet_context_field *packet_context_field;
167
168 /* Current event header field (NULL if not created yet) */
169 struct bt_event_header_field *event_header_field;
170
171 /* Current packet (NULL if not created yet) */
172 struct bt_packet *packet;
173
174 /* Current stream (NULL if not set yet) */
175 struct bt_stream *stream;
176
177 /* Current event (NULL if not created yet) */
178 struct bt_event *event;
179
180 /* Current event notification (NULL if not created yet) */
181 struct bt_notification *event_notif;
182
183 /*
184 * Current `timestamp_end` field (to consider before switching
185 * packets). If it's set, it's a field which is within
186 * `dscopes.stream_packet_context` below, which is in `packet`
187 * above.
188 */
189 struct bt_field *cur_timestamp_end;
190
191 /* Database of current dynamic scopes (owned by this) */
192 struct {
193 struct bt_field *trace_packet_header;
194 struct bt_field *stream_packet_context;
195 struct bt_field *stream_event_header;
196 struct bt_field *stream_event_context;
197 struct bt_field *event_context;
198 struct bt_field *event_payload;
199 } dscopes;
200
201 /*
202 * Special field overrides.
203 *
204 * Overrides are used to implement the behaviours of special fields such
205 * as "timestamp_end" (which must be ignored until the end of the
206 * packet), "id" (event id) which can be present multiple times and must
207 * be updated multiple time.
208 *
209 * This should be used to implement the behaviour of integer fields
210 * mapped to clocks and other "tagged" fields (in CTF 2).
211 *
212 * bt_field_type to struct field_cb_override
213 */
214 GHashTable *field_overrides;
215
216 /* Current state */
217 enum state state;
218
219 /* Current medium buffer data */
220 struct {
221 /* Last address provided by medium */
222 const uint8_t *addr;
223
224 /* Buffer size provided by medium (bytes) */
225 size_t sz;
226
227 /* Offset within whole packet of addr (bits) */
228 size_t packet_offset;
229
230 /* Current position from addr (bits) */
231 size_t at;
232
233 /* Position of the last event header from addr (bits) */
234 size_t last_eh_at;
235 } buf;
236
237 /* Binary type reader */
238 struct bt_btr *btr;
239
240 /* Current medium data */
241 struct {
242 struct bt_notif_iter_medium_ops medops;
243 size_t max_request_sz;
244 void *data;
245 } medium;
246
247 /* Stream beginning was emitted */
248 bool stream_begin_emitted;
249
250 /* Current packet size (bits) (-1 if unknown) */
251 int64_t cur_packet_size;
252
253 /* Current content size (bits) (-1 if unknown) */
254 int64_t cur_content_size;
255
256 /* Current packet default beginning and end clock values */
257 struct clock_value cur_packet_begin_cv;
258 struct clock_value cur_packet_end_cv;
259
260 /*
261 * Offset, in the underlying media, of the current packet's start
262 * (-1 if unknown).
263 */
264 off_t cur_packet_offset;
265
266 /* bt_clock_class to uint64_t. */
267 GHashTable *clock_states;
268
269 /*
270 * Cache of the trace-constant field paths (event header type)
271 * associated to the current trace.
272 */
273 struct trace_field_path_cache trace_field_path_cache;
274
275 /*
276 * Field path cache associated with the current stream class.
277 * Ownership of this structure belongs to the field_path_caches HT.
278 */
279 struct stream_class_field_path_cache *cur_sc_field_path_cache;
280
281 /* bt_stream_class to struct stream_class_field_path_cache. */
282 GHashTable *sc_field_path_caches;
283 };
284
285 static inline
286 const char *state_string(enum state state)
287 {
288 switch (state) {
289 case STATE_INIT:
290 return "STATE_INIT";
291 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
292 return "STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN";
293 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
294 return "STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE";
295 case STATE_AFTER_TRACE_PACKET_HEADER:
296 return "STATE_AFTER_TRACE_PACKET_HEADER";
297 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN:
298 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN";
299 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE:
300 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE";
301 case STATE_AFTER_STREAM_PACKET_CONTEXT:
302 return "STATE_AFTER_STREAM_PACKET_CONTEXT";
303 case STATE_EMIT_NOTIF_NEW_PACKET:
304 return "STATE_EMIT_NOTIF_NEW_PACKET";
305 case STATE_EMIT_NOTIF_NEW_STREAM:
306 return "STATE_EMIT_NOTIF_NEW_STREAM";
307 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN:
308 return "STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN";
309 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE:
310 return "STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE";
311 case STATE_AFTER_STREAM_EVENT_HEADER:
312 return "STATE_AFTER_STREAM_EVENT_HEADER";
313 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN:
314 return "STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN";
315 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE:
316 return "STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE";
317 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN:
318 return "STATE_DSCOPE_EVENT_CONTEXT_BEGIN";
319 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE:
320 return "STATE_DSCOPE_EVENT_CONTEXT_CONTINUE";
321 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN:
322 return "STATE_DSCOPE_EVENT_PAYLOAD_BEGIN";
323 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE:
324 return "STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE";
325 case STATE_EMIT_NOTIF_EVENT:
326 return "STATE_EMIT_NOTIF_EVENT";
327 case STATE_EMIT_NOTIF_END_OF_PACKET:
328 return "STATE_EMIT_NOTIF_END_OF_PACKET";
329 case STATE_DONE:
330 return "STATE_DONE";
331 case STATE_SKIP_PACKET_PADDING:
332 return "STATE_SKIP_PACKET_PADDING";
333 default:
334 return "(unknown)";
335 }
336 }
337
338 static
339 int bt_notif_iter_switch_packet(struct bt_notif_iter *notit);
340
341 static
342 enum bt_btr_status btr_timestamp_end_cb(void *value,
343 struct bt_field_type *type, void *data);
344
345 static
346 struct stack *stack_new(struct bt_notif_iter *notit)
347 {
348 struct stack *stack = NULL;
349
350 stack = g_new0(struct stack, 1);
351 if (!stack) {
352 BT_LOGE_STR("Failed to allocate one stack.");
353 goto error;
354 }
355
356 stack->entries = g_array_new(FALSE, TRUE, sizeof(struct stack_entry));
357 if (!stack->entries) {
358 BT_LOGE_STR("Failed to allocate a GArray.");
359 goto error;
360 }
361
362 BT_LOGD("Created stack: notit-addr=%p, stack-addr=%p", notit, stack);
363 return stack;
364
365 error:
366 g_free(stack);
367 return NULL;
368 }
369
370 static
371 void stack_destroy(struct stack *stack)
372 {
373 BT_ASSERT(stack);
374 BT_LOGD("Destroying stack: addr=%p", stack);
375
376 if (stack->entries) {
377 g_array_free(stack->entries, TRUE);
378 }
379
380 g_free(stack);
381 }
382
383 static
384 int stack_push(struct stack *stack, struct bt_field *base)
385 {
386 struct stack_entry *entry;
387
388 BT_ASSERT(stack);
389 BT_ASSERT(base);
390 BT_LOGV("Pushing base field on stack: stack-addr=%p, "
391 "stack-size-before=%zu, stack-size-after=%zu",
392 stack, stack->size, stack->size + 1);
393
394 if (stack->entries->len == stack->size) {
395 g_array_set_size(stack->entries, stack->size + 1);
396 }
397
398 entry = &g_array_index(stack->entries, struct stack_entry, stack->size);
399 entry->base = base;
400 entry->index = 0;
401 stack->size++;
402 return 0;
403 }
404
405 static inline
406 unsigned int stack_size(struct stack *stack)
407 {
408 BT_ASSERT(stack);
409 return stack->size;
410 }
411
412 static
413 void stack_pop(struct stack *stack)
414 {
415 BT_ASSERT(stack);
416 BT_ASSERT(stack_size(stack));
417 BT_LOGV("Popping from stack: "
418 "stack-addr=%p, stack-size-before=%zu, stack-size-after=%zu",
419 stack, stack->size, stack->size - 1);
420 stack->size--;
421 }
422
423 static inline
424 struct stack_entry *stack_top(struct stack *stack)
425 {
426 BT_ASSERT(stack);
427 BT_ASSERT(stack_size(stack));
428 return &g_array_index(stack->entries, struct stack_entry,
429 stack->size - 1);
430 }
431
432 static inline
433 bool stack_empty(struct stack *stack)
434 {
435 return stack_size(stack) == 0;
436 }
437
438 static
439 void stack_clear(struct stack *stack)
440 {
441 BT_ASSERT(stack);
442 stack->size = 0;
443 }
444
445 static inline
446 enum bt_notif_iter_status notif_iter_status_from_m_status(
447 enum bt_notif_iter_medium_status m_status)
448 {
449 return (int) m_status;
450 }
451
452 static inline
453 size_t buf_size_bits(struct bt_notif_iter *notit)
454 {
455 return notit->buf.sz * 8;
456 }
457
458 static inline
459 size_t buf_available_bits(struct bt_notif_iter *notit)
460 {
461 return buf_size_bits(notit) - notit->buf.at;
462 }
463
464 static inline
465 size_t packet_at(struct bt_notif_iter *notit)
466 {
467 return notit->buf.packet_offset + notit->buf.at;
468 }
469
470 static inline
471 void buf_consume_bits(struct bt_notif_iter *notit, size_t incr)
472 {
473 BT_LOGV("Advancing cursor: notit-addr=%p, cur-before=%zu, cur-after=%zu",
474 notit, notit->buf.at, notit->buf.at + incr);
475 notit->buf.at += incr;
476 }
477
478 static
479 enum bt_notif_iter_status request_medium_bytes(
480 struct bt_notif_iter *notit)
481 {
482 uint8_t *buffer_addr = NULL;
483 size_t buffer_sz = 0;
484 enum bt_notif_iter_medium_status m_status;
485
486 BT_LOGV("Calling user function (request bytes): notit-addr=%p, "
487 "request-size=%zu", notit, notit->medium.max_request_sz);
488 m_status = notit->medium.medops.request_bytes(
489 notit->medium.max_request_sz, &buffer_addr,
490 &buffer_sz, notit->medium.data);
491 BT_LOGV("User function returned: status=%s, buf-addr=%p, buf-size=%zu",
492 bt_notif_iter_medium_status_string(m_status),
493 buffer_addr, buffer_sz);
494 if (m_status == BT_NOTIF_ITER_MEDIUM_STATUS_OK) {
495 BT_ASSERT(buffer_sz != 0);
496
497 /* New packet offset is old one + old size (in bits) */
498 notit->buf.packet_offset += buf_size_bits(notit);
499
500 /* Restart at the beginning of the new medium buffer */
501 notit->buf.at = 0;
502 notit->buf.last_eh_at = SIZE_MAX;
503
504 /* New medium buffer size */
505 notit->buf.sz = buffer_sz;
506
507 /* New medium buffer address */
508 notit->buf.addr = buffer_addr;
509
510 BT_LOGV("User function returned new bytes: "
511 "packet-offset=%zu, cur=%zu, size=%zu, addr=%p",
512 notit->buf.packet_offset, notit->buf.at,
513 notit->buf.sz, notit->buf.addr);
514 BT_LOGV_MEM(buffer_addr, buffer_sz, "Returned bytes at %p:",
515 buffer_addr);
516 } else if (m_status == BT_NOTIF_ITER_MEDIUM_STATUS_EOF) {
517 /*
518 * User returned end of stream: validate that we're not
519 * in the middle of a packet header, packet context, or
520 * event.
521 */
522 if (notit->cur_packet_size >= 0) {
523 if (packet_at(notit) == notit->cur_packet_size) {
524 goto end;
525 }
526 } else {
527 if (packet_at(notit) == 0) {
528 goto end;
529 }
530
531 if (notit->buf.last_eh_at != SIZE_MAX &&
532 notit->buf.at == notit->buf.last_eh_at) {
533 goto end;
534 }
535 }
536
537 /* All other states are invalid */
538 BT_LOGW("User function returned %s, but notification iterator is in an unexpected state: "
539 "state=%s, cur-packet-size=%" PRId64 ", cur=%zu, "
540 "packet-cur=%zu, last-eh-at=%zu",
541 bt_notif_iter_medium_status_string(m_status),
542 state_string(notit->state),
543 notit->cur_packet_size,
544 notit->buf.at, packet_at(notit),
545 notit->buf.last_eh_at);
546 m_status = BT_NOTIF_ITER_MEDIUM_STATUS_ERROR;
547 } else if (m_status < 0) {
548 BT_LOGW("User function failed: status=%s",
549 bt_notif_iter_medium_status_string(m_status));
550 }
551
552 end:
553 return notif_iter_status_from_m_status(m_status);
554 }
555
556 static inline
557 enum bt_notif_iter_status buf_ensure_available_bits(
558 struct bt_notif_iter *notit)
559 {
560 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
561
562 if (buf_available_bits(notit) == 0) {
563 /*
564 * This _cannot_ return BT_NOTIF_ITER_STATUS_OK
565 * _and_ no bits.
566 */
567 status = request_medium_bytes(notit);
568 }
569
570 return status;
571 }
572
573 static inline
574 void reset_clock_value(struct clock_value *cv)
575 {
576 BT_ASSERT(cv);
577 cv->clock_class = NULL;
578 cv->raw_value = UINT64_C(-1);
579 }
580
581 static
582 enum bt_notif_iter_status read_dscope_begin_state(
583 struct bt_notif_iter *notit,
584 struct bt_field_type *dscope_field_type,
585 enum state done_state, enum state continue_state,
586 struct bt_field *dscope_field)
587 {
588 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
589 enum bt_btr_status btr_status;
590 size_t consumed_bits;
591
592 notit->cur_dscope_field = dscope_field;
593 BT_LOGV("Starting BTR: notit-addr=%p, btr-addr=%p, ft-addr=%p",
594 notit, notit->btr, dscope_field_type);
595 consumed_bits = bt_btr_start(notit->btr, dscope_field_type,
596 notit->buf.addr, notit->buf.at, packet_at(notit),
597 notit->buf.sz, &btr_status);
598 BT_LOGV("BTR consumed bits: size=%zu", consumed_bits);
599
600 switch (btr_status) {
601 case BT_BTR_STATUS_OK:
602 /* type was read completely */
603 BT_LOGV_STR("Field was completely decoded.");
604 notit->state = done_state;
605 break;
606 case BT_BTR_STATUS_EOF:
607 BT_LOGV_STR("BTR needs more data to decode field completely.");
608 notit->state = continue_state;
609 break;
610 default:
611 BT_LOGW("BTR failed to start: notit-addr=%p, btr-addr=%p, "
612 "status=%s", notit, notit->btr,
613 bt_btr_status_string(btr_status));
614 status = BT_NOTIF_ITER_STATUS_ERROR;
615 goto end;
616 }
617
618 /* Consume bits now since we know we're not in an error state */
619 buf_consume_bits(notit, consumed_bits);
620
621 end:
622 return status;
623 }
624
625 static
626 enum bt_notif_iter_status read_dscope_continue_state(
627 struct bt_notif_iter *notit, enum state done_state)
628 {
629 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
630 enum bt_btr_status btr_status;
631 size_t consumed_bits;
632
633 BT_LOGV("Continuing BTR: notit-addr=%p, btr-addr=%p",
634 notit, notit->btr);
635
636 status = buf_ensure_available_bits(notit);
637 if (status != BT_NOTIF_ITER_STATUS_OK) {
638 if (status < 0) {
639 BT_LOGW("Cannot ensure that buffer has at least one byte: "
640 "notif-addr=%p, status=%s",
641 notit, bt_notif_iter_status_string(status));
642 } else {
643 BT_LOGV("Cannot ensure that buffer has at least one byte: "
644 "notif-addr=%p, status=%s",
645 notit, bt_notif_iter_status_string(status));
646 }
647
648 goto end;
649 }
650
651
652 consumed_bits = bt_btr_continue(notit->btr, notit->buf.addr,
653 notit->buf.sz, &btr_status);
654 BT_LOGV("BTR consumed bits: size=%zu", consumed_bits);
655
656 switch (btr_status) {
657 case BT_BTR_STATUS_OK:
658 /* Type was read completely. */
659 BT_LOGV_STR("Field was completely decoded.");
660 notit->state = done_state;
661 break;
662 case BT_BTR_STATUS_EOF:
663 /* Stay in this continue state. */
664 BT_LOGV_STR("BTR needs more data to decode field completely.");
665 break;
666 default:
667 BT_LOGW("BTR failed to continue: notit-addr=%p, btr-addr=%p, "
668 "status=%s", notit, notit->btr,
669 bt_btr_status_string(btr_status));
670 status = BT_NOTIF_ITER_STATUS_ERROR;
671 goto end;
672 }
673
674 /* Consume bits now since we know we're not in an error state. */
675 buf_consume_bits(notit, consumed_bits);
676 end:
677 return status;
678 }
679
680 static
681 void release_event_dscopes(struct bt_notif_iter *notit)
682 {
683 notit->dscopes.stream_event_header = NULL;
684
685 if (notit->event_header_field) {
686 bt_event_header_field_release(notit->event_header_field);
687 notit->event_header_field = NULL;
688 }
689
690 notit->dscopes.stream_event_context = NULL;
691 notit->dscopes.event_context = NULL;
692 notit->dscopes.event_payload = NULL;
693 }
694
695 static
696 void release_all_dscopes(struct bt_notif_iter *notit)
697 {
698 notit->dscopes.trace_packet_header = NULL;
699
700 if (notit->packet_header_field) {
701 bt_packet_header_field_release(notit->packet_header_field);
702 notit->packet_header_field = NULL;
703 }
704
705 notit->dscopes.stream_packet_context = NULL;
706
707 if (notit->packet_context_field) {
708 bt_packet_context_field_release(notit->packet_context_field);
709 notit->packet_context_field = NULL;
710 }
711
712 release_event_dscopes(notit);
713 }
714
715 static
716 enum bt_notif_iter_status read_packet_header_begin_state(
717 struct bt_notif_iter *notit)
718 {
719 struct bt_field_type *packet_header_type = NULL;
720 enum bt_notif_iter_status ret = BT_NOTIF_ITER_STATUS_OK;
721
722 if (bt_notif_iter_switch_packet(notit)) {
723 BT_LOGW("Cannot switch packet: notit-addr=%p", notit);
724 ret = BT_NOTIF_ITER_STATUS_ERROR;
725 goto end;
726 }
727
728 /* Packet header type is common to the whole trace. */
729 packet_header_type = bt_trace_borrow_packet_header_field_type(
730 notit->meta.trace);
731 if (!packet_header_type) {
732 notit->state = STATE_AFTER_TRACE_PACKET_HEADER;
733 goto end;
734 }
735
736 /*
737 * Create free packet header field from trace. This field is
738 * going to be moved to the packet once we create it. We cannot
739 * create the packet now because:
740 *
741 * 1. A packet is created from a stream.
742 * 2. A stream is created from a stream class.
743 * 3. We need the packet header field's content to know the ID
744 * of the stream class to select.
745 */
746 BT_ASSERT(!notit->packet_header_field);
747 notit->packet_header_field = bt_trace_create_packet_header_field(
748 notit->meta.trace);
749 if (!notit->packet_header_field) {
750 BT_LOGE_STR("Cannot create packet header field wrapper from trace.");
751 ret = BT_NOTIF_ITER_STATUS_ERROR;
752 goto end;
753 }
754
755 notit->dscopes.trace_packet_header =
756 bt_packet_header_field_borrow_field(notit->packet_header_field);
757 BT_ASSERT(notit->dscopes.trace_packet_header);
758 BT_LOGV("Decoding packet header field:"
759 "notit-addr=%p, trace-addr=%p, trace-name=\"%s\", ft-addr=%p",
760 notit, notit->meta.trace,
761 bt_trace_get_name(notit->meta.trace), packet_header_type);
762 ret = read_dscope_begin_state(notit, packet_header_type,
763 STATE_AFTER_TRACE_PACKET_HEADER,
764 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE,
765 notit->dscopes.trace_packet_header);
766 if (ret < 0) {
767 BT_LOGW("Cannot decode packet header field: "
768 "notit-addr=%p, trace-addr=%p, "
769 "trace-name=\"%s\", ft-addr=%p",
770 notit, notit->meta.trace,
771 bt_trace_get_name(notit->meta.trace),
772 packet_header_type);
773 }
774
775 end:
776 return ret;
777 }
778
779 static
780 enum bt_notif_iter_status read_packet_header_continue_state(
781 struct bt_notif_iter *notit)
782 {
783 return read_dscope_continue_state(notit,
784 STATE_AFTER_TRACE_PACKET_HEADER);
785 }
786
787 static
788 struct stream_class_field_path_cache *
789 create_stream_class_field_path_cache_entry(
790 struct bt_notif_iter *notit,
791 struct bt_stream_class *stream_class)
792 {
793 int v = -1;
794 int id = -1;
795 int timestamp_end = -1;
796 int packet_size = -1;
797 int content_size = -1;
798 struct stream_class_field_path_cache *cache_entry = g_new0(
799 struct stream_class_field_path_cache, 1);
800 struct bt_field_type *event_header = NULL, *packet_context = NULL;
801
802 if (!cache_entry) {
803 BT_LOGE_STR("Failed to allocate one stream class field path cache.");
804 goto end;
805 }
806
807 event_header = bt_stream_class_borrow_event_header_field_type(
808 stream_class);
809 if (event_header && bt_field_type_is_structure(event_header)) {
810 int i, count;
811
812 count = bt_field_type_structure_get_field_count(
813 event_header);
814 BT_ASSERT(count >= 0);
815
816 for (i = 0; i < count; i++) {
817 int ret;
818 const char *name;
819
820 ret = bt_field_type_structure_borrow_field_by_index(
821 event_header, &name, NULL, i);
822 if (ret) {
823 BT_LOGE("Cannot get event header structure field type's field: "
824 "notit-addr=%p, stream-class-addr=%p, "
825 "stream-class-name=\"%s\", "
826 "stream-class-id=%" PRId64 ", "
827 "ft-addr=%p, index=%d",
828 notit, stream_class,
829 bt_stream_class_get_name(stream_class),
830 bt_stream_class_get_id(stream_class),
831 event_header, i);
832 goto error;
833 }
834
835 if (v != -1 && id != -1) {
836 break;
837 }
838
839 if (v == -1 && strcmp(name, "v") == 0) {
840 v = i;
841 } else if (id == -1 && !strcmp(name, "id")) {
842 id = i;
843 }
844 }
845 }
846
847 packet_context = bt_stream_class_borrow_packet_context_field_type(
848 stream_class);
849 if (packet_context && bt_field_type_is_structure(packet_context)) {
850 int i, count;
851
852 count = bt_field_type_structure_get_field_count(
853 packet_context);
854 BT_ASSERT(count >= 0);
855
856 for (i = 0; i < count; i++) {
857 int ret;
858 const char *name;
859 struct bt_field_type *field_type;
860
861 if (timestamp_end != -1 && packet_size != -1 &&
862 content_size != -1) {
863 break;
864 }
865
866 ret = bt_field_type_structure_borrow_field_by_index(
867 packet_context, &name, &field_type, i);
868 if (ret) {
869 BT_LOGE("Cannot get packet context structure field type's field: "
870 "notit-addr=%p, stream-class-addr=%p, "
871 "stream-class-name=\"%s\", "
872 "stream-class-id=%" PRId64 ", "
873 "ft-addr=%p, index=%d",
874 notit, stream_class,
875 bt_stream_class_get_name(stream_class),
876 bt_stream_class_get_id(stream_class),
877 event_header, i);
878 goto error;
879 }
880
881 if (timestamp_end == -1 &&
882 strcmp(name, "timestamp_end") == 0) {
883 struct field_cb_override *override = g_new0(
884 struct field_cb_override, 1);
885
886 if (!override) {
887 goto error;
888 }
889
890 override->func = btr_timestamp_end_cb;
891 override->data = notit;
892 g_hash_table_insert(notit->field_overrides,
893 field_type, override);
894 timestamp_end = i;
895 } else if (packet_size == -1 &&
896 !strcmp(name, "packet_size")) {
897 packet_size = i;
898 } else if (content_size == -1 &&
899 !strcmp(name, "content_size")) {
900 content_size = i;
901 }
902 }
903 }
904
905 cache_entry->v = v;
906 cache_entry->id = id;
907 cache_entry->timestamp_end = timestamp_end;
908 cache_entry->packet_size = packet_size;
909 cache_entry->content_size = content_size;
910
911 end:
912 return cache_entry;
913
914 error:
915 g_free(cache_entry);
916 cache_entry = NULL;
917 goto end;
918 }
919
920 static
921 struct stream_class_field_path_cache *get_stream_class_field_path_cache(
922 struct bt_notif_iter *notit,
923 struct bt_stream_class *stream_class)
924 {
925 bool cache_entry_found;
926 struct stream_class_field_path_cache *cache_entry;
927
928 cache_entry_found = g_hash_table_lookup_extended(
929 notit->sc_field_path_caches,
930 stream_class, NULL, (gpointer) &cache_entry);
931 if (unlikely(!cache_entry_found)) {
932 cache_entry = create_stream_class_field_path_cache_entry(notit,
933 stream_class);
934 g_hash_table_insert(notit->sc_field_path_caches,
935 stream_class, (gpointer) cache_entry);
936 }
937
938 return cache_entry;
939 }
940
941 static inline
942 enum bt_notif_iter_status set_current_stream_class(
943 struct bt_notif_iter *notit)
944 {
945 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
946 struct bt_field_type *packet_header_type = NULL;
947 struct bt_field_type *stream_id_field_type = NULL;
948 struct bt_stream_class *new_stream_class = NULL;
949 uint64_t stream_id;
950
951 /* Clear the current stream class field path cache. */
952 notit->cur_sc_field_path_cache = NULL;
953
954 /* Is there any "stream_id" field in the packet header? */
955 packet_header_type = bt_trace_borrow_packet_header_field_type(
956 notit->meta.trace);
957 if (!packet_header_type) {
958 /*
959 * No packet header, therefore no `stream_id` field,
960 * therefore only one stream class.
961 */
962 goto single_stream_class;
963 }
964
965 BT_ASSERT(bt_field_type_is_structure(packet_header_type));
966
967 // TODO: optimalize!
968 stream_id_field_type =
969 bt_field_type_structure_borrow_field_type_by_name(
970 packet_header_type, "stream_id");
971 if (stream_id_field_type) {
972 /* Find appropriate stream class using current stream ID */
973 int ret;
974 struct bt_field *stream_id_field = NULL;
975
976 BT_ASSERT(notit->dscopes.trace_packet_header);
977
978 // TODO: optimalize!
979 stream_id_field = bt_field_structure_borrow_field_by_name(
980 notit->dscopes.trace_packet_header, "stream_id");
981 BT_ASSERT(stream_id_field);
982 ret = bt_field_integer_unsigned_get_value(
983 stream_id_field, &stream_id);
984 BT_ASSERT(!ret);
985 } else {
986 single_stream_class:
987 /* Only one stream: pick the first stream class */
988 BT_ASSERT(bt_trace_get_stream_class_count(
989 notit->meta.trace) == 1);
990 stream_id = 0;
991 }
992
993 BT_LOGV("Found stream class ID to use: notit-addr=%p, "
994 "stream-class-id=%" PRIu64 ", "
995 "trace-addr=%p, trace-name=\"%s\"",
996 notit, stream_id, notit->meta.trace,
997 bt_trace_get_name(notit->meta.trace));
998
999 new_stream_class = bt_trace_borrow_stream_class_by_id(
1000 notit->meta.trace, stream_id);
1001 if (!new_stream_class) {
1002 BT_LOGW("No stream class with ID of stream class ID to use in trace: "
1003 "notit-addr=%p, stream-class-id=%" PRIu64 ", "
1004 "trace-addr=%p, trace-name=\"%s\"",
1005 notit, stream_id, notit->meta.trace,
1006 bt_trace_get_name(notit->meta.trace));
1007 status = BT_NOTIF_ITER_STATUS_ERROR;
1008 goto end;
1009 }
1010
1011 if (notit->meta.stream_class) {
1012 if (new_stream_class != notit->meta.stream_class) {
1013 BT_LOGW("Two packets refer to two different stream classes within the same packet sequence: "
1014 "notit-addr=%p, prev-stream-class-addr=%p, "
1015 "prev-stream-class-name=\"%s\", "
1016 "prev-stream-class-id=%" PRId64 ", "
1017 "next-stream-class-addr=%p, "
1018 "next-stream-class-name=\"%s\", "
1019 "next-stream-class-id=%" PRId64 ", "
1020 "trace-addr=%p, trace-name=\"%s\"",
1021 notit, notit->meta.stream_class,
1022 bt_stream_class_get_name(notit->meta.stream_class),
1023 bt_stream_class_get_id(notit->meta.stream_class),
1024 new_stream_class,
1025 bt_stream_class_get_name(new_stream_class),
1026 bt_stream_class_get_id(new_stream_class),
1027 notit->meta.trace,
1028 bt_trace_get_name(notit->meta.trace));
1029 status = BT_NOTIF_ITER_STATUS_ERROR;
1030 goto end;
1031 }
1032 } else {
1033 notit->meta.stream_class = new_stream_class;
1034 }
1035
1036 BT_LOGV("Set current stream class: "
1037 "notit-addr=%p, stream-class-addr=%p, "
1038 "stream-class-name=\"%s\", stream-class-id=%" PRId64,
1039 notit, notit->meta.stream_class,
1040 bt_stream_class_get_name(notit->meta.stream_class),
1041 bt_stream_class_get_id(notit->meta.stream_class));
1042
1043 /*
1044 * Retrieve (or lazily create) the current stream class field path
1045 * cache.
1046 */
1047 notit->cur_sc_field_path_cache = get_stream_class_field_path_cache(
1048 notit, notit->meta.stream_class);
1049 if (!notit->cur_sc_field_path_cache) {
1050 BT_LOGW("Cannot retrieve stream class field path from cache: "
1051 "notit-addr=%p, stream-class-addr=%p, "
1052 "stream-class-name=\"%s\", stream-class-id=%" PRId64,
1053 notit, notit->meta.stream_class,
1054 bt_stream_class_get_name(notit->meta.stream_class),
1055 bt_stream_class_get_id(notit->meta.stream_class));
1056 status = BT_NOTIF_ITER_STATUS_ERROR;
1057 goto end;
1058 }
1059
1060 end:
1061 return status;
1062 }
1063
1064 static inline
1065 uint64_t get_cur_stream_instance_id(struct bt_notif_iter *notit)
1066 {
1067 struct bt_field *stream_instance_id_field = NULL;
1068 uint64_t stream_instance_id = -1ULL;
1069 int ret;
1070
1071 if (!notit->dscopes.trace_packet_header) {
1072 goto end;
1073 }
1074
1075 stream_instance_id_field = bt_field_structure_borrow_field_by_name(
1076 notit->dscopes.trace_packet_header, "stream_instance_id");
1077 if (!stream_instance_id_field) {
1078 goto end;
1079 }
1080
1081 ret = bt_field_integer_unsigned_get_value(stream_instance_id_field,
1082 &stream_instance_id);
1083 if (ret) {
1084 stream_instance_id = -1ULL;
1085 goto end;
1086 }
1087
1088 end:
1089 return stream_instance_id;
1090 }
1091
1092 static inline
1093 enum bt_notif_iter_status set_current_stream(struct bt_notif_iter *notit)
1094 {
1095 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1096 struct bt_stream *stream = NULL;
1097
1098 BT_LOGV("Calling user function (get stream): notit-addr=%p, "
1099 "stream-class-addr=%p, stream-class-name=\"%s\", "
1100 "stream-class-id=%" PRId64,
1101 notit, notit->meta.stream_class,
1102 bt_stream_class_get_name(notit->meta.stream_class),
1103 bt_stream_class_get_id(notit->meta.stream_class));
1104 stream = bt_get(notit->medium.medops.borrow_stream(
1105 notit->meta.stream_class, get_cur_stream_instance_id(notit),
1106 notit->medium.data));
1107 BT_LOGV("User function returned: stream-addr=%p", stream);
1108 if (!stream) {
1109 BT_LOGW_STR("User function failed to return a stream object for the given stream class.");
1110 status = BT_NOTIF_ITER_STATUS_ERROR;
1111 goto end;
1112 }
1113
1114 if (notit->stream && stream != notit->stream) {
1115 BT_LOGW("User function returned a different stream than the previous one for the same sequence of packets.");
1116 status = BT_NOTIF_ITER_STATUS_ERROR;
1117 goto end;
1118 }
1119
1120 BT_MOVE(notit->stream, stream);
1121
1122 end:
1123 bt_put(stream);
1124 return status;
1125 }
1126
1127 static inline
1128 enum bt_notif_iter_status set_current_packet(struct bt_notif_iter *notit)
1129 {
1130 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1131 struct bt_packet *packet = NULL;
1132
1133 BT_LOGV("Creating packet for packet notification: "
1134 "notit-addr=%p", notit);
1135 BT_LOGV("Creating packet from stream: "
1136 "notit-addr=%p, stream-addr=%p, "
1137 "stream-class-addr=%p, "
1138 "stream-class-name=\"%s\", "
1139 "stream-class-id=%" PRId64,
1140 notit, notit->stream, notit->meta.stream_class,
1141 bt_stream_class_get_name(notit->meta.stream_class),
1142 bt_stream_class_get_id(notit->meta.stream_class));
1143
1144 /* Create packet */
1145 BT_ASSERT(notit->stream);
1146 packet = bt_packet_create(notit->stream);
1147 if (!packet) {
1148 BT_LOGE("Cannot create packet from stream: "
1149 "notit-addr=%p, stream-addr=%p, "
1150 "stream-class-addr=%p, "
1151 "stream-class-name=\"%s\", "
1152 "stream-class-id=%" PRId64,
1153 notit, notit->stream, notit->meta.stream_class,
1154 bt_stream_class_get_name(notit->meta.stream_class),
1155 bt_stream_class_get_id(notit->meta.stream_class));
1156 goto error;
1157 }
1158
1159 goto end;
1160
1161 error:
1162 BT_PUT(packet);
1163 status = BT_NOTIF_ITER_STATUS_ERROR;
1164
1165 end:
1166 BT_MOVE(notit->packet, packet);
1167 return status;
1168 }
1169
1170 static
1171 enum bt_notif_iter_status after_packet_header_state(
1172 struct bt_notif_iter *notit)
1173 {
1174 enum bt_notif_iter_status status;
1175
1176 status = set_current_stream_class(notit);
1177 if (status != BT_NOTIF_ITER_STATUS_OK) {
1178 goto end;
1179 }
1180
1181 notit->state = STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN;
1182
1183 end:
1184 return status;
1185 }
1186
1187 static
1188 enum bt_notif_iter_status read_packet_context_begin_state(
1189 struct bt_notif_iter *notit)
1190 {
1191 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1192 struct bt_field_type *packet_context_type;
1193
1194 BT_ASSERT(notit->meta.stream_class);
1195 packet_context_type = bt_stream_class_borrow_packet_context_field_type(
1196 notit->meta.stream_class);
1197 if (!packet_context_type) {
1198 BT_LOGV("No packet packet context field type in stream class: continuing: "
1199 "notit-addr=%p, stream-class-addr=%p, "
1200 "stream-class-name=\"%s\", stream-class-id=%" PRId64,
1201 notit, notit->meta.stream_class,
1202 bt_stream_class_get_name(notit->meta.stream_class),
1203 bt_stream_class_get_id(notit->meta.stream_class));
1204 notit->state = STATE_AFTER_STREAM_PACKET_CONTEXT;
1205 goto end;
1206 }
1207
1208 /*
1209 * Create free packet context field from stream class. This
1210 * field is going to be moved to the packet once we create it.
1211 * We cannot create the packet now because a packet is created
1212 * from a stream, and this API must be able to return the packet
1213 * header and context fields without creating a stream
1214 * (bt_notif_iter_get_packet_header_context_fields()).
1215 */
1216 BT_ASSERT(!notit->packet_context_field);
1217 notit->packet_context_field =
1218 bt_stream_class_create_packet_context_field(
1219 notit->meta.stream_class);
1220 if (!notit->packet_context_field) {
1221 BT_LOGE_STR("Cannot create packet context field wrapper from stream class.");
1222 status = BT_NOTIF_ITER_STATUS_ERROR;
1223 goto end;
1224 }
1225
1226 notit->dscopes.stream_packet_context =
1227 bt_packet_context_field_borrow_field(notit->packet_context_field);
1228 BT_ASSERT(notit->dscopes.stream_packet_context);
1229 BT_LOGV("Decoding packet context field: "
1230 "notit-addr=%p, stream-class-addr=%p, "
1231 "stream-class-name=\"%s\", stream-class-id=%" PRId64 ", "
1232 "ft-addr=%p",
1233 notit, notit->meta.stream_class,
1234 bt_stream_class_get_name(notit->meta.stream_class),
1235 bt_stream_class_get_id(notit->meta.stream_class),
1236 packet_context_type);
1237 status = read_dscope_begin_state(notit, packet_context_type,
1238 STATE_AFTER_STREAM_PACKET_CONTEXT,
1239 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE,
1240 notit->dscopes.stream_packet_context);
1241 if (status < 0) {
1242 BT_LOGW("Cannot decode packet context field: "
1243 "notit-addr=%p, stream-class-addr=%p, "
1244 "stream-class-name=\"%s\", "
1245 "stream-class-id=%" PRId64 ", ft-addr=%p",
1246 notit, notit->meta.stream_class,
1247 bt_stream_class_get_name(notit->meta.stream_class),
1248 bt_stream_class_get_id(notit->meta.stream_class),
1249 packet_context_type);
1250 }
1251
1252 end:
1253 return status;
1254 }
1255
1256 static
1257 enum bt_notif_iter_status read_packet_context_continue_state(
1258 struct bt_notif_iter *notit)
1259 {
1260 return read_dscope_continue_state(notit,
1261 STATE_AFTER_STREAM_PACKET_CONTEXT);
1262 }
1263
1264 static inline
1265 uint64_t get_field_raw_clock_value(struct bt_field *base_field,
1266 const char *field_name, struct bt_clock_class **user_cc)
1267 {
1268 struct bt_field *field;
1269 struct bt_field_type *ft;
1270 struct bt_clock_class *clock_class = NULL;
1271 uint64_t val = UINT64_C(-1);
1272 int ret;
1273
1274 field = bt_field_structure_borrow_field_by_name(base_field, field_name);
1275 if (!field) {
1276 goto end;
1277 }
1278
1279 ft = bt_field_borrow_type(field);
1280 BT_ASSERT(ft);
1281
1282 if (!bt_field_type_is_integer(ft)) {
1283 goto end;
1284 }
1285
1286 clock_class = bt_field_type_integer_borrow_mapped_clock_class(ft);
1287 if (!clock_class) {
1288 goto end;
1289 }
1290
1291 ret = bt_field_integer_unsigned_get_value(field, &val);
1292 BT_ASSERT(ret == 0);
1293
1294 end:
1295 *user_cc = clock_class;
1296 return val;
1297 }
1298
1299 static
1300 void set_current_packet_begin_end(struct bt_notif_iter *notit)
1301 {
1302 struct bt_clock_class *clock_class;
1303 uint64_t val;
1304
1305 if (!notit->dscopes.stream_packet_context) {
1306 goto end;
1307 }
1308
1309 val = get_field_raw_clock_value(notit->dscopes.stream_packet_context,
1310 "timestamp_begin", &clock_class);
1311 notit->cur_packet_begin_cv.clock_class = clock_class;
1312 notit->cur_packet_begin_cv.raw_value = val;
1313 val = get_field_raw_clock_value(notit->dscopes.stream_packet_context,
1314 "timestamp_end", &clock_class);
1315 notit->cur_packet_end_cv.clock_class = clock_class;
1316 notit->cur_packet_end_cv.raw_value = val;
1317
1318 end:
1319 return;
1320 }
1321
1322 static
1323 enum bt_notif_iter_status set_current_packet_content_sizes(
1324 struct bt_notif_iter *notit)
1325 {
1326 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1327 struct bt_field *packet_size_field = NULL;
1328 struct bt_field *content_size_field = NULL;
1329 uint64_t content_size = -1ULL, packet_size = -1ULL;
1330
1331 if (!notit->dscopes.stream_packet_context) {
1332 goto end;
1333 }
1334
1335 packet_size_field = bt_field_structure_borrow_field_by_name(
1336 notit->dscopes.stream_packet_context, "packet_size");
1337 content_size_field = bt_field_structure_borrow_field_by_name(
1338 notit->dscopes.stream_packet_context, "content_size");
1339 if (packet_size_field) {
1340 int ret = bt_field_integer_unsigned_get_value(
1341 packet_size_field, &packet_size);
1342
1343 BT_ASSERT(ret == 0);
1344 if (packet_size == 0) {
1345 BT_LOGW("Invalid packet size: packet context field indicates packet size is zero: "
1346 "notit-addr=%p, packet-context-field-addr=%p",
1347 notit, notit->dscopes.stream_packet_context);
1348 status = BT_NOTIF_ITER_STATUS_ERROR;
1349 goto end;
1350 } else if ((packet_size % 8) != 0) {
1351 BT_LOGW("Invalid packet size: packet context field indicates packet size is not a multiple of 8: "
1352 "notit-addr=%p, packet-context-field-addr=%p, "
1353 "packet-size=%" PRIu64,
1354 notit, notit->dscopes.stream_packet_context,
1355 packet_size);
1356 status = BT_NOTIF_ITER_STATUS_ERROR;
1357 goto end;
1358 }
1359 }
1360
1361 if (content_size_field) {
1362 int ret = bt_field_integer_unsigned_get_value(
1363 content_size_field, &content_size);
1364
1365 BT_ASSERT(ret == 0);
1366 } else {
1367 content_size = packet_size;
1368 }
1369
1370 if (content_size > packet_size) {
1371 BT_LOGW("Invalid packet or content size: packet context field indicates content size is greater than packet size: "
1372 "notit-addr=%p, packet-context-field-addr=%p, "
1373 "packet-size=%" PRIu64 ", content-size=%" PRIu64,
1374 notit, notit->dscopes.stream_packet_context,
1375 packet_size, content_size);
1376 status = BT_NOTIF_ITER_STATUS_ERROR;
1377 goto end;
1378 }
1379
1380 if (packet_size != -1ULL) {
1381 notit->cur_packet_size = packet_size;
1382 } else {
1383 /*
1384 * Use the content size as packet size indicator if the
1385 * packet size field is missing. This means there is no
1386 * padding in this stream.
1387 */
1388 notit->cur_packet_size = content_size;
1389 }
1390 notit->cur_content_size = content_size;
1391 BT_LOGV("Set current packet and content sizes: "
1392 "notit-addr=%p, packet-size=%" PRIu64 ", content-size=%" PRIu64,
1393 notit, packet_size, content_size);
1394 end:
1395 return status;
1396 }
1397
1398 static
1399 enum bt_notif_iter_status after_packet_context_state(
1400 struct bt_notif_iter *notit)
1401 {
1402 enum bt_notif_iter_status status;
1403
1404 status = set_current_packet_content_sizes(notit);
1405 if (status != BT_NOTIF_ITER_STATUS_OK) {
1406 goto end;
1407 }
1408
1409 set_current_packet_begin_end(notit);
1410
1411 if (notit->stream_begin_emitted) {
1412 notit->state = STATE_EMIT_NOTIF_NEW_PACKET;
1413 } else {
1414 notit->state = STATE_EMIT_NOTIF_NEW_STREAM;
1415 }
1416
1417 end:
1418 return status;
1419 }
1420
1421 static
1422 enum bt_notif_iter_status read_event_header_begin_state(
1423 struct bt_notif_iter *notit)
1424 {
1425 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1426 struct bt_field_type *event_header_type = NULL;
1427
1428 /* Reset the position of the last event header */
1429 notit->buf.last_eh_at = notit->buf.at;
1430
1431 /* Check if we have some content left */
1432 if (notit->cur_content_size >= 0) {
1433 if (packet_at(notit) == notit->cur_content_size) {
1434 /* No more events! */
1435 BT_LOGV("Reached end of packet: notit-addr=%p, "
1436 "cur=%zu", notit, packet_at(notit));
1437 notit->state = STATE_EMIT_NOTIF_END_OF_PACKET;
1438 goto end;
1439 } else if (packet_at(notit) > notit->cur_content_size) {
1440 /* That's not supposed to happen */
1441 BT_LOGV("Before decoding event header field: cursor is passed the packet's content: "
1442 "notit-addr=%p, content-size=%" PRId64 ", "
1443 "cur=%zu", notit, notit->cur_content_size,
1444 packet_at(notit));
1445 status = BT_NOTIF_ITER_STATUS_ERROR;
1446 goto end;
1447 }
1448 }
1449
1450 release_event_dscopes(notit);
1451 BT_ASSERT(notit->meta.stream_class);
1452 event_header_type = bt_stream_class_borrow_event_header_field_type(
1453 notit->meta.stream_class);
1454 if (!event_header_type) {
1455 notit->state = STATE_AFTER_STREAM_EVENT_HEADER;
1456 goto end;
1457 }
1458
1459 BT_ASSERT(!notit->event_header_field);
1460 notit->event_header_field = bt_stream_class_create_event_header_field(
1461 notit->meta.stream_class);
1462 if (!notit->event_header_field) {
1463 BT_LOGE_STR("Cannot create event header field wrapper from trace.");
1464 status = BT_NOTIF_ITER_STATUS_ERROR;
1465 goto end;
1466 }
1467
1468 notit->dscopes.stream_event_header =
1469 bt_event_header_field_borrow_field(notit->event_header_field);
1470 BT_ASSERT(notit->dscopes.stream_event_header);
1471 BT_LOGV("Decoding event header field: "
1472 "notit-addr=%p, stream-class-addr=%p, "
1473 "stream-class-name=\"%s\", stream-class-id=%" PRId64 ", "
1474 "ft-addr=%p",
1475 notit, notit->meta.stream_class,
1476 bt_stream_class_get_name(notit->meta.stream_class),
1477 bt_stream_class_get_id(notit->meta.stream_class),
1478 event_header_type);
1479 status = read_dscope_begin_state(notit, event_header_type,
1480 STATE_AFTER_STREAM_EVENT_HEADER,
1481 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE,
1482 notit->dscopes.stream_event_header);
1483 if (status < 0) {
1484 BT_LOGW("Cannot decode event header field: "
1485 "notit-addr=%p, stream-class-addr=%p, "
1486 "stream-class-name=\"%s\", "
1487 "stream-class-id=%" PRId64 ", ft-addr=%p",
1488 notit, notit->meta.stream_class,
1489 bt_stream_class_get_name(notit->meta.stream_class),
1490 bt_stream_class_get_id(notit->meta.stream_class),
1491 event_header_type);
1492 }
1493
1494 end:
1495 return status;
1496 }
1497
1498 static
1499 enum bt_notif_iter_status read_event_header_continue_state(
1500 struct bt_notif_iter *notit)
1501 {
1502 return read_dscope_continue_state(notit,
1503 STATE_AFTER_STREAM_EVENT_HEADER);
1504 }
1505
1506 static inline
1507 enum bt_notif_iter_status set_current_event_class(struct bt_notif_iter *notit)
1508 {
1509 /*
1510 * The assert() calls in this function are okay because it is
1511 * assumed here that all the metadata objects have been
1512 * validated for CTF correctness before decoding actual streams.
1513 */
1514
1515 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1516 struct bt_field_type *event_header_type;
1517 struct bt_field_type *id_field_type = NULL;
1518 struct bt_field_type *v_field_type = NULL;
1519 uint64_t event_id = -1ULL;
1520 int ret;
1521
1522 event_header_type = bt_stream_class_borrow_event_header_field_type(
1523 notit->meta.stream_class);
1524 if (!event_header_type) {
1525 /*
1526 * No event header, therefore no event class ID field,
1527 * therefore only one event class.
1528 */
1529 goto single_event_class;
1530 }
1531
1532 /* Is there any "id"/"v" field in the event header? */
1533 BT_ASSERT(bt_field_type_is_structure(event_header_type));
1534 id_field_type = bt_field_type_structure_borrow_field_type_by_name(
1535 event_header_type, "id");
1536 v_field_type = bt_field_type_structure_borrow_field_type_by_name(
1537 event_header_type, "v");
1538 BT_ASSERT(notit->dscopes.stream_event_header);
1539 if (v_field_type) {
1540 /*
1541 * _ _____ _____
1542 * | | |_ _|_ _| __ __ _
1543 * | | | | | || '_ \ / _` |
1544 * | |___| | | || | | | (_| | S P E C I A L
1545 * |_____|_| |_||_| |_|\__, | C A S E â„¢
1546 * |___/
1547 */
1548 struct bt_field *v_field = NULL;
1549 struct bt_field *v_struct_field = NULL;
1550 struct bt_field *v_struct_id_field = NULL;
1551
1552 // TODO: optimalize!
1553 v_field = bt_field_structure_borrow_field_by_name(
1554 notit->dscopes.stream_event_header, "v");
1555 BT_ASSERT(v_field);
1556
1557 v_struct_field =
1558 bt_field_variant_borrow_current_field(v_field);
1559 if (!v_struct_field) {
1560 goto end_v_field_type;
1561 }
1562
1563 // TODO: optimalize!
1564 v_struct_id_field = bt_field_structure_borrow_field_by_name(
1565 v_struct_field, "id");
1566 if (!v_struct_id_field) {
1567 goto end_v_field_type;
1568 }
1569
1570 if (bt_field_is_integer(v_struct_id_field)) {
1571 ret = bt_field_integer_unsigned_get_value(
1572 v_struct_id_field, &event_id);
1573 if (ret) {
1574 BT_LOGV("Cannot get value of unsigned integer field (`id`): continuing: "
1575 "notit=%p, field-addr=%p",
1576 notit, v_struct_id_field);
1577 event_id = -1ULL;
1578 }
1579 }
1580 }
1581
1582 end_v_field_type:
1583 if (id_field_type && event_id == -1ULL) {
1584 /* Check "id" field */
1585 struct bt_field *id_field = NULL;
1586 int ret_get_value = 0;
1587
1588 // TODO: optimalize!
1589 id_field = bt_field_structure_borrow_field_by_name(
1590 notit->dscopes.stream_event_header, "id");
1591 if (!id_field) {
1592 goto check_event_id;
1593 }
1594
1595 ret_get_value = bt_field_integer_unsigned_get_value(
1596 id_field, &event_id);
1597 BT_ASSERT(ret_get_value == 0);
1598 }
1599
1600 check_event_id:
1601 if (event_id == -1ULL) {
1602 single_event_class:
1603 /* Event ID not found: single event? */
1604 BT_ASSERT(bt_stream_class_get_event_class_count(
1605 notit->meta.stream_class) == 1);
1606 event_id = 0;
1607 }
1608
1609 BT_LOGV("Found event class ID to use: notit-addr=%p, "
1610 "stream-class-addr=%p, stream-class-name=\"%s\", "
1611 "stream-class-id=%" PRId64 ", "
1612 "event-class-id=%" PRIu64,
1613 notit, notit->meta.stream_class,
1614 bt_stream_class_get_name(notit->meta.stream_class),
1615 bt_stream_class_get_id(notit->meta.stream_class),
1616 event_id);
1617 notit->meta.event_class = bt_stream_class_borrow_event_class_by_id(
1618 notit->meta.stream_class, event_id);
1619 if (!notit->meta.event_class) {
1620 BT_LOGW("No event class with ID of event class ID to use in stream class: "
1621 "notit-addr=%p, stream-class-addr=%p, "
1622 "stream-class-name=\"%s\", "
1623 "stream-class-id=%" PRId64 ", "
1624 "event-class-id=%" PRIu64,
1625 notit, notit->meta.stream_class,
1626 bt_stream_class_get_name(notit->meta.stream_class),
1627 bt_stream_class_get_id(notit->meta.stream_class),
1628 event_id);
1629 status = BT_NOTIF_ITER_STATUS_ERROR;
1630 goto end;
1631 }
1632
1633 BT_LOGV("Set current event class: "
1634 "notit-addr=%p, event-class-addr=%p, "
1635 "event-class-name=\"%s\", event-class-id=%" PRId64,
1636 notit, notit->meta.event_class,
1637 bt_event_class_get_name(notit->meta.event_class),
1638 bt_event_class_get_id(notit->meta.event_class));
1639
1640 end:
1641 return status;
1642 }
1643
1644 static inline
1645 enum bt_notif_iter_status set_current_event_notification(
1646 struct bt_notif_iter *notit)
1647 {
1648 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1649 struct bt_notification *notif = NULL;
1650
1651 BT_ASSERT(notit->meta.event_class);
1652 BT_ASSERT(notit->packet);
1653 BT_LOGV("Creating event notification from event class and packet: "
1654 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", packet-addr=%p",
1655 notit, notit->meta.event_class,
1656 bt_event_class_get_name(notit->meta.event_class),
1657 notit->packet);
1658 BT_ASSERT(notit->notif_iter);
1659 notif = bt_notification_event_create(notit->notif_iter,
1660 notit->meta.event_class, notit->packet);
1661 if (!notif) {
1662 BT_LOGE("Cannot create event notification: "
1663 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", "
1664 "packet-addr=%p",
1665 notit, notit->meta.event_class,
1666 bt_event_class_get_name(notit->meta.event_class),
1667 notit->packet);
1668 goto error;
1669 }
1670
1671 goto end;
1672
1673 error:
1674 BT_PUT(notif);
1675 status = BT_NOTIF_ITER_STATUS_ERROR;
1676
1677 end:
1678 BT_MOVE(notit->event_notif, notif);
1679 return status;
1680 }
1681
1682 static
1683 enum bt_notif_iter_status after_event_header_state(
1684 struct bt_notif_iter *notit)
1685 {
1686 enum bt_notif_iter_status status;
1687
1688 status = set_current_event_class(notit);
1689 if (status != BT_NOTIF_ITER_STATUS_OK) {
1690 goto end;
1691 }
1692
1693 status = set_current_event_notification(notit);
1694 if (status != BT_NOTIF_ITER_STATUS_OK) {
1695 goto end;
1696 }
1697
1698 notit->event = bt_notification_event_borrow_event(notit->event_notif);
1699 BT_ASSERT(notit->event);
1700
1701 if (notit->event_header_field) {
1702 int ret;
1703
1704 BT_ASSERT(notit->event);
1705 ret = bt_event_move_header(notit->event,
1706 notit->event_header_field);
1707
1708 if (ret) {
1709 status = BT_NOTIF_ITER_STATUS_ERROR;
1710 goto end;
1711 }
1712
1713 notit->event_header_field = NULL;
1714
1715 /*
1716 * At this point notit->dscopes.stream_event_header has
1717 * the same value as the event header field within
1718 * notit->event.
1719 */
1720 BT_ASSERT(bt_event_borrow_header(notit->event) ==
1721 notit->dscopes.stream_event_header);
1722 }
1723
1724 notit->state = STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN;
1725
1726 end:
1727 return status;
1728 }
1729
1730 static
1731 enum bt_notif_iter_status read_stream_event_context_begin_state(
1732 struct bt_notif_iter *notit)
1733 {
1734 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1735 struct bt_field_type *stream_event_context_type;
1736
1737 stream_event_context_type =
1738 bt_stream_class_borrow_event_context_field_type(
1739 notit->meta.stream_class);
1740 if (!stream_event_context_type) {
1741 notit->state = STATE_DSCOPE_EVENT_CONTEXT_BEGIN;
1742 goto end;
1743 }
1744
1745 BT_ASSERT(!notit->dscopes.stream_event_context);
1746 notit->dscopes.stream_event_context =
1747 bt_event_borrow_stream_event_context(notit->event);
1748 BT_ASSERT(notit->dscopes.stream_event_context);
1749 BT_LOGV("Decoding stream event context field: "
1750 "notit-addr=%p, stream-class-addr=%p, "
1751 "stream-class-name=\"%s\", stream-class-id=%" PRId64 ", "
1752 "ft-addr=%p",
1753 notit, notit->meta.stream_class,
1754 bt_stream_class_get_name(notit->meta.stream_class),
1755 bt_stream_class_get_id(notit->meta.stream_class),
1756 stream_event_context_type);
1757 status = read_dscope_begin_state(notit, stream_event_context_type,
1758 STATE_DSCOPE_EVENT_CONTEXT_BEGIN,
1759 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE,
1760 notit->dscopes.stream_event_context);
1761 if (status < 0) {
1762 BT_LOGW("Cannot decode stream event context field: "
1763 "notit-addr=%p, stream-class-addr=%p, "
1764 "stream-class-name=\"%s\", "
1765 "stream-class-id=%" PRId64 ", ft-addr=%p",
1766 notit, notit->meta.stream_class,
1767 bt_stream_class_get_name(notit->meta.stream_class),
1768 bt_stream_class_get_id(notit->meta.stream_class),
1769 stream_event_context_type);
1770 }
1771
1772 end:
1773 return status;
1774 }
1775
1776 static
1777 enum bt_notif_iter_status read_stream_event_context_continue_state(
1778 struct bt_notif_iter *notit)
1779 {
1780 return read_dscope_continue_state(notit,
1781 STATE_DSCOPE_EVENT_CONTEXT_BEGIN);
1782 }
1783
1784 static
1785 enum bt_notif_iter_status read_event_context_begin_state(
1786 struct bt_notif_iter *notit)
1787 {
1788 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1789 struct bt_field_type *event_context_type;
1790
1791 event_context_type = bt_event_class_borrow_context_field_type(
1792 notit->meta.event_class);
1793 if (!event_context_type) {
1794 notit->state = STATE_DSCOPE_EVENT_PAYLOAD_BEGIN;
1795 goto end;
1796 }
1797
1798 BT_ASSERT(!notit->dscopes.event_context);
1799 notit->dscopes.event_context = bt_event_borrow_context(notit->event);
1800 BT_ASSERT(notit->dscopes.event_context);
1801 BT_LOGV("Decoding event context field: "
1802 "notit-addr=%p, event-class-addr=%p, "
1803 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
1804 "ft-addr=%p",
1805 notit, notit->meta.event_class,
1806 bt_event_class_get_name(notit->meta.event_class),
1807 bt_event_class_get_id(notit->meta.event_class),
1808 event_context_type);
1809 status = read_dscope_begin_state(notit, event_context_type,
1810 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN,
1811 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE,
1812 notit->dscopes.event_context);
1813 if (status < 0) {
1814 BT_LOGW("Cannot decode event context field: "
1815 "notit-addr=%p, event-class-addr=%p, "
1816 "event-class-name=\"%s\", "
1817 "event-class-id=%" PRId64 ", ft-addr=%p",
1818 notit, notit->meta.event_class,
1819 bt_event_class_get_name(notit->meta.event_class),
1820 bt_event_class_get_id(notit->meta.event_class),
1821 event_context_type);
1822 }
1823
1824 end:
1825 return status;
1826 }
1827
1828 static
1829 enum bt_notif_iter_status read_event_context_continue_state(
1830 struct bt_notif_iter *notit)
1831 {
1832 return read_dscope_continue_state(notit,
1833 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN);
1834 }
1835
1836 static
1837 enum bt_notif_iter_status read_event_payload_begin_state(
1838 struct bt_notif_iter *notit)
1839 {
1840 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1841 struct bt_field_type *event_payload_type;
1842
1843 event_payload_type = bt_event_class_borrow_payload_field_type(
1844 notit->meta.event_class);
1845 if (!event_payload_type) {
1846 notit->state = STATE_EMIT_NOTIF_EVENT;
1847 goto end;
1848 }
1849
1850 BT_ASSERT(!notit->dscopes.event_payload);
1851 notit->dscopes.event_payload = bt_event_borrow_payload(notit->event);
1852 BT_ASSERT(notit->dscopes.event_payload);
1853 BT_LOGV("Decoding event payload field: "
1854 "notit-addr=%p, event-class-addr=%p, "
1855 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
1856 "ft-addr=%p",
1857 notit, notit->meta.event_class,
1858 bt_event_class_get_name(notit->meta.event_class),
1859 bt_event_class_get_id(notit->meta.event_class),
1860 event_payload_type);
1861 status = read_dscope_begin_state(notit, event_payload_type,
1862 STATE_EMIT_NOTIF_EVENT,
1863 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE,
1864 notit->dscopes.event_payload);
1865 if (status < 0) {
1866 BT_LOGW("Cannot decode event payload field: "
1867 "notit-addr=%p, event-class-addr=%p, "
1868 "event-class-name=\"%s\", "
1869 "event-class-id=%" PRId64 ", ft-addr=%p",
1870 notit, notit->meta.event_class,
1871 bt_event_class_get_name(notit->meta.event_class),
1872 bt_event_class_get_id(notit->meta.event_class),
1873 event_payload_type);
1874 }
1875
1876 end:
1877 return status;
1878 }
1879
1880 static
1881 enum bt_notif_iter_status read_event_payload_continue_state(
1882 struct bt_notif_iter *notit)
1883 {
1884 return read_dscope_continue_state(notit, STATE_EMIT_NOTIF_EVENT);
1885 }
1886
1887 static
1888 enum bt_notif_iter_status skip_packet_padding_state(
1889 struct bt_notif_iter *notit)
1890 {
1891 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1892 size_t bits_to_skip;
1893
1894 BT_ASSERT(notit->cur_packet_size > 0);
1895 bits_to_skip = notit->cur_packet_size - packet_at(notit);
1896 if (bits_to_skip == 0) {
1897 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1898 goto end;
1899 } else {
1900 size_t bits_to_consume;
1901
1902 BT_LOGV("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu",
1903 bits_to_skip, notit, bits_to_skip);
1904 status = buf_ensure_available_bits(notit);
1905 if (status != BT_NOTIF_ITER_STATUS_OK) {
1906 goto end;
1907 }
1908
1909 bits_to_consume = MIN(buf_available_bits(notit), bits_to_skip);
1910 BT_LOGV("Skipping %zu bits of padding: notit-addr=%p, size=%zu",
1911 bits_to_consume, notit, bits_to_consume);
1912 buf_consume_bits(notit, bits_to_consume);
1913 bits_to_skip = notit->cur_packet_size - packet_at(notit);
1914 if (bits_to_skip == 0) {
1915 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1916 goto end;
1917 }
1918 }
1919
1920 end:
1921 return status;
1922 }
1923
1924 static inline
1925 enum bt_notif_iter_status handle_state(struct bt_notif_iter *notit)
1926 {
1927 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1928 const enum state state = notit->state;
1929
1930 BT_LOGV("Handling state: notit-addr=%p, state=%s",
1931 notit, state_string(state));
1932
1933 // TODO: optimalize!
1934 switch (state) {
1935 case STATE_INIT:
1936 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1937 break;
1938 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
1939 status = read_packet_header_begin_state(notit);
1940 break;
1941 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
1942 status = read_packet_header_continue_state(notit);
1943 break;
1944 case STATE_AFTER_TRACE_PACKET_HEADER:
1945 status = after_packet_header_state(notit);
1946 break;
1947 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN:
1948 status = read_packet_context_begin_state(notit);
1949 break;
1950 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE:
1951 status = read_packet_context_continue_state(notit);
1952 break;
1953 case STATE_AFTER_STREAM_PACKET_CONTEXT:
1954 status = after_packet_context_state(notit);
1955 break;
1956 case STATE_EMIT_NOTIF_NEW_STREAM:
1957 notit->state = STATE_EMIT_NOTIF_NEW_PACKET;
1958 break;
1959 case STATE_EMIT_NOTIF_NEW_PACKET:
1960 notit->state = STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN;
1961 break;
1962 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN:
1963 status = read_event_header_begin_state(notit);
1964 break;
1965 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE:
1966 status = read_event_header_continue_state(notit);
1967 break;
1968 case STATE_AFTER_STREAM_EVENT_HEADER:
1969 status = after_event_header_state(notit);
1970 break;
1971 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN:
1972 status = read_stream_event_context_begin_state(notit);
1973 break;
1974 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE:
1975 status = read_stream_event_context_continue_state(notit);
1976 break;
1977 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN:
1978 status = read_event_context_begin_state(notit);
1979 break;
1980 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE:
1981 status = read_event_context_continue_state(notit);
1982 break;
1983 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN:
1984 status = read_event_payload_begin_state(notit);
1985 break;
1986 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE:
1987 status = read_event_payload_continue_state(notit);
1988 break;
1989 case STATE_EMIT_NOTIF_EVENT:
1990 notit->state = STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN;
1991 break;
1992 case STATE_SKIP_PACKET_PADDING:
1993 status = skip_packet_padding_state(notit);
1994 break;
1995 case STATE_EMIT_NOTIF_END_OF_PACKET:
1996 notit->state = STATE_SKIP_PACKET_PADDING;
1997 break;
1998 default:
1999 BT_LOGD("Unknown CTF plugin notification iterator state: "
2000 "notit-addr=%p, state=%d", notit, notit->state);
2001 abort();
2002 }
2003
2004 BT_LOGV("Handled state: notit-addr=%p, status=%s, "
2005 "prev-state=%s, cur-state=%s",
2006 notit, bt_notif_iter_status_string(status),
2007 state_string(state), state_string(notit->state));
2008 return status;
2009 }
2010
2011 /**
2012 * Resets the internal state of a CTF notification iterator.
2013 */
2014 BT_HIDDEN
2015 void bt_notif_iter_reset(struct bt_notif_iter *notit)
2016 {
2017 BT_ASSERT(notit);
2018 BT_LOGD("Resetting notification iterator: addr=%p", notit);
2019 stack_clear(notit->stack);
2020 notit->meta.stream_class = NULL;
2021 notit->meta.event_class = NULL;
2022 BT_PUT(notit->packet);
2023 BT_PUT(notit->stream);
2024 BT_PUT(notit->event_notif);
2025 release_all_dscopes(notit);
2026 notit->cur_dscope_field = NULL;
2027
2028 if (notit->packet_header_field) {
2029 bt_packet_header_field_release(notit->packet_header_field);
2030 notit->packet_header_field = NULL;
2031 }
2032
2033 if (notit->packet_context_field) {
2034 bt_packet_context_field_release(notit->packet_context_field);
2035 notit->packet_context_field = NULL;
2036 }
2037
2038 if (notit->event_header_field) {
2039 bt_event_header_field_release(notit->event_header_field);
2040 notit->event_header_field = NULL;
2041 }
2042
2043 notit->buf.addr = NULL;
2044 notit->buf.sz = 0;
2045 notit->buf.at = 0;
2046 notit->buf.last_eh_at = SIZE_MAX;
2047 notit->buf.packet_offset = 0;
2048 notit->state = STATE_INIT;
2049 notit->cur_content_size = -1;
2050 notit->cur_packet_size = -1;
2051 notit->cur_packet_offset = -1;
2052 reset_clock_value(&notit->cur_packet_begin_cv);
2053 reset_clock_value(&notit->cur_packet_end_cv);
2054 notit->stream_begin_emitted = false;
2055 notit->cur_timestamp_end = NULL;
2056 }
2057
2058 static
2059 int bt_notif_iter_switch_packet(struct bt_notif_iter *notit)
2060 {
2061 int ret = 0;
2062
2063 /*
2064 * We don't put the stream class here because we need to make
2065 * sure that all the packets processed by the same notification
2066 * iterator refer to the same stream class (the first one).
2067 */
2068 BT_ASSERT(notit);
2069
2070 if (notit->cur_packet_size != -1) {
2071 notit->cur_packet_offset += notit->cur_packet_size;
2072 }
2073
2074 BT_LOGV("Switching packet: notit-addr=%p, cur=%zu, "
2075 "packet-offset=%" PRId64, notit, notit->buf.at,
2076 notit->cur_packet_offset);
2077 stack_clear(notit->stack);
2078 notit->meta.event_class = NULL;
2079 BT_PUT(notit->packet);
2080 BT_PUT(notit->event_notif);
2081 notit->cur_timestamp_end = NULL;
2082 release_all_dscopes(notit);
2083 notit->cur_dscope_field = NULL;
2084
2085 /*
2086 * Adjust current buffer so that addr points to the beginning of the new
2087 * packet.
2088 */
2089 if (notit->buf.addr) {
2090 size_t consumed_bytes = (size_t) (notit->buf.at / CHAR_BIT);
2091
2092 /* Packets are assumed to start on a byte frontier. */
2093 if (notit->buf.at % CHAR_BIT) {
2094 BT_LOGW("Cannot switch packet: current position is not a multiple of 8: "
2095 "notit-addr=%p, cur=%zu", notit, notit->buf.at);
2096 ret = -1;
2097 goto end;
2098 }
2099
2100 notit->buf.addr += consumed_bytes;
2101 notit->buf.sz -= consumed_bytes;
2102 notit->buf.at = 0;
2103 notit->buf.packet_offset = 0;
2104 BT_LOGV("Adjusted buffer: addr=%p, size=%zu",
2105 notit->buf.addr, notit->buf.sz);
2106 }
2107
2108 notit->cur_content_size = -1;
2109 notit->cur_packet_size = -1;
2110 reset_clock_value(&notit->cur_packet_begin_cv);
2111 reset_clock_value(&notit->cur_packet_end_cv);
2112 notit->cur_sc_field_path_cache = NULL;
2113
2114 end:
2115 return ret;
2116 }
2117
2118 static
2119 struct bt_field *borrow_next_field(struct bt_notif_iter *notit)
2120 {
2121 struct bt_field *next_field = NULL;
2122 struct bt_field *base_field;
2123 struct bt_field_type *base_type;
2124 size_t index;
2125
2126 BT_ASSERT(!stack_empty(notit->stack));
2127 index = stack_top(notit->stack)->index;
2128 base_field = stack_top(notit->stack)->base;
2129 BT_ASSERT(base_field);
2130 base_type = bt_field_borrow_type(base_field);
2131 BT_ASSERT(base_type);
2132
2133 switch (bt_field_type_get_type_id(base_type)) {
2134 case BT_FIELD_TYPE_ID_STRUCT:
2135 {
2136 next_field = bt_field_structure_borrow_field_by_index(
2137 base_field, index);
2138 break;
2139 }
2140 case BT_FIELD_TYPE_ID_ARRAY:
2141 next_field = bt_field_array_borrow_field(base_field, index);
2142 break;
2143 case BT_FIELD_TYPE_ID_SEQUENCE:
2144 next_field = bt_field_sequence_borrow_field(base_field, index);
2145 break;
2146 case BT_FIELD_TYPE_ID_VARIANT:
2147 next_field = bt_field_variant_borrow_current_field(base_field);
2148 break;
2149 default:
2150 BT_LOGF("Unknown base field type ID: "
2151 "notit-addr=%p, ft-addr=%p, ft-id=%s",
2152 notit, base_type,
2153 bt_common_field_type_id_string(
2154 bt_field_type_get_type_id(base_type)));
2155 abort();
2156 }
2157
2158 return next_field;
2159 }
2160
2161 static
2162 void update_clock_state(uint64_t *state, struct bt_field *value_field,
2163 struct bt_field_type *value_type)
2164 {
2165 uint64_t requested_new_value;
2166 uint64_t requested_new_value_mask;
2167 uint64_t cur_value_masked;
2168 int requested_new_value_size;
2169 int ret;
2170
2171 BT_ASSERT(value_type);
2172 BT_ASSERT(bt_field_type_is_integer(value_type));
2173 requested_new_value_size =
2174 bt_field_type_integer_get_size(value_type);
2175 BT_ASSERT(requested_new_value_size > 0);
2176 ret = bt_field_integer_unsigned_get_value(value_field,
2177 &requested_new_value);
2178 BT_ASSERT(!ret);
2179
2180 /*
2181 * Special case for a 64-bit new value, which is the limit
2182 * of a clock value as of this version: overwrite the
2183 * current value directly.
2184 */
2185 if (requested_new_value_size == 64) {
2186 *state = requested_new_value;
2187 goto end;
2188 }
2189
2190 requested_new_value_mask = (1ULL << requested_new_value_size) - 1;
2191 cur_value_masked = *state & requested_new_value_mask;
2192
2193 if (requested_new_value < cur_value_masked) {
2194 /*
2195 * It looks like a wrap happened on the number of bits
2196 * of the requested new value. Assume that the clock
2197 * value wrapped only one time.
2198 */
2199 *state += requested_new_value_mask + 1;
2200 }
2201
2202 /* Clear the low bits of the current clock value. */
2203 *state &= ~requested_new_value_mask;
2204
2205 /* Set the low bits of the current clock value. */
2206 *state |= requested_new_value;
2207
2208 end:
2209 BT_LOGV("Updated clock's value from integer field's value: "
2210 "value=%" PRIu64, *state);
2211 }
2212
2213 static
2214 enum bt_btr_status update_clock(struct bt_notif_iter *notit,
2215 struct bt_field *int_field)
2216 {
2217 gboolean clock_class_found;
2218 uint64_t *clock_state = NULL;
2219 struct bt_field_type *int_field_type = NULL;
2220 enum bt_btr_status ret = BT_BTR_STATUS_OK;
2221 struct bt_clock_class *clock_class = NULL;
2222
2223 int_field_type = bt_field_borrow_type(int_field);
2224 BT_ASSERT(int_field_type);
2225
2226 if (bt_field_type_is_enumeration(int_field_type)) {
2227 int_field_type =
2228 bt_field_type_enumeration_borrow_container_field_type(
2229 int_field_type);
2230 }
2231
2232 clock_class = bt_field_type_integer_borrow_mapped_clock_class(
2233 int_field_type);
2234 if (likely(!clock_class)) {
2235 goto end;
2236 }
2237
2238 clock_class_found = g_hash_table_lookup_extended(notit->clock_states,
2239 clock_class, NULL, (gpointer) &clock_state);
2240 if (!clock_class_found) {
2241 clock_state = g_new0(uint64_t, 1);
2242 if (!clock_state) {
2243 BT_LOGE_STR("Failed to allocate a uint64_t.");
2244 ret = BT_BTR_STATUS_ENOMEM;
2245 goto end;
2246 }
2247
2248 g_hash_table_insert(notit->clock_states, clock_class,
2249 clock_state);
2250 }
2251
2252 /* Update the clock's state. */
2253 BT_LOGV("Updating notification iterator's clock's value from integer field: "
2254 "notit-addr=%p, clock-class-addr=%p, "
2255 "clock-class-name=\"%s\", value=%" PRIu64,
2256 notit, clock_class,
2257 bt_clock_class_get_name(clock_class), *clock_state);
2258 update_clock_state(clock_state, int_field, int_field_type);
2259
2260 end:
2261 return ret;
2262 }
2263
2264 static
2265 enum bt_btr_status btr_unsigned_int_common(uint64_t value,
2266 struct bt_field_type *type, void *data,
2267 struct bt_field **out_field)
2268 {
2269 enum bt_btr_status status = BT_BTR_STATUS_OK;
2270 struct bt_field *field = NULL;
2271 struct bt_notif_iter *notit = data;
2272 int ret;
2273
2274 BT_LOGV("Common unsigned integer function called from BTR: "
2275 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2276 "ft-id=%s, value=%" PRIu64,
2277 notit, notit->btr, type,
2278 bt_common_field_type_id_string(
2279 bt_field_type_get_type_id(type)),
2280 value);
2281 field = borrow_next_field(notit);
2282 if (!field) {
2283 BT_LOGW("Cannot borrow next field: notit-addr=%p", notit);
2284 status = BT_BTR_STATUS_ERROR;
2285 goto end;
2286 }
2287
2288 BT_ASSERT(bt_field_is_integer(field) || bt_field_is_enumeration(field));
2289 ret = bt_field_integer_unsigned_set_value(field, value);
2290 BT_ASSERT(ret == 0);
2291 stack_top(notit->stack)->index++;
2292 *out_field = field;
2293
2294 end:
2295 return status;
2296 }
2297
2298 static
2299 enum bt_btr_status btr_timestamp_end_cb(void *value,
2300 struct bt_field_type *type, void *data)
2301 {
2302 enum bt_btr_status status;
2303 struct bt_field *field = NULL;
2304 struct bt_notif_iter *notit = data;
2305 uint64_t uvalue = *((uint64_t *) value);
2306
2307 BT_LOGV("`timestamp_end` unsigned integer function called from BTR: "
2308 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2309 "ft-id=%s",
2310 notit, notit->btr, type,
2311 bt_common_field_type_id_string(
2312 bt_field_type_get_type_id(type)));
2313
2314 status = btr_unsigned_int_common(uvalue, type, data, &field);
2315
2316 /* Set as the current packet's end timestamp field */
2317 notit->cur_timestamp_end = field;
2318 return status;
2319 }
2320
2321 static
2322 enum bt_btr_status btr_unsigned_int_cb(uint64_t value,
2323 struct bt_field_type *type, void *data)
2324 {
2325 struct bt_notif_iter *notit = data;
2326 enum bt_btr_status status = BT_BTR_STATUS_OK;
2327 struct bt_field *field = NULL;
2328 struct field_cb_override *override;
2329
2330 BT_LOGV("Unsigned integer function called from BTR: "
2331 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2332 "ft-id=%s, value=%" PRIu64,
2333 notit, notit->btr, type,
2334 bt_common_field_type_id_string(
2335 bt_field_type_get_type_id(type)),
2336 value);
2337 override = g_hash_table_lookup(notit->field_overrides, type);
2338 if (unlikely(override)) {
2339 /* Override function logs errors */
2340 status = override->func(&value, type, override->data);
2341 goto end;
2342 }
2343
2344 status = btr_unsigned_int_common(value, type, data, &field);
2345 if (status != BT_BTR_STATUS_OK) {
2346 /* btr_unsigned_int_common() logs errors */
2347 goto end;
2348 }
2349
2350 status = update_clock(notit, field);
2351
2352 end:
2353 return status;
2354 }
2355
2356 static
2357 enum bt_btr_status btr_signed_int_cb(int64_t value,
2358 struct bt_field_type *type, void *data)
2359 {
2360 enum bt_btr_status status = BT_BTR_STATUS_OK;
2361 struct bt_field *field = NULL;
2362 struct bt_notif_iter *notit = data;
2363 int ret;
2364
2365 BT_LOGV("Signed integer function called from BTR: "
2366 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2367 "ft-id=%s, value=%" PRId64,
2368 notit, notit->btr, type,
2369 bt_common_field_type_id_string(
2370 bt_field_type_get_type_id(type)),
2371 value);
2372 field = borrow_next_field(notit);
2373 if (!field) {
2374 BT_LOGW("Cannot borrow next field: notit-addr=%p", notit);
2375 status = BT_BTR_STATUS_ERROR;
2376 goto end;
2377 }
2378
2379 BT_ASSERT(bt_field_is_integer(field) || bt_field_is_enumeration(field));
2380 ret = bt_field_integer_signed_set_value(field, value);
2381 BT_ASSERT(ret == 0);
2382 stack_top(notit->stack)->index++;
2383
2384 end:
2385 return status;
2386 }
2387
2388 static
2389 enum bt_btr_status btr_floating_point_cb(double value,
2390 struct bt_field_type *type, void *data)
2391 {
2392 enum bt_btr_status status = BT_BTR_STATUS_OK;
2393 struct bt_field *field = NULL;
2394 struct bt_notif_iter *notit = data;
2395 int ret;
2396
2397 BT_LOGV("Floating point number function called from BTR: "
2398 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2399 "ft-id=%s, value=%f",
2400 notit, notit->btr, type,
2401 bt_common_field_type_id_string(
2402 bt_field_type_get_type_id(type)),
2403 value);
2404 field = borrow_next_field(notit);
2405 if (!field) {
2406 BT_LOGW("Cannot borrow next field: notit-addr=%p", notit);
2407 status = BT_BTR_STATUS_ERROR;
2408 goto end;
2409 }
2410
2411 ret = bt_field_floating_point_set_value(field, value);
2412 BT_ASSERT(!ret);
2413 stack_top(notit->stack)->index++;
2414
2415 end:
2416 return status;
2417 }
2418
2419 static
2420 enum bt_btr_status btr_string_begin_cb(
2421 struct bt_field_type *type, void *data)
2422 {
2423 enum bt_btr_status status = BT_BTR_STATUS_OK;
2424 struct bt_field *field = NULL;
2425 struct bt_notif_iter *notit = data;
2426 int ret;
2427
2428 BT_LOGV("String (beginning) function called from BTR: "
2429 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2430 "ft-id=%s",
2431 notit, notit->btr, type,
2432 bt_common_field_type_id_string(
2433 bt_field_type_get_type_id(type)));
2434 field = borrow_next_field(notit);
2435 if (!field) {
2436 BT_LOGW("Cannot get next field: notit-addr=%p", notit);
2437 status = BT_BTR_STATUS_ERROR;
2438 goto end;
2439 }
2440
2441 ret = bt_field_string_clear(field);
2442 BT_ASSERT(ret == 0);
2443
2444 /*
2445 * Push on stack. Not a compound type per se, but we know that only
2446 * btr_string_cb() may be called between this call and a subsequent
2447 * call to btr_string_end_cb().
2448 */
2449 ret = stack_push(notit->stack, field);
2450 if (ret) {
2451 BT_LOGE("Cannot push string field on stack: "
2452 "notit-addr=%p, field-addr=%p", notit, field);
2453 status = BT_BTR_STATUS_ERROR;
2454 goto end;
2455 }
2456
2457 end:
2458 return status;
2459 }
2460
2461 static
2462 enum bt_btr_status btr_string_cb(const char *value,
2463 size_t len, struct bt_field_type *type, void *data)
2464 {
2465 enum bt_btr_status status = BT_BTR_STATUS_OK;
2466 struct bt_field *field = NULL;
2467 struct bt_notif_iter *notit = data;
2468 int ret;
2469
2470 BT_LOGV("String (substring) function called from BTR: "
2471 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2472 "ft-id=%s, string-length=%zu",
2473 notit, notit->btr, type,
2474 bt_common_field_type_id_string(
2475 bt_field_type_get_type_id(type)),
2476 len);
2477 field = stack_top(notit->stack)->base;
2478 BT_ASSERT(field);
2479
2480 /* Append current substring */
2481 ret = bt_field_string_append_len(field, value, len);
2482 if (ret) {
2483 BT_LOGE("Cannot append substring to string field's value: "
2484 "notit-addr=%p, field-addr=%p, string-length=%zu, "
2485 "ret=%d", notit, field, len, ret);
2486 status = BT_BTR_STATUS_ERROR;
2487 goto end;
2488 }
2489
2490 end:
2491 return status;
2492 }
2493
2494 static
2495 enum bt_btr_status btr_string_end_cb(
2496 struct bt_field_type *type, void *data)
2497 {
2498 struct bt_notif_iter *notit = data;
2499
2500 BT_LOGV("String (end) function called from BTR: "
2501 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2502 "ft-id=%s",
2503 notit, notit->btr, type,
2504 bt_common_field_type_id_string(
2505 bt_field_type_get_type_id(type)));
2506
2507 /* Pop string field */
2508 stack_pop(notit->stack);
2509
2510 /* Go to next field */
2511 stack_top(notit->stack)->index++;
2512 return BT_BTR_STATUS_OK;
2513 }
2514
2515 enum bt_btr_status btr_compound_begin_cb(
2516 struct bt_field_type *type, void *data)
2517 {
2518 enum bt_btr_status status = BT_BTR_STATUS_OK;
2519 struct bt_notif_iter *notit = data;
2520 struct bt_field *field;
2521 int ret;
2522
2523 BT_LOGV("Compound (beginning) function called from BTR: "
2524 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2525 "ft-id=%s",
2526 notit, notit->btr, type,
2527 bt_common_field_type_id_string(
2528 bt_field_type_get_type_id(type)));
2529
2530 /* Borrow field */
2531 if (stack_empty(notit->stack)) {
2532 /* Root: already set by read_dscope_begin_state() */
2533 field = notit->cur_dscope_field;
2534 } else {
2535 field = borrow_next_field(notit);
2536 if (!field) {
2537 BT_LOGW("Cannot borrow next field: notit-addr=%p", notit);
2538 status = BT_BTR_STATUS_ERROR;
2539 goto end;
2540 }
2541 }
2542
2543 /* Push field */
2544 BT_ASSERT(field);
2545 ret = stack_push(notit->stack, field);
2546 if (ret) {
2547 BT_LOGE("Cannot push compound field onto the stack: "
2548 "notit-addr=%p, ft-addr=%p, ft-id=%s, ret=%d",
2549 notit, type,
2550 bt_common_field_type_id_string(
2551 bt_field_type_get_type_id(type)),
2552 ret);
2553 status = BT_BTR_STATUS_ERROR;
2554 goto end;
2555 }
2556
2557 end:
2558 return status;
2559 }
2560
2561 enum bt_btr_status btr_compound_end_cb(
2562 struct bt_field_type *type, void *data)
2563 {
2564 struct bt_notif_iter *notit = data;
2565
2566 BT_LOGV("Compound (end) function called from BTR: "
2567 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2568 "ft-id=%s",
2569 notit, notit->btr, type,
2570 bt_common_field_type_id_string(
2571 bt_field_type_get_type_id(type)));
2572 BT_ASSERT(!stack_empty(notit->stack));
2573
2574 /* Pop stack */
2575 stack_pop(notit->stack);
2576
2577 /* If the stack is not empty, increment the base's index */
2578 if (!stack_empty(notit->stack)) {
2579 stack_top(notit->stack)->index++;
2580 }
2581
2582 return BT_BTR_STATUS_OK;
2583 }
2584
2585 static
2586 struct bt_field *resolve_field(struct bt_notif_iter *notit,
2587 struct bt_field_path *path)
2588 {
2589 struct bt_field *field = NULL;
2590 unsigned int i;
2591
2592 if (BT_LOG_ON_VERBOSE) {
2593 GString *gstr = bt_field_path_string(path);
2594
2595 BT_LOGV("Resolving field path: notit-addr=%p, field-path=\"%s\"",
2596 notit, gstr ? gstr->str : NULL);
2597
2598 if (gstr) {
2599 g_string_free(gstr, TRUE);
2600 }
2601 }
2602
2603 switch (bt_field_path_get_root_scope(path)) {
2604 case BT_SCOPE_TRACE_PACKET_HEADER:
2605 field = notit->dscopes.trace_packet_header;
2606 break;
2607 case BT_SCOPE_STREAM_PACKET_CONTEXT:
2608 field = notit->dscopes.stream_packet_context;
2609 break;
2610 case BT_SCOPE_STREAM_EVENT_HEADER:
2611 field = notit->dscopes.stream_event_header;
2612 break;
2613 case BT_SCOPE_STREAM_EVENT_CONTEXT:
2614 field = notit->dscopes.stream_event_context;
2615 break;
2616 case BT_SCOPE_EVENT_CONTEXT:
2617 field = notit->dscopes.event_context;
2618 break;
2619 case BT_SCOPE_EVENT_FIELDS:
2620 field = notit->dscopes.event_payload;
2621 break;
2622 default:
2623 BT_LOGF("Cannot resolve field path: unknown scope: "
2624 "notit-addr=%p, root-scope=%s",
2625 notit, bt_common_scope_string(
2626 bt_field_path_get_root_scope(path)));
2627 abort();
2628 }
2629
2630 if (!field) {
2631 BT_LOGW("Cannot resolve field path: root field not found: "
2632 "notit-addr=%p, root-scope=%s",
2633 notit, bt_common_scope_string(
2634 bt_field_path_get_root_scope(path)));
2635 goto end;
2636 }
2637
2638 for (i = 0; i < bt_field_path_get_index_count(path); ++i) {
2639 struct bt_field *next_field = NULL;
2640 struct bt_field_type *field_type;
2641 int index = bt_field_path_get_index(path, i);
2642
2643 field_type = bt_field_borrow_type(field);
2644 BT_ASSERT(field_type);
2645
2646 if (bt_field_type_is_structure(field_type)) {
2647 next_field = bt_field_structure_borrow_field_by_index(
2648 field, index);
2649 } else if (bt_field_type_is_variant(field_type)) {
2650 next_field =
2651 bt_field_variant_borrow_current_field(field);
2652 }
2653
2654 field = NULL;
2655
2656 if (!next_field) {
2657 BT_LOGW("Cannot find next field: "
2658 "notit-addr=%p, ft-addr=%p, ft-id=%s, index=%d",
2659 notit, field_type,
2660 bt_common_field_type_id_string(
2661 bt_field_type_get_type_id(field_type)),
2662 index);
2663 goto end;
2664 }
2665
2666 /* Move next field -> field */
2667 field = next_field;
2668 }
2669
2670 end:
2671 return field;
2672 }
2673
2674 static
2675 int64_t btr_get_sequence_length_cb(struct bt_field_type *type, void *data)
2676 {
2677 int64_t ret = -1;
2678 int iret;
2679 struct bt_field *seq_field;
2680 struct bt_field_path *field_path;
2681 struct bt_notif_iter *notit = data;
2682 struct bt_field *length_field = NULL;
2683 uint64_t length;
2684
2685 field_path = bt_field_type_sequence_borrow_length_field_path(type);
2686 BT_ASSERT(field_path);
2687 length_field = resolve_field(notit, field_path);
2688 if (!length_field) {
2689 BT_LOGW("Cannot resolve sequence field type's length field path: "
2690 "notit-addr=%p, ft-addr=%p",
2691 notit, type);
2692 goto end;
2693 }
2694
2695 iret = bt_field_integer_unsigned_get_value(length_field, &length);
2696 if (iret) {
2697 BT_LOGE("Cannot get value of sequence length field: "
2698 "notit-addr=%p, field-addr=%p",
2699 notit, length_field);
2700 goto end;
2701 }
2702
2703 seq_field = stack_top(notit->stack)->base;
2704 iret = bt_field_sequence_set_length(seq_field, length);
2705 if (iret) {
2706 BT_LOGE("Cannot set sequence field's length field: "
2707 "notit-addr=%p, seq-field-addr=%p, "
2708 "length=%" PRIu64,
2709 notit, seq_field, length);
2710 goto end;
2711 }
2712
2713 ret = (int64_t) length;
2714
2715 end:
2716 return ret;
2717 }
2718
2719 static
2720 struct bt_field_type *btr_borrow_variant_field_type_cb(
2721 struct bt_field_type *type, void *data)
2722 {
2723 int ret;
2724 struct bt_field_path *path;
2725 struct bt_notif_iter *notit = data;
2726 struct bt_field *var_field;
2727 struct bt_field *tag_field = NULL;
2728 struct bt_field_type *tag_ft = NULL;
2729 struct bt_field_type *tag_int_ft = NULL;
2730 struct bt_field *selected_field = NULL;
2731 struct bt_field_type *selected_field_type = NULL;
2732
2733 path = bt_field_type_variant_borrow_tag_field_path(type);
2734 BT_ASSERT(path);
2735 tag_field = resolve_field(notit, path);
2736 if (!tag_field) {
2737 BT_LOGW("Cannot resolve variant field type's tag field path: "
2738 "notit-addr=%p, ft-addr=%p",
2739 notit, type);
2740 goto end;
2741 }
2742
2743 /*
2744 * We found the enumeration tag field instance which should be
2745 * able to select a current field for this variant. This
2746 * callback function we're in is called _after_
2747 * compound_begin(), so the current stack top's base field is
2748 * the variant field in question. We set the variant field's tag
2749 * here and then get the current (selected) field thanks to this
2750 * tag field's value. This current field will also provide us
2751 * with its type. Then, this current field will remain the
2752 * current selected one until the next callback function call
2753 * which is used to fill it.
2754 */
2755 var_field = stack_top(notit->stack)->base;
2756 tag_ft = bt_field_borrow_type(tag_field);
2757 tag_int_ft = bt_field_type_enumeration_borrow_container_field_type(
2758 tag_ft);
2759
2760 if (bt_field_type_integer_is_signed(tag_int_ft)) {
2761 int64_t tag_value;
2762
2763 ret = bt_field_integer_signed_get_value(tag_field, &tag_value);
2764 BT_ASSERT(ret == 0);
2765 ret = bt_field_variant_set_tag_signed(var_field, tag_value);
2766 BT_ASSERT(ret == 0);
2767 } else {
2768 uint64_t tag_value;
2769
2770 ret = bt_field_integer_unsigned_get_value(tag_field,
2771 &tag_value);
2772 BT_ASSERT(ret == 0);
2773 ret = bt_field_variant_set_tag_unsigned(var_field, tag_value);
2774 BT_ASSERT(ret == 0);
2775 }
2776
2777 selected_field = bt_field_variant_borrow_current_field(var_field);
2778 if (!selected_field) {
2779 BT_LOGW("Cannot borrow variant field's current field: "
2780 "notit-addr=%p, var-field-addr=%p",
2781 notit, var_field);
2782 goto end;
2783 }
2784
2785 selected_field_type = bt_field_borrow_type(selected_field);
2786
2787 end:
2788 return selected_field_type;
2789 }
2790
2791 static
2792 int set_event_clocks(struct bt_notif_iter *notit)
2793 {
2794 int ret;
2795 GHashTableIter iter;
2796 struct bt_clock_class *clock_class;
2797 uint64_t *clock_state;
2798
2799 g_hash_table_iter_init(&iter, notit->clock_states);
2800
2801 while (g_hash_table_iter_next(&iter, (gpointer) &clock_class,
2802 (gpointer) &clock_state)) {
2803 ret = bt_event_set_clock_value(notit->event, clock_class,
2804 *clock_state, BT_TRUE);
2805 if (ret) {
2806 BT_LOGE("Cannot set event's default clock value: "
2807 "notit-addr=%p, clock-class-addr=%p, "
2808 "clock-class-name=\"%s\"",
2809 notit, clock_class,
2810 bt_clock_class_get_name(clock_class));
2811 ret = -1;
2812 goto end;
2813 }
2814 }
2815
2816 ret = 0;
2817
2818 end:
2819 return ret;
2820 }
2821
2822 static
2823 void notify_new_stream(struct bt_notif_iter *notit,
2824 struct bt_notification **notification)
2825 {
2826 enum bt_notif_iter_status status;
2827 struct bt_notification *ret = NULL;
2828
2829 status = set_current_stream(notit);
2830 if (status != BT_NOTIF_ITER_STATUS_OK) {
2831 BT_PUT(ret);
2832 goto end;
2833 }
2834
2835 BT_ASSERT(notit->stream);
2836 BT_ASSERT(notit->notif_iter);
2837 ret = bt_notification_stream_begin_create(notit->notif_iter,
2838 notit->stream);
2839 if (!ret) {
2840 BT_LOGE("Cannot create stream beginning notification: "
2841 "notit-addr=%p, stream-addr=%p",
2842 notit, notit->stream);
2843 return;
2844 }
2845
2846 end:
2847 *notification = ret;
2848 }
2849
2850 static
2851 void notify_end_of_stream(struct bt_notif_iter *notit,
2852 struct bt_notification **notification)
2853 {
2854 struct bt_notification *ret;
2855
2856 if (!notit->stream) {
2857 BT_LOGE("Cannot create stream for stream notification: "
2858 "notit-addr=%p", notit);
2859 return;
2860 }
2861
2862 BT_ASSERT(notit->notif_iter);
2863 ret = bt_notification_stream_end_create(notit->notif_iter,
2864 notit->stream);
2865 if (!ret) {
2866 BT_LOGE("Cannot create stream beginning notification: "
2867 "notit-addr=%p, stream-addr=%p",
2868 notit, notit->stream);
2869 return;
2870 }
2871 *notification = ret;
2872 }
2873
2874 static
2875 void notify_new_packet(struct bt_notif_iter *notit,
2876 struct bt_notification **notification)
2877 {
2878 int ret;
2879 enum bt_notif_iter_status status;
2880 struct bt_notification *notif = NULL;
2881
2882 status = set_current_packet(notit);
2883 if (status != BT_NOTIF_ITER_STATUS_OK) {
2884 goto end;
2885 }
2886
2887 BT_ASSERT(notit->packet);
2888
2889 if (notit->packet_header_field) {
2890 ret = bt_packet_move_header(notit->packet,
2891 notit->packet_header_field);
2892 if (ret) {
2893 goto end;
2894 }
2895
2896 notit->packet_header_field = NULL;
2897
2898 /*
2899 * At this point notit->dscopes.trace_packet_header has
2900 * the same value as the packet header field within
2901 * notit->packet.
2902 */
2903 BT_ASSERT(bt_packet_borrow_header(notit->packet) ==
2904 notit->dscopes.trace_packet_header);
2905 }
2906
2907 if (notit->packet_context_field) {
2908 ret = bt_packet_move_context(notit->packet,
2909 notit->packet_context_field);
2910 if (ret) {
2911 goto end;
2912 }
2913
2914 notit->packet_context_field = NULL;
2915
2916 /*
2917 * At this point notit->dscopes.trace_packet_header has
2918 * the same value as the packet header field within
2919 * notit->packet.
2920 */
2921 BT_ASSERT(bt_packet_borrow_context(notit->packet) ==
2922 notit->dscopes.stream_packet_context);
2923 }
2924
2925 if (notit->cur_packet_begin_cv.clock_class) {
2926 ret = bt_packet_set_beginning_clock_value(notit->packet,
2927 notit->cur_packet_begin_cv.clock_class,
2928 notit->cur_packet_begin_cv.raw_value, BT_TRUE);
2929 if (ret) {
2930 BT_LOGE("Cannot set packet's default beginning clock value: "
2931 "notit-addr=%p, packet-addr=%p",
2932 notit, notit->packet);
2933 goto end;
2934 }
2935 }
2936
2937 if (notit->cur_packet_end_cv.clock_class) {
2938 ret = bt_packet_set_end_clock_value(notit->packet,
2939 notit->cur_packet_end_cv.clock_class,
2940 notit->cur_packet_end_cv.raw_value, BT_TRUE);
2941 if (ret) {
2942 BT_LOGE("Cannot set packet's default end clock value: "
2943 "notit-addr=%p, packet-addr=%p",
2944 notit, notit->packet);
2945 goto end;
2946 }
2947 }
2948
2949 BT_ASSERT(notit->notif_iter);
2950 notif = bt_notification_packet_begin_create(notit->notif_iter,
2951 notit->packet);
2952 if (!notif) {
2953 BT_LOGE("Cannot create packet beginning notification: "
2954 "notit-addr=%p, packet-addr=%p",
2955 notit, notit->packet);
2956 goto end;
2957 }
2958
2959 *notification = notif;
2960
2961 end:
2962 return;
2963 }
2964
2965 static
2966 void notify_end_of_packet(struct bt_notif_iter *notit,
2967 struct bt_notification **notification)
2968 {
2969 struct bt_notification *notif;
2970
2971 if (!notit->packet) {
2972 return;
2973 }
2974
2975 BT_ASSERT(notit->notif_iter);
2976 notif = bt_notification_packet_end_create(notit->notif_iter,
2977 notit->packet);
2978 if (!notif) {
2979 BT_LOGE("Cannot create packet end notification: "
2980 "notit-addr=%p, packet-addr=%p",
2981 notit, notit->packet);
2982 return;
2983 }
2984
2985 BT_PUT(notit->packet);
2986 *notification = notif;
2987 }
2988
2989 static
2990 void init_trace_field_path_cache(struct bt_trace *trace,
2991 struct trace_field_path_cache *trace_field_path_cache)
2992 {
2993 int stream_id = -1;
2994 int stream_instance_id = -1;
2995 int i, count;
2996 struct bt_field_type *packet_header = NULL;
2997
2998 packet_header = bt_trace_borrow_packet_header_field_type(trace);
2999 if (!packet_header) {
3000 goto end;
3001 }
3002
3003 if (!bt_field_type_is_structure(packet_header)) {
3004 goto end;
3005 }
3006
3007 count = bt_field_type_structure_get_field_count(packet_header);
3008 BT_ASSERT(count >= 0);
3009
3010 for (i = 0; (i < count && (stream_id == -1 || stream_instance_id == -1)); i++) {
3011 int ret;
3012 const char *field_name;
3013
3014 ret = bt_field_type_structure_borrow_field_by_index(
3015 packet_header, &field_name, NULL, i);
3016 if (ret) {
3017 BT_LOGE("Cannot get structure field's field: "
3018 "field-addr=%p, index=%d",
3019 packet_header, i);
3020 goto end;
3021 }
3022
3023 if (stream_id == -1 && !strcmp(field_name, "stream_id")) {
3024 stream_id = i;
3025 } else if (stream_instance_id == -1 &&
3026 !strcmp(field_name, "stream_instance_id")) {
3027 stream_instance_id = i;
3028 }
3029 }
3030
3031 end:
3032 trace_field_path_cache->stream_id = stream_id;
3033 trace_field_path_cache->stream_instance_id = stream_instance_id;
3034 }
3035
3036 BT_HIDDEN
3037 struct bt_notif_iter *bt_notif_iter_create(struct bt_trace *trace,
3038 size_t max_request_sz,
3039 struct bt_notif_iter_medium_ops medops, void *data)
3040 {
3041 struct bt_notif_iter *notit = NULL;
3042 struct bt_btr_cbs cbs = {
3043 .types = {
3044 .signed_int = btr_signed_int_cb,
3045 .unsigned_int = btr_unsigned_int_cb,
3046 .floating_point = btr_floating_point_cb,
3047 .string_begin = btr_string_begin_cb,
3048 .string = btr_string_cb,
3049 .string_end = btr_string_end_cb,
3050 .compound_begin = btr_compound_begin_cb,
3051 .compound_end = btr_compound_end_cb,
3052 },
3053 .query = {
3054 .get_sequence_length = btr_get_sequence_length_cb,
3055 .borrow_variant_field_type = btr_borrow_variant_field_type_cb,
3056 },
3057 };
3058
3059 BT_ASSERT(trace);
3060 BT_ASSERT(medops.request_bytes);
3061 BT_ASSERT(medops.borrow_stream);
3062 BT_LOGD("Creating CTF plugin notification iterator: "
3063 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
3064 "data=%p",
3065 trace, bt_trace_get_name(trace), max_request_sz, data);
3066 notit = g_new0(struct bt_notif_iter, 1);
3067 if (!notit) {
3068 BT_LOGE_STR("Failed to allocate one CTF plugin notification iterator.");
3069 goto end;
3070 }
3071 notit->clock_states = g_hash_table_new_full(g_direct_hash,
3072 g_direct_equal, NULL, g_free);
3073 if (!notit->clock_states) {
3074 BT_LOGE_STR("Failed to allocate a GHashTable.");
3075 goto error;
3076 }
3077 notit->meta.trace = trace;
3078 notit->medium.medops = medops;
3079 notit->medium.max_request_sz = max_request_sz;
3080 notit->medium.data = data;
3081 notit->stack = stack_new(notit);
3082 if (!notit->stack) {
3083 BT_LOGE_STR("Failed to create field stack.");
3084 goto error;
3085 }
3086
3087 notit->btr = bt_btr_create(cbs, notit);
3088 if (!notit->btr) {
3089 BT_LOGE_STR("Failed to create binary type reader (BTR).");
3090 goto error;
3091 }
3092
3093 bt_notif_iter_reset(notit);
3094 init_trace_field_path_cache(trace, &notit->trace_field_path_cache);
3095 notit->sc_field_path_caches = g_hash_table_new_full(g_direct_hash,
3096 g_direct_equal, NULL, g_free);
3097 if (!notit->sc_field_path_caches) {
3098 BT_LOGE_STR("Failed to allocate a GHashTable.");
3099 goto error;
3100 }
3101
3102 notit->field_overrides = g_hash_table_new_full(g_direct_hash,
3103 g_direct_equal, NULL, g_free);
3104 if (!notit->field_overrides) {
3105 BT_LOGE_STR("Failed to allocate a GHashTable.");
3106 goto error;
3107 }
3108
3109 BT_LOGD("Created CTF plugin notification iterator: "
3110 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
3111 "data=%p, notit-addr=%p",
3112 trace, bt_trace_get_name(trace), max_request_sz, data,
3113 notit);
3114 notit->cur_packet_offset = 0;
3115
3116 end:
3117 return notit;
3118
3119 error:
3120 bt_notif_iter_destroy(notit);
3121 notit = NULL;
3122 goto end;
3123 }
3124
3125 void bt_notif_iter_destroy(struct bt_notif_iter *notit)
3126 {
3127 BT_PUT(notit->packet);
3128 BT_PUT(notit->stream);
3129 release_all_dscopes(notit);
3130
3131 BT_LOGD("Destroying CTF plugin notification iterator: addr=%p", notit);
3132
3133 if (notit->stack) {
3134 BT_LOGD_STR("Destroying field stack.");
3135 stack_destroy(notit->stack);
3136 }
3137
3138 if (notit->btr) {
3139 BT_LOGD("Destroying BTR: btr-addr=%p", notit->btr);
3140 bt_btr_destroy(notit->btr);
3141 }
3142
3143 if (notit->clock_states) {
3144 g_hash_table_destroy(notit->clock_states);
3145 }
3146
3147 if (notit->sc_field_path_caches) {
3148 g_hash_table_destroy(notit->sc_field_path_caches);
3149 }
3150
3151 if (notit->field_overrides) {
3152 g_hash_table_destroy(notit->field_overrides);
3153 }
3154
3155 g_free(notit);
3156 }
3157
3158 enum bt_notif_iter_status bt_notif_iter_get_next_notification(
3159 struct bt_notif_iter *notit,
3160 struct bt_private_connection_private_notification_iterator *notif_iter,
3161 struct bt_notification **notification)
3162 {
3163 int ret;
3164 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
3165
3166 BT_ASSERT(notit);
3167 BT_ASSERT(notification);
3168
3169 if (notit->state == STATE_DONE) {
3170 status = BT_NOTIF_ITER_STATUS_EOF;
3171 goto end;
3172 }
3173
3174 notit->notif_iter = notif_iter;
3175
3176 BT_LOGV("Getting next notification: notit-addr=%p", notit);
3177
3178 while (true) {
3179 status = handle_state(notit);
3180 if (status == BT_NOTIF_ITER_STATUS_AGAIN) {
3181 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_AGAIN.");
3182 goto end;
3183 }
3184
3185 if (status != BT_NOTIF_ITER_STATUS_OK) {
3186 if (status == BT_NOTIF_ITER_STATUS_EOF) {
3187 enum state next_state = notit->state;
3188
3189 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_EOF.");
3190
3191 if (notit->packet) {
3192 notify_end_of_packet(notit,
3193 notification);
3194 } else {
3195 notify_end_of_stream(notit,
3196 notification);
3197 next_state = STATE_DONE;
3198 }
3199
3200 if (!*notification) {
3201 status = BT_NOTIF_ITER_STATUS_ERROR;
3202 goto end;
3203 }
3204
3205 status = BT_NOTIF_ITER_STATUS_OK;
3206 notit->state = next_state;
3207 } else {
3208 BT_LOGW("Cannot handle state: "
3209 "notit-addr=%p, state=%s",
3210 notit, state_string(notit->state));
3211 }
3212
3213 goto end;
3214 }
3215
3216 switch (notit->state) {
3217 case STATE_EMIT_NOTIF_NEW_STREAM:
3218 /* notify_new_stream() logs errors */
3219 notify_new_stream(notit, notification);
3220 if (!*notification) {
3221 status = BT_NOTIF_ITER_STATUS_ERROR;
3222 }
3223 notit->stream_begin_emitted = true;
3224 goto end;
3225 case STATE_EMIT_NOTIF_NEW_PACKET:
3226 /* notify_new_packet() logs errors */
3227 notify_new_packet(notit, notification);
3228 if (!*notification) {
3229 status = BT_NOTIF_ITER_STATUS_ERROR;
3230 }
3231
3232 goto end;
3233 case STATE_EMIT_NOTIF_EVENT:
3234 BT_ASSERT(notit->event_notif);
3235 ret = set_event_clocks(notit);
3236 if (ret) {
3237 status = BT_NOTIF_ITER_STATUS_ERROR;
3238 goto end;
3239 }
3240
3241 *notification = notit->event_notif;
3242 notit->event_notif = NULL;
3243 goto end;
3244 case STATE_EMIT_NOTIF_END_OF_PACKET:
3245 /* Update clock with timestamp_end field. */
3246 if (notit->cur_timestamp_end) {
3247 enum bt_btr_status btr_status;
3248
3249 btr_status = update_clock(notit,
3250 notit->cur_timestamp_end);
3251 if (btr_status != BT_BTR_STATUS_OK) {
3252 BT_LOGW("Cannot update stream's clock value: "
3253 "notit-addr=%p", notit);
3254 status = BT_NOTIF_ITER_STATUS_ERROR;
3255 goto end;
3256 }
3257 }
3258
3259 /* notify_end_of_packet() logs errors */
3260 notify_end_of_packet(notit, notification);
3261 if (!*notification) {
3262 status = BT_NOTIF_ITER_STATUS_ERROR;
3263 }
3264
3265 goto end;
3266 default:
3267 /* Non-emitting state: continue */
3268 break;
3269 }
3270 }
3271
3272 end:
3273 return status;
3274 }
3275
3276 BT_HIDDEN
3277 enum bt_notif_iter_status bt_notif_iter_borrow_packet_header_context_fields(
3278 struct bt_notif_iter *notit,
3279 struct bt_field **packet_header_field,
3280 struct bt_field **packet_context_field)
3281 {
3282 int ret;
3283 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
3284
3285 BT_ASSERT(notit);
3286
3287 if (notit->state == STATE_EMIT_NOTIF_NEW_PACKET) {
3288 /* We're already there */
3289 goto set_fields;
3290 }
3291
3292 while (true) {
3293 status = handle_state(notit);
3294 if (status == BT_NOTIF_ITER_STATUS_AGAIN) {
3295 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_AGAIN.");
3296 goto end;
3297 }
3298 if (status != BT_NOTIF_ITER_STATUS_OK) {
3299 if (status == BT_NOTIF_ITER_STATUS_EOF) {
3300 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_EOF.");
3301 } else {
3302 BT_LOGW("Cannot handle state: "
3303 "notit-addr=%p, state=%s",
3304 notit, state_string(notit->state));
3305 }
3306 goto end;
3307 }
3308
3309 switch (notit->state) {
3310 case STATE_EMIT_NOTIF_NEW_PACKET:
3311 /*
3312 * Packet header and context fields are
3313 * potentially decoded (or they don't exist).
3314 */
3315 goto set_fields;
3316 case STATE_INIT:
3317 case STATE_EMIT_NOTIF_NEW_STREAM:
3318 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
3319 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
3320 case STATE_AFTER_TRACE_PACKET_HEADER:
3321 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN:
3322 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE:
3323 case STATE_AFTER_STREAM_PACKET_CONTEXT:
3324 /* Non-emitting state: continue */
3325 break;
3326 default:
3327 /*
3328 * We should never get past the
3329 * STATE_EMIT_NOTIF_NEW_PACKET state.
3330 */
3331 BT_LOGF("Unexpected state: notit-addr=%p, state=%s",
3332 notit, state_string(notit->state));
3333 abort();
3334 }
3335 }
3336
3337 set_fields:
3338 ret = set_current_packet_content_sizes(notit);
3339 if (ret) {
3340 status = BT_NOTIF_ITER_STATUS_ERROR;
3341 goto end;
3342 }
3343
3344 if (packet_header_field) {
3345 *packet_header_field = notit->dscopes.trace_packet_header;
3346 }
3347
3348 if (packet_context_field) {
3349 *packet_context_field = notit->dscopes.stream_packet_context;
3350 }
3351
3352 end:
3353 return status;
3354 }
3355
3356 BT_HIDDEN
3357 void bt_notif_iter_set_medops_data(struct bt_notif_iter *notit,
3358 void *medops_data)
3359 {
3360 BT_ASSERT(notit);
3361 notit->medium.data = medops_data;
3362 }
3363
3364 BT_HIDDEN
3365 enum bt_notif_iter_status bt_notif_iter_seek(
3366 struct bt_notif_iter *notit, off_t offset)
3367 {
3368 enum bt_notif_iter_status ret = BT_NOTIF_ITER_STATUS_OK;
3369 enum bt_notif_iter_medium_status medium_status;
3370
3371 BT_ASSERT(notit);
3372 if (offset < 0) {
3373 BT_LOGE("Cannot seek to negative offset: offset=%jd", offset);
3374 ret = BT_NOTIF_ITER_STATUS_INVAL;
3375 goto end;
3376 }
3377
3378 if (!notit->medium.medops.seek) {
3379 ret = BT_NOTIF_ITER_STATUS_UNSUPPORTED;
3380 BT_LOGD("Aborting seek as the iterator's underlying media does not implement seek support.");
3381 goto end;
3382 }
3383
3384 medium_status = notit->medium.medops.seek(
3385 BT_NOTIF_ITER_SEEK_WHENCE_SET, offset,
3386 notit->medium.data);
3387 if (medium_status != BT_NOTIF_ITER_MEDIUM_STATUS_OK) {
3388 if (medium_status == BT_NOTIF_ITER_MEDIUM_STATUS_EOF) {
3389 ret = BT_NOTIF_ITER_STATUS_EOF;
3390 } else {
3391 ret = BT_NOTIF_ITER_STATUS_ERROR;
3392 goto end;
3393 }
3394 }
3395
3396 bt_notif_iter_reset(notit);
3397 notit->cur_packet_offset = offset;
3398 end:
3399 return ret;
3400 }
3401
3402 BT_HIDDEN
3403 off_t bt_notif_iter_get_current_packet_offset(
3404 struct bt_notif_iter *notit)
3405 {
3406 BT_ASSERT(notit);
3407 return notit->cur_packet_offset;
3408 }
3409
3410 BT_HIDDEN
3411 off_t bt_notif_iter_get_current_packet_size(
3412 struct bt_notif_iter *notit)
3413 {
3414 BT_ASSERT(notit);
3415 return notit->cur_packet_size;
3416 }
This page took 0.139617 seconds and 5 git commands to generate.