I wrote a simple llvm Pass that counts opcodes in a C ++ source file. I have no problem with the source file and I have successfully taken its .bc file. Now, when I run it through my Pass, it crashes. Code to go below (SourceCode is not a problem):
#define DEBUG_TYPE "opCounter"
#include "llvm/Pass.h"
#include "llvm/IR/Function.h"
#include "llvm/Support/raw_ostream.h"
#include <map>
using namespace llvm;
namespace
{
struct CountOperands : public FunctionPass
{
std::map<std::string,int> opCounter;
static char ID;
CountOperands() : FunctionPass(ID) {}
virtual bool runOnFunction( Function &F)
{
errs() << "Function Name: " << F.getName() << "\n";
for (Function::iterator bb = F.begin(), e = F.end(); bb != e; ++bb)
{
BasicBlock &b = *bb;
errs() << "##########Works fine till here!"<<"\n";
for (BasicBlock::iterator i = b.begin(), e2 = b.end(); i != e2; ++i)
{
if ( opCounter.find(i->getOpcodeName()) == opCounter.end() )
{
opCounter[i->getOpcodeName()] = 1;
}
else
{
opCounter[i->getOpcodeName()] += 1;
}
}
}
std::map <std::string, int>::iterator i = opCounter.begin();
std::map <std::string, int>::iterator e3 = opCounter.end();
while(i != e3)
{
errs() << i->first << ": " << i->second << "\n";
i++;
}
errs() << "\n";
opCounter.clear();
return false;
}
};
}
char CountOperands::ID = 0;
static RegisterPass<CountOperands> X("opCounter", "Counts the OpCodes in a single Function");
I run these commands to run my test.cpp program through pass:
clang ++ -emit-llvm testOp.cpp -c -o test.bc then do and finally
opt -load ../../../ Release + Asserts / lib / LLVMopCounter.so -opCounter <test.bc> / dev / null
The output is similar:
homer@ubuntu:~/sbx/walle_code_execution/codeexe/aspire/bin2vm/LLVM-3.6.0/llvm.src/lib/Transforms/OperandCounter$ opt -load ../../../Release+Asserts/lib/LLVMopCounter.so -opCounter < test.bc >/dev/null
Function Name: main
##########Works fine till here!
0 libLLVM-3.4.so.1 0x00007f9bfecea5d2 llvm::sys::PrintStackTrace(_IO_FILE*) + 34
1 libLLVM-3.4.so.1 0x00007f9bfecea3c4
2 libc.so.6 0x00007f9bfd769ff0
3 LLVMopCounter.so 0x00007f9bfc7730a4
4 libLLVM-3.4.so.1 0x00007f9bfe6baf77 llvm::FPPassManager::runOnFunction(llvm::Function&) + 471
5 libLLVM-3.4.so.1 0x00007f9bfe6baffb llvm::FPPassManager::runOnModule(llvm::Module&) + 43
6 libLLVM-3.4.so.1 0x00007f9bfe6bd4b5 llvm::legacy::PassManagerImpl::run(llvm::Module&) + 693
7 opt 0x0000000000412c8d main + 2461
8 libc.so.6 0x00007f9bfd754ec5 __libc_start_main + 245
9 opt 0x0000000000413b40
Stack dump:
0. Program arguments: opt -load ../../../Release+Asserts/lib/LLVMopCounter.so -opCounter
1. Running pass 'Function Pass Manager' on module '<stdin>'.
2. Running pass 'Counts the OpCodes in a single Function' on function '@main'
Segmentation fault (core dumped)
source
share