There are three ways to investigate a problem in an Emacs Lisp program, depending on what you are doing with the program when the problem appears.
Another useful debugging tool is the dribble file. When a dribble file is open, Emacs copies all keyboard input characters to that file. Afterward, you can examine the file to find out what input was used. See section Terminal Input.
For debugging problems in terminal descriptions, the
open-termscript
function can be useful. See section Terminal Output.
The ordinary Lisp debugger provides the ability to suspend evaluation of a form. While evaluation is suspended (a state that is commonly known as a break), you may examine the run time stack, examine the values of local or global variables, or change those values. Since a break is a recursive edit, all the usual editing facilities of Emacs are available; you can even run programs that will enter the debugger recursively. See section Recursive Editing.
The most important time to enter the debugger is when a Lisp error happens. This allows you to investigate the immediate causes of the error.
However, entry to the debugger is not a normal consequence of an
error. Many commands frequently cause Lisp errors when invoked
inappropriately (such as C-f at the end of the buffer), and during
ordinary editing it would be very inconvenient to enter the debugger
each time this happens. So if you want errors to enter the debugger, set
the variable debug-on-error
to non-nil
. (The command
toggle-debug-on-error
provides an easy way to do this.)
debug-on-error
is t
, all
kinds of errors call the debugger (except those listed in
debug-ignored-errors
). If it is nil
, none call the
debugger.
The value can also be a list of error conditions that should call the
debugger. For example, if you set it to the list
(void-variable)
, then only errors about a variable that has no
value invoke the debugger.
When this variable is non-nil
, Emacs does not create an error
handler around process filter functions and sentinels. Therefore,
errors in these functions also invoke the debugger. See section Processes.
debug-on-error
.
The normal value of this variable lists several errors that happen often
during editing but rarely result from bugs in Lisp programs. However,
"rarely" is not "never"; if your program fails with an error that
matches this list, you will need to change this list in order to debug
the error. The easiest way is usually to set
debug-ignored-errors
to nil
.
condition-case
never run the
debugger, even if debug-on-error
is non-nil
. In other
words, condition-case
gets a chance to handle the error before
the debugger gets a chance.
If you set debug-on-signal
to a non-nil
value, then the
debugger gets the first chance at every error; an error will invoke the
debugger regardless of any condition-case
, if it fits the
criteria specified by the values of debug-on-error
and
debug-ignored-errors
.
Warning: This variable is strong medicine! Various parts of
Emacs handle errors in the normal course of affairs, and you may not
even realize that errors happen there. If you set
debug-on-signal
to a non-nil
value, those errors will
enter the debugger.
Warning: debug-on-signal
has no effect when
debug-on-error
is nil
.
To debug an error that happens during loading of the `.emacs'
file, use the option `--debug-init', which binds
debug-on-error
to t
while loading `.emacs', and
bypasses the condition-case
which normally catches errors in the
init file.
If your `.emacs' file sets debug-on-error
, the effect may
not last past the end of loading `.emacs'. (This is an undesirable
byproduct of the code that implements the `--debug-init' command
line option.) The best way to make `.emacs' set
debug-on-error
permanently is with after-init-hook
, like
this:
(add-hook 'after-init-hook '(lambda () (setq debug-on-error t)))
When a program loops infinitely and fails to return, your first problem is to stop the loop. On most operating systems, you can do this with C-g, which causes quit.
Ordinary quitting gives no information about why the program was
looping. To get more information, you can set the variable
debug-on-quit
to non-nil
. Quitting with C-g is not
considered an error, and debug-on-error
has no effect on the
handling of C-g. Likewise, debug-on-quit
has no effect on
errors.
Once you have the debugger running in the middle of the infinite loop, you can proceed from the debugger using the stepping commands. If you step through the entire loop, you will probably get enough information to solve the problem.
quit
is signaled and not handled. If debug-on-quit
is non-nil
,
then the debugger is called whenever you quit (that is, type C-g).
If debug-on-quit
is nil
, then the debugger is not called
when you quit. See section Quitting.
To investigate a problem that happens in the middle of a program, one useful technique is to enter the debugger whenever a certain function is called. You can do this to the function in which the problem occurs, and then step through the function, or you can do this to a function called shortly before the problem, step quickly over the call to that function, and then step through its caller.
(debug 'debug)
into
the function definition as the first form.
Any function defined as Lisp code may be set to break on entry, regardless of whether it is interpreted code or compiled code. If the function is a command, it will enter the debugger when called from Lisp and when called interactively (after the reading of the arguments). You can't debug primitive functions (i.e., those written in C) this way.
When debug-on-entry
is called interactively, it prompts for
function-name in the minibuffer. If the function is already set
up to invoke the debugger on entry, debug-on-entry
does nothing.
debug-on-entry
always returns function-name.
Note: if you redefine a function after using
debug-on-entry
on it, the code to enter the debugger is discarded
by the redefinition. In effect, redefining the function cancels
the break-on-entry feature for that function.
(defun fact (n) (if (zerop n) 1 (* n (fact (1- n))))) => fact (debug-on-entry 'fact) => fact (fact 3) ------ Buffer: *Backtrace* ------ Entering: * fact(3) eval-region(4870 4878 t) byte-code("...") eval-last-sexp(nil) (let ...) eval-insert-last-sexp(nil) * call-interactively(eval-insert-last-sexp) ------ Buffer: *Backtrace* ------ (symbol-function 'fact) => (lambda (n) (debug (quote debug)) (if (zerop n) 1 (* n (fact (1- n)))))
debug-on-entry
on
function-name. When called interactively, it prompts for
function-name in the minibuffer. If function-name is
nil
or the empty string, it cancels break-on-entry for all
functions.
Calling cancel-debug-on-entry
does nothing to a function which is
not currently set up to break on entry. It always returns
function-name.
You can cause the debugger to be called at a certain point in your
program by writing the expression (debug)
at that point. To do
this, visit the source file, insert the text `(debug)' at the
proper place, and type C-M-x. Warning: if you do this
for temporary debugging purposes, be sure to undo this insertion before
you save the file!
The place where you insert `(debug)' must be a place where an
additional form can be evaluated and its value ignored. (If the value
of (debug)
isn't ignored, it will alter the execution of the
program!) The most common suitable places are inside a progn
or
an implicit progn
(see section Sequencing).
When the debugger is entered, it displays the previously selected buffer in one window and a buffer named `*Backtrace*' in another window. The backtrace buffer contains one line for each level of Lisp function execution currently going on. At the beginning of this buffer is a message describing the reason that the debugger was invoked (such as the error message and associated data, if it was invoked due to an error).
The backtrace buffer is read-only and uses a special major mode, Debugger mode, in which letters are defined as debugger commands. The usual Emacs editing commands are available; thus, you can switch windows to examine the buffer that was being edited at the time of the error, switch buffers, visit files, or do any other sort of editing. However, the debugger is a recursive editing level (see section Recursive Editing) and it is wise to go back to the backtrace buffer and exit the debugger (with the q command) when you are finished with it. Exiting the debugger gets out of the recursive edit and kills the backtrace buffer.
The backtrace buffer shows you the functions that are executing and their argument values. It also allows you to specify a stack frame by moving point to the line describing that frame. (A stack frame is the place where the Lisp interpreter records information about a particular invocation of a function.) The frame whose line point is on is considered the current frame. Some of the debugger commands operate on the current frame.
The debugger itself must be run byte-compiled, since it makes assumptions about how many stack frames are used for the debugger itself. These assumptions are false if the debugger is running interpreted.
Inside the debugger (in Debugger mode), these special commands are available in addition to the usual cursor motion commands. (Keep in mind that all the usual facilities of Emacs, such as switching windows or buffers, are still available.)
The most important use of debugger commands is for stepping through code, so that you can see how control flows. The debugger can step through the control structures of an interpreted function, but cannot do so in a byte-compiled function. If you would like to step through a byte-compiled function, replace it with an interpreted definition of the same function. (To do this, visit the source for the function and type C-M-x on its definition.)
Here is a list of Debugger mode commands:
debug
and use its return value. Otherwise, r has the same
effect as c, and the specified return value does not matter.
You can't use r when the debugger was entered due to an error.
Here we describe in full detail the function debug
that is used
to invoke the debugger.
The Debugger mode c and r commands exit the recursive edit;
then debug
switches back to the previous buffer and returns to
whatever called debug
. This is the only way the function
debug
can return to its caller.
The use of the debugger-args is that debug
displays the
rest of its arguments at the top of the `*Backtrace*' buffer, so
that the user can see them. Except as described below, this is the
only way these arguments are used.
However, certain values for first argument to debug
have a
special significance. (Normally, these values are used only by the
internals of Emacs, and not by programmers calling debug
.) Here
is a table of these special values:
lambda
lambda
means debug
was called because
of entry to a function when debug-on-next-call
was
non-nil
. The debugger displays `Entering:' as a line of
text at the top of the buffer.
debug
debug
as first argument indicates a call to debug
because
of entry to a function that was set to debug on entry. The debugger
displays `Entering:', just as in the lambda
case. It also
marks the stack frame for that function so that it will invoke the
debugger when exited.
t
t
, this indicates a call to
debug
due to evaluation of a list form when
debug-on-next-call
is non-nil
. The debugger displays the
following as the top line in the buffer:
Beginning evaluation of function call form:
exit
exit
, it indicates the exit of a stack
frame previously marked to invoke the debugger on exit. The second
argument given to debug
in this case is the value being returned
from the frame. The debugger displays `Return value:' in the top
line of the buffer, followed by the value being returned.
error
error
, the debugger indicates that
it is being entered because an error or quit
was signaled and not
handled, by displaying `Signaling:' followed by the error signaled
and any arguments to signal
. For example,
(let ((debug-on-error t)) (/ 1 0)) ------ Buffer: *Backtrace* ------ Signaling: (arith-error) /(1 0) ... ------ Buffer: *Backtrace* ------If an error was signaled, presumably the variable
debug-on-error
is non-nil
. If quit
was signaled,
then presumably the variable debug-on-quit
is non-nil
.
nil
nil
as the first of the debugger-args when you want
to enter the debugger explicitly. The rest of the debugger-args
are printed on the top line of the buffer. You can use this feature to
display messages--for example, to remind yourself of the conditions
under which debug
is called.
This section describes functions and variables used internally by the debugger.
debug
.
The first argument that Lisp hands to the function indicates why it
was called. The convention for arguments is detailed in the description
of debug
.
debug
to fill up the
`*Backtrace*' buffer. It is written in C, since it must have access
to the stack to determine which function calls are active. The return
value is always nil
.
In the following example, a Lisp expression calls backtrace
explicitly. This prints the backtrace to the stream
standard-output
: in this case, to the buffer
`backtrace-output'. Each line of the backtrace represents one
function call. The line shows the values of the function's arguments if
they are all known. If they are still being computed, the line says so.
The arguments of special forms are elided.
(with-output-to-temp-buffer "backtrace-output" (let ((var 1)) (save-excursion (setq var (eval '(progn (1+ var) (list 'testing (backtrace)))))))) => nil ----------- Buffer: backtrace-output ------------ backtrace() (list ...computing arguments...) (progn ...) eval((progn (1+ var) (list (quote testing) (backtrace)))) (setq ...) (save-excursion ...) (let ...) (with-output-to-temp-buffer ...) eval-region(1973 2142 #<buffer *scratch*>) byte-code("... for eval-print-last-sexp ...") eval-print-last-sexp(nil) * call-interactively(eval-print-last-sexp) ----------- Buffer: backtrace-output ------------
The character `*' indicates a frame whose debug-on-exit flag is set.
nil
, it says to call the debugger before
the next eval
, apply
or funcall
. Entering the
debugger sets debug-on-next-call
to nil
.
The d command in the debugger works by setting this variable.
nil
, this will cause the debugger to be entered when that
frame later exits. Even a nonlocal exit through that frame will enter
the debugger.
This function is used only by the debugger.
nil
. The debugger can set this variable to leave
information for future debugger invocations during the same command
invocation.
The advantage, for the debugger, of using this variable rather than an ordinary global variable is that the data will never carry over to a subsequent command invocation.
backtrace-frame
is intended for use in Lisp
debuggers. It returns information about what computation is happening
in the stack frame frame-number levels down.
If that frame has not evaluated the arguments yet (or is a special
form), the value is (nil function arg-forms...)
.
If that frame has evaluated its arguments and called its function
already, the value is (t function
arg-values...)
.
In the return value, function is whatever was supplied as the
CAR of the evaluated list, or a lambda
expression in the
case of a macro call. If the function has a &rest
argument, that
is represented as the tail of the list arg-values.
If frame-number is out of range, backtrace-frame
returns
nil
.
Edebug is a source-level debugger for Emacs Lisp programs with which you can:
The first three sections below should tell you enough about Edebug to enable you to use it.
To debug a Lisp program with Edebug, you must first instrument
the Lisp code that you want to debug. A simple way to do this is to
first move point into the definition of a function or macro and then do
C-u C-M-x (eval-defun
with a prefix argument). See
section Instrumenting for Edebug, for alternative ways to instrument code.
Once a function is instrumented, any call to the function activates Edebug. Activating Edebug may stop execution and let you step through the function, or it may update the display and continue execution while checking for debugging commands, depending on which Edebug execution mode you have selected. The default execution mode is step, which does stop execution. See section Edebug Execution Modes.
Within Edebug, you normally view an Emacs buffer showing the source of the Lisp code you are debugging. This is referred to as the source code buffer. This buffer is temporarily read-only.
An arrow at the left margin indicates the line where the function is executing. Point initially shows where within the line the function is executing, but this ceases to be true if you move point yourself.
If you instrument the definition of fac
(shown below) and then
execute (fac 3)
, here is what you normally see. Point is at the
open-parenthesis before if
.
(defun fac (n) =>-!-(if (< 0 n) (* n (fac (1- n))) 1))
The places within a function where Edebug can stop execution are called
stop points. These occur both before and after each subexpression
that is a list, and also after each variable reference.
Here we show with periods the stop points found in the function
fac
:
(defun fac (n) .(if .(< 0 n.). .(* n. .(fac (1- n.).).). 1).)
The special commands of Edebug are available in the source code buffer
in addition to the commands of Emacs Lisp mode. For example, you can
type the Edebug command SPC to execute until the next stop point.
If you type SPC once after entry to fac
, here is the
display you will see:
(defun fac (n) =>(if -!-(< 0 n) (* n (fac (1- n))) 1))
When Edebug stops execution after an expression, it displays the expression's value in the echo area.
Other frequently used commands are b to set a breakpoint at a stop point, g to execute until a breakpoint is reached, and q to exit Edebug and return to the top-level command loop. Type ? to display a list of all Edebug commands.
In order to use Edebug to debug Lisp code, you must first instrument the code. Instrumenting code inserts additional code into it, to invoke Edebug at the proper places.
Once you have loaded Edebug, the command C-M-x
(eval-defun
) is redefined so that when invoked with a prefix
argument on a definition, it instruments the definition before
evaluating it. (The source code itself is not modified.) If the
variable edebug-all-defs
is non-nil
, that inverts the
meaning of the prefix argument: then C-M-x instruments the
definition unless it has a prefix argument. The default value of
edebug-all-defs
is nil
. The command M-x
edebug-all-defs toggles the value of the variable
edebug-all-defs
.
If edebug-all-defs
is non-nil
, then the commands
eval-region
, eval-current-buffer
, and eval-buffer
also instrument any definitions they evaluate. Similarly,
edebug-all-forms
controls whether eval-region
should
instrument any form, even non-defining forms. This doesn't apply
to loading or evaluations in the minibuffer. The command M-x
edebug-all-forms toggles this option.
Another command, M-x edebug-eval-top-level-form, is available to
instrument any top-level form regardless of the values of
edebug-all-defs
and edebug-all-forms
.
While Edebug is active, the command I
(edebug-instrument-callee
) instruments the definition of the
function or macro called by the list form after point, if is not already
instrumented. This is possible only if Edebug knows where to find the
source for that function; after loading Edebug, eval-region
records the position of every definition it evaluates, even if not
instrumenting it. See also the i command (see section Jumping), which
steps into the call after instrumenting the function.
Edebug knows how to instrument all the standard special forms,
interactive
forms with an expression argument, anonymous lambda
expressions, and other defining forms. Edebug cannot know what a
user-defined macro will do with the arguments of a macro call, so you
must tell it; see section Instrumenting Macro Calls, for details.
When Edebug is about to instrument code for the first time in a
session, it runs the hook edebug-setup-hook
, then sets it to
nil
. You can use this to arrange to load Edebug specifications
(see section Instrumenting Macro Calls) associated with a package you are
using, but actually load them only if you use Edebug.
To remove instrumentation from a definition, simply re-evaluate its
definition in a way that does not instrument. There are two ways of
evaluating forms that never instrument them: from a file with
load
, and from the minibuffer with eval-expression
(M-:).
If Edebug detects a syntax error while instrumenting, it leaves point
at the erroneous code and signals an invalid-read-syntax
error.
See section Evaluation, for other evaluation functions available inside of Edebug.
Edebug supports several execution modes for running the program you are debugging. We call these alternatives Edebug execution modes; do not confuse them with major or minor modes. The current Edebug execution mode determines how far Edebug continues execution before stopping--whether it stops at each stop point, or continues to the next breakpoint, for example--and how much Edebug displays the progress of the evaluation before it stops.
Normally, you specify the Edebug execution mode by typing a command to continue the program in a certain mode. Here is a table of these commands. All except for S resume execution of the program, at least for a certain distance.
edebug-stop
).
edebug-step-mode
).
edebug-next-mode
). Also see edebug-forward-sexp
in
section Miscellaneous Edebug Commands.
edebug-trace-mode
).
edebug-Trace-fast-mode
).
edebug-go-mode
). See section Breakpoints.
edebug-continue-mode
).
edebug-Continue-fast-mode
).
edebug-Go-nonstop-mode
). You
can still stop the program by typing S, or any editing command.
In general, the execution modes earlier in the above list run the program more slowly or stop sooner than the modes later in the list.
While executing or tracing, you can interrupt the execution by typing any Edebug command. Edebug stops the program at the next stop point and then executes the command you typed. For example, typing t during execution switches to trace mode at the next stop point. You can use S to stop execution without doing anything else.
If your function happens to read input, a character you type intending to interrupt execution may be read by the function instead. You can avoid such unintended results by paying attention to when your program wants input.
Keyboard macros containing the commands in this section do not
completely work: exiting from Edebug, to resume the program, loses track
of the keyboard macro. This is not easy to fix. Also, defining or
executing a keyboard macro outside of Edebug does not affect commands
inside Edebug. This is usually an advantage. But see the
edebug-continue-kbd-macro
option (see section Edebug Options).
When you enter a new Edebug level, the initial execution mode comes from
the value of the variable edebug-initial-mode
. By default, this
specifies step mode. Note that you may reenter the same Edebug level
several times if, for example, an instrumented function is called
several times from one command.
The commands described in this section execute until they reach a specified location. All except i make a temporary breakpoint to establish the place to stop, then switch to go mode. Any other breakpoint reached before the intended stop point will also stop execution. See section Breakpoints, for the details on breakpoints.
These commands may fail to work as expected in case of nonlocal exit, because a nonlocal exit can bypass the temporary breakpoint where you expected the program to stop.
edebug-goto-here
).
edebug-forward-sexp
).
The h command proceeds to the stop point near the current location of point, using a temporary breakpoint. See section Breakpoints, for more information about breakpoints.
The f command runs the program forward over one expression. More precisely, it sets a temporary breakpoint at the position that C-M-f would reach, then executes in go mode so that the program will stop at breakpoints.
With a prefix argument n, the temporary breakpoint is placed n sexps beyond point. If the containing list ends before n more elements, then the place to stop is after the containing expression.
Be careful that the position C-M-f finds is a place that the
program will really get to; this may not be true in a
cond
, for example.
The f command does forward-sexp
starting at point, rather
than at the stop point, for flexibility. If you want to execute one
expression from the current stop point, type w first, to
move point there, and then type f.
The o command continues "out of" an expression. It places a temporary breakpoint at the end of the sexp containing point. If the containing sexp is a function definition itself, o continues until just before the last sexp in the definition. If that is where you are now, it returns from the function and then stops. In other words, this command does not exit the currently executing function unless you are positioned after the last sexp.
The i command steps into the function or macro called by the list form after point, and stops at its first stop point. Note that the form need not be the one about to be evaluated. But if the form is a function call about to be evaluated, remember to use this command before any of the arguments are evaluated, since otherwise it will be too late.
The i command instruments the function or macro it's supposed to step into, if it isn't instrumented already. This is convenient, but keep in mind that the function or macro remains instrumented unless you explicitly arrange to deinstrument it.
Some miscellaneous Edebug commands are described here.
edebug-help
).
abort-recursive-edit
).
top-level
). This
exits all recursive editing levels, including all levels of Edebug
activity. However, instrumented code protected with
unwind-protect
or condition-case
forms may resume
debugging.
top-level-nonstop
).
edebug-previous-result
).
edebug-backtrace
).
You cannot use debugger commands in the backtrace buffer in Edebug as
you would in the standard debugger.
The backtrace buffer is killed automatically when you continue
execution.
From the Edebug recursive edit, you may invoke commands that activate Edebug again recursively. Any time Edebug is active, you can quit to the top level with q or abort one recursive edit level with C-]. You can display a backtrace of all the pending evaluations with d.
Edebug's step mode stops execution at the next stop point reached. There are three other ways to stop Edebug execution once it has started: breakpoints, the global break condition, and source breakpoints.
While using Edebug, you can specify breakpoints in the program you are testing: points where execution should stop. You can set a breakpoint at any stop point, as defined in section Using Edebug. For setting and unsetting breakpoints, the stop point that is affected is the first one at or after point in the source code buffer. Here are the Edebug commands for breakpoints:
edebug-set-breakpoint
). If you use a prefix argument, the
breakpoint is temporary (it turns off the first time it stops the
program).
edebug-unset-breakpoint
).
nil
value
(edebug-set-conditional-breakpoint
). With a prefix argument, the
breakpoint is temporary.
edebug-next-breakpoint
).
While in Edebug, you can set a breakpoint with b and unset one with u. First move point to the Edebug stop point of your choice, then type b or u to set or unset a breakpoint there. Unsetting a breakpoint where none has been set has no effect.
Re-evaluating or reinstrumenting a definition forgets all its breakpoints.
A conditional breakpoint tests a condition each time the program
gets there. Any errors that occur as a result of evaluating the
condition are ignored, as if the result were nil
. To set a
conditional breakpoint, use x, and specify the condition
expression in the minibuffer. Setting a conditional breakpoint at a
stop point that has a previously established conditional breakpoint puts
the previous condition expression in the minibuffer so you can edit it.
You can make a conditional or unconditional breakpoint temporary by using a prefix argument with the command to set the breakpoint. When a temporary breakpoint stops the program, it is automatically unset.
Edebug always stops or pauses at a breakpoint except when the Edebug mode is Go-nonstop. In that mode, it ignores breakpoints entirely.
To find out where your breakpoints are, use the B command, which moves point to the next breakpoint following point, within the same function, or to the first breakpoint if there are no following breakpoints. This command does not continue execution--it just moves point in the buffer.
A global break condition stops execution when a specified
condition is satisfied, no matter where that may occur. Edebug
evaluates the global break condition at every stop point. If it
evaluates to a non-nil
value, then execution stops or pauses
depending on the execution mode, as if a breakpoint had been hit. If
evaluating the condition gets an error, execution does not stop.
The condition expression is stored in
edebug-global-break-condition
. You can specify a new expression
using the X command (edebug-set-global-break-condition
).
The global break condition is the simplest way to find where in your
code some event occurs, but it makes code run much more slowly. So you
should reset the condition to nil
when not using it.
All breakpoints in a definition are forgotten each time you
reinstrument it. To make a breakpoint that won't be forgotten, you can
write a source breakpoint, which is simply a call to the function
edebug
in your source code. You can, of course, make such a call
conditional. For example, in the fac
function, insert the first
line as shown below to stop when the argument reaches zero:
(defun fac (n) (if (= n 0) (edebug)) (if (< 0 n) (* n (fac (1- n))) 1))
When the fac
definition is instrumented and the function is
called, the call to edebug
acts as a breakpoint. Depending on
the execution mode, Edebug stops or pauses there.
If no instrumented code is being executed when edebug
is called,
that function calls debug
.
Emacs normally displays an error message when an error is signaled and
not handled with condition-case
. While Edebug is active and
executing instrumented code, it normally responds to all unhandled
errors. You can customize this with the options edebug-on-error
and edebug-on-quit
; see section Edebug Options.
When Edebug responds to an error, it shows the last stop point encountered before the error. This may be the location of a call to a function which was not instrumented, within which the error actually occurred. For an unbound variable error, the last known stop point might be quite distant from the offending variable reference. In that case you might want to display a full backtrace (see section Miscellaneous Edebug Commands).
If you change debug-on-error
or debug-on-quit
while
Edebug is active, these changes will be forgotten when Edebug becomes
inactive. Furthermore, during Edebug's recursive edit, these variables
are bound to the values they had outside of Edebug.
These Edebug commands let you view aspects of the buffer and window status as they were before entry to Edebug. The outside window configuration is the collection of windows and contents that were in effect outside of Edebug.
edebug-view-outside
).
edebug-bounce-point
). With a prefix argument n,
pause for n seconds instead.
edebug-where
).
If you use this command in a different window displaying the same
buffer, that window will be used instead to display the current
definition in the future.
edebug-toggle-save-windows
).
With a prefix argument, W
only toggles saving and restoring of
the selected window. To specify a window that is not displaying the
source code buffer, you must use C-x X W from the global keymap.
You can view the outside window configuration with v or just bounce to the point in the current buffer with p, even if it is not normally displayed. After moving point, you may wish to jump back to the stop point with w from a source code buffer.
Each time you use W to turn saving off, Edebug forgets the saved outside window configuration--so that even if you turn saving back on, the current window configuration remains unchanged when you next exit Edebug (by continuing the program). However, the automatic redisplay of `*edebug*' and `*edebug-trace*' may conflict with the buffers you wish to see unless you have enough windows open.
While within Edebug, you can evaluate expressions "as if" Edebug were not running. Edebug tries to be invisible to the expression's evaluation and printing. Evaluation of expressions that cause side effects will work as expected except for things that Edebug explicitly saves and restores. See section The Outside Context, for details on this process.
edebug-eval-expression
). That is, Edebug tries to minimize its
interference with the evaluation.
edebug-eval-last-sexp
).
Edebug supports evaluation of expressions containing references to
lexically bound symbols created by the following constructs in
`cl.el' (version 2.03 or later): lexical-let
,
macrolet
, and symbol-macrolet
.
You can use the evaluation list buffer, called `*edebug*', to evaluate expressions interactively. You can also set up the evaluation list of expressions to be evaluated automatically each time Edebug updates the display.
edebug-visit-eval-list
).
In the `*edebug*' buffer you can use the commands of Lisp Interaction mode (see section `Lisp Interaction' in The GNU Emacs Manual) as well as these special commands:
edebug-eval-print-last-sexp
).
edebug-eval-last-sexp
).
edebug-update-eval-list
).
edebug-delete-eval-item
).
edebug-where
).
You can evaluate expressions in the evaluation list window with C-j or C-x C-e, just as you would in `*scratch*'; but they are evaluated in the context outside of Edebug.
The expressions you enter interactively (and their results) are lost when you continue execution; but you can set up an evaluation list consisting of expressions to be evaluated each time execution stops.
To do this, write one or more evaluation list groups in the evaluation list buffer. An evaluation list group consists of one or more Lisp expressions. Groups are separated by comment lines.
The command C-c C-u (edebug-update-eval-list
) rebuilds the
evaluation list, scanning the buffer and using the first expression of
each group. (The idea is that the second expression of the group is the
value previously computed and displayed.)
Each entry to Edebug redisplays the evaluation list by inserting each expression in the buffer, followed by its current value. It also inserts comment lines so that each expression becomes its own group. Thus, if you type C-c C-u again without changing the buffer text, the evaluation list is effectively unchanged.
If an error occurs during an evaluation from the evaluation list, the error message is displayed in a string as if it were the result. Therefore, expressions that use variables not currently valid do not interrupt your debugging.
Here is an example of what the evaluation list window looks like after several expressions have been added to it:
(current-buffer) #<buffer *scratch*> ;--------------------------------------------------------------- (selected-window) #<window 16 on *scratch*> ;--------------------------------------------------------------- (point) 196 ;--------------------------------------------------------------- bad-var "Symbol's value as variable is void: bad-var" ;--------------------------------------------------------------- (recursion-depth) 0 ;--------------------------------------------------------------- this-command eval-last-sexp ;---------------------------------------------------------------
To delete a group, move point into it and type C-c C-d, or simply delete the text for the group and update the evaluation list with C-c C-u. To add a new expression to the evaluation list, insert the expression at a suitable place, and insert a new comment line. (You need not insert dashes in the comment line--its contents don't matter.) Then type C-c C-u.
After selecting `*edebug*', you can return to the source code buffer with C-c C-w. The `*edebug*' buffer is killed when you continue execution, and recreated next time it is needed.
If an expression in your program produces a value containing circular list structure, you may get an error when Edebug attempts to print it.
One way to cope with circular structure is to set print-length
or print-level
to truncate the printing. Edebug does this for
you; it binds print-length
and print-level
to 50 if they
were nil
. (Actually, the variables edebug-print-length
and edebug-print-level
specify the values to use within Edebug.)
See section Variables Affecting Output.
nil
, bind print-length
to this while printing
results in Edebug. The default value is 50
.
nil
, bind print-level
to this while printing
results in Edebug. The default value is 50
.
You can also print circular structures and structures that share elements more informatively by using the `cust-print' package.
To load `cust-print' and activate custom printing only for Edebug, simply use the command M-x edebug-install-custom-print. To restore the standard print functions, use M-x edebug-uninstall-custom-print.
Here is an example of code that creates a circular structure:
(setq a '(x y)) (setcar a a)
Custom printing prints this as `Result: #1=(#1# y)'. The `#1=' notation labels the structure that follows it with the label `1', and the `#1#' notation references the previously labeled structure. This notation is used for any shared elements of lists or vectors.
nil
, bind print-circle
to this while printing
results in Edebug. The default value is nil
.
Other programs can also use custom printing; see `cust-print.el' for details.
Edebug can record an execution trace, storing it in a buffer named
`*edebug-trace*'. This is a log of function calls and returns,
showing the function names and their arguments and values. To enable
trace recording, set edebug-trace
to a non-nil
value.
Making a trace buffer is not the same thing as using trace execution mode (see section Edebug Execution Modes).
When trace recording is enabled, each function entry and exit adds lines to the trace buffer. A function entry record looks like `::::{' followed by the function name and argument values. A function exit record looks like `::::}' followed by the function name and result of the function.
The number of `:'s in an entry shows its recursion depth. You can use the braces in the trace buffer to find the matching beginning or end of function calls.
You can customize trace recording for function entry and exit by
redefining the functions edebug-print-trace-before
and
edebug-print-trace-after
.
edebug-tracing
returns the value of the last form in body.
(apply 'format format-string format-args)
.
It also appends a newline to separate entries.
edebug-tracing
and edebug-trace
insert lines in the
trace buffer whenever they are called, even if Edebug is not active.
Adding text to the trace buffer also scrolls its window to show the last
lines inserted.
Edebug provides rudimentary coverage testing and display of execution frequency.
Coverage testing works by comparing the result of each expression with the previous result; each form in the program is considered "covered" if it has returned two different values since you began testing coverage in the current Emacs session. Thus, to do coverage testing on your program, execute it under various conditions and note whether it behaves correctly; Edebug will tell you when you have tried enough different conditions that each form has returned two different values.
Coverage testing makes execution slower, so it is only done if
edebug-test-coverage
is non-nil
. Frequency counting is
performed for all execution of an instrumented function, even if the
execution mode is Go-nonstop, and regardless of whether coverage testing
is enabled.
Use M-x edebug-display-freq-count to display both the coverage information and the frequency counts for a definition.
The frequency counts appear as comment lines after each line of code,
and you can undo all insertions with one undo
command. The
counts appear under the `(' before an expression or the `)'
after an expression, or on the last character of a variable. To
simplify the display, a count is not shown if it is equal to the
count of an earlier expression on the same line.
The character `=' following the count for an expression says that the expression has returned the same value each time it was evaluated. In other words, it is not yet "covered" for coverage testing purposes.
To clear the frequency count and coverage data for a definition,
simply reinstrument it with eval-defun
.
For example, after evaluating (fac 5)
with a source
breakpoint, and setting edebug-test-coverage
to t
, when
the breakpoint is reached, the frequency data looks like this:
(defun fac (n) (if (= n 0) (edebug)) ;#6 1 0 =5 (if (< 0 n) ;#5 = (* n (fac (1- n))) ;# 5 0 1)) ;# 0
The comment lines show that fac
was called 6 times. The
first if
statement returned 5 times with the same result each
time; the same is true of the condition on the second if
.
The recursive call of fac
did not return at all.
Edebug tries to be transparent to the program you are debugging, but it does not succeed completely. Edebug also tries to be transparent when you evaluate expressions with e or with the evaluation list buffer, by temporarily restoring the outside context. This section explains precisely what context Edebug restores, and how Edebug fails to be completely transparent.
Whenever Edebug is entered, it needs to save and restore certain data before even deciding whether to make trace information or stop the program.
max-lisp-eval-depth
and max-specpdl-size
are both
incremented once to reduce Edebug's impact on the stack. You could,
however, still run out of stack space when using Edebug.
executing-macro
is bound to
edebug-continue-kbd-macro
.
When Edebug needs to display something (e.g., in trace mode), it saves the current window configuration from "outside" Edebug (see section Window Configurations). When you exit Edebug (by continuing the program), it restores the previous window configuration.
Emacs redisplays only when it pauses. Usually, when you continue execution, the program comes back into Edebug at a breakpoint or after stepping without pausing or reading input in between. In such cases, Emacs never gets a chance to redisplay the "outside" configuration. What you see is the same window configuration as the last time Edebug was active, with no interruption.
Entry to Edebug for displaying something also saves and restores the following data, but some of these are deliberately not restored if an error or quit signal occurs.
edebug-save-windows
is non-nil
(see section Edebug Display Update).
The window configuration is not restored on error or quit, but the
outside selected window is reselected even on error or quit in
case a save-excursion
is active. If the value of
edebug-save-windows
is a list, only the listed windows are saved
and restored.
The window start and horizontal scrolling of the source code buffer are
not restored, however, so that the display remains coherent within Edebug.
edebug-save-displayed-buffer-points
is non-nil
.
overlay-arrow-position
and
overlay-arrow-string
are saved and restored. So you can safely
invoke Edebug from the recursive edit elsewhere in the same buffer.
cursor-in-echo-area
is locally bound to nil
so that
the cursor shows up in the window.
When Edebug is entered and actually reads commands from the user, it saves (and later restores) these additional data:
last-command
, this-command
, last-command-char
,
last-input-char
, last-input-event
,
last-command-event
, last-event-frame
,
last-nonmenu-event
, and track-mouse
. Commands used within
Edebug do not affect these variables outside of Edebug.
The key sequence returned by this-command-keys
is changed by
executing commands within Edebug and there is no way to reset
the key sequence from Lisp.
Edebug cannot save and restore the value of
unread-command-events
. Entering Edebug while this variable has a
nontrivial value can interfere with execution of the program you are
debugging.
command-history
. In rare cases this can alter execution.
standard-output
and standard-input
are bound to nil
by the recursive-edit
, but Edebug temporarily restores them during
evaluations.
defining-kbd-macro
is bound to
edebug-continue-kbd-macro
.
When Edebug instruments an expression that calls a Lisp macro, it needs additional information about the macro to do the job properly. This is because there is no a-priori way to tell which subexpressions of the macro call are forms to be evaluated. (Evaluation may occur explicitly in the macro body, or when the resulting expansion is evaluated, or any time later.)
Therefore, you must define an Edebug specification for each macro that
Edebug will encounter, to explain the format of calls to that macro. To
do this, use def-edebug-spec
.
The macro argument can actually be any symbol, not just a macro name.
Here is a simple example that defines the specification for the
for
example macro (see section Evaluating Macro Arguments Repeatedly), followed by an
alternative, equivalent specification.
(def-edebug-spec for (symbolp "from" form "to" form "do" &rest form)) (def-edebug-spec for (symbolp ['from form] ['to form] ['do body]))
Here is a table of the possibilities for specification and how each directs processing of arguments.
t
0
A specification list is required for an Edebug specification if
some arguments of a macro call are evaluated while others are not. Some
elements in a specification list match one or more arguments, but others
modify the processing of all following elements. The latter, called
specification keywords, are symbols beginning with `&' (such
as &optional
).
A specification list may contain sublists which match arguments that are themselves lists, or it may contain vectors used for grouping. Sublists and groups thus subdivide the specification list into a hierarchy of levels. Specification keywords apply only to the remainder of the sublist or group they are contained in.
When a specification list involves alternatives or repetition, matching it against an actual macro call may require backtracking. See section Backtracking in Specifications, for more details.
Edebug specifications provide the power of regular expression matching, plus some context-free grammar constructs: the matching of sublists with balanced parentheses, recursive processing of forms, and recursion via indirect specifications.
Here's a table of the possible elements of a specification list, with their meanings:
sexp
form
place
setf
construct.
body
&rest form
. See &rest
below.
function-form
quote
rather than
function
, since it instruments the body of the lambda expression
either way.
lambda-expr
&optional
[&optional specs...]
. To specify that several
elements must all match or none, use &optional
[specs...]
. See the defun
example below.
&rest
[&rest specs...]
.
To specify several elements that must all match on every repetition, use
&rest [specs...]
.
&or
&or
specification fails.
Each list element following &or
is a single alternative. To
group two or more list elements as a single alternative, enclose them in
[...]
.
¬
&or
, but if any of them match, the specification fails. If none
of them match, nothing is matched, but the ¬
specification
succeeds.
&define
&define
keyword should be the first element in
a list specification.
nil
gate
let
example
below.
other-symbol
def-edebug-spec
just as for macros. See the defun
example below.
Otherwise, the symbol should be a predicate. The predicate is called
with the argument and the specification fails if the predicate returns
nil
. In either case, that argument is not instrumented.
Some suitable predicates include symbolp
, integerp
,
stringp
, vectorp
, and atom
.
[elements...]
"string"
'symbol
, where the name
of symbol is the string, but the string form is preferred.
(vector elements...)
(elements...)
(spec . [(more
specs...)])
) whose elements match the non-dotted list arguments.
This is useful in recursive specifications such as in the backquote
example below. Also see the description of a nil
specification
above for terminating such recursion.
Note that a sublist specification written as (specs . nil)
is equivalent to (specs)
, and (specs .
(sublist-elements...))
is equivalent to (specs
sublist-elements...)
.
Here is a list of additional specifications that may appear only after
&define
. See the defun
example below.
name
:name
:name
should be a symbol; it is used as an additional
name component for the definition. You can use this to add a unique,
static component to the name of the definition. It may be used more
than once.
arg
lambda-list
def-body
body
, described above, but a definition body must be instrumented
with a different Edebug call that looks up information associated with
the definition. Use def-body
for the highest level list of forms
within the definition.
def-form
def-body
, except use this to match a single form rather than
a list of forms. As a special case, def-form
also means that
tracing information is not output when the form is executed. See the
interactive
example below.
If a specification fails to match at some point, this does not
necessarily mean a syntax error will be signaled; instead,
backtracking will take place until all alternatives have been
exhausted. Eventually every element of the argument list must be
matched by some element in the specification, and every required element
in the specification must match some argument.
When a syntax error is detected, it might not be reported until much
later after higher-level alternatives have been exhausted, and with the
point positioned further from the real error. But if backtracking is
disabled when an error occurs, it can be reported immediately. Note
that backtracking is also reenabled automatically in several situations;
it is reenabled when a new alternative is established by
&optional
, &rest
, or &or
, or at the start of
processing a sublist, group, or indirect specification. The effect of
enabling or disabling backtracking is limited to the remainder of the
level currently being processed and lower levels.
Backtracking is disabled while matching any of the
form specifications (that is, form
, body
, def-form
, and
def-body
). These specifications will match any form so any error
must be in the form itself rather than at a higher level.
Backtracking is also disabled after successfully matching a quoted
symbol or string specification, since this usually indicates a
recognized construct. But if you have a set of alternative constructs that
all begin with the same symbol, you can usually work around this
constraint by factoring the symbol out of the alternatives, e.g.,
["foo" &or [first case] [second case] ...]
.
Most needs are satisfied by these two ways that bactracking is
automatically disabled, but occasionally it is useful to explicitly
disable backtracking by using the gate
specification. This is
useful when you know that no higher alternatives could apply. See the
example of the let
specification.
It may be easier to understand Edebug specifications by studying the examples provided here.
A let
special form has a sequence of bindings and a body. Each
of the bindings is either a symbol or a sublist with a symbol and
optional expression. In the specification below, notice the gate
inside of the sublist to prevent backtracking once a sublist is found.
(def-edebug-spec let ((&rest &or symbolp (gate symbolp &optional form)) body))
Edebug uses the following specifications for defun
and
defmacro
and the associated argument list and interactive
specifications. It is necessary to handle interactive forms specially
since an expression argument it is actually evaluated outside of the
function body.
(def-edebug-spec defmacro defun) ; Indirect ref todefun
spec. (def-edebug-spec defun (&define name lambda-list [&optional stringp] ; Match the doc string, if present. [&optional ("interactive" interactive)] def-body)) (def-edebug-spec lambda-list (([&rest arg] [&optional ["&optional" arg &rest arg]] &optional ["&rest" arg] ))) (def-edebug-spec interactive (&optional &or stringp def-form)) ; Notice:def-form
The specification for backquote below illustrates how to match
dotted lists and use nil
to terminate recursion. It also
illustrates how components of a vector may be matched. (The actual
specification defined by Edebug does not support dotted lists because
doing so causes very deep recursion that could fail.)
(def-edebug-spec ` (backquote-form)) ; Alias just for clarity. (def-edebug-spec backquote-form (&or ([&or "," ",@"] &or ("quote" backquote-form) form) (backquote-form . [&or nil backquote-form]) (vector &rest backquote-form) sexp))
These options affect the behavior of Edebug:
edebug-setup-hook
is reset to nil
. You could use this to
load up Edebug specifications associated with a package you are using
but only when you also use Edebug.
See section Instrumenting for Edebug.
nil
, normal evaluation of defining forms such as
defun
and defmacro
instruments them for Edebug. This
applies to eval-defun
, eval-region
, eval-buffer
,
and eval-current-buffer
.
Use the command M-x edebug-all-defs to toggle the value of this option. See section Instrumenting for Edebug.
nil
, the commands eval-defun
,
eval-region
, eval-buffer
, and eval-current-buffer
instrument all forms, even those that don't define anything.
This doesn't apply to loading or evaluations in the minibuffer.
Use the command M-x edebug-all-forms to toggle the value of this option. See section Instrumenting for Edebug.
nil
, Edebug saves and restores the window
configuration. That takes some time, so if your program does not care
what happens to the window configurations, it is better to set this
variable to nil
.
If the value is a list, only the listed windows are saved and restored.
You can use the W command in Edebug to change this variable interactively. See section Edebug Display Update.
nil
, Edebug saves and restores point in all
displayed buffers.
Saving and restoring point in other buffers is necessary if you are debugging code that changes the point of a buffer which is displayed in a non-selected window. If Edebug or the user then selects the window, point in that buffer will move to the window's value of point.
Saving and restoring point in all buffers is expensive, since it requires selecting each window twice, so enable this only if you need it. See section Edebug Display Update.
nil
, it specifies the initial execution
mode for Edebug when it is first activated. Possible values are
step
, next
, go
, Go-nonstop
, trace
,
Trace-fast
, continue
, and Continue-fast
.
The default value is step
.
See section Edebug Execution Modes.
nil
means display a trace of function entry and exit.
Tracing output is displayed in a buffer named `*edebug-trace*', one
function entry or exit per line, indented by the recursion level.
The default value is nil
.
Also see edebug-tracing
, in section Trace Buffer.
nil
, Edebug tests coverage of all expressions debugged.
See section Coverage Testing.
nil
, continue defining or executing any keyboard macro
that is executing outside of Edebug. Use this with caution since it is not
debugged.
See section Edebug Execution Modes.
debug-on-error
to this value, if
debug-on-error
was previously nil
. See section Trapping Errors.
debug-on-quit
to this value, if
debug-on-quit
was previously nil
. See section Trapping Errors.
If you change the values of edebug-on-error
or
edebug-on-quit
while Edebug is active, their values won't be used
until the next time Edebug is invoked via a new command.
nil
, an expression to test for at every stop point.
If the result is non-nil, then break. Errors are ignored.
See section Global Break Condition.
The Lisp reader reports invalid syntax, but cannot say where the real problem is. For example, the error "End of file during parsing" in evaluating an expression indicates an excess of open parentheses (or square brackets). The reader detects this imbalance at the end of the file, but it cannot figure out where the close parenthesis should have been. Likewise, "Invalid read syntax: ")"" indicates an excess close parenthesis or missing open parenthesis, but does not say where the missing parenthesis belongs. How, then, to find what to change?
If the problem is not simply an imbalance of parentheses, a useful technique is to try C-M-e at the beginning of each defun, and see if it goes to the place where that defun appears to end. If it does not, there is a problem in that defun.
However, unmatched parentheses are the most common syntax errors in Lisp, and we can give further advice for those cases. (In addition, just moving point through the code with Show Paren mode enabled might find the mismatch.)
The first step is to find the defun that is unbalanced. If there is
an excess open parenthesis, the way to do this is to insert a
close parenthesis at the end of the file and type C-M-b
(backward-sexp
). This will move you to the beginning of the
defun that is unbalanced. (Then type C-SPC C-_ C-u
C-SPC to set the mark there, undo the insertion of the
close parenthesis, and finally return to the mark.)
The next step is to determine precisely what is wrong. There is no way to be sure of this except by studying the program, but often the existing indentation is a clue to where the parentheses should have been. The easiest way to use this clue is to reindent with C-M-q and see what moves. But don't do this yet! Keep reading, first.
Before you do this, make sure the defun has enough close parentheses. Otherwise, C-M-q will get an error, or will reindent all the rest of the file until the end. So move to the end of the defun and insert a close parenthesis there. Don't use C-M-e to move there, since that too will fail to work until the defun is balanced.
Now you can go to the beginning of the defun and type C-M-q. Usually all the lines from a certain point to the end of the function will shift to the right. There is probably a missing close parenthesis, or a superfluous open parenthesis, near that point. (However, don't assume this is true; study the code to make sure.) Once you have found the discrepancy, undo the C-M-q with C-_, since the old indentation is probably appropriate to the intended parentheses.
After you think you have fixed the problem, use C-M-q again. If the old indentation actually fit the intended nesting of parentheses, and you have put back those parentheses, C-M-q should not change anything.
To deal with an excess close parenthesis, first insert an open parenthesis at the beginning of the file, back up over it, and type C-M-f to find the end of the unbalanced defun. (Then type C-SPC C-_ C-u C-SPC to set the mark there, undo the insertion of the open parenthesis, and finally return to the mark.)
Then find the actual matching close parenthesis by typing C-M-f at the beginning of that defun. This will leave you somewhere short of the place where the defun ought to end. It is possible that you will find a spurious close parenthesis in that vicinity.
If you don't see a problem at that point, the next thing to do is to type C-M-q at the beginning of the defun. A range of lines will probably shift left; if so, the missing open parenthesis or spurious close parenthesis is probably near the first of those lines. (However, don't assume this is true; study the code to make sure.) Once you have found the discrepancy, undo the C-M-q with C-_, since the old indentation is probably appropriate to the intended parentheses.
After you think you have fixed the problem, use C-M-q again. If the old indentation actually fit the intended nesting of parentheses, and you have put back those parentheses, C-M-q should not change anything.
When an error happens during byte compilation, it is normally due to invalid syntax in the program you are compiling. The compiler prints a suitable error message in the `*Compile-Log*' buffer, and then stops. The message may state a function name in which the error was found, or it may not. Either way, here is how to find out where in the file the error occurred.
What you should do is switch to the buffer ` *Compiler Input*'. (Note that the buffer name starts with a space, so it does not show up in M-x list-buffers.) This buffer contains the program being compiled, and point shows how far the byte compiler was able to read.
If the error was due to invalid Lisp syntax, point shows exactly where the invalid syntax was detected. The cause of the error is not necessarily near by! Use the techniques in the previous section to find the error.
If the error was detected while compiling a form that had been read successfully, then point is located at the end of the form. In this case, this technique can't localize the error precisely, but can still show you which function to check.
Go to the first, previous, next, last section, table of contents.