0.9.16.38:
[sbcl.git] / src / code / step.lisp
1 ;;;; single stepper for SBCL
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
11
12 ;;;; Single stepping works by having compiler insert STEP-CONDITION
13 ;;;; signalling forms into code compiled at high debug settings, and
14 ;;;; having a handler for them at the toplevel.
15
16 (in-package "SB-IMPL") ; in warm SBCL
17
18 (defun step-form (form args)
19   (restart-case
20       (signal 'step-form-condition
21               :form form
22               :args args)
23     (step-continue ()
24       :report "Resume normal execution"
25       (disable-stepping)
26       (setf *step-out* nil))
27     (step-out ()
28       :report "Resume stepping after returning from this function"
29       (disable-stepping)
30       (setf *step-out* t)
31       nil)
32     (step-next ()
33       :report "Step over call"
34       nil)
35     (step-into ()
36       :report "Step into call"
37       t)))
38
39 (defun step-values (form &rest values)
40   (declare (dynamic-extent values))
41   (signal 'step-values-condition :form form :result values)
42   (values-list values))
43
44 (defvar *step-help* "The following commands are available at the single
45 stepper's prompt:
46
47  S: Step into the current expression.
48  N: Evaluate the current expression without stepping.
49  C: Evaluate to finish without stepping.
50  Q: Abort evaluation.
51  B: Backtrace.
52  ?: Display this message.
53 ")
54
55 (defgeneric single-step (condition))
56
57 (defmethod single-step ((condition step-values-condition))
58   (let ((values (step-condition-result condition)))
59     (format *debug-io* "; ~A => ~:[#<no value>~;~{~S~^, ~}~]~%"
60             (step-condition-form condition)
61             values values)))
62
63 (defmethod single-step ((condition step-form-condition))
64   (let ((form (step-condition-form condition))
65         (args (step-condition-args condition)))
66     (let ((*print-circle* t)
67           (*print-pretty* t)
68           (*print-readably* nil))
69       (format *debug-io*
70               "; Evaluating call:~%~<; ~@;  ~A~:>~%~
71                ; ~:[With arguments:~%~<; ~@;~{  ~S~^~%~}~:>~;With unknown arguments~]~%"
72               (list form)
73               (eq args :unknown)
74               (list args)))
75     (finish-output *debug-io*)
76     (let ((*stack-top-hint* (sb-di::find-stepped-frame)))
77       (invoke-debugger condition))))
78
79 (defvar *stepper-hook* 'single-step
80   #+sb-doc "Customization hook for alternative single-steppers.
81 *STEPPER-HOOK* is bound to NIL prior to calling the bound function
82 with the STEP-CONDITION as argument.")
83
84 (defun invoke-stepper (condition)
85   (when (and (stepping-enabled-p)
86              *stepper-hook*)
87     (let ((hook *stepper-hook*)
88           (*stepper-hook* nil))
89       (funcall hook condition))))
90
91 (defmacro step (form)
92   #+sb-doc
93   "The form is evaluated with single stepping enabled. Function calls
94 outside the lexical scope of the form can be stepped into only if the
95 functions in question have been compiled with sufficient DEBUG policy
96 to be at least partially steppable."
97   `(locally
98        (declare (optimize (sb-c:insert-step-conditions 0)))
99      (format t "Single stepping. Type ? for help.~%")
100      (let ((*step-out* :maybe))
101        (with-stepping-enabled
102          (locally (declare (optimize (sb-c:insert-step-conditions 3)))
103            ,form)))))