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