12

Is it possible to debug an llvm pass using gdb? I couldn't find any docs on the llvm site.

3 Answers 3

10

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.

Sign up to request clarification or add additional context in comments.

2 Comments

But can I debug an llvm pass? LLVM passes are usually run using opt so I don't know how to use gdb to run it. Can you tell me the command?
You can run e.g. "gdb /usr/local/bin/opt" and type "run <your command line>".
7

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:

  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.

2 Comments

Hi, I think I did not add debug symbols while building LLVM. Can I still use gdb for debugging? Will that have any limitations on debugging?
There shouldn't be any problems.
1

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.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.