Added Copyright notice.
[bpt/guile.git] / doc / ref / debugging.texi
CommitLineData
2da09c3f
MV
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
9401323e 7@page
ce9d0562
NJ
8@node Debugging Features
9@chapter Debugging Features
9401323e 10
9a69a50e
NJ
11Guile includes debugging tools to help you work out what is going wrong
12when a program signals an error or behaves differently to how you would
13expect. This chapter describes how to use these tools.
14
15Broadly speaking, Guile's debugging support allows you to do two things:
16
17@itemize @bullet
18@item
19specify @dfn{breakpoints} --- points in the execution of a program where
20execution should pause so you can see what is going on
21
22@item
23examine in detail the ``scene of the crime'' --- in other words, the
24execution context at a breakpoint, or when the last error occurred.
25@end itemize
26
27@noindent
28The details are more complex and more powerful @dots{}
29
9401323e 30@menu
9a69a50e
NJ
31* Debug Last Error:: Debugging the most recent error.
32* Breakpoints:: Setting and manipulating breakpoints.
33* Interactive Debugger:: Using the interactive debugger.
34* Tracing:: Tracing program execution.
9401323e
NJ
35@end menu
36
9401323e 37
9a69a50e
NJ
38@node Debug Last Error
39@section Debugging the Most Recent Error
40
41When an error is signalled, Guile remembers the execution context where
42the error occurred. By default, Guile then displays only the most
43immediate information about where and why the error occurred, for
44example:
45
46@lisp
47(make-string (* 4 (+ 3 #\s)) #\space)
48@print{}
49standard input:2:19: In procedure + in expression (+ 3 #\s):
50standard input:2:19: Wrong type argument: #\s
51ABORT: (wrong-type-arg)
52
53Type "(backtrace)" to get more information or "(debug)" to enter the debugger.
54@end lisp
55
56@noindent
57However, as the message above says, you can obtain much more information
58about the context of the error by typing @code{(backtrace)} or
59@code{(debug)}:
60
61@deffn {Scheme Procedure} backtrace
62@deffnx {C Function} scm_backtrace ()
63Display a backtrace of the stack saved by the last error
64to the current output port.
65@end deffn
66
67@deffn {Scheme Procedure} debug
68Invoke the Guile debugger to explore the context of the last error.
69@end deffn
70
71@code{(backtrace)} displays the Scheme call stack at the point where the
72error occurred:
73
74@lisp
75(backtrace)
76@print{}
77Backtrace:
78In standard input:
79 2: 0* [make-string ...
80 2: 1* [* 4 ...
81 2: 2* [+ 3 #\s]
82
83Type "(debug-enable 'backtrace)" if you would like a backtrace
84automatically if an error occurs in the future.
85@end lisp
86
87@noindent
88In a more complex scenario than this one, this can be extremely useful
89for understanding where and why the error occurred. For more on the
90format of the displayed backtrace, see the subsection below.
91
92@code{(debug)} takes you into Guile's interactive debugger, which
93provides commands that allow you to
94
95@itemize @bullet
96@item
97display the Scheme call stack at the point where the error occurred
98(the @code{backtrace} command --- see @ref{Display Backtrace})
99
100@item
101move up and down the call stack, to see in detail the expression being
102evaluated, or the procedure being applied, in each @dfn{frame} (the
103@code{up}, @code{down}, @code{frame}, @code{position}, @code{info args}
104and @code{info frame} commands --- see @ref{Frame Selection} and
105@ref{Frame Information})
106
107@item
108examine the values of variables and expressions in the context of each
109frame (the @code{evaluate} command --- see @ref{Frame Evaluation}).
110@end itemize
111
112Use of the interactive debugger, including these commands, is described
113in @ref{Interactive Debugger}.
114
115@menu
116* Backtrace Format:: How to interpret a backtrace.
117@end menu
118
119
120@node Backtrace Format
121@subsection How to Interpret a Backtrace
122
123
124@node Breakpoints
125@section Breakpoints
126
127If you are not already familiar with the concept of breakpoints, the
128first subsection below explains how they work are why they are useful.
129
130Broadly speaking, Guile's breakpoint support consists of
131
132@itemize @bullet
133@item
134type-specific features for @emph{creating} breakpoints of various types
135
136@item
137relatively generic features for @emph{manipulating} the behaviour of
138breakpoints once they've been created.
139@end itemize
140
141Different breakpoint types are implemented as different classes in a
142GOOPS hierarchy with common base class @code{<breakpoint>}. The magic
143of generic functions then allows most of the manipulation functions to
144be generic by default but specializable (by breakpoint class) if the
145need arises.
146
147Generic breakpoint support is provided by the @code{(ice-9 debugger
148breakpoints)} module, so you will almost always need to use this module
149in order to access the functionality described here:
150
151@smalllisp
152(use-modules (ice-9 debugger breakpoints))
153@end smalllisp
154
155@noindent
156You may like to add this to your @file{.guile} file.
157
158@menu
159* Breakpoints Overview::
160* Source Breakpoints::
161* Procedural Breakpoints::
162* Setting Breakpoints::
163* break! trace! trace-subtree!::
164* Accessing Breakpoints::
165* Breakpoint Behaviours::
166* Enabling and Disabling::
167* Deleting Breakpoints::
168* Breakpoint Information::
169* Other Breakpoint Types::
170@end menu
171
172
173@node Breakpoints Overview
174@subsection How Breakpoints Work and Why They Are Useful
175
176Often, debugging the last error is not enough to tell you what went
177wrong. For example, the root cause of the error may have arisen a long
178time before the error was signalled, in which case the execution context
179of the error is too late to be useful. Or your program might not signal
180an error at all, just return an unexpected result or have some incorrect
181side effect.
182
183In many such cases, it's useful to pause the program at or before the
184point where you suspect the problem arises. Then you can explore the
185stack, display the values of key variables, and generally check that the
186state of the program is as you expect. If all is well, you can let the
187program continue running normally, or step more slowly through each
188expression that the Scheme interpreter evaluates. Single-stepping may
189reveal that the program is going through blocks of code that you didn't
190intend --- a useful data point for understanding what the underlying
191problem is.
192
193Telling Guile where or when to pause a program is called @dfn{setting a
194breakpoint}. When a breakpoint is hit, Guile's default behaviour is to
195enter the interactive debugger, where there are now two sets of commands
196available:
197
198@itemize @bullet
199@item
200all the commands as described for last error debugging (@pxref{Debug
201Last Error}), which allow you to explore the stack and so on
202
203@item
204additional commands for continuing program execution in various ways:
205@code{next}, @code{step}, @code{finish}, @code{trace-finish} and
206@code{continue}.
207@end itemize
208
209Use of the interactive debugger is described in @ref{Interactive
210Debugger}.
211
212
213@node Source Breakpoints
214@subsection Source Breakpoints
215
216A source breakpoint is a breakpoint that triggers whenever program
217execution hits a particular source location. A source breakpoint can be
218conveniently set simply by evaluating code that has @code{##} inserted
219into it at the position where you want the breakpoint to be.
220
221For example, to set a breakpoint immediately before evaluation of
222@code{(= n 0)} in the following procedure definition, evaluate:
223
224@smalllisp
225(define (fact1 n)
226 (if ##(= n 0)
227 1
228 (* n (fact1 (- n 1)))))
229@print{}
230Set breakpoint 1: standard input:4:9: (= n 0)
231@end smalllisp
232
233@noindent
234Note the message confirming that you have set a breakpoint. If you
235don't see this, something isn't working.
236
237@code{##} is provided by the @code{(ice-9 debugger breakpoints source)} module,
238so you must use this module before trying to set breakpoints in this
239way:
240
241@smalllisp
242(use-modules (ice-9 debugger breakpoints source))
243@end smalllisp
244
245@noindent
246You may like to add this to your @file{.guile} file.
247
248The default behaviour for source breakpoints is @code{debug-here}
249(@pxref{Breakpoint Behaviours}), which means to enter the command line
250debugger when the breakpoint is hit. So, if you now use @code{fact1},
251that is what happens.
252
253@smalllisp
254guile> (fact1 3)
255Hit breakpoint 1: standard input:4:9: (= n 0)
256Frame 3 at standard input:4:9
257 (= n 0)
258debug>
259@end smalllisp
260
261
262@node Procedural Breakpoints
263@subsection Procedural Breakpoints
264
265A procedural breakpoint is a breakpoint that triggers whenever Guile is
266about to apply a specified procedure to its (already evaluated)
267arguments. To set a procedural breakpoint, call @code{break!} with the
268target procedure as a single argument. For example:
269
270@smalllisp
271(define (fact1 n)
272 (if (= n 0)
273 1
274 (* n (fact1 (- n 1)))))
275
276(break! fact1)
277@print{}
278Set breakpoint 1: [fact1]
279@result{}
280#<<procedure-breakpoint> 808b0b0>
281@end smalllisp
282
283Alternatives to @code{break!} are @code{trace!} and
284@code{trace-subtree!}. The difference is that these three calls create
285a breakpoint in the same place but with three different behaviours,
286respectively @code{debug-here}, @code{trace-here} and
287@code{trace-subtree}. Breakpoint behaviours are documented fully later
288(@pxref{Breakpoint Behaviours}), but to give a quick taste, here's an
289example of running code that includes a procedural breakpoint with the
290@code{trace-here} behaviour.
291
292@smalllisp
293(trace! fact1)
294@print{}
295Set breakpoint 1: [fact1]
296@result{}
297#<<procedure-breakpoint> 808b0b0>
298
299(fact1 4)
300@print{}
301| [fact1 4]
302| | [fact1 3]
303| | | [fact1 2]
304| | | | [fact1 1]
305| | | | | [fact1 0]
306| | | | | 1
307| | | | 2
308| | | 6
309| | 24
310| 24
311@result{}
31224
313@end smalllisp
314
315To set and use procedural breakpoints, you will need to use the
316@code{(ice-9 debugger breakpoints procedural)} module:
317
318@smalllisp
319(use-modules (ice-9 debugger breakpoints procedural))
320@end smalllisp
321
322@noindent
323You may like to add this to your @file{.guile} file.
324
325
326@node Setting Breakpoints
327@subsection Setting Breakpoints
328
329In general, that is. We've already seen how to set source and
330procedural breakpoints conveniently in practice. This section explains
331how those conveniences map onto a more general mechanism.
332
333The general mechanism for setting breakpoints is the generic function
334@code{set-breakpoint!}:
335
336@deffn {Generic Function} set-breakpoint! behaviour . location-args
337Set a breakpoint with behaviour @var{behaviour} at the location
338specified by @var{location-args}.
339
340The form of the @var{location-args} depends upon what methods for
341@code{set-breakpoint!} have been provided by the implementations of
342subclasses of the @code{<breakpoint>} base class.
343@end deffn
344
345So, for example, @code{(ice-9 debugger breakpoints procedural)} implements the
346@code{<procedure-breakpoint>} subclass and provides a
347@code{set-breakpoint!} method that takes a procedure argument:
348
349@deffn {Method} set-breakpoint! behaviour (proc <procedure>)
350Set a breakpoint with behaviour @var{behaviour} before applications of
351the procedure @var{proc}.
352@end deffn
353
354@code{(ice-9 debugger breakpoints source)} implements the
355@code{<source-breakpoint>} subclass and provides a
356@code{set-breakpoint!} method that takes two arguments describing the
357source expression on which the breakpoint should be set:
358
359@deffn {Method} set-breakpoint! behaviour x-as-read (x-pairified <pair>)
360Set a breakpoint with behaviour @var{behaviour} on the source expression
361@var{x-pairified}, storing @var{x-as-read} for use in messages
362describing the breakpoint.
363@end deffn
364
365A non-type-specific @code{set-breakpoint!} method is provided by the
366generic module @code{(ice-9 debugger breakpoints)}. It allows you to change the
367behaviour of an existing breakpoint that is identified by its breakpoint
368number:
369
370@deffn {Method} set-breakpoint! behaviour (number <integer>)
371Change the behaviour of existing breakpoint number @var{number} to
372@var{behaviour}.
373@end deffn
374
375
376@node break! trace! trace-subtree!
377@subsection break! trace! trace-subtree!
378
379We have already talked above about the use of @code{break!},
380@code{trace!} and @code{trace-subtree!} for setting procedural
381breakpoints. Now that @code{set-breakpoint!} has been introduced, we
382can reveal that @code{break!}, @code{trace!} and @code{trace-subtree!}
383are in fact just wrappers for @code{set-breakpoint!} that specify
384particular breakpoint behaviours, respectively @code{debug-here},
385@code{trace-here} and @code{trace-subtree}.
386
387@smalllisp
388(break! . @var{args})
389 @equiv{} (set-breakpoint! debug-here . @var{args})
390(trace! . @var{args})
391 @equiv{} (set-breakpoint! trace-here . @var{args})
392(trace-subtree! . @var{args})
393 @equiv{} (set-breakpoint! trace-subtree . @var{args})
394@end smalllisp
395
396This means that these three procedures can be used to set the
397corresponding behaviours for any type of breakpoint for which a
398@code{set-breakpoint!} method exists, not just procedural ones.
399
400
401@node Accessing Breakpoints
402@subsection Accessing Breakpoints
403
404Information about the state and behaviour of a breakpoint is stored in
405an instance of the appropriate breakpoint class. To access and change
406that information, therefore, you need to get hold of the desired
407breakpoint instance.
408
409The generic function @code{get-breakpoint} meets this need:
410
411@deffn {Generic Function} get-breakpoint . location-args
412Find and return the breakpoint instance at the location specified by
413@var{location-args}.
414
415The form of the @var{location-args} depends upon what methods for
416@code{get-breakpoint} have been provided by the implementations of
417subclasses of the @code{<breakpoint>} base class.
418@end deffn
419
420For every @code{set-breakpoint!} method there is a corresponding
421@code{get-breakpoint} method which interprets the @var{location-args} in
422the same way. Since those interpretations are described above
423(@pxref{Setting Breakpoints}), we won't repeat them here, except to note
424again the useful type-independent case:
425
426@smalllisp
427(get-breakpoint @var{number})
428@end smalllisp
429
430@noindent
431returns the breakpoint instance for the existing breakpoint numbered
432@var{number}.
433
434
435@node Breakpoint Behaviours
436@subsection Breakpoint Behaviours
437
438A breakpoint's @dfn{behaviour} determines what happens when that
439breakpoint is hit. Several kinds of behaviour are generally useful.
440
441@table @code
442@item debug-here
443Enter the command line debugger. This gives the opportunity to explore
444the stack, evaluate expressions in any of the pending stack frames,
445change breakpoint properties or set new breakpoints, and continue
446program execution when you are done.
447
448@item trace-here
449Trace the current stack frame. For expressions being evaluated, this
450shows the expression. For procedure applications, it shows the
451procedure name and its arguments @emph{post-evaluation}. For both
452expressions and applications, the indentation of the tracing indicates
453whether the traced items are mutually tail recursive.
454
455@item trace-subtree
456Trace the current stack frame, and enable tracing for all future
457evaluations and applications until the current stack frame is exited.
458@code{trace-subtree} is a great preliminary exploration tool when all
459you know is that there is a bug ``somewhere in XXX or in something that
460XXX calls''.
461
462@item (at-exit @var{thunk})
463Don't do anything now, but arrange for @var{thunk} to be executed when
464the current stack frame is exited. For example, the operation that most
465debugging tools call ``finish'' is @code{(at-exit debug-here)}.
466
467@item (at-next @var{count} @var{thunk})
468@dots{} arrange for @var{thunk} to be executed when beginning the
469@var{count}th next evaluation or application with source location in the
470current file.
471
472@item (at-entry @var{count} @var{thunk})
473@dots{} arrange for @var{thunk} to be executed when beginning the
474@var{count}th next evaluation (regardless of source location).
475
476@item (at-apply @var{count} @var{thunk})
477@dots{} arrange for @var{thunk} to be executed just before performing
478the @var{count}th next application (regardless of source location).
479
480@item (at-step @var{count} @var{thunk})
481Synthesis of @code{at-entry} and @code{at-apply}; counts both
482evaluations and applications.
483@end table
484
485Every breakpoint instance has a slot in which its behaviour is stored.
486If you have a breakpoint instance in hand, you can change its behaviour
487using the @code{bp-behaviour} accessor.
488
489@deffn {Accessor} bp-behaviour breakpoint
490Get or set the behaviour of the breakpoint instance @var{breakpoint}.
491@end deffn
492
493@noindent
494(An @dfn{accessor} supports the setting of a property like this:
495
496@smalllisp
497(set! (bp-behaviour @var{breakpoint}) @var{new-behaviour})
498@end smalllisp
499
500@noindent
501See the GOOPS manual for further information on accessors.)
502
503Alternatively, if you know how to specify the @var{location-args} for
504the breakpoint in question, you can change its behaviour using
505@code{set-breakpoint!}. For example:
506
507@smalllisp
508;; Change behaviour of breakpoint number 2.
509(set-breakpoint! @var{new-behaviour} 2)
510
511;; Change behaviour of procedural breakpoint on [fact1].
512(set-breakpoint! @var{new-behaviour} fact1)
513@end smalllisp
514
515In all cases, the behaviour that you specify should be either a single
516thunk, or a list of thunks, to be called when the breakpoint is hit.
517
518The most common behaviours above are exported as thunks from the
519@code{(ice-9 debugger behaviour)} module. So, if you use this module, you can
520use those behaviours directly like this:
521
522@smalllisp
523(use-modules (ice-9 debugger behaviour))
524(set-breakpoint! trace-subtree 2)
525(set! (bp-behaviour (get-breakpoint 3)) debug-here)
526@end smalllisp
527
528@noindent
529You can also use the list option to combine common behaviours:
530
531@smalllisp
532(set-breakpoint! (list trace-here debug-here) 2)
533@end smalllisp
534
535@noindent
536Or, for more customized behaviour, you could build and use your own
537thunk like this:
538
539@smalllisp
540(define (my-behaviour)
541 (trace-here)
542 (at-exit (lambda ()
543 (display "Exiting frame of my-behaviour bp\n")
544 ... do something unusual ...)))
545
546(set-breakpoint my-behaviour 2)
547@end smalllisp
548
549
550@node Enabling and Disabling
551@subsection Enabling and Disabling
552
553Independently of its behaviour, each breakpoint also keeps track of
554whether it is currently enabled. This is a straightforward convenience
555to allow breakpoints to be temporarily switched off without losing all
556their carefully constructed properties.
557
558If you have a breakpoint instance in hand, you can enable or disable it
559using the @code{bp-enabled?} accessor:
560
561@deffn {Accessor} bp-enabled? breakpoint
562Get or set the enabled state of the specified @var{breakpoint}.
563@end deffn
564
565Alternatively, you can enable or disable a breakpoint by its location
566args:
567
568@deffn {Procedure} enable-breakpoint! . location-args
569@deffnx {Procedure} disable-breakpoint! . location-args
570Enable or disable the breakpoint at the location specified by
571@var{location-args}.
572@end deffn
573
574@code{enable-breakpoint!} and @code{disable-breakpoint!} are implemented
575using @code{get-breakpoint} and @code{bp-enabled?}, so any
576@var{location-args} that are valid for @code{get-breakpoint} will work
577also for these procedures.
578
579
580@node Deleting Breakpoints
581@subsection Deleting Breakpoints
582
583Given a breakpoint instance in hand, you can deactivate it and remove it
584from the global list of current breakpoints by calling
585@code{bp-delete!}:
586
587@deffn {Generic Function} bp-delete! breakpoint
588Delete breakpoint @var{breakpoint}. This means (1) doing whatever is
589needed to prevent the breakpoint from triggering again, and (2) removing
590it from the global list of current breakpoints.
591@end deffn
592
593Alternatively, you can delete a breakpoint by its location args:
594
595@deffn {Procedure} delete-breakpoint! . location-args
596Delete the breakpoint at the location specified by @var{location-args}.
597@end deffn
598
599@code{delete-breakpoint!} is implemented using @code{get-breakpoint} and
600@code{bp-delete!}, so any @var{location-args} that are valid for
601@code{get-breakpoint} will work also for @code{delete-breakpoint!}.
602
603There is no way to reinstate a deleted breakpoint. Final destruction of
604the breakpoint instance is determined by the usual garbage collection
605rules.
606
607
608@node Breakpoint Information
609@subsection Breakpoint Information
610
611To get Guile to print a description of a breakpoint instance, use
612@code{bp-describe}:
613
614@smalllisp
615(bp-describe (get-breakpoint 1) #t)
616@print{}
617Breakpoint 1: [fact1]
618 enabled? = #t
619 behaviour = #<procedure trace-here ()>
620@end smalllisp
621
622@deffn {Generic Function} bp-describe breakpoint port
623Print a description of @var{breakpoint} to the specified @var{port}.
624@var{port} can be @code{#t} for standard output, or else any output
625port.
626@end deffn
627
628Following the usual model, @code{describe-breakpoint} is also provided:
629
630@deffn {Procedure} describe-breakpoint . location-args
631Print (to standard output) a description of the breakpoint at location
632specified by @var{location-args}.
633@end deffn
634
635Finally, two stragglers. @code{all-breakpoints} returns a list of all
636current breakpoints. @code{describe-all-breakpoints} combines
637@code{bp-describe} and @code{all-breakpoints} by printing a description
638of all current breakpoints to standard output.
639
640@deffn {Procedure} all-breakpoints
641Return a list of all current breakpoints, ordered by breakpoint number.
642@end deffn
643
644@deffn {Procedure} describe-all-breakpoints
645Print a description of all current breakpoints to standard output.
646@end deffn
647
648
649@node Other Breakpoint Types
650@subsection Other Breakpoint Types
651
652Besides source and procedural breakpoints, Guile includes an early
653implementation of a third class of breakpoints: @dfn{range} breakpoints.
654These are breakpoints that trigger when program execution enters (or
655perhaps exits) a defined range of source locations.
656
657Sadly, these don't yet work well. The apparent problem is that the
658extra methods for @code{set-breakpoint!} and @code{get-breakpoint} cause
659some kind of explosion in the time taken by GOOPS to construct its
660method cache and to dispatch calls involving these generic functions.
661But we haven't really investigated enough to be sure that this is the
662real issue.
663
664If you're interested in looking and/or investigating anyway, please feel
665free to check out and play with the @code{(ice-9 debugger breakpoints
666range)} module.
667
668The other kind of breakpoint that we'd like to have is watchpoints, but
669this hasn't been implemented at all yet. Watchpoints may turn out to be
670impractical for performance reasons.
671
672
673@node Interactive Debugger
674@section Using the Interactive Debugger
675
676Guile's interactive debugger is a command line application that accepts
677commands from you for examining the stack and, if at a breakpoint, for
678continuing program execution in various ways. Unlike in the normal
679Guile REPL, commands are typed mostly without parentheses.
680
681When you first enter the debugger, it introduces itself with a message
682like this:
683
684@lisp
685This is the Guile debugger -- for help, type `help'.
686There are 3 frames on the stack.
687
688Frame 2 at standard input:36:19
689 [+ 3 #\s]
690debug>
691@end lisp
692
693@noindent
694``debug>'' is the debugger's prompt, and a useful reminder that you are
695not in the normal Guile REPL. The available commands are described in
696detail in the following subsections.
697
698@menu
699* Display Backtrace:: backtrace.
700* Frame Selection:: up, down, frame.
701* Frame Information:: info args, info frame, position.
702* Frame Evaluation:: evaluate.
703* Single Stepping:: step, next.
704* Run To Frame Exit:: finish, trace-finish.
705* Continue Execution:: continue.
706* Leave Debugger:: quit.
707@end menu
708
709
710@node Display Backtrace
711@subsection Display Backtrace
712
713The @code{backtrace} command, which can also be invoked as @code{bt} or
714@code{where}, displays the call stack (aka backtrace) at the point where
715the debugger was entered:
716
717@lisp
718debug> bt
719In standard input:
720 36: 0* [make-string ...
721 36: 1* [* 4 ...
722 36: 2* [+ 3 #\s]
723@end lisp
724
725@deffn {Debugger Command} backtrace [count]
726@deffnx {Debugger Command} bt [count]
727@deffnx {Debugger Command} where [count]
728Print backtrace of all stack frames, or of the innermost @var{count}
8e733f10 729frames. With a negative argument, print the outermost -@var{count}
9a69a50e
NJ
730frames. If the number of frames isn't explicitly given, the debug
731option @code{depth} determines the maximum number of frames printed.
732@end deffn
733
734The format of the displayed backtrace is the same as for the
735@code{backtrace} procedure --- see @ref{Backtrace Format} for details.
736
737
738@node Frame Selection
739@subsection Frame Selection
740
741A call stack consists of a sequence of stack @dfn{frames}, with each
742frame describing one level of the nested evaluations and applications
743that the program was executing when it hit a breakpoint or an error.
744Frames are numbered such that frame 0 is the outermost --- i.e. the
745operation on the call stack that began least recently --- and frame N-1
746the innermost (where N is the total number of frames on the stack).
747
748When you enter the debugger, the innermost frame is selected, which
749means that the commands for getting information about the ``current''
750frame, or for evaluating expressions in the context of the current
751frame, will do so by default with respect to the innermost frame. To
752select a different frame, so that these operations will apply to it
753instead, use the @code{up}, @code{down} and @code{frame} commands like
754this:
755
756@lisp
757debug> up
758Frame 1 at standard input:36:14
8e733f10 759 [* 4 ...
9a69a50e
NJ
760debug> frame 0
761Frame 0 at standard input:36:1
8e733f10 762 [make-string ...
9a69a50e
NJ
763debug> down
764Frame 1 at standard input:36:14
8e733f10 765 [* 4 ...
9a69a50e
NJ
766@end lisp
767
768@deffn {Debugger Command} up [n]
769Move @var{n} frames up the stack. For positive @var{n}, this
770advances toward the outermost frame, to higher frame numbers, to
771frames that have existed longer. @var{n} defaults to one.
772@end deffn
773
774@deffn {Debugger Command} down [n]
775Move @var{n} frames down the stack. For positive @var{n}, this
776advances toward the innermost frame, to lower frame numbers, to frames
777that were created more recently. @var{n} defaults to one.
778@end deffn
779
780@deffn {Debugger Command} frame [n]
781Select and print a stack frame. With no argument, print the selected
782stack frame. (See also ``info frame''.) An argument specifies the
783frame to select; it must be a stack-frame number.
784@end deffn
785
786
787@node Frame Information
788@subsection Frame Information
789
790[to be completed]
791
8e733f10 792@deffn {Debugger Command} {info frame}
9a69a50e
NJ
793All about selected stack frame.
794@end deffn
795
8e733f10 796@deffn {Debugger Command} {info args}
9a69a50e
NJ
797Argument variables of current stack frame.
798@end deffn
799
800@deffn {Debugger Command} position
801Display the position of the current expression.
802@end deffn
803
804
805@node Frame Evaluation
806@subsection Frame Evaluation
807
808[to be completed]
809
810@deffn {Debugger Command} evaluate expression
811Evaluate an expression.
812The expression must appear on the same line as the command,
813however it may be continued over multiple lines.
814@end deffn
815
816
817@node Single Stepping
818@subsection Single Stepping
819
820[to be completed]
821
822@deffn {Debugger Command} step [n]
823Continue until entry to @var{n}th next frame.
824@end deffn
825
826@deffn {Debugger Command} next [n]
827Continue until entry to @var{n}th next frame in same file.
828@end deffn
829
830
831@node Run To Frame Exit
832@subsection Run To Frame Exit
833
834[to be completed]
835
836@deffn {Debugger Command} finish
837Continue until evaluation of the current frame is complete, and
838print the result obtained.
839@end deffn
840
841@deffn {Debugger Command} trace-finish
842Trace until evaluation of the current frame is complete.
843@end deffn
844
845
846@node Continue Execution
847@subsection Continue Execution
848
849[to be completed]
850
851@deffn {Debugger Command} continue
852Continue program execution.
853@end deffn
854
855
856@node Leave Debugger
857@subsection Leave Debugger
858
859[to be completed]
860
861@deffn {Debugger Command} quit
862Exit the debugger.
863@end deffn
864
865
866@node Tracing
867@section Tracing
868
869Tracing has already been described as a breakpoint behaviour
870(@pxref{Breakpoint Behaviours}), but we mention it again here because it
871is so useful, and because Guile actually now has @emph{two} mechanisms
872for tracing, and its worth clarifying the differences between them.
873
874@menu
875* Old Tracing:: Tracing provided by (ice-9 debug).
876* New Tracing:: Breakpoint-based tracing.
877* Tracing Compared:: Differences between old and new.
878@end menu
879
880
881@node Old Tracing
882@subsection Tracing Provided by @code{(ice-9 debug)}
883
884The @code{(ice-9 debug)} module implements tracing of procedure
885applications. When a procedure is @dfn{traced}, it means that every
886call to that procedure is reported to the user during a program run.
887The idea is that you can mark a collection of procedures for tracing,
888and Guile will subsequently print out a line of the form
889
890@smalllisp
891| | [@var{procedure} @var{args} @dots{}]
892@end smalllisp
893
894whenever a marked procedure is about to be applied to its arguments.
895This can help a programmer determine whether a function is being called
896at the wrong time or with the wrong set of arguments.
897
898In addition, the indentation of the output is useful for demonstrating
899how the traced applications are or are not tail recursive with respect
900to each other. Thus, a trace of a non-tail recursive factorial
901implementation looks like this:
902
903@smalllisp
904[fact1 4]
905| [fact1 3]
906| | [fact1 2]
907| | | [fact1 1]
908| | | | [fact1 0]
909| | | | 1
910| | | 1
911| | 2
912| 6
91324
914@end smalllisp
915
916While a typical tail recursive implementation would look more like this:
917
918@smalllisp
919[fact2 4]
920[facti 1 4]
921[facti 4 3]
922[facti 12 2]
923[facti 24 1]
924[facti 24 0]
92524
926@end smalllisp
927
928@deffn {Scheme Procedure} trace procedure
929Enable tracing for @code{procedure}. While a program is being run,
930Guile will print a brief report at each call to a traced procedure,
931advising the user which procedure was called and the arguments that were
9401323e 932passed to it.
9a69a50e 933@end deffn
9401323e 934
9a69a50e
NJ
935@deffn {Scheme Procedure} untrace procedure
936Disable tracing for @code{procedure}.
937@end deffn
9401323e 938
9a69a50e 939Here is another example:
9401323e
NJ
940
941@lisp
942(define (rev ls)
943 (if (null? ls)
944 '()
945 (append (rev (cdr ls))
946 (cons (car ls) '())))) @result{} rev
947
948(trace rev) @result{} (rev)
949
950(rev '(a b c d e))
951@result{} [rev (a b c d e)]
952 | [rev (b c d e)]
953 | | [rev (c d e)]
954 | | | [rev (d e)]
955 | | | | [rev (e)]
956 | | | | | [rev ()]
957 | | | | | ()
958 | | | | (e)
959 | | | (e d)
960 | | (e d c)
961 | (e d c b)
962 (e d c b a)
963 (e d c b a)
964@end lisp
9a69a50e 965
9401323e 966Note the way Guile indents the output, illustrating the depth of
9a69a50e 967execution at each procedure call. This can be used to demonstrate, for
9401323e
NJ
968example, that Guile implements self-tail-recursion properly:
969
970@lisp
971(define (rev ls sl)
972 (if (null? ls)
973 sl
974 (rev (cdr ls)
975 (cons (car ls) sl)))) @result{} rev
976
977(trace rev) @result{} (rev)
978
979(rev '(a b c d e) '())
980@result{} [rev (a b c d e) ()]
981 [rev (b c d e) (a)]
982 [rev (c d e) (b a)]
983 [rev (d e) (c b a)]
984 [rev (e) (d c b a)]
985 [rev () (e d c b a)]
986 (e d c b a)
987 (e d c b a)
988@end lisp
989
990Since the tail call is effectively optimized to a @code{goto} statement,
991there is no need for Guile to create a new stack frame for each
9a69a50e 992iteration. Tracing reveals this optimization in operation.
4c731ece 993
4c731ece 994
9a69a50e
NJ
995@node New Tracing
996@subsection Breakpoint-based Tracing
997
998Guile's newer mechanism implements tracing as an optional behaviour for
999any kind of breakpoint.
1000
1001To trace a procedure (in the same kind of way as the older tracing), use
1002the @code{trace!} procedure to set a procedure breakpoint with
1003@code{trace-here} behaviour:
1004
1005@lisp
1006(trace! fact1)
1007@print{}
1008Set breakpoint 1: [fact1]
1009@result{}
1010#<<procedure-breakpoint> 40337bf0>
1011
1012(fact1 4)
1013@print{}
1014| [fact1 4]
1015| | [fact1 3]
1016| | | [fact1 2]
1017| | | | [fact1 1]
1018| | | | | [fact1 0]
1019| | | | | 1
1020| | | | 2
1021| | | 6
1022| | 24
1023| 24
1024@result{}
102524
1026@end lisp
1027
1028To trace evaluation of a source expression, evaluate code containing a
1029breakpoint marker @code{##} in the appropriate place, then use
1030@code{set-breakpoint} to change the behaviour of the new breakpoint to
1031@code{trace-here}:
1032
1033@lisp
1034(define (fact1 n)
1035 (if ##(= n 0)
1036 1
1037 (* n (fact1 (- n 1)))))
1038@print{}
1039Set breakpoint 4: standard input:13:9: (= n 0)
1040
1041(use-modules (ice-9 debugger behaviour))
1042(set-breakpoint! trace-here 4)
1043@print{}
1044Breakpoint 4: standard input:13:9: (= n 0)
8e733f10
NJ
1045 enabled? = #t
1046 behaviour = #<procedure trace-here ()>
9a69a50e
NJ
1047
1048(fact1 4)
1049@print{}
1050| (= n 0)
1051| #f
1052| (= n 0)
1053| #f
1054| (= n 0)
1055| #f
1056| (= n 0)
1057| #f
1058| (= n 0)
1059| #t
1060@result{}
106124
1062@end lisp
1063
1064@noindent
1065(Note --- this example reveals a bug: each occurrence of @code{(= n 0)}
1066should be shown indented with respect to the one before it, as
1067@code{fact1} does not call itself tail-recursively.)
1068
1069You can also give a breakpoint the @code{trace-subtree} behaviour, which
1070means to trace the breakpoint location itself plus any evaluations and
1071applications that occur below it in the call stack. In the following
1072example, this allows us to see the evaluated arguments that are being
1073compared by the @code{=} procedure:
1074
1075@lisp
1076(set-breakpoint! trace-subtree 4)
1077@print{}
1078Breakpoint 4: standard input:13:9: (= n 0)
8e733f10
NJ
1079 enabled? = #t
1080 behaviour = #<procedure trace-subtree ()>
9a69a50e
NJ
1081
1082(fact1 4)
1083@print{}
1084| (= n 0)
1085| [= 4 0]
1086| #f
1087| (= n 0)
1088| [= 3 0]
1089| #f
1090| (= n 0)
1091| [= 2 0]
1092| #f
1093| (= n 0)
1094| [= 1 0]
1095| #f
1096| (= n 0)
1097| [= 0 0]
1098| #t
1099@result{}
110024
1101@end lisp
1102
1103
1104@node Tracing Compared
1105@subsection Differences Between Old and New Tracing Mechanisms
1106
1107The newer tracing mechanism is more general and so more powerful than
1108the older one: it works for expressions as well as procedure
1109applications, and it implements the useful @code{trace-subtree}
1110behaviour as well as the more traditional @code{trace-here}.
1111
1112The older mechanism will probably become obsolete eventually, but it's
1113worth keeping it around for a while until we are sure that the new
1114mechanism is correct and does what programmers need.