1 ;;; debug.el --- debuggers and related commands for Emacs
3 ;; Copyright (C) 1985-1986, 1994, 2001-2012 Free Software Foundation, Inc.
6 ;; Keywords: lisp, tools, maint
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; This is a major mode documented in the Emacs Lisp manual.
31 (defgroup debugger nil
32 "Debuggers and related commands for Emacs."
36 (defcustom debugger-mode-hook nil
37 "Hooks run when `debugger-mode' is turned on."
42 (defcustom debugger-batch-max-lines
40
43 "Maximum lines to show in debugger buffer in a noninteractive Emacs.
44 When the debugger is entered and Emacs is running in batch mode,
45 if the backtrace text has more than this many lines,
46 the middle is discarded, and just the beginning and end are displayed."
51 (defcustom debugger-bury-or-kill
'bury
52 "How to proceed with the debugger buffer when exiting `debug'.
53 The value used here affects the behavior of operations on any
54 window previously showing the debugger buffer.
56 `nil' means that if its window is not deleted when exiting the
57 debugger, invoking `switch-to-prev-buffer' will usually show
58 the debugger buffer again.
60 `append' means that if the window is not deleted, the debugger
61 buffer moves to the end of the window's previous buffers so
62 it's less likely that a future invocation of
63 `switch-to-prev-buffer' will switch to it. Also, it moves the
64 buffer to the end of the frame's buffer list.
66 `bury' means that if the window is not deleted, its buffer is
67 removed from the window's list of previous buffers. Also, it
68 moves the buffer to the end of the frame's buffer list. This
69 value provides the most reliable remedy to not have
70 `switch-to-prev-buffer' switch to the debugger buffer again
71 without killing the buffer.
73 `kill' means to kill the debugger buffer.
75 The value used here is passed to `quit-restore-window'."
77 (const :tag
"Keep alive" nil
)
78 (const :tag
"Append" append
)
79 (const :tag
"Bury" bury
)
80 (const :tag
"Kill" kill
))
84 (defvar debug-function-list nil
85 "List of functions currently set for debug on entry.")
87 (defvar debugger-step-after-exit nil
88 "Non-nil means \"single-step\" after the debugger exits.")
90 (defvar debugger-value nil
91 "This is the value for the debugger to return, when it returns.")
93 (defvar debugger-old-buffer nil
94 "This is the buffer that was current when the debugger was entered.")
96 (defvar debugger-previous-window nil
97 "This is the window last showing the debugger buffer.")
99 (defvar debugger-previous-window-height nil
100 "The last recorded height of `debugger-previous-window'.")
102 (defvar debugger-previous-backtrace nil
103 "The contents of the previous backtrace (including text properties).
104 This is to optimize `debugger-make-xrefs'.")
106 (defvar debugger-outer-match-data
)
107 (defvar debugger-outer-load-read-function
)
108 (defvar debugger-outer-overriding-local-map
)
109 (defvar debugger-outer-overriding-terminal-local-map
)
110 (defvar debugger-outer-track-mouse
)
111 (defvar debugger-outer-last-command
)
112 (defvar debugger-outer-this-command
)
113 (defvar debugger-outer-unread-command-events
)
114 (defvar debugger-outer-unread-post-input-method-events
)
115 (defvar debugger-outer-last-input-event
)
116 (defvar debugger-outer-last-command-event
)
117 (defvar debugger-outer-last-nonmenu-event
)
118 (defvar debugger-outer-last-event-frame
)
119 (defvar debugger-outer-standard-input
)
120 (defvar debugger-outer-standard-output
)
121 (defvar debugger-outer-inhibit-redisplay
)
122 (defvar debugger-outer-cursor-in-echo-area
)
123 (defvar debugger-will-be-back nil
124 "Non-nil if we expect to get back in the debugger soon.")
126 (defvar inhibit-debug-on-entry nil
127 "Non-nil means that debug-on-entry is disabled.")
129 (defvar debugger-jumping-flag nil
130 "Non-nil means that debug-on-entry is disabled.
131 This variable is used by `debugger-jump', `debugger-step-through',
132 and `debugger-reenable' to temporarily disable debug-on-entry.")
134 (defvar inhibit-trace
) ;Not yet implemented.
136 (defvar debugger-args nil
137 "Arguments with which the debugger was called.
138 It is a list expected to take the form (CAUSE . REST)
140 - debug: called for entry to a flagged function.
141 - t: called because of debug-on-next-call.
142 - lambda: same thing but via `funcall'.
143 - exit: called because of exit of a flagged function.
144 - error: called because of `debug-on-error'.")
147 (setq debugger
'debug
)
149 (defun debug (&rest debugger-args
)
150 "Enter debugger. \\<debugger-mode-map>`\\[debugger-continue]' returns from the debugger.
151 Arguments are mainly for use when this is called from the internals
154 You may call with no args, or you may pass nil as the first arg and
155 any other args you like. In that case, the list of args after the
156 first will be printed into the backtrace buffer."
158 (if inhibit-redisplay
159 ;; Don't really try to enter debugger within an eval from redisplay.
161 (unless noninteractive
162 (message "Entering debugger..."))
164 (debugger-previous-state
165 (if (get-buffer "*Backtrace*")
166 (with-current-buffer (get-buffer "*Backtrace*")
167 (list major-mode
(buffer-string)))))
168 (debugger-buffer (get-buffer-create "*Backtrace*"))
169 (debugger-old-buffer (current-buffer))
170 (debugger-window nil
)
171 (debugger-step-after-exit nil
)
172 (debugger-will-be-back nil
)
173 ;; Don't keep reading from an executing kbd macro!
174 (executing-kbd-macro nil
)
175 ;; Save the outer values of these vars for the `e' command
176 ;; before we replace the values.
177 (debugger-outer-match-data (match-data))
178 (debugger-outer-load-read-function load-read-function
)
179 (debugger-outer-overriding-local-map overriding-local-map
)
180 (debugger-outer-overriding-terminal-local-map
181 overriding-terminal-local-map
)
182 (debugger-outer-track-mouse track-mouse
)
183 (debugger-outer-last-command last-command
)
184 (debugger-outer-this-command this-command
)
185 (debugger-outer-unread-command-events unread-command-events
)
186 (debugger-outer-unread-post-input-method-events
187 unread-post-input-method-events
)
188 (debugger-outer-last-input-event last-input-event
)
189 (debugger-outer-last-command-event last-command-event
)
190 (debugger-outer-last-nonmenu-event last-nonmenu-event
)
191 (debugger-outer-last-event-frame last-event-frame
)
192 (debugger-outer-standard-input standard-input
)
193 (debugger-outer-standard-output standard-output
)
194 (debugger-outer-inhibit-redisplay inhibit-redisplay
)
195 (debugger-outer-cursor-in-echo-area cursor-in-echo-area
)
196 (debugger-with-timeout-suspend (with-timeout-suspend)))
197 ;; Set this instead of binding it, so that `q'
198 ;; will not restore it.
199 (setq overriding-terminal-local-map nil
)
200 ;; Don't let these magic variables affect the debugger itself.
201 (let ((last-command nil
) this-command track-mouse
203 (inhibit-debug-on-entry t
)
204 unread-command-events
205 unread-post-input-method-events
206 last-input-event last-command-event last-nonmenu-event
210 ;; If we are inside a minibuffer, allow nesting
211 ;; so that we don't get an error from the `e' command.
212 (enable-recursive-minibuffers
213 (or enable-recursive-minibuffers
(> (minibuffer-depth) 0)))
214 (standard-input t
) (standard-output t
)
216 (cursor-in-echo-area nil
))
219 (when (eq (car debugger-args
) 'debug
)
220 ;; Skip the frames for backtrace-debug, byte-code,
221 ;; and implement-debug-on-entry.
222 (backtrace-debug 4 t
)
223 ;; Place an extra debug-on-exit for macro's.
224 (when (eq 'lambda
(car-safe (cadr (backtrace-frame 4))))
225 (backtrace-debug 5 t
)))
228 `((display-buffer-reuse-window
229 display-buffer-in-previous-window
)
230 .
(,(when debugger-previous-window
231 `(previous-window .
,debugger-previous-window
)))))
232 (setq debugger-window
(selected-window))
233 (if (eq debugger-previous-window debugger-window
)
234 (when debugger-jumping-flag
235 ;; Try to restore previous height of debugger
240 (- debugger-previous-window-height
241 (window-total-size debugger-window
)))
243 (setq debugger-previous-window debugger-window
))
245 (debugger-setup-buffer debugger-args
)
247 ;; If the backtrace is long, save the beginning
248 ;; and the end, but discard the middle.
249 (when (> (count-lines (point-min) (point-max))
250 debugger-batch-max-lines
)
251 (goto-char (point-min))
252 (forward-line (/ 2 debugger-batch-max-lines
))
253 (let ((middlestart (point)))
254 (goto-char (point-max))
255 (forward-line (- (/ 2 debugger-batch-max-lines
)
256 debugger-batch-max-lines
))
257 (delete-region middlestart
(point)))
259 (goto-char (point-min))
260 (message "%s" (buffer-string))
263 (let ((standard-output nil
)
264 (buffer-read-only t
))
266 ;; Make sure we unbind buffer-read-only in the right buffer.
269 (when (and (not debugger-will-be-back
)
270 (window-live-p debugger-window
)
271 (eq (window-buffer debugger-window
) debugger-buffer
))
272 ;; Record height of debugger window.
273 (setq debugger-previous-window-height
274 (window-total-size debugger-window
))
275 ;; Unshow debugger-buffer.
276 (quit-restore-window debugger-window debugger-bury-or-kill
))
277 ;; Restore previous state of debugger-buffer in case we were
278 ;; in a recursive invocation of the debugger, otherwise just
279 ;; erase the buffer and put it into fundamental mode.
280 (when (buffer-live-p debugger-buffer
)
281 (with-current-buffer debugger-buffer
282 (let ((inhibit-read-only t
))
284 (if (null debugger-previous-state
)
286 (insert (nth 1 debugger-previous-state
))
287 (funcall (nth 0 debugger-previous-state
))))))
288 (with-timeout-unsuspend debugger-with-timeout-suspend
)
289 (set-match-data debugger-outer-match-data
)))
290 ;; Put into effect the modified values of these variables
291 ;; in case the user set them with the `e' command.
292 (setq load-read-function debugger-outer-load-read-function
)
293 (setq overriding-local-map debugger-outer-overriding-local-map
)
294 (setq overriding-terminal-local-map
295 debugger-outer-overriding-terminal-local-map
)
296 (setq track-mouse debugger-outer-track-mouse
)
297 (setq last-command debugger-outer-last-command
)
298 (setq this-command debugger-outer-this-command
)
299 (setq unread-command-events debugger-outer-unread-command-events
)
300 (setq unread-post-input-method-events
301 debugger-outer-unread-post-input-method-events
)
302 (setq last-input-event debugger-outer-last-input-event
)
303 (setq last-command-event debugger-outer-last-command-event
)
304 (setq last-nonmenu-event debugger-outer-last-nonmenu-event
)
305 (setq last-event-frame debugger-outer-last-event-frame
)
306 (setq standard-input debugger-outer-standard-input
)
307 (setq standard-output debugger-outer-standard-output
)
308 (setq inhibit-redisplay debugger-outer-inhibit-redisplay
)
309 (setq cursor-in-echo-area debugger-outer-cursor-in-echo-area
)
310 (setq debug-on-next-call debugger-step-after-exit
)
313 (defun debugger-setup-buffer (debugger-args)
314 "Initialize the `*Backtrace*' buffer for entry to the debugger.
315 That buffer should be current already."
316 (setq buffer-read-only nil
)
318 (set-buffer-multibyte t
) ;Why was it nil ? -stef
319 (setq buffer-undo-list t
)
320 (let ((standard-output (current-buffer))
321 (print-escape-newlines t
)
325 (goto-char (point-min))
326 (delete-region (point)
328 (search-forward "\n debug(")
329 (forward-line (if (eq (car debugger-args
) 'debug
)
330 2 ; Remove implement-debug-on-entry frame.
333 (insert "Debugger entered")
334 ;; lambda is for debug-on-call when a function call is next.
335 ;; debug is for debug-on-entry function called.
336 (pcase (car debugger-args
)
338 (insert "--entering a function:\n"))
339 ;; Exiting a function.
341 (insert "--returning value: ")
342 (setq debugger-value
(nth 1 debugger-args
))
343 (prin1 debugger-value
(current-buffer))
348 ;; Debugger entered for an error.
350 (insert "--Lisp error: ")
351 (prin1 (nth 1 debugger-args
) (current-buffer))
353 ;; debug-on-call, when the next thing is an eval.
355 (insert "--beginning evaluation of function call form:\n"))
356 ;; User calls debug directly.
359 (prin1 (if (eq (car debugger-args
) 'nil
)
360 (cdr debugger-args
) debugger-args
)
363 ;; After any frame that uses eval-buffer,
364 ;; insert a line that states the buffer position it's reading at.
366 (let ((tem eval-buffer-list
))
368 (re-search-forward "^ eval-\\(buffer\\|region\\)(" nil t
))
370 (insert (format " ; Reading at buffer position %d"
371 ;; This will get the wrong result
372 ;; if there are two nested eval-region calls
373 ;; for the same buffer. That's not a very useful case.
374 (with-current-buffer (car tem
)
377 (debugger-make-xrefs))
379 (defun debugger-make-xrefs (&optional buffer
)
380 "Attach cross-references to function names in the `*Backtrace*' buffer."
382 (with-current-buffer (or buffer
(current-buffer))
384 (setq buffer
(current-buffer))
385 (let ((inhibit-read-only t
)
386 (old-end (point-min)) (new-end (point-min)))
387 ;; If we saved an old backtrace, find the common part
388 ;; between the new and the old.
389 ;; Compare line by line, starting from the end,
390 ;; because that's the part that is likely to be unchanged.
391 (if debugger-previous-backtrace
392 (let (old-start new-start
(all-match t
))
393 (goto-char (point-max))
395 (insert debugger-previous-backtrace
)
396 (while (and all-match
(not (bobp)))
397 (setq old-end
(point))
399 (setq old-start
(point))
400 (with-current-buffer buffer
401 (setq new-end
(point))
403 (setq new-start
(point)))
405 (let ((case-fold-search nil
))
406 (compare-buffer-substrings
407 (current-buffer) old-start old-end
408 buffer new-start new-end
))))
409 (setq all-match nil
))))
410 ;; Now new-end is the position of the start of the
411 ;; unchanged part in the current buffer, and old-end is
412 ;; the position of that same text in the saved old
413 ;; backtrace. But we must subtract (point-min) since strings are
414 ;; indexed in origin 0.
416 ;; Replace the unchanged part of the backtrace
417 ;; with the text from debugger-previous-backtrace,
418 ;; since that already has the proper xrefs.
419 ;; With this optimization, we only need to scan
420 ;; the changed part of the backtrace.
421 (delete-region new-end
(point-max))
422 (goto-char (point-max))
423 (insert (substring debugger-previous-backtrace
424 (- old-end
(point-min))))
425 ;; Make the unchanged part of the backtrace inaccessible
426 ;; so it won't be scanned.
427 (narrow-to-region (point-min) new-end
)))
429 ;; Scan the new part of the backtrace, inserting xrefs.
430 (goto-char (point-min))
432 (goto-char (+ (point) 2))
433 (skip-syntax-forward "^w_")
436 (end (progn (skip-syntax-forward "w_") (point)))
437 (sym (intern-soft (buffer-substring-no-properties
439 (file (and sym
(symbol-file sym
'defun
))))
442 ;; help-xref-button needs to operate on something matched
443 ;; by a regexp, so set that up for it.
444 (re-search-forward "\\(\\sw\\|\\s_\\)+")
445 (help-xref-button 0 'help-function-def sym file
)))
448 (setq debugger-previous-backtrace
(buffer-string)))))
450 (defun debugger-step-through ()
451 "Proceed, stepping through subexpressions of this expression.
452 Enter another debugger on next entry to eval, apply or funcall."
454 (setq debugger-step-after-exit t
)
455 (setq debugger-jumping-flag t
)
456 (setq debugger-will-be-back t
)
457 (add-hook 'post-command-hook
'debugger-reenable
)
458 (message "Proceeding, will debug on next eval or call.")
459 (exit-recursive-edit))
461 (defun debugger-continue ()
462 "Continue, evaluating this expression without stopping."
464 (unless debugger-may-continue
465 (error "Cannot continue"))
466 (message "Continuing.")
468 ;; Check to see if we've flagged some frame for debug-on-exit, in which
469 ;; case we'll probably come back to the debugger soon.
470 (goto-char (point-min))
471 (if (re-search-forward "^\\* " nil t
)
472 (setq debugger-will-be-back t
)))
473 (exit-recursive-edit))
475 (defun debugger-return-value (val)
476 "Continue, specifying value to return.
477 This is only useful when the value returned from the debugger
478 will be used, such as in a debug on exit from a frame."
479 (interactive "XReturn value (evaluated): ")
480 (when (memq (car debugger-args
) '(t lambda error debug
))
481 (error "Cannot return a value %s"
482 (if (eq (car debugger-args
) 'error
)
483 "from an error" "at function entrance")))
484 (setq debugger-value val
)
485 (princ "Returning " t
)
486 (prin1 debugger-value
)
488 ;; Check to see if we've flagged some frame for debug-on-exit, in which
489 ;; case we'll probably come back to the debugger soon.
490 (goto-char (point-min))
491 (if (re-search-forward "^\\* " nil t
)
492 (setq debugger-will-be-back t
)))
493 (exit-recursive-edit))
495 (defun debugger-jump ()
496 "Continue to exit from this frame, with all debug-on-entry suspended."
499 (setq debugger-jumping-flag t
)
500 (add-hook 'post-command-hook
'debugger-reenable
)
501 (message "Continuing through this frame")
502 (setq debugger-will-be-back t
)
503 (exit-recursive-edit))
505 (defun debugger-reenable ()
506 "Turn all debug-on-entry functions back on.
507 This function is put on `post-command-hook' by `debugger-jump' and
508 removes itself from that hook."
509 (setq debugger-jumping-flag nil
)
510 (remove-hook 'post-command-hook
'debugger-reenable
))
512 (defun debugger-frame-number ()
513 "Return number of frames in backtrace before the one point points at."
516 (let ((opoint (point))
518 (while (not (eq (cadr (backtrace-frame count
)) 'debug
))
519 (setq count
(1+ count
)))
520 ;; Skip implement-debug-on-entry frame.
521 (when (eq 'implement-debug-on-entry
(cadr (backtrace-frame (1+ count
))))
522 (setq count
(1+ count
)))
523 (goto-char (point-min))
524 (when (looking-at "Debugger entered--\\(Lisp error\\|returning value\\):")
525 (goto-char (match-end 0))
530 (if (= (following-char) ?\
()
535 (if (looking-at " *;;;")
537 (setq count
(1+ count
)))
540 (defun debugger-frame ()
541 "Request entry to debugger when this frame exits.
542 Applies to the frame whose line point is on in the backtrace."
546 (if (looking-at " *;;;\\|[a-z]")
547 (error "This line is not a function call")))
549 (backtrace-debug (debugger-frame-number) t
)
550 (if (= (following-char) ?
)
551 (let ((inhibit-read-only t
))
556 (defun debugger-frame-clear ()
557 "Do not enter debugger when this frame exits.
558 Applies to the frame whose line point is on in the backtrace."
562 (if (looking-at " *;;;\\|[a-z]")
563 (error "This line is not a function call")))
565 (backtrace-debug (debugger-frame-number) nil
)
566 (if (= (following-char) ?
*)
567 (let ((inhibit-read-only t
))
572 (defmacro debugger-env-macro
(&rest body
)
573 "Run BODY in original environment."
576 (if (null (buffer-name debugger-old-buffer
))
577 ;; old buffer deleted
578 (setq debugger-old-buffer
(current-buffer)))
579 (set-buffer debugger-old-buffer
)
580 (let ((load-read-function debugger-outer-load-read-function
)
581 (overriding-terminal-local-map
582 debugger-outer-overriding-terminal-local-map
)
583 (overriding-local-map debugger-outer-overriding-local-map
)
584 (track-mouse debugger-outer-track-mouse
)
585 (last-command debugger-outer-last-command
)
586 (this-command debugger-outer-this-command
)
587 (unread-command-events debugger-outer-unread-command-events
)
588 (unread-post-input-method-events
589 debugger-outer-unread-post-input-method-events
)
590 (last-input-event debugger-outer-last-input-event
)
591 (last-command-event debugger-outer-last-command-event
)
592 (last-nonmenu-event debugger-outer-last-nonmenu-event
)
593 (last-event-frame debugger-outer-last-event-frame
)
594 (standard-input debugger-outer-standard-input
)
595 (standard-output debugger-outer-standard-output
)
596 (inhibit-redisplay debugger-outer-inhibit-redisplay
)
597 (cursor-in-echo-area debugger-outer-cursor-in-echo-area
))
598 (set-match-data debugger-outer-match-data
)
601 (setq debugger-outer-match-data
(match-data))
602 (setq debugger-outer-load-read-function load-read-function
)
603 (setq debugger-outer-overriding-terminal-local-map
604 overriding-terminal-local-map
)
605 (setq debugger-outer-overriding-local-map overriding-local-map
)
606 (setq debugger-outer-track-mouse track-mouse
)
607 (setq debugger-outer-last-command last-command
)
608 (setq debugger-outer-this-command this-command
)
609 (setq debugger-outer-unread-command-events unread-command-events
)
610 (setq debugger-outer-unread-post-input-method-events
611 unread-post-input-method-events
)
612 (setq debugger-outer-last-input-event last-input-event
)
613 (setq debugger-outer-last-command-event last-command-event
)
614 (setq debugger-outer-last-nonmenu-event last-nonmenu-event
)
615 (setq debugger-outer-last-event-frame last-event-frame
)
616 (setq debugger-outer-standard-input standard-input
)
617 (setq debugger-outer-standard-output standard-output
)
618 (setq debugger-outer-inhibit-redisplay inhibit-redisplay
)
619 (setq debugger-outer-cursor-in-echo-area cursor-in-echo-area
)
622 (defun debugger-eval-expression (exp)
623 "Eval an expression, in an environment like that outside the debugger."
625 (list (read-from-minibuffer "Eval: "
626 nil read-expression-map t
627 'read-expression-history
)))
628 (debugger-env-macro (eval-expression exp
)))
630 (defvar debugger-mode-map
631 (let ((map (make-keymap))
632 (menu-map (make-sparse-keymap)))
633 (set-keymap-parent map button-buffer-map
)
634 (suppress-keymap map
)
635 (define-key map
"-" 'negative-argument
)
636 (define-key map
"b" 'debugger-frame
)
637 (define-key map
"c" 'debugger-continue
)
638 (define-key map
"j" 'debugger-jump
)
639 (define-key map
"r" 'debugger-return-value
)
640 (define-key map
"u" 'debugger-frame-clear
)
641 (define-key map
"d" 'debugger-step-through
)
642 (define-key map
"l" 'debugger-list-functions
)
643 (define-key map
"h" 'describe-mode
)
644 (define-key map
"q" 'top-level
)
645 (define-key map
"e" 'debugger-eval-expression
)
646 (define-key map
" " 'next-line
)
647 (define-key map
"R" 'debugger-record-expression
)
648 (define-key map
"\C-m" 'debug-help-follow
)
649 (define-key map
[mouse-2
] 'push-button
)
650 (define-key map
[menu-bar debugger
] (cons "Debugger" menu-map
))
651 (define-key menu-map
[deb-top
]
652 '(menu-item "Quit" top-level
653 :help
"Quit debugging and return to top level"))
654 (define-key menu-map
[deb-s0
] '("--"))
655 (define-key menu-map
[deb-descr
]
656 '(menu-item "Describe Debugger Mode" describe-mode
657 :help
"Display documentation for debugger-mode"))
658 (define-key menu-map
[deb-hfol
]
659 '(menu-item "Help Follow" debug-help-follow
660 :help
"Follow cross-reference"))
661 (define-key menu-map
[deb-nxt
]
662 '(menu-item "Next Line" next-line
663 :help
"Move cursor down"))
664 (define-key menu-map
[deb-s1
] '("--"))
665 (define-key menu-map
[deb-lfunc
]
666 '(menu-item "List debug on entry functions" debugger-list-functions
667 :help
"Display a list of all the functions now set to debug on entry"))
668 (define-key menu-map
[deb-fclear
]
669 '(menu-item "Cancel debug frame" debugger-frame-clear
670 :help
"Do not enter debugger when this frame exits"))
671 (define-key menu-map
[deb-frame
]
672 '(menu-item "Debug frame" debugger-frame
673 :help
"Request entry to debugger when this frame exits"))
674 (define-key menu-map
[deb-s2
] '("--"))
675 (define-key menu-map
[deb-ret
]
676 '(menu-item "Return value..." debugger-return-value
677 :help
"Continue, specifying value to return."))
678 (define-key menu-map
[deb-rec
]
679 '(menu-item "Display and Record Expression" debugger-record-expression
680 :help
"Display a variable's value and record it in `*Backtrace-record*' buffer"))
681 (define-key menu-map
[deb-eval
]
682 '(menu-item "Eval Expression..." debugger-eval-expression
683 :help
"Eval an expression, in an environment like that outside the debugger"))
684 (define-key menu-map
[deb-jump
]
685 '(menu-item "Jump" debugger-jump
686 :help
"Continue to exit from this frame, with all debug-on-entry suspended"))
687 (define-key menu-map
[deb-cont
]
688 '(menu-item "Continue" debugger-continue
689 :help
"Continue, evaluating this expression without stopping"))
690 (define-key menu-map
[deb-step
]
691 '(menu-item "Step through" debugger-step-through
692 :help
"Proceed, stepping through subexpressions of this expression"))
695 (put 'debugger-mode
'mode-class
'special
)
697 (defun debugger-mode ()
698 "Mode for backtrace buffers, selected in debugger.
699 \\<debugger-mode-map>
700 A line starts with `*' if exiting that frame will call the debugger.
701 Type \\[debugger-frame] or \\[debugger-frame-clear] to set or remove the `*'.
703 When in debugger due to frame being exited,
704 use the \\[debugger-return-value] command to override the value
705 being returned from that frame.
707 Use \\[debug-on-entry] and \\[cancel-debug-on-entry] to control
708 which functions will enter the debugger when called.
710 Complete list of commands:
711 \\{debugger-mode-map}"
712 (kill-all-local-variables)
713 (setq major-mode
'debugger-mode
)
714 (setq mode-name
"Debugger")
715 (setq truncate-lines t
)
716 (set-syntax-table emacs-lisp-mode-syntax-table
)
717 (use-local-map debugger-mode-map
)
718 (run-mode-hooks 'debugger-mode-hook
))
720 (defcustom debugger-record-buffer
"*Debugger-record*"
721 "Buffer name for expression values, for \\[debugger-record-expression]."
726 (defun debugger-record-expression (exp)
727 "Display a variable's value and record it in `*Backtrace-record*' buffer."
729 (list (read-from-minibuffer
732 read-expression-map t
733 'read-expression-history
)))
734 (let* ((buffer (get-buffer-create debugger-record-buffer
))
735 (standard-output buffer
))
736 (princ (format "Debugger Eval (%s): " exp
))
737 (princ (debugger-eval-expression exp
))
740 (with-current-buffer (get-buffer debugger-record-buffer
)
742 (buffer-substring (line-beginning-position 0)
743 (line-end-position 0)))))
745 (declare-function help-xref-interned
"help-mode" (symbol))
747 (defun debug-help-follow (&optional pos
)
748 "Follow cross-reference at POS, defaulting to point.
750 For the cross-reference format, see `help-make-xrefs'."
753 ;; Ideally we'd just do (call-interactively 'help-follow) except that this
754 ;; assumes we're already in a *Help* buffer and reuses it, so it ends up
755 ;; incorrectly "reusing" the *Backtrace* buffer to show the help info.
758 (unless (push-button pos
)
759 ;; check if the symbol under point is a function or variable
763 (goto-char pos
) (skip-syntax-backward "w_")
764 (buffer-substring (point)
765 (progn (skip-syntax-forward "w_")
767 (when (or (boundp sym
) (fboundp sym
) (facep sym
))
768 (help-xref-interned sym
)))))
770 ;; When you change this, you may also need to change the number of
771 ;; frames that the debugger skips.
772 (defun implement-debug-on-entry ()
773 "Conditionally call the debugger.
774 A call to this function is inserted by `debug-on-entry' to cause
775 functions to break on entry."
776 (if (or inhibit-debug-on-entry debugger-jumping-flag
)
778 (funcall debugger
'debug
)))
780 (defun debugger-special-form-p (symbol)
781 "Return whether SYMBOL is a special form."
782 (and (fboundp symbol
)
783 (subrp (symbol-function symbol
))
784 (eq (cdr (subr-arity (symbol-function symbol
))) 'unevalled
)))
787 (defun debug-on-entry (function)
788 "Request FUNCTION to invoke debugger each time it is called.
790 When called interactively, prompt for FUNCTION in the minibuffer.
792 This works by modifying the definition of FUNCTION. If you tell the
793 debugger to continue, FUNCTION's execution proceeds. If FUNCTION is a
794 normal function or a macro written in Lisp, you can also step through
795 its execution. FUNCTION can also be a primitive that is not a special
796 form, in which case stepping is not possible. Break-on-entry for
797 primitive functions only works when that function is called from Lisp.
799 Use \\[cancel-debug-on-entry] to cancel the effect of this command.
800 Redefining FUNCTION also cancels it."
802 (let ((fn (function-called-at-point)) val
)
803 (when (debugger-special-form-p fn
)
805 (setq val
(completing-read
807 (format "Debug on entry to function (default %s): " fn
)
808 "Debug on entry to function: ")
811 (and (fboundp symbol
)
812 (not (debugger-special-form-p symbol
))))
813 t nil nil
(symbol-name fn
)))
814 (list (if (equal val
"") fn
(intern val
)))))
815 ;; FIXME: Use advice.el.
816 (when (debugger-special-form-p function
)
817 (error "Function %s is a special form" function
))
818 (if (or (symbolp (symbol-function function
))
819 (subrp (symbol-function function
)))
820 ;; The function is built-in or aliased to another function.
821 ;; Create a wrapper in which we can add the debug call.
822 (fset function
`(lambda (&rest debug-on-entry-args
)
823 ,(interactive-form (symbol-function function
))
824 (apply ',(symbol-function function
)
825 debug-on-entry-args
)))
826 (when (autoloadp (symbol-function function
))
827 ;; The function is autoloaded. Load its real definition.
828 (autoload-do-load (symbol-function function
) function
))
829 (when (or (not (consp (symbol-function function
)))
830 (and (eq (car (symbol-function function
)) 'macro
)
831 (not (consp (cdr (symbol-function function
))))))
832 ;; The function is byte-compiled. Create a wrapper in which
833 ;; we can add the debug call.
834 (debug-convert-byte-code function
)))
835 (unless (consp (symbol-function function
))
836 (error "Definition of %s is not a list" function
))
837 (fset function
(debug-on-entry-1 function t
))
838 (unless (memq function debug-function-list
)
839 (push function debug-function-list
))
843 (defun cancel-debug-on-entry (&optional function
)
844 "Undo effect of \\[debug-on-entry] on FUNCTION.
845 If FUNCTION is nil, cancel debug-on-entry for all functions.
846 When called interactively, prompt for FUNCTION in the minibuffer.
847 To specify a nil argument interactively, exit with an empty minibuffer."
851 "Cancel debug on entry to function (default all functions): "
852 (mapcar 'symbol-name debug-function-list
) nil t
)))
854 (unless (string= name
"")
857 (not (string= function
""))) ; Pre 22.1 compatibility test.
859 (let ((defn (debug-on-entry-1 function nil
)))
861 (when (and (equal (nth 1 defn
) '(&rest debug-on-entry-args
))
862 (eq (car (nth 3 defn
)) 'apply
))
863 ;; `defn' is a wrapper introduced in debug-on-entry.
864 ;; Get rid of it since we don't need it any more.
865 (setq defn
(nth 1 (nth 1 (nth 3 defn
)))))
867 (fset function defn
))
868 (setq debug-function-list
(delq function debug-function-list
))
870 (message "Cancelling debug-on-entry for all functions")
871 (mapcar 'cancel-debug-on-entry debug-function-list
)))
873 (defun debug-arglist (definition)
874 ;; FIXME: copied from ad-arglist.
875 "Return the argument list of DEFINITION."
877 (help-function-arglist definition
'preserve-names
))
879 (defun debug-convert-byte-code (function)
880 (let* ((defn (symbol-function function
))
881 (macro (eq (car-safe defn
) 'macro
)))
882 (when macro
(setq defn
(cdr defn
)))
883 (when (byte-code-function-p defn
)
884 (let* ((args (debug-arglist defn
))
886 `((,(if (memq '&rest args
) #'apply
#'funcall
)
888 ,@(remq '&rest
(remq '&optional args
))))))
889 (if (> (length defn
) 5)
890 ;; The mere presence of field 5 is sufficient to make
892 (push `(interactive ,(aref defn
5)) body
))
893 (if (and (> (length defn
) 4) (aref defn
4))
894 ;; Use `documentation' here, to get the actual string,
895 ;; in case the compiled function has a reference
897 (setq body
(cons (documentation function
) body
)))
898 (setq defn
`(closure (t) ,args
,@body
)))
899 (when macro
(setq defn
(cons 'macro defn
)))
900 (fset function defn
))))
902 (defun debug-on-entry-1 (function flag
)
903 (let* ((defn (symbol-function function
))
905 (when (eq (car-safe tail
) 'macro
)
906 (setq tail
(cdr tail
)))
907 (if (not (memq (car-safe tail
) '(closure lambda
)))
908 ;; Only signal an error when we try to set debug-on-entry.
909 ;; When we try to clear debug-on-entry, we are now done.
911 (error "%s is not a user-defined Lisp function" function
))
912 (if (eq (car tail
) 'closure
) (setq tail
(cdr tail
)))
913 (setq tail
(cdr tail
))
914 ;; Skip the docstring.
915 (when (and (stringp (cadr tail
)) (cddr tail
))
916 (setq tail
(cdr tail
)))
917 ;; Skip the interactive form.
918 (when (eq 'interactive
(car-safe (cadr tail
)))
919 (setq tail
(cdr tail
)))
920 (unless (eq flag
(equal (cadr tail
) '(implement-debug-on-entry)))
921 ;; Add/remove debug statement as needed.
922 (setcdr tail
(if flag
923 (cons '(implement-debug-on-entry) (cdr tail
))
927 (defun debugger-list-functions ()
928 "Display a list of all the functions now set to debug on entry."
931 (help-setup-xref '(debugger-list-functions)
932 (called-interactively-p 'interactive
))
933 (with-output-to-temp-buffer (help-buffer)
934 (with-current-buffer standard-output
935 (if (null debug-function-list
)
936 (princ "No debug-on-entry functions now\n")
937 (princ "Functions set to debug on entry:\n\n")
938 (dolist (fun debug-function-list
)
939 (make-text-button (point) (progn (prin1 fun
) (point))
941 'help-args
(list fun
))
944 (princ "Note: if you have redefined a function, then it may no longer\n")
945 (princ "be set to debug on entry, even if it is in the list.")))))
949 ;;; debug.el ends here