Commit | Line | Data |
---|---|---|
c0274f38 ER |
1 | ;;; debug.el --- debuggers and related commands for Emacs |
2 | ||
2f74c84b | 3 | ;; Copyright (C) 1985, 1986, 1994, 2001, 2003 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 | ||
10 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 | ;; it under the terms of the GNU General Public License as published by | |
e5167999 | 12 | ;; the Free Software Foundation; either version 2, or (at your option) |
0231f2dc JB |
13 | ;; any later version. |
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 | |
b578f267 EN |
21 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
22 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 | ;; Boston, MA 02111-1307, USA. | |
0231f2dc | 24 | |
e41b2db1 ER |
25 | ;;; Commentary: |
26 | ||
27 | ;; This is a major mode documented in the Emacs manual. | |
28 | ||
e5167999 | 29 | ;;; Code: |
0231f2dc | 30 | |
bb9a622e MB |
31 | (require 'button) |
32 | ||
666b9413 SE |
33 | (defgroup debugger nil |
34 | "Debuggers and related commands for Emacs." | |
35 | :prefix "debugger-" | |
36 | :group 'debug) | |
8c1cd093 | 37 | |
666b9413 SE |
38 | (defcustom debugger-mode-hook nil |
39 | "*Hooks run when `debugger-mode' is turned on." | |
40 | :type 'hook | |
cd32a7ba DN |
41 | :group 'debugger |
42 | :version "20.3") | |
8c1cd093 | 43 | |
63ca439e GM |
44 | (defcustom debugger-batch-max-lines 40 |
45 | "*Maximum lines to show in debugger buffer in a noninteractive Emacs. | |
46 | When the debugger is entered and Emacs is running in batch mode, | |
47 | if the backtrace text has more than this many lines, | |
48 | the middle is discarded, and just the beginning and end are displayed." | |
49 | :type 'integer | |
50 | :group 'debugger | |
51 | :version "21.1") | |
0231f2dc | 52 | |
666b9413 SE |
53 | (defcustom debug-function-list nil |
54 | "List of functions currently set for debug on entry." | |
55 | :type '(repeat function) | |
56 | :group 'debugger) | |
57 | ||
58 | (defcustom debugger-step-after-exit nil | |
59 | "Non-nil means \"single-step\" after the debugger exits." | |
60 | :type 'boolean | |
61 | :group 'debugger) | |
2eeeb0d2 RS |
62 | |
63 | (defvar debugger-value nil | |
64 | "This is the value for the debugger to return, when it returns.") | |
65 | ||
66 | (defvar debugger-old-buffer nil | |
67 | "This is the buffer that was current when the debugger was entered.") | |
68 | ||
25c58854 RS |
69 | (defvar debugger-previous-backtrace nil |
70 | "The contents of the previous backtrace (including text properties). | |
71 | This is to optimize `debugger-make-xrefs'.") | |
72 | ||
cf1c8cd9 | 73 | (defvar debugger-outer-match-data) |
80ddb8ed RS |
74 | (defvar debugger-outer-load-read-function) |
75 | (defvar debugger-outer-overriding-local-map) | |
29e2b496 | 76 | (defvar debugger-outer-overriding-terminal-local-map) |
35cf010d RS |
77 | (defvar debugger-outer-track-mouse) |
78 | (defvar debugger-outer-last-command) | |
79 | (defvar debugger-outer-this-command) | |
c82fbd0c RS |
80 | ;; unread-command-char is obsolete, |
81 | ;; but we still save and restore it | |
82 | ;; in case some user program still tries to set it. | |
35cf010d RS |
83 | (defvar debugger-outer-unread-command-char) |
84 | (defvar debugger-outer-unread-command-events) | |
0359db82 | 85 | (defvar debugger-outer-unread-post-input-method-events) |
35cf010d RS |
86 | (defvar debugger-outer-last-input-event) |
87 | (defvar debugger-outer-last-command-event) | |
88 | (defvar debugger-outer-last-nonmenu-event) | |
89 | (defvar debugger-outer-last-event-frame) | |
90 | (defvar debugger-outer-standard-input) | |
91 | (defvar debugger-outer-standard-output) | |
bf90c2c0 | 92 | (defvar debugger-outer-inhibit-redisplay) |
35cf010d RS |
93 | (defvar debugger-outer-cursor-in-echo-area) |
94 | ||
0231f2dc JB |
95 | ;;;###autoload |
96 | (setq debugger 'debug) | |
97 | ;;;###autoload | |
98 | (defun debug (&rest debugger-args) | |
2512acba | 99 | "Enter debugger. To return, type \\<debugger-mode-map>`\\[debugger-continue]'. |
0231f2dc JB |
100 | Arguments are mainly for use when this is called from the internals |
101 | of the evaluator. | |
102 | ||
103 | You may call with no args, or you may pass nil as the first arg and | |
104 | any other args you like. In that case, the list of args after the | |
105 | first will be printed into the backtrace buffer." | |
73e78cf1 | 106 | (interactive) |
e734f584 RS |
107 | (if inhibit-redisplay |
108 | ;; Don't really try to enter debugger within an eval from redisplay. | |
109 | debugger-value | |
110 | (unless noninteractive | |
111 | (message "Entering debugger...")) | |
112 | (let (debugger-value | |
113 | (debug-on-error nil) | |
114 | (debug-on-quit nil) | |
115 | (debugger-buffer (let ((default-major-mode 'fundamental-mode)) | |
116 | (get-buffer-create "*Backtrace*"))) | |
117 | (debugger-old-buffer (current-buffer)) | |
118 | (debugger-step-after-exit nil) | |
119 | ;; Don't keep reading from an executing kbd macro! | |
120 | (executing-kbd-macro nil) | |
121 | ;; Save the outer values of these vars for the `e' command | |
122 | ;; before we replace the values. | |
123 | (debugger-outer-match-data (match-data)) | |
124 | (debugger-outer-load-read-function load-read-function) | |
125 | (debugger-outer-overriding-local-map overriding-local-map) | |
126 | (debugger-outer-overriding-terminal-local-map | |
127 | overriding-terminal-local-map) | |
128 | (debugger-outer-track-mouse track-mouse) | |
129 | (debugger-outer-last-command last-command) | |
130 | (debugger-outer-this-command this-command) | |
131 | (debugger-outer-unread-command-char unread-command-char) | |
132 | (debugger-outer-unread-command-events unread-command-events) | |
133 | (debugger-outer-unread-post-input-method-events | |
134 | unread-post-input-method-events) | |
135 | (debugger-outer-last-input-event last-input-event) | |
136 | (debugger-outer-last-command-event last-command-event) | |
137 | (debugger-outer-last-nonmenu-event last-nonmenu-event) | |
138 | (debugger-outer-last-event-frame last-event-frame) | |
139 | (debugger-outer-standard-input standard-input) | |
140 | (debugger-outer-standard-output standard-output) | |
141 | (debugger-outer-inhibit-redisplay inhibit-redisplay) | |
142 | (debugger-outer-cursor-in-echo-area cursor-in-echo-area)) | |
143 | ;; Set this instead of binding it, so that `q' | |
144 | ;; will not restore it. | |
145 | (setq overriding-terminal-local-map nil) | |
146 | ;; Don't let these magic variables affect the debugger itself. | |
147 | (let ((last-command nil) this-command track-mouse | |
148 | (unread-command-char -1) unread-command-events | |
149 | unread-post-input-method-events | |
150 | last-input-event last-command-event last-nonmenu-event | |
151 | last-event-frame | |
152 | overriding-local-map | |
153 | load-read-function | |
154 | ;; If we are inside a minibuffer, allow nesting | |
155 | ;; so that we don't get an error from the `e' command. | |
156 | (enable-recursive-minibuffers | |
157 | (or enable-recursive-minibuffers (> (minibuffer-depth) 0))) | |
158 | (standard-input t) (standard-output t) | |
159 | inhibit-redisplay | |
160 | (cursor-in-echo-area nil)) | |
161 | (unwind-protect | |
162 | (save-excursion | |
163 | (save-window-excursion | |
164 | (pop-to-buffer debugger-buffer) | |
165 | (debugger-mode) | |
166 | (debugger-setup-buffer debugger-args) | |
167 | (when noninteractive | |
168 | ;; If the backtrace is long, save the beginning | |
169 | ;; and the end, but discard the middle. | |
170 | (when (> (count-lines (point-min) (point-max)) | |
171 | debugger-batch-max-lines) | |
172 | (goto-char (point-min)) | |
173 | (forward-line (/ 2 debugger-batch-max-lines)) | |
174 | (let ((middlestart (point))) | |
175 | (goto-char (point-max)) | |
176 | (forward-line (- (/ 2 debugger-batch-max-lines) | |
177 | debugger-batch-max-lines)) | |
178 | (delete-region middlestart (point))) | |
179 | (insert "...\n")) | |
63ca439e | 180 | (goto-char (point-min)) |
2f74c84b | 181 | (message "%s" (buffer-string)) |
e734f584 RS |
182 | (kill-emacs)) |
183 | (if (eq (car debugger-args) 'debug) | |
184 | ;; Skip the frames for backtrace-debug, byte-code, and debug. | |
185 | (backtrace-debug 3 t)) | |
186 | (debugger-reenable) | |
35cf010d | 187 | (message "") |
e734f584 RS |
188 | (let ((inhibit-trace t) |
189 | (standard-output nil) | |
190 | (buffer-read-only t)) | |
191 | (message "") | |
192 | ;; Make sure we unbind buffer-read-only in the right buffer. | |
193 | (save-excursion | |
194 | (recursive-edit))))) | |
195 | ;; Kill or at least neuter the backtrace buffer, so that users | |
196 | ;; don't try to execute debugger commands in an invalid context. | |
197 | (if (get-buffer-window debugger-buffer 'visible) | |
198 | ;; Still visible despite the save-window-excursion? Maybe it | |
199 | ;; it's in a pop-up frame. It would be annoying to delete and | |
200 | ;; recreate it every time the debugger stops, so instead we'll | |
201 | ;; erase it but leave it visible. | |
202 | (save-excursion | |
203 | (set-buffer debugger-buffer) | |
204 | (erase-buffer) | |
205 | (fundamental-mode)) | |
206 | (kill-buffer debugger-buffer)) | |
207 | (set-match-data debugger-outer-match-data))) | |
208 | ;; Put into effect the modified values of these variables | |
209 | ;; in case the user set them with the `e' command. | |
210 | (setq load-read-function debugger-outer-load-read-function) | |
211 | (setq overriding-local-map debugger-outer-overriding-local-map) | |
212 | (setq overriding-terminal-local-map | |
213 | debugger-outer-overriding-terminal-local-map) | |
214 | (setq track-mouse debugger-outer-track-mouse) | |
215 | (setq last-command debugger-outer-last-command) | |
216 | (setq this-command debugger-outer-this-command) | |
217 | (setq unread-command-char debugger-outer-unread-command-char) | |
218 | (setq unread-command-events debugger-outer-unread-command-events) | |
219 | (setq unread-post-input-method-events | |
220 | debugger-outer-unread-post-input-method-events) | |
221 | (setq last-input-event debugger-outer-last-input-event) | |
222 | (setq last-command-event debugger-outer-last-command-event) | |
223 | (setq last-nonmenu-event debugger-outer-last-nonmenu-event) | |
224 | (setq last-event-frame debugger-outer-last-event-frame) | |
225 | (setq standard-input debugger-outer-standard-input) | |
226 | (setq standard-output debugger-outer-standard-output) | |
227 | (setq inhibit-redisplay debugger-outer-inhibit-redisplay) | |
228 | (setq cursor-in-echo-area debugger-outer-cursor-in-echo-area) | |
229 | (setq debug-on-next-call debugger-step-after-exit) | |
230 | debugger-value))) | |
0231f2dc | 231 | \f |
63ca439e GM |
232 | (defun debugger-setup-buffer (debugger-args) |
233 | "Initialize the `*Backtrace*' buffer for entry to the debugger. | |
234 | That buffer should be current already." | |
235 | (setq buffer-read-only nil) | |
236 | (erase-buffer) | |
237 | (set-buffer-multibyte nil) | |
238 | (let ((standard-output (current-buffer)) | |
239 | (print-escape-newlines t) | |
240 | (print-level 8) | |
241 | (print-length 50)) | |
242 | (backtrace)) | |
243 | (goto-char (point-min)) | |
244 | (delete-region (point) | |
245 | (progn | |
246 | (search-forward "\n debug(") | |
247 | (forward-line 1) | |
248 | (point))) | |
249 | (insert "Debugger entered") | |
250 | ;; lambda is for debug-on-call when a function call is next. | |
251 | ;; debug is for debug-on-entry function called. | |
252 | (cond ((memq (car debugger-args) '(lambda debug)) | |
253 | (insert "--entering a function:\n") | |
254 | (if (eq (car debugger-args) 'debug) | |
255 | (progn | |
256 | (delete-char 1) | |
257 | (insert ?*) | |
258 | (beginning-of-line)))) | |
259 | ;; Exiting a function. | |
260 | ((eq (car debugger-args) 'exit) | |
261 | (insert "--returning value: ") | |
262 | (setq debugger-value (nth 1 debugger-args)) | |
263 | (prin1 debugger-value (current-buffer)) | |
264 | (insert ?\n) | |
265 | (delete-char 1) | |
266 | (insert ? ) | |
267 | (beginning-of-line)) | |
268 | ;; Debugger entered for an error. | |
269 | ((eq (car debugger-args) 'error) | |
270 | (insert "--Lisp error: ") | |
271 | (prin1 (nth 1 debugger-args) (current-buffer)) | |
272 | (insert ?\n)) | |
273 | ;; debug-on-call, when the next thing is an eval. | |
274 | ((eq (car debugger-args) t) | |
275 | (insert "--beginning evaluation of function call form:\n")) | |
276 | ;; User calls debug directly. | |
277 | (t | |
278 | (insert ": ") | |
279 | (prin1 (if (eq (car debugger-args) 'nil) | |
280 | (cdr debugger-args) debugger-args) | |
281 | (current-buffer)) | |
cc1bde62 | 282 | (insert ?\n))) |
27bde5f0 RS |
283 | ;; After any frame that uses eval-buffer, |
284 | ;; insert a line that states the buffer position it's reading at. | |
285 | (save-excursion | |
286 | (while (re-search-forward "^ eval-buffer(" nil t) | |
287 | (end-of-line) | |
288 | (insert (format "\n ;;; Reading at buffer position %d" | |
289 | (with-current-buffer (nth 2 (backtrace-frame (debugger-frame-number))) | |
65fe45f2 | 290 | (point)))))) |
cc1bde62 SS |
291 | (debugger-make-xrefs)) |
292 | ||
293 | (defun debugger-make-xrefs (&optional buffer) | |
25c58854 | 294 | "Attach cross-references to symbol names in the `*Backtrace*' buffer." |
cc1bde62 SS |
295 | (interactive "b") |
296 | (save-excursion | |
297 | (set-buffer (or buffer (current-buffer))) | |
25c58854 RS |
298 | (setq buffer (current-buffer)) |
299 | (let ((buffer-read-only nil) | |
6836bb69 | 300 | (old-end (point-min)) (new-end (point-min))) |
25c58854 RS |
301 | ;; If we saved an old backtrace, find the common part |
302 | ;; between the new and the old. | |
303 | ;; Compare line by line, starting from the end, | |
304 | ;; because that's the part that is likely to be unchanged. | |
305 | (if debugger-previous-backtrace | |
306 | (let (old-start new-start (all-match t)) | |
307 | (goto-char (point-max)) | |
308 | (with-temp-buffer | |
309 | (insert debugger-previous-backtrace) | |
310 | (while (and all-match (not (bobp))) | |
311 | (setq old-end (point)) | |
312 | (forward-line -1) | |
313 | (setq old-start (point)) | |
314 | (with-current-buffer buffer | |
315 | (setq new-end (point)) | |
316 | (forward-line -1) | |
317 | (setq new-start (point))) | |
318 | (if (not (zerop | |
319 | (compare-buffer-substrings | |
320 | (current-buffer) old-start old-end | |
321 | buffer new-start new-end))) | |
322 | (setq all-match nil)))) | |
323 | ;; Now new-end is the position of the start of the | |
324 | ;; unchanged part in the current buffer, and old-end is | |
325 | ;; the position of that same text in the saved old | |
6836bb69 | 326 | ;; backtrace. But we must subtract (point-min) since strings are |
25c58854 RS |
327 | ;; indexed in origin 0. |
328 | ||
329 | ;; Replace the unchanged part of the backtrace | |
330 | ;; with the text from debugger-previous-backtrace, | |
331 | ;; since that already has the proper xrefs. | |
332 | ;; With this optimization, we only need to scan | |
333 | ;; the changed part of the backtrace. | |
334 | (delete-region new-end (point-max)) | |
335 | (goto-char (point-max)) | |
6836bb69 SM |
336 | (insert (substring debugger-previous-backtrace |
337 | (- old-end (point-min)))) | |
25c58854 RS |
338 | ;; Make the unchanged part of the backtrace inaccessible |
339 | ;; so it won't be scanned. | |
340 | (narrow-to-region (point-min) new-end))) | |
a1506d29 | 341 | |
25c58854 RS |
342 | ;; Scan the new part of the backtrace, inserting xrefs. |
343 | (goto-char (point-min)) | |
344 | (while (progn | |
345 | (skip-syntax-forward "^w_") | |
346 | (not (eobp))) | |
347 | (let* ((beg (point)) | |
348 | (end (progn (skip-syntax-forward "w_") (point))) | |
349 | (sym (intern-soft (buffer-substring-no-properties | |
350 | beg end))) | |
351 | (file (and sym (symbol-file sym)))) | |
25c58854 RS |
352 | (when file |
353 | (goto-char beg) | |
354 | ;; help-xref-button needs to operate on something matched | |
355 | ;; by a regexp, so set that up for it. | |
356 | (re-search-forward "\\(\\(\\sw\\|\\s_\\)+\\)") | |
f27b0335 RS |
357 | (help-xref-button 1 'help-function-def sym file))) |
358 | (forward-line 1)) | |
25c58854 RS |
359 | (widen)) |
360 | (setq debugger-previous-backtrace (buffer-string)))) | |
63ca439e | 361 | \f |
0231f2dc JB |
362 | (defun debugger-step-through () |
363 | "Proceed, stepping through subexpressions of this expression. | |
364 | Enter another debugger on next entry to eval, apply or funcall." | |
365 | (interactive) | |
366 | (setq debugger-step-after-exit t) | |
367 | (message "Proceeding, will debug on next eval or call.") | |
368 | (exit-recursive-edit)) | |
369 | ||
370 | (defun debugger-continue () | |
371 | "Continue, evaluating this expression without stopping." | |
372 | (interactive) | |
094e0928 GM |
373 | (unless debugger-may-continue |
374 | (error "Cannot continue")) | |
0231f2dc JB |
375 | (message "Continuing.") |
376 | (exit-recursive-edit)) | |
377 | ||
378 | (defun debugger-return-value (val) | |
379 | "Continue, specifying value to return. | |
380 | This is only useful when the value returned from the debugger | |
381 | will be used, such as in a debug on exit from a frame." | |
382 | (interactive "XReturn value (evaluated): ") | |
383 | (setq debugger-value val) | |
384 | (princ "Returning " t) | |
385 | (prin1 debugger-value) | |
386 | (exit-recursive-edit)) | |
387 | ||
388 | (defun debugger-jump () | |
389 | "Continue to exit from this frame, with all debug-on-entry suspended." | |
390 | (interactive) | |
27bde5f0 | 391 | (debugger-frame) |
0231f2dc JB |
392 | ;; Turn off all debug-on-entry functions |
393 | ;; but leave them in the list. | |
394 | (let ((list debug-function-list)) | |
395 | (while list | |
396 | (fset (car list) | |
397 | (debug-on-entry-1 (car list) (symbol-function (car list)) nil)) | |
398 | (setq list (cdr list)))) | |
399 | (message "Continuing through this frame") | |
400 | (exit-recursive-edit)) | |
401 | ||
402 | (defun debugger-reenable () | |
403 | "Turn all debug-on-entry functions back on." | |
404 | (let ((list debug-function-list)) | |
405 | (while list | |
406 | (or (consp (symbol-function (car list))) | |
407 | (debug-convert-byte-code (car list))) | |
408 | (fset (car list) | |
409 | (debug-on-entry-1 (car list) (symbol-function (car list)) t)) | |
410 | (setq list (cdr list))))) | |
411 | ||
412 | (defun debugger-frame-number () | |
413 | "Return number of frames in backtrace before the one point points at." | |
414 | (save-excursion | |
415 | (beginning-of-line) | |
416 | (let ((opoint (point)) | |
417 | (count 0)) | |
27bde5f0 RS |
418 | (while (not (eq (cadr (backtrace-frame count)) 'debug)) |
419 | (setq count (1+ count))) | |
0231f2dc JB |
420 | (goto-char (point-min)) |
421 | (if (or (equal (buffer-substring (point) (+ (point) 6)) | |
422 | "Signal") | |
423 | (equal (buffer-substring (point) (+ (point) 6)) | |
424 | "Return")) | |
425 | (progn | |
426 | (search-forward ":") | |
427 | (forward-sexp 1))) | |
428 | (forward-line 1) | |
429 | (while (progn | |
430 | (forward-char 2) | |
431 | (if (= (following-char) ?\() | |
432 | (forward-sexp 1) | |
433 | (forward-sexp 2)) | |
434 | (forward-line 1) | |
435 | (<= (point) opoint)) | |
65fe45f2 RS |
436 | (if (looking-at " *;;;") |
437 | (forward-line 1)) | |
0231f2dc JB |
438 | (setq count (1+ count))) |
439 | count))) | |
440 | ||
0231f2dc JB |
441 | (defun debugger-frame () |
442 | "Request entry to debugger when this frame exits. | |
443 | Applies to the frame whose line point is on in the backtrace." | |
444 | (interactive) | |
fa5b1b57 RS |
445 | (save-excursion |
446 | (beginning-of-line) | |
447 | (if (looking-at " *;;;\\|[a-z]") | |
448 | (error "This line is not a function call"))) | |
0231f2dc | 449 | (beginning-of-line) |
27bde5f0 | 450 | (backtrace-debug (debugger-frame-number) t) |
0231f2dc JB |
451 | (if (= (following-char) ? ) |
452 | (let ((buffer-read-only nil)) | |
453 | (delete-char 1) | |
454 | (insert ?*))) | |
455 | (beginning-of-line)) | |
456 | ||
457 | (defun debugger-frame-clear () | |
c519f68f | 458 | "Do not enter debugger when this frame exits. |
0231f2dc JB |
459 | Applies to the frame whose line point is on in the backtrace." |
460 | (interactive) | |
fa5b1b57 RS |
461 | (save-excursion |
462 | (beginning-of-line) | |
463 | (if (looking-at " *;;;\\|[a-z]") | |
464 | (error "This line is not a function call"))) | |
0231f2dc | 465 | (beginning-of-line) |
27bde5f0 | 466 | (backtrace-debug (debugger-frame-number) nil) |
0231f2dc JB |
467 | (if (= (following-char) ?*) |
468 | (let ((buffer-read-only nil)) | |
469 | (delete-char 1) | |
470 | (insert ? ))) | |
471 | (beginning-of-line)) | |
472 | ||
8c1cd093 KH |
473 | |
474 | ||
475 | (put 'debugger-env-macro 'lisp-indent-function 0) | |
476 | (defmacro debugger-env-macro (&rest body) | |
477 | "Run BODY in original environment." | |
6c2599ed SS |
478 | `(save-excursion |
479 | (if (null (buffer-name debugger-old-buffer)) | |
480 | ;; old buffer deleted | |
481 | (setq debugger-old-buffer (current-buffer))) | |
482 | (set-buffer debugger-old-buffer) | |
483 | (let ((load-read-function debugger-outer-load-read-function) | |
484 | (overriding-terminal-local-map | |
485 | debugger-outer-overriding-terminal-local-map) | |
486 | (overriding-local-map debugger-outer-overriding-local-map) | |
487 | (track-mouse debugger-outer-track-mouse) | |
488 | (last-command debugger-outer-last-command) | |
489 | (this-command debugger-outer-this-command) | |
490 | (unread-command-char debugger-outer-unread-command-char) | |
491 | (unread-command-events debugger-outer-unread-command-events) | |
492 | (unread-post-input-method-events | |
493 | debugger-outer-unread-post-input-method-events) | |
494 | (last-input-event debugger-outer-last-input-event) | |
495 | (last-command-event debugger-outer-last-command-event) | |
496 | (last-nonmenu-event debugger-outer-last-nonmenu-event) | |
497 | (last-event-frame debugger-outer-last-event-frame) | |
498 | (standard-input debugger-outer-standard-input) | |
499 | (standard-output debugger-outer-standard-output) | |
500 | (inhibit-redisplay debugger-outer-inhibit-redisplay) | |
501 | (cursor-in-echo-area debugger-outer-cursor-in-echo-area)) | |
502 | (set-match-data debugger-outer-match-data) | |
503 | (prog1 (progn ,@body) | |
504 | (setq debugger-outer-match-data (match-data)) | |
505 | (setq debugger-outer-load-read-function load-read-function) | |
506 | (setq debugger-outer-overriding-terminal-local-map | |
507 | overriding-terminal-local-map) | |
508 | (setq debugger-outer-overriding-local-map overriding-local-map) | |
509 | (setq debugger-outer-track-mouse track-mouse) | |
510 | (setq debugger-outer-last-command last-command) | |
511 | (setq debugger-outer-this-command this-command) | |
512 | (setq debugger-outer-unread-command-char unread-command-char) | |
513 | (setq debugger-outer-unread-command-events unread-command-events) | |
514 | (setq debugger-outer-unread-post-input-method-events | |
515 | unread-post-input-method-events) | |
516 | (setq debugger-outer-last-input-event last-input-event) | |
517 | (setq debugger-outer-last-command-event last-command-event) | |
518 | (setq debugger-outer-last-nonmenu-event last-nonmenu-event) | |
519 | (setq debugger-outer-last-event-frame last-event-frame) | |
520 | (setq debugger-outer-standard-input standard-input) | |
521 | (setq debugger-outer-standard-output standard-output) | |
522 | (setq debugger-outer-inhibit-redisplay inhibit-redisplay) | |
523 | (setq debugger-outer-cursor-in-echo-area cursor-in-echo-area) | |
524 | )))) | |
8c1cd093 | 525 | |
0231f2dc | 526 | (defun debugger-eval-expression (exp) |
8782c06b RS |
527 | "Eval an expression, in an environment like that outside the debugger." |
528 | (interactive | |
529 | (list (read-from-minibuffer "Eval: " | |
530 | nil read-expression-map t | |
531 | 'read-expression-history))) | |
8c1cd093 | 532 | (debugger-env-macro (eval-expression exp))) |
0231f2dc JB |
533 | \f |
534 | (defvar debugger-mode-map nil) | |
cc1bde62 | 535 | (unless debugger-mode-map |
0231f2dc JB |
536 | (let ((loop ? )) |
537 | (setq debugger-mode-map (make-keymap)) | |
bb9a622e | 538 | (set-keymap-parent debugger-mode-map button-buffer-map) |
0231f2dc JB |
539 | (suppress-keymap debugger-mode-map) |
540 | (define-key debugger-mode-map "-" 'negative-argument) | |
541 | (define-key debugger-mode-map "b" 'debugger-frame) | |
542 | (define-key debugger-mode-map "c" 'debugger-continue) | |
543 | (define-key debugger-mode-map "j" 'debugger-jump) | |
544 | (define-key debugger-mode-map "r" 'debugger-return-value) | |
545 | (define-key debugger-mode-map "u" 'debugger-frame-clear) | |
546 | (define-key debugger-mode-map "d" 'debugger-step-through) | |
547 | (define-key debugger-mode-map "l" 'debugger-list-functions) | |
548 | (define-key debugger-mode-map "h" 'describe-mode) | |
549 | (define-key debugger-mode-map "q" 'top-level) | |
550 | (define-key debugger-mode-map "e" 'debugger-eval-expression) | |
8c1cd093 KH |
551 | (define-key debugger-mode-map " " 'next-line) |
552 | (define-key debugger-mode-map "R" 'debugger-record-expression) | |
f27b0335 | 553 | (define-key debugger-mode-map "\C-m" 'help-follow) |
cc1bde62 | 554 | (define-key debugger-mode-map [mouse-2] 'push-button) |
8c1cd093 KH |
555 | )) |
556 | ||
557 | ||
666b9413 SE |
558 | (defcustom debugger-record-buffer "*Debugger-record*" |
559 | "*Buffer name for expression values, for \\[debugger-record-expression]." | |
560 | :type 'string | |
cd32a7ba DN |
561 | :group 'debugger |
562 | :version "20.3") | |
8c1cd093 KH |
563 | |
564 | (defun debugger-record-expression (exp) | |
565 | "Display a variable's value and record it in `*Backtrace-record*' buffer." | |
566 | (interactive | |
567 | (list (read-from-minibuffer | |
568 | "Record Eval: " | |
569 | nil | |
570 | read-expression-map t | |
571 | 'read-expression-history))) | |
572 | (let* ((buffer (get-buffer-create debugger-record-buffer)) | |
573 | (standard-output buffer)) | |
574 | (princ (format "Debugger Eval (%s): " exp)) | |
575 | (princ (debugger-eval-expression exp)) | |
576 | (terpri)) | |
577 | ||
578 | (with-current-buffer (get-buffer debugger-record-buffer) | |
579 | (save-excursion | |
580 | (forward-line -1) | |
581 | (message | |
582 | (buffer-substring (point) (progn (end-of-line) (point))))))) | |
0231f2dc JB |
583 | |
584 | (put 'debugger-mode 'mode-class 'special) | |
585 | ||
586 | (defun debugger-mode () | |
587 | "Mode for backtrace buffers, selected in debugger. | |
588 | \\<debugger-mode-map> | |
589 | A line starts with `*' if exiting that frame will call the debugger. | |
590 | Type \\[debugger-frame] or \\[debugger-frame-clear] to set or remove the `*'. | |
591 | ||
592 | When in debugger due to frame being exited, | |
593 | use the \\[debugger-return-value] command to override the value | |
594 | being returned from that frame. | |
595 | ||
596 | Use \\[debug-on-entry] and \\[cancel-debug-on-entry] to control | |
597 | which functions will enter the debugger when called. | |
598 | ||
599 | Complete list of commands: | |
600 | \\{debugger-mode-map}" | |
8c1cd093 | 601 | (kill-all-local-variables) |
0231f2dc JB |
602 | (setq major-mode 'debugger-mode) |
603 | (setq mode-name "Debugger") | |
604 | (setq truncate-lines t) | |
605 | (set-syntax-table emacs-lisp-mode-syntax-table) | |
8c1cd093 KH |
606 | (use-local-map debugger-mode-map) |
607 | (run-hooks 'debugger-mode-hook)) | |
0231f2dc JB |
608 | \f |
609 | ;;;###autoload | |
610 | (defun debug-on-entry (function) | |
611 | "Request FUNCTION to invoke debugger each time it is called. | |
2512acba RS |
612 | If you tell the debugger to continue, FUNCTION's execution proceeds. |
613 | This works by modifying the definition of FUNCTION, | |
0231f2dc JB |
614 | which must be written in Lisp, not predefined. |
615 | Use \\[cancel-debug-on-entry] to cancel the effect of this command. | |
2512acba | 616 | Redefining FUNCTION also cancels it." |
0231f2dc JB |
617 | (interactive "aDebug on entry (to function): ") |
618 | (debugger-reenable) | |
f9e899b6 | 619 | ;; Handle a function that has been aliased to some other function. |
7473b6ad SM |
620 | (if (and (subrp (symbol-function function)) |
621 | (eq (cdr (subr-arity (symbol-function function))) 'unevalled)) | |
622 | (error "Function %s is a special form" function)) | |
623 | (if (or (symbolp (symbol-function function)) | |
624 | (subrp (symbol-function function))) | |
625 | ;; Create a wrapper in which we can then add the necessary debug call. | |
f9e899b6 | 626 | (fset function `(lambda (&rest debug-on-entry-args) |
7473b6ad | 627 | ,(interactive-form (symbol-function function)) |
f9e899b6 KH |
628 | (apply ',(symbol-function function) |
629 | debug-on-entry-args)))) | |
0231f2dc JB |
630 | (or (consp (symbol-function function)) |
631 | (debug-convert-byte-code function)) | |
632 | (or (consp (symbol-function function)) | |
633 | (error "Definition of %s is not a list" function)) | |
634 | (fset function (debug-on-entry-1 function (symbol-function function) t)) | |
635 | (or (memq function debug-function-list) | |
6836bb69 | 636 | (push function debug-function-list)) |
0231f2dc JB |
637 | function) |
638 | ||
639 | ;;;###autoload | |
640 | (defun cancel-debug-on-entry (&optional function) | |
641 | "Undo effect of \\[debug-on-entry] on FUNCTION. | |
642 | If argument is nil or an empty string, cancel for all functions." | |
10a4c11f JB |
643 | (interactive |
644 | (list (let ((name | |
645 | (completing-read "Cancel debug on entry (to function): " | |
cafbc9a5 | 646 | (mapcar 'symbol-name debug-function-list) |
10a4c11f JB |
647 | nil t nil))) |
648 | (if name (intern name))))) | |
0231f2dc JB |
649 | (debugger-reenable) |
650 | (if (and function (not (string= function ""))) | |
651 | (progn | |
7473b6ad SM |
652 | (let ((f (debug-on-entry-1 function (symbol-function function) nil))) |
653 | (condition-case nil | |
654 | (if (and (equal (nth 1 f) '(&rest debug-on-entry-args)) | |
655 | (eq (car (nth 3 f)) 'apply)) | |
656 | ;; `f' is a wrapper introduced in debug-on-entry. | |
657 | ;; Get rid of it since we don't need it any more. | |
658 | (setq f (nth 1 (nth 1 (nth 3 f))))) | |
659 | (error nil)) | |
660 | (fset function f)) | |
0231f2dc JB |
661 | (setq debug-function-list (delq function debug-function-list)) |
662 | function) | |
663 | (message "Cancelling debug-on-entry for all functions") | |
664 | (mapcar 'cancel-debug-on-entry debug-function-list))) | |
665 | ||
666 | (defun debug-convert-byte-code (function) | |
667 | (let ((defn (symbol-function function))) | |
668 | (if (not (consp defn)) | |
669 | ;; Assume a compiled code object. | |
670 | (let* ((contents (append defn nil)) | |
671 | (body | |
672 | (list (list 'byte-code (nth 1 contents) | |
673 | (nth 2 contents) (nth 3 contents))))) | |
674 | (if (nthcdr 5 contents) | |
675 | (setq body (cons (list 'interactive (nth 5 contents)) body))) | |
676 | (if (nth 4 contents) | |
c8c76dd3 RS |
677 | ;; Use `documentation' here, to get the actual string, |
678 | ;; in case the compiled function has a reference | |
679 | ;; to the .elc file. | |
680 | (setq body (cons (documentation function) body))) | |
0231f2dc JB |
681 | (fset function (cons 'lambda (cons (car contents) body))))))) |
682 | ||
683 | (defun debug-on-entry-1 (function defn flag) | |
684 | (if (subrp defn) | |
685 | (error "%s is a built-in function" function) | |
686 | (if (eq (car defn) 'macro) | |
687 | (debug-on-entry-1 function (cdr defn) flag) | |
688 | (or (eq (car defn) 'lambda) | |
689 | (error "%s not user-defined Lisp function" function)) | |
7473b6ad SM |
690 | (let ((tail (cddr defn))) |
691 | ;; Skip the docstring. | |
692 | (if (stringp (car tail)) (setq tail (cdr tail))) | |
693 | ;; Skip the interactive form. | |
694 | (if (eq 'interactive (car-safe (car tail))) (setq tail (cdr tail))) | |
695 | (unless (eq flag (equal (car tail) '(debug 'debug))) | |
696 | ;; Add/remove debug statement as needed. | |
697 | (if (not flag) | |
698 | (progn (setcar tail (cadr tail)) | |
699 | (setcdr tail (cddr tail))) | |
700 | (setcdr tail (cons (car tail) (cdr tail))) | |
701 | (setcar tail '(debug 'debug)))) | |
702 | defn)))) | |
0231f2dc JB |
703 | |
704 | (defun debugger-list-functions () | |
705 | "Display a list of all the functions now set to debug on entry." | |
706 | (interactive) | |
707 | (with-output-to-temp-buffer "*Help*" | |
708 | (if (null debug-function-list) | |
709 | (princ "No debug-on-entry functions now\n") | |
710 | (princ "Functions set to debug on entry:\n\n") | |
711 | (let ((list debug-function-list)) | |
712 | (while list | |
713 | (prin1 (car list)) | |
714 | (terpri) | |
715 | (setq list (cdr list)))) | |
716 | (princ "Note: if you have redefined a function, then it may no longer\n") | |
89961ff8 KH |
717 | (princ "be set to debug on entry, even if it is in the list.")) |
718 | (save-excursion | |
719 | (set-buffer standard-output) | |
720 | (help-mode)))) | |
c0274f38 | 721 | |
896546cd RS |
722 | (provide 'debug) |
723 | ||
c0274f38 | 724 | ;;; debug.el ends here |