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?
(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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With