2 (define-module (ice-9 debugging ice-9-debugger-extensions)
3 #:use-module (ice-9 debugger))
5 ;;; Upgrade the debugger state object so that it can carry a flag
6 ;;; indicating whether the debugging session is continuable.
8 (cond ((string>=? (version) "1.7")
9 (use-modules (ice-9 debugger state))
10 (define-module (ice-9 debugger state)))
12 (define-module (ice-9 debugger))))
14 (set! state-rtd (make-record-type "debugger-state" '(stack index flags)))
15 (set! state? (record-predicate state-rtd))
17 (let ((make-state-internal (record-constructor state-rtd
18 '(stack index flags))))
19 (lambda (stack index . flags)
20 (make-state-internal stack index flags))))
21 (set! state-stack (record-accessor state-rtd 'stack))
22 (set! state-index (record-accessor state-rtd 'index))
24 (define state-flags (record-accessor state-rtd 'flags))
26 ;;; Add commands that (ice-9 debugger) doesn't currently have, for
27 ;;; continuing or single stepping program execution.
29 (cond ((string>=? (version) "1.7")
30 (use-modules (ice-9 debugger command-loop))
31 (define-module (ice-9 debugger command-loop)
32 #:use-module (ice-9 debugger)
33 #:use-module (ice-9 debugger state)
34 #:use-module (ice-9 debugging traps))
35 (define new-define-command define-command)
37 (lambda (name argument-template documentation procedure)
38 (new-define-command name argument-template procedure))))
40 (define-module (ice-9 debugger))))
42 (use-modules (ice-9 debugging steps)
43 (ice-9 debugging trace))
45 (define (assert-continuable state)
46 ;; Check that debugger is in a state where `continuing' makes sense.
47 ;; If not, signal an error.
48 (or (memq #:continuable (state-flags state))
49 (user-error "This debug session is not continuable.")))
51 (define (debugger:continue state)
52 "Tell the program being debugged to continue running. (In fact this is
53 the same as the @code{quit} command, because it exits the debugger
54 command loop and so allows whatever code it was that invoked the
55 debugger to continue.)"
56 (assert-continuable state)
57 (throw 'exit-debugger))
59 (define (debugger:finish state)
60 "Continue until evaluation of the current frame is complete, and
61 print the result obtained."
62 (assert-continuable state)
63 (at-exit (- (stack-length (state-stack state))
65 (list trace-trap debug-trap))
66 (debugger:continue state))
68 (define (debugger:step state n)
69 "Tell the debugged program to do @var{n} more steps from its current
70 position. One @dfn{step} means executing until the next frame entry
71 or exit of any kind. @var{n} defaults to 1."
72 (assert-continuable state)
73 (at-step debug-trap (or n 1))
74 (debugger:continue state))
76 (define (debugger:next state n)
77 "Tell the debugged program to do @var{n} more steps from its current
78 position, but only counting frame entries and exits where the
79 corresponding source code comes from the same file as the current
80 stack frame. (See @ref{Step Traps} for the details of how this
81 works.) If the current stack frame has no source code, the effect of
82 this command is the same as of @code{step}. @var{n} defaults to 1."
83 (assert-continuable state)
86 (frame-file-name (stack-ref (state-stack state)
88 (if (memq #:return (state-flags state))
90 (- (stack-length (state-stack state)) (state-index state))))
91 (debugger:continue state))
93 (define-command "continue" '()
94 "Continue program execution."
97 (define-command "finish" '()
98 "Continue until evaluation of the current frame is complete, and
99 print the result obtained."
102 (define-command "step" '('optional exact-integer)
103 "Continue until entry to @var{n}th next frame."
106 (define-command "next" '('optional exact-integer)
107 "Continue until entry to @var{n}th next frame in same file."
110 ;;; Export a couple of procedures for use by (ice-9 debugging trace).
112 (cond ((string>=? (version) "1.7"))
114 (define-module (ice-9 debugger))
115 (export write-frame-short/expression
116 write-frame-short/application)))
118 ;;; Provide a `debug-trap' entry point in (ice-9 debugger). This is
119 ;;; designed so that it can be called to explore the stack at a
120 ;;; breakpoint, and to single step from the breakpoint.
122 (define-module (ice-9 debugger))
124 (use-modules (ice-9 debugging traps))
126 (define *not-yet-introduced* #t)
128 (cond ((string>=? (version) "1.7"))
130 (define (debugger-command-loop state)
131 (read-and-dispatch-commands state (current-input-port)))))
133 (define-public (debug-trap trap-context)
134 "Invoke the Guile debugger to explore the stack at the specified @var{trap}."
135 (start-stack 'debugger
136 (let* ((stack (tc:stack trap-context))
137 (flags1 (let ((trap-type (tc:type trap-context)))
141 (tc:return-value trap-context)))
144 (flags (if (tc:continuation trap-context)
145 (cons #:continuable flags1)
147 (state (apply make-state stack 0 flags)))
148 (if *not-yet-introduced*
149 (let ((ssize (stack-length stack)))
150 (display "This is the Guile debugger -- for help, type `help'.\n")
151 (set! *not-yet-introduced* #f)
153 (display "There is 1 frame on the stack.\n\n")
154 (format #t "There are ~A frames on the stack.\n\n" ssize))))
155 (write-state-short-with-source-location state)
156 (debugger-command-loop state))))
158 (define write-state-short-with-source-location
159 (cond ((string>=? (version) "1.7")
163 (let* ((frame (stack-ref (state-stack state) (state-index state)))
164 (source (frame-source frame))
165 (position (and source (source-position source))))
166 (format #t "Frame ~A at " (frame-number frame))
168 (display-position position)
169 (display "unknown source location"))
172 (write-frame-short frame)