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 ;;; FIXME: This should use some get-argslist like functionality that
26 ;;; we actually export.
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
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)
48 (if (typep fun 'sb-eval::interpreted-function)
49 (sb-eval::interpreted-function-lambda-list fun)
54 (defun zoop (zeep &key beep)
56 (assert (equal (get-arglist #'zoop) '(zeep &key beep)))
58 ;;; Check some predefined functions too.
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 (with-test (:name :predefined-functions-1)
65 (destructuring-bind (object-sym &optional-sym stream-sym) (get-arglist #'print)
66 (assert (symbolp object-sym))
67 (assert (eql &optional-sym '&optional))
68 (assert (symbolp stream-sym))))
69 (with-test (:name :predefined-functions-2)
70 (destructuring-bind (dest-sym control-sym &rest-sym format-args-sym)
71 (get-arglist #'format)
72 (assert (symbolp dest-sym))
73 (assert (symbolp control-sym))
74 (assert (eql &rest-sym '&rest))
75 (assert (symbolp format-args-sym))))
77 ;;; Check for backtraces generally being correct. Ensure that the
78 ;;; actual backtrace finishes (doesn't signal any errors on its own),
79 ;;; and that it contains the frames we expect, doesn't contain any
80 ;;; "bogus stack frame"s, and contains the appropriate toplevel call
81 ;;; and hasn't been cut off anywhere.
82 (defun verify-backtrace (test-function frame-specs &key (allow-stunted nil))
83 (labels ((args-equal (want real)
84 (cond ((eq '&rest (car want))
88 ((or (eq '? (car want)) (equal (car want) (car real)))
89 (args-equal (cdr want) (cdr real)))
95 ((error (lambda (condition)
96 ;; find the part of the backtrace we're interested in
97 (let* ((full-backtrace (sb-debug:backtrace-as-list))
98 (backtrace (member (caar frame-specs) full-backtrace
102 (setf result condition)
105 (format t "~&//~S not in backtrace:~% ~S~%"
109 ;; check that we have all the frames we wanted
112 (unless (or (not spec)
113 (and (equal (car spec) (car frame))
114 (args-equal (cdr spec)
116 (print (list :wanted spec :got frame))
121 ;; Make sure the backtrace isn't stunted in
122 ;; any way. (Depends on running in the main
123 ;; thread.) FIXME: On Windows we get two
124 ;; extra foreign frames below regular frames.
125 (unless (find '(sb-impl::toplevel-init) backtrace
127 (print (list :backtrace-stunted backtrace))
129 (return-from outer-handler)))))
130 (funcall test-function)))
133 (defvar *undefined-function-frame*
135 '(#+(or x86 x86-64) "bogus stack frame"
136 #-(or x86 x86-64) "undefined function"))
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.)
143 (declare (optimize (speed 2) (debug 1))) ; tail call elimination
144 (#:undefined-function 42))
146 (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
147 (#:undefined-function 42))
149 (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
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)))))
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
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))))))
175 (with-test (:name :backtrace-interrupted-condition-wait
176 :skipped-on '(not :sb-thread)
177 ;; For some unfathomable reason the backtrace becomes
178 ;; stunted on Darwin, ending at _sigtramp, when we add
179 ;; :TIMEOUT NIL to the frame we expect. If we leave it out,
180 ;; the backtrace is fine -- but the test fails. I can only
183 (let ((m (sb-thread:make-mutex))
184 (q (sb-thread:make-waitqueue)))
185 (assert (verify-backtrace
187 (sb-thread:with-mutex (m)
188 (handler-bind ((timeout (lambda (c)
191 (sb-thread:condition-wait q m)))))
192 `((sb-thread:condition-wait ,q ,m :timeout nil))))))
194 ;;; Division by zero was a common error on PPC. It depended on the
195 ;;; return function either being before INTEGER-/-INTEGER in memory,
196 ;;; or more than MOST-POSITIVE-FIXNUM bytes ahead. It also depends on
197 ;;; INTEGER-/-INTEGER calling SIGNED-TRUNCATE. I believe Raymond Toy
198 ;;; says that the Sparc backend (at least for CMUCL) inlines this, so
199 ;;; if SBCL does the same this test is probably not good for the
202 ;;; Disabling tail call elimination on this will probably ensure that
203 ;;; the return value (to the flet or the enclosing top level form) is
204 ;;; more than MOST-POSITIVE-FIXNUM with the current spaces on OS X.
205 ;;; Enabling it might catch other problems, so do it anyway.
207 (declare (optimize (speed 2) (debug 1))) ; tail call elimination
210 (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
213 (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
215 (with-test (:name (:divide-by-zero :bug-346)
216 :fails-on :alpha) ; bug 346
217 (assert (verify-backtrace (lambda () (test #'optimized))
219 (list '(flet test) #'optimized)))))
220 (with-test (:name (:divide-by-zero :bug-356)
221 :fails-on :alpha) ; bug 356
222 (assert (verify-backtrace (lambda () (test #'not-optimized))
224 '((flet not-optimized))
225 (list '(flet test) #'not-optimized))))))
227 (with-test (:name (:throw :no-such-tag)
234 (throw 'no-such-tag t))
235 (assert (verify-backtrace #'throw-test '((throw-test))))))
237 ;;; test entry point handling in backtraces
242 (defmacro defbt (n ll &body body)
245 (defun ,(intern (format nil "BT.~A.1" n)) ,ll
247 ;; no arguments saved
248 (defun ,(intern (format nil "BT.~A.2" n)) ,ll
249 (declare (optimize (debug 1) (speed 3)))
251 ;; no lambda-list saved
252 (defun ,(intern (format nil "BT.~A.3" n)) ,ll
253 (declare (optimize (debug 0)))
262 (defbt 3 (&key (key (oops)))
265 ;;; ERROR instead of OOPS so that tail call elimination doesn't happen
266 (defbt 4 (&optional opt)
267 (list (error "error")))
269 (defbt 5 (&optional (opt (oops)))
272 (defmacro with-details (bool &body body)
273 `(let ((sb-debug:*show-entry-point-details* ,bool))
277 (error "XEPs in backtraces: ~S" x))
279 (with-test (:name :bug-354)
281 (assert (not (verify-backtrace (lambda () (bug-354 354))
283 ((sb-c::tl-xep bug-354) &rest))))))
284 (assert (verify-backtrace (lambda () (bug-354 354)) '((bug-354 354)))))
286 ;;; FIXME: This test really should be broken into smaller pieces
287 (with-test (:name (:backtrace :tl-xep))
289 (assert (verify-backtrace #'namestring
290 '(((sb-c::tl-xep namestring) 0 ?)))))
292 (assert (verify-backtrace #'namestring
295 (with-test (:name (:backtrace :more-processor))
297 (assert (verify-backtrace (lambda () (bt.1.1 :key))
298 '(((sb-c::&more-processor bt.1.1) &rest))))
299 (assert (verify-backtrace (lambda () (bt.1.2 :key))
300 '(((sb-c::&more-processor bt.1.2) &rest))))
301 (assert (verify-backtrace (lambda () (bt.1.3 :key))
302 '(((sb-c::&more-processor bt.1.3) &rest)))))
304 (assert (verify-backtrace (lambda () (bt.1.1 :key))
306 (assert (verify-backtrace (lambda () (bt.1.2 :key))
308 (assert (verify-backtrace (lambda () (bt.1.3 :key))
309 '((bt.1.3 &rest))))))
311 (with-test (:name (:backtrace :xep))
313 (assert (verify-backtrace #'bt.2.1
314 '(((sb-c::xep bt.2.1) 0 ?))))
315 (assert (verify-backtrace #'bt.2.2
316 '(((sb-c::xep bt.2.2) &rest))))
317 (assert (verify-backtrace #'bt.2.3
318 '(((sb-c::xep bt.2.3) &rest)))))
320 (assert (verify-backtrace #'bt.2.1
322 (assert (verify-backtrace #'bt.2.2
324 (assert (verify-backtrace #'bt.2.3
325 '((bt.2.3 &rest))))))
327 (with-test (:name (:backtrace :varargs-entry))
329 (assert (verify-backtrace #'bt.3.1
330 '(((sb-c::varargs-entry bt.3.1) :key nil))))
331 (assert (verify-backtrace #'bt.3.2
332 '(((sb-c::varargs-entry bt.3.2) :key ?))))
333 (assert (verify-backtrace #'bt.3.3
334 '(((sb-c::varargs-entry bt.3.3) &rest)))))
336 (assert (verify-backtrace #'bt.3.1
337 '((bt.3.1 :key nil))))
338 (assert (verify-backtrace #'bt.3.2
340 (assert (verify-backtrace #'bt.3.3
341 '((bt.3.3 &rest))))))
343 (with-test (:name (:backtrace :hairy-args-processor))
345 (assert (verify-backtrace #'bt.4.1
346 '(((sb-c::hairy-arg-processor bt.4.1) ?))))
347 (assert (verify-backtrace #'bt.4.2
348 '(((sb-c::hairy-arg-processor bt.4.2) ?))))
349 (assert (verify-backtrace #'bt.4.3
350 '(((sb-c::hairy-arg-processor bt.4.3) &rest)))))
352 (assert (verify-backtrace #'bt.4.1
354 (assert (verify-backtrace #'bt.4.2
356 (assert (verify-backtrace #'bt.4.3
357 '((bt.4.3 &rest))))))
360 (with-test (:name (:backtrace :optional-processor))
362 (assert (verify-backtrace #'bt.5.1
363 '(((sb-c::&optional-processor bt.5.1)))))
364 (assert (verify-backtrace #'bt.5.2
365 '(((sb-c::&optional-processor bt.5.2) &rest))))
366 (assert (verify-backtrace #'bt.5.3
367 '(((sb-c::&optional-processor bt.5.3) &rest)))))
369 (assert (verify-backtrace #'bt.5.1
371 (assert (verify-backtrace #'bt.5.2
373 (assert (verify-backtrace #'bt.5.3
374 '((bt.5.3 &rest))))))
376 (write-line "//compile nil")
377 (defvar *compile-nil-error* (compile nil '(lambda (x) (cons (when x (error "oops")) nil))))
378 (defvar *compile-nil-non-tc* (compile nil '(lambda (y) (cons (funcall *compile-nil-error* y) nil))))
379 (with-test (:name (:compile nil))
380 (assert (verify-backtrace (lambda () (funcall *compile-nil-non-tc* 13))
382 ((lambda (y)) 13)))))
384 (with-test (:name :clos-slot-typecheckfun-named)
388 (eval `(locally (declare (optimize safety))
389 (defclass clos-typecheck-test ()
390 ((slot :type fixnum)))
391 (setf (slot-value (make-instance 'clos-typecheck-test) 'slot) t))))
392 '(((sb-pcl::slot-typecheck fixnum) t)))))
394 (with-test (:name :clos-emf-named)
399 (defmethod clos-emf-named-test ((x symbol)) x)
400 (defmethod clos-emf-named-test :before (x) (assert x))
401 (clos-emf-named-test nil))))
402 '(((sb-pcl::emf clos-emf-named-test) ? ? nil)))))
404 (with-test (:name :bug-310173)
406 (let* ((names '(a b))
407 (req (loop repeat n collect (pop names))))
409 `(lambda (,@req &rest rest)
410 (let ((* *)) ; no tail-call
411 (apply '/ ,@req rest)))))))
413 (verify-backtrace (lambda ()
414 (funcall (make-fun 0) 10 11 0))
415 '((sb-kernel:two-arg-/ 10/11 0)
417 ((lambda (&rest rest)) 10 11 0))))
419 (verify-backtrace (lambda ()
420 (funcall (make-fun 1) 10 11 0))
421 '((sb-kernel:two-arg-/ 10/11 0)
423 ((lambda (a &rest rest)) 10 11 0))))
425 (verify-backtrace (lambda ()
426 (funcall (make-fun 2) 10 11 0))
427 '((sb-kernel:two-arg-/ 10/11 0)
429 ((lambda (a b &rest rest)) 10 11 0))))))
436 (defun trace-fact (n)
439 (* n (trace-fact (1- n)))))
441 (with-test (:name (trace :simple))
442 (let ((out (with-output-to-string (*trace-output*)
444 (assert (eq 'ok (trace-this)))
446 (assert (search "TRACE-THIS" out))
447 (assert (search "returned OK" out))))
450 ;;; This is not a WITH-TEST :FAILS-ON PPC DARWIN since there are
451 ;;; suspicions that the breakpoint trace might corrupt the whole image
452 ;;; on that platform.
453 (with-test (:name (trace :encapsulate nil)
454 :fails-on '(or (and :ppc (not :linux)) :sparc :mips)
455 :broken-on '(or :darwin :sunos))
456 (let ((out (with-output-to-string (*trace-output*)
457 (trace trace-this :encapsulate nil)
458 (assert (eq 'ok (trace-this)))
460 (assert (search "TRACE-THIS" out))
461 (assert (search "returned OK" out))))
463 (with-test (:name (trace-recursive :encapsulate nil)
464 :fails-on '(or (and :ppc (not :linux)) :sparc :mips :sunos)
465 :broken-on '(or :darwin (and :x86 :sunos)))
466 (let ((out (with-output-to-string (*trace-output*)
467 (trace trace-fact :encapsulate nil)
468 (assert (= 120 (trace-fact 5)))
470 (assert (search "TRACE-FACT" out))
471 (assert (search "returned 1" out))
472 (assert (search "returned 120" out))))
474 (defun trace-and-fmakunbound-this (x)
477 (with-test (:name :bug-667657)
478 (trace trace-and-fmakunbound-this)
479 (fmakunbound 'trace-and-fmakunbound-this)
481 (assert (not (trace))))
483 (with-test (:name :bug-414)
484 (handler-bind ((warning #'error))
485 (load (compile-file "bug-414.lisp"))
486 (disassemble 'bug-414)))
488 (with-test (:name :bug-310175 :fails-on '(not :stack-allocatable-lists))
489 ;; KLUDGE: Not all DX-enabled platforms DX CONS, and the compiler
490 ;; transforms two-arg-LIST* (and one-arg-LIST) to CONS. Therefore,
491 ;; use two-arg-LIST, which should get through to VOP LIST, and thus
492 ;; stack-allocate on a predictable set of machines.
493 (let ((dx-arg (list t t)))
494 (declare (dynamic-extent dx-arg))
495 (flet ((dx-arg-backtrace (x)
496 (declare (optimize (debug 2)))
497 (prog1 (sb-debug:backtrace-as-list 10)
498 (assert (sb-debug::stack-allocated-p x)))))
499 (declare (notinline dx-arg-backtrace))
500 (assert (member-if (lambda (frame)
502 (equal '(flet dx-arg-backtrace) (car frame))
503 (notany #'sb-debug::stack-allocated-p (cdr frame))))
504 (dx-arg-backtrace dx-arg))))))
506 (with-test (:name :bug-795245)
514 (sb-debug:backtrace 100 (make-broadcast-stream))
516 (throw 'done :error))
518 (throw 'done :ok))))))
519 (apply '/= nil 1 2 nil))))))
521 ;;;; test infinite error protection
523 (defmacro nest-errors (n-levels error-form)
525 `(handler-bind ((error (lambda (condition)
526 (declare (ignore condition))
528 (nest-errors ,(1- n-levels) ,error-form))
531 (defun erroring-debugger-hook (condition old-debugger-hook)
532 (let ((*debugger-hook* old-debugger-hook))
533 (format t "recursive condition: ~A~%" condition) (force-output)
534 (error "recursive condition: ~A" condition)))
536 (defun test-inifinite-error-protection ()
537 ;; after 50 successful throws to SB-IMPL::TOPLEVEL-CATCHER sbcl used
538 ;; to halt, it produces so much garbage that's hard to suppress that
539 ;; it is tested only once
540 (write-line "--HARMLESS BUT ALARMING BACKTRACE COMING UP--")
541 (let ((*debugger-hook* #'erroring-debugger-hook))
543 (let ((error-counter 0)
544 (*terminal-io* (make-broadcast-stream)))
548 (catch 'sb-impl::toplevel-catcher
549 (nest-errors 20 (error "infinite error ~s"
550 (incf error-counter)))
552 (write-line "--END OF H-B-A-B--"))
554 (with-test (:name infinite-error-protection)
556 (test-inifinite-error-protection))
558 (with-test (:name (infinite-error-protection :thread)
559 :skipped-on '(not :sb-thread))
561 (let ((thread (sb-thread:make-thread #'test-inifinite-error-protection)))
562 (loop while (sb-thread:thread-alive-p thread))))
564 ;; unconditional, in case either previous left it enabled
567 ;;;; test some limitations of MAKE-LISP-OBJ
569 ;;; Older GENCGC systems had a bug in the pointer validation used by
570 ;;; MAKE-LISP-OBJ that made SIMPLE-FUN objects always fail to
572 (with-test (:name (make-lisp-obj :simple-funs))
573 (sb-sys:without-gcing
574 (assert (eq #'identity
575 (sb-kernel:make-lisp-obj
576 (sb-kernel:get-lisp-obj-address
579 ;;; Older CHENEYGC systems didn't perform any real pointer validity
580 ;;; checks beyond "is this pointer to somewhere in heap space".
581 (with-test (:name (make-lisp-obj :pointer-validation))
582 ;; Fun and games: We need to test MAKE-LISP-OBJ with a known-bogus
583 ;; address, but we also need the GC to not pitch a fit if it sees an
584 ;; object with said bogus address. Thus, construct our known-bogus
585 ;; object within an area of unboxed storage (a vector) in static
586 ;; space. We'll make it a simple object, (CONS 0 0), which has an
587 ;; in-memory representation of two consecutive zero words. We
588 ;; allocate a three-word vector so that we can guarantee a
589 ;; double-word aligned double-word of zeros no matter what happens
590 ;; with the vector-data-offset (currently double-word aligned).
591 (let* ((memory (sb-int:make-static-vector 3 :element-type `(unsigned-byte ,sb-vm:n-word-bits)
593 (vector-data-address (sb-sys:sap-int (sb-kernel::vector-sap memory)))
594 (object-base-address (logandc2 (+ vector-data-address sb-vm:lowtag-mask) sb-vm:lowtag-mask))
595 (object-tagged-address (+ object-base-address sb-vm:list-pointer-lowtag)))
598 (sb-kernel:make-lisp-obj object-tagged-address nil)
599 (assert (not valid-p)))))
601 (write-line "/debug.impure.lisp done")