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