我是新來LLVM,並試圖找到鎖聲明語句,然後做一些instrumention工作中,這樣的代碼:如何在llvm中發現鎖定聲明指令?
#include <iostream>
#include <thread>
#include <mutex>
using namespace std;
int share = 42;
mutex m;
void f()
{
m.lock();
--share;
cout << "function f -> share: " << share << '\n';
m.unlock();
}
int main()
{
thread thf{f};
thf.join();
return 0;
}
我想找到鎖聲明指令如: mutex m;
的llvm instrumention通過這樣的:
struct SkeletonPass : public FunctionPass {
static char ID;
SkeletonPass() : FunctionPass(ID) {}
virtual bool runOnFunction(Function &F) {
// Get the function to call from our runtime library.
LLVMContext &Ctx = F.getContext();
Constant *logFunc = F.getParent()->getOrInsertFunction(
"logop", Type::getVoidTy(Ctx), Type::getInt32Ty(Ctx), NULL
);
for (auto &B : F) {
for (auto &I : B) {
***if ((&I) is lock declaration instruction)*** {
// Insert something *after* `op`.
IRBuilder<> builder(op);
builder.SetInsertPoint(&B, ++builder.GetInsertPoint());
// Insert a call to function.
builder.CreateCall(logFunc, ConstantInt::get(Type::getInt32Ty(Ctx), 2));
return true;
}
}
}
總之,請你告訴我如何發現鎖定聲明指令,謝謝!
非常感謝!在你的幫助下,我解決了這個問題。演示非常方便! –
我有[鏈接]另一個問題(http://stackoverflow.com/questions/35893006/how-to-define-string-type-in-getorinsertfunction-llvm)。我認爲你必須知道如何解決它。如果你知道,請告訴我該怎麼做。謝謝 ! –