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