]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/debug.el
0e307fae70a0c51ebc5fdba73d2380e64c9150b8
[gnu-emacs] / 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-2015 Free Software Foundation,
4 ;; Inc.
5
6 ;; Maintainer: emacs-devel@gnu.org
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 unread-command-events
169 unread-post-input-method-events
170 last-input-event last-command-event last-nonmenu-event
171 last-event-frame
172 overriding-local-map
173 load-read-function
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)
179 inhibit-redisplay
180 (cursor-in-echo-area nil)
181 (window-configuration (current-window-configuration)))
182 (unwind-protect
183 (save-excursion
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)))
191 (pop-to-buffer
192 debugger-buffer
193 `((display-buffer-reuse-window
194 display-buffer-in-previous-window)
195 . (,(when (and (window-live-p debugger-previous-window)
196 (frame-visible-p
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
203 ;; window.
204 (condition-case nil
205 (window-resize
206 debugger-window
207 (- debugger-previous-window-height
208 (window-total-height debugger-window)))
209 (error nil)))
210 (setq debugger-previous-window debugger-window))
211 (debugger-mode)
212 (debugger-setup-buffer debugger-args)
213 (when noninteractive
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)))
225 (insert "...\n"))
226 (goto-char (point-min))
227 (message "%s" (buffer-string))
228 (kill-emacs -1))
229 (message "")
230 (let ((standard-output nil)
231 (buffer-read-only t))
232 (message "")
233 ;; Make sure we unbind buffer-read-only in the right buffer.
234 (save-excursion
235 (recursive-edit))))
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))
246 (progn
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))
257 (erase-buffer)
258 (if (null debugger-previous-state)
259 (fundamental-mode)
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)
265 debugger-value)))
266 \f
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)
271 (erase-buffer)
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)
276 (print-level 8)
277 (print-length 50))
278 (backtrace))
279 (goto-char (point-min))
280 (delete-region (point)
281 (progn
282 (search-forward "\n debug(")
283 (forward-line (if (eq (car args) 'debug)
284 ;; Remove debug--implement-debug-on-entry
285 ;; and the advice's `apply' frame.
286 3
287 1))
288 (point)))
289 (insert "Debugger entered")
290 ;; lambda is for debug-on-call when a function call is next.
291 ;; debug is for debug-on-entry function called.
292 (let ((pos (point)))
293 (pcase (car args)
294 ((or `lambda `debug)
295 (insert "--entering a function:\n")
296 (setq pos (1- (point))))
297 ;; Exiting a function.
298 (`exit
299 (insert "--returning value: ")
300 (setq pos (point))
301 (setq debugger-value (nth 1 args))
302 (prin1 debugger-value (current-buffer))
303 (insert ?\n)
304 (delete-char 1)
305 (insert ? )
306 (beginning-of-line))
307 ;; Debugger entered for an error.
308 (`error
309 (insert "--Lisp error: ")
310 (setq pos (point))
311 (prin1 (nth 1 args) (current-buffer))
312 (insert ?\n))
313 ;; debug-on-call, when the next thing is an eval.
314 (`t
315 (insert "--beginning evaluation of function call form:\n")
316 (setq pos (1- (point))))
317 ;; User calls debug directly.
318 (_
319 (insert ": ")
320 (setq pos (point))
321 (prin1 (if (eq (car args) 'nil)
322 (cdr args) args)
323 (current-buffer))
324 (insert ?\n)))
325 ;; Place point on "stack frame 0" (bug#15101).
326 (goto-char pos))
327 ;; After any frame that uses eval-buffer,
328 ;; insert a line that states the buffer position it's reading at.
329 (save-excursion
330 (let ((tem eval-buffer-list))
331 (while (and tem
332 (re-search-forward "^ eval-\\(buffer\\|region\\)(" nil t))
333 (end-of-line)
334 (insert (format " ; Reading at buffer position %d"
335 ;; This will get the wrong result
336 ;; if there are two nested eval-region calls
337 ;; for the same buffer. That's not a very useful case.
338 (with-current-buffer (car tem)
339 (point))))
340 (pop tem))))
341 (debugger-make-xrefs))
342
343 (defun debugger-make-xrefs (&optional buffer)
344 "Attach cross-references to function names in the `*Backtrace*' buffer."
345 (interactive "b")
346 (with-current-buffer (or buffer (current-buffer))
347 (save-excursion
348 (setq buffer (current-buffer))
349 (let ((inhibit-read-only t)
350 (old-end (point-min)) (new-end (point-min)))
351 ;; If we saved an old backtrace, find the common part
352 ;; between the new and the old.
353 ;; Compare line by line, starting from the end,
354 ;; because that's the part that is likely to be unchanged.
355 (if debugger-previous-backtrace
356 (let (old-start new-start (all-match t))
357 (goto-char (point-max))
358 (with-temp-buffer
359 (insert debugger-previous-backtrace)
360 (while (and all-match (not (bobp)))
361 (setq old-end (point))
362 (forward-line -1)
363 (setq old-start (point))
364 (with-current-buffer buffer
365 (setq new-end (point))
366 (forward-line -1)
367 (setq new-start (point)))
368 (if (not (zerop
369 (let ((case-fold-search nil))
370 (compare-buffer-substrings
371 (current-buffer) old-start old-end
372 buffer new-start new-end))))
373 (setq all-match nil))))
374 ;; Now new-end is the position of the start of the
375 ;; unchanged part in the current buffer, and old-end is
376 ;; the position of that same text in the saved old
377 ;; backtrace. But we must subtract (point-min) since strings are
378 ;; indexed in origin 0.
379
380 ;; Replace the unchanged part of the backtrace
381 ;; with the text from debugger-previous-backtrace,
382 ;; since that already has the proper xrefs.
383 ;; With this optimization, we only need to scan
384 ;; the changed part of the backtrace.
385 (delete-region new-end (point-max))
386 (goto-char (point-max))
387 (insert (substring debugger-previous-backtrace
388 (- old-end (point-min))))
389 ;; Make the unchanged part of the backtrace inaccessible
390 ;; so it won't be scanned.
391 (narrow-to-region (point-min) new-end)))
392
393 ;; Scan the new part of the backtrace, inserting xrefs.
394 (goto-char (point-min))
395 (while (progn
396 (goto-char (+ (point) 2))
397 (skip-syntax-forward "^w_")
398 (not (eobp)))
399 (let* ((beg (point))
400 (end (progn (skip-syntax-forward "w_") (point)))
401 (sym (intern-soft (buffer-substring-no-properties
402 beg end)))
403 (file (and sym (symbol-file sym 'defun))))
404 (when file
405 (goto-char beg)
406 ;; help-xref-button needs to operate on something matched
407 ;; by a regexp, so set that up for it.
408 (re-search-forward "\\(\\sw\\|\\s_\\)+")
409 (help-xref-button 0 'help-function-def sym file)))
410 (forward-line 1))
411 (widen))
412 (setq debugger-previous-backtrace (buffer-string)))))
413 \f
414 (defun debugger-step-through ()
415 "Proceed, stepping through subexpressions of this expression.
416 Enter another debugger on next entry to eval, apply or funcall."
417 (interactive)
418 (setq debugger-step-after-exit t)
419 (setq debugger-jumping-flag t)
420 (setq debugger-will-be-back t)
421 (add-hook 'post-command-hook 'debugger-reenable)
422 (message "Proceeding, will debug on next eval or call.")
423 (exit-recursive-edit))
424
425 (defun debugger-continue ()
426 "Continue, evaluating this expression without stopping."
427 (interactive)
428 (unless debugger-may-continue
429 (error "Cannot continue"))
430 (message "Continuing.")
431 (save-excursion
432 ;; Check to see if we've flagged some frame for debug-on-exit, in which
433 ;; case we'll probably come back to the debugger soon.
434 (goto-char (point-min))
435 (if (re-search-forward "^\\* " nil t)
436 (setq debugger-will-be-back t)))
437 (exit-recursive-edit))
438
439 (defun debugger-return-value (val)
440 "Continue, specifying value to return.
441 This is only useful when the value returned from the debugger
442 will be used, such as in a debug on exit from a frame."
443 (interactive "XReturn value (evaluated): ")
444 (when (memq (car debugger-args) '(t lambda error debug))
445 (error "Cannot return a value %s"
446 (if (eq (car debugger-args) 'error)
447 "from an error" "at function entrance")))
448 (setq debugger-value val)
449 (princ "Returning " t)
450 (prin1 debugger-value)
451 (save-excursion
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))
458
459 (defun debugger-jump ()
460 "Continue to exit from this frame, with all debug-on-entry suspended."
461 (interactive)
462 (debugger-frame)
463 (setq debugger-jumping-flag t)
464 (add-hook 'post-command-hook 'debugger-reenable)
465 (message "Continuing through this frame")
466 (setq debugger-will-be-back t)
467 (exit-recursive-edit))
468
469 (defun debugger-reenable ()
470 "Turn all debug-on-entry functions back on.
471 This function is put on `post-command-hook' by `debugger-jump' and
472 removes itself from that hook."
473 (setq debugger-jumping-flag nil)
474 (remove-hook 'post-command-hook 'debugger-reenable))
475
476 (defun debugger-frame-number (&optional skip-base)
477 "Return number of frames in backtrace before the one point points at."
478 (save-excursion
479 (beginning-of-line)
480 (if (looking-at " *;;;\\|[a-z]")
481 (error "This line is not a function call"))
482 (let ((opoint (point))
483 (count 0))
484 (unless skip-base
485 (while (not (eq (cadr (backtrace-frame count)) 'debug))
486 (setq count (1+ count)))
487 ;; Skip debug--implement-debug-on-entry frame.
488 (when (eq 'debug--implement-debug-on-entry
489 (cadr (backtrace-frame (1+ count))))
490 (setq count (+ 2 count))))
491 (goto-char (point-min))
492 (when (looking-at "Debugger entered--\\(Lisp error\\|returning value\\):")
493 (goto-char (match-end 0))
494 (forward-sexp 1))
495 (forward-line 1)
496 (while (progn
497 (forward-char 2)
498 (cond ((debugger--locals-visible-p)
499 (goto-char (next-single-char-property-change
500 (point) 'locals-visible)))
501 ((= (following-char) ?\()
502 (forward-sexp 1))
503 (t
504 (forward-sexp 2)))
505 (forward-line 1)
506 (<= (point) opoint))
507 (if (looking-at " *;;;")
508 (forward-line 1))
509 (setq count (1+ count)))
510 count)))
511
512 (defun debugger-frame ()
513 "Request entry to debugger when this frame exits.
514 Applies to the frame whose line point is on in the backtrace."
515 (interactive)
516 (backtrace-debug (debugger-frame-number) t)
517 (beginning-of-line)
518 (if (= (following-char) ? )
519 (let ((inhibit-read-only t))
520 (delete-char 1)
521 (insert ?*)))
522 (beginning-of-line))
523
524 (defun debugger-frame-clear ()
525 "Do not enter debugger when this frame exits.
526 Applies to the frame whose line point is on in the backtrace."
527 (interactive)
528 (backtrace-debug (debugger-frame-number) nil)
529 (beginning-of-line)
530 (if (= (following-char) ?*)
531 (let ((inhibit-read-only t))
532 (delete-char 1)
533 (insert ? )))
534 (beginning-of-line))
535
536 (defmacro debugger-env-macro (&rest body)
537 "Run BODY in original environment."
538 (declare (indent 0))
539 `(progn
540 (set-match-data debugger-outer-match-data)
541 (prog1
542 (progn ,@body)
543 (setq debugger-outer-match-data (match-data)))))
544
545 (defun debugger--backtrace-base ()
546 "Return the function name that marks the top of the backtrace.
547 See `backtrace-frame'."
548 (cond ((eq 'debug--implement-debug-on-entry
549 (cadr (backtrace-frame 1 'debug)))
550 'debug--implement-debug-on-entry)
551 (t 'debug)))
552
553 (defun debugger-eval-expression (exp &optional nframe)
554 "Eval an expression, in an environment like that outside the debugger.
555 The environment used is the one when entering the activation frame at point."
556 (interactive
557 (list (read--expression "Eval in stack frame: ")))
558 (let ((nframe (or nframe
559 (condition-case nil (1+ (debugger-frame-number 'skip-base))
560 (error 0)))) ;; If on first line.
561 (base (debugger--backtrace-base)))
562 (debugger-env-macro
563 (let ((val (backtrace-eval exp nframe base)))
564 (prog1
565 (prin1 val t)
566 (let ((str (eval-expression-print-format val)))
567 (if str (princ str t))))))))
568
569 (defun debugger--locals-visible-p ()
570 "Are the local variables of the current stack frame visible?"
571 (save-excursion
572 (move-to-column 2)
573 (get-text-property (point) 'locals-visible)))
574
575 (defun debugger--insert-locals (locals)
576 "Insert the local variables LOCALS at point."
577 (cond ((null locals)
578 (insert "\n [no locals]"))
579 (t
580 (let ((print-escape-newlines t))
581 (dolist (s+v locals)
582 (let ((symbol (car s+v))
583 (value (cdr s+v)))
584 (insert "\n ")
585 (prin1 symbol (current-buffer))
586 (insert " = ")
587 (prin1 value (current-buffer))))))))
588
589 (defun debugger--show-locals ()
590 "For the frame at point, insert locals and add text properties."
591 (let* ((nframe (1+ (debugger-frame-number 'skip-base)))
592 (base (debugger--backtrace-base))
593 (locals (backtrace--locals nframe base))
594 (inhibit-read-only t))
595 (save-excursion
596 (let ((start (progn
597 (move-to-column 2)
598 (point))))
599 (end-of-line)
600 (debugger--insert-locals locals)
601 (add-text-properties start (point) '(locals-visible t))))))
602
603 (defun debugger--hide-locals ()
604 "Delete local variables and remove the text property."
605 (let* ((col (current-column))
606 (end (progn
607 (move-to-column 2)
608 (next-single-char-property-change (point) 'locals-visible)))
609 (start (previous-single-char-property-change end 'locals-visible))
610 (inhibit-read-only t))
611 (remove-text-properties start end '(locals-visible))
612 (goto-char start)
613 (end-of-line)
614 (delete-region (point) end)
615 (move-to-column col)))
616
617 (defun debugger-toggle-locals ()
618 "Show or hide local variables of the current stack frame."
619 (interactive)
620 (cond ((debugger--locals-visible-p)
621 (debugger--hide-locals))
622 (t
623 (debugger--show-locals))))
624
625 \f
626 (defvar debugger-mode-map
627 (let ((map (make-keymap))
628 (menu-map (make-sparse-keymap)))
629 (set-keymap-parent map button-buffer-map)
630 (suppress-keymap map)
631 (define-key map "-" 'negative-argument)
632 (define-key map "b" 'debugger-frame)
633 (define-key map "c" 'debugger-continue)
634 (define-key map "j" 'debugger-jump)
635 (define-key map "r" 'debugger-return-value)
636 (define-key map "u" 'debugger-frame-clear)
637 (define-key map "d" 'debugger-step-through)
638 (define-key map "l" 'debugger-list-functions)
639 (define-key map "h" 'describe-mode)
640 (define-key map "q" 'top-level)
641 (define-key map "e" 'debugger-eval-expression)
642 (define-key map "v" 'debugger-toggle-locals) ; "v" is for "variables".
643 (define-key map " " 'next-line)
644 (define-key map "R" 'debugger-record-expression)
645 (define-key map "\C-m" 'debug-help-follow)
646 (define-key map [mouse-2] 'push-button)
647 (define-key map [menu-bar debugger] (cons "Debugger" menu-map))
648 (define-key menu-map [deb-top]
649 '(menu-item "Quit" top-level
650 :help "Quit debugging and return to top level"))
651 (define-key menu-map [deb-s0] '("--"))
652 (define-key menu-map [deb-descr]
653 '(menu-item "Describe Debugger Mode" describe-mode
654 :help "Display documentation for debugger-mode"))
655 (define-key menu-map [deb-hfol]
656 '(menu-item "Help Follow" debug-help-follow
657 :help "Follow cross-reference"))
658 (define-key menu-map [deb-nxt]
659 '(menu-item "Next Line" next-line
660 :help "Move cursor down"))
661 (define-key menu-map [deb-s1] '("--"))
662 (define-key menu-map [deb-lfunc]
663 '(menu-item "List debug on entry functions" debugger-list-functions
664 :help "Display a list of all the functions now set to debug on entry"))
665 (define-key menu-map [deb-fclear]
666 '(menu-item "Cancel debug frame" debugger-frame-clear
667 :help "Do not enter debugger when this frame exits"))
668 (define-key menu-map [deb-frame]
669 '(menu-item "Debug frame" debugger-frame
670 :help "Request entry to debugger when this frame exits"))
671 (define-key menu-map [deb-s2] '("--"))
672 (define-key menu-map [deb-ret]
673 '(menu-item "Return value..." debugger-return-value
674 :help "Continue, specifying value to return."))
675 (define-key menu-map [deb-rec]
676 '(menu-item "Display and Record Expression" debugger-record-expression
677 :help "Display a variable's value and record it in `*Backtrace-record*' buffer"))
678 (define-key menu-map [deb-eval]
679 '(menu-item "Eval Expression..." debugger-eval-expression
680 :help "Eval an expression, in an environment like that outside the debugger"))
681 (define-key menu-map [deb-jump]
682 '(menu-item "Jump" debugger-jump
683 :help "Continue to exit from this frame, with all debug-on-entry suspended"))
684 (define-key menu-map [deb-cont]
685 '(menu-item "Continue" debugger-continue
686 :help "Continue, evaluating this expression without stopping"))
687 (define-key menu-map [deb-step]
688 '(menu-item "Step through" debugger-step-through
689 :help "Proceed, stepping through subexpressions of this expression"))
690 map))
691
692 (put 'debugger-mode 'mode-class 'special)
693
694 (define-derived-mode debugger-mode fundamental-mode "Debugger"
695 "Mode for backtrace buffers, selected in debugger.
696 \\<debugger-mode-map>
697 A line starts with `*' if exiting that frame will call the debugger.
698 Type \\[debugger-frame] or \\[debugger-frame-clear] to set or remove the `*'.
699
700 When in debugger due to frame being exited,
701 use the \\[debugger-return-value] command to override the value
702 being returned from that frame.
703
704 Use \\[debug-on-entry] and \\[cancel-debug-on-entry] to control
705 which functions will enter the debugger when called.
706
707 Complete list of commands:
708 \\{debugger-mode-map}"
709 (setq truncate-lines t)
710 (set-syntax-table emacs-lisp-mode-syntax-table)
711 (use-local-map debugger-mode-map))
712 \f
713 (defcustom debugger-record-buffer "*Debugger-record*"
714 "Buffer name for expression values, for \\[debugger-record-expression]."
715 :type 'string
716 :group 'debugger
717 :version "20.3")
718
719 (defun debugger-record-expression (exp)
720 "Display a variable's value and record it in `*Backtrace-record*' buffer."
721 (interactive
722 (list (read--expression "Record Eval: ")))
723 (let* ((buffer (get-buffer-create debugger-record-buffer))
724 (standard-output buffer))
725 (princ (format "Debugger Eval (%s): " exp))
726 (princ (debugger-eval-expression exp))
727 (terpri))
728
729 (with-current-buffer (get-buffer debugger-record-buffer)
730 (message "%s"
731 (buffer-substring (line-beginning-position 0)
732 (line-end-position 0)))))
733
734 (defun debug-help-follow (&optional pos)
735 "Follow cross-reference at POS, defaulting to point.
736
737 For the cross-reference format, see `help-make-xrefs'."
738 (interactive "d")
739 ;; Ideally we'd just do (call-interactively 'help-follow) except that this
740 ;; assumes we're already in a *Help* buffer and reuses it, so it ends up
741 ;; incorrectly "reusing" the *Backtrace* buffer to show the help info.
742 (unless pos
743 (setq pos (point)))
744 (unless (push-button pos)
745 ;; check if the symbol under point is a function or variable
746 (let ((sym
747 (intern
748 (save-excursion
749 (goto-char pos) (skip-syntax-backward "w_")
750 (buffer-substring (point)
751 (progn (skip-syntax-forward "w_")
752 (point)))))))
753 (when (or (boundp sym) (fboundp sym) (facep sym))
754 (describe-symbol sym)))))
755 \f
756 ;; When you change this, you may also need to change the number of
757 ;; frames that the debugger skips.
758 (defun debug--implement-debug-on-entry (&rest _ignore)
759 "Conditionally call the debugger.
760 A call to this function is inserted by `debug-on-entry' to cause
761 functions to break on entry."
762 (if (or inhibit-debug-on-entry debugger-jumping-flag)
763 nil
764 (let ((inhibit-debug-on-entry t))
765 (funcall debugger 'debug))))
766
767 ;;;###autoload
768 (defun debug-on-entry (function)
769 "Request FUNCTION to invoke debugger each time it is called.
770
771 When called interactively, prompt for FUNCTION in the minibuffer.
772
773 This works by modifying the definition of FUNCTION. If you tell the
774 debugger to continue, FUNCTION's execution proceeds. If FUNCTION is a
775 normal function or a macro written in Lisp, you can also step through
776 its execution. FUNCTION can also be a primitive that is not a special
777 form, in which case stepping is not possible. Break-on-entry for
778 primitive functions only works when that function is called from Lisp.
779
780 Use \\[cancel-debug-on-entry] to cancel the effect of this command.
781 Redefining FUNCTION also cancels it."
782 (interactive
783 (let ((fn (function-called-at-point)) val)
784 (when (special-form-p fn)
785 (setq fn nil))
786 (setq val (completing-read
787 (if fn
788 (format "Debug on entry to function (default %s): " fn)
789 "Debug on entry to function: ")
790 obarray
791 #'(lambda (symbol)
792 (and (fboundp symbol)
793 (not (special-form-p symbol))))
794 t nil nil (symbol-name fn)))
795 (list (if (equal val "") fn (intern val)))))
796 (advice-add function :before #'debug--implement-debug-on-entry
797 '((depth . -100)))
798 function)
799
800 (defun debug--function-list ()
801 "List of functions currently set for debug on entry."
802 (let ((funs '()))
803 (mapatoms
804 (lambda (s)
805 (when (advice-member-p #'debug--implement-debug-on-entry s)
806 (push s funs))))
807 funs))
808
809 ;;;###autoload
810 (defun cancel-debug-on-entry (&optional function)
811 "Undo effect of \\[debug-on-entry] on FUNCTION.
812 If FUNCTION is nil, cancel debug-on-entry for all functions.
813 When called interactively, prompt for FUNCTION in the minibuffer.
814 To specify a nil argument interactively, exit with an empty minibuffer."
815 (interactive
816 (list (let ((name
817 (completing-read
818 "Cancel debug on entry to function (default all functions): "
819 (mapcar #'symbol-name (debug--function-list)) nil t)))
820 (when name
821 (unless (string= name "")
822 (intern name))))))
823 (if function
824 (progn
825 (advice-remove function #'debug--implement-debug-on-entry)
826 function)
827 (message "Canceling debug-on-entry for all functions")
828 (mapcar #'cancel-debug-on-entry (debug--function-list))))
829
830 (defun debugger-list-functions ()
831 "Display a list of all the functions now set to debug on entry."
832 (interactive)
833 (require 'help-mode)
834 (help-setup-xref '(debugger-list-functions)
835 (called-interactively-p 'interactive))
836 (with-output-to-temp-buffer (help-buffer)
837 (with-current-buffer standard-output
838 (let ((funs (debug--function-list)))
839 (if (null funs)
840 (princ "No debug-on-entry functions now\n")
841 (princ "Functions set to debug on entry:\n\n")
842 (dolist (fun funs)
843 (make-text-button (point) (progn (prin1 fun) (point))
844 'type 'help-function
845 'help-args (list fun))
846 (terpri))
847 (terpri)
848 (princ "Note: if you have redefined a function, then it may no longer\n")
849 (princ "be set to debug on entry, even if it is in the list."))))))
850
851 (provide 'debug)
852
853 ;;; debug.el ends here