# Debugging julia code

**URL:** <https://discourse.julialang.org/t/debugging-julia-code/21246>\
**Category:** General Usage\
**Created:** [February 27, 2019, 5:05am UTC](https://discourse.julialang.org/t/debugging-julia-code/21246 "2019-02-27T05:05:03Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Suraj\_Pawar](https://avatars.discourse-cdn.com/v4/letter/s/aca169/32.png) [@Suraj\_Pawar](https://discourse.julialang.org/u/Suraj_Pawar)\
**Post date:** [February 27, 2019, 5:05am UTC](https://discourse.julialang.org/t/debugging-julia-code/21246/1 "2019-02-27T05:05:03Z")

</div>

What are good strategies for debugging Julia code? Can I debug the Julia code using gdb? I mainly want to see the variables at various points in the program.  
Thank you.

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [February 27, 2019, 6:23am UTC](https://discourse.julialang.org/t/debugging-julia-code/21246/2 "2019-02-27T06:23:26Z")

</div>

`println("var: ", var)` 😉

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [February 27, 2019, 6:28am UTC](https://discourse.julialang.org/t/debugging-julia-code/21246/3 "2019-02-27T06:28:26Z")

</div>

Better `@show variable`.

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [February 27, 2019, 7:00am UTC](https://discourse.julialang.org/t/debugging-julia-code/21246/4 "2019-02-27T07:00:54Z")

</div>

[ASTInterpreter2](https://github.com/JuliaDebug/ASTInterpreter2.jl) seems to work

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [February 27, 2019, 7:38am UTC](https://discourse.julialang.org/t/debugging-julia-code/21246/5 "2019-02-27T07:38:55Z")

</div>

[GitHub - JuliaDebug/Debugger.jl: Julia debugger](https://github.com/JuliaDebug/Debugger.jl) is work-in-progress

---

<div class="post-metadata">

**Author:** ![Suraj\_Pawar](https://avatars.discourse-cdn.com/v4/letter/s/aca169/32.png) [@Suraj\_Pawar](https://discourse.julialang.org/u/Suraj_Pawar)\
**Post date:** [February 27, 2019, 5:54pm UTC](https://discourse.julialang.org/t/debugging-julia-code/21246/6 "2019-02-27T17:54:02Z")

</div>

is there any option in which I can execute the code line by line? I have too many variables and printing/ showing all variables might not be an efficient way to debug.  
Thank you.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [February 27, 2019, 5:58pm UTC](https://discourse.julialang.org/t/debugging-julia-code/21246/7 "2019-02-27T17:58:03Z")

</div>

Line-by-line debugging requires some work on the compiler first. Check this thread for more infos:

> [@Compiler work priorities](https://discourse.julialang.org/t/compiler-work-priorities/17623):
>
> In response to I thought I might write a little post about the rough priorities of the compiler team: Correctness finding and fixing compiler and inference bugs Multithreading [non-copying task stack switching](https://github.com/JuliaLang/julia/pull/13099) (done) [new PARTR parallel runtime](https://github.com/JuliaLang/julia/pull/22631)[locks for I/O operations](https://github.com/libuv/libuv/issues/1595) other thread safety Compile-time latency, aka “the time-to-first-plot problem” making compilation faster caching more things Compiler-related packages and tools [PackageCompiler](https://github.com/JuliaLang/PackageCompiler.jl) Debugger [Cxx](https://github.com/Keno/Cxx.jl) type checking/linting J…

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [February 27, 2019, 6:10pm UTC](https://discourse.julialang.org/t/debugging-julia-code/21246/8 "2019-02-27T18:10:13Z")

</div>

> [@Suraj\_Pawar](#):
>
> is there any option in which I can execute the code line by line? I have too many variables and printing/ showing all variables might not be an efficient way to debug.  
> Thank you.

Debugger.jl does precisely that:

```julia
julia> Debugger.@enter gcd(2,3)
In gcd(a, b) at intfuncs.jl:31
31 function gcd(a::T, b::T) where T<:Union{Int8,UInt8,Int16,UInt16,Int32,UInt32,Int64,UInt64,Int128,UInt128}
32 a == 0 && return abs(b)
33 b == 0 && return abs(a)

About to run: (==)(2, 0)
1|debug> n
In gcd(a, b) at intfuncs.jl:31
31 function gcd(a::T, b::T) where T<:Union{Int8,UInt8,Int16,UInt16,Int32,UInt32,Int64,UInt64,Int128,UInt128}
32 a == 0 && return abs(b)
33 b == 0 && return abs(a)
34 za = trailing_zeros(a)

About to run: (==)(3, 0)
1|julia> a+b
5

```

It’s not _quite_ ready for a release yet, but I hope we’ll get there soon.

---

<div class="post-metadata">

**Author:** ![evanfields](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evanfields/32/1744_2.png) [@evanfields](https://discourse.julialang.org/u/evanfields)\
**Post date:** [February 27, 2019, 6:15pm UTC](https://discourse.julialang.org/t/debugging-julia-code/21246/9 "2019-02-27T18:15:02Z")

</div>

It’s probably also worth mentioning [Rebugger](https://github.com/timholy/Rebugger.jl). I’m not sure how it compares to the in-progress Debugger.jl and would love to learn more!

---

<div class="post-metadata">

**Author:** ![Suraj\_Pawar](https://avatars.discourse-cdn.com/v4/letter/s/aca169/32.png) [@Suraj\_Pawar](https://discourse.julialang.org/u/Suraj_Pawar)\
**Post date:** [February 27, 2019, 7:58pm UTC](https://discourse.julialang.org/t/debugging-julia-code/21246/10 "2019-02-27T19:58:44Z")

</div>

[ASTInterpreter2](https://github.com/JuliaDebug/ASTInterpreter2.jl) seems to work

This seems to be a good package. It would be good to have a feature to print variable while debugging. “fr v” command lists variables, but not value of variables in some cases (especially matrices).  
Thank you.

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [February 27, 2019, 8:16pm UTC](https://discourse.julialang.org/t/debugging-julia-code/21246/11 "2019-02-27T20:16:51Z")

</div>

You can just use ` to switch to a (kind of) julia repl and enter the variable there to get it displayed. Or index into or whatever.

Debugger.jl + JuliaInterpreter.jl will replace ASTInterpreter2.jl soon, with less bugs and significantly higher performance.

---

<div class="post-metadata">

**Author:** ![Balinus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/balinus/32/243_2.png) [@Balinus](https://discourse.julialang.org/u/Balinus)\
**Post date:** [February 27, 2019, 9:00pm UTC](https://discourse.julialang.org/t/debugging-julia-code/21246/12 "2019-02-27T21:00:50Z")

</div>

Awesome news! 🙂

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [March 1, 2019, 1:30pm UTC](https://discourse.julialang.org/t/debugging-julia-code/21246/13 "2019-03-01T13:30:57Z")

</div>

Early adopters are welcome to play with it now, but keep in mind stuff may break. Hopefully within the next week or two we’ll have a major announcement and accompanying blog post.

The core work is happening in JuliaInterpreter; Debugger.jl, Rebugger.jl, and Juno will all be different “front ends.” If you’re interested exploring the new capabilities via Rebugger, try running with [this branch of Revise](https://github.com/timholy/Revise.jl/pull/243) and [this branch of Rebugger](https://github.com/timholy/Rebugger.jl/pull/60).

In the meantime, if people want to help us get over the finish line, [Julia tests don't pass · Issue #13 · JuliaDebug/JuliaInterpreter.jl · GitHub](https://github.com/JuliaDebug/JuliaInterpreter.jl/issues/13) is looking for all the helpers we can get. The new debugger will support breakpoints and run _all_ code in the interpreter, and for that to be a positive experience we have to be able to run any valid Julia code correctly. Julia’s own test suite is the best opportunity to find out how we’re doing. It’s starting to look pretty good (it wasn’t at the beginning 😊) but there is no doubt that important fixes remain to be made.

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [March 1, 2019, 1:51pm UTC](https://discourse.julialang.org/t/debugging-julia-code/21246/14 "2019-03-01T13:51:46Z")

</div>

The only thing I’d really want from a debugger is the ability to inspect the stack when an error occurs, without having to rerun the computations. Is there any hope along those lines? Common Lisp could do it, and it had a very similar memory/execution model.

---

<div class="post-metadata">

**Author:** ![braamvandyk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/braamvandyk/32/5086_2.png) [@braamvandyk](https://discourse.julialang.org/u/braamvandyk)\
**Post date:** [March 1, 2019, 4:43pm UTC](https://discourse.julialang.org/t/debugging-julia-code/21246/15 "2019-03-01T16:43:52Z")

</div>

Any plans to integrate Debugger into Juno?

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [March 1, 2019, 4:53pm UTC](https://discourse.julialang.org/t/debugging-julia-code/21246/16 "2019-03-01T16:53:55Z")

</div>

Yes, for sure. I plan to release a new Juno version with Debugger support at the same time the Debugger stack is released.

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [March 1, 2019, 6:47pm UTC](https://discourse.julialang.org/t/debugging-julia-code/21246/17 "2019-03-01T18:47:18Z")

</div>

The branch of Rebugger linked above should now be quite good at that, although (at least currently) it does require two passes, one to trigger the error and instrument the code, the second to capture the inputs.

But for an answer to what you’re precisely hoping for, it’s coming quite soon: [Add support for breakpoints (!) by timholy · Pull Request #81 · JuliaDebug/JuliaInterpreter.jl · GitHub](https://github.com/JuliaDebug/JuliaInterpreter.jl/pull/81)

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [March 1, 2019, 6:59pm UTC](https://discourse.julialang.org/t/debugging-julia-code/21246/18 "2019-03-01T18:59:53Z")

</div>

Doesn’t that PR require an explicit `breakpoint()`, thus I need run the code twice? Is there a way to automatically breakpoint on any exception?

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [March 1, 2019, 7:03pm UTC](https://discourse.julialang.org/t/debugging-julia-code/21246/19 "2019-03-01T19:03:40Z")

</div>

No, it doesn’t. See [this test](https://github.com/JuliaDebug/JuliaInterpreter.jl/blob/e7f71ca34ce79d60db0f6832f570fb71a98d7d81/test/breakpoints.jl#L69-L79).

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [March 1, 2019, 7:37pm UTC](https://discourse.julialang.org/t/debugging-julia-code/21246/20 "2019-03-01T19:37:54Z")

</div>

Ok, but… it implies running the whole thing inside of `JuliaInterpreter.enter_call`?

My scenario is: I do some development, then start some heavy computation. Five minutes later, there’s a MethodError. In Python or Common Lisp, I can directly step into the debugger and figure out what went wrong. I don’t think it’d be reasonable to run _everything_ through `JuliaInterpreter.jl`, is it? It would have significant runtime cost.

That said, I need to try Rebugger again, thank you so much for sharing all of your efforts. I’m constantly amazed at how thorough you’ve been with Revise.jl!

[Next page](https://discourse.julialang.org/t/debugging-julia-code/21246.md?page=2)
