【发布时间】:2018-03-06 03:00:17
【问题描述】:
我知道我可以使用核心转储文件找出程序出错的地方。但是,有一些错误,即使你用核心文件调试它,你仍然不知道它为什么会出错。所以我想传达的是gdb和core文件可以帮助你调试的bug的范围是有限的。那有多大限制?
例如,我写如下代码:(libfoo.c)
#include <stdio.h>
#include <stdlib.h>
void foo(void);
int main()
{
puts("This is a mis-compiled runnable shared library");
return 0;
}
void foo()
{
puts("This is the shared function");
}
以下是makefile:(Makefile)
.PHONY : all clean
all : libfoo.c
gcc -g -Wall -shared -fPIC -Wl,-soname,$(basename $^).so.1 -o $(basename $^).so.1.0.0 $^; \
#the correct compiling command should be :
#gcc -g -Wall -shared -fPIC -pie -Wl,--export-dynamic,-soname,$(basename $^).so.1 -o $(basename $^).so.1.0.0 $^;
sudo ldconfig $(CURDIR); #this will set up soname link \
ln -s $(basename $^).so.1.0.0 $(basename $^).so #this will set up linker name link;
clean :
-rm libfoo.s*; sudo ldconfig;#roll back
当我运行它./libfoo.so 时,我遇到了分段错误,这是因为我以错误的方式编译了可运行共享库。但我想确切地知道是什么导致了分段错误。所以我使用了gdb libfoo.so.1.0.0 corefile,然后是bt,得到了以下结果:
[xhan@localhost Desktop]$ gdb ./libfoo.so core.8326
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-100.el7
Copyright (C) 2013 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 "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/xiaohan/Desktop/libfoo.so.1.0.0...done.
warning: core file may not match specified executable file.
[New LWP 8326]
Core was generated by `./libfoo.so'.
Program terminated with signal 11, Segmentation fault.
#0 0x0000000000000001 in ?? ()
(gdb) bt
#0 0x0000000000000001 in ?? ()
#1 0x00007ffd29cd13b4 in ?? ()
#2 0x0000000000000000 in ?? ()
(gdb) quit
但我仍然不知道是什么导致了分段错误。调试核心文件不能给我任何线索,我的分段错误的原因是我使用了错误的编译命令。
谁能帮我调试一下?或者谁能告诉我即使使用 gdb 和核心文件也无法调试的错误范围?只回答一个问题的答案也将被接受。
谢谢!
我持有的重要假设:
- 有些人可能会问我为什么要使共享库可运行。我这样做是因为我想编译一个类似于
/lib64/ld-2.17.so的共享库。 - 当然,您不能依赖 gdb 告诉您造成每个错误的原因。例如,如果你只是简单地
chmod +x nonexecutable并运行它,然后得到一个错误(通常这不会转储核心文件),并尝试使用 gdb 调试它,这有点“疯狂”。但是,一旦可以在运行时加载“可执行文件”并转储核心文件,您就可以使用 gdb 对其进行调试,此外,还可以找到有关程序出错原因的线索。但是,在我的问题./libfoo.so中,我完全迷失了。
【问题讨论】:
标签: gdb shared-libraries coredump