(new-input-prompt): Renamed from "prompt".
[bpt/guile.git] / doc / ref / scheme-using.texi
CommitLineData
46f7666d
NJ
1@c -*-texinfo-*-
2@c This is part of the GNU Guile Reference Manual.
3@c Copyright (C) 2006
4@c Free Software Foundation, Inc.
5@c See the file guile.texi for copying conditions.
6
7@node Using Guile Interactively
8@section Using Guile Interactively
9
10When you start up Guile by typing just @code{guile}, without a
11@code{-c} argument or the name of a script to execute, you get an
12interactive interpreter where you can enter Scheme expressions, and
13Guile will evaluate them and print the results for you. Here are some
14simple examples.
15
16@lisp
17guile> (+ 3 4 5)
1812
19guile> (display "Hello world!\n")
20Hello world!
21guile> (values 'a 'b)
22a
23b
24@end lisp
25
26@noindent
27This mode of use is called a @dfn{REPL}, which is short for
28``Read-Eval-Print Loop'', because the Guile interpreter first reads the
29expression that you have typed, then evaluates it, and then prints the
30result.
31
32@menu
33* Readline::
34* Value Historyx::
35* Error Handling::
36* Interactive Debugger:: Using the interactive debugger.
37@end menu
38
39
40@node Readline
41@subsection Readline
42
43To make it easier for you to repeat and vary previously entered
44expressions, or to edit the expression that you're typing in, Guile
45can use the GNU Readline library. This is not enabled by default
46because of licensing reasons, but all you need to activate Readline is
47the following pair of lines.
48
49@lisp
50guile> (use-modules (ice-9 readline))
51guile> (activate-readline)
52@end lisp
53
54It's a good idea to put these two lines (without the ``guile>''
55prompts) in your @file{.guile} file. Guile reads this file when it
56starts up interactively, so anything in this file has the same effect
57as if you type it in by hand at the ``guile>'' prompt.
58
59
60@node Value Historyx
61@subsection Value History
62
63Just as Readline helps you to reuse a previous input line, @dfn{value
64history} allows you to use the @emph{result} of a previous evaluation
65in a new expression. When value history is enabled, each evaluation
66result is automatically assigned to the next in the sequence of
67variables @code{$1}, @code{$2}, @dots{}, and you can then use these
68variables in subsequent expressions.
69
70@lisp
71guile> (iota 10)
72$1 = (0 1 2 3 4 5 6 7 8 9)
73guile> (apply * (cdr $1))
74$2 = 362880
75guile> (sqrt $2)
76$3 = 602.3952191045344
77guile> (cons $2 $1)
78$4 = (362880 0 1 2 3 4 5 6 7 8 9)
79@end lisp
80
81To enable value history, type @code{(use-modules (ice-9 history))} at
82the Guile prompt, or add this to your @file{.guile} file. (It is not
83enabled by default, to avoid the possibility of conflicting with some
84other use you may have for the variables @code{$1}, @code{$2},
85@dots{}, and also because it prevents the stored evaluation results
86from being garbage collected, which some people may not want.)
87
88
89@node Error Handling
90@subsection Error Handling
91
92When code being evaluated from the REPL hits an error, Guile remembers
93the execution context where the error occurred and can give you three
94levels of information about what the error was and exactly where it
95occurred.
96
ee6be719 97By default, Guile displays only the first level, which is the most
46f7666d
NJ
98immediate information about where and why the error occurred, for
99example:
100
101@lisp
102(make-string (* 4 (+ 3 #\s)) #\space)
103@print{}
104standard input:2:19: In procedure + in expression (+ 3 #\s):
105standard input:2:19: Wrong type argument: #\s
106ABORT: (wrong-type-arg)
107
ee6be719
NJ
108Type "(backtrace)" to get more information
109or "(debug)" to enter the debugger.
46f7666d
NJ
110@end lisp
111
112@noindent
113However, as the message above says, you can obtain more information
114about the context of the error by typing @code{(backtrace)} or
115@code{(debug)}.
116
117@code{(backtrace)} displays the Scheme call stack at the point where the
118error occurred:
119
120@lisp
121(backtrace)
122@print{}
123Backtrace:
124In standard input:
125 2: 0* [make-string ...
126 2: 1* [* 4 ...
127 2: 2* [+ 3 #\s]
128
129Type "(debug-enable 'backtrace)" if you would like a backtrace
130automatically if an error occurs in the future.
131@end lisp
132
133@noindent
134In a more complex scenario than this one, this can be extremely useful
135for understanding where and why the error occurred. You can make Guile
136show the backtrace automatically by adding @code{(debug-enable
137'backtrace)} to your @file{.guile}.
138
139@code{(debug)} takes you into Guile's interactive debugger, which
140provides commands that allow you to
141
142@itemize @bullet
143@item
144display the Scheme call stack at the point where the error occurred
145(the @code{backtrace} command --- see @ref{Display Backtrace})
146
147@item
148move up and down the call stack, to see in detail the expression being
149evaluated, or the procedure being applied, in each @dfn{frame} (the
150@code{up}, @code{down}, @code{frame}, @code{position}, @code{info args}
151and @code{info frame} commands --- see @ref{Frame Selection} and
152@ref{Frame Information})
153
154@item
155examine the values of variables and expressions in the context of each
156frame (the @code{evaluate} command --- see @ref{Frame Evaluation}).
157@end itemize
158
159@noindent
ee6be719 160The interactive debugger is documented further in the following section.
46f7666d
NJ
161
162
163@node Interactive Debugger
164@subsection Using the Interactive Debugger
165
166Guile's interactive debugger is a command line application that accepts
167commands from you for examining the stack and, if at a breakpoint, for
168continuing program execution in various ways. Unlike in the normal
169Guile REPL, commands are typed mostly without parentheses.
170
171When you first enter the debugger, it introduces itself with a message
172like this:
173
174@lisp
175This is the Guile debugger -- for help, type `help'.
176There are 3 frames on the stack.
177
178Frame 2 at standard input:36:19
179 [+ 3 #\s]
180debug>
181@end lisp
182
183@noindent
184``debug>'' is the debugger's prompt, and a reminder that you are not in
ee6be719
NJ
185the normal Guile REPL. In case you find yourself in the debugger by
186mistake, the @code{quit} command will return you to the REPL.
187
188@deffn {Debugger Command} quit
189Exit the debugger.
190@end deffn
191
192The other available commands are described in the following subsections.
46f7666d
NJ
193
194@menu
195* Display Backtrace:: backtrace.
196* Frame Selection:: up, down, frame.
197* Frame Information:: info args, info frame, position.
198* Frame Evaluation:: evaluate.
ee6be719 199* Stepping and Continuing:: step, next, (trace-)finish, continue.
46f7666d
NJ
200@end menu
201
202
203@node Display Backtrace
204@subsubsection Display Backtrace
205
206The @code{backtrace} command, which can also be invoked as @code{bt} or
207@code{where}, displays the call stack (aka backtrace) at the point where
208the debugger was entered:
209
210@lisp
211debug> bt
212In standard input:
213 36: 0* [make-string ...
214 36: 1* [* 4 ...
215 36: 2* [+ 3 #\s]
216@end lisp
217
218@deffn {Debugger Command} backtrace [count]
219@deffnx {Debugger Command} bt [count]
220@deffnx {Debugger Command} where [count]
221Print backtrace of all stack frames, or of the innermost @var{count}
222frames. With a negative argument, print the outermost -@var{count}
223frames. If the number of frames isn't explicitly given, the debug
224option @code{depth} determines the maximum number of frames printed.
225@end deffn
226
227The format of the displayed backtrace is the same as for the
ee6be719 228@code{display-backtrace} procedure (@pxref{Examining the Stack}).
46f7666d
NJ
229
230
231@node Frame Selection
232@subsubsection Frame Selection
233
234A call stack consists of a sequence of stack @dfn{frames}, with each
235frame describing one level of the nested evaluations and applications
236that the program was executing when it hit a breakpoint or an error.
237Frames are numbered such that frame 0 is the outermost --- i.e. the
238operation on the call stack that began least recently --- and frame N-1
239the innermost (where N is the total number of frames on the stack).
240
241When you enter the debugger, the innermost frame is selected, which
242means that the commands for getting information about the ``current''
243frame, or for evaluating expressions in the context of the current
244frame, will do so by default with respect to the innermost frame. To
245select a different frame, so that these operations will apply to it
246instead, use the @code{up}, @code{down} and @code{frame} commands like
247this:
248
249@lisp
250debug> up
251Frame 1 at standard input:36:14
252 [* 4 ...
253debug> frame 0
254Frame 0 at standard input:36:1
255 [make-string ...
256debug> down
257Frame 1 at standard input:36:14
258 [* 4 ...
259@end lisp
260
261@deffn {Debugger Command} up [n]
262Move @var{n} frames up the stack. For positive @var{n}, this
ee6be719 263advances toward the outermost frame, to lower frame numbers, to
46f7666d
NJ
264frames that have existed longer. @var{n} defaults to one.
265@end deffn
266
267@deffn {Debugger Command} down [n]
268Move @var{n} frames down the stack. For positive @var{n}, this
ee6be719 269advances toward the innermost frame, to higher frame numbers, to frames
46f7666d
NJ
270that were created more recently. @var{n} defaults to one.
271@end deffn
272
273@deffn {Debugger Command} frame [n]
274Select and print a stack frame. With no argument, print the selected
275stack frame. (See also ``info frame''.) An argument specifies the
276frame to select; it must be a stack-frame number.
277@end deffn
278
279
280@node Frame Information
281@subsubsection Frame Information
282
ee6be719
NJ
283The following commands return detailed information about the currently
284selected frame.
46f7666d
NJ
285
286@deffn {Debugger Command} {info frame}
ee6be719
NJ
287Display a verbose description of the selected frame. The information
288that this command provides is equivalent to what can be deduced from the
289one line summary for the frame that appears in a backtrace, but is
290presented and explained more clearly.
46f7666d
NJ
291@end deffn
292
293@deffn {Debugger Command} {info args}
ee6be719
NJ
294Display the argument variables of the current stack frame. Arguments
295can also be seen in the backtrace, but are presented more clearly by
296this command.
46f7666d
NJ
297@end deffn
298
299@deffn {Debugger Command} position
ee6be719
NJ
300Display the name of the source file that the current expression comes
301from, and the line and column number of the expression's opening
302parenthesis within that file. This information is only available when
303the @code{positions} read option is enabled (@pxref{Reader options}).
46f7666d
NJ
304@end deffn
305
306
307@node Frame Evaluation
308@subsubsection Frame Evaluation
309
ee6be719
NJ
310The @code{evaluate} command is most useful for querying the value of a
311variable, either global or local, in the environment of the selected
312stack frame, but it can be used more generally to evaluate any
313expression.
46f7666d
NJ
314
315@deffn {Debugger Command} evaluate expression
ee6be719
NJ
316Evaluate an expression in the environment of the selected stack frame.
317The expression must appear on the same line as the command, however it
318may be continued over multiple lines.
46f7666d
NJ
319@end deffn
320
321
ee6be719
NJ
322@node Stepping and Continuing
323@subsubsection Single Stepping and Continuing Execution
46f7666d 324
ee6be719
NJ
325The commands in this subsection all apply only when the stack is
326@dfn{continuable} --- in other words when it makes sense for the program
327that the stack comes from to continue running. Usually this means that
328the program stopped because of a trap or a breakpoint.
46f7666d
NJ
329
330@deffn {Debugger Command} step [n]
ee6be719
NJ
331Tell the debugged program to do @var{n} more steps from its current
332position. One @dfn{step} means executing until the next frame entry or
333exit of any kind. @var{n} defaults to 1.
46f7666d
NJ
334@end deffn
335
336@deffn {Debugger Command} next [n]
ee6be719
NJ
337Tell the debugged program to do @var{n} more steps from its current
338position, but only counting frame entries and exits where the
339corresponding source code comes from the same file as the current stack
340frame. (See @ref{Step Traps} for the details of how this works.) If
341the current stack frame has no source code, the effect of this command
342is the same as of @code{step}. @var{n} defaults to 1.
46f7666d
NJ
343@end deffn
344
46f7666d 345@deffn {Debugger Command} finish
ee6be719
NJ
346Tell the program being debugged to continue running until the completion
347of the current stack frame, and at that time to print the result and
348reenter the command line debugger.
46f7666d
NJ
349@end deffn
350
46f7666d 351@deffn {Debugger Command} continue
ee6be719
NJ
352Tell the program being debugged to continue running. (In fact this is
353the same as the @code{quit} command, because it exits the debugger
354command loop and so allows whatever code it was that invoked the
355debugger to continue.)
46f7666d
NJ
356@end deffn
357
358
359@node Using Guile in Emacs
360@section Using Guile in Emacs
361
01d2ee15
NJ
362There are quite a few options for working on Guile Scheme code in
363Emacs. The simplest options are to use Emacs's standard
364@code{scheme-mode} for editing code, and to run the interpreter when you
365need it by typing ``guile'' at the prompt of a @code{*shell*} buffer,
366but there are Emacs libraries available which add various bells and
367whistles to this. The following diagram shows these libraries and how
368they relate to each other, with the arrows indicating ``builds on'' or
369``extends''. For example, the Quack library builds on cmuscheme, which
370in turn builds on the standard scheme mode.
371
372@example
373 scheme
374 ^
375 |
376 .-----+-----.
377 | |
378 cmuscheme xscheme
379 ^
380 |
381 .-----+-----.
382 | |
383 Quack GDS
384@end example
385
386@dfn{scheme}, written by Bill Rozas and Dave Love, is Emacs's standard
387mode for Scheme code files. It provides Scheme-sensitive syntax
388highlighting, parenthesis matching, indentation and so on.
389
390@dfn{cmuscheme}, written by Olin Shivers, provides a comint-based Scheme
391interaction buffer, so that you can run an interpreter more directly
94a2c24a 392than with the @code{*shell*} buffer approach by typing @kbd{M-x
01d2ee15
NJ
393run-scheme}. It also extends @code{scheme-mode} so that there are key
394presses for sending selected bits of code from a Scheme buffer to this
395interpreter. This means that when you are writing some code and want to
396check what an expression evaluates to, you can easily select that code
397and send it to the interpreter for evaluation, then switch to the
398interpreter to see what the result is. cmuscheme is included in the
399standard Emacs distribution.
400
401@dfn{Quack}, written by Neil Van Dyke, adds a number of incremental
402improvements to the scheme/cmuscheme combination: convenient menu
403entries for looking up Scheme-related references (such as the SRFIs);
404enhanced indentation rules that are customized for particular Scheme
405interpreters, including Guile; an enhanced version of the
406@code{run-scheme} command that knows the names of the common Scheme
407interpreters and remembers which one you used last time; and so on.
408Quack is available from @uref{http://www.neilvandyke.org/quack}.
409
410@dfn{GDS}, written by Neil Jerram, also builds on the scheme/cmuscheme
411combination, but with a fundamental change to the way that Scheme code
412fragments are sent to the interpreter for evaluation. cmuscheme and
413Quack send code fragments to the interpreter's standard input, on the
414assumption that the interpreter is expecting to read Scheme expressions
415there, and then monitor the interpreter's standard output to infer what
416the result of the evaluation is. GDS doesn't use standard input and
417output like this. Instead, it sets up a socket connection between the
418Scheme interpreter and Emacs, and sends and receives messages using a
419simple protocol through this socket. The messages include requests to
420evaluate Scheme code, and responses conveying the results of an
94a2c24a 421evaluation, thus providing similar function to cmuscheme or Quack. They
01d2ee15
NJ
422also include requests for setting breakpoints, stack exploration and
423debugging, which go beyond what cmuscheme or Quack can do. The price of
424this extra power, however, is that GDS is Guile-specific. GDS requires
425the Scheme interpreter, or any program that GDS is debugging, to run
426some GDS-specific library code; currently this code is written as a
427Guile module and uses a lot of debugging-related features that are
428specific to Guile. GDS is now included in the Guile distribution; for
429previous Guile releases (1.8.x and earlier) it can be obtained as part
430of the @code{guile-debugging} package from
431@uref{http://www.ossau.uklinux.net/guile}.
432
94a2c24a
NJ
433Finally, @dfn{xscheme} is similar to cmuscheme --- in that it starts up
434a Scheme interaction process and sends commands to that process's
435standard input --- and to GDS --- in that it has support beyond
436cmuscheme or Quack for exploring the Scheme stack when an error has
437occurred --- but is implemented specifically for MIT/GNU Scheme. Hence
438it isn't really relevant to Guile work in Emacs, except as a reference
439for useful features that could be implemented in one of the other
440libraries mentioned here.
441
442In summary, the best current choice for working on Guile code in Emacs
443is either Quack or GDS, depending on which of these libraries' features
444you find most important. For more information on Quack, please see the
445website referenced above. GDS is documented further in the rest of this
446section.
01d2ee15
NJ
447
448@menu
449* GDS Introduction::
94a2c24a 450* GDS Architecture::
01d2ee15 451* GDS Getting Started::
01d2ee15
NJ
452* Displaying the Scheme Stack::
453* Continuing Execution::
454* Evaluating Scheme Code::
455* Setting and Managing Breakpoints::
456* Access to Guile Help and Completion::
457* Associating Buffers with Clients::
458* An Example GDS Session::
01d2ee15
NJ
459@end menu
460
461
462@node GDS Introduction
463@subsection GDS Introduction
464
465GDS aims to allow you to work on Guile Scheme code in the same kind of
466way that Emacs allows you to work on Emacs Lisp code: providing easy
467access to help, evaluating arbitrary fragments of code, a nice debugging
94a2c24a 468interface, and so on. The thinking behind the GDS library is that you
01d2ee15 469will usually be doing one of two things.
46f7666d
NJ
470
471@enumerate
472@item
01d2ee15
NJ
473Writing or editing code. The code will be in a normal Emacs Scheme mode
474buffer, and GDS extends Scheme mode to add keystrokes and menu items for
475the things that are likely to be useful to you when working on code:
46f7666d
NJ
476
477@itemize
478@item
94a2c24a
NJ
479completing the identifier at point, with respect to the set of variable
480names that are known to the associated Guile process
46f7666d 481@item
94a2c24a 482accessing Guile's built in ``help'' and ``apropos'' commands
46f7666d 483@item
94a2c24a
NJ
484evaluating fragments of code to check what they do, with the results
485popping up in a temporary Emacs window.
46f7666d
NJ
486@end itemize
487
488@item
01d2ee15 489Debugging a Guile Scheme program. When your program hits an error or a
94a2c24a
NJ
490breakpoint, GDS shows you the relevant code and the Scheme stack, and
491makes it easy to
46f7666d
NJ
492
493@itemize
494@item
495look at the values of local variables
496@item
497see what is happening at all levels of the Scheme stack
498@item
94a2c24a
NJ
499set new breakpoints (by simply typing @kbd{C-x @key{SPC}}) or modify
500existing ones
501@item
46f7666d
NJ
502continue execution, either normally or step by step.
503@end itemize
46f7666d 504
94a2c24a
NJ
505The presentation makes it very easy to move up and down the stack,
506showing whenever possible the source code for each frame in another
507Emacs buffer. It also provides convenient keystrokes for telling Guile
508what to do next; for example, you can select a stack frame and tell
509Guile to run until that frame completes, at which point GDS will display
510the frame's return value.
4f6e3015 511@end enumerate
94a2c24a 512
4f6e3015
NJ
513Combinations of these well too. You can evaluate a fragment of code (in
514a Scheme buffer) that contains a breakpoint, then use the debugging
515interface to step through the code at the breakpoint. You can also run
516a program until it hits a breakpoint, then examine, modify and
94a2c24a
NJ
517reevaluate some of the relevant code, and then tell the program to
518continue running.
46f7666d 519
94a2c24a
NJ
520GDS can provide these facilities for any number of Guile Scheme programs
521(which we often refer to as ``clients'') at once, and these programs can
522be started either independently of GDS, including outside Emacs, or
523specifically @emph{by} GDS.
62ae9557 524
94a2c24a
NJ
525Communication between each Guile client program and GDS uses a TCP
526socket, which means that it is orthogonal to any other interfaces that
527the client program has. In particular GDS does not interfere with a
528program's standard input and output.
62ae9557 529
94a2c24a
NJ
530
531@node GDS Architecture
532@subsection GDS Architecture
533
534In order to understand the following documentation fully it will help to
535have a picture in mind of how GDS works, so we briefly describe that
536here. GDS consists of three components.
46f7666d 537
62ae9557
NJ
538@itemize
539@item
94a2c24a
NJ
540The GDS @dfn{interface} code is written in Emacs Lisp and runs inside
541Emacs. This code, consisting of the installed files @file{gds.el} and
542@file{gds-server.el}, is responsible for displaying information from
543Guile in Emacs windows, and for responding to Emacs commands and
544keystrokes by sending instructions back to the Guile program being
545debugged.
546
547@item
548The GDS @dfn{server} code is written in Scheme and runs as an Emacs
549inferior process. It acts as a multiplexer between the (possibly
550multiple) Guile programs being debugged and the interface code running
551in Emacs. The server code is the installed file
552@file{gds-server.scm}.
62ae9557
NJ
553
554@item
94a2c24a
NJ
555The GDS @dfn{client} code is written in Scheme (installed file
556@file{gds-client.scm}), and must be loaded as a module by each Guile
557program that wants to use GDS in any way.
62ae9557
NJ
558@end itemize
559
560@noindent
94a2c24a
NJ
561The following diagram shows how these components are connected to each
562other.
62ae9557 563
94a2c24a
NJ
564@example
565+----------------+
566| Program #1 |
567| |
568| +------------+ |
569| | GDS Client |-_
570| +------------+ |-_ +-------------------+
571+----------------+ -_TCP | Emacs |
572 -_ | |
573 -_+------------+ | +---------------+ |
574 _| GDS Server |-----| GDS Interface | |
575+----------------+ _- +------------+ | +---------------+ |
576| Program #2 | _- +-------------------+
577| | _- TCP
578| +------------+ _-
579| | GDS Client |-|
580| +------------+ |
581+----------------+
582@end example
62ae9557 583
94a2c24a
NJ
584@cindex TCP, use of
585The data exchanged between client and server components, and between
586server and interface, is a sequence of sexps (parenthesised expressions)
587that are designed so as to be directly readable by both Scheme and Emacs
588Lisp. The use of a TCP connection means that the server and Emacs
589interface can theoretically be on a different computer from the programs
590being debugged, but in practice there are currently two problems with
591this. Firstly the GDS API doesn't provide any way of specifying a
592non-local server to connect to, and secondly there is no security or
593authentication mechanism in the GDS protocol. These are issues that
594should be addressed in the near future.
62ae9557 595
94a2c24a 596
01d2ee15
NJ
597@node GDS Getting Started
598@subsection Getting Started with GDS
62ae9557 599
94a2c24a 600To enable the use of GDS in your own Emacs sessions, simply add
62ae9557
NJ
601
602@lisp
603(require 'gds)
604@end lisp
605
606@noindent
94a2c24a
NJ
607somewhere in your @file{.emacs} file. This will cause Emacs to load the
608GDS Emacs Lisp code when starting up, and to start the inferior GDS
609server process so that it is ready and waiting for any Guile programs
610that want to use GDS.
62ae9557 611
94a2c24a
NJ
612(If GDS's Scheme code is not installed in one of the locations in
613Guile's load path, you may find that the server process fails to start.
614When this happens you will see an error message from Emacs:
62ae9557 615
94a2c24a
NJ
616@lisp
617error in process filter: Wrong type argument: listp, Backtrace:
618@end lisp
62ae9557 619
94a2c24a
NJ
620@noindent
621and the @code{gds-debug} buffer will contain a Scheme backtrace ending
622with the message:
62ae9557 623
94a2c24a
NJ
624@lisp
625no code for module (ossau gds-server)
626@end lisp
62ae9557 627
94a2c24a
NJ
628@noindent
629The solution for this is to customize the Emacs variable
630@code{gds-scheme-directory} so that it specifies where the GDS Scheme
631code is installed. Then either restart Emacs or type @kbd{M-x
632gds-run-debug-server} to try starting the GDS server process again.)
633
634For evaluations, help and completion from Scheme code buffers that you
635are working on, this is all you need. The first time you do any of
636these things, GDS will automatically start a new Guile client program as
637an Emacs subprocess. This Guile program does nothing but wait for and
638act on instructions from GDS, and we refer to it as a @dfn{utility}
639Guile client. Over time this utility client will accumulate the code
640that you ask it to evaluate, and you can also tell it to load complete
641files or modules by sending it @code{load} or @code{use-modules}
642expressions. You can set breakpoints and evaluate code which hits those
643breakpoints, and GDS will pop up the stack at the breakpoint so you can
644explore your code by single-stepping and evaluating test expressions.
94a2c24a
NJ
645For a hands-on, tutorial introduction to using GDS in this way, use
646Emacs to open the file @file{gds-tutorial.txt} (which should have been
647installed as part of Guile, perhaps under @file{/usr/share/doc/guile}),
648and then follow the steps in that file.
649
650When you want to use GDS to explore or debug an independent Guile
651application, you need to add something to that application's Scheme code
652to cause it to connect to and interact with GDS at the right times. The
653following subsections describe the various ways of doing this.
62ae9557 654
94a2c24a 655@subsubsection Setting Specific Breakpoints
62ae9557 656
1e1387ca
NJ
657The first option is to use @code{break-in} or @code{break-at} to set
658specific breakpoints in the application's code. This requires code like
659the following.
660
62ae9557 661@lisp
94a2c24a
NJ
662(use-modules (ice-9 debugging breakpoints)
663 (ice-9 gds-client))
62ae9557 664
94a2c24a
NJ
665(break-in 'fact2 "ice-9/debugging/example-fns"
666 #:behaviour gds-debug-trap)
1e1387ca
NJ
667(break-in 'facti "ice-9/debugging/example-fns"
668 #:behaviour gds-debug-trap)
62ae9557
NJ
669@end lisp
670
1e1387ca
NJ
671@noindent
672The @code{#:behaviour gds-debug-trap} clauses mean to use GDS to display
673the stack when one of these breakpoints is hit. For more on
674breakpoints, @code{break-in} and @code{break-at}, see @ref{Intro to
675Breakpoints}.
62ae9557
NJ
676
677@subsubsection Setting GDS-managed Breakpoints
678
1e1387ca
NJ
679Instead of listing specific breakpoints in application code, you can use
680GDS to manage the set of breakpoints that you want from Emacs, and tell
681the application to download the breakpoints that it should set from
682GDS. The code for this is:
683
62ae9557
NJ
684@lisp
685(use-modules (ice-9 gds-client))
686(set-gds-breakpoints)
687@end lisp
688
689These lines tell the program to connect to GDS immediately and download
690a set of breakpoint definitions. The program sets those breakpoints in
691its code, then continues running.
692
693When the program later hits one of the breakpoints, it will use GDS to
1e1387ca 694display the stack and wait for instruction on what to do next.
62ae9557 695
94a2c24a 696@subsubsection Invoking GDS when an Exception Occurs
62ae9557 697
1e1387ca
NJ
698Another option is to use GDS to catch and display any exceptions that
699are thrown by the application's code. If you already have a
700@code{lazy-catch} or @code{with-throw-handler} around the area of code
701that you want to monitor, you just need to add the following to the
702handler code:
703
704@lisp
705(gds-debug-trap (throw->trap-context key args))
706@end lisp
707
708@noindent
709where @code{key} and @code{args} are the first and rest arguments that
710Guile passes to the handler. (In other words, they assume the handler
711signature @code{(lambda (key . args) @dots{})}.) With Guile 1.8 or
712later, you can also do this with a @code{catch}, by adding this same
713code to the catch's optional pre-unwind handler.
714
715If you don't already have any of these, insert a whole
716@code{with-throw-handler} expression around the code of interest like
717this:
718
719@lisp
720(with-throw-handler #t
721 (lambda ()
722 ;; Protected code here.
723 )
724 (lambda (key . args)
725 (gds-debug-trap (throw->trap-context key args))))
726@end lisp
727
728In all cases you will need to use the @code{(ice-9 gds-client)} and
729@code{(ice-9 debugging traps)} modules.
730
731Two special cases of this are the lazy-catch that the Guile REPL code
732uses to catch exceptions in user code, and the lazy-catch inside the
733@code{stack-catch} utility procedure that is provided by the
734@code{(ice-9 stack-catch)} module. Both of these use a handler called
735@code{lazy-handler-dispatch}, which you can modify automatically so that
736it calls GDS to display the stack when an exception occurs. To do this,
737use the @code{on-lazy-handler-dispatch} procedure as follows.
738
62ae9557 739@lisp
94a2c24a
NJ
740(use-modules (ice-9 gds-client)
741 (ice-9 debugging traps))
62ae9557 742
94a2c24a 743(on-lazy-handler-dispatch gds-debug-trap)
62ae9557
NJ
744@end lisp
745
1e1387ca
NJ
746@noindent
747After this the program will use GDS to display the stack whenever it
748hits an exception that is protected by a @code{lazy-catch} using
749@code{lazy-catch-handler} (defined in @file{boot-9.scm}).
62ae9557
NJ
750
751@subsubsection Accepting GDS Instructions at Any Time
752
753In addition to setting breakpoints and/or an exception handler as
754described above, a Guile program can in principle set itself up to
755accept new instructions from GDS at any time, not just when it has
756stopped at a breakpoint or exception. This would allow the GDS user to
757set new breakpoints or to evaluate code in the context of the running
758program, without having to wait for the program to stop first.
759
760@lisp
761(use-modules (ice-9 gds-client))
762(gds-accept-input #t)
763@end lisp
764
765@code{gds-accept-input} causes the calling program to loop processing
766instructions from GDS, until GDS sends the @code{continue} instruction.
767This blocks the thread that calls it, however, so it will normally be
768more practical for the program to set up a dedicated GDS thread and call
769@code{gds-accept-input} from that thread.
770
771For @code{select}-driven applications, an alternative approach would be
772for the GDS client code to provide an API which allowed the application
773to
774
775@itemize
776@item
777discover the file descriptors (or Scheme ports) that are used for
778receiving instruction from the GDS front end, so that it could include
779these in its @code{select} call
780
781@item
782call the GDS instruction handler when @code{select} indicated data
783available for reading on those descriptors/ports.
784@end itemize
785
786@noindent
787This approach is not yet implemented, though.
788
789@subsubsection Utility Guile Implementation
790
1e1387ca
NJ
791We conclude this subsection with an aside, by noting that the
792``utility'' Guile client described above is nothing more than a
793combination of the previous options.
62ae9557 794
1e1387ca 795To be precise, the code for the utility Guile client is essentially just
62ae9557
NJ
796this:
797
798@lisp
799(use-modules (ice-9 gds-client))
800
801(set-gds-breakpoints)
802(named-module-use! '(guile-user) '(ice-9 session))
803(gds-accept-input #f))
804@end lisp
805
806@code{set-gds-breakpoints} works as already described. The
807@code{named-module-use!} line ensures that the client can process
1e1387ca
NJ
808@code{help} and @code{apropos} expressions, to implement lookups in
809Guile's online help. The @code{#f} parameter to @code{gds-accept-input}
810means that the @code{continue} instruction will not cause the
811instruction loop to exit, which makes sense here because the utility
812client has nothing to do except to process GDS instructions.
62ae9557
NJ
813
814(The utility client does not use @code{on-lazy-handler-dispatch},
815because it has its own mechanism for catching and reporting exceptions
816in the code that it is asked to evaluate. This mechanism summarizes the
817exception and gives the user a button they can click to see the full
818stack, so the end result is very similar to what
819@code{on-lazy-handler-dispatch} provides.)
820
821
822@node Displaying the Scheme Stack
823@subsection Displaying the Scheme Stack
824
825When you specify @code{gds-debug-trap} as the behaviour for a trap or
826a breakpoint and the Guile program concerned hits that trap or
827breakpoint, GDS displays the stack and the relevant Scheme source code
828in Emacs, allowing you to explore the state of the program and then
829decide what to do next. The same applies if the program calls
830@code{(on-lazy-handler-dispatch gds-debug-trap)} and then throws an
831exception that passes through @code{lazy-handler-dispatch}, except
832that in this case you can only explore; it isn't possible to continue
833normal execution after an exception.
834
835The following commands are available in the stack buffer for exploring
836the state of the program.
837
838@table @asis
839@item @kbd{u}, @kbd{C-p}, @kbd{@key{up}}
840@findex gds-up
841Select the stack frame one up from the currently selected frame
842(@code{gds-up}). GDS displays stack frames with the innermost at the
843top, so moving ``up'' means selecting a more ``inner'' frame.
844
845@item @kbd{d}, @kbd{C-n}, @kbd{@key{down}}
846@findex gds-down
847Select the stack frame one down from the currently selected frame
848(@code{gds-down}). GDS displays stack frames with the innermost at the
849top, so moving ``down'' means selecting a more ``outer'' frame.
850
851@item @kbd{@key{RET}}
852@findex gds-select-stack-frame
853Select the stack frame at point (@code{gds-select-stack-frame}). This
854is useful after clicking somewhere in the stack trace with the mouse.
855@end table
856
857Selecting a frame means that GDS will display the source code
858corresponding to that frame in the adjacent window, and that
859subsequent frame-sensitive commands, such as @code{gds-evaluate} (see
860below) and @code{gds-step-over} (@pxref{Continuing Execution}), will
861refer to that frame.
862
863@table @kbd
864@item e
865@findex gds-evaluate
866Evaluate a variable or expression in the local environment of the
867selected stack frame (@code{gds-evaluate}). The result is displayed in
868the echo area.
869
870@item I
871@findex gds-frame-info
872Show summary information about the selected stack frame
873(@code{gds-frame-info}). This includes what type of frame it is, the
874associated expression, and the frame's source location, if any.
875
876@item A
877@findex gds-frame-args
878For an application frame, display the frame's arguments
879(@code{gds-frame-args}).
880
881@item S
882@findex gds-proc-source
883For an application frame, show the Scheme source code of the procedure
884being called (@code{gds-proc-source}). The source code (where
885available) is displayed in the echo area.
886@end table
887
888@kbd{S} (@code{gds-proc-source}) is useful when the procedure being
889called was created by an anonymous @code{(lambda @dots{})} expression.
890Such procedures appear in the stack trace as @code{<procedure #f
891(@dots{})>}, which doesn't give you much clue as to what will happen
892next. @kbd{S} will show you the procedure's code, which is usually
893enough for you to identify it.
894
895
896@node Continuing Execution
897@subsection Continuing Execution
898
899If it makes sense to continue execution from the stack which is being
900displayed, GDS provides the following further commands in the stack
901buffer.
902
903@table @asis
904@item @kbd{g}, @kbd{c}, @kbd{q}
905@findex gds-go
906Tell the program to continue running (@code{gds-go}). It may of course
907stop again if it hits another trap, or another occurrence of the same
908trap.
909
910The multiple keystrokes reflect that you can think of this as ``going'',
911``continuing'' or ``quitting'' (in the sense of quitting the GDS
912display).
913
914@item @kbd{@key{SPC}}
915@findex gds-step-file
916Tell the program to do a single-step to the next entry or exit of a
917frame whose code comes from the same source file as the selected stack
918frame (@code{gds-step-file}).
919
920In other words, you can hit @kbd{@key{SPC}} repeatedly to step through
921the code in a given file, automatically stepping @emph{over} any
922evaluations or procedure calls that use code from other files (or from
923no file).
924
925If the selected stack frame has no source, the effect of this command is
926the same as that of @kbd{i}, described next.
927
928@item @kbd{i}
929@findex gds-step-into
930Tell the debugged program to do a single-step to the next frame entry or
931exit of any kind (@code{gds-step-into}). @kbd{i} therefore steps
932through code at the most detailed level possible.
933
934@item @kbd{o}
935@findex gds-step-over
936Tell the debugged program to continue running until the selected stack
937frame completes, and then to display its result (@code{gds-step-over}).
938Note that the program may stop before then if it hits another trap; in
939this case the trap telling it to stop when the marked frame completes
940remains in place and so will still fire at the appropriate point.
941@end table
942
943
944@node Evaluating Scheme Code
945@subsection Evaluating Scheme Code
946
947The following keystrokes and commands provide various ways of sending
948code to a Guile client process for evaluation.
949
950@table @kbd
951@item M-C-x
952@findex gds-eval-defun
953Evaluate the ``top level defun'' that the cursor is in, in other words
954the smallest balanced expression which includes the cursor and whose
955opening parenthesis is in column 0 (@code{gds-eval-defun}).
956
957@item C-x C-e
958@findex gds-eval-last-sexp
959Evaluate the expression that ends just before the cursor
960(@code{gds-eval-last-sexp}). This is designed so that it is easy to
961evaluate an expression that you have just finished typing.
962
963@item C-c C-e
964@findex gds-eval-expression
965Read a Scheme expression using the minibuffer, and evaluate that
966expression (@code{gds-eval-expression}).
967
968@item C-c C-r
969@findex gds-eval-region
970Evaluate the Scheme code in the marked region of the current buffer
971(@code{gds-eval-region}). Note that GDS does not check whether the
972region contains a balanced expression, or try to expand the region so
973that it does; it uses the region exactly as it is.
974@end table
975
976
977@node Setting and Managing Breakpoints
978@subsection Setting and Managing Breakpoints
979
980You can create a breakpoint in GDS by typing @kbd{C-x @key{SPC}} in a
981Scheme mode buffer. To create a breakpoint on calls to a procedure
982--- i.e. the equivalent of calling @code{break-in} --- place the
983cursor on the procedure's name and type @kbd{C-x @key{SPC}}. To
984create breakpoints on a particular expression, or on the series of
985expressions in a particular region --- i.e. as with @code{break-at}
986--- select the expression or region in the usual way and type @kbd{C-x
987@key{SPC}}. In general, GDS assumes that you want a @code{break-at}
988breakpoint if there is an active region, and a @code{break-in}
989breakpoint otherwise.
990
991When you create a breakpoint like this, two things happen. Firstly,
992if the current buffer is associated with a Guile client program, the
993new breakpoint definition is immediately sent to that client (or, if
994the client cannot accept input immediately, it is held in readiness to
995pass to the client at the next possible opportunity). This allows the
996new breakpoint to take effect as soon as possible in the relevant
997client program.
998
999Secondly, it is added to GDS's @emph{global} list of all breakpoints.
1000This list holds the breakpoint information that will be given to any
1001client program that asks for it by calling @code{set-gds-breakpoints}.
1002The fact that this list is global, rather than client-specific, means
1003that the breakpoints you have set will automatically be recreated if
1004the program you are debugging has to be stopped and restarted ---
1005which in my experience happens often.@footnote{An important point here
1006is that there is nothing that unambiguously relates two subsequent
1007runs of the same client program, which might allow GDS to pass on
1008breakpoint settings more precisely.}
1009
1010(The only possible downside of this last point is that if you are
1011debugging two programs in parallel, which have some code in common,
1012you might not want a common code breakpoint in one program to be set
1013in the other program as well. But this feels like a small concern in
1014comparison to the benefit of breakpoints persisting as just described.)
1015
1016
1017@node Access to Guile Help and Completion
1018@subsection Access to Guile Help and Completion
1019
1020The following keystrokes provide fast and convenient access to Guile's
1021built in help, and to completion with respect to the set of defined and
1022accessible symbols.
1023
1024@table @kbd
1025@item C-h g
1026@findex gds-help-symbol
1027Get Guile help for a particular symbol, with the same results as if
1028you had typed @code{(help SYMBOL)} into the Guile REPL
1029(@code{gds-help-symbol}). The symbol to query defaults to the word at
1030or before the cursor but can also be entered or edited in the
1031minibuffer. The available help is popped up in a temporary Emacs
1032window.
1033
1034@item C-h C-g
1035@findex gds-apropos
1036List all accessible Guile symbols matching a given regular expression,
1037with the same results as if you had typed @code{(apropos REGEXP)} into
1038the Guile REPL (@code{gds-apropos}). The regexp to query defaults to
1039the word at or before the cursor but can also be entered or edited in
1040the minibuffer. The list of matching symbols is popped up in a
1041temporary Emacs window.
1042
1043@item M-@key{TAB}
1044@findex gds-complete-symbol
1045Try to complete the symbol at the cursor by matching it against the
1046set of all defined and accessible bindings in the associated Guile
1047process (@code{gds-complete-symbol}). If there are any extra
1048characters that can be definitively added to the symbol at point, they
1049are inserted. Otherwise, if there are any completions available, they
1050are popped up in a temporary Emacs window, where one of them can be
1051selected using either @kbd{@key{RET}} or the mouse.
1052@end table
1053
1054
1055@node Associating Buffers with Clients
1056@subsection Associating Buffers with Clients
1057
1058The first time that you use one of GDS's evaluation, help or completion
1059commands from a given Scheme mode buffer, GDS will ask which Guile
1060client program you want to use for the operation, or if you want to
1061start up a new ``utility'' client. After that GDS considers the buffer
1062to be ``associated'' with the selected client, and so sends all further
1063requests to that client, but you can override this by explicitly
1064associating the buffer with a different client, or by removing the
1065default association.
1066
1067@table @kbd
1068@item M-x gds-associate-buffer
1069Associate (or re-associate) the current buffer with a particular Guile
1070client program. The available clients are listed, and you can also
1071choose to start up a new ``utility'' client for this buffer to associate
1072with.
1073
1074@item M-x gds-dissociate-buffer
1075Dissociate the current buffer from its client, if any. This means that
1076the next time you use an evaluation, help or completion command, GDS
1077will ask you again which client to send the request to.
1078@end table
1079
1080When a buffer is associated with a client program, the buffer's modeline
1081shows whether the client is currently able to accept instruction from
1082GDS. This is done by adding one of the following suffixes to the
1083``Scheme'' major mode indicator:
1084
1085@table @asis
1086@item :ready
1087The client program (or one of its threads, if multithreaded) is
1088currently ready to accept instruction from GDS. In other words, if you
1089send it a help or evaluation request, you should see the result pretty
1090much immediately.
1091
1092@item :running
1093The client program is not currently able to accept instruction from
1094GDS. This means that it (or all of its threads, if multithreaded) is
1095busy, or waiting for input other than from GDS.
1096
1097@item :debug
1098The client program (or one of its threads, if multithreaded) is stopped
1099in ``debugging mode'' with GDS displaying the stack for a trap or
1100exception. It is waiting for instruction from GDS on what to do next.
1101@end table
1102
1103
1104@node An Example GDS Session
1105@subsection An Example GDS Session
1106
1107Create a file, @file{testgds.scm} say, for experimenting with GDS and
1108Scheme code, and type this into it:
1109
1110@lisp
1111(use-modules (ice-9 debugging traps)
1112 (ice-9 gds-client)
1113 (ice-9 debugging example-fns))
1114(install-trap (make <procedure-trap>
1115 #:behaviour gds-debug-trap
1116 #:procedure fact1))
1117@end lisp
1118
1119@noindent
1120Now select all of this code and type @kbd{C-c C-r} to send the selected
1121region to Guile for evaluation. GDS will ask you which Guile process to
1122use; unless you know that you already have another Guile application
1123running and connected to GDS, choose the ``Start a new Guile'' option,
94a2c24a
NJ
1124which starts one of the ``utility'' processes described in @ref{GDS
1125Getting Started}.
62ae9557
NJ
1126
1127The results of the evaluation pop up in a window like this:
1128
1129@lisp
1130(use-modules (ice-9 debugging traps)\n @dots{}
1131
1132;;; Evaluating subexpression 1 in current module (guile-user)
1133 @result{} no (or unspecified) value
1134
1135;;; Evaluating subexpression 2 in current module (guile-user)
1136 @result{} no (or unspecified) value
1137
1138--:** *Guile Evaluation* (Scheme:ready)--All------------
1139@end lisp
1140
1141@noindent
1142this tells you that the evaluation was successful but that the return
1143values were unspecified. Its effect was to load a module of example
1144functions and set a trap on one of these functions, @code{fact1}, that
1145calculates the factorial of its argument.
1146
1147If you now call @code{fact1}, you can see the trap and GDS's stack
1148display in action. To do this add
1149
1150@lisp
1151(fact1 4)
1152@end lisp
1153
1154@noindent
1155to your @file{testgds.scm} buffer and type @kbd{C-x C-e} (which
1156evaluates the expression that the cursor is just after the end of). The
1157result is:
1158
1159@lisp
1160(fact1 4)
1161
1162;;; Evaluating in current module (guile-user)
1163 @result{} 24
1164
1165--:** *Guile Evaluation* (Scheme:ready)--All------------
1166@end lisp
1167
1168@noindent
1169which is correct, but indicates that we forgot the step needed to enable
1170the trap mechanism. To do this, type @kbd{C-c C-e} and then enter
1171
1172@lisp
1173(trap-enable 'traps)
1174@end lisp
1175
1176@noindent
1177into the minibuffer. (You could equally have typed this into your test
1178file and evaluated it from there; we use @kbd{C-c C-e} here to
1179demonstrate the minibuffer option and because you typically wouldn't
1180want to leave this kind of global setting in the source code that you
1181are working on.)
1182
1183If you now type @kbd{C-x C-e} to evaluate @code{(fact1 4)} again, a GDS
1184stack window like the following appears:
1185
1186@lisp
1187Calling procedure:
1188=> s [fact1 4]
1189 s [primitive-eval (fact1 4)]
1190
1191
1192--:** PID 28729 (Guile-Debug)--All------------
1193@end lisp
1194
1195GDS's most compelling feature is its single-stepping. To get an
1196immediate feel for what this is like, make sure your Emacs is prepared
01d2ee15
NJ
1197as described in @ref{GDS Getting Started}, then type the following code
1198into an interactive Guile session.
62ae9557
NJ
1199
1200@lisp
1201(fact1 4)
1202@end lisp
1203
1204@noindent
1205This will cause the GDS Guile-Debug window to pop up in Emacs, where
1206you can then press @kbd{i} once and @kbd{@key{SPC}} repeatedly to
1207single-step through the code from the point of the initial trap.
1208
1209(@kbd{i} is needed as the first keystroke rather than @kbd{@key{SPC}},
1210because the aim here is to step through code in the @code{(ice-9
1211debugging example-fns)} module, whose source file is
1212@file{@dots{}/ice-9/debugging/example-fns.scm}, but the initial
1213@code{(fact1 4)} call comes from the Guile session, whose ``source
1214file'' Guile presents as @file{standard input}. If the user starts by
1215pressing @kbd{@key{SPC}} instead of @kbd{i}, the effect is that the
1216program runs until it hits the first recursive call @code{(fact1 (- n
12171))}, where it stops because of the trap on @code{fact1} firing again.
1218At this point, the source file @emph{is}
1219@file{@dots{}/ice-9/debugging/example-fns.scm}, because the recursive
1220@code{(fact1 (- n 1))} call comes from code in that file, so further
1221pressing of @kbd{@key{SPC}} successfully single-steps through this
1222file.)
1223
1224
46f7666d
NJ
1225@c Local Variables:
1226@c TeX-master: "guile.texi"
1227@c End: