gdb: how does it know the variable type and size?

2019-07-27 13:31发布

问题:

I'm trying to figure this out as I'm trying to do the same thing (hopefully) with a home grown script:

Example C code:

typedef struct _B
{
    A aa;
    double b;
    char c[LEN];
    int d;
    char *a_ptr[10];
 } B;

 B this_b;

If I compile this with gcc -g and gdb a.out afterwards, gdb knows exactly what and where a_ptr is:

(gdb) p &(this_b.a_ptr)
$1 = (char *(*)[10]) 0x804a084

how does it do that? And can I do the same thing (knowing it's address and type) through other utilities?

回答1:

When you build with the -g flag, GCC (and most other compilers) stores additional "debugging info" in your binary (a.out).

You can examine that info with tools other than GDB. For example, readelf -w a.out (assuming you are on Linux or another ELF platform).

You can also compare the size of a.out when built with and without -g. It is not uncommon for the debug binary to be 5 to 10 times larger.



回答2:

This info is known on compile time. Gcc gathers it and stores it to be used later by different tools (gdb in this case).



标签: gdb binutils