3

When trying to debug some simple code, I could not get GDB to identify a local variable from a memory address with info symbol.

The code was compiled using g++ -g3 -Wall -Wextra.

int main()
{
    int foo = 1234;
    return foo;
}
(gdb) p foo
$1 = 1234
(gdb) p &foo
$2 = (int *) 0x7fffffffd7c4
(gdb) info symbol 0x7fffffffd7c4
No symbol matches 0x7fffffffd7c4.
(gdb) info address foo
Symbol "foo" is a complex DWARF expression:
     0: DW_OP_fbreg -28
.

Why can't GDB identify the variable in this case? Does info symbol only work for global objects?

Brun
  • 65
  • 1
  • 6
  • I think this can be considered as gdb bug. There is already reported one: https://sourceware.org/bugzilla/show_bug.cgi?id=24901. – ks1322 Apr 29 '20 at 17:18
  • An alternative is `info locals`, which is the right way of inspecting local variables stored in the stack. https://stackoverflow.com/questions/6261392/printing-all-global-variables-local-variables – Brun Apr 29 '20 at 19:59

1 Answers1

1

Local variables are residing on the stack or maybe in registers. Accessing the stack has nothing to do with the symbol table of a program. As described in Examining the symbol table

The commands described in this chapter allow you to inquire about the symbols (names of variables, functions and types) defined in your program. This information is inherent in the text of your program and does not change as your program executes.

Local variables are never constant in position as the current position of the stack frame depends on the call depth and many other things. Local variables are simply not part of the programs symbol table so you can't examine them with the symbol command.

You can see what is in your symbol table by simply call nm from your shell, maybe in addition with c++filt to get readable names.

> nm|c++filt

Klaus
  • 24,205
  • 7
  • 58
  • 113
  • FYI, `nm|c++filt` is equivalent to `nm -C`. – Employed Russian Apr 29 '20 at 18:09
  • That explains it, thanks! Apparently `info locals` is the intended way of checking out local variables in the stack. https://stackoverflow.com/questions/6261392/printing-all-global-variables-local-variables – Brun Apr 29 '20 at 19:57
  • @EmployedRussian: Good hint! Did not know it before.Thanks for it! – Klaus Apr 30 '20 at 08:30
  • I'm having inconsistent gdb output executing 'info symbols' on a non-running .so file. I can take two symbols, both of type R_ARM_ABS32, yet one symbol can be resolved and unclobbered; The other: "No symbol matches" – JoeManiaci Aug 11 '21 at 16:16