From 7d34edfc1c4e6f874ce3a757373d2e4c5b547f0d Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Thu, 10 Nov 2022 09:46:42 -0500 Subject: [PATCH] Implement gather string Signed-off-by: Mathieu Desnoyers --- include/side/trace.h | 31 +++++++++++++++++++++++++++++++ src/test.c | 39 +++++++++++++++++++++++++++++++++++++++ src/tracer.c | 29 +++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+) diff --git a/include/side/trace.h b/include/side/trace.h index 5c182ac..e2eb419 100644 --- a/include/side/trace.h +++ b/include/side/trace.h @@ -122,6 +122,7 @@ enum side_type_label { SIDE_TYPE_GATHER_BYTE, SIDE_TYPE_GATHER_POINTER, SIDE_TYPE_GATHER_FLOAT, + SIDE_TYPE_GATHER_STRING, /* Gather compound types */ SIDE_TYPE_GATHER_STRUCT, @@ -398,6 +399,12 @@ struct side_type_gather_float { struct side_type_float type; } SIDE_PACKED; +struct side_type_gather_string { + uint64_t offset; /* bytes */ + uint8_t access_mode; /* enum side_type_gather_access_mode */ + struct side_type_string type; +} SIDE_PACKED; + struct side_type_gather_enum { const struct side_enum_mappings *mappings; const struct side_type *elem_type; @@ -430,6 +437,7 @@ struct side_type_gather { struct side_type_gather_byte side_byte; struct side_type_gather_integer side_integer; struct side_type_gather_float side_float; + struct side_type_gather_string side_string; struct side_type_gather_enum side_enum; struct side_type_gather_array side_array; struct side_type_gather_vla side_vla; @@ -504,6 +512,7 @@ struct side_arg_static { void *side_byte_gather_ptr; void *side_integer_gather_ptr; void *side_float_gather_ptr; + void *side_string_gather_ptr; /* Gather compound types */ void *side_array_gather_ptr; @@ -1141,6 +1150,27 @@ struct side_event_description { #define side_field_gather_float_be(_name, _offset, _float_size, _access_mode, _attr) \ _side_field(_name, side_type_gather_float_be(_offset, _float_size, _access_mode, _attr)) +#define side_type_gather_string(_offset, _access_mode, _attr) \ + { \ + .type = SIDE_TYPE_GATHER_STRING, \ + .u = { \ + .side_gather = { \ + .u = { \ + .side_string = { \ + .offset = _offset, \ + .access_mode = _access_mode, \ + .type = { \ + .attr = _attr, \ + .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \ + }, \ + }, \ + }, \ + }, \ + }, \ + } +#define side_field_gather_string(_name, _offset, _access_mode, _attr) \ + _side_field(_name, side_type_gather_string(_offset, _access_mode, SIDE_PARAM(_attr))) + #define side_type_gather_enum(_mappings, _elem_type) \ { \ .type = SIDE_TYPE_GATHER_ENUM, \ @@ -1261,6 +1291,7 @@ struct side_event_description { #define side_arg_gather_pointer(_ptr) { .type = SIDE_TYPE_GATHER_POINTER, .u = { .side_static = { .side_integer_gather_ptr = (_ptr) } } } #define side_arg_gather_integer(_ptr) { .type = SIDE_TYPE_GATHER_INTEGER, .u = { .side_static = { .side_integer_gather_ptr = (_ptr) } } } #define side_arg_gather_float(_ptr) { .type = SIDE_TYPE_GATHER_FLOAT, .u = { .side_static = { .side_float_gather_ptr = (_ptr) } } } +#define side_arg_gather_string(_ptr) { .type = SIDE_TYPE_GATHER_STRING, .u = { .side_static = { .side_string_gather_ptr = (_ptr) } } } #define side_arg_gather_struct(_ptr) { .type = SIDE_TYPE_GATHER_STRUCT, .u = { .side_static = { .side_struct_gather_ptr = (_ptr) } } } #define side_arg_gather_array(_ptr) { .type = SIDE_TYPE_GATHER_ARRAY, .u = { .side_static = { .side_array_gather_ptr = (_ptr) } } } #define side_arg_gather_vla(_ptr, _length_ptr) { .type = SIDE_TYPE_GATHER_VLA, .u = { .side_static = { .side_vla_gather = { .ptr = (_ptr), .length_ptr = (_length_ptr) } } } } diff --git a/src/test.c b/src/test.c index 7b8bf2b..e4bb1ea 100644 --- a/src/test.c +++ b/src/test.c @@ -2118,6 +2118,44 @@ void test_gather_enum(void) ); } +side_static_event(my_provider_event_gatherstring, + "myprovider", "myeventgatherstring", SIDE_LOGLEVEL_DEBUG, + side_field_list( + side_field_gather_string("string", 0, SIDE_TYPE_GATHER_ACCESS_DIRECT, side_attr_list()), + side_field_gather_array("arrayptr", + side_elem(side_type_gather_string(0, SIDE_TYPE_GATHER_ACCESS_POINTER, side_attr_list())), + 3, 0, SIDE_TYPE_GATHER_ACCESS_DIRECT, side_attr_list() + ), + side_field_gather_array("array", + side_elem(side_type_gather_string(0, SIDE_TYPE_GATHER_ACCESS_DIRECT, side_attr_list())), + 3, 0, SIDE_TYPE_GATHER_ACCESS_DIRECT, side_attr_list() + ), + ), + side_attr_list() +); + +static +void test_gather_string(void) +{ + side_event_cond(my_provider_event_gatherstring) { + char *str1 = "abcdef"; + char *ptrarray[3] = { + "abc", + "def", + "ghi", + }; + char flatarray[] = { 'a', 'b', '\0', 'c', 'd', '\0', 'e', 'f', '\0' }; + + side_event_call(my_provider_event_gatherstring, + side_arg_list( + side_arg_gather_string(str1), + side_arg_gather_array(ptrarray), + side_arg_gather_array(flatarray), + ) + ); + } +} + int main() { test_fields(); @@ -2167,5 +2205,6 @@ int main() test_gather_bool(); test_gather_pointer(); test_gather_enum(); + test_gather_string(); return 0; } diff --git a/src/tracer.c b/src/tracer.c index 864799e..9a4d39a 100644 --- a/src/tracer.c +++ b/src/tracer.c @@ -46,6 +46,8 @@ uint32_t tracer_print_gather_integer_type(const struct side_type_gather *type_ga static uint32_t tracer_print_gather_float_type(const struct side_type_gather *type_gather, const void *_ptr); static +uint32_t tracer_print_gather_string_type(const struct side_type_gather *type_gather, const void *_ptr); +static uint32_t tracer_print_gather_enum_type(const struct side_type_gather *type_gather, const void *_ptr); static uint32_t tracer_print_gather_struct(const struct side_type_gather *type_gather, const void *_ptr); @@ -847,6 +849,9 @@ void tracer_print_type(const struct side_type *type_desc, const struct side_arg case SIDE_TYPE_GATHER_FLOAT: (void) tracer_print_gather_float_type(&type_desc->u.side_gather, item->u.side_static.side_float_gather_ptr); break; + case SIDE_TYPE_GATHER_STRING: + (void) tracer_print_gather_string_type(&type_desc->u.side_gather, item->u.side_static.side_string_gather_ptr); + break; /* Gather compound type */ case SIDE_TYPE_GATHER_STRUCT: @@ -1088,6 +1093,27 @@ uint32_t tracer_print_gather_float_type(const struct side_type_gather *type_gath return tracer_gather_size(access_mode, float_size_bytes); } +static +uint32_t tracer_print_gather_string_type(const struct side_type_gather *type_gather, const void *_ptr) +{ + enum side_type_gather_access_mode access_mode = + (enum side_type_gather_access_mode) type_gather->u.side_string.access_mode; + const char *ptr = (const char *) _ptr; + size_t string_len; + + ptr = tracer_gather_access(access_mode, ptr + type_gather->u.side_string.offset); + tracer_print_type_header(":", type_gather->u.side_string.type.attr, + type_gather->u.side_string.type.nr_attr); + if (ptr) { + printf("\"%s\"", ptr); + string_len = strlen(ptr) + 1; + } else { + printf(""); + string_len = 1; + } + return tracer_gather_size(access_mode, string_len); +} + static uint32_t tracer_print_gather_type(const struct side_type *type_desc, const void *ptr) { @@ -1113,6 +1139,9 @@ uint32_t tracer_print_gather_type(const struct side_type *type_desc, const void case SIDE_TYPE_GATHER_FLOAT: len = tracer_print_gather_float_type(&type_desc->u.side_gather, ptr); break; + case SIDE_TYPE_GATHER_STRING: + len = tracer_print_gather_string_type(&type_desc->u.side_gather, ptr); + break; /* Gather enum types */ case SIDE_TYPE_GATHER_ENUM: -- 2.34.1