debug-on-entry
Emacs starts the debugger automatically when your function has an error.
Incidentally, you can start the debugger manually for all versions of Emacs; the advantage is that the debugger runs even if you do not have a bug in your code. Sometimes your code will be free of bugs!
You can enter the debugger when you call the function by calling
debug-on-entry
.
Type:
M-x debug-on-entry RET triangle-bugged RET
Now, evaluate the following:
(triangle-bugged 5)
All versions of Emacs will create a *Backtrace* buffer and tell
you that it is beginning to evaluate the triangle-bugged
function:
---------- Buffer: *Backtrace* ---------- Debugger entered--entering a function: * triangle-bugged(5) eval((triangle-bugged 5) nil)
eval-expression((triangle-bugged 5) nil nil 127) funcall-interactively(eval-expression (triangle-bugged 5) nil nil 127) call-interactively(eval-expression nil nil) command-execute(eval-expression) ---------- Buffer: *Backtrace* ----------
In the *Backtrace* buffer, type d. Emacs will evaluate
the first expression in triangle-bugged
; the buffer will look
like this:
---------- Buffer: *Backtrace* ---------- Debugger entered--beginning evaluation of function call form: * (let ((total 0)) (while (> number 0) (setq total ...) (setq number ...)) total) * triangle-bugged(5) eval((triangle-bugged 5))
eval((triangle-bugged 5) nil) eval-expression((triangle-bugged 5) nil nil 127) funcall-interactively(eval-expression (triangle-bugged 5) nil nil 127) call-interactively(eval-expression nil nil) command-execute(eval-expression) ---------- Buffer: *Backtrace* ----------
Now, type d again, eight times, slowly. Each time you type d, Emacs will evaluate another expression in the function definition.
Eventually, the buffer will look like this:
---------- Buffer: *Backtrace* ---------- Debugger entered--beginning evaluation of function call form: * (setq number (1= number)) * (while (> number 0) (setq total (+ total number)) (setq number (1= number)))
* (let ((total 0)) (while (> number 0) (setq total ...) (setq number ...)) total) * triangle-bugged(5) eval((triangle-bugged 5) nil)
eval-expression((triangle-bugged 5) nil nil 127) funcall-interactively(eval-expression (triangle-bugged 5) nil nil 127) call-interactively(eval-expression nil nil) command-execute(eval-expression) ---------- Buffer: *Backtrace* ----------
Finally, after you type d two more times, Emacs will reach the error, and the top two lines of the *Backtrace* buffer will look like this:
---------- Buffer: *Backtrace* ---------- Debugger entered--Lisp error: (void-function 1=) * (1= number) … ---------- Buffer: *Backtrace* ----------
By typing d, you were able to step through the function.
You can quit a *Backtrace* buffer by typing q in it; this
quits the trace, but does not cancel debug-on-entry
.
To cancel the effect of debug-on-entry
, call
cancel-debug-on-entry
and the name of the function, like this:
M-x cancel-debug-on-entry RET triangle-bugged RET
(If you are reading this in Info, cancel debug-on-entry
now.)