# Debugging like R's browser() possible?

**URL:** <https://discourse.julialang.org/t/debugging-like-rs-browser-possible/13925>\
**Category:** New to Julia\
**Created:** [August 23, 2018, 10:17am UTC](https://discourse.julialang.org/t/debugging-like-rs-browser-possible/13925 "2018-08-23T10:17:12Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [August 23, 2018, 10:17am UTC](https://discourse.julialang.org/t/debugging-like-rs-browser-possible/13925/1 "2018-08-23T10:17:12Z")

</div>

In R, I could put a `browser()` into any where I want. It gives me a REPL so that inspection, changing variable values, etc. are possible. It makes debugging very easy and convenient.

Ideally, if we could do it in Julia, it would be like:

```julia
function xxx()
    """
    codes that are 100% fine
    """

    browser() # REPL for inspection and making arbitrary adhoc changes

    """
    codes that may have problems
    """
end

```

Is it possible in Julia?

Right now I’m using `ASTIntepreter2.@enter()`, which allows me to step into a function. However, everytime I have to start at the _beginning_ of the function rather in the _middle_, or being _conditional_…

any suggestions? thanks.

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [August 23, 2018, 1:00pm UTC](https://discourse.julialang.org/t/debugging-like-rs-browser-possible/13925/2 "2018-08-23T13:00:13Z")

</div>

My understanding is that these tools are under development. I don’t really know much about how they work or what features they will ultimately include. Nonetheless, you might look into the debugger utility discussed in this thread: [[ANN] Rebugger: interactive debugging for Julia 0.7/1.0 - #10 by tim.holy](https://discourse.julialang.org/t/ann-rebugger-interactive-debugging-for-julia-0-7-1-0/13843/10)

---

<div class="post-metadata">

**Author:** ![floswald](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/floswald/32/195_2.png) [@floswald](https://discourse.julialang.org/u/floswald)\
**Post date:** [August 23, 2018, 2:33pm UTC](https://discourse.julialang.org/t/debugging-like-rs-browser-possible/13925/3 "2018-08-23T14:33:41Z")

</div>

my best solution to this is to wrap

```
"""
codes that may have problems
"""

```

into a separate function and then do

```julia
newfunction()
    """
    codes that may have problems
    """
end
function xxx()
    """
    codes that are 100% fine
    """

    if debugging
        @enter newfunction()
    end
end

```

---

<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:** [August 23, 2018, 3:07pm UTC](https://discourse.julialang.org/t/debugging-like-rs-browser-possible/13925/4 "2018-08-23T15:07:47Z")

</div>

…and we have our first example of something “breakpoint-like” for Rebugger 😄. I started a list here: [https://github.com/timholy/Rebugger.jl/issues/33](https://github.com/timholy/Rebugger.jl/issues/33)

I do want to understand whether this is really something we want or whether there might be a better way. So some questions:

- how often do you know in advance which method is problematic? Most often I start at the top and work my way into the call chain, discovering as I go where the problem lies. Hence “stepin” functionality typically covers my needs.
- for those cases where you do have a suspect, do you really want to edit the source code to insert that `browser` line? What about just saying `breakon(xxx)`, where `xxx` is the name of your function, at the Julia prompt?

---

<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:** [August 23, 2018, 3:17pm UTC](https://discourse.julialang.org/t/debugging-like-rs-browser-possible/13925/5 "2018-08-23T15:17:07Z")

</div>

In my cases (I’m not the OP), I know the culprit in advance most of the time (it’s the function I’m currently working on). Regarding your second point. Both would be useful 😁. If I had to choose though I’d go with `breakon`.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [August 23, 2018, 3:21pm UTC](https://discourse.julialang.org/t/debugging-like-rs-browser-possible/13925/6 "2018-08-23T15:21:10Z")

</div>

> [@tim.holy](#):
>
> `breakon(xxx)` ,

`breakon(error)`, like matlab’s `dbstop if error`?

---

<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:** [August 23, 2018, 3:25pm UTC](https://discourse.julialang.org/t/debugging-like-rs-browser-possible/13925/7 "2018-08-23T15:25:28Z")

</div>

> `breakon(error)` , like matlab’s `dbstop if error` ?

Rebugger already has that, it’s just `Meta-s`.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 23, 2018, 3:38pm UTC](https://discourse.julialang.org/t/debugging-like-rs-browser-possible/13925/8 "2018-08-23T15:38:21Z")

</div>

> [@tim.holy](#):
>
> how often do you know in advance which method is problematic? Most often I start at the top and work my way into the call chain, discovering as I go where the problem lies. Hence “stepin” functionality typically covers my needs.

Sometimes, it’s in the 8500th iteration of a loop. Or maybe somewhere after the loop. Then stepping becomes tedious.

Definitely prefer `breakon(method, linenumber)`.

---

<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:** [August 23, 2018, 3:58pm UTC](https://discourse.julialang.org/t/debugging-like-rs-browser-possible/13925/9 "2018-08-23T15:58:00Z")

</div>

> Sometimes, it’s in the 8500th iteration of a loop. Or maybe somewhere after the loop. Then stepping becomes tedious.

Not at all (correction: I agree stepping is tedious in traditional debuggers, but less so in Rebugger); step to the method and then edit the line that Rebugger provides you to

```julia
rebug> @eval SomeModule let (vars...,) = getstored(uuid) begin
           # some code that Rebugger put here for you
           for iter = 1:10000
               # some other code Rebugger put here
               # Now you add this:
               if iter == 8500
                   @show all_the_vars...
               end
               # yet more code Rebugger put here
           end
...

```

This is part of why I wonder if breakpoints are that important: since you interact with Rebugger through the normal REPL, a breakpoint isn’t some special “thing”, its just you tweaking the code to do whatever you want. Since you can edit code while effectively _in debug mode_ you can do anything you want.

If it’s in the 8500th call to the method from some external method, then you can’t entirely do it at the REPL; you might have to add a `if condition throw(SomeError()) end` and then `Meta-s` on the caller. After that, you’ll have access to the direct inputs that caused the error to be thrown.

---

<div class="post-metadata">

**Author:** ![tshort](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tshort/32/43_2.png) [@tshort](https://discourse.julialang.org/u/tshort)\
**Post date:** [August 23, 2018, 3:58pm UTC](https://discourse.julialang.org/t/debugging-like-rs-browser-possible/13925/10 "2018-08-23T15:58:42Z")

</div>

For the question of usage like R’s `browser()`, you can use `error()` in the code and get to it using `Meta-s` (`Alt-s`) when calling the top-level function.
