linux 软件调试技巧

1 objcopy + gdb的使用

1.1 创建一个debug.c文件,内容如下:

#include <stdio.h>

int main (void)
{
        printf ("Hello world.\n");
        return 0;
}

1.1.1 编译release版本,该版本不支持gdb调试

gcc debug.c -o debug_release

1.1.2 编译debug版本

gcc -g debug.c -o debug_debug

$ ll
total 36
drwxr-xr-x 2 guang guang  4096 616 15:14 ./
drwxr-xr-x 6 guang guang  4096 616 15:10 ../
-rw-r--r-- 1 guang guang    79 616 15:13 debug.c
-rwxr-xr-x 1 guang guang 10744 616 15:14 debug_debug*
-rwxr-xr-x 1 guang guang  8296 616 15:14 debug_release*

1.1.3 使用objcopy从debug_debug里面提取debug信息

$ objcopy --only-keep-debug debug_debug debug.dbg
$ ls
debug.c  debug.dbg  debug_debug  debug_release

1.1.4 使用如下方法让release版本的elf文件支持gdb调试

$ gdb -q --symbol=debug.dbg --exec=debug_release
Reading symbols from debug.dbg...done.
(gdb) r
Starting program: /home/guang/Develop/trainning/debug/debug_release 
warning: Probes-based dynamic linker interface failed.
Reverting to original interface.
Hello world.
[Inferior 1 (process 3603) exited normally]
(gdb)  

1.1.5 也可以将debug.dbg合并到debug_release文件中

guang@guang-ubuntu:~/Develop/trainning/debug$ gdb debug_release 
GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git
Copyright (C) 2018 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-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 debug_release...Reading symbols from /home/guang/Develop/trainning/debug/debug.dbg...done.
done.
(gdb) r
Starting program: /home/guang/Develop/trainning/debug/debug_release 
Hello world.
[Inferior 1 (process 3679) exited normally]
(gdb) 

猜你喜欢

转载自blog.csdn.net/skyleemon/article/details/80713230