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

我与影子孤独终老i 提交于 2019-12-11 12:43:52

问题


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).



来源:https://stackoverflow.com/questions/7496849/gdb-how-does-it-know-the-variable-type-and-size

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!