| 1 | /* |
| 2 | * Babeltrace - CTF message iterator |
| 3 | * |
| 4 | * Copyright (c) 2015-2018 EfficiOS Inc. and Linux Foundation |
| 5 | * Copyright (c) 2015-2018 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-MSG-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 <babeltrace/assert-internal.h> |
| 35 | #include <string.h> |
| 36 | #include <babeltrace/babeltrace.h> |
| 37 | #include <babeltrace/common-internal.h> |
| 38 | #include <glib.h> |
| 39 | #include <stdlib.h> |
| 40 | |
| 41 | #include "msg-iter.h" |
| 42 | #include "../bfcr/bfcr.h" |
| 43 | |
| 44 | struct bt_msg_iter; |
| 45 | |
| 46 | /* A visit stack entry */ |
| 47 | struct stack_entry { |
| 48 | /* |
| 49 | * Current base field, one of: |
| 50 | * |
| 51 | * * string |
| 52 | * * structure |
| 53 | * * array |
| 54 | * * sequence |
| 55 | * * variant |
| 56 | * |
| 57 | * Field is borrowed. |
| 58 | */ |
| 59 | bt_field *base; |
| 60 | |
| 61 | /* Index of next field to set */ |
| 62 | size_t index; |
| 63 | }; |
| 64 | |
| 65 | /* Visit stack */ |
| 66 | struct stack { |
| 67 | /* Entries (struct stack_entry) */ |
| 68 | GArray *entries; |
| 69 | |
| 70 | /* Number of active entries */ |
| 71 | size_t size; |
| 72 | }; |
| 73 | |
| 74 | /* State */ |
| 75 | enum state { |
| 76 | STATE_INIT, |
| 77 | STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN, |
| 78 | STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE, |
| 79 | STATE_AFTER_TRACE_PACKET_HEADER, |
| 80 | STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN, |
| 81 | STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE, |
| 82 | STATE_AFTER_STREAM_PACKET_CONTEXT, |
| 83 | STATE_EMIT_MSG_NEW_STREAM, |
| 84 | STATE_EMIT_MSG_NEW_PACKET, |
| 85 | STATE_DSCOPE_EVENT_HEADER_BEGIN, |
| 86 | STATE_DSCOPE_EVENT_HEADER_CONTINUE, |
| 87 | STATE_AFTER_EVENT_HEADER, |
| 88 | STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN, |
| 89 | STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE, |
| 90 | STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN, |
| 91 | STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE, |
| 92 | STATE_DSCOPE_EVENT_PAYLOAD_BEGIN, |
| 93 | STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE, |
| 94 | STATE_EMIT_MSG_EVENT, |
| 95 | STATE_EMIT_MSG_END_OF_PACKET, |
| 96 | STATE_DONE, |
| 97 | STATE_SKIP_PACKET_PADDING, |
| 98 | }; |
| 99 | |
| 100 | /* CTF message iterator */ |
| 101 | struct bt_msg_iter { |
| 102 | /* Visit stack */ |
| 103 | struct stack *stack; |
| 104 | |
| 105 | /* Current message iterator to create messages (weak) */ |
| 106 | bt_self_message_iterator *msg_iter; |
| 107 | |
| 108 | /* |
| 109 | * Current dynamic scope field pointer. |
| 110 | * |
| 111 | * This is set by read_dscope_begin_state() and contains the |
| 112 | * value of one of the pointers in `dscopes` below. |
| 113 | */ |
| 114 | bt_field *cur_dscope_field; |
| 115 | |
| 116 | /* |
| 117 | * True if we're done filling a string field from a text |
| 118 | * array/sequence payload. |
| 119 | */ |
| 120 | bool done_filling_string; |
| 121 | |
| 122 | /* Trace and classes */ |
| 123 | /* True to set IR fields */ |
| 124 | bool set_ir_fields; |
| 125 | |
| 126 | struct { |
| 127 | struct ctf_trace_class *tc; |
| 128 | struct ctf_stream_class *sc; |
| 129 | struct ctf_event_class *ec; |
| 130 | } meta; |
| 131 | |
| 132 | /* Current packet header field wrapper (NULL if not created yet) */ |
| 133 | bt_packet_header_field *packet_header_field; |
| 134 | |
| 135 | /* Current packet header field wrapper (NULL if not created yet) */ |
| 136 | bt_packet_context_field *packet_context_field; |
| 137 | |
| 138 | /* Current event header field (NULL if not created yet) */ |
| 139 | bt_event_header_field *event_header_field; |
| 140 | |
| 141 | /* Current packet (NULL if not created yet) */ |
| 142 | bt_packet *packet; |
| 143 | |
| 144 | /* Current stream (NULL if not set yet) */ |
| 145 | bt_stream *stream; |
| 146 | |
| 147 | /* Current event (NULL if not created yet) */ |
| 148 | bt_event *event; |
| 149 | |
| 150 | /* Current event message (NULL if not created yet) */ |
| 151 | bt_message *event_msg; |
| 152 | |
| 153 | /* Database of current dynamic scopes */ |
| 154 | struct { |
| 155 | bt_field *trace_packet_header; |
| 156 | bt_field *stream_packet_context; |
| 157 | bt_field *event_header; |
| 158 | bt_field *event_common_context; |
| 159 | bt_field *event_spec_context; |
| 160 | bt_field *event_payload; |
| 161 | } dscopes; |
| 162 | |
| 163 | /* Current state */ |
| 164 | enum state state; |
| 165 | |
| 166 | /* Current medium buffer data */ |
| 167 | struct { |
| 168 | /* Last address provided by medium */ |
| 169 | const uint8_t *addr; |
| 170 | |
| 171 | /* Buffer size provided by medium (bytes) */ |
| 172 | size_t sz; |
| 173 | |
| 174 | /* Offset within whole packet of addr (bits) */ |
| 175 | size_t packet_offset; |
| 176 | |
| 177 | /* Current position from addr (bits) */ |
| 178 | size_t at; |
| 179 | |
| 180 | /* Position of the last event header from addr (bits) */ |
| 181 | size_t last_eh_at; |
| 182 | } buf; |
| 183 | |
| 184 | /* Binary type reader */ |
| 185 | struct bt_bfcr *bfcr; |
| 186 | |
| 187 | /* Current medium data */ |
| 188 | struct { |
| 189 | struct bt_msg_iter_medium_ops medops; |
| 190 | size_t max_request_sz; |
| 191 | void *data; |
| 192 | } medium; |
| 193 | |
| 194 | /* Stream beginning was emitted */ |
| 195 | bool stream_begin_emitted; |
| 196 | |
| 197 | /* Current packet size (bits) (-1 if unknown) */ |
| 198 | int64_t cur_exp_packet_total_size; |
| 199 | |
| 200 | /* Current content size (bits) (-1 if unknown) */ |
| 201 | int64_t cur_exp_packet_content_size; |
| 202 | |
| 203 | /* Current stream class ID */ |
| 204 | int64_t cur_stream_class_id; |
| 205 | |
| 206 | /* Current event class ID */ |
| 207 | int64_t cur_event_class_id; |
| 208 | |
| 209 | /* Current data stream ID */ |
| 210 | int64_t cur_data_stream_id; |
| 211 | |
| 212 | /* |
| 213 | * Offset, in the underlying media, of the current packet's |
| 214 | * start (-1 if unknown). |
| 215 | */ |
| 216 | off_t cur_packet_offset; |
| 217 | |
| 218 | /* Default clock's current value */ |
| 219 | uint64_t default_clock_snapshot; |
| 220 | |
| 221 | /* End of packet snapshots */ |
| 222 | struct { |
| 223 | uint64_t discarded_events; |
| 224 | uint64_t packets; |
| 225 | uint64_t beginning_clock; |
| 226 | uint64_t end_clock; |
| 227 | } snapshots; |
| 228 | |
| 229 | /* Stored values (for sequence lengths, variant tags) */ |
| 230 | GArray *stored_values; |
| 231 | }; |
| 232 | |
| 233 | static inline |
| 234 | const char *state_string(enum state state) |
| 235 | { |
| 236 | switch (state) { |
| 237 | case STATE_INIT: |
| 238 | return "STATE_INIT"; |
| 239 | case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN: |
| 240 | return "STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN"; |
| 241 | case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE: |
| 242 | return "STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE"; |
| 243 | case STATE_AFTER_TRACE_PACKET_HEADER: |
| 244 | return "STATE_AFTER_TRACE_PACKET_HEADER"; |
| 245 | case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN: |
| 246 | return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN"; |
| 247 | case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE: |
| 248 | return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE"; |
| 249 | case STATE_AFTER_STREAM_PACKET_CONTEXT: |
| 250 | return "STATE_AFTER_STREAM_PACKET_CONTEXT"; |
| 251 | case STATE_EMIT_MSG_NEW_PACKET: |
| 252 | return "STATE_EMIT_MSG_NEW_PACKET"; |
| 253 | case STATE_EMIT_MSG_NEW_STREAM: |
| 254 | return "STATE_EMIT_MSG_NEW_STREAM"; |
| 255 | case STATE_DSCOPE_EVENT_HEADER_BEGIN: |
| 256 | return "STATE_DSCOPE_EVENT_HEADER_BEGIN"; |
| 257 | case STATE_DSCOPE_EVENT_HEADER_CONTINUE: |
| 258 | return "STATE_DSCOPE_EVENT_HEADER_CONTINUE"; |
| 259 | case STATE_AFTER_EVENT_HEADER: |
| 260 | return "STATE_AFTER_EVENT_HEADER"; |
| 261 | case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN: |
| 262 | return "STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN"; |
| 263 | case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE: |
| 264 | return "STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE"; |
| 265 | case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN: |
| 266 | return "STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN"; |
| 267 | case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE: |
| 268 | return "STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE"; |
| 269 | case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN: |
| 270 | return "STATE_DSCOPE_EVENT_PAYLOAD_BEGIN"; |
| 271 | case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE: |
| 272 | return "STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE"; |
| 273 | case STATE_EMIT_MSG_EVENT: |
| 274 | return "STATE_EMIT_MSG_EVENT"; |
| 275 | case STATE_EMIT_MSG_END_OF_PACKET: |
| 276 | return "STATE_EMIT_MSG_END_OF_PACKET"; |
| 277 | case STATE_DONE: |
| 278 | return "STATE_DONE"; |
| 279 | case STATE_SKIP_PACKET_PADDING: |
| 280 | return "STATE_SKIP_PACKET_PADDING"; |
| 281 | default: |
| 282 | return "(unknown)"; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | static |
| 287 | int bt_msg_iter_switch_packet(struct bt_msg_iter *notit); |
| 288 | |
| 289 | static |
| 290 | struct stack *stack_new(struct bt_msg_iter *notit) |
| 291 | { |
| 292 | struct stack *stack = NULL; |
| 293 | |
| 294 | stack = g_new0(struct stack, 1); |
| 295 | if (!stack) { |
| 296 | BT_LOGE_STR("Failed to allocate one stack."); |
| 297 | goto error; |
| 298 | } |
| 299 | |
| 300 | stack->entries = g_array_new(FALSE, TRUE, sizeof(struct stack_entry)); |
| 301 | if (!stack->entries) { |
| 302 | BT_LOGE_STR("Failed to allocate a GArray."); |
| 303 | goto error; |
| 304 | } |
| 305 | |
| 306 | BT_LOGD("Created stack: notit-addr=%p, stack-addr=%p", notit, stack); |
| 307 | goto end; |
| 308 | |
| 309 | error: |
| 310 | g_free(stack); |
| 311 | stack = NULL; |
| 312 | |
| 313 | end: |
| 314 | return stack; |
| 315 | } |
| 316 | |
| 317 | static |
| 318 | void stack_destroy(struct stack *stack) |
| 319 | { |
| 320 | BT_ASSERT(stack); |
| 321 | BT_LOGD("Destroying stack: addr=%p", stack); |
| 322 | |
| 323 | if (stack->entries) { |
| 324 | g_array_free(stack->entries, TRUE); |
| 325 | } |
| 326 | |
| 327 | g_free(stack); |
| 328 | } |
| 329 | |
| 330 | static |
| 331 | void stack_push(struct stack *stack, bt_field *base) |
| 332 | { |
| 333 | struct stack_entry *entry; |
| 334 | |
| 335 | BT_ASSERT(stack); |
| 336 | BT_ASSERT(base); |
| 337 | BT_LOGV("Pushing base field on stack: stack-addr=%p, " |
| 338 | "stack-size-before=%zu, stack-size-after=%zu", |
| 339 | stack, stack->size, stack->size + 1); |
| 340 | |
| 341 | if (stack->entries->len == stack->size) { |
| 342 | g_array_set_size(stack->entries, stack->size + 1); |
| 343 | } |
| 344 | |
| 345 | entry = &g_array_index(stack->entries, struct stack_entry, stack->size); |
| 346 | entry->base = base; |
| 347 | entry->index = 0; |
| 348 | stack->size++; |
| 349 | } |
| 350 | |
| 351 | static inline |
| 352 | unsigned int stack_size(struct stack *stack) |
| 353 | { |
| 354 | BT_ASSERT(stack); |
| 355 | return stack->size; |
| 356 | } |
| 357 | |
| 358 | static |
| 359 | void stack_pop(struct stack *stack) |
| 360 | { |
| 361 | BT_ASSERT(stack); |
| 362 | BT_ASSERT(stack_size(stack)); |
| 363 | BT_LOGV("Popping from stack: " |
| 364 | "stack-addr=%p, stack-size-before=%zu, stack-size-after=%zu", |
| 365 | stack, stack->size, stack->size - 1); |
| 366 | stack->size--; |
| 367 | } |
| 368 | |
| 369 | static inline |
| 370 | struct stack_entry *stack_top(struct stack *stack) |
| 371 | { |
| 372 | BT_ASSERT(stack); |
| 373 | BT_ASSERT(stack_size(stack)); |
| 374 | return &g_array_index(stack->entries, struct stack_entry, |
| 375 | stack->size - 1); |
| 376 | } |
| 377 | |
| 378 | static inline |
| 379 | bool stack_empty(struct stack *stack) |
| 380 | { |
| 381 | return stack_size(stack) == 0; |
| 382 | } |
| 383 | |
| 384 | static |
| 385 | void stack_clear(struct stack *stack) |
| 386 | { |
| 387 | BT_ASSERT(stack); |
| 388 | stack->size = 0; |
| 389 | } |
| 390 | |
| 391 | static inline |
| 392 | enum bt_msg_iter_status msg_iter_status_from_m_status( |
| 393 | enum bt_msg_iter_medium_status m_status) |
| 394 | { |
| 395 | /* They are the same */ |
| 396 | return (int) m_status; |
| 397 | } |
| 398 | |
| 399 | static inline |
| 400 | size_t buf_size_bits(struct bt_msg_iter *notit) |
| 401 | { |
| 402 | return notit->buf.sz * 8; |
| 403 | } |
| 404 | |
| 405 | static inline |
| 406 | size_t buf_available_bits(struct bt_msg_iter *notit) |
| 407 | { |
| 408 | return buf_size_bits(notit) - notit->buf.at; |
| 409 | } |
| 410 | |
| 411 | static inline |
| 412 | size_t packet_at(struct bt_msg_iter *notit) |
| 413 | { |
| 414 | return notit->buf.packet_offset + notit->buf.at; |
| 415 | } |
| 416 | |
| 417 | static inline |
| 418 | void buf_consume_bits(struct bt_msg_iter *notit, size_t incr) |
| 419 | { |
| 420 | BT_LOGV("Advancing cursor: notit-addr=%p, cur-before=%zu, cur-after=%zu", |
| 421 | notit, notit->buf.at, notit->buf.at + incr); |
| 422 | notit->buf.at += incr; |
| 423 | } |
| 424 | |
| 425 | static |
| 426 | enum bt_msg_iter_status request_medium_bytes( |
| 427 | struct bt_msg_iter *notit) |
| 428 | { |
| 429 | uint8_t *buffer_addr = NULL; |
| 430 | size_t buffer_sz = 0; |
| 431 | enum bt_msg_iter_medium_status m_status; |
| 432 | |
| 433 | BT_LOGV("Calling user function (request bytes): notit-addr=%p, " |
| 434 | "request-size=%zu", notit, notit->medium.max_request_sz); |
| 435 | m_status = notit->medium.medops.request_bytes( |
| 436 | notit->medium.max_request_sz, &buffer_addr, |
| 437 | &buffer_sz, notit->medium.data); |
| 438 | BT_LOGV("User function returned: status=%s, buf-addr=%p, buf-size=%zu", |
| 439 | bt_msg_iter_medium_status_string(m_status), |
| 440 | buffer_addr, buffer_sz); |
| 441 | if (m_status == BT_MSG_ITER_MEDIUM_STATUS_OK) { |
| 442 | BT_ASSERT(buffer_sz != 0); |
| 443 | |
| 444 | /* New packet offset is old one + old size (in bits) */ |
| 445 | notit->buf.packet_offset += buf_size_bits(notit); |
| 446 | |
| 447 | /* Restart at the beginning of the new medium buffer */ |
| 448 | notit->buf.at = 0; |
| 449 | notit->buf.last_eh_at = SIZE_MAX; |
| 450 | |
| 451 | /* New medium buffer size */ |
| 452 | notit->buf.sz = buffer_sz; |
| 453 | |
| 454 | /* New medium buffer address */ |
| 455 | notit->buf.addr = buffer_addr; |
| 456 | |
| 457 | BT_LOGV("User function returned new bytes: " |
| 458 | "packet-offset=%zu, cur=%zu, size=%zu, addr=%p", |
| 459 | notit->buf.packet_offset, notit->buf.at, |
| 460 | notit->buf.sz, notit->buf.addr); |
| 461 | BT_LOGV_MEM(buffer_addr, buffer_sz, "Returned bytes at %p:", |
| 462 | buffer_addr); |
| 463 | } else if (m_status == BT_MSG_ITER_MEDIUM_STATUS_EOF) { |
| 464 | /* |
| 465 | * User returned end of stream: validate that we're not |
| 466 | * in the middle of a packet header, packet context, or |
| 467 | * event. |
| 468 | */ |
| 469 | if (notit->cur_exp_packet_total_size >= 0) { |
| 470 | if (packet_at(notit) == |
| 471 | notit->cur_exp_packet_total_size) { |
| 472 | goto end; |
| 473 | } |
| 474 | } else { |
| 475 | if (packet_at(notit) == 0) { |
| 476 | goto end; |
| 477 | } |
| 478 | |
| 479 | if (notit->buf.last_eh_at != SIZE_MAX && |
| 480 | notit->buf.at == notit->buf.last_eh_at) { |
| 481 | goto end; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | /* All other states are invalid */ |
| 486 | BT_LOGW("User function returned %s, but message iterator is in an unexpected state: " |
| 487 | "state=%s, cur-packet-size=%" PRId64 ", cur=%zu, " |
| 488 | "packet-cur=%zu, last-eh-at=%zu", |
| 489 | bt_msg_iter_medium_status_string(m_status), |
| 490 | state_string(notit->state), |
| 491 | notit->cur_exp_packet_total_size, |
| 492 | notit->buf.at, packet_at(notit), |
| 493 | notit->buf.last_eh_at); |
| 494 | m_status = BT_MSG_ITER_MEDIUM_STATUS_ERROR; |
| 495 | } else if (m_status < 0) { |
| 496 | BT_LOGW("User function failed: status=%s", |
| 497 | bt_msg_iter_medium_status_string(m_status)); |
| 498 | } |
| 499 | |
| 500 | end: |
| 501 | return msg_iter_status_from_m_status(m_status); |
| 502 | } |
| 503 | |
| 504 | static inline |
| 505 | enum bt_msg_iter_status buf_ensure_available_bits( |
| 506 | struct bt_msg_iter *notit) |
| 507 | { |
| 508 | enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK; |
| 509 | |
| 510 | if (unlikely(buf_available_bits(notit) == 0)) { |
| 511 | /* |
| 512 | * This _cannot_ return BT_MSG_ITER_STATUS_OK |
| 513 | * _and_ no bits. |
| 514 | */ |
| 515 | status = request_medium_bytes(notit); |
| 516 | } |
| 517 | |
| 518 | return status; |
| 519 | } |
| 520 | |
| 521 | static |
| 522 | enum bt_msg_iter_status read_dscope_begin_state( |
| 523 | struct bt_msg_iter *notit, |
| 524 | struct ctf_field_class *dscope_fc, |
| 525 | enum state done_state, enum state continue_state, |
| 526 | bt_field *dscope_field) |
| 527 | { |
| 528 | enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK; |
| 529 | enum bt_bfcr_status bfcr_status; |
| 530 | size_t consumed_bits; |
| 531 | |
| 532 | notit->cur_dscope_field = dscope_field; |
| 533 | BT_LOGV("Starting BFCR: notit-addr=%p, bfcr-addr=%p, fc-addr=%p", |
| 534 | notit, notit->bfcr, dscope_fc); |
| 535 | consumed_bits = bt_bfcr_start(notit->bfcr, dscope_fc, |
| 536 | notit->buf.addr, notit->buf.at, packet_at(notit), |
| 537 | notit->buf.sz, &bfcr_status); |
| 538 | BT_LOGV("BFCR consumed bits: size=%zu", consumed_bits); |
| 539 | |
| 540 | switch (bfcr_status) { |
| 541 | case BT_BFCR_STATUS_OK: |
| 542 | /* Field class was read completely */ |
| 543 | BT_LOGV_STR("Field was completely decoded."); |
| 544 | notit->state = done_state; |
| 545 | break; |
| 546 | case BT_BFCR_STATUS_EOF: |
| 547 | BT_LOGV_STR("BFCR needs more data to decode field completely."); |
| 548 | notit->state = continue_state; |
| 549 | break; |
| 550 | default: |
| 551 | BT_LOGW("BFCR failed to start: notit-addr=%p, bfcr-addr=%p, " |
| 552 | "status=%s", notit, notit->bfcr, |
| 553 | bt_bfcr_status_string(bfcr_status)); |
| 554 | status = BT_MSG_ITER_STATUS_ERROR; |
| 555 | goto end; |
| 556 | } |
| 557 | |
| 558 | /* Consume bits now since we know we're not in an error state */ |
| 559 | buf_consume_bits(notit, consumed_bits); |
| 560 | |
| 561 | end: |
| 562 | return status; |
| 563 | } |
| 564 | |
| 565 | static |
| 566 | enum bt_msg_iter_status read_dscope_continue_state( |
| 567 | struct bt_msg_iter *notit, enum state done_state) |
| 568 | { |
| 569 | enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK; |
| 570 | enum bt_bfcr_status bfcr_status; |
| 571 | size_t consumed_bits; |
| 572 | |
| 573 | BT_LOGV("Continuing BFCR: notit-addr=%p, bfcr-addr=%p", |
| 574 | notit, notit->bfcr); |
| 575 | |
| 576 | status = buf_ensure_available_bits(notit); |
| 577 | if (status != BT_MSG_ITER_STATUS_OK) { |
| 578 | if (status < 0) { |
| 579 | BT_LOGW("Cannot ensure that buffer has at least one byte: " |
| 580 | "msg-addr=%p, status=%s", |
| 581 | notit, bt_msg_iter_status_string(status)); |
| 582 | } else { |
| 583 | BT_LOGV("Cannot ensure that buffer has at least one byte: " |
| 584 | "msg-addr=%p, status=%s", |
| 585 | notit, bt_msg_iter_status_string(status)); |
| 586 | } |
| 587 | |
| 588 | goto end; |
| 589 | } |
| 590 | |
| 591 | consumed_bits = bt_bfcr_continue(notit->bfcr, notit->buf.addr, |
| 592 | notit->buf.sz, &bfcr_status); |
| 593 | BT_LOGV("BFCR consumed bits: size=%zu", consumed_bits); |
| 594 | |
| 595 | switch (bfcr_status) { |
| 596 | case BT_BFCR_STATUS_OK: |
| 597 | /* Type was read completely. */ |
| 598 | BT_LOGV_STR("Field was completely decoded."); |
| 599 | notit->state = done_state; |
| 600 | break; |
| 601 | case BT_BFCR_STATUS_EOF: |
| 602 | /* Stay in this continue state. */ |
| 603 | BT_LOGV_STR("BFCR needs more data to decode field completely."); |
| 604 | break; |
| 605 | default: |
| 606 | BT_LOGW("BFCR failed to continue: notit-addr=%p, bfcr-addr=%p, " |
| 607 | "status=%s", notit, notit->bfcr, |
| 608 | bt_bfcr_status_string(bfcr_status)); |
| 609 | status = BT_MSG_ITER_STATUS_ERROR; |
| 610 | goto end; |
| 611 | } |
| 612 | |
| 613 | /* Consume bits now since we know we're not in an error state. */ |
| 614 | buf_consume_bits(notit, consumed_bits); |
| 615 | end: |
| 616 | return status; |
| 617 | } |
| 618 | |
| 619 | static |
| 620 | void release_event_dscopes(struct bt_msg_iter *notit) |
| 621 | { |
| 622 | notit->dscopes.event_header = NULL; |
| 623 | |
| 624 | if (notit->event_header_field) { |
| 625 | bt_event_header_field_release(notit->event_header_field); |
| 626 | notit->event_header_field = NULL; |
| 627 | } |
| 628 | |
| 629 | notit->dscopes.event_common_context = NULL; |
| 630 | notit->dscopes.event_spec_context = NULL; |
| 631 | notit->dscopes.event_payload = NULL; |
| 632 | } |
| 633 | |
| 634 | static |
| 635 | void release_all_dscopes(struct bt_msg_iter *notit) |
| 636 | { |
| 637 | notit->dscopes.trace_packet_header = NULL; |
| 638 | |
| 639 | if (notit->packet_header_field) { |
| 640 | bt_packet_header_field_release(notit->packet_header_field); |
| 641 | notit->packet_header_field = NULL; |
| 642 | } |
| 643 | |
| 644 | notit->dscopes.stream_packet_context = NULL; |
| 645 | |
| 646 | if (notit->packet_context_field) { |
| 647 | bt_packet_context_field_release(notit->packet_context_field); |
| 648 | notit->packet_context_field = NULL; |
| 649 | } |
| 650 | |
| 651 | release_event_dscopes(notit); |
| 652 | } |
| 653 | |
| 654 | static |
| 655 | enum bt_msg_iter_status read_packet_header_begin_state( |
| 656 | struct bt_msg_iter *notit) |
| 657 | { |
| 658 | struct ctf_field_class *packet_header_fc = NULL; |
| 659 | enum bt_msg_iter_status ret = BT_MSG_ITER_STATUS_OK; |
| 660 | |
| 661 | if (bt_msg_iter_switch_packet(notit)) { |
| 662 | BT_LOGW("Cannot switch packet: notit-addr=%p", notit); |
| 663 | ret = BT_MSG_ITER_STATUS_ERROR; |
| 664 | goto end; |
| 665 | } |
| 666 | |
| 667 | /* Packet header class is common to the whole trace class. */ |
| 668 | packet_header_fc = notit->meta.tc->packet_header_fc; |
| 669 | if (!packet_header_fc) { |
| 670 | notit->state = STATE_AFTER_TRACE_PACKET_HEADER; |
| 671 | goto end; |
| 672 | } |
| 673 | |
| 674 | BT_ASSERT(!notit->packet_header_field); |
| 675 | |
| 676 | if (packet_header_fc->in_ir) { |
| 677 | /* |
| 678 | * Create free packet header field from trace class. |
| 679 | * This field is going to be moved to the packet once we |
| 680 | * create it. We cannot create the packet now because: |
| 681 | * |
| 682 | * 1. A packet is created from a stream. |
| 683 | * 2. A stream is created from a stream class. |
| 684 | * 3. We need the packet header field's content to know |
| 685 | * the ID of the stream class to select. |
| 686 | */ |
| 687 | notit->packet_header_field = |
| 688 | bt_packet_header_field_create( |
| 689 | notit->meta.tc->ir_tc); |
| 690 | if (!notit->packet_header_field) { |
| 691 | BT_LOGE_STR("Cannot create packet header field wrapper from trace class."); |
| 692 | ret = BT_MSG_ITER_STATUS_ERROR; |
| 693 | goto end; |
| 694 | } |
| 695 | |
| 696 | notit->dscopes.trace_packet_header = |
| 697 | bt_packet_header_field_borrow_field( |
| 698 | notit->packet_header_field); |
| 699 | BT_ASSERT(notit->dscopes.trace_packet_header); |
| 700 | } |
| 701 | |
| 702 | notit->cur_stream_class_id = -1; |
| 703 | notit->cur_event_class_id = -1; |
| 704 | notit->cur_data_stream_id = -1; |
| 705 | BT_LOGV("Decoding packet header field:" |
| 706 | "notit-addr=%p, trace-class-addr=%p, fc-addr=%p", |
| 707 | notit, notit->meta.tc, packet_header_fc); |
| 708 | ret = read_dscope_begin_state(notit, packet_header_fc, |
| 709 | STATE_AFTER_TRACE_PACKET_HEADER, |
| 710 | STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE, |
| 711 | notit->dscopes.trace_packet_header); |
| 712 | if (ret < 0) { |
| 713 | BT_LOGW("Cannot decode packet header field: " |
| 714 | "notit-addr=%p, trace-class-addr=%p, " |
| 715 | "fc-addr=%p", |
| 716 | notit, notit->meta.tc, packet_header_fc); |
| 717 | } |
| 718 | |
| 719 | end: |
| 720 | return ret; |
| 721 | } |
| 722 | |
| 723 | static |
| 724 | enum bt_msg_iter_status read_packet_header_continue_state( |
| 725 | struct bt_msg_iter *notit) |
| 726 | { |
| 727 | return read_dscope_continue_state(notit, |
| 728 | STATE_AFTER_TRACE_PACKET_HEADER); |
| 729 | } |
| 730 | |
| 731 | static inline |
| 732 | enum bt_msg_iter_status set_current_stream_class(struct bt_msg_iter *notit) |
| 733 | { |
| 734 | enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK; |
| 735 | struct ctf_stream_class *new_stream_class = NULL; |
| 736 | |
| 737 | if (notit->cur_stream_class_id == -1) { |
| 738 | /* |
| 739 | * No current stream class ID field, therefore only one |
| 740 | * stream class. |
| 741 | */ |
| 742 | if (notit->meta.tc->stream_classes->len != 1) { |
| 743 | BT_LOGW("Need exactly one stream class since there's " |
| 744 | "no stream class ID field: " |
| 745 | "notit-addr=%p", notit); |
| 746 | status = BT_MSG_ITER_STATUS_ERROR; |
| 747 | goto end; |
| 748 | } |
| 749 | |
| 750 | new_stream_class = notit->meta.tc->stream_classes->pdata[0]; |
| 751 | notit->cur_stream_class_id = new_stream_class->id; |
| 752 | } |
| 753 | |
| 754 | new_stream_class = ctf_trace_class_borrow_stream_class_by_id( |
| 755 | notit->meta.tc, notit->cur_stream_class_id); |
| 756 | if (!new_stream_class) { |
| 757 | BT_LOGW("No stream class with ID of stream class ID to use in trace class: " |
| 758 | "notit-addr=%p, stream-class-id=%" PRIu64 ", " |
| 759 | "trace-class-addr=%p", |
| 760 | notit, notit->cur_stream_class_id, notit->meta.tc); |
| 761 | status = BT_MSG_ITER_STATUS_ERROR; |
| 762 | goto end; |
| 763 | } |
| 764 | |
| 765 | if (notit->meta.sc) { |
| 766 | if (new_stream_class != notit->meta.sc) { |
| 767 | BT_LOGW("Two packets refer to two different stream classes within the same packet sequence: " |
| 768 | "notit-addr=%p, prev-stream-class-addr=%p, " |
| 769 | "prev-stream-class-id=%" PRId64 ", " |
| 770 | "next-stream-class-addr=%p, " |
| 771 | "next-stream-class-id=%" PRId64 ", " |
| 772 | "trace-addr=%p", |
| 773 | notit, notit->meta.sc, |
| 774 | notit->meta.sc->id, |
| 775 | new_stream_class, |
| 776 | new_stream_class->id, |
| 777 | notit->meta.tc); |
| 778 | status = BT_MSG_ITER_STATUS_ERROR; |
| 779 | goto end; |
| 780 | } |
| 781 | } else { |
| 782 | notit->meta.sc = new_stream_class; |
| 783 | } |
| 784 | |
| 785 | BT_LOGV("Set current stream class: " |
| 786 | "notit-addr=%p, stream-class-addr=%p, " |
| 787 | "stream-class-id=%" PRId64, |
| 788 | notit, notit->meta.sc, notit->meta.sc->id); |
| 789 | |
| 790 | end: |
| 791 | return status; |
| 792 | } |
| 793 | |
| 794 | static inline |
| 795 | enum bt_msg_iter_status set_current_stream(struct bt_msg_iter *notit) |
| 796 | { |
| 797 | enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK; |
| 798 | bt_stream *stream = NULL; |
| 799 | |
| 800 | BT_LOGV("Calling user function (get stream): notit-addr=%p, " |
| 801 | "stream-class-addr=%p, stream-class-id=%" PRId64, |
| 802 | notit, notit->meta.sc, |
| 803 | notit->meta.sc->id); |
| 804 | stream = notit->medium.medops.borrow_stream( |
| 805 | notit->meta.sc->ir_sc, notit->cur_data_stream_id, |
| 806 | notit->medium.data); |
| 807 | bt_stream_get_ref(stream); |
| 808 | BT_LOGV("User function returned: stream-addr=%p", stream); |
| 809 | if (!stream) { |
| 810 | BT_LOGW_STR("User function failed to return a stream object " |
| 811 | "for the given stream class."); |
| 812 | status = BT_MSG_ITER_STATUS_ERROR; |
| 813 | goto end; |
| 814 | } |
| 815 | |
| 816 | if (notit->stream && stream != notit->stream) { |
| 817 | BT_LOGW("User function returned a different stream than the " |
| 818 | "previous one for the same sequence of packets."); |
| 819 | status = BT_MSG_ITER_STATUS_ERROR; |
| 820 | goto end; |
| 821 | } |
| 822 | |
| 823 | BT_STREAM_MOVE_REF(notit->stream, stream); |
| 824 | |
| 825 | end: |
| 826 | bt_stream_put_ref(stream); |
| 827 | return status; |
| 828 | } |
| 829 | |
| 830 | static inline |
| 831 | enum bt_msg_iter_status set_current_packet(struct bt_msg_iter *notit) |
| 832 | { |
| 833 | enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK; |
| 834 | bt_packet *packet = NULL; |
| 835 | |
| 836 | BT_LOGV("Creating packet for packet message: " |
| 837 | "notit-addr=%p", notit); |
| 838 | BT_LOGV("Creating packet from stream: " |
| 839 | "notit-addr=%p, stream-addr=%p, " |
| 840 | "stream-class-addr=%p, " |
| 841 | "stream-class-id=%" PRId64, |
| 842 | notit, notit->stream, notit->meta.sc, |
| 843 | notit->meta.sc->id); |
| 844 | |
| 845 | /* Create packet */ |
| 846 | BT_ASSERT(notit->stream); |
| 847 | packet = bt_packet_create(notit->stream); |
| 848 | if (!packet) { |
| 849 | BT_LOGE("Cannot create packet from stream: " |
| 850 | "notit-addr=%p, stream-addr=%p, " |
| 851 | "stream-class-addr=%p, " |
| 852 | "stream-class-id=%" PRId64, |
| 853 | notit, notit->stream, notit->meta.sc, |
| 854 | notit->meta.sc->id); |
| 855 | goto error; |
| 856 | } |
| 857 | |
| 858 | goto end; |
| 859 | |
| 860 | error: |
| 861 | BT_PACKET_PUT_REF_AND_RESET(packet); |
| 862 | status = BT_MSG_ITER_STATUS_ERROR; |
| 863 | |
| 864 | end: |
| 865 | BT_PACKET_MOVE_REF(notit->packet, packet); |
| 866 | return status; |
| 867 | } |
| 868 | |
| 869 | static |
| 870 | enum bt_msg_iter_status after_packet_header_state( |
| 871 | struct bt_msg_iter *notit) |
| 872 | { |
| 873 | enum bt_msg_iter_status status; |
| 874 | |
| 875 | status = set_current_stream_class(notit); |
| 876 | if (status != BT_MSG_ITER_STATUS_OK) { |
| 877 | goto end; |
| 878 | } |
| 879 | |
| 880 | notit->state = STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN; |
| 881 | |
| 882 | end: |
| 883 | return status; |
| 884 | } |
| 885 | |
| 886 | static |
| 887 | enum bt_msg_iter_status read_packet_context_begin_state( |
| 888 | struct bt_msg_iter *notit) |
| 889 | { |
| 890 | enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK; |
| 891 | struct ctf_field_class *packet_context_fc; |
| 892 | |
| 893 | BT_ASSERT(notit->meta.sc); |
| 894 | packet_context_fc = notit->meta.sc->packet_context_fc; |
| 895 | if (!packet_context_fc) { |
| 896 | BT_LOGV("No packet packet context field class in stream class: continuing: " |
| 897 | "notit-addr=%p, stream-class-addr=%p, " |
| 898 | "stream-class-id=%" PRId64, |
| 899 | notit, notit->meta.sc, |
| 900 | notit->meta.sc->id); |
| 901 | notit->state = STATE_AFTER_STREAM_PACKET_CONTEXT; |
| 902 | goto end; |
| 903 | } |
| 904 | |
| 905 | BT_ASSERT(!notit->packet_context_field); |
| 906 | |
| 907 | if (packet_context_fc->in_ir) { |
| 908 | /* |
| 909 | * Create free packet context field from stream class. |
| 910 | * This field is going to be moved to the packet once we |
| 911 | * create it. We cannot create the packet now because a |
| 912 | * packet is created from a stream, and this API must be |
| 913 | * able to return the packet header and context fields |
| 914 | * without creating a stream |
| 915 | * (read_packet_header_context_fields()). |
| 916 | */ |
| 917 | notit->packet_context_field = |
| 918 | bt_packet_context_field_create( |
| 919 | notit->meta.sc->ir_sc); |
| 920 | if (!notit->packet_context_field) { |
| 921 | BT_LOGE_STR("Cannot create packet context field wrapper from stream class."); |
| 922 | status = BT_MSG_ITER_STATUS_ERROR; |
| 923 | goto end; |
| 924 | } |
| 925 | |
| 926 | notit->dscopes.stream_packet_context = |
| 927 | bt_packet_context_field_borrow_field( |
| 928 | notit->packet_context_field); |
| 929 | BT_ASSERT(notit->dscopes.stream_packet_context); |
| 930 | } |
| 931 | |
| 932 | BT_LOGV("Decoding packet context field: " |
| 933 | "notit-addr=%p, stream-class-addr=%p, " |
| 934 | "stream-class-id=%" PRId64 ", fc-addr=%p", |
| 935 | notit, notit->meta.sc, |
| 936 | notit->meta.sc->id, packet_context_fc); |
| 937 | status = read_dscope_begin_state(notit, packet_context_fc, |
| 938 | STATE_AFTER_STREAM_PACKET_CONTEXT, |
| 939 | STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE, |
| 940 | notit->dscopes.stream_packet_context); |
| 941 | if (status < 0) { |
| 942 | BT_LOGW("Cannot decode packet context field: " |
| 943 | "notit-addr=%p, stream-class-addr=%p, " |
| 944 | "stream-class-id=%" PRId64 ", fc-addr=%p", |
| 945 | notit, notit->meta.sc, |
| 946 | notit->meta.sc->id, |
| 947 | packet_context_fc); |
| 948 | } |
| 949 | |
| 950 | end: |
| 951 | return status; |
| 952 | } |
| 953 | |
| 954 | static |
| 955 | enum bt_msg_iter_status read_packet_context_continue_state( |
| 956 | struct bt_msg_iter *notit) |
| 957 | { |
| 958 | return read_dscope_continue_state(notit, |
| 959 | STATE_AFTER_STREAM_PACKET_CONTEXT); |
| 960 | } |
| 961 | |
| 962 | static |
| 963 | enum bt_msg_iter_status set_current_packet_content_sizes( |
| 964 | struct bt_msg_iter *notit) |
| 965 | { |
| 966 | enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK; |
| 967 | |
| 968 | if (notit->cur_exp_packet_total_size == -1) { |
| 969 | if (notit->cur_exp_packet_content_size != -1) { |
| 970 | BT_LOGW("Content size is set, but packet size is not: " |
| 971 | "notit-addr=%p, packet-context-field-addr=%p, " |
| 972 | "packet-size=%" PRId64 ", content-size=%" PRId64, |
| 973 | notit, notit->dscopes.stream_packet_context, |
| 974 | notit->cur_exp_packet_total_size, |
| 975 | notit->cur_exp_packet_content_size); |
| 976 | status = BT_MSG_ITER_STATUS_ERROR; |
| 977 | goto end; |
| 978 | } |
| 979 | } else { |
| 980 | if (notit->cur_exp_packet_content_size == -1) { |
| 981 | notit->cur_exp_packet_content_size = |
| 982 | notit->cur_exp_packet_total_size; |
| 983 | } |
| 984 | } |
| 985 | |
| 986 | if (notit->cur_exp_packet_content_size > |
| 987 | notit->cur_exp_packet_total_size) { |
| 988 | BT_LOGW("Invalid packet or content size: " |
| 989 | "content size is greater than packet size: " |
| 990 | "notit-addr=%p, packet-context-field-addr=%p, " |
| 991 | "packet-size=%" PRId64 ", content-size=%" PRId64, |
| 992 | notit, notit->dscopes.stream_packet_context, |
| 993 | notit->cur_exp_packet_total_size, |
| 994 | notit->cur_exp_packet_content_size); |
| 995 | status = BT_MSG_ITER_STATUS_ERROR; |
| 996 | goto end; |
| 997 | } |
| 998 | |
| 999 | BT_LOGV("Set current packet and content sizes: " |
| 1000 | "notit-addr=%p, packet-size=%" PRIu64 ", content-size=%" PRIu64, |
| 1001 | notit, notit->cur_exp_packet_total_size, |
| 1002 | notit->cur_exp_packet_content_size); |
| 1003 | end: |
| 1004 | return status; |
| 1005 | } |
| 1006 | |
| 1007 | static |
| 1008 | enum bt_msg_iter_status after_packet_context_state( |
| 1009 | struct bt_msg_iter *notit) |
| 1010 | { |
| 1011 | enum bt_msg_iter_status status; |
| 1012 | |
| 1013 | status = set_current_packet_content_sizes(notit); |
| 1014 | if (status != BT_MSG_ITER_STATUS_OK) { |
| 1015 | goto end; |
| 1016 | } |
| 1017 | |
| 1018 | if (notit->stream_begin_emitted) { |
| 1019 | notit->state = STATE_EMIT_MSG_NEW_PACKET; |
| 1020 | } else { |
| 1021 | notit->state = STATE_EMIT_MSG_NEW_STREAM; |
| 1022 | } |
| 1023 | |
| 1024 | end: |
| 1025 | return status; |
| 1026 | } |
| 1027 | |
| 1028 | static |
| 1029 | enum bt_msg_iter_status read_event_header_begin_state( |
| 1030 | struct bt_msg_iter *notit) |
| 1031 | { |
| 1032 | enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK; |
| 1033 | struct ctf_field_class *event_header_fc = NULL; |
| 1034 | |
| 1035 | /* Reset the position of the last event header */ |
| 1036 | notit->buf.last_eh_at = notit->buf.at; |
| 1037 | notit->cur_event_class_id = -1; |
| 1038 | |
| 1039 | /* Check if we have some content left */ |
| 1040 | if (notit->cur_exp_packet_content_size >= 0) { |
| 1041 | if (unlikely(packet_at(notit) == |
| 1042 | notit->cur_exp_packet_content_size)) { |
| 1043 | /* No more events! */ |
| 1044 | BT_LOGV("Reached end of packet: notit-addr=%p, " |
| 1045 | "cur=%zu", notit, packet_at(notit)); |
| 1046 | notit->state = STATE_EMIT_MSG_END_OF_PACKET; |
| 1047 | goto end; |
| 1048 | } else if (unlikely(packet_at(notit) > |
| 1049 | notit->cur_exp_packet_content_size)) { |
| 1050 | /* That's not supposed to happen */ |
| 1051 | BT_LOGV("Before decoding event header field: cursor is passed the packet's content: " |
| 1052 | "notit-addr=%p, content-size=%" PRId64 ", " |
| 1053 | "cur=%zu", notit, |
| 1054 | notit->cur_exp_packet_content_size, |
| 1055 | packet_at(notit)); |
| 1056 | status = BT_MSG_ITER_STATUS_ERROR; |
| 1057 | goto end; |
| 1058 | } |
| 1059 | } else { |
| 1060 | /* |
| 1061 | * "Infinite" content: we're done when the medium has |
| 1062 | * nothing else for us. |
| 1063 | */ |
| 1064 | status = buf_ensure_available_bits(notit); |
| 1065 | if (status != BT_MSG_ITER_STATUS_OK) { |
| 1066 | /* |
| 1067 | * If this function returns |
| 1068 | * `BT_MSG_ITER_STATUS_EOF`: |
| 1069 | * |
| 1070 | * 1. bt_msg_iter_get_next_message() |
| 1071 | * emits a "packet end" message. This |
| 1072 | * resets the current packet. The state |
| 1073 | * remains unchanged otherwise. |
| 1074 | * 2. This function is called again. It returns |
| 1075 | * `BT_MSG_ITER_STATUS_EOF` again. |
| 1076 | * 3. bt_msg_iter_get_next_message() |
| 1077 | * emits a "stream end" message because |
| 1078 | * there's no current packet. It sets the |
| 1079 | * current state to `STATE_DONE`. |
| 1080 | */ |
| 1081 | goto end; |
| 1082 | } |
| 1083 | } |
| 1084 | |
| 1085 | release_event_dscopes(notit); |
| 1086 | BT_ASSERT(notit->meta.sc); |
| 1087 | event_header_fc = notit->meta.sc->event_header_fc; |
| 1088 | if (!event_header_fc) { |
| 1089 | notit->state = STATE_AFTER_EVENT_HEADER; |
| 1090 | goto end; |
| 1091 | } |
| 1092 | |
| 1093 | if (event_header_fc->in_ir) { |
| 1094 | BT_ASSERT(!notit->event_header_field); |
| 1095 | notit->event_header_field = |
| 1096 | bt_event_header_field_create( |
| 1097 | notit->meta.sc->ir_sc); |
| 1098 | if (!notit->event_header_field) { |
| 1099 | BT_LOGE_STR("Cannot create event header field wrapper from trace class."); |
| 1100 | status = BT_MSG_ITER_STATUS_ERROR; |
| 1101 | goto end; |
| 1102 | } |
| 1103 | |
| 1104 | notit->dscopes.event_header = |
| 1105 | bt_event_header_field_borrow_field( |
| 1106 | notit->event_header_field); |
| 1107 | BT_ASSERT(notit->dscopes.event_header); |
| 1108 | } |
| 1109 | |
| 1110 | BT_LOGV("Decoding event header field: " |
| 1111 | "notit-addr=%p, stream-class-addr=%p, " |
| 1112 | "stream-class-id=%" PRId64 ", " |
| 1113 | "fc-addr=%p", |
| 1114 | notit, notit->meta.sc, |
| 1115 | notit->meta.sc->id, |
| 1116 | event_header_fc); |
| 1117 | status = read_dscope_begin_state(notit, event_header_fc, |
| 1118 | STATE_AFTER_EVENT_HEADER, |
| 1119 | STATE_DSCOPE_EVENT_HEADER_CONTINUE, |
| 1120 | notit->dscopes.event_header); |
| 1121 | if (status < 0) { |
| 1122 | BT_LOGW("Cannot decode event header field: " |
| 1123 | "notit-addr=%p, stream-class-addr=%p, " |
| 1124 | "stream-class-id=%" PRId64 ", fc-addr=%p", |
| 1125 | notit, notit->meta.sc, |
| 1126 | notit->meta.sc->id, |
| 1127 | event_header_fc); |
| 1128 | } |
| 1129 | |
| 1130 | end: |
| 1131 | return status; |
| 1132 | } |
| 1133 | |
| 1134 | static |
| 1135 | enum bt_msg_iter_status read_event_header_continue_state( |
| 1136 | struct bt_msg_iter *notit) |
| 1137 | { |
| 1138 | return read_dscope_continue_state(notit, |
| 1139 | STATE_AFTER_EVENT_HEADER); |
| 1140 | } |
| 1141 | |
| 1142 | static inline |
| 1143 | enum bt_msg_iter_status set_current_event_class(struct bt_msg_iter *notit) |
| 1144 | { |
| 1145 | enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK; |
| 1146 | |
| 1147 | struct ctf_event_class *new_event_class = NULL; |
| 1148 | |
| 1149 | if (notit->cur_event_class_id == -1) { |
| 1150 | /* |
| 1151 | * No current event class ID field, therefore only one |
| 1152 | * event class. |
| 1153 | */ |
| 1154 | if (notit->meta.sc->event_classes->len != 1) { |
| 1155 | BT_LOGW("Need exactly one event class since there's " |
| 1156 | "no event class ID field: " |
| 1157 | "notit-addr=%p", notit); |
| 1158 | status = BT_MSG_ITER_STATUS_ERROR; |
| 1159 | goto end; |
| 1160 | } |
| 1161 | |
| 1162 | new_event_class = notit->meta.sc->event_classes->pdata[0]; |
| 1163 | notit->cur_event_class_id = new_event_class->id; |
| 1164 | } |
| 1165 | |
| 1166 | new_event_class = ctf_stream_class_borrow_event_class_by_id( |
| 1167 | notit->meta.sc, notit->cur_event_class_id); |
| 1168 | if (!new_event_class) { |
| 1169 | BT_LOGW("No event class with ID of event class ID to use in stream class: " |
| 1170 | "notit-addr=%p, stream-class-id=%" PRIu64 ", " |
| 1171 | "event-class-id=%" PRIu64 ", " |
| 1172 | "trace-class-addr=%p", |
| 1173 | notit, notit->meta.sc->id, notit->cur_event_class_id, |
| 1174 | notit->meta.tc); |
| 1175 | status = BT_MSG_ITER_STATUS_ERROR; |
| 1176 | goto end; |
| 1177 | } |
| 1178 | |
| 1179 | notit->meta.ec = new_event_class; |
| 1180 | BT_LOGV("Set current event class: " |
| 1181 | "notit-addr=%p, event-class-addr=%p, " |
| 1182 | "event-class-id=%" PRId64 ", " |
| 1183 | "event-class-name=\"%s\"", |
| 1184 | notit, notit->meta.ec, notit->meta.ec->id, |
| 1185 | notit->meta.ec->name->str); |
| 1186 | |
| 1187 | end: |
| 1188 | return status; |
| 1189 | } |
| 1190 | |
| 1191 | static inline |
| 1192 | enum bt_msg_iter_status set_current_event_message( |
| 1193 | struct bt_msg_iter *notit) |
| 1194 | { |
| 1195 | enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK; |
| 1196 | bt_message *msg = NULL; |
| 1197 | |
| 1198 | BT_ASSERT(notit->meta.ec); |
| 1199 | BT_ASSERT(notit->packet); |
| 1200 | BT_LOGV("Creating event message from event class and packet: " |
| 1201 | "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", packet-addr=%p", |
| 1202 | notit, notit->meta.ec, |
| 1203 | notit->meta.ec->name->str, |
| 1204 | notit->packet); |
| 1205 | BT_ASSERT(notit->msg_iter); |
| 1206 | msg = bt_message_event_create(notit->msg_iter, |
| 1207 | notit->meta.ec->ir_ec, notit->packet); |
| 1208 | if (!msg) { |
| 1209 | BT_LOGE("Cannot create event message: " |
| 1210 | "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", " |
| 1211 | "packet-addr=%p", |
| 1212 | notit, notit->meta.ec, |
| 1213 | notit->meta.ec->name->str, |
| 1214 | notit->packet); |
| 1215 | goto error; |
| 1216 | } |
| 1217 | |
| 1218 | goto end; |
| 1219 | |
| 1220 | error: |
| 1221 | BT_MESSAGE_PUT_REF_AND_RESET(msg); |
| 1222 | status = BT_MSG_ITER_STATUS_ERROR; |
| 1223 | |
| 1224 | end: |
| 1225 | BT_MESSAGE_MOVE_REF(notit->event_msg, msg); |
| 1226 | return status; |
| 1227 | } |
| 1228 | |
| 1229 | static |
| 1230 | enum bt_msg_iter_status after_event_header_state( |
| 1231 | struct bt_msg_iter *notit) |
| 1232 | { |
| 1233 | enum bt_msg_iter_status status; |
| 1234 | |
| 1235 | status = set_current_event_class(notit); |
| 1236 | if (status != BT_MSG_ITER_STATUS_OK) { |
| 1237 | goto end; |
| 1238 | } |
| 1239 | |
| 1240 | status = set_current_event_message(notit); |
| 1241 | if (status != BT_MSG_ITER_STATUS_OK) { |
| 1242 | goto end; |
| 1243 | } |
| 1244 | |
| 1245 | notit->event = bt_message_event_borrow_event( |
| 1246 | notit->event_msg); |
| 1247 | BT_ASSERT(notit->event); |
| 1248 | |
| 1249 | if (notit->event_header_field) { |
| 1250 | int ret; |
| 1251 | |
| 1252 | BT_ASSERT(notit->event); |
| 1253 | ret = bt_event_move_header_field(notit->event, |
| 1254 | notit->event_header_field); |
| 1255 | if (ret) { |
| 1256 | status = BT_MSG_ITER_STATUS_ERROR; |
| 1257 | goto end; |
| 1258 | } |
| 1259 | |
| 1260 | notit->event_header_field = NULL; |
| 1261 | |
| 1262 | /* |
| 1263 | * At this point notit->dscopes.event_header has |
| 1264 | * the same value as the event header field within |
| 1265 | * notit->event. |
| 1266 | */ |
| 1267 | BT_ASSERT(bt_event_borrow_header_field( |
| 1268 | notit->event) == notit->dscopes.event_header); |
| 1269 | } |
| 1270 | |
| 1271 | notit->state = STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN; |
| 1272 | |
| 1273 | end: |
| 1274 | return status; |
| 1275 | } |
| 1276 | |
| 1277 | static |
| 1278 | enum bt_msg_iter_status read_event_common_context_begin_state( |
| 1279 | struct bt_msg_iter *notit) |
| 1280 | { |
| 1281 | enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK; |
| 1282 | struct ctf_field_class *event_common_context_fc; |
| 1283 | |
| 1284 | event_common_context_fc = notit->meta.sc->event_common_context_fc; |
| 1285 | if (!event_common_context_fc) { |
| 1286 | notit->state = STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN; |
| 1287 | goto end; |
| 1288 | } |
| 1289 | |
| 1290 | if (event_common_context_fc->in_ir) { |
| 1291 | BT_ASSERT(!notit->dscopes.event_common_context); |
| 1292 | notit->dscopes.event_common_context = |
| 1293 | bt_event_borrow_common_context_field( |
| 1294 | notit->event); |
| 1295 | BT_ASSERT(notit->dscopes.event_common_context); |
| 1296 | } |
| 1297 | |
| 1298 | BT_LOGV("Decoding event common context field: " |
| 1299 | "notit-addr=%p, stream-class-addr=%p, " |
| 1300 | "stream-class-id=%" PRId64 ", " |
| 1301 | "fc-addr=%p", |
| 1302 | notit, notit->meta.sc, |
| 1303 | notit->meta.sc->id, |
| 1304 | event_common_context_fc); |
| 1305 | status = read_dscope_begin_state(notit, event_common_context_fc, |
| 1306 | STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN, |
| 1307 | STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE, |
| 1308 | notit->dscopes.event_common_context); |
| 1309 | if (status < 0) { |
| 1310 | BT_LOGW("Cannot decode event common context field: " |
| 1311 | "notit-addr=%p, stream-class-addr=%p, " |
| 1312 | "stream-class-id=%" PRId64 ", fc-addr=%p", |
| 1313 | notit, notit->meta.sc, |
| 1314 | notit->meta.sc->id, |
| 1315 | event_common_context_fc); |
| 1316 | } |
| 1317 | |
| 1318 | end: |
| 1319 | return status; |
| 1320 | } |
| 1321 | |
| 1322 | static |
| 1323 | enum bt_msg_iter_status read_event_common_context_continue_state( |
| 1324 | struct bt_msg_iter *notit) |
| 1325 | { |
| 1326 | return read_dscope_continue_state(notit, |
| 1327 | STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN); |
| 1328 | } |
| 1329 | |
| 1330 | static |
| 1331 | enum bt_msg_iter_status read_event_spec_context_begin_state( |
| 1332 | struct bt_msg_iter *notit) |
| 1333 | { |
| 1334 | enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK; |
| 1335 | struct ctf_field_class *event_spec_context_fc; |
| 1336 | |
| 1337 | event_spec_context_fc = notit->meta.ec->spec_context_fc; |
| 1338 | if (!event_spec_context_fc) { |
| 1339 | notit->state = STATE_DSCOPE_EVENT_PAYLOAD_BEGIN; |
| 1340 | goto end; |
| 1341 | } |
| 1342 | |
| 1343 | if (event_spec_context_fc->in_ir) { |
| 1344 | BT_ASSERT(!notit->dscopes.event_spec_context); |
| 1345 | notit->dscopes.event_spec_context = |
| 1346 | bt_event_borrow_specific_context_field( |
| 1347 | notit->event); |
| 1348 | BT_ASSERT(notit->dscopes.event_spec_context); |
| 1349 | } |
| 1350 | |
| 1351 | BT_LOGV("Decoding event specific context field: " |
| 1352 | "notit-addr=%p, event-class-addr=%p, " |
| 1353 | "event-class-name=\"%s\", event-class-id=%" PRId64 ", " |
| 1354 | "fc-addr=%p", |
| 1355 | notit, notit->meta.ec, |
| 1356 | notit->meta.ec->name->str, |
| 1357 | notit->meta.ec->id, |
| 1358 | event_spec_context_fc); |
| 1359 | status = read_dscope_begin_state(notit, event_spec_context_fc, |
| 1360 | STATE_DSCOPE_EVENT_PAYLOAD_BEGIN, |
| 1361 | STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE, |
| 1362 | notit->dscopes.event_spec_context); |
| 1363 | if (status < 0) { |
| 1364 | BT_LOGW("Cannot decode event specific context field: " |
| 1365 | "notit-addr=%p, event-class-addr=%p, " |
| 1366 | "event-class-name=\"%s\", " |
| 1367 | "event-class-id=%" PRId64 ", fc-addr=%p", |
| 1368 | notit, notit->meta.ec, |
| 1369 | notit->meta.ec->name->str, |
| 1370 | notit->meta.ec->id, |
| 1371 | event_spec_context_fc); |
| 1372 | } |
| 1373 | |
| 1374 | end: |
| 1375 | return status; |
| 1376 | } |
| 1377 | |
| 1378 | static |
| 1379 | enum bt_msg_iter_status read_event_spec_context_continue_state( |
| 1380 | struct bt_msg_iter *notit) |
| 1381 | { |
| 1382 | return read_dscope_continue_state(notit, |
| 1383 | STATE_DSCOPE_EVENT_PAYLOAD_BEGIN); |
| 1384 | } |
| 1385 | |
| 1386 | static |
| 1387 | enum bt_msg_iter_status read_event_payload_begin_state( |
| 1388 | struct bt_msg_iter *notit) |
| 1389 | { |
| 1390 | enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK; |
| 1391 | struct ctf_field_class *event_payload_fc; |
| 1392 | |
| 1393 | event_payload_fc = notit->meta.ec->payload_fc; |
| 1394 | if (!event_payload_fc) { |
| 1395 | notit->state = STATE_EMIT_MSG_EVENT; |
| 1396 | goto end; |
| 1397 | } |
| 1398 | |
| 1399 | if (event_payload_fc->in_ir) { |
| 1400 | BT_ASSERT(!notit->dscopes.event_payload); |
| 1401 | notit->dscopes.event_payload = |
| 1402 | bt_event_borrow_payload_field( |
| 1403 | notit->event); |
| 1404 | BT_ASSERT(notit->dscopes.event_payload); |
| 1405 | } |
| 1406 | |
| 1407 | BT_LOGV("Decoding event payload field: " |
| 1408 | "notit-addr=%p, event-class-addr=%p, " |
| 1409 | "event-class-name=\"%s\", event-class-id=%" PRId64 ", " |
| 1410 | "fc-addr=%p", |
| 1411 | notit, notit->meta.ec, |
| 1412 | notit->meta.ec->name->str, |
| 1413 | notit->meta.ec->id, |
| 1414 | event_payload_fc); |
| 1415 | status = read_dscope_begin_state(notit, event_payload_fc, |
| 1416 | STATE_EMIT_MSG_EVENT, |
| 1417 | STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE, |
| 1418 | notit->dscopes.event_payload); |
| 1419 | if (status < 0) { |
| 1420 | BT_LOGW("Cannot decode event payload field: " |
| 1421 | "notit-addr=%p, event-class-addr=%p, " |
| 1422 | "event-class-name=\"%s\", " |
| 1423 | "event-class-id=%" PRId64 ", fc-addr=%p", |
| 1424 | notit, notit->meta.ec, |
| 1425 | notit->meta.ec->name->str, |
| 1426 | notit->meta.ec->id, |
| 1427 | event_payload_fc); |
| 1428 | } |
| 1429 | |
| 1430 | end: |
| 1431 | return status; |
| 1432 | } |
| 1433 | |
| 1434 | static |
| 1435 | enum bt_msg_iter_status read_event_payload_continue_state( |
| 1436 | struct bt_msg_iter *notit) |
| 1437 | { |
| 1438 | return read_dscope_continue_state(notit, STATE_EMIT_MSG_EVENT); |
| 1439 | } |
| 1440 | |
| 1441 | static |
| 1442 | enum bt_msg_iter_status skip_packet_padding_state( |
| 1443 | struct bt_msg_iter *notit) |
| 1444 | { |
| 1445 | enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK; |
| 1446 | size_t bits_to_skip; |
| 1447 | |
| 1448 | BT_ASSERT(notit->cur_exp_packet_total_size > 0); |
| 1449 | bits_to_skip = notit->cur_exp_packet_total_size - packet_at(notit); |
| 1450 | if (bits_to_skip == 0) { |
| 1451 | notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN; |
| 1452 | goto end; |
| 1453 | } else { |
| 1454 | size_t bits_to_consume; |
| 1455 | |
| 1456 | BT_LOGV("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu", |
| 1457 | bits_to_skip, notit, bits_to_skip); |
| 1458 | status = buf_ensure_available_bits(notit); |
| 1459 | if (status != BT_MSG_ITER_STATUS_OK) { |
| 1460 | goto end; |
| 1461 | } |
| 1462 | |
| 1463 | bits_to_consume = MIN(buf_available_bits(notit), bits_to_skip); |
| 1464 | BT_LOGV("Skipping %zu bits of padding: notit-addr=%p, size=%zu", |
| 1465 | bits_to_consume, notit, bits_to_consume); |
| 1466 | buf_consume_bits(notit, bits_to_consume); |
| 1467 | bits_to_skip = notit->cur_exp_packet_total_size - |
| 1468 | packet_at(notit); |
| 1469 | if (bits_to_skip == 0) { |
| 1470 | notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN; |
| 1471 | goto end; |
| 1472 | } |
| 1473 | } |
| 1474 | |
| 1475 | end: |
| 1476 | return status; |
| 1477 | } |
| 1478 | |
| 1479 | static inline |
| 1480 | enum bt_msg_iter_status handle_state(struct bt_msg_iter *notit) |
| 1481 | { |
| 1482 | enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK; |
| 1483 | const enum state state = notit->state; |
| 1484 | |
| 1485 | BT_LOGV("Handling state: notit-addr=%p, state=%s", |
| 1486 | notit, state_string(state)); |
| 1487 | |
| 1488 | // TODO: optimalize! |
| 1489 | switch (state) { |
| 1490 | case STATE_INIT: |
| 1491 | notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN; |
| 1492 | break; |
| 1493 | case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN: |
| 1494 | status = read_packet_header_begin_state(notit); |
| 1495 | break; |
| 1496 | case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE: |
| 1497 | status = read_packet_header_continue_state(notit); |
| 1498 | break; |
| 1499 | case STATE_AFTER_TRACE_PACKET_HEADER: |
| 1500 | status = after_packet_header_state(notit); |
| 1501 | break; |
| 1502 | case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN: |
| 1503 | status = read_packet_context_begin_state(notit); |
| 1504 | break; |
| 1505 | case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE: |
| 1506 | status = read_packet_context_continue_state(notit); |
| 1507 | break; |
| 1508 | case STATE_AFTER_STREAM_PACKET_CONTEXT: |
| 1509 | status = after_packet_context_state(notit); |
| 1510 | break; |
| 1511 | case STATE_EMIT_MSG_NEW_STREAM: |
| 1512 | notit->state = STATE_EMIT_MSG_NEW_PACKET; |
| 1513 | break; |
| 1514 | case STATE_EMIT_MSG_NEW_PACKET: |
| 1515 | notit->state = STATE_DSCOPE_EVENT_HEADER_BEGIN; |
| 1516 | break; |
| 1517 | case STATE_DSCOPE_EVENT_HEADER_BEGIN: |
| 1518 | status = read_event_header_begin_state(notit); |
| 1519 | break; |
| 1520 | case STATE_DSCOPE_EVENT_HEADER_CONTINUE: |
| 1521 | status = read_event_header_continue_state(notit); |
| 1522 | break; |
| 1523 | case STATE_AFTER_EVENT_HEADER: |
| 1524 | status = after_event_header_state(notit); |
| 1525 | break; |
| 1526 | case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN: |
| 1527 | status = read_event_common_context_begin_state(notit); |
| 1528 | break; |
| 1529 | case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE: |
| 1530 | status = read_event_common_context_continue_state(notit); |
| 1531 | break; |
| 1532 | case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN: |
| 1533 | status = read_event_spec_context_begin_state(notit); |
| 1534 | break; |
| 1535 | case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE: |
| 1536 | status = read_event_spec_context_continue_state(notit); |
| 1537 | break; |
| 1538 | case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN: |
| 1539 | status = read_event_payload_begin_state(notit); |
| 1540 | break; |
| 1541 | case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE: |
| 1542 | status = read_event_payload_continue_state(notit); |
| 1543 | break; |
| 1544 | case STATE_EMIT_MSG_EVENT: |
| 1545 | notit->state = STATE_DSCOPE_EVENT_HEADER_BEGIN; |
| 1546 | break; |
| 1547 | case STATE_SKIP_PACKET_PADDING: |
| 1548 | status = skip_packet_padding_state(notit); |
| 1549 | break; |
| 1550 | case STATE_EMIT_MSG_END_OF_PACKET: |
| 1551 | notit->state = STATE_SKIP_PACKET_PADDING; |
| 1552 | break; |
| 1553 | default: |
| 1554 | BT_LOGD("Unknown CTF plugin message iterator state: " |
| 1555 | "notit-addr=%p, state=%d", notit, notit->state); |
| 1556 | abort(); |
| 1557 | } |
| 1558 | |
| 1559 | BT_LOGV("Handled state: notit-addr=%p, status=%s, " |
| 1560 | "prev-state=%s, cur-state=%s", |
| 1561 | notit, bt_msg_iter_status_string(status), |
| 1562 | state_string(state), state_string(notit->state)); |
| 1563 | return status; |
| 1564 | } |
| 1565 | |
| 1566 | /** |
| 1567 | * Resets the internal state of a CTF message iterator. |
| 1568 | */ |
| 1569 | BT_HIDDEN |
| 1570 | void bt_msg_iter_reset(struct bt_msg_iter *notit) |
| 1571 | { |
| 1572 | BT_ASSERT(notit); |
| 1573 | BT_LOGD("Resetting message iterator: addr=%p", notit); |
| 1574 | stack_clear(notit->stack); |
| 1575 | notit->meta.sc = NULL; |
| 1576 | notit->meta.ec = NULL; |
| 1577 | BT_PACKET_PUT_REF_AND_RESET(notit->packet); |
| 1578 | BT_STREAM_PUT_REF_AND_RESET(notit->stream); |
| 1579 | BT_MESSAGE_PUT_REF_AND_RESET(notit->event_msg); |
| 1580 | release_all_dscopes(notit); |
| 1581 | notit->cur_dscope_field = NULL; |
| 1582 | |
| 1583 | if (notit->packet_header_field) { |
| 1584 | bt_packet_header_field_release(notit->packet_header_field); |
| 1585 | notit->packet_header_field = NULL; |
| 1586 | } |
| 1587 | |
| 1588 | if (notit->packet_context_field) { |
| 1589 | bt_packet_context_field_release(notit->packet_context_field); |
| 1590 | notit->packet_context_field = NULL; |
| 1591 | } |
| 1592 | |
| 1593 | if (notit->event_header_field) { |
| 1594 | bt_event_header_field_release(notit->event_header_field); |
| 1595 | notit->event_header_field = NULL; |
| 1596 | } |
| 1597 | |
| 1598 | notit->buf.addr = NULL; |
| 1599 | notit->buf.sz = 0; |
| 1600 | notit->buf.at = 0; |
| 1601 | notit->buf.last_eh_at = SIZE_MAX; |
| 1602 | notit->buf.packet_offset = 0; |
| 1603 | notit->state = STATE_INIT; |
| 1604 | notit->cur_exp_packet_content_size = -1; |
| 1605 | notit->cur_exp_packet_total_size = -1; |
| 1606 | notit->cur_packet_offset = -1; |
| 1607 | notit->cur_stream_class_id = -1; |
| 1608 | notit->cur_event_class_id = -1; |
| 1609 | notit->cur_data_stream_id = -1; |
| 1610 | notit->stream_begin_emitted = false; |
| 1611 | } |
| 1612 | |
| 1613 | static |
| 1614 | int bt_msg_iter_switch_packet(struct bt_msg_iter *notit) |
| 1615 | { |
| 1616 | int ret = 0; |
| 1617 | |
| 1618 | /* |
| 1619 | * We don't put the stream class here because we need to make |
| 1620 | * sure that all the packets processed by the same message |
| 1621 | * iterator refer to the same stream class (the first one). |
| 1622 | */ |
| 1623 | BT_ASSERT(notit); |
| 1624 | |
| 1625 | if (notit->cur_exp_packet_total_size != -1) { |
| 1626 | notit->cur_packet_offset += notit->cur_exp_packet_total_size; |
| 1627 | } |
| 1628 | |
| 1629 | BT_LOGV("Switching packet: notit-addr=%p, cur=%zu, " |
| 1630 | "packet-offset=%" PRId64, notit, notit->buf.at, |
| 1631 | notit->cur_packet_offset); |
| 1632 | stack_clear(notit->stack); |
| 1633 | notit->meta.ec = NULL; |
| 1634 | BT_PACKET_PUT_REF_AND_RESET(notit->packet); |
| 1635 | BT_MESSAGE_PUT_REF_AND_RESET(notit->event_msg); |
| 1636 | release_all_dscopes(notit); |
| 1637 | notit->cur_dscope_field = NULL; |
| 1638 | |
| 1639 | /* |
| 1640 | * Adjust current buffer so that addr points to the beginning of the new |
| 1641 | * packet. |
| 1642 | */ |
| 1643 | if (notit->buf.addr) { |
| 1644 | size_t consumed_bytes = (size_t) (notit->buf.at / CHAR_BIT); |
| 1645 | |
| 1646 | /* Packets are assumed to start on a byte frontier. */ |
| 1647 | if (notit->buf.at % CHAR_BIT) { |
| 1648 | BT_LOGW("Cannot switch packet: current position is not a multiple of 8: " |
| 1649 | "notit-addr=%p, cur=%zu", notit, notit->buf.at); |
| 1650 | ret = -1; |
| 1651 | goto end; |
| 1652 | } |
| 1653 | |
| 1654 | notit->buf.addr += consumed_bytes; |
| 1655 | notit->buf.sz -= consumed_bytes; |
| 1656 | notit->buf.at = 0; |
| 1657 | notit->buf.packet_offset = 0; |
| 1658 | BT_LOGV("Adjusted buffer: addr=%p, size=%zu", |
| 1659 | notit->buf.addr, notit->buf.sz); |
| 1660 | } |
| 1661 | |
| 1662 | notit->cur_exp_packet_content_size = -1; |
| 1663 | notit->cur_exp_packet_total_size = -1; |
| 1664 | notit->cur_stream_class_id = -1; |
| 1665 | notit->cur_event_class_id = -1; |
| 1666 | notit->cur_data_stream_id = -1; |
| 1667 | notit->snapshots.discarded_events = UINT64_C(-1); |
| 1668 | notit->snapshots.packets = UINT64_C(-1); |
| 1669 | notit->snapshots.beginning_clock = UINT64_C(-1); |
| 1670 | notit->snapshots.end_clock = UINT64_C(-1); |
| 1671 | |
| 1672 | end: |
| 1673 | return ret; |
| 1674 | } |
| 1675 | |
| 1676 | static |
| 1677 | bt_field *borrow_next_field(struct bt_msg_iter *notit) |
| 1678 | { |
| 1679 | bt_field *next_field = NULL; |
| 1680 | bt_field *base_field; |
| 1681 | const bt_field_class *base_fc; |
| 1682 | size_t index; |
| 1683 | |
| 1684 | BT_ASSERT(!stack_empty(notit->stack)); |
| 1685 | index = stack_top(notit->stack)->index; |
| 1686 | base_field = stack_top(notit->stack)->base; |
| 1687 | BT_ASSERT(base_field); |
| 1688 | base_fc = bt_field_borrow_class_const(base_field); |
| 1689 | BT_ASSERT(base_fc); |
| 1690 | |
| 1691 | switch (bt_field_class_get_type(base_fc)) { |
| 1692 | case BT_FIELD_CLASS_TYPE_STRUCTURE: |
| 1693 | { |
| 1694 | BT_ASSERT(index < |
| 1695 | bt_field_class_structure_get_member_count( |
| 1696 | bt_field_borrow_class_const( |
| 1697 | base_field))); |
| 1698 | next_field = |
| 1699 | bt_field_structure_borrow_member_field_by_index( |
| 1700 | base_field, index); |
| 1701 | break; |
| 1702 | } |
| 1703 | case BT_FIELD_CLASS_TYPE_STATIC_ARRAY: |
| 1704 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY: |
| 1705 | BT_ASSERT(index < bt_field_array_get_length(base_field)); |
| 1706 | next_field = bt_field_array_borrow_element_field_by_index( |
| 1707 | base_field, index); |
| 1708 | break; |
| 1709 | case BT_FIELD_CLASS_TYPE_VARIANT: |
| 1710 | BT_ASSERT(index == 0); |
| 1711 | next_field = bt_field_variant_borrow_selected_option_field( |
| 1712 | base_field); |
| 1713 | break; |
| 1714 | default: |
| 1715 | abort(); |
| 1716 | } |
| 1717 | |
| 1718 | BT_ASSERT(next_field); |
| 1719 | return next_field; |
| 1720 | } |
| 1721 | |
| 1722 | static |
| 1723 | void update_default_clock(struct bt_msg_iter *notit, uint64_t new_val, |
| 1724 | uint64_t new_val_size) |
| 1725 | { |
| 1726 | uint64_t new_val_mask; |
| 1727 | uint64_t cur_value_masked; |
| 1728 | |
| 1729 | BT_ASSERT(new_val_size > 0); |
| 1730 | |
| 1731 | /* |
| 1732 | * Special case for a 64-bit new value, which is the limit |
| 1733 | * of a clock value as of this version: overwrite the |
| 1734 | * current value directly. |
| 1735 | */ |
| 1736 | if (new_val_size == 64) { |
| 1737 | notit->default_clock_snapshot = new_val; |
| 1738 | goto end; |
| 1739 | } |
| 1740 | |
| 1741 | new_val_mask = (1ULL << new_val_size) - 1; |
| 1742 | cur_value_masked = notit->default_clock_snapshot & new_val_mask; |
| 1743 | |
| 1744 | if (new_val < cur_value_masked) { |
| 1745 | /* |
| 1746 | * It looks like a wrap happened on the number of bits |
| 1747 | * of the requested new value. Assume that the clock |
| 1748 | * value wrapped only one time. |
| 1749 | */ |
| 1750 | notit->default_clock_snapshot += new_val_mask + 1; |
| 1751 | } |
| 1752 | |
| 1753 | /* Clear the low bits of the current clock value. */ |
| 1754 | notit->default_clock_snapshot &= ~new_val_mask; |
| 1755 | |
| 1756 | /* Set the low bits of the current clock value. */ |
| 1757 | notit->default_clock_snapshot |= new_val; |
| 1758 | |
| 1759 | end: |
| 1760 | BT_LOGV("Updated default clock's value from integer field's value: " |
| 1761 | "value=%" PRIu64, notit->default_clock_snapshot); |
| 1762 | } |
| 1763 | |
| 1764 | static |
| 1765 | enum bt_bfcr_status bfcr_unsigned_int_cb(uint64_t value, |
| 1766 | struct ctf_field_class *fc, void *data) |
| 1767 | { |
| 1768 | struct bt_msg_iter *notit = data; |
| 1769 | enum bt_bfcr_status status = BT_BFCR_STATUS_OK; |
| 1770 | bt_field *field = NULL; |
| 1771 | struct ctf_field_class_int *int_fc = (void *) fc; |
| 1772 | |
| 1773 | BT_LOGV("Unsigned integer function called from BFCR: " |
| 1774 | "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, " |
| 1775 | "fc-type=%d, fc-in-ir=%d, value=%" PRIu64, |
| 1776 | notit, notit->bfcr, fc, fc->type, fc->in_ir, value); |
| 1777 | |
| 1778 | if (likely(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE)) { |
| 1779 | goto update_def_clock; |
| 1780 | } |
| 1781 | |
| 1782 | switch (int_fc->meaning) { |
| 1783 | case CTF_FIELD_CLASS_MEANING_EVENT_CLASS_ID: |
| 1784 | notit->cur_event_class_id = value; |
| 1785 | break; |
| 1786 | case CTF_FIELD_CLASS_MEANING_DATA_STREAM_ID: |
| 1787 | notit->cur_data_stream_id = value; |
| 1788 | break; |
| 1789 | case CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME: |
| 1790 | notit->snapshots.beginning_clock = value; |
| 1791 | break; |
| 1792 | case CTF_FIELD_CLASS_MEANING_PACKET_END_TIME: |
| 1793 | notit->snapshots.end_clock = value; |
| 1794 | break; |
| 1795 | case CTF_FIELD_CLASS_MEANING_STREAM_CLASS_ID: |
| 1796 | notit->cur_stream_class_id = value; |
| 1797 | break; |
| 1798 | case CTF_FIELD_CLASS_MEANING_MAGIC: |
| 1799 | if (value != 0xc1fc1fc1) { |
| 1800 | BT_LOGW("Invalid CTF magic number: notit-addr=%p, " |
| 1801 | "magic=%" PRIx64, notit, value); |
| 1802 | status = BT_BFCR_STATUS_ERROR; |
| 1803 | goto end; |
| 1804 | } |
| 1805 | |
| 1806 | break; |
| 1807 | case CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT: |
| 1808 | notit->snapshots.packets = value; |
| 1809 | break; |
| 1810 | case CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT: |
| 1811 | notit->snapshots.discarded_events = value; |
| 1812 | break; |
| 1813 | case CTF_FIELD_CLASS_MEANING_EXP_PACKET_TOTAL_SIZE: |
| 1814 | notit->cur_exp_packet_total_size = value; |
| 1815 | break; |
| 1816 | case CTF_FIELD_CLASS_MEANING_EXP_PACKET_CONTENT_SIZE: |
| 1817 | notit->cur_exp_packet_content_size = value; |
| 1818 | break; |
| 1819 | default: |
| 1820 | abort(); |
| 1821 | } |
| 1822 | |
| 1823 | update_def_clock: |
| 1824 | if (unlikely(int_fc->mapped_clock_class)) { |
| 1825 | update_default_clock(notit, value, int_fc->base.size); |
| 1826 | } |
| 1827 | |
| 1828 | if (unlikely(int_fc->storing_index >= 0)) { |
| 1829 | g_array_index(notit->stored_values, uint64_t, |
| 1830 | (uint64_t) int_fc->storing_index) = value; |
| 1831 | } |
| 1832 | |
| 1833 | if (unlikely(!fc->in_ir)) { |
| 1834 | goto end; |
| 1835 | } |
| 1836 | |
| 1837 | field = borrow_next_field(notit); |
| 1838 | BT_ASSERT(field); |
| 1839 | BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc); |
| 1840 | BT_ASSERT(bt_field_get_class_type(field) == |
| 1841 | BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER || |
| 1842 | bt_field_get_class_type(field) == |
| 1843 | BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION); |
| 1844 | bt_field_unsigned_integer_set_value(field, value); |
| 1845 | stack_top(notit->stack)->index++; |
| 1846 | |
| 1847 | end: |
| 1848 | return status; |
| 1849 | } |
| 1850 | |
| 1851 | static |
| 1852 | enum bt_bfcr_status bfcr_unsigned_int_char_cb(uint64_t value, |
| 1853 | struct ctf_field_class *fc, void *data) |
| 1854 | { |
| 1855 | int ret; |
| 1856 | struct bt_msg_iter *notit = data; |
| 1857 | enum bt_bfcr_status status = BT_BFCR_STATUS_OK; |
| 1858 | bt_field *string_field = NULL; |
| 1859 | struct ctf_field_class_int *int_fc = (void *) fc; |
| 1860 | char str[2] = {'\0', '\0'}; |
| 1861 | |
| 1862 | BT_LOGV("Unsigned integer character function called from BFCR: " |
| 1863 | "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, " |
| 1864 | "fc-type=%d, fc-in-ir=%d, value=%" PRIu64, |
| 1865 | notit, notit->bfcr, fc, fc->type, fc->in_ir, value); |
| 1866 | BT_ASSERT(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE); |
| 1867 | BT_ASSERT(!int_fc->mapped_clock_class); |
| 1868 | BT_ASSERT(int_fc->storing_index < 0); |
| 1869 | |
| 1870 | if (unlikely(!fc->in_ir)) { |
| 1871 | goto end; |
| 1872 | } |
| 1873 | |
| 1874 | if (notit->done_filling_string) { |
| 1875 | goto end; |
| 1876 | } |
| 1877 | |
| 1878 | if (value == 0) { |
| 1879 | notit->done_filling_string = true; |
| 1880 | goto end; |
| 1881 | } |
| 1882 | |
| 1883 | string_field = stack_top(notit->stack)->base; |
| 1884 | BT_ASSERT(bt_field_get_class_type(string_field) == |
| 1885 | BT_FIELD_CLASS_TYPE_STRING); |
| 1886 | |
| 1887 | /* Append character */ |
| 1888 | str[0] = (char) value; |
| 1889 | ret = bt_field_string_append_with_length(string_field, str, 1); |
| 1890 | if (ret) { |
| 1891 | BT_LOGE("Cannot append character to string field's value: " |
| 1892 | "notit-addr=%p, field-addr=%p, ret=%d", |
| 1893 | notit, string_field, ret); |
| 1894 | status = BT_BFCR_STATUS_ERROR; |
| 1895 | goto end; |
| 1896 | } |
| 1897 | |
| 1898 | end: |
| 1899 | return status; |
| 1900 | } |
| 1901 | |
| 1902 | static |
| 1903 | enum bt_bfcr_status bfcr_signed_int_cb(int64_t value, |
| 1904 | struct ctf_field_class *fc, void *data) |
| 1905 | { |
| 1906 | enum bt_bfcr_status status = BT_BFCR_STATUS_OK; |
| 1907 | bt_field *field = NULL; |
| 1908 | struct bt_msg_iter *notit = data; |
| 1909 | struct ctf_field_class_int *int_fc = (void *) fc; |
| 1910 | |
| 1911 | BT_LOGV("Signed integer function called from BFCR: " |
| 1912 | "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, " |
| 1913 | "fc-type=%d, fc-in-ir=%d, value=%" PRId64, |
| 1914 | notit, notit->bfcr, fc, fc->type, fc->in_ir, value); |
| 1915 | BT_ASSERT(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE); |
| 1916 | |
| 1917 | if (unlikely(int_fc->storing_index >= 0)) { |
| 1918 | g_array_index(notit->stored_values, uint64_t, |
| 1919 | (uint64_t) int_fc->storing_index) = (uint64_t) value; |
| 1920 | } |
| 1921 | |
| 1922 | if (unlikely(!fc->in_ir)) { |
| 1923 | goto end; |
| 1924 | } |
| 1925 | |
| 1926 | field = borrow_next_field(notit); |
| 1927 | BT_ASSERT(field); |
| 1928 | BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc); |
| 1929 | BT_ASSERT(bt_field_get_class_type(field) == |
| 1930 | BT_FIELD_CLASS_TYPE_SIGNED_INTEGER || |
| 1931 | bt_field_get_class_type(field) == |
| 1932 | BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION); |
| 1933 | bt_field_signed_integer_set_value(field, value); |
| 1934 | stack_top(notit->stack)->index++; |
| 1935 | |
| 1936 | end: |
| 1937 | return status; |
| 1938 | } |
| 1939 | |
| 1940 | static |
| 1941 | enum bt_bfcr_status bfcr_floating_point_cb(double value, |
| 1942 | struct ctf_field_class *fc, void *data) |
| 1943 | { |
| 1944 | enum bt_bfcr_status status = BT_BFCR_STATUS_OK; |
| 1945 | bt_field *field = NULL; |
| 1946 | struct bt_msg_iter *notit = data; |
| 1947 | |
| 1948 | BT_LOGV("Floating point number function called from BFCR: " |
| 1949 | "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, " |
| 1950 | "fc-type=%d, fc-in-ir=%d, value=%f", |
| 1951 | notit, notit->bfcr, fc, fc->type, fc->in_ir, value); |
| 1952 | |
| 1953 | if (unlikely(!fc->in_ir)) { |
| 1954 | goto end; |
| 1955 | } |
| 1956 | |
| 1957 | field = borrow_next_field(notit); |
| 1958 | BT_ASSERT(field); |
| 1959 | BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc); |
| 1960 | BT_ASSERT(bt_field_get_class_type(field) == |
| 1961 | BT_FIELD_CLASS_TYPE_REAL); |
| 1962 | bt_field_real_set_value(field, value); |
| 1963 | stack_top(notit->stack)->index++; |
| 1964 | |
| 1965 | end: |
| 1966 | return status; |
| 1967 | } |
| 1968 | |
| 1969 | static |
| 1970 | enum bt_bfcr_status bfcr_string_begin_cb( |
| 1971 | struct ctf_field_class *fc, void *data) |
| 1972 | { |
| 1973 | bt_field *field = NULL; |
| 1974 | struct bt_msg_iter *notit = data; |
| 1975 | int ret; |
| 1976 | |
| 1977 | BT_LOGV("String (beginning) function called from BFCR: " |
| 1978 | "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, " |
| 1979 | "fc-type=%d, fc-in-ir=%d", |
| 1980 | notit, notit->bfcr, fc, fc->type, fc->in_ir); |
| 1981 | |
| 1982 | if (unlikely(!fc->in_ir)) { |
| 1983 | goto end; |
| 1984 | } |
| 1985 | |
| 1986 | field = borrow_next_field(notit); |
| 1987 | BT_ASSERT(field); |
| 1988 | BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc); |
| 1989 | BT_ASSERT(bt_field_get_class_type(field) == |
| 1990 | BT_FIELD_CLASS_TYPE_STRING); |
| 1991 | ret = bt_field_string_clear(field); |
| 1992 | BT_ASSERT(ret == 0); |
| 1993 | |
| 1994 | /* |
| 1995 | * Push on stack. Not a compound class per se, but we know that |
| 1996 | * only bfcr_string_cb() may be called between this call and a |
| 1997 | * subsequent call to bfcr_string_end_cb(). |
| 1998 | */ |
| 1999 | stack_push(notit->stack, field); |
| 2000 | |
| 2001 | end: |
| 2002 | return BT_BFCR_STATUS_OK; |
| 2003 | } |
| 2004 | |
| 2005 | static |
| 2006 | enum bt_bfcr_status bfcr_string_cb(const char *value, |
| 2007 | size_t len, struct ctf_field_class *fc, void *data) |
| 2008 | { |
| 2009 | enum bt_bfcr_status status = BT_BFCR_STATUS_OK; |
| 2010 | bt_field *field = NULL; |
| 2011 | struct bt_msg_iter *notit = data; |
| 2012 | int ret; |
| 2013 | |
| 2014 | BT_LOGV("String (substring) function called from BFCR: " |
| 2015 | "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, " |
| 2016 | "fc-type=%d, fc-in-ir=%d, string-length=%zu", |
| 2017 | notit, notit->bfcr, fc, fc->type, fc->in_ir, |
| 2018 | len); |
| 2019 | |
| 2020 | if (unlikely(!fc->in_ir)) { |
| 2021 | goto end; |
| 2022 | } |
| 2023 | |
| 2024 | field = stack_top(notit->stack)->base; |
| 2025 | BT_ASSERT(field); |
| 2026 | |
| 2027 | /* Append current substring */ |
| 2028 | ret = bt_field_string_append_with_length(field, value, len); |
| 2029 | if (ret) { |
| 2030 | BT_LOGE("Cannot append substring to string field's value: " |
| 2031 | "notit-addr=%p, field-addr=%p, string-length=%zu, " |
| 2032 | "ret=%d", notit, field, len, ret); |
| 2033 | status = BT_BFCR_STATUS_ERROR; |
| 2034 | goto end; |
| 2035 | } |
| 2036 | |
| 2037 | end: |
| 2038 | return status; |
| 2039 | } |
| 2040 | |
| 2041 | static |
| 2042 | enum bt_bfcr_status bfcr_string_end_cb( |
| 2043 | struct ctf_field_class *fc, void *data) |
| 2044 | { |
| 2045 | struct bt_msg_iter *notit = data; |
| 2046 | |
| 2047 | BT_LOGV("String (end) function called from BFCR: " |
| 2048 | "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, " |
| 2049 | "fc-type=%d, fc-in-ir=%d", |
| 2050 | notit, notit->bfcr, fc, fc->type, fc->in_ir); |
| 2051 | |
| 2052 | if (unlikely(!fc->in_ir)) { |
| 2053 | goto end; |
| 2054 | } |
| 2055 | |
| 2056 | /* Pop string field */ |
| 2057 | stack_pop(notit->stack); |
| 2058 | |
| 2059 | /* Go to next field */ |
| 2060 | stack_top(notit->stack)->index++; |
| 2061 | |
| 2062 | end: |
| 2063 | return BT_BFCR_STATUS_OK; |
| 2064 | } |
| 2065 | |
| 2066 | enum bt_bfcr_status bfcr_compound_begin_cb( |
| 2067 | struct ctf_field_class *fc, void *data) |
| 2068 | { |
| 2069 | struct bt_msg_iter *notit = data; |
| 2070 | bt_field *field; |
| 2071 | |
| 2072 | BT_LOGV("Compound (beginning) function called from BFCR: " |
| 2073 | "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, " |
| 2074 | "fc-type=%d, fc-in-ir=%d", |
| 2075 | notit, notit->bfcr, fc, fc->type, fc->in_ir); |
| 2076 | |
| 2077 | if (!fc->in_ir) { |
| 2078 | goto end; |
| 2079 | } |
| 2080 | |
| 2081 | /* Borrow field */ |
| 2082 | if (stack_empty(notit->stack)) { |
| 2083 | /* Root: already set by read_dscope_begin_state() */ |
| 2084 | field = notit->cur_dscope_field; |
| 2085 | } else { |
| 2086 | field = borrow_next_field(notit); |
| 2087 | BT_ASSERT(field); |
| 2088 | } |
| 2089 | |
| 2090 | /* Push field */ |
| 2091 | BT_ASSERT(field); |
| 2092 | BT_ASSERT(bt_field_borrow_class_const(field) == fc->ir_fc); |
| 2093 | stack_push(notit->stack, field); |
| 2094 | |
| 2095 | /* |
| 2096 | * Change BFCR "unsigned int" callback if it's a text |
| 2097 | * array/sequence. |
| 2098 | */ |
| 2099 | if (fc->type == CTF_FIELD_CLASS_TYPE_ARRAY || |
| 2100 | fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE) { |
| 2101 | struct ctf_field_class_array_base *array_fc = (void *) fc; |
| 2102 | |
| 2103 | if (array_fc->is_text) { |
| 2104 | int ret; |
| 2105 | |
| 2106 | BT_ASSERT(bt_field_get_class_type(field) == |
| 2107 | BT_FIELD_CLASS_TYPE_STRING); |
| 2108 | notit->done_filling_string = false; |
| 2109 | ret = bt_field_string_clear(field); |
| 2110 | BT_ASSERT(ret == 0); |
| 2111 | bt_bfcr_set_unsigned_int_cb(notit->bfcr, |
| 2112 | bfcr_unsigned_int_char_cb); |
| 2113 | } |
| 2114 | } |
| 2115 | |
| 2116 | end: |
| 2117 | return BT_BFCR_STATUS_OK; |
| 2118 | } |
| 2119 | |
| 2120 | enum bt_bfcr_status bfcr_compound_end_cb( |
| 2121 | struct ctf_field_class *fc, void *data) |
| 2122 | { |
| 2123 | struct bt_msg_iter *notit = data; |
| 2124 | |
| 2125 | BT_LOGV("Compound (end) function called from BFCR: " |
| 2126 | "notit-addr=%p, bfcr-addr=%p, fc-addr=%p, " |
| 2127 | "fc-type=%d, fc-in-ir=%d", |
| 2128 | notit, notit->bfcr, fc, fc->type, fc->in_ir); |
| 2129 | |
| 2130 | if (!fc->in_ir) { |
| 2131 | goto end; |
| 2132 | } |
| 2133 | |
| 2134 | BT_ASSERT(!stack_empty(notit->stack)); |
| 2135 | BT_ASSERT(bt_field_borrow_class_const(stack_top(notit->stack)->base) == |
| 2136 | fc->ir_fc); |
| 2137 | |
| 2138 | /* |
| 2139 | * Reset BFCR "unsigned int" callback if it's a text |
| 2140 | * array/sequence. |
| 2141 | */ |
| 2142 | if (fc->type == CTF_FIELD_CLASS_TYPE_ARRAY || |
| 2143 | fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE) { |
| 2144 | struct ctf_field_class_array_base *array_fc = (void *) fc; |
| 2145 | |
| 2146 | if (array_fc->is_text) { |
| 2147 | BT_ASSERT(bt_field_get_class_type( |
| 2148 | stack_top(notit->stack)->base) == |
| 2149 | BT_FIELD_CLASS_TYPE_STRING); |
| 2150 | bt_bfcr_set_unsigned_int_cb(notit->bfcr, |
| 2151 | bfcr_unsigned_int_cb); |
| 2152 | } |
| 2153 | } |
| 2154 | |
| 2155 | /* Pop stack */ |
| 2156 | stack_pop(notit->stack); |
| 2157 | |
| 2158 | /* If the stack is not empty, increment the base's index */ |
| 2159 | if (!stack_empty(notit->stack)) { |
| 2160 | stack_top(notit->stack)->index++; |
| 2161 | } |
| 2162 | |
| 2163 | end: |
| 2164 | return BT_BFCR_STATUS_OK; |
| 2165 | } |
| 2166 | |
| 2167 | static |
| 2168 | int64_t bfcr_get_sequence_length_cb(struct ctf_field_class *fc, void *data) |
| 2169 | { |
| 2170 | bt_field *seq_field; |
| 2171 | struct bt_msg_iter *notit = data; |
| 2172 | struct ctf_field_class_sequence *seq_fc = (void *) fc; |
| 2173 | int64_t length = -1; |
| 2174 | int ret; |
| 2175 | |
| 2176 | length = (uint64_t) g_array_index(notit->stored_values, uint64_t, |
| 2177 | seq_fc->stored_length_index); |
| 2178 | seq_field = stack_top(notit->stack)->base; |
| 2179 | BT_ASSERT(seq_field); |
| 2180 | ret = bt_field_dynamic_array_set_length(seq_field, (uint64_t) length); |
| 2181 | if (ret) { |
| 2182 | BT_LOGE("Cannot set dynamic array field's length field: " |
| 2183 | "notit-addr=%p, field-addr=%p, " |
| 2184 | "length=%" PRIu64, notit, seq_field, length); |
| 2185 | } |
| 2186 | |
| 2187 | return length; |
| 2188 | } |
| 2189 | |
| 2190 | static |
| 2191 | struct ctf_field_class *bfcr_borrow_variant_selected_field_class_cb( |
| 2192 | struct ctf_field_class *fc, void *data) |
| 2193 | { |
| 2194 | int ret; |
| 2195 | uint64_t i; |
| 2196 | int64_t option_index = -1; |
| 2197 | struct bt_msg_iter *notit = data; |
| 2198 | struct ctf_field_class_variant *var_fc = (void *) fc; |
| 2199 | struct ctf_named_field_class *selected_option = NULL; |
| 2200 | struct ctf_field_class *ret_fc = NULL; |
| 2201 | union { |
| 2202 | uint64_t u; |
| 2203 | int64_t i; |
| 2204 | } tag; |
| 2205 | |
| 2206 | /* Get variant's tag */ |
| 2207 | tag.u = g_array_index(notit->stored_values, uint64_t, |
| 2208 | var_fc->stored_tag_index); |
| 2209 | |
| 2210 | /* |
| 2211 | * Check each range to find the selected option's index. |
| 2212 | */ |
| 2213 | if (var_fc->tag_fc->base.is_signed) { |
| 2214 | for (i = 0; i < var_fc->ranges->len; i++) { |
| 2215 | struct ctf_field_class_variant_range *range = |
| 2216 | ctf_field_class_variant_borrow_range_by_index( |
| 2217 | var_fc, i); |
| 2218 | |
| 2219 | if (tag.i >= range->range.lower.i && |
| 2220 | tag.i <= range->range.upper.i) { |
| 2221 | option_index = (int64_t) range->option_index; |
| 2222 | break; |
| 2223 | } |
| 2224 | } |
| 2225 | } else { |
| 2226 | for (i = 0; i < var_fc->ranges->len; i++) { |
| 2227 | struct ctf_field_class_variant_range *range = |
| 2228 | ctf_field_class_variant_borrow_range_by_index( |
| 2229 | var_fc, i); |
| 2230 | |
| 2231 | if (tag.u >= range->range.lower.u && |
| 2232 | tag.u <= range->range.upper.u) { |
| 2233 | option_index = (int64_t) range->option_index; |
| 2234 | break; |
| 2235 | } |
| 2236 | } |
| 2237 | } |
| 2238 | |
| 2239 | if (option_index < 0) { |
| 2240 | BT_LOGW("Cannot find variant field class's option: " |
| 2241 | "notit-addr=%p, var-fc-addr=%p, u-tag=%" PRIu64 ", " |
| 2242 | "i-tag=%" PRId64, notit, var_fc, tag.u, tag.i); |
| 2243 | goto end; |
| 2244 | } |
| 2245 | |
| 2246 | selected_option = ctf_field_class_variant_borrow_option_by_index( |
| 2247 | var_fc, (uint64_t) option_index); |
| 2248 | |
| 2249 | if (selected_option->fc->in_ir) { |
| 2250 | bt_field *var_field = stack_top(notit->stack)->base; |
| 2251 | |
| 2252 | ret = bt_field_variant_select_option_field( |
| 2253 | var_field, option_index); |
| 2254 | if (ret) { |
| 2255 | BT_LOGW("Cannot select variant field's option field: " |
| 2256 | "notit-addr=%p, var-field-addr=%p, " |
| 2257 | "opt-index=%" PRId64, notit, var_field, |
| 2258 | option_index); |
| 2259 | goto end; |
| 2260 | } |
| 2261 | } |
| 2262 | |
| 2263 | ret_fc = selected_option->fc; |
| 2264 | |
| 2265 | end: |
| 2266 | return ret_fc; |
| 2267 | } |
| 2268 | |
| 2269 | static |
| 2270 | void set_event_default_clock_snapshot(struct bt_msg_iter *notit) |
| 2271 | { |
| 2272 | bt_event *event = |
| 2273 | bt_message_event_borrow_event( |
| 2274 | notit->event_msg); |
| 2275 | bt_stream_class *sc = notit->meta.sc->ir_sc; |
| 2276 | |
| 2277 | BT_ASSERT(event); |
| 2278 | |
| 2279 | if (bt_stream_class_borrow_default_clock_class(sc)) { |
| 2280 | bt_event_set_default_clock_snapshot(event, |
| 2281 | notit->default_clock_snapshot); |
| 2282 | } |
| 2283 | } |
| 2284 | |
| 2285 | static |
| 2286 | void notify_new_stream(struct bt_msg_iter *notit, |
| 2287 | bt_message **message) |
| 2288 | { |
| 2289 | enum bt_msg_iter_status status; |
| 2290 | bt_message *ret = NULL; |
| 2291 | |
| 2292 | status = set_current_stream(notit); |
| 2293 | if (status != BT_MSG_ITER_STATUS_OK) { |
| 2294 | BT_MESSAGE_PUT_REF_AND_RESET(ret); |
| 2295 | goto end; |
| 2296 | } |
| 2297 | |
| 2298 | BT_ASSERT(notit->stream); |
| 2299 | BT_ASSERT(notit->msg_iter); |
| 2300 | ret = bt_message_stream_beginning_create(notit->msg_iter, |
| 2301 | notit->stream); |
| 2302 | if (!ret) { |
| 2303 | BT_LOGE("Cannot create stream beginning message: " |
| 2304 | "notit-addr=%p, stream-addr=%p", |
| 2305 | notit, notit->stream); |
| 2306 | return; |
| 2307 | } |
| 2308 | |
| 2309 | end: |
| 2310 | *message = ret; |
| 2311 | } |
| 2312 | |
| 2313 | static |
| 2314 | void notify_end_of_stream(struct bt_msg_iter *notit, |
| 2315 | bt_message **message) |
| 2316 | { |
| 2317 | bt_message *ret; |
| 2318 | |
| 2319 | if (!notit->stream) { |
| 2320 | BT_LOGE("Cannot create stream for stream message: " |
| 2321 | "notit-addr=%p", notit); |
| 2322 | return; |
| 2323 | } |
| 2324 | |
| 2325 | BT_ASSERT(notit->msg_iter); |
| 2326 | ret = bt_message_stream_end_create(notit->msg_iter, |
| 2327 | notit->stream); |
| 2328 | if (!ret) { |
| 2329 | BT_LOGE("Cannot create stream beginning message: " |
| 2330 | "notit-addr=%p, stream-addr=%p", |
| 2331 | notit, notit->stream); |
| 2332 | return; |
| 2333 | } |
| 2334 | *message = ret; |
| 2335 | } |
| 2336 | |
| 2337 | static |
| 2338 | void notify_new_packet(struct bt_msg_iter *notit, |
| 2339 | bt_message **message) |
| 2340 | { |
| 2341 | int ret; |
| 2342 | enum bt_msg_iter_status status; |
| 2343 | bt_message *msg = NULL; |
| 2344 | const bt_stream_class *sc; |
| 2345 | |
| 2346 | status = set_current_packet(notit); |
| 2347 | if (status != BT_MSG_ITER_STATUS_OK) { |
| 2348 | goto end; |
| 2349 | } |
| 2350 | |
| 2351 | BT_ASSERT(notit->packet); |
| 2352 | sc = notit->meta.sc->ir_sc; |
| 2353 | BT_ASSERT(sc); |
| 2354 | |
| 2355 | if (bt_stream_class_packets_have_discarded_event_counter_snapshot(sc)) { |
| 2356 | BT_ASSERT(notit->snapshots.discarded_events != UINT64_C(-1)); |
| 2357 | bt_packet_set_discarded_event_counter_snapshot( |
| 2358 | notit->packet, notit->snapshots.discarded_events); |
| 2359 | } |
| 2360 | |
| 2361 | if (bt_stream_class_packets_have_packet_counter_snapshot(sc)) { |
| 2362 | BT_ASSERT(notit->snapshots.packets != UINT64_C(-1)); |
| 2363 | bt_packet_set_packet_counter_snapshot( |
| 2364 | notit->packet, notit->snapshots.packets); |
| 2365 | } |
| 2366 | |
| 2367 | if (bt_stream_class_packets_have_default_beginning_clock_snapshot(sc)) { |
| 2368 | BT_ASSERT(notit->snapshots.beginning_clock != UINT64_C(-1)); |
| 2369 | bt_packet_set_default_beginning_clock_snapshot( |
| 2370 | notit->packet, notit->snapshots.beginning_clock); |
| 2371 | } |
| 2372 | |
| 2373 | if (bt_stream_class_packets_have_default_end_clock_snapshot(sc)) { |
| 2374 | BT_ASSERT(notit->snapshots.end_clock != UINT64_C(-1)); |
| 2375 | bt_packet_set_default_end_clock_snapshot( |
| 2376 | notit->packet, notit->snapshots.end_clock); |
| 2377 | } |
| 2378 | |
| 2379 | if (notit->packet_header_field) { |
| 2380 | ret = bt_packet_move_header_field( |
| 2381 | notit->packet, notit->packet_header_field); |
| 2382 | if (ret) { |
| 2383 | goto end; |
| 2384 | } |
| 2385 | |
| 2386 | notit->packet_header_field = NULL; |
| 2387 | |
| 2388 | /* |
| 2389 | * At this point notit->dscopes.trace_packet_header has |
| 2390 | * the same value as the packet header field within |
| 2391 | * notit->packet. |
| 2392 | */ |
| 2393 | BT_ASSERT(bt_packet_borrow_header_field( |
| 2394 | notit->packet) == |
| 2395 | notit->dscopes.trace_packet_header); |
| 2396 | } |
| 2397 | |
| 2398 | if (notit->packet_context_field) { |
| 2399 | ret = bt_packet_move_context_field( |
| 2400 | notit->packet, notit->packet_context_field); |
| 2401 | if (ret) { |
| 2402 | goto end; |
| 2403 | } |
| 2404 | |
| 2405 | notit->packet_context_field = NULL; |
| 2406 | |
| 2407 | /* |
| 2408 | * At this point notit->dscopes.trace_packet_header has |
| 2409 | * the same value as the packet header field within |
| 2410 | * notit->packet. |
| 2411 | */ |
| 2412 | BT_ASSERT(bt_packet_borrow_context_field( |
| 2413 | notit->packet) == |
| 2414 | notit->dscopes.stream_packet_context); |
| 2415 | } |
| 2416 | |
| 2417 | BT_ASSERT(notit->msg_iter); |
| 2418 | msg = bt_message_packet_beginning_create(notit->msg_iter, |
| 2419 | notit->packet); |
| 2420 | if (!msg) { |
| 2421 | BT_LOGE("Cannot create packet beginning message: " |
| 2422 | "notit-addr=%p, packet-addr=%p", |
| 2423 | notit, notit->packet); |
| 2424 | goto end; |
| 2425 | } |
| 2426 | |
| 2427 | *message = msg; |
| 2428 | |
| 2429 | end: |
| 2430 | return; |
| 2431 | } |
| 2432 | |
| 2433 | static |
| 2434 | void notify_end_of_packet(struct bt_msg_iter *notit, |
| 2435 | bt_message **message) |
| 2436 | { |
| 2437 | bt_message *msg; |
| 2438 | |
| 2439 | if (!notit->packet) { |
| 2440 | return; |
| 2441 | } |
| 2442 | |
| 2443 | /* Update default clock from packet's end time */ |
| 2444 | if (notit->snapshots.end_clock != UINT64_C(-1)) { |
| 2445 | notit->default_clock_snapshot = notit->snapshots.end_clock; |
| 2446 | } |
| 2447 | |
| 2448 | BT_ASSERT(notit->msg_iter); |
| 2449 | msg = bt_message_packet_end_create(notit->msg_iter, |
| 2450 | notit->packet); |
| 2451 | if (!msg) { |
| 2452 | BT_LOGE("Cannot create packet end message: " |
| 2453 | "notit-addr=%p, packet-addr=%p", |
| 2454 | notit, notit->packet); |
| 2455 | return; |
| 2456 | |
| 2457 | } |
| 2458 | |
| 2459 | BT_PACKET_PUT_REF_AND_RESET(notit->packet); |
| 2460 | *message = msg; |
| 2461 | } |
| 2462 | |
| 2463 | BT_HIDDEN |
| 2464 | struct bt_msg_iter *bt_msg_iter_create(struct ctf_trace_class *tc, |
| 2465 | size_t max_request_sz, |
| 2466 | struct bt_msg_iter_medium_ops medops, void *data) |
| 2467 | { |
| 2468 | struct bt_msg_iter *notit = NULL; |
| 2469 | struct bt_bfcr_cbs cbs = { |
| 2470 | .classes = { |
| 2471 | .signed_int = bfcr_signed_int_cb, |
| 2472 | .unsigned_int = bfcr_unsigned_int_cb, |
| 2473 | .floating_point = bfcr_floating_point_cb, |
| 2474 | .string_begin = bfcr_string_begin_cb, |
| 2475 | .string = bfcr_string_cb, |
| 2476 | .string_end = bfcr_string_end_cb, |
| 2477 | .compound_begin = bfcr_compound_begin_cb, |
| 2478 | .compound_end = bfcr_compound_end_cb, |
| 2479 | }, |
| 2480 | .query = { |
| 2481 | .get_sequence_length = bfcr_get_sequence_length_cb, |
| 2482 | .borrow_variant_selected_field_class = bfcr_borrow_variant_selected_field_class_cb, |
| 2483 | }, |
| 2484 | }; |
| 2485 | |
| 2486 | BT_ASSERT(tc); |
| 2487 | BT_ASSERT(medops.request_bytes); |
| 2488 | BT_ASSERT(medops.borrow_stream); |
| 2489 | BT_LOGD("Creating CTF plugin message iterator: " |
| 2490 | "trace-addr=%p, max-request-size=%zu, " |
| 2491 | "data=%p", tc, max_request_sz, data); |
| 2492 | notit = g_new0(struct bt_msg_iter, 1); |
| 2493 | if (!notit) { |
| 2494 | BT_LOGE_STR("Failed to allocate one CTF plugin message iterator."); |
| 2495 | goto end; |
| 2496 | } |
| 2497 | notit->meta.tc = tc; |
| 2498 | notit->medium.medops = medops; |
| 2499 | notit->medium.max_request_sz = max_request_sz; |
| 2500 | notit->medium.data = data; |
| 2501 | notit->stack = stack_new(notit); |
| 2502 | notit->stored_values = g_array_new(FALSE, TRUE, sizeof(uint64_t)); |
| 2503 | g_array_set_size(notit->stored_values, tc->stored_value_count); |
| 2504 | |
| 2505 | if (!notit->stack) { |
| 2506 | BT_LOGE_STR("Failed to create field stack."); |
| 2507 | goto error; |
| 2508 | } |
| 2509 | |
| 2510 | notit->bfcr = bt_bfcr_create(cbs, notit); |
| 2511 | if (!notit->bfcr) { |
| 2512 | BT_LOGE_STR("Failed to create binary class reader (BFCR)."); |
| 2513 | goto error; |
| 2514 | } |
| 2515 | |
| 2516 | bt_msg_iter_reset(notit); |
| 2517 | BT_LOGD("Created CTF plugin message iterator: " |
| 2518 | "trace-addr=%p, max-request-size=%zu, " |
| 2519 | "data=%p, notit-addr=%p", |
| 2520 | tc, max_request_sz, data, notit); |
| 2521 | notit->cur_packet_offset = 0; |
| 2522 | |
| 2523 | end: |
| 2524 | return notit; |
| 2525 | |
| 2526 | error: |
| 2527 | bt_msg_iter_destroy(notit); |
| 2528 | notit = NULL; |
| 2529 | goto end; |
| 2530 | } |
| 2531 | |
| 2532 | void bt_msg_iter_destroy(struct bt_msg_iter *notit) |
| 2533 | { |
| 2534 | BT_PACKET_PUT_REF_AND_RESET(notit->packet); |
| 2535 | BT_STREAM_PUT_REF_AND_RESET(notit->stream); |
| 2536 | release_all_dscopes(notit); |
| 2537 | |
| 2538 | BT_LOGD("Destroying CTF plugin message iterator: addr=%p", notit); |
| 2539 | |
| 2540 | if (notit->stack) { |
| 2541 | BT_LOGD_STR("Destroying field stack."); |
| 2542 | stack_destroy(notit->stack); |
| 2543 | } |
| 2544 | |
| 2545 | if (notit->bfcr) { |
| 2546 | BT_LOGD("Destroying BFCR: bfcr-addr=%p", notit->bfcr); |
| 2547 | bt_bfcr_destroy(notit->bfcr); |
| 2548 | } |
| 2549 | |
| 2550 | if (notit->stored_values) { |
| 2551 | g_array_free(notit->stored_values, TRUE); |
| 2552 | } |
| 2553 | |
| 2554 | g_free(notit); |
| 2555 | } |
| 2556 | |
| 2557 | enum bt_msg_iter_status bt_msg_iter_get_next_message( |
| 2558 | struct bt_msg_iter *notit, |
| 2559 | bt_self_message_iterator *msg_iter, |
| 2560 | bt_message **message) |
| 2561 | { |
| 2562 | enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK; |
| 2563 | |
| 2564 | BT_ASSERT(notit); |
| 2565 | BT_ASSERT(message); |
| 2566 | |
| 2567 | if (notit->state == STATE_DONE) { |
| 2568 | status = BT_MSG_ITER_STATUS_EOF; |
| 2569 | goto end; |
| 2570 | } |
| 2571 | |
| 2572 | notit->msg_iter = msg_iter; |
| 2573 | |
| 2574 | BT_LOGV("Getting next message: notit-addr=%p", notit); |
| 2575 | |
| 2576 | while (true) { |
| 2577 | status = handle_state(notit); |
| 2578 | if (status == BT_MSG_ITER_STATUS_AGAIN) { |
| 2579 | BT_LOGV_STR("Medium returned BT_MSG_ITER_STATUS_AGAIN."); |
| 2580 | goto end; |
| 2581 | } |
| 2582 | |
| 2583 | if (status != BT_MSG_ITER_STATUS_OK) { |
| 2584 | if (status == BT_MSG_ITER_STATUS_EOF) { |
| 2585 | enum state next_state = notit->state; |
| 2586 | |
| 2587 | BT_LOGV_STR("Medium returned BT_MSG_ITER_STATUS_EOF."); |
| 2588 | |
| 2589 | if (notit->packet) { |
| 2590 | notify_end_of_packet(notit, |
| 2591 | message); |
| 2592 | } else { |
| 2593 | notify_end_of_stream(notit, |
| 2594 | message); |
| 2595 | next_state = STATE_DONE; |
| 2596 | } |
| 2597 | |
| 2598 | if (!*message) { |
| 2599 | status = BT_MSG_ITER_STATUS_ERROR; |
| 2600 | goto end; |
| 2601 | } |
| 2602 | |
| 2603 | status = BT_MSG_ITER_STATUS_OK; |
| 2604 | notit->state = next_state; |
| 2605 | } else { |
| 2606 | BT_LOGW("Cannot handle state: " |
| 2607 | "notit-addr=%p, state=%s", |
| 2608 | notit, state_string(notit->state)); |
| 2609 | } |
| 2610 | |
| 2611 | goto end; |
| 2612 | } |
| 2613 | |
| 2614 | switch (notit->state) { |
| 2615 | case STATE_EMIT_MSG_NEW_STREAM: |
| 2616 | /* notify_new_stream() logs errors */ |
| 2617 | notify_new_stream(notit, message); |
| 2618 | |
| 2619 | if (!*message) { |
| 2620 | status = BT_MSG_ITER_STATUS_ERROR; |
| 2621 | } |
| 2622 | |
| 2623 | notit->stream_begin_emitted = true; |
| 2624 | goto end; |
| 2625 | case STATE_EMIT_MSG_NEW_PACKET: |
| 2626 | /* notify_new_packet() logs errors */ |
| 2627 | notify_new_packet(notit, message); |
| 2628 | |
| 2629 | if (!*message) { |
| 2630 | status = BT_MSG_ITER_STATUS_ERROR; |
| 2631 | } |
| 2632 | |
| 2633 | goto end; |
| 2634 | case STATE_EMIT_MSG_EVENT: |
| 2635 | BT_ASSERT(notit->event_msg); |
| 2636 | set_event_default_clock_snapshot(notit); |
| 2637 | *message = notit->event_msg; |
| 2638 | notit->event_msg = NULL; |
| 2639 | goto end; |
| 2640 | case STATE_EMIT_MSG_END_OF_PACKET: |
| 2641 | /* notify_end_of_packet() logs errors */ |
| 2642 | notify_end_of_packet(notit, message); |
| 2643 | |
| 2644 | if (!*message) { |
| 2645 | status = BT_MSG_ITER_STATUS_ERROR; |
| 2646 | } |
| 2647 | |
| 2648 | goto end; |
| 2649 | default: |
| 2650 | /* Non-emitting state: continue */ |
| 2651 | break; |
| 2652 | } |
| 2653 | } |
| 2654 | |
| 2655 | end: |
| 2656 | return status; |
| 2657 | } |
| 2658 | |
| 2659 | static |
| 2660 | enum bt_msg_iter_status read_packet_header_context_fields( |
| 2661 | struct bt_msg_iter *notit) |
| 2662 | { |
| 2663 | int ret; |
| 2664 | enum bt_msg_iter_status status = BT_MSG_ITER_STATUS_OK; |
| 2665 | |
| 2666 | BT_ASSERT(notit); |
| 2667 | |
| 2668 | if (notit->state == STATE_EMIT_MSG_NEW_PACKET) { |
| 2669 | /* We're already there */ |
| 2670 | goto end; |
| 2671 | } |
| 2672 | |
| 2673 | while (true) { |
| 2674 | status = handle_state(notit); |
| 2675 | if (status == BT_MSG_ITER_STATUS_AGAIN) { |
| 2676 | BT_LOGV_STR("Medium returned BT_MSG_ITER_STATUS_AGAIN."); |
| 2677 | goto end; |
| 2678 | } |
| 2679 | if (status != BT_MSG_ITER_STATUS_OK) { |
| 2680 | if (status == BT_MSG_ITER_STATUS_EOF) { |
| 2681 | BT_LOGV_STR("Medium returned BT_MSG_ITER_STATUS_EOF."); |
| 2682 | } else { |
| 2683 | BT_LOGW("Cannot handle state: " |
| 2684 | "notit-addr=%p, state=%s", |
| 2685 | notit, state_string(notit->state)); |
| 2686 | } |
| 2687 | goto end; |
| 2688 | } |
| 2689 | |
| 2690 | switch (notit->state) { |
| 2691 | case STATE_EMIT_MSG_NEW_PACKET: |
| 2692 | /* |
| 2693 | * Packet header and context fields are |
| 2694 | * potentially decoded (or they don't exist). |
| 2695 | */ |
| 2696 | goto end; |
| 2697 | case STATE_INIT: |
| 2698 | case STATE_EMIT_MSG_NEW_STREAM: |
| 2699 | case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN: |
| 2700 | case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE: |
| 2701 | case STATE_AFTER_TRACE_PACKET_HEADER: |
| 2702 | case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN: |
| 2703 | case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE: |
| 2704 | case STATE_AFTER_STREAM_PACKET_CONTEXT: |
| 2705 | /* Non-emitting state: continue */ |
| 2706 | break; |
| 2707 | default: |
| 2708 | /* |
| 2709 | * We should never get past the |
| 2710 | * STATE_EMIT_MSG_NEW_PACKET state. |
| 2711 | */ |
| 2712 | BT_LOGF("Unexpected state: notit-addr=%p, state=%s", |
| 2713 | notit, state_string(notit->state)); |
| 2714 | abort(); |
| 2715 | } |
| 2716 | } |
| 2717 | |
| 2718 | end: |
| 2719 | ret = set_current_packet_content_sizes(notit); |
| 2720 | if (ret) { |
| 2721 | status = BT_MSG_ITER_STATUS_ERROR; |
| 2722 | goto end; |
| 2723 | } |
| 2724 | |
| 2725 | return status; |
| 2726 | } |
| 2727 | |
| 2728 | BT_HIDDEN |
| 2729 | void bt_msg_iter_set_medops_data(struct bt_msg_iter *notit, |
| 2730 | void *medops_data) |
| 2731 | { |
| 2732 | BT_ASSERT(notit); |
| 2733 | notit->medium.data = medops_data; |
| 2734 | } |
| 2735 | |
| 2736 | BT_HIDDEN |
| 2737 | enum bt_msg_iter_status bt_msg_iter_seek( |
| 2738 | struct bt_msg_iter *notit, off_t offset) |
| 2739 | { |
| 2740 | enum bt_msg_iter_status ret = BT_MSG_ITER_STATUS_OK; |
| 2741 | enum bt_msg_iter_medium_status medium_status; |
| 2742 | |
| 2743 | BT_ASSERT(notit); |
| 2744 | if (offset < 0) { |
| 2745 | BT_LOGE("Cannot seek to negative offset: offset=%jd", offset); |
| 2746 | ret = BT_MSG_ITER_STATUS_INVAL; |
| 2747 | goto end; |
| 2748 | } |
| 2749 | |
| 2750 | if (!notit->medium.medops.seek) { |
| 2751 | ret = BT_MSG_ITER_STATUS_UNSUPPORTED; |
| 2752 | BT_LOGD("Aborting seek as the iterator's underlying media does not implement seek support."); |
| 2753 | goto end; |
| 2754 | } |
| 2755 | |
| 2756 | medium_status = notit->medium.medops.seek( |
| 2757 | BT_MSG_ITER_SEEK_WHENCE_SET, offset, notit->medium.data); |
| 2758 | if (medium_status != BT_MSG_ITER_MEDIUM_STATUS_OK) { |
| 2759 | if (medium_status == BT_MSG_ITER_MEDIUM_STATUS_EOF) { |
| 2760 | ret = BT_MSG_ITER_STATUS_EOF; |
| 2761 | } else { |
| 2762 | ret = BT_MSG_ITER_STATUS_ERROR; |
| 2763 | goto end; |
| 2764 | } |
| 2765 | } |
| 2766 | |
| 2767 | bt_msg_iter_reset(notit); |
| 2768 | notit->cur_packet_offset = offset; |
| 2769 | |
| 2770 | end: |
| 2771 | return ret; |
| 2772 | } |
| 2773 | |
| 2774 | BT_HIDDEN |
| 2775 | off_t bt_msg_iter_get_current_packet_offset(struct bt_msg_iter *notit) |
| 2776 | { |
| 2777 | BT_ASSERT(notit); |
| 2778 | return notit->cur_packet_offset; |
| 2779 | } |
| 2780 | |
| 2781 | BT_HIDDEN |
| 2782 | off_t bt_msg_iter_get_current_packet_size( |
| 2783 | struct bt_msg_iter *notit) |
| 2784 | { |
| 2785 | BT_ASSERT(notit); |
| 2786 | return notit->cur_exp_packet_total_size; |
| 2787 | } |
| 2788 | |
| 2789 | BT_HIDDEN |
| 2790 | void bt_msg_trace_class_changed(struct bt_msg_iter *notit) |
| 2791 | { |
| 2792 | if (notit->meta.tc->stored_value_count > notit->stored_values->len) { |
| 2793 | g_array_set_size(notit->stored_values, |
| 2794 | notit->meta.tc->stored_value_count); |
| 2795 | } |
| 2796 | } |
| 2797 | |
| 2798 | BT_HIDDEN |
| 2799 | enum bt_msg_iter_status bt_msg_iter_get_packet_properties( |
| 2800 | struct bt_msg_iter *notit, |
| 2801 | struct bt_msg_iter_packet_properties *props) |
| 2802 | { |
| 2803 | enum bt_msg_iter_status status; |
| 2804 | |
| 2805 | BT_ASSERT(notit); |
| 2806 | BT_ASSERT(props); |
| 2807 | status = read_packet_header_context_fields(notit); |
| 2808 | if (status != BT_MSG_ITER_STATUS_OK) { |
| 2809 | goto end; |
| 2810 | } |
| 2811 | |
| 2812 | props->exp_packet_total_size = |
| 2813 | (uint64_t) notit->cur_exp_packet_total_size; |
| 2814 | props->exp_packet_content_size = |
| 2815 | (uint64_t) notit->cur_exp_packet_content_size; |
| 2816 | BT_ASSERT(props->stream_class_id >= 0); |
| 2817 | props->stream_class_id = (uint64_t) notit->cur_stream_class_id; |
| 2818 | props->data_stream_id = notit->cur_data_stream_id; |
| 2819 | props->snapshots.discarded_events = notit->snapshots.discarded_events; |
| 2820 | props->snapshots.packets = notit->snapshots.packets; |
| 2821 | props->snapshots.beginning_clock = notit->snapshots.beginning_clock; |
| 2822 | props->snapshots.end_clock = notit->snapshots.end_clock; |
| 2823 | |
| 2824 | end: |
| 2825 | return status; |
| 2826 | } |