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