JuliaDebug / Debugger.jl

Julia debugger
MIT License
469 stars 43 forks source link

Debugger Skips FileIO do file loop #174

Open mattcbro opened 5 years ago

mattcbro commented 5 years ago

The debugger won't step through do file loops, commonly used in file IO syntax. This has been true actually since the days of ASTInterpreter2.jl . I provide a simple example that creates an HDF5 file, though I think the problem is more general than this. In some of my more complicated IO functions, JuliaInterpreter will just hang within said loop.

using FileIO
using HDF5
using Debugger

function genout(outfile; N=10)
println("Generate HDF5 file: $(outfile) N=$(N)")
h5open(outfile, "w") do file
    xdat = g_create(file, "data")
    for q = 1:N
        qstr = string(q)
        xdat[qstr] = q
    end
end # do file

end

@enter genout("out.hd5", N=5)

If you step through the code using the n command it will simply skip past the do file loop.

fredrikekre commented 5 years ago

do block is just syntax for creating an anonymous function so you need to step into that call with e.g. s:

julia> function f(x)
           y = map(x) do z
               z^2
           end
           return y
       end;

julia> @enter f([1,2,3])
In f(x) at REPL[7]:2
 1  function f(x)
>2      y = map(x) do z
 3          z^2
 4      end
 5      return y
 6  end;

About to run: (map)(getfield(Main, Symbol("##7#8"))(), [1, 2, 3])

In this example the do block that we want to step into is the function getfield(Main, Symbol("##7#8"))() so with s we can first step into map and then finally the anonymous function will be called. Since there can be a large number of steps until the function is called it can be nice to set a breakpoint like:

julia> function f(x)
           y = map(x) do z
               @bp
               z^2
           end
           return y
       end;

julia> @run f([1,2,3])
Hit breakpoint:
In #17(z) at REPL[10]:3
 1  function f(x)
 2      y = map(x) do z
●3          @bp
>4          z^2
 5      end
 6      return y
 7  end;