comparison man/lispref/debugging.texi @ 0:376386a54a3c r19-14

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