zsim原版默认不支持trace driven(源码里虽然有这一块,但是是半成品),不过其他人实现了trace-driven版本
链接
一、环境
gcc-7(使用gcc-7的原因是因为memtrace这个项目的SConstruct里代码写的
env['CC'] = 'gcc7' env['CXX'] = 'g++-7'
,我就尽量和作者一致了)
ubuntu 16.04
代码:https://github.com/stanford-mast/zsim/tree/memtrace,我使用的github项目版本(commitID:70c8aa6ea5e52bf3e12400adc3644c8792c5ceaa)
gcc-7安装
在source.list里加(中科大的ppa反向代理)deb https://launchpad.proxy.ustclug.org/ubuntu-toolchain-r/test/ubuntu xenial main
apt安装sudo apt install g++7
,sudo apt install gcc-7
另外,排个坑,用gcc-4.8.5编译DynamoRIO源码,好像因为默认不是c++11,所以编译出来的库有问题,而gcc-7没问题
1.1 安装说明
- zsim-trace-driven目前支持两种格式的trace,分别是MEMTRACE、PT(YT没有实现,不考虑)。
- Memtrace格式的trace是由dynamorio截取的;PT格式的trace是由perf截取的。
在安装的时候 - PT无法用于内存模拟,得到的读写地址会始终为0(我已经发邮件向作者求证过了)
- 因此在安装前,根据trace前端不同,安装也不完全一样。
- 先有的Memtrace版本,后有的PT版本。PT版本没有把依赖完全清掉,所以使用PT trace的话,暂时还是要装dynamorio(或者可以自己修改文件来移除依赖)
1.2 安装依赖
1.2.1 zsim本身的依赖
- pin(可以使用 ramulator-pim 里面的pin)
- libhdf5:
sudo apt install libhdf5-dev
,然后在SConstruct里改几行代码,把hdf5的include和lib加进去
..
- libconfig (可以使用 ramulator-pim 里面的libconfig)
- boost
sudo apt install libboost-dev-all
1.2.2 zsim-trace-driven添加的依赖
perf(可选,使用Memtrace时,不需要安装)
-
我用apt安装,不行,因为内核版本不知道填啥,没有完全对应的,于是我就下载linux源码进行安装
-
有的linux内核中的perf版本,不支持一些参数,也就是下面这个命令,即不支持insnlen,insn两个参数,后来我就下载linux 5.8的内核源码,使用里面的perf是没问题的。
perf script --itrace=i0ns --ns -F ip,insnlen,insn
报错:
| perf script --itrace=i0ns --ns -F ip,insnlen,insn
Invalid field requested.
Usage: perf script [<options>]
or: perf script [<options>] record <script> [<record-options>] <command>
or: perf script [<options>] report <script> [script-args]
or: perf script [<options>] <script> [<record-options>] <command>
or: perf script [<options>] <top-script> [script-args]
-F, --fields <str> comma separated output fields prepend with 'type:'. Valid types: hw,sw,trace,raw. Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,addr,symoff,period,iregs,brstack,brstacksym,flags
comm,tid,pid,time,cpu,event,trace,ip,sym,dso,addr,symoff,period,iregs,brstack,brstacksym,flags
-
安装过程很简单
源码下载:https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/snapshot/linux-5.8.tar.gz
cd linux-5.8 cd tools/perf make sudo ln ./perf /usr/bin
-
安装后,perf文档可能没更新,但是上面的命令已经可以成功运行了
xed安装
https://github.com/intelxed/xed
git clone https://github.com/intelxed/xed.git xed
git clone https://github.com/intelxed/mbuild.git mbuild
cd xed
./mfile.py
./mfile.py --shared
./mfile.py install
DynamoRIO安装
git clone https://github.com/DynamoRIO/dynamorio.git
可用的git版本 ad03a4ac7517b8cc3f21a2aa6e68b1a52cae74e4
,新版本的接口有一些不太一致
git reset --hard ad03a4ac7517b8cc3f21a2aa6e68b1a52cae74e4
可以进行回溯
安装教程github首页找不到,其实在wiki里后几页(现在好像又搬地方了。。不过我这里的备份应该还是可用的)
https://github.com/DynamoRIO/dynamorio/wiki/How-To-Build
dynamorio对cmake有版本要求,要求3.7版本以上的,我用ubuntu的apt安装的cmake版本比较低,所以只能自己去安装了。我使用的版本为3.7.2。(cmake-3.7.2-Linux-x86_64.sh)
参考:https://askubuntu.com/questions/355565/how-do-i-install-the-latest-version-of-cmake-from-the-command-line
附上cmake的安装方案之一(可选)
$ sudo mkdir /opt/cmake
$ sudo sh cmake-3.7.2-Linux-x86_64.sh --prefix=/opt/cmake
# 确认文件存在后再链接
$ sudo ln -s /opt/cmake/cmake-3.7.2-Linux-x86_64/bin/cmake /usr/local/bin/cmake
$ cmake -version
cmake version 3.7.2
CMake suite maintained and supported by Kitware (kitware.com/cmake).
dynamorio安装
# Install dependencies for Ubuntu 15+. Adjust this command as appropriate for
# other distributions (in particular, use "cmake3" for Ubuntu Trusty).
$ sudo apt-get install cmake g++ g++-multilib doxygen git zlib1g-dev
# Get sources.
$ git clone https://github.com/DynamoRIO/dynamorio.git
下面是我加的一行命令,回溯版本
$ git reset --hard ad03a4ac7517b8cc3f21a2aa6e68b1a52cae74e4
# Make a separate build directory. Building in the source directory is not
# supported.
$ cd dynamorio && mkdir build && cd build
# Generate makefiles with CMake. Pass in the path to your source directory.
$ cmake ..
# Build.
$ make -j
# Run echo under DR to see if it works. If you configured for a debug or 32-bit
# build, your path will be different. For example, a 32-bit debug build would
# put drrun in ./bin32/ and need -debug flag to run debug build.
$ ./bin64/drrun echo hello world
hello world
DynamoRIO我也是用gcc-7编译的,一开始用gcc 4.8.5就出问题了,原因是gcc 4.8.5编译的时候应该默认没有c++11?
二、使用
2.1 前端(trace种类)
该zsim修改版根据代码,是又三种格式(MEMTRACE、PT和YT),但是YT似乎还没有实现。
MEMTRACE、PT都可以使用,但是PT无法用于内存模拟,得到的地址会始终为0(我已经发邮件向作者求证过了)。而具体使用哪个trace,只要在配置文件里修改一下参数即可。
2.2 MEMTRACE
如何生成
- 使用DynamoRIO生成trace(使用dynamorio里的drmemtrace工具)
- 具体方法见文档 drcachesim Offline Traces and Analysis
# To dump a trace for future offline analysis, use the offline parameter:
$ bin64/drrun -t drcachesim -offline -- /path/to/target/app <args> <for> <app>
# The collected traces will be dumped into a newly created directory, which can be passed to drcachesim for offline cache simulation with the -indir option:
$ bin64/drrun -t drcachesim -indir drmemtrace.app.pid.xxxx.dir/
简单说明:
DynamoRIO自带一个模拟器叫drcachesim,上面命令中的-t drcachesim
就是告诉DynamoRIO我们要使用drcachesim
drcachesim使用的trace其实是由drmemtrace生成的,所以文件名才会是以drmemtrace开头
第一条命令中的-offline
表示我们需要生成离线的trace,再后面的命令就是我们要运行的程序了
而第二条命令
文档中有参数说明:
比如
-outdir
default value: .
For the offline analysis mode (when -offline is requested), specifies the path to a directory where per-thread trace files will be written.
但是我发现似乎“drmemtrace.app.pid.xxxx.dir”这个文件夹的名字无法指定?
如何使用
使用MEMTRACE时,需要指定两个参数
第一个是trace0,值为“drmemtrace.app.pid.xxxx.dir/raw”,是上一节第一条命令运行后生成的
另外一个是trace_binaries,值为“drmemtrace.app.pid.xxxx.dir/trace”,是上一节第二条命令运行后生成的
参考 配置文件
或者我的文件
trace0 = "/home/trace/DynamoRIO/temp/drmemtrace.test.o.16022.7939.dir/trace"
trace_binaries = "/trace/DynamoRIO/temp/drmemtrace.test.o.16022.7939.dir/raw"
#type
trace_type = "MEMTRACE";
2.3 PT-TRACE
使用perf生成,readme文档写得很清楚,且只有一个参数指定,比较简单。