我想写一个LLVM传递来检测每个内存访问。 这是我想要做的。
给定任何C / C ++程序(如下面给出的那样),我试图在每次读取/写入/写入内存的指令之前和之后插入对某些函数的调用。例如,考虑下面的C ++程序(Account.cpp)
#include <stdio.h>
class Account {
int balance;
public:
Account(int b)
{
balance = b;
}
~Account(){ }
int read()
{
int r;
r = balance;
return r;
}
void deposit(int n)
{
balance = balance + n;
}
void withdraw(int n)
{
int r = read();
balance = r - n;
}
};
int main ()
{
Account* a = new Account(10);
a->deposit(1);
a->withdraw(2);
delete a;
}
所以在仪器之后我的程序应该是这样的:
#include <stdio.h>
class Account
{
int balance;
public:
Account(int b)
{
balance = b;
}
~Account(){ }
int read()
{
int r;
foo();
r = balance;
foo();
return r;
}
void deposit(int n)
{
foo();
balance = balance + n;
foo();
}
void withdraw(int n)
{
foo();
int r = read();
foo();
foo();
balance = r - n;
foo();
}
};
int main ()
{
Account* a = new Account(10);
a->deposit(1);
a->withdraw(2);
delete a;
}
其中foo()可以是任何函数,比如获取当前系统时间或递增计数器......等等。
请给我举例(源代码,教程等)以及如何运行它的步骤。我已经阅读了关于如何制作LLVM Pass的教程 http://llvm.org/docs/WritingAnLLVMPass.html,但无法弄清楚如何为上述问题写一个传球。