Add test cases for non-consing WITHOUT-GCING and WITH-PINNED-OBJECTS.
[sbcl.git] / tests / debug.impure.lisp
1 ;;;; This file is for testing debugging functionality, using
2 ;;;; test machinery which might have side effects (e.g.
3 ;;;; executing DEFUN).
4
5 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; more information.
7 ;;;;
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
10 ;;;; from CMU CL.
11 ;;;;
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.
15
16 (cl:in-package :cl-user)
17
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))
21
22 \f
23 ;;;; Check that we get debug arglists right.
24
25 (defvar *p* (namestring *load-truename*))
26
27 ;;; FIXME: This should use some get-argslist like functionality that
28 ;;; we actually export.
29 ;;;
30 ;;; Return the debug arglist of the function object FUN as a list, or
31 ;;; punt with :UNKNOWN.
32 (defun get-arglist (fun)
33   (declare (type function fun))
34   ;; The Lisp-level type FUNCTION can conceal a multitude of sins..
35   (case (sb-kernel:widetag-of fun)
36     (#.sb-vm:simple-fun-header-widetag
37       (sb-kernel:%simple-fun-arglist fun))
38     (#.sb-vm:closure-header-widetag (get-arglist
39                                      (sb-kernel:%closure-fun fun)))
40     ;; In code/describe.lisp, ll. 227 (%describe-fun), we use a scheme
41     ;; like above, and it seems to work. -- MNA 2001-06-12
42     ;;
43     ;; (There might be other cases with arglist info also.
44     ;; SIMPLE-FUN-HEADER-WIDETAG and CLOSURE-HEADER-WIDETAG just
45     ;; happen to be the two case that I had my nose rubbed in when
46     ;; debugging a GC problem caused by applying %SIMPLE-FUN-ARGLIST to
47     ;; a closure. -- WHN 2001-06-05)
48     (t
49      #+sb-eval
50      (if (typep fun 'sb-eval::interpreted-function)
51          (sb-eval::interpreted-function-lambda-list fun)
52          :unknown)
53      #-sb-eval
54      :unknown)))
55
56 (defun zoop (zeep &key beep)
57   blurp)
58 (assert (equal (get-arglist #'zoop) '(zeep &key beep)))
59
60 ;;; Check some predefined functions too.
61 ;;;
62 ;;; (We don't know exactly what the arguments are, e.g. the first
63 ;;; argument of PRINT might be SB-IMPL::OBJECT or SB-KERNEL::OBJ or
64 ;;; whatever. But we do know the general structure that a correct
65 ;;; answer should have, so we can safely do a lot of checks.)
66 (with-test (:name :predefined-functions-1)
67   (destructuring-bind (object-sym &optional-sym stream-sym) (get-arglist #'print)
68     (assert (symbolp object-sym))
69     (assert (eql &optional-sym '&optional))
70     (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))))
78
79 ;;; Check for backtraces generally being correct.  Ensure that the
80 ;;; actual backtrace finishes (doesn't signal any errors on its own),
81 ;;; and that it contains the frames we expect, doesn't contain any
82 ;;; "bogus stack frame"s, and contains the appropriate toplevel call
83 ;;; and hasn't been cut off anywhere.
84 (defun verify-backtrace (test-function frame-specs &key (allow-stunted nil) details)
85   (labels ((args-equal (want real)
86              (cond ((eq '&rest (car want))
87                     t)
88                    ((endp want)
89                     (endp real))
90                    ((or (eq '? (car want)) (equal (car want) (car real)))
91                     (args-equal (cdr want) (cdr real)))
92                    (t
93                     nil))))
94     (let ((result nil))
95       (block outer-handler
96         (handler-bind
97             ((error (lambda (condition)
98                       ;; find the part of the backtrace we're interested in
99                       (let (full-backtrace)
100                         (sb-debug::map-backtrace
101                          (lambda (frame)
102                            (multiple-value-bind (name args info)
103                                (sb-debug::frame-call frame #+nil #+nil
104                                                            :replace-dynamic-extent-objects t)
105                              (if details
106                                  (push (list (cons name args) info) full-backtrace)
107                                  (push (cons name args) full-backtrace)))))
108
109                         (setf full-backtrace (nreverse full-backtrace))
110                         (let ((backtrace (if details
111                                              (member (caaar frame-specs)
112                                                      full-backtrace
113                                                      :key #'caar
114                                                      :test #'equal)
115                                              (member (caar frame-specs)
116                                                      full-backtrace
117                                                      :key #'car
118                                                      :test #'equal))))
119
120                           (setf result condition)
121
122                           (unless backtrace
123                             (format t "~&//~S not in backtrace:~%   ~S~%"
124                                     (caar frame-specs)
125                                     full-backtrace)
126                             (setf result nil))
127                           ;; check that we have all the frames we wanted
128                           (mapcar
129                            (lambda (spec frame)
130                              (unless (or (not spec)
131                                          (if details
132                                              (handler-case
133                                                  (and (args-equal (car spec)
134                                                                   (car frame))
135                                                       (equal (cdr spec) (cdr frame)))
136                                                (error (e)
137                                                  (print (list :spec spec :frame frame))
138                                                  (error e)))
139                                              (and (equal (car spec) (car frame))
140                                                   (args-equal (cdr spec)
141                                                               (cdr frame)))))
142                                (print (list :wanted spec :got frame))
143                                (setf result nil)))
144                            frame-specs
145                            backtrace)
146
147                           ;; Make sure the backtrace isn't stunted in
148                           ;; any way.  (Depends on running in the main
149                           ;; thread.) FIXME: On Windows we get two
150                           ;; extra foreign frames below regular frames.
151                           (unless (find (if details
152                                             '((sb-impl::toplevel-init) ())
153                                             '(sb-impl::toplevel-init))
154                                         backtrace
155                                         :test #'equal)
156                             (print (list :backtrace-stunted backtrace))
157                             (setf result nil))
158                           (return-from outer-handler))))))
159           (funcall test-function)))
160       result)))
161
162 (defvar *undefined-function-frame*
163   ;; bug 353
164   '("undefined function"))
165
166 ;;; Test for "undefined function" (undefined_tramp) working properly.
167 ;;; Try it with and without tail call elimination, since they can have
168 ;;; different effects.  (Specifically, if undefined_tramp is incorrect
169 ;;; a stunted stack can result from the tail call variant.)
170 (flet ((optimized ()
171          (declare (optimize (speed 2) (debug 1))) ; tail call elimination
172          (#:undefined-function 42))
173        (not-optimized ()
174          (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
175          (#:undefined-function 42))
176        (test (fun)
177          (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
178          (funcall fun)))
179
180   (with-test (:name (:undefined-function :bug-346)
181                     ;; Failures on ALPHA, SPARC, MIPS, and probably
182                     ;; HPPA are due to not having a full and valid
183                     ;; stack frame for the undefined function frame.
184                     ;; See PPC undefined_tramp for details.
185               :fails-on '(or :alpha :sparc :mips
186                           (and :x86-64 :freebsd)))
187     (assert (verify-backtrace
188              (lambda () (test #'optimized))
189              (list *undefined-function-frame*
190                    (list `(flet test :in ,*p*) #'optimized)))))
191
192   ;; bug 353: This test fails at least most of the time for x86/linux
193   ;; ca. 0.8.20.16. -- WHN
194   (with-test (:name (:undefined-function :bug-353))
195     (assert (verify-backtrace
196              (lambda () (test #'not-optimized))
197              (list *undefined-function-frame*
198                    (list `(flet not-optimized :in ,*p*))
199                    (list `(flet test :in ,*p*) #'not-optimized))))))
200
201 (with-test (:name :backtrace-interrupted-condition-wait
202             :skipped-on '(not :sb-thread)
203                   ;; For some unfathomable reason the backtrace becomes
204                   ;; stunted, ending at _sigtramp, when we add :TIMEOUT NIL to
205                   ;; the frame we expect. If we leave it out, the backtrace is
206                   ;; fine -- but the test fails. I can only boggle right now.
207             :fails-on `(or (and :x86 :linux)
208                            :win32))
209   (let ((m (sb-thread:make-mutex))
210         (q (sb-thread:make-waitqueue)))
211     (assert (verify-backtrace
212              (lambda ()
213               (sb-thread:with-mutex (m)
214                 (handler-bind ((timeout (lambda (c)
215                                           (error "foo"))))
216                   (with-timeout 0.1
217                     (sb-thread:condition-wait q m)))))
218             `((sb-thread:condition-wait ,q ,m :timeout nil))))))
219
220 ;;; Division by zero was a common error on PPC. It depended on the
221 ;;; return function either being before INTEGER-/-INTEGER in memory,
222 ;;; or more than MOST-POSITIVE-FIXNUM bytes ahead. It also depends on
223 ;;; INTEGER-/-INTEGER calling SIGNED-TRUNCATE. I believe Raymond Toy
224 ;;; says that the Sparc backend (at least for CMUCL) inlines this, so
225 ;;; if SBCL does the same this test is probably not good for the
226 ;;; Sparc.
227 ;;;
228 ;;; Disabling tail call elimination on this will probably ensure that
229 ;;; the return value (to the flet or the enclosing top level form) is
230 ;;; more than MOST-POSITIVE-FIXNUM with the current spaces on OS X.
231 ;;; Enabling it might catch other problems, so do it anyway.
232 (flet ((optimized ()
233          (declare (optimize (speed 2) (debug 1))) ; tail call elimination
234          (/ 42 0))
235        (not-optimized ()
236          (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
237          (/ 42 0))
238        (test (fun)
239          (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
240          (funcall fun)))
241   (with-test (:name (:divide-by-zero :bug-346)
242               :fails-on :alpha)  ; bug 346
243     (assert (verify-backtrace (lambda () (test #'optimized))
244                               (list '(/ 42 &rest)
245                                     (list `(flet test :in ,*p*) #'optimized)))))
246   (with-test (:name (:divide-by-zero :bug-356)
247               :fails-on :alpha)  ; bug 356
248     (assert (verify-backtrace (lambda () (test #'not-optimized))
249                               (list '(/ 42 &rest)
250                                     `((flet not-optimized :in ,*p*))
251                                     (list `(flet test :in ,*p*) #'not-optimized))))))
252
253 (with-test (:name (:throw :no-such-tag)
254             :fails-on '(or
255                         (and :sparc :linux)
256                         :alpha
257                         :mips))
258   (progn
259     (defun throw-test ()
260       (throw 'no-such-tag t))
261     (assert (verify-backtrace #'throw-test '((throw-test))))))
262
263 (defun bug-308926 (x)
264   (let ((v "foo"))
265     (flet ((bar (z)
266              (oops v z)
267              (oops z v)))
268       (bar x)
269       (bar v))))
270
271 (with-test (:name :bug-308926)
272   (assert (verify-backtrace (lambda () (bug-308926 13))
273                             '(((flet bar :in bug-308926) 13)
274                               (bug-308926 &rest t)))))
275
276 ;;; test entry point handling in backtraces
277
278 (defun oops ()
279   (error "oops"))
280
281 (with-test (:name :xep-too-many-arguments)
282   (assert (verify-backtrace (lambda () (oops 1 2 3 4 5 6))
283                             '((oops ? ? ? ? ? ?)))))
284
285 (defmacro defbt (n ll &body body)
286   ;; WTF is this? This is a way to make these tests not depend so much on the
287   ;; details of LOAD/EVAL. Around 1.0.57 we changed %SIMPLE-EVAL to be
288   ;; slightly smarter, which meant that things which used to have xeps
289   ;; suddently had tl-xeps, etc. This takes care of that.
290   `(funcall
291     (compile nil
292              '(lambda ()
293                (progn
294                  ;; normal debug info
295                  (defun ,(intern (format nil "BT.~A.1" n)) ,ll
296                    ,@body)
297                  ;; no arguments saved
298                  (defun ,(intern (format nil "BT.~A.2" n)) ,ll
299                    (declare (optimize (debug 1) (speed 3)))
300                    ,@body)
301                  ;; no lambda-list saved
302                  (defun ,(intern (format nil "BT.~A.3" n)) ,ll
303                    (declare (optimize (debug 0)))
304                    ,@body))))))
305
306 (defbt 1 (&key key)
307   (list key))
308
309 (defbt 2 (x)
310   (list x))
311
312 (defbt 3 (&key (key (oops)))
313   (list key))
314
315 ;;; ERROR instead of OOPS so that tail call elimination doesn't happen
316 (defbt 4 (&optional opt)
317   (list (error "error")))
318
319 (defbt 5 (&optional (opt (oops)))
320   (list opt))
321
322 (defun bug-354 (x)
323   (error "XEPs in backtraces: ~S" x))
324
325 (with-test (:name :bug-354)
326   (assert (not (verify-backtrace (lambda () (bug-354 354))
327                                  '((bug-354 354)
328                                    (((bug-354 &rest) (:tl :external)) 354)))))
329   (assert (verify-backtrace (lambda () (bug-354 354)) '((bug-354 354)))))
330
331 ;;; FIXME: This test really should be broken into smaller pieces
332 (with-test (:name (:backtrace :tl-xep))
333   (assert (verify-backtrace #'namestring
334                             '(((namestring) (:tl :external)))
335                             :details t))
336   (assert (verify-backtrace #'namestring
337                             '((namestring)))))
338
339 (with-test (:name (:backtrace :more-processor))
340   (assert (verify-backtrace (lambda () (bt.1.1 :key))
341                             '(((bt.1.1 :key) (:more :optional)))
342                             :details t))
343   (assert (verify-backtrace (lambda () (bt.1.2 :key))
344                             '(((bt.1.2 ?) (:more :optional)))
345                             :details t))
346   (assert (verify-backtrace (lambda () (bt.1.3 :key))
347                             '(((bt.1.3 &rest) (:more :optional)))
348                             :details t))
349   (assert (verify-backtrace (lambda () (bt.1.1 :key))
350                             '((bt.1.1 :key))))
351   (assert (verify-backtrace (lambda () (bt.1.2 :key))
352                             '((bt.1.2 &rest))))
353   (assert (verify-backtrace (lambda () (bt.1.3 :key))
354                             '((bt.1.3 &rest)))))
355
356 (with-test (:name (:backtrace :xep))
357   (assert (verify-backtrace #'bt.2.1
358                             '(((bt.2.1) (:external)))
359                             :details t))
360   (assert (verify-backtrace #'bt.2.2
361                             '(((bt.2.2 &rest) (:external)))
362                             :details t))
363   (assert (verify-backtrace #'bt.2.3
364                             '(((bt.2.3 &rest) (:external)))
365                             :details t))
366   (assert (verify-backtrace #'bt.2.1
367                             '((bt.2.1))))
368   (assert (verify-backtrace #'bt.2.2
369                             '((bt.2.2 &rest))))
370   (assert (verify-backtrace #'bt.2.3
371                             '((bt.2.3 &rest)))))
372
373 ;;; This test is somewhat deceptively named. Due to confusion in debug naming
374 ;;; these functions used to have sb-c::varargs-entry debug names for their
375 ;;; main lambda.
376 (with-test (:name (:backtrace :varargs-entry))
377   (assert (verify-backtrace #'bt.3.1
378                             '((bt.3.1 :key nil))))
379   (assert (verify-backtrace #'bt.3.2
380                             '((bt.3.2 :key ?))))
381   (assert (verify-backtrace #'bt.3.3
382                             '((bt.3.3 &rest))))
383   (assert (verify-backtrace #'bt.3.1
384                             '((bt.3.1 :key nil))))
385   (assert (verify-backtrace #'bt.3.2
386                             '((bt.3.2 :key ?))))
387   (assert (verify-backtrace #'bt.3.3
388                             '((bt.3.3 &rest)))))
389
390 ;;; This test is somewhat deceptively named. Due to confusion in debug naming
391 ;;; these functions used to have sb-c::hairy-args-processor debug names for
392 ;;; their main lambda.
393 (with-test (:name (:backtrace :hairy-args-processor))
394   (assert (verify-backtrace #'bt.4.1
395                             '((bt.4.1 ?))))
396   (assert (verify-backtrace #'bt.4.2
397                             '((bt.4.2 ?))))
398   (assert (verify-backtrace #'bt.4.3
399                             '((bt.4.3 &rest))))
400   (assert (verify-backtrace #'bt.4.1
401                             '((bt.4.1 ?))))
402   (assert (verify-backtrace #'bt.4.2
403                             '((bt.4.2 ?))))
404   (assert (verify-backtrace #'bt.4.3
405                             '((bt.4.3 &rest)))))
406
407
408 (with-test (:name (:backtrace :optional-processor))
409   (assert (verify-backtrace #'bt.5.1
410                             '(((bt.5.1) (:optional)))
411                             :details t))
412   (assert (verify-backtrace #'bt.5.2
413                             '(((bt.5.2 &rest) (:optional)))
414                             :details t))
415   (assert (verify-backtrace #'bt.5.3
416                             '(((bt.5.3 &rest) (:optional)))
417                             :details t))
418   (assert (verify-backtrace #'bt.5.1
419                             '((bt.5.1))))
420   (assert (verify-backtrace #'bt.5.2
421                             '((bt.5.2 &rest))))
422   (assert (verify-backtrace #'bt.5.3
423                             '((bt.5.3 &rest)))))
424
425 (write-line "//compile nil")
426 (defvar *compile-nil-error* (compile nil '(lambda (x) (cons (when x (error "oops")) nil))))
427 (defvar *compile-nil-non-tc* (compile nil '(lambda (y) (cons (funcall *compile-nil-error* y) nil))))
428 (with-test (:name (:compile nil))
429   (assert (verify-backtrace (lambda () (funcall *compile-nil-non-tc* 13))
430                             `(((lambda (x) :in ,*p*) 13)
431                               ((lambda (y) :in ,*p*) 13)))))
432
433 (with-test (:name :clos-slot-typecheckfun-named)
434   (assert
435    (verify-backtrace
436     (lambda ()
437       (eval `(locally (declare (optimize safety))
438                (defclass clos-typecheck-test ()
439                  ((slot :type fixnum)))
440                (setf (slot-value (make-instance 'clos-typecheck-test) 'slot) t))))
441     '(((sb-pcl::slot-typecheck fixnum) t)))))
442
443 (with-test (:name :clos-emf-named)
444   (assert
445    (verify-backtrace
446     (lambda ()
447       (eval `(progn
448                (defmethod clos-emf-named-test ((x symbol)) x)
449                (defmethod clos-emf-named-test :before (x) (assert x))
450                (clos-emf-named-test nil))))
451     '(((sb-pcl::emf clos-emf-named-test) ? ? nil)))))
452
453 (with-test (:name :bug-310173)
454   (flet ((make-fun (n)
455            (let* ((names '(a b))
456                   (req (loop repeat n collect (pop names))))
457              (compile nil
458                       `(lambda (,@req &rest rest)
459                          (let ((* *)) ; no tail-call
460                            (apply '/ ,@req rest)))))))
461     (assert
462      (verify-backtrace (lambda ()
463                          (funcall (make-fun 0) 10 11 0))
464                        `((sb-kernel:two-arg-/ 10/11 0)
465                          (/ 10 11 0)
466                          ((lambda (&rest rest) :in ,*p*) 10 11 0))))
467     (assert
468      (verify-backtrace (lambda ()
469                          (funcall (make-fun 1) 10 11 0))
470                        `((sb-kernel:two-arg-/ 10/11 0)
471                          (/ 10 11 0)
472                          ((lambda (a &rest rest) :in ,*p*) 10 11 0))))
473     (assert
474      (verify-backtrace (lambda ()
475                          (funcall (make-fun 2) 10 11 0))
476                        `((sb-kernel:two-arg-/ 10/11 0)
477                          (/ 10 11 0)
478                          ((lambda (a b &rest rest) :in ,*p*) 10 11 0))))))
479
480 ;;;; test TRACE
481
482 (defun trace-this ()
483   'ok)
484
485 (defun trace-fact (n)
486   (if (zerop n)
487       1
488       (* n (trace-fact (1- n)))))
489
490 (with-test (:name (trace :simple))
491   (let ((out (with-output-to-string (*trace-output*)
492                (trace trace-this)
493                (assert (eq 'ok (trace-this)))
494                (untrace))))
495     (assert (search "TRACE-THIS" out))
496     (assert (search "returned OK" out))))
497
498 ;;; bug 379
499 ;;; This is not a WITH-TEST :FAILS-ON PPC DARWIN since there are
500 ;;; suspicions that the breakpoint trace might corrupt the whole image
501 ;;; on that platform.
502 (with-test (:name (trace :encapsulate nil)
503             :fails-on '(or (and :ppc (not :linux)) :sparc :mips)
504             :broken-on '(or :darwin :sunos))
505   (let ((out (with-output-to-string (*trace-output*)
506                (trace trace-this :encapsulate nil)
507                (assert (eq 'ok (trace-this)))
508                (untrace))))
509     (assert (search "TRACE-THIS" out))
510     (assert (search "returned OK" out))))
511
512 (with-test (:name (trace-recursive :encapsulate nil)
513             :fails-on '(or (and :ppc (not :linux)) :sparc :mips :sunos)
514             :broken-on '(or :darwin (and :x86 :sunos)))
515   (let ((out (with-output-to-string (*trace-output*)
516                (trace trace-fact :encapsulate nil)
517                (assert (= 120 (trace-fact 5)))
518                (untrace))))
519     (assert (search "TRACE-FACT" out))
520     (assert (search "returned 1" out))
521     (assert (search "returned 120" out))))
522
523 (defun trace-and-fmakunbound-this (x)
524   x)
525
526 (with-test (:name :bug-667657)
527   (trace trace-and-fmakunbound-this)
528   (fmakunbound 'trace-and-fmakunbound-this)
529   (untrace)
530   (assert (not (trace))))
531
532 (with-test (:name :bug-414)
533   (handler-bind ((warning #'error))
534     (load (compile-file "bug-414.lisp"))
535     (disassemble 'bug-414)))
536
537 (with-test (:name :bug-310175 :fails-on '(not :stack-allocatable-lists))
538   ;; KLUDGE: Not all DX-enabled platforms DX CONS, and the compiler
539   ;; transforms two-arg-LIST* (and one-arg-LIST) to CONS.  Therefore,
540   ;; use two-arg-LIST, which should get through to VOP LIST, and thus
541   ;; stack-allocate on a predictable set of machines.
542   (let ((dx-arg (list t t)))
543     (declare (dynamic-extent dx-arg))
544     (flet ((dx-arg-backtrace (x)
545              (declare (optimize (debug 2)))
546              (prog1 (sb-debug:list-backtrace :count 10)
547                (assert (sb-debug::stack-allocated-p x)))))
548       (declare (notinline dx-arg-backtrace))
549       (assert (member-if (lambda (frame)
550                            (and (consp frame)
551                                 (consp (car frame))
552                                 (equal '(flet dx-arg-backtrace :in) (butlast (car frame)))
553                                 (notany #'sb-debug::stack-allocated-p (cdr frame))))
554                          (dx-arg-backtrace dx-arg))))))
555
556 (with-test (:name :bug-795245)
557   (assert
558    (eq :ok
559        (catch 'done
560          (handler-bind
561              ((error (lambda (e)
562                        (declare (ignore e))
563                        (handler-case
564                            (sb-debug:print-backtrace :count 100
565                                                      :stream (make-broadcast-stream))
566                          (error ()
567                            (throw 'done :error))
568                          (:no-error ()
569                            (throw 'done :ok))))))
570            (apply '/= nil 1 2 nil))))))
571
572 ;;;; test infinite error protection
573
574 (defmacro nest-errors (n-levels error-form)
575   (if (< 0 n-levels)
576       `(handler-bind ((error (lambda (condition)
577                                (declare (ignore condition))
578                                ,error-form)))
579         (nest-errors ,(1- n-levels) ,error-form))
580       error-form))
581
582 (defun erroring-debugger-hook (condition old-debugger-hook)
583   (let ((*debugger-hook* old-debugger-hook))
584     (format t "recursive condition: ~A~%" condition) (force-output)
585     (error "recursive condition: ~A" condition)))
586
587 (defun test-inifinite-error-protection ()
588   ;; after 50 successful throws to SB-IMPL::TOPLEVEL-CATCHER sbcl used
589   ;; to halt, it produces so much garbage that's hard to suppress that
590   ;; it is tested only once
591   (write-line "--HARMLESS BUT ALARMING BACKTRACE COMING UP--")
592   (let ((*debugger-hook* #'erroring-debugger-hook))
593     (loop repeat 1 do
594           (let ((error-counter 0)
595                 (*terminal-io* (make-broadcast-stream)))
596             (assert
597              (not (eq
598                    :normal-exit
599                    (catch 'sb-impl::toplevel-catcher
600                      (nest-errors 20 (error "infinite error ~s"
601                                             (incf error-counter)))
602                      :normal-exit)))))))
603   (write-line "--END OF H-B-A-B--"))
604
605 (with-test (:name infinite-error-protection)
606   (enable-debugger)
607   (test-inifinite-error-protection))
608
609 (with-test (:name (infinite-error-protection :thread)
610                   :skipped-on '(not :sb-thread))
611   (enable-debugger)
612   (let ((thread (sb-thread:make-thread #'test-inifinite-error-protection)))
613     (loop while (sb-thread:thread-alive-p thread))))
614
615 ;; unconditional, in case either previous left it enabled
616 (disable-debugger)
617 \f
618 ;;;; test some limitations of MAKE-LISP-OBJ
619
620 ;;; Older GENCGC systems had a bug in the pointer validation used by
621 ;;; MAKE-LISP-OBJ that made SIMPLE-FUN objects always fail to
622 ;;; validate.
623 (with-test (:name (make-lisp-obj :simple-funs))
624   (sb-sys:without-gcing
625     (assert (eq #'identity
626                 (sb-kernel:make-lisp-obj
627                  (sb-kernel:get-lisp-obj-address
628                   #'identity))))))
629
630 ;;; Older CHENEYGC systems didn't perform any real pointer validity
631 ;;; checks beyond "is this pointer to somewhere in heap space".
632 (with-test (:name (make-lisp-obj :pointer-validation))
633   ;; Fun and games: We need to test MAKE-LISP-OBJ with a known-bogus
634   ;; address, but we also need the GC to not pitch a fit if it sees an
635   ;; object with said bogus address.  Thus, construct our known-bogus
636   ;; object within an area of unboxed storage (a vector) in static
637   ;; space.  We'll make it a simple object, (CONS 0 0), which has an
638   ;; in-memory representation of two consecutive zero words.  We
639   ;; allocate a three-word vector so that we can guarantee a
640   ;; double-word aligned double-word of zeros no matter what happens
641   ;; with the vector-data-offset (currently double-word aligned).
642   (let* ((memory (sb-int:make-static-vector 3 :element-type `(unsigned-byte ,sb-vm:n-word-bits)
643                                             :initial-element 0))
644          (vector-data-address (sb-sys:sap-int (sb-kernel::vector-sap memory)))
645          (object-base-address (logandc2 (+ vector-data-address sb-vm:lowtag-mask) sb-vm:lowtag-mask))
646          (object-tagged-address (+ object-base-address sb-vm:list-pointer-lowtag)))
647     (multiple-value-bind (object valid-p)
648         (sb-kernel:make-lisp-obj object-tagged-address nil)
649       (declare (ignore object))
650       (assert (not valid-p)))))
651
652 (defun test-debugger (control form &rest targets)
653   (let ((out (make-string-output-stream))
654         (oops t))
655     (unwind-protect
656          (progn
657            (with-simple-restart (debugger-test-done! "Debugger Test Done!")
658              (let* ((*debug-io* (make-two-way-stream
659                                  (make-string-input-stream control)
660                                  (make-broadcast-stream out #+nil *standard-output*)))
661                     ;; Initial announcement goes to *ERROR-OUTPUT*
662                     (*error-output* *debug-io*)
663                     (*invoke-debugger-hook* nil))
664                (handler-bind ((error #'invoke-debugger))
665                  (eval form))))
666            (setf oops nil))
667       (when oops
668         (error "Uncontrolled unwind from debugger test.")))
669     ;; For sanity's sake this is outside the *debug-io* rebinding -- otherwise
670     ;; it could swallow our asserts!
671     (with-input-from-string (s (get-output-stream-string out))
672       (loop for line = (read-line s nil)
673             while line
674             do (assert targets)
675                #+nil
676                (format *error-output* "Got: ~A~%" line)
677                (let ((match (pop targets)))
678                  (if (eq '* match)
679                      ;; Whatever, till the next line matches.
680                      (let ((text (pop targets)))
681                        #+nil
682                        (format *error-output* "Looking for: ~A~%" text)
683                        (unless (search text line)
684                          (push text targets)
685                          (push match targets)))
686                      (unless (search match line)
687                        (format *error-output* "~&Wanted: ~S~%   Got: ~S~%" match line)
688                        (setf oops t))))))
689     ;; Check that we saw everything we wanted
690     (when targets
691       (error "Missed: ~S" targets))
692     (assert (not oops))))
693
694 (with-test (:name (:debugger :source 1))
695   (test-debugger
696    "d
697     source 0
698     debugger-test-done!"
699    `(progn
700       (defun this-will-break (x)
701                (declare (optimize debug))
702                (let* ((y (- x x))
703                       (z (/ x y)))
704                  (+ x z)))
705       (this-will-break 1))
706    '*
707    "debugger invoked"
708    '*
709    "DIVISION-BY-ZERO"
710    "operands (1 0)"
711    '*
712    "INTEGER-/-INTEGER"
713    "(THIS-WILL-BREAK 1)"
714    "1]"
715    "(/ X Y)"
716    "1]"))
717
718 (with-test (:name (:debugger :source 2))
719   (test-debugger
720    "d
721     source 0
722     debugger-test-done!"
723    `(locally (declare (optimize (speed 0) (safety 3) (debug 3)))
724       (let ((f #'(lambda (x cont)
725                    (print x (make-broadcast-stream))
726                    (if (zerop x)
727                        (error "~%foo")
728                        (funcall cont (1- x) cont)))))
729         (funcall f 10 f)))
730    '*
731    "debugger"
732    '*
733    "foo"
734    '*
735    "source: (ERROR \"~%foo\")"
736    '*
737    "(LAMBDA (X CONT)"
738    '*
739    "(FUNCALL CONT (1- X) CONT)"
740    "1]"))
741
742 (with-test (:name (disassemble :high-debug-eval))
743   (eval `(defun this-will-be-disassembled (x)
744            (declare (optimize debug))
745            (+ x x)))
746   (let* ((oopses (make-string-output-stream))
747          (disassembly
748            (let ((*error-output* oopses))
749              (with-output-to-string (*standard-output*)
750                (disassemble 'this-will-be-disassembled)))))
751     (with-input-from-string (s disassembly)
752       (assert (search "; disassembly for THIS-WILL-BE-DISASSEMBLED"
753                       (read-line s))))
754     (let ((problems (get-output-stream-string oopses)))
755       (unless (zerop (length problems))
756         (error problems)))))
757
758 (defun this-too-will-be-disasssembled (x)
759   (declare (optimize debug))
760   (+ x x))
761
762 (with-test (:name (disassemble :high-debug-load))
763   (let* ((oopses (make-string-output-stream))
764          (disassembly
765            (let ((*error-output* oopses))
766              (with-output-to-string (*standard-output*)
767                (disassemble 'this-too-will-be-disasssembled)))))
768     (with-input-from-string (s disassembly)
769       (assert (equal "; disassembly for THIS-TOO-WILL-BE-DISASSSEMBLED"
770                      (read-line s))))
771     (let ((problems (get-output-stream-string oopses)))
772       (unless (zerop (length problems))
773         (error problems)))))
774
775 (defgeneric gf-dispatch-test/gf (x y)
776   (:method (x y)
777     (+ x y)))
778 (defun gf-dispatch-test/f (z)
779   (gf-dispatch-test/gf z))
780
781 (with-test (:name :gf-dispatch-backtrace)
782   ;; Fill the cache
783   (gf-dispatch-test/gf 1 1)
784   ;; Wrong argument count
785   (assert (verify-backtrace (lambda () (gf-dispatch-test/f 42))
786                             '(((sb-pcl::gf-dispatch gf-dispatch-test/gf) 42)))))
787
788 (write-line "/debug.impure.lisp done")