<html><head><meta http-equiv="Content-Type" content="text/html; charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class=""><font face="Times" style="font-size: 14px;" class="">Hello All,</font></div><div class=""><font face="Times" style="font-size: 14px;" class=""><br class=""></font></div><div class=""><font face="Times" style="font-size: 14px;" class="">I am trying to write llvm pass to instrument function in external library. I am using clang to automatically load my pass. I have test.cpp which calls functions in library testlib.so. I have written llvm pass to instrument all reads and writes in test.cpp and call function(</font><span style="font-family: Times; font-size: 14px; background-color: rgb(255, 255, 255);" class="">recordRW</span><span style="font-size: 14px; font-family: Times;" class="">) in testlib.so. </span></div><div class=""><font face="Times" style="font-size: 14px;" class="">this is how my pass looks like -</font></div><div class=""><font face="Times" style="font-size: 14px;" class=""><br class=""></font></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Times" style="font-size: 14px;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(205, 121, 35);" class="">for</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> (</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 188, 38);" class="">auto</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> &BB : F) {</span></font></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Times" style="font-size: 14px;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">      </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(205, 121, 35);" class="">for</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> (</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 188, 38);" class="">auto</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> &I : BB) {</span></font></div><div style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(82, 48, 225); background-color: rgb(255, 255, 255);" class=""><font face="Times" style="font-size: 14px;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class="">        </span><span style="font-variant-ligatures: no-common-ligatures;" class="">// Is this a load or store? Get the address.</span></font></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Times" style="font-size: 14px;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">        Value *Ptr = </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(195, 55, 32);" class="">nullptr</span><span style="font-variant-ligatures: no-common-ligatures;" class="">;</span></font></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Times" style="font-size: 14px;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">        Value *op_l = </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(195, 55, 32);" class="">nullptr</span><span style="font-variant-ligatures: no-common-ligatures;" class="">;</span></font></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Times" style="font-size: 14px;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">        Value *op_s = </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(195, 55, 32);" class="">nullptr</span><span style="font-variant-ligatures: no-common-ligatures;" class="">;</span></font></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Times" style="font-size: 14px;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">        </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(205, 121, 35);" class="">if</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> (</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 188, 38);" class="">auto</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> *LI = dyn_cast<LoadInst>(&I)) {</span></font></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures; font-size: 14px;" class=""><font face="Times" class="">          Ptr = LI->getPointerOperand();</font></span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(82, 48, 225); background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0); font-size: 14px;" class=""><font face="Times" class="">  </font></span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Times" style="font-size: 14px;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">            Constant* read = ConstantInt::get(Type::getInt32Ty(M.getContext()), </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(195, 55, 32);" class="">0</span><span style="font-variant-ligatures: no-common-ligatures;" class="">);</span></font></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures; font-size: 14px;" class=""><font face="Times" class="">            instrument_access(Ptr, &(*LI), read, M);</font></span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Times" style="font-size: 14px;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">            modified = </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(195, 55, 32);" class="">true;</span></font></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Times" style="font-size: 14px;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">        } </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(205, 121, 35);" class="">else</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(205, 121, 35);" class="">if</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> (</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 188, 38);" class="">auto</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> *SI = dyn_cast<StoreInst>(&I)) {</span></font></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures; font-size: 14px;" class=""><font face="Times" class="">          Ptr = SI->getPointerOperand();</font></span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(82, 48, 225); background-color: rgb(255, 255, 255);" class=""><font face="Times" style="font-size: 14px;" class=""><br class=""></font></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Times" style="font-size: 14px;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">            Constant* write = ConstantInt::get(Type::getInt32Ty(M.getContext()), </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(195, 55, 32);" class="">1</span><span style="font-variant-ligatures: no-common-ligatures;" class="">);</span></font></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures; font-size: 14px;" class=""><font face="Times" class="">            instrument_access(Ptr, &(*SI),write, M);</font></span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Times" style="font-size: 14px;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">            modified = </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(195, 55, 32);" class="">true</span><span style="font-variant-ligatures: no-common-ligatures;" class="">;</span></font></div><div style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(82, 48, 225); background-color: rgb(255, 255, 255);" class=""><font face="Times" style="font-size: 14px;" class=""><br class=""></font></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures; font-size: 14px;" class=""><font face="Times" class="">        }</font></span></div></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Times" style="font-size: 14px;" class="">    }</font></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures; font-size: 14px;" class=""><font face="Times" class="">}</font></span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures; font-size: 14px;" class=""><font face="Times" class=""><br class=""></font></span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures; font-size: 14px;" class=""><font face="Times" class=""><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 188, 38);" class="">void</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> instrument_access(Value* op, Instruction* inst, Constant* rdWr, Module& module) {</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">    SmallVector<Value*, </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(195, 55, 32);" class="">8</span><span style="font-variant-ligatures: no-common-ligatures;" class="">> args;</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">    Instruction* get_tid = CallInst::Create(getThreadId, args, </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(195, 55, 32);" class="">""</span><span style="font-variant-ligatures: no-common-ligatures;" class="">, inst);</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">    BitCastInst* bitcast = </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(205, 121, 35);" class="">new</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> BitCastInst(op,</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">           PointerType::getUnqual(Type::getInt8Ty(module.getContext())),</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">             </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(195, 55, 32);" class="">""</span><span style="font-variant-ligatures: no-common-ligatures;" class="">, inst);</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; min-height: 21px;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""></span><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">    args.push_back(get_tid);</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">    args.push_back(bitcast);</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">    args.push_back(rdWr);</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; min-height: 21px;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""></span><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">    Instruction* recordRWCall = CallInst::Create(recordRW, args, </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(195, 55, 32);" class="">""</span><span style="font-variant-ligatures: no-common-ligatures;" class="">, inst);</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">  }</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><br class=""></span></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class="">I have run my pass with opt and I could see that recordRWCall has been created but this function is not called when I run test.cpp. How can I integrate all this together? What I want to do is when I run test.cpp, and if there is any read or write instruction, recordRW defined in testlib.so should be called. I have read a lot on Internet but I could find a way to do something like this. Any help would be much appreciated.</div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class="">Regards,</div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class="">Sangeeta</div></font></span></div></body></html>