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