2007-09-06 04:25:08 +00:00
|
|
|
@c -*-texinfo-*-
|
|
|
|
@c This is part of the GNU Emacs Lisp Reference Manual.
|
2025-01-01 07:39:17 +00:00
|
|
|
@c Copyright (C) 1990--1994, 1998--1999, 2001--2025 Free Software
|
2013-01-01 09:11:05 +00:00
|
|
|
@c Foundation, Inc.
|
2007-09-06 04:25:08 +00:00
|
|
|
@c See the file elisp.texi for copying conditions.
|
2012-05-26 18:34:14 -07:00
|
|
|
@node Debugging
|
2007-09-06 04:25:08 +00:00
|
|
|
@chapter Debugging Lisp Programs
|
Improve indexing on the chapter/section/subsection levels.
doc/lispref/windows.texi (Recombining Windows): Index subject of sections.
doc/lispref/variables.texi (Variables with Restricted Values)
(Generalized Variables): Index subject of sections.
doc/lispref/text.texi (Buffer Contents, Examining Properties)
(Changing Properties, Property Search, Substitution): Index
subject of sections.
doc/lispref/syntax.texi (Motion and Syntax, Parsing Expressions)
(Motion via Parsing, Position Parse, Control Parsing): Index
subject of sections.
doc/lispref/strings.texi (Predicates for Strings, Creating Strings)
(Modifying Strings, Text Comparison): Index subject of sections.
doc/lispref/searching.texi (Syntax of Regexps, Regexp Special)
(Regexp Functions, Regexp Functions): Index subject of sections.
doc/lispref/processes.texi (Subprocess Creation, Process Information): Index
subject of sections.
doc/lispref/positions.texi (Screen Lines): Index subject of sections.
doc/lispref/nonascii.texi (Scanning Charsets, Specifying Coding Systems):
Index subject of sections.
doc/lispref/minibuf.texi (Text from Minibuffer, Object from Minibuffer)
(Multiple Queries, Minibuffer Contents): Index subject of
sections.
doc/lispref/markers.texi (Predicates on Markers, Creating Markers)
(Information from Markers, Moving Markers): Index subject of
sections.
doc/lispref/macros.texi (Defining Macros, Problems with Macros): Index
subject of sections.
doc/lispref/loading.texi (Loading Non-ASCII, Where Defined): Index subject
of sections.
doc/lispref/lists.texi (List-related Predicates, List Variables, Setcar)
(Setcdr, Plist Access): Index subject of sections.
doc/lispref/keymaps.texi (Controlling Active Maps, Scanning Keymaps)
(Modifying Menus): Index subject of sections.
doc/lispref/help.texi (Accessing Documentation, Help Functions): Index
subject of sections.
doc/lispref/hash.texi (Hash Access): Index subject of sections.
doc/lispref/functions.texi (Core Advising Primitives)
(Advising Named Functions, Porting old advices): Index subject of
sections.
doc/lispref/frames.texi (Creating Frames, Initial Parameters)
(Position Parameters, Buffer Parameters, Minibuffers and Frames)
(Pop-Up Menus, Drag and Drop): Index subject of sections.
doc/lispref/files.texi (Visiting Functions, Kinds of Files)
(Unique File Names): Index subject of sections.
doc/lispref/display.texi (Refresh Screen, Echo Area Customization)
(Warning Variables, Warning Options, Delayed Warnings)
(Temporary Displays, Managing Overlays, Overlay Properties)
(Finding Overlays, Size of Displayed Text, Defining Faces)
(Attribute Functions, Displaying Faces, Face Remapping)
(Basic Faces, Font Lookup, Fontsets, Replacing Specs)
(Defining Images, Showing Images): Index subject of sections.
doc/lispref/debugging.texi (Debugging, Explicit Debug)
(Invoking the Debugger, Excess Open, Excess Close): Index subject
of sections.
doc/lispref/customize.texi (Defining New Types, Applying Customizations)
(Custom Themes): Index subject of sections.
doc/lispref/control.texi (Sequencing, Combining Conditions)
(Processing of Errors, Cleanups): Index subject of sections.
doc/lispref/compile.texi (Eval During Compile): Index subject of sections.
doc/lispref/commands.texi (Using Interactive, Distinguish Interactive)
(Command Loop Info, Classifying Events, Event Mod)
(Invoking the Input Method): Index subject of sections.
doc/lispref/buffers.texi (Buffer List, Buffer Gap): Index subject of sections.
doc/lispref/backups.texi (Making Backups, Numbered Backups, Backup Names)
(Reverting): Index subject of sections.
doc/lispref/abbrevs.texi (Abbrev Tables, Defining Abbrevs, Abbrev Files)
(Abbrev Expansion, Standard Abbrev Tables, Abbrev Properties)
(Abbrev Table Properties): Index subject of sections.
doc/lispref/os.texi (Time of Day, Time Conversion, Time Parsing)
(Time Calculations, Idle Timers): Index subject of sections.
2014-12-23 20:42:30 +02:00
|
|
|
@cindex debugging lisp programs
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2012-02-11 14:17:22 +08:00
|
|
|
There are several ways to find and investigate problems in an Emacs
|
|
|
|
Lisp program.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@itemize @bullet
|
|
|
|
@item
|
2012-02-11 14:17:22 +08:00
|
|
|
If a problem occurs when you run the program, you can use the built-in
|
2023-11-17 14:28:13 +02:00
|
|
|
Emacs Lisp debugger (@pxref{Debugger}) to suspend the Lisp evaluator,
|
|
|
|
and examine and/or alter its internal state.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item
|
2012-02-11 14:17:22 +08:00
|
|
|
You can use Edebug, a source-level debugger for Emacs Lisp.
|
2023-11-17 14:28:13 +02:00
|
|
|
@xref{Edebug}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2018-01-13 13:53:47 +02:00
|
|
|
@item
|
|
|
|
@cindex tracing Lisp programs
|
|
|
|
You can trace the execution of functions involved in the problem using
|
|
|
|
the tracing facilities provided by the @file{trace.el} package. This
|
|
|
|
package provides the functions @code{trace-function-foreground} and
|
|
|
|
@code{trace-function-background} for tracing function calls, and
|
|
|
|
@code{trace-values} for adding values of select variables to the
|
|
|
|
trace. For the details, see the documentation of these facilities in
|
|
|
|
@file{trace.el}.
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@item
|
2012-02-11 14:17:22 +08:00
|
|
|
If a syntactic problem is preventing Lisp from even reading the
|
|
|
|
program, you can locate it using Lisp editing commands.
|
|
|
|
|
|
|
|
@item
|
|
|
|
You can look at the error and warning messages produced by the byte
|
|
|
|
compiler when it compiles the program. @xref{Compiler Errors}.
|
|
|
|
|
|
|
|
@item
|
|
|
|
You can use the Testcover package to perform coverage testing on the
|
|
|
|
program.
|
|
|
|
|
|
|
|
@item
|
|
|
|
You can use the ERT package to write regression tests for the program.
|
2013-07-02 20:03:47 -07:00
|
|
|
@xref{Top,the ERT manual,, ert, ERT: Emacs Lisp Regression Testing}.
|
2012-11-20 20:52:03 -05:00
|
|
|
|
|
|
|
@item
|
|
|
|
You can profile the program to get hints about how to make it more efficient.
|
2023-11-17 11:47:46 +02:00
|
|
|
@xref{Profiling}.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end itemize
|
|
|
|
|
2012-02-11 14:17:22 +08:00
|
|
|
Other useful tools for debugging input and output problems are the
|
|
|
|
dribble file (@pxref{Terminal Input}) and the @code{open-termscript}
|
|
|
|
function (@pxref{Terminal Output}).
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@menu
|
2012-02-11 14:17:22 +08:00
|
|
|
* Debugger:: A debugger for the Emacs Lisp evaluator.
|
Untabify doc/lispref/*.texi.
* abbrevs.texi, commands.texi, compile.texi, debugging.texi:
* display.texi, edebug.texi, elisp.texi, eval.texi, files.texi:
* frames.texi, functions.texi, internals.texi, keymaps.texi:
* loading.texi, minibuf.texi, numbers.texi, os.texi, processes.texi:
* searching.texi, sequences.texi, strings.texi, syntax.texi:
* text.texi, tips.texi, vol1.texi, vol2.texi, windows.texi:
Untabify Texinfo files.
2010-06-22 20:36:56 -07:00
|
|
|
* Edebug:: A source-level Emacs Lisp debugger.
|
2007-09-06 04:25:08 +00:00
|
|
|
* Syntax Errors:: How to find syntax errors.
|
|
|
|
* Test Coverage:: Ensuring you have tested all branches in your code.
|
2012-11-20 00:02:54 -08:00
|
|
|
* Profiling:: Measuring the resources that your code uses.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end menu
|
|
|
|
|
|
|
|
@node Debugger
|
|
|
|
@section The Lisp Debugger
|
|
|
|
@cindex debugger for Emacs Lisp
|
|
|
|
@cindex Lisp debugger
|
|
|
|
@cindex break
|
|
|
|
|
|
|
|
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.
|
2022-08-11 19:31:09 +00:00
|
|
|
* Debugging Redisplay:: Getting backtraces from redisplay errors.
|
Untabify doc/lispref/*.texi.
* abbrevs.texi, commands.texi, compile.texi, debugging.texi:
* display.texi, edebug.texi, elisp.texi, eval.texi, files.texi:
* frames.texi, functions.texi, internals.texi, keymaps.texi:
* loading.texi, minibuf.texi, numbers.texi, os.texi, processes.texi:
* searching.texi, sequences.texi, strings.texi, syntax.texi:
* text.texi, tips.texi, vol1.texi, vol2.texi, windows.texi:
Untabify Texinfo files.
2010-06-22 20:36:56 -07:00
|
|
|
* Infinite Loops:: Stopping and debugging a program that doesn't exit.
|
2007-09-06 04:25:08 +00:00
|
|
|
* Function Debugging:: Entering it when a certain function is called.
|
2015-12-13 14:47:58 -05:00
|
|
|
* Variable Debugging:: Entering it when a variable is modified.
|
2007-09-06 04:25:08 +00:00
|
|
|
* Explicit Debug:: Entering it at a certain point in the program.
|
2018-06-19 07:27:41 -07:00
|
|
|
* Using Debugger:: What the debugger does.
|
|
|
|
* Backtraces:: What you see while in the debugger.
|
2007-09-06 04:25:08 +00:00
|
|
|
* Debugger Commands:: Commands used while in the debugger.
|
|
|
|
* Invoking the Debugger:: How to call the function @code{debug}.
|
|
|
|
* Internals of Debugger:: Subroutines of the debugger, and global variables.
|
|
|
|
@end menu
|
|
|
|
|
|
|
|
@node Error Debugging
|
|
|
|
@subsection Entering the Debugger on an Error
|
|
|
|
@cindex error debugging
|
|
|
|
@cindex debugging errors
|
|
|
|
|
|
|
|
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
|
2012-02-11 14:17:22 +08:00
|
|
|
error. Many commands signal Lisp errors when invoked inappropriately,
|
|
|
|
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.)
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2022-08-11 19:31:09 +00:00
|
|
|
Note that, for technical reasons, you cannot use the facilities
|
|
|
|
defined in this subsection to debug errors in Lisp that the redisplay
|
|
|
|
code has invoked. @xref{Debugging Redisplay}, for help with these.
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@defopt debug-on-error
|
2009-03-14 21:21:41 +00:00
|
|
|
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 kinds of errors call the debugger, except those listed in
|
|
|
|
@code{debug-ignored-errors} (see below). If it is @code{nil}, none
|
2012-02-11 14:17:22 +08:00
|
|
|
call the debugger.
|
|
|
|
|
|
|
|
The value can also be a list of error conditions (@pxref{Signaling
|
|
|
|
Errors}). Then the debugger is called only for error conditions in
|
|
|
|
this list (except those also listed in @code{debug-ignored-errors}).
|
|
|
|
For example, if you set @code{debug-on-error} to the list
|
|
|
|
@code{(void-variable)}, the debugger is only called for errors about a
|
|
|
|
variable that has no value.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2012-02-11 14:17:22 +08:00
|
|
|
Note that @code{eval-expression-debug-on-error} overrides this
|
|
|
|
variable in some cases; see below.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
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
|
2012-02-11 14:17:22 +08:00
|
|
|
This variable specifies errors which should not enter the debugger,
|
|
|
|
regardless of the value of @code{debug-on-error}. Its value is a list
|
|
|
|
of error condition symbols and/or regular expressions. If the error
|
|
|
|
has any of those condition symbols, or if the error message matches
|
|
|
|
any of the regular expressions, then that error does not enter the
|
|
|
|
debugger.
|
|
|
|
|
2012-11-10 16:37:40 -08:00
|
|
|
The normal value of this variable includes @code{user-error}, as well
|
|
|
|
as 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 may try
|
|
|
|
changing this list to debug the error. The easiest way is usually to
|
|
|
|
set @code{debug-ignored-errors} to @code{nil}.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defopt
|
|
|
|
|
|
|
|
@defopt eval-expression-debug-on-error
|
2012-02-11 14:17:22 +08:00
|
|
|
If this variable has a non-@code{nil} value (the default), running the
|
|
|
|
command @code{eval-expression} causes @code{debug-on-error} to be
|
2017-10-09 22:53:19 -07:00
|
|
|
temporarily bound to @code{t}. @xref{Lisp Eval,, Evaluating
|
2020-09-01 23:59:11 +02:00
|
|
|
Emacs Lisp Expressions, emacs, The GNU Emacs Manual}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2012-02-11 14:17:22 +08:00
|
|
|
If @code{eval-expression-debug-on-error} is @code{nil}, then the value
|
|
|
|
of @code{debug-on-error} is not changed during @code{eval-expression}.
|
2012-02-04 13:58:00 -08:00
|
|
|
@end defopt
|
|
|
|
|
2016-02-01 19:01:34 +01:00
|
|
|
@defopt debug-on-signal
|
2012-02-11 14:17:22 +08:00
|
|
|
Normally, errors caught by @code{condition-case} never invoke the
|
|
|
|
debugger. The @code{condition-case} gets a chance to handle the error
|
|
|
|
before the debugger gets a chance.
|
|
|
|
|
|
|
|
If you change @code{debug-on-signal} to a non-@code{nil} value, the
|
|
|
|
debugger gets the first chance at every error, regardless of the
|
|
|
|
presence of @code{condition-case}. (To invoke the debugger, the error
|
|
|
|
must still fulfill the criteria specified by @code{debug-on-error} and
|
|
|
|
@code{debug-ignored-errors}.)
|
|
|
|
|
2016-10-22 11:52:14 -04:00
|
|
|
@cindex emacsclient, getting a backtrace
|
|
|
|
@cindex backtrace from emacsclient's @option{--eval}
|
|
|
|
For example, setting this variable is useful to get a backtrace from
|
|
|
|
code evaluated by emacsclient's @option{--eval} option. If Lisp code
|
|
|
|
evaluated by emacsclient signals an error while this variable is
|
|
|
|
non-@code{nil}, the backtrace will popup in the running Emacs.
|
|
|
|
|
2012-02-11 14:17:22 +08:00
|
|
|
@strong{Warning:} Setting this variable to non-@code{nil} may have
|
|
|
|
annoying effects. Various parts of Emacs catch errors in the normal
|
|
|
|
course of affairs, and you may not even realize that errors happen
|
|
|
|
there. If you need to debug code wrapped in @code{condition-case},
|
|
|
|
consider using @code{condition-case-unless-debug} (@pxref{Handling
|
|
|
|
Errors}).
|
2016-02-01 19:01:34 +01:00
|
|
|
@end defopt
|
2012-02-11 14:17:22 +08:00
|
|
|
|
2012-02-04 13:58:00 -08:00
|
|
|
@defopt debug-on-event
|
|
|
|
If you set @code{debug-on-event} to a special event (@pxref{Special
|
|
|
|
Events}), Emacs will try to enter the debugger as soon as it receives
|
|
|
|
this event, bypassing @code{special-event-map}. At present, the only
|
|
|
|
supported values correspond to the signals @code{SIGUSR1} and
|
2012-02-04 19:37:19 -08:00
|
|
|
@code{SIGUSR2} (this is the default). This can be helpful when
|
|
|
|
@code{inhibit-quit} is set and Emacs is not otherwise responding.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defopt
|
|
|
|
|
2012-11-10 16:58:51 -08:00
|
|
|
@cindex message, finding what causes a particular message
|
|
|
|
@defvar debug-on-message
|
|
|
|
If you set @code{debug-on-message} to a regular expression,
|
|
|
|
Emacs will enter the debugger if it displays a matching message in the
|
|
|
|
echo area. For example, this can be useful when trying to find the
|
|
|
|
cause of a particular message.
|
2022-05-01 13:40:13 +02:00
|
|
|
@end defvar
|
|
|
|
|
|
|
|
@defvar debug-allow-recursive-debug
|
|
|
|
You can evaluate forms in the current stack frame in the
|
|
|
|
@samp{*Backtrace*} buffer with the @key{e} command, and while
|
|
|
|
edebugging you can use the @key{e} and @key{C-x C-e} commands to do
|
|
|
|
something similar. By default, the debugger is inhibited by these
|
|
|
|
commands (because (re-)entering the debugger at this point will
|
|
|
|
usually take you out of the debugging context you're in). Set
|
|
|
|
@code{debug-allow-recursive-debug} to a non-@code{nil} value to allow
|
|
|
|
these commands to enter the debugger recursively.
|
2012-11-10 16:58:51 -08:00
|
|
|
@end defvar
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
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.
|
|
|
|
|
2022-08-11 19:31:09 +00:00
|
|
|
@node Debugging Redisplay
|
|
|
|
@subsection Debugging Redisplay Errors
|
|
|
|
@cindex redisplay errors
|
|
|
|
@cindex debugging redisplay errors
|
|
|
|
|
|
|
|
When an error occurs in Lisp code which redisplay has invoked, Emacs's
|
|
|
|
usual debugging mechanisms are unusable, for technical reasons. This
|
|
|
|
subsection describes how to get a backtrace from such an error, which
|
|
|
|
should be helpful in debugging it.
|
|
|
|
|
|
|
|
These directions apply to Lisp forms used, for example, in
|
|
|
|
@code{:eval} mode line constructs (@pxref{Mode Line Data}), and in all
|
|
|
|
hooks invoked from redisplay, such as:
|
|
|
|
|
|
|
|
@itemize
|
|
|
|
@item
|
|
|
|
@code{fontification-functions} (@pxref{Auto Faces}).
|
|
|
|
@item
|
|
|
|
@code{window-scroll-functions} (@pxref{Window Hooks}).
|
|
|
|
@end itemize
|
|
|
|
|
|
|
|
Note that if you have had an error in a hook function called from
|
|
|
|
redisplay, the error handling might have removed this function from
|
|
|
|
the hook. You will thus need to reinitialize that hook somehow,
|
|
|
|
perhaps with @code{add-hook}, to be able to replay the bug.
|
|
|
|
|
|
|
|
To generate a backtrace in these circumstances, set the variable
|
|
|
|
@code{backtrace-on-redisplay-error} to non-@code{nil}. When the error
|
|
|
|
occurs, Emacs will dump the backtrace to the buffer
|
|
|
|
@file{*Redisplay-trace*}, but won't automatically display it in a
|
|
|
|
window. This is to avoid needlessly corrupting the redisplay you are
|
|
|
|
debugging. You will thus need to display the buffer yourself, with a
|
|
|
|
command such as @code{switch-to-buffer-other-frame} @key{C-x 5 b}.
|
|
|
|
|
|
|
|
@defvar backtrace-on-redisplay-error
|
|
|
|
Set this variable to non-@code{nil} to enable the generation of a
|
|
|
|
backtrace when an error occurs in any Lisp called from redisplay.
|
|
|
|
@end defvar
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@node Infinite Loops
|
|
|
|
@subsection Debugging Infinite Loops
|
|
|
|
@cindex infinite loops
|
|
|
|
@cindex loops, infinite
|
|
|
|
@cindex quitting from infinite loop
|
|
|
|
@cindex stopping an infinite loop
|
|
|
|
|
|
|
|
When a program loops infinitely and fails to return, your first
|
2012-02-11 14:17:22 +08:00
|
|
|
problem is to stop the loop. On most operating systems, you can do
|
|
|
|
this with @kbd{C-g}, which causes a @dfn{quit}. @xref{Quitting}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
Ordinary quitting gives no information about why the program was
|
|
|
|
looping. To get more information, you can set the variable
|
2012-02-11 14:17:22 +08:00
|
|
|
@code{debug-on-quit} to non-@code{nil}. 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 may get enough information to solve the problem.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2012-02-11 14:17:22 +08:00
|
|
|
Quitting with @kbd{C-g} is not considered an error, and
|
|
|
|
@code{debug-on-error} has no effect on the handling of @kbd{C-g}.
|
|
|
|
Likewise, @code{debug-on-quit} has no effect on errors.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@defopt debug-on-quit
|
2012-02-11 14:17:22 +08:00
|
|
|
This variable determines whether the debugger is called when
|
|
|
|
@code{quit} is signaled and not handled. If @code{debug-on-quit} is
|
|
|
|
non-@code{nil}, then the debugger is called whenever you quit (that
|
|
|
|
is, type @kbd{C-g}). If @code{debug-on-quit} is @code{nil} (the
|
|
|
|
default), then the debugger is not called when you quit.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defopt
|
|
|
|
|
|
|
|
@node Function Debugging
|
|
|
|
@subsection Entering the Debugger on a Function Call
|
|
|
|
@cindex function call debugging
|
|
|
|
@cindex debugging specific functions
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
@deffn Command debug-on-entry function-name
|
|
|
|
This function requests @var{function-name} to invoke the debugger each
|
2014-07-08 14:24:39 -04:00
|
|
|
time it is called.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
Any function or macro 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 also set debug-on-entry for primitive functions
|
|
|
|
(i.e., those written in C) this way, but it only takes effect when the
|
|
|
|
primitive is called from Lisp code. Debug-on-entry is not allowed for
|
|
|
|
special forms.
|
|
|
|
|
|
|
|
When @code{debug-on-entry} is called interactively, it prompts for
|
|
|
|
@var{function-name} in the minibuffer. If the function is already set
|
|
|
|
up to invoke the debugger on entry, @code{debug-on-entry} does nothing.
|
|
|
|
@code{debug-on-entry} always returns @var{function-name}.
|
|
|
|
|
|
|
|
Here's an example to illustrate use of this function:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(defun fact (n)
|
|
|
|
(if (zerop n) 1
|
|
|
|
(* n (fact (1- n)))))
|
|
|
|
@result{} fact
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
(debug-on-entry 'fact)
|
|
|
|
@result{} fact
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
(fact 3)
|
|
|
|
@end group
|
|
|
|
|
|
|
|
@group
|
|
|
|
------ Buffer: *Backtrace* ------
|
|
|
|
Debugger entered--entering a function:
|
|
|
|
* fact(3)
|
|
|
|
eval((fact 3))
|
|
|
|
eval-last-sexp-1(nil)
|
|
|
|
eval-last-sexp(nil)
|
|
|
|
call-interactively(eval-last-sexp)
|
|
|
|
------ Buffer: *Backtrace* ------
|
|
|
|
@end group
|
|
|
|
|
|
|
|
@end example
|
|
|
|
@end deffn
|
|
|
|
|
|
|
|
@deffn Command cancel-debug-on-entry &optional function-name
|
|
|
|
This function undoes the effect of @code{debug-on-entry} on
|
|
|
|
@var{function-name}. When called interactively, it prompts for
|
|
|
|
@var{function-name} in the minibuffer. If @var{function-name} is
|
|
|
|
omitted or @code{nil}, it cancels break-on-entry for all functions.
|
|
|
|
Calling @code{cancel-debug-on-entry} does nothing to a function which is
|
|
|
|
not currently set up to break on entry.
|
|
|
|
@end deffn
|
|
|
|
|
2015-12-13 14:47:58 -05:00
|
|
|
@node Variable Debugging
|
|
|
|
@subsection Entering the debugger when a variable is modified
|
|
|
|
@cindex variable write debugging
|
|
|
|
@cindex debugging changes to variables
|
|
|
|
|
|
|
|
Sometimes a problem with a function is due to a wrong setting of a
|
|
|
|
variable. Setting up the debugger to trigger whenever the variable is
|
|
|
|
changed is a quick way to find the origin of the setting.
|
|
|
|
|
|
|
|
@deffn Command debug-on-variable-change variable
|
|
|
|
This function arranges for the debugger to be called whenever
|
|
|
|
@var{variable} is modified.
|
|
|
|
|
|
|
|
It is implemented using the watchpoint mechanism, so it inherits the
|
|
|
|
same characteristics and limitations: all aliases of @var{variable}
|
|
|
|
will be watched together, only dynamic variables can be watched, and
|
|
|
|
changes to the objects referenced by variables are not detected. For
|
|
|
|
details, see @ref{Watching Variables}.
|
|
|
|
@end deffn
|
|
|
|
|
|
|
|
@deffn Command cancel-debug-on-variable-change &optional variable
|
|
|
|
This function undoes the effect of @code{debug-on-variable-change} on
|
|
|
|
@var{variable}. When called interactively, it prompts for
|
|
|
|
@var{variable} in the minibuffer. If @var{variable} is omitted or
|
|
|
|
@code{nil}, it cancels break-on-change for all variables. Calling
|
|
|
|
@code{cancel-debug-on-variable-change} does nothing to a variable
|
|
|
|
which is not currently set up to break on change.
|
|
|
|
@end deffn
|
|
|
|
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@node Explicit Debug
|
|
|
|
@subsection Explicit Entry to the Debugger
|
Improve indexing on the chapter/section/subsection levels.
doc/lispref/windows.texi (Recombining Windows): Index subject of sections.
doc/lispref/variables.texi (Variables with Restricted Values)
(Generalized Variables): Index subject of sections.
doc/lispref/text.texi (Buffer Contents, Examining Properties)
(Changing Properties, Property Search, Substitution): Index
subject of sections.
doc/lispref/syntax.texi (Motion and Syntax, Parsing Expressions)
(Motion via Parsing, Position Parse, Control Parsing): Index
subject of sections.
doc/lispref/strings.texi (Predicates for Strings, Creating Strings)
(Modifying Strings, Text Comparison): Index subject of sections.
doc/lispref/searching.texi (Syntax of Regexps, Regexp Special)
(Regexp Functions, Regexp Functions): Index subject of sections.
doc/lispref/processes.texi (Subprocess Creation, Process Information): Index
subject of sections.
doc/lispref/positions.texi (Screen Lines): Index subject of sections.
doc/lispref/nonascii.texi (Scanning Charsets, Specifying Coding Systems):
Index subject of sections.
doc/lispref/minibuf.texi (Text from Minibuffer, Object from Minibuffer)
(Multiple Queries, Minibuffer Contents): Index subject of
sections.
doc/lispref/markers.texi (Predicates on Markers, Creating Markers)
(Information from Markers, Moving Markers): Index subject of
sections.
doc/lispref/macros.texi (Defining Macros, Problems with Macros): Index
subject of sections.
doc/lispref/loading.texi (Loading Non-ASCII, Where Defined): Index subject
of sections.
doc/lispref/lists.texi (List-related Predicates, List Variables, Setcar)
(Setcdr, Plist Access): Index subject of sections.
doc/lispref/keymaps.texi (Controlling Active Maps, Scanning Keymaps)
(Modifying Menus): Index subject of sections.
doc/lispref/help.texi (Accessing Documentation, Help Functions): Index
subject of sections.
doc/lispref/hash.texi (Hash Access): Index subject of sections.
doc/lispref/functions.texi (Core Advising Primitives)
(Advising Named Functions, Porting old advices): Index subject of
sections.
doc/lispref/frames.texi (Creating Frames, Initial Parameters)
(Position Parameters, Buffer Parameters, Minibuffers and Frames)
(Pop-Up Menus, Drag and Drop): Index subject of sections.
doc/lispref/files.texi (Visiting Functions, Kinds of Files)
(Unique File Names): Index subject of sections.
doc/lispref/display.texi (Refresh Screen, Echo Area Customization)
(Warning Variables, Warning Options, Delayed Warnings)
(Temporary Displays, Managing Overlays, Overlay Properties)
(Finding Overlays, Size of Displayed Text, Defining Faces)
(Attribute Functions, Displaying Faces, Face Remapping)
(Basic Faces, Font Lookup, Fontsets, Replacing Specs)
(Defining Images, Showing Images): Index subject of sections.
doc/lispref/debugging.texi (Debugging, Explicit Debug)
(Invoking the Debugger, Excess Open, Excess Close): Index subject
of sections.
doc/lispref/customize.texi (Defining New Types, Applying Customizations)
(Custom Themes): Index subject of sections.
doc/lispref/control.texi (Sequencing, Combining Conditions)
(Processing of Errors, Cleanups): Index subject of sections.
doc/lispref/compile.texi (Eval During Compile): Index subject of sections.
doc/lispref/commands.texi (Using Interactive, Distinguish Interactive)
(Command Loop Info, Classifying Events, Event Mod)
(Invoking the Input Method): Index subject of sections.
doc/lispref/buffers.texi (Buffer List, Buffer Gap): Index subject of sections.
doc/lispref/backups.texi (Making Backups, Numbered Backups, Backup Names)
(Reverting): Index subject of sections.
doc/lispref/abbrevs.texi (Abbrev Tables, Defining Abbrevs, Abbrev Files)
(Abbrev Expansion, Standard Abbrev Tables, Abbrev Properties)
(Abbrev Table Properties): Index subject of sections.
doc/lispref/os.texi (Time of Day, Time Conversion, Time Parsing)
(Time Calculations, Idle Timers): Index subject of sections.
2014-12-23 20:42:30 +02:00
|
|
|
@cindex debugger, explicit entry
|
|
|
|
@cindex force entry to debugger
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2018-07-07 11:37:43 +03:00
|
|
|
@cindex @code{eval-defun}, and explicit entry to debugger
|
2007-09-06 04:25:08 +00:00
|
|
|
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} (@code{eval-defun}, a Lisp mode key
|
|
|
|
binding). @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
|
|
|
|
of @code{(debug)} isn't ignored, it will alter the execution of the
|
|
|
|
program!) The most common suitable places are inside a @code{progn} or
|
|
|
|
an implicit @code{progn} (@pxref{Sequencing}).
|
|
|
|
|
2012-09-04 23:21:00 +02:00
|
|
|
If you don't know exactly where in the source code you want to put
|
|
|
|
the debug statement, but you want to display a backtrace when a
|
|
|
|
certain message is displayed, you can set @code{debug-on-message} to a
|
|
|
|
regular expression matching the desired message.
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@node Using Debugger
|
|
|
|
@subsection Using the Debugger
|
|
|
|
|
|
|
|
When the debugger is entered, it displays the previously selected
|
2012-04-10 00:11:23 -07:00
|
|
|
buffer in one window and a buffer named @file{*Backtrace*} in another
|
2007-09-06 04:25:08 +00:00
|
|
|
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).
|
|
|
|
|
2012-11-10 17:16:25 -08:00
|
|
|
@vindex debugger-bury-or-kill
|
2007-09-06 04:25:08 +00:00
|
|
|
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 (@pxref{Recursive Editing})
|
|
|
|
and it is wise to go back to the backtrace buffer and exit the debugger
|
|
|
|
(with the @kbd{q} command) when you are finished with it. Exiting
|
2012-11-10 17:16:25 -08:00
|
|
|
the debugger gets out of the recursive edit and buries the backtrace
|
|
|
|
buffer. (You can customize what the @kbd{q} command does with the
|
|
|
|
backtrace buffer by setting the variable @code{debugger-bury-or-kill}.
|
|
|
|
For example, set it to @code{kill} if you prefer to kill the buffer
|
|
|
|
rather than bury it. Consult the variable's documentation for more
|
|
|
|
possibilities.)
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2011-07-15 15:16:11 +02:00
|
|
|
When the debugger has been entered, the @code{debug-on-error}
|
|
|
|
variable is temporarily set according to
|
|
|
|
@code{eval-expression-debug-on-error}. If the latter variable is
|
|
|
|
non-@code{nil}, @code{debug-on-error} will temporarily be set to
|
2022-05-01 13:07:14 +02:00
|
|
|
@code{t}. However, further errors that occur while debugging won't
|
|
|
|
(by default) trigger another debugger, because @code{inhibit-debugger}
|
|
|
|
will also be bound to non-@code{nil}.
|
2011-07-15 15:16:11 +02:00
|
|
|
|
2018-06-19 07:27:41 -07:00
|
|
|
The debugger itself must be run byte-compiled, since it makes
|
|
|
|
assumptions about the state of the Lisp interpreter. These
|
|
|
|
assumptions are false if the debugger is running interpreted.
|
|
|
|
|
|
|
|
@node Backtraces
|
|
|
|
@subsection Backtraces
|
|
|
|
@cindex backtrace buffer
|
|
|
|
|
|
|
|
Debugger mode is derived from Backtrace mode, which is also used to
|
2020-06-17 12:53:40 +01:00
|
|
|
show backtraces by Edebug and ERT@. (@pxref{Edebug}, and @ref{Top,the
|
2018-08-01 07:47:12 -07:00
|
|
|
ERT manual,, ert, ERT: Emacs Lisp Regression Testing}.)
|
2018-06-19 07:27:41 -07:00
|
|
|
|
|
|
|
@cindex stack frame
|
|
|
|
The backtrace buffer shows you the functions that are executing and
|
|
|
|
their argument values. When a backtrace buffer is created, it shows
|
|
|
|
each stack frame on one, possibly very long, line. (A stack frame is
|
|
|
|
the place where the Lisp interpreter records information about a
|
|
|
|
particular invocation of a function.) The most recently called
|
|
|
|
function will be at the top.
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@cindex current stack frame
|
2018-06-19 07:27:41 -07:00
|
|
|
In a backtrace you can specify a stack frame by moving point to a line
|
|
|
|
describing that frame. The frame whose line point is on is considered
|
|
|
|
the @dfn{current frame}.
|
|
|
|
|
|
|
|
If a function name is underlined, that means Emacs knows where its
|
|
|
|
source code is located. You can click with the mouse on that name, or
|
|
|
|
move to it and type @key{RET}, to visit the source code. You can also
|
|
|
|
type @key{RET} while point is on any name of a function or variable
|
|
|
|
which is not underlined, to see help information for that symbol in a
|
2018-08-01 07:47:12 -07:00
|
|
|
help buffer, if any exists. The @code{xref-find-definitions} command,
|
2020-12-29 16:55:06 +01:00
|
|
|
bound to @kbd{M-.}, can also be used on any identifier in a backtrace
|
2018-08-01 07:47:12 -07:00
|
|
|
(@pxref{Looking Up Identifiers,,,emacs, The GNU Emacs Manual}).
|
2018-06-19 07:27:41 -07:00
|
|
|
|
|
|
|
In backtraces, the tails of long lists and the ends of long strings,
|
|
|
|
vectors or structures, as well as objects which are deeply nested,
|
|
|
|
will be printed as underlined ``...''. You can click with the mouse
|
|
|
|
on a ``...'', or type @key{RET} while point is on it, to show the part
|
|
|
|
of the object that was hidden. To control how much abbreviation is
|
|
|
|
done, customize @code{backtrace-line-length}.
|
|
|
|
|
|
|
|
Here is a list of commands for navigating and viewing backtraces:
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2018-06-19 07:27:41 -07:00
|
|
|
@table @kbd
|
|
|
|
@item v
|
|
|
|
Toggle the display of local variables of the current stack frame.
|
|
|
|
|
|
|
|
@item p
|
|
|
|
Move to the beginning of the frame, or to the beginning
|
|
|
|
of the previous frame.
|
|
|
|
|
|
|
|
@item n
|
|
|
|
Move to the beginning of the next frame.
|
|
|
|
|
|
|
|
@item +
|
|
|
|
Add line breaks and indentation to the top-level Lisp form at point to
|
|
|
|
make it more readable.
|
|
|
|
|
2018-07-14 07:23:15 -07:00
|
|
|
@item -
|
2018-06-19 07:27:41 -07:00
|
|
|
Collapse the top-level Lisp form at point back to a single line.
|
|
|
|
|
|
|
|
@item #
|
|
|
|
Toggle @code{print-circle} for the frame at point.
|
|
|
|
|
2019-07-30 10:00:27 -07:00
|
|
|
@item :
|
|
|
|
Toggle @code{print-gensym} for the frame at point.
|
|
|
|
|
2018-07-14 08:05:51 -07:00
|
|
|
@item .
|
|
|
|
Expand all the forms abbreviated with ``...'' in the frame at point.
|
|
|
|
|
2018-06-19 07:27:41 -07:00
|
|
|
@end table
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@node Debugger Commands
|
|
|
|
@subsection Debugger Commands
|
|
|
|
@cindex debugger command list
|
|
|
|
|
|
|
|
The debugger buffer (in Debugger mode) provides special commands in
|
2018-06-19 07:27:41 -07:00
|
|
|
addition to the usual Emacs commands and to the Backtrace mode commands
|
|
|
|
described in the previous section. The most important use of
|
2007-09-06 04:25:08 +00:00
|
|
|
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.) You cannot use the Lisp debugger
|
|
|
|
to step through a primitive function.
|
|
|
|
|
2018-06-19 07:27:41 -07:00
|
|
|
Some of the debugger commands operate on the current frame. If a
|
|
|
|
frame starts with a star, that means that exiting that frame will call the
|
|
|
|
debugger again. This is useful for examining the return value of a
|
|
|
|
function.
|
|
|
|
|
2013-12-22 11:36:04 +08:00
|
|
|
@c FIXME: Add @findex for the following commands? --xfq
|
2007-09-06 04:25:08 +00:00
|
|
|
Here is a list of Debugger mode commands:
|
|
|
|
|
|
|
|
@table @kbd
|
|
|
|
@item c
|
2012-02-12 17:51:03 +08:00
|
|
|
Exit the debugger and continue execution. This resumes execution of
|
|
|
|
the program as if the debugger had never been entered (aside from any
|
|
|
|
side-effects that you caused by changing variable values or data
|
|
|
|
structures while inside the debugger).
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item d
|
|
|
|
Continue execution, but enter the debugger the next time any Lisp
|
|
|
|
function is called. This allows you to step through the
|
|
|
|
subexpressions of an expression, seeing what values the subexpressions
|
|
|
|
compute, and what else they do.
|
|
|
|
|
|
|
|
The stack frame made for the function call which enters the debugger in
|
|
|
|
this way will be flagged automatically so that the debugger will be
|
|
|
|
called again when the frame is exited. You can use the @kbd{u} command
|
|
|
|
to cancel this flag.
|
|
|
|
|
|
|
|
@item b
|
|
|
|
Flag the current frame so that the debugger will be entered when the
|
|
|
|
frame is exited. Frames flagged in this way are marked with stars
|
|
|
|
in the backtrace buffer.
|
|
|
|
|
|
|
|
@item u
|
|
|
|
Don't enter the debugger when the current frame is exited. This
|
|
|
|
cancels a @kbd{b} command on that frame. The visible effect is to
|
|
|
|
remove the star from the line in the backtrace buffer.
|
|
|
|
|
|
|
|
@item j
|
|
|
|
Flag the current frame like @kbd{b}. Then continue execution like
|
|
|
|
@kbd{c}, but temporarily disable break-on-entry for all functions that
|
|
|
|
are set up to do so by @code{debug-on-entry}.
|
|
|
|
|
2022-05-01 13:40:13 +02:00
|
|
|
@vindex debug-allow-recursive-debug
|
2007-09-06 04:25:08 +00:00
|
|
|
@item e
|
2014-02-13 00:26:01 -08:00
|
|
|
Read a Lisp expression in the minibuffer, evaluate it (with the
|
|
|
|
relevant lexical environment, if applicable), and print the
|
2007-09-06 04:25:08 +00:00
|
|
|
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 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
|
2022-05-01 13:40:13 +02:00
|
|
|
the variable values within the debugger. By default, this command
|
|
|
|
suppresses the debugger during evaluation, so that an error in the
|
|
|
|
evaluated expression won't add a new error on top of the existing one.
|
|
|
|
Set the @code{debug-allow-recursive-debug} user option to a
|
|
|
|
non-@code{nil} value to override this.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item R
|
|
|
|
Like @kbd{e}, but also save the result of evaluation in the
|
2012-04-10 00:11:23 -07:00
|
|
|
buffer @file{*Debugger-record*}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@item q
|
|
|
|
Terminate the program being debugged; return to top-level Emacs
|
|
|
|
command execution.
|
|
|
|
|
|
|
|
If the debugger was entered due to a @kbd{C-g} but you really want
|
|
|
|
to quit, and not debug, use the @kbd{q} command.
|
|
|
|
|
|
|
|
@item r
|
|
|
|
Return a value from the debugger. The value is computed by reading an
|
|
|
|
expression with the minibuffer and evaluating it.
|
|
|
|
|
|
|
|
The @kbd{r} command is useful when the debugger was invoked due to exit
|
|
|
|
from a Lisp call frame (as requested with @kbd{b} or by entering the
|
|
|
|
frame with @kbd{d}); then the value specified in the @kbd{r} command is
|
|
|
|
used as the value of that frame. It is also useful if you call
|
|
|
|
@code{debug} and use its return value. Otherwise, @kbd{r} has the same
|
|
|
|
effect as @kbd{c}, and the specified return value does not matter.
|
|
|
|
|
|
|
|
You can't use @kbd{r} when the debugger was entered due to an error.
|
|
|
|
|
|
|
|
@item l
|
|
|
|
Display a list of functions that will invoke the debugger when called.
|
|
|
|
This is a list of functions that are set to break on entry by means of
|
2014-07-08 14:24:39 -04:00
|
|
|
@code{debug-on-entry}.
|
2013-12-22 11:36:04 +08:00
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@end table
|
|
|
|
|
|
|
|
@node Invoking the Debugger
|
|
|
|
@subsection Invoking the Debugger
|
Improve indexing on the chapter/section/subsection levels.
doc/lispref/windows.texi (Recombining Windows): Index subject of sections.
doc/lispref/variables.texi (Variables with Restricted Values)
(Generalized Variables): Index subject of sections.
doc/lispref/text.texi (Buffer Contents, Examining Properties)
(Changing Properties, Property Search, Substitution): Index
subject of sections.
doc/lispref/syntax.texi (Motion and Syntax, Parsing Expressions)
(Motion via Parsing, Position Parse, Control Parsing): Index
subject of sections.
doc/lispref/strings.texi (Predicates for Strings, Creating Strings)
(Modifying Strings, Text Comparison): Index subject of sections.
doc/lispref/searching.texi (Syntax of Regexps, Regexp Special)
(Regexp Functions, Regexp Functions): Index subject of sections.
doc/lispref/processes.texi (Subprocess Creation, Process Information): Index
subject of sections.
doc/lispref/positions.texi (Screen Lines): Index subject of sections.
doc/lispref/nonascii.texi (Scanning Charsets, Specifying Coding Systems):
Index subject of sections.
doc/lispref/minibuf.texi (Text from Minibuffer, Object from Minibuffer)
(Multiple Queries, Minibuffer Contents): Index subject of
sections.
doc/lispref/markers.texi (Predicates on Markers, Creating Markers)
(Information from Markers, Moving Markers): Index subject of
sections.
doc/lispref/macros.texi (Defining Macros, Problems with Macros): Index
subject of sections.
doc/lispref/loading.texi (Loading Non-ASCII, Where Defined): Index subject
of sections.
doc/lispref/lists.texi (List-related Predicates, List Variables, Setcar)
(Setcdr, Plist Access): Index subject of sections.
doc/lispref/keymaps.texi (Controlling Active Maps, Scanning Keymaps)
(Modifying Menus): Index subject of sections.
doc/lispref/help.texi (Accessing Documentation, Help Functions): Index
subject of sections.
doc/lispref/hash.texi (Hash Access): Index subject of sections.
doc/lispref/functions.texi (Core Advising Primitives)
(Advising Named Functions, Porting old advices): Index subject of
sections.
doc/lispref/frames.texi (Creating Frames, Initial Parameters)
(Position Parameters, Buffer Parameters, Minibuffers and Frames)
(Pop-Up Menus, Drag and Drop): Index subject of sections.
doc/lispref/files.texi (Visiting Functions, Kinds of Files)
(Unique File Names): Index subject of sections.
doc/lispref/display.texi (Refresh Screen, Echo Area Customization)
(Warning Variables, Warning Options, Delayed Warnings)
(Temporary Displays, Managing Overlays, Overlay Properties)
(Finding Overlays, Size of Displayed Text, Defining Faces)
(Attribute Functions, Displaying Faces, Face Remapping)
(Basic Faces, Font Lookup, Fontsets, Replacing Specs)
(Defining Images, Showing Images): Index subject of sections.
doc/lispref/debugging.texi (Debugging, Explicit Debug)
(Invoking the Debugger, Excess Open, Excess Close): Index subject
of sections.
doc/lispref/customize.texi (Defining New Types, Applying Customizations)
(Custom Themes): Index subject of sections.
doc/lispref/control.texi (Sequencing, Combining Conditions)
(Processing of Errors, Cleanups): Index subject of sections.
doc/lispref/compile.texi (Eval During Compile): Index subject of sections.
doc/lispref/commands.texi (Using Interactive, Distinguish Interactive)
(Command Loop Info, Classifying Events, Event Mod)
(Invoking the Input Method): Index subject of sections.
doc/lispref/buffers.texi (Buffer List, Buffer Gap): Index subject of sections.
doc/lispref/backups.texi (Making Backups, Numbered Backups, Backup Names)
(Reverting): Index subject of sections.
doc/lispref/abbrevs.texi (Abbrev Tables, Defining Abbrevs, Abbrev Files)
(Abbrev Expansion, Standard Abbrev Tables, Abbrev Properties)
(Abbrev Table Properties): Index subject of sections.
doc/lispref/os.texi (Time of Day, Time Conversion, Time Parsing)
(Time Calculations, Idle Timers): Index subject of sections.
2014-12-23 20:42:30 +02:00
|
|
|
@cindex invoking lisp debugger
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
Here we describe in full detail the function @code{debug} that is used
|
|
|
|
to invoke the debugger.
|
|
|
|
|
2012-05-15 11:38:50 +02:00
|
|
|
@deffn Command debug &rest debugger-args
|
2023-11-09 10:33:28 +02:00
|
|
|
This function enters the debugger. In interactive sessions, it
|
|
|
|
switches to a buffer named @file{*Backtrace*} (or
|
|
|
|
@file{*Backtrace*<2>} if it is the second recursive entry to the
|
|
|
|
debugger, etc.), and fills it with information about the stack of Lisp
|
|
|
|
function calls. It then enters a recursive edit, showing the
|
|
|
|
backtrace buffer in Debugger mode. In batch mode (more generally,
|
|
|
|
when @code{noninteractive} is non-@code{nil}, @pxref{Batch Mode}),
|
|
|
|
this function shows the Lisp backtrace on the standard error stream,
|
|
|
|
and then kills Emacs, causing it to exit with a non-zero exit code
|
|
|
|
(@pxref{Killing Emacs}). Binding
|
|
|
|
@code{backtrace-on-error-noninteractive} to @code{nil} suppresses the
|
|
|
|
backtrace in batch mode, see below.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
The Debugger mode @kbd{c}, @kbd{d}, @kbd{j}, and @kbd{r} commands exit
|
|
|
|
the recursive edit; then @code{debug} switches back to the previous
|
|
|
|
buffer and returns to whatever called @code{debug}. This is the only
|
|
|
|
way the function @code{debug} can return to its caller.
|
|
|
|
|
|
|
|
The use of the @var{debugger-args} is that @code{debug} displays the
|
2012-04-10 00:11:23 -07:00
|
|
|
rest of its arguments at the top of the @file{*Backtrace*} buffer, so
|
2007-09-06 04:25:08 +00:00
|
|
|
that the user can see them. Except as described below, this is the
|
|
|
|
@emph{only} way these arguments are used.
|
|
|
|
|
|
|
|
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
|
|
|
|
@cindex @code{lambda} in debug
|
|
|
|
A first argument of @code{lambda} means @code{debug} was called
|
|
|
|
because of entry to a function when @code{debug-on-next-call} was
|
|
|
|
non-@code{nil}. The debugger displays @samp{Debugger
|
|
|
|
entered--entering a function:} as a line of text at the top of the
|
|
|
|
buffer.
|
|
|
|
|
|
|
|
@item debug
|
|
|
|
@code{debug} as first argument means @code{debug} was called because
|
|
|
|
of entry to a function that was set to debug on entry. The debugger
|
|
|
|
displays the string @samp{Debugger entered--entering a function:},
|
|
|
|
just as in the @code{lambda} case. It also marks the stack frame for
|
|
|
|
that function so that it will invoke the debugger when exited.
|
|
|
|
|
|
|
|
@item t
|
|
|
|
When the first argument is @code{t}, this indicates a call to
|
|
|
|
@code{debug} due to evaluation of a function call form when
|
|
|
|
@code{debug-on-next-call} is non-@code{nil}. The debugger displays
|
|
|
|
@samp{Debugger entered--beginning evaluation of function call form:}
|
|
|
|
as the top line in the buffer.
|
|
|
|
|
|
|
|
@item exit
|
|
|
|
When the first argument is @code{exit}, it indicates the exit of a
|
|
|
|
stack frame previously marked to invoke the debugger on exit. The
|
|
|
|
second argument given to @code{debug} in this case is the value being
|
|
|
|
returned from the frame. The debugger displays @samp{Debugger
|
|
|
|
entered--returning value:} in the top line of the buffer, followed by
|
|
|
|
the value being returned.
|
|
|
|
|
|
|
|
@item error
|
|
|
|
@cindex @code{error} in debug
|
|
|
|
When the first argument is @code{error}, the debugger indicates that
|
|
|
|
it is being entered because an error or @code{quit} was signaled and
|
|
|
|
not handled, by displaying @samp{Debugger entered--Lisp error:}
|
|
|
|
followed by the error signaled and any arguments to @code{signal}.
|
|
|
|
For example,
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(let ((debug-on-error t))
|
|
|
|
(/ 1 0))
|
|
|
|
@end group
|
|
|
|
|
|
|
|
@group
|
|
|
|
------ Buffer: *Backtrace* ------
|
|
|
|
Debugger entered--Lisp error: (arith-error)
|
|
|
|
/(1 0)
|
|
|
|
...
|
|
|
|
------ Buffer: *Backtrace* ------
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
If an error was signaled, presumably the variable
|
|
|
|
@code{debug-on-error} is non-@code{nil}. If @code{quit} was signaled,
|
|
|
|
then presumably the variable @code{debug-on-quit} is non-@code{nil}.
|
|
|
|
|
|
|
|
@item nil
|
|
|
|
Use @code{nil} as the first of the @var{debugger-args} when you want
|
|
|
|
to enter the debugger explicitly. The rest of the @var{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 @code{debug} is called.
|
|
|
|
@end table
|
2012-05-15 11:38:50 +02:00
|
|
|
@end deffn
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2023-11-09 10:33:28 +02:00
|
|
|
@defvar backtrace-on-error-noninteractive
|
|
|
|
If this variable is non-@code{nil}, the default, entering the debugger
|
|
|
|
in batch mode shows the backtrace of Lisp functions calls. Binding
|
|
|
|
the variable to the @code{nil} value suppresses the backtrace and
|
|
|
|
shows only the error message.
|
|
|
|
@end defvar
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@node Internals of Debugger
|
|
|
|
@subsection Internals of the Debugger
|
|
|
|
|
|
|
|
This section describes functions and variables used internally by the
|
|
|
|
debugger.
|
|
|
|
|
|
|
|
@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. 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
|
|
|
|
was called. The convention for arguments is detailed in the description
|
|
|
|
of @code{debug} (@pxref{Invoking the Debugger}).
|
|
|
|
@end defvar
|
|
|
|
|
2018-01-28 19:27:39 +01:00
|
|
|
@defun backtrace
|
2007-09-06 04:25:08 +00:00
|
|
|
@cindex run time stack
|
|
|
|
@cindex call stack
|
|
|
|
This function prints a trace of Lisp function calls currently active.
|
2018-07-18 08:30:45 -07:00
|
|
|
The trace is identical to the one that @code{debug} would show in the
|
Fix some Texinfo markup in manuals
* doc/emacs/macos.texi (Mac / GNUstep Customization):
* doc/lispintro/emacs-lisp-intro.texi (condition-case):
* doc/lispref/control.texi (pcase Macro):
* doc/lispref/debugging.texi (Internals of Debugger):
* doc/lispref/internals.texi (Building Emacs):
* doc/lispref/modes.texi (Imenu):
(Parser-based Font Lock, Parser-based Indentation):
* doc/lispref/parsing.texi (Retrieving Nodes, Tree-sitter C API):
* doc/lispref/processes.texi (Network, Bindat Types):
* doc/lispref/searching.texi (Rx Functions):
* doc/lispref/text.texi (Replacing):
* doc/lispref/windows.texi (Textual Scrolling):
* doc/misc/calc.texi (Killing From Stack, Customizing Calc):
* doc/misc/cc-mode.texi (Misc Font Locking, List Line-Up):
* doc/misc/ede.texi (ede-cpp-root-project)
(ede-proj-target-makefile, ede-sourcecode):
* doc/misc/ert.texi (Running Tests in Batch Mode):
* doc/misc/eudc.texi (Emacs-only Configuration, The Server Hotlist):
* doc/misc/eww.texi (Advanced):
* doc/misc/flymake.texi (Starting Flymake)
(Proc customization variables):
* doc/misc/tramp.texi (File name completion):
* doc/misc/gnus.texi (Summary Buffer Lines, Gnus Registry Setup)
(Fancy splitting to parent, Customizing the IMAP Connection)
(Mail Source Specifiers, Agent as Cache): Consistently mark up nil
and t as @code. Also fix the markup and wording of some surrounding
text (bug#64016).
* doc/lispref/display.texi (SVG Images, Icons):
* doc/lispref/modes.texi (Customizing Keywords): Prefer ASCII
apostrophe over Unicode right single quotation mark.
2023-06-12 14:42:31 +01:00
|
|
|
@file{*Backtrace*} buffer. The return value is always @code{nil}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
In the following example, a Lisp expression calls @code{backtrace}
|
|
|
|
explicitly. This prints the backtrace to the stream
|
|
|
|
@code{standard-output}, which, in this case, is the buffer
|
|
|
|
@samp{backtrace-output}.
|
|
|
|
|
2018-07-18 08:30:45 -07:00
|
|
|
Each line of the backtrace represents one function call. The line
|
|
|
|
shows the function followed by a list of the values of the function's
|
|
|
|
arguments if they are all known; if they are still being computed, the
|
|
|
|
line consists of a list containing the function and its unevaluated
|
2018-08-01 07:47:12 -07:00
|
|
|
arguments. Long lists or deeply nested structures may be elided.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@smallexample
|
|
|
|
@group
|
|
|
|
(with-output-to-temp-buffer "backtrace-output"
|
|
|
|
(let ((var 1))
|
|
|
|
(save-excursion
|
|
|
|
(setq var (eval '(progn
|
|
|
|
(1+ var)
|
|
|
|
(list 'testing (backtrace))))))))
|
|
|
|
|
|
|
|
@result{} (testing nil)
|
|
|
|
@end group
|
|
|
|
|
|
|
|
@group
|
|
|
|
----------- Buffer: backtrace-output ------------
|
|
|
|
backtrace()
|
2018-07-18 08:30:45 -07:00
|
|
|
(list 'testing (backtrace))
|
2007-09-06 04:25:08 +00:00
|
|
|
@end group
|
|
|
|
(progn ...)
|
2018-01-15 13:42:51 -05:00
|
|
|
eval((progn (1+ var) (list 'testing (backtrace))))
|
2007-09-06 04:25:08 +00:00
|
|
|
(setq ...)
|
|
|
|
(save-excursion ...)
|
|
|
|
(let ...)
|
|
|
|
(with-output-to-temp-buffer ...)
|
|
|
|
eval((with-output-to-temp-buffer ...))
|
|
|
|
eval-last-sexp-1(nil)
|
|
|
|
@group
|
|
|
|
eval-last-sexp(nil)
|
|
|
|
call-interactively(eval-last-sexp)
|
|
|
|
----------- Buffer: backtrace-output ------------
|
|
|
|
@end group
|
|
|
|
@end smallexample
|
2018-01-28 19:27:39 +01:00
|
|
|
@end defun
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2018-01-28 19:27:39 +01:00
|
|
|
@defopt debugger-stack-frame-as-list
|
2016-09-30 16:22:26 +03:00
|
|
|
If this variable is non-@code{nil}, every stack frame of the backtrace
|
|
|
|
is displayed as a list. This aims at improving the backtrace
|
|
|
|
readability at the cost of special forms no longer being visually
|
|
|
|
different from regular function calls.
|
|
|
|
|
|
|
|
With @code{debugger-stack-frame-as-list} non-@code{nil}, the above
|
|
|
|
example would look as follows:
|
|
|
|
|
|
|
|
@smallexample
|
|
|
|
@group
|
|
|
|
----------- Buffer: backtrace-output ------------
|
|
|
|
(backtrace)
|
2018-07-18 08:30:45 -07:00
|
|
|
(list 'testing (backtrace))
|
2016-09-30 16:22:26 +03:00
|
|
|
@end group
|
|
|
|
(progn ...)
|
2018-01-15 13:42:51 -05:00
|
|
|
(eval (progn (1+ var) (list 'testing (backtrace))))
|
2016-09-30 16:22:26 +03:00
|
|
|
(setq ...)
|
|
|
|
(save-excursion ...)
|
|
|
|
(let ...)
|
|
|
|
(with-output-to-temp-buffer ...)
|
|
|
|
(eval (with-output-to-temp-buffer ...))
|
|
|
|
(eval-last-sexp-1 nil)
|
|
|
|
@group
|
|
|
|
(eval-last-sexp nil)
|
|
|
|
(call-interactively eval-last-sexp)
|
|
|
|
----------- Buffer: backtrace-output ------------
|
|
|
|
@end group
|
|
|
|
@end smallexample
|
2018-01-28 19:27:39 +01:00
|
|
|
@end defopt
|
2016-09-30 16:22:26 +03:00
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@defvar debug-on-next-call
|
|
|
|
@cindex @code{eval}, and debugging
|
|
|
|
@cindex @code{apply}, and debugging
|
|
|
|
@cindex @code{funcall}, and debugging
|
|
|
|
If this variable is non-@code{nil}, it says to call the debugger before
|
|
|
|
the next @code{eval}, @code{apply} or @code{funcall}. Entering the
|
|
|
|
debugger sets @code{debug-on-next-call} to @code{nil}.
|
|
|
|
|
|
|
|
The @kbd{d} command in the debugger works by setting this variable.
|
|
|
|
@end defvar
|
|
|
|
|
|
|
|
@defun backtrace-debug level flag
|
|
|
|
This function sets the debug-on-exit flag of the stack frame @var{level}
|
|
|
|
levels down the stack, giving it the value @var{flag}. If @var{flag} is
|
|
|
|
non-@code{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.
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defvar command-debug-status
|
|
|
|
This variable records the debugging status of the current interactive
|
|
|
|
command. Each time a command is called interactively, this variable is
|
|
|
|
bound to @code{nil}. The debugger can set this variable to leave
|
|
|
|
information for future debugger invocations during the same 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.
|
2016-10-04 17:34:51 +03:00
|
|
|
|
|
|
|
This variable is obsolete and will be removed in future versions.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defvar
|
|
|
|
|
2016-12-05 00:52:14 -05:00
|
|
|
@defun backtrace-frame frame-number &optional base
|
2007-09-06 04:25:08 +00:00
|
|
|
The function @code{backtrace-frame} is intended for use in Lisp
|
|
|
|
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 evaluated its arguments and called its 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
|
|
|
|
@sc{car} of the evaluated list, or a @code{lambda} expression in the
|
|
|
|
case of a macro call. If the function has a @code{&rest} argument, that
|
|
|
|
is represented as the tail of the list @var{arg-values}.
|
|
|
|
|
2016-12-05 00:52:14 -05:00
|
|
|
If @var{base} is specified, @var{frame-number} counts relative to
|
|
|
|
the topmost frame whose @var{function} is @var{base}.
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
If @var{frame-number} is out of range, @code{backtrace-frame} returns
|
|
|
|
@code{nil}.
|
|
|
|
@end defun
|
|
|
|
|
2016-12-05 00:52:14 -05:00
|
|
|
@defun mapbacktrace function &optional base
|
|
|
|
The function @code{mapbacktrace} calls @var{function} once for each
|
|
|
|
frame in the backtrace, starting at the first frame whose function is
|
|
|
|
@var{base} (or from the top if @var{base} is omitted or @code{nil}).
|
|
|
|
|
|
|
|
@var{function} is called with four arguments: @var{evald}, @var{func},
|
|
|
|
@var{args}, and @var{flags}.
|
|
|
|
|
|
|
|
If a frame has not evaluated its arguments yet or is a special form,
|
|
|
|
@var{evald} is @code{nil} and @var{args} is a list of forms.
|
|
|
|
|
|
|
|
If a frame has evaluated its arguments and called its function
|
|
|
|
already, @var{evald} is @code{t} and @var{args} is a list of values.
|
|
|
|
@var{flags} is a plist of properties of the current frame: currently,
|
|
|
|
the only supported property is @code{:debug-on-exit}, which is
|
|
|
|
@code{t} if the stack frame's @code{debug-on-exit} flag is set.
|
|
|
|
@end defun
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@include edebug.texi
|
|
|
|
|
|
|
|
@node Syntax Errors
|
|
|
|
@section Debugging Invalid Lisp Syntax
|
|
|
|
@cindex debugging invalid Lisp syntax
|
|
|
|
|
|
|
|
The Lisp reader reports invalid syntax, but cannot say where the real
|
2015-09-15 08:46:48 -07:00
|
|
|
problem is. For example, the error @samp{End of file during parsing} in
|
2007-09-06 04:25:08 +00:00
|
|
|
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
|
2015-09-15 08:46:48 -07:00
|
|
|
been. Likewise, @samp{Invalid read syntax: ")"} indicates an excess close
|
2007-09-06 04:25:08 +00:00
|
|
|
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
|
2019-08-03 12:41:35 +03:00
|
|
|
technique is to try @kbd{C-M-e} (@code{end-of-defun}, @pxref{Moving by
|
|
|
|
Defuns,,,emacs, The GNU Emacs Manual}) 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.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@cindex unbalanced parentheses
|
|
|
|
@cindex parenthesis mismatch, debugging
|
|
|
|
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.)
|
|
|
|
|
|
|
|
@menu
|
|
|
|
* Excess Open:: How to find a spurious open paren or missing close.
|
|
|
|
* Excess Close:: How to find a spurious close paren or missing open.
|
|
|
|
@end menu
|
|
|
|
|
|
|
|
@node Excess Open
|
|
|
|
@subsection Excess Open Parentheses
|
Improve indexing on the chapter/section/subsection levels.
doc/lispref/windows.texi (Recombining Windows): Index subject of sections.
doc/lispref/variables.texi (Variables with Restricted Values)
(Generalized Variables): Index subject of sections.
doc/lispref/text.texi (Buffer Contents, Examining Properties)
(Changing Properties, Property Search, Substitution): Index
subject of sections.
doc/lispref/syntax.texi (Motion and Syntax, Parsing Expressions)
(Motion via Parsing, Position Parse, Control Parsing): Index
subject of sections.
doc/lispref/strings.texi (Predicates for Strings, Creating Strings)
(Modifying Strings, Text Comparison): Index subject of sections.
doc/lispref/searching.texi (Syntax of Regexps, Regexp Special)
(Regexp Functions, Regexp Functions): Index subject of sections.
doc/lispref/processes.texi (Subprocess Creation, Process Information): Index
subject of sections.
doc/lispref/positions.texi (Screen Lines): Index subject of sections.
doc/lispref/nonascii.texi (Scanning Charsets, Specifying Coding Systems):
Index subject of sections.
doc/lispref/minibuf.texi (Text from Minibuffer, Object from Minibuffer)
(Multiple Queries, Minibuffer Contents): Index subject of
sections.
doc/lispref/markers.texi (Predicates on Markers, Creating Markers)
(Information from Markers, Moving Markers): Index subject of
sections.
doc/lispref/macros.texi (Defining Macros, Problems with Macros): Index
subject of sections.
doc/lispref/loading.texi (Loading Non-ASCII, Where Defined): Index subject
of sections.
doc/lispref/lists.texi (List-related Predicates, List Variables, Setcar)
(Setcdr, Plist Access): Index subject of sections.
doc/lispref/keymaps.texi (Controlling Active Maps, Scanning Keymaps)
(Modifying Menus): Index subject of sections.
doc/lispref/help.texi (Accessing Documentation, Help Functions): Index
subject of sections.
doc/lispref/hash.texi (Hash Access): Index subject of sections.
doc/lispref/functions.texi (Core Advising Primitives)
(Advising Named Functions, Porting old advices): Index subject of
sections.
doc/lispref/frames.texi (Creating Frames, Initial Parameters)
(Position Parameters, Buffer Parameters, Minibuffers and Frames)
(Pop-Up Menus, Drag and Drop): Index subject of sections.
doc/lispref/files.texi (Visiting Functions, Kinds of Files)
(Unique File Names): Index subject of sections.
doc/lispref/display.texi (Refresh Screen, Echo Area Customization)
(Warning Variables, Warning Options, Delayed Warnings)
(Temporary Displays, Managing Overlays, Overlay Properties)
(Finding Overlays, Size of Displayed Text, Defining Faces)
(Attribute Functions, Displaying Faces, Face Remapping)
(Basic Faces, Font Lookup, Fontsets, Replacing Specs)
(Defining Images, Showing Images): Index subject of sections.
doc/lispref/debugging.texi (Debugging, Explicit Debug)
(Invoking the Debugger, Excess Open, Excess Close): Index subject
of sections.
doc/lispref/customize.texi (Defining New Types, Applying Customizations)
(Custom Themes): Index subject of sections.
doc/lispref/control.texi (Sequencing, Combining Conditions)
(Processing of Errors, Cleanups): Index subject of sections.
doc/lispref/compile.texi (Eval During Compile): Index subject of sections.
doc/lispref/commands.texi (Using Interactive, Distinguish Interactive)
(Command Loop Info, Classifying Events, Event Mod)
(Invoking the Input Method): Index subject of sections.
doc/lispref/buffers.texi (Buffer List, Buffer Gap): Index subject of sections.
doc/lispref/backups.texi (Making Backups, Numbered Backups, Backup Names)
(Reverting): Index subject of sections.
doc/lispref/abbrevs.texi (Abbrev Tables, Defining Abbrevs, Abbrev Files)
(Abbrev Expansion, Standard Abbrev Tables, Abbrev Properties)
(Abbrev Table Properties): Index subject of sections.
doc/lispref/os.texi (Time of Day, Time Conversion, Time Parsing)
(Time Calculations, Idle Timers): Index subject of sections.
2014-12-23 20:42:30 +02:00
|
|
|
@cindex excess open parentheses
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
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 go to the end of
|
2019-08-03 12:41:35 +03:00
|
|
|
the file and type @kbd{C-u C-M-u} (@code{backward-up-list},
|
|
|
|
@pxref{Moving by Parens,,,emacs, The GNU Emacs Manual}). This will
|
|
|
|
move you to the beginning of the first defun that is unbalanced.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
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 @kbd{C-M-q}
|
2019-08-03 12:41:35 +03:00
|
|
|
(@code{indent-pp-sexp}, @pxref{Multi-line Indent,,,emacs, The GNU
|
|
|
|
Emacs Manual}) and see what moves. @strong{But don't do this yet!}
|
|
|
|
Keep reading, first.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
Before you do this, make sure the defun has enough close parentheses.
|
|
|
|
Otherwise, @kbd{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
|
2019-08-03 12:41:35 +03:00
|
|
|
close parenthesis there. Don't use @kbd{C-M-e} (@code{end-of-defun}) to
|
|
|
|
move there, since that too will fail to work until the defun is balanced.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
Now you can go to the beginning of the defun and type @kbd{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
|
2019-08-03 12:41:35 +03:00
|
|
|
the discrepancy, undo the @kbd{C-M-q} with @kbd{C-_} (@code{undo}),
|
|
|
|
since the old indentation is probably appropriate to the intended
|
|
|
|
parentheses.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
and you have put back those parentheses, @kbd{C-M-q} should not change
|
|
|
|
anything.
|
|
|
|
|
|
|
|
@node Excess Close
|
|
|
|
@subsection Excess Close Parentheses
|
Improve indexing on the chapter/section/subsection levels.
doc/lispref/windows.texi (Recombining Windows): Index subject of sections.
doc/lispref/variables.texi (Variables with Restricted Values)
(Generalized Variables): Index subject of sections.
doc/lispref/text.texi (Buffer Contents, Examining Properties)
(Changing Properties, Property Search, Substitution): Index
subject of sections.
doc/lispref/syntax.texi (Motion and Syntax, Parsing Expressions)
(Motion via Parsing, Position Parse, Control Parsing): Index
subject of sections.
doc/lispref/strings.texi (Predicates for Strings, Creating Strings)
(Modifying Strings, Text Comparison): Index subject of sections.
doc/lispref/searching.texi (Syntax of Regexps, Regexp Special)
(Regexp Functions, Regexp Functions): Index subject of sections.
doc/lispref/processes.texi (Subprocess Creation, Process Information): Index
subject of sections.
doc/lispref/positions.texi (Screen Lines): Index subject of sections.
doc/lispref/nonascii.texi (Scanning Charsets, Specifying Coding Systems):
Index subject of sections.
doc/lispref/minibuf.texi (Text from Minibuffer, Object from Minibuffer)
(Multiple Queries, Minibuffer Contents): Index subject of
sections.
doc/lispref/markers.texi (Predicates on Markers, Creating Markers)
(Information from Markers, Moving Markers): Index subject of
sections.
doc/lispref/macros.texi (Defining Macros, Problems with Macros): Index
subject of sections.
doc/lispref/loading.texi (Loading Non-ASCII, Where Defined): Index subject
of sections.
doc/lispref/lists.texi (List-related Predicates, List Variables, Setcar)
(Setcdr, Plist Access): Index subject of sections.
doc/lispref/keymaps.texi (Controlling Active Maps, Scanning Keymaps)
(Modifying Menus): Index subject of sections.
doc/lispref/help.texi (Accessing Documentation, Help Functions): Index
subject of sections.
doc/lispref/hash.texi (Hash Access): Index subject of sections.
doc/lispref/functions.texi (Core Advising Primitives)
(Advising Named Functions, Porting old advices): Index subject of
sections.
doc/lispref/frames.texi (Creating Frames, Initial Parameters)
(Position Parameters, Buffer Parameters, Minibuffers and Frames)
(Pop-Up Menus, Drag and Drop): Index subject of sections.
doc/lispref/files.texi (Visiting Functions, Kinds of Files)
(Unique File Names): Index subject of sections.
doc/lispref/display.texi (Refresh Screen, Echo Area Customization)
(Warning Variables, Warning Options, Delayed Warnings)
(Temporary Displays, Managing Overlays, Overlay Properties)
(Finding Overlays, Size of Displayed Text, Defining Faces)
(Attribute Functions, Displaying Faces, Face Remapping)
(Basic Faces, Font Lookup, Fontsets, Replacing Specs)
(Defining Images, Showing Images): Index subject of sections.
doc/lispref/debugging.texi (Debugging, Explicit Debug)
(Invoking the Debugger, Excess Open, Excess Close): Index subject
of sections.
doc/lispref/customize.texi (Defining New Types, Applying Customizations)
(Custom Themes): Index subject of sections.
doc/lispref/control.texi (Sequencing, Combining Conditions)
(Processing of Errors, Cleanups): Index subject of sections.
doc/lispref/compile.texi (Eval During Compile): Index subject of sections.
doc/lispref/commands.texi (Using Interactive, Distinguish Interactive)
(Command Loop Info, Classifying Events, Event Mod)
(Invoking the Input Method): Index subject of sections.
doc/lispref/buffers.texi (Buffer List, Buffer Gap): Index subject of sections.
doc/lispref/backups.texi (Making Backups, Numbered Backups, Backup Names)
(Reverting): Index subject of sections.
doc/lispref/abbrevs.texi (Abbrev Tables, Defining Abbrevs, Abbrev Files)
(Abbrev Expansion, Standard Abbrev Tables, Abbrev Properties)
(Abbrev Table Properties): Index subject of sections.
doc/lispref/os.texi (Time of Day, Time Conversion, Time Parsing)
(Time Calculations, Idle Timers): Index subject of sections.
2014-12-23 20:42:30 +02:00
|
|
|
@cindex excess close parentheses
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
To deal with an excess close parenthesis, first go to the beginning
|
2019-08-03 12:41:35 +03:00
|
|
|
of the file, then type @kbd{C-u -1 C-M-u} (@code{backward-up-list}
|
|
|
|
with an argument of @minus{}1) to find the end of the first unbalanced
|
|
|
|
defun.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
Then find the actual matching close parenthesis by typing @kbd{C-M-f}
|
2019-08-03 12:41:35 +03:00
|
|
|
(@code{forward-sexp}, @pxref{Expressions,,,emacs, The GNU Emacs Manual})
|
2007-09-06 04:25:08 +00:00
|
|
|
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
|
2019-08-03 12:41:35 +03:00
|
|
|
type @kbd{C-M-q} (@code{indent-pp-sexp}) 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 @kbd{C-M-q}
|
|
|
|
with @kbd{C-_} (@code{undo}), since the old indentation is probably
|
|
|
|
appropriate to the intended parentheses.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
After you think you have fixed the problem, use @kbd{C-M-q} again. If
|
|
|
|
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 Test Coverage
|
|
|
|
@section Test Coverage
|
|
|
|
@cindex coverage testing
|
|
|
|
|
|
|
|
@findex testcover-start
|
|
|
|
@findex testcover-mark-all
|
|
|
|
@findex testcover-next-mark
|
|
|
|
You can do coverage testing for a file of Lisp code by loading the
|
|
|
|
@code{testcover} library and using the command @kbd{M-x
|
|
|
|
testcover-start @key{RET} @var{file} @key{RET}} to instrument the
|
|
|
|
code. Then test your code by calling it one or more times. Then use
|
|
|
|
the command @kbd{M-x testcover-mark-all} to display colored highlights
|
|
|
|
on the code to show where coverage is insufficient. The command
|
|
|
|
@kbd{M-x testcover-next-mark} will move point forward to the next
|
|
|
|
highlighted spot.
|
|
|
|
|
|
|
|
Normally, a red highlight indicates the form was never completely
|
|
|
|
evaluated; a brown highlight means it always evaluated to the same
|
|
|
|
value (meaning there has been little testing of what is done with the
|
|
|
|
result). However, the red highlight is skipped for forms that can't
|
|
|
|
possibly complete their evaluation, such as @code{error}. The brown
|
|
|
|
highlight is skipped for forms that are expected to always evaluate to
|
|
|
|
the same value, such as @code{(setq x 14)}.
|
|
|
|
|
|
|
|
For difficult cases, you can add do-nothing macros to your code to
|
|
|
|
give advice to the test coverage tool.
|
|
|
|
|
|
|
|
@defmac 1value form
|
|
|
|
Evaluate @var{form} and return its value, but inform coverage testing
|
|
|
|
that @var{form}'s value should always be the same.
|
|
|
|
@end defmac
|
|
|
|
|
|
|
|
@defmac noreturn form
|
|
|
|
Evaluate @var{form}, informing coverage testing that @var{form} should
|
|
|
|
never return. If it ever does return, you get a run-time error.
|
|
|
|
@end defmac
|
|
|
|
|
|
|
|
Edebug also has a coverage testing feature (@pxref{Coverage
|
|
|
|
Testing}). These features partly duplicate each other, and it would
|
|
|
|
be cleaner to combine them.
|
2012-11-20 00:02:54 -08:00
|
|
|
|
|
|
|
|
|
|
|
@node Profiling
|
|
|
|
@section Profiling
|
|
|
|
@cindex profiling
|
2015-05-16 17:30:50 +03:00
|
|
|
@cindex profile
|
2018-02-17 11:16:35 +02:00
|
|
|
@cindex performance analysis
|
2012-11-20 00:02:54 -08:00
|
|
|
@cindex measuring resource usage
|
|
|
|
@cindex memory usage
|
|
|
|
|
2018-02-17 11:59:36 +02:00
|
|
|
If your program is working correctly, but not fast enough, and you
|
|
|
|
want to make it run more quickly or efficiently, the first thing to do
|
|
|
|
is @dfn{profile} your code so that you know where it spends most of
|
|
|
|
the execution time. If you find that one particular function is
|
|
|
|
responsible for a significant portion of the execution time, you can
|
|
|
|
start looking for ways to optimize that piece.
|
2012-11-20 00:02:54 -08:00
|
|
|
|
2018-08-28 07:49:49 -04:00
|
|
|
@findex profiler-start
|
|
|
|
@findex profiler-report
|
|
|
|
@findex profiler-stop
|
2012-11-20 00:02:54 -08:00
|
|
|
Emacs has built-in support for this. To begin profiling, type
|
2021-06-19 15:39:11 +03:00
|
|
|
@w{@kbd{M-x profiler-start}}. You can choose to sample CPU usage
|
|
|
|
periodically (@code{cpu}), when memory is allocated (@code{memory}),
|
|
|
|
or both. Then run the code you'd like to speed up. After that, type
|
|
|
|
@kbd{M-x profiler-report} to display a summary buffer for CPU usage
|
|
|
|
sampled by each type (cpu and memory) that you chose to profile. The
|
|
|
|
names of the report buffers include the times at which the reports
|
2018-02-17 11:59:36 +02:00
|
|
|
were generated, so you can generate another report later on without
|
|
|
|
erasing previous results. When you have finished profiling, type
|
|
|
|
@kbd{M-x profiler-stop} (there is a small overhead associated with
|
|
|
|
profiling, so we don't recommend leaving it active except when you are
|
|
|
|
actually running the code you want to examine).
|
2012-11-20 00:02:54 -08:00
|
|
|
|
2012-11-20 20:52:03 -05:00
|
|
|
The profiler report buffer shows, on each line, a function that was
|
2021-06-19 08:17:47 -07:00
|
|
|
called, preceded by how much CPU resources it used in
|
2018-02-17 11:59:36 +02:00
|
|
|
absolute and percentage terms since profiling started. If a given
|
2020-12-22 12:06:21 +00:00
|
|
|
line has a @samp{+} symbol to the left of the function name, you can
|
|
|
|
expand that line by typing @kbd{@key{RET}}, in order to see the
|
|
|
|
function(s) called by the higher-level function. Use a prefix
|
|
|
|
argument (@kbd{C-u @key{RET}}) to see the whole call tree below a
|
|
|
|
function. Pressing @kbd{@key{RET}} again will collapse back to the
|
|
|
|
original state.
|
2012-11-20 20:52:03 -05:00
|
|
|
|
2024-01-24 21:34:16 +02:00
|
|
|
@findex profiler-report-find-entry
|
|
|
|
@findex profiler-report-describe-entry
|
|
|
|
@findex profiler-find-profile
|
|
|
|
@findex profiler-find-profile-other-window
|
|
|
|
@findex profiler-report-compare-profile
|
|
|
|
Press @kbd{j} (@code{profiler-report-find-entry}) or @kbd{mouse-2} to
|
|
|
|
jump to the definition of a function at point. Press @kbd{d}
|
|
|
|
(@code{profiler-report-describe-entry}) to view a function's
|
|
|
|
documentation. You can save a profile to a file using @kbd{C-x C-w}
|
|
|
|
(@code{profiler-report-write-profile}) and read a saved profile with
|
|
|
|
@w{@kbd{M-x profiler-find-profile}} or @w{@kbd{M-x
|
|
|
|
profiler-find-profile-other-window}}. You can compare two profiles
|
|
|
|
using @kbd{=} (@code{profiler-report-compare-profile}).
|
2012-11-20 00:02:54 -08:00
|
|
|
|
2012-11-20 20:52:03 -05:00
|
|
|
@c FIXME reversed calltree?
|
2012-11-20 00:02:54 -08:00
|
|
|
|
|
|
|
@cindex @file{elp.el}
|
|
|
|
@cindex timing programs
|
2018-02-17 11:59:36 +02:00
|
|
|
The @file{elp} library offers an alternative approach, which is useful
|
|
|
|
when you know in advance which Lisp function(s) you want to profile.
|
|
|
|
Using that library, you begin by setting @code{elp-function-list} to
|
|
|
|
the list of function symbols---those are the functions you want to
|
|
|
|
profile. Then type @w{@kbd{M-x elp-instrument-list @key{RET} nil
|
|
|
|
@key{RET}}} to arrange for profiling those functions. After running
|
|
|
|
the code you want to profile, invoke @w{@kbd{M-x elp-results}} to
|
|
|
|
display the current results. See the file @file{elp.el} for more
|
|
|
|
detailed instructions. This approach is limited to profiling
|
|
|
|
functions written in Lisp, it cannot profile Emacs primitives.
|
2012-11-20 00:02:54 -08:00
|
|
|
|
|
|
|
@cindex @file{benchmark.el}
|
|
|
|
@cindex benchmarking
|
2018-02-17 11:59:36 +02:00
|
|
|
You can measure the time it takes to evaluate individual Emacs Lisp
|
2021-03-17 19:04:28 -04:00
|
|
|
forms using the @file{benchmark} library. See the function
|
2021-09-13 12:08:58 +02:00
|
|
|
@code{benchmark-call} as well as the macros @code{benchmark-run},
|
2023-07-04 17:21:54 +02:00
|
|
|
@code{benchmark-run-compiled}, and @code{benchmark-progn} in
|
|
|
|
@file{benchmark.el}. You can also use the @code{benchmark} command
|
|
|
|
for timing forms interactively.
|
2012-11-20 20:32:09 -05:00
|
|
|
|
|
|
|
@c Not worth putting in the printed manual.
|
|
|
|
@ifnottex
|
|
|
|
@cindex --enable-profiling option of configure
|
2012-11-21 09:14:42 -05:00
|
|
|
To profile Emacs at the level of its C code, you can build it using the
|
2012-11-20 20:32:09 -05:00
|
|
|
@option{--enable-profiling} option of @command{configure}. When Emacs
|
|
|
|
exits, it generates a file @file{gmon.out} that you can examine using
|
|
|
|
the @command{gprof} utility. This feature is mainly useful for
|
|
|
|
debugging Emacs. It actually stops the Lisp-level @kbd{M-x
|
|
|
|
profiler-@dots{}} commands described above from working.
|
|
|
|
@end ifnottex
|