Is it possible to debug an llvm pass using gdb? I couldn't find any docs on the llvm site.
3 Answers
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.
2 Comments
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::preparePassManagerin terminal.Running the pass. I have a bitcode file called
trial.bcand the sameLLVMHello.sopass so I run it withrun -load ~/llvm/build/lib/LLVMHello.so -hello < ~/llvmexamples/trial.bc > /dev/nullgdb will now stop at
Pass::preparePassManagerand from here on we can use step and next to trace the execution.