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