在GDB中,我如何获得全局变量的地址

时间:2011-07-25 22:17:02

标签: debugging gdb

我想要检查一些全局变量。但“info var my_global”不显示地址。有没有办法获得全局变量的真实地址?

1 个答案:

答案 0 :(得分:2)

print &my_global应该可以正常工作。一个简单的例子:

#include <stdio.h>

int x = 12;

int main(int argc, char **argv)
{
  printf("%d\n", x);
  return 0;
}

然后建立&amp;调试:

$ make example
clang -g    example.c   -o example
$ gdb example
(gdb) break main
Breakpoint 1 at 0x100000f04: file example.c, line 8.
(gdb) run
Starting program: example 
Reading symbols for shared libraries +. done

Breakpoint 1, main () at example.c:8
8     printf("%d\n", x);
(gdb) print &x
$1 = (int *) 0x100001068
Current language:  auto; currently minimal