Remove Babeltrace 1 files and reorganize the tree
[babeltrace.git] / plugins / ctf / common / metadata / visitor-generate-ir.c
index ec406bab4573dda7eb1649487eb4c7dde8f8cd68..c16c97065a868c5676c61289c2da091f58929fde 100644 (file)
 #include <glib.h>
 #include <inttypes.h>
 #include <errno.h>
-#include <babeltrace/ctf/metadata.h>
 #include <babeltrace/compat/uuid.h>
 #include <babeltrace/endian.h>
 #include <babeltrace/ref.h>
-#include <babeltrace/ctf/events-internal.h>
 #include <babeltrace/ctf-ir/trace.h>
 #include <babeltrace/ctf-ir/stream-class.h>
 #include <babeltrace/ctf-ir/event.h>
 #include <babeltrace/ctf-ir/event-class.h>
 #include <babeltrace/ctf-ir/field-types.h>
 #include <babeltrace/ctf-ir/field-types-internal.h>
-#include <babeltrace/ctf-ir/clock.h>
+#include <babeltrace/ctf-ir/clock-class.h>
 
 #include "scanner.h"
 #include "parser.h"
@@ -120,6 +118,25 @@ enum {
        _EVENT_FIELDS_SET =             _BV(6),
 };
 
+enum loglevel {
+        LOGLEVEL_EMERG                  = 0,
+        LOGLEVEL_ALERT                  = 1,
+        LOGLEVEL_CRIT                   = 2,
+        LOGLEVEL_ERR                    = 3,
+        LOGLEVEL_WARNING                = 4,
+        LOGLEVEL_NOTICE                 = 5,
+        LOGLEVEL_INFO                   = 6,
+        LOGLEVEL_DEBUG_SYSTEM           = 7,
+        LOGLEVEL_DEBUG_PROGRAM          = 8,
+        LOGLEVEL_DEBUG_PROCESS          = 9,
+        LOGLEVEL_DEBUG_MODULE           = 10,
+        LOGLEVEL_DEBUG_UNIT             = 11,
+        LOGLEVEL_DEBUG_FUNCTION         = 12,
+        LOGLEVEL_DEBUG_LINE             = 13,
+        LOGLEVEL_DEBUG                  = 14,
+       _NR_LOGLEVELS                   = 15,
+};
+
 /* Prefixes of type aliases */
 #define _PREFIX_ALIAS                  'a'
 #define _PREFIX_ENUM                   'e'
@@ -210,6 +227,37 @@ struct ctx {
        GHashTable *stream_classes;
 };
 
+static
+const char *loglevel_str [] = {
+       [ LOGLEVEL_EMERG ] = "TRACE_EMERG",
+       [ LOGLEVEL_ALERT ] = "TRACE_ALERT",
+       [ LOGLEVEL_CRIT ] = "TRACE_CRIT",
+       [ LOGLEVEL_ERR ] = "TRACE_ERR",
+       [ LOGLEVEL_WARNING ] = "TRACE_WARNING",
+       [ LOGLEVEL_NOTICE ] = "TRACE_NOTICE",
+       [ LOGLEVEL_INFO ] = "TRACE_INFO",
+       [ LOGLEVEL_DEBUG_SYSTEM ] = "TRACE_DEBUG_SYSTEM",
+       [ LOGLEVEL_DEBUG_PROGRAM ] = "TRACE_DEBUG_PROGRAM",
+       [ LOGLEVEL_DEBUG_PROCESS ] = "TRACE_DEBUG_PROCESS",
+       [ LOGLEVEL_DEBUG_MODULE ] = "TRACE_DEBUG_MODULE",
+       [ LOGLEVEL_DEBUG_UNIT ] = "TRACE_DEBUG_UNIT",
+       [ LOGLEVEL_DEBUG_FUNCTION ] = "TRACE_DEBUG_FUNCTION",
+       [ LOGLEVEL_DEBUG_LINE ] = "TRACE_DEBUG_LINE",
+       [ LOGLEVEL_DEBUG ] = "TRACE_DEBUG",
+};
+
+static
+const char *print_loglevel(int64_t value)
+{
+       if (value < 0) {
+               return NULL;
+       }
+       if (value >= _NR_LOGLEVELS) {
+               return "<<UNKNOWN>>";
+       }
+       return loglevel_str[value];
+}
+
 /**
  * Creates a new declaration scope.
  *
@@ -1768,6 +1816,15 @@ int visit_struct_decl(struct ctx *ctx, const char *name,
                        goto error;
                }
 
+               if (min_align_value != 0) {
+                       ret = bt_ctf_field_type_set_alignment(*struct_decl,
+                                       min_align_value);
+                       if (ret) {
+                               _PERROR("%s", "failed to set structure's minimal alignment");
+                               goto error;
+                       }
+               }
+
                ret = ctx_push_scope(ctx);
                if (ret) {
                        _PERROR("%s", "cannot push scope");
@@ -2188,7 +2245,7 @@ int visit_integer_decl(struct ctx *ctx,
        int signedness = 0;
        struct ctf_node *expression;
        uint64_t alignment = 0, size = 0;
-       struct bt_ctf_clock *mapped_clock = NULL;
+       struct bt_ctf_clock_class *mapped_clock = NULL;
        enum bt_ctf_string_encoding encoding = BT_CTF_STRING_ENCODING_NONE;
        enum bt_ctf_integer_base base = BT_CTF_INTEGER_BASE_DECIMAL;
        enum bt_ctf_byte_order byte_order =
@@ -2455,7 +2512,7 @@ int visit_integer_decl(struct ctx *ctx,
                                continue;
                        }
 
-                       mapped_clock = bt_ctf_trace_get_clock_by_name(
+                       mapped_clock = bt_ctf_trace_get_clock_class_by_name(
                                ctx->trace, clock_name);
                        if (!mapped_clock) {
                                _PERROR("invalid \"map\" attribute in integer declaration: cannot find clock \"%s\"",
@@ -2504,7 +2561,7 @@ int visit_integer_decl(struct ctx *ctx,
 
        if (mapped_clock) {
                /* Move clock */
