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