Rework test infrastructure to keep track of tests which are disabled
[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
18 ;;; The debugger doesn't have any native knowledge of the interpreter
19 (when (eq sb-ext:*evaluator-mode* :interpret)
20   (sb-ext:quit :unix-status 104))
21
22 \f
23 ;;;; Check that we get debug arglists right.
24
25 ;;; FIXME: This should use some get-argslist like functionality that
26 ;;; we actually export.
27 ;;;
28 ;;; Return the debug arglist of the function object FUN as a list, or
29 ;;; punt with :UNKNOWN.
30 (defun get-arglist (fun)
31   (declare (type function fun))
32   ;; The Lisp-level type FUNCTION can conceal a multitude of sins..
33   (case (sb-kernel:widetag-of fun)
34     (#.sb-vm:simple-fun-header-widetag
35       (sb-kernel:%simple-fun-arglist fun))
36     (#.sb-vm:closure-header-widetag (get-arglist
37                                      (sb-kernel:%closure-fun fun)))
38     ;; In code/describe.lisp, ll. 227 (%describe-fun), we use a scheme
39     ;; like above, and it seems to work. -- MNA 2001-06-12
40     ;;
41     ;; (There might be other cases with arglist info also.
42     ;; SIMPLE-FUN-HEADER-WIDETAG and CLOSURE-HEADER-WIDETAG just
43     ;; happen to be the two case that I had my nose rubbed in when
44     ;; debugging a GC problem caused by applying %SIMPLE-FUN-ARGLIST to
45     ;; a closure. -- WHN 2001-06-05)
46     (t
47      #+sb-eval
48      (if (typep fun 'sb-eval::interpreted-function)
49          (sb-eval::interpreted-function-lambda-list fun)
50          :unknown)
51      #-sb-eval
52      :unknown)))
53
54 (defun zoop (zeep &key beep)
55   blurp)
56 (assert (equal (get-arglist #'zoop) '(zeep &key beep)))
57
58 ;;; Check some predefined functions too.
59 ;;;
60 ;;; (We don't know exactly what the arguments are, e.g. the first
61 ;;; argument of PRINT might be SB-IMPL::OBJECT or SB-KERNEL::OBJ or
62 ;;; whatever. But we do know the general structure that a correct
63 ;;; answer should have, so we can safely do a lot of checks.)
64 (destructuring-bind (object-sym &optional-sym stream-sym) (get-arglist #'print)
65   (assert (symbolp object-sym))
66   (assert (eql &optional-sym '&optional))
67   (assert (symbolp stream-sym)))
68 (destructuring-bind (dest-sym control-sym &rest-sym format-args-sym)
69     (get-arglist #'format)
70   (assert (symbolp dest-sym))
71   (assert (symbolp control-sym))
72   (assert (eql &rest-sym '&rest))
73   (assert (symbolp format-args-sym)))
74
75 ;;; Check for backtraces generally being correct.  Ensure that the
76 ;;; actual backtrace finishes (doesn't signal any errors on its own),
77 ;;; and that it contains the frames we expect, doesn't contain any
78 ;;; "bogus stack frame"s, and contains the appropriate toplevel call
79 ;;; and hasn't been cut off anywhere.
80 (defun verify-backtrace (test-function frame-specs &key (allow-stunted nil))
81   (labels ((args-equal (want real)
82              (cond ((eq '&rest (car want))
83                     t)
84                    ((endp want)
85                     (endp real))
86                    ((or (eq '? (car want)) (equal (car want) (car real)))
87                     (args-equal (cdr want) (cdr real)))
88                    (t
89                     nil))))
90     (let ((result nil))
91       (block outer-handler
92         (handler-bind
93             ((error (lambda (condition)
94                       ;; find the part of the backtrace we're interested in
95                       (let* ((full-backtrace (sb-debug:backtrace-as-list))
96                              (backtrace (member (caar frame-specs) full-backtrace
97                                                 :key #'car
98                                                 :test #'equal)))
99
100                         (setf result condition)
101
102                         (unless backtrace
103                           (format t "~&//~S not in backtrace:~%   ~S~%"
104                                   (caar frame-specs)
105                                   full-backtrace)
106                           (setf result nil))
107
108                         ;; check that we have all the frames we wanted
109                         (mapcar
110                          (lambda (spec frame)
111                            (unless (or (not spec)
112                                        (and (equal (car spec) (car frame))
113                                             (args-equal (cdr spec)
114                                                         (cdr frame))))
115                              (print (list :wanted spec :got frame))
116                              (setf result nil)))
117                          frame-specs
118                          backtrace)
119
120                         ;; Make sure the backtrace isn't stunted in
121                         ;; any way.  (Depends on running in the main
122                         ;; thread.) FIXME: On Windows we get two
123                         ;; extra foreign frames below regular frames.
124                         (unless (find '(sb-impl::toplevel-init) backtrace
125                                       :test #'equal)
126                           (print (list :backtrace-stunted backtrace))
127                           (setf result nil))
128                         (return-from outer-handler)))))
129           (funcall test-function)))
130       result)))
131
132 (defvar *undefined-function-frame*
133   ;; bug 353
134   '(#+(or x86 x86-64) "bogus stack frame"
135     #-(or x86 x86-64) "undefined function"))
136
137 ;;; Test for "undefined function" (undefined_tramp) working properly.
138 ;;; Try it with and without tail call elimination, since they can have
139 ;;; different effects.  (Specifically, if undefined_tramp is incorrect
140 ;;; a stunted stack can result from the tail call variant.)
141 (flet ((optimized ()
142          (declare (optimize (speed 2) (debug 1))) ; tail call elimination
143          (#:undefined-function 42))
144        (not-optimized ()
145          (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
146          (#:undefined-function 42))
147        (test (fun)
148          (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
149          (funcall fun)))
150
151   (with-test (:name (:undefined-function :bug-346)
152               :fails-on '(or :alpha :ppc :sparc :mips
153                           (and :x86-64 :freebsd)))
154     (assert (verify-backtrace
155              (lambda () (test #'optimized))
156              (list *undefined-function-frame*
157                    (list '(flet test) #'optimized)))))
158
159   ;; bug 353: This test fails at least most of the time for x86/linux
160   ;; ca. 0.8.20.16. -- WHN
161   (with-test (:name (:undefined-function :bug-353)
162               ;; This used to have fewer :fails-on features pre-0.9.16.38,
163               ;; but it turns out that the bug was just being masked by
164               ;; the presence of the IR1 stepper instrumentation (and
165               ;; is thus again failing now that the instrumentation is
166               ;; no more).
167               :fails-on '(or :alpha :mips :ppc))
168     (assert (verify-backtrace
169              (lambda () (test #'not-optimized))
170              (list *undefined-function-frame*
171                    (list '(flet not-optimized))
172                    (list '(flet test) #'not-optimized))))))
173
174 ;;; Division by zero was a common error on PPC. It depended on the
175 ;;; return function either being before INTEGER-/-INTEGER in memory,
176 ;;; or more than MOST-POSITIVE-FIXNUM bytes ahead. It also depends on
177 ;;; INTEGER-/-INTEGER calling SIGNED-TRUNCATE. I believe Raymond Toy
178 ;;; says that the Sparc backend (at least for CMUCL) inlines this, so
179 ;;; if SBCL does the same this test is probably not good for the
180 ;;; Sparc.
181 ;;;
182 ;;; Disabling tail call elimination on this will probably ensure that
183 ;;; the return value (to the flet or the enclosing top level form) is
184 ;;; more than MOST-POSITIVE-FIXNUM with the current spaces on OS X.
185 ;;; Enabling it might catch other problems, so do it anyway.
186 (flet ((optimized ()
187          (declare (optimize (speed 2) (debug 1))) ; tail call elimination
188          (/ 42 0))
189        (not-optimized ()
190          (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
191          (/ 42 0))
192        (test (fun)
193          (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
194          (funcall fun)))
195   (with-test (:name (:divide-by-zero :bug-346)
196               :fails-on :alpha)  ; bug 346
197     (assert (verify-backtrace (lambda () (test #'optimized))
198                               (list '(/ 42 &rest)
199                                     (list '(flet test) #'optimized)))))
200   (with-test (:name (:divide-by-zero :bug-356)
201               :fails-on :alpha)  ; bug 356
202     (assert (verify-backtrace (lambda () (test #'not-optimized))
203                               (list '(/ 42 &rest)
204                                     '((flet not-optimized))
205                                     (list '(flet test) #'not-optimized))))))
206
207 (with-test (:name (:throw :no-such-tag)
208             :fails-on '(or
209                         (and :sparc :linux)
210                         :alpha
211                         :mips))
212   (progn
213     (defun throw-test ()
214       (throw 'no-such-tag t))
215     (assert (verify-backtrace #'throw-test '((throw-test))))))
216
217 ;;; test entry point handling in backtraces
218
219 (defun oops ()
220   (error "oops"))
221
222 (defmacro defbt (n ll &body body)
223   `(progn
224      ;; normal debug info
225      (defun ,(intern (format nil "BT.~A.1" n)) ,ll
226        ,@body)
227      ;; no arguments saved
228      (defun ,(intern (format nil "BT.~A.2" n)) ,ll
229        (declare (optimize (debug 1) (speed 3)))
230        ,@body)
231      ;; no lambda-list saved
232      (defun ,(intern (format nil "BT.~A.3" n)) ,ll
233        (declare (optimize (debug 0)))
234        ,@body)))
235
236 (defbt 1 (&key key)
237   (list key))
238
239 (defbt 2 (x)
240   (list x))
241
242 (defbt 3 (&key (key (oops)))
243   (list key))
244
245 ;;; ERROR instead of OOPS so that tail call elimination doesn't happen
246 (defbt 4 (&optional opt)
247   (list (error "error")))
248
249 (defbt 5 (&optional (opt (oops)))
250   (list opt))
251
252 (defmacro with-details (bool &body body)
253   `(let ((sb-debug:*show-entry-point-details* ,bool))
254      ,@body))
255
256 (defun bug-354 (x)
257   (error "XEPs in backtraces: ~S" x))
258
259 (with-test (:name :bug-354)
260   (with-details t
261     (assert (not (verify-backtrace (lambda () (bug-354 354))
262                                    '((bug-354 &rest)
263                                      ((sb-c::tl-xep bug-354) &rest))))))
264   (assert (verify-backtrace (lambda () (bug-354 354)) '((bug-354 354)))))
265
266 ;;; FIXME: This test really should be broken into smaller pieces
267 (with-test (:name (:backtrace :tl-xep))
268   (with-details t
269     (assert (verify-backtrace #'namestring
270                               '(((sb-c::tl-xep namestring) 0 ?)))))
271   (with-details nil
272     (assert (verify-backtrace #'namestring
273                               '((namestring))))))
274
275 (with-test (:name (:backtrace :more-processor))
276   (with-details t
277     (assert (verify-backtrace (lambda () (bt.1.1 :key))
278                               '(((sb-c::&more-processor bt.1.1) &rest))))
279     (assert (verify-backtrace (lambda () (bt.1.2 :key))
280                               '(((sb-c::&more-processor bt.1.2) &rest))))
281     (assert (verify-backtrace (lambda () (bt.1.3 :key))
282                               '(((sb-c::&more-processor bt.1.3) &rest)))))
283   (with-details nil
284     (assert (verify-backtrace (lambda () (bt.1.1 :key))
285                               '((bt.1.1 :key))))
286     (assert (verify-backtrace (lambda () (bt.1.2 :key))
287                               '((bt.1.2 &rest))))
288     (assert (verify-backtrace (lambda () (bt.1.3 :key))
289                               '((bt.1.3 &rest))))))
290
291 (with-test (:name (:backtrace :xep))
292   (with-details t
293     (assert (verify-backtrace #'bt.2.1
294                               '(((sb-c::xep bt.2.1) 0 ?))))
295     (assert (verify-backtrace #'bt.2.2
296                               '(((sb-c::xep bt.2.2) &rest))))
297     (assert (verify-backtrace #'bt.2.3
298                               '(((sb-c::xep bt.2.3) &rest)))))
299   (with-details nil
300     (assert (verify-backtrace #'bt.2.1
301                               '((bt.2.1))))
302     (assert (verify-backtrace #'bt.2.2
303                               '((bt.2.2 &rest))))
304     (assert (verify-backtrace #'bt.2.3
305                               '((bt.2.3 &rest))))))
306
307 (with-test (:name (:backtrace :varargs-entry))
308   (with-details t
309     (assert (verify-backtrace #'bt.3.1
310                               '(((sb-c::varargs-entry bt.3.1) :key nil))))
311     (assert (verify-backtrace #'bt.3.2
312                               '(((sb-c::varargs-entry bt.3.2) :key ?))))
313     (assert (verify-backtrace #'bt.3.3
314                               '(((sb-c::varargs-entry bt.3.3) &rest)))))
315   (with-details nil
316     (assert (verify-backtrace #'bt.3.1
317                               '((bt.3.1 :key nil))))
318     (assert (verify-backtrace #'bt.3.2
319                               '((bt.3.2 :key ?))))
320     (assert (verify-backtrace #'bt.3.3
321                               '((bt.3.3 &rest))))))
322
323 (with-test (:name (:backtrace :hairy-args-processor))
324   (with-details t
325     (assert (verify-backtrace #'bt.4.1
326                               '(((sb-c::hairy-arg-processor bt.4.1) ?))))
327     (assert (verify-backtrace #'bt.4.2
328                               '(((sb-c::hairy-arg-processor bt.4.2) ?))))
329     (assert (verify-backtrace #'bt.4.3
330                               '(((sb-c::hairy-arg-processor bt.4.3) &rest)))))
331   (with-details nil
332     (assert (verify-backtrace #'bt.4.1
333                               '((bt.4.1 ?))))
334     (assert (verify-backtrace #'bt.4.2
335                               '((bt.4.2 ?))))
336     (assert (verify-backtrace #'bt.4.3
337                               '((bt.4.3 &rest))))))
338
339
340 (with-test (:name (:backtrace :optional-processor))
341   (with-details t
342     (assert (verify-backtrace #'bt.5.1
343                               '(((sb-c::&optional-processor bt.5.1)))))
344     (assert (verify-backtrace #'bt.5.2
345                               '(((sb-c::&optional-processor bt.5.2) &rest))))
346     (assert (verify-backtrace #'bt.5.3
347                               '(((sb-c::&optional-processor bt.5.3) &rest)))))
348   (with-details nil
349     (assert (verify-backtrace #'bt.5.1
350                               '((bt.5.1))))
351     (assert (verify-backtrace #'bt.5.2
352                               '((bt.5.2 &rest))))
353     (assert (verify-backtrace #'bt.5.3
354                               '((bt.5.3 &rest))))))
355
356 (write-line "//compile nil")
357 (defvar *compile-nil-error* (compile nil '(lambda (x) (cons (when x (error "oops")) nil))))
358 (defvar *compile-nil-non-tc* (compile nil '(lambda (y) (cons (funcall *compile-nil-error* y) nil))))
359 (assert (verify-backtrace (lambda () (funcall *compile-nil-non-tc* 13))
360                           '(((lambda (x)) 13)
361                             ((lambda (y)) 13))))
362
363 (with-test (:name :clos-slot-typecheckfun-named)
364   (assert
365    (verify-backtrace
366     (lambda ()
367       (eval `(locally (declare (optimize safety))
368                (defclass clos-typecheck-test ()
369                  ((slot :type fixnum)))
370                (setf (slot-value (make-instance 'clos-typecheck-test) 'slot) t))))
371     '(((sb-pcl::slot-typecheck fixnum) t)))))
372
373 (with-test (:name :clos-emf-named)
374   (assert
375    (verify-backtrace
376     (lambda ()
377       (eval `(progn
378                (defmethod clos-emf-named-test ((x symbol)) x)
379                (defmethod clos-emf-named-test :before (x) (assert x))
380                (clos-emf-named-test nil))))
381     '(((sb-pcl::emf clos-emf-named-test) ? ? nil)))))
382
383 (with-test (:name :bug-310173)
384   (flet ((make-fun (n)
385            (let* ((names '(a b))
386                   (req (loop repeat n collect (pop names))))
387              (compile nil
388                       `(lambda (,@req &rest rest)
389                          (let ((* *)) ; no tail-call
390                            (apply '/ ,@req rest)))))))
391     (assert
392      (verify-backtrace (lambda ()
393                          (funcall (make-fun 0) 10 11 0))
394                        '((sb-kernel:two-arg-/ 10/11 0)
395                          (/ 10 11 0)
396                          ((lambda (&rest rest)) 10 11 0))))
397     (assert
398      (verify-backtrace (lambda ()
399                          (funcall (make-fun 1) 10 11 0))
400                        '((sb-kernel:two-arg-/ 10/11 0)
401                          (/ 10 11 0)
402                          ((lambda (a &rest rest)) 10 11 0))))
403     (assert
404      (verify-backtrace (lambda ()
405                          (funcall (make-fun 2) 10 11 0))
406                        '((sb-kernel:two-arg-/ 10/11 0)
407                          (/ 10 11 0)
408                          ((lambda (a b &rest rest)) 10 11 0))))))
409
410 ;;;; test TRACE
411
412 (defun trace-this ()
413   'ok)
414
415 (defun trace-fact (n)
416   (if (zerop n)
417       1
418       (* n (trace-fact (1- n)))))
419
420 (let ((out (with-output-to-string (*trace-output*)
421              (trace trace-this)
422              (assert (eq 'ok (trace-this)))
423              (untrace))))
424   (assert (search "TRACE-THIS" out))
425   (assert (search "returned OK" out)))
426
427 ;;; bug 379
428 ;;; This is not a WITH-TEST :FAILS-ON PPC DARWIN since there are
429 ;;; suspicions that the breakpoint trace might corrupt the whole image
430 ;;; on that platform.
431 (with-test (:name (trace :encapsulate nil)
432             :fails-on '(or (and :ppc (not :linux)) :sparc :mips)
433             :broken-on '(or :darwin :sunos))
434   (let ((out (with-output-to-string (*trace-output*)
435                (trace trace-this :encapsulate nil)
436                (assert (eq 'ok (trace-this)))
437                (untrace))))
438     (assert (search "TRACE-THIS" out))
439     (assert (search "returned OK" out))))
440
441 (with-test (:name (trace-recursive :encapsulate nil)
442             :fails-on '(or (and :ppc (not :linux)) :sparc :mips :sunos)
443             :broken-on '(or :darwin (and :x86 :sunos)))
444   (let ((out (with-output-to-string (*trace-output*)
445                (trace trace-fact :encapsulate nil)
446                (assert (= 120 (trace-fact 5)))
447                (untrace))))
448     (assert (search "TRACE-FACT" out))
449     (assert (search "returned 1" out))
450     (assert (search "returned 120" out))))
451
452 (defun trace-and-fmakunbound-this (x)
453   x)
454
455 (with-test (:name :bug-667657)
456   (trace trace-and-fmakunbound-this)
457   (fmakunbound 'trace-and-fmakunbound-this)
458   (untrace)
459   (assert (not (trace))))
460
461 (with-test (:name :bug-414)
462   (handler-bind ((warning #'error))
463     (load (compile-file "bug-414.lisp"))
464     (disassemble 'bug-414)))
465
466 (with-test (:name :bug-310175)
467   (let ((dx-arg (cons t t)))
468     (declare (dynamic-extent dx-arg))
469     (flet ((dx-arg-backtrace (x)
470              (declare (optimize (debug 2)))
471              (prog1 (sb-debug:backtrace-as-list 10)
472                (assert (sb-debug::stack-allocated-p x)))))
473       (declare (notinline dx-arg-backtrace))
474       (assert (member-if (lambda (frame)
475                            (and (consp frame)
476                                 (equal '(flet dx-arg-backtrace) (car frame))
477                                 (notany #'sb-debug::stack-allocated-p (cdr frame))))
478                          (dx-arg-backtrace dx-arg))))))
479
480 ;;;; test infinite error protection
481
482 (defmacro nest-errors (n-levels error-form)
483   (if (< 0 n-levels)
484       `(handler-bind ((error (lambda (condition)
485                                (declare (ignore condition))
486                                ,error-form)))
487         (nest-errors ,(1- n-levels) ,error-form))
488       error-form))
489
490 (defun erroring-debugger-hook (condition old-debugger-hook)
491   (let ((*debugger-hook* old-debugger-hook))
492     (format t "recursive condition: ~A~%" condition) (force-output)
493     (error "recursive condition: ~A" condition)))
494
495 (defun test-inifinite-error-protection ()
496   ;; after 50 successful throws to SB-IMPL::TOPLEVEL-CATCHER sbcl used
497   ;; to halt, it produces so much garbage that's hard to suppress that
498   ;; it is tested only once
499   (write-line "--HARMLESS BUT ALARMING BACKTRACE COMING UP--")
500   (let ((*debugger-hook* #'erroring-debugger-hook))
501     (loop repeat 1 do
502           (let ((error-counter 0)
503                 (*terminal-io* (make-broadcast-stream)))
504             (assert
505              (not (eq
506                    :normal-exit
507                    (catch 'sb-impl::toplevel-catcher
508                      (nest-errors 20 (error "infinite error ~s"
509                                             (incf error-counter)))
510                      :normal-exit)))))))
511   (write-line "--END OF H-B-A-B--"))
512
513 (enable-debugger)
514
515 (test-inifinite-error-protection)
516
517 #+sb-thread
518 (let ((thread (sb-thread:make-thread #'test-inifinite-error-protection)))
519   (loop while (sb-thread:thread-alive-p thread)))
520
521 (disable-debugger)
522
523 (write-line "/debug.impure.lisp done")