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