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