api-debug.texi 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Guile Reference Manual.
  3. @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2007
  4. @c Free Software Foundation, Inc.
  5. @c See the file guile.texi for copying conditions.
  6. @page
  7. @node Debugging
  8. @section Debugging Infrastructure
  9. @cindex Debugging
  10. In order to understand Guile's debugging facilities, you first need to
  11. understand a little about how the evaluator works and what the Scheme
  12. stack is. With that in place we explain the low level trap calls that
  13. the evaluator can be configured to make, and the trap and breakpoint
  14. infrastructure that builds on top of those calls.
  15. @menu
  16. * Evaluation Model:: Evaluation and the Scheme stack.
  17. * Debug on Error:: Debugging when an error occurs.
  18. * Traps::
  19. * Debugging Examples::
  20. @end menu
  21. @node Evaluation Model
  22. @subsection Evaluation and the Scheme Stack
  23. The idea of the Scheme stack is central to a lot of debugging. It
  24. always exists implicitly, as a result of the way that the Guile
  25. evaluator works, and can be summoned into concrete existence as a
  26. first-class Scheme value by the @code{make-stack} call, so that an
  27. introspective Scheme program -- such as a debugger -- can present it in
  28. some way and allow the user to query its details. The first thing to
  29. understand, therefore, is how the workings of the evaluator build up the
  30. stack.
  31. @cindex Evaluations
  32. @cindex Applications
  33. Broadly speaking, the evaluator performs @dfn{evaluations} and
  34. @dfn{applications}. An evaluation means that it is looking at a source
  35. code expression like @code{(+ x 5)} or @code{(if msg (loop))}, deciding
  36. whether the top level of the expression is a procedure call, macro,
  37. builtin syntax, or whatever, and doing some appropriate processing in
  38. each case. (In the examples here, @code{(+ x 5)} would normally be a
  39. procedure call, and @code{(if msg (loop))} builtin syntax.) For a
  40. procedure call, ``appropriate processing'' includes evaluating the
  41. procedure's arguments, as that must happen before the procedure itself
  42. can be called. An application means calling a procedure once its
  43. arguments have been calculated.
  44. @cindex Stack
  45. @cindex Frames
  46. @cindex Stack frames
  47. Typically evaluations and applications alternate with each other, and
  48. together they form a @dfn{stack} of operations pending completion. This
  49. is because, on the one hand, evaluation of an expression like @code{(+ x
  50. 5)} requires --- once its arguments have been calculated --- an
  51. application (in this case, of the procedure @code{+}) before it can
  52. complete and return a result, and, on the other hand, the application of
  53. a procedure written in Scheme involves evaluating the sequence of
  54. expressions that constitute that procedure's code. Each level on this
  55. stack is called a @dfn{frame}.
  56. Therefore, when an error occurs in a running program, or the program
  57. hits a breakpoint, or in fact at any point that the programmer chooses,
  58. its state at that point can be represented by a @dfn{stack} of all the
  59. evaluations and procedure applications that are logically in progress at
  60. that time, each of which is known as a @dfn{frame}. The programmer can
  61. learn more about the program's state at that point by inspecting the
  62. stack and its frames.
  63. @menu
  64. * Capturing the Stack or Innermost Stack Frame::
  65. * Examining the Stack::
  66. * Examining Stack Frames::
  67. * Source Properties:: Remembering the source of an expression.
  68. * Decoding Memoized Source Expressions::
  69. * Starting a New Stack::
  70. @end menu
  71. @node Capturing the Stack or Innermost Stack Frame
  72. @subsubsection Capturing the Stack or Innermost Stack Frame
  73. A Scheme program can use the @code{make-stack} primitive anywhere in its
  74. code, with first arg @code{#t}, to construct a Scheme value that
  75. describes the Scheme stack at that point.
  76. @lisp
  77. (make-stack #t)
  78. @result{}
  79. #<stack 805c840:808d250>
  80. @end lisp
  81. @deffn {Scheme Procedure} make-stack obj . args
  82. @deffnx {C Function} scm_make_stack (obj, args)
  83. Create a new stack. If @var{obj} is @code{#t}, the current
  84. evaluation stack is used for creating the stack frames,
  85. otherwise the frames are taken from @var{obj} (which must be
  86. either a debug object or a continuation).
  87. @var{args} should be a list containing any combination of
  88. integer, procedure and @code{#t} values.
  89. These values specify various ways of cutting away uninteresting
  90. stack frames from the top and bottom of the stack that
  91. @code{make-stack} returns. They come in pairs like this:
  92. @code{(@var{inner_cut_1} @var{outer_cut_1} @var{inner_cut_2}
  93. @var{outer_cut_2} @dots{})}.
  94. Each @var{inner_cut_N} can be @code{#t}, an integer, or a
  95. procedure. @code{#t} means to cut away all frames up to but
  96. excluding the first user module frame. An integer means to cut
  97. away exactly that number of frames. A procedure means to cut
  98. away all frames up to but excluding the application frame whose
  99. procedure matches the specified one.
  100. Each @var{outer_cut_N} can be an integer or a procedure. An
  101. integer means to cut away that number of frames. A procedure
  102. means to cut away frames down to but excluding the application
  103. frame whose procedure matches the specified one.
  104. If the @var{outer_cut_N} of the last pair is missing, it is
  105. taken as 0.
  106. @end deffn
  107. @deffn {Scheme Procedure} last-stack-frame obj
  108. @deffnx {C Function} scm_last_stack_frame (obj)
  109. Return the last (innermost) frame of @var{obj}, which must be
  110. either a debug object or a continuation.
  111. @end deffn
  112. @node Examining the Stack
  113. @subsubsection Examining the Stack
  114. @deffn {Scheme Procedure} stack? obj
  115. @deffnx {C Function} scm_stack_p (obj)
  116. Return @code{#t} if @var{obj} is a calling stack.
  117. @end deffn
  118. @deffn {Scheme Procedure} stack-id stack
  119. @deffnx {C Function} scm_stack_id (stack)
  120. Return the identifier given to @var{stack} by @code{start-stack}.
  121. @end deffn
  122. @deffn {Scheme Procedure} stack-length stack
  123. @deffnx {C Function} scm_stack_length (stack)
  124. Return the length of @var{stack}.
  125. @end deffn
  126. @deffn {Scheme Procedure} stack-ref stack index
  127. @deffnx {C Function} scm_stack_ref (stack, index)
  128. Return the @var{index}'th frame from @var{stack}.
  129. @end deffn
  130. @deffn {Scheme Procedure} display-backtrace stack port [first [depth [highlights]]]
  131. @deffnx {C Function} scm_display_backtrace_with_highlights (stack, port, first, depth, highlights)
  132. @deffnx {C Function} scm_display_backtrace (stack, port, first, depth)
  133. Display a backtrace to the output port @var{port}. @var{stack}
  134. is the stack to take the backtrace from, @var{first} specifies
  135. where in the stack to start and @var{depth} how many frames
  136. to display. @var{first} and @var{depth} can be @code{#f},
  137. which means that default values will be used.
  138. If @var{highlights} is given it should be a list; the elements
  139. of this list will be highlighted wherever they appear in the
  140. backtrace.
  141. @end deffn
  142. @node Examining Stack Frames
  143. @subsubsection Examining Stack Frames
  144. @deffn {Scheme Procedure} frame? obj
  145. @deffnx {C Function} scm_frame_p (obj)
  146. Return @code{#t} if @var{obj} is a stack frame.
  147. @end deffn
  148. @deffn {Scheme Procedure} frame-number frame
  149. @deffnx {C Function} scm_frame_number (frame)
  150. Return the frame number of @var{frame}.
  151. @end deffn
  152. @deffn {Scheme Procedure} frame-previous frame
  153. @deffnx {C Function} scm_frame_previous (frame)
  154. Return the previous frame of @var{frame}, or @code{#f} if
  155. @var{frame} is the first frame in its stack.
  156. @end deffn
  157. @deffn {Scheme Procedure} frame-next frame
  158. @deffnx {C Function} scm_frame_next (frame)
  159. Return the next frame of @var{frame}, or @code{#f} if
  160. @var{frame} is the last frame in its stack.
  161. @end deffn
  162. @deffn {Scheme Procedure} frame-source frame
  163. @deffnx {C Function} scm_frame_source (frame)
  164. Return the source of @var{frame}.
  165. @end deffn
  166. @deffn {Scheme Procedure} frame-procedure? frame
  167. @deffnx {C Function} scm_frame_procedure_p (frame)
  168. Return @code{#t} if a procedure is associated with @var{frame}.
  169. @end deffn
  170. @deffn {Scheme Procedure} frame-procedure frame
  171. @deffnx {C Function} scm_frame_procedure (frame)
  172. Return the procedure for @var{frame}, or @code{#f} if no
  173. procedure is associated with @var{frame}.
  174. @end deffn
  175. @deffn {Scheme Procedure} frame-arguments frame
  176. @deffnx {C Function} scm_frame_arguments (frame)
  177. Return the arguments of @var{frame}.
  178. @end deffn
  179. @deffn {Scheme Procedure} frame-evaluating-args? frame
  180. @deffnx {C Function} scm_frame_evaluating_args_p (frame)
  181. Return @code{#t} if @var{frame} contains evaluated arguments.
  182. @end deffn
  183. @deffn {Scheme Procedure} frame-overflow? frame
  184. @deffnx {C Function} scm_frame_overflow_p (frame)
  185. Return @code{#t} if @var{frame} is an overflow frame.
  186. @end deffn
  187. @deffn {Scheme Procedure} frame-real? frame
  188. @deffnx {C Function} scm_frame_real_p (frame)
  189. Return @code{#t} if @var{frame} is a real frame.
  190. @end deffn
  191. @deffn {Scheme Procedure} display-application frame [port [indent]]
  192. @deffnx {C Function} scm_display_application (frame, port, indent)
  193. Display a procedure application @var{frame} to the output port
  194. @var{port}. @var{indent} specifies the indentation of the
  195. output.
  196. @end deffn
  197. @node Source Properties
  198. @subsubsection Source Properties
  199. @cindex source properties
  200. As Guile reads in Scheme code from file or from standard input, it
  201. remembers the file name, line number and column number where each
  202. expression begins. These pieces of information are known as the
  203. @dfn{source properties} of the expression. If an expression undergoes
  204. transformation --- for example, if there is a syntax transformer in
  205. effect, or the expression is a macro call --- the source properties are
  206. copied from the untransformed to the transformed expression so that, if
  207. an error occurs when evaluating the transformed expression, Guile's
  208. debugger can point back to the file and location where the expression
  209. originated.
  210. The way that source properties are stored means that Guile can only
  211. associate source properties with parenthesized expressions, and not, for
  212. example, with individual symbols, numbers or strings. The difference
  213. can be seen by typing @code{(xxx)} and @code{xxx} at the Guile prompt
  214. (where the variable @code{xxx} has not been defined):
  215. @example
  216. guile> (xxx)
  217. standard input:2:1: In expression (xxx):
  218. standard input:2:1: Unbound variable: xxx
  219. ABORT: (unbound-variable)
  220. guile> xxx
  221. <unnamed port>: In expression xxx:
  222. <unnamed port>: Unbound variable: xxx
  223. ABORT: (unbound-variable)
  224. @end example
  225. @noindent
  226. In the latter case, no source properties were stored, so the best that
  227. Guile could say regarding the location of the problem was ``<unnamed
  228. port>''.
  229. The recording of source properties is controlled by the read option
  230. named ``positions'' (@pxref{Reader options}). This option is switched
  231. @emph{on} by default, together with the debug options ``debug'' and
  232. ``backtrace'' (@pxref{Debugger options}), when Guile is run
  233. interactively; all these options are @emph{off} by default when Guile
  234. runs a script non-interactively.
  235. The following procedures can be used to access and set the source
  236. properties of read expressions.
  237. @deffn {Scheme Procedure} set-source-properties! obj plist
  238. @deffnx {C Function} scm_set_source_properties_x (obj, plist)
  239. Install the association list @var{plist} as the source property
  240. list for @var{obj}.
  241. @end deffn
  242. @deffn {Scheme Procedure} set-source-property! obj key datum
  243. @deffnx {C Function} scm_set_source_property_x (obj, key, datum)
  244. Set the source property of object @var{obj}, which is specified by
  245. @var{key} to @var{datum}. Normally, the key will be a symbol.
  246. @end deffn
  247. @deffn {Scheme Procedure} source-properties obj
  248. @deffnx {C Function} scm_source_properties (obj)
  249. Return the source property association list of @var{obj}.
  250. @end deffn
  251. @deffn {Scheme Procedure} source-property obj key
  252. @deffnx {C Function} scm_source_property (obj, key)
  253. Return the source property specified by @var{key} from
  254. @var{obj}'s source property list.
  255. @end deffn
  256. In practice there are only two ways that you should use the ability to
  257. set an expression's source breakpoints.
  258. @itemize
  259. @item
  260. To set a breakpoint on an expression, use @code{(set-source-property!
  261. @var{expr} 'breakpoint #t)}. If you do this, you should also set the
  262. @code{traps} and @code{enter-frame-handler} trap options
  263. (@pxref{Evaluator trap options}) and @code{breakpoints} debug option
  264. (@pxref{Debugger options}) appropriately, and the evaluator will then
  265. call your enter frame handler whenever it is about to evaluate that
  266. expression.
  267. @item
  268. To make a read or constructed expression appear to have come from a
  269. different source than what the expression's source properties already
  270. say, you can use @code{set-source-property!} to set the expression's
  271. @code{filename}, @code{line} and @code{column} properties. The
  272. properties that you set will then show up later if that expression is
  273. involved in a backtrace or error report.
  274. @end itemize
  275. If you are looking for a way to attach arbitrary information to an
  276. expression other than these properties, you should use
  277. @code{make-object-property} instead (@pxref{Object Properties}), because
  278. that will avoid bloating the source property hash table, which is really
  279. only intended for the specific purposes described in this section.
  280. @node Decoding Memoized Source Expressions
  281. @subsubsection Decoding Memoized Source Expressions
  282. @deffn {Scheme Procedure} memoized? obj
  283. @deffnx {C Function} scm_memoized_p (obj)
  284. Return @code{#t} if @var{obj} is memoized.
  285. @end deffn
  286. @deffn {Scheme Procedure} unmemoize m
  287. @deffnx {C Function} scm_unmemoize (m)
  288. Unmemoize the memoized expression @var{m},
  289. @end deffn
  290. @deffn {Scheme Procedure} memoized-environment m
  291. @deffnx {C Function} scm_memoized_environment (m)
  292. Return the environment of the memoized expression @var{m}.
  293. @end deffn
  294. @node Starting a New Stack
  295. @subsubsection Starting a New Stack
  296. @deffn {Scheme Syntax} start-stack id exp
  297. Evaluate @var{exp} on a new calling stack with identity @var{id}. If
  298. @var{exp} is interrupted during evaluation, backtraces will not display
  299. frames farther back than @var{exp}'s top-level form. This macro is a
  300. way of artificially limiting backtraces and stack procedures, largely as
  301. a convenience to the user.
  302. @end deffn
  303. @node Debug on Error
  304. @subsection Debugging when an error occurs
  305. A common requirement is to be able to show as much useful context as
  306. possible when a Scheme program hits an error. The most immediate
  307. information about an error is the kind of error that it is -- such as
  308. ``division by zero'' -- and any parameters that the code which signalled
  309. the error chose explicitly to provide. This information originates with
  310. the @code{error} or @code{throw} call (or their C code equivalents, if
  311. the error is detected by C code) that signals the error, and is passed
  312. automatically to the handler procedure of the innermost applicable
  313. @code{catch}, @code{lazy-catch} or @code{with-throw-handler} expression.
  314. @subsubsection Intercepting basic error information
  315. Therefore, to catch errors that occur within a chunk of Scheme code, and
  316. to intercept basic information about those errors, you need to execute
  317. that code inside the dynamic context of a @code{catch},
  318. @code{lazy-catch} or @code{with-throw-handler} expression, or the
  319. equivalent in C. In Scheme, this means you need something like this:
  320. @lisp
  321. (catch #t
  322. (lambda ()
  323. ;; Execute the code in which
  324. ;; you want to catch errors here.
  325. ...)
  326. (lambda (key . parameters)
  327. ;; Put the code which you want
  328. ;; to handle an error here.
  329. ...))
  330. @end lisp
  331. @noindent
  332. The @code{catch} here can also be @code{lazy-catch} or
  333. @code{with-throw-handler}; see @ref{Throw Handlers} and @ref{Lazy Catch}
  334. for the details of how these differ from @code{catch}. The @code{#t}
  335. means that the catch is applicable to all kinds of error; if you want to
  336. restrict your catch to just one kind of error, you can put the symbol
  337. for that kind of error instead of @code{#t}. The equivalent to this in
  338. C would be something like this:
  339. @lisp
  340. SCM my_body_proc (void *body_data)
  341. @{
  342. /* Execute the code in which
  343. you want to catch errors here. */
  344. ...
  345. @}
  346. SCM my_handler_proc (void *handler_data,
  347. SCM key,
  348. SCM parameters)
  349. @{
  350. /* Put the code which you want
  351. to handle an error here. */
  352. ...
  353. @}
  354. @{
  355. ...
  356. scm_c_catch (SCM_BOOL_T,
  357. my_body_proc, body_data,
  358. my_handler_proc, handler_data,
  359. NULL, NULL);
  360. ...
  361. @}
  362. @end lisp
  363. @noindent
  364. Again, as with the Scheme version, @code{scm_c_catch} could be replaced
  365. by @code{scm_internal_lazy_catch} or @code{scm_c_with_throw_handler},
  366. and @code{SCM_BOOL_T} could instead be the symbol for a particular kind
  367. of error.
  368. @subsubsection Capturing the full error stack
  369. The other interesting information about an error is the full Scheme
  370. stack at the point where the error occurred; in other words what
  371. innermost expression was being evaluated, what was the expression that
  372. called that one, and so on. If you want to write your code so that it
  373. captures and can display this information as well, there are three
  374. important things to understand.
  375. Firstly, the code in question must be executed using the debugging
  376. version of the evaluator, because information about the Scheme stack is
  377. only available at all from the debugging evaluator. Using the debugging
  378. evaluator means that the debugger option (@pxref{Debugger options})
  379. called @code{debug} must be enabled; this can be done by running
  380. @code{(debug-enable 'debug)} or @code{(turn-on-debugging)} at the top
  381. level of your program; or by running guile with the @code{--debug}
  382. command line option, if your program begins life as a Scheme script.
  383. Secondly, the stack at the point of the error needs to be explicitly
  384. captured by a @code{make-stack} call (or the C equivalent
  385. @code{scm_make_stack}). The Guile library does not do this
  386. ``automatically'' for you, so you will need to write code with a
  387. @code{make-stack} or @code{scm_make_stack} call yourself. (We emphasise
  388. this point because some people are misled by the fact that the Guile
  389. interactive REPL code @emph{does} capture and display the stack
  390. automatically. But the Guile interactive REPL is itself a Scheme
  391. program@footnote{In effect, it is the default program which is run when
  392. no commands or script file are specified on the Guile command line.}
  393. running on top of the Guile library, and which uses @code{catch} and
  394. @code{make-stack} in the way we are about to describe to capture the
  395. stack when an error occurs.)
  396. Thirdly, in order to capture the stack effectively at the point where
  397. the error occurred, the @code{make-stack} call must be made before Guile
  398. unwinds the stack back to the location of the prevailing catch
  399. expression. This means that the @code{make-stack} call must be made
  400. within the handler of a @code{lazy-catch} or @code{with-throw-handler}
  401. expression, or the optional "pre-unwind" handler of a @code{catch}.
  402. (For the full story of how these alternatives differ from each other,
  403. see @ref{Exceptions}. The main difference is that @code{catch}
  404. terminates the error, whereas @code{lazy-catch} and
  405. @code{with-throw-handler} only intercept it temporarily and then allow
  406. it to continue propagating up to the next innermost handler.)
  407. So, here are some examples of how to do all this in Scheme and in C.
  408. For the purpose of these examples we assume that the captured stack
  409. should be stored in a variable, so that it can be displayed or
  410. arbitrarily processed later on. In Scheme:
  411. @lisp
  412. (let ((captured-stack #f))
  413. (catch #t
  414. (lambda ()
  415. ;; Execute the code in which
  416. ;; you want to catch errors here.
  417. ...)
  418. (lambda (key . parameters)
  419. ;; Put the code which you want
  420. ;; to handle an error after the
  421. ;; stack has been unwound here.
  422. ...)
  423. (lambda (key . parameters)
  424. ;; Capture the stack here:
  425. (set! captured-stack (make-stack #t))))
  426. ...
  427. (if captured-stack
  428. (begin
  429. ;; Display or process the captured stack.
  430. ...))
  431. ...)
  432. @end lisp
  433. @noindent
  434. And in C:
  435. @lisp
  436. SCM my_body_proc (void *body_data)
  437. @{
  438. /* Execute the code in which
  439. you want to catch errors here. */
  440. ...
  441. @}
  442. SCM my_handler_proc (void *handler_data,
  443. SCM key,
  444. SCM parameters)
  445. @{
  446. /* Put the code which you want
  447. to handle an error after the
  448. stack has been unwound here. */
  449. ...
  450. @}
  451. SCM my_preunwind_proc (void *handler_data,
  452. SCM key,
  453. SCM parameters)
  454. @{
  455. /* Capture the stack here: */
  456. *(SCM *)handler_data = scm_make_stack (SCM_BOOL_T, SCM_EOL);
  457. @}
  458. @{
  459. SCM captured_stack = SCM_BOOL_F;
  460. ...
  461. scm_c_catch (SCM_BOOL_T,
  462. my_body_proc, body_data,
  463. my_handler_proc, handler_data,
  464. my_preunwind_proc, &captured_stack);
  465. ...
  466. if (captured_stack != SCM_BOOL_F)
  467. @{
  468. /* Display or process the captured stack. */
  469. ...
  470. @}
  471. ...
  472. @}
  473. @end lisp
  474. @noindent
  475. Note that you don't have to wait until after the @code{catch} or
  476. @code{scm_c_catch} has returned. You can also do whatever you like with
  477. the stack immediately after it has been captured in the pre-unwind
  478. handler, or in the normal (post-unwind) handler. (Except that for the
  479. latter case in C you will need to change @code{handler_data} in the
  480. @code{scm_c_catch(@dots{})} call to @code{&captured_stack}, so that
  481. @code{my_handler_proc} has access to the captured stack.)
  482. @subsubsection Displaying or interrogating the captured stack
  483. Once you have a captured stack, you can interrogate and display its
  484. details in any way that you want, using the @code{stack-@dots{}} and
  485. @code{frame-@dots{}} API described in @ref{Examining the Stack} and
  486. @ref{Examining Stack Frames}.
  487. If you want to print out a backtrace in the same format that the Guile
  488. REPL does, you can use the @code{display-backtrace} procedure to do so.
  489. You can also use @code{display-application} to display an individual
  490. application frame -- that is, a frame that satisfies the
  491. @code{frame-procedure?} predicate -- in the Guile REPL format.
  492. @subsubsection What the Guile REPL does
  493. The Guile REPL code (in @file{ice-9/boot-9.scm}) uses a @code{catch}
  494. with a pre-unwind handler to capture the stack when an error occurs in
  495. an expression that was typed into the REPL, and saves the captured stack
  496. in a fluid (@pxref{Fluids and Dynamic States}) called
  497. @code{the-last-stack}. You can then use the @code{(backtrace)} command,
  498. which is basically equivalent to @code{(display-backtrace (fluid-ref
  499. the-last-stack))}, to print out this stack at any time until it is
  500. overwritten by the next error that occurs.
  501. @deffn {Scheme Procedure} backtrace [highlights]
  502. @deffnx {C Function} scm_backtrace_with_highlights (highlights)
  503. @deffnx {C Function} scm_backtrace ()
  504. Display a backtrace of the stack saved by the last error
  505. to the current output port. If @var{highlights} is given
  506. it should be a list; the elements of this list will be
  507. highlighted wherever they appear in the backtrace.
  508. @end deffn
  509. You can also use the @code{(debug)} command to explore the saved stack
  510. using an interactive command-line-driven debugger. See @ref{Interactive
  511. Debugger} for more information about this.
  512. @deffn {Scheme Procedure} debug
  513. Invoke the Guile debugger to explore the context of the last error.
  514. @end deffn
  515. @node Traps
  516. @subsection Traps
  517. @cindex Traps
  518. @cindex Evaluator trap calls
  519. @cindex Breakpoints
  520. @cindex Trace
  521. @cindex Tracing
  522. @cindex Code coverage
  523. @cindex Profiling
  524. The low level C code of Guile's evaluator can be configured to call
  525. out at key points to arbitrary user-specified procedures. These
  526. procedures, and the circumstances under which the evaluator calls
  527. them, are configured by the ``evaluator trap options'' interface
  528. (@pxref{Evaluator trap options}), and by the @code{trace} and
  529. @code{breakpoints} fields of the ``debug options'' interface
  530. (@pxref{Debugger options}). In principle this allows Scheme code to
  531. implement any model it chooses for examining the evaluation stack as
  532. program execution proceeds, and for suspending execution to be resumed
  533. later. Possible applications of this feature include breakpoints,
  534. runtime tracing, code coverage, and profiling.
  535. @cindex Trap classes
  536. @cindex Trap objects
  537. Based on these low level trap calls, Guile provides a higher level,
  538. object-oriented interface for the manipulation of traps. Different
  539. kinds of trap are represented as GOOPS classes; for example, the
  540. @code{<procedure-trap>} class describes traps that are triggered by
  541. invocation of a specified procedure. A particular instance of a trap
  542. class --- or @dfn{trap object} --- describes the condition under which
  543. a single trap will be triggered, and what will happen then; for
  544. example, an instance of @code{<procedure-trap>} whose @code{procedure}
  545. and @code{behaviour} slots contain @code{my-factorial} and
  546. @code{debug-trap} would be a trap that enters the command line
  547. debugger when the @code{my-factorial} procedure is invoked.
  548. The following subsections describe all this in detail, for both the
  549. user wanting to use traps, and the developer interested in
  550. understanding how the interface hangs together.
  551. @subsubsection A Quick Note on Terminology
  552. @cindex Trap terminology
  553. It feels natural to use the word ``trap'' in some form for all levels
  554. of the structure just described, so we need to be clear on the
  555. terminology we use to describe each particular level. The terminology
  556. used in this subsection is as follows.
  557. @itemize @bullet
  558. @item
  559. @cindex Evaluator trap calls
  560. @cindex Low level trap calls
  561. ``Low level trap calls'', or ``low level traps'', are the calls made
  562. directly from the C code of the Guile evaluator.
  563. @item
  564. @cindex Trap classes
  565. ``Trap classes'' are self-explanatory.
  566. @item
  567. @cindex Trap objects
  568. ``Trap objects'', ``trap instances'', or just ``traps'', are instances
  569. of a trap class, and each describe a single logical trap condition
  570. plus behaviour as specified by the user of this interface.
  571. @end itemize
  572. A good example of when it is important to be clear, is when we talk
  573. below of behaviours that should only happen once per low level trap.
  574. A single low level trap call will typically map onto the processing of
  575. several trap objects, so ``once per low level trap'' is significantly
  576. different from ``once per trap''.
  577. @menu
  578. * How to Set a Trap::
  579. * Specifying Trap Behaviour::
  580. * Trap Context::
  581. * Tracing Examples::
  582. * Tracing Configuration::
  583. * Tracing and (ice-9 debug)::
  584. * Traps Installing More Traps::
  585. * Common Trap Options::
  586. * Procedure Traps::
  587. * Exit Traps::
  588. * Entry Traps::
  589. * Apply Traps::
  590. * Step Traps::
  591. * Source Traps::
  592. * Location Traps::
  593. * Trap Shorthands::
  594. * Trap Utilities::
  595. @end menu
  596. @node How to Set a Trap
  597. @subsubsection How to Set a Trap
  598. @cindex Setting traps
  599. @cindex Installing and uninstalling traps
  600. Setting a trap is done in two parts. First the trap is defined by
  601. creating an instance of the appropriate trap class, with slot values
  602. specifying the condition under which the trap will fire and the action
  603. to take when it fires. Secondly the trap object thus created must be
  604. @dfn{installed}.
  605. To make this immediately concrete, here is an example that sets a trap
  606. to fire on the next application of the @code{facti} procedure, and to
  607. handle the trap by entering the command line debugger.
  608. @lisp
  609. (install-trap (make <procedure-trap>
  610. #:procedure facti
  611. #:single-shot #t
  612. #:behaviour debug-trap))
  613. @end lisp
  614. @noindent
  615. Briefly, the elements of this incantation are as follows. (All of
  616. these are described more fully in the following subsubsections.)
  617. @itemize @bullet
  618. @item
  619. @code{<procedure-trap>} is the trap class for trapping on invocation
  620. of a specific procedure.
  621. @item
  622. @code{#:procedure facti} says that the specific procedure to trap on for this
  623. trap object is @code{facti}.
  624. @item
  625. @code{#:single-shot #t} says that this trap should only fire on the
  626. @emph{next} invocation of @code{facti}, not on all future invocations
  627. (which is the default if the @code{#:single-shot} option is not
  628. specified).
  629. @item
  630. @code{#:behaviour debug-trap} says that the trap infrastructure should
  631. call the procedure @code{debug-trap} when this trap fires.
  632. @item
  633. Finally, the @code{install-trap} call installs the trap immediately.
  634. @end itemize
  635. @noindent
  636. It is of course possible for the user to define more convenient
  637. shorthands for setting common kinds of traps. @xref{Trap Shorthands},
  638. for some examples.
  639. The ability to install, uninstall and reinstall a trap without losing
  640. its definition is Guile's equivalent of the disable/enable commands
  641. provided by debuggers like GDB.
  642. @deffn {Generic Function} install-trap trap
  643. Install the trap object @var{trap}, so that its behaviour will be
  644. executed when the conditions for the trap firing are met.
  645. @end deffn
  646. @deffn {Generic Function} uninstall-trap trap
  647. Uninstall the trap object @var{trap}, so that its behaviour will
  648. @emph{not} be executed even if the conditions for the trap firing are
  649. met.
  650. @end deffn
  651. @node Specifying Trap Behaviour
  652. @subsubsection Specifying Trap Behaviour
  653. @cindex Trap behaviour
  654. Guile provides several ``out-of-the-box'' behaviours for common needs.
  655. All of the following can be used directly as the value of the
  656. @code{#:behaviour} option when creating a trap object.
  657. @deffn {Procedure} debug-trap trap-context
  658. Enter Guile's command line debugger to explore the stack at
  659. @var{trap-context}, and to single-step or continue program execution
  660. from that point.
  661. @end deffn
  662. @deffn {Procedure} gds-debug-trap trap-context
  663. Use the GDS debugging interface, which displays the stack and
  664. corresponding source code via Emacs, to explore the stack at
  665. @var{trap-context} and to single-step or continue program execution
  666. from that point.
  667. @end deffn
  668. @cindex Trace
  669. @cindex Tracing
  670. @deffn {Procedure} trace-trap trap-context
  671. Display trace information to summarize the current @var{trap-context}.
  672. @end deffn
  673. @deffn {Procedure} trace-at-exit trap-context
  674. Install a further trap to cause the return value of the application or
  675. evaluation just starting (as described by @var{trap-context}) to be
  676. traced using @code{trace-trap}, when this application or evaluation
  677. completes. The extra trap is automatically uninstalled after the
  678. return value has been traced.
  679. @end deffn
  680. @deffn {Procedure} trace-until-exit trap-context
  681. Install a further trap so that every step that the evaluator performs
  682. as part of the application or evaluation just starting (as described
  683. by @var{trap-context}) is traced using @code{trace-trap}. The extra
  684. trap is automatically uninstalled when the application or evaluation
  685. is complete. @code{trace-until-exit} can be very useful as a first
  686. step when all you know is that there is a bug ``somewhere in XXX or in
  687. something that XXX calls''.
  688. @end deffn
  689. @noindent
  690. @code{debug-trap} and @code{gds-debug-trap} are provided by the modules
  691. @code{(ice-9 debugger)} and @code{(ice-9 gds-client)} respectively, and
  692. their behaviours are fairly self-explanatory. For more information on
  693. the operation of the GDS interface via Emacs, see @ref{Using Guile in
  694. Emacs}. The tracing behaviours are explained more fully below.
  695. @cindex Trap context
  696. More generally, the @dfn{behaviour} specified for a trap can be any
  697. procedure that expects to be called with one @dfn{trap context}
  698. argument. A trivial example would be:
  699. @lisp
  700. (define (report-stack-depth trap-context)
  701. (display "Stack depth at the trap is: ")
  702. (display (tc:depth trap-context))
  703. (newline))
  704. @end lisp
  705. @node Trap Context
  706. @subsubsection Trap Context
  707. The @dfn{trap context} is an object that caches information about the
  708. low level trap call and the stack at the point of the trap, and is
  709. passed as the only argument to all behaviour procedures. The
  710. information in the trap context can be accessed through the procedures
  711. beginning @code{tc:} that are exported by the @code{(ice-9 debugging
  712. traps)} module@footnote{Plus of course any procedures that build on
  713. these, such as the @code{trace/@dots{}} procedures exported by
  714. @code{(ice-9 debugging trace)} (@pxref{Tracing Configuration}).}; the
  715. most useful of these are as follows.
  716. @deffn {Generic Function} tc:type trap-context
  717. Indicates the type of the low level trap by returning one of the
  718. keywords @code{#:application}, @code{#:evaluation}, @code{#:return} or
  719. @code{#:error}.
  720. @end deffn
  721. @deffn {Generic Function} tc:return-value trap-context
  722. When @code{tc:type} gives @code{#:return}, this provides the value
  723. that is being returned.
  724. @end deffn
  725. @deffn {Generic Function} tc:stack trap-context
  726. Provides the stack at the point of the trap (as computed by
  727. @code{make-stack}, but cached so that the lengthy @code{make-stack}
  728. operation is not performed more than once for the same low level
  729. trap).
  730. @end deffn
  731. @deffn {Generic Function} tc:frame trap-context
  732. The innermost frame of the stack at the point of the trap.
  733. @end deffn
  734. @deffn {Generic Function} tc:depth trap-context
  735. The number of frames (including tail recursive non-real frames) in the
  736. stack at the point of the trap.
  737. @end deffn
  738. @deffn {Generic Function} tc:real-depth trap-context
  739. The number of real frames (that is, excluding the non-real frames that
  740. describe tail recursive calls) in the stack at the point of the trap.
  741. @end deffn
  742. @node Tracing Examples
  743. @subsubsection Tracing Examples
  744. The following examples show what tracing is and the kind of output that
  745. it generates. In the first example, we define a recursive function for
  746. reversing a list, then watch the effect of the recursive calls by
  747. tracing each call and return value.
  748. @lisp
  749. guile> (define (rev ls)
  750. (if (null? ls)
  751. ls
  752. (append (rev (cdr ls))
  753. (list (car ls)))))
  754. guile> (use-modules (ice-9 debugging traps) (ice-9 debugging trace))
  755. guile> (define t1 (make <procedure-trap>
  756. #:procedure rev
  757. #:behaviour (list trace-trap
  758. trace-at-exit)))
  759. guile> (install-trap t1)
  760. guile> (rev '(a b c))
  761. | 2: [rev (a b c)]
  762. | 3: [rev (b c)]
  763. | 4: [rev (c)]
  764. | 5: [rev ()]
  765. | 5: =>()
  766. | 4: =>(c)
  767. | 3: =>(c b)
  768. | 2: =>(c b a)
  769. (c b a)
  770. @end lisp
  771. @noindent
  772. The number before the colon in this output (which follows @code{(ice-9
  773. debugging trace)}'s default output format) is the number of real frames
  774. on the stack. The fact that this number increases for each recursive
  775. call confirms that the implementation above of @code{rev} is not
  776. tail-recursive.
  777. In the next example, we probe the @emph{internal} workings of
  778. @code{rev} in more detail by using the @code{trace-until-exit}
  779. behaviour.
  780. @lisp
  781. guile> (uninstall-trap t1)
  782. guile> (define t2 (make <procedure-trap>
  783. #:procedure rev
  784. #:behaviour (list trace-trap
  785. trace-until-exit)))
  786. guile> (install-trap t2)
  787. guile> (rev '(a b))
  788. | 2: [rev (a b)]
  789. | 2: (if (null? ls) ls (append (rev (cdr ls)) (list (car ls))))
  790. | 3: (null? ls)
  791. | 3: [null? (a b)]
  792. | 3: =>#f
  793. | 2: (append (rev (cdr ls)) (list (car ls)))
  794. | 3: (rev (cdr ls))
  795. | 4: (cdr ls)
  796. | 4: [cdr (a b)]
  797. | 4: =>(b)
  798. | 3: [rev (b)]
  799. | 3: (if (null? ls) ls (append (rev (cdr ls)) (list (car ls))))
  800. | 4: (null? ls)
  801. | 4: [null? (b)]
  802. | 4: =>#f
  803. | 3: (append (rev (cdr ls)) (list (car ls)))
  804. | 4: (rev (cdr ls))
  805. | 5: (cdr ls)
  806. | 5: [cdr (b)]
  807. | 5: =>()
  808. | 4: [rev ()]
  809. | 4: (if (null? ls) ls (append (rev (cdr ls)) (list (car ls))))
  810. | 5: (null? ls)
  811. | 5: [null? ()]
  812. | 5: =>#t
  813. | 4: (list (car ls))
  814. | 5: (car ls)
  815. | 5: [car (b)]
  816. | 5: =>b
  817. | 4: [list b]
  818. | 4: =>(b)
  819. | 3: [append () (b)]
  820. | 3: =>(b)
  821. | 3: (list (car ls))
  822. | 4: (car ls)
  823. | 4: [car (a b)]
  824. | 4: =>a
  825. | 3: [list a]
  826. | 3: =>(a)
  827. | 2: [append (b) (a)]
  828. | 2: =>(b a)
  829. (b a)
  830. @end lisp
  831. @noindent
  832. The output in this case shows every step that the evaluator performs
  833. in evaluating @code{(rev '(a b))}.
  834. @node Tracing Configuration
  835. @subsubsection Tracing Configuration
  836. The detail of what gets printed in each trace line, and the port to
  837. which tracing is written, can be configured by the procedures
  838. @code{set-trace-layout} and @code{trace-port}, both exported by the
  839. @code{(ice-9 debugging trace)} module.
  840. @deffn {Procedure with Setter} trace-port
  841. Get or set the port to which tracing is printed. The default is the
  842. value of @code{(current-output-port)} when the @code{(ice-9 debugging
  843. trace)} module is first loaded.
  844. @end deffn
  845. @deffn {Procedure} set-trace-layout format-string . arg-procs
  846. Layout each trace line using @var{format-string} and @var{arg-procs}.
  847. For each trace line, the list of values to be printed is obtained by
  848. calling all the @var{arg-procs}, passing the trap context as the only
  849. parameter to each one. This list of values is then formatted using
  850. the specified @var{format-string}.
  851. @end deffn
  852. @noindent
  853. The @code{(ice-9 debugging trace)} module exports a set of arg-proc
  854. procedures to cover most common needs, with names beginning
  855. @code{trace/}. These are all implemented on top of the @code{tc:} trap
  856. context accessor procedures documented in @ref{Trap Context}, and if any
  857. trace output not provided by the following is needed, it should be
  858. possible to implement based on a combination of the @code{tc:}
  859. procedures.
  860. @deffn {Procedure} trace/pid trap-context
  861. An arg-proc that returns the current process ID.
  862. @end deffn
  863. @deffn {Procedure} trace/stack-id trap-context
  864. An arg-proc that returns the stack ID of the stack in which the
  865. current trap occurred.
  866. @end deffn
  867. @deffn {Procedure} trace/stack-depth trap-context
  868. An arg-proc that returns the length (including non-real frames) of the
  869. stack at the point of the current trap.
  870. @end deffn
  871. @deffn {Procedure} trace/stack-real-depth trap-context
  872. An arg-proc that returns the length excluding non-real frames of the
  873. stack at the point of the current trap.
  874. @end deffn
  875. @deffn {Procedure} trace/stack trap-context
  876. An arg-proc that returns a string summarizing stack information. This
  877. string includes the stack ID, real depth, and count of additional
  878. non-real frames, with the format @code{"~a:~a+~a"}.
  879. @end deffn
  880. @deffn {Procedure} trace/source-file-name trap-context
  881. An arg-proc that returns the name of the source file for the innermost
  882. stack frame, or an empty string if source is not available for the
  883. innermost frame.
  884. @end deffn
  885. @deffn {Procedure} trace/source-line trap-context
  886. An arg-proc that returns the line number of the source code for the
  887. innermost stack frame, or zero if source is not available for the
  888. innermost frame.
  889. @end deffn
  890. @deffn {Procedure} trace/source-column trap-context
  891. An arg-proc that returns the column number of the start of the source
  892. code for the innermost stack frame, or zero if source is not available
  893. for the innermost frame.
  894. @end deffn
  895. @deffn {Procedure} trace/source trap-context
  896. An arg-proc that returns the source location for the innermost stack
  897. frame. This is a string composed of file name, line and column number
  898. with the format @code{"~a:~a:~a"}, or an empty string if source is not
  899. available for the innermost frame.
  900. @end deffn
  901. @deffn {Procedure} trace/type trap-context
  902. An arg-proc that returns a three letter abbreviation indicating the
  903. type of the current trap: @code{"APP"} for an application frame,
  904. @code{"EVA"} for an evaluation, @code{"RET"} for an exit trap, or
  905. @code{"ERR"} for an error (pseudo-)trap.
  906. @end deffn
  907. @deffn {Procedure} trace/real? trap-context
  908. An arg-proc that returns @code{" "} if the innermost stack frame is a
  909. real frame, or @code{"t"} if it is not.
  910. @end deffn
  911. @deffn {Procedure} trace/info trap-context
  912. An arg-proc that returns a string describing the expression being
  913. evaluated, application being performed, or return value, according to
  914. the current trap type.
  915. @end deffn
  916. @noindent
  917. @code{trace/stack-depth} and @code{trace/stack-real-depth} are identical
  918. to the trap context methods @code{tc:depth} and @code{tc:real-depth}
  919. described before (@pxref{Trap Context}), but renamed here for
  920. convenience.
  921. The default trace layout, as exhibited by the examples of the previous
  922. subsubsubsection, is set by this line of code from the @code{(ice-9 debugging
  923. traps)} module:
  924. @lisp
  925. (set-trace-layout "|~3@@a: ~a\n" trace/stack-real-depth trace/info)
  926. @end lisp
  927. @noindent
  928. If we rerun the first of those examples, but with trace layout
  929. configured to show source location and trap type in addition, the
  930. output looks like this:
  931. @lisp
  932. guile> (set-trace-layout "| ~25a ~3@@a: ~a ~a\n"
  933. trace/source
  934. trace/stack-real-depth
  935. trace/type
  936. trace/info)
  937. guile> (rev '(a b c))
  938. | standard input:29:0 2: APP [rev (a b c)]
  939. | standard input:4:21 3: APP [rev (b c)]
  940. | standard input:4:21 4: APP [rev (c)]
  941. | standard input:4:21 5: APP [rev ()]
  942. | standard input:2:9 5: RET =>()
  943. | standard input:4:13 4: RET =>(c)
  944. | standard input:4:13 3: RET =>(c b)
  945. | standard input:4:13 2: RET =>(c b a)
  946. (c b a)
  947. @end lisp
  948. @node Tracing and (ice-9 debug)
  949. @subsubsection Tracing and (ice-9 debug)
  950. The @code{(ice-9 debug)} module provides a tracing facility
  951. (@pxref{Tracing}) that is roughly similar to that described here, but
  952. there are important differences.
  953. @itemize @bullet
  954. @item
  955. The @code{(ice-9 debug)} trace gives a nice pictorial view of changes
  956. in stack depth, by using indentation like this:
  957. @lisp
  958. [fact1 4]
  959. | [fact1 3]
  960. | | [fact1 2]
  961. | | | [fact1 1]
  962. | | | | [fact1 0]
  963. | | | | 1
  964. | | | 1
  965. | | 2
  966. | 6
  967. 24
  968. @end lisp
  969. However its output can @emph{only} show the information seen here,
  970. which corresponds to @code{(ice-9 debugging trace)}'s
  971. @code{trace/info} procedure; it cannot be configured to show other
  972. pieces of information about the trap context in the way that the
  973. @code{(ice-9 debugging trace)} implementation can.
  974. @item
  975. The @code{(ice-9 debug)} trace only allows the tracing of procedure
  976. applications and their return values, whereas the @code{(ice-9 debugging
  977. trace)} implementation allows any kind of trap to be traced.
  978. It's interesting to note that @code{(ice-9 debug)}'s restriction here,
  979. which might initially appear to be just a straightforward consequence
  980. of its implementation, is also somewhat dictated by its pictorial
  981. display. The use of indentation in the output relies on hooking into
  982. the low level trap calls in such a way that the trapped application
  983. entries and exits exactly balance each other. The @code{ice-9
  984. debugging trace} implementation allows traps to be installed such that
  985. entry and exit traps don't necessarily balance, which means that, in
  986. general, indentation diagrams like the one above don't work.
  987. @end itemize
  988. It isn't currently possible to use both @code{(ice-9 debug)} trace and
  989. @code{(ice-9 debugging trace)} in the same Guile session, because
  990. their settings of the low level trap options conflict with each other.
  991. @node Traps Installing More Traps
  992. @subsubsection Traps Installing More Traps
  993. Sometimes it is desirable for the behaviour at one trap to install
  994. further traps. In other words, the behaviour is something like
  995. ``Don't do much right now, but set things up to stop after two or
  996. three more steps'', or ``@dots{} when this frame completes''. This is
  997. absolutely fine. For example, it is easy to code a generic ``do
  998. so-and-so when the current frame exits'' procedure, which can be used
  999. wherever a trap context is available, as follows.
  1000. @lisp
  1001. (define (at-exit trap-context behaviour)
  1002. (install-trap (make <exit-trap>
  1003. #:depth (tc:depth trap-context)
  1004. #:single-shot #t
  1005. #:behaviour behaviour)))
  1006. @end lisp
  1007. To continue and pin down the example, this could then be used as part
  1008. of a behaviour whose purpose was to measure the accumulated time spent
  1009. in and below a specified procedure.
  1010. @lisp
  1011. (define calls 0)
  1012. (define total 0)
  1013. (define accumulate-time
  1014. (lambda (trap-context)
  1015. (set! calls (+ calls 1))
  1016. (let ((entry (current-time)))
  1017. (at-exit trap-context
  1018. (lambda (ignored)
  1019. (set! total
  1020. (+ total (- (current-time)
  1021. entry))))))))
  1022. (install-trap (make <procedure-trap>
  1023. #:procedure my-proc
  1024. #:behaviour accumulate-time))
  1025. @end lisp
  1026. @node Common Trap Options
  1027. @subsubsection Common Trap Options
  1028. When creating any kind of trap object, settings for the trap being
  1029. created are specified as options on the @code{make} call using syntax
  1030. like this:
  1031. @lisp
  1032. (make <@var{trap-class}>
  1033. #:@var{option-keyword} @var{setting}
  1034. @dots{})
  1035. @end lisp
  1036. The following common options are provided by the base class
  1037. @code{<trap>}, and so can be specified for any kind of trap.
  1038. @deffn {Class} <trap>
  1039. Base class for trap objects.
  1040. @end deffn
  1041. @deffn {Trap Option} #:condition thunk
  1042. If not @code{#f}, this is a thunk which is called when the trap fires,
  1043. to determine whether trap processing should proceed any further. If
  1044. the thunk returns @code{#f}, the trap is basically suppressed.
  1045. Otherwise processing continues normally. (Default value @code{#f}.)
  1046. @end deffn
  1047. @deffn {Trap Option} #:skip-count count
  1048. A count of valid (after @code{#:condition} processing) firings of this
  1049. trap to skip. (Default value 0.)
  1050. @end deffn
  1051. @deffn {Trap Option} #:single-shot boolean
  1052. If not @code{#f}, this indicates that the trap should be automatically
  1053. uninstalled after it has successfully fired (after @code{#:condition}
  1054. and @code{#:skip-count} processing) for the first time. (Default
  1055. value @code{#f}.)
  1056. @end deffn
  1057. @deffn {Trap Option} #:behaviour behaviour-proc
  1058. A trap behaviour procedure --- as discussed in the preceding subsubsection
  1059. --- or a list of such procedures, in which case each procedure is
  1060. called in turn when the trap fires. (Default value @code{'()}.)
  1061. @end deffn
  1062. @deffn {Trap Option} #:repeat-identical-behaviour boolean
  1063. Normally, if multiple trap objects are triggered by the same low level
  1064. trap, and they request the same behaviour, it's only actually useful
  1065. to do that behaviour once (per low level trap); so by default multiple
  1066. requests for the same behaviour are coalesced. If this option is set
  1067. other than @code{#f}, the contents of the @code{#:behaviour} option
  1068. are uniquified so that they avoid being coalesced in this way.
  1069. (Default value @code{#f}.)
  1070. @end deffn
  1071. @node Procedure Traps
  1072. @subsubsection Procedure Traps
  1073. The @code{<procedure-trap>} class implements traps that are triggered
  1074. upon application of a specified procedure. Instances of this class
  1075. should use the @code{#:procedure} option to specify the procedure to
  1076. trap on.
  1077. @deffn {Class} <procedure-trap>
  1078. Class for traps triggered by application of a specified procedure.
  1079. @end deffn
  1080. @deffn {Trap Option} #:procedure procedure
  1081. Specifies the procedure to trap on.
  1082. @end deffn
  1083. @noindent
  1084. Example:
  1085. @lisp
  1086. (install-trap (make <procedure-trap>
  1087. #:procedure my-proc
  1088. #:behaviour (list trace-trap
  1089. trace-until-exit)))
  1090. @end lisp
  1091. @node Exit Traps
  1092. @subsubsection Exit Traps
  1093. The @code{<exit-trap>} class implements traps that are triggered upon
  1094. stack frame exit past a specified stack depth. Instances of this
  1095. class should use the @code{#:depth} option to specify the target stack
  1096. depth.
  1097. @deffn {Class} <exit-trap>
  1098. Class for traps triggered by exit past a specified stack depth.
  1099. @end deffn
  1100. @deffn {Trap Option} #:depth depth
  1101. Specifies the reference depth for the trap.
  1102. @end deffn
  1103. @noindent
  1104. Example:
  1105. @lisp
  1106. (define (trace-at-exit trap-context)
  1107. (install-trap (make <exit-trap>
  1108. #:depth (tc:depth trap-context)
  1109. #:single-shot #t
  1110. #:behaviour trace-trap)))
  1111. @end lisp
  1112. @noindent
  1113. (This is the actual definition of the @code{trace-at-exit} behaviour.)
  1114. @node Entry Traps
  1115. @subsubsection Entry Traps
  1116. The @code{<entry-trap>} class implements traps that are triggered upon
  1117. any stack frame entry. No further parameters are needed to specify an
  1118. instance of this class, so there are no class-specific trap options.
  1119. Note that it remains possible to use the common trap options
  1120. (@pxref{Common Trap Options}), for example to set a trap for the
  1121. @var{n}th next frame entry.
  1122. @deffn {Class} <entry-trap>
  1123. Class for traps triggered by any stack frame entry.
  1124. @end deffn
  1125. @noindent
  1126. Example:
  1127. @lisp
  1128. (install-trap (make <entry-trap>
  1129. #:skip-count 5
  1130. #:behaviour gds-debug-trap))
  1131. @end lisp
  1132. @node Apply Traps
  1133. @subsubsection Apply Traps
  1134. The @code{<apply-trap>} class implements traps that are triggered upon
  1135. any procedure application. No further parameters are needed to
  1136. specify an instance of this class, so there are no class-specific trap
  1137. options. Note that it remains possible to use the common trap options
  1138. (@pxref{Common Trap Options}), for example to set a trap for the next
  1139. application where some condition is true.
  1140. @deffn {Class} <apply-trap>
  1141. Class for traps triggered by any procedure application.
  1142. @end deffn
  1143. @noindent
  1144. Example:
  1145. @lisp
  1146. (install-trap (make <apply-trap>
  1147. #:condition my-condition
  1148. #:behaviour gds-debug-trap))
  1149. @end lisp
  1150. @node Step Traps
  1151. @subsubsection Step Traps
  1152. The @code{<step-trap>} class implements traps that do single-stepping
  1153. through a program's execution. They come in two flavours, with and
  1154. without a specified file name. If a file name is specified, the trap
  1155. is triggered by the next evaluation, application or frame exit
  1156. pertaining to source code from the specified file. If a file name is
  1157. not specified, the trap is triggered by the next evaluation,
  1158. application or frame exit from any file (or for code whose source
  1159. location was not recorded), in other words by the next evaluator step
  1160. of any kind.
  1161. The design goal of the @code{<step-trap>} class is to match what a
  1162. user would intuitively think of as single-stepping through their code,
  1163. either through code in general (roughly corresponding to GDB's
  1164. @code{step} command, for example), or through code from a particular
  1165. source file (roughly corresponding to GDB's @code{next}). Therefore
  1166. if you are using a step trap to single-step through code and finding
  1167. its behaviour counter-intuitive, please report that so we can improve
  1168. it.
  1169. The implementation and options of the @code{<step-trap>} class are
  1170. complicated by the fact that it is unreliable to determine whether a
  1171. low level frame exit trap is applicable to a specified file by
  1172. examining the details of the reported frame. This is a consequence of
  1173. tail recursion, which has the effect that many frames can be removed
  1174. from the stack at once, with only the outermost frame being reported
  1175. by the low level trap call. The effects of this on the
  1176. @code{<step-trap>} class are such as to require the introduction of
  1177. the strange-looking @code{#:exit-depth} option, for the following
  1178. reasons.
  1179. @itemize @bullet
  1180. @item
  1181. When stopped at the start of an application or evaluation frame, and
  1182. it is desired to continue execution until the next ``step'' in the same
  1183. source file, that next step could be the start of a nested application
  1184. or evaluation frame, or --- if the procedure definition is in a
  1185. different file, for example --- it could be the exit from the current
  1186. frame.
  1187. @item
  1188. Because of the effects of tail recursion noted above, the current
  1189. frame exit possibility must be expressed as frame exit past a
  1190. specified stack depth. When an instance of the @code{<step-trap>}
  1191. class is installed from the context of an application or evaluation
  1192. frame entry, the @code{#:exit-depth} option should be used to specify
  1193. this stack depth.
  1194. @item
  1195. When stopped at a frame exit, on the other hand, we know that the next
  1196. step must be an application or evaluation frame entry. In this
  1197. context the @code{#:exit-depth} option is not needed and should be
  1198. omitted or set to @code{#f}.
  1199. @end itemize
  1200. @noindent
  1201. When a step trap is installed without @code{#:single-shot #t}, such
  1202. that it keeps firing, the @code{<step-trap>} code automatically
  1203. updates its idea of the @code{#:exit-depth} setting each time, so that
  1204. the trap always fires correctly for the following step.
  1205. @deffn {Class} <step-trap>
  1206. Class for single-stepping traps.
  1207. @end deffn
  1208. @deffn {Trap Option} #:file-name name
  1209. If not @code{#f}, this is a string containing the name of a source
  1210. file, and restricts the step trap to evaluation steps within that
  1211. source file. (Default value @code{#f}.)
  1212. @end deffn
  1213. @deffn {Trap Option} #:exit-depth depth
  1214. If not @code{#f}, this is a positive integer implying that the next
  1215. step may be frame exit past the stack depth @var{depth}. See the
  1216. discussion above for more details. (Default value @code{#f}.)
  1217. @end deffn
  1218. @noindent
  1219. Example:
  1220. @lisp
  1221. (install-trap (make <step-trap>
  1222. #:file-name (frame-file-name
  1223. (stack-ref stack index))
  1224. #:exit-depth (- (stack-length stack)
  1225. (stack-ref stack index))
  1226. #:single-shot #t
  1227. #:behaviour debug-trap))
  1228. @end lisp
  1229. @node Source Traps
  1230. @subsubsection Source Traps
  1231. The @code{<source-trap>} class implements traps that are attached to a
  1232. precise source code expression, as read by the reader, and which fire
  1233. each time that that expression is evaluated. These traps use a low
  1234. level Guile feature which can mark individual expressions for
  1235. trapping, and are relatively efficient. But it can be tricky to get
  1236. at the source expression in the first place, and these traps are
  1237. liable to become irrelevant if the procedure containing the expression
  1238. is reevaluated; these issues are discussed further below.
  1239. @deffn {Class} <source-trap>
  1240. Class for traps triggered by evaluation of a specific Scheme
  1241. expression.
  1242. @end deffn
  1243. @deffn {Trap Option} #:expression expr
  1244. Specifies the Scheme expression to trap on.
  1245. @end deffn
  1246. @noindent
  1247. Example:
  1248. @lisp
  1249. (display "Enter an expression: ")
  1250. (let ((x (read)))
  1251. (install-trap (make <source-trap>
  1252. #:expression x
  1253. #:behaviour (list trace-trap
  1254. trace-at-exit)))
  1255. (primitive-eval x))
  1256. @print{}
  1257. Enter an expression: (+ 1 2 3 4 5 6)
  1258. | 3: (+ 1 2 3 4 5 6)
  1259. | 3: =>21
  1260. 21
  1261. @end lisp
  1262. The key point here is that the expression specified by the
  1263. @code{#:expression} option must be @emph{exactly} (i.e. @code{eq?} to)
  1264. what is going to be evaluated later. It doesn't work, for example, to
  1265. say @code{#:expression '(+ x 3)}, with the expectation that the trap
  1266. will fire whenever evaluating any expression @code{(+ x 3)}.
  1267. The @code{trap-here} macro can be used in source code to create and
  1268. install a source trap correctly. Take for example the factorial
  1269. function defined in the @code{(ice-9 debugging example-fns)} module:
  1270. @lisp
  1271. (define (fact1 n)
  1272. (if (= n 0)
  1273. 1
  1274. (* n (fact1 (- n 1)))))
  1275. @end lisp
  1276. @noindent
  1277. To set a source trap on a particular expression --- let's say the
  1278. expression @code{(= n 0)} --- edit the code so that the expression is
  1279. enclosed in a @code{trap-here} macro call like this:
  1280. @lisp
  1281. (define (fact1 n)
  1282. (if (trap-here (= n 0) #:behaviour debug-trap)
  1283. 1
  1284. (* n (fact1 (- n 1)))))
  1285. @end lisp
  1286. @deffn {Macro} trap-here expression . trap-options
  1287. Install a source trap with options @var{trap-options} on
  1288. @var{expression}, then return with the whole call transformed to
  1289. @code{(begin @var{expression})}.
  1290. @end deffn
  1291. Note that if the @code{trap-here} incantation is removed, and
  1292. @code{fact1} then redefined by reloading its source file, the effect
  1293. of the source trap is lost, because the text ``(= n 0)'' is read again
  1294. from scratch and becomes a new expression @code{(= n 0)} which does
  1295. not have the ``trap here'' mark on it.
  1296. If the semantics and setting of source traps seem unwieldy, location
  1297. traps may meet your need more closely; these are described in the
  1298. following subsubsection.
  1299. @node Location Traps
  1300. @subsubsection Location Traps
  1301. The @code{<location-trap>} class implements traps that are triggered
  1302. by evaluation of code at a specific source location. When compared
  1303. with source traps, they are easier to set, and do not become
  1304. irrelevant when the relevant code is reloaded; but unfortunately they
  1305. are a lot less efficient, as they require running some ``are we in the
  1306. right place for a trap'' code on every low level frame entry trap
  1307. call.
  1308. @deffn {Class} <location-trap>
  1309. Class for traps triggered by evaluation of code at a specific source
  1310. location.
  1311. @end deffn
  1312. @deffn {Trap Option} #:file-regexp regexp
  1313. A regular expression specifying the filenames that will match this
  1314. trap. This option must be specified when creating a location trap.
  1315. @end deffn
  1316. @deffn {Trap Option} #:line line
  1317. The line number (0-based) of the source location at which the trap
  1318. should be triggered. This option must be specified when creating a
  1319. location trap.
  1320. @end deffn
  1321. @deffn {Trap Option} #:column column
  1322. The column number (0-based) of the source location at which the trap
  1323. should be triggered. This option must be specified when creating a
  1324. location trap.
  1325. @end deffn
  1326. @noindent
  1327. Here is an example, which matches the @code{(facti (- n 1) (* a n))}
  1328. expression in @file{ice-9/debugging/example-fns.scm}:
  1329. @lisp
  1330. (install-trap (make <location-trap>
  1331. #:file-regexp "example-fns.scm"
  1332. #:line 11
  1333. #:column 6
  1334. #:behaviour gds-debug-trap))
  1335. @end lisp
  1336. @node Trap Shorthands
  1337. @subsubsection Trap Shorthands
  1338. If the code described in the preceding subsubsections for creating and
  1339. manipulating traps seems a little long-winded, it is of course
  1340. possible to define more convenient shorthand forms for typical usage
  1341. patterns. Here are some examples.
  1342. @lisp
  1343. (define (break! proc)
  1344. (install-trap (make <procedure-trap>
  1345. #:procedure proc
  1346. #:behaviour gds-debug-trap)))
  1347. (define (trace! proc)
  1348. (install-trap (make <procedure-trap>
  1349. #:procedure proc
  1350. #:behaviour (list trace-trap
  1351. trace-at-exit))))
  1352. (define (trace-subtree! proc)
  1353. (install-trap (make <procedure-trap>
  1354. #:procedure proc
  1355. #:behaviour (list trace-trap
  1356. trace-until-exit))))
  1357. @end lisp
  1358. Definitions like these are not provided out-of-the-box by Guile,
  1359. because different users will have different ideas about what their
  1360. default debugger should be, or, for example, which of the common trap
  1361. options (@pxref{Common Trap Options}) it might be useful to expose
  1362. through such shorthand procedures.
  1363. @node Trap Utilities
  1364. @subsubsection Trap Utilities
  1365. @code{list-traps} can be used to print a description of all known trap
  1366. objects. This uses a weak value hash table, keyed by a trap index
  1367. number. Each trap object has its index number assigned, and is added
  1368. to the hash table, when it is created by a @code{make @var{trap-class}
  1369. @dots{}} call. When a trap object is GC'd, it is automatically
  1370. removed from the hash table, and so no longer appears in the output
  1371. from @code{list-traps}.
  1372. @deffn {Variable} all-traps
  1373. Weak value hash table containing all known trap objects.
  1374. @end deffn
  1375. @deffn {Procedure} list-traps
  1376. Print a description of all known trap objects.
  1377. @end deffn
  1378. The following example shows a single trap that traces applications of
  1379. the procedure @code{facti}.
  1380. @lisp
  1381. guile> (list-traps)
  1382. #<<procedure-trap> 100d2e30> is an instance of class <procedure-trap>
  1383. Slots are:
  1384. number = 1
  1385. installed = #t
  1386. condition = #f
  1387. skip-count = 0
  1388. single-shot = #f
  1389. behaviour = (#<procedure trace-trap (trap-context)>)
  1390. repeat-identical-behaviour = #f
  1391. procedure = #<procedure facti (n a)>
  1392. @end lisp
  1393. When @code{all-traps} or @code{list-traps} reveals a trap that you
  1394. want to modify but no longer have a reference to, you can retrieve the
  1395. trap object by calling @code{get-trap} with the trap's number. For
  1396. example, here's how you could change the behaviour of the trap listed
  1397. just above.
  1398. @lisp
  1399. (slot-set! (get-trap 1) 'behaviour (list debug-trap))
  1400. @end lisp
  1401. @deffn {Procedure} get-trap number
  1402. Return the trap object with the specified @var{number}, or @code{#f}
  1403. if there isn't one.
  1404. @end deffn
  1405. @node Debugging Examples
  1406. @subsection Debugging Examples
  1407. Here we present some examples of what you can do with the debugging
  1408. facilities just described.
  1409. @menu
  1410. * Single Stepping through a Procedure's Code::
  1411. * Profiling or Tracing a Procedure's Code::
  1412. @end menu
  1413. @node Single Stepping through a Procedure's Code
  1414. @subsubsection Single Stepping through a Procedure's Code
  1415. A good way to explore in detail what a Scheme procedure does is to set
  1416. a trap on it and then single step through what it does. To do this,
  1417. make and install a @code{<procedure-trap>} with the @code{debug-trap}
  1418. behaviour from @code{(ice-9 debugging ice-9-debugger-extensions)}.
  1419. The following sample session illustrates this. It assumes that the
  1420. file @file{matrix.scm} defines a procedure @code{mkmatrix}, which is
  1421. the one we want to explore, and another procedure @code{do-main} which
  1422. calls @code{mkmatrix}.
  1423. @lisp
  1424. $ /usr/bin/guile -q
  1425. guile> (use-modules (ice-9 debugger)
  1426. (ice-9 debugging ice-9-debugger-extensions)
  1427. (ice-9 debugging traps))
  1428. guile> (load "matrix.scm")
  1429. guile> (install-trap (make <procedure-trap>
  1430. #:procedure mkmatrix
  1431. #:behaviour debug-trap))
  1432. guile> (do-main 4)
  1433. This is the Guile debugger -- for help, type `help'.
  1434. There are 3 frames on the stack.
  1435. Frame 2 at matrix.scm:8:3
  1436. [mkmatrix]
  1437. debug> next
  1438. Frame 3 at matrix.scm:4:3
  1439. (let ((x 1)) (quote this-is-a-matric))
  1440. debug> info frame
  1441. Stack frame: 3
  1442. This frame is an evaluation.
  1443. The expression being evaluated is:
  1444. matrix.scm:4:3:
  1445. (let ((x 1)) (quote this-is-a-matric))
  1446. debug> next
  1447. Frame 3 at matrix.scm:5:21
  1448. (quote this-is-a-matric)
  1449. debug> bt
  1450. In unknown file:
  1451. ?: 0* [primitive-eval (do-main 4)]
  1452. In standard input:
  1453. 4: 1* [do-main 4]
  1454. In matrix.scm:
  1455. 8: 2 [mkmatrix]
  1456. ...
  1457. 5: 3 (quote this-is-a-matric)
  1458. debug> quit
  1459. this-is-a-matric
  1460. guile>
  1461. @end lisp
  1462. Or you can use Guile's Emacs interface (GDS), by using the module
  1463. @code{(ice-9 gds-client)} instead of @code{(ice-9 debugger)} and
  1464. @code{(ice-9 debugging ice-9-debugger-extensions)}, and changing
  1465. @code{debug-trap} to @code{gds-debug-trap}. Then the stack and
  1466. corresponding source locations are displayed in Emacs instead of on
  1467. the Guile command line.
  1468. @node Profiling or Tracing a Procedure's Code
  1469. @subsubsection Profiling or Tracing a Procedure's Code
  1470. What if you wanted to get a trace of everything that the Guile
  1471. evaluator does within a given procedure, but without Guile stopping
  1472. and waiting for your input at every step? For this requirement you
  1473. can install a trap on the procedure, as in the previous example, but
  1474. instead of @code{debug-trap} or @code{gds-debug-trap}, use the
  1475. @code{trace-trap} and @code{trace-until-exit} behaviours provided by
  1476. the @code{(ice-9 debugging trace)} module.
  1477. @lisp
  1478. guile> (use-modules (ice-9 debugging traps) (ice-9 debugging trace))
  1479. guile> (load "matrix.scm")
  1480. guile> (install-trap (make <procedure-trap>
  1481. #:procedure mkmatrix
  1482. #:behaviour (list trace-trap trace-until-exit)))
  1483. guile> (do-main 4)
  1484. | 2: [mkmatrix]
  1485. | 3: [#<procedure #f (a sym definep)> #<autoload # b7c93870> define #f]
  1486. | 3: [#<procedure #f (a sym definep)> #<autoload # b7c93870> define #f]
  1487. | 4: (and (memq sym bindings) (let ...))
  1488. | 5: (memq sym bindings)
  1489. | 5: [memq define (debug)]
  1490. | 5: =>#f
  1491. | 3: [#<procedure #f (a sym definep)> #<autoload # b7c93870> define #f]
  1492. | 3: [#<procedure #f (a sym definep)> #<autoload # b7c93870> define #f]
  1493. | 4: (and (memq sym bindings) (let ...))
  1494. | 5: (memq sym bindings)
  1495. | 5: [memq define (debug)]
  1496. | 5: =>#f
  1497. | 3: [#<procedure #f (a sym definep)> #<autoload # b7c93870> let #f]
  1498. | 3: [#<procedure #f (a sym definep)> #<autoload # b7c93870> let #f]
  1499. | 4: (and (memq sym bindings) (let ...))
  1500. | 5: (memq sym bindings)
  1501. | 5: [memq let (debug)]
  1502. | 5: =>#f
  1503. | 3: [#<procedure #f (a sym definep)> #<autoload # b7c93870> let #f]
  1504. | 3: [#<procedure #f (a sym definep)> #<autoload # b7c93870> let #f]
  1505. | 4: (and (memq sym bindings) (let ...))
  1506. | 5: (memq sym bindings)
  1507. | 5: [memq let (debug)]
  1508. | 5: =>#f
  1509. | 3: [#<procedure #f (a sym definep)> #<autoload # b7c93870> let #f]
  1510. | 3: [#<procedure #f (a sym definep)> #<autoload # b7c93870> let #f]
  1511. | 4: (and (memq sym bindings) (let ...))
  1512. | 5: (memq sym bindings)
  1513. | 5: [memq let (debug)]
  1514. | 5: =>#f
  1515. | 2: (letrec ((yy 23)) (let ((x 1)) (quote this-is-a-matric)))
  1516. | 3: [#<procedure #f (a sym definep)> #<autoload # b7c93870> let #f]
  1517. | 3: [#<procedure #f (a sym definep)> #<autoload # b7c93870> let #f]
  1518. | 4: (and (memq sym bindings) (let ...))
  1519. | 5: (memq sym bindings)
  1520. | 5: [memq let (debug)]
  1521. | 5: =>#f
  1522. | 3: [#<procedure #f (a sym definep)> #<autoload # b7c93870> let #f]
  1523. | 3: [#<procedure #f (a sym definep)> #<autoload # b7c93870> let #f]
  1524. | 4: (and (memq sym bindings) (let ...))
  1525. | 5: (memq sym bindings)
  1526. | 5: [memq let (debug)]
  1527. | 5: =>#f
  1528. | 3: [#<procedure #f (a sym definep)> #<autoload # b7c93870> let #f]
  1529. | 3: [#<procedure #f (a sym definep)> #<autoload # b7c93870> let #f]
  1530. | 4: (and (memq sym bindings) (let ...))
  1531. | 5: (memq sym bindings)
  1532. | 5: [memq let (debug)]
  1533. | 5: =>#f
  1534. | 2: (let ((x 1)) (quote this-is-a-matric))
  1535. | 3: [#<procedure #f (a sym definep)> #<autoload # b7c93870> let #f]
  1536. | 3: [#<procedure #f (a sym definep)> #<autoload # b7c93870> let #f]
  1537. | 4: (and (memq sym bindings) (let ...))
  1538. | 5: (memq sym bindings)
  1539. | 5: [memq let (debug)]
  1540. | 5: =>#f
  1541. | 2: [let (let # #) (# # #)]
  1542. | 2: [let (let # #) (# # #)]
  1543. | 2: =>(#@@let* (x 1) #@@let (quote this-is-a-matric))
  1544. this-is-a-matric
  1545. guile> (do-main 4)
  1546. | 2: [mkmatrix]
  1547. | 2: (letrec ((yy 23)) (let* ((x 1)) (quote this-is-a-matric)))
  1548. | 2: (let* ((x 1)) (quote this-is-a-matric))
  1549. | 2: (quote this-is-a-matric)
  1550. | 2: =>this-is-a-matric
  1551. this-is-a-matric
  1552. guile>
  1553. @end lisp
  1554. This example shows the default configuration for how each line of trace
  1555. output is formatted, which is:
  1556. @itemize
  1557. @item
  1558. the character @code{|}, a visual clue that the line is a line of trace
  1559. output, followed by
  1560. @item
  1561. a number indicating the real evaluator stack depth (where ``real'' means
  1562. not counting tail-calls), followed by
  1563. @item
  1564. a summary of the expression being evaluated (@code{(@dots{})}), the
  1565. procedure being called (@code{[@dots{}]}), or the value being returned
  1566. from an evaluation or procedure call (@code{=>@dots{}}).
  1567. @end itemize
  1568. @noindent
  1569. You can customize @code{(ice-9 debugging trace)} to show different
  1570. information in each trace line using the @code{set-trace-layout}
  1571. procedure. The next example shows how to get the source location in
  1572. each trace line instead of the stack depth.
  1573. @lisp
  1574. guile> (set-trace-layout "|~16@@a: ~a\n" trace/source trace/info)
  1575. guile> (do-main 4)
  1576. | matrix.scm:7:2: [mkmatrix]
  1577. | : (letrec ((yy 23)) (let* ((x 1)) (quote this-is-a-matric)))
  1578. | matrix.scm:3:2: (let* ((x 1)) (quote this-is-a-matric))
  1579. | matrix.scm:4:4: (quote this-is-a-matric)
  1580. | matrix.scm:4:4: =>this-is-a-matric
  1581. this-is-a-matric
  1582. guile>
  1583. @end lisp
  1584. (For anyone wondering why the first @code{(do-main 4)} call above
  1585. generates lots more trace lines than the subsequent calls: these
  1586. examples also demonstrate how the Guile evaluator ``memoizes'' code.
  1587. When Guile evaluates a source code expression for the first time, it
  1588. changes some parts of the expression so that they will be quicker to
  1589. evaluate when that expression is evaluated again; this is called
  1590. memoization. The trace output from the first @code{(do-main 4)} call
  1591. shows memoization steps, such as an internal define being transformed to
  1592. a letrec.)
  1593. @c Local Variables:
  1594. @c TeX-master: "guile.texi"
  1595. @c End: