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