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 ;;;; Check that we get debug arglists right.
20 ;;; FIXME: This should use some get-argslist like functionality that
21 ;;; we actually export.
23 ;;; Return the debug arglist of the function object FUN as a list, or
24 ;;; punt with :UNKNOWN.
25 (defun get-arglist (fun)
26 (declare (type function fun))
27 ;; The Lisp-level type FUNCTION can conceal a multitude of sins..
28 (case (sb-kernel:widetag-of fun)
29 (#.sb-vm:simple-fun-header-widetag
30 (sb-kernel:%simple-fun-arglist fun))
31 (#.sb-vm:closure-header-widetag (get-arglist
32 (sb-kernel:%closure-fun fun)))
33 ;; In code/describe.lisp, ll. 227 (%describe-fun), we use a scheme
34 ;; like above, and it seems to work. -- MNA 2001-06-12
36 ;; (There might be other cases with arglist info also.
37 ;; SIMPLE-FUN-HEADER-WIDETAG and CLOSURE-HEADER-WIDETAG just
38 ;; happen to be the two case that I had my nose rubbed in when
39 ;; debugging a GC problem caused by applying %SIMPLE-FUN-ARGLIST to
40 ;; a closure. -- WHN 2001-06-05)
43 (defun zoop (zeep &key beep)
45 (assert (equal (get-arglist #'zoop) '(zeep &key beep)))
47 ;;; Check some predefined functions too.
49 ;;; (We don't know exactly what the arguments are, e.g. the first
50 ;;; argument of PRINT might be SB-IMPL::OBJECT or SB-KERNEL::OBJ or
51 ;;; whatever. But we do know the general structure that a correct
52 ;;; answer should have, so we can safely do a lot of checks.)
53 (destructuring-bind (object-sym &optional-sym stream-sym) (get-arglist #'print)
54 (assert (symbolp object-sym))
55 (assert (eql &optional-sym '&optional))
56 (assert (symbolp stream-sym)))
57 (destructuring-bind (dest-sym control-sym &rest-sym format-args-sym)
58 (get-arglist #'format)
59 (assert (symbolp dest-sym))
60 (assert (symbolp control-sym))
61 (assert (eql &rest-sym '&rest))
62 (assert (symbolp format-args-sym)))
64 ;;; Check for backtraces generally being correct. Ensure that the
65 ;;; actual backtrace finishes (doesn't signal any errors on its own),
66 ;;; and that it contains the frames we expect, doesn't contain any
67 ;;; "bogus stack frame"s, and contains the appropriate toplevel call
68 ;;; and hasn't been cut off anywhere.
69 (defun verify-backtrace (test-function frame-specs &key (allow-stunted nil))
70 (labels ((args-equal (want real)
71 (cond ((eq '&rest (car want))
75 ((or (eq '? (car want)) (equal (car want) (car real)))
76 (args-equal (cdr want) (cdr real)))
82 ((error (lambda (condition)
83 ;; find the part of the backtrace we're interested in
84 (let ((backtrace (progn
86 (member (caar frame-specs)
87 (sb-debug:backtrace-as-list)
91 (setf result condition)
94 (print :missing-backtrace)
97 ;; check that we have all the frames we wanted
100 (unless (or (not spec)
101 (and (equal (car spec) (car frame))
102 (args-equal (cdr spec)
104 (print (list :mismatch spec frame))
109 ;; Make sure the backtrace isn't stunted in
110 ;; any way. (Depends on running in the main
112 (let ((end (last backtrace 2)))
113 (unless (equal (caar end)
114 (if *show-entry-point-details*
115 '(sb-c::tl-xep sb-impl::toplevel-init)
116 'sb-impl::toplevel-init))
117 (print (list :backtrace-stunted (caar end)))
119 (return-from outer-handler)))))
120 (funcall test-function)))
123 (defvar *undefined-function-frame*
125 '(#+(or x86 x86-64) "bogus stack frame"
126 #-(or x86 x86-64) "undefined function"))
128 ;;; Test for "undefined function" (undefined_tramp) working properly.
129 ;;; Try it with and without tail call elimination, since they can have
130 ;;; different effects. (Specifically, if undefined_tramp is incorrect
131 ;;; a stunted stack can result from the tail call variant.)
133 (declare (optimize (speed 2) (debug 1))) ; tail call elimination
134 (#:undefined-function 42))
136 (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
137 (#:undefined-function 42))
139 (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
142 (with-test (:name (:undefined-function :bug-346)
143 :fails-on '(or :alpha)) ; bug 346
144 (assert (verify-backtrace
145 (lambda () (test #'optimized))
146 (list *undefined-function-frame*
147 (list '(flet test) #'optimized)))))
149 ;; bug 353: This test fails at least most of the time for x86/linux
150 ;; ca. 0.8.20.16. -- WHN
151 (with-test (:name (:undefined-function :bug-356)
152 :fails-on '(or (and :x86 :linux) :alpha))
153 (assert (verify-backtrace
154 (lambda () (test #'not-optimized))
155 (list *undefined-function-frame*
156 (list '(flet not-optimized))
157 (list '(flet test) #'not-optimized))))))
159 ;;; Division by zero was a common error on PPC. It depended on the
160 ;;; return function either being before INTEGER-/-INTEGER in memory,
161 ;;; or more than MOST-POSITIVE-FIXNUM bytes ahead. It also depends on
162 ;;; INTEGER-/-INTEGER calling SIGNED-TRUNCATE. I believe Raymond Toy
163 ;;; says that the Sparc backend (at least for CMUCL) inlines this, so
164 ;;; if SBCL does the same this test is probably not good for the
167 ;;; Disabling tail call elimination on this will probably ensure that
168 ;;; the return value (to the flet or the enclosing top level form) is
169 ;;; more than MOST-POSITIVE-FIXNUM with the current spaces on OS X.
170 ;;; Enabling it might catch other problems, so do it anyway.
172 (declare (optimize (speed 2) (debug 1))) ; tail call elimination
175 (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
178 (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
180 (with-test (:name (:divide-by-zero :bug-346)
181 :fails-on '(or :alpha)) ; bug 346
182 (assert (verify-backtrace (lambda () (test #'optimized))
184 (list '(flet test) #'optimized)))))
185 (with-test (:name (:divide-by-zero :bug-356)
186 :fails-on '(or :alpha)) ; bug 356
187 (assert (verify-backtrace (lambda () (test #'not-optimized))
189 '((flet not-optimized))
190 (list '(flet test) #'not-optimized))))))
192 (with-test (:name (:throw :no-such-tag)
194 (and :x86 (or :linux :freebsd sunos))
199 (throw 'no-such-tag t))
200 (assert (verify-backtrace #'throw-test '((throw-test))))))
202 ;;; test entry point handling in backtraces
207 (defmacro defbt (n ll &body body)
210 (defun ,(intern (format nil "BT.~A.1" n)) ,ll
212 ;; no arguments saved
213 (defun ,(intern (format nil "BT.~A.2" n)) ,ll
214 (declare (optimize (debug 1) (speed 3)))
216 ;; no lambda-list saved
217 (defun ,(intern (format nil "BT.~A.3" n)) ,ll
218 (declare (optimize (debug 0)))
227 (defbt 3 (&key (key (oops)))
230 ;;; ERROR instead of OOPS so that tail call elimination doesn't happen
231 (defbt 4 (&optional opt)
232 (list (error "error")))
234 (defbt 5 (&optional (opt (oops)))
237 ;;; FIXME: This test really should be broken into smaller pieces
238 (with-test (:name (:backtrace :misc)
239 :fails-on '(and :x86 (or :linux :sunos)))
240 (macrolet ((with-details (bool &body body)
241 `(let ((sb-debug:*show-entry-point-details* ,bool))
247 (assert (verify-backtrace #'namestring
248 '(((sb-c::tl-xep namestring) 0 ?)))))
250 (assert (verify-backtrace #'namestring
256 (assert (verify-backtrace (lambda () (bt.1.1 :key))
257 '(((sb-c::&more-processor bt.1.1) &rest))))
258 (assert (verify-backtrace (lambda () (bt.1.2 :key))
259 '(((sb-c::&more-processor bt.1.2) &rest))))
260 (assert (verify-backtrace (lambda () (bt.1.3 :key))
261 '(((sb-c::&more-processor bt.1.3) &rest)))))
263 (assert (verify-backtrace (lambda () (bt.1.1 :key))
265 (assert (verify-backtrace (lambda () (bt.1.2 :key))
267 (assert (verify-backtrace (lambda () (bt.1.3 :key))
273 (assert (verify-backtrace #'bt.2.1
274 '(((sb-c::xep bt.2.1) 0 ?))))
275 (assert (verify-backtrace #'bt.2.2
276 '(((sb-c::xep bt.2.2) &rest))))
277 (assert (verify-backtrace #'bt.2.3
278 '(((sb-c::xep bt.2.3) &rest)))))
280 (assert (verify-backtrace #'bt.2.1
282 (assert (verify-backtrace #'bt.2.2
284 (assert (verify-backtrace #'bt.2.3
288 (print :varargs-entry)
290 (assert (verify-backtrace #'bt.3.1
291 '(((sb-c::varargs-entry bt.3.1) :key nil))))
292 (assert (verify-backtrace #'bt.3.2
293 '(((sb-c::varargs-entry bt.3.2) :key ?))))
294 (assert (verify-backtrace #'bt.3.3
295 '(((sb-c::varargs-entry bt.3.3) &rest)))))
297 (assert (verify-backtrace #'bt.3.1
298 '((bt.3.1 :key nil))))
299 (assert (verify-backtrace #'bt.3.2
301 (assert (verify-backtrace #'bt.3.3
304 ;; HAIRY-ARG-PROCESSOR
305 (print :hairy-args-processor)
307 (assert (verify-backtrace #'bt.4.1
308 '(((sb-c::hairy-arg-processor bt.4.1) ?))))
309 (assert (verify-backtrace #'bt.4.2
310 '(((sb-c::hairy-arg-processor bt.4.2) ?))))
311 (assert (verify-backtrace #'bt.4.3
312 '(((sb-c::hairy-arg-processor bt.4.3) &rest)))))
314 (assert (verify-backtrace #'bt.4.1
316 (assert (verify-backtrace #'bt.4.2
318 (assert (verify-backtrace #'bt.4.3
321 ;; &OPTIONAL-PROCESSOR
322 (print :optional-processor)
324 (assert (verify-backtrace #'bt.5.1
325 '(((sb-c::&optional-processor bt.5.1)))))
326 (assert (verify-backtrace #'bt.5.2
327 '(((sb-c::&optional-processor bt.5.2) &rest))))
328 (assert (verify-backtrace #'bt.5.3
329 '(((sb-c::&optional-processor bt.5.3) &rest)))))
331 (assert (verify-backtrace #'bt.5.1
333 (assert (verify-backtrace #'bt.5.2
335 (assert (verify-backtrace #'bt.5.3
336 '((bt.5.3 &rest)))))))
343 (defun trace-fact (n)
346 (* n (trace-fact (1- n)))))
348 (let ((out (with-output-to-string (*trace-output*)
350 (assert (eq 'ok (trace-this)))
352 (assert (search "TRACE-THIS" out))
353 (assert (search "returned OK" out)))
356 ;;; This is not a WITH-TEST :FAILS-ON PPC DARWIN since there are
357 ;;; suspicions that the breakpoint trace might corrupt the whole image
358 ;;; on that platform.
359 #-(and (or ppc x86) darwin)
360 (with-test (:name (trace :encapsulate nil)
361 :fails-on '(or :ppc :sparc :mips))
362 (let ((out (with-output-to-string (*trace-output*)
363 (trace trace-this :encapsulate nil)
364 (assert (eq 'ok (trace-this)))
366 (assert (search "TRACE-THIS" out))
367 (assert (search "returned OK" out))))
369 #-(and (or ppc x86) darwin)
370 (with-test (:name (trace-recursive :encapsulate nil)
371 :fails-on '(or :ppc :sparc :mips))
372 (let ((out (with-output-to-string (*trace-output*)
373 (trace trace-fact :encapsulate nil)
374 (assert (= 120 (trace-fact 5)))
376 (assert (search "TRACE-FACT" out))
377 (assert (search "returned 1" out))
378 (assert (search "returned 120" out))))
380 ;;;; test infinite error protection
382 (defmacro nest-errors (n-levels error-form)
384 `(handler-bind ((error (lambda (condition)
385 (declare (ignore condition))
387 (nest-errors ,(1- n-levels) ,error-form))
390 (defun erroring-debugger-hook (condition old-debugger-hook)
391 (let ((*debugger-hook* old-debugger-hook))
392 (format t "recursive condition: ~A~%" condition) (force-output)
393 (error "recursive condition: ~A" condition)))
395 (defun test-inifinite-error-protection ()
396 ;; after 50 successful throws to SB-IMPL::TOPLEVEL-CATCHER sbcl used
397 ;; to halt, it produces so much garbage that's hard to suppress that
398 ;; it is tested only once
399 (let ((*debugger-hook* #'erroring-debugger-hook))
401 (let ((error-counter 0)
402 (*terminal-io* (make-broadcast-stream)))
406 (catch 'sb-impl::toplevel-catcher
407 (nest-errors 20 (error "infinite error ~s"
408 (incf error-counter)))
413 (test-inifinite-error-protection)
416 (let ((thread (sb-thread:make-thread #'test-inifinite-error-protection)))
417 (loop while (sb-thread:thread-alive-p thread)))