Find the exact address of variable Buf

那年仲夏 提交于 2020-01-11 21:24:58

问题


As reference, I'm using the following code:

#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;
}

How can I get gdb to show me the address of the buf variable?


回答1:


(gdb) p &a if you need the address of variable a. A variable might be cached in a register though, in which case GDB would tell you address requested for identifier "a" which is in register $xxx.

Sidenote: do not use gets, see here.




回答2:


The & operator will work when gdb is set to C language mode (and Objective-C).

In any language mode you can use

(gdb) info address buf
Symbol "buf" is static storage at address 0x903278.

(The output does not correspond exactly to your code.) I am writing this answer because this question is found even by people looking for the answer for other languages (including myself). One can also always switch to the C mode by set language c, but the symbol names may be different after this change.




回答3:


If you enter the following into gdb, you'll get the address:

start
p &buf

as in the following transcript:

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)


来源:https://stackoverflow.com/questions/4462915/find-the-exact-address-of-variable-buf

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