Move support both literal and explicit struct definition
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Mon, 24 Oct 2022 15:56:58 +0000 (11:56 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Mon, 24 Oct 2022 15:56:58 +0000 (11:56 -0400)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
include/side/trace.h
src/test.c
src/tracer.c

index 3581b75c9f3b2ea01f5f94098bff4bf7bb8c16e7..439159979a279736c7d8b6918e7ec9c4a5c40d45 100644 (file)
@@ -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) \
        { \
index c3a335976b41ac0318ab2d0b67f690629e71a466..e8abc9ad9ff27722e4ecdc864ff359badaff4894 100644 (file)
@@ -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();
index d2b79ec7393faf117fbc545b0b2ced9604378fce..0efb24494b7c9ec31cf061229870ad8652a4bdab 100644 (file)
@@ -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(" }");
 }
This page took 0.026509 seconds and 4 git commands to generate.