lib: make packets and packet messages optional, disabled by default
[babeltrace.git] / src / plugins / ctf / common / msg-iter / msg-iter.c
CommitLineData
e98a2d6e 1/*
d6e69534 2 * Babeltrace - CTF message iterator
06a626b8 3 *
44c440bc
PP
4 * Copyright (c) 2015-2018 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2015-2018 Philippe Proulx <pproulx@efficios.com>
e98a2d6e
PP
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
eb7f6c4f 26#define BT_COMP_LOG_SELF_COMP (notit->self_comp)
ea14c7dd 27#define BT_LOG_OUTPUT_LEVEL (notit->log_level)
350ad6c1 28#define BT_LOG_TAG "PLUGIN/CTF/MSG-ITER"
eb7f6c4f 29#include "plugins/comp-logging.h"
fdf0e7a0 30
e98a2d6e
PP
31#include <stdint.h>
32#include <inttypes.h>
33#include <stdio.h>
34#include <stddef.h>
35#include <stdbool.h>
578e048b 36#include "common/assert.h"
e98a2d6e 37#include <string.h>
3fadfbc0 38#include <babeltrace2/babeltrace.h>
578e048b 39#include "common/common.h"
e98a2d6e 40#include <glib.h>
0fbb9a9f 41#include <stdlib.h>
e98a2d6e 42
d6e69534 43#include "msg-iter.h"
5cd6d0e5 44#include "../bfcr/bfcr.h"
e98a2d6e 45
d6e69534 46struct bt_msg_iter;
e98a2d6e
PP
47
48/* A visit stack entry */
49struct stack_entry {
50 /*
51 * Current base field, one of:
52 *
53 * * string
54 * * structure
55 * * array
56 * * sequence
57 * * variant
58 *
312c056a 59 * Field is borrowed.
e98a2d6e 60 */
b19ff26f 61 bt_field *base;
e98a2d6e 62
44c440bc 63 /* Index of next field to set */
e98a2d6e
PP
64 size_t index;
65};
66
ea14c7dd
PP
67struct bt_msg_iter;
68
e98a2d6e
PP
69/* Visit stack */
70struct stack {
ea14c7dd
PP
71 struct bt_msg_iter *notit;
72
afe86a06
PP
73 /* Entries (struct stack_entry) */
74 GArray *entries;
75
76 /* Number of active entries */
77 size_t size;
e98a2d6e
PP
78};
79
80/* State */
81enum state {
82 STATE_INIT,
83 STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN,
84 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE,
85 STATE_AFTER_TRACE_PACKET_HEADER,
86 STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN,
87 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE,
88 STATE_AFTER_STREAM_PACKET_CONTEXT,
495490c5 89 STATE_CHECK_EMIT_MSG_STREAM_BEGINNING,
fc917f65 90 STATE_EMIT_MSG_STREAM_BEGINNING,
495490c5
PP
91 STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS,
92 STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS,
93 STATE_EMIT_MSG_DISCARDED_EVENTS,
94 STATE_EMIT_MSG_DISCARDED_PACKETS,
fc917f65 95 STATE_EMIT_MSG_PACKET_BEGINNING,
44c440bc
PP
96 STATE_DSCOPE_EVENT_HEADER_BEGIN,
97 STATE_DSCOPE_EVENT_HEADER_CONTINUE,
98 STATE_AFTER_EVENT_HEADER,
99 STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN,
100 STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE,
101 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN,
102 STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE,
e98a2d6e
PP
103 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN,
104 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE,
d6e69534 105 STATE_EMIT_MSG_EVENT,
e98a2d6e 106 STATE_SKIP_PACKET_PADDING,
fc917f65
PP
107 STATE_EMIT_MSG_PACKET_END_MULTI,
108 STATE_EMIT_MSG_PACKET_END_SINGLE,
188edac1 109 STATE_CHECK_EMIT_MSG_STREAM_END,
fc917f65
PP
110 STATE_EMIT_MSG_STREAM_END,
111 STATE_DONE,
e98a2d6e
PP
112};
113
495490c5
PP
114struct end_of_packet_snapshots {
115 uint64_t discarded_events;
116 uint64_t packets;
117 uint64_t beginning_clock;
118 uint64_t end_clock;
119};
120
d6e69534
PP
121/* CTF message iterator */
122struct bt_msg_iter {
e98a2d6e
PP
123 /* Visit stack */
124 struct stack *stack;
125
d6e69534
PP
126 /* Current message iterator to create messages (weak) */
127 bt_self_message_iterator *msg_iter;
5c563278 128
188edac1 129 /* True to emit a stream beginning message. */
fc917f65
PP
130 bool emit_stream_begin_msg;
131
188edac1 132 /* True to emit a stream end message. */
fc917f65
PP
133 bool emit_stream_end_msg;
134
495490c5
PP
135 /* True to set the stream */
136 bool set_stream;
137
e98a2d6e
PP
138 /*
139 * Current dynamic scope field pointer.
140 *
312c056a
PP
141 * This is set by read_dscope_begin_state() and contains the
142 * value of one of the pointers in `dscopes` below.
e98a2d6e 143 */
b19ff26f 144 bt_field *cur_dscope_field;
e98a2d6e 145
44c440bc
PP
146 /*
147 * True if we're done filling a string field from a text
148 * array/sequence payload.
149 */
150 bool done_filling_string;
151
152 /* Trace and classes */
41693723
PP
153 /* True to set IR fields */
154 bool set_ir_fields;
155
e98a2d6e 156 struct {
44c440bc
PP
157 struct ctf_trace_class *tc;
158 struct ctf_stream_class *sc;
159 struct ctf_event_class *ec;
e98a2d6e
PP
160 } meta;
161
83ebb7f1 162 /* Current packet context field wrapper (NULL if not created yet) */
b19ff26f 163 bt_packet_context_field *packet_context_field;
312c056a 164
e98a2d6e 165 /* Current packet (NULL if not created yet) */
b19ff26f 166 bt_packet *packet;
e98a2d6e 167
af87daef 168 /* Current stream (NULL if not set yet) */
b19ff26f 169 bt_stream *stream;
af87daef 170
312c056a 171 /* Current event (NULL if not created yet) */
b19ff26f 172 bt_event *event;
312c056a 173
d6e69534
PP
174 /* Current event message (NULL if not created yet) */
175 bt_message *event_msg;
312c056a 176
44c440bc 177 /* Database of current dynamic scopes */
e98a2d6e 178 struct {
b19ff26f 179 bt_field *stream_packet_context;
b19ff26f
PP
180 bt_field *event_common_context;
181 bt_field *event_spec_context;
182 bt_field *event_payload;
e98a2d6e
PP
183 } dscopes;
184
185 /* Current state */
186 enum state state;
187
2cf1d51e 188 /* Current medium buffer data */
e98a2d6e
PP
189 struct {
190 /* Last address provided by medium */
191 const uint8_t *addr;
192
193 /* Buffer size provided by medium (bytes) */
194 size_t sz;
195
196 /* Offset within whole packet of addr (bits) */
197 size_t packet_offset;
198
199 /* Current position from addr (bits) */
200 size_t at;
174e773b
PP
201
202 /* Position of the last event header from addr (bits) */
203 size_t last_eh_at;
e98a2d6e
PP
204 } buf;
205
206 /* Binary type reader */
5cd6d0e5 207 struct bt_bfcr *bfcr;
e98a2d6e 208
2cf1d51e 209 /* Current medium data */
e98a2d6e 210 struct {
d6e69534 211 struct bt_msg_iter_medium_ops medops;
e98a2d6e
PP
212 size_t max_request_sz;
213 void *data;
214 } medium;
215
216 /* Current packet size (bits) (-1 if unknown) */
44c440bc 217 int64_t cur_exp_packet_total_size;
e98a2d6e
PP
218
219 /* Current content size (bits) (-1 if unknown) */
44c440bc 220 int64_t cur_exp_packet_content_size;
c44c3e70 221
44c440bc
PP
222 /* Current stream class ID */
223 int64_t cur_stream_class_id;
9e0c8dbb 224
44c440bc
PP
225 /* Current event class ID */
226 int64_t cur_event_class_id;
5f870343 227
44c440bc
PP
228 /* Current data stream ID */
229 int64_t cur_data_stream_id;
5f870343
JG
230
231 /*
44c440bc
PP
232 * Offset, in the underlying media, of the current packet's
233 * start (-1 if unknown).
5f870343 234 */
44c440bc
PP
235 off_t cur_packet_offset;
236
237 /* Default clock's current value */
605e1019 238 uint64_t default_clock_snapshot;
5f870343 239
495490c5
PP
240 /* End of current packet snapshots */
241 struct end_of_packet_snapshots snapshots;
242
243 /* End of previous packet snapshots */
244 struct end_of_packet_snapshots prev_packet_snapshots;
44c440bc
PP
245
246 /* Stored values (for sequence lengths, variant tags) */
247 GArray *stored_values;
ea14c7dd
PP
248
249 /* Iterator's current log level */
250 bt_logging_level log_level;
eb7f6c4f
PP
251
252 /* Iterator's owning self component, or `NULL` if none (query) */
253 bt_self_component *self_comp;
e98a2d6e
PP
254};
255
fdf0e7a0
PP
256static inline
257const char *state_string(enum state state)
258{
259 switch (state) {
260 case STATE_INIT:
261 return "STATE_INIT";
262 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
263 return "STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN";
264 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
265 return "STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE";
266 case STATE_AFTER_TRACE_PACKET_HEADER:
267 return "STATE_AFTER_TRACE_PACKET_HEADER";
268 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN:
269 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN";
270 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE:
271 return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE";
272 case STATE_AFTER_STREAM_PACKET_CONTEXT:
273 return "STATE_AFTER_STREAM_PACKET_CONTEXT";
fc917f65
PP
274 case STATE_EMIT_MSG_STREAM_BEGINNING:
275 return "STATE_EMIT_MSG_STREAM_BEGINNING";
fc917f65
PP
276 case STATE_EMIT_MSG_PACKET_BEGINNING:
277 return "STATE_EMIT_MSG_PACKET_BEGINNING";
495490c5
PP
278 case STATE_EMIT_MSG_DISCARDED_EVENTS:
279 return "STATE_EMIT_MSG_DISCARDED_EVENTS";
280 case STATE_EMIT_MSG_DISCARDED_PACKETS:
281 return "STATE_EMIT_MSG_DISCARDED_PACKETS";
44c440bc
PP
282 case STATE_DSCOPE_EVENT_HEADER_BEGIN:
283 return "STATE_DSCOPE_EVENT_HEADER_BEGIN";
284 case STATE_DSCOPE_EVENT_HEADER_CONTINUE:
285 return "STATE_DSCOPE_EVENT_HEADER_CONTINUE";
286 case STATE_AFTER_EVENT_HEADER:
287 return "STATE_AFTER_EVENT_HEADER";
288 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN:
289 return "STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN";
290 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE:
291 return "STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE";
292 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN:
293 return "STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN";
294 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE:
295 return "STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE";
fdf0e7a0
PP
296 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN:
297 return "STATE_DSCOPE_EVENT_PAYLOAD_BEGIN";
298 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE:
299 return "STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE";
d6e69534
PP
300 case STATE_EMIT_MSG_EVENT:
301 return "STATE_EMIT_MSG_EVENT";
fdf0e7a0
PP
302 case STATE_SKIP_PACKET_PADDING:
303 return "STATE_SKIP_PACKET_PADDING";
fc917f65
PP
304 case STATE_EMIT_MSG_PACKET_END_MULTI:
305 return "STATE_EMIT_MSG_PACKET_END_MULTI";
306 case STATE_EMIT_MSG_PACKET_END_SINGLE:
307 return "STATE_EMIT_MSG_PACKET_END_SINGLE";
fc917f65
PP
308 case STATE_EMIT_MSG_STREAM_END:
309 return "STATE_EMIT_MSG_STREAM_END";
310 case STATE_DONE:
311 return "STATE_DONE";
fdf0e7a0
PP
312 default:
313 return "(unknown)";
314 }
315}
316
2cf1d51e 317static
d6e69534 318int bt_msg_iter_switch_packet(struct bt_msg_iter *notit);
2cf1d51e 319
e98a2d6e 320static
d6e69534 321struct stack *stack_new(struct bt_msg_iter *notit)
e98a2d6e
PP
322{
323 struct stack *stack = NULL;
324
325 stack = g_new0(struct stack, 1);
326 if (!stack) {
eb7f6c4f 327 BT_COMP_LOGE_STR("Failed to allocate one stack.");
e98a2d6e
PP
328 goto error;
329 }
330
ea14c7dd 331 stack->notit = notit;
afe86a06 332 stack->entries = g_array_new(FALSE, TRUE, sizeof(struct stack_entry));
e98a2d6e 333 if (!stack->entries) {
eb7f6c4f 334 BT_COMP_LOGE_STR("Failed to allocate a GArray.");
e98a2d6e
PP
335 goto error;
336 }
337
eb7f6c4f 338 BT_COMP_LOGD("Created stack: notit-addr=%p, stack-addr=%p", notit, stack);
44c440bc 339 goto end;
fdf0e7a0 340
e98a2d6e
PP
341error:
342 g_free(stack);
44c440bc
PP
343 stack = NULL;
344
345end:
346 return stack;
e98a2d6e
PP
347}
348
349static
350void stack_destroy(struct stack *stack)
351{
ea14c7dd
PP
352 struct bt_msg_iter *notit;
353
f6ccaed9 354 BT_ASSERT(stack);
ea14c7dd 355 notit = stack->notit;
eb7f6c4f 356 BT_COMP_LOGD("Destroying stack: addr=%p", stack);
afe86a06
PP
357
358 if (stack->entries) {
359 g_array_free(stack->entries, TRUE);
360 }
361
e98a2d6e
PP
362 g_free(stack);
363}
364
365static
b19ff26f 366void stack_push(struct stack *stack, bt_field *base)
e98a2d6e 367{
e98a2d6e 368 struct stack_entry *entry;
ea14c7dd 369 struct bt_msg_iter *notit;
e98a2d6e 370
f6ccaed9 371 BT_ASSERT(stack);
ea14c7dd 372 notit = stack->notit;
f6ccaed9 373 BT_ASSERT(base);
ef267d12 374 BT_COMP_LOGT("Pushing base field on stack: stack-addr=%p, "
afe86a06
PP
375 "stack-size-before=%zu, stack-size-after=%zu",
376 stack, stack->size, stack->size + 1);
377
378 if (stack->entries->len == stack->size) {
379 g_array_set_size(stack->entries, stack->size + 1);
e98a2d6e
PP
380 }
381
afe86a06 382 entry = &g_array_index(stack->entries, struct stack_entry, stack->size);
312c056a 383 entry->base = base;
afe86a06
PP
384 entry->index = 0;
385 stack->size++;
e98a2d6e
PP
386}
387
388static inline
389unsigned int stack_size(struct stack *stack)
390{
f6ccaed9 391 BT_ASSERT(stack);
afe86a06 392 return stack->size;
e98a2d6e
PP
393}
394
395static
396void stack_pop(struct stack *stack)
397{
ea14c7dd
PP
398 struct bt_msg_iter *notit;
399
f6ccaed9
PP
400 BT_ASSERT(stack);
401 BT_ASSERT(stack_size(stack));
ea14c7dd 402 notit = stack->notit;
ef267d12 403 BT_COMP_LOGT("Popping from stack: "
afe86a06
PP
404 "stack-addr=%p, stack-size-before=%zu, stack-size-after=%zu",
405 stack, stack->size, stack->size - 1);
406 stack->size--;
e98a2d6e
PP
407}
408
409static inline
410struct stack_entry *stack_top(struct stack *stack)
411{
f6ccaed9
PP
412 BT_ASSERT(stack);
413 BT_ASSERT(stack_size(stack));
afe86a06
PP
414 return &g_array_index(stack->entries, struct stack_entry,
415 stack->size - 1);
e98a2d6e
PP
416}
417
418static inline
419bool stack_empty(struct stack *stack)
420{
421 return stack_size(stack) == 0;
422}
423
424static
425void stack_clear(struct stack *stack)
426{
f6ccaed9 427 BT_ASSERT(stack);
afe86a06 428 stack->size = 0;
e98a2d6e
PP
429}
430
431static inline
d6e69534
PP
432enum bt_msg_iter_status msg_iter_status_from_m_status(
433 enum bt_msg_iter_medium_status m_status)
e98a2d6e 434{
44c440bc 435 /* They are the same */
dc77b521 436 return (int) m_status;
e98a2d6e
PP
437}
438
439static inline
d6e69534 440size_t buf_size_bits(struct bt_msg_iter *notit)
e98a2d6e 441{
fdf0e7a0 442 return notit->buf.sz * 8;
e98a2d6e
PP
443}
444
445static inline
d6e69534 446size_t buf_available_bits(struct bt_msg_iter *notit)
e98a2d6e
PP
447{
448 return buf_size_bits(notit) - notit->buf.at;
449}
450
451static inline
d6e69534 452size_t packet_at(struct bt_msg_iter *notit)
e98a2d6e
PP
453{
454 return notit->buf.packet_offset + notit->buf.at;
455}
456
e98a2d6e 457static inline
d6e69534 458void buf_consume_bits(struct bt_msg_iter *notit, size_t incr)
e98a2d6e 459{
ef267d12 460 BT_COMP_LOGT("Advancing cursor: notit-addr=%p, cur-before=%zu, cur-after=%zu",
fdf0e7a0 461 notit, notit->buf.at, notit->buf.at + incr);
e98a2d6e
PP
462 notit->buf.at += incr;
463}
464
e98a2d6e 465static
d6e69534
PP
466enum bt_msg_iter_status request_medium_bytes(
467 struct bt_msg_iter *notit)
e98a2d6e 468{
fdf0e7a0
PP
469 uint8_t *buffer_addr = NULL;
470 size_t buffer_sz = 0;
d6e69534 471 enum bt_msg_iter_medium_status m_status;
e98a2d6e 472
eb7f6c4f 473 BT_COMP_LOGD("Calling user function (request bytes): notit-addr=%p, "
fdf0e7a0 474 "request-size=%zu", notit, notit->medium.max_request_sz);
e98a2d6e
PP
475 m_status = notit->medium.medops.request_bytes(
476 notit->medium.max_request_sz, &buffer_addr,
477 &buffer_sz, notit->medium.data);
eb7f6c4f 478 BT_COMP_LOGD("User function returned: status=%s, buf-addr=%p, buf-size=%zu",
d6e69534 479 bt_msg_iter_medium_status_string(m_status),
fdf0e7a0 480 buffer_addr, buffer_sz);
d6e69534 481 if (m_status == BT_MSG_ITER_MEDIUM_STATUS_OK) {
f6ccaed9 482 BT_ASSERT(buffer_sz != 0);
e98a2d6e
PP
483
484 /* New packet offset is old one + old size (in bits) */
485 notit->buf.packet_offset += buf_size_bits(notit);
486
487 /* Restart at the beginning of the new medium buffer */
488 notit->buf.at = 0;
2b186c3e 489 notit->buf.last_eh_at = SIZE_MAX;
e98a2d6e
PP
490
491 /* New medium buffer size */
492 notit->buf.sz = buffer_sz;
493
494 /* New medium buffer address */
495 notit->buf.addr = buffer_addr;
fdf0e7a0 496
eb7f6c4f 497 BT_COMP_LOGD("User function returned new bytes: "
fdf0e7a0
PP
498 "packet-offset=%zu, cur=%zu, size=%zu, addr=%p",
499 notit->buf.packet_offset, notit->buf.at,
500 notit->buf.sz, notit->buf.addr);
eb7f6c4f 501 BT_COMP_LOGD_MEM(buffer_addr, buffer_sz, "Returned bytes at %p:",
fdf0e7a0 502 buffer_addr);
d6e69534 503 } else if (m_status == BT_MSG_ITER_MEDIUM_STATUS_EOF) {
0684124b
PP
504 /*
505 * User returned end of stream: validate that we're not
506 * in the middle of a packet header, packet context, or
507 * event.
508 */
44c440bc
PP
509 if (notit->cur_exp_packet_total_size >= 0) {
510 if (packet_at(notit) ==
511 notit->cur_exp_packet_total_size) {
df0139b8 512 goto end;
0684124b 513 }
df0139b8
PP
514 } else {
515 if (packet_at(notit) == 0) {
516 goto end;
0684124b 517 }
0684124b 518
df0139b8
PP
519 if (notit->buf.last_eh_at != SIZE_MAX &&
520 notit->buf.at == notit->buf.last_eh_at) {
521 goto end;
0684124b
PP
522 }
523 }
524
0684124b 525 /* All other states are invalid */
eb7f6c4f 526 BT_COMP_LOGW("User function returned %s, but message iterator is in an unexpected state: "
df0139b8
PP
527 "state=%s, cur-packet-size=%" PRId64 ", cur=%zu, "
528 "packet-cur=%zu, last-eh-at=%zu",
d6e69534 529 bt_msg_iter_medium_status_string(m_status),
df0139b8 530 state_string(notit->state),
44c440bc 531 notit->cur_exp_packet_total_size,
df0139b8
PP
532 notit->buf.at, packet_at(notit),
533 notit->buf.last_eh_at);
d6e69534 534 m_status = BT_MSG_ITER_MEDIUM_STATUS_ERROR;
fdf0e7a0 535 } else if (m_status < 0) {
eb7f6c4f 536 BT_COMP_LOGW("User function failed: status=%s",
d6e69534 537 bt_msg_iter_medium_status_string(m_status));
e98a2d6e
PP
538 }
539
df0139b8 540end:
d6e69534 541 return msg_iter_status_from_m_status(m_status);
e98a2d6e
PP
542}
543
544static inline
d6e69534
PP
545enum bt_msg_iter_status buf_ensure_available_bits(
546 struct bt_msg_iter *notit)
e98a2d6e 547{
d6e69534 548 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
e98a2d6e 549
91d81473 550 if (G_UNLIKELY(buf_available_bits(notit) == 0)) {
e98a2d6e 551 /*
d6e69534 552 * This _cannot_ return BT_MSG_ITER_STATUS_OK
e98a2d6e
PP
553 * _and_ no bits.
554 */
555 status = request_medium_bytes(notit);
556 }
557
558 return status;
559}
560
561static
d6e69534
PP
562enum bt_msg_iter_status read_dscope_begin_state(
563 struct bt_msg_iter *notit,
5cd6d0e5 564 struct ctf_field_class *dscope_fc,
e98a2d6e 565 enum state done_state, enum state continue_state,
b19ff26f 566 bt_field *dscope_field)
e98a2d6e 567{
d6e69534 568 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
5cd6d0e5 569 enum bt_bfcr_status bfcr_status;
e98a2d6e
PP
570 size_t consumed_bits;
571
e98a2d6e 572 notit->cur_dscope_field = dscope_field;
ef267d12 573 BT_COMP_LOGT("Starting BFCR: notit-addr=%p, bfcr-addr=%p, fc-addr=%p",
5cd6d0e5
PP
574 notit, notit->bfcr, dscope_fc);
575 consumed_bits = bt_bfcr_start(notit->bfcr, dscope_fc,
e98a2d6e 576 notit->buf.addr, notit->buf.at, packet_at(notit),
5cd6d0e5 577 notit->buf.sz, &bfcr_status);
ef267d12 578 BT_COMP_LOGT("BFCR consumed bits: size=%zu", consumed_bits);
e98a2d6e 579
5cd6d0e5
PP
580 switch (bfcr_status) {
581 case BT_BFCR_STATUS_OK:
582 /* Field class was read completely */
ef267d12 583 BT_COMP_LOGT_STR("Field was completely decoded.");
e98a2d6e
PP
584 notit->state = done_state;
585 break;
5cd6d0e5 586 case BT_BFCR_STATUS_EOF:
ef267d12 587 BT_COMP_LOGT_STR("BFCR needs more data to decode field completely.");
e98a2d6e
PP
588 notit->state = continue_state;
589 break;
590 default:
eb7f6c4f 591 BT_COMP_LOGW("BFCR failed to start: notit-addr=%p, bfcr-addr=%p, "
5cd6d0e5
PP
592 "status=%s", notit, notit->bfcr,
593 bt_bfcr_status_string(bfcr_status));
d6e69534 594 status = BT_MSG_ITER_STATUS_ERROR;
e98a2d6e
PP
595 goto end;
596 }
597
598 /* Consume bits now since we know we're not in an error state */
599 buf_consume_bits(notit, consumed_bits);
600
601end:
602 return status;
603}
604
605static
d6e69534
PP
606enum bt_msg_iter_status read_dscope_continue_state(
607 struct bt_msg_iter *notit, enum state done_state)
e98a2d6e 608{
d6e69534 609 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
5cd6d0e5 610 enum bt_bfcr_status bfcr_status;
e98a2d6e
PP
611 size_t consumed_bits;
612
ef267d12 613 BT_COMP_LOGT("Continuing BFCR: notit-addr=%p, bfcr-addr=%p",
5cd6d0e5 614 notit, notit->bfcr);
df0139b8 615
e98a2d6e 616 status = buf_ensure_available_bits(notit);
d6e69534 617 if (status != BT_MSG_ITER_STATUS_OK) {
fdf0e7a0 618 if (status < 0) {
eb7f6c4f 619 BT_COMP_LOGW("Cannot ensure that buffer has at least one byte: "
d6e69534
PP
620 "msg-addr=%p, status=%s",
621 notit, bt_msg_iter_status_string(status));
fdf0e7a0 622 } else {
ef267d12 623 BT_COMP_LOGT("Cannot ensure that buffer has at least one byte: "
d6e69534
PP
624 "msg-addr=%p, status=%s",
625 notit, bt_msg_iter_status_string(status));
fdf0e7a0
PP
626 }
627
e98a2d6e
PP
628 goto end;
629 }
630
5cd6d0e5
PP
631 consumed_bits = bt_bfcr_continue(notit->bfcr, notit->buf.addr,
632 notit->buf.sz, &bfcr_status);
ef267d12 633 BT_COMP_LOGT("BFCR consumed bits: size=%zu", consumed_bits);
e98a2d6e 634
5cd6d0e5
PP
635 switch (bfcr_status) {
636 case BT_BFCR_STATUS_OK:
78586d8a 637 /* Type was read completely. */
ef267d12 638 BT_COMP_LOGT_STR("Field was completely decoded.");
e98a2d6e
PP
639 notit->state = done_state;
640 break;
5cd6d0e5 641 case BT_BFCR_STATUS_EOF:
78586d8a 642 /* Stay in this continue state. */
ef267d12 643 BT_COMP_LOGT_STR("BFCR needs more data to decode field completely.");
e98a2d6e
PP
644 break;
645 default:
eb7f6c4f 646 BT_COMP_LOGW("BFCR failed to continue: notit-addr=%p, bfcr-addr=%p, "
5cd6d0e5
PP
647 "status=%s", notit, notit->bfcr,
648 bt_bfcr_status_string(bfcr_status));
d6e69534 649 status = BT_MSG_ITER_STATUS_ERROR;
e98a2d6e
PP
650 goto end;
651 }
652
78586d8a 653 /* Consume bits now since we know we're not in an error state. */
e98a2d6e 654 buf_consume_bits(notit, consumed_bits);
e98a2d6e
PP
655end:
656 return status;
657}
658
659static
d6e69534 660void release_event_dscopes(struct bt_msg_iter *notit)
e98a2d6e 661{
44c440bc
PP
662 notit->dscopes.event_common_context = NULL;
663 notit->dscopes.event_spec_context = NULL;
312c056a 664 notit->dscopes.event_payload = NULL;
e98a2d6e
PP
665}
666
667static
d6e69534 668void release_all_dscopes(struct bt_msg_iter *notit)
e98a2d6e 669{
312c056a
PP
670 notit->dscopes.stream_packet_context = NULL;
671
672 if (notit->packet_context_field) {
40f4ba76 673 bt_packet_context_field_release(notit->packet_context_field);
312c056a
PP
674 notit->packet_context_field = NULL;
675 }
676
677 release_event_dscopes(notit);
e98a2d6e
PP
678}
679
680static
d6e69534
PP
681enum bt_msg_iter_status read_packet_header_begin_state(
682 struct bt_msg_iter *notit)
e98a2d6e 683{
5cd6d0e5 684 struct ctf_field_class *packet_header_fc = NULL;
d6e69534 685 enum bt_msg_iter_status ret = BT_MSG_ITER_STATUS_OK;
e98a2d6e 686
d6e69534 687 if (bt_msg_iter_switch_packet(notit)) {
eb7f6c4f 688 BT_COMP_LOGW("Cannot switch packet: notit-addr=%p", notit);
d6e69534 689 ret = BT_MSG_ITER_STATUS_ERROR;
2cf1d51e
JG
690 goto end;
691 }
e98a2d6e 692
fc917f65
PP
693 /*
694 * Make sure at least one bit is available for this packet. An
695 * empty packet is impossible. If we reach the end of the medium
696 * at this point, then it's considered the end of the stream.
697 */
698 ret = buf_ensure_available_bits(notit);
699 switch (ret) {
700 case BT_MSG_ITER_STATUS_OK:
701 break;
702 case BT_MSG_ITER_STATUS_EOF:
703 ret = BT_MSG_ITER_STATUS_OK;
188edac1 704 notit->state = STATE_CHECK_EMIT_MSG_STREAM_END;
fc917f65
PP
705 goto end;
706 default:
707 goto end;
708 }
709
862ca4ed 710 /* Packet header class is common to the whole trace class. */
5cd6d0e5
PP
711 packet_header_fc = notit->meta.tc->packet_header_fc;
712 if (!packet_header_fc) {
835b2d10 713 notit->state = STATE_AFTER_TRACE_PACKET_HEADER;
e98a2d6e
PP
714 goto end;
715 }
716
44c440bc
PP
717 notit->cur_stream_class_id = -1;
718 notit->cur_event_class_id = -1;
719 notit->cur_data_stream_id = -1;
eb7f6c4f 720 BT_COMP_LOGD("Decoding packet header field:"
862ca4ed
PP
721 "notit-addr=%p, trace-class-addr=%p, fc-addr=%p",
722 notit, notit->meta.tc, packet_header_fc);
5cd6d0e5 723 ret = read_dscope_begin_state(notit, packet_header_fc,
fdf0e7a0 724 STATE_AFTER_TRACE_PACKET_HEADER,
83ebb7f1 725 STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE, NULL);
fdf0e7a0 726 if (ret < 0) {
eb7f6c4f 727 BT_COMP_LOGW("Cannot decode packet header field: "
862ca4ed
PP
728 "notit-addr=%p, trace-class-addr=%p, "
729 "fc-addr=%p",
730 notit, notit->meta.tc, packet_header_fc);
fdf0e7a0 731 }
d1e46835 732
e98a2d6e 733end:
2cf1d51e 734 return ret;
e98a2d6e
PP
735}
736
737static
d6e69534
PP
738enum bt_msg_iter_status read_packet_header_continue_state(
739 struct bt_msg_iter *notit)
e98a2d6e
PP
740{
741 return read_dscope_continue_state(notit,
44c440bc 742 STATE_AFTER_TRACE_PACKET_HEADER);
5f870343
JG
743}
744
e98a2d6e 745static inline
d6e69534 746enum bt_msg_iter_status set_current_stream_class(struct bt_msg_iter *notit)
e98a2d6e 747{
d6e69534 748 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
44c440bc
PP
749 struct ctf_stream_class *new_stream_class = NULL;
750
751 if (notit->cur_stream_class_id == -1) {
fdf0e7a0 752 /*
44c440bc
PP
753 * No current stream class ID field, therefore only one
754 * stream class.
fdf0e7a0 755 */
44c440bc 756 if (notit->meta.tc->stream_classes->len != 1) {
eb7f6c4f 757 BT_COMP_LOGW("Need exactly one stream class since there's "
44c440bc 758 "no stream class ID field: "
862ca4ed 759 "notit-addr=%p", notit);
d6e69534 760 status = BT_MSG_ITER_STATUS_ERROR;
44c440bc
PP
761 goto end;
762 }
e98a2d6e 763
44c440bc
PP
764 new_stream_class = notit->meta.tc->stream_classes->pdata[0];
765 notit->cur_stream_class_id = new_stream_class->id;
e98a2d6e
PP
766 }
767
44c440bc
PP
768 new_stream_class = ctf_trace_class_borrow_stream_class_by_id(
769 notit->meta.tc, notit->cur_stream_class_id);
115de887 770 if (!new_stream_class) {
eb7f6c4f 771 BT_COMP_LOGW("No stream class with ID of stream class ID to use in trace class: "
fdf0e7a0 772 "notit-addr=%p, stream-class-id=%" PRIu64 ", "
862ca4ed
PP
773 "trace-class-addr=%p",
774 notit, notit->cur_stream_class_id, notit->meta.tc);
d6e69534 775 status = BT_MSG_ITER_STATUS_ERROR;
e98a2d6e
PP
776 goto end;
777 }
778
44c440bc
PP
779 if (notit->meta.sc) {
780 if (new_stream_class != notit->meta.sc) {
eb7f6c4f 781 BT_COMP_LOGW("Two packets refer to two different stream classes within the same packet sequence: "
115de887 782 "notit-addr=%p, prev-stream-class-addr=%p, "
115de887
PP
783 "prev-stream-class-id=%" PRId64 ", "
784 "next-stream-class-addr=%p, "
115de887 785 "next-stream-class-id=%" PRId64 ", "
862ca4ed 786 "trace-addr=%p",
44c440bc
PP
787 notit, notit->meta.sc,
788 notit->meta.sc->id,
115de887 789 new_stream_class,
44c440bc 790 new_stream_class->id,
862ca4ed 791 notit->meta.tc);
d6e69534 792 status = BT_MSG_ITER_STATUS_ERROR;
115de887
PP
793 goto end;
794 }
795 } else {
44c440bc 796 notit->meta.sc = new_stream_class;
115de887
PP
797 }
798
eb7f6c4f 799 BT_COMP_LOGD("Set current stream class: "
fdf0e7a0 800 "notit-addr=%p, stream-class-addr=%p, "
44c440bc
PP
801 "stream-class-id=%" PRId64,
802 notit, notit->meta.sc, notit->meta.sc->id);
d1e46835 803
e98a2d6e 804end:
e98a2d6e
PP
805 return status;
806}
807
312c056a 808static inline
d6e69534 809enum bt_msg_iter_status set_current_stream(struct bt_msg_iter *notit)
312c056a 810{
d6e69534 811 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
b19ff26f 812 bt_stream *stream = NULL;
312c056a 813
eb7f6c4f 814 BT_COMP_LOGD("Calling user function (get stream): notit-addr=%p, "
44c440bc
PP
815 "stream-class-addr=%p, stream-class-id=%" PRId64,
816 notit, notit->meta.sc,
817 notit->meta.sc->id);
398454ed 818 stream = notit->medium.medops.borrow_stream(
44c440bc 819 notit->meta.sc->ir_sc, notit->cur_data_stream_id,
398454ed 820 notit->medium.data);
c5b9b441 821 bt_stream_get_ref(stream);
eb7f6c4f 822 BT_COMP_LOGD("User function returned: stream-addr=%p", stream);
312c056a 823 if (!stream) {
eb7f6c4f 824 BT_COMP_LOGW_STR("User function failed to return a stream object "
44c440bc 825 "for the given stream class.");
d6e69534 826 status = BT_MSG_ITER_STATUS_ERROR;
312c056a
PP
827 goto end;
828 }
829
830 if (notit->stream && stream != notit->stream) {
eb7f6c4f 831 BT_COMP_LOGW("User function returned a different stream than the "
44c440bc 832 "previous one for the same sequence of packets.");
d6e69534 833 status = BT_MSG_ITER_STATUS_ERROR;
312c056a
PP
834 goto end;
835 }
836
c5b9b441 837 BT_STREAM_MOVE_REF(notit->stream, stream);
312c056a
PP
838
839end:
c5b9b441 840 bt_stream_put_ref(stream);
312c056a
PP
841 return status;
842}
843
844static inline
d6e69534 845enum bt_msg_iter_status set_current_packet(struct bt_msg_iter *notit)
312c056a 846{
d6e69534 847 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
b19ff26f 848 bt_packet *packet = NULL;
312c056a 849
eb7f6c4f 850 BT_COMP_LOGD("Creating packet for packet message: "
312c056a 851 "notit-addr=%p", notit);
eb7f6c4f 852 BT_COMP_LOGD("Creating packet from stream: "
312c056a
PP
853 "notit-addr=%p, stream-addr=%p, "
854 "stream-class-addr=%p, "
312c056a 855 "stream-class-id=%" PRId64,
44c440bc
PP
856 notit, notit->stream, notit->meta.sc,
857 notit->meta.sc->id);
312c056a
PP
858
859 /* Create packet */
860 BT_ASSERT(notit->stream);
40f4ba76 861 packet = bt_packet_create(notit->stream);
312c056a 862 if (!packet) {
eb7f6c4f 863 BT_COMP_LOGE("Cannot create packet from stream: "
312c056a
PP
864 "notit-addr=%p, stream-addr=%p, "
865 "stream-class-addr=%p, "
312c056a 866 "stream-class-id=%" PRId64,
44c440bc
PP
867 notit, notit->stream, notit->meta.sc,
868 notit->meta.sc->id);
312c056a
PP
869 goto error;
870 }
871
872 goto end;
873
874error:
c5b9b441 875 BT_PACKET_PUT_REF_AND_RESET(packet);
d6e69534 876 status = BT_MSG_ITER_STATUS_ERROR;
312c056a
PP
877
878end:
c5b9b441 879 BT_PACKET_MOVE_REF(notit->packet, packet);
312c056a
PP
880 return status;
881}
882
e98a2d6e 883static
d6e69534
PP
884enum bt_msg_iter_status after_packet_header_state(
885 struct bt_msg_iter *notit)
e98a2d6e 886{
d6e69534 887 enum bt_msg_iter_status status;
e98a2d6e
PP
888
889 status = set_current_stream_class(notit);
d6e69534 890 if (status != BT_MSG_ITER_STATUS_OK) {
312c056a 891 goto end;
e98a2d6e
PP
892 }
893
312c056a
PP
894 notit->state = STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN;
895
896end:
e98a2d6e
PP
897 return status;
898}
899
900static
d6e69534
PP
901enum bt_msg_iter_status read_packet_context_begin_state(
902 struct bt_msg_iter *notit)
e98a2d6e 903{
d6e69534 904 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
5cd6d0e5 905 struct ctf_field_class *packet_context_fc;
e98a2d6e 906
44c440bc 907 BT_ASSERT(notit->meta.sc);
5cd6d0e5
PP
908 packet_context_fc = notit->meta.sc->packet_context_fc;
909 if (!packet_context_fc) {
eb7f6c4f 910 BT_COMP_LOGD("No packet packet context field class in stream class: continuing: "
fdf0e7a0 911 "notit-addr=%p, stream-class-addr=%p, "
44c440bc
PP
912 "stream-class-id=%" PRId64,
913 notit, notit->meta.sc,
914 notit->meta.sc->id);
835b2d10 915 notit->state = STATE_AFTER_STREAM_PACKET_CONTEXT;
e98a2d6e
PP
916 goto end;
917 }
918
312c056a 919 BT_ASSERT(!notit->packet_context_field);
44c440bc 920
5cd6d0e5 921 if (packet_context_fc->in_ir) {
44c440bc
PP
922 /*
923 * Create free packet context field from stream class.
924 * This field is going to be moved to the packet once we
925 * create it. We cannot create the packet now because a
926 * packet is created from a stream, and this API must be
83ebb7f1
PP
927 * able to return the packet context properties without
928 * creating a stream
929 * (bt_msg_iter_get_packet_properties()).
44c440bc
PP
930 */
931 notit->packet_context_field =
40f4ba76 932 bt_packet_context_field_create(
e5be10ef 933 notit->meta.sc->ir_sc);
44c440bc 934 if (!notit->packet_context_field) {
eb7f6c4f 935 BT_COMP_LOGE_STR("Cannot create packet context field wrapper from stream class.");
d6e69534 936 status = BT_MSG_ITER_STATUS_ERROR;
44c440bc
PP
937 goto end;
938 }
939
940 notit->dscopes.stream_packet_context =
40f4ba76 941 bt_packet_context_field_borrow_field(
e5be10ef 942 notit->packet_context_field);
44c440bc 943 BT_ASSERT(notit->dscopes.stream_packet_context);
312c056a
PP
944 }
945
eb7f6c4f 946 BT_COMP_LOGD("Decoding packet context field: "
fdf0e7a0 947 "notit-addr=%p, stream-class-addr=%p, "
5cd6d0e5 948 "stream-class-id=%" PRId64 ", fc-addr=%p",
44c440bc 949 notit, notit->meta.sc,
5cd6d0e5
PP
950 notit->meta.sc->id, packet_context_fc);
951 status = read_dscope_begin_state(notit, packet_context_fc,
fdf0e7a0
PP
952 STATE_AFTER_STREAM_PACKET_CONTEXT,
953 STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE,
312c056a 954 notit->dscopes.stream_packet_context);
fdf0e7a0 955 if (status < 0) {
eb7f6c4f 956 BT_COMP_LOGW("Cannot decode packet context field: "
fdf0e7a0 957 "notit-addr=%p, stream-class-addr=%p, "
5cd6d0e5 958 "stream-class-id=%" PRId64 ", fc-addr=%p",
44c440bc
PP
959 notit, notit->meta.sc,
960 notit->meta.sc->id,
5cd6d0e5 961 packet_context_fc);
fdf0e7a0 962 }
e98a2d6e
PP
963
964end:
e98a2d6e
PP
965 return status;
966}
967
968static
d6e69534
PP
969enum bt_msg_iter_status read_packet_context_continue_state(
970 struct bt_msg_iter *notit)
e98a2d6e
PP
971{
972 return read_dscope_continue_state(notit,
78586d8a 973 STATE_AFTER_STREAM_PACKET_CONTEXT);
e98a2d6e
PP
974}
975
78586d8a 976static
d6e69534
PP
977enum bt_msg_iter_status set_current_packet_content_sizes(
978 struct bt_msg_iter *notit)
e98a2d6e 979{
d6e69534 980 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
e98a2d6e 981
44c440bc
PP
982 if (notit->cur_exp_packet_total_size == -1) {
983 if (notit->cur_exp_packet_content_size != -1) {
01f71e30
PP
984 notit->cur_exp_packet_total_size =
985 notit->cur_exp_packet_content_size;
e98a2d6e 986 }
e98a2d6e 987 } else {
44c440bc
PP
988 if (notit->cur_exp_packet_content_size == -1) {
989 notit->cur_exp_packet_content_size =
990 notit->cur_exp_packet_total_size;
991 }
e98a2d6e
PP
992 }
993
9067de2b
PP
994 BT_ASSERT((notit->cur_exp_packet_total_size >= 0 &&
995 notit->cur_exp_packet_content_size >= 0) ||
996 (notit->cur_exp_packet_total_size < 0 &&
997 notit->cur_exp_packet_content_size < 0));
01f71e30 998
44c440bc
PP
999 if (notit->cur_exp_packet_content_size >
1000 notit->cur_exp_packet_total_size) {
eb7f6c4f 1001 BT_COMP_LOGW("Invalid packet or content size: "
44c440bc 1002 "content size is greater than packet size: "
fdf0e7a0 1003 "notit-addr=%p, packet-context-field-addr=%p, "
44c440bc 1004 "packet-size=%" PRId64 ", content-size=%" PRId64,
fdf0e7a0 1005 notit, notit->dscopes.stream_packet_context,
44c440bc
PP
1006 notit->cur_exp_packet_total_size,
1007 notit->cur_exp_packet_content_size);
d6e69534 1008 status = BT_MSG_ITER_STATUS_ERROR;
fdf0e7a0
PP
1009 goto end;
1010 }
1011
eb7f6c4f 1012 BT_COMP_LOGD("Set current packet and content sizes: "
fdf0e7a0 1013 "notit-addr=%p, packet-size=%" PRIu64 ", content-size=%" PRIu64,
44c440bc
PP
1014 notit, notit->cur_exp_packet_total_size,
1015 notit->cur_exp_packet_content_size);
fc917f65 1016
e98a2d6e 1017end:
e98a2d6e
PP
1018 return status;
1019}
1020
1021static
fc917f65 1022enum bt_msg_iter_status after_packet_context_state(struct bt_msg_iter *notit)
e98a2d6e 1023{
d6e69534 1024 enum bt_msg_iter_status status;
e98a2d6e
PP
1025
1026 status = set_current_packet_content_sizes(notit);
d6e69534 1027 if (status != BT_MSG_ITER_STATUS_OK) {
e22b45d0
PP
1028 goto end;
1029 }
1030
fc917f65
PP
1031 if (notit->stream) {
1032 /*
1033 * Stream exists, which means we already emitted at
1034 * least one packet beginning message, so the initial
1035 * stream beginning message was also emitted.
1036 */
495490c5 1037 notit->state = STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS;
e22b45d0 1038 } else {
495490c5 1039 notit->state = STATE_CHECK_EMIT_MSG_STREAM_BEGINNING;
e98a2d6e
PP
1040 }
1041
e22b45d0 1042end:
e98a2d6e
PP
1043 return status;
1044}
1045
1046static
fc917f65 1047enum bt_msg_iter_status read_event_header_begin_state(struct bt_msg_iter *notit)
e98a2d6e 1048{
d6e69534 1049 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
5cd6d0e5 1050 struct ctf_field_class *event_header_fc = NULL;
e98a2d6e 1051
174e773b
PP
1052 /* Reset the position of the last event header */
1053 notit->buf.last_eh_at = notit->buf.at;
44c440bc 1054 notit->cur_event_class_id = -1;
174e773b 1055
e98a2d6e 1056 /* Check if we have some content left */
44c440bc 1057 if (notit->cur_exp_packet_content_size >= 0) {
91d81473 1058 if (G_UNLIKELY(packet_at(notit) ==
44c440bc 1059 notit->cur_exp_packet_content_size)) {
e98a2d6e 1060 /* No more events! */
eb7f6c4f 1061 BT_COMP_LOGD("Reached end of packet: notit-addr=%p, "
fdf0e7a0 1062 "cur=%zu", notit, packet_at(notit));
fc917f65 1063 notit->state = STATE_EMIT_MSG_PACKET_END_MULTI;
e98a2d6e 1064 goto end;
91d81473 1065 } else if (G_UNLIKELY(packet_at(notit) >
44c440bc 1066 notit->cur_exp_packet_content_size)) {
e98a2d6e 1067 /* That's not supposed to happen */
eb7f6c4f 1068 BT_COMP_LOGD("Before decoding event header field: cursor is passed the packet's content: "
1974687e 1069 "notit-addr=%p, content-size=%" PRId64 ", "
44c440bc
PP
1070 "cur=%zu", notit,
1071 notit->cur_exp_packet_content_size,
fdf0e7a0 1072 packet_at(notit));
d6e69534 1073 status = BT_MSG_ITER_STATUS_ERROR;
e98a2d6e
PP
1074 goto end;
1075 }
44c440bc
PP
1076 } else {
1077 /*
1078 * "Infinite" content: we're done when the medium has
1079 * nothing else for us.
1080 */
1081 status = buf_ensure_available_bits(notit);
fc917f65
PP
1082 switch (status) {
1083 case BT_MSG_ITER_STATUS_OK:
1084 break;
1085 case BT_MSG_ITER_STATUS_EOF:
1086 status = BT_MSG_ITER_STATUS_OK;
1087 notit->state = STATE_EMIT_MSG_PACKET_END_SINGLE;
1088 goto end;
1089 default:
44c440bc
PP
1090 goto end;
1091 }
e98a2d6e
PP
1092 }
1093
312c056a 1094 release_event_dscopes(notit);
44c440bc 1095 BT_ASSERT(notit->meta.sc);
5cd6d0e5
PP
1096 event_header_fc = notit->meta.sc->event_header_fc;
1097 if (!event_header_fc) {
44c440bc 1098 notit->state = STATE_AFTER_EVENT_HEADER;
e98a2d6e
PP
1099 goto end;
1100 }
1101
eb7f6c4f 1102 BT_COMP_LOGD("Decoding event header field: "
fdf0e7a0 1103 "notit-addr=%p, stream-class-addr=%p, "
44c440bc 1104 "stream-class-id=%" PRId64 ", "
5cd6d0e5 1105 "fc-addr=%p",
44c440bc
PP
1106 notit, notit->meta.sc,
1107 notit->meta.sc->id,
5cd6d0e5
PP
1108 event_header_fc);
1109 status = read_dscope_begin_state(notit, event_header_fc,
44c440bc 1110 STATE_AFTER_EVENT_HEADER,
83ebb7f1 1111 STATE_DSCOPE_EVENT_HEADER_CONTINUE, NULL);
fdf0e7a0 1112 if (status < 0) {
eb7f6c4f 1113 BT_COMP_LOGW("Cannot decode event header field: "
fdf0e7a0 1114 "notit-addr=%p, stream-class-addr=%p, "
5cd6d0e5 1115 "stream-class-id=%" PRId64 ", fc-addr=%p",
44c440bc
PP
1116 notit, notit->meta.sc,
1117 notit->meta.sc->id,
5cd6d0e5 1118 event_header_fc);
fdf0e7a0 1119 }
e98a2d6e 1120
d1e46835 1121end:
e98a2d6e
PP
1122 return status;
1123}
1124
1125static
d6e69534
PP
1126enum bt_msg_iter_status read_event_header_continue_state(
1127 struct bt_msg_iter *notit)
e98a2d6e
PP
1128{
1129 return read_dscope_continue_state(notit,
44c440bc 1130 STATE_AFTER_EVENT_HEADER);
e98a2d6e
PP
1131}
1132
1133static inline
d6e69534 1134enum bt_msg_iter_status set_current_event_class(struct bt_msg_iter *notit)
e98a2d6e 1135{
d6e69534 1136 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
e98a2d6e 1137
44c440bc 1138 struct ctf_event_class *new_event_class = NULL;
e98a2d6e 1139
44c440bc 1140 if (notit->cur_event_class_id == -1) {
e98a2d6e 1141 /*
44c440bc
PP
1142 * No current event class ID field, therefore only one
1143 * event class.
e98a2d6e 1144 */
44c440bc 1145 if (notit->meta.sc->event_classes->len != 1) {
eb7f6c4f 1146 BT_COMP_LOGW("Need exactly one event class since there's "
44c440bc 1147 "no event class ID field: "
862ca4ed 1148 "notit-addr=%p", notit);
d6e69534 1149 status = BT_MSG_ITER_STATUS_ERROR;
44c440bc 1150 goto end;
fdf0e7a0 1151 }
e98a2d6e 1152
44c440bc
PP
1153 new_event_class = notit->meta.sc->event_classes->pdata[0];
1154 notit->cur_event_class_id = new_event_class->id;
e98a2d6e
PP
1155 }
1156
44c440bc
PP
1157 new_event_class = ctf_stream_class_borrow_event_class_by_id(
1158 notit->meta.sc, notit->cur_event_class_id);
1159 if (!new_event_class) {
eb7f6c4f 1160 BT_COMP_LOGW("No event class with ID of event class ID to use in stream class: "
44c440bc
PP
1161 "notit-addr=%p, stream-class-id=%" PRIu64 ", "
1162 "event-class-id=%" PRIu64 ", "
862ca4ed 1163 "trace-class-addr=%p",
44c440bc 1164 notit, notit->meta.sc->id, notit->cur_event_class_id,
862ca4ed 1165 notit->meta.tc);
d6e69534 1166 status = BT_MSG_ITER_STATUS_ERROR;
e98a2d6e
PP
1167 goto end;
1168 }
1169
44c440bc 1170 notit->meta.ec = new_event_class;
eb7f6c4f 1171 BT_COMP_LOGD("Set current event class: "
fdf0e7a0 1172 "notit-addr=%p, event-class-addr=%p, "
44c440bc
PP
1173 "event-class-id=%" PRId64 ", "
1174 "event-class-name=\"%s\"",
1175 notit, notit->meta.ec, notit->meta.ec->id,
1176 notit->meta.ec->name->str);
fdf0e7a0 1177
e98a2d6e 1178end:
e98a2d6e
PP
1179 return status;
1180}
1181
312c056a 1182static inline
d6e69534
PP
1183enum bt_msg_iter_status set_current_event_message(
1184 struct bt_msg_iter *notit)
312c056a 1185{
d6e69534
PP
1186 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
1187 bt_message *msg = NULL;
312c056a 1188
44c440bc 1189 BT_ASSERT(notit->meta.ec);
312c056a 1190 BT_ASSERT(notit->packet);
eb7f6c4f 1191 BT_COMP_LOGD("Creating event message from event class and packet: "
312c056a 1192 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", packet-addr=%p",
44c440bc
PP
1193 notit, notit->meta.ec,
1194 notit->meta.ec->name->str,
312c056a 1195 notit->packet);
d6e69534 1196 BT_ASSERT(notit->msg_iter);
2c091c04
PP
1197 BT_ASSERT(notit->meta.sc);
1198
1199 if (bt_stream_class_borrow_default_clock_class(notit->meta.sc->ir_sc)) {
26fc5aed 1200 msg = bt_message_event_create_with_packet_and_default_clock_snapshot(
2c091c04
PP
1201 notit->msg_iter, notit->meta.ec->ir_ec,
1202 notit->packet, notit->default_clock_snapshot);
1203 } else {
26fc5aed 1204 msg = bt_message_event_create_with_packet(notit->msg_iter,
2c091c04
PP
1205 notit->meta.ec->ir_ec, notit->packet);
1206 }
1207
d6e69534 1208 if (!msg) {
eb7f6c4f 1209 BT_COMP_LOGE("Cannot create event message: "
312c056a
PP
1210 "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", "
1211 "packet-addr=%p",
44c440bc
PP
1212 notit, notit->meta.ec,
1213 notit->meta.ec->name->str,
312c056a
PP
1214 notit->packet);
1215 goto error;
1216 }
1217
1218 goto end;
1219
1220error:
d6e69534
PP
1221 BT_MESSAGE_PUT_REF_AND_RESET(msg);
1222 status = BT_MSG_ITER_STATUS_ERROR;
312c056a
PP
1223
1224end:
d6e69534 1225 BT_MESSAGE_MOVE_REF(notit->event_msg, msg);
312c056a
PP
1226 return status;
1227}
1228
e98a2d6e 1229static
d6e69534
PP
1230enum bt_msg_iter_status after_event_header_state(
1231 struct bt_msg_iter *notit)
e98a2d6e 1232{
d6e69534 1233 enum bt_msg_iter_status status;
e98a2d6e 1234
e98a2d6e 1235 status = set_current_event_class(notit);
d6e69534 1236 if (status != BT_MSG_ITER_STATUS_OK) {
e98a2d6e
PP
1237 goto end;
1238 }
1239
d6e69534
PP
1240 status = set_current_event_message(notit);
1241 if (status != BT_MSG_ITER_STATUS_OK) {
312c056a
PP
1242 goto end;
1243 }
1244
d6e69534
PP
1245 notit->event = bt_message_event_borrow_event(
1246 notit->event_msg);
312c056a 1247 BT_ASSERT(notit->event);
44c440bc 1248 notit->state = STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN;
e98a2d6e
PP
1249
1250end:
1251 return status;
1252}
1253
1254static
d6e69534
PP
1255enum bt_msg_iter_status read_event_common_context_begin_state(
1256 struct bt_msg_iter *notit)
e98a2d6e 1257{
d6e69534 1258 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
5cd6d0e5 1259 struct ctf_field_class *event_common_context_fc;
e98a2d6e 1260
5cd6d0e5
PP
1261 event_common_context_fc = notit->meta.sc->event_common_context_fc;
1262 if (!event_common_context_fc) {
44c440bc 1263 notit->state = STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN;
e98a2d6e
PP
1264 goto end;
1265 }
1266
5cd6d0e5 1267 if (event_common_context_fc->in_ir) {
44c440bc
PP
1268 BT_ASSERT(!notit->dscopes.event_common_context);
1269 notit->dscopes.event_common_context =
40f4ba76 1270 bt_event_borrow_common_context_field(
e5be10ef 1271 notit->event);
44c440bc
PP
1272 BT_ASSERT(notit->dscopes.event_common_context);
1273 }
1274
ef267d12 1275 BT_COMP_LOGT("Decoding event common context field: "
fdf0e7a0 1276 "notit-addr=%p, stream-class-addr=%p, "
44c440bc 1277 "stream-class-id=%" PRId64 ", "
5cd6d0e5 1278 "fc-addr=%p",
44c440bc
PP
1279 notit, notit->meta.sc,
1280 notit->meta.sc->id,
5cd6d0e5
PP
1281 event_common_context_fc);
1282 status = read_dscope_begin_state(notit, event_common_context_fc,
44c440bc
PP
1283 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN,
1284 STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE,
1285 notit->dscopes.event_common_context);
fdf0e7a0 1286 if (status < 0) {
eb7f6c4f 1287 BT_COMP_LOGW("Cannot decode event common context field: "
fdf0e7a0 1288 "notit-addr=%p, stream-class-addr=%p, "
5cd6d0e5 1289 "stream-class-id=%" PRId64 ", fc-addr=%p",
44c440bc
PP
1290 notit, notit->meta.sc,
1291 notit->meta.sc->id,
5cd6d0e5 1292 event_common_context_fc);
fdf0e7a0 1293 }
e98a2d6e
PP
1294
1295end:
e98a2d6e
PP
1296 return status;
1297}
1298
1299static
d6e69534
PP
1300enum bt_msg_iter_status read_event_common_context_continue_state(
1301 struct bt_msg_iter *notit)
e98a2d6e
PP
1302{
1303 return read_dscope_continue_state(notit,
44c440bc 1304 STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN);
e98a2d6e
PP
1305}
1306
1307static
d6e69534
PP
1308enum bt_msg_iter_status read_event_spec_context_begin_state(
1309 struct bt_msg_iter *notit)
e98a2d6e 1310{
d6e69534 1311 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
5cd6d0e5 1312 struct ctf_field_class *event_spec_context_fc;
e98a2d6e 1313
5cd6d0e5
PP
1314 event_spec_context_fc = notit->meta.ec->spec_context_fc;
1315 if (!event_spec_context_fc) {
835b2d10 1316 notit->state = STATE_DSCOPE_EVENT_PAYLOAD_BEGIN;
e98a2d6e
PP
1317 goto end;
1318 }
fdf0e7a0 1319
5cd6d0e5 1320 if (event_spec_context_fc->in_ir) {
44c440bc 1321 BT_ASSERT(!notit->dscopes.event_spec_context);
e5be10ef 1322 notit->dscopes.event_spec_context =
40f4ba76 1323 bt_event_borrow_specific_context_field(
e5be10ef 1324 notit->event);
44c440bc
PP
1325 BT_ASSERT(notit->dscopes.event_spec_context);
1326 }
1327
ef267d12 1328 BT_COMP_LOGT("Decoding event specific context field: "
fdf0e7a0
PP
1329 "notit-addr=%p, event-class-addr=%p, "
1330 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
5cd6d0e5 1331 "fc-addr=%p",
44c440bc
PP
1332 notit, notit->meta.ec,
1333 notit->meta.ec->name->str,
1334 notit->meta.ec->id,
5cd6d0e5
PP
1335 event_spec_context_fc);
1336 status = read_dscope_begin_state(notit, event_spec_context_fc,
e98a2d6e 1337 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN,
44c440bc
PP
1338 STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE,
1339 notit->dscopes.event_spec_context);
fdf0e7a0 1340 if (status < 0) {
eb7f6c4f 1341 BT_COMP_LOGW("Cannot decode event specific context field: "
fdf0e7a0
PP
1342 "notit-addr=%p, event-class-addr=%p, "
1343 "event-class-name=\"%s\", "
5cd6d0e5 1344 "event-class-id=%" PRId64 ", fc-addr=%p",
44c440bc
PP
1345 notit, notit->meta.ec,
1346 notit->meta.ec->name->str,
1347 notit->meta.ec->id,
5cd6d0e5 1348 event_spec_context_fc);
fdf0e7a0 1349 }
e98a2d6e
PP
1350
1351end:
e98a2d6e
PP
1352 return status;
1353}
1354
1355static
d6e69534
PP
1356enum bt_msg_iter_status read_event_spec_context_continue_state(
1357 struct bt_msg_iter *notit)
e98a2d6e
PP
1358{
1359 return read_dscope_continue_state(notit,
1360 STATE_DSCOPE_EVENT_PAYLOAD_BEGIN);
1361}
1362
1363static
d6e69534
PP
1364enum bt_msg_iter_status read_event_payload_begin_state(
1365 struct bt_msg_iter *notit)
e98a2d6e 1366{
d6e69534 1367 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
5cd6d0e5 1368 struct ctf_field_class *event_payload_fc;
e98a2d6e 1369
5cd6d0e5
PP
1370 event_payload_fc = notit->meta.ec->payload_fc;
1371 if (!event_payload_fc) {
d6e69534 1372 notit->state = STATE_EMIT_MSG_EVENT;
e98a2d6e
PP
1373 goto end;
1374 }
1375
5cd6d0e5 1376 if (event_payload_fc->in_ir) {
44c440bc 1377 BT_ASSERT(!notit->dscopes.event_payload);
e5be10ef 1378 notit->dscopes.event_payload =
40f4ba76 1379 bt_event_borrow_payload_field(
e5be10ef 1380 notit->event);
44c440bc
PP
1381 BT_ASSERT(notit->dscopes.event_payload);
1382 }
1383
ef267d12 1384 BT_COMP_LOGT("Decoding event payload field: "
fdf0e7a0
PP
1385 "notit-addr=%p, event-class-addr=%p, "
1386 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
5cd6d0e5 1387 "fc-addr=%p",
44c440bc
PP
1388 notit, notit->meta.ec,
1389 notit->meta.ec->name->str,
1390 notit->meta.ec->id,
5cd6d0e5
PP
1391 event_payload_fc);
1392 status = read_dscope_begin_state(notit, event_payload_fc,
d6e69534 1393 STATE_EMIT_MSG_EVENT,
e98a2d6e 1394 STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE,
312c056a 1395 notit->dscopes.event_payload);
fdf0e7a0 1396 if (status < 0) {
eb7f6c4f 1397 BT_COMP_LOGW("Cannot decode event payload field: "
fdf0e7a0
PP
1398 "notit-addr=%p, event-class-addr=%p, "
1399 "event-class-name=\"%s\", "
5cd6d0e5 1400 "event-class-id=%" PRId64 ", fc-addr=%p",
44c440bc
PP
1401 notit, notit->meta.ec,
1402 notit->meta.ec->name->str,
1403 notit->meta.ec->id,
5cd6d0e5 1404 event_payload_fc);
fdf0e7a0 1405 }
e98a2d6e
PP
1406
1407end:
e98a2d6e
PP
1408 return status;
1409}
1410
1411static
d6e69534
PP
1412enum bt_msg_iter_status read_event_payload_continue_state(
1413 struct bt_msg_iter *notit)
e98a2d6e 1414{
d6e69534 1415 return read_dscope_continue_state(notit, STATE_EMIT_MSG_EVENT);
e98a2d6e
PP
1416}
1417
1418static
fc917f65 1419enum bt_msg_iter_status skip_packet_padding_state(struct bt_msg_iter *notit)
e98a2d6e 1420{
d6e69534 1421 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
e98a2d6e
PP
1422 size_t bits_to_skip;
1423
44c440bc
PP
1424 BT_ASSERT(notit->cur_exp_packet_total_size > 0);
1425 bits_to_skip = notit->cur_exp_packet_total_size - packet_at(notit);
e98a2d6e
PP
1426 if (bits_to_skip == 0) {
1427 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1428 goto end;
1429 } else {
1430 size_t bits_to_consume;
fdf0e7a0 1431
eb7f6c4f 1432 BT_COMP_LOGD("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu",
fdf0e7a0 1433 bits_to_skip, notit, bits_to_skip);
e98a2d6e 1434 status = buf_ensure_available_bits(notit);
d6e69534 1435 if (status != BT_MSG_ITER_STATUS_OK) {
e98a2d6e
PP
1436 goto end;
1437 }
1438
1439 bits_to_consume = MIN(buf_available_bits(notit), bits_to_skip);
eb7f6c4f 1440 BT_COMP_LOGD("Skipping %zu bits of padding: notit-addr=%p, size=%zu",
fdf0e7a0 1441 bits_to_consume, notit, bits_to_consume);
e98a2d6e 1442 buf_consume_bits(notit, bits_to_consume);
44c440bc
PP
1443 bits_to_skip = notit->cur_exp_packet_total_size -
1444 packet_at(notit);
e98a2d6e
PP
1445 if (bits_to_skip == 0) {
1446 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1447 goto end;
1448 }
1449 }
1450
1451end:
1452 return status;
1453}
1454
495490c5
PP
1455static
1456enum bt_msg_iter_status check_emit_msg_stream_beginning_state(
1457 struct bt_msg_iter *notit)
1458{
1459 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
1460
1461 if (notit->set_stream) {
1462 status = set_current_stream(notit);
1463 if (status != BT_MSG_ITER_STATUS_OK) {
1464 goto end;
1465 }
1466 }
1467
1468 if (notit->emit_stream_begin_msg) {
1469 notit->state = STATE_EMIT_MSG_STREAM_BEGINNING;
1470 } else {
1471 /* Stream's first packet */
1472 notit->state = STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS;
1473 }
1474
1475end:
1476 return status;
1477}
1478
1479static
1480enum bt_msg_iter_status check_emit_msg_discarded_events(
1481 struct bt_msg_iter *notit)
1482{
1483 notit->state = STATE_EMIT_MSG_DISCARDED_EVENTS;
1484
afd45274
PP
1485 if (!notit->meta.sc->has_discarded_events) {
1486 notit->state = STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS;
1487 goto end;
1488 }
1489
495490c5
PP
1490 if (notit->prev_packet_snapshots.discarded_events == UINT64_C(-1)) {
1491 if (notit->snapshots.discarded_events == 0 ||
1492 notit->snapshots.discarded_events == UINT64_C(-1)) {
1493 /*
1494 * Stream's first packet with no discarded
1495 * events or no information about discarded
1496 * events: do not emit.
1497 */
1498 notit->state = STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS;
1499 }
1500 } else {
1501 /*
1502 * If the previous packet has a value for this counter,
1503 * then this counter is defined for the whole stream.
1504 */
1505 BT_ASSERT(notit->snapshots.discarded_events != UINT64_C(-1));
1506
1507 if (notit->snapshots.discarded_events -
1508 notit->prev_packet_snapshots.discarded_events == 0) {
1509 /*
1510 * No discarded events since previous packet: do
1511 * not emit.
1512 */
1513 notit->state = STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS;
1514 }
1515 }
1516
afd45274 1517end:
495490c5
PP
1518 return BT_MSG_ITER_STATUS_OK;
1519}
1520
1521static
1522enum bt_msg_iter_status check_emit_msg_discarded_packets(
1523 struct bt_msg_iter *notit)
1524{
1525 notit->state = STATE_EMIT_MSG_DISCARDED_PACKETS;
1526
afd45274
PP
1527 if (!notit->meta.sc->has_discarded_packets) {
1528 notit->state = STATE_EMIT_MSG_PACKET_BEGINNING;
1529 goto end;
1530 }
1531
495490c5
PP
1532 if (notit->prev_packet_snapshots.packets == UINT64_C(-1)) {
1533 /*
1534 * Stream's first packet or no information about
1535 * discarded packets: do not emit. In other words, if
1536 * this is the first packet and its sequence number is
1537 * not 0, do not consider that packets were previously
1538 * lost: we might be reading a partial stream (LTTng
1539 * snapshot for example).
1540 */
1541 notit->state = STATE_EMIT_MSG_PACKET_BEGINNING;
1542 } else {
1543 /*
1544 * If the previous packet has a value for this counter,
1545 * then this counter is defined for the whole stream.
1546 */
1547 BT_ASSERT(notit->snapshots.packets != UINT64_C(-1));
1548
1549 if (notit->snapshots.packets -
1550 notit->prev_packet_snapshots.packets <= 1) {
1551 /*
1552 * No discarded packets since previous packet:
1553 * do not emit.
1554 */
1555 notit->state = STATE_EMIT_MSG_PACKET_BEGINNING;
1556 }
1557 }
1558
afd45274 1559end:
495490c5
PP
1560 return BT_MSG_ITER_STATUS_OK;
1561}
1562
1563static
188edac1 1564enum bt_msg_iter_status check_emit_msg_stream_end(
495490c5
PP
1565 struct bt_msg_iter *notit)
1566{
1567 if (notit->emit_stream_end_msg) {
188edac1 1568 notit->state = STATE_EMIT_MSG_STREAM_END;
495490c5
PP
1569 } else {
1570 notit->state = STATE_DONE;
1571 }
1572
1573 return BT_MSG_ITER_STATUS_OK;
1574}
1575
e98a2d6e 1576static inline
d6e69534 1577enum bt_msg_iter_status handle_state(struct bt_msg_iter *notit)
e98a2d6e 1578{
d6e69534 1579 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
fdf0e7a0 1580 const enum state state = notit->state;
e98a2d6e 1581
ef267d12 1582 BT_COMP_LOGT("Handling state: notit-addr=%p, state=%s",
fdf0e7a0 1583 notit, state_string(state));
e98a2d6e
PP
1584
1585 // TODO: optimalize!
fdf0e7a0 1586 switch (state) {
e98a2d6e
PP
1587 case STATE_INIT:
1588 notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN;
1589 break;
1590 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
1591 status = read_packet_header_begin_state(notit);
1592 break;
1593 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
1594 status = read_packet_header_continue_state(notit);
1595 break;
1596 case STATE_AFTER_TRACE_PACKET_HEADER:
1597 status = after_packet_header_state(notit);
1598 break;
1599 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN:
1600 status = read_packet_context_begin_state(notit);
1601 break;
1602 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE:
1603 status = read_packet_context_continue_state(notit);
1604 break;
1605 case STATE_AFTER_STREAM_PACKET_CONTEXT:
1606 status = after_packet_context_state(notit);
1607 break;
495490c5
PP
1608 case STATE_CHECK_EMIT_MSG_STREAM_BEGINNING:
1609 status = check_emit_msg_stream_beginning_state(notit);
1610 break;
fc917f65 1611 case STATE_EMIT_MSG_STREAM_BEGINNING:
495490c5
PP
1612 notit->state = STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS;
1613 break;
1614 case STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS:
1615 status = check_emit_msg_discarded_events(notit);
1616 break;
1617 case STATE_EMIT_MSG_DISCARDED_EVENTS:
1618 notit->state = STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS;
1619 break;
1620 case STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS:
1621 status = check_emit_msg_discarded_packets(notit);
1622 break;
1623 case STATE_EMIT_MSG_DISCARDED_PACKETS:
fc917f65 1624 notit->state = STATE_EMIT_MSG_PACKET_BEGINNING;
f42867e2 1625 break;
fc917f65 1626 case STATE_EMIT_MSG_PACKET_BEGINNING:
44c440bc 1627 notit->state = STATE_DSCOPE_EVENT_HEADER_BEGIN;
e98a2d6e 1628 break;
44c440bc 1629 case STATE_DSCOPE_EVENT_HEADER_BEGIN:
e98a2d6e
PP
1630 status = read_event_header_begin_state(notit);
1631 break;
44c440bc 1632 case STATE_DSCOPE_EVENT_HEADER_CONTINUE:
e98a2d6e
PP
1633 status = read_event_header_continue_state(notit);
1634 break;
44c440bc 1635 case STATE_AFTER_EVENT_HEADER:
e98a2d6e
PP
1636 status = after_event_header_state(notit);
1637 break;
44c440bc
PP
1638 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN:
1639 status = read_event_common_context_begin_state(notit);
e98a2d6e 1640 break;
44c440bc
PP
1641 case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE:
1642 status = read_event_common_context_continue_state(notit);
e98a2d6e 1643 break;
44c440bc
PP
1644 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN:
1645 status = read_event_spec_context_begin_state(notit);
e98a2d6e 1646 break;
44c440bc
PP
1647 case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE:
1648 status = read_event_spec_context_continue_state(notit);
e98a2d6e
PP
1649 break;
1650 case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN:
1651 status = read_event_payload_begin_state(notit);
1652 break;
1653 case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE:
1654 status = read_event_payload_continue_state(notit);
1655 break;
d6e69534 1656 case STATE_EMIT_MSG_EVENT:
44c440bc 1657 notit->state = STATE_DSCOPE_EVENT_HEADER_BEGIN;
e98a2d6e
PP
1658 break;
1659 case STATE_SKIP_PACKET_PADDING:
1660 status = skip_packet_padding_state(notit);
1661 break;
fc917f65 1662 case STATE_EMIT_MSG_PACKET_END_MULTI:
e98a2d6e
PP
1663 notit->state = STATE_SKIP_PACKET_PADDING;
1664 break;
fc917f65 1665 case STATE_EMIT_MSG_PACKET_END_SINGLE:
188edac1 1666 notit->state = STATE_CHECK_EMIT_MSG_STREAM_END;
495490c5 1667 break;
188edac1
SM
1668 case STATE_CHECK_EMIT_MSG_STREAM_END:
1669 status = check_emit_msg_stream_end(notit);
fc917f65
PP
1670 break;
1671 case STATE_EMIT_MSG_STREAM_END:
1672 notit->state = STATE_DONE;
1673 break;
1674 case STATE_DONE:
1675 break;
fdf0e7a0 1676 default:
eb7f6c4f 1677 BT_COMP_LOGF("Unknown CTF plugin message iterator state: "
fdf0e7a0
PP
1678 "notit-addr=%p, state=%d", notit, notit->state);
1679 abort();
e98a2d6e
PP
1680 }
1681
ef267d12 1682 BT_COMP_LOGT("Handled state: notit-addr=%p, status=%s, "
fdf0e7a0 1683 "prev-state=%s, cur-state=%s",
d6e69534 1684 notit, bt_msg_iter_status_string(status),
fdf0e7a0 1685 state_string(state), state_string(notit->state));
e98a2d6e
PP
1686 return status;
1687}
1688
f42867e2 1689BT_HIDDEN
495490c5 1690void bt_msg_iter_reset_for_next_stream_file(struct bt_msg_iter *notit)
e98a2d6e 1691{
f6ccaed9 1692 BT_ASSERT(notit);
eb7f6c4f 1693 BT_COMP_LOGD("Resetting message iterator: addr=%p", notit);
e98a2d6e 1694 stack_clear(notit->stack);
44c440bc
PP
1695 notit->meta.sc = NULL;
1696 notit->meta.ec = NULL;
c5b9b441
PP
1697 BT_PACKET_PUT_REF_AND_RESET(notit->packet);
1698 BT_STREAM_PUT_REF_AND_RESET(notit->stream);
d6e69534 1699 BT_MESSAGE_PUT_REF_AND_RESET(notit->event_msg);
312c056a
PP
1700 release_all_dscopes(notit);
1701 notit->cur_dscope_field = NULL;
1702
312c056a 1703 if (notit->packet_context_field) {
40f4ba76 1704 bt_packet_context_field_release(notit->packet_context_field);
312c056a
PP
1705 notit->packet_context_field = NULL;
1706 }
1707
e98a2d6e
PP
1708 notit->buf.addr = NULL;
1709 notit->buf.sz = 0;
1710 notit->buf.at = 0;
2b186c3e 1711 notit->buf.last_eh_at = SIZE_MAX;
e98a2d6e
PP
1712 notit->buf.packet_offset = 0;
1713 notit->state = STATE_INIT;
44c440bc
PP
1714 notit->cur_exp_packet_content_size = -1;
1715 notit->cur_exp_packet_total_size = -1;
9e0c8dbb 1716 notit->cur_packet_offset = -1;
44c440bc 1717 notit->cur_event_class_id = -1;
495490c5
PP
1718 notit->snapshots.beginning_clock = UINT64_C(-1);
1719 notit->snapshots.end_clock = UINT64_C(-1);
1720}
1721
1722/**
1723 * Resets the internal state of a CTF message iterator.
1724 */
1725BT_HIDDEN
1726void bt_msg_iter_reset(struct bt_msg_iter *notit)
1727{
1728 bt_msg_iter_reset_for_next_stream_file(notit);
1729 notit->cur_stream_class_id = -1;
44c440bc 1730 notit->cur_data_stream_id = -1;
fc917f65
PP
1731 notit->emit_stream_begin_msg = true;
1732 notit->emit_stream_end_msg = true;
495490c5
PP
1733 notit->snapshots.discarded_events = UINT64_C(-1);
1734 notit->snapshots.packets = UINT64_C(-1);
1735 notit->prev_packet_snapshots.discarded_events = UINT64_C(-1);
1736 notit->prev_packet_snapshots.packets = UINT64_C(-1);
1737 notit->prev_packet_snapshots.beginning_clock = UINT64_C(-1);
1738 notit->prev_packet_snapshots.end_clock = UINT64_C(-1);
e98a2d6e
PP
1739}
1740
2cf1d51e 1741static
d6e69534 1742int bt_msg_iter_switch_packet(struct bt_msg_iter *notit)
2cf1d51e
JG
1743{
1744 int ret = 0;
1745
115de887
PP
1746 /*
1747 * We don't put the stream class here because we need to make
d6e69534 1748 * sure that all the packets processed by the same message
115de887
PP
1749 * iterator refer to the same stream class (the first one).
1750 */
f6ccaed9 1751 BT_ASSERT(notit);
312c056a 1752
44c440bc
PP
1753 if (notit->cur_exp_packet_total_size != -1) {
1754 notit->cur_packet_offset += notit->cur_exp_packet_total_size;
9e0c8dbb 1755 }
312c056a 1756
eb7f6c4f 1757 BT_COMP_LOGD("Switching packet: notit-addr=%p, cur=%zu, "
9e0c8dbb
JG
1758 "packet-offset=%" PRId64, notit, notit->buf.at,
1759 notit->cur_packet_offset);
2cf1d51e 1760 stack_clear(notit->stack);
44c440bc 1761 notit->meta.ec = NULL;
c5b9b441 1762 BT_PACKET_PUT_REF_AND_RESET(notit->packet);
d6e69534 1763 BT_MESSAGE_PUT_REF_AND_RESET(notit->event_msg);
312c056a
PP
1764 release_all_dscopes(notit);
1765 notit->cur_dscope_field = NULL;
2cf1d51e
JG
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) {
eb7f6c4f 1776 BT_COMP_LOGW("Cannot switch packet: current position is not a multiple of 8: "
fdf0e7a0 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;
eb7f6c4f 1786 BT_COMP_LOGD("Adjusted buffer: addr=%p, size=%zu",
fdf0e7a0 1787 notit->buf.addr, notit->buf.sz);
2cf1d51e
JG
1788 }
1789
44c440bc
PP
1790 notit->cur_exp_packet_content_size = -1;
1791 notit->cur_exp_packet_total_size = -1;
1792 notit->cur_stream_class_id = -1;
1793 notit->cur_event_class_id = -1;
1794 notit->cur_data_stream_id = -1;
495490c5 1795 notit->prev_packet_snapshots = notit->snapshots;
44c440bc
PP
1796 notit->snapshots.discarded_events = UINT64_C(-1);
1797 notit->snapshots.packets = UINT64_C(-1);
1798 notit->snapshots.beginning_clock = UINT64_C(-1);
1799 notit->snapshots.end_clock = UINT64_C(-1);
fdf0e7a0 1800
2cf1d51e
JG
1801end:
1802 return ret;
1803}
1804
e98a2d6e 1805static
d6e69534 1806bt_field *borrow_next_field(struct bt_msg_iter *notit)
e98a2d6e 1807{
b19ff26f
PP
1808 bt_field *next_field = NULL;
1809 bt_field *base_field;
1810 const bt_field_class *base_fc;
e98a2d6e
PP
1811 size_t index;
1812
f6ccaed9 1813 BT_ASSERT(!stack_empty(notit->stack));
e98a2d6e
PP
1814 index = stack_top(notit->stack)->index;
1815 base_field = stack_top(notit->stack)->base;
f6ccaed9 1816 BT_ASSERT(base_field);
40f4ba76 1817 base_fc = bt_field_borrow_class_const(base_field);
5cd6d0e5 1818 BT_ASSERT(base_fc);
e98a2d6e 1819
40f4ba76 1820 switch (bt_field_class_get_type(base_fc)) {
864cad70 1821 case BT_FIELD_CLASS_TYPE_STRUCTURE:
f42867e2 1822 {
44c440bc 1823 BT_ASSERT(index <
5cd6d0e5 1824 bt_field_class_structure_get_member_count(
40f4ba76
PP
1825 bt_field_borrow_class_const(
1826 base_field)));
e5be10ef 1827 next_field =
40f4ba76 1828 bt_field_structure_borrow_member_field_by_index(
e5be10ef 1829 base_field, index);
e98a2d6e 1830 break;
f42867e2 1831 }
864cad70
PP
1832 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
1833 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
40f4ba76
PP
1834 BT_ASSERT(index < bt_field_array_get_length(base_field));
1835 next_field = bt_field_array_borrow_element_field_by_index(
44c440bc 1836 base_field, index);
e98a2d6e 1837 break;
864cad70 1838 case BT_FIELD_CLASS_TYPE_VARIANT:
44c440bc 1839 BT_ASSERT(index == 0);
40f4ba76 1840 next_field = bt_field_variant_borrow_selected_option_field(
44c440bc 1841 base_field);
e98a2d6e
PP
1842 break;
1843 default:
0fbb9a9f 1844 abort();
e98a2d6e
PP
1845 }
1846
44c440bc 1847 BT_ASSERT(next_field);
e98a2d6e
PP
1848 return next_field;
1849}
1850
c44c3e70 1851static
d6e69534 1852void update_default_clock(struct bt_msg_iter *notit, uint64_t new_val,
44c440bc 1853 uint64_t new_val_size)
c44c3e70 1854{
44c440bc 1855 uint64_t new_val_mask;
c44c3e70 1856 uint64_t cur_value_masked;
c44c3e70 1857
44c440bc 1858 BT_ASSERT(new_val_size > 0);
c44c3e70
JG
1859
1860 /*
1861 * Special case for a 64-bit new value, which is the limit
1862 * of a clock value as of this version: overwrite the
1863 * current value directly.
1864 */
44c440bc 1865 if (new_val_size == 64) {
605e1019 1866 notit->default_clock_snapshot = new_val;
c44c3e70
JG
1867 goto end;
1868 }
1869
44c440bc 1870 new_val_mask = (1ULL << new_val_size) - 1;
605e1019 1871 cur_value_masked = notit->default_clock_snapshot & new_val_mask;
c44c3e70 1872
44c440bc 1873 if (new_val < cur_value_masked) {
c44c3e70
JG
1874 /*
1875 * It looks like a wrap happened on the number of bits
1876 * of the requested new value. Assume that the clock
1877 * value wrapped only one time.
1878 */
605e1019 1879 notit->default_clock_snapshot += new_val_mask + 1;
c44c3e70
JG
1880 }
1881
1882 /* Clear the low bits of the current clock value. */
605e1019 1883 notit->default_clock_snapshot &= ~new_val_mask;
c44c3e70
JG
1884
1885 /* Set the low bits of the current clock value. */
605e1019 1886 notit->default_clock_snapshot |= new_val;
d1e46835 1887
c44c3e70 1888end:
ef267d12 1889 BT_COMP_LOGT("Updated default clock's value from integer field's value: "
605e1019 1890 "value=%" PRIu64, notit->default_clock_snapshot);
c44c3e70
JG
1891}
1892
1893static
5cd6d0e5
PP
1894enum bt_bfcr_status bfcr_unsigned_int_cb(uint64_t value,
1895 struct ctf_field_class *fc, void *data)
c44c3e70 1896{
d6e69534 1897 struct bt_msg_iter *notit = data;
5cd6d0e5 1898 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
b19ff26f 1899 bt_field *field = NULL;
5cd6d0e5 1900 struct ctf_field_class_int *int_fc = (void *) fc;
c44c3e70 1901
ef267d12 1902 BT_COMP_LOGT("Unsigned integer function called from BFCR: "
5cd6d0e5 1903 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
864cad70
PP
1904 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64,
1905 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
312c056a 1906
91d81473 1907 if (G_LIKELY(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE)) {
44c440bc 1908 goto update_def_clock;
c44c3e70
JG
1909 }
1910
5cd6d0e5
PP
1911 switch (int_fc->meaning) {
1912 case CTF_FIELD_CLASS_MEANING_EVENT_CLASS_ID:
44c440bc
PP
1913 notit->cur_event_class_id = value;
1914 break;
5cd6d0e5 1915 case CTF_FIELD_CLASS_MEANING_DATA_STREAM_ID:
44c440bc
PP
1916 notit->cur_data_stream_id = value;
1917 break;
5cd6d0e5 1918 case CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME:
44c440bc
PP
1919 notit->snapshots.beginning_clock = value;
1920 break;
5cd6d0e5 1921 case CTF_FIELD_CLASS_MEANING_PACKET_END_TIME:
44c440bc
PP
1922 notit->snapshots.end_clock = value;
1923 break;
5cd6d0e5 1924 case CTF_FIELD_CLASS_MEANING_STREAM_CLASS_ID:
44c440bc
PP
1925 notit->cur_stream_class_id = value;
1926 break;
5cd6d0e5 1927 case CTF_FIELD_CLASS_MEANING_MAGIC:
44c440bc 1928 if (value != 0xc1fc1fc1) {
eb7f6c4f 1929 BT_COMP_LOGW("Invalid CTF magic number: notit-addr=%p, "
44c440bc 1930 "magic=%" PRIx64, notit, value);
5cd6d0e5 1931 status = BT_BFCR_STATUS_ERROR;
c44c3e70
JG
1932 goto end;
1933 }
fdf0e7a0 1934
44c440bc 1935 break;
5cd6d0e5 1936 case CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT:
44c440bc
PP
1937 notit->snapshots.packets = value;
1938 break;
5cd6d0e5 1939 case CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT:
44c440bc
PP
1940 notit->snapshots.discarded_events = value;
1941 break;
5cd6d0e5 1942 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_TOTAL_SIZE:
44c440bc
PP
1943 notit->cur_exp_packet_total_size = value;
1944 break;
5cd6d0e5 1945 case CTF_FIELD_CLASS_MEANING_EXP_PACKET_CONTENT_SIZE:
44c440bc
PP
1946 notit->cur_exp_packet_content_size = value;
1947 break;
1948 default:
1949 abort();
c44c3e70
JG
1950 }
1951
44c440bc 1952update_def_clock:
91d81473 1953 if (G_UNLIKELY(int_fc->mapped_clock_class)) {
5cd6d0e5 1954 update_default_clock(notit, value, int_fc->base.size);
44c440bc 1955 }
c44c3e70 1956
91d81473 1957 if (G_UNLIKELY(int_fc->storing_index >= 0)) {
44c440bc 1958 g_array_index(notit->stored_values, uint64_t,
5cd6d0e5 1959 (uint64_t) int_fc->storing_index) = value;
44c440bc 1960 }
e98a2d6e 1961
91d81473 1962 if (G_UNLIKELY(!fc->in_ir)) {
312c056a 1963 goto end;
e98a2d6e
PP
1964 }
1965
44c440bc
PP
1966 field = borrow_next_field(notit);
1967 BT_ASSERT(field);
40f4ba76
PP
1968 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
1969 BT_ASSERT(bt_field_get_class_type(field) ==
1970 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER ||
1971 bt_field_get_class_type(field) ==
1972 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION);
1973 bt_field_unsigned_integer_set_value(field, value);
e98a2d6e 1974 stack_top(notit->stack)->index++;
fdf0e7a0 1975
312c056a 1976end:
e98a2d6e
PP
1977 return status;
1978}
1979
5f870343 1980static
5cd6d0e5
PP
1981enum bt_bfcr_status bfcr_unsigned_int_char_cb(uint64_t value,
1982 struct ctf_field_class *fc, void *data)
5f870343 1983{
44c440bc 1984 int ret;
d6e69534 1985 struct bt_msg_iter *notit = data;
5cd6d0e5 1986 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
b19ff26f 1987 bt_field *string_field = NULL;
5cd6d0e5 1988 struct ctf_field_class_int *int_fc = (void *) fc;
44c440bc 1989 char str[2] = {'\0', '\0'};
5f870343 1990
ef267d12 1991 BT_COMP_LOGT("Unsigned integer character function called from BFCR: "
5cd6d0e5 1992 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
864cad70
PP
1993 "fc-type=%d, fc-in-ir=%d, value=%" PRIu64,
1994 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
5cd6d0e5
PP
1995 BT_ASSERT(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE);
1996 BT_ASSERT(!int_fc->mapped_clock_class);
1997 BT_ASSERT(int_fc->storing_index < 0);
312c056a 1998
91d81473 1999 if (G_UNLIKELY(!fc->in_ir)) {
44c440bc
PP
2000 goto end;
2001 }
5f870343 2002
44c440bc 2003 if (notit->done_filling_string) {
5f870343
JG
2004 goto end;
2005 }
2006
44c440bc
PP
2007 if (value == 0) {
2008 notit->done_filling_string = true;
5f870343
JG
2009 goto end;
2010 }
2011
44c440bc 2012 string_field = stack_top(notit->stack)->base;
40f4ba76
PP
2013 BT_ASSERT(bt_field_get_class_type(string_field) ==
2014 BT_FIELD_CLASS_TYPE_STRING);
44c440bc
PP
2015
2016 /* Append character */
2017 str[0] = (char) value;
40f4ba76 2018 ret = bt_field_string_append_with_length(string_field, str, 1);
44c440bc 2019 if (ret) {
eb7f6c4f 2020 BT_COMP_LOGE("Cannot append character to string field's value: "
44c440bc
PP
2021 "notit-addr=%p, field-addr=%p, ret=%d",
2022 notit, string_field, ret);
5cd6d0e5 2023 status = BT_BFCR_STATUS_ERROR;
44c440bc
PP
2024 goto end;
2025 }
312c056a 2026
5f870343
JG
2027end:
2028 return status;
2029}
2030
2031static
5cd6d0e5
PP
2032enum bt_bfcr_status bfcr_signed_int_cb(int64_t value,
2033 struct ctf_field_class *fc, void *data)
e98a2d6e 2034{
5cd6d0e5 2035 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
b19ff26f 2036 bt_field *field = NULL;
d6e69534 2037 struct bt_msg_iter *notit = data;
5cd6d0e5 2038 struct ctf_field_class_int *int_fc = (void *) fc;
e98a2d6e 2039
ef267d12 2040 BT_COMP_LOGT("Signed integer function called from BFCR: "
5cd6d0e5 2041 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
864cad70
PP
2042 "fc-type=%d, fc-in-ir=%d, value=%" PRId64,
2043 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
5cd6d0e5 2044 BT_ASSERT(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE);
44c440bc 2045
91d81473 2046 if (G_UNLIKELY(int_fc->storing_index >= 0)) {
44c440bc 2047 g_array_index(notit->stored_values, uint64_t,
5cd6d0e5 2048 (uint64_t) int_fc->storing_index) = (uint64_t) value;
44c440bc
PP
2049 }
2050
91d81473 2051 if (G_UNLIKELY(!fc->in_ir)) {
312c056a 2052 goto end;
e98a2d6e
PP
2053 }
2054
44c440bc
PP
2055 field = borrow_next_field(notit);
2056 BT_ASSERT(field);
40f4ba76
PP
2057 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
2058 BT_ASSERT(bt_field_get_class_type(field) ==
2059 BT_FIELD_CLASS_TYPE_SIGNED_INTEGER ||
2060 bt_field_get_class_type(field) ==
2061 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION);
2062 bt_field_signed_integer_set_value(field, value);
e98a2d6e 2063 stack_top(notit->stack)->index++;
fdf0e7a0 2064
312c056a 2065end:
e98a2d6e
PP
2066 return status;
2067}
2068
2069static
5cd6d0e5
PP
2070enum bt_bfcr_status bfcr_floating_point_cb(double value,
2071 struct ctf_field_class *fc, void *data)
e98a2d6e 2072{
5cd6d0e5 2073 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
b19ff26f 2074 bt_field *field = NULL;
d6e69534 2075 struct bt_msg_iter *notit = data;
e98a2d6e 2076
ef267d12 2077 BT_COMP_LOGT("Floating point number function called from BFCR: "
5cd6d0e5 2078 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
864cad70
PP
2079 "fc-type=%d, fc-in-ir=%d, value=%f",
2080 notit, notit->bfcr, fc, fc->type, fc->in_ir, value);
41693723 2081
91d81473 2082 if (G_UNLIKELY(!fc->in_ir)) {
41693723
PP
2083 goto end;
2084 }
2085
312c056a 2086 field = borrow_next_field(notit);
44c440bc 2087 BT_ASSERT(field);
40f4ba76
PP
2088 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
2089 BT_ASSERT(bt_field_get_class_type(field) ==
2090 BT_FIELD_CLASS_TYPE_REAL);
2091 bt_field_real_set_value(field, value);
e98a2d6e 2092 stack_top(notit->stack)->index++;
41693723
PP
2093
2094end:
e98a2d6e
PP
2095 return status;
2096}
2097
2098static
5cd6d0e5
PP
2099enum bt_bfcr_status bfcr_string_begin_cb(
2100 struct ctf_field_class *fc, void *data)
e98a2d6e 2101{
b19ff26f 2102 bt_field *field = NULL;
d6e69534 2103 struct bt_msg_iter *notit = data;
e98a2d6e 2104
ef267d12 2105 BT_COMP_LOGT("String (beginning) function called from BFCR: "
5cd6d0e5 2106 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
864cad70
PP
2107 "fc-type=%d, fc-in-ir=%d",
2108 notit, notit->bfcr, fc, fc->type, fc->in_ir);
e98a2d6e 2109
91d81473 2110 if (G_UNLIKELY(!fc->in_ir)) {
41693723
PP
2111 goto end;
2112 }
2113
44c440bc
PP
2114 field = borrow_next_field(notit);
2115 BT_ASSERT(field);
40f4ba76
PP
2116 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
2117 BT_ASSERT(bt_field_get_class_type(field) ==
2118 BT_FIELD_CLASS_TYPE_STRING);
d24d5663 2119 bt_field_string_clear(field);
312c056a 2120
e98a2d6e 2121 /*
5cd6d0e5
PP
2122 * Push on stack. Not a compound class per se, but we know that
2123 * only bfcr_string_cb() may be called between this call and a
2124 * subsequent call to bfcr_string_end_cb().
e98a2d6e 2125 */
44c440bc 2126 stack_push(notit->stack, field);
41693723
PP
2127
2128end:
5cd6d0e5 2129 return BT_BFCR_STATUS_OK;
e98a2d6e
PP
2130}
2131
2132static
5cd6d0e5
PP
2133enum bt_bfcr_status bfcr_string_cb(const char *value,
2134 size_t len, struct ctf_field_class *fc, void *data)
e98a2d6e 2135{
5cd6d0e5 2136 enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
b19ff26f 2137 bt_field *field = NULL;
d6e69534 2138 struct bt_msg_iter *notit = data;
e98a2d6e
PP
2139 int ret;
2140
ef267d12 2141 BT_COMP_LOGT("String (substring) function called from BFCR: "
5cd6d0e5 2142 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
864cad70
PP
2143 "fc-type=%d, fc-in-ir=%d, string-length=%zu",
2144 notit, notit->bfcr, fc, fc->type, fc->in_ir,
fdf0e7a0 2145 len);
41693723 2146
91d81473 2147 if (G_UNLIKELY(!fc->in_ir)) {
41693723
PP
2148 goto end;
2149 }
2150
e98a2d6e 2151 field = stack_top(notit->stack)->base;
f6ccaed9 2152 BT_ASSERT(field);
e98a2d6e 2153
312c056a 2154 /* Append current substring */
40f4ba76 2155 ret = bt_field_string_append_with_length(field, value, len);
e98a2d6e 2156 if (ret) {
eb7f6c4f 2157 BT_COMP_LOGE("Cannot append substring to string field's value: "
fdf0e7a0
PP
2158 "notit-addr=%p, field-addr=%p, string-length=%zu, "
2159 "ret=%d", notit, field, len, ret);
5cd6d0e5 2160 status = BT_BFCR_STATUS_ERROR;
e98a2d6e
PP
2161 goto end;
2162 }
2163
2164end:
2165 return status;
2166}
2167
2168static
5cd6d0e5
PP
2169enum bt_bfcr_status bfcr_string_end_cb(
2170 struct ctf_field_class *fc, void *data)
e98a2d6e 2171{
d6e69534 2172 struct bt_msg_iter *notit = data;
e98a2d6e 2173
ef267d12 2174 BT_COMP_LOGT("String (end) function called from BFCR: "
5cd6d0e5 2175 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
864cad70
PP
2176 "fc-type=%d, fc-in-ir=%d",
2177 notit, notit->bfcr, fc, fc->type, fc->in_ir);
41693723 2178
91d81473 2179 if (G_UNLIKELY(!fc->in_ir)) {
41693723
PP
2180 goto end;
2181 }
fdf0e7a0 2182
e98a2d6e
PP
2183 /* Pop string field */
2184 stack_pop(notit->stack);
2185
2186 /* Go to next field */
2187 stack_top(notit->stack)->index++;
41693723
PP
2188
2189end:
5cd6d0e5 2190 return BT_BFCR_STATUS_OK;
e98a2d6e
PP
2191}
2192
5cd6d0e5
PP
2193enum bt_bfcr_status bfcr_compound_begin_cb(
2194 struct ctf_field_class *fc, void *data)
e98a2d6e 2195{
d6e69534 2196 struct bt_msg_iter *notit = data;
b19ff26f 2197 bt_field *field;
e98a2d6e 2198
ef267d12 2199 BT_COMP_LOGT("Compound (beginning) function called from BFCR: "
5cd6d0e5 2200 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
864cad70
PP
2201 "fc-type=%d, fc-in-ir=%d",
2202 notit, notit->bfcr, fc, fc->type, fc->in_ir);
44c440bc 2203
5cd6d0e5 2204 if (!fc->in_ir) {
44c440bc
PP
2205 goto end;
2206 }
fdf0e7a0 2207
312c056a 2208 /* Borrow field */
e98a2d6e 2209 if (stack_empty(notit->stack)) {
312c056a
PP
2210 /* Root: already set by read_dscope_begin_state() */
2211 field = notit->cur_dscope_field;
e98a2d6e 2212 } else {
312c056a 2213 field = borrow_next_field(notit);
44c440bc 2214 BT_ASSERT(field);
e98a2d6e
PP
2215 }
2216
2217 /* Push field */
f6ccaed9 2218 BT_ASSERT(field);
40f4ba76 2219 BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc);
44c440bc
PP
2220 stack_push(notit->stack, field);
2221
2222 /*
5cd6d0e5 2223 * Change BFCR "unsigned int" callback if it's a text
44c440bc
PP
2224 * array/sequence.
2225 */
864cad70
PP
2226 if (fc->type == CTF_FIELD_CLASS_TYPE_ARRAY ||
2227 fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE) {
5cd6d0e5 2228 struct ctf_field_class_array_base *array_fc = (void *) fc;
44c440bc 2229
5cd6d0e5 2230 if (array_fc->is_text) {
40f4ba76
PP
2231 BT_ASSERT(bt_field_get_class_type(field) ==
2232 BT_FIELD_CLASS_TYPE_STRING);
44c440bc 2233 notit->done_filling_string = false;
d24d5663 2234 bt_field_string_clear(field);
5cd6d0e5
PP
2235 bt_bfcr_set_unsigned_int_cb(notit->bfcr,
2236 bfcr_unsigned_int_char_cb);
44c440bc 2237 }
e98a2d6e
PP
2238 }
2239
2240end:
5cd6d0e5 2241 return BT_BFCR_STATUS_OK;
e98a2d6e
PP
2242}
2243
5cd6d0e5
PP
2244enum bt_bfcr_status bfcr_compound_end_cb(
2245 struct ctf_field_class *fc, void *data)
e98a2d6e 2246{
d6e69534 2247 struct bt_msg_iter *notit = data;
e98a2d6e 2248
ef267d12 2249 BT_COMP_LOGT("Compound (end) function called from BFCR: "
5cd6d0e5 2250 "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, "
864cad70
PP
2251 "fc-type=%d, fc-in-ir=%d",
2252 notit, notit->bfcr, fc, fc->type, fc->in_ir);
e98a2d6e 2253
5cd6d0e5 2254 if (!fc->in_ir) {
e98a2d6e
PP
2255 goto end;
2256 }
2257
44c440bc 2258 BT_ASSERT(!stack_empty(notit->stack));
40f4ba76 2259 BT_ASSERT(bt_field_borrow_class_const(stack_top(notit->stack)->base) ==
5cd6d0e5 2260 fc->ir_fc);
e98a2d6e 2261
44c440bc 2262 /*
5cd6d0e5 2263 * Reset BFCR "unsigned int" callback if it's a text
44c440bc
PP
2264 * array/sequence.
2265 */
864cad70
PP
2266 if (fc->type == CTF_FIELD_CLASS_TYPE_ARRAY ||
2267 fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE) {
5cd6d0e5 2268 struct ctf_field_class_array_base *array_fc = (void *) fc;
44c440bc 2269
5cd6d0e5 2270 if (array_fc->is_text) {
864cad70 2271 BT_ASSERT(bt_field_get_class_type(
40f4ba76 2272 stack_top(notit->stack)->base) ==
e5be10ef 2273 BT_FIELD_CLASS_TYPE_STRING);
5cd6d0e5
PP
2274 bt_bfcr_set_unsigned_int_cb(notit->bfcr,
2275 bfcr_unsigned_int_cb);
e98a2d6e 2276 }
44c440bc 2277 }
e98a2d6e 2278
44c440bc
PP
2279 /* Pop stack */
2280 stack_pop(notit->stack);
e98a2d6e 2281
44c440bc
PP
2282 /* If the stack is not empty, increment the base's index */
2283 if (!stack_empty(notit->stack)) {
2284 stack_top(notit->stack)->index++;
e98a2d6e
PP
2285 }
2286
2287end:
5cd6d0e5 2288 return BT_BFCR_STATUS_OK;
e98a2d6e
PP
2289}
2290
2291static
5cd6d0e5 2292int64_t bfcr_get_sequence_length_cb(struct ctf_field_class *fc, void *data)
e98a2d6e 2293{
b19ff26f 2294 bt_field *seq_field;
d6e69534 2295 struct bt_msg_iter *notit = data;
5cd6d0e5 2296 struct ctf_field_class_sequence *seq_fc = (void *) fc;
44c440bc
PP
2297 int64_t length = -1;
2298 int ret;
e98a2d6e 2299
44c440bc 2300 length = (uint64_t) g_array_index(notit->stored_values, uint64_t,
5cd6d0e5 2301 seq_fc->stored_length_index);
fdf0e7a0 2302 seq_field = stack_top(notit->stack)->base;
44c440bc 2303 BT_ASSERT(seq_field);
785a6bba
PP
2304
2305 /*
2306 * bfcr_get_sequence_length_cb() also gets called back for a
2307 * text sequence, but the destination field is a string field.
2308 * Only set the field's sequence length if the destination field
2309 * is a sequence field.
2310 */
2311 if (!seq_fc->base.is_text) {
2312 BT_ASSERT(bt_field_get_class_type(seq_field) ==
2313 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY);
2314 ret = bt_field_dynamic_array_set_length(seq_field,
2315 (uint64_t) length);
2316 if (ret) {
eb7f6c4f 2317 BT_COMP_LOGE("Cannot set dynamic array field's length field: "
785a6bba
PP
2318 "notit-addr=%p, field-addr=%p, "
2319 "length=%" PRIu64, notit, seq_field, length);
2320 }
2cf1d51e 2321 }
fdf0e7a0 2322
44c440bc 2323 return length;
e98a2d6e
PP
2324}
2325
2326static
5cd6d0e5
PP
2327struct ctf_field_class *bfcr_borrow_variant_selected_field_class_cb(
2328 struct ctf_field_class *fc, void *data)
e98a2d6e 2329{
312c056a 2330 int ret;
44c440bc
PP
2331 uint64_t i;
2332 int64_t option_index = -1;
d6e69534 2333 struct bt_msg_iter *notit = data;
5cd6d0e5
PP
2334 struct ctf_field_class_variant *var_fc = (void *) fc;
2335 struct ctf_named_field_class *selected_option = NULL;
2336 struct ctf_field_class *ret_fc = NULL;
44c440bc
PP
2337 union {
2338 uint64_t u;
2339 int64_t i;
2340 } tag;
2341
2342 /* Get variant's tag */
2343 tag.u = g_array_index(notit->stored_values, uint64_t,
5cd6d0e5 2344 var_fc->stored_tag_index);
e98a2d6e
PP
2345
2346 /*
44c440bc 2347 * Check each range to find the selected option's index.
e98a2d6e 2348 */
5cd6d0e5
PP
2349 if (var_fc->tag_fc->base.is_signed) {
2350 for (i = 0; i < var_fc->ranges->len; i++) {
2351 struct ctf_field_class_variant_range *range =
2352 ctf_field_class_variant_borrow_range_by_index(
2353 var_fc, i);
44c440bc
PP
2354
2355 if (tag.i >= range->range.lower.i &&
2356 tag.i <= range->range.upper.i) {
2357 option_index = (int64_t) range->option_index;
2358 break;
2359 }
2360 }
312c056a 2361 } else {
5cd6d0e5
PP
2362 for (i = 0; i < var_fc->ranges->len; i++) {
2363 struct ctf_field_class_variant_range *range =
2364 ctf_field_class_variant_borrow_range_by_index(
2365 var_fc, i);
44c440bc
PP
2366
2367 if (tag.u >= range->range.lower.u &&
2368 tag.u <= range->range.upper.u) {
2369 option_index = (int64_t) range->option_index;
2370 break;
2371 }
2372 }
312c056a
PP
2373 }
2374
44c440bc 2375 if (option_index < 0) {
eb7f6c4f 2376 BT_COMP_LOGW("Cannot find variant field class's option: "
5cd6d0e5
PP
2377 "notit-addr=%p, var-fc-addr=%p, u-tag=%" PRIu64 ", "
2378 "i-tag=%" PRId64, notit, var_fc, tag.u, tag.i);
e98a2d6e
PP
2379 goto end;
2380 }
2381
5cd6d0e5
PP
2382 selected_option = ctf_field_class_variant_borrow_option_by_index(
2383 var_fc, (uint64_t) option_index);
e98a2d6e 2384
5cd6d0e5 2385 if (selected_option->fc->in_ir) {
b19ff26f 2386 bt_field *var_field = stack_top(notit->stack)->base;
1556a1af 2387
40f4ba76 2388 ret = bt_field_variant_select_option_field(
e5be10ef 2389 var_field, option_index);
e22b45d0 2390 if (ret) {
eb7f6c4f 2391 BT_COMP_LOGW("Cannot select variant field's option field: "
44c440bc
PP
2392 "notit-addr=%p, var-field-addr=%p, "
2393 "opt-index=%" PRId64, notit, var_field,
2394 option_index);
1556a1af
JG
2395 goto end;
2396 }
af87daef
PP
2397 }
2398
5cd6d0e5 2399 ret_fc = selected_option->fc;
e22b45d0 2400
af87daef 2401end:
5cd6d0e5 2402 return ret_fc;
44c440bc
PP
2403}
2404
f42867e2 2405static
fc917f65 2406void create_msg_stream_beginning(struct bt_msg_iter *notit,
d6e69534 2407 bt_message **message)
f42867e2 2408{
d6e69534 2409 bt_message *ret = NULL;
f42867e2 2410
f42867e2 2411 BT_ASSERT(notit->stream);
d6e69534
PP
2412 BT_ASSERT(notit->msg_iter);
2413 ret = bt_message_stream_beginning_create(notit->msg_iter,
f0010051 2414 notit->stream);
f42867e2 2415 if (!ret) {
eb7f6c4f 2416 BT_COMP_LOGE("Cannot create stream beginning message: "
f42867e2
PP
2417 "notit-addr=%p, stream-addr=%p",
2418 notit, notit->stream);
2419 return;
2420 }
2421
d6e69534 2422 *message = ret;
f42867e2
PP
2423}
2424
fc917f65
PP
2425static
2426void create_msg_stream_end(struct bt_msg_iter *notit, bt_message **message)
f42867e2 2427{
d6e69534 2428 bt_message *ret;
f42867e2
PP
2429
2430 if (!notit->stream) {
eb7f6c4f 2431 BT_COMP_LOGE("Cannot create stream for stream message: "
f42867e2
PP
2432 "notit-addr=%p", notit);
2433 return;
2434 }
2435
d6e69534
PP
2436 BT_ASSERT(notit->msg_iter);
2437 ret = bt_message_stream_end_create(notit->msg_iter,
f0010051 2438 notit->stream);
f42867e2 2439 if (!ret) {
eb7f6c4f 2440 BT_COMP_LOGE("Cannot create stream end message: "
f42867e2
PP
2441 "notit-addr=%p, stream-addr=%p",
2442 notit, notit->stream);
2443 return;
2444 }
fc917f65 2445
d6e69534 2446 *message = ret;
f42867e2
PP
2447}
2448
78586d8a 2449static
fc917f65 2450void create_msg_packet_beginning(struct bt_msg_iter *notit,
d6e69534 2451 bt_message **message)
e98a2d6e 2452{
312c056a 2453 int ret;
d6e69534
PP
2454 enum bt_msg_iter_status status;
2455 bt_message *msg = NULL;
b19ff26f 2456 const bt_stream_class *sc;
e98a2d6e 2457
312c056a 2458 status = set_current_packet(notit);
d6e69534 2459 if (status != BT_MSG_ITER_STATUS_OK) {
312c056a 2460 goto end;
e98a2d6e
PP
2461 }
2462
312c056a 2463 BT_ASSERT(notit->packet);
40f4ba76 2464 sc = notit->meta.sc->ir_sc;
44c440bc
PP
2465 BT_ASSERT(sc);
2466
312c056a 2467 if (notit->packet_context_field) {
40f4ba76 2468 ret = bt_packet_move_context_field(
e5be10ef 2469 notit->packet, notit->packet_context_field);
312c056a
PP
2470 if (ret) {
2471 goto end;
2472 }
2473
2474 notit->packet_context_field = NULL;
2475
2476 /*
83ebb7f1
PP
2477 * At this point notit->dscopes.stream_packet_context
2478 * has the same value as the packet context field within
312c056a
PP
2479 * notit->packet.
2480 */
40f4ba76 2481 BT_ASSERT(bt_packet_borrow_context_field(
e5be10ef 2482 notit->packet) ==
312c056a
PP
2483 notit->dscopes.stream_packet_context);
2484 }
2485
d6e69534 2486 BT_ASSERT(notit->msg_iter);
a6d85d2f 2487
afd45274
PP
2488 if (notit->meta.sc->packets_have_ts_begin) {
2489 BT_ASSERT(notit->snapshots.beginning_clock != UINT64_C(-1));
a33f7c34 2490 msg = bt_message_packet_beginning_create_with_default_clock_snapshot(
afd45274
PP
2491 notit->msg_iter, notit->packet,
2492 notit->snapshots.beginning_clock);
a33f7c34 2493 } else {
a6d85d2f
PP
2494 msg = bt_message_packet_beginning_create(notit->msg_iter,
2495 notit->packet);
a6d85d2f
PP
2496 }
2497
d6e69534 2498 if (!msg) {
eb7f6c4f 2499 BT_COMP_LOGE("Cannot create packet beginning message: "
fdf0e7a0
PP
2500 "notit-addr=%p, packet-addr=%p",
2501 notit, notit->packet);
e22b45d0 2502 goto end;
78586d8a 2503 }
312c056a 2504
d6e69534 2505 *message = msg;
e22b45d0
PP
2506
2507end:
2508 return;
e98a2d6e
PP
2509}
2510
78586d8a 2511static
fc917f65 2512void create_msg_packet_end(struct bt_msg_iter *notit, bt_message **message)
e98a2d6e 2513{
d6e69534 2514 bt_message *msg;
e98a2d6e 2515
e98a2d6e 2516 if (!notit->packet) {
78586d8a 2517 return;
e98a2d6e
PP
2518 }
2519
44c440bc
PP
2520 /* Update default clock from packet's end time */
2521 if (notit->snapshots.end_clock != UINT64_C(-1)) {
605e1019 2522 notit->default_clock_snapshot = notit->snapshots.end_clock;
44c440bc
PP
2523 }
2524
d6e69534 2525 BT_ASSERT(notit->msg_iter);
a6d85d2f 2526
afd45274 2527 if (notit->meta.sc->packets_have_ts_end) {
a33f7c34 2528 BT_ASSERT(notit->snapshots.end_clock != UINT64_C(-1));
a6d85d2f
PP
2529 msg = bt_message_packet_end_create_with_default_clock_snapshot(
2530 notit->msg_iter, notit->packet,
2531 notit->snapshots.end_clock);
a33f7c34
PP
2532 } else {
2533 msg = bt_message_packet_end_create(notit->msg_iter,
2534 notit->packet);
a6d85d2f
PP
2535 }
2536
d6e69534 2537 if (!msg) {
eb7f6c4f 2538 BT_COMP_LOGE("Cannot create packet end message: "
fdf0e7a0
PP
2539 "notit-addr=%p, packet-addr=%p",
2540 notit, notit->packet);
78586d8a 2541 return;
ccf82993 2542
78586d8a 2543 }
e98a2d6e 2544
c5b9b441 2545 BT_PACKET_PUT_REF_AND_RESET(notit->packet);
d6e69534 2546 *message = msg;
e98a2d6e
PP
2547}
2548
495490c5
PP
2549static
2550void create_msg_discarded_events(struct bt_msg_iter *notit,
2551 bt_message **message)
2552{
2553 bt_message *msg;
2554 uint64_t beginning_raw_value = UINT64_C(-1);
2555 uint64_t end_raw_value = UINT64_C(-1);
495490c5
PP
2556
2557 BT_ASSERT(notit->msg_iter);
2558 BT_ASSERT(notit->stream);
afd45274 2559 BT_ASSERT(notit->meta.sc->has_discarded_events);
495490c5 2560
afd45274
PP
2561 if (notit->meta.sc->discarded_events_have_default_cs) {
2562 if (notit->prev_packet_snapshots.discarded_events == UINT64_C(-1)) {
2563 /*
2564 * We discarded events, but before (and possibly
2565 * including) the current packet: use this packet's time
2566 * range, and do not have a specific count.
2567 */
2568 beginning_raw_value = notit->snapshots.beginning_clock;
2569 end_raw_value = notit->snapshots.end_clock;
2570 } else {
2571 beginning_raw_value = notit->prev_packet_snapshots.end_clock;
2572 end_raw_value = notit->snapshots.end_clock;
2573 }
495490c5 2574
afd45274
PP
2575 BT_ASSERT(beginning_raw_value != UINT64_C(-1));
2576 BT_ASSERT(end_raw_value != UINT64_C(-1));
495490c5
PP
2577 msg = bt_message_discarded_events_create_with_default_clock_snapshots(
2578 notit->msg_iter, notit->stream, beginning_raw_value,
2579 end_raw_value);
2580 } else {
2581 msg = bt_message_discarded_events_create(notit->msg_iter,
2582 notit->stream);
2583 }
2584
2585 if (!msg) {
eb7f6c4f 2586 BT_COMP_LOGE("Cannot create discarded events message: "
495490c5
PP
2587 "notit-addr=%p, stream-addr=%p",
2588 notit, notit->stream);
2589 return;
2590 }
2591
afd45274
PP
2592 if (notit->prev_packet_snapshots.discarded_events != UINT64_C(-1)) {
2593 bt_message_discarded_events_set_count(msg,
2594 notit->snapshots.discarded_events -
2595 notit->prev_packet_snapshots.discarded_events);
495490c5
PP
2596 }
2597
2598 *message = msg;
2599}
2600
2601static
2602void create_msg_discarded_packets(struct bt_msg_iter *notit,
2603 bt_message **message)
2604{
2605 bt_message *msg;
2606
2607 BT_ASSERT(notit->msg_iter);
2608 BT_ASSERT(notit->stream);
afd45274 2609 BT_ASSERT(notit->meta.sc->has_discarded_packets);
495490c5
PP
2610 BT_ASSERT(notit->prev_packet_snapshots.packets !=
2611 UINT64_C(-1));
2612
afd45274
PP
2613 if (notit->meta.sc->discarded_packets_have_default_cs) {
2614 BT_ASSERT(notit->prev_packet_snapshots.end_clock != UINT64_C(-1));
2615 BT_ASSERT(notit->snapshots.beginning_clock != UINT64_C(-1));
495490c5
PP
2616 msg = bt_message_discarded_packets_create_with_default_clock_snapshots(
2617 notit->msg_iter, notit->stream,
2618 notit->prev_packet_snapshots.end_clock,
2619 notit->snapshots.beginning_clock);
2620 } else {
2621 msg = bt_message_discarded_packets_create(notit->msg_iter,
2622 notit->stream);
2623 }
2624
2625 if (!msg) {
eb7f6c4f 2626 BT_COMP_LOGE("Cannot create discarded packets message: "
495490c5
PP
2627 "notit-addr=%p, stream-addr=%p",
2628 notit, notit->stream);
2629 return;
2630 }
2631
2632 bt_message_discarded_packets_set_count(msg,
2633 notit->snapshots.packets -
2634 notit->prev_packet_snapshots.packets - 1);
2635 *message = msg;
2636}
2637
c44c3e70 2638BT_HIDDEN
d6e69534 2639struct bt_msg_iter *bt_msg_iter_create(struct ctf_trace_class *tc,
e98a2d6e 2640 size_t max_request_sz,
ea14c7dd 2641 struct bt_msg_iter_medium_ops medops, void *data,
eb7f6c4f 2642 bt_logging_level log_level, bt_self_component *self_comp)
e98a2d6e 2643{
d6e69534 2644 struct bt_msg_iter *notit = NULL;
5cd6d0e5
PP
2645 struct bt_bfcr_cbs cbs = {
2646 .classes = {
2647 .signed_int = bfcr_signed_int_cb,
2648 .unsigned_int = bfcr_unsigned_int_cb,
2649 .floating_point = bfcr_floating_point_cb,
2650 .string_begin = bfcr_string_begin_cb,
2651 .string = bfcr_string_cb,
2652 .string_end = bfcr_string_end_cb,
2653 .compound_begin = bfcr_compound_begin_cb,
2654 .compound_end = bfcr_compound_end_cb,
e98a2d6e
PP
2655 },
2656 .query = {
5cd6d0e5
PP
2657 .get_sequence_length = bfcr_get_sequence_length_cb,
2658 .borrow_variant_selected_field_class = bfcr_borrow_variant_selected_field_class_cb,
e98a2d6e
PP
2659 },
2660 };
2661
44c440bc 2662 BT_ASSERT(tc);
f6ccaed9 2663 BT_ASSERT(medops.request_bytes);
312c056a 2664 BT_ASSERT(medops.borrow_stream);
eb7f6c4f 2665 BT_COMP_LOG_CUR_LVL(BT_LOG_DEBUG, log_level, self_comp,
ea14c7dd 2666 "Creating CTF plugin message iterator: "
862ca4ed 2667 "trace-addr=%p, max-request-size=%zu, "
ea14c7dd
PP
2668 "data=%p, log-level=%s", tc, max_request_sz, data,
2669 bt_common_logging_level_string(log_level));
d6e69534 2670 notit = g_new0(struct bt_msg_iter, 1);
e98a2d6e 2671 if (!notit) {
eb7f6c4f 2672 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp,
ea14c7dd 2673 "Failed to allocate one CTF plugin message iterator.");
e98a2d6e
PP
2674 goto end;
2675 }
eb7f6c4f 2676 notit->self_comp = self_comp;
ea14c7dd 2677 notit->log_level = log_level;
44c440bc 2678 notit->meta.tc = tc;
e98a2d6e
PP
2679 notit->medium.medops = medops;
2680 notit->medium.max_request_sz = max_request_sz;
2681 notit->medium.data = data;
e98a2d6e 2682 notit->stack = stack_new(notit);
44c440bc
PP
2683 notit->stored_values = g_array_new(FALSE, TRUE, sizeof(uint64_t));
2684 g_array_set_size(notit->stored_values, tc->stored_value_count);
2685
e98a2d6e 2686 if (!notit->stack) {
eb7f6c4f 2687 BT_COMP_LOGE_STR("Failed to create field stack.");
c44c3e70 2688 goto error;
e98a2d6e
PP
2689 }
2690
c4a714d2 2691 notit->bfcr = bt_bfcr_create(cbs, notit, log_level, NULL);
5cd6d0e5 2692 if (!notit->bfcr) {
eb7f6c4f 2693 BT_COMP_LOGE_STR("Failed to create binary class reader (BFCR).");
c44c3e70 2694 goto error;
e98a2d6e
PP
2695 }
2696
d6e69534 2697 bt_msg_iter_reset(notit);
eb7f6c4f 2698 BT_COMP_LOGD("Created CTF plugin message iterator: "
862ca4ed 2699 "trace-addr=%p, max-request-size=%zu, "
ea14c7dd
PP
2700 "data=%p, notit-addr=%p, log-level=%s",
2701 tc, max_request_sz, data, notit,
2702 bt_common_logging_level_string(log_level));
9e0c8dbb 2703 notit->cur_packet_offset = 0;
fdf0e7a0 2704
e98a2d6e
PP
2705end:
2706 return notit;
fdf0e7a0 2707
c44c3e70 2708error:
d6e69534 2709 bt_msg_iter_destroy(notit);
c44c3e70
JG
2710 notit = NULL;
2711 goto end;
e98a2d6e
PP
2712}
2713
d6e69534 2714void bt_msg_iter_destroy(struct bt_msg_iter *notit)
e98a2d6e 2715{
c5b9b441
PP
2716 BT_PACKET_PUT_REF_AND_RESET(notit->packet);
2717 BT_STREAM_PUT_REF_AND_RESET(notit->stream);
312c056a 2718 release_all_dscopes(notit);
e98a2d6e 2719
eb7f6c4f 2720 BT_COMP_LOGD("Destroying CTF plugin message iterator: addr=%p", notit);
fdf0e7a0 2721
e98a2d6e 2722 if (notit->stack) {
eb7f6c4f 2723 BT_COMP_LOGD_STR("Destroying field stack.");
e98a2d6e
PP
2724 stack_destroy(notit->stack);
2725 }
2726
5cd6d0e5 2727 if (notit->bfcr) {
eb7f6c4f 2728 BT_COMP_LOGD("Destroying BFCR: bfcr-addr=%p", notit->bfcr);
5cd6d0e5 2729 bt_bfcr_destroy(notit->bfcr);
e98a2d6e
PP
2730 }
2731
44c440bc
PP
2732 if (notit->stored_values) {
2733 g_array_free(notit->stored_values, TRUE);
5f870343 2734 }
fdf0e7a0 2735
e98a2d6e
PP
2736 g_free(notit);
2737}
2738
d6e69534
PP
2739enum bt_msg_iter_status bt_msg_iter_get_next_message(
2740 struct bt_msg_iter *notit,
fc917f65 2741 bt_self_message_iterator *msg_iter, bt_message **message)
e98a2d6e 2742{
d6e69534 2743 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
e98a2d6e 2744
f6ccaed9 2745 BT_ASSERT(notit);
d6e69534 2746 BT_ASSERT(message);
d6e69534 2747 notit->msg_iter = msg_iter;
495490c5 2748 notit->set_stream = true;
eb7f6c4f 2749 BT_COMP_LOGD("Getting next message: notit-addr=%p", notit);
fdf0e7a0 2750
e98a2d6e
PP
2751 while (true) {
2752 status = handle_state(notit);
91d81473 2753 if (G_UNLIKELY(status == BT_MSG_ITER_STATUS_AGAIN)) {
eb7f6c4f 2754 BT_COMP_LOGD_STR("Medium returned BT_MSG_ITER_STATUS_AGAIN.");
7cdc2bab 2755 goto end;
91d81473 2756 } else if (G_UNLIKELY(status != BT_MSG_ITER_STATUS_OK)) {
eb7f6c4f 2757 BT_COMP_LOGW("Cannot handle state: notit-addr=%p, state=%s",
fc917f65 2758 notit, state_string(notit->state));
e98a2d6e
PP
2759 goto end;
2760 }
2761
2762 switch (notit->state) {
fc917f65
PP
2763 case STATE_EMIT_MSG_EVENT:
2764 BT_ASSERT(notit->event_msg);
fc917f65
PP
2765 *message = notit->event_msg;
2766 notit->event_msg = NULL;
495490c5
PP
2767 goto end;
2768 case STATE_EMIT_MSG_DISCARDED_EVENTS:
2769 /* create_msg_discared_events() logs errors */
2770 create_msg_discarded_events(notit, message);
2771
2772 if (!*message) {
2773 status = BT_MSG_ITER_STATUS_ERROR;
2774 }
2775
2776 goto end;
2777 case STATE_EMIT_MSG_DISCARDED_PACKETS:
2778 /* create_msg_discared_packets() logs errors */
2779 create_msg_discarded_packets(notit, message);
2780
2781 if (!*message) {
2782 status = BT_MSG_ITER_STATUS_ERROR;
2783 }
2784
fc917f65
PP
2785 goto end;
2786 case STATE_EMIT_MSG_PACKET_BEGINNING:
2787 /* create_msg_packet_beginning() logs errors */
2788 create_msg_packet_beginning(notit, message);
44c440bc 2789
d6e69534
PP
2790 if (!*message) {
2791 status = BT_MSG_ITER_STATUS_ERROR;
f42867e2 2792 }
44c440bc 2793
f42867e2 2794 goto end;
fc917f65
PP
2795 case STATE_EMIT_MSG_PACKET_END_SINGLE:
2796 case STATE_EMIT_MSG_PACKET_END_MULTI:
2797 /* create_msg_packet_end() logs errors */
2798 create_msg_packet_end(notit, message);
44c440bc 2799
d6e69534
PP
2800 if (!*message) {
2801 status = BT_MSG_ITER_STATUS_ERROR;
e98a2d6e 2802 }
312c056a 2803
495490c5 2804 goto end;
fc917f65 2805 case STATE_EMIT_MSG_STREAM_BEGINNING:
495490c5
PP
2806 /* create_msg_stream_beginning() logs errors */
2807 create_msg_stream_beginning(notit, message);
fc917f65 2808
495490c5
PP
2809 if (!*message) {
2810 status = BT_MSG_ITER_STATUS_ERROR;
fc917f65
PP
2811 }
2812
495490c5 2813 goto end;
fc917f65 2814 case STATE_EMIT_MSG_STREAM_END:
495490c5
PP
2815 /* create_msg_stream_end() logs errors */
2816 create_msg_stream_end(notit, message);
fc917f65 2817
495490c5
PP
2818 if (!*message) {
2819 status = BT_MSG_ITER_STATUS_ERROR;
fc917f65
PP
2820 }
2821
495490c5 2822 goto end;
fc917f65
PP
2823 case STATE_DONE:
2824 status = BT_MSG_ITER_STATUS_EOF;
e98a2d6e
PP
2825 goto end;
2826 default:
2827 /* Non-emitting state: continue */
2828 break;
2829 }
2830 }
2831
2832end:
2833 return status;
2834}
87187cbf 2835
41693723
PP
2836static
2837enum bt_msg_iter_status read_packet_header_context_fields(
2838 struct bt_msg_iter *notit)
87187cbf 2839{
9e0c8dbb 2840 int ret;
d6e69534 2841 enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK;
87187cbf 2842
f6ccaed9 2843 BT_ASSERT(notit);
495490c5 2844 notit->set_stream = false;
87187cbf 2845
fc917f65 2846 if (notit->state == STATE_EMIT_MSG_PACKET_BEGINNING) {
87187cbf 2847 /* We're already there */
41693723 2848 goto end;
87187cbf
PP
2849 }
2850
2851 while (true) {
2852 status = handle_state(notit);
91d81473 2853 if (G_UNLIKELY(status == BT_MSG_ITER_STATUS_AGAIN)) {
eb7f6c4f 2854 BT_COMP_LOGD_STR("Medium returned BT_MSG_ITER_STATUS_AGAIN.");
87187cbf 2855 goto end;
91d81473 2856 } else if (G_UNLIKELY(status != BT_MSG_ITER_STATUS_OK)) {
eb7f6c4f 2857 BT_COMP_LOGW("Cannot handle state: notit-addr=%p, state=%s",
fc917f65 2858 notit, state_string(notit->state));
87187cbf
PP
2859 goto end;
2860 }
2861
2862 switch (notit->state) {
fc917f65 2863 case STATE_EMIT_MSG_PACKET_BEGINNING:
87187cbf
PP
2864 /*
2865 * Packet header and context fields are
2866 * potentially decoded (or they don't exist).
2867 */
41693723 2868 goto end;
87187cbf
PP
2869 case STATE_INIT:
2870 case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN:
2871 case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE:
2872 case STATE_AFTER_TRACE_PACKET_HEADER:
2873 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN:
2874 case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE:
2875 case STATE_AFTER_STREAM_PACKET_CONTEXT:
495490c5 2876 case STATE_CHECK_EMIT_MSG_STREAM_BEGINNING:
fc917f65 2877 case STATE_EMIT_MSG_STREAM_BEGINNING:
495490c5
PP
2878 case STATE_CHECK_EMIT_MSG_DISCARDED_EVENTS:
2879 case STATE_EMIT_MSG_DISCARDED_EVENTS:
2880 case STATE_CHECK_EMIT_MSG_DISCARDED_PACKETS:
2881 case STATE_EMIT_MSG_DISCARDED_PACKETS:
87187cbf
PP
2882 /* Non-emitting state: continue */
2883 break;
2884 default:
2885 /*
2886 * We should never get past the
fc917f65 2887 * STATE_EMIT_MSG_PACKET_BEGINNING state.
87187cbf 2888 */
eb7f6c4f 2889 BT_COMP_LOGF("Unexpected state: notit-addr=%p, state=%s",
fdf0e7a0 2890 notit, state_string(notit->state));
0fbb9a9f 2891 abort();
87187cbf
PP
2892 }
2893 }
2894
41693723 2895end:
312c056a
PP
2896 ret = set_current_packet_content_sizes(notit);
2897 if (ret) {
d6e69534 2898 status = BT_MSG_ITER_STATUS_ERROR;
312c056a
PP
2899 }
2900
87187cbf
PP
2901 return status;
2902}
6de92955
PP
2903
2904BT_HIDDEN
d6e69534 2905void bt_msg_iter_set_medops_data(struct bt_msg_iter *notit,
6de92955
PP
2906 void *medops_data)
2907{
f6ccaed9 2908 BT_ASSERT(notit);
6de92955
PP
2909 notit->medium.data = medops_data;
2910}
9e0c8dbb
JG
2911
2912BT_HIDDEN
fc917f65
PP
2913enum bt_msg_iter_status bt_msg_iter_seek(struct bt_msg_iter *notit,
2914 off_t offset)
9e0c8dbb 2915{
d6e69534
PP
2916 enum bt_msg_iter_status ret = BT_MSG_ITER_STATUS_OK;
2917 enum bt_msg_iter_medium_status medium_status;
9e0c8dbb 2918
f6ccaed9 2919 BT_ASSERT(notit);
9e0c8dbb 2920 if (offset < 0) {
eb7f6c4f 2921 BT_COMP_LOGE("Cannot seek to negative offset: offset=%jd", offset);
d6e69534 2922 ret = BT_MSG_ITER_STATUS_INVAL;
9e0c8dbb
JG
2923 goto end;
2924 }
2925
2926 if (!notit->medium.medops.seek) {
d6e69534 2927 ret = BT_MSG_ITER_STATUS_UNSUPPORTED;
eb7f6c4f 2928 BT_COMP_LOGD("Aborting seek as the iterator's underlying media does not implement seek support.");
9e0c8dbb
JG
2929 goto end;
2930 }
2931
2932 medium_status = notit->medium.medops.seek(
d6e69534
PP
2933 BT_MSG_ITER_SEEK_WHENCE_SET, offset, notit->medium.data);
2934 if (medium_status != BT_MSG_ITER_MEDIUM_STATUS_OK) {
2935 if (medium_status == BT_MSG_ITER_MEDIUM_STATUS_EOF) {
2936 ret = BT_MSG_ITER_STATUS_EOF;
9e0c8dbb 2937 } else {
d6e69534 2938 ret = BT_MSG_ITER_STATUS_ERROR;
9e0c8dbb
JG
2939 goto end;
2940 }
2941 }
2942
d6e69534 2943 bt_msg_iter_reset(notit);
9e0c8dbb 2944 notit->cur_packet_offset = offset;
44c440bc 2945
9e0c8dbb
JG
2946end:
2947 return ret;
2948}
2949
44c440bc 2950BT_HIDDEN
d6e69534
PP
2951enum bt_msg_iter_status bt_msg_iter_get_packet_properties(
2952 struct bt_msg_iter *notit,
2953 struct bt_msg_iter_packet_properties *props)
44c440bc 2954{
41693723
PP
2955 enum bt_msg_iter_status status;
2956
44c440bc
PP
2957 BT_ASSERT(notit);
2958 BT_ASSERT(props);
41693723
PP
2959 status = read_packet_header_context_fields(notit);
2960 if (status != BT_MSG_ITER_STATUS_OK) {
2961 goto end;
2962 }
44c440bc 2963
fc917f65
PP
2964 props->exp_packet_total_size = notit->cur_exp_packet_total_size;
2965 props->exp_packet_content_size = notit->cur_exp_packet_content_size;
44c440bc
PP
2966 props->stream_class_id = (uint64_t) notit->cur_stream_class_id;
2967 props->data_stream_id = notit->cur_data_stream_id;
2968 props->snapshots.discarded_events = notit->snapshots.discarded_events;
2969 props->snapshots.packets = notit->snapshots.packets;
2970 props->snapshots.beginning_clock = notit->snapshots.beginning_clock;
2971 props->snapshots.end_clock = notit->snapshots.end_clock;
41693723
PP
2972
2973end:
2974 return status;
9e0c8dbb 2975}
fc917f65
PP
2976
2977BT_HIDDEN
2978void bt_msg_iter_set_emit_stream_beginning_message(struct bt_msg_iter *notit,
2979 bool val)
2980{
2981 notit->emit_stream_begin_msg = val;
2982}
2983
2984BT_HIDDEN
2985void bt_msg_iter_set_emit_stream_end_message(struct bt_msg_iter *notit,
2986 bool val)
2987{
2988 notit->emit_stream_end_msg = val;
2989}
This page took 0.236901 seconds and 4 git commands to generate.