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