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 (destructuring-bind (object-sym &optional-sym stream-sym
) (get-arglist #'print
)
65 (assert (symbolp object-sym
))
66 (assert (eql &optional-sym
'&optional
))
67 (assert (symbolp stream-sym
)))
68 (destructuring-bind (dest-sym control-sym
&rest-sym format-args-sym
)
69 (get-arglist #'format
)
70 (assert (symbolp dest-sym
))
71 (assert (symbolp control-sym
))
72 (assert (eql &rest-sym
'&rest
))
73 (assert (symbolp format-args-sym
)))
75 ;;; Check for backtraces generally being correct. Ensure that the
76 ;;; actual backtrace finishes (doesn't signal any errors on its own),
77 ;;; and that it contains the frames we expect, doesn't contain any
78 ;;; "bogus stack frame"s, and contains the appropriate toplevel call
79 ;;; and hasn't been cut off anywhere.
80 (defun verify-backtrace (test-function frame-specs
&key
(allow-stunted nil
))
81 (labels ((args-equal (want real
)
82 (cond ((eq '&rest
(car want
))
86 ((or (eq '?
(car want
)) (equal (car want
) (car real
)))
87 (args-equal (cdr want
) (cdr real
)))
93 ((error (lambda (condition)
94 ;; find the part of the backtrace we're interested in
95 (let* ((full-backtrace (sb-debug:backtrace-as-list
))
96 (backtrace (member (caar frame-specs
) full-backtrace
100 (setf result condition
)
103 (format t
"~&//~S not in backtrace:~% ~S~%"
108 ;; check that we have all the frames we wanted
111 (unless (or (not spec
)
112 (and (equal (car spec
) (car frame
))
113 (args-equal (cdr spec
)
115 (print (list :mismatch spec frame
))
120 ;; Make sure the backtrace isn't stunted in
121 ;; any way. (Depends on running in the main
122 ;; thread.) FIXME: On Windows we get two
123 ;; extra foreign frames below regular frames.
124 (let ((end (last backtrace
#-win32
2 #+win32
4)))
125 (unless (equal (caar end
)
126 'sb-impl
::toplevel-init
)
127 (print (list :backtrace-stunted
(caar end
)))
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
(or :freebsd
:darwin
))))
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 :x86
:x86-64
: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 ;;; Division by zero was a common error on PPC. It depended on the
176 ;;; return function either being before INTEGER-/-INTEGER in memory,
177 ;;; or more than MOST-POSITIVE-FIXNUM bytes ahead. It also depends on
178 ;;; INTEGER-/-INTEGER calling SIGNED-TRUNCATE. I believe Raymond Toy
179 ;;; says that the Sparc backend (at least for CMUCL) inlines this, so
180 ;;; if SBCL does the same this test is probably not good for the
183 ;;; Disabling tail call elimination on this will probably ensure that
184 ;;; the return value (to the flet or the enclosing top level form) is
185 ;;; more than MOST-POSITIVE-FIXNUM with the current spaces on OS X.
186 ;;; Enabling it might catch other problems, so do it anyway.
188 (declare (optimize (speed 2) (debug 1))) ; tail call elimination
191 (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
194 (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
196 (with-test (:name
(:divide-by-zero
:bug-346
)
197 :fails-on
'(or :alpha
(and :x86-64
:darwin
))) ; bug 346
198 (assert (verify-backtrace (lambda () (test #'optimized
))
200 (list '(flet test
) #'optimized
)))))
201 (with-test (:name
(:divide-by-zero
:bug-356
)
202 :fails-on
'(or :alpha
(and :x86-64
:darwin
))) ; bug 356
203 (assert (verify-backtrace (lambda () (test #'not-optimized
))
205 '((flet not-optimized
))
206 (list '(flet test
) #'not-optimized
))))))
208 (with-test (:name
(:throw
:no-such-tag
)
214 (and :x86-64
:darwin
)
216 (and :x86-64
:openbsd
)
222 (throw 'no-such-tag t
))
223 (assert (verify-backtrace #'throw-test
'((throw-test))))))
225 ;;; test entry point handling in backtraces
230 (defmacro defbt
(n ll
&body body
)
233 (defun ,(intern (format nil
"BT.~A.1" n
)) ,ll
235 ;; no arguments saved
236 (defun ,(intern (format nil
"BT.~A.2" n
)) ,ll
237 (declare (optimize (debug 1) (speed 3)))
239 ;; no lambda-list saved
240 (defun ,(intern (format nil
"BT.~A.3" n
)) ,ll
241 (declare (optimize (debug 0)))
250 (defbt 3 (&key
(key (oops)))
253 ;;; ERROR instead of OOPS so that tail call elimination doesn't happen
254 (defbt 4 (&optional opt
)
255 (list (error "error")))
257 (defbt 5 (&optional
(opt (oops)))
260 (defmacro with-details
(bool &body body
)
261 `(let ((sb-debug:*show-entry-point-details
* ,bool
))
265 (error "XEPs in backtraces: ~S" x
))
267 (with-test (:name
:bug-354
)
269 (assert (not (verify-backtrace (lambda () (bug-354 354))
271 ((sb-c::tl-xep bug-354
) &rest
))))))
272 (assert (verify-backtrace (lambda () (bug-354 354)) '((bug-354 354)))))
274 ;;; FIXME: This test really should be broken into smaller pieces
275 (with-test (:name
(:backtrace
:misc
)
276 :fails-on
'(or (and :x86
(or :sunos
)) (and :x86-64
:darwin
)))
277 (write-line "//tl-xep")
279 (assert (verify-backtrace #'namestring
280 '(((sb-c::tl-xep namestring
) 0 ?
)))))
282 (assert (verify-backtrace #'namestring
287 (assert (verify-backtrace (lambda () (bt.1.1 :key
))
288 '(((sb-c::&more-processor bt
.1.1) &rest
))))
289 (assert (verify-backtrace (lambda () (bt.1.2 :key
))
290 '(((sb-c::&more-processor bt
.1.2) &rest
))))
291 (assert (verify-backtrace (lambda () (bt.1.3 :key
))
292 '(((sb-c::&more-processor bt
.1.3) &rest
)))))
294 (assert (verify-backtrace (lambda () (bt.1.1 :key
))
296 (assert (verify-backtrace (lambda () (bt.1.2 :key
))
298 (assert (verify-backtrace (lambda () (bt.1.3 :key
))
304 (assert (verify-backtrace #'bt
.2.1
305 '(((sb-c::xep bt
.2.1) 0 ?
))))
306 (assert (verify-backtrace #'bt
.2.2
307 '(((sb-c::xep bt
.2.2) &rest
))))
308 (assert (verify-backtrace #'bt
.2.3
309 '(((sb-c::xep bt
.2.3) &rest
)))))
311 (assert (verify-backtrace #'bt
.2.1
313 (assert (verify-backtrace #'bt
.2.2
315 (assert (verify-backtrace #'bt
.2.3
319 (write-line "//varargs-entry")
321 (assert (verify-backtrace #'bt
.3.1
322 '(((sb-c::varargs-entry bt
.3.1) :key nil
))))
323 (assert (verify-backtrace #'bt
.3.2
324 '(((sb-c::varargs-entry bt
.3.2) :key ?
))))
325 (assert (verify-backtrace #'bt
.3.3
326 '(((sb-c::varargs-entry bt
.3.3) &rest
)))))
328 (assert (verify-backtrace #'bt
.3.1
329 '((bt.3.1 :key nil
))))
330 (assert (verify-backtrace #'bt
.3.2
332 (assert (verify-backtrace #'bt
.3.3
335 ;; HAIRY-ARG-PROCESSOR
336 (write-line "//hairy-args-processor")
338 (assert (verify-backtrace #'bt
.4.1
339 '(((sb-c::hairy-arg-processor bt
.4.1) ?
))))
340 (assert (verify-backtrace #'bt
.4.2
341 '(((sb-c::hairy-arg-processor bt
.4.2) ?
))))
342 (assert (verify-backtrace #'bt
.4.3
343 '(((sb-c::hairy-arg-processor bt
.4.3) &rest
)))))
345 (assert (verify-backtrace #'bt
.4.1
347 (assert (verify-backtrace #'bt
.4.2
349 (assert (verify-backtrace #'bt
.4.3
352 ;; &OPTIONAL-PROCESSOR
353 (write-line "//optional-processor")
355 (assert (verify-backtrace #'bt
.5.1
356 '(((sb-c::&optional-processor bt
.5.1)))))
357 (assert (verify-backtrace #'bt
.5.2
358 '(((sb-c::&optional-processor bt
.5.2) &rest
))))
359 (assert (verify-backtrace #'bt
.5.3
360 '(((sb-c::&optional-processor bt
.5.3) &rest
)))))
362 (assert (verify-backtrace #'bt
.5.1
364 (assert (verify-backtrace #'bt
.5.2
366 (assert (verify-backtrace #'bt
.5.3
367 '((bt.5.3 &rest
))))))
369 (write-line "//compile nil")
370 (defvar *compile-nil-error
* (compile nil
'(lambda (x) (cons (when x
(error "oops")) nil
))))
371 (defvar *compile-nil-non-tc
* (compile nil
'(lambda (y) (cons (funcall *compile-nil-error
* y
) nil
))))
372 (assert (verify-backtrace (lambda () (funcall *compile-nil-non-tc
* 13))
381 (defun trace-fact (n)
384 (* n
(trace-fact (1- n
)))))
386 (let ((out (with-output-to-string (*trace-output
*)
388 (assert (eq 'ok
(trace-this)))
390 (assert (search "TRACE-THIS" out
))
391 (assert (search "returned OK" out
)))
394 ;;; This is not a WITH-TEST :FAILS-ON PPC DARWIN since there are
395 ;;; suspicions that the breakpoint trace might corrupt the whole image
396 ;;; on that platform.
397 #-
(and (or ppc x86 x86-64
) darwin
)
398 (with-test (:name
(trace :encapsulate nil
)
399 :fails-on
'(or :ppc
:sparc
:mips
))
400 (let ((out (with-output-to-string (*trace-output
*)
401 (trace trace-this
:encapsulate nil
)
402 (assert (eq 'ok
(trace-this)))
404 (assert (search "TRACE-THIS" out
))
405 (assert (search "returned OK" out
))))
407 #-
(and (or ppc x86 x86-64
) darwin
)
408 (with-test (:name
(trace-recursive :encapsulate nil
)
409 :fails-on
'(or :ppc
:sparc
:mips
))
410 (let ((out (with-output-to-string (*trace-output
*)
411 (trace trace-fact
:encapsulate nil
)
412 (assert (= 120 (trace-fact 5)))
414 (assert (search "TRACE-FACT" out
))
415 (assert (search "returned 1" out
))
416 (assert (search "returned 120" out
))))
418 (with-test (:name
:bug-414
)
419 (handler-bind ((warning #'error
))
420 (load (compile-file "bug-414.lisp"))
421 (disassemble 'bug-414
)))
423 ;;;; test infinite error protection
425 (defmacro nest-errors
(n-levels error-form
)
427 `(handler-bind ((error (lambda (condition)
428 (declare (ignore condition
))
430 (nest-errors ,(1- n-levels
) ,error-form
))
433 (defun erroring-debugger-hook (condition old-debugger-hook
)
434 (let ((*debugger-hook
* old-debugger-hook
))
435 (format t
"recursive condition: ~A~%" condition
) (force-output)
436 (error "recursive condition: ~A" condition
)))
438 (defun test-inifinite-error-protection ()
439 ;; after 50 successful throws to SB-IMPL::TOPLEVEL-CATCHER sbcl used
440 ;; to halt, it produces so much garbage that's hard to suppress that
441 ;; it is tested only once
442 (write-line "--HARMLESS BUT ALARMING BACKTRACE COMING UP--")
443 (let ((*debugger-hook
* #'erroring-debugger-hook
))
445 (let ((error-counter 0)
446 (*terminal-io
* (make-broadcast-stream)))
450 (catch 'sb-impl
::toplevel-catcher
451 (nest-errors 20 (error "infinite error ~s"
452 (incf error-counter
)))
454 (write-line "--END OF H-B-A-B--"))
458 (test-inifinite-error-protection)
461 (let ((thread (sb-thread:make-thread
#'test-inifinite-error-protection
)))
462 (loop while
(sb-thread:thread-alive-p thread
)))
466 (write-line "/debug.impure.lisp done")