4

How do I dump debug_loc section from an executable on Linux if default options (-g) are provided to GCC while compiling a C file? I use readelf linux utility.

GCC by default produces DWARF info in DWARF4 format, so if I pass -g-dwarf2 I can see .debug_loc section but how do inspect that section if info is generated with default options as I don't see the section in dump?

username_4567
  • 4,737
  • 12
  • 56
  • 92
  • Do you use a recent version of binutils? On Ubuntu 14.04 I can build executable with "gcc -g test.c -o test". Then do "readelf --debug-dump=info test" and see it's DWARF 4, then do readelf --debug-dump=decodedline test and get the contents of .debug_line section. – dbrank0 Feb 11 '15 at 07:09
  • ohh I did not know this option. Please explain more about this and put it in answer I will accept it thanks a lot!! – username_4567 Feb 11 '15 at 07:12

2 Answers2

0

Do you use a recent version of binutils? On Ubuntu 14.04 I can build executable with "gcc -g test.c -o test".

With "readelf --debug-dump=info test" I can confirm it's using DWARF 4.

After that, you have (at least) two ways to dump the contents of .debug_line section.

readelf --debug-dump=decodedline test

This will dump decoded line number information. You get line==address mappings directly.

There is also:

readelf --debug-dump=rawline test

This gets you raw debug_line contents.

dbrank0
  • 9,026
  • 2
  • 37
  • 55
  • 1
    Wait a sec. This is giving me contents of debug_line section and not debug_loc? what is the connection/difference? – username_4567 Feb 11 '15 at 10:05
  • Sorry. My mistake. I thought you were looking for debug_line section. Use --debug-dump=loc to get .debug_loc. – dbrank0 Feb 11 '15 at 10:33
  • FYI: for simple executable (one function) this generates no debug_loc section for some reason. For more complex one, it does. Funny. – dbrank0 Feb 11 '15 at 10:39
  • I don't see .debug_loc in dwarf 4 (I can find it in dwarf 2, same code). Any reason? – xiaogw Mar 18 '21 at 22:24
0

Usually, .debug_loc is generated for optimized code. Try optimizing your code using compiler flags.

ultimate cause
  • 2,264
  • 4
  • 27
  • 44