dynInst的官方网站是http://www.dyninst.org/
1、下载dynInst安装包,网址为
http://www.dyninst.org/downloads/archive/
我下载的是8.x Series版本
libdyninst_8.1.2-1_amd64.deb和libdyninst-dev_8.1.2-1_amd64.deb
2.下载dynInst的依赖包boost、libelf、libdwarf、g++
1)下载boost
sudo apt-cache search boost
sudo apt-get install libboost-dev
2)下载libelf
sudo apt-cache search libelf
sudo apt-get install libelf-dev
3)下载libdwarf,参考链接http://askubuntu.com/questions/502749/install-libdwarf-so-on-ubuntu
1、Download libdwarf-20130207.tar.gz
Extract the archive and in terminal type:
2、cd dwarf-2013-02-07/libdwarf
./configure --enable-shared
make
3、At the end, just copy the libdawrf.so into /usr/lib
4)下载g++,否则会报错cc: error trying to exec ‘cc1plus’: execvp: No such file or directory
sudo apt-get install g++
3、安装dynInst
sudo dpkg -i libdyninst_8.1.2-1_amd64.deb
sudo dpkg -i libdyninst-dev_8.1.2-1_amd64.deb
如果要卸载该软件,可以使用(可以使用tab键补全)
sudo dpkg -r libdyninst
sudo dpkg -r libdyninst-dev
如果不出意外的话,dynInst就安装好了
4、写个example
参考http://www.wangchao.net.cn/bbsdetail_36147.html、http://it.zhaozhao.info/archives/60071和http://yinyunqiao.blogspot.com/2009/05/dyninst.html,但是由于时间的问题,dynInst的API做了改动,例子是不能运行的。下附我写的一个小例子,可以运行
test_dynamic.c:
#include<stdio.h>
#include<unistd.h>
void old(){
printf("old old old pid is %d\n",getpid()) ;
}
void new(){
printf("new new new pid is %d\n",getpid()) ;
}
int main(){
while(1){
old() ;
sleep(5) ;
}
}
这个程序里面包含2个函数,各自输出不同的打印信息。从程序里面我们可以看到,这个程序在运行时候,调用old函数,也就是说该程序的输出就是old old……..,这样的信息,那么好,我们编写修改程序,动态改变该程序,让该程序输出new函数的打印信息。
modify.cpp
#include<iostream>
#include<sys/fcntl.h>
#include<stdio.h>
#include<unistd.h>
#include"BPatch.h"
#include"BPatch_Vector.h"
#include"BPatch_thread.h"
#include"BPatch_snippet.h"
int main(int argc,char** argv){
BPatch* bpatch = new BPatch ;
BPatch_Vector<BPatch_function*> old_func ;
BPatch_Vector<BPatch_function*> new_func ;
BPatch_process* app = bpatch->processAttach("test_dynamic",atoi(argv[1])) ;
BPatch_image* appImage = app->getImage() ;
BPatch_Vector<BPatch_function*> *tmp = appImage->findFunction("old",old_func) ;
tmp = appImage->findFunction("new",new_func) ;
app->replaceFunction(*old_func[0],*new_func[0]) ;
app->detach(1) ;
return 0 ;
}
5、设置运行环境,执行例子
在.bashrc最后面添加
vim ~/.bashrc
DYNINST_INCLUDE=/usr/include/dyninst
DYNINST_LIB=/usr/lib
export DYNINSTAPI_RT_LIB=$DYNINST_LIB/libdyninstAPI_RT.so
编辑/etc/sysctl.d/10-ptrace.conf参考连接http://www.2cto.com/os/201303/197816.html
sudo vim /etc/sysctl.d/10-ptrace.conf
kernel.yama.ptrace_scope = 0
重启电脑
编译test_dynamic.c
gcc test_dynamic.c -o test_dynamic
编译modify.cpp
编写Makefile文件
DYNINST_INCLUDE=/usr/include/dyninst
DYNINST_LIB=/usr/lib
modify: modify.o
$(CC) modify.o -L$(DYNINST_LIB) -ldyninstAPI -o modify
modify.o: modify.cpp
$(CC) -c $(CFLAGS) -I$(DYNINST_INCLUDE) modify.cpp -std=c++0x
最后
make
6、运行例子
./test_dynamic.c
./modify [pid]