Home > OS >  GDB, how do I find the location of variables
GDB, how do I find the location of variables

Time:02-19

I'm working with gdb to debug a c-executable.

I have this simple file

int main() {
    char *secret = "secret";

    char *buf = "hey";
    
    write(1, buf, 250);
}

Which I then run with gdb. I would then like to see the variable secret on the stack. This however I cannot figure out how to do, I tried:

$ info locals
No symbol table info available.

With no effect. So how does one find the location of a variable?

CodePudding user response:

In order for GDB to know the location of local variables, you must compile your source(s) with -g flag.

CodePudding user response:

You should get a result if you compile with the -g flag and set a breakpoint somewhere in main. If you haven't run the program yet, or let it complete, you won't see the local variables.

For example:

(gdb) b 6
Breakpoint 1 at 0x1004010a3: file sec.c, line 6.
(gdb) r
[.....]
Thread 1 "sec" hit Breakpoint 1, main () at sec.c:6
6           write(1, buf, 250);
(gdb) info locals
secret = 0x100403000 "secret"
buf = 0x100403007 "hey"

Alternatively, you can just print a variable to see the address:

(gdb) print secret
$1 = 0x100403000 "secret"

For something like an int you would have to use print &x to see the address as print x will just show the value.

  •  Tags:  
  • c gdb
  • Related