# Request: Julia debug mode

**URL:** https://discourse.julialang.org/t/request-julia-debug-mode/87832
**Category:** General Usage
**Created:** [September 26, 2022, 6:55pm UTC](https://discourse.julialang.org/t/request-julia-debug-mode/87832 "2022-09-26T18:55:07Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)
#### Post date: [September 26, 2022, 6:55pm UTC](https://discourse.julialang.org/t/request-julia-debug-mode/87832/1 "2022-09-26T18:55:07Z")

</div>

Can you sport were I got lots of allocations? I don’t need a debugger, right now, I found the bug, this is just a motivating example, and I think a debug mode would be better than a debugger.

```julia
function time_code2(A)
  code = encode_64_full((A[i], A[i+1], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
  for i in eachindex(A)[1:64:end]
    decode_64_full(code)
  end
end

julia> @time time_code2(A_orig2)
  0.002365 seconds (10.00 k allocations: 5.189 MiB)

```

The loop count is 10000 (like the allocations). In case you don’t see it, here’s the original (I was trying to simplify the loop to only time decode\_64\_full, moving encode\_64\_full out of it):

```julia
function time_code(A)
  s = 0; for i in eachindex(A)[1:64:end]
    decode_64_full(encode_64_full((A[i], A[i+1], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))) # A[i:i+63]
  end
end

julia> @time time_code(A_orig2)
  0.009769 seconds

```

Can you see it now:

```julia
function time_code3(A)
  code = encode_64_full((A[1], A[1+1], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
  s = 0; for i in eachindex(A)[1:64:end]
    decode_64_full(code)
  end
end

julia> @time time_code3(A_orig2)
  0.000026 seconds

```

I was staring at the code for a while.

The original title was “Julia without global variables”, then I changed my mind to “Julia without global variables, a debug mode”. I didn’t want to give away where the bug is.

---

<div class="post-metadata">

### Author: ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)
#### Post date: [September 26, 2022, 7:06pm UTC](https://discourse.julialang.org/t/request-julia-debug-mode/87832/2 "2022-09-26T19:06:04Z")

</div>

For these kinds of issues, I recommend JET:

```julia
julia> using JET

julia> function time_code2(A)
         code = encode_64_full((A[i], A[i+1], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
         for i in eachindex(A)[1:64:end]
           decode_64_full(code)
         end
       end
time_code2 (generic function with 1 method)

julia> @report_opt time_code2([1,2,3])
═════ 5 possible errors found ═════ ┌ @ REPL[2]:2 A[%1] │ runtime dispatch detected: (A::Vector{Int64})[%1::Any]::Any
└─────────────
┌ @ REPL[2]:2 %3 + 1
│ runtime dispatch detected: (%3::Any + 1)::Any
└─────────────
┌ @ REPL[2]:2 A[%4]
│ runtime dispatch detected: (A::Vector{Int64})[%4::Any]::Any
└─────────────
┌ @ REPL[2]:2 %7(%6)
│ runtime dispatch detected: %7::Any(%6::Tuple{Any, Any, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64})::Any
└─────────────
┌ @ REPL[2]:4 %69(%8)
│ runtime dispatch detected: %69::Any(%8::Any)::Any

```

Indeed, it would be nice if it was somehow easier to have JET-like functionality without having to type in every function call. It’s not obvious how to achieve that in a language so generic as Julia though,

---

<div class="post-metadata">

### Author: ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)
#### Post date: [September 26, 2022, 7:25pm UTC](https://discourse.julialang.org/t/request-julia-debug-mode/87832/3 "2022-09-26T19:25:16Z")

</div>

> [@jakobnissen](#):
>
> I recommend JET

Thanks, I knew of JET.jl, but I’m not sure it’s a substitute for a debug mode. I’m a bit surprised by “5 possible errors found” and none of them are very readable to me yet, nor pointing to the smoking gun. And for me (I would have given up, but useful(?) to know you get a restricted list without the dependent functions):

```julia
julia> @report_opt time_code2([1,2,3])
═════ 50 possible errors found ═════

```

In case it’s not obvious to people, then I should have known, `A[i]` is no longer in the scope of the loop, is `i` is now pointing to a global variable, would be a runtime error had it not been also defined. But I didn’t think of that line in relations to allocations.

So what I propose is a Julia mode without global variable _access_. I suppose having globals is ok for the REPL.

```julia
help?> global
[..]
  julia> function foo()
             global z = 6 # use the z variable defined outside 

```

Maybe this is actually a bug in Julia, or why else allow being explicit about a global being used?

Some but very few languages (V) have no global variables, and while e.g. C and C++ to have global variables you’re less likely to define them and allow the function to compile by accident than in a REPL language.

I have some other ideas this debug bode could do. Such as disable `@inbounds`, `@inline` and run with `-O0`. You could also call it a scripting mode. Just being able to disable globals though (or separately) seems like a good option, but doing it by default would be a breaking change for 2.0, unless of course that’s already intended?
