Commit | Line | Data |
---|---|---|
ec16eb78 | 1 | ;;;; Copyright (C) 1996, 1997, 1998, 1999, 2001, 2006, 2010 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 | |
53befeb7 | 6 | ;;;; version 3 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 | 22 | (define-module (ice-9 debug) |
ec16eb78 AW |
23 | #:use-module (ice-9 save-stack) |
24 | #:export (frame-number->index trace untrace trace-stack untrace-stack)) | |
4fe4070a MD |
25 | |
26 | \f | |
8ccbf00d MD |
27 | ;;; {Misc} |
28 | ;;; | |
1a179b03 | 29 | (define (frame-number->index n . stack) |
6de43e5f MD |
30 | (let ((stack (if (null? stack) |
31 | (fluid-ref the-last-stack) | |
32 | (car stack)))) | |
33 | (if (memq 'backwards (debug-options)) | |
34 | n | |
35 | (- (stack-length stack) n 1)))) | |
8ccbf00d MD |
36 | |
37 | \f | |
e6875011 MD |
38 | ;;; {Trace} |
39 | ;;; | |
9a942103 MD |
40 | ;;; This code is just an experimental prototype (e. g., it is not |
41 | ;;; thread safe), but since it's at the same time useful, it's | |
42 | ;;; included anyway. | |
43 | ;;; | |
e6875011 MD |
44 | (define traced-procedures '()) |
45 | ||
1a179b03 | 46 | (define (trace . args) |
e6875011 MD |
47 | (if (null? args) |
48 | (nameify traced-procedures) | |
49 | (begin | |
50 | (for-each (lambda (proc) | |
cb3a1784 MD |
51 | (if (not (procedure? proc)) |
52 | (error "trace: Wrong type argument:" proc)) | |
e6875011 MD |
53 | (set-procedure-property! proc 'trace #t) |
54 | (if (not (memq proc traced-procedures)) | |
55 | (set! traced-procedures | |
56 | (cons proc traced-procedures)))) | |
57 | args) | |
d95c0b76 NJ |
58 | (trap-set! apply-frame-handler trace-entry) |
59 | (trap-set! exit-frame-handler trace-exit) | |
60 | ;; We used to reset `trace-level' here to 0, but this is wrong | |
61 | ;; if `trace' itself is being traced, since `trace-exit' will | |
62 | ;; then decrement `trace-level' to -1! It shouldn't actually | |
63 | ;; be necessary to set `trace-level' here at all. | |
e6875011 MD |
64 | (debug-enable 'trace) |
65 | (nameify args)))) | |
66 | ||
1a179b03 | 67 | (define (untrace . args) |
e6875011 MD |
68 | (if (and (null? args) |
69 | (not (null? traced-procedures))) | |
70 | (apply untrace traced-procedures) | |
71 | (begin | |
72 | (for-each (lambda (proc) | |
73 | (set-procedure-property! proc 'trace #f) | |
74 | (set! traced-procedures (delq! proc traced-procedures))) | |
75 | args) | |
76 | (if (null? traced-procedures) | |
77 | (debug-disable 'trace)) | |
78 | (nameify args)))) | |
79 | ||
80 | (define (nameify ls) | |
81 | (map (lambda (proc) | |
82 | (let ((name (procedure-name proc))) | |
83 | (or name proc))) | |
84 | ls)) | |
85 | ||
86 | (define trace-level 0) | |
59e1116d | 87 | (add-hook! abort-hook (lambda () (set! trace-level 0))) |
e6875011 | 88 | |
941614c6 NJ |
89 | (define traced-stack-ids (list 'repl-stack)) |
90 | (define trace-all-stacks? #f) | |
91 | ||
1a179b03 | 92 | (define (trace-stack id) |
941614c6 NJ |
93 | "Add ID to the set of stack ids for which tracing is active. |
94 | If `#t' is in this set, tracing is active regardless of stack context. | |
95 | To remove ID again, use `untrace-stack'. If you add the same ID twice | |
96 | using `trace-stack', you will need to remove it twice." | |
97 | (set! traced-stack-ids (cons id traced-stack-ids)) | |
98 | (set! trace-all-stacks? (memq #t traced-stack-ids))) | |
99 | ||
1a179b03 | 100 | (define (untrace-stack id) |
941614c6 NJ |
101 | "Remove ID from the set of stack ids for which tracing is active." |
102 | (set! traced-stack-ids (delq1! id traced-stack-ids)) | |
103 | (set! trace-all-stacks? (memq #t traced-stack-ids))) | |
104 | ||
e6875011 | 105 | (define (trace-entry key cont tail) |
941614c6 NJ |
106 | (if (or trace-all-stacks? |
107 | (memq (stack-id cont) traced-stack-ids)) | |
9a942103 MD |
108 | (let ((cep (current-error-port)) |
109 | (frame (last-stack-frame cont))) | |
110 | (if (not tail) | |
111 | (set! trace-level (+ trace-level 1))) | |
112 | (let indent ((n trace-level)) | |
113 | (cond ((> n 1) (display "| " cep) (indent (- n 1))))) | |
08cc62c7 MD |
114 | (display-application frame cep) |
115 | (newline cep))) | |
9a942103 MD |
116 | ;; It's not necessary to call the continuation since |
117 | ;; execution will continue if the handler returns | |
118 | ;(cont #f) | |
119 | ) | |
e6875011 MD |
120 | |
121 | (define (trace-exit key cont retval) | |
941614c6 NJ |
122 | (if (or trace-all-stacks? |
123 | (memq (stack-id cont) traced-stack-ids)) | |
9a942103 MD |
124 | (let ((cep (current-error-port))) |
125 | (set! trace-level (- trace-level 1)) | |
126 | (let indent ((n trace-level)) | |
127 | (cond ((> n 0) (display "| " cep) (indent (- n 1))))) | |
128 | (write retval cep) | |
b89203a1 | 129 | (newline cep)))) |
e6875011 | 130 | |
e6875011 | 131 | \f |
9a942103 MD |
132 | ;;; A fix to get the error handling working together with the module system. |
133 | ;;; | |
296ff5e7 MV |
134 | ;;; XXX - Still needed? |
135 | (module-set! the-root-module 'debug-options debug-options) |