Instrumenting C/C++ code using LLVM

后端 未结 2 2041
温柔的废话
温柔的废话 2021-02-02 01:17

I want to write a LLVM pass to instrument every memory access. Here is what I am trying to do.

Given any C/C++ program (like the one given below), I am trying to insert

2条回答
  •  渐次进展
    2021-02-02 02:15

    Try something like this: ( you need to fill in the blanks and make the iterator loop work despite the fact that items are being inserted )

    class ThePass : public llvm::BasicBlockPass {
      public:
      ThePass() : BasicBlockPass() {}
      virtual bool runOnBasicBlock(llvm::BasicBlock &bb);
    };
    bool ThePass::runOnBasicBlock(BasicBlock &bb) {
      bool retval = false;
      for (BasicBlock::iterator bbit = bb.begin(), bbie = bb.end(); bbit != bbie;
       ++bbit) { // Make loop work given updates
       Instruction *i = bbit;
    
       CallInst * beforeCall = // INSERT THIS
       beforeCall->insertBefore(i);
    
       if (!i->isTerminator()) {
          CallInst * afterCall = // INSERT THIS
          afterCall->insertAfter(i);
       }
      }
      return retval;
    }
    

    Hope this helps!

提交回复
热议问题