Placing breakpoints dynamically in code and debugging with vs-code

I’m new to julia and i was wondering how to place a breakpoint dynamically through code, and debugging with the vs-code extension. For example, on Windows, with the Visual Studio IDE and C++, we can use something like __debugbreak() to place breakpoints through our code, but how to do that in julia using vs-code?

Perhaps, we can create a macro to do this:

macro breakpoint(condition)
        if $condition
            BREAKPOINT # what this should be?

And then using that macro and debugging with vs-code. How can we to do that?

Sorry for my english or if something that i wrote doesn’t see to make sense, i’m not a native english speaker.

That macro already exists and is called @bp. You’ll need to access it as e.g. Main.VSCodeServer.JuliaInterpreter.@bp at the moment, which is a bit verbose.

Note that VSCode also supports conditional breakpoints out of the box; just right click on a breakpoint and select Edit breakpoint to add a condition.


When i use Main.VSCodeServer.JuliaInterpreter.@bp in my code, and execute through the Run and Debug tab in vs-code, i get this error:

Warning: Some Julia code in the VS Code extension crashed with
│   e =
│    LoadError: UndefVarError: VSCodeServer not defined
│    in expression starting at c:\Users\jorge\dev\Omnix\src\Omnigl.jl:13
└ @ VSCodeDebugger c:\Users\jorge\.vscode\extensions\julialang.language-julia-1.1.40\scripts\error_handler.jl:5
ERROR: LoadError: UndefVarError: VSCodeServer not defined
 [1] lower
   @ .\meta.jl:165 [inlined]
 [2] VSCodeDebugger.JuliaInterpreter.Frame(mod::Module, ex::Expr)
   @ VSCodeDebugger.JuliaInterpreter c:\Users\jorge\.vscode\extensions\julialang.language-julia-1.1.40\scripts\packages\JuliaInterpreter\src\types.jl:245
 [3] get_next_top_level_frame(state::VSCodeDebugger.DebugAdapter.DebuggerState)
   @ VSCodeDebugger.DebugAdapter c:\Users\jorge\.vscode\extensions\julialang.language-julia-1.1.40\scripts\packages\DebugAdapter\src\debugger_core.jl:59 
 [4] our_debug_command(cmd::Symbol, state::VSCodeDebugger.DebugAdapter.DebuggerState)
   @ VSCodeDebugger.DebugAdapter c:\Users\jorge\.vscode\extensions\julialang.language-julia-1.1.40\scripts\packages\DebugAdapter\src\debugger_core.jl:81 
 [5] startdebug(socket::Base.PipeEndpoint, error_handler::VSCodeDebugger.var"#3#4"{Tuple{String, String}})
   @ VSCodeDebugger.DebugAdapter c:\Users\jorge\.vscode\extensions\julialang.language-julia-1.1.40\scripts\packages\DebugAdapter\src\packagedef.jl:104   
 [6] startdebugger()
   @ VSCodeDebugger c:\Users\jorge\.vscode\extensions\julialang.language-julia-1.1.40\scripts\packages\VSCodeDebugger\src\VSCodeDebugger.jl:38
 [7] top-level scope
   @ c:\Users\jorge\.vscode\extensions\julialang.language-julia-1.1.40\scripts\debugger\run_debugger.jl:9
in expression starting at c:\Users\jorge\dev\Omnix\src\Omnigl.jl:13

Julia debuggee finished. Press ENTER to close this terminal.

Ah, yeah. That’ll only work when debugging in the REPL. In a separate debugging session you’ll need VSCodeDebugger.JuliaInterpreter.@bp instead.

var"@bp" = (isdefined(Main, :VSCodeServer) ? Main.VSCodeServer : Main.VSCodeDebugger).JuliaInterpreter.var"@bp"

works in both cases. I’d still urge you to use the normal graphical breakpoints though – are you missing some functionality or why do you want to put the breakpoints directly into your code?


Thanks, that solves for what i want.

I have a extern C/C++ API with binds for Julia, and every function of that API may be susceptible to errors so a way to debugging theses functions programmatically (if they failed for some reason) will be better than manually placing breakpoints in every function call of that API.

1 Like