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