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