Mercurial > emacs
annotate lisp/emacs-lisp/debug.el @ 5474:239620e1795d
(shell-cd): Function removed.
(shell-prefixed-directory-name): New defsubst.
(shell-process-popd, shell-process-pushd, shell-process-cd,
shell-resync-dirs): Apply it to ARG when it's a directory name.
Use (concat comint-file-name-prefix "~") in place of (getenv "HOME") or "~".
Call cd instead of shell-cd.
author | Roland McGrath <roland@gnu.org> |
---|---|
date | Thu, 06 Jan 1994 17:02:00 +0000 |
parents | db0ab86f381a |
children | ab9c9a611b5a |
rev | line source |
---|---|
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
477
diff
changeset
|
1 ;;; debug.el --- debuggers and related commands for Emacs |
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
477
diff
changeset
|
2 |
845 | 3 ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc. |
4 | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
5 ;; Maintainer: FSF |
2247
2c7997f249eb
Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
845
diff
changeset
|
6 ;; Keywords: lisp, tools, maint |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
7 |
473 | 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 | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
12 ;; the Free Software Foundation; either version 2, or (at your option) |
473 | 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 | |
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
23 | |
2307
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
24 ;;; Commentary: |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
25 |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
26 ;; This is a major mode documented in the Emacs manual. |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
27 |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
28 ;;; Code: |
473 | 29 |
30 (defvar debug-function-list nil | |
31 "List of functions currently set for debug on entry.") | |
32 | |
33 ;;;###autoload | |
34 (setq debugger 'debug) | |
35 ;;;###autoload | |
36 (defun debug (&rest debugger-args) | |
37 "Enter debugger. Returns if user says \"continue\". | |
38 Arguments are mainly for use when this is called from the internals | |
39 of the evaluator. | |
40 | |
41 You may call with no args, or you may pass nil as the first arg and | |
42 any other args you like. In that case, the list of args after the | |
43 first will be printed into the backtrace buffer." | |
44 (message "Entering debugger...") | |
45 (let (debugger-value | |
46 (debugger-match-data (match-data)) | |
47 (debug-on-error nil) | |
48 (debug-on-quit nil) | |
49 (debugger-buffer (let ((default-major-mode 'fundamental-mode)) | |
50 (generate-new-buffer "*Backtrace*"))) | |
51 (debugger-old-buffer (current-buffer)) | |
52 (debugger-step-after-exit nil) | |
53 ;; Don't keep reading from an executing kbd macro! | |
54 (executing-macro nil) | |
5430
db0ab86f381a
(debug): Bind this-command and last-command.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
55 last-command this command |
473 | 56 (cursor-in-echo-area nil)) |
57 (unwind-protect | |
58 (save-excursion | |
59 (save-window-excursion | |
60 (pop-to-buffer debugger-buffer) | |
61 (erase-buffer) | |
62 (let ((standard-output (current-buffer)) | |
63 (print-escape-newlines t) | |
64 (print-length 50)) | |
65 (backtrace)) | |
66 (goto-char (point-min)) | |
67 (debugger-mode) | |
68 (delete-region (point) | |
69 (progn | |
70 (search-forward "\n debug(") | |
71 (forward-line 1) | |
72 (point))) | |
73 (debugger-reenable) | |
74 (cond ((memq (car debugger-args) '(lambda debug)) | |
75 (insert "Entering:\n") | |
76 (if (eq (car debugger-args) 'debug) | |
77 (progn | |
78 (backtrace-debug 4 t) | |
79 (delete-char 1) | |
80 (insert ?*) | |
81 (beginning-of-line)))) | |
82 ((eq (car debugger-args) 'exit) | |
83 (insert "Return value: ") | |
84 (setq debugger-value (nth 1 debugger-args)) | |
85 (prin1 debugger-value (current-buffer)) | |
86 (insert ?\n) | |
87 (delete-char 1) | |
88 (insert ? ) | |
89 (beginning-of-line)) | |
90 ((eq (car debugger-args) 'error) | |
91 (insert "Signalling: ") | |
92 (prin1 (nth 1 debugger-args) (current-buffer)) | |
93 (insert ?\n)) | |
94 ((eq (car debugger-args) t) | |
95 (insert "Beginning evaluation of function call form:\n")) | |
96 (t | |
97 (prin1 (if (eq (car debugger-args) 'nil) | |
98 (cdr debugger-args) debugger-args) | |
99 (current-buffer)) | |
100 (insert ?\n))) | |
101 (message "") | |
102 (let ((inhibit-trace t) | |
103 (standard-output nil) | |
104 (buffer-read-only t)) | |
105 (message "") | |
106 (recursive-edit)))) | |
107 ;; So that users do not try to execute debugger commands | |
108 ;; in an invalid context | |
109 (kill-buffer debugger-buffer) | |
110 (store-match-data debugger-match-data)) | |
111 (setq debug-on-next-call debugger-step-after-exit) | |
112 debugger-value)) | |
113 | |
114 (defun debugger-step-through () | |
115 "Proceed, stepping through subexpressions of this expression. | |
116 Enter another debugger on next entry to eval, apply or funcall." | |
117 (interactive) | |
118 (setq debugger-step-after-exit t) | |
119 (message "Proceeding, will debug on next eval or call.") | |
120 (exit-recursive-edit)) | |
121 | |
122 (defun debugger-continue () | |
123 "Continue, evaluating this expression without stopping." | |
124 (interactive) | |
125 (message "Continuing.") | |
126 (exit-recursive-edit)) | |
127 | |
128 (defun debugger-return-value (val) | |
129 "Continue, specifying value to return. | |
130 This is only useful when the value returned from the debugger | |
131 will be used, such as in a debug on exit from a frame." | |
132 (interactive "XReturn value (evaluated): ") | |
133 (setq debugger-value val) | |
134 (princ "Returning " t) | |
135 (prin1 debugger-value) | |
136 (exit-recursive-edit)) | |
137 | |
138 (defun debugger-jump () | |
139 "Continue to exit from this frame, with all debug-on-entry suspended." | |
140 (interactive) | |
141 ;; Compensate for the two extra stack frames for debugger-jump. | |
142 (let ((debugger-frame-offset (+ debugger-frame-offset 2))) | |
143 (debugger-frame)) | |
144 ;; Turn off all debug-on-entry functions | |
145 ;; but leave them in the list. | |
146 (let ((list debug-function-list)) | |
147 (while list | |
148 (fset (car list) | |
149 (debug-on-entry-1 (car list) (symbol-function (car list)) nil)) | |
150 (setq list (cdr list)))) | |
151 (message "Continuing through this frame") | |
152 (exit-recursive-edit)) | |
153 | |
154 (defun debugger-reenable () | |
155 "Turn all debug-on-entry functions back on." | |
156 (let ((list debug-function-list)) | |
157 (while list | |
158 (or (consp (symbol-function (car list))) | |
159 (debug-convert-byte-code (car list))) | |
160 (fset (car list) | |
161 (debug-on-entry-1 (car list) (symbol-function (car list)) t)) | |
162 (setq list (cdr list))))) | |
163 | |
164 (defun debugger-frame-number () | |
165 "Return number of frames in backtrace before the one point points at." | |
166 (save-excursion | |
167 (beginning-of-line) | |
168 (let ((opoint (point)) | |
169 (count 0)) | |
170 (goto-char (point-min)) | |
171 (if (or (equal (buffer-substring (point) (+ (point) 6)) | |
172 "Signal") | |
173 (equal (buffer-substring (point) (+ (point) 6)) | |
174 "Return")) | |
175 (progn | |
176 (search-forward ":") | |
177 (forward-sexp 1))) | |
178 (forward-line 1) | |
179 (while (progn | |
180 (forward-char 2) | |
181 (if (= (following-char) ?\() | |
182 (forward-sexp 1) | |
183 (forward-sexp 2)) | |
184 (forward-line 1) | |
185 (<= (point) opoint)) | |
186 (setq count (1+ count))) | |
187 count))) | |
188 | |
189 ;; Chosen empirically to account for all the frames | |
190 ;; that will exist when debugger-frame is called | |
191 ;; within the first one that appears in the backtrace buffer. | |
192 ;; Assumes debugger-frame is called from a key; | |
193 ;; will be wrong if it is called with Meta-x. | |
194 (defconst debugger-frame-offset 8 "") | |
195 | |
196 (defun debugger-frame () | |
197 "Request entry to debugger when this frame exits. | |
198 Applies to the frame whose line point is on in the backtrace." | |
199 (interactive) | |
200 (beginning-of-line) | |
201 (let ((level (debugger-frame-number))) | |
202 (backtrace-debug (+ level debugger-frame-offset) t)) | |
203 (if (= (following-char) ? ) | |
204 (let ((buffer-read-only nil)) | |
205 (delete-char 1) | |
206 (insert ?*))) | |
207 (beginning-of-line)) | |
208 | |
209 (defun debugger-frame-clear () | |
210 "Do not enter to debugger when this frame exits. | |
211 Applies to the frame whose line point is on in the backtrace." | |
212 (interactive) | |
213 (beginning-of-line) | |
214 (let ((level (debugger-frame-number))) | |
215 (backtrace-debug (+ level debugger-frame-offset) nil)) | |
216 (if (= (following-char) ?*) | |
217 (let ((buffer-read-only nil)) | |
218 (delete-char 1) | |
219 (insert ? ))) | |
220 (beginning-of-line)) | |
221 | |
222 (defun debugger-eval-expression (exp) | |
223 (interactive "xEval: ") | |
224 (save-excursion | |
225 (if (null (buffer-name debugger-old-buffer)) | |
226 ;; old buffer deleted | |
227 (setq debugger-old-buffer (current-buffer))) | |
228 (set-buffer debugger-old-buffer) | |
229 (eval-expression exp))) | |
230 | |
231 (defvar debugger-mode-map nil) | |
232 (if debugger-mode-map | |
233 nil | |
234 (let ((loop ? )) | |
235 (setq debugger-mode-map (make-keymap)) | |
236 (suppress-keymap debugger-mode-map) | |
237 (define-key debugger-mode-map "-" 'negative-argument) | |
238 (define-key debugger-mode-map "b" 'debugger-frame) | |
239 (define-key debugger-mode-map "c" 'debugger-continue) | |
240 (define-key debugger-mode-map "j" 'debugger-jump) | |
241 (define-key debugger-mode-map "r" 'debugger-return-value) | |
242 (define-key debugger-mode-map "u" 'debugger-frame-clear) | |
243 (define-key debugger-mode-map "d" 'debugger-step-through) | |
244 (define-key debugger-mode-map "l" 'debugger-list-functions) | |
245 (define-key debugger-mode-map "h" 'describe-mode) | |
246 (define-key debugger-mode-map "q" 'top-level) | |
247 (define-key debugger-mode-map "e" 'debugger-eval-expression) | |
248 (define-key debugger-mode-map " " 'next-line))) | |
249 | |
250 (put 'debugger-mode 'mode-class 'special) | |
251 | |
252 (defun debugger-mode () | |
253 "Mode for backtrace buffers, selected in debugger. | |
254 \\<debugger-mode-map> | |
255 A line starts with `*' if exiting that frame will call the debugger. | |
256 Type \\[debugger-frame] or \\[debugger-frame-clear] to set or remove the `*'. | |
257 | |
258 When in debugger due to frame being exited, | |
259 use the \\[debugger-return-value] command to override the value | |
260 being returned from that frame. | |
261 | |
262 Use \\[debug-on-entry] and \\[cancel-debug-on-entry] to control | |
263 which functions will enter the debugger when called. | |
264 | |
265 Complete list of commands: | |
266 \\{debugger-mode-map}" | |
267 (kill-all-local-variables) | |
268 (setq major-mode 'debugger-mode) | |
269 (setq mode-name "Debugger") | |
270 (setq truncate-lines t) | |
271 (set-syntax-table emacs-lisp-mode-syntax-table) | |
272 (use-local-map debugger-mode-map)) | |
273 | |
274 ;;;###autoload | |
275 (defun debug-on-entry (function) | |
276 "Request FUNCTION to invoke debugger each time it is called. | |
277 If the user continues, FUNCTION's execution proceeds. | |
278 Works by modifying the definition of FUNCTION, | |
279 which must be written in Lisp, not predefined. | |
280 Use \\[cancel-debug-on-entry] to cancel the effect of this command. | |
281 Redefining FUNCTION also does that." | |
282 (interactive "aDebug on entry (to function): ") | |
283 (debugger-reenable) | |
284 (if (subrp (symbol-function function)) | |
285 (error "Function %s is a primitive" function)) | |
286 (or (consp (symbol-function function)) | |
287 (debug-convert-byte-code function)) | |
288 (or (consp (symbol-function function)) | |
289 (error "Definition of %s is not a list" function)) | |
290 (fset function (debug-on-entry-1 function (symbol-function function) t)) | |
291 (or (memq function debug-function-list) | |
292 (setq debug-function-list (cons function debug-function-list))) | |
293 function) | |
294 | |
295 ;;;###autoload | |
296 (defun cancel-debug-on-entry (&optional function) | |
297 "Undo effect of \\[debug-on-entry] on FUNCTION. | |
298 If argument is nil or an empty string, cancel for all functions." | |
477 | 299 (interactive |
300 (list (let ((name | |
301 (completing-read "Cancel debug on entry (to function): " | |
302 ;; Make an "alist" of the functions | |
303 ;; that now have debug on entry. | |
304 (mapcar 'list | |
305 (mapcar 'symbol-name | |
306 debug-function-list)) | |
307 nil t nil))) | |
308 (if name (intern name))))) | |
473 | 309 (debugger-reenable) |
310 (if (and function (not (string= function ""))) | |
311 (progn | |
312 (fset function | |
313 (debug-on-entry-1 function (symbol-function function) nil)) | |
314 (setq debug-function-list (delq function debug-function-list)) | |
315 function) | |
316 (message "Cancelling debug-on-entry for all functions") | |
317 (mapcar 'cancel-debug-on-entry debug-function-list))) | |
318 | |
319 (defun debug-convert-byte-code (function) | |
320 (let ((defn (symbol-function function))) | |
321 (if (not (consp defn)) | |
322 ;; Assume a compiled code object. | |
323 (let* ((contents (append defn nil)) | |
324 (body | |
325 (list (list 'byte-code (nth 1 contents) | |
326 (nth 2 contents) (nth 3 contents))))) | |
327 (if (nthcdr 5 contents) | |
328 (setq body (cons (list 'interactive (nth 5 contents)) body))) | |
329 (if (nth 4 contents) | |
330 (setq body (cons (nth 4 contents) body))) | |
331 (fset function (cons 'lambda (cons (car contents) body))))))) | |
332 | |
333 (defun debug-on-entry-1 (function defn flag) | |
334 (if (subrp defn) | |
335 (error "%s is a built-in function" function) | |
336 (if (eq (car defn) 'macro) | |
337 (debug-on-entry-1 function (cdr defn) flag) | |
338 (or (eq (car defn) 'lambda) | |
339 (error "%s not user-defined Lisp function" function)) | |
340 (let (tail prec) | |
341 (if (stringp (car (nthcdr 2 defn))) | |
342 (setq tail (nthcdr 3 defn) | |
343 prec (list (car defn) (car (cdr defn)) | |
344 (car (cdr (cdr defn))))) | |
345 (setq tail (nthcdr 2 defn) | |
346 prec (list (car defn) (car (cdr defn))))) | |
347 (if (eq flag (equal (car tail) '(debug 'debug))) | |
348 defn | |
349 (if flag | |
350 (nconc prec (cons '(debug 'debug) tail)) | |
351 (nconc prec (cdr tail)))))))) | |
352 | |
353 (defun debugger-list-functions () | |
354 "Display a list of all the functions now set to debug on entry." | |
355 (interactive) | |
356 (with-output-to-temp-buffer "*Help*" | |
357 (if (null debug-function-list) | |
358 (princ "No debug-on-entry functions now\n") | |
359 (princ "Functions set to debug on entry:\n\n") | |
360 (let ((list debug-function-list)) | |
361 (while list | |
362 (prin1 (car list)) | |
363 (terpri) | |
364 (setq list (cdr list)))) | |
365 (princ "Note: if you have redefined a function, then it may no longer\n") | |
366 (princ "be set to debug on entry, even if it is in the list.")))) | |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
477
diff
changeset
|
367 |
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
477
diff
changeset
|
368 ;;; debug.el ends here |