我想用gdb调试以下程序:
#include<stdio.h>
#include<math.h>
double take_log(double a)
{
return log(a);
}
int main()
{
printf("hello world\n");
printf("%lf\n", take_log(32));
return 0;
}
我使用-g编译并运行时没有问题:
$ cc test.c -g -o lm -lm
$ lm
hello world
3.465736
但是当我启动gdb进行调试时,我遇到了分段错误:
$ gdb lm
[gdb startup message here]
(gdb) start
Breakpoint 1 at 0x40065b: file test.c, line 11.
Starting program: [path to program]/lm
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7de711b in check_match.12080 () from /lib64/ld-linux-x86-64.so.2
奇怪的是,如果我将探查器选项-pg添加到编译器,那么gdb运行没有问题:
$ cc test.c -g -pg -o lm -lm
$ gdb lm
[gdb startup message]
(gdb) start
Breakpoint 1 at 0x4007f5: file test.c, line 11.
Starting program: [path to program]/lm
Executing new program: [path to program]/lm
main () at test.c:11
11 printf("hello world\n");
(gdb) next
hello world
12 printf("%lf\n", take_log(32));
(gdb) next
3.465736
13 return 0;
(gdb) next
14 }
(gdb)
更奇怪的是(或者由于同样的原因),如果我在编译期间省略-pg选项,还要注释掉行
// printf("hello world\n");
然后gdb再次运行没有问题。
这里发生了什么?
这里有一些可能有用的细节:
$ echo $LD_LIBRARY_PATH
$ echo $LIBRARY_PATH
$ ldd lm
linux-vdso.so.1 => (0x00007ffff7ffe000)
libm.so.6 => /lib64/libm.so.6 (0x00007ffff7d64000)
libc.so.6 => /lib64/libc.so.6 (0x00007ffff79eb000)
/lib64/ld-linux-x86-64.so.2 (0x0000555555554000)
$ cc -v
[...] gcc version 4.3.4 [gcc-4_3-branch revision 152973] (SUSE Linux)