prettier backtraces
[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                            :darwin
209                            :win32))
210   (let ((m (sb-thread:make-mutex))
211         (q (sb-thread:make-waitqueue)))
212     (assert (verify-backtrace
213              (lambda ()
214               (sb-thread:with-mutex (m)
215                 (handler-bind ((timeout (lambda (c)
216                                           (error "foo"))))
217                   (with-timeout 0.1
218                     (sb-thread:condition-wait q m)))))
219             `((sb-thread:condition-wait ,q ,m :timeout nil))))))
220
221 ;;; Division by zero was a common error on PPC. It depended on the
222 ;;; return function either being before INTEGER-/-INTEGER in memory,
223 ;;; or more than MOST-POSITIVE-FIXNUM bytes ahead. It also depends on
224 ;;; INTEGER-/-INTEGER calling SIGNED-TRUNCATE. I believe Raymond Toy
225 ;;; says that the Sparc backend (at least for CMUCL) inlines this, so
226 ;;; if SBCL does the same this test is probably not good for the
227 ;;; Sparc.
228 ;;;
229 ;;; Disabling tail call elimination on this will probably ensure that
230 ;;; the return value (to the flet or the enclosing top level form) is
231 ;;; more than MOST-POSITIVE-FIXNUM with the current spaces on OS X.
232 ;;; Enabling it might catch other problems, so do it anyway.
233 (flet ((optimized ()
234          (declare (optimize (speed 2) (debug 1))) ; tail call elimination
235          (/ 42 0))
236        (not-optimized ()
237          (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
238          (/ 42 0))
239        (test (fun)
240          (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
241          (funcall fun)))
242   (with-test (:name (:divide-by-zero :bug-346)
243               :fails-on :alpha)  ; bug 346
244     (assert (verify-backtrace (lambda () (test #'optimized))
245                               (list '(/ 42 &rest)
246                                     (list `(flet test :in ,*p*) #'optimized)))))
247   (with-test (:name (:divide-by-zero :bug-356)
248               :fails-on :alpha)  ; bug 356
249     (assert (verify-backtrace (lambda () (test #'not-optimized))
250                               (list '(/ 42 &rest)
251                                     `((flet not-optimized :in ,*p*))
252                                     (list `(flet test :in ,*p*) #'not-optimized))))))
253
254 (with-test (:name (:throw :no-such-tag)
255             :fails-on '(or
256                         (and :sparc :linux)
257                         :alpha
258                         :mips))
259   (progn
260     (defun throw-test ()
261       (throw 'no-such-tag t))
262     (assert (verify-backtrace #'throw-test '((throw-test))))))
263
264 (defun bug-308926 (x)
265   (let ((v "foo"))
266     (flet ((bar (z)
267              (oops v z)
268              (oops z v)))
269       (bar x)
270       (bar v))))
271
272 (with-test (:name :bug-308926)
273   (assert (verify-backtrace (lambda () (bug-308926 13))
274                             '(((flet bar :in bug-308926) 13)
275                               (bug-308926 &rest t)))))
276
277 ;;; test entry point handling in backtraces
278
279 (defun oops ()
280   (error "oops"))
281
282 (with-test (:name :xep-too-many-arguments)
283   (assert (verify-backtrace (lambda () (oops 1 2 3 4 5 6))
284                             '((oops ? ? ? ? ? ?)))))
285
286 (defmacro defbt (n ll &body body)
287   ;; WTF is this? This is a way to make these tests not depend so much on the
288   ;; details of LOAD/EVAL. Around 1.0.57 we changed %SIMPLE-EVAL to be
289   ;; slightly smarter, which meant that things which used to have xeps
290   ;; suddently had tl-xeps, etc. This takes care of that.
291   `(funcall
292     (compile nil
293              '(lambda ()
294                (progn
295                  ;; normal debug info
296                  (defun ,(intern (format nil "BT.~A.1" n)) ,ll
297                    ,@body)
298                  ;; no arguments saved
299                  (defun ,(intern (format nil "BT.~A.2" n)) ,ll
300                    (declare (optimize (debug 1) (speed 3)))
301                    ,@body)
302                  ;; no lambda-list saved
303                  (defun ,(intern (format nil "BT.~A.3" n)) ,ll
304                    (declare (optimize (debug 0)))
305                    ,@body))))))
306
307 (defbt 1 (&key key)
308   (list key))
309
310 (defbt 2 (x)
311   (list x))
312
313 (defbt 3 (&key (key (oops)))
314   (list key))
315
316 ;;; ERROR instead of OOPS so that tail call elimination doesn't happen
317 (defbt 4 (&optional opt)
318   (list (error "error")))
319
320 (defbt 5 (&optional (opt (oops)))
321   (list opt))
322
323 (defun bug-354 (x)
324   (error "XEPs in backtraces: ~S" x))
325
326 (with-test (:name :bug-354)
327   (assert (not (verify-backtrace (lambda () (bug-354 354))
328                                  '((bug-354 354)
329                                    (((bug-354 &rest) (:tl :external)) 354)))))
330   (assert (verify-backtrace (lambda () (bug-354 354)) '((bug-354 354)))))
331
332 ;;; FIXME: This test really should be broken into smaller pieces
333 (with-test (:name (:backtrace :tl-xep))
334   (assert (verify-backtrace #'namestring
335                             '(((namestring) (:tl :external)))
336                             :details t))
337   (assert (verify-backtrace #'namestring
338                             '((namestring)))))
339
340 (with-test (:name (:backtrace :more-processor))
341   (assert (verify-backtrace (lambda () (bt.1.1 :key))
342                             '(((bt.1.1 :key) (:more :optional)))
343                             :details t))
344   (assert (verify-backtrace (lambda () (bt.1.2 :key))
345                             '(((bt.1.2 ?) (:more :optional)))
346                             :details t))
347   (assert (verify-backtrace (lambda () (bt.1.3 :key))
348                             '(((bt.1.3 &rest) (:more :optional)))
349                             :details t))
350   (assert (verify-backtrace (lambda () (bt.1.1 :key))
351                             '((bt.1.1 :key))))
352   (assert (verify-backtrace (lambda () (bt.1.2 :key))
353                             '((bt.1.2 &rest))))
354   (assert (verify-backtrace (lambda () (bt.1.3 :key))
355                             '((bt.1.3 &rest)))))
356
357 (with-test (:name (:backtrace :xep))
358   (assert (verify-backtrace #'bt.2.1
359                             '(((bt.2.1) (:external)))
360                             :details t))
361   (assert (verify-backtrace #'bt.2.2
362                             '(((bt.2.2 &rest) (:external)))
363                             :details t))
364   (assert (verify-backtrace #'bt.2.3
365                             '(((bt.2.3 &rest) (:external)))
366                             :details t))
367   (assert (verify-backtrace #'bt.2.1
368                             '((bt.2.1))))
369   (assert (verify-backtrace #'bt.2.2
370                             '((bt.2.2 &rest))))
371   (assert (verify-backtrace #'bt.2.3
372                             '((bt.2.3 &rest)))))
373
374 ;;; This test is somewhat deceptively named. Due to confusion in debug naming
375 ;;; these functions used to have sb-c::varargs-entry debug names for their
376 ;;; main lambda.
377 (with-test (:name (:backtrace :varargs-entry))
378   (assert (verify-backtrace #'bt.3.1
379                             '((bt.3.1 :key nil))))
380   (assert (verify-backtrace #'bt.3.2
381                             '((bt.3.2 :key ?))))
382   (assert (verify-backtrace #'bt.3.3
383                             '((bt.3.3 &rest))))
384   (assert (verify-backtrace #'bt.3.1
385                             '((bt.3.1 :key nil))))
386   (assert (verify-backtrace #'bt.3.2
387                             '((bt.3.2 :key ?))))
388   (assert (verify-backtrace #'bt.3.3
389                             '((bt.3.3 &rest)))))
390
391 ;;; This test is somewhat deceptively named. Due to confusion in debug naming
392 ;;; these functions used to have sb-c::hairy-args-processor debug names for
393 ;;; their main lambda.
394 (with-test (:name (:backtrace :hairy-args-processor))
395   (assert (verify-backtrace #'bt.4.1
396                             '((bt.4.1 ?))))
397   (assert (verify-backtrace #'bt.4.2
398                             '((bt.4.2 ?))))
399   (assert (verify-backtrace #'bt.4.3
400                             '((bt.4.3 &rest))))
401   (assert (verify-backtrace #'bt.4.1
402                             '((bt.4.1 ?))))
403   (assert (verify-backtrace #'bt.4.2
404                             '((bt.4.2 ?))))
405   (assert (verify-backtrace #'bt.4.3
406                             '((bt.4.3 &rest)))))
407
408
409 (with-test (:name (:backtrace :optional-processor))
410   (assert (verify-backtrace #'bt.5.1
411                             '(((bt.5.1) (:optional)))
412                             :details t))
413   (assert (verify-backtrace #'bt.5.2
414                             '(((bt.5.2 &rest) (:optional)))
415                             :details t))
416   (assert (verify-backtrace #'bt.5.3
417                             '(((bt.5.3 &rest) (:optional)))
418                             :details t))
419   (assert (verify-backtrace #'bt.5.1
420                             '((bt.5.1))))
421   (assert (verify-backtrace #'bt.5.2
422                             '((bt.5.2 &rest))))
423   (assert (verify-backtrace #'bt.5.3
424                             '((bt.5.3 &rest)))))
425
426 (write-line "//compile nil")
427 (defvar *compile-nil-error* (compile nil '(lambda (x) (cons (when x (error "oops")) nil))))
428 (defvar *compile-nil-non-tc* (compile nil '(lambda (y) (cons (funcall *compile-nil-error* y) nil))))
429 (with-test (:name (:compile nil))
430   (assert (verify-backtrace (lambda () (funcall *compile-nil-non-tc* 13))
431                             `(((lambda (x) :in ,*p*) 13)
432                               ((lambda (y) :in ,*p*) 13)))))
433
434 (with-test (:name :clos-slot-typecheckfun-named)
435   (assert
436    (verify-backtrace
437     (lambda ()
438       (eval `(locally (declare (optimize safety))
439                (defclass clos-typecheck-test ()
440                  ((slot :type fixnum)))
441                (setf (slot-value (make-instance 'clos-typecheck-test) 'slot) t))))
442     '(((sb-pcl::slot-typecheck fixnum) t)))))
443
444 (with-test (:name :clos-emf-named)
445   (assert
446    (verify-backtrace
447     (lambda ()
448       (eval `(progn
449                (defmethod clos-emf-named-test ((x symbol)) x)
450                (defmethod clos-emf-named-test :before (x) (assert x))
451                (clos-emf-named-test nil))))
452     '(((sb-pcl::emf clos-emf-named-test) ? ? nil)))))
453
454 (with-test (:name :bug-310173)
455   (flet ((make-fun (n)
456            (let* ((names '(a b))
457                   (req (loop repeat n collect (pop names))))
458              (compile nil
459                       `(lambda (,@req &rest rest)
460                          (let ((* *)) ; no tail-call
461                            (apply '/ ,@req rest)))))))
462     (assert
463      (verify-backtrace (lambda ()
464                          (funcall (make-fun 0) 10 11 0))
465                        `((sb-kernel:two-arg-/ 10/11 0)
466                          (/ 10 11 0)
467                          ((lambda (&rest rest) :in ,*p*) 10 11 0))))
468     (assert
469      (verify-backtrace (lambda ()
470                          (funcall (make-fun 1) 10 11 0))
471                        `((sb-kernel:two-arg-/ 10/11 0)
472                          (/ 10 11 0)
473                          ((lambda (a &rest rest) :in ,*p*) 10 11 0))))
474     (assert
475      (verify-backtrace (lambda ()
476                          (funcall (make-fun 2) 10 11 0))
477                        `((sb-kernel:two-arg-/ 10/11 0)
478                          (/ 10 11 0)
479                          ((lambda (a b &rest rest) :in ,*p*) 10 11 0))))))
480
481 ;;;; test TRACE
482
483 (defun trace-this ()
484   'ok)
485
486 (defun trace-fact (n)
487   (if (zerop n)
488       1
489       (* n (trace-fact (1- n)))))
490
491 (with-test (:name (trace :simple))
492   (let ((out (with-output-to-string (*trace-output*)
493                (trace trace-this)
494                (assert (eq 'ok (trace-this)))
495                (untrace))))
496     (assert (search "TRACE-THIS" out))
497     (assert (search "returned OK" out))))
498
499 ;;; bug 379
500 ;;; This is not a WITH-TEST :FAILS-ON PPC DARWIN since there are
501 ;;; suspicions that the breakpoint trace might corrupt the whole image
502 ;;; on that platform.
503 (with-test (:name (trace :encapsulate nil)
504             :fails-on '(or (and :ppc (not :linux)) :sparc :mips)
505             :broken-on '(or :darwin :sunos))
506   (let ((out (with-output-to-string (*trace-output*)
507                (trace trace-this :encapsulate nil)
508                (assert (eq 'ok (trace-this)))
509                (untrace))))
510     (assert (search "TRACE-THIS" out))
511     (assert (search "returned OK" out))))
512
513 (with-test (:name (trace-recursive :encapsulate nil)
514             :fails-on '(or (and :ppc (not :linux)) :sparc :mips :sunos)
515             :broken-on '(or :darwin (and :x86 :sunos)))
516   (let ((out (with-output-to-string (*trace-output*)
517                (trace trace-fact :encapsulate nil)
518                (assert (= 120 (trace-fact 5)))
519                (untrace))))
520     (assert (search "TRACE-FACT" out))
521     (assert (search "returned 1" out))
522     (assert (search "returned 120" out))))
523
524 (defun trace-and-fmakunbound-this (x)
525   x)
526
527 (with-test (:name :bug-667657)
528   (trace trace-and-fmakunbound-this)
529   (fmakunbound 'trace-and-fmakunbound-this)
530   (untrace)
531   (assert (not (trace))))
532
533 (with-test (:name :bug-414)
534   (handler-bind ((warning #'error))
535     (load (compile-file "bug-414.lisp"))
536     (disassemble 'bug-414)))
537
538 (with-test (:name :bug-310175 :fails-on '(not :stack-allocatable-lists))
539   ;; KLUDGE: Not all DX-enabled platforms DX CONS, and the compiler
540   ;; transforms two-arg-LIST* (and one-arg-LIST) to CONS.  Therefore,
541   ;; use two-arg-LIST, which should get through to VOP LIST, and thus
542   ;; stack-allocate on a predictable set of machines.
543   (let ((dx-arg (list t t)))
544     (declare (dynamic-extent dx-arg))
545     (flet ((dx-arg-backtrace (x)
546              (declare (optimize (debug 2)))
547              (prog1 (sb-debug:list-backtrace :count 10)
548                (assert (sb-debug::stack-allocated-p x)))))
549       (declare (notinline dx-arg-backtrace))
550       (assert (member-if (lambda (frame)
551                            (and (consp frame)
552                                 (consp (car frame))
553                                 (equal '(flet dx-arg-backtrace :in) (butlast (car frame)))
554                                 (notany #'sb-debug::stack-allocated-p (cdr frame))))
555                          (dx-arg-backtrace dx-arg))))))
556
557 (with-test (:name :bug-795245)
558   (assert
559    (eq :ok
560        (catch 'done
561          (handler-bind
562              ((error (lambda (e)
563                        (declare (ignore e))
564                        (handler-case
565                            (sb-debug:print-backtrace :count 100
566                                                      :stream (make-broadcast-stream))
567                          (error ()
568                            (throw 'done :error))
569                          (:no-error ()
570                            (throw 'done :ok))))))
571            (apply '/= nil 1 2 nil))))))
572
573 ;;;; test infinite error protection
574
575 (defmacro nest-errors (n-levels error-form)
576   (if (< 0 n-levels)
577       `(handler-bind ((error (lambda (condition)
578                                (declare (ignore condition))
579                                ,error-form)))
580         (nest-errors ,(1- n-levels) ,error-form))
581       error-form))
582
583 (defun erroring-debugger-hook (condition old-debugger-hook)
584   (let ((*debugger-hook* old-debugger-hook))
585     (format t "recursive condition: ~A~%" condition) (force-output)
586     (error "recursive condition: ~A" condition)))
587
588 (defun test-inifinite-error-protection ()
589   ;; after 50 successful throws to SB-IMPL::TOPLEVEL-CATCHER sbcl used
590   ;; to halt, it produces so much garbage that's hard to suppress that
591   ;; it is tested only once
592   (write-line "--HARMLESS BUT ALARMING BACKTRACE COMING UP--")
593   (let ((*debugger-hook* #'erroring-debugger-hook))
594     (loop repeat 1 do
595           (let ((error-counter 0)
596                 (*terminal-io* (make-broadcast-stream)))
597             (assert
598              (not (eq
599                    :normal-exit
600                    (catch 'sb-impl::toplevel-catcher
601                      (nest-errors 20 (error "infinite error ~s"
602                                             (incf error-counter)))
603                      :normal-exit)))))))
604   (write-line "--END OF H-B-A-B--"))
605
606 (with-test (:name infinite-error-protection)
607   (enable-debugger)
608   (test-inifinite-error-protection))
609
610 (with-test (:name (infinite-error-protection :thread)
611                   :skipped-on '(not :sb-thread))
612   (enable-debugger)
613   (let ((thread (sb-thread:make-thread #'test-inifinite-error-protection)))
614     (loop while (sb-thread:thread-alive-p thread))))
615
616 ;; unconditional, in case either previous left it enabled
617 (disable-debugger)
618 \f
619 ;;;; test some limitations of MAKE-LISP-OBJ
620
621 ;;; Older GENCGC systems had a bug in the pointer validation used by
622 ;;; MAKE-LISP-OBJ that made SIMPLE-FUN objects always fail to
623 ;;; validate.
624 (with-test (:name (make-lisp-obj :simple-funs))
625   (sb-sys:without-gcing
626     (assert (eq #'identity
627                 (sb-kernel:make-lisp-obj
628                  (sb-kernel:get-lisp-obj-address
629                   #'identity))))))
630
631 ;;; Older CHENEYGC systems didn't perform any real pointer validity
632 ;;; checks beyond "is this pointer to somewhere in heap space".
633 (with-test (:name (make-lisp-obj :pointer-validation))
634   ;; Fun and games: We need to test MAKE-LISP-OBJ with a known-bogus
635   ;; address, but we also need the GC to not pitch a fit if it sees an
636   ;; object with said bogus address.  Thus, construct our known-bogus
637   ;; object within an area of unboxed storage (a vector) in static
638   ;; space.  We'll make it a simple object, (CONS 0 0), which has an
639   ;; in-memory representation of two consecutive zero words.  We
640   ;; allocate a three-word vector so that we can guarantee a
641   ;; double-word aligned double-word of zeros no matter what happens
642   ;; with the vector-data-offset (currently double-word aligned).
643   (let* ((memory (sb-int:make-static-vector 3 :element-type `(unsigned-byte ,sb-vm:n-word-bits)
644                                             :initial-element 0))
645          (vector-data-address (sb-sys:sap-int (sb-kernel::vector-sap memory)))
646          (object-base-address (logandc2 (+ vector-data-address sb-vm:lowtag-mask) sb-vm:lowtag-mask))
647          (object-tagged-address (+ object-base-address sb-vm:list-pointer-lowtag)))
648     (multiple-value-bind (object valid-p)
649         (sb-kernel:make-lisp-obj object-tagged-address nil)
650       (declare (ignore object))
651       (assert (not valid-p)))))
652
653 (defun test-debugger (control form &rest targets)
654   (let ((out (make-string-output-stream))
655         (oops t))
656     (unwind-protect
657          (progn
658            (with-simple-restart (debugger-test-done! "Debugger Test Done!")
659              (let* ((*debug-io* (make-two-way-stream
660                                  (make-string-input-stream control)
661                                  (make-broadcast-stream out #+nil *standard-output*)))
662                     ;; Initial announcement goes to *ERROR-OUTPUT*
663                     (*error-output* *debug-io*)
664                     (*invoke-debugger-hook* nil))
665                (handler-bind ((error #'invoke-debugger))
666                  (eval form))))
667            (setf oops nil))
668       (when oops
669         (error "Uncontrolled unwind from debugger test.")))
670     ;; For sanity's sake this is outside the *debug-io* rebinding -- otherwise
671     ;; it could swallow our asserts!
672     (with-input-from-string (s (get-output-stream-string out))
673       (loop for line = (read-line s nil)
674             while line
675             do (assert targets)
676                #+nil
677                (format *error-output* "Got: ~A~%" line)
678                (let ((match (pop targets)))
679                  (if (eq '* match)
680                      ;; Whatever, till the next line matches.
681                      (let ((text (pop targets)))
682                        #+nil
683                        (format *error-output* "Looking for: ~A~%" text)
684                        (unless (search text line)
685                          (push text targets)
686                          (push match targets)))
687                      (unless (search match line)
688                        (format *error-output* "~&Wanted: ~S~%   Got: ~S~%" match line)
689                        (setf oops t))))))
690     ;; Check that we saw everything we wanted
691     (when targets
692       (error "Missed: ~S" targets))
693     (assert (not oops))))
694
695 (with-test (:name (:debugger :source 1))
696   (test-debugger
697    "d
698     source 0
699     debugger-test-done!"
700    `(progn
701       (defun this-will-break (x)
702                (declare (optimize debug))
703                (let* ((y (- x x))
704                       (z (/ x y)))
705                  (+ x z)))
706       (this-will-break 1))
707    '*
708    "debugger invoked"
709    '*
710    "DIVISION-BY-ZERO"
711    "operands (1 0)"
712    '*
713    "INTEGER-/-INTEGER"
714    "(THIS-WILL-BREAK 1)"
715    "1]"
716    "(/ X Y)"
717    "1]"))
718
719 (with-test (:name (:debugger :source 2))
720   (test-debugger
721    "d
722     source 0
723     debugger-test-done!"
724    `(locally (declare (optimize (speed 0) (safety 3) (debug 3)))
725       (let ((f #'(lambda (x cont)
726                    (print x (make-broadcast-stream))
727                    (if (zerop x)
728                        (error "~%foo")
729                        (funcall cont (1- x) cont)))))
730         (funcall f 10 f)))
731    '*
732    "debugger"
733    '*
734    "foo"
735    '*
736    "source: (ERROR \"~%foo\")"
737    '*
738    "(LAMBDA (X CONT)"
739    '*
740    "(FUNCALL CONT (1- X) CONT)"
741    "1]"))
742
743 (with-test (:name (disassemble :high-debug-eval))
744   (eval `(defun this-will-be-disassembled (x)
745            (declare (optimize debug))
746            (+ x x)))
747   (let* ((oopses (make-string-output-stream))
748          (disassembly
749            (let ((*error-output* oopses))
750              (with-output-to-string (*standard-output*)
751                (disassemble 'this-will-be-disassembled)))))
752     (with-input-from-string (s disassembly)
753       (assert (search "; disassembly for THIS-WILL-BE-DISASSEMBLED"
754                       (read-line s))))
755     (let ((problems (get-output-stream-string oopses)))
756       (unless (zerop (length problems))
757         (error problems)))))
758
759 (defun this-too-will-be-disasssembled (x)
760   (declare (optimize debug))
761   (+ x x))
762
763 (with-test (:name (disassemble :high-debug-load))
764   (let* ((oopses (make-string-output-stream))
765          (disassembly
766            (let ((*error-output* oopses))
767              (with-output-to-string (*standard-output*)
768                (disassemble 'this-too-will-be-disasssembled)))))
769     (with-input-from-string (s disassembly)
770       (assert (equal "; disassembly for THIS-TOO-WILL-BE-DISASSSEMBLED"
771                      (read-line s))))
772     (let ((problems (get-output-stream-string oopses)))
773       (unless (zerop (length problems))
774         (error problems)))))
775
776 (defgeneric gf-dispatch-test/gf (x y)
777   (:method (x y)
778     (+ x y)))
779 (defun gf-dispatch-test/f (z)
780   (gf-dispatch-test/gf z))
781
782 (with-test (:name :gf-dispatch-backtrace)
783   ;; Fill the cache
784   (gf-dispatch-test/gf 1 1)
785   ;; Wrong argument count
786   (assert (verify-backtrace (lambda () (gf-dispatch-test/f 42))
787                             '(((sb-pcl::gf-dispatch gf-dispatch-test/gf) 42)))))
788
789 (write-line "/debug.impure.lisp done")