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