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-widetag
35 (sb-kernel:%simple-fun-arglist fun
))
36 (#.sb-vm
:closure-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-WIDETAG and CLOSURE-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)
47 ;; FIXME: what about #+sb-fasteval ?
49 (if (typep fun
'sb-eval
::interpreted-function
)
50 (sb-eval::interpreted-function-lambda-list fun
)
55 (defun zoop (zeep &key beep
)
57 (assert (equal (get-arglist #'zoop
) '(zeep &key beep
)))
59 ;;; Check some predefined functions too.
61 ;;; (We don't know exactly what the arguments are, e.g. the first
62 ;;; argument of PRINT might be SB-IMPL::OBJECT or SB-KERNEL::OBJ or
63 ;;; whatever. But we do know the general structure that a correct
64 ;;; answer should have, so we can safely do a lot of checks.)
65 (with-test (:name
:predefined-functions-1
)
66 (destructuring-bind (object-sym &optional-sym stream-sym
) (get-arglist #'print
)
67 (assert (symbolp object-sym
))
68 (assert (eql &optional-sym
'&optional
))
69 (assert (symbolp stream-sym
))))
71 (with-test (:name
:predefined-functions-2
)
72 (destructuring-bind (dest-sym control-sym
&rest-sym format-args-sym
)
73 (get-arglist #'format
)
74 (assert (symbolp dest-sym
))
75 (assert (symbolp control-sym
))
76 (assert (eql &rest-sym
'&rest
))
77 (assert (symbolp format-args-sym
))))
81 (defmacro with-traced-function
((name &rest options
) &body body
)
82 `(with-output-to-string (*trace-output
*)
85 (trace ,name
,@options
)
87 (ignore-errors (untrace ,name
)))))
95 (* n
(trace-fact (1- n
)))))
97 (with-test (:name
(trace :simple
))
98 (let ((output (with-traced-function (trace-this)
99 (assert (eq 'ok
(trace-this))))))
100 (assert (search "TRACE-THIS" output
))
101 (assert (search "returned OK" output
))))
104 (with-test (:name
(trace :encapsulate nil
)
105 :fails-on
(or (and :ppc
(not :linux
)) :sparc
:arm64
)
106 :broken-on
(or :sunos
:hppa
))
107 (let ((output (with-traced-function (trace-this :encapsulate nil
)
108 (assert (eq 'ok
(trace-this))))))
109 (assert (search "TRACE-THIS" output
))
110 (assert (search "returned OK" output
))))
112 (with-test (:name
(:trace
:encapsulate nil
:recursive
)
113 :fails-on
(or (and :ppc
(not :linux
)) :sparc
:sunos
:arm64
)
114 :broken-on
(or (and :x86
:sunos
) :hppa
))
115 (let ((output (with-traced-function (trace-fact :encapsulate nil
)
116 (assert (= 120 (trace-fact 5))))))
117 (assert (search "TRACE-FACT" output
))
118 (assert (search "returned 1" output
))
119 (assert (search "returned 120" output
))))
121 (defun trace-and-fmakunbound-this (x)
124 (with-test (:name
(trace fmakunbound
:bug-667657
))
125 (trace trace-and-fmakunbound-this
)
126 (fmakunbound 'trace-and-fmakunbound-this
)
128 (assert (not (trace))))
130 (with-test (:name
(trace :report nil
:smoke
))
131 (let ((output (with-traced-function (trace-this :report nil
)
132 (assert (eq 'ok
(trace-this))))))
133 (assert (sequence:emptyp output
))))
135 (with-test (:name
(trace :report nil
:print
))
136 (let ((output (with-traced-function
137 (trace-fact :report nil
:print
(sb-debug:arg
0))
138 (assert (eq '2 (trace-fact 2))))))
139 (assert (string= output
(format nil
"2~@
143 (with-test (:name
:bug-414
)
144 (handler-bind ((warning #'error
))
145 (load (compile-file "bug-414.lisp"))
146 (disassemble 'bug-414
)))
148 ;; A known function can be stored as a code constant in lieu of the
149 ;; usual mode of storing an #<fdefn> and looking up the function from it.
150 ;; One such usage occurs with TAIL-CALL-VARIABLE (e.g. via APPLY).
151 ;; Show that declaring the function locally notinline uses the #<fdefn>
152 ;; by first compiling a call that would have elided the #<fdefn>
154 (defun test-compile-then-load (filename junk
)
155 (declare (notinline compile-file load
))
156 (apply 'load
(apply 'compile-file filename junk
) junk
))
157 (compile 'test-compile-then-load
)
158 (with-test (:name
:traceable-known-fun
)
159 (let ((s (make-string-output-stream)))
160 (trace compile-file load
)
161 (let ((*trace-output
* s
))
162 (test-compile-then-load "bug-414.lisp" nil
))
164 (assert (>= (count #\Newline
(get-output-stream-string s
)) 4))))
166 (with-test (:name
:bug-310175
:fails-on
(not :stack-allocatable-lists
))
167 ;; KLUDGE: Not all DX-enabled platforms DX CONS, and the compiler
168 ;; transforms two-arg-LIST* (and one-arg-LIST) to CONS. Therefore,
169 ;; use two-arg-LIST, which should get through to VOP LIST, and thus
170 ;; stack-allocate on a predictable set of machines.
171 (let ((dx-arg (list t t
)))
172 (declare (dynamic-extent dx-arg
))
173 (flet ((dx-arg-backtrace (x)
174 (declare (optimize (debug 2)))
175 (prog1 (sb-debug:list-backtrace
:count
10)
176 (assert (sb-debug::stack-allocated-p x
)))))
177 (declare (notinline dx-arg-backtrace
))
178 (assert (member-if (lambda (frame)
181 (equal '(flet dx-arg-backtrace
:in
) (butlast (car frame
)))
182 (notany #'sb-debug
::stack-allocated-p
(cdr frame
))))
183 (dx-arg-backtrace dx-arg
))))))
185 (with-test (:name
:bug-795245
)
193 (sb-debug:print-backtrace
:count
100
194 :stream
(make-broadcast-stream))
196 (throw 'done
:error
))
198 (throw 'done
:ok
))))))
199 (apply '/= nil
1 2 nil
))))))
201 ;;;; test infinite error protection
203 (defmacro nest-errors
(n-levels error-form
)
205 `(handler-bind ((error (lambda (condition)
206 (declare (ignore condition
))
208 (nest-errors ,(1- n-levels
) ,error-form
))
211 (defun erroring-debugger-hook (condition old-debugger-hook
)
212 (let ((*debugger-hook
* old-debugger-hook
))
213 (format t
"recursive condition: ~A~%" condition
) (force-output)
214 (error "recursive condition: ~A" condition
)))
216 (defun test-infinite-error-protection ()
217 ;; after 50 successful throws to SB-IMPL::TOPLEVEL-CATCHER sbcl used
218 ;; to halt, it produces so much garbage that's hard to suppress that
219 ;; it is tested only once
220 (write-line "--HARMLESS BUT ALARMING BACKTRACE COMING UP--")
221 (let ((*debugger-hook
* #'erroring-debugger-hook
))
223 (let ((error-counter 0)
224 (*terminal-io
* (make-broadcast-stream)))
228 (catch 'sb-impl
::toplevel-catcher
229 (nest-errors 20 (error "infinite error ~s"
230 (incf error-counter
)))
232 (write-line "--END OF H-B-A-B--"))
234 (with-test (:name
:infinite-error-protection
)
236 (test-infinite-error-protection))
238 (with-test (:name
(:infinite-error-protection
:thread
)
239 :skipped-on
(not :sb-thread
))
241 (let ((thread (sb-thread:make-thread
#'test-infinite-error-protection
)))
242 (loop while
(sb-thread:thread-alive-p thread
))))
244 ;; unconditional, in case either previous left it enabled
247 ;;;; test some limitations of MAKE-LISP-OBJ
249 ;;; Older GENCGC systems had a bug in the pointer validation used by
250 ;;; MAKE-LISP-OBJ that made SIMPLE-FUN objects always fail to
252 (with-test (:name
(:make-lisp-obj
:simple-funs
))
253 (sb-sys:without-gcing
254 (assert (eq #'identity
255 (sb-kernel:make-lisp-obj
256 (sb-kernel:get-lisp-obj-address
259 ;;; Older CHENEYGC systems didn't perform any real pointer validity
260 ;;; checks beyond "is this pointer to somewhere in heap space".
261 (with-test (:name
(:make-lisp-obj
:pointer-validation
))
262 ;; Fun and games: We need to test MAKE-LISP-OBJ with a known-bogus
263 ;; address, but we also need the GC to not pitch a fit if it sees an
264 ;; object with said bogus address. Thus, construct our known-bogus
265 ;; object within an area of unboxed storage (a vector) in static
266 ;; space. We'll make it a simple object, (CONS 0 0), which has an
267 ;; in-memory representation of two consecutive zero words. We
268 ;; allocate a three-word vector so that we can guarantee a
269 ;; double-word aligned double-word of zeros no matter what happens
270 ;; with the vector-data-offset (currently double-word aligned).
271 (let* ((memory (sb-int:make-static-vector
3 :element-type
`(unsigned-byte ,sb-vm
:n-word-bits
)
273 (vector-data-address (sb-sys:sap-int
(sb-kernel::vector-sap memory
)))
274 (object-base-address (logandc2 (+ vector-data-address sb-vm
:lowtag-mask
) sb-vm
:lowtag-mask
))
275 (object-tagged-address (+ object-base-address sb-vm
:list-pointer-lowtag
)))
276 (multiple-value-bind (object valid-p
)
277 (sb-kernel:make-lisp-obj object-tagged-address nil
)
278 (declare (ignore object
))
279 (assert (not valid-p
)))))
281 (defun test-debugger (control form
&rest targets
)
282 (let ((out (make-string-output-stream))
286 (with-simple-restart (debugger-test-done! "Debugger Test Done!")
287 (let* ((*debug-io
* (make-two-way-stream
288 (make-string-input-stream control
)
289 (make-broadcast-stream out
#+nil
*standard-output
*)))
290 ;; Initial announcement goes to *ERROR-OUTPUT*
291 (*error-output
* *debug-io
*)
292 (*invoke-debugger-hook
* nil
))
293 (handler-bind ((error #'invoke-debugger
))
297 (error "Uncontrolled unwind from debugger test.")))
298 ;; For sanity's sake this is outside the *debug-io* rebinding -- otherwise
299 ;; it could swallow our asserts!
300 (with-input-from-string (s (get-output-stream-string out
))
301 (loop for line
= (read-line s nil
)
303 do
(assert targets nil
"Line = ~a" line
)
305 (format *error-output
* "Got: ~A~%" line
)
306 (let ((match (pop targets
)))
308 ;; Whatever, till the next line matches.
309 (let ((text (pop targets
)))
311 (format *error-output
* "Looking for: ~A~%" text
)
312 (unless (search text line
)
314 (push match targets
)))
315 (unless (search match line
)
316 (format *error-output
* "~&Wanted: ~S~% Got: ~S~%" match line
)
318 ;; Check that we saw everything we wanted
320 (error "Missed: ~S" targets
))
321 (assert (not oops
))))
323 (with-test (:name
(:debugger
:source
1))
329 (defun this-will-break (x)
330 (declare (optimize debug
))
339 "Operation was (/ 1 0)"
342 "(THIS-WILL-BREAK 1)"
347 (with-test (:name
(:debugger
:source
2))
352 `(locally (declare (optimize (speed 0) (safety 3) (debug 3)))
353 (let ((f #'(lambda (x cont
)
354 (print x
(make-broadcast-stream))
357 (funcall cont
(1- x
) cont
)))))
364 "source: (ERROR \"~%foo\")"
368 "(FUNCALL CONT (1- X) CONT)"
371 (with-test (:name
(:debugger
:bogus-debug-fun
:source
))
382 (with-test (:name
(disassemble :high-debug-eval
))
383 (eval `(defun this-will-be-disassembled (x)
384 (declare (optimize debug
))
386 (let* ((oopses (make-string-output-stream))
388 (let ((*error-output
* oopses
))
389 (with-output-to-string (*standard-output
*)
390 (disassemble 'this-will-be-disassembled
)))))
391 (with-input-from-string (s disassembly
)
392 (assert (search "; disassembly for THIS-WILL-BE-DISASSEMBLED"
394 (let ((problems (get-output-stream-string oopses
)))
395 (unless (zerop (length problems
))
398 (defun this-too-will-be-disasssembled (x)
399 (declare (optimize debug
))
402 (with-test (:name
(disassemble :high-debug-load
))
403 (let* ((oopses (make-string-output-stream))
405 (let ((*error-output
* oopses
))
406 (with-output-to-string (*standard-output
*)
407 (disassemble 'this-too-will-be-disasssembled
)))))
408 (with-input-from-string (s disassembly
)
409 (assert (equal "; disassembly for THIS-TOO-WILL-BE-DISASSSEMBLED"
411 (let ((problems (get-output-stream-string oopses
)))
412 (unless (zerop (length problems
))
415 (with-test (:name
(:xep-arglist-clean-up
:bug-1192929
))
418 (handler-bind ((error (lambda (e)
420 (return (< (length (car (sb-debug:list-backtrace
:count
1)))
422 (funcall (compile nil
`(lambda (i) (declare ((mod 65536) i
)) i
)) nil
)))))
426 (defun print-backtrace-to-string/debug-print-variable-alist
(x)
428 (with-output-to-string (stream)
429 (let ((*debug-print-variable-alist
* '((*print-length
* .
5)
430 (*print-level
* .
3))))
431 (sb-debug:print-backtrace
:stream stream
:count
5)))
432 x
)) ; Force use of X to prevent flushing
434 (with-test (:name
(:print-frame-call
:respect
*debug-print-variable-alist
*
435 *print-length
* :bug-1261646
))
436 (let* ((printed (print-backtrace-to-string/debug-print-variable-alist
(make-array 200)))
437 (call "(PRINT-BACKTRACE-TO-STRING/DEBUG-PRINT-VARIABLE-ALIST ")
438 (position (+ (search call printed
) (length call
))))
439 (assert (eql position
(search "#(0 0 0 0 0 ...)" printed
:start2 position
)))))
441 (with-test (:name
(:print-frame-call
:respect
*debug-print-variable-alist
*
442 *print-level
* :bug-1261646
))
443 (let* ((printed (print-backtrace-to-string/debug-print-variable-alist
445 (call "(PRINT-BACKTRACE-TO-STRING/DEBUG-PRINT-VARIABLE-ALIST ")
446 (position (+ (search call printed
) (length call
))))
447 (assert (eql position
(search "((#))" printed
:start2 position
)))))
453 (with-test (:name
:trace-debug-arg
)
454 (trace foo
:print-after
(setf *x
* (sb-debug:arg
0)))
458 (trace foo
:print
(setf *x
* (sb-debug:arg
0)))
462 (trace foo
:condition
(eql (setf *x
* (sb-debug:arg
0)) 0))
466 (trace foo
:condition-after
(setf *x
* (sb-debug:arg
0)))
470 (trace foo
:break
(and (setf *x
* (sb-debug:arg
0)) nil
))
474 (trace foo
:break-all
(and (setf *x
* (sb-debug:arg
0)) nil
))
477 (trace foo
:break-after
(and (setf *x
* (sb-debug:arg
0)) nil
))
480 (defun frobbleize (arg) (declare (ignore arg
)) (sb-debug:list-backtrace
) 'win
)
481 (defmethod low-debug-method ((self t
))
482 (declare (optimize (debug 0)))
483 (frobbleize 'me
) ; make this not a tail call, so it remains on stack
485 (with-test (:name
:clean-fast-method-frame-lossage
)
486 (low-debug-method 42)) ; no need to assert. it either crashes or doesn't
488 (defun return-65535 ()
491 (with-test (:name
:indirect-closure-values
)
494 (handler-bind ((error (lambda (c)
496 (sb-debug::map-backtrace
498 (let ((sb-debug::*current-frame
* frame
)
499 (name (sb-debug::frame-call frame
)))
500 (when (or (eq name
'test
)
502 (or (eql (search '(labels f1
) name
) 0)
503 (eql (search '(labels f2
) name
) 0))))
505 (assert (eql (var 'a
) 2))))))
509 `(sb-int:named-lambda test
()
510 (declare (optimize debug
))
519 (assert (= count
3))))
521 (with-test (:name
:indirect-closure-values
.2)
524 (handler-bind ((error (lambda (c)
526 (sb-debug::map-backtrace
528 (let ((sb-debug::*current-frame
* frame
)
529 (name (sb-debug::frame-call frame
)))
530 (when (or (eq name
'test
)
532 (or (eql (search '(labels f1
) name
) 0)
533 (eql (search '(labels f2
) name
) 0))))
535 (assert (eql (var 'a
) 65535))))))
539 `(sb-int:named-lambda test
()
540 (declare (optimize debug
))
541 (let ((a (return-65535)))
542 (declare ((unsigned-byte 16) a
))
550 (assert (= count
3))))
552 (with-test (:name
:non-tail-self-call-bad-variables
)
555 (handler-bind ((error (lambda (c)
557 (sb-debug::map-backtrace
559 (let ((sb-debug::*current-frame
* frame
))
560 (multiple-value-bind (name args
)
561 (sb-debug::frame-call frame
)
562 (when (eq name
'test
)
563 (assert (or (null args
)
564 (equal args
'(nil))))
568 (compile nil
`(sb-int:named-lambda test
(&optional x
)
569 (declare (optimize sb-c
::recognize-self-calls
))
570 (signal 'error
:format-control
"~a" :format-arguments
(list x
))
573 (assert (= count
1))))
575 (with-test (:name
:local-tail-call-variables
)
578 (handler-bind ((error (lambda (c)
580 (sb-debug::map-backtrace
582 (let ((sb-debug::*current-frame
* frame
))
583 (multiple-value-bind (name args
)
584 (sb-debug::frame-call frame
)
585 (when (eq name
'test
)
586 (assert (equal args
'(error)))
590 (compile nil
`(sb-int:named-lambda test
(x)
592 ;; If :local-tail-call fails, this will fail
593 ;; too, because there's no jump between
594 ;; SIGNAL and the call to TAIL and it will
595 ;; show (flet tail) in the backtrace.
597 (declare (notinline tail
))
600 (assert (= count
1))))
602 (with-test (:name
:variables-surrounding-inlined-code
)
605 (handler-bind ((error (lambda (c)
607 (sb-debug::map-backtrace
609 (let ((sb-debug::*current-frame
* frame
))
610 (multiple-value-bind (name)
611 (sb-debug::frame-call frame
)
612 (when (eq name
'test
)
613 (assert (equal (sb-debug:var
'l
) '(1 2 3)))
617 (compile nil
`(sb-int:named-lambda test
(a i
)
618 (declare (optimize (debug 3)))
619 (let ((l (list 1 2 3)))
623 (assert (= count
1))))
625 (with-test (:name
:variables-surrounding-inlined-code
.2)
628 (handler-bind ((error (lambda (c)
630 (sb-debug::map-backtrace
632 (let ((sb-debug::*current-frame
* frame
))
633 (multiple-value-bind (name)
634 (sb-debug::frame-call frame
)
635 (when (eq name
'test
)
636 (assert (equal (sb-debug:var
'l
) '(1 2 3)))
640 (compile nil
`(sb-int:named-lambda test
(c)
641 (declare (optimize (debug 3)))
642 (let ((l (list 1 2 3)))
643 (map 'list
#'signal c
)
646 (assert (= count
1))))