diff options
Diffstat (limited to 'doc/lispref/debugging.texi')
| -rw-r--r-- | doc/lispref/debugging.texi | 216 |
1 files changed, 96 insertions, 120 deletions
diff --git a/doc/lispref/debugging.texi b/doc/lispref/debugging.texi index cc92fc225f9..6e7d0078e07 100644 --- a/doc/lispref/debugging.texi +++ b/doc/lispref/debugging.texi | |||
| @@ -6,41 +6,46 @@ | |||
| 6 | @node Debugging, Read and Print, Advising Functions, Top | 6 | @node Debugging, Read and Print, Advising Functions, Top |
| 7 | @chapter Debugging Lisp Programs | 7 | @chapter Debugging Lisp Programs |
| 8 | 8 | ||
| 9 | There are three ways to investigate a problem in an Emacs Lisp program, | 9 | There are several ways to find and investigate problems in an Emacs |
| 10 | depending on what you are doing with the program when the problem appears. | 10 | Lisp program. |
| 11 | 11 | ||
| 12 | @itemize @bullet | 12 | @itemize @bullet |
| 13 | @item | 13 | @item |
| 14 | If the problem occurs when you run the program, you can use a Lisp | 14 | If a problem occurs when you run the program, you can use the built-in |
| 15 | debugger to investigate what is happening during execution. In addition | 15 | Emacs Lisp debugger to suspend the Lisp evaluator, and examine and/or |
| 16 | to the ordinary debugger, Emacs comes with a source-level debugger, | 16 | alter its internal state. |
| 17 | Edebug. This chapter describes both of them. | ||
| 18 | 17 | ||
| 19 | @item | 18 | @item |
| 20 | If the problem is syntactic, so that Lisp cannot even read the program, | 19 | You can use Edebug, a source-level debugger for Emacs Lisp. |
| 21 | you can use the Emacs facilities for editing Lisp to localize it. | ||
| 22 | 20 | ||
| 23 | @item | 21 | @item |
| 24 | If the problem occurs when trying to compile the program with the byte | 22 | If a syntactic problem is preventing Lisp from even reading the |
| 25 | compiler, you need to know how to examine the compiler's input buffer. | 23 | program, you can locate it using Lisp editing commands. |
| 24 | |||
| 25 | @item | ||
| 26 | You can look at the error and warning messages produced by the byte | ||
| 27 | compiler when it compiles the program. @xref{Compiler Errors}. | ||
| 28 | |||
| 29 | @item | ||
| 30 | You can use the Testcover package to perform coverage testing on the | ||
| 31 | program. | ||
| 32 | |||
| 33 | @item | ||
| 34 | You can use the ERT package to write regression tests for the program. | ||
| 35 | @xref{Top,the ERT manual,, ERT, ERT: Emacs Lisp Regression Testing}. | ||
| 26 | @end itemize | 36 | @end itemize |
| 27 | 37 | ||
| 38 | Other useful tools for debugging input and output problems are the | ||
| 39 | dribble file (@pxref{Terminal Input}) and the @code{open-termscript} | ||
| 40 | function (@pxref{Terminal Output}). | ||
| 41 | |||
| 28 | @menu | 42 | @menu |
| 29 | * Debugger:: How the Emacs Lisp debugger is implemented. | 43 | * Debugger:: A debugger for the Emacs Lisp evaluator. |
| 30 | * Edebug:: A source-level Emacs Lisp debugger. | 44 | * Edebug:: A source-level Emacs Lisp debugger. |
| 31 | * Syntax Errors:: How to find syntax errors. | 45 | * Syntax Errors:: How to find syntax errors. |
| 32 | * Test Coverage:: Ensuring you have tested all branches in your code. | 46 | * Test Coverage:: Ensuring you have tested all branches in your code. |
| 33 | * Compilation Errors:: How to find errors that show up in byte compilation. | ||
| 34 | @end menu | 47 | @end menu |
| 35 | 48 | ||
| 36 | Another useful debugging tool is the dribble file. When a dribble | ||
| 37 | file is open, Emacs copies all keyboard input characters to that file. | ||
| 38 | Afterward, you can examine the file to find out what input was used. | ||
| 39 | @xref{Terminal Input}. | ||
| 40 | |||
| 41 | For debugging problems in terminal descriptions, the | ||
| 42 | @code{open-termscript} function can be useful. @xref{Terminal Output}. | ||
| 43 | |||
| 44 | @node Debugger | 49 | @node Debugger |
| 45 | @section The Lisp Debugger | 50 | @section The Lisp Debugger |
| 46 | @cindex debugger for Emacs Lisp | 51 | @cindex debugger for Emacs Lisp |
| @@ -76,25 +81,29 @@ happens. This allows you to investigate the immediate causes of the | |||
| 76 | error. | 81 | error. |
| 77 | 82 | ||
| 78 | However, entry to the debugger is not a normal consequence of an | 83 | However, entry to the debugger is not a normal consequence of an |
| 79 | error. Many commands frequently cause Lisp errors when invoked | 84 | error. Many commands signal Lisp errors when invoked inappropriately, |
| 80 | inappropriately, and during ordinary editing it would be very | 85 | and during ordinary editing it would be very inconvenient to enter the |
| 81 | inconvenient to enter the debugger each time this happens. So if you | 86 | debugger each time this happens. So if you want errors to enter the |
| 82 | want errors to enter the debugger, set the variable | 87 | debugger, set the variable @code{debug-on-error} to non-@code{nil}. |
| 83 | @code{debug-on-error} to non-@code{nil}. (The command | 88 | (The command @code{toggle-debug-on-error} provides an easy way to do |
| 84 | @code{toggle-debug-on-error} provides an easy way to do this.) | 89 | this.) |
| 85 | 90 | ||
| 86 | @defopt debug-on-error | 91 | @defopt debug-on-error |
| 87 | This variable determines whether the debugger is called when an error | 92 | This variable determines whether the debugger is called when an error |
| 88 | is signaled and not handled. If @code{debug-on-error} is @code{t}, | 93 | is signaled and not handled. If @code{debug-on-error} is @code{t}, |
| 89 | all kinds of errors call the debugger, except those listed in | 94 | all kinds of errors call the debugger, except those listed in |
| 90 | @code{debug-ignored-errors} (see below). If it is @code{nil}, none | 95 | @code{debug-ignored-errors} (see below). If it is @code{nil}, none |
| 91 | call the debugger. (Note that @code{eval-expression-debug-on-error} | 96 | call the debugger. |
| 92 | affects the setting of this variable in some cases; see below.) | ||
| 93 | 97 | ||
| 94 | The value can also be a list of error conditions that should call the | 98 | The value can also be a list of error conditions (@pxref{Signaling |
| 95 | debugger. For example, if you set it to the list | 99 | Errors}). Then the debugger is called only for error conditions in |
| 96 | @code{(void-variable)}, then only errors about a variable that has no | 100 | this list (except those also listed in @code{debug-ignored-errors}). |
| 97 | value invoke the debugger. | 101 | For example, if you set @code{debug-on-error} to the list |
| 102 | @code{(void-variable)}, the debugger is only called for errors about a | ||
| 103 | variable that has no value. | ||
| 104 | |||
| 105 | Note that @code{eval-expression-debug-on-error} overrides this | ||
| 106 | variable in some cases; see below. | ||
| 98 | 107 | ||
| 99 | When this variable is non-@code{nil}, Emacs does not create an error | 108 | When this variable is non-@code{nil}, Emacs does not create an error |
| 100 | handler around process filter functions and sentinels. Therefore, | 109 | handler around process filter functions and sentinels. Therefore, |
| @@ -102,52 +111,50 @@ errors in these functions also invoke the debugger. @xref{Processes}. | |||
| 102 | @end defopt | 111 | @end defopt |
| 103 | 112 | ||
| 104 | @defopt debug-ignored-errors | 113 | @defopt debug-ignored-errors |
| 105 | This variable specifies certain kinds of errors that should not enter | 114 | This variable specifies errors which should not enter the debugger, |
| 106 | the debugger. Its value is a list of error condition symbols and/or | 115 | regardless of the value of @code{debug-on-error}. Its value is a list |
| 107 | regular expressions. If the error has any of those condition symbols, | 116 | of error condition symbols and/or regular expressions. If the error |
| 108 | or if the error message matches any of the regular expressions, then | 117 | has any of those condition symbols, or if the error message matches |
| 109 | that error does not enter the debugger, regardless of the value of | 118 | any of the regular expressions, then that error does not enter the |
| 110 | @code{debug-on-error}. | 119 | debugger. |
| 111 | 120 | ||
| 112 | The normal value of this variable lists several errors that happen often | 121 | The normal value of this variable lists several errors that happen |
| 113 | during editing but rarely result from bugs in Lisp programs. However, | 122 | often during editing but rarely result from bugs in Lisp programs. |
| 114 | ``rarely'' is not ``never''; if your program fails with an error that | 123 | However, ``rarely'' is not ``never''; if your program fails with an |
| 115 | matches this list, you will need to change this list in order to debug | 124 | error that matches this list, you may try changing this list to debug |
| 116 | the error. The easiest way is usually to set | 125 | the error. The easiest way is usually to set |
| 117 | @code{debug-ignored-errors} to @code{nil}. | 126 | @code{debug-ignored-errors} to @code{nil}. |
| 118 | @end defopt | 127 | @end defopt |
| 119 | 128 | ||
| 120 | @defopt eval-expression-debug-on-error | 129 | @defopt eval-expression-debug-on-error |
| 121 | If this variable has a non-@code{nil} value, then | 130 | If this variable has a non-@code{nil} value (the default), running the |
| 122 | @code{debug-on-error} is set to @code{t} when evaluating with the | 131 | command @code{eval-expression} causes @code{debug-on-error} to be |
| 123 | command @code{eval-expression}. If | 132 | temporarily bound to to @code{t}. @xref{Lisp Eval,, Evaluating |
| 124 | @code{eval-expression-debug-on-error} is @code{nil}, then the value of | ||
| 125 | @code{debug-on-error} is not changed. @xref{Lisp Eval,, Evaluating | ||
| 126 | Emacs-Lisp Expressions, emacs, The GNU Emacs Manual}. | 133 | Emacs-Lisp Expressions, emacs, The GNU Emacs Manual}. |
| 127 | @end defopt | ||
| 128 | 134 | ||
| 129 | @defopt debug-on-signal | 135 | If @code{eval-expression-debug-on-error} is @code{nil}, then the value |
| 130 | Normally, errors that are caught by @code{condition-case} never run the | 136 | of @code{debug-on-error} is not changed during @code{eval-expression}. |
| 131 | debugger, even if @code{debug-on-error} is non-@code{nil}. In other | ||
| 132 | words, @code{condition-case} gets a chance to handle the error before | ||
| 133 | the debugger gets a chance. | ||
| 134 | |||
| 135 | If you set @code{debug-on-signal} to a non-@code{nil} value, then the | ||
| 136 | debugger gets the first chance at every error; an error will invoke the | ||
| 137 | debugger regardless of any @code{condition-case}, if it fits the | ||
| 138 | criteria specified by the values of @code{debug-on-error} and | ||
| 139 | @code{debug-ignored-errors}. | ||
| 140 | |||
| 141 | @strong{Warning:} This variable is strong medicine! Various parts of | ||
| 142 | Emacs handle errors in the normal course of affairs, and you may not | ||
| 143 | even realize that errors happen there. If you set | ||
| 144 | @code{debug-on-signal} to a non-@code{nil} value, those errors will | ||
| 145 | enter the debugger. | ||
| 146 | |||
| 147 | @strong{Warning:} @code{debug-on-signal} has no effect when | ||
| 148 | @code{debug-on-error} is @code{nil}. | ||
| 149 | @end defopt | 137 | @end defopt |
| 150 | 138 | ||
| 139 | @defvar debug-on-signal | ||
| 140 | Normally, errors caught by @code{condition-case} never invoke the | ||
| 141 | debugger. The @code{condition-case} gets a chance to handle the error | ||
| 142 | before the debugger gets a chance. | ||
| 143 | |||
| 144 | If you change @code{debug-on-signal} to a non-@code{nil} value, the | ||
| 145 | debugger gets the first chance at every error, regardless of the | ||
| 146 | presence of @code{condition-case}. (To invoke the debugger, the error | ||
| 147 | must still fulfill the criteria specified by @code{debug-on-error} and | ||
| 148 | @code{debug-ignored-errors}.) | ||
| 149 | |||
| 150 | @strong{Warning:} Setting this variable to non-@code{nil} may have | ||
| 151 | annoying effects. Various parts of Emacs catch errors in the normal | ||
| 152 | course of affairs, and you may not even realize that errors happen | ||
| 153 | there. If you need to debug code wrapped in @code{condition-case}, | ||
| 154 | consider using @code{condition-case-unless-debug} (@pxref{Handling | ||
| 155 | Errors}). | ||
| 156 | @end defvar | ||
| 157 | |||
| 151 | @defopt debug-on-event | 158 | @defopt debug-on-event |
| 152 | If you set @code{debug-on-event} to a special event (@pxref{Special | 159 | If you set @code{debug-on-event} to a special event (@pxref{Special |
| 153 | Events}), Emacs will try to enter the debugger as soon as it receives | 160 | Events}), Emacs will try to enter the debugger as soon as it receives |
| @@ -171,27 +178,26 @@ init file. | |||
| 171 | @cindex stopping an infinite loop | 178 | @cindex stopping an infinite loop |
| 172 | 179 | ||
| 173 | When a program loops infinitely and fails to return, your first | 180 | When a program loops infinitely and fails to return, your first |
| 174 | problem is to stop the loop. On most operating systems, you can do this | 181 | problem is to stop the loop. On most operating systems, you can do |
| 175 | with @kbd{C-g}, which causes a @dfn{quit}. | 182 | this with @kbd{C-g}, which causes a @dfn{quit}. @xref{Quitting}. |
| 176 | 183 | ||
| 177 | Ordinary quitting gives no information about why the program was | 184 | Ordinary quitting gives no information about why the program was |
| 178 | looping. To get more information, you can set the variable | 185 | looping. To get more information, you can set the variable |
| 179 | @code{debug-on-quit} to non-@code{nil}. Quitting with @kbd{C-g} is not | 186 | @code{debug-on-quit} to non-@code{nil}. Once you have the debugger |
| 180 | considered an error, and @code{debug-on-error} has no effect on the | 187 | running in the middle of the infinite loop, you can proceed from the |
| 181 | handling of @kbd{C-g}. Likewise, @code{debug-on-quit} has no effect on | 188 | debugger using the stepping commands. If you step through the entire |
| 182 | errors. | 189 | loop, you may get enough information to solve the problem. |
| 183 | 190 | ||
| 184 | Once you have the debugger running in the middle of the infinite loop, | 191 | Quitting with @kbd{C-g} is not considered an error, and |
| 185 | you can proceed from the debugger using the stepping commands. If you | 192 | @code{debug-on-error} has no effect on the handling of @kbd{C-g}. |
| 186 | step through the entire loop, you will probably get enough information | 193 | Likewise, @code{debug-on-quit} has no effect on errors. |
| 187 | to solve the problem. | ||
| 188 | 194 | ||
| 189 | @defopt debug-on-quit | 195 | @defopt debug-on-quit |
| 190 | This variable determines whether the debugger is called when @code{quit} | 196 | This variable determines whether the debugger is called when |
| 191 | is signaled and not handled. If @code{debug-on-quit} is non-@code{nil}, | 197 | @code{quit} is signaled and not handled. If @code{debug-on-quit} is |
| 192 | then the debugger is called whenever you quit (that is, type @kbd{C-g}). | 198 | non-@code{nil}, then the debugger is called whenever you quit (that |
| 193 | If @code{debug-on-quit} is @code{nil}, then the debugger is not called | 199 | is, type @kbd{C-g}). If @code{debug-on-quit} is @code{nil} (the |
| 194 | when you quit. @xref{Quitting}. | 200 | default), then the debugger is not called when you quit. |
| 195 | @end defopt | 201 | @end defopt |
| 196 | 202 | ||
| 197 | @node Function Debugging | 203 | @node Function Debugging |
| @@ -337,8 +343,8 @@ that exiting that frame will call the debugger again. This is useful | |||
| 337 | for examining the return value of a function. | 343 | for examining the return value of a function. |
| 338 | 344 | ||
| 339 | If a function name is underlined, that means the debugger knows | 345 | If a function name is underlined, that means the debugger knows |
| 340 | where its source code is located. You can click @kbd{Mouse-2} on that | 346 | where its source code is located. You can click with the mouse on |
| 341 | name, or move to it and type @key{RET}, to visit the source code. | 347 | that name, or move to it and type @key{RET}, to visit the source code. |
| 342 | 348 | ||
| 343 | The debugger itself must be run byte-compiled, since it makes | 349 | The debugger itself must be run byte-compiled, since it makes |
| 344 | assumptions about how many stack frames are used for the debugger | 350 | assumptions about how many stack frames are used for the debugger |
| @@ -364,14 +370,10 @@ to step through a primitive function. | |||
| 364 | 370 | ||
| 365 | @table @kbd | 371 | @table @kbd |
| 366 | @item c | 372 | @item c |
| 367 | Exit the debugger and continue execution. When continuing is possible, | 373 | Exit the debugger and continue execution. This resumes execution of |
| 368 | it resumes execution of the program as if the debugger had never been | 374 | the program as if the debugger had never been entered (aside from any |
| 369 | entered (aside from any side-effects that you caused by changing | 375 | side-effects that you caused by changing variable values or data |
| 370 | variable values or data structures while inside the debugger). | 376 | structures while inside the debugger). |
| 371 | |||
| 372 | Continuing is possible after entry to the debugger due to function entry | ||
| 373 | or exit, explicit invocation, or quitting. You cannot continue if the | ||
| 374 | debugger was entered because of an error. | ||
| 375 | 377 | ||
| 376 | @item d | 378 | @item d |
| 377 | Continue execution, but enter the debugger the next time any Lisp | 379 | Continue execution, but enter the debugger the next time any Lisp |
| @@ -790,29 +792,3 @@ never return. If it ever does return, you get a run-time error. | |||
| 790 | Edebug also has a coverage testing feature (@pxref{Coverage | 792 | Edebug also has a coverage testing feature (@pxref{Coverage |
| 791 | Testing}). These features partly duplicate each other, and it would | 793 | Testing}). These features partly duplicate each other, and it would |
| 792 | be cleaner to combine them. | 794 | be cleaner to combine them. |
| 793 | |||
| 794 | @node Compilation Errors | ||
| 795 | @section Debugging Problems in Compilation | ||
| 796 | @cindex debugging byte compilation problems | ||
| 797 | |||
| 798 | When an error happens during byte compilation, it is normally due to | ||
| 799 | invalid syntax in the program you are compiling. The compiler prints a | ||
| 800 | suitable error message in the @samp{*Compile-Log*} buffer, and then | ||
| 801 | stops. The message may state a function name in which the error was | ||
| 802 | found, or it may not. Either way, here is how to find out where in the | ||
| 803 | file the error occurred. | ||
| 804 | |||
| 805 | What you should do is switch to the buffer @w{@samp{ *Compiler Input*}}. | ||
| 806 | (Note that the buffer name starts with a space, so it does not show | ||
| 807 | up in @kbd{M-x list-buffers}.) This buffer contains the program being | ||
| 808 | compiled, and point shows how far the byte compiler was able to read. | ||
| 809 | |||
| 810 | If the error was due to invalid Lisp syntax, point shows exactly where | ||
| 811 | the invalid syntax was @emph{detected}. The cause of the error is not | ||
| 812 | necessarily near by! Use the techniques in the previous section to find | ||
| 813 | the error. | ||
| 814 | |||
| 815 | If the error was detected while compiling a form that had been read | ||
| 816 | successfully, then point is located at the end of the form. In this | ||
| 817 | case, this technique can't localize the error precisely, but can still | ||
| 818 | show you which function to check. | ||