首页 > 代码库 > gdbserver 远程调试问题:设置文件和so搜索路径

gdbserver 远程调试问题:设置文件和so搜索路径

编写一个必然crash 的程序

#include <stdio.h>

void crash(){
 
    char *a=0;
    *a=0;
}
int main()
 {
        printf("hello world\n");
        crash();
        printf("after crash\n");
 }
执行gdb 远程调试

gdbserver :1234 a.out 


运行 gdb , 运行命令  target remote :1234

必须用 file 指定可执行文件,才能看见调用函数

(gdb) c
Continuing.

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400534 in ?? ()
(gdb) bt
#0  0x0000000000400534 in ?? ()
#1  0x00007fffffffe4c0 in ?? ()
#2  0x000000000040054c in ?? ()
#3  0x0000000000000000 in ?? ()
(gdb) file /home/payne/hello/a.out
A program is being debugged already.
Are you sure you want to change the file? (y or n) y
Reading symbols from /home/payne/hello/a.out...done.
(gdb) bt
#0  0x0000000000400534 in fun1 () at hello.cpp:6
#1  0x000000000040054c in main () at hello.cpp:12

不用 -g 编译也能看见函数,但不能定位到行

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400534 in crash() ()
(gdb) bt
#0  0x0000000000400534 in crash() ()
#1  0x000000000040054c in main ()

把 crash 放到 so 中会如何?

生成libcrash.so 重新编译 hello.cpp

g++ -o libcrash.so -shared crash.cpp 

export LD_LIBRARY_PATH=`pwd`

g++  hello.cpp  -lcrash


gdb 不能定位到 so 中的函数

rogram received signal SIGSEGV, Segmentation fault.
0x00007ffff7ff65bc in ?? ()
(gdb) bt
#0  0x00007ffff7ff65bc in ?? ()
#1  0x00007fffffffe470 in ?? ()
#2  0x0000000000400657 in ?? ()
#3  0x0000000000000000 in ?? ()
(gdb) file /home/payne/hello/a.out
A program is being debugged already.
Are you sure you want to change the file? (y or n) 
Please answer y or n.
A program is being debugged already.
Are you sure you want to change the file? (y or n) y
Reading symbols from /home/payne/hello/a.out...(no debugging symbols found)...done.
(gdb) bty
Undefined command: "bty".  Try "help".
(gdb) bt
#0  0x00007ffff7ff65bc in ?? ()
#1  0x00007fffffffe470 in ?? ()
#2  0x0000000000400657 in main ()

用 set solib-search-path 指定路径, 可以得到 so 中的位置

(gdb) set solib-search-path /home/payne/hello
Reading symbols from /home/payne/hello/libcrash.so...(no debugging symbols found)...done.
Loaded symbols for /home/payne/hello/libcrash.so
Reading symbols from /lib/x86_64-linux-gnu/libc.so.6...Reading symbols from /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.15.so...done.
done.
Loaded symbols for /lib/x86_64-linux-gnu/libc.so.6
Reading symbols from /lib64/ld-linux-x86-64.so.2...Reading symbols from /usr/lib/debug/lib/x86_64-linux-gnu/ld-2.15.so...done.
done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
(gdb) bt
#0  0x00007ffff7ff65bc in crash() () from /home/payne/hello/libcrash.so
#1  0x0000000000400657 in main ()