Easy debugging in Julia using Infiltrator.jl

Jan 17, 2022 · 282 words · 2 minute read

For some reason, I always find it hard to get used to the debugger whenever I switch to a new language. This is also true for Julia, which is where I’ve spent most of my coding time on recently. Usually, I just fall back to the classical “debugging technique” of just printing stuff out.

Since I am almost always working in the REPL, I usually want to inspect some variable and play with it. In Python, if I have some variable foo deep inside one of my classes, it is always easy to make it accesible as self.foo, and then inspect it in the REPL. However, Julia doesn’t do classes – everything is in functions. So it’s not obvious how to make variables inside a function accessible outside in the global scope. It would be nice to have a way of accessing local variables inside a function directly from the REPL.

Enter Infiltrator.jl. It’s a full debugger, and is actually very fast and easy to use. But what I wanted to highlight here is the macro @exfiltrate. This immediately solves the problem I raised above.

Say you have a function

function add_two(x)
    y = x+1
    z = y+1
    return z
end

and you wanted to have access to the variable y in the REPL. All you do (after using Infiltrator) is to add the macro @exfiltrate to it:

function add_two(x)
    y = x+1
    z = y+1
    @exfiltrate
    return z
end

Now, when you run this function, the @exfiltrate macro will store all the local variables in the global scope in the namespace safehouse. In the above example, the variable y will now be accesible from the REPL as safehouse.y. Isn’t that neat?