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