8

又一debug装逼技能:record, replay

 3 years ago
source link: https://zhuanlan.zhihu.com/p/364075104
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

又一debug装逼技能:record, replay

一个人NB的不是标签

上次写了文章讲述如何利用Python提升gdb的debug技能——你还在用GDB调试程序吗?(点赞770,收藏1156。我写的文章大部分收藏多余点赞,看来喜欢先收藏,然后学习。)在评论里说要讲一下gdb的逆向调试。现在就来填一下这个坑。

这里的逆向调试,不是逆向工程的逆向,而是在debug的时候,需要退回到过去,比如还想再看看这个函数是如何计算的,但是又不想重新启动程序,或者重新复现。这里的退回到过去,就是逆向。大部分调试工具都支持逆向,比如Visual Studio可以直接拖拉鼠标从过去的某点重新运行,见下面的简短视频。

如何在Linux使用gdb实现相同的效果?(本文重点介绍一款大杀器)

gdb reverse debugging

gdb在7.0的时候(2009年)支持了逆向调试。将release的摘要复制如下,方便英文阅读能力强的读者。

reverse debugging (the ability to make the program being debugged step and continue in reverse).

  • reverse-continue ('rc') -- Continue program being debugged but run it in reverse
  • reverse-finish -- Execute backward until just before the selected stack frame is called
  • reverse-next ('rn') -- Step program backward, proceeding through subroutine calls.
  • reverse-nexti ('rni') -- Step backward one instruction, but proceed through called subroutines.
  • reverse-step ('rs') -- Step program backward until it reaches the beginning of a previous source line
  • reverse-stepi -- Step backward exactly one instruction
  • set exec-direction (forward/reverse) -- Set direction of execution.

可以看到,reverse debugging就是将程序回退,上面是支持的一些指令。

hello world的程序就不演示,大家可以自行尝试。需要注意的是,reverse的步骤

  1. 在一开始的地方设置断点
  2. 程序运行到断点,输入record录制操作。如果不录制,那么gdb就不能回退了。相当于按下存档键。
  3. 这时开始操作,然后使用相应的reverse debugging 的命令进行回退。
  4. 最后将录制停止record stop

下面就介绍本文要说的大杀器——

rr: Record, Replay

gdb提供的reverse debugging比较原始,需要长时间使用才能熟练。debug是一件耗时耗力的事情,总有大牛为我们开路。rr: record, Replay就是一大杀器(装逼神器)。借助rr,我们可以一次录制,反复replay,就像在看片,可以随时往回退,而且状态都不变化。

github的介绍是

rr is a lightweight tool for recording, replaying and debugging execution of applications (trees of processes and threads). Debugging extends gdb with very efficient reverse-execution, which in combination with standard gdb/x86 features like hardware data watchpoints, makes debugging much more fun. More information about the project, including instructions on how to install, run, and build rr, is at https://rr-project.org. The best technical overview is currently the paper Engineering Record And Replay For Deployability: Extended Technical Report.

大致翻译为,rr是一个轻量级的工具,让你可以录制,重放,调试程序运行。高效的拓展了gdb的逆向调试。使得调试代码更有乐趣。

Youtube上已经有demo视频,我就不再献丑了。为了方便不能观看的同学,现在将它上传到这里。从这个视频中,我们就可以看到rr使用的简单和强大。

rr注意事项

rr是拓展了gdb,所以gdb支持的语言,它基本支持。安装方法参见上面给的rr的链接。需要注意的是,rr并不是什么系统以及任何程序都能支持的。以下是一些注意的事项(方便初学者,大牛自然会自己解决)。

# Please follow the offical instruction: https://github.com/rr-debugger/rr/wiki/Building-And-Installing
# sudo dnf install \
#   ccache cmake make gcc gcc-c++ gdb libgcc libgcc.i686 \
#   glibc-devel glibc-devel.i686 libstdc++-devel libstdc++-devel.i686 \
#   python3-pexpect man-pages ninja-build capnproto capnproto-libs capnproto-devel

# following are the instruction on my CentOS
yum install -y https://download-ib01.fedoraproject.org/pub/epel/7/x86_64/Packages/p/python36-ptyprocess-0.5.1-7.el7.noarch.rpm

yum install -y https://download-ib01.fedoraproject.org/pub/epel/7/x86_64/Packages/p/python36-pexpect-4.8.0-1.el7.noarch.rpm

# https://capnproto.org/install.html
capn_dir=/tmp/capn
mkdir -p $capn_dir
cd $capn_dir
curl -O https://capnproto.org/capnproto-c++-0.8.0.tar.gz
tar zxf capnproto-c++-0.8.0.tar.gz
cd capnproto-c++-0.8.0
./configure
make -j6 check
make install
rm -rf $capn_dir
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/:/usr/share/pkgconfig/

rr_dir=/tmp/rr
mkdir -p $rr_dir
cd $rr_dir
git clone https://github.com/rr-debugger/rr.git
mkdir obj && cd obj
cmake ../rr
make -j8
make install
rm -rf $rr_dir

# echo "kernel.perf_event_paranoid=1" >> /etc/sysctl.conf
# sysctl -p

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK