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