程序运行时Trace: DynamoRIO

1. 程序运行时Trace,DynamoRIO

最近在做一个trace程序执行路径的项目,了解到DynamoRIO可以实现这个功能,在此记录学习以下。首先,DynamoRIO是什么:DynamoRIO是一个运行时代码控制系统(runtime code manipulation system),支持程序运行时对程序的任一部分进行修改。DynamoRIO exports an interface for building dynamic tools for a wide variety of uses: program analysis and understanding, profiling, instrumentation, optimization, translation, etc.。在此我们用来监控代码的执行路径,最终由此结果得出程序执行的CFG(控制流图,Control-Flow Graphic)。

Ubuntu 16.04 下,获取DynamoRIO方式。其中已经包含了DR的可执行文件。

wget https://github.com/DynamoRIO/dynamorio/releases/download/release_7_0_0_rc1/DynamoRIO-Linux-7.0.0-RC1.tar.gz
tar -xzvf DynamoRIO-Linux-7.0.0-RC1.tar.gz
mkdir dynamorio
mv DynamoRIO-Linux-7.0.0-RC1 dynamorio
rm DynamoRIO-Linux-7.0.0-RC1.tar.gz'

2. 使用

DR工具下载好后,我们首先编译其自带的smapleC语言源文件进行简单的使用尝试。

cd dynamorio/
mkdir build && cd build/
cmake -DDynamoRIO_DIR=../cmake ../samples

执行完以上命令后,可以看到在build文件目录下,生成了以下文件。到这一步还没有生成对应的so文件,还需要make 对应的链接脚本文件,例如make inscount。inscount 的作用是记录指令执行的总数(count of the total number of instructions executed)。

├── bbbuf.ldscript
├── bbcount.ldscript
├── bbsize.ldscript
├── bin
...
├── countcalls.ldscript
├── div.ldscript
├── empty.ldscript
├── inc2add.ldscript
├── inline.ldscript
├── inscount.ldscript
├── instrace_simple.ldscript
├── instrace_x86_binary.ldscript
├── instrace_x86_text.ldscript
├── instrcalls.ldscript
├── Makefile
├── memtrace_simple.ldscript
...
├── ssljack.ldscript
├── stl_test.ldscript
├── syscall.ldscript
└── wrap.ldscript

执行make inscount后,首先会扫描目标的依赖集,告诉你生成的目标文件的用法

make inscount
Scanning dependencies of target inscount
[ 50%] Building CXX object CMakeFiles/inscount.dir/inscount.cpp.o
[100%] Linking CXX shared library bin/libinscount.so
Usage: pass to drconfig or drrun: -c ../dynamorio/build/bin/libinscount.so
[100%] Built target inscount

重新回到dynamorio文件目录下,运行./bin64/drrun -c ./build/bin/libinscount.so -- gedit,注意根据你的机器位数选择对应的可执行文件。此处我们以统计可执行文件gedit的指令数量为例。其显示结果为Instrumentation results: 38676535 instructions executed

至此,DynamoRIO整个流程相当于是跑通了。通过阅读inscont.cC源文件可以发现,其大部分是调用了DR的API来实现其功能。所以说如果我们需要实现对程序定制的控制逻辑,需要了解官方API文档。

你可能感兴趣的:(工具笔记)