Commit | Line | Data |
---|---|---|
231d8498 | 1 | ;;; debug.el --- debuggers and related commands for Emacs -*- lexical-binding: t -*- |
c0274f38 | 2 | |
acaf905b | 3 | ;; Copyright (C) 1985-1986, 1994, 2001-2012 Free Software Foundation, Inc. |
9750e079 | 4 | |
e5167999 | 5 | ;; Maintainer: FSF |
e9571d2a | 6 | ;; Keywords: lisp, tools, maint |
e5167999 | 7 | |
0231f2dc JB |
8 | ;; This file is part of GNU Emacs. |
9 | ||
d6cba7ae | 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
0231f2dc | 11 | ;; it under the terms of the GNU General Public License as published by |
d6cba7ae GM |
12 | ;; the Free Software Foundation, either version 3 of the License, or |
13 | ;; (at your option) any later version. | |
0231f2dc JB |
14 | |
15 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;; GNU General Public License for more details. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
d6cba7ae | 21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
0231f2dc | 22 | |
e41b2db1 ER |
23 | ;;; Commentary: |
24 | ||
07f3fdb1 | 25 | ;; This is a major mode documented in the Emacs Lisp manual. |
e41b2db1 | 26 | |
e5167999 | 27 | ;;; Code: |
0231f2dc | 28 | |
bb9a622e MB |
29 | (require 'button) |
30 | ||
666b9413 SE |
31 | (defgroup debugger nil |
32 | "Debuggers and related commands for Emacs." | |
33 | :prefix "debugger-" | |
34 | :group 'debug) | |
8c1cd093 | 35 | |
666b9413 | 36 | (defcustom debugger-mode-hook nil |
cb711556 | 37 | "Hooks run when `debugger-mode' is turned on." |
666b9413 | 38 | :type 'hook |
cd32a7ba DN |
39 | :group 'debugger |
40 | :version "20.3") | |
8c1cd093 | 41 | |
63ca439e | 42 | (defcustom debugger-batch-max-lines 40 |
cb711556 | 43 | "Maximum lines to show in debugger buffer in a noninteractive Emacs. |
63ca439e GM |
44 | When the debugger is entered and Emacs is running in batch mode, |
45 | if the backtrace text has more than this many lines, | |
46 | the middle is discarded, and just the beginning and end are displayed." | |
47 | :type 'integer | |
48 | :group 'debugger | |
49 | :version "21.1") | |
0231f2dc | 50 | |
fa2bcf43 | 51 | (defcustom debugger-bury-or-kill 'bury |
57fc0fee GM |
52 | "What to do with the debugger buffer when exiting `debug'. |
53 | The value affects the behavior of operations on any window | |
54 | previously showing the debugger buffer. | |
fa2bcf43 MR |
55 | |
56 | `nil' means that if its window is not deleted when exiting the | |
57 | debugger, invoking `switch-to-prev-buffer' will usually show | |
58 | the debugger buffer again. | |
59 | ||
60 | `append' means that if the window is not deleted, the debugger | |
61 | buffer moves to the end of the window's previous buffers so | |
62 | it's less likely that a future invocation of | |
63 | `switch-to-prev-buffer' will switch to it. Also, it moves the | |
64 | buffer to the end of the frame's buffer list. | |
65 | ||
66 | `bury' means that if the window is not deleted, its buffer is | |
67 | removed from the window's list of previous buffers. Also, it | |
68 | moves the buffer to the end of the frame's buffer list. This | |
69 | value provides the most reliable remedy to not have | |
70 | `switch-to-prev-buffer' switch to the debugger buffer again | |
71 | without killing the buffer. | |
72 | ||
73 | `kill' means to kill the debugger buffer. | |
74 | ||
75 | The value used here is passed to `quit-restore-window'." | |
76 | :type '(choice | |
77 | (const :tag "Keep alive" nil) | |
37ab5092 MR |
78 | (const :tag "Append" append) |
79 | (const :tag "Bury" bury) | |
80 | (const :tag "Kill" kill)) | |
fa2bcf43 | 81 | :group 'debugger |
68146aa5 | 82 | :version "24.3") |
fa2bcf43 | 83 | |
15cf150d LK |
84 | (defvar debugger-step-after-exit nil |
85 | "Non-nil means \"single-step\" after the debugger exits.") | |
2eeeb0d2 RS |
86 | |
87 | (defvar debugger-value nil | |
88 | "This is the value for the debugger to return, when it returns.") | |
89 | ||
90 | (defvar debugger-old-buffer nil | |
91 | "This is the buffer that was current when the debugger was entered.") | |
92 | ||
fa2bcf43 MR |
93 | (defvar debugger-previous-window nil |
94 | "This is the window last showing the debugger buffer.") | |
95 | ||
4dece104 MR |
96 | (defvar debugger-previous-window-height nil |
97 | "The last recorded height of `debugger-previous-window'.") | |
98 | ||
25c58854 RS |
99 | (defvar debugger-previous-backtrace nil |
100 | "The contents of the previous backtrace (including text properties). | |
101 | This is to optimize `debugger-make-xrefs'.") | |
102 | ||
cf1c8cd9 | 103 | (defvar debugger-outer-match-data) |
80ddb8ed RS |
104 | (defvar debugger-outer-load-read-function) |
105 | (defvar debugger-outer-overriding-local-map) | |
29e2b496 | 106 | (defvar debugger-outer-overriding-terminal-local-map) |
35cf010d RS |
107 | (defvar debugger-outer-track-mouse) |
108 | (defvar debugger-outer-last-command) | |
109 | (defvar debugger-outer-this-command) | |
35cf010d | 110 | (defvar debugger-outer-unread-command-events) |
0359db82 | 111 | (defvar debugger-outer-unread-post-input-method-events) |
35cf010d RS |
112 | (defvar debugger-outer-last-input-event) |
113 | (defvar debugger-outer-last-command-event) | |
114 | (defvar debugger-outer-last-nonmenu-event) | |
115 | (defvar debugger-outer-last-event-frame) | |
116 | (defvar debugger-outer-standard-input) | |
117 | (defvar debugger-outer-standard-output) | |
bf90c2c0 | 118 | (defvar debugger-outer-inhibit-redisplay) |
35cf010d | 119 | (defvar debugger-outer-cursor-in-echo-area) |
9b530428 SM |
120 | (defvar debugger-will-be-back nil |
121 | "Non-nil if we expect to get back in the debugger soon.") | |
35cf010d | 122 | |
b6b77a90 LK |
123 | (defvar inhibit-debug-on-entry nil |
124 | "Non-nil means that debug-on-entry is disabled.") | |
125 | ||
126 | (defvar debugger-jumping-flag nil | |
127 | "Non-nil means that debug-on-entry is disabled. | |
9b54d453 LK |
128 | This variable is used by `debugger-jump', `debugger-step-through', |
129 | and `debugger-reenable' to temporarily disable debug-on-entry.") | |
cf9a1b69 | 130 | |
d48f0f00 | 131 | (defvar inhibit-trace) ;Not yet implemented. |
77932668 | 132 | |
e24e27be SM |
133 | (defvar debugger-args nil |
134 | "Arguments with which the debugger was called. | |
135 | It is a list expected to take the form (CAUSE . REST) | |
136 | where CAUSE can be: | |
137 | - debug: called for entry to a flagged function. | |
138 | - t: called because of debug-on-next-call. | |
139 | - lambda: same thing but via `funcall'. | |
140 | - exit: called because of exit of a flagged function. | |
141 | - error: called because of `debug-on-error'.") | |
142 | ||
0231f2dc JB |
143 | ;;;###autoload |
144 | (setq debugger 'debug) | |
145 | ;;;###autoload | |
231d8498 | 146 | (defun debug (&rest args) |
a7c33da2 | 147 | "Enter debugger. \\<debugger-mode-map>`\\[debugger-continue]' returns from the debugger. |
0231f2dc JB |
148 | Arguments are mainly for use when this is called from the internals |
149 | of the evaluator. | |
150 | ||
151 | You may call with no args, or you may pass nil as the first arg and | |
152 | any other args you like. In that case, the list of args after the | |
153 | first will be printed into the backtrace buffer." | |
73e78cf1 | 154 | (interactive) |
e734f584 RS |
155 | (if inhibit-redisplay |
156 | ;; Don't really try to enter debugger within an eval from redisplay. | |
157 | debugger-value | |
158 | (unless noninteractive | |
159 | (message "Entering debugger...")) | |
160 | (let (debugger-value | |
e227544d SM |
161 | (debugger-previous-state |
162 | (if (get-buffer "*Backtrace*") | |
163 | (with-current-buffer (get-buffer "*Backtrace*") | |
164 | (list major-mode (buffer-string))))) | |
231d8498 | 165 | (debugger-args args) |
72f16325 | 166 | (debugger-buffer (get-buffer-create "*Backtrace*")) |
96e8d411 | 167 | (debugger-old-buffer (current-buffer)) |
fa2bcf43 | 168 | (debugger-window nil) |
e734f584 | 169 | (debugger-step-after-exit nil) |
9b530428 | 170 | (debugger-will-be-back nil) |
e734f584 RS |
171 | ;; Don't keep reading from an executing kbd macro! |
172 | (executing-kbd-macro nil) | |
173 | ;; Save the outer values of these vars for the `e' command | |
174 | ;; before we replace the values. | |
175 | (debugger-outer-match-data (match-data)) | |
176 | (debugger-outer-load-read-function load-read-function) | |
177 | (debugger-outer-overriding-local-map overriding-local-map) | |
178 | (debugger-outer-overriding-terminal-local-map | |
179 | overriding-terminal-local-map) | |
180 | (debugger-outer-track-mouse track-mouse) | |
181 | (debugger-outer-last-command last-command) | |
182 | (debugger-outer-this-command this-command) | |
e734f584 RS |
183 | (debugger-outer-unread-command-events unread-command-events) |
184 | (debugger-outer-unread-post-input-method-events | |
185 | unread-post-input-method-events) | |
186 | (debugger-outer-last-input-event last-input-event) | |
187 | (debugger-outer-last-command-event last-command-event) | |
188 | (debugger-outer-last-nonmenu-event last-nonmenu-event) | |
189 | (debugger-outer-last-event-frame last-event-frame) | |
190 | (debugger-outer-standard-input standard-input) | |
191 | (debugger-outer-standard-output standard-output) | |
192 | (debugger-outer-inhibit-redisplay inhibit-redisplay) | |
385480e5 RS |
193 | (debugger-outer-cursor-in-echo-area cursor-in-echo-area) |
194 | (debugger-with-timeout-suspend (with-timeout-suspend))) | |
e734f584 RS |
195 | ;; Set this instead of binding it, so that `q' |
196 | ;; will not restore it. | |
197 | (setq overriding-terminal-local-map nil) | |
198 | ;; Don't let these magic variables affect the debugger itself. | |
199 | (let ((last-command nil) this-command track-mouse | |
cf9a1b69 SM |
200 | (inhibit-trace t) |
201 | (inhibit-debug-on-entry t) | |
12cf32ce | 202 | unread-command-events |
e734f584 RS |
203 | unread-post-input-method-events |
204 | last-input-event last-command-event last-nonmenu-event | |
205 | last-event-frame | |
206 | overriding-local-map | |
207 | load-read-function | |
208 | ;; If we are inside a minibuffer, allow nesting | |
209 | ;; so that we don't get an error from the `e' command. | |
210 | (enable-recursive-minibuffers | |
211 | (or enable-recursive-minibuffers (> (minibuffer-depth) 0))) | |
212 | (standard-input t) (standard-output t) | |
213 | inhibit-redisplay | |
88ded8ac MR |
214 | (cursor-in-echo-area nil) |
215 | (window-configuration (current-window-configuration))) | |
e734f584 RS |
216 | (unwind-protect |
217 | (save-excursion | |
fa2bcf43 MR |
218 | (when (eq (car debugger-args) 'debug) |
219 | ;; Skip the frames for backtrace-debug, byte-code, | |
231d8498 | 220 | ;; debug--implement-debug-on-entry and the advice's `apply'. |
fa2bcf43 MR |
221 | (backtrace-debug 4 t) |
222 | ;; Place an extra debug-on-exit for macro's. | |
223 | (when (eq 'lambda (car-safe (cadr (backtrace-frame 4)))) | |
224 | (backtrace-debug 5 t))) | |
225 | (pop-to-buffer | |
226 | debugger-buffer | |
227 | `((display-buffer-reuse-window | |
228 | display-buffer-in-previous-window) | |
229 | . (,(when debugger-previous-window | |
230 | `(previous-window . ,debugger-previous-window))))) | |
231 | (setq debugger-window (selected-window)) | |
4dece104 MR |
232 | (if (eq debugger-previous-window debugger-window) |
233 | (when debugger-jumping-flag | |
234 | ;; Try to restore previous height of debugger | |
235 | ;; window. | |
236 | (condition-case nil | |
237 | (window-resize | |
238 | debugger-window | |
239 | (- debugger-previous-window-height | |
240 | (window-total-size debugger-window))) | |
241 | (error nil))) | |
242 | (setq debugger-previous-window debugger-window)) | |
fa2bcf43 MR |
243 | (debugger-mode) |
244 | (debugger-setup-buffer debugger-args) | |
245 | (when noninteractive | |
246 | ;; If the backtrace is long, save the beginning | |
247 | ;; and the end, but discard the middle. | |
248 | (when (> (count-lines (point-min) (point-max)) | |
249 | debugger-batch-max-lines) | |
63ca439e | 250 | (goto-char (point-min)) |
fa2bcf43 MR |
251 | (forward-line (/ 2 debugger-batch-max-lines)) |
252 | (let ((middlestart (point))) | |
253 | (goto-char (point-max)) | |
254 | (forward-line (- (/ 2 debugger-batch-max-lines) | |
255 | debugger-batch-max-lines)) | |
256 | (delete-region middlestart (point))) | |
257 | (insert "...\n")) | |
258 | (goto-char (point-min)) | |
259 | (message "%s" (buffer-string)) | |
260 | (kill-emacs -1)) | |
261 | (message "") | |
262 | (let ((standard-output nil) | |
263 | (buffer-read-only t)) | |
35cf010d | 264 | (message "") |
fa2bcf43 MR |
265 | ;; Make sure we unbind buffer-read-only in the right buffer. |
266 | (save-excursion | |
267 | (recursive-edit)))) | |
fb0104da MR |
268 | (when (and (window-live-p debugger-window) |
269 | (eq (window-buffer debugger-window) debugger-buffer)) | |
270 | ;; Record height of debugger window. | |
271 | (setq debugger-previous-window-height | |
272 | (window-total-size debugger-window))) | |
88ded8ac MR |
273 | (if debugger-will-be-back |
274 | ;; Restore previous window configuration (Bug#12623). | |
275 | (set-window-configuration window-configuration) | |
276 | (when (and (window-live-p debugger-window) | |
277 | (eq (window-buffer debugger-window) debugger-buffer)) | |
278 | (progn | |
88ded8ac MR |
279 | ;; Unshow debugger-buffer. |
280 | (quit-restore-window debugger-window debugger-bury-or-kill) | |
281 | ;; Restore current buffer (Bug#12502). | |
282 | (set-buffer debugger-old-buffer)))) | |
fa2bcf43 MR |
283 | ;; Restore previous state of debugger-buffer in case we were |
284 | ;; in a recursive invocation of the debugger, otherwise just | |
285 | ;; erase the buffer and put it into fundamental mode. | |
286 | (when (buffer-live-p debugger-buffer) | |
287 | (with-current-buffer debugger-buffer | |
288 | (let ((inhibit-read-only t)) | |
289 | (erase-buffer) | |
290 | (if (null debugger-previous-state) | |
291 | (fundamental-mode) | |
292 | (insert (nth 1 debugger-previous-state)) | |
293 | (funcall (nth 0 debugger-previous-state)))))) | |
385480e5 | 294 | (with-timeout-unsuspend debugger-with-timeout-suspend) |
e734f584 RS |
295 | (set-match-data debugger-outer-match-data))) |
296 | ;; Put into effect the modified values of these variables | |
297 | ;; in case the user set them with the `e' command. | |
298 | (setq load-read-function debugger-outer-load-read-function) | |
299 | (setq overriding-local-map debugger-outer-overriding-local-map) | |
300 | (setq overriding-terminal-local-map | |
301 | debugger-outer-overriding-terminal-local-map) | |
302 | (setq track-mouse debugger-outer-track-mouse) | |
303 | (setq last-command debugger-outer-last-command) | |
304 | (setq this-command debugger-outer-this-command) | |
e734f584 RS |
305 | (setq unread-command-events debugger-outer-unread-command-events) |
306 | (setq unread-post-input-method-events | |
307 | debugger-outer-unread-post-input-method-events) | |
308 | (setq last-input-event debugger-outer-last-input-event) | |
309 | (setq last-command-event debugger-outer-last-command-event) | |
310 | (setq last-nonmenu-event debugger-outer-last-nonmenu-event) | |
311 | (setq last-event-frame debugger-outer-last-event-frame) | |
312 | (setq standard-input debugger-outer-standard-input) | |
313 | (setq standard-output debugger-outer-standard-output) | |
314 | (setq inhibit-redisplay debugger-outer-inhibit-redisplay) | |
315 | (setq cursor-in-echo-area debugger-outer-cursor-in-echo-area) | |
316 | (setq debug-on-next-call debugger-step-after-exit) | |
317 | debugger-value))) | |
0231f2dc | 318 | \f |
231d8498 | 319 | (defun debugger-setup-buffer (args) |
63ca439e GM |
320 | "Initialize the `*Backtrace*' buffer for entry to the debugger. |
321 | That buffer should be current already." | |
322 | (setq buffer-read-only nil) | |
323 | (erase-buffer) | |
c61dc887 | 324 | (set-buffer-multibyte t) ;Why was it nil ? -stef |
49c23b70 | 325 | (setq buffer-undo-list t) |
63ca439e GM |
326 | (let ((standard-output (current-buffer)) |
327 | (print-escape-newlines t) | |
328 | (print-level 8) | |
329 | (print-length 50)) | |
330 | (backtrace)) | |
331 | (goto-char (point-min)) | |
332 | (delete-region (point) | |
333 | (progn | |
334 | (search-forward "\n debug(") | |
231d8498 SM |
335 | (forward-line (if (eq (car args) 'debug) |
336 | ;; Remove debug--implement-debug-on-entry | |
337 | ;; and the advice's `apply' frame. | |
338 | 3 | |
439368ed | 339 | 1)) |
63ca439e GM |
340 | (point))) |
341 | (insert "Debugger entered") | |
342 | ;; lambda is for debug-on-call when a function call is next. | |
343 | ;; debug is for debug-on-entry function called. | |
231d8498 | 344 | (pcase (car args) |
e24e27be SM |
345 | ((or `lambda `debug) |
346 | (insert "--entering a function:\n")) | |
347 | ;; Exiting a function. | |
348 | (`exit | |
349 | (insert "--returning value: ") | |
231d8498 | 350 | (setq debugger-value (nth 1 args)) |
e24e27be SM |
351 | (prin1 debugger-value (current-buffer)) |
352 | (insert ?\n) | |
353 | (delete-char 1) | |
354 | (insert ? ) | |
355 | (beginning-of-line)) | |
356 | ;; Debugger entered for an error. | |
357 | (`error | |
358 | (insert "--Lisp error: ") | |
231d8498 | 359 | (prin1 (nth 1 args) (current-buffer)) |
e24e27be SM |
360 | (insert ?\n)) |
361 | ;; debug-on-call, when the next thing is an eval. | |
362 | (`t | |
363 | (insert "--beginning evaluation of function call form:\n")) | |
364 | ;; User calls debug directly. | |
365 | (_ | |
366 | (insert ": ") | |
231d8498 SM |
367 | (prin1 (if (eq (car args) 'nil) |
368 | (cdr args) args) | |
e24e27be SM |
369 | (current-buffer)) |
370 | (insert ?\n))) | |
27bde5f0 RS |
371 | ;; After any frame that uses eval-buffer, |
372 | ;; insert a line that states the buffer position it's reading at. | |
373 | (save-excursion | |
245f0222 RS |
374 | (let ((tem eval-buffer-list)) |
375 | (while (and tem | |
376 | (re-search-forward "^ eval-\\(buffer\\|region\\)(" nil t)) | |
377 | (end-of-line) | |
378 | (insert (format " ; Reading at buffer position %d" | |
379 | ;; This will get the wrong result | |
380 | ;; if there are two nested eval-region calls | |
381 | ;; for the same buffer. That's not a very useful case. | |
382 | (with-current-buffer (car tem) | |
383 | (point)))) | |
384 | (pop tem)))) | |
cc1bde62 SS |
385 | (debugger-make-xrefs)) |
386 | ||
387 | (defun debugger-make-xrefs (&optional buffer) | |
dc64809a | 388 | "Attach cross-references to function names in the `*Backtrace*' buffer." |
cc1bde62 | 389 | (interactive "b") |
9a529312 | 390 | (with-current-buffer (or buffer (current-buffer)) |
8e39b2e8 GM |
391 | (save-excursion |
392 | (setq buffer (current-buffer)) | |
393 | (let ((inhibit-read-only t) | |
5f66aa05 GM |
394 | (old-end (point-min)) (new-end (point-min))) |
395 | ;; If we saved an old backtrace, find the common part | |
396 | ;; between the new and the old. | |
397 | ;; Compare line by line, starting from the end, | |
398 | ;; because that's the part that is likely to be unchanged. | |
399 | (if debugger-previous-backtrace | |
400 | (let (old-start new-start (all-match t)) | |
401 | (goto-char (point-max)) | |
402 | (with-temp-buffer | |
403 | (insert debugger-previous-backtrace) | |
404 | (while (and all-match (not (bobp))) | |
405 | (setq old-end (point)) | |
406 | (forward-line -1) | |
407 | (setq old-start (point)) | |
408 | (with-current-buffer buffer | |
409 | (setq new-end (point)) | |
410 | (forward-line -1) | |
411 | (setq new-start (point))) | |
412 | (if (not (zerop | |
413 | (let ((case-fold-search nil)) | |
414 | (compare-buffer-substrings | |
415 | (current-buffer) old-start old-end | |
416 | buffer new-start new-end)))) | |
417 | (setq all-match nil)))) | |
418 | ;; Now new-end is the position of the start of the | |
419 | ;; unchanged part in the current buffer, and old-end is | |
420 | ;; the position of that same text in the saved old | |
421 | ;; backtrace. But we must subtract (point-min) since strings are | |
422 | ;; indexed in origin 0. | |
25c58854 | 423 | |
5f66aa05 GM |
424 | ;; Replace the unchanged part of the backtrace |
425 | ;; with the text from debugger-previous-backtrace, | |
426 | ;; since that already has the proper xrefs. | |
427 | ;; With this optimization, we only need to scan | |
428 | ;; the changed part of the backtrace. | |
429 | (delete-region new-end (point-max)) | |
430 | (goto-char (point-max)) | |
431 | (insert (substring debugger-previous-backtrace | |
432 | (- old-end (point-min)))) | |
433 | ;; Make the unchanged part of the backtrace inaccessible | |
434 | ;; so it won't be scanned. | |
435 | (narrow-to-region (point-min) new-end))) | |
a1506d29 | 436 | |
5f66aa05 GM |
437 | ;; Scan the new part of the backtrace, inserting xrefs. |
438 | (goto-char (point-min)) | |
439 | (while (progn | |
440 | (goto-char (+ (point) 2)) | |
441 | (skip-syntax-forward "^w_") | |
442 | (not (eobp))) | |
443 | (let* ((beg (point)) | |
444 | (end (progn (skip-syntax-forward "w_") (point))) | |
445 | (sym (intern-soft (buffer-substring-no-properties | |
446 | beg end))) | |
447 | (file (and sym (symbol-file sym 'defun)))) | |
448 | (when file | |
449 | (goto-char beg) | |
450 | ;; help-xref-button needs to operate on something matched | |
451 | ;; by a regexp, so set that up for it. | |
452 | (re-search-forward "\\(\\sw\\|\\s_\\)+") | |
453 | (help-xref-button 0 'help-function-def sym file))) | |
454 | (forward-line 1)) | |
455 | (widen)) | |
8e39b2e8 | 456 | (setq debugger-previous-backtrace (buffer-string))))) |
63ca439e | 457 | \f |
0231f2dc JB |
458 | (defun debugger-step-through () |
459 | "Proceed, stepping through subexpressions of this expression. | |
460 | Enter another debugger on next entry to eval, apply or funcall." | |
461 | (interactive) | |
462 | (setq debugger-step-after-exit t) | |
9b54d453 | 463 | (setq debugger-jumping-flag t) |
9b530428 | 464 | (setq debugger-will-be-back t) |
9b54d453 | 465 | (add-hook 'post-command-hook 'debugger-reenable) |
0231f2dc JB |
466 | (message "Proceeding, will debug on next eval or call.") |
467 | (exit-recursive-edit)) | |
468 | ||
469 | (defun debugger-continue () | |
470 | "Continue, evaluating this expression without stopping." | |
471 | (interactive) | |
094e0928 GM |
472 | (unless debugger-may-continue |
473 | (error "Cannot continue")) | |
0231f2dc | 474 | (message "Continuing.") |
9b530428 SM |
475 | (save-excursion |
476 | ;; Check to see if we've flagged some frame for debug-on-exit, in which | |
477 | ;; case we'll probably come back to the debugger soon. | |
478 | (goto-char (point-min)) | |
479 | (if (re-search-forward "^\\* " nil t) | |
480 | (setq debugger-will-be-back t))) | |
0231f2dc JB |
481 | (exit-recursive-edit)) |
482 | ||
483 | (defun debugger-return-value (val) | |
484 | "Continue, specifying value to return. | |
485 | This is only useful when the value returned from the debugger | |
486 | will be used, such as in a debug on exit from a frame." | |
487 | (interactive "XReturn value (evaluated): ") | |
e24e27be SM |
488 | (when (memq (car debugger-args) '(t lambda error debug)) |
489 | (error "Cannot return a value %s" | |
490 | (if (eq (car debugger-args) 'error) | |
491 | "from an error" "at function entrance"))) | |
0231f2dc JB |
492 | (setq debugger-value val) |
493 | (princ "Returning " t) | |
494 | (prin1 debugger-value) | |
9b530428 SM |
495 | (save-excursion |
496 | ;; Check to see if we've flagged some frame for debug-on-exit, in which | |
497 | ;; case we'll probably come back to the debugger soon. | |
498 | (goto-char (point-min)) | |
499 | (if (re-search-forward "^\\* " nil t) | |
500 | (setq debugger-will-be-back t))) | |
0231f2dc JB |
501 | (exit-recursive-edit)) |
502 | ||
503 | (defun debugger-jump () | |
504 | "Continue to exit from this frame, with all debug-on-entry suspended." | |
505 | (interactive) | |
27bde5f0 | 506 | (debugger-frame) |
b6b77a90 LK |
507 | (setq debugger-jumping-flag t) |
508 | (add-hook 'post-command-hook 'debugger-reenable) | |
0231f2dc | 509 | (message "Continuing through this frame") |
9b530428 | 510 | (setq debugger-will-be-back t) |
0231f2dc JB |
511 | (exit-recursive-edit)) |
512 | ||
513 | (defun debugger-reenable () | |
b6b77a90 LK |
514 | "Turn all debug-on-entry functions back on. |
515 | This function is put on `post-command-hook' by `debugger-jump' and | |
516 | removes itself from that hook." | |
517 | (setq debugger-jumping-flag nil) | |
518 | (remove-hook 'post-command-hook 'debugger-reenable)) | |
0231f2dc JB |
519 | |
520 | (defun debugger-frame-number () | |
521 | "Return number of frames in backtrace before the one point points at." | |
522 | (save-excursion | |
523 | (beginning-of-line) | |
524 | (let ((opoint (point)) | |
525 | (count 0)) | |
27bde5f0 RS |
526 | (while (not (eq (cadr (backtrace-frame count)) 'debug)) |
527 | (setq count (1+ count))) | |
231d8498 SM |
528 | ;; Skip debug--implement-debug-on-entry frame. |
529 | (when (eq 'debug--implement-debug-on-entry | |
530 | (cadr (backtrace-frame (1+ count)))) | |
531 | (setq count (+ 2 count))) | |
0231f2dc | 532 | (goto-char (point-min)) |
439368ed LK |
533 | (when (looking-at "Debugger entered--\\(Lisp error\\|returning value\\):") |
534 | (goto-char (match-end 0)) | |
535 | (forward-sexp 1)) | |
0231f2dc JB |
536 | (forward-line 1) |
537 | (while (progn | |
538 | (forward-char 2) | |
539 | (if (= (following-char) ?\() | |
540 | (forward-sexp 1) | |
541 | (forward-sexp 2)) | |
542 | (forward-line 1) | |
543 | (<= (point) opoint)) | |
65fe45f2 RS |
544 | (if (looking-at " *;;;") |
545 | (forward-line 1)) | |
0231f2dc JB |
546 | (setq count (1+ count))) |
547 | count))) | |
548 | ||
0231f2dc JB |
549 | (defun debugger-frame () |
550 | "Request entry to debugger when this frame exits. | |
551 | Applies to the frame whose line point is on in the backtrace." | |
552 | (interactive) | |
fa5b1b57 RS |
553 | (save-excursion |
554 | (beginning-of-line) | |
555 | (if (looking-at " *;;;\\|[a-z]") | |
556 | (error "This line is not a function call"))) | |
0231f2dc | 557 | (beginning-of-line) |
27bde5f0 | 558 | (backtrace-debug (debugger-frame-number) t) |
0231f2dc | 559 | (if (= (following-char) ? ) |
9b530428 | 560 | (let ((inhibit-read-only t)) |
0231f2dc JB |
561 | (delete-char 1) |
562 | (insert ?*))) | |
563 | (beginning-of-line)) | |
564 | ||
565 | (defun debugger-frame-clear () | |
c519f68f | 566 | "Do not enter debugger when this frame exits. |
0231f2dc JB |
567 | Applies to the frame whose line point is on in the backtrace." |
568 | (interactive) | |
fa5b1b57 RS |
569 | (save-excursion |
570 | (beginning-of-line) | |
571 | (if (looking-at " *;;;\\|[a-z]") | |
572 | (error "This line is not a function call"))) | |
0231f2dc | 573 | (beginning-of-line) |
27bde5f0 | 574 | (backtrace-debug (debugger-frame-number) nil) |
0231f2dc | 575 | (if (= (following-char) ?*) |
9b530428 | 576 | (let ((inhibit-read-only t)) |
0231f2dc JB |
577 | (delete-char 1) |
578 | (insert ? ))) | |
579 | (beginning-of-line)) | |
580 | ||
8c1cd093 KH |
581 | (defmacro debugger-env-macro (&rest body) |
582 | "Run BODY in original environment." | |
f291fe60 | 583 | (declare (indent 0)) |
6c2599ed SS |
584 | `(save-excursion |
585 | (if (null (buffer-name debugger-old-buffer)) | |
586 | ;; old buffer deleted | |
587 | (setq debugger-old-buffer (current-buffer))) | |
588 | (set-buffer debugger-old-buffer) | |
589 | (let ((load-read-function debugger-outer-load-read-function) | |
590 | (overriding-terminal-local-map | |
591 | debugger-outer-overriding-terminal-local-map) | |
592 | (overriding-local-map debugger-outer-overriding-local-map) | |
593 | (track-mouse debugger-outer-track-mouse) | |
594 | (last-command debugger-outer-last-command) | |
595 | (this-command debugger-outer-this-command) | |
6c2599ed SS |
596 | (unread-command-events debugger-outer-unread-command-events) |
597 | (unread-post-input-method-events | |
598 | debugger-outer-unread-post-input-method-events) | |
599 | (last-input-event debugger-outer-last-input-event) | |
600 | (last-command-event debugger-outer-last-command-event) | |
601 | (last-nonmenu-event debugger-outer-last-nonmenu-event) | |
602 | (last-event-frame debugger-outer-last-event-frame) | |
603 | (standard-input debugger-outer-standard-input) | |
604 | (standard-output debugger-outer-standard-output) | |
605 | (inhibit-redisplay debugger-outer-inhibit-redisplay) | |
606 | (cursor-in-echo-area debugger-outer-cursor-in-echo-area)) | |
607 | (set-match-data debugger-outer-match-data) | |
12cf32ce | 608 | (prog1 |
bfeae2cf | 609 | (progn ,@body) |
6c2599ed SS |
610 | (setq debugger-outer-match-data (match-data)) |
611 | (setq debugger-outer-load-read-function load-read-function) | |
612 | (setq debugger-outer-overriding-terminal-local-map | |
613 | overriding-terminal-local-map) | |
614 | (setq debugger-outer-overriding-local-map overriding-local-map) | |
615 | (setq debugger-outer-track-mouse track-mouse) | |
616 | (setq debugger-outer-last-command last-command) | |
617 | (setq debugger-outer-this-command this-command) | |
6c2599ed SS |
618 | (setq debugger-outer-unread-command-events unread-command-events) |
619 | (setq debugger-outer-unread-post-input-method-events | |
620 | unread-post-input-method-events) | |
621 | (setq debugger-outer-last-input-event last-input-event) | |
622 | (setq debugger-outer-last-command-event last-command-event) | |
623 | (setq debugger-outer-last-nonmenu-event last-nonmenu-event) | |
624 | (setq debugger-outer-last-event-frame last-event-frame) | |
625 | (setq debugger-outer-standard-input standard-input) | |
626 | (setq debugger-outer-standard-output standard-output) | |
627 | (setq debugger-outer-inhibit-redisplay inhibit-redisplay) | |
628 | (setq debugger-outer-cursor-in-echo-area cursor-in-echo-area) | |
629 | )))) | |
8c1cd093 | 630 | |
0231f2dc | 631 | (defun debugger-eval-expression (exp) |
8782c06b RS |
632 | "Eval an expression, in an environment like that outside the debugger." |
633 | (interactive | |
634 | (list (read-from-minibuffer "Eval: " | |
635 | nil read-expression-map t | |
636 | 'read-expression-history))) | |
8c1cd093 | 637 | (debugger-env-macro (eval-expression exp))) |
0231f2dc | 638 | \f |
78d54dee | 639 | (defvar debugger-mode-map |
b2371818 DN |
640 | (let ((map (make-keymap)) |
641 | (menu-map (make-sparse-keymap))) | |
78d54dee SM |
642 | (set-keymap-parent map button-buffer-map) |
643 | (suppress-keymap map) | |
644 | (define-key map "-" 'negative-argument) | |
645 | (define-key map "b" 'debugger-frame) | |
646 | (define-key map "c" 'debugger-continue) | |
647 | (define-key map "j" 'debugger-jump) | |
648 | (define-key map "r" 'debugger-return-value) | |
649 | (define-key map "u" 'debugger-frame-clear) | |
650 | (define-key map "d" 'debugger-step-through) | |
651 | (define-key map "l" 'debugger-list-functions) | |
652 | (define-key map "h" 'describe-mode) | |
653 | (define-key map "q" 'top-level) | |
654 | (define-key map "e" 'debugger-eval-expression) | |
655 | (define-key map " " 'next-line) | |
656 | (define-key map "R" 'debugger-record-expression) | |
245f0222 | 657 | (define-key map "\C-m" 'debug-help-follow) |
78d54dee | 658 | (define-key map [mouse-2] 'push-button) |
b2371818 DN |
659 | (define-key map [menu-bar debugger] (cons "Debugger" menu-map)) |
660 | (define-key menu-map [deb-top] | |
661 | '(menu-item "Quit" top-level | |
f6ec6415 | 662 | :help "Quit debugging and return to top level")) |
b2371818 DN |
663 | (define-key menu-map [deb-s0] '("--")) |
664 | (define-key menu-map [deb-descr] | |
665 | '(menu-item "Describe Debugger Mode" describe-mode | |
666 | :help "Display documentation for debugger-mode")) | |
667 | (define-key menu-map [deb-hfol] | |
668 | '(menu-item "Help Follow" debug-help-follow | |
669 | :help "Follow cross-reference")) | |
670 | (define-key menu-map [deb-nxt] | |
671 | '(menu-item "Next Line" next-line | |
672 | :help "Move cursor down")) | |
673 | (define-key menu-map [deb-s1] '("--")) | |
674 | (define-key menu-map [deb-lfunc] | |
675 | '(menu-item "List debug on entry functions" debugger-list-functions | |
676 | :help "Display a list of all the functions now set to debug on entry")) | |
677 | (define-key menu-map [deb-fclear] | |
678 | '(menu-item "Cancel debug frame" debugger-frame-clear | |
679 | :help "Do not enter debugger when this frame exits")) | |
680 | (define-key menu-map [deb-frame] | |
681 | '(menu-item "Debug frame" debugger-frame | |
682 | :help "Request entry to debugger when this frame exits")) | |
683 | (define-key menu-map [deb-s2] '("--")) | |
684 | (define-key menu-map [deb-ret] | |
685 | '(menu-item "Return value..." debugger-return-value | |
686 | :help "Continue, specifying value to return.")) | |
687 | (define-key menu-map [deb-rec] | |
688 | '(menu-item "Display and Record Expression" debugger-record-expression | |
689 | :help "Display a variable's value and record it in `*Backtrace-record*' buffer")) | |
690 | (define-key menu-map [deb-eval] | |
691 | '(menu-item "Eval Expression..." debugger-eval-expression | |
692 | :help "Eval an expression, in an environment like that outside the debugger")) | |
693 | (define-key menu-map [deb-jump] | |
694 | '(menu-item "Jump" debugger-jump | |
695 | :help "Continue to exit from this frame, with all debug-on-entry suspended")) | |
696 | (define-key menu-map [deb-cont] | |
697 | '(menu-item "Continue" debugger-continue | |
231d8498 | 698 | :help "Continue, evaluating this expression without stopping")) |
b2371818 DN |
699 | (define-key menu-map [deb-step] |
700 | '(menu-item "Step through" debugger-step-through | |
231d8498 | 701 | :help "Proceed, stepping through subexpressions of this expression")) |
78d54dee | 702 | map)) |
028d38a2 RS |
703 | |
704 | (put 'debugger-mode 'mode-class 'special) | |
705 | ||
706 | (defun debugger-mode () | |
707 | "Mode for backtrace buffers, selected in debugger. | |
708 | \\<debugger-mode-map> | |
709 | A line starts with `*' if exiting that frame will call the debugger. | |
710 | Type \\[debugger-frame] or \\[debugger-frame-clear] to set or remove the `*'. | |
711 | ||
712 | When in debugger due to frame being exited, | |
713 | use the \\[debugger-return-value] command to override the value | |
714 | being returned from that frame. | |
715 | ||
716 | Use \\[debug-on-entry] and \\[cancel-debug-on-entry] to control | |
717 | which functions will enter the debugger when called. | |
718 | ||
719 | Complete list of commands: | |
720 | \\{debugger-mode-map}" | |
721 | (kill-all-local-variables) | |
722 | (setq major-mode 'debugger-mode) | |
723 | (setq mode-name "Debugger") | |
724 | (setq truncate-lines t) | |
725 | (set-syntax-table emacs-lisp-mode-syntax-table) | |
726 | (use-local-map debugger-mode-map) | |
727 | (run-mode-hooks 'debugger-mode-hook)) | |
245f0222 | 728 | \f |
666b9413 | 729 | (defcustom debugger-record-buffer "*Debugger-record*" |
cb711556 | 730 | "Buffer name for expression values, for \\[debugger-record-expression]." |
666b9413 | 731 | :type 'string |
cd32a7ba DN |
732 | :group 'debugger |
733 | :version "20.3") | |
8c1cd093 KH |
734 | |
735 | (defun debugger-record-expression (exp) | |
736 | "Display a variable's value and record it in `*Backtrace-record*' buffer." | |
737 | (interactive | |
738 | (list (read-from-minibuffer | |
739 | "Record Eval: " | |
740 | nil | |
741 | read-expression-map t | |
742 | 'read-expression-history))) | |
743 | (let* ((buffer (get-buffer-create debugger-record-buffer)) | |
744 | (standard-output buffer)) | |
745 | (princ (format "Debugger Eval (%s): " exp)) | |
746 | (princ (debugger-eval-expression exp)) | |
747 | (terpri)) | |
748 | ||
749 | (with-current-buffer (get-buffer debugger-record-buffer) | |
8e71c318 | 750 | (message "%s" |
be940bc5 SM |
751 | (buffer-substring (line-beginning-position 0) |
752 | (line-end-position 0))))) | |
0231f2dc | 753 | |
f63b822e GM |
754 | (declare-function help-xref-interned "help-mode" (symbol)) |
755 | ||
245f0222 RS |
756 | (defun debug-help-follow (&optional pos) |
757 | "Follow cross-reference at POS, defaulting to point. | |
758 | ||
759 | For the cross-reference format, see `help-make-xrefs'." | |
760 | (interactive "d") | |
761 | (require 'help-mode) | |
a5f1e154 LT |
762 | ;; Ideally we'd just do (call-interactively 'help-follow) except that this |
763 | ;; assumes we're already in a *Help* buffer and reuses it, so it ends up | |
764 | ;; incorrectly "reusing" the *Backtrace* buffer to show the help info. | |
245f0222 RS |
765 | (unless pos |
766 | (setq pos (point))) | |
767 | (unless (push-button pos) | |
768 | ;; check if the symbol under point is a function or variable | |
769 | (let ((sym | |
770 | (intern | |
771 | (save-excursion | |
772 | (goto-char pos) (skip-syntax-backward "w_") | |
773 | (buffer-substring (point) | |
774 | (progn (skip-syntax-forward "w_") | |
775 | (point))))))) | |
776 | (when (or (boundp sym) (fboundp sym) (facep sym)) | |
1fe3c8f5 | 777 | (help-xref-interned sym))))) |
0231f2dc | 778 | \f |
449808f7 LK |
779 | ;; When you change this, you may also need to change the number of |
780 | ;; frames that the debugger skips. | |
231d8498 | 781 | (defun debug--implement-debug-on-entry (&rest _ignore) |
449808f7 LK |
782 | "Conditionally call the debugger. |
783 | A call to this function is inserted by `debug-on-entry' to cause | |
784 | functions to break on entry." | |
785 | (if (or inhibit-debug-on-entry debugger-jumping-flag) | |
786 | nil | |
787 | (funcall debugger 'debug))) | |
788 | ||
0231f2dc JB |
789 | ;;;###autoload |
790 | (defun debug-on-entry (function) | |
791 | "Request FUNCTION to invoke debugger each time it is called. | |
8ac3941d | 792 | |
8e71c318 | 793 | When called interactively, prompt for FUNCTION in the minibuffer. |
8ac3941d LK |
794 | |
795 | This works by modifying the definition of FUNCTION. If you tell the | |
796 | debugger to continue, FUNCTION's execution proceeds. If FUNCTION is a | |
797 | normal function or a macro written in Lisp, you can also step through | |
798 | its execution. FUNCTION can also be a primitive that is not a special | |
799 | form, in which case stepping is not possible. Break-on-entry for | |
800 | primitive functions only works when that function is called from Lisp. | |
801 | ||
0231f2dc | 802 | Use \\[cancel-debug-on-entry] to cancel the effect of this command. |
2512acba | 803 | Redefining FUNCTION also cancels it." |
ce5ce46d LK |
804 | (interactive |
805 | (let ((fn (function-called-at-point)) val) | |
231d8498 | 806 | (when (special-form-p fn) |
ce5ce46d | 807 | (setq fn nil)) |
3731a850 | 808 | (setq val (completing-read |
ce5ce46d LK |
809 | (if fn |
810 | (format "Debug on entry to function (default %s): " fn) | |
811 | "Debug on entry to function: ") | |
812 | obarray | |
813 | #'(lambda (symbol) | |
814 | (and (fboundp symbol) | |
231d8498 | 815 | (not (special-form-p symbol)))) |
ce5ce46d LK |
816 | t nil nil (symbol-name fn))) |
817 | (list (if (equal val "") fn (intern val))))) | |
231d8498 | 818 | (advice-add function :before #'debug--implement-debug-on-entry) |
0231f2dc JB |
819 | function) |
820 | ||
231d8498 SM |
821 | (defun debug--function-list () |
822 | "List of functions currently set for debug on entry." | |
823 | (let ((funs '())) | |
824 | (mapatoms | |
825 | (lambda (s) | |
826 | (when (advice-member-p #'debug--implement-debug-on-entry s) | |
827 | (push s funs)))) | |
828 | funs)) | |
829 | ||
0231f2dc JB |
830 | ;;;###autoload |
831 | (defun cancel-debug-on-entry (&optional function) | |
832 | "Undo effect of \\[debug-on-entry] on FUNCTION. | |
dd72a03a | 833 | If FUNCTION is nil, cancel debug-on-entry for all functions. |
8e71c318 LT |
834 | When called interactively, prompt for FUNCTION in the minibuffer. |
835 | To specify a nil argument interactively, exit with an empty minibuffer." | |
10a4c11f JB |
836 | (interactive |
837 | (list (let ((name | |
922a9de3 | 838 | (completing-read |
5b76833f | 839 | "Cancel debug on entry to function (default all functions): " |
231d8498 | 840 | (mapcar #'symbol-name (debug--function-list)) nil t))) |
dd72a03a LK |
841 | (when name |
842 | (unless (string= name "") | |
843 | (intern name)))))) | |
231d8498 | 844 | (if function |
0231f2dc | 845 | (progn |
231d8498 | 846 | (advice-remove function #'debug--implement-debug-on-entry) |
0231f2dc JB |
847 | function) |
848 | (message "Cancelling debug-on-entry for all functions") | |
231d8498 | 849 | (mapcar #'cancel-debug-on-entry (debug--function-list)))) |
0231f2dc JB |
850 | |
851 | (defun debugger-list-functions () | |
852 | "Display a list of all the functions now set to debug on entry." | |
853 | (interactive) | |
78d54dee | 854 | (require 'help-mode) |
32226619 JB |
855 | (help-setup-xref '(debugger-list-functions) |
856 | (called-interactively-p 'interactive)) | |
78d54dee SM |
857 | (with-output-to-temp-buffer (help-buffer) |
858 | (with-current-buffer standard-output | |
231d8498 SM |
859 | (let ((funs (debug--function-list))) |
860 | (if (null funs) | |
861 | (princ "No debug-on-entry functions now\n") | |
862 | (princ "Functions set to debug on entry:\n\n") | |
863 | (dolist (fun funs) | |
864 | (make-text-button (point) (progn (prin1 fun) (point)) | |
865 | 'type 'help-function | |
866 | 'help-args (list fun)) | |
867 | (terpri)) | |
868 | (terpri) | |
869 | (princ "Note: if you have redefined a function, then it may no longer\n") | |
870 | (princ "be set to debug on entry, even if it is in the list.")))))) | |
c0274f38 | 871 | |
896546cd RS |
872 | (provide 'debug) |
873 | ||
c0274f38 | 874 | ;;; debug.el ends here |