Variable name, function arguments at runtime in C

Can I find out the parameters of functions and types of variable names at run time in a C program? For example, if I have a function:

int abc(int x, float y , somestruct z ){
    char a;
    int b ;
}

Can I find out inside this function abc()what are the names of the arguments and variables, i.e. In this case, it x, y, z, a, band they are of the type int, float, somestruct, char, int.

Tell me if there is another function:

float some_func(specialstruct my_struct, int index){

} 

I need to know that the argument names my_struct, indexand types specialstruct, int.

Do I need this information at runtime?

I have access to the base pointer and return address, can I get the necessary information using the pointer above.

, dladdr().

, GDB , ?

+4
4

C. - . , , . IMO - C-.

+1

C. , , . , .

+1

, dlsym dladdr, . , , C, , . , , , , .

backtrace . GNU , ( ). ( ), backtrace_symbols .

__LINE__ __FILE__ , , .

. C , . , , .

0

, C ++.

, Reflection C/++ .

dwarfdump , . DWARF , , .. , libdwarfdump .

:

typedef struct somestruct 
{
   int i;
   int j;
} somestruct ;

int abc(int x, float y , struct somestruct z ){
    char a;
    int b ;
}


int main(int argc, char* argv[])
{

   struct somestruct z;
   abc(1,1.0f,z);
   return 0;
}

dwarfdump

< 1><0x00000055>    DW_TAG_subprogram
                      DW_AT_external              yes(1)
                      DW_AT_name                  "abc"
                      DW_AT_decl_file             0x00000001 /tmp/dwarf.c
                      DW_AT_decl_line             0x00000009
                      DW_AT_prototyped            yes(1)
                      DW_AT_type                  <0x0000004e>
                      DW_AT_low_pc                0x004004ed
                      DW_AT_high_pc               <offset-from-lowpc>18
                      DW_AT_frame_base            len 0x0001: 9c: DW_OP_call_frame_cfa
                      DW_AT_GNU_all_call_sites    yes(1)
                      DW_AT_sibling               <0x000000ad>
< 2><0x00000076>      DW_TAG_formal_parameter
                        DW_AT_name                  "x"
                        DW_AT_decl_file             0x00000001 /tmp/dwarf.c
                        DW_AT_decl_line             0x00000009
                        DW_AT_type                  <0x0000004e>
                        DW_AT_location              len 0x0002: 916c: DW_OP_fbreg -20
< 2><0x00000082>      DW_TAG_formal_parameter
                        DW_AT_name                  "y"
                        DW_AT_decl_file             0x00000001 /tmp/dwarf.c
                        DW_AT_decl_line             0x00000009
                        DW_AT_type                  <0x000000ad>
                        DW_AT_location              len 0x0002: 9168: DW_OP_fbreg -24
< 2><0x0000008e>      DW_TAG_formal_parameter
                        DW_AT_name                  "z"
                        DW_AT_decl_file             0x00000001 /tmp/dwarf.c
                        DW_AT_decl_line             0x00000009
                        DW_AT_type                  <0x0000002d>
                        DW_AT_location              len 0x0002: 9160:        DW_OP_fbreg -32

, "abc" x, y z.

x 0x4e.

0x4e. 0x2d - somestruct, z.

< 1><0x0000002d>    DW_TAG_structure_type
                      DW_AT_name                  "somestruct"
                      DW_AT_byte_size             0x00000008
                      DW_AT_decl_file             0x00000001 /tmp/dwarf.c
                      DW_AT_decl_line             0x00000003
                      DW_AT_sibling               <0x0000004e>

< 1><0x0000004e>    DW_TAG_base_type
                      DW_AT_byte_size             0x00000004
                      DW_AT_encoding              DW_ATE_signed
                      DW_AT_name                  "int"

The combination of ptrace, ELF, DWARF, and the / proc file system allows gdb to read static and dynamic information for the process. Another process may use similar functionality to create Reflection functionality.

I used variations of this strategy to create custom debuggers and memory leak detectors. I have never seen this strategy used for business logic.

0
source

Source: https://habr.com/ru/post/1654668/


All Articles