]> code.delx.au - gnu-emacs/blob - lispref/debugging.texi
*** empty log message ***
[gnu-emacs] / lispref / debugging.texi
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1998 Free Software Foundation, Inc.
4 @c See the file elisp.texi for copying conditions.
5 @setfilename ../info/debugging
6 @node Debugging, Read and Print, Advising Functions, Top
7 @chapter Debugging Lisp Programs
8
9 There are three ways to investigate a problem in an Emacs Lisp program,
10 depending on what you are doing with the program when the problem appears.
11
12 @itemize @bullet
13 @item
14 If the problem occurs when you run the program, you can use a Lisp
15 debugger to investigate what is happening during execution. In addition
16 to the ordinary debugger, Emacs comes with a source level debugger,
17 Edebug. This chapter describes both of them.
18
19 @item
20 If the problem is syntactic, so that Lisp cannot even read the program,
21 you can use the Emacs facilities for editing Lisp to localize it.
22
23 @item
24 If the problem occurs when trying to compile the program with the byte
25 compiler, you need to know how to examine the compiler's input buffer.
26 @end itemize
27
28 @menu
29 * Debugger:: How the Emacs Lisp debugger is implemented.
30 * Edebug:: A source-level Emacs Lisp debugger.
31 * Syntax Errors:: How to find syntax errors.
32 * Compilation Errors:: How to find errors that show up in byte compilation.
33 @end menu
34
35 Another useful debugging tool is the dribble file. When a dribble
36 file is open, Emacs copies all keyboard input characters to that file.
37 Afterward, you can examine the file to find out what input was used.
38 @xref{Terminal Input}.
39
40 For debugging problems in terminal descriptions, the
41 @code{open-termscript} function can be useful. @xref{Terminal Output}.
42
43 @node Debugger
44 @section The Lisp Debugger
45 @cindex debugger
46 @cindex Lisp debugger
47 @cindex break
48
49 The ordinary @dfn{Lisp debugger} provides the ability to suspend
50 evaluation of a form. While evaluation is suspended (a state that is
51 commonly known as a @dfn{break}), you may examine the run time stack,
52 examine the values of local or global variables, or change those values.
53 Since a break is a recursive edit, all the usual editing facilities of
54 Emacs are available; you can even run programs that will enter the
55 debugger recursively. @xref{Recursive Editing}.
56
57 @menu
58 * Error Debugging:: Entering the debugger when an error happens.
59 * Infinite Loops:: Stopping and debugging a program that doesn't exit.
60 * Function Debugging:: Entering it when a certain function is called.
61 * Explicit Debug:: Entering it at a certain point in the program.
62 * Using Debugger:: What the debugger does; what you see while in it.
63 * Debugger Commands:: Commands used while in the debugger.
64 * Invoking the Debugger:: How to call the function @code{debug}.
65 * Internals of Debugger:: Subroutines of the debugger, and global variables.
66 @end menu
67
68 @node Error Debugging
69 @subsection Entering the Debugger on an Error
70 @cindex error debugging
71 @cindex debugging errors
72
73 The most important time to enter the debugger is when a Lisp error
74 happens. This allows you to investigate the immediate causes of the
75 error.
76
77 However, entry to the debugger is not a normal consequence of an
78 error. Many commands frequently cause Lisp errors when invoked
79 inappropriately (such as @kbd{C-f} at the end of the buffer), and during
80 ordinary editing it would be very inconvenient to enter the debugger
81 each time this happens. So if you want errors to enter the debugger, set
82 the variable @code{debug-on-error} to non-@code{nil}. (The command
83 @code{toggle-debug-on-error} provides an easy way to do this.)
84
85 @defopt debug-on-error
86 This variable determines whether the debugger is called when an error is
87 signaled and not handled. If @code{debug-on-error} is @code{t}, all
88 kinds of errors call the debugger (except those listed in
89 @code{debug-ignored-errors}). If it is @code{nil}, none call the
90 debugger.
91
92 The value can also be a list of error conditions that should call the
93 debugger. For example, if you set it to the list
94 @code{(void-variable)}, then only errors about a variable that has no
95 value invoke the debugger.
96
97 When this variable is non-@code{nil}, Emacs does not create an error
98 handler around process filter functions and sentinels. Therefore,
99 errors in these functions also invoke the debugger. @xref{Processes}.
100 @end defopt
101
102 @defopt debug-ignored-errors
103 This variable specifies certain kinds of errors that should not enter
104 the debugger. Its value is a list of error condition symbols and/or
105 regular expressions. If the error has any of those condition symbols,
106 or if the error message matches any of the regular expressions, then
107 that error does not enter the debugger, regardless of the value of
108 @code{debug-on-error}.
109
110 The normal value of this variable lists several errors that happen often
111 during editing but rarely result from bugs in Lisp programs. However,
112 ``rarely'' is not ``never''; if your program fails with an error that
113 matches this list, you will need to change this list in order to debug
114 the error. The easiest way is usually to set
115 @code{debug-ignored-errors} to @code{nil}.
116 @end defopt
117
118 @defopt debug-on-signal
119 Normally, errors that are caught by @code{condition-case} never run the
120 debugger, even if @code{debug-on-error} is non-@code{nil}. In other
121 words, @code{condition-case} gets a chance to handle the error before
122 the debugger gets a chance.
123
124 If you set @code{debug-on-signal} to a non-@code{nil} value, then the
125 debugger gets the first chance at every error; an error will invoke the
126 debugger regardless of any @code{condition-case}, if it fits the
127 criteria specified by the values of @code{debug-on-error} and
128 @code{debug-ignored-errors}.
129
130 @strong{Warning:} This variable is strong medicine! Various parts of
131 Emacs handle errors in the normal course of affairs, and you may not
132 even realize that errors happen there. If you set
133 @code{debug-on-signal} to a non-@code{nil} value, those errors will
134 enter the debugger.
135
136 @strong{Warning:} @code{debug-on-signal} has no effect when
137 @code{debug-on-error} is @code{nil}.
138 @end defopt
139
140 To debug an error that happens during loading of the @file{.emacs}
141 file, use the option @samp{--debug-init}, which binds
142 @code{debug-on-error} to @code{t} while loading @file{.emacs}, and
143 bypasses the @code{condition-case} which normally catches errors in the
144 init file.
145
146 If your @file{.emacs} file sets @code{debug-on-error}, the effect may
147 not last past the end of loading @file{.emacs}. (This is an undesirable
148 byproduct of the code that implements the @samp{--debug-init} command
149 line option.) The best way to make @file{.emacs} set
150 @code{debug-on-error} permanently is with @code{after-init-hook}, like
151 this:
152
153 @example
154 (add-hook 'after-init-hook
155 '(lambda () (setq debug-on-error t)))
156 @end example
157
158 @node Infinite Loops
159 @subsection Debugging Infinite Loops
160 @cindex infinite loops
161 @cindex loops, infinite
162 @cindex quitting from infinite loop
163 @cindex stopping an infinite loop
164
165 When a program loops infinitely and fails to return, your first
166 problem is to stop the loop. On most operating systems, you can do this
167 with @kbd{C-g}, which causes quit.
168
169 Ordinary quitting gives no information about why the program was
170 looping. To get more information, you can set the variable
171 @code{debug-on-quit} to non-@code{nil}. Quitting with @kbd{C-g} is not
172 considered an error, and @code{debug-on-error} has no effect on the
173 handling of @kbd{C-g}. Likewise, @code{debug-on-quit} has no effect on
174 errors.
175
176 Once you have the debugger running in the middle of the infinite loop,
177 you can proceed from the debugger using the stepping commands. If you
178 step through the entire loop, you will probably get enough information
179 to solve the problem.
180
181 @defopt debug-on-quit
182 This variable determines whether the debugger is called when @code{quit}
183 is signaled and not handled. If @code{debug-on-quit} is non-@code{nil},
184 then the debugger is called whenever you quit (that is, type @kbd{C-g}).
185 If @code{debug-on-quit} is @code{nil}, then the debugger is not called
186 when you quit. @xref{Quitting}.
187 @end defopt
188
189 @node Function Debugging
190 @subsection Entering the Debugger on a Function Call
191 @cindex function call debugging
192 @cindex debugging specific functions
193
194 To investigate a problem that happens in the middle of a program, one
195 useful technique is to enter the debugger whenever a certain function is
196 called. You can do this to the function in which the problem occurs,
197 and then step through the function, or you can do this to a function
198 called shortly before the problem, step quickly over the call to that
199 function, and then step through its caller.
200
201 @deffn Command debug-on-entry function-name
202 This function requests @var{function-name} to invoke the debugger each time
203 it is called. It works by inserting the form @code{(debug 'debug)} into
204 the function definition as the first form.
205
206 Any function defined as Lisp code may be set to break on entry,
207 regardless of whether it is interpreted code or compiled code. If the
208 function is a command, it will enter the debugger when called from Lisp
209 and when called interactively (after the reading of the arguments). You
210 can't debug primitive functions (i.e., those written in C) this way.
211
212 When @code{debug-on-entry} is called interactively, it prompts for
213 @var{function-name} in the minibuffer. If the function is already set
214 up to invoke the debugger on entry, @code{debug-on-entry} does nothing.
215 @code{debug-on-entry} always returns @var{function-name}.
216
217 @strong{Note:} if you redefine a function after using
218 @code{debug-on-entry} on it, the code to enter the debugger is discarded
219 by the redefinition. In effect, redefining the function cancels
220 the break-on-entry feature for that function.
221
222 @example
223 @group
224 (defun fact (n)
225 (if (zerop n) 1
226 (* n (fact (1- n)))))
227 @result{} fact
228 @end group
229 @group
230 (debug-on-entry 'fact)
231 @result{} fact
232 @end group
233 @group
234 (fact 3)
235 @end group
236
237 @group
238 ------ Buffer: *Backtrace* ------
239 Entering:
240 * fact(3)
241 eval-region(4870 4878 t)
242 byte-code("...")
243 eval-last-sexp(nil)
244 (let ...)
245 eval-insert-last-sexp(nil)
246 * call-interactively(eval-insert-last-sexp)
247 ------ Buffer: *Backtrace* ------
248 @end group
249
250 @group
251 (symbol-function 'fact)
252 @result{} (lambda (n)
253 (debug (quote debug))
254 (if (zerop n) 1 (* n (fact (1- n)))))
255 @end group
256 @end example
257 @end deffn
258
259 @deffn Command cancel-debug-on-entry function-name
260 This function undoes the effect of @code{debug-on-entry} on
261 @var{function-name}. When called interactively, it prompts for
262 @var{function-name} in the minibuffer. If @var{function-name} is
263 @code{nil} or the empty string, it cancels break-on-entry for all
264 functions.
265
266 Calling @code{cancel-debug-on-entry} does nothing to a function which is
267 not currently set up to break on entry. It always returns
268 @var{function-name}.
269 @end deffn
270
271 @node Explicit Debug
272 @subsection Explicit Entry to the Debugger
273
274 You can cause the debugger to be called at a certain point in your
275 program by writing the expression @code{(debug)} at that point. To do
276 this, visit the source file, insert the text @samp{(debug)} at the
277 proper place, and type @kbd{C-M-x}. @strong{Warning:} if you do this
278 for temporary debugging purposes, be sure to undo this insertion before
279 you save the file!
280
281 The place where you insert @samp{(debug)} must be a place where an
282 additional form can be evaluated and its value ignored. (If the value
283 of @code{(debug)} isn't ignored, it will alter the execution of the
284 program!) The most common suitable places are inside a @code{progn} or
285 an implicit @code{progn} (@pxref{Sequencing}).
286
287 @node Using Debugger
288 @subsection Using the Debugger
289
290 When the debugger is entered, it displays the previously selected
291 buffer in one window and a buffer named @samp{*Backtrace*} in another
292 window. The backtrace buffer contains one line for each level of Lisp
293 function execution currently going on. At the beginning of this buffer
294 is a message describing the reason that the debugger was invoked (such
295 as the error message and associated data, if it was invoked due to an
296 error).
297
298 The backtrace buffer is read-only and uses a special major mode,
299 Debugger mode, in which letters are defined as debugger commands. The
300 usual Emacs editing commands are available; thus, you can switch windows
301 to examine the buffer that was being edited at the time of the error,
302 switch buffers, visit files, or do any other sort of editing. However,
303 the debugger is a recursive editing level (@pxref{Recursive Editing})
304 and it is wise to go back to the backtrace buffer and exit the debugger
305 (with the @kbd{q} command) when you are finished with it. Exiting
306 the debugger gets out of the recursive edit and kills the backtrace
307 buffer.
308
309 @cindex current stack frame
310 The backtrace buffer shows you the functions that are executing and
311 their argument values. It also allows you to specify a stack frame by
312 moving point to the line describing that frame. (A stack frame is the
313 place where the Lisp interpreter records information about a particular
314 invocation of a function.) The frame whose line point is on is
315 considered the @dfn{current frame}. Some of the debugger commands
316 operate on the current frame.
317
318 The debugger itself must be run byte-compiled, since it makes
319 assumptions about how many stack frames are used for the debugger
320 itself. These assumptions are false if the debugger is running
321 interpreted.
322
323 @need 3000
324
325 @node Debugger Commands
326 @subsection Debugger Commands
327 @cindex debugger command list
328
329 Inside the debugger (in Debugger mode), these special commands are
330 available in addition to the usual cursor motion commands. (Keep in
331 mind that all the usual facilities of Emacs, such as switching windows
332 or buffers, are still available.)
333
334 The most important use of debugger commands is for stepping through
335 code, so that you can see how control flows. The debugger can step
336 through the control structures of an interpreted function, but cannot do
337 so in a byte-compiled function. If you would like to step through a
338 byte-compiled function, replace it with an interpreted definition of the
339 same function. (To do this, visit the source for the function and type
340 @kbd{C-M-x} on its definition.)
341
342 Here is a list of Debugger mode commands:
343
344 @table @kbd
345 @item c
346 Exit the debugger and continue execution. When continuing is possible,
347 it resumes execution of the program as if the debugger had never been
348 entered (aside from any side-effects that you caused by changing
349 variable values or data structures while inside the debugger).
350
351 Continuing is possible after entry to the debugger due to function entry
352 or exit, explicit invocation, or quitting. You cannot continue if the
353 debugger was entered because of an error.
354
355 @item d
356 Continue execution, but enter the debugger the next time any Lisp
357 function is called. This allows you to step through the
358 subexpressions of an expression, seeing what values the subexpressions
359 compute, and what else they do.
360
361 The stack frame made for the function call which enters the debugger in
362 this way will be flagged automatically so that the debugger will be
363 called again when the frame is exited. You can use the @kbd{u} command
364 to cancel this flag.
365
366 @item b
367 Flag the current frame so that the debugger will be entered when the
368 frame is exited. Frames flagged in this way are marked with stars
369 in the backtrace buffer.
370
371 @item u
372 Don't enter the debugger when the current frame is exited. This
373 cancels a @kbd{b} command on that frame. The visible effect is to
374 remove the star from the line in the backtrace buffer.
375
376 @item e
377 Read a Lisp expression in the minibuffer, evaluate it, and print the
378 value in the echo area. The debugger alters certain important
379 variables, and the current buffer, as part of its operation; @kbd{e}
380 temporarily restores their values from outside the debugger, so you can
381 examine and change them. This makes the debugger more transparent. By
382 contrast, @kbd{M-:} does nothing special in the debugger; it shows you
383 the variable values within the debugger.
384
385 @item R
386 Like @kbd{e}, but also save the result of evaluation in the
387 buffer @samp{*Debugger-record*}.
388
389 @item q
390 Terminate the program being debugged; return to top-level Emacs
391 command execution.
392
393 If the debugger was entered due to a @kbd{C-g} but you really want
394 to quit, and not debug, use the @kbd{q} command.
395
396 @item r
397 Return a value from the debugger. The value is computed by reading an
398 expression with the minibuffer and evaluating it.
399
400 The @kbd{r} command is useful when the debugger was invoked due to exit
401 from a Lisp call frame (as requested with @kbd{b} or by entering the
402 frame with @kbd{d}); then the value specified in the @kbd{r} command is
403 used as the value of that frame. It is also useful if you call
404 @code{debug} and use its return value. Otherwise, @kbd{r} has the same
405 effect as @kbd{c}, and the specified return value does not matter.
406
407 You can't use @kbd{r} when the debugger was entered due to an error.
408 @end table
409
410 @node Invoking the Debugger
411 @subsection Invoking the Debugger
412
413 Here we describe in full detail the function @code{debug} that is used
414 to invoke the debugger.
415
416 @defun debug &rest debugger-args
417 This function enters the debugger. It switches buffers to a buffer
418 named @samp{*Backtrace*} (or @samp{*Backtrace*<2>} if it is the second
419 recursive entry to the debugger, etc.), and fills it with information
420 about the stack of Lisp function calls. It then enters a recursive
421 edit, showing the backtrace buffer in Debugger mode.
422
423 The Debugger mode @kbd{c} and @kbd{r} commands exit the recursive edit;
424 then @code{debug} switches back to the previous buffer and returns to
425 whatever called @code{debug}. This is the only way the function
426 @code{debug} can return to its caller.
427
428 The use of the @var{debugger-args} is that @code{debug} displays the
429 rest of its arguments at the top of the @samp{*Backtrace*} buffer, so
430 that the user can see them. Except as described below, this is the
431 @emph{only} way these arguments are used.
432
433 However, certain values for first argument to @code{debug} have a
434 special significance. (Normally, these values are used only by the
435 internals of Emacs, and not by programmers calling @code{debug}.) Here
436 is a table of these special values:
437
438 @table @code
439 @item lambda
440 @cindex @code{lambda} in debug
441 A first argument of @code{lambda} means @code{debug} was called because
442 of entry to a function when @code{debug-on-next-call} was
443 non-@code{nil}. The debugger displays @samp{Entering:} as a line of
444 text at the top of the buffer.
445
446 @item debug
447 @code{debug} as first argument indicates a call to @code{debug} because
448 of entry to a function that was set to debug on entry. The debugger
449 displays @samp{Entering:}, just as in the @code{lambda} case. It also
450 marks the stack frame for that function so that it will invoke the
451 debugger when exited.
452
453 @item t
454 When the first argument is @code{t}, this indicates a call to
455 @code{debug} due to evaluation of a list form when
456 @code{debug-on-next-call} is non-@code{nil}. The debugger displays the
457 following as the top line in the buffer:
458
459 @smallexample
460 Beginning evaluation of function call form:
461 @end smallexample
462
463 @item exit
464 When the first argument is @code{exit}, it indicates the exit of a stack
465 frame previously marked to invoke the debugger on exit. The second
466 argument given to @code{debug} in this case is the value being returned
467 from the frame. The debugger displays @samp{Return value:} in the top
468 line of the buffer, followed by the value being returned.
469
470 @item error
471 @cindex @code{error} in debug
472 When the first argument is @code{error}, the debugger indicates that
473 it is being entered because an error or @code{quit} was signaled and not
474 handled, by displaying @samp{Signaling:} followed by the error signaled
475 and any arguments to @code{signal}. For example,
476
477 @example
478 @group
479 (let ((debug-on-error t))
480 (/ 1 0))
481 @end group
482
483 @group
484 ------ Buffer: *Backtrace* ------
485 Signaling: (arith-error)
486 /(1 0)
487 ...
488 ------ Buffer: *Backtrace* ------
489 @end group
490 @end example
491
492 If an error was signaled, presumably the variable
493 @code{debug-on-error} is non-@code{nil}. If @code{quit} was signaled,
494 then presumably the variable @code{debug-on-quit} is non-@code{nil}.
495
496 @item nil
497 Use @code{nil} as the first of the @var{debugger-args} when you want
498 to enter the debugger explicitly. The rest of the @var{debugger-args}
499 are printed on the top line of the buffer. You can use this feature to
500 display messages---for example, to remind yourself of the conditions
501 under which @code{debug} is called.
502 @end table
503 @end defun
504
505 @node Internals of Debugger
506 @subsection Internals of the Debugger
507
508 This section describes functions and variables used internally by the
509 debugger.
510
511 @defvar debugger
512 The value of this variable is the function to call to invoke the
513 debugger. Its value must be a function of any number of arguments (or,
514 more typically, the name of a function). Presumably this function will
515 enter some kind of debugger. The default value of the variable is
516 @code{debug}.
517
518 The first argument that Lisp hands to the function indicates why it
519 was called. The convention for arguments is detailed in the description
520 of @code{debug}.
521 @end defvar
522
523 @deffn Command backtrace
524 @cindex run time stack
525 @cindex call stack
526 This function prints a trace of Lisp function calls currently active.
527 This is the function used by @code{debug} to fill up the
528 @samp{*Backtrace*} buffer. It is written in C, since it must have access
529 to the stack to determine which function calls are active. The return
530 value is always @code{nil}.
531
532 In the following example, a Lisp expression calls @code{backtrace}
533 explicitly. This prints the backtrace to the stream
534 @code{standard-output}: in this case, to the buffer
535 @samp{backtrace-output}. Each line of the backtrace represents one
536 function call. The line shows the values of the function's arguments if
537 they are all known. If they are still being computed, the line says so.
538 The arguments of special forms are elided.
539
540 @smallexample
541 @group
542 (with-output-to-temp-buffer "backtrace-output"
543 (let ((var 1))
544 (save-excursion
545 (setq var (eval '(progn
546 (1+ var)
547 (list 'testing (backtrace))))))))
548
549 @result{} nil
550 @end group
551
552 @group
553 ----------- Buffer: backtrace-output ------------
554 backtrace()
555 (list ...computing arguments...)
556 @end group
557 (progn ...)
558 eval((progn (1+ var) (list (quote testing) (backtrace))))
559 (setq ...)
560 (save-excursion ...)
561 (let ...)
562 (with-output-to-temp-buffer ...)
563 eval-region(1973 2142 #<buffer *scratch*>)
564 byte-code("... for eval-print-last-sexp ...")
565 @group
566 eval-print-last-sexp(nil)
567 * call-interactively(eval-print-last-sexp)
568 ----------- Buffer: backtrace-output ------------
569 @end group
570 @end smallexample
571
572 The character @samp{*} indicates a frame whose debug-on-exit flag is
573 set.
574 @end deffn
575
576 @ignore @c Not worth mentioning
577 @defopt stack-trace-on-error
578 @cindex stack trace
579 This variable controls whether Lisp automatically displays a
580 backtrace buffer after every error that is not handled. A quit signal
581 counts as an error for this variable. If it is non-@code{nil} then a
582 backtrace is shown in a pop-up buffer named @samp{*Backtrace*} on every
583 error. If it is @code{nil}, then a backtrace is not shown.
584
585 When a backtrace is shown, that buffer is not selected. If either
586 @code{debug-on-quit} or @code{debug-on-error} is also non-@code{nil}, then
587 a backtrace is shown in one buffer, and the debugger is popped up in
588 another buffer with its own backtrace.
589
590 We consider this feature to be obsolete and superseded by the debugger
591 itself.
592 @end defopt
593 @end ignore
594
595 @defvar debug-on-next-call
596 @cindex @code{eval}, and debugging
597 @cindex @code{apply}, and debugging
598 @cindex @code{funcall}, and debugging
599 If this variable is non-@code{nil}, it says to call the debugger before
600 the next @code{eval}, @code{apply} or @code{funcall}. Entering the
601 debugger sets @code{debug-on-next-call} to @code{nil}.
602
603 The @kbd{d} command in the debugger works by setting this variable.
604 @end defvar
605
606 @defun backtrace-debug level flag
607 This function sets the debug-on-exit flag of the stack frame @var{level}
608 levels down the stack, giving it the value @var{flag}. If @var{flag} is
609 non-@code{nil}, this will cause the debugger to be entered when that
610 frame later exits. Even a nonlocal exit through that frame will enter
611 the debugger.
612
613 This function is used only by the debugger.
614 @end defun
615
616 @defvar command-debug-status
617 This variable records the debugging status of the current interactive
618 command. Each time a command is called interactively, this variable is
619 bound to @code{nil}. The debugger can set this variable to leave
620 information for future debugger invocations during the same command
621 invocation.
622
623 The advantage, for the debugger, of using this variable rather than an
624 ordinary global variable is that the data will never carry over to a
625 subsequent command invocation.
626 @end defvar
627
628 @defun backtrace-frame frame-number
629 The function @code{backtrace-frame} is intended for use in Lisp
630 debuggers. It returns information about what computation is happening
631 in the stack frame @var{frame-number} levels down.
632
633 If that frame has not evaluated the arguments yet (or is a special
634 form), the value is @code{(nil @var{function} @var{arg-forms}@dots{})}.
635
636 If that frame has evaluated its arguments and called its function
637 already, the value is @code{(t @var{function}
638 @var{arg-values}@dots{})}.
639
640 In the return value, @var{function} is whatever was supplied as the
641 @sc{car} of the evaluated list, or a @code{lambda} expression in the
642 case of a macro call. If the function has a @code{&rest} argument, that
643 is represented as the tail of the list @var{arg-values}.
644
645 If @var{frame-number} is out of range, @code{backtrace-frame} returns
646 @code{nil}.
647 @end defun
648
649 @include edebug.texi
650
651 @node Syntax Errors
652 @section Debugging Invalid Lisp Syntax
653
654 The Lisp reader reports invalid syntax, but cannot say where the real
655 problem is. For example, the error ``End of file during parsing'' in
656 evaluating an expression indicates an excess of open parentheses (or
657 square brackets). The reader detects this imbalance at the end of the
658 file, but it cannot figure out where the close parenthesis should have
659 been. Likewise, ``Invalid read syntax: ")"'' indicates an excess close
660 parenthesis or missing open parenthesis, but does not say where the
661 missing parenthesis belongs. How, then, to find what to change?
662
663 If the problem is not simply an imbalance of parentheses, a useful
664 technique is to try @kbd{C-M-e} at the beginning of each defun, and see
665 if it goes to the place where that defun appears to end. If it does
666 not, there is a problem in that defun.
667
668 However, unmatched parentheses are the most common syntax errors in
669 Lisp, and we can give further advice for those cases. (In addition,
670 just moving point through the code with Show Paren mode enabled might
671 find the mismatch.)
672
673 @menu
674 * Excess Open:: How to find a spurious open paren or missing close.
675 * Excess Close:: How to find a spurious close paren or missing open.
676 @end menu
677
678 @node Excess Open
679 @subsection Excess Open Parentheses
680
681 The first step is to find the defun that is unbalanced. If there is
682 an excess open parenthesis, the way to do this is to insert a
683 close parenthesis at the end of the file and type @kbd{C-M-b}
684 (@code{backward-sexp}). This will move you to the beginning of the
685 defun that is unbalanced. (Then type @kbd{C-@key{SPC} C-_ C-u
686 C-@key{SPC}} to set the mark there, undo the insertion of the
687 close parenthesis, and finally return to the mark.)
688
689 The next step is to determine precisely what is wrong. There is no
690 way to be sure of this except by studying the program, but often the
691 existing indentation is a clue to where the parentheses should have
692 been. The easiest way to use this clue is to reindent with @kbd{C-M-q}
693 and see what moves. @strong{But don't do this yet!} Keep reading,
694 first.
695
696 Before you do this, make sure the defun has enough close parentheses.
697 Otherwise, @kbd{C-M-q} will get an error, or will reindent all the rest
698 of the file until the end. So move to the end of the defun and insert a
699 close parenthesis there. Don't use @kbd{C-M-e} to move there, since
700 that too will fail to work until the defun is balanced.
701
702 Now you can go to the beginning of the defun and type @kbd{C-M-q}.
703 Usually all the lines from a certain point to the end of the function
704 will shift to the right. There is probably a missing close parenthesis,
705 or a superfluous open parenthesis, near that point. (However, don't
706 assume this is true; study the code to make sure.) Once you have found
707 the discrepancy, undo the @kbd{C-M-q} with @kbd{C-_}, since the old
708 indentation is probably appropriate to the intended parentheses.
709
710 After you think you have fixed the problem, use @kbd{C-M-q} again. If
711 the old indentation actually fit the intended nesting of parentheses,
712 and you have put back those parentheses, @kbd{C-M-q} should not change
713 anything.
714
715 @node Excess Close
716 @subsection Excess Close Parentheses
717
718 To deal with an excess close parenthesis, first insert an open
719 parenthesis at the beginning of the file, back up over it, and type
720 @kbd{C-M-f} to find the end of the unbalanced defun. (Then type
721 @kbd{C-@key{SPC} C-_ C-u C-@key{SPC}} to set the mark there, undo the
722 insertion of the open parenthesis, and finally return to the mark.)
723
724 Then find the actual matching close parenthesis by typing @kbd{C-M-f}
725 at the beginning of that defun. This will leave you somewhere short of
726 the place where the defun ought to end. It is possible that you will
727 find a spurious close parenthesis in that vicinity.
728
729 If you don't see a problem at that point, the next thing to do is to
730 type @kbd{C-M-q} at the beginning of the defun. A range of lines will
731 probably shift left; if so, the missing open parenthesis or spurious
732 close parenthesis is probably near the first of those lines. (However,
733 don't assume this is true; study the code to make sure.) Once you have
734 found the discrepancy, undo the @kbd{C-M-q} with @kbd{C-_}, since the
735 old indentation is probably appropriate to the intended parentheses.
736
737 After you think you have fixed the problem, use @kbd{C-M-q} again. If
738 the old indentation actually fit the intended nesting of parentheses,
739 and you have put back those parentheses, @kbd{C-M-q} should not change
740 anything.
741
742 @node Compilation Errors
743 @section Debugging Problems in Compilation
744
745 When an error happens during byte compilation, it is normally due to
746 invalid syntax in the program you are compiling. The compiler prints a
747 suitable error message in the @samp{*Compile-Log*} buffer, and then
748 stops. The message may state a function name in which the error was
749 found, or it may not. Either way, here is how to find out where in the
750 file the error occurred.
751
752 What you should do is switch to the buffer @w{@samp{ *Compiler Input*}}.
753 (Note that the buffer name starts with a space, so it does not show
754 up in @kbd{M-x list-buffers}.) This buffer contains the program being
755 compiled, and point shows how far the byte compiler was able to read.
756
757 If the error was due to invalid Lisp syntax, point shows exactly where
758 the invalid syntax was @emph{detected}. The cause of the error is not
759 necessarily near by! Use the techniques in the previous section to find
760 the error.
761
762 If the error was detected while compiling a form that had been read
763 successfully, then point is located at the end of the form. In this
764 case, this technique can't localize the error precisely, but can still
765 show you which function to check.