423

How can I print all global variables/local variables? Is that possible in gdb?

Cole Tobin
  • 9,206
  • 15
  • 49
  • 74
cpuer
  • 7,413
  • 14
  • 35
  • 39

3 Answers3

603

Type info variables to list "All global and static variable names" (huge list.

Type info locals to list "Local variables of current stack frame" (names and values), including static variables in that function.

Type info args to list "Arguments of the current stack frame" (names and values).

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • 3
    @KennyTM,the static variable names in output of `info variables` should be static variables within that compile unit,right? – cpuer Jun 07 '11 at 07:09
  • 4
    @cpuer: Not necessarily. It only displays the name in the symbol table. For instance, with gcc on Mac a static variable `y` is renamed to `y.1913` on compilation. – kennytm Jun 07 '11 at 07:14
  • 1
    @KennyTM ,isn't static variables inside function stored the same way as static variables outside function(in the symbol table)? – cpuer Jun 07 '11 at 07:20
  • 3
    @cpuer: They are stored the same way but the symbol names will be difference. Consider you have a static variable `y` in function `foo` and another `y` in `bar`. To distinguish them, a different name must be assigned to the two `y`'s. – kennytm Jun 07 '11 at 07:41
  • 1
    @KennyTM ,further more,is it possible to get where a variable is declared and defined respectively? – cpuer Jun 07 '11 at 07:53
  • 1
    @cpuer: `info var` does show where a variable is defined for those having debug symbols. But not for locals. – kennytm Jun 07 '11 at 08:07
  • 1
    @KennyTM,seems wrong,`Undefined info command: "var ". Try "help info".` after typing `info var`.. – cpuer Jun 07 '11 at 08:10
  • 1
    @cpuer: `info var` is the same as `info variables`. It works at least on gdb 6.3... – kennytm Jun 07 '11 at 08:28
  • Note: [there's a bug](https://sourceware.org/bugzilla/show_bug.cgi?id=26644) which in short is that `i varaibles` does not print ones defined as a macro. Seeing them currently requires a separate command `info macros`. – Hi-Angel Sep 22 '20 at 10:24
  • you can also print a specific variable, i.e you have a variable named modified to 'p modified' and will how you the value of the current, or 'p &modified' to see it's address – MissSergeivna Sep 30 '22 at 06:12
144

In case you want to see the local variables of a calling function use select-frame before info locals

E.g.:

(gdb) bt
#0  0xfec3c0b5 in _lwp_kill () from /lib/libc.so.1
#1  0xfec36f39 in thr_kill () from /lib/libc.so.1
#2  0xfebe3603 in raise () from /lib/libc.so.1
#3  0xfebc2961 in abort () from /lib/libc.so.1
#4  0xfebc2bef in _assert_c99 () from /lib/libc.so.1
#5  0x08053260 in main (argc=1, argv=0x8047958) at ber.c:480
(gdb) info locals
No symbol table info available.
(gdb) select-frame 5
(gdb) info locals
i = 28
(gdb) 
Samuel Åslund
  • 2,814
  • 2
  • 18
  • 23
33

In addition, since info locals does not display the arguments to the function you're in, use

(gdb) info args

For example:

int main(int argc, char *argv[]) {
    argc = 6*7;    //Break here.
    return 0;
}

argc and argv won't be shown by info locals. The message will be "No locals."

Reference: info locals command.

Evgeni Sergeev
  • 22,495
  • 17
  • 107
  • 124