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