Implement dynamic type attributes
[libside.git] / src / tracer.c
index 2637f4aa8ff0dd9412fd67cb56db1f740bd1611a..fd8ed5eb7314e0bf04b5e085e7e3d65bfcc5b483 100644 (file)
@@ -17,11 +17,29 @@ void tracer_print_array(const struct side_type_description *type_desc, const str
 static
 void tracer_print_vla(const struct side_type_description *type_desc, const struct side_arg_vec_description *sav_desc);
 static
-void tracer_print_vla_visitor(const struct side_type_description *type_desc, void *ctx);
+void tracer_print_vla_visitor(const struct side_type_description *type_desc, void *app_ctx);
 static
 void tracer_print_array_fixint(const struct side_type_description *type_desc, const struct side_arg_vec *item);
 static
 void tracer_print_vla_fixint(const struct side_type_description *type_desc, const struct side_arg_vec *item);
+static
+void tracer_print_dynamic(const struct side_arg_dynamic_vec *dynamic_item);
+
+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 ? ", " : "");
+               printf("{ key: \"%s\", value: \"%s\" }",
+                       attr[i].key, attr[i].value);
+       }
+       printf(" ]");
+}
 
 static
 void tracer_print_type(const struct side_type_description *type_desc, const struct side_arg_vec *item)
