From 9843982dca5a80c001fc5989a9ce5a2283b0740f Mon Sep 17 00:00:00 2001 From: Julien Desfossez Date: Thu, 16 Feb 2012 11:53:44 -0500 Subject: [PATCH] API : Access CTF events fields Update the iterator code to return a bt_ctf_event. Access to field list within a scope. Access to field values depending on type. [ Edit by Mathieu Desnoyers: minor updates: uint64_t for timestamp, and style fixes. ] Signed-off-by: Julien Desfossez Signed-off-by: Mathieu Desnoyers --- converter/babeltrace.c | 8 +- include/Makefile.am | 3 +- include/babeltrace/ctf/events.h | 149 ++++++++++++ include/babeltrace/iterator-internal.h | 3 + include/babeltrace/iterator.h | 7 +- include/babeltrace/types.h | 15 +- lib/Makefile.am | 3 +- lib/ctf-events.c | 305 +++++++++++++++++++++++++ lib/iterator.c | 21 +- 9 files changed, 480 insertions(+), 34 deletions(-) create mode 100644 include/babeltrace/ctf/events.h create mode 100644 lib/ctf-events.c diff --git a/converter/babeltrace.c b/converter/babeltrace.c index 539734ee..65220183 100644 --- a/converter/babeltrace.c +++ b/converter/babeltrace.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -397,10 +398,9 @@ int convert_trace(struct trace_descriptor *td_write, struct bt_context *ctx) { struct bt_iter *iter; - struct ctf_stream *stream; - struct ctf_stream_event *event; struct ctf_text_stream_pos *sout; struct bt_iter_pos begin_pos; + struct bt_ctf_event *ctf_event; int ret; sout = container_of(td_write, struct ctf_text_stream_pos, @@ -412,8 +412,8 @@ int convert_trace(struct trace_descriptor *td_write, ret = -1; goto error_iter; } - while (bt_iter_read_event(iter, &stream, &event) == 0) { - ret = sout->parent.event_cb(&sout->parent, stream); + while ((ctf_event = bt_iter_read_ctf_event(iter))) { + ret = sout->parent.event_cb(&sout->parent, ctf_event->stream); if (ret) { fprintf(stderr, "[error] Writing event failed.\n"); goto end; diff --git a/include/Makefile.am b/include/Makefile.am index 12b89470..4ad0af4c 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -4,7 +4,8 @@ babeltraceinclude_HEADERS = \ babeltrace/context.h \ babeltrace/iterator.h \ babeltrace/trace-collection.h \ - babeltrace/trace-handle.h + babeltrace/trace-handle.h \ + babeltrace/ctf/events.h noinst_HEADERS = \ diff --git a/include/babeltrace/ctf/events.h b/include/babeltrace/ctf/events.h new file mode 100644 index 00000000..eb644125 --- /dev/null +++ b/include/babeltrace/ctf/events.h @@ -0,0 +1,149 @@ +#ifndef _BABELTRACE_CTF_EVENTS_H +#define _BABELTRACE_CTF_EVENTS_H + +/* + * BabelTrace + * + * CTF events API + * + * Copyright 2011-2012 EfficiOS Inc. and Linux Foundation + * + * Author: Mathieu Desnoyers + * Julien Desfossez + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + */ + +#include + +struct ctf_stream; +struct ctf_stream_event; +struct definition; + +/* + * the top-level scopes in CTF + */ +enum bt_ctf_scope { + BT_TRACE_PACKET_HEADER = 0, + BT_STREAM_PACKET_CONTEXT = 1, + BT_STREAM_EVENT_HEADER = 2, + BT_STREAM_EVENT_CONTEXT = 3, + BT_EVENT_CONTEXT = 4, + BT_EVENT_FIELDS = 5, +}; + +/* + * the supported CTF types + */ +enum ctf_type_id { + CTF_TYPE_UNKNOWN = 0, + CTF_TYPE_INTEGER, + CTF_TYPE_FLOAT, + CTF_TYPE_ENUM, + CTF_TYPE_STRING, + CTF_TYPE_STRUCT, + CTF_TYPE_UNTAGGED_VARIANT, + CTF_TYPE_VARIANT, + CTF_TYPE_ARRAY, + CTF_TYPE_SEQUENCE, + NR_CTF_TYPES, +}; + +/* + * the structure to manipulate events + */ +struct bt_ctf_event { + struct ctf_stream *stream; + struct ctf_stream_event *event; +}; + +/* + * bt_ctf_get_top_level_scope: return a definition of the top-level scope + * + * Top-level scopes are defined in the bt_ctf_scope enum. + * In order to get a field or a field list, the user needs to pass a + * scope as argument, this scope can be a top-level scope or a scope + * relative to an arbitrary field. This function provides the mapping + * between the enum and the actual definition of top-level scopes. + * On error return NULL. + */ +struct definition *bt_ctf_get_top_level_scope(struct bt_ctf_event *event, + enum bt_ctf_scope scope); + +/* + * bt_ctf_event_get_name: returns the name of the event or NULL on error + */ +const char *bt_ctf_event_name(struct bt_ctf_event *event); + +/* + * bt_ctf_get_timestamp: returns the timestamp of the event or -1ULL on error + */ +uint64_t bt_ctf_get_timestamp(struct bt_ctf_event *event); + +/* + * bt_ctf_get_field_list: returns an array of *def pointing to each field of + * the event. The array is NULL terminated. + * On error : return NULL. + */ +int bt_ctf_get_field_list(struct bt_ctf_event *event, + struct definition *scope, + struct definition const * const **list, + unsigned int *count); + +/* + * bt_ctf_get_field: returns the definition of a specific field + */ +struct definition *bt_ctf_get_field(struct bt_ctf_event *event, + struct definition *scope, + const char *field); + +/* + * bt_ctf_get_index: if the field is an array or a sequence, return the element + * at position index, otherwise return NULL; + */ +struct definition *bt_ctf_get_index(struct bt_ctf_event *event, + struct definition *field, + unsigned int index); + +/* + * bt_ctf_field_name: returns the name of a field or NULL on error + */ +const char *bt_ctf_field_name(const struct definition *def); + +/* + * bt_ctf_field_type: returns the type of a field or -1 if unknown + */ +enum ctf_type_id bt_ctf_field_type(struct definition *def); + +/* + * Field access functions + * + * These functions return the value associated with the field passed in + * parameter. + * + * If the field does not exist or is not of the type requested, the value + * returned is undefined. To check if an error occured, use the + * bt_ctf_field_error() function after accessing a field. + */ +uint64_t bt_ctf_get_uint64(struct definition *field); +int64_t bt_ctf_get_int64(struct definition *field); +char *bt_ctf_get_char_array(struct definition *field); +char *bt_ctf_get_string(struct definition *field); + +/* + * bt_ctf_field_error: returns the last error code encountered while + * accessing a field and reset the error flag. + * Return 0 if no error, a negative value otherwise. + */ +int bt_ctf_field_get_error(void); + +#endif /* _BABELTRACE_CTF_EVENTS_H */ diff --git a/include/babeltrace/iterator-internal.h b/include/babeltrace/iterator-internal.h index 9cbc79cf..121d0dcc 100644 --- a/include/babeltrace/iterator-internal.h +++ b/include/babeltrace/iterator-internal.h @@ -21,6 +21,8 @@ * all copies or substantial portions of the Software. */ +#include + /* * struct bt_iter: data structure representing an iterator on a trace * collection. @@ -29,6 +31,7 @@ struct bt_iter { struct ptr_heap *stream_heap; struct bt_context *ctx; struct bt_iter_pos *end_pos; + struct bt_ctf_event current_ctf_event; /* last read event */ GArray *callbacks; /* Array of struct bt_stream_callbacks */ struct bt_callback_chain main_callbacks; /* For all events */ /* diff --git a/include/babeltrace/iterator.h b/include/babeltrace/iterator.h index 0acd91d4..3dcf0825 100644 --- a/include/babeltrace/iterator.h +++ b/include/babeltrace/iterator.h @@ -19,6 +19,7 @@ #include #include +#include /* Forward declarations */ struct bt_iter; @@ -107,15 +108,13 @@ struct bt_iter_pos *bt_iter_create_time_pos(struct bt_iter *iter, uint64_t timestamp); /* - * bt_iter_read_event: Read the iterator's current event data. + * bt_iter_read_ctf_event: Read the iterator's current event data. * * @iter: trace collection iterator (input) * @stream: stream containing event at current position (output) * @event: current event (output) * Return 0 on success, negative error value on error. */ -int bt_iter_read_event(struct bt_iter *iter, - struct ctf_stream **stream, - struct ctf_stream_event **event); +struct bt_ctf_event *bt_iter_read_ctf_event(struct bt_iter *iter); #endif /* _BABELTRACE_ITERATOR_H */ diff --git a/include/babeltrace/types.h b/include/babeltrace/types.h index b4d2d4a7..e3c419d1 100644 --- a/include/babeltrace/types.h +++ b/include/babeltrace/types.h @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -69,20 +70,6 @@ struct definition_scope { GArray *scope_path; /* array of GQuark */ }; -enum ctf_type_id { - CTF_TYPE_UNKNOWN = 0, - CTF_TYPE_INTEGER, - CTF_TYPE_FLOAT, - CTF_TYPE_ENUM, - CTF_TYPE_STRING, - CTF_TYPE_STRUCT, - CTF_TYPE_UNTAGGED_VARIANT, - CTF_TYPE_VARIANT, - CTF_TYPE_ARRAY, - CTF_TYPE_SEQUENCE, - NR_CTF_TYPES, -}; - struct declaration { enum ctf_type_id id; size_t alignment; /* type alignment, in bits */ diff --git a/lib/Makefile.am b/lib/Makefile.am index 56068fd8..079ed13e 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -9,7 +9,8 @@ libbabeltrace_la_SOURCES = babeltrace.c \ iterator.c \ context.c \ trace-handle.c \ - trace-collection.c + trace-collection.c \ + ctf-events.c libbabeltrace_la_LIBADD = \ $(top_builddir)/types/libbabeltrace_types.la \ diff --git a/lib/ctf-events.c b/lib/ctf-events.c new file mode 100644 index 00000000..dbb0cd66 --- /dev/null +++ b/lib/ctf-events.c @@ -0,0 +1,305 @@ +/* + * ctf_events.c + * + * Babeltrace Library + * + * Copyright 2011-2012 EfficiOS Inc. and Linux Foundation + * + * Author: Mathieu Desnoyers + * Julien Desfossez + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + */ + +#include +#include +#include +#include +#include + +/* + * thread local storage to store the last error that occured + * while reading a field, this variable must be accessed by + * bt_ctf_field_error only + */ +__thread int bt_ctf_last_field_error = 0; + +struct definition *bt_ctf_get_top_level_scope(struct bt_ctf_event *event, + enum bt_ctf_scope scope) +{ + struct definition *tmp = NULL; + + switch (scope) { + case BT_TRACE_PACKET_HEADER: + if (!event->stream) + goto error; + if (event->stream->trace_packet_header) + tmp = &event->stream->trace_packet_header->p; + break; + case BT_STREAM_PACKET_CONTEXT: + if (!event->stream) + goto error; + if (event->stream->stream_packet_context) + tmp = &event->stream->stream_packet_context->p; + break; + case BT_STREAM_EVENT_HEADER: + if (!event->stream) + goto error; + if (event->stream->stream_event_header) + tmp = &event->stream->stream_event_header->p; + break; + case BT_STREAM_EVENT_CONTEXT: + if (!event->stream) + goto error; + if (event->stream->stream_event_context) + tmp = &event->stream->stream_event_context->p; + break; + case BT_EVENT_CONTEXT: + if (!event->event) + goto error; + if (event->event->event_context) + tmp = &event->event->event_context->p; + break; + case BT_EVENT_FIELDS: + if (!event->event) + goto error; + if (event->event->event_fields) + tmp = &event->event->event_fields->p; + break; + } + return tmp; + +error: + return NULL; +} + +struct definition *bt_ctf_get_field(struct bt_ctf_event *event, + struct definition *scope, + const char *field) +{ + struct definition *def; + + if (scope) { + def = lookup_definition(scope, field); + if (bt_ctf_field_type(def) == CTF_TYPE_VARIANT) { + struct definition_variant *variant_definition; + variant_definition = container_of(def, + struct definition_variant, p); + return variant_definition->current_field; + } + return def; + } + return NULL; +} + +struct definition *bt_ctf_get_index(struct bt_ctf_event *event, + struct definition *field, + unsigned int index) +{ + struct definition *ret = NULL; + + if (bt_ctf_field_type(field) == CTF_TYPE_ARRAY) { + struct definition_array *array_definition; + array_definition = container_of(field, + struct definition_array, p); + ret = array_index(array_definition, index); + } else if (bt_ctf_field_type(field) == CTF_TYPE_SEQUENCE) { + struct definition_sequence *sequence_definition; + sequence_definition = container_of(field, + struct definition_sequence, p); + ret = sequence_index(sequence_definition, index); + } + return ret; +} + +const char *bt_ctf_event_name(struct bt_ctf_event *event) +{ + struct ctf_event *event_class; + struct ctf_stream_class *stream_class; + + if (!event) + return NULL; + stream_class = event->stream->stream_class; + event_class = g_ptr_array_index(stream_class->events_by_id, + event->stream->event_id); + return g_quark_to_string(event_class->name); +} + +const char *bt_ctf_field_name(const struct definition *def) +{ + if (def) + return g_quark_to_string(def->name); + return NULL; +} + +enum ctf_type_id bt_ctf_field_type(struct definition *def) +{ + if (def) + return def->declaration->id; + return CTF_TYPE_UNKNOWN; +} + +int bt_ctf_get_field_list(struct bt_ctf_event *event, + struct definition *scope, + struct definition const * const **list, + unsigned int *count) +{ + switch (bt_ctf_field_type(scope)) { + case CTF_TYPE_INTEGER: + case CTF_TYPE_FLOAT: + case CTF_TYPE_STRING: + case CTF_TYPE_ENUM: + goto error; + case CTF_TYPE_STRUCT: + { + struct definition_struct *def_struct; + + def_struct = container_of(scope, struct definition_struct, p); + if (!def_struct) + goto error; + if (def_struct->fields->pdata) { + *list = (struct definition const* const*) def_struct->fields->pdata; + *count = def_struct->fields->len; + goto end; + } else { + goto error; + } + } + case CTF_TYPE_UNTAGGED_VARIANT: + goto error; + case CTF_TYPE_VARIANT: + { + struct definition_variant *def_variant; + + def_variant = container_of(scope, struct definition_variant, p); + if (!def_variant) + goto error; + if (def_variant->fields->pdata) { + *list = (struct definition const* const*) def_variant->fields->pdata; + *count = def_variant->fields->len; + goto end; + } else { + goto error; + } + } + case CTF_TYPE_ARRAY: + { + struct definition_array *def_array; + + def_array = container_of(scope, struct definition_array, p); + if (!def_array) + goto error; + if (def_array->elems->pdata) { + *list = (struct definition const* const*) def_array->elems->pdata; + *count = def_array->elems->len; + goto end; + } else { + goto error; + } + } + case CTF_TYPE_SEQUENCE: + { + struct definition_sequence *def_sequence; + + def_sequence = container_of(scope, struct definition_sequence, p); + if (!def_sequence) + goto error; + if (def_sequence->elems->pdata) { + *list = (struct definition const* const*) def_sequence->elems->pdata; + *count = def_sequence->elems->len; + goto end; + } else { + goto error; + } + } + default: + break; + } + +end: + return 0; + +error: + *list = NULL; + *count = 0; + return -1; +} + +uint64_t bt_ctf_get_timestamp(struct bt_ctf_event *event) +{ + if (event && event->stream->has_timestamp) + return event->stream->timestamp; + else + return 0; +} + +static void bt_ctf_field_set_error(int error) +{ + bt_ctf_last_field_error = error; +} + +int bt_ctf_field_get_error(void) +{ + int ret; + ret = bt_ctf_last_field_error; + bt_ctf_last_field_error = 0; + + return ret; +} + +uint64_t bt_ctf_get_uint64(struct definition *field) +{ + unsigned int ret = 0; + + if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) + ret = get_unsigned_int(field); + else + bt_ctf_field_set_error(-EINVAL); + + return ret; +} + +int64_t bt_ctf_get_signed_int64(struct definition *field) +{ + int ret = 0; + + if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) + ret = get_signed_int(field); + else + bt_ctf_field_set_error(-EINVAL); + + return ret; + +} + +char *bt_ctf_get_char_array(struct definition *field) +{ + char *ret = NULL; + + if (field && bt_ctf_field_type(field) == CTF_TYPE_ARRAY) + ret = get_char_array(field)->str; + else + bt_ctf_field_set_error(-EINVAL); + + return ret; +} + +char *bt_ctf_get_string(struct definition *field) +{ + char *ret = NULL; + + if (field && bt_ctf_field_type(field) == CTF_TYPE_STRING) + ret = get_string(field); + else + bt_ctf_field_set_error(-EINVAL); + + return ret; +} diff --git a/lib/iterator.c b/lib/iterator.c index 3a288a06..16b8f930 100644 --- a/lib/iterator.c +++ b/lib/iterator.c @@ -27,6 +27,7 @@ #include #include #include +#include #include struct stream_saved_pos { @@ -577,27 +578,27 @@ end: return ret; } -int bt_iter_read_event(struct bt_iter *iter, - struct ctf_stream **stream, - struct ctf_stream_event **event) +struct bt_ctf_event *bt_iter_read_ctf_event(struct bt_iter *iter) { struct ctf_file_stream *file_stream; - int ret = 0; + struct bt_ctf_event *ret = &iter->current_ctf_event; file_stream = heap_maximum(iter->stream_heap); if (!file_stream) { /* end of file for all streams */ - ret = EOF; - goto end; + goto stop; } - *stream = &file_stream->parent; - *event = g_ptr_array_index((*stream)->events_by_id, (*stream)->event_id); + ret->stream = &file_stream->parent; + ret->event = g_ptr_array_index(ret->stream->events_by_id, + ret->stream->event_id); - if ((*stream)->stream_id > iter->callbacks->len) + if (ret->stream->stream_id > iter->callbacks->len) goto end; - process_callbacks(iter, *stream); + process_callbacks(iter, ret->stream); end: return ret; +stop: + return NULL; } -- 2.34.1