Fix Debugging GDB Symbols

I was doing some C++ debugging on Ubuntu and noticed to find that non of the local symbols were being displayed in GDB. The only symbol available was std::__ioinit. After some digging I found the solution was to add -gdwarf-2 to the compiler command line:

g++ -g -gdwarf-2  

Turns out this is because GCC 4.8+ now generates DWARF4 debugging symbols whereas older version of GCC generated DWARF2 symbols. The problem was my older version of GDB did not know how to use the newer DWARF format.

As noted in GCC 4.8 Release Notes:

DWARF4 is now the default when generating DWARF debug information. When -g is used on a platform that uses DWARF debugging information, GCC will now default to -gdwarf-4 -fno-debug-types-section.
GDB 7.5, Valgrind 3.8.0 and elfutils 0.154 debug information consumers support DWARF4 by default. Before GCC 4.8 the default version used was DWARF2. To make GCC 4.8 generate an older DWARF version use -g together with -gdwarf-2 or -gdwarf-3. The default for Darwin and VxWorks is still -gdwarf-2 -gstrict-dwarf.

Paul Soucy

Read more posts by this author.