使用gdb调试llvm pass。

12

是否可以使用gdb调试llvm pass?我在llvm网站上找不到任何文档。

3个回答

9

是的。在非发布模式下构建LLVM(默认情况下)。它比发布版本构建需要更长时间,但您可以使用gdb调试生成的目标文件。

一个注意事项:我不得不将我的Linux机器升级到3GB内存才能使LLVM调试模式的链接时间合理。


3
但是我能调试LLVM Pass吗?通常使用opt运行LLVM Pass,所以我不知道如何使用gdb来运行它。你能告诉我命令吗? - Arjun Singri
2
你可以运行例如"gdb /usr/local/bin/opt"并输入"run <your command line>"。 - Richard Pennington

7

首先确保LLVM使用启用了调试选项编译,这基本上是默认设置。如果您没有使用非默认选项编译LLVM,则当前的构建应该是可以的。

所有LLVM Passes都是使用LLVM的opt(优化器)工具运行的。Passes被编译成共享对象文件,即build/lib中的LLVMHello.so文件,然后由opt工具加载。为了调试或跟踪Pass,我们必须在LLVM开始执行.so文件之前停止它,因为无法在共享对象文件中设置断点。相反,我们可以在调用Pass之前的代码中设置断点。

我们将在llvm/lib/IR/Pass.cpp中设置断点。

以下是如何操作:

  1. Navigate to build/bin and open terminal and type gdb opt. If you compiled llvm with the debug symbols added then gdb will take some time to load debugging symbols, otherwise gdb will say loading debugging symbols ... (no debugging symbols found).

  2. Now we need to set a break point at the void Pass::preparePassManager(PMStack &) method in Pass.cpp. This is probably the first (or one of the first) methods involved in loading the pass. You can do this by by typing break llvm::Pass::preparePassManager in terminal.

  3. Running the pass. I have a bitcode file called trial.bc and the same LLVMHello.so pass so I run it with

    run -load ~/llvm/build/lib/LLVMHello.so  -hello < ~/llvmexamples/trial.bc > /dev/null
    

    gdb will now stop at Pass::preparePassManager and from here on we can use step and next to trace the execution.


嗨,我想在构建LLVM时没有添加调试符号。我仍然可以使用gdb进行调试吗?这会对调试有任何限制吗? - algoProg
不应该有任何问题。 - Muhammad Safi

1

遵循Richard Pennington的建议+添加反引号对我有效:

gdb /usr/local/bin/opt

然后输入。
run `opt -load=/pathTo/LLVMHello.so  -hello < /pathTo/your.bc > /dev/null`

注意:我本来想评论,但是由于声望不足而无法评论。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接