1 ;;;; This file is for testing debugging functionality, using
2 ;;;; test machinery which might have side effects (e.g.
5 ;;;; This software is part of the SBCL system. See the README file for
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
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.
16 (cl:in-package :cl-user)
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))
23 ;;;; Check that we get debug arglists right.
25 (defvar *p* (namestring *load-truename*))
27 ;;; FIXME: This should use some get-argslist like functionality that
28 ;;; we actually export.
30 ;;; Return the debug arglist of the function object FUN as a list, or
31 ;;; punt with :UNKNOWN.
32 (defun get-arglist (fun)
33 (declare (type function fun))
34 ;; The Lisp-level type FUNCTION can conceal a multitude of sins..
35 (case (sb-kernel:widetag-of fun)
36 (#.sb-vm:simple-fun-header-widetag
37 (sb-kernel:%simple-fun-arglist fun))
38 (#.sb-vm:closure-header-widetag (get-arglist
39 (sb-kernel:%closure-fun fun)))
40 ;; In code/describe.lisp, ll. 227 (%describe-fun), we use a scheme
41 ;; like above, and it seems to work. -- MNA 2001-06-12
43 ;; (There might be other cases with arglist info also.
44 ;; SIMPLE-FUN-HEADER-WIDETAG and CLOSURE-HEADER-WIDETAG just
45 ;; happen to be the two case that I had my nose rubbed in when
46 ;; debugging a GC problem caused by applying %SIMPLE-FUN-ARGLIST to
47 ;; a closure. -- WHN 2001-06-05)
50 (if (typep fun 'sb-eval::interpreted-function)
51 (sb-eval::interpreted-function-lambda-list fun)
56 (defun zoop (zeep &key beep)
58 (assert (equal (get-arglist #'zoop) '(zeep &key beep)))
60 ;;; Check some predefined functions too.
62 ;;; (We don't know exactly what the arguments are, e.g. the first
63 ;;; argument of PRINT might be SB-IMPL::OBJECT or SB-KERNEL::OBJ or
64 ;;; whatever. But we do know the general structure that a correct
65 ;;; answer should have, so we can safely do a lot of checks.)
66 (with-test (:name :predefined-functions-1)
67 (destructuring-bind (object-sym &optional-sym stream-sym) (get-arglist #'print)
68 (assert (symbolp object-sym))
69 (assert (eql &optional-sym '&optional))
70 (assert (symbolp stream-sym))))
71 (with-test (:name :predefined-functions-2)
72 (destructuring-bind (dest-sym control-sym &rest-sym format-args-sym)
73 (get-arglist #'format)
74 (assert (symbolp dest-sym))
75 (assert (symbolp control-sym))
76 (assert (eql &rest-sym '&rest))
77 (assert (symbolp format-args-sym))))
79 ;;; Check for backtraces generally being correct. Ensure that the
80 ;;; actual backtrace finishes (doesn't signal any errors on its own),
81 ;;; and that it contains the frames we expect, doesn't contain any
82 ;;; "bogus stack frame"s, and contains the appropriate toplevel call
83 ;;; and hasn't been cut off anywhere.
84 (defun verify-backtrace (test-function frame-specs &key (allow-stunted nil))
85 (labels ((args-equal (want real)
86 (cond ((eq '&rest (car want))
90 ((or (eq '? (car want)) (equal (car want) (car real)))
91 (args-equal (cdr want) (cdr real)))
97 ((error (lambda (condition)
98 ;; find the part of the backtrace we're interested in
99 (let* ((full-backtrace (sb-debug:backtrace-as-list))
100 (backtrace (member (caar frame-specs) full-backtrace
104 (setf result condition)
107 (format t "~&//~S not in backtrace:~% ~S~%"
111 ;; check that we have all the frames we wanted
114 (unless (or (not spec)
115 (and (equal (car spec) (car frame))
116 (args-equal (cdr spec)
118 (print (list :wanted spec :got frame))
123 ;; Make sure the backtrace isn't stunted in
124 ;; any way. (Depends on running in the main
125 ;; thread.) FIXME: On Windows we get two
126 ;; extra foreign frames below regular frames.
127 (unless (find '(sb-impl::toplevel-init) backtrace
129 (print (list :backtrace-stunted backtrace))
131 (return-from outer-handler)))))
132 (funcall test-function)))
135 (defvar *undefined-function-frame*
137 '("undefined function"))
139 ;;; Test for "undefined function" (undefined_tramp) working properly.
140 ;;; Try it with and without tail call elimination, since they can have
141 ;;; different effects. (Specifically, if undefined_tramp is incorrect
142 ;;; a stunted stack can result from the tail call variant.)
144 (declare (optimize (speed 2) (debug 1))) ; tail call elimination
145 (#:undefined-function 42))
147 (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
148 (#:undefined-function 42))
150 (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
153 (with-test (:name (:undefined-function :bug-346)
154 ;; Failures on ALPHA, SPARC, MIPS, and probably
155 ;; HPPA are due to not having a full and valid
156 ;; stack frame for the undefined function frame.
157 ;; See PPC undefined_tramp for details.
158 :fails-on '(or :alpha :sparc :mips
159 (and :x86-64 :freebsd)))
160 (assert (verify-backtrace
161 (lambda () (test #'optimized))
162 (list *undefined-function-frame*
163 (list `(flet test :in ,*p*) #'optimized)))))
165 ;; bug 353: This test fails at least most of the time for x86/linux
166 ;; ca. 0.8.20.16. -- WHN
167 (with-test (:name (:undefined-function :bug-353))
168 (assert (verify-backtrace
169 (lambda () (test #'not-optimized))
170 (list *undefined-function-frame*
171 (list `(flet not-optimized :in ,*p*))
172 (list `(flet test :in ,*p*) #'not-optimized))))))
174 (with-test (:name :backtrace-interrupted-condition-wait
175 :skipped-on '(not :sb-thread)
176 ;; For some unfathomable reason the backtrace becomes
177 ;; stunted on Darwin, ending at _sigtramp, when we add
178 ;; :TIMEOUT NIL to the frame we expect. If we leave it out,
179 ;; the backtrace is fine -- but the test fails. I can only
181 :fails-on '(or (and :x86 :linux) :darwin))
182 (let ((m (sb-thread:make-mutex))
183 (q (sb-thread:make-waitqueue)))
184 (assert (verify-backtrace
186 (sb-thread:with-mutex (m)
187 (handler-bind ((timeout (lambda (c)
190 (sb-thread:condition-wait q m)))))
191 `((sb-thread:condition-wait ,q ,m :timeout nil))))))
193 ;;; Division by zero was a common error on PPC. It depended on the
194 ;;; return function either being before INTEGER-/-INTEGER in memory,
195 ;;; or more than MOST-POSITIVE-FIXNUM bytes ahead. It also depends on
196 ;;; INTEGER-/-INTEGER calling SIGNED-TRUNCATE. I believe Raymond Toy
197 ;;; says that the Sparc backend (at least for CMUCL) inlines this, so
198 ;;; if SBCL does the same this test is probably not good for the
201 ;;; Disabling tail call elimination on this will probably ensure that
202 ;;; the return value (to the flet or the enclosing top level form) is
203 ;;; more than MOST-POSITIVE-FIXNUM with the current spaces on OS X.
204 ;;; Enabling it might catch other problems, so do it anyway.
206 (declare (optimize (speed 2) (debug 1))) ; tail call elimination
209 (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
212 (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
214 (with-test (:name (:divide-by-zero :bug-346)
215 :fails-on :alpha) ; bug 346
216 (assert (verify-backtrace (lambda () (test #'optimized))
218 (list `(flet test :in ,*p*) #'optimized)))))
219 (with-test (:name (:divide-by-zero :bug-356)
220 :fails-on :alpha) ; bug 356
221 (assert (verify-backtrace (lambda () (test #'not-optimized))
223 `((flet not-optimized :in ,*p*))
224 (list `(flet test :in ,*p*) #'not-optimized))))))
226 (with-test (:name (:throw :no-such-tag)
233 (throw 'no-such-tag t))
234 (assert (verify-backtrace #'throw-test '((throw-test))))))
236 (defun bug-308926 (x)
244 (with-test (:name :bug-308926)
245 (assert (verify-backtrace (lambda () (bug-308926 13))
246 '(((flet bar :in bug-308926) 13)
247 (bug-308926 &rest t)))))
249 ;;; test entry point handling in backtraces
254 (defmacro defbt (n ll &body body)
257 (defun ,(intern (format nil "BT.~A.1" n)) ,ll
259 ;; no arguments saved
260 (defun ,(intern (format nil "BT.~A.2" n)) ,ll
261 (declare (optimize (debug 1) (speed 3)))
263 ;; no lambda-list saved
264 (defun ,(intern (format nil "BT.~A.3" n)) ,ll
265 (declare (optimize (debug 0)))
274 (defbt 3 (&key (key (oops)))
277 ;;; ERROR instead of OOPS so that tail call elimination doesn't happen
278 (defbt 4 (&optional opt)
279 (list (error "error")))
281 (defbt 5 (&optional (opt (oops)))
284 (defmacro with-details (bool &body body)
285 `(let ((sb-debug:*show-entry-point-details* ,bool))
289 (error "XEPs in backtraces: ~S" x))
291 (with-test (:name :bug-354)
293 (assert (not (verify-backtrace (lambda () (bug-354 354))
295 ((sb-c::tl-xep bug-354) &rest))))))
296 (assert (verify-backtrace (lambda () (bug-354 354)) '((bug-354 354)))))
298 ;;; FIXME: This test really should be broken into smaller pieces
299 (with-test (:name (:backtrace :tl-xep))
301 (assert (verify-backtrace #'namestring
302 '(((sb-c::tl-xep namestring) 0 ?)))))
304 (assert (verify-backtrace #'namestring
307 (with-test (:name (:backtrace :more-processor))
309 (assert (verify-backtrace (lambda () (bt.1.1 :key))
310 '(((sb-c::&more-processor bt.1.1) &rest))))
311 (assert (verify-backtrace (lambda () (bt.1.2 :key))
312 '(((sb-c::&more-processor bt.1.2) &rest))))
313 (assert (verify-backtrace (lambda () (bt.1.3 :key))
314 '(((sb-c::&more-processor bt.1.3) &rest)))))
316 (assert (verify-backtrace (lambda () (bt.1.1 :key))
318 (assert (verify-backtrace (lambda () (bt.1.2 :key))
320 (assert (verify-backtrace (lambda () (bt.1.3 :key))
321 '((bt.1.3 &rest))))))
323 (with-test (:name (:backtrace :xep))
325 (assert (verify-backtrace #'bt.2.1
326 '(((sb-c::xep bt.2.1) 0 ?))))
327 (assert (verify-backtrace #'bt.2.2
328 '(((sb-c::xep bt.2.2) &rest))))
329 (assert (verify-backtrace #'bt.2.3
330 '(((sb-c::xep bt.2.3) &rest)))))
332 (assert (verify-backtrace #'bt.2.1
334 (assert (verify-backtrace #'bt.2.2
336 (assert (verify-backtrace #'bt.2.3
337 '((bt.2.3 &rest))))))
339 (with-test (:name (:backtrace :varargs-entry))
341 (assert (verify-backtrace #'bt.3.1
342 '(((sb-c::varargs-entry bt.3.1) :key nil))))
343 (assert (verify-backtrace #'bt.3.2
344 '(((sb-c::varargs-entry bt.3.2) :key ?))))
345 (assert (verify-backtrace #'bt.3.3
346 '(((sb-c::varargs-entry bt.3.3) &rest)))))
348 (assert (verify-backtrace #'bt.3.1
349 '((bt.3.1 :key nil))))
350 (assert (verify-backtrace #'bt.3.2
352 (assert (verify-backtrace #'bt.3.3
353 '((bt.3.3 &rest))))))
355 (with-test (:name (:backtrace :hairy-args-processor))
357 (assert (verify-backtrace #'bt.4.1
358 '(((sb-c::hairy-arg-processor bt.4.1) ?))))
359 (assert (verify-backtrace #'bt.4.2
360 '(((sb-c::hairy-arg-processor bt.4.2) ?))))
361 (assert (verify-backtrace #'bt.4.3
362 '(((sb-c::hairy-arg-processor bt.4.3) &rest)))))
364 (assert (verify-backtrace #'bt.4.1
366 (assert (verify-backtrace #'bt.4.2
368 (assert (verify-backtrace #'bt.4.3
369 '((bt.4.3 &rest))))))
372 (with-test (:name (:backtrace :optional-processor))
374 (assert (verify-backtrace #'bt.5.1
375 '(((sb-c::&optional-processor bt.5.1)))))
376 (assert (verify-backtrace #'bt.5.2
377 '(((sb-c::&optional-processor bt.5.2) &rest))))
378 (assert (verify-backtrace #'bt.5.3
379 '(((sb-c::&optional-processor bt.5.3) &rest)))))
381 (assert (verify-backtrace #'bt.5.1
383 (assert (verify-backtrace #'bt.5.2
385 (assert (verify-backtrace #'bt.5.3
386 '((bt.5.3 &rest))))))
388 (write-line "//compile nil")
389 (defvar *compile-nil-error* (compile nil '(lambda (x) (cons (when x (error "oops")) nil))))
390 (defvar *compile-nil-non-tc* (compile nil '(lambda (y) (cons (funcall *compile-nil-error* y) nil))))
391 (with-test (:name (:compile nil))
392 (assert (verify-backtrace (lambda () (funcall *compile-nil-non-tc* 13))
393 `(((lambda (x) :in ,*p*) 13)
394 ((lambda (y) :in ,*p*) 13)))))
396 (with-test (:name :clos-slot-typecheckfun-named)
400 (eval `(locally (declare (optimize safety))
401 (defclass clos-typecheck-test ()
402 ((slot :type fixnum)))
403 (setf (slot-value (make-instance 'clos-typecheck-test) 'slot) t))))
404 '(((sb-pcl::slot-typecheck fixnum) t)))))
406 (with-test (:name :clos-emf-named)
411 (defmethod clos-emf-named-test ((x symbol)) x)
412 (defmethod clos-emf-named-test :before (x) (assert x))
413 (clos-emf-named-test nil))))
414 '(((sb-pcl::emf clos-emf-named-test) ? ? nil)))))
416 (with-test (:name :bug-310173)
418 (let* ((names '(a b))
419 (req (loop repeat n collect (pop names))))
421 `(lambda (,@req &rest rest)
422 (let ((* *)) ; no tail-call
423 (apply '/ ,@req rest)))))))
425 (verify-backtrace (lambda ()
426 (funcall (make-fun 0) 10 11 0))
427 `((sb-kernel:two-arg-/ 10/11 0)
429 ((lambda (&rest rest) :in ,*p*) 10 11 0))))
431 (verify-backtrace (lambda ()
432 (funcall (make-fun 1) 10 11 0))
433 `((sb-kernel:two-arg-/ 10/11 0)
435 ((lambda (a &rest rest) :in ,*p*) 10 11 0))))
437 (verify-backtrace (lambda ()
438 (funcall (make-fun 2) 10 11 0))
439 `((sb-kernel:two-arg-/ 10/11 0)
441 ((lambda (a b &rest rest) :in ,*p*) 10 11 0))))))
448 (defun trace-fact (n)
451 (* n (trace-fact (1- n)))))
453 (with-test (:name (trace :simple))
454 (let ((out (with-output-to-string (*trace-output*)
456 (assert (eq 'ok (trace-this)))
458 (assert (search "TRACE-THIS" out))
459 (assert (search "returned OK" out))))
462 ;;; This is not a WITH-TEST :FAILS-ON PPC DARWIN since there are
463 ;;; suspicions that the breakpoint trace might corrupt the whole image
464 ;;; on that platform.
465 (with-test (:name (trace :encapsulate nil)
466 :fails-on '(or (and :ppc (not :linux)) :sparc :mips)
467 :broken-on '(or :darwin :sunos))
468 (let ((out (with-output-to-string (*trace-output*)
469 (trace trace-this :encapsulate nil)
470 (assert (eq 'ok (trace-this)))
472 (assert (search "TRACE-THIS" out))
473 (assert (search "returned OK" out))))
475 (with-test (:name (trace-recursive :encapsulate nil)
476 :fails-on '(or (and :ppc (not :linux)) :sparc :mips :sunos)
477 :broken-on '(or :darwin (and :x86 :sunos)))
478 (let ((out (with-output-to-string (*trace-output*)
479 (trace trace-fact :encapsulate nil)
480 (assert (= 120 (trace-fact 5)))
482 (assert (search "TRACE-FACT" out))
483 (assert (search "returned 1" out))
484 (assert (search "returned 120" out))))
486 (defun trace-and-fmakunbound-this (x)
489 (with-test (:name :bug-667657)
490 (trace trace-and-fmakunbound-this)
491 (fmakunbound 'trace-and-fmakunbound-this)
493 (assert (not (trace))))
495 (with-test (:name :bug-414)
496 (handler-bind ((warning #'error))
497 (load (compile-file "bug-414.lisp"))
498 (disassemble 'bug-414)))
500 (with-test (:name :bug-310175 :fails-on '(not :stack-allocatable-lists))
501 ;; KLUDGE: Not all DX-enabled platforms DX CONS, and the compiler
502 ;; transforms two-arg-LIST* (and one-arg-LIST) to CONS. Therefore,
503 ;; use two-arg-LIST, which should get through to VOP LIST, and thus
504 ;; stack-allocate on a predictable set of machines.
505 (let ((dx-arg (list t t)))
506 (declare (dynamic-extent dx-arg))
507 (flet ((dx-arg-backtrace (x)
508 (declare (optimize (debug 2)))
509 (prog1 (sb-debug:backtrace-as-list 10)
510 (assert (sb-debug::stack-allocated-p x)))))
511 (declare (notinline dx-arg-backtrace))
512 (assert (member-if (lambda (frame)
515 (equal '(flet dx-arg-backtrace :in) (butlast (car frame)))
516 (notany #'sb-debug::stack-allocated-p (cdr frame))))
517 (dx-arg-backtrace dx-arg))))))
519 (with-test (:name :bug-795245)
527 (sb-debug:backtrace 100 (make-broadcast-stream))
529 (throw 'done :error))
531 (throw 'done :ok))))))
532 (apply '/= nil 1 2 nil))))))
534 ;;;; test infinite error protection
536 (defmacro nest-errors (n-levels error-form)
538 `(handler-bind ((error (lambda (condition)
539 (declare (ignore condition))
541 (nest-errors ,(1- n-levels) ,error-form))
544 (defun erroring-debugger-hook (condition old-debugger-hook)
545 (let ((*debugger-hook* old-debugger-hook))
546 (format t "recursive condition: ~A~%" condition) (force-output)
547 (error "recursive condition: ~A" condition)))
549 (defun test-inifinite-error-protection ()
550 ;; after 50 successful throws to SB-IMPL::TOPLEVEL-CATCHER sbcl used
551 ;; to halt, it produces so much garbage that's hard to suppress that
552 ;; it is tested only once
553 (write-line "--HARMLESS BUT ALARMING BACKTRACE COMING UP--")
554 (let ((*debugger-hook* #'erroring-debugger-hook))
556 (let ((error-counter 0)
557 (*terminal-io* (make-broadcast-stream)))
561 (catch 'sb-impl::toplevel-catcher
562 (nest-errors 20 (error "infinite error ~s"
563 (incf error-counter)))
565 (write-line "--END OF H-B-A-B--"))
567 (with-test (:name infinite-error-protection)
569 (test-inifinite-error-protection))
571 (with-test (:name (infinite-error-protection :thread)
572 :skipped-on '(not :sb-thread))
574 (let ((thread (sb-thread:make-thread #'test-inifinite-error-protection)))
575 (loop while (sb-thread:thread-alive-p thread))))
577 ;; unconditional, in case either previous left it enabled
580 ;;;; test some limitations of MAKE-LISP-OBJ
582 ;;; Older GENCGC systems had a bug in the pointer validation used by
583 ;;; MAKE-LISP-OBJ that made SIMPLE-FUN objects always fail to
585 (with-test (:name (make-lisp-obj :simple-funs))
586 (sb-sys:without-gcing
587 (assert (eq #'identity
588 (sb-kernel:make-lisp-obj
589 (sb-kernel:get-lisp-obj-address
592 ;;; Older CHENEYGC systems didn't perform any real pointer validity
593 ;;; checks beyond "is this pointer to somewhere in heap space".
594 (with-test (:name (make-lisp-obj :pointer-validation))
595 ;; Fun and games: We need to test MAKE-LISP-OBJ with a known-bogus
596 ;; address, but we also need the GC to not pitch a fit if it sees an
597 ;; object with said bogus address. Thus, construct our known-bogus
598 ;; object within an area of unboxed storage (a vector) in static
599 ;; space. We'll make it a simple object, (CONS 0 0), which has an
600 ;; in-memory representation of two consecutive zero words. We
601 ;; allocate a three-word vector so that we can guarantee a
602 ;; double-word aligned double-word of zeros no matter what happens
603 ;; with the vector-data-offset (currently double-word aligned).
604 (let* ((memory (sb-int:make-static-vector 3 :element-type `(unsigned-byte ,sb-vm:n-word-bits)
606 (vector-data-address (sb-sys:sap-int (sb-kernel::vector-sap memory)))
607 (object-base-address (logandc2 (+ vector-data-address sb-vm:lowtag-mask) sb-vm:lowtag-mask))
608 (object-tagged-address (+ object-base-address sb-vm:list-pointer-lowtag)))
611 (sb-kernel:make-lisp-obj object-tagged-address nil)
612 (assert (not valid-p)))))
614 (write-line "/debug.impure.lisp done")