先写一个简单的文件1.c:
$ cat 1.c
void foo(void)
{
}
然后编译两个库,一个不加 -g, 一个加:
gcc 1.c -c
gdb -shared -fpic -o lib1.so 1.o
cp 1.c 2.c
gcc -c -g 2.c
gcc -shared -fpic -o lib2.so 2.o
下面介绍几种判断的方法:
1. 使用 objdump
$ objdump --debugging lib2.so | grep debug
Contents of the .debug_aranges section:
Offset into .debug_info: 0x0
Contents of the .debug_info section:
Contents of the .debug_abbrev section:
Raw dump of debug contents of section .debug_line:
Contents of the .debug_str section:
可以看出,对于使用 -g的库, objdump --debugging会有很多 debug相关的信息输出。对于 没有加-g的库,则没有这样的输出.
也可用 readelf -w 或者 objdump -W
2.使用 gdb.
e$ gdb lib2.so
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from lib2.so...done.
注意有输出: Reading symbols from lib2.so...done.
对于 lib1.so,输出为 Reading symbols from lib1.so...(no debugging symbols found)...done.