Mercurial > hg > xemacs-beta
comparison lisp/prim/debug.el @ 0:376386a54a3c r19-14
Import from CVS: tag r19-14
author | cvs |
---|---|
date | Mon, 13 Aug 2007 08:45:50 +0200 |
parents | |
children | b82b59fe008d |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:376386a54a3c |
---|---|
1 ;;; debug.el --- debuggers and related commands for XEmacs | |
2 | |
3 ;; Copyright (C) 1985, 1986, 1993, 1994 Free Software Foundation, Inc. | |
4 | |
5 ;; Maintainer: FSF | |
6 ;; Keyword: lisp, tools | |
7 | |
8 ;; This file is part of XEmacs. | |
9 | |
10 ;; XEmacs is free software; you can redistribute it and/or modify it | |
11 ;; 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 ;; XEmacs is distributed in the hope that it will be useful, but | |
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
18 ;; General Public License for more details. | |
19 | |
20 ;; You should have received a copy of the GNU General Public License | |
21 ;; along with XEmacs; see the file COPYING. If not, write to the Free | |
22 ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
23 | |
24 ;;; Synched up with: FSF 19.30. | |
25 | |
26 ;;; Code: | |
27 | |
28 (defvar debug-function-list nil | |
29 "List of functions currently set for debug on entry.") | |
30 | |
31 (defvar debugger-step-after-exit nil | |
32 "Non-nil means \"single-step\" after the debugger exits.") | |
33 | |
34 (defvar debugger-value nil | |
35 "This is the value for the debugger to return, when it returns.") | |
36 | |
37 (defvar debugger-old-buffer nil | |
38 "This is the buffer that was current when the debugger was entered.") | |
39 | |
40 ;;#### This is terminally random an nigh-unmaintainable. | |
41 ;;#### (need progv in elisp...) | |
42 (defvar debugger-outer-match-data) | |
43 (defvar debugger-outer-load-read-function) | |
44 (defvar debugger-outer-overriding-local-map) | |
45 ;; FSFmacs (defvar debugger-outer-track-mouse) | |
46 (defvar debugger-outer-last-command) | |
47 (defvar debugger-outer-this-command) | |
48 (defvar debugger-outer-unread-command-event) | |
49 (defvar debugger-outer-last-input-event) | |
50 (defvar debugger-outer-last-input-char) | |
51 (defvar debugger-outer-last-input-time) | |
52 (defvar debugger-outer-last-command-event) | |
53 (defvar debugger-outer-last-command-char) | |
54 (defvar debugger-outer-standard-input) | |
55 (defvar debugger-outer-standard-output) | |
56 (defvar debugger-outer-cursor-in-echo-area) | |
57 | |
58 ;;;don't ###autoload, loadup.el does something smarter. | |
59 (setq debugger 'debug) | |
60 | |
61 (defvar debugger-step-after-exit) | |
62 (defvar debugger-old-buffer) | |
63 (defvar debugger-value) | |
64 | |
65 ;;;###autoload | |
66 (defun debug (&rest debugger-args) | |
67 "Enter debugger. To return, type \\<debugger-mode-map>`\\[debugger-continue]'. | |
68 Arguments are mainly for use when this is called | |
69 from the internals of the evaluator. | |
70 You may call with no args, or you may | |
71 pass nil as the first arg and any other args you like. | |
72 In that case, the list of args after the first will | |
73 be printed into the backtrace buffer." | |
74 (interactive) | |
75 ;; XEmacs: it doesn't work to enter the debugger non-interactively | |
76 ;; so just print out a backtrace and exit. | |
77 (if (noninteractive) (apply 'early-error-handler debugger-args)) | |
78 (message "Entering debugger...") | |
79 (let (debugger-value | |
80 (debug-on-error nil) | |
81 (debug-on-quit nil) | |
82 (debug-on-signal nil) | |
83 (debugger-buffer (let ((default-major-mode 'fundamental-mode)) | |
84 (get-buffer-create "*Backtrace*"))) | |
85 ;; #### I18N3 set the debugger-buffer to output-translating | |
86 (debugger-old-buffer (current-buffer)) | |
87 (debugger-step-after-exit nil) | |
88 ;; Don't keep reading from an executing kbd macro! | |
89 (executing-macro nil) | |
90 ;; Save the outer values of these vars for the `e' command | |
91 ;; before we replace the values. | |
92 (debugger-outer-match-data (match-data)) | |
93 (debugger-outer-load-read-function load-read-function) | |
94 (debugger-outer-overriding-local-map overriding-local-map) | |
95 ;; FSFmacs (debugger-outer-track-mouse track-mouse) | |
96 (debugger-outer-last-command last-command) | |
97 (debugger-outer-this-command this-command) | |
98 (debugger-outer-unread-command-event unread-command-event) | |
99 (debugger-outer-last-input-event last-input-event) | |
100 (debugger-outer-last-input-char last-input-char) | |
101 (debugger-outer-last-input-time last-input-time) | |
102 (debugger-outer-last-command-event last-command-event) | |
103 (debugger-outer-last-command-char last-command-char) | |
104 (debugger-outer-standard-input standard-input) | |
105 (debugger-outer-standard-output standard-output) | |
106 (debugger-outer-cursor-in-echo-area cursor-in-echo-area) | |
107 ) | |
108 ;; Don't let these magic variables affect the debugger itself. | |
109 (unwind-protect ;XEmacs change | |
110 (let ((last-command nil) | |
111 (this-command nil) | |
112 (unread-command-event nil) | |
113 (last-input-event (allocate-event)) | |
114 (last-input-char -1) | |
115 (last-input-time nil) | |
116 (last-command-event (allocate-event)) | |
117 (last-command-char -1) | |
118 overriding-local-map | |
119 load-read-function | |
120 (standard-input t) | |
121 (standard-output t) | |
122 (cursor-in-echo-area nil)) | |
123 (save-excursion | |
124 (save-window-excursion | |
125 (pop-to-buffer debugger-buffer) | |
126 (erase-buffer) | |
127 (let ((standard-output (current-buffer)) | |
128 (print-escape-newlines t) | |
129 (print-length 50)) | |
130 (backtrace)) | |
131 (goto-char (point-min)) | |
132 (debugger-mode) | |
133 (delete-region (point) | |
134 (progn | |
135 (re-search-forward "\n[* ] debug(") | |
136 (forward-line 1) | |
137 (point))) | |
138 ;; lambda is for debug-on-call when a function call is next. | |
139 ;; debug is for debug-on-entry function called. | |
140 (debugger-reenable) | |
141 (cond ((memq (car debugger-args) '(lambda debug)) | |
142 (insert "Entering:\n") | |
143 (if (eq (car debugger-args) 'debug) | |
144 (progn | |
145 ;; Skip the frames for backtrace-debug, byte-code, | |
146 ;; and debug. | |
147 (backtrace-debug 3 t) | |
148 (delete-char 1) | |
149 (insert ?*) | |
150 (beginning-of-line)))) | |
151 ;; Exiting a function. | |
152 ((eq (car debugger-args) 'exit) | |
153 (insert "Return value: ") | |
154 (setq debugger-value (nth 1 debugger-args)) | |
155 (prin1 debugger-value (current-buffer)) | |
156 (insert ?\n) | |
157 (delete-char 1) | |
158 (insert ? ) | |
159 (beginning-of-line)) | |
160 ;; Debugger entered for an error. | |
161 ((eq (car debugger-args) 'error) | |
162 (insert "Signalling: ") | |
163 (prin1 (nth 1 debugger-args) (current-buffer)) | |
164 (insert ?\n)) | |
165 ;; debug-on-call, when the next thing is an eval. | |
166 ((eq (car debugger-args) t) | |
167 (insert | |
168 "Beginning evaluation of function call form:\n")) | |
169 ;; User calls debug directly. | |
170 (t | |
171 (prin1 (if (eq (car debugger-args) 'nil) | |
172 (cdr debugger-args) debugger-args) | |
173 (current-buffer)) | |
174 (insert ?\n))) | |
175 (message "") | |
176 (let ((inhibit-trace t) | |
177 (standard-output nil) | |
178 (buffer-read-only t)) | |
179 (message nil) | |
180 (recursive-edit))) | |
181 debugger-value)) | |
182 ;; Kill or at least neuter the backtrace buffer, so that users | |
183 ;; don't try to execute debugger commands in an invalid context. | |
184 (if (get-buffer-window debugger-buffer 'visible) | |
185 ;; Still visible despite the save-window-excursion? Maybe it | |
186 ;; it's in a pop-up frame. It would be annoying to delete and | |
187 ;; recreate it every time the debugger stops, so instead we'll | |
188 ;; erase it but leave it visible. | |
189 (save-excursion | |
190 (set-buffer debugger-buffer) | |
191 (erase-buffer) | |
192 (fundamental-mode)) | |
193 (kill-buffer debugger-buffer)) | |
194 (store-match-data debugger-outer-match-data) | |
195 ;; Put into effect the modified values of these variables | |
196 ;; in case the user set them with the `e' command. | |
197 (setq load-read-function debugger-outer-load-read-function) | |
198 (setq overriding-local-map debugger-outer-overriding-local-map) | |
199 ;; FSFmacs (setq track-mouse debugger-outer-track-mouse) | |
200 (setq last-command debugger-outer-last-command) | |
201 (setq this-command debugger-outer-this-command) | |
202 (setq unread-command-event debugger-outer-unread-command-event) | |
203 (setq last-input-event debugger-outer-last-input-event) | |
204 (setq last-input-char debugger-outer-last-input-char) | |
205 (setq last-input-time debugger-outer-last-input-time) | |
206 (setq last-command-event debugger-outer-last-command-event) | |
207 (setq last-command-char debugger-outer-last-command-char) | |
208 (setq standard-input debugger-outer-standard-input) | |
209 (setq standard-output debugger-outer-standard-output) | |
210 (setq cursor-in-echo-area debugger-outer-cursor-in-echo-area) | |
211 (setq debug-on-next-call debugger-step-after-exit) ;do this last! | |
212 ))) | |
213 | |
214 | |
215 (defun debugger-exit () | |
216 (condition-case nil | |
217 (let ((debug-on-error nil) | |
218 (debug-on-signal nil)) | |
219 ;; Tell signal to keep searching for handlers | |
220 (throw 'debugger t)) | |
221 ;; Called from an old version of Emacs, perhaps? | |
222 (no-catch (exit-recursive-edit)))) | |
223 | |
224 | |
225 (defun debugger-step-through () | |
226 "Proceed, stepping through subexpressions of this expression. | |
227 Enter another debugger on next entry to eval, apply or funcall." | |
228 (interactive) | |
229 (setq debugger-step-after-exit t) | |
230 (message "Proceeding, will debug on next eval or call.") | |
231 (debugger-exit)) | |
232 | |
233 (defun debugger-continue () | |
234 "Continue, evaluating this expression without stopping." | |
235 (interactive) | |
236 (message "Continuing.") | |
237 (debugger-exit)) | |
238 | |
239 (defun debugger-return-value (val) | |
240 "Continue, specifying value to return. | |
241 This is only useful when the value returned from the debugger | |
242 will be used, such as in a debug on exit from a frame." | |
243 (interactive "XReturn value (evaluated): ") | |
244 (setq debugger-value val) | |
245 (princ "Returning " t) | |
246 (prin1 debugger-value) | |
247 (exit-recursive-edit)) | |
248 | |
249 ;; Chosen empirically to account for all the frames | |
250 ;; that will exist when debugger-frame is called | |
251 ;; within the first one that appears in the backtrace buffer. | |
252 ;; Assumes debugger-frame is called from a key; | |
253 ;; will be wrong if it is called with Meta-x. | |
254 (defconst debugger-frame-offset 8 "") | |
255 | |
256 (defun debugger-jump () | |
257 "Continue to exit from this frame, with all debug-on-entry suspended." | |
258 (interactive) | |
259 ;; Compensate for the two extra stack frames for debugger-jump. | |
260 (let ((debugger-frame-offset (+ debugger-frame-offset 2))) | |
261 (debugger-frame)) | |
262 ;; Turn off all debug-on-entry functions | |
263 ;; but leave them in the list. | |
264 (let ((list debug-function-list)) | |
265 (while list | |
266 (fset (car list) | |
267 (debug-on-entry-1 (car list) (symbol-function (car list)) nil)) | |
268 (setq list (cdr list)))) | |
269 (message "Continuing through this frame") | |
270 (debugger-exit)) | |
271 | |
272 (defun debugger-reenable () | |
273 "Turn all debug-on-entry functions back on." | |
274 (let ((list debug-function-list)) | |
275 (while list | |
276 (or (consp (symbol-function (car list))) | |
277 (debug-convert-byte-code (car list))) | |
278 (fset (car list) | |
279 (debug-on-entry-1 (car list) (symbol-function (car list)) t)) | |
280 (setq list (cdr list))))) | |
281 | |
282 (defun debugger-frame-number () | |
283 "Return number of frames in backtrace before the one point points at." | |
284 (save-excursion | |
285 (beginning-of-line) | |
286 (let ((opoint (point)) | |
287 (count 0)) | |
288 (goto-char (point-min)) | |
289 ;; #### I18N3 will not localize properly! | |
290 (if (or (equal (buffer-substring (point) (+ (point) 6)) | |
291 (gettext "Signal")) | |
292 (equal (buffer-substring (point) (+ (point) 6)) | |
293 (gettext "Return"))) | |
294 (progn | |
295 (search-forward ":") | |
296 (forward-sexp 1))) | |
297 (forward-line 1) | |
298 (while (progn | |
299 (forward-char 2) | |
300 (if (= (following-char) ?\() | |
301 (forward-sexp 1) | |
302 (forward-sexp 2)) | |
303 (forward-line 1) | |
304 (<= (point) opoint)) | |
305 (setq count (1+ count))) | |
306 count))) | |
307 | |
308 (defun debugger-frame () | |
309 "Request entry to debugger when this frame exits. | |
310 Applies to the frame whose line point is on in the backtrace." | |
311 (interactive) | |
312 (beginning-of-line) | |
313 (let ((level (debugger-frame-number))) | |
314 (backtrace-debug (+ level debugger-frame-offset) t)) | |
315 (if (= (following-char) ? ) | |
316 (let ((buffer-read-only nil)) | |
317 (delete-char 1) | |
318 (insert ?*))) | |
319 (beginning-of-line)) | |
320 | |
321 (defun debugger-frame-clear () | |
322 "Do not enter to debugger when this frame exits. | |
323 Applies to the frame whose line point is on in the backtrace." | |
324 (interactive) | |
325 (beginning-of-line) | |
326 (let ((level (debugger-frame-number))) | |
327 (backtrace-debug (+ level debugger-frame-offset) nil)) | |
328 (if (= (following-char) ?*) | |
329 (let ((buffer-read-only nil)) | |
330 (delete-char 1) | |
331 (insert ? ))) | |
332 (beginning-of-line)) | |
333 | |
334 (defun debugger-eval-expression (debugger-exp) | |
335 "Eval an expression, in an environment like that outside the debugger." | |
336 (interactive | |
337 (list (read-from-minibuffer "Eval: " | |
338 nil read-expression-map t | |
339 'read-expression-history))) | |
340 (save-excursion | |
341 (if (null (buffer-name debugger-old-buffer)) | |
342 ;; old buffer deleted | |
343 (setq debugger-old-buffer (current-buffer))) | |
344 (set-buffer debugger-old-buffer) | |
345 (let ((last-command debugger-outer-last-command) | |
346 (this-command debugger-outer-this-command) | |
347 (unread-command-event debugger-outer-unread-command-event) | |
348 (last-input-event debugger-outer-last-input-event) | |
349 (last-input-char debugger-outer-last-input-char) | |
350 (last-input-time debugger-outer-last-input-time) | |
351 (last-command-event debugger-outer-last-command-event) | |
352 (last-command-char debugger-outer-last-command-char) | |
353 (standard-input debugger-outer-standard-input) | |
354 (standard-output debugger-outer-standard-output) | |
355 (cursor-in-echo-area debugger-outer-cursor-in-echo-area) | |
356 (overriding-local-map debugger-outer-overriding-local-map) | |
357 (load-read-function debugger-outer-load-read-function)) | |
358 (store-match-data debugger-outer-match-data) | |
359 (prog1 (eval-expression debugger-exp) | |
360 (setq debugger-outer-match-data (match-data) | |
361 debugger-outer-load-read-function load-read-function | |
362 debugger-outer-overriding-local-map overriding-local-map | |
363 debugger-outer-last-command last-command | |
364 debugger-outer-this-command this-command | |
365 debugger-outer-unread-command-event unread-command-event | |
366 debugger-outer-last-input-event last-input-event | |
367 debugger-outer-last-input-char last-input-char | |
368 debugger-outer-last-input-time last-input-time | |
369 debugger-outer-last-command-event last-command-event | |
370 debugger-outer-last-command-char last-command-char | |
371 debugger-outer-standard-input standard-input | |
372 debugger-outer-standard-output standard-output | |
373 debugger-outer-cursor-in-echo-area cursor-in-echo-area))))) | |
374 | |
375 (defvar debugger-mode-map | |
376 (let ((map (make-keymap))) | |
377 (set-keymap-name map 'debugger-mode-map) | |
378 (suppress-keymap map) | |
379 (define-key map "-" 'negative-argument) | |
380 (define-key map "b" 'debugger-frame) | |
381 (define-key map "c" 'debugger-continue) | |
382 (define-key map "j" 'debugger-jump) | |
383 (define-key map "r" 'debugger-return-value) | |
384 (define-key map "u" 'debugger-frame-clear) | |
385 (define-key map "d" 'debugger-step-through) | |
386 (define-key map "l" 'debugger-list-functions) | |
387 (define-key map "h" 'describe-mode) | |
388 (define-key map "q" 'top-level) | |
389 (define-key map "e" 'debugger-eval-expression) | |
390 (define-key map " " 'next-line) | |
391 map)) | |
392 | |
393 (put 'debugger-mode 'mode-class 'special) | |
394 | |
395 (defun debugger-mode () | |
396 "Mode for backtrace buffers, selected in debugger. | |
397 \\<debugger-mode-map> | |
398 A line starts with `*' if exiting that frame will call the debugger. | |
399 Type \\[debugger-frame] or \\[debugger-frame-clear] to set or remove the `*'. | |
400 | |
401 When in debugger due to frame being exited, | |
402 use the \\[debugger-return-value] command to override the value | |
403 being returned from that frame. | |
404 | |
405 Use \\[debug-on-entry] and \\[cancel-debug-on-entry] to control | |
406 which functions will enter the debugger when called. | |
407 | |
408 Complete list of commands: | |
409 \\{debugger-mode-map}" | |
410 (kill-all-local-variables) | |
411 (setq major-mode 'debugger-mode) | |
412 (setq mode-name (gettext "Debugger")) | |
413 (setq truncate-lines t) | |
414 (set-syntax-table emacs-lisp-mode-syntax-table) | |
415 (use-local-map debugger-mode-map)) | |
416 | |
417 ;;;###autoload | |
418 (defun debug-on-entry (function) | |
419 "Request FUNCTION to invoke debugger each time it is called. | |
420 If you tell the debugger to continue, FUNCTION's execution proceeds. | |
421 This works by modifying the definition of FUNCTION, | |
422 which must be written in Lisp, not predefined. | |
423 Use \\[cancel-debug-on-entry] to cancel the effect of this command. | |
424 Redefining FUNCTION also cancels it." | |
425 (interactive "aDebug on entry (to function): ") | |
426 (debugger-reenable) | |
427 (if (subrp (symbol-function function)) | |
428 (error "Function %s is a primitive" function)) | |
429 (or (consp (symbol-function function)) | |
430 (debug-convert-byte-code function)) | |
431 (or (consp (symbol-function function)) | |
432 (error "Definition of %s is not a list" function)) | |
433 (fset function (debug-on-entry-1 function (symbol-function function) t)) | |
434 (or (memq function debug-function-list) | |
435 (setq debug-function-list (cons function debug-function-list))) | |
436 function) | |
437 | |
438 ;;;###autoload | |
439 (defun cancel-debug-on-entry (&optional function) | |
440 "Undo effect of \\[debug-on-entry] on FUNCTION. | |
441 If argument is nil or an empty string, cancel for all functions." | |
442 (interactive | |
443 (list (let ((name | |
444 (completing-read "Cancel debug on entry (to function): " | |
445 ;; Make an "alist" of the functions | |
446 ;; that now have debug on entry. | |
447 (mapcar 'list (mapcar 'symbol-name | |
448 debug-function-list)) | |
449 nil t nil))) | |
450 (if name (intern name))))) | |
451 (debugger-reenable) | |
452 (if (and function (not (string= function ""))) | |
453 (progn | |
454 (fset function | |
455 (debug-on-entry-1 function (symbol-function function) nil)) | |
456 (setq debug-function-list (delq function debug-function-list)) | |
457 function) | |
458 (message "Cancelling debug-on-entry for all functions") | |
459 (mapcar 'cancel-debug-on-entry debug-function-list))) | |
460 | |
461 (defun debug-convert-byte-code (function) | |
462 (let ((defn (symbol-function function))) | |
463 (if (not (consp defn)) | |
464 ;; Assume a compiled code object. | |
465 (let* ((contents (append defn nil)) | |
466 (body | |
467 (list (list 'byte-code (nth 1 contents) | |
468 (nth 2 contents) (nth 3 contents))))) | |
469 (if (nthcdr 5 contents) | |
470 (setq body (cons (list 'interactive (nth 5 contents)) body))) | |
471 (if (nth 4 contents) | |
472 ;; Use `documentation' here, to get the actual string, | |
473 ;; in case the compiled function has a reference | |
474 ;; to the .elc file. | |
475 (setq body (cons (documentation function) body))) | |
476 (fset function (cons 'lambda (cons (car contents) body))))))) | |
477 | |
478 (defun debug-on-entry-1 (function defn flag) | |
479 (if (subrp defn) | |
480 (error "%s is a built-in function" function) | |
481 (if (eq (car defn) 'macro) | |
482 (debug-on-entry-1 function (cdr defn) flag) | |
483 (or (eq (car defn) 'lambda) | |
484 (error "%s not user-defined Lisp function" function)) | |
485 (let (tail prec) | |
486 (if (stringp (car (nthcdr 2 defn))) | |
487 (setq tail (nthcdr 3 defn) | |
488 prec (list (car defn) (car (cdr defn)) (car (cdr (cdr defn))))) | |
489 (setq tail (nthcdr 2 defn) | |
490 prec (list (car defn) (car (cdr defn))))) | |
491 (if (eq flag (equal (car tail) '(debug 'debug))) | |
492 defn | |
493 (if flag | |
494 (nconc prec (cons '(debug 'debug) tail)) | |
495 (nconc prec (cdr tail)))))))) | |
496 | |
497 (defun debugger-list-functions () | |
498 "Display a list of all the functions now set to debug on entry." | |
499 (interactive) | |
500 (with-output-to-temp-buffer "*Help*" | |
501 (if (null debug-function-list) | |
502 (princ "No debug-on-entry functions now\n") | |
503 (princ "Functions set to debug on entry:\n\n") | |
504 (let ((list debug-function-list)) | |
505 (while list | |
506 (prin1 (car list)) | |
507 (terpri) | |
508 (setq list (cdr list)))) | |
509 (princ "Note: if you have redefined a function, then it may no longer\n") | |
510 (princ "be set to debug on entry, even if it is in the list.")) | |
511 (save-excursion | |
512 (set-buffer standard-output) | |
513 (help-mode)))) | |
514 | |
515 ;;; debug.el ends here |