Skip to content

Commit 4cc10b1

Browse files
authored
An export-worthy API for break_on_error (#140)
* An export-worthy API for break_on_error Some day we hope to have more possibilities for triggering automatic breakpoints (issue #102). For now, let's at least establish an extensible user-level API.
1 parent 841c826 commit 4cc10b1

File tree

5 files changed

+43
-6
lines changed

5 files changed

+43
-6
lines changed

docs/src/dev_reference.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ breakpoint
6060
enable
6161
disable
6262
remove
63+
break_on
64+
break_off
6365
JuliaInterpreter.dummy_breakpoint
6466
```
6567

src/JuliaInterpreter.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ using CodeTracking
1414

1515
export @interpret, Compiled, Frame, root, leaf,
1616
BreakpointRef, breakpoint, @breakpoint, breakpoints, enable, disable, remove,
17-
debug_command, @bp
17+
debug_command, @bp, break_on, break_off
1818

1919
module CompiledCalls
2020
# This module is for handling intrinsics that must be compiled (llvmcall)

src/breakpoints.jl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,40 @@ function remove()
129129
return nothing
130130
end
131131

132+
"""
133+
break_on(states...)
134+
135+
Turn on automatic breakpoints when any of the conditions described in `states` occurs.
136+
The supported states are:
137+
138+
- `:error`: trigger a breakpoint any time an uncaught exception is thrown
139+
"""
140+
function break_on(states::Vararg{Symbol})
141+
for state in states
142+
if state == :error
143+
break_on_error[] = true
144+
else
145+
throw(ArgumentError(string("unsupported state :", state)))
146+
end
147+
end
148+
end
149+
150+
"""
151+
break_off(states...)
152+
153+
Turn off automatic breakpoints when any of the conditions described in `states` occurs.
154+
See [`break_on`](@ref) for a description of valid states.
155+
"""
156+
function break_off(states::Vararg{Symbol})
157+
for state in states
158+
if state == :error
159+
break_on_error[] = false
160+
else
161+
throw(ArgumentError(string("unsupported state :", state)))
162+
end
163+
end
164+
end
165+
132166
"""
133167
breakpoint(f, sig)
134168
breakpoint(f, sig, line)

test/breakpoints.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ end
130130

131131
# break on error
132132
try
133-
JuliaInterpreter.break_on_error[] = true
133+
@test_throws ArgumentError("unsupported state :missing") break_on(:missing)
134+
break_on(:error)
134135

135136
inner(x) = error("oops")
136137
outer() = inner(1)
@@ -158,7 +159,7 @@ end
158159
@test v isa ErrorException
159160
@test stacklength(frame) == 1
160161
finally
161-
JuliaInterpreter.break_on_error[] = false
162+
break_off(:error)
162163
end
163164

164165
# Breakpoint display

test/debug.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ struct B{T} end
247247
# Don't break on caught exceptions
248248
err_caught = Any[nothing]
249249
function f_exc_outer()
250-
try
250+
try
251251
f_exc_inner()
252252
catch err;
253253
err_caught[1] = err
@@ -282,7 +282,7 @@ struct B{T} end
282282

283283
# Break on error
284284
try
285-
JuliaInterpreter.break_on_error[] = true
285+
break_on(:error)
286286
fr = JuliaInterpreter.enter_call(f_outer)
287287
fr, pc = debug_command(JuliaInterpreter.finish_and_return!, fr, :finish)
288288
@test fr.framecode.scope.name == :error
@@ -293,7 +293,7 @@ struct B{T} end
293293
@test isa(pc, BreakpointRef)
294294
@test pc.err isa UndefVarError
295295
finally
296-
JuliaInterpreter.break_on_error[] = false
296+
break_off(:error)
297297
end
298298

299299
@testset "breakpoints" begin

0 commit comments

Comments
 (0)