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