debugging.texi 35 KB

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