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