Debugging an llvm pass with gdb
Is it possible to debug an llvm pass using gdb? I couldn't f开发者_如何学编程ind any docs on the llvm site.
Yes. Build LLVM in non-release mode (the default). It takes a bit longer than a release build, but you can use gdb to debug the resulting object file.
One note of caution: I had to upgrade my Linux box to 3GB of memory to make LLVM debug mode link times reasonable.
First make sure LLVM is compiled with debug options enabled, which is basically the default setting. If you didn't compile LLVM with non-default options then your current build should be fine.
All LLVM passes are run using LLVM's opt
(optimizer) tool. Passes are compiled into shared object files, i.e., LLVMHello.so
file in build/lib
and then loaded by the opt
tool. To debug or step through the pass we have to halt LLVM before it starts executing the .so
file because there is no way to put a break point in a shared object file. Instead, we can put a break in the code before it invokes the pass.
We're going to put a breakpoint in llvm/lib/IR/Pass.cpp
Here's how to do it:
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 sayloading debugging symbols ... (no debugging symbols found)
.Now we need to set a break point at the
void Pass::preparePassManager(PMStack &)
method inPass.cpp
. This is probably the first (or one of the first) methods involved in loading the pass. You can do this by by typingbreak llvm::Pass::preparePassManager
in terminal.Running the pass. I have a bitcode file called
trial.bc
and the sameLLVMHello.so
pass so I run it withrun -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.
Following Richard Penningtons advice + adding backticks works for me:
gdb /usr/local/bin/opt
then type
run `opt -load=/pathTo/LLVMHello.so -hello < /pathTo/your.bc > /dev/null`
Note: I would have commented, but couldn't (missing rep.)
精彩评论