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 | ||
fdf0e7a0 PP |
26 | #define BT_LOG_TAG "PLUGIN-CTF-NOTIF-ITER" |
27 | #include "logging.h" | |
28 | ||
e98a2d6e PP |
29 | #include <stdint.h> |
30 | #include <inttypes.h> | |
31 | #include <stdio.h> | |
32 | #include <stddef.h> | |
33 | #include <stdbool.h> | |
8b45963b | 34 | #include <babeltrace/assert-internal.h> |
e98a2d6e | 35 | #include <string.h> |
e0831b38 | 36 | #include <babeltrace/babeltrace.h> |
fdf0e7a0 | 37 | #include <babeltrace/ctf-ir/field-types-internal.h> |
fdf0e7a0 | 38 | #include <babeltrace/ctf-ir/field-path-internal.h> |
e98a2d6e | 39 | #include <glib.h> |
0fbb9a9f | 40 | #include <stdlib.h> |
e98a2d6e | 41 | |
06a626b8 JG |
42 | #include "notif-iter.h" |
43 | #include "../btr/btr.h" | |
e98a2d6e | 44 | |
839d52a5 | 45 | struct bt_notif_iter; |
e98a2d6e PP |
46 | |
47 | /* A visit stack entry */ | |
48 | struct stack_entry { | |
49 | /* | |
50 | * Current base field, one of: | |
51 | * | |
52 | * * string | |
53 | * * structure | |
54 | * * array | |
55 | * * sequence | |
56 | * * variant | |
57 | * | |
58 | * Field is owned by this. | |
59 | */ | |
839d52a5 | 60 | struct bt_field *base; |
e98a2d6e PP |
61 | |
62 | /* index of next field to set */ | |
63 | size_t index; | |
64 | }; | |
65 | ||
66 | /* Visit stack */ | |
67 | struct stack { | |
68 | /* Entries (struct stack_entry *) (top is last element) */ | |
69 | GPtrArray *entries; | |
e98a2d6e PP |
70 | }; |
71 | ||
72 | /* State */ | |
73 | enum state { | |
74 | STATE_INIT, | |
75 | STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN, | |
76 | STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE, | |
77 | STATE_AFTER_TRACE_PACKET_HEADER, | |
78 | STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN, | |
79 | STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE, | |
80 | STATE_AFTER_STREAM_PACKET_CONTEXT, | |
81 | STATE_EMIT_NOTIF_NEW_PACKET, | |
82 | STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN, | |
83 | STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE, | |
84 | STATE_AFTER_STREAM_EVENT_HEADER, | |
85 | STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN, | |
86 | STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE, | |
87 | STATE_DSCOPE_EVENT_CONTEXT_BEGIN, | |
88 | STATE_DSCOPE_EVENT_CONTEXT_CONTINUE, | |
89 | STATE_DSCOPE_EVENT_PAYLOAD_BEGIN, | |
90 | STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE, | |
91 | STATE_EMIT_NOTIF_EVENT, | |
92 | STATE_EMIT_NOTIF_END_OF_PACKET, | |
93 | STATE_SKIP_PACKET_PADDING, | |
94 | }; | |
95 | ||
5f870343 JG |
96 | struct trace_field_path_cache { |
97 | /* | |
98 | * Indexes of the stream_id and stream_instance_id field in the packet | |
99 | * header structure, -1 if unset. | |
100 | */ | |
101 | int stream_id; | |
102 | int stream_instance_id; | |
103 | }; | |
104 | ||
105 | struct stream_class_field_path_cache { | |
106 | /* | |
107 | * Indexes of the v and id fields in the stream event header structure, | |
108 | * -1 if unset. | |
109 | */ | |
110 | int v; | |
111 | int id; | |
112 | ||
113 | /* | |
114 | * index of the timestamp_end, packet_size and content_size fields in | |
115 | * the stream packet context structure. Set to -1 if the fields were | |
116 | * not found. | |
117 | */ | |
118 | int timestamp_end; | |
119 | int packet_size; | |
120 | int content_size; | |
121 | }; | |
122 | ||
123 | struct field_cb_override { | |
839d52a5 PP |
124 | enum bt_btr_status (* func)(void *value, |
125 | struct bt_field_type *type, void *data); | |
5f870343 JG |
126 | void *data; |
127 | }; | |
128 | ||
e98a2d6e | 129 | /* CTF notification iterator */ |
839d52a5 | 130 | struct bt_notif_iter { |
e98a2d6e PP |
131 | /* Visit stack */ |
132 | struct stack *stack; | |
133 | ||
e98a2d6e PP |
134 | /* |
135 | * Current dynamic scope field pointer. | |
136 | * | |
137 | * This is set when a dynamic scope field is first created by | |
138 | * btr_compound_begin_cb(). It points to one of the fields in | |
139 | * dscopes below. | |
140 | */ | |
839d52a5 | 141 | struct bt_field **cur_dscope_field; |
e98a2d6e PP |
142 | |
143 | /* Trace and classes (owned by this) */ | |
144 | struct { | |
839d52a5 PP |
145 | struct bt_trace *trace; |
146 | struct bt_stream_class *stream_class; | |
147 | struct bt_event_class *event_class; | |
e98a2d6e PP |
148 | } meta; |
149 | ||
150 | /* Current packet (NULL if not created yet) */ | |
839d52a5 | 151 | struct bt_packet *packet; |
e98a2d6e | 152 | |
5184a1d6 | 153 | /* Current stream (NULL if not set yet) */ |
839d52a5 | 154 | struct bt_stream *stream; |
5184a1d6 | 155 | |
5f870343 JG |
156 | /* |
157 | * Current timestamp_end field (to consider before switching packets). | |
158 | */ | |
839d52a5 | 159 | struct bt_field *cur_timestamp_end; |
5f870343 | 160 | |
e98a2d6e PP |
161 | /* Database of current dynamic scopes (owned by this) */ |
162 | struct { | |
839d52a5 PP |
163 | struct bt_field *trace_packet_header; |
164 | struct bt_field *stream_packet_context; | |
165 | struct bt_field *stream_event_header; | |
166 | struct bt_field *stream_event_context; | |
167 | struct bt_field *event_context; | |
168 | struct bt_field *event_payload; | |
e98a2d6e PP |
169 | } dscopes; |
170 | ||
5f870343 JG |
171 | /* |
172 | * Special field overrides. | |
173 | * | |
174 | * Overrides are used to implement the behaviours of special fields such | |
175 | * as "timestamp_end" (which must be ignored until the end of the | |
176 | * packet), "id" (event id) which can be present multiple times and must | |
177 | * be updated multiple time. | |
178 | * | |
179 | * This should be used to implement the behaviour of integer fields | |
180 | * mapped to clocks and other "tagged" fields (in CTF 2). | |
181 | * | |
839d52a5 | 182 | * bt_field_type to struct field_cb_override |
5f870343 JG |
183 | */ |
184 | GHashTable *field_overrides; | |
185 | ||
e98a2d6e PP |
186 | /* Current state */ |
187 | enum state state; | |
188 | ||
2cf1d51e | 189 | /* Current medium buffer data */ |
e98a2d6e PP |
190 | struct { |
191 | /* Last address provided by medium */ | |
192 | const uint8_t *addr; | |
193 | ||
194 | /* Buffer size provided by medium (bytes) */ | |
195 | size_t sz; | |
196 | ||
197 | /* Offset within whole packet of addr (bits) */ | |
198 | size_t packet_offset; | |
199 | ||
200 | /* Current position from addr (bits) */ | |
201 | size_t at; | |
9b876b7b PP |
202 | |
203 | /* Position of the last event header from addr (bits) */ | |
204 | size_t last_eh_at; | |
e98a2d6e PP |
205 | } buf; |
206 | ||
207 | /* Binary type reader */ | |
839d52a5 | 208 | struct bt_btr *btr; |
e98a2d6e | 209 | |
2cf1d51e | 210 | /* Current medium data */ |
e98a2d6e | 211 | struct { |
839d52a5 | 212 | struct bt_notif_iter_medium_ops medops; |
e98a2d6e PP |
213 | size_t max_request_sz; |
214 | void *data; | |
215 | } medium; | |
216 | ||
217 | /* Current packet size (bits) (-1 if unknown) */ | |
e7a4393b | 218 | int64_t cur_packet_size; |
e98a2d6e PP |
219 | |
220 | /* Current content size (bits) (-1 if unknown) */ | |
e7a4393b | 221 | int64_t cur_content_size; |
c44c3e70 | 222 | |
bb230c9b JG |
223 | /* |
224 | * Offset, in the underlying media, of the current packet's start | |
225 | * (-1 if unknown). | |
226 | */ | |
227 | off_t cur_packet_offset; | |
228 | ||
839d52a5 | 229 | /* bt_clock_class to uint64_t. */ |
c44c3e70 | 230 | GHashTable *clock_states; |
5f870343 JG |
231 | |
232 | /* | |
233 | * Cache of the trace-constant field paths (event header type) | |
234 | * associated to the current trace. | |
235 | */ | |
236 | struct trace_field_path_cache trace_field_path_cache; | |
237 | ||
238 | /* | |
239 | * Field path cache associated with the current stream class. | |
240 | * Ownership of this structure belongs to the field_path_caches HT. | |
241 | */ | |
242 | struct stream_class_field_path_cache *cur_sc_field_path_cache; | |
243 | ||
839d52a5 | 244 | /* bt_stream_class to struct stream_class_field_path_cache. */ |
5f870343 | 245 | GHashTable *sc_field_path_caches; |
e98a2d6e PP |
246 | }; |
247 | ||
fdf0e7a0 PP |
248 | static inline |
249 | const char *state_string(enum state state) | |
250 | { | |
251 | switch (state) { | |
252 | case STATE_INIT: | |
253 | return "STATE_INIT"; | |
254 | case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN: | |
255 | return "STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN"; | |
256 | case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE: | |
257 | return "STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE"; | |
258 | case STATE_AFTER_TRACE_PACKET_HEADER: | |
259 | return "STATE_AFTER_TRACE_PACKET_HEADER"; | |
260 | case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN: | |
261 | return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN"; | |
262 | case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE: | |
263 | return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE"; | |
264 | case STATE_AFTER_STREAM_PACKET_CONTEXT: | |
265 | return "STATE_AFTER_STREAM_PACKET_CONTEXT"; | |
266 | case STATE_EMIT_NOTIF_NEW_PACKET: | |
267 | return "STATE_EMIT_NOTIF_NEW_PACKET"; | |
268 | case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN: | |
269 | return "STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN"; | |
270 | case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE: | |
271 | return "STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE"; | |
272 | case STATE_AFTER_STREAM_EVENT_HEADER: | |
273 | return "STATE_AFTER_STREAM_EVENT_HEADER"; | |
274 | case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN: | |
275 | return "STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN"; | |
276 | case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE: | |
277 | return "STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE"; | |
278 | case STATE_DSCOPE_EVENT_CONTEXT_BEGIN: | |
279 | return "STATE_DSCOPE_EVENT_CONTEXT_BEGIN"; | |
280 | case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE: | |
281 | return "STATE_DSCOPE_EVENT_CONTEXT_CONTINUE"; | |
282 | case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN: | |
283 | return "STATE_DSCOPE_EVENT_PAYLOAD_BEGIN"; | |
284 | case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE: | |
285 | return "STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE"; | |
286 | case STATE_EMIT_NOTIF_EVENT: | |
287 | return "STATE_EMIT_NOTIF_EVENT"; | |
288 | case STATE_EMIT_NOTIF_END_OF_PACKET: | |
289 | return "STATE_EMIT_NOTIF_END_OF_PACKET"; | |
290 | case STATE_SKIP_PACKET_PADDING: | |
291 | return "STATE_SKIP_PACKET_PADDING"; | |
292 | default: | |
293 | return "(unknown)"; | |
294 | } | |
295 | } | |
296 | ||
2cf1d51e | 297 | static |
839d52a5 | 298 | int bt_notif_iter_switch_packet(struct bt_notif_iter *notit); |
2cf1d51e | 299 | |
5f870343 | 300 | static |
839d52a5 PP |
301 | enum bt_btr_status btr_timestamp_end_cb(void *value, |
302 | struct bt_field_type *type, void *data); | |
5f870343 | 303 | |
e98a2d6e PP |
304 | static |
305 | void stack_entry_free_func(gpointer data) | |
306 | { | |
307 | struct stack_entry *entry = data; | |
308 | ||
309 | bt_put(entry->base); | |
310 | g_free(entry); | |
311 | } | |
312 | ||
313 | static | |
839d52a5 | 314 | struct stack *stack_new(struct bt_notif_iter *notit) |
e98a2d6e PP |
315 | { |
316 | struct stack *stack = NULL; | |
317 | ||
318 | stack = g_new0(struct stack, 1); | |
319 | if (!stack) { | |
fdf0e7a0 | 320 | BT_LOGE_STR("Failed to allocate one stack."); |
e98a2d6e PP |
321 | goto error; |
322 | } | |
323 | ||
324 | stack->entries = g_ptr_array_new_with_free_func(stack_entry_free_func); | |
325 | if (!stack->entries) { | |
fdf0e7a0 | 326 | BT_LOGE_STR("Failed to allocate a GPtrArray."); |
e98a2d6e PP |
327 | goto error; |
328 | } | |
329 | ||
fdf0e7a0 | 330 | BT_LOGD("Created stack: notit-addr=%p, stack-addr=%p", notit, stack); |
e98a2d6e | 331 | return stack; |
fdf0e7a0 | 332 | |
e98a2d6e PP |
333 | error: |
334 | g_free(stack); | |
e98a2d6e PP |
335 | return NULL; |
336 | } | |
337 | ||
338 | static | |
339 | void stack_destroy(struct stack *stack) | |
340 | { | |
8b45963b | 341 | BT_ASSERT(stack); |
fdf0e7a0 | 342 | BT_LOGD("Destroying stack: addr=%p", stack); |
e98a2d6e PP |
343 | g_ptr_array_free(stack->entries, TRUE); |
344 | g_free(stack); | |
345 | } | |
346 | ||
347 | static | |
839d52a5 | 348 | int stack_push(struct stack *stack, struct bt_field *base) |
e98a2d6e PP |
349 | { |
350 | int ret = 0; | |
351 | struct stack_entry *entry; | |
e98a2d6e | 352 | |
8b45963b PP |
353 | BT_ASSERT(stack); |
354 | BT_ASSERT(base); | |
fdf0e7a0 PP |
355 | BT_LOGV("Pushing base field on stack: stack-addr=%p, " |
356 | "stack-size-before=%u, stack-size-after=%u", | |
357 | stack, stack->entries->len, stack->entries->len + 1); | |
e98a2d6e PP |
358 | entry = g_new0(struct stack_entry, 1); |
359 | if (!entry) { | |
fdf0e7a0 | 360 | BT_LOGE_STR("Failed to allocate one stack entry."); |
e98a2d6e PP |
361 | ret = -1; |
362 | goto end; | |
363 | } | |
364 | ||
365 | entry->base = bt_get(base); | |
366 | g_ptr_array_add(stack->entries, entry); | |
367 | ||
368 | end: | |
369 | return ret; | |
370 | } | |
371 | ||
372 | static inline | |
373 | unsigned int stack_size(struct stack *stack) | |
374 | { | |
8b45963b | 375 | BT_ASSERT(stack); |
e98a2d6e PP |
376 | |
377 | return stack->entries->len; | |
378 | } | |
379 | ||
380 | static | |
381 | void stack_pop(struct stack *stack) | |
382 | { | |
8b45963b PP |
383 | BT_ASSERT(stack); |
384 | BT_ASSERT(stack_size(stack)); | |
fdf0e7a0 PP |
385 | BT_LOGV("Popping from stack: " |
386 | "stack-addr=%p, stack-size-before=%u, stack-size-after=%u", | |
387 | stack, stack->entries->len, stack->entries->len - 1); | |
e98a2d6e PP |
388 | g_ptr_array_remove_index(stack->entries, stack->entries->len - 1); |
389 | } | |
390 | ||
391 | static inline | |
392 | struct stack_entry *stack_top(struct stack *stack) | |
393 | { | |
8b45963b PP |
394 | BT_ASSERT(stack); |
395 | BT_ASSERT(stack_size(stack)); | |
e98a2d6e PP |
396 | |
397 | return g_ptr_array_index(stack->entries, stack->entries->len - 1); | |
398 | } | |
399 | ||
400 | static inline | |
401 | bool stack_empty(struct stack *stack) | |
402 | { | |
403 | return stack_size(stack) == 0; | |
404 | } | |
405 | ||
406 | static | |
407 | void stack_clear(struct stack *stack) | |
408 | { | |
8b45963b | 409 | BT_ASSERT(stack); |
e98a2d6e PP |
410 | |
411 | if (!stack_empty(stack)) { | |
fdf0e7a0 PP |
412 | BT_LOGV("Clearing stack: stack-addr=%p, stack-size=%u", |
413 | stack, stack->entries->len); | |
e98a2d6e PP |
414 | g_ptr_array_remove_range(stack->entries, 0, stack_size(stack)); |
415 | } | |
416 | ||
8b45963b | 417 | BT_ASSERT(stack_empty(stack)); |
e98a2d6e PP |
418 | } |
419 | ||
420 | static inline | |
839d52a5 PP |
421 | enum bt_notif_iter_status notif_iter_status_from_m_status( |
422 | enum bt_notif_iter_medium_status m_status) | |
e98a2d6e | 423 | { |
dc77b521 | 424 | return (int) m_status; |
e98a2d6e PP |
425 | } |
426 | ||
427 | static inline | |
839d52a5 | 428 | size_t buf_size_bits(struct bt_notif_iter *notit) |
e98a2d6e | 429 | { |
fdf0e7a0 | 430 | return notit->buf.sz * 8; |
e98a2d6e PP |
431 | } |
432 | ||
433 | static inline | |
839d52a5 | 434 | size_t buf_available_bits(struct bt_notif_iter *notit) |
e98a2d6e PP |
435 | { |
436 | return buf_size_bits(notit) - notit->buf.at; | |
437 | } | |
438 | ||
439 | static inline | |
839d52a5 | 440 | size_t packet_at(struct bt_notif_iter *notit) |
e98a2d6e PP |
441 | { |
442 | return notit->buf.packet_offset + notit->buf.at; | |
443 | } | |
444 | ||
e98a2d6e | 445 | static inline |
839d52a5 | 446 | void buf_consume_bits(struct bt_notif_iter *notit, size_t incr) |
e98a2d6e | 447 | { |
fdf0e7a0 PP |
448 | BT_LOGV("Advancing cursor: notit-addr=%p, cur-before=%zu, cur-after=%zu", |
449 | notit, notit->buf.at, notit->buf.at + incr); | |
e98a2d6e PP |
450 | notit->buf.at += incr; |
451 | } | |
452 | ||
e98a2d6e | 453 | static |
839d52a5 PP |
454 | enum bt_notif_iter_status request_medium_bytes( |
455 | struct bt_notif_iter *notit) | |
e98a2d6e | 456 | { |
fdf0e7a0 PP |
457 | uint8_t *buffer_addr = NULL; |
458 | size_t buffer_sz = 0; | |
839d52a5 | 459 | enum bt_notif_iter_medium_status m_status; |
e98a2d6e | 460 | |
fdf0e7a0 PP |
461 | BT_LOGV("Calling user function (request bytes): notit-addr=%p, " |
462 | "request-size=%zu", notit, notit->medium.max_request_sz); | |
e98a2d6e PP |
463 | m_status = notit->medium.medops.request_bytes( |
464 | notit->medium.max_request_sz, &buffer_addr, | |
465 | &buffer_sz, notit->medium.data); | |
fdf0e7a0 | 466 | BT_LOGV("User function returned: status=%s, buf-addr=%p, buf-size=%zu", |
839d52a5 | 467 | bt_notif_iter_medium_status_string(m_status), |
fdf0e7a0 | 468 | buffer_addr, buffer_sz); |
839d52a5 | 469 | if (m_status == BT_NOTIF_ITER_MEDIUM_STATUS_OK) { |
8b45963b | 470 | BT_ASSERT(buffer_sz != 0); |
e98a2d6e PP |
471 | |
472 | /* New packet offset is old one + old size (in bits) */ | |
473 | notit->buf.packet_offset += buf_size_bits(notit); | |
474 | ||
475 | /* Restart at the beginning of the new medium buffer */ | |
476 | notit->buf.at = 0; | |
bb8744a7 | 477 | notit->buf.last_eh_at = SIZE_MAX; |
e98a2d6e PP |
478 | |
479 | /* New medium buffer size */ | |
480 | notit->buf.sz = buffer_sz; | |
481 | ||
482 | /* New medium buffer address */ | |
483 | notit->buf.addr = buffer_addr; | |
fdf0e7a0 PP |
484 | |
485 | BT_LOGV("User function returned new bytes: " | |
486 | "packet-offset=%zu, cur=%zu, size=%zu, addr=%p", | |
487 | notit->buf.packet_offset, notit->buf.at, | |
488 | notit->buf.sz, notit->buf.addr); | |
489 | BT_LOGV_MEM(buffer_addr, buffer_sz, "Returned bytes at %p:", | |
490 | buffer_addr); | |
839d52a5 | 491 | } else if (m_status == BT_NOTIF_ITER_MEDIUM_STATUS_EOF) { |
7116f6c0 PP |
492 | /* |
493 | * User returned end of stream: validate that we're not | |
494 | * in the middle of a packet header, packet context, or | |
495 | * event. | |
496 | */ | |
42ef5ec2 PP |
497 | if (notit->cur_packet_size >= 0) { |
498 | if (packet_at(notit) == notit->cur_packet_size) { | |
499 | goto end; | |
7116f6c0 | 500 | } |
42ef5ec2 PP |
501 | } else { |
502 | if (packet_at(notit) == 0) { | |
503 | goto end; | |
7116f6c0 | 504 | } |
7116f6c0 | 505 | |
42ef5ec2 PP |
506 | if (notit->buf.last_eh_at != SIZE_MAX && |
507 | notit->buf.at == notit->buf.last_eh_at) { | |
508 | goto end; | |
7116f6c0 PP |
509 | } |
510 | } | |
511 | ||
7116f6c0 PP |
512 | /* All other states are invalid */ |
513 | BT_LOGW("User function returned %s, but notification iterator is in an unexpected state: " | |
42ef5ec2 PP |
514 | "state=%s, cur-packet-size=%" PRId64 ", cur=%zu, " |
515 | "packet-cur=%zu, last-eh-at=%zu", | |
839d52a5 | 516 | bt_notif_iter_medium_status_string(m_status), |
42ef5ec2 PP |
517 | state_string(notit->state), |
518 | notit->cur_packet_size, | |
519 | notit->buf.at, packet_at(notit), | |
520 | notit->buf.last_eh_at); | |
839d52a5 | 521 | m_status = BT_NOTIF_ITER_MEDIUM_STATUS_ERROR; |
fdf0e7a0 PP |
522 | } else if (m_status < 0) { |
523 | BT_LOGW("User function failed: status=%s", | |
839d52a5 | 524 | bt_notif_iter_medium_status_string(m_status)); |
e98a2d6e PP |
525 | } |
526 | ||
42ef5ec2 | 527 | end: |
e98a2d6e PP |
528 | return notif_iter_status_from_m_status(m_status); |
529 | } | |
530 | ||
531 | static inline | |
839d52a5 PP |
532 | enum bt_notif_iter_status buf_ensure_available_bits( |
533 | struct bt_notif_iter *notit) | |
e98a2d6e | 534 | { |
839d52a5 | 535 | enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; |
e98a2d6e PP |
536 | |
537 | if (buf_available_bits(notit) == 0) { | |
538 | /* | |
839d52a5 | 539 | * This _cannot_ return BT_NOTIF_ITER_STATUS_OK |
e98a2d6e PP |
540 | * _and_ no bits. |
541 | */ | |
542 | status = request_medium_bytes(notit); | |
543 | } | |
544 | ||
545 | return status; | |
546 | } | |
547 | ||
548 | static | |
839d52a5 PP |
549 | enum bt_notif_iter_status read_dscope_begin_state( |
550 | struct bt_notif_iter *notit, | |
551 | struct bt_field_type *dscope_field_type, | |
e98a2d6e | 552 | enum state done_state, enum state continue_state, |
839d52a5 | 553 | struct bt_field **dscope_field) |
e98a2d6e | 554 | { |
839d52a5 PP |
555 | enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; |
556 | enum bt_btr_status btr_status; | |
e98a2d6e PP |
557 | size_t consumed_bits; |
558 | ||
e98a2d6e PP |
559 | bt_put(*dscope_field); |
560 | notit->cur_dscope_field = dscope_field; | |
fdf0e7a0 PP |
561 | BT_LOGV("Starting BTR: notit-addr=%p, btr-addr=%p, ft-addr=%p", |
562 | notit, notit->btr, dscope_field_type); | |
839d52a5 | 563 | consumed_bits = bt_btr_start(notit->btr, dscope_field_type, |
e98a2d6e PP |
564 | notit->buf.addr, notit->buf.at, packet_at(notit), |
565 | notit->buf.sz, &btr_status); | |
fdf0e7a0 | 566 | BT_LOGV("BTR consumed bits: size=%zu", consumed_bits); |
e98a2d6e PP |
567 | |
568 | switch (btr_status) { | |
839d52a5 | 569 | case BT_BTR_STATUS_OK: |
e98a2d6e | 570 | /* type was read completely */ |
fdf0e7a0 | 571 | BT_LOGV_STR("Field was completely decoded."); |
e98a2d6e PP |
572 | notit->state = done_state; |
573 | break; | |
839d52a5 | 574 | case BT_BTR_STATUS_EOF: |
fdf0e7a0 | 575 | BT_LOGV_STR("BTR needs more data to decode field completely."); |
e98a2d6e PP |
576 | notit->state = continue_state; |
577 | break; | |
578 | default: | |
fdf0e7a0 PP |
579 | BT_LOGW("BTR failed to start: notit-addr=%p, btr-addr=%p, " |
580 | "status=%s", notit, notit->btr, | |
839d52a5 PP |
581 | bt_btr_status_string(btr_status)); |
582 | status = BT_NOTIF_ITER_STATUS_ERROR; | |
e98a2d6e PP |
583 | goto end; |
584 | } | |
585 | ||
586 | /* Consume bits now since we know we're not in an error state */ | |
587 | buf_consume_bits(notit, consumed_bits); | |
588 | ||
589 | end: | |
590 | return status; | |
591 | } | |
592 | ||
593 | static | |
839d52a5 PP |
594 | enum bt_notif_iter_status read_dscope_continue_state( |
595 | struct bt_notif_iter *notit, enum state done_state) | |
e98a2d6e | 596 | { |
839d52a5 PP |
597 | enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; |
598 | enum bt_btr_status btr_status; | |
e98a2d6e PP |
599 | size_t consumed_bits; |
600 | ||
42ef5ec2 PP |
601 | BT_LOGV("Continuing BTR: notit-addr=%p, btr-addr=%p", |
602 | notit, notit->btr); | |
603 | ||
e98a2d6e | 604 | status = buf_ensure_available_bits(notit); |
839d52a5 | 605 | if (status != BT_NOTIF_ITER_STATUS_OK) { |
fdf0e7a0 PP |
606 | if (status < 0) { |
607 | BT_LOGW("Cannot ensure that buffer has at least one byte: " | |
608 | "notif-addr=%p, status=%s", | |
839d52a5 | 609 | notit, bt_notif_iter_status_string(status)); |
fdf0e7a0 PP |
610 | } else { |
611 | BT_LOGV("Cannot ensure that buffer has at least one byte: " | |
612 | "notif-addr=%p, status=%s", | |
839d52a5 | 613 | notit, bt_notif_iter_status_string(status)); |
fdf0e7a0 PP |
614 | } |
615 | ||
e98a2d6e PP |
616 | goto end; |
617 | } | |
618 | ||
42ef5ec2 | 619 | |
839d52a5 | 620 | consumed_bits = bt_btr_continue(notit->btr, notit->buf.addr, |
fdf0e7a0 PP |
621 | notit->buf.sz, &btr_status); |
622 | BT_LOGV("BTR consumed bits: size=%zu", consumed_bits); | |
e98a2d6e PP |
623 | |
624 | switch (btr_status) { | |
839d52a5 | 625 | case BT_BTR_STATUS_OK: |
78586d8a | 626 | /* Type was read completely. */ |
fdf0e7a0 | 627 | BT_LOGV_STR("Field was completely decoded."); |
e98a2d6e PP |
628 | notit->state = done_state; |
629 | break; | |
839d52a5 | 630 | case BT_BTR_STATUS_EOF: |
78586d8a | 631 | /* Stay in this continue state. */ |
fdf0e7a0 | 632 | BT_LOGV_STR("BTR needs more data to decode field completely."); |
e98a2d6e PP |
633 | break; |
634 | default: | |
fdf0e7a0 PP |
635 | BT_LOGW("BTR failed to continue: notit-addr=%p, btr-addr=%p, " |
636 | "status=%s", notit, notit->btr, | |
839d52a5 PP |
637 | bt_btr_status_string(btr_status)); |
638 | status = BT_NOTIF_ITER_STATUS_ERROR; | |
e98a2d6e PP |
639 | goto end; |
640 | } | |
641 | ||
78586d8a | 642 | /* Consume bits now since we know we're not in an error state. */ |
e98a2d6e | 643 | buf_consume_bits(notit, consumed_bits); |
e98a2d6e PP |
644 | end: |
645 | return status; | |
646 | } | |
647 | ||
648 | static | |
839d52a5 | 649 | void put_event_dscopes(struct bt_notif_iter *notit) |
e98a2d6e | 650 | { |
fdf0e7a0 | 651 | BT_LOGV_STR("Putting event header field."); |
e98a2d6e | 652 | BT_PUT(notit->dscopes.stream_event_header); |
fdf0e7a0 | 653 | BT_LOGV_STR("Putting stream event context field."); |
e98a2d6e | 654 | BT_PUT(notit->dscopes.stream_event_context); |
fdf0e7a0 | 655 | BT_LOGV_STR("Putting event context field."); |
e98a2d6e | 656 | BT_PUT(notit->dscopes.event_context); |
fdf0e7a0 | 657 | BT_LOGV_STR("Putting event payload field."); |
e98a2d6e PP |
658 | BT_PUT(notit->dscopes.event_payload); |
659 | } | |
660 | ||
661 | static | |
839d52a5 | 662 | void put_all_dscopes(struct bt_notif_iter *notit) |
e98a2d6e | 663 | { |
fdf0e7a0 | 664 | BT_LOGV_STR("Putting packet header field."); |
e98a2d6e | 665 | BT_PUT(notit->dscopes.trace_packet_header); |
fdf0e7a0 | 666 | BT_LOGV_STR("Putting packet context field."); |
e98a2d6e PP |
667 | BT_PUT(notit->dscopes.stream_packet_context); |
668 | put_event_dscopes(notit); | |
669 | } | |
670 | ||
671 | static | |
839d52a5 PP |
672 | enum bt_notif_iter_status read_packet_header_begin_state( |
673 | struct bt_notif_iter *notit) | |
e98a2d6e | 674 | { |
839d52a5 PP |
675 | struct bt_field_type *packet_header_type = NULL; |
676 | enum bt_notif_iter_status ret = BT_NOTIF_ITER_STATUS_OK; | |
e98a2d6e | 677 | |
839d52a5 | 678 | if (bt_notif_iter_switch_packet(notit)) { |
fdf0e7a0 | 679 | BT_LOGW("Cannot switch packet: notit-addr=%p", notit); |
839d52a5 | 680 | ret = BT_NOTIF_ITER_STATUS_ERROR; |
2cf1d51e JG |
681 | goto end; |
682 | } | |
e98a2d6e | 683 | |
2cf1d51e | 684 | /* Packet header type is common to the whole trace. */ |
839d52a5 | 685 | packet_header_type = bt_trace_get_packet_header_type( |
78586d8a | 686 | notit->meta.trace); |
e98a2d6e | 687 | if (!packet_header_type) { |
835b2d10 | 688 | notit->state = STATE_AFTER_TRACE_PACKET_HEADER; |
e98a2d6e PP |
689 | goto end; |
690 | } | |
691 | ||
fdf0e7a0 PP |
692 | BT_LOGV("Decoding packet header field:" |
693 | "notit-addr=%p, trace-addr=%p, trace-name=\"%s\", ft-addr=%p", | |
694 | notit, notit->meta.trace, | |
839d52a5 | 695 | bt_trace_get_name(notit->meta.trace), packet_header_type); |
2cf1d51e | 696 | ret = read_dscope_begin_state(notit, packet_header_type, |
fdf0e7a0 PP |
697 | STATE_AFTER_TRACE_PACKET_HEADER, |
698 | STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE, | |
699 | ¬it->dscopes.trace_packet_header); | |
700 | if (ret < 0) { | |
701 | BT_LOGW("Cannot decode packet header field: " | |
702 | "notit-addr=%p, trace-addr=%p, " | |
703 | "trace-name=\"%s\", ft-addr=%p", | |
704 | notit, notit->meta.trace, | |
839d52a5 | 705 | bt_trace_get_name(notit->meta.trace), |
fdf0e7a0 PP |
706 | packet_header_type); |
707 | } | |
e98a2d6e PP |
708 | end: |
709 | BT_PUT(packet_header_type); | |
2cf1d51e | 710 | return ret; |
e98a2d6e PP |
711 | } |
712 | ||
713 | static | |
839d52a5 PP |
714 | enum bt_notif_iter_status read_packet_header_continue_state( |
715 | struct bt_notif_iter *notit) | |
e98a2d6e PP |
716 | { |
717 | return read_dscope_continue_state(notit, | |
78586d8a | 718 | STATE_AFTER_TRACE_PACKET_HEADER); |
e98a2d6e PP |
719 | } |
720 | ||
721 | static inline | |
839d52a5 | 722 | bool is_struct_type(struct bt_field_type *field_type) |
e98a2d6e | 723 | { |
839d52a5 PP |
724 | return bt_field_type_get_type_id(field_type) == |
725 | BT_FIELD_TYPE_ID_STRUCT; | |
e98a2d6e PP |
726 | } |
727 | ||
728 | static inline | |
839d52a5 | 729 | bool is_variant_type(struct bt_field_type *field_type) |
e98a2d6e | 730 | { |
839d52a5 PP |
731 | return bt_field_type_get_type_id(field_type) == |
732 | BT_FIELD_TYPE_ID_VARIANT; | |
e98a2d6e PP |
733 | } |
734 | ||
5f870343 JG |
735 | static |
736 | struct stream_class_field_path_cache * | |
737 | create_stream_class_field_path_cache_entry( | |
839d52a5 PP |
738 | struct bt_notif_iter *notit, |
739 | struct bt_stream_class *stream_class) | |
5f870343 JG |
740 | { |
741 | int v = -1; | |
742 | int id = -1; | |
743 | int timestamp_end = -1; | |
744 | int packet_size = -1; | |
745 | int content_size = -1; | |
746 | struct stream_class_field_path_cache *cache_entry = g_new0( | |
747 | struct stream_class_field_path_cache, 1); | |
839d52a5 | 748 | struct bt_field_type *event_header = NULL, *packet_context = NULL; |
5f870343 JG |
749 | |
750 | if (!cache_entry) { | |
fdf0e7a0 | 751 | BT_LOGE_STR("Failed to allocate one stream class field path cache."); |
5f870343 JG |
752 | goto end; |
753 | } | |
754 | ||
839d52a5 PP |
755 | event_header = bt_stream_class_get_event_header_type(stream_class); |
756 | if (event_header && bt_field_type_is_structure(event_header)) { | |
5f870343 JG |
757 | int i, count; |
758 | ||
839d52a5 | 759 | count = bt_field_type_structure_get_field_count( |
fdf0e7a0 | 760 | event_header); |
8b45963b | 761 | BT_ASSERT(count >= 0); |
fdf0e7a0 | 762 | |
5f870343 JG |
763 | for (i = 0; i < count; i++) { |
764 | int ret; | |
765 | const char *name; | |
766 | ||
839d52a5 | 767 | ret = bt_field_type_structure_get_field_by_index( |
5f870343 JG |
768 | event_header, &name, NULL, i); |
769 | if (ret) { | |
fdf0e7a0 PP |
770 | BT_LOGE("Cannot get event header structure field type's field: " |
771 | "notit-addr=%p, stream-class-addr=%p, " | |
772 | "stream-class-name=\"%s\", " | |
773 | "stream-class-id=%" PRId64 ", " | |
774 | "ft-addr=%p, index=%d", | |
775 | notit, stream_class, | |
839d52a5 PP |
776 | bt_stream_class_get_name(stream_class), |
777 | bt_stream_class_get_id(stream_class), | |
fdf0e7a0 | 778 | event_header, i); |
5f870343 JG |
779 | goto error; |
780 | } | |
781 | ||
782 | if (v != -1 && id != -1) { | |
783 | break; | |
784 | } | |
fdf0e7a0 PP |
785 | |
786 | if (v == -1 && strcmp(name, "v") == 0) { | |
5f870343 JG |
787 | v = i; |
788 | } else if (id == -1 && !strcmp(name, "id")) { | |
789 | id = i; | |
790 | } | |
791 | } | |
792 | } | |
793 | ||
839d52a5 | 794 | packet_context = bt_stream_class_get_packet_context_type( |
5f870343 | 795 | stream_class); |
839d52a5 | 796 | if (packet_context && bt_field_type_is_structure(packet_context)) { |
5f870343 JG |
797 | int i, count; |
798 | ||
839d52a5 | 799 | count = bt_field_type_structure_get_field_count( |
fdf0e7a0 | 800 | packet_context); |
8b45963b | 801 | BT_ASSERT(count >= 0); |
fdf0e7a0 | 802 | |
5f870343 JG |
803 | for (i = 0; i < count; i++) { |
804 | int ret; | |
805 | const char *name; | |
839d52a5 | 806 | struct bt_field_type *field_type; |
5f870343 JG |
807 | |
808 | if (timestamp_end != -1 && packet_size != -1 && | |
809 | content_size != -1) { | |
810 | break; | |
811 | } | |
812 | ||
839d52a5 | 813 | ret = bt_field_type_structure_get_field_by_index( |
5f870343 JG |
814 | packet_context, &name, &field_type, i); |
815 | if (ret) { | |
fdf0e7a0 PP |
816 | BT_LOGE("Cannot get packet context structure field type's field: " |
817 | "notit-addr=%p, stream-class-addr=%p, " | |
818 | "stream-class-name=\"%s\", " | |
819 | "stream-class-id=%" PRId64 ", " | |
820 | "ft-addr=%p, index=%d", | |
821 | notit, stream_class, | |
839d52a5 PP |
822 | bt_stream_class_get_name(stream_class), |
823 | bt_stream_class_get_id(stream_class), | |
fdf0e7a0 | 824 | event_header, i); |
5f870343 JG |
825 | goto error; |
826 | } | |
827 | ||
828 | if (timestamp_end == -1 && | |
fdf0e7a0 | 829 | strcmp(name, "timestamp_end") == 0) { |
5f870343 JG |
830 | struct field_cb_override *override = g_new0( |
831 | struct field_cb_override, 1); | |
832 | ||
833 | if (!override) { | |
834 | BT_PUT(field_type); | |
835 | goto error; | |
836 | } | |
837 | ||
838 | override->func = btr_timestamp_end_cb; | |
839 | override->data = notit; | |
5f870343 | 840 | g_hash_table_insert(notit->field_overrides, |
fdf0e7a0 | 841 | bt_get(field_type), override); |
5f870343 JG |
842 | timestamp_end = i; |
843 | } else if (packet_size == -1 && | |
844 | !strcmp(name, "packet_size")) { | |
845 | packet_size = i; | |
846 | } else if (content_size == -1 && | |
847 | !strcmp(name, "content_size")) { | |
848 | content_size = i; | |
849 | } | |
850 | BT_PUT(field_type); | |
851 | } | |
852 | } | |
853 | ||
854 | cache_entry->v = v; | |
855 | cache_entry->id = id; | |
856 | cache_entry->timestamp_end = timestamp_end; | |
857 | cache_entry->packet_size = packet_size; | |
858 | cache_entry->content_size = content_size; | |
859 | end: | |
860 | BT_PUT(event_header); | |
861 | BT_PUT(packet_context); | |
862 | return cache_entry; | |
863 | error: | |
864 | g_free(cache_entry); | |
865 | cache_entry = NULL; | |
866 | goto end; | |
867 | } | |
868 | ||
869 | static | |
870 | struct stream_class_field_path_cache *get_stream_class_field_path_cache( | |
839d52a5 PP |
871 | struct bt_notif_iter *notit, |
872 | struct bt_stream_class *stream_class) | |
5f870343 JG |
873 | { |
874 | bool cache_entry_found; | |
875 | struct stream_class_field_path_cache *cache_entry; | |
876 | ||
877 | cache_entry_found = g_hash_table_lookup_extended( | |
878 | notit->sc_field_path_caches, | |
879 | stream_class, NULL, (gpointer) &cache_entry); | |
880 | if (unlikely(!cache_entry_found)) { | |
881 | cache_entry = create_stream_class_field_path_cache_entry(notit, | |
fdf0e7a0 | 882 | stream_class); |
5f870343 | 883 | g_hash_table_insert(notit->sc_field_path_caches, |
fdf0e7a0 | 884 | bt_get(stream_class), (gpointer) cache_entry); |
5f870343 JG |
885 | } |
886 | ||
887 | return cache_entry; | |
888 | } | |
889 | ||
e98a2d6e | 890 | static inline |
839d52a5 PP |
891 | enum bt_notif_iter_status set_current_stream_class( |
892 | struct bt_notif_iter *notit) | |
e98a2d6e | 893 | { |
839d52a5 PP |
894 | enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; |
895 | struct bt_field_type *packet_header_type = NULL; | |
896 | struct bt_field_type *stream_id_field_type = NULL; | |
897 | struct bt_stream_class *new_stream_class = NULL; | |
e98a2d6e PP |
898 | uint64_t stream_id; |
899 | ||
5f870343 JG |
900 | /* Clear the current stream class field path cache. */ |
901 | notit->cur_sc_field_path_cache = NULL; | |
902 | ||
e98a2d6e | 903 | /* Is there any "stream_id" field in the packet header? */ |
839d52a5 | 904 | packet_header_type = bt_trace_get_packet_header_type( |
fdf0e7a0 | 905 | notit->meta.trace); |
e98a2d6e | 906 | if (!packet_header_type) { |
fdf0e7a0 PP |
907 | /* |
908 | * No packet header, therefore no `stream_id` field, | |
909 | * therefore only one stream class. | |
910 | */ | |
911 | goto single_stream_class; | |
e98a2d6e PP |
912 | } |
913 | ||
8b45963b | 914 | BT_ASSERT(is_struct_type(packet_header_type)); |
e98a2d6e PP |
915 | |
916 | // TODO: optimalize! | |
917 | stream_id_field_type = | |
839d52a5 | 918 | bt_field_type_structure_get_field_type_by_name( |
fdf0e7a0 | 919 | packet_header_type, "stream_id"); |
e98a2d6e PP |
920 | if (stream_id_field_type) { |
921 | /* Find appropriate stream class using current stream ID */ | |
e98a2d6e | 922 | int ret; |
839d52a5 | 923 | struct bt_field *stream_id_field = NULL; |
e98a2d6e | 924 | |
8b45963b | 925 | BT_ASSERT(notit->dscopes.trace_packet_header); |
e98a2d6e PP |
926 | |
927 | // TODO: optimalize! | |
839d52a5 | 928 | stream_id_field = bt_field_structure_get_field_by_name( |
78586d8a | 929 | notit->dscopes.trace_packet_header, "stream_id"); |
8b45963b | 930 | BT_ASSERT(stream_id_field); |
839d52a5 | 931 | ret = bt_field_unsigned_integer_get_value( |
78586d8a | 932 | stream_id_field, &stream_id); |
8b45963b | 933 | BT_ASSERT(!ret); |
e98a2d6e PP |
934 | BT_PUT(stream_id_field); |
935 | } else { | |
fdf0e7a0 | 936 | single_stream_class: |
e98a2d6e | 937 | /* Only one stream: pick the first stream class */ |
8b45963b | 938 | BT_ASSERT(bt_trace_get_stream_class_count( |
78586d8a | 939 | notit->meta.trace) == 1); |
e98a2d6e PP |
940 | stream_id = 0; |
941 | } | |
942 | ||
fdf0e7a0 PP |
943 | BT_LOGV("Found stream class ID to use: notit-addr=%p, " |
944 | "stream-class-id=%" PRIu64 ", " | |
945 | "trace-addr=%p, trace-name=\"%s\"", | |
946 | notit, stream_id, notit->meta.trace, | |
839d52a5 | 947 | bt_trace_get_name(notit->meta.trace)); |
fdf0e7a0 | 948 | |
839d52a5 | 949 | new_stream_class = bt_trace_get_stream_class_by_id( |
fdf0e7a0 | 950 | notit->meta.trace, stream_id); |
c937c615 | 951 | if (!new_stream_class) { |
fdf0e7a0 PP |
952 | BT_LOGW("No stream class with ID of stream class ID to use in trace: " |
953 | "notit-addr=%p, stream-class-id=%" PRIu64 ", " | |
954 | "trace-addr=%p, trace-name=\"%s\"", | |
955 | notit, stream_id, notit->meta.trace, | |
839d52a5 PP |
956 | bt_trace_get_name(notit->meta.trace)); |
957 | status = BT_NOTIF_ITER_STATUS_ERROR; | |
e98a2d6e PP |
958 | goto end; |
959 | } | |
960 | ||
c937c615 PP |
961 | if (notit->meta.stream_class) { |
962 | if (new_stream_class != notit->meta.stream_class) { | |
963 | BT_LOGW("Two packets refer to two different stream classes within the same packet sequence: " | |
964 | "notit-addr=%p, prev-stream-class-addr=%p, " | |
965 | "prev-stream-class-name=\"%s\", " | |
966 | "prev-stream-class-id=%" PRId64 ", " | |
967 | "next-stream-class-addr=%p, " | |
968 | "next-stream-class-name=\"%s\", " | |
969 | "next-stream-class-id=%" PRId64 ", " | |
970 | "trace-addr=%p, trace-name=\"%s\"", | |
971 | notit, notit->meta.stream_class, | |
839d52a5 PP |
972 | bt_stream_class_get_name(notit->meta.stream_class), |
973 | bt_stream_class_get_id(notit->meta.stream_class), | |
c937c615 | 974 | new_stream_class, |
839d52a5 PP |
975 | bt_stream_class_get_name(new_stream_class), |
976 | bt_stream_class_get_id(new_stream_class), | |
c937c615 | 977 | notit->meta.trace, |
839d52a5 PP |
978 | bt_trace_get_name(notit->meta.trace)); |
979 | status = BT_NOTIF_ITER_STATUS_ERROR; | |
c937c615 PP |
980 | goto end; |
981 | } | |
982 | } else { | |
983 | BT_MOVE(notit->meta.stream_class, new_stream_class); | |
984 | } | |
985 | ||
fdf0e7a0 PP |
986 | BT_LOGV("Set current stream class: " |
987 | "notit-addr=%p, stream-class-addr=%p, " | |
988 | "stream-class-name=\"%s\", stream-class-id=%" PRId64, | |
989 | notit, notit->meta.stream_class, | |
839d52a5 PP |
990 | bt_stream_class_get_name(notit->meta.stream_class), |
991 | bt_stream_class_get_id(notit->meta.stream_class)); | |
fdf0e7a0 | 992 | |
5f870343 JG |
993 | /* |
994 | * Retrieve (or lazily create) the current stream class field path | |
995 | * cache. | |
996 | */ | |
997 | notit->cur_sc_field_path_cache = get_stream_class_field_path_cache( | |
fdf0e7a0 | 998 | notit, notit->meta.stream_class); |
5f870343 | 999 | if (!notit->cur_sc_field_path_cache) { |
fdf0e7a0 PP |
1000 | BT_LOGW("Cannot retrieve stream class field path from cache: " |
1001 | "notit-addr=%p, stream-class-addr=%p, " | |
1002 | "stream-class-name=\"%s\", stream-class-id=%" PRId64, | |
1003 | notit, notit->meta.stream_class, | |
839d52a5 PP |
1004 | bt_stream_class_get_name(notit->meta.stream_class), |
1005 | bt_stream_class_get_id(notit->meta.stream_class)); | |
1006 | status = BT_NOTIF_ITER_STATUS_ERROR; | |
5f870343 JG |
1007 | goto end; |
1008 | } | |
e98a2d6e PP |
1009 | end: |
1010 | BT_PUT(packet_header_type); | |
1011 | BT_PUT(stream_id_field_type); | |
c937c615 | 1012 | bt_put(new_stream_class); |
e98a2d6e PP |
1013 | return status; |
1014 | } | |
1015 | ||
1016 | static | |
839d52a5 PP |
1017 | enum bt_notif_iter_status after_packet_header_state( |
1018 | struct bt_notif_iter *notit) | |
e98a2d6e | 1019 | { |
839d52a5 | 1020 | enum bt_notif_iter_status status; |
e98a2d6e PP |
1021 | |
1022 | status = set_current_stream_class(notit); | |
839d52a5 | 1023 | if (status == BT_NOTIF_ITER_STATUS_OK) { |
e98a2d6e PP |
1024 | notit->state = STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN; |
1025 | } | |
1026 | ||
1027 | return status; | |
1028 | } | |
1029 | ||
1030 | static | |
839d52a5 PP |
1031 | enum bt_notif_iter_status read_packet_context_begin_state( |
1032 | struct bt_notif_iter *notit) | |
e98a2d6e | 1033 | { |
839d52a5 PP |
1034 | enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; |
1035 | struct bt_field_type *packet_context_type; | |
e98a2d6e | 1036 | |
8b45963b | 1037 | BT_ASSERT(notit->meta.stream_class); |
839d52a5 | 1038 | packet_context_type = bt_stream_class_get_packet_context_type( |
fdf0e7a0 | 1039 | notit->meta.stream_class); |
e98a2d6e | 1040 | if (!packet_context_type) { |
fdf0e7a0 PP |
1041 | BT_LOGV("No packet packet context field type in stream class: continuing: " |
1042 | "notit-addr=%p, stream-class-addr=%p, " | |
1043 | "stream-class-name=\"%s\", stream-class-id=%" PRId64, | |
1044 | notit, notit->meta.stream_class, | |
839d52a5 PP |
1045 | bt_stream_class_get_name(notit->meta.stream_class), |
1046 | bt_stream_class_get_id(notit->meta.stream_class)); | |
835b2d10 | 1047 | notit->state = STATE_AFTER_STREAM_PACKET_CONTEXT; |
e98a2d6e PP |
1048 | goto end; |
1049 | } | |
1050 | ||
fdf0e7a0 PP |
1051 | BT_LOGV("Decoding packet context field: " |
1052 | "notit-addr=%p, stream-class-addr=%p, " | |
1053 | "stream-class-name=\"%s\", stream-class-id=%" PRId64 ", " | |
1054 | "ft-addr=%p", | |
1055 | notit, notit->meta.stream_class, | |
839d52a5 PP |
1056 | bt_stream_class_get_name(notit->meta.stream_class), |
1057 | bt_stream_class_get_id(notit->meta.stream_class), | |
fdf0e7a0 | 1058 | packet_context_type); |
e98a2d6e | 1059 | status = read_dscope_begin_state(notit, packet_context_type, |
fdf0e7a0 PP |
1060 | STATE_AFTER_STREAM_PACKET_CONTEXT, |
1061 | STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE, | |
1062 | ¬it->dscopes.stream_packet_context); | |
1063 | if (status < 0) { | |
1064 | BT_LOGW("Cannot decode packet context field: " | |
1065 | "notit-addr=%p, stream-class-addr=%p, " | |
1066 | "stream-class-name=\"%s\", " | |
1067 | "stream-class-id=%" PRId64 ", ft-addr=%p", | |
1068 | notit, notit->meta.stream_class, | |
839d52a5 PP |
1069 | bt_stream_class_get_name(notit->meta.stream_class), |
1070 | bt_stream_class_get_id(notit->meta.stream_class), | |
fdf0e7a0 PP |
1071 | packet_context_type); |
1072 | } | |
e98a2d6e PP |
1073 | |
1074 | end: | |
1075 | BT_PUT(packet_context_type); | |
e98a2d6e PP |
1076 | return status; |
1077 | } | |
1078 | ||
1079 | static | |
839d52a5 PP |
1080 | enum bt_notif_iter_status read_packet_context_continue_state( |
1081 | struct bt_notif_iter *notit) | |
e98a2d6e PP |
1082 | { |
1083 | return read_dscope_continue_state(notit, | |
78586d8a | 1084 | STATE_AFTER_STREAM_PACKET_CONTEXT); |
e98a2d6e PP |
1085 | } |
1086 | ||
78586d8a | 1087 | static |
839d52a5 PP |
1088 | enum bt_notif_iter_status set_current_packet_content_sizes( |
1089 | struct bt_notif_iter *notit) | |
e98a2d6e | 1090 | { |
839d52a5 PP |
1091 | enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; |
1092 | struct bt_field *packet_size_field = NULL; | |
1093 | struct bt_field *content_size_field = NULL; | |
a27839b0 | 1094 | uint64_t content_size = -1ULL, packet_size = -1ULL; |
e98a2d6e | 1095 | |
89b08333 MD |
1096 | if (!notit->dscopes.stream_packet_context) { |
1097 | goto end; | |
1098 | } | |
e98a2d6e | 1099 | |
839d52a5 | 1100 | packet_size_field = bt_field_structure_get_field_by_name( |
fdf0e7a0 | 1101 | notit->dscopes.stream_packet_context, "packet_size"); |
839d52a5 | 1102 | content_size_field = bt_field_structure_get_field_by_name( |
fdf0e7a0 | 1103 | notit->dscopes.stream_packet_context, "content_size"); |
e98a2d6e | 1104 | if (packet_size_field) { |
839d52a5 | 1105 | int ret = bt_field_unsigned_integer_get_value( |
fdf0e7a0 | 1106 | packet_size_field, &packet_size); |
e98a2d6e | 1107 | |
8b45963b | 1108 | BT_ASSERT(ret == 0); |
e98a2d6e | 1109 | if (packet_size == 0) { |
fdf0e7a0 PP |
1110 | BT_LOGW("Invalid packet size: packet context field indicates packet size is zero: " |
1111 | "notit-addr=%p, packet-context-field-addr=%p", | |
1112 | notit, notit->dscopes.stream_packet_context); | |
839d52a5 | 1113 | status = BT_NOTIF_ITER_STATUS_ERROR; |
e98a2d6e PP |
1114 | goto end; |
1115 | } else if ((packet_size % 8) != 0) { | |
fdf0e7a0 PP |
1116 | BT_LOGW("Invalid packet size: packet context field indicates packet size is not a multiple of 8: " |
1117 | "notit-addr=%p, packet-context-field-addr=%p, " | |
1118 | "packet-size=%" PRIu64, | |
1119 | notit, notit->dscopes.stream_packet_context, | |
1120 | packet_size); | |
839d52a5 | 1121 | status = BT_NOTIF_ITER_STATUS_ERROR; |
e98a2d6e PP |
1122 | goto end; |
1123 | } | |
1124 | } | |
78586d8a | 1125 | |
e98a2d6e | 1126 | if (content_size_field) { |
839d52a5 | 1127 | int ret = bt_field_unsigned_integer_get_value( |
e98a2d6e | 1128 | content_size_field, &content_size); |
78586d8a | 1129 | |
8b45963b | 1130 | BT_ASSERT(ret == 0); |
e98a2d6e PP |
1131 | } else { |
1132 | content_size = packet_size; | |
1133 | } | |
1134 | ||
fdf0e7a0 PP |
1135 | if (content_size > packet_size) { |
1136 | BT_LOGW("Invalid packet or content size: packet context field indicates content size is greater than packet size: " | |
1137 | "notit-addr=%p, packet-context-field-addr=%p, " | |
1138 | "packet-size=%" PRIu64 ", content-size=%" PRIu64, | |
1139 | notit, notit->dscopes.stream_packet_context, | |
1140 | packet_size, content_size); | |
839d52a5 | 1141 | status = BT_NOTIF_ITER_STATUS_ERROR; |
fdf0e7a0 PP |
1142 | goto end; |
1143 | } | |
1144 | ||
a27839b0 MD |
1145 | if (packet_size != -1ULL) { |
1146 | notit->cur_packet_size = packet_size; | |
1147 | } else { | |
1148 | /* | |
1149 | * Use the content size as packet size indicator if the | |
1150 | * packet size field is missing. This means there is no | |
1151 | * padding in this stream. | |
1152 | */ | |
1153 | notit->cur_packet_size = content_size; | |
1154 | } | |
e98a2d6e | 1155 | notit->cur_content_size = content_size; |
fdf0e7a0 PP |
1156 | BT_LOGV("Set current packet and content sizes: " |
1157 | "notit-addr=%p, packet-size=%" PRIu64 ", content-size=%" PRIu64, | |
1158 | notit, packet_size, content_size); | |
e98a2d6e PP |
1159 | end: |
1160 | BT_PUT(packet_size_field); | |
1161 | BT_PUT(content_size_field); | |
e98a2d6e PP |
1162 | return status; |
1163 | } | |
1164 | ||
1165 | static | |
839d52a5 PP |
1166 | enum bt_notif_iter_status after_packet_context_state( |
1167 | struct bt_notif_iter *notit) | |
e98a2d6e | 1168 | { |
839d52a5 | 1169 | enum bt_notif_iter_status status; |
e98a2d6e PP |
1170 | |
1171 | status = set_current_packet_content_sizes(notit); | |
839d52a5 | 1172 | if (status == BT_NOTIF_ITER_STATUS_OK) { |
e98a2d6e PP |
1173 | notit->state = STATE_EMIT_NOTIF_NEW_PACKET; |
1174 | } | |
1175 | ||
1176 | return status; | |
1177 | } | |
1178 | ||
1179 | static | |
839d52a5 PP |
1180 | enum bt_notif_iter_status read_event_header_begin_state( |
1181 | struct bt_notif_iter *notit) | |
e98a2d6e | 1182 | { |
839d52a5 PP |
1183 | enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; |
1184 | struct bt_field_type *event_header_type = NULL; | |
e98a2d6e | 1185 | |
9b876b7b PP |
1186 | /* Reset the position of the last event header */ |
1187 | notit->buf.last_eh_at = notit->buf.at; | |
1188 | ||
e98a2d6e PP |
1189 | /* Check if we have some content left */ |
1190 | if (notit->cur_content_size >= 0) { | |
1191 | if (packet_at(notit) == notit->cur_content_size) { | |
1192 | /* No more events! */ | |
fdf0e7a0 PP |
1193 | BT_LOGV("Reached end of packet: notit-addr=%p, " |
1194 | "cur=%zu", notit, packet_at(notit)); | |
e98a2d6e PP |
1195 | notit->state = STATE_EMIT_NOTIF_END_OF_PACKET; |
1196 | goto end; | |
1197 | } else if (packet_at(notit) > notit->cur_content_size) { | |
1198 | /* That's not supposed to happen */ | |
fdf0e7a0 | 1199 | BT_LOGV("Before decoding event header field: cursor is passed the packet's content: " |
41342c71 | 1200 | "notit-addr=%p, content-size=%" PRId64 ", " |
fdf0e7a0 PP |
1201 | "cur=%zu", notit, notit->cur_content_size, |
1202 | packet_at(notit)); | |
839d52a5 | 1203 | status = BT_NOTIF_ITER_STATUS_ERROR; |
e98a2d6e PP |
1204 | goto end; |
1205 | } | |
1206 | } | |
1207 | ||
839d52a5 | 1208 | event_header_type = bt_stream_class_get_event_header_type( |
e98a2d6e PP |
1209 | notit->meta.stream_class); |
1210 | if (!event_header_type) { | |
835b2d10 | 1211 | notit->state = STATE_AFTER_STREAM_EVENT_HEADER; |
e98a2d6e PP |
1212 | goto end; |
1213 | } | |
1214 | ||
1215 | put_event_dscopes(notit); | |
fdf0e7a0 PP |
1216 | BT_LOGV("Decoding event header field: " |
1217 | "notit-addr=%p, stream-class-addr=%p, " | |
1218 | "stream-class-name=\"%s\", stream-class-id=%" PRId64 ", " | |
1219 | "ft-addr=%p", | |
1220 | notit, notit->meta.stream_class, | |
839d52a5 PP |
1221 | bt_stream_class_get_name(notit->meta.stream_class), |
1222 | bt_stream_class_get_id(notit->meta.stream_class), | |
fdf0e7a0 | 1223 | event_header_type); |
e98a2d6e PP |
1224 | status = read_dscope_begin_state(notit, event_header_type, |
1225 | STATE_AFTER_STREAM_EVENT_HEADER, | |
1226 | STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE, | |
1227 | ¬it->dscopes.stream_event_header); | |
fdf0e7a0 PP |
1228 | if (status < 0) { |
1229 | BT_LOGW("Cannot decode event header field: " | |
1230 | "notit-addr=%p, stream-class-addr=%p, " | |
1231 | "stream-class-name=\"%s\", " | |
1232 | "stream-class-id=%" PRId64 ", ft-addr=%p", | |
1233 | notit, notit->meta.stream_class, | |
839d52a5 PP |
1234 | bt_stream_class_get_name(notit->meta.stream_class), |
1235 | bt_stream_class_get_id(notit->meta.stream_class), | |
fdf0e7a0 PP |
1236 | event_header_type); |
1237 | } | |
e98a2d6e PP |
1238 | end: |
1239 | BT_PUT(event_header_type); | |
1240 | ||
1241 | return status; | |
1242 | } | |
1243 | ||
1244 | static | |
839d52a5 PP |
1245 | enum bt_notif_iter_status read_event_header_continue_state( |
1246 | struct bt_notif_iter *notit) | |
e98a2d6e PP |
1247 | { |
1248 | return read_dscope_continue_state(notit, | |
1249 | STATE_AFTER_STREAM_EVENT_HEADER); | |
1250 | } | |
1251 | ||
1252 | static inline | |
839d52a5 | 1253 | enum bt_notif_iter_status set_current_event_class(struct bt_notif_iter *notit) |
e98a2d6e PP |
1254 | { |
1255 | /* | |
1256 | * The assert() calls in this function are okay because it is | |
1257 | * assumed here that all the metadata objects have been | |
1258 | * validated for CTF correctness before decoding actual streams. | |
1259 | */ | |
1260 | ||
839d52a5 PP |
1261 | enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; |
1262 | struct bt_field_type *event_header_type; | |
1263 | struct bt_field_type *id_field_type = NULL; | |
1264 | struct bt_field_type *v_field_type = NULL; | |
e98a2d6e PP |
1265 | uint64_t event_id = -1ULL; |
1266 | int ret; | |
1267 | ||
839d52a5 | 1268 | event_header_type = bt_stream_class_get_event_header_type( |
e98a2d6e PP |
1269 | notit->meta.stream_class); |
1270 | if (!event_header_type) { | |
fdf0e7a0 PP |
1271 | /* |
1272 | * No event header, therefore no event class ID field, | |
1273 | * therefore only one event class. | |
1274 | */ | |
1275 | goto single_event_class; | |
e98a2d6e PP |
1276 | } |
1277 | ||
1278 | /* Is there any "id"/"v" field in the event header? */ | |
8b45963b | 1279 | BT_ASSERT(is_struct_type(event_header_type)); |
839d52a5 | 1280 | id_field_type = bt_field_type_structure_get_field_type_by_name( |
e98a2d6e | 1281 | event_header_type, "id"); |
839d52a5 | 1282 | v_field_type = bt_field_type_structure_get_field_type_by_name( |
e98a2d6e | 1283 | event_header_type, "v"); |
8b45963b | 1284 | BT_ASSERT(notit->dscopes.stream_event_header); |
e98a2d6e PP |
1285 | if (v_field_type) { |
1286 | /* | |
1287 | * _ _____ _____ | |
1288 | * | | |_ _|_ _| __ __ _ | |
1289 | * | | | | | || '_ \ / _` | | |
1290 | * | |___| | | || | | | (_| | S P E C I A L | |
1291 | * |_____|_| |_||_| |_|\__, | C A S E ™ | |
1292 | * |___/ | |
1293 | */ | |
839d52a5 PP |
1294 | struct bt_field *v_field = NULL; |
1295 | struct bt_field *v_struct_field = NULL; | |
1296 | struct bt_field *v_struct_id_field = NULL; | |
e98a2d6e PP |
1297 | |
1298 | // TODO: optimalize! | |
839d52a5 | 1299 | v_field = bt_field_structure_get_field_by_name( |
e98a2d6e | 1300 | notit->dscopes.stream_event_header, "v"); |
8b45963b | 1301 | BT_ASSERT(v_field); |
e98a2d6e PP |
1302 | |
1303 | v_struct_field = | |
839d52a5 | 1304 | bt_field_variant_get_current_field(v_field); |
e98a2d6e PP |
1305 | if (!v_struct_field) { |
1306 | goto end_v_field_type; | |
1307 | } | |
1308 | ||
1309 | // TODO: optimalize! | |
1310 | v_struct_id_field = | |
839d52a5 | 1311 | bt_field_structure_get_field_by_name(v_struct_field, "id"); |
e98a2d6e PP |
1312 | if (!v_struct_id_field) { |
1313 | goto end_v_field_type; | |
1314 | } | |
1315 | ||
839d52a5 PP |
1316 | if (bt_field_is_integer(v_struct_id_field)) { |
1317 | ret = bt_field_unsigned_integer_get_value( | |
fdf0e7a0 PP |
1318 | v_struct_id_field, &event_id); |
1319 | if (ret) { | |
1320 | BT_LOGV("Cannot get value of unsigned integer field (`id`): continuing: " | |
1321 | "notit=%p, field-addr=%p", | |
1322 | notit, v_struct_id_field); | |
1323 | event_id = -1ULL; | |
1324 | } | |
e98a2d6e PP |
1325 | } |
1326 | ||
1327 | end_v_field_type: | |
1328 | BT_PUT(v_field); | |
1329 | BT_PUT(v_struct_field); | |
1330 | BT_PUT(v_struct_id_field); | |
1331 | } | |
1332 | ||
1333 | if (id_field_type && event_id == -1ULL) { | |
1334 | /* Check "id" field */ | |
839d52a5 | 1335 | struct bt_field *id_field = NULL; |
0b0bf682 | 1336 | int ret_get_value = 0; |
e98a2d6e PP |
1337 | |
1338 | // TODO: optimalize! | |
839d52a5 | 1339 | id_field = bt_field_structure_get_field_by_name( |
e98a2d6e | 1340 | notit->dscopes.stream_event_header, "id"); |
fdf0e7a0 PP |
1341 | if (!id_field) { |
1342 | goto check_event_id; | |
1343 | } | |
e98a2d6e | 1344 | |
839d52a5 PP |
1345 | if (bt_field_is_integer(id_field)) { |
1346 | ret_get_value = bt_field_unsigned_integer_get_value( | |
e98a2d6e | 1347 | id_field, &event_id); |
839d52a5 PP |
1348 | } else if (bt_field_is_enumeration(id_field)) { |
1349 | struct bt_field *container; | |
e98a2d6e | 1350 | |
839d52a5 | 1351 | container = bt_field_enumeration_get_container( |
e98a2d6e | 1352 | id_field); |
8b45963b | 1353 | BT_ASSERT(container); |
839d52a5 | 1354 | ret_get_value = bt_field_unsigned_integer_get_value( |
e98a2d6e PP |
1355 | container, &event_id); |
1356 | BT_PUT(container); | |
1357 | } | |
fdf0e7a0 | 1358 | |
8b45963b | 1359 | BT_ASSERT(ret_get_value == 0); |
e98a2d6e PP |
1360 | BT_PUT(id_field); |
1361 | } | |
1362 | ||
fdf0e7a0 | 1363 | check_event_id: |
e98a2d6e | 1364 | if (event_id == -1ULL) { |
fdf0e7a0 | 1365 | single_event_class: |
e98a2d6e | 1366 | /* Event ID not found: single event? */ |
8b45963b | 1367 | BT_ASSERT(bt_stream_class_get_event_class_count( |
e98a2d6e PP |
1368 | notit->meta.stream_class) == 1); |
1369 | event_id = 0; | |
1370 | } | |
1371 | ||
fdf0e7a0 PP |
1372 | BT_LOGV("Found event class ID to use: notit-addr=%p, " |
1373 | "stream-class-addr=%p, stream-class-name=\"%s\", " | |
1374 | "stream-class-id=%" PRId64 ", " | |
1375 | "event-class-id=%" PRIu64, | |
1376 | notit, notit->meta.stream_class, | |
839d52a5 PP |
1377 | bt_stream_class_get_name(notit->meta.stream_class), |
1378 | bt_stream_class_get_id(notit->meta.stream_class), | |
fdf0e7a0 | 1379 | event_id); |
e98a2d6e | 1380 | BT_PUT(notit->meta.event_class); |
839d52a5 | 1381 | notit->meta.event_class = bt_stream_class_get_event_class_by_id( |
e98a2d6e PP |
1382 | notit->meta.stream_class, event_id); |
1383 | if (!notit->meta.event_class) { | |
fdf0e7a0 PP |
1384 | BT_LOGW("No event class with ID of event class ID to use in stream class: " |
1385 | "notit-addr=%p, stream-class-addr=%p, " | |
1386 | "stream-class-name=\"%s\", " | |
1387 | "stream-class-id=%" PRId64 ", " | |
1388 | "event-class-id=%" PRIu64, | |
1389 | notit, notit->meta.stream_class, | |
839d52a5 PP |
1390 | bt_stream_class_get_name(notit->meta.stream_class), |
1391 | bt_stream_class_get_id(notit->meta.stream_class), | |
fdf0e7a0 | 1392 | event_id); |
839d52a5 | 1393 | status = BT_NOTIF_ITER_STATUS_ERROR; |
e98a2d6e PP |
1394 | goto end; |
1395 | } | |
1396 | ||
fdf0e7a0 PP |
1397 | BT_LOGV("Set current event class: " |
1398 | "notit-addr=%p, event-class-addr=%p, " | |
1399 | "event-class-name=\"%s\", event-class-id=%" PRId64, | |
1400 | notit, notit->meta.event_class, | |
839d52a5 PP |
1401 | bt_event_class_get_name(notit->meta.event_class), |
1402 | bt_event_class_get_id(notit->meta.event_class)); | |
fdf0e7a0 | 1403 | |
e98a2d6e PP |
1404 | end: |
1405 | BT_PUT(event_header_type); | |
1406 | BT_PUT(id_field_type); | |
1407 | BT_PUT(v_field_type); | |
1408 | ||
1409 | return status; | |
1410 | } | |
1411 | ||
1412 | static | |
839d52a5 PP |
1413 | enum bt_notif_iter_status after_event_header_state( |
1414 | struct bt_notif_iter *notit) | |
e98a2d6e | 1415 | { |
839d52a5 | 1416 | enum bt_notif_iter_status status; |
e98a2d6e | 1417 | |
e98a2d6e | 1418 | status = set_current_event_class(notit); |
839d52a5 | 1419 | if (status != BT_NOTIF_ITER_STATUS_OK) { |
e98a2d6e PP |
1420 | goto end; |
1421 | } | |
1422 | ||
1423 | notit->state = STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN; | |
1424 | ||
1425 | end: | |
1426 | return status; | |
1427 | } | |
1428 | ||
1429 | static | |
839d52a5 PP |
1430 | enum bt_notif_iter_status read_stream_event_context_begin_state( |
1431 | struct bt_notif_iter *notit) | |
e98a2d6e | 1432 | { |
839d52a5 PP |
1433 | enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; |
1434 | struct bt_field_type *stream_event_context_type; | |
e98a2d6e | 1435 | |
839d52a5 | 1436 | stream_event_context_type = bt_stream_class_get_event_context_type( |
e98a2d6e PP |
1437 | notit->meta.stream_class); |
1438 | if (!stream_event_context_type) { | |
835b2d10 | 1439 | notit->state = STATE_DSCOPE_EVENT_CONTEXT_BEGIN; |
e98a2d6e PP |
1440 | goto end; |
1441 | } | |
1442 | ||
fdf0e7a0 PP |
1443 | BT_LOGV("Decoding stream event context field: " |
1444 | "notit-addr=%p, stream-class-addr=%p, " | |
1445 | "stream-class-name=\"%s\", stream-class-id=%" PRId64 ", " | |
1446 | "ft-addr=%p", | |
1447 | notit, notit->meta.stream_class, | |
839d52a5 PP |
1448 | bt_stream_class_get_name(notit->meta.stream_class), |
1449 | bt_stream_class_get_id(notit->meta.stream_class), | |
fdf0e7a0 | 1450 | stream_event_context_type); |
e98a2d6e PP |
1451 | status = read_dscope_begin_state(notit, stream_event_context_type, |
1452 | STATE_DSCOPE_EVENT_CONTEXT_BEGIN, | |
1453 | STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE, | |
1454 | ¬it->dscopes.stream_event_context); | |
fdf0e7a0 PP |
1455 | if (status < 0) { |
1456 | BT_LOGW("Cannot decode stream event context field: " | |
1457 | "notit-addr=%p, stream-class-addr=%p, " | |
1458 | "stream-class-name=\"%s\", " | |
1459 | "stream-class-id=%" PRId64 ", ft-addr=%p", | |
1460 | notit, notit->meta.stream_class, | |
839d52a5 PP |
1461 | bt_stream_class_get_name(notit->meta.stream_class), |
1462 | bt_stream_class_get_id(notit->meta.stream_class), | |
fdf0e7a0 PP |
1463 | stream_event_context_type); |
1464 | } | |
e98a2d6e PP |
1465 | |
1466 | end: | |
1467 | BT_PUT(stream_event_context_type); | |
1468 | ||
1469 | return status; | |
1470 | } | |
1471 | ||
1472 | static | |
839d52a5 PP |
1473 | enum bt_notif_iter_status read_stream_event_context_continue_state( |
1474 | struct bt_notif_iter *notit) | |
e98a2d6e PP |
1475 | { |
1476 | return read_dscope_continue_state(notit, | |
1477 | STATE_DSCOPE_EVENT_CONTEXT_BEGIN); | |
1478 | } | |
1479 | ||
1480 | static | |
839d52a5 PP |
1481 | enum bt_notif_iter_status read_event_context_begin_state( |
1482 | struct bt_notif_iter *notit) | |
e98a2d6e | 1483 | { |
839d52a5 PP |
1484 | enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; |
1485 | struct bt_field_type *event_context_type; | |
e98a2d6e | 1486 | |
839d52a5 | 1487 | event_context_type = bt_event_class_get_context_type( |
e98a2d6e PP |
1488 | notit->meta.event_class); |
1489 | if (!event_context_type) { | |
835b2d10 | 1490 | notit->state = STATE_DSCOPE_EVENT_PAYLOAD_BEGIN; |
e98a2d6e PP |
1491 | goto end; |
1492 | } | |
fdf0e7a0 PP |
1493 | |
1494 | BT_LOGV("Decoding event context field: " | |
1495 | "notit-addr=%p, event-class-addr=%p, " | |
1496 | "event-class-name=\"%s\", event-class-id=%" PRId64 ", " | |
1497 | "ft-addr=%p", | |
1498 | notit, notit->meta.event_class, | |
839d52a5 PP |
1499 | bt_event_class_get_name(notit->meta.event_class), |
1500 | bt_event_class_get_id(notit->meta.event_class), | |
fdf0e7a0 | 1501 | event_context_type); |
e98a2d6e PP |
1502 | status = read_dscope_begin_state(notit, event_context_type, |
1503 | STATE_DSCOPE_EVENT_PAYLOAD_BEGIN, | |
1504 | STATE_DSCOPE_EVENT_CONTEXT_CONTINUE, | |
1505 | ¬it->dscopes.event_context); | |
fdf0e7a0 PP |
1506 | if (status < 0) { |
1507 | BT_LOGW("Cannot decode event context field: " | |
1508 | "notit-addr=%p, event-class-addr=%p, " | |
1509 | "event-class-name=\"%s\", " | |
1510 | "event-class-id=%" PRId64 ", ft-addr=%p", | |
1511 | notit, notit->meta.event_class, | |
839d52a5 PP |
1512 | bt_event_class_get_name(notit->meta.event_class), |
1513 | bt_event_class_get_id(notit->meta.event_class), | |
fdf0e7a0 PP |
1514 | event_context_type); |
1515 | } | |
e98a2d6e PP |
1516 | |
1517 | end: | |
1518 | BT_PUT(event_context_type); | |
1519 | ||
1520 | return status; | |
1521 | } | |
1522 | ||
1523 | static | |
839d52a5 PP |
1524 | enum bt_notif_iter_status read_event_context_continue_state( |
1525 | struct bt_notif_iter *notit) | |
e98a2d6e PP |
1526 | { |
1527 | return read_dscope_continue_state(notit, | |
1528 | STATE_DSCOPE_EVENT_PAYLOAD_BEGIN); | |
1529 | } | |
1530 | ||
1531 | static | |
839d52a5 PP |
1532 | enum bt_notif_iter_status read_event_payload_begin_state( |
1533 | struct bt_notif_iter *notit) | |
e98a2d6e | 1534 | { |
839d52a5 PP |
1535 | enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; |
1536 | struct bt_field_type *event_payload_type; | |
e98a2d6e | 1537 | |
839d52a5 | 1538 | event_payload_type = bt_event_class_get_payload_type( |
e98a2d6e PP |
1539 | notit->meta.event_class); |
1540 | if (!event_payload_type) { | |
835b2d10 | 1541 | notit->state = STATE_EMIT_NOTIF_EVENT; |
e98a2d6e PP |
1542 | goto end; |
1543 | } | |
1544 | ||
fdf0e7a0 PP |
1545 | BT_LOGV("Decoding event payload field: " |
1546 | "notit-addr=%p, event-class-addr=%p, " | |
1547 | "event-class-name=\"%s\", event-class-id=%" PRId64 ", " | |
1548 | "ft-addr=%p", | |
1549 | notit, notit->meta.event_class, | |
839d52a5 PP |
1550 | bt_event_class_get_name(notit->meta.event_class), |
1551 | bt_event_class_get_id(notit->meta.event_class), | |
fdf0e7a0 | 1552 | event_payload_type); |
e98a2d6e PP |
1553 | status = read_dscope_begin_state(notit, event_payload_type, |
1554 | STATE_EMIT_NOTIF_EVENT, | |
1555 | STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE, | |
1556 | ¬it->dscopes.event_payload); | |
fdf0e7a0 PP |
1557 | if (status < 0) { |
1558 | BT_LOGW("Cannot decode event payload field: " | |
1559 | "notit-addr=%p, event-class-addr=%p, " | |
1560 | "event-class-name=\"%s\", " | |
1561 | "event-class-id=%" PRId64 ", ft-addr=%p", | |
1562 | notit, notit->meta.event_class, | |
839d52a5 PP |
1563 | bt_event_class_get_name(notit->meta.event_class), |
1564 | bt_event_class_get_id(notit->meta.event_class), | |
fdf0e7a0 PP |
1565 | event_payload_type); |
1566 | } | |
e98a2d6e PP |
1567 | |
1568 | end: | |
1569 | BT_PUT(event_payload_type); | |
1570 | ||
1571 | return status; | |
1572 | } | |
1573 | ||
1574 | static | |
839d52a5 PP |
1575 | enum bt_notif_iter_status read_event_payload_continue_state( |
1576 | struct bt_notif_iter *notit) | |
e98a2d6e PP |
1577 | { |
1578 | return read_dscope_continue_state(notit, STATE_EMIT_NOTIF_EVENT); | |
1579 | } | |
1580 | ||
1581 | static | |
839d52a5 PP |
1582 | enum bt_notif_iter_status skip_packet_padding_state( |
1583 | struct bt_notif_iter *notit) | |
e98a2d6e | 1584 | { |
839d52a5 | 1585 | enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; |
e98a2d6e PP |
1586 | size_t bits_to_skip; |
1587 | ||
8b45963b | 1588 | BT_ASSERT(notit->cur_packet_size > 0); |
e98a2d6e PP |
1589 | bits_to_skip = notit->cur_packet_size - packet_at(notit); |
1590 | if (bits_to_skip == 0) { | |
1591 | notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN; | |
1592 | goto end; | |
1593 | } else { | |
1594 | size_t bits_to_consume; | |
fdf0e7a0 PP |
1595 | |
1596 | BT_LOGV("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu", | |
1597 | bits_to_skip, notit, bits_to_skip); | |
e98a2d6e | 1598 | status = buf_ensure_available_bits(notit); |
839d52a5 | 1599 | if (status != BT_NOTIF_ITER_STATUS_OK) { |
e98a2d6e PP |
1600 | goto end; |
1601 | } | |
1602 | ||
1603 | bits_to_consume = MIN(buf_available_bits(notit), bits_to_skip); | |
fdf0e7a0 PP |
1604 | BT_LOGV("Skipping %zu bits of padding: notit-addr=%p, size=%zu", |
1605 | bits_to_consume, notit, bits_to_consume); | |
e98a2d6e PP |
1606 | buf_consume_bits(notit, bits_to_consume); |
1607 | bits_to_skip = notit->cur_packet_size - packet_at(notit); | |
1608 | if (bits_to_skip == 0) { | |
1609 | notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN; | |
1610 | goto end; | |
1611 | } | |
1612 | } | |
1613 | ||
1614 | end: | |
1615 | return status; | |
1616 | } | |
1617 | ||
1618 | static inline | |
839d52a5 | 1619 | enum bt_notif_iter_status handle_state(struct bt_notif_iter *notit) |
e98a2d6e | 1620 | { |
839d52a5 | 1621 | enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; |
fdf0e7a0 | 1622 | const enum state state = notit->state; |
e98a2d6e | 1623 | |
fdf0e7a0 PP |
1624 | BT_LOGV("Handling state: notit-addr=%p, state=%s", |
1625 | notit, state_string(state)); | |
e98a2d6e PP |
1626 | |
1627 | // TODO: optimalize! | |
fdf0e7a0 | 1628 | switch (state) { |
e98a2d6e PP |
1629 | case STATE_INIT: |
1630 | notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN; | |
1631 | break; | |
1632 | case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN: | |
1633 | status = read_packet_header_begin_state(notit); | |
1634 | break; | |
1635 | case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE: | |
1636 | status = read_packet_header_continue_state(notit); | |
1637 | break; | |
1638 | case STATE_AFTER_TRACE_PACKET_HEADER: | |
1639 | status = after_packet_header_state(notit); | |
1640 | break; | |
1641 | case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN: | |
1642 | status = read_packet_context_begin_state(notit); | |
1643 | break; | |
1644 | case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE: | |
1645 | status = read_packet_context_continue_state(notit); | |
1646 | break; | |
1647 | case STATE_AFTER_STREAM_PACKET_CONTEXT: | |
1648 | status = after_packet_context_state(notit); | |
1649 | break; | |
1650 | case STATE_EMIT_NOTIF_NEW_PACKET: | |
1651 | notit->state = STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN; | |
1652 | break; | |
1653 | case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN: | |
1654 | status = read_event_header_begin_state(notit); | |
1655 | break; | |
1656 | case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE: | |
1657 | status = read_event_header_continue_state(notit); | |
1658 | break; | |
1659 | case STATE_AFTER_STREAM_EVENT_HEADER: | |
1660 | status = after_event_header_state(notit); | |
1661 | break; | |
1662 | case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN: | |
1663 | status = read_stream_event_context_begin_state(notit); | |
1664 | break; | |
1665 | case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE: | |
1666 | status = read_stream_event_context_continue_state(notit); | |
1667 | break; | |
1668 | case STATE_DSCOPE_EVENT_CONTEXT_BEGIN: | |
1669 | status = read_event_context_begin_state(notit); | |
1670 | break; | |
1671 | case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE: | |
1672 | status = read_event_context_continue_state(notit); | |
1673 | break; | |
1674 | case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN: | |
1675 | status = read_event_payload_begin_state(notit); | |
1676 | break; | |
1677 | case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE: | |
1678 | status = read_event_payload_continue_state(notit); | |
1679 | break; | |
1680 | case STATE_EMIT_NOTIF_EVENT: | |
1681 | notit->state = STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN; | |
1682 | break; | |
1683 | case STATE_SKIP_PACKET_PADDING: | |
1684 | status = skip_packet_padding_state(notit); | |
1685 | break; | |
1686 | case STATE_EMIT_NOTIF_END_OF_PACKET: | |
1687 | notit->state = STATE_SKIP_PACKET_PADDING; | |
1688 | break; | |
fdf0e7a0 PP |
1689 | default: |
1690 | BT_LOGD("Unknown CTF plugin notification iterator state: " | |
1691 | "notit-addr=%p, state=%d", notit, notit->state); | |
1692 | abort(); | |
e98a2d6e PP |
1693 | } |
1694 | ||
fdf0e7a0 PP |
1695 | BT_LOGV("Handled state: notit-addr=%p, status=%s, " |
1696 | "prev-state=%s, cur-state=%s", | |
839d52a5 | 1697 | notit, bt_notif_iter_status_string(status), |
fdf0e7a0 | 1698 | state_string(state), state_string(notit->state)); |
e98a2d6e PP |
1699 | return status; |
1700 | } | |
1701 | ||
2cf1d51e JG |
1702 | /** |
1703 | * Resets the internal state of a CTF notification iterator. | |
2cf1d51e JG |
1704 | */ |
1705 | static | |
839d52a5 | 1706 | void bt_notif_iter_reset(struct bt_notif_iter *notit) |
e98a2d6e | 1707 | { |
8b45963b | 1708 | BT_ASSERT(notit); |
fdf0e7a0 | 1709 | BT_LOGD("Resetting notification iterator: addr=%p", notit); |
e98a2d6e PP |
1710 | stack_clear(notit->stack); |
1711 | BT_PUT(notit->meta.stream_class); | |
1712 | BT_PUT(notit->meta.event_class); | |
1713 | BT_PUT(notit->packet); | |
5184a1d6 | 1714 | BT_PUT(notit->stream); |
e98a2d6e PP |
1715 | put_all_dscopes(notit); |
1716 | notit->buf.addr = NULL; | |
1717 | notit->buf.sz = 0; | |
1718 | notit->buf.at = 0; | |
bb8744a7 | 1719 | notit->buf.last_eh_at = SIZE_MAX; |
e98a2d6e PP |
1720 | notit->buf.packet_offset = 0; |
1721 | notit->state = STATE_INIT; | |
1722 | notit->cur_content_size = -1; | |
1723 | notit->cur_packet_size = -1; | |
bb230c9b | 1724 | notit->cur_packet_offset = -1; |
e98a2d6e PP |
1725 | } |
1726 | ||
2cf1d51e | 1727 | static |
839d52a5 | 1728 | int bt_notif_iter_switch_packet(struct bt_notif_iter *notit) |
2cf1d51e JG |
1729 | { |
1730 | int ret = 0; | |
1731 | ||
c937c615 PP |
1732 | /* |
1733 | * We don't put the stream class here because we need to make | |
1734 | * sure that all the packets processed by the same notification | |
1735 | * iterator refer to the same stream class (the first one). | |
1736 | */ | |
8b45963b | 1737 | BT_ASSERT(notit); |
bb230c9b JG |
1738 | if (notit->cur_packet_size != -1) { |
1739 | notit->cur_packet_offset += notit->cur_packet_size; | |
1740 | } | |
1741 | BT_LOGV("Switching packet: notit-addr=%p, cur=%zu, " | |
1742 | "packet-offset=%" PRId64, notit, notit->buf.at, | |
1743 | notit->cur_packet_offset); | |
2cf1d51e | 1744 | stack_clear(notit->stack); |
2cf1d51e JG |
1745 | BT_PUT(notit->meta.event_class); |
1746 | BT_PUT(notit->packet); | |
5f870343 | 1747 | BT_PUT(notit->cur_timestamp_end); |
2cf1d51e JG |
1748 | put_all_dscopes(notit); |
1749 | ||
1750 | /* | |
1751 | * Adjust current buffer so that addr points to the beginning of the new | |
1752 | * packet. | |
1753 | */ | |
1754 | if (notit->buf.addr) { | |
1755 | size_t consumed_bytes = (size_t) (notit->buf.at / CHAR_BIT); | |
1756 | ||
1757 | /* Packets are assumed to start on a byte frontier. */ | |
1758 | if (notit->buf.at % CHAR_BIT) { | |
fdf0e7a0 PP |
1759 | BT_LOGW("Cannot switch packet: current position is not a multiple of 8: " |
1760 | "notit-addr=%p, cur=%zu", notit, notit->buf.at); | |
2cf1d51e JG |
1761 | ret = -1; |
1762 | goto end; | |
1763 | } | |
1764 | ||
1765 | notit->buf.addr += consumed_bytes; | |
1766 | notit->buf.sz -= consumed_bytes; | |
1767 | notit->buf.at = 0; | |
1768 | notit->buf.packet_offset = 0; | |
fdf0e7a0 PP |
1769 | BT_LOGV("Adjusted buffer: addr=%p, size=%zu", |
1770 | notit->buf.addr, notit->buf.sz); | |
2cf1d51e JG |
1771 | } |
1772 | ||
1773 | notit->cur_content_size = -1; | |
1774 | notit->cur_packet_size = -1; | |
5f870343 | 1775 | notit->cur_sc_field_path_cache = NULL; |
fdf0e7a0 | 1776 | |
2cf1d51e JG |
1777 | end: |
1778 | return ret; | |
1779 | } | |
1780 | ||
e98a2d6e | 1781 | static |
839d52a5 | 1782 | struct bt_field *get_next_field(struct bt_notif_iter *notit) |
e98a2d6e | 1783 | { |
839d52a5 PP |
1784 | struct bt_field *next_field = NULL; |
1785 | struct bt_field *base_field; | |
1786 | struct bt_field_type *base_type; | |
e98a2d6e PP |
1787 | size_t index; |
1788 | ||
8b45963b | 1789 | BT_ASSERT(!stack_empty(notit->stack)); |
e98a2d6e PP |
1790 | index = stack_top(notit->stack)->index; |
1791 | base_field = stack_top(notit->stack)->base; | |
8b45963b | 1792 | BT_ASSERT(base_field); |
839d52a5 | 1793 | base_type = bt_field_get_type(base_field); |
8b45963b | 1794 | BT_ASSERT(base_type); |
e98a2d6e | 1795 | |
839d52a5 PP |
1796 | switch (bt_field_type_get_type_id(base_type)) { |
1797 | case BT_FIELD_TYPE_ID_STRUCT: | |
1798 | next_field = bt_field_structure_get_field_by_index( | |
e98a2d6e PP |
1799 | base_field, index); |
1800 | break; | |
839d52a5 PP |
1801 | case BT_FIELD_TYPE_ID_ARRAY: |
1802 | next_field = bt_field_array_get_field(base_field, index); | |
e98a2d6e | 1803 | break; |
839d52a5 PP |
1804 | case BT_FIELD_TYPE_ID_SEQUENCE: |
1805 | next_field = bt_field_sequence_get_field(base_field, index); | |
e98a2d6e | 1806 | break; |
839d52a5 PP |
1807 | case BT_FIELD_TYPE_ID_VARIANT: |
1808 | next_field = bt_field_variant_get_current_field(base_field); | |
e98a2d6e PP |
1809 | break; |
1810 | default: | |
fdf0e7a0 PP |
1811 | BT_LOGF("Unknown base field type ID: " |
1812 | "notit-addr=%p, ft-addr=%p, ft-id=%s", | |
1813 | notit, base_type, | |
839d52a5 PP |
1814 | bt_field_type_id_string( |
1815 | bt_field_type_get_type_id(base_type))); | |
0fbb9a9f | 1816 | abort(); |
e98a2d6e PP |
1817 | } |
1818 | ||
e98a2d6e | 1819 | BT_PUT(base_type); |
e98a2d6e PP |
1820 | return next_field; |
1821 | } | |
1822 | ||
c44c3e70 JG |
1823 | static |
1824 | void update_clock_state(uint64_t *state, | |
839d52a5 | 1825 | struct bt_field *value_field) |
c44c3e70 | 1826 | { |
839d52a5 | 1827 | struct bt_field_type *value_type = NULL; |
c44c3e70 JG |
1828 | uint64_t requested_new_value; |
1829 | uint64_t requested_new_value_mask; | |
1830 | uint64_t cur_value_masked; | |
1831 | int requested_new_value_size; | |
1832 | int ret; | |
1833 | ||
839d52a5 | 1834 | value_type = bt_field_get_type(value_field); |
8b45963b PP |
1835 | BT_ASSERT(value_type); |
1836 | BT_ASSERT(bt_field_type_is_integer(value_type)); | |
c44c3e70 | 1837 | requested_new_value_size = |
839d52a5 | 1838 | bt_field_type_integer_get_size(value_type); |
8b45963b | 1839 | BT_ASSERT(requested_new_value_size > 0); |
839d52a5 | 1840 | ret = bt_field_unsigned_integer_get_value(value_field, |
c44c3e70 | 1841 | &requested_new_value); |
8b45963b | 1842 | BT_ASSERT(!ret); |
c44c3e70 JG |
1843 | |
1844 | /* | |
1845 | * Special case for a 64-bit new value, which is the limit | |
1846 | * of a clock value as of this version: overwrite the | |
1847 | * current value directly. | |
1848 | */ | |
1849 | if (requested_new_value_size == 64) { | |
1850 | *state = requested_new_value; | |
1851 | goto end; | |
1852 | } | |
1853 | ||
1854 | requested_new_value_mask = (1ULL << requested_new_value_size) - 1; | |
1855 | cur_value_masked = *state & requested_new_value_mask; | |
1856 | ||
1857 | if (requested_new_value < cur_value_masked) { | |
1858 | /* | |
1859 | * It looks like a wrap happened on the number of bits | |
1860 | * of the requested new value. Assume that the clock | |
1861 | * value wrapped only one time. | |
1862 | */ | |
1863 | *state += requested_new_value_mask + 1; | |
1864 | } | |
1865 | ||
1866 | /* Clear the low bits of the current clock value. */ | |
1867 | *state &= ~requested_new_value_mask; | |
1868 | ||
1869 | /* Set the low bits of the current clock value. */ | |
1870 | *state |= requested_new_value; | |
1871 | end: | |
fdf0e7a0 PP |
1872 | BT_LOGV("Updated clock's value from integer field's value: " |
1873 | "value=%" PRIu64, *state); | |
c44c3e70 JG |
1874 | bt_put(value_type); |
1875 | } | |
1876 | ||
1877 | static | |
839d52a5 PP |
1878 | enum bt_btr_status update_clock(struct bt_notif_iter *notit, |
1879 | struct bt_field *int_field) | |
c44c3e70 | 1880 | { |
ac0c6bdd | 1881 | gboolean clock_class_found; |
501a8569 | 1882 | uint64_t *clock_state = NULL; |
839d52a5 PP |
1883 | struct bt_field_type *int_field_type = NULL; |
1884 | enum bt_btr_status ret = BT_BTR_STATUS_OK; | |
1885 | struct bt_clock_class *clock_class = NULL; | |
c44c3e70 | 1886 | |
839d52a5 | 1887 | int_field_type = bt_field_get_type(int_field); |
8b45963b | 1888 | BT_ASSERT(int_field_type); |
839d52a5 | 1889 | clock_class = bt_field_type_integer_get_mapped_clock_class( |
f45bfe88 | 1890 | int_field_type); |
ac0c6bdd | 1891 | if (likely(!clock_class)) { |
f45bfe88 | 1892 | goto end; |
c44c3e70 JG |
1893 | } |
1894 | ||
ac0c6bdd | 1895 | clock_class_found = g_hash_table_lookup_extended(notit->clock_states, |
fdf0e7a0 | 1896 | clock_class, NULL, (gpointer) &clock_state); |
501a8569 | 1897 | if (!clock_class_found) { |
c44c3e70 JG |
1898 | clock_state = g_new0(uint64_t, 1); |
1899 | if (!clock_state) { | |
fdf0e7a0 | 1900 | BT_LOGE_STR("Failed to allocate a uint64_t."); |
839d52a5 | 1901 | ret = BT_BTR_STATUS_ENOMEM; |
c44c3e70 JG |
1902 | goto end; |
1903 | } | |
fdf0e7a0 | 1904 | |
ac0c6bdd | 1905 | g_hash_table_insert(notit->clock_states, bt_get(clock_class), |
fdf0e7a0 | 1906 | clock_state); |
c44c3e70 JG |
1907 | } |
1908 | ||
1909 | /* Update the clock's state. */ | |
fdf0e7a0 PP |
1910 | BT_LOGV("Updating notification iterator's clock's value from integer field: " |
1911 | "notit-addr=%p, clock-class-addr=%p, " | |
1912 | "clock-class-name=\"%s\", value=%" PRIu64, | |
1913 | notit, clock_class, | |
839d52a5 | 1914 | bt_clock_class_get_name(clock_class), *clock_state); |
c44c3e70 JG |
1915 | update_clock_state(clock_state, int_field); |
1916 | end: | |
f45bfe88 | 1917 | bt_put(int_field_type); |
ac0c6bdd | 1918 | bt_put(clock_class); |
c44c3e70 JG |
1919 | return ret; |
1920 | } | |
1921 | ||
e98a2d6e | 1922 | static |
839d52a5 PP |
1923 | enum bt_btr_status btr_unsigned_int_common(uint64_t value, |
1924 | struct bt_field_type *type, void *data, | |
1925 | struct bt_field **out_int_field) | |
1926 | { | |
1927 | enum bt_btr_status status = BT_BTR_STATUS_OK; | |
1928 | struct bt_field *field = NULL; | |
1929 | struct bt_field *int_field = NULL; | |
1930 | struct bt_notif_iter *notit = data; | |
e98a2d6e PP |
1931 | int ret; |
1932 | ||
fdf0e7a0 PP |
1933 | BT_LOGV("Common unsigned integer function called from BTR: " |
1934 | "notit-addr=%p, btr-addr=%p, ft-addr=%p, " | |
1935 | "ft-id=%s, value=%" PRIu64, | |
1936 | notit, notit->btr, type, | |
839d52a5 PP |
1937 | bt_field_type_id_string( |
1938 | bt_field_type_get_type_id(type)), | |
fdf0e7a0 PP |
1939 | value); |
1940 | ||
5f870343 | 1941 | /* Create next field */ |
e98a2d6e PP |
1942 | field = get_next_field(notit); |
1943 | if (!field) { | |
fdf0e7a0 | 1944 | BT_LOGW("Cannot get next field: notit-addr=%p", notit); |
839d52a5 | 1945 | status = BT_BTR_STATUS_ERROR; |
c44c3e70 | 1946 | goto end_no_put; |
e98a2d6e PP |
1947 | } |
1948 | ||
839d52a5 PP |
1949 | switch(bt_field_type_get_type_id(type)) { |
1950 | case BT_FIELD_TYPE_ID_INTEGER: | |
e98a2d6e PP |
1951 | /* Integer field is created field */ |
1952 | BT_MOVE(int_field, field); | |
c44c3e70 | 1953 | bt_get(type); |
e98a2d6e | 1954 | break; |
839d52a5 PP |
1955 | case BT_FIELD_TYPE_ID_ENUM: |
1956 | int_field = bt_field_enumeration_get_container(field); | |
8b45963b | 1957 | BT_ASSERT(int_field); |
839d52a5 | 1958 | type = bt_field_get_type(int_field); |
8b45963b | 1959 | BT_ASSERT(type); |
e98a2d6e PP |
1960 | break; |
1961 | default: | |
fdf0e7a0 PP |
1962 | BT_LOGF("Unexpected field type ID: " |
1963 | "notit-addr=%p, ft-addr=%p, ft-id=%s", | |
1964 | notit, type, | |
839d52a5 PP |
1965 | bt_field_type_id_string( |
1966 | bt_field_type_get_type_id(type))); | |
0fbb9a9f | 1967 | abort(); |
e98a2d6e PP |
1968 | } |
1969 | ||
8b45963b | 1970 | BT_ASSERT(int_field); |
839d52a5 | 1971 | ret = bt_field_unsigned_integer_set_value(int_field, value); |
8b45963b | 1972 | BT_ASSERT(ret == 0); |
e98a2d6e | 1973 | stack_top(notit->stack)->index++; |
f45bfe88 | 1974 | *out_int_field = int_field; |
e98a2d6e | 1975 | BT_PUT(field); |
c44c3e70 | 1976 | BT_PUT(type); |
fdf0e7a0 | 1977 | |
c44c3e70 | 1978 | end_no_put: |
e98a2d6e PP |
1979 | return status; |
1980 | } | |
1981 | ||
5f870343 | 1982 | static |
839d52a5 PP |
1983 | enum bt_btr_status btr_timestamp_end_cb(void *value, |
1984 | struct bt_field_type *type, void *data) | |
5f870343 | 1985 | { |
839d52a5 PP |
1986 | enum bt_btr_status status; |
1987 | struct bt_field *field = NULL; | |
1988 | struct bt_notif_iter *notit = data; | |
5f870343 | 1989 | |
fdf0e7a0 PP |
1990 | BT_LOGV("`timestamp_end` unsigned integer function called from BTR: " |
1991 | "notit-addr=%p, btr-addr=%p, ft-addr=%p, " | |
1992 | "ft-id=%s", | |
1993 | notit, notit->btr, type, | |
839d52a5 PP |
1994 | bt_field_type_id_string( |
1995 | bt_field_type_get_type_id(type))); | |
5f870343 JG |
1996 | status = btr_unsigned_int_common(*((uint64_t *) value), type, data, |
1997 | &field); | |
1998 | ||
1999 | /* Set as the current packet's timestamp_end field. */ | |
2000 | BT_MOVE(notit->cur_timestamp_end, field); | |
2001 | return status; | |
2002 | } | |
2003 | ||
e98a2d6e | 2004 | static |
839d52a5 PP |
2005 | enum bt_btr_status btr_unsigned_int_cb(uint64_t value, |
2006 | struct bt_field_type *type, void *data) | |
5f870343 | 2007 | { |
839d52a5 PP |
2008 | struct bt_notif_iter *notit = data; |
2009 | enum bt_btr_status status = BT_BTR_STATUS_OK; | |
2010 | struct bt_field *field = NULL; | |
5f870343 JG |
2011 | struct field_cb_override *override; |
2012 | ||
fdf0e7a0 PP |
2013 | BT_LOGV("Unsigned integer function called from BTR: " |
2014 | "notit-addr=%p, btr-addr=%p, ft-addr=%p, " | |
2015 | "ft-id=%s, value=%" PRIu64, | |
2016 | notit, notit->btr, type, | |
839d52a5 PP |
2017 | bt_field_type_id_string( |
2018 | bt_field_type_get_type_id(type)), | |
fdf0e7a0 PP |
2019 | value); |
2020 | override = g_hash_table_lookup(notit->field_overrides, type); | |
5f870343 | 2021 | if (unlikely(override)) { |
fdf0e7a0 | 2022 | /* Override function logs errors */ |
5f870343 JG |
2023 | status = override->func(&value, type, override->data); |
2024 | goto end; | |
2025 | } | |
2026 | ||
2027 | status = btr_unsigned_int_common(value, type, data, &field); | |
839d52a5 | 2028 | if (status != BT_BTR_STATUS_OK) { |
fdf0e7a0 | 2029 | /* btr_unsigned_int_common() logs errors */ |
5f870343 JG |
2030 | goto end; |
2031 | } | |
2032 | ||
f45bfe88 | 2033 | status = update_clock(notit, field); |
5f870343 JG |
2034 | BT_PUT(field); |
2035 | end: | |
2036 | return status; | |
2037 | } | |
2038 | ||
2039 | static | |
839d52a5 PP |
2040 | enum bt_btr_status btr_signed_int_cb(int64_t value, |
2041 | struct bt_field_type *type, void *data) | |
e98a2d6e | 2042 | { |
839d52a5 PP |
2043 | enum bt_btr_status status = BT_BTR_STATUS_OK; |
2044 | struct bt_field *field = NULL; | |
2045 | struct bt_field *int_field = NULL; | |
2046 | struct bt_notif_iter *notit = data; | |
e98a2d6e PP |
2047 | int ret; |
2048 | ||
fdf0e7a0 PP |
2049 | BT_LOGV("Signed integer function called from BTR: " |
2050 | "notit-addr=%p, btr-addr=%p, ft-addr=%p, " | |
2051 | "ft-id=%s, value=%" PRId64, | |
2052 | notit, notit->btr, type, | |
839d52a5 PP |
2053 | bt_field_type_id_string( |
2054 | bt_field_type_get_type_id(type)), | |
fdf0e7a0 PP |
2055 | value); |
2056 | ||
5f870343 | 2057 | /* create next field */ |
e98a2d6e PP |
2058 | field = get_next_field(notit); |
2059 | if (!field) { | |
fdf0e7a0 | 2060 | BT_LOGW("Cannot get next field: notit-addr=%p", notit); |
839d52a5 | 2061 | status = BT_BTR_STATUS_ERROR; |
c44c3e70 | 2062 | goto end_no_put; |
e98a2d6e PP |
2063 | } |
2064 | ||
839d52a5 PP |
2065 | switch(bt_field_type_get_type_id(type)) { |
2066 | case BT_FIELD_TYPE_ID_INTEGER: | |
e98a2d6e PP |
2067 | /* Integer field is created field */ |
2068 | BT_MOVE(int_field, field); | |
c44c3e70 | 2069 | bt_get(type); |
e98a2d6e | 2070 | break; |
839d52a5 PP |
2071 | case BT_FIELD_TYPE_ID_ENUM: |
2072 | int_field = bt_field_enumeration_get_container(field); | |
8b45963b | 2073 | BT_ASSERT(int_field); |
839d52a5 | 2074 | type = bt_field_get_type(int_field); |
8b45963b | 2075 | BT_ASSERT(type); |
e98a2d6e PP |
2076 | break; |
2077 | default: | |
fdf0e7a0 PP |
2078 | BT_LOGF("Unexpected field type ID: " |
2079 | "notit-addr=%p, ft-addr=%p, ft-id=%s", | |
2080 | notit, type, | |
839d52a5 PP |
2081 | bt_field_type_id_string( |
2082 | bt_field_type_get_type_id(type))); | |
0fbb9a9f | 2083 | abort(); |
e98a2d6e PP |
2084 | } |
2085 | ||
8b45963b | 2086 | BT_ASSERT(int_field); |
839d52a5 | 2087 | ret = bt_field_signed_integer_set_value(int_field, value); |
8b45963b | 2088 | BT_ASSERT(!ret); |
e98a2d6e | 2089 | stack_top(notit->stack)->index++; |
f45bfe88 | 2090 | status = update_clock(notit, int_field); |
e98a2d6e PP |
2091 | BT_PUT(field); |
2092 | BT_PUT(int_field); | |
c44c3e70 | 2093 | BT_PUT(type); |
fdf0e7a0 | 2094 | |
c44c3e70 | 2095 | end_no_put: |
e98a2d6e PP |
2096 | return status; |
2097 | } | |
2098 | ||
2099 | static | |
839d52a5 PP |
2100 | enum bt_btr_status btr_floating_point_cb(double value, |
2101 | struct bt_field_type *type, void *data) | |
e98a2d6e | 2102 | { |
839d52a5 PP |
2103 | enum bt_btr_status status = BT_BTR_STATUS_OK; |
2104 | struct bt_field *field = NULL; | |
2105 | struct bt_notif_iter *notit = data; | |
e98a2d6e PP |
2106 | int ret; |
2107 | ||
fdf0e7a0 PP |
2108 | BT_LOGV("Floating point number function called from BTR: " |
2109 | "notit-addr=%p, btr-addr=%p, ft-addr=%p, " | |
2110 | "ft-id=%s, value=%f", | |
2111 | notit, notit->btr, type, | |
839d52a5 PP |
2112 | bt_field_type_id_string( |
2113 | bt_field_type_get_type_id(type)), | |
fdf0e7a0 PP |
2114 | value); |
2115 | ||
e98a2d6e PP |
2116 | /* Create next field */ |
2117 | field = get_next_field(notit); | |
2118 | if (!field) { | |
fdf0e7a0 | 2119 | BT_LOGW("Cannot get next field: notit-addr=%p", notit); |
839d52a5 | 2120 | status = BT_BTR_STATUS_ERROR; |
e98a2d6e PP |
2121 | goto end; |
2122 | } | |
2123 | ||
839d52a5 | 2124 | ret = bt_field_floating_point_set_value(field, value); |
8b45963b | 2125 | BT_ASSERT(!ret); |
e98a2d6e PP |
2126 | stack_top(notit->stack)->index++; |
2127 | ||
2128 | end: | |
2129 | BT_PUT(field); | |
e98a2d6e PP |
2130 | return status; |
2131 | } | |
2132 | ||
2133 | static | |
839d52a5 PP |
2134 | enum bt_btr_status btr_string_begin_cb( |
2135 | struct bt_field_type *type, void *data) | |
e98a2d6e | 2136 | { |
839d52a5 PP |
2137 | enum bt_btr_status status = BT_BTR_STATUS_OK; |
2138 | struct bt_field *field = NULL; | |
2139 | struct bt_notif_iter *notit = data; | |
e98a2d6e PP |
2140 | int ret; |
2141 | ||
fdf0e7a0 PP |
2142 | BT_LOGV("String (beginning) function called from BTR: " |
2143 | "notit-addr=%p, btr-addr=%p, ft-addr=%p, " | |
2144 | "ft-id=%s", | |
2145 | notit, notit->btr, type, | |
839d52a5 PP |
2146 | bt_field_type_id_string( |
2147 | bt_field_type_get_type_id(type))); | |
fdf0e7a0 | 2148 | |
e98a2d6e PP |
2149 | /* Create next field */ |
2150 | field = get_next_field(notit); | |
2151 | if (!field) { | |
fdf0e7a0 | 2152 | BT_LOGW("Cannot get next field: notit-addr=%p", notit); |
839d52a5 | 2153 | status = BT_BTR_STATUS_ERROR; |
e98a2d6e PP |
2154 | goto end; |
2155 | } | |
2156 | ||
2157 | /* | |
2158 | * Push on stack. Not a compound type per se, but we know that only | |
2159 | * btr_string_cb() may be called between this call and a subsequent | |
2160 | * call to btr_string_end_cb(). | |
2161 | */ | |
2162 | ret = stack_push(notit->stack, field); | |
2163 | if (ret) { | |
fdf0e7a0 PP |
2164 | BT_LOGE("Cannot push string field on stack: " |
2165 | "notit-addr=%p, field-addr=%p", notit, field); | |
839d52a5 | 2166 | status = BT_BTR_STATUS_ERROR; |
e98a2d6e PP |
2167 | goto end; |
2168 | } | |
2169 | ||
e5df2ae3 JG |
2170 | /* |
2171 | * Initialize string field payload to an empty string since in the | |
2172 | * case of a length 0 string the btr_string_cb won't be called and | |
2173 | * we will end up with an unset string payload. | |
2174 | */ | |
839d52a5 | 2175 | ret = bt_field_string_set_value(field, ""); |
e5df2ae3 | 2176 | if (ret) { |
fdf0e7a0 PP |
2177 | BT_LOGE("Cannot initialize string field's value to an empty string: " |
2178 | "notit-addr=%p, field-addr=%p, ret=%d", | |
2179 | notit, field, ret); | |
839d52a5 | 2180 | status = BT_BTR_STATUS_ERROR; |
e5df2ae3 JG |
2181 | goto end; |
2182 | } | |
2183 | ||
e98a2d6e PP |
2184 | end: |
2185 | BT_PUT(field); | |
2186 | ||
2187 | return status; | |
2188 | } | |
2189 | ||
2190 | static | |
839d52a5 PP |
2191 | enum bt_btr_status btr_string_cb(const char *value, |
2192 | size_t len, struct bt_field_type *type, void *data) | |
e98a2d6e | 2193 | { |
839d52a5 PP |
2194 | enum bt_btr_status status = BT_BTR_STATUS_OK; |
2195 | struct bt_field *field = NULL; | |
2196 | struct bt_notif_iter *notit = data; | |
e98a2d6e PP |
2197 | int ret; |
2198 | ||
fdf0e7a0 PP |
2199 | BT_LOGV("String (substring) function called from BTR: " |
2200 | "notit-addr=%p, btr-addr=%p, ft-addr=%p, " | |
2201 | "ft-id=%s, string-length=%zu", | |
2202 | notit, notit->btr, type, | |
839d52a5 PP |
2203 | bt_field_type_id_string( |
2204 | bt_field_type_get_type_id(type)), | |
fdf0e7a0 PP |
2205 | len); |
2206 | ||
e98a2d6e PP |
2207 | /* Get string field */ |
2208 | field = stack_top(notit->stack)->base; | |
8b45963b | 2209 | BT_ASSERT(field); |
e98a2d6e PP |
2210 | |
2211 | /* Append current string */ | |
839d52a5 | 2212 | ret = bt_field_string_append_len(field, value, len); |
e98a2d6e | 2213 | if (ret) { |
fdf0e7a0 PP |
2214 | BT_LOGE("Cannot append substring to string field's value: " |
2215 | "notit-addr=%p, field-addr=%p, string-length=%zu, " | |
2216 | "ret=%d", notit, field, len, ret); | |
839d52a5 | 2217 | status = BT_BTR_STATUS_ERROR; |
e98a2d6e PP |
2218 | goto end; |
2219 | } | |
2220 | ||
2221 | end: | |
2222 | return status; | |
2223 | } | |
2224 | ||
2225 | static | |
839d52a5 PP |
2226 | enum bt_btr_status btr_string_end_cb( |
2227 | struct bt_field_type *type, void *data) | |
e98a2d6e | 2228 | { |
839d52a5 | 2229 | struct bt_notif_iter *notit = data; |
e98a2d6e | 2230 | |
fdf0e7a0 PP |
2231 | BT_LOGV("String (end) function called from BTR: " |
2232 | "notit-addr=%p, btr-addr=%p, ft-addr=%p, " | |
2233 | "ft-id=%s", | |
2234 | notit, notit->btr, type, | |
839d52a5 PP |
2235 | bt_field_type_id_string( |
2236 | bt_field_type_get_type_id(type))); | |
fdf0e7a0 | 2237 | |
e98a2d6e PP |
2238 | /* Pop string field */ |
2239 | stack_pop(notit->stack); | |
2240 | ||
2241 | /* Go to next field */ | |
2242 | stack_top(notit->stack)->index++; | |
839d52a5 | 2243 | return BT_BTR_STATUS_OK; |
e98a2d6e PP |
2244 | } |
2245 | ||
839d52a5 PP |
2246 | enum bt_btr_status btr_compound_begin_cb( |
2247 | struct bt_field_type *type, void *data) | |
e98a2d6e | 2248 | { |
839d52a5 PP |
2249 | enum bt_btr_status status = BT_BTR_STATUS_OK; |
2250 | struct bt_notif_iter *notit = data; | |
2251 | struct bt_field *field; | |
e98a2d6e PP |
2252 | int ret; |
2253 | ||
fdf0e7a0 PP |
2254 | BT_LOGV("Compound (beginning) function called from BTR: " |
2255 | "notit-addr=%p, btr-addr=%p, ft-addr=%p, " | |
2256 | "ft-id=%s", | |
2257 | notit, notit->btr, type, | |
839d52a5 PP |
2258 | bt_field_type_id_string( |
2259 | bt_field_type_get_type_id(type))); | |
fdf0e7a0 | 2260 | |
e98a2d6e PP |
2261 | /* Create field */ |
2262 | if (stack_empty(notit->stack)) { | |
2263 | /* Root: create dynamic scope field */ | |
839d52a5 | 2264 | *notit->cur_dscope_field = bt_field_create(type); |
e98a2d6e PP |
2265 | field = *notit->cur_dscope_field; |
2266 | ||
2267 | /* | |
2268 | * Field will be put at the end of this function | |
2269 | * (stack_push() will take one reference, but this | |
2270 | * reference is lost upon the equivalent stack_pop() | |
2271 | * later), so also get it for our context to own it. | |
2272 | */ | |
2273 | bt_get(*notit->cur_dscope_field); | |
fdf0e7a0 PP |
2274 | |
2275 | if (!field) { | |
2276 | BT_LOGE("Cannot create compound field: " | |
2277 | "notit-addr=%p, ft-addr=%p, ft-id=%s", | |
2278 | notit, type, | |
839d52a5 PP |
2279 | bt_field_type_id_string( |
2280 | bt_field_type_get_type_id(type))); | |
2281 | status = BT_BTR_STATUS_ERROR; | |
fdf0e7a0 PP |
2282 | goto end; |
2283 | } | |
e98a2d6e PP |
2284 | } else { |
2285 | field = get_next_field(notit); | |
fdf0e7a0 PP |
2286 | if (!field) { |
2287 | BT_LOGW("Cannot get next field: notit-addr=%p", notit); | |
839d52a5 | 2288 | status = BT_BTR_STATUS_ERROR; |
fdf0e7a0 PP |
2289 | goto end; |
2290 | } | |
e98a2d6e PP |
2291 | } |
2292 | ||
2293 | /* Push field */ | |
8b45963b | 2294 | BT_ASSERT(field); |
e98a2d6e PP |
2295 | ret = stack_push(notit->stack, field); |
2296 | if (ret) { | |
fdf0e7a0 PP |
2297 | BT_LOGE("Cannot push compound field onto the stack: " |
2298 | "notit-addr=%p, ft-addr=%p, ft-id=%s, ret=%d", | |
2299 | notit, type, | |
839d52a5 PP |
2300 | bt_field_type_id_string( |
2301 | bt_field_type_get_type_id(type)), | |
fdf0e7a0 | 2302 | ret); |
839d52a5 | 2303 | status = BT_BTR_STATUS_ERROR; |
e98a2d6e PP |
2304 | goto end; |
2305 | } | |
2306 | ||
2307 | end: | |
2308 | BT_PUT(field); | |
2309 | ||
2310 | return status; | |
2311 | } | |
2312 | ||
839d52a5 PP |
2313 | enum bt_btr_status btr_compound_end_cb( |
2314 | struct bt_field_type *type, void *data) | |
e98a2d6e | 2315 | { |
839d52a5 | 2316 | struct bt_notif_iter *notit = data; |
e98a2d6e | 2317 | |
fdf0e7a0 PP |
2318 | BT_LOGV("Compound (end) function called from BTR: " |
2319 | "notit-addr=%p, btr-addr=%p, ft-addr=%p, " | |
2320 | "ft-id=%s", | |
2321 | notit, notit->btr, type, | |
839d52a5 PP |
2322 | bt_field_type_id_string( |
2323 | bt_field_type_get_type_id(type))); | |
8b45963b | 2324 | BT_ASSERT(!stack_empty(notit->stack)); |
e98a2d6e PP |
2325 | |
2326 | /* Pop stack */ | |
2327 | stack_pop(notit->stack); | |
2328 | ||
2329 | /* If the stack is not empty, increment the base's index */ | |
2330 | if (!stack_empty(notit->stack)) { | |
2331 | stack_top(notit->stack)->index++; | |
2332 | } | |
2333 | ||
839d52a5 | 2334 | return BT_BTR_STATUS_OK; |
e98a2d6e PP |
2335 | } |
2336 | ||
2337 | static | |
839d52a5 PP |
2338 | struct bt_field *resolve_field(struct bt_notif_iter *notit, |
2339 | struct bt_field_path *path) | |
e98a2d6e | 2340 | { |
839d52a5 | 2341 | struct bt_field *field = NULL; |
e98a2d6e PP |
2342 | unsigned int i; |
2343 | ||
fdf0e7a0 | 2344 | if (BT_LOG_ON_VERBOSE) { |
839d52a5 | 2345 | GString *gstr = bt_field_path_string(path); |
fdf0e7a0 PP |
2346 | |
2347 | BT_LOGV("Resolving field path: notit-addr=%p, field-path=\"%s\"", | |
2348 | notit, gstr ? gstr->str : NULL); | |
2349 | ||
2350 | if (gstr) { | |
2351 | g_string_free(gstr, TRUE); | |
2352 | } | |
2353 | } | |
2354 | ||
839d52a5 PP |
2355 | switch (bt_field_path_get_root_scope(path)) { |
2356 | case BT_SCOPE_TRACE_PACKET_HEADER: | |
e98a2d6e PP |
2357 | field = notit->dscopes.trace_packet_header; |
2358 | break; | |
839d52a5 | 2359 | case BT_SCOPE_STREAM_PACKET_CONTEXT: |
e98a2d6e PP |
2360 | field = notit->dscopes.stream_packet_context; |
2361 | break; | |
839d52a5 | 2362 | case BT_SCOPE_STREAM_EVENT_HEADER: |
e98a2d6e PP |
2363 | field = notit->dscopes.stream_event_header; |
2364 | break; | |
839d52a5 | 2365 | case BT_SCOPE_STREAM_EVENT_CONTEXT: |
e98a2d6e PP |
2366 | field = notit->dscopes.stream_event_context; |
2367 | break; | |
839d52a5 | 2368 | case BT_SCOPE_EVENT_CONTEXT: |
e98a2d6e PP |
2369 | field = notit->dscopes.event_context; |
2370 | break; | |
839d52a5 | 2371 | case BT_SCOPE_EVENT_FIELDS: |
e98a2d6e PP |
2372 | field = notit->dscopes.event_payload; |
2373 | break; | |
2374 | default: | |
fdf0e7a0 PP |
2375 | BT_LOGF("Cannot resolve field path: unknown scope: " |
2376 | "notit-addr=%p, root-scope=%s", | |
839d52a5 PP |
2377 | notit, bt_scope_string( |
2378 | bt_field_path_get_root_scope(path))); | |
fdf0e7a0 | 2379 | abort(); |
e98a2d6e PP |
2380 | } |
2381 | ||
2382 | if (!field) { | |
fdf0e7a0 PP |
2383 | BT_LOGW("Cannot resolve field path: root field not found: " |
2384 | "notit-addr=%p, root-scope=%s", | |
839d52a5 PP |
2385 | notit, bt_scope_string( |
2386 | bt_field_path_get_root_scope(path))); | |
e98a2d6e PP |
2387 | goto end; |
2388 | } | |
2389 | ||
2390 | bt_get(field); | |
2391 | ||
839d52a5 PP |
2392 | for (i = 0; i < bt_field_path_get_index_count(path); ++i) { |
2393 | struct bt_field *next_field = NULL; | |
2394 | struct bt_field_type *field_type; | |
2395 | int index = bt_field_path_get_index(path, i); | |
e98a2d6e | 2396 | |
839d52a5 | 2397 | field_type = bt_field_get_type(field); |
8b45963b | 2398 | BT_ASSERT(field_type); |
e98a2d6e PP |
2399 | |
2400 | if (is_struct_type(field_type)) { | |
839d52a5 | 2401 | next_field = bt_field_structure_get_field_by_index( |
e98a2d6e PP |
2402 | field, index); |
2403 | } else if (is_variant_type(field_type)) { | |
2404 | next_field = | |
839d52a5 | 2405 | bt_field_variant_get_current_field(field); |
e98a2d6e PP |
2406 | } |
2407 | ||
2408 | BT_PUT(field); | |
2409 | BT_PUT(field_type); | |
2410 | ||
2411 | if (!next_field) { | |
fdf0e7a0 PP |
2412 | BT_LOGW("Cannot find next field: " |
2413 | "notit-addr=%p, ft-addr=%p, ft-id=%s, index=%d", | |
2414 | notit, field_type, | |
839d52a5 PP |
2415 | bt_field_type_id_string( |
2416 | bt_field_type_get_type_id(field_type)), | |
fdf0e7a0 | 2417 | index); |
e98a2d6e PP |
2418 | goto end; |
2419 | } | |
2420 | ||
2421 | /* Move next field -> field */ | |
2422 | BT_MOVE(field, next_field); | |
2423 | } | |
2424 | ||
2425 | end: | |
2426 | return field; | |
2427 | } | |
2428 | ||
2429 | static | |
839d52a5 | 2430 | int64_t btr_get_sequence_length_cb(struct bt_field_type *type, void *data) |
e98a2d6e PP |
2431 | { |
2432 | int64_t ret = -1; | |
2433 | int iret; | |
839d52a5 PP |
2434 | struct bt_field *seq_field; |
2435 | struct bt_field_path *field_path; | |
2436 | struct bt_notif_iter *notit = data; | |
2437 | struct bt_field *length_field = NULL; | |
e98a2d6e PP |
2438 | uint64_t length; |
2439 | ||
839d52a5 | 2440 | field_path = bt_field_type_sequence_get_length_field_path(type); |
8b45963b | 2441 | BT_ASSERT(field_path); |
2cf1d51e JG |
2442 | length_field = resolve_field(notit, field_path); |
2443 | if (!length_field) { | |
fdf0e7a0 PP |
2444 | BT_LOGW("Cannot resolve sequence field type's length field path: " |
2445 | "notit-addr=%p, ft-addr=%p", | |
2446 | notit, type); | |
e98a2d6e PP |
2447 | goto end; |
2448 | } | |
2449 | ||
839d52a5 | 2450 | iret = bt_field_unsigned_integer_get_value(length_field, &length); |
e98a2d6e | 2451 | if (iret) { |
fdf0e7a0 PP |
2452 | BT_LOGE("Cannot get value of sequence length field: " |
2453 | "notit-addr=%p, field-addr=%p", | |
2454 | notit, length_field); | |
e98a2d6e PP |
2455 | goto end; |
2456 | } | |
2457 | ||
fdf0e7a0 | 2458 | seq_field = stack_top(notit->stack)->base; |
839d52a5 | 2459 | iret = bt_field_sequence_set_length(seq_field, length_field); |
2cf1d51e | 2460 | if (iret) { |
fdf0e7a0 PP |
2461 | BT_LOGE("Cannot set sequence field's length field: " |
2462 | "notit-addr=%p, seq-field-addr=%p, " | |
2463 | "length-field-addr=%p, ", | |
2464 | notit, seq_field, length_field); | |
2cf1d51e JG |
2465 | goto end; |
2466 | } | |
fdf0e7a0 | 2467 | |
e98a2d6e PP |
2468 | ret = (int64_t) length; |
2469 | ||
2470 | end: | |
2cf1d51e | 2471 | BT_PUT(length_field); |
e98a2d6e PP |
2472 | BT_PUT(field_path); |
2473 | ||
2474 | return ret; | |
2475 | } | |
2476 | ||
2477 | static | |
839d52a5 PP |
2478 | struct bt_field_type *btr_get_variant_type_cb( |
2479 | struct bt_field_type *type, void *data) | |
e98a2d6e | 2480 | { |
839d52a5 PP |
2481 | struct bt_field_path *path; |
2482 | struct bt_notif_iter *notit = data; | |
2483 | struct bt_field *var_field; | |
2484 | struct bt_field *tag_field = NULL; | |
2485 | struct bt_field *selected_field = NULL; | |
2486 | struct bt_field_type *selected_field_type = NULL; | |
e98a2d6e | 2487 | |
839d52a5 | 2488 | path = bt_field_type_variant_get_tag_field_path(type); |
8b45963b | 2489 | BT_ASSERT(path); |
e98a2d6e PP |
2490 | tag_field = resolve_field(notit, path); |
2491 | if (!tag_field) { | |
fdf0e7a0 PP |
2492 | BT_LOGW("Cannot resolve variant field type's tag field path: " |
2493 | "notit-addr=%p, ft-addr=%p", | |
2494 | notit, type); | |
e98a2d6e PP |
2495 | goto end; |
2496 | } | |
2497 | ||
2498 | /* | |
2499 | * We found the enumeration tag field instance which should be | |
2500 | * able to select a current field for this variant. This | |
2501 | * callback function we're in is called _after_ | |
2502 | * compound_begin(), so the current stack top's base field is | |
2503 | * the variant field in question. We get the selected field here | |
2504 | * thanks to this tag field (thus creating the selected field), | |
2505 | * which will also provide us with its type. Then, this field | |
2506 | * will remain the current selected one until the next callback | |
2507 | * function call which is used to fill the current selected | |
2508 | * field. | |
2509 | */ | |
fdf0e7a0 | 2510 | var_field = stack_top(notit->stack)->base; |
839d52a5 | 2511 | selected_field = bt_field_variant_get_field(var_field, tag_field); |
e98a2d6e | 2512 | if (!selected_field) { |
fdf0e7a0 PP |
2513 | BT_LOGW("Cannot get variant field's selection using tag field: " |
2514 | "notit-addr=%p, var-field-addr=%p, tag-field-addr=%p", | |
2515 | notit, var_field, tag_field); | |
e98a2d6e PP |
2516 | goto end; |
2517 | } | |
2518 | ||
839d52a5 | 2519 | selected_field_type = bt_field_get_type(selected_field); |
e98a2d6e PP |
2520 | |
2521 | end: | |
2522 | BT_PUT(tag_field); | |
2523 | BT_PUT(selected_field); | |
f77ae72a | 2524 | BT_PUT(path); |
e98a2d6e PP |
2525 | |
2526 | return selected_field_type; | |
2527 | } | |
2528 | ||
1556a1af | 2529 | static |
839d52a5 PP |
2530 | int set_event_clocks(struct bt_event *event, |
2531 | struct bt_notif_iter *notit) | |
1556a1af JG |
2532 | { |
2533 | int ret; | |
2534 | GHashTableIter iter; | |
839d52a5 | 2535 | struct bt_clock_class *clock_class; |
1556a1af JG |
2536 | uint64_t *clock_state; |
2537 | ||
2538 | g_hash_table_iter_init(&iter, notit->clock_states); | |
2539 | ||
ac0c6bdd | 2540 | while (g_hash_table_iter_next(&iter, (gpointer) &clock_class, |
1556a1af | 2541 | (gpointer) &clock_state)) { |
839d52a5 | 2542 | struct bt_clock_value *clock_value; |
1556a1af | 2543 | |
839d52a5 | 2544 | clock_value = bt_clock_value_create(clock_class, |
ac0c6bdd | 2545 | *clock_state); |
1556a1af | 2546 | if (!clock_value) { |
fdf0e7a0 PP |
2547 | BT_LOGE("Cannot create clock value from clock class: " |
2548 | "notit-addr=%p, clock-class-addr=%p, " | |
2549 | "clock-class-name=\"%s\"", | |
2550 | notit, clock_class, | |
839d52a5 | 2551 | bt_clock_class_get_name(clock_class)); |
1556a1af JG |
2552 | ret = -1; |
2553 | goto end; | |
2554 | } | |
839d52a5 | 2555 | ret = bt_event_set_clock_value(event, clock_value); |
1556a1af JG |
2556 | bt_put(clock_value); |
2557 | if (ret) { | |
839d52a5 PP |
2558 | struct bt_event_class *event_class = |
2559 | bt_event_get_class(event); | |
fdf0e7a0 | 2560 | |
8b45963b | 2561 | BT_ASSERT(event_class); |
fdf0e7a0 PP |
2562 | BT_LOGE("Cannot set event's clock value: " |
2563 | "notit-addr=%p, event-addr=%p, " | |
2564 | "event-class-name=\"%s\", " | |
2565 | "event-class-id=%" PRId64 ", " | |
2566 | "clock-class-addr=%p, " | |
2567 | "clock-class-name=\"%s\", " | |
2568 | "clock-value-addr=%p", | |
2569 | notit, event, | |
839d52a5 PP |
2570 | bt_event_class_get_name(event_class), |
2571 | bt_event_class_get_id(event_class), | |
fdf0e7a0 | 2572 | clock_class, |
839d52a5 | 2573 | bt_clock_class_get_name(clock_class), |
fdf0e7a0 PP |
2574 | clock_value); |
2575 | bt_put(event_class); | |
1556a1af JG |
2576 | goto end; |
2577 | } | |
2578 | } | |
fdf0e7a0 | 2579 | |
1556a1af JG |
2580 | ret = 0; |
2581 | end: | |
2582 | return ret; | |
2583 | } | |
2584 | ||
78586d8a | 2585 | static |
839d52a5 | 2586 | struct bt_event *create_event(struct bt_notif_iter *notit) |
e98a2d6e | 2587 | { |
e98a2d6e | 2588 | int ret; |
839d52a5 | 2589 | struct bt_event *event; |
e98a2d6e | 2590 | |
fdf0e7a0 PP |
2591 | BT_LOGV("Creating event for event notification: " |
2592 | "notit-addr=%p, event-class-addr=%p, " | |
2593 | "event-class-name=\"%s\", " | |
2594 | "event-class-id=%" PRId64, | |
2595 | notit, notit->meta.event_class, | |
839d52a5 PP |
2596 | bt_event_class_get_name(notit->meta.event_class), |
2597 | bt_event_class_get_id(notit->meta.event_class)); | |
fdf0e7a0 | 2598 | |
78586d8a | 2599 | /* Create event object. */ |
839d52a5 | 2600 | event = bt_event_create(notit->meta.event_class); |
e98a2d6e | 2601 | if (!event) { |
fdf0e7a0 PP |
2602 | BT_LOGE("Cannot create event: " |
2603 | "notit-addr=%p, event-class-addr=%p, " | |
2604 | "event-class-name=\"%s\", " | |
2605 | "event-class-id=%" PRId64, | |
2606 | notit, notit->meta.event_class, | |
839d52a5 PP |
2607 | bt_event_class_get_name(notit->meta.event_class), |
2608 | bt_event_class_get_id(notit->meta.event_class)); | |
e98a2d6e PP |
2609 | goto error; |
2610 | } | |
2611 | ||
78586d8a | 2612 | /* Set header, stream event context, context, and payload fields. */ |
839d52a5 | 2613 | ret = bt_event_set_header(event, |
e98a2d6e PP |
2614 | notit->dscopes.stream_event_header); |
2615 | if (ret) { | |
fdf0e7a0 PP |
2616 | BT_LOGE("Cannot set event's header field: " |
2617 | "notit-addr=%p, event-addr=%p, event-class-addr=%p, " | |
2618 | "event-class-name=\"%s\", " | |
2619 | "event-class-id=%" PRId64 ", field-addr=%p", | |
2620 | notit, event, notit->meta.event_class, | |
839d52a5 PP |
2621 | bt_event_class_get_name(notit->meta.event_class), |
2622 | bt_event_class_get_id(notit->meta.event_class), | |
fdf0e7a0 | 2623 | notit->dscopes.stream_event_header); |
e98a2d6e PP |
2624 | goto error; |
2625 | } | |
2626 | ||
839d52a5 | 2627 | ret = bt_event_set_stream_event_context(event, |
e98a2d6e PP |
2628 | notit->dscopes.stream_event_context); |
2629 | if (ret) { | |
fdf0e7a0 PP |
2630 | BT_LOGE("Cannot set event's context field: " |
2631 | "notit-addr=%p, event-addr=%p, event-class-addr=%p, " | |
2632 | "event-class-name=\"%s\", " | |
2633 | "event-class-id=%" PRId64 ", field-addr=%p", | |
2634 | notit, event, notit->meta.event_class, | |
839d52a5 PP |
2635 | bt_event_class_get_name(notit->meta.event_class), |
2636 | bt_event_class_get_id(notit->meta.event_class), | |
fdf0e7a0 | 2637 | notit->dscopes.stream_event_context); |
e98a2d6e PP |
2638 | goto error; |
2639 | } | |
2640 | ||
839d52a5 | 2641 | ret = bt_event_set_event_context(event, |
e98a2d6e PP |
2642 | notit->dscopes.event_context); |
2643 | if (ret) { | |
fdf0e7a0 PP |
2644 | BT_LOGE("Cannot set event's stream event context field: " |
2645 | "notit-addr=%p, event-addr=%p, event-class-addr=%p, " | |
2646 | "event-class-name=\"%s\", " | |
2647 | "event-class-id=%" PRId64 ", field-addr=%p", | |
2648 | notit, event, notit->meta.event_class, | |
839d52a5 PP |
2649 | bt_event_class_get_name(notit->meta.event_class), |
2650 | bt_event_class_get_id(notit->meta.event_class), | |
fdf0e7a0 | 2651 | notit->dscopes.event_context); |
e98a2d6e PP |
2652 | goto error; |
2653 | } | |
2654 | ||
839d52a5 | 2655 | ret = bt_event_set_event_payload(event, |
e98a2d6e PP |
2656 | notit->dscopes.event_payload); |
2657 | if (ret) { | |
fdf0e7a0 PP |
2658 | BT_LOGE("Cannot set event's payload field: " |
2659 | "notit-addr=%p, event-addr=%p, event-class-addr=%p, " | |
2660 | "event-class-name=\"%s\", " | |
2661 | "event-class-id=%" PRId64 ", field-addr=%p", | |
2662 | notit, event, notit->meta.event_class, | |
839d52a5 PP |
2663 | bt_event_class_get_name(notit->meta.event_class), |
2664 | bt_event_class_get_id(notit->meta.event_class), | |
fdf0e7a0 | 2665 | notit->dscopes.event_payload); |
e98a2d6e PP |
2666 | goto error; |
2667 | } | |
2668 | ||
1556a1af JG |
2669 | ret = set_event_clocks(event, notit); |
2670 | if (ret) { | |
fdf0e7a0 PP |
2671 | BT_LOGE("Cannot set event's clock values: " |
2672 | "notit-addr=%p, event-addr=%p, event-class-addr=%p, " | |
2673 | "event-class-name=\"%s\", " | |
2674 | "event-class-id=%" PRId64, | |
2675 | notit, event, notit->meta.event_class, | |
839d52a5 PP |
2676 | bt_event_class_get_name(notit->meta.event_class), |
2677 | bt_event_class_get_id(notit->meta.event_class)); | |
1556a1af JG |
2678 | goto error; |
2679 | } | |
2680 | ||
78586d8a | 2681 | /* Associate with current packet. */ |
8b45963b | 2682 | BT_ASSERT(notit->packet); |
839d52a5 | 2683 | ret = bt_event_set_packet(event, notit->packet); |
e98a2d6e | 2684 | if (ret) { |
fdf0e7a0 PP |
2685 | BT_LOGE("Cannot set event's header field: " |
2686 | "notit-addr=%p, event-addr=%p, event-class-addr=%p, " | |
2687 | "event-class-name=\"%s\", " | |
2688 | "event-class-id=%" PRId64 ", packet-addr=%p", | |
2689 | notit, event, notit->meta.event_class, | |
839d52a5 PP |
2690 | bt_event_class_get_name(notit->meta.event_class), |
2691 | bt_event_class_get_id(notit->meta.event_class), | |
fdf0e7a0 | 2692 | notit->packet); |
e98a2d6e PP |
2693 | goto error; |
2694 | } | |
2695 | ||
2696 | goto end; | |
fdf0e7a0 | 2697 | |
e98a2d6e PP |
2698 | error: |
2699 | BT_PUT(event); | |
fdf0e7a0 | 2700 | |
e98a2d6e PP |
2701 | end: |
2702 | return event; | |
2703 | } | |
2704 | ||
0659c536 | 2705 | static |
839d52a5 | 2706 | uint64_t get_cur_stream_instance_id(struct bt_notif_iter *notit) |
0659c536 | 2707 | { |
839d52a5 | 2708 | struct bt_field *stream_instance_id_field = NULL; |
0659c536 PP |
2709 | uint64_t stream_instance_id = -1ULL; |
2710 | int ret; | |
2711 | ||
2712 | if (!notit->dscopes.trace_packet_header) { | |
2713 | goto end; | |
2714 | } | |
2715 | ||
839d52a5 | 2716 | stream_instance_id_field = bt_field_structure_get_field_by_name( |
0659c536 PP |
2717 | notit->dscopes.trace_packet_header, "stream_instance_id"); |
2718 | if (!stream_instance_id_field) { | |
2719 | goto end; | |
2720 | } | |
2721 | ||
839d52a5 | 2722 | ret = bt_field_unsigned_integer_get_value(stream_instance_id_field, |
0659c536 PP |
2723 | &stream_instance_id); |
2724 | if (ret) { | |
2725 | stream_instance_id = -1ULL; | |
2726 | goto end; | |
2727 | } | |
2728 | ||
2729 | end: | |
2730 | bt_put(stream_instance_id_field); | |
2731 | return stream_instance_id; | |
2732 | } | |
2733 | ||
78586d8a | 2734 | static |
839d52a5 | 2735 | int set_stream(struct bt_notif_iter *notit) |
e98a2d6e | 2736 | { |
5184a1d6 | 2737 | int ret = 0; |
839d52a5 | 2738 | struct bt_stream *stream = NULL; |
e98a2d6e | 2739 | |
fdf0e7a0 PP |
2740 | BT_LOGV("Calling user function (get stream): notit-addr=%p, " |
2741 | "stream-class-addr=%p, stream-class-name=\"%s\", " | |
2742 | "stream-class-id=%" PRId64, | |
2743 | notit, notit->meta.stream_class, | |
839d52a5 PP |
2744 | bt_stream_class_get_name(notit->meta.stream_class), |
2745 | bt_stream_class_get_id(notit->meta.stream_class)); | |
5184a1d6 | 2746 | stream = bt_get(notit->medium.medops.get_stream( |
0659c536 PP |
2747 | notit->meta.stream_class, get_cur_stream_instance_id(notit), |
2748 | notit->medium.data)); | |
5184a1d6 | 2749 | BT_LOGV("User function returned: stream-addr=%p", stream); |
e98a2d6e | 2750 | if (!stream) { |
fdf0e7a0 | 2751 | BT_LOGW_STR("User function failed to return a stream object for the given stream class."); |
5184a1d6 PP |
2752 | ret = -1; |
2753 | goto end; | |
2754 | } | |
2755 | ||
2756 | if (notit->stream && stream != notit->stream) { | |
2757 | BT_LOGW("User function returned a different stream than the previous one for the same sequence of packets."); | |
2758 | ret = -1; | |
2759 | goto end; | |
2760 | } | |
2761 | ||
2762 | BT_MOVE(notit->stream, stream); | |
2763 | ||
2764 | end: | |
2765 | bt_put(stream); | |
2766 | return ret; | |
2767 | } | |
2768 | ||
2769 | static | |
839d52a5 | 2770 | void create_packet(struct bt_notif_iter *notit) |
5184a1d6 PP |
2771 | { |
2772 | int ret; | |
839d52a5 | 2773 | struct bt_packet *packet = NULL; |
5184a1d6 PP |
2774 | |
2775 | BT_LOGV("Creating packet for packet notification: " | |
2776 | "notit-addr=%p", notit); | |
2777 | ||
2778 | /* Ask the user for the stream */ | |
2779 | ret = set_stream(notit); | |
2780 | if (ret) { | |
e98a2d6e PP |
2781 | goto error; |
2782 | } | |
2783 | ||
fdf0e7a0 PP |
2784 | BT_LOGV("Creating packet from stream: " |
2785 | "notit-addr=%p, stream-addr=%p, " | |
2786 | "stream-class-addr=%p, " | |
2787 | "stream-class-name=\"%s\", " | |
2788 | "stream-class-id=%" PRId64, | |
5184a1d6 | 2789 | notit, notit->stream, notit->meta.stream_class, |
839d52a5 PP |
2790 | bt_stream_class_get_name(notit->meta.stream_class), |
2791 | bt_stream_class_get_id(notit->meta.stream_class)); | |
fdf0e7a0 | 2792 | |
e98a2d6e | 2793 | /* Create packet */ |
839d52a5 | 2794 | packet = bt_packet_create(notit->stream); |
e98a2d6e | 2795 | if (!packet) { |
fdf0e7a0 PP |
2796 | BT_LOGE("Cannot create packet from stream: " |
2797 | "notit-addr=%p, stream-addr=%p, " | |
2798 | "stream-class-addr=%p, " | |
2799 | "stream-class-name=\"%s\", " | |
2800 | "stream-class-id=%" PRId64, | |
5184a1d6 | 2801 | notit, notit->stream, notit->meta.stream_class, |
839d52a5 PP |
2802 | bt_stream_class_get_name(notit->meta.stream_class), |
2803 | bt_stream_class_get_id(notit->meta.stream_class)); | |
e98a2d6e PP |
2804 | goto error; |
2805 | } | |
2806 | ||
2807 | /* Set packet's context and header fields */ | |
2808 | if (notit->dscopes.trace_packet_header) { | |
839d52a5 | 2809 | ret = bt_packet_set_header(packet, |
fdf0e7a0 | 2810 | notit->dscopes.trace_packet_header); |
e98a2d6e | 2811 | if (ret) { |
fdf0e7a0 PP |
2812 | BT_LOGE("Cannot set packet's header field: " |
2813 | "notit-addr=%p, packet-addr=%p, " | |
2814 | "stream-addr=%p, " | |
2815 | "stream-class-addr=%p, " | |
2816 | "stream-class-name=\"%s\", " | |
2817 | "stream-class-id=%" PRId64 ", " | |
2818 | "field-addr=%p", | |
5184a1d6 | 2819 | notit, packet, notit->stream, notit->meta.stream_class, |
839d52a5 PP |
2820 | bt_stream_class_get_name(notit->meta.stream_class), |
2821 | bt_stream_class_get_id(notit->meta.stream_class), | |
fdf0e7a0 | 2822 | notit->dscopes.trace_packet_header); |
e98a2d6e PP |
2823 | goto error; |
2824 | } | |
2825 | } | |
2826 | ||
2827 | if (notit->dscopes.stream_packet_context) { | |
839d52a5 | 2828 | ret = bt_packet_set_context(packet, |
fdf0e7a0 | 2829 | notit->dscopes.stream_packet_context); |
e98a2d6e | 2830 | if (ret) { |
fdf0e7a0 PP |
2831 | BT_LOGE("Cannot set packet's context field: " |
2832 | "notit-addr=%p, packet-addr=%p, " | |
2833 | "stream-addr=%p, " | |
2834 | "stream-class-addr=%p, " | |
2835 | "stream-class-name=\"%s\", " | |
2836 | "stream-class-id=%" PRId64 ", " | |
2837 | "field-addr=%p", | |
5184a1d6 | 2838 | notit, packet, notit->stream, notit->meta.stream_class, |
839d52a5 PP |
2839 | bt_stream_class_get_name(notit->meta.stream_class), |
2840 | bt_stream_class_get_id(notit->meta.stream_class), | |
fdf0e7a0 | 2841 | notit->dscopes.trace_packet_header); |
e98a2d6e PP |
2842 | goto error; |
2843 | } | |
2844 | } | |
2845 | ||
2846 | goto end; | |
fdf0e7a0 | 2847 | |
e98a2d6e PP |
2848 | error: |
2849 | BT_PUT(packet); | |
fdf0e7a0 | 2850 | |
e98a2d6e PP |
2851 | end: |
2852 | BT_MOVE(notit->packet, packet); | |
2853 | } | |
2854 | ||
78586d8a | 2855 | static |
839d52a5 | 2856 | void notify_new_packet(struct bt_notif_iter *notit, |
78586d8a | 2857 | struct bt_notification **notification) |
e98a2d6e | 2858 | { |
78586d8a | 2859 | struct bt_notification *ret; |
e98a2d6e | 2860 | |
78586d8a | 2861 | /* Initialize the iterator's current packet */ |
e98a2d6e PP |
2862 | create_packet(notit); |
2863 | if (!notit->packet) { | |
fdf0e7a0 PP |
2864 | BT_LOGE("Cannot create packet for packet notification: " |
2865 | "notit-addr=%p", notit); | |
78586d8a | 2866 | return; |
e98a2d6e PP |
2867 | } |
2868 | ||
ea0e619e | 2869 | ret = bt_notification_packet_begin_create(notit->packet); |
78586d8a | 2870 | if (!ret) { |
fdf0e7a0 PP |
2871 | BT_LOGE("Cannot create packet beginning notification: " |
2872 | "notit-addr=%p, packet-addr=%p", | |
2873 | notit, notit->packet); | |
78586d8a JG |
2874 | return; |
2875 | } | |
2876 | *notification = ret; | |
e98a2d6e PP |
2877 | } |
2878 | ||
78586d8a | 2879 | static |
839d52a5 | 2880 | void notify_end_of_packet(struct bt_notif_iter *notit, |
78586d8a | 2881 | struct bt_notification **notification) |
e98a2d6e | 2882 | { |
78586d8a | 2883 | struct bt_notification *ret; |
e98a2d6e | 2884 | |
e98a2d6e | 2885 | if (!notit->packet) { |
78586d8a | 2886 | return; |
e98a2d6e PP |
2887 | } |
2888 | ||
78586d8a JG |
2889 | ret = bt_notification_packet_end_create(notit->packet); |
2890 | if (!ret) { | |
fdf0e7a0 PP |
2891 | BT_LOGE("Cannot create packet end notification: " |
2892 | "notit-addr=%p, packet-addr=%p", | |
2893 | notit, notit->packet); | |
78586d8a JG |
2894 | return; |
2895 | } | |
2896 | BT_PUT(notit->packet); | |
2897 | *notification = ret; | |
e98a2d6e PP |
2898 | } |
2899 | ||
78586d8a | 2900 | static |
839d52a5 | 2901 | void notify_event(struct bt_notif_iter *notit, |
0982a26d | 2902 | struct bt_clock_class_priority_map *cc_prio_map, |
78586d8a | 2903 | struct bt_notification **notification) |
e98a2d6e | 2904 | { |
839d52a5 | 2905 | struct bt_event *event = NULL; |
78586d8a | 2906 | struct bt_notification *ret = NULL; |
e98a2d6e | 2907 | |
9b876b7b PP |
2908 | /* Make sure that the event contains at least one bit of data */ |
2909 | if (notit->buf.at == notit->buf.last_eh_at) { | |
2910 | BT_LOGE("Cannot create empty event with 0 bits of data: " | |
2911 | "notit-addr=%p, packet-cur=%zu", | |
2912 | notit, packet_at(notit)); | |
2913 | goto end; | |
2914 | } | |
2915 | ||
e98a2d6e PP |
2916 | /* Create event */ |
2917 | event = create_event(notit); | |
2918 | if (!event) { | |
fdf0e7a0 PP |
2919 | BT_LOGE("Cannot create event for event notification: " |
2920 | "notit-addr=%p", notit); | |
78586d8a | 2921 | goto end; |
e98a2d6e | 2922 | } |
e98a2d6e | 2923 | |
0982a26d | 2924 | ret = bt_notification_event_create(event, cc_prio_map); |
78586d8a | 2925 | if (!ret) { |
fdf0e7a0 PP |
2926 | BT_LOGE("Cannot create event notification: " |
2927 | "notit-addr=%p, event-addr=%p, " | |
2928 | "cc-prio-map-addr=%p", | |
2929 | notit, event, cc_prio_map); | |
78586d8a | 2930 | goto end; |
e98a2d6e | 2931 | } |
78586d8a JG |
2932 | *notification = ret; |
2933 | end: | |
2934 | BT_PUT(event); | |
e98a2d6e PP |
2935 | } |
2936 | ||
5f870343 | 2937 | static |
839d52a5 | 2938 | void init_trace_field_path_cache(struct bt_trace *trace, |
5f870343 JG |
2939 | struct trace_field_path_cache *trace_field_path_cache) |
2940 | { | |
2941 | int stream_id = -1; | |
2942 | int stream_instance_id = -1; | |
2943 | int i, count; | |
839d52a5 | 2944 | struct bt_field_type *packet_header = NULL; |
5f870343 | 2945 | |
839d52a5 | 2946 | packet_header = bt_trace_get_packet_header_type(trace); |
5f870343 JG |
2947 | if (!packet_header) { |
2948 | goto end; | |
2949 | } | |
2950 | ||
839d52a5 | 2951 | if (!bt_field_type_is_structure(packet_header)) { |
5f870343 JG |
2952 | goto end; |
2953 | } | |
2954 | ||
839d52a5 | 2955 | count = bt_field_type_structure_get_field_count(packet_header); |
8b45963b | 2956 | BT_ASSERT(count >= 0); |
5f870343 JG |
2957 | |
2958 | for (i = 0; (i < count && (stream_id == -1 || stream_instance_id == -1)); i++) { | |
2959 | int ret; | |
2960 | const char *field_name; | |
2961 | ||
839d52a5 | 2962 | ret = bt_field_type_structure_get_field_by_index(packet_header, |
5f870343 JG |
2963 | &field_name, NULL, i); |
2964 | if (ret) { | |
fdf0e7a0 PP |
2965 | BT_LOGE("Cannot get structure field's field: " |
2966 | "field-addr=%p, index=%d", | |
2967 | packet_header, i); | |
5f870343 JG |
2968 | goto end; |
2969 | } | |
2970 | ||
2971 | if (stream_id == -1 && !strcmp(field_name, "stream_id")) { | |
2972 | stream_id = i; | |
2973 | } else if (stream_instance_id == -1 && | |
2974 | !strcmp(field_name, "stream_instance_id")) { | |
2975 | stream_instance_id = i; | |
2976 | } | |
2977 | } | |
fdf0e7a0 | 2978 | |
5f870343 JG |
2979 | end: |
2980 | trace_field_path_cache->stream_id = stream_id; | |
2981 | trace_field_path_cache->stream_instance_id = stream_instance_id; | |
2982 | BT_PUT(packet_header); | |
2983 | } | |
2984 | ||
c44c3e70 | 2985 | BT_HIDDEN |
839d52a5 | 2986 | struct bt_notif_iter *bt_notif_iter_create(struct bt_trace *trace, |
e98a2d6e | 2987 | size_t max_request_sz, |
839d52a5 | 2988 | struct bt_notif_iter_medium_ops medops, void *data) |
e98a2d6e | 2989 | { |
839d52a5 PP |
2990 | struct bt_notif_iter *notit = NULL; |
2991 | struct bt_btr_cbs cbs = { | |
e98a2d6e PP |
2992 | .types = { |
2993 | .signed_int = btr_signed_int_cb, | |
2994 | .unsigned_int = btr_unsigned_int_cb, | |
2995 | .floating_point = btr_floating_point_cb, | |
2996 | .string_begin = btr_string_begin_cb, | |
2997 | .string = btr_string_cb, | |
2998 | .string_end = btr_string_end_cb, | |
2999 | .compound_begin = btr_compound_begin_cb, | |
3000 | .compound_end = btr_compound_end_cb, | |
3001 | }, | |
3002 | .query = { | |
3003 | .get_sequence_length = btr_get_sequence_length_cb, | |
3004 | .get_variant_type = btr_get_variant_type_cb, | |
3005 | }, | |
3006 | }; | |
3007 | ||
8b45963b PP |
3008 | BT_ASSERT(trace); |
3009 | BT_ASSERT(medops.request_bytes); | |
3010 | BT_ASSERT(medops.get_stream); | |
fdf0e7a0 PP |
3011 | BT_LOGD("Creating CTF plugin notification iterator: " |
3012 | "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, " | |
3013 | "data=%p", | |
839d52a5 PP |
3014 | trace, bt_trace_get_name(trace), max_request_sz, data); |
3015 | notit = g_new0(struct bt_notif_iter, 1); | |
e98a2d6e | 3016 | if (!notit) { |
fdf0e7a0 | 3017 | BT_LOGE_STR("Failed to allocate one CTF plugin notification iterator."); |
e98a2d6e PP |
3018 | goto end; |
3019 | } | |
c44c3e70 | 3020 | notit->clock_states = g_hash_table_new_full(g_direct_hash, |
fdf0e7a0 | 3021 | g_direct_equal, bt_put, g_free); |
c44c3e70 | 3022 | if (!notit->clock_states) { |
fdf0e7a0 | 3023 | BT_LOGE_STR("Failed to allocate a GHashTable."); |
c44c3e70 JG |
3024 | goto error; |
3025 | } | |
35d47007 | 3026 | notit->meta.trace = bt_get(trace); |
e98a2d6e PP |
3027 | notit->medium.medops = medops; |
3028 | notit->medium.max_request_sz = max_request_sz; | |
3029 | notit->medium.data = data; | |
e98a2d6e PP |
3030 | notit->stack = stack_new(notit); |
3031 | if (!notit->stack) { | |
fdf0e7a0 | 3032 | BT_LOGE_STR("Failed to create field stack."); |
c44c3e70 | 3033 | goto error; |
e98a2d6e PP |
3034 | } |
3035 | ||
839d52a5 | 3036 | notit->btr = bt_btr_create(cbs, notit); |
e98a2d6e | 3037 | if (!notit->btr) { |
fdf0e7a0 | 3038 | BT_LOGE_STR("Failed to create binary type reader (BTR)."); |
c44c3e70 | 3039 | goto error; |
e98a2d6e PP |
3040 | } |
3041 | ||
839d52a5 | 3042 | bt_notif_iter_reset(notit); |
5f870343 JG |
3043 | init_trace_field_path_cache(trace, ¬it->trace_field_path_cache); |
3044 | notit->sc_field_path_caches = g_hash_table_new_full(g_direct_hash, | |
fdf0e7a0 | 3045 | g_direct_equal, bt_put, g_free); |
5f870343 | 3046 | if (!notit->sc_field_path_caches) { |
fdf0e7a0 | 3047 | BT_LOGE_STR("Failed to allocate a GHashTable."); |
5f870343 JG |
3048 | goto error; |
3049 | } | |
3050 | ||
3051 | notit->field_overrides = g_hash_table_new_full(g_direct_hash, | |
fdf0e7a0 | 3052 | g_direct_equal, bt_put, g_free); |
5f870343 | 3053 | if (!notit->field_overrides) { |
fdf0e7a0 | 3054 | BT_LOGE_STR("Failed to allocate a GHashTable."); |
5f870343 JG |
3055 | goto error; |
3056 | } | |
3057 | ||
fdf0e7a0 PP |
3058 | BT_LOGD("Created CTF plugin notification iterator: " |
3059 | "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, " | |
3060 | "data=%p, notit-addr=%p", | |
839d52a5 | 3061 | trace, bt_trace_get_name(trace), max_request_sz, data, |
fdf0e7a0 | 3062 | notit); |
bb230c9b | 3063 | notit->cur_packet_offset = 0; |
fdf0e7a0 | 3064 | |
e98a2d6e PP |
3065 | end: |
3066 | return notit; | |
fdf0e7a0 | 3067 | |
c44c3e70 | 3068 | error: |
839d52a5 | 3069 | bt_notif_iter_destroy(notit); |
c44c3e70 JG |
3070 | notit = NULL; |
3071 | goto end; | |
e98a2d6e PP |
3072 | } |
3073 | ||
839d52a5 | 3074 | void bt_notif_iter_destroy(struct bt_notif_iter *notit) |
e98a2d6e PP |
3075 | { |
3076 | BT_PUT(notit->meta.trace); | |
3077 | BT_PUT(notit->meta.stream_class); | |
3078 | BT_PUT(notit->meta.event_class); | |
3079 | BT_PUT(notit->packet); | |
5184a1d6 | 3080 | BT_PUT(notit->stream); |
5f870343 | 3081 | BT_PUT(notit->cur_timestamp_end); |
e98a2d6e PP |
3082 | put_all_dscopes(notit); |
3083 | ||
fdf0e7a0 PP |
3084 | BT_LOGD("Destroying CTF plugin notification iterator: addr=%p", notit); |
3085 | ||
e98a2d6e | 3086 | if (notit->stack) { |
fdf0e7a0 | 3087 | BT_LOGD_STR("Destroying field stack."); |
e98a2d6e PP |
3088 | stack_destroy(notit->stack); |
3089 | } | |
3090 | ||
3091 | if (notit->btr) { | |
fdf0e7a0 | 3092 | BT_LOGD("Destroying BTR: btr-addr=%p", notit->btr); |
839d52a5 | 3093 | bt_btr_destroy(notit->btr); |
e98a2d6e PP |
3094 | } |
3095 | ||
c44c3e70 JG |
3096 | if (notit->clock_states) { |
3097 | g_hash_table_destroy(notit->clock_states); | |
3098 | } | |
5f870343 JG |
3099 | |
3100 | if (notit->sc_field_path_caches) { | |
3101 | g_hash_table_destroy(notit->sc_field_path_caches); | |
3102 | } | |
3103 | ||
3104 | if (notit->field_overrides) { | |
3105 | g_hash_table_destroy(notit->field_overrides); | |
3106 | } | |
fdf0e7a0 | 3107 | |
e98a2d6e PP |
3108 | g_free(notit); |
3109 | } | |
3110 | ||
839d52a5 PP |
3111 | enum bt_notif_iter_status bt_notif_iter_get_next_notification( |
3112 | struct bt_notif_iter *notit, | |
0982a26d | 3113 | struct bt_clock_class_priority_map *cc_prio_map, |
78586d8a | 3114 | struct bt_notification **notification) |
e98a2d6e | 3115 | { |
839d52a5 | 3116 | enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; |
e98a2d6e | 3117 | |
8b45963b PP |
3118 | BT_ASSERT(notit); |
3119 | BT_ASSERT(notification); | |
e98a2d6e | 3120 | |
fdf0e7a0 PP |
3121 | BT_LOGV("Getting next notification: notit-addr=%p, cc-prio-map-addr=%p", |
3122 | notit, cc_prio_map); | |
3123 | ||
e98a2d6e PP |
3124 | while (true) { |
3125 | status = handle_state(notit); | |
839d52a5 PP |
3126 | if (status == BT_NOTIF_ITER_STATUS_AGAIN) { |
3127 | BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_AGAIN."); | |
7cdc2bab MD |
3128 | goto end; |
3129 | } | |
839d52a5 PP |
3130 | if (status != BT_NOTIF_ITER_STATUS_OK) { |
3131 | if (status == BT_NOTIF_ITER_STATUS_EOF) { | |
3132 | BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_EOF."); | |
e98a2d6e | 3133 | } else { |
fdf0e7a0 PP |
3134 | BT_LOGW("Cannot handle state: " |
3135 | "notit-addr=%p, state=%s", | |
3136 | notit, state_string(notit->state)); | |
e98a2d6e PP |
3137 | } |
3138 | goto end; | |
3139 | } | |
3140 | ||
3141 | switch (notit->state) { | |
3142 | case STATE_EMIT_NOTIF_NEW_PACKET: | |
fdf0e7a0 | 3143 | /* notify_new_packet() logs errors */ |
e98a2d6e PP |
3144 | notify_new_packet(notit, notification); |
3145 | if (!*notification) { | |
839d52a5 | 3146 | status = BT_NOTIF_ITER_STATUS_ERROR; |
e98a2d6e PP |
3147 | } |
3148 | goto end; | |
3149 | case STATE_EMIT_NOTIF_EVENT: | |
fdf0e7a0 | 3150 | /* notify_event() logs errors */ |
0982a26d | 3151 | notify_event(notit, cc_prio_map, notification); |
e98a2d6e | 3152 | if (!*notification) { |
839d52a5 | 3153 | status = BT_NOTIF_ITER_STATUS_ERROR; |
e98a2d6e PP |
3154 | } |
3155 | goto end; | |
3156 | case STATE_EMIT_NOTIF_END_OF_PACKET: | |
5f870343 JG |
3157 | /* Update clock with timestamp_end field. */ |
3158 | if (notit->cur_timestamp_end) { | |
839d52a5 PP |
3159 | enum bt_btr_status btr_status; |
3160 | struct bt_field_type *field_type = | |
3161 | bt_field_get_type( | |
5f870343 JG |
3162 | notit->cur_timestamp_end); |
3163 | ||
8b45963b | 3164 | BT_ASSERT(field_type); |
f45bfe88 PP |
3165 | btr_status = update_clock(notit, |
3166 | notit->cur_timestamp_end); | |
5f870343 | 3167 | BT_PUT(field_type); |
839d52a5 | 3168 | if (btr_status != BT_BTR_STATUS_OK) { |
fdf0e7a0 PP |
3169 | BT_LOGW("Cannot update stream's clock value: " |
3170 | "notit-addr=%p", notit); | |
839d52a5 | 3171 | status = BT_NOTIF_ITER_STATUS_ERROR; |
5f870343 JG |
3172 | goto end; |
3173 | } | |
3174 | } | |
3175 | ||
fdf0e7a0 | 3176 | /* notify_end_of_packet() logs errors */ |
e98a2d6e PP |
3177 | notify_end_of_packet(notit, notification); |
3178 | if (!*notification) { | |
839d52a5 | 3179 | status = BT_NOTIF_ITER_STATUS_ERROR; |
e98a2d6e PP |
3180 | } |
3181 | goto end; | |
3182 | default: | |
3183 | /* Non-emitting state: continue */ | |
3184 | break; | |
3185 | } | |
3186 | } | |
3187 | ||
3188 | end: | |
3189 | return status; | |
3190 | } | |
87187cbf PP |
3191 | |
3192 | BT_HIDDEN | |
839d52a5 PP |
3193 | enum bt_notif_iter_status bt_notif_iter_get_packet_header_context_fields( |
3194 | struct bt_notif_iter *notit, | |
3195 | struct bt_field **packet_header_field, | |
3196 | struct bt_field **packet_context_field) | |
87187cbf | 3197 | { |
bb230c9b | 3198 | int ret; |
839d52a5 | 3199 | enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; |
87187cbf | 3200 | |
8b45963b | 3201 | BT_ASSERT(notit); |
87187cbf PP |
3202 | |
3203 | if (notit->state == STATE_EMIT_NOTIF_NEW_PACKET) { | |
3204 | /* We're already there */ | |
3205 | goto set_fields; | |
3206 | } | |
3207 | ||
3208 | while (true) { | |
3209 | status = handle_state(notit); | |
839d52a5 PP |
3210 | if (status == BT_NOTIF_ITER_STATUS_AGAIN) { |
3211 | BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_AGAIN."); | |
87187cbf PP |
3212 | goto end; |
3213 | } | |
839d52a5 PP |
3214 | if (status != BT_NOTIF_ITER_STATUS_OK) { |
3215 | if (status == BT_NOTIF_ITER_STATUS_EOF) { | |
3216 | BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_EOF."); | |
87187cbf | 3217 | } else { |
fdf0e7a0 PP |
3218 | BT_LOGW("Cannot handle state: " |
3219 | "notit-addr=%p, state=%s", | |
3220 | notit, state_string(notit->state)); | |
87187cbf PP |
3221 | } |
3222 | goto end; | |
3223 | } | |
3224 | ||
3225 | switch (notit->state) { | |
3226 | case STATE_EMIT_NOTIF_NEW_PACKET: | |
3227 | /* | |
3228 | * Packet header and context fields are | |
3229 | * potentially decoded (or they don't exist). | |
3230 | */ | |
3231 | goto set_fields; | |
3232 | case STATE_INIT: | |
3233 | case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN: | |
3234 | case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE: | |
3235 | case STATE_AFTER_TRACE_PACKET_HEADER: | |
3236 | case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN: | |
3237 | case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE: | |
3238 | case STATE_AFTER_STREAM_PACKET_CONTEXT: | |
3239 | /* Non-emitting state: continue */ | |
3240 | break; | |
3241 | default: | |
3242 | /* | |
3243 | * We should never get past the | |
3244 | * STATE_EMIT_NOTIF_NEW_PACKET state. | |
3245 | */ | |
fdf0e7a0 PP |
3246 | BT_LOGF("Unexpected state: notit-addr=%p, state=%s", |
3247 | notit, state_string(notit->state)); | |
0fbb9a9f | 3248 | abort(); |
87187cbf PP |
3249 | } |
3250 | } | |
3251 | ||
3252 | set_fields: | |
3253 | if (packet_header_field) { | |
3254 | *packet_header_field = bt_get(notit->dscopes.trace_packet_header); | |
3255 | } | |
3256 | ||
3257 | if (packet_context_field) { | |
3258 | *packet_context_field = bt_get(notit->dscopes.stream_packet_context); | |
3259 | } | |
3260 | ||
bb230c9b JG |
3261 | ret = set_current_packet_content_sizes(notit); |
3262 | if (ret) { | |
839d52a5 | 3263 | status = BT_NOTIF_ITER_STATUS_ERROR; |
bb230c9b JG |
3264 | goto end; |
3265 | } | |
87187cbf PP |
3266 | end: |
3267 | return status; | |
3268 | } | |
55fa6491 PP |
3269 | |
3270 | BT_HIDDEN | |
839d52a5 | 3271 | void bt_notif_iter_set_medops_data(struct bt_notif_iter *notit, |
55fa6491 PP |
3272 | void *medops_data) |
3273 | { | |
8b45963b | 3274 | BT_ASSERT(notit); |
55fa6491 PP |
3275 | notit->medium.data = medops_data; |
3276 | } | |
bb230c9b JG |
3277 | |
3278 | BT_HIDDEN | |
839d52a5 PP |
3279 | enum bt_notif_iter_status bt_notif_iter_seek( |
3280 | struct bt_notif_iter *notit, off_t offset) | |
bb230c9b | 3281 | { |
839d52a5 PP |
3282 | enum bt_notif_iter_status ret = BT_NOTIF_ITER_STATUS_OK; |
3283 | enum bt_notif_iter_medium_status medium_status; | |
bb230c9b | 3284 | |
8b45963b | 3285 | BT_ASSERT(notit); |
bb230c9b JG |
3286 | if (offset < 0) { |
3287 | BT_LOGE("Cannot seek to negative offset: offset=%jd", offset); | |
839d52a5 | 3288 | ret = BT_NOTIF_ITER_STATUS_INVAL; |
bb230c9b JG |
3289 | goto end; |
3290 | } | |
3291 | ||
3292 | if (!notit->medium.medops.seek) { | |
839d52a5 | 3293 | ret = BT_NOTIF_ITER_STATUS_UNSUPPORTED; |
bb230c9b JG |
3294 | BT_LOGD("Aborting seek as the iterator's underlying media does not implement seek support."); |
3295 | goto end; | |
3296 | } | |
3297 | ||
3298 | medium_status = notit->medium.medops.seek( | |
839d52a5 | 3299 | BT_NOTIF_ITER_SEEK_WHENCE_SET, offset, |
bb230c9b | 3300 | notit->medium.data); |
839d52a5 PP |
3301 | if (medium_status != BT_NOTIF_ITER_MEDIUM_STATUS_OK) { |
3302 | if (medium_status == BT_NOTIF_ITER_MEDIUM_STATUS_EOF) { | |
3303 | ret = BT_NOTIF_ITER_STATUS_EOF; | |
bb230c9b | 3304 | } else { |
839d52a5 | 3305 | ret = BT_NOTIF_ITER_STATUS_ERROR; |
bb230c9b JG |
3306 | goto end; |
3307 | } | |
3308 | } | |
3309 | ||
839d52a5 | 3310 | bt_notif_iter_reset(notit); |
bb230c9b JG |
3311 | notit->cur_packet_offset = offset; |
3312 | end: | |
3313 | return ret; | |
3314 | } | |
3315 | ||
3316 | BT_HIDDEN | |
839d52a5 PP |
3317 | off_t bt_notif_iter_get_current_packet_offset( |
3318 | struct bt_notif_iter *notit) | |
bb230c9b | 3319 | { |
8b45963b | 3320 | BT_ASSERT(notit); |
bb230c9b JG |
3321 | return notit->cur_packet_offset; |
3322 | } | |
3323 | ||
3324 | BT_HIDDEN | |
839d52a5 PP |
3325 | off_t bt_notif_iter_get_current_packet_size( |
3326 | struct bt_notif_iter *notit) | |
bb230c9b | 3327 | { |
8b45963b | 3328 | BT_ASSERT(notit); |
bb230c9b JG |
3329 | return notit->cur_packet_size; |
3330 | } |