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