Commit | Line | Data |
---|---|---|
e98a2d6e PP |
1 | /* |
2 | * Babeltrace - CTF notification iterator | |
06a626b8 | 3 | * |
e98a2d6e PP |
4 | * Copyright (c) 2015-2016 EfficiOS Inc. and Linux Foundation |
5 | * Copyright (c) 2015-2016 Philippe Proulx <pproulx@efficios.com> | |
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 | ||
26 | #include <stdint.h> | |
27 | #include <inttypes.h> | |
28 | #include <stdio.h> | |
29 | #include <stddef.h> | |
30 | #include <stdbool.h> | |
31 | #include <assert.h> | |
32 | #include <string.h> | |
33 | #include <babeltrace/ctf-ir/field-types.h> | |
34 | #include <babeltrace/ctf-ir/field-path.h> | |
35 | #include <babeltrace/ctf-ir/fields.h> | |
36 | #include <babeltrace/ctf-ir/stream-class.h> | |
37 | #include <babeltrace/ctf-ir/packet.h> | |
38 | #include <babeltrace/ctf-ir/stream.h> | |
39 | #include <babeltrace/ctf-ir/clock.h> | |
40 | #include <babeltrace/ctf-ir/event-class.h> | |
78586d8a JG |
41 | #include <babeltrace/plugin/notification/packet.h> |
42 | #include <babeltrace/plugin/notification/event.h> | |
e98a2d6e PP |
43 | #include <babeltrace/ref.h> |
44 | #include <glib.h> | |
45 | ||
46 | #define PRINT_ERR_STREAM notit->err_stream | |
47 | #define PRINT_PREFIX "ctf-notif-iter" | |
48 | #include "print.h" | |
49 | ||
06a626b8 JG |
50 | #include "notif-iter.h" |
51 | #include "../btr/btr.h" | |
e98a2d6e PP |
52 | |
53 | #define BYTES_TO_BITS(x) ((x) * 8) | |
54 | ||
55 | struct bt_ctf_notif_iter; | |
56 | ||
57 | /* A visit stack entry */ | |
58 | struct stack_entry { | |
59 | /* | |
60 | * Current base field, one of: | |
61 | * | |
62 | * * string | |
63 | * * structure | |
64 | * * array | |
65 | * * sequence | |
66 | * * variant | |
67 | * | |
68 | * Field is owned by this. | |
69 | */ | |
70 | struct bt_ctf_field *base; | |
71 | ||
72 | /* index of next field to set */ | |
73 | size_t index; | |
74 | }; | |
75 | ||
76 | /* Visit stack */ | |
77 | struct stack { | |
78 | /* Entries (struct stack_entry *) (top is last element) */ | |
79 | GPtrArray *entries; | |
e98a2d6e PP |
80 | }; |
81 | ||
82 | /* State */ | |
83 | enum state { | |
84 | STATE_INIT, | |
85 | STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN, | |
86 | STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE, | |
87 | STATE_AFTER_TRACE_PACKET_HEADER, | |
88 | STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN, | |
89 | STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE, | |
90 | STATE_AFTER_STREAM_PACKET_CONTEXT, | |
91 | STATE_EMIT_NOTIF_NEW_PACKET, | |
92 | STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN, | |
93 | STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE, | |
94 | STATE_AFTER_STREAM_EVENT_HEADER, | |
95 | STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN, | |
96 | STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE, | |
97 | STATE_DSCOPE_EVENT_CONTEXT_BEGIN, | |
98 | STATE_DSCOPE_EVENT_CONTEXT_CONTINUE, | |
99 | STATE_DSCOPE_EVENT_PAYLOAD_BEGIN, | |
100 | STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE, | |
101 | STATE_EMIT_NOTIF_EVENT, | |
102 | STATE_EMIT_NOTIF_END_OF_PACKET, | |
103 | STATE_SKIP_PACKET_PADDING, | |
104 | }; | |
105 | ||
106 | /* CTF notification iterator */ | |
107 | struct bt_ctf_notif_iter { | |
108 | /* Visit stack */ | |
109 | struct stack *stack; | |
110 | ||
111 | /* Error stream (may be NULL) */ | |
112 | FILE *err_stream; | |
113 | ||
114 | /* | |
115 | * Current dynamic scope field pointer. | |
116 | * | |
117 | * This is set when a dynamic scope field is first created by | |
118 | * btr_compound_begin_cb(). It points to one of the fields in | |
119 | * dscopes below. | |
120 | */ | |
121 | struct bt_ctf_field **cur_dscope_field; | |
122 | ||
123 | /* Trace and classes (owned by this) */ | |
124 | struct { | |
125 | struct bt_ctf_trace *trace; | |
126 | struct bt_ctf_stream_class *stream_class; | |
127 | struct bt_ctf_event_class *event_class; | |
128 | } meta; | |
129 | ||
130 | /* Current packet (NULL if not created yet) */ | |
131 | struct bt_ctf_packet *packet; | |
132 | ||
133 | /* Database of current dynamic scopes (owned by this) */ | |
134 | struct { | |
135 | struct bt_ctf_field *trace_packet_header; | |
136 | struct bt_ctf_field *stream_packet_context; | |
137 | struct bt_ctf_field *stream_event_header; | |
138 | struct bt_ctf_field *stream_event_context; | |
139 | struct bt_ctf_field *event_context; | |
140 | struct bt_ctf_field *event_payload; | |
141 | } dscopes; | |
142 | ||
143 | /* Current state */ | |
144 | enum state state; | |
145 | ||
2cf1d51e | 146 | /* Current medium buffer data */ |
e98a2d6e PP |
147 | struct { |
148 | /* Last address provided by medium */ | |
149 | const uint8_t *addr; | |
150 | ||
151 | /* Buffer size provided by medium (bytes) */ | |
152 | size_t sz; | |
153 | ||
154 | /* Offset within whole packet of addr (bits) */ | |
155 | size_t packet_offset; | |
156 | ||
157 | /* Current position from addr (bits) */ | |
158 | size_t at; | |
159 | } buf; | |
160 | ||
161 | /* Binary type reader */ | |
162 | struct bt_ctf_btr *btr; | |
163 | ||
2cf1d51e | 164 | /* Current medium data */ |
e98a2d6e PP |
165 | struct { |
166 | struct bt_ctf_notif_iter_medium_ops medops; | |
167 | size_t max_request_sz; | |
168 | void *data; | |
169 | } medium; | |
170 | ||
171 | /* Current packet size (bits) (-1 if unknown) */ | |
e7a4393b | 172 | int64_t cur_packet_size; |
e98a2d6e PP |
173 | |
174 | /* Current content size (bits) (-1 if unknown) */ | |
e7a4393b | 175 | int64_t cur_content_size; |
c44c3e70 JG |
176 | |
177 | /* bt_ctf_clock to uint64_t. */ | |
178 | GHashTable *clock_states; | |
e98a2d6e PP |
179 | }; |
180 | ||
2cf1d51e JG |
181 | static |
182 | int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter *notit); | |
183 | ||
e98a2d6e PP |
184 | static |
185 | void stack_entry_free_func(gpointer data) | |
186 | { | |
187 | struct stack_entry *entry = data; | |
188 | ||
189 | bt_put(entry->base); | |
190 | g_free(entry); | |
191 | } | |
192 | ||
193 | static | |
194 | struct stack *stack_new(struct bt_ctf_notif_iter *notit) | |
195 | { | |
196 | struct stack *stack = NULL; | |
197 | ||
198 | stack = g_new0(struct stack, 1); | |
199 | if (!stack) { | |
200 | goto error; | |
201 | } | |
202 | ||
203 | stack->entries = g_ptr_array_new_with_free_func(stack_entry_free_func); | |
204 | if (!stack->entries) { | |
205 | goto error; | |
206 | } | |
207 | ||
e98a2d6e | 208 | return stack; |
e98a2d6e PP |
209 | error: |
210 | g_free(stack); | |
e98a2d6e PP |
211 | return NULL; |
212 | } | |
213 | ||
214 | static | |
215 | void stack_destroy(struct stack *stack) | |
216 | { | |
217 | assert(stack); | |
218 | g_ptr_array_free(stack->entries, TRUE); | |
219 | g_free(stack); | |
220 | } | |
221 | ||
222 | static | |
223 | int stack_push(struct stack *stack, struct bt_ctf_field *base) | |
224 | { | |
225 | int ret = 0; | |
226 | struct stack_entry *entry; | |
e98a2d6e PP |
227 | |
228 | assert(stack); | |
229 | assert(base); | |
e98a2d6e PP |
230 | entry = g_new0(struct stack_entry, 1); |
231 | if (!entry) { | |
e98a2d6e PP |
232 | ret = -1; |
233 | goto end; | |
234 | } | |
235 | ||
236 | entry->base = bt_get(base); | |
237 | g_ptr_array_add(stack->entries, entry); | |
238 | ||
239 | end: | |
240 | return ret; | |
241 | } | |
242 | ||
243 | static inline | |
244 | unsigned int stack_size(struct stack *stack) | |
245 | { | |
246 | assert(stack); | |
247 | ||
248 | return stack->entries->len; | |
249 | } | |
250 | ||
251 | static | |
252 | void stack_pop(struct stack *stack) | |
253 | { | |
254 | assert(stack); | |
255 | assert(stack_size(stack)); | |
256 | g_ptr_array_remove_index(stack->entries, stack->entries->len - 1); | |
257 | } | |
258 | ||
259 | static inline | |
260 | struct stack_entry *stack_top(struct stack *stack) | |
261 | { | |
262 | assert(stack); | |
263 | assert(stack_size(stack)); | |
264 | ||
265 | return g_ptr_array_index(stack->entries, stack->entries->len - 1); | |
266 | } | |
267 | ||
268 | static inline | |
269 | bool stack_empty(struct stack *stack) | |
270 | { | |
271 | return stack_size(stack) == 0; | |
272 | } | |
273 | ||
274 | static | |
275 | void stack_clear(struct stack *stack) | |
276 | { | |
277 | assert(stack); | |
278 | ||
279 | if (!stack_empty(stack)) { | |
280 | g_ptr_array_remove_range(stack->entries, 0, stack_size(stack)); | |
281 | } | |
282 | ||
283 | assert(stack_empty(stack)); | |
284 | } | |
285 | ||
286 | static inline | |
287 | enum bt_ctf_notif_iter_status notif_iter_status_from_m_status( | |
288 | enum bt_ctf_notif_iter_medium_status m_status) | |
289 | { | |
290 | return m_status; | |
291 | } | |
292 | ||
293 | static inline | |
294 | size_t buf_size_bits(struct bt_ctf_notif_iter *notit) | |
295 | { | |
296 | return BYTES_TO_BITS(notit->buf.sz); | |
297 | } | |
298 | ||
299 | static inline | |
300 | size_t buf_available_bits(struct bt_ctf_notif_iter *notit) | |
301 | { | |
302 | return buf_size_bits(notit) - notit->buf.at; | |
303 | } | |
304 | ||
305 | static inline | |
306 | size_t packet_at(struct bt_ctf_notif_iter *notit) | |
307 | { | |
308 | return notit->buf.packet_offset + notit->buf.at; | |
309 | } | |
310 | ||
311 | static inline | |
312 | size_t remaining_content_bits(struct bt_ctf_notif_iter *notit) | |
313 | { | |
314 | if (notit->cur_content_size == -1) { | |
315 | return -1; | |
316 | } | |
317 | ||
318 | return notit->cur_content_size - packet_at(notit); | |
319 | } | |
320 | ||
321 | static inline | |
322 | size_t remaining_packet_bits(struct bt_ctf_notif_iter *notit) | |
323 | { | |
324 | if (notit->cur_packet_size == -1) { | |
325 | return -1; | |
326 | } | |
327 | ||
328 | return notit->cur_packet_size - packet_at(notit); | |
329 | } | |
330 | ||
331 | static inline | |
332 | void buf_consume_bits(struct bt_ctf_notif_iter *notit, size_t incr) | |
333 | { | |
334 | notit->buf.at += incr; | |
335 | } | |
336 | ||
337 | static inline | |
338 | bool buf_has_enough_bits(struct bt_ctf_notif_iter *notit, size_t sz) | |
339 | { | |
340 | return buf_available_bits(notit) >= sz; | |
341 | } | |
342 | ||
343 | static | |
2cf1d51e JG |
344 | enum bt_ctf_notif_iter_status request_medium_bytes( |
345 | struct bt_ctf_notif_iter *notit) | |
e98a2d6e PP |
346 | { |
347 | uint8_t *buffer_addr; | |
348 | size_t buffer_sz; | |
349 | enum bt_ctf_notif_iter_medium_status m_status; | |
350 | ||
351 | m_status = notit->medium.medops.request_bytes( | |
352 | notit->medium.max_request_sz, &buffer_addr, | |
353 | &buffer_sz, notit->medium.data); | |
354 | if (m_status == BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK) { | |
355 | assert(buffer_sz != 0); | |
356 | ||
357 | /* New packet offset is old one + old size (in bits) */ | |
358 | notit->buf.packet_offset += buf_size_bits(notit); | |
359 | ||
360 | /* Restart at the beginning of the new medium buffer */ | |
361 | notit->buf.at = 0; | |
362 | ||
363 | /* New medium buffer size */ | |
364 | notit->buf.sz = buffer_sz; | |
365 | ||
366 | /* New medium buffer address */ | |
367 | notit->buf.addr = buffer_addr; | |
368 | } | |
369 | ||
370 | return notif_iter_status_from_m_status(m_status); | |
371 | } | |
372 | ||
373 | static inline | |
374 | enum bt_ctf_notif_iter_status buf_ensure_available_bits( | |
375 | struct bt_ctf_notif_iter *notit) | |
376 | { | |
377 | enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK; | |
378 | ||
379 | if (buf_available_bits(notit) == 0) { | |
380 | /* | |
381 | * This _cannot_ return BT_CTF_NOTIF_ITER_STATUS_OK | |
382 | * _and_ no bits. | |
383 | */ | |
384 | status = request_medium_bytes(notit); | |
385 | } | |
386 | ||
387 | return status; | |
388 | } | |
389 | ||
390 | static | |
391 | enum bt_ctf_notif_iter_status read_dscope_begin_state( | |
392 | struct bt_ctf_notif_iter *notit, | |
393 | struct bt_ctf_field_type *dscope_field_type, | |
394 | enum state done_state, enum state continue_state, | |
395 | struct bt_ctf_field **dscope_field) | |
396 | { | |
397 | enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK; | |
398 | enum bt_ctf_btr_status btr_status; | |
399 | size_t consumed_bits; | |
400 | ||
401 | status = buf_ensure_available_bits(notit); | |
402 | if (status != BT_CTF_NOTIF_ITER_STATUS_OK) { | |
403 | goto end; | |
404 | } | |
405 | ||
406 | bt_put(*dscope_field); | |
407 | notit->cur_dscope_field = dscope_field; | |
408 | consumed_bits = bt_ctf_btr_start(notit->btr, dscope_field_type, | |
409 | notit->buf.addr, notit->buf.at, packet_at(notit), | |
410 | notit->buf.sz, &btr_status); | |
411 | ||
412 | switch (btr_status) { | |
413 | case BT_CTF_BTR_STATUS_OK: | |
414 | /* type was read completely */ | |
415 | notit->state = done_state; | |
416 | break; | |
417 | case BT_CTF_BTR_STATUS_EOF: | |
418 | notit->state = continue_state; | |
419 | break; | |
420 | default: | |
421 | PERR("Binary type reader failed to start\n"); | |
422 | status = BT_CTF_NOTIF_ITER_STATUS_ERROR; | |
423 | goto end; | |
424 | } | |
425 | ||
426 | /* Consume bits now since we know we're not in an error state */ | |
427 | buf_consume_bits(notit, consumed_bits); | |
428 | ||
429 | end: | |
430 | return status; | |
431 | } | |
432 | ||
433 | static | |
434 | enum bt_ctf_notif_iter_status read_dscope_continue_state( | |
435 | struct bt_ctf_notif_iter *notit, enum state done_state) | |
436 | { | |
437 | enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK; | |
438 | enum bt_ctf_btr_status btr_status; | |
439 | size_t consumed_bits; | |
440 | ||
441 | status = buf_ensure_available_bits(notit); | |
442 | if (status != BT_CTF_NOTIF_ITER_STATUS_OK) { | |
443 | goto end; | |
444 | } | |
445 | ||
446 | consumed_bits = bt_ctf_btr_continue(notit->btr, notit->buf.addr, | |
78586d8a | 447 | notit->buf.sz, &btr_status); |
e98a2d6e PP |
448 | |
449 | switch (btr_status) { | |
450 | case BT_CTF_BTR_STATUS_OK: | |
78586d8a | 451 | /* Type was read completely. */ |
e98a2d6e PP |
452 | notit->state = done_state; |
453 | break; | |
454 | case BT_CTF_BTR_STATUS_EOF: | |
78586d8a | 455 | /* Stay in this continue state. */ |
e98a2d6e PP |
456 | break; |
457 | default: | |
458 | PERR("Binary type reader failed to continue\n"); | |
459 | status = BT_CTF_NOTIF_ITER_STATUS_ERROR; | |
460 | goto end; | |
461 | } | |
462 | ||
78586d8a | 463 | /* Consume bits now since we know we're not in an error state. */ |
e98a2d6e | 464 | buf_consume_bits(notit, consumed_bits); |
e98a2d6e PP |
465 | end: |
466 | return status; | |
467 | } | |
468 | ||
469 | static | |
470 | void put_event_dscopes(struct bt_ctf_notif_iter *notit) | |
471 | { | |
472 | BT_PUT(notit->dscopes.stream_event_header); | |
473 | BT_PUT(notit->dscopes.stream_event_context); | |
474 | BT_PUT(notit->dscopes.event_context); | |
475 | BT_PUT(notit->dscopes.event_payload); | |
476 | } | |
477 | ||
478 | static | |
479 | void put_all_dscopes(struct bt_ctf_notif_iter *notit) | |
480 | { | |
481 | BT_PUT(notit->dscopes.trace_packet_header); | |
482 | BT_PUT(notit->dscopes.stream_packet_context); | |
483 | put_event_dscopes(notit); | |
484 | } | |
485 | ||
486 | static | |
487 | enum bt_ctf_notif_iter_status read_packet_header_begin_state( | |
488 | struct bt_ctf_notif_iter *notit) | |
489 | { | |
2cf1d51e JG |
490 | struct bt_ctf_field_type *packet_header_type = NULL; |
491 | enum bt_ctf_notif_iter_status ret = BT_CTF_NOTIF_ITER_STATUS_OK; | |
e98a2d6e | 492 | |
2cf1d51e JG |
493 | if (bt_ctf_notif_iter_switch_packet(notit)) { |
494 | ret = BT_CTF_NOTIF_ITER_STATUS_ERROR; | |
495 | goto end; | |
496 | } | |
e98a2d6e | 497 | |
2cf1d51e | 498 | /* Packet header type is common to the whole trace. */ |
e98a2d6e | 499 | packet_header_type = bt_ctf_trace_get_packet_header_type( |
78586d8a | 500 | notit->meta.trace); |
e98a2d6e PP |
501 | if (!packet_header_type) { |
502 | PERR("Failed to retrieve trace's packet header type\n"); | |
2cf1d51e | 503 | ret = BT_CTF_NOTIF_ITER_STATUS_ERROR; |
e98a2d6e PP |
504 | goto end; |
505 | } | |
506 | ||
2cf1d51e | 507 | ret = read_dscope_begin_state(notit, packet_header_type, |
78586d8a JG |
508 | STATE_AFTER_TRACE_PACKET_HEADER, |
509 | STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE, | |
510 | ¬it->dscopes.trace_packet_header); | |
e98a2d6e PP |
511 | end: |
512 | BT_PUT(packet_header_type); | |
2cf1d51e | 513 | return ret; |
e98a2d6e PP |
514 | } |
515 | ||
516 | static | |
517 | enum bt_ctf_notif_iter_status read_packet_header_continue_state( | |
518 | struct bt_ctf_notif_iter *notit) | |
519 | { | |
520 | return read_dscope_continue_state(notit, | |
78586d8a | 521 | STATE_AFTER_TRACE_PACKET_HEADER); |
e98a2d6e PP |
522 | } |
523 | ||
524 | static inline | |
525 | bool is_struct_type(struct bt_ctf_field_type *field_type) | |
526 | { | |
78586d8a JG |
527 | return bt_ctf_field_type_get_type_id(field_type) == |
528 | BT_CTF_TYPE_ID_STRUCT; | |
e98a2d6e PP |
529 | } |
530 | ||
531 | static inline | |
532 | bool is_variant_type(struct bt_ctf_field_type *field_type) | |
533 | { | |
78586d8a JG |
534 | return bt_ctf_field_type_get_type_id(field_type) == |
535 | BT_CTF_TYPE_ID_VARIANT; | |
e98a2d6e PP |
536 | } |
537 | ||
538 | static inline | |
539 | enum bt_ctf_notif_iter_status set_current_stream_class(struct bt_ctf_notif_iter *notit) | |
540 | { | |
541 | enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK; | |
542 | struct bt_ctf_field_type *packet_header_type; | |
543 | struct bt_ctf_field_type *stream_id_field_type = NULL; | |
544 | uint64_t stream_id; | |
545 | ||
546 | /* Is there any "stream_id" field in the packet header? */ | |
547 | packet_header_type = bt_ctf_trace_get_packet_header_type( | |
78586d8a | 548 | notit->meta.trace); |
e98a2d6e PP |
549 | if (!packet_header_type) { |
550 | PERR("Failed to retrieve trace's packet header type\n"); | |
551 | status = BT_CTF_NOTIF_ITER_STATUS_ERROR; | |
552 | goto end; | |
553 | } | |
554 | ||
555 | assert(is_struct_type(packet_header_type)); | |
556 | ||
557 | // TODO: optimalize! | |
558 | stream_id_field_type = | |
78586d8a JG |
559 | bt_ctf_field_type_structure_get_field_type_by_name( |
560 | packet_header_type, "stream_id"); | |
e98a2d6e PP |
561 | if (stream_id_field_type) { |
562 | /* Find appropriate stream class using current stream ID */ | |
563 | struct bt_ctf_field *stream_id_field = NULL; | |
564 | int ret; | |
565 | ||
566 | assert(notit->dscopes.trace_packet_header); | |
567 | ||
568 | // TODO: optimalize! | |
569 | stream_id_field = bt_ctf_field_structure_get_field( | |
78586d8a | 570 | notit->dscopes.trace_packet_header, "stream_id"); |
e98a2d6e PP |
571 | assert(stream_id_field); |
572 | ret = bt_ctf_field_unsigned_integer_get_value( | |
78586d8a | 573 | stream_id_field, &stream_id); |
e98a2d6e PP |
574 | assert(!ret); |
575 | BT_PUT(stream_id_field); | |
576 | } else { | |
577 | /* Only one stream: pick the first stream class */ | |
578 | assert(bt_ctf_trace_get_stream_class_count( | |
78586d8a | 579 | notit->meta.trace) == 1); |
e98a2d6e PP |
580 | stream_id = 0; |
581 | } | |
582 | ||
583 | BT_PUT(notit->meta.stream_class); | |
5b29e799 | 584 | notit->meta.stream_class = bt_ctf_trace_get_stream_class_by_id( |
78586d8a | 585 | notit->meta.trace, stream_id); |
e98a2d6e PP |
586 | if (!notit->meta.stream_class) { |
587 | PERR("Cannot find stream class with ID %" PRIu64 "\n", | |
588 | stream_id); | |
589 | status = BT_CTF_NOTIF_ITER_STATUS_ERROR; | |
590 | goto end; | |
591 | } | |
592 | ||
593 | end: | |
594 | BT_PUT(packet_header_type); | |
595 | BT_PUT(stream_id_field_type); | |
596 | ||
597 | return status; | |
598 | } | |
599 | ||
600 | static | |
601 | enum bt_ctf_notif_iter_status after_packet_header_state( | |
602 | struct bt_ctf_notif_iter *notit) | |
603 | { | |
604 | enum bt_ctf_notif_iter_status status; | |
605 | ||
606 | status = set_current_stream_class(notit); | |
607 | if (status == BT_CTF_NOTIF_ITER_STATUS_OK) { | |
608 | notit->state = STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN; | |
609 | } | |
610 | ||
611 | return status; | |
612 | } | |
613 | ||
614 | static | |
615 | enum bt_ctf_notif_iter_status read_packet_context_begin_state( | |
616 | struct bt_ctf_notif_iter *notit) | |
617 | { | |
618 | enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK; | |
619 | struct bt_ctf_field_type *packet_context_type; | |
620 | ||
621 | assert(notit->meta.stream_class); | |
622 | packet_context_type = bt_ctf_stream_class_get_packet_context_type( | |
78586d8a | 623 | notit->meta.stream_class); |
e98a2d6e PP |
624 | if (!packet_context_type) { |
625 | PERR("Failed to retrieve stream class's packet context\n"); | |
626 | status = BT_CTF_NOTIF_ITER_STATUS_ERROR; | |
627 | goto end; | |
628 | } | |
629 | ||
630 | status = read_dscope_begin_state(notit, packet_context_type, | |
78586d8a JG |
631 | STATE_AFTER_STREAM_PACKET_CONTEXT, |
632 | STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE, | |
633 | ¬it->dscopes.stream_packet_context); | |
e98a2d6e PP |
634 | |
635 | end: | |
636 | BT_PUT(packet_context_type); | |
e98a2d6e PP |
637 | return status; |
638 | } | |
639 | ||
640 | static | |
641 | enum bt_ctf_notif_iter_status read_packet_context_continue_state( | |
642 | struct bt_ctf_notif_iter *notit) | |
643 | { | |
644 | return read_dscope_continue_state(notit, | |
78586d8a | 645 | STATE_AFTER_STREAM_PACKET_CONTEXT); |
e98a2d6e PP |
646 | } |
647 | ||
78586d8a | 648 | static |
e98a2d6e PP |
649 | enum bt_ctf_notif_iter_status set_current_packet_content_sizes( |
650 | struct bt_ctf_notif_iter *notit) | |
651 | { | |
652 | enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK; | |
653 | struct bt_ctf_field *packet_size_field = NULL; | |
654 | struct bt_ctf_field *content_size_field = NULL; | |
655 | uint64_t content_size = -1, packet_size = -1; | |
656 | ||
657 | assert(notit->dscopes.stream_packet_context); | |
658 | ||
e98a2d6e | 659 | packet_size_field = bt_ctf_field_structure_get_field( |
78586d8a | 660 | notit->dscopes.stream_packet_context, "packet_size"); |
e98a2d6e | 661 | content_size_field = bt_ctf_field_structure_get_field( |
78586d8a | 662 | notit->dscopes.stream_packet_context, "content_size"); |
e98a2d6e PP |
663 | if (packet_size_field) { |
664 | int ret = bt_ctf_field_unsigned_integer_get_value( | |
78586d8a | 665 | packet_size_field, &packet_size); |
e98a2d6e | 666 | |
78586d8a | 667 | assert(!ret); |
e98a2d6e PP |
668 | if (packet_size == 0) { |
669 | PERR("Decoded packet size is 0\n"); | |
670 | status = BT_CTF_NOTIF_ITER_STATUS_ERROR; | |
671 | goto end; | |
672 | } else if ((packet_size % 8) != 0) { | |
673 | PERR("Decoded packet size is not a multiple of 8\n"); | |
674 | status = BT_CTF_NOTIF_ITER_STATUS_ERROR; | |
675 | goto end; | |
676 | } | |
677 | } | |
78586d8a | 678 | |
e98a2d6e PP |
679 | if (content_size_field) { |
680 | int ret = bt_ctf_field_unsigned_integer_get_value( | |
681 | content_size_field, &content_size); | |
78586d8a | 682 | |
e98a2d6e PP |
683 | assert(!ret); |
684 | } else { | |
685 | content_size = packet_size; | |
686 | } | |
687 | ||
688 | notit->cur_packet_size = packet_size; | |
689 | notit->cur_content_size = content_size; | |
e98a2d6e PP |
690 | end: |
691 | BT_PUT(packet_size_field); | |
692 | BT_PUT(content_size_field); | |
e98a2d6e PP |
693 | return status; |
694 | } | |
695 | ||
696 | static | |
697 | enum bt_ctf_notif_iter_status after_packet_context_state( | |
698 | struct bt_ctf_notif_iter *notit) | |
699 | { | |
700 | enum bt_ctf_notif_iter_status status; | |
701 | ||
702 | status = set_current_packet_content_sizes(notit); | |
703 | if (status == BT_CTF_NOTIF_ITER_STATUS_OK) { | |
704 | notit->state = STATE_EMIT_NOTIF_NEW_PACKET; | |
705 | } | |
706 | ||
707 | return status; | |
708 | } | |
709 | ||
710 | static | |
711 | enum bt_ctf_notif_iter_status read_event_header_begin_state( | |
712 | struct bt_ctf_notif_iter *notit) | |
713 | { | |
714 | enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK; | |
715 | struct bt_ctf_field_type *event_header_type = NULL; | |
716 | ||
717 | /* Check if we have some content left */ | |
718 | if (notit->cur_content_size >= 0) { | |
719 | if (packet_at(notit) == notit->cur_content_size) { | |
720 | /* No more events! */ | |
721 | notit->state = STATE_EMIT_NOTIF_END_OF_PACKET; | |
722 | goto end; | |
723 | } else if (packet_at(notit) > notit->cur_content_size) { | |
724 | /* That's not supposed to happen */ | |
725 | PERR("Cursor passed packet's content size:\n"); | |
78586d8a | 726 | PERR("\tDecoded content size: %zu\n", |
e98a2d6e | 727 | notit->cur_content_size); |
78586d8a | 728 | PERR("\tCursor position: %zu\n", packet_at(notit)); |
e98a2d6e PP |
729 | status = BT_CTF_NOTIF_ITER_STATUS_ERROR; |
730 | goto end; | |
731 | } | |
732 | } | |
733 | ||
734 | event_header_type = bt_ctf_stream_class_get_event_header_type( | |
735 | notit->meta.stream_class); | |
736 | if (!event_header_type) { | |
737 | PERR("Failed to retrieve stream class's event header type\n"); | |
738 | status = BT_CTF_NOTIF_ITER_STATUS_ERROR; | |
739 | goto end; | |
740 | } | |
741 | ||
742 | put_event_dscopes(notit); | |
743 | status = read_dscope_begin_state(notit, event_header_type, | |
744 | STATE_AFTER_STREAM_EVENT_HEADER, | |
745 | STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE, | |
746 | ¬it->dscopes.stream_event_header); | |
e98a2d6e PP |
747 | end: |
748 | BT_PUT(event_header_type); | |
749 | ||
750 | return status; | |
751 | } | |
752 | ||
753 | static | |
754 | enum bt_ctf_notif_iter_status read_event_header_continue_state( | |
755 | struct bt_ctf_notif_iter *notit) | |
756 | { | |
757 | return read_dscope_continue_state(notit, | |
758 | STATE_AFTER_STREAM_EVENT_HEADER); | |
759 | } | |
760 | ||
761 | static inline | |
762 | enum bt_ctf_notif_iter_status set_current_event_class(struct bt_ctf_notif_iter *notit) | |
763 | { | |
764 | /* | |
765 | * The assert() calls in this function are okay because it is | |
766 | * assumed here that all the metadata objects have been | |
767 | * validated for CTF correctness before decoding actual streams. | |
768 | */ | |
769 | ||
770 | enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK; | |
771 | struct bt_ctf_field_type *event_header_type; | |
772 | struct bt_ctf_field_type *id_field_type = NULL; | |
773 | struct bt_ctf_field_type *v_field_type = NULL; | |
774 | uint64_t event_id = -1ULL; | |
775 | int ret; | |
776 | ||
777 | event_header_type = bt_ctf_stream_class_get_event_header_type( | |
778 | notit->meta.stream_class); | |
779 | if (!event_header_type) { | |
780 | PERR("Failed to retrieve stream class's event header type\n"); | |
781 | status = BT_CTF_NOTIF_ITER_STATUS_ERROR; | |
782 | goto end; | |
783 | } | |
784 | ||
785 | /* Is there any "id"/"v" field in the event header? */ | |
786 | assert(is_struct_type(event_header_type)); | |
787 | id_field_type = bt_ctf_field_type_structure_get_field_type_by_name( | |
788 | event_header_type, "id"); | |
789 | v_field_type = bt_ctf_field_type_structure_get_field_type_by_name( | |
790 | event_header_type, "v"); | |
791 | assert(notit->dscopes.stream_event_header); | |
792 | if (v_field_type) { | |
793 | /* | |
794 | * _ _____ _____ | |
795 | * | | |_ _|_ _| __ __ _ | |
796 | * | | | | | || '_ \ / _` | | |
797 | * | |___| | | || | | | (_| | S P E C I A L | |
798 | * |_____|_| |_||_| |_|\__, | C A S E ™ | |
799 | * |___/ | |
800 | */ | |
801 | struct bt_ctf_field *v_field = NULL; | |
802 | struct bt_ctf_field *v_struct_field = NULL; | |
803 | struct bt_ctf_field *v_struct_id_field = NULL; | |
804 | ||
805 | // TODO: optimalize! | |
806 | v_field = bt_ctf_field_structure_get_field( | |
807 | notit->dscopes.stream_event_header, "v"); | |
808 | assert(v_field); | |
809 | ||
810 | v_struct_field = | |
811 | bt_ctf_field_variant_get_current_field(v_field); | |
812 | if (!v_struct_field) { | |
813 | goto end_v_field_type; | |
814 | } | |
815 | ||
816 | // TODO: optimalize! | |
817 | v_struct_id_field = | |
818 | bt_ctf_field_structure_get_field(v_struct_field, "id"); | |
819 | if (!v_struct_id_field) { | |
820 | goto end_v_field_type; | |
821 | } | |
822 | ||
823 | ret = bt_ctf_field_unsigned_integer_get_value( | |
824 | v_struct_id_field, &event_id); | |
825 | if (ret) { | |
826 | event_id = -1ULL; | |
827 | } | |
828 | ||
829 | end_v_field_type: | |
830 | BT_PUT(v_field); | |
831 | BT_PUT(v_struct_field); | |
832 | BT_PUT(v_struct_id_field); | |
833 | } | |
834 | ||
835 | if (id_field_type && event_id == -1ULL) { | |
836 | /* Check "id" field */ | |
837 | struct bt_ctf_field *id_field = NULL; | |
838 | int ret; | |
839 | ||
840 | // TODO: optimalize! | |
841 | id_field = bt_ctf_field_structure_get_field( | |
842 | notit->dscopes.stream_event_header, "id"); | |
843 | assert(id_field); | |
844 | assert(bt_ctf_field_is_integer(id_field) || | |
845 | bt_ctf_field_is_enumeration(id_field)); | |
846 | ||
847 | if (bt_ctf_field_is_integer(id_field)) { | |
848 | ret = bt_ctf_field_unsigned_integer_get_value( | |
849 | id_field, &event_id); | |
850 | } else { | |
851 | struct bt_ctf_field *container; | |
852 | ||
853 | container = bt_ctf_field_enumeration_get_container( | |
854 | id_field); | |
855 | assert(container); | |
856 | ret = bt_ctf_field_unsigned_integer_get_value( | |
857 | container, &event_id); | |
858 | BT_PUT(container); | |
859 | } | |
860 | assert(!ret); | |
861 | BT_PUT(id_field); | |
862 | } | |
863 | ||
864 | if (event_id == -1ULL) { | |
865 | /* Event ID not found: single event? */ | |
866 | assert(bt_ctf_stream_class_get_event_class_count( | |
867 | notit->meta.stream_class) == 1); | |
868 | event_id = 0; | |
869 | } | |
870 | ||
871 | BT_PUT(notit->meta.event_class); | |
872 | notit->meta.event_class = bt_ctf_stream_class_get_event_class_by_id( | |
873 | notit->meta.stream_class, event_id); | |
874 | if (!notit->meta.event_class) { | |
875 | PERR("Cannot find event class with ID %" PRIu64 "\n", event_id); | |
876 | status = BT_CTF_NOTIF_ITER_STATUS_ERROR; | |
877 | goto end; | |
878 | } | |
879 | ||
880 | end: | |
881 | BT_PUT(event_header_type); | |
882 | BT_PUT(id_field_type); | |
883 | BT_PUT(v_field_type); | |
884 | ||
885 | return status; | |
886 | } | |
887 | ||
888 | static | |
889 | enum bt_ctf_notif_iter_status after_event_header_state( | |
890 | struct bt_ctf_notif_iter *notit) | |
891 | { | |
892 | enum bt_ctf_notif_iter_status status; | |
893 | ||
e98a2d6e PP |
894 | status = set_current_event_class(notit); |
895 | if (status != BT_CTF_NOTIF_ITER_STATUS_OK) { | |
896 | PERR("Failed to set current event class\n"); | |
897 | goto end; | |
898 | } | |
899 | ||
900 | notit->state = STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN; | |
901 | ||
902 | end: | |
903 | return status; | |
904 | } | |
905 | ||
906 | static | |
907 | enum bt_ctf_notif_iter_status read_stream_event_context_begin_state( | |
908 | struct bt_ctf_notif_iter *notit) | |
909 | { | |
910 | enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK; | |
911 | struct bt_ctf_field_type *stream_event_context_type; | |
912 | ||
913 | stream_event_context_type = bt_ctf_stream_class_get_event_context_type( | |
914 | notit->meta.stream_class); | |
915 | if (!stream_event_context_type) { | |
916 | PERR("Failed to retrieve stream class's event context type\n"); | |
917 | status = BT_CTF_NOTIF_ITER_STATUS_ERROR; | |
918 | goto end; | |
919 | } | |
920 | ||
921 | status = read_dscope_begin_state(notit, stream_event_context_type, | |
922 | STATE_DSCOPE_EVENT_CONTEXT_BEGIN, | |
923 | STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE, | |
924 | ¬it->dscopes.stream_event_context); | |
925 | ||
926 | end: | |
927 | BT_PUT(stream_event_context_type); | |
928 | ||
929 | return status; | |
930 | } | |
931 | ||
932 | static | |
933 | enum bt_ctf_notif_iter_status read_stream_event_context_continue_state( | |
934 | struct bt_ctf_notif_iter *notit) | |
935 | { | |
936 | return read_dscope_continue_state(notit, | |
937 | STATE_DSCOPE_EVENT_CONTEXT_BEGIN); | |
938 | } | |
939 | ||
940 | static | |
941 | enum bt_ctf_notif_iter_status read_event_context_begin_state( | |
942 | struct bt_ctf_notif_iter *notit) | |
943 | { | |
944 | enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK; | |
945 | struct bt_ctf_field_type *event_context_type; | |
946 | ||
947 | event_context_type = bt_ctf_event_class_get_context_type( | |
948 | notit->meta.event_class); | |
949 | if (!event_context_type) { | |
950 | PERR("Failed to retrieve event class's context type\n"); | |
951 | status = BT_CTF_NOTIF_ITER_STATUS_ERROR; | |
952 | goto end; | |
953 | } | |
954 | ||
955 | status = read_dscope_begin_state(notit, event_context_type, | |
956 | STATE_DSCOPE_EVENT_PAYLOAD_BEGIN, | |
957 | STATE_DSCOPE_EVENT_CONTEXT_CONTINUE, | |
958 | ¬it->dscopes.event_context); | |
959 | ||
960 | end: | |
961 | BT_PUT(event_context_type); | |
962 | ||
963 | return status; | |
964 | } | |
965 | ||
966 | static | |
967 | enum bt_ctf_notif_iter_status read_event_context_continue_state( | |
968 | struct bt_ctf_notif_iter *notit) | |
969 | { | |
970 | return read_dscope_continue_state(notit, | |
971 | STATE_DSCOPE_EVENT_PAYLOAD_BEGIN); | |
972 | } | |
973 | ||
974 | static | |
975 | enum bt_ctf_notif_iter_status read_event_payload_begin_state( | |
976 | struct bt_ctf_notif_iter *notit) | |
977 | { | |
978 | enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK; | |
979 | struct bt_ctf_field_type *event_payload_type; | |
980 | ||
981 | event_payload_type = bt_ctf_event_class_get_payload_type( | |
982 | notit->meta.event_class); | |
983 | if (!event_payload_type) { | |
984 | PERR("Failed to retrieve event class's payload type\n"); | |
985 | status = BT_CTF_NOTIF_ITER_STATUS_ERROR; | |
986 | goto end; | |
987 | } | |
988 | ||
989 | status = read_dscope_begin_state(notit, event_payload_type, | |
990 | STATE_EMIT_NOTIF_EVENT, | |
991 | STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE, | |
992 | ¬it->dscopes.event_payload); | |
993 | ||
994 | end: | |
995 | BT_PUT(event_payload_type); | |
996 | ||
997 | return status; | |
998 | } | |
999 | ||
1000 | static | |
1001 | enum bt_ctf_notif_iter_status read_event_payload_continue_state( | |
1002 | struct bt_ctf_notif_iter *notit) | |
1003 | { | |
1004 | return read_dscope_continue_state(notit, STATE_EMIT_NOTIF_EVENT); | |
1005 | } | |
1006 | ||
1007 | static | |
1008 | enum bt_ctf_notif_iter_status skip_packet_padding_state( | |
1009 | struct bt_ctf_notif_iter *notit) | |
1010 | { | |
1011 | enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK; | |
1012 | size_t bits_to_skip; | |
1013 | ||
1014 | assert(notit->cur_packet_size > 0); | |
1015 | bits_to_skip = notit->cur_packet_size - packet_at(notit); | |
1016 | if (bits_to_skip == 0) { | |
1017 | notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN; | |
1018 | goto end; | |
1019 | } else { | |
1020 | size_t bits_to_consume; | |
1021 | status = buf_ensure_available_bits(notit); | |
1022 | if (status != BT_CTF_NOTIF_ITER_STATUS_OK) { | |
1023 | goto end; | |
1024 | } | |
1025 | ||
1026 | bits_to_consume = MIN(buf_available_bits(notit), bits_to_skip); | |
1027 | buf_consume_bits(notit, bits_to_consume); | |
1028 | bits_to_skip = notit->cur_packet_size - packet_at(notit); | |
1029 | if (bits_to_skip == 0) { | |
1030 | notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN; | |
1031 | goto end; | |
1032 | } | |
1033 | } | |
1034 | ||
1035 | end: | |
1036 | return status; | |
1037 | } | |
1038 | ||
1039 | static inline | |
1040 | enum bt_ctf_notif_iter_status handle_state(struct bt_ctf_notif_iter *notit) | |
1041 | { | |
1042 | enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK; | |
1043 | ||
1044 | PDBG("Handling state %d\n", notit->state); | |
1045 | ||
1046 | // TODO: optimalize! | |
1047 | switch (notit->state) { | |
1048 | case STATE_INIT: | |
1049 | notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN; | |
1050 | break; | |
1051 | case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN: | |
1052 | status = read_packet_header_begin_state(notit); | |
1053 | break; | |
1054 | case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE: | |
1055 | status = read_packet_header_continue_state(notit); | |
1056 | break; | |
1057 | case STATE_AFTER_TRACE_PACKET_HEADER: | |
1058 | status = after_packet_header_state(notit); | |
1059 | break; | |
1060 | case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN: | |
1061 | status = read_packet_context_begin_state(notit); | |
1062 | break; | |
1063 | case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE: | |
1064 | status = read_packet_context_continue_state(notit); | |
1065 | break; | |
1066 | case STATE_AFTER_STREAM_PACKET_CONTEXT: | |
1067 | status = after_packet_context_state(notit); | |
1068 | break; | |
1069 | case STATE_EMIT_NOTIF_NEW_PACKET: | |
1070 | notit->state = STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN; | |
1071 | break; | |
1072 | case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN: | |
1073 | status = read_event_header_begin_state(notit); | |
1074 | break; | |
1075 | case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE: | |
1076 | status = read_event_header_continue_state(notit); | |
1077 | break; | |
1078 | case STATE_AFTER_STREAM_EVENT_HEADER: | |
1079 | status = after_event_header_state(notit); | |
1080 | break; | |
1081 | case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN: | |
1082 | status = read_stream_event_context_begin_state(notit); | |
1083 | break; | |
1084 | case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE: | |
1085 | status = read_stream_event_context_continue_state(notit); | |
1086 | break; | |
1087 | case STATE_DSCOPE_EVENT_CONTEXT_BEGIN: | |
1088 | status = read_event_context_begin_state(notit); | |
1089 | break; | |
1090 | case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE: | |
1091 | status = read_event_context_continue_state(notit); | |
1092 | break; | |
1093 | case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN: | |
1094 | status = read_event_payload_begin_state(notit); | |
1095 | break; | |
1096 | case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE: | |
1097 | status = read_event_payload_continue_state(notit); | |
1098 | break; | |
1099 | case STATE_EMIT_NOTIF_EVENT: | |
1100 | notit->state = STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN; | |
1101 | break; | |
1102 | case STATE_SKIP_PACKET_PADDING: | |
1103 | status = skip_packet_padding_state(notit); | |
1104 | break; | |
1105 | case STATE_EMIT_NOTIF_END_OF_PACKET: | |
1106 | notit->state = STATE_SKIP_PACKET_PADDING; | |
1107 | break; | |
1108 | } | |
1109 | ||
1110 | return status; | |
1111 | } | |
1112 | ||
2cf1d51e JG |
1113 | |
1114 | /** | |
1115 | * Resets the internal state of a CTF notification iterator. | |
1116 | * | |
1117 | * This function can be used when it is desired to seek to the beginning | |
1118 | * of another packet. It is expected that the next call to | |
1119 | * bt_ctf_notif_iter_medium_ops::request_bytes() made by this | |
1120 | * notification iterator will return the \em first bytes of a \em | |
1121 | * packet. | |
1122 | * | |
1123 | * @param notif_iter CTF notification iterator | |
1124 | */ | |
1125 | static | |
e98a2d6e PP |
1126 | void bt_ctf_notif_iter_reset(struct bt_ctf_notif_iter *notit) |
1127 | { | |
1128 | assert(notit); | |
1129 | stack_clear(notit->stack); | |
1130 | BT_PUT(notit->meta.stream_class); | |
1131 | BT_PUT(notit->meta.event_class); | |
1132 | BT_PUT(notit->packet); | |
1133 | put_all_dscopes(notit); | |
1134 | notit->buf.addr = NULL; | |
1135 | notit->buf.sz = 0; | |
1136 | notit->buf.at = 0; | |
1137 | notit->buf.packet_offset = 0; | |
1138 | notit->state = STATE_INIT; | |
1139 | notit->cur_content_size = -1; | |
1140 | notit->cur_packet_size = -1; | |
1141 | } | |
1142 | ||
2cf1d51e JG |
1143 | static |
1144 | int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter *notit) | |
1145 | { | |
1146 | int ret = 0; | |
1147 | ||
1148 | assert(notit); | |
1149 | stack_clear(notit->stack); | |
1150 | BT_PUT(notit->meta.stream_class); | |
1151 | BT_PUT(notit->meta.event_class); | |
1152 | BT_PUT(notit->packet); | |
1153 | put_all_dscopes(notit); | |
1154 | ||
1155 | /* | |
1156 | * Adjust current buffer so that addr points to the beginning of the new | |
1157 | * packet. | |
1158 | */ | |
1159 | if (notit->buf.addr) { | |
1160 | size_t consumed_bytes = (size_t) (notit->buf.at / CHAR_BIT); | |
1161 | ||
1162 | /* Packets are assumed to start on a byte frontier. */ | |
1163 | if (notit->buf.at % CHAR_BIT) { | |
1164 | ret = -1; | |
1165 | goto end; | |
1166 | } | |
1167 | ||
1168 | notit->buf.addr += consumed_bytes; | |
1169 | notit->buf.sz -= consumed_bytes; | |
1170 | notit->buf.at = 0; | |
1171 | notit->buf.packet_offset = 0; | |
1172 | } | |
1173 | ||
1174 | notit->cur_content_size = -1; | |
1175 | notit->cur_packet_size = -1; | |
1176 | end: | |
1177 | return ret; | |
1178 | } | |
1179 | ||
e98a2d6e PP |
1180 | static |
1181 | struct bt_ctf_field *get_next_field(struct bt_ctf_notif_iter *notit) | |
1182 | { | |
1183 | struct bt_ctf_field *next_field = NULL; | |
1184 | struct bt_ctf_field *base_field; | |
1185 | struct bt_ctf_field_type *base_type; | |
1186 | size_t index; | |
1187 | ||
1188 | assert(!stack_empty(notit->stack)); | |
1189 | index = stack_top(notit->stack)->index; | |
1190 | base_field = stack_top(notit->stack)->base; | |
1191 | base_type = bt_ctf_field_get_type(base_field); | |
1192 | if (!base_type) { | |
1193 | PERR("Failed to get base field's type\n"); | |
1194 | goto end; | |
1195 | } | |
1196 | ||
1197 | switch (bt_ctf_field_type_get_type_id(base_type)) { | |
1198 | case BT_CTF_TYPE_ID_STRUCT: | |
1199 | next_field = bt_ctf_field_structure_get_field_by_index( | |
1200 | base_field, index); | |
1201 | break; | |
1202 | case BT_CTF_TYPE_ID_ARRAY: | |
1203 | next_field = bt_ctf_field_array_get_field(base_field, index); | |
1204 | break; | |
1205 | case BT_CTF_TYPE_ID_SEQUENCE: | |
1206 | next_field = bt_ctf_field_sequence_get_field(base_field, index); | |
1207 | break; | |
1208 | case BT_CTF_TYPE_ID_VARIANT: | |
1209 | next_field = bt_ctf_field_variant_get_current_field(base_field); | |
1210 | break; | |
1211 | default: | |
1212 | assert(false); | |
1213 | break; | |
1214 | } | |
1215 | ||
2cf1d51e JG |
1216 | if (!next_field) { |
1217 | next_field = NULL; | |
1218 | } | |
1219 | ||
e98a2d6e PP |
1220 | end: |
1221 | BT_PUT(base_type); | |
1222 | ||
1223 | return next_field; | |
1224 | } | |
1225 | ||
c44c3e70 JG |
1226 | static |
1227 | void update_clock_state(uint64_t *state, | |
1228 | struct bt_ctf_field *value_field) | |
1229 | { | |
1230 | struct bt_ctf_field_type *value_type = NULL; | |
1231 | uint64_t requested_new_value; | |
1232 | uint64_t requested_new_value_mask; | |
1233 | uint64_t cur_value_masked; | |
1234 | int requested_new_value_size; | |
1235 | int ret; | |
1236 | ||
1237 | value_type = bt_ctf_field_get_type(value_field); | |
1238 | assert(value_type); | |
1239 | ||
1240 | requested_new_value_size = | |
1241 | bt_ctf_field_type_integer_get_size(value_type); | |
1242 | assert(requested_new_value_size > 0); | |
1243 | ||
1244 | ret = bt_ctf_field_unsigned_integer_get_value(value_field, | |
1245 | &requested_new_value); | |
1246 | assert(!ret); | |
1247 | ||
1248 | /* | |
1249 | * Special case for a 64-bit new value, which is the limit | |
1250 | * of a clock value as of this version: overwrite the | |
1251 | * current value directly. | |
1252 | */ | |
1253 | if (requested_new_value_size == 64) { | |
1254 | *state = requested_new_value; | |
1255 | goto end; | |
1256 | } | |
1257 | ||
1258 | requested_new_value_mask = (1ULL << requested_new_value_size) - 1; | |
1259 | cur_value_masked = *state & requested_new_value_mask; | |
1260 | ||
1261 | if (requested_new_value < cur_value_masked) { | |
1262 | /* | |
1263 | * It looks like a wrap happened on the number of bits | |
1264 | * of the requested new value. Assume that the clock | |
1265 | * value wrapped only one time. | |
1266 | */ | |
1267 | *state += requested_new_value_mask + 1; | |
1268 | } | |
1269 | ||
1270 | /* Clear the low bits of the current clock value. */ | |
1271 | *state &= ~requested_new_value_mask; | |
1272 | ||
1273 | /* Set the low bits of the current clock value. */ | |
1274 | *state |= requested_new_value; | |
1275 | end: | |
1276 | bt_put(value_type); | |
1277 | } | |
1278 | ||
1279 | static | |
1280 | enum bt_ctf_btr_status update_clock(struct bt_ctf_notif_iter *notit, | |
1281 | struct bt_ctf_field_type *int_field_type, | |
1282 | struct bt_ctf_field *int_field) | |
1283 | { | |
1284 | gboolean clock_found; | |
1285 | uint64_t *clock_state; | |
1286 | enum bt_ctf_btr_status ret = BT_CTF_BTR_STATUS_OK; | |
1287 | struct bt_ctf_clock *clock = bt_ctf_field_type_integer_get_mapped_clock( | |
1288 | int_field_type); | |
1289 | ||
1290 | if (likely(!clock)) { | |
1291 | goto end_no_clock; | |
1292 | } | |
1293 | ||
1294 | clock_found = g_hash_table_lookup_extended(notit->clock_states, | |
1295 | clock, NULL, (gpointer) &clock_state); | |
1296 | if (unlikely(!clock_found)) { | |
1297 | const char *clock_name = bt_ctf_clock_get_name(clock); | |
1298 | ||
1299 | PERR("Unknown clock %s mapped to integer encountered in stream\n", | |
1300 | clock_name ? : "NULL"); | |
1301 | ret = BT_CTF_BTR_STATUS_ERROR; | |
1302 | goto end; | |
1303 | } | |
1304 | ||
1305 | if (unlikely(!clock_state)) { | |
1306 | clock_state = g_new0(uint64_t, 1); | |
1307 | if (!clock_state) { | |
1308 | ret = BT_CTF_BTR_STATUS_ENOMEM; | |
1309 | goto end; | |
1310 | } | |
1311 | g_hash_table_insert(notit->clock_states, bt_get(clock), | |
1312 | clock_state); | |
1313 | } | |
1314 | ||
1315 | /* Update the clock's state. */ | |
1316 | update_clock_state(clock_state, int_field); | |
1317 | end: | |
1318 | bt_put(clock); | |
1319 | end_no_clock: | |
1320 | return ret; | |
1321 | } | |
1322 | ||
e98a2d6e PP |
1323 | static |
1324 | enum bt_ctf_btr_status btr_signed_int_cb(int64_t value, | |
1325 | struct bt_ctf_field_type *type, void *data) | |
1326 | { | |
1327 | enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK; | |
1328 | struct bt_ctf_field *field = NULL; | |
1329 | struct bt_ctf_field *int_field = NULL; | |
1330 | struct bt_ctf_notif_iter *notit = data; | |
1331 | int ret; | |
1332 | ||
1333 | /* create next field */ | |
1334 | field = get_next_field(notit); | |
1335 | if (!field) { | |
1336 | PERR("Failed to get next field (signed int)\n"); | |
1337 | status = BT_CTF_BTR_STATUS_ERROR; | |
c44c3e70 | 1338 | goto end_no_put; |
e98a2d6e PP |
1339 | } |
1340 | ||
1341 | switch(bt_ctf_field_type_get_type_id(type)) { | |
1342 | case BT_CTF_TYPE_ID_INTEGER: | |
1343 | /* Integer field is created field */ | |
1344 | BT_MOVE(int_field, field); | |
c44c3e70 | 1345 | bt_get(type); |
e98a2d6e PP |
1346 | break; |
1347 | case BT_CTF_TYPE_ID_ENUM: | |
1348 | int_field = bt_ctf_field_enumeration_get_container(field); | |
c44c3e70 | 1349 | type = bt_ctf_field_get_type(int_field); |
e98a2d6e PP |
1350 | break; |
1351 | default: | |
c44c3e70 JG |
1352 | assert(0); |
1353 | type = NULL; | |
e98a2d6e PP |
1354 | break; |
1355 | } | |
1356 | ||
1357 | if (!int_field) { | |
1358 | PERR("Failed to get integer field\n"); | |
1359 | status = BT_CTF_BTR_STATUS_ERROR; | |
1360 | goto end; | |
1361 | } | |
1362 | ||
1363 | ret = bt_ctf_field_signed_integer_set_value(int_field, value); | |
1364 | assert(!ret); | |
1365 | stack_top(notit->stack)->index++; | |
c44c3e70 | 1366 | status = update_clock(notit, type, int_field); |
e98a2d6e PP |
1367 | end: |
1368 | BT_PUT(field); | |
1369 | BT_PUT(int_field); | |
c44c3e70 JG |
1370 | BT_PUT(type); |
1371 | end_no_put: | |
e98a2d6e PP |
1372 | return status; |
1373 | } | |
1374 | ||
1375 | static | |
1376 | enum bt_ctf_btr_status btr_unsigned_int_cb(uint64_t value, | |
1377 | struct bt_ctf_field_type *type, void *data) | |
1378 | { | |
1379 | enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK; | |
1380 | struct bt_ctf_field *field = NULL; | |
1381 | struct bt_ctf_field *int_field = NULL; | |
1382 | struct bt_ctf_notif_iter *notit = data; | |
1383 | int ret; | |
1384 | ||
1385 | /* Create next field */ | |
1386 | field = get_next_field(notit); | |
1387 | if (!field) { | |
1388 | PERR("Failed to get next field (unsigned int)\n"); | |
1389 | status = BT_CTF_BTR_STATUS_ERROR; | |
c44c3e70 | 1390 | goto end_no_put; |
e98a2d6e PP |
1391 | } |
1392 | ||
1393 | switch(bt_ctf_field_type_get_type_id(type)) { | |
1394 | case BT_CTF_TYPE_ID_INTEGER: | |
1395 | /* Integer field is created field */ | |
1396 | BT_MOVE(int_field, field); | |
c44c3e70 | 1397 | bt_get(type); |
e98a2d6e PP |
1398 | break; |
1399 | case BT_CTF_TYPE_ID_ENUM: | |
1400 | int_field = bt_ctf_field_enumeration_get_container(field); | |
c44c3e70 | 1401 | type = bt_ctf_field_get_type(int_field); |
e98a2d6e PP |
1402 | break; |
1403 | default: | |
c44c3e70 JG |
1404 | assert(0); |
1405 | type = NULL; | |
e98a2d6e PP |
1406 | break; |
1407 | } | |
1408 | ||
1409 | if (!int_field) { | |
1410 | PERR("Failed to get integer field\n"); | |
1411 | status = BT_CTF_BTR_STATUS_ERROR; | |
1412 | goto end; | |
1413 | } | |
1414 | ||
1415 | ret = bt_ctf_field_unsigned_integer_set_value(int_field, value); | |
1416 | assert(!ret); | |
1417 | stack_top(notit->stack)->index++; | |
c44c3e70 | 1418 | status = update_clock(notit, type, int_field); |
e98a2d6e PP |
1419 | end: |
1420 | BT_PUT(field); | |
1421 | BT_PUT(int_field); | |
c44c3e70 JG |
1422 | BT_PUT(type); |
1423 | end_no_put: | |
e98a2d6e PP |
1424 | return status; |
1425 | } | |
1426 | ||
1427 | static | |
1428 | enum bt_ctf_btr_status btr_floating_point_cb(double value, | |
1429 | struct bt_ctf_field_type *type, void *data) | |
1430 | { | |
1431 | enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK; | |
1432 | struct bt_ctf_field *field = NULL; | |
1433 | struct bt_ctf_notif_iter *notit = data; | |
1434 | int ret; | |
1435 | ||
1436 | /* Create next field */ | |
1437 | field = get_next_field(notit); | |
1438 | if (!field) { | |
1439 | PERR("Failed to get next field (floating point number)\n"); | |
1440 | status = BT_CTF_BTR_STATUS_ERROR; | |
1441 | goto end; | |
1442 | } | |
1443 | ||
1444 | ret = bt_ctf_field_floating_point_set_value(field, value); | |
1445 | assert(!ret); | |
1446 | stack_top(notit->stack)->index++; | |
1447 | ||
1448 | end: | |
1449 | BT_PUT(field); | |
1450 | ||
1451 | return status; | |
1452 | } | |
1453 | ||
1454 | static | |
1455 | enum bt_ctf_btr_status btr_string_begin_cb( | |
1456 | struct bt_ctf_field_type *type, void *data) | |
1457 | { | |
1458 | enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK; | |
1459 | struct bt_ctf_field *field = NULL; | |
1460 | struct bt_ctf_notif_iter *notit = data; | |
1461 | int ret; | |
1462 | ||
1463 | /* Create next field */ | |
1464 | field = get_next_field(notit); | |
1465 | if (!field) { | |
1466 | PERR("Failed to get next field (string)\n"); | |
1467 | status = BT_CTF_BTR_STATUS_ERROR; | |
1468 | goto end; | |
1469 | } | |
1470 | ||
1471 | /* | |
1472 | * Push on stack. Not a compound type per se, but we know that only | |
1473 | * btr_string_cb() may be called between this call and a subsequent | |
1474 | * call to btr_string_end_cb(). | |
1475 | */ | |
1476 | ret = stack_push(notit->stack, field); | |
1477 | if (ret) { | |
1478 | PERR("Failed to push string field onto the stack\n"); | |
1479 | status = BT_CTF_BTR_STATUS_ERROR; | |
1480 | goto end; | |
1481 | } | |
1482 | ||
e5df2ae3 JG |
1483 | /* |
1484 | * Initialize string field payload to an empty string since in the | |
1485 | * case of a length 0 string the btr_string_cb won't be called and | |
1486 | * we will end up with an unset string payload. | |
1487 | */ | |
1488 | ret = bt_ctf_field_string_set_value(field, ""); | |
1489 | if (ret) { | |
1490 | PERR("Failed to initialize string field\n"); | |
1491 | status = BT_CTF_BTR_STATUS_ERROR; | |
1492 | goto end; | |
1493 | } | |
1494 | ||
e98a2d6e PP |
1495 | end: |
1496 | BT_PUT(field); | |
1497 | ||
1498 | return status; | |
1499 | } | |
1500 | ||
1501 | static | |
1502 | enum bt_ctf_btr_status btr_string_cb(const char *value, | |
1503 | size_t len, struct bt_ctf_field_type *type, void *data) | |
1504 | { | |
1505 | enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK; | |
1506 | struct bt_ctf_field *field = NULL; | |
1507 | struct bt_ctf_notif_iter *notit = data; | |
1508 | int ret; | |
1509 | ||
1510 | /* Get string field */ | |
1511 | field = stack_top(notit->stack)->base; | |
1512 | assert(field); | |
1513 | ||
1514 | /* Append current string */ | |
1515 | ret = bt_ctf_field_string_append_len(field, value, len); | |
1516 | if (ret) { | |
1517 | PERR("Failed to append a string to a string field\n"); | |
1518 | status = BT_CTF_BTR_STATUS_ERROR; | |
1519 | goto end; | |
1520 | } | |
1521 | ||
1522 | end: | |
1523 | return status; | |
1524 | } | |
1525 | ||
1526 | static | |
1527 | enum bt_ctf_btr_status btr_string_end_cb( | |
1528 | struct bt_ctf_field_type *type, void *data) | |
1529 | { | |
1530 | struct bt_ctf_notif_iter *notit = data; | |
1531 | ||
1532 | /* Pop string field */ | |
1533 | stack_pop(notit->stack); | |
1534 | ||
1535 | /* Go to next field */ | |
1536 | stack_top(notit->stack)->index++; | |
1537 | ||
1538 | return BT_CTF_BTR_STATUS_OK; | |
1539 | } | |
1540 | ||
1541 | enum bt_ctf_btr_status btr_compound_begin_cb( | |
1542 | struct bt_ctf_field_type *type, void *data) | |
1543 | { | |
1544 | enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK; | |
1545 | struct bt_ctf_notif_iter *notit = data; | |
1546 | struct bt_ctf_field *field; | |
1547 | int ret; | |
1548 | ||
1549 | /* Create field */ | |
1550 | if (stack_empty(notit->stack)) { | |
1551 | /* Root: create dynamic scope field */ | |
1552 | *notit->cur_dscope_field = bt_ctf_field_create(type); | |
1553 | field = *notit->cur_dscope_field; | |
1554 | ||
1555 | /* | |
1556 | * Field will be put at the end of this function | |
1557 | * (stack_push() will take one reference, but this | |
1558 | * reference is lost upon the equivalent stack_pop() | |
1559 | * later), so also get it for our context to own it. | |
1560 | */ | |
1561 | bt_get(*notit->cur_dscope_field); | |
1562 | } else { | |
1563 | field = get_next_field(notit); | |
1564 | } | |
1565 | ||
1566 | if (!field) { | |
1567 | PERR("Failed to get next field or create dynamic scope field\n"); | |
1568 | status = BT_CTF_BTR_STATUS_ERROR; | |
1569 | goto end; | |
1570 | } | |
1571 | ||
1572 | /* Push field */ | |
1573 | ret = stack_push(notit->stack, field); | |
1574 | if (ret) { | |
1575 | PERR("Failed to push compound field onto the stack\n"); | |
1576 | status = BT_CTF_BTR_STATUS_ERROR; | |
1577 | goto end; | |
1578 | } | |
1579 | ||
1580 | end: | |
1581 | BT_PUT(field); | |
1582 | ||
1583 | return status; | |
1584 | } | |
1585 | ||
1586 | enum bt_ctf_btr_status btr_compound_end_cb( | |
1587 | struct bt_ctf_field_type *type, void *data) | |
1588 | { | |
1589 | struct bt_ctf_notif_iter *notit = data; | |
1590 | ||
1591 | assert(!stack_empty(notit->stack)); | |
1592 | ||
1593 | /* Pop stack */ | |
1594 | stack_pop(notit->stack); | |
1595 | ||
1596 | /* If the stack is not empty, increment the base's index */ | |
1597 | if (!stack_empty(notit->stack)) { | |
1598 | stack_top(notit->stack)->index++; | |
1599 | } | |
1600 | ||
1601 | return BT_CTF_BTR_STATUS_OK; | |
1602 | } | |
1603 | ||
1604 | static | |
1605 | struct bt_ctf_field *resolve_field(struct bt_ctf_notif_iter *notit, | |
1606 | struct bt_ctf_field_path *path) | |
1607 | { | |
1608 | struct bt_ctf_field *field = NULL; | |
1609 | unsigned int i; | |
1610 | ||
1611 | switch (bt_ctf_field_path_get_root_scope(path)) { | |
1612 | case BT_CTF_SCOPE_TRACE_PACKET_HEADER: | |
1613 | field = notit->dscopes.trace_packet_header; | |
1614 | break; | |
1615 | case BT_CTF_SCOPE_STREAM_PACKET_CONTEXT: | |
1616 | field = notit->dscopes.stream_packet_context; | |
1617 | break; | |
1618 | case BT_CTF_SCOPE_STREAM_EVENT_HEADER: | |
1619 | field = notit->dscopes.stream_event_header; | |
1620 | break; | |
1621 | case BT_CTF_SCOPE_STREAM_EVENT_CONTEXT: | |
1622 | field = notit->dscopes.stream_event_context; | |
1623 | break; | |
1624 | case BT_CTF_SCOPE_EVENT_CONTEXT: | |
1625 | field = notit->dscopes.event_context; | |
1626 | break; | |
1627 | case BT_CTF_SCOPE_EVENT_FIELDS: | |
1628 | field = notit->dscopes.event_payload; | |
1629 | break; | |
1630 | default: | |
1631 | break; | |
1632 | } | |
1633 | ||
1634 | if (!field) { | |
1635 | goto end; | |
1636 | } | |
1637 | ||
1638 | bt_get(field); | |
1639 | ||
1640 | for (i = 0; i < bt_ctf_field_path_get_index_count(path); ++i) { | |
1641 | struct bt_ctf_field *next_field = NULL; | |
1642 | struct bt_ctf_field_type *field_type; | |
1643 | int index = bt_ctf_field_path_get_index(path, i); | |
1644 | ||
1645 | field_type = bt_ctf_field_get_type(field); | |
1646 | if (!field_type) { | |
1647 | BT_PUT(field); | |
1648 | goto end; | |
1649 | } | |
1650 | ||
1651 | if (is_struct_type(field_type)) { | |
1652 | next_field = bt_ctf_field_structure_get_field_by_index( | |
1653 | field, index); | |
1654 | } else if (is_variant_type(field_type)) { | |
1655 | next_field = | |
1656 | bt_ctf_field_variant_get_current_field(field); | |
1657 | } | |
1658 | ||
1659 | BT_PUT(field); | |
1660 | BT_PUT(field_type); | |
1661 | ||
1662 | if (!next_field) { | |
1663 | goto end; | |
1664 | } | |
1665 | ||
1666 | /* Move next field -> field */ | |
1667 | BT_MOVE(field, next_field); | |
1668 | } | |
1669 | ||
1670 | end: | |
1671 | return field; | |
1672 | } | |
1673 | ||
1674 | static | |
1675 | int64_t btr_get_sequence_length_cb(struct bt_ctf_field_type *type, void *data) | |
1676 | { | |
1677 | int64_t ret = -1; | |
1678 | int iret; | |
1679 | struct bt_ctf_field_path *field_path; | |
1680 | struct bt_ctf_notif_iter *notit = data; | |
2cf1d51e | 1681 | struct bt_ctf_field *length_field = NULL; |
e98a2d6e PP |
1682 | uint64_t length; |
1683 | ||
1684 | field_path = bt_ctf_field_type_sequence_get_length_field_path(type); | |
1685 | if (!field_path) { | |
1686 | goto end; | |
1687 | } | |
1688 | ||
2cf1d51e JG |
1689 | length_field = resolve_field(notit, field_path); |
1690 | if (!length_field) { | |
e98a2d6e PP |
1691 | goto end; |
1692 | } | |
1693 | ||
2cf1d51e | 1694 | iret = bt_ctf_field_unsigned_integer_get_value(length_field, &length); |
e98a2d6e PP |
1695 | if (iret) { |
1696 | goto end; | |
1697 | } | |
1698 | ||
2cf1d51e JG |
1699 | iret = bt_ctf_field_sequence_set_length(stack_top(notit->stack)->base, |
1700 | length_field); | |
1701 | if (iret) { | |
1702 | goto end; | |
1703 | } | |
e98a2d6e PP |
1704 | ret = (int64_t) length; |
1705 | ||
1706 | end: | |
2cf1d51e | 1707 | BT_PUT(length_field); |
e98a2d6e PP |
1708 | BT_PUT(field_path); |
1709 | ||
1710 | return ret; | |
1711 | } | |
1712 | ||
1713 | static | |
1714 | struct bt_ctf_field_type *btr_get_variant_type_cb( | |
1715 | struct bt_ctf_field_type *type, void *data) | |
1716 | { | |
1717 | struct bt_ctf_field_path *path; | |
1718 | struct bt_ctf_notif_iter *notit = data; | |
1719 | struct bt_ctf_field *tag_field = NULL; | |
1720 | struct bt_ctf_field *selected_field = NULL; | |
1721 | struct bt_ctf_field_type *selected_field_type = NULL; | |
1722 | ||
1723 | path = bt_ctf_field_type_variant_get_tag_field_path(type); | |
1724 | if (!path) { | |
1725 | goto end; | |
1726 | } | |
1727 | ||
1728 | tag_field = resolve_field(notit, path); | |
1729 | if (!tag_field) { | |
1730 | goto end; | |
1731 | } | |
1732 | ||
1733 | /* | |
1734 | * We found the enumeration tag field instance which should be | |
1735 | * able to select a current field for this variant. This | |
1736 | * callback function we're in is called _after_ | |
1737 | * compound_begin(), so the current stack top's base field is | |
1738 | * the variant field in question. We get the selected field here | |
1739 | * thanks to this tag field (thus creating the selected field), | |
1740 | * which will also provide us with its type. Then, this field | |
1741 | * will remain the current selected one until the next callback | |
1742 | * function call which is used to fill the current selected | |
1743 | * field. | |
1744 | */ | |
1745 | selected_field = bt_ctf_field_variant_get_field( | |
1746 | stack_top(notit->stack)->base, tag_field); | |
1747 | if (!selected_field) { | |
1748 | goto end; | |
1749 | } | |
1750 | ||
1751 | selected_field_type = bt_ctf_field_get_type(selected_field); | |
1752 | ||
1753 | end: | |
1754 | BT_PUT(tag_field); | |
1755 | BT_PUT(selected_field); | |
f77ae72a | 1756 | BT_PUT(path); |
e98a2d6e PP |
1757 | |
1758 | return selected_field_type; | |
1759 | } | |
1760 | ||
78586d8a JG |
1761 | static |
1762 | struct bt_ctf_event *create_event(struct bt_ctf_notif_iter *notit) | |
e98a2d6e | 1763 | { |
e98a2d6e | 1764 | int ret; |
78586d8a | 1765 | struct bt_ctf_event *event; |
e98a2d6e | 1766 | |
78586d8a | 1767 | /* Create event object. */ |
e98a2d6e PP |
1768 | event = bt_ctf_event_create(notit->meta.event_class); |
1769 | if (!event) { | |
1770 | goto error; | |
1771 | } | |
1772 | ||
78586d8a | 1773 | /* Set header, stream event context, context, and payload fields. */ |
e98a2d6e PP |
1774 | ret = bt_ctf_event_set_header(event, |
1775 | notit->dscopes.stream_event_header); | |
1776 | if (ret) { | |
1777 | goto error; | |
1778 | } | |
1779 | ||
1780 | ret = bt_ctf_event_set_stream_event_context(event, | |
1781 | notit->dscopes.stream_event_context); | |
1782 | if (ret) { | |
1783 | goto error; | |
1784 | } | |
1785 | ||
1786 | ret = bt_ctf_event_set_event_context(event, | |
1787 | notit->dscopes.event_context); | |
1788 | if (ret) { | |
1789 | goto error; | |
1790 | } | |
1791 | ||
1792 | ret = bt_ctf_event_set_payload_field(event, | |
1793 | notit->dscopes.event_payload); | |
1794 | if (ret) { | |
1795 | goto error; | |
1796 | } | |
1797 | ||
78586d8a | 1798 | /* Associate with current packet. */ |
e98a2d6e PP |
1799 | assert(notit->packet); |
1800 | ret = bt_ctf_event_set_packet(event, notit->packet); | |
1801 | if (ret) { | |
1802 | goto error; | |
1803 | } | |
1804 | ||
1805 | goto end; | |
e98a2d6e PP |
1806 | error: |
1807 | BT_PUT(event); | |
e98a2d6e PP |
1808 | end: |
1809 | return event; | |
1810 | } | |
1811 | ||
78586d8a JG |
1812 | static |
1813 | void create_packet(struct bt_ctf_notif_iter *notit) | |
e98a2d6e PP |
1814 | { |
1815 | int ret; | |
1816 | struct bt_ctf_stream *stream = NULL; | |
1817 | struct bt_ctf_packet *packet = NULL; | |
1818 | ||
1819 | /* Ask the user for the stream */ | |
1820 | stream = notit->medium.medops.get_stream(notit->meta.stream_class, | |
78586d8a | 1821 | notit->medium.data); |
e98a2d6e PP |
1822 | if (!stream) { |
1823 | goto error; | |
1824 | } | |
1825 | ||
1826 | /* Create packet */ | |
1827 | packet = bt_ctf_packet_create(stream); | |
1828 | if (!packet) { | |
1829 | goto error; | |
1830 | } | |
1831 | ||
1832 | /* Set packet's context and header fields */ | |
1833 | if (notit->dscopes.trace_packet_header) { | |
1834 | ret = bt_ctf_packet_set_header(packet, | |
78586d8a | 1835 | notit->dscopes.trace_packet_header); |
e98a2d6e PP |
1836 | if (ret) { |
1837 | goto error; | |
1838 | } | |
1839 | } | |
1840 | ||
1841 | if (notit->dscopes.stream_packet_context) { | |
1842 | ret = bt_ctf_packet_set_context(packet, | |
78586d8a | 1843 | notit->dscopes.stream_packet_context); |
e98a2d6e PP |
1844 | if (ret) { |
1845 | goto error; | |
1846 | } | |
1847 | } | |
1848 | ||
1849 | goto end; | |
e98a2d6e PP |
1850 | error: |
1851 | BT_PUT(packet); | |
e98a2d6e PP |
1852 | end: |
1853 | BT_MOVE(notit->packet, packet); | |
1854 | } | |
1855 | ||
78586d8a JG |
1856 | static |
1857 | void notify_new_packet(struct bt_ctf_notif_iter *notit, | |
1858 | struct bt_notification **notification) | |
e98a2d6e | 1859 | { |
78586d8a | 1860 | struct bt_notification *ret; |
e98a2d6e | 1861 | |
78586d8a | 1862 | /* Initialize the iterator's current packet */ |
e98a2d6e PP |
1863 | create_packet(notit); |
1864 | if (!notit->packet) { | |
78586d8a | 1865 | return; |
e98a2d6e PP |
1866 | } |
1867 | ||
78586d8a JG |
1868 | ret = bt_notification_packet_start_create(notit->packet); |
1869 | if (!ret) { | |
1870 | return; | |
1871 | } | |
1872 | *notification = ret; | |
e98a2d6e PP |
1873 | } |
1874 | ||
78586d8a JG |
1875 | static |
1876 | void notify_end_of_packet(struct bt_ctf_notif_iter *notit, | |
1877 | struct bt_notification **notification) | |
e98a2d6e | 1878 | { |
78586d8a | 1879 | struct bt_notification *ret; |
e98a2d6e | 1880 | |
e98a2d6e | 1881 | if (!notit->packet) { |
78586d8a | 1882 | return; |
e98a2d6e PP |
1883 | } |
1884 | ||
78586d8a JG |
1885 | ret = bt_notification_packet_end_create(notit->packet); |
1886 | if (!ret) { | |
1887 | return; | |
1888 | } | |
1889 | BT_PUT(notit->packet); | |
1890 | *notification = ret; | |
e98a2d6e PP |
1891 | } |
1892 | ||
78586d8a JG |
1893 | static |
1894 | void notify_event(struct bt_ctf_notif_iter *notit, | |
1895 | struct bt_notification **notification) | |
e98a2d6e | 1896 | { |
78586d8a JG |
1897 | struct bt_ctf_event *event; |
1898 | struct bt_notification *ret = NULL; | |
e98a2d6e PP |
1899 | |
1900 | /* Create event */ | |
1901 | event = create_event(notit); | |
1902 | if (!event) { | |
78586d8a | 1903 | goto end; |
e98a2d6e | 1904 | } |
e98a2d6e | 1905 | |
78586d8a JG |
1906 | ret = bt_notification_event_create(event); |
1907 | if (!ret) { | |
1908 | goto end; | |
e98a2d6e | 1909 | } |
78586d8a JG |
1910 | *notification = ret; |
1911 | end: | |
1912 | BT_PUT(event); | |
e98a2d6e PP |
1913 | } |
1914 | ||
043e2020 JG |
1915 | static |
1916 | void notify_eos(struct bt_ctf_notif_iter *notit, | |
1917 | struct bt_notification **notification) | |
1918 | { | |
1919 | struct bt_ctf_event *event; | |
1920 | struct bt_notification *ret = NULL; | |
1921 | ||
1922 | /* Create event */ | |
1923 | event = create_event(notit); | |
1924 | if (!event) { | |
1925 | goto end; | |
1926 | } | |
1927 | ||
1928 | ret = bt_notification_stream_end_create(event); | |
1929 | if (!ret) { | |
1930 | goto end; | |
1931 | } | |
1932 | *notification = ret; | |
1933 | end: | |
1934 | BT_PUT(event); | |
1935 | } | |
1936 | ||
c44c3e70 JG |
1937 | static |
1938 | int init_clock_states(GHashTable *clock_states, struct bt_ctf_trace *trace) | |
1939 | { | |
1940 | int clock_count, i, ret = 0; | |
1941 | ||
1942 | clock_count = bt_ctf_trace_get_clock_count(trace); | |
1943 | if (clock_count <= 0) { | |
1944 | ret = -1; | |
1945 | goto end; | |
1946 | } | |
1947 | ||
1948 | for (i = 0; i < clock_count; i++) { | |
1949 | struct bt_ctf_clock *clock; | |
1950 | ||
1951 | clock = bt_ctf_trace_get_clock(trace, i); | |
1952 | if (!clock) { | |
1953 | ret = -1; | |
1954 | goto end; | |
1955 | } | |
1956 | ||
1957 | g_hash_table_insert(clock_states, bt_get(clock), NULL); | |
1958 | bt_put(clock); | |
1959 | } | |
1960 | end: | |
1961 | return ret; | |
1962 | } | |
1963 | ||
1964 | BT_HIDDEN | |
e98a2d6e PP |
1965 | struct bt_ctf_notif_iter *bt_ctf_notif_iter_create(struct bt_ctf_trace *trace, |
1966 | size_t max_request_sz, | |
1967 | struct bt_ctf_notif_iter_medium_ops medops, | |
1968 | void *data, FILE *err_stream) | |
1969 | { | |
c44c3e70 | 1970 | int ret; |
e98a2d6e PP |
1971 | struct bt_ctf_notif_iter *notit = NULL; |
1972 | struct bt_ctf_btr_cbs cbs = { | |
1973 | .types = { | |
1974 | .signed_int = btr_signed_int_cb, | |
1975 | .unsigned_int = btr_unsigned_int_cb, | |
1976 | .floating_point = btr_floating_point_cb, | |
1977 | .string_begin = btr_string_begin_cb, | |
1978 | .string = btr_string_cb, | |
1979 | .string_end = btr_string_end_cb, | |
1980 | .compound_begin = btr_compound_begin_cb, | |
1981 | .compound_end = btr_compound_end_cb, | |
1982 | }, | |
1983 | .query = { | |
1984 | .get_sequence_length = btr_get_sequence_length_cb, | |
1985 | .get_variant_type = btr_get_variant_type_cb, | |
1986 | }, | |
1987 | }; | |
1988 | ||
1989 | assert(trace); | |
1990 | assert(medops.request_bytes); | |
1991 | notit = g_new0(struct bt_ctf_notif_iter, 1); | |
1992 | if (!notit) { | |
1993 | PERR("Failed to allocate memory for CTF notification iterator\n"); | |
1994 | goto end; | |
1995 | } | |
c44c3e70 JG |
1996 | notit->clock_states = g_hash_table_new_full(g_direct_hash, |
1997 | g_direct_equal, bt_put, g_free); | |
1998 | if (!notit->clock_states) { | |
1999 | PERR("Failed to create hash table\n"); | |
2000 | goto error; | |
2001 | } | |
2002 | ret = init_clock_states(notit->clock_states, trace); | |
2003 | if (ret) { | |
2004 | PERR("Failed to initialize stream clock states\n"); | |
2005 | goto error; | |
2006 | } | |
35d47007 | 2007 | notit->meta.trace = bt_get(trace); |
e98a2d6e PP |
2008 | notit->medium.medops = medops; |
2009 | notit->medium.max_request_sz = max_request_sz; | |
2010 | notit->medium.data = data; | |
2011 | notit->err_stream = err_stream; | |
2012 | notit->stack = stack_new(notit); | |
2013 | if (!notit->stack) { | |
2014 | PERR("Failed to create stack\n"); | |
c44c3e70 | 2015 | goto error; |
e98a2d6e PP |
2016 | } |
2017 | ||
2018 | notit->btr = bt_ctf_btr_create(cbs, notit, err_stream); | |
2019 | if (!notit->btr) { | |
2020 | PERR("Failed to create binary type reader\n"); | |
c44c3e70 | 2021 | goto error; |
e98a2d6e PP |
2022 | } |
2023 | ||
2024 | bt_ctf_notif_iter_reset(notit); | |
2025 | ||
2026 | end: | |
2027 | return notit; | |
c44c3e70 JG |
2028 | error: |
2029 | bt_ctf_notif_iter_destroy(notit); | |
2030 | notit = NULL; | |
2031 | goto end; | |
e98a2d6e PP |
2032 | } |
2033 | ||
2034 | void bt_ctf_notif_iter_destroy(struct bt_ctf_notif_iter *notit) | |
2035 | { | |
2036 | BT_PUT(notit->meta.trace); | |
2037 | BT_PUT(notit->meta.stream_class); | |
2038 | BT_PUT(notit->meta.event_class); | |
2039 | BT_PUT(notit->packet); | |
2040 | put_all_dscopes(notit); | |
2041 | ||
2042 | if (notit->stack) { | |
2043 | stack_destroy(notit->stack); | |
2044 | } | |
2045 | ||
2046 | if (notit->btr) { | |
2047 | bt_ctf_btr_destroy(notit->btr); | |
2048 | } | |
2049 | ||
c44c3e70 JG |
2050 | if (notit->clock_states) { |
2051 | g_hash_table_destroy(notit->clock_states); | |
2052 | } | |
e98a2d6e PP |
2053 | g_free(notit); |
2054 | } | |
2055 | ||
2056 | enum bt_ctf_notif_iter_status bt_ctf_notif_iter_get_next_notification( | |
2057 | struct bt_ctf_notif_iter *notit, | |
78586d8a | 2058 | struct bt_notification **notification) |
e98a2d6e PP |
2059 | { |
2060 | enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK; | |
2061 | ||
2062 | assert(notit); | |
2063 | assert(notification); | |
2064 | ||
2065 | while (true) { | |
2066 | status = handle_state(notit); | |
2067 | if (status != BT_CTF_NOTIF_ITER_STATUS_OK) { | |
2068 | if (status == BT_CTF_NOTIF_ITER_STATUS_EOF) { | |
78586d8a | 2069 | PDBG("Medium operation reported end of stream\n"); |
e98a2d6e PP |
2070 | } else { |
2071 | PERR("Failed to handle state:\n"); | |
78586d8a | 2072 | PERR("\tState: %d\n", notit->state); |
e98a2d6e PP |
2073 | } |
2074 | goto end; | |
2075 | } | |
2076 | ||
2077 | switch (notit->state) { | |
2078 | case STATE_EMIT_NOTIF_NEW_PACKET: | |
2079 | PDBG("Emitting new packet notification\n"); | |
2080 | notify_new_packet(notit, notification); | |
2081 | if (!*notification) { | |
2082 | status = BT_CTF_NOTIF_ITER_STATUS_ERROR; | |
2083 | } | |
2084 | goto end; | |
2085 | case STATE_EMIT_NOTIF_EVENT: | |
2086 | PDBG("Emitting event notification\n"); | |
2087 | notify_event(notit, notification); | |
2088 | if (!*notification) { | |
2089 | status = BT_CTF_NOTIF_ITER_STATUS_ERROR; | |
2090 | } | |
2091 | goto end; | |
2092 | case STATE_EMIT_NOTIF_END_OF_PACKET: | |
2093 | PDBG("Emitting end of packet notification\n"); | |
2094 | notify_end_of_packet(notit, notification); | |
2095 | if (!*notification) { | |
2096 | status = BT_CTF_NOTIF_ITER_STATUS_ERROR; | |
2097 | } | |
2098 | goto end; | |
2099 | default: | |
2100 | /* Non-emitting state: continue */ | |
2101 | break; | |
2102 | } | |
2103 | } | |
2104 | ||
2105 | end: | |
2106 | return status; | |
2107 | } |