首页 > *nix技术, 跟踪调试 > 利用GDB7.0的反向调试(reverse debug)特性调试程序

利用GDB7.0的反向调试(reverse debug)特性调试程序

2013年1月11日 发表评论 阅读评论 1,079 次浏览

同事问到一个问题:假设a函数调用了b函数,并且当前执行点已经进入到b函数,如果想退到a函数来查看程序状态,是否有这样的调试工具?因为2010年在CU上写过一篇博客“利用GDB7.0的反向调试(reverse debug)特性调试程序”,因此我知道Linux下的GDB就可以,虽然这需要7.0以后的版本。当时回头看了一下那篇博客,现在把它修正一下并转载过来,为个人博客文章充充数。另外,ibm网站上有一篇不错的详细介绍文章:探索 Gdb7.0 的新特性反向调试 (reverse debug)。

GDB7.0 是在2009年10月6号份正式发布的,其提供了一个名为反向调试 (reverse debug:Reverse debugging, Process record and replay,http://sourceware.org/gdb/news/) 的新特性对于我们平常调试程序会有所帮助。

October 06, 2009: GDB 7.0 Released!
The latest version of GDB, version 7.0, is available for download.
Changes in this release include:
Support for native x86/x86_64 Darwin, x86_64 MinGW
Support for Lattice Mico32, x86/x86_64 DICOS, S+core 3 targets
gdbserver support for x86 Windows CE
Python scripting support
Reverse debugging, Process record and replay
Non-stop debugging
Multi-architecture debugging
Multi-inferior, multi-process debugging
See the NEWS file for a more complete and detailed list of what this release includes.

平常我们的调试总是下一步,下一步来进行,而“反向调试”,从字面上理解,应该就是上一步,上一步,事实上,GDB7.0提供的确实也就是这样的功能,这个特性决定了它能给我们带来什么样的好处,即方便调试,提高效率。当断点的位置没有设置正确,导致错过重要的代码路径时,无需重启调试会话,而只需简单的退回去即可,如本文最前面所说的场景。

废话少说,看个简单示例,

[root@www 3]# gdb -v
GNU gdb (GDB) Red Hat Enterprise Linux (7.1-29.el6)
Copyright (C) 2010 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/>.
[root@www 3]# ls
test.c
[root@www 3]# gcc test.c -g -O0
[root@www 3]# gdb -q a.out
Reading symbols from /home/work/3/a.out...done.
(gdb) b main
Breakpoint 1 at 0x400478: file test.c, line 5.
(gdb) r
Starting program: /home/work/3/a.out 

Breakpoint 1, main () at test.c:5
warning: Source file is more recent than executable.
5               int a = 100;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.7.el6.x86_64
(gdb) re
record            remove-inferior   restore           reverse-finish    reverse-search
refresh           require           return            reverse-next      reverse-step
remote            restart           reverse-continue  reverse-nexti     reverse-stepi
(gdb) record
(gdb) n
6               a ++;
(gdb)
7               a --;
(gdb) p a
$1 = 101
(gdb) rn
6               a ++;
(gdb) p a
$2 = 100
(gdb) n

No more reverse-execution history.
main () at test.c:7
7               a --;
(gdb) p a
$3 = 101
(gdb) n
8               a += 2;
(gdb) p a
$4 = 100
(gdb) n
9               a -= 3;
(gdb) p a
$5 = 102
(gdb) n
10              return 0;
(gdb) p a
$6 = 99
(gdb) rn
9               a -= 3;
(gdb) p a
$7 = 102
(gdb) n

No more reverse-execution history.
main () at test.c:10
10              return 0;
(gdb) rn
9               a -= 3;
(gdb) p a
$8 = 102
(gdb) rn
8               a += 2;
(gdb) p a
$9 = 100
(gdb)

果然够简单,就当作是一个引子吧,详细情况请参考前面提到的ibm网站文章。

转载请保留地址:http://lenky.info/archives/2013/01/11/2187http://lenky.info/?p=2187


备注:如无特殊说明,文章内容均出自Lenky个人的真实理解而并非存心妄自揣测来故意愚人耳目。由于个人水平有限,虽力求内容正确无误,但仍然难免出错,请勿见怪,如果可以则请留言告之,并欢迎来讨论。另外值得说明的是,Lenky的部分文章以及部分内容参考借鉴了网络上各位网友的热心分享,特别是一些带有完全参考的文章,其后附带的链接内容也许更直接、更丰富,而我只是做了一下归纳&转述,在此也一并表示感谢。关于本站的所有技术文章,欢迎转载,但请遵从CC创作共享协议,而一些私人性质较强的心情随笔,建议不要转载。

法律:根据最新颁布的《信息网络传播权保护条例》,如果您认为本文章的任何内容侵犯了您的权利,请以或书面等方式告知,本站将及时删除相关内容或链接。

  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.