1 ;;; Traps: stepping, breakpoints, and such.
3 ;; Copyright (C) 2010, 2012, 2013 Free Software Foundation, Inc.
5 ;;; This library is free software; you can redistribute it and/or
6 ;;; modify it under the terms of the GNU Lesser General Public
7 ;;; License as published by the Free Software Foundation; either
8 ;;; version 3 of the License, or (at your option) any later version.
10 ;;; This library is distributed in the hope that it will be useful,
11 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;; Lesser General Public License for more details.
15 ;;; You should have received a copy of the GNU Lesser General Public
16 ;;; License along with this library; if not, write to the Free Software
17 ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 ;;; Guile's debugging capabilities come from the hooks that its VM
22 ;;; provides. For example, there is a hook that is fired when a function
23 ;;; is called, and even a hook that gets fired at every retired
26 ;;; But as the firing of these hooks is interleaved with the program
27 ;;; execution, if we want to debug a program, we have to write an
28 ;;; imperative program that mutates the state of these hooks, and to
29 ;;; dispatch the hooks to a more semantic context.
31 ;;; For example if we have placed a breakpoint at foo.scm:38, and
32 ;;; determined that that location maps to the 18th instruction in
33 ;;; procedure `bar', then we will need per-instruction hooks within
34 ;;; `bar' -- but when running other procedures, we can have the
35 ;;; per-instruction hooks off.
37 ;;; Our approach is to define "traps". The behavior of a trap is
38 ;;; specified when the trap is created. After creation, traps expose a
39 ;;; limited, uniform interface: they are either on or off.
41 ;;; To take our foo.scm:38 example again, we can define a trap that
42 ;;; calls a function when control transfers to that source line --
43 ;;; trap-at-source-location below. Calling the trap-at-source-location
44 ;;; function adds to the VM hooks in such at way that it can do its job.
45 ;;; The result of calling the function is a "disable-hook" closure that,
46 ;;; when called, will turn off that trap.
48 ;;; The result of calling the "disable-hook" closure, in turn, is an
49 ;;; "enable-hook" closure, which when called turns the hook back on, and
50 ;;; returns a "disable-hook" closure.
52 ;;; It's a little confusing. The summary is, call these functions to add
53 ;;; a trap; and call their return value to disable the trap.
57 (define-module (system vm traps)
58 #:use-module (system base pmatch)
59 #:use-module (system vm vm)
60 #:use-module (system vm debug)
61 #:use-module (system vm frame)
62 #:use-module (system vm program)
63 #:use-module (system vm instruction)
64 #:use-module (system xref)
65 #:use-module (rnrs bytevectors)
66 #:export (trap-at-procedure-call
68 trap-instructions-in-procedure
69 trap-at-procedure-ip-in-range
70 trap-at-source-location
72 trap-in-dynamic-extent
73 trap-calls-in-dynamic-extent
74 trap-instructions-in-dynamic-extent
75 trap-calls-to-procedure
76 trap-matching-instructions))
78 (define-syntax arg-check
80 ((_ arg predicate? message)
81 (if (not (predicate? arg))
82 (error "bad argument ~a: ~a" 'arg message)))
84 (if (not (predicate? arg))
85 (error "bad argument ~a: expected ~a" 'arg 'predicate?)))))
87 (define (new-disabled-trap vm enable disable)
89 (define-syntax disabled?
91 (disabled? (not enabled?))
92 ((set! disabled? val) (set! enabled? (not val)))))
94 (define* (enable-trap #:optional frame)
95 (if enabled? (error "trap already enabled"))
100 (define* (disable-trap #:optional frame)
101 (if disabled? (error "trap already disabled"))
108 (define (new-enabled-trap vm frame enable disable)
109 ((new-disabled-trap vm enable disable) frame))
111 ;; Returns an absolute IP.
112 (define (program-last-ip prog)
113 (let ((pdi (find-program-debug-info (program-code prog))))
114 (and pdi (program-debug-info-size pdi))))
116 (define (frame-matcher proc match-code?)
119 (let ((start (program-code proc))
120 (end (program-last-ip proc)))
122 (let ((ip (frame-instruction-pointer frame)))
123 (and (<= start ip) (< ip end)))))
126 (eq? (frame-procedure frame) proc))))
128 ;; A basic trap, fires when a procedure is called.
130 (define* (trap-at-procedure-call proc handler #:key (vm (the-vm))
132 (our-frame? (frame-matcher proc closure?)))
133 (arg-check proc procedure?)
134 (arg-check handler procedure?)
136 (define (apply-hook frame)
137 (if (our-frame? frame)
143 (add-hook! (vm-apply-hook vm) apply-hook))
145 (remove-hook! (vm-apply-hook vm) apply-hook)))))
147 ;; A more complicated trap, traps when control enters a procedure.
149 ;; Control can enter a procedure via:
150 ;; * A procedure call.
151 ;; * A return to a procedure's frame on the stack.
152 ;; * A continuation returning directly to an application of this
155 ;; Control can leave a procedure via:
156 ;; * A normal return from the procedure.
157 ;; * An application of another procedure.
158 ;; * An invocation of a continuation.
161 (define* (trap-in-procedure proc enter-handler exit-handler
162 #:key current-frame (vm (the-vm))
164 (our-frame? (frame-matcher proc closure?)))
165 (arg-check proc procedure?)
166 (arg-check enter-handler procedure?)
167 (arg-check exit-handler procedure?)
169 (define (enter-proc frame)
171 (warn "already in proc" frame)
173 (enter-handler frame)
174 (set! in-proc? #t))))
176 (define (exit-proc frame)
181 (warn "not in proc" frame)))
183 (define (apply-hook frame)
186 (if (our-frame? frame)
189 (define (push-cont-hook frame)
193 (define (pop-cont-hook frame . values)
196 (if (our-frame? (frame-previous frame))
197 (enter-proc (frame-previous frame))))
199 (define (abort-hook frame . values)
202 (if (our-frame? frame)
205 (define (restore-hook frame)
208 (if (our-frame? frame)
214 (add-hook! (vm-apply-hook vm) apply-hook)
215 (add-hook! (vm-push-continuation-hook vm) push-cont-hook)
216 (add-hook! (vm-pop-continuation-hook vm) pop-cont-hook)
217 (add-hook! (vm-abort-continuation-hook vm) abort-hook)
218 (add-hook! (vm-restore-continuation-hook vm) restore-hook)
219 (if (and frame (our-frame? frame))
224 (remove-hook! (vm-apply-hook vm) apply-hook)
225 (remove-hook! (vm-push-continuation-hook vm) push-cont-hook)
226 (remove-hook! (vm-pop-continuation-hook vm) pop-cont-hook)
227 (remove-hook! (vm-abort-continuation-hook vm) abort-hook)
228 (remove-hook! (vm-restore-continuation-hook vm) restore-hook)))))
230 ;; Building on trap-in-procedure, we have trap-instructions-in-procedure
232 (define* (trap-instructions-in-procedure proc next-handler exit-handler
233 #:key current-frame (vm (the-vm))
236 (frame-matcher proc closure?)))
237 (arg-check proc procedure?)
238 (arg-check next-handler procedure?)
239 (arg-check exit-handler procedure?)
241 (define (next-hook frame)
242 (if (our-frame? frame)
243 (next-handler frame)))
245 (define (enter frame)
246 (add-hook! (vm-next-hook vm) next-hook)
247 (if frame (next-hook frame)))
251 (remove-hook! (vm-next-hook vm) next-hook))
253 (trap-in-procedure proc enter exit
254 #:current-frame current-frame #:vm vm
255 #:our-frame? our-frame?)))
257 (define (non-negative-integer? x)
258 (and (number? x) (integer? x) (exact? x) (not (negative? x))))
260 (define (positive-integer? x)
261 (and (number? x) (integer? x) (exact? x) (positive? x)))
267 (non-negative-integer? (car x))
268 (non-negative-integer? (cdr x))))
271 (define (in-range? range i)
272 (or-map (lambda (bounds)
273 (and (<= (car bounds) i)
277 ;; Building on trap-instructions-in-procedure, we have
278 ;; trap-at-procedure-ip-in-range.
280 (define* (trap-at-procedure-ip-in-range proc range handler
281 #:key current-frame (vm (the-vm))
284 (frame-matcher proc closure?)))
285 (arg-check proc procedure?)
286 (arg-check range range?)
287 (arg-check handler procedure?)
288 (let ((fp-stack '()))
289 (define (cull-frames! fp)
290 (let lp ((frames fp-stack))
291 (if (and (pair? frames) (< (car frames) fp))
293 (set! fp-stack frames))))
295 (define (next-handler frame)
296 (let ((fp (frame-address frame))
297 (ip (frame-instruction-pointer frame)))
299 (let ((now-in-range? (in-range? range ip))
300 (was-in-range? (and (pair? fp-stack) (= (car fp-stack) fp))))
303 (if (not now-in-range?)
304 (set! fp-stack (cdr fp-stack))))
306 (set! fp-stack (cons fp fp-stack))
309 (define (exit-handler frame)
310 (if (and (pair? fp-stack)
311 (= (car fp-stack) (frame-address frame)))
312 (set! fp-stack (cdr fp-stack))))
314 (trap-instructions-in-procedure proc next-handler exit-handler
315 #:current-frame current-frame #:vm vm
316 #:our-frame? our-frame?)))
318 (define (program-sources-by-line proc file)
321 (let ((code (program-code proc)))
322 (let lp ((sources (program-sources proc))
326 (pmatch (car sources)
327 ((,start-ip ,start-file ,start-line . ,start-col)
328 (if (equal? start-file file)
330 (if (pair? (cdr sources))
331 (pmatch (cadr sources)
333 (cons (+ start-ip code)
335 (else (error "unexpected")))
336 (cons (+ start-ip code)
337 (program-last-ip proc)))
340 (else (error "unexpected"))))
345 (assv-set! alist (car pair)
347 (or (assv-ref alist (car pair))
350 (sort! alist (lambda (x y) (< (car x) (car y))))
354 (define (source->ip-range proc file line)
355 (or (or-map (lambda (line-and-ranges)
357 ((= (car line-and-ranges) line)
358 (cdr line-and-ranges))
359 ((> (car line-and-ranges) line)
360 (warn "no instructions found at" file ":" line
361 "; using line" (car line-and-ranges) "instead")
362 (cdr line-and-ranges))
364 (program-sources-by-line proc file))
366 (warn "no instructions found for" file ":" line)
369 (define (source-closures-or-procedures file line)
370 (let ((closures (source-closures file line)))
373 (values (source-procedures file line) #f))))
375 ;; Building on trap-on-instructions-in-procedure, we have
376 ;; trap-at-source-location. The parameter `user-line' is one-indexed, as
377 ;; a user counts lines, instead of zero-indexed, as Guile counts lines.
379 (define* (trap-at-source-location file user-line handler
380 #:key current-frame (vm (the-vm)))
381 (arg-check file string?)
382 (arg-check user-line positive-integer?)
383 (arg-check handler procedure?)
386 (lambda () (source-closures-or-procedures file (1- user-line)))
387 (lambda (procs closures?)
394 (let ((range (source->ip-range proc file (1- user-line))))
395 (trap-at-procedure-ip-in-range proc range handler
396 #:current-frame current-frame
398 #:closure? closures?)))
401 (error "No procedures found at ~a:~a." file user-line)))
403 (for-each (lambda (trap) (trap frame)) traps)
404 (set! traps #f)))))))
408 ;; On a different tack, now we're going to build up a set of traps that
409 ;; do useful things during the dynamic extent of a procedure's
410 ;; application. First, a trap for when a frame returns.
412 (define* (trap-frame-finish frame return-handler abort-handler
414 (arg-check frame frame?)
415 (arg-check return-handler procedure?)
416 (arg-check abort-handler procedure?)
417 (let ((fp (frame-address frame)))
418 (define (pop-cont-hook frame . values)
419 (if (and fp (eq? (frame-address frame) fp))
422 (apply return-handler frame values))))
424 (define (abort-hook frame . values)
425 (if (and fp (< (frame-address frame) fp))
428 (apply abort-handler frame values))))
434 (error "return-or-abort traps may only be enabled once"))
435 (add-hook! (vm-pop-continuation-hook vm) pop-cont-hook)
436 (add-hook! (vm-abort-continuation-hook vm) abort-hook)
437 (add-hook! (vm-restore-continuation-hook vm) abort-hook))
440 (remove-hook! (vm-pop-continuation-hook vm) pop-cont-hook)
441 (remove-hook! (vm-abort-continuation-hook vm) abort-hook)
442 (remove-hook! (vm-restore-continuation-hook vm) abort-hook)))))
444 ;; A more traditional dynamic-wind trap. Perhaps this should not be
445 ;; based on the above trap-frame-finish?
447 (define* (trap-in-dynamic-extent proc enter-handler return-handler abort-handler
448 #:key current-frame (vm (the-vm))
450 (our-frame? (frame-matcher proc closure?)))
451 (arg-check proc procedure?)
452 (arg-check enter-handler procedure?)
453 (arg-check return-handler procedure?)
454 (arg-check abort-handler procedure?)
455 (let ((exit-trap #f))
456 (define (return-hook frame . values)
457 (exit-trap frame) ; disable the return/abort trap.
459 (return-handler frame))
461 (define (abort-hook frame . values)
462 (exit-trap frame) ; disable the return/abort trap.
464 (abort-handler frame))
466 (define (apply-hook frame)
467 (if (and (not exit-trap) (our-frame? frame))
469 (enter-handler frame)
471 (trap-frame-finish frame return-hook abort-hook
477 (add-hook! (vm-apply-hook vm) apply-hook))
482 (remove-hook! (vm-apply-hook vm) apply-hook)))))
484 ;; Trapping all procedure calls within a dynamic extent, recording the
485 ;; depth of the call stack relative to the original procedure.
487 (define* (trap-calls-in-dynamic-extent proc apply-handler return-handler
488 #:key current-frame (vm (the-vm))
491 (frame-matcher proc closure?)))
492 (arg-check proc procedure?)
493 (arg-check apply-handler procedure?)
494 (arg-check return-handler procedure?)
495 (let ((*call-depth* 0))
496 (define (trace-push frame)
497 (set! *call-depth* (1+ *call-depth*)))
499 (define (trace-pop frame . values)
500 (apply return-handler frame *call-depth* values)
501 (set! *call-depth* (1- *call-depth*)))
503 (define (trace-apply frame)
504 (apply-handler frame *call-depth*))
506 ;; FIXME: recalc depth on abort
508 (define (enter frame)
509 (add-hook! (vm-push-continuation-hook vm) trace-push)
510 (add-hook! (vm-pop-continuation-hook vm) trace-pop)
511 (add-hook! (vm-apply-hook vm) trace-apply))
513 (define (leave frame)
514 (remove-hook! (vm-push-continuation-hook vm) trace-push)
515 (remove-hook! (vm-pop-continuation-hook vm) trace-pop)
516 (remove-hook! (vm-apply-hook vm) trace-apply))
518 (define (return frame)
521 (define (abort frame)
524 (trap-in-dynamic-extent proc enter return abort
525 #:current-frame current-frame #:vm vm
526 #:our-frame? our-frame?)))
528 ;; Trapping all retired intructions within a dynamic extent.
530 (define* (trap-instructions-in-dynamic-extent proc next-handler
531 #:key current-frame (vm (the-vm))
534 (frame-matcher proc closure?)))
535 (arg-check proc procedure?)
536 (arg-check next-handler procedure?)
538 (define (trace-next frame)
539 (next-handler frame))
541 (define (enter frame)
542 (add-hook! (vm-next-hook vm) trace-next))
544 (define (leave frame)
545 (remove-hook! (vm-next-hook vm) trace-next))
547 (define (return frame)
550 (define (abort frame)
553 (trap-in-dynamic-extent proc enter return abort
554 #:current-frame current-frame #:vm vm
555 #:our-frame? our-frame?)))
557 ;; Traps calls and returns for a given procedure, keeping track of the call depth.
559 (define* (trap-calls-to-procedure proc apply-handler return-handler
561 (arg-check proc procedure?)
562 (arg-check apply-handler procedure?)
563 (arg-check return-handler procedure?)
564 (let ((pending-finish-traps '())
566 (define (apply-hook frame)
567 (let ((depth (length pending-finish-traps)))
569 (apply-handler frame depth)
571 (if (not (eq? (frame-address frame) last-fp))
572 (let ((finish-trap #f))
573 (define (frame-finished frame)
574 (finish-trap frame) ;; disables the trap.
575 (set! pending-finish-traps
576 (delq finish-trap pending-finish-traps))
577 (set! finish-trap #f))
579 (define (return-hook frame . values)
580 (frame-finished frame)
581 (apply return-handler frame depth values))
583 ;; FIXME: abort handler?
584 (define (abort-hook frame . values)
585 (frame-finished frame))
588 (trap-frame-finish frame return-hook abort-hook #:vm vm))
589 (set! pending-finish-traps
590 (cons finish-trap pending-finish-traps))))))
592 ;; The basic idea is that we install one trap that fires for calls,
593 ;; but that each call installs its own finish trap. Those finish
594 ;; traps remove themselves as their frames finish or abort.
596 ;; However since to the outside world we present the interface of
597 ;; just being one trap, disabling this calls-to-procedure trap
598 ;; should take care of disabling all of the pending finish traps. We
599 ;; keep track of pending traps through the pending-finish-traps
602 ;; So since we know that the trap-at-procedure will be enabled, and
603 ;; thus returning a disable closure, we make sure to wrap that
604 ;; closure in something that will disable pending finish traps.
605 (define (with-pending-finish-disablers trap)
606 (define (with-pending-finish-enablers trap)
607 (lambda* (#:optional frame)
608 (with-pending-finish-disablers (trap frame))))
610 (lambda* (#:optional frame)
611 (for-each (lambda (disable) (disable frame))
612 pending-finish-traps)
613 (set! pending-finish-traps '())
614 (with-pending-finish-enablers (trap frame))))
616 (with-pending-finish-disablers
617 (trap-at-procedure-call proc apply-hook #:vm vm))))
619 ;; Trap when the source location changes.
621 (define* (trap-matching-instructions frame-pred handler
623 (arg-check frame-pred procedure?)
624 (arg-check handler procedure?)
626 (define (next-hook frame)
627 (if (frame-pred frame)
633 (add-hook! (vm-next-hook vm) next-hook))
635 (remove-hook! (vm-next-hook vm) next-hook)))))