Add support for lexical variables to the debugger's `e' command.
[bpt/emacs.git] / lisp / emacs-lisp / debug.el
1 ;;; debug.el --- debuggers and related commands for Emacs -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 1985-1986, 1994, 2001-2013 Free Software Foundation,
4 ;; Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: lisp, tools, maint
8
9 ;; This file is part of GNU Emacs.
10
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.
15
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.
20
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/>.
23
24 ;;; Commentary:
25
26 ;; This is a major mode documented in the Emacs Lisp manual.
27
28 ;;; Code:
29
30 (require 'button)
31
32 (defgroup debugger nil
33 "Debuggers and related commands for Emacs."
34 :prefix "debugger-"
35 :group 'debug)
36
37 (defcustom debugger-mode-hook nil
38 "Hooks run when `debugger-mode' is turned on."
39 :type 'hook
40 :group 'debugger
41 :version "20.3")
42
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."
48 :type 'integer
49 :group 'debugger
50 :version "21.1")
51
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.
56
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.
60
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.
66
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.
73
74 `kill' means to kill the debugger buffer.
75
76 The value used here is passed to `quit-restore-window'."
77 :type '(choice
78 (const :tag "Keep alive" nil)
79 (const :tag "Append" append)
80 (const :tag "Bury" bury)
81 (const :tag "Kill" kill))
82 :group 'debugger
83 :version "24.3")
84
85 (defvar debugger-step-after-exit nil
86 "Non-nil means \"single-step\" after the debugger exits.")
87
88 (defvar debugger-value nil
89 "This is the value for the debugger to return, when it returns.")
90
91 (defvar debugger-old-buffer nil
92 "This is the buffer that was current when the debugger was entered.")
93
94 (defvar debugger-previous-window nil
95 "This is the window last showing the debugger buffer.")
96
97 (defvar debugger-previous-window-height nil
98 "The last recorded height of `debugger-previous-window'.")
99
100 (defvar debugger-previous-backtrace nil
101 "The contents of the previous backtrace (including text properties).
102 This is to optimize `debugger-make-xrefs'.")
103
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.")
107
108 (defvar inhibit-debug-on-entry nil
109 "Non-nil means that debug-on-entry is disabled.")
110
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.")
115
116 (defvar inhibit-trace) ;Not yet implemented.
117
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)
121 where CAUSE can be:
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'.")
127
128 ;;;###autoload
129 (setq debugger 'debug)
130 ;;;###autoload
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
134 of the evaluator.
135
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."
139 (interactive)
140 (if inhibit-redisplay
141 ;; Don't really try to enter debugger within an eval from redisplay.
142 debugger-value
143 (unless noninteractive
144 (message "Entering debugger..."))
145 (let (debugger-value
146 (debugger-previous-state
147 (if (get-buffer "*Backtrace*")
148 (with-current-buffer (get-buffer "*Backtrace*")
149 (list major-mode (buffer-string)))))
150 (debugger-args args)
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
167 (inhibit-trace t)
168 (inhibit-debug-on-entry t)
169 unread-command-events
170 unread-post-input-method-events
171 last-input-event last-command-event last-nonmenu-event
172 last-event-frame
173 overriding-local-map
174 load-read-function
175 ;; If we are inside a minibuffer, allow nesting
176 ;; so that we don't get an error from the `e' command.
177 (enable-recursive-minibuffers
178 (or enable-recursive-minibuffers (> (minibuffer-depth) 0)))
179 (standard-input t) (standard-output t)
180 inhibit-redisplay
181 (cursor-in-echo-area nil)
182 (window-configuration (current-window-configuration)))
183 (unwind-protect
184 (save-excursion
185 (when (eq (car debugger-args) 'debug)
186 ;; Skip the frames for backtrace-debug, byte-code,
187 ;; debug--implement-debug-on-entry and the advice's `apply'.
188 (backtrace-debug 4 t)
189 ;; Place an extra debug-on-exit for macro's.
190 (when (eq 'lambda (car-safe (cadr (backtrace-frame 4))))
191 (backtrace-debug 5 t)))
192 (pop-to-buffer
193 debugger-buffer
194 `((display-buffer-reuse-window
195 display-buffer-in-previous-window)
196 . (,(when debugger-previous-window
197 `(previous-window . ,debugger-previous-window)))))
198 (setq debugger-window (selected-window))
199 (if (eq debugger-previous-window debugger-window)
200 (when debugger-jumping-flag
201 ;; Try to restore previous height of debugger
202 ;; window.
203 (condition-case nil
204 (window-resize
205 debugger-window
206 (- debugger-previous-window-height
207 (window-total-size debugger-window)))
208 (error nil)))
209 (setq debugger-previous-window debugger-window))
210 (debugger-mode)
211 (debugger-setup-buffer debugger-args)
212 (when noninteractive
213 ;; If the backtrace is long, save the beginning
214 ;; and the end, but discard the middle.
215 (when (> (count-lines (point-min) (point-max))
216 debugger-batch-max-lines)
217 (goto-char (point-min))
218 (forward-line (/ 2 debugger-batch-max-lines))
219 (let ((middlestart (point)))
220 (goto-char (point-max))
221 (forward-line (- (/ 2 debugger-batch-max-lines)
222 debugger-batch-max-lines))
223 (delete-region middlestart (point)))
224 (insert "...\n"))
225 (goto-char (point-min))
226 (message "%s" (buffer-string))
227 (kill-emacs -1))
228 (message "")
229 (let ((standard-output nil)
230 (buffer-read-only t))
231 (message "")
232 ;; Make sure we unbind buffer-read-only in the right buffer.
233 (save-excursion
234 (recursive-edit))))
235 (when (and (window-live-p debugger-window)
236 (eq (window-buffer debugger-window) debugger-buffer))
237 ;; Record height of debugger window.
238 (setq debugger-previous-window-height
239 (window-total-size debugger-window)))
240 (if debugger-will-be-back
241 ;; Restore previous window configuration (Bug#12623).
242 (set-window-configuration window-configuration)
243 (when (and (window-live-p debugger-window)
244 (eq (window-buffer debugger-window) debugger-buffer))
245 (progn
246 ;; Unshow debugger-buffer.
247 (quit-restore-window debugger-window debugger-bury-or-kill)
248 ;; Restore current buffer (Bug#12502).
249 (set-buffer debugger-old-buffer))))
250 ;; Restore previous state of debugger-buffer in case we were
251 ;; in a recursive invocation of the debugger, otherwise just
252 ;; erase the buffer and put it into fundamental mode.
253 (when (buffer-live-p debugger-buffer)
254 (with-current-buffer debugger-buffer
255 (let ((inhibit-read-only t))
256 (erase-buffer)
257 (if (null debugger-previous-state)
258 (fundamental-mode)
259 (insert (nth 1 debugger-previous-state))
260 (funcall (nth 0 debugger-previous-state))))))
261 (with-timeout-unsuspend debugger-with-timeout-suspend)
262 (set-match-data debugger-outer-match-data)))
263 (setq debug-on-next-call debugger-step-after-exit)
264 debugger-value)))
265 \f
266 (defun debugger-setup-buffer (args)
267 "Initialize the `*Backtrace*' buffer for entry to the debugger.
268 That buffer should be current already."
269 (setq buffer-read-only nil)
270 (erase-buffer)
271 (set-buffer-multibyte t) ;Why was it nil ? -stef
272 (setq buffer-undo-list t)
273 (let ((standard-output (current-buffer))
274 (print-escape-newlines t)
275 (print-level 8)
276 (print-length 50))
277 (backtrace))
278 (goto-char (point-min))
279 (delete-region (point)
280 (progn
281 (search-forward "\n debug(")
282 (forward-line (if (eq (car args) 'debug)
283 ;; Remove debug--implement-debug-on-entry
284 ;; and the advice's `apply' frame.
285 3
286 1))
287 (point)))
288 (insert "Debugger entered")
289 ;; lambda is for debug-on-call when a function call is next.
290 ;; debug is for debug-on-entry function called.
291 (pcase (car args)
292 ((or `lambda `debug)
293 (insert "--entering a function:\n"))
294 ;; Exiting a function.
295 (`exit
296 (insert "--returning value: ")
297 (setq debugger-value (nth 1 args))
298 (prin1 debugger-value (current-buffer))
299 (insert ?\n)
300 (delete-char 1)
301 (insert ? )
302 (beginning-of-line))
303 ;; Debugger entered for an error.
304 (`error
305 (insert "--Lisp error: ")
306 (prin1 (nth 1 args) (current-buffer))
307 (insert ?\n))
308 ;; debug-on-call, when the next thing is an eval.
309 (`t
310 (insert "--beginning evaluation of function call form:\n"))
311 ;; User calls debug directly.
312 (_
313 (insert ": ")
314 (prin1 (if (eq (car args) 'nil)
315 (cdr args) args)
316 (current-buffer))
317 (insert ?\n)))
318 ;; After any frame that uses eval-buffer,
319 ;; insert a line that states the buffer position it's reading at.
320 (save-excursion
321 (let ((tem eval-buffer-list))
322 (while (and tem
323 (re-search-forward "^ eval-\\(buffer\\|region\\)(" nil t))
324 (end-of-line)
325 (insert (format " ; Reading at buffer position %d"
326 ;; This will get the wrong result
327 ;; if there are two nested eval-region calls
328 ;; for the same buffer. That's not a very useful case.
329 (with-current-buffer (car tem)
330 (point))))
331 (pop tem))))
332 (debugger-make-xrefs))
333
334 (defun debugger-make-xrefs (&optional buffer)
335 "Attach cross-references to function names in the `*Backtrace*' buffer."
336 (interactive "b")
337 (with-current-buffer (or buffer (current-buffer))
338 (save-excursion
339 (setq buffer (current-buffer))
340 (let ((inhibit-read-only t)
341 (old-end (point-min)) (new-end (point-min)))
342 ;; If we saved an old backtrace, find the common part
343 ;; between the new and the old.
344 ;; Compare line by line, starting from the end,
345 ;; because that's the part that is likely to be unchanged.
346 (if debugger-previous-backtrace
347 (let (old-start new-start (all-match t))
348 (goto-char (point-max))
349 (with-temp-buffer
350 (insert debugger-previous-backtrace)
351 (while (and all-match (not (bobp)))
352 (setq old-end (point))
353 (forward-line -1)
354 (setq old-start (point))
355 (with-current-buffer buffer
356 (setq new-end (point))
357 (forward-line -1)
358 (setq new-start (point)))
359 (if (not (zerop
360 (let ((case-fold-search nil))
361 (compare-buffer-substrings
362 (current-buffer) old-start old-end
363 buffer new-start new-end))))
364 (setq all-match nil))))
365 ;; Now new-end is the position of the start of the
366 ;; unchanged part in the current buffer, and old-end is
367 ;; the position of that same text in the saved old
368 ;; backtrace. But we must subtract (point-min) since strings are
369 ;; indexed in origin 0.
370
371 ;; Replace the unchanged part of the backtrace
372 ;; with the text from debugger-previous-backtrace,
373 ;; since that already has the proper xrefs.
374 ;; With this optimization, we only need to scan
375 ;; the changed part of the backtrace.
376 (delete-region new-end (point-max))
377 (goto-char (point-max))
378 (insert (substring debugger-previous-backtrace
379 (- old-end (point-min))))
380 ;; Make the unchanged part of the backtrace inaccessible
381 ;; so it won't be scanned.
382 (narrow-to-region (point-min) new-end)))
383
384 ;; Scan the new part of the backtrace, inserting xrefs.
385 (goto-char (point-min))
386 (while (progn
387 (goto-char (+ (point) 2))
388 (skip-syntax-forward "^w_")
389 (not (eobp)))
390 (let* ((beg (point))
391 (end (progn (skip-syntax-forward "w_") (point)))
392 (sym (intern-soft (buffer-substring-no-properties
393 beg end)))
394 (file (and sym (symbol-file sym 'defun))))
395 (when file
396 (goto-char beg)
397 ;; help-xref-button needs to operate on something matched
398 ;; by a regexp, so set that up for it.
399 (re-search-forward "\\(\\sw\\|\\s_\\)+")
400 (help-xref-button 0 'help-function-def sym file)))
401 (forward-line 1))
402 (widen))
403 (setq debugger-previous-backtrace (buffer-string)))))
404 \f
405 (defun debugger-step-through ()
406 "Proceed, stepping through subexpressions of this expression.
407 Enter another debugger on next entry to eval, apply or funcall."
408 (interactive)
409 (setq debugger-step-after-exit t)
410 (setq debugger-jumping-flag t)
411 (setq debugger-will-be-back t)
412 (add-hook 'post-command-hook 'debugger-reenable)
413 (message "Proceeding, will debug on next eval or call.")
414 (exit-recursive-edit))
415
416 (defun debugger-continue ()
417 "Continue, evaluating this expression without stopping."
418 (interactive)
419 (unless debugger-may-continue
420 (error "Cannot continue"))
421 (message "Continuing.")
422 (save-excursion
423 ;; Check to see if we've flagged some frame for debug-on-exit, in which
424 ;; case we'll probably come back to the debugger soon.
425 (goto-char (point-min))
426 (if (re-search-forward "^\\* " nil t)
427 (setq debugger-will-be-back t)))
428 (exit-recursive-edit))
429
430 (defun debugger-return-value (val)
431 "Continue, specifying value to return.
432 This is only useful when the value returned from the debugger
433 will be used, such as in a debug on exit from a frame."
434 (interactive "XReturn value (evaluated): ")
435 (when (memq (car debugger-args) '(t lambda error debug))
436 (error "Cannot return a value %s"
437 (if (eq (car debugger-args) 'error)
438 "from an error" "at function entrance")))
439 (setq debugger-value val)
440 (princ "Returning " t)
441 (prin1 debugger-value)
442 (save-excursion
443 ;; Check to see if we've flagged some frame for debug-on-exit, in which
444 ;; case we'll probably come back to the debugger soon.
445 (goto-char (point-min))
446 (if (re-search-forward "^\\* " nil t)
447 (setq debugger-will-be-back t)))
448 (exit-recursive-edit))
449
450 (defun debugger-jump ()
451 "Continue to exit from this frame, with all debug-on-entry suspended."
452 (interactive)
453 (debugger-frame)
454 (setq debugger-jumping-flag t)
455 (add-hook 'post-command-hook 'debugger-reenable)
456 (message "Continuing through this frame")
457 (setq debugger-will-be-back t)
458 (exit-recursive-edit))
459
460 (defun debugger-reenable ()
461 "Turn all debug-on-entry functions back on.
462 This function is put on `post-command-hook' by `debugger-jump' and
463 removes itself from that hook."
464 (setq debugger-jumping-flag nil)
465 (remove-hook 'post-command-hook 'debugger-reenable))
466
467 (defun debugger-frame-number (&optional skip-base)
468 "Return number of frames in backtrace before the one point points at."
469 (save-excursion
470 (beginning-of-line)
471 (if (looking-at " *;;;\\|[a-z]")
472 (error "This line is not a function call"))
473 (let ((opoint (point))
474 (count 0))
475 (unless skip-base
476 (while (not (eq (cadr (backtrace-frame count)) 'debug))
477 (setq count (1+ count)))
478 ;; Skip debug--implement-debug-on-entry frame.
479 (when (eq 'debug--implement-debug-on-entry
480 (cadr (backtrace-frame (1+ count))))
481 (setq count (+ 2 count))))
482 (goto-char (point-min))
483 (when (looking-at "Debugger entered--\\(Lisp error\\|returning value\\):")
484 (goto-char (match-end 0))
485 (forward-sexp 1))
486 (forward-line 1)
487 (while (progn
488 (forward-char 2)
489 (if (= (following-char) ?\()
490 (forward-sexp 1)
491 (forward-sexp 2))
492 (forward-line 1)
493 (<= (point) opoint))
494 (if (looking-at " *;;;")
495 (forward-line 1))
496 (setq count (1+ count)))
497 count)))
498
499 (defun debugger-frame ()
500 "Request entry to debugger when this frame exits.
501 Applies to the frame whose line point is on in the backtrace."
502 (interactive)
503 (backtrace-debug (debugger-frame-number) t)
504 (beginning-of-line)
505 (if (= (following-char) ? )
506 (let ((inhibit-read-only t))
507 (delete-char 1)
508 (insert ?*)))
509 (beginning-of-line))
510
511 (defun debugger-frame-clear ()
512 "Do not enter debugger when this frame exits.
513 Applies to the frame whose line point is on in the backtrace."
514 (interactive)
515 (backtrace-debug (debugger-frame-number) nil)
516 (beginning-of-line)
517 (if (= (following-char) ?*)
518 (let ((inhibit-read-only t))
519 (delete-char 1)
520 (insert ? )))
521 (beginning-of-line))
522
523 (defmacro debugger-env-macro (&rest body)
524 "Run BODY in original environment."
525 (declare (indent 0))
526 `(save-excursion
527 (if (null (buffer-live-p debugger-old-buffer))
528 ;; old buffer deleted
529 (setq debugger-old-buffer (current-buffer)))
530 (set-buffer debugger-old-buffer)
531 (set-match-data debugger-outer-match-data)
532 (prog1
533 (progn ,@body)
534 (setq debugger-outer-match-data (match-data)))))
535
536 (defun debugger-eval-expression (exp)
537 "Eval an expression, in an environment like that outside the debugger.
538 The environment used is the one when entering the activation frame at point."
539 (interactive
540 (list (read-from-minibuffer "Eval: "
541 nil read-expression-map t
542 'read-expression-history)))
543 (let ((nframe (condition-case nil (1+ (debugger-frame-number 'skip-base))
544 (error 0))) ;; If on first line.
545 (base (if (eq 'debug--implement-debug-on-entry
546 (cadr (backtrace-frame 1 'debug)))
547 'debug--implement-debug-on-entry 'debug)))
548 (debugger-env-macro
549 (let ((val (backtrace-eval exp nframe base)))
550 (prog1
551 (prin1 val t)
552 (let ((str (eval-expression-print-format val)))
553 (if str (princ str t))))))))
554 \f
555 (defvar debugger-mode-map
556 (let ((map (make-keymap))
557 (menu-map (make-sparse-keymap)))
558 (set-keymap-parent map button-buffer-map)
559 (suppress-keymap map)
560 (define-key map "-" 'negative-argument)
561 (define-key map "b" 'debugger-frame)
562 (define-key map "c" 'debugger-continue)
563 (define-key map "j" 'debugger-jump)
564 (define-key map "r" 'debugger-return-value)
565 (define-key map "u" 'debugger-frame-clear)
566 (define-key map "d" 'debugger-step-through)
567 (define-key map "l" 'debugger-list-functions)
568 (define-key map "h" 'describe-mode)
569 (define-key map "q" 'top-level)
570 (define-key map "e" 'debugger-eval-expression)
571 (define-key map " " 'next-line)
572 (define-key map "R" 'debugger-record-expression)
573 (define-key map "\C-m" 'debug-help-follow)
574 (define-key map [mouse-2] 'push-button)
575 (define-key map [menu-bar debugger] (cons "Debugger" menu-map))
576 (define-key menu-map [deb-top]
577 '(menu-item "Quit" top-level
578 :help "Quit debugging and return to top level"))
579 (define-key menu-map [deb-s0] '("--"))
580 (define-key menu-map [deb-descr]
581 '(menu-item "Describe Debugger Mode" describe-mode
582 :help "Display documentation for debugger-mode"))
583 (define-key menu-map [deb-hfol]
584 '(menu-item "Help Follow" debug-help-follow
585 :help "Follow cross-reference"))
586 (define-key menu-map [deb-nxt]
587 '(menu-item "Next Line" next-line
588 :help "Move cursor down"))
589 (define-key menu-map [deb-s1] '("--"))
590 (define-key menu-map [deb-lfunc]
591 '(menu-item "List debug on entry functions" debugger-list-functions
592 :help "Display a list of all the functions now set to debug on entry"))
593 (define-key menu-map [deb-fclear]
594 '(menu-item "Cancel debug frame" debugger-frame-clear
595 :help "Do not enter debugger when this frame exits"))
596 (define-key menu-map [deb-frame]
597 '(menu-item "Debug frame" debugger-frame
598 :help "Request entry to debugger when this frame exits"))
599 (define-key menu-map [deb-s2] '("--"))
600 (define-key menu-map [deb-ret]
601 '(menu-item "Return value..." debugger-return-value
602 :help "Continue, specifying value to return."))
603 (define-key menu-map [deb-rec]
604 '(menu-item "Display and Record Expression" debugger-record-expression
605 :help "Display a variable's value and record it in `*Backtrace-record*' buffer"))
606 (define-key menu-map [deb-eval]
607 '(menu-item "Eval Expression..." debugger-eval-expression
608 :help "Eval an expression, in an environment like that outside the debugger"))
609 (define-key menu-map [deb-jump]
610 '(menu-item "Jump" debugger-jump
611 :help "Continue to exit from this frame, with all debug-on-entry suspended"))
612 (define-key menu-map [deb-cont]
613 '(menu-item "Continue" debugger-continue
614 :help "Continue, evaluating this expression without stopping"))
615 (define-key menu-map [deb-step]
616 '(menu-item "Step through" debugger-step-through
617 :help "Proceed, stepping through subexpressions of this expression"))
618 map))
619
620 (put 'debugger-mode 'mode-class 'special)
621
622 (defun debugger-mode ()
623 "Mode for backtrace buffers, selected in debugger.
624 \\<debugger-mode-map>
625 A line starts with `*' if exiting that frame will call the debugger.
626 Type \\[debugger-frame] or \\[debugger-frame-clear] to set or remove the `*'.
627
628 When in debugger due to frame being exited,
629 use the \\[debugger-return-value] command to override the value
630 being returned from that frame.
631
632 Use \\[debug-on-entry] and \\[cancel-debug-on-entry] to control
633 which functions will enter the debugger when called.
634
635 Complete list of commands:
636 \\{debugger-mode-map}"
637 (kill-all-local-variables)
638 (setq major-mode 'debugger-mode)
639 (setq mode-name "Debugger")
640 (setq truncate-lines t)
641 (set-syntax-table emacs-lisp-mode-syntax-table)
642 (use-local-map debugger-mode-map)
643 (run-mode-hooks 'debugger-mode-hook))
644 \f
645 (defcustom debugger-record-buffer "*Debugger-record*"
646 "Buffer name for expression values, for \\[debugger-record-expression]."
647 :type 'string
648 :group 'debugger
649 :version "20.3")
650
651 (defun debugger-record-expression (exp)
652 "Display a variable's value and record it in `*Backtrace-record*' buffer."
653 (interactive
654 (list (read-from-minibuffer
655 "Record Eval: "
656 nil
657 read-expression-map t
658 'read-expression-history)))
659 (let* ((buffer (get-buffer-create debugger-record-buffer))
660 (standard-output buffer))
661 (princ (format "Debugger Eval (%s): " exp))
662 (princ (debugger-eval-expression exp))
663 (terpri))
664
665 (with-current-buffer (get-buffer debugger-record-buffer)
666 (message "%s"
667 (buffer-substring (line-beginning-position 0)
668 (line-end-position 0)))))
669
670 (declare-function help-xref-interned "help-mode" (symbol))
671
672 (defun debug-help-follow (&optional pos)
673 "Follow cross-reference at POS, defaulting to point.
674
675 For the cross-reference format, see `help-make-xrefs'."
676 (interactive "d")
677 (require 'help-mode)
678 ;; Ideally we'd just do (call-interactively 'help-follow) except that this
679 ;; assumes we're already in a *Help* buffer and reuses it, so it ends up
680 ;; incorrectly "reusing" the *Backtrace* buffer to show the help info.
681 (unless pos
682 (setq pos (point)))
683 (unless (push-button pos)
684 ;; check if the symbol under point is a function or variable
685 (let ((sym
686 (intern
687 (save-excursion
688 (goto-char pos) (skip-syntax-backward "w_")
689 (buffer-substring (point)
690 (progn (skip-syntax-forward "w_")
691 (point)))))))
692 (when (or (boundp sym) (fboundp sym) (facep sym))
693 (help-xref-interned sym)))))
694 \f
695 ;; When you change this, you may also need to change the number of
696 ;; frames that the debugger skips.
697 (defun debug--implement-debug-on-entry (&rest _ignore)
698 "Conditionally call the debugger.
699 A call to this function is inserted by `debug-on-entry' to cause
700 functions to break on entry."
701 (if (or inhibit-debug-on-entry debugger-jumping-flag)
702 nil
703 (funcall debugger 'debug)))
704
705 ;;;###autoload
706 (defun debug-on-entry (function)
707 "Request FUNCTION to invoke debugger each time it is called.
708
709 When called interactively, prompt for FUNCTION in the minibuffer.
710
711 This works by modifying the definition of FUNCTION. If you tell the
712 debugger to continue, FUNCTION's execution proceeds. If FUNCTION is a
713 normal function or a macro written in Lisp, you can also step through
714 its execution. FUNCTION can also be a primitive that is not a special
715 form, in which case stepping is not possible. Break-on-entry for
716 primitive functions only works when that function is called from Lisp.
717
718 Use \\[cancel-debug-on-entry] to cancel the effect of this command.
719 Redefining FUNCTION also cancels it."
720 (interactive
721 (let ((fn (function-called-at-point)) val)
722 (when (special-form-p fn)
723 (setq fn nil))
724 (setq val (completing-read
725 (if fn
726 (format "Debug on entry to function (default %s): " fn)
727 "Debug on entry to function: ")
728 obarray
729 #'(lambda (symbol)
730 (and (fboundp symbol)
731 (not (special-form-p symbol))))
732 t nil nil (symbol-name fn)))
733 (list (if (equal val "") fn (intern val)))))
734 (advice-add function :before #'debug--implement-debug-on-entry)
735 function)
736
737 (defun debug--function-list ()
738 "List of functions currently set for debug on entry."
739 (let ((funs '()))
740 (mapatoms
741 (lambda (s)
742 (when (advice-member-p #'debug--implement-debug-on-entry s)
743 (push s funs))))
744 funs))
745
746 ;;;###autoload
747 (defun cancel-debug-on-entry (&optional function)
748 "Undo effect of \\[debug-on-entry] on FUNCTION.
749 If FUNCTION is nil, cancel debug-on-entry for all functions.
750 When called interactively, prompt for FUNCTION in the minibuffer.
751 To specify a nil argument interactively, exit with an empty minibuffer."
752 (interactive
753 (list (let ((name
754 (completing-read
755 "Cancel debug on entry to function (default all functions): "
756 (mapcar #'symbol-name (debug--function-list)) nil t)))
757 (when name
758 (unless (string= name "")
759 (intern name))))))
760 (if function
761 (progn
762 (advice-remove function #'debug--implement-debug-on-entry)
763 function)
764 (message "Cancelling debug-on-entry for all functions")
765 (mapcar #'cancel-debug-on-entry (debug--function-list))))
766
767 (defun debugger-list-functions ()
768 "Display a list of all the functions now set to debug on entry."
769 (interactive)
770 (require 'help-mode)
771 (help-setup-xref '(debugger-list-functions)
772 (called-interactively-p 'interactive))
773 (with-output-to-temp-buffer (help-buffer)
774 (with-current-buffer standard-output
775 (let ((funs (debug--function-list)))
776 (if (null funs)
777 (princ "No debug-on-entry functions now\n")
778 (princ "Functions set to debug on entry:\n\n")
779 (dolist (fun funs)
780 (make-text-button (point) (progn (prin1 fun) (point))
781 'type 'help-function
782 'help-args (list fun))
783 (terpri))
784 (terpri)
785 (princ "Note: if you have redefined a function, then it may no longer\n")
786 (princ "be set to debug on entry, even if it is in the list."))))))
787
788 (provide 'debug)
789
790 ;;; debug.el ends here