@c -*-texinfo-*-
@c This is part of the GNU Emacs Lisp Reference Manual.
-@c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1998 Free Software Foundation, Inc.
+@c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1998, 1999
+@c Free Software Foundation, Inc.
@c See the file elisp.texi for copying conditions.
@setfilename ../info/debugging
-@node Debugging, Read and Print, Advising, Top
+@node Debugging, Read and Print, Advising Functions, Top
@chapter Debugging Lisp Programs
There are three ways to investigate a problem in an Emacs Lisp program,
@itemize @bullet
@item
If the problem occurs when you run the program, you can use a Lisp
-debugger (either the default debugger or Edebug) to investigate what is
-happening during execution.
+debugger to investigate what is happening during execution. In addition
+to the ordinary debugger, Emacs comes with a source level debugger,
+Edebug. This chapter describes both of them.
@item
If the problem is syntactic, so that Lisp cannot even read the program,
@menu
* Debugger:: How the Emacs Lisp debugger is implemented.
+* Edebug:: A source-level Emacs Lisp debugger.
* Syntax Errors:: How to find syntax errors.
* Compilation Errors:: How to find errors that show up in byte compilation.
-* Edebug:: A source-level Emacs Lisp debugger.
@end menu
Another useful debugging tool is the dribble file. When a dribble
@cindex Lisp debugger
@cindex break
- The @dfn{Lisp debugger} provides the ability to suspend evaluation of
-a form. While evaluation is suspended (a state that is commonly known
-as a @dfn{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. @xref{Recursive Editing}.
+ The ordinary @dfn{Lisp debugger} provides the ability to suspend
+evaluation of a form. While evaluation is suspended (a state that is
+commonly known as a @dfn{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. @xref{Recursive Editing}.
@menu
* Error Debugging:: Entering the debugger when an error happens.
error.
However, entry to the debugger is not a normal consequence of an
-error. Many commands frequently get Lisp errors when invoked in
-inappropriate contexts (such as @kbd{C-f} at the end of the buffer) and
-during ordinary editing it would be very unpleasant to enter the
-debugger each time this happens. If you want errors to enter the
-debugger, set the variable @code{debug-on-error} to non-@code{nil}.
+error. Many commands frequently cause Lisp errors when invoked
+inappropriately (such as @kbd{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 @code{debug-on-error} to non-@code{nil}. (The command
+@code{toggle-debug-on-error} provides an easy way to do this.)
@defopt debug-on-error
This variable determines whether the debugger is called when an error is
signaled and not handled. If @code{debug-on-error} is @code{t}, all
-errors call the debugger. If it is @code{nil}, none call the debugger.
+kinds of errors call the debugger (except those listed in
+@code{debug-ignored-errors}). If it is @code{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
@code{(void-variable)}, then only errors about a variable that has no
value invoke the debugger.
-When this variable is non-@code{nil}, Emacs does not catch errors that
-happen in process filter functions and sentinels. Therefore, these
-errors also can invoke the debugger. @xref{Processes}.
+When this variable is non-@code{nil}, Emacs does not create an error
+handler around process filter functions and sentinels. Therefore,
+errors in these functions also invoke the debugger. @xref{Processes}.
@end defopt
@defopt debug-ignored-errors
words, @code{condition-case} gets a chance to handle the error before
the debugger gets a chance.
-If you set @code{debug-on-signal} non-@code{nil}, then the debugger gets
-first chance at every error; an error will invoke the debugger
-regardless of any @code{condition-case}, if the fits the criterion
-specified by the values of @code{debug-on-error} and
+If you set @code{debug-on-signal} to a non-@code{nil} value, then the
+debugger gets the first chance at every error; an error will invoke the
+debugger regardless of any @code{condition-case}, if it fits the
+criteria specified by the values of @code{debug-on-error} and
@code{debug-ignored-errors}.
@strong{Warning:} This variable is strong medicine! Various parts of
@code{debug-on-error} is @code{nil}.
@end defopt
- To debug an error that happens during loading of the @file{.emacs}
-file, use the option @samp{-debug-init}, which binds
-@code{debug-on-error} to @code{t} while @file{.emacs} is loaded and
-inhibits use of @code{condition-case} to catch init-file errors.
+ To debug an error that happens during loading of the init
+file, use the option @samp{--debug-init}. This binds
+@code{debug-on-error} to @code{t} while loading the init file, and
+bypasses the @code{condition-case} which normally catches errors in the
+init file.
- If your @file{.emacs} file sets @code{debug-on-error}, the effect may
-not last past the end of loading @file{.emacs}. (This is an undesirable
-byproduct of the code that implements the @samp{-debug-init} command
-line option.) The best way to make @file{.emacs} set
+ If your init file sets @code{debug-on-error}, the effect may
+not last past the end of loading the init file. (This is an undesirable
+byproduct of the code that implements the @samp{--debug-init} command
+line option.) The best way to make the init file set
@code{debug-on-error} permanently is with @code{after-init-hook}, like
this:
@example
(add-hook 'after-init-hook
- '(lambda () (setq debug-on-error t)))
+ (lambda () (setq debug-on-error t)))
@end example
@node Infinite Loops
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 @kbd{C-g}, which causes quit.
+with @kbd{C-g}, which causes a @dfn{quit}.
Ordinary quitting gives no information about why the program was
looping. To get more information, you can set the variable
You can cause the debugger to be called at a certain point in your
program by writing the expression @code{(debug)} at that point. To do
this, visit the source file, insert the text @samp{(debug)} at the
-proper place, and type @kbd{C-M-x}. Be sure to undo this insertion
-before you save the file!
+proper place, and type @kbd{C-M-x}. @strong{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 @samp{(debug)} must be a place where an
additional form can be evaluated and its value ignored. (If the value
considered the @dfn{current frame}. Some of the debugger commands
operate on the current frame.
+ If a function name is underlined, that means the debugger knows
+where its source code is located. You can click @kbd{Mouse-2} on that
+name, or move to it and type @key{RET}, to visit the source code.
+
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
@subsection Debugger Commands
@cindex debugger command list
- 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
-@kbd{C-M-x} on its definition.)
+ The debugger buffer (in Debugger mode) provides special commands in
+addition to the usual Emacs commands. 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 @kbd{C-M-x} on its definition.)
Here is a list of Debugger mode commands:
@item c
Exit the debugger and continue execution. When continuing is possible,
it resumes execution of the program as if the debugger had never been
-entered (aside from the effect of any variables or data structures you
-may have changed while inside the debugger).
+entered (aside from any side-effects that you caused by changing
+variable values or data structures while inside the debugger).
Continuing is possible after entry to the debugger due to function entry
or exit, explicit invocation, or quitting. You cannot continue if the
Read a Lisp expression in the minibuffer, evaluate it, and print the
value in the echo area. The debugger alters certain important
variables, and the current buffer, as part of its operation; @kbd{e}
-temporarily restores their outside-the-debugger values so you can
-examine them. This makes the debugger more transparent. By contrast,
-@kbd{M-:} does nothing special in the debugger; it shows you the
-variable values within the debugger.
+temporarily restores their values from outside the debugger, so you can
+examine and change them. This makes the debugger more transparent. By
+contrast, @kbd{M-:} does nothing special in the debugger; it shows you
+the variable values within the debugger.
@item R
Like @kbd{e}, but also save the result of evaluation in the
@node Invoking the Debugger
@subsection Invoking the Debugger
- Here we describe fully the function used to invoke the debugger.
+ Here we describe in full detail the function @code{debug} that is used
+to invoke the debugger.
@defun debug &rest debugger-args
This function enters the debugger. It switches buffers to a buffer
whatever called @code{debug}. This is the only way the function
@code{debug} can return to its caller.
-If the first of the @var{debugger-args} passed to @code{debug} is
-@code{nil} (or if it is not one of the special values in the table
-below), then @code{debug} displays the rest of its arguments at the
-top of the @samp{*Backtrace*} buffer. This mechanism is used to display
-a message to the user.
-
-However, if the first argument passed to @code{debug} is one of the
-following special values, then it has special significance. Normally,
-these values are passed to @code{debug} only by the internals of Emacs
-and the debugger, and not by programmers calling @code{debug}.
+The use of the @var{debugger-args} is that @code{debug} displays the
+rest of its arguments at the top of the @samp{*Backtrace*} buffer, so
+that the user can see them. Except as described below, this is the
+@emph{only} way these arguments are used.
-The special values are:
+However, certain values for first argument to @code{debug} have a
+special significance. (Normally, these values are used only by the
+internals of Emacs, and not by programmers calling @code{debug}.) Here
+is a table of these special values:
@table @code
@item lambda
@defvar debugger
The value of this variable is the function to call to invoke the
-debugger. Its value must be a function of any number of arguments (or,
-more typically, the name of a function). Presumably this function will
-enter some kind of debugger. The default value of the variable is
+debugger. Its value must be a function of any number of arguments, or,
+more typically, the name of a function. This function should invoke
+some kind of debugger. The default value of the variable is
@code{debug}.
The first argument that Lisp hands to the function indicates why it
In the following example, a Lisp expression calls @code{backtrace}
explicitly. This prints the backtrace to the stream
-@code{standard-output}: in this case, to the buffer
-@samp{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.
+@code{standard-output}, which, in this case, is the buffer
+@samp{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.
@smallexample
@group
----------- Buffer: backtrace-output ------------
backtrace()
(list ...computing arguments...)
+@end group
(progn ...)
eval((progn (1+ var) (list (quote testing) (backtrace))))
(setq ...)
(with-output-to-temp-buffer ...)
eval-region(1973 2142 #<buffer *scratch*>)
byte-code("... for eval-print-last-sexp ...")
+@group
eval-print-last-sexp(nil)
* call-interactively(eval-print-last-sexp)
----------- Buffer: backtrace-output ------------
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.
+The advantage of using this variable rather than an ordinary global
+variable is that the data will never carry over to a subsequent command
+invocation.
@end defvar
@defun backtrace-frame frame-number
debuggers. It returns information about what computation is happening
in the stack frame @var{frame-number} levels down.
-If that frame has not evaluated the arguments yet (or is a special
-form), the value is @code{(nil @var{function} @var{arg-forms}@dots{})}.
+If that frame has not evaluated the arguments yet, or is a special
+form, the value is @code{(nil @var{function} @var{arg-forms}@dots{})}.
If that frame has evaluated its arguments and called its function
-already, the value is @code{(t @var{function}
+already, the return value is @code{(t @var{function}
@var{arg-values}@dots{})}.
In the return value, @var{function} is whatever was supplied as the
@code{nil}.
@end defun
+@include edebug.texi
+
@node Syntax Errors
@section Debugging Invalid Lisp Syntax
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.
+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.)
@menu
* Excess Open:: How to find a spurious open paren or missing close.
@subsection Excess Open Parentheses
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 @kbd{C-M-b}
-(@code{backward-sexp}). This will move you to the beginning of the
-defun that is unbalanced. (Then type @kbd{C-@key{SPC} C-_ C-u
-C-@key{SPC}} to set the mark there, undo the insertion of the
-close parenthesis, and finally return to the mark.)
+an excess open parenthesis, the way to do this is to go to the end of
+the file and type @kbd{C-u C-M-u}. This will move you to the beginning
+of the defun that is unbalanced.
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
@node Excess Close
@subsection Excess Close Parentheses
- To deal with an excess close parenthesis, first insert an open
-parenthesis at the beginning of the file, back up over it, and type
-@kbd{C-M-f} to find the end of the unbalanced defun. (Then type
-@kbd{C-@key{SPC} C-_ C-u C-@key{SPC}} to set the mark there, undo the
-insertion of the open parenthesis, and finally return to the mark.)
+ To deal with an excess close parenthesis, first go to the beginning of
+the file, then type @kbd{C-u -1 C-M-u} to find the end of the unbalanced
+defun.
Then find the actual matching close parenthesis by typing @kbd{C-M-f}
at the beginning of that defun. This will leave you somewhere short of
old indentation is probably appropriate to the intended parentheses.
After you think you have fixed the problem, use @kbd{C-M-q} again. If
-the old indentation actually fit the intended nesting of parentheses,
+the old indentation actually fits the intended nesting of parentheses,
and you have put back those parentheses, @kbd{C-M-q} should not change
anything.
-@node Compilation Errors, Edebug, Syntax Errors, Debugging
+@node Compilation Errors
@section Debugging Problems in Compilation
When an error happens during byte compilation, it is normally due to
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.
-
-@include edebug.texi