From c7a14585e9cfda92da601e88bb8a29123e4973c0 Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Mon, 24 Oct 2022 11:56:58 -0400 Subject: [PATCH] Move support both literal and explicit struct definition Signed-off-by: Mathieu Desnoyers --- include/side/trace.h | 42 +++++++++++++++++++++++++------------ src/test.c | 49 +++++++++++++++++++++++++++++++++++--------- src/tracer.c | 4 ++-- 3 files changed, 70 insertions(+), 25 deletions(-) diff --git a/include/side/trace.h b/include/side/trace.h index 3581b75..4391599 100644 --- a/include/side/trace.h +++ b/include/side/trace.h @@ -227,15 +227,19 @@ struct side_enum_bitmap_mappings { uint32_t nr_mappings; }; +struct side_type_struct { + uint32_t nr_fields; + uint32_t nr_attr; + const struct side_event_field *fields; + const struct side_attr *attr; +}; + struct side_type_description { uint32_t type; /* enum side_type */ uint32_t nr_attr; const struct side_attr *attr; union { - struct { - uint32_t nr_fields; - const struct side_event_field *fields; - } side_struct; + const struct side_type_struct *side_struct; struct { uint32_t length; const struct side_type_description *elem_type; @@ -532,20 +536,32 @@ struct side_tracer_dynamic_vla_visitor_ctx { #define side_field_enum_bitmap64(_name, _mappings, _attr) \ _side_field_enum_bitmap(_name, SIDE_TYPE_ENUM_BITMAP64, SIDE_PARAM(_mappings), SIDE_PARAM(_attr)) -#define side_type_struct(_fields, _attr) \ +#define side_type_struct(_struct) \ { \ .type = SIDE_TYPE_STRUCT, \ - .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \ - .attr = _attr, \ + .nr_attr = 0, \ + .attr = NULL, \ .u = { \ - .side_struct = { \ - .nr_fields = SIDE_ARRAY_SIZE(SIDE_PARAM(_fields)), \ - .fields = _fields, \ - }, \ + .side_struct = _struct, \ }, \ } -#define side_field_struct(_name, _fields, _attr) \ - _side_field(_name, side_type_struct(SIDE_PARAM(_fields), SIDE_PARAM(_attr))) +#define side_field_struct(_name, _struct) \ + _side_field(_name, side_type_struct(SIDE_PARAM(_struct))) + +#define _side_type_struct_define(_fields, _attr) \ + { \ + .nr_fields = SIDE_ARRAY_SIZE(SIDE_PARAM(_fields)), \ + .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \ + .fields = _fields, \ + .attr = _attr, \ + } + +#define side_define_struct(_identifier, _fields, _attr) \ + const struct side_type_struct _identifier = _side_type_struct_define(SIDE_PARAM(_fields), SIDE_PARAM(_attr)) + +#define side_struct_literal(_fields, _attr) \ + SIDE_COMPOUND_LITERAL(const struct side_type_struct, \ + _side_type_struct_define(SIDE_PARAM(_fields), SIDE_PARAM(_attr))) #define side_type_array(_elem_type, _length, _attr) \ { \ diff --git a/src/test.c b/src/test.c index c3a3359..e8abc9a 100644 --- a/src/test.c +++ b/src/test.c @@ -34,27 +34,55 @@ void test_fields(void) side_arg_dynamic(side_arg_dynamic_string("zzz", side_attr_list())))); } -static side_define_event(my_provider_event2, "myprovider", "myevent2", SIDE_LOGLEVEL_DEBUG, +static side_define_event(my_provider_event_struct_literal, "myprovider", "myeventstructliteral", SIDE_LOGLEVEL_DEBUG, side_field_list( - side_field_struct("structfield", - side_field_list( - side_field_u32("x", side_attr_list()), - side_field_s64("y", side_attr_list()), - ), - side_attr_list() + side_field_struct("structliteral", + side_struct_literal( + side_field_list( + side_field_u32("x", side_attr_list()), + side_field_s64("y", side_attr_list()), + ), + side_attr_list() + ) ), side_field_u8("z", side_attr_list()), ), side_attr_list() ); +static +void test_struct_literal(void) +{ + my_provider_event_struct_literal_enabled = 1; + side_event_cond(my_provider_event_struct_literal) { + side_arg_define_vec(mystruct, side_arg_list(side_arg_u32(21), side_arg_s64(22))); + side_event_call(my_provider_event_struct_literal, side_arg_list(side_arg_struct(&mystruct), side_arg_u8(55))); + } +} + +static side_define_struct(mystructdef, + side_field_list( + side_field_u32("x", side_attr_list()), + side_field_s64("y", side_attr_list()), + ), + side_attr_list() +); + +static side_define_event(my_provider_event_struct, "myprovider", "myeventstruct", SIDE_LOGLEVEL_DEBUG, + side_field_list( + side_field_struct("struct", &mystructdef), + side_field_u8("z", side_attr_list()), + ), + side_attr_list() +); + static void test_struct(void) { - my_provider_event2_enabled = 1; - side_event_cond(my_provider_event2) { + my_provider_event_struct_enabled = 1; + side_event_cond(my_provider_event_struct) { side_arg_define_vec(mystruct, side_arg_list(side_arg_u32(21), side_arg_s64(22))); - side_event_call(my_provider_event2, side_arg_list(side_arg_struct(&mystruct), side_arg_u8(55))); + side_event_call(my_provider_event_struct, side_arg_list(side_arg_struct(&mystruct), side_arg_u8(55))); } } @@ -1046,6 +1074,7 @@ void test_blob(void) int main() { test_fields(); + test_struct_literal(); test_struct(); test_array(); test_vla(); diff --git a/src/tracer.c b/src/tracer.c index d2b79ec..0efb244 100644 --- a/src/tracer.c +++ b/src/tracer.c @@ -391,14 +391,14 @@ void tracer_print_struct(const struct side_type_description *type_desc, const st uint32_t side_sav_len = sav_desc->len; int i; - if (type_desc->u.side_struct.nr_fields != side_sav_len) { + if (type_desc->u.side_struct->nr_fields != side_sav_len) { printf("ERROR: number of fields mismatch between description and arguments of structure\n"); abort(); } printf("{ "); for (i = 0; i < side_sav_len; i++) { printf("%s", i ? ", " : ""); - tracer_print_field(&type_desc->u.side_struct.fields[i], &sav[i]); + tracer_print_field(&type_desc->u.side_struct->fields[i], &sav[i]); } printf(" }"); } -- 2.34.1