0.9.2.43:
[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 ((eq '&rest (car want))
72                     t)
73                    ((endp want)
74                     (endp real))
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   ;; bug 353: This test fails at least most of the time for x86/linux
149   ;; ca. 0.8.20.16. -- WHN
150   #-(and x86 linux)
151   (assert (verify-backtrace
152            (lambda () (test #'not-optimized))
153            (list *undefined-function-frame*
154                  (list '(flet not-optimized))
155                  (list '(flet test) #'not-optimized)))))
156
157 #-alpha ; bug 346
158 ;;; Division by zero was a common error on PPC.  It depended on the
159 ;;; return function either being before INTEGER-/-INTEGER in memory,
160 ;;; or more than MOST-POSITIVE-FIXNUM bytes ahead.  It also depends on
161 ;;; INTEGER-/-INTEGER calling SIGNED-TRUNCATE.  I believe Raymond Toy
162 ;;; says that the Sparc backend (at least for CMUCL) inlines this, so
163 ;;; if SBCL does the same this test is probably not good for the
164 ;;; Sparc.
165 ;;;
166 ;;; Disabling tail call elimination on this will probably ensure that
167 ;;; the return value (to the flet or the enclosing top level form) is
168 ;;; more than MOST-POSITIVE-FIXNUM with the current spaces on OS X.
169 ;;; Enabling it might catch other problems, so do it anyway.
170 (flet ((optimized ()
171          (declare (optimize (speed 2) (debug 1))) ; tail call elimination
172          (/ 42 0))
173        (not-optimized ()
174          (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
175          (/ 42 0))
176        (test (fun)
177          (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
178          (funcall fun)))
179   (assert (verify-backtrace (lambda () (test #'optimized))
180                             (list '(/ 42 &rest)
181                                   (list '(flet test) #'optimized))))
182   (assert (verify-backtrace (lambda () (test #'not-optimized))
183                             (list '(/ 42 &rest)
184                                   '((flet not-optimized))
185                                   (list '(flet test) #'not-optimized)))))
186
187 #-(or alpha (and x86 linux)) ; bug 61
188 (progn
189   (defun throw-test ()
190     (throw 'no-such-tag t))
191   (assert (verify-backtrace #'throw-test '((throw-test)))))
192
193 ;;; test entry point handling in backtraces
194
195 (defun oops ()
196   (error "oops"))
197
198 (defmacro defbt (n ll &body body)
199   `(progn
200      ;; normal debug info
201      (defun ,(intern (format nil "BT.~A.1" n)) ,ll
202        ,@body)
203      ;; no arguments saved
204      (defun ,(intern (format nil "BT.~A.2" n)) ,ll
205        (declare (optimize (debug 1) (speed 3)))
206        ,@body)
207      ;; no lambda-list saved
208      (defun ,(intern (format nil "BT.~A.3" n)) ,ll
209        (declare (optimize (debug 0)))
210        ,@body)))
211
212 (defbt 1 (&key key)
213   (list key))
214
215 (defbt 2 (x)
216   (list x))
217
218 (defbt 3 (&key (key (oops)))
219   (list key))
220
221 ;;; ERROR instead of OOPS so that tail call elimination doesn't happen
222 (defbt 4 (&optional opt)
223   (list (error "error")))
224
225 (defbt 5 (&optional (opt (oops)))
226   (list opt))
227
228 #-(and x86 linux)
229 (macrolet ((with-details (bool &body body)
230              `(let ((sb-debug:*show-entry-point-details* ,bool))
231                 ,@body)))
232
233   ;; TL-XEP
234   (print :tl-xep)
235   (with-details t
236     (assert (verify-backtrace #'namestring
237                               '(((sb-c::tl-xep namestring) 0 ?)))))
238   (with-details nil
239     (assert (verify-backtrace #'namestring
240                               '((namestring)))))
241
242
243   ;; &MORE-PROCESSOR
244   (with-details t
245     (assert (verify-backtrace (lambda () (bt.1.1 :key))
246                               '(((sb-c::&more-processor bt.1.1) &rest))))
247     (assert (verify-backtrace (lambda () (bt.1.2 :key))
248                               '(((sb-c::&more-processor bt.1.2) &rest))))
249     (assert (verify-backtrace (lambda () (bt.1.3 :key))
250                               '(((sb-c::&more-processor bt.1.3) &rest)))))
251   (with-details nil
252     (assert (verify-backtrace (lambda () (bt.1.1 :key))
253                               '((bt.1.1 :key))))
254     (assert (verify-backtrace (lambda () (bt.1.2 :key))
255                               '((bt.1.2 &rest))))
256     (assert (verify-backtrace (lambda () (bt.1.3 :key))
257                               '((bt.1.3 &rest)))))
258
259   ;; XEP
260   (print :xep)
261   (with-details t
262     (assert (verify-backtrace #'bt.2.1
263                               '(((sb-c::xep bt.2.1) 0 ?))))
264     (assert (verify-backtrace #'bt.2.2
265                               '(((sb-c::xep bt.2.2) &rest))))
266     (assert (verify-backtrace #'bt.2.3
267                               '(((sb-c::xep bt.2.3) &rest)))))
268   (with-details nil
269     (assert (verify-backtrace #'bt.2.1
270                               '((bt.2.1))))
271     (assert (verify-backtrace #'bt.2.2
272                               '((bt.2.2 &rest))))
273     (assert (verify-backtrace #'bt.2.3
274                               '((bt.2.3 &rest)))))
275
276   ;; VARARGS-ENTRY
277   (print :varargs-entry)
278   (with-details t
279     (assert (verify-backtrace #'bt.3.1
280                              '(((sb-c::varargs-entry bt.3.1) :key nil))))
281     (assert (verify-backtrace #'bt.3.2
282                              '(((sb-c::varargs-entry bt.3.2) :key ?))))
283     (assert (verify-backtrace #'bt.3.3
284                              '(((sb-c::varargs-entry bt.3.3) &rest)))))
285   (with-details nil
286     (assert (verify-backtrace #'bt.3.1
287                               '((bt.3.1 :key nil))))
288     (assert (verify-backtrace #'bt.3.2
289                               '((bt.3.2 :key ?))))
290     (assert (verify-backtrace #'bt.3.3
291                               '((bt.3.3 &rest)))))
292
293   ;; HAIRY-ARG-PROCESSOR
294   (print :hairy-args-processor)
295   (with-details t
296     (assert (verify-backtrace #'bt.4.1
297                               '(((sb-c::hairy-arg-processor bt.4.1) ?))))
298     (assert (verify-backtrace #'bt.4.2
299                               '(((sb-c::hairy-arg-processor bt.4.2) ?))))
300     (assert (verify-backtrace #'bt.4.3
301                               '(((sb-c::hairy-arg-processor bt.4.3) &rest)))))
302   (with-details nil
303     (assert (verify-backtrace #'bt.4.1
304                               '((bt.4.1 ?))))
305     (assert (verify-backtrace #'bt.4.2
306                               '((bt.4.2 ?))))
307     (assert (verify-backtrace #'bt.4.3
308                               '((bt.4.3 &rest)))))
309
310   ;; &OPTIONAL-PROCESSOR
311   (print :optional-processor)
312   (with-details t
313     (assert (verify-backtrace #'bt.5.1
314                               '(((sb-c::&optional-processor bt.5.1)))))
315     (assert (verify-backtrace #'bt.5.2
316                               '(((sb-c::&optional-processor bt.5.2) &rest))))
317     (assert (verify-backtrace #'bt.5.3
318                               '(((sb-c::&optional-processor bt.5.3) &rest)))))
319   (with-details nil
320     (assert (verify-backtrace #'bt.5.1
321                               '((bt.5.1))))
322     (assert (verify-backtrace #'bt.5.2
323                               '((bt.5.2 &rest))))
324     (assert (verify-backtrace #'bt.5.3
325                               '((bt.5.3 &rest))))))
326
327 ;;;; test TRACE
328
329 (defun trace-this ()
330   'ok)
331
332 (let ((out (with-output-to-string (*trace-output*)
333              (trace trace-this)
334              (assert (eq 'ok (trace-this)))
335              (untrace))))
336   (assert (search "TRACE-THIS" out))
337   (assert (search "returned OK" out)))
338
339 #-(and ppc darwin)
340 ;;; bug 379
341 (let ((out (with-output-to-string (*trace-output*)
342              (trace trace-this :encapsulate nil)
343              (assert (eq 'ok (trace-this)))
344              (untrace))))
345   (assert (search "TRACE-THIS" out))
346   (assert (search "returned OK" out)))
347
348 ;;;; test infinite error protection
349
350 (defmacro nest-errors (n-levels error-form)
351   (if (< 0 n-levels)
352       `(handler-bind ((error (lambda (condition)
353                                (declare (ignore condition))
354                                ,error-form)))
355         (nest-errors ,(1- n-levels) ,error-form))
356       error-form))
357
358 (defun erroring-debugger-hook (condition old-debugger-hook)
359   (let ((*debugger-hook* old-debugger-hook))
360     (format t "recursive condition: ~A~%" condition) (force-output)
361     (error "recursive condition: ~A" condition)))
362
363 (defun test-inifinite-error-protection ()
364   ;; after 50 successful throws to SB-IMPL::TOPLEVEL-CATCHER sbcl used
365   ;; to halt, it produces so much garbage that's hard to suppress that
366   ;; it is tested only once
367   (let ((*debugger-hook* #'erroring-debugger-hook))
368     (loop repeat 1 do
369           (let ((error-counter 0)
370                 (*terminal-io* (make-broadcast-stream)))
371             (assert
372              (not (eq
373                    :normal-exit
374                    (catch 'sb-impl::toplevel-catcher
375                      (nest-errors 20 (error "infinite error ~s"
376                                             (incf error-counter)))
377                      :normal-exit))))))))
378
379 (test-inifinite-error-protection)
380
381 #+sb-thread
382 (let ((thread (sb-thread:make-thread #'test-inifinite-error-protection)))
383   (loop while (sb-thread:thread-alive-p thread)))
384
385 ;;; success
386 (quit :unix-status 104)