Skip to content

Commit 3ec0432

Browse files
authored
allow breaking on constructors (#312)
1 parent 4e4da53 commit 3ec0432

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

src/breakpoints.jl

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ function get_function_in_module_or_Main(m::Module, f::Symbol)
2222
m = Main
2323
end
2424
f = getfield(m, f)
25-
return f isa Function ? f : nothing
25+
return (f isa Function || f isa Type) ? f : nothing
2626
end
2727

2828
function add_breakpoint!(state::DebuggerState, cmd::AbstractString)
2929
cmd = strip(cmd)
30-
bp_error(err) = (printstyled(stderr, err, "\n", color = Base.error_color()); false)
31-
undef_func(m, f) = bp_error("$f in " * (m !== Main ? "$m or" : "") * " Main is either not a function or not defined")
30+
bp_error() = (@error "Empty breakpoint expression"; false)
31+
bp_error(err) = (@error err; false)
32+
undef_func(m, expr) = bp_error("Expression $(expr) in " * (m !== Main ? "$m or" : "") * " Main did not evaluate to a function or type")
3233
isempty(cmd) && return bp_error()
3334
frame = active_frame(state)
3435

@@ -71,36 +72,28 @@ function add_breakpoint!(state::DebuggerState, cmd::AbstractString)
7172
m = moduleof(frame)
7273
has_args = false
7374
if location_expr isa Symbol
74-
fsym = location_expr
75-
f = get_function_in_module_or_Main(m, fsym)
75+
f = get_function_in_module_or_Main(m, location_expr)
7676
else
77-
# check that the expr is a chain of getproperty calls
78-
expr = fsym = location_expr
77+
expr = location_expr
7978
if isexpr(expr, :call)
8079
has_args = true
8180
expr = expr.args[1]
8281
end
83-
while expr isa Expr
84-
if expr.head == Symbol(".") && length(expr.args) == 2 && expr.args[2] isa QuoteNode
85-
expr = expr.args[1]
86-
else
87-
@goto not_a_function
88-
end
89-
end
9082
for m in (moduleof(frame), Main)
9183
try
92-
f_eval = Base.eval(m, has_args ? location_expr.args[1] : location_expr)
93-
if f_eval isa Function
84+
f_eval = Base.eval(m, expr)
85+
if f_eval isa Function || f_eval isa Type
9486
f = f_eval
9587
break
9688
end
97-
catch
89+
catch e
90+
bp_error("error when evaluating expression $(expr) in module $m")
9891
end
9992
end
10093
end
101-
f === nothing && return undef_func(m, fsym)
94+
f === nothing && return undef_func(m, location_expr)
10295
if !has_args
103-
@info "added breakpoint for function $f" * (line === nothing ? "" : ":$line")
96+
@info string("added breakpoint for ", f isa Function ? "function" : "type", " $f", (line === nothing ? "" : ":$line"))
10497
breakpoint(f, line, cond_expr)
10598
return true
10699
end

test/misc.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@ execute_command(state, Val{:bp}(), """bp disable""")
190190
@test bp2.enabled[] == false
191191
JuliaInterpreter.remove()
192192

193+
frame = Debugger.@make_frame 1.0:2.0:3.0
194+
state = dummy_state(frame)
195+
execute_command(state, Val{:bp}(), """bp add StepRangeLen""")
196+
execute_command(state, Val{:c}(), "c")
197+
@test state.frame !== nothing
198+
JuliaInterpreter.remove()
193199

194200
@info "BEGIN ERRORS -------------------------------------"
195201
execute_command(state, Val{:bp}(), """bp add Base""")

0 commit comments

Comments
 (0)