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