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