# use gdb to debug julia

**URL:** <https://discourse.julialang.org/t/use-gdb-to-debug-julia/17679>\
**Category:** General Usage\
**Created:** [November 18, 2018, 2:48pm UTC](https://discourse.julialang.org/t/use-gdb-to-debug-julia/17679 "2018-11-18T14:48:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ralzq01](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralzq01/32/6143_2.png) [@ralzq01](https://discourse.julialang.org/u/ralzq01)\
**Post date:** [November 18, 2018, 2:48pm UTC](https://discourse.julialang.org/t/use-gdb-to-debug-julia/17679/1 "2018-11-18T14:48:31Z")

</div>

Hi, I am trying to use gdb for julia debugging. Although there is a [debugging tips](https://docs.julialang.org/en/v1/devdocs/debuggingtips/#Displaying-Julia-variables-1), 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:

```julia
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!

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 18, 2018, 3:20pm UTC](https://discourse.julialang.org/t/use-gdb-to-debug-julia/17679/2 "2018-11-18T15:20:24Z")

</div>

`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](https://en.wikipedia.org/wiki/X86_calling_conventions#x86-64_calling_conventions). (`$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_`

---

<div class="post-metadata">

**Author:** ![ralzq01](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralzq01/32/6143_2.png) [@ralzq01](https://discourse.julialang.org/u/ralzq01)\
**Post date:** [November 19, 2018, 6:34am UTC](https://discourse.julialang.org/t/use-gdb-to-debug-julia/17679/3 "2018-11-19T06:34:07Z")

</div>

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