Right now I am trying to capture the function name and the number of
arguments ,
so this following is the pass I wrote .
-------------------------------------------------------------
struct pass06a : public ModulePass {
virtual bool runOnModule(Module &M) {
std::vector<const Type*> pList;
pList.push_back( PointerType::get(Type::SByteTy) );
pList.push_back( Type::UIntTy );
FunctionType* FT = FunctionType::get( Type::IntTy, pList, /*is
vararg*/ false);
Function* CF = new Function(FT, Function::ExternalLinkage,
std::string("logExternalCalls"), &M);
for(Module::iterator i = M.begin(), e = M.end(); i != e; i++ ){
for(Function::iterator fi = i->begin(), fe = i->end(); fi != fe
; fi++ ){
for(BasicBlock::iterator bi = fi->begin(), be = fi->end(); bi
!= be ; bi++ ){
if(isa<CallInst>(*bi)){
const CallInst* CI = cast<CallInst>(bi);
const Function* F = CI->getCalledFunction() ;
if( F->isExternal() ){
std::vector<Value*> paramList;
Constant *fname = ConstantArray::get(F->getName());
Constant *GEP = ConstantExpr::getGetElementPtr(fname,
std::vector<Constant*>(2,Constant::getNullValue(Type::LongTy)));
paramList.push_back( GEP);
paramList.push_back( ConstantUInt::get( Type::UIntTy,
CI->getNumOperands()) );
CallInst* newCI = new CallInst( CF, paramList, "", bi
);
}
}
}
}
}
return true;
}
-------------------------------------------------------------------------------------------------------------------------------------------
and I have added the following function to tracelib.c
int logExternalCalls(char *fname, unsigned i){
printf("function: %s \t No of args: %d\n", fname, i);
return 0;
}
---------------------------------------------------------------------------------
The pass compiles fine but when i try to run it on a simple hello.c
program i get the following error
opt -load ~/work/llvm/Debug/lib/libpass06.so -pass06a <hello.bc >
test.bc
opt: Constants.cpp:1431: static llvm::Constant*
llvm::ConstantExpr::getGetElementPtr(llvm::Constant*, const
std::vector<llvm::Constant*, std::allocator<llvm::Constant*>
>&):
Assertion `Ty && "GEP indices invalid!"' failed.
opt((anonymous namespace)::PrintStackTrace()+0x1a)[0x86a74d2]
opt((anonymous namespace)::SignalHandler(int)+0xcb)[0x86a7745]
/lib/libc.so.6[0x40149b68]
/lib/libc.so.6(abort+0x16d)[0x4014b05d]
/lib/libc.so.6(__assert_fail+0x10f)[0x40142e2f]
opt(llvm::ConstantExpr::getGetElementPtr(llvm::Constant*,
std::vector<llvm::Constant*, std::allocator<llvm::Constant*> >
const&)+0xd9)[0x85effc7]
/home/ary/work/llvm/Debug/lib/libpass06.so((anonymous
namespace)::pass06a::runOnModule(llvm::Module&)+0x3dc)[0x40027902]
opt(llvm::PassManagerTraits<llvm::Module>::runPass(llvm::ModulePass*,
llvm::Module*)+0x1f)[0x865476b]
opt(llvm::PassManagerT<llvm::Module>::runOnUnit(llvm::Module*)+0x5c3)[0x8648197]
opt(llvm::PassManagerTraits<llvm::Module>::runOnModule(llvm::Module&)+0x1f)[0x8649835]
opt(llvm::PassManager::run(llvm::Module&)+0x23)[0x85ff639]
opt(main+0x95e)[0x83ab362]
/lib/libc.so.6(__libc_start_main+0xbc)[0x4013610c]
opt[0x83aa961]
Aborted
======================looks like my invocation to getGetElementPtr() is not what
it should be
. But I am not sure what it should be :(
Thanks,
Abhijit Ray