找到变量Buf的确切地址

Nee*_*fra 24 stack-overflow buffer gdb

作为参考,我使用以下代码:

#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;
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能gdb告诉我buf变量的地址?

Nik*_*sov 36

(gdb) p &a如果你需要变量的地址a.但是,变量可能会缓存在寄存器中,在这种情况下,GDB会告诉您address requested for identifier "a" which is in register $xxx.

旁注:不要使用gets,请看这里.

  • 你的意思是你的处理器没有100字节的寄存器?伙计,你在运行什么硬件? (5认同)
  • 我不会将`buf`缓存在寄存器中.好吧,除非它是一个血腥的_big_注册:-) (3认同)

Vla*_*r F 9

&当gdb设置为C语言模式(和Objective-C)时,运算符将工作.

在任何语言模式下,您都可以使用

(gdb) info address buf
Symbol "buf" is static storage at address 0x903278.
Run Code Online (Sandbox Code Playgroud)

(输出与你的代码并不完全一致.)我正在写这个答案,因为即使是那些寻找其他语言(包括我自己)答案的人也能找到这个问题.也可以始终切换到C模式set language c,但在此更改后符号名称可能会有所不同.


pax*_*blo 5

如果在 gdb 中输入以下内容,您将获得地址:

start
p &buf
Run Code Online (Sandbox Code Playgroud)

如以下记录:

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)
Run Code Online (Sandbox Code Playgroud)

  • 这是 CygWin。只有 $DEITY 知道模拟 UNIX 的幕后情况:-) (5认同)
  • `当前上下文中没有符号“buf”。` (2认同)