# Placing breakpoints dynamically in code and debugging with vs-code

**URL:** <https://discourse.julialang.org/t/placing-breakpoints-dynamically-in-code-and-debugging-with-vs-code/61270>\
**Category:** VS Code\
**Tags:** debugging, vscode\
**Created:** [May 16, 2021, 11:45pm UTC](https://discourse.julialang.org/t/placing-breakpoints-dynamically-in-code-and-debugging-with-vs-code/61270 "2021-05-16T23:45:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jorge\_Pereira](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jorge_pereira/32/25156_2.png) [@Jorge\_Pereira](https://discourse.julialang.org/u/Jorge_Pereira)\
**Post date:** [May 16, 2021, 11:45pm UTC](https://discourse.julialang.org/t/placing-breakpoints-dynamically-in-code-and-debugging-with-vs-code/61270/1 "2021-05-16T23:45:14Z")

</div>

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:

```julia
macro breakpoint(condition)
    quote
        if $condition
            BREAKPOINT # what this should be?
        end
    end
end

```

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.

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [May 17, 2021, 7:30am UTC](https://discourse.julialang.org/t/placing-breakpoints-dynamically-in-code-and-debugging-with-vs-code/61270/2 "2021-05-17T07:30:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jorge\_Pereira](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jorge_pereira/32/25156_2.png) [@Jorge\_Pereira](https://discourse.julialang.org/u/Jorge_Pereira)\
**Post date:** [May 17, 2021, 7:28pm UTC](https://discourse.julialang.org/t/placing-breakpoints-dynamically-in-code-and-debugging-with-vs-code/61270/3 "2021-05-17T19:28:27Z")

</div>

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:

```julia
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
Stacktrace:
 [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.

```

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [May 17, 2021, 10:13pm UTC](https://discourse.julialang.org/t/placing-breakpoints-dynamically-in-code-and-debugging-with-vs-code/61270/4 "2021-05-17T22:13:48Z")

</div>

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

```julia
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?

---

<div class="post-metadata">

**Author:** ![Jorge\_Pereira](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jorge_pereira/32/25156_2.png) [@Jorge\_Pereira](https://discourse.julialang.org/u/Jorge_Pereira)\
**Post date:** [May 17, 2021, 10:37pm UTC](https://discourse.julialang.org/t/placing-breakpoints-dynamically-in-code-and-debugging-with-vs-code/61270/5 "2021-05-17T22:37:48Z")

</div>

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.
