1 ;;; Traps: stepping, breakpoints, and such.
3 ;; Copyright (C) 2010, 2012 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 frame)
61 #:use-module (system vm program)
62 #:use-module (system vm objcode)
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 (define (frame-matcher proc match-objcode?)
114 (let ((frame-proc (frame-procedure frame)))
115 (or (eq? frame-proc proc)
116 (and (program? frame-proc)
117 (eq? (program-objcode frame-proc)
118 (program-objcode proc))))))
120 (eq? (frame-procedure frame) proc))))
122 ;; A basic trap, fires when a procedure is called.
124 (define* (trap-at-procedure-call proc handler #:key (vm (the-vm))
126 (our-frame? (frame-matcher proc closure?)))
127 (arg-check proc procedure?)
128 (arg-check handler procedure?)
130 (define (apply-hook frame)
131 (if (our-frame? frame)
137 (add-hook! (vm-apply-hook vm) apply-hook))
139 (remove-hook! (vm-apply-hook vm) apply-hook)))))
141 ;; A more complicated trap, traps when control enters a procedure.
143 ;; Control can enter a procedure via:
144 ;; * A procedure call.
145 ;; * A return to a procedure's frame on the stack.
146 ;; * A continuation returning directly to an application of this
149 ;; Control can leave a procedure via:
150 ;; * A normal return from the procedure.
151 ;; * An application of another procedure.
152 ;; * An invocation of a continuation.
155 (define* (trap-in-procedure proc enter-handler exit-handler
156 #:key current-frame (vm (the-vm))
158 (our-frame? (frame-matcher proc closure?)))
159 (arg-check proc procedure?)
160 (arg-check enter-handler procedure?)
161 (arg-check exit-handler procedure?)
163 (define (enter-proc frame)
165 (warn "already in proc" frame)
167 (enter-handler frame)
168 (set! in-proc? #t))))
170 (define (exit-proc frame)
175 (warn "not in proc" frame)))
177 (define (apply-hook frame)
180 (if (our-frame? frame)
183 (define (push-cont-hook frame)
187 (define (pop-cont-hook frame . values)
190 (if (our-frame? (frame-previous frame))
191 (enter-proc (frame-previous frame))))
193 (define (abort-hook frame . values)
196 (if (our-frame? frame)
199 (define (restore-hook frame)
202 (if (our-frame? frame)
208 (add-hook! (vm-apply-hook vm) apply-hook)
209 (add-hook! (vm-push-continuation-hook vm) push-cont-hook)
210 (add-hook! (vm-pop-continuation-hook vm) pop-cont-hook)
211 (add-hook! (vm-abort-continuation-hook vm) abort-hook)
212 (add-hook! (vm-restore-continuation-hook vm) restore-hook)
213 (if (and frame (our-frame? frame))
218 (remove-hook! (vm-apply-hook vm) apply-hook)
219 (remove-hook! (vm-push-continuation-hook vm) push-cont-hook)
220 (remove-hook! (vm-pop-continuation-hook vm) pop-cont-hook)
221 (remove-hook! (vm-abort-continuation-hook vm) abort-hook)
222 (remove-hook! (vm-restore-continuation-hook vm) restore-hook)))))
224 ;; Building on trap-in-procedure, we have trap-instructions-in-procedure
226 (define* (trap-instructions-in-procedure proc next-handler exit-handler
227 #:key current-frame (vm (the-vm))
230 (frame-matcher proc closure?)))
231 (arg-check proc procedure?)
232 (arg-check next-handler procedure?)
233 (arg-check exit-handler procedure?)
235 (define (next-hook frame)
236 (if (our-frame? frame)
237 (next-handler frame)))
239 (define (enter frame)
240 (add-hook! (vm-next-hook vm) next-hook)
241 (if frame (next-hook frame)))
245 (remove-hook! (vm-next-hook vm) next-hook))
247 (trap-in-procedure proc enter exit
248 #:current-frame current-frame #:vm vm
249 #:our-frame? our-frame?)))
251 (define (non-negative-integer? x)
252 (and (number? x) (integer? x) (exact? x) (not (negative? x))))
254 (define (positive-integer? x)
255 (and (number? x) (integer? x) (exact? x) (positive? x)))
261 (non-negative-integer? (car x))
262 (non-negative-integer? (cdr x))))
265 (define (in-range? range i)
266 (or-map (lambda (bounds)
267 (and (<= (car bounds) i)
271 ;; Building on trap-instructions-in-procedure, we have
272 ;; trap-at-procedure-ip-in-range.
274 (define* (trap-at-procedure-ip-in-range proc range handler
275 #:key current-frame (vm (the-vm))
278 (frame-matcher proc closure?)))
279 (arg-check proc procedure?)
280 (arg-check range range?)
281 (arg-check handler procedure?)
282 (let ((fp-stack '()))
283 (define (cull-frames! fp)
284 (let lp ((frames fp-stack))
285 (if (and (pair? frames) (< (car frames) fp))
287 (set! fp-stack frames))))
289 (define (next-handler frame)
290 (let ((fp (frame-address frame))
291 (ip (frame-instruction-pointer frame)))
293 (let ((now-in-range? (in-range? range ip))
294 (was-in-range? (and (pair? fp-stack) (= (car fp-stack) fp))))
297 (if (not now-in-range?)
298 (set! fp-stack (cdr fp-stack))))
300 (set! fp-stack (cons fp fp-stack))
303 (define (exit-handler frame)
304 (if (and (pair? fp-stack)
305 (= (car fp-stack) (frame-address frame)))
306 (set! fp-stack (cdr fp-stack))))
308 (trap-instructions-in-procedure proc next-handler exit-handler
309 #:current-frame current-frame #:vm vm
310 #:our-frame? our-frame?)))
312 ;; FIXME: define this in objcode somehow. We are reffing the first
313 ;; uint32 in the objcode, which is the length of the program (without
315 (define (program-last-ip prog)
316 (bytevector-u32-native-ref (objcode->bytecode (program-objcode prog)) 0))
318 (define (program-sources-by-line proc file)
319 (let lp ((sources (program-sources-pre-retire proc))
323 (pmatch (car sources)
324 ((,start-ip ,start-file ,start-line . ,start-col)
325 (if (equal? start-file file)
326 (cons (cons start-line
327 (if (pair? (cdr sources))
328 (pmatch (cadr sources)
330 (cons start-ip end-ip))
331 (else (error "unexpected")))
332 (cons start-ip (program-last-ip proc))))
335 (else (error "unexpected"))))
340 (assv-set! alist (car pair)
342 (or (assv-ref alist (car pair))
345 (sort! alist (lambda (x y) (< (car x) (car y))))
348 (define (source->ip-range proc file line)
349 (or (or-map (lambda (line-and-ranges)
351 ((= (car line-and-ranges) line)
352 (cdr line-and-ranges))
353 ((> (car line-and-ranges) line)
354 (warn "no instructions found at" file ":" line
355 "; using line" (car line-and-ranges) "instead")
356 (cdr line-and-ranges))
358 (program-sources-by-line proc file))
360 (warn "no instructions found for" file ":" line)
363 (define (source-closures-or-procedures file line)
364 (let ((closures (source-closures file line)))
367 (values (source-procedures file line) #f))))
369 ;; Building on trap-on-instructions-in-procedure, we have
370 ;; trap-at-source-location. The parameter `user-line' is one-indexed, as
371 ;; a user counts lines, instead of zero-indexed, as Guile counts lines.
373 (define* (trap-at-source-location file user-line handler
374 #:key current-frame (vm (the-vm)))
375 (arg-check file string?)
376 (arg-check user-line positive-integer?)
377 (arg-check handler procedure?)
380 (lambda () (source-closures-or-procedures file (1- user-line)))
381 (lambda (procs closures?)
388 (let ((range (source->ip-range proc file (1- user-line))))
389 (trap-at-procedure-ip-in-range proc range handler
390 #:current-frame current-frame
392 #:closure? closures?)))
395 (error "No procedures found at ~a:~a." file user-line)))
397 (for-each (lambda (trap) (trap frame)) traps)
398 (set! traps #f)))))))
402 ;; On a different tack, now we're going to build up a set of traps that
403 ;; do useful things during the dynamic extent of a procedure's
404 ;; application. First, a trap for when a frame returns.
406 (define* (trap-frame-finish frame return-handler abort-handler
408 (arg-check frame frame?)
409 (arg-check return-handler procedure?)
410 (arg-check abort-handler procedure?)
411 (let ((fp (frame-address frame)))
412 (define (pop-cont-hook frame . values)
413 (if (and fp (eq? (frame-address frame) fp))
416 (apply return-handler frame values))))
418 (define (abort-hook frame . values)
419 (if (and fp (< (frame-address frame) fp))
422 (apply abort-handler frame values))))
428 (error "return-or-abort traps may only be enabled once"))
429 (add-hook! (vm-pop-continuation-hook vm) pop-cont-hook)
430 (add-hook! (vm-abort-continuation-hook vm) abort-hook)
431 (add-hook! (vm-restore-continuation-hook vm) abort-hook))
434 (remove-hook! (vm-pop-continuation-hook vm) pop-cont-hook)
435 (remove-hook! (vm-abort-continuation-hook vm) abort-hook)
436 (remove-hook! (vm-restore-continuation-hook vm) abort-hook)))))
438 ;; A more traditional dynamic-wind trap. Perhaps this should not be
439 ;; based on the above trap-frame-finish?
441 (define* (trap-in-dynamic-extent proc enter-handler return-handler abort-handler
442 #:key current-frame (vm (the-vm))
444 (our-frame? (frame-matcher proc closure?)))
445 (arg-check proc procedure?)
446 (arg-check enter-handler procedure?)
447 (arg-check return-handler procedure?)
448 (arg-check abort-handler procedure?)
449 (let ((exit-trap #f))
450 (define (return-hook frame . values)
451 (exit-trap frame) ; disable the return/abort trap.
453 (return-handler frame))
455 (define (abort-hook frame . values)
456 (exit-trap frame) ; disable the return/abort trap.
458 (abort-handler frame))
460 (define (apply-hook frame)
461 (if (and (not exit-trap) (our-frame? frame))
463 (enter-handler frame)
465 (trap-frame-finish frame return-hook abort-hook
471 (add-hook! (vm-apply-hook vm) apply-hook))
476 (remove-hook! (vm-apply-hook vm) apply-hook)))))
478 ;; Trapping all procedure calls within a dynamic extent, recording the
479 ;; depth of the call stack relative to the original procedure.
481 (define* (trap-calls-in-dynamic-extent proc apply-handler return-handler
482 #:key current-frame (vm (the-vm))
485 (frame-matcher proc closure?)))
486 (arg-check proc procedure?)
487 (arg-check apply-handler procedure?)
488 (arg-check return-handler procedure?)
489 (let ((*call-depth* 0))
490 (define (trace-push frame)
491 (set! *call-depth* (1+ *call-depth*)))
493 (define (trace-pop frame . values)
494 (apply return-handler frame *call-depth* values)
495 (set! *call-depth* (1- *call-depth*)))
497 (define (trace-apply frame)
498 (apply-handler frame *call-depth*))
500 ;; FIXME: recalc depth on abort
502 (define (enter frame)
503 (add-hook! (vm-push-continuation-hook vm) trace-push)
504 (add-hook! (vm-pop-continuation-hook vm) trace-pop)
505 (add-hook! (vm-apply-hook vm) trace-apply))
507 (define (leave frame)
508 (remove-hook! (vm-push-continuation-hook vm) trace-push)
509 (remove-hook! (vm-pop-continuation-hook vm) trace-pop)
510 (remove-hook! (vm-apply-hook vm) trace-apply))
512 (define (return frame)
515 (define (abort frame)
518 (trap-in-dynamic-extent proc enter return abort
519 #:current-frame current-frame #:vm vm
520 #:our-frame? our-frame?)))
522 ;; Trapping all retired intructions within a dynamic extent.
524 (define* (trap-instructions-in-dynamic-extent proc next-handler
525 #:key current-frame (vm (the-vm))
528 (frame-matcher proc closure?)))
529 (arg-check proc procedure?)
530 (arg-check next-handler procedure?)
532 (define (trace-next frame)
533 (next-handler frame))
535 (define (enter frame)
536 (add-hook! (vm-next-hook vm) trace-next))
538 (define (leave frame)
539 (remove-hook! (vm-next-hook vm) trace-next))
541 (define (return frame)
544 (define (abort frame)
547 (trap-in-dynamic-extent proc enter return abort
548 #:current-frame current-frame #:vm vm
549 #:our-frame? our-frame?)))
551 ;; Traps calls and returns for a given procedure, keeping track of the call depth.
553 (define* (trap-calls-to-procedure proc apply-handler return-handler
555 (arg-check proc procedure?)
556 (arg-check apply-handler procedure?)
557 (arg-check return-handler procedure?)
558 (let ((pending-finish-traps '())
560 (define (apply-hook frame)
561 (let ((depth (length pending-finish-traps)))
563 (apply-handler frame depth)
565 (if (not (eq? (frame-address frame) last-fp))
566 (let ((finish-trap #f))
567 (define (frame-finished frame)
568 (finish-trap frame) ;; disables the trap.
569 (set! pending-finish-traps
570 (delq finish-trap pending-finish-traps))
571 (set! finish-trap #f))
573 (define (return-hook frame . values)
574 (frame-finished frame)
575 (apply return-handler frame depth values))
577 ;; FIXME: abort handler?
578 (define (abort-hook frame . values)
579 (frame-finished frame))
582 (trap-frame-finish frame return-hook abort-hook #:vm vm))
583 (set! pending-finish-traps
584 (cons finish-trap pending-finish-traps))))))
586 ;; The basic idea is that we install one trap that fires for calls,
587 ;; but that each call installs its own finish trap. Those finish
588 ;; traps remove themselves as their frames finish or abort.
590 ;; However since to the outside world we present the interface of
591 ;; just being one trap, disabling this calls-to-procedure trap
592 ;; should take care of disabling all of the pending finish traps. We
593 ;; keep track of pending traps through the pending-finish-traps
596 ;; So since we know that the trap-at-procedure will be enabled, and
597 ;; thus returning a disable closure, we make sure to wrap that
598 ;; closure in something that will disable pending finish traps.
599 (define (with-pending-finish-disablers trap)
600 (define (with-pending-finish-enablers trap)
601 (lambda* (#:optional frame)
602 (with-pending-finish-disablers (trap frame))))
604 (lambda* (#:optional frame)
605 (for-each (lambda (disable) (disable frame))
606 pending-finish-traps)
607 (set! pending-finish-traps '())
608 (with-pending-finish-enablers (trap frame))))
610 (with-pending-finish-disablers
611 (trap-at-procedure-call proc apply-hook #:vm vm))))
613 ;; Trap when the source location changes.
615 (define* (trap-matching-instructions frame-pred handler
617 (arg-check frame-pred procedure?)
618 (arg-check handler procedure?)
620 (define (next-hook frame)
621 (if (frame-pred frame)
627 (add-hook! (vm-next-hook vm) next-hook))
629 (remove-hook! (vm-next-hook vm) next-hook)))))