Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Clang 如何在llvm中发现锁声明指令?_Clang_Llvm_Instrumentation - Fatal编程技术网

Clang 如何在llvm中发现锁声明指令?

Clang 如何在llvm中发现锁声明指令?,clang,llvm,instrumentation,Clang,Llvm,Instrumentation,我是llvm新手,尝试查找锁声明语句,然后执行一些检测工作,代码如下: #include <iostream> #include <thread> #include <mutex> using namespace std; int share = 42; mutex m; void f() { m.lock(); --share; cout << "function f -> share: " << s

我是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;
}
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;
      }
    }
  }

总之,您能告诉我如何发现锁声明指令吗,谢谢

您可以使用LLVM的
cppcbackend
来编译代码。这将产生一个组成源代码的C++代码。然后,您可以很容易地找到如何
mutexm定义是通过LLVM API构建的


运行
clang-march=cpp foo.cpp
以使用cppcbackend。或者,您可以使用在线编译代码。

您可以使用LLVM的
cppbend
编译代码。这将产生一个组成源代码的C++代码。然后,您可以很容易地找到如何
mutexm定义是通过LLVM API构建的


运行
clang-march=cpp foo.cpp
以使用cppcbackend。或者,您可以使用联机编译代码。

声明将显示为全局声明,因此您应该编写一个模块过程来查找它,而不是函数过程。它应该看起来像:

@m = global %mutex zeroinitializer
事实上,通过在上的演示,您确实可以看到:

...    
%"class.std::__1::mutex" = type { %struct.pthread_mutex_t }
%struct.pthread_mutex_t = type { %union.anon }
%union.anon = type { [5 x i8*] }
...
@m = global %"class.std::__1::mutex" zeroinitializer, align 8

声明将显示为全局,因此您应该编写一个模块过程来查找它,而不是函数过程。它应该看起来像:

@m = global %mutex zeroinitializer
事实上,通过在上的演示,您确实可以看到:

...    
%"class.std::__1::mutex" = type { %struct.pthread_mutex_t }
%struct.pthread_mutex_t = type { %union.anon }
%union.anon = type { [5 x i8*] }
...
@m = global %"class.std::__1::mutex" zeroinitializer, align 8

非常感谢!在你的帮助下我解决了这个问题。演示非常方便!Ths!我还有一个问题要问。我想你一定知道怎么解决它。如果你知道的话,你能告诉我怎么做吗。非常感谢。非常感谢!在你的帮助下我解决了这个问题。演示非常方便!Ths!我还有一个问题要问。我想你一定知道怎么解决它。如果你知道的话,你能告诉我怎么做吗。非常感谢。