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