1 ;;; debug.el --- debuggers and related commands for Emacs -*- lexical-binding: t -*-
3 ;; Copyright (C) 1985-1986, 1994, 2001-2016 Free Software Foundation,
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: lisp, tools, maint
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; This is a major mode documented in the Emacs Lisp manual.
32 (defgroup debugger nil
33 "Debuggers and related commands for Emacs."
37 (defcustom debugger-mode-hook nil
38 "Hooks run when `debugger-mode' is turned on."
43 (defcustom debugger-batch-max-lines
40
44 "Maximum lines to show in debugger buffer in a noninteractive Emacs.
45 When the debugger is entered and Emacs is running in batch mode,
46 if the backtrace text has more than this many lines,
47 the middle is discarded, and just the beginning and end are displayed."
52 (defcustom debugger-bury-or-kill
'bury
53 "What to do with the debugger buffer when exiting `debug'.
54 The value affects the behavior of operations on any window
55 previously showing the debugger buffer.
57 nil means that if its window is not deleted when exiting the
58 debugger, invoking `switch-to-prev-buffer' will usually show
59 the debugger buffer again.
61 `append' means that if the window is not deleted, the debugger
62 buffer moves to the end of the window's previous buffers so
63 it's less likely that a future invocation of
64 `switch-to-prev-buffer' will switch to it. Also, it moves the
65 buffer to the end of the frame's buffer list.
67 `bury' means that if the window is not deleted, its buffer is
68 removed from the window's list of previous buffers. Also, it
69 moves the buffer to the end of the frame's buffer list. This
70 value provides the most reliable remedy to not have
71 `switch-to-prev-buffer' switch to the debugger buffer again
72 without killing the buffer.
74 `kill' means to kill the debugger buffer.
76 The value used here is passed to `quit-restore-window'."
78 (const :tag
"Keep alive" nil
)
79 (const :tag
"Append" append
)
80 (const :tag
"Bury" bury
)
81 (const :tag
"Kill" kill
))
85 (defvar debugger-step-after-exit nil
86 "Non-nil means \"single-step\" after the debugger exits.")
88 (defvar debugger-value nil
89 "This is the value for the debugger to return, when it returns.")
91 (defvar debugger-old-buffer nil
92 "This is the buffer that was current when the debugger was entered.")
94 (defvar debugger-previous-window nil
95 "This is the window last showing the debugger buffer.")
97 (defvar debugger-previous-window-height nil
98 "The last recorded height of `debugger-previous-window'.")
100 (defvar debugger-previous-backtrace nil
101 "The contents of the previous backtrace (including text properties).
102 This is to optimize `debugger-make-xrefs'.")
104 (defvar debugger-outer-match-data
)
105 (defvar debugger-will-be-back nil
106 "Non-nil if we expect to get back in the debugger soon.")
108 (defvar inhibit-debug-on-entry nil
109 "Non-nil means that `debug-on-entry' is disabled.")
111 (defvar debugger-jumping-flag nil
112 "Non-nil means that `debug-on-entry' is disabled.
113 This variable is used by `debugger-jump', `debugger-step-through',
114 and `debugger-reenable' to temporarily disable debug-on-entry.")
116 (defvar inhibit-trace
) ;Not yet implemented.
118 (defvar debugger-args nil
119 "Arguments with which the debugger was called.
120 It is a list expected to take the form (CAUSE . REST)
122 - debug: called for entry to a flagged function.
123 - t: called because of debug-on-next-call.
124 - lambda: same thing but via `funcall'.
125 - exit: called because of exit of a flagged function.
126 - error: called because of `debug-on-error'.")
129 (setq debugger
'debug
)
131 (defun debug (&rest args
)
132 "Enter debugger. \\<debugger-mode-map>`\\[debugger-continue]' returns from the debugger.
133 Arguments are mainly for use when this is called from the internals
136 You may call with no args, or you may pass nil as the first arg and
137 any other args you like. In that case, the list of args after the
138 first will be printed into the backtrace buffer."
140 (if inhibit-redisplay
141 ;; Don't really try to enter debugger within an eval from redisplay.
143 (unless noninteractive
144 (message "Entering debugger..."))
146 (debugger-previous-state
147 (if (get-buffer "*Backtrace*")
148 (with-current-buffer (get-buffer "*Backtrace*")
149 (list major-mode
(buffer-string)))))
151 (debugger-buffer (get-buffer-create "*Backtrace*"))
152 (debugger-old-buffer (current-buffer))
153 (debugger-window nil
)
154 (debugger-step-after-exit nil
)
155 (debugger-will-be-back nil
)
156 ;; Don't keep reading from an executing kbd macro!
157 (executing-kbd-macro nil
)
158 ;; Save the outer values of these vars for the `e' command
159 ;; before we replace the values.
160 (debugger-outer-match-data (match-data))
161 (debugger-with-timeout-suspend (with-timeout-suspend)))
162 ;; Set this instead of binding it, so that `q'
163 ;; will not restore it.
164 (setq overriding-terminal-local-map nil
)
165 ;; Don't let these magic variables affect the debugger itself.
166 (let ((last-command nil
) this-command track-mouse
168 unread-command-events
169 unread-post-input-method-events
170 last-input-event last-command-event last-nonmenu-event
174 ;; If we are inside a minibuffer, allow nesting
175 ;; so that we don't get an error from the `e' command.
176 (enable-recursive-minibuffers
177 (or enable-recursive-minibuffers
(> (minibuffer-depth) 0)))
178 (standard-input t
) (standard-output t
)
180 (cursor-in-echo-area nil
)
181 (window-configuration (current-window-configuration)))
184 (when (eq (car debugger-args
) 'debug
)
185 ;; Skip the frames for backtrace-debug, byte-code,
186 ;; debug--implement-debug-on-entry and the advice's `apply'.
187 (backtrace-debug 4 t
)
188 ;; Place an extra debug-on-exit for macro's.
189 (when (eq 'lambda
(car-safe (cadr (backtrace-frame 4))))
190 (backtrace-debug 5 t
)))
193 `((display-buffer-reuse-window
194 display-buffer-in-previous-window
)
195 .
(,(when (and (window-live-p debugger-previous-window
)
197 (window-frame debugger-previous-window
)))
198 `(previous-window .
,debugger-previous-window
)))))
199 (setq debugger-window
(selected-window))
200 (if (eq debugger-previous-window debugger-window
)
201 (when debugger-jumping-flag
202 ;; Try to restore previous height of debugger
207 (- debugger-previous-window-height
208 (window-total-height debugger-window
)))
210 (setq debugger-previous-window debugger-window
))
212 (debugger-setup-buffer debugger-args
)
214 ;; If the backtrace is long, save the beginning
215 ;; and the end, but discard the middle.
216 (when (> (count-lines (point-min) (point-max))
217 debugger-batch-max-lines
)
218 (goto-char (point-min))
219 (forward-line (/ 2 debugger-batch-max-lines
))
220 (let ((middlestart (point)))
221 (goto-char (point-max))
222 (forward-line (- (/ 2 debugger-batch-max-lines
)
223 debugger-batch-max-lines
))
224 (delete-region middlestart
(point)))
226 (goto-char (point-min))
227 (message "%s" (buffer-string))
230 (let ((standard-output nil
)
231 (buffer-read-only t
))
233 ;; Make sure we unbind buffer-read-only in the right buffer.
236 (when (and (window-live-p debugger-window
)
237 (eq (window-buffer debugger-window
) debugger-buffer
))
238 ;; Record height of debugger window.
239 (setq debugger-previous-window-height
240 (window-total-height debugger-window
)))
241 (if debugger-will-be-back
242 ;; Restore previous window configuration (Bug#12623).
243 (set-window-configuration window-configuration
)
244 (when (and (window-live-p debugger-window
)
245 (eq (window-buffer debugger-window
) debugger-buffer
))
247 ;; Unshow debugger-buffer.
248 (quit-restore-window debugger-window debugger-bury-or-kill
)
249 ;; Restore current buffer (Bug#12502).
250 (set-buffer debugger-old-buffer
))))
251 ;; Restore previous state of debugger-buffer in case we were
252 ;; in a recursive invocation of the debugger, otherwise just
253 ;; erase the buffer and put it into fundamental mode.
254 (when (buffer-live-p debugger-buffer
)
255 (with-current-buffer debugger-buffer
256 (let ((inhibit-read-only t
))
258 (if (null debugger-previous-state
)
260 (insert (nth 1 debugger-previous-state
))
261 (funcall (nth 0 debugger-previous-state
))))))
262 (with-timeout-unsuspend debugger-with-timeout-suspend
)
263 (set-match-data debugger-outer-match-data
)))
264 (setq debug-on-next-call debugger-step-after-exit
)
267 (defun debugger-setup-buffer (args)
268 "Initialize the `*Backtrace*' buffer for entry to the debugger.
269 That buffer should be current already."
270 (setq buffer-read-only nil
)
272 (set-buffer-multibyte t
) ;Why was it nil ? -stef
273 (setq buffer-undo-list t
)
274 (let ((standard-output (current-buffer))
275 (print-escape-newlines t
)
279 (goto-char (point-min))
280 (delete-region (point)
282 (search-forward (if debugger-stack-frame-as-list
285 (forward-line (if (eq (car args
) 'debug
)
286 ;; Remove debug--implement-debug-on-entry
287 ;; and the advice's `apply' frame.
291 (insert "Debugger entered")
292 ;; lambda is for debug-on-call when a function call is next.
293 ;; debug is for debug-on-entry function called.
297 (insert "--entering a function:\n")
298 (setq pos
(1- (point))))
299 ;; Exiting a function.
301 (insert "--returning value: ")
303 (setq debugger-value
(nth 1 args
))
304 (prin1 debugger-value
(current-buffer))
309 ;; Watchpoint triggered.
310 ((and `watchpoint
(let `(,symbol
,newval .
,details
) (cdr args
)))
314 (`(makunbound nil
) (format "making %s void" symbol
))
315 (`(makunbound ,buffer
) (format "killing local value of %s in buffer %s"
317 (`(defvaralias ,_
) (format "aliasing %s to %s" symbol newval
))
318 (`(let ,_
) (format "let-binding %s to %S" symbol newval
))
319 (`(unlet ,_
) (format "ending let-binding of %s" symbol
))
320 (`(set nil
) (format "setting %s to %S" symbol newval
))
321 (`(set ,buffer
) (format "setting %s in buffer %s to %S"
322 symbol buffer newval
))
323 (_ (error "unrecognized watchpoint triggered %S" (cdr args
))))
327 ;; Debugger entered for an error.
329 (insert "--Lisp error: ")
331 (prin1 (nth 1 args
) (current-buffer))
333 ;; debug-on-call, when the next thing is an eval.
335 (insert "--beginning evaluation of function call form:\n")
336 (setq pos
(1- (point))))
337 ;; User calls debug directly.
341 (prin1 (if (eq (car args
) 'nil
)
345 ;; Place point on "stack frame 0" (bug#15101).
347 ;; After any frame that uses eval-buffer,
348 ;; insert a line that states the buffer position it's reading at.
350 (let ((tem eval-buffer-list
))
352 (re-search-forward "^ eval-\\(buffer\\|region\\)(" nil t
))
354 (insert (format " ; Reading at buffer position %d"
355 ;; This will get the wrong result
356 ;; if there are two nested eval-region calls
357 ;; for the same buffer. That's not a very useful case.
358 (with-current-buffer (car tem
)
361 (debugger-make-xrefs))
363 (defun debugger-make-xrefs (&optional buffer
)
364 "Attach cross-references to function names in the `*Backtrace*' buffer."
366 (with-current-buffer (or buffer
(current-buffer))
368 (setq buffer
(current-buffer))
369 (let ((inhibit-read-only t
)
370 (old-end (point-min)) (new-end (point-min)))
371 ;; If we saved an old backtrace, find the common part
372 ;; between the new and the old.
373 ;; Compare line by line, starting from the end,
374 ;; because that's the part that is likely to be unchanged.
375 (if debugger-previous-backtrace
376 (let (old-start new-start
(all-match t
))
377 (goto-char (point-max))
379 (insert debugger-previous-backtrace
)
380 (while (and all-match
(not (bobp)))
381 (setq old-end
(point))
383 (setq old-start
(point))
384 (with-current-buffer buffer
385 (setq new-end
(point))
387 (setq new-start
(point)))
389 (let ((case-fold-search nil
))
390 (compare-buffer-substrings
391 (current-buffer) old-start old-end
392 buffer new-start new-end
))))
393 (setq all-match nil
))))
394 ;; Now new-end is the position of the start of the
395 ;; unchanged part in the current buffer, and old-end is
396 ;; the position of that same text in the saved old
397 ;; backtrace. But we must subtract (point-min) since strings are
398 ;; indexed in origin 0.
400 ;; Replace the unchanged part of the backtrace
401 ;; with the text from debugger-previous-backtrace,
402 ;; since that already has the proper xrefs.
403 ;; With this optimization, we only need to scan
404 ;; the changed part of the backtrace.
405 (delete-region new-end
(point-max))
406 (goto-char (point-max))
407 (insert (substring debugger-previous-backtrace
408 (- old-end
(point-min))))
409 ;; Make the unchanged part of the backtrace inaccessible
410 ;; so it won't be scanned.
411 (narrow-to-region (point-min) new-end
)))
413 ;; Scan the new part of the backtrace, inserting xrefs.
414 (goto-char (point-min))
416 (goto-char (+ (point) 2))
417 (skip-syntax-forward "^w_")
420 (end (progn (skip-syntax-forward "w_") (point)))
421 (sym (intern-soft (buffer-substring-no-properties
423 (file (and sym
(symbol-file sym
'defun
))))
426 ;; help-xref-button needs to operate on something matched
427 ;; by a regexp, so set that up for it.
428 (re-search-forward "\\(\\sw\\|\\s_\\)+")
429 (help-xref-button 0 'help-function-def sym file
)))
432 (setq debugger-previous-backtrace
(buffer-string)))))
434 (defun debugger-step-through ()
435 "Proceed, stepping through subexpressions of this expression.
436 Enter another debugger on next entry to eval, apply or funcall."
438 (setq debugger-step-after-exit t
)
439 (setq debugger-jumping-flag t
)
440 (setq debugger-will-be-back t
)
441 (add-hook 'post-command-hook
'debugger-reenable
)
442 (message "Proceeding, will debug on next eval or call.")
443 (exit-recursive-edit))
445 (defun debugger-continue ()
446 "Continue, evaluating this expression without stopping."
448 (unless debugger-may-continue
449 (error "Cannot continue"))
450 (message "Continuing.")
452 ;; Check to see if we've flagged some frame for debug-on-exit, in which
453 ;; case we'll probably come back to the debugger soon.
454 (goto-char (point-min))
455 (if (re-search-forward "^\\* " nil t
)
456 (setq debugger-will-be-back t
)))
457 (exit-recursive-edit))
459 (defun debugger-return-value (val)
460 "Continue, specifying value to return.
461 This is only useful when the value returned from the debugger
462 will be used, such as in a debug on exit from a frame."
463 (interactive "XReturn value (evaluated): ")
464 (when (memq (car debugger-args
) '(t lambda error debug
))
465 (error "Cannot return a value %s"
466 (if (eq (car debugger-args
) 'error
)
467 "from an error" "at function entrance")))
468 (setq debugger-value val
)
469 (princ "Returning " t
)
470 (prin1 debugger-value
)
472 ;; Check to see if we've flagged some frame for debug-on-exit, in which
473 ;; case we'll probably come back to the debugger soon.
474 (goto-char (point-min))
475 (if (re-search-forward "^\\* " nil t
)
476 (setq debugger-will-be-back t
)))
477 (exit-recursive-edit))
479 (defun debugger-jump ()
480 "Continue to exit from this frame, with all debug-on-entry suspended."
483 (setq debugger-jumping-flag t
)
484 (add-hook 'post-command-hook
'debugger-reenable
)
485 (message "Continuing through this frame")
486 (setq debugger-will-be-back t
)
487 (exit-recursive-edit))
489 (defun debugger-reenable ()
490 "Turn all debug-on-entry functions back on.
491 This function is put on `post-command-hook' by `debugger-jump' and
492 removes itself from that hook."
493 (setq debugger-jumping-flag nil
)
494 (remove-hook 'post-command-hook
'debugger-reenable
))
496 (defun debugger-frame-number (&optional skip-base
)
497 "Return number of frames in backtrace before the one point points at."
500 (if (looking-at " *;;;\\|[a-z]")
501 (error "This line is not a function call"))
502 (let ((opoint (point))
505 (while (not (eq (cadr (backtrace-frame count
)) 'debug
))
506 (setq count
(1+ count
)))
507 ;; Skip debug--implement-debug-on-entry frame.
508 (when (eq 'debug--implement-debug-on-entry
509 (cadr (backtrace-frame (1+ count
))))
510 (setq count
(+ 2 count
))))
511 (goto-char (point-min))
512 (when (looking-at "Debugger entered--\\(Lisp error\\|returning value\\):")
513 (goto-char (match-end 0))
518 (cond ((debugger--locals-visible-p)
519 (goto-char (next-single-char-property-change
520 (point) 'locals-visible
)))
521 ((= (following-char) ?\
()
527 (if (looking-at " *;;;")
529 (setq count
(1+ count
)))
532 (defun debugger-frame ()
533 "Request entry to debugger when this frame exits.
534 Applies to the frame whose line point is on in the backtrace."
536 (backtrace-debug (debugger-frame-number) t
)
538 (if (= (following-char) ?
)
539 (let ((inhibit-read-only t
))
544 (defun debugger-frame-clear ()
545 "Do not enter debugger when this frame exits.
546 Applies to the frame whose line point is on in the backtrace."
548 (backtrace-debug (debugger-frame-number) nil
)
550 (if (= (following-char) ?
*)
551 (let ((inhibit-read-only t
))
556 (defmacro debugger-env-macro
(&rest body
)
557 "Run BODY in original environment."
560 (set-match-data debugger-outer-match-data
)
563 (setq debugger-outer-match-data
(match-data)))))
565 (defun debugger--backtrace-base ()
566 "Return the function name that marks the top of the backtrace.
567 See `backtrace-frame'."
568 (cond ((eq 'debug--implement-debug-on-entry
569 (cadr (backtrace-frame 1 'debug
)))
570 'debug--implement-debug-on-entry
)
573 (defun debugger-eval-expression (exp &optional nframe
)
574 "Eval an expression, in an environment like that outside the debugger.
575 The environment used is the one when entering the activation frame at point."
577 (list (read--expression "Eval in stack frame: ")))
578 (let ((nframe (or nframe
579 (condition-case nil
(1+ (debugger-frame-number 'skip-base
))
580 (error 0)))) ;; If on first line.
581 (base (debugger--backtrace-base)))
583 (let ((val (backtrace-eval exp nframe base
)))
586 (let ((str (eval-expression-print-format val
)))
587 (if str
(princ str t
))))))))
589 (defun debugger--locals-visible-p ()
590 "Are the local variables of the current stack frame visible?"
593 (get-text-property (point) 'locals-visible
)))
595 (defun debugger--insert-locals (locals)
596 "Insert the local variables LOCALS at point."
598 (insert "\n [no locals]"))
600 (let ((print-escape-newlines t
))
602 (let ((symbol (car s
+v
))
605 (prin1 symbol
(current-buffer))
607 (prin1 value
(current-buffer))))))))
609 (defun debugger--show-locals ()
610 "For the frame at point, insert locals and add text properties."
611 (let* ((nframe (1+ (debugger-frame-number 'skip-base
)))
612 (base (debugger--backtrace-base))
613 (locals (backtrace--locals nframe base
))
614 (inhibit-read-only t
))
620 (debugger--insert-locals locals
)
621 (add-text-properties start
(point) '(locals-visible t
))))))
623 (defun debugger--hide-locals ()
624 "Delete local variables and remove the text property."
625 (let* ((col (current-column))
628 (next-single-char-property-change (point) 'locals-visible
)))
629 (start (previous-single-char-property-change end
'locals-visible
))
630 (inhibit-read-only t
))
631 (remove-text-properties start end
'(locals-visible))
634 (delete-region (point) end
)
635 (move-to-column col
)))
637 (defun debugger-toggle-locals ()
638 "Show or hide local variables of the current stack frame."
640 (cond ((debugger--locals-visible-p)
641 (debugger--hide-locals))
643 (debugger--show-locals))))
646 (defvar debugger-mode-map
647 (let ((map (make-keymap))
648 (menu-map (make-sparse-keymap)))
649 (set-keymap-parent map button-buffer-map
)
650 (suppress-keymap map
)
651 (define-key map
"-" 'negative-argument
)
652 (define-key map
"b" 'debugger-frame
)
653 (define-key map
"c" 'debugger-continue
)
654 (define-key map
"j" 'debugger-jump
)
655 (define-key map
"r" 'debugger-return-value
)
656 (define-key map
"u" 'debugger-frame-clear
)
657 (define-key map
"d" 'debugger-step-through
)
658 (define-key map
"l" 'debugger-list-functions
)
659 (define-key map
"h" 'describe-mode
)
660 (define-key map
"q" 'top-level
)
661 (define-key map
"e" 'debugger-eval-expression
)
662 (define-key map
"v" 'debugger-toggle-locals
) ; "v" is for "variables".
663 (define-key map
" " 'next-line
)
664 (define-key map
"R" 'debugger-record-expression
)
665 (define-key map
"\C-m" 'debug-help-follow
)
666 (define-key map
[mouse-2
] 'push-button
)
667 (define-key map
[menu-bar debugger
] (cons "Debugger" menu-map
))
668 (define-key menu-map
[deb-top
]
669 '(menu-item "Quit" top-level
670 :help
"Quit debugging and return to top level"))
671 (define-key menu-map
[deb-s0
] '("--"))
672 (define-key menu-map
[deb-descr
]
673 '(menu-item "Describe Debugger Mode" describe-mode
674 :help
"Display documentation for debugger-mode"))
675 (define-key menu-map
[deb-hfol
]
676 '(menu-item "Help Follow" debug-help-follow
677 :help
"Follow cross-reference"))
678 (define-key menu-map
[deb-nxt
]
679 '(menu-item "Next Line" next-line
680 :help
"Move cursor down"))
681 (define-key menu-map
[deb-s1
] '("--"))
682 (define-key menu-map
[deb-lfunc
]
683 '(menu-item "List debug on entry functions" debugger-list-functions
684 :help
"Display a list of all the functions now set to debug on entry"))
685 (define-key menu-map
[deb-fclear
]
686 '(menu-item "Cancel debug frame" debugger-frame-clear
687 :help
"Do not enter debugger when this frame exits"))
688 (define-key menu-map
[deb-frame
]
689 '(menu-item "Debug frame" debugger-frame
690 :help
"Request entry to debugger when this frame exits"))
691 (define-key menu-map
[deb-s2
] '("--"))
692 (define-key menu-map
[deb-ret
]
693 '(menu-item "Return value..." debugger-return-value
694 :help
"Continue, specifying value to return."))
695 (define-key menu-map
[deb-rec
]
696 '(menu-item "Display and Record Expression" debugger-record-expression
697 :help
"Display a variable's value and record it in `*Backtrace-record*' buffer"))
698 (define-key menu-map
[deb-eval
]
699 '(menu-item "Eval Expression..." debugger-eval-expression
700 :help
"Eval an expression, in an environment like that outside the debugger"))
701 (define-key menu-map
[deb-jump
]
702 '(menu-item "Jump" debugger-jump
703 :help
"Continue to exit from this frame, with all debug-on-entry suspended"))
704 (define-key menu-map
[deb-cont
]
705 '(menu-item "Continue" debugger-continue
706 :help
"Continue, evaluating this expression without stopping"))
707 (define-key menu-map
[deb-step
]
708 '(menu-item "Step through" debugger-step-through
709 :help
"Proceed, stepping through subexpressions of this expression"))
712 (put 'debugger-mode
'mode-class
'special
)
714 (define-derived-mode debugger-mode fundamental-mode
"Debugger"
715 "Mode for backtrace buffers, selected in debugger.
716 \\<debugger-mode-map>
717 A line starts with `*' if exiting that frame will call the debugger.
718 Type \\[debugger-frame] or \\[debugger-frame-clear] to set or remove the `*'.
720 When in debugger due to frame being exited,
721 use the \\[debugger-return-value] command to override the value
722 being returned from that frame.
724 Use \\[debug-on-entry] and \\[cancel-debug-on-entry] to control
725 which functions will enter the debugger when called.
727 Complete list of commands:
728 \\{debugger-mode-map}"
729 (setq truncate-lines t
)
730 (set-syntax-table emacs-lisp-mode-syntax-table
)
731 (use-local-map debugger-mode-map
))
733 (defcustom debugger-record-buffer
"*Debugger-record*"
734 "Buffer name for expression values, for \\[debugger-record-expression]."
739 (defun debugger-record-expression (exp)
740 "Display a variable's value and record it in `*Backtrace-record*' buffer."
742 (list (read--expression "Record Eval: ")))
743 (let* ((buffer (get-buffer-create debugger-record-buffer
))
744 (standard-output buffer
))
745 (princ (format "Debugger Eval (%s): " exp
))
746 (princ (debugger-eval-expression exp
))
749 (with-current-buffer (get-buffer debugger-record-buffer
)
751 (buffer-substring (line-beginning-position 0)
752 (line-end-position 0)))))
754 (defun debug-help-follow (&optional pos
)
755 "Follow cross-reference at POS, defaulting to point.
757 For the cross-reference format, see `help-make-xrefs'."
759 ;; Ideally we'd just do (call-interactively 'help-follow) except that this
760 ;; assumes we're already in a *Help* buffer and reuses it, so it ends up
761 ;; incorrectly "reusing" the *Backtrace* buffer to show the help info.
764 (unless (push-button pos
)
765 ;; check if the symbol under point is a function or variable
769 (goto-char pos
) (skip-syntax-backward "w_")
770 (buffer-substring (point)
771 (progn (skip-syntax-forward "w_")
773 (when (or (boundp sym
) (fboundp sym
) (facep sym
))
774 (describe-symbol sym
)))))
776 ;; When you change this, you may also need to change the number of
777 ;; frames that the debugger skips.
778 (defun debug--implement-debug-on-entry (&rest _ignore
)
779 "Conditionally call the debugger.
780 A call to this function is inserted by `debug-on-entry' to cause
781 functions to break on entry."
782 (if (or inhibit-debug-on-entry debugger-jumping-flag
)
784 (let ((inhibit-debug-on-entry t
))
785 (funcall debugger
'debug
))))
788 (defun debug-on-entry (function)
789 "Request FUNCTION to invoke debugger each time it is called.
791 When called interactively, prompt for FUNCTION in the minibuffer.
793 This works by modifying the definition of FUNCTION. If you tell the
794 debugger to continue, FUNCTION's execution proceeds. If FUNCTION is a
795 normal function or a macro written in Lisp, you can also step through
796 its execution. FUNCTION can also be a primitive that is not a special
797 form, in which case stepping is not possible. Break-on-entry for
798 primitive functions only works when that function is called from Lisp.
800 Use \\[cancel-debug-on-entry] to cancel the effect of this command.
801 Redefining FUNCTION also cancels it."
803 (let ((fn (function-called-at-point)) val
)
804 (when (special-form-p fn
)
806 (setq val
(completing-read
808 (format "Debug on entry to function (default %s): " fn
)
809 "Debug on entry to function: ")
812 (and (fboundp symbol
)
813 (not (special-form-p symbol
))))
814 t nil nil
(symbol-name fn
)))
815 (list (if (equal val
"") fn
(intern val
)))))
816 (advice-add function
:before
#'debug--implement-debug-on-entry
820 (defun debug--function-list ()
821 "List of functions currently set for debug on entry."
825 (when (advice-member-p #'debug--implement-debug-on-entry s
)
830 (defun cancel-debug-on-entry (&optional function
)
831 "Undo effect of \\[debug-on-entry] on FUNCTION.
832 If FUNCTION is nil, cancel debug-on-entry for all functions.
833 When called interactively, prompt for FUNCTION in the minibuffer.
834 To specify a nil argument interactively, exit with an empty minibuffer."
838 "Cancel debug on entry to function (default all functions): "
839 (mapcar #'symbol-name
(debug--function-list)) nil t
)))
841 (unless (string= name
"")
845 (advice-remove function
#'debug--implement-debug-on-entry
)
847 (message "Canceling debug-on-entry for all functions")
848 (mapcar #'cancel-debug-on-entry
(debug--function-list))))
850 (defun debugger-list-functions ()
851 "Display a list of all the functions now set to debug on entry."
854 (help-setup-xref '(debugger-list-functions)
855 (called-interactively-p 'interactive
))
856 (with-output-to-temp-buffer (help-buffer)
857 (with-current-buffer standard-output
858 (let ((funs (debug--function-list)))
860 (princ "No debug-on-entry functions now\n")
861 (princ "Functions set to debug on entry:\n\n")
863 (make-text-button (point) (progn (prin1 fun
) (point))
865 'help-args
(list fun
))
868 (princ "Note: if you have redefined a function, then it may no longer\n")
869 (princ "be set to debug on entry, even if it is in the list."))))))
871 (defun debug--implement-debug-watch (symbol newval op where
)
872 "Conditionally call the debugger.
873 This function is called when SYMBOL's value is modified."
874 (if (or inhibit-debug-on-entry debugger-jumping-flag
)
876 (let ((inhibit-debug-on-entry t
))
877 (funcall debugger
'watchpoint symbol newval op where
))))
880 (defun debug-on-variable-change (variable)
881 "Trigger a debugger invocation when VARIABLE is changed.
883 When called interactively, prompt for VARIABLE in the minibuffer.
885 This works by calling `add-variable-watch' on VARIABLE. If you
886 quit from the debugger, this will abort the change (unless the
887 change is caused by the termination of a let-binding).
889 The watchpoint may be circumvented by C code that changes the
890 variable directly (i.e., not via `set'). Changing the value of
891 the variable (e.g., `setcar' on a list variable) will not trigger
894 Use \\[cancel-debug-on-variable-change] to cancel the effect of
895 this command. Uninterning VARIABLE or making it an alias of
896 another symbol also cancels it."
898 (let* ((var-at-point (variable-at-point))
899 (var (and (symbolp var-at-point
) var-at-point
))
900 (val (completing-read
901 (concat "Debug when setting variable"
902 (if var
(format " (default %s): " var
) ": "))
904 t nil nil
(and var
(symbol-name var
)))))
905 (list (if (equal val
"") var
(intern val
)))))
906 (add-variable-watcher variable
#'debug--implement-debug-watch
))
909 (defalias 'debug-watch
#'debug-on-variable-change
)
912 (defun debug--variable-list ()
913 "List of variables currently set for debug on set."
917 (when (memq #'debug--implement-debug-watch
923 (defun cancel-debug-on-variable-change (&optional variable
)
924 "Undo effect of \\[debug-on-variable-change] on VARIABLE.
925 If VARIABLE is nil, cancel debug-on-variable-change for all variables.
926 When called interactively, prompt for VARIABLE in the minibuffer.
927 To specify a nil argument interactively, exit with an empty minibuffer."
931 "Cancel debug on set for variable (default all variables): "
932 (mapcar #'symbol-name
(debug--variable-list)) nil t
)))
934 (unless (string= name
"")
937 (remove-variable-watcher variable
#'debug--implement-debug-watch
)
938 (message "Canceling debug-watch for all variables")
939 (mapc #'cancel-debug-watch
(debug--variable-list))))
942 (defalias 'cancel-debug-watch
#'cancel-debug-on-variable-change
)
946 ;;; debug.el ends here