先寫一個簡單的文件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.