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 ;;; Division by zero was a common error on PPC. It depended on the
177 ;;; return function either being before INTEGER-/-INTEGER in memory,
178 ;;; or more than MOST-POSITIVE-FIXNUM bytes ahead. It also depends on
179 ;;; INTEGER-/-INTEGER calling SIGNED-TRUNCATE. I believe Raymond Toy
180 ;;; says that the Sparc backend (at least for CMUCL) inlines this, so
181 ;;; if SBCL does the same this test is probably not good for the
184 ;;; Disabling tail call elimination on this will probably ensure that
185 ;;; the return value (to the flet or the enclosing top level form) is
186 ;;; more than MOST-POSITIVE-FIXNUM with the current spaces on OS X.
187 ;;; Enabling it might catch other problems, so do it anyway.
189 (declare (optimize (speed 2) (debug 1))) ; tail call elimination
192 (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
195 (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
197 (with-test (:name (:divide-by-zero :bug-346)
198 :fails-on :alpha) ; bug 346
199 (assert (verify-backtrace (lambda () (test #'optimized))
201 (list '(flet test) #'optimized)))))
202 (with-test (:name (:divide-by-zero :bug-356)
203 :fails-on :alpha) ; bug 356
204 (assert (verify-backtrace (lambda () (test #'not-optimized))
206 '((flet not-optimized))
207 (list '(flet test) #'not-optimized))))))
209 (with-test (:name (:throw :no-such-tag)
216 (throw 'no-such-tag t))
217 (assert (verify-backtrace #'throw-test '((throw-test))))))
219 ;;; test entry point handling in backtraces
224 (defmacro defbt (n ll &body body)
227 (defun ,(intern (format nil "BT.~A.1" n)) ,ll
229 ;; no arguments saved
230 (defun ,(intern (format nil "BT.~A.2" n)) ,ll
231 (declare (optimize (debug 1) (speed 3)))
233 ;; no lambda-list saved
234 (defun ,(intern (format nil "BT.~A.3" n)) ,ll
235 (declare (optimize (debug 0)))
244 (defbt 3 (&key (key (oops)))
247 ;;; ERROR instead of OOPS so that tail call elimination doesn't happen
248 (defbt 4 (&optional opt)
249 (list (error "error")))
251 (defbt 5 (&optional (opt (oops)))
254 (defmacro with-details (bool &body body)
255 `(let ((sb-debug:*show-entry-point-details* ,bool))
259 (error "XEPs in backtraces: ~S" x))
261 (with-test (:name :bug-354)
263 (assert (not (verify-backtrace (lambda () (bug-354 354))
265 ((sb-c::tl-xep bug-354) &rest))))))
266 (assert (verify-backtrace (lambda () (bug-354 354)) '((bug-354 354)))))
268 ;;; FIXME: This test really should be broken into smaller pieces
269 (with-test (:name (:backtrace :tl-xep))
271 (assert (verify-backtrace #'namestring
272 '(((sb-c::tl-xep namestring) 0 ?)))))
274 (assert (verify-backtrace #'namestring
277 (with-test (:name (:backtrace :more-processor))
279 (assert (verify-backtrace (lambda () (bt.1.1 :key))
280 '(((sb-c::&more-processor bt.1.1) &rest))))
281 (assert (verify-backtrace (lambda () (bt.1.2 :key))
282 '(((sb-c::&more-processor bt.1.2) &rest))))
283 (assert (verify-backtrace (lambda () (bt.1.3 :key))
284 '(((sb-c::&more-processor bt.1.3) &rest)))))
286 (assert (verify-backtrace (lambda () (bt.1.1 :key))
288 (assert (verify-backtrace (lambda () (bt.1.2 :key))
290 (assert (verify-backtrace (lambda () (bt.1.3 :key))
291 '((bt.1.3 &rest))))))
293 (with-test (:name (:backtrace :xep))
295 (assert (verify-backtrace #'bt.2.1
296 '(((sb-c::xep bt.2.1) 0 ?))))
297 (assert (verify-backtrace #'bt.2.2
298 '(((sb-c::xep bt.2.2) &rest))))
299 (assert (verify-backtrace #'bt.2.3
300 '(((sb-c::xep bt.2.3) &rest)))))
302 (assert (verify-backtrace #'bt.2.1
304 (assert (verify-backtrace #'bt.2.2
306 (assert (verify-backtrace #'bt.2.3
307 '((bt.2.3 &rest))))))
309 (with-test (:name (:backtrace :varargs-entry))
311 (assert (verify-backtrace #'bt.3.1
312 '(((sb-c::varargs-entry bt.3.1) :key nil))))
313 (assert (verify-backtrace #'bt.3.2
314 '(((sb-c::varargs-entry bt.3.2) :key ?))))
315 (assert (verify-backtrace #'bt.3.3
316 '(((sb-c::varargs-entry bt.3.3) &rest)))))
318 (assert (verify-backtrace #'bt.3.1
319 '((bt.3.1 :key nil))))
320 (assert (verify-backtrace #'bt.3.2
322 (assert (verify-backtrace #'bt.3.3
323 '((bt.3.3 &rest))))))
325 (with-test (:name (:backtrace :hairy-args-processor))
327 (assert (verify-backtrace #'bt.4.1
328 '(((sb-c::hairy-arg-processor bt.4.1) ?))))
329 (assert (verify-backtrace #'bt.4.2
330 '(((sb-c::hairy-arg-processor bt.4.2) ?))))
331 (assert (verify-backtrace #'bt.4.3
332 '(((sb-c::hairy-arg-processor bt.4.3) &rest)))))
334 (assert (verify-backtrace #'bt.4.1
336 (assert (verify-backtrace #'bt.4.2
338 (assert (verify-backtrace #'bt.4.3
339 '((bt.4.3 &rest))))))
342 (with-test (:name (:backtrace :optional-processor))
344 (assert (verify-backtrace #'bt.5.1
345 '(((sb-c::&optional-processor bt.5.1)))))
346 (assert (verify-backtrace #'bt.5.2
347 '(((sb-c::&optional-processor bt.5.2) &rest))))
348 (assert (verify-backtrace #'bt.5.3
349 '(((sb-c::&optional-processor bt.5.3) &rest)))))
351 (assert (verify-backtrace #'bt.5.1
353 (assert (verify-backtrace #'bt.5.2
355 (assert (verify-backtrace #'bt.5.3
356 '((bt.5.3 &rest))))))
358 (write-line "//compile nil")
359 (defvar *compile-nil-error* (compile nil '(lambda (x) (cons (when x (error "oops")) nil))))
360 (defvar *compile-nil-non-tc* (compile nil '(lambda (y) (cons (funcall *compile-nil-error* y) nil))))
361 (with-test (:name (:compile nil))
362 (assert (verify-backtrace (lambda () (funcall *compile-nil-non-tc* 13))
364 ((lambda (y)) 13)))))
366 (with-test (:name :clos-slot-typecheckfun-named)
370 (eval `(locally (declare (optimize safety))
371 (defclass clos-typecheck-test ()
372 ((slot :type fixnum)))
373 (setf (slot-value (make-instance 'clos-typecheck-test) 'slot) t))))
374 '(((sb-pcl::slot-typecheck fixnum) t)))))
376 (with-test (:name :clos-emf-named)
381 (defmethod clos-emf-named-test ((x symbol)) x)
382 (defmethod clos-emf-named-test :before (x) (assert x))
383 (clos-emf-named-test nil))))
384 '(((sb-pcl::emf clos-emf-named-test) ? ? nil)))))
386 (with-test (:name :bug-310173)
388 (let* ((names '(a b))
389 (req (loop repeat n collect (pop names))))
391 `(lambda (,@req &rest rest)
392 (let ((* *)) ; no tail-call
393 (apply '/ ,@req rest)))))))
395 (verify-backtrace (lambda ()
396 (funcall (make-fun 0) 10 11 0))
397 '((sb-kernel:two-arg-/ 10/11 0)
399 ((lambda (&rest rest)) 10 11 0))))
401 (verify-backtrace (lambda ()
402 (funcall (make-fun 1) 10 11 0))
403 '((sb-kernel:two-arg-/ 10/11 0)
405 ((lambda (a &rest rest)) 10 11 0))))
407 (verify-backtrace (lambda ()
408 (funcall (make-fun 2) 10 11 0))
409 '((sb-kernel:two-arg-/ 10/11 0)
411 ((lambda (a b &rest rest)) 10 11 0))))))
418 (defun trace-fact (n)
421 (* n (trace-fact (1- n)))))
423 (with-test (:name (trace :simple))
424 (let ((out (with-output-to-string (*trace-output*)
426 (assert (eq 'ok (trace-this)))
428 (assert (search "TRACE-THIS" out))
429 (assert (search "returned OK" out))))
432 ;;; This is not a WITH-TEST :FAILS-ON PPC DARWIN since there are
433 ;;; suspicions that the breakpoint trace might corrupt the whole image
434 ;;; on that platform.
435 (with-test (:name (trace :encapsulate nil)
436 :fails-on '(or (and :ppc (not :linux)) :sparc :mips)
437 :broken-on '(or :darwin :sunos))
438 (let ((out (with-output-to-string (*trace-output*)
439 (trace trace-this :encapsulate nil)
440 (assert (eq 'ok (trace-this)))
442 (assert (search "TRACE-THIS" out))
443 (assert (search "returned OK" out))))
445 (with-test (:name (trace-recursive :encapsulate nil)
446 :fails-on '(or (and :ppc (not :linux)) :sparc :mips :sunos)
447 :broken-on '(or :darwin (and :x86 :sunos)))
448 (let ((out (with-output-to-string (*trace-output*)
449 (trace trace-fact :encapsulate nil)
450 (assert (= 120 (trace-fact 5)))
452 (assert (search "TRACE-FACT" out))
453 (assert (search "returned 1" out))
454 (assert (search "returned 120" out))))
456 (defun trace-and-fmakunbound-this (x)
459 (with-test (:name :bug-667657)
460 (trace trace-and-fmakunbound-this)
461 (fmakunbound 'trace-and-fmakunbound-this)
463 (assert (not (trace))))
465 (with-test (:name :bug-414)
466 (handler-bind ((warning #'error))
467 (load (compile-file "bug-414.lisp"))
468 (disassemble 'bug-414)))
470 (with-test (:name :bug-310175)
471 (let ((dx-arg (cons t t)))
472 (declare (dynamic-extent dx-arg))
473 (flet ((dx-arg-backtrace (x)
474 (declare (optimize (debug 2)))
475 (prog1 (sb-debug:backtrace-as-list 10)
476 (assert (sb-debug::stack-allocated-p x)))))
477 (declare (notinline dx-arg-backtrace))
478 (assert (member-if (lambda (frame)
480 (equal '(flet dx-arg-backtrace) (car frame))
481 (notany #'sb-debug::stack-allocated-p (cdr frame))))
482 (dx-arg-backtrace dx-arg))))))
484 (with-test (:name :bug-795245)
492 (sb-debug:backtrace 100 (make-broadcast-stream))
494 (throw 'done :error))
496 (throw 'done :ok))))))
497 (apply '/= nil 1 2 nil))))))
499 ;;;; test infinite error protection
501 (defmacro nest-errors (n-levels error-form)
503 `(handler-bind ((error (lambda (condition)
504 (declare (ignore condition))
506 (nest-errors ,(1- n-levels) ,error-form))
509 (defun erroring-debugger-hook (condition old-debugger-hook)
510 (let ((*debugger-hook* old-debugger-hook))
511 (format t "recursive condition: ~A~%" condition) (force-output)
512 (error "recursive condition: ~A" condition)))
514 (defun test-inifinite-error-protection ()
515 ;; after 50 successful throws to SB-IMPL::TOPLEVEL-CATCHER sbcl used
516 ;; to halt, it produces so much garbage that's hard to suppress that
517 ;; it is tested only once
518 (write-line "--HARMLESS BUT ALARMING BACKTRACE COMING UP--")
519 (let ((*debugger-hook* #'erroring-debugger-hook))
521 (let ((error-counter 0)
522 (*terminal-io* (make-broadcast-stream)))
526 (catch 'sb-impl::toplevel-catcher
527 (nest-errors 20 (error "infinite error ~s"
528 (incf error-counter)))
530 (write-line "--END OF H-B-A-B--"))
532 (with-test (:name infinite-error-protection)
534 (test-inifinite-error-protection))
536 (with-test (:name (infinite-error-protection :thread)
537 :skipped-on '(not :sb-thread))
539 (let ((thread (sb-thread:make-thread #'test-inifinite-error-protection)))
540 (loop while (sb-thread:thread-alive-p thread))))
542 ;; unconditional, in case either previous left it enabled
545 (write-line "/debug.impure.lisp done")