home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / prim / debug.el < prev    next >
Encoding:
Text File  |  1995-08-04  |  17.4 KB  |  475 lines

  1. ;;; debug.el --- debuggers and related commands for XEmacs
  2.  
  3. ;; Copyright (C) 1985, 1986, 1993, 1994 Free Software Foundation, Inc.
  4.  
  5. ;; Maintainer: FSF
  6. ;; Keyword: lisp, tools
  7.  
  8. ;; This file is part of XEmacs.
  9.  
  10. ;; XEmacs is free software; you can redistribute it and/or modify it
  11. ;; under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; XEmacs is distributed in the hope that it will be useful, but
  16. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18. ;; General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  22. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24. ;;; Synched up with: FSF 19.28.
  25.  
  26. ;;; Code:
  27.  
  28. (defvar debug-function-list nil
  29.   "List of functions currently set for debug on entry.")
  30.  
  31. ;;#### This is terminally random an nigh-unmaintainable.
  32. ;;####  (need progv in elisp...)
  33. (defvar debugger-outer-match-data)
  34. ;; FSFmacs (defvar debugger-outer-track-mouse)
  35. (defvar debugger-outer-last-command)
  36. (defvar debugger-outer-this-command)
  37. (defvar debugger-outer-unread-command-event)
  38. (defvar debugger-outer-last-input-event)
  39. (defvar debugger-outer-last-input-char)
  40. (defvar debugger-outer-last-input-time)
  41. (defvar debugger-outer-last-command-event)
  42. (defvar debugger-outer-last-command-char)
  43. (defvar debugger-outer-standard-input)
  44. (defvar debugger-outer-standard-output)
  45. (defvar debugger-outer-cursor-in-echo-area)
  46.  
  47. ;;;don't ###autoload, loadup.el does something smarter.
  48. (setq debugger 'debug)
  49.  
  50. (defvar debugger-step-after-exit)
  51. (defvar debugger-old-buffer)
  52. (defvar debugger-value)
  53.  
  54. ;;;###autoload
  55. (defun debug (&rest debugger-args)
  56.   "Enter debugger.  To return, type \\<debugger-mode-map>`\\[debugger-continue]'.
  57. Arguments are mainly for use when this is called
  58.  from the internals of the evaluator.
  59. You may call with no args, or you may
  60.  pass nil as the first arg and any other args you like.
  61.  In that case, the list of args after the first will 
  62.  be printed into the backtrace buffer."
  63.   (message "Entering debugger...")
  64.   (let (debugger-value
  65.     (debug-on-error nil)
  66.     (debug-on-quit nil)
  67.     (debug-on-signal nil)
  68.     (debugger-buffer (let ((default-major-mode 'fundamental-mode))
  69.                (get-buffer-create "*Backtrace*")))
  70.     ;; #### I18N3 set the debugger-buffer to output-translating
  71.     (debugger-old-buffer (current-buffer))
  72.     (debugger-step-after-exit nil)
  73.         ;; Don't keep reading from an executing kbd macro!
  74.         (executing-macro nil)
  75.         ;; Save the outer values of these vars for the `e' command
  76.     ;; before we replace the values.
  77.     (debugger-outer-match-data (match-data))
  78.         (debugger-outer-last-command last-command)
  79.         (debugger-outer-this-command this-command)
  80.         (debugger-outer-unread-command-event unread-command-event)
  81.         (debugger-outer-last-input-event last-input-event)
  82.         (debugger-outer-last-input-char last-input-char)
  83.         (debugger-outer-last-input-time last-input-time)
  84.         (debugger-outer-last-command-event last-command-event)
  85.         (debugger-outer-last-command-char last-command-char)
  86.         (debugger-outer-standard-input standard-input)
  87.         (debugger-outer-standard-output standard-output)
  88.         (debugger-outer-cursor-in-echo-area cursor-in-echo-area)
  89.         )
  90.     ;; Don't let these magic variables affect the debugger itself.
  91.     (unwind-protect                     ;XEmacs change
  92.     (let ((last-command nil)
  93.           (this-command nil)
  94.           (unread-command-event nil)
  95.           (last-input-event (allocate-event))
  96.           (last-input-char -1)
  97.           (last-input-time nil)
  98.           (last-command-event (allocate-event))
  99.           (last-command-char -1)
  100.           (standard-input t)
  101.           (standard-output t)
  102.           (cursor-in-echo-area nil))
  103.       (save-excursion
  104.         (save-window-excursion
  105.           (pop-to-buffer debugger-buffer)
  106.           (erase-buffer)
  107.           (let ((standard-output (current-buffer))
  108.             (print-escape-newlines t)
  109.             (print-length 50))
  110.         (backtrace))
  111.           (goto-char (point-min))
  112.           (debugger-mode)
  113.           (delete-region (point)
  114.                  (progn
  115.                    (re-search-forward "\n[* ] debug(")
  116.                    (forward-line 1)
  117.                    (point)))
  118.           (debugger-reenable)
  119.           (cond ((memq (car debugger-args) '(lambda debug))
  120.              (insert "Entering:\n")
  121.              (if (eq (car debugger-args) 'debug)
  122.              (progn
  123.                (backtrace-debug 4 t)
  124.                (delete-char 1)
  125.                (insert ?*)
  126.                (beginning-of-line))))
  127.             ((eq (car debugger-args) 'exit)
  128.              (insert "Return value: ")
  129.              (setq debugger-value (nth 1 debugger-args))
  130.              (prin1 debugger-value (current-buffer))
  131.              (insert ?\n)
  132.              (delete-char 1)
  133.              (insert ? )
  134.              (beginning-of-line))
  135.             ((eq (car debugger-args) 'error)
  136.              (insert "Signalling: ")
  137.              (prin1 (nth 1 debugger-args) (current-buffer))
  138.              (insert ?\n))
  139.             ((eq (car debugger-args) t)
  140.              (insert
  141.               "Beginning evaluation of function call form:\n"))
  142.             (t
  143.              (prin1 (if (eq (car debugger-args) 'nil)
  144.                 (cdr debugger-args) debugger-args)
  145.                 (current-buffer))
  146.              (insert ?\n)))
  147.           (message "")
  148.           (let ((inhibit-trace t)
  149.             (standard-output nil)
  150.             (buffer-read-only t))
  151.         (message nil)
  152.         (recursive-edit)))
  153.         debugger-value))
  154.     ;; Kill or at least neuter the backtrace buffer, so that users
  155.     ;; don't try to execute debugger commands in an invalid context.
  156.       (if (get-buffer-window debugger-buffer 'visible)
  157.         ;; Still visible despite the save-window-excursion?  Maybe it
  158.         ;; it's in a pop-up frame.  It would be annoying to delete and
  159.         ;; recreate it every time the debugger stops, so instead we'll
  160.         ;; erase it but leave it visible.
  161.       (save-excursion
  162.         (set-buffer debugger-buffer)
  163.         (erase-buffer)
  164.         (fundamental-mode))
  165.       (kill-buffer debugger-buffer))
  166.       (store-match-data debugger-outer-match-data)
  167.       ;; Put into effect the modified values of these variables
  168.       ;; in case the user set them with the `e' command.
  169.       (setq last-command debugger-outer-last-command)
  170.       (setq this-command debugger-outer-this-command)
  171.       (setq unread-command-event debugger-outer-unread-command-event)
  172.       (setq last-input-event debugger-outer-last-input-event)
  173.       (setq last-input-char debugger-outer-last-input-char)
  174.       (setq last-input-time debugger-outer-last-input-time)
  175.       (setq last-command-event debugger-outer-last-command-event)
  176.       (setq last-command-char debugger-outer-last-command-char)
  177.       (setq standard-input debugger-outer-standard-input)
  178.       (setq standard-output debugger-outer-standard-output)
  179.       (setq cursor-in-echo-area debugger-outer-cursor-in-echo-area)
  180.       (setq debug-on-next-call debugger-step-after-exit) ;do this last!
  181.       )))
  182.  
  183.  
  184. (defun debugger-exit ()
  185.   (condition-case nil
  186.       (let ((debug-on-error nil)
  187.         (debug-on-signal nil))
  188.         ;; Tell signal to keep searching for handlers
  189.         (throw 'debugger t))
  190.     ;; Called from an old version of Emacs, perhaps?
  191.     (no-catch (exit-recursive-edit))))
  192.  
  193.  
  194. (defun debugger-step-through ()
  195.   "Proceed, stepping through subexpressions of this expression.
  196. Enter another debugger on next entry to eval, apply or funcall."
  197.   (interactive)
  198.   (setq debugger-step-after-exit t)
  199.   (message "Proceeding, will debug on next eval or call.")
  200.   (debugger-exit))
  201.  
  202. (defun debugger-continue ()
  203.   "Continue, evaluating this expression without stopping."
  204.   (interactive)
  205.   (message "Continuing.")
  206.   (debugger-exit))
  207.  
  208. (defun debugger-return-value (val)
  209.   "Continue, specifying value to return.
  210. This is only useful when the value returned from the debugger
  211. will be used, such as in a debug on exit from a frame."
  212.   (interactive "XReturn value (evaluated): ")
  213.   (setq debugger-value val)
  214.   (princ "Returning " t)
  215.   (prin1 debugger-value)
  216.   (exit-recursive-edit))
  217.  
  218. ;; Chosen empirically to account for all the frames
  219. ;; that will exist when debugger-frame is called
  220. ;; within the first one that appears in the backtrace buffer.
  221. ;; Assumes debugger-frame is called from a key;
  222. ;; will be wrong if it is called with Meta-x.
  223. (defconst debugger-frame-offset 8 "")
  224.  
  225. (defun debugger-jump ()
  226.   "Continue to exit from this frame, with all debug-on-entry suspended."
  227.   (interactive)
  228.   ;; Compensate for the two extra stack frames for debugger-jump.
  229.   (let ((debugger-frame-offset (+ debugger-frame-offset 2)))
  230.     (debugger-frame))
  231.   ;; Turn off all debug-on-entry functions
  232.   ;; but leave them in the list.
  233.   (let ((list debug-function-list))
  234.     (while list
  235.       (fset (car list)
  236.         (debug-on-entry-1 (car list) (symbol-function (car list)) nil))
  237.       (setq list (cdr list))))
  238.   (message "Continuing through this frame")
  239.   (debugger-exit))
  240.  
  241. (defun debugger-reenable ()
  242.   "Turn all debug-on-entry functions back on."
  243.   (let ((list debug-function-list))
  244.     (while list
  245.       (or (consp (symbol-function (car list)))
  246.       (debug-convert-byte-code (car list)))
  247.       (fset (car list)
  248.         (debug-on-entry-1 (car list) (symbol-function (car list)) t))
  249.       (setq list (cdr list)))))
  250.  
  251. (defun debugger-frame-number ()
  252.   "Return number of frames in backtrace before the one point points at."
  253.   (save-excursion
  254.     (beginning-of-line)
  255.     (let ((opoint (point))
  256.       (count 0))
  257.       (goto-char (point-min))
  258.       ;; #### I18N3 will not localize properly!
  259.       (if (or (equal (buffer-substring (point) (+ (point) 6))
  260.              (gettext "Signal"))
  261.           (equal (buffer-substring (point) (+ (point) 6))
  262.              (gettext "Return")))
  263.       (progn
  264.         (search-forward ":")
  265.         (forward-sexp 1)))
  266.       (forward-line 1)
  267.       (while (progn
  268.            (forward-char 2)
  269.            (if (= (following-char) ?\()
  270.            (forward-sexp 1)
  271.          (forward-sexp 2))
  272.            (forward-line 1)
  273.            (<= (point) opoint))
  274.     (setq count (1+ count)))
  275.       count)))
  276.  
  277. (defun debugger-frame ()
  278.   "Request entry to debugger when this frame exits.
  279. Applies to the frame whose line point is on in the backtrace."
  280.   (interactive)
  281.   (beginning-of-line)
  282.   (let ((level (debugger-frame-number)))
  283.     (backtrace-debug (+ level debugger-frame-offset) t))
  284.   (if (= (following-char) ? )
  285.       (let ((buffer-read-only nil))
  286.     (delete-char 1)
  287.     (insert ?*)))
  288.   (beginning-of-line))
  289.  
  290. (defun debugger-frame-clear ()
  291.   "Do not enter to debugger when this frame exits.
  292. Applies to the frame whose line point is on in the backtrace."
  293.   (interactive)
  294.   (beginning-of-line)
  295.   (let ((level (debugger-frame-number)))
  296.     (backtrace-debug (+ level debugger-frame-offset) nil))
  297.   (if (= (following-char) ?*)
  298.       (let ((buffer-read-only nil))
  299.     (delete-char 1)
  300.     (insert ? )))
  301.   (beginning-of-line))
  302.  
  303. (defun debugger-eval-expression (debugger-exp)
  304.   "Eval an expression, in an environment like that outside the debugger."
  305.   (interactive
  306.    (list (read-from-minibuffer "Eval: "
  307.                    nil read-expression-map t
  308.                    'read-expression-history)))
  309.   (save-excursion
  310.     (if (null (buffer-name debugger-old-buffer))
  311.     ;; old buffer deleted
  312.     (setq debugger-old-buffer (current-buffer)))
  313.     (set-buffer debugger-old-buffer)
  314.     (let ((last-command debugger-outer-last-command)
  315.           (this-command debugger-outer-this-command)
  316.           (unread-command-event debugger-outer-unread-command-event)
  317.           (last-input-event debugger-outer-last-input-event)
  318.           (last-input-char debugger-outer-last-input-char)
  319.           (last-input-time debugger-outer-last-input-time)
  320.           (last-command-event debugger-outer-last-command-event)
  321.           (last-command-char debugger-outer-last-command-char)
  322.           (standard-input debugger-outer-standard-input)
  323.           (standard-output debugger-outer-standard-output)
  324.           (cursor-in-echo-area debugger-outer-cursor-in-echo-area))
  325.       (store-match-data debugger-outer-match-data)
  326.       (prog1 (eval-expression debugger-exp)
  327.     (setq debugger-outer-match-data (match-data)
  328.           debugger-outer-last-command last-command
  329.               debugger-outer-this-command this-command
  330.               debugger-outer-unread-command-event unread-command-event
  331.               debugger-outer-last-input-event last-input-event
  332.               debugger-outer-last-input-char last-input-char
  333.               debugger-outer-last-input-time last-input-time
  334.               debugger-outer-last-command-event last-command-event
  335.               debugger-outer-last-command-char last-command-char
  336.               debugger-outer-standard-input standard-input
  337.               debugger-outer-standard-output standard-output
  338.               debugger-outer-cursor-in-echo-area cursor-in-echo-area)))))
  339.  
  340. (defvar debugger-mode-map
  341.   (let ((map (make-keymap)))
  342.     (set-keymap-name map 'debugger-mode-map)
  343.     (suppress-keymap map)
  344.     (define-key map "-" 'negative-argument)
  345.     (define-key map "b" 'debugger-frame)
  346.     (define-key map "c" 'debugger-continue)
  347.     (define-key map "j" 'debugger-jump)
  348.     (define-key map "r" 'debugger-return-value)
  349.     (define-key map "u" 'debugger-frame-clear)
  350.     (define-key map "d" 'debugger-step-through)
  351.     (define-key map "l" 'debugger-list-functions)
  352.     (define-key map "h" 'describe-mode)
  353.     (define-key map "q" 'top-level)
  354.     (define-key map "e" 'debugger-eval-expression)
  355.     (define-key map " " 'next-line)
  356.     map))
  357.  
  358. (put 'debugger-mode 'mode-class 'special)
  359.  
  360. (defun debugger-mode ()
  361.   "Mode for backtrace buffers, selected in debugger.
  362. \\<debugger-mode-map>
  363. A line starts with `*' if exiting that frame will call the debugger.
  364. Type \\[debugger-frame] or \\[debugger-frame-clear] to set or remove the `*'.
  365.  
  366. When in debugger due to frame being exited,
  367. use the \\[debugger-return-value] command to override the value
  368. being returned from that frame.
  369.  
  370. Use \\[debug-on-entry] and \\[cancel-debug-on-entry] to control
  371. which functions will enter the debugger when called.
  372.  
  373. Complete list of commands:
  374. \\{debugger-mode-map}"
  375.   (kill-all-local-variables)    
  376.   (setq major-mode 'debugger-mode)
  377.   (setq mode-name (gettext "Debugger"))
  378.   (setq truncate-lines t)
  379.   (set-syntax-table emacs-lisp-mode-syntax-table)
  380.   (use-local-map debugger-mode-map))
  381.  
  382. ;;;###autoload
  383. (defun debug-on-entry (function)
  384.   "Request FUNCTION to invoke debugger each time it is called.
  385. If you tell the debugger to continue, FUNCTION's execution proceeds.
  386. This works by modifying the definition of FUNCTION,
  387. which must be written in Lisp, not predefined.
  388. Use \\[cancel-debug-on-entry] to cancel the effect of this command.
  389. Redefining FUNCTION also cancels it."
  390.   (interactive "aDebug on entry (to function): ")
  391.   (debugger-reenable)
  392.   (if (subrp (symbol-function function))
  393.       (error "Function %s is a primitive" function))
  394.   (or (consp (symbol-function function))
  395.       (debug-convert-byte-code function))
  396.   (or (consp (symbol-function function))
  397.       (error "Definition of %s is not a list" function))
  398.   (fset function (debug-on-entry-1 function (symbol-function function) t))
  399.   (or (memq function debug-function-list)
  400.       (setq debug-function-list (cons function debug-function-list)))
  401.   function)
  402.  
  403. ;;;###autoload
  404. (defun cancel-debug-on-entry (&optional function)
  405.   "Undo effect of \\[debug-on-entry] on FUNCTION.
  406. If argument is nil or an empty string, cancel for all functions."
  407.   (interactive
  408.    (list (let ((name
  409.                 (completing-read "Cancel debug on entry (to function): "
  410.                                  ;; Make an "alist" of the functions
  411.                  ;; that now have debug on entry.
  412.                  (mapcar 'list (mapcar 'symbol-name
  413.                                                        debug-function-list))
  414.                  nil t nil)))
  415.        (if name (intern name)))))
  416.   (debugger-reenable)
  417.   (if (and function (not (string= function "")))
  418.       (progn
  419.     (fset function
  420.           (debug-on-entry-1 function (symbol-function function) nil))
  421.     (setq debug-function-list (delq function debug-function-list))
  422.     function)
  423.     (message "Cancelling debug-on-entry for all functions")
  424.     (mapcar 'cancel-debug-on-entry debug-function-list)))
  425.  
  426. (defun debug-convert-byte-code (function)
  427.   (let ((defn (symbol-function function)))
  428.     (if (not (consp defn))
  429.     ;; Assume a compiled code object.
  430.     (let* ((contents (append defn nil))
  431.            (body
  432.         (list (list 'byte-code (nth 1 contents)
  433.                 (nth 2 contents) (nth 3 contents)))))
  434.       (if (nthcdr 5 contents)
  435.           (setq body (cons (list 'interactive (nth 5 contents)) body)))
  436.       (if (nth 4 contents)
  437.           (setq body (cons (nth 4 contents) body)))
  438.       (fset function (cons 'lambda (cons (car contents) body)))))))
  439.  
  440. (defun debug-on-entry-1 (function defn flag)
  441.   (if (subrp defn)
  442.       (error "%s is a built-in function" function)
  443.     (if (eq (car defn) 'macro)
  444.     (debug-on-entry-1 function (cdr defn) flag)
  445.       (or (eq (car defn) 'lambda)
  446.       (error "%s not user-defined Lisp function" function))
  447.       (let (tail prec)
  448.     (if (stringp (car (nthcdr 2 defn)))
  449.         (setq tail (nthcdr 3 defn)
  450.           prec (list (car defn) (car (cdr defn)) (car (cdr (cdr defn)))))
  451.       (setq tail (nthcdr 2 defn)
  452.         prec (list (car defn) (car (cdr defn)))))
  453.     (if (eq flag (equal (car tail) '(debug 'debug)))
  454.         defn
  455.       (if flag
  456.           (nconc prec (cons '(debug 'debug) tail))
  457.         (nconc prec (cdr tail))))))))
  458.  
  459. (defun debugger-list-functions ()
  460.   "Display a list of all the functions now set to debug on entry."
  461.   (interactive)
  462.   (with-output-to-temp-buffer "*Help*"
  463.     (if (null debug-function-list)
  464.     (princ "No debug-on-entry functions now\n")
  465.       (princ "Functions set to debug on entry:\n\n")
  466.       (let ((list debug-function-list))
  467.     (while list
  468.       (prin1 (car list))
  469.       (terpri)
  470.       (setq list (cdr list))))
  471.       (princ "Note: if you have redefined a function, then it may no longer\n")
  472.       (princ "be set to debug on entry, even if it is in the list."))))
  473.  
  474. ;;; debug.el ends here
  475.