Commit | Line | Data |
---|---|---|
c0274f38 ER |
1 | ;;; debug.el --- debuggers and related commands for Emacs |
2 | ||
14769773 | 3 | ;; Copyright (C) 1985, 1986, 1994 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 | |
666b9413 SE |
31 | (defgroup debugger nil |
32 | "Debuggers and related commands for Emacs." | |
33 | :prefix "debugger-" | |
34 | :group 'debug) | |
8c1cd093 | 35 | |
666b9413 SE |
36 | (defcustom debugger-mode-hook nil |
37 | "*Hooks run when `debugger-mode' is turned on." | |
38 | :type 'hook | |
cd32a7ba DN |
39 | :group 'debugger |
40 | :version "20.3") | |
8c1cd093 | 41 | |
63ca439e GM |
42 | (defcustom debugger-batch-max-lines 40 |
43 | "*Maximum lines to show in debugger buffer in a noninteractive Emacs. | |
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 | |
666b9413 SE |
51 | (defcustom debug-function-list nil |
52 | "List of functions currently set for debug on entry." | |
53 | :type '(repeat function) | |
54 | :group 'debugger) | |
55 | ||
56 | (defcustom debugger-step-after-exit nil | |
57 | "Non-nil means \"single-step\" after the debugger exits." | |
58 | :type 'boolean | |
59 | :group 'debugger) | |
2eeeb0d2 RS |
60 | |
61 | (defvar debugger-value nil | |
62 | "This is the value for the debugger to return, when it returns.") | |
63 | ||
64 | (defvar debugger-old-buffer nil | |
65 | "This is the buffer that was current when the debugger was entered.") | |
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) | |
74 | (defvar debugger-outer-unread-command-char) | |
75 | (defvar debugger-outer-unread-command-events) | |
0359db82 | 76 | (defvar debugger-outer-unread-post-input-method-events) |
35cf010d RS |
77 | (defvar debugger-outer-last-input-event) |
78 | (defvar debugger-outer-last-command-event) | |
79 | (defvar debugger-outer-last-nonmenu-event) | |
80 | (defvar debugger-outer-last-event-frame) | |
81 | (defvar debugger-outer-standard-input) | |
82 | (defvar debugger-outer-standard-output) | |
bf90c2c0 | 83 | (defvar debugger-outer-inhibit-redisplay) |
35cf010d RS |
84 | (defvar debugger-outer-cursor-in-echo-area) |
85 | ||
0231f2dc JB |
86 | ;;;###autoload |
87 | (setq debugger 'debug) | |
88 | ;;;###autoload | |
89 | (defun debug (&rest debugger-args) | |
2512acba | 90 | "Enter debugger. To return, type \\<debugger-mode-map>`\\[debugger-continue]'. |
0231f2dc JB |
91 | Arguments are mainly for use when this is called from the internals |
92 | of the evaluator. | |
93 | ||
94 | You may call with no args, or you may pass nil as the first arg and | |
95 | any other args you like. In that case, the list of args after the | |
96 | first will be printed into the backtrace buffer." | |
73e78cf1 | 97 | (interactive) |
63ca439e GM |
98 | (unless noninteractive |
99 | (message "Entering debugger...")) | |
0231f2dc | 100 | (let (debugger-value |
0231f2dc JB |
101 | (debug-on-error nil) |
102 | (debug-on-quit nil) | |
103 | (debugger-buffer (let ((default-major-mode 'fundamental-mode)) | |
36363ac1 | 104 | (get-buffer-create "*Backtrace*"))) |
0231f2dc JB |
105 | (debugger-old-buffer (current-buffer)) |
106 | (debugger-step-after-exit nil) | |
107 | ;; Don't keep reading from an executing kbd macro! | |
efcf38c7 | 108 | (executing-kbd-macro nil) |
35cf010d RS |
109 | ;; Save the outer values of these vars for the `e' command |
110 | ;; before we replace the values. | |
cf1c8cd9 | 111 | (debugger-outer-match-data (match-data)) |
80ddb8ed RS |
112 | (debugger-outer-load-read-function load-read-function) |
113 | (debugger-outer-overriding-local-map overriding-local-map) | |
29e2b496 RS |
114 | (debugger-outer-overriding-terminal-local-map |
115 | overriding-terminal-local-map) | |
35cf010d RS |
116 | (debugger-outer-track-mouse track-mouse) |
117 | (debugger-outer-last-command last-command) | |
118 | (debugger-outer-this-command this-command) | |
119 | (debugger-outer-unread-command-char unread-command-char) | |
120 | (debugger-outer-unread-command-events unread-command-events) | |
0359db82 KH |
121 | (debugger-outer-unread-post-input-method-events |
122 | unread-post-input-method-events) | |
35cf010d RS |
123 | (debugger-outer-last-input-event last-input-event) |
124 | (debugger-outer-last-command-event last-command-event) | |
125 | (debugger-outer-last-nonmenu-event last-nonmenu-event) | |
126 | (debugger-outer-last-event-frame last-event-frame) | |
127 | (debugger-outer-standard-input standard-input) | |
128 | (debugger-outer-standard-output standard-output) | |
bf90c2c0 | 129 | (debugger-outer-inhibit-redisplay inhibit-redisplay) |
35cf010d | 130 | (debugger-outer-cursor-in-echo-area cursor-in-echo-area)) |
9d07cf38 RS |
131 | ;; Set this instead of binding it, so that `q' |
132 | ;; will not restore it. | |
6c2599ed | 133 | (setq overriding-terminal-local-map nil) |
35cf010d RS |
134 | ;; Don't let these magic variables affect the debugger itself. |
135 | (let ((last-command nil) this-command track-mouse | |
b2ecc630 | 136 | (unread-command-char -1) unread-command-events |
0359db82 | 137 | unread-post-input-method-events |
35cf010d RS |
138 | last-input-event last-command-event last-nonmenu-event |
139 | last-event-frame | |
80ddb8ed RS |
140 | overriding-local-map |
141 | load-read-function | |
c7387861 RS |
142 | ;; If we are inside a minibuffer, allow nesting |
143 | ;; so that we don't get an error from the `e' command. | |
f0ee2336 AS |
144 | (enable-recursive-minibuffers |
145 | (or enable-recursive-minibuffers (> (minibuffer-depth) 0))) | |
35cf010d | 146 | (standard-input t) (standard-output t) |
bf90c2c0 | 147 | inhibit-redisplay |
35cf010d RS |
148 | (cursor-in-echo-area nil)) |
149 | (unwind-protect | |
150 | (save-excursion | |
151 | (save-window-excursion | |
152 | (pop-to-buffer debugger-buffer) | |
35cf010d | 153 | (debugger-mode) |
63ca439e GM |
154 | (debugger-setup-buffer debugger-args) |
155 | (when noninteractive | |
156 | ;; If the backtrace is long, save the beginning | |
157 | ;; and the end, but discard the middle. | |
158 | (when (> (count-lines (point-min) (point-max)) | |
159 | debugger-batch-max-lines) | |
160 | (goto-char (point-min)) | |
161 | (forward-line (/ 2 debugger-batch-max-lines)) | |
162 | (let ((middlestart (point))) | |
163 | (goto-char (point-max)) | |
164 | (forward-line (- (/ 2 debugger-batch-max-lines) | |
165 | debugger-batch-max-lines)) | |
166 | (delete-region middlestart (point))) | |
167 | (insert "...\n")) | |
168 | (goto-char (point-min)) | |
169 | (message (buffer-string)) | |
170 | (kill-emacs)) | |
171 | (if (eq (car debugger-args) 'debug) | |
172 | ;; Skip the frames for backtrace-debug, byte-code, and debug. | |
173 | (backtrace-debug 3 t)) | |
35cf010d | 174 | (debugger-reenable) |
0231f2dc | 175 | (message "") |
35cf010d RS |
176 | (let ((inhibit-trace t) |
177 | (standard-output nil) | |
178 | (buffer-read-only t)) | |
179 | (message "") | |
614710af RS |
180 | ;; Make sure we unbind buffer-read-only in the right buffer. |
181 | (save-excursion | |
182 | (recursive-edit))))) | |
36363ac1 KH |
183 | ;; Kill or at least neuter the backtrace buffer, so that users |
184 | ;; don't try to execute debugger commands in an invalid context. | |
185 | (if (get-buffer-window debugger-buffer 'visible) | |
186 | ;; Still visible despite the save-window-excursion? Maybe it | |
187 | ;; it's in a pop-up frame. It would be annoying to delete and | |
188 | ;; recreate it every time the debugger stops, so instead we'll | |
189 | ;; erase it but leave it visible. | |
190 | (save-excursion | |
191 | (set-buffer debugger-buffer) | |
192 | (erase-buffer) | |
193 | (fundamental-mode)) | |
194 | (kill-buffer debugger-buffer)) | |
17b3e87b | 195 | (set-match-data debugger-outer-match-data))) |
35cf010d RS |
196 | ;; Put into effect the modified values of these variables |
197 | ;; in case the user set them with the `e' command. | |
80ddb8ed RS |
198 | (setq load-read-function debugger-outer-load-read-function) |
199 | (setq overriding-local-map debugger-outer-overriding-local-map) | |
29e2b496 RS |
200 | (setq overriding-terminal-local-map |
201 | debugger-outer-overriding-terminal-local-map) | |
35cf010d RS |
202 | (setq track-mouse debugger-outer-track-mouse) |
203 | (setq last-command debugger-outer-last-command) | |
204 | (setq this-command debugger-outer-this-command) | |
205 | (setq unread-command-char debugger-outer-unread-command-char) | |
206 | (setq unread-command-events debugger-outer-unread-command-events) | |
0359db82 KH |
207 | (setq unread-post-input-method-events |
208 | debugger-outer-unread-post-input-method-events) | |
35cf010d RS |
209 | (setq last-input-event debugger-outer-last-input-event) |
210 | (setq last-command-event debugger-outer-last-command-event) | |
211 | (setq last-nonmenu-event debugger-outer-last-nonmenu-event) | |
212 | (setq last-event-frame debugger-outer-last-event-frame) | |
213 | (setq standard-input debugger-outer-standard-input) | |
214 | (setq standard-output debugger-outer-standard-output) | |
bf90c2c0 | 215 | (setq inhibit-redisplay debugger-outer-inhibit-redisplay) |
14769773 | 216 | (setq cursor-in-echo-area debugger-outer-cursor-in-echo-area) |
0231f2dc JB |
217 | (setq debug-on-next-call debugger-step-after-exit) |
218 | debugger-value)) | |
219 | \f | |
63ca439e GM |
220 | (defun debugger-setup-buffer (debugger-args) |
221 | "Initialize the `*Backtrace*' buffer for entry to the debugger. | |
222 | That buffer should be current already." | |
223 | (setq buffer-read-only nil) | |
224 | (erase-buffer) | |
225 | (set-buffer-multibyte nil) | |
226 | (let ((standard-output (current-buffer)) | |
227 | (print-escape-newlines t) | |
228 | (print-level 8) | |
229 | (print-length 50)) | |
230 | (backtrace)) | |
231 | (goto-char (point-min)) | |
232 | (delete-region (point) | |
233 | (progn | |
234 | (search-forward "\n debug(") | |
235 | (forward-line 1) | |
236 | (point))) | |
237 | (insert "Debugger entered") | |
238 | ;; lambda is for debug-on-call when a function call is next. | |
239 | ;; debug is for debug-on-entry function called. | |
240 | (cond ((memq (car debugger-args) '(lambda debug)) | |
241 | (insert "--entering a function:\n") | |
242 | (if (eq (car debugger-args) 'debug) | |
243 | (progn | |
244 | (delete-char 1) | |
245 | (insert ?*) | |
246 | (beginning-of-line)))) | |
247 | ;; Exiting a function. | |
248 | ((eq (car debugger-args) 'exit) | |
249 | (insert "--returning value: ") | |
250 | (setq debugger-value (nth 1 debugger-args)) | |
251 | (prin1 debugger-value (current-buffer)) | |
252 | (insert ?\n) | |
253 | (delete-char 1) | |
254 | (insert ? ) | |
255 | (beginning-of-line)) | |
256 | ;; Debugger entered for an error. | |
257 | ((eq (car debugger-args) 'error) | |
258 | (insert "--Lisp error: ") | |
259 | (prin1 (nth 1 debugger-args) (current-buffer)) | |
260 | (insert ?\n)) | |
261 | ;; debug-on-call, when the next thing is an eval. | |
262 | ((eq (car debugger-args) t) | |
263 | (insert "--beginning evaluation of function call form:\n")) | |
264 | ;; User calls debug directly. | |
265 | (t | |
266 | (insert ": ") | |
267 | (prin1 (if (eq (car debugger-args) 'nil) | |
268 | (cdr debugger-args) debugger-args) | |
269 | (current-buffer)) | |
270 | (insert ?\n)))) | |
271 | \f | |
0231f2dc JB |
272 | (defun debugger-step-through () |
273 | "Proceed, stepping through subexpressions of this expression. | |
274 | Enter another debugger on next entry to eval, apply or funcall." | |
275 | (interactive) | |
276 | (setq debugger-step-after-exit t) | |
277 | (message "Proceeding, will debug on next eval or call.") | |
278 | (exit-recursive-edit)) | |
279 | ||
280 | (defun debugger-continue () | |
281 | "Continue, evaluating this expression without stopping." | |
282 | (interactive) | |
094e0928 GM |
283 | (unless debugger-may-continue |
284 | (error "Cannot continue")) | |
0231f2dc JB |
285 | (message "Continuing.") |
286 | (exit-recursive-edit)) | |
287 | ||
288 | (defun debugger-return-value (val) | |
289 | "Continue, specifying value to return. | |
290 | This is only useful when the value returned from the debugger | |
291 | will be used, such as in a debug on exit from a frame." | |
292 | (interactive "XReturn value (evaluated): ") | |
293 | (setq debugger-value val) | |
294 | (princ "Returning " t) | |
295 | (prin1 debugger-value) | |
296 | (exit-recursive-edit)) | |
297 | ||
63ca439e GM |
298 | ;; Chosen empirically to account for all the frames |
299 | ;; that will exist when debugger-frame is called | |
300 | ;; within the first one that appears in the backtrace buffer. | |
301 | ;; Assumes debugger-frame is called from a key; | |
302 | ;; will be wrong if it is called with Meta-x. | |
303 | (defconst debugger-frame-offset 8 "") | |
304 | ||
0231f2dc JB |
305 | (defun debugger-jump () |
306 | "Continue to exit from this frame, with all debug-on-entry suspended." | |
307 | (interactive) | |
308 | ;; Compensate for the two extra stack frames for debugger-jump. | |
309 | (let ((debugger-frame-offset (+ debugger-frame-offset 2))) | |
310 | (debugger-frame)) | |
311 | ;; Turn off all debug-on-entry functions | |
312 | ;; but leave them in the list. | |
313 | (let ((list debug-function-list)) | |
314 | (while list | |
315 | (fset (car list) | |
316 | (debug-on-entry-1 (car list) (symbol-function (car list)) nil)) | |
317 | (setq list (cdr list)))) | |
318 | (message "Continuing through this frame") | |
319 | (exit-recursive-edit)) | |
320 | ||
321 | (defun debugger-reenable () | |
322 | "Turn all debug-on-entry functions back on." | |
323 | (let ((list debug-function-list)) | |
324 | (while list | |
325 | (or (consp (symbol-function (car list))) | |
326 | (debug-convert-byte-code (car list))) | |
327 | (fset (car list) | |
328 | (debug-on-entry-1 (car list) (symbol-function (car list)) t)) | |
329 | (setq list (cdr list))))) | |
330 | ||
331 | (defun debugger-frame-number () | |
332 | "Return number of frames in backtrace before the one point points at." | |
333 | (save-excursion | |
334 | (beginning-of-line) | |
335 | (let ((opoint (point)) | |
336 | (count 0)) | |
337 | (goto-char (point-min)) | |
338 | (if (or (equal (buffer-substring (point) (+ (point) 6)) | |
339 | "Signal") | |
340 | (equal (buffer-substring (point) (+ (point) 6)) | |
341 | "Return")) | |
342 | (progn | |
343 | (search-forward ":") | |
344 | (forward-sexp 1))) | |
345 | (forward-line 1) | |
346 | (while (progn | |
347 | (forward-char 2) | |
348 | (if (= (following-char) ?\() | |
349 | (forward-sexp 1) | |
350 | (forward-sexp 2)) | |
351 | (forward-line 1) | |
352 | (<= (point) opoint)) | |
353 | (setq count (1+ count))) | |
354 | count))) | |
355 | ||
0231f2dc JB |
356 | (defun debugger-frame () |
357 | "Request entry to debugger when this frame exits. | |
358 | Applies to the frame whose line point is on in the backtrace." | |
359 | (interactive) | |
360 | (beginning-of-line) | |
361 | (let ((level (debugger-frame-number))) | |
362 | (backtrace-debug (+ level debugger-frame-offset) t)) | |
363 | (if (= (following-char) ? ) | |
364 | (let ((buffer-read-only nil)) | |
365 | (delete-char 1) | |
366 | (insert ?*))) | |
367 | (beginning-of-line)) | |
368 | ||
369 | (defun debugger-frame-clear () | |
c519f68f | 370 | "Do not enter debugger when this frame exits. |
0231f2dc JB |
371 | Applies to the frame whose line point is on in the backtrace." |
372 | (interactive) | |
373 | (beginning-of-line) | |
374 | (let ((level (debugger-frame-number))) | |
375 | (backtrace-debug (+ level debugger-frame-offset) nil)) | |
376 | (if (= (following-char) ?*) | |
377 | (let ((buffer-read-only nil)) | |
378 | (delete-char 1) | |
379 | (insert ? ))) | |
380 | (beginning-of-line)) | |
381 | ||
8c1cd093 KH |
382 | |
383 | ||
384 | (put 'debugger-env-macro 'lisp-indent-function 0) | |
385 | (defmacro debugger-env-macro (&rest body) | |
386 | "Run BODY in original environment." | |
6c2599ed SS |
387 | `(save-excursion |
388 | (if (null (buffer-name debugger-old-buffer)) | |
389 | ;; old buffer deleted | |
390 | (setq debugger-old-buffer (current-buffer))) | |
391 | (set-buffer debugger-old-buffer) | |
392 | (let ((load-read-function debugger-outer-load-read-function) | |
393 | (overriding-terminal-local-map | |
394 | debugger-outer-overriding-terminal-local-map) | |
395 | (overriding-local-map debugger-outer-overriding-local-map) | |
396 | (track-mouse debugger-outer-track-mouse) | |
397 | (last-command debugger-outer-last-command) | |
398 | (this-command debugger-outer-this-command) | |
399 | (unread-command-char debugger-outer-unread-command-char) | |
400 | (unread-command-events debugger-outer-unread-command-events) | |
401 | (unread-post-input-method-events | |
402 | debugger-outer-unread-post-input-method-events) | |
403 | (last-input-event debugger-outer-last-input-event) | |
404 | (last-command-event debugger-outer-last-command-event) | |
405 | (last-nonmenu-event debugger-outer-last-nonmenu-event) | |
406 | (last-event-frame debugger-outer-last-event-frame) | |
407 | (standard-input debugger-outer-standard-input) | |
408 | (standard-output debugger-outer-standard-output) | |
409 | (inhibit-redisplay debugger-outer-inhibit-redisplay) | |
410 | (cursor-in-echo-area debugger-outer-cursor-in-echo-area)) | |
411 | (set-match-data debugger-outer-match-data) | |
412 | (prog1 (progn ,@body) | |
413 | (setq debugger-outer-match-data (match-data)) | |
414 | (setq debugger-outer-load-read-function load-read-function) | |
415 | (setq debugger-outer-overriding-terminal-local-map | |
416 | overriding-terminal-local-map) | |
417 | (setq debugger-outer-overriding-local-map overriding-local-map) | |
418 | (setq debugger-outer-track-mouse track-mouse) | |
419 | (setq debugger-outer-last-command last-command) | |
420 | (setq debugger-outer-this-command this-command) | |
421 | (setq debugger-outer-unread-command-char unread-command-char) | |
422 | (setq debugger-outer-unread-command-events unread-command-events) | |
423 | (setq debugger-outer-unread-post-input-method-events | |
424 | unread-post-input-method-events) | |
425 | (setq debugger-outer-last-input-event last-input-event) | |
426 | (setq debugger-outer-last-command-event last-command-event) | |
427 | (setq debugger-outer-last-nonmenu-event last-nonmenu-event) | |
428 | (setq debugger-outer-last-event-frame last-event-frame) | |
429 | (setq debugger-outer-standard-input standard-input) | |
430 | (setq debugger-outer-standard-output standard-output) | |
431 | (setq debugger-outer-inhibit-redisplay inhibit-redisplay) | |
432 | (setq debugger-outer-cursor-in-echo-area cursor-in-echo-area) | |
433 | )))) | |
8c1cd093 | 434 | |
0231f2dc | 435 | (defun debugger-eval-expression (exp) |
8782c06b RS |
436 | "Eval an expression, in an environment like that outside the debugger." |
437 | (interactive | |
438 | (list (read-from-minibuffer "Eval: " | |
439 | nil read-expression-map t | |
440 | 'read-expression-history))) | |
8c1cd093 | 441 | (debugger-env-macro (eval-expression exp))) |
0231f2dc JB |
442 | \f |
443 | (defvar debugger-mode-map nil) | |
444 | (if debugger-mode-map | |
445 | nil | |
446 | (let ((loop ? )) | |
447 | (setq debugger-mode-map (make-keymap)) | |
448 | (suppress-keymap debugger-mode-map) | |
449 | (define-key debugger-mode-map "-" 'negative-argument) | |
450 | (define-key debugger-mode-map "b" 'debugger-frame) | |
451 | (define-key debugger-mode-map "c" 'debugger-continue) | |
452 | (define-key debugger-mode-map "j" 'debugger-jump) | |
453 | (define-key debugger-mode-map "r" 'debugger-return-value) | |
454 | (define-key debugger-mode-map "u" 'debugger-frame-clear) | |
455 | (define-key debugger-mode-map "d" 'debugger-step-through) | |
456 | (define-key debugger-mode-map "l" 'debugger-list-functions) | |
457 | (define-key debugger-mode-map "h" 'describe-mode) | |
458 | (define-key debugger-mode-map "q" 'top-level) | |
459 | (define-key debugger-mode-map "e" 'debugger-eval-expression) | |
8c1cd093 KH |
460 | (define-key debugger-mode-map " " 'next-line) |
461 | (define-key debugger-mode-map "R" 'debugger-record-expression) | |
462 | )) | |
463 | ||
464 | ||
666b9413 SE |
465 | (defcustom debugger-record-buffer "*Debugger-record*" |
466 | "*Buffer name for expression values, for \\[debugger-record-expression]." | |
467 | :type 'string | |
cd32a7ba DN |
468 | :group 'debugger |
469 | :version "20.3") | |
8c1cd093 KH |
470 | |
471 | (defun debugger-record-expression (exp) | |
472 | "Display a variable's value and record it in `*Backtrace-record*' buffer." | |
473 | (interactive | |
474 | (list (read-from-minibuffer | |
475 | "Record Eval: " | |
476 | nil | |
477 | read-expression-map t | |
478 | 'read-expression-history))) | |
479 | (let* ((buffer (get-buffer-create debugger-record-buffer)) | |
480 | (standard-output buffer)) | |
481 | (princ (format "Debugger Eval (%s): " exp)) | |
482 | (princ (debugger-eval-expression exp)) | |
483 | (terpri)) | |
484 | ||
485 | (with-current-buffer (get-buffer debugger-record-buffer) | |
486 | (save-excursion | |
487 | (forward-line -1) | |
488 | (message | |
489 | (buffer-substring (point) (progn (end-of-line) (point))))))) | |
0231f2dc JB |
490 | |
491 | (put 'debugger-mode 'mode-class 'special) | |
492 | ||
493 | (defun debugger-mode () | |
494 | "Mode for backtrace buffers, selected in debugger. | |
495 | \\<debugger-mode-map> | |
496 | A line starts with `*' if exiting that frame will call the debugger. | |
497 | Type \\[debugger-frame] or \\[debugger-frame-clear] to set or remove the `*'. | |
498 | ||
499 | When in debugger due to frame being exited, | |
500 | use the \\[debugger-return-value] command to override the value | |
501 | being returned from that frame. | |
502 | ||
503 | Use \\[debug-on-entry] and \\[cancel-debug-on-entry] to control | |
504 | which functions will enter the debugger when called. | |
505 | ||
506 | Complete list of commands: | |
507 | \\{debugger-mode-map}" | |
8c1cd093 | 508 | (kill-all-local-variables) |
0231f2dc JB |
509 | (setq major-mode 'debugger-mode) |
510 | (setq mode-name "Debugger") | |
511 | (setq truncate-lines t) | |
512 | (set-syntax-table emacs-lisp-mode-syntax-table) | |
8c1cd093 KH |
513 | (use-local-map debugger-mode-map) |
514 | (run-hooks 'debugger-mode-hook)) | |
0231f2dc JB |
515 | \f |
516 | ;;;###autoload | |
517 | (defun debug-on-entry (function) | |
518 | "Request FUNCTION to invoke debugger each time it is called. | |
2512acba RS |
519 | If you tell the debugger to continue, FUNCTION's execution proceeds. |
520 | This works by modifying the definition of FUNCTION, | |
0231f2dc JB |
521 | which must be written in Lisp, not predefined. |
522 | Use \\[cancel-debug-on-entry] to cancel the effect of this command. | |
2512acba | 523 | Redefining FUNCTION also cancels it." |
0231f2dc JB |
524 | (interactive "aDebug on entry (to function): ") |
525 | (debugger-reenable) | |
f9e899b6 KH |
526 | ;; Handle a function that has been aliased to some other function. |
527 | (if (symbolp (symbol-function function)) | |
528 | (fset function `(lambda (&rest debug-on-entry-args) | |
529 | (apply ',(symbol-function function) | |
530 | debug-on-entry-args)))) | |
0231f2dc JB |
531 | (if (subrp (symbol-function function)) |
532 | (error "Function %s is a primitive" function)) | |
533 | (or (consp (symbol-function function)) | |
534 | (debug-convert-byte-code function)) | |
535 | (or (consp (symbol-function function)) | |
536 | (error "Definition of %s is not a list" function)) | |
537 | (fset function (debug-on-entry-1 function (symbol-function function) t)) | |
538 | (or (memq function debug-function-list) | |
539 | (setq debug-function-list (cons function debug-function-list))) | |
540 | function) | |
541 | ||
542 | ;;;###autoload | |
543 | (defun cancel-debug-on-entry (&optional function) | |
544 | "Undo effect of \\[debug-on-entry] on FUNCTION. | |
545 | If argument is nil or an empty string, cancel for all functions." | |
10a4c11f JB |
546 | (interactive |
547 | (list (let ((name | |
548 | (completing-read "Cancel debug on entry (to function): " | |
549 | ;; Make an "alist" of the functions | |
550 | ;; that now have debug on entry. | |
551 | (mapcar 'list | |
552 | (mapcar 'symbol-name | |
553 | debug-function-list)) | |
554 | nil t nil))) | |
555 | (if name (intern name))))) | |
0231f2dc JB |
556 | (debugger-reenable) |
557 | (if (and function (not (string= function ""))) | |
558 | (progn | |
559 | (fset function | |
560 | (debug-on-entry-1 function (symbol-function function) nil)) | |
561 | (setq debug-function-list (delq function debug-function-list)) | |
562 | function) | |
563 | (message "Cancelling debug-on-entry for all functions") | |
564 | (mapcar 'cancel-debug-on-entry debug-function-list))) | |
565 | ||
566 | (defun debug-convert-byte-code (function) | |
567 | (let ((defn (symbol-function function))) | |
568 | (if (not (consp defn)) | |
569 | ;; Assume a compiled code object. | |
570 | (let* ((contents (append defn nil)) | |
571 | (body | |
572 | (list (list 'byte-code (nth 1 contents) | |
573 | (nth 2 contents) (nth 3 contents))))) | |
574 | (if (nthcdr 5 contents) | |
575 | (setq body (cons (list 'interactive (nth 5 contents)) body))) | |
576 | (if (nth 4 contents) | |
c8c76dd3 RS |
577 | ;; Use `documentation' here, to get the actual string, |
578 | ;; in case the compiled function has a reference | |
579 | ;; to the .elc file. | |
580 | (setq body (cons (documentation function) body))) | |
0231f2dc JB |
581 | (fset function (cons 'lambda (cons (car contents) body))))))) |
582 | ||
583 | (defun debug-on-entry-1 (function defn flag) | |
584 | (if (subrp defn) | |
585 | (error "%s is a built-in function" function) | |
586 | (if (eq (car defn) 'macro) | |
587 | (debug-on-entry-1 function (cdr defn) flag) | |
588 | (or (eq (car defn) 'lambda) | |
589 | (error "%s not user-defined Lisp function" function)) | |
590 | (let (tail prec) | |
591 | (if (stringp (car (nthcdr 2 defn))) | |
592 | (setq tail (nthcdr 3 defn) | |
593 | prec (list (car defn) (car (cdr defn)) | |
594 | (car (cdr (cdr defn))))) | |
595 | (setq tail (nthcdr 2 defn) | |
596 | prec (list (car defn) (car (cdr defn))))) | |
597 | (if (eq flag (equal (car tail) '(debug 'debug))) | |
598 | defn | |
599 | (if flag | |
600 | (nconc prec (cons '(debug 'debug) tail)) | |
601 | (nconc prec (cdr tail)))))))) | |
602 | ||
603 | (defun debugger-list-functions () | |
604 | "Display a list of all the functions now set to debug on entry." | |
605 | (interactive) | |
606 | (with-output-to-temp-buffer "*Help*" | |
607 | (if (null debug-function-list) | |
608 | (princ "No debug-on-entry functions now\n") | |
609 | (princ "Functions set to debug on entry:\n\n") | |
610 | (let ((list debug-function-list)) | |
611 | (while list | |
612 | (prin1 (car list)) | |
613 | (terpri) | |
614 | (setq list (cdr list)))) | |
615 | (princ "Note: if you have redefined a function, then it may no longer\n") | |
89961ff8 KH |
616 | (princ "be set to debug on entry, even if it is in the list.")) |
617 | (save-excursion | |
618 | (set-buffer standard-output) | |
619 | (help-mode)))) | |
c0274f38 | 620 | |
896546cd RS |
621 | (provide 'debug) |
622 | ||
c0274f38 | 623 | ;;; debug.el ends here |