Sandra,
It's unlikely you'll get someone to help you debug a problem like
this without more information (and without giving some evidence that
you have tried hard to fix it yourself). Let me suggest what I
suggest to post-graduate and undergraduate students taking my
compiler classes when they send such a question:
1. Use a debugger (e.g. gdb) to find the symptoms. You may have done
so already but if so, it wasn't clear.
2. Try to narrow down the cause of the problem yourself. Often, the
problem is obvious and you won't need help.
3. If you still cannot figure it out, try to construct a small test
case that shows the problem. For LLVM, use bugpoint to do this -- it
should work very well for your kind of pass.
4. If you still need help, give this list as much information as you
can based on what you've found so far. Then, they will feel
justified in helping you.
Good luck,
--Vikram Adve
----------------------------------------------------------------------
VIKRAM S. ADVE
Associate Professor, Computer Science E-MAIL: vadve at cs.uiuc.edu
Siebel Center for Computer Science PHONE: (217) 244-2016
Univ. of Illinois at Urbana-Champaign FAX: (217) 265-6582
201 N. Goodwin Ave. http://www.cs.uiuc.edu/~vadve
Urbana IL 61801-2302. http://llvm.cs.uiuc.edu/
----------------------------------------------------------------------
On Oct 16, 2005, at 1:24 PM, Sandra Johnson wrote:
> Hi ,
>
> I am using LLVM for my Post Graduate course project on
> Optimization. I am trying to do some insrtumentation to the
> bytecode.I 've been going through your Instrumentation code for the
> past few days in /llvm/lib/Transforms/Instrumentation folder and
> finally found two ways of instrumentation :
>
> 1) injecting LLVM bytecode instructions
>
> 2) calling an external C function.
>
> I am trying both and I am comfortable with the 2).
> My goal is to insert a global variable , which will count the total
> number of functions executed.At each method entry the global
> variable should be incremented and at the last method exit , it
> should print the count. The code compiled successfully, but when I
> used in the opt tool, I get an error in the getElementPtr(..)
> statement and finally a segmentation fault error. Please help me
> regarding this.
>
> I am sending the code as an attachment.
>
> I 've written a pass which will do instrumentation using the opt
> tool and then execute with lli JIT enabled.
>
> Thank you ,
>
> Sandra Johnson , M.E. (C.S.E.)
>
> Anna University,
>
> Chennai , India.
>
>
> Yahoo! Music Unlimited - Access over 1 million songs. Try it free.
> //===- Sample.cpp - Example code from "Writing an LLVM Pass"
> ---------------===//
> //
> // The LLVM Compiler Infrastructure
> //
>
>
> #include "llvm/Constants.h"
> #include "llvm/DerivedTypes.h"
> #include "llvm/Instructions.h"
> #include "llvm/Transforms/Instrumentation.h"
> #include "llvm/Transforms/ProfilingUtils.h"
> //copied from lib/Transforms/Instrumentation to include/Transforms
> #include "llvm/Assembly/Writer.h"
> #include "llvm/Support/Debug.h"
>
>
>
> #include "llvm/Pass.h"
> #include "llvm/Function.h"
> #include "llvm/Module.h"
> #include "Samplefunctions.h"
> #include <iostream>
> #include <sstream>
> using namespace llvm;
>
> void InsertPrintCode(Value *v,std::string str,Function
> *Func,Instruction *InsertBefore,Module &M)
> {
> std::ostringstream OutStr;
> if (v) WriteAsOperand(OutStr, v);
> Constant *Init = ConstantArray::get(str+OutStr.str()+"\n");
> GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
>
> GlobalValue::InternalLinkage, Init,
> "");
> M.getGlobalList().push_back(GV);
> Constant *GEP=ConstantExpr::getGetElementPtr(GV,
> std::vector<Constant*>(2,Constant::getNullValue
> (Type::LongTy)));
> std::vector<Value*> PrintArgs;
> PrintArgs.push_back(GEP);
> if (v) {
>
> PrintArgs.push_back(v);
>
> }
>
>
> new CallInst(Func, PrintArgs, "", InsertBefore);
>
> }
> static void InsertInstrumentationCall (BasicBlock *BB,
> const std::string FnName) {
> DEBUG (std::cerr << "InsertInstrumentationCall (\""
<< BB-
> >getName ()
> << "\", \"" << FnName
<< ")\n");
> Module &M = *BB->getParent ()->getParent ();
> Function *InstrFn = M.getOrInsertFunction (FnName,
> Type::VoidTy,NULL);
>
> // Insert the call after any alloca or PHI instructions...
> BasicBlock::iterator InsertPos = BB->begin();
> while (isa<AllocaInst>(InsertPos) || isa<PHINode>(InsertPos))
> ++InsertPos;
>
> std::vector<Value*> noargs;
> Instruction *InstrCall = new CallInst (InstrFn,"", InsertPos);
> }
>
>
>
> namespace {
> // FuncProf - The first implementation, without getAnalysisUsage.
> /*struct FuncProf : public FunctionPass {
> virtual bool runOnFunction(Function &F) {
>
>
> std::cerr << "Function Profiling : " <<
F.getName() << "\n";
> return false;
> }
> };
> RegisterOpt<FuncProf> X("myprofile", "Runtime
Optimization Pass");*/
>
> // ModuleProf - The first implementation, without getAnalysisUsage.
> struct ModuleProf : public ModulePass {
> virtual bool runOnModule(Module &M) {
> std::cerr << "O.K..1\n";
> const Type *SBP=PointerType::get(Type::SByteTy);
> const FunctionType *MTy=FunctionType::get
> (Type::IntTy,std::vector<const Type*>(1,SBP),true);
> Function *PrintFunc=M.getOrInsertFunction("printf",MTy);
>
> unsigned i = 0;
> BasicBlock *BB;
> Instruction *InsertPos;
> ReturnInst *RetPos;
> const std::string &msgentry="Entering Function : ";
> const std::string &msgexit ="Leaving Function : ";
> const std::string &msgexit1 ="Leaving Function : %d ";
>
> GlobalVariable *Count = new GlobalVariable(Type::IntTy, false,
> GlobalValue::InternalLinkage, 0,
> "FunCtr");
>
> M.getGlobalList().push_back(Count);
>
>
> // Create the getelementptr constant expression
> std::vector<Value*> indices;
> indices.push_back(ConstantInt::get(Type::IntTy, 0));
> indices.push_back(ConstantInt::get(Type::IntTy, 0));
>
> GlobalValue *Counter=Count;
> std::cerr << "O.K..2\n";
> Constant *FnVar = ConstantExpr::getGetElementPtr(Counter,
> indices);
> std::cerr << "O.K..3\n";
>
> Value *Int0 = ConstantInt::get(Type::IntTy, 0);
> Value *Int1=ConstantSInt::get(Type::UIntTy,1);
>
> Instruction *rInst;
> for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
> {
> BB=&F->getEntryBlock();
>
> if (!F->isExternal())
> {
>
> InsertPos=BB->begin();
> RetPos= cast<ReturnInst>(BB->getTerminator());
> //FunCtr++
>
> Value *OldVal=new LoadInst(FnVar, "FunCtr",
InsertPos);
> //BB->getInstList().push_back(OldVal);
>
> Value *NewVal=BinaryOperator::create(Instruction::Add,
> OldVal, Int1,"FunCtr", InsertPos);
> //BB->getInstList().push_back(NewVal);
>
>
> Instruction *stInst = new StoreInst(NewVal, FnVar, InsertPos);
> //BB->getInstList().push_back(stInst);
> //
>
>
> //IncrementCounterInBlock(F->begin(), 0, Counters);
> InsertPrintCode(0,msgentry+F->getName(),PrintFunc,InsertPos,M);
> InsertInstrumentationCall (BB, "llvm_profile_function");
> InsertPrintCode(0,msgexit+F->getName(),PrintFunc,RetPos,M);
> //Value *Intem = ConstantInt::get(Type::IntTy, Counters);
> //InsertPrintCode(Intem,msgexit1+F->getName
> (),PrintFunc,RetPos,M);
>
> }
> }
>
> return true;
> }
> };
> RegisterOpt<ModuleProf> X("moduleprofile", "Runtime
Optimization
> Module Pass");
> }
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>