Namespace the struct trace_descriptor
[babeltrace.git] / formats / ctf / events.c
index c281db54a2844feb249b39b7ee9958a7b697d286..d9460b169e98e68ebb0c0f6a9630b4f563f4d522 100644 (file)
  *
  * The above copyright notice and this permission notice shall be included in
  * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
  */
 
 #include <babeltrace/babeltrace.h>
@@ -41,8 +49,8 @@ __thread int bt_ctf_last_field_error = 0;
 const struct definition *bt_ctf_get_top_level_scope(const struct bt_ctf_event *ctf_event,
                enum bt_ctf_scope scope)
 {
-       struct definition *tmp = NULL;
-       struct ctf_event_definition *event;
+       const struct definition *tmp = NULL;
+       const struct ctf_event_definition *event;
 
        if (!ctf_event)
                return NULL;
@@ -92,13 +100,13 @@ const struct definition *bt_ctf_get_field(const struct bt_ctf_event *ctf_event,
                const struct definition *scope,
                const char *field)
 {
-       struct definition *def;
+       const struct definition *def;
        char *field_underscore;
 
        if (!ctf_event || !scope || !field)
                return NULL;
 
-       def = lookup_definition(scope, field);
+       def = bt_lookup_definition(scope, field);
        /*
         * optionally a field can have an underscore prefix, try
         * to lookup the field with this prefix if it failed
@@ -107,13 +115,13 @@ const struct definition *bt_ctf_get_field(const struct bt_ctf_event *ctf_event,
                field_underscore = g_new(char, strlen(field) + 2);
                field_underscore[0] = '_';
                strcpy(&field_underscore[1], field);
-               def = lookup_definition(scope, field_underscore);
+               def = bt_lookup_definition(scope, field_underscore);
                g_free(field_underscore);
        }
-       if (bt_ctf_field_type(bt_ctf_get_field_decl(def)) == CTF_TYPE_VARIANT) {
-               struct definition_variant *variant_definition;
+       if (bt_ctf_field_type(bt_ctf_get_decl_from_def(def)) == CTF_TYPE_VARIANT) {
+               const struct definition_variant *variant_definition;
                variant_definition = container_of(def,
-                               struct definition_variant, p);
+                               const struct definition_variant, p);
                return variant_definition->current_field;
        }
        return def;
@@ -128,25 +136,25 @@ const struct definition *bt_ctf_get_index(const struct bt_ctf_event *ctf_event,
        if (!ctf_event || !field)
                return NULL;
 
-       if (bt_ctf_field_type(bt_ctf_get_field_decl(field)) == CTF_TYPE_ARRAY) {
+       if (bt_ctf_field_type(bt_ctf_get_decl_from_def(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(bt_ctf_get_field_decl(field)) == CTF_TYPE_SEQUENCE) {
+               ret = bt_array_index(array_definition, index);
+       } else if (bt_ctf_field_type(bt_ctf_get_decl_from_def(field)) == CTF_TYPE_SEQUENCE) {
                struct definition_sequence *sequence_definition;
                sequence_definition = container_of(field,
                                struct definition_sequence, p);
-               ret = sequence_index(sequence_definition, index);
+               ret = bt_sequence_index(sequence_definition, index);
        }
        return ret;
 }
 
 const char *bt_ctf_event_name(const struct bt_ctf_event *ctf_event)
 {
-       struct ctf_event_declaration *event_class;
-       struct ctf_stream_declaration *stream_class;
-       struct ctf_event_definition *event;
+       const struct ctf_event_declaration *event_class;
+       const struct ctf_stream_declaration *stream_class;
+       const struct ctf_event_definition *event;
 
        if (!ctf_event)
                return NULL;
@@ -160,7 +168,7 @@ const char *bt_ctf_event_name(const struct bt_ctf_event *ctf_event)
 
 const char *bt_ctf_field_name(const struct definition *def)
 {
-       if (!def)
+       if (!def || !def->name)
                return NULL;
 
        return rem_(g_quark_to_string(def->name));
@@ -182,7 +190,7 @@ int bt_ctf_get_field_list(const struct bt_ctf_event *ctf_event,
        if (!ctf_event || !scope || !list || !count)
                return -EINVAL;
 
-       switch (bt_ctf_field_type(bt_ctf_get_field_decl(scope))) {
+       switch (bt_ctf_field_type(bt_ctf_get_decl_from_def(scope))) {
        case CTF_TYPE_INTEGER:
        case CTF_TYPE_FLOAT:
        case CTF_TYPE_STRING:
@@ -270,15 +278,15 @@ error:
 struct bt_context *bt_ctf_event_get_context(const struct bt_ctf_event *ctf_event)
 {
        struct bt_context *ret = NULL;
-       struct ctf_file_stream *cfs;
-       struct ctf_trace *trace;
-       struct ctf_event_definition *event;
+       const struct ctf_file_stream *cfs;
+       const struct ctf_trace *trace;
+       const struct ctf_event_definition *event;
 
        if (!ctf_event)
                return NULL;
 
        event = ctf_event->parent;
-       cfs = container_of(event->stream, struct ctf_file_stream,
+       cfs = container_of(event->stream, const struct ctf_file_stream,
                        parent);
        trace = cfs->parent.stream_class->trace;
        if (trace->ctx)
@@ -290,15 +298,15 @@ struct bt_context *bt_ctf_event_get_context(const struct bt_ctf_event *ctf_event
 int bt_ctf_event_get_handle_id(const struct bt_ctf_event *ctf_event)
 {
        int ret = -1;
-       struct ctf_file_stream *cfs;
-       struct ctf_trace *trace;
-       struct ctf_event_definition *event;
+       const struct ctf_file_stream *cfs;
+       const struct ctf_trace *trace;
+       const struct ctf_event_definition *event;
 
        if (!ctf_event)
                return -EINVAL;
 
        event = ctf_event->parent;
-       cfs = container_of(event->stream, struct ctf_file_stream,
+       cfs = container_of(event->stream, const struct ctf_file_stream,
                        parent);
        trace = cfs->parent.stream_class->trace;
        if (trace->handle)
@@ -309,7 +317,7 @@ int bt_ctf_event_get_handle_id(const struct bt_ctf_event *ctf_event)
 
 uint64_t bt_ctf_get_timestamp(const struct bt_ctf_event *ctf_event)
 {
-       struct ctf_event_definition *event;
+       const struct ctf_event_definition *event;
 
        if (!ctf_event)
                return -1ULL;
@@ -323,7 +331,7 @@ uint64_t bt_ctf_get_timestamp(const struct bt_ctf_event *ctf_event)
 
 uint64_t bt_ctf_get_cycles(const struct bt_ctf_event *ctf_event)
 {
-       struct ctf_event_definition *event;
+       const struct ctf_event_definition *event;
 
        if (!ctf_event)
                return -1ULL;
@@ -349,140 +357,117 @@ int bt_ctf_field_get_error(void)
        return ret;
 }
 
-static struct declaration_integer *get_declaration_integer(const struct declaration *decl)
+static const struct declaration_integer *
+get_declaration_integer(const struct declaration *decl)
 {
-       struct declaration_field *field_decl;
-       struct declaration_integer *ret = NULL;
-
-       if (decl && bt_ctf_field_type(decl) == CTF_TYPE_INTEGER) {
-               field_decl = (struct declaration_field *) decl;
-               ret = ((struct declaration_integer *) field_decl->declaration);
-       }
-
-       return ret;
+       if (!decl || bt_ctf_field_type(decl) != CTF_TYPE_INTEGER)
+               return NULL;
+       return container_of(decl, const struct declaration_integer, p);
 }
 
-static struct declaration_string *get_declaration_string(const struct declaration *decl)
+static const struct declaration_string *
+get_declaration_string(const struct declaration *decl)
 {
-       struct declaration_field *field_decl;
-       struct declaration_string *ret = NULL;
-
-       if (decl && bt_ctf_field_type(decl) == CTF_TYPE_STRING) {
-               field_decl = (struct declaration_field *) decl;
-               ret = ((struct declaration_string *) field_decl->declaration);
-       }
-
-       return ret;
+       if (!decl || bt_ctf_field_type(decl) != CTF_TYPE_STRING)
+               return NULL;
+       return container_of(decl, const struct declaration_string, p);
 }
 
-static struct declaration_array *get_declaration_array(const struct declaration *decl)
+static const struct declaration_array *
+get_declaration_array(const struct declaration *decl)
 {
-       struct declaration_field *field_decl;
-       struct declaration_array *ret = NULL;
-
-       if (decl && bt_ctf_field_type(decl) == CTF_TYPE_ARRAY) {
-               field_decl = (struct declaration_field *) decl;
-               ret = ((struct declaration_array *) field_decl->declaration);
-       }
+       if (!decl || bt_ctf_field_type(decl) != CTF_TYPE_ARRAY)
+               return NULL;
+       return container_of(decl, const struct declaration_array, p);
+}
 
-       return ret;
+static const struct declaration_sequence *
+get_declaration_sequence(const struct declaration *decl)
+{
+       if (!decl || bt_ctf_field_type(decl) != CTF_TYPE_SEQUENCE)
+               return NULL;
+       return container_of(decl, const struct declaration_sequence, p);
 }
 
 int bt_ctf_get_int_signedness(const struct declaration *decl)
 {
-       int ret;
-       struct declaration_integer *integer;
+       const struct declaration_integer *integer;
 
        integer = get_declaration_integer(decl);
-       if (integer) {
-               ret = integer->signedness;
-       } else {
-               ret = -EINVAL;
+       if (!integer) {
                bt_ctf_field_set_error(-EINVAL);
+               return -EINVAL;
        }
-
-       return ret;
+       return integer->signedness;
 }
 
 int bt_ctf_get_int_base(const struct declaration *decl)
 {
-       int ret;
-       struct declaration_integer *integer;
+       const struct declaration_integer *integer;
 
        integer = get_declaration_integer(decl);
-       if (integer) {
-               ret = integer->base;
-       } else {
-               ret = -EINVAL;
+       if (!integer) {
                bt_ctf_field_set_error(-EINVAL);
+               return -EINVAL;
        }
-
-       return ret;
+       return integer->base;
 }
 
 int bt_ctf_get_int_byte_order(const struct declaration *decl)
 {
-       int ret;
-       struct declaration_integer *integer;
+       const struct declaration_integer *integer;
 
        integer = get_declaration_integer(decl);
-       if (integer) {
-               ret = integer->byte_order;
-       } else {
-               ret = -EINVAL;
+       if (!integer) {
                bt_ctf_field_set_error(-EINVAL);
+               return -EINVAL;
        }
-
-       return ret;
+       return integer->byte_order;
 }
 
 ssize_t bt_ctf_get_int_len(const struct declaration *decl)
 {
-       ssize_t ret;
-       struct declaration_integer *integer;
+       const struct declaration_integer *integer;
 
        integer = get_declaration_integer(decl);
-       if (integer) {
-               ret = (ssize_t) integer->len;
-       } else {
-               ret = -EINVAL;
+       if (!integer) {
                bt_ctf_field_set_error(-EINVAL);
+               return -EINVAL;
        }
-
-       return ret;
+       return (ssize_t) integer->len;
 }
 
 const struct definition *bt_ctf_get_enum_int(const struct definition *field)
 {
-       struct definition_enum *def_enum;
+       const struct definition_enum *def_enum;
 
-       if (!field || bt_ctf_field_type(bt_ctf_get_field_decl(field)) != CTF_TYPE_ENUM) {
+       if (!field || bt_ctf_field_type(bt_ctf_get_decl_from_def(field)) != CTF_TYPE_ENUM) {
                bt_ctf_field_set_error(-EINVAL);
                return NULL;
        }
-       def_enum = container_of(field, struct definition_enum, p);
+       def_enum = container_of(field, const struct definition_enum, p);
        return &def_enum->integer->p;
 }
 
 const char *bt_ctf_get_enum_str(const struct definition *field)
 {
-       struct definition_enum *def_enum;
-       struct declaration_enum *decl_enum;
+       const struct definition_enum *def_enum;
+       const struct declaration_enum *decl_enum;
        GArray *array;
        const char *ret;
 
-       if (!field || bt_ctf_field_type(bt_ctf_get_field_decl(field)) != CTF_TYPE_ENUM) {
+       if (!field || bt_ctf_field_type(bt_ctf_get_decl_from_def(field)) != CTF_TYPE_ENUM) {
                bt_ctf_field_set_error(-EINVAL);
                return NULL;
        }
-       def_enum = container_of(field, struct definition_enum, p);
+       def_enum = container_of(field, const struct definition_enum, p);
        decl_enum = def_enum->declaration;
-       if (get_int_signedness(&def_enum->integer->p)) {
-               array = enum_int_to_quark_set(decl_enum,
-                       get_signed_int(&def_enum->integer->p));
+       if (bt_get_int_signedness(&def_enum->integer->p)) {
+               array = bt_enum_int_to_quark_set(decl_enum,
+                       bt_get_signed_int(&def_enum->integer->p));
        } else {
-               array = enum_uint_to_quark_set(decl_enum,
-                       get_unsigned_int(&def_enum->integer->p));
+               array = bt_enum_uint_to_quark_set(decl_enum,
+                       bt_get_unsigned_int(&def_enum->integer->p));
        }
        if (!array) {
                bt_ctf_field_set_error(-ENOENT);
@@ -503,27 +488,49 @@ const char *bt_ctf_get_enum_str(const struct definition *field)
 enum ctf_string_encoding bt_ctf_get_encoding(const struct declaration *decl)
 {
        enum ctf_string_encoding ret = 0;
-       struct declaration_integer *integer;
-       struct declaration_string *string;
+       enum ctf_type_id type;
+       const struct declaration_integer *integer;
+       const struct declaration_string *string;
+       const struct declaration_array *array;
+       const struct declaration_sequence *sequence;
 
        if (!decl)
                goto error;
 
-       if (bt_ctf_field_type(decl) == CTF_TYPE_INTEGER) {
-               integer = get_declaration_integer(decl);
-               if (integer) {
-                       ret = integer->encoding;
-               } else {
+       type = bt_ctf_field_type(decl);
+
+       switch (type) {
+       case CTF_TYPE_ARRAY:
+               array = get_declaration_array(decl);
+               if (!array)
                        goto error;
-               }
-       } else if (bt_ctf_field_type(decl) == CTF_TYPE_STRING) {
+               integer = get_declaration_integer(array->elem);
+               if (!integer)
+                       goto error;
+               ret = integer->encoding;
+               break;
+       case CTF_TYPE_SEQUENCE:
+               sequence = get_declaration_sequence(decl);
+               if (!sequence)
+                       goto error;
+               integer = get_declaration_integer(sequence->elem);
+               if (!integer)
+                       goto error;
+               ret = integer->encoding;
+               break;
+       case CTF_TYPE_STRING:
                string = get_declaration_string(decl);
-               if (string) {
-                       ret = string->encoding;
-               } else {
+               if (!string)
                        goto error;
-               }
-       } else {
+               ret = string->encoding;
+               break;
+       case CTF_TYPE_INTEGER:
+               integer = get_declaration_integer(decl);
+               if (!integer)
+                       goto error;
+               ret = integer->encoding;
+               break;
+       default:
                goto error;
        }
        return ret;
@@ -535,21 +542,12 @@ error:
 
 int bt_ctf_get_array_len(const struct declaration *decl)
 {
-       int ret;
-       struct declaration_array *array;
+       const struct declaration_array *array;
 
-       if (decl && bt_ctf_field_type(decl) == CTF_TYPE_ARRAY) {
-               array = get_declaration_array(decl);
-               if (array) {
-                       ret = array->len;
-               } else {
-                       goto error;
-               }
-       } else {
+       array = get_declaration_array(decl);
+       if (!array)
                goto error;
-       }
-
-       return ret;
+       return array->len;
 
 error:
        bt_ctf_field_set_error(-EINVAL);
@@ -560,8 +558,8 @@ uint64_t bt_ctf_get_uint64(const struct definition *field)
 {
        uint64_t ret = 0;
 
-       if (field && bt_ctf_field_type(bt_ctf_get_field_decl(field)) == CTF_TYPE_INTEGER)
-               ret = get_unsigned_int(field);
+       if (field && bt_ctf_field_type(bt_ctf_get_decl_from_def(field)) == CTF_TYPE_INTEGER)
+               ret = bt_get_unsigned_int(field);
        else
                bt_ctf_field_set_error(-EINVAL);
 
@@ -572,13 +570,12 @@ int64_t bt_ctf_get_int64(const struct definition *field)
 {
        int64_t ret = 0;
 
-       if (field && bt_ctf_field_type(bt_ctf_get_field_decl(field)) == CTF_TYPE_INTEGER)
-               ret = get_signed_int(field);
+       if (field && bt_ctf_field_type(bt_ctf_get_decl_from_def(field)) == CTF_TYPE_INTEGER)
+               ret = bt_get_signed_int(field);
        else
                bt_ctf_field_set_error(-EINVAL);
 
        return ret;
-
 }
 
 char *bt_ctf_get_char_array(const struct definition *field)
@@ -586,8 +583,8 @@ char *bt_ctf_get_char_array(const struct definition *field)
        char *ret = NULL;
        GString *char_array;
 
-       if (field && bt_ctf_field_type(bt_ctf_get_field_decl(field)) == CTF_TYPE_ARRAY) {
-               char_array = get_char_array(field);
+       if (field && bt_ctf_field_type(bt_ctf_get_decl_from_def(field)) == CTF_TYPE_ARRAY) {
+               char_array = bt_get_char_array(field);
                if (char_array) {
                        ret = char_array->str;
                        goto end;
@@ -603,8 +600,8 @@ char *bt_ctf_get_string(const struct definition *field)
 {
        char *ret = NULL;
 
-       if (field && bt_ctf_field_type(bt_ctf_get_field_decl(field)) == CTF_TYPE_STRING)
-               ret = get_string(field);
+       if (field && bt_ctf_field_type(bt_ctf_get_decl_from_def(field)) == CTF_TYPE_STRING)
+               ret = bt_get_string(field);
        else
                bt_ctf_field_set_error(-EINVAL);
 
@@ -616,7 +613,7 @@ int bt_ctf_get_event_decl_list(int handle_id, struct bt_context *ctx,
                unsigned int *count)
 {
        struct bt_trace_handle *handle;
-       struct trace_descriptor *td;
+       struct bt_trace_descriptor *td;
        struct ctf_trace *tin;
 
        if (!ctx || !list || !count)
@@ -770,10 +767,19 @@ const char *bt_ctf_get_decl_field_name(const struct bt_ctf_field_decl *field)
        return rem_(g_quark_to_string(((struct declaration_field *) field)->name));
 }
 
-const struct declaration *bt_ctf_get_field_decl(const struct definition *def)
+const struct declaration *bt_ctf_get_decl_from_def(const struct definition *def)
 {
        if (def)
                return def->declaration;
 
        return NULL;
 }
+
+const struct declaration *bt_ctf_get_decl_from_field_decl(
+               const struct bt_ctf_field_decl *field)
+{
+       if (field)
+               return ((struct declaration_field *) field)->declaration;
+
+       return NULL;
+}
This page took 0.029759 seconds and 4 git commands to generate.