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