Merge commit 'e20d7001c3f7150400169fecb0bf0eefdf122fe2' into vm-check
[bpt/guile.git] / module / ice-9 / debug.scm
CommitLineData
cd5fea8d 1;;;; Copyright (C) 1996, 1997, 1998, 1999, 2001, 2006 Free Software Foundation
4fe4070a 2;;;;
73be1d9e
MV
3;;;; This library is free software; you can redistribute it and/or
4;;;; modify it under the terms of the GNU Lesser General Public
5;;;; License as published by the Free Software Foundation; either
6;;;; version 2.1 of the License, or (at your option) any later version.
4fe4070a 7;;;;
73be1d9e 8;;;; This library is distributed in the hope that it will be useful,
4fe4070a 9;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
10;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11;;;; Lesser General Public License for more details.
4fe4070a 12;;;;
73be1d9e
MV
13;;;; You should have received a copy of the GNU Lesser General Public
14;;;; License along with this library; if not, write to the Free Software
92205699 15;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
a482f2cc 16;;;;
4fe4070a
MD
17;;;; The author can be reached at djurfeldt@nada.kth.se
18;;;; Mikael Djurfeldt, SANS/NADA KTH, 10044 STOCKHOLM, SWEDEN
19;;;;
20\f
21
1a179b03
MD
22(define-module (ice-9 debug)
23 :export (frame-number->index trace untrace trace-stack untrace-stack))
4fe4070a
MD
24
25\f
8ccbf00d
MD
26;;; {Misc}
27;;;
1a179b03 28(define (frame-number->index n . stack)
6de43e5f
MD
29 (let ((stack (if (null? stack)
30 (fluid-ref the-last-stack)
31 (car stack))))
32 (if (memq 'backwards (debug-options))
33 n
34 (- (stack-length stack) n 1))))
8ccbf00d
MD
35
36\f
e6875011
MD
37;;; {Trace}
38;;;
9a942103
MD
39;;; This code is just an experimental prototype (e. g., it is not
40;;; thread safe), but since it's at the same time useful, it's
41;;; included anyway.
42;;;
e6875011
MD
43(define traced-procedures '())
44
1a179b03 45(define (trace . args)
e6875011
MD
46 (if (null? args)
47 (nameify traced-procedures)
48 (begin
49 (for-each (lambda (proc)
cb3a1784
MD
50 (if (not (procedure? proc))
51 (error "trace: Wrong type argument:" proc))
e6875011
MD
52 (set-procedure-property! proc 'trace #t)
53 (if (not (memq proc traced-procedures))
54 (set! traced-procedures
55 (cons proc traced-procedures))))
56 args)
d95c0b76
NJ
57 (trap-set! apply-frame-handler trace-entry)
58 (trap-set! exit-frame-handler trace-exit)
59 ;; We used to reset `trace-level' here to 0, but this is wrong
60 ;; if `trace' itself is being traced, since `trace-exit' will
61 ;; then decrement `trace-level' to -1! It shouldn't actually
62 ;; be necessary to set `trace-level' here at all.
e6875011
MD
63 (debug-enable 'trace)
64 (nameify args))))
65
1a179b03 66(define (untrace . args)
e6875011
MD
67 (if (and (null? args)
68 (not (null? traced-procedures)))
69 (apply untrace traced-procedures)
70 (begin
71 (for-each (lambda (proc)
72 (set-procedure-property! proc 'trace #f)
73 (set! traced-procedures (delq! proc traced-procedures)))
74 args)
75 (if (null? traced-procedures)
76 (debug-disable 'trace))
77 (nameify args))))
78
79(define (nameify ls)
80 (map (lambda (proc)
81 (let ((name (procedure-name proc)))
82 (or name proc)))
83 ls))
84
85(define trace-level 0)
59e1116d 86(add-hook! abort-hook (lambda () (set! trace-level 0)))
e6875011 87
941614c6
NJ
88(define traced-stack-ids (list 'repl-stack))
89(define trace-all-stacks? #f)
90
1a179b03 91(define (trace-stack id)
941614c6
NJ
92 "Add ID to the set of stack ids for which tracing is active.
93If `#t' is in this set, tracing is active regardless of stack context.
94To remove ID again, use `untrace-stack'. If you add the same ID twice
95using `trace-stack', you will need to remove it twice."
96 (set! traced-stack-ids (cons id traced-stack-ids))
97 (set! trace-all-stacks? (memq #t traced-stack-ids)))
98
1a179b03 99(define (untrace-stack id)
941614c6
NJ
100 "Remove ID from the set of stack ids for which tracing is active."
101 (set! traced-stack-ids (delq1! id traced-stack-ids))
102 (set! trace-all-stacks? (memq #t traced-stack-ids)))
103
e6875011 104(define (trace-entry key cont tail)
941614c6
NJ
105 (if (or trace-all-stacks?
106 (memq (stack-id cont) traced-stack-ids))
9a942103
MD
107 (let ((cep (current-error-port))
108 (frame (last-stack-frame cont)))
109 (if (not tail)
110 (set! trace-level (+ trace-level 1)))
111 (let indent ((n trace-level))
112 (cond ((> n 1) (display "| " cep) (indent (- n 1)))))
08cc62c7
MD
113 (display-application frame cep)
114 (newline cep)))
9a942103
MD
115 ;; It's not necessary to call the continuation since
116 ;; execution will continue if the handler returns
117 ;(cont #f)
118 )
e6875011
MD
119
120(define (trace-exit key cont retval)
941614c6
NJ
121 (if (or trace-all-stacks?
122 (memq (stack-id cont) traced-stack-ids))
9a942103
MD
123 (let ((cep (current-error-port)))
124 (set! trace-level (- trace-level 1))
125 (let indent ((n trace-level))
126 (cond ((> n 0) (display "| " cep) (indent (- n 1)))))
127 (write retval cep)
b89203a1 128 (newline cep))))
e6875011 129
e6875011 130\f
9a942103
MD
131;;; A fix to get the error handling working together with the module system.
132;;;
296ff5e7
MV
133;;; XXX - Still needed?
134(module-set! the-root-module 'debug-options debug-options)