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~%"
110 ;; check that we have all the frames we wanted
113 (unless (or (not spec)
114 (and (equal (car spec) (car frame))
115 (args-equal (cdr spec)
117 (print (list :wanted spec :got frame))
122 ;; Make sure the backtrace isn't stunted in
123 ;; any way. (Depends on running in the main
124 ;; thread.) FIXME: On Windows we get two
125 ;; extra foreign frames below regular frames.
126 (unless (find '(sb-impl::toplevel-init) backtrace
128 (print (list :backtrace-stunted backtrace))
130 (return-from outer-handler)))))
131 (funcall test-function)))
134 (defvar *undefined-function-frame*
136 '(#+(or x86 x86-64) "bogus stack frame"
137 #-(or x86 x86-64) "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 :fails-on '(or :alpha :ppc :sparc :mips
155 (and :x86-64 :freebsd)))
156 (assert (verify-backtrace
157 (lambda () (test #'optimized))
158 (list *undefined-function-frame*
159 (list '(flet test) #'optimized)))))
161 ;; bug 353: This test fails at least most of the time for x86/linux
162 ;; ca. 0.8.20.16. -- WHN
163 (with-test (:name (:undefined-function :bug-353)
164 ;; This used to have fewer :fails-on features pre-0.9.16.38,
165 ;; but it turns out that the bug was just being masked by
166 ;; the presence of the IR1 stepper instrumentation (and
167 ;; is thus again failing now that the instrumentation is
169 :fails-on '(or :alpha :mips :ppc))
170 (assert (verify-backtrace
171 (lambda () (test #'not-optimized))
172 (list *undefined-function-frame*
173 (list '(flet not-optimized))
174 (list '(flet test) #'not-optimized))))))
176 (with-test (:name :backtrace-interrupted-condition-wait
177 :skipped-on '(not :sb-thread))
178 (let ((m (sb-thread:make-mutex))
179 (q (sb-thread:make-waitqueue)))
180 (assert (verify-backtrace
182 (sb-thread:with-mutex (m)
183 (handler-bind ((timeout (lambda (c)
186 (sb-thread:condition-wait q m)))))
187 `((sb-thread:condition-wait ,q ,m))))))
189 ;;; Division by zero was a common error on PPC. It depended on the
190 ;;; return function either being before INTEGER-/-INTEGER in memory,
191 ;;; or more than MOST-POSITIVE-FIXNUM bytes ahead. It also depends on
192 ;;; INTEGER-/-INTEGER calling SIGNED-TRUNCATE. I believe Raymond Toy
193 ;;; says that the Sparc backend (at least for CMUCL) inlines this, so
194 ;;; if SBCL does the same this test is probably not good for the
197 ;;; Disabling tail call elimination on this will probably ensure that
198 ;;; the return value (to the flet or the enclosing top level form) is
199 ;;; more than MOST-POSITIVE-FIXNUM with the current spaces on OS X.
200 ;;; Enabling it might catch other problems, so do it anyway.
202 (declare (optimize (speed 2) (debug 1))) ; tail call elimination
205 (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
208 (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
210 (with-test (:name (:divide-by-zero :bug-346)
211 :fails-on :alpha) ; bug 346
212 (assert (verify-backtrace (lambda () (test #'optimized))
214 (list '(flet test) #'optimized)))))
215 (with-test (:name (:divide-by-zero :bug-356)
216 :fails-on :alpha) ; bug 356
217 (assert (verify-backtrace (lambda () (test #'not-optimized))
219 '((flet not-optimized))
220 (list '(flet test) #'not-optimized))))))
222 (with-test (:name (:throw :no-such-tag)
229 (throw 'no-such-tag t))
230 (assert (verify-backtrace #'throw-test '((throw-test))))))
232 ;;; test entry point handling in backtraces
237 (defmacro defbt (n ll &body body)
240 (defun ,(intern (format nil "BT.~A.1" n)) ,ll
242 ;; no arguments saved
243 (defun ,(intern (format nil "BT.~A.2" n)) ,ll
244 (declare (optimize (debug 1) (speed 3)))
246 ;; no lambda-list saved
247 (defun ,(intern (format nil "BT.~A.3" n)) ,ll
248 (declare (optimize (debug 0)))
257 (defbt 3 (&key (key (oops)))
260 ;;; ERROR instead of OOPS so that tail call elimination doesn't happen
261 (defbt 4 (&optional opt)
262 (list (error "error")))
264 (defbt 5 (&optional (opt (oops)))
267 (defmacro with-details (bool &body body)
268 `(let ((sb-debug:*show-entry-point-details* ,bool))
272 (error "XEPs in backtraces: ~S" x))
274 (with-test (:name :bug-354)
276 (assert (not (verify-backtrace (lambda () (bug-354 354))
278 ((sb-c::tl-xep bug-354) &rest))))))
279 (assert (verify-backtrace (lambda () (bug-354 354)) '((bug-354 354)))))
281 ;;; FIXME: This test really should be broken into smaller pieces
282 (with-test (:name (:backtrace :tl-xep))
284 (assert (verify-backtrace #'namestring
285 '(((sb-c::tl-xep namestring) 0 ?)))))
287 (assert (verify-backtrace #'namestring
290 (with-test (:name (:backtrace :more-processor))
292 (assert (verify-backtrace (lambda () (bt.1.1 :key))
293 '(((sb-c::&more-processor bt.1.1) &rest))))
294 (assert (verify-backtrace (lambda () (bt.1.2 :key))
295 '(((sb-c::&more-processor bt.1.2) &rest))))
296 (assert (verify-backtrace (lambda () (bt.1.3 :key))
297 '(((sb-c::&more-processor bt.1.3) &rest)))))
299 (assert (verify-backtrace (lambda () (bt.1.1 :key))
301 (assert (verify-backtrace (lambda () (bt.1.2 :key))
303 (assert (verify-backtrace (lambda () (bt.1.3 :key))
304 '((bt.1.3 &rest))))))
306 (with-test (:name (:backtrace :xep))
308 (assert (verify-backtrace #'bt.2.1
309 '(((sb-c::xep bt.2.1) 0 ?))))
310 (assert (verify-backtrace #'bt.2.2
311 '(((sb-c::xep bt.2.2) &rest))))
312 (assert (verify-backtrace #'bt.2.3
313 '(((sb-c::xep bt.2.3) &rest)))))
315 (assert (verify-backtrace #'bt.2.1
317 (assert (verify-backtrace #'bt.2.2
319 (assert (verify-backtrace #'bt.2.3
320 '((bt.2.3 &rest))))))
322 (with-test (:name (:backtrace :varargs-entry))
324 (assert (verify-backtrace #'bt.3.1
325 '(((sb-c::varargs-entry bt.3.1) :key nil))))
326 (assert (verify-backtrace #'bt.3.2
327 '(((sb-c::varargs-entry bt.3.2) :key ?))))
328 (assert (verify-backtrace #'bt.3.3
329 '(((sb-c::varargs-entry bt.3.3) &rest)))))
331 (assert (verify-backtrace #'bt.3.1
332 '((bt.3.1 :key nil))))
333 (assert (verify-backtrace #'bt.3.2
335 (assert (verify-backtrace #'bt.3.3
336 '((bt.3.3 &rest))))))
338 (with-test (:name (:backtrace :hairy-args-processor))
340 (assert (verify-backtrace #'bt.4.1
341 '(((sb-c::hairy-arg-processor bt.4.1) ?))))
342 (assert (verify-backtrace #'bt.4.2
343 '(((sb-c::hairy-arg-processor bt.4.2) ?))))
344 (assert (verify-backtrace #'bt.4.3
345 '(((sb-c::hairy-arg-processor bt.4.3) &rest)))))
347 (assert (verify-backtrace #'bt.4.1
349 (assert (verify-backtrace #'bt.4.2
351 (assert (verify-backtrace #'bt.4.3
352 '((bt.4.3 &rest))))))
355 (with-test (:name (:backtrace :optional-processor))
357 (assert (verify-backtrace #'bt.5.1
358 '(((sb-c::&optional-processor bt.5.1)))))
359 (assert (verify-backtrace #'bt.5.2
360 '(((sb-c::&optional-processor bt.5.2) &rest))))
361 (assert (verify-backtrace #'bt.5.3
362 '(((sb-c::&optional-processor bt.5.3) &rest)))))
364 (assert (verify-backtrace #'bt.5.1
366 (assert (verify-backtrace #'bt.5.2
368 (assert (verify-backtrace #'bt.5.3
369 '((bt.5.3 &rest))))))
371 (write-line "//compile nil")
372 (defvar *compile-nil-error* (compile nil '(lambda (x) (cons (when x (error "oops")) nil))))
373 (defvar *compile-nil-non-tc* (compile nil '(lambda (y) (cons (funcall *compile-nil-error* y) nil))))
374 (with-test (:name (:compile nil))
375 (assert (verify-backtrace (lambda () (funcall *compile-nil-non-tc* 13))
377 ((lambda (y)) 13)))))
379 (with-test (:name :clos-slot-typecheckfun-named)
383 (eval `(locally (declare (optimize safety))
384 (defclass clos-typecheck-test ()
385 ((slot :type fixnum)))
386 (setf (slot-value (make-instance 'clos-typecheck-test) 'slot) t))))
387 '(((sb-pcl::slot-typecheck fixnum) t)))))
389 (with-test (:name :clos-emf-named)
394 (defmethod clos-emf-named-test ((x symbol)) x)
395 (defmethod clos-emf-named-test :before (x) (assert x))
396 (clos-emf-named-test nil))))
397 '(((sb-pcl::emf clos-emf-named-test) ? ? nil)))))
399 (with-test (:name :bug-310173)
401 (let* ((names '(a b))
402 (req (loop repeat n collect (pop names))))
404 `(lambda (,@req &rest rest)
405 (let ((* *)) ; no tail-call
406 (apply '/ ,@req rest)))))))
408 (verify-backtrace (lambda ()
409 (funcall (make-fun 0) 10 11 0))
410 '((sb-kernel:two-arg-/ 10/11 0)
412 ((lambda (&rest rest)) 10 11 0))))
414 (verify-backtrace (lambda ()
415 (funcall (make-fun 1) 10 11 0))
416 '((sb-kernel:two-arg-/ 10/11 0)
418 ((lambda (a &rest rest)) 10 11 0))))
420 (verify-backtrace (lambda ()
421 (funcall (make-fun 2) 10 11 0))
422 '((sb-kernel:two-arg-/ 10/11 0)
424 ((lambda (a b &rest rest)) 10 11 0))))))
431 (defun trace-fact (n)
434 (* n (trace-fact (1- n)))))
436 (with-test (:name (trace :simple))
437 (let ((out (with-output-to-string (*trace-output*)
439 (assert (eq 'ok (trace-this)))
441 (assert (search "TRACE-THIS" out))
442 (assert (search "returned OK" out))))
445 ;;; This is not a WITH-TEST :FAILS-ON PPC DARWIN since there are
446 ;;; suspicions that the breakpoint trace might corrupt the whole image
447 ;;; on that platform.
448 (with-test (:name (trace :encapsulate nil)
449 :fails-on '(or (and :ppc (not :linux)) :sparc :mips)
450 :broken-on '(or :darwin :sunos))
451 (let ((out (with-output-to-string (*trace-output*)
452 (trace trace-this :encapsulate nil)
453 (assert (eq 'ok (trace-this)))
455 (assert (search "TRACE-THIS" out))
456 (assert (search "returned OK" out))))
458 (with-test (:name (trace-recursive :encapsulate nil)
459 :fails-on '(or (and :ppc (not :linux)) :sparc :mips :sunos)
460 :broken-on '(or :darwin (and :x86 :sunos)))
461 (let ((out (with-output-to-string (*trace-output*)
462 (trace trace-fact :encapsulate nil)
463 (assert (= 120 (trace-fact 5)))
465 (assert (search "TRACE-FACT" out))
466 (assert (search "returned 1" out))
467 (assert (search "returned 120" out))))
469 (defun trace-and-fmakunbound-this (x)
472 (with-test (:name :bug-667657)
473 (trace trace-and-fmakunbound-this)
474 (fmakunbound 'trace-and-fmakunbound-this)
476 (assert (not (trace))))
478 (with-test (:name :bug-414)
479 (handler-bind ((warning #'error))
480 (load (compile-file "bug-414.lisp"))
481 (disassemble 'bug-414)))
483 (with-test (:name :bug-310175)
484 (let ((dx-arg (cons t t)))
485 (declare (dynamic-extent dx-arg))
486 (flet ((dx-arg-backtrace (x)
487 (declare (optimize (debug 2)))
488 (prog1 (sb-debug:backtrace-as-list 10)
489 (assert (sb-debug::stack-allocated-p x)))))
490 (declare (notinline dx-arg-backtrace))
491 (assert (member-if (lambda (frame)
493 (equal '(flet dx-arg-backtrace) (car frame))
494 (notany #'sb-debug::stack-allocated-p (cdr frame))))
495 (dx-arg-backtrace dx-arg))))))
497 (with-test (:name :bug-795245)
505 (sb-debug:backtrace 100 (make-broadcast-stream))
507 (throw 'done :error))
509 (throw 'done :ok))))))
510 (apply '/= nil 1 2 nil))))))
512 ;;;; test infinite error protection
514 (defmacro nest-errors (n-levels error-form)
516 `(handler-bind ((error (lambda (condition)
517 (declare (ignore condition))
519 (nest-errors ,(1- n-levels) ,error-form))
522 (defun erroring-debugger-hook (condition old-debugger-hook)
523 (let ((*debugger-hook* old-debugger-hook))
524 (format t "recursive condition: ~A~%" condition) (force-output)
525 (error "recursive condition: ~A" condition)))
527 (defun test-inifinite-error-protection ()
528 ;; after 50 successful throws to SB-IMPL::TOPLEVEL-CATCHER sbcl used
529 ;; to halt, it produces so much garbage that's hard to suppress that
530 ;; it is tested only once
531 (write-line "--HARMLESS BUT ALARMING BACKTRACE COMING UP--")
532 (let ((*debugger-hook* #'erroring-debugger-hook))
534 (let ((error-counter 0)
535 (*terminal-io* (make-broadcast-stream)))
539 (catch 'sb-impl::toplevel-catcher
540 (nest-errors 20 (error "infinite error ~s"
541 (incf error-counter)))
543 (write-line "--END OF H-B-A-B--"))
545 (with-test (:name infinite-error-protection)
547 (test-inifinite-error-protection))
549 (with-test (:name (infinite-error-protection :thread)
550 :skipped-on '(not :sb-thread))
552 (let ((thread (sb-thread:make-thread #'test-inifinite-error-protection)))
553 (loop while (sb-thread:thread-alive-p thread))))
555 ;; unconditional, in case either previous left it enabled
558 (write-line "/debug.impure.lisp done")