debug has for-trap? field
[bpt/guile.git] / module / system / repl / debug.scm
CommitLineData
33df2ec7
AW
1;;; Guile VM debugging facilities
2
3;;; Copyright (C) 2001, 2009, 2010 Free Software Foundation, Inc.
4;;;
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.
9;;;
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.
14;;;
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
18
19;;; Code:
20
21(define-module (system repl debug)
22 #:use-module (system base pmatch)
23 #:use-module (system base syntax)
24 #:use-module (system base language)
25 #:use-module (system vm vm)
26 #:use-module (system vm frame)
27 #:use-module (ice-9 rdelim)
28 #:use-module (ice-9 pretty-print)
29 #:use-module (ice-9 format)
30 #:use-module ((system vm inspect) #:select ((inspect . %inspect)))
31 #:use-module (system vm program)
32 #:export (<debug>
a36c3a45
AW
33 make-debug debug?
34 debug-frames debug-index debug-error-message debug-for-trap?
542f975e 35 print-registers print-locals print-frame print-frames frame->module
586aff5a
AW
36 stack->vector narrow-stack->vector
37 frame->stack-vector))
33df2ec7 38
2e67eb6f
AW
39;; TODO:
40;;
2e67eb6f
AW
41;; eval expression in context of frame
42;; set local variable in frame
2e67eb6f
AW
43;; step until greater source line
44;; watch expression
2e67eb6f 45;; set printing width
2e67eb6f
AW
46;; disassemble the current function
47;; inspect any object
33df2ec7
AW
48
49;;;
50;;; Debugger
51;;;
52;;; The actual interaction loop of the debugger is run by the repl. This module
53;;; simply exports a data structure to hold the debugger state, along with its
54;;; accessors, and provides some helper functions.
55;;;
56
a36c3a45 57(define-record <debug> frames index error-message for-trap?)
33df2ec7
AW
58
59\f
60
61(define (reverse-hashq h)
62 (let ((ret (make-hash-table)))
63 (hash-for-each
64 (lambda (k v)
65 (hashq-set! ret v (cons k (hashq-ref ret v '()))))
66 h)
67 ret))
68
542f975e
AW
69(define* (print-registers frame #:optional (port (current-output-port))
70 #:key (per-line-prefix " "))
71 (define (print fmt val)
72 (display per-line-prefix port)
73 (run-hook before-print-hook val)
74 (format port fmt val))
75
76 (format port "~aRegisters:~%" per-line-prefix)
77 (print "ip = ~d\n" (frame-instruction-pointer frame))
78 (print "sp = #x~x\n" (frame-stack-pointer frame))
79 (print "fp = #x~x\n" (frame-address frame)))
80
33df2ec7 81(define* (print-locals frame #:optional (port (current-output-port))
97b3800e 82 #:key (width 72) (per-line-prefix " "))
33df2ec7
AW
83 (let ((bindings (frame-bindings frame)))
84 (cond
85 ((null? bindings)
86 (format port "~aNo local variables.~%" per-line-prefix))
87 (else
88 (format port "~aLocal variables:~%" per-line-prefix)
89 (for-each
90 (lambda (binding)
97b3800e
AW
91 (let ((v (let ((x (frame-local-ref frame (binding:index binding))))
92 (if (binding:boxed? binding)
93 (variable-ref x)
94 x))))
95 (display per-line-prefix port)
96 (run-hook before-print-hook v)
97 (format port "~a~:[~; (boxed)~] = ~v:@y\n"
98 (binding:name binding) (binding:boxed? binding) width v)))
33df2ec7
AW
99 (frame-bindings frame))))))
100
101(define* (print-frame frame #:optional (port (current-output-port))
102 #:key index (width 72) (full? #f) (last-source #f))
103 (define (source:pretty-file source)
104 (if source
105 (or (source:file source) "current input")
106 "unknown file"))
107 (let* ((source (frame-source frame))
108 (file (source:pretty-file source))
e867d563 109 (line (and=> source source:line-for-user))
08039143 110 (col (and=> source source:column)))
33df2ec7
AW
111 (if (and file (not (equal? file (source:pretty-file last-source))))
112 (format port "~&In ~a:~&" file))
08039143
AW
113 (format port "~9@a~:[~*~3_~;~3d~] ~v:@y~%"
114 (if line (format #f "~a:~a" line col) "")
115 index index width (frame-call-representation frame))
33df2ec7
AW
116 (if full?
117 (print-locals frame #:width width
118 #:per-line-prefix " "))))
119
120(define* (print-frames frames
121 #:optional (port (current-output-port))
122 #:key (width 72) (full? #f) (forward? #f) count)
123 (let* ((len (vector-length frames))
124 (lower-idx (if (or (not count) (positive? count))
125 0
126 (max 0 (+ len count))))
127 (upper-idx (if (and count (negative? count))
128 (1- len)
129 (1- (if count (min count len) len))))
130 (inc (if forward? 1 -1)))
131 (let lp ((i (if forward? lower-idx upper-idx))
132 (last-source #f))
133 (if (<= lower-idx i upper-idx)
134 (let* ((frame (vector-ref frames i)))
135 (print-frame frame port #:index i #:width width #:full? full?
136 #:last-source last-source)
137 (lp (+ i inc) (frame-source frame)))))))
138
139;; Ideally here we would have something much more syntactic, in that a set! to a
140;; local var that is not settable would raise an error, and export etc forms
141;; would modify the module in question: but alack, this is what we have now.
142;; Patches welcome!
143(define (frame->module frame)
144 (let ((proc (frame-procedure frame)))
145 (if (program? proc)
146 (let* ((mod (or (program-module proc) (current-module)))
147 (mod* (make-module)))
148 (module-use! mod* mod)
149 (for-each
150 (lambda (binding)
151 (let* ((x (frame-local-ref frame (binding:index binding)))
152 (var (if (binding:boxed? binding) x (make-variable x))))
153 (format #t
154 "~:[Read-only~;Mutable~] local variable ~a = ~70:@y\n"
155 (binding:boxed? binding)
156 (binding:name binding)
157 (if (variable-bound? var) (variable-ref var) var))
158 (module-add! mod* (binding:name binding) var)))
159 (frame-bindings frame))
160 mod*)
161 (current-module))))
162
163
33df2ec7
AW
164(define (stack->vector stack)
165 (let* ((len (stack-length stack))
166 (v (make-vector len)))
167 (if (positive? len)
168 (let lp ((i 0) (frame (stack-ref stack 0)))
169 (if (< i len)
170 (begin
171 (vector-set! v i frame)
172 (lp (1+ i) (frame-previous frame))))))
173 v))
174
175(define (narrow-stack->vector stack . args)
3d4f8e3c
AW
176 (let ((narrowed (apply make-stack (stack-ref stack 0) args)))
177 (if narrowed
178 (stack->vector narrowed)
179 #()))) ; ? Can be the case for a tail-call to `throw' tho
180
586aff5a
AW
181(define (frame->stack-vector frame)
182 (let ((tag (and (pair? (fluid-ref %stacks))
183 (cdar (fluid-ref %stacks)))))
184 (narrow-stack->vector
185 (make-stack frame)
186 ;; Take the stack from the given frame, cutting 0
187 ;; frames.
188 0
189 ;; Narrow the end of the stack to the most recent
190 ;; start-stack.
191 tag
192 ;; And one more frame, because %start-stack
193 ;; invoking the start-stack thunk has its own frame
194 ;; too.
195 0 (and tag 1))))
33df2ec7 196
2e67eb6f
AW
197;; (define (debug)
198;; (run-debugger
199;; (narrow-stack->vector
200;; (make-stack #t)
201;; ;; Narrow the `make-stack' frame and the `debug' frame
202;; 2
203;; ;; Narrow the end of the stack to the most recent start-stack.
204;; (and (pair? (fluid-ref %stacks))
205;; (cdar (fluid-ref %stacks))))))
206