1.0.0.2: TRACE :ENCAPSULATE NIL, plus other minor Windows improvements
[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.) FIXME: On Windows we get two
123                         ;; extra foreign frames below regular frames.
124                         (let ((end (last backtrace #-win32 2 #+win32 4)))
125                           (unless (equal (caar end)
126                                          (if *show-entry-point-details*
127                                              '(sb-c::tl-xep sb-impl::toplevel-init)
128                                              'sb-impl::toplevel-init))
129                             (print (list :backtrace-stunted (caar end)))
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   '(#+(or x86 x86-64) "bogus stack frame"
138     #-(or x86 x86-64) "undefined function"))
139
140 ;;; Test for "undefined function" (undefined_tramp) working properly.
141 ;;; Try it with and without tail call elimination, since they can have
142 ;;; different effects.  (Specifically, if undefined_tramp is incorrect
143 ;;; a stunted stack can result from the tail call variant.)
144 (flet ((optimized ()
145          (declare (optimize (speed 2) (debug 1))) ; tail call elimination
146          (#:undefined-function 42))
147        (not-optimized ()
148          (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
149          (#:undefined-function 42))
150        (test (fun)
151          (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
152          (funcall fun)))
153
154   (with-test (:name (:undefined-function :bug-346)
155               :fails-on '(or :alpha))   ; bug 346
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 :x86 :x86-64 :alpha))
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 '(or :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 '(or :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 :x86 (or :linux :freebsd sunos))
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 ;;; FIXME: This test really should be broken into smaller pieces
255 (with-test (:name (:backtrace :misc)
256             :fails-on '(and :x86 (or :linux :sunos)))
257   (macrolet ((with-details (bool &body body)
258                `(let ((sb-debug:*show-entry-point-details* ,bool))
259                  ,@body)))
260
261     ;; TL-XEP
262     (print :tl-xep)
263     (with-details t
264       (assert (verify-backtrace #'namestring
265                                 '(((sb-c::tl-xep namestring) 0 ?)))))
266     (with-details nil
267       (assert (verify-backtrace #'namestring
268                                 '((namestring)))))
269
270
271     ;; &MORE-PROCESSOR
272     (with-details t
273       (assert (verify-backtrace (lambda () (bt.1.1 :key))
274                                 '(((sb-c::&more-processor bt.1.1) &rest))))
275       (assert (verify-backtrace (lambda () (bt.1.2 :key))
276                                 '(((sb-c::&more-processor bt.1.2) &rest))))
277       (assert (verify-backtrace (lambda () (bt.1.3 :key))
278                                 '(((sb-c::&more-processor bt.1.3) &rest)))))
279     (with-details nil
280       (assert (verify-backtrace (lambda () (bt.1.1 :key))
281                                 '((bt.1.1 :key))))
282       (assert (verify-backtrace (lambda () (bt.1.2 :key))
283                                 '((bt.1.2 &rest))))
284       (assert (verify-backtrace (lambda () (bt.1.3 :key))
285                                 '((bt.1.3 &rest)))))
286
287     ;; XEP
288     (print :xep)
289     (with-details t
290       (assert (verify-backtrace #'bt.2.1
291                                 '(((sb-c::xep bt.2.1) 0 ?))))
292       (assert (verify-backtrace #'bt.2.2
293                                 '(((sb-c::xep bt.2.2) &rest))))
294       (assert (verify-backtrace #'bt.2.3
295                                 '(((sb-c::xep bt.2.3) &rest)))))
296     (with-details nil
297       (assert (verify-backtrace #'bt.2.1
298                                 '((bt.2.1))))
299       (assert (verify-backtrace #'bt.2.2
300                                 '((bt.2.2 &rest))))
301       (assert (verify-backtrace #'bt.2.3
302                                 '((bt.2.3 &rest)))))
303
304     ;; VARARGS-ENTRY
305     (print :varargs-entry)
306     (with-details t
307       (assert (verify-backtrace #'bt.3.1
308                                 '(((sb-c::varargs-entry bt.3.1) :key nil))))
309       (assert (verify-backtrace #'bt.3.2
310                                 '(((sb-c::varargs-entry bt.3.2) :key ?))))
311       (assert (verify-backtrace #'bt.3.3
312                                 '(((sb-c::varargs-entry bt.3.3) &rest)))))
313     (with-details nil
314       (assert (verify-backtrace #'bt.3.1
315                                 '((bt.3.1 :key nil))))
316       (assert (verify-backtrace #'bt.3.2
317                                 '((bt.3.2 :key ?))))
318       (assert (verify-backtrace #'bt.3.3
319                                 '((bt.3.3 &rest)))))
320
321     ;; HAIRY-ARG-PROCESSOR
322     (print :hairy-args-processor)
323     (with-details t
324       (assert (verify-backtrace #'bt.4.1
325                                 '(((sb-c::hairy-arg-processor bt.4.1) ?))))
326       (assert (verify-backtrace #'bt.4.2
327                                 '(((sb-c::hairy-arg-processor bt.4.2) ?))))
328       (assert (verify-backtrace #'bt.4.3
329                                 '(((sb-c::hairy-arg-processor bt.4.3) &rest)))))
330     (with-details nil
331       (assert (verify-backtrace #'bt.4.1
332                                 '((bt.4.1 ?))))
333       (assert (verify-backtrace #'bt.4.2
334                                 '((bt.4.2 ?))))
335       (assert (verify-backtrace #'bt.4.3
336                                 '((bt.4.3 &rest)))))
337
338     ;; &OPTIONAL-PROCESSOR
339     (print :optional-processor)
340     (with-details t
341       (assert (verify-backtrace #'bt.5.1
342                                 '(((sb-c::&optional-processor bt.5.1)))))
343       (assert (verify-backtrace #'bt.5.2
344                                 '(((sb-c::&optional-processor bt.5.2) &rest))))
345       (assert (verify-backtrace #'bt.5.3
346                                 '(((sb-c::&optional-processor bt.5.3) &rest)))))
347     (with-details nil
348       (assert (verify-backtrace #'bt.5.1
349                                 '((bt.5.1))))
350       (assert (verify-backtrace #'bt.5.2
351                                 '((bt.5.2 &rest))))
352       (assert (verify-backtrace #'bt.5.3
353                                 '((bt.5.3 &rest)))))))
354
355 ;;;; test TRACE
356
357 (defun trace-this ()
358   'ok)
359
360 (defun trace-fact (n)
361   (if (zerop n)
362       1
363       (* n (trace-fact (1- n)))))
364
365 (let ((out (with-output-to-string (*trace-output*)
366              (trace trace-this)
367              (assert (eq 'ok (trace-this)))
368              (untrace))))
369   (assert (search "TRACE-THIS" out))
370   (assert (search "returned OK" out)))
371
372 ;;; bug 379
373 ;;; This is not a WITH-TEST :FAILS-ON PPC DARWIN since there are
374 ;;; suspicions that the breakpoint trace might corrupt the whole image
375 ;;; on that platform.
376 #-(and (or ppc x86) darwin)
377 (with-test (:name (trace :encapsulate nil)
378             :fails-on '(or :ppc :sparc :mips))
379   (let ((out (with-output-to-string (*trace-output*)
380                (trace trace-this :encapsulate nil)
381                (assert (eq 'ok (trace-this)))
382                (untrace))))
383     (assert (search "TRACE-THIS" out))
384     (assert (search "returned OK" out))))
385
386 #-(and (or ppc x86) darwin)
387 (with-test (:name (trace-recursive :encapsulate nil)
388             :fails-on '(or :ppc :sparc :mips))
389   (let ((out (with-output-to-string (*trace-output*)
390                (trace trace-fact :encapsulate nil)
391                (assert (= 120 (trace-fact 5)))
392                (untrace))))
393     (assert (search "TRACE-FACT" out))
394     (assert (search "returned 1" out))
395     (assert (search "returned 120" out))))
396
397 ;;;; test infinite error protection
398
399 (defmacro nest-errors (n-levels error-form)
400   (if (< 0 n-levels)
401       `(handler-bind ((error (lambda (condition)
402                                (declare (ignore condition))
403                                ,error-form)))
404         (nest-errors ,(1- n-levels) ,error-form))
405       error-form))
406
407 (defun erroring-debugger-hook (condition old-debugger-hook)
408   (let ((*debugger-hook* old-debugger-hook))
409     (format t "recursive condition: ~A~%" condition) (force-output)
410     (error "recursive condition: ~A" condition)))
411
412 (defun test-inifinite-error-protection ()
413   ;; after 50 successful throws to SB-IMPL::TOPLEVEL-CATCHER sbcl used
414   ;; to halt, it produces so much garbage that's hard to suppress that
415   ;; it is tested only once
416   (let ((*debugger-hook* #'erroring-debugger-hook))
417     (loop repeat 1 do
418           (let ((error-counter 0)
419                 (*terminal-io* (make-broadcast-stream)))
420             (assert
421              (not (eq
422                    :normal-exit
423                    (catch 'sb-impl::toplevel-catcher
424                      (nest-errors 20 (error "infinite error ~s"
425                                             (incf error-counter)))
426                      :normal-exit))))))))
427
428 (enable-debugger)
429
430 (test-inifinite-error-protection)
431
432 #+sb-thread
433 (let ((thread (sb-thread:make-thread #'test-inifinite-error-protection)))
434   (loop while (sb-thread:thread-alive-p thread)))
435
436 (disable-debugger)
437
438 (write-line "/debug.impure.lisp done")