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