X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=blobdiff_plain;f=plugins%2Fctf%2Fcommon%2Fnotif-iter%2Fnotif-iter.c;h=5927285901469f46a8153aef62ad4800202b7b91;hp=bc20f8814b1de423158fa04961a9134d42f4a599;hb=44c440bc5fe8219cc17d1b786d91fd83c4c9860a;hpb=7cdc2bab17acd56d035b204518ef845fa5a9f1c7 diff --git a/plugins/ctf/common/notif-iter/notif-iter.c b/plugins/ctf/common/notif-iter/notif-iter.c index bc20f881..59272859 100644 --- a/plugins/ctf/common/notif-iter/notif-iter.c +++ b/plugins/ctf/common/notif-iter/notif-iter.c @@ -1,8 +1,8 @@ /* * Babeltrace - CTF notification iterator * - * Copyright (c) 2015-2016 EfficiOS Inc. and Linux Foundation - * Copyright (c) 2015-2016 Philippe Proulx + * Copyright (c) 2015-2018 EfficiOS Inc. and Linux Foundation + * Copyright (c) 2015-2018 Philippe Proulx * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -23,38 +23,25 @@ * SOFTWARE. */ +#define BT_LOG_TAG "PLUGIN-CTF-NOTIF-ITER" +#include "logging.h" + #include #include #include #include #include -#include +#include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include #include - -#define PRINT_ERR_STREAM notit->err_stream -#define PRINT_PREFIX "ctf-notif-iter" -#include "../print.h" +#include #include "notif-iter.h" #include "../btr/btr.h" -#define BYTES_TO_BITS(x) ((x) * 8) - -struct bt_ctf_notif_iter; +struct bt_notif_iter; /* A visit stack entry */ struct stack_entry { @@ -67,18 +54,21 @@ struct stack_entry { * * sequence * * variant * - * Field is owned by this. + * Field is borrowed. */ - struct bt_ctf_field *base; + struct bt_field *base; - /* index of next field to set */ + /* Index of next field to set */ size_t index; }; /* Visit stack */ struct stack { - /* Entries (struct stack_entry *) (top is last element) */ - GPtrArray *entries; + /* Entries (struct stack_entry) */ + GArray *entries; + + /* Number of active entries */ + size_t size; }; /* State */ @@ -90,111 +80,83 @@ enum state { STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN, STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE, STATE_AFTER_STREAM_PACKET_CONTEXT, + STATE_EMIT_NOTIF_NEW_STREAM, STATE_EMIT_NOTIF_NEW_PACKET, - STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN, - STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE, - STATE_AFTER_STREAM_EVENT_HEADER, - STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN, - STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE, - STATE_DSCOPE_EVENT_CONTEXT_BEGIN, - STATE_DSCOPE_EVENT_CONTEXT_CONTINUE, + STATE_DSCOPE_EVENT_HEADER_BEGIN, + STATE_DSCOPE_EVENT_HEADER_CONTINUE, + STATE_AFTER_EVENT_HEADER, + STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN, + STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE, + STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN, + STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE, STATE_DSCOPE_EVENT_PAYLOAD_BEGIN, STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE, STATE_EMIT_NOTIF_EVENT, STATE_EMIT_NOTIF_END_OF_PACKET, + STATE_DONE, STATE_SKIP_PACKET_PADDING, }; -struct trace_field_path_cache { - /* - * Indexes of the stream_id and stream_instance_id field in the packet - * header structure, -1 if unset. - */ - int stream_id; - int stream_instance_id; -}; - -struct stream_class_field_path_cache { - /* - * Indexes of the v and id fields in the stream event header structure, - * -1 if unset. - */ - int v; - int id; - - /* - * index of the timestamp_end, packet_size and content_size fields in - * the stream packet context structure. Set to -1 if the fields were - * not found. - */ - int timestamp_end; - int packet_size; - int content_size; -}; - -struct field_cb_override { - enum bt_ctf_btr_status (* func)(void *value, - struct bt_ctf_field_type *type, void *data); - void *data; -}; - /* CTF notification iterator */ -struct bt_ctf_notif_iter { +struct bt_notif_iter { /* Visit stack */ struct stack *stack; - /* Error stream (may be NULL) */ - FILE *err_stream; + /* Current notification iterator to create notifications (weak) */ + struct bt_private_connection_private_notification_iterator *notif_iter; /* * Current dynamic scope field pointer. * - * This is set when a dynamic scope field is first created by - * btr_compound_begin_cb(). It points to one of the fields in - * dscopes below. + * This is set by read_dscope_begin_state() and contains the + * value of one of the pointers in `dscopes` below. + */ + struct bt_field *cur_dscope_field; + + /* + * True if we're done filling a string field from a text + * array/sequence payload. */ - struct bt_ctf_field **cur_dscope_field; + bool done_filling_string; - /* Trace and classes (owned by this) */ + /* Trace and classes */ struct { - struct bt_ctf_trace *trace; - struct bt_ctf_stream_class *stream_class; - struct bt_ctf_event_class *event_class; + struct ctf_trace_class *tc; + struct ctf_stream_class *sc; + struct ctf_event_class *ec; } meta; + /* Current packet header field wrapper (NULL if not created yet) */ + struct bt_packet_header_field *packet_header_field; + + /* Current packet header field wrapper (NULL if not created yet) */ + struct bt_packet_context_field *packet_context_field; + + /* Current event header field (NULL if not created yet) */ + struct bt_event_header_field *event_header_field; + /* Current packet (NULL if not created yet) */ - struct bt_ctf_packet *packet; + struct bt_packet *packet; - /* - * Current timestamp_end field (to consider before switching packets). - */ - struct bt_ctf_field *cur_timestamp_end; + /* Current stream (NULL if not set yet) */ + struct bt_stream *stream; + + /* Current event (NULL if not created yet) */ + struct bt_event *event; - /* Database of current dynamic scopes (owned by this) */ + /* Current event notification (NULL if not created yet) */ + struct bt_notification *event_notif; + + /* Database of current dynamic scopes */ struct { - struct bt_ctf_field *trace_packet_header; - struct bt_ctf_field *stream_packet_context; - struct bt_ctf_field *stream_event_header; - struct bt_ctf_field *stream_event_context; - struct bt_ctf_field *event_context; - struct bt_ctf_field *event_payload; + struct bt_field *trace_packet_header; + struct bt_field *stream_packet_context; + struct bt_field *event_header; + struct bt_field *event_common_context; + struct bt_field *event_spec_context; + struct bt_field *event_payload; } dscopes; - /* - * Special field overrides. - * - * Overrides are used to implement the behaviours of special fields such - * as "timestamp_end" (which must be ignored until the end of the - * packet), "id" (event id) which can be present multiple times and must - * be updated multiple time. - * - * This should be used to implement the behaviour of integer fields - * mapped to clocks and other "tagged" fields (in CTF 2). - * - * bt_ctf_field_type to struct field_cb_override - */ - GHashTable *field_overrides; - /* Current state */ enum state state; @@ -211,132 +173,203 @@ struct bt_ctf_notif_iter { /* Current position from addr (bits) */ size_t at; + + /* Position of the last event header from addr (bits) */ + size_t last_eh_at; } buf; /* Binary type reader */ - struct bt_ctf_btr *btr; + struct bt_btr *btr; /* Current medium data */ struct { - struct bt_ctf_notif_iter_medium_ops medops; + struct bt_notif_iter_medium_ops medops; size_t max_request_sz; void *data; } medium; + /* Stream beginning was emitted */ + bool stream_begin_emitted; + /* Current packet size (bits) (-1 if unknown) */ - int64_t cur_packet_size; + int64_t cur_exp_packet_total_size; /* Current content size (bits) (-1 if unknown) */ - int64_t cur_content_size; + int64_t cur_exp_packet_content_size; - /* bt_ctf_clock_class to uint64_t. */ - GHashTable *clock_states; + /* Current stream class ID */ + int64_t cur_stream_class_id; - /* - * Cache of the trace-constant field paths (event header type) - * associated to the current trace. - */ - struct trace_field_path_cache trace_field_path_cache; + /* Current event class ID */ + int64_t cur_event_class_id; + + /* Current data stream ID */ + int64_t cur_data_stream_id; /* - * Field path cache associated with the current stream class. - * Ownership of this structure belongs to the field_path_caches HT. + * Offset, in the underlying media, of the current packet's + * start (-1 if unknown). */ - struct stream_class_field_path_cache *cur_sc_field_path_cache; - - /* bt_ctf_stream_class to struct stream_class_field_path_cache. */ - GHashTable *sc_field_path_caches; -}; + off_t cur_packet_offset; -static -int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter *notit); + /* Default clock's current value */ + uint64_t default_clock_val; -static -enum bt_ctf_btr_status btr_timestamp_end_cb(void *value, - struct bt_ctf_field_type *type, void *data); + /* End of packet snapshots */ + struct { + uint64_t discarded_events; + uint64_t packets; + uint64_t beginning_clock; + uint64_t end_clock; + } snapshots; + + /* Stored values (for sequence lengths, variant tags) */ + GArray *stored_values; +}; -static -void stack_entry_free_func(gpointer data) +static inline +const char *state_string(enum state state) { - struct stack_entry *entry = data; - - bt_put(entry->base); - g_free(entry); + switch (state) { + case STATE_INIT: + return "STATE_INIT"; + case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN: + return "STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN"; + case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE: + return "STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE"; + case STATE_AFTER_TRACE_PACKET_HEADER: + return "STATE_AFTER_TRACE_PACKET_HEADER"; + case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN: + return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN"; + case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE: + return "STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE"; + case STATE_AFTER_STREAM_PACKET_CONTEXT: + return "STATE_AFTER_STREAM_PACKET_CONTEXT"; + case STATE_EMIT_NOTIF_NEW_PACKET: + return "STATE_EMIT_NOTIF_NEW_PACKET"; + case STATE_EMIT_NOTIF_NEW_STREAM: + return "STATE_EMIT_NOTIF_NEW_STREAM"; + case STATE_DSCOPE_EVENT_HEADER_BEGIN: + return "STATE_DSCOPE_EVENT_HEADER_BEGIN"; + case STATE_DSCOPE_EVENT_HEADER_CONTINUE: + return "STATE_DSCOPE_EVENT_HEADER_CONTINUE"; + case STATE_AFTER_EVENT_HEADER: + return "STATE_AFTER_EVENT_HEADER"; + case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN: + return "STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN"; + case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE: + return "STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE"; + case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN: + return "STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN"; + case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE: + return "STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE"; + case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN: + return "STATE_DSCOPE_EVENT_PAYLOAD_BEGIN"; + case STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE: + return "STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE"; + case STATE_EMIT_NOTIF_EVENT: + return "STATE_EMIT_NOTIF_EVENT"; + case STATE_EMIT_NOTIF_END_OF_PACKET: + return "STATE_EMIT_NOTIF_END_OF_PACKET"; + case STATE_DONE: + return "STATE_DONE"; + case STATE_SKIP_PACKET_PADDING: + return "STATE_SKIP_PACKET_PADDING"; + default: + return "(unknown)"; + } } static -struct stack *stack_new(struct bt_ctf_notif_iter *notit) +int bt_notif_iter_switch_packet(struct bt_notif_iter *notit); + +static +struct stack *stack_new(struct bt_notif_iter *notit) { struct stack *stack = NULL; stack = g_new0(struct stack, 1); if (!stack) { + BT_LOGE_STR("Failed to allocate one stack."); goto error; } - stack->entries = g_ptr_array_new_with_free_func(stack_entry_free_func); + stack->entries = g_array_new(FALSE, TRUE, sizeof(struct stack_entry)); if (!stack->entries) { + BT_LOGE_STR("Failed to allocate a GArray."); goto error; } - return stack; + BT_LOGD("Created stack: notit-addr=%p, stack-addr=%p", notit, stack); + goto end; + error: g_free(stack); - return NULL; + stack = NULL; + +end: + return stack; } static void stack_destroy(struct stack *stack) { - assert(stack); - g_ptr_array_free(stack->entries, TRUE); + BT_ASSERT(stack); + BT_LOGD("Destroying stack: addr=%p", stack); + + if (stack->entries) { + g_array_free(stack->entries, TRUE); + } + g_free(stack); } static -int stack_push(struct stack *stack, struct bt_ctf_field *base) +void stack_push(struct stack *stack, struct bt_field *base) { - int ret = 0; struct stack_entry *entry; - assert(stack); - assert(base); - entry = g_new0(struct stack_entry, 1); - if (!entry) { - ret = -1; - goto end; - } + BT_ASSERT(stack); + BT_ASSERT(base); + BT_LOGV("Pushing base field on stack: stack-addr=%p, " + "stack-size-before=%zu, stack-size-after=%zu", + stack, stack->size, stack->size + 1); - entry->base = bt_get(base); - g_ptr_array_add(stack->entries, entry); + if (stack->entries->len == stack->size) { + g_array_set_size(stack->entries, stack->size + 1); + } -end: - return ret; + entry = &g_array_index(stack->entries, struct stack_entry, stack->size); + entry->base = base; + entry->index = 0; + stack->size++; } static inline unsigned int stack_size(struct stack *stack) { - assert(stack); - - return stack->entries->len; + BT_ASSERT(stack); + return stack->size; } static void stack_pop(struct stack *stack) { - assert(stack); - assert(stack_size(stack)); - g_ptr_array_remove_index(stack->entries, stack->entries->len - 1); + BT_ASSERT(stack); + BT_ASSERT(stack_size(stack)); + BT_LOGV("Popping from stack: " + "stack-addr=%p, stack-size-before=%zu, stack-size-after=%zu", + stack, stack->size, stack->size - 1); + stack->size--; } static inline struct stack_entry *stack_top(struct stack *stack) { - assert(stack); - assert(stack_size(stack)); - - return g_ptr_array_index(stack->entries, stack->entries->len - 1); + BT_ASSERT(stack); + BT_ASSERT(stack_size(stack)); + return &g_array_index(stack->entries, struct stack_entry, + stack->size - 1); } static inline @@ -348,85 +381,132 @@ bool stack_empty(struct stack *stack) static void stack_clear(struct stack *stack) { - assert(stack); - - if (!stack_empty(stack)) { - g_ptr_array_remove_range(stack->entries, 0, stack_size(stack)); - } - - assert(stack_empty(stack)); + BT_ASSERT(stack); + stack->size = 0; } static inline -enum bt_ctf_notif_iter_status notif_iter_status_from_m_status( - enum bt_ctf_notif_iter_medium_status m_status) +enum bt_notif_iter_status notif_iter_status_from_m_status( + enum bt_notif_iter_medium_status m_status) { + /* They are the same */ return (int) m_status; } static inline -size_t buf_size_bits(struct bt_ctf_notif_iter *notit) +size_t buf_size_bits(struct bt_notif_iter *notit) { - return BYTES_TO_BITS(notit->buf.sz); + return notit->buf.sz * 8; } static inline -size_t buf_available_bits(struct bt_ctf_notif_iter *notit) +size_t buf_available_bits(struct bt_notif_iter *notit) { return buf_size_bits(notit) - notit->buf.at; } static inline -size_t packet_at(struct bt_ctf_notif_iter *notit) +size_t packet_at(struct bt_notif_iter *notit) { return notit->buf.packet_offset + notit->buf.at; } static inline -void buf_consume_bits(struct bt_ctf_notif_iter *notit, size_t incr) +void buf_consume_bits(struct bt_notif_iter *notit, size_t incr) { + BT_LOGV("Advancing cursor: notit-addr=%p, cur-before=%zu, cur-after=%zu", + notit, notit->buf.at, notit->buf.at + incr); notit->buf.at += incr; } static -enum bt_ctf_notif_iter_status request_medium_bytes( - struct bt_ctf_notif_iter *notit) +enum bt_notif_iter_status request_medium_bytes( + struct bt_notif_iter *notit) { - uint8_t *buffer_addr; - size_t buffer_sz; - enum bt_ctf_notif_iter_medium_status m_status; + uint8_t *buffer_addr = NULL; + size_t buffer_sz = 0; + enum bt_notif_iter_medium_status m_status; + BT_LOGV("Calling user function (request bytes): notit-addr=%p, " + "request-size=%zu", notit, notit->medium.max_request_sz); m_status = notit->medium.medops.request_bytes( notit->medium.max_request_sz, &buffer_addr, &buffer_sz, notit->medium.data); - if (m_status == BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK) { - assert(buffer_sz != 0); + BT_LOGV("User function returned: status=%s, buf-addr=%p, buf-size=%zu", + bt_notif_iter_medium_status_string(m_status), + buffer_addr, buffer_sz); + if (m_status == BT_NOTIF_ITER_MEDIUM_STATUS_OK) { + BT_ASSERT(buffer_sz != 0); /* New packet offset is old one + old size (in bits) */ notit->buf.packet_offset += buf_size_bits(notit); /* Restart at the beginning of the new medium buffer */ notit->buf.at = 0; + notit->buf.last_eh_at = SIZE_MAX; /* New medium buffer size */ notit->buf.sz = buffer_sz; /* New medium buffer address */ notit->buf.addr = buffer_addr; + + BT_LOGV("User function returned new bytes: " + "packet-offset=%zu, cur=%zu, size=%zu, addr=%p", + notit->buf.packet_offset, notit->buf.at, + notit->buf.sz, notit->buf.addr); + BT_LOGV_MEM(buffer_addr, buffer_sz, "Returned bytes at %p:", + buffer_addr); + } else if (m_status == BT_NOTIF_ITER_MEDIUM_STATUS_EOF) { + /* + * User returned end of stream: validate that we're not + * in the middle of a packet header, packet context, or + * event. + */ + if (notit->cur_exp_packet_total_size >= 0) { + if (packet_at(notit) == + notit->cur_exp_packet_total_size) { + goto end; + } + } else { + if (packet_at(notit) == 0) { + goto end; + } + + if (notit->buf.last_eh_at != SIZE_MAX && + notit->buf.at == notit->buf.last_eh_at) { + goto end; + } + } + + /* All other states are invalid */ + BT_LOGW("User function returned %s, but notification iterator is in an unexpected state: " + "state=%s, cur-packet-size=%" PRId64 ", cur=%zu, " + "packet-cur=%zu, last-eh-at=%zu", + bt_notif_iter_medium_status_string(m_status), + state_string(notit->state), + notit->cur_exp_packet_total_size, + notit->buf.at, packet_at(notit), + notit->buf.last_eh_at); + m_status = BT_NOTIF_ITER_MEDIUM_STATUS_ERROR; + } else if (m_status < 0) { + BT_LOGW("User function failed: status=%s", + bt_notif_iter_medium_status_string(m_status)); } +end: return notif_iter_status_from_m_status(m_status); } static inline -enum bt_ctf_notif_iter_status buf_ensure_available_bits( - struct bt_ctf_notif_iter *notit) +enum bt_notif_iter_status buf_ensure_available_bits( + struct bt_notif_iter *notit) { - enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK; + enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; - if (buf_available_bits(notit) == 0) { + if (unlikely(buf_available_bits(notit) == 0)) { /* - * This _cannot_ return BT_CTF_NOTIF_ITER_STATUS_OK + * This _cannot_ return BT_NOTIF_ITER_STATUS_OK * _and_ no bits. */ status = request_medium_bytes(notit); @@ -436,38 +516,39 @@ enum bt_ctf_notif_iter_status buf_ensure_available_bits( } static -enum bt_ctf_notif_iter_status read_dscope_begin_state( - struct bt_ctf_notif_iter *notit, - struct bt_ctf_field_type *dscope_field_type, +enum bt_notif_iter_status read_dscope_begin_state( + struct bt_notif_iter *notit, + struct ctf_field_type *dscope_ft, enum state done_state, enum state continue_state, - struct bt_ctf_field **dscope_field) + struct bt_field *dscope_field) { - enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK; - enum bt_ctf_btr_status btr_status; + enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; + enum bt_btr_status btr_status; size_t consumed_bits; - status = buf_ensure_available_bits(notit); - if (status != BT_CTF_NOTIF_ITER_STATUS_OK) { - goto end; - } - - bt_put(*dscope_field); notit->cur_dscope_field = dscope_field; - consumed_bits = bt_ctf_btr_start(notit->btr, dscope_field_type, + BT_LOGV("Starting BTR: notit-addr=%p, btr-addr=%p, ft-addr=%p", + notit, notit->btr, dscope_ft); + consumed_bits = bt_btr_start(notit->btr, dscope_ft, notit->buf.addr, notit->buf.at, packet_at(notit), notit->buf.sz, &btr_status); + BT_LOGV("BTR consumed bits: size=%zu", consumed_bits); switch (btr_status) { - case BT_CTF_BTR_STATUS_OK: + case BT_BTR_STATUS_OK: /* type was read completely */ + BT_LOGV_STR("Field was completely decoded."); notit->state = done_state; break; - case BT_CTF_BTR_STATUS_EOF: + case BT_BTR_STATUS_EOF: + BT_LOGV_STR("BTR needs more data to decode field completely."); notit->state = continue_state; break; default: - PERR("Binary type reader failed to start\n"); - status = BT_CTF_NOTIF_ITER_STATUS_ERROR; + BT_LOGW("BTR failed to start: notit-addr=%p, btr-addr=%p, " + "status=%s", notit, notit->btr, + bt_btr_status_string(btr_status)); + status = BT_NOTIF_ITER_STATUS_ERROR; goto end; } @@ -479,32 +560,50 @@ end: } static -enum bt_ctf_notif_iter_status read_dscope_continue_state( - struct bt_ctf_notif_iter *notit, enum state done_state) +enum bt_notif_iter_status read_dscope_continue_state( + struct bt_notif_iter *notit, enum state done_state) { - enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK; - enum bt_ctf_btr_status btr_status; + enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; + enum bt_btr_status btr_status; size_t consumed_bits; + BT_LOGV("Continuing BTR: notit-addr=%p, btr-addr=%p", + notit, notit->btr); + status = buf_ensure_available_bits(notit); - if (status != BT_CTF_NOTIF_ITER_STATUS_OK) { + if (status != BT_NOTIF_ITER_STATUS_OK) { + if (status < 0) { + BT_LOGW("Cannot ensure that buffer has at least one byte: " + "notif-addr=%p, status=%s", + notit, bt_notif_iter_status_string(status)); + } else { + BT_LOGV("Cannot ensure that buffer has at least one byte: " + "notif-addr=%p, status=%s", + notit, bt_notif_iter_status_string(status)); + } + goto end; } - consumed_bits = bt_ctf_btr_continue(notit->btr, notit->buf.addr, - notit->buf.sz, &btr_status); + consumed_bits = bt_btr_continue(notit->btr, notit->buf.addr, + notit->buf.sz, &btr_status); + BT_LOGV("BTR consumed bits: size=%zu", consumed_bits); switch (btr_status) { - case BT_CTF_BTR_STATUS_OK: + case BT_BTR_STATUS_OK: /* Type was read completely. */ + BT_LOGV_STR("Field was completely decoded."); notit->state = done_state; break; - case BT_CTF_BTR_STATUS_EOF: + case BT_BTR_STATUS_EOF: /* Stay in this continue state. */ + BT_LOGV_STR("BTR needs more data to decode field completely."); break; default: - PERR("Binary type reader failed to continue\n"); - status = BT_CTF_NOTIF_ITER_STATUS_ERROR; + BT_LOGW("BTR failed to continue: notit-addr=%p, btr-addr=%p, " + "status=%s", notit, notit->btr, + bt_btr_status_string(btr_status)); + status = BT_NOTIF_ITER_STATUS_ERROR; goto end; } @@ -515,712 +614,853 @@ end: } static -void put_event_dscopes(struct bt_ctf_notif_iter *notit) +void release_event_dscopes(struct bt_notif_iter *notit) { - BT_PUT(notit->dscopes.stream_event_header); - BT_PUT(notit->dscopes.stream_event_context); - BT_PUT(notit->dscopes.event_context); - BT_PUT(notit->dscopes.event_payload); + notit->dscopes.event_header = NULL; + + if (notit->event_header_field) { + bt_event_header_field_release(notit->event_header_field); + notit->event_header_field = NULL; + } + + notit->dscopes.event_common_context = NULL; + notit->dscopes.event_spec_context = NULL; + notit->dscopes.event_payload = NULL; } static -void put_all_dscopes(struct bt_ctf_notif_iter *notit) +void release_all_dscopes(struct bt_notif_iter *notit) { - BT_PUT(notit->dscopes.trace_packet_header); - BT_PUT(notit->dscopes.stream_packet_context); - put_event_dscopes(notit); + notit->dscopes.trace_packet_header = NULL; + + if (notit->packet_header_field) { + bt_packet_header_field_release(notit->packet_header_field); + notit->packet_header_field = NULL; + } + + notit->dscopes.stream_packet_context = NULL; + + if (notit->packet_context_field) { + bt_packet_context_field_release(notit->packet_context_field); + notit->packet_context_field = NULL; + } + + release_event_dscopes(notit); } static -enum bt_ctf_notif_iter_status read_packet_header_begin_state( - struct bt_ctf_notif_iter *notit) +enum bt_notif_iter_status read_packet_header_begin_state( + struct bt_notif_iter *notit) { - struct bt_ctf_field_type *packet_header_type = NULL; - enum bt_ctf_notif_iter_status ret = BT_CTF_NOTIF_ITER_STATUS_OK; + struct ctf_field_type *packet_header_ft = NULL; + enum bt_notif_iter_status ret = BT_NOTIF_ITER_STATUS_OK; - if (bt_ctf_notif_iter_switch_packet(notit)) { - ret = BT_CTF_NOTIF_ITER_STATUS_ERROR; + if (bt_notif_iter_switch_packet(notit)) { + BT_LOGW("Cannot switch packet: notit-addr=%p", notit); + ret = BT_NOTIF_ITER_STATUS_ERROR; goto end; } /* Packet header type is common to the whole trace. */ - packet_header_type = bt_ctf_trace_get_packet_header_type( - notit->meta.trace); - if (!packet_header_type) { + packet_header_ft = notit->meta.tc->packet_header_ft; + if (!packet_header_ft) { notit->state = STATE_AFTER_TRACE_PACKET_HEADER; goto end; } - ret = read_dscope_begin_state(notit, packet_header_type, - STATE_AFTER_TRACE_PACKET_HEADER, - STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE, - ¬it->dscopes.trace_packet_header); + BT_ASSERT(!notit->packet_header_field); + + if (packet_header_ft->in_ir) { + /* + * Create free packet header field from trace. This + * field is going to be moved to the packet once we + * create it. We cannot create the packet now because: + * + * 1. A packet is created from a stream. + * 2. A stream is created from a stream class. + * 3. We need the packet header field's content to know + * the ID of the stream class to select. + */ + notit->packet_header_field = bt_packet_header_field_create( + notit->meta.tc->ir_tc); + if (!notit->packet_header_field) { + BT_LOGE_STR("Cannot create packet header field wrapper from trace."); + ret = BT_NOTIF_ITER_STATUS_ERROR; + goto end; + } + + notit->dscopes.trace_packet_header = + bt_packet_header_field_borrow_field(notit->packet_header_field); + BT_ASSERT(notit->dscopes.trace_packet_header); + } + + notit->cur_stream_class_id = -1; + notit->cur_event_class_id = -1; + notit->cur_data_stream_id = -1; + BT_LOGV("Decoding packet header field:" + "notit-addr=%p, trace-addr=%p, trace-name=\"%s\", ft-addr=%p", + notit, notit->meta.tc, + notit->meta.tc->name->str, packet_header_ft); + ret = read_dscope_begin_state(notit, packet_header_ft, + STATE_AFTER_TRACE_PACKET_HEADER, + STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE, + notit->dscopes.trace_packet_header); + if (ret < 0) { + BT_LOGW("Cannot decode packet header field: " + "notit-addr=%p, trace-addr=%p, " + "trace-name=\"%s\", ft-addr=%p", + notit, notit->meta.tc, + notit->meta.tc->name->str, + packet_header_ft); + } + end: - BT_PUT(packet_header_type); return ret; } static -enum bt_ctf_notif_iter_status read_packet_header_continue_state( - struct bt_ctf_notif_iter *notit) +enum bt_notif_iter_status read_packet_header_continue_state( + struct bt_notif_iter *notit) { return read_dscope_continue_state(notit, - STATE_AFTER_TRACE_PACKET_HEADER); + STATE_AFTER_TRACE_PACKET_HEADER); } static inline -bool is_struct_type(struct bt_ctf_field_type *field_type) +enum bt_notif_iter_status set_current_stream_class(struct bt_notif_iter *notit) { - return bt_ctf_field_type_get_type_id(field_type) == - BT_CTF_FIELD_TYPE_ID_STRUCT; -} + enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; + struct ctf_stream_class *new_stream_class = NULL; -static inline -bool is_variant_type(struct bt_ctf_field_type *field_type) -{ - return bt_ctf_field_type_get_type_id(field_type) == - BT_CTF_FIELD_TYPE_ID_VARIANT; -} + if (notit->cur_stream_class_id == -1) { + /* + * No current stream class ID field, therefore only one + * stream class. + */ + if (notit->meta.tc->stream_classes->len != 1) { + BT_LOGW("Need exactly one stream class since there's " + "no stream class ID field: " + "notit-addr=%p, trace-name=\"%s\"", + notit, notit->meta.tc->name->str); + status = BT_NOTIF_ITER_STATUS_ERROR; + goto end; + } -static -struct stream_class_field_path_cache * -create_stream_class_field_path_cache_entry( - struct bt_ctf_notif_iter *notit, - struct bt_ctf_stream_class *stream_class) -{ - int v = -1; - int id = -1; - int timestamp_end = -1; - int packet_size = -1; - int content_size = -1; - struct stream_class_field_path_cache *cache_entry = g_new0( - struct stream_class_field_path_cache, 1); - struct bt_ctf_field_type *event_header = NULL, *packet_context = NULL; - - if (!cache_entry) { + new_stream_class = notit->meta.tc->stream_classes->pdata[0]; + notit->cur_stream_class_id = new_stream_class->id; goto end; } - event_header = bt_ctf_stream_class_get_event_header_type(stream_class); - if (event_header && bt_ctf_field_type_is_structure(event_header)) { - int i, count; - - count = bt_ctf_field_type_structure_get_field_count( - event_header); - if (count < 0) { - goto error; - } - for (i = 0; i < count; i++) { - int ret; - const char *name; - - ret = bt_ctf_field_type_structure_get_field( - event_header, &name, NULL, i); - if (ret) { - goto error; - } - - if (v != -1 && id != -1) { - break; - } - if (v == -1 && !strcmp(name, "v")) { - v = i; - } else if (id == -1 && !strcmp(name, "id")) { - id = i; - } - } + new_stream_class = ctf_trace_class_borrow_stream_class_by_id( + notit->meta.tc, notit->cur_stream_class_id); + if (!new_stream_class) { + BT_LOGW("No stream class with ID of stream class ID to use in trace: " + "notit-addr=%p, stream-class-id=%" PRIu64 ", " + "trace-addr=%p, trace-name=\"%s\"", + notit, notit->cur_stream_class_id, notit->meta.tc, + notit->meta.tc->name->str); + status = BT_NOTIF_ITER_STATUS_ERROR; + goto end; } - packet_context = bt_ctf_stream_class_get_packet_context_type( - stream_class); - if (packet_context && bt_ctf_field_type_is_structure(packet_context)) { - int i, count; - - count = bt_ctf_field_type_structure_get_field_count( - packet_context); - if (count < 0) { - goto error; + if (notit->meta.sc) { + if (new_stream_class != notit->meta.sc) { + BT_LOGW("Two packets refer to two different stream classes within the same packet sequence: " + "notit-addr=%p, prev-stream-class-addr=%p, " + "prev-stream-class-id=%" PRId64 ", " + "next-stream-class-addr=%p, " + "next-stream-class-id=%" PRId64 ", " + "trace-addr=%p, trace-name=\"%s\"", + notit, notit->meta.sc, + notit->meta.sc->id, + new_stream_class, + new_stream_class->id, + notit->meta.tc, + notit->meta.tc->name->str); + status = BT_NOTIF_ITER_STATUS_ERROR; + goto end; } - for (i = 0; i < count; i++) { - int ret; - const char *name; - struct bt_ctf_field_type *field_type; - - if (timestamp_end != -1 && packet_size != -1 && - content_size != -1) { - break; - } - - ret = bt_ctf_field_type_structure_get_field( - packet_context, &name, &field_type, i); - if (ret) { - goto error; - } - - if (timestamp_end == -1 && - !strcmp(name, "timestamp_end")) { - struct field_cb_override *override = g_new0( - struct field_cb_override, 1); + } else { + notit->meta.sc = new_stream_class; + } - if (!override) { - BT_PUT(field_type); - goto error; - } + BT_LOGV("Set current stream class: " + "notit-addr=%p, stream-class-addr=%p, " + "stream-class-id=%" PRId64, + notit, notit->meta.sc, notit->meta.sc->id); - override->func = btr_timestamp_end_cb; - override->data = notit; +end: + return status; +} - g_hash_table_insert(notit->field_overrides, - bt_get(field_type), override); +static inline +enum bt_notif_iter_status set_current_stream(struct bt_notif_iter *notit) +{ + enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; + struct bt_stream *stream = NULL; + + BT_LOGV("Calling user function (get stream): notit-addr=%p, " + "stream-class-addr=%p, stream-class-id=%" PRId64, + notit, notit->meta.sc, + notit->meta.sc->id); + stream = bt_get(notit->medium.medops.borrow_stream( + notit->meta.sc->ir_sc, notit->cur_data_stream_id, + notit->medium.data)); + BT_LOGV("User function returned: stream-addr=%p", stream); + if (!stream) { + BT_LOGW_STR("User function failed to return a stream object " + "for the given stream class."); + status = BT_NOTIF_ITER_STATUS_ERROR; + goto end; + } - timestamp_end = i; - } else if (packet_size == -1 && - !strcmp(name, "packet_size")) { - packet_size = i; - } else if (content_size == -1 && - !strcmp(name, "content_size")) { - content_size = i; - } - BT_PUT(field_type); - } + if (notit->stream && stream != notit->stream) { + BT_LOGW("User function returned a different stream than the " + "previous one for the same sequence of packets."); + status = BT_NOTIF_ITER_STATUS_ERROR; + goto end; } - cache_entry->v = v; - cache_entry->id = id; - cache_entry->timestamp_end = timestamp_end; - cache_entry->packet_size = packet_size; - cache_entry->content_size = content_size; + BT_MOVE(notit->stream, stream); + end: - BT_PUT(event_header); - BT_PUT(packet_context); - return cache_entry; -error: - g_free(cache_entry); - cache_entry = NULL; - goto end; + bt_put(stream); + return status; } -static -struct stream_class_field_path_cache *get_stream_class_field_path_cache( - struct bt_ctf_notif_iter *notit, - struct bt_ctf_stream_class *stream_class) +static inline +enum bt_notif_iter_status set_current_packet(struct bt_notif_iter *notit) { - bool cache_entry_found; - struct stream_class_field_path_cache *cache_entry; - - cache_entry_found = g_hash_table_lookup_extended( - notit->sc_field_path_caches, - stream_class, NULL, (gpointer) &cache_entry); - if (unlikely(!cache_entry_found)) { - cache_entry = create_stream_class_field_path_cache_entry(notit, - stream_class); - g_hash_table_insert(notit->sc_field_path_caches, - bt_get(stream_class), (gpointer) cache_entry); - } + enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; + struct bt_packet *packet = NULL; - return cache_entry; -} + BT_LOGV("Creating packet for packet notification: " + "notit-addr=%p", notit); + BT_LOGV("Creating packet from stream: " + "notit-addr=%p, stream-addr=%p, " + "stream-class-addr=%p, " + "stream-class-id=%" PRId64, + notit, notit->stream, notit->meta.sc, + notit->meta.sc->id); -static inline -enum bt_ctf_notif_iter_status set_current_stream_class( - struct bt_ctf_notif_iter *notit) -{ - enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK; - struct bt_ctf_field_type *packet_header_type = NULL; - struct bt_ctf_field_type *stream_id_field_type = NULL; - uint64_t stream_id; - - /* Clear the current stream class field path cache. */ - notit->cur_sc_field_path_cache = NULL; - - /* Is there any "stream_id" field in the packet header? */ - packet_header_type = bt_ctf_trace_get_packet_header_type( - notit->meta.trace); - if (!packet_header_type) { - PERR("Failed to retrieve trace's packet header type\n"); - status = BT_CTF_NOTIF_ITER_STATUS_ERROR; - goto end; + /* Create packet */ + BT_ASSERT(notit->stream); + packet = bt_packet_create(notit->stream); + if (!packet) { + BT_LOGE("Cannot create packet from stream: " + "notit-addr=%p, stream-addr=%p, " + "stream-class-addr=%p, " + "stream-class-id=%" PRId64, + notit, notit->stream, notit->meta.sc, + notit->meta.sc->id); + goto error; } - assert(is_struct_type(packet_header_type)); + goto end; - // TODO: optimalize! - stream_id_field_type = - bt_ctf_field_type_structure_get_field_type_by_name( - packet_header_type, "stream_id"); - if (stream_id_field_type) { - /* Find appropriate stream class using current stream ID */ - int ret; - struct bt_ctf_field *stream_id_field = NULL; - - assert(notit->dscopes.trace_packet_header); - - // TODO: optimalize! - stream_id_field = bt_ctf_field_structure_get_field( - notit->dscopes.trace_packet_header, "stream_id"); - assert(stream_id_field); - ret = bt_ctf_field_unsigned_integer_get_value( - stream_id_field, &stream_id); - assert(!ret); - BT_PUT(stream_id_field); - } else { - /* Only one stream: pick the first stream class */ - assert(bt_ctf_trace_get_stream_class_count( - notit->meta.trace) == 1); - stream_id = 0; - } - - BT_PUT(notit->meta.stream_class); - notit->meta.stream_class = bt_ctf_trace_get_stream_class_by_id( - notit->meta.trace, stream_id); - if (!notit->meta.stream_class) { - PERR("Cannot find stream class with ID %" PRIu64 "\n", - stream_id); - status = BT_CTF_NOTIF_ITER_STATUS_ERROR; - goto end; - } +error: + BT_PUT(packet); + status = BT_NOTIF_ITER_STATUS_ERROR; - /* - * Retrieve (or lazily create) the current stream class field path - * cache. - */ - notit->cur_sc_field_path_cache = get_stream_class_field_path_cache( - notit, notit->meta.stream_class); - if (!notit->cur_sc_field_path_cache) { - PERR("Failed to retrieve stream class field path cache\n"); - status = BT_CTF_NOTIF_ITER_STATUS_ERROR; - goto end; - } end: - BT_PUT(packet_header_type); - BT_PUT(stream_id_field_type); - + BT_MOVE(notit->packet, packet); return status; } static -enum bt_ctf_notif_iter_status after_packet_header_state( - struct bt_ctf_notif_iter *notit) +enum bt_notif_iter_status after_packet_header_state( + struct bt_notif_iter *notit) { - enum bt_ctf_notif_iter_status status; + enum bt_notif_iter_status status; status = set_current_stream_class(notit); - if (status == BT_CTF_NOTIF_ITER_STATUS_OK) { - notit->state = STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN; + if (status != BT_NOTIF_ITER_STATUS_OK) { + goto end; } + notit->state = STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN; + +end: return status; } static -enum bt_ctf_notif_iter_status read_packet_context_begin_state( - struct bt_ctf_notif_iter *notit) -{ - enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK; - struct bt_ctf_field_type *packet_context_type; - - assert(notit->meta.stream_class); - packet_context_type = bt_ctf_stream_class_get_packet_context_type( - notit->meta.stream_class); - if (!packet_context_type) { +enum bt_notif_iter_status read_packet_context_begin_state( + struct bt_notif_iter *notit) +{ + enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; + struct ctf_field_type *packet_context_ft; + + BT_ASSERT(notit->meta.sc); + packet_context_ft = notit->meta.sc->packet_context_ft; + if (!packet_context_ft) { + BT_LOGV("No packet packet context field type in stream class: continuing: " + "notit-addr=%p, stream-class-addr=%p, " + "stream-class-id=%" PRId64, + notit, notit->meta.sc, + notit->meta.sc->id); notit->state = STATE_AFTER_STREAM_PACKET_CONTEXT; goto end; } - status = read_dscope_begin_state(notit, packet_context_type, - STATE_AFTER_STREAM_PACKET_CONTEXT, - STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE, - ¬it->dscopes.stream_packet_context); + BT_ASSERT(!notit->packet_context_field); + + if (packet_context_ft->in_ir) { + /* + * Create free packet context field from stream class. + * This field is going to be moved to the packet once we + * create it. We cannot create the packet now because a + * packet is created from a stream, and this API must be + * able to return the packet header and context fields + * without creating a stream + * (bt_notif_iter_borrow_packet_header_context_fields()). + */ + notit->packet_context_field = + bt_packet_context_field_create(notit->meta.sc->ir_sc); + if (!notit->packet_context_field) { + BT_LOGE_STR("Cannot create packet context field wrapper from stream class."); + status = BT_NOTIF_ITER_STATUS_ERROR; + goto end; + } + + notit->dscopes.stream_packet_context = + bt_packet_context_field_borrow_field(notit->packet_context_field); + BT_ASSERT(notit->dscopes.stream_packet_context); + } + + BT_LOGV("Decoding packet context field: " + "notit-addr=%p, stream-class-addr=%p, " + "stream-class-id=%" PRId64 ", ft-addr=%p", + notit, notit->meta.sc, + notit->meta.sc->id, packet_context_ft); + status = read_dscope_begin_state(notit, packet_context_ft, + STATE_AFTER_STREAM_PACKET_CONTEXT, + STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE, + notit->dscopes.stream_packet_context); + if (status < 0) { + BT_LOGW("Cannot decode packet context field: " + "notit-addr=%p, stream-class-addr=%p, " + "stream-class-id=%" PRId64 ", ft-addr=%p", + notit, notit->meta.sc, + notit->meta.sc->id, + packet_context_ft); + } end: - BT_PUT(packet_context_type); return status; } static -enum bt_ctf_notif_iter_status read_packet_context_continue_state( - struct bt_ctf_notif_iter *notit) +enum bt_notif_iter_status read_packet_context_continue_state( + struct bt_notif_iter *notit) { return read_dscope_continue_state(notit, STATE_AFTER_STREAM_PACKET_CONTEXT); } static -enum bt_ctf_notif_iter_status set_current_packet_content_sizes( - struct bt_ctf_notif_iter *notit) -{ - enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK; - struct bt_ctf_field *packet_size_field = NULL; - struct bt_ctf_field *content_size_field = NULL; - uint64_t content_size = -1, packet_size = -1; - - assert(notit->dscopes.stream_packet_context); - - packet_size_field = bt_ctf_field_structure_get_field( - notit->dscopes.stream_packet_context, "packet_size"); - content_size_field = bt_ctf_field_structure_get_field( - notit->dscopes.stream_packet_context, "content_size"); - if (packet_size_field) { - int ret = bt_ctf_field_unsigned_integer_get_value( - packet_size_field, &packet_size); - - assert(!ret); - if (packet_size == 0) { - PERR("Decoded packet size is 0\n"); - status = BT_CTF_NOTIF_ITER_STATUS_ERROR; - goto end; - } else if ((packet_size % 8) != 0) { - PERR("Decoded packet size is not a multiple of 8\n"); - status = BT_CTF_NOTIF_ITER_STATUS_ERROR; +enum bt_notif_iter_status set_current_packet_content_sizes( + struct bt_notif_iter *notit) +{ + enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; + + if (notit->cur_exp_packet_total_size == -1) { + if (notit->cur_exp_packet_content_size != -1) { + BT_LOGW("Content size is set, but packet size is not: " + "notit-addr=%p, packet-context-field-addr=%p, " + "packet-size=%" PRId64 ", content-size=%" PRId64, + notit, notit->dscopes.stream_packet_context, + notit->cur_exp_packet_total_size, + notit->cur_exp_packet_content_size); + status = BT_NOTIF_ITER_STATUS_ERROR; goto end; } + } else { + if (notit->cur_exp_packet_content_size == -1) { + notit->cur_exp_packet_content_size = + notit->cur_exp_packet_total_size; + } } - if (content_size_field) { - int ret = bt_ctf_field_unsigned_integer_get_value( - content_size_field, &content_size); - - assert(!ret); - } else { - content_size = packet_size; + if (notit->cur_exp_packet_content_size > + notit->cur_exp_packet_total_size) { + BT_LOGW("Invalid packet or content size: " + "content size is greater than packet size: " + "notit-addr=%p, packet-context-field-addr=%p, " + "packet-size=%" PRId64 ", content-size=%" PRId64, + notit, notit->dscopes.stream_packet_context, + notit->cur_exp_packet_total_size, + notit->cur_exp_packet_content_size); + status = BT_NOTIF_ITER_STATUS_ERROR; + goto end; } - notit->cur_packet_size = packet_size; - notit->cur_content_size = content_size; + BT_LOGV("Set current packet and content sizes: " + "notit-addr=%p, packet-size=%" PRIu64 ", content-size=%" PRIu64, + notit, notit->cur_exp_packet_total_size, + notit->cur_exp_packet_content_size); end: - BT_PUT(packet_size_field); - BT_PUT(content_size_field); return status; } static -enum bt_ctf_notif_iter_status after_packet_context_state( - struct bt_ctf_notif_iter *notit) +enum bt_notif_iter_status after_packet_context_state( + struct bt_notif_iter *notit) { - enum bt_ctf_notif_iter_status status; + enum bt_notif_iter_status status; status = set_current_packet_content_sizes(notit); - if (status == BT_CTF_NOTIF_ITER_STATUS_OK) { + if (status != BT_NOTIF_ITER_STATUS_OK) { + goto end; + } + + if (notit->stream_begin_emitted) { notit->state = STATE_EMIT_NOTIF_NEW_PACKET; + } else { + notit->state = STATE_EMIT_NOTIF_NEW_STREAM; } +end: return status; } static -enum bt_ctf_notif_iter_status read_event_header_begin_state( - struct bt_ctf_notif_iter *notit) +enum bt_notif_iter_status read_event_header_begin_state( + struct bt_notif_iter *notit) { - enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK; - struct bt_ctf_field_type *event_header_type = NULL; + enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; + struct ctf_field_type *event_header_ft = NULL; + + /* Reset the position of the last event header */ + notit->buf.last_eh_at = notit->buf.at; + notit->cur_event_class_id = -1; /* Check if we have some content left */ - if (notit->cur_content_size >= 0) { - if (packet_at(notit) == notit->cur_content_size) { + if (notit->cur_exp_packet_content_size >= 0) { + if (unlikely(packet_at(notit) == + notit->cur_exp_packet_content_size)) { /* No more events! */ + BT_LOGV("Reached end of packet: notit-addr=%p, " + "cur=%zu", notit, packet_at(notit)); notit->state = STATE_EMIT_NOTIF_END_OF_PACKET; goto end; - } else if (packet_at(notit) > notit->cur_content_size) { + } else if (unlikely(packet_at(notit) > + notit->cur_exp_packet_content_size)) { /* That's not supposed to happen */ - PERR("Cursor passed packet's content size:\n"); - PERR("\tDecoded content size: %zu\n", - notit->cur_content_size); - PERR("\tCursor position: %zu\n", packet_at(notit)); - status = BT_CTF_NOTIF_ITER_STATUS_ERROR; + BT_LOGV("Before decoding event header field: cursor is passed the packet's content: " + "notit-addr=%p, content-size=%" PRId64 ", " + "cur=%zu", notit, + notit->cur_exp_packet_content_size, + packet_at(notit)); + status = BT_NOTIF_ITER_STATUS_ERROR; + goto end; + } + } else { + /* + * "Infinite" content: we're done when the medium has + * nothing else for us. + */ + status = buf_ensure_available_bits(notit); + if (status != BT_NOTIF_ITER_STATUS_OK) { + /* + * If this function returns + * `BT_NOTIF_ITER_STATUS_EOF`: + * + * 1. bt_notif_iter_get_next_notification() + * emits a "packet end" notification. This + * resets the current packet. The state + * remains unchanged otherwise. + * 2. This function is called again. It returns + * `BT_NOTIF_ITER_STATUS_EOF` again. + * 3. bt_notif_iter_get_next_notification() + * emits a "stream end" notification because + * there's no current packet. It sets the + * current state to `STATE_DONE`. + */ goto end; } } - event_header_type = bt_ctf_stream_class_get_event_header_type( - notit->meta.stream_class); - if (!event_header_type) { - notit->state = STATE_AFTER_STREAM_EVENT_HEADER; + release_event_dscopes(notit); + BT_ASSERT(notit->meta.sc); + event_header_ft = notit->meta.sc->event_header_ft; + if (!event_header_ft) { + notit->state = STATE_AFTER_EVENT_HEADER; goto end; } - put_event_dscopes(notit); - status = read_dscope_begin_state(notit, event_header_type, - STATE_AFTER_STREAM_EVENT_HEADER, - STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE, - ¬it->dscopes.stream_event_header); -end: - BT_PUT(event_header_type); + if (event_header_ft->in_ir) { + BT_ASSERT(!notit->event_header_field); + notit->event_header_field = bt_event_header_field_create( + notit->meta.sc->ir_sc); + if (!notit->event_header_field) { + BT_LOGE_STR("Cannot create event header field wrapper from trace."); + status = BT_NOTIF_ITER_STATUS_ERROR; + goto end; + } + notit->dscopes.event_header = + bt_event_header_field_borrow_field(notit->event_header_field); + BT_ASSERT(notit->dscopes.event_header); + } + + BT_LOGV("Decoding event header field: " + "notit-addr=%p, stream-class-addr=%p, " + "stream-class-id=%" PRId64 ", " + "ft-addr=%p", + notit, notit->meta.sc, + notit->meta.sc->id, + event_header_ft); + status = read_dscope_begin_state(notit, event_header_ft, + STATE_AFTER_EVENT_HEADER, + STATE_DSCOPE_EVENT_HEADER_CONTINUE, + notit->dscopes.event_header); + if (status < 0) { + BT_LOGW("Cannot decode event header field: " + "notit-addr=%p, stream-class-addr=%p, " + "stream-class-id=%" PRId64 ", ft-addr=%p", + notit, notit->meta.sc, + notit->meta.sc->id, + event_header_ft); + } + +end: return status; } static -enum bt_ctf_notif_iter_status read_event_header_continue_state( - struct bt_ctf_notif_iter *notit) +enum bt_notif_iter_status read_event_header_continue_state( + struct bt_notif_iter *notit) { return read_dscope_continue_state(notit, - STATE_AFTER_STREAM_EVENT_HEADER); + STATE_AFTER_EVENT_HEADER); } static inline -enum bt_ctf_notif_iter_status set_current_event_class(struct bt_ctf_notif_iter *notit) +enum bt_notif_iter_status set_current_event_class(struct bt_notif_iter *notit) { - /* - * The assert() calls in this function are okay because it is - * assumed here that all the metadata objects have been - * validated for CTF correctness before decoding actual streams. - */ - - enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK; - struct bt_ctf_field_type *event_header_type; - struct bt_ctf_field_type *id_field_type = NULL; - struct bt_ctf_field_type *v_field_type = NULL; - uint64_t event_id = -1ULL; - int ret; + enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; - event_header_type = bt_ctf_stream_class_get_event_header_type( - notit->meta.stream_class); - if (!event_header_type) { - PERR("Failed to retrieve stream class's event header type\n"); - status = BT_CTF_NOTIF_ITER_STATUS_ERROR; - goto end; - } + struct ctf_event_class *new_event_class = NULL; - /* Is there any "id"/"v" field in the event header? */ - assert(is_struct_type(event_header_type)); - id_field_type = bt_ctf_field_type_structure_get_field_type_by_name( - event_header_type, "id"); - v_field_type = bt_ctf_field_type_structure_get_field_type_by_name( - event_header_type, "v"); - assert(notit->dscopes.stream_event_header); - if (v_field_type) { + if (notit->cur_event_class_id == -1) { /* - * _ _____ _____ - * | | |_ _|_ _| __ __ _ - * | | | | | || '_ \ / _` | - * | |___| | | || | | | (_| | S P E C I A L - * |_____|_| |_||_| |_|\__, | C A S E ™ - * |___/ + * No current event class ID field, therefore only one + * event class. */ - struct bt_ctf_field *v_field = NULL; - struct bt_ctf_field *v_struct_field = NULL; - struct bt_ctf_field *v_struct_id_field = NULL; - - // TODO: optimalize! - v_field = bt_ctf_field_structure_get_field( - notit->dscopes.stream_event_header, "v"); - assert(v_field); - - v_struct_field = - bt_ctf_field_variant_get_current_field(v_field); - if (!v_struct_field) { - goto end_v_field_type; - } - - // TODO: optimalize! - v_struct_id_field = - bt_ctf_field_structure_get_field(v_struct_field, "id"); - if (!v_struct_id_field) { - goto end_v_field_type; + if (notit->meta.sc->event_classes->len != 1) { + BT_LOGW("Need exactly one event class since there's " + "no event class ID field: " + "notit-addr=%p, trace-name=\"%s\"", + notit, notit->meta.tc->name->str); + status = BT_NOTIF_ITER_STATUS_ERROR; + goto end; } - ret = bt_ctf_field_unsigned_integer_get_value( - v_struct_id_field, &event_id); - if (ret) { - event_id = -1ULL; - } + new_event_class = notit->meta.sc->event_classes->pdata[0]; + notit->cur_event_class_id = new_event_class->id; + goto end; + } -end_v_field_type: - BT_PUT(v_field); - BT_PUT(v_struct_field); - BT_PUT(v_struct_id_field); + new_event_class = ctf_stream_class_borrow_event_class_by_id( + notit->meta.sc, notit->cur_event_class_id); + if (!new_event_class) { + BT_LOGW("No event class with ID of event class ID to use in stream class: " + "notit-addr=%p, stream-class-id=%" PRIu64 ", " + "event-class-id=%" PRIu64 ", " + "trace-addr=%p, trace-name=\"%s\"", + notit, notit->meta.sc->id, notit->cur_event_class_id, + notit->meta.tc, notit->meta.tc->name->str); + status = BT_NOTIF_ITER_STATUS_ERROR; + goto end; } - if (id_field_type && event_id == -1ULL) { - /* Check "id" field */ - struct bt_ctf_field *id_field = NULL; - int ret; + notit->meta.ec = new_event_class; + BT_LOGV("Set current event class: " + "notit-addr=%p, event-class-addr=%p, " + "event-class-id=%" PRId64 ", " + "event-class-name=\"%s\"", + notit, notit->meta.ec, notit->meta.ec->id, + notit->meta.ec->name->str); - // TODO: optimalize! - id_field = bt_ctf_field_structure_get_field( - notit->dscopes.stream_event_header, "id"); - assert(id_field); - assert(bt_ctf_field_is_integer(id_field) || - bt_ctf_field_is_enumeration(id_field)); +end: + return status; +} - if (bt_ctf_field_is_integer(id_field)) { - ret = bt_ctf_field_unsigned_integer_get_value( - id_field, &event_id); - } else { - struct bt_ctf_field *container; - - container = bt_ctf_field_enumeration_get_container( - id_field); - assert(container); - ret = bt_ctf_field_unsigned_integer_get_value( - container, &event_id); - BT_PUT(container); - } - assert(!ret); - BT_PUT(id_field); +static inline +enum bt_notif_iter_status set_current_event_notification( + struct bt_notif_iter *notit) +{ + enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; + struct bt_notification *notif = NULL; + + BT_ASSERT(notit->meta.ec); + BT_ASSERT(notit->packet); + BT_LOGV("Creating event notification from event class and packet: " + "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", packet-addr=%p", + notit, notit->meta.ec, + notit->meta.ec->name->str, + notit->packet); + BT_ASSERT(notit->notif_iter); + notif = bt_notification_event_create(notit->notif_iter, + notit->meta.ec->ir_ec, notit->packet); + if (!notif) { + BT_LOGE("Cannot create event notification: " + "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", " + "packet-addr=%p", + notit, notit->meta.ec, + notit->meta.ec->name->str, + notit->packet); + goto error; } - if (event_id == -1ULL) { - /* Event ID not found: single event? */ - assert(bt_ctf_stream_class_get_event_class_count( - notit->meta.stream_class) == 1); - event_id = 0; - } + goto end; - BT_PUT(notit->meta.event_class); - notit->meta.event_class = bt_ctf_stream_class_get_event_class_by_id( - notit->meta.stream_class, event_id); - if (!notit->meta.event_class) { - PERR("Cannot find event class with ID %" PRIu64 "\n", event_id); - status = BT_CTF_NOTIF_ITER_STATUS_ERROR; - goto end; - } +error: + BT_PUT(notif); + status = BT_NOTIF_ITER_STATUS_ERROR; end: - BT_PUT(event_header_type); - BT_PUT(id_field_type); - BT_PUT(v_field_type); - + BT_MOVE(notit->event_notif, notif); return status; } static -enum bt_ctf_notif_iter_status after_event_header_state( - struct bt_ctf_notif_iter *notit) +enum bt_notif_iter_status after_event_header_state( + struct bt_notif_iter *notit) { - enum bt_ctf_notif_iter_status status; + enum bt_notif_iter_status status; status = set_current_event_class(notit); - if (status != BT_CTF_NOTIF_ITER_STATUS_OK) { - PERR("Failed to set current event class\n"); + if (status != BT_NOTIF_ITER_STATUS_OK) { goto end; } - notit->state = STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN; - + status = set_current_event_notification(notit); + if (status != BT_NOTIF_ITER_STATUS_OK) { + goto end; + } + + notit->event = bt_notification_event_borrow_event(notit->event_notif); + BT_ASSERT(notit->event); + + if (notit->event_header_field) { + int ret; + + BT_ASSERT(notit->event); + ret = bt_event_move_header(notit->event, + notit->event_header_field); + if (ret) { + status = BT_NOTIF_ITER_STATUS_ERROR; + goto end; + } + + notit->event_header_field = NULL; + + /* + * At this point notit->dscopes.event_header has + * the same value as the event header field within + * notit->event. + */ + BT_ASSERT(bt_event_borrow_header_field(notit->event) == + notit->dscopes.event_header); + } + + notit->state = STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN; + end: return status; } static -enum bt_ctf_notif_iter_status read_stream_event_context_begin_state( - struct bt_ctf_notif_iter *notit) +enum bt_notif_iter_status read_event_common_context_begin_state( + struct bt_notif_iter *notit) { - enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK; - struct bt_ctf_field_type *stream_event_context_type; + enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; + struct ctf_field_type *event_common_context_ft; - stream_event_context_type = bt_ctf_stream_class_get_event_context_type( - notit->meta.stream_class); - if (!stream_event_context_type) { - notit->state = STATE_DSCOPE_EVENT_CONTEXT_BEGIN; + event_common_context_ft = notit->meta.sc->event_common_context_ft; + if (!event_common_context_ft) { + notit->state = STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN; goto end; } - status = read_dscope_begin_state(notit, stream_event_context_type, - STATE_DSCOPE_EVENT_CONTEXT_BEGIN, - STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE, - ¬it->dscopes.stream_event_context); + if (event_common_context_ft->in_ir) { + BT_ASSERT(!notit->dscopes.event_common_context); + notit->dscopes.event_common_context = + bt_event_borrow_common_context_field(notit->event); + BT_ASSERT(notit->dscopes.event_common_context); + } + + BT_LOGV("Decoding event common context field: " + "notit-addr=%p, stream-class-addr=%p, " + "stream-class-id=%" PRId64 ", " + "ft-addr=%p", + notit, notit->meta.sc, + notit->meta.sc->id, + event_common_context_ft); + status = read_dscope_begin_state(notit, event_common_context_ft, + STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN, + STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE, + notit->dscopes.event_common_context); + if (status < 0) { + BT_LOGW("Cannot decode event common context field: " + "notit-addr=%p, stream-class-addr=%p, " + "stream-class-id=%" PRId64 ", ft-addr=%p", + notit, notit->meta.sc, + notit->meta.sc->id, + event_common_context_ft); + } end: - BT_PUT(stream_event_context_type); - return status; } static -enum bt_ctf_notif_iter_status read_stream_event_context_continue_state( - struct bt_ctf_notif_iter *notit) +enum bt_notif_iter_status read_event_common_context_continue_state( + struct bt_notif_iter *notit) { return read_dscope_continue_state(notit, - STATE_DSCOPE_EVENT_CONTEXT_BEGIN); + STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN); } static -enum bt_ctf_notif_iter_status read_event_context_begin_state( - struct bt_ctf_notif_iter *notit) +enum bt_notif_iter_status read_event_spec_context_begin_state( + struct bt_notif_iter *notit) { - enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK; - struct bt_ctf_field_type *event_context_type; + enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; + struct ctf_field_type *event_spec_context_ft; - event_context_type = bt_ctf_event_class_get_context_type( - notit->meta.event_class); - if (!event_context_type) { + event_spec_context_ft = notit->meta.ec->spec_context_ft; + if (!event_spec_context_ft) { notit->state = STATE_DSCOPE_EVENT_PAYLOAD_BEGIN; goto end; } - status = read_dscope_begin_state(notit, event_context_type, + + if (event_spec_context_ft->in_ir) { + BT_ASSERT(!notit->dscopes.event_spec_context); + notit->dscopes.event_spec_context = bt_event_borrow_specific_context_field( + notit->event); + BT_ASSERT(notit->dscopes.event_spec_context); + } + + BT_LOGV("Decoding event specific context field: " + "notit-addr=%p, event-class-addr=%p, " + "event-class-name=\"%s\", event-class-id=%" PRId64 ", " + "ft-addr=%p", + notit, notit->meta.ec, + notit->meta.ec->name->str, + notit->meta.ec->id, + event_spec_context_ft); + status = read_dscope_begin_state(notit, event_spec_context_ft, STATE_DSCOPE_EVENT_PAYLOAD_BEGIN, - STATE_DSCOPE_EVENT_CONTEXT_CONTINUE, - ¬it->dscopes.event_context); + STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE, + notit->dscopes.event_spec_context); + if (status < 0) { + BT_LOGW("Cannot decode event specific context field: " + "notit-addr=%p, event-class-addr=%p, " + "event-class-name=\"%s\", " + "event-class-id=%" PRId64 ", ft-addr=%p", + notit, notit->meta.ec, + notit->meta.ec->name->str, + notit->meta.ec->id, + event_spec_context_ft); + } end: - BT_PUT(event_context_type); - return status; } static -enum bt_ctf_notif_iter_status read_event_context_continue_state( - struct bt_ctf_notif_iter *notit) +enum bt_notif_iter_status read_event_spec_context_continue_state( + struct bt_notif_iter *notit) { return read_dscope_continue_state(notit, STATE_DSCOPE_EVENT_PAYLOAD_BEGIN); } static -enum bt_ctf_notif_iter_status read_event_payload_begin_state( - struct bt_ctf_notif_iter *notit) +enum bt_notif_iter_status read_event_payload_begin_state( + struct bt_notif_iter *notit) { - enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK; - struct bt_ctf_field_type *event_payload_type; + enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; + struct ctf_field_type *event_payload_ft; - event_payload_type = bt_ctf_event_class_get_payload_type( - notit->meta.event_class); - if (!event_payload_type) { + event_payload_ft = notit->meta.ec->payload_ft; + if (!event_payload_ft) { notit->state = STATE_EMIT_NOTIF_EVENT; goto end; } - status = read_dscope_begin_state(notit, event_payload_type, + if (event_payload_ft->in_ir) { + BT_ASSERT(!notit->dscopes.event_payload); + notit->dscopes.event_payload = bt_event_borrow_payload_field( + notit->event); + BT_ASSERT(notit->dscopes.event_payload); + } + + BT_LOGV("Decoding event payload field: " + "notit-addr=%p, event-class-addr=%p, " + "event-class-name=\"%s\", event-class-id=%" PRId64 ", " + "ft-addr=%p", + notit, notit->meta.ec, + notit->meta.ec->name->str, + notit->meta.ec->id, + event_payload_ft); + status = read_dscope_begin_state(notit, event_payload_ft, STATE_EMIT_NOTIF_EVENT, STATE_DSCOPE_EVENT_PAYLOAD_CONTINUE, - ¬it->dscopes.event_payload); + notit->dscopes.event_payload); + if (status < 0) { + BT_LOGW("Cannot decode event payload field: " + "notit-addr=%p, event-class-addr=%p, " + "event-class-name=\"%s\", " + "event-class-id=%" PRId64 ", ft-addr=%p", + notit, notit->meta.ec, + notit->meta.ec->name->str, + notit->meta.ec->id, + event_payload_ft); + } end: - BT_PUT(event_payload_type); - return status; } static -enum bt_ctf_notif_iter_status read_event_payload_continue_state( - struct bt_ctf_notif_iter *notit) +enum bt_notif_iter_status read_event_payload_continue_state( + struct bt_notif_iter *notit) { return read_dscope_continue_state(notit, STATE_EMIT_NOTIF_EVENT); } static -enum bt_ctf_notif_iter_status skip_packet_padding_state( - struct bt_ctf_notif_iter *notit) +enum bt_notif_iter_status skip_packet_padding_state( + struct bt_notif_iter *notit) { - enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK; + enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; size_t bits_to_skip; - assert(notit->cur_packet_size > 0); - bits_to_skip = notit->cur_packet_size - packet_at(notit); + BT_ASSERT(notit->cur_exp_packet_total_size > 0); + bits_to_skip = notit->cur_exp_packet_total_size - packet_at(notit); if (bits_to_skip == 0) { notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN; goto end; } else { size_t bits_to_consume; + + BT_LOGV("Trying to skip %zu bits of padding: notit-addr=%p, size=%zu", + bits_to_skip, notit, bits_to_skip); status = buf_ensure_available_bits(notit); - if (status != BT_CTF_NOTIF_ITER_STATUS_OK) { + if (status != BT_NOTIF_ITER_STATUS_OK) { goto end; } bits_to_consume = MIN(buf_available_bits(notit), bits_to_skip); + BT_LOGV("Skipping %zu bits of padding: notit-addr=%p, size=%zu", + bits_to_consume, notit, bits_to_consume); buf_consume_bits(notit, bits_to_consume); - bits_to_skip = notit->cur_packet_size - packet_at(notit); + bits_to_skip = notit->cur_exp_packet_total_size - + packet_at(notit); if (bits_to_skip == 0) { notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN; goto end; @@ -1232,14 +1472,16 @@ end: } static inline -enum bt_ctf_notif_iter_status handle_state(struct bt_ctf_notif_iter *notit) +enum bt_notif_iter_status handle_state(struct bt_notif_iter *notit) { - enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK; + enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; + const enum state state = notit->state; - PDBG("Handling state %d\n", notit->state); + BT_LOGV("Handling state: notit-addr=%p, state=%s", + notit, state_string(state)); // TODO: optimalize! - switch (notit->state) { + switch (state) { case STATE_INIT: notit->state = STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN; break; @@ -1261,29 +1503,32 @@ enum bt_ctf_notif_iter_status handle_state(struct bt_ctf_notif_iter *notit) case STATE_AFTER_STREAM_PACKET_CONTEXT: status = after_packet_context_state(notit); break; + case STATE_EMIT_NOTIF_NEW_STREAM: + notit->state = STATE_EMIT_NOTIF_NEW_PACKET; + break; case STATE_EMIT_NOTIF_NEW_PACKET: - notit->state = STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN; + notit->state = STATE_DSCOPE_EVENT_HEADER_BEGIN; break; - case STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN: + case STATE_DSCOPE_EVENT_HEADER_BEGIN: status = read_event_header_begin_state(notit); break; - case STATE_DSCOPE_STREAM_EVENT_HEADER_CONTINUE: + case STATE_DSCOPE_EVENT_HEADER_CONTINUE: status = read_event_header_continue_state(notit); break; - case STATE_AFTER_STREAM_EVENT_HEADER: + case STATE_AFTER_EVENT_HEADER: status = after_event_header_state(notit); break; - case STATE_DSCOPE_STREAM_EVENT_CONTEXT_BEGIN: - status = read_stream_event_context_begin_state(notit); + case STATE_DSCOPE_EVENT_COMMON_CONTEXT_BEGIN: + status = read_event_common_context_begin_state(notit); break; - case STATE_DSCOPE_STREAM_EVENT_CONTEXT_CONTINUE: - status = read_stream_event_context_continue_state(notit); + case STATE_DSCOPE_EVENT_COMMON_CONTEXT_CONTINUE: + status = read_event_common_context_continue_state(notit); break; - case STATE_DSCOPE_EVENT_CONTEXT_BEGIN: - status = read_event_context_begin_state(notit); + case STATE_DSCOPE_EVENT_SPEC_CONTEXT_BEGIN: + status = read_event_spec_context_begin_state(notit); break; - case STATE_DSCOPE_EVENT_CONTEXT_CONTINUE: - status = read_event_context_continue_state(notit); + case STATE_DSCOPE_EVENT_SPEC_CONTEXT_CONTINUE: + status = read_event_spec_context_continue_state(notit); break; case STATE_DSCOPE_EVENT_PAYLOAD_BEGIN: status = read_event_payload_begin_state(notit); @@ -1292,7 +1537,7 @@ enum bt_ctf_notif_iter_status handle_state(struct bt_ctf_notif_iter *notit) status = read_event_payload_continue_state(notit); break; case STATE_EMIT_NOTIF_EVENT: - notit->state = STATE_DSCOPE_STREAM_EVENT_HEADER_BEGIN; + notit->state = STATE_DSCOPE_EVENT_HEADER_BEGIN; break; case STATE_SKIP_PACKET_PADDING: status = skip_packet_padding_state(notit); @@ -1300,44 +1545,91 @@ enum bt_ctf_notif_iter_status handle_state(struct bt_ctf_notif_iter *notit) case STATE_EMIT_NOTIF_END_OF_PACKET: notit->state = STATE_SKIP_PACKET_PADDING; break; + default: + BT_LOGD("Unknown CTF plugin notification iterator state: " + "notit-addr=%p, state=%d", notit, notit->state); + abort(); } + BT_LOGV("Handled state: notit-addr=%p, status=%s, " + "prev-state=%s, cur-state=%s", + notit, bt_notif_iter_status_string(status), + state_string(state), state_string(notit->state)); return status; } /** * Resets the internal state of a CTF notification iterator. */ -static -void bt_ctf_notif_iter_reset(struct bt_ctf_notif_iter *notit) +BT_HIDDEN +void bt_notif_iter_reset(struct bt_notif_iter *notit) { - assert(notit); + BT_ASSERT(notit); + BT_LOGD("Resetting notification iterator: addr=%p", notit); stack_clear(notit->stack); - BT_PUT(notit->meta.stream_class); - BT_PUT(notit->meta.event_class); + notit->meta.sc = NULL; + notit->meta.ec = NULL; BT_PUT(notit->packet); - put_all_dscopes(notit); + BT_PUT(notit->stream); + BT_PUT(notit->event_notif); + release_all_dscopes(notit); + notit->cur_dscope_field = NULL; + + if (notit->packet_header_field) { + bt_packet_header_field_release(notit->packet_header_field); + notit->packet_header_field = NULL; + } + + if (notit->packet_context_field) { + bt_packet_context_field_release(notit->packet_context_field); + notit->packet_context_field = NULL; + } + + if (notit->event_header_field) { + bt_event_header_field_release(notit->event_header_field); + notit->event_header_field = NULL; + } + notit->buf.addr = NULL; notit->buf.sz = 0; notit->buf.at = 0; + notit->buf.last_eh_at = SIZE_MAX; notit->buf.packet_offset = 0; notit->state = STATE_INIT; - notit->cur_content_size = -1; - notit->cur_packet_size = -1; + notit->cur_exp_packet_content_size = -1; + notit->cur_exp_packet_total_size = -1; + notit->cur_packet_offset = -1; + notit->cur_stream_class_id = -1; + notit->cur_event_class_id = -1; + notit->cur_data_stream_id = -1; + notit->stream_begin_emitted = false; } static -int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter *notit) +int bt_notif_iter_switch_packet(struct bt_notif_iter *notit) { int ret = 0; - assert(notit); + /* + * We don't put the stream class here because we need to make + * sure that all the packets processed by the same notification + * iterator refer to the same stream class (the first one). + */ + BT_ASSERT(notit); + + if (notit->cur_exp_packet_total_size != -1) { + notit->cur_packet_offset += notit->cur_exp_packet_total_size; + } + + BT_LOGV("Switching packet: notit-addr=%p, cur=%zu, " + "packet-offset=%" PRId64, notit, notit->buf.at, + notit->cur_packet_offset); stack_clear(notit->stack); - BT_PUT(notit->meta.stream_class); - BT_PUT(notit->meta.event_class); + notit->meta.ec = NULL; BT_PUT(notit->packet); - BT_PUT(notit->cur_timestamp_end); - put_all_dscopes(notit); + BT_PUT(notit->event_notif); + release_all_dscopes(notit); + notit->cur_dscope_field = NULL; /* * Adjust current buffer so that addr points to the beginning of the new @@ -1348,6 +1640,8 @@ int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter *notit) /* Packets are assumed to start on a byte frontier. */ if (notit->buf.at % CHAR_BIT) { + BT_LOGW("Cannot switch packet: current position is not a multiple of 8: " + "notit-addr=%p, cur=%zu", notit, notit->buf.at); ret = -1; goto end; } @@ -1356,401 +1650,358 @@ int bt_ctf_notif_iter_switch_packet(struct bt_ctf_notif_iter *notit) notit->buf.sz -= consumed_bytes; notit->buf.at = 0; notit->buf.packet_offset = 0; + BT_LOGV("Adjusted buffer: addr=%p, size=%zu", + notit->buf.addr, notit->buf.sz); } - notit->cur_content_size = -1; - notit->cur_packet_size = -1; - notit->cur_sc_field_path_cache = NULL; + notit->cur_exp_packet_content_size = -1; + notit->cur_exp_packet_total_size = -1; + notit->cur_stream_class_id = -1; + notit->cur_event_class_id = -1; + notit->cur_data_stream_id = -1; + notit->snapshots.discarded_events = UINT64_C(-1); + notit->snapshots.packets = UINT64_C(-1); + notit->snapshots.beginning_clock = UINT64_C(-1); + notit->snapshots.end_clock = UINT64_C(-1); + end: return ret; } static -struct bt_ctf_field *get_next_field(struct bt_ctf_notif_iter *notit) +struct bt_field *borrow_next_field(struct bt_notif_iter *notit) { - struct bt_ctf_field *next_field = NULL; - struct bt_ctf_field *base_field; - struct bt_ctf_field_type *base_type; + struct bt_field *next_field = NULL; + struct bt_field *base_field; + struct bt_field_type *base_ft; size_t index; - assert(!stack_empty(notit->stack)); + BT_ASSERT(!stack_empty(notit->stack)); index = stack_top(notit->stack)->index; base_field = stack_top(notit->stack)->base; - base_type = bt_ctf_field_get_type(base_field); - if (!base_type) { - PERR("Failed to get base field's type\n"); - goto end; - } - - switch (bt_ctf_field_type_get_type_id(base_type)) { - case BT_CTF_FIELD_TYPE_ID_STRUCT: - next_field = bt_ctf_field_structure_get_field_by_index( + BT_ASSERT(base_field); + base_ft = bt_field_borrow_type(base_field); + BT_ASSERT(base_ft); + + switch (bt_field_type_get_type_id(base_ft)) { + case BT_FIELD_TYPE_ID_STRUCTURE: + { + BT_ASSERT(index < + bt_field_type_structure_get_member_count( + bt_field_borrow_type(base_field))); + next_field = bt_field_structure_borrow_member_field_by_index( base_field, index); break; - case BT_CTF_FIELD_TYPE_ID_ARRAY: - next_field = bt_ctf_field_array_get_field(base_field, index); - break; - case BT_CTF_FIELD_TYPE_ID_SEQUENCE: - next_field = bt_ctf_field_sequence_get_field(base_field, index); + } + case BT_FIELD_TYPE_ID_STATIC_ARRAY: + case BT_FIELD_TYPE_ID_DYNAMIC_ARRAY: + BT_ASSERT(index < bt_field_array_get_length(base_field)); + next_field = bt_field_array_borrow_element_field_by_index( + base_field, index); break; - case BT_CTF_FIELD_TYPE_ID_VARIANT: - next_field = bt_ctf_field_variant_get_current_field(base_field); + case BT_FIELD_TYPE_ID_VARIANT: + BT_ASSERT(index == 0); + next_field = bt_field_variant_borrow_selected_option_field( + base_field); break; default: - assert(false); - break; + abort(); } -end: - BT_PUT(base_type); - + BT_ASSERT(next_field); return next_field; } static -void update_clock_state(uint64_t *state, - struct bt_ctf_field *value_field) +void update_default_clock(struct bt_notif_iter *notit, uint64_t new_val, + uint64_t new_val_size) { - struct bt_ctf_field_type *value_type = NULL; - uint64_t requested_new_value; - uint64_t requested_new_value_mask; + uint64_t new_val_mask; uint64_t cur_value_masked; - int requested_new_value_size; - int ret; - - value_type = bt_ctf_field_get_type(value_field); - assert(value_type); - - requested_new_value_size = - bt_ctf_field_type_integer_get_size(value_type); - assert(requested_new_value_size > 0); - ret = bt_ctf_field_unsigned_integer_get_value(value_field, - &requested_new_value); - assert(!ret); + BT_ASSERT(new_val_size > 0); /* * Special case for a 64-bit new value, which is the limit * of a clock value as of this version: overwrite the * current value directly. */ - if (requested_new_value_size == 64) { - *state = requested_new_value; + if (new_val_size == 64) { + notit->default_clock_val = new_val; goto end; } - requested_new_value_mask = (1ULL << requested_new_value_size) - 1; - cur_value_masked = *state & requested_new_value_mask; + new_val_mask = (1ULL << new_val_size) - 1; + cur_value_masked = notit->default_clock_val & new_val_mask; - if (requested_new_value < cur_value_masked) { + if (new_val < cur_value_masked) { /* * It looks like a wrap happened on the number of bits * of the requested new value. Assume that the clock * value wrapped only one time. */ - *state += requested_new_value_mask + 1; + notit->default_clock_val += new_val_mask + 1; } /* Clear the low bits of the current clock value. */ - *state &= ~requested_new_value_mask; + notit->default_clock_val &= ~new_val_mask; /* Set the low bits of the current clock value. */ - *state |= requested_new_value; + notit->default_clock_val |= new_val; + end: - bt_put(value_type); + BT_LOGV("Updated default clock's value from integer field's value: " + "value=%" PRIu64, notit->default_clock_val); } static -enum bt_ctf_btr_status update_clock(struct bt_ctf_notif_iter *notit, - struct bt_ctf_field *int_field) +enum bt_btr_status btr_unsigned_int_cb(uint64_t value, + struct ctf_field_type *ft, void *data) { - gboolean clock_class_found; - uint64_t *clock_state; - struct bt_ctf_field_type *int_field_type = NULL; - enum bt_ctf_btr_status ret = BT_CTF_BTR_STATUS_OK; - struct bt_ctf_clock_class *clock_class = NULL; - - int_field_type = bt_ctf_field_get_type(int_field); - if (unlikely(!int_field_type)) { - goto end; - } + struct bt_notif_iter *notit = data; + enum bt_btr_status status = BT_BTR_STATUS_OK; + struct bt_field *field = NULL; + struct ctf_field_type_int *int_ft = (void *) ft; - clock_class = bt_ctf_field_type_integer_get_mapped_clock_class( - int_field_type); - if (likely(!clock_class)) { - goto end; - } + BT_LOGV("Unsigned integer function called from BTR: " + "notit-addr=%p, btr-addr=%p, ft-addr=%p, " + "ft-id=%d, ft-in-ir=%d, value=%" PRIu64, + notit, notit->btr, ft, ft->id, ft->in_ir, value); - clock_class_found = g_hash_table_lookup_extended(notit->clock_states, - clock_class, NULL, (gpointer) &clock_state); - if (unlikely(!clock_class_found)) { - const char *clock_class_name = - bt_ctf_clock_class_get_name(clock_class); - - PERR("Unknown clock class %s mapped to integer encountered in stream\n", - clock_class_name ? : "NULL"); - ret = BT_CTF_BTR_STATUS_ERROR; - goto end; + if (likely(int_ft->meaning == CTF_FIELD_TYPE_MEANING_NONE)) { + goto update_def_clock; } - if (unlikely(!clock_state)) { - clock_state = g_new0(uint64_t, 1); - if (!clock_state) { - ret = BT_CTF_BTR_STATUS_ENOMEM; + switch (int_ft->meaning) { + case CTF_FIELD_TYPE_MEANING_EVENT_CLASS_ID: + notit->cur_event_class_id = value; + break; + case CTF_FIELD_TYPE_MEANING_DATA_STREAM_ID: + notit->cur_data_stream_id = value; + break; + case CTF_FIELD_TYPE_MEANING_PACKET_BEGINNING_TIME: + notit->snapshots.beginning_clock = value; + break; + case CTF_FIELD_TYPE_MEANING_PACKET_END_TIME: + notit->snapshots.end_clock = value; + break; + case CTF_FIELD_TYPE_MEANING_STREAM_CLASS_ID: + notit->cur_stream_class_id = value; + break; + case CTF_FIELD_TYPE_MEANING_MAGIC: + if (value != 0xc1fc1fc1) { + BT_LOGW("Invalid CTF magic number: notit-addr=%p, " + "magic=%" PRIx64, notit, value); + status = BT_BTR_STATUS_ERROR; goto end; } - g_hash_table_insert(notit->clock_states, bt_get(clock_class), - clock_state); - } - /* Update the clock's state. */ - update_clock_state(clock_state, int_field); -end: - bt_put(int_field_type); - bt_put(clock_class); - return ret; -} - -static -enum bt_ctf_btr_status btr_unsigned_int_common(uint64_t value, - struct bt_ctf_field_type *type, void *data, - struct bt_ctf_field **out_int_field) -{ - enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK; - struct bt_ctf_field *field = NULL; - struct bt_ctf_field *int_field = NULL; - struct bt_ctf_notif_iter *notit = data; - int ret; - - /* Create next field */ - field = get_next_field(notit); - if (!field) { - PERR("Failed to get next field (unsigned int)\n"); - status = BT_CTF_BTR_STATUS_ERROR; - goto end_no_put; - } - - switch(bt_ctf_field_type_get_type_id(type)) { - case BT_CTF_FIELD_TYPE_ID_INTEGER: - /* Integer field is created field */ - BT_MOVE(int_field, field); - bt_get(type); break; - case BT_CTF_FIELD_TYPE_ID_ENUM: - int_field = bt_ctf_field_enumeration_get_container(field); - type = bt_ctf_field_get_type(int_field); + case CTF_FIELD_TYPE_MEANING_PACKET_COUNTER_SNAPSHOT: + notit->snapshots.packets = value; break; - default: - assert(0); - type = NULL; + case CTF_FIELD_TYPE_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT: + notit->snapshots.discarded_events = value; + break; + case CTF_FIELD_TYPE_MEANING_EXP_PACKET_TOTAL_SIZE: + notit->cur_exp_packet_total_size = value; + break; + case CTF_FIELD_TYPE_MEANING_EXP_PACKET_CONTENT_SIZE: + notit->cur_exp_packet_content_size = value; break; + default: + abort(); + } + +update_def_clock: + if (unlikely(int_ft->mapped_clock_class)) { + update_default_clock(notit, value, int_ft->base.size); } - if (!int_field) { - PERR("Failed to get integer field\n"); - status = BT_CTF_BTR_STATUS_ERROR; + if (unlikely(int_ft->storing_index >= 0)) { + g_array_index(notit->stored_values, uint64_t, + (uint64_t) int_ft->storing_index) = value; + } + + if (unlikely(!ft->in_ir)) { goto end; } - ret = bt_ctf_field_unsigned_integer_set_value(int_field, value); - assert(!ret); + field = borrow_next_field(notit); + BT_ASSERT(field); + BT_ASSERT(bt_field_borrow_type(field) == ft->ir_ft); + BT_ASSERT(bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_UNSIGNED_INTEGER || + bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_UNSIGNED_ENUMERATION); + bt_field_unsigned_integer_set_value(field, value); stack_top(notit->stack)->index++; - *out_int_field = int_field; end: - BT_PUT(field); - BT_PUT(type); -end_no_put: return status; } static -enum bt_ctf_btr_status btr_timestamp_end_cb(void *value, - struct bt_ctf_field_type *type, void *data) +enum bt_btr_status btr_unsigned_int_char_cb(uint64_t value, + struct ctf_field_type *ft, void *data) { - enum bt_ctf_btr_status status; - struct bt_ctf_field *field = NULL; - struct bt_ctf_notif_iter *notit = data; - - status = btr_unsigned_int_common(*((uint64_t *) value), type, data, - &field); + int ret; + struct bt_notif_iter *notit = data; + enum bt_btr_status status = BT_BTR_STATUS_OK; + struct bt_field *string_field = NULL; + struct ctf_field_type_int *int_ft = (void *) ft; + char str[2] = {'\0', '\0'}; + + BT_LOGV("Unsigned integer character function called from BTR: " + "notit-addr=%p, btr-addr=%p, ft-addr=%p, " + "ft-id=%d, ft-in-ir=%d, value=%" PRIu64, + notit, notit->btr, ft, ft->id, ft->in_ir, value); + BT_ASSERT(int_ft->meaning == CTF_FIELD_TYPE_MEANING_NONE); + BT_ASSERT(!int_ft->mapped_clock_class); + BT_ASSERT(int_ft->storing_index < 0); + + if (unlikely(!ft->in_ir)) { + goto end; + } - /* Set as the current packet's timestamp_end field. */ - BT_MOVE(notit->cur_timestamp_end, field); - return status; -} + if (notit->done_filling_string) { + goto end; + } -static -enum bt_ctf_btr_status btr_unsigned_int_cb(uint64_t value, - struct bt_ctf_field_type *type, void *data) -{ - struct bt_ctf_notif_iter *notit = data; - enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK; - struct bt_ctf_field *field = NULL; - struct field_cb_override *override; - - override = g_hash_table_lookup(notit->field_overrides, - type); - if (unlikely(override)) { - status = override->func(&value, type, override->data); + if (value == 0) { + notit->done_filling_string = true; goto end; } - status = btr_unsigned_int_common(value, type, data, &field); - if (status != BT_CTF_BTR_STATUS_OK) { + string_field = stack_top(notit->stack)->base; + BT_ASSERT(bt_field_get_type_id(string_field) == BT_FIELD_TYPE_ID_STRING); + + /* Append character */ + str[0] = (char) value; + ret = bt_field_string_append_with_length(string_field, str, 1); + if (ret) { + BT_LOGE("Cannot append character to string field's value: " + "notit-addr=%p, field-addr=%p, ret=%d", + notit, string_field, ret); + status = BT_BTR_STATUS_ERROR; goto end; } - status = update_clock(notit, field); - BT_PUT(field); end: return status; } static -enum bt_ctf_btr_status btr_signed_int_cb(int64_t value, - struct bt_ctf_field_type *type, void *data) +enum bt_btr_status btr_signed_int_cb(int64_t value, + struct ctf_field_type *ft, void *data) { - enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK; - struct bt_ctf_field *field = NULL; - struct bt_ctf_field *int_field = NULL; - struct bt_ctf_notif_iter *notit = data; - int ret; + enum bt_btr_status status = BT_BTR_STATUS_OK; + struct bt_field *field = NULL; + struct bt_notif_iter *notit = data; + struct ctf_field_type_int *int_ft = (void *) ft; - /* create next field */ - field = get_next_field(notit); - if (!field) { - PERR("Failed to get next field (signed int)\n"); - status = BT_CTF_BTR_STATUS_ERROR; - goto end_no_put; - } + BT_LOGV("Signed integer function called from BTR: " + "notit-addr=%p, btr-addr=%p, ft-addr=%p, " + "ft-id=%d, ft-in-ir=%d, value=%" PRId64, + notit, notit->btr, ft, ft->id, ft->in_ir, value); + BT_ASSERT(int_ft->meaning == CTF_FIELD_TYPE_MEANING_NONE); - switch(bt_ctf_field_type_get_type_id(type)) { - case BT_CTF_FIELD_TYPE_ID_INTEGER: - /* Integer field is created field */ - BT_MOVE(int_field, field); - bt_get(type); - break; - case BT_CTF_FIELD_TYPE_ID_ENUM: - int_field = bt_ctf_field_enumeration_get_container(field); - type = bt_ctf_field_get_type(int_field); - break; - default: - assert(0); - type = NULL; - break; + if (unlikely(int_ft->storing_index >= 0)) { + g_array_index(notit->stored_values, uint64_t, + (uint64_t) int_ft->storing_index) = (uint64_t) value; } - if (!int_field) { - PERR("Failed to get integer field\n"); - status = BT_CTF_BTR_STATUS_ERROR; + if (unlikely(!ft->in_ir)) { goto end; } - ret = bt_ctf_field_signed_integer_set_value(int_field, value); - assert(!ret); + field = borrow_next_field(notit); + BT_ASSERT(field); + BT_ASSERT(bt_field_borrow_type(field) == ft->ir_ft); + BT_ASSERT(bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_SIGNED_INTEGER || + bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_SIGNED_ENUMERATION); + bt_field_signed_integer_set_value(field, value); stack_top(notit->stack)->index++; - status = update_clock(notit, int_field); + end: - BT_PUT(field); - BT_PUT(int_field); - BT_PUT(type); -end_no_put: return status; } static -enum bt_ctf_btr_status btr_floating_point_cb(double value, - struct bt_ctf_field_type *type, void *data) -{ - enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK; - struct bt_ctf_field *field = NULL; - struct bt_ctf_notif_iter *notit = data; - int ret; - - /* Create next field */ - field = get_next_field(notit); - if (!field) { - PERR("Failed to get next field (floating point number)\n"); - status = BT_CTF_BTR_STATUS_ERROR; - goto end; - } - - ret = bt_ctf_field_floating_point_set_value(field, value); - assert(!ret); +enum bt_btr_status btr_floating_point_cb(double value, + struct ctf_field_type *ft, void *data) +{ + enum bt_btr_status status = BT_BTR_STATUS_OK; + struct bt_field *field = NULL; + struct bt_notif_iter *notit = data; + + BT_LOGV("Floating point number function called from BTR: " + "notit-addr=%p, btr-addr=%p, ft-addr=%p, " + "ft-id=%d, ft-in-ir=%d, value=%f", + notit, notit->btr, ft, ft->id, ft->in_ir, value); + BT_ASSERT(ft->in_ir); + field = borrow_next_field(notit); + BT_ASSERT(field); + BT_ASSERT(bt_field_borrow_type(field) == ft->ir_ft); + BT_ASSERT(bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_REAL); + bt_field_real_set_value(field, value); stack_top(notit->stack)->index++; - -end: - BT_PUT(field); - return status; } static -enum bt_ctf_btr_status btr_string_begin_cb( - struct bt_ctf_field_type *type, void *data) +enum bt_btr_status btr_string_begin_cb( + struct ctf_field_type *ft, void *data) { - enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK; - struct bt_ctf_field *field = NULL; - struct bt_ctf_notif_iter *notit = data; + struct bt_field *field = NULL; + struct bt_notif_iter *notit = data; int ret; - /* Create next field */ - field = get_next_field(notit); - if (!field) { - PERR("Failed to get next field (string)\n"); - status = BT_CTF_BTR_STATUS_ERROR; - goto end; - } + BT_LOGV("String (beginning) function called from BTR: " + "notit-addr=%p, btr-addr=%p, ft-addr=%p, " + "ft-id=%d, ft-in-ir=%d", + notit, notit->btr, ft, ft->id, ft->in_ir); - /* - * Push on stack. Not a compound type per se, but we know that only - * btr_string_cb() may be called between this call and a subsequent - * call to btr_string_end_cb(). - */ - ret = stack_push(notit->stack, field); - if (ret) { - PERR("Failed to push string field onto the stack\n"); - status = BT_CTF_BTR_STATUS_ERROR; - goto end; - } + BT_ASSERT(ft->in_ir); + field = borrow_next_field(notit); + BT_ASSERT(field); + BT_ASSERT(bt_field_borrow_type(field) == ft->ir_ft); + BT_ASSERT(bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_STRING); + ret = bt_field_string_clear(field); + BT_ASSERT(ret == 0); /* - * Initialize string field payload to an empty string since in the - * case of a length 0 string the btr_string_cb won't be called and - * we will end up with an unset string payload. + * Push on stack. Not a compound type per se, but we know that + * only btr_string_cb() may be called between this call and a + * subsequent call to btr_string_end_cb(). */ - ret = bt_ctf_field_string_set_value(field, ""); - if (ret) { - PERR("Failed to initialize string field\n"); - status = BT_CTF_BTR_STATUS_ERROR; - goto end; - } - -end: - BT_PUT(field); - - return status; + stack_push(notit->stack, field); + return BT_BTR_STATUS_OK; } static -enum bt_ctf_btr_status btr_string_cb(const char *value, - size_t len, struct bt_ctf_field_type *type, void *data) +enum bt_btr_status btr_string_cb(const char *value, + size_t len, struct ctf_field_type *ft, void *data) { - enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK; - struct bt_ctf_field *field = NULL; - struct bt_ctf_notif_iter *notit = data; + enum bt_btr_status status = BT_BTR_STATUS_OK; + struct bt_field *field = NULL; + struct bt_notif_iter *notit = data; int ret; - /* Get string field */ + BT_LOGV("String (substring) function called from BTR: " + "notit-addr=%p, btr-addr=%p, ft-addr=%p, " + "ft-id=%d, ft-in-ir=%d, string-length=%zu", + notit, notit->btr, ft, ft->id, ft->in_ir, + len); + BT_ASSERT(ft->in_ir); field = stack_top(notit->stack)->base; - assert(field); + BT_ASSERT(field); - /* Append current string */ - ret = bt_ctf_field_string_append_len(field, value, len); + /* Append current substring */ + ret = bt_field_string_append_with_length(field, value, len); if (ret) { - PERR("Failed to append a string to a string field\n"); - status = BT_CTF_BTR_STATUS_ERROR; + BT_LOGE("Cannot append substring to string field's value: " + "notit-addr=%p, field-addr=%p, string-length=%zu, " + "ret=%d", notit, field, len, ret); + status = BT_BTR_STATUS_ERROR; goto end; } @@ -1759,514 +2010,432 @@ end: } static -enum bt_ctf_btr_status btr_string_end_cb( - struct bt_ctf_field_type *type, void *data) +enum bt_btr_status btr_string_end_cb( + struct ctf_field_type *ft, void *data) { - struct bt_ctf_notif_iter *notit = data; + struct bt_notif_iter *notit = data; + + BT_LOGV("String (end) function called from BTR: " + "notit-addr=%p, btr-addr=%p, ft-addr=%p, " + "ft-id=%d, ft-in-ir=%d", + notit, notit->btr, ft, ft->id, ft->in_ir); + BT_ASSERT(ft->in_ir); /* Pop string field */ stack_pop(notit->stack); /* Go to next field */ stack_top(notit->stack)->index++; - - return BT_CTF_BTR_STATUS_OK; + return BT_BTR_STATUS_OK; } -enum bt_ctf_btr_status btr_compound_begin_cb( - struct bt_ctf_field_type *type, void *data) +enum bt_btr_status btr_compound_begin_cb( + struct ctf_field_type *ft, void *data) { - enum bt_ctf_btr_status status = BT_CTF_BTR_STATUS_OK; - struct bt_ctf_notif_iter *notit = data; - struct bt_ctf_field *field; - int ret; - - /* Create field */ - if (stack_empty(notit->stack)) { - /* Root: create dynamic scope field */ - *notit->cur_dscope_field = bt_ctf_field_create(type); - field = *notit->cur_dscope_field; + struct bt_notif_iter *notit = data; + struct bt_field *field; - /* - * Field will be put at the end of this function - * (stack_push() will take one reference, but this - * reference is lost upon the equivalent stack_pop() - * later), so also get it for our context to own it. - */ - bt_get(*notit->cur_dscope_field); - } else { - field = get_next_field(notit); - } + BT_LOGV("Compound (beginning) function called from BTR: " + "notit-addr=%p, btr-addr=%p, ft-addr=%p, " + "ft-id=%d, ft-in-ir=%d", + notit, notit->btr, ft, ft->id, ft->in_ir); - if (!field) { - PERR("Failed to get next field or create dynamic scope field\n"); - status = BT_CTF_BTR_STATUS_ERROR; + if (!ft->in_ir) { goto end; } - /* Push field */ - ret = stack_push(notit->stack, field); - if (ret) { - PERR("Failed to push compound field onto the stack\n"); - status = BT_CTF_BTR_STATUS_ERROR; - goto end; + /* Borrow field */ + if (stack_empty(notit->stack)) { + /* Root: already set by read_dscope_begin_state() */ + field = notit->cur_dscope_field; + } else { + field = borrow_next_field(notit); + BT_ASSERT(field); } -end: - BT_PUT(field); - - return status; -} - -enum bt_ctf_btr_status btr_compound_end_cb( - struct bt_ctf_field_type *type, void *data) -{ - struct bt_ctf_notif_iter *notit = data; + /* Push field */ + BT_ASSERT(field); + BT_ASSERT(bt_field_borrow_type(field) == ft->ir_ft); + stack_push(notit->stack, field); - assert(!stack_empty(notit->stack)); + /* + * Change BTR "unsigned int" callback if it's a text + * array/sequence. + */ + if (ft->id == CTF_FIELD_TYPE_ID_ARRAY || + ft->id == CTF_FIELD_TYPE_ID_SEQUENCE) { + struct ctf_field_type_array_base *array_ft = (void *) ft; - /* Pop stack */ - stack_pop(notit->stack); + if (array_ft->is_text) { + int ret; - /* If the stack is not empty, increment the base's index */ - if (!stack_empty(notit->stack)) { - stack_top(notit->stack)->index++; + BT_ASSERT(bt_field_get_type_id(field) == + BT_FIELD_TYPE_ID_STRING); + notit->done_filling_string = false; + ret = bt_field_string_clear(field); + BT_ASSERT(ret == 0); + bt_btr_set_unsigned_int_cb(notit->btr, + btr_unsigned_int_char_cb); + } } - return BT_CTF_BTR_STATUS_OK; +end: + return BT_BTR_STATUS_OK; } -static -struct bt_ctf_field *resolve_field(struct bt_ctf_notif_iter *notit, - struct bt_ctf_field_path *path) +enum bt_btr_status btr_compound_end_cb( + struct ctf_field_type *ft, void *data) { - struct bt_ctf_field *field = NULL; - unsigned int i; + struct bt_notif_iter *notit = data; - switch (bt_ctf_field_path_get_root_scope(path)) { - case BT_CTF_SCOPE_TRACE_PACKET_HEADER: - field = notit->dscopes.trace_packet_header; - break; - case BT_CTF_SCOPE_STREAM_PACKET_CONTEXT: - field = notit->dscopes.stream_packet_context; - break; - case BT_CTF_SCOPE_STREAM_EVENT_HEADER: - field = notit->dscopes.stream_event_header; - break; - case BT_CTF_SCOPE_STREAM_EVENT_CONTEXT: - field = notit->dscopes.stream_event_context; - break; - case BT_CTF_SCOPE_EVENT_CONTEXT: - field = notit->dscopes.event_context; - break; - case BT_CTF_SCOPE_EVENT_FIELDS: - field = notit->dscopes.event_payload; - break; - default: - break; - } + BT_LOGV("Compound (end) function called from BTR: " + "notit-addr=%p, btr-addr=%p, ft-addr=%p, " + "ft-id=%d, ft-in-ir=%d", + notit, notit->btr, ft, ft->id, ft->in_ir); - if (!field) { + if (!ft->in_ir) { goto end; } - bt_get(field); - - for (i = 0; i < bt_ctf_field_path_get_index_count(path); ++i) { - struct bt_ctf_field *next_field = NULL; - struct bt_ctf_field_type *field_type; - int index = bt_ctf_field_path_get_index(path, i); - - field_type = bt_ctf_field_get_type(field); - if (!field_type) { - BT_PUT(field); - goto end; - } + BT_ASSERT(!stack_empty(notit->stack)); + BT_ASSERT(bt_field_borrow_type(stack_top(notit->stack)->base) == + ft->ir_ft); - if (is_struct_type(field_type)) { - next_field = bt_ctf_field_structure_get_field_by_index( - field, index); - } else if (is_variant_type(field_type)) { - next_field = - bt_ctf_field_variant_get_current_field(field); - } - - BT_PUT(field); - BT_PUT(field_type); - - if (!next_field) { - goto end; + /* + * Reset BTR "unsigned int" callback if it's a text + * array/sequence. + */ + if (ft->id == CTF_FIELD_TYPE_ID_ARRAY || + ft->id == CTF_FIELD_TYPE_ID_SEQUENCE) { + struct ctf_field_type_array_base *array_ft = (void *) ft; + + if (array_ft->is_text) { + BT_ASSERT(bt_field_get_type_id( + stack_top(notit->stack)->base) == + BT_FIELD_TYPE_ID_STRING); + bt_btr_set_unsigned_int_cb(notit->btr, + btr_unsigned_int_cb); } - - /* Move next field -> field */ - BT_MOVE(field, next_field); } -end: - return field; -} - -static -int64_t btr_get_sequence_length_cb(struct bt_ctf_field_type *type, void *data) -{ - int64_t ret = -1; - int iret; - struct bt_ctf_field_path *field_path; - struct bt_ctf_notif_iter *notit = data; - struct bt_ctf_field *length_field = NULL; - uint64_t length; - - field_path = bt_ctf_field_type_sequence_get_length_field_path(type); - if (!field_path) { - goto end; - } - - length_field = resolve_field(notit, field_path); - if (!length_field) { - goto end; - } - - iret = bt_ctf_field_unsigned_integer_get_value(length_field, &length); - if (iret) { - goto end; - } + /* Pop stack */ + stack_pop(notit->stack); - iret = bt_ctf_field_sequence_set_length(stack_top(notit->stack)->base, - length_field); - if (iret) { - goto end; + /* If the stack is not empty, increment the base's index */ + if (!stack_empty(notit->stack)) { + stack_top(notit->stack)->index++; } - ret = (int64_t) length; end: - BT_PUT(length_field); - BT_PUT(field_path); - - return ret; + return BT_BTR_STATUS_OK; } static -struct bt_ctf_field_type *btr_get_variant_type_cb( - struct bt_ctf_field_type *type, void *data) +int64_t btr_get_sequence_length_cb(struct ctf_field_type *ft, void *data) { - struct bt_ctf_field_path *path; - struct bt_ctf_notif_iter *notit = data; - struct bt_ctf_field *tag_field = NULL; - struct bt_ctf_field *selected_field = NULL; - struct bt_ctf_field_type *selected_field_type = NULL; - - path = bt_ctf_field_type_variant_get_tag_field_path(type); - if (!path) { - goto end; - } - - tag_field = resolve_field(notit, path); - if (!tag_field) { - goto end; - } + struct bt_field *seq_field; + struct bt_notif_iter *notit = data; + struct ctf_field_type_sequence *seq_ft = (void *) ft; + int64_t length = -1; + int ret; - /* - * We found the enumeration tag field instance which should be - * able to select a current field for this variant. This - * callback function we're in is called _after_ - * compound_begin(), so the current stack top's base field is - * the variant field in question. We get the selected field here - * thanks to this tag field (thus creating the selected field), - * which will also provide us with its type. Then, this field - * will remain the current selected one until the next callback - * function call which is used to fill the current selected - * field. - */ - selected_field = bt_ctf_field_variant_get_field( - stack_top(notit->stack)->base, tag_field); - if (!selected_field) { - goto end; + length = (uint64_t) g_array_index(notit->stored_values, uint64_t, + seq_ft->stored_length_index); + seq_field = stack_top(notit->stack)->base; + BT_ASSERT(seq_field); + ret = bt_field_dynamic_array_set_length(seq_field, (uint64_t) length); + if (ret) { + BT_LOGE("Cannot set dynamic array field's length field: " + "notit-addr=%p, field-addr=%p, " + "length=%" PRIu64, notit, seq_field, length); } - selected_field_type = bt_ctf_field_get_type(selected_field); - -end: - BT_PUT(tag_field); - BT_PUT(selected_field); - BT_PUT(path); - - return selected_field_type; + return length; } static -int set_event_clocks(struct bt_ctf_event *event, - struct bt_ctf_notif_iter *notit) +struct ctf_field_type *btr_borrow_variant_selected_field_type_cb( + struct ctf_field_type *ft, void *data) { int ret; - GHashTableIter iter; - struct bt_ctf_clock_class *clock_class; - uint64_t *clock_state; - - g_hash_table_iter_init(&iter, notit->clock_states); + uint64_t i; + int64_t option_index = -1; + struct bt_notif_iter *notit = data; + struct ctf_field_type_variant *var_ft = (void *) ft; + struct ctf_named_field_type *selected_option = NULL; + struct ctf_field_type *ret_ft = NULL; + union { + uint64_t u; + int64_t i; + } tag; + + /* Get variant's tag */ + tag.u = g_array_index(notit->stored_values, uint64_t, + var_ft->stored_tag_index); - while (g_hash_table_iter_next(&iter, (gpointer) &clock_class, - (gpointer) &clock_state)) { - struct bt_ctf_clock_value *clock_value; - - clock_value = bt_ctf_clock_value_create(clock_class, - *clock_state); - if (!clock_value) { - ret = -1; - goto end; + /* + * Check each range to find the selected option's index. + */ + if (var_ft->tag_ft->base.is_signed) { + for (i = 0; i < var_ft->ranges->len; i++) { + struct ctf_field_type_variant_range *range = + ctf_field_type_variant_borrow_range_by_index( + var_ft, i); + + if (tag.i >= range->range.lower.i && + tag.i <= range->range.upper.i) { + option_index = (int64_t) range->option_index; + break; + } } - ret = bt_ctf_event_set_clock_value(event, clock_value); - bt_put(clock_value); - if (ret) { - goto end; + } else { + for (i = 0; i < var_ft->ranges->len; i++) { + struct ctf_field_type_variant_range *range = + ctf_field_type_variant_borrow_range_by_index( + var_ft, i); + + if (tag.u >= range->range.lower.u && + tag.u <= range->range.upper.u) { + option_index = (int64_t) range->option_index; + break; + } } } - ret = 0; -end: - return ret; -} - -static -struct bt_ctf_event *create_event(struct bt_ctf_notif_iter *notit) -{ - int ret; - struct bt_ctf_event *event; - - /* Create event object. */ - event = bt_ctf_event_create(notit->meta.event_class); - if (!event) { - goto error; - } - /* Set header, stream event context, context, and payload fields. */ - ret = bt_ctf_event_set_header(event, - notit->dscopes.stream_event_header); - if (ret) { - goto error; - } - - ret = bt_ctf_event_set_stream_event_context(event, - notit->dscopes.stream_event_context); - if (ret) { - goto error; + if (option_index < 0) { + BT_LOGW("Cannot find variant field type's option: " + "notit-addr=%p, var-ft-addr=%p, u-tag=%" PRIu64 ", " + "i-tag=%" PRId64, notit, var_ft, tag.u, tag.i); + goto end; } - ret = bt_ctf_event_set_event_context(event, - notit->dscopes.event_context); - if (ret) { - goto error; - } + selected_option = ctf_field_type_variant_borrow_option_by_index( + var_ft, (uint64_t) option_index); - ret = bt_ctf_event_set_event_payload(event, - notit->dscopes.event_payload); - if (ret) { - goto error; - } + if (selected_option->ft->in_ir) { + struct bt_field *var_field = stack_top(notit->stack)->base; - ret = set_event_clocks(event, notit); - if (ret) { - goto error; + ret = bt_field_variant_select_option_field(var_field, + option_index); + if (ret) { + BT_LOGW("Cannot select variant field's option field: " + "notit-addr=%p, var-field-addr=%p, " + "opt-index=%" PRId64, notit, var_field, + option_index); + goto end; + } } - /* Associate with current packet. */ - assert(notit->packet); - ret = bt_ctf_event_set_packet(event, notit->packet); - if (ret) { - goto error; - } + ret_ft = selected_option->ft; - goto end; -error: - BT_PUT(event); end: - return event; + return ret_ft; } static -void create_packet(struct bt_ctf_notif_iter *notit) +void set_event_default_clock_value(struct bt_notif_iter *notit) { - int ret; - struct bt_ctf_stream *stream = NULL; - struct bt_ctf_packet *packet = NULL; + struct bt_event *event = bt_notification_event_borrow_event( + notit->event_notif); + struct bt_stream_class *sc = notit->meta.sc->ir_sc; - /* Ask the user for the stream */ - stream = notit->medium.medops.get_stream(notit->meta.stream_class, - notit->medium.data); - if (!stream) { - goto error; - } + BT_ASSERT(event); - /* Create packet */ - packet = bt_ctf_packet_create(stream); - if (!packet) { - goto error; - } + if (bt_stream_class_borrow_default_clock_class(sc)) { + int ret = bt_event_set_default_clock_value(event, + notit->default_clock_val); - /* Set packet's context and header fields */ - if (notit->dscopes.trace_packet_header) { - ret = bt_ctf_packet_set_header(packet, - notit->dscopes.trace_packet_header); - if (ret) { - goto error; - } + BT_ASSERT(ret == 0); } - - if (notit->dscopes.stream_packet_context) { - ret = bt_ctf_packet_set_context(packet, - notit->dscopes.stream_packet_context); - if (ret) { - goto error; - } - } - - goto end; -error: - BT_PUT(packet); -end: - BT_MOVE(notit->packet, packet); } static -void notify_new_packet(struct bt_ctf_notif_iter *notit, +void notify_new_stream(struct bt_notif_iter *notit, struct bt_notification **notification) { - struct bt_notification *ret; + enum bt_notif_iter_status status; + struct bt_notification *ret = NULL; - /* Initialize the iterator's current packet */ - create_packet(notit); - if (!notit->packet) { - return; + status = set_current_stream(notit); + if (status != BT_NOTIF_ITER_STATUS_OK) { + BT_PUT(ret); + goto end; } - ret = bt_notification_packet_begin_create(notit->packet); + BT_ASSERT(notit->stream); + BT_ASSERT(notit->notif_iter); + ret = bt_notification_stream_begin_create(notit->notif_iter, + notit->stream); if (!ret) { + BT_LOGE("Cannot create stream beginning notification: " + "notit-addr=%p, stream-addr=%p", + notit, notit->stream); return; } + +end: *notification = ret; } static -void notify_end_of_packet(struct bt_ctf_notif_iter *notit, +void notify_end_of_stream(struct bt_notif_iter *notit, struct bt_notification **notification) { struct bt_notification *ret; - if (!notit->packet) { + if (!notit->stream) { + BT_LOGE("Cannot create stream for stream notification: " + "notit-addr=%p", notit); return; } - ret = bt_notification_packet_end_create(notit->packet); + BT_ASSERT(notit->notif_iter); + ret = bt_notification_stream_end_create(notit->notif_iter, + notit->stream); if (!ret) { + BT_LOGE("Cannot create stream beginning notification: " + "notit-addr=%p, stream-addr=%p", + notit, notit->stream); return; } - BT_PUT(notit->packet); *notification = ret; } static -void notify_event(struct bt_ctf_notif_iter *notit, - struct bt_clock_class_priority_map *cc_prio_map, +void notify_new_packet(struct bt_notif_iter *notit, struct bt_notification **notification) { - struct bt_ctf_event *event; - struct bt_notification *ret = NULL; + int ret; + enum bt_notif_iter_status status; + struct bt_notification *notif = NULL; + struct bt_stream_class *sc; - /* Create event */ - event = create_event(notit); - if (!event) { + status = set_current_packet(notit); + if (status != BT_NOTIF_ITER_STATUS_OK) { goto end; } - ret = bt_notification_event_create(event, cc_prio_map); - if (!ret) { - goto end; + BT_ASSERT(notit->packet); + sc = notit->meta.sc->ir_sc; + BT_ASSERT(sc); + + if (bt_stream_class_packets_have_discarded_event_counter_snapshot(sc)) { + BT_ASSERT(notit->snapshots.discarded_events != UINT64_C(-1)); + ret = bt_packet_set_discarded_event_counter_snapshot( + notit->packet, notit->snapshots.discarded_events); + BT_ASSERT(ret == 0); } - *notification = ret; -end: - BT_PUT(event); -} -static -int init_clock_states(GHashTable *clock_states, struct bt_ctf_trace *trace) -{ - int clock_class_count, i, ret = 0; + if (bt_stream_class_packets_have_packet_counter_snapshot(sc)) { + BT_ASSERT(notit->snapshots.packets != UINT64_C(-1)); + ret = bt_packet_set_packet_counter_snapshot( + notit->packet, notit->snapshots.packets); + BT_ASSERT(ret == 0); + } - clock_class_count = bt_ctf_trace_get_clock_class_count(trace); - if (clock_class_count <= 0) { - ret = -1; - goto end; + if (bt_stream_class_packets_have_default_beginning_clock_value(sc)) { + BT_ASSERT(notit->snapshots.beginning_clock != UINT64_C(-1)); + ret = bt_packet_set_default_beginning_clock_value( + notit->packet, notit->snapshots.beginning_clock); + BT_ASSERT(ret == 0); } - for (i = 0; i < clock_class_count; i++) { - struct bt_ctf_clock_class *clock_class; + if (bt_stream_class_packets_have_default_end_clock_value(sc)) { + BT_ASSERT(notit->snapshots.end_clock != UINT64_C(-1)); + ret = bt_packet_set_default_end_clock_value( + notit->packet, notit->snapshots.end_clock); + BT_ASSERT(ret == 0); + } - clock_class = bt_ctf_trace_get_clock_class_by_index(trace, i); - if (!clock_class) { - ret = -1; + if (notit->packet_header_field) { + ret = bt_packet_move_header_field(notit->packet, + notit->packet_header_field); + if (ret) { goto end; } - g_hash_table_insert(clock_states, bt_get(clock_class), NULL); - bt_put(clock_class); + notit->packet_header_field = NULL; + + /* + * At this point notit->dscopes.trace_packet_header has + * the same value as the packet header field within + * notit->packet. + */ + BT_ASSERT(bt_packet_borrow_header_field(notit->packet) == + notit->dscopes.trace_packet_header); } -end: - return ret; -} -static -void init_trace_field_path_cache(struct bt_ctf_trace *trace, - struct trace_field_path_cache *trace_field_path_cache) -{ - int stream_id = -1; - int stream_instance_id = -1; - int i, count; - struct bt_ctf_field_type *packet_header = NULL; + if (notit->packet_context_field) { + ret = bt_packet_move_context_field(notit->packet, + notit->packet_context_field); + if (ret) { + goto end; + } - packet_header = bt_ctf_trace_get_packet_header_type(trace); - if (!packet_header) { - goto end; + notit->packet_context_field = NULL; + + /* + * At this point notit->dscopes.trace_packet_header has + * the same value as the packet header field within + * notit->packet. + */ + BT_ASSERT(bt_packet_borrow_context_field(notit->packet) == + notit->dscopes.stream_packet_context); } - if (!bt_ctf_field_type_is_structure(packet_header)) { + BT_ASSERT(notit->notif_iter); + notif = bt_notification_packet_begin_create(notit->notif_iter, + notit->packet); + if (!notif) { + BT_LOGE("Cannot create packet beginning notification: " + "notit-addr=%p, packet-addr=%p", + notit, notit->packet); goto end; } - count = bt_ctf_field_type_structure_get_field_count(packet_header); - if (count < 0) { - goto end; + *notification = notif; + +end: + return; +} + +static +void notify_end_of_packet(struct bt_notif_iter *notit, + struct bt_notification **notification) +{ + struct bt_notification *notif; + + if (!notit->packet) { + return; } - for (i = 0; (i < count && (stream_id == -1 || stream_instance_id == -1)); i++) { - int ret; - const char *field_name; + /* Update default clock from packet's end time */ + if (notit->snapshots.end_clock != UINT64_C(-1)) { + notit->default_clock_val = notit->snapshots.end_clock; + } - ret = bt_ctf_field_type_structure_get_field(packet_header, - &field_name, NULL, i); - if (ret) { - goto end; - } + BT_ASSERT(notit->notif_iter); + notif = bt_notification_packet_end_create(notit->notif_iter, + notit->packet); + if (!notif) { + BT_LOGE("Cannot create packet end notification: " + "notit-addr=%p, packet-addr=%p", + notit, notit->packet); + return; - if (stream_id == -1 && !strcmp(field_name, "stream_id")) { - stream_id = i; - } else if (stream_instance_id == -1 && - !strcmp(field_name, "stream_instance_id")) { - stream_instance_id = i; - } } -end: - trace_field_path_cache->stream_id = stream_id; - trace_field_path_cache->stream_instance_id = stream_instance_id; - BT_PUT(packet_header); + + BT_PUT(notit->packet); + *notification = notif; } BT_HIDDEN -struct bt_ctf_notif_iter *bt_ctf_notif_iter_create(struct bt_ctf_trace *trace, +struct bt_notif_iter *bt_notif_iter_create(struct ctf_trace_class *tc, size_t max_request_sz, - struct bt_ctf_notif_iter_medium_ops medops, - void *data, FILE *err_stream) + struct bt_notif_iter_medium_ops medops, void *data) { - int ret; - struct bt_ctf_notif_iter *notit = NULL; - struct bt_ctf_btr_cbs cbs = { + struct bt_notif_iter *notit = NULL; + struct bt_btr_cbs cbs = { .types = { .signed_int = btr_signed_int_cb, .unsigned_int = btr_unsigned_int_cb, @@ -2279,164 +2448,173 @@ struct bt_ctf_notif_iter *bt_ctf_notif_iter_create(struct bt_ctf_trace *trace, }, .query = { .get_sequence_length = btr_get_sequence_length_cb, - .get_variant_type = btr_get_variant_type_cb, + .borrow_variant_selected_field_type = btr_borrow_variant_selected_field_type_cb, }, }; - assert(trace); - assert(medops.request_bytes); - assert(medops.get_stream); - notit = g_new0(struct bt_ctf_notif_iter, 1); + BT_ASSERT(tc); + BT_ASSERT(medops.request_bytes); + BT_ASSERT(medops.borrow_stream); + BT_LOGD("Creating CTF plugin notification iterator: " + "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, " + "data=%p", tc, tc->name->str, max_request_sz, data); + notit = g_new0(struct bt_notif_iter, 1); if (!notit) { - PERR("Failed to allocate memory for CTF notification iterator\n"); + BT_LOGE_STR("Failed to allocate one CTF plugin notification iterator."); goto end; } - notit->clock_states = g_hash_table_new_full(g_direct_hash, - g_direct_equal, bt_put, g_free); - if (!notit->clock_states) { - PERR("Failed to create hash table\n"); - goto error; - } - ret = init_clock_states(notit->clock_states, trace); - if (ret) { - PERR("Failed to initialize stream clock states\n"); - goto error; - } - notit->meta.trace = bt_get(trace); + notit->meta.tc = tc; notit->medium.medops = medops; notit->medium.max_request_sz = max_request_sz; notit->medium.data = data; - notit->err_stream = err_stream; notit->stack = stack_new(notit); + notit->stored_values = g_array_new(FALSE, TRUE, sizeof(uint64_t)); + g_array_set_size(notit->stored_values, tc->stored_value_count); + if (!notit->stack) { - PERR("Failed to create stack\n"); + BT_LOGE_STR("Failed to create field stack."); goto error; } - notit->btr = bt_ctf_btr_create(cbs, notit, err_stream); + notit->btr = bt_btr_create(cbs, notit); if (!notit->btr) { - PERR("Failed to create binary type reader\n"); - goto error; - } - - bt_ctf_notif_iter_reset(notit); - - init_trace_field_path_cache(trace, ¬it->trace_field_path_cache); - notit->sc_field_path_caches = g_hash_table_new_full(g_direct_hash, - g_direct_equal, bt_put, g_free); - if (!notit->sc_field_path_caches) { - PERR("Failed to create stream class field path caches\n"); + BT_LOGE_STR("Failed to create binary type reader (BTR)."); goto error; } - notit->field_overrides = g_hash_table_new_full(g_direct_hash, - g_direct_equal, bt_put, g_free); - if (!notit->field_overrides) { - goto error; - } + bt_notif_iter_reset(notit); + BT_LOGD("Created CTF plugin notification iterator: " + "trace-addr=%p, trace-name=\"%s\", max-request-size=%zu, " + "data=%p, notit-addr=%p", + tc, tc->name->str, max_request_sz, data, + notit); + notit->cur_packet_offset = 0; end: return notit; + error: - bt_ctf_notif_iter_destroy(notit); + bt_notif_iter_destroy(notit); notit = NULL; goto end; } -void bt_ctf_notif_iter_destroy(struct bt_ctf_notif_iter *notit) +void bt_notif_iter_destroy(struct bt_notif_iter *notit) { - BT_PUT(notit->meta.trace); - BT_PUT(notit->meta.stream_class); - BT_PUT(notit->meta.event_class); BT_PUT(notit->packet); - BT_PUT(notit->cur_timestamp_end); - put_all_dscopes(notit); + BT_PUT(notit->stream); + release_all_dscopes(notit); + + BT_LOGD("Destroying CTF plugin notification iterator: addr=%p", notit); if (notit->stack) { + BT_LOGD_STR("Destroying field stack."); stack_destroy(notit->stack); } if (notit->btr) { - bt_ctf_btr_destroy(notit->btr); - } - - if (notit->clock_states) { - g_hash_table_destroy(notit->clock_states); + BT_LOGD("Destroying BTR: btr-addr=%p", notit->btr); + bt_btr_destroy(notit->btr); } - if (notit->sc_field_path_caches) { - g_hash_table_destroy(notit->sc_field_path_caches); + if (notit->stored_values) { + g_array_free(notit->stored_values, TRUE); } - if (notit->field_overrides) { - g_hash_table_destroy(notit->field_overrides); - } g_free(notit); } -enum bt_ctf_notif_iter_status bt_ctf_notif_iter_get_next_notification( - struct bt_ctf_notif_iter *notit, - struct bt_clock_class_priority_map *cc_prio_map, +enum bt_notif_iter_status bt_notif_iter_get_next_notification( + struct bt_notif_iter *notit, + struct bt_private_connection_private_notification_iterator *notif_iter, struct bt_notification **notification) { - enum bt_ctf_notif_iter_status status = BT_CTF_NOTIF_ITER_STATUS_OK; + enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; + + BT_ASSERT(notit); + BT_ASSERT(notification); + + if (notit->state == STATE_DONE) { + status = BT_NOTIF_ITER_STATUS_EOF; + goto end; + } - assert(notit); - assert(notification); + notit->notif_iter = notif_iter; + + BT_LOGV("Getting next notification: notit-addr=%p", notit); while (true) { status = handle_state(notit); - if (status == BT_CTF_NOTIF_ITER_STATUS_AGAIN) { - PDBG("Medium operation reported \"try again later\""); + if (status == BT_NOTIF_ITER_STATUS_AGAIN) { + BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_AGAIN."); goto end; } - if (status != BT_CTF_NOTIF_ITER_STATUS_OK) { - if (status == BT_CTF_NOTIF_ITER_STATUS_EOF) { - PDBG("Medium operation reported end of stream\n"); + + if (status != BT_NOTIF_ITER_STATUS_OK) { + if (status == BT_NOTIF_ITER_STATUS_EOF) { + enum state next_state = notit->state; + + BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_EOF."); + + if (notit->packet) { + notify_end_of_packet(notit, + notification); + } else { + notify_end_of_stream(notit, + notification); + next_state = STATE_DONE; + } + + if (!*notification) { + status = BT_NOTIF_ITER_STATUS_ERROR; + goto end; + } + + status = BT_NOTIF_ITER_STATUS_OK; + notit->state = next_state; } else { - PERR("Failed to handle state:\n"); - PERR("\tState: %d\n", notit->state); + BT_LOGW("Cannot handle state: " + "notit-addr=%p, state=%s", + notit, state_string(notit->state)); } + goto end; } switch (notit->state) { + case STATE_EMIT_NOTIF_NEW_STREAM: + /* notify_new_stream() logs errors */ + notify_new_stream(notit, notification); + + if (!*notification) { + status = BT_NOTIF_ITER_STATUS_ERROR; + } + + notit->stream_begin_emitted = true; + goto end; case STATE_EMIT_NOTIF_NEW_PACKET: - PDBG("Emitting new packet notification\n"); + /* notify_new_packet() logs errors */ notify_new_packet(notit, notification); + if (!*notification) { - status = BT_CTF_NOTIF_ITER_STATUS_ERROR; + status = BT_NOTIF_ITER_STATUS_ERROR; } + goto end; case STATE_EMIT_NOTIF_EVENT: - PDBG("Emitting event notification\n"); - notify_event(notit, cc_prio_map, notification); - if (!*notification) { - status = BT_CTF_NOTIF_ITER_STATUS_ERROR; - } + BT_ASSERT(notit->event_notif); + set_event_default_clock_value(notit); + *notification = notit->event_notif; + notit->event_notif = NULL; goto end; case STATE_EMIT_NOTIF_END_OF_PACKET: - /* Update clock with timestamp_end field. */ - if (notit->cur_timestamp_end) { - enum bt_ctf_btr_status btr_status; - struct bt_ctf_field_type *field_type = - bt_ctf_field_get_type( - notit->cur_timestamp_end); - - btr_status = update_clock(notit, - notit->cur_timestamp_end); - BT_PUT(field_type); - if (btr_status != BT_CTF_BTR_STATUS_OK) { - status = BT_CTF_NOTIF_ITER_STATUS_ERROR; - goto end; - } - } - - PDBG("Emitting end of packet notification\n"); + /* notify_end_of_packet() logs errors */ notify_end_of_packet(notit, notification); + if (!*notification) { - status = BT_CTF_NOTIF_ITER_STATUS_ERROR; + status = BT_NOTIF_ITER_STATUS_ERROR; } + goto end; default: /* Non-emitting state: continue */ @@ -2447,3 +2625,175 @@ enum bt_ctf_notif_iter_status bt_ctf_notif_iter_get_next_notification( end: return status; } + +BT_HIDDEN +enum bt_notif_iter_status bt_notif_iter_borrow_packet_header_context_fields( + struct bt_notif_iter *notit, + struct bt_field **packet_header_field, + struct bt_field **packet_context_field) +{ + int ret; + enum bt_notif_iter_status status = BT_NOTIF_ITER_STATUS_OK; + + BT_ASSERT(notit); + + if (notit->state == STATE_EMIT_NOTIF_NEW_PACKET) { + /* We're already there */ + goto set_fields; + } + + while (true) { + status = handle_state(notit); + if (status == BT_NOTIF_ITER_STATUS_AGAIN) { + BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_AGAIN."); + goto end; + } + if (status != BT_NOTIF_ITER_STATUS_OK) { + if (status == BT_NOTIF_ITER_STATUS_EOF) { + BT_LOGV_STR("Medium returned BT_NOTIF_ITER_STATUS_EOF."); + } else { + BT_LOGW("Cannot handle state: " + "notit-addr=%p, state=%s", + notit, state_string(notit->state)); + } + goto end; + } + + switch (notit->state) { + case STATE_EMIT_NOTIF_NEW_PACKET: + /* + * Packet header and context fields are + * potentially decoded (or they don't exist). + */ + goto set_fields; + case STATE_INIT: + case STATE_EMIT_NOTIF_NEW_STREAM: + case STATE_DSCOPE_TRACE_PACKET_HEADER_BEGIN: + case STATE_DSCOPE_TRACE_PACKET_HEADER_CONTINUE: + case STATE_AFTER_TRACE_PACKET_HEADER: + case STATE_DSCOPE_STREAM_PACKET_CONTEXT_BEGIN: + case STATE_DSCOPE_STREAM_PACKET_CONTEXT_CONTINUE: + case STATE_AFTER_STREAM_PACKET_CONTEXT: + /* Non-emitting state: continue */ + break; + default: + /* + * We should never get past the + * STATE_EMIT_NOTIF_NEW_PACKET state. + */ + BT_LOGF("Unexpected state: notit-addr=%p, state=%s", + notit, state_string(notit->state)); + abort(); + } + } + +set_fields: + ret = set_current_packet_content_sizes(notit); + if (ret) { + status = BT_NOTIF_ITER_STATUS_ERROR; + goto end; + } + + if (packet_header_field) { + *packet_header_field = notit->dscopes.trace_packet_header; + } + + if (packet_context_field) { + *packet_context_field = notit->dscopes.stream_packet_context; + } + +end: + return status; +} + +BT_HIDDEN +void bt_notif_iter_set_medops_data(struct bt_notif_iter *notit, + void *medops_data) +{ + BT_ASSERT(notit); + notit->medium.data = medops_data; +} + +BT_HIDDEN +enum bt_notif_iter_status bt_notif_iter_seek( + struct bt_notif_iter *notit, off_t offset) +{ + enum bt_notif_iter_status ret = BT_NOTIF_ITER_STATUS_OK; + enum bt_notif_iter_medium_status medium_status; + + BT_ASSERT(notit); + if (offset < 0) { + BT_LOGE("Cannot seek to negative offset: offset=%jd", offset); + ret = BT_NOTIF_ITER_STATUS_INVAL; + goto end; + } + + if (!notit->medium.medops.seek) { + ret = BT_NOTIF_ITER_STATUS_UNSUPPORTED; + BT_LOGD("Aborting seek as the iterator's underlying media does not implement seek support."); + goto end; + } + + medium_status = notit->medium.medops.seek( + BT_NOTIF_ITER_SEEK_WHENCE_SET, offset, notit->medium.data); + if (medium_status != BT_NOTIF_ITER_MEDIUM_STATUS_OK) { + if (medium_status == BT_NOTIF_ITER_MEDIUM_STATUS_EOF) { + ret = BT_NOTIF_ITER_STATUS_EOF; + } else { + ret = BT_NOTIF_ITER_STATUS_ERROR; + goto end; + } + } + + bt_notif_iter_reset(notit); + notit->cur_packet_offset = offset; + +end: + return ret; +} + +BT_HIDDEN +off_t bt_notif_iter_get_current_packet_offset(struct bt_notif_iter *notit) +{ + BT_ASSERT(notit); + return notit->cur_packet_offset; +} + +BT_HIDDEN +off_t bt_notif_iter_get_current_packet_size( + struct bt_notif_iter *notit) +{ + BT_ASSERT(notit); + return notit->cur_exp_packet_total_size; +} + +BT_HIDDEN +void bt_notif_trace_class_changed(struct bt_notif_iter *notit) +{ + if (notit->meta.tc->stored_value_count > notit->stored_values->len) { + g_array_set_size(notit->stored_values, + notit->meta.tc->stored_value_count); + } +} + +BT_HIDDEN +enum bt_notif_iter_status bt_notif_iter_get_packet_properties( + struct bt_notif_iter *notit, + struct bt_notif_iter_packet_properties *props) +{ + BT_ASSERT(notit); + BT_ASSERT(props); + + props->exp_packet_total_size = + (uint64_t) notit->cur_exp_packet_total_size; + props->exp_packet_content_size = + (uint64_t) notit->cur_exp_packet_content_size; + BT_ASSERT(props->stream_class_id >= 0); + props->stream_class_id = (uint64_t) notit->cur_stream_class_id; + props->data_stream_id = notit->cur_data_stream_id; + props->snapshots.discarded_events = notit->snapshots.discarded_events; + props->snapshots.packets = notit->snapshots.packets; + props->snapshots.beginning_clock = notit->snapshots.beginning_clock; + props->snapshots.end_clock = notit->snapshots.end_clock; + return BT_NOTIF_ITER_STATUS_OK; +}