0.8.20.17:
[sbcl.git] / tests / debug.impure.lisp
1 ;;;; This file is for testing debugging functionality, using
2 ;;;; test machinery which might have side effects (e.g. 
3 ;;;; executing DEFUN).
4
5 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; more information.
7 ;;;;
8 ;;;; While most of SBCL is derived from the CMU CL system, the test
9 ;;;; files (like this one) were written from scratch after the fork
10 ;;;; from CMU CL.
11 ;;;; 
12 ;;;; This software is in the public domain and is provided with
13 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
14 ;;;; more information.
15
16 (cl:in-package :cl-user)
17 \f
18 ;;;; Check that we get debug arglists right.
19
20 ;;; FIXME: This should use some get-argslist like functionality that
21 ;;; we actually export.
22 ;;;
23 ;;; Return the debug arglist of the function object FUN as a list, or
24 ;;; punt with :UNKNOWN.
25 (defun get-arglist (fun)
26   (declare (type function fun))
27   ;; The Lisp-level type FUNCTION can conceal a multitude of sins..
28   (case (sb-kernel:widetag-of fun)
29     (#.sb-vm:simple-fun-header-widetag
30       (sb-kernel:%simple-fun-arglist fun))
31     (#.sb-vm:closure-header-widetag (get-arglist
32                                      (sb-kernel:%closure-fun fun)))
33     ;; In code/describe.lisp, ll. 227 (%describe-fun), we use a scheme
34     ;; like above, and it seems to work. -- MNA 2001-06-12
35     ;;
36     ;; (There might be other cases with arglist info also.
37     ;; SIMPLE-FUN-HEADER-WIDETAG and CLOSURE-HEADER-WIDETAG just
38     ;; happen to be the two case that I had my nose rubbed in when
39     ;; debugging a GC problem caused by applying %SIMPLE-FUN-ARGLIST to
40     ;; a closure. -- WHN 2001-06-05)
41     (t :unknown)))
42
43 (defun zoop (zeep &key beep)
44   blurp)
45 (assert (equal (get-arglist #'zoop) '(zeep &key beep)))
46
47 ;;; Check some predefined functions too.
48 ;;;
49 ;;; (We don't know exactly what the arguments are, e.g. the first
50 ;;; argument of PRINT might be SB-IMPL::OBJECT or SB-KERNEL::OBJ or
51 ;;; whatever. But we do know the general structure that a correct
52 ;;; answer should have, so we can safely do a lot of checks.)
53 (destructuring-bind (object-sym &optional-sym stream-sym) (get-arglist #'print)
54   (assert (symbolp object-sym))
55   (assert (eql &optional-sym '&optional))
56   (assert (symbolp stream-sym)))
57 (destructuring-bind (dest-sym control-sym &rest-sym format-args-sym)
58     (get-arglist #'format)
59   (assert (symbolp dest-sym))
60   (assert (symbolp control-sym))
61   (assert (eql &rest-sym '&rest))
62   (assert (symbolp format-args-sym)))
63
64 ;;; Check for backtraces generally being correct.  Ensure that the
65 ;;; actual backtrace finishes (doesn't signal any errors on its own),
66 ;;; and that it contains the frames we expect, doesn't contain any
67 ;;; "bogus stack frame"s, and contains the appropriate toplevel call
68 ;;; and hasn't been cut off anywhere.
69 (defun verify-backtrace (test-function frame-specs &key (allow-stunted nil))
70   (labels ((args-equal (want real)
71              (cond ((endp want)
72                     (endp real))
73                    ((eq '&rest (car want))
74                     t)
75                    ((or (eq '? (car want)) (equal (car want) (car real)))
76                     (args-equal (cdr want) (cdr real)))
77                    (t
78                     nil))))
79     (let ((result nil))
80       (block outer-handler
81         (handler-bind
82             ((error (lambda (condition)
83                       ;; find the part of the backtrace we're interested in
84                       (let ((backtrace (progn
85                                          ;; (backtrace 13)
86                                          (member (caar frame-specs)
87                                                  (sb-debug:backtrace-as-list)
88                                                  :key #'car
89                                                  :test #'equal))))
90                         
91                         (setf result condition)
92                         
93                         (unless backtrace
94                           (print :missing-backtrace)
95                           (setf result nil))
96                         
97                         ;; check that we have all the frames we wanted
98                         (mapcar 
99                          (lambda (spec frame)
100                            (unless (or (not spec)
101                                        (and (equal (car spec) (car frame))
102                                             (args-equal (cdr spec) 
103                                                         (cdr frame))))
104                              (print (list :mismatch spec frame))
105                              (setf result nil)))
106                          frame-specs
107                          backtrace)
108                         
109                         ;; Make sure the backtrace isn't stunted in
110                         ;; any way.  (Depends on running in the main
111                         ;; thread.)
112                         (let ((end (last backtrace 2)))
113                           (unless (equal (caar end) 
114                                          (if *show-entry-point-details*
115                                              '(sb-c::tl-xep sb-impl::toplevel-init)
116                                              'sb-impl::toplevel-init))
117                             (print (list :backtrace-stunted (caar end)))
118                             (setf result nil)))
119                         (return-from outer-handler)))))
120           (funcall test-function)))
121       result)))
122
123 (defvar *undefined-function-frame*
124   ;; bug 353
125   '(#+(or x86 x86-64) "bogus stack frame"
126     #-(or x86 x86-64) "undefined function"))
127
128 #-(or alpha) ; bug 346
129 ;;; Test for "undefined function" (undefined_tramp) working properly.
130 ;;; Try it with and without tail call elimination, since they can have
131 ;;; different effects.  (Specifically, if undefined_tramp is incorrect
132 ;;; a stunted stack can result from the tail call variant.)
133 (flet ((optimized ()
134          (declare (optimize (speed 2) (debug 1))) ; tail call elimination
135          (#:undefined-function 42))
136        (not-optimized ()
137          (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
138          (#:undefined-function 42))
139        (test (fun)
140          (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
141          (funcall fun)))
142
143   (assert (verify-backtrace
144            (lambda () (test #'optimized))
145            (list *undefined-function-frame*
146                  (list '(flet test) #'optimized))))
147   
148   #-x86 ; bug 353: This test fails at least most of the time for x86/linux ca. 0.8.20.16. -- WHN
149   (assert (verify-backtrace 
150            (lambda () (test #'not-optimized))
151            (list *undefined-function-frame*
152                  (list '(flet not-optimized))
153                  (list '(flet test) #'not-optimized)))))
154
155 #-alpha ; bug 346
156 ;;; Division by zero was a common error on PPC.  It depended on the
157 ;;; return function either being before INTEGER-/-INTEGER in memory,
158 ;;; or more than MOST-POSITIVE-FIXNUM bytes ahead.  It also depends on
159 ;;; INTEGER-/-INTEGER calling SIGNED-TRUNCATE.  I believe Raymond Toy
160 ;;; says that the Sparc backend (at least for CMUCL) inlines this, so
161 ;;; if SBCL does the same this test is probably not good for the
162 ;;; Sparc.
163 ;;;
164 ;;; Disabling tail call elimination on this will probably ensure that
165 ;;; the return value (to the flet or the enclosing top level form) is
166 ;;; more than MOST-POSITIVE-FIXNUM with the current spaces on OS X.
167 ;;; Enabling it might catch other problems, so do it anyway.
168 (flet ((optimized ()
169          (declare (optimize (speed 2) (debug 1))) ; tail call elimination
170          (/ 42 0))
171        (not-optimized ()
172          (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
173          (/ 42 0))
174        (test (fun)
175          (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
176          (funcall fun)))
177   (assert (verify-backtrace (lambda () (test #'optimized))
178                             (list '(/ 42 &rest) 
179                                   (list '(flet test) #'optimized))))
180   (assert (verify-backtrace (lambda () (test #'not-optimized))
181                             (list '(/ 42 &rest)
182                                   '((flet not-optimized))
183                                   (list '(flet test) #'not-optimized)))))
184
185 #-(or alpha (and x86 linux)) ; bug 61
186 (progn
187   (defun throw-test ()
188     (throw 'no-such-tag t))
189   (assert (verify-backtrace #'throw-test '((throw-test)))))
190
191 ;;; test entry point handling in backtraces
192
193 (defun oops ()
194   (error "oops"))
195
196 (defun bt.1 (&key key)
197   (list key))
198
199 (defun bt.2 (x)
200   (list x))
201
202 (defun bt.3 (&key (key (oops)))
203   (list key))
204
205 ;;; ERROR instead of OOPS so that tail call elimination doesn't happen
206 (defun bt.4 (&optional opt)
207   (list (error "error")))
208
209 (defun bt.5 (&optional (opt (oops)))
210   (list opt))
211
212 #-(and x86 linux)
213 (macrolet ((with-details (bool &body body)
214              `(let ((sb-debug:*show-entry-point-details* ,bool))
215                 ,@body)))
216
217   ;; &MORE-PROCESSOR
218   (with-details t
219     (assert (verify-backtrace (lambda () (bt.1 :key))
220                               '(((sb-c::&more-processor bt.1) &rest)))))
221   (with-details nil
222     (assert (verify-backtrace (lambda () (bt.1 :key))
223                               '((bt.1 :key)))))
224
225   ;; XEP
226   (with-details t
227     (assert (verify-backtrace #'bt.2
228                               '(((sb-c::xep bt.2) 0 ?)))))
229   (with-details nil
230     (assert (verify-backtrace #'bt.2
231                               '((bt.2)))))
232
233   ;; TL-XEP
234   (with-details t
235     (assert (verify-backtrace #'namestring
236                               '(((sb-c::tl-xep namestring) 0 ?)))))
237   (with-details nil
238     (assert (verify-backtrace #'namestring
239                               '((namestring)))))
240
241   ;; VARARGS-ENTRY
242   (with-details t
243     (assert (verify-backtrace #'bt.3
244                              '(((sb-c::varargs-entry bt.3) :key nil)))))
245   (with-details nil
246     (assert (verify-backtrace #'bt.3
247                               '((bt.3 :key nil)))))
248
249   ;; HAIRY-ARG-PROCESSOR
250   (with-details t
251     (assert (verify-backtrace #'bt.4
252                               '(((sb-c::hairy-arg-processor bt.4) ?)))))
253   (with-details nil
254     (assert (verify-backtrace #'bt.4
255                               '((bt.4 ?)))))
256
257   ;; &OPTIONAL-PROCESSOR
258   (with-details t
259     (assert (verify-backtrace #'bt.5
260                               '(((sb-c::&optional-processor bt.5))))))
261   (with-details nil
262     (assert (verify-backtrace #'bt.5
263                               '((bt.5))))))
264
265 ;;; success
266 (quit :unix-status 104)