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