Reputation: 615
Is it possible to debug an llvm pass using gdb? I couldn't find any docs on the llvm site.
Upvotes: 12
Views: 9947
Reputation: 383
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 say loading debugging symbols ... (no debugging symbols found)
.
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.
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.
Upvotes: 7
Reputation: 415
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.)
Upvotes: 1
Reputation: 19965
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.
Upvotes: 10