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