ctf plugin: notif iter: use "borrow" functions for metadata 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_borrow_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
717 end:
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_borrow_event_header_field_type(
764 stream_class);
765 if (event_header && bt_field_type_is_structure(event_header)) {
766 int i, count;
767
768 count = bt_field_type_structure_get_field_count(
769 event_header);
770 BT_ASSERT(count >= 0);
771
772 for (i = 0; i < count; i++) {
773 int ret;
774 const char *name;
775
776 ret = bt_field_type_structure_borrow_field_by_index(
777 event_header, &name, NULL, i);
778 if (ret) {
779 BT_LOGE("Cannot get event header structure field type's field: "
780 "notit-addr=%p, stream-class-addr=%p, "
781 "stream-class-name=\"%s\", "
782 "stream-class-id=%" PRId64 ", "
783 "ft-addr=%p, index=%d",
784 notit, stream_class,
785 bt_stream_class_get_name(stream_class),
786 bt_stream_class_get_id(stream_class),
787 event_header, i);
788 goto error;
789 }
790
791 if (v != -1 && id != -1) {
792 break;
793 }
794
795 if (v == -1 && strcmp(name, "v") == 0) {
796 v = i;
797 } else if (id == -1 && !strcmp(name, "id")) {
798 id = i;
799 }
800 }
801 }
802
803 packet_context = bt_stream_class_borrow_packet_context_field_type(
804 stream_class);
805 if (packet_context && bt_field_type_is_structure(packet_context)) {
806 int i, count;
807
808 count = bt_field_type_structure_get_field_count(
809 packet_context);
810 BT_ASSERT(count >= 0);
811
812 for (i = 0; i < count; i++) {
813 int ret;
814 const char *name;
815 struct bt_field_type *field_type;
816
817 if (timestamp_end != -1 && packet_size != -1 &&
818 content_size != -1) {
819 break;
820 }
821
822 ret = bt_field_type_structure_borrow_field_by_index(
823 packet_context, &name, &field_type, i);
824 if (ret) {
825 BT_LOGE("Cannot get packet context structure field type's field: "
826 "notit-addr=%p, stream-class-addr=%p, "
827 "stream-class-name=\"%s\", "
828 "stream-class-id=%" PRId64 ", "
829 "ft-addr=%p, index=%d",
830 notit, stream_class,
831 bt_stream_class_get_name(stream_class),
832 bt_stream_class_get_id(stream_class),
833 event_header, i);
834 goto error;
835 }
836
837 if (timestamp_end == -1 &&
838 strcmp(name, "timestamp_end") == 0) {
839 struct field_cb_override *override = g_new0(
840 struct field_cb_override, 1);
841
842 if (!override) {
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 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 }
859 }
860
861 cache_entry->v = v;
862 cache_entry->id = id;
863 cache_entry->timestamp_end = timestamp_end;
864 cache_entry->packet_size = packet_size;
865 cache_entry->content_size = content_size;
866
867 end:
868 return cache_entry;
869
870 error:
871 g_free(cache_entry);
872 cache_entry = NULL;
873 goto end;
874 }
875
876 static
877 struct stream_class_field_path_cache *get_stream_class_field_path_cache(
878 struct bt_notif_iter *notit,
879 struct bt_stream_class *stream_class)
880 {
881 bool cache_entry_found;
882 struct stream_class_field_path_cache *cache_entry;
883
884 cache_entry_found = g_hash_table_lookup_extended(
885 notit->sc_field_path_caches,
886 stream_class, NULL, (gpointer) &cache_entry);
887 if (unlikely(!cache_entry_found)) {
888 cache_entry = create_stream_class_field_path_cache_entry(notit,
889 stream_class);
890 g_hash_table_insert(notit->sc_field_path_caches,
891 stream_class, (gpointer) cache_entry);
892 }
893
894 return cache_entry;
895 }
896
897 static inline
898 enum bt_notif_iter_status set_current_stream_class(
899 struct bt_notif_iter *notit)
900 {
901 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
902 struct bt_field_type *packet_header_type = NULL;
903 struct bt_field_type *stream_id_field_type = NULL;
904 struct bt_stream_class *new_stream_class = NULL;
905 uint64_t stream_id;
906
907 /* Clear the current stream class field path cache. */
908 notit->cur_sc_field_path_cache = NULL;
909
910 /* Is there any "stream_id" field in the packet header? */
911 packet_header_type = bt_trace_borrow_packet_header_field_type(
912 notit->meta.trace);
913 if (!packet_header_type) {
914 /*
915 * No packet header, therefore no `stream_id` field,
916 * therefore only one stream class.
917 */
918 goto single_stream_class;
919 }
920
921 BT_ASSERT(is_struct_type(packet_header_type));
922
923 // TODO: optimalize!
924 stream_id_field_type =
925 bt_field_type_structure_borrow_field_type_by_name(
926 packet_header_type, "stream_id");
927 if (stream_id_field_type) {
928 /* Find appropriate stream class using current stream ID */
929 int ret;
930 struct bt_field *stream_id_field = NULL;
931
932 BT_ASSERT(notit->dscopes.trace_packet_header);
933
934 // TODO: optimalize!
935 stream_id_field = bt_field_structure_get_field_by_name(
936 notit->dscopes.trace_packet_header, "stream_id");
937 BT_ASSERT(stream_id_field);
938 ret = bt_field_integer_unsigned_get_value(
939 stream_id_field, &stream_id);
940 BT_ASSERT(!ret);
941 BT_PUT(stream_id_field);
942 } else {
943 single_stream_class:
944 /* Only one stream: pick the first stream class */
945 BT_ASSERT(bt_trace_get_stream_class_count(
946 notit->meta.trace) == 1);
947 stream_id = 0;
948 }
949
950 BT_LOGV("Found stream class ID to use: notit-addr=%p, "
951 "stream-class-id=%" PRIu64 ", "
952 "trace-addr=%p, trace-name=\"%s\"",
953 notit, stream_id, notit->meta.trace,
954 bt_trace_get_name(notit->meta.trace));
955
956 new_stream_class = bt_trace_borrow_stream_class_by_id(
957 notit->meta.trace, stream_id);
958 if (!new_stream_class) {
959 BT_LOGW("No stream class with ID of stream class ID to use in trace: "
960 "notit-addr=%p, stream-class-id=%" PRIu64 ", "
961 "trace-addr=%p, trace-name=\"%s\"",
962 notit, stream_id, notit->meta.trace,
963 bt_trace_get_name(notit->meta.trace));
964 status = BT_NOTIF_ITER_STATUS_ERROR;
965 goto end;
966 }
967
968 if (notit->meta.stream_class) {
969 if (new_stream_class != notit->meta.stream_class) {
970 BT_LOGW("Two packets refer to two different stream classes within the same packet sequence: "
971 "notit-addr=%p, prev-stream-class-addr=%p, "
972 "prev-stream-class-name=\"%s\", "
973 "prev-stream-class-id=%" PRId64 ", "
974 "next-stream-class-addr=%p, "
975 "next-stream-class-name=\"%s\", "
976 "next-stream-class-id=%" PRId64 ", "
977 "trace-addr=%p, trace-name=\"%s\"",
978 notit, notit->meta.stream_class,
979 bt_stream_class_get_name(notit->meta.stream_class),
980 bt_stream_class_get_id(notit->meta.stream_class),
981 new_stream_class,
982 bt_stream_class_get_name(new_stream_class),
983 bt_stream_class_get_id(new_stream_class),
984 notit->meta.trace,
985 bt_trace_get_name(notit->meta.trace));
986 status = BT_NOTIF_ITER_STATUS_ERROR;
987 goto end;
988 }
989 } else {
990 notit->meta.stream_class = new_stream_class;
991 }
992
993 BT_LOGV("Set current stream class: "
994 "notit-addr=%p, stream-class-addr=%p, "
995 "stream-class-name=\"%s\", stream-class-id=%" PRId64,
996 notit, notit->meta.stream_class,
997 bt_stream_class_get_name(notit->meta.stream_class),
998 bt_stream_class_get_id(notit->meta.stream_class));
999
1000 /*
1001 * Retrieve (or lazily create) the current stream class field path
1002 * cache.
1003 */
1004 notit->cur_sc_field_path_cache = get_stream_class_field_path_cache(
1005 notit, notit->meta.stream_class);
1006 if (!notit->cur_sc_field_path_cache) {
1007 BT_LOGW("Cannot retrieve stream class field path from cache: "
1008 "notit-addr=%p, stream-class-addr=%p, "
1009 "stream-class-name=\"%s\", stream-class-id=%" PRId64,
1010 notit, notit->meta.stream_class,
1011 bt_stream_class_get_name(notit->meta.stream_class),
1012 bt_stream_class_get_id(notit->meta.stream_class));
1013 status = BT_NOTIF_ITER_STATUS_ERROR;
1014 goto end;
1015 }
1016
1017 end:
1018 return status;
1019 }
1020
1021 static
1022 enum bt_notif_iter_status after_packet_header_state(
1023 struct bt_notif_iter *notit)
1024 {
1025 enum bt_notif_iter_status status;
1026
1027 status = set_current_stream_class(notit);
1028 if (status == BT_NOTIF_ITER_STATUS_OK) {
1029 notit->state = STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN;
1030 }
1031
1032 return status;
1033 }
1034
1035 static
1036 enum bt_notif_iter_status read_packet_context_begin_state(
1037 struct bt_notif_iter *notit)
1038 {
1039 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1040 struct bt_field_type *packet_context_type;
1041
1042 BT_ASSERT(notit->meta.stream_class);
1043 packet_context_type = bt_stream_class_borrow_packet_context_field_type(
1044 notit->meta.stream_class);
1045 if (!packet_context_type) {
1046 BT_LOGV("No packet packet context field type in stream class: continuing: "
1047 "notit-addr=%p, stream-class-addr=%p, "
1048 "stream-class-name=\"%s\", stream-class-id=%" PRId64,
1049 notit, notit->meta.stream_class,
1050 bt_stream_class_get_name(notit->meta.stream_class),
1051 bt_stream_class_get_id(notit->meta.stream_class));
1052 notit->state = STATE_AFTER_STREAM_PACKET_CONTEXT;
1053 goto end;
1054 }
1055
1056 BT_LOGV("Decoding packet context field: "
1057 "notit-addr=%p, stream-class-addr=%p, "
1058 "stream-class-name=\"%s\", stream-class-id=%" PRId64 ", "
1059 "ft-addr=%p",
1060 notit, notit->meta.stream_class,
1061 bt_stream_class_get_name(notit->meta.stream_class),
1062 bt_stream_class_get_id(notit->meta.stream_class),
1063 packet_context_type);
1064 status = read_dscope_begin_state(notit, packet_context_type,
1065 STATE_AFTER_STREAM_PACKET_CONTEXT,
1066 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE,
1067 &notit->dscopes.stream_packet_context);
1068 if (status < 0) {
1069 BT_LOGW("Cannot decode packet context field: "
1070 "notit-addr=%p, stream-class-addr=%p, "
1071 "stream-class-name=\"%s\", "
1072 "stream-class-id=%" PRId64 ", ft-addr=%p",
1073 notit, notit->meta.stream_class,
1074 bt_stream_class_get_name(notit->meta.stream_class),
1075 bt_stream_class_get_id(notit->meta.stream_class),
1076 packet_context_type);
1077 }
1078
1079 end:
1080 return status;
1081 }
1082
1083 static
1084 enum bt_notif_iter_status read_packet_context_continue_state(
1085 struct bt_notif_iter *notit)
1086 {
1087 return read_dscope_continue_state(notit,
1088 STATE_AFTER_STREAM_PACKET_CONTEXT);
1089 }
1090
1091 static
1092 enum bt_notif_iter_status set_current_packet_content_sizes(
1093 struct bt_notif_iter *notit)
1094 {
1095 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1096 struct bt_field *packet_size_field = NULL;
1097 struct bt_field *content_size_field = NULL;
1098 uint64_t content_size = -1ULL, packet_size = -1ULL;
1099
1100 if (!notit->dscopes.stream_packet_context) {
1101 goto end;
1102 }
1103
1104 packet_size_field = bt_field_structure_get_field_by_name(
1105 notit->dscopes.stream_packet_context, "packet_size");
1106 content_size_field = bt_field_structure_get_field_by_name(
1107 notit->dscopes.stream_packet_context, "content_size");
1108 if (packet_size_field) {
1109 int ret = bt_field_integer_unsigned_get_value(
1110 packet_size_field, &packet_size);
1111
1112 BT_ASSERT(ret == 0);
1113 if (packet_size == 0) {
1114 BT_LOGW("Invalid packet size: packet context field indicates packet size is zero: "
1115 "notit-addr=%p, packet-context-field-addr=%p",
1116 notit, notit->dscopes.stream_packet_context);
1117 status = BT_NOTIF_ITER_STATUS_ERROR;
1118 goto end;
1119 } else if ((packet_size % 8) != 0) {
1120 BT_LOGW("Invalid packet size: packet context field indicates packet size is not a multiple of 8: "
1121 "notit-addr=%p, packet-context-field-addr=%p, "
1122 "packet-size=%" PRIu64,
1123 notit, notit->dscopes.stream_packet_context,
1124 packet_size);
1125 status = BT_NOTIF_ITER_STATUS_ERROR;
1126 goto end;
1127 }
1128 }
1129
1130 if (content_size_field) {
1131 int ret = bt_field_integer_unsigned_get_value(
1132 content_size_field, &content_size);
1133
1134 BT_ASSERT(ret == 0);
1135 } else {
1136 content_size = packet_size;
1137 }
1138
1139 if (content_size > packet_size) {
1140 BT_LOGW("Invalid packet or content size: packet context field indicates content size is greater than packet size: "
1141 "notit-addr=%p, packet-context-field-addr=%p, "
1142 "packet-size=%" PRIu64 ", content-size=%" PRIu64,
1143 notit, notit->dscopes.stream_packet_context,
1144 packet_size, content_size);
1145 status = BT_NOTIF_ITER_STATUS_ERROR;
1146 goto end;
1147 }
1148
1149 if (packet_size != -1ULL) {
1150 notit->cur_packet_size = packet_size;
1151 } else {
1152 /*
1153 * Use the content size as packet size indicator if the
1154 * packet size field is missing. This means there is no
1155 * padding in this stream.
1156 */
1157 notit->cur_packet_size = content_size;
1158 }
1159 notit->cur_content_size = content_size;
1160 BT_LOGV("Set current packet and content sizes: "
1161 "notit-addr=%p, packet-size=%" PRIu64 ", content-size=%" PRIu64,
1162 notit, packet_size, content_size);
1163 end:
1164 BT_PUT(packet_size_field);
1165 BT_PUT(content_size_field);
1166 return status;
1167 }
1168
1169 static
1170 enum bt_notif_iter_status after_packet_context_state(
1171 struct bt_notif_iter *notit)
1172 {
1173 enum bt_notif_iter_status status;
1174
1175 status = set_current_packet_content_sizes(notit);
1176 if (status == BT_NOTIF_ITER_STATUS_OK) {
1177 if (notit->stream_begin_emitted) {
1178 notit->state = STATE_EMIT_NOTIF_NEW_PACKET;
1179 } else {
1180 notit->state = STATE_EMIT_NOTIF_NEW_STREAM;
1181 }
1182 }
1183
1184 return status;
1185 }
1186
1187 static
1188 enum bt_notif_iter_status read_event_header_begin_state(
1189 struct bt_notif_iter *notit)
1190 {
1191 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1192 struct bt_field_type *event_header_type = NULL;
1193
1194 /* Reset the position of the last event header */
1195 notit->buf.last_eh_at = notit->buf.at;
1196
1197 /* Check if we have some content left */
1198 if (notit->cur_content_size >= 0) {
1199 if (packet_at(notit) == notit->cur_content_size) {
1200 /* No more events! */
1201 BT_LOGV("Reached end of packet: notit-addr=%p, "
1202 "cur=%zu", notit, packet_at(notit));
1203 notit->state = STATE_EMIT_NOTIF_END_OF_PACKET;
1204 goto end;
1205 } else if (packet_at(notit) > notit->cur_content_size) {
1206 /* That's not supposed to happen */
1207 BT_LOGV("Before decoding event header field: cursor is passed the packet's content: "
1208 "notit-addr=%p, content-size=%" PRId64 ", "
1209 "cur=%zu", notit, notit->cur_content_size,
1210 packet_at(notit));
1211 status = BT_NOTIF_ITER_STATUS_ERROR;
1212 goto end;
1213 }
1214 }
1215
1216 event_header_type = bt_stream_class_borrow_event_header_field_type(
1217 notit->meta.stream_class);
1218 if (!event_header_type) {
1219 notit->state = STATE_AFTER_STREAM_EVENT_HEADER;
1220 goto end;
1221 }
1222
1223 put_event_dscopes(notit);
1224 BT_LOGV("Decoding event header field: "
1225 "notit-addr=%p, stream-class-addr=%p, "
1226 "stream-class-name=\"%s\", stream-class-id=%" PRId64 ", "
1227 "ft-addr=%p",
1228 notit, notit->meta.stream_class,
1229 bt_stream_class_get_name(notit->meta.stream_class),
1230 bt_stream_class_get_id(notit->meta.stream_class),
1231 event_header_type);
1232 status = read_dscope_begin_state(notit, event_header_type,
1233 STATE_AFTER_STREAM_EVENT_HEADER,
1234 STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE,
1235 &notit->dscopes.stream_event_header);
1236 if (status < 0) {
1237 BT_LOGW("Cannot decode event header field: "
1238 "notit-addr=%p, stream-class-addr=%p, "
1239 "stream-class-name=\"%s\", "
1240 "stream-class-id=%" PRId64 ", ft-addr=%p",
1241 notit, notit->meta.stream_class,
1242 bt_stream_class_get_name(notit->meta.stream_class),
1243 bt_stream_class_get_id(notit->meta.stream_class),
1244 event_header_type);
1245 }
1246
1247 end:
1248 return status;
1249 }
1250
1251 static
1252 enum bt_notif_iter_status read_event_header_continue_state(
1253 struct bt_notif_iter *notit)
1254 {
1255 return read_dscope_continue_state(notit,
1256 STATE_AFTER_STREAM_EVENT_HEADER);
1257 }
1258
1259 static inline
1260 enum bt_notif_iter_status set_current_event_class(struct bt_notif_iter *notit)
1261 {
1262 /*
1263 * The assert() calls in this function are okay because it is
1264 * assumed here that all the metadata objects have been
1265 * validated for CTF correctness before decoding actual streams.
1266 */
1267
1268 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1269 struct bt_field_type *event_header_type;
1270 struct bt_field_type *id_field_type = NULL;
1271 struct bt_field_type *v_field_type = NULL;
1272 uint64_t event_id = -1ULL;
1273 int ret;
1274
1275 event_header_type = bt_stream_class_borrow_event_header_field_type(
1276 notit->meta.stream_class);
1277 if (!event_header_type) {
1278 /*
1279 * No event header, therefore no event class ID field,
1280 * therefore only one event class.
1281 */
1282 goto single_event_class;
1283 }
1284
1285 /* Is there any "id"/"v" field in the event header? */
1286 BT_ASSERT(is_struct_type(event_header_type));
1287 id_field_type = bt_field_type_structure_borrow_field_type_by_name(
1288 event_header_type, "id");
1289 v_field_type = bt_field_type_structure_borrow_field_type_by_name(
1290 event_header_type, "v");
1291 BT_ASSERT(notit->dscopes.stream_event_header);
1292 if (v_field_type) {
1293 /*
1294 * _ _____ _____
1295 * | | |_ _|_ _| __ __ _
1296 * | | | | | || '_ \ / _` |
1297 * | |___| | | || | | | (_| | S P E C I A L
1298 * |_____|_| |_||_| |_|\__, | C A S E â„¢
1299 * |___/
1300 */
1301 struct bt_field *v_field = NULL;
1302 struct bt_field *v_struct_field = NULL;
1303 struct bt_field *v_struct_id_field = NULL;
1304
1305 // TODO: optimalize!
1306 v_field = bt_field_structure_get_field_by_name(
1307 notit->dscopes.stream_event_header, "v");
1308 BT_ASSERT(v_field);
1309
1310 v_struct_field =
1311 bt_field_variant_get_current_field(v_field);
1312 if (!v_struct_field) {
1313 goto end_v_field_type;
1314 }
1315
1316 // TODO: optimalize!
1317 v_struct_id_field =
1318 bt_field_structure_get_field_by_name(v_struct_field, "id");
1319 if (!v_struct_id_field) {
1320 goto end_v_field_type;
1321 }
1322
1323 if (bt_field_is_integer(v_struct_id_field)) {
1324 ret = bt_field_integer_unsigned_get_value(
1325 v_struct_id_field, &event_id);
1326 if (ret) {
1327 BT_LOGV("Cannot get value of unsigned integer field (`id`): continuing: "
1328 "notit=%p, field-addr=%p",
1329 notit, v_struct_id_field);
1330 event_id = -1ULL;
1331 }
1332 }
1333
1334 end_v_field_type:
1335 BT_PUT(v_field);
1336 BT_PUT(v_struct_field);
1337 BT_PUT(v_struct_id_field);
1338 }
1339
1340 if (id_field_type && event_id == -1ULL) {
1341 /* Check "id" field */
1342 struct bt_field *id_field = NULL;
1343 int ret_get_value = 0;
1344
1345 // TODO: optimalize!
1346 id_field = bt_field_structure_get_field_by_name(
1347 notit->dscopes.stream_event_header, "id");
1348 if (!id_field) {
1349 goto check_event_id;
1350 }
1351
1352 if (bt_field_is_integer(id_field)) {
1353 ret_get_value = bt_field_integer_unsigned_get_value(
1354 id_field, &event_id);
1355 } else if (bt_field_is_enumeration(id_field)) {
1356 struct bt_field *container;
1357
1358 container = bt_field_enumeration_get_container(
1359 id_field);
1360 BT_ASSERT(container);
1361 ret_get_value = bt_field_integer_unsigned_get_value(
1362 container, &event_id);
1363 BT_PUT(container);
1364 }
1365
1366 BT_ASSERT(ret_get_value == 0);
1367 BT_PUT(id_field);
1368 }
1369
1370 check_event_id:
1371 if (event_id == -1ULL) {
1372 single_event_class:
1373 /* Event ID not found: single event? */
1374 BT_ASSERT(bt_stream_class_get_event_class_count(
1375 notit->meta.stream_class) == 1);
1376 event_id = 0;
1377 }
1378
1379 BT_LOGV("Found event class ID to use: notit-addr=%p, "
1380 "stream-class-addr=%p, stream-class-name=\"%s\", "
1381 "stream-class-id=%" PRId64 ", "
1382 "event-class-id=%" PRIu64,
1383 notit, notit->meta.stream_class,
1384 bt_stream_class_get_name(notit->meta.stream_class),
1385 bt_stream_class_get_id(notit->meta.stream_class),
1386 event_id);
1387 notit->meta.event_class = bt_stream_class_borrow_event_class_by_id(
1388 notit->meta.stream_class, event_id);
1389 if (!notit->meta.event_class) {
1390 BT_LOGW("No event class with ID of event class ID to use in stream class: "
1391 "notit-addr=%p, stream-class-addr=%p, "
1392 "stream-class-name=\"%s\", "
1393 "stream-class-id=%" PRId64 ", "
1394 "event-class-id=%" PRIu64,
1395 notit, notit->meta.stream_class,
1396 bt_stream_class_get_name(notit->meta.stream_class),
1397 bt_stream_class_get_id(notit->meta.stream_class),
1398 event_id);
1399 status = BT_NOTIF_ITER_STATUS_ERROR;
1400 goto end;
1401 }
1402
1403 BT_LOGV("Set current event class: "
1404 "notit-addr=%p, event-class-addr=%p, "
1405 "event-class-name=\"%s\", event-class-id=%" PRId64,
1406 notit, notit->meta.event_class,
1407 bt_event_class_get_name(notit->meta.event_class),
1408 bt_event_class_get_id(notit->meta.event_class));
1409
1410 end:
1411 return status;
1412 }
1413
1414 static
1415 enum bt_notif_iter_status after_event_header_state(
1416 struct bt_notif_iter *notit)
1417 {
1418 enum bt_notif_iter_status status;
1419
1420 status = set_current_event_class(notit);
1421 if (status != BT_NOTIF_ITER_STATUS_OK) {
1422 goto end;
1423 }
1424
1425 notit->state = STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN;
1426
1427 end:
1428 return status;
1429 }
1430
1431 static
1432 enum bt_notif_iter_status read_stream_event_context_begin_state(
1433 struct bt_notif_iter *notit)
1434 {
1435 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1436 struct bt_field_type *stream_event_context_type;
1437
1438 stream_event_context_type =
1439 bt_stream_class_borrow_event_context_field_type(
1440 notit->meta.stream_class);
1441 if (!stream_event_context_type) {
1442 notit->state = STATE_DSCOPE_EVENT_CONTEXT_BEGIN;
1443 goto end;
1444 }
1445
1446 BT_LOGV("Decoding stream event context field: "
1447 "notit-addr=%p, stream-class-addr=%p, "
1448 "stream-class-name=\"%s\", stream-class-id=%" PRId64 ", "
1449 "ft-addr=%p",
1450 notit, notit->meta.stream_class,
1451 bt_stream_class_get_name(notit->meta.stream_class),
1452 bt_stream_class_get_id(notit->meta.stream_class),
1453 stream_event_context_type);
1454 status = read_dscope_begin_state(notit, stream_event_context_type,
1455 STATE_DSCOPE_EVENT_CONTEXT_BEGIN,
1456 STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE,
1457 &notit->dscopes.stream_event_context);
1458 if (status < 0) {
1459 BT_LOGW("Cannot decode stream event context field: "
1460 "notit-addr=%p, stream-class-addr=%p, "
1461 "stream-class-name=\"%s\", "
1462 "stream-class-id=%" PRId64 ", ft-addr=%p",
1463 notit, notit->meta.stream_class,
1464 bt_stream_class_get_name(notit->meta.stream_class),
1465 bt_stream_class_get_id(notit->meta.stream_class),
1466 stream_event_context_type);
1467 }
1468
1469 end:
1470 return status;
1471 }
1472
1473 static
1474 enum bt_notif_iter_status read_stream_event_context_continue_state(
1475 struct bt_notif_iter *notit)
1476 {
1477 return read_dscope_continue_state(notit,
1478 STATE_DSCOPE_EVENT_CONTEXT_BEGIN);
1479 }
1480
1481 static
1482 enum bt_notif_iter_status read_event_context_begin_state(
1483 struct bt_notif_iter *notit)
1484 {
1485 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1486 struct bt_field_type *event_context_type;
1487
1488 event_context_type = bt_event_class_borrow_context_field_type(
1489 notit->meta.event_class);
1490 if (!event_context_type) {
1491 notit->state = STATE_DSCOPE_EVENT_PAYLOAD_BEGIN;
1492 goto end;
1493 }
1494
1495 BT_LOGV("Decoding event context field: "
1496 "notit-addr=%p, event-class-addr=%p, "
1497 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
1498 "ft-addr=%p",
1499 notit, notit->meta.event_class,
1500 bt_event_class_get_name(notit->meta.event_class),
1501 bt_event_class_get_id(notit->meta.event_class),
1502 event_context_type);
1503 status = read_dscope_begin_state(notit, event_context_type,
1504 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN,
1505 STATE_DSCOPE_EVENT_CONTEXT_CONTINUE,
1506 &notit->dscopes.event_context);
1507 if (status < 0) {
1508 BT_LOGW("Cannot decode event context field: "
1509 "notit-addr=%p, event-class-addr=%p, "
1510 "event-class-name=\"%s\", "
1511 "event-class-id=%" PRId64 ", ft-addr=%p",
1512 notit, notit->meta.event_class,
1513 bt_event_class_get_name(notit->meta.event_class),
1514 bt_event_class_get_id(notit->meta.event_class),
1515 event_context_type);
1516 }
1517
1518 end:
1519 return status;
1520 }
1521
1522 static
1523 enum bt_notif_iter_status read_event_context_continue_state(
1524 struct bt_notif_iter *notit)
1525 {
1526 return read_dscope_continue_state(notit,
1527 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN);
1528 }
1529
1530 static
1531 enum bt_notif_iter_status read_event_payload_begin_state(
1532 struct bt_notif_iter *notit)
1533 {
1534 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1535 struct bt_field_type *event_payload_type;
1536
1537 event_payload_type = bt_event_class_borrow_payload_field_type(
1538 notit->meta.event_class);
1539 if (!event_payload_type) {
1540 notit->state = STATE_EMIT_NOTIF_EVENT;
1541 goto end;
1542 }
1543
1544 BT_LOGV("Decoding event payload field: "
1545 "notit-addr=%p, event-class-addr=%p, "
1546 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
1547 "ft-addr=%p",
1548 notit, notit->meta.event_class,
1549 bt_event_class_get_name(notit->meta.event_class),
1550 bt_event_class_get_id(notit->meta.event_class),
1551 event_payload_type);
1552 status = read_dscope_begin_state(notit, event_payload_type,
1553 STATE_EMIT_NOTIF_EVENT,
1554 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE,
1555 &notit->dscopes.event_payload);
1556 if (status < 0) {
1557 BT_LOGW("Cannot decode event payload field: "
1558 "notit-addr=%p, event-class-addr=%p, "
1559 "event-class-name=\"%s\", "
1560 "event-class-id=%" PRId64 ", ft-addr=%p",
1561 notit, notit->meta.event_class,
1562 bt_event_class_get_name(notit->meta.event_class),
1563 bt_event_class_get_id(notit->meta.event_class),
1564 event_payload_type);
1565 }
1566
1567 end:
1568 return status;
1569 }
1570
1571 static
1572 enum bt_notif_iter_status read_event_payload_continue_state(
1573 struct bt_notif_iter *notit)
1574 {
1575 return read_dscope_continue_state(notit, STATE_EMIT_NOTIF_EVENT);
1576 }
1577
1578 static
1579 enum bt_notif_iter_status skip_packet_padding_state(
1580 struct bt_notif_iter *notit)
1581 {
1582 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1583 size_t bits_to_skip;
1584
1585 BT_ASSERT(notit->cur_packet_size > 0);
1586 bits_to_skip = notit->cur_packet_size - packet_at(notit);
1587 if (bits_to_skip == 0) {
1588 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1589 goto end;
1590 } else {
1591 size_t bits_to_consume;
1592
1593 BT_LOGV("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu",
1594 bits_to_skip, notit, bits_to_skip);
1595 status = buf_ensure_available_bits(notit);
1596 if (status != BT_NOTIF_ITER_STATUS_OK) {
1597 goto end;
1598 }
1599
1600 bits_to_consume = MIN(buf_available_bits(notit), bits_to_skip);
1601 BT_LOGV("Skipping %zu bits of padding: notit-addr=%p, size=%zu",
1602 bits_to_consume, notit, bits_to_consume);
1603 buf_consume_bits(notit, bits_to_consume);
1604 bits_to_skip = notit->cur_packet_size - packet_at(notit);
1605 if (bits_to_skip == 0) {
1606 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1607 goto end;
1608 }
1609 }
1610
1611 end:
1612 return status;
1613 }
1614
1615 static inline
1616 enum bt_notif_iter_status handle_state(struct bt_notif_iter *notit)
1617 {
1618 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
1619 const enum state state = notit->state;
1620
1621 BT_LOGV("Handling state: notit-addr=%p, state=%s",
1622 notit, state_string(state));
1623
1624 // TODO: optimalize!
1625 switch (state) {
1626 case STATE_INIT:
1627 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1628 break;
1629 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
1630 status = read_packet_header_begin_state(notit);
1631 break;
1632 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
1633 status = read_packet_header_continue_state(notit);
1634 break;
1635 case STATE_AFTER_TRACE_PACKET_HEADER:
1636 status = after_packet_header_state(notit);
1637 break;
1638 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN:
1639 status = read_packet_context_begin_state(notit);
1640 break;
1641 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE:
1642 status = read_packet_context_continue_state(notit);
1643 break;
1644 case STATE_AFTER_STREAM_PACKET_CONTEXT:
1645 status = after_packet_context_state(notit);
1646 break;
1647 case STATE_EMIT_NOTIF_NEW_STREAM:
1648 notit->state = STATE_EMIT_NOTIF_NEW_PACKET;
1649 break;
1650 case STATE_EMIT_NOTIF_NEW_PACKET:
1651 notit->state = STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN;
1652 break;
1653 case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN:
1654 status = read_event_header_begin_state(notit);
1655 break;
1656 case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE:
1657 status = read_event_header_continue_state(notit);
1658 break;
1659 case STATE_AFTER_STREAM_EVENT_HEADER:
1660 status = after_event_header_state(notit);
1661 break;
1662 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN:
1663 status = read_stream_event_context_begin_state(notit);
1664 break;
1665 case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE:
1666 status = read_stream_event_context_continue_state(notit);
1667 break;
1668 case STATE_DSCOPE_EVENT_CONTEXT_BEGIN:
1669 status = read_event_context_begin_state(notit);
1670 break;
1671 case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE:
1672 status = read_event_context_continue_state(notit);
1673 break;
1674 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN:
1675 status = read_event_payload_begin_state(notit);
1676 break;
1677 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE:
1678 status = read_event_payload_continue_state(notit);
1679 break;
1680 case STATE_EMIT_NOTIF_EVENT:
1681 notit->state = STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN;
1682 break;
1683 case STATE_SKIP_PACKET_PADDING:
1684 status = skip_packet_padding_state(notit);
1685 break;
1686 case STATE_EMIT_NOTIF_END_OF_PACKET:
1687 notit->state = STATE_SKIP_PACKET_PADDING;
1688 break;
1689 default:
1690 BT_LOGD("Unknown CTF plugin notification iterator state: "
1691 "notit-addr=%p, state=%d", notit, notit->state);
1692 abort();
1693 }
1694
1695 BT_LOGV("Handled state: notit-addr=%p, status=%s, "
1696 "prev-state=%s, cur-state=%s",
1697 notit, bt_notif_iter_status_string(status),
1698 state_string(state), state_string(notit->state));
1699 return status;
1700 }
1701
1702 /**
1703 * Resets the internal state of a CTF notification iterator.
1704 */
1705 BT_HIDDEN
1706 void bt_notif_iter_reset(struct bt_notif_iter *notit)
1707 {
1708 BT_ASSERT(notit);
1709 BT_LOGD("Resetting notification iterator: addr=%p", notit);
1710 stack_clear(notit->stack);
1711 notit->meta.stream_class = NULL;
1712 notit->meta.event_class = NULL;
1713 BT_PUT(notit->packet);
1714 BT_PUT(notit->stream);
1715 put_all_dscopes(notit);
1716 notit->buf.addr = NULL;
1717 notit->buf.sz = 0;
1718 notit->buf.at = 0;
1719 notit->buf.last_eh_at = SIZE_MAX;
1720 notit->buf.packet_offset = 0;
1721 notit->state = STATE_INIT;
1722 notit->cur_content_size = -1;
1723 notit->cur_packet_size = -1;
1724 notit->cur_packet_offset = -1;
1725 notit->stream_begin_emitted = false;
1726 }
1727
1728 static
1729 int bt_notif_iter_switch_packet(struct bt_notif_iter *notit)
1730 {
1731 int ret = 0;
1732
1733 /*
1734 * We don't put the stream class here because we need to make
1735 * sure that all the packets processed by the same notification
1736 * iterator refer to the same stream class (the first one).
1737 */
1738 BT_ASSERT(notit);
1739 if (notit->cur_packet_size != -1) {
1740 notit->cur_packet_offset += notit->cur_packet_size;
1741 }
1742 BT_LOGV("Switching packet: notit-addr=%p, cur=%zu, "
1743 "packet-offset=%" PRId64, notit, notit->buf.at,
1744 notit->cur_packet_offset);
1745 stack_clear(notit->stack);
1746 notit->meta.event_class = NULL;
1747 BT_PUT(notit->packet);
1748 BT_PUT(notit->cur_timestamp_end);
1749 put_all_dscopes(notit);
1750
1751 /*
1752 * Adjust current buffer so that addr points to the beginning of the new
1753 * packet.
1754 */
1755 if (notit->buf.addr) {
1756 size_t consumed_bytes = (size_t) (notit->buf.at / CHAR_BIT);
1757
1758 /* Packets are assumed to start on a byte frontier. */
1759 if (notit->buf.at % CHAR_BIT) {
1760 BT_LOGW("Cannot switch packet: current position is not a multiple of 8: "
1761 "notit-addr=%p, cur=%zu", notit, notit->buf.at);
1762 ret = -1;
1763 goto end;
1764 }
1765
1766 notit->buf.addr += consumed_bytes;
1767 notit->buf.sz -= consumed_bytes;
1768 notit->buf.at = 0;
1769 notit->buf.packet_offset = 0;
1770 BT_LOGV("Adjusted buffer: addr=%p, size=%zu",
1771 notit->buf.addr, notit->buf.sz);
1772 }
1773
1774 notit->cur_content_size = -1;
1775 notit->cur_packet_size = -1;
1776 notit->cur_sc_field_path_cache = NULL;
1777
1778 end:
1779 return ret;
1780 }
1781
1782 static
1783 struct bt_field *get_next_field(struct bt_notif_iter *notit)
1784 {
1785 struct bt_field *next_field = NULL;
1786 struct bt_field *base_field;
1787 struct bt_field_type *base_type;
1788 size_t index;
1789
1790 BT_ASSERT(!stack_empty(notit->stack));
1791 index = stack_top(notit->stack)->index;
1792 base_field = stack_top(notit->stack)->base;
1793 BT_ASSERT(base_field);
1794 base_type = bt_field_borrow_type(base_field);
1795 BT_ASSERT(base_type);
1796
1797 switch (bt_field_type_get_type_id(base_type)) {
1798 case BT_FIELD_TYPE_ID_STRUCT:
1799 {
1800 next_field = bt_field_structure_get_field_by_index(
1801 base_field, index);
1802 break;
1803 }
1804 case BT_FIELD_TYPE_ID_ARRAY:
1805 next_field = bt_field_array_get_field(base_field, index);
1806 break;
1807 case BT_FIELD_TYPE_ID_SEQUENCE:
1808 next_field = bt_field_sequence_get_field(base_field, index);
1809 break;
1810 case BT_FIELD_TYPE_ID_VARIANT:
1811 next_field = bt_field_variant_get_current_field(base_field);
1812 break;
1813 default:
1814 BT_LOGF("Unknown base field type ID: "
1815 "notit-addr=%p, ft-addr=%p, ft-id=%s",
1816 notit, base_type,
1817 bt_common_field_type_id_string(
1818 bt_field_type_get_type_id(base_type)));
1819 abort();
1820 }
1821
1822 return next_field;
1823 }
1824
1825 static
1826 void update_clock_state(uint64_t *state,
1827 struct bt_field *value_field)
1828 {
1829 struct bt_field_type *value_type = NULL;
1830 uint64_t requested_new_value;
1831 uint64_t requested_new_value_mask;
1832 uint64_t cur_value_masked;
1833 int requested_new_value_size;
1834 int ret;
1835
1836 value_type = bt_field_borrow_type(value_field);
1837 BT_ASSERT(value_type);
1838 BT_ASSERT(bt_field_type_is_integer(value_type));
1839 requested_new_value_size =
1840 bt_field_type_integer_get_size(value_type);
1841 BT_ASSERT(requested_new_value_size > 0);
1842 ret = bt_field_integer_unsigned_get_value(value_field,
1843 &requested_new_value);
1844 BT_ASSERT(!ret);
1845
1846 /*
1847 * Special case for a 64-bit new value, which is the limit
1848 * of a clock value as of this version: overwrite the
1849 * current value directly.
1850 */
1851 if (requested_new_value_size == 64) {
1852 *state = requested_new_value;
1853 goto end;
1854 }
1855
1856 requested_new_value_mask = (1ULL << requested_new_value_size) - 1;
1857 cur_value_masked = *state & requested_new_value_mask;
1858
1859 if (requested_new_value < cur_value_masked) {
1860 /*
1861 * It looks like a wrap happened on the number of bits
1862 * of the requested new value. Assume that the clock
1863 * value wrapped only one time.
1864 */
1865 *state += requested_new_value_mask + 1;
1866 }
1867
1868 /* Clear the low bits of the current clock value. */
1869 *state &= ~requested_new_value_mask;
1870
1871 /* Set the low bits of the current clock value. */
1872 *state |= requested_new_value;
1873
1874 end:
1875 BT_LOGV("Updated clock's value from integer field's value: "
1876 "value=%" PRIu64, *state);
1877 }
1878
1879 static
1880 enum bt_btr_status update_clock(struct bt_notif_iter *notit,
1881 struct bt_field *int_field)
1882 {
1883 gboolean clock_class_found;
1884 uint64_t *clock_state = NULL;
1885 struct bt_field_type *int_field_type = NULL;
1886 enum bt_btr_status ret = BT_BTR_STATUS_OK;
1887 struct bt_clock_class *clock_class = NULL;
1888
1889 int_field_type = bt_field_borrow_type(int_field);
1890 BT_ASSERT(int_field_type);
1891 clock_class = bt_field_type_integer_borrow_mapped_clock_class(
1892 int_field_type);
1893 if (likely(!clock_class)) {
1894 goto end;
1895 }
1896
1897 clock_class_found = g_hash_table_lookup_extended(notit->clock_states,
1898 clock_class, NULL, (gpointer) &clock_state);
1899 if (!clock_class_found) {
1900 clock_state = g_new0(uint64_t, 1);
1901 if (!clock_state) {
1902 BT_LOGE_STR("Failed to allocate a uint64_t.");
1903 ret = BT_BTR_STATUS_ENOMEM;
1904 goto end;
1905 }
1906
1907 g_hash_table_insert(notit->clock_states, clock_class,
1908 clock_state);
1909 }
1910
1911 /* Update the clock's state. */
1912 BT_LOGV("Updating notification iterator's clock's value from integer field: "
1913 "notit-addr=%p, clock-class-addr=%p, "
1914 "clock-class-name=\"%s\", value=%" PRIu64,
1915 notit, clock_class,
1916 bt_clock_class_get_name(clock_class), *clock_state);
1917 update_clock_state(clock_state, int_field);
1918
1919 end:
1920 return ret;
1921 }
1922
1923 static
1924 enum bt_btr_status btr_unsigned_int_common(uint64_t value,
1925 struct bt_field_type *type, void *data,
1926 struct bt_field **out_int_field)
1927 {
1928 enum bt_btr_status status = BT_BTR_STATUS_OK;
1929 struct bt_field *field = NULL;
1930 struct bt_field *int_field = NULL;
1931 struct bt_notif_iter *notit = data;
1932 int ret;
1933
1934 BT_LOGV("Common unsigned integer function called from BTR: "
1935 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
1936 "ft-id=%s, value=%" PRIu64,
1937 notit, notit->btr, type,
1938 bt_common_field_type_id_string(
1939 bt_field_type_get_type_id(type)),
1940 value);
1941
1942 /* Create next field */
1943 field = get_next_field(notit);
1944 if (!field) {
1945 BT_LOGW("Cannot get next field: notit-addr=%p", notit);
1946 status = BT_BTR_STATUS_ERROR;
1947 goto end_no_put;
1948 }
1949
1950 switch(bt_field_type_get_type_id(type)) {
1951 case BT_FIELD_TYPE_ID_INTEGER:
1952 /* Integer field is created field */
1953 BT_MOVE(int_field, field);
1954 break;
1955 case BT_FIELD_TYPE_ID_ENUM:
1956 int_field = bt_field_enumeration_get_container(field);
1957 BT_ASSERT(int_field);
1958 type = bt_field_borrow_type(int_field);
1959 BT_ASSERT(type);
1960 break;
1961 default:
1962 BT_LOGF("Unexpected field type ID: "
1963 "notit-addr=%p, ft-addr=%p, ft-id=%s",
1964 notit, type,
1965 bt_common_field_type_id_string(
1966 bt_field_type_get_type_id(type)));
1967 abort();
1968 }
1969
1970 BT_ASSERT(int_field);
1971 ret = bt_field_integer_unsigned_set_value(int_field, value);
1972 BT_ASSERT(ret == 0);
1973 stack_top(notit->stack)->index++;
1974 *out_int_field = int_field;
1975 BT_PUT(field);
1976
1977 end_no_put:
1978 return status;
1979 }
1980
1981 static
1982 enum bt_btr_status btr_timestamp_end_cb(void *value,
1983 struct bt_field_type *type, void *data)
1984 {
1985 enum bt_btr_status status;
1986 struct bt_field *field = NULL;
1987 struct bt_notif_iter *notit = data;
1988
1989 BT_LOGV("`timestamp_end` unsigned integer function called from BTR: "
1990 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
1991 "ft-id=%s",
1992 notit, notit->btr, type,
1993 bt_common_field_type_id_string(
1994 bt_field_type_get_type_id(type)));
1995 status = btr_unsigned_int_common(*((uint64_t *) value), type, data,
1996 &field);
1997
1998 /* Set as the current packet's timestamp_end field. */
1999 BT_MOVE(notit->cur_timestamp_end, field);
2000 return status;
2001 }
2002
2003 static
2004 enum bt_btr_status btr_unsigned_int_cb(uint64_t value,
2005 struct bt_field_type *type, void *data)
2006 {
2007 struct bt_notif_iter *notit = data;
2008 enum bt_btr_status status = BT_BTR_STATUS_OK;
2009 struct bt_field *field = NULL;
2010 struct field_cb_override *override;
2011
2012 BT_LOGV("Unsigned integer function called from BTR: "
2013 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2014 "ft-id=%s, value=%" PRIu64,
2015 notit, notit->btr, type,
2016 bt_common_field_type_id_string(
2017 bt_field_type_get_type_id(type)),
2018 value);
2019 override = g_hash_table_lookup(notit->field_overrides, type);
2020 if (unlikely(override)) {
2021 /* Override function logs errors */
2022 status = override->func(&value, type, override->data);
2023 goto end;
2024 }
2025
2026 status = btr_unsigned_int_common(value, type, data, &field);
2027 if (status != BT_BTR_STATUS_OK) {
2028 /* btr_unsigned_int_common() logs errors */
2029 goto end;
2030 }
2031
2032 status = update_clock(notit, field);
2033 BT_PUT(field);
2034 end:
2035 return status;
2036 }
2037
2038 static
2039 enum bt_btr_status btr_signed_int_cb(int64_t value,
2040 struct bt_field_type *type, void *data)
2041 {
2042 enum bt_btr_status status = BT_BTR_STATUS_OK;
2043 struct bt_field *field = NULL;
2044 struct bt_field *int_field = NULL;
2045 struct bt_notif_iter *notit = data;
2046 int ret;
2047
2048 BT_LOGV("Signed integer function called from BTR: "
2049 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2050 "ft-id=%s, value=%" PRId64,
2051 notit, notit->btr, type,
2052 bt_common_field_type_id_string(
2053 bt_field_type_get_type_id(type)),
2054 value);
2055
2056 /* create next field */
2057 field = get_next_field(notit);
2058 if (!field) {
2059 BT_LOGW("Cannot get next field: notit-addr=%p", notit);
2060 status = BT_BTR_STATUS_ERROR;
2061 goto end_no_put;
2062 }
2063
2064 switch(bt_field_type_get_type_id(type)) {
2065 case BT_FIELD_TYPE_ID_INTEGER:
2066 /* Integer field is created field */
2067 BT_MOVE(int_field, field);
2068 break;
2069 case BT_FIELD_TYPE_ID_ENUM:
2070 int_field = bt_field_enumeration_get_container(field);
2071 BT_ASSERT(int_field);
2072 type = bt_field_borrow_type(int_field);
2073 BT_ASSERT(type);
2074 break;
2075 default:
2076 BT_LOGF("Unexpected field type ID: "
2077 "notit-addr=%p, ft-addr=%p, ft-id=%s",
2078 notit, type,
2079 bt_common_field_type_id_string(
2080 bt_field_type_get_type_id(type)));
2081 abort();
2082 }
2083
2084 BT_ASSERT(int_field);
2085 ret = bt_field_integer_signed_set_value(int_field, value);
2086 BT_ASSERT(!ret);
2087 stack_top(notit->stack)->index++;
2088 status = update_clock(notit, int_field);
2089 BT_PUT(field);
2090 BT_PUT(int_field);
2091
2092 end_no_put:
2093 return status;
2094 }
2095
2096 static
2097 enum bt_btr_status btr_floating_point_cb(double value,
2098 struct bt_field_type *type, void *data)
2099 {
2100 enum bt_btr_status status = BT_BTR_STATUS_OK;
2101 struct bt_field *field = NULL;
2102 struct bt_notif_iter *notit = data;
2103 int ret;
2104
2105 BT_LOGV("Floating point number function called from BTR: "
2106 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2107 "ft-id=%s, value=%f",
2108 notit, notit->btr, type,
2109 bt_common_field_type_id_string(
2110 bt_field_type_get_type_id(type)),
2111 value);
2112
2113 /* Create next field */
2114 field = get_next_field(notit);
2115 if (!field) {
2116 BT_LOGW("Cannot get next field: notit-addr=%p", notit);
2117 status = BT_BTR_STATUS_ERROR;
2118 goto end;
2119 }
2120
2121 ret = bt_field_floating_point_set_value(field, value);
2122 BT_ASSERT(!ret);
2123 stack_top(notit->stack)->index++;
2124
2125 end:
2126 BT_PUT(field);
2127 return status;
2128 }
2129
2130 static
2131 enum bt_btr_status btr_string_begin_cb(
2132 struct bt_field_type *type, void *data)
2133 {
2134 enum bt_btr_status status = BT_BTR_STATUS_OK;
2135 struct bt_field *field = NULL;
2136 struct bt_notif_iter *notit = data;
2137 int ret;
2138
2139 BT_LOGV("String (beginning) function called from BTR: "
2140 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2141 "ft-id=%s",
2142 notit, notit->btr, type,
2143 bt_common_field_type_id_string(
2144 bt_field_type_get_type_id(type)));
2145
2146 /* Create next field */
2147 field = get_next_field(notit);
2148 if (!field) {
2149 BT_LOGW("Cannot get next field: notit-addr=%p", notit);
2150 status = BT_BTR_STATUS_ERROR;
2151 goto end;
2152 }
2153
2154 /*
2155 * Push on stack. Not a compound type per se, but we know that only
2156 * btr_string_cb() may be called between this call and a subsequent
2157 * call to btr_string_end_cb().
2158 */
2159 ret = stack_push(notit->stack, field);
2160 if (ret) {
2161 BT_LOGE("Cannot push string field on stack: "
2162 "notit-addr=%p, field-addr=%p", notit, field);
2163 status = BT_BTR_STATUS_ERROR;
2164 goto end;
2165 }
2166
2167 /*
2168 * Initialize string field payload to an empty string since in the
2169 * case of a length 0 string the btr_string_cb won't be called and
2170 * we will end up with an unset string payload.
2171 */
2172 ret = bt_field_string_set_value(field, "");
2173 if (ret) {
2174 BT_LOGE("Cannot initialize string field's value to an empty string: "
2175 "notit-addr=%p, field-addr=%p, ret=%d",
2176 notit, field, ret);
2177 status = BT_BTR_STATUS_ERROR;
2178 goto end;
2179 }
2180
2181 end:
2182 BT_PUT(field);
2183
2184 return status;
2185 }
2186
2187 static
2188 enum bt_btr_status btr_string_cb(const char *value,
2189 size_t len, struct bt_field_type *type, void *data)
2190 {
2191 enum bt_btr_status status = BT_BTR_STATUS_OK;
2192 struct bt_field *field = NULL;
2193 struct bt_notif_iter *notit = data;
2194 int ret;
2195
2196 BT_LOGV("String (substring) function called from BTR: "
2197 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2198 "ft-id=%s, string-length=%zu",
2199 notit, notit->btr, type,
2200 bt_common_field_type_id_string(
2201 bt_field_type_get_type_id(type)),
2202 len);
2203
2204 /* Get string field */
2205 field = stack_top(notit->stack)->base;
2206 BT_ASSERT(field);
2207
2208 /* Append current string */
2209 ret = bt_field_string_append_len(field, value, len);
2210 if (ret) {
2211 BT_LOGE("Cannot append substring to string field's value: "
2212 "notit-addr=%p, field-addr=%p, string-length=%zu, "
2213 "ret=%d", notit, field, len, ret);
2214 status = BT_BTR_STATUS_ERROR;
2215 goto end;
2216 }
2217
2218 end:
2219 return status;
2220 }
2221
2222 static
2223 enum bt_btr_status btr_string_end_cb(
2224 struct bt_field_type *type, void *data)
2225 {
2226 struct bt_notif_iter *notit = data;
2227
2228 BT_LOGV("String (end) function called from BTR: "
2229 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2230 "ft-id=%s",
2231 notit, notit->btr, type,
2232 bt_common_field_type_id_string(
2233 bt_field_type_get_type_id(type)));
2234
2235 /* Pop string field */
2236 stack_pop(notit->stack);
2237
2238 /* Go to next field */
2239 stack_top(notit->stack)->index++;
2240 return BT_BTR_STATUS_OK;
2241 }
2242
2243 enum bt_btr_status btr_compound_begin_cb(
2244 struct bt_field_type *type, void *data)
2245 {
2246 enum bt_btr_status status = BT_BTR_STATUS_OK;
2247 struct bt_notif_iter *notit = data;
2248 struct bt_field *field;
2249 int ret;
2250
2251 BT_LOGV("Compound (beginning) function called from BTR: "
2252 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2253 "ft-id=%s",
2254 notit, notit->btr, type,
2255 bt_common_field_type_id_string(
2256 bt_field_type_get_type_id(type)));
2257
2258 /* Create field */
2259 if (stack_empty(notit->stack)) {
2260 /* Root: create dynamic scope field */
2261 *notit->cur_dscope_field = bt_field_create(type);
2262 field = *notit->cur_dscope_field;
2263
2264 /*
2265 * Field will be put at the end of this function
2266 * (stack_push() will take one reference, but this
2267 * reference is lost upon the equivalent stack_pop()
2268 * later), so also get it for our context to own it.
2269 */
2270 bt_get(*notit->cur_dscope_field);
2271
2272 if (!field) {
2273 BT_LOGE("Cannot create compound field: "
2274 "notit-addr=%p, ft-addr=%p, ft-id=%s",
2275 notit, type,
2276 bt_common_field_type_id_string(
2277 bt_field_type_get_type_id(type)));
2278 status = BT_BTR_STATUS_ERROR;
2279 goto end;
2280 }
2281 } else {
2282 field = get_next_field(notit);
2283 if (!field) {
2284 BT_LOGW("Cannot get next field: notit-addr=%p", notit);
2285 status = BT_BTR_STATUS_ERROR;
2286 goto end;
2287 }
2288 }
2289
2290 /* Push field */
2291 BT_ASSERT(field);
2292 ret = stack_push(notit->stack, field);
2293 if (ret) {
2294 BT_LOGE("Cannot push compound field onto the stack: "
2295 "notit-addr=%p, ft-addr=%p, ft-id=%s, ret=%d",
2296 notit, type,
2297 bt_common_field_type_id_string(
2298 bt_field_type_get_type_id(type)),
2299 ret);
2300 status = BT_BTR_STATUS_ERROR;
2301 goto end;
2302 }
2303
2304 end:
2305 BT_PUT(field);
2306 return status;
2307 }
2308
2309 enum bt_btr_status btr_compound_end_cb(
2310 struct bt_field_type *type, void *data)
2311 {
2312 struct bt_notif_iter *notit = data;
2313
2314 BT_LOGV("Compound (end) function called from BTR: "
2315 "notit-addr=%p, btr-addr=%p, ft-addr=%p, "
2316 "ft-id=%s",
2317 notit, notit->btr, type,
2318 bt_common_field_type_id_string(
2319 bt_field_type_get_type_id(type)));
2320 BT_ASSERT(!stack_empty(notit->stack));
2321
2322 /* Pop stack */
2323 stack_pop(notit->stack);
2324
2325 /* If the stack is not empty, increment the base's index */
2326 if (!stack_empty(notit->stack)) {
2327 stack_top(notit->stack)->index++;
2328 }
2329
2330 return BT_BTR_STATUS_OK;
2331 }
2332
2333 static
2334 struct bt_field *resolve_field(struct bt_notif_iter *notit,
2335 struct bt_field_path *path)
2336 {
2337 struct bt_field *field = NULL;
2338 unsigned int i;
2339
2340 if (BT_LOG_ON_VERBOSE) {
2341 GString *gstr = bt_field_path_string(path);
2342
2343 BT_LOGV("Resolving field path: notit-addr=%p, field-path=\"%s\"",
2344 notit, gstr ? gstr->str : NULL);
2345
2346 if (gstr) {
2347 g_string_free(gstr, TRUE);
2348 }
2349 }
2350
2351 switch (bt_field_path_get_root_scope(path)) {
2352 case BT_SCOPE_TRACE_PACKET_HEADER:
2353 field = notit->dscopes.trace_packet_header;
2354 break;
2355 case BT_SCOPE_STREAM_PACKET_CONTEXT:
2356 field = notit->dscopes.stream_packet_context;
2357 break;
2358 case BT_SCOPE_STREAM_EVENT_HEADER:
2359 field = notit->dscopes.stream_event_header;
2360 break;
2361 case BT_SCOPE_STREAM_EVENT_CONTEXT:
2362 field = notit->dscopes.stream_event_context;
2363 break;
2364 case BT_SCOPE_EVENT_CONTEXT:
2365 field = notit->dscopes.event_context;
2366 break;
2367 case BT_SCOPE_EVENT_FIELDS:
2368 field = notit->dscopes.event_payload;
2369 break;
2370 default:
2371 BT_LOGF("Cannot resolve field path: unknown scope: "
2372 "notit-addr=%p, root-scope=%s",
2373 notit, bt_common_scope_string(
2374 bt_field_path_get_root_scope(path)));
2375 abort();
2376 }
2377
2378 if (!field) {
2379 BT_LOGW("Cannot resolve field path: root field not found: "
2380 "notit-addr=%p, root-scope=%s",
2381 notit, bt_common_scope_string(
2382 bt_field_path_get_root_scope(path)));
2383 goto end;
2384 }
2385
2386 bt_get(field);
2387
2388 for (i = 0; i < bt_field_path_get_index_count(path); ++i) {
2389 struct bt_field *next_field = NULL;
2390 struct bt_field_type *field_type;
2391 int index = bt_field_path_get_index(path, i);
2392
2393 field_type = bt_field_borrow_type(field);
2394 BT_ASSERT(field_type);
2395
2396 if (is_struct_type(field_type)) {
2397 next_field = bt_field_structure_get_field_by_index(
2398 field, index);
2399 } else if (is_variant_type(field_type)) {
2400 next_field =
2401 bt_field_variant_get_current_field(field);
2402 }
2403
2404 BT_PUT(field);
2405
2406 if (!next_field) {
2407 BT_LOGW("Cannot find next field: "
2408 "notit-addr=%p, ft-addr=%p, ft-id=%s, index=%d",
2409 notit, field_type,
2410 bt_common_field_type_id_string(
2411 bt_field_type_get_type_id(field_type)),
2412 index);
2413 goto end;
2414 }
2415
2416 /* Move next field -> field */
2417 BT_MOVE(field, next_field);
2418 }
2419
2420 end:
2421 return field;
2422 }
2423
2424 static
2425 int64_t btr_get_sequence_length_cb(struct bt_field_type *type, void *data)
2426 {
2427 int64_t ret = -1;
2428 int iret;
2429 struct bt_field *seq_field;
2430 struct bt_field_path *field_path;
2431 struct bt_notif_iter *notit = data;
2432 struct bt_field *length_field = NULL;
2433 uint64_t length;
2434
2435 field_path = bt_field_type_sequence_borrow_length_field_path(type);
2436 BT_ASSERT(field_path);
2437 length_field = resolve_field(notit, field_path);
2438 if (!length_field) {
2439 BT_LOGW("Cannot resolve sequence field type's length field path: "
2440 "notit-addr=%p, ft-addr=%p",
2441 notit, type);
2442 goto end;
2443 }
2444
2445 iret = bt_field_integer_unsigned_get_value(length_field, &length);
2446 if (iret) {
2447 BT_LOGE("Cannot get value of sequence length field: "
2448 "notit-addr=%p, field-addr=%p",
2449 notit, length_field);
2450 goto end;
2451 }
2452
2453 seq_field = stack_top(notit->stack)->base;
2454 iret = bt_field_sequence_set_length(seq_field, length_field);
2455 if (iret) {
2456 BT_LOGE("Cannot set sequence field's length field: "
2457 "notit-addr=%p, seq-field-addr=%p, "
2458 "length-field-addr=%p, ",
2459 notit, seq_field, length_field);
2460 goto end;
2461 }
2462
2463 ret = (int64_t) length;
2464
2465 end:
2466 BT_PUT(length_field);
2467 return ret;
2468 }
2469
2470 static
2471 struct bt_field_type *btr_borrow_variant_field_type_cb(
2472 struct bt_field_type *type, void *data)
2473 {
2474 struct bt_field_path *path;
2475 struct bt_notif_iter *notit = data;
2476 struct bt_field *var_field;
2477 struct bt_field *tag_field = NULL;
2478 struct bt_field *selected_field = NULL;
2479 struct bt_field_type *selected_field_type = NULL;
2480
2481 path = bt_field_type_variant_borrow_tag_field_path(type);
2482 BT_ASSERT(path);
2483 tag_field = resolve_field(notit, path);
2484 if (!tag_field) {
2485 BT_LOGW("Cannot resolve variant field type's tag field path: "
2486 "notit-addr=%p, ft-addr=%p",
2487 notit, type);
2488 goto end;
2489 }
2490
2491 /*
2492 * We found the enumeration tag field instance which should be
2493 * able to select a current field for this variant. This
2494 * callback function we're in is called _after_
2495 * compound_begin(), so the current stack top's base field is
2496 * the variant field in question. We get the selected field here
2497 * thanks to this tag field (thus creating the selected field),
2498 * which will also provide us with its type. Then, this field
2499 * will remain the current selected one until the next callback
2500 * function call which is used to fill the current selected
2501 * field.
2502 */
2503 var_field = stack_top(notit->stack)->base;
2504 selected_field = bt_field_variant_get_field(var_field, tag_field);
2505 if (!selected_field) {
2506 BT_LOGW("Cannot get variant field's selection using tag field: "
2507 "notit-addr=%p, var-field-addr=%p, tag-field-addr=%p",
2508 notit, var_field, tag_field);
2509 goto end;
2510 }
2511
2512 selected_field_type = bt_field_borrow_type(selected_field);
2513
2514 end:
2515 BT_PUT(tag_field);
2516 BT_PUT(selected_field);
2517 return selected_field_type;
2518 }
2519
2520 static
2521 int set_event_clocks(struct bt_event *event,
2522 struct bt_notif_iter *notit)
2523 {
2524 int ret;
2525 GHashTableIter iter;
2526 struct bt_clock_class *clock_class;
2527 uint64_t *clock_state;
2528
2529 g_hash_table_iter_init(&iter, notit->clock_states);
2530
2531 while (g_hash_table_iter_next(&iter, (gpointer) &clock_class,
2532 (gpointer) &clock_state)) {
2533 struct bt_clock_value *clock_value;
2534
2535 clock_value = bt_clock_value_create(clock_class,
2536 *clock_state);
2537 if (!clock_value) {
2538 BT_LOGE("Cannot create clock value from clock class: "
2539 "notit-addr=%p, clock-class-addr=%p, "
2540 "clock-class-name=\"%s\"",
2541 notit, clock_class,
2542 bt_clock_class_get_name(clock_class));
2543 ret = -1;
2544 goto end;
2545 }
2546 ret = bt_event_set_clock_value(event, clock_value);
2547 bt_put(clock_value);
2548 if (ret) {
2549 struct bt_event_class *event_class =
2550 bt_event_borrow_class(event);
2551
2552 BT_ASSERT(event_class);
2553 BT_LOGE("Cannot set event's clock value: "
2554 "notit-addr=%p, event-addr=%p, "
2555 "event-class-name=\"%s\", "
2556 "event-class-id=%" PRId64 ", "
2557 "clock-class-addr=%p, "
2558 "clock-class-name=\"%s\", "
2559 "clock-value-addr=%p",
2560 notit, event,
2561 bt_event_class_get_name(event_class),
2562 bt_event_class_get_id(event_class),
2563 clock_class,
2564 bt_clock_class_get_name(clock_class),
2565 clock_value);
2566 goto end;
2567 }
2568 }
2569
2570 ret = 0;
2571 end:
2572 return ret;
2573 }
2574
2575 static
2576 struct bt_event *create_event(struct bt_notif_iter *notit)
2577 {
2578 int ret;
2579 struct bt_event *event;
2580
2581 BT_LOGV("Creating event for event notification: "
2582 "notit-addr=%p, event-class-addr=%p, "
2583 "event-class-name=\"%s\", "
2584 "event-class-id=%" PRId64,
2585 notit, notit->meta.event_class,
2586 bt_event_class_get_name(notit->meta.event_class),
2587 bt_event_class_get_id(notit->meta.event_class));
2588
2589 /* Create event object. */
2590 event = bt_event_create(notit->meta.event_class);
2591 if (!event) {
2592 BT_LOGE("Cannot create event: "
2593 "notit-addr=%p, event-class-addr=%p, "
2594 "event-class-name=\"%s\", "
2595 "event-class-id=%" PRId64,
2596 notit, notit->meta.event_class,
2597 bt_event_class_get_name(notit->meta.event_class),
2598 bt_event_class_get_id(notit->meta.event_class));
2599 goto error;
2600 }
2601
2602 /* Set header, stream event context, context, and payload fields. */
2603 ret = bt_event_set_header(event,
2604 notit->dscopes.stream_event_header);
2605 if (ret) {
2606 BT_LOGE("Cannot set event's header field: "
2607 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2608 "event-class-name=\"%s\", "
2609 "event-class-id=%" PRId64 ", field-addr=%p",
2610 notit, event, notit->meta.event_class,
2611 bt_event_class_get_name(notit->meta.event_class),
2612 bt_event_class_get_id(notit->meta.event_class),
2613 notit->dscopes.stream_event_header);
2614 goto error;
2615 }
2616
2617 ret = bt_event_set_stream_event_context(event,
2618 notit->dscopes.stream_event_context);
2619 if (ret) {
2620 BT_LOGE("Cannot set event's context field: "
2621 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2622 "event-class-name=\"%s\", "
2623 "event-class-id=%" PRId64 ", field-addr=%p",
2624 notit, event, notit->meta.event_class,
2625 bt_event_class_get_name(notit->meta.event_class),
2626 bt_event_class_get_id(notit->meta.event_class),
2627 notit->dscopes.stream_event_context);
2628 goto error;
2629 }
2630
2631 ret = bt_event_set_context(event,
2632 notit->dscopes.event_context);
2633 if (ret) {
2634 BT_LOGE("Cannot set event's stream event context field: "
2635 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2636 "event-class-name=\"%s\", "
2637 "event-class-id=%" PRId64 ", field-addr=%p",
2638 notit, event, notit->meta.event_class,
2639 bt_event_class_get_name(notit->meta.event_class),
2640 bt_event_class_get_id(notit->meta.event_class),
2641 notit->dscopes.event_context);
2642 goto error;
2643 }
2644
2645 ret = bt_event_set_payload(event,
2646 notit->dscopes.event_payload);
2647 if (ret) {
2648 BT_LOGE("Cannot set event's payload field: "
2649 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2650 "event-class-name=\"%s\", "
2651 "event-class-id=%" PRId64 ", field-addr=%p",
2652 notit, event, notit->meta.event_class,
2653 bt_event_class_get_name(notit->meta.event_class),
2654 bt_event_class_get_id(notit->meta.event_class),
2655 notit->dscopes.event_payload);
2656 goto error;
2657 }
2658
2659 ret = set_event_clocks(event, notit);
2660 if (ret) {
2661 BT_LOGE("Cannot set event's clock values: "
2662 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2663 "event-class-name=\"%s\", "
2664 "event-class-id=%" PRId64,
2665 notit, event, notit->meta.event_class,
2666 bt_event_class_get_name(notit->meta.event_class),
2667 bt_event_class_get_id(notit->meta.event_class));
2668 goto error;
2669 }
2670
2671 /* Associate with current packet. */
2672 BT_ASSERT(notit->packet);
2673 ret = bt_event_set_packet(event, notit->packet);
2674 if (ret) {
2675 BT_LOGE("Cannot set event's header field: "
2676 "notit-addr=%p, event-addr=%p, event-class-addr=%p, "
2677 "event-class-name=\"%s\", "
2678 "event-class-id=%" PRId64 ", packet-addr=%p",
2679 notit, event, notit->meta.event_class,
2680 bt_event_class_get_name(notit->meta.event_class),
2681 bt_event_class_get_id(notit->meta.event_class),
2682 notit->packet);
2683 goto error;
2684 }
2685
2686 goto end;
2687
2688 error:
2689 BT_PUT(event);
2690
2691 end:
2692 return event;
2693 }
2694
2695 static
2696 uint64_t get_cur_stream_instance_id(struct bt_notif_iter *notit)
2697 {
2698 struct bt_field *stream_instance_id_field = NULL;
2699 uint64_t stream_instance_id = -1ULL;
2700 int ret;
2701
2702 if (!notit->dscopes.trace_packet_header) {
2703 goto end;
2704 }
2705
2706 stream_instance_id_field = bt_field_structure_get_field_by_name(
2707 notit->dscopes.trace_packet_header, "stream_instance_id");
2708 if (!stream_instance_id_field) {
2709 goto end;
2710 }
2711
2712 ret = bt_field_integer_unsigned_get_value(stream_instance_id_field,
2713 &stream_instance_id);
2714 if (ret) {
2715 stream_instance_id = -1ULL;
2716 goto end;
2717 }
2718
2719 end:
2720 bt_put(stream_instance_id_field);
2721 return stream_instance_id;
2722 }
2723
2724 static
2725 int set_stream(struct bt_notif_iter *notit)
2726 {
2727 int ret = 0;
2728 struct bt_stream *stream = NULL;
2729
2730 BT_LOGV("Calling user function (get stream): notit-addr=%p, "
2731 "stream-class-addr=%p, stream-class-name=\"%s\", "
2732 "stream-class-id=%" PRId64,
2733 notit, notit->meta.stream_class,
2734 bt_stream_class_get_name(notit->meta.stream_class),
2735 bt_stream_class_get_id(notit->meta.stream_class));
2736 stream = bt_get(notit->medium.medops.get_stream(
2737 notit->meta.stream_class, get_cur_stream_instance_id(notit),
2738 notit->medium.data));
2739 BT_LOGV("User function returned: stream-addr=%p", stream);
2740 if (!stream) {
2741 BT_LOGW_STR("User function failed to return a stream object for the given stream class.");
2742 ret = -1;
2743 goto end;
2744 }
2745
2746 if (notit->stream && stream != notit->stream) {
2747 BT_LOGW("User function returned a different stream than the previous one for the same sequence of packets.");
2748 ret = -1;
2749 goto end;
2750 }
2751
2752 BT_MOVE(notit->stream, stream);
2753
2754 end:
2755 bt_put(stream);
2756 return ret;
2757 }
2758
2759 static
2760 void create_packet(struct bt_notif_iter *notit)
2761 {
2762 int ret;
2763 struct bt_packet *packet = NULL;
2764
2765 BT_LOGV("Creating packet for packet notification: "
2766 "notit-addr=%p", notit);
2767 BT_LOGV("Creating packet from stream: "
2768 "notit-addr=%p, stream-addr=%p, "
2769 "stream-class-addr=%p, "
2770 "stream-class-name=\"%s\", "
2771 "stream-class-id=%" PRId64,
2772 notit, notit->stream, notit->meta.stream_class,
2773 bt_stream_class_get_name(notit->meta.stream_class),
2774 bt_stream_class_get_id(notit->meta.stream_class));
2775
2776 /* Create packet */
2777 BT_ASSERT(notit->stream);
2778 packet = bt_packet_create(notit->stream);
2779 if (!packet) {
2780 BT_LOGE("Cannot create packet from stream: "
2781 "notit-addr=%p, stream-addr=%p, "
2782 "stream-class-addr=%p, "
2783 "stream-class-name=\"%s\", "
2784 "stream-class-id=%" PRId64,
2785 notit, notit->stream, notit->meta.stream_class,
2786 bt_stream_class_get_name(notit->meta.stream_class),
2787 bt_stream_class_get_id(notit->meta.stream_class));
2788 goto error;
2789 }
2790
2791 /* Set packet's context and header fields */
2792 if (notit->dscopes.trace_packet_header) {
2793 ret = bt_packet_set_header(packet,
2794 notit->dscopes.trace_packet_header);
2795 if (ret) {
2796 BT_LOGE("Cannot set packet's header field: "
2797 "notit-addr=%p, packet-addr=%p, "
2798 "stream-addr=%p, "
2799 "stream-class-addr=%p, "
2800 "stream-class-name=\"%s\", "
2801 "stream-class-id=%" PRId64 ", "
2802 "field-addr=%p",
2803 notit, packet, 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 notit->dscopes.trace_packet_header);
2807 goto error;
2808 }
2809 }
2810
2811 if (notit->dscopes.stream_packet_context) {
2812 ret = bt_packet_set_context(packet,
2813 notit->dscopes.stream_packet_context);
2814 if (ret) {
2815 BT_LOGE("Cannot set packet's context field: "
2816 "notit-addr=%p, packet-addr=%p, "
2817 "stream-addr=%p, "
2818 "stream-class-addr=%p, "
2819 "stream-class-name=\"%s\", "
2820 "stream-class-id=%" PRId64 ", "
2821 "field-addr=%p",
2822 notit, packet, notit->stream, notit->meta.stream_class,
2823 bt_stream_class_get_name(notit->meta.stream_class),
2824 bt_stream_class_get_id(notit->meta.stream_class),
2825 notit->dscopes.trace_packet_header);
2826 goto error;
2827 }
2828 }
2829
2830 goto end;
2831
2832 error:
2833 BT_PUT(packet);
2834
2835 end:
2836 BT_MOVE(notit->packet, packet);
2837 }
2838
2839 static
2840 void notify_new_stream(struct bt_notif_iter *notit,
2841 struct bt_notification **notification)
2842 {
2843 struct bt_notification *ret = NULL;
2844 int iret;
2845
2846 /* Ask the user for the stream */
2847 iret = set_stream(notit);
2848 if (iret) {
2849 goto end;
2850 }
2851
2852 BT_ASSERT(notit->stream);
2853 ret = bt_notification_stream_begin_create(notit->stream);
2854 if (!ret) {
2855 BT_LOGE("Cannot create stream beginning notification: "
2856 "notit-addr=%p, stream-addr=%p",
2857 notit, notit->stream);
2858 return;
2859 }
2860
2861 end:
2862 *notification = ret;
2863 }
2864
2865 static
2866 void notify_end_of_stream(struct bt_notif_iter *notit,
2867 struct bt_notification **notification)
2868 {
2869 struct bt_notification *ret;
2870
2871 if (!notit->stream) {
2872 BT_LOGE("Cannot create stream for stream notification: "
2873 "notit-addr=%p", notit);
2874 return;
2875 }
2876
2877 ret = bt_notification_stream_end_create(notit->stream);
2878 if (!ret) {
2879 BT_LOGE("Cannot create stream beginning notification: "
2880 "notit-addr=%p, stream-addr=%p",
2881 notit, notit->stream);
2882 return;
2883 }
2884 *notification = ret;
2885 }
2886
2887 static
2888 void notify_new_packet(struct bt_notif_iter *notit,
2889 struct bt_notification **notification)
2890 {
2891 struct bt_notification *ret;
2892
2893 /* Initialize the iterator's current packet */
2894 create_packet(notit);
2895 if (!notit->packet) {
2896 BT_LOGE("Cannot create packet for packet notification: "
2897 "notit-addr=%p", notit);
2898 return;
2899 }
2900
2901 ret = bt_notification_packet_begin_create(notit->packet);
2902 if (!ret) {
2903 BT_LOGE("Cannot create packet beginning notification: "
2904 "notit-addr=%p, packet-addr=%p",
2905 notit, notit->packet);
2906 return;
2907 }
2908 *notification = ret;
2909 }
2910
2911 static
2912 void notify_end_of_packet(struct bt_notif_iter *notit,
2913 struct bt_notification **notification)
2914 {
2915 struct bt_notification *ret;
2916
2917 if (!notit->packet) {
2918 return;
2919 }
2920
2921 ret = bt_notification_packet_end_create(notit->packet);
2922 if (!ret) {
2923 BT_LOGE("Cannot create packet end notification: "
2924 "notit-addr=%p, packet-addr=%p",
2925 notit, notit->packet);
2926 return;
2927 }
2928 BT_PUT(notit->packet);
2929 *notification = ret;
2930 }
2931
2932 static
2933 void notify_event(struct bt_notif_iter *notit,
2934 struct bt_clock_class_priority_map *cc_prio_map,
2935 struct bt_notification **notification)
2936 {
2937 struct bt_event *event = NULL;
2938 struct bt_notification *ret = NULL;
2939
2940 /* Make sure that the event contains at least one bit of data */
2941 if (notit->buf.at == notit->buf.last_eh_at) {
2942 BT_LOGE("Cannot create empty event with 0 bits of data: "
2943 "notit-addr=%p, packet-cur=%zu",
2944 notit, packet_at(notit));
2945 goto end;
2946 }
2947
2948 /* Create event */
2949 event = create_event(notit);
2950 if (!event) {
2951 BT_LOGE("Cannot create event for event notification: "
2952 "notit-addr=%p", notit);
2953 goto end;
2954 }
2955
2956 ret = bt_notification_event_create(event, cc_prio_map);
2957 if (!ret) {
2958 BT_LOGE("Cannot create event notification: "
2959 "notit-addr=%p, event-addr=%p, "
2960 "cc-prio-map-addr=%p",
2961 notit, event, cc_prio_map);
2962 goto end;
2963 }
2964 *notification = ret;
2965 end:
2966 BT_PUT(event);
2967 }
2968
2969 static
2970 void init_trace_field_path_cache(struct bt_trace *trace,
2971 struct trace_field_path_cache *trace_field_path_cache)
2972 {
2973 int stream_id = -1;
2974 int stream_instance_id = -1;
2975 int i, count;
2976 struct bt_field_type *packet_header = NULL;
2977
2978 packet_header = bt_trace_borrow_packet_header_field_type(trace);
2979 if (!packet_header) {
2980 goto end;
2981 }
2982
2983 if (!bt_field_type_is_structure(packet_header)) {
2984 goto end;
2985 }
2986
2987 count = bt_field_type_structure_get_field_count(packet_header);
2988 BT_ASSERT(count >= 0);
2989
2990 for (i = 0; (i < count && (stream_id == -1 || stream_instance_id == -1)); i++) {
2991 int ret;
2992 const char *field_name;
2993
2994 ret = bt_field_type_structure_borrow_field_by_index(
2995 packet_header, &field_name, NULL, i);
2996 if (ret) {
2997 BT_LOGE("Cannot get structure field's field: "
2998 "field-addr=%p, index=%d",
2999 packet_header, i);
3000 goto end;
3001 }
3002
3003 if (stream_id == -1 && !strcmp(field_name, "stream_id")) {
3004 stream_id = i;
3005 } else if (stream_instance_id == -1 &&
3006 !strcmp(field_name, "stream_instance_id")) {
3007 stream_instance_id = i;
3008 }
3009 }
3010
3011 end:
3012 trace_field_path_cache->stream_id = stream_id;
3013 trace_field_path_cache->stream_instance_id = stream_instance_id;
3014 }
3015
3016 BT_HIDDEN
3017 struct bt_notif_iter *bt_notif_iter_create(struct bt_trace *trace,
3018 size_t max_request_sz,
3019 struct bt_notif_iter_medium_ops medops, void *data)
3020 {
3021 struct bt_notif_iter *notit = NULL;
3022 struct bt_btr_cbs cbs = {
3023 .types = {
3024 .signed_int = btr_signed_int_cb,
3025 .unsigned_int = btr_unsigned_int_cb,
3026 .floating_point = btr_floating_point_cb,
3027 .string_begin = btr_string_begin_cb,
3028 .string = btr_string_cb,
3029 .string_end = btr_string_end_cb,
3030 .compound_begin = btr_compound_begin_cb,
3031 .compound_end = btr_compound_end_cb,
3032 },
3033 .query = {
3034 .get_sequence_length = btr_get_sequence_length_cb,
3035 .borrow_variant_field_type = btr_borrow_variant_field_type_cb,
3036 },
3037 };
3038
3039 BT_ASSERT(trace);
3040 BT_ASSERT(medops.request_bytes);
3041 BT_ASSERT(medops.get_stream);
3042 BT_LOGD("Creating CTF plugin notification iterator: "
3043 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
3044 "data=%p",
3045 trace, bt_trace_get_name(trace), max_request_sz, data);
3046 notit = g_new0(struct bt_notif_iter, 1);
3047 if (!notit) {
3048 BT_LOGE_STR("Failed to allocate one CTF plugin notification iterator.");
3049 goto end;
3050 }
3051 notit->clock_states = g_hash_table_new_full(g_direct_hash,
3052 g_direct_equal, NULL, g_free);
3053 if (!notit->clock_states) {
3054 BT_LOGE_STR("Failed to allocate a GHashTable.");
3055 goto error;
3056 }
3057 notit->meta.trace = trace;
3058 notit->medium.medops = medops;
3059 notit->medium.max_request_sz = max_request_sz;
3060 notit->medium.data = data;
3061 notit->stack = stack_new(notit);
3062 if (!notit->stack) {
3063 BT_LOGE_STR("Failed to create field stack.");
3064 goto error;
3065 }
3066
3067 notit->btr = bt_btr_create(cbs, notit);
3068 if (!notit->btr) {
3069 BT_LOGE_STR("Failed to create binary type reader (BTR).");
3070 goto error;
3071 }
3072
3073 bt_notif_iter_reset(notit);
3074 init_trace_field_path_cache(trace, &notit->trace_field_path_cache);
3075 notit->sc_field_path_caches = g_hash_table_new_full(g_direct_hash,
3076 g_direct_equal, NULL, g_free);
3077 if (!notit->sc_field_path_caches) {
3078 BT_LOGE_STR("Failed to allocate a GHashTable.");
3079 goto error;
3080 }
3081
3082 notit->field_overrides = g_hash_table_new_full(g_direct_hash,
3083 g_direct_equal, NULL, g_free);
3084 if (!notit->field_overrides) {
3085 BT_LOGE_STR("Failed to allocate a GHashTable.");
3086 goto error;
3087 }
3088
3089 BT_LOGD("Created CTF plugin notification iterator: "
3090 "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, "
3091 "data=%p, notit-addr=%p",
3092 trace, bt_trace_get_name(trace), max_request_sz, data,
3093 notit);
3094 notit->cur_packet_offset = 0;
3095
3096 end:
3097 return notit;
3098
3099 error:
3100 bt_notif_iter_destroy(notit);
3101 notit = NULL;
3102 goto end;
3103 }
3104
3105 void bt_notif_iter_destroy(struct bt_notif_iter *notit)
3106 {
3107 BT_PUT(notit->packet);
3108 BT_PUT(notit->stream);
3109 BT_PUT(notit->cur_timestamp_end);
3110 put_all_dscopes(notit);
3111
3112 BT_LOGD("Destroying CTF plugin notification iterator: addr=%p", notit);
3113
3114 if (notit->stack) {
3115 BT_LOGD_STR("Destroying field stack.");
3116 stack_destroy(notit->stack);
3117 }
3118
3119 if (notit->btr) {
3120 BT_LOGD("Destroying BTR: btr-addr=%p", notit->btr);
3121 bt_btr_destroy(notit->btr);
3122 }
3123
3124 if (notit->clock_states) {
3125 g_hash_table_destroy(notit->clock_states);
3126 }
3127
3128 if (notit->sc_field_path_caches) {
3129 g_hash_table_destroy(notit->sc_field_path_caches);
3130 }
3131
3132 if (notit->field_overrides) {
3133 g_hash_table_destroy(notit->field_overrides);
3134 }
3135
3136 g_free(notit);
3137 }
3138
3139 enum bt_notif_iter_status bt_notif_iter_get_next_notification(
3140 struct bt_notif_iter *notit,
3141 struct bt_clock_class_priority_map *cc_prio_map,
3142 struct bt_notification **notification)
3143 {
3144 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
3145
3146 BT_ASSERT(notit);
3147 BT_ASSERT(notification);
3148
3149 if (notit->state == STATE_DONE) {
3150 status = BT_NOTIF_ITER_STATUS_EOF;
3151 goto end;
3152 }
3153
3154 BT_LOGV("Getting next notification: notit-addr=%p, cc-prio-map-addr=%p",
3155 notit, cc_prio_map);
3156
3157 while (true) {
3158 status = handle_state(notit);
3159 if (status == BT_NOTIF_ITER_STATUS_AGAIN) {
3160 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_AGAIN.");
3161 goto end;
3162 }
3163 if (status != BT_NOTIF_ITER_STATUS_OK) {
3164 if (status == BT_NOTIF_ITER_STATUS_EOF) {
3165 enum state next_state = notit->state;
3166
3167 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_EOF.");
3168
3169 if (notit->packet) {
3170 notify_end_of_packet(notit, notification);
3171 } else {
3172 notify_end_of_stream(notit, notification);
3173 next_state = STATE_DONE;
3174 }
3175
3176 if (!*notification) {
3177 status = BT_NOTIF_ITER_STATUS_ERROR;
3178 goto end;
3179 }
3180
3181 status = BT_NOTIF_ITER_STATUS_OK;
3182 notit->state = next_state;
3183 goto end;
3184 } else {
3185 BT_LOGW("Cannot handle state: "
3186 "notit-addr=%p, state=%s",
3187 notit, state_string(notit->state));
3188 }
3189 goto end;
3190 }
3191
3192 switch (notit->state) {
3193 case STATE_EMIT_NOTIF_NEW_STREAM:
3194 /* notify_new_stream() logs errors */
3195 notify_new_stream(notit, notification);
3196 if (!*notification) {
3197 status = BT_NOTIF_ITER_STATUS_ERROR;
3198 }
3199 notit->stream_begin_emitted = true;
3200 goto end;
3201 case STATE_EMIT_NOTIF_NEW_PACKET:
3202 /* notify_new_packet() logs errors */
3203 notify_new_packet(notit, notification);
3204 if (!*notification) {
3205 status = BT_NOTIF_ITER_STATUS_ERROR;
3206 }
3207 goto end;
3208 case STATE_EMIT_NOTIF_EVENT:
3209 /* notify_event() logs errors */
3210 notify_event(notit, cc_prio_map, notification);
3211 if (!*notification) {
3212 status = BT_NOTIF_ITER_STATUS_ERROR;
3213 }
3214 goto end;
3215 case STATE_EMIT_NOTIF_END_OF_PACKET:
3216 /* Update clock with timestamp_end field. */
3217 if (notit->cur_timestamp_end) {
3218 enum bt_btr_status btr_status;
3219 struct bt_field_type *field_type =
3220 bt_field_borrow_type(
3221 notit->cur_timestamp_end);
3222
3223 BT_ASSERT(field_type);
3224 btr_status = update_clock(notit,
3225 notit->cur_timestamp_end);
3226 if (btr_status != BT_BTR_STATUS_OK) {
3227 BT_LOGW("Cannot update stream's clock value: "
3228 "notit-addr=%p", notit);
3229 status = BT_NOTIF_ITER_STATUS_ERROR;
3230 goto end;
3231 }
3232 }
3233
3234 /* notify_end_of_packet() logs errors */
3235 notify_end_of_packet(notit, notification);
3236 if (!*notification) {
3237 status = BT_NOTIF_ITER_STATUS_ERROR;
3238 }
3239 goto end;
3240 default:
3241 /* Non-emitting state: continue */
3242 break;
3243 }
3244 }
3245
3246 end:
3247 return status;
3248 }
3249
3250 BT_HIDDEN
3251 enum bt_notif_iter_status bt_notif_iter_get_packet_header_context_fields(
3252 struct bt_notif_iter *notit,
3253 struct bt_field **packet_header_field,
3254 struct bt_field **packet_context_field)
3255 {
3256 int ret;
3257 enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK;
3258
3259 BT_ASSERT(notit);
3260
3261 if (notit->state == STATE_EMIT_NOTIF_NEW_PACKET) {
3262 /* We're already there */
3263 goto set_fields;
3264 }
3265
3266 while (true) {
3267 status = handle_state(notit);
3268 if (status == BT_NOTIF_ITER_STATUS_AGAIN) {
3269 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_AGAIN.");
3270 goto end;
3271 }
3272 if (status != BT_NOTIF_ITER_STATUS_OK) {
3273 if (status == BT_NOTIF_ITER_STATUS_EOF) {
3274 BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_EOF.");
3275 } else {
3276 BT_LOGW("Cannot handle state: "
3277 "notit-addr=%p, state=%s",
3278 notit, state_string(notit->state));
3279 }
3280 goto end;
3281 }
3282
3283 switch (notit->state) {
3284 case STATE_EMIT_NOTIF_NEW_PACKET:
3285 /*
3286 * Packet header and context fields are
3287 * potentially decoded (or they don't exist).
3288 */
3289 goto set_fields;
3290 case STATE_INIT:
3291 case STATE_EMIT_NOTIF_NEW_STREAM:
3292 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
3293 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
3294 case STATE_AFTER_TRACE_PACKET_HEADER:
3295 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN:
3296 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE:
3297 case STATE_AFTER_STREAM_PACKET_CONTEXT:
3298 /* Non-emitting state: continue */
3299 break;
3300 default:
3301 /*
3302 * We should never get past the
3303 * STATE_EMIT_NOTIF_NEW_PACKET state.
3304 */
3305 BT_LOGF("Unexpected state: notit-addr=%p, state=%s",
3306 notit, state_string(notit->state));
3307 abort();
3308 }
3309 }
3310
3311 set_fields:
3312 if (packet_header_field) {
3313 *packet_header_field = bt_get(notit->dscopes.trace_packet_header);
3314 }
3315
3316 if (packet_context_field) {
3317 *packet_context_field = bt_get(notit->dscopes.stream_packet_context);
3318 }
3319
3320 ret = set_current_packet_content_sizes(notit);
3321 if (ret) {
3322 status = BT_NOTIF_ITER_STATUS_ERROR;
3323 goto end;
3324 }
3325 end:
3326 return status;
3327 }
3328
3329 BT_HIDDEN
3330 void bt_notif_iter_set_medops_data(struct bt_notif_iter *notit,
3331 void *medops_data)
3332 {
3333 BT_ASSERT(notit);
3334 notit->medium.data = medops_data;
3335 }
3336
3337 BT_HIDDEN
3338 enum bt_notif_iter_status bt_notif_iter_seek(
3339 struct bt_notif_iter *notit, off_t offset)
3340 {
3341 enum bt_notif_iter_status ret = BT_NOTIF_ITER_STATUS_OK;
3342 enum bt_notif_iter_medium_status medium_status;
3343
3344 BT_ASSERT(notit);
3345 if (offset < 0) {
3346 BT_LOGE("Cannot seek to negative offset: offset=%jd", offset);
3347 ret = BT_NOTIF_ITER_STATUS_INVAL;
3348 goto end;
3349 }
3350
3351 if (!notit->medium.medops.seek) {
3352 ret = BT_NOTIF_ITER_STATUS_UNSUPPORTED;
3353 BT_LOGD("Aborting seek as the iterator's underlying media does not implement seek support.");
3354 goto end;
3355 }
3356
3357 medium_status = notit->medium.medops.seek(
3358 BT_NOTIF_ITER_SEEK_WHENCE_SET, offset,
3359 notit->medium.data);
3360 if (medium_status != BT_NOTIF_ITER_MEDIUM_STATUS_OK) {
3361 if (medium_status == BT_NOTIF_ITER_MEDIUM_STATUS_EOF) {
3362 ret = BT_NOTIF_ITER_STATUS_EOF;
3363 } else {
3364 ret = BT_NOTIF_ITER_STATUS_ERROR;
3365 goto end;
3366 }
3367 }
3368
3369 bt_notif_iter_reset(notit);
3370 notit->cur_packet_offset = offset;
3371 end:
3372 return ret;
3373 }
3374
3375 BT_HIDDEN
3376 off_t bt_notif_iter_get_current_packet_offset(
3377 struct bt_notif_iter *notit)
3378 {
3379 BT_ASSERT(notit);
3380 return notit->cur_packet_offset;
3381 }
3382
3383 BT_HIDDEN
3384 off_t bt_notif_iter_get_current_packet_size(
3385 struct bt_notif_iter *notit)
3386 {
3387 BT_ASSERT(notit);
3388 return notit->cur_packet_size;
3389 }
This page took 0.179337 seconds and 5 git commands to generate.