Introduce side_call*
[libside.git] / src / tracer.c
index 03d47ef753c85d9b683597503c4a95ddab653470..24d6189f600ab07e5092d9b5bd78ad7a484dce0e 100644 (file)
@@ -7,6 +7,7 @@
 #include <inttypes.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <stdbool.h>
 
 #include <side/trace.h>
 
@@ -25,6 +26,152 @@ void tracer_print_vla_fixint(const struct side_type_description *type_desc, cons
 static
 void tracer_print_dynamic(const struct side_arg_dynamic_vec *dynamic_item);
 
+static
+void tracer_print_attr_type(const struct side_attr *attr)
+{
+       printf("{ key: \"%s\", value: ", attr->key);
+       switch (attr->value.type) {
+       case SIDE_ATTR_TYPE_BOOL:
+               printf("%s", attr->value.u.side_bool ? "true" : "false");
+               break;
+       case SIDE_ATTR_TYPE_U8:
+               printf("%" PRIu8, attr->value.u.side_u8);
+               break;
+       case SIDE_ATTR_TYPE_U16:
+               printf("%" PRIu16, attr->value.u.side_u16);
+               break;
+       case SIDE_ATTR_TYPE_U32:
+               printf("%" PRIu32, attr->value.u.side_u32);
+               break;
+       case SIDE_ATTR_TYPE_U64:
+               printf("%" PRIu64, attr->value.u.side_u64);
+               break;
+       case SIDE_ATTR_TYPE_S8:
+               printf("%" PRId8, attr->value.u.side_s8);
+               break;
+       case SIDE_ATTR_TYPE_S16:
+               printf("%" PRId16, attr->value.u.side_s16);
+               break;
+       case SIDE_ATTR_TYPE_S32:
+               printf("%" PRId32, attr->value.u.side_s32);
+               break;
+       case SIDE_ATTR_TYPE_S64:
+               printf("%" PRId64, attr->value.u.side_s64);
+               break;
+       case SIDE_ATTR_TYPE_FLOAT_BINARY16:
+#if __HAVE_FLOAT16
+               printf("%g", (double) attr->value.u.side_float_binary16);
+               break;
+#else
+               printf("ERROR: Unsupported binary16 float type\n");
+               abort();
+#endif
+       case SIDE_ATTR_TYPE_FLOAT_BINARY32:
+#if __HAVE_FLOAT32
+               printf("%g", (double) attr->value.u.side_float_binary32);
+               break;
+#else
+               printf("ERROR: Unsupported binary32 float type\n");
+               abort();
+#endif
+       case SIDE_ATTR_TYPE_FLOAT_BINARY64:
+#if __HAVE_FLOAT64
+               printf("%g", (double) attr->value.u.side_float_binary64);
+               break;
+#else
+               printf("ERROR: Unsupported binary64 float type\n");
+               abort();
+#endif
+       case SIDE_ATTR_TYPE_FLOAT_BINARY128:
+#if __HAVE_FLOAT128
+               printf("%Lg", (long double) attr->value.u.side_float_binary128);
+               break;
+#else
+               printf("ERROR: Unsupported binary128 float type\n");
+               abort();
+#endif
+       case SIDE_ATTR_TYPE_STRING:
+               printf("\"%s\"", attr->value.u.string);
+               break;
+       default:
+               printf("<UNKNOWN TYPE>");
+               abort();
+       }
+       printf(" }");
+}
+
+static
+void print_attributes(const char *prefix_str, const struct side_attr *attr, uint32_t nr_attr)
+{
+       int i;
+
+       if (!nr_attr)
+               return;
+       printf("%s[ ", prefix_str);
+       for (i = 0; i < nr_attr; i++) {
+               printf("%s", i ? ", " : "");
+               tracer_print_attr_type(&attr[i]);
+       }
+       printf(" ]");
+}
+
+static
+void print_enum(const struct side_enum_mappings *side_enum_mappings, int64_t value)
+{
+       int i, print_count = 0;
+
+       printf("%" PRId64 ", labels: [ ", value);
+       for (i = 0; i < side_enum_mappings->nr_mappings; i++) {
+               const struct side_enum_mapping *mapping = &side_enum_mappings->mappings[i];
+
+               if (mapping->range_end < mapping->range_begin) {
+                       printf("ERROR: Unexpected enum range: %" PRIu64 "-%" PRIu64 "\n",
+                               mapping->range_begin, mapping->range_end);
+                       abort();
+               }
+               if (value >= mapping->range_begin && value <= mapping->range_end) {
+                       printf("%s", print_count++ ? ", " : "");
+                       printf("\"%s\"", mapping->label);
+               }
+       }
+       if (!print_count)
+               printf("<NO LABEL>");
+       printf(" ]");
+}
+
+static
+void print_enum_bitmap(const struct side_enum_mappings *side_enum_mappings, uint64_t value)
+{
+       int i, print_count = 0;
+
+       printf("0x%" PRIx64 ", labels: [ ", value);
+       for (i = 0; i < side_enum_mappings->nr_mappings; i++) {
+               const struct side_enum_mapping *mapping = &side_enum_mappings->mappings[i];
+               bool match = false;
+               int64_t bit;
+
+               if (mapping->range_begin < 0 || mapping->range_end > 63
+                               || mapping->range_end < mapping->range_begin) {
+                       printf("ERROR: Unexpected enum bitmap range: %" PRIu64 "-%" PRIu64 "\n",
+                               mapping->range_begin, mapping->range_end);
+                       abort();
+               }
+               for (bit = mapping->range_begin; bit <= mapping->range_end; bit++) {
+                       if (value & (1ULL << bit)) {
+                               match = true;
+                               break;
+                       }
+               }
+               if (match) {
+                       printf("%s", print_count++ ? ", " : "");
+                       printf("\"%s\"", mapping->label);
+               }
+       }
+       if (!print_count)
+               printf("<NO LABEL>");
+       printf(" ]");
+}
+
 static
 void tracer_print_type(const struct side_type_description *type_desc, const struct side_arg_vec *item)
 {
@@ -63,6 +210,10 @@ void tracer_print_type(const struct side_type_description *type_desc, const stru
                }
                break;
        }
+       printf("{ ");
+       print_attributes("attr: ", type_desc->attr, type_desc->nr_attr);
+       printf("%s", type_desc->nr_attr ? ", " : "");
+       printf("value: ");
        switch (item->type) {
        case SIDE_TYPE_BOOL:
                printf("%s", item->u.side_bool ? "true" : "false");
@@ -91,6 +242,89 @@ void tracer_print_type(const struct side_type_description *type_desc, const stru
        case SIDE_TYPE_S64:
                printf("%" PRId64, item->u.side_s64);
                break;
+
+       case SIDE_TYPE_ENUM_U8:
+               print_enum(type_desc->u.side_enum_mappings,
+                       (int64_t) item->u.side_u8);
+               break;
+       case SIDE_TYPE_ENUM_U16:
+               print_enum(type_desc->u.side_enum_mappings,
+                       (int64_t) item->u.side_u16);
+               break;
+       case SIDE_TYPE_ENUM_U32:
+               print_enum(type_desc->u.side_enum_mappings,
+                       (int64_t) item->u.side_u32);
+               break;
+       case SIDE_TYPE_ENUM_U64:
+               print_enum(type_desc->u.side_enum_mappings,
+                       (int64_t) item->u.side_u64);
+               break;
+       case SIDE_TYPE_ENUM_S8:
+               print_enum(type_desc->u.side_enum_mappings,
+                       (int64_t) item->u.side_s8);
+               break;
+       case SIDE_TYPE_ENUM_S16:
+               print_enum(type_desc->u.side_enum_mappings,
+                       (int64_t) item->u.side_s16);
+               break;
+       case SIDE_TYPE_ENUM_S32:
+               print_enum(type_desc->u.side_enum_mappings,
+                       (int64_t) item->u.side_s32);
+               break;
+       case SIDE_TYPE_ENUM_S64:
+               print_enum(type_desc->u.side_enum_mappings,
+                       item->u.side_s64);
+               break;
+
+       case SIDE_TYPE_ENUM_BITMAP8:
+               print_enum_bitmap(type_desc->u.side_enum_mappings,
+                       (uint64_t) item->u.side_u8);
+               break;
+       case SIDE_TYPE_ENUM_BITMAP16:
+               print_enum_bitmap(type_desc->u.side_enum_mappings,
+                       (uint64_t) item->u.side_u16);
+               break;
+       case SIDE_TYPE_ENUM_BITMAP32:
+               print_enum_bitmap(type_desc->u.side_enum_mappings,
+                       (uint64_t) item->u.side_u32);
+               break;
+       case SIDE_TYPE_ENUM_BITMAP64:
+               print_enum_bitmap(type_desc->u.side_enum_mappings,
+                       item->u.side_u64);
+               break;
+
+       case SIDE_TYPE_FLOAT_BINARY16:
+#if __HAVE_FLOAT16
+               printf("%g", (double) item->u.side_float_binary16);
+               break;
+#else
+               printf("ERROR: Unsupported binary16 float type\n");
+               abort();
+#endif
+       case SIDE_TYPE_FLOAT_BINARY32:
+#if __HAVE_FLOAT32
+               printf("%g", (double) item->u.side_float_binary32);
+               break;
+#else
+               printf("ERROR: Unsupported binary32 float type\n");
+               abort();
+#endif
+       case SIDE_TYPE_FLOAT_BINARY64:
+#if __HAVE_FLOAT64
+               printf("%g", (double) item->u.side_float_binary64);
+               break;
+#else
+               printf("ERROR: Unsupported binary64 float type\n");
+               abort();
+#endif
+       case SIDE_TYPE_FLOAT_BINARY128:
+#if __HAVE_FLOAT128
+               printf("%Lg", (long double) item->u.side_float_binary128);
+               break;
+#else
+               printf("ERROR: Unsupported binary128 float type\n");
+               abort();
+#endif
        case SIDE_TYPE_STRING:
                printf("\"%s\"", item->u.string);
                break;
@@ -133,6 +367,7 @@ void tracer_print_type(const struct side_type_description *type_desc, const stru
                printf("<UNKNOWN TYPE>");
                abort();
        }
+       printf(" }");
 }
 
 static
@@ -541,6 +776,10 @@ void tracer_print_dynamic_vla_visitor(const struct side_arg_dynamic_vec *item)
 static
 void tracer_print_dynamic(const struct side_arg_dynamic_vec *item)
 {
+       printf("{ ");
+       print_attributes("attr:: ", item->attr, item->nr_attr);
+       printf("%s", item->nr_attr ? ", " : "");
+       printf("value: ");
        switch (item->dynamic_type) {
        case SIDE_DYNAMIC_TYPE_NULL:
                printf("<NULL TYPE>");
@@ -572,6 +811,38 @@ void tracer_print_dynamic(const struct side_arg_dynamic_vec *item)
        case SIDE_DYNAMIC_TYPE_S64:
                printf("%" PRId64, item->u.side_s64);
                break;
+       case SIDE_DYNAMIC_TYPE_FLOAT_BINARY16:
+#if __HAVE_FLOAT16
+               printf("%g", (double) item->u.side_float_binary16);
+               break;
+#else
+               printf("ERROR: Unsupported binary16 float type\n");
+               abort();
+#endif
+       case SIDE_DYNAMIC_TYPE_FLOAT_BINARY32:
+#if __HAVE_FLOAT32
+               printf("%g", (double) item->u.side_float_binary32);
+               break;
+#else
+               printf("ERROR: Unsupported binary32 float type\n");
+               abort();
+#endif
+       case SIDE_DYNAMIC_TYPE_FLOAT_BINARY64:
+#if __HAVE_FLOAT64
+               printf("%g", (double) item->u.side_float_binary64);
+               break;
+#else
+               printf("ERROR: Unsupported binary64 float type\n");
+               abort();
+#endif
+       case SIDE_DYNAMIC_TYPE_FLOAT_BINARY128:
+#if __HAVE_FLOAT128
+               printf("%Lg", (long double) item->u.side_float_binary128);
+               break;
+#else
+               printf("ERROR: Unsupported binary128 float type\n");
+               abort();
+#endif
        case SIDE_DYNAMIC_TYPE_STRING:
                printf("\"%s\"", item->u.string);
                break;
@@ -591,22 +862,7 @@ void tracer_print_dynamic(const struct side_arg_dynamic_vec *item)
                printf("<UNKNOWN TYPE>");
                abort();
        }
-}
-
-static
-void print_attributes(const struct side_attr *attr, uint32_t nr_attr)
-{
-       int i;
-
-       if (!nr_attr)
-               return;
-       printf(", attributes: [ ");
-       for (i = 0; i < nr_attr; i++) {
-               printf("%s", i ? ", " : "");
-               printf("{ key: \"%s\", value: \"%s\" }",
-                       attr[i].key, attr[i].value);
-       }
-       printf(" ]");
+       printf(" }");
 }
 
 static
@@ -623,8 +879,8 @@ void tracer_print_static_fields(const struct side_event_description *desc,
                printf("ERROR: number of fields mismatch between description and arguments\n");
                abort();
        }
-       print_attributes(desc->attr, desc->nr_attr);
-       printf("%s", side_sav_len ? ", " : "");
+       print_attributes(", attributes: ", desc->attr, desc->nr_attr);
+       printf("%s", side_sav_len ? ", fields: [ " : "");
        for (i = 0; i < side_sav_len; i++) {
                printf("%s", i ? ", " : "");
                tracer_print_field(&desc->fields[i], &sav[i]);
@@ -635,11 +891,11 @@ void tracer_print_static_fields(const struct side_event_description *desc,
 
 void tracer_call(const struct side_event_description *desc, const struct side_arg_vec_description *sav_desc)
 {
-       if (side_unlikely(desc->flags & SIDE_EVENT_FLAG_VARIADIC)) {
-               printf("ERROR: unexpected variadic event description\n");
-               abort();
-       }
-       tracer_print_static_fields(desc, sav_desc, NULL);
+       int nr_fields = 0;
+
+       tracer_print_static_fields(desc, sav_desc, &nr_fields);
+       if (nr_fields)
+               printf(" ]");
        printf("\n");
 }
 
@@ -656,11 +912,13 @@ void tracer_call_variadic(const struct side_event_description *desc,
                printf("ERROR: unexpected non-variadic event description\n");
                abort();
        }
-       printf("%s", var_struct_len && !nr_fields ? ", " : "");
+       printf("%s", var_struct_len && !nr_fields ? ", fields: [ " : "");
        for (i = 0; i < var_struct_len; i++, nr_fields++) {
                printf("%s", nr_fields ? ", " : "");
                printf("%s:: ", var_struct->fields[i].field_name);
                tracer_print_dynamic(&var_struct->fields[i].elem);
        }
+       if (i)
+               printf(" ]");
        printf("\n");
 }
This page took 0.028279 seconds and 4 git commands to generate.