edebug.texi 63 KB

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