Skip to content

Commit

Permalink
Logging attempt 2 (#10)
Browse files Browse the repository at this point in the history
* testing windows compatible approach

* some improvements

* one more fix

* reflected changes in the test

* added base types

* introduced actual type

* fixed stack smash

* added some debug

* fixed complex type

* added support for clang
  • Loading branch information
aodinokov authored Jun 23, 2024
1 parent 81bdf27 commit 44cd76f
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 55 deletions.
70 changes: 42 additions & 28 deletions examples/c_print_args/print_args.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void vprint_args(metac_tag_map_t * p_tag_map, metac_flag_t calling, metac_entry_

printf("%s(", metac_entry_name(p_entry));

char buf[16];
char buf[32];

for (int i = 0; i < metac_entry_paremeters_count(p_entry); ++i) {
if (i > 0) {
Expand All @@ -40,40 +40,54 @@ void vprint_args(metac_tag_map_t * p_tag_map, metac_flag_t calling, metac_entry_
// something is wrong
break;
}

metac_size_t param_byte_sz = 0;
if (metac_entry_byte_size(p_param_type_entry, &param_byte_sz) != 0) {
// something is wrong
break;
}


int handled = 0;
#define _handle_sz_(_sz_) \
do { \
if (param_byte_sz == _sz_) { \
char *x = va_arg(args, char[_sz_]); \
if (x == NULL) { break; } \
memcpy(buf, x, param_byte_sz); \
handled = 1; \
} \
} while(0)
_handle_sz_(1);
_handle_sz_(2);
_handle_sz_(3);
_handle_sz_(4);
_handle_sz_(5);
_handle_sz_(6);
_handle_sz_(7);
_handle_sz_(8);
_handle_sz_(9);
_handle_sz_(10);
_handle_sz_(11);
_handle_sz_(12);
_handle_sz_(13);
_handle_sz_(14);
_handle_sz_(15);
_handle_sz_(16);
#undef _handle_sz_

if (metac_entry_is_base_type(p_param_type_entry) != 0) {
// take what type of base type it is. It can be char, unsigned char.. etc
metac_name_t param_base_type_name = metac_entry_base_type_name(p_param_type_entry);
#define _base_type_arg_(_type_, _va_type_, _pseudoname_) \
do { \
if (handled == 0 && strcmp(param_base_type_name, #_pseudoname_) == 0 && param_byte_sz == sizeof(_type_)) { \
_type_ val = va_arg(args, _va_type_); \
memcpy(buf, &val, sizeof(val)); \
handled = 1; \
} \
} while(0)
// handle all known base types
_base_type_arg_(char, int, char);
_base_type_arg_(unsigned char, int, unsigned char);
_base_type_arg_(short, int, short int);
_base_type_arg_(unsigned short, int, unsigned short int);
_base_type_arg_(int, int, int);
_base_type_arg_(unsigned int, unsigned int, unsigned int);
_base_type_arg_(long, long, long int);
_base_type_arg_(unsigned long, unsigned long, unsigned long int);
_base_type_arg_(long long, long long, long long int);
_base_type_arg_(unsigned long long, unsigned long long, unsigned long long int);
_base_type_arg_(bool, int, _Bool);
_base_type_arg_(float, double, float);
_base_type_arg_(double, double, double);
_base_type_arg_(long double, long double, long double);
_base_type_arg_(float complex, float complex, complex float);
_base_type_arg_(double complex, double complex, complex double);
_base_type_arg_(long double complex, long double complex, long complex double);
#undef _base_type_arg_
} else if (metac_entry_is_pointer(p_param_type_entry) != 0){
do {
if (handled == 0 ) {
void * val = va_arg(args, void *);
memcpy(buf, &val, sizeof(val));
handled = 1;
}
} while(0);
}
if (handled == 0) {
break;
}
Expand Down
81 changes: 75 additions & 6 deletions examples/c_print_args/print_args_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,82 @@

#include "print_args.c"

int test_function2_with_args(int *a, short b){
return *a + b + 999;
void test_function_with_base_args(
char arg_00,
unsigned char arg_01,
short arg_02,
unsigned short arg_03,
int arg_04,
unsigned int arg_05,
long arg_06,
unsigned long arg_07,
long long arg_08,
unsigned long long arg_09,
bool arg_10,
float arg_11,
double arg_12,
long double arg_13,
float complex arg_14,
double complex arg_15,
long double complex arg_16
){
printf("arg_11 %f\n", arg_11);
return;
}
METAC_GSYM_LINK(test_function2_with_args);
METAC_GSYM_LINK(test_function_with_base_args);

void test_function_with_base_args_ptr(char * arg_00,
unsigned char *arg_01,
short *arg_02,
unsigned short *arg_03,
int *arg_04,
unsigned int *arg_05,
long *arg_06,
unsigned long *arg_07,
long long *arg_08,
unsigned long long *arg_09,
bool *arg_10,
float *arg_11,
double *arg_12,
long double *arg_13,
float complex *arg_14,
double complex *arg_15,
long double complex *arg_16){
return;
}
METAC_GSYM_LINK(test_function_with_base_args_ptr);


METAC_START_TEST(sanity){
// this doesn't pass on Windows yet
// int x = 689;
// METAC_WRAP_FN_RES(int, test_function2_with_args, &x, 2);
char arg_00 = 0;
unsigned char arg_01 = 1;
short arg_02 = 2;
unsigned short arg_03 = 3;
int arg_04 = 4;
unsigned int arg_05 = 5;
long arg_06 = 6;
unsigned long arg_07 = 7;
long long arg_08 = 8;
unsigned long long arg_09 = 9;
bool arg_10 = true;
float arg_11 = 11.0;
double arg_12 = 12.0;
long double arg_13 = 13.0;
float complex arg_14 = 14.0 - 14.0*I;
double complex arg_15 = 15.0 - 15.0*I;
long double complex arg_16 = 16.0 + 116.0*I;

METAC_WRAP_FN_NORES(test_function_with_base_args,
arg_00, arg_01, arg_02, arg_03, arg_04,
arg_05, arg_06, arg_07, arg_08, arg_09,
arg_10, arg_11, arg_12, arg_13, arg_14,
arg_15, arg_16);

METAC_WRAP_FN_NORES(test_function_with_base_args_ptr,
&arg_00, &arg_01, &arg_02, &arg_03, &arg_04,
&arg_05, &arg_06, &arg_07, &arg_08, &arg_09,
&arg_10, &arg_11, &arg_12, &arg_13, &arg_14,
&arg_15, &arg_16);


}END_TEST
32 changes: 24 additions & 8 deletions pkg/metadb/metadb_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,13 @@ var baseTypeSynonims = map[string]string{
"long long unsigned": "unsigned long long int",
"long long unsigned int": "unsigned long long int",

/* clang has only complex. with different size */
"complex float": "complex",
"float complex": "complex",
"complex double": "complex",
"double complex": "complex",
"long double complex": "complex",
"complex long double": "complex",
"long complex double": "complex",
"complex float": "complex float",
"float complex": "complex float",
"complex double": "complex double",
"double complex": "complex double",
"long double complex": "long complex double",
"complex long double": "long complex double",
"long complex double": "long complex double",
}

func deSynonim(in string) string {
Expand Down Expand Up @@ -104,6 +103,23 @@ func (baseType *BaseType) fromEntry(entry *dwarfy.Entry) error {
}
// correct name of type to avoid synonims. e.g. if long use long int (or vice-versa)
dsname := deSynonim(*baseType.Name)
if dsname == "complex" {
if baseType.ByteSize == nil {
return fmt.Errorf("the ByteSize isn't set for complex")
}
var bsz = *baseType.ByteSize
/* clang has only complex. with different size */
switch bsz {
case 8:
dsname = "complex float"
case 16:
dsname = "complex double"
case 32:
dsname = "long complex double"
default:
return fmt.Errorf("the ByteSize %d isn't supported", bsz)
}
}
baseType.Name = &dsname

encoding, ok := entry.Val("Encoding").(int64)
Expand Down
6 changes: 6 additions & 0 deletions src/entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ static int _entry_check_base_type(metac_entry_t *p_entry, metac_name_t expected_

assert(p_entry->name != NULL);

// clang doesn't have long complex double -> downgrade to "complex double"
if (strcmp(expected_name, "long complex double") == 0 &&
sizeof(long complex double) == sizeof(complex double)) {
expected_name = "complex double";
}

if (strcmp(p_entry->name, expected_name) == 0 && (
p_entry->base_type_info.encoding == METAC_ENC_undefined ||
p_entry->base_type_info.encoding == expected_encoding) && (
Expand Down
39 changes: 27 additions & 12 deletions src/value_base_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ metac_flag_t metac_value_is_ldouble(metac_value_t * p_val) {
return metac_value_check_base_type(p_val, "long double", METAC_ENC_float, sizeof(long double)) == 0;
}
metac_flag_t metac_value_is_float_complex(metac_value_t * p_val) {
return metac_value_check_base_type(p_val, "complex", METAC_ENC_complex_float, sizeof(float complex)) == 0;
return metac_value_check_base_type(p_val, "complex float", METAC_ENC_complex_float, sizeof(float complex)) == 0;
}
metac_flag_t metac_value_is_double_complex(metac_value_t * p_val) {
return metac_value_check_base_type(p_val, "complex", METAC_ENC_complex_float, sizeof(double complex)) == 0;
return metac_value_check_base_type(p_val, "complex double", METAC_ENC_complex_float, sizeof(double complex)) == 0;
}
metac_flag_t metac_value_is_ldouble_complex(metac_value_t * p_val) {
return metac_value_check_base_type(p_val, "complex", METAC_ENC_complex_float, sizeof(long double complex)) == 0;
return metac_value_check_base_type(p_val, "long complex double", METAC_ENC_complex_float, sizeof(long double complex)) == 0;
}
/**/
int metac_value_char(metac_value_t * p_val, char *p_var) {
Expand Down Expand Up @@ -105,13 +105,13 @@ int metac_value_ldouble(metac_value_t * p_val, long double *p_var) {
return metac_value_base_type(p_val, "long double", METAC_ENC_float, (void*)p_var, sizeof(long double));
}
int metac_value_float_complex(metac_value_t * p_val, float complex *p_var) {
return metac_value_base_type(p_val, "complex", METAC_ENC_complex_float, (void*)p_var, sizeof(float complex));
return metac_value_base_type(p_val, "complex float", METAC_ENC_complex_float, (void*)p_var, sizeof(float complex));
}
int metac_value_double_complex(metac_value_t * p_val, double complex *p_var) {
return metac_value_base_type(p_val, "complex", METAC_ENC_complex_float, (void*)p_var, sizeof(double complex));
return metac_value_base_type(p_val, "complex double", METAC_ENC_complex_float, (void*)p_var, sizeof(double complex));
}
int metac_value_ldouble_complex(metac_value_t * p_val, long double complex *p_var) {
return metac_value_base_type(p_val, "complex", METAC_ENC_complex_float, (void*)p_var, sizeof(long double complex));
return metac_value_base_type(p_val, "long complex double", METAC_ENC_complex_float, (void*)p_var, sizeof(long double complex));
}
/* */
int metac_value_set_char(metac_value_t * p_val, char var) {
Expand Down Expand Up @@ -157,13 +157,13 @@ int metac_value_set_ldouble(metac_value_t * p_val, long double var) {
return metac_value_set_base_type(p_val, "long double", METAC_ENC_float, &var, sizeof(long double));
}
int metac_value_set_float_complex(metac_value_t * p_val, float complex var) {
return metac_value_set_base_type(p_val, "complex", METAC_ENC_complex_float, &var, sizeof(float complex));
return metac_value_set_base_type(p_val, "complex float", METAC_ENC_complex_float, &var, sizeof(float complex));
}
int metac_value_set_double_complex(metac_value_t * p_val, double complex var) {
return metac_value_set_base_type(p_val, "complex", METAC_ENC_complex_float, &var, sizeof(double complex));
return metac_value_set_base_type(p_val, "complex double", METAC_ENC_complex_float, &var, sizeof(double complex));
}
int metac_value_set_ldouble_complex(metac_value_t * p_val, long double complex var) {
return metac_value_set_base_type(p_val, "complex", METAC_ENC_complex_float, &var, sizeof(long double complex));
return metac_value_set_base_type(p_val, "long complex double", METAC_ENC_complex_float, &var, sizeof(long double complex));
}

int metac_value_num(metac_value_t * p_val, metac_num_t * p_var) {
Expand Down Expand Up @@ -324,10 +324,25 @@ char *metac_value_base_type_string(metac_value_t * p_val) {
_string_(float, float, "%f", v);
_string_(double, double, "%lf", v);
_string_(long double, ldouble, "%Lf", v);
_string_(float complex, float_complex, "%lf + I * %lf", creal(v), cimag(v));
_string_(double complex, double_complex, "%lf + I * %lf", creal(v), cimag(v));
_string_(long double complex, ldouble_complex, "%lf + I * %lf", creal(v), cimag(v));
#undef _string_
#define _string_(_type_, _pseudoname_) \
do { \
if ( metac_value_is_##_pseudoname_(p_val) != 0) { \
_type_ v; \
if (metac_value_##_pseudoname_(p_val, &v) != 0) { \
return NULL; \
} \
if (cimag(v) < 0.0) { \
return dsprintf( "%lf - I * %lf", creal(v), -1.0 * cimag(v)); \
} \
return dsprintf( "%lf + I * %lf", creal(v), cimag(v)); \
} \
} while(0)
_string_(float complex, float_complex);
_string_(double complex, double_complex);
_string_(long double complex, ldouble_complex);
#undef _string_


return NULL;
}
2 changes: 1 addition & 1 deletion src/value_string_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ void test7_sanity_with_handler(metac_tag_map_t *p_tag_map) {
test7.content_len = 1;
test7.p_content = &t7_contnt1_1;

expected_s = "{.data = 888, .content_type = 1, .content_len = 1, .p_content = (struct t7_cntnt1 []){{.c = 19.330000 + I * -0.400000,},},}";
expected_s = "{.data = 888, .content_type = 1, .content_len = 1, .p_content = (struct t7_cntnt1 []){{.c = 19.330000 - I * 0.400000,},},}";
s = metac_value_string_ex(p_val, METAC_WMODE_deep, p_tag_map);
fail_unless(s != NULL, "got NULL");
fail_unless(strcmp(s, expected_s) == 0, "expected %s, got %s", expected_s, s);
Expand Down

0 comments on commit 44cd76f

Please sign in to comment.