Home > Software design >  Equivalent of the Rstudio `browser()` function in Julia for debugging
Equivalent of the Rstudio `browser()` function in Julia for debugging

Time:02-11

I wondered if there is an equivalent to the browser() statement available in RStudio for debugging purposes for Julia (I am using the Juno IDE at the moment).

The R function browser() halts execution and invokes an environment browser when it is called. So, in principle, we can put browser() anywhere in our code to stop in this particular line and see what's stored in the environment at that moment, which is terrific for debugging purposes.

For instance, the code below will stop when i>3. Hence, that's exactly what we will see in the environment browser available in RStudio, where we will observe that i=4 at that moment in the code.

for (i in 1:5) {
  print(i)  
  if (i>3) {
    browser()
  }
}
[1] 1
[1] 2
[1] 3
[1] 4
Called from: eval(ei, envir)
Browse[1]> 

CodePudding user response:

Have a look at Debugger.jl. Specifically the Place breakpoints in source code section:

It is sometimes more convenient to choose in the source code when to break. This is done for instance in Matlab/Octave with keyboard, and in R with browser(). You can use the @bp macro to do this

Your R example translated to Julia:

julia> using Debugger

julia> @run for i in 1:5
           println(i)
           if i > 3
               @bp
           end
       end
1
2
3
4
Hit breakpoint:
In ##thunk#257() at REPL[4]:1
  9  │         Base.println(i)
 10  │    = i > 3
 11  └──       goto #4 if not 
●12  3 ─       nothing
>13  4 ┄       @_2 = Base.iterate(%1, %8)
 14  │    = @_2 === nothing
 15  │    = ($(QuoteNode(Core.Intrinsics.not_int)))()
 16  └──       goto #6 if not 
 17  5 ─       goto #2

About to run: (iterate)(1:5, 4)
1|debug>

This is a general solution for Julia, Juno IDE also has integrated debugging: Debugging, Juno manual.

CodePudding user response:

Infiltrator.jl's @infiltrate seems like the equivalent in Julia:

julia> using Infiltrator

julia> for i in 1:5
         println(i)
         if i > 3
           @infiltrate
         end
       end
1
2
3
4
Infiltrating top-level scope at REPL[1]:4:

infil> i
4
  • Related