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