-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathbreakpoints.jl
362 lines (311 loc) · 11.2 KB
/
breakpoints.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
const _breakpoints = AbstractBreakpoint[]
"""
breakpoints()::Vector{AbstractBreakpoint}
Return an array with all breakpoints.
"""
breakpoints() = _breakpoints
function add_to_existing_framecodes(bp::AbstractBreakpoint)
for framecode in values(framedict)
add_breakpoint_if_match!(framecode, bp)
end
end
function add_breakpoint_if_match!(framecode::FrameCode, bp::AbstractBreakpoint)
if framecode_matches_breakpoint(framecode, bp)
stmtidx = bp.line === 0 ? 1 : statementnumber(framecode, bp.line)
breakpoint!(framecode, stmtidx, bp.condition, bp.enabled[])
push!(bp.instances, BreakpointRef(framecode, stmtidx))
end
end
function framecode_matches_breakpoint(framecode::FrameCode, bp::BreakpointSignature)
function extract_function_from_method(m::Method)
sig = Base.unwrap_unionall(m.sig)
ft0 = sig.parameters[1]
ft = Base.unwrap_unionall(ft0)
if ft <: Function && isa(ft, DataType) && isdefined(ft, :instance)
return ft.instance
elseif isa(ft, DataType) && ft.name === Type.body.name
return ft.parameters[1]
else
return ft
end
end
framecode.scope isa Method || return false
meth = framecode.scope
bp.f isa Method && return meth === bp.f
bp.f === extract_function_from_method(meth) || return false
bp.sig === nothing && return true
return bp.sig <: meth.sig
end
"""
breakpoint(f, [sig], [line], [condition])
Add a breakpoint to `f` with the specified argument types `sig`.¨
If `sig` is not given, the breakpoint will apply to all methods of `f`.
If `f` is a method, the breakpoint will only apply to that method.
Optionally specify an absolute line number `line` in the source file; the default
is to break upon entry at the first line of the body.
Without `condition`, the breakpoint will be triggered every time it is encountered;
the second only if `condition` evaluates to `true`.
`condition` should be written in terms of the arguments and local variables of `f`.
# Example
```julia
function radius2(x, y)
return x^2 + y^2
end
breakpoint(radius2, Tuple{Int,Int}, :(y > x))
```
"""
function breakpoint(f::Union{Method, Function}, sig=nothing, line::Integer=0, condition::Condition=nothing)
sig !== nothing && (sig = Base.to_tuple_type(sig))
bp = BreakpointSignature(f, sig, line, condition, Ref(true), BreakpointRef[])
add_to_existing_framecodes(bp)
idx = findfirst(bp2 -> same_location(bp, bp2), _breakpoints)
idx === nothing ? push!(_breakpoints, bp) : (_breakpoints[idx] = bp)
return bp
end
breakpoint(f::Union{Method, Function}, sig, condition::Condition) = breakpoint(f, sig, 0, condition)
breakpoint(f::Union{Method, Function}, line::Integer, condition::Condition=nothing) = breakpoint(f, nothing, line, condition)
breakpoint(f::Union{Method, Function}, condition::Condition) = breakpoint(f, nothing, 0, condition)
"""
breakpoint(file, line, [condition])
Set a breakpoint in `file` at `line`. The argument `file` can be a filename, a partial path or absolute path.
For example, `file = foo.jl` will match against all files with the name `foo.jl`,
`file = src/foo.jl` will match against all paths containing `src/foo.jl`, e.g. both `Foo/src/foo.jl` and `Bar/src/foo.jl`.
Absolute paths only matches against the file with that exact absolute path.
"""
function breakpoint(file::AbstractString, line::Integer, condition::Condition=nothing)
file = normpath(file)
apath = CodeTracking.maybe_fix_path(abspath(file))
ispath(apath) && (apath = realpath(apath))
bp = BreakpointFileLocation(file, apath, line, condition, Ref(true), BreakpointRef[])
add_to_existing_framecodes(bp)
idx = findfirst(bp2 -> same_location(bp, bp2), _breakpoints)
idx === nothing ? push!(_breakpoints, bp) : (_breakpoints[idx] = bp)
return bp
end
function framecode_matches_breakpoint(framecode::FrameCode, bp::BreakpointFileLocation)
framecode.scope isa Method || return false
meth = framecode.scope
methpath = CodeTracking.maybe_fix_path(String(meth.file))
ispath(methpath) && (methpath = realpath(methpath))
if bp.abspath == methpath || endswith(methpath, bp.path)
return method_contains_line(meth, bp.line)
else
return false
end
end
function shouldbreak(frame::Frame, pc::Int)
bps = frame.framecode.breakpoints
isassigned(bps, pc) || return false
bp = bps[pc]
bp.isactive || return false
return Base.invokelatest(bp.condition, frame)::Bool
end
function prepare_slotfunction(framecode::FrameCode, body::Union{Symbol,Expr})
ismeth = framecode.scope isa Method
uslotnames = Set{Symbol}()
slotnames = Symbol[]
for name in framecode.src.slotnames
if name ∉ uslotnames
push!(slotnames, name)
push!(uslotnames, name)
end
end
framename, dataname = gensym("frame"), gensym("data")
assignments = Expr[:($dataname = $framename.framedata)]
default = Unassigned()
for i = 1:length(slotnames)
slotname = framecode.src.slotnames[i]
qslotname = QuoteNode(slotname)
list = framecode.slotnamelists[slotname]
if length(list) == 1
maxexpr = :($dataname.last_reference[$(list[1])] > 0 ? $(list[1]) : 0)
else
maxcounter, maxidx = gensym("maxcounter"), gensym("maxidx")
maxexpr = quote
begin
$maxcounter, $maxidx = 0, 0
for l in $list
counter = $dataname.last_reference[l]
if counter > $maxcounter
$maxcounter, $maxidx = counter, l
end
end
$maxidx
end
end
end
maxexsym = gensym("slotid")
push!(assignments, :($maxexsym = $maxexpr))
push!(assignments, :($slotname = $maxexsym > 0 ? something($dataname.locals[$maxexsym]) : $default))
end
if ismeth
syms = sparam_syms(framecode.scope)
for i = 1:length(syms)
push!(assignments, Expr(:(=), syms[i], :($dataname.sparams[$i])))
end
end
funcname = ismeth ? gensym("slotfunction") : gensym(Symbol(framecode.scope.name, "_slotfunction"))
return Expr(:function, Expr(:call, funcname, framename), Expr(:block, assignments..., body))
end
_unpack(condition) = isa(condition, Expr) ? (Main, condition) : condition
## The fundamental implementations of breakpoint-setting
function breakpoint!(framecode::FrameCode, pc, condition::Condition=nothing, enabled=true)
stmtidx = pc
if condition === nothing
framecode.breakpoints[stmtidx] = BreakpointState(enabled)
else
mod, cond = _unpack(condition)
fex = prepare_slotfunction(framecode, cond)
framecode.breakpoints[stmtidx] = BreakpointState(enabled, Core.eval(mod, fex))
end
end
breakpoint!(frame::Frame, pc=frame.pc, condition::Condition=nothing) =
breakpoint!(frame.framecode, pc, condition)
update_states!(bp::AbstractBreakpoint) = foreach(bpref -> update_state!(bpref, bp.enabled[]), bp.instances)
update_state!(bp::BreakpointRef, v::Bool) = bp[] = v
"""
enable(bp::AbstractBreakpoint)
Enable breakpoint `bp`.
"""
enable(bp::AbstractBreakpoint) = (bp.enabled[] = true; update_states!(bp))
enable(bp::BreakpointRef) = bp[] = true
"""
disable(bp::AbstractBreakpoint)
Disable breakpoint `bp`. Disabled breakpoints can be re-enabled with [`enable`](@ref).
"""
disable(bp::AbstractBreakpoint) = (bp.enabled[] = false; update_states!(bp))
disable(bp::BreakpointRef) = bp[] = false
"""
remove(bp::AbstractBreakpoint)
Remove (delete) breakpoint `bp`. Removed breakpoints cannot be re-enabled.
"""
function remove(bp::AbstractBreakpoint)
idx = findfirst(isequal(bp), _breakpoints)
idx === nothing || deleteat!(_breakpoints, idx)
foreach(remove, bp.instances)
end
function remove(bp::BreakpointRef)
bp.framecode.breakpoints[bp.stmtidx] = BreakpointState(false, falsecondition)
return nothing
end
"""
toggle(bp::AbstractBreakpoint)
Toggle breakpoint `bp`.
"""
toggle(bp::AbstractBreakpoint) = (bp.enabled[] = !bp.enabled[]; update_states!(bp))
function toggle(bp::BreakpointRef)
state = bp[]
bp.framecode.breakpoints[bp.stmtidx] = BreakpointState(!state.isactive, state.condition)
end
"""
enable()
Enable all breakpoints.
"""
enable() = foreach(enable, _breakpoints)
"""
disable()
Disable all breakpoints.
"""
disable() = foreach(disable, _breakpoints)
"""
remove()
Remove all breakpoints.
"""
function remove()
for bp in _breakpoints
foreach(remove, bp.instances)
end
empty!(_breakpoints)
end
"""
break_on(states...)
Turn on automatic breakpoints when any of the conditions described in `states` occurs.
The supported states are:
- `:error`: trigger a breakpoint any time an uncaught exception is thrown
- `:throw` : trigger a breakpoint any time a throw is executed (even if it will eventually be caught)
"""
function break_on(states::Vararg{Symbol})
for state in states
if state == :error
break_on_error[] = true
elseif state == :throw
break_on_throw[] = true
else
throw(ArgumentError(string("unsupported state :", state)))
end
end
end
"""
break_off(states...)
Turn off automatic breakpoints when any of the conditions described in `states` occurs.
See [`break_on`](@ref) for a description of valid states.
"""
function break_off(states::Vararg{Symbol})
for state in states
if state == :error
break_on_error[] = false
elseif state == :throw
break_on_throw[] = false
else
throw(ArgumentError(string("unsupported state :", state)))
end
end
end
"""
@breakpoint f(args...) condition=nothing
@breakpoint f(args...) line condition=nothing
Break upon entry, or at the specified line number, in the method called by `f(args...)`.
Optionally supply a condition expressed in terms of the arguments and internal variables
of the method.
If `line` is supplied, it must be a literal integer.
# Example
Suppose a method `mysum` is defined as follows, where the numbers to the left are the line
number in the file:
```
12 function mysum(A)
13 s = zero(eltype(A))
14 for a in A
15 s += a
16 end
17 return s
18 end
```
Then
```
@breakpoint mysum(A) 15 s>10
```
would cause execution of the loop to break whenever `s>10`.
"""
macro breakpoint(call_expr, args...)
whichexpr = InteractiveUtils.gen_call_with_extracted_types(__module__, :which, call_expr)
haveline, line, condition = false, 0, nothing
while !isempty(args)
arg = first(args)
if isa(arg, Integer)
haveline, line = true, arg
else
condition = arg
end
args = Base.tail(args)
end
condexpr = condition === nothing ? nothing : Expr(:quote, condition)
if haveline
return quote
local method = $whichexpr
$breakpoint(method, $line, $condexpr)
end
else
return quote
local method = $whichexpr
$breakpoint(method, $condexpr)
end
end
end
const __BREAKPOINT_MARKER__ = nothing
"""
@bp
Insert a breakpoint at a location in the source code.
"""
macro bp()
return esc(:($(JuliaInterpreter).__BREAKPOINT_MARKER__))
end