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 <julien.desfossez@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
#include <babeltrace/format.h>
#include <babeltrace/context.h>
#include <babeltrace/ctf/types.h>
+#include <babeltrace/ctf/events.h>
#include <babeltrace/ctf-text/types.h>
#include <babeltrace/iterator.h>
#include <popt.h>
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,
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;
babeltrace/context.h \
babeltrace/iterator.h \
babeltrace/trace-collection.h \
- babeltrace/trace-handle.h
+ babeltrace/trace-handle.h \
+ babeltrace/ctf/events.h
noinst_HEADERS = \
--- /dev/null
+#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 <mathieu.desnoyers@efficios.com>
+ * Julien Desfossez <julien.desfossez@efficios.com>
+ *
+ * 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 <stdint.h>
+
+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 */
* all copies or substantial portions of the Software.
*/
+#include <babeltrace/ctf/events.h>
+
/*
* struct bt_iter: data structure representing an iterator on a trace
* collection.
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 */
/*
#include <babeltrace/format.h>
#include <babeltrace/context.h>
+#include <babeltrace/ctf/events.h>
/* Forward declarations */
struct bt_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 */
#include <babeltrace/align.h>
#include <babeltrace/list.h>
+#include <babeltrace/ctf/events.h>
#include <stdbool.h>
#include <stdint.h>
#include <limits.h>
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 */
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 \
--- /dev/null
+/*
+ * ctf_events.c
+ *
+ * Babeltrace Library
+ *
+ * Copyright 2011-2012 EfficiOS Inc. and Linux Foundation
+ *
+ * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ * Julien Desfossez <julien.desfossez@efficios.com>
+ *
+ * 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 <babeltrace/babeltrace.h>
+#include <babeltrace/format.h>
+#include <babeltrace/ctf/events.h>
+#include <babeltrace/ctf-ir/metadata.h>
+#include <glib.h>
+
+/*
+ * 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;
+}
#include <babeltrace/iterator-internal.h>
#include <babeltrace/iterator.h>
#include <babeltrace/prio_heap.h>
+#include <babeltrace/ctf/events.h>
#include <inttypes.h>
struct stream_saved_pos {
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;
}