-               ret |= bt_ctf_field_type_integer_set_mapped_clock(
+               ret |= bt_ctf_field_type_integer_set_mapped_clock_class(
                        *integer_decl, mapped_clock);
                bt_put(mapped_clock);
                mapped_clock = NULL;
@@ -3047,7 +3104,9 @@ int visit_event_decl_entry(struct ctx *ctx, struct ctf_node *node,
 
                        _SET(set, _EVENT_FIELDS_SET);
                } else if (!strcmp(left, "loglevel")) {
-                       uint64_t loglevel;
+                       uint64_t loglevel_value;
+                       const char *loglevel_str;
+                       struct bt_value *value_obj, *str_obj;
 
                        if (_IS_SET(set, _EVENT_LOGLEVEL_SET)) {
                                _PERROR_DUP_ATTR("loglevel",
@@ -3057,15 +3116,38 @@ int visit_event_decl_entry(struct ctx *ctx, struct ctf_node *node,
                        }
 
                        ret = get_unary_unsigned(&node->u.ctf_expression.right,
-                               &loglevel);
+                               &loglevel_value);
                        if (ret) {
                                _PERROR("%s", "unexpected unary expression for event declaration's \"loglevel\" attribute");
                                ret = -EINVAL;
                                goto error;
                        }
-
-                       // TODO: FIXME: set log level here
-
+                       value_obj = bt_value_integer_create_init(loglevel_value);
+                       if (!value_obj) {
+                               _PERROR("%s", "cannot allocate memory for loglevel value object");
+                               ret = -ENOMEM;
+                               goto error;
+                       }
+                       if (bt_ctf_event_class_set_attribute(event_class,
+                               "loglevel", value_obj) != BT_VALUE_STATUS_OK) {
+                               _PERROR("%s", "cannot set loglevel value");
+                               ret = -EINVAL;
+                               bt_put(value_obj);
+                               goto error;
+                       }
+                       loglevel_str = print_loglevel(loglevel_value);
+                       if (loglevel_str) {
+                               str_obj = bt_value_string_create_init(loglevel_str);
+                               if (bt_ctf_event_class_set_attribute(event_class,
+                                               "loglevel_string", str_obj) != BT_VALUE_STATUS_OK) {
+                                       _PERROR("%s", "cannot set loglevel string");
+                                       ret = -EINVAL;
+                                       bt_put(str_obj);
+                                       goto error;
+                               }
+                               bt_put(str_obj);
+                       }
+                       bt_put(value_obj);
                        _SET(set, _EVENT_LOGLEVEL_SET);
                } else if (!strcmp(left, "model.emf.uri")) {
                        char *right;
@@ -3163,43 +3245,21 @@ int reset_event_decl_types(struct ctx *ctx,
        struct bt_ctf_event_class *event_class)
 {
        int ret = 0;
-       _BT_CTF_FIELD_TYPE_INIT(decl);
 
-       /* Event context */
-       decl = bt_ctf_field_type_structure_create();
-       if (!decl) {
-               _PERROR("%s", "cannot create initial, empty event context structure");
-               ret = -ENOMEM;
-               goto error;
-       }
-
-       ret = bt_ctf_event_class_set_context_type(event_class, decl);
-       BT_PUT(decl);
+       /* Context type. */
+       ret = bt_ctf_event_class_set_context_type(event_class, NULL);
        if (ret) {
-               _PERROR("%s", "cannot set initial, empty event context structure");
-               goto error;
-       }
-
-       /* Event payload */
-       decl = bt_ctf_field_type_structure_create();
-       if (!decl) {
-               _PERROR("%s", "cannot create initial, empty event payload structure");
-               ret = -ENOMEM;
-               goto error;
+               _PERROR("%s", "cannot set initial NULL event context");
+               goto end;
        }
 
-       ret = bt_ctf_event_class_set_payload_type(event_class, decl);
-       BT_PUT(decl);
+       /* Event payload. */
+       ret = bt_ctf_event_class_set_payload_type(event_class, NULL);
        if (ret) {
-               _PERROR("%s", "cannot set initial, empty event payload structure");
-               goto error;
+               _PERROR("%s", "cannot set initial NULL event payload");
+               goto end;
        }
-
-       return 0;
-
-error:
-       BT_PUT(decl);
-
+end:
        return ret;
 }
 
@@ -3208,58 +3268,28 @@ int reset_stream_decl_types(struct ctx *ctx,
        struct bt_ctf_stream_class *stream_class)
 {
        int ret = 0;
-       _BT_CTF_FIELD_TYPE_INIT(decl);
 
-       /* Packet context */
-       decl = bt_ctf_field_type_structure_create();
-       if (!decl) {
-               _PERROR("%s", "cannot create initial, empty packet context structure");
-               ret = -ENOMEM;
-               goto error;
-       }
-
-       ret = bt_ctf_stream_class_set_packet_context_type(stream_class, decl);
-       BT_PUT(decl);
+       /* Packet context. */
+       ret = bt_ctf_stream_class_set_packet_context_type(stream_class, NULL);
        if (ret) {
-               _PERROR("%s", "cannot set initial, empty packet context structure");
-               goto error;
-       }
-
-       /* Event header */
-       decl = bt_ctf_field_type_structure_create();
-       if (!decl) {
-               _PERROR("%s", "cannot create initial, empty event header structure");
-               ret = -ENOMEM;
-               goto error;
+               _PERROR("%s", "cannot set initial empty packet context");
+               goto end;
        }
 
-       ret = bt_ctf_stream_class_set_event_header_type(stream_class, decl);
-       BT_PUT(decl);
+       /* Event header. */
+       ret = bt_ctf_stream_class_set_event_header_type(stream_class, NULL);
        if (ret) {
-               _PERROR("%s", "cannot set initial, empty event header structure");
-               goto error;
-       }
-
-       /* Event context */
-       decl = bt_ctf_field_type_structure_create();
-       if (!decl) {
-               _PERROR("%s", "cannot create initial, empty stream event context structure");
-               ret = -ENOMEM;
-               goto error;
+               _PERROR("%s", "cannot set initial empty event header");
+               goto end;
        }
 
-       ret = bt_ctf_stream_class_set_event_context_type(stream_class, decl);
-       BT_PUT(decl);
+       /* Event context. */
+       ret = bt_ctf_stream_class_set_event_context_type(stream_class, NULL);
        if (ret) {
-               _PERROR("%s", "cannot set initial, empty stream event context structure");
-               goto error;
+               _PERROR("%s", "cannot set initial empty stream event context");
+               goto end;
        }
-
-       return 0;
-
-error:
-       BT_PUT(decl);
-
+end:
        return ret;
 }
 
@@ -3276,8 +3306,8 @@ struct bt_ctf_stream_class *create_reset_stream_class(struct ctx *ctx)
        }
 
        /*
-        * Set packet context, event header, and event context to empty
-        * structures to override the default ones.
+        * Set packet context, event header, and event context to NULL to
+        * override the default ones.
         */
        ret = reset_stream_decl_types(ctx, stream_class);
        if (ret) {
@@ -3322,8 +3352,7 @@ int visit_event_decl(struct ctx *ctx, struct ctf_node *node)
        event_class = bt_ctf_event_class_create(event_name);
 
        /*
-        * Set context and fields to empty structures to override the
-        * default ones.
+        * Unset context and fields to override the default ones.
         */
        ret = reset_event_decl_types(ctx, event_class);
        if (ret) {
@@ -4124,7 +4153,7 @@ error:
 
 static
 int visit_clock_decl_entry(struct ctx *ctx, struct ctf_node *entry_node,
-       struct bt_ctf_clock *clock, int *set)
+       struct bt_ctf_clock_class *clock, int *set)
 {
        int ret = 0;
        char *left = NULL;
@@ -4157,7 +4186,7 @@ int visit_clock_decl_entry(struct ctx *ctx, struct ctf_node *entry_node,
                        goto error;
                }
 
-               ret = bt_ctf_clock_set_name(clock, right);
+               ret = bt_ctf_clock_class_set_name(clock, right);
                if (ret) {
                        _PERROR("%s", "cannot set clock's name");
                        g_free(right);
@@ -4181,7 +4210,7 @@ int visit_clock_decl_entry(struct ctx *ctx, struct ctf_node *entry_node,
                        goto error;
                }
 
-               ret = bt_ctf_clock_set_uuid(clock, uuid);
+               ret = bt_ctf_clock_class_set_uuid(clock, uuid);
                if (ret) {
                        _PERROR("%s", "cannot set clock's UUID");
                        goto error;
@@ -4205,7 +4234,7 @@ int visit_clock_decl_entry(struct ctx *ctx, struct ctf_node *entry_node,
                        goto error;
                }
 
-               ret = bt_ctf_clock_set_description(clock, right);
+               ret = bt_ctf_clock_class_set_description(clock, right);
                if (ret) {
                        _PERROR("%s", "cannot set clock's description");
                        g_free(right);
@@ -4231,7 +4260,7 @@ int visit_clock_decl_entry(struct ctx *ctx, struct ctf_node *entry_node,
                        goto error;
                }
 
-               ret = bt_ctf_clock_set_frequency(clock, freq);
+               ret = bt_ctf_clock_class_set_frequency(clock, freq);
                if (ret) {
                        _PERROR("%s", "cannot set clock's frequency");
                        goto error;
@@ -4255,7 +4284,7 @@ int visit_clock_decl_entry(struct ctx *ctx, struct ctf_node *entry_node,
                        goto error;
                }
 
-               ret = bt_ctf_clock_set_precision(clock, precision);
+               ret = bt_ctf_clock_class_set_precision(clock, precision);
                if (ret) {
                        _PERROR("%s", "cannot set clock's precision");
                        goto error;
@@ -4279,7 +4308,7 @@ int visit_clock_decl_entry(struct ctx *ctx, struct ctf_node *entry_node,
                        goto error;
                }
 
-               ret = bt_ctf_clock_set_offset_s(clock, offset_s);
+               ret = bt_ctf_clock_class_set_offset_s(clock, offset_s);
                if (ret) {
                        _PERROR("%s", "cannot set clock's offset in seconds");
                        goto error;
@@ -4303,7 +4332,7 @@ int visit_clock_decl_entry(struct ctx *ctx, struct ctf_node *entry_node,
                        goto error;
                }
 
-               ret = bt_ctf_clock_set_offset(clock, offset);
+               ret = bt_ctf_clock_class_set_offset_cycles(clock, offset);
                if (ret) {
                        _PERROR("%s", "cannot set clock's offset in cycles");
                        goto error;
@@ -4329,7 +4358,7 @@ int visit_clock_decl_entry(struct ctx *ctx, struct ctf_node *entry_node,
                        goto error;
                }
 
-               ret = bt_ctf_clock_set_is_absolute(clock, ret);
+               ret = bt_ctf_clock_class_set_is_absolute(clock, ret);
                if (ret) {
                        _PERROR("%s", "cannot set clock's absolute option");
                        goto error;
@@ -4357,7 +4386,7 @@ int visit_clock_decl(struct ctx *ctx, struct ctf_node *clock_node)
 {
        int ret = 0;
        int set = 0;
-       struct bt_ctf_clock *clock;
+       struct bt_ctf_clock_class *clock;
        struct ctf_node *entry_node;
        struct bt_list_head *decl_list = &clock_node->u.clock.declaration_list;
 
@@ -4366,7 +4395,7 @@ int visit_clock_decl(struct ctx *ctx, struct ctf_node *clock_node)
        }
 
        clock_node->visited = TRUE;
-       clock = bt_ctf_clock_create(NULL);
+       clock = bt_ctf_clock_class_create(NULL);
        if (!clock) {
                _PERROR("%s", "cannot create clock");
                ret = -ENOMEM;
@@ -4387,13 +4416,13 @@ int visit_clock_decl(struct ctx *ctx, struct ctf_node *clock_node)
                goto error;
        }
 
-       if (bt_ctf_trace_get_clock_count(ctx->trace) != 0) {
+       if (bt_ctf_trace_get_clock_class_count(ctx->trace) != 0) {
                _PERROR("%s", "only CTF traces with a single clock declaration are supported as of this version");
                ret = -EINVAL;
                goto error;
        }
 
-       ret = bt_ctf_trace_add_clock(ctx->trace, clock);
+       ret = bt_ctf_trace_add_clock_class(ctx->trace, clock);
        if (ret) {
                _PERROR("%s", "cannot add clock to trace");
                goto error;
@@ -4489,7 +4518,6 @@ int ctf_visitor_generate_ir(FILE *efd, struct ctf_node *node,
 {
        int ret = 0;
        struct ctx *ctx = NULL;
-       _BT_CTF_FIELD_TYPE_INIT(packet_header_decl);
 
        printf_verbose("CTF visitor: AST -> CTF IR...\n");
 
@@ -4500,19 +4528,8 @@ int ctf_visitor_generate_ir(FILE *efd, struct ctf_node *node,
                goto error;
        }
 
-       /* Set packet header to an empty struct tu override the default one */
-       packet_header_decl = bt_ctf_field_type_structure_create();
-
-       if (!packet_header_decl) {
-               _FPERROR(efd,
-                       "%s",
-                       "cannot create initial, empty packet header structure");
-               ret = -ENOMEM;
-               goto error;
-       }
-
-       ret = bt_ctf_trace_set_packet_header_type(*trace, packet_header_decl);
-       BT_PUT(packet_header_decl);
+       /* Set packet header to NULL to override the default one */
+       ret = bt_ctf_trace_set_packet_header_type(*trace, NULL);
        if (ret) {
                _FPERROR(efd,
                        "%s",
@@ -4654,7 +4671,6 @@ int ctf_visitor_generate_ir(FILE *efd, struct ctf_node *node,
        return ret;
 
 error:
-       BT_PUT(packet_header_decl);
        ctx_destroy(ctx);
        BT_PUT(*trace);
 
This page took 0.038692 seconds and 4 git commands to generate.