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:exit
:code
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
37 (get-arglist (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
))))
70 (with-test (:name
:predefined-functions-2
)
71 (destructuring-bind (dest-sym control-sym
&rest-sym format-args-sym
)
72 (get-arglist #'format
)
73 (assert (symbolp dest-sym
))
74 (assert (symbolp control-sym
))
75 (assert (eql &rest-sym
'&rest
))
76 (assert (symbolp format-args-sym
))))
86 (* n
(trace-fact (1- n
)))))
88 (with-test (:name
(trace :simple
))
89 (let ((out (with-output-to-string (*trace-output
*)
91 (assert (eq 'ok
(trace-this)))
93 (assert (search "TRACE-THIS" out
))
94 (assert (search "returned OK" out
))))
97 ;;; This is not a WITH-TEST :FAILS-ON PPC DARWIN since there are
98 ;;; suspicions that the breakpoint trace might corrupt the whole image
100 (with-test (:name
(trace :encapsulate nil
)
101 :fails-on
'(or (and :ppc
(not :linux
)) :sparc
)
102 :broken-on
'(or :darwin
:sunos
:hppa
))
103 (let ((out (with-output-to-string (*trace-output
*)
104 (trace trace-this
:encapsulate nil
)
105 (assert (eq 'ok
(trace-this)))
107 (assert (search "TRACE-THIS" out
))
108 (assert (search "returned OK" out
))))
110 (with-test (:name
(:trace-recursive
:encapsulate nil
)
111 :fails-on
'(or (and :ppc
(not :linux
)) :sparc
:sunos
)
112 :broken-on
'(or :darwin
(and :x86
:sunos
) :hppa
))
113 (let ((out (with-output-to-string (*trace-output
*)
114 (trace trace-fact
:encapsulate nil
)
115 (assert (= 120 (trace-fact 5)))
117 (assert (search "TRACE-FACT" out
))
118 (assert (search "returned 1" out
))
119 (assert (search "returned 120" out
))))
121 (defun trace-and-fmakunbound-this (x)
124 (with-test (:name
:bug-667657
)
125 (trace trace-and-fmakunbound-this
)
126 (fmakunbound 'trace-and-fmakunbound-this
)
128 (assert (not (trace))))
130 (with-test (:name
:bug-414
)
131 (handler-bind ((warning #'error
))
132 (load (compile-file "bug-414.lisp"))
133 (disassemble 'bug-414
)))
135 ;; A known function can be stored as a code constant in lieu of the
136 ;; usual mode of storing an #<fdefn> and looking up the function from it.
137 ;; One such usage occurs with TAIL-CALL-VARIABLE (e.g. via APPLY).
138 ;; Show that declaring the function locally notinline uses the #<fdefn>
139 ;; by first compiling a call that would have elided the #<fdefn>
141 (defun test-compile-then-load (filename junk
)
142 (declare (notinline compile-file load
))
143 (apply 'load
(apply 'compile-file filename junk
) junk
))
144 (compile 'test-compile-then-load
)
145 (with-test (:name
:traceable-known-fun
)
146 (let ((s (make-string-output-stream)))
147 (trace compile-file load
)
148 (let ((*trace-output
* s
))
149 (test-compile-then-load "bug-414.lisp" nil
))
151 (assert (>= (count #\Newline
(get-output-stream-string s
)) 4))))
153 (with-test (:name
:bug-310175
:fails-on
'(not :stack-allocatable-lists
))
154 ;; KLUDGE: Not all DX-enabled platforms DX CONS, and the compiler
155 ;; transforms two-arg-LIST* (and one-arg-LIST) to CONS. Therefore,
156 ;; use two-arg-LIST, which should get through to VOP LIST, and thus
157 ;; stack-allocate on a predictable set of machines.
158 (let ((dx-arg (list t t
)))
159 (declare (dynamic-extent dx-arg
))
160 (flet ((dx-arg-backtrace (x)
161 (declare (optimize (debug 2)))
162 (prog1 (sb-debug:list-backtrace
:count
10)
163 (assert (sb-debug::stack-allocated-p x
)))))
164 (declare (notinline dx-arg-backtrace
))
165 (assert (member-if (lambda (frame)
168 (equal '(flet dx-arg-backtrace
:in
) (butlast (car frame
)))
169 (notany #'sb-debug
::stack-allocated-p
(cdr frame
))))
170 (dx-arg-backtrace dx-arg
))))))
172 (with-test (:name
:bug-795245
)
180 (sb-debug:print-backtrace
:count
100
181 :stream
(make-broadcast-stream))
183 (throw 'done
:error
))
185 (throw 'done
:ok
))))))
186 (apply '/= nil
1 2 nil
))))))
188 ;;;; test infinite error protection
190 (defmacro nest-errors
(n-levels error-form
)
192 `(handler-bind ((error (lambda (condition)
193 (declare (ignore condition
))
195 (nest-errors ,(1- n-levels
) ,error-form
))
198 (defun erroring-debugger-hook (condition old-debugger-hook
)
199 (let ((*debugger-hook
* old-debugger-hook
))
200 (format t
"recursive condition: ~A~%" condition
) (force-output)
201 (error "recursive condition: ~A" condition
)))
203 (defun test-infinite-error-protection ()
204 ;; after 50 successful throws to SB-IMPL::TOPLEVEL-CATCHER sbcl used
205 ;; to halt, it produces so much garbage that's hard to suppress that
206 ;; it is tested only once
207 (write-line "--HARMLESS BUT ALARMING BACKTRACE COMING UP--")
208 (let ((*debugger-hook
* #'erroring-debugger-hook
))
210 (let ((error-counter 0)
211 (*terminal-io
* (make-broadcast-stream)))
215 (catch 'sb-impl
::toplevel-catcher
216 (nest-errors 20 (error "infinite error ~s"
217 (incf error-counter
)))
219 (write-line "--END OF H-B-A-B--"))
221 (with-test (:name
:infinite-error-protection
)
223 (test-infinite-error-protection))
225 (with-test (:name
(:infinite-error-protection
:thread
)
226 :skipped-on
'(not :sb-thread
))
228 (let ((thread (sb-thread:make-thread
#'test-infinite-error-protection
)))
229 (loop while
(sb-thread:thread-alive-p thread
))))
231 ;; unconditional, in case either previous left it enabled
234 ;;;; test some limitations of MAKE-LISP-OBJ
236 ;;; Older GENCGC systems had a bug in the pointer validation used by
237 ;;; MAKE-LISP-OBJ that made SIMPLE-FUN objects always fail to
239 (with-test (:name
(:make-lisp-obj
:simple-funs
))
240 (sb-sys:without-gcing
241 (assert (eq #'identity
242 (sb-kernel:make-lisp-obj
243 (sb-kernel:get-lisp-obj-address
246 ;;; Older CHENEYGC systems didn't perform any real pointer validity
247 ;;; checks beyond "is this pointer to somewhere in heap space".
248 (with-test (:name
(:make-lisp-obj
:pointer-validation
))
249 ;; Fun and games: We need to test MAKE-LISP-OBJ with a known-bogus
250 ;; address, but we also need the GC to not pitch a fit if it sees an
251 ;; object with said bogus address. Thus, construct our known-bogus
252 ;; object within an area of unboxed storage (a vector) in static
253 ;; space. We'll make it a simple object, (CONS 0 0), which has an
254 ;; in-memory representation of two consecutive zero words. We
255 ;; allocate a three-word vector so that we can guarantee a
256 ;; double-word aligned double-word of zeros no matter what happens
257 ;; with the vector-data-offset (currently double-word aligned).
258 (let* ((memory (sb-int:make-static-vector
3 :element-type
`(unsigned-byte ,sb-vm
:n-word-bits
)
260 (vector-data-address (sb-sys:sap-int
(sb-kernel::vector-sap memory
)))
261 (object-base-address (logandc2 (+ vector-data-address sb-vm
:lowtag-mask
) sb-vm
:lowtag-mask
))
262 (object-tagged-address (+ object-base-address sb-vm
:list-pointer-lowtag
)))
263 (multiple-value-bind (object valid-p
)
264 (sb-kernel:make-lisp-obj object-tagged-address nil
)
265 (declare (ignore object
))
266 (assert (not valid-p
)))))
268 (defun test-debugger (control form
&rest targets
)
269 (let ((out (make-string-output-stream))
273 (with-simple-restart (debugger-test-done! "Debugger Test Done!")
274 (let* ((*debug-io
* (make-two-way-stream
275 (make-string-input-stream control
)
276 (make-broadcast-stream out
#+nil
*standard-output
*)))
277 ;; Initial announcement goes to *ERROR-OUTPUT*
278 (*error-output
* *debug-io
*)
279 (*invoke-debugger-hook
* nil
))
280 (handler-bind ((error #'invoke-debugger
))
284 (error "Uncontrolled unwind from debugger test.")))
285 ;; For sanity's sake this is outside the *debug-io* rebinding -- otherwise
286 ;; it could swallow our asserts!
287 (with-input-from-string (s (get-output-stream-string out
))
288 (loop for line
= (read-line s nil
)
292 (format *error-output
* "Got: ~A~%" line
)
293 (let ((match (pop targets
)))
295 ;; Whatever, till the next line matches.
296 (let ((text (pop targets
)))
298 (format *error-output
* "Looking for: ~A~%" text
)
299 (unless (search text line
)
301 (push match targets
)))
302 (unless (search match line
)
303 (format *error-output
* "~&Wanted: ~S~% Got: ~S~%" match line
)
305 ;; Check that we saw everything we wanted
307 (error "Missed: ~S" targets
))
308 (assert (not oops
))))
310 (with-test (:name
(:debugger
:source
1))
316 (defun this-will-break (x)
317 (declare (optimize debug
))
329 "(THIS-WILL-BREAK 1)"
334 (with-test (:name
(:debugger
:source
2))
339 `(locally (declare (optimize (speed 0) (safety 3) (debug 3)))
340 (let ((f #'(lambda (x cont
)
341 (print x
(make-broadcast-stream))
344 (funcall cont
(1- x
) cont
)))))
351 "source: (ERROR \"~%foo\")"
355 "(FUNCALL CONT (1- X) CONT)"
358 (with-test (:name
(disassemble :high-debug-eval
))
359 (eval `(defun this-will-be-disassembled (x)
360 (declare (optimize debug
))
362 (let* ((oopses (make-string-output-stream))
364 (let ((*error-output
* oopses
))
365 (with-output-to-string (*standard-output
*)
366 (disassemble 'this-will-be-disassembled
)))))
367 (with-input-from-string (s disassembly
)
368 (assert (search "; disassembly for THIS-WILL-BE-DISASSEMBLED"
370 (let ((problems (get-output-stream-string oopses
)))
371 (unless (zerop (length problems
))
374 (defun this-too-will-be-disasssembled (x)
375 (declare (optimize debug
))
378 (with-test (:name
(disassemble :high-debug-load
))
379 (let* ((oopses (make-string-output-stream))
381 (let ((*error-output
* oopses
))
382 (with-output-to-string (*standard-output
*)
383 (disassemble 'this-too-will-be-disasssembled
)))))
384 (with-input-from-string (s disassembly
)
385 (assert (equal "; disassembly for THIS-TOO-WILL-BE-DISASSSEMBLED"
387 (let ((problems (get-output-stream-string oopses
)))
388 (unless (zerop (length problems
))
391 ;; The test named :GF-dispatch-backtrace depends on the fact that renaming
392 ;; a closure works, and that the debugger can extract a closure name.
393 ;; First things first: verify that a closure can be named.
394 (defun make-adder (x)
395 (sb-impl::set-closure-name
(lambda (y) (+ x y
)) `(adder ,x
)))
396 (with-test (:name
:closure-renaming-really-works
)
397 (let ((f1 (make-adder 5))
398 (expect "#<CLOSURE (ADDER 5)"))
399 (assert (= (mismatch (write-to-string (make-adder 5)) expect
)
401 (assert (and (eq (sb-impl::set-closure-name f1
"ADD5") f1
)
402 (string= (sb-impl::%fun-name f1
) "ADD5")))))
404 (with-test (:name
(:xep-arglist-clean-up
:bug-1192929
))
407 (handler-bind ((error (lambda (e)
409 (return (< (length (car (sb-debug:list-backtrace
:count
1)))
411 (funcall (compile nil
`(lambda (i) (declare ((mod 65536) i
)) i
)) nil
)))))
415 (defun print-backtrace-to-string/debug-print-variable-alist
(x)
417 (with-output-to-string (stream)
418 (let ((*debug-print-variable-alist
* '((*print-length
* .
5)
419 (*print-level
* .
3))))
420 (sb-debug:print-backtrace
:stream stream
:count
5)))
421 x
)) ; Force use of X to prevent flushing
423 (with-test (:name
(:print-frame-call
:respect
*debug-print-variable-alist
*
424 *print-length
* :bug-1261646
))
425 (let* ((printed (print-backtrace-to-string/debug-print-variable-alist
(make-array 200)))
426 (call "(PRINT-BACKTRACE-TO-STRING/DEBUG-PRINT-VARIABLE-ALIST ")
427 (position (+ (search call printed
) (length call
))))
428 (assert (eql position
(search "#(0 0 0 0 0 ...)" printed
:start2 position
)))))
430 (with-test (:name
(:print-frame-call
:respect
*debug-print-variable-alist
*
431 *print-level
* :bug-1261646
))
432 (let* ((printed (print-backtrace-to-string/debug-print-variable-alist
434 (call "(PRINT-BACKTRACE-TO-STRING/DEBUG-PRINT-VARIABLE-ALIST ")
435 (position (+ (search call printed
) (length call
))))
436 (assert (eql position
(search "((#))" printed
:start2 position
)))))
442 (with-test (:name
:trace-debug-arg
)
443 (trace foo
:print-after
(setf *x
* (sb-debug:arg
0)))
447 (trace foo
:print
(setf *x
* (sb-debug:arg
0)))
451 (trace foo
:condition
(eql (setf *x
* (sb-debug:arg
0)) 0))
455 (trace foo
:condition-after
(setf *x
* (sb-debug:arg
0)))
459 (trace foo
:break
(and (setf *x
* (sb-debug:arg
0)) nil
))
463 (trace foo
:break-all
(and (setf *x
* (sb-debug:arg
0)) nil
))
466 (trace foo
:break-after
(and (setf *x
* (sb-debug:arg
0)) nil
))
469 (defun frobbleize (arg) (sb-debug:print-backtrace
) 'win
)
470 (defmethod low-debug-method ((self t
))
471 (declare (optimize (debug 0)))
472 (frobbleize 'me
) ; make this not a tail call, so it remains on stack
474 (with-test (:name
:clean-fast-method-frame-lossage
)
475 (low-debug-method 42)) ; no need to assert. it either crashes or doesn't
477 (defun return-65535 ()
480 (with-test (:name
:indirect-closure-values
)
483 (handler-bind ((error (lambda (c)
485 (sb-debug::map-backtrace
487 (let ((sb-debug::*current-frame
* frame
)
488 (name (sb-debug::frame-call frame
)))
489 (when (or (eq name
'test
)
491 (or (eql (search '(labels f1
) name
) 0)
492 (eql (search '(labels f2
) name
) 0))))
494 (assert (eql (var 'a
) 2))))))
498 `(sb-int:named-lambda test
()
499 (declare (optimize debug
))
508 (assert (= count
3))))
510 (with-test (:name
:indirect-closure-values
.2)
513 (handler-bind ((error (lambda (c)
515 (sb-debug::map-backtrace
517 (let ((sb-debug::*current-frame
* frame
)
518 (name (sb-debug::frame-call frame
)))
519 (when (or (eq name
'test
)
521 (or (eql (search '(labels f1
) name
) 0)
522 (eql (search '(labels f2
) name
) 0))))
524 (assert (eql (var 'a
) 65535))))))
528 `(sb-int:named-lambda test
()
529 (declare (optimize debug
))
530 (let ((a (return-65535)))
531 (declare ((unsigned-byte 16) a
))
539 (assert (= count
3))))
541 (with-test (:name
:non-tail-self-call-bad-variables
)
544 (handler-bind ((error (lambda (c)
546 (sb-debug::map-backtrace
548 (let ((sb-debug::*current-frame
* frame
))
549 (multiple-value-bind (name args
)
550 (sb-debug::frame-call frame
)
551 (when (eq name
'test
)
552 (assert (or (null args
)
553 (equal args
'(nil))))
557 (compile nil
`(sb-int:named-lambda test
(&optional x
)
558 (declare (optimize sb-c
::recognize-self-calls
))
559 (signal 'error
:format-control
"~a" :format-arguments
(list x
))
562 (assert (= count
1))))
564 (with-test (:name
:local-tail-call-variables
)
567 (handler-bind ((error (lambda (c)
569 (sb-debug::map-backtrace
571 (let ((sb-debug::*current-frame
* frame
))
572 (multiple-value-bind (name args
)
573 (sb-debug::frame-call frame
)
574 (when (eq name
'test
)
575 (assert (equal args
'(error)))
579 (compile nil
`(sb-int:named-lambda test
(x)
581 ;; If :local-tail-call fails, this will fail
582 ;; too, because there's no jump between
583 ;; SIGNAL and the call to TAIL and it will
584 ;; show (flet tail) in the backtrace.
586 (declare (notinline tail
))
589 (assert (= count
1))))
591 (with-test (:name
:variables-surrounding-inlined-code
)
594 (handler-bind ((error (lambda (c)
596 (sb-debug::map-backtrace
598 (let ((sb-debug::*current-frame
* frame
))
599 (multiple-value-bind (name)
600 (sb-debug::frame-call frame
)
601 (when (eq name
'test
)
602 (assert (equal (sb-debug:var
'l
) '(1 2 3)))
606 (compile nil
`(sb-int:named-lambda test
(a i
)
607 (declare (optimize (debug 3)))
608 (let ((l (list 1 2 3)))
612 (assert (= count
1))))
614 (with-test (:name
:variables-surrounding-inlined-code
.2)
617 (handler-bind ((error (lambda (c)
619 (sb-debug::map-backtrace
621 (let ((sb-debug::*current-frame
* frame
))
622 (multiple-value-bind (name)
623 (sb-debug::frame-call frame
)
624 (when (eq name
'test
)
625 (assert (equal (sb-debug:var
'l
) '(1 2 3)))
629 (compile nil
`(sb-int:named-lambda test
(c)
630 (declare (optimize (debug 3)))
631 (let ((l (list 1 2 3)))
632 (map 'list
#'signal c
)
635 (assert (= count
1))))