| 1 | /* |
| 2 | * Babeltrace - CTF notification iterator |
| 3 | * |
| 4 | * Copyright (c) 2015-2016 EfficiOS Inc. and Linux Foundation |
| 5 | * Copyright (c) 2015-2016 Philippe Proulx <pproulx@efficios.com> |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | * of this software and associated documentation files (the "Software"), to deal |
| 9 | * in the Software without restriction, including without limitation the rights |
| 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | * copies of the Software, and to permit persons to whom the Software is |
| 12 | * furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included in |
| 15 | * all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | * SOFTWARE. |
| 24 | */ |
| 25 | |
| 26 | #define BT_LOG_TAG "PLUGIN-CTF-NOTIF-ITER" |
| 27 | #include "logging.h" |
| 28 | |
| 29 | #include <stdint.h> |
| 30 | #include <inttypes.h> |
| 31 | #include <stdio.h> |
| 32 | #include <stddef.h> |
| 33 | #include <stdbool.h> |
| 34 | #include <assert.h> |
| 35 | #include <string.h> |
| 36 | #include <babeltrace/babeltrace.h> |
| 37 | #include <babeltrace/ctf-ir/field-types-internal.h> |
| 38 | #include <babeltrace/ctf-ir/field-path-internal.h> |
| 39 | #include <glib.h> |
| 40 | #include <stdlib.h> |
| 41 | |
| 42 | #include "notif-iter.h" |
| 43 | #include "../btr/btr.h" |
| 44 | |
| 45 | struct bt_notif_iter; |
| 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 | */ |
| 60 | struct bt_field *base; |
| 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; |
| 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 | |
| 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 { |
| 124 | enum bt_btr_status (* func)(void *value, |
| 125 | struct bt_field_type *type, void *data); |
| 126 | void *data; |
| 127 | }; |
| 128 | |
| 129 | /* CTF notification iterator */ |
| 130 | struct bt_notif_iter { |
| 131 | /* Visit stack */ |
| 132 | struct stack *stack; |
| 133 | |
| 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 | */ |
| 141 | struct bt_field **cur_dscope_field; |
| 142 | |
| 143 | /* Trace and classes (owned by this) */ |
| 144 | struct { |
| 145 | struct bt_trace *trace; |
| 146 | struct bt_stream_class *stream_class; |
| 147 | struct bt_event_class *event_class; |
| 148 | } meta; |
| 149 | |
| 150 | /* Current packet (NULL if not created yet) */ |
| 151 | struct bt_packet *packet; |
| 152 | |
| 153 | /* Current stream (NULL if not set yet) */ |
| 154 | struct bt_stream *stream; |
| 155 | |
| 156 | /* |
| 157 | * Current timestamp_end field (to consider before switching packets). |
| 158 | */ |
| 159 | struct bt_field *cur_timestamp_end; |
| 160 | |
| 161 | /* Database of current dynamic scopes (owned by this) */ |
| 162 | struct { |
| 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; |
| 169 | } dscopes; |
| 170 | |
| 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 | * |
| 182 | * bt_field_type to struct field_cb_override |
| 183 | */ |
| 184 | GHashTable *field_overrides; |
| 185 | |
| 186 | /* Current state */ |
| 187 | enum state state; |
| 188 | |
| 189 | /* Current medium buffer data */ |
| 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; |
| 202 | |
| 203 | /* Position of the last event header from addr (bits) */ |
| 204 | size_t last_eh_at; |
| 205 | } buf; |
| 206 | |
| 207 | /* Binary type reader */ |
| 208 | struct bt_btr *btr; |
| 209 | |
| 210 | /* Current medium data */ |
| 211 | struct { |
| 212 | struct bt_notif_iter_medium_ops medops; |
| 213 | size_t max_request_sz; |
| 214 | void *data; |
| 215 | } medium; |
| 216 | |
| 217 | /* Current packet size (bits) (-1 if unknown) */ |
| 218 | int64_t cur_packet_size; |
| 219 | |
| 220 | /* Current content size (bits) (-1 if unknown) */ |
| 221 | int64_t cur_content_size; |
| 222 | |
| 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 | |
| 229 | /* bt_clock_class to uint64_t. */ |
| 230 | GHashTable *clock_states; |
| 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 | |
| 244 | /* bt_stream_class to struct stream_class_field_path_cache. */ |
| 245 | GHashTable *sc_field_path_caches; |
| 246 | }; |
| 247 | |
| 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 | |
| 297 | static |
| 298 | int bt_notif_iter_switch_packet(struct bt_notif_iter *notit); |
| 299 | |
| 300 | static |
| 301 | enum bt_btr_status btr_timestamp_end_cb(void *value, |
| 302 | struct bt_field_type *type, void *data); |
| 303 | |
| 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 |
| 314 | struct stack *stack_new(struct bt_notif_iter *notit) |
| 315 | { |
| 316 | struct stack *stack = NULL; |
| 317 | |
| 318 | stack = g_new0(struct stack, 1); |
| 319 | if (!stack) { |
| 320 | BT_LOGE_STR("Failed to allocate one stack."); |
| 321 | goto error; |
| 322 | } |
| 323 | |
| 324 | stack->entries = g_ptr_array_new_with_free_func(stack_entry_free_func); |
| 325 | if (!stack->entries) { |
| 326 | BT_LOGE_STR("Failed to allocate a GPtrArray."); |
| 327 | goto error; |
| 328 | } |
| 329 | |
| 330 | BT_LOGD("Created stack: notit-addr=%p, stack-addr=%p", notit, stack); |
| 331 | return stack; |
| 332 | |
| 333 | error: |
| 334 | g_free(stack); |
| 335 | return NULL; |
| 336 | } |
| 337 | |
| 338 | static |
| 339 | void stack_destroy(struct stack *stack) |
| 340 | { |
| 341 | assert(stack); |
| 342 | BT_LOGD("Destroying stack: addr=%p", stack); |
| 343 | g_ptr_array_free(stack->entries, TRUE); |
| 344 | g_free(stack); |
| 345 | } |
| 346 | |
| 347 | static |
| 348 | int stack_push(struct stack *stack, struct bt_field *base) |
| 349 | { |
| 350 | int ret = 0; |
| 351 | struct stack_entry *entry; |
| 352 | |
| 353 | assert(stack); |
| 354 | assert(base); |
| 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); |
| 358 | entry = g_new0(struct stack_entry, 1); |
| 359 | if (!entry) { |
| 360 | BT_LOGE_STR("Failed to allocate one stack entry."); |
| 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 | { |
| 375 | assert(stack); |
| 376 | |
| 377 | return stack->entries->len; |
| 378 | } |
| 379 | |
| 380 | static |
| 381 | void stack_pop(struct stack *stack) |
| 382 | { |
| 383 | assert(stack); |
| 384 | assert(stack_size(stack)); |
| 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); |
| 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 | { |
| 394 | assert(stack); |
| 395 | assert(stack_size(stack)); |
| 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 | { |
| 409 | assert(stack); |
| 410 | |
| 411 | if (!stack_empty(stack)) { |
| 412 | BT_LOGV("Clearing stack: stack-addr=%p, stack-size=%u", |
| 413 | stack, stack->entries->len); |
| 414 | g_ptr_array_remove_range(stack->entries, 0, stack_size(stack)); |
| 415 | } |
| 416 | |
| 417 | assert(stack_empty(stack)); |
| 418 | } |
| 419 | |
| 420 | static inline |
| 421 | enum bt_notif_iter_status notif_iter_status_from_m_status( |
| 422 | enum bt_notif_iter_medium_status m_status) |
| 423 | { |
| 424 | return (int) m_status; |
| 425 | } |
| 426 | |
| 427 | static inline |
| 428 | size_t buf_size_bits(struct bt_notif_iter *notit) |
| 429 | { |
| 430 | return notit->buf.sz * 8; |
| 431 | } |
| 432 | |
| 433 | static inline |
| 434 | size_t buf_available_bits(struct bt_notif_iter *notit) |
| 435 | { |
| 436 | return buf_size_bits(notit) - notit->buf.at; |
| 437 | } |
| 438 | |
| 439 | static inline |
| 440 | size_t packet_at(struct bt_notif_iter *notit) |
| 441 | { |
| 442 | return notit->buf.packet_offset + notit->buf.at; |
| 443 | } |
| 444 | |
| 445 | static inline |
| 446 | void buf_consume_bits(struct bt_notif_iter *notit, size_t incr) |
| 447 | { |
| 448 | BT_LOGV("Advancing cursor: notit-addr=%p, cur-before=%zu, cur-after=%zu", |
| 449 | notit, notit->buf.at, notit->buf.at + incr); |
| 450 | notit->buf.at += incr; |
| 451 | } |
| 452 | |
| 453 | static |
| 454 | enum bt_notif_iter_status request_medium_bytes( |
| 455 | struct bt_notif_iter *notit) |
| 456 | { |
| 457 | uint8_t *buffer_addr = NULL; |
| 458 | size_t buffer_sz = 0; |
| 459 | enum bt_notif_iter_medium_status m_status; |
| 460 | |
| 461 | BT_LOGV("Calling user function (request bytes): notit-addr=%p, " |
| 462 | "request-size=%zu", notit, notit->medium.max_request_sz); |
| 463 | m_status = notit->medium.medops.request_bytes( |
| 464 | notit->medium.max_request_sz, &buffer_addr, |
| 465 | &buffer_sz, notit->medium.data); |
| 466 | BT_LOGV("User function returned: status=%s, buf-addr=%p, buf-size=%zu", |
| 467 | bt_notif_iter_medium_status_string(m_status), |
| 468 | buffer_addr, buffer_sz); |
| 469 | if (m_status == BT_NOTIF_ITER_MEDIUM_STATUS_OK) { |
| 470 | assert(buffer_sz != 0); |
| 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; |
| 477 | notit->buf.last_eh_at = SIZE_MAX; |
| 478 | |
| 479 | /* New medium buffer size */ |
| 480 | notit->buf.sz = buffer_sz; |
| 481 | |
| 482 | /* New medium buffer address */ |
| 483 | notit->buf.addr = buffer_addr; |
| 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); |
| 491 | } else if (m_status == BT_NOTIF_ITER_MEDIUM_STATUS_EOF) { |
| 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 | */ |
| 497 | if (notit->cur_packet_size >= 0) { |
| 498 | if (packet_at(notit) == notit->cur_packet_size) { |
| 499 | goto end; |
| 500 | } |
| 501 | } else { |
| 502 | if (packet_at(notit) == 0) { |
| 503 | goto end; |
| 504 | } |
| 505 | |
| 506 | if (notit->buf.last_eh_at != SIZE_MAX && |
| 507 | notit->buf.at == notit->buf.last_eh_at) { |
| 508 | goto end; |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | /* All other states are invalid */ |
| 513 | BT_LOGW("User function returned %s, but notification iterator is in an unexpected state: " |
| 514 | "state=%s, cur-packet-size=%" PRId64 ", cur=%zu, " |
| 515 | "packet-cur=%zu, last-eh-at=%zu", |
| 516 | bt_notif_iter_medium_status_string(m_status), |
| 517 | state_string(notit->state), |
| 518 | notit->cur_packet_size, |
| 519 | notit->buf.at, packet_at(notit), |
| 520 | notit->buf.last_eh_at); |
| 521 | m_status = BT_NOTIF_ITER_MEDIUM_STATUS_ERROR; |
| 522 | } else if (m_status < 0) { |
| 523 | BT_LOGW("User function failed: status=%s", |
| 524 | bt_notif_iter_medium_status_string(m_status)); |
| 525 | } |
| 526 | |
| 527 | end: |
| 528 | return notif_iter_status_from_m_status(m_status); |
| 529 | } |
| 530 | |
| 531 | static inline |
| 532 | enum bt_notif_iter_status buf_ensure_available_bits( |
| 533 | struct bt_notif_iter *notit) |
| 534 | { |
| 535 | enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; |
| 536 | |
| 537 | if (buf_available_bits(notit) == 0) { |
| 538 | /* |
| 539 | * This _cannot_ return BT_NOTIF_ITER_STATUS_OK |
| 540 | * _and_ no bits. |
| 541 | */ |
| 542 | status = request_medium_bytes(notit); |
| 543 | } |
| 544 | |
| 545 | return status; |
| 546 | } |
| 547 | |
| 548 | static |
| 549 | enum bt_notif_iter_status read_dscope_begin_state( |
| 550 | struct bt_notif_iter *notit, |
| 551 | struct bt_field_type *dscope_field_type, |
| 552 | enum state done_state, enum state continue_state, |
| 553 | struct bt_field **dscope_field) |
| 554 | { |
| 555 | enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; |
| 556 | enum bt_btr_status btr_status; |
| 557 | size_t consumed_bits; |
| 558 | |
| 559 | bt_put(*dscope_field); |
| 560 | notit->cur_dscope_field = dscope_field; |
| 561 | BT_LOGV("Starting BTR: notit-addr=%p, btr-addr=%p, ft-addr=%p", |
| 562 | notit, notit->btr, dscope_field_type); |
| 563 | consumed_bits = bt_btr_start(notit->btr, dscope_field_type, |
| 564 | notit->buf.addr, notit->buf.at, packet_at(notit), |
| 565 | notit->buf.sz, &btr_status); |
| 566 | BT_LOGV("BTR consumed bits: size=%zu", consumed_bits); |
| 567 | |
| 568 | switch (btr_status) { |
| 569 | case BT_BTR_STATUS_OK: |
| 570 | /* type was read completely */ |
| 571 | BT_LOGV_STR("Field was completely decoded."); |
| 572 | notit->state = done_state; |
| 573 | break; |
| 574 | case BT_BTR_STATUS_EOF: |
| 575 | BT_LOGV_STR("BTR needs more data to decode field completely."); |
| 576 | notit->state = continue_state; |
| 577 | break; |
| 578 | default: |
| 579 | BT_LOGW("BTR failed to start: notit-addr=%p, btr-addr=%p, " |
| 580 | "status=%s", notit, notit->btr, |
| 581 | bt_btr_status_string(btr_status)); |
| 582 | status = BT_NOTIF_ITER_STATUS_ERROR; |
| 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 |
| 594 | enum bt_notif_iter_status read_dscope_continue_state( |
| 595 | struct bt_notif_iter *notit, enum state done_state) |
| 596 | { |
| 597 | enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; |
| 598 | enum bt_btr_status btr_status; |
| 599 | size_t consumed_bits; |
| 600 | |
| 601 | BT_LOGV("Continuing BTR: notit-addr=%p, btr-addr=%p", |
| 602 | notit, notit->btr); |
| 603 | |
| 604 | status = buf_ensure_available_bits(notit); |
| 605 | if (status != BT_NOTIF_ITER_STATUS_OK) { |
| 606 | if (status < 0) { |
| 607 | BT_LOGW("Cannot ensure that buffer has at least one byte: " |
| 608 | "notif-addr=%p, status=%s", |
| 609 | notit, bt_notif_iter_status_string(status)); |
| 610 | } else { |
| 611 | BT_LOGV("Cannot ensure that buffer has at least one byte: " |
| 612 | "notif-addr=%p, status=%s", |
| 613 | notit, bt_notif_iter_status_string(status)); |
| 614 | } |
| 615 | |
| 616 | goto end; |
| 617 | } |
| 618 | |
| 619 | |
| 620 | consumed_bits = bt_btr_continue(notit->btr, notit->buf.addr, |
| 621 | notit->buf.sz, &btr_status); |
| 622 | BT_LOGV("BTR consumed bits: size=%zu", consumed_bits); |
| 623 | |
| 624 | switch (btr_status) { |
| 625 | case BT_BTR_STATUS_OK: |
| 626 | /* Type was read completely. */ |
| 627 | BT_LOGV_STR("Field was completely decoded."); |
| 628 | notit->state = done_state; |
| 629 | break; |
| 630 | case BT_BTR_STATUS_EOF: |
| 631 | /* Stay in this continue state. */ |
| 632 | BT_LOGV_STR("BTR needs more data to decode field completely."); |
| 633 | break; |
| 634 | default: |
| 635 | BT_LOGW("BTR failed to continue: notit-addr=%p, btr-addr=%p, " |
| 636 | "status=%s", notit, notit->btr, |
| 637 | bt_btr_status_string(btr_status)); |
| 638 | status = BT_NOTIF_ITER_STATUS_ERROR; |
| 639 | goto end; |
| 640 | } |
| 641 | |
| 642 | /* Consume bits now since we know we're not in an error state. */ |
| 643 | buf_consume_bits(notit, consumed_bits); |
| 644 | end: |
| 645 | return status; |
| 646 | } |
| 647 | |
| 648 | static |
| 649 | void put_event_dscopes(struct bt_notif_iter *notit) |
| 650 | { |
| 651 | BT_LOGV_STR("Putting event header field."); |
| 652 | BT_PUT(notit->dscopes.stream_event_header); |
| 653 | BT_LOGV_STR("Putting stream event context field."); |
| 654 | BT_PUT(notit->dscopes.stream_event_context); |
| 655 | BT_LOGV_STR("Putting event context field."); |
| 656 | BT_PUT(notit->dscopes.event_context); |
| 657 | BT_LOGV_STR("Putting event payload field."); |
| 658 | BT_PUT(notit->dscopes.event_payload); |
| 659 | } |
| 660 | |
| 661 | static |
| 662 | void put_all_dscopes(struct bt_notif_iter *notit) |
| 663 | { |
| 664 | BT_LOGV_STR("Putting packet header field."); |
| 665 | BT_PUT(notit->dscopes.trace_packet_header); |
| 666 | BT_LOGV_STR("Putting packet context field."); |
| 667 | BT_PUT(notit->dscopes.stream_packet_context); |
| 668 | put_event_dscopes(notit); |
| 669 | } |
| 670 | |
| 671 | static |
| 672 | enum bt_notif_iter_status read_packet_header_begin_state( |
| 673 | struct bt_notif_iter *notit) |
| 674 | { |
| 675 | struct bt_field_type *packet_header_type = NULL; |
| 676 | enum bt_notif_iter_status ret = BT_NOTIF_ITER_STATUS_OK; |
| 677 | |
| 678 | if (bt_notif_iter_switch_packet(notit)) { |
| 679 | BT_LOGW("Cannot switch packet: notit-addr=%p", notit); |
| 680 | ret = BT_NOTIF_ITER_STATUS_ERROR; |
| 681 | goto end; |
| 682 | } |
| 683 | |
| 684 | /* Packet header type is common to the whole trace. */ |
| 685 | packet_header_type = bt_trace_get_packet_header_type( |
| 686 | notit->meta.trace); |
| 687 | if (!packet_header_type) { |
| 688 | notit->state = STATE_AFTER_TRACE_PACKET_HEADER; |
| 689 | goto end; |
| 690 | } |
| 691 | |
| 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, |
| 695 | bt_trace_get_name(notit->meta.trace), packet_header_type); |
| 696 | ret = read_dscope_begin_state(notit, packet_header_type, |
| 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, |
| 705 | bt_trace_get_name(notit->meta.trace), |
| 706 | packet_header_type); |
| 707 | } |
| 708 | end: |
| 709 | BT_PUT(packet_header_type); |
| 710 | return ret; |
| 711 | } |
| 712 | |
| 713 | static |
| 714 | enum bt_notif_iter_status read_packet_header_continue_state( |
| 715 | struct bt_notif_iter *notit) |
| 716 | { |
| 717 | return read_dscope_continue_state(notit, |
| 718 | STATE_AFTER_TRACE_PACKET_HEADER); |
| 719 | } |
| 720 | |
| 721 | static inline |
| 722 | bool is_struct_type(struct bt_field_type *field_type) |
| 723 | { |
| 724 | return bt_field_type_get_type_id(field_type) == |
| 725 | BT_FIELD_TYPE_ID_STRUCT; |
| 726 | } |
| 727 | |
| 728 | static inline |
| 729 | bool is_variant_type(struct bt_field_type *field_type) |
| 730 | { |
| 731 | return bt_field_type_get_type_id(field_type) == |
| 732 | BT_FIELD_TYPE_ID_VARIANT; |
| 733 | } |
| 734 | |
| 735 | static |
| 736 | struct stream_class_field_path_cache * |
| 737 | create_stream_class_field_path_cache_entry( |
| 738 | struct bt_notif_iter *notit, |
| 739 | struct bt_stream_class *stream_class) |
| 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); |
| 748 | struct bt_field_type *event_header = NULL, *packet_context = NULL; |
| 749 | |
| 750 | if (!cache_entry) { |
| 751 | BT_LOGE_STR("Failed to allocate one stream class field path cache."); |
| 752 | goto end; |
| 753 | } |
| 754 | |
| 755 | event_header = bt_stream_class_get_event_header_type(stream_class); |
| 756 | if (event_header && bt_field_type_is_structure(event_header)) { |
| 757 | int i, count; |
| 758 | |
| 759 | count = bt_field_type_structure_get_field_count( |
| 760 | event_header); |
| 761 | assert(count >= 0); |
| 762 | |
| 763 | for (i = 0; i < count; i++) { |
| 764 | int ret; |
| 765 | const char *name; |
| 766 | |
| 767 | ret = bt_field_type_structure_get_field_by_index( |
| 768 | event_header, &name, NULL, i); |
| 769 | if (ret) { |
| 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, |
| 776 | bt_stream_class_get_name(stream_class), |
| 777 | bt_stream_class_get_id(stream_class), |
| 778 | event_header, i); |
| 779 | goto error; |
| 780 | } |
| 781 | |
| 782 | if (v != -1 && id != -1) { |
| 783 | break; |
| 784 | } |
| 785 | |
| 786 | if (v == -1 && strcmp(name, "v") == 0) { |
| 787 | v = i; |
| 788 | } else if (id == -1 && !strcmp(name, "id")) { |
| 789 | id = i; |
| 790 | } |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | packet_context = bt_stream_class_get_packet_context_type( |
| 795 | stream_class); |
| 796 | if (packet_context && bt_field_type_is_structure(packet_context)) { |
| 797 | int i, count; |
| 798 | |
| 799 | count = bt_field_type_structure_get_field_count( |
| 800 | packet_context); |
| 801 | assert(count >= 0); |
| 802 | |
| 803 | for (i = 0; i < count; i++) { |
| 804 | int ret; |
| 805 | const char *name; |
| 806 | struct bt_field_type *field_type; |
| 807 | |
| 808 | if (timestamp_end != -1 && packet_size != -1 && |
| 809 | content_size != -1) { |
| 810 | break; |
| 811 | } |
| 812 | |
| 813 | ret = bt_field_type_structure_get_field_by_index( |
| 814 | packet_context, &name, &field_type, i); |
| 815 | if (ret) { |
| 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, |
| 822 | bt_stream_class_get_name(stream_class), |
| 823 | bt_stream_class_get_id(stream_class), |
| 824 | event_header, i); |
| 825 | goto error; |
| 826 | } |
| 827 | |
| 828 | if (timestamp_end == -1 && |
| 829 | strcmp(name, "timestamp_end") == 0) { |
| 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; |
| 840 | g_hash_table_insert(notit->field_overrides, |
| 841 | bt_get(field_type), override); |
| 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( |
| 871 | struct bt_notif_iter *notit, |
| 872 | struct bt_stream_class *stream_class) |
| 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, |
| 882 | stream_class); |
| 883 | g_hash_table_insert(notit->sc_field_path_caches, |
| 884 | bt_get(stream_class), (gpointer) cache_entry); |
| 885 | } |
| 886 | |
| 887 | return cache_entry; |
| 888 | } |
| 889 | |
| 890 | static inline |
| 891 | enum bt_notif_iter_status set_current_stream_class( |
| 892 | struct bt_notif_iter *notit) |
| 893 | { |
| 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; |
| 898 | uint64_t stream_id; |
| 899 | |
| 900 | /* Clear the current stream class field path cache. */ |
| 901 | notit->cur_sc_field_path_cache = NULL; |
| 902 | |
| 903 | /* Is there any "stream_id" field in the packet header? */ |
| 904 | packet_header_type = bt_trace_get_packet_header_type( |
| 905 | notit->meta.trace); |
| 906 | if (!packet_header_type) { |
| 907 | /* |
| 908 | * No packet header, therefore no `stream_id` field, |
| 909 | * therefore only one stream class. |
| 910 | */ |
| 911 | goto single_stream_class; |
| 912 | } |
| 913 | |
| 914 | assert(is_struct_type(packet_header_type)); |
| 915 | |
| 916 | // TODO: optimalize! |
| 917 | stream_id_field_type = |
| 918 | bt_field_type_structure_get_field_type_by_name( |
| 919 | packet_header_type, "stream_id"); |
| 920 | if (stream_id_field_type) { |
| 921 | /* Find appropriate stream class using current stream ID */ |
| 922 | int ret; |
| 923 | struct bt_field *stream_id_field = NULL; |
| 924 | |
| 925 | assert(notit->dscopes.trace_packet_header); |
| 926 | |
| 927 | // TODO: optimalize! |
| 928 | stream_id_field = bt_field_structure_get_field_by_name( |
| 929 | notit->dscopes.trace_packet_header, "stream_id"); |
| 930 | assert(stream_id_field); |
| 931 | ret = bt_field_unsigned_integer_get_value( |
| 932 | stream_id_field, &stream_id); |
| 933 | assert(!ret); |
| 934 | BT_PUT(stream_id_field); |
| 935 | } else { |
| 936 | single_stream_class: |
| 937 | /* Only one stream: pick the first stream class */ |
| 938 | assert(bt_trace_get_stream_class_count( |
| 939 | notit->meta.trace) == 1); |
| 940 | stream_id = 0; |
| 941 | } |
| 942 | |
| 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, |
| 947 | bt_trace_get_name(notit->meta.trace)); |
| 948 | |
| 949 | new_stream_class = bt_trace_get_stream_class_by_id( |
| 950 | notit->meta.trace, stream_id); |
| 951 | if (!new_stream_class) { |
| 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, |
| 956 | bt_trace_get_name(notit->meta.trace)); |
| 957 | status = BT_NOTIF_ITER_STATUS_ERROR; |
| 958 | goto end; |
| 959 | } |
| 960 | |
| 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, |
| 972 | bt_stream_class_get_name(notit->meta.stream_class), |
| 973 | bt_stream_class_get_id(notit->meta.stream_class), |
| 974 | new_stream_class, |
| 975 | bt_stream_class_get_name(new_stream_class), |
| 976 | bt_stream_class_get_id(new_stream_class), |
| 977 | notit->meta.trace, |
| 978 | bt_trace_get_name(notit->meta.trace)); |
| 979 | status = BT_NOTIF_ITER_STATUS_ERROR; |
| 980 | goto end; |
| 981 | } |
| 982 | } else { |
| 983 | BT_MOVE(notit->meta.stream_class, new_stream_class); |
| 984 | } |
| 985 | |
| 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, |
| 990 | bt_stream_class_get_name(notit->meta.stream_class), |
| 991 | bt_stream_class_get_id(notit->meta.stream_class)); |
| 992 | |
| 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( |
| 998 | notit, notit->meta.stream_class); |
| 999 | if (!notit->cur_sc_field_path_cache) { |
| 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, |
| 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; |
| 1007 | goto end; |
| 1008 | } |
| 1009 | end: |
| 1010 | BT_PUT(packet_header_type); |
| 1011 | BT_PUT(stream_id_field_type); |
| 1012 | bt_put(new_stream_class); |
| 1013 | return status; |
| 1014 | } |
| 1015 | |
| 1016 | static |
| 1017 | enum bt_notif_iter_status after_packet_header_state( |
| 1018 | struct bt_notif_iter *notit) |
| 1019 | { |
| 1020 | enum bt_notif_iter_status status; |
| 1021 | |
| 1022 | status = set_current_stream_class(notit); |
| 1023 | if (status == BT_NOTIF_ITER_STATUS_OK) { |
| 1024 | notit->state = STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN; |
| 1025 | } |
| 1026 | |
| 1027 | return status; |
| 1028 | } |
| 1029 | |
| 1030 | static |
| 1031 | enum bt_notif_iter_status read_packet_context_begin_state( |
| 1032 | struct bt_notif_iter *notit) |
| 1033 | { |
| 1034 | enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; |
| 1035 | struct bt_field_type *packet_context_type; |
| 1036 | |
| 1037 | assert(notit->meta.stream_class); |
| 1038 | packet_context_type = bt_stream_class_get_packet_context_type( |
| 1039 | notit->meta.stream_class); |
| 1040 | if (!packet_context_type) { |
| 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, |
| 1045 | bt_stream_class_get_name(notit->meta.stream_class), |
| 1046 | bt_stream_class_get_id(notit->meta.stream_class)); |
| 1047 | notit->state = STATE_AFTER_STREAM_PACKET_CONTEXT; |
| 1048 | goto end; |
| 1049 | } |
| 1050 | |
| 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, |
| 1056 | bt_stream_class_get_name(notit->meta.stream_class), |
| 1057 | bt_stream_class_get_id(notit->meta.stream_class), |
| 1058 | packet_context_type); |
| 1059 | status = read_dscope_begin_state(notit, packet_context_type, |
| 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, |
| 1069 | bt_stream_class_get_name(notit->meta.stream_class), |
| 1070 | bt_stream_class_get_id(notit->meta.stream_class), |
| 1071 | packet_context_type); |
| 1072 | } |
| 1073 | |
| 1074 | end: |
| 1075 | BT_PUT(packet_context_type); |
| 1076 | return status; |
| 1077 | } |
| 1078 | |
| 1079 | static |
| 1080 | enum bt_notif_iter_status read_packet_context_continue_state( |
| 1081 | struct bt_notif_iter *notit) |
| 1082 | { |
| 1083 | return read_dscope_continue_state(notit, |
| 1084 | STATE_AFTER_STREAM_PACKET_CONTEXT); |
| 1085 | } |
| 1086 | |
| 1087 | static |
| 1088 | enum bt_notif_iter_status set_current_packet_content_sizes( |
| 1089 | struct bt_notif_iter *notit) |
| 1090 | { |
| 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; |
| 1094 | uint64_t content_size = -1ULL, packet_size = -1ULL; |
| 1095 | |
| 1096 | if (!notit->dscopes.stream_packet_context) { |
| 1097 | goto end; |
| 1098 | } |
| 1099 | |
| 1100 | packet_size_field = bt_field_structure_get_field_by_name( |
| 1101 | notit->dscopes.stream_packet_context, "packet_size"); |
| 1102 | content_size_field = bt_field_structure_get_field_by_name( |
| 1103 | notit->dscopes.stream_packet_context, "content_size"); |
| 1104 | if (packet_size_field) { |
| 1105 | int ret = bt_field_unsigned_integer_get_value( |
| 1106 | packet_size_field, &packet_size); |
| 1107 | |
| 1108 | assert(ret == 0); |
| 1109 | if (packet_size == 0) { |
| 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); |
| 1113 | status = BT_NOTIF_ITER_STATUS_ERROR; |
| 1114 | goto end; |
| 1115 | } else if ((packet_size % 8) != 0) { |
| 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); |
| 1121 | status = BT_NOTIF_ITER_STATUS_ERROR; |
| 1122 | goto end; |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | if (content_size_field) { |
| 1127 | int ret = bt_field_unsigned_integer_get_value( |
| 1128 | content_size_field, &content_size); |
| 1129 | |
| 1130 | assert(ret == 0); |
| 1131 | } else { |
| 1132 | content_size = packet_size; |
| 1133 | } |
| 1134 | |
| 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); |
| 1141 | status = BT_NOTIF_ITER_STATUS_ERROR; |
| 1142 | goto end; |
| 1143 | } |
| 1144 | |
| 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 | } |
| 1155 | notit->cur_content_size = content_size; |
| 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); |
| 1159 | end: |
| 1160 | BT_PUT(packet_size_field); |
| 1161 | BT_PUT(content_size_field); |
| 1162 | return status; |
| 1163 | } |
| 1164 | |
| 1165 | static |
| 1166 | enum bt_notif_iter_status after_packet_context_state( |
| 1167 | struct bt_notif_iter *notit) |
| 1168 | { |
| 1169 | enum bt_notif_iter_status status; |
| 1170 | |
| 1171 | status = set_current_packet_content_sizes(notit); |
| 1172 | if (status == BT_NOTIF_ITER_STATUS_OK) { |
| 1173 | notit->state = STATE_EMIT_NOTIF_NEW_PACKET; |
| 1174 | } |
| 1175 | |
| 1176 | return status; |
| 1177 | } |
| 1178 | |
| 1179 | static |
| 1180 | enum bt_notif_iter_status read_event_header_begin_state( |
| 1181 | struct bt_notif_iter *notit) |
| 1182 | { |
| 1183 | enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; |
| 1184 | struct bt_field_type *event_header_type = NULL; |
| 1185 | |
| 1186 | /* Reset the position of the last event header */ |
| 1187 | notit->buf.last_eh_at = notit->buf.at; |
| 1188 | |
| 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! */ |
| 1193 | BT_LOGV("Reached end of packet: notit-addr=%p, " |
| 1194 | "cur=%zu", notit, packet_at(notit)); |
| 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 */ |
| 1199 | BT_LOGV("Before decoding event header field: cursor is passed the packet's content: " |
| 1200 | "notit-addr=%p, content-size=%" PRId64 ", " |
| 1201 | "cur=%zu", notit, notit->cur_content_size, |
| 1202 | packet_at(notit)); |
| 1203 | status = BT_NOTIF_ITER_STATUS_ERROR; |
| 1204 | goto end; |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | event_header_type = bt_stream_class_get_event_header_type( |
| 1209 | notit->meta.stream_class); |
| 1210 | if (!event_header_type) { |
| 1211 | notit->state = STATE_AFTER_STREAM_EVENT_HEADER; |
| 1212 | goto end; |
| 1213 | } |
| 1214 | |
| 1215 | put_event_dscopes(notit); |
| 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, |
| 1221 | bt_stream_class_get_name(notit->meta.stream_class), |
| 1222 | bt_stream_class_get_id(notit->meta.stream_class), |
| 1223 | event_header_type); |
| 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); |
| 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, |
| 1234 | bt_stream_class_get_name(notit->meta.stream_class), |
| 1235 | bt_stream_class_get_id(notit->meta.stream_class), |
| 1236 | event_header_type); |
| 1237 | } |
| 1238 | end: |
| 1239 | BT_PUT(event_header_type); |
| 1240 | |
| 1241 | return status; |
| 1242 | } |
| 1243 | |
| 1244 | static |
| 1245 | enum bt_notif_iter_status read_event_header_continue_state( |
| 1246 | struct bt_notif_iter *notit) |
| 1247 | { |
| 1248 | return read_dscope_continue_state(notit, |
| 1249 | STATE_AFTER_STREAM_EVENT_HEADER); |
| 1250 | } |
| 1251 | |
| 1252 | static inline |
| 1253 | enum bt_notif_iter_status set_current_event_class(struct bt_notif_iter *notit) |
| 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 | |
| 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; |
| 1265 | uint64_t event_id = -1ULL; |
| 1266 | int ret; |
| 1267 | |
| 1268 | event_header_type = bt_stream_class_get_event_header_type( |
| 1269 | notit->meta.stream_class); |
| 1270 | if (!event_header_type) { |
| 1271 | /* |
| 1272 | * No event header, therefore no event class ID field, |
| 1273 | * therefore only one event class. |
| 1274 | */ |
| 1275 | goto single_event_class; |
| 1276 | } |
| 1277 | |
| 1278 | /* Is there any "id"/"v" field in the event header? */ |
| 1279 | assert(is_struct_type(event_header_type)); |
| 1280 | id_field_type = bt_field_type_structure_get_field_type_by_name( |
| 1281 | event_header_type, "id"); |
| 1282 | v_field_type = bt_field_type_structure_get_field_type_by_name( |
| 1283 | event_header_type, "v"); |
| 1284 | assert(notit->dscopes.stream_event_header); |
| 1285 | if (v_field_type) { |
| 1286 | /* |
| 1287 | * _ _____ _____ |
| 1288 | * | | |_ _|_ _| __ __ _ |
| 1289 | * | | | | | || '_ \ / _` | |
| 1290 | * | |___| | | || | | | (_| | S P E C I A L |
| 1291 | * |_____|_| |_||_| |_|\__, | C A S E â„¢ |
| 1292 | * |___/ |
| 1293 | */ |
| 1294 | struct bt_field *v_field = NULL; |
| 1295 | struct bt_field *v_struct_field = NULL; |
| 1296 | struct bt_field *v_struct_id_field = NULL; |
| 1297 | |
| 1298 | // TODO: optimalize! |
| 1299 | v_field = bt_field_structure_get_field_by_name( |
| 1300 | notit->dscopes.stream_event_header, "v"); |
| 1301 | assert(v_field); |
| 1302 | |
| 1303 | v_struct_field = |
| 1304 | bt_field_variant_get_current_field(v_field); |
| 1305 | if (!v_struct_field) { |
| 1306 | goto end_v_field_type; |
| 1307 | } |
| 1308 | |
| 1309 | // TODO: optimalize! |
| 1310 | v_struct_id_field = |
| 1311 | bt_field_structure_get_field_by_name(v_struct_field, "id"); |
| 1312 | if (!v_struct_id_field) { |
| 1313 | goto end_v_field_type; |
| 1314 | } |
| 1315 | |
| 1316 | if (bt_field_is_integer(v_struct_id_field)) { |
| 1317 | ret = bt_field_unsigned_integer_get_value( |
| 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 | } |
| 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 */ |
| 1335 | struct bt_field *id_field = NULL; |
| 1336 | int ret_get_value = 0; |
| 1337 | |
| 1338 | // TODO: optimalize! |
| 1339 | id_field = bt_field_structure_get_field_by_name( |
| 1340 | notit->dscopes.stream_event_header, "id"); |
| 1341 | if (!id_field) { |
| 1342 | goto check_event_id; |
| 1343 | } |
| 1344 | |
| 1345 | if (bt_field_is_integer(id_field)) { |
| 1346 | ret_get_value = bt_field_unsigned_integer_get_value( |
| 1347 | id_field, &event_id); |
| 1348 | } else if (bt_field_is_enumeration(id_field)) { |
| 1349 | struct bt_field *container; |
| 1350 | |
| 1351 | container = bt_field_enumeration_get_container( |
| 1352 | id_field); |
| 1353 | assert(container); |
| 1354 | ret_get_value = bt_field_unsigned_integer_get_value( |
| 1355 | container, &event_id); |
| 1356 | BT_PUT(container); |
| 1357 | } |
| 1358 | |
| 1359 | assert(ret_get_value == 0); |
| 1360 | BT_PUT(id_field); |
| 1361 | } |
| 1362 | |
| 1363 | check_event_id: |
| 1364 | if (event_id == -1ULL) { |
| 1365 | single_event_class: |
| 1366 | /* Event ID not found: single event? */ |
| 1367 | assert(bt_stream_class_get_event_class_count( |
| 1368 | notit->meta.stream_class) == 1); |
| 1369 | event_id = 0; |
| 1370 | } |
| 1371 | |
| 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, |
| 1377 | bt_stream_class_get_name(notit->meta.stream_class), |
| 1378 | bt_stream_class_get_id(notit->meta.stream_class), |
| 1379 | event_id); |
| 1380 | BT_PUT(notit->meta.event_class); |
| 1381 | notit->meta.event_class = bt_stream_class_get_event_class_by_id( |
| 1382 | notit->meta.stream_class, event_id); |
| 1383 | if (!notit->meta.event_class) { |
| 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, |
| 1390 | bt_stream_class_get_name(notit->meta.stream_class), |
| 1391 | bt_stream_class_get_id(notit->meta.stream_class), |
| 1392 | event_id); |
| 1393 | status = BT_NOTIF_ITER_STATUS_ERROR; |
| 1394 | goto end; |
| 1395 | } |
| 1396 | |
| 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, |
| 1401 | bt_event_class_get_name(notit->meta.event_class), |
| 1402 | bt_event_class_get_id(notit->meta.event_class)); |
| 1403 | |
| 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 |
| 1413 | enum bt_notif_iter_status after_event_header_state( |
| 1414 | struct bt_notif_iter *notit) |
| 1415 | { |
| 1416 | enum bt_notif_iter_status status; |
| 1417 | |
| 1418 | status = set_current_event_class(notit); |
| 1419 | if (status != BT_NOTIF_ITER_STATUS_OK) { |
| 1420 | goto end; |
| 1421 | } |
| 1422 | |
| 1423 | notit->state = STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN; |
| 1424 | |
| 1425 | end: |
| 1426 | return status; |
| 1427 | } |
| 1428 | |
| 1429 | static |
| 1430 | enum bt_notif_iter_status read_stream_event_context_begin_state( |
| 1431 | struct bt_notif_iter *notit) |
| 1432 | { |
| 1433 | enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; |
| 1434 | struct bt_field_type *stream_event_context_type; |
| 1435 | |
| 1436 | stream_event_context_type = bt_stream_class_get_event_context_type( |
| 1437 | notit->meta.stream_class); |
| 1438 | if (!stream_event_context_type) { |
| 1439 | notit->state = STATE_DSCOPE_EVENT_CONTEXT_BEGIN; |
| 1440 | goto end; |
| 1441 | } |
| 1442 | |
| 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, |
| 1448 | bt_stream_class_get_name(notit->meta.stream_class), |
| 1449 | bt_stream_class_get_id(notit->meta.stream_class), |
| 1450 | stream_event_context_type); |
| 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); |
| 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, |
| 1461 | bt_stream_class_get_name(notit->meta.stream_class), |
| 1462 | bt_stream_class_get_id(notit->meta.stream_class), |
| 1463 | stream_event_context_type); |
| 1464 | } |
| 1465 | |
| 1466 | end: |
| 1467 | BT_PUT(stream_event_context_type); |
| 1468 | |
| 1469 | return status; |
| 1470 | } |
| 1471 | |
| 1472 | static |
| 1473 | enum bt_notif_iter_status read_stream_event_context_continue_state( |
| 1474 | struct bt_notif_iter *notit) |
| 1475 | { |
| 1476 | return read_dscope_continue_state(notit, |
| 1477 | STATE_DSCOPE_EVENT_CONTEXT_BEGIN); |
| 1478 | } |
| 1479 | |
| 1480 | static |
| 1481 | enum bt_notif_iter_status read_event_context_begin_state( |
| 1482 | struct bt_notif_iter *notit) |
| 1483 | { |
| 1484 | enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; |
| 1485 | struct bt_field_type *event_context_type; |
| 1486 | |
| 1487 | event_context_type = bt_event_class_get_context_type( |
| 1488 | notit->meta.event_class); |
| 1489 | if (!event_context_type) { |
| 1490 | notit->state = STATE_DSCOPE_EVENT_PAYLOAD_BEGIN; |
| 1491 | goto end; |
| 1492 | } |
| 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, |
| 1499 | bt_event_class_get_name(notit->meta.event_class), |
| 1500 | bt_event_class_get_id(notit->meta.event_class), |
| 1501 | event_context_type); |
| 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); |
| 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, |
| 1512 | bt_event_class_get_name(notit->meta.event_class), |
| 1513 | bt_event_class_get_id(notit->meta.event_class), |
| 1514 | event_context_type); |
| 1515 | } |
| 1516 | |
| 1517 | end: |
| 1518 | BT_PUT(event_context_type); |
| 1519 | |
| 1520 | return status; |
| 1521 | } |
| 1522 | |
| 1523 | static |
| 1524 | enum bt_notif_iter_status read_event_context_continue_state( |
| 1525 | struct bt_notif_iter *notit) |
| 1526 | { |
| 1527 | return read_dscope_continue_state(notit, |
| 1528 | STATE_DSCOPE_EVENT_PAYLOAD_BEGIN); |
| 1529 | } |
| 1530 | |
| 1531 | static |
| 1532 | enum bt_notif_iter_status read_event_payload_begin_state( |
| 1533 | struct bt_notif_iter *notit) |
| 1534 | { |
| 1535 | enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; |
| 1536 | struct bt_field_type *event_payload_type; |
| 1537 | |
| 1538 | event_payload_type = bt_event_class_get_payload_type( |
| 1539 | notit->meta.event_class); |
| 1540 | if (!event_payload_type) { |
| 1541 | notit->state = STATE_EMIT_NOTIF_EVENT; |
| 1542 | goto end; |
| 1543 | } |
| 1544 | |
| 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, |
| 1550 | bt_event_class_get_name(notit->meta.event_class), |
| 1551 | bt_event_class_get_id(notit->meta.event_class), |
| 1552 | event_payload_type); |
| 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); |
| 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, |
| 1563 | bt_event_class_get_name(notit->meta.event_class), |
| 1564 | bt_event_class_get_id(notit->meta.event_class), |
| 1565 | event_payload_type); |
| 1566 | } |
| 1567 | |
| 1568 | end: |
| 1569 | BT_PUT(event_payload_type); |
| 1570 | |
| 1571 | return status; |
| 1572 | } |
| 1573 | |
| 1574 | static |
| 1575 | enum bt_notif_iter_status read_event_payload_continue_state( |
| 1576 | struct bt_notif_iter *notit) |
| 1577 | { |
| 1578 | return read_dscope_continue_state(notit, STATE_EMIT_NOTIF_EVENT); |
| 1579 | } |
| 1580 | |
| 1581 | static |
| 1582 | enum bt_notif_iter_status skip_packet_padding_state( |
| 1583 | struct bt_notif_iter *notit) |
| 1584 | { |
| 1585 | enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; |
| 1586 | size_t bits_to_skip; |
| 1587 | |
| 1588 | assert(notit->cur_packet_size > 0); |
| 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; |
| 1595 | |
| 1596 | BT_LOGV("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu", |
| 1597 | bits_to_skip, notit, bits_to_skip); |
| 1598 | status = buf_ensure_available_bits(notit); |
| 1599 | if (status != BT_NOTIF_ITER_STATUS_OK) { |
| 1600 | goto end; |
| 1601 | } |
| 1602 | |
| 1603 | bits_to_consume = MIN(buf_available_bits(notit), bits_to_skip); |
| 1604 | BT_LOGV("Skipping %zu bits of padding: notit-addr=%p, size=%zu", |
| 1605 | bits_to_consume, notit, bits_to_consume); |
| 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 |
| 1619 | enum bt_notif_iter_status handle_state(struct bt_notif_iter *notit) |
| 1620 | { |
| 1621 | enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; |
| 1622 | const enum state state = notit->state; |
| 1623 | |
| 1624 | BT_LOGV("Handling state: notit-addr=%p, state=%s", |
| 1625 | notit, state_string(state)); |
| 1626 | |
| 1627 | // TODO: optimalize! |
| 1628 | switch (state) { |
| 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; |
| 1689 | default: |
| 1690 | BT_LOGD("Unknown CTF plugin notification iterator state: " |
| 1691 | "notit-addr=%p, state=%d", notit, notit->state); |
| 1692 | abort(); |
| 1693 | } |
| 1694 | |
| 1695 | BT_LOGV("Handled state: notit-addr=%p, status=%s, " |
| 1696 | "prev-state=%s, cur-state=%s", |
| 1697 | notit, bt_notif_iter_status_string(status), |
| 1698 | state_string(state), state_string(notit->state)); |
| 1699 | return status; |
| 1700 | } |
| 1701 | |
| 1702 | /** |
| 1703 | * Resets the internal state of a CTF notification iterator. |
| 1704 | */ |
| 1705 | static |
| 1706 | void bt_notif_iter_reset(struct bt_notif_iter *notit) |
| 1707 | { |
| 1708 | assert(notit); |
| 1709 | BT_LOGD("Resetting notification iterator: addr=%p", notit); |
| 1710 | stack_clear(notit->stack); |
| 1711 | BT_PUT(notit->meta.stream_class); |
| 1712 | BT_PUT(notit->meta.event_class); |
| 1713 | BT_PUT(notit->packet); |
| 1714 | BT_PUT(notit->stream); |
| 1715 | put_all_dscopes(notit); |
| 1716 | notit->buf.addr = NULL; |
| 1717 | notit->buf.sz = 0; |
| 1718 | notit->buf.at = 0; |
| 1719 | notit->buf.last_eh_at = SIZE_MAX; |
| 1720 | notit->buf.packet_offset = 0; |
| 1721 | notit->state = STATE_INIT; |
| 1722 | notit->cur_content_size = -1; |
| 1723 | notit->cur_packet_size = -1; |
| 1724 | notit->cur_packet_offset = -1; |
| 1725 | } |
| 1726 | |
| 1727 | static |
| 1728 | int bt_notif_iter_switch_packet(struct bt_notif_iter *notit) |
| 1729 | { |
| 1730 | int ret = 0; |
| 1731 | |
| 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 | */ |
| 1737 | assert(notit); |
| 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); |
| 1744 | stack_clear(notit->stack); |
| 1745 | BT_PUT(notit->meta.event_class); |
| 1746 | BT_PUT(notit->packet); |
| 1747 | BT_PUT(notit->cur_timestamp_end); |
| 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) { |
| 1759 | BT_LOGW("Cannot switch packet: current position is not a multiple of 8: " |
| 1760 | "notit-addr=%p, cur=%zu", notit, notit->buf.at); |
| 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; |
| 1769 | BT_LOGV("Adjusted buffer: addr=%p, size=%zu", |
| 1770 | notit->buf.addr, notit->buf.sz); |
| 1771 | } |
| 1772 | |
| 1773 | notit->cur_content_size = -1; |
| 1774 | notit->cur_packet_size = -1; |
| 1775 | notit->cur_sc_field_path_cache = NULL; |
| 1776 | |
| 1777 | end: |
| 1778 | return ret; |
| 1779 | } |
| 1780 | |
| 1781 | static |
| 1782 | struct bt_field *get_next_field(struct bt_notif_iter *notit) |
| 1783 | { |
| 1784 | struct bt_field *next_field = NULL; |
| 1785 | struct bt_field *base_field; |
| 1786 | struct bt_field_type *base_type; |
| 1787 | size_t index; |
| 1788 | |
| 1789 | assert(!stack_empty(notit->stack)); |
| 1790 | index = stack_top(notit->stack)->index; |
| 1791 | base_field = stack_top(notit->stack)->base; |
| 1792 | assert(base_field); |
| 1793 | base_type = bt_field_get_type(base_field); |
| 1794 | assert(base_type); |
| 1795 | |
| 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( |
| 1799 | base_field, index); |
| 1800 | break; |
| 1801 | case BT_FIELD_TYPE_ID_ARRAY: |
| 1802 | next_field = bt_field_array_get_field(base_field, index); |
| 1803 | break; |
| 1804 | case BT_FIELD_TYPE_ID_SEQUENCE: |
| 1805 | next_field = bt_field_sequence_get_field(base_field, index); |
| 1806 | break; |
| 1807 | case BT_FIELD_TYPE_ID_VARIANT: |
| 1808 | next_field = bt_field_variant_get_current_field(base_field); |
| 1809 | break; |
| 1810 | default: |
| 1811 | BT_LOGF("Unknown base field type ID: " |
| 1812 | "notit-addr=%p, ft-addr=%p, ft-id=%s", |
| 1813 | notit, base_type, |
| 1814 | bt_field_type_id_string( |
| 1815 | bt_field_type_get_type_id(base_type))); |
| 1816 | abort(); |
| 1817 | } |
| 1818 | |
| 1819 | BT_PUT(base_type); |
| 1820 | return next_field; |
| 1821 | } |
| 1822 | |
| 1823 | static |
| 1824 | void update_clock_state(uint64_t *state, |
| 1825 | struct bt_field *value_field) |
| 1826 | { |
| 1827 | struct bt_field_type *value_type = NULL; |
| 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 | |
| 1834 | value_type = bt_field_get_type(value_field); |
| 1835 | assert(value_type); |
| 1836 | assert(bt_field_type_is_integer(value_type)); |
| 1837 | requested_new_value_size = |
| 1838 | bt_field_type_integer_get_size(value_type); |
| 1839 | assert(requested_new_value_size > 0); |
| 1840 | ret = bt_field_unsigned_integer_get_value(value_field, |
| 1841 | &requested_new_value); |
| 1842 | assert(!ret); |
| 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: |
| 1872 | BT_LOGV("Updated clock's value from integer field's value: " |
| 1873 | "value=%" PRIu64, *state); |
| 1874 | bt_put(value_type); |
| 1875 | } |
| 1876 | |
| 1877 | static |
| 1878 | enum bt_btr_status update_clock(struct bt_notif_iter *notit, |
| 1879 | struct bt_field *int_field) |
| 1880 | { |
| 1881 | gboolean clock_class_found; |
| 1882 | uint64_t *clock_state = NULL; |
| 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; |
| 1886 | |
| 1887 | int_field_type = bt_field_get_type(int_field); |
| 1888 | assert(int_field_type); |
| 1889 | clock_class = bt_field_type_integer_get_mapped_clock_class( |
| 1890 | int_field_type); |
| 1891 | if (likely(!clock_class)) { |
| 1892 | goto end; |
| 1893 | } |
| 1894 | |
| 1895 | clock_class_found = g_hash_table_lookup_extended(notit->clock_states, |
| 1896 | clock_class, NULL, (gpointer) &clock_state); |
| 1897 | if (!clock_class_found) { |
| 1898 | clock_state = g_new0(uint64_t, 1); |
| 1899 | if (!clock_state) { |
| 1900 | BT_LOGE_STR("Failed to allocate a uint64_t."); |
| 1901 | ret = BT_BTR_STATUS_ENOMEM; |
| 1902 | goto end; |
| 1903 | } |
| 1904 | |
| 1905 | g_hash_table_insert(notit->clock_states, bt_get(clock_class), |
| 1906 | clock_state); |
| 1907 | } |
| 1908 | |
| 1909 | /* Update the clock's state. */ |
| 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, |
| 1914 | bt_clock_class_get_name(clock_class), *clock_state); |
| 1915 | update_clock_state(clock_state, int_field); |
| 1916 | end: |
| 1917 | bt_put(int_field_type); |
| 1918 | bt_put(clock_class); |
| 1919 | return ret; |
| 1920 | } |
| 1921 | |
| 1922 | static |
| 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; |
| 1931 | int ret; |
| 1932 | |
| 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, |
| 1937 | bt_field_type_id_string( |
| 1938 | bt_field_type_get_type_id(type)), |
| 1939 | value); |
| 1940 | |
| 1941 | /* Create next field */ |
| 1942 | field = get_next_field(notit); |
| 1943 | if (!field) { |
| 1944 | BT_LOGW("Cannot get next field: notit-addr=%p", notit); |
| 1945 | status = BT_BTR_STATUS_ERROR; |
| 1946 | goto end_no_put; |
| 1947 | } |
| 1948 | |
| 1949 | switch(bt_field_type_get_type_id(type)) { |
| 1950 | case BT_FIELD_TYPE_ID_INTEGER: |
| 1951 | /* Integer field is created field */ |
| 1952 | BT_MOVE(int_field, field); |
| 1953 | bt_get(type); |
| 1954 | break; |
| 1955 | case BT_FIELD_TYPE_ID_ENUM: |
| 1956 | int_field = bt_field_enumeration_get_container(field); |
| 1957 | assert(int_field); |
| 1958 | type = bt_field_get_type(int_field); |
| 1959 | assert(type); |
| 1960 | break; |
| 1961 | default: |
| 1962 | BT_LOGF("Unexpected field type ID: " |
| 1963 | "notit-addr=%p, ft-addr=%p, ft-id=%s", |
| 1964 | notit, type, |
| 1965 | bt_field_type_id_string( |
| 1966 | bt_field_type_get_type_id(type))); |
| 1967 | abort(); |
| 1968 | } |
| 1969 | |
| 1970 | assert(int_field); |
| 1971 | ret = bt_field_unsigned_integer_set_value(int_field, value); |
| 1972 | assert(ret == 0); |
| 1973 | stack_top(notit->stack)->index++; |
| 1974 | *out_int_field = int_field; |
| 1975 | BT_PUT(field); |
| 1976 | BT_PUT(type); |
| 1977 | |
| 1978 | end_no_put: |
| 1979 | return status; |
| 1980 | } |
| 1981 | |
| 1982 | static |
| 1983 | enum bt_btr_status btr_timestamp_end_cb(void *value, |
| 1984 | struct bt_field_type *type, void *data) |
| 1985 | { |
| 1986 | enum bt_btr_status status; |
| 1987 | struct bt_field *field = NULL; |
| 1988 | struct bt_notif_iter *notit = data; |
| 1989 | |
| 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, |
| 1994 | bt_field_type_id_string( |
| 1995 | bt_field_type_get_type_id(type))); |
| 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 | |
| 2004 | static |
| 2005 | enum bt_btr_status btr_unsigned_int_cb(uint64_t value, |
| 2006 | struct bt_field_type *type, void *data) |
| 2007 | { |
| 2008 | struct bt_notif_iter *notit = data; |
| 2009 | enum bt_btr_status status = BT_BTR_STATUS_OK; |
| 2010 | struct bt_field *field = NULL; |
| 2011 | struct field_cb_override *override; |
| 2012 | |
| 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, |
| 2017 | bt_field_type_id_string( |
| 2018 | bt_field_type_get_type_id(type)), |
| 2019 | value); |
| 2020 | override = g_hash_table_lookup(notit->field_overrides, type); |
| 2021 | if (unlikely(override)) { |
| 2022 | /* Override function logs errors */ |
| 2023 | status = override->func(&value, type, override->data); |
| 2024 | goto end; |
| 2025 | } |
| 2026 | |
| 2027 | status = btr_unsigned_int_common(value, type, data, &field); |
| 2028 | if (status != BT_BTR_STATUS_OK) { |
| 2029 | /* btr_unsigned_int_common() logs errors */ |
| 2030 | goto end; |
| 2031 | } |
| 2032 | |
| 2033 | status = update_clock(notit, field); |
| 2034 | BT_PUT(field); |
| 2035 | end: |
| 2036 | return status; |
| 2037 | } |
| 2038 | |
| 2039 | static |
| 2040 | enum bt_btr_status btr_signed_int_cb(int64_t value, |
| 2041 | struct bt_field_type *type, void *data) |
| 2042 | { |
| 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; |
| 2047 | int ret; |
| 2048 | |
| 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, |
| 2053 | bt_field_type_id_string( |
| 2054 | bt_field_type_get_type_id(type)), |
| 2055 | value); |
| 2056 | |
| 2057 | /* create next field */ |
| 2058 | field = get_next_field(notit); |
| 2059 | if (!field) { |
| 2060 | BT_LOGW("Cannot get next field: notit-addr=%p", notit); |
| 2061 | status = BT_BTR_STATUS_ERROR; |
| 2062 | goto end_no_put; |
| 2063 | } |
| 2064 | |
| 2065 | switch(bt_field_type_get_type_id(type)) { |
| 2066 | case BT_FIELD_TYPE_ID_INTEGER: |
| 2067 | /* Integer field is created field */ |
| 2068 | BT_MOVE(int_field, field); |
| 2069 | bt_get(type); |
| 2070 | break; |
| 2071 | case BT_FIELD_TYPE_ID_ENUM: |
| 2072 | int_field = bt_field_enumeration_get_container(field); |
| 2073 | assert(int_field); |
| 2074 | type = bt_field_get_type(int_field); |
| 2075 | assert(type); |
| 2076 | break; |
| 2077 | default: |
| 2078 | BT_LOGF("Unexpected field type ID: " |
| 2079 | "notit-addr=%p, ft-addr=%p, ft-id=%s", |
| 2080 | notit, type, |
| 2081 | bt_field_type_id_string( |
| 2082 | bt_field_type_get_type_id(type))); |
| 2083 | abort(); |
| 2084 | } |
| 2085 | |
| 2086 | assert(int_field); |
| 2087 | ret = bt_field_signed_integer_set_value(int_field, value); |
| 2088 | assert(!ret); |
| 2089 | stack_top(notit->stack)->index++; |
| 2090 | status = update_clock(notit, int_field); |
| 2091 | BT_PUT(field); |
| 2092 | BT_PUT(int_field); |
| 2093 | BT_PUT(type); |
| 2094 | |
| 2095 | end_no_put: |
| 2096 | return status; |
| 2097 | } |
| 2098 | |
| 2099 | static |
| 2100 | enum bt_btr_status btr_floating_point_cb(double value, |
| 2101 | struct bt_field_type *type, void *data) |
| 2102 | { |
| 2103 | enum bt_btr_status status = BT_BTR_STATUS_OK; |
| 2104 | struct bt_field *field = NULL; |
| 2105 | struct bt_notif_iter *notit = data; |
| 2106 | int ret; |
| 2107 | |
| 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, |
| 2112 | bt_field_type_id_string( |
| 2113 | bt_field_type_get_type_id(type)), |
| 2114 | value); |
| 2115 | |
| 2116 | /* Create next field */ |
| 2117 | field = get_next_field(notit); |
| 2118 | if (!field) { |
| 2119 | BT_LOGW("Cannot get next field: notit-addr=%p", notit); |
| 2120 | status = BT_BTR_STATUS_ERROR; |
| 2121 | goto end; |
| 2122 | } |
| 2123 | |
| 2124 | ret = bt_field_floating_point_set_value(field, value); |
| 2125 | assert(!ret); |
| 2126 | stack_top(notit->stack)->index++; |
| 2127 | |
| 2128 | end: |
| 2129 | BT_PUT(field); |
| 2130 | return status; |
| 2131 | } |
| 2132 | |
| 2133 | static |
| 2134 | enum bt_btr_status btr_string_begin_cb( |
| 2135 | struct bt_field_type *type, void *data) |
| 2136 | { |
| 2137 | enum bt_btr_status status = BT_BTR_STATUS_OK; |
| 2138 | struct bt_field *field = NULL; |
| 2139 | struct bt_notif_iter *notit = data; |
| 2140 | int ret; |
| 2141 | |
| 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, |
| 2146 | bt_field_type_id_string( |
| 2147 | bt_field_type_get_type_id(type))); |
| 2148 | |
| 2149 | /* Create next field */ |
| 2150 | field = get_next_field(notit); |
| 2151 | if (!field) { |
| 2152 | BT_LOGW("Cannot get next field: notit-addr=%p", notit); |
| 2153 | status = BT_BTR_STATUS_ERROR; |
| 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) { |
| 2164 | BT_LOGE("Cannot push string field on stack: " |
| 2165 | "notit-addr=%p, field-addr=%p", notit, field); |
| 2166 | status = BT_BTR_STATUS_ERROR; |
| 2167 | goto end; |
| 2168 | } |
| 2169 | |
| 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 | */ |
| 2175 | ret = bt_field_string_set_value(field, ""); |
| 2176 | if (ret) { |
| 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); |
| 2180 | status = BT_BTR_STATUS_ERROR; |
| 2181 | goto end; |
| 2182 | } |
| 2183 | |
| 2184 | end: |
| 2185 | BT_PUT(field); |
| 2186 | |
| 2187 | return status; |
| 2188 | } |
| 2189 | |
| 2190 | static |
| 2191 | enum bt_btr_status btr_string_cb(const char *value, |
| 2192 | size_t len, struct bt_field_type *type, void *data) |
| 2193 | { |
| 2194 | enum bt_btr_status status = BT_BTR_STATUS_OK; |
| 2195 | struct bt_field *field = NULL; |
| 2196 | struct bt_notif_iter *notit = data; |
| 2197 | int ret; |
| 2198 | |
| 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, |
| 2203 | bt_field_type_id_string( |
| 2204 | bt_field_type_get_type_id(type)), |
| 2205 | len); |
| 2206 | |
| 2207 | /* Get string field */ |
| 2208 | field = stack_top(notit->stack)->base; |
| 2209 | assert(field); |
| 2210 | |
| 2211 | /* Append current string */ |
| 2212 | ret = bt_field_string_append_len(field, value, len); |
| 2213 | if (ret) { |
| 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); |
| 2217 | status = BT_BTR_STATUS_ERROR; |
| 2218 | goto end; |
| 2219 | } |
| 2220 | |
| 2221 | end: |
| 2222 | return status; |
| 2223 | } |
| 2224 | |
| 2225 | static |
| 2226 | enum bt_btr_status btr_string_end_cb( |
| 2227 | struct bt_field_type *type, void *data) |
| 2228 | { |
| 2229 | struct bt_notif_iter *notit = data; |
| 2230 | |
| 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, |
| 2235 | bt_field_type_id_string( |
| 2236 | bt_field_type_get_type_id(type))); |
| 2237 | |
| 2238 | /* Pop string field */ |
| 2239 | stack_pop(notit->stack); |
| 2240 | |
| 2241 | /* Go to next field */ |
| 2242 | stack_top(notit->stack)->index++; |
| 2243 | return BT_BTR_STATUS_OK; |
| 2244 | } |
| 2245 | |
| 2246 | enum bt_btr_status btr_compound_begin_cb( |
| 2247 | struct bt_field_type *type, void *data) |
| 2248 | { |
| 2249 | enum bt_btr_status status = BT_BTR_STATUS_OK; |
| 2250 | struct bt_notif_iter *notit = data; |
| 2251 | struct bt_field *field; |
| 2252 | int ret; |
| 2253 | |
| 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, |
| 2258 | bt_field_type_id_string( |
| 2259 | bt_field_type_get_type_id(type))); |
| 2260 | |
| 2261 | /* Create field */ |
| 2262 | if (stack_empty(notit->stack)) { |
| 2263 | /* Root: create dynamic scope field */ |
| 2264 | *notit->cur_dscope_field = bt_field_create(type); |
| 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); |
| 2274 | |
| 2275 | if (!field) { |
| 2276 | BT_LOGE("Cannot create compound field: " |
| 2277 | "notit-addr=%p, ft-addr=%p, ft-id=%s", |
| 2278 | notit, type, |
| 2279 | bt_field_type_id_string( |
| 2280 | bt_field_type_get_type_id(type))); |
| 2281 | status = BT_BTR_STATUS_ERROR; |
| 2282 | goto end; |
| 2283 | } |
| 2284 | } else { |
| 2285 | field = get_next_field(notit); |
| 2286 | if (!field) { |
| 2287 | BT_LOGW("Cannot get next field: notit-addr=%p", notit); |
| 2288 | status = BT_BTR_STATUS_ERROR; |
| 2289 | goto end; |
| 2290 | } |
| 2291 | } |
| 2292 | |
| 2293 | /* Push field */ |
| 2294 | assert(field); |
| 2295 | ret = stack_push(notit->stack, field); |
| 2296 | if (ret) { |
| 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, |
| 2300 | bt_field_type_id_string( |
| 2301 | bt_field_type_get_type_id(type)), |
| 2302 | ret); |
| 2303 | status = BT_BTR_STATUS_ERROR; |
| 2304 | goto end; |
| 2305 | } |
| 2306 | |
| 2307 | end: |
| 2308 | BT_PUT(field); |
| 2309 | |
| 2310 | return status; |
| 2311 | } |
| 2312 | |
| 2313 | enum bt_btr_status btr_compound_end_cb( |
| 2314 | struct bt_field_type *type, void *data) |
| 2315 | { |
| 2316 | struct bt_notif_iter *notit = data; |
| 2317 | |
| 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, |
| 2322 | bt_field_type_id_string( |
| 2323 | bt_field_type_get_type_id(type))); |
| 2324 | assert(!stack_empty(notit->stack)); |
| 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 | |
| 2334 | return BT_BTR_STATUS_OK; |
| 2335 | } |
| 2336 | |
| 2337 | static |
| 2338 | struct bt_field *resolve_field(struct bt_notif_iter *notit, |
| 2339 | struct bt_field_path *path) |
| 2340 | { |
| 2341 | struct bt_field *field = NULL; |
| 2342 | unsigned int i; |
| 2343 | |
| 2344 | if (BT_LOG_ON_VERBOSE) { |
| 2345 | GString *gstr = bt_field_path_string(path); |
| 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 | |
| 2355 | switch (bt_field_path_get_root_scope(path)) { |
| 2356 | case BT_SCOPE_TRACE_PACKET_HEADER: |
| 2357 | field = notit->dscopes.trace_packet_header; |
| 2358 | break; |
| 2359 | case BT_SCOPE_STREAM_PACKET_CONTEXT: |
| 2360 | field = notit->dscopes.stream_packet_context; |
| 2361 | break; |
| 2362 | case BT_SCOPE_STREAM_EVENT_HEADER: |
| 2363 | field = notit->dscopes.stream_event_header; |
| 2364 | break; |
| 2365 | case BT_SCOPE_STREAM_EVENT_CONTEXT: |
| 2366 | field = notit->dscopes.stream_event_context; |
| 2367 | break; |
| 2368 | case BT_SCOPE_EVENT_CONTEXT: |
| 2369 | field = notit->dscopes.event_context; |
| 2370 | break; |
| 2371 | case BT_SCOPE_EVENT_FIELDS: |
| 2372 | field = notit->dscopes.event_payload; |
| 2373 | break; |
| 2374 | default: |
| 2375 | BT_LOGF("Cannot resolve field path: unknown scope: " |
| 2376 | "notit-addr=%p, root-scope=%s", |
| 2377 | notit, bt_scope_string( |
| 2378 | bt_field_path_get_root_scope(path))); |
| 2379 | abort(); |
| 2380 | } |
| 2381 | |
| 2382 | if (!field) { |
| 2383 | BT_LOGW("Cannot resolve field path: root field not found: " |
| 2384 | "notit-addr=%p, root-scope=%s", |
| 2385 | notit, bt_scope_string( |
| 2386 | bt_field_path_get_root_scope(path))); |
| 2387 | goto end; |
| 2388 | } |
| 2389 | |
| 2390 | bt_get(field); |
| 2391 | |
| 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); |
| 2396 | |
| 2397 | field_type = bt_field_get_type(field); |
| 2398 | assert(field_type); |
| 2399 | |
| 2400 | if (is_struct_type(field_type)) { |
| 2401 | next_field = bt_field_structure_get_field_by_index( |
| 2402 | field, index); |
| 2403 | } else if (is_variant_type(field_type)) { |
| 2404 | next_field = |
| 2405 | bt_field_variant_get_current_field(field); |
| 2406 | } |
| 2407 | |
| 2408 | BT_PUT(field); |
| 2409 | BT_PUT(field_type); |
| 2410 | |
| 2411 | if (!next_field) { |
| 2412 | BT_LOGW("Cannot find next field: " |
| 2413 | "notit-addr=%p, ft-addr=%p, ft-id=%s, index=%d", |
| 2414 | notit, field_type, |
| 2415 | bt_field_type_id_string( |
| 2416 | bt_field_type_get_type_id(field_type)), |
| 2417 | index); |
| 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 |
| 2430 | int64_t btr_get_sequence_length_cb(struct bt_field_type *type, void *data) |
| 2431 | { |
| 2432 | int64_t ret = -1; |
| 2433 | int iret; |
| 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; |
| 2438 | uint64_t length; |
| 2439 | |
| 2440 | field_path = bt_field_type_sequence_get_length_field_path(type); |
| 2441 | assert(field_path); |
| 2442 | length_field = resolve_field(notit, field_path); |
| 2443 | if (!length_field) { |
| 2444 | BT_LOGW("Cannot resolve sequence field type's length field path: " |
| 2445 | "notit-addr=%p, ft-addr=%p", |
| 2446 | notit, type); |
| 2447 | goto end; |
| 2448 | } |
| 2449 | |
| 2450 | iret = bt_field_unsigned_integer_get_value(length_field, &length); |
| 2451 | if (iret) { |
| 2452 | BT_LOGE("Cannot get value of sequence length field: " |
| 2453 | "notit-addr=%p, field-addr=%p", |
| 2454 | notit, length_field); |
| 2455 | goto end; |
| 2456 | } |
| 2457 | |
| 2458 | seq_field = stack_top(notit->stack)->base; |
| 2459 | iret = bt_field_sequence_set_length(seq_field, length_field); |
| 2460 | if (iret) { |
| 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); |
| 2465 | goto end; |
| 2466 | } |
| 2467 | |
| 2468 | ret = (int64_t) length; |
| 2469 | |
| 2470 | end: |
| 2471 | BT_PUT(length_field); |
| 2472 | BT_PUT(field_path); |
| 2473 | |
| 2474 | return ret; |
| 2475 | } |
| 2476 | |
| 2477 | static |
| 2478 | struct bt_field_type *btr_get_variant_type_cb( |
| 2479 | struct bt_field_type *type, void *data) |
| 2480 | { |
| 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; |
| 2487 | |
| 2488 | path = bt_field_type_variant_get_tag_field_path(type); |
| 2489 | assert(path); |
| 2490 | tag_field = resolve_field(notit, path); |
| 2491 | if (!tag_field) { |
| 2492 | BT_LOGW("Cannot resolve variant field type's tag field path: " |
| 2493 | "notit-addr=%p, ft-addr=%p", |
| 2494 | notit, type); |
| 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 | */ |
| 2510 | var_field = stack_top(notit->stack)->base; |
| 2511 | selected_field = bt_field_variant_get_field(var_field, tag_field); |
| 2512 | if (!selected_field) { |
| 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); |
| 2516 | goto end; |
| 2517 | } |
| 2518 | |
| 2519 | selected_field_type = bt_field_get_type(selected_field); |
| 2520 | |
| 2521 | end: |
| 2522 | BT_PUT(tag_field); |
| 2523 | BT_PUT(selected_field); |
| 2524 | BT_PUT(path); |
| 2525 | |
| 2526 | return selected_field_type; |
| 2527 | } |
| 2528 | |
| 2529 | static |
| 2530 | int set_event_clocks(struct bt_event *event, |
| 2531 | struct bt_notif_iter *notit) |
| 2532 | { |
| 2533 | int ret; |
| 2534 | GHashTableIter iter; |
| 2535 | struct bt_clock_class *clock_class; |
| 2536 | uint64_t *clock_state; |
| 2537 | |
| 2538 | g_hash_table_iter_init(&iter, notit->clock_states); |
| 2539 | |
| 2540 | while (g_hash_table_iter_next(&iter, (gpointer) &clock_class, |
| 2541 | (gpointer) &clock_state)) { |
| 2542 | struct bt_clock_value *clock_value; |
| 2543 | |
| 2544 | clock_value = bt_clock_value_create(clock_class, |
| 2545 | *clock_state); |
| 2546 | if (!clock_value) { |
| 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, |
| 2551 | bt_clock_class_get_name(clock_class)); |
| 2552 | ret = -1; |
| 2553 | goto end; |
| 2554 | } |
| 2555 | ret = bt_event_set_clock_value(event, clock_value); |
| 2556 | bt_put(clock_value); |
| 2557 | if (ret) { |
| 2558 | struct bt_event_class *event_class = |
| 2559 | bt_event_get_class(event); |
| 2560 | |
| 2561 | assert(event_class); |
| 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, |
| 2570 | bt_event_class_get_name(event_class), |
| 2571 | bt_event_class_get_id(event_class), |
| 2572 | clock_class, |
| 2573 | bt_clock_class_get_name(clock_class), |
| 2574 | clock_value); |
| 2575 | bt_put(event_class); |
| 2576 | goto end; |
| 2577 | } |
| 2578 | } |
| 2579 | |
| 2580 | ret = 0; |
| 2581 | end: |
| 2582 | return ret; |
| 2583 | } |
| 2584 | |
| 2585 | static |
| 2586 | struct bt_event *create_event(struct bt_notif_iter *notit) |
| 2587 | { |
| 2588 | int ret; |
| 2589 | struct bt_event *event; |
| 2590 | |
| 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, |
| 2596 | bt_event_class_get_name(notit->meta.event_class), |
| 2597 | bt_event_class_get_id(notit->meta.event_class)); |
| 2598 | |
| 2599 | /* Create event object. */ |
| 2600 | event = bt_event_create(notit->meta.event_class); |
| 2601 | if (!event) { |
| 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, |
| 2607 | bt_event_class_get_name(notit->meta.event_class), |
| 2608 | bt_event_class_get_id(notit->meta.event_class)); |
| 2609 | goto error; |
| 2610 | } |
| 2611 | |
| 2612 | /* Set header, stream event context, context, and payload fields. */ |
| 2613 | ret = bt_event_set_header(event, |
| 2614 | notit->dscopes.stream_event_header); |
| 2615 | if (ret) { |
| 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, |
| 2621 | bt_event_class_get_name(notit->meta.event_class), |
| 2622 | bt_event_class_get_id(notit->meta.event_class), |
| 2623 | notit->dscopes.stream_event_header); |
| 2624 | goto error; |
| 2625 | } |
| 2626 | |
| 2627 | ret = bt_event_set_stream_event_context(event, |
| 2628 | notit->dscopes.stream_event_context); |
| 2629 | if (ret) { |
| 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, |
| 2635 | bt_event_class_get_name(notit->meta.event_class), |
| 2636 | bt_event_class_get_id(notit->meta.event_class), |
| 2637 | notit->dscopes.stream_event_context); |
| 2638 | goto error; |
| 2639 | } |
| 2640 | |
| 2641 | ret = bt_event_set_event_context(event, |
| 2642 | notit->dscopes.event_context); |
| 2643 | if (ret) { |
| 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, |
| 2649 | bt_event_class_get_name(notit->meta.event_class), |
| 2650 | bt_event_class_get_id(notit->meta.event_class), |
| 2651 | notit->dscopes.event_context); |
| 2652 | goto error; |
| 2653 | } |
| 2654 | |
| 2655 | ret = bt_event_set_event_payload(event, |
| 2656 | notit->dscopes.event_payload); |
| 2657 | if (ret) { |
| 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, |
| 2663 | bt_event_class_get_name(notit->meta.event_class), |
| 2664 | bt_event_class_get_id(notit->meta.event_class), |
| 2665 | notit->dscopes.event_payload); |
| 2666 | goto error; |
| 2667 | } |
| 2668 | |
| 2669 | ret = set_event_clocks(event, notit); |
| 2670 | if (ret) { |
| 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, |
| 2676 | bt_event_class_get_name(notit->meta.event_class), |
| 2677 | bt_event_class_get_id(notit->meta.event_class)); |
| 2678 | goto error; |
| 2679 | } |
| 2680 | |
| 2681 | /* Associate with current packet. */ |
| 2682 | assert(notit->packet); |
| 2683 | ret = bt_event_set_packet(event, notit->packet); |
| 2684 | if (ret) { |
| 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, |
| 2690 | bt_event_class_get_name(notit->meta.event_class), |
| 2691 | bt_event_class_get_id(notit->meta.event_class), |
| 2692 | notit->packet); |
| 2693 | goto error; |
| 2694 | } |
| 2695 | |
| 2696 | goto end; |
| 2697 | |
| 2698 | error: |
| 2699 | BT_PUT(event); |
| 2700 | |
| 2701 | end: |
| 2702 | return event; |
| 2703 | } |
| 2704 | |
| 2705 | static |
| 2706 | uint64_t get_cur_stream_instance_id(struct bt_notif_iter *notit) |
| 2707 | { |
| 2708 | struct bt_field *stream_instance_id_field = NULL; |
| 2709 | uint64_t stream_instance_id = -1ULL; |
| 2710 | int ret; |
| 2711 | |
| 2712 | if (!notit->dscopes.trace_packet_header) { |
| 2713 | goto end; |
| 2714 | } |
| 2715 | |
| 2716 | stream_instance_id_field = bt_field_structure_get_field_by_name( |
| 2717 | notit->dscopes.trace_packet_header, "stream_instance_id"); |
| 2718 | if (!stream_instance_id_field) { |
| 2719 | goto end; |
| 2720 | } |
| 2721 | |
| 2722 | ret = bt_field_unsigned_integer_get_value(stream_instance_id_field, |
| 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 | |
| 2734 | static |
| 2735 | int set_stream(struct bt_notif_iter *notit) |
| 2736 | { |
| 2737 | int ret = 0; |
| 2738 | struct bt_stream *stream = NULL; |
| 2739 | |
| 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, |
| 2744 | bt_stream_class_get_name(notit->meta.stream_class), |
| 2745 | bt_stream_class_get_id(notit->meta.stream_class)); |
| 2746 | stream = bt_get(notit->medium.medops.get_stream( |
| 2747 | notit->meta.stream_class, get_cur_stream_instance_id(notit), |
| 2748 | notit->medium.data)); |
| 2749 | BT_LOGV("User function returned: stream-addr=%p", stream); |
| 2750 | if (!stream) { |
| 2751 | BT_LOGW_STR("User function failed to return a stream object for the given stream class."); |
| 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 |
| 2770 | void create_packet(struct bt_notif_iter *notit) |
| 2771 | { |
| 2772 | int ret; |
| 2773 | struct bt_packet *packet = NULL; |
| 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) { |
| 2781 | goto error; |
| 2782 | } |
| 2783 | |
| 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, |
| 2789 | notit, notit->stream, notit->meta.stream_class, |
| 2790 | bt_stream_class_get_name(notit->meta.stream_class), |
| 2791 | bt_stream_class_get_id(notit->meta.stream_class)); |
| 2792 | |
| 2793 | /* Create packet */ |
| 2794 | packet = bt_packet_create(notit->stream); |
| 2795 | if (!packet) { |
| 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, |
| 2801 | notit, notit->stream, notit->meta.stream_class, |
| 2802 | bt_stream_class_get_name(notit->meta.stream_class), |
| 2803 | bt_stream_class_get_id(notit->meta.stream_class)); |
| 2804 | goto error; |
| 2805 | } |
| 2806 | |
| 2807 | /* Set packet's context and header fields */ |
| 2808 | if (notit->dscopes.trace_packet_header) { |
| 2809 | ret = bt_packet_set_header(packet, |
| 2810 | notit->dscopes.trace_packet_header); |
| 2811 | if (ret) { |
| 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", |
| 2819 | notit, packet, notit->stream, notit->meta.stream_class, |
| 2820 | bt_stream_class_get_name(notit->meta.stream_class), |
| 2821 | bt_stream_class_get_id(notit->meta.stream_class), |
| 2822 | notit->dscopes.trace_packet_header); |
| 2823 | goto error; |
| 2824 | } |
| 2825 | } |
| 2826 | |
| 2827 | if (notit->dscopes.stream_packet_context) { |
| 2828 | ret = bt_packet_set_context(packet, |
| 2829 | notit->dscopes.stream_packet_context); |
| 2830 | if (ret) { |
| 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", |
| 2838 | notit, packet, notit->stream, notit->meta.stream_class, |
| 2839 | bt_stream_class_get_name(notit->meta.stream_class), |
| 2840 | bt_stream_class_get_id(notit->meta.stream_class), |
| 2841 | notit->dscopes.trace_packet_header); |
| 2842 | goto error; |
| 2843 | } |
| 2844 | } |
| 2845 | |
| 2846 | goto end; |
| 2847 | |
| 2848 | error: |
| 2849 | BT_PUT(packet); |
| 2850 | |
| 2851 | end: |
| 2852 | BT_MOVE(notit->packet, packet); |
| 2853 | } |
| 2854 | |
| 2855 | static |
| 2856 | void notify_new_packet(struct bt_notif_iter *notit, |
| 2857 | struct bt_notification **notification) |
| 2858 | { |
| 2859 | struct bt_notification *ret; |
| 2860 | |
| 2861 | /* Initialize the iterator's current packet */ |
| 2862 | create_packet(notit); |
| 2863 | if (!notit->packet) { |
| 2864 | BT_LOGE("Cannot create packet for packet notification: " |
| 2865 | "notit-addr=%p", notit); |
| 2866 | return; |
| 2867 | } |
| 2868 | |
| 2869 | ret = bt_notification_packet_begin_create(notit->packet); |
| 2870 | if (!ret) { |
| 2871 | BT_LOGE("Cannot create packet beginning notification: " |
| 2872 | "notit-addr=%p, packet-addr=%p", |
| 2873 | notit, notit->packet); |
| 2874 | return; |
| 2875 | } |
| 2876 | *notification = ret; |
| 2877 | } |
| 2878 | |
| 2879 | static |
| 2880 | void notify_end_of_packet(struct bt_notif_iter *notit, |
| 2881 | struct bt_notification **notification) |
| 2882 | { |
| 2883 | struct bt_notification *ret; |
| 2884 | |
| 2885 | if (!notit->packet) { |
| 2886 | return; |
| 2887 | } |
| 2888 | |
| 2889 | ret = bt_notification_packet_end_create(notit->packet); |
| 2890 | if (!ret) { |
| 2891 | BT_LOGE("Cannot create packet end notification: " |
| 2892 | "notit-addr=%p, packet-addr=%p", |
| 2893 | notit, notit->packet); |
| 2894 | return; |
| 2895 | } |
| 2896 | BT_PUT(notit->packet); |
| 2897 | *notification = ret; |
| 2898 | } |
| 2899 | |
| 2900 | static |
| 2901 | void notify_event(struct bt_notif_iter *notit, |
| 2902 | struct bt_clock_class_priority_map *cc_prio_map, |
| 2903 | struct bt_notification **notification) |
| 2904 | { |
| 2905 | struct bt_event *event = NULL; |
| 2906 | struct bt_notification *ret = NULL; |
| 2907 | |
| 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 | |
| 2916 | /* Create event */ |
| 2917 | event = create_event(notit); |
| 2918 | if (!event) { |
| 2919 | BT_LOGE("Cannot create event for event notification: " |
| 2920 | "notit-addr=%p", notit); |
| 2921 | goto end; |
| 2922 | } |
| 2923 | |
| 2924 | ret = bt_notification_event_create(event, cc_prio_map); |
| 2925 | if (!ret) { |
| 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); |
| 2930 | goto end; |
| 2931 | } |
| 2932 | *notification = ret; |
| 2933 | end: |
| 2934 | BT_PUT(event); |
| 2935 | } |
| 2936 | |
| 2937 | static |
| 2938 | void init_trace_field_path_cache(struct bt_trace *trace, |
| 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; |
| 2944 | struct bt_field_type *packet_header = NULL; |
| 2945 | |
| 2946 | packet_header = bt_trace_get_packet_header_type(trace); |
| 2947 | if (!packet_header) { |
| 2948 | goto end; |
| 2949 | } |
| 2950 | |
| 2951 | if (!bt_field_type_is_structure(packet_header)) { |
| 2952 | goto end; |
| 2953 | } |
| 2954 | |
| 2955 | count = bt_field_type_structure_get_field_count(packet_header); |
| 2956 | assert(count >= 0); |
| 2957 | |
| 2958 | for (i = 0; (i < count && (stream_id == -1 || stream_instance_id == -1)); i++) { |
| 2959 | int ret; |
| 2960 | const char *field_name; |
| 2961 | |
| 2962 | ret = bt_field_type_structure_get_field_by_index(packet_header, |
| 2963 | &field_name, NULL, i); |
| 2964 | if (ret) { |
| 2965 | BT_LOGE("Cannot get structure field's field: " |
| 2966 | "field-addr=%p, index=%d", |
| 2967 | packet_header, i); |
| 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 | } |
| 2978 | |
| 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 | |
| 2985 | BT_HIDDEN |
| 2986 | struct bt_notif_iter *bt_notif_iter_create(struct bt_trace *trace, |
| 2987 | size_t max_request_sz, |
| 2988 | struct bt_notif_iter_medium_ops medops, void *data) |
| 2989 | { |
| 2990 | struct bt_notif_iter *notit = NULL; |
| 2991 | struct bt_btr_cbs cbs = { |
| 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 | |
| 3008 | assert(trace); |
| 3009 | assert(medops.request_bytes); |
| 3010 | assert(medops.get_stream); |
| 3011 | BT_LOGD("Creating CTF plugin notification iterator: " |
| 3012 | "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, " |
| 3013 | "data=%p", |
| 3014 | trace, bt_trace_get_name(trace), max_request_sz, data); |
| 3015 | notit = g_new0(struct bt_notif_iter, 1); |
| 3016 | if (!notit) { |
| 3017 | BT_LOGE_STR("Failed to allocate one CTF plugin notification iterator."); |
| 3018 | goto end; |
| 3019 | } |
| 3020 | notit->clock_states = g_hash_table_new_full(g_direct_hash, |
| 3021 | g_direct_equal, bt_put, g_free); |
| 3022 | if (!notit->clock_states) { |
| 3023 | BT_LOGE_STR("Failed to allocate a GHashTable."); |
| 3024 | goto error; |
| 3025 | } |
| 3026 | notit->meta.trace = bt_get(trace); |
| 3027 | notit->medium.medops = medops; |
| 3028 | notit->medium.max_request_sz = max_request_sz; |
| 3029 | notit->medium.data = data; |
| 3030 | notit->stack = stack_new(notit); |
| 3031 | if (!notit->stack) { |
| 3032 | BT_LOGE_STR("Failed to create field stack."); |
| 3033 | goto error; |
| 3034 | } |
| 3035 | |
| 3036 | notit->btr = bt_btr_create(cbs, notit); |
| 3037 | if (!notit->btr) { |
| 3038 | BT_LOGE_STR("Failed to create binary type reader (BTR)."); |
| 3039 | goto error; |
| 3040 | } |
| 3041 | |
| 3042 | bt_notif_iter_reset(notit); |
| 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, |
| 3045 | g_direct_equal, bt_put, g_free); |
| 3046 | if (!notit->sc_field_path_caches) { |
| 3047 | BT_LOGE_STR("Failed to allocate a GHashTable."); |
| 3048 | goto error; |
| 3049 | } |
| 3050 | |
| 3051 | notit->field_overrides = g_hash_table_new_full(g_direct_hash, |
| 3052 | g_direct_equal, bt_put, g_free); |
| 3053 | if (!notit->field_overrides) { |
| 3054 | BT_LOGE_STR("Failed to allocate a GHashTable."); |
| 3055 | goto error; |
| 3056 | } |
| 3057 | |
| 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", |
| 3061 | trace, bt_trace_get_name(trace), max_request_sz, data, |
| 3062 | notit); |
| 3063 | notit->cur_packet_offset = 0; |
| 3064 | |
| 3065 | end: |
| 3066 | return notit; |
| 3067 | |
| 3068 | error: |
| 3069 | bt_notif_iter_destroy(notit); |
| 3070 | notit = NULL; |
| 3071 | goto end; |
| 3072 | } |
| 3073 | |
| 3074 | void bt_notif_iter_destroy(struct bt_notif_iter *notit) |
| 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); |
| 3080 | BT_PUT(notit->stream); |
| 3081 | BT_PUT(notit->cur_timestamp_end); |
| 3082 | put_all_dscopes(notit); |
| 3083 | |
| 3084 | BT_LOGD("Destroying CTF plugin notification iterator: addr=%p", notit); |
| 3085 | |
| 3086 | if (notit->stack) { |
| 3087 | BT_LOGD_STR("Destroying field stack."); |
| 3088 | stack_destroy(notit->stack); |
| 3089 | } |
| 3090 | |
| 3091 | if (notit->btr) { |
| 3092 | BT_LOGD("Destroying BTR: btr-addr=%p", notit->btr); |
| 3093 | bt_btr_destroy(notit->btr); |
| 3094 | } |
| 3095 | |
| 3096 | if (notit->clock_states) { |
| 3097 | g_hash_table_destroy(notit->clock_states); |
| 3098 | } |
| 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 | } |
| 3107 | |
| 3108 | g_free(notit); |
| 3109 | } |
| 3110 | |
| 3111 | enum bt_notif_iter_status bt_notif_iter_get_next_notification( |
| 3112 | struct bt_notif_iter *notit, |
| 3113 | struct bt_clock_class_priority_map *cc_prio_map, |
| 3114 | struct bt_notification **notification) |
| 3115 | { |
| 3116 | enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; |
| 3117 | |
| 3118 | assert(notit); |
| 3119 | assert(notification); |
| 3120 | |
| 3121 | BT_LOGV("Getting next notification: notit-addr=%p, cc-prio-map-addr=%p", |
| 3122 | notit, cc_prio_map); |
| 3123 | |
| 3124 | while (true) { |
| 3125 | status = handle_state(notit); |
| 3126 | if (status == BT_NOTIF_ITER_STATUS_AGAIN) { |
| 3127 | BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_AGAIN."); |
| 3128 | goto end; |
| 3129 | } |
| 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."); |
| 3133 | } else { |
| 3134 | BT_LOGW("Cannot handle state: " |
| 3135 | "notit-addr=%p, state=%s", |
| 3136 | notit, state_string(notit->state)); |
| 3137 | } |
| 3138 | goto end; |
| 3139 | } |
| 3140 | |
| 3141 | switch (notit->state) { |
| 3142 | case STATE_EMIT_NOTIF_NEW_PACKET: |
| 3143 | /* notify_new_packet() logs errors */ |
| 3144 | notify_new_packet(notit, notification); |
| 3145 | if (!*notification) { |
| 3146 | status = BT_NOTIF_ITER_STATUS_ERROR; |
| 3147 | } |
| 3148 | goto end; |
| 3149 | case STATE_EMIT_NOTIF_EVENT: |
| 3150 | /* notify_event() logs errors */ |
| 3151 | notify_event(notit, cc_prio_map, notification); |
| 3152 | if (!*notification) { |
| 3153 | status = BT_NOTIF_ITER_STATUS_ERROR; |
| 3154 | } |
| 3155 | goto end; |
| 3156 | case STATE_EMIT_NOTIF_END_OF_PACKET: |
| 3157 | /* Update clock with timestamp_end field. */ |
| 3158 | if (notit->cur_timestamp_end) { |
| 3159 | enum bt_btr_status btr_status; |
| 3160 | struct bt_field_type *field_type = |
| 3161 | bt_field_get_type( |
| 3162 | notit->cur_timestamp_end); |
| 3163 | |
| 3164 | assert(field_type); |
| 3165 | btr_status = update_clock(notit, |
| 3166 | notit->cur_timestamp_end); |
| 3167 | BT_PUT(field_type); |
| 3168 | if (btr_status != BT_BTR_STATUS_OK) { |
| 3169 | BT_LOGW("Cannot update stream's clock value: " |
| 3170 | "notit-addr=%p", notit); |
| 3171 | status = BT_NOTIF_ITER_STATUS_ERROR; |
| 3172 | goto end; |
| 3173 | } |
| 3174 | } |
| 3175 | |
| 3176 | /* notify_end_of_packet() logs errors */ |
| 3177 | notify_end_of_packet(notit, notification); |
| 3178 | if (!*notification) { |
| 3179 | status = BT_NOTIF_ITER_STATUS_ERROR; |
| 3180 | } |
| 3181 | goto end; |
| 3182 | default: |
| 3183 | /* Non-emitting state: continue */ |
| 3184 | break; |
| 3185 | } |
| 3186 | } |
| 3187 | |
| 3188 | end: |
| 3189 | return status; |
| 3190 | } |
| 3191 | |
| 3192 | BT_HIDDEN |
| 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) |
| 3197 | { |
| 3198 | int ret; |
| 3199 | enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; |
| 3200 | |
| 3201 | assert(notit); |
| 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); |
| 3210 | if (status == BT_NOTIF_ITER_STATUS_AGAIN) { |
| 3211 | BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_AGAIN."); |
| 3212 | goto end; |
| 3213 | } |
| 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."); |
| 3217 | } else { |
| 3218 | BT_LOGW("Cannot handle state: " |
| 3219 | "notit-addr=%p, state=%s", |
| 3220 | notit, state_string(notit->state)); |
| 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 | */ |
| 3246 | BT_LOGF("Unexpected state: notit-addr=%p, state=%s", |
| 3247 | notit, state_string(notit->state)); |
| 3248 | abort(); |
| 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 | |
| 3261 | ret = set_current_packet_content_sizes(notit); |
| 3262 | if (ret) { |
| 3263 | status = BT_NOTIF_ITER_STATUS_ERROR; |
| 3264 | goto end; |
| 3265 | } |
| 3266 | end: |
| 3267 | return status; |
| 3268 | } |
| 3269 | |
| 3270 | BT_HIDDEN |
| 3271 | void bt_notif_iter_set_medops_data(struct bt_notif_iter *notit, |
| 3272 | void *medops_data) |
| 3273 | { |
| 3274 | assert(notit); |
| 3275 | notit->medium.data = medops_data; |
| 3276 | } |
| 3277 | |
| 3278 | BT_HIDDEN |
| 3279 | enum bt_notif_iter_status bt_notif_iter_seek( |
| 3280 | struct bt_notif_iter *notit, off_t offset) |
| 3281 | { |
| 3282 | enum bt_notif_iter_status ret = BT_NOTIF_ITER_STATUS_OK; |
| 3283 | enum bt_notif_iter_medium_status medium_status; |
| 3284 | |
| 3285 | assert(notit); |
| 3286 | if (offset < 0) { |
| 3287 | BT_LOGE("Cannot seek to negative offset: offset=%jd", offset); |
| 3288 | ret = BT_NOTIF_ITER_STATUS_INVAL; |
| 3289 | goto end; |
| 3290 | } |
| 3291 | |
| 3292 | if (!notit->medium.medops.seek) { |
| 3293 | ret = BT_NOTIF_ITER_STATUS_UNSUPPORTED; |
| 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( |
| 3299 | BT_NOTIF_ITER_SEEK_WHENCE_SET, offset, |
| 3300 | notit->medium.data); |
| 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; |
| 3304 | } else { |
| 3305 | ret = BT_NOTIF_ITER_STATUS_ERROR; |
| 3306 | goto end; |
| 3307 | } |
| 3308 | } |
| 3309 | |
| 3310 | bt_notif_iter_reset(notit); |
| 3311 | notit->cur_packet_offset = offset; |
| 3312 | end: |
| 3313 | return ret; |
| 3314 | } |
| 3315 | |
| 3316 | BT_HIDDEN |
| 3317 | off_t bt_notif_iter_get_current_packet_offset( |
| 3318 | struct bt_notif_iter *notit) |
| 3319 | { |
| 3320 | assert(notit); |
| 3321 | return notit->cur_packet_offset; |
| 3322 | } |
| 3323 | |
| 3324 | BT_HIDDEN |
| 3325 | off_t bt_notif_iter_get_current_packet_size( |
| 3326 | struct bt_notif_iter *notit) |
| 3327 | { |
| 3328 | assert(notit); |
| 3329 | return notit->cur_packet_size; |
| 3330 | } |