<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" class="" style="font-size: 14px;">Hello All,</font></div><div class=""><font face="Times" class="" style="font-size: 14px;"><br class=""></font></div><div class=""><font face="Times" class="" style="font-size: 14px;">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 class="" style="font-family: Times; font-size: 14px; background-color: rgb(255, 255, 255);">recordRW</span><span class="" style="font-size: 14px; font-family: Times;">) in testlib.so. </span></div><div class=""><font face="Times" class="" style="font-size: 14px;">this is how my pass looks like -</font></div><div class=""><font face="Times" class="" style="font-size: 14px;"><br class=""></font></div><div class=""><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);"><font face="Times" class="" style="font-size: 14px;"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(205, 121, 35);">for</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> (</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 188, 38);">auto</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> &BB : F) {</span></font></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);"><font face="Times" class="" style="font-size: 14px;"><span class="" style="font-variant-ligatures: no-common-ligatures;">      </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(205, 121, 35);">for</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> (</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 188, 38);">auto</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> &I : BB) {</span></font></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(82, 48, 225); background-color: rgb(255, 255, 255);"><font face="Times" class="" style="font-size: 14px;"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);">        </span><span class="" style="font-variant-ligatures: no-common-ligatures;">// Is this a load or store? Get the address.</span></font></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);"><font face="Times" class="" style="font-size: 14px;"><span class="" style="font-variant-ligatures: no-common-ligatures;">        Value *Ptr = </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(195, 55, 32);">nullptr</span><span class="" style="font-variant-ligatures: no-common-ligatures;">;</span></font></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);"><font face="Times" class="" style="font-size: 14px;"><span class="" style="font-variant-ligatures: no-common-ligatures;">        Value *op_l = </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(195, 55, 32);">nullptr</span><span class="" style="font-variant-ligatures: no-common-ligatures;">;</span></font></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);"><font face="Times" class="" style="font-size: 14px;"><span class="" style="font-variant-ligatures: no-common-ligatures;">        Value *op_s = </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(195, 55, 32);">nullptr</span><span class="" style="font-variant-ligatures: no-common-ligatures;">;</span></font></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);"><font face="Times" class="" style="font-size: 14px;"><span class="" style="font-variant-ligatures: no-common-ligatures;">        </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(205, 121, 35);">if</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> (</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 188, 38);">auto</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> *LI = dyn_cast<LoadInst>(&I)) {</span></font></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures; font-size: 14px;"><font face="Times" class="">          Ptr = LI->getPointerOperand();</font></span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(82, 48, 225); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0); font-size: 14px;"><font face="Times" class="">  </font></span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);"><font face="Times" class="" style="font-size: 14px;"><span class="" style="font-variant-ligatures: no-common-ligatures;">            Constant* read = ConstantInt::get(Type::getInt32Ty(M.getContext()), </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(195, 55, 32);">0</span><span class="" style="font-variant-ligatures: no-common-ligatures;">);</span></font></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures; font-size: 14px;"><font face="Times" class="">            instrument_access(Ptr, &(*LI), read, M);</font></span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);"><font face="Times" class="" style="font-size: 14px;"><span class="" style="font-variant-ligatures: no-common-ligatures;">            modified = </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(195, 55, 32);">true;</span></font></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);"><font face="Times" class="" style="font-size: 14px;"><span class="" style="font-variant-ligatures: no-common-ligatures;">        } </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(205, 121, 35);">else</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(205, 121, 35);">if</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> (</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 188, 38);">auto</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> *SI = dyn_cast<StoreInst>(&I)) {</span></font></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures; font-size: 14px;"><font face="Times" class="">          Ptr = SI->getPointerOperand();</font></span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(82, 48, 225); background-color: rgb(255, 255, 255);"><font face="Times" class="" style="font-size: 14px;"><br class=""></font></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);"><font face="Times" class="" style="font-size: 14px;"><span class="" style="font-variant-ligatures: no-common-ligatures;">            Constant* write = ConstantInt::get(Type::getInt32Ty(M.getContext()), </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(195, 55, 32);">1</span><span class="" style="font-variant-ligatures: no-common-ligatures;">);</span></font></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures; font-size: 14px;"><font face="Times" class="">            instrument_access(Ptr, &(*SI),write, M);</font></span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);"><font face="Times" class="" style="font-size: 14px;"><span class="" style="font-variant-ligatures: no-common-ligatures;">            modified = </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(195, 55, 32);">true</span><span class="" style="font-variant-ligatures: no-common-ligatures;">;</span></font></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(82, 48, 225); background-color: rgb(255, 255, 255);"><font face="Times" class="" style="font-size: 14px;"><br class=""></font></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures; font-size: 14px;"><font face="Times" class="">        }</font></span></div></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);"><font face="Times" class="" style="font-size: 14px;">    }</font></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures; font-size: 14px;"><font face="Times" class="">}</font></span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures; font-size: 14px;"><font face="Times" class=""><br class=""></font></span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures; font-size: 14px;"><font face="Times" class=""><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 188, 38);">void</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> instrument_access(Value* op, Instruction* inst, Constant* rdWr, Module& module) {</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">    SmallVector<Value*, </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(195, 55, 32);">8</span><span class="" style="font-variant-ligatures: no-common-ligatures;">> args;</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">    Instruction* get_tid = CallInst::Create(getThreadId, args, </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(195, 55, 32);">""</span><span class="" style="font-variant-ligatures: no-common-ligatures;">, inst);</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">    BitCastInst* bitcast = </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(205, 121, 35);">new</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> BitCastInst(op,</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">           PointerType::getUnqual(Type::getInt8Ty(module.getContext())),</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">             </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(195, 55, 32);">""</span><span class="" style="font-variant-ligatures: no-common-ligatures;">, inst);</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; min-height: 21px;"><span class="" style="font-variant-ligatures: no-common-ligatures;"></span><br class=""></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">    args.push_back(get_tid);</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">    args.push_back(bitcast);</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">    args.push_back(rdWr);</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; min-height: 21px;"><span class="" style="font-variant-ligatures: no-common-ligatures;"></span><br class=""></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">    Instruction* recordRWCall = CallInst::Create(recordRW, args, </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(195, 55, 32);">""</span><span class="" style="font-variant-ligatures: no-common-ligatures;">, inst);</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">  }</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;"><br class=""></span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;">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 class="" style="margin: 0px; font-stretch: normal; line-height: normal;"><br class=""></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;">Regards,</div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal;">Sangeeta</div></font></span></div></body></html>