简体   繁体   English

如何判断一个库是否是用 -g 编译的?

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

I have some compiled libraries on x86 Linux and I want to quickly determine whether they were compiled with debugging symbols.我在 x86 Linux 上有一些编译的库,我想快速确定它们是否使用调试符号编译。

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: 至少在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 无论归档/共享库是使用或不使用-g选项构建的

What really helped me to determine whether -g was used is readelf tool: 真正帮助我确定是否使用-greadelf工具:

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 . 你可以传递你需要的任何值--debug-dump选项而不是decodedline

If you're running on Linux, use objdump --debugging . 如果您在Linux上运行,请使用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. 如果有调试符号,输出将更加冗长。

nm -a <lib> will print all symbols from library, including debug ones. nm -a <lib>将打印库中的所有符号,包括调试符号。

So you can compare the outputs of nm <lib> and nm -a <lib> - if they differ, your lib contains some debug symbols. 因此,您可以比较nm <lib>nm -a <lib> - 如果它们不同,则lib包含一些调试符号。

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. 以前的答案都没有为我提供有意义的结果:没有调试符号的libs提供了大量输出等。

On OSX you can use dsymutil -s and dwarfdump . 在OSX上,您可以使用dsymutil -sdwarfdump

Using dsymutil -s <lib_file> | more 使用dsymutil -s <lib_file> | more dsymutil -s <lib_file> | more you will see source file paths in files that have debug symbols, but only the function names otherwise. 您将在具有调试符号的文件中看到dsymutil -s <lib_file> | more源文件路径,否则只会看到函数名称。

You can use objdump for this. 你可以使用objdump

EDIT: From the man-page: 编辑:从手册页:

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

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, ie the binary contains a debug link section. 答案建议使用objdump --debuggingreadelf --debug-dump=...在调试信息存储在与二进制文件分开的文件中的情况下不起作用,即二进制文件包含调试链接部分。 Perhaps one could call that a bug in readelf . 也许有人可以称之为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. 有关详细信息,请参阅GDB手册中的单独调试文件

https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/developer_guide/debugging 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.命令 readelf -wi 文件是对调试信息的良好验证,在您的程序中编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何LD_PRELOAD自己的编译库? - How can I LD_PRELOAD my own compiled library? 如何在编译为WebAssembly的Rust库中使用C库? - How do I use a C library in a Rust library compiled to WebAssembly? 如何将已编译库的源路径链接到Eclipse中的其他位置? - How can I link the source path of a compiled library to a different location in Eclipse? 如何获取g_dbus_connection_signal_subscribe函数来告诉我有关预先存在的对象/接口的信息? - How can I get the g_dbus_connection_signal_subscribe function to tell me about pre-existing objects/interfaces? 如何告诉GCC为-l而不是系统使用自定义库? - How can I tell GCC to use custom library for -l instead of the system one? 我可以在G ++的C ++程序中使用CUDA吗? 还是只能使用GCC编译CUDA? - Can I use CUDA with C++ program by G++. Or CUDA can be compiled only with GCC? 如何将已编译的“.so”库与 Scons 链接? - How do I link a compiled “.so” library with Scons? 如何在Linux上的C库中使用编译的二进制文件? - How do I use a compiled binary for a C library on Linux? 如何判断可执行文件是否已针对本机进行编译? - How to tell whether an executable was compiled for the present machine? 我怎么知道一个函数是否进入内核 - How can I tell a function enters kernel or not
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM