1 ;;; debug.el --- debuggers and related commands for Emacs -*- lexical-binding: t -*-
3 ;; Copyright (C) 1985-1986, 1994, 2001-2017 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 <https://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-print-function
#'cl-prin1
53 "Function used to print values in the debugger backtraces."
55 :options
'(cl-prin1 prin1
)
58 (defcustom debugger-bury-or-kill
'bury
59 "What to do with the debugger buffer when exiting `debug'.
60 The value affects the behavior of operations on any window
61 previously showing the debugger buffer.
63 nil means that if its window is not deleted when exiting the
64 debugger, invoking `switch-to-prev-buffer' will usually show
65 the debugger buffer again.
67 `append' means that if the window is not deleted, the debugger
68 buffer moves to the end of the window's previous buffers so
69 it's less likely that a future invocation of
70 `switch-to-prev-buffer' will switch to it. Also, it moves the
71 buffer to the end of the frame's buffer list.
73 `bury' means that if the window is not deleted, its buffer is
74 removed from the window's list of previous buffers. Also, it
75 moves the buffer to the end of the frame's buffer list. This
76 value provides the most reliable remedy to not have
77 `switch-to-prev-buffer' switch to the debugger buffer again
78 without killing the buffer.
80 `kill' means to kill the debugger buffer.
82 The value used here is passed to `quit-restore-window'."
84 (const :tag
"Keep alive" nil
)
85 (const :tag
"Append" append
)
86 (const :tag
"Bury" bury
)
87 (const :tag
"Kill" kill
))
91 (defvar debugger-step-after-exit nil
92 "Non-nil means \"single-step\" after the debugger exits.")
94 (defvar debugger-value nil
95 "This is the value for the debugger to return, when it returns.")
97 (defvar debugger-old-buffer nil
98 "This is the buffer that was current when the debugger was entered.")
100 (defvar debugger-previous-window nil
101 "This is the window last showing the debugger buffer.")
103 (defvar debugger-previous-window-height nil
104 "The last recorded height of `debugger-previous-window'.")
106 (defvar debugger-previous-backtrace nil
107 "The contents of the previous backtrace (including text properties).
108 This is to optimize `debugger-make-xrefs'.")
110 (defvar debugger-outer-match-data
)
111 (defvar debugger-will-be-back nil
112 "Non-nil if we expect to get back in the debugger soon.")
114 (defvar inhibit-debug-on-entry nil
115 "Non-nil means that `debug-on-entry' is disabled.")
117 (defvar debugger-jumping-flag nil
118 "Non-nil means that `debug-on-entry' is disabled.
119 This variable is used by `debugger-jump', `debugger-step-through',
120 and `debugger-reenable' to temporarily disable debug-on-entry.")
122 (defvar inhibit-trace
) ;Not yet implemented.
124 (defvar debugger-args nil
125 "Arguments with which the debugger was called.
126 It is a list expected to take the form (CAUSE . REST)
128 - debug: called for entry to a flagged function.
129 - t: called because of debug-on-next-call.
130 - lambda: same thing but via `funcall'.
131 - exit: called because of exit of a flagged function.
132 - error: called because of `debug-on-error'.")
135 (setq debugger
'debug
)
137 (defun debug (&rest args
)
138 "Enter debugger. \\<debugger-mode-map>`\\[debugger-continue]' returns from the debugger.
139 Arguments are mainly for use when this is called from the internals
142 You may call with no args, or you may pass nil as the first arg and
143 any other args you like. In that case, the list of args after the
144 first will be printed into the backtrace buffer."
146 (if inhibit-redisplay
147 ;; Don't really try to enter debugger within an eval from redisplay.
149 (unless noninteractive
150 (message "Entering debugger..."))
152 (debugger-previous-state
153 (if (get-buffer "*Backtrace*")
154 (with-current-buffer (get-buffer "*Backtrace*")
155 (list major-mode
(buffer-string)))))
157 (debugger-buffer (get-buffer-create "*Backtrace*"))
158 (debugger-old-buffer (current-buffer))
159 (debugger-window nil
)
160 (debugger-step-after-exit nil
)
161 (debugger-will-be-back nil
)
162 ;; Don't keep reading from an executing kbd macro!
163 (executing-kbd-macro nil
)
164 ;; Save the outer values of these vars for the `e' command
165 ;; before we replace the values.
166 (debugger-outer-match-data (match-data))
167 (debugger-with-timeout-suspend (with-timeout-suspend)))
168 ;; Set this instead of binding it, so that `q'
169 ;; will not restore it.
170 (setq overriding-terminal-local-map nil
)
171 ;; Don't let these magic variables affect the debugger itself.
172 (let ((last-command nil
) this-command track-mouse
174 unread-command-events
175 unread-post-input-method-events
176 last-input-event last-command-event last-nonmenu-event
180 ;; If we are inside a minibuffer, allow nesting
181 ;; so that we don't get an error from the `e' command.
182 (enable-recursive-minibuffers
183 (or enable-recursive-minibuffers
(> (minibuffer-depth) 0)))
184 (standard-input t
) (standard-output t
)
186 (cursor-in-echo-area nil
)
187 (window-configuration (current-window-configuration)))
190 (when (eq (car debugger-args
) 'debug
)
191 ;; Skip the frames for backtrace-debug, byte-code,
192 ;; debug--implement-debug-on-entry and the advice's `apply'.
193 (backtrace-debug 4 t
)
194 ;; Place an extra debug-on-exit for macro's.
195 (when (eq 'lambda
(car-safe (cadr (backtrace-frame 4))))
196 (backtrace-debug 5 t
)))
199 `((display-buffer-reuse-window
200 display-buffer-in-previous-window
)
201 .
(,(when (and (window-live-p debugger-previous-window
)
203 (window-frame debugger-previous-window
)))
204 `(previous-window .
,debugger-previous-window
)))))
205 (setq debugger-window
(selected-window))
206 (if (eq debugger-previous-window debugger-window
)
207 (when debugger-jumping-flag
208 ;; Try to restore previous height of debugger
213 (- debugger-previous-window-height
214 (window-total-height debugger-window
)))
216 (setq debugger-previous-window debugger-window
))
218 (debugger-setup-buffer debugger-args
)
220 ;; If the backtrace is long, save the beginning
221 ;; and the end, but discard the middle.
222 (when (> (count-lines (point-min) (point-max))
223 debugger-batch-max-lines
)
224 (goto-char (point-min))
225 (forward-line (/ 2 debugger-batch-max-lines
))
226 (let ((middlestart (point)))
227 (goto-char (point-max))
228 (forward-line (- (/ 2 debugger-batch-max-lines
)
229 debugger-batch-max-lines
))
230 (delete-region middlestart
(point)))
232 (goto-char (point-min))
233 (message "%s" (buffer-string))
236 (let ((standard-output nil
)
237 (buffer-read-only t
))
239 ;; Make sure we unbind buffer-read-only in the right buffer.
242 (when (and (window-live-p debugger-window
)
243 (eq (window-buffer debugger-window
) debugger-buffer
))
244 ;; Record height of debugger window.
245 (setq debugger-previous-window-height
246 (window-total-height debugger-window
)))
247 (if debugger-will-be-back
248 ;; Restore previous window configuration (Bug#12623).
249 (set-window-configuration window-configuration
)
250 (when (and (window-live-p debugger-window
)
251 (eq (window-buffer debugger-window
) debugger-buffer
))
253 ;; Unshow debugger-buffer.
254 (quit-restore-window debugger-window debugger-bury-or-kill
)
255 ;; Restore current buffer (Bug#12502).
256 (set-buffer debugger-old-buffer
)))
257 ;; Forget debugger window, it won't be back (Bug#17882).
258 (setq debugger-previous-window nil
))
259 ;; Restore previous state of debugger-buffer in case we were
260 ;; in a recursive invocation of the debugger, otherwise just
261 ;; erase the buffer and put it into fundamental mode.
262 (when (buffer-live-p debugger-buffer
)
263 (with-current-buffer debugger-buffer
264 (let ((inhibit-read-only t
))
266 (if (null debugger-previous-state
)
268 (insert (nth 1 debugger-previous-state
))
269 (funcall (nth 0 debugger-previous-state
))))))
270 (with-timeout-unsuspend debugger-with-timeout-suspend
)
271 (set-match-data debugger-outer-match-data
)))
272 (setq debug-on-next-call debugger-step-after-exit
)
276 (defun debugger-insert-backtrace (frames do-xrefs
)
277 "Format and insert the backtrace FRAMES at point.
278 Make functions into cross-reference buttons if DO-XREFS is non-nil."
279 (let ((standard-output (current-buffer))
280 (eval-buffers eval-buffer-list
))
281 (require 'help-mode
) ; Define `help-function-def' button type.
282 (pcase-dolist (`(,evald
,fun
,args
,flags
) frames
)
283 (insert (if (plist-get flags
:debug-on-exit
)
285 (let ((fun-file (and do-xrefs
(symbol-file fun
'defun
)))
288 ((and evald
(not debugger-stack-frame-as-list
))
289 (funcall debugger-print-function fun
)
290 (if args
(funcall debugger-print-function args
) (princ "()")))
292 (funcall debugger-print-function
(cons fun args
))
295 (make-text-button fun-pt
(+ fun-pt
(length (symbol-name fun
)))
296 :type
'help-function-def
297 'help-args
(list fun fun-file
))))
298 ;; After any frame that uses eval-buffer, insert a line that
299 ;; states the buffer position it's reading at.
300 (when (and eval-buffers
(memq fun
'(eval-buffer eval-region
)))
301 (insert (format " ; Reading at buffer position %d"
302 ;; This will get the wrong result if there are
303 ;; two nested eval-region calls for the same
304 ;; buffer. That's not a very useful case.
305 (with-current-buffer (pop eval-buffers
)
309 (defun debugger-setup-buffer (args)
310 "Initialize the `*Backtrace*' buffer for entry to the debugger.
311 That buffer should be current already."
312 (setq buffer-read-only nil
)
314 (set-buffer-multibyte t
) ;Why was it nil ? -stef
315 (setq buffer-undo-list t
)
316 (insert "Debugger entered")
317 (let ((frames (nthcdr
318 ;; Remove debug--implement-debug-on-entry and the
319 ;; advice's `apply' frame.
320 (if (eq (car args
) 'debug
) 3 1)
321 (backtrace-frames 'debug
)))
322 (print-escape-newlines t
)
323 (print-escape-control-characters t
)
328 ;; lambda is for debug-on-call when a function call is next.
329 ;; debug is for debug-on-entry function called.
331 (insert "--entering a function:\n")
332 (setq pos
(1- (point))))
333 ;; Exiting a function.
335 (insert "--returning value: ")
337 (setq debugger-value
(nth 1 args
))
338 (funcall debugger-print-function debugger-value
(current-buffer))
339 (setf (cl-getf (nth 3 (car frames
)) :debug-on-exit
) nil
)
341 ;; Watchpoint triggered.
342 ((and `watchpoint
(let `(,symbol
,newval .
,details
) (cdr args
)))
346 (`(makunbound nil
) (format "making %s void" symbol
))
347 (`(makunbound ,buffer
) (format "killing local value of %s in buffer %s"
349 (`(defvaralias ,_
) (format "aliasing %s to %s" symbol newval
))
350 (`(let ,_
) (format "let-binding %s to %S" symbol newval
))
351 (`(unlet ,_
) (format "ending let-binding of %s" symbol
))
352 (`(set nil
) (format "setting %s to %S" symbol newval
))
353 (`(set ,buffer
) (format "setting %s in buffer %s to %S"
354 symbol buffer newval
))
355 (_ (error "unrecognized watchpoint triggered %S" (cdr args
))))
359 ;; Debugger entered for an error.
361 (insert "--Lisp error: ")
363 (funcall debugger-print-function
(nth 1 args
) (current-buffer))
365 ;; debug-on-call, when the next thing is an eval.
367 (insert "--beginning evaluation of function call form:\n")
368 (setq pos
(1- (point))))
369 ;; User calls debug directly.
373 (funcall debugger-print-function
374 (if (eq (car args
) 'nil
)
378 (debugger-insert-backtrace frames t
)
379 ;; Place point on "stack frame 0" (bug#15101).
383 (defun debugger-step-through ()
384 "Proceed, stepping through subexpressions of this expression.
385 Enter another debugger on next entry to eval, apply or funcall."
387 (setq debugger-step-after-exit t
)
388 (setq debugger-jumping-flag t
)
389 (setq debugger-will-be-back t
)
390 (add-hook 'post-command-hook
'debugger-reenable
)
391 (message "Proceeding, will debug on next eval or call.")
392 (exit-recursive-edit))
394 (defun debugger-continue ()
395 "Continue, evaluating this expression without stopping."
397 (unless debugger-may-continue
398 (error "Cannot continue"))
399 (message "Continuing.")
401 ;; Check to see if we've flagged some frame for debug-on-exit, in which
402 ;; case we'll probably come back to the debugger soon.
403 (goto-char (point-min))
404 (if (re-search-forward "^\\* " nil t
)
405 (setq debugger-will-be-back t
)))
406 (exit-recursive-edit))
408 (defun debugger-return-value (val)
409 "Continue, specifying value to return.
410 This is only useful when the value returned from the debugger
411 will be used, such as in a debug on exit from a frame."
412 (interactive "XReturn value (evaluated): ")
413 (when (memq (car debugger-args
) '(t lambda error debug
))
414 (error "Cannot return a value %s"
415 (if (eq (car debugger-args
) 'error
)
416 "from an error" "at function entrance")))
417 (setq debugger-value val
)
418 (princ "Returning " t
)
419 (prin1 debugger-value
)
421 ;; Check to see if we've flagged some frame for debug-on-exit, in which
422 ;; case we'll probably come back to the debugger soon.
423 (goto-char (point-min))
424 (if (re-search-forward "^\\* " nil t
)
425 (setq debugger-will-be-back t
)))
426 (exit-recursive-edit))
428 (defun debugger-jump ()
429 "Continue to exit from this frame, with all debug-on-entry suspended."
432 (setq debugger-jumping-flag t
)
433 (add-hook 'post-command-hook
'debugger-reenable
)
434 (message "Continuing through this frame")
435 (setq debugger-will-be-back t
)
436 (exit-recursive-edit))
438 (defun debugger-reenable ()
439 "Turn all debug-on-entry functions back on.
440 This function is put on `post-command-hook' by `debugger-jump' and
441 removes itself from that hook."
442 (setq debugger-jumping-flag nil
)
443 (remove-hook 'post-command-hook
'debugger-reenable
))
445 (defun debugger-frame-number (&optional skip-base
)
446 "Return number of frames in backtrace before the one point points at."
449 (if (looking-at " *;;;\\|[a-z]")
450 (error "This line is not a function call"))
451 (let ((opoint (point))
454 (while (not (eq (cadr (backtrace-frame count
)) 'debug
))
455 (setq count
(1+ count
)))
456 ;; Skip debug--implement-debug-on-entry frame.
457 (when (eq 'debug--implement-debug-on-entry
458 (cadr (backtrace-frame (1+ count
))))
459 (setq count
(+ 2 count
))))
460 (goto-char (point-min))
461 (when (looking-at "Debugger entered--\\(Lisp error\\|returning value\\):")
462 (goto-char (match-end 0))
467 (cond ((debugger--locals-visible-p)
468 (goto-char (next-single-char-property-change
469 (point) 'locals-visible
)))
470 ((= (following-char) ?\
()
476 (if (looking-at " *;;;")
478 (setq count
(1+ count
)))
481 (defun debugger-frame ()
482 "Request entry to debugger when this frame exits.
483 Applies to the frame whose line point is on in the backtrace."
485 (backtrace-debug (debugger-frame-number) t
)
487 (if (= (following-char) ?
)
488 (let ((inhibit-read-only t
))
493 (defun debugger-frame-clear ()
494 "Do not enter debugger when this frame exits.
495 Applies to the frame whose line point is on in the backtrace."
497 (backtrace-debug (debugger-frame-number) nil
)
499 (if (= (following-char) ?
*)
500 (let ((inhibit-read-only t
))
505 (defmacro debugger-env-macro
(&rest body
)
506 "Run BODY in original environment."
509 (set-match-data debugger-outer-match-data
)
512 (setq debugger-outer-match-data
(match-data)))))
514 (defun debugger--backtrace-base ()
515 "Return the function name that marks the top of the backtrace.
516 See `backtrace-frame'."
517 (cond ((eq 'debug--implement-debug-on-entry
518 (cadr (backtrace-frame 1 'debug
)))
519 'debug--implement-debug-on-entry
)
522 (defun debugger-eval-expression (exp &optional nframe
)
523 "Eval an expression, in an environment like that outside the debugger.
524 The environment used is the one when entering the activation frame at point."
526 (list (read--expression "Eval in stack frame: ")))
527 (let ((nframe (or nframe
528 (condition-case nil
(1+ (debugger-frame-number 'skip-base
))
529 (error 0)))) ;; If on first line.
530 (base (debugger--backtrace-base)))
532 (let ((val (backtrace-eval exp nframe base
)))
535 (let ((str (eval-expression-print-format val
)))
536 (if str
(princ str t
))))))))
538 (defun debugger--locals-visible-p ()
539 "Are the local variables of the current stack frame visible?"
542 (get-text-property (point) 'locals-visible
)))
544 (defun debugger--insert-locals (locals)
545 "Insert the local variables LOCALS at point."
547 (insert "\n [no locals]"))
549 (let ((print-escape-newlines t
))
551 (let ((symbol (car s
+v
))
554 (prin1 symbol
(current-buffer))
556 (prin1 value
(current-buffer))))))))
558 (defun debugger--show-locals ()
559 "For the frame at point, insert locals and add text properties."
560 (let* ((nframe (1+ (debugger-frame-number 'skip-base
)))
561 (base (debugger--backtrace-base))
562 (locals (backtrace--locals nframe base
))
563 (inhibit-read-only t
))
569 (debugger--insert-locals locals
)
570 (add-text-properties start
(point) '(locals-visible t
))))))
572 (defun debugger--hide-locals ()
573 "Delete local variables and remove the text property."
574 (let* ((col (current-column))
577 (next-single-char-property-change (point) 'locals-visible
)))
578 (start (previous-single-char-property-change end
'locals-visible
))
579 (inhibit-read-only t
))
580 (remove-text-properties start end
'(locals-visible))
583 (delete-region (point) end
)
584 (move-to-column col
)))
586 (defun debugger-toggle-locals ()
587 "Show or hide local variables of the current stack frame."
589 (cond ((debugger--locals-visible-p)
590 (debugger--hide-locals))
592 (debugger--show-locals))))
595 (defvar debugger-mode-map
596 (let ((map (make-keymap))
597 (menu-map (make-sparse-keymap)))
598 (set-keymap-parent map button-buffer-map
)
599 (suppress-keymap map
)
600 (define-key map
"-" 'negative-argument
)
601 (define-key map
"b" 'debugger-frame
)
602 (define-key map
"c" 'debugger-continue
)
603 (define-key map
"j" 'debugger-jump
)
604 (define-key map
"r" 'debugger-return-value
)
605 (define-key map
"u" 'debugger-frame-clear
)
606 (define-key map
"d" 'debugger-step-through
)
607 (define-key map
"l" 'debugger-list-functions
)
608 (define-key map
"h" 'describe-mode
)
609 (define-key map
"q" 'top-level
)
610 (define-key map
"e" 'debugger-eval-expression
)
611 (define-key map
"v" 'debugger-toggle-locals
) ; "v" is for "variables".
612 (define-key map
" " 'next-line
)
613 (define-key map
"R" 'debugger-record-expression
)
614 (define-key map
"\C-m" 'debug-help-follow
)
615 (define-key map
[mouse-2
] 'push-button
)
616 (define-key map
[menu-bar debugger
] (cons "Debugger" menu-map
))
617 (define-key menu-map
[deb-top
]
618 '(menu-item "Quit" top-level
619 :help
"Quit debugging and return to top level"))
620 (define-key menu-map
[deb-s0
] '("--"))
621 (define-key menu-map
[deb-descr
]
622 '(menu-item "Describe Debugger Mode" describe-mode
623 :help
"Display documentation for debugger-mode"))
624 (define-key menu-map
[deb-hfol
]
625 '(menu-item "Help Follow" debug-help-follow
626 :help
"Follow cross-reference"))
627 (define-key menu-map
[deb-nxt
]
628 '(menu-item "Next Line" next-line
629 :help
"Move cursor down"))
630 (define-key menu-map
[deb-s1
] '("--"))
631 (define-key menu-map
[deb-lfunc
]
632 '(menu-item "List debug on entry functions" debugger-list-functions
633 :help
"Display a list of all the functions now set to debug on entry"))
634 (define-key menu-map
[deb-fclear
]
635 '(menu-item "Cancel debug frame" debugger-frame-clear
636 :help
"Do not enter debugger when this frame exits"))
637 (define-key menu-map
[deb-frame
]
638 '(menu-item "Debug frame" debugger-frame
639 :help
"Request entry to debugger when this frame exits"))
640 (define-key menu-map
[deb-s2
] '("--"))
641 (define-key menu-map
[deb-ret
]
642 '(menu-item "Return value..." debugger-return-value
643 :help
"Continue, specifying value to return."))
644 (define-key menu-map
[deb-rec
]
645 '(menu-item "Display and Record Expression" debugger-record-expression
646 :help
"Display a variable's value and record it in `*Backtrace-record*' buffer"))
647 (define-key menu-map
[deb-eval
]
648 '(menu-item "Eval Expression..." debugger-eval-expression
649 :help
"Eval an expression, in an environment like that outside the debugger"))
650 (define-key menu-map
[deb-jump
]
651 '(menu-item "Jump" debugger-jump
652 :help
"Continue to exit from this frame, with all debug-on-entry suspended"))
653 (define-key menu-map
[deb-cont
]
654 '(menu-item "Continue" debugger-continue
655 :help
"Continue, evaluating this expression without stopping"))
656 (define-key menu-map
[deb-step
]
657 '(menu-item "Step through" debugger-step-through
658 :help
"Proceed, stepping through subexpressions of this expression"))
661 (put 'debugger-mode
'mode-class
'special
)
663 (define-derived-mode debugger-mode fundamental-mode
"Debugger"
664 "Mode for backtrace buffers, selected in debugger.
665 \\<debugger-mode-map>
666 A line starts with `*' if exiting that frame will call the debugger.
667 Type \\[debugger-frame] or \\[debugger-frame-clear] to set or remove the `*'.
669 When in debugger due to frame being exited,
670 use the \\[debugger-return-value] command to override the value
671 being returned from that frame.
673 Use \\[debug-on-entry] and \\[cancel-debug-on-entry] to control
674 which functions will enter the debugger when called.
676 Complete list of commands:
677 \\{debugger-mode-map}"
678 (setq truncate-lines t
)
679 (set-syntax-table emacs-lisp-mode-syntax-table
)
680 (add-hook 'kill-buffer-hook
681 (lambda () (if (> (recursion-depth) 0) (top-level)))
683 (use-local-map debugger-mode-map
))
685 (defcustom debugger-record-buffer
"*Debugger-record*"
686 "Buffer name for expression values, for \\[debugger-record-expression]."
691 (defun debugger-record-expression (exp)
692 "Display a variable's value and record it in `*Backtrace-record*' buffer."
694 (list (read--expression "Record Eval: ")))
695 (let* ((buffer (get-buffer-create debugger-record-buffer
))
696 (standard-output buffer
))
697 (princ (format "Debugger Eval (%s): " exp
))
698 (princ (debugger-eval-expression exp
))
701 (with-current-buffer (get-buffer debugger-record-buffer
)
703 (buffer-substring (line-beginning-position 0)
704 (line-end-position 0)))))
706 (defun debug-help-follow (&optional pos
)
707 "Follow cross-reference at POS, defaulting to point.
709 For the cross-reference format, see `help-make-xrefs'."
711 ;; Ideally we'd just do (call-interactively 'help-follow) except that this
712 ;; assumes we're already in a *Help* buffer and reuses it, so it ends up
713 ;; incorrectly "reusing" the *Backtrace* buffer to show the help info.
716 (unless (push-button pos
)
717 ;; check if the symbol under point is a function or variable
721 (goto-char pos
) (skip-syntax-backward "w_")
722 (buffer-substring (point)
723 (progn (skip-syntax-forward "w_")
725 (when (or (boundp sym
) (fboundp sym
) (facep sym
))
726 (describe-symbol sym
)))))
728 ;; When you change this, you may also need to change the number of
729 ;; frames that the debugger skips.
730 (defun debug--implement-debug-on-entry (&rest _ignore
)
731 "Conditionally call the debugger.
732 A call to this function is inserted by `debug-on-entry' to cause
733 functions to break on entry."
734 (if (or inhibit-debug-on-entry debugger-jumping-flag
)
736 (let ((inhibit-debug-on-entry t
))
737 (funcall debugger
'debug
))))
740 (defun debug-on-entry (function)
741 "Request FUNCTION to invoke debugger each time it is called.
743 When called interactively, prompt for FUNCTION in the minibuffer.
745 This works by modifying the definition of FUNCTION. If you tell the
746 debugger to continue, FUNCTION's execution proceeds. If FUNCTION is a
747 normal function or a macro written in Lisp, you can also step through
748 its execution. FUNCTION can also be a primitive that is not a special
749 form, in which case stepping is not possible. Break-on-entry for
750 primitive functions only works when that function is called from Lisp.
752 Use \\[cancel-debug-on-entry] to cancel the effect of this command.
753 Redefining FUNCTION also cancels it."
755 (let ((fn (function-called-at-point)) val
)
756 (when (special-form-p fn
)
758 (setq val
(completing-read
760 (format "Debug on entry to function (default %s): " fn
)
761 "Debug on entry to function: ")
764 (and (fboundp symbol
)
765 (not (special-form-p symbol
))))
766 t nil nil
(symbol-name fn
)))
767 (list (if (equal val
"") fn
(intern val
)))))
768 (advice-add function
:before
#'debug--implement-debug-on-entry
772 (defun debug--function-list ()
773 "List of functions currently set for debug on entry."
777 (when (advice-member-p #'debug--implement-debug-on-entry s
)
782 (defun cancel-debug-on-entry (&optional function
)
783 "Undo effect of \\[debug-on-entry] on FUNCTION.
784 If FUNCTION is nil, cancel debug-on-entry for all functions.
785 When called interactively, prompt for FUNCTION in the minibuffer.
786 To specify a nil argument interactively, exit with an empty minibuffer."
790 "Cancel debug on entry to function (default all functions): "
791 (mapcar #'symbol-name
(debug--function-list)) nil t
)))
793 (unless (string= name
"")
797 (advice-remove function
#'debug--implement-debug-on-entry
)
799 (message "Canceling debug-on-entry for all functions")
800 (mapcar #'cancel-debug-on-entry
(debug--function-list))))
802 (defun debugger-list-functions ()
803 "Display a list of all the functions now set to debug on entry."
806 (help-setup-xref '(debugger-list-functions)
807 (called-interactively-p 'interactive
))
808 (with-output-to-temp-buffer (help-buffer)
809 (with-current-buffer standard-output
810 (let ((funs (debug--function-list)))
812 (princ "No debug-on-entry functions now\n")
813 (princ "Functions set to debug on entry:\n\n")
815 (make-text-button (point) (progn (prin1 fun
) (point))
817 'help-args
(list fun
))
819 ;; Now that debug--function-list uses advice-member-p, its
820 ;; output should be reliable (except for bugs and the exceptional
821 ;; case where some other advice ends up overriding ours).
823 ;;(princ "Note: if you have redefined a function, then it may no longer\n")
824 ;;(princ "be set to debug on entry, even if it is in the list.")
827 (defun debug--implement-debug-watch (symbol newval op where
)
828 "Conditionally call the debugger.
829 This function is called when SYMBOL's value is modified."
830 (if (or inhibit-debug-on-entry debugger-jumping-flag
)
832 (let ((inhibit-debug-on-entry t
))
833 (funcall debugger
'watchpoint symbol newval op where
))))
836 (defun debug-on-variable-change (variable)
837 "Trigger a debugger invocation when VARIABLE is changed.
839 When called interactively, prompt for VARIABLE in the minibuffer.
841 This works by calling `add-variable-watch' on VARIABLE. If you
842 quit from the debugger, this will abort the change (unless the
843 change is caused by the termination of a let-binding).
845 The watchpoint may be circumvented by C code that changes the
846 variable directly (i.e., not via `set'). Changing the value of
847 the variable (e.g., `setcar' on a list variable) will not trigger
850 Use \\[cancel-debug-on-variable-change] to cancel the effect of
851 this command. Uninterning VARIABLE or making it an alias of
852 another symbol also cancels it."
854 (let* ((var-at-point (variable-at-point))
855 (var (and (symbolp var-at-point
) var-at-point
))
856 (val (completing-read
857 (concat "Debug when setting variable"
858 (if var
(format " (default %s): " var
) ": "))
860 t nil nil
(and var
(symbol-name var
)))))
861 (list (if (equal val
"") var
(intern val
)))))
862 (add-variable-watcher variable
#'debug--implement-debug-watch
))
865 (defalias 'debug-watch
#'debug-on-variable-change
)
868 (defun debug--variable-list ()
869 "List of variables currently set for debug on set."
873 (when (memq #'debug--implement-debug-watch
879 (defun cancel-debug-on-variable-change (&optional variable
)
880 "Undo effect of \\[debug-on-variable-change] on VARIABLE.
881 If VARIABLE is nil, cancel debug-on-variable-change for all variables.
882 When called interactively, prompt for VARIABLE in the minibuffer.
883 To specify a nil argument interactively, exit with an empty minibuffer."
887 "Cancel debug on set for variable (default all variables): "
888 (mapcar #'symbol-name
(debug--variable-list)) nil t
)))
890 (unless (string= name
"")
893 (remove-variable-watcher variable
#'debug--implement-debug-watch
)
894 (message "Canceling debug-watch for all variables")
895 (mapc #'cancel-debug-watch
(debug--variable-list))))
898 (defalias 'cancel-debug-watch
#'cancel-debug-on-variable-change
)
902 ;;; debug.el ends here