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