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