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