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