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