Implement R7RS 'syntax-error'.
[bpt/guile.git] / doc / ref / api-debug.texi
CommitLineData
07d83abe
MV
1@c -*-texinfo-*-
2@c This is part of the GNU Guile Reference Manual.
994d87be 3@c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2007, 2010, 2011, 2012, 2013
07d83abe
MV
4@c Free Software Foundation, Inc.
5@c See the file guile.texi for copying conditions.
6
07d83abe
MV
7@node Debugging
8@section Debugging Infrastructure
9
b20ef3a6 10@cindex Debugging
5af872e1 11In order to understand Guile's debugging facilities, you first need to
42cb9b03 12understand a little about how Guile represent the Scheme control stack.
a1652dec
AW
13With that in place we explain the low level trap calls that the virtual
14machine can be configured to make, and the trap and breakpoint
5af872e1
NJ
15infrastructure that builds on top of those calls.
16
07d83abe 17@menu
5af872e1 18* Evaluation Model:: Evaluation and the Scheme stack.
5db7c0bf 19* Source Properties:: From expressions to source locations.
659c1e29 20* Programmatic Error Handling:: Debugging when an error occurs.
3b541ca2 21* Traps:: Breakpoints, tracepoints, oh my!
5af872e1
NJ
22@end menu
23
24@node Evaluation Model
25@subsection Evaluation and the Scheme Stack
26
42cb9b03
AW
27The idea of the Scheme stack is central to a lot of debugging. The
28Scheme stack is a reified representation of the pending function returns
a1652dec 29in an expression's continuation. As Guile implements function calls
42cb9b03 30using a stack, this reification takes the form of a number of nested
a1652dec
AW
31stack frames, each of which corresponds to the application of a
32procedure to a set of arguments.
42cb9b03
AW
33
34A Scheme stack always exists implicitly, and can be summoned into
35concrete existence as a first-class Scheme value by the
36@code{make-stack} call, so that an introspective Scheme program -- such
37as a debugger -- can present it in some way and allow the user to query
38its details. The first thing to understand, therefore, is how Guile's
39function call convention creates the stack.
40
41Broadly speaking, Guile represents all control flow on a stack. Calling
42a function involves pushing an empty frame on the stack, then evaluating
43the procedure and its arguments, then fixing up the new frame so that it
44points to the old one. Frames on the stack are thus linked together. A
45tail call is the same, except it reuses the existing frame instead of
46pushing on a new one.
47
48In this way, the only frames that are on the stack are ``active''
49frames, frames which need to do some work before the computation is
50complete. On the other hand, a function that has tail-called another
51function will not be on the stack, as it has no work left to do.
5af872e1
NJ
52
53Therefore, when an error occurs in a running program, or the program
54hits a breakpoint, or in fact at any point that the programmer chooses,
55its state at that point can be represented by a @dfn{stack} of all the
a1652dec
AW
56procedure applications that are logically in progress at that time, each
57of which is known as a @dfn{frame}. The programmer can learn more about
58the program's state at that point by inspecting the stack and its
59frames.
5af872e1
NJ
60
61@menu
5db7c0bf
AW
62* Stack Capture:: Reifying a continuation.
63* Stacks:: Accessors for the stack data type.
64* Frames:: Likewise, accessors for stack frames.
07d83abe
MV
65@end menu
66
5db7c0bf
AW
67@node Stack Capture
68@subsubsection Stack Capture
07d83abe 69
5af872e1
NJ
70A Scheme program can use the @code{make-stack} primitive anywhere in its
71code, with first arg @code{#t}, to construct a Scheme value that
72describes the Scheme stack at that point.
73
74@lisp
75(make-stack #t)
76@result{}
a1652dec 77#<stack 25205a0>
5af872e1 78@end lisp
07d83abe 79
5db7c0bf
AW
80Use @code{start-stack} to limit the stack extent captured by future
81@code{make-stack} calls.
82
df0a1002 83@deffn {Scheme Procedure} make-stack obj arg @dots{}
07d83abe
MV
84@deffnx {C Function} scm_make_stack (obj, args)
85Create a new stack. If @var{obj} is @code{#t}, the current
86evaluation stack is used for creating the stack frames,
87otherwise the frames are taken from @var{obj} (which must be
42cb9b03 88a continuation or a frame object).
07d83abe 89
df0a1002
BT
90@var{arg} @dots{} can be any combination of integer, procedure, prompt
91tag and @code{#t} values.
07d83abe
MV
92
93These values specify various ways of cutting away uninteresting
94stack frames from the top and bottom of the stack that
95@code{make-stack} returns. They come in pairs like this:
96@code{(@var{inner_cut_1} @var{outer_cut_1} @var{inner_cut_2}
97@var{outer_cut_2} @dots{})}.
98
64de6db5 99Each @var{inner_cut_i} can be @code{#t}, an integer, a prompt
42cb9b03
AW
100tag, or a procedure. @code{#t} means to cut away all frames up
101to but excluding the first user module frame. An integer means
102to cut away exactly that number of frames. A prompt tag means
103to cut away all frames that are inside a prompt with the given
104tag. A procedure means to cut away all frames up to but
105excluding the application frame whose procedure matches the
106specified one.
107
64de6db5 108Each @var{outer_cut_i} can be an integer, a prompt tag, or a
42cb9b03
AW
109procedure. An integer means to cut away that number of frames.
110A prompt tag means to cut away all frames that are outside a
111prompt with the given tag. A procedure means to cut away
112frames down to but excluding the application frame whose
07d83abe
MV
113procedure matches the specified one.
114
64de6db5 115If the @var{outer_cut_i} of the last pair is missing, it is
07d83abe
MV
116taken as 0.
117@end deffn
118
5db7c0bf
AW
119@deffn {Scheme Syntax} start-stack id exp
120Evaluate @var{exp} on a new calling stack with identity @var{id}. If
121@var{exp} is interrupted during evaluation, backtraces will not display
122frames farther back than @var{exp}'s top-level form. This macro is a
123way of artificially limiting backtraces and stack procedures, largely as
124a convenience to the user.
125@end deffn
126
07d83abe 127
6e069bbf
AW
128@node Stacks
129@subsubsection Stacks
07d83abe
MV
130
131@deffn {Scheme Procedure} stack? obj
132@deffnx {C Function} scm_stack_p (obj)
133Return @code{#t} if @var{obj} is a calling stack.
134@end deffn
135
136@deffn {Scheme Procedure} stack-id stack
137@deffnx {C Function} scm_stack_id (stack)
138Return the identifier given to @var{stack} by @code{start-stack}.
139@end deffn
140
141@deffn {Scheme Procedure} stack-length stack
142@deffnx {C Function} scm_stack_length (stack)
143Return the length of @var{stack}.
144@end deffn
145
146@deffn {Scheme Procedure} stack-ref stack index
147@deffnx {C Function} scm_stack_ref (stack, index)
148Return the @var{index}'th frame from @var{stack}.
149@end deffn
150
7cd44c6d
MV
151@deffn {Scheme Procedure} display-backtrace stack port [first [depth [highlights]]]
152@deffnx {C Function} scm_display_backtrace_with_highlights (stack, port, first, depth, highlights)
07d83abe 153@deffnx {C Function} scm_display_backtrace (stack, port, first, depth)
fc3d5c43 154Display a backtrace to the output port @var{port}. @var{stack}
07d83abe 155is the stack to take the backtrace from, @var{first} specifies
fc3d5c43
NJ
156where in the stack to start and @var{depth} how many frames
157to display. @var{first} and @var{depth} can be @code{#f},
07d83abe 158which means that default values will be used.
fc3d5c43
NJ
159If @var{highlights} is given it should be a list; the elements
160of this list will be highlighted wherever they appear in the
161backtrace.
07d83abe
MV
162@end deffn
163
164
6e069bbf
AW
165@node Frames
166@subsubsection Frames
07d83abe
MV
167
168@deffn {Scheme Procedure} frame? obj
169@deffnx {C Function} scm_frame_p (obj)
170Return @code{#t} if @var{obj} is a stack frame.
171@end deffn
172
07d83abe
MV
173@deffn {Scheme Procedure} frame-previous frame
174@deffnx {C Function} scm_frame_previous (frame)
175Return the previous frame of @var{frame}, or @code{#f} if
176@var{frame} is the first frame in its stack.
177@end deffn
178
07d83abe
MV
179@deffn {Scheme Procedure} frame-procedure frame
180@deffnx {C Function} scm_frame_procedure (frame)
181Return the procedure for @var{frame}, or @code{#f} if no
182procedure is associated with @var{frame}.
183@end deffn
184
185@deffn {Scheme Procedure} frame-arguments frame
186@deffnx {C Function} scm_frame_arguments (frame)
187Return the arguments of @var{frame}.
188@end deffn
189
5db7c0bf
AW
190@deffn {Scheme Procedure} frame-address frame
191@deffnx {Scheme Procedure} frame-instruction-pointer frame
192@deffnx {Scheme Procedure} frame-stack-pointer frame
193Accessors for the three VM registers associated with this frame: the
194frame pointer (fp), instruction pointer (ip), and stack pointer (sp),
195respectively. @xref{VM Concepts}, for more information.
196@end deffn
197
198@deffn {Scheme Procedure} frame-dynamic-link frame
199@deffnx {Scheme Procedure} frame-return-address frame
200@deffnx {Scheme Procedure} frame-mv-return-address frame
201Accessors for the three saved VM registers in a frame: the previous
202frame pointer, the single-value return address, and the multiple-value
203return address. @xref{Stack Layout}, for more information.
204@end deffn
205
206@deffn {Scheme Procedure} frame-num-locals frame
207@deffnx {Scheme Procedure} frame-local-ref frame i
208@deffnx {Scheme Procedure} frame-local-set! frame i val
209Accessors for the temporary values corresponding to @var{frame}'s
210procedure application. The first local is the first argument given to
211the procedure. After the arguments, there are the local variables, and
212after that temporary values. @xref{Stack Layout}, for more information.
213@end deffn
214
07d83abe
MV
215@deffn {Scheme Procedure} display-application frame [port [indent]]
216@deffnx {C Function} scm_display_application (frame, port, indent)
217Display a procedure application @var{frame} to the output port
218@var{port}. @var{indent} specifies the indentation of the
219output.
220@end deffn
221
5db7c0bf
AW
222Additionally, the @code{(system vm frame)} module defines a number of
223higher-level introspective procedures, for example to retrieve the names
224of local variables, and the source location to correspond to a
225frame. See its source code for more details.
226
07d83abe 227
5af872e1 228@node Source Properties
5db7c0bf 229@subsection Source Properties
5af872e1
NJ
230
231@cindex source properties
232As Guile reads in Scheme code from file or from standard input, it
233remembers the file name, line number and column number where each
42cb9b03
AW
234expression begins. These pieces of information are known as the
235@dfn{source properties} of the expression. Syntax expanders and the
236compiler propagate these source properties to compiled procedures, so
237that, if an error occurs when evaluating the transformed expression,
238Guile's debugger can point back to the file and location where the
239expression originated.
5af872e1 240
b131b233 241The way that source properties are stored means that Guile cannot
38f19074
MW
242associate source properties with individual symbols, keywords,
243characters, booleans, or small integers. This can be seen by typing
b131b233
MW
244@code{(xxx)} and @code{xxx} at the Guile prompt (where the variable
245@code{xxx} has not been defined):
5af872e1
NJ
246
247@example
a1652dec
AW
248scheme@@(guile-user)> (xxx)
249<unnamed port>:4:1: In procedure module-lookup:
250<unnamed port>:4:1: Unbound variable: xxx
251
252scheme@@(guile-user)> xxx
253ERROR: In procedure module-lookup:
254ERROR: Unbound variable: xxx
5af872e1
NJ
255@end example
256
257@noindent
a1652dec
AW
258In the latter case, no source properties were stored, so the error
259doesn't have any source information.
5af872e1 260
76b9bac5
MW
261@deffn {Scheme Procedure} supports-source-properties? obj
262@deffnx {C Function} scm_supports_source_properties_p (obj)
263Return #t if source properties can be associated with @var{obj},
264otherwise return #f.
265@end deffn
266
5af872e1 267The recording of source properties is controlled by the read option
1518f649 268named ``positions'' (@pxref{Scheme Read}). This option is switched
42cb9b03 269@emph{on} by default.
5af872e1
NJ
270
271The following procedures can be used to access and set the source
272properties of read expressions.
273
916f175f
NJ
274@deffn {Scheme Procedure} set-source-properties! obj alist
275@deffnx {C Function} scm_set_source_properties_x (obj, alist)
276Install the association list @var{alist} as the source property
5af872e1
NJ
277list for @var{obj}.
278@end deffn
279
280@deffn {Scheme Procedure} set-source-property! obj key datum
281@deffnx {C Function} scm_set_source_property_x (obj, key, datum)
282Set the source property of object @var{obj}, which is specified by
283@var{key} to @var{datum}. Normally, the key will be a symbol.
284@end deffn
285
286@deffn {Scheme Procedure} source-properties obj
287@deffnx {C Function} scm_source_properties (obj)
288Return the source property association list of @var{obj}.
289@end deffn
290
291@deffn {Scheme Procedure} source-property obj key
292@deffnx {C Function} scm_source_property (obj, key)
916f175f
NJ
293Return the property specified by @var{key} from @var{obj}'s source
294properties.
5af872e1
NJ
295@end deffn
296
b131b233
MW
297If the @code{positions} reader option is enabled, supported expressions
298will have values set for the @code{filename}, @code{line} and
42cb9b03 299@code{column} properties.
5af872e1 300
925172cf
AW
301Source properties are also associated with syntax objects. Procedural
302macros can get at the source location of their input using the
303@code{syntax-source} accessor. @xref{Syntax Transformer Helpers}, for
304more.
305
306Guile also defines a couple of convenience macros built on
307@code{syntax-source}:
308
309@deffn {Scheme Syntax} current-source-location
310Expands to the source properties corresponding to the location of the
311@code{(current-source-location)} form.
312@end deffn
313
314@deffn {Scheme Syntax} current-filename
315Expands to the current filename: the filename that the
316@code{(current-filename)} form appears in. Expands to @code{#f} if this
317information is unavailable.
318@end deffn
319
1fc8dcc7
AW
320If you're stuck with defmacros (@pxref{Defmacros}), and want to preserve
321source information, the following helper function might be useful to
322you:
07d83abe 323
1fc8dcc7
AW
324@deffn {Scheme Procedure} cons-source xorig x y
325@deffnx {C Function} scm_cons_source (xorig, x, y)
326Create and return a new pair whose car and cdr are @var{x} and @var{y}.
327Any source properties associated with @var{xorig} are also associated
328with the new pair.
07d83abe
MV
329@end deffn
330
331
659c1e29
AW
332@node Programmatic Error Handling
333@subsection Programmatic Error Handling
334
335For better or for worse, all programs have bugs, and dealing with bugs
336is part of programming. This section deals with that class of bugs that
337causes an exception to be raised -- from your own code, from within a
338library, or from Guile itself.
339
340@menu
341* Catching Exceptions:: Handling errors after the stack is unwound.
342* Capturing Stacks:: Capturing the stack at the time of error.
343* Pre-Unwind Debugging:: Debugging before the exception is thrown.
344* Debug Options:: A historical interface to debugging.
345@end menu
346
347@node Catching Exceptions
348@subsubsection Catching Exceptions
5af872e1 349
2202fd6c
NJ
350A common requirement is to be able to show as much useful context as
351possible when a Scheme program hits an error. The most immediate
352information about an error is the kind of error that it is -- such as
353``division by zero'' -- and any parameters that the code which signalled
354the error chose explicitly to provide. This information originates with
355the @code{error} or @code{throw} call (or their C code equivalents, if
356the error is detected by C code) that signals the error, and is passed
357automatically to the handler procedure of the innermost applicable
42cb9b03 358@code{catch} or @code{with-throw-handler} expression.
2202fd6c 359
2202fd6c
NJ
360Therefore, to catch errors that occur within a chunk of Scheme code, and
361to intercept basic information about those errors, you need to execute
42cb9b03
AW
362that code inside the dynamic context of a @code{catch} or
363@code{with-throw-handler} expression, or the equivalent in C. In Scheme,
364this means you need something like this:
2202fd6c
NJ
365
366@lisp
367(catch #t
368 (lambda ()
369 ;; Execute the code in which
370 ;; you want to catch errors here.
371 ...)
372 (lambda (key . parameters)
373 ;; Put the code which you want
374 ;; to handle an error here.
375 ...))
376@end lisp
377
378@noindent
659c1e29
AW
379The @code{catch} here can also be @code{with-throw-handler}; see
380@ref{Throw Handlers} for information on the when you might want to use
381@code{with-throw-handler} instead of @code{catch}.
382
383For example, to print out a message and return #f when an error occurs,
384you might use:
385
386@smalllisp
387(define (catch-all thunk)
388 (catch #t
389 thunk
390 (lambda (key . parameters)
391 (format (current-error-port)
392 "Uncaught throw to '~a: ~a\n" key parameters)
393 #f)))
394
395(catch-all
396 (lambda () (error "Not a vegetable: tomato")))
3b3518e7 397@print{} Uncaught throw to 'misc-error: (#f ~A (Not a vegetable: tomato) #f)
659c1e29
AW
398@result{} #f
399@end smalllisp
400
401The @code{#t} means that the catch is applicable to all kinds of error.
402If you want to restrict your catch to just one kind of error, you can
403put the symbol for that kind of error instead of @code{#t}. The
404equivalent to this in C would be something like this:
2202fd6c
NJ
405
406@lisp
407SCM my_body_proc (void *body_data)
408@{
409 /* Execute the code in which
410 you want to catch errors here. */
411 ...
412@}
413
fc3d5c43
NJ
414SCM my_handler_proc (void *handler_data,
415 SCM key,
416 SCM parameters)
2202fd6c
NJ
417@{
418 /* Put the code which you want
419 to handle an error here. */
420 ...
421@}
422
423@{
424 ...
425 scm_c_catch (SCM_BOOL_T,
426 my_body_proc, body_data,
427 my_handler_proc, handler_data,
428 NULL, NULL);
429 ...
430@}
431@end lisp
432
433@noindent
434Again, as with the Scheme version, @code{scm_c_catch} could be replaced
42cb9b03
AW
435by @code{scm_c_with_throw_handler}, and @code{SCM_BOOL_T} could instead
436be the symbol for a particular kind of error.
2202fd6c 437
659c1e29 438@node Capturing Stacks
2202fd6c
NJ
439@subsubsection Capturing the full error stack
440
441The other interesting information about an error is the full Scheme
442stack at the point where the error occurred; in other words what
443innermost expression was being evaluated, what was the expression that
444called that one, and so on. If you want to write your code so that it
42cb9b03 445captures and can display this information as well, there are a couple
2202fd6c
NJ
446important things to understand.
447
42cb9b03 448Firstly, the stack at the point of the error needs to be explicitly
2202fd6c 449captured by a @code{make-stack} call (or the C equivalent
fc3d5c43 450@code{scm_make_stack}). The Guile library does not do this
2202fd6c
NJ
451``automatically'' for you, so you will need to write code with a
452@code{make-stack} or @code{scm_make_stack} call yourself. (We emphasise
453this point because some people are misled by the fact that the Guile
454interactive REPL code @emph{does} capture and display the stack
455automatically. But the Guile interactive REPL is itself a Scheme
456program@footnote{In effect, it is the default program which is run when
457no commands or script file are specified on the Guile command line.}
458running on top of the Guile library, and which uses @code{catch} and
459@code{make-stack} in the way we are about to describe to capture the
460stack when an error occurs.)
461
42cb9b03
AW
462And secondly, in order to capture the stack effectively at the point
463where the error occurred, the @code{make-stack} call must be made before
464Guile unwinds the stack back to the location of the prevailing catch
465expression. This means that the @code{make-stack} call must be made
466within the handler of a @code{with-throw-handler} expression, or the
467optional "pre-unwind" handler of a @code{catch}. (For the full story of
468how these alternatives differ from each other, see @ref{Exceptions}. The
469main difference is that @code{catch} terminates the error, whereas
470@code{with-throw-handler} only intercepts it temporarily and then allow
fc3d5c43
NJ
471it to continue propagating up to the next innermost handler.)
472
473So, here are some examples of how to do all this in Scheme and in C.
474For the purpose of these examples we assume that the captured stack
475should be stored in a variable, so that it can be displayed or
476arbitrarily processed later on. In Scheme:
477
478@lisp
479(let ((captured-stack #f))
480 (catch #t
481 (lambda ()
482 ;; Execute the code in which
483 ;; you want to catch errors here.
484 ...)
485 (lambda (key . parameters)
486 ;; Put the code which you want
487 ;; to handle an error after the
488 ;; stack has been unwound here.
489 ...)
490 (lambda (key . parameters)
491 ;; Capture the stack here:
492 (set! captured-stack (make-stack #t))))
493 ...
494 (if captured-stack
495 (begin
496 ;; Display or process the captured stack.
497 ...))
498 ...)
499@end lisp
500
501@noindent
502And in C:
503
504@lisp
505SCM my_body_proc (void *body_data)
506@{
507 /* Execute the code in which
508 you want to catch errors here. */
509 ...
510@}
511
512SCM my_handler_proc (void *handler_data,
513 SCM key,
514 SCM parameters)
515@{
516 /* Put the code which you want
517 to handle an error after the
518 stack has been unwound here. */
519 ...
520@}
521
522SCM my_preunwind_proc (void *handler_data,
523 SCM key,
524 SCM parameters)
525@{
526 /* Capture the stack here: */
527 *(SCM *)handler_data = scm_make_stack (SCM_BOOL_T, SCM_EOL);
528@}
529
530@{
531 SCM captured_stack = SCM_BOOL_F;
532 ...
533 scm_c_catch (SCM_BOOL_T,
534 my_body_proc, body_data,
535 my_handler_proc, handler_data,
536 my_preunwind_proc, &captured_stack);
537 ...
538 if (captured_stack != SCM_BOOL_F)
539 @{
540 /* Display or process the captured stack. */
541 ...
542 @}
543 ...
544@}
545@end lisp
546
fc3d5c43
NJ
547Once you have a captured stack, you can interrogate and display its
548details in any way that you want, using the @code{stack-@dots{}} and
6e069bbf
AW
549@code{frame-@dots{}} API described in @ref{Stacks} and
550@ref{Frames}.
fc3d5c43
NJ
551
552If you want to print out a backtrace in the same format that the Guile
553REPL does, you can use the @code{display-backtrace} procedure to do so.
554You can also use @code{display-application} to display an individual
659c1e29 555frame in the Guile REPL format.
fc3d5c43 556
659c1e29
AW
557@node Pre-Unwind Debugging
558@subsubsection Pre-Unwind Debugging
fc3d5c43 559
659c1e29
AW
560Instead of saving a stack away and waiting for the @code{catch} to
561return, you can handle errors directly, from within the pre-unwind
562handler.
563
564For example, to show a backtrace when an error is thrown, you might want
565to use a procedure like this:
566
567@lisp
568(define (with-backtrace thunk)
569 (with-throw-handler #t
570 thunk
571 (lambda args (backtrace))))
572(with-backtrace (lambda () (error "Not a vegetable: tomato")))
573@end lisp
574
575Since we used @code{with-throw-handler} here, we didn't actually catch
576the error. @xref{Throw Handlers}, for more information. However, we did
577print out a context at the time of the error, using the built-in
578procedure, @code{backtrace}.
fc3d5c43 579
5af872e1
NJ
580@deffn {Scheme Procedure} backtrace [highlights]
581@deffnx {C Function} scm_backtrace_with_highlights (highlights)
582@deffnx {C Function} scm_backtrace ()
659c1e29
AW
583Display a backtrace of the current stack to the current output port. If
584@var{highlights} is given it should be a list; the elements of this list
585will be highlighted wherever they appear in the backtrace.
5af872e1
NJ
586@end deffn
587
659c1e29
AW
588The Guile REPL code (in @file{system/repl/repl.scm} and related files)
589uses a @code{catch} with a pre-unwind handler to capture the stack when
590an error occurs in an expression that was typed into the REPL, and debug
591that stack interactively in the context of the error.
592
593These procedures are available for use by user programs, in the
594@code{(system repl error-handling)} module.
595
596@lisp
597(use-modules (system repl error-handling))
598@end lisp
5b2da4cc 599
1f603ae2
AW
600@deffn {Scheme Procedure} call-with-error-handling thunk @
601 [#:on-error on-error='debug] [#:post-error post-error='catch] @
602 [#:pass-keys pass-keys='(quit)] [#:trap-handler trap-handler='debug]
603Call a thunk in a context in which errors are handled.
604
605There are four keyword arguments:
606
607@table @var
608@item on-error
609Specifies what to do before the stack is unwound.
610
611Valid options are @code{debug} (the default), which will enter a
612debugger; @code{pass}, in which case nothing is done, and the exception
613is rethrown; or a procedure, which will be the pre-unwind handler.
614
615@item post-error
616Specifies what to do after the stack is unwound.
617
618Valid options are @code{catch} (the default), which will silently catch
619errors, returning the unspecified value; @code{report}, which prints out
620a description of the error (via @code{display-error}), and then returns
621the unspecified value; or a procedure, which will be the catch handler.
622
623@item trap-handler
624Specifies a trap handler: what to do when a breakpoint is hit.
625
626Valid options are @code{debug}, which will enter the debugger;
627@code{pass}, which does nothing; or @code{disabled}, which disables
628traps entirely. @xref{Traps}, for more information.
629
630@item pass-keys
631A set of keys to ignore, as a list.
632@end table
5af872e1
NJ
633@end deffn
634
659c1e29 635@node Debug Options
1cfdb1bb
AW
636@subsubsection Debug options
637
c005daf9
AW
638The behavior of the @code{backtrace} procedure and of the default error
639handler can be parameterized via the debug options.
1cfdb1bb
AW
640
641@cindex options - debug
642@cindex debug options
643@deffn {Scheme Procedure} debug-options [setting]
644Display the current settings of the debug options. If @var{setting} is
645omitted, only a short form of the current read options is printed.
646Otherwise if @var{setting} is the symbol @code{help}, a complete options
647description is displayed.
648@end deffn
649
650The set of available options, and their default values, may be had by
651invoking @code{debug-options} at the prompt.
652
653@smallexample
654scheme@@(guile-user)>
655backwards no Display backtrace in anti-chronological order.
656width 79 Maximal width of backtrace.
657depth 20 Maximal length of printed backtrace.
658backtrace yes Show backtrace on error.
659stack 1048576 Stack size limit (measured in words;
660 0 = no check).
661show-file-name #t Show file names and line numbers in backtraces
662 when not `#f'. A value of `base' displays only
663 base names, while `#t' displays full names.
664warn-deprecated no Warn when deprecated features are used.
665@end smallexample
666
667The boolean options may be toggled with @code{debug-enable} and
668@code{debug-disable}. The non-boolean @code{keywords} option must be set
669using @code{debug-set!}.
670
671@deffn {Scheme Procedure} debug-enable option-name
672@deffnx {Scheme Procedure} debug-disable option-name
1233b383 673@deffnx {Scheme Syntax} debug-set! option-name value
1cfdb1bb
AW
674Modify the debug options. @code{debug-enable} should be used with boolean
675options and switches them on, @code{debug-disable} switches them off.
1233b383
AW
676
677@code{debug-set!} can be used to set an option to a specific value. Due
678to historical oddities, it is a macro that expects an unquoted option
679name.
1cfdb1bb
AW
680@end deffn
681
682@subsubheading Stack overflow
683
684@cindex overflow, stack
685@cindex stack overflow
686Stack overflow errors are caused by a computation trying to use more
3b3518e7
AW
687stack space than has been enabled by the @code{stack} option. There are
688actually two kinds of stack that can overflow, the C stack and the
689Scheme stack.
690
691Scheme stack overflows can occur if Scheme procedures recurse too far
692deeply. An example would be the following recursive loop:
693
694@lisp
695scheme@@(guile-user)> (let lp () (+ 1 (lp)))
696<unnamed port>:8:17: In procedure vm-run:
697<unnamed port>:8:17: VM: Stack overflow
698@end lisp
699
700The default stack size should allow for about 10000 frames or so, so one
701usually doesn't hit this level of recursion. Unfortunately there is no
702way currently to make a VM with a bigger stack. If you are in this
703unfortunate situation, please file a bug, and in the meantime, rewrite
704your code to be tail-recursive (@pxref{Tail Calls}).
705
706The other limit you might hit would be C stack overflows. If you call a
707primitive procedure which then calls a Scheme procedure in a loop, you
708will consume C stack space. Guile tries to detect excessive consumption
709of C stack space, throwing an error when you have hit 80% of the
710process' available stack (as allocated by the operating system), or 160
711kilowords in the absence of a strict limit.
712
713For example, looping through @code{call-with-vm}, a primitive that calls
714a thunk, gives us the following:
1cfdb1bb
AW
715
716@lisp
3b3518e7
AW
717scheme@@(guile-user)> (use-modules (system vm vm))
718scheme@@(guile-user)> (debug-set! stack 10000)
719scheme@@(guile-user)> (let lp () (call-with-vm (the-vm) lp))
720ERROR: In procedure call-with-vm:
1cfdb1bb 721ERROR: Stack overflow
1cfdb1bb
AW
722@end lisp
723
724If you get an error like this, you can either try rewriting your code to
725use less stack space, or increase the maximum stack size. To increase
726the maximum stack size, use @code{debug-set!}, for example:
727
728@lisp
729(debug-set! stack 200000)
1cfdb1bb
AW
730@end lisp
731
3b3518e7
AW
732But of course it's better to have your code operate without so much
733resource consumption, avoiding loops through C trampolines.
1cfdb1bb 734
62ae9557 735
24dbb5ed
NJ
736@node Traps
737@subsection Traps
62ae9557
NJ
738
739@cindex Traps
6e069bbf 740@cindex VM hooks
62ae9557
NJ
741@cindex Breakpoints
742@cindex Trace
743@cindex Tracing
744@cindex Code coverage
745@cindex Profiling
42cb9b03 746Guile's virtual machine can be configured to call out at key points to
6e069bbf
AW
747arbitrary user-specified procedures.
748
749In principle, these @dfn{hooks} allow Scheme code to implement any model
750it chooses for examining the evaluation stack as program execution
751proceeds, and for suspending execution to be resumed later.
752
753VM hooks are very low-level, though, and so Guile also has a library of
754higher-level @dfn{traps} on top of the VM hooks. A trap is an execution
755condition that, when fulfilled, will fire a handler. For example, Guile
756defines a trap that fires when control reaches a certain source
757location.
758
759Finally, Guile also defines a third level of abstractions: per-thread
760@dfn{trap states}. A trap state exists to give names to traps, and to
761hold on to the set of traps so that they can be enabled, disabled, or
762removed. The trap state infrastructure defines the most useful
763abstractions for most cases. For example, Guile's REPL uses trap state
764functions to set breakpoints and tracepoints.
24dbb5ed
NJ
765
766The following subsections describe all this in detail, for both the
767user wanting to use traps, and the developer interested in
62ae9557
NJ
768understanding how the interface hangs together.
769
770
6e069bbf
AW
771@menu
772* VM Hooks:: Modifying Guile's virtual machine.
773* Trap Interface:: Traps are on or off.
774* Low-Level Traps:: The various kinds of low-level traps.
775* Tracing Traps:: Traps to trace procedure calls and returns.
776* Trap States:: One state (per thread) to bind them.
63e36ea6 777* High-Level Traps:: The highest-level trap interface. Use this.
6e069bbf 778@end menu
42cb9b03 779
42cb9b03 780
6e069bbf
AW
781@node VM Hooks
782@subsubsection VM Hooks
42cb9b03 783
6e069bbf
AW
784Everything that runs in Guile runs on its virtual machine, a C program
785that defines a number of operations that Scheme programs can
786perform.
62ae9557 787
6e069bbf
AW
788Note that there are multiple VM ``engines'' for Guile. Only some of them
789have support for hooks compiled in. Normally the deal is that you get
790hooks if you are running interactively, and otherwise they are disabled,
791as they do have some overhead (about 10 or 20 percent).
62ae9557 792
6e069bbf
AW
793To ensure that you are running with hooks, pass @code{--debug} to Guile
794when running your program, or otherwise use the @code{call-with-vm} and
795@code{set-vm-engine!} procedures to ensure that you are running in a VM
796with the @code{debug} engine.
62ae9557 797
6e069bbf
AW
798To digress, Guile's VM has 6 different hooks (@pxref{Hooks}) that can be
799fired at different times, which may be accessed with the following
800procedures.
62ae9557 801
6e069bbf 802All hooks are called with one argument, the frame in
5db7c0bf
AW
803question. @xref{Frames}. Since these hooks may be fired very
804frequently, Guile does a terrible thing: it allocates the frames on the
805C stack instead of the garbage-collected heap.
62ae9557 806
6e069bbf
AW
807The upshot here is that the frames are only valid within the dynamic
808extent of the call to the hook. If a hook procedure keeps a reference to
809the frame outside the extent of the hook, bad things will happen.
62ae9557 810
6e069bbf 811The interface to hooks is provided by the @code{(system vm vm)} module:
62ae9557 812
6e069bbf
AW
813@example
814(use-modules (system vm vm))
815@end example
62ae9557 816
6e069bbf
AW
817@noindent
818The result of calling @code{the-vm} is usually passed as the @var{vm}
819argument to all of these procedures.
62ae9557 820
6e069bbf
AW
821@deffn {Scheme Procedure} vm-next-hook vm
822The hook that will be fired before an instruction is retired (and
823executed).
824@end deffn
62ae9557 825
6e069bbf
AW
826@deffn {Scheme Procedure} vm-push-continuation-hook vm
827The hook that will be fired after preparing a new frame. Fires just
828before applying a procedure in a non-tail context, just before the
829corresponding apply-hook.
830@end deffn
62ae9557 831
6e069bbf
AW
832@deffn {Scheme Procedure} vm-pop-continuation-hook vm
833The hook that will be fired before returning from a frame.
62ae9557 834
6e069bbf
AW
835This hook is a bit trickier than the rest, in that there is a particular
836interpretation of the values on the stack. Specifically, the top value
837on the stack is the number of values being returned, and the next
838@var{n} values are the actual values being returned, with the last value
839highest on the stack.
840@end deffn
62ae9557 841
6e069bbf
AW
842@deffn {Scheme Procedure} vm-apply-hook vm
843The hook that will be fired before a procedure is applied. The frame's
844procedure will have already been set to the new procedure.
62ae9557 845
6e069bbf
AW
846Note that procedure application is somewhat orthogonal to continuation
847pushes and pops. A non-tail call to a procedure will result first in a
848firing of the push-continuation hook, then this application hook,
849whereas a tail call will run without having fired a push-continuation
850hook.
851@end deffn
62ae9557 852
6e069bbf
AW
853@deffn {Scheme Procedure} vm-abort-continuation-hook vm
854The hook that will be called after aborting to a
855prompt. @xref{Prompts}. The stack will be in the same state as for
856@code{vm-pop-continuation-hook}.
857@end deffn
62ae9557 858
6e069bbf
AW
859@deffn {Scheme Procedure} vm-restore-continuation-hook vm
860The hook that will be called after restoring an undelimited
861continuation. Unfortunately it's not currently possible to introspect on
862the values that were given to the continuation.
863@end deffn
62ae9557 864
6e069bbf
AW
865@cindex VM trace level
866These hooks do impose a performance penalty, if they are on. Obviously,
867the @code{vm-next-hook} has quite an impact, performance-wise. Therefore
868Guile exposes a single, heavy-handed knob to turn hooks on or off, the
869@dfn{VM trace level}. If the trace level is positive, hooks run;
870otherwise they don't.
62ae9557 871
5db7c0bf
AW
872For convenience, when the VM fires a hook, it does so with the trap
873level temporarily set to 0. That way the hooks don't fire while you're
874handling a hook. The trace level is restored to whatever it was once the hook
875procedure finishes.
876
6e069bbf
AW
877@deffn {Scheme Procedure} vm-trace-level vm
878Retrieve the ``trace level'' of the VM. If positive, the trace hooks
879associated with @var{vm} will be run. The initial trace level is 0.
880@end deffn
62ae9557 881
6e069bbf
AW
882@deffn {Scheme Procedure} set-vm-trace-level! vm level
883Set the ``trace level'' of the VM.
884@end deffn
885
886@xref{A Virtual Machine for Guile}, for more information on Guile's
887virtual machine.
888
889@node Trap Interface
890@subsubsection Trap Interface
891
892The capabilities provided by hooks are great, but hooks alone rarely
893correspond to what users want to do.
894
895For example, if a user wants to break when and if control reaches a
896certain source location, how do you do it? If you install a ``next''
897hook, you get unacceptable overhead for the execution of the entire
898program. It would be possible to install an ``apply'' hook, then if the
899procedure encompasses those source locations, install a ``next'' hook,
900but already you're talking about one concept that might be implemented
901by a varying number of lower-level concepts.
902
903It's best to be clear about things and define one abstraction for all
904such conditions: the @dfn{trap}.
905
906Considering the myriad capabilities offered by the hooks though, there
907is only a minimum of functionality shared by all traps. Guile's current
908take is to reduce this to the absolute minimum, and have the only
909standard interface of a trap be ``turn yourself on'' or ``turn yourself
910off''.
911
912This interface sounds a bit strange, but it is useful to procedurally
913compose higher-level traps from lower-level building blocks. For
914example, Guile defines a trap that calls one handler when control enters
915a procedure, and another when control leaves the procedure. Given that
916trap, one can define a trap that adds to the next-hook only when within
917a given procedure. Building further, one can define a trap that fires
918when control reaches particular instructions within a procedure.
919
920Or of course you can stop at any of these intermediate levels. For
921example, one might only be interested in calls to a given procedure. But
922the point is that a simple enable/disable interface is all the
923commonality that exists between the various kinds of traps, and
924furthermore that such an interface serves to allow ``higher-level''
925traps to be composed from more primitive ones.
926
927Specifically, a trap, in Guile, is a procedure. When a trap is created,
928by convention the trap is enabled; therefore, the procedure that is the
929trap will, when called, disable the trap, and return a procedure that
930will enable the trap, and so on.
931
932Trap procedures take one optional argument: the current frame. (A trap
933may want to add to different sets of hooks depending on the frame that
934is current at enable-time.)
935
936If this all sounds very complicated, it's because it is. Some of it is
937essential, but probably most of it is not. The advantage of using this
938minimal interface is that composability is more lexically apparent than
939when, for example, using a stateful interface based on GOOPS. But
940perhaps this reflects the cognitive limitations of the programmer who
941made the current interface more than anything else.
942
943@node Low-Level Traps
944@subsubsection Low-Level Traps
945
5db7c0bf
AW
946To summarize the last sections, traps are enabled or disabled, and when
947they are enabled, they add to various VM hooks.
948
949Note, however, that @emph{traps do not increase the VM trace level}. So
950if you create a trap, it will be enabled, but unless something else
951increases the VM's trace level (@pxref{VM Hooks}), the trap will not
952fire. It turns out that getting the VM trace level right is tricky
953without a global view of what traps are enabled. @xref{Trap States},
954for Guile's answer to this problem.
955
956Traps are created by calling procedures. Most of these procedures share
957a set of common keyword arguments, so rather than document them
958separately, we discuss them all together here:
959
960@table @code
961@item #:vm
962The VM to instrument. Defaults to the current thread's VM.
963@item #:closure?
964For traps that depend on the current frame's procedure, this argument
965specifies whether to trap on the only the specific procedure given, or
966on any closure that has the given procedure's code. Defaults to
967@code{#f}.
968@item #:current-frame
969For traps that enable more hooks depending on their dynamic context,
970this argument gives the current frame that the trap is running in.
971Defaults to @code{#f}.
972@end table
973
63e36ea6
AW
974To have access to these procedures, you'll need to have imported the
975@code{(system vm traps)} module:
976
977@lisp
978(use-modules (system vm traps))
979@end lisp
980
6e069bbf 981@deffn {Scheme Procedure} trap-at-procedure-call proc handler @
5db7c0bf
AW
982 [#:vm] [#:closure?]
983A trap that calls @var{handler} when @var{proc} is applied.
6e069bbf 984@end deffn
62ae9557 985
6e069bbf 986@deffn {Scheme Procedure} trap-in-procedure proc @
5db7c0bf
AW
987 enter-handler exit-handler [#:current-frame] [#:vm] [#:closure?]
988A trap that calls @var{enter-handler} when control enters @var{proc},
989and @var{exit-handler} when control leaves @var{proc}.
6e069bbf
AW
990
991Control can enter a procedure via:
5db7c0bf
AW
992@itemize
993@item
994A procedure call.
995@item
996A return to a procedure's frame on the stack.
997@item
998A continuation returning directly to an application of this procedure.
999@end itemize
6e069bbf
AW
1000
1001Control can leave a procedure via:
5db7c0bf
AW
1002@itemize
1003@item
1004A normal return from the procedure.
1005@item
1006An application of another procedure.
1007@item
1008An invocation of a continuation.
1009@item
1010An abort.
1011@end itemize
62ae9557
NJ
1012@end deffn
1013
6e069bbf 1014@deffn {Scheme Procedure} trap-instructions-in-procedure proc @
5db7c0bf
AW
1015 next-handler exit-handler [#:current-frame] [#:vm] [#:closure?]
1016A trap that calls @var{next-handler} for every instruction executed in
1017@var{proc}, and @var{exit-handler} when execution leaves @var{proc}.
62ae9557
NJ
1018@end deffn
1019
6e069bbf 1020@deffn {Scheme Procedure} trap-at-procedure-ip-in-range proc range @
5db7c0bf
AW
1021 handler [#:current-frame] [#:vm] [#:closure?]
1022A trap that calls @var{handler} when execution enters a range of
1023instructions in @var{proc}. @var{range} is a simple of pairs,
1024@code{((@var{start} . @var{end}) ...)}. The @var{start} addresses are
1025inclusive, and @var{end} addresses are exclusive.
6e069bbf 1026@end deffn
62ae9557 1027
6e069bbf 1028@deffn {Scheme Procedure} trap-at-source-location file user-line handler @
5db7c0bf
AW
1029 [#:current-frame] [#:vm]
1030A trap that fires when control reaches a given source location. The
1031@var{user-line} parameter is one-indexed, as a user counts lines,
1032instead of zero-indexed, as Guile counts lines.
6e069bbf 1033@end deffn
62ae9557 1034
5db7c0bf
AW
1035@deffn {Scheme Procedure} trap-frame-finish frame @
1036 return-handler abort-handler [#:vm]
1037A trap that fires when control leaves the given frame. @var{frame}
1038should be a live frame in the current continuation. @var{return-handler}
1039will be called on a normal return, and @var{abort-handler} on a nonlocal
1040exit.
6e069bbf 1041@end deffn
62ae9557 1042
6e069bbf 1043@deffn {Scheme Procedure} trap-in-dynamic-extent proc @
5db7c0bf
AW
1044 enter-handler return-handler abort-handler [#:vm] [#:closure?]
1045A more traditional dynamic-wind trap, which fires @var{enter-handler}
1046when control enters @var{proc}, @var{return-handler} on a normal return,
1047and @var{abort-handler} on a nonlocal exit.
1048
1049Note that rewinds are not handled, so there is no rewind handler.
62ae9557
NJ
1050@end deffn
1051
6e069bbf 1052@deffn {Scheme Procedure} trap-calls-in-dynamic-extent proc @
5db7c0bf
AW
1053 apply-handler return-handler [#:current-frame] [#:vm] [#:closure?]
1054A trap that calls @var{apply-handler} every time a procedure is applied,
1055and @var{return-handler} for returns, but only during the dynamic extent
1056of an application of @var{proc}.
62ae9557
NJ
1057@end deffn
1058
6e069bbf 1059@deffn {Scheme Procedure} trap-instructions-in-dynamic-extent proc @
5db7c0bf 1060 next-handler [#:current-frame] [#:vm] [#:closure?]
ecb87335 1061A trap that calls @var{next-handler} for all retired instructions within
5db7c0bf 1062the dynamic extent of a call to @var{proc}.
62ae9557
NJ
1063@end deffn
1064
6e069bbf 1065@deffn {Scheme Procedure} trap-calls-to-procedure proc @
5db7c0bf
AW
1066 apply-handler return-handler [#:vm]
1067A trap that calls @var{apply-handler} whenever @var{proc} is applied,
1068and @var{return-handler} when it returns, but with an additional
1069argument, the call depth.
1070
1071That is to say, the handlers will get two arguments: the frame in
1072question, and the call depth (a non-negative integer).
62ae9557
NJ
1073@end deffn
1074
5db7c0bf
AW
1075@deffn {Scheme Procedure} trap-matching-instructions frame-pred handler [#:vm]
1076A trap that calls @var{frame-pred} at every instruction, and if
1077@var{frame-pred} returns a true value, calls @var{handler} on the
1078frame.
62ae9557
NJ
1079@end deffn
1080
6e069bbf
AW
1081@node Tracing Traps
1082@subsubsection Tracing Traps
62ae9557 1083
6e069bbf
AW
1084The @code{(system vm trace)} module defines a number of traps for
1085tracing of procedure applications. When a procedure is @dfn{traced}, it
1086means that every call to that procedure is reported to the user during a
1087program run. The idea is that you can mark a collection of procedures
1088for tracing, and Guile will subsequently print out a line of the form
62ae9557
NJ
1089
1090@lisp
6e069bbf 1091| | (@var{procedure} @var{args} @dots{})
62ae9557
NJ
1092@end lisp
1093
6e069bbf
AW
1094whenever a marked procedure is about to be applied to its arguments.
1095This can help a programmer determine whether a function is being called
1096at the wrong time or with the wrong set of arguments.
1097
1098In addition, the indentation of the output is useful for demonstrating
1099how the traced applications are or are not tail recursive with respect
1100to each other. Thus, a trace of a non-tail recursive factorial
1101implementation looks like this:
1102
1103@lisp
1104scheme@@(guile-user)> (define (fact1 n)
1105 (if (zero? n) 1
1106 (* n (fact1 (1- n)))))
1107scheme@@(guile-user)> ,trace (fact1 4)
1108trace: (fact1 4)
1109trace: | (fact1 3)
1110trace: | | (fact1 2)
1111trace: | | | (fact1 1)
1112trace: | | | | (fact1 0)
1113trace: | | | | 1
1114trace: | | | 1
1115trace: | | 2
1116trace: | 6
1117trace: 24
1118@end lisp
1119
1120While a typical tail recursive implementation would look more like this:
1121
1122@lisp
1123scheme@@(guile-user)> (define (facti acc n)
1124 (if (zero? n) acc
1125 (facti (* n acc) (1- n))))
1126scheme@@(guile-user)> (define (fact2 n) (facti 1 n))
1127scheme@@(guile-user)> ,trace (fact2 4)
1128trace: (fact2 4)
1129trace: (facti 1 4)
1130trace: (facti 4 3)
1131trace: (facti 12 2)
1132trace: (facti 24 1)
1133trace: (facti 24 0)
1134trace: 24
1135@end lisp
1136
90729e71
AW
1137The low-level traps below (@pxref{Low-Level Traps}) share some common
1138options:
1139
1140@table @code
1141@item #:width
1142The maximum width of trace output. Trace printouts will try not to
1143exceed this column, but for highly nested procedure calls, it may be
1144unavoidable. Defaults to 80.
1145@item #:vm
1146The VM on which to add the traps. Defaults to the current thread's VM.
1147@item #:prefix
1148A string to print out before each trace line. As seen above in the
1149examples, defaults to @code{"trace: "}.
1150@end table
1151
63e36ea6
AW
1152To have access to these procedures, you'll need to have imported the
1153@code{(system vm trace)} module:
1154
1155@lisp
1156(use-modules (system vm trace))
1157@end lisp
1158
90729e71
AW
1159@deffn {Scheme Procedure} trace-calls-to-procedure proc @
1160 [#:width] [#:vm] [#:prefix]
1161Print a trace at applications of and returns from @var{proc}.
6e069bbf
AW
1162@end deffn
1163
90729e71
AW
1164@deffn {Scheme Procedure} trace-calls-in-procedure proc @
1165 [#:width] [#:vm] [#:prefix]
1166Print a trace at all applications and returns within the dynamic extent
1167of calls to @var{proc}.
6e069bbf
AW
1168@end deffn
1169
90729e71
AW
1170@deffn {Scheme Procedure} trace-instructions-in-procedure proc [#:width] [#:vm]
1171Print a trace at all instructions executed in the dynamic extent of
1172calls to @var{proc}.
6e069bbf
AW
1173@end deffn
1174
1175In addition, Guile defines a procedure to call a thunk, tracing all
1176procedure calls and returns within the thunk.
1177
994d87be
BT
1178@deffn {Scheme Procedure} call-with-trace thunk [#:calls?=#t] @
1179 [#:instructions?=#f] @
1180 [#:width=80] [#:vm=(the-vm)]
6e069bbf
AW
1181Call @var{thunk}, tracing all execution within its dynamic extent.
1182
1183If @var{calls?} is true, Guile will print a brief report at each
1184procedure call and return, as given above.
1185
1186If @var{instructions?} is true, Guile will also print a message each
1187time an instruction is executed. This is a lot of output, but it is
1188sometimes useful when doing low-level optimization.
1189
1190Note that because this procedure manipulates the VM trace level
1191directly, it doesn't compose well with traps at the REPL.
1192@end deffn
1193
1194@xref{Profile Commands}, for more information on tracing at the REPL.
1195
1196@node Trap States
1197@subsubsection Trap States
1198
de03880a
AW
1199When multiple traps are present in a system, we begin to have a
1200bookkeeping problem. How are they named? How does one disable, enable,
1201or delete them?
1202
1203Guile's answer to this is to keep an implicit per-thread @dfn{trap
1204state}. The trap state object is not exposed to the user; rather, API
1205that works on trap states fetches the current trap state from the
1206dynamic environment.
1207
6b1d1af7 1208Traps are identified by integers. A trap can be enabled, disabled, or
de03880a
AW
1209removed, and can have an associated user-visible name.
1210
63e36ea6
AW
1211These procedures have their own module:
1212
1213@lisp
1214(use-modules (system vm trap-state))
1215@end lisp
1216
de03880a
AW
1217@deffn {Scheme Procedure} add-trap! trap name
1218Add a trap to the current trap state, associating the given @var{name}
1219with it. Returns a fresh trap identifier (an integer).
1220
63e36ea6
AW
1221Note that usually the more specific functions detailed in
1222@ref{High-Level Traps} are used in preference to this one.
de03880a
AW
1223@end deffn
1224
6e069bbf 1225@deffn {Scheme Procedure} list-traps
de03880a
AW
1226List the current set of traps, both enabled and disabled. Returns a list
1227of integers.
6e069bbf
AW
1228@end deffn
1229
1230@deffn {Scheme Procedure} trap-name idx
de03880a
AW
1231Returns the name associated with trap @var{idx}, or @code{#f} if there
1232is no such trap.
6e069bbf
AW
1233@end deffn
1234
1235@deffn {Scheme Procedure} trap-enabled? idx
de03880a
AW
1236Returns @code{#t} if trap @var{idx} is present and enabled, or @code{#f}
1237otherwise.
6e069bbf
AW
1238@end deffn
1239
1240@deffn {Scheme Procedure} enable-trap! idx
de03880a 1241Enables trap @var{idx}.
6e069bbf
AW
1242@end deffn
1243
1244@deffn {Scheme Procedure} disable-trap! idx
de03880a 1245Disables trap @var{idx}.
6e069bbf
AW
1246@end deffn
1247
1248@deffn {Scheme Procedure} delete-trap! idx
de03880a 1249Removes trap @var{idx}, disabling it first, if necessary.
6e069bbf 1250@end deffn
62ae9557 1251
63e36ea6
AW
1252@node High-Level Traps
1253@subsubsection High-Level Traps
1254
1255The low-level trap API allows one to make traps that call procedures,
1256and the trap state API allows one to keep track of what traps are
1257there. But neither of these APIs directly helps you when you want to
1258set a breakpoint, because it's unclear what to do when the trap fires.
1259Do you enter a debugger, or mail a summary of the situation to your
1260great-aunt, or what?
1261
1262So for the common case in which you just want to install breakpoints,
6b1d1af7
AW
1263and then have them all result in calls to one parameterizable procedure,
1264we have the high-level trap interface.
63e36ea6
AW
1265
1266Perhaps we should have started this section with this interface, as it's
1267clearly the one most people should use. But as its capabilities and
1268limitations proceed from the lower layers, we felt that the
6b1d1af7 1269character-building exercise of building a mental model might be helpful.
62ae9557 1270
63e36ea6
AW
1271These procedures share a module with trap states:
1272
1273@lisp
1274(use-modules (system vm trap-state))
1275@end lisp
62ae9557 1276
6e069bbf 1277@deffn {Scheme Procedure} with-default-trap-handler handler thunk
63e36ea6
AW
1278Call @var{thunk} in a dynamic context in which @var{handler} is the
1279current trap handler.
1280
1281Additionally, during the execution of @var{thunk}, the VM trace level
1282(@pxref{VM Hooks}) is set to the number of enabled traps. This ensures
1283that traps will in fact fire.
1284
1285@var{handler} may be @code{#f}, in which case VM hooks are not enabled
1286as they otherwise would be, as there is nothing to handle the traps.
62ae9557
NJ
1287@end deffn
1288
63e36ea6
AW
1289The trace-level-setting behavior of @code{with-default-trap-handler} is
1290one of its more useful aspects, but if you are willing to forgo that,
1291and just want to install a global trap handler, there's a function for
1292that too:
1293
6e069bbf 1294@deffn {Scheme Procedure} install-trap-handler! handler
63e36ea6 1295Set the current thread's trap handler to @var{handler}.
6e069bbf
AW
1296@end deffn
1297
63e36ea6
AW
1298Trap handlers are called when traps installed by procedures from this
1299module fire. The current ``consumer'' of this API is Guile's REPL, but
1300one might easily imagine other trap handlers being used to integrate
1301with other debugging tools.
6e069bbf 1302
63e36ea6
AW
1303@cindex Breakpoints
1304@cindex Setting breakpoints
6e069bbf 1305@deffn {Scheme Procedure} add-trap-at-procedure-call! proc
63e36ea6
AW
1306Install a trap that will fire when @var{proc} is called.
1307
1308This is a breakpoint.
62ae9557
NJ
1309@end deffn
1310
63e36ea6
AW
1311@cindex Tracepoints
1312@cindex Setting tracepoints
6e069bbf 1313@deffn {Scheme Procedure} add-trace-at-procedure-call! proc
63e36ea6
AW
1314Install a trap that will print a tracing message when @var{proc} is
1315called. @xref{Tracing Traps}, for more information.
1316
1317This is a tracepoint.
62ae9557
NJ
1318@end deffn
1319
6e069bbf 1320@deffn {Scheme Procedure} add-trap-at-source-location! file user-line
63e36ea6
AW
1321Install a trap that will fire when control reaches the given source
1322location. @var{user-line} is one-indexed, as users count lines, instead
1323of zero-indexed, as Guile counts lines.
1324
1325This is a source breakpoint.
62ae9557
NJ
1326@end deffn
1327
6e069bbf 1328@deffn {Scheme Procedure} add-ephemeral-trap-at-frame-finish! frame handler
63e36ea6
AW
1329Install a trap that will call @var{handler} when @var{frame} finishes
1330executing. The trap will be removed from the trap state after firing, or
1331on nonlocal exit.
1332
1333This is a finish trap, used to implement the ``finish'' REPL command.
62ae9557
NJ
1334@end deffn
1335
63e36ea6
AW
1336@deffn {Scheme Procedure} add-ephemeral-stepping-trap! frame handler [#:into?] [#:instruction?]
1337Install a trap that will call @var{handler} after stepping to a
1338different source line or instruction. The trap will be removed from the
1339trap state after firing, or on nonlocal exit.
1340
1341If @var{instruction?} is false (the default), the trap will fire when
1342control reaches a new source line. Otherwise it will fire when control
1343reaches a new instruction.
1344
1345Additionally, if @var{into?} is false (not the default), the trap will
1346only fire for frames at or prior to the given frame. If @var{into?} is
1347true (the default), the trap may step into nested procedure
1348invocations.
1349
1350This is a stepping trap, used to implement the ``step'', ``next'',
1351``step-instruction'', and ``next-instruction'' REPL commands.
62ae9557
NJ
1352@end deffn
1353
1354
07d83abe
MV
1355@c Local Variables:
1356@c TeX-master: "guile.texi"
1357@c End: