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