@@ -55,13 +73,20 @@ void tracer_print_type(const struct side_type_description *type_desc, const stru
                break;
 
        default:
-               if (type_desc->type != SIDE_TYPE_DYNAMIC && type_desc->type != item->type) {
+               if (type_desc->type != item->type) {
                        printf("ERROR: type mismatch between description and arguments\n");
                        abort();
                }
                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");
+               break;
        case SIDE_TYPE_U8:
                printf("%" PRIu8, item->u.side_u8);
                break;
@@ -87,7 +112,7 @@ void tracer_print_type(const struct side_type_description *type_desc, const stru
                printf("%" PRId64, item->u.side_s64);
                break;
        case SIDE_TYPE_STRING:
-               printf("%s", item->u.string);
+               printf("\"%s\"", item->u.string);
                break;
        case SIDE_TYPE_STRUCT:
                tracer_print_struct(type_desc, item->u.side_struct);
@@ -99,7 +124,7 @@ void tracer_print_type(const struct side_type_description *type_desc, const stru
                tracer_print_vla(type_desc, item->u.side_vla);
                break;
        case SIDE_TYPE_VLA_VISITOR:
-               tracer_print_vla_visitor(type_desc, item->u.side_vla_visitor_ctx);
+               tracer_print_vla_visitor(type_desc, item->u.side_vla_app_visitor_ctx);
                break;
        case SIDE_TYPE_ARRAY_U8:
        case SIDE_TYPE_ARRAY_U16:
@@ -121,18 +146,21 @@ void tracer_print_type(const struct side_type_description *type_desc, const stru
        case SIDE_TYPE_VLA_S64:
                tracer_print_vla_fixint(type_desc, item);
                break;
+       case SIDE_TYPE_DYNAMIC:
+               tracer_print_dynamic(&item->u.dynamic);
+               break;
        default:
                printf("<UNKNOWN TYPE>");
                abort();
        }
+       printf(" }");
 }
 
 static
 void tracer_print_field(const struct side_event_field *item_desc, const struct side_arg_vec *item)
 {
-       printf("(\"%s\", ", item_desc->field_name);
+       printf("%s: ", item_desc->field_name);
        tracer_print_type(&item_desc->side_type, item);
-       printf(")");
 }
 
 static
@@ -188,44 +216,45 @@ void tracer_print_vla(const struct side_type_description *type_desc, const struc
        printf(" ]");
 }
 
+struct tracer_visitor_priv {
+       const struct side_type_description *elem_type;
+       int i;
+};
+
+static
+enum side_visitor_status tracer_write_elem_cb(const struct side_tracer_visitor_ctx *tracer_ctx,
+                       const struct side_arg_vec *elem)
+{
+       struct tracer_visitor_priv *tracer_priv = tracer_ctx->priv;
+
+       printf("%s", tracer_priv->i++ ? ", " : "");
+       tracer_print_type(tracer_priv->elem_type, elem);
+       return SIDE_VISITOR_STATUS_OK;
+}
+
 static
-void tracer_print_vla_visitor(const struct side_type_description *type_desc, void *ctx)
+void tracer_print_vla_visitor(const struct side_type_description *type_desc, void *app_ctx)
 {
        enum side_visitor_status status;
-       int i;
+       struct tracer_visitor_priv tracer_priv = {
+               .elem_type = type_desc->u.side_vla_visitor.elem_type,
+               .i = 0,
+       };
+       const struct side_tracer_visitor_ctx tracer_ctx = {
+               .write_elem = tracer_write_elem_cb,
+               .priv = &tracer_priv,
+       };
 
-       status = type_desc->u.side_vla_visitor.begin(ctx);
-       if (status != SIDE_VISITOR_STATUS_OK) {
+       printf("[ ");
+       status = type_desc->u.side_vla_visitor.visitor(&tracer_ctx, app_ctx);
+       switch (status) {
+       case SIDE_VISITOR_STATUS_OK:
+               break;
+       case SIDE_VISITOR_STATUS_ERROR:
                printf("ERROR: Visitor error\n");
                abort();
        }
-
-       printf("[ ");
-       status = SIDE_VISITOR_STATUS_OK;
-       for (i = 0; status == SIDE_VISITOR_STATUS_OK; i++) {
-               struct side_arg_vec sav_elem;
-
-               status = type_desc->u.side_vla_visitor.get_next(ctx, &sav_elem);
-               switch (status) {
-               case SIDE_VISITOR_STATUS_OK:
-                       break;
-               case SIDE_VISITOR_STATUS_ERROR:
-                       printf("ERROR: Visitor error\n");
-                       abort();
-               case SIDE_VISITOR_STATUS_END:
-                       continue;
-               }
-               printf("%s", i ? ", " : "");
-               tracer_print_type(type_desc->u.side_vla_visitor.elem_type, &sav_elem);
-       }
        printf(" ]");
-       if (type_desc->u.side_vla_visitor.end) {
-               status = type_desc->u.side_vla_visitor.end(ctx);
-               if (status != SIDE_VISITOR_STATUS_OK) {
-                       printf("ERROR: Visitor error\n");
-                       abort();
-               }
-       }
 }
 
 void tracer_print_array_fixint(const struct side_type_description *type_desc, const struct side_arg_vec *item)
@@ -236,70 +265,43 @@ void tracer_print_array_fixint(const struct side_type_description *type_desc, co
        enum side_type side_type;
        int i;
 
-       if (elem_type->type != SIDE_TYPE_DYNAMIC) {
-               switch (item->type) {
-               case SIDE_TYPE_ARRAY_U8:
-                       if (elem_type->type != SIDE_TYPE_U8)
-                               goto type_error;
-                       break;
-               case SIDE_TYPE_ARRAY_U16:
-                       if (elem_type->type != SIDE_TYPE_U16)
-                               goto type_error;
-                       break;
-               case SIDE_TYPE_ARRAY_U32:
-                       if (elem_type->type != SIDE_TYPE_U32)
-                               goto type_error;
-                       break;
-               case SIDE_TYPE_ARRAY_U64:
-                       if (elem_type->type != SIDE_TYPE_U64)
-                               goto type_error;
-                       break;
-               case SIDE_TYPE_ARRAY_S8:
-                       if (elem_type->type != SIDE_TYPE_S8)
-                               goto type_error;
-                       break;
-               case SIDE_TYPE_ARRAY_S16:
-                       if (elem_type->type != SIDE_TYPE_S16)
-                               goto type_error;
-                       break;
-               case SIDE_TYPE_ARRAY_S32:
-                       if (elem_type->type != SIDE_TYPE_S32)
-                               goto type_error;
-                       break;
-               case SIDE_TYPE_ARRAY_S64:
-                       if (elem_type->type != SIDE_TYPE_S64)
-                               goto type_error;
-                       break;
-               }
-               side_type = elem_type->type;
-       } else {
-               switch (item->type) {
-               case SIDE_TYPE_ARRAY_U8:
-                       side_type = SIDE_TYPE_U8;
-                       break;
-               case SIDE_TYPE_ARRAY_U16:
-                       side_type = SIDE_TYPE_U16;
-                       break;
-               case SIDE_TYPE_ARRAY_U32:
-                       side_type = SIDE_TYPE_U32;
-                       break;
-               case SIDE_TYPE_ARRAY_U64:
-                       side_type = SIDE_TYPE_U64;
-                       break;
-               case SIDE_TYPE_ARRAY_S8:
-                       side_type = SIDE_TYPE_S8;
-                       break;
-               case SIDE_TYPE_ARRAY_S16:
-                       side_type = SIDE_TYPE_S16;
-                       break;
-               case SIDE_TYPE_ARRAY_S32:
-                       side_type = SIDE_TYPE_S32;
-                       break;
-               case SIDE_TYPE_ARRAY_S64:
-                       side_type = SIDE_TYPE_S64;
-                       break;
-               }
+       switch (item->type) {
+       case SIDE_TYPE_ARRAY_U8:
+               if (elem_type->type != SIDE_TYPE_U8)
+                       goto type_error;
+               break;
+       case SIDE_TYPE_ARRAY_U16:
+               if (elem_type->type != SIDE_TYPE_U16)
+                       goto type_error;
+               break;
+       case SIDE_TYPE_ARRAY_U32:
+               if (elem_type->type != SIDE_TYPE_U32)
+                       goto type_error;
+               break;
+       case SIDE_TYPE_ARRAY_U64:
+               if (elem_type->type != SIDE_TYPE_U64)
+                       goto type_error;
+               break;
+       case SIDE_TYPE_ARRAY_S8:
+               if (elem_type->type != SIDE_TYPE_S8)
+                       goto type_error;
+               break;
+       case SIDE_TYPE_ARRAY_S16:
+               if (elem_type->type != SIDE_TYPE_S16)
+                       goto type_error;
+               break;
+       case SIDE_TYPE_ARRAY_S32:
+               if (elem_type->type != SIDE_TYPE_S32)
+                       goto type_error;
+               break;
+       case SIDE_TYPE_ARRAY_S64:
+               if (elem_type->type != SIDE_TYPE_S64)
+                       goto type_error;
+               break;
+       default:
+               goto type_error;
        }
+       side_type = elem_type->type;
 
        printf("[ ");
        for (i = 0; i < side_sav_len; i++) {
@@ -357,74 +359,43 @@ void tracer_print_vla_fixint(const struct side_type_description *type_desc, cons
        enum side_type side_type;
        int i;
 
-       if (elem_type->type != SIDE_TYPE_DYNAMIC) {
-               switch (item->type) {
-               case SIDE_TYPE_VLA_U8:
-                       if (elem_type->type != SIDE_TYPE_U8)
-                               goto type_error;
-                       break;
-               case SIDE_TYPE_VLA_U16:
-                       if (elem_type->type != SIDE_TYPE_U16)
-                               goto type_error;
-                       break;
-               case SIDE_TYPE_VLA_U32:
-                       if (elem_type->type != SIDE_TYPE_U32)
-                               goto type_error;
-                       break;
-               case SIDE_TYPE_VLA_U64:
-                       if (elem_type->type != SIDE_TYPE_U64)
-                               goto type_error;
-                       break;
-               case SIDE_TYPE_VLA_S8:
-                       if (elem_type->type != SIDE_TYPE_S8)
-                               goto type_error;
-                       break;
-               case SIDE_TYPE_VLA_S16:
-                       if (elem_type->type != SIDE_TYPE_S16)
-                               goto type_error;
-                       break;
-               case SIDE_TYPE_VLA_S32:
-                       if (elem_type->type != SIDE_TYPE_S32)
-                               goto type_error;
-                       break;
-               case SIDE_TYPE_VLA_S64:
-                       if (elem_type->type != SIDE_TYPE_S64)
-                               goto type_error;
-                       break;
-               default:
+       switch (item->type) {
+       case SIDE_TYPE_VLA_U8:
+               if (elem_type->type != SIDE_TYPE_U8)
                        goto type_error;
-               }
-               side_type = elem_type->type;
-       } else {
-               switch (item->type) {
-               case SIDE_TYPE_VLA_U8:
-                       side_type = SIDE_TYPE_U8;
-                       break;
-               case SIDE_TYPE_VLA_U16:
-                       side_type = SIDE_TYPE_U16;
-                       break;
-               case SIDE_TYPE_VLA_U32:
-                       side_type = SIDE_TYPE_U32;
-                       break;
-               case SIDE_TYPE_VLA_U64:
-                       side_type = SIDE_TYPE_U64;
-                       break;
-               case SIDE_TYPE_VLA_S8:
-                       side_type = SIDE_TYPE_S8;
-                       break;
-               case SIDE_TYPE_VLA_S16:
-                       side_type = SIDE_TYPE_S16;
-                       break;
-               case SIDE_TYPE_VLA_S32:
-                       side_type = SIDE_TYPE_S32;
-                       break;
-               case SIDE_TYPE_VLA_S64:
-                       side_type = SIDE_TYPE_S64;
-                       break;
-               default:
+               break;
+       case SIDE_TYPE_VLA_U16:
+               if (elem_type->type != SIDE_TYPE_U16)
                        goto type_error;
-               }
+               break;
+       case SIDE_TYPE_VLA_U32:
+               if (elem_type->type != SIDE_TYPE_U32)
+                       goto type_error;
+               break;
+       case SIDE_TYPE_VLA_U64:
+               if (elem_type->type != SIDE_TYPE_U64)
+                       goto type_error;
+               break;
+       case SIDE_TYPE_VLA_S8:
+               if (elem_type->type != SIDE_TYPE_S8)
+                       goto type_error;
+               break;
+       case SIDE_TYPE_VLA_S16:
+               if (elem_type->type != SIDE_TYPE_S16)
+                       goto type_error;
+               break;
+       case SIDE_TYPE_VLA_S32:
+               if (elem_type->type != SIDE_TYPE_S32)
+                       goto type_error;
+               break;
+       case SIDE_TYPE_VLA_S64:
+               if (elem_type->type != SIDE_TYPE_S64)
+                       goto type_error;
+               break;
+       default:
+               goto type_error;
        }
+       side_type = elem_type->type;
 
        printf("[ ");
        for (i = 0; i < side_sav_len; i++) {
@@ -474,20 +445,238 @@ type_error:
        abort();
 }
 
-void tracer_call(const struct side_event_description *desc, const struct side_arg_vec_description *sav_desc)
+static
+void tracer_print_dynamic_struct(const struct side_arg_dynamic_event_struct *dynamic_struct)
+{
+       const struct side_arg_dynamic_event_field *fields = dynamic_struct->fields;
+       uint32_t len = dynamic_struct->len;
+       int i;
+
+       printf("[ ");
+       for (i = 0; i < len; i++) {
+               printf("%s", i ? ", " : "");
+               printf("%s:: ", fields[i].field_name);
+               tracer_print_dynamic(&fields[i].elem);
+       }
+       printf(" ]");
+}
+
+struct tracer_dynamic_struct_visitor_priv {
+       int i;
+};
+
+static
+enum side_visitor_status tracer_dynamic_struct_write_elem_cb(
+                       const struct side_tracer_dynamic_struct_visitor_ctx *tracer_ctx,
+                       const struct side_arg_dynamic_event_field *dynamic_field)
+{
+       struct tracer_dynamic_struct_visitor_priv *tracer_priv = tracer_ctx->priv;
+
+       printf("%s", tracer_priv->i++ ? ", " : "");
+       printf("%s:: ", dynamic_field->field_name);
+       tracer_print_dynamic(&dynamic_field->elem);
+       return SIDE_VISITOR_STATUS_OK;
+}
+
+static
+void tracer_print_dynamic_struct_visitor(const struct side_arg_dynamic_vec *item)
+{
+       enum side_visitor_status status;
+       struct tracer_dynamic_struct_visitor_priv tracer_priv = {
+               .i = 0,
+       };
+       const struct side_tracer_dynamic_struct_visitor_ctx tracer_ctx = {
+               .write_field = tracer_dynamic_struct_write_elem_cb,
+               .priv = &tracer_priv,
+       };
+       void *app_ctx = item->u.side_dynamic_struct_visitor.app_ctx;
+
+       printf("[ ");
+       status = item->u.side_dynamic_struct_visitor.visitor(&tracer_ctx, app_ctx);
+       switch (status) {
+       case SIDE_VISITOR_STATUS_OK:
+               break;
+       case SIDE_VISITOR_STATUS_ERROR:
+               printf("ERROR: Visitor error\n");
+               abort();
+       }
+       printf(" ]");
+}
+
+static
+void tracer_print_dynamic_vla(const struct side_arg_dynamic_vec_vla *vla)
+{
+       const struct side_arg_dynamic_vec *sav = vla->sav;
+       uint32_t side_sav_len = vla->len;
+       int i;
+
+       printf("[ ");
+       for (i = 0; i < side_sav_len; i++) {
+               printf("%s", i ? ", " : "");
+               tracer_print_dynamic(&sav[i]);
+       }
+       printf(" ]");
+}
+
+struct tracer_dynamic_vla_visitor_priv {
+       int i;
+};
+
+static
+enum side_visitor_status tracer_dynamic_vla_write_elem_cb(
+                       const struct side_tracer_dynamic_vla_visitor_ctx *tracer_ctx,
+                       const struct side_arg_dynamic_vec *elem)
+{
+       struct tracer_dynamic_vla_visitor_priv *tracer_priv = tracer_ctx->priv;
+
+       printf("%s", tracer_priv->i++ ? ", " : "");
+       tracer_print_dynamic(elem);
+       return SIDE_VISITOR_STATUS_OK;
+}
+
+static
+void tracer_print_dynamic_vla_visitor(const struct side_arg_dynamic_vec *item)
+{
+       enum side_visitor_status status;
+       struct tracer_dynamic_vla_visitor_priv tracer_priv = {
+               .i = 0,
+       };
+       const struct side_tracer_dynamic_vla_visitor_ctx tracer_ctx = {
+               .write_elem = tracer_dynamic_vla_write_elem_cb,
+               .priv = &tracer_priv,
+       };
+       void *app_ctx = item->u.side_dynamic_vla_visitor.app_ctx;
+
+       printf("[ ");
+       status = item->u.side_dynamic_vla_visitor.visitor(&tracer_ctx, app_ctx);
+       switch (status) {
+       case SIDE_VISITOR_STATUS_OK:
+               break;
+       case SIDE_VISITOR_STATUS_ERROR:
+               printf("ERROR: Visitor error\n");
+               abort();
+       }
+       printf(" ]");
+}
+
+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>");
+               break;
+       case SIDE_DYNAMIC_TYPE_BOOL:
+               printf("%s", item->u.side_bool ? "true" : "false");
+               break;
+       case SIDE_DYNAMIC_TYPE_U8:
+               printf("%" PRIu8, item->u.side_u8);
+               break;
+       case SIDE_DYNAMIC_TYPE_U16:
+               printf("%" PRIu16, item->u.side_u16);
+               break;
+       case SIDE_DYNAMIC_TYPE_U32:
+               printf("%" PRIu32, item->u.side_u32);
+               break;
+       case SIDE_DYNAMIC_TYPE_U64:
+               printf("%" PRIu64, item->u.side_u64);
+               break;
+       case SIDE_DYNAMIC_TYPE_S8:
+               printf("%" PRId8, item->u.side_s8);
+               break;
+       case SIDE_DYNAMIC_TYPE_S16:
+               printf("%" PRId16, item->u.side_s16);
+               break;
+       case SIDE_DYNAMIC_TYPE_S32:
+               printf("%" PRId32, item->u.side_s32);
+               break;
+       case SIDE_DYNAMIC_TYPE_S64:
+               printf("%" PRId64, item->u.side_s64);
+               break;
+       case SIDE_DYNAMIC_TYPE_STRING:
+               printf("\"%s\"", item->u.string);
+               break;
+       case SIDE_DYNAMIC_TYPE_STRUCT:
+               tracer_print_dynamic_struct(item->u.side_dynamic_struct);
+               break;
+       case SIDE_DYNAMIC_TYPE_STRUCT_VISITOR:
+               tracer_print_dynamic_struct_visitor(item);
+               break;
+       case SIDE_DYNAMIC_TYPE_VLA:
+               tracer_print_dynamic_vla(item->u.side_dynamic_vla);
+               break;
+       case SIDE_DYNAMIC_TYPE_VLA_VISITOR:
+               tracer_print_dynamic_vla_visitor(item);
+               break;
+       default:
+               printf("<UNKNOWN TYPE>");
+               abort();
+       }
+       printf(" }");
+}
+
+static
+void tracer_print_static_fields(const struct side_event_description *desc,
+               const struct side_arg_vec_description *sav_desc,
+               int *nr_items)
 {
        const struct side_arg_vec *sav = sav_desc->sav;
        uint32_t side_sav_len = sav_desc->len;
        int i;
 
-       printf("provider: %s, event: %s", desc->provider_name, desc->event_name);
+       printf("provider: %s, event: %s", desc->provider_name, desc->event_name);
        if (desc->nr_fields != side_sav_len) {
                printf("ERROR: number of fields mismatch between description and arguments\n");
                abort();
        }
+       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]);
        }
+       if (nr_items)
+               *nr_items = i;
+}
+
+void tracer_call(const struct side_event_description *desc, const struct side_arg_vec_description *sav_desc)
+{
+       int nr_fields = 0;
+
+       if (side_unlikely(desc->flags & SIDE_EVENT_FLAG_VARIADIC)) {
+               printf("ERROR: unexpected variadic event description\n");
+               abort();
+       }
+       tracer_print_static_fields(desc, sav_desc, &nr_fields);
+       if (nr_fields)
+               printf(" ]");
+       printf("\n");
+}
+
+void tracer_call_variadic(const struct side_event_description *desc,
+       const struct side_arg_vec_description *sav_desc,
+       const struct side_arg_dynamic_event_struct *var_struct)
+{
+       uint32_t var_struct_len = var_struct->len;
+       int nr_fields = 0, i;
+
+       tracer_print_static_fields(desc, sav_desc, &nr_fields);
+
+       if (side_unlikely(!(desc->flags & SIDE_EVENT_FLAG_VARIADIC))) {
+               printf("ERROR: unexpected non-variadic event description\n");
+               abort();
+       }
+       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.029804 seconds and 4 git commands to generate.