Using GDB with the Julia compiler
Context
When starting to dive in the Julia internals, you will encounter C and C++ files at some point. Debugging these can be a pain... Without the right tools. And GDB is definitely one of them. It can be scary to use though... Especially when there is Julia running on top!
Things aren't that complicated though. See for yourself!
(Note that these are instructions for Linux. They should be somewhat reproducible with Windows/MacOS, but I can't guarantee it.)
Connecting GDB and Julia
Attaching GDB to a running Julia REPL
In your Julia REPL, type
getpid()
to get Julia's current process id.In another terminal, type
sudo gdb --pid JULIA_PID
, replacingJULIA_PID
with the process id you just got.sudo
is required to attach gdb to the Julia process like this.Setup breakpoints: you can type
break function_name
to break when the function is called, orbreak path/to/file.cpp:1234
to break when the line in the given file is reached.Type
c
(short forcontinue
) to let the Julia process continue.You are good to go! Follow these debugging tips to get started.
The easier way: launching Julia from gdb
The method above sure works, but it requires two terminals (might be annoying) and sudo
(might be problematic). You can also simply type gdb julia
, starting a new gdb session. Set your breakpoints, and then run
will actually start Julia and you will switch to its REPL. When encountering a breakpoint, you switch back to the gdb console. You can go back and forth like this.
The lack of simultaneous control on gdb and julia can be annoying, but this is quicker to set up. The choice is yours!
Some tips
Printing variables
More info here.
For
jl_value_t *
variables, you can typecall jl_(my_var)
in GDB to printmy_var
in the Julia REPL.For LLVM variables, you can type
call my_var->dump()
in GDB to printmy_var
in the GDB terminal.
GDB caveats
Typing
Enter
with an empty line in GDB repeats the last command. And commands are buffered! Meaning, if you type Enter 5 times after runningcontinue
, it willcontinue
5 times after hitting a breakpoint, effectively ignoring your 5 next breakpoints. Do not rage typeEnter
!