作为参考,我使用以下代码:
#include <stdio.h>
#include <string.h>
int main (void) {
char buf[100]; // ------> How do I find the address in gdb?
printf ("Buffer is at memory location: %08x\n", &buf);
strcpy (buf, "some random text");
printf ("Text is [%s]\n", buf);
return 0;
}
如何让gdb
向我显示buf
变量的地址?
答案 0 :(得分:34)
(gdb) p &a
如果您需要变量a
的地址。但是,变量可能会缓存在寄存器中,在这种情况下,GDB会告诉您address requested for identifier "a" which is in register $xxx
。
旁注:请勿使用gets
,请参阅here。
答案 1 :(得分:8)
当gdb设置为C语言模式(和Objective-C)时,&
运算符将起作用。
在任何语言模式下,您都可以使用
(gdb) info address buf
Symbol "buf" is static storage at address 0x903278.
(输出与你的代码并不完全一致。)我正在写这个答案,因为即使是那些寻找其他语言(包括我自己)答案的人也能找到这个问题。也可以通过set language c
始终切换到C模式,但此更改后符号名称可能会有所不同。
答案 2 :(得分:4)
如果您在gdb中输入以下内容,您将获得地址:
start
p &buf
如以下成绩单:
pax$ gdb ./qq.exe
GNU gdb 6.8.0.20080328-cvs (cygwin-special)
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-pc-cygwin"...
(gdb) start
Breakpoint 1 at 0x401144: file qq.c, line 2.
Starting program: /home/pax/qq.exe
[New thread 2912.0xf9c]
[New thread 2912.0x518]
main () at qq.c:2
2 int main (int argc, char **argv) {
(gdb) p &buf
$1 = (char (*)[100]) 0x22ccd0
(gdb)