more robust backtraces for syscalls on x86
[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 (with-test (:name :interrupted-syscall)
177   (let ((m (sb-thread:make-mutex))
178         (q (sb-thread:make-waitqueue)))
179     (assert (verify-backtrace
180             (lambda ()
181               (sb-thread:with-mutex (m)
182                 (handler-bind ((timeout (lambda (c)
183                                           (error "foo"))))
184                   (with-timeout 0.1
185                     (sb-thread:condition-wait q m)))))
186             `((sb-thread:condition-wait ,q ,m))))))
187
188 ;;; Division by zero was a common error on PPC. It depended on the
189 ;;; return function either being before INTEGER-/-INTEGER in memory,
190 ;;; or more than MOST-POSITIVE-FIXNUM bytes ahead. It also depends on
191 ;;; INTEGER-/-INTEGER calling SIGNED-TRUNCATE. I believe Raymond Toy
192 ;;; says that the Sparc backend (at least for CMUCL) inlines this, so
193 ;;; if SBCL does the same this test is probably not good for the
194 ;;; Sparc.
195 ;;;
196 ;;; Disabling tail call elimination on this will probably ensure that
197 ;;; the return value (to the flet or the enclosing top level form) is
198 ;;; more than MOST-POSITIVE-FIXNUM with the current spaces on OS X.
199 ;;; Enabling it might catch other problems, so do it anyway.
200 (flet ((optimized ()
201          (declare (optimize (speed 2) (debug 1))) ; tail call elimination
202          (/ 42 0))
203        (not-optimized ()
204          (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
205          (/ 42 0))
206        (test (fun)
207          (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
208          (funcall fun)))
209   (with-test (:name (:divide-by-zero :bug-346)
210               :fails-on :alpha)  ; bug 346
211     (assert (verify-backtrace (lambda () (test #'optimized))
212                               (list '(/ 42 &rest)
213                                     (list '(flet test) #'optimized)))))
214   (with-test (:name (:divide-by-zero :bug-356)
215               :fails-on :alpha)  ; bug 356
216     (assert (verify-backtrace (lambda () (test #'not-optimized))
217                               (list '(/ 42 &rest)
218                                     '((flet not-optimized))
219                                     (list '(flet test) #'not-optimized))))))
220
221 (with-test (:name (:throw :no-such-tag)
222             :fails-on '(or
223                         (and :sparc :linux)
224                         :alpha
225                         :mips))
226   (progn
227     (defun throw-test ()
228       (throw 'no-such-tag t))
229     (assert (verify-backtrace #'throw-test '((throw-test))))))
230
231 ;;; test entry point handling in backtraces
232
233 (defun oops ()
234   (error "oops"))
235
236 (defmacro defbt (n ll &body body)
237   `(progn
238      ;; normal debug info
239      (defun ,(intern (format nil "BT.~A.1" n)) ,ll
240        ,@body)
241      ;; no arguments saved
242      (defun ,(intern (format nil "BT.~A.2" n)) ,ll
243        (declare (optimize (debug 1) (speed 3)))
244        ,@body)
245      ;; no lambda-list saved
246      (defun ,(intern (format nil "BT.~A.3" n)) ,ll
247        (declare (optimize (debug 0)))
248        ,@body)))
249
250 (defbt 1 (&key key)
251   (list key))
252
253 (defbt 2 (x)
254   (list x))
255
256 (defbt 3 (&key (key (oops)))
257   (list key))
258
259 ;;; ERROR instead of OOPS so that tail call elimination doesn't happen
260 (defbt 4 (&optional opt)
261   (list (error "error")))
262
263 (defbt 5 (&optional (opt (oops)))
264   (list opt))
265
266 (defmacro with-details (bool &body body)
267   `(let ((sb-debug:*show-entry-point-details* ,bool))
268      ,@body))
269
270 (defun bug-354 (x)
271   (error "XEPs in backtraces: ~S" x))
272
273 (with-test (:name :bug-354)
274   (with-details t
275     (assert (not (verify-backtrace (lambda () (bug-354 354))
276                                    '((bug-354 &rest)
277                                      ((sb-c::tl-xep bug-354) &rest))))))
278   (assert (verify-backtrace (lambda () (bug-354 354)) '((bug-354 354)))))
279
280 ;;; FIXME: This test really should be broken into smaller pieces
281 (with-test (:name (:backtrace :tl-xep))
282   (with-details t
283     (assert (verify-backtrace #'namestring
284                               '(((sb-c::tl-xep namestring) 0 ?)))))
285   (with-details nil
286     (assert (verify-backtrace #'namestring
287                               '((namestring))))))
288
289 (with-test (:name (:backtrace :more-processor))
290   (with-details t
291     (assert (verify-backtrace (lambda () (bt.1.1 :key))
292                               '(((sb-c::&more-processor bt.1.1) &rest))))
293     (assert (verify-backtrace (lambda () (bt.1.2 :key))
294                               '(((sb-c::&more-processor bt.1.2) &rest))))
295     (assert (verify-backtrace (lambda () (bt.1.3 :key))
296                               '(((sb-c::&more-processor bt.1.3) &rest)))))
297   (with-details nil
298     (assert (verify-backtrace (lambda () (bt.1.1 :key))
299                               '((bt.1.1 :key))))
300     (assert (verify-backtrace (lambda () (bt.1.2 :key))
301                               '((bt.1.2 &rest))))
302     (assert (verify-backtrace (lambda () (bt.1.3 :key))
303                               '((bt.1.3 &rest))))))
304
305 (with-test (:name (:backtrace :xep))
306   (with-details t
307     (assert (verify-backtrace #'bt.2.1
308                               '(((sb-c::xep bt.2.1) 0 ?))))
309     (assert (verify-backtrace #'bt.2.2
310                               '(((sb-c::xep bt.2.2) &rest))))
311     (assert (verify-backtrace #'bt.2.3
312                               '(((sb-c::xep bt.2.3) &rest)))))
313   (with-details nil
314     (assert (verify-backtrace #'bt.2.1
315                               '((bt.2.1))))
316     (assert (verify-backtrace #'bt.2.2
317                               '((bt.2.2 &rest))))
318     (assert (verify-backtrace #'bt.2.3
319                               '((bt.2.3 &rest))))))
320
321 (with-test (:name (:backtrace :varargs-entry))
322   (with-details t
323     (assert (verify-backtrace #'bt.3.1
324                               '(((sb-c::varargs-entry bt.3.1) :key nil))))
325     (assert (verify-backtrace #'bt.3.2
326                               '(((sb-c::varargs-entry bt.3.2) :key ?))))
327     (assert (verify-backtrace #'bt.3.3
328                               '(((sb-c::varargs-entry bt.3.3) &rest)))))
329   (with-details nil
330     (assert (verify-backtrace #'bt.3.1
331                               '((bt.3.1 :key nil))))
332     (assert (verify-backtrace #'bt.3.2
333                               '((bt.3.2 :key ?))))
334     (assert (verify-backtrace #'bt.3.3
335                               '((bt.3.3 &rest))))))
336
337 (with-test (:name (:backtrace :hairy-args-processor))
338   (with-details t
339     (assert (verify-backtrace #'bt.4.1
340                               '(((sb-c::hairy-arg-processor bt.4.1) ?))))
341     (assert (verify-backtrace #'bt.4.2
342                               '(((sb-c::hairy-arg-processor bt.4.2) ?))))
343     (assert (verify-backtrace #'bt.4.3
344                               '(((sb-c::hairy-arg-processor bt.4.3) &rest)))))
345   (with-details nil
346     (assert (verify-backtrace #'bt.4.1
347                               '((bt.4.1 ?))))
348     (assert (verify-backtrace #'bt.4.2
349                               '((bt.4.2 ?))))
350     (assert (verify-backtrace #'bt.4.3
351                               '((bt.4.3 &rest))))))
352
353
354 (with-test (:name (:backtrace :optional-processor))
355   (with-details t
356     (assert (verify-backtrace #'bt.5.1
357                               '(((sb-c::&optional-processor bt.5.1)))))
358     (assert (verify-backtrace #'bt.5.2
359                               '(((sb-c::&optional-processor bt.5.2) &rest))))
360     (assert (verify-backtrace #'bt.5.3
361                               '(((sb-c::&optional-processor bt.5.3) &rest)))))
362   (with-details nil
363     (assert (verify-backtrace #'bt.5.1
364                               '((bt.5.1))))
365     (assert (verify-backtrace #'bt.5.2
366                               '((bt.5.2 &rest))))
367     (assert (verify-backtrace #'bt.5.3
368                               '((bt.5.3 &rest))))))
369
370 (write-line "//compile nil")
371 (defvar *compile-nil-error* (compile nil '(lambda (x) (cons (when x (error "oops")) nil))))
372 (defvar *compile-nil-non-tc* (compile nil '(lambda (y) (cons (funcall *compile-nil-error* y) nil))))
373 (with-test (:name (:compile nil))
374   (assert (verify-backtrace (lambda () (funcall *compile-nil-non-tc* 13))
375                             '(((lambda (x)) 13)
376                               ((lambda (y)) 13)))))
377
378 (with-test (:name :clos-slot-typecheckfun-named)
379   (assert
380    (verify-backtrace
381     (lambda ()
382       (eval `(locally (declare (optimize safety))
383                (defclass clos-typecheck-test ()
384                  ((slot :type fixnum)))
385                (setf (slot-value (make-instance 'clos-typecheck-test) 'slot) t))))
386     '(((sb-pcl::slot-typecheck fixnum) t)))))
387
388 (with-test (:name :clos-emf-named)
389   (assert
390    (verify-backtrace
391     (lambda ()
392       (eval `(progn
393                (defmethod clos-emf-named-test ((x symbol)) x)
394                (defmethod clos-emf-named-test :before (x) (assert x))
395                (clos-emf-named-test nil))))
396     '(((sb-pcl::emf clos-emf-named-test) ? ? nil)))))
397
398 (with-test (:name :bug-310173)
399   (flet ((make-fun (n)
400            (let* ((names '(a b))
401                   (req (loop repeat n collect (pop names))))
402              (compile nil
403                       `(lambda (,@req &rest rest)
404                          (let ((* *)) ; no tail-call
405                            (apply '/ ,@req rest)))))))
406     (assert
407      (verify-backtrace (lambda ()
408                          (funcall (make-fun 0) 10 11 0))
409                        '((sb-kernel:two-arg-/ 10/11 0)
410                          (/ 10 11 0)
411                          ((lambda (&rest rest)) 10 11 0))))
412     (assert
413      (verify-backtrace (lambda ()
414                          (funcall (make-fun 1) 10 11 0))
415                        '((sb-kernel:two-arg-/ 10/11 0)
416                          (/ 10 11 0)
417                          ((lambda (a &rest rest)) 10 11 0))))
418     (assert
419      (verify-backtrace (lambda ()
420                          (funcall (make-fun 2) 10 11 0))
421                        '((sb-kernel:two-arg-/ 10/11 0)
422                          (/ 10 11 0)
423                          ((lambda (a b &rest rest)) 10 11 0))))))
424
425 ;;;; test TRACE
426
427 (defun trace-this ()
428   'ok)
429
430 (defun trace-fact (n)
431   (if (zerop n)
432       1
433       (* n (trace-fact (1- n)))))
434
435 (with-test (:name (trace :simple))
436   (let ((out (with-output-to-string (*trace-output*)
437                (trace trace-this)
438                (assert (eq 'ok (trace-this)))
439                (untrace))))
440     (assert (search "TRACE-THIS" out))
441     (assert (search "returned OK" out))))
442
443 ;;; bug 379
444 ;;; This is not a WITH-TEST :FAILS-ON PPC DARWIN since there are
445 ;;; suspicions that the breakpoint trace might corrupt the whole image
446 ;;; on that platform.
447 (with-test (:name (trace :encapsulate nil)
448             :fails-on '(or (and :ppc (not :linux)) :sparc :mips)
449             :broken-on '(or :darwin :sunos))
450   (let ((out (with-output-to-string (*trace-output*)
451                (trace trace-this :encapsulate nil)
452                (assert (eq 'ok (trace-this)))
453                (untrace))))
454     (assert (search "TRACE-THIS" out))
455     (assert (search "returned OK" out))))
456
457 (with-test (:name (trace-recursive :encapsulate nil)
458             :fails-on '(or (and :ppc (not :linux)) :sparc :mips :sunos)
459             :broken-on '(or :darwin (and :x86 :sunos)))
460   (let ((out (with-output-to-string (*trace-output*)
461                (trace trace-fact :encapsulate nil)
462                (assert (= 120 (trace-fact 5)))
463                (untrace))))
464     (assert (search "TRACE-FACT" out))
465     (assert (search "returned 1" out))
466     (assert (search "returned 120" out))))
467
468 (defun trace-and-fmakunbound-this (x)
469   x)
470
471 (with-test (:name :bug-667657)
472   (trace trace-and-fmakunbound-this)
473   (fmakunbound 'trace-and-fmakunbound-this)
474   (untrace)
475   (assert (not (trace))))
476
477 (with-test (:name :bug-414)
478   (handler-bind ((warning #'error))
479     (load (compile-file "bug-414.lisp"))
480     (disassemble 'bug-414)))
481
482 (with-test (:name :bug-310175)
483   (let ((dx-arg (cons t t)))
484     (declare (dynamic-extent dx-arg))
485     (flet ((dx-arg-backtrace (x)
486              (declare (optimize (debug 2)))
487              (prog1 (sb-debug:backtrace-as-list 10)
488                (assert (sb-debug::stack-allocated-p x)))))
489       (declare (notinline dx-arg-backtrace))
490       (assert (member-if (lambda (frame)
491                            (and (consp frame)
492                                 (equal '(flet dx-arg-backtrace) (car frame))
493                                 (notany #'sb-debug::stack-allocated-p (cdr frame))))
494                          (dx-arg-backtrace dx-arg))))))
495
496 (with-test (:name :bug-795245)
497   (assert
498    (eq :ok
499        (catch 'done
500          (handler-bind
501              ((error (lambda (e)
502                        (declare (ignore e))
503                        (handler-case
504                            (sb-debug:backtrace 100 (make-broadcast-stream))
505                          (error ()
506                            (throw 'done :error))
507                          (:no-error ()
508                            (throw 'done :ok))))))
509            (apply '/= nil 1 2 nil))))))
510
511 ;;;; test infinite error protection
512
513 (defmacro nest-errors (n-levels error-form)
514   (if (< 0 n-levels)
515       `(handler-bind ((error (lambda (condition)
516                                (declare (ignore condition))
517                                ,error-form)))
518         (nest-errors ,(1- n-levels) ,error-form))
519       error-form))
520
521 (defun erroring-debugger-hook (condition old-debugger-hook)
522   (let ((*debugger-hook* old-debugger-hook))
523     (format t "recursive condition: ~A~%" condition) (force-output)
524     (error "recursive condition: ~A" condition)))
525
526 (defun test-inifinite-error-protection ()
527   ;; after 50 successful throws to SB-IMPL::TOPLEVEL-CATCHER sbcl used
528   ;; to halt, it produces so much garbage that's hard to suppress that
529   ;; it is tested only once
530   (write-line "--HARMLESS BUT ALARMING BACKTRACE COMING UP--")
531   (let ((*debugger-hook* #'erroring-debugger-hook))
532     (loop repeat 1 do
533           (let ((error-counter 0)
534                 (*terminal-io* (make-broadcast-stream)))
535             (assert
536              (not (eq
537                    :normal-exit
538                    (catch 'sb-impl::toplevel-catcher
539                      (nest-errors 20 (error "infinite error ~s"
540                                             (incf error-counter)))
541                      :normal-exit)))))))
542   (write-line "--END OF H-B-A-B--"))
543
544 (with-test (:name infinite-error-protection)
545   (enable-debugger)
546   (test-inifinite-error-protection))
547
548 (with-test (:name (infinite-error-protection :thread)
549                   :skipped-on '(not :sb-thread))
550   (enable-debugger)
551   (let ((thread (sb-thread:make-thread #'test-inifinite-error-protection)))
552     (loop while (sb-thread:thread-alive-p thread))))
553
554 ;; unconditional, in case either previous left it enabled
555 (disable-debugger)
556
557 (write-line "/debug.impure.lisp done")