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