From 19fa6aa2063d6e7559c297a4628f55aeeb19f518 Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Mon, 17 Oct 2022 05:27:40 -0400 Subject: [PATCH] Introduce variadic event Signed-off-by: Mathieu Desnoyers --- include/side/trace.h | 22 +++++++++++++++++++++- src/test.c | 19 +++++++++++++++++++ src/tracer.c | 31 +++++++++++++++++++++++++++++-- src/tracer.h | 6 +++++- 4 files changed, 74 insertions(+), 4 deletions(-) diff --git a/include/side/trace.h b/include/side/trace.h index 4b3f323..278b32f 100644 --- a/include/side/trace.h +++ b/include/side/trace.h @@ -234,7 +234,7 @@ struct side_tracer_visitor_ctx { struct side_tracer_dynamic_struct_visitor_ctx { enum side_visitor_status (*write_field)( const struct side_tracer_dynamic_struct_visitor_ctx *tracer_ctx, - const struct side_arg_dynamic_event_field *field); + const struct side_arg_dynamic_event_struct *dynamic_struct); void *priv; /* Private tracer context. */ }; @@ -427,6 +427,7 @@ struct side_tracer_dynamic_vla_visitor_ctx { #define side_arg_list(...) __VA_ARGS__ #define side_event_cond(desc) if (side_unlikely((desc)->enabled)) + #define side_event_call(desc, _sav) \ { \ const struct side_arg_vec side_sav[] = { _sav }; \ @@ -441,6 +442,25 @@ struct side_tracer_dynamic_vla_visitor_ctx { side_event_cond(desc) \ side_event_call(desc, SIDE_PARAM(sav)) +#define side_event_call_variadic(desc, _sav, _var_fields) \ + { \ + const struct side_arg_vec side_sav[] = { _sav }; \ + const struct side_arg_vec_description sav_desc = { \ + .sav = side_sav, \ + .len = SIDE_ARRAY_SIZE(side_sav), \ + }; \ + const struct side_arg_dynamic_event_field side_fields[] = { _var_fields }; \ + const struct side_arg_dynamic_event_struct var_struct = { \ + .fields = side_fields, \ + .len = SIDE_ARRAY_SIZE(side_fields), \ + }; \ + tracer_call_variadic(desc, &sav_desc, &var_struct); \ + } + +#define side_event_variadic(desc, sav, var) \ + side_event_cond(desc) \ + side_event_call_variadic(desc, SIDE_PARAM(sav), SIDE_PARAM(var)) + #define side_define_event(_identifier, _provider, _event, _loglevel, _fields) \ struct side_event_description _identifier = { \ .version = 0, \ diff --git a/src/test.c b/src/test.c index 52ba9a4..099c17b 100644 --- a/src/test.c +++ b/src/test.c @@ -443,6 +443,24 @@ void test_dynamic_nested_vla(void) side_arg_list(side_arg_dynamic(side_arg_dynamic_vla(&myvla)))); } +static side_define_event(my_provider_event_variadic, + "myprovider", "myvariadicevent", SIDE_LOGLEVEL_DEBUG, + side_field_list() +); + +static +void test_variadic(void) +{ + my_provider_event_variadic.enabled = 1; + side_event_variadic(&my_provider_event_variadic, + side_arg_list(), + side_arg_list( + side_arg_dynamic_field("a", side_arg_dynamic_u32(55)), + side_arg_dynamic_field("b", side_arg_dynamic_s8(-4)), + ) + ); +} + int main() { test_fields(); @@ -461,5 +479,6 @@ int main() test_dynamic_vla_struct(); test_dynamic_struct_vla(); test_dynamic_nested_vla(); + test_variadic(); return 0; } diff --git a/src/tracer.c b/src/tracer.c index bc767f6..b48baff 100644 --- a/src/tracer.c +++ b/src/tracer.c @@ -135,9 +135,8 @@ void tracer_print_type(const struct side_type_description *type_desc, const stru 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 @@ -561,3 +560,31 @@ void tracer_call(const struct side_event_description *desc, const struct side_ar } 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) +{ + const struct side_arg_vec *sav = sav_desc->sav; + uint32_t side_sav_len = sav_desc->len, + var_struct_len = var_struct->len; + int i, j; + + 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(); + } + /* static */ + for (i = 0; i < side_sav_len; i++) { + printf("%s", i ? ", " : ""); + tracer_print_field(&desc->fields[i], &sav[i]); + } + /* variadic */ + for (j = 0; j < var_struct_len; j++, i++) { + printf("%s", i ? ", " : ""); + printf("%s:: ", var_struct->fields[j].field_name); + tracer_print_dynamic(&var_struct->fields[j].elem); + } + printf("\n"); +} diff --git a/src/tracer.h b/src/tracer.h index d3ee02b..e3d8e85 100644 --- a/src/tracer.h +++ b/src/tracer.h @@ -8,6 +8,10 @@ #include -void tracer_call(const struct side_event_description *desc, const struct side_arg_vec_description *sav_desc); +void tracer_call(const struct side_event_description *desc, + const struct side_arg_vec_description *sav_desc); +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); #endif -- 2.34.1