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."
54 :type
'(choice (const cl-prin1
)
59 (defcustom debugger-bury-or-kill
'bury
60 "What to do with the debugger buffer when exiting `debug'.
61 The value affects the behavior of operations on any window
62 previously showing the debugger buffer.
64 nil means that if its window is not deleted when exiting the
65 debugger, invoking `switch-to-prev-buffer' will usually show
66 the debugger buffer again.
68 `append' means that if the window is not deleted, the debugger
69 buffer moves to the end of the window's previous buffers so
70 it's less likely that a future invocation of
71 `switch-to-prev-buffer' will switch to it. Also, it moves the
72 buffer to the end of the frame's buffer list.
74 `bury' means that if the window is not deleted, its buffer is
75 removed from the window's list of previous buffers. Also, it
76 moves the buffer to the end of the frame's buffer list. This
77 value provides the most reliable remedy to not have
78 `switch-to-prev-buffer' switch to the debugger buffer again
79 without killing the buffer.
81 `kill' means to kill the debugger buffer.
83 The value used here is passed to `quit-restore-window'."
85 (const :tag
"Keep alive" nil
)
86 (const :tag
"Append" append
)
87 (const :tag
"Bury" bury
)
88 (const :tag
"Kill" kill
))
92 (defvar debugger-step-after-exit nil
93 "Non-nil means \"single-step\" after the debugger exits.")
95 (defvar debugger-value nil
96 "This is the value for the debugger to return, when it returns.")
98 (defvar debugger-old-buffer nil
99 "This is the buffer that was current when the debugger was entered.")
101 (defvar debugger-previous-window nil
102 "This is the window last showing the debugger buffer.")
104 (defvar debugger-previous-window-height nil
105 "The last recorded height of `debugger-previous-window'.")
107 (defvar debugger-previous-backtrace nil
108 "The contents of the previous backtrace (including text properties).
109 This is to optimize `debugger-make-xrefs'.")
111 (defvar debugger-outer-match-data
)
112 (defvar debugger-will-be-back nil
113 "Non-nil if we expect to get back in the debugger soon.")
115 (defvar inhibit-debug-on-entry nil
116 "Non-nil means that `debug-on-entry' is disabled.")
118 (defvar debugger-jumping-flag nil
119 "Non-nil means that `debug-on-entry' is disabled.
120 This variable is used by `debugger-jump', `debugger-step-through',
121 and `debugger-reenable' to temporarily disable debug-on-entry.")
123 (defvar inhibit-trace
) ;Not yet implemented.
125 (defvar debugger-args nil
126 "Arguments with which the debugger was called.
127 It is a list expected to take the form (CAUSE . REST)
129 - debug: called for entry to a flagged function.
130 - t: called because of debug-on-next-call.
131 - lambda: same thing but via `funcall'.
132 - exit: called because of exit of a flagged function.
133 - error: called because of `debug-on-error'.")
136 (setq debugger
'debug
)
138 (defun debug (&rest args
)
139 "Enter debugger. \\<debugger-mode-map>`\\[debugger-continue]' returns from the debugger.
140 Arguments are mainly for use when this is called from the internals
143 You may call with no args, or you may pass nil as the first arg and
144 any other args you like. In that case, the list of args after the
145 first will be printed into the backtrace buffer."
147 (if inhibit-redisplay
148 ;; Don't really try to enter debugger within an eval from redisplay.
150 (unless noninteractive
151 (message "Entering debugger..."))
153 (debugger-previous-state
154 (if (get-buffer "*Backtrace*")
155 (with-current-buffer (get-buffer "*Backtrace*")
156 (list major-mode
(buffer-string)))))
158 (debugger-buffer (get-buffer-create "*Backtrace*"))
159 (debugger-old-buffer (current-buffer))
160 (debugger-window nil
)
161 (debugger-step-after-exit nil
)
162 (debugger-will-be-back nil
)
163 ;; Don't keep reading from an executing kbd macro!
164 (executing-kbd-macro nil
)
165 ;; Save the outer values of these vars for the `e' command
166 ;; before we replace the values.
167 (debugger-outer-match-data (match-data))
168 (debugger-with-timeout-suspend (with-timeout-suspend)))
169 ;; Set this instead of binding it, so that `q'
170 ;; will not restore it.
171 (setq overriding-terminal-local-map nil
)
172 ;; Don't let these magic variables affect the debugger itself.
173 (let ((last-command nil
) this-command track-mouse
175 unread-command-events
176 unread-post-input-method-events
177 last-input-event last-command-event last-nonmenu-event
181 ;; If we are inside a minibuffer, allow nesting
182 ;; so that we don't get an error from the `e' command.
183 (enable-recursive-minibuffers
184 (or enable-recursive-minibuffers
(> (minibuffer-depth) 0)))
185 (standard-input t
) (standard-output t
)
187 (cursor-in-echo-area nil
)
188 (window-configuration (current-window-configuration)))
191 (when (eq (car debugger-args
) 'debug
)
192 ;; Skip the frames for backtrace-debug, byte-code,
193 ;; debug--implement-debug-on-entry and the advice's `apply'.
194 (backtrace-debug 4 t
)
195 ;; Place an extra debug-on-exit for macro's.
196 (when (eq 'lambda
(car-safe (cadr (backtrace-frame 4))))
197 (backtrace-debug 5 t
)))
200 `((display-buffer-reuse-window
201 display-buffer-in-previous-window
)
202 .
(,(when (and (window-live-p debugger-previous-window
)
204 (window-frame debugger-previous-window
)))
205 `(previous-window .
,debugger-previous-window
)))))
206 (setq debugger-window
(selected-window))
207 (if (eq debugger-previous-window debugger-window
)
208 (when debugger-jumping-flag
209 ;; Try to restore previous height of debugger
214 (- debugger-previous-window-height
215 (window-total-height debugger-window
)))
217 (setq debugger-previous-window debugger-window
))
219 (debugger-setup-buffer debugger-args
)
221 ;; If the backtrace is long, save the beginning
222 ;; and the end, but discard the middle.
223 (when (> (count-lines (point-min) (point-max))
224 debugger-batch-max-lines
)
225 (goto-char (point-min))
226 (forward-line (/ 2 debugger-batch-max-lines
))
227 (let ((middlestart (point)))
228 (goto-char (point-max))
229 (forward-line (- (/ 2 debugger-batch-max-lines
)
230 debugger-batch-max-lines
))
231 (delete-region middlestart
(point)))
233 (goto-char (point-min))
234 (message "%s" (buffer-string))
237 (let ((standard-output nil
)
238 (buffer-read-only t
))
240 ;; Make sure we unbind buffer-read-only in the right buffer.
243 (when (and (window-live-p debugger-window
)
244 (eq (window-buffer debugger-window
) debugger-buffer
))
245 ;; Record height of debugger window.
246 (setq debugger-previous-window-height
247 (window-total-height debugger-window
)))
248 (if debugger-will-be-back
249 ;; Restore previous window configuration (Bug#12623).
250 (set-window-configuration window-configuration
)
251 (when (and (window-live-p debugger-window
)
252 (eq (window-buffer debugger-window
) debugger-buffer
))
254 ;; Unshow debugger-buffer.
255 (quit-restore-window debugger-window debugger-bury-or-kill
)
256 ;; Restore current buffer (Bug#12502).
257 (set-buffer debugger-old-buffer
)))
258 ;; Forget debugger window, it won't be back (Bug#17882).
259 (setq debugger-previous-window nil
))
260 ;; Restore previous state of debugger-buffer in case we were
261 ;; in a recursive invocation of the debugger, otherwise just
262 ;; erase the buffer and put it into fundamental mode.
263 (when (buffer-live-p debugger-buffer
)
264 (with-current-buffer debugger-buffer
265 (let ((inhibit-read-only t
))
267 (if (null debugger-previous-state
)
269 (insert (nth 1 debugger-previous-state
))
270 (funcall (nth 0 debugger-previous-state
))))))
271 (with-timeout-unsuspend debugger-with-timeout-suspend
)
272 (set-match-data debugger-outer-match-data
)))
273 (setq debug-on-next-call debugger-step-after-exit
)
276 (defun debugger--print (obj &optional stream
)
278 (funcall debugger-print-function obj stream
)
280 (message "Error in debug printer: %S" err
)
281 (prin1 obj stream
))))
283 (defun debugger-insert-backtrace (frames do-xrefs
)
284 "Format and insert the backtrace FRAMES at point.
285 Make functions into cross-reference buttons if DO-XREFS is non-nil."
286 (let ((standard-output (current-buffer))
287 (eval-buffers eval-buffer-list
))
288 (require 'help-mode
) ; Define `help-function-def' button type.
289 (pcase-dolist (`(,evald
,fun
,args
,flags
) frames
)
290 (insert (if (plist-get flags
:debug-on-exit
)
292 (let ((fun-file (and do-xrefs
(symbol-file fun
'defun
)))
295 ((and evald
(not debugger-stack-frame-as-list
))
296 (debugger--print fun
)
297 (if args
(debugger--print args
) (princ "()")))
299 (debugger--print (cons fun args
))
302 (make-text-button fun-pt
(+ fun-pt
(length (symbol-name fun
)))
303 :type
'help-function-def
304 'help-args
(list fun fun-file
))))
305 ;; After any frame that uses eval-buffer, insert a line that
306 ;; states the buffer position it's reading at.
307 (when (and eval-buffers
(memq fun
'(eval-buffer eval-region
)))
308 (insert (format " ; Reading at buffer position %d"
309 ;; This will get the wrong result if there are
310 ;; two nested eval-region calls for the same
311 ;; buffer. That's not a very useful case.
312 (with-current-buffer (pop eval-buffers
)
316 (defun debugger-setup-buffer (args)
317 "Initialize the `*Backtrace*' buffer for entry to the debugger.
318 That buffer should be current already."
319 (setq buffer-read-only nil
)
321 (set-buffer-multibyte t
) ;Why was it nil ? -stef
322 (setq buffer-undo-list t
)
323 (insert "Debugger entered")
324 (let ((frames (nthcdr
325 ;; Remove debug--implement-debug-on-entry and the
326 ;; advice's `apply' frame.
327 (if (eq (car args
) 'debug
) 3 1)
328 (backtrace-frames 'debug
)))
329 (print-escape-newlines t
)
330 (print-escape-control-characters t
)
335 ;; lambda is for debug-on-call when a function call is next.
336 ;; debug is for debug-on-entry function called.
338 (insert "--entering a function:\n")
339 (setq pos
(1- (point))))
340 ;; Exiting a function.
342 (insert "--returning value: ")
344 (setq debugger-value
(nth 1 args
))
345 (debugger--print debugger-value
(current-buffer))
346 (setf (cl-getf (nth 3 (car frames
)) :debug-on-exit
) nil
)
348 ;; Watchpoint triggered.
349 ((and `watchpoint
(let `(,symbol
,newval .
,details
) (cdr args
)))
353 (`(makunbound nil
) (format "making %s void" symbol
))
354 (`(makunbound ,buffer
) (format "killing local value of %s in buffer %s"
356 (`(defvaralias ,_
) (format "aliasing %s to %s" symbol newval
))
357 (`(let ,_
) (format "let-binding %s to %S" symbol newval
))
358 (`(unlet ,_
) (format "ending let-binding of %s" symbol
))
359 (`(set nil
) (format "setting %s to %S" symbol newval
))
360 (`(set ,buffer
) (format "setting %s in buffer %s to %S"
361 symbol buffer newval
))
362 (_ (error "unrecognized watchpoint triggered %S" (cdr args
))))
366 ;; Debugger entered for an error.
368 (insert "--Lisp error: ")
370 (debugger--print (nth 1 args
) (current-buffer))
372 ;; debug-on-call, when the next thing is an eval.
374 (insert "--beginning evaluation of function call form:\n")
375 (setq pos
(1- (point))))
376 ;; User calls debug directly.
381 (if (eq (car args
) 'nil
)
385 (debugger-insert-backtrace frames t
)
386 ;; Place point on "stack frame 0" (bug#15101).
390 (defun debugger-step-through ()
391 "Proceed, stepping through subexpressions of this expression.
392 Enter another debugger on next entry to eval, apply or funcall."
394 (setq debugger-step-after-exit t
)
395 (setq debugger-jumping-flag t
)
396 (setq debugger-will-be-back t
)
397 (add-hook 'post-command-hook
'debugger-reenable
)
398 (message "Proceeding, will debug on next eval or call.")
399 (exit-recursive-edit))
401 (defun debugger-continue ()
402 "Continue, evaluating this expression without stopping."
404 (unless debugger-may-continue
405 (error "Cannot continue"))
406 (message "Continuing.")
408 ;; Check to see if we've flagged some frame for debug-on-exit, in which
409 ;; case we'll probably come back to the debugger soon.
410 (goto-char (point-min))
411 (if (re-search-forward "^\\* " nil t
)
412 (setq debugger-will-be-back t
)))
413 (exit-recursive-edit))
415 (defun debugger-return-value (val)
416 "Continue, specifying value to return.
417 This is only useful when the value returned from the debugger
418 will be used, such as in a debug on exit from a frame."
419 (interactive "XReturn value (evaluated): ")
420 (when (memq (car debugger-args
) '(t lambda error debug
))
421 (error "Cannot return a value %s"
422 (if (eq (car debugger-args
) 'error
)
423 "from an error" "at function entrance")))
424 (setq debugger-value val
)
425 (princ "Returning " t
)
426 (debugger--print debugger-value
)
428 ;; Check to see if we've flagged some frame for debug-on-exit, in which
429 ;; case we'll probably come back to the debugger soon.
430 (goto-char (point-min))
431 (if (re-search-forward "^\\* " nil t
)
432 (setq debugger-will-be-back t
)))
433 (exit-recursive-edit))
435 (defun debugger-jump ()
436 "Continue to exit from this frame, with all debug-on-entry suspended."
439 (setq debugger-jumping-flag t
)
440 (add-hook 'post-command-hook
'debugger-reenable
)
441 (message "Continuing through this frame")
442 (setq debugger-will-be-back t
)
443 (exit-recursive-edit))
445 (defun debugger-reenable ()
446 "Turn all debug-on-entry functions back on.
447 This function is put on `post-command-hook' by `debugger-jump' and
448 removes itself from that hook."
449 (setq debugger-jumping-flag nil
)
450 (remove-hook 'post-command-hook
'debugger-reenable
))
452 (defun debugger-frame-number (&optional skip-base
)
453 "Return number of frames in backtrace before the one point points at."
456 (if (looking-at " *;;;\\|[a-z]")
457 (error "This line is not a function call"))
458 (let ((opoint (point))
461 (while (not (eq (cadr (backtrace-frame count
)) 'debug
))
462 (setq count
(1+ count
)))
463 ;; Skip debug--implement-debug-on-entry frame.
464 (when (eq 'debug--implement-debug-on-entry
465 (cadr (backtrace-frame (1+ count
))))
466 (setq count
(+ 2 count
))))
467 (goto-char (point-min))
468 (when (looking-at "Debugger entered--\\(Lisp error\\|returning value\\):")
469 (goto-char (match-end 0))
474 (cond ((debugger--locals-visible-p)
475 (goto-char (next-single-char-property-change
476 (point) 'locals-visible
)))
477 ((= (following-char) ?\
()
483 (if (looking-at " *;;;")
485 (setq count
(1+ count
)))
488 (defun debugger-frame ()
489 "Request entry to debugger when this frame exits.
490 Applies to the frame whose line point is on in the backtrace."
492 (backtrace-debug (debugger-frame-number) t
)
494 (if (= (following-char) ?
)
495 (let ((inhibit-read-only t
))
500 (defun debugger-frame-clear ()
501 "Do not enter debugger when this frame exits.
502 Applies to the frame whose line point is on in the backtrace."
504 (backtrace-debug (debugger-frame-number) nil
)
506 (if (= (following-char) ?
*)
507 (let ((inhibit-read-only t
))
512 (defmacro debugger-env-macro
(&rest body
)
513 "Run BODY in original environment."
516 (set-match-data debugger-outer-match-data
)
519 (setq debugger-outer-match-data
(match-data)))))
521 (defun debugger--backtrace-base ()
522 "Return the function name that marks the top of the backtrace.
523 See `backtrace-frame'."
524 (cond ((eq 'debug--implement-debug-on-entry
525 (cadr (backtrace-frame 1 'debug
)))
526 'debug--implement-debug-on-entry
)
529 (defun debugger-eval-expression (exp &optional nframe
)
530 "Eval an expression, in an environment like that outside the debugger.
531 The environment used is the one when entering the activation frame at point."
533 (list (read--expression "Eval in stack frame: ")))
534 (let ((nframe (or nframe
535 (condition-case nil
(1+ (debugger-frame-number 'skip-base
))
536 (error 0)))) ;; If on first line.
537 (base (debugger--backtrace-base)))
539 (let ((val (backtrace-eval exp nframe base
)))
541 (debugger--print val t
)
542 (let ((str (eval-expression-print-format val
)))
543 (if str
(princ str t
))))))))
545 (defun debugger--locals-visible-p ()
546 "Are the local variables of the current stack frame visible?"
549 (get-text-property (point) 'locals-visible
)))
551 (defun debugger--insert-locals (locals)
552 "Insert the local variables LOCALS at point."
554 (insert "\n [no locals]"))
556 (let ((print-escape-newlines t
))
558 (let ((symbol (car s
+v
))
561 (prin1 symbol
(current-buffer))
563 (debugger--print value
(current-buffer))))))))
565 (defun debugger--show-locals ()
566 "For the frame at point, insert locals and add text properties."
567 (let* ((nframe (1+ (debugger-frame-number 'skip-base
)))
568 (base (debugger--backtrace-base))
569 (locals (backtrace--locals nframe base
))
570 (inhibit-read-only t
))
576 (debugger--insert-locals locals
)
577 (add-text-properties start
(point) '(locals-visible t
))))))
579 (defun debugger--hide-locals ()
580 "Delete local variables and remove the text property."
581 (let* ((col (current-column))
584 (next-single-char-property-change (point) 'locals-visible
)))
585 (start (previous-single-char-property-change end
'locals-visible
))
586 (inhibit-read-only t
))
587 (remove-text-properties start end
'(locals-visible))
590 (delete-region (point) end
)
591 (move-to-column col
)))
593 (defun debugger-toggle-locals ()
594 "Show or hide local variables of the current stack frame."
596 (cond ((debugger--locals-visible-p)
597 (debugger--hide-locals))
599 (debugger--show-locals))))
602 (defvar debugger-mode-map
603 (let ((map (make-keymap))
604 (menu-map (make-sparse-keymap)))
605 (set-keymap-parent map button-buffer-map
)
606 (suppress-keymap map
)
607 (define-key map
"-" 'negative-argument
)
608 (define-key map
"b" 'debugger-frame
)
609 (define-key map
"c" 'debugger-continue
)
610 (define-key map
"j" 'debugger-jump
)
611 (define-key map
"r" 'debugger-return-value
)
612 (define-key map
"u" 'debugger-frame-clear
)
613 (define-key map
"d" 'debugger-step-through
)
614 (define-key map
"l" 'debugger-list-functions
)
615 (define-key map
"h" 'describe-mode
)
616 (define-key map
"q" 'top-level
)
617 (define-key map
"e" 'debugger-eval-expression
)
618 (define-key map
"v" 'debugger-toggle-locals
) ; "v" is for "variables".
619 (define-key map
" " 'next-line
)
620 (define-key map
"R" 'debugger-record-expression
)
621 (define-key map
"\C-m" 'debug-help-follow
)
622 (define-key map
[mouse-2
] 'push-button
)
623 (define-key map
[menu-bar debugger
] (cons "Debugger" menu-map
))
624 (define-key menu-map
[deb-top
]
625 '(menu-item "Quit" top-level
626 :help
"Quit debugging and return to top level"))
627 (define-key menu-map
[deb-s0
] '("--"))
628 (define-key menu-map
[deb-descr
]
629 '(menu-item "Describe Debugger Mode" describe-mode
630 :help
"Display documentation for debugger-mode"))
631 (define-key menu-map
[deb-hfol
]
632 '(menu-item "Help Follow" debug-help-follow
633 :help
"Follow cross-reference"))
634 (define-key menu-map
[deb-nxt
]
635 '(menu-item "Next Line" next-line
636 :help
"Move cursor down"))
637 (define-key menu-map
[deb-s1
] '("--"))
638 (define-key menu-map
[deb-lfunc
]
639 '(menu-item "List debug on entry functions" debugger-list-functions
640 :help
"Display a list of all the functions now set to debug on entry"))
641 (define-key menu-map
[deb-fclear
]
642 '(menu-item "Cancel debug frame" debugger-frame-clear
643 :help
"Do not enter debugger when this frame exits"))
644 (define-key menu-map
[deb-frame
]
645 '(menu-item "Debug frame" debugger-frame
646 :help
"Request entry to debugger when this frame exits"))
647 (define-key menu-map
[deb-s2
] '("--"))
648 (define-key menu-map
[deb-ret
]
649 '(menu-item "Return value..." debugger-return-value
650 :help
"Continue, specifying value to return."))
651 (define-key menu-map
[deb-rec
]
652 '(menu-item "Display and Record Expression" debugger-record-expression
653 :help
"Display a variable's value and record it in `*Backtrace-record*' buffer"))
654 (define-key menu-map
[deb-eval
]
655 '(menu-item "Eval Expression..." debugger-eval-expression
656 :help
"Eval an expression, in an environment like that outside the debugger"))
657 (define-key menu-map
[deb-jump
]
658 '(menu-item "Jump" debugger-jump
659 :help
"Continue to exit from this frame, with all debug-on-entry suspended"))
660 (define-key menu-map
[deb-cont
]
661 '(menu-item "Continue" debugger-continue
662 :help
"Continue, evaluating this expression without stopping"))
663 (define-key menu-map
[deb-step
]
664 '(menu-item "Step through" debugger-step-through
665 :help
"Proceed, stepping through subexpressions of this expression"))
668 (put 'debugger-mode
'mode-class
'special
)
670 (define-derived-mode debugger-mode fundamental-mode
"Debugger"
671 "Mode for backtrace buffers, selected in debugger.
672 \\<debugger-mode-map>
673 A line starts with `*' if exiting that frame will call the debugger.
674 Type \\[debugger-frame] or \\[debugger-frame-clear] to set or remove the `*'.
676 When in debugger due to frame being exited,
677 use the \\[debugger-return-value] command to override the value
678 being returned from that frame.
680 Use \\[debug-on-entry] and \\[cancel-debug-on-entry] to control
681 which functions will enter the debugger when called.
683 Complete list of commands:
684 \\{debugger-mode-map}"
685 (setq truncate-lines t
)
686 (set-syntax-table emacs-lisp-mode-syntax-table
)
687 (add-hook 'kill-buffer-hook
688 (lambda () (if (> (recursion-depth) 0) (top-level)))
690 (use-local-map debugger-mode-map
))
692 (defcustom debugger-record-buffer
"*Debugger-record*"
693 "Buffer name for expression values, for \\[debugger-record-expression]."
698 (defun debugger-record-expression (exp)
699 "Display a variable's value and record it in `*Backtrace-record*' buffer."
701 (list (read--expression "Record Eval: ")))
702 (let* ((buffer (get-buffer-create debugger-record-buffer
))
703 (standard-output buffer
))
704 (princ (format "Debugger Eval (%s): " exp
))
705 (princ (debugger-eval-expression exp
))
708 (with-current-buffer (get-buffer debugger-record-buffer
)
710 (buffer-substring (line-beginning-position 0)
711 (line-end-position 0)))))
713 (defun debug-help-follow (&optional pos
)
714 "Follow cross-reference at POS, defaulting to point.
716 For the cross-reference format, see `help-make-xrefs'."
718 ;; Ideally we'd just do (call-interactively 'help-follow) except that this
719 ;; assumes we're already in a *Help* buffer and reuses it, so it ends up
720 ;; incorrectly "reusing" the *Backtrace* buffer to show the help info.
723 (unless (push-button pos
)
724 ;; check if the symbol under point is a function or variable
728 (goto-char pos
) (skip-syntax-backward "w_")
729 (buffer-substring (point)
730 (progn (skip-syntax-forward "w_")
732 (when (or (boundp sym
) (fboundp sym
) (facep sym
))
733 (describe-symbol sym
)))))
735 ;; When you change this, you may also need to change the number of
736 ;; frames that the debugger skips.
737 (defun debug--implement-debug-on-entry (&rest _ignore
)
738 "Conditionally call the debugger.
739 A call to this function is inserted by `debug-on-entry' to cause
740 functions to break on entry."
741 (if (or inhibit-debug-on-entry debugger-jumping-flag
)
743 (let ((inhibit-debug-on-entry t
))
744 (funcall debugger
'debug
))))
747 (defun debug-on-entry (function)
748 "Request FUNCTION to invoke debugger each time it is called.
750 When called interactively, prompt for FUNCTION in the minibuffer.
752 This works by modifying the definition of FUNCTION. If you tell the
753 debugger to continue, FUNCTION's execution proceeds. If FUNCTION is a
754 normal function or a macro written in Lisp, you can also step through
755 its execution. FUNCTION can also be a primitive that is not a special
756 form, in which case stepping is not possible. Break-on-entry for
757 primitive functions only works when that function is called from Lisp.
759 Use \\[cancel-debug-on-entry] to cancel the effect of this command.
760 Redefining FUNCTION also cancels it."
762 (let ((fn (function-called-at-point)) val
)
763 (when (special-form-p fn
)
765 (setq val
(completing-read
767 (format "Debug on entry to function (default %s): " fn
)
768 "Debug on entry to function: ")
771 (and (fboundp symbol
)
772 (not (special-form-p symbol
))))
773 t nil nil
(symbol-name fn
)))
774 (list (if (equal val
"") fn
(intern val
)))))
775 (advice-add function
:before
#'debug--implement-debug-on-entry
779 (defun debug--function-list ()
780 "List of functions currently set for debug on entry."
784 (when (advice-member-p #'debug--implement-debug-on-entry s
)
789 (defun cancel-debug-on-entry (&optional function
)
790 "Undo effect of \\[debug-on-entry] on FUNCTION.
791 If FUNCTION is nil, cancel debug-on-entry for all functions.
792 When called interactively, prompt for FUNCTION in the minibuffer.
793 To specify a nil argument interactively, exit with an empty minibuffer."
797 "Cancel debug on entry to function (default all functions): "
798 (mapcar #'symbol-name
(debug--function-list)) nil t
)))
800 (unless (string= name
"")
804 (advice-remove function
#'debug--implement-debug-on-entry
)
806 (message "Canceling debug-on-entry for all functions")
807 (mapcar #'cancel-debug-on-entry
(debug--function-list))))
809 (defun debugger-list-functions ()
810 "Display a list of all the functions now set to debug on entry."
813 (help-setup-xref '(debugger-list-functions)
814 (called-interactively-p 'interactive
))
815 (with-output-to-temp-buffer (help-buffer)
816 (with-current-buffer standard-output
817 (let ((funs (debug--function-list)))
819 (princ "No debug-on-entry functions now\n")
820 (princ "Functions set to debug on entry:\n\n")
822 (make-text-button (point) (progn (prin1 fun
) (point))
824 'help-args
(list fun
))
826 ;; Now that debug--function-list uses advice-member-p, its
827 ;; output should be reliable (except for bugs and the exceptional
828 ;; case where some other advice ends up overriding ours).
830 ;;(princ "Note: if you have redefined a function, then it may no longer\n")
831 ;;(princ "be set to debug on entry, even if it is in the list.")
834 (defun debug--implement-debug-watch (symbol newval op where
)
835 "Conditionally call the debugger.
836 This function is called when SYMBOL's value is modified."
837 (if (or inhibit-debug-on-entry debugger-jumping-flag
)
839 (let ((inhibit-debug-on-entry t
))
840 (funcall debugger
'watchpoint symbol newval op where
))))
843 (defun debug-on-variable-change (variable)
844 "Trigger a debugger invocation when VARIABLE is changed.
846 When called interactively, prompt for VARIABLE in the minibuffer.
848 This works by calling `add-variable-watch' on VARIABLE. If you
849 quit from the debugger, this will abort the change (unless the
850 change is caused by the termination of a let-binding).
852 The watchpoint may be circumvented by C code that changes the
853 variable directly (i.e., not via `set'). Changing the value of
854 the variable (e.g., `setcar' on a list variable) will not trigger
857 Use \\[cancel-debug-on-variable-change] to cancel the effect of
858 this command. Uninterning VARIABLE or making it an alias of
859 another symbol also cancels it."
861 (let* ((var-at-point (variable-at-point))
862 (var (and (symbolp var-at-point
) var-at-point
))
863 (val (completing-read
864 (concat "Debug when setting variable"
865 (if var
(format " (default %s): " var
) ": "))
867 t nil nil
(and var
(symbol-name var
)))))
868 (list (if (equal val
"") var
(intern val
)))))
869 (add-variable-watcher variable
#'debug--implement-debug-watch
))
872 (defalias 'debug-watch
#'debug-on-variable-change
)
875 (defun debug--variable-list ()
876 "List of variables currently set for debug on set."
880 (when (memq #'debug--implement-debug-watch
886 (defun cancel-debug-on-variable-change (&optional variable
)
887 "Undo effect of \\[debug-on-variable-change] on VARIABLE.
888 If VARIABLE is nil, cancel debug-on-variable-change for all variables.
889 When called interactively, prompt for VARIABLE in the minibuffer.
890 To specify a nil argument interactively, exit with an empty minibuffer."
894 "Cancel debug on set for variable (default all variables): "
895 (mapcar #'symbol-name
(debug--variable-list)) nil t
)))
897 (unless (string= name
"")
900 (remove-variable-watcher variable
#'debug--implement-debug-watch
)
901 (message "Canceling debug-watch for all variables")
902 (mapc #'cancel-debug-watch
(debug--variable-list))))
905 (defalias 'cancel-debug-watch
#'cancel-debug-on-variable-change
)
909 ;;; debug.el ends here