0.9.16.27:
[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 (destructuring-bind (object-sym &optional-sym stream-sym) (get-arglist #'print)
65   (assert (symbolp object-sym))
66   (assert (eql &optional-sym '&optional))
67   (assert (symbolp stream-sym)))
68 (destructuring-bind (dest-sym control-sym &rest-sym format-args-sym)
69     (get-arglist #'format)
70   (assert (symbolp dest-sym))
71   (assert (symbolp control-sym))
72   (assert (eql &rest-sym '&rest))
73   (assert (symbolp format-args-sym)))
74
75 ;;; Check for backtraces generally being correct.  Ensure that the
76 ;;; actual backtrace finishes (doesn't signal any errors on its own),
77 ;;; and that it contains the frames we expect, doesn't contain any
78 ;;; "bogus stack frame"s, and contains the appropriate toplevel call
79 ;;; and hasn't been cut off anywhere.
80 (defun verify-backtrace (test-function frame-specs &key (allow-stunted nil))
81   (labels ((args-equal (want real)
82              (cond ((eq '&rest (car want))
83                     t)
84                    ((endp want)
85                     (endp real))
86                    ((or (eq '? (car want)) (equal (car want) (car real)))
87                     (args-equal (cdr want) (cdr real)))
88                    (t
89                     nil))))
90     (let ((result nil))
91       (block outer-handler
92         (handler-bind
93             ((error (lambda (condition)
94                       ;; find the part of the backtrace we're interested in
95                       (let ((backtrace (progn
96                                          ;; (backtrace 13)
97                                          (member (caar frame-specs)
98                                                  (sb-debug:backtrace-as-list)
99                                                  :key #'car
100                                                  :test #'equal))))
101
102                         (setf result condition)
103
104                         (unless backtrace
105                           (print :missing-backtrace)
106                           (setf result nil))
107
108                         ;; check that we have all the frames we wanted
109                         (mapcar
110                          (lambda (spec frame)
111                            (unless (or (not spec)
112                                        (and (equal (car spec) (car frame))
113                                             (args-equal (cdr spec)
114                                                         (cdr frame))))
115                              (print (list :mismatch spec frame))
116                              (setf result nil)))
117                          frame-specs
118                          backtrace)
119
120                         ;; Make sure the backtrace isn't stunted in
121                         ;; any way.  (Depends on running in the main
122                         ;; thread.)
123                         (let ((end (last backtrace 2)))
124                           (unless (equal (caar end)
125                                          (if *show-entry-point-details*
126                                              '(sb-c::tl-xep sb-impl::toplevel-init)
127                                              'sb-impl::toplevel-init))
128                             (print (list :backtrace-stunted (caar end)))
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))   ; bug 346
155     (assert (verify-backtrace
156              (lambda () (test #'optimized))
157              (list *undefined-function-frame*
158                    (list '(flet test) #'optimized)))))
159
160   ;; bug 353: This test fails at least most of the time for x86/linux
161   ;; ca. 0.8.20.16. -- WHN
162   (with-test (:name (:undefined-function :bug-356)
163               :fails-on '(or (and :x86 :linux) :alpha))
164     (assert (verify-backtrace
165              (lambda () (test #'not-optimized))
166              (list *undefined-function-frame*
167                    (list '(flet not-optimized))
168                    (list '(flet test) #'not-optimized))))))
169
170 ;;; Division by zero was a common error on PPC.  It depended on the
171 ;;; return function either being before INTEGER-/-INTEGER in memory,
172 ;;; or more than MOST-POSITIVE-FIXNUM bytes ahead.  It also depends on
173 ;;; INTEGER-/-INTEGER calling SIGNED-TRUNCATE.  I believe Raymond Toy
174 ;;; says that the Sparc backend (at least for CMUCL) inlines this, so
175 ;;; if SBCL does the same this test is probably not good for the
176 ;;; Sparc.
177 ;;;
178 ;;; Disabling tail call elimination on this will probably ensure that
179 ;;; the return value (to the flet or the enclosing top level form) is
180 ;;; more than MOST-POSITIVE-FIXNUM with the current spaces on OS X.
181 ;;; Enabling it might catch other problems, so do it anyway.
182 (flet ((optimized ()
183          (declare (optimize (speed 2) (debug 1))) ; tail call elimination
184          (/ 42 0))
185        (not-optimized ()
186          (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
187          (/ 42 0))
188        (test (fun)
189          (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
190          (funcall fun)))
191   (with-test (:name (:divide-by-zero :bug-346)
192               :fails-on '(or :alpha))   ; bug 346
193     (assert (verify-backtrace (lambda () (test #'optimized))
194                               (list '(/ 42 &rest)
195                                     (list '(flet test) #'optimized)))))
196   (with-test (:name (:divide-by-zero :bug-356)
197               :fails-on '(or :alpha))   ; bug 356
198     (assert (verify-backtrace (lambda () (test #'not-optimized))
199                               (list '(/ 42 &rest)
200                                     '((flet not-optimized))
201                                     (list '(flet test) #'not-optimized))))))
202
203 (with-test (:name (:throw :no-such-tag)
204             :fails-on '(or
205                         (and :x86 (or :linux :freebsd sunos))
206                         :alpha
207                         :mips))
208   (progn
209     (defun throw-test ()
210       (throw 'no-such-tag t))
211     (assert (verify-backtrace #'throw-test '((throw-test))))))
212
213 ;;; test entry point handling in backtraces
214
215 (defun oops ()
216   (error "oops"))
217
218 (defmacro defbt (n ll &body body)
219   `(progn
220      ;; normal debug info
221      (defun ,(intern (format nil "BT.~A.1" n)) ,ll
222        ,@body)
223      ;; no arguments saved
224      (defun ,(intern (format nil "BT.~A.2" n)) ,ll
225        (declare (optimize (debug 1) (speed 3)))
226        ,@body)
227      ;; no lambda-list saved
228      (defun ,(intern (format nil "BT.~A.3" n)) ,ll
229        (declare (optimize (debug 0)))
230        ,@body)))
231
232 (defbt 1 (&key key)
233   (list key))
234
235 (defbt 2 (x)
236   (list x))
237
238 (defbt 3 (&key (key (oops)))
239   (list key))
240
241 ;;; ERROR instead of OOPS so that tail call elimination doesn't happen
242 (defbt 4 (&optional opt)
243   (list (error "error")))
244
245 (defbt 5 (&optional (opt (oops)))
246   (list opt))
247
248 ;;; FIXME: This test really should be broken into smaller pieces
249 (with-test (:name (:backtrace :misc)
250             :fails-on '(and :x86 (or :linux :sunos)))
251   (macrolet ((with-details (bool &body body)
252                `(let ((sb-debug:*show-entry-point-details* ,bool))
253                  ,@body)))
254
255     ;; TL-XEP
256     (print :tl-xep)
257     (with-details t
258       (assert (verify-backtrace #'namestring
259                                 '(((sb-c::tl-xep namestring) 0 ?)))))
260     (with-details nil
261       (assert (verify-backtrace #'namestring
262                                 '((namestring)))))
263
264
265     ;; &MORE-PROCESSOR
266     (with-details t
267       (assert (verify-backtrace (lambda () (bt.1.1 :key))
268                                 '(((sb-c::&more-processor bt.1.1) &rest))))
269       (assert (verify-backtrace (lambda () (bt.1.2 :key))
270                                 '(((sb-c::&more-processor bt.1.2) &rest))))
271       (assert (verify-backtrace (lambda () (bt.1.3 :key))
272                                 '(((sb-c::&more-processor bt.1.3) &rest)))))
273     (with-details nil
274       (assert (verify-backtrace (lambda () (bt.1.1 :key))
275                                 '((bt.1.1 :key))))
276       (assert (verify-backtrace (lambda () (bt.1.2 :key))
277                                 '((bt.1.2 &rest))))
278       (assert (verify-backtrace (lambda () (bt.1.3 :key))
279                                 '((bt.1.3 &rest)))))
280
281     ;; XEP
282     (print :xep)
283     (with-details t
284       (assert (verify-backtrace #'bt.2.1
285                                 '(((sb-c::xep bt.2.1) 0 ?))))
286       (assert (verify-backtrace #'bt.2.2
287                                 '(((sb-c::xep bt.2.2) &rest))))
288       (assert (verify-backtrace #'bt.2.3
289                                 '(((sb-c::xep bt.2.3) &rest)))))
290     (with-details nil
291       (assert (verify-backtrace #'bt.2.1
292                                 '((bt.2.1))))
293       (assert (verify-backtrace #'bt.2.2
294                                 '((bt.2.2 &rest))))
295       (assert (verify-backtrace #'bt.2.3
296                                 '((bt.2.3 &rest)))))
297
298     ;; VARARGS-ENTRY
299     (print :varargs-entry)
300     (with-details t
301       (assert (verify-backtrace #'bt.3.1
302                                 '(((sb-c::varargs-entry bt.3.1) :key nil))))
303       (assert (verify-backtrace #'bt.3.2
304                                 '(((sb-c::varargs-entry bt.3.2) :key ?))))
305       (assert (verify-backtrace #'bt.3.3
306                                 '(((sb-c::varargs-entry bt.3.3) &rest)))))
307     (with-details nil
308       (assert (verify-backtrace #'bt.3.1
309                                 '((bt.3.1 :key nil))))
310       (assert (verify-backtrace #'bt.3.2
311                                 '((bt.3.2 :key ?))))
312       (assert (verify-backtrace #'bt.3.3
313                                 '((bt.3.3 &rest)))))
314
315     ;; HAIRY-ARG-PROCESSOR
316     (print :hairy-args-processor)
317     (with-details t
318       (assert (verify-backtrace #'bt.4.1
319                                 '(((sb-c::hairy-arg-processor bt.4.1) ?))))
320       (assert (verify-backtrace #'bt.4.2
321                                 '(((sb-c::hairy-arg-processor bt.4.2) ?))))
322       (assert (verify-backtrace #'bt.4.3
323                                 '(((sb-c::hairy-arg-processor bt.4.3) &rest)))))
324     (with-details nil
325       (assert (verify-backtrace #'bt.4.1
326                                 '((bt.4.1 ?))))
327       (assert (verify-backtrace #'bt.4.2
328                                 '((bt.4.2 ?))))
329       (assert (verify-backtrace #'bt.4.3
330                                 '((bt.4.3 &rest)))))
331
332     ;; &OPTIONAL-PROCESSOR
333     (print :optional-processor)
334     (with-details t
335       (assert (verify-backtrace #'bt.5.1
336                                 '(((sb-c::&optional-processor bt.5.1)))))
337       (assert (verify-backtrace #'bt.5.2
338                                 '(((sb-c::&optional-processor bt.5.2) &rest))))
339       (assert (verify-backtrace #'bt.5.3
340                                 '(((sb-c::&optional-processor bt.5.3) &rest)))))
341     (with-details nil
342       (assert (verify-backtrace #'bt.5.1
343                                 '((bt.5.1))))
344       (assert (verify-backtrace #'bt.5.2
345                                 '((bt.5.2 &rest))))
346       (assert (verify-backtrace #'bt.5.3
347                                 '((bt.5.3 &rest)))))))
348
349 ;;;; test TRACE
350
351 (defun trace-this ()
352   'ok)
353
354 (defun trace-fact (n)
355   (if (zerop n)
356       1
357       (* n (trace-fact (1- n)))))
358
359 (let ((out (with-output-to-string (*trace-output*)
360              (trace trace-this)
361              (assert (eq 'ok (trace-this)))
362              (untrace))))
363   (assert (search "TRACE-THIS" out))
364   (assert (search "returned OK" out)))
365
366 ;;; bug 379
367 ;;; This is not a WITH-TEST :FAILS-ON PPC DARWIN since there are
368 ;;; suspicions that the breakpoint trace might corrupt the whole image
369 ;;; on that platform.
370 #-(and (or ppc x86) darwin)
371 (with-test (:name (trace :encapsulate nil)
372             :fails-on '(or :ppc :sparc :mips))
373   (let ((out (with-output-to-string (*trace-output*)
374                (trace trace-this :encapsulate nil)
375                (assert (eq 'ok (trace-this)))
376                (untrace))))
377     (assert (search "TRACE-THIS" out))
378     (assert (search "returned OK" out))))
379
380 #-(and (or ppc x86) darwin)
381 (with-test (:name (trace-recursive :encapsulate nil)
382             :fails-on '(or :ppc :sparc :mips))
383   (let ((out (with-output-to-string (*trace-output*)
384                (trace trace-fact :encapsulate nil)
385                (assert (= 120 (trace-fact 5)))
386                (untrace))))
387     (assert (search "TRACE-FACT" out))
388     (assert (search "returned 1" out))
389     (assert (search "returned 120" out))))
390
391 ;;;; test infinite error protection
392
393 (defmacro nest-errors (n-levels error-form)
394   (if (< 0 n-levels)
395       `(handler-bind ((error (lambda (condition)
396                                (declare (ignore condition))
397                                ,error-form)))
398         (nest-errors ,(1- n-levels) ,error-form))
399       error-form))
400
401 (defun erroring-debugger-hook (condition old-debugger-hook)
402   (let ((*debugger-hook* old-debugger-hook))
403     (format t "recursive condition: ~A~%" condition) (force-output)
404     (error "recursive condition: ~A" condition)))
405
406 (defun test-inifinite-error-protection ()
407   ;; after 50 successful throws to SB-IMPL::TOPLEVEL-CATCHER sbcl used
408   ;; to halt, it produces so much garbage that's hard to suppress that
409   ;; it is tested only once
410   (let ((*debugger-hook* #'erroring-debugger-hook))
411     (loop repeat 1 do
412           (let ((error-counter 0)
413                 (*terminal-io* (make-broadcast-stream)))
414             (assert
415              (not (eq
416                    :normal-exit
417                    (catch 'sb-impl::toplevel-catcher
418                      (nest-errors 20 (error "infinite error ~s"
419                                             (incf error-counter)))
420                      :normal-exit))))))))
421
422 (enable-debugger)
423
424 (test-inifinite-error-protection)
425
426 #+sb-thread
427 (let ((thread (sb-thread:make-thread #'test-inifinite-error-protection)))
428   (loop while (sb-thread:thread-alive-p thread)))
429
430 (disable-debugger)