74aec924926979178c6ae609fa9de07abaa32205
[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:quit :unix-status 104))
21
22 \f
23 ;;;; Check that we get debug arglists right.
24
25 ;;; FIXME: This should use some get-argslist like functionality that
26 ;;; we actually export.
27 ;;;
28 ;;; Return the debug arglist of the function object FUN as a list, or
29 ;;; punt with :UNKNOWN.
30 (defun get-arglist (fun)
31   (declare (type function fun))
32   ;; The Lisp-level type FUNCTION can conceal a multitude of sins..
33   (case (sb-kernel:widetag-of fun)
34     (#.sb-vm:simple-fun-header-widetag
35       (sb-kernel:%simple-fun-arglist fun))
36     (#.sb-vm:closure-header-widetag (get-arglist
37                                      (sb-kernel:%closure-fun fun)))
38     ;; In code/describe.lisp, ll. 227 (%describe-fun), we use a scheme
39     ;; like above, and it seems to work. -- MNA 2001-06-12
40     ;;
41     ;; (There might be other cases with arglist info also.
42     ;; SIMPLE-FUN-HEADER-WIDETAG and CLOSURE-HEADER-WIDETAG just
43     ;; happen to be the two case that I had my nose rubbed in when
44     ;; debugging a GC problem caused by applying %SIMPLE-FUN-ARGLIST to
45     ;; a closure. -- WHN 2001-06-05)
46     (t
47      #+sb-eval
48      (if (typep fun 'sb-eval::interpreted-function)
49          (sb-eval::interpreted-function-lambda-list fun)
50          :unknown)
51      #-sb-eval
52      :unknown)))
53
54 (defun zoop (zeep &key beep)
55   blurp)
56 (assert (equal (get-arglist #'zoop) '(zeep &key beep)))
57
58 ;;; Check some predefined functions too.
59 ;;;
60 ;;; (We don't know exactly what the arguments are, e.g. the first
61 ;;; argument of PRINT might be SB-IMPL::OBJECT or SB-KERNEL::OBJ or
62 ;;; whatever. But we do know the general structure that a correct
63 ;;; answer should have, so we can safely do a lot of checks.)
64 (with-test (:name :predefined-functions-1)
65   (destructuring-bind (object-sym &optional-sym stream-sym) (get-arglist #'print)
66     (assert (symbolp object-sym))
67     (assert (eql &optional-sym '&optional))
68     (assert (symbolp stream-sym))))
69 (with-test (:name :predefined-functions-2)
70   (destructuring-bind (dest-sym control-sym &rest-sym format-args-sym)
71       (get-arglist #'format)
72     (assert (symbolp dest-sym))
73     (assert (symbolp control-sym))
74     (assert (eql &rest-sym '&rest))
75     (assert (symbolp format-args-sym))))
76
77 ;;; Check for backtraces generally being correct.  Ensure that the
78 ;;; actual backtrace finishes (doesn't signal any errors on its own),
79 ;;; and that it contains the frames we expect, doesn't contain any
80 ;;; "bogus stack frame"s, and contains the appropriate toplevel call
81 ;;; and hasn't been cut off anywhere.
82 (defun verify-backtrace (test-function frame-specs &key (allow-stunted nil))
83   (labels ((args-equal (want real)
84              (cond ((eq '&rest (car want))
85                     t)
86                    ((endp want)
87                     (endp real))
88                    ((or (eq '? (car want)) (equal (car want) (car real)))
89                     (args-equal (cdr want) (cdr real)))
90                    (t
91                     nil))))
92     (let ((result nil))
93       (block outer-handler
94         (handler-bind
95             ((error (lambda (condition)
96                       ;; find the part of the backtrace we're interested in
97                       (let* ((full-backtrace (sb-debug:backtrace-as-list))
98                              (backtrace (member (caar frame-specs) full-backtrace
99                                                 :key #'car
100                                                 :test #'equal)))
101
102                         (setf result condition)
103
104                         (unless backtrace
105                           (format t "~&//~S not in backtrace:~%   ~S~%"
106                                   (caar frame-specs)
107                                   full-backtrace)
108                           (setf result nil))
109
110                         ;; check that we have all the frames we wanted
111                         (mapcar
112                          (lambda (spec frame)
113                            (unless (or (not spec)
114                                        (and (equal (car spec) (car frame))
115                                             (args-equal (cdr spec)
116                                                         (cdr frame))))
117                              (print (list :wanted spec :got frame))
118                              (setf result nil)))
119                          frame-specs
120                          backtrace)
121
122                         ;; Make sure the backtrace isn't stunted in
123                         ;; any way.  (Depends on running in the main
124                         ;; thread.) FIXME: On Windows we get two
125                         ;; extra foreign frames below regular frames.
126                         (unless (find '(sb-impl::toplevel-init) backtrace
127                                       :test #'equal)
128                           (print (list :backtrace-stunted backtrace))
129                           (setf result nil))
130                         (return-from outer-handler)))))
131           (funcall test-function)))
132       result)))
133
134 (defvar *undefined-function-frame*
135   ;; bug 353
136   '(#+(or x86 x86-64) "bogus stack frame"
137     #-(or x86 x86-64) "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               :fails-on '(or :alpha :ppc :sparc :mips
155                           (and :x86-64 :freebsd)))
156     (assert (verify-backtrace
157              (lambda () (test #'optimized))
158              (list *undefined-function-frame*
159                    (list '(flet test) #'optimized)))))
160
161   ;; bug 353: This test fails at least most of the time for x86/linux
162   ;; ca. 0.8.20.16. -- WHN
163   (with-test (:name (:undefined-function :bug-353)
164               ;; This used to have fewer :fails-on features pre-0.9.16.38,
165               ;; but it turns out that the bug was just being masked by
166               ;; the presence of the IR1 stepper instrumentation (and
167               ;; is thus again failing now that the instrumentation is
168               ;; no more).
169               :fails-on '(or :alpha :mips :ppc))
170     (assert (verify-backtrace
171              (lambda () (test #'not-optimized))
172              (list *undefined-function-frame*
173                    (list '(flet not-optimized))
174                    (list '(flet test) #'not-optimized))))))
175
176 ;;; Division by zero was a common error on PPC. It depended on the
177 ;;; return function either being before INTEGER-/-INTEGER in memory,
178 ;;; or more than MOST-POSITIVE-FIXNUM bytes ahead. It also depends on
179 ;;; INTEGER-/-INTEGER calling SIGNED-TRUNCATE. I believe Raymond Toy
180 ;;; says that the Sparc backend (at least for CMUCL) inlines this, so
181 ;;; if SBCL does the same this test is probably not good for the
182 ;;; Sparc.
183 ;;;
184 ;;; Disabling tail call elimination on this will probably ensure that
185 ;;; the return value (to the flet or the enclosing top level form) is
186 ;;; more than MOST-POSITIVE-FIXNUM with the current spaces on OS X.
187 ;;; Enabling it might catch other problems, so do it anyway.
188 (flet ((optimized ()
189          (declare (optimize (speed 2) (debug 1))) ; tail call elimination
190          (/ 42 0))
191        (not-optimized ()
192          (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
193          (/ 42 0))
194        (test (fun)
195          (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
196          (funcall fun)))
197   (with-test (:name (:divide-by-zero :bug-346)
198               :fails-on :alpha)  ; bug 346
199     (assert (verify-backtrace (lambda () (test #'optimized))
200                               (list '(/ 42 &rest)
201                                     (list '(flet test) #'optimized)))))
202   (with-test (:name (:divide-by-zero :bug-356)
203               :fails-on :alpha)  ; bug 356
204     (assert (verify-backtrace (lambda () (test #'not-optimized))
205                               (list '(/ 42 &rest)
206                                     '((flet not-optimized))
207                                     (list '(flet test) #'not-optimized))))))
208
209 (with-test (:name (:throw :no-such-tag)
210             :fails-on '(or
211                         (and :sparc :linux)
212                         :alpha
213                         :mips))
214   (progn
215     (defun throw-test ()
216       (throw 'no-such-tag t))
217     (assert (verify-backtrace #'throw-test '((throw-test))))))
218
219 ;;; test entry point handling in backtraces
220
221 (defun oops ()
222   (error "oops"))
223
224 (defmacro defbt (n ll &body body)
225   `(progn
226      ;; normal debug info
227      (defun ,(intern (format nil "BT.~A.1" n)) ,ll
228        ,@body)
229      ;; no arguments saved
230      (defun ,(intern (format nil "BT.~A.2" n)) ,ll
231        (declare (optimize (debug 1) (speed 3)))
232        ,@body)
233      ;; no lambda-list saved
234      (defun ,(intern (format nil "BT.~A.3" n)) ,ll
235        (declare (optimize (debug 0)))
236        ,@body)))
237
238 (defbt 1 (&key key)
239   (list key))
240
241 (defbt 2 (x)
242   (list x))
243
244 (defbt 3 (&key (key (oops)))
245   (list key))
246
247 ;;; ERROR instead of OOPS so that tail call elimination doesn't happen
248 (defbt 4 (&optional opt)
249   (list (error "error")))
250
251 (defbt 5 (&optional (opt (oops)))
252   (list opt))
253
254 (defmacro with-details (bool &body body)
255   `(let ((sb-debug:*show-entry-point-details* ,bool))
256      ,@body))
257
258 (defun bug-354 (x)
259   (error "XEPs in backtraces: ~S" x))
260
261 (with-test (:name :bug-354)
262   (with-details t
263     (assert (not (verify-backtrace (lambda () (bug-354 354))
264                                    '((bug-354 &rest)
265                                      ((sb-c::tl-xep bug-354) &rest))))))
266   (assert (verify-backtrace (lambda () (bug-354 354)) '((bug-354 354)))))
267
268 ;;; FIXME: This test really should be broken into smaller pieces
269 (with-test (:name (:backtrace :tl-xep))
270   (with-details t
271     (assert (verify-backtrace #'namestring
272                               '(((sb-c::tl-xep namestring) 0 ?)))))
273   (with-details nil
274     (assert (verify-backtrace #'namestring
275                               '((namestring))))))
276
277 (with-test (:name (:backtrace :more-processor))
278   (with-details t
279     (assert (verify-backtrace (lambda () (bt.1.1 :key))
280                               '(((sb-c::&more-processor bt.1.1) &rest))))
281     (assert (verify-backtrace (lambda () (bt.1.2 :key))
282                               '(((sb-c::&more-processor bt.1.2) &rest))))
283     (assert (verify-backtrace (lambda () (bt.1.3 :key))
284                               '(((sb-c::&more-processor bt.1.3) &rest)))))
285   (with-details nil
286     (assert (verify-backtrace (lambda () (bt.1.1 :key))
287                               '((bt.1.1 :key))))
288     (assert (verify-backtrace (lambda () (bt.1.2 :key))
289                               '((bt.1.2 &rest))))
290     (assert (verify-backtrace (lambda () (bt.1.3 :key))
291                               '((bt.1.3 &rest))))))
292
293 (with-test (:name (:backtrace :xep))
294   (with-details t
295     (assert (verify-backtrace #'bt.2.1
296                               '(((sb-c::xep bt.2.1) 0 ?))))
297     (assert (verify-backtrace #'bt.2.2
298                               '(((sb-c::xep bt.2.2) &rest))))
299     (assert (verify-backtrace #'bt.2.3
300                               '(((sb-c::xep bt.2.3) &rest)))))
301   (with-details nil
302     (assert (verify-backtrace #'bt.2.1
303                               '((bt.2.1))))
304     (assert (verify-backtrace #'bt.2.2
305                               '((bt.2.2 &rest))))
306     (assert (verify-backtrace #'bt.2.3
307                               '((bt.2.3 &rest))))))
308
309 (with-test (:name (:backtrace :varargs-entry))
310   (with-details t
311     (assert (verify-backtrace #'bt.3.1
312                               '(((sb-c::varargs-entry bt.3.1) :key nil))))
313     (assert (verify-backtrace #'bt.3.2
314                               '(((sb-c::varargs-entry bt.3.2) :key ?))))
315     (assert (verify-backtrace #'bt.3.3
316                               '(((sb-c::varargs-entry bt.3.3) &rest)))))
317   (with-details nil
318     (assert (verify-backtrace #'bt.3.1
319                               '((bt.3.1 :key nil))))
320     (assert (verify-backtrace #'bt.3.2
321                               '((bt.3.2 :key ?))))
322     (assert (verify-backtrace #'bt.3.3
323                               '((bt.3.3 &rest))))))
324
325 (with-test (:name (:backtrace :hairy-args-processor))
326   (with-details t
327     (assert (verify-backtrace #'bt.4.1
328                               '(((sb-c::hairy-arg-processor bt.4.1) ?))))
329     (assert (verify-backtrace #'bt.4.2
330                               '(((sb-c::hairy-arg-processor bt.4.2) ?))))
331     (assert (verify-backtrace #'bt.4.3
332                               '(((sb-c::hairy-arg-processor bt.4.3) &rest)))))
333   (with-details nil
334     (assert (verify-backtrace #'bt.4.1
335                               '((bt.4.1 ?))))
336     (assert (verify-backtrace #'bt.4.2
337                               '((bt.4.2 ?))))
338     (assert (verify-backtrace #'bt.4.3
339                               '((bt.4.3 &rest))))))
340
341
342 (with-test (:name (:backtrace :optional-processor))
343   (with-details t
344     (assert (verify-backtrace #'bt.5.1
345                               '(((sb-c::&optional-processor bt.5.1)))))
346     (assert (verify-backtrace #'bt.5.2
347                               '(((sb-c::&optional-processor bt.5.2) &rest))))
348     (assert (verify-backtrace #'bt.5.3
349                               '(((sb-c::&optional-processor bt.5.3) &rest)))))
350   (with-details nil
351     (assert (verify-backtrace #'bt.5.1
352                               '((bt.5.1))))
353     (assert (verify-backtrace #'bt.5.2
354                               '((bt.5.2 &rest))))
355     (assert (verify-backtrace #'bt.5.3
356                               '((bt.5.3 &rest))))))
357
358 (write-line "//compile nil")
359 (defvar *compile-nil-error* (compile nil '(lambda (x) (cons (when x (error "oops")) nil))))
360 (defvar *compile-nil-non-tc* (compile nil '(lambda (y) (cons (funcall *compile-nil-error* y) nil))))
361 (with-test (:name (:compile nil))
362   (assert (verify-backtrace (lambda () (funcall *compile-nil-non-tc* 13))
363                             '(((lambda (x)) 13)
364                               ((lambda (y)) 13)))))
365
366 (with-test (:name :clos-slot-typecheckfun-named)
367   (assert
368    (verify-backtrace
369     (lambda ()
370       (eval `(locally (declare (optimize safety))
371                (defclass clos-typecheck-test ()
372                  ((slot :type fixnum)))
373                (setf (slot-value (make-instance 'clos-typecheck-test) 'slot) t))))
374     '(((sb-pcl::slot-typecheck fixnum) t)))))
375
376 (with-test (:name :clos-emf-named)
377   (assert
378    (verify-backtrace
379     (lambda ()
380       (eval `(progn
381                (defmethod clos-emf-named-test ((x symbol)) x)
382                (defmethod clos-emf-named-test :before (x) (assert x))
383                (clos-emf-named-test nil))))
384     '(((sb-pcl::emf clos-emf-named-test) ? ? nil)))))
385
386 (with-test (:name :bug-310173)
387   (flet ((make-fun (n)
388            (let* ((names '(a b))
389                   (req (loop repeat n collect (pop names))))
390              (compile nil
391                       `(lambda (,@req &rest rest)
392                          (let ((* *)) ; no tail-call
393                            (apply '/ ,@req rest)))))))
394     (assert
395      (verify-backtrace (lambda ()
396                          (funcall (make-fun 0) 10 11 0))
397                        '((sb-kernel:two-arg-/ 10/11 0)
398                          (/ 10 11 0)
399                          ((lambda (&rest rest)) 10 11 0))))
400     (assert
401      (verify-backtrace (lambda ()
402                          (funcall (make-fun 1) 10 11 0))
403                        '((sb-kernel:two-arg-/ 10/11 0)
404                          (/ 10 11 0)
405                          ((lambda (a &rest rest)) 10 11 0))))
406     (assert
407      (verify-backtrace (lambda ()
408                          (funcall (make-fun 2) 10 11 0))
409                        '((sb-kernel:two-arg-/ 10/11 0)
410                          (/ 10 11 0)
411                          ((lambda (a b &rest rest)) 10 11 0))))))
412
413 ;;;; test TRACE
414
415 (defun trace-this ()
416   'ok)
417
418 (defun trace-fact (n)
419   (if (zerop n)
420       1
421       (* n (trace-fact (1- n)))))
422
423 (with-test (:name (trace :simple))
424   (let ((out (with-output-to-string (*trace-output*)
425                (trace trace-this)
426                (assert (eq 'ok (trace-this)))
427                (untrace))))
428     (assert (search "TRACE-THIS" out))
429     (assert (search "returned OK" out))))
430
431 ;;; bug 379
432 ;;; This is not a WITH-TEST :FAILS-ON PPC DARWIN since there are
433 ;;; suspicions that the breakpoint trace might corrupt the whole image
434 ;;; on that platform.
435 (with-test (:name (trace :encapsulate nil)
436             :fails-on '(or (and :ppc (not :linux)) :sparc :mips)
437             :broken-on '(or :darwin :sunos))
438   (let ((out (with-output-to-string (*trace-output*)
439                (trace trace-this :encapsulate nil)
440                (assert (eq 'ok (trace-this)))
441                (untrace))))
442     (assert (search "TRACE-THIS" out))
443     (assert (search "returned OK" out))))
444
445 (with-test (:name (trace-recursive :encapsulate nil)
446             :fails-on '(or (and :ppc (not :linux)) :sparc :mips :sunos)
447             :broken-on '(or :darwin (and :x86 :sunos)))
448   (let ((out (with-output-to-string (*trace-output*)
449                (trace trace-fact :encapsulate nil)
450                (assert (= 120 (trace-fact 5)))
451                (untrace))))
452     (assert (search "TRACE-FACT" out))
453     (assert (search "returned 1" out))
454     (assert (search "returned 120" out))))
455
456 (defun trace-and-fmakunbound-this (x)
457   x)
458
459 (with-test (:name :bug-667657)
460   (trace trace-and-fmakunbound-this)
461   (fmakunbound 'trace-and-fmakunbound-this)
462   (untrace)
463   (assert (not (trace))))
464
465 (with-test (:name :bug-414)
466   (handler-bind ((warning #'error))
467     (load (compile-file "bug-414.lisp"))
468     (disassemble 'bug-414)))
469
470 (with-test (:name :bug-310175)
471   (let ((dx-arg (cons t t)))
472     (declare (dynamic-extent dx-arg))
473     (flet ((dx-arg-backtrace (x)
474              (declare (optimize (debug 2)))
475              (prog1 (sb-debug:backtrace-as-list 10)
476                (assert (sb-debug::stack-allocated-p x)))))
477       (declare (notinline dx-arg-backtrace))
478       (assert (member-if (lambda (frame)
479                            (and (consp frame)
480                                 (equal '(flet dx-arg-backtrace) (car frame))
481                                 (notany #'sb-debug::stack-allocated-p (cdr frame))))
482                          (dx-arg-backtrace dx-arg))))))
483
484 (with-test (:name :bug-795245)
485   (assert
486    (eq :ok
487        (catch 'done
488          (handler-bind
489              ((error (lambda (e)
490                        (declare (ignore e))
491                        (handler-case
492                            (sb-debug:backtrace 100 (make-broadcast-stream))
493                          (error ()
494                            (throw 'done :error))
495                          (:no-error ()
496                            (throw 'done :ok))))))
497            (apply '/= nil 1 2 nil))))))
498
499 ;;;; test infinite error protection
500
501 (defmacro nest-errors (n-levels error-form)
502   (if (< 0 n-levels)
503       `(handler-bind ((error (lambda (condition)
504                                (declare (ignore condition))
505                                ,error-form)))
506         (nest-errors ,(1- n-levels) ,error-form))
507       error-form))
508
509 (defun erroring-debugger-hook (condition old-debugger-hook)
510   (let ((*debugger-hook* old-debugger-hook))
511     (format t "recursive condition: ~A~%" condition) (force-output)
512     (error "recursive condition: ~A" condition)))
513
514 (defun test-inifinite-error-protection ()
515   ;; after 50 successful throws to SB-IMPL::TOPLEVEL-CATCHER sbcl used
516   ;; to halt, it produces so much garbage that's hard to suppress that
517   ;; it is tested only once
518   (write-line "--HARMLESS BUT ALARMING BACKTRACE COMING UP--")
519   (let ((*debugger-hook* #'erroring-debugger-hook))
520     (loop repeat 1 do
521           (let ((error-counter 0)
522                 (*terminal-io* (make-broadcast-stream)))
523             (assert
524              (not (eq
525                    :normal-exit
526                    (catch 'sb-impl::toplevel-catcher
527                      (nest-errors 20 (error "infinite error ~s"
528                                             (incf error-counter)))
529                      :normal-exit)))))))
530   (write-line "--END OF H-B-A-B--"))
531
532 (with-test (:name infinite-error-protection)
533   (enable-debugger)
534   (test-inifinite-error-protection))
535
536 (with-test (:name (infinite-error-protection :thread)
537                   :skipped-on '(not :sb-thread))
538   (enable-debugger)
539   (let ((thread (sb-thread:make-thread #'test-inifinite-error-protection)))
540     (loop while (sb-thread:thread-alive-p thread))))
541
542 ;; unconditional, in case either previous left it enabled
543 (disable-debugger)
544
545 (write-line "/debug.impure.lisp done")