How can I tell if a library was compiled with -g?

CDebuggingGdbDebug Symbols

C Problem Overview


I have some compiled libraries on x86 Linux and I want to quickly determine whether they were compiled with debugging symbols.

C Solutions


Solution 1 - C

If you're running on Linux, use objdump --debugging. There should be an entry for each object file in the library. For object files without debugging symbols, you'll see something like:

objdump --debugging libvoidincr.a
In archive libvoidincr.a:

voidincr.o:     file format elf64-x86-64

If there are debugging symbols, the output will be much more verbose.

Solution 2 - C

The suggested command

objdump --debugging libinspected.a
objdump --debugging libinspected.so

gives me always the same result at least on Ubuntu/Linaro 4.5.2:

libinspected.a:     file format elf64-x86-64
libinspected.so:     file format elf64-x86-64

no matter whether the archive/shared library was built with or without -g option

What really helped me to determine whether -g was used is readelf tool:

readelf --debug-dump=decodedline libinspected.so

or

readelf --debug-dump=line libinspected.so

This will print out set of lines consisting of source filename, line number and address if such debug info is included into library, otherwise it'll print nothing.

You may pass whatever value you'll find necessary for --debug-dump option instead of decodedline.

Solution 3 - C

What helped is:

gdb mylib.so

It prints when debug symbols are not found:

Reading symbols from mylib.so...(no debugging symbols found)...done.

Or when found:

Reading symbols from mylib.so...done.

None of earlier answers were giving meaningful results for me: libs without debug symbols were giving lots of output, etc.

Solution 4 - C

nm -a <lib> will print all symbols from library, including debug ones.

So you can compare the outputs of nm <lib> and nm -a <lib> - if they differ, your lib contains some debug symbols.

Solution 5 - C

On OSX you can use dsymutil -s and dwarfdump.

Using dsymutil -s <lib_file> | more you will see source file paths in files that have debug symbols, but only the function names otherwise.

Solution 6 - C

You can use objdump for this.

EDIT: From the man-page:

-W
--dwarf
Displays  the  contents of the DWARF debug sections in the file, if
any are present.

Solution 7 - C

Answers suggesting the use of objdump --debugging or readelf --debug-dump=... don't work in the case that debug information is stored in a file separate from the binary, i.e. the binary contains a debug link section. Perhaps one could call that a bug in readelf.

The following code should handle this correctly:

# Test whether debug information is available for a given binary
has_debug_info() {
  readelf -S "$1" | grep -q " \(.debug_info\)\|\(.gnu_debuglink\) "
}

See [Separate Debug Files][1] in the GDB manual for more information.

[1]: https://sourceware.org/gdb/onlinedocs/gdb/Separate-Debug-Files.html "Separate Debug Files"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDan HookView Question on Stackoverflow
Solution 1 - CMatt McClellanView Answer on Stackoverflow
Solution 2 - CAlex InTechnoView Answer on Stackoverflow
Solution 3 - CVelkanView Answer on Stackoverflow
Solution 4 - CqrdlView Answer on Stackoverflow
Solution 5 - CglennrView Answer on Stackoverflow
Solution 6 - CswegiView Answer on Stackoverflow
Solution 7 - CackView Answer on Stackoverflow