(PC-do-completion): Remove text properties from
[bpt/emacs.git] / lispref / edebug.texi
CommitLineData
73804d4b
RS
1@comment -*-texinfo-*-
2
3@c This file is intended to be used as a section within the Emacs Lisp
4@c Reference Manual. It may also be used by an independent Edebug User
5@c Manual, edebug.tex, in which case the Edebug node below should be used
6@c with the following links to the Bugs section and to the top level:
7
8@c , Bugs and Todo List, Top, Top
9
87b2d5ff 10@node Edebug,, Compilation Errors, Debugging
73804d4b
RS
11@section Edebug
12@cindex Edebug mode
13
14@cindex Edebug
15 Edebug is a source-level debugger for Emacs Lisp programs with which
16you can:
17
18@itemize @bullet
19@item
20Step through evaluation, stopping before and after each expression.
21
22@item
23Set conditional or unconditional breakpoints.
24
25@item
26Stop when a specified condition is true (the global break event).
27
28@item
29Trace slow or fast, stopping briefly at each stop point, or
30at each breakpoint.
31
32@item
33Display expression results and evaluate expressions as if outside of
34Edebug.
35
36@item
37Automatically reevaluate a list of expressions and
38display their results each time Edebug updates the display.
39
40@item
41Output trace info on function enter and exit.
42
43@item
44Stop when an error occurs.
45
46@item
47Display a backtrace, omitting Edebug's own frames.
48
49@item
50Specify argument evaluation for macros and defining forms.
51
52@item
53Obtain rudimentary coverage testing and frequency counts.
54@end itemize
55
56The first three sections below should tell you enough about Edebug to
57enable you to use it.
58
59@menu
60* Using Edebug:: Introduction to use of Edebug.
61* Instrumenting:: You must instrument your code
62 in order to debug it with Edebug.
63* Modes: Edebug Execution Modes. Execution modes, stopping more or less often.
64* Jumping:: Commands to jump to a specified place.
65* Misc: Edebug Misc. Miscellaneous commands.
66* Breakpoints:: Setting breakpoints to make the program stop.
67* Trapping Errors:: trapping errors with Edebug.
68* Views: Edebug Views. Views inside and outside of Edebug.
69* Eval: Edebug Eval. Evaluating expressions within Edebug.
70* Eval List:: Expressions whose values are displayed
71 each time you enter Edebug.
72* Printing in Edebug:: Customization of printing.
73* Trace Buffer:: How to produce trace output in a buffer.
74* Coverage Testing:: How to test evaluation coverage.
75* The Outside Context:: Data that Edebug saves and restores.
76* Instrumenting Macro Calls:: Specifying how to handle macro calls.
77* Options: Edebug Options. Option variables for customizing Edebug.
78@end menu
79
80@node Using Edebug
81@subsection Using Edebug
82
83 To debug a Lisp program with Edebug, you must first @dfn{instrument}
84the Lisp code that you want to debug. A simple way to do this is to
85first move point into the definition of a function or macro and then do
86@kbd{C-u C-M-x} (@code{eval-defun} with a prefix argument). See
87@ref{Instrumenting}, for alternative ways to instrument code.
88
89 Once a function is instrumented, any call to the function activates
90Edebug. Activating Edebug may stop execution and let you step through
91the function, or it may update the display and continue execution while
92checking for debugging commands, depending on which Edebug execution
93mode you have selected. The default execution mode is step, which does
94stop execution. @xref{Edebug Execution Modes}.
95
96 Within Edebug, you normally view an Emacs buffer showing the source of
97the Lisp code you are debugging. This is referred to as the @dfn{source
98code buffer}. This buffer is temporarily read-only.
99
100 An arrow at the left margin indicates the line where the function is
101executing. Point initially shows where within the line the function is
102executing, but this ceases to be true if you move point yourself.
103
104 If you instrument the definition of @code{fac} (shown below) and then
105execute @code{(fac 3)}, here is what you normally see. Point is at the
106open-parenthesis before @code{if}.
107
108@example
109(defun fac (n)
110=>@point{}(if (< 0 n)
111 (* n (fac (1- n)))
112 1))
113@end example
114
115@cindex stop points
116The places within a function where Edebug can stop execution are called
117@dfn{stop points}. These occur both before and after each subexpression
118that is a list, and also after each variable reference.
119Here we show with periods the stop points found in the function
120@code{fac}:
121
122@example
123(defun fac (n)
124 .(if .(< 0 n.).
125 .(* n. .(fac (1- n.).).).
126 1).)
127@end example
128
129The special commands of Edebug are available in the source code buffer
130in addition to the commands of Emacs Lisp mode. For example, you can
131type the Edebug command @key{SPC} to execute until the next stop point.
132If you type @key{SPC} once after entry to @code{fac}, here is the
133display you will see:
134
135@example
136(defun fac (n)
137=>(if @point{}(< 0 n)
138 (* n (fac (1- n)))
139 1))
140@end example
141
142When Edebug stops execution after an expression, it displays the
143expression's value in the echo area.
144
145Other frequently used commands are @kbd{b} to set a breakpoint at a stop
146point, @kbd{g} to execute until a breakpoint is reached, and @kbd{q} to
147exit Edebug and return to the top-level command loop. Type @kbd{?} to
148display a list of all Edebug commands.
149
150@node Instrumenting
151@subsection Instrumenting for Edebug
152
153 In order to use Edebug to debug Lisp code, you must first
154@dfn{instrument} the code. Instrumenting code inserts additional code
87b2d5ff 155into it, to invoke Edebug at the proper places.
73804d4b
RS
156
157@kindex C-M-x
158@findex eval-defun (Edebug)
159 Once you have loaded Edebug, the command @kbd{C-M-x}
160(@code{eval-defun}) is redefined so that when invoked with a prefix
161argument on a definition, it instruments the definition before
162evaluating it. (The source code itself is not modified.) If the
163variable @code{edebug-all-defs} is non-@code{nil}, that inverts the
164meaning of the prefix argument: then @kbd{C-M-x} instruments the
165definition @emph{unless} it has a prefix argument. The default value of
166@code{edebug-all-defs} is @code{nil}. The command @kbd{M-x
167edebug-all-defs} toggles the value of the variable
168@code{edebug-all-defs}.
169
170@findex edebug-all-forms
171@findex eval-region (Edebug)
172@findex eval-current-buffer (Edebug)
173 If @code{edebug-all-defs} is non-@code{nil}, then the commands
174@code{eval-region}, @code{eval-current-buffer}, and @code{eval-buffer}
175also instrument any definitions they evaluate. Similarly,
176@code{edebug-all-forms} controls whether @code{eval-region} should
177instrument @emph{any} form, even non-defining forms. This doesn't apply
178to loading or evaluations in the minibuffer. The command @kbd{M-x
179edebug-all-forms} toggles this option.
180
181@findex edebug-eval-top-level-form
182Another command, @kbd{M-x edebug-eval-top-level-form}, is available to
183instrument any top-level form regardless of the value of
184@code{edebug-all-defs} or @code{edebug-all-forms}.
185
186When Edebug is about to instrument code for the first time in a session,
187it runs the hook @code{edebug-setup-hook}, then sets it to @code{nil}.
188You can use this to load up Edebug specifications associated with a
189package you are using, but only when you also use Edebug.
190
191While Edebug is active, the command @kbd{I}
192(@code{edebug-instrument-callee}) instruments the definition of the
193function or macro called by the list form after point, if is not already
194instrumented. This is possible only if Edebug knows where to find the
195source for that function; after loading Edebug, @code{eval-region}
196records the position of every definition it evaluates, even if not
197instrumenting it. See also the @kbd{i} command (@pxref{Jumping}), which
198steps into the call after instrumenting the function.
199
200@cindex special forms (Edebug)
201@cindex interactive commands (Edebug)
202@cindex anonymous lambda expressions (Edebug)
203@cindex Common Lisp (Edebug)
204@pindex cl.el (Edebug)
205@pindex cl-specs.el
206 Edebug knows how to instrument all the standard special forms, an
207interactive form with an expression argument, anonymous lambda
208expressions, and other defining forms. Edebug cannot know what a
209user-defined macro will do with the arguments of a macro call, so you
210must tell it; @xref{Instrumenting Macro Calls}, for details.
211
212@findex eval-expression (Edebug)
213 To remove instrumentation from a definition, simply reevaluate its
214definition in a way that does not instrument. There are two ways of
87b2d5ff 215evaluating forms that never instrument them: from a file with
73804d4b 216@code{load}, and from the minibuffer with @code{eval-expression}
bfe721d1 217(@kbd{M-:}).
73804d4b
RS
218
219 If Edebug detects a syntax error while instrumenting, it leaves point
220at the erroneous code and signals an @code{invalid-read-syntax} error.
221
222 @xref{Edebug Eval}, for other evaluation functions available
223inside of Edebug.
224
225@node Edebug Execution Modes
226@subsection Edebug Execution Modes
227
228@cindex Edebug execution modes
229Edebug supports several execution modes for running the program you are
230debugging. We call these alternatives @dfn{Edebug execution modes}; do
87b2d5ff 231not confuse them with major or minor modes. The current Edebug execution mode
73804d4b
RS
232determines how far Edebug continues execution before stopping---whether
233it stops at each stop point, or continues to the next breakpoint, for
234example---and how much Edebug displays the progress of the evaluation
235before it stops.
236
237Normally, you specify the Edebug execution mode by typing a command to
238continue the program in a certain mode. Here is a table of these
239commands. All except for @kbd{S} resume execution of the program, at
240least for a certain distance.
241
242@table @kbd
243@item S
244Stop: don't execute any more of the program for now, just wait for more
245Edebug commands (@code{edebug-stop}).
246
247@item @key{SPC}
248Step: stop at the next stop point encountered (@code{edebug-step-mode}).
249
250@item n
251Next: stop at the next stop point encountered after an expression
252(@code{edebug-next-mode}). Also see @code{edebug-forward-sexp} in
253@ref{Edebug Misc}.
254
255@item t
256Trace: pause one second at each Edebug stop point (@code{edebug-trace-mode}).
257
258@item T
259Rapid trace: update the display at each stop point, but don't actually
260pause (@code{edebug-Trace-fast-mode}).
261
262@item g
263Go: run until the next breakpoint (@code{edebug-go-mode}). @xref{Breakpoints}.
264
265@item c
266Continue: pause one second at each breakpoint, and then continue
267(@code{edebug-continue-mode}).
268
269@item C
270Rapid continue: move point to each breakpoint, but don't pause
271(@code{edebug-Continue-fast-mode}).
272
273@item G
274Go non-stop: ignore breakpoints (@code{edebug-Go-nonstop-mode}). You
275can still stop the program by typing @kbd{S}, or any editing command.
276@end table
277
278In general, the execution modes earlier in the above list run the
87b2d5ff 279program more slowly or stop sooner than the modes later in the list.
73804d4b
RS
280
281While executing or tracing, you can interrupt the execution by typing
282any Edebug command. Edebug stops the program at the next stop point and
87b2d5ff
RS
283then executes the command you typed. For example, typing @kbd{t} during
284execution switches to trace mode at the next stop point. You can use
285@kbd{S} to stop execution without doing anything else.
73804d4b
RS
286
287If your function happens to read input, a character you type intending
288to interrupt execution may be read by the function instead. You can
289avoid such unintended results by paying attention to when your program
290wants input.
291
292@cindex keyboard macros (Edebug)
293Keyboard macros containing the commands in this section do not
294completely work: exiting from Edebug, to resume the program, loses track
295of the keyboard macro. This is not easy to fix. Also, defining or
296executing a keyboard macro outside of Edebug does not affect commands
297inside Edebug. This is usually an advantage. But see the
298@code{edebug-continue-kbd-macro} option (@pxref{Edebug Options}).
299
300When you enter a new Edebug level, the initial execution mode comes from
301the value of the variable @code{edebug-initial-mode}. By default, this
302specifies step mode. Note that you may reenter the same Edebug level
303several times if, for example, an instrumented function is called
304several times from one command.
305
306
307@node Jumping
308@subsection Jumping
309
310 The commands described in this section execute until they reach a
311specified location. All except @kbd{i} make a temporary breakpoint to
312establish the place to stop, then switch to go mode. Any other
313breakpoint reached before the intended stop point will also stop
314execution. @xref{Breakpoints}, for the details on breakpoints.
315
316 These commands may fail to work as expected in case of nonlocal exit,
317because a nonlocal exit can bypass the temporary breakpoint where you
318expected the program to stop.
319
320@table @kbd
321@item h
322Proceed to the stop point near where point is (@code{edebug-goto-here}).
323
324@item f
325Run the program forward over one expression
326(@code{edebug-forward-sexp}).
327
328@item o
329Run the program until the end of the containing sexp.
330
331@item i
332Step into the function or macro called by the form after point.
333@end table
334
335The @kbd{h} command proceeds to the stop point near the current location
336if point, using a temporary breakpoint. See @ref{Breakpoints}, for more
337about breakpoints.
338
339The @kbd{f} command runs the program forward over one expression. More
340precisely, it sets a temporary breakpoint at the position that
341@kbd{C-M-f} would reach, then executes in go mode so that the program
342will stop at breakpoints.
343
344With a prefix argument @var{n}, the temporary breakpoint is placed
345@var{n} sexps beyond point. If the containing list ends before @var{n}
346more elements, then the place to stop is after the containing
347expression.
348
349Be careful that the position @kbd{C-M-f} finds is a place that the
350program will really get to; this may not be true in a
351@code{cond}, for example.
352
353The @kbd{f} command does @code{forward-sexp} starting at point, rather
354than at the stop point, for flexibility. If you want to execute one
355expression @emph{from the current stop point}, type @kbd{w} first, to
356move point there, and then type @kbd{f}.
357
358The @kbd{o} command continues ``out of'' an expression. It places a
359temporary breakpoint at the end of the sexp containing point. If the
87b2d5ff
RS
360containing sexp is a function definition itself, @kbd{o} continues until
361just before the last sexp in the definition. If that is where you are
362now, it returns from the function and then stops. In other words, this
73804d4b
RS
363command does not exit the currently executing function unless you are
364positioned after the last sexp.
365
366The @kbd{i} command steps into the function or macro called by the list
87b2d5ff
RS
367form after point, and stops at its first stop point. Note that the form
368need not be the one about to be evaluated. But if the form is a
369function call about to be evaluated, remember to use this command before
370any of the arguments are evaluated, since otherwise it will be too late.
73804d4b
RS
371
372The @kbd{i} command instruments the function or macro it's supposed to
373step into, if it isn't instrumented already. This is convenient, but keep
374in mind that the function or macro remains instrumented unless you explicitly
375arrange to deinstrument it.
376
377@node Edebug Misc
378@subsection Miscellaneous Edebug Commands
379
380 Some miscellaneous Edebug commands are described here.
381
382@table @kbd
383@item ?
384Display the help message for Edebug (@code{edebug-help}).
385
386@item C-]
387Abort one level back to the previous command level
388(@code{abort-recursive-edit}).
389
390@item q
391Return to the top level editor command loop (@code{top-level}). This
392exits all recursive editing levels, including all levels of Edebug
393activity. However, instrumented code protected with
394@code{unwind-protect} or @code{condition-case} forms may resume
395debugging.
396
397@item Q
398Like @kbd{q} but don't stop even for protected code
399(@code{top-level-nonstop}).
400
401@item r
402Redisplay the most recently known expression result in the echo area
403(@code{edebug-previous-result}).
404
405@item d
406Display a backtrace, excluding Edebug's own functions for clarity
407(@code{edebug-backtrace}).
408
409You cannot use debugger commands in the backtrace buffer in Edebug as
410you would in the standard debugger.
411
412The backtrace buffer is killed automatically when you continue
413execution.
414@end table
415
87b2d5ff 416From the Edebug recursive edit, you may invoke commands that activate
73804d4b
RS
417Edebug again recursively. Any time Edebug is active, you can quit to
418the top level with @kbd{q} or abort one recursive edit level with
419@kbd{C-]}. You can display a backtrace of all the
420pending evaluations with @kbd{d}.
421
422@node Breakpoints
423@subsection Breakpoints
424
425@cindex breakpoints
426Edebug's step mode stops execution at the next stop point reached.
427There are three other ways to stop Edebug execution once it has started:
428breakpoints, the global break condition, and source breakpoints.
429
430While using Edebug, you can specify @dfn{breakpoints} in the program you
431are testing: points where execution should stop. You can set a
432breakpoint at any stop point, as defined in @ref{Using Edebug}. For
433setting and unsetting breakpoints, the stop point that is affected is
434the first one at or after point in the source code buffer. Here are the
435Edebug commands for breakpoints:
436
437@table @kbd
438@item b
439Set a breakpoint at the stop point at or after point
440(@code{edebug-set-breakpoint}). If you use a prefix argument, the
441breakpoint is temporary (it turns off the first time it stops the
442program).
443
444@item u
445Unset the breakpoint (if any) at the stop point at or after
446point (@code{edebug-unset-breakpoint}).
447
448@item x @var{condition} @key{RET}
449Set a conditional breakpoint which stops the program only if
450@var{condition} evaluates to a non-@code{nil} value
451(@code{edebug-set-conditional-breakpoint}). With a prefix argument, the
452breakpoint is temporary.
453
454@item B
455Move point to the next breakpoint in the definition
456(@code{edebug-next-breakpoint}).
457@end table
458
459While in Edebug, you can set a breakpoint with @kbd{b} and unset one
460with @kbd{u}. First move point to the Edebug stop point of your choice,
461then type @kbd{b} or @kbd{u} to set or unset a breakpoint there.
462Unsetting a breakpoint where none has been set has no effect.
463
464Reevaluating or reinstrumenting a definition forgets all its breakpoints.
465
466A @dfn{conditional breakpoint} tests a condition each time the program
467gets there. Any errors that occur as a result of evaluating the
468condition are ignored, as if the result were @code{nil}. To set a
469conditional breakpoint, use @kbd{x}, and specify the condition
470expression in the minibuffer. Setting a conditional breakpoint at a
471stop point that has a previously established conditional breakpoint puts
472the previous condition expression in the minibuffer so you can edit it.
473
474You can make a conditional or unconditional breakpoint
475@dfn{temporary} by using a prefix arg with the command to set the
476breakpoint. When a temporary breakpoint stops the program, it is
477automatically unset.
478
479Edebug always stops or pauses at a breakpoint except when the Edebug
480mode is Go-nonstop. In that mode, it ignores breakpoints entirely.
481
482To find out where your breakpoints are, use the @kbd{B} command, which
87b2d5ff
RS
483moves point to the next breakpoint following point, within the same
484function, or to the first breakpoint if there are no following
485breakpoints. This command does not continue execution---it just moves
486point in the buffer.
73804d4b
RS
487
488@menu
489* Global Break Condition:: Breaking on an event.
490* Source Breakpoints:: Embedding breakpoints in source code.
491@end menu
492
493
494@node Global Break Condition
495@subsubsection Global Break Condition
496
497@cindex stopping on events
498@cindex global break condition
499 A @dfn{global break condition} stops execution when a specified
500condition is satisfied, no matter where that may occur. Edebug
501evaluates the global break condition at every stop point. If it
502evaluates to a non-@code{nil} value, then execution stops or pauses
503depending on the execution mode, as if a breakpoint had been hit. If
504evaluating the condition gets an error, execution does not stop.
505
506@findex edebug-set-global-break-condition
507@vindex edebug-global-break-condition
508 You can set or edit the condition expression, stored in
509@code{edebug-global-break-condition}, using the @kbd{X} command
510(@code{edebug-set-global-break-condition}).
511
512 The global break condition is the simplest way to find where in your
513code some event occurs, but it makes code run much more slowly. So you
514should reset the condition to @code{nil} when not using it.
515
516@node Source Breakpoints
517@subsubsection Source Breakpoints
518
519@findex edebug
520@cindex source breakpoints
521 All breakpoints in a definition are forgotten each time you
522reinstrument it. To make a breakpoint that won't be forgotten, you can
523write a @dfn{source breakpoint}, which is simply a call to the function
524@code{edebug} in your source code. You can, of course, make such a call
525conditional. For example, in the @code{fac} function, insert the first
526line as shown below to stop when the argument reaches zero:
527
528@example
529(defun fac (n)
530 (if (= n 0) (edebug))
531 (if (< 0 n)
532 (* n (fac (1- n)))
533 1))
534@end example
535
536When the @code{fac} definition is instrumented and the function is
537called, the call to @code{edebug} acts as a breakpoint. Depending on
538the execution mode, Edebug stops or pauses there.
539
540If no instrumented code is being executed when @code{edebug} is called,
541that function calls @code{debug}.
542@c This may not be a good idea anymore.
543
544@node Trapping Errors
545@subsection Trapping Errors
546
547Emacs normally displays an error message when an error is signaled and
548not handled with @code{condition-case}. While Edebug is active, it
549normally responds to all unhandled errors. You can customize this with
550the options @code{edebug-on-error} and @code{edebug-on-quit}; see
551@ref{Edebug Options}.
552
553When Edebug responds to an error, it shows the last stop point
554encountered before the error. This may be the location of a call to a
555function which was not instrumented, within which the error actually
556occurred. For an unbound variable error, the last known stop point
557might be quite distant from the offending variable reference. In that
558case you might want to display a full backtrace (@pxref{Edebug Misc}).
559
87b2d5ff 560@c Edebug should be changed for the following: -- dan
73804d4b
RS
561If you change @code{debug-on-error} or @code{debug-on-quit} while
562Edebug is active, these changes will be forgotten when Edebug becomes
563inactive. Furthermore, during Edebug's recursive edit, these variables
564are bound to the values they had outside of Edebug.
565
73804d4b
RS
566@node Edebug Views
567@subsection Edebug Views
568
569These Edebug commands let you view aspects of the buffer and window
87b2d5ff
RS
570status that obtained before entry to Edebug. The outside window
571configuration is the collection of windows and contents that were in
572effect outside of Edebug.
73804d4b
RS
573
574@table @kbd
575@item v
87b2d5ff
RS
576Temporarily view the outside window configuration
577(@code{edebug-view-outside}).
73804d4b
RS
578
579@item p
580Temporarily display the outside current buffer with point at its outside
581position (@code{edebug-bounce-point}). With a prefix argument @var{n},
582pause for @var{n} seconds instead.
583
584@item w
585Move point back to the current stop point (@code{edebug-where}) in the
586source code buffer. Also, if you use this command in a different window
587displaying the same buffer, that window will be used instead to display
588the current definition in the future.
589
590@item W
87b2d5ff
RS
591@c Its function is not simply to forget the saved configuration -- dan
592Toggle whether Edebug saves and restores the outside window
593configuration (@code{edebug-toggle-save-windows}).
594
595With a prefix argument, @code{W} only toggles saving and restoring of
596the selected window. To specify a window that is not displaying the
597source code buffer, you must use @kbd{C-x X W} from the global keymap.
73804d4b
RS
598@end table
599
600You can view the outside window configuration with @kbd{v} or just
601bounce to the point in the current buffer with @kbd{p}, even if
602it is not normally displayed. After moving point, you may wish to jump
603back to the stop point with @kbd{w} from a source code buffer.
604
87b2d5ff
RS
605Each time you use @kbd{W} to turn saving @emph{off}, Edebug forgets the
606saved outside window configuration---so that even if you turn saving
607back @emph{on}, the current window configuration remains unchanged when
608you next exit Edebug (by continuing the program). However, the
609automatic redisplay of @samp{*edebug*} and @samp{*edebug-trace*} may
610conflict with the buffers you wish to see unless you have enough windows
611open.
73804d4b
RS
612
613@node Edebug Eval
614@subsection Evaluation
615
616While within Edebug, you can evaluate expressions ``as if'' Edebug were
617not running. Edebug tries to be invisible to the expression's
618evaluation and printing. Evaluation of expressions that cause side
619effects will work as expected except for things that Edebug explicitly
620saves and restores. @xref{The Outside Context}, for details on this
621process.
622
623@table @kbd
624@item e @var{exp} @key{RET}
625Evaluate expression @var{exp} in the context outside of Edebug
626(@code{edebug-eval-expression}). That is, Edebug tries to minimize its
627interference with the evaluation.
628
bfe721d1 629@item M-: @var{exp} @key{RET}
73804d4b
RS
630Evaluate expression @var{exp} in the context of Edebug itself.
631
632@item C-x C-e
633Evaluate the expression before point, in the context outside of Edebug
634(@code{edebug-eval-last-sexp}).
635@end table
636
637@cindex lexical binding (Edebug)
638Edebug supports evaluation of expressions containing references to
639lexically bound symbols created by the following constructs in
640@file{cl.el} (version 2.03 or later): @code{lexical-let},
641@code{macrolet}, and @code{symbol-macrolet}.
642
643
644@node Eval List
645@subsection Evaluation List Buffer
646
647You can use the @dfn{evaluation list buffer}, called @samp{*edebug*}, to
648evaluate expressions interactively. You can also set up the
649@dfn{evaluation list} of expressions to be evaluated automatically each
650time Edebug updates the display.
651
652@table @kbd
653@item E
654Switch to the evaluation list buffer @samp{*edebug*}
655(@code{edebug-visit-eval-list}).
656@end table
657
658In the @samp{*edebug*} buffer you can use the commands of Lisp
659Interaction mode (@pxref{Lisp Interaction,,, emacs, The GNU Emacs
660Manual}) as well as these special commands:
661
662@table @kbd
663@item LFD
664Evaluate the expression before point, in the outside context, and insert
665the value in the buffer (@code{edebug-eval-print-last-sexp}).
666
667@item C-x C-e
668Evaluate the expression before point, in the context outside of Edebug
669(@code{edebug-eval-last-sexp}).
670
671@item C-c C-u
87b2d5ff 672Build a new evaluation list from the contents of the buffer
73804d4b
RS
673(@code{edebug-update-eval-list}).
674
675@item C-c C-d
676Delete the evaluation list group that point is in
677(@code{edebug-delete-eval-item}).
678
679@item C-c C-w
680Switch back to the source code buffer at the current stop point
681(@code{edebug-where}).
682@end table
683
684You can evaluate expressions in the evaluation list window with
685@kbd{LFD} or @kbd{C-x C-e}, just as you would in @samp{*scratch*};
686but they are evaluated in the context outside of Edebug.
687
688The expressions you enter interactively (and their results) are lost
689when you continue execution; but you can set up an @dfn{evaluation list}
690consisting of expressions to be evaluated each time execution stops.
691
692@cindex evaluation list group
693To do this, write one or more @dfn{evaluation list groups} in the
694evaluation list buffer. An evaluation list group consists of one or
695more Lisp expressions. Groups are separated by comment lines.
696
697The command @kbd{C-c C-u} (@code{edebug-update-eval-list}) rebuilds the
698evaluation list, scanning the buffer and using the first expression of
87b2d5ff
RS
699each group. (The idea is that the second expression of the group is the
700value previously computed and displayed.)
73804d4b
RS
701
702Be careful not to add expressions that execute instrumented code since
703that would cause an infinite loop.
704@c There ought to be a way to fix this.
705
87b2d5ff
RS
706Each entry to Edebug redisplays the evaluation list by inserting each
707expression in the buffer, followed by its current value. It also
708inserts comment lines so that each expression becomes its own group.
709Thus, if you type @kbd{C-c C-u} again without changing the buffer text,
710the evaluation list is effectively unchanged.
73804d4b
RS
711
712If an error occurs during an evaluation from the evaluation list, the
713error message is displayed in a string as if it were the result.
714Therefore, expressions that use variables not currently valid do not
715interrupt your debugging.
716
717Here is an example of what the evaluation list window looks like after
718several expressions have been added to it:
719
720@smallexample
721(current-buffer)
722#<buffer *scratch*>
723;---------------------------------------------------------------
724(selected-window)
725#<window 16 on *scratch*>
726;---------------------------------------------------------------
727(point)
728196
729;---------------------------------------------------------------
730bad-var
731"Symbol's value as variable is void: bad-var"
732;---------------------------------------------------------------
733(recursion-depth)
7340
735;---------------------------------------------------------------
736this-command
737eval-last-sexp
738;---------------------------------------------------------------
739@end smallexample
740
741To delete a group, move point into it and type @kbd{C-c C-d}, or simply
742delete the text for the group and update the evaluation list with
743@kbd{C-c C-u}. To add a new expression to the evaluation list, insert
744the expression at a suitable place, and insert a new comment line. (You
745need not insert dashes in the comment line---its contents don't matter.)
746Then type @kbd{C-c C-u}.
747
748After selecting @samp{*edebug*}, you can return to the source code
749buffer with @kbd{C-c C-w}. The @samp{*edebug*} buffer is killed when
750you continue execution, and recreated next time it is needed.
751
752
753@node Printing in Edebug
754@subsection Printing in Edebug
755
756@cindex printing (Edebug)
757@cindex printing circular structures
758@pindex cust-print
759 If an expression in your program produces a value containing circular
760list structure, you may get an error when Edebug attempts to print it.
761
762@vindex edebug-print-length
763@vindex edebug-print-level
764 One way to cope with circular structure is to set @code{print-length}
765or @code{print-level} to truncate the printing. Edebug does this for
766you; it binds @code{print-length} and @code{print-level} to 50 if they
767were @code{nil}. (Actually, the variables @code{edebug-print-length}
768and @code{edebug-print-level} specify the values to use within Edebug.)
769@xref{Output Variables}.
770
771 You can also print circular structures and structures that share
772elements more informatively by using the @file{cust-print} package.
773
774 To load @file{cust-print} and activate custom printing only for
775Edebug, simply use the command @kbd{M-x edebug-install-custom-print}.
776To restore the standard print functions, use @kbd{M-x
777edebug-uninstall-custom-print}.
778
779 Here is an example of code that creates a circular structure:
780
781@example
782(setq a '(x y))
783(setcar a a))
784@end example
785
786@noindent
787Custom printing prints this as @samp{Result: #1=(#1# y)}. The
788@samp{#1=} notation labels the structure that follows it with the label
789@samp{1}, and the @samp{#1#} notation references the previously labelled
790structure. This notation is used for any shared elements of lists or
791vectors.
792
793 Other programs can also use custom printing; see @file{cust-print.el}
794for details.
795
796@node Trace Buffer
797@subsection Trace Buffer
798@cindex trace buffer
799
87b2d5ff 800 Edebug can record an execution trace, storing it in a buffer named
73804d4b
RS
801@samp{*edebug-trace*}. This is a log of function calls and returns,
802showing the function names and their arguments and values. To enable
803trace recording, set @code{edebug-trace} to a non-@code{nil} value.
804
805 Making a trace buffer is not the same thing as using trace execution
806mode (@pxref{Edebug Execution Modes}).
807
808 When trace recording is enabled, each function entry and exit adds
809lines to the trace buffer. A function entry record looks like
810@samp{::::@{} followed by the function name and argument values. A
811function exit record looks like @samp{::::@}} followed by the function
812name and result of the function.
813
814 The number of @samp{:}s in an entry shows its recursion depth. You
815can use the braces in the trace buffer to find the matching beginning or
816end of function calls.
817
818@findex edebug-print-trace-before
819@findex edebug-print-trace-after
820 You can customize trace recording for function entry and exit by
821redefining the functions @code{edebug-print-trace-before} and
822@code{edebug-print-trace-after}.
823
824@defmac edebug-tracing string body@dots{}
825This macro requests additional trace information around the execution
826of the @var{body} forms. The argument @var{string} specifies text
827to put in the trace buffer. All the arguments are evaluated.
828@code{edebug-tracing} returns the value of the last form in @var{body}.
829@end defmac
830
831@defun edebug-trace format-string &rest format-args
832This function inserts text in the trace buffer. It computes the text
833with @code{(apply 'format @var{format-string} @var{format-args})}.
87b2d5ff 834It also appends a newline to separate entries.
73804d4b
RS
835@end defun
836
837 @code{edebug-tracing} and @code{edebug-trace} insert lines in the trace
838buffer even if Edebug is not active.
839
840 Adding text to the trace buffer also scrolls its window to show the
841last lines inserted.
842
73804d4b
RS
843@node Coverage Testing
844@subsection Coverage Testing
845
846@cindex coverage testing
847@cindex frequency counts
848@cindex performance analysis
849Edebug provides rudimentary coverage testing and display of execution
850frequency. All execution of an instrumented function accumulates
851frequency counts, both before and after evaluation of each instrumented
852expression, even if the execution mode is Go-nonstop. Coverage testing
853is more expensive, so it is only done if @code{edebug-test-coverage} is
854non-@code{nil}. The command @kbd{M-x edebug-display-freq-count}
855displays both the frequency data and the coverage data (if recorded).
856
857@deffn Command edebug-display-freq-count
858This command displays the frequency count data for each line of the
859current definition.
860
87b2d5ff 861The frequency counts appear as comment lines after each line of code, and
73804d4b 862you can undo all insertions with one @code{undo} command. The counts
87b2d5ff 863appear under the @kbd{(} before an expression or the @kbd{)} after
73804d4b
RS
864an expression, or on the last character of a symbol. Values do not appear if
865they are equal to the previous count on the same line.
866
867The character @samp{=} following the count for an expression says that
868the expression has returned the same value each time it was evaluated
869This is the only coverage information that Edebug records.
870
871To clear the frequency count and coverage data for a definition,
872reinstrument it.
873@end deffn
874
875For example, after evaluating @code{(fac 5)} with a source
876breakpoint, and setting @code{edebug-test-coverage} to @code{t}, when
877the breakpoint is reached, the frequency data looks like this:
878
879@example
880(defun fac (n)
881 (if (= n 0) (edebug))
882;#6 1 0 =5
883 (if (< 0 n)
884;#5 =
885 (* n (fac (1- n)))
886;# 5 0
887 1))
888;# 0
889@end example
890
87b2d5ff
RS
891The comment lines show that @code{fac} was called 6 times. The
892first @code{if} statement returned 5 times with the same result each
73804d4b 893time; the same is true of the condition on the second @code{if}.
87b2d5ff 894The recursive call of @code{fac} did not return at all.
73804d4b
RS
895
896
897@node The Outside Context
898@subsection The Outside Context
899
900Edebug tries to be transparent to the program you are debugging, but it
901does not succeed completely. Edebug also tries to be transparent when
902you evaluate expressions with @kbd{e} or with the evaluation list
903buffer, by temporarily restoring the outside context. This section
904explains precisely what context Edebug restores, and how Edebug fails to
905be completely transparent.
906
73804d4b
RS
907@menu
908* Checking Whether to Stop:: When Edebug decides what to do.
909* Edebug Display Update:: When Edebug updates the display.
910* Edebug Recursive Edit:: When Edebug stops execution.
911@end menu
912
913@node Checking Whether to Stop
914@subsubsection Checking Whether to Stop
915
87b2d5ff
RS
916Whenever Edebug is entered, it needs to save and restore certain data
917before even deciding whether to make trace information or stop the
918program.
73804d4b
RS
919
920@itemize @bullet
921@item
922@code{max-lisp-eval-depth} and @code{max-specpdl-size} are both
923incremented one time to reduce Edebug's impact on the stack.
924You could, however, still run out of stack space when using Edebug.
925
926@item
927The state of keyboard macro execution is saved and restored. While
928Edebug is active, @code{executing-macro} is bound to
929@code{edebug-continue-kbd-macro}.
930
931@end itemize
932
933
934@node Edebug Display Update
935@subsubsection Edebug Display Update
936
87b2d5ff
RS
937@c This paragraph is not filled, because LaLiberte's conversion script
938@c needs an xref to be on just one line.
73804d4b 939When Edebug needs to display something (e.g., in trace mode), it saves
87b2d5ff
RS
940the current window configuration from ``outside'' Edebug
941(@pxref{Window Configurations}). When you exit Edebug (by continuing
942the program), it restores the previous window configuration.
73804d4b
RS
943
944Emacs redisplays only when it pauses. Usually, when you continue
945execution, the program comes back into Edebug at a breakpoint or after
946stepping without pausing or reading input in between. In such cases,
947Emacs never gets a chance to redisplay the ``outside'' configuration.
948What you see is the same window configuration as the last time Edebug
949was active, with no interruption.
950
951Entry to Edebug for displaying something also saves and restores the
952following data, but some of these are deliberately not restored if an
953error or quit signal occurs.
954
955@itemize @bullet
956@item
957@cindex current buffer point and mark (Edebug)
958Which buffer is current, and the positions of point and the mark in the
959current buffer, are saved and restored.
960
961@item
962@cindex window configuration (Edebug)
963The outside window configuration is saved and restored if
964@code{edebug-save-windows} is non-@code{nil} (@pxref{Edebug Display Update}).
965
966The window configuration is not restored on error or quit, but the
967outside selected window @emph{is} reselected even on error or quit in
968case a @code{save-excursion} is active. If the value of
969@code{edebug-save-windows} is a list, only the listed windows are saved
970and restored.
971
972The window start and horizontal scrolling of the source code buffer are
973not restored, however, so that the display remains coherent within Edebug.
974
975@item
73804d4b
RS
976The value of point in each displayed buffer is saved and restored if
977@code{edebug-save-displayed-buffer-points} is non-@code{nil}.
978
979@item
980The variables @code{overlay-arrow-position} and
981@code{overlay-arrow-string} are saved and restored. So you can safely
982invoke Edebug from the recursive edit elsewhere in the same buffer.
983
984@item
985@code{cursor-in-echo-area} is locally bound to @code{nil} so that
986the cursor shows up in the window.
987@end itemize
988
989@node Edebug Recursive Edit
990@subsubsection Edebug Recursive Edit
991
992When Edebug is entered and actually reads commands from the user, it
993saves (and later restores) these additional data:
994
995@itemize @bullet
996@item
997The current match data. @xref{Match Data}.
998
999@item
1000@code{last-command}, @code{this-command}, @code{last-command-char},
1001@code{last-input-char}, @code{last-input-event},
1002@code{last-command-event}, @code{last-event-frame},
1003@code{last-nonmenu-event}, and @code{track-mouse}. Commands used within
1004Edebug do not affect these variables outside of Edebug.
1005
1006The key sequence returned by @code{this-command-keys} is changed by
1007executing commands within Edebug and there is no way to reset
1008the key sequence from Lisp.
1009
87b2d5ff
RS
1010Edebug cannot save and restore the value of
1011@code{unread-command-events}. Entering Edebug while this variable has a
1012nontrivial value can interfere with execution of the program you are
1013debugging.
1014
73804d4b
RS
1015@item
1016Complex commands executed while in Edebug are added to the variable
1017@code{command-history}. In rare cases this can alter execution.
1018
1019@item
1020Within Edebug, the recursion depth appears one deeper than the recursion
1021depth outside Edebug. This is not true of the automatically updated
1022evaluation list window.
1023
1024@item
1025@code{standard-output} and @code{standard-input} are bound to @code{nil}
1026by the @code{recursive-edit}, but Edebug temporarily restores them during
1027evaluations.
1028
1029@item
1030The state of keyboard macro definition is saved and restored. While
1031Edebug is active, @code{defining-kbd-macro} is bound to
1032@code{edebug-continue-kbd-macro}.
1033@end itemize
1034
1035@node Instrumenting Macro Calls
1036@subsection Instrumenting Macro Calls
1037
1038When Edebug instruments an expression that calls a Lisp macro, it needs
1039additional advice to do the job properly. This is because there is no
1040way to tell which subexpressions of the macro call are forms to be
1041evaluated. (Evaluation may occur explicitly in the macro body, or when
1042the resulting expansion is evaluated, or any time later.) You must
1043explain the format of calls to each macro to enable Edebug to handle it.
87b2d5ff 1044To do this, use @code{def-edebug-spec} to define the format of
73804d4b
RS
1045calls to a given macro.
1046
1047@deffn Macro def-edebug-spec macro specification
1048Specify which expressions of a call to macro @var{macro} are forms to be
1049evaluated. For simple macros, the @var{specification} often looks very
1050similar to the formal argument list of the macro definition, but
1051specifications are much more general than macro arguments.
1052
1053The @var{macro} argument may actually be any symbol, not just a macro
1054name.
1055@end deffn
1056
1057Here is a simple example that defines the specification for the
1058@code{for} macro described in the Emacs Lisp Reference Manual, followed
1059by an alternative, equivalent specification.
1060
1061@example
1062(def-edebug-spec for
1063 (symbolp "from" form "to" form "do" &rest form))
1064
1065(def-edebug-spec for
1066 (symbolp ['from form] ['to form] ['do body]))
1067@end example
1068
1069Here is a table of the possibilities for @var{specification} and how each
1070directs processing of arguments.
1071
ec221d13 1072@table @asis
73804d4b
RS
1073@item @code{t}
1074All arguments are instrumented for evaluation.
1075
1076@item @code{0}
1077None of the arguments is instrumented.
1078
1079@item a symbol
1080The symbol must have an Edebug specification which is used instead.
1081This indirection is repeated until another kind of specification is
1082found. This allows you to inherit the specification for another macro.
1083
1084@item a list
1085The elements of the list describe the types of the arguments of a
1086calling form. The possible elements of a specification list are
1087described in the following sections.
1088@end table
1089
1090@menu
1091* Specification List:: How to specify complex patterns of evaluation.
1092* Backtracking:: What Edebug does when matching fails.
73804d4b
RS
1093* Specification Examples:: To help understand specifications.
1094@end menu
1095
1096
1097@node Specification List
1098@subsubsection Specification List
1099
1100@cindex Edebug specification list
1101A @dfn{specification list} is required for an Edebug specification if
1102some arguments of a macro call are evaluated while others are not. Some
1103elements in a specification list match one or more arguments, but others
1104modify the processing of all following elements. The latter, called
1105@dfn{specification keywords}, are symbols beginning with @samp{&} (such
1106as @code{&optional}).
1107
1108A specification list may contain sublists which match arguments that are
1109themselves lists, or it may contain vectors used for grouping. Sublists
1110and groups thus subdivide the specification list into a hierarchy of
1111levels. Specification keywords only apply to the remainder of the
1112sublist or group they are contained in.
1113
1114When a specification list involves alternatives or repetition, matching
1115it against an actual macro call may require backtracking.
1116@xref{Backtracking}, for more details.
1117
1118Edebug specifications provide the power of regular expression matching,
1119plus some context-free grammar constructs: the matching of sublists with
1120balanced parentheses, recursive processing of forms, and recursion via
1121indirect specifications.
1122
1123Here's a table of the possible elements of a specification list, with
1124their meanings:
1125
1126@table @code
1127@item sexp
87b2d5ff
RS
1128A single Lisp object, not unevaluated.
1129@c "unevaluated expression" is not meaningful, because
1130@c an expression is a Lisp object intended for evaluation.
73804d4b
RS
1131
1132@item form
1133A single evaluated expression, which is instrumented.
1134
1135@item place
1136@findex edebug-unwrap
1137A place to store a value, as in the Common Lisp @code{setf} construct.
1138
1139@item body
1140Short for @code{&rest form}. See @code{&rest} below.
1141
1142@item function-form
1143A function form: either a quoted function symbol, a quoted lambda
1144expression, or a form (that should evaluate to a function symbol or
1145lambda expression). This is useful when an argument that's a lambda
1146expression might be quoted with @code{quote} rather than
1147@code{function}, since it instruments the body of the lambda expression
1148either way.
1149
1150@item lambda-expr
1151A lambda expression with no quoting.
1152
1153@item &optional
1154@kindex &optional @r{(Edebug)}
1155All following elements in the specification list are optional; as soon
1156as one does not match, Edebug stops matching at this level.
1157
1158To make just a few elements optional followed by non-optional elements,
1159use @code{[&optional @var{specs}@dots{}]}. To specify that several
1160elements must all match or none, use @code{&optional
1161[@var{specs}@dots{}]}. See the @code{defun} example below.
1162
1163@item &rest
1164@kindex &rest @r{(Edebug)}
1165All following elements in the specification list are repeated zero or
1166more times. All the elements need not match in the last repetition,
1167however.
1168
1169To repeat only a few elements, use @code{[&rest @var{specs}@dots{}]}.
1170To specify several elements that must all match on every repetition, use
1171@code{&rest [@var{specs}@dots{}]}.
1172
1173@item &or
1174@kindex &or @r{(Edebug)}
1175Each of the following elements in the specification list is an
1176alternative. One of the alternatives must match, or the @code{&or}
1177specification fails.
1178
1179Each list element following @code{&or} is a single alternative. To
1180group two or more list elements as a single alternative, enclose them in
1181@code{[@dots{}]}.
1182
1183@item &not
1184@kindex &not @r{(Edebug)}
1185Each of the following elements is matched as alternatives as if by using
1186@code{&or}, but if any of them match, the specification fails. If none
1187of them match, nothing is matched, but the @code{&not} specification
1188succeeds.
1189
1190@item &define
1191@kindex &define @r{(Edebug)}
1192Indicates that the specification is for a defining form. The defining
1193form itself is not instrumented (i.e. Edebug does not stop before and
1194after the defining form), but forms inside it typically will be
1195instrumented. The @code{&define} keyword should be the first element in
1196a list specification.
1197
1198@item nil
1199This is successful when there are no more arguments to match at the
1200current argument list level; otherwise it fails. See sublist
1201specifications and the backquote example below.
1202
1203@item gate
1204@cindex preventing backtracking
1205No argument is matched but backtracking through the gate is disabled
1206while matching the remainder of the specifications at this level. This
1207is primarily used to generate more specific syntax error messages. See
1208@ref{Backtracking}, for more details. Also see the @code{let} example
1209below.
1210
1211@item @var{other-symbol}
1212@cindex indirect specifications
1213Any other symbol in a specification list may be a predicate or an
1214indirect specification.
1215
1216If the symbol has an Edebug specification, this @dfn{indirect
1217specification} should be either a list specification that is used in
1218place of the symbol, or a function that is called to process the
1219arguments. The specification may be defined with @code{def-edebug-spec}
1220just as for macros. See the @code{defun} example below.
1221
1222Otherwise, the symbol should be a predicate. The predicate is called
1223with the argument and the specification fails if the predicate returns
1224@code{nil}. In either case, that argument is not instrumented.
1225
73804d4b
RS
1226Some suitable predicates include @code{symbolp}, @code{integerp},
1227@code{stringp}, @code{vectorp}, and @code{atom}.
73804d4b
RS
1228
1229@item [@var{elements}@dots{}]
1230@cindex [@dots{}] (Edebug)
1231A vector of elements groups the elements into a single @dfn{group
1232specification}. Its meaning has nothing to do with vectors.
1233
1234@item "@var{string}"
1235The argument should be a symbol named @var{string}. This specification
1236is equivalent to the quoted symbol, @code{'@var{symbol}}, where the name
1237of @var{symbol} is the @var{string}, but the string form is preferred.
1238
73804d4b
RS
1239@item (vector @var{elements}@dots{})
1240The argument should be a vector whose elements must match the
1241@var{elements} in the specification. See the backquote example below.
1242
1243@item (@var{elements}@dots{})
1244Any other list is a @dfn{sublist specification} and the argument must be
1245a list whose elements match the specification @var{elements}.
1246
1247@cindex dotted lists (Edebug)
1248A sublist specification may be a dotted list and the corresponding list
1249argument may then be a dotted list. Alternatively, the last @sc{cdr} of a
1250dotted list specification may be another sublist specification (via a
1251grouping or an indirect specification, e.g. @code{(spec . [(more
1252specs@dots{})])}) whose elements match the non-dotted list arguments.
1253This is useful in recursive specifications such as in the backquote
1254example below. Also see the description of a @code{nil} specification
1255above for terminating such recursion.
1256
87b2d5ff
RS
1257Note that a sublist specification written as @code{(specs . nil)}
1258is equivalent to @code{(specs)}, and @code{(specs .
1259(sublist-elements@dots{}))} is equivalent to @code{(specs
73804d4b
RS
1260sublist-elements@dots{})}.
1261@end table
1262
1263@c Need to document extensions with &symbol and :symbol
1264
1265Here is a list of additional specifications that may only appear after
1266@code{&define}. See the @code{defun} example below.
1267
1268@table @code
1269@item name
1270The argument, a symbol, is the name of the defining form.
1271
1272A defining form is not required to have a name field; and it may have
1273multiple name fields.
1274
1275@item :name
1276This construct does not actually match an argument. The element
1277following @code{:name} should be a symbol; it is used as an additional
1278name component for the definition. You can use this to add a unique,
1279static component to the name of the definition. It may be used more
1280than once.
1281
1282@item arg
1283The argument, a symbol, is the name of an argument of the defining form.
1284However, lambda list keywords (symbols starting with @samp{@code{&}})
87b2d5ff 1285are not allowed.
73804d4b
RS
1286
1287@item lambda-list
1288@cindex lambda-list (Edebug)
1289This matches a lambda list---the argument list of a lambda expression.
73804d4b
RS
1290
1291@item def-body
1292The argument is the body of code in a definition. This is like
1293@code{body}, described above, but a definition body must be instrumented
1294with a different Edebug call that looks up information associated with
1295the definition. Use @code{def-body} for the highest level list of forms
1296within the definition.
1297
1298@item def-form
1299The argument is a single, highest-level form in a definition. This is
1300like @code{def-body}, except use this to match a single form rather than
1301a list of forms. As a special case, @code{def-form} also means that
1302tracing information is not output when the form is executed. See the
1303@code{interactive} example below.
1304@end table
1305
1306@node Backtracking
1307@subsubsection Backtracking
1308
1309@cindex backtracking
1310@cindex syntax error (Edebug)
1311If a specification fails to match at some point, this does not
1312necessarily mean a syntax error will be signaled; instead,
1313@dfn{backtracking} will take place until all alternatives have been
1314exhausted. Eventually every element of the argument list must be
1315matched by some element in the specification, and every required element
1316in the specification must match some argument.
1317
1318Backtracking is disabled for the remainder of a sublist or group when
1319certain conditions occur, described below. Backtracking is reenabled
1320when a new alternative is established by @code{&optional}, @code{&rest},
1321or @code{&or}. It is also reenabled initially when processing a
1322sublist or group specification or an indirect specification.
1323
1324You might want to disable backtracking to commit to some alternative so
1325that Edebug can provide a more specific syntax error message. Normally,
1326if no alternative matches, Edebug reports that none matched, but if one
1327alternative is committed to, Edebug can report how it failed to match.
1328
1329First, backtracking is disabled while matching any of the form
1330specifications (i.e. @code{form}, @code{body}, @code{def-form}, and
1331@code{def-body}). These specifications will match any form so any error
1332must be in the form itself rather than at a higher level.
1333
1334Second, backtracking is disabled after successfully matching a quoted
1335symbol or string specification, since this usually indicates a
1336recognized construct. If you have a set of alternative constructs that
1337all begin with the same symbol, you can usually work around this
1338constraint by factoring the symbol out of the alternatives, e.g.,
1339@code{["foo" &or [first case] [second case] ...]}.
1340
1341Third, backtracking may be explicitly disabled by using the
1342@code{gate} specification. This is useful when you know that
1343no higher alternatives may apply.
1344
73804d4b
RS
1345@node Specification Examples
1346@subsubsection Specification Examples
1347
1348It may be easier to understand Edebug specifications by studying
1349the examples provided here.
1350
1351A @code{let} special form has a sequence of bindings and a body. Each
1352of the bindings is either a symbol or a sublist with a symbol and
1353optional value. In the specification below, notice the @code{gate}
1354inside of the sublist to prevent backtracking once a sublist is found.
1355
1356@example
1357(def-edebug-spec let
1358 ((&rest
1359 &or symbolp (gate symbolp &optional form))
1360 body))
1361@end example
1362
1363Edebug uses the following specifications for @code{defun} and
1364@code{defmacro} and the associated argument list and @code{interactive}
1365specifications. It is necessary to handle interactive forms specially
1366since an expression argument it is actually evaluated outside of the
1367function body.
1368
87b2d5ff
RS
1369@smallexample
1370(def-edebug-spec defmacro defun) ; @r{Indirect ref to @code{defun} spec.}
73804d4b
RS
1371(def-edebug-spec defun
1372 (&define name lambda-list
87b2d5ff 1373 [&optional stringp] ; @r{Match the doc string, if present.}
73804d4b
RS
1374 [&optional ("interactive" interactive)]
1375 def-body))
1376
1377(def-edebug-spec lambda-list
1378 (([&rest arg]
1379 [&optional ["&optional" arg &rest arg]]
1380 &optional ["&rest" arg]
1381 )))
1382
1383(def-edebug-spec interactive
1384 (&optional &or stringp def-form)) ; @r{Notice: @code{def-form}}
87b2d5ff 1385@end smallexample
73804d4b
RS
1386
1387The specification for backquote below illustrates how to match
1388dotted lists and use @code{nil} to terminate recursion. It also
1389illustrates how components of a vector may be matched. (The actual
1390specification defined by Edebug does not support dotted lists because
1391doing so causes very deep recursion that could fail.)
1392
87b2d5ff
RS
1393@smallexample
1394(def-edebug-spec ` (backquote-form)) ; @r{Alias just for clarity.}
73804d4b
RS
1395
1396(def-edebug-spec backquote-form
1397 (&or ([&or "," ",@@"] &or ("quote" backquote-form) form)
1398 (backquote-form . [&or nil backquote-form])
1399 (vector &rest backquote-form)
1400 sexp))
87b2d5ff 1401@end smallexample
73804d4b
RS
1402
1403
1404@node Edebug Options
1405@subsection Edebug Options
1406
1407 These options affect the behavior of Edebug:
1408
1409@defopt edebug-setup-hook
1410Functions to call before Edebug is used. Each time it is set to a new
1411value, Edebug will call those functions once and then
1412@code{edebug-setup-hook} is reset to @code{nil}. You could use this to
1413load up Edebug specifications associated with a package you are using
1414but only when you also use Edebug.
1415@xref{Instrumenting}.
1416@end defopt
1417
1418@defopt edebug-all-defs
1419If this is non-@code{nil}, normal evaluation of defining forms such as
1420@code{defun} and @code{defmacro} instruments them for Edebug. This
87b2d5ff
RS
1421applies to @code{eval-defun}, @code{eval-region}, @code{eval-buffer},
1422and @code{eval-current-buffer}.
1423
1424Use the command @kbd{M-x edebug-all-defs} to toggle the value of this
1425option. @xref{Instrumenting}.
73804d4b
RS
1426@end defopt
1427
1428@defopt edebug-all-forms
87b2d5ff
RS
1429If this is non-@code{nil}, the commands @code{eval-defun},
1430@code{eval-region}, @code{eval-buffer}, and @code{eval-current-buffer}
1431instrument all forms, even those that don't define anything.
1432This doesn't apply to loading or evaluations in the minibuffer.
73804d4b
RS
1433
1434Use the command @kbd{M-x edebug-all-forms} to toggle the value of this
87b2d5ff 1435option. @xref{Instrumenting}.
73804d4b
RS
1436@end defopt
1437
1438@defopt edebug-save-windows
1439If this is non-@code{nil}, Edebug saves and restores the window
1440configuration. That takes some time, so if your program does not care
1441what happens to the window configurations, it is better to set this
1442variable to @code{nil}.
1443
1444If the value is a list, only the listed windows are saved and
1445restored.
1446
1447You can use the @kbd{W} command in Edebug to change this variable
1448interactively. @xref{Edebug Display Update}.
1449@end defopt
1450
1451@defopt edebug-save-displayed-buffer-points
87b2d5ff
RS
1452If this is non-@code{nil}, Edebug saves and restores point in all
1453displayed buffers.
73804d4b
RS
1454
1455Saving and restoring point in other buffers is necessary if you are
1456debugging code that changes the point of a buffer which is displayed in
1457a non-selected window. If Edebug or the user then selects the window,
87b2d5ff 1458point in that buffer will move to the window's value of point.
73804d4b
RS
1459
1460Saving and restoring point in all buffers is expensive, since it
1461requires selecting each window twice, so enable this only if you need
1462it. @xref{Edebug Display Update}.
1463@end defopt
1464
1465@defopt edebug-initial-mode
1466If this variable is non-@code{nil}, it specifies the initial execution
1467mode for Edebug when it is first activated. Possible values are
1468@code{step}, @code{next}, @code{go}, @code{Go-nonstop}, @code{trace},
1469@code{Trace-fast}, @code{continue}, and @code{Continue-fast}.
1470
1471The default value is @code{step}.
1472@xref{Edebug Execution Modes}.
1473@end defopt
1474
1475@defopt edebug-trace
1476@findex edebug-print-trace-before
1477@findex edebug-print-trace-after
1478Non-@code{nil} means display a trace of function entry and exit.
1479Tracing output is displayed in a buffer named @samp{*edebug-trace*}, one
1480function entry or exit per line, indented by the recursion level.
1481
1482The default value is @code{nil}.
1483
87b2d5ff 1484Also see @code{edebug-tracing}, in @xref{Trace Buffer}.
73804d4b
RS
1485@end defopt
1486
1487@defopt edebug-test-coverage
1488If non-@code{nil}, Edebug tests coverage of all expressions debugged.
1489This is done by comparing the result of each expression
1490with the previous result. Coverage is considered OK if two different
1491results are found. So to sufficiently test the coverage of your code,
1492try to execute it under conditions that evaluate all expressions more
1493than once, and produce different results for each expression.
1494
1495Use @kbd{M-x edebug-display-freq-count} to display the frequency count
1496and coverage information for a definition.
1497@xref{Coverage Testing}.
1498@end defopt
1499
1500@defopt edebug-continue-kbd-macro
1501If non-@code{nil}, continue defining or executing any keyboard macro
1502that is executing outside of Edebug. Use this with caution since it is not
1503debugged.
1504@xref{Edebug Execution Modes}.
1505@end defopt
1506
1507@defopt edebug-print-length
1508If non-@code{nil}, bind @code{print-length} to this while printing
1509results in Edebug. The default value is @code{50}.
1510@xref{Printing in Edebug}.
1511@end defopt
1512
1513@defopt edebug-print-level
1514If non-@code{nil}, bind @code{print-level} to this while printing
1515results in Edebug. The default value is @code{50}.
1516@end defopt
1517
1518@defopt edebug-print-circle
1519If non-@code{nil}, bind @code{print-circle} to this while printing
1520results in Edebug. The default value is @code{nil}.
1521@end defopt
1522
1523@defopt edebug-on-error
1524Edebug binds @code{debug-on-error} to this value, if
1525@code{debug-on-error} was previously @code{nil}. @xref{Trapping
1526Errors}.
1527@end defopt
1528
1529@defopt edebug-on-quit
1530Edebug binds @code{debug-on-quit} to this value, if
1531@code{debug-on-quit} was previously @code{nil}. @xref{Trapping
1532Errors}.
1533@end defopt
1534
1535 If you change the values of @code{edebug-on-error} or
1536@code{edebug-on-quit} while Edebug is active, their values won't be used
87b2d5ff
RS
1537until the @emph{next} time Edebug is invoked via a new command.
1538@c Not necessarily a deeper command level.
1539@c A new command is not precisely true, but that is close enough -- dan
73804d4b
RS
1540
1541@defopt edebug-global-break-condition
1542If non-@code{nil}, an expression to test for at every stop point.
1543If the result is non-nil, then break. Errors are ignored.
1544@xref{Global Break Condition}.
1545@end defopt