use gdb to debug julia

Hi, I am trying to use gdb for julia debugging. Although there is a debugging tips, I still can’t find the correct way to use gdb for julia.

For example, I want to show values of variable a in gdb for the following test.jl code:

a = 2
b = 3.
f(x, y) = 2x + y
d = f(a, b)
println("d: $d")

What I do is to first insert code ccall(:jl_breakpoint, Cvoid, (Any,), a) between line 3 and line 4, then I start gdb with $gdb --args julia test.jl and do (gdb) break jl_breakpoint . I also do (gdb) handle SIGSEGV noprint nostop pass before running. After run the gdb, I can hit the function but when I try to do (gdb) call jl_(a) , it will get !!! ERROR in jl_ -- ABORTING !!! .

Can someone show me how to correctly use gdb for Julia? Thanks!

jl_breakpoint is a C function so you need to use the c variable name instead of the julia one. I have no idea what gdb happens to pick up as a in this context. The C variable name, if gdb knows it, will be shown when you hit the break point (should be v). If you don’t have debug info, it won’t be shown and you won’t be able to access it by name. You can still access the function argument though but i don’t think there’s a generic way to do it. You need to know the calling convention on the platform for this. For x64, see x86 calling conventions - Wikipedia. ($rcx on windows and $rdi on unix). For x86 IIRC it’s on the stack. For arm and aarch64 it would be r0 and x0 respectively. No idea about power but probably a register. You can also use @code_native to figure this out in julia.

And if you only want to print the value, you can also do jump *jl_

2 Likes

Thank you so much! I realized I should compile julia to debug mode: make debug and use julia/usr/bin/julia-debug to activate gdb. In this case, I can see the parameter v in stack, so I can use call jl_(v) to get value of a.