* lisp/image.el (insert-image, insert-sliced-image): Doc fix.
[emacs.git] / doc / lispref / debugging.texi
blob11532b19781e108f41696e8f6bf4fe3d19d973c7
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990-1994, 1998-1999, 2001-2012 Free Software Foundation, Inc.
4 @c See the file elisp.texi for copying conditions.
5 @node Debugging
6 @chapter Debugging Lisp Programs
8   There are several ways to find and investigate problems in an Emacs
9 Lisp program.
11 @itemize @bullet
12 @item
13 If a problem occurs when you run the program, you can use the built-in
14 Emacs Lisp debugger to suspend the Lisp evaluator, and examine and/or
15 alter its internal state.
17 @item
18 You can use Edebug, a source-level debugger for Emacs Lisp.
20 @item
21 If a syntactic problem is preventing Lisp from even reading the
22 program, you can locate it using Lisp editing commands.
24 @item
25 You can look at the error and warning messages produced by the byte
26 compiler when it compiles the program.  @xref{Compiler Errors}.
28 @item
29 You can use the Testcover package to perform coverage testing on the
30 program.
32 @item
33 You can use the ERT package to write regression tests for the program.
34 @xref{Top,the ERT manual,, ERT, ERT: Emacs Lisp Regression Testing}.
35 @end itemize
37   Other useful tools for debugging input and output problems are the
38 dribble file (@pxref{Terminal Input}) and the @code{open-termscript}
39 function (@pxref{Terminal Output}).
41 @menu
42 * Debugger::            A debugger for the Emacs Lisp evaluator.
43 * Edebug::              A source-level Emacs Lisp debugger.
44 * Syntax Errors::       How to find syntax errors.
45 * Test Coverage::       Ensuring you have tested all branches in your code.
46 @end menu
48 @node Debugger
49 @section The Lisp Debugger
50 @cindex debugger for Emacs Lisp
51 @cindex Lisp debugger
52 @cindex break
54   The ordinary @dfn{Lisp debugger} provides the ability to suspend
55 evaluation of a form.  While evaluation is suspended (a state that is
56 commonly known as a @dfn{break}), you may examine the run time stack,
57 examine the values of local or global variables, or change those values.
58 Since a break is a recursive edit, all the usual editing facilities of
59 Emacs are available; you can even run programs that will enter the
60 debugger recursively.  @xref{Recursive Editing}.
62 @menu
63 * Error Debugging::       Entering the debugger when an error happens.
64 * Infinite Loops::        Stopping and debugging a program that doesn't exit.
65 * Function Debugging::    Entering it when a certain function is called.
66 * Explicit Debug::        Entering it at a certain point in the program.
67 * Using Debugger::        What the debugger does; what you see while in it.
68 * Debugger Commands::     Commands used while in the debugger.
69 * Invoking the Debugger:: How to call the function @code{debug}.
70 * Internals of Debugger:: Subroutines of the debugger, and global variables.
71 @end menu
73 @node Error Debugging
74 @subsection Entering the Debugger on an Error
75 @cindex error debugging
76 @cindex debugging errors
78   The most important time to enter the debugger is when a Lisp error
79 happens.  This allows you to investigate the immediate causes of the
80 error.
82   However, entry to the debugger is not a normal consequence of an
83 error.  Many commands signal Lisp errors when invoked inappropriately,
84 and during ordinary editing it would be very inconvenient to enter the
85 debugger each time this happens.  So if you want errors to enter the
86 debugger, set the variable @code{debug-on-error} to non-@code{nil}.
87 (The command @code{toggle-debug-on-error} provides an easy way to do
88 this.)
90 @defopt debug-on-error
91 This variable determines whether the debugger is called when an error
92 is signaled and not handled.  If @code{debug-on-error} is @code{t},
93 all kinds of errors call the debugger, except those listed in
94 @code{debug-ignored-errors} (see below).  If it is @code{nil}, none
95 call the debugger.
97 The value can also be a list of error conditions (@pxref{Signaling
98 Errors}).  Then the debugger is called only for error conditions in
99 this list (except those also listed in @code{debug-ignored-errors}).
100 For example, if you set @code{debug-on-error} to the list
101 @code{(void-variable)}, the debugger is only called for errors about a
102 variable that has no value.
104 Note that @code{eval-expression-debug-on-error} overrides this
105 variable in some cases; see below.
107 When this variable is non-@code{nil}, Emacs does not create an error
108 handler around process filter functions and sentinels.  Therefore,
109 errors in these functions also invoke the debugger.  @xref{Processes}.
110 @end defopt
112 @defopt debug-ignored-errors
113 This variable specifies errors which should not enter the debugger,
114 regardless of the value of @code{debug-on-error}.  Its value is a list
115 of error condition symbols and/or regular expressions.  If the error
116 has any of those condition symbols, or if the error message matches
117 any of the regular expressions, then that error does not enter the
118 debugger.
120 The normal value of this variable includes @code{user-error}, as well
121 as several errors that happen often during editing but rarely result
122 from bugs in Lisp programs.  However, ``rarely'' is not ``never''; if
123 your program fails with an error that matches this list, you may try
124 changing this list to debug the error.  The easiest way is usually to
125 set @code{debug-ignored-errors} to @code{nil}.
126 @end defopt
128 @defopt eval-expression-debug-on-error
129 If this variable has a non-@code{nil} value (the default), running the
130 command @code{eval-expression} causes @code{debug-on-error} to be
131 temporarily bound to to @code{t}.  @xref{Lisp Eval,, Evaluating
132 Emacs-Lisp Expressions, emacs, The GNU Emacs Manual}.
134 If @code{eval-expression-debug-on-error} is @code{nil}, then the value
135 of @code{debug-on-error} is not changed during @code{eval-expression}.
136 @end defopt
138 @defvar debug-on-signal
139 Normally, errors caught by @code{condition-case} never invoke the
140 debugger.  The @code{condition-case} gets a chance to handle the error
141 before the debugger gets a chance.
143 If you change @code{debug-on-signal} to a non-@code{nil} value, the
144 debugger gets the first chance at every error, regardless of the
145 presence of @code{condition-case}.  (To invoke the debugger, the error
146 must still fulfill the criteria specified by @code{debug-on-error} and
147 @code{debug-ignored-errors}.)
149 @strong{Warning:} Setting this variable to non-@code{nil} may have
150 annoying effects.  Various parts of Emacs catch errors in the normal
151 course of affairs, and you may not even realize that errors happen
152 there.  If you need to debug code wrapped in @code{condition-case},
153 consider using @code{condition-case-unless-debug} (@pxref{Handling
154 Errors}).
155 @end defvar
157 @defopt debug-on-event
158 If you set @code{debug-on-event} to a special event (@pxref{Special
159 Events}), Emacs will try to enter the debugger as soon as it receives
160 this event, bypassing @code{special-event-map}.  At present, the only
161 supported values correspond to the signals @code{SIGUSR1} and
162 @code{SIGUSR2} (this is the default).  This can be helpful when
163 @code{inhibit-quit} is set and Emacs is not otherwise responding.
164 @end defopt
166 @cindex message, finding what causes a particular message
167 @defvar debug-on-message
168 If you set @code{debug-on-message} to a regular expression,
169 Emacs will enter the debugger if it displays a matching message in the
170 echo area.  For example, this can be useful when trying to find the
171 cause of a particular message.
172 @end defvar
174   To debug an error that happens during loading of the init
175 file, use the option @samp{--debug-init}.  This binds
176 @code{debug-on-error} to @code{t} while loading the init file, and
177 bypasses the @code{condition-case} which normally catches errors in the
178 init file.
180 @node Infinite Loops
181 @subsection Debugging Infinite Loops
182 @cindex infinite loops
183 @cindex loops, infinite
184 @cindex quitting from infinite loop
185 @cindex stopping an infinite loop
187   When a program loops infinitely and fails to return, your first
188 problem is to stop the loop.  On most operating systems, you can do
189 this with @kbd{C-g}, which causes a @dfn{quit}.  @xref{Quitting}.
191   Ordinary quitting gives no information about why the program was
192 looping.  To get more information, you can set the variable
193 @code{debug-on-quit} to non-@code{nil}.  Once you have the debugger
194 running in the middle of the infinite loop, you can proceed from the
195 debugger using the stepping commands.  If you step through the entire
196 loop, you may get enough information to solve the problem.
198   Quitting with @kbd{C-g} is not considered an error, and
199 @code{debug-on-error} has no effect on the handling of @kbd{C-g}.
200 Likewise, @code{debug-on-quit} has no effect on errors.
202 @defopt debug-on-quit
203 This variable determines whether the debugger is called when
204 @code{quit} is signaled and not handled.  If @code{debug-on-quit} is
205 non-@code{nil}, then the debugger is called whenever you quit (that
206 is, type @kbd{C-g}).  If @code{debug-on-quit} is @code{nil} (the
207 default), then the debugger is not called when you quit.
208 @end defopt
210 @node Function Debugging
211 @subsection Entering the Debugger on a Function Call
212 @cindex function call debugging
213 @cindex debugging specific functions
215   To investigate a problem that happens in the middle of a program, one
216 useful technique is to enter the debugger whenever a certain function is
217 called.  You can do this to the function in which the problem occurs,
218 and then step through the function, or you can do this to a function
219 called shortly before the problem, step quickly over the call to that
220 function, and then step through its caller.
222 @deffn Command debug-on-entry function-name
223 This function requests @var{function-name} to invoke the debugger each
224 time it is called.  It works by inserting the form
225 @code{(implement-debug-on-entry)} into the function definition as the
226 first form.
228 Any function or macro defined as Lisp code may be set to break on
229 entry, regardless of whether it is interpreted code or compiled code.
230 If the function is a command, it will enter the debugger when called
231 from Lisp and when called interactively (after the reading of the
232 arguments).  You can also set debug-on-entry for primitive functions
233 (i.e., those written in C) this way, but it only takes effect when the
234 primitive is called from Lisp code.  Debug-on-entry is not allowed for
235 special forms.
237 When @code{debug-on-entry} is called interactively, it prompts for
238 @var{function-name} in the minibuffer.  If the function is already set
239 up to invoke the debugger on entry, @code{debug-on-entry} does nothing.
240 @code{debug-on-entry} always returns @var{function-name}.
242 @strong{Warning:} if you redefine a function after using
243 @code{debug-on-entry} on it, the code to enter the debugger is
244 discarded by the redefinition.  In effect, redefining the function
245 cancels the break-on-entry feature for that function.
247 Here's an example to illustrate use of this function:
249 @example
250 @group
251 (defun fact (n)
252   (if (zerop n) 1
253       (* n (fact (1- n)))))
254      @result{} fact
255 @end group
256 @group
257 (debug-on-entry 'fact)
258      @result{} fact
259 @end group
260 @group
261 (fact 3)
262 @end group
264 @group
265 ------ Buffer: *Backtrace* ------
266 Debugger entered--entering a function:
267 * fact(3)
268   eval((fact 3))
269   eval-last-sexp-1(nil)
270   eval-last-sexp(nil)
271   call-interactively(eval-last-sexp)
272 ------ Buffer: *Backtrace* ------
273 @end group
275 @group
276 (symbol-function 'fact)
277      @result{} (lambda (n)
278           (debug (quote debug))
279           (if (zerop n) 1 (* n (fact (1- n)))))
280 @end group
281 @end example
282 @end deffn
284 @deffn Command cancel-debug-on-entry &optional function-name
285 This function undoes the effect of @code{debug-on-entry} on
286 @var{function-name}.  When called interactively, it prompts for
287 @var{function-name} in the minibuffer.  If @var{function-name} is
288 omitted or @code{nil}, it cancels break-on-entry for all functions.
289 Calling @code{cancel-debug-on-entry} does nothing to a function which is
290 not currently set up to break on entry.
291 @end deffn
293 @node Explicit Debug
294 @subsection Explicit Entry to the Debugger
296   You can cause the debugger to be called at a certain point in your
297 program by writing the expression @code{(debug)} at that point.  To do
298 this, visit the source file, insert the text @samp{(debug)} at the
299 proper place, and type @kbd{C-M-x} (@code{eval-defun}, a Lisp mode key
300 binding).  @strong{Warning:} if you do this for temporary debugging
301 purposes, be sure to undo this insertion before you save the file!
303   The place where you insert @samp{(debug)} must be a place where an
304 additional form can be evaluated and its value ignored.  (If the value
305 of @code{(debug)} isn't ignored, it will alter the execution of the
306 program!)  The most common suitable places are inside a @code{progn} or
307 an implicit @code{progn} (@pxref{Sequencing}).
309   If you don't know exactly where in the source code you want to put
310 the debug statement, but you want to display a backtrace when a
311 certain message is displayed, you can set @code{debug-on-message} to a
312 regular expression matching the desired message.
314 @node Using Debugger
315 @subsection Using the Debugger
317   When the debugger is entered, it displays the previously selected
318 buffer in one window and a buffer named @file{*Backtrace*} in another
319 window.  The backtrace buffer contains one line for each level of Lisp
320 function execution currently going on.  At the beginning of this buffer
321 is a message describing the reason that the debugger was invoked (such
322 as the error message and associated data, if it was invoked due to an
323 error).
325 @vindex debugger-bury-or-kill
326   The backtrace buffer is read-only and uses a special major mode,
327 Debugger mode, in which letters are defined as debugger commands.  The
328 usual Emacs editing commands are available; thus, you can switch windows
329 to examine the buffer that was being edited at the time of the error,
330 switch buffers, visit files, or do any other sort of editing.  However,
331 the debugger is a recursive editing level (@pxref{Recursive Editing})
332 and it is wise to go back to the backtrace buffer and exit the debugger
333 (with the @kbd{q} command) when you are finished with it.  Exiting
334 the debugger gets out of the recursive edit and buries the backtrace
335 buffer.  (You can customize what the @kbd{q} command does with the
336 backtrace buffer by setting the variable @code{debugger-bury-or-kill}.
337 For example, set it to @code{kill} if you prefer to kill the buffer
338 rather than bury it.  Consult the variable's documentation for more
339 possibilities.)
341   When the debugger has been entered, the @code{debug-on-error}
342 variable is temporarily set according to
343 @code{eval-expression-debug-on-error}.  If the latter variable is
344 non-@code{nil}, @code{debug-on-error} will temporarily be set to
345 @code{t}.  This means that any further errors that occur while doing a
346 debugging session will (by default) trigger another backtrace.  If
347 this is not what you want, you can either set
348 @code{eval-expression-debug-on-error} to @code{nil}, or set
349 @code{debug-on-error} to @code{nil} in @code{debugger-mode-hook}.
351 @cindex current stack frame
352   The backtrace buffer shows you the functions that are executing and
353 their argument values.  It also allows you to specify a stack frame by
354 moving point to the line describing that frame.  (A stack frame is the
355 place where the Lisp interpreter records information about a particular
356 invocation of a function.)  The frame whose line point is on is
357 considered the @dfn{current frame}.  Some of the debugger commands
358 operate on the current frame.  If a line starts with a star, that means
359 that exiting that frame will call the debugger again.  This is useful
360 for examining the return value of a function.
362   If a function name is underlined, that means the debugger knows
363 where its source code is located.  You can click with the mouse on
364 that name, or move to it and type @key{RET}, to visit the source code.
366   The debugger itself must be run byte-compiled, since it makes
367 assumptions about how many stack frames are used for the debugger
368 itself.  These assumptions are false if the debugger is running
369 interpreted.
371 @node Debugger Commands
372 @subsection Debugger Commands
373 @cindex debugger command list
375   The debugger buffer (in Debugger mode) provides special commands in
376 addition to the usual Emacs commands.  The most important use of
377 debugger commands is for stepping through code, so that you can see
378 how control flows.  The debugger can step through the control
379 structures of an interpreted function, but cannot do so in a
380 byte-compiled function.  If you would like to step through a
381 byte-compiled function, replace it with an interpreted definition of
382 the same function.  (To do this, visit the source for the function and
383 type @kbd{C-M-x} on its definition.)  You cannot use the Lisp debugger
384 to step through a primitive function.
386   Here is a list of Debugger mode commands:
388 @table @kbd
389 @item c
390 Exit the debugger and continue execution.  This resumes execution of
391 the program as if the debugger had never been entered (aside from any
392 side-effects that you caused by changing variable values or data
393 structures while inside the debugger).
395 @item d
396 Continue execution, but enter the debugger the next time any Lisp
397 function is called.  This allows you to step through the
398 subexpressions of an expression, seeing what values the subexpressions
399 compute, and what else they do.
401 The stack frame made for the function call which enters the debugger in
402 this way will be flagged automatically so that the debugger will be
403 called again when the frame is exited.  You can use the @kbd{u} command
404 to cancel this flag.
406 @item b
407 Flag the current frame so that the debugger will be entered when the
408 frame is exited.  Frames flagged in this way are marked with stars
409 in the backtrace buffer.
411 @item u
412 Don't enter the debugger when the current frame is exited.  This
413 cancels a @kbd{b} command on that frame.  The visible effect is to
414 remove the star from the line in the backtrace buffer.
416 @item j
417 Flag the current frame like @kbd{b}.  Then continue execution like
418 @kbd{c}, but temporarily disable break-on-entry for all functions that
419 are set up to do so by @code{debug-on-entry}.
421 @item e
422 Read a Lisp expression in the minibuffer, evaluate it, and print the
423 value in the echo area.  The debugger alters certain important
424 variables, and the current buffer, as part of its operation; @kbd{e}
425 temporarily restores their values from outside the debugger, so you can
426 examine and change them.  This makes the debugger more transparent.  By
427 contrast, @kbd{M-:} does nothing special in the debugger; it shows you
428 the variable values within the debugger.
430 @item R
431 Like @kbd{e}, but also save the result of evaluation in the
432 buffer @file{*Debugger-record*}.
434 @item q
435 Terminate the program being debugged; return to top-level Emacs
436 command execution.
438 If the debugger was entered due to a @kbd{C-g} but you really want
439 to quit, and not debug, use the @kbd{q} command.
441 @item r
442 Return a value from the debugger.  The value is computed by reading an
443 expression with the minibuffer and evaluating it.
445 The @kbd{r} command is useful when the debugger was invoked due to exit
446 from a Lisp call frame (as requested with @kbd{b} or by entering the
447 frame with @kbd{d}); then the value specified in the @kbd{r} command is
448 used as the value of that frame.  It is also useful if you call
449 @code{debug} and use its return value.  Otherwise, @kbd{r} has the same
450 effect as @kbd{c}, and the specified return value does not matter.
452 You can't use @kbd{r} when the debugger was entered due to an error.
454 @item l
455 Display a list of functions that will invoke the debugger when called.
456 This is a list of functions that are set to break on entry by means of
457 @code{debug-on-entry}.  @strong{Warning:} if you redefine such a
458 function and thus cancel the effect of @code{debug-on-entry}, it may
459 erroneously show up in this list.
460 @end table
462 @node Invoking the Debugger
463 @subsection Invoking the Debugger
465   Here we describe in full detail the function @code{debug} that is used
466 to invoke the debugger.
468 @deffn Command debug &rest debugger-args
469 This function enters the debugger.  It switches buffers to a buffer
470 named @file{*Backtrace*} (or @file{*Backtrace*<2>} if it is the second
471 recursive entry to the debugger, etc.), and fills it with information
472 about the stack of Lisp function calls.  It then enters a recursive
473 edit, showing the backtrace buffer in Debugger mode.
475 The Debugger mode @kbd{c}, @kbd{d}, @kbd{j}, and @kbd{r} commands exit
476 the recursive edit; then @code{debug} switches back to the previous
477 buffer and returns to whatever called @code{debug}.  This is the only
478 way the function @code{debug} can return to its caller.
480 The use of the @var{debugger-args} is that @code{debug} displays the
481 rest of its arguments at the top of the @file{*Backtrace*} buffer, so
482 that the user can see them.  Except as described below, this is the
483 @emph{only} way these arguments are used.
485 However, certain values for first argument to @code{debug} have a
486 special significance.  (Normally, these values are used only by the
487 internals of Emacs, and not by programmers calling @code{debug}.)  Here
488 is a table of these special values:
490 @table @code
491 @item lambda
492 @cindex @code{lambda} in debug
493 A first argument of @code{lambda} means @code{debug} was called
494 because of entry to a function when @code{debug-on-next-call} was
495 non-@code{nil}.  The debugger displays @samp{Debugger
496 entered--entering a function:} as a line of text at the top of the
497 buffer.
499 @item debug
500 @code{debug} as first argument means @code{debug} was called because
501 of entry to a function that was set to debug on entry.  The debugger
502 displays the string @samp{Debugger entered--entering a function:},
503 just as in the @code{lambda} case.  It also marks the stack frame for
504 that function so that it will invoke the debugger when exited.
506 @item t
507 When the first argument is @code{t}, this indicates a call to
508 @code{debug} due to evaluation of a function call form when
509 @code{debug-on-next-call} is non-@code{nil}.  The debugger displays
510 @samp{Debugger entered--beginning evaluation of function call form:}
511 as the top line in the buffer.
513 @item exit
514 When the first argument is @code{exit}, it indicates the exit of a
515 stack frame previously marked to invoke the debugger on exit.  The
516 second argument given to @code{debug} in this case is the value being
517 returned from the frame.  The debugger displays @samp{Debugger
518 entered--returning value:} in the top line of the buffer, followed by
519 the value being returned.
521 @item error
522 @cindex @code{error} in debug
523 When the first argument is @code{error}, the debugger indicates that
524 it is being entered because an error or @code{quit} was signaled and
525 not handled, by displaying @samp{Debugger entered--Lisp error:}
526 followed by the error signaled and any arguments to @code{signal}.
527 For example,
529 @example
530 @group
531 (let ((debug-on-error t))
532   (/ 1 0))
533 @end group
535 @group
536 ------ Buffer: *Backtrace* ------
537 Debugger entered--Lisp error: (arith-error)
538   /(1 0)
540 ------ Buffer: *Backtrace* ------
541 @end group
542 @end example
544 If an error was signaled, presumably the variable
545 @code{debug-on-error} is non-@code{nil}.  If @code{quit} was signaled,
546 then presumably the variable @code{debug-on-quit} is non-@code{nil}.
548 @item nil
549 Use @code{nil} as the first of the @var{debugger-args} when you want
550 to enter the debugger explicitly.  The rest of the @var{debugger-args}
551 are printed on the top line of the buffer.  You can use this feature to
552 display messages---for example, to remind yourself of the conditions
553 under which @code{debug} is called.
554 @end table
555 @end deffn
557 @node Internals of Debugger
558 @subsection Internals of the Debugger
560   This section describes functions and variables used internally by the
561 debugger.
563 @defvar debugger
564 The value of this variable is the function to call to invoke the
565 debugger.  Its value must be a function of any number of arguments, or,
566 more typically, the name of a function.  This function should invoke
567 some kind of debugger.  The default value of the variable is
568 @code{debug}.
570 The first argument that Lisp hands to the function indicates why it
571 was called.  The convention for arguments is detailed in the description
572 of @code{debug} (@pxref{Invoking the Debugger}).
573 @end defvar
575 @deffn Command backtrace
576 @cindex run time stack
577 @cindex call stack
578 This function prints a trace of Lisp function calls currently active.
579 This is the function used by @code{debug} to fill up the
580 @file{*Backtrace*} buffer.  It is written in C, since it must have access
581 to the stack to determine which function calls are active.  The return
582 value is always @code{nil}.
584 In the following example, a Lisp expression calls @code{backtrace}
585 explicitly.  This prints the backtrace to the stream
586 @code{standard-output}, which, in this case, is the buffer
587 @samp{backtrace-output}.
589 Each line of the backtrace represents one function call.  The line shows
590 the values of the function's arguments if they are all known; if they
591 are still being computed, the line says so.  The arguments of special
592 forms are elided.
594 @smallexample
595 @group
596 (with-output-to-temp-buffer "backtrace-output"
597   (let ((var 1))
598     (save-excursion
599       (setq var (eval '(progn
600                          (1+ var)
601                          (list 'testing (backtrace))))))))
603      @result{} (testing nil)
604 @end group
606 @group
607 ----------- Buffer: backtrace-output ------------
608   backtrace()
609   (list ...computing arguments...)
610 @end group
611   (progn ...)
612   eval((progn (1+ var) (list (quote testing) (backtrace))))
613   (setq ...)
614   (save-excursion ...)
615   (let ...)
616   (with-output-to-temp-buffer ...)
617   eval((with-output-to-temp-buffer ...))
618   eval-last-sexp-1(nil)
619 @group
620   eval-last-sexp(nil)
621   call-interactively(eval-last-sexp)
622 ----------- Buffer: backtrace-output ------------
623 @end group
624 @end smallexample
625 @end deffn
627 @defvar debug-on-next-call
628 @cindex @code{eval}, and debugging
629 @cindex @code{apply}, and debugging
630 @cindex @code{funcall}, and debugging
631 If this variable is non-@code{nil}, it says to call the debugger before
632 the next @code{eval}, @code{apply} or @code{funcall}.  Entering the
633 debugger sets @code{debug-on-next-call} to @code{nil}.
635 The @kbd{d} command in the debugger works by setting this variable.
636 @end defvar
638 @defun backtrace-debug level flag
639 This function sets the debug-on-exit flag of the stack frame @var{level}
640 levels down the stack, giving it the value @var{flag}.  If @var{flag} is
641 non-@code{nil}, this will cause the debugger to be entered when that
642 frame later exits.  Even a nonlocal exit through that frame will enter
643 the debugger.
645 This function is used only by the debugger.
646 @end defun
648 @defvar command-debug-status
649 This variable records the debugging status of the current interactive
650 command.  Each time a command is called interactively, this variable is
651 bound to @code{nil}.  The debugger can set this variable to leave
652 information for future debugger invocations during the same command
653 invocation.
655 The advantage of using this variable rather than an ordinary global
656 variable is that the data will never carry over to a subsequent command
657 invocation.
658 @end defvar
660 @defun backtrace-frame frame-number
661 The function @code{backtrace-frame} is intended for use in Lisp
662 debuggers.  It returns information about what computation is happening
663 in the stack frame @var{frame-number} levels down.
665 If that frame has not evaluated the arguments yet, or is a special
666 form, the value is @code{(nil @var{function} @var{arg-forms}@dots{})}.
668 If that frame has evaluated its arguments and called its function
669 already, the return value is @code{(t @var{function}
670 @var{arg-values}@dots{})}.
672 In the return value, @var{function} is whatever was supplied as the
673 @sc{car} of the evaluated list, or a @code{lambda} expression in the
674 case of a macro call.  If the function has a @code{&rest} argument, that
675 is represented as the tail of the list @var{arg-values}.
677 If @var{frame-number} is out of range, @code{backtrace-frame} returns
678 @code{nil}.
679 @end defun
681 @include edebug.texi
683 @node Syntax Errors
684 @section Debugging Invalid Lisp Syntax
685 @cindex debugging invalid Lisp syntax
687   The Lisp reader reports invalid syntax, but cannot say where the real
688 problem is.  For example, the error ``End of file during parsing'' in
689 evaluating an expression indicates an excess of open parentheses (or
690 square brackets).  The reader detects this imbalance at the end of the
691 file, but it cannot figure out where the close parenthesis should have
692 been.  Likewise, ``Invalid read syntax: ")"'' indicates an excess close
693 parenthesis or missing open parenthesis, but does not say where the
694 missing parenthesis belongs.  How, then, to find what to change?
696   If the problem is not simply an imbalance of parentheses, a useful
697 technique is to try @kbd{C-M-e} at the beginning of each defun, and see
698 if it goes to the place where that defun appears to end.  If it does
699 not, there is a problem in that defun.
701 @cindex unbalanced parentheses
702 @cindex parenthesis mismatch, debugging
703   However, unmatched parentheses are the most common syntax errors in
704 Lisp, and we can give further advice for those cases.  (In addition,
705 just moving point through the code with Show Paren mode enabled might
706 find the mismatch.)
708 @menu
709 * Excess Open::     How to find a spurious open paren or missing close.
710 * Excess Close::    How to find a spurious close paren or missing open.
711 @end menu
713 @node Excess Open
714 @subsection Excess Open Parentheses
716   The first step is to find the defun that is unbalanced.  If there is
717 an excess open parenthesis, the way to do this is to go to the end of
718 the file and type @kbd{C-u C-M-u}.  This will move you to the
719 beginning of the first defun that is unbalanced.
721   The next step is to determine precisely what is wrong.  There is no
722 way to be sure of this except by studying the program, but often the
723 existing indentation is a clue to where the parentheses should have
724 been.  The easiest way to use this clue is to reindent with @kbd{C-M-q}
725 and see what moves.  @strong{But don't do this yet!}  Keep reading,
726 first.
728   Before you do this, make sure the defun has enough close parentheses.
729 Otherwise, @kbd{C-M-q} will get an error, or will reindent all the rest
730 of the file until the end.  So move to the end of the defun and insert a
731 close parenthesis there.  Don't use @kbd{C-M-e} to move there, since
732 that too will fail to work until the defun is balanced.
734   Now you can go to the beginning of the defun and type @kbd{C-M-q}.
735 Usually all the lines from a certain point to the end of the function
736 will shift to the right.  There is probably a missing close parenthesis,
737 or a superfluous open parenthesis, near that point.  (However, don't
738 assume this is true; study the code to make sure.)  Once you have found
739 the discrepancy, undo the @kbd{C-M-q} with @kbd{C-_}, since the old
740 indentation is probably appropriate to the intended parentheses.
742   After you think you have fixed the problem, use @kbd{C-M-q} again.  If
743 the old indentation actually fit the intended nesting of parentheses,
744 and you have put back those parentheses, @kbd{C-M-q} should not change
745 anything.
747 @node Excess Close
748 @subsection Excess Close Parentheses
750   To deal with an excess close parenthesis, first go to the beginning
751 of the file, then type @kbd{C-u -1 C-M-u} to find the end of the first
752 unbalanced defun.
754   Then find the actual matching close parenthesis by typing @kbd{C-M-f}
755 at the beginning of that defun.  This will leave you somewhere short of
756 the place where the defun ought to end.  It is possible that you will
757 find a spurious close parenthesis in that vicinity.
759   If you don't see a problem at that point, the next thing to do is to
760 type @kbd{C-M-q} at the beginning of the defun.  A range of lines will
761 probably shift left; if so, the missing open parenthesis or spurious
762 close parenthesis is probably near the first of those lines.  (However,
763 don't assume this is true; study the code to make sure.)  Once you have
764 found the discrepancy, undo the @kbd{C-M-q} with @kbd{C-_}, since the
765 old indentation is probably appropriate to the intended parentheses.
767   After you think you have fixed the problem, use @kbd{C-M-q} again.  If
768 the old indentation actually fits the intended nesting of parentheses,
769 and you have put back those parentheses, @kbd{C-M-q} should not change
770 anything.
772 @node Test Coverage
773 @section Test Coverage
774 @cindex coverage testing
776 @findex testcover-start
777 @findex testcover-mark-all
778 @findex testcover-next-mark
779   You can do coverage testing for a file of Lisp code by loading the
780 @code{testcover} library and using the command @kbd{M-x
781 testcover-start @key{RET} @var{file} @key{RET}} to instrument the
782 code.  Then test your code by calling it one or more times.  Then use
783 the command @kbd{M-x testcover-mark-all} to display colored highlights
784 on the code to show where coverage is insufficient.  The command
785 @kbd{M-x testcover-next-mark} will move point forward to the next
786 highlighted spot.
788   Normally, a red highlight indicates the form was never completely
789 evaluated; a brown highlight means it always evaluated to the same
790 value (meaning there has been little testing of what is done with the
791 result).  However, the red highlight is skipped for forms that can't
792 possibly complete their evaluation, such as @code{error}.  The brown
793 highlight is skipped for forms that are expected to always evaluate to
794 the same value, such as @code{(setq x 14)}.
796   For difficult cases, you can add do-nothing macros to your code to
797 give advice to the test coverage tool.
799 @defmac 1value form
800 Evaluate @var{form} and return its value, but inform coverage testing
801 that @var{form}'s value should always be the same.
802 @end defmac
804 @defmac noreturn form
805 Evaluate @var{form}, informing coverage testing that @var{form} should
806 never return.  If it ever does return, you get a run-time error.
807 @end defmac
809   Edebug also has a coverage testing feature (@pxref{Coverage
810 Testing}).  These features partly duplicate each other, and it would
811 be cleaner to combine them.