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