32

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?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Neefra
  • 365
  • 1
  • 4
  • 5
  • Reverse question: [debugging - How to get the symbol name for a memory address in GDB? - Stack Overflow](https://stackoverflow.com/questions/762628/how-to-get-the-symbol-name-for-a-memory-address-in-gdb) (however does not work with local variables) ■ To do in Python API: [Get address of a global symbol from symbol name with GDB Python API - Stack Overflow](https://stackoverflow.com/questions/54070055/get-address-of-a-global-symbol-from-symbol-name-with-gdb-python-api?noredirect=1&lq=1) – user202729 Dec 19 '21 at 02:41

3 Answers3

44

(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.

Community
  • 1
  • 1
Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
8

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.

  • Apart from that there are other differences, read the documentation: https://sourceware.org/gdb/onlinedocs/gdb/Symbols.html – user202729 Dec 07 '21 at 01:48
5

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)
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 5
    It's CygWin. Only $DEITY knows what's going on under the covers to emulate UNIX :-) – paxdiablo Dec 16 '10 at 16:54
  • 2
    `No symbol "buf" in current context.` – Aaron Esau May 02 '17 at 04:33
  • Well, @Arin, I'd guess that you're doing something wrong since that's an *actual transcript* of it working. Granted it was answered some six years ago so my memory may be faulty but I'm unlikely to have been in the habit of just making stuff up :-) If it doesn't work for you, I'd suggest posting a question where you'll likely get a more comprehensive group of people looking at it (i.e., not just me). – paxdiablo May 02 '17 at 13:18