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