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