1.0.46.11: faster slot-accesses in the presence of SLOT-VALUE-USING-CLASS &co
[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 :mismatch spec 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                         (let ((end (last backtrace #-win32 2 #+win32 4)))
125                           (unless (equal (caar end)
126                                          'sb-impl::toplevel-init)
127                             (print (list :backtrace-stunted (caar end)))
128                             (setf result nil)))
129                         (return-from outer-handler)))))
130           (funcall test-function)))
131       result)))
132
133 (defvar *undefined-function-frame*
134   ;; bug 353
135   '(#+(or x86 x86-64) "bogus stack frame"
136     #-(or x86 x86-64) "undefined function"))
137
138 ;;; Test for "undefined function" (undefined_tramp) working properly.
139 ;;; Try it with and without tail call elimination, since they can have
140 ;;; different effects.  (Specifically, if undefined_tramp is incorrect
141 ;;; a stunted stack can result from the tail call variant.)
142 (flet ((optimized ()
143          (declare (optimize (speed 2) (debug 1))) ; tail call elimination
144          (#:undefined-function 42))
145        (not-optimized ()
146          (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
147          (#:undefined-function 42))
148        (test (fun)
149          (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
150          (funcall fun)))
151
152   (with-test (:name (:undefined-function :bug-346)
153               :fails-on '(or :alpha :ppc :sparc :mips
154                           (and :x86-64 :freebsd)))
155     (assert (verify-backtrace
156              (lambda () (test #'optimized))
157              (list *undefined-function-frame*
158                    (list '(flet test) #'optimized)))))
159
160   ;; bug 353: This test fails at least most of the time for x86/linux
161   ;; ca. 0.8.20.16. -- WHN
162   (with-test (:name (:undefined-function :bug-353)
163               ;; This used to have fewer :fails-on features pre-0.9.16.38,
164               ;; but it turns out that the bug was just being masked by
165               ;; the presence of the IR1 stepper instrumentation (and
166               ;; is thus again failing now that the instrumentation is
167               ;; no more).
168               :fails-on '(or :alpha :mips :ppc))
169     (assert (verify-backtrace
170              (lambda () (test #'not-optimized))
171              (list *undefined-function-frame*
172                    (list '(flet not-optimized))
173                    (list '(flet test) #'not-optimized))))))
174
175 ;;; Division by zero was a common error on PPC. It depended on the
176 ;;; return function either being before INTEGER-/-INTEGER in memory,
177 ;;; or more than MOST-POSITIVE-FIXNUM bytes ahead. It also depends on
178 ;;; INTEGER-/-INTEGER calling SIGNED-TRUNCATE. I believe Raymond Toy
179 ;;; says that the Sparc backend (at least for CMUCL) inlines this, so
180 ;;; if SBCL does the same this test is probably not good for the
181 ;;; Sparc.
182 ;;;
183 ;;; Disabling tail call elimination on this will probably ensure that
184 ;;; the return value (to the flet or the enclosing top level form) is
185 ;;; more than MOST-POSITIVE-FIXNUM with the current spaces on OS X.
186 ;;; Enabling it might catch other problems, so do it anyway.
187 (flet ((optimized ()
188          (declare (optimize (speed 2) (debug 1))) ; tail call elimination
189          (/ 42 0))
190        (not-optimized ()
191          (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
192          (/ 42 0))
193        (test (fun)
194          (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
195          (funcall fun)))
196   (with-test (:name (:divide-by-zero :bug-346)
197               :fails-on :alpha)  ; bug 346
198     (assert (verify-backtrace (lambda () (test #'optimized))
199                               (list '(/ 42 &rest)
200                                     (list '(flet test) #'optimized)))))
201   (with-test (:name (:divide-by-zero :bug-356)
202               :fails-on :alpha)  ; bug 356
203     (assert (verify-backtrace (lambda () (test #'not-optimized))
204                               (list '(/ 42 &rest)
205                                     '((flet not-optimized))
206                                     (list '(flet test) #'not-optimized))))))
207
208 (with-test (:name (:throw :no-such-tag)
209             :fails-on '(or
210                         (and :sparc :linux)
211                         :alpha
212                         :mips))
213   (progn
214     (defun throw-test ()
215       (throw 'no-such-tag t))
216     (assert (verify-backtrace #'throw-test '((throw-test))))))
217
218 ;;; test entry point handling in backtraces
219
220 (defun oops ()
221   (error "oops"))
222
223 (defmacro defbt (n ll &body body)
224   `(progn
225      ;; normal debug info
226      (defun ,(intern (format nil "BT.~A.1" n)) ,ll
227        ,@body)
228      ;; no arguments saved
229      (defun ,(intern (format nil "BT.~A.2" n)) ,ll
230        (declare (optimize (debug 1) (speed 3)))
231        ,@body)
232      ;; no lambda-list saved
233      (defun ,(intern (format nil "BT.~A.3" n)) ,ll
234        (declare (optimize (debug 0)))
235        ,@body)))
236
237 (defbt 1 (&key key)
238   (list key))
239
240 (defbt 2 (x)
241   (list x))
242
243 (defbt 3 (&key (key (oops)))
244   (list key))
245
246 ;;; ERROR instead of OOPS so that tail call elimination doesn't happen
247 (defbt 4 (&optional opt)
248   (list (error "error")))
249
250 (defbt 5 (&optional (opt (oops)))
251   (list opt))
252
253 (defmacro with-details (bool &body body)
254   `(let ((sb-debug:*show-entry-point-details* ,bool))
255      ,@body))
256
257 (defun bug-354 (x)
258   (error "XEPs in backtraces: ~S" x))
259
260 (with-test (:name :bug-354)
261   (with-details t
262     (assert (not (verify-backtrace (lambda () (bug-354 354))
263                                    '((bug-354 &rest)
264                                      ((sb-c::tl-xep bug-354) &rest))))))
265   (assert (verify-backtrace (lambda () (bug-354 354)) '((bug-354 354)))))
266
267 ;;; FIXME: This test really should be broken into smaller pieces
268 (with-test (:name (:backtrace :tl-xep)
269                   :fails-on '(and :x86 (or :sunos)))
270   (with-details t
271     (assert (verify-backtrace #'namestring
272                               '(((sb-c::tl-xep namestring) 0 ?)))))
273   (with-details nil
274     (assert (verify-backtrace #'namestring
275                               '((namestring))))))
276
277 (with-test (:name (:backtrace :more-processor)
278                   :fails-on '(and :x86 (or :sunos)))
279   (with-details t
280     (assert (verify-backtrace (lambda () (bt.1.1 :key))
281                               '(((sb-c::&more-processor bt.1.1) &rest))))
282     (assert (verify-backtrace (lambda () (bt.1.2 :key))
283                               '(((sb-c::&more-processor bt.1.2) &rest))))
284     (assert (verify-backtrace (lambda () (bt.1.3 :key))
285                               '(((sb-c::&more-processor bt.1.3) &rest)))))
286   (with-details nil
287     (assert (verify-backtrace (lambda () (bt.1.1 :key))
288                               '((bt.1.1 :key))))
289     (assert (verify-backtrace (lambda () (bt.1.2 :key))
290                               '((bt.1.2 &rest))))
291     (assert (verify-backtrace (lambda () (bt.1.3 :key))
292                               '((bt.1.3 &rest))))))
293
294 (with-test (:name (:backtrace :xep)
295                   :fails-on '(and :x86 (or :sunos)))
296   (with-details t
297     (assert (verify-backtrace #'bt.2.1
298                               '(((sb-c::xep bt.2.1) 0 ?))))
299     (assert (verify-backtrace #'bt.2.2
300                               '(((sb-c::xep bt.2.2) &rest))))
301     (assert (verify-backtrace #'bt.2.3
302                               '(((sb-c::xep bt.2.3) &rest)))))
303   (with-details nil
304     (assert (verify-backtrace #'bt.2.1
305                               '((bt.2.1))))
306     (assert (verify-backtrace #'bt.2.2
307                               '((bt.2.2 &rest))))
308     (assert (verify-backtrace #'bt.2.3
309                               '((bt.2.3 &rest))))))
310
311 (with-test (:name (:backtrace :varargs-entry)
312                   :fails-on '(and :x86 (or :sunos)))
313   (with-details t
314     (assert (verify-backtrace #'bt.3.1
315                               '(((sb-c::varargs-entry bt.3.1) :key nil))))
316     (assert (verify-backtrace #'bt.3.2
317                               '(((sb-c::varargs-entry bt.3.2) :key ?))))
318     (assert (verify-backtrace #'bt.3.3
319                               '(((sb-c::varargs-entry bt.3.3) &rest)))))
320   (with-details nil
321     (assert (verify-backtrace #'bt.3.1
322                               '((bt.3.1 :key nil))))
323     (assert (verify-backtrace #'bt.3.2
324                               '((bt.3.2 :key ?))))
325     (assert (verify-backtrace #'bt.3.3
326                               '((bt.3.3 &rest))))))
327
328 (with-test (:name (:backtrace :hairy-args-processor)
329                   :fails-on '(and :x86 (or :sunos)))
330   (with-details t
331     (assert (verify-backtrace #'bt.4.1
332                               '(((sb-c::hairy-arg-processor bt.4.1) ?))))
333     (assert (verify-backtrace #'bt.4.2
334                               '(((sb-c::hairy-arg-processor bt.4.2) ?))))
335     (assert (verify-backtrace #'bt.4.3
336                               '(((sb-c::hairy-arg-processor bt.4.3) &rest)))))
337   (with-details nil
338     (assert (verify-backtrace #'bt.4.1
339                               '((bt.4.1 ?))))
340     (assert (verify-backtrace #'bt.4.2
341                               '((bt.4.2 ?))))
342     (assert (verify-backtrace #'bt.4.3
343                               '((bt.4.3 &rest))))))
344
345
346 (with-test (:name (:backtrace :optional-processor)
347                   :fails-on '(and :x86 (or :sunos)))
348   (with-details t
349     (assert (verify-backtrace #'bt.5.1
350                               '(((sb-c::&optional-processor bt.5.1)))))
351     (assert (verify-backtrace #'bt.5.2
352                               '(((sb-c::&optional-processor bt.5.2) &rest))))
353     (assert (verify-backtrace #'bt.5.3
354                               '(((sb-c::&optional-processor bt.5.3) &rest)))))
355   (with-details nil
356     (assert (verify-backtrace #'bt.5.1
357                               '((bt.5.1))))
358     (assert (verify-backtrace #'bt.5.2
359                               '((bt.5.2 &rest))))
360     (assert (verify-backtrace #'bt.5.3
361                               '((bt.5.3 &rest))))))
362
363 (write-line "//compile nil")
364 (defvar *compile-nil-error* (compile nil '(lambda (x) (cons (when x (error "oops")) nil))))
365 (defvar *compile-nil-non-tc* (compile nil '(lambda (y) (cons (funcall *compile-nil-error* y) nil))))
366 (assert (verify-backtrace (lambda () (funcall *compile-nil-non-tc* 13))
367                           '(((lambda (x)) 13)
368                             ((lambda (y)) 13))))
369
370 (with-test (:name :clos-slot-typecheckfun-named)
371   (assert
372    (verify-backtrace
373     (lambda ()
374       (eval `(locally (declare (optimize safety))
375                (defclass clos-typecheck-test ()
376                  ((slot :type fixnum)))
377                (setf (slot-value (make-instance 'clos-typecheck-test) 'slot) t))))
378     '(((sb-pcl::slot-typecheck fixnum) t)))))
379
380 (with-test (:name :clos-emf-named)
381   (assert
382    (verify-backtrace
383     (lambda ()
384       (eval `(progn
385                (defmethod clos-emf-named-test ((x symbol)) x)
386                (defmethod clos-emf-named-test :before (x) (assert x))
387                (clos-emf-named-test nil))))
388     '(((sb-pcl::emf clos-emf-named-test) ? ? nil)))))
389
390 (with-test (:name :bug-310173)
391   (flet ((make-fun (n)
392            (let* ((names '(a b))
393                   (req (loop repeat n collect (pop names))))
394              (compile nil
395                       `(lambda (,@req &rest rest)
396                          (let ((* *)) ; no tail-call
397                            (apply '/ ,@req rest)))))))
398     (assert
399      (verify-backtrace (lambda ()
400                          (funcall (make-fun 0) 10 11 0))
401                        '((sb-kernel:two-arg-/ 10/11 0)
402                          (/ 10 11 0)
403                          ((lambda (&rest rest)) 10 11 0))))
404     (assert
405      (verify-backtrace (lambda ()
406                          (funcall (make-fun 1) 10 11 0))
407                        '((sb-kernel:two-arg-/ 10/11 0)
408                          (/ 10 11 0)
409                          ((lambda (a &rest rest)) 10 11 0))))
410     (assert
411      (verify-backtrace (lambda ()
412                          (funcall (make-fun 2) 10 11 0))
413                        '((sb-kernel:two-arg-/ 10/11 0)
414                          (/ 10 11 0)
415                          ((lambda (a b &rest rest)) 10 11 0))))))
416
417 ;;;; test TRACE
418
419 (defun trace-this ()
420   'ok)
421
422 (defun trace-fact (n)
423   (if (zerop n)
424       1
425       (* n (trace-fact (1- n)))))
426
427 (let ((out (with-output-to-string (*trace-output*)
428              (trace trace-this)
429              (assert (eq 'ok (trace-this)))
430              (untrace))))
431   (assert (search "TRACE-THIS" out))
432   (assert (search "returned OK" out)))
433
434 ;;; bug 379
435 ;;; This is not a WITH-TEST :FAILS-ON PPC DARWIN since there are
436 ;;; suspicions that the breakpoint trace might corrupt the whole image
437 ;;; on that platform.
438 #-(and (or ppc x86 x86-64) (or darwin sunos))
439 (with-test (:name (trace :encapsulate nil)
440             :fails-on '(or (and :ppc (not :linux)) :sparc :mips))
441   (let ((out (with-output-to-string (*trace-output*)
442                (trace trace-this :encapsulate nil)
443                (assert (eq 'ok (trace-this)))
444                (untrace))))
445     (assert (search "TRACE-THIS" out))
446     (assert (search "returned OK" out))))
447
448 #-(and (or ppc x86 x86-64) darwin)
449 (with-test (:name (trace-recursive :encapsulate nil)
450             :fails-on '(or (and :ppc (not :linux)) :sparc :mips))
451   (let ((out (with-output-to-string (*trace-output*)
452                (trace trace-fact :encapsulate nil)
453                (assert (= 120 (trace-fact 5)))
454                (untrace))))
455     (assert (search "TRACE-FACT" out))
456     (assert (search "returned 1" out))
457     (assert (search "returned 120" out))))
458
459 (defun trace-and-fmakunbound-this (x)
460   x)
461
462 (with-test (:name :bug-667657)
463   (trace trace-and-fmakunbound-this)
464   (fmakunbound 'trace-and-fmakunbound-this)
465   (untrace)
466   (assert (not (trace))))
467
468 (with-test (:name :bug-414)
469   (handler-bind ((warning #'error))
470     (load (compile-file "bug-414.lisp"))
471     (disassemble 'bug-414)))
472
473 (with-test (:name :bug-310175)
474   (let ((dx-arg (cons t t)))
475     (declare (dynamic-extent dx-arg))
476     (flet ((dx-arg-backtrace (x)
477              (declare (optimize (debug 2)))
478              (prog1 (sb-debug:backtrace-as-list 10)
479                (assert (sb-debug::stack-allocated-p x)))))
480       (declare (notinline dx-arg-backtrace))
481       (assert (member-if (lambda (frame)
482                            (and (consp frame)
483                                 (equal '(flet dx-arg-backtrace) (car frame))
484                                 (notany #'sb-debug::stack-allocated-p (cdr frame))))
485                          (dx-arg-backtrace dx-arg))))))
486
487 ;;;; test infinite error protection
488
489 (defmacro nest-errors (n-levels error-form)
490   (if (< 0 n-levels)
491       `(handler-bind ((error (lambda (condition)
492                                (declare (ignore condition))
493                                ,error-form)))
494         (nest-errors ,(1- n-levels) ,error-form))
495       error-form))
496
497 (defun erroring-debugger-hook (condition old-debugger-hook)
498   (let ((*debugger-hook* old-debugger-hook))
499     (format t "recursive condition: ~A~%" condition) (force-output)
500     (error "recursive condition: ~A" condition)))
501
502 (defun test-inifinite-error-protection ()
503   ;; after 50 successful throws to SB-IMPL::TOPLEVEL-CATCHER sbcl used
504   ;; to halt, it produces so much garbage that's hard to suppress that
505   ;; it is tested only once
506   (write-line "--HARMLESS BUT ALARMING BACKTRACE COMING UP--")
507   (let ((*debugger-hook* #'erroring-debugger-hook))
508     (loop repeat 1 do
509           (let ((error-counter 0)
510                 (*terminal-io* (make-broadcast-stream)))
511             (assert
512              (not (eq
513                    :normal-exit
514                    (catch 'sb-impl::toplevel-catcher
515                      (nest-errors 20 (error "infinite error ~s"
516                                             (incf error-counter)))
517                      :normal-exit)))))))
518   (write-line "--END OF H-B-A-B--"))
519
520 (enable-debugger)
521
522 (test-inifinite-error-protection)
523
524 #+sb-thread
525 (let ((thread (sb-thread:make-thread #'test-inifinite-error-protection)))
526   (loop while (sb-thread:thread-alive-p thread)))
527
528 (disable-debugger)
529
530 (write-line "/debug.impure.lisp done")