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

8

126

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

Gery answered 4/1, 2010 at 13:52 Comment(0)
M
104

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.

Malamute answered 4/1, 2010 at 14:2 Comment(7)
There are also obdjump -W lib and readelf -w lib. The latter one is more configurable - see readelf(1) manpage.We
For any binary, (including those compiled with -g) objdump gives me the response of "no recognized debugging information" unless I compile it with -gstabs. This appears to be a recognized bug.Gery
Dan, on which platform did you try this?Decumbent
Employed Russian: from man objdump(1), the --debugging flag "attempts to parse STABS and IEEE debugging format information stored in the file and print it out using a C like syntax. If neither of these formats are found this option falls back on the -W option to print any DWARF information in the file."Malamute
objdump -g gives me nothing for a simple test.o compiled both with and without g, making it effectively useless. Ubuntu 12.04, gcc 4.6.3, GNU objdump 2.22. nm -a seems to be more useful.Belfry
objdump -W does the jobDrinking
When testing on linux shared library (*.so), it produces lot's of output on without debugging symbols. nm -a looks more reliable.Whitehurst
T
108

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.

Tartarous answered 9/10, 2012 at 14:4 Comment(1)
works perfectly. I tried this command on my executable with first CMAKE_BUILD_TYPE RELEASE and the command returned empty. Then I tried with CMAKE_BUILD_TYPE DEBUG and then there were quite a lot of output.Dogs
M
104

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.

Malamute answered 4/1, 2010 at 14:2 Comment(7)
There are also obdjump -W lib and readelf -w lib. The latter one is more configurable - see readelf(1) manpage.We
For any binary, (including those compiled with -g) objdump gives me the response of "no recognized debugging information" unless I compile it with -gstabs. This appears to be a recognized bug.Gery
Dan, on which platform did you try this?Decumbent
Employed Russian: from man objdump(1), the --debugging flag "attempts to parse STABS and IEEE debugging format information stored in the file and print it out using a C like syntax. If neither of these formats are found this option falls back on the -W option to print any DWARF information in the file."Malamute
objdump -g gives me nothing for a simple test.o compiled both with and without g, making it effectively useless. Ubuntu 12.04, gcc 4.6.3, GNU objdump 2.22. nm -a seems to be more useful.Belfry
objdump -W does the jobDrinking
When testing on linux shared library (*.so), it produces lot's of output on without debugging symbols. nm -a looks more reliable.Whitehurst
S
53

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.

Swigart answered 6/6, 2016 at 10:2 Comment(2)
Thx! This worked for me, using clang compiler in Android with cmake :)Belay
super great for a fast checking ! also works on *.o object files.Casas
S
35

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.

Salop answered 4/1, 2010 at 13:56 Comment(4)
@Employed Russian Can you please elaborate on this? Why do you think it is a wrong tool? It does the job, and does it on Linux as well.Salop
Even for Embedded linux based on kernel 2.6.35, xxx-objdump, xxx-nm works fine.Bortman
nm -a has alias nm --debug-syms which is self-explanatory :-).Whitehurst
Simply type diff <(nm <lib>) <(nm -a <lib>) to get an easy diffAshkhabad
I
20

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.

Intumescence answered 28/8, 2013 at 20:41 Comment(1)
Can you provide an elaboration on what to look for in the output of, for example, dsymutil -s? Does the existence of output mean that it was built with debug symbols, or should it be grepped?Hippel
D
12

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.
Decumbent answered 4/1, 2010 at 13:55 Comment(1)
Doesn't work with macOS's LLDB objdump: error: unknown argument '--dwarf'Benz
B
8

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 in the GDB manual for more information.

Blitz answered 14/9, 2016 at 0:6 Comment(1)
So, readelf -S example | grep debug would be better. To find link file using readelf --debug-dump=links example | grep link (GNU readelf version 2.31.1-13.fc29)Furey
L
0

https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/developer_guide/debugging

The command readelf -wi file is a good verification of debuginfo, compiled within your program.

Lialiabilities answered 17/8, 2022 at 6:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.