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