API : Access CTF events fields
authorJulien Desfossez <julien.desfossez@efficios.com>
Thu, 16 Feb 2012 16:53:44 +0000 (11:53 -0500)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 16 Feb 2012 16:53:44 +0000 (11:53 -0500)
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>
converter/babeltrace.c
include/Makefile.am
include/babeltrace/ctf/events.h [new file with mode: 0644]
include/babeltrace/iterator-internal.h
include/babeltrace/iterator.h
include/babeltrace/types.h
lib/Makefile.am
lib/ctf-events.c [new file with mode: 0644]
lib/iterator.c

index 539734eeaa55df4e3ba23019f8cbe5d9560fef51..65220183722fdabd91a68d9947f662041c43d829 100644 (file)
@@ -24,6 +24,7 @@
 #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>
@@ -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;
index 12b8947091f1a979d6b348001e2deac44240e91a..4ad0af4c5663455fda5de64e479986b193f7c3b1 100644 (file)
@@ -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 (file)
index 0000000..eb64412
--- /dev/null
@@ -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 <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 */
index 9cbc79cfc1efffdabd5e0bba66b44c93ffce6365..121d0dccb3d6c4bf3a74918a64e176632225d2ad 100644 (file)
@@ -21,6 +21,8 @@
  * 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.
@@ -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 */
        /*
index 0acd91d431b2a98e59c7ad2e43c395dfc53166e2..3dcf0825f2efaf03ef7b9545512d0b61f52a7946 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <babeltrace/format.h>
 #include <babeltrace/context.h>
+#include <babeltrace/ctf/events.h>
 
 /* 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 */
index b4d2d4a7dc8b423a5bf1f7ebea971f381384ef2f..e3c419d1bb86b37ba3e74ab6fa3aac9a1e702897 100644 (file)
@@ -23,6 +23,7 @@
 
 #include <babeltrace/align.h>
 #include <babeltrace/list.h>
+#include <babeltrace/ctf/events.h>
 #include <stdbool.h>
 #include <stdint.h>
 #include <limits.h>
@@ -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 */
index 56068fd89288493c0b393b0b0ce22ff41278f4cf..079ed13eaa2f1ce1ef2a8a62ba1831db8b114f6c 100644 (file)
@@ -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 (file)
index 0000000..dbb0cd6
--- /dev/null
@@ -0,0 +1,305 @@
+/*
+ * 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;
+}
index 3a288a06e07ede3efe2893a0762bcac8bbbe67c2..16b8f93004a910f0975245cf59f9cf83f1d38fc9 100644 (file)
@@ -27,6 +27,7 @@
 #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 {
@@ -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;
 }
This page took 0.048509 seconds and 4 git commands to generate.