Merge from origin/emacs-26
[emacs.git] / doc / emacs / building.texi
blob8e2fdf11370a9320e7c4c765fc37371239e5e0d9
1 @c This is part of the Emacs manual.
2 @c Copyright (C) 1985-1987, 1993-1995, 1997, 2000-2018 Free Software
3 @c Foundation, Inc.
4 @c See file emacs.texi for copying conditions.
5 @node Building
6 @chapter Compiling and Testing Programs
7 @cindex building programs
8 @cindex program building
9 @cindex running Lisp functions
11   The previous chapter discusses the Emacs commands that are useful
12 for making changes in programs.  This chapter deals with commands that
13 assist in the process of compiling and testing programs.
15 @menu
16 * Compilation::         Compiling programs in languages other
17                           than Lisp (C, Pascal, etc.).
18 * Compilation Mode::    The mode for visiting compiler errors.
19 * Compilation Shell::   Customizing your shell properly
20                           for use in the compilation buffer.
21 * Grep Searching::      Searching with grep.
22 * Flymake::             Finding syntax errors on the fly.
23 * Debuggers::           Running symbolic debuggers for non-Lisp programs.
24 * Executing Lisp::      Various modes for editing Lisp programs,
25                           with different facilities for running
26                           the Lisp programs.
27 * Libraries: Lisp Libraries.      How Lisp programs are loaded into Emacs.
28 * Eval: Lisp Eval.      Executing a single Lisp expression in Emacs.
29 * Interaction: Lisp Interaction.  Executing Lisp in an Emacs buffer.
30 * External Lisp::       Communicating through Emacs with a separate Lisp.
31 @end menu
33 @node Compilation
34 @section Running Compilations under Emacs
35 @cindex inferior process
36 @cindex make
37 @cindex compilation errors
38 @cindex error log
40   Emacs can run compilers for languages such as C and Fortran, feeding
41 the compilation log into an Emacs buffer.  It can also parse the error
42 messages and show you where the errors occurred.
44 @table @kbd
45 @item M-x compile
46 Run a compiler asynchronously under Emacs, with error messages going to
47 the @file{*compilation*} buffer.
48 @item M-x recompile
49 Invoke a compiler with the same command as in the last invocation of
50 @kbd{M-x compile}.
51 @item M-x kill-compilation
52 Kill the running compilation subprocess.
53 @end table
55 @findex compile
56   To run @code{make} or another compilation command, type @kbd{M-x
57 compile}.  This reads a shell command line using the minibuffer, and
58 then executes the command by running a shell as a subprocess (or
59 @dfn{inferior process}) of Emacs.  The output is inserted in a buffer
60 named @file{*compilation*}.  The current buffer's default directory is
61 used as the working directory for the execution of the command, so by
62 default compilation takes place in that directory.
64 @vindex compile-command
65   The default compilation command is @samp{make -k}, which is usually
66 correct for programs compiled using the @command{make} utility (the
67 @samp{-k} flag tells @command{make} to continue compiling as much as
68 possible after an error).  @xref{Top,, Make, make, GNU Make Manual}.
69 If you have done @kbd{M-x compile} before, the command that you
70 specified is automatically stored in the variable
71 @code{compile-command}; this is used as the default the next time you
72 type @kbd{M-x compile}.  A file can also specify a file-local value
73 for @code{compile-command} (@pxref{File Variables}).
75   Starting a compilation displays the @file{*compilation*} buffer in
76 another window but does not select it.  While the compilation is
77 running, the word @samp{run} is shown in the major mode indicator for
78 the @file{*compilation*} buffer, and the word @samp{Compiling} appears
79 in all mode lines.  You do not have to keep the @file{*compilation*}
80 buffer visible while compilation is running; it continues in any case.
81 When the compilation ends, for whatever reason, the mode line of the
82 @file{*compilation*} buffer changes to say @samp{exit} (followed by
83 the exit code: @samp{[0]} for a normal exit), or @samp{signal} (if a
84 signal terminated the process).
86   If you want to watch the compilation transcript as it appears,
87 switch to the @file{*compilation*} buffer and move point to the end of
88 the buffer.  When point is at the end, new compilation output is
89 inserted above point, which remains at the end.  Otherwise, point
90 remains fixed while compilation output is added at the end of the
91 buffer.
93   While compilation proceeds, the mode line shows the number of
94 errors, warnings, and informational messages emitted by the compiler
95 so far.
97 @cindex compilation buffer, keeping point at end
98 @vindex compilation-scroll-output
99   If you change the variable @code{compilation-scroll-output} to a
100 non-@code{nil} value, the @file{*compilation*} buffer scrolls
101 automatically to follow the output.  If the value is
102 @code{first-error}, scrolling stops when the first error appears,
103 leaving point at that error.  For any other non-@code{nil} value,
104 scrolling continues until there is no more output.
106 @findex recompile
107   To rerun the last compilation with the same command, type @kbd{M-x
108 recompile}.  This reuses the compilation command from the last
109 invocation of @kbd{M-x compile}.  It also reuses the
110 @file{*compilation*} buffer and starts the compilation in its default
111 directory, which is the directory in which the previous compilation
112 was started.
114 @findex kill-compilation
115 @vindex compilation-always-kill
116   Starting a new compilation also kills any compilation already
117 running in @file{*compilation*}, as the buffer can only handle one
118 compilation at any time.  However, @kbd{M-x compile} asks for
119 confirmation before actually killing a compilation that is running; to
120 always automatically kill the compilation without asking, change the
121 variable @code{compilation-always-kill} to @code{t}.  You can also
122 kill a compilation process with the command @kbd{M-x
123 kill-compilation}.
125   To run two compilations at once, start the first one, then rename
126 the @file{*compilation*} buffer (perhaps using @code{rename-uniquely};
127 @pxref{Misc Buffer}), then switch buffers and start the other
128 compilation.  This will create a new @file{*compilation*} buffer.
130 @vindex compilation-environment
131   You can control the environment passed to the compilation command
132 with the variable @code{compilation-environment}.  Its value is a list
133 of environment variable settings; each element should be a string of
134 the form @code{"@var{envvarname}=@var{value}"}.  These environment
135 variable settings override the usual ones.
137 @node Compilation Mode
138 @section Compilation Mode
140 @cindex Compilation mode
141 @cindex mode, Compilation
142 @cindex locus
143   The @file{*compilation*} buffer uses a major mode called Compilation
144 mode.  Compilation mode turns each error message in the buffer into a
145 hyperlink; you can move point to it and type @key{RET}, or click on it
146 with the mouse (@pxref{Mouse References}), to visit the @dfn{locus} of
147 the error message in a separate window.  The locus is the specific
148 position in a file where that error occurred.
150 @findex compile-goto-error
151 @vindex compilation-auto-jump-to-first-error
152   If you change the variable
153 @code{compilation-auto-jump-to-first-error} to a non-@code{nil} value,
154 Emacs automatically visits the locus of the first error message that
155 appears in the @file{*compilation*} buffer.
157   Compilation mode provides the following additional commands.  These
158 commands can also be used in @file{*grep*} buffers, where the
159 hyperlinks are search matches rather than error messages (@pxref{Grep
160 Searching}).
162 @table @kbd
163 @item M-g M-n
164 @itemx M-g n
165 @itemx C-x `
166 Visit the locus of the next error message or match (@code{next-error}).
167 @item M-g M-p
168 @itemx M-g p
169 Visit the locus of the previous error message or match
170 (@code{previous-error}).
171 @item M-n
172 Move point to the next error message or match, without visiting its
173 locus (@code{compilation-next-error}).
174 @item M-p
175 Move point to the previous error message or match, without visiting
176 its locus (@code{compilation-previous-error}).
177 @item M-@}
178 Move point to the next error message or match occurring in a different
179 file (@code{compilation-next-file}).
180 @item M-@{
181 Move point to the previous error message or match occurring in a
182 different file (@code{compilation-previous-file}).
183 @item C-c C-f
184 Toggle Next Error Follow minor mode, which makes cursor motion in the
185 compilation buffer produce automatic source display.
186 @end table
188 @kindex M-g M-n
189 @kindex M-g n
190 @kindex C-x `
191 @findex next-error
192 @vindex next-error-highlight
193   To visit errors sequentially, type @w{@kbd{C-x `}}
194 (@code{next-error}), or equivalently @kbd{M-g M-n} or @kbd{M-g n}.
195 This command can be invoked from any buffer, not just a Compilation
196 mode buffer.  The first time you invoke it after a compilation, it
197 visits the locus of the first error message.  Each subsequent
198 @w{@kbd{C-x `}} visits the next error, in a similar fashion.  If you
199 visit a specific error with @key{RET} or a mouse click in the
200 @file{*compilation*} buffer, subsequent @w{@kbd{C-x `}} commands
201 advance from there.  When @w{@kbd{C-x `}} finds no more error messages
202 to visit, it signals an error.  @w{@kbd{C-u C-x `}} starts again from
203 the beginning of the compilation buffer, and visits the first locus.
205   @kbd{M-g M-p} or @kbd{M-g p} (@code{previous-error}) iterates
206 through errors in the opposite direction.
208   The @code{next-error} and @code{previous-error} commands don't just
209 act on the errors or matches listed in @file{*compilation*} and
210 @file{*grep*} buffers; they also know how to iterate through error or
211 match lists produced by other commands, such as @kbd{M-x occur}
212 (@pxref{Other Repeating Search}).  If the current buffer contains
213 error messages or matches, these commands will iterate through them;
214 otherwise, Emacs looks for a buffer containing error messages or
215 matches amongst the windows of the selected frame, then for any buffer
216 that @code{next-error} or @code{previous-error} previously visited,
217 and finally all other buffers.  Any buffer these commands iterate
218 through that is not currently displayed in a window will be displayed.
220 @vindex compilation-skip-threshold
221   By default, the @code{next-error} and @code{previous-error} commands
222 skip less important messages.  The variable
223 @code{compilation-skip-threshold} controls this.  The default value,
224 1, means to skip anything less important than a warning.  A value of 2
225 means to skip anything less important than an error, while 0 means not
226 to skip any messages.
228   When Emacs visits the locus of an error message, it momentarily
229 highlights the relevant source line.  The duration of this highlight
230 is determined by the variable @code{next-error-highlight}.
232 @vindex compilation-context-lines
233   If the @file{*compilation*} buffer is shown in a window with a left
234 fringe (@pxref{Fringes}), the locus-visiting commands put an arrow in
235 the fringe, pointing to the current error message.  If the window has
236 no left fringe, such as on a text terminal, these commands scroll the
237 window so that the current message is at the top of the window.  If
238 you change the variable @code{compilation-context-lines} to an integer
239 value @var{n}, these commands scroll the window so that the current
240 error message is @var{n} lines from the top, whether or not there is a
241 fringe; the default value, @code{nil}, gives the behavior described
242 above.
244 @vindex compilation-error-regexp-alist
245 @vindex grep-regexp-alist
246   To parse messages from the compiler, Compilation mode uses the
247 variable @code{compilation-error-regexp-alist} which lists various
248 error message formats and tells Emacs how to extract the locus from
249 each.  A similar variable, @code{grep-regexp-alist}, tells Emacs how
250 to parse output from a @code{grep} command (@pxref{Grep Searching}).
252 @findex compilation-next-error
253 @findex compilation-previous-error
254 @findex compilation-next-file
255 @findex compilation-previous-file
256   Compilation mode also defines the keys @key{SPC} and @key{DEL} to
257 scroll by screenfuls; @kbd{M-n} (@code{compilation-next-error}) and
258 @kbd{M-p} (@code{compilation-previous-error}) to move to the next or
259 previous error message; and @kbd{M-@{} (@code{compilation-next-file})
260 and @kbd{M-@}} (@code{compilation-previous-file}) to move to the next
261 or previous error message for a different source file.
263 @cindex Next Error Follow mode
264 @findex next-error-follow-minor-mode
265   You can type @kbd{C-c C-f} to toggle Next Error Follow mode.  In
266 this minor mode, ordinary cursor motion in the compilation buffer
267 automatically updates the source buffer, i.e., moving the cursor over
268 an error message causes the locus of that error to be displayed.
270   The features of Compilation mode are also available in a minor mode
271 called Compilation Minor mode.  This lets you parse error messages in
272 any buffer, not just a normal compilation output buffer.  Type
273 @kbd{M-x compilation-minor-mode} to enable the minor mode.  For
274 instance, in an Rlogin buffer (@pxref{Remote Host}), Compilation minor
275 mode automatically accesses remote source files by FTP (@pxref{File
276 Names}).
278 @node Compilation Shell
279 @section Subshells for Compilation
281   The @kbd{M-x compile} command uses a shell to run the compilation
282 command, but specifies the option for a noninteractive shell.  This
283 means, in particular, that the shell should start with no prompt.  If
284 you find your usual shell prompt making an unsightly appearance in the
285 @file{*compilation*} buffer, it means you have made a mistake in your
286 shell's init file by setting the prompt unconditionally.  (This init
287 file may be named @file{.bashrc}, @file{.profile}, @file{.cshrc},
288 @file{.shrc}, etc., depending on what shell you use.)  The shell init
289 file should set the prompt only if there already is a prompt.  Here's
290 how to do it in bash:
292 @example
293 if [ "$@{PS1+set@}" = set ]
294 then PS1=@dots{}
296 @end example
298 @noindent
299 And here's how to do it in csh:
301 @example
302 if ($?prompt) set prompt = @dots{}
303 @end example
305 @vindex TERM@r{, environment variable, in compilation mode}
306   If you want to customize the value of the @env{TERM} environment
307 variable passed to the compilation subshell, customize the variable
308 @code{comint-terminfo-terminal} (@pxref{Shell Options}).
310   Emacs does not expect a compiler process to launch asynchronous
311 subprocesses; if it does, and they keep running after the main
312 compiler process has terminated, Emacs may kill them or their output
313 may not arrive in Emacs.  To avoid this problem, make the main
314 compilation process wait for its subprocesses to finish.  In a shell
315 script, you can do this using @samp{$!} and @samp{wait}, like this:
317 @example
318 (sleep 10; echo 2nd)& pid=$!  # @r{Record pid of subprocess}
319 echo first message
320 wait $pid                     # @r{Wait for subprocess}
321 @end example
323 @noindent
324 If the background process does not output to the compilation buffer,
325 so you only need to prevent it from being killed when the main
326 compilation process terminates, this is sufficient:
328 @example
329 nohup @var{command}; sleep 1
330 @end example
332 @ifnottex
333   On MS-DOS, asynchronous subprocesses are
334 not supported, so @kbd{M-x compile} runs the compilation command
335 synchronously (i.e., you must wait until the command finishes before
336 you can do anything else in Emacs).  @xref{MS-DOS}.
337 @end ifnottex
339 @node Grep Searching
340 @section Searching with Grep under Emacs
342   Just as you can run a compiler from Emacs and then visit the lines
343 with compilation errors, you can also run @command{grep} and then
344 visit the lines on which matches were found.  This works by treating
345 the matches reported by @command{grep} as if they were errors.
346 The output buffer uses Grep mode, which is a variant of Compilation
347 mode (@pxref{Compilation Mode}).
349 @table @kbd
350 @item M-x grep
351 @itemx M-x lgrep
352 Run @command{grep} asynchronously under Emacs, listing matching lines in
353 the buffer named @file{*grep*}.
354 @item M-x grep-find
355 @itemx M-x find-grep
356 @itemx M-x rgrep
357 Run @command{grep} via @code{find}, and collect output in the
358 @file{*grep*} buffer.
359 @item M-x zrgrep
360 Run @code{zgrep} and collect output in the @file{*grep*} buffer.
361 @item M-x kill-grep
362 Kill the running @command{grep} subprocess.
363 @end table
365 @findex grep
366   To run @command{grep}, type @kbd{M-x grep}, then enter a command line
367 that specifies how to run @command{grep}.  Use the same arguments you
368 would give @command{grep} when running it normally: a @command{grep}-style
369 regexp (usually in single-quotes to quote the shell's special
370 characters) followed by file names, which may use wildcards.  If you
371 specify a prefix argument for @kbd{M-x grep}, it finds the identifier
372 (@pxref{Xref}) in the buffer around point, and puts that into the
373 default @command{grep} command.
375   Your command need not simply run @command{grep}; you can use any shell
376 command that produces output in the same format.  For instance, you
377 can chain @command{grep} commands, like this:
379 @example
380 grep -nH -e foo *.el | grep bar | grep toto
381 @end example
383   The output from @command{grep} goes in the @file{*grep*} buffer.  You
384 can find the corresponding lines in the original files using @w{@kbd{C-x
385 `}}, @key{RET}, and so forth, just like compilation errors.
387   Some grep programs accept a @samp{--color} option to output special
388 markers around matches for the purpose of highlighting.  You can make
389 use of this feature by setting @code{grep-highlight-matches} to
390 @code{t}.  When displaying a match in the source buffer, the exact
391 match will be highlighted, instead of the entire source line.
393   As with compilation commands (@pxref{Compilation}), while the grep
394 command runs, the mode line shows the running number of matches found
395 and highlighted so far.
397   The @command{grep} commands will offer to save buffers before
398 running.  This is controlled by the @code{grep-save-buffers} variable.
399 The possible values are either @code{nil} (don't save), @code{ask}
400 (ask before saving), or a function which will be used as a predicate
401 (and is called with the file name as the parameter and should return
402 non-@code{nil} if the buffer is to be saved).  Any other
403 non-@code{nil} value means that all buffers should be saved without
404 asking.
406 @findex grep-find
407 @findex find-grep
408   The command @kbd{M-x grep-find} (also available as @kbd{M-x
409 find-grep}) is similar to @kbd{M-x grep}, but it supplies a different
410 initial default for the command---one that runs both @code{find} and
411 @command{grep}, so as to search every file in a directory tree.  See also
412 the @code{find-grep-dired} command, in @ref{Dired and Find}.
414 @findex lgrep
415 @findex rgrep
416 @findex zrgrep
417   The commands @kbd{M-x lgrep} (local grep) and @kbd{M-x rgrep}
418 (recursive grep) are more user-friendly versions of @command{grep} and
419 @code{grep-find}, which prompt separately for the regular expression
420 to match, the files to search, and the base directory for the search.
421 Case sensitivity of the search is controlled by the current value of
422 @code{case-fold-search}.  The command @kbd{M-x zrgrep} is similar to
423 @kbd{M-x rgrep}, but it calls @command{zgrep} instead of
424 @command{grep} to search the contents of gzipped files.
426   These commands build the shell commands based on the variables
427 @code{grep-template} (for @code{lgrep}) and @code{grep-find-template}
428 (for @code{rgrep}).  The files to search can use aliases defined in
429 the variable @code{grep-files-aliases}.
431 @vindex grep-find-ignored-directories
432   Directories listed in the variable
433 @code{grep-find-ignored-directories} are automatically skipped by
434 @kbd{M-x rgrep}.  The default value includes the data directories used
435 by various version control systems.
437 @vindex grep-find-hide
438   By default, the shell commands constructed for @code{lgrep},
439 @code{rgrep}, and @code{zgrep} are abbreviated for display by
440 concealing the part that contains a long list of files and directories
441 to ignore.  You can reveal the concealed part by clicking on the
442 button with ellipsis, which represents them.  To disable this
443 abbreviation of the shell commands, customize the option
444 @code{grep-find-hide} to a @code{nil} value.
446 @node Flymake
447 @section Finding Syntax Errors On The Fly
448 @cindex checking syntax
450   Flymake mode is a minor mode that performs on-the-fly syntax
451 checking for many programming and markup languages, including C, C++,
452 Perl, HTML, and @TeX{}/@LaTeX{}.  It is somewhat analogous to Flyspell
453 mode, which performs spell checking for ordinary human languages in a
454 similar fashion (@pxref{Spelling}).  As you edit a file, Flymake mode
455 runs an appropriate syntax checking tool in the background, using a
456 temporary copy of the buffer.  It then parses the error and warning
457 messages, and highlights the erroneous lines in the buffer.  The
458 syntax checking tool used depends on the language; for example, for
459 C/C++ files this is usually the C compiler.  Flymake can also use
460 build tools such as @code{make} for checking complicated projects.
462   To enable Flymake mode, type @kbd{M-x flymake-mode}.  You can jump
463 to the errors that it finds by using @kbd{M-x flymake-goto-next-error}
464 and @kbd{M-x flymake-goto-prev-error}.  To display any error messages
465 associated with the current line, type @kbd{M-x
466 flymake-display-err-menu-for-current-line}.
468   For more details about using Flymake,
469 @ifnottex
470 see @ref{Top, Flymake, Flymake, flymake, The Flymake Manual}.
471 @end ifnottex
472 @iftex
473 see the Flymake Info manual, which is distributed with Emacs.
474 @end iftex
476 @node Debuggers
477 @section Running Debuggers Under Emacs
478 @cindex debuggers
479 @cindex GUD library
480 @cindex GDB
481 @cindex DBX
482 @cindex SDB
483 @cindex XDB
484 @cindex Perldb
485 @cindex JDB
486 @cindex PDB
488 The GUD (Grand Unified Debugger) library provides an Emacs interface
489 to a wide variety of symbolic debuggers.  It can run the GNU Debugger
490 (GDB), as well as DBX, SDB, XDB, Perl's debugging mode, the Python
491 debugger PDB, and the Java Debugger JDB.
493   Emacs provides a special interface to GDB, which uses extra Emacs
494 windows to display the state of the debugged program.  @xref{GDB
495 Graphical Interface}.
497   Emacs also has a built-in debugger for Emacs Lisp programs.
498 @xref{Debugging,, The Lisp Debugger, elisp, the Emacs Lisp Reference
499 Manual}.
501 @menu
502 * Starting GUD::        How to start a debugger subprocess.
503 * Debugger Operation::  Connection between the debugger and source buffers.
504 * Commands of GUD::     Key bindings for common commands.
505 * GUD Customization::   Defining your own commands for GUD.
506 * GDB Graphical Interface::  An enhanced mode that uses GDB features to
507                         implement a graphical debugging environment.
508 @end menu
510 @node Starting GUD
511 @subsection Starting GUD
513   There are several commands for starting a debugger subprocess, each
514 corresponding to a particular debugger program.
516 @table @kbd
517 @item M-x gdb
518 @findex gdb
519 Run GDB as a subprocess, and interact with it via an IDE-like Emacs
520 interface.  @xref{GDB Graphical Interface}, for more information about
521 this command.
523 @item M-x gud-gdb
524 @findex gud-gdb
525 Run GDB, using a GUD interaction buffer for input and output to the
526 GDB subprocess (@pxref{Debugger Operation}).  If such a buffer already
527 exists, switch to it; otherwise, create the buffer and switch to it.
529 The other commands in this list do the same, for other debugger
530 programs.
532 @item M-x perldb
533 @findex perldb
534 Run the Perl interpreter in debug mode.
536 @item M-x jdb
537 @findex jdb
538 Run the Java debugger.
540 @item M-x pdb
541 @findex pdb
542 Run the Python debugger.
544 @item M-x dbx
545 @findex dbx
546 Run the DBX debugger.
548 @item M-x xdb
549 @findex xdb
550 @vindex gud-xdb-directories
551 Run the XDB debugger.
553 @item M-x sdb
554 @findex sdb
555 Run the SDB debugger.
556 @end table
558   Each of these commands reads a command line to invoke the debugger,
559 using the minibuffer.  The minibuffer's initial contents contain the
560 standard executable name and options for the debugger, and sometimes
561 also a guess for the name of the executable file you want to debug.
562 Shell wildcards and variables are not allowed in this command line.
563 Emacs assumes that the first command argument which does not start
564 with a @samp{-} is the executable file name.
566 @cindex remote host, debugging on
567   Tramp provides a facility for remote debugging, whereby both the
568 debugger and the program being debugged are on the same remote host.
569 @xref{Running a debugger on a remote host,,, tramp, The Tramp Manual},
570 for details.  This is separate from GDB's remote debugging feature,
571 where the program and the debugger run on different machines
572 (@pxref{Remote Debugging,, Debugging Remote Programs, gdb, The GNU
573 debugger}).
575 @node Debugger Operation
576 @subsection Debugger Operation
577 @cindex GUD interaction buffer
579   The @dfn{GUD interaction buffer} is an Emacs buffer which is used to
580 send text commands to a debugger subprocess, and record its output.
581 This is the basic interface for interacting with a debugger, used by
582 @kbd{M-x gud-gdb} and other commands listed in
583 @iftex
584 the preceding section.
585 @end iftex
586 @ifnottex
587 @ref{Starting GUD}.
588 @end ifnottex
589 The @kbd{M-x gdb} command extends this interface with additional
590 specialized buffers for controlling breakpoints, stack frames, and
591 other aspects of the debugger state (@pxref{GDB Graphical Interface}).
593   The GUD interaction buffer uses a variant of Shell mode, so the
594 Emacs commands defined by Shell mode are available (@pxref{Shell
595 Mode}).  Completion is available for most debugger commands
596 (@pxref{Completion}), and you can use the usual Shell mode history
597 commands to repeat them.
598 @iftex
599 See the next section
600 @end iftex
601 @ifnottex
602 @xref{Commands of GUD},
603 @end ifnottex
604 for special commands that can be used in the GUD interaction buffer.
606   As you debug a program, Emacs displays the relevant source files by
607 visiting them in Emacs buffers, with an arrow in the left fringe
608 indicating the current execution line.  (On a text terminal, the arrow
609 appears as @samp{=>}, overlaid on the first two text columns.)  Moving
610 point in such a buffer does not move the arrow.  You are free to edit
611 these source files, but note that inserting or deleting lines will
612 throw off the arrow's positioning, as Emacs has no way to figure out
613 which edited source line corresponds to the line reported by the
614 debugger subprocess.  To update this information, you typically have
615 to recompile and restart the program.
617 @cindex GUD Tooltip mode
618 @cindex mode, GUD Tooltip
619 @findex gud-tooltip-mode
620 @vindex gud-tooltip-echo-area
621   GUD Tooltip mode is a global minor mode that adds tooltip support to
622 GUD@.  To toggle this mode, type @kbd{M-x gud-tooltip-mode}.  It is
623 disabled by default.  If enabled, you can move the mouse pointer over a
624 variable, a function, or a macro (collectively called
625 @dfn{identifiers}) to show their values in tooltips
626 (@pxref{Tooltips}).  If just placing the mouse pointer over an
627 expression doesn't show the value of the expression you had in mind,
628 you can tell Emacs more explicitly what expression to evaluate by
629 dragging the mouse over the expression, then leaving the mouse inside
630 the marked area.  The GUD Tooltip mode takes effect in the GUD
631 interaction buffer, and in all source buffers with major modes listed
632 in the variable @code{gud-tooltip-modes}.  If the variable
633 @code{gud-tooltip-echo-area} is non-@code{nil}, or if you turned off
634 the tooltip mode, values are shown in the echo area instead of a
635 tooltip.
637   When using GUD Tooltip mode with @kbd{M-x gud-gdb}, displaying an
638 expression's value in GDB can sometimes expand a macro, potentially
639 causing side effects in the debugged program.  For that reason, using
640 tooltips in @code{gud-gdb} is disabled.  If you use the @kbd{M-x gdb}
641 interface, this problem does not occur, as there is special code to
642 avoid side-effects; furthermore, you can display macro definitions
643 associated with an identifier when the program is not executing.
645 @node Commands of GUD
646 @subsection Commands of GUD
648   GUD provides commands for setting and clearing breakpoints,
649 selecting stack frames, and stepping through the program.
651 @table @kbd
652 @item C-x C-a C-b
653 @kindex C-x C-a C-b
654 Set a breakpoint on the source line that point is on.
655 @end table
657   @kbd{C-x C-a C-b} (@code{gud-break}), when called in a source
658 buffer, sets a debugger breakpoint on the current source line.  This
659 command is available only after starting GUD@.  If you call it in a
660 buffer that is not associated with any debugger subprocess, it signals
661 an error.
663 @kindex C-x C-a @r{(GUD)}
664   The following commands are available both in the GUD interaction
665 buffer and globally, but with different key bindings.  The keys
666 starting with @kbd{C-c} are available only in the GUD interaction
667 buffer, while those starting with @kbd{C-x C-a} are available
668 globally.  Some of these commands are also available via the tool bar;
669 some are not supported by certain debuggers.
671 @table @kbd
672 @item C-c C-l
673 @kindex C-c C-l @r{(GUD)}
674 @itemx C-x C-a C-l
675 @findex gud-refresh
676 Display, in another window, the last source line referred to in the
677 GUD interaction buffer (@code{gud-refresh}).
679 @item C-c C-s
680 @kindex C-c C-s @r{(GUD)}
681 @itemx C-x C-a C-s
682 @findex gud-step
683 Execute the next single line of code (@code{gud-step}).  If the line
684 contains a function call, execution stops after entering the called
685 function.
687 @item C-c C-n
688 @kindex C-c C-n @r{(GUD)}
689 @itemx C-x C-a C-n
690 @findex gud-next
691 Execute the next single line of code, stepping across function calls
692 without stopping inside the functions (@code{gud-next}).
694 @item C-c C-i
695 @kindex C-c C-i @r{(GUD)}
696 @itemx C-x C-a C-i
697 @findex gud-stepi
698 Execute a single machine instruction (@code{gud-stepi}).
700 @item C-c C-p
701 @kindex C-c C-p @r{(GUD)}
702 @itemx C-x C-a C-p
703 @findex gud-print
704 Evaluate the expression at point (@code{gud-print}).  If Emacs
705 does not print the exact expression that you want, mark it as a region
706 first.
708 @need 3000
709 @item C-c C-r
710 @kindex C-c C-r @r{(GUD)}
711 @itemx C-x C-a C-r
712 @findex gud-cont
713 Continue execution without specifying any stopping point.  The program
714 will run until it hits a breakpoint, terminates, or gets a signal that
715 the debugger is checking for (@code{gud-cont}).
717 @need 1000
718 @item C-c C-d
719 @kindex C-c C-d @r{(GUD)}
720 @itemx C-x C-a C-d
721 @findex gud-remove
722 Delete the breakpoint(s) on the current source line, if any
723 (@code{gud-remove}).  If you use this command in the GUD interaction
724 buffer, it applies to the line where the program last stopped.
726 @item C-c C-t
727 @kindex C-c C-t @r{(GUD)}
728 @itemx C-x C-a C-t
729 @findex gud-tbreak
730 Set a temporary breakpoint on the current source line, if any
731 (@code{gud-tbreak}).  If you use this command in the GUD interaction
732 buffer, it applies to the line where the program last stopped.
734 @item C-c <
735 @kindex C-c < @r{(GUD)}
736 @itemx C-x C-a <
737 @findex gud-up
738 Select the next enclosing stack frame (@code{gud-up}).  This is
739 equivalent to the GDB command @samp{up}.
741 @item C-c >
742 @kindex C-c > @r{(GUD)}
743 @itemx C-x C-a >
744 @findex gud-down
745 Select the next inner stack frame (@code{gud-down}).  This is
746 equivalent to the GDB command @samp{down}.
748 @item C-c C-u
749 @kindex C-c C-u @r{(GUD)}
750 @itemx C-x C-a C-u
751 @findex gud-until
752 Continue execution to the current line (@code{gud-until}).  The
753 program will run until it hits a breakpoint, terminates, gets a signal
754 that the debugger is checking for, or reaches the line on which the
755 cursor currently sits.
757 @item C-c C-f
758 @kindex C-c C-f @r{(GUD)}
759 @itemx C-x C-a C-f
760 @findex gud-finish
761 Run the program until the selected stack frame returns or
762 stops for some other reason (@code{gud-finish}).
763 @end table
765   If you are using GDB, these additional key bindings are available:
767 @table @kbd
768 @item C-x C-a C-j
769 @kindex C-x C-a C-j @r{(GUD)}
770 @findex gud-jump
771 Only useful in a source buffer, @code{gud-jump} transfers the
772 program's execution point to the current line.  In other words, the
773 next line that the program executes will be the one where you gave the
774 command.  If the new execution line is in a different function from
775 the previously one, GDB prompts for confirmation since the results may
776 be bizarre.  See the GDB manual entry regarding @code{jump} for
777 details.
779 @item @key{TAB}
780 @kindex TAB @r{(GUD)}
781 @findex gud-gdb-complete-command
782 With GDB, complete a symbol name (@code{gud-gdb-complete-command}).
783 This key is available only in the GUD interaction buffer.
784 @end table
786   These commands interpret a numeric argument as a repeat count, when
787 that makes sense.
789   Because @key{TAB} serves as a completion command, you can't use it to
790 enter a tab as input to the program you are debugging with GDB@.
791 Instead, type @kbd{C-q @key{TAB}} to enter a tab.
793 @node GUD Customization
794 @subsection GUD Customization
796 @vindex gdb-mode-hook
797 @vindex dbx-mode-hook
798 @vindex sdb-mode-hook
799 @vindex xdb-mode-hook
800 @vindex perldb-mode-hook
801 @vindex pdb-mode-hook
802 @vindex jdb-mode-hook
803   On startup, GUD runs one of the following hooks:
804 @code{gdb-mode-hook}, if you are using GDB; @code{dbx-mode-hook}, if
805 you are using DBX; @code{sdb-mode-hook}, if you are using SDB;
806 @code{xdb-mode-hook}, if you are using XDB; @code{perldb-mode-hook},
807 for Perl debugging mode; @code{pdb-mode-hook}, for PDB;
808 @code{jdb-mode-hook}, for JDB@.  @xref{Hooks}.
810   The @code{gud-def} Lisp macro (@pxref{Defining Macros,,, elisp, the
811 Emacs Lisp Reference Manual}) provides a convenient way to define an
812 Emacs command that sends a particular command string to the debugger,
813 and set up a key binding for in the GUD interaction buffer:
815 @findex gud-def
816 @example
817 (gud-def @var{function} @var{cmdstring} @var{binding} @var{docstring})
818 @end example
820   This defines a command named @var{function} which sends
821 @var{cmdstring} to the debugger process, and gives it the documentation
822 string @var{docstring}.  You can then use the command @var{function} in any
823 buffer.  If @var{binding} is non-@code{nil}, @code{gud-def} also binds
824 the command to @kbd{C-c @var{binding}} in the GUD buffer's mode and to
825 @kbd{C-x C-a @var{binding}} generally.
827   The command string @var{cmdstring} may contain certain
828 @samp{%}-sequences that stand for data to be filled in at the time
829 @var{function} is called:
831 @table @samp
832 @item %f
833 The name of the current source file.  If the current buffer is the GUD
834 buffer, then the current source file is the file that the program
835 stopped in.
837 @item %l
838 The number of the current source line.  If the current buffer is the GUD
839 buffer, then the current source line is the line that the program
840 stopped in.
842 @item %e
843 In transient-mark-mode the text in the region, if it is active.
844 Otherwise the text of the C lvalue or function-call expression at or
845 adjacent to point.
847 @item %a
848 The text of the hexadecimal address at or adjacent to point.
850 @item %p
851 The numeric argument of the called function, as a decimal number.  If
852 the command is used without a numeric argument, @samp{%p} stands for the
853 empty string.
855 If you don't use @samp{%p} in the command string, the command you define
856 ignores any numeric argument.
858 @item %d
859 The name of the directory of the current source file.
861 @item %c
862 Fully qualified class name derived from the expression surrounding point
863 (jdb only).
864 @end table
866 @node GDB Graphical Interface
867 @subsection GDB Graphical Interface
869   The command @kbd{M-x gdb} starts GDB in an IDE-like interface, with
870 specialized buffers for controlling breakpoints, stack frames, and
871 other aspects of the debugger state.  It also provides additional ways
872 to control the debugging session with the mouse, such as clicking in
873 the fringe of a source buffer to set a breakpoint there.
875 @vindex gud-gdb-command-name
876   To run GDB using just the GUD interaction buffer interface, without
877 these additional features, use @kbd{M-x gud-gdb} (@pxref{Starting
878 GUD}).  You must use this if you want to debug multiple programs
879 within one Emacs session, as that is currently unsupported by @kbd{M-x
880 gdb}.
882   Internally, @kbd{M-x gdb} informs GDB that its screen size is
883 unlimited; for correct operation, you must not change GDB's screen
884 height and width values during the debugging session.
886 @menu
887 * GDB User Interface Layout::   Control the number of displayed buffers.
888 * Source Buffers::              Use the mouse in the fringe/margin to
889                                 control your program.
890 * Breakpoints Buffer::          A breakpoint control panel.
891 * Threads Buffer::              Displays your threads.
892 * Stack Buffer::                Select a frame from the call stack.
893 * Other GDB Buffers::           Other buffers for controlling the GDB state.
894 * Watch Expressions::           Monitor variable values in the speedbar.
895 * Multithreaded Debugging::     Debugging programs with several threads.
896 @end menu
898 @node GDB User Interface Layout
899 @subsubsection GDB User Interface Layout
900 @cindex GDB User Interface layout
902 @vindex gdb-many-windows
903   If the variable @code{gdb-many-windows} is @code{nil} (the default),
904 @kbd{M-x gdb} normally displays only the GUD interaction buffer.
905 However, if the variable @code{gdb-show-main} is also non-@code{nil},
906 it starts with two windows: one displaying the GUD interaction buffer,
907 and the other showing the source for the @code{main} function of the
908 program you are debugging.
910   If @code{gdb-many-windows} is non-@code{nil}, then @kbd{M-x gdb}
911 displays the following frame layout:
913 @smallexample
914 @group
915 +--------------------------------+--------------------------------+
916 |   GUD interaction buffer       |   Locals/Registers buffer      |
917 |--------------------------------+--------------------------------+
918 |   Primary Source buffer        |   I/O buffer for debugged pgm  |
919 |--------------------------------+--------------------------------+
920 |   Stack buffer                 |   Breakpoints/Threads buffer   |
921 +--------------------------------+--------------------------------+
922 @end group
923 @end smallexample
925 @findex gdb-restore-windows
926 @findex gdb-many-windows
927   If you ever change the window layout, you can restore the many-windows
928 layout by typing @kbd{M-x gdb-restore-windows}.  To toggle
929 between the many windows layout and a simple layout with just the GUD
930 interaction buffer and a source file, type @kbd{M-x gdb-many-windows}.
932   If you have an elaborate window setup, and don't want
933 @code{gdb-many-windows} to disrupt that, it is better to invoke
934 @kbd{M-x gdb} in a separate frame to begin with, then the arrangement
935 of windows on your original frame will not be affected.  A separate
936 frame for GDB sessions can come in especially handy if you work on a
937 text-mode terminal, where the screen estate for windows could be at a
938 premium.
940   You may also specify additional GDB-related buffers to display,
941 either in the same frame or a different one.  Select the buffers you
942 want by typing @kbd{M-x gdb-display-@var{buffertype}-buffer} or
943 @kbd{M-x gdb-frame-@var{buffertype}-buffer}, where @var{buffertype}
944 is the relevant buffer type, such as @samp{breakpoints}.  You can do
945 the same with the menu bar, with the @samp{GDB-Windows} and
946 @samp{GDB-Frames} sub-menus of the @samp{GUD} menu.
948   When you finish debugging, kill the GUD interaction buffer with
949 @kbd{C-x k}, which will also kill all the buffers associated with the
950 session.  However you need not do this if, after editing and
951 re-compiling your source code within Emacs, you wish to continue
952 debugging.  When you restart execution, GDB automatically finds the
953 new executable.  Keeping the GUD interaction buffer has the advantage
954 of keeping the shell history as well as GDB's breakpoints.  You do
955 need to check that the breakpoints in recently edited source files are
956 still in the right places.
958 @node Source Buffers
959 @subsubsection Source Buffers
960 @cindex fringes, for debugging
962 @table @asis
963 @item @kbd{mouse-1} (in fringe)
964 Set or clear a breakpoint on that line.
966 @item @kbd{C-mouse-1} (in fringe)
967 Enable or disable a breakpoint on that line.
969 @item @kbd{mouse-3} (in fringe)
970 Continue execution to that line.
972 @item @kbd{C-mouse-3} (in fringe)
973 Jump to that line.
974 @end table
976   On a graphical display, you can click @kbd{mouse-1} in the fringe of
977 a source buffer, to set a breakpoint on that line (@pxref{Fringes}).
978 A red dot appears in the fringe, where you clicked.  If a breakpoint
979 already exists there, the click removes it.  A @kbd{C-mouse-1} click
980 enables or disables an existing breakpoint; a breakpoint that is
981 disabled, but not unset, is indicated by a gray dot.
983   On a text terminal, or when fringes are disabled, enabled
984 breakpoints are indicated with a @samp{B} character in the left margin
985 of the window.  Disabled breakpoints are indicated with @samp{b}.
986 (The margin is only displayed if a breakpoint is present.)
988   A solid arrow in the left fringe of a source buffer indicates the
989 line of the innermost frame where the debugged program has stopped.  A
990 hollow arrow indicates the current execution line of a higher-level
991 frame.  If you drag the arrow in the fringe with @kbd{mouse-1}, that
992 causes execution to advance to the line where you release the button.
993 Alternatively, you can click @kbd{mouse-3} in the fringe to advance to
994 that line.  You can click @kbd{C-mouse-3} in the fringe to jump to
995 that line without executing the intermediate lines.  This command
996 allows you to go backwards, which can be useful for running through
997 code that has already executed, in order to examine its execution in
998 more detail.
1000 @node Breakpoints Buffer
1001 @subsubsection Breakpoints Buffer
1003   The GDB Breakpoints buffer shows the breakpoints, watchpoints and
1004 catchpoints in the debugger session.  @xref{Breakpoints,,, gdb, The
1005 GNU debugger}.  It provides the following commands, which mostly apply
1006 to the @dfn{current breakpoint} (the breakpoint which point is on):
1008 @table @kbd
1009 @item @key{SPC}
1010 @kindex SPC @r{(GDB Breakpoints buffer)}
1011 @findex gdb-toggle-breakpoint
1012 Enable/disable current breakpoint (@code{gdb-toggle-breakpoint}).  On
1013 a graphical display, this changes the color of the dot in the fringe
1014 of the source buffer at that line.  The dot is red when the breakpoint
1015 is enabled, and gray when it is disabled.
1017 @item D
1018 @kindex D @r{(GDB Breakpoints buffer)}
1019 @findex gdb-delete-breakpoint
1020 Delete the current breakpoint (@code{gdb-delete-breakpoint}).
1022 @item @key{RET}
1023 @kindex RET @r{(GDB Breakpoints buffer)}
1024 @findex gdb-goto-breakpoint
1025 Visit the source line for the current breakpoint
1026 (@code{gdb-goto-breakpoint}).
1028 @item mouse-2
1029 @kindex mouse-2 @r{(GDB Breakpoints buffer)}
1030 Visit the source line for the breakpoint you click on.
1031 @end table
1033 @vindex gdb-show-threads-by-default
1034   When @code{gdb-many-windows} is non-@code{nil}, the GDB Breakpoints
1035 buffer shares its window with the GDB Threads buffer.  To switch from
1036 one to the other click with @kbd{mouse-1} on the relevant button in
1037 the header line.  If @code{gdb-show-threads-by-default} is
1038 non-@code{nil}, the GDB Threads buffer is the one shown by default.
1040 @node Threads Buffer
1041 @subsubsection Threads Buffer
1043 @findex gdb-select-thread
1044   The GDB Threads buffer displays a summary of the threads in the
1045 debugged program.  @xref{Threads, Threads, Debugging programs with
1046 multiple threads, gdb, The GNU debugger}.  To select a thread, move
1047 point there and press @key{RET} (@code{gdb-select-thread}), or click on
1048 it with @kbd{mouse-2}.  This also displays the associated source
1049 buffer, and updates the contents of the other GDB buffers.
1051   You can customize variables under @code{gdb-buffers} group to select
1052 fields included in GDB Threads buffer.
1054 @table @code
1055 @item gdb-thread-buffer-verbose-names
1056 @vindex gdb-thread-buffer-verbose-names
1057 Show long thread names like @samp{Thread 0x4e2ab70 (LWP 1983)}.
1059 @item gdb-thread-buffer-arguments
1060 @vindex gdb-thread-buffer-arguments
1061 Show arguments of thread top frames.
1063 @item gdb-thread-buffer-locations
1064 @vindex gdb-thread-buffer-locations
1065 Show file information or library names.
1067 @item gdb-thread-buffer-addresses
1068 @vindex gdb-thread-buffer-addresses
1069 Show addresses for thread frames in threads buffer.
1070 @end table
1072   To view information for several threads simultaneously, use the
1073 following commands from the GDB Threads buffer.
1075 @table @kbd
1076 @item d
1077 @kindex d @r{(GDB threads buffer)}
1078 @findex gdb-display-disassembly-for-thread
1079 Display disassembly buffer for the thread at current line
1080 (@code{gdb-display-disassembly-for-thread}).
1082 @item f
1083 @kindex f @r{(GDB threads buffer)}
1084 @findex gdb-display-stack-for-thread
1085 Display the GDB Stack buffer for the thread at current line
1086 (@code{gdb-display-stack-for-thread}).
1088 @item l
1089 @kindex l @r{(GDB threads buffer)}
1090 @findex gdb-display-locals-for-thread
1091 Display the GDB Locals buffer for the thread at current line
1092 (@code{gdb-display-locals-for-thread}).
1094 @item r
1095 @kindex r @r{(GDB threads buffer)}
1096 @findex gdb-display-registers-for-thread
1097 Display the GDB Registers buffer for the thread at current line
1098 (@code{gdb-display-registers-for-thread}).
1099 @end table
1101 @noindent
1102 Their upper-case counterparts, @kbd{D}, @kbd{F} ,@kbd{L} and @kbd{R},
1103 display the corresponding buffer in a new frame.
1105   When you create a buffer showing information about some specific
1106 thread, it becomes bound to that thread and keeps showing actual
1107 information while you debug your program.  The mode indicator for each
1108 GDB buffer shows the number of the thread whose information that
1109 buffer displays.  The thread number is also included in the name of
1110 each bound buffer.
1112   Further commands are available in the GDB Threads buffer which
1113 depend on the mode of GDB that is used for controlling execution of
1114 your program.  @xref{Multithreaded Debugging}.
1116 @node Stack Buffer
1117 @subsubsection Stack Buffer
1119   The GDB Stack buffer displays a @dfn{call stack}, with one line for
1120 each of the nested subroutine calls (@dfn{stack frames}) in the
1121 debugger session.  @xref{Backtrace,, Backtraces, gdb, The GNU
1122 debugger}.
1124 @findex gdb-frames-select
1125   On graphical displays, the selected stack frame is indicated by an
1126 arrow in the fringe.  On text terminals, or when fringes are disabled,
1127 the selected stack frame is displayed in reverse contrast.  To select
1128 a stack frame, move point in its line and type @key{RET}
1129 (@code{gdb-frames-select}), or click @kbd{mouse-2} on it.  Doing so
1130 also updates the Locals buffer
1131 @ifnottex
1132 (@pxref{Other GDB Buffers}).
1133 @end ifnottex
1134 @iftex
1135 (described in the next section).
1136 @end iftex
1138 @node Other GDB Buffers
1139 @subsubsection Other GDB Buffers
1141 @table @asis
1142 @item Locals Buffer
1143 This buffer displays the values of local variables of the current
1144 frame for simple data types (@pxref{Frame Info, Frame Info,
1145 Information on a frame, gdb, The GNU debugger}).  Press @key{RET} or
1146 click @kbd{mouse-2} on the value if you want to edit it.
1148 Arrays and structures display their type only.  With GDB 6.4 or later,
1149 you can examine the value of the local variable at point by typing
1150 @key{RET}, or with a @kbd{mouse-2} click.  With earlier versions of
1151 GDB, use @key{RET} or @kbd{mouse-2} on the type description
1152 (@samp{[struct/union]} or @samp{[array]}).  @xref{Watch Expressions}.
1154 @item Registers Buffer
1155 @findex toggle-gdb-all-registers
1156 This buffer displays the values held by the registers
1157 (@pxref{Registers,,, gdb, The GNU debugger}).  Press @key{RET} or
1158 click @kbd{mouse-2} on a register if you want to edit its value.  With
1159 GDB 6.4 or later, recently changed register values display with
1160 @code{font-lock-warning-face}.
1162 @item Assembler Buffer
1163 The assembler buffer displays the current frame as machine code.  An
1164 arrow points to the current instruction, and you can set and remove
1165 breakpoints as in a source buffer.  Breakpoint icons also appear in
1166 the fringe or margin.
1168 @item Memory Buffer
1169 The memory buffer lets you examine sections of program memory
1170 (@pxref{Memory, Memory, Examining memory, gdb, The GNU debugger}).
1171 Click @kbd{mouse-1} on the appropriate part of the header line to
1172 change the starting address or number of data items that the buffer
1173 displays.  Alternatively, use @kbd{S} or @kbd{N} respectively.  Click
1174 @kbd{mouse-3} on the header line to select the display format or unit
1175 size for these data items.
1176 @end table
1178 When @code{gdb-many-windows} is non-@code{nil}, the locals buffer
1179 shares its window with the registers buffer, just like breakpoints and
1180 threads buffers.  To switch from one to the other, click with
1181 @kbd{mouse-1} on the relevant button in the header line.
1183 @node Watch Expressions
1184 @subsubsection Watch Expressions
1185 @cindex Watching expressions in GDB
1187 @findex gud-watch
1188 @kindex C-x C-a C-w @r{(GUD)}
1189   If you want to see how a variable changes each time your program
1190 stops, move point into the variable name and click on the watch icon
1191 in the tool bar (@code{gud-watch}) or type @kbd{C-x C-a C-w}.  If you
1192 specify a prefix argument, you can enter the variable name in the
1193 minibuffer.
1195   Each watch expression is displayed in the speedbar
1196 (@pxref{Speedbar}).  Complex data types, such as arrays, structures
1197 and unions are represented in a tree format.  Leaves and simple data
1198 types show the name of the expression and its value and, when the
1199 speedbar frame is selected, display the type as a tooltip.  Higher
1200 levels show the name, type and address value for pointers and just the
1201 name and type otherwise.  Root expressions also display the frame
1202 address as a tooltip to help identify the frame in which they were
1203 defined.
1205   To expand or contract a complex data type, click @kbd{mouse-2} or
1206 press @key{SPC} on the tag to the left of the expression.  Emacs asks
1207 for confirmation before expanding the expression if its number of
1208 immediate children exceeds the value of the variable
1209 @code{gdb-max-children}.
1211 @kindex D @r{(GDB speedbar)}
1212 @findex gdb-var-delete
1213   To delete a complex watch expression, move point to the root
1214 expression in the speedbar and type @kbd{D} (@code{gdb-var-delete}).
1216 @kindex RET @r{(GDB speedbar)}
1217 @findex gdb-edit-value
1218   To edit a variable with a simple data type, or a simple element of a
1219 complex data type, move point there in the speedbar and type @key{RET}
1220 (@code{gdb-edit-value}).  Or you can click @kbd{mouse-2} on a value to
1221 edit it.  Either way, this reads the new value using the minibuffer.
1223 @vindex gdb-show-changed-values
1224   If you set the variable @code{gdb-show-changed-values} to
1225 non-@code{nil} (the default value), Emacs uses
1226 @code{font-lock-warning-face} to highlight values that have recently
1227 changed and @code{shadow} face to make variables which have gone out of
1228 scope less noticeable.  When a variable goes out of scope you can't
1229 edit its value.
1231 @vindex gdb-delete-out-of-scope
1232   If the variable @code{gdb-delete-out-of-scope} is non-@code{nil}
1233 (the default value), Emacs automatically deletes watch expressions
1234 which go out of scope.  Sometimes, when your program re-enters the
1235 same function many times, it may be useful to set this value to
1236 @code{nil} so that you don't need to recreate the watch expression.
1238 @vindex gdb-use-colon-colon-notation
1239   If the variable @code{gdb-use-colon-colon-notation} is
1240 non-@code{nil}, Emacs uses the @samp{@var{function}::@var{variable}}
1241 format.  This allows the user to display watch expressions which share
1242 the same variable name.  The default value is @code{nil}.
1244 @vindex gdb-speedbar-auto-raise
1245 To automatically raise the speedbar every time the display of watch
1246 expressions updates, set @code{gdb-speedbar-auto-raise} to
1247 non-@code{nil}.  This can be useful if you are debugging with a full
1248 screen Emacs frame.
1250 @node Multithreaded Debugging
1251 @subsubsection Multithreaded Debugging
1252 @cindex Multithreaded debugging in GDB
1253 @cindex Non-stop debugging in GDB
1255   In GDB's @dfn{all-stop mode}, whenever your program stops, all
1256 execution threads stop.  Likewise, whenever you restart the program,
1257 all threads start executing.  @xref{All-Stop Mode, , All-Stop Mode,
1258 gdb, The GNU debugger}.  For some multi-threaded targets, GDB supports
1259 a further mode of operation, called @dfn{non-stop mode}, in which you
1260 can examine stopped program threads in the debugger while other
1261 threads continue to execute freely.  @xref{Non-Stop Mode, , Non-Stop
1262 Mode, gdb, The GNU debugger}.  Versions of GDB prior to 7.0 do not
1263 support non-stop mode, and it does not work on all targets.
1265 @vindex gdb-non-stop-setting
1266   The variable @code{gdb-non-stop-setting} determines whether Emacs
1267 runs GDB in all-stop mode or non-stop mode.  The default is @code{t},
1268 which means it tries to use non-stop mode if that is available.  If
1269 you change the value to @code{nil}, or if non-stop mode is
1270 unavailable, Emacs runs GDB in all-stop mode.  The variable takes
1271 effect when Emacs begins a debugging session; if you change its value,
1272 you should restart any active debugging session.
1274 @vindex gdb-switch-when-another-stopped
1275   When a thread stops in non-stop mode, Emacs usually switches to that
1276 thread.  If you don't want Emacs to do this switch if another stopped
1277 thread is already selected, change the variable
1278 @code{gdb-switch-when-another-stopped} to @code{nil}.
1280 @vindex gdb-switch-reasons
1281   Emacs can decide whether or not to switch to the stopped thread
1282 depending on the reason which caused the stop.  Customize the variable
1283 @code{gdb-switch-reasons} to select the stop reasons which will cause
1284 a thread switch.
1286 @vindex gdb-stopped-functions
1287   The variable @code{gdb-stopped-functions} allows you to execute your
1288 functions whenever some thread stops.
1290   In non-stop mode, you can switch between different modes for GUD
1291 execution control commands.
1293 @vindex gdb-gud-control-all-threads
1294 @table @dfn
1295 @item Non-stop/A
1297   When @code{gdb-gud-control-all-threads} is @code{t} (the default
1298 value), interruption and continuation commands apply to all threads,
1299 so you can halt or continue all your threads with one command using
1300 @code{gud-stop-subjob} and @code{gud-cont}, respectively.  The
1301 @samp{Go} button is shown on the tool bar when at least one thread is
1302 stopped, whereas @samp{Stop} button is shown when at least one thread
1303 is running.
1305 @item Non-stop/T
1307 When @code{gdb-gud-control-all-threads} is @code{nil}, only the
1308 current thread is stopped/continued.  @samp{Go} and @samp{Stop}
1309 buttons on the GUD tool bar are shown depending on the state of
1310 current thread.
1311 @end table
1313 You can change the current value of @code{gdb-gud-control-all-threads}
1314 from the tool bar or from @samp{GUD->GDB-MI} menu.
1316   Stepping commands always apply to the current thread.
1318   In non-stop mode, you can interrupt/continue your threads without
1319 selecting them.  Hitting @kbd{i} in threads buffer interrupts thread
1320 under point, @kbd{c} continues it, @kbd{s} steps through.  More such
1321 commands may be added in the future.
1323   Note that when you interrupt a thread, it stops with the
1324 @samp{signal received} reason.  If that reason is included in your
1325 @code{gdb-switch-reasons} (it is by default), Emacs will switch to
1326 that thread.
1328 @node Executing Lisp
1329 @section Executing Lisp Expressions
1331   Emacs has major modes for several variants of Lisp.  They use the
1332 same editing commands as other programming language modes
1333 (@pxref{Programs}).  In addition, they provide special commands for
1334 executing Lisp expressions.
1336 @table @asis
1337 @item Emacs Lisp mode
1338 The mode for editing Emacs Lisp source files.  It defines @kbd{C-M-x}
1339 to evaluate the current top-level Lisp expression.  @xref{Lisp Eval}.
1341 @item Lisp Interaction mode
1342 The mode for an interactive Emacs Lisp session.  It defines @kbd{C-j}
1343 to evaluate the expression before point and insert its value in the
1344 buffer.  @xref{Lisp Interaction}.
1346 @item Lisp mode
1347 The mode for editing source files of programs that run in Lisps other
1348 than Emacs Lisp.  It defines @kbd{C-M-x} to evaluate the current
1349 top-level expression in an external Lisp.  @xref{External Lisp}.
1351 @item Inferior Lisp mode
1352 The mode for an interactive session with an external Lisp which is
1353 being run as a subprocess (or @dfn{inferior process}) of Emacs.
1354 @ifnottex
1355 @xref{External Lisp}.
1356 @end ifnottex
1358 @item Scheme mode
1359 Like Lisp mode, but for Scheme programs.
1361 @item Inferior Scheme mode
1362 Like Inferior Lisp mode, but for Scheme.
1363 @end table
1365 @node Lisp Libraries
1366 @section Libraries of Lisp Code for Emacs
1367 @cindex libraries
1368 @cindex loading Lisp code
1370   Emacs Lisp code is stored in files whose names conventionally end in
1371 @file{.el}.  Such files are automatically visited in Emacs Lisp mode.
1373 @cindex byte code
1374   Emacs Lisp code can be compiled into byte-code, which loads faster,
1375 takes up less space, and executes faster.  By convention, compiled
1376 Emacs Lisp code goes in a separate file whose name ends in
1377 @samp{.elc}.  For example, the compiled code for @file{foo.el} goes in
1378 @file{foo.elc}.  @xref{Byte Compilation,, Byte Compilation, elisp, the
1379 Emacs Lisp Reference Manual}.
1381 @findex load-file
1382   To @dfn{load} an Emacs Lisp file, type @kbd{M-x load-file}.  This
1383 command reads a file name using the minibuffer, and executes the
1384 contents of that file as Emacs Lisp code.  It is not necessary to
1385 visit the file first; this command reads the file directly from disk,
1386 not from an existing Emacs buffer.
1388 @findex load
1389 @findex load-library
1390 @vindex load-prefer-newer
1391 @cindex load path for Emacs Lisp
1392   If an Emacs Lisp file is installed in the Emacs Lisp @dfn{load path}
1393 (defined below), you can load it by typing @kbd{M-x load-library},
1394 instead of using @kbd{M-x load-file}.  The @kbd{M-x load-library}
1395 command prompts for a @dfn{library name} rather than a file name; it
1396 searches through each directory in the Emacs Lisp load path, trying to
1397 find a file matching that library name.  If the library name is
1398 @samp{@var{foo}}, it tries looking for files named
1399 @file{@var{foo}.elc}, @file{@var{foo}.el}, and @file{@var{foo}}.  The
1400 default behavior is to load the first file found.  This command
1401 prefers @file{.elc} files over @file{.el} files because compiled files
1402 load and run faster.  If it finds that @file{@var{lib}.el} is newer
1403 than @file{@var{lib}.elc}, it issues a warning, in case someone made
1404 changes to the @file{.el} file and forgot to recompile it, but loads
1405 the @file{.elc} file anyway.  (Due to this behavior, you can save
1406 unfinished edits to Emacs Lisp source files, and not recompile until
1407 your changes are ready for use.)  If you set the option
1408 @code{load-prefer-newer} to a non-@code{nil} value, however, then
1409 rather than the procedure described above, Emacs loads whichever
1410 version of the file is newest.
1412   Emacs Lisp programs usually load Emacs Lisp files using the
1413 @code{load} function.  This is similar to @code{load-library}, but is
1414 lower-level and accepts additional arguments.  @xref{How Programs Do
1415 Loading,,, elisp, the Emacs Lisp Reference Manual}.
1417 @vindex load-path
1418   The Emacs Lisp load path is specified by the variable
1419 @code{load-path}.  Its value should be a list of directories
1420 (strings).  These directories are searched, in the specified order, by
1421 the @kbd{M-x load-library} command, the lower-level @code{load}
1422 function, and other Emacs functions that find Emacs Lisp libraries.
1423 An entry in @code{load-path} can also have the special value
1424 @code{nil}, which stands for the current default directory, but it is
1425 almost always a bad idea to use this, because its meaning will depend
1426 on the buffer that is current when @code{load-path} is used by Emacs.
1427 (If you find yourself wishing that @code{nil} were in the list, most
1428 likely what you really want is to use @kbd{M-x load-file}.)
1430   The default value of @code{load-path} is a list of directories where
1431 the Lisp code for Emacs itself is stored.  If you have libraries of
1432 your own in another directory, you can add that directory to the load
1433 path.  Unlike most other variables described in this manual,
1434 @code{load-path} cannot be changed via the Customize interface
1435 (@pxref{Easy Customization}), but you can add a directory to it by
1436 putting a line like this in your init file (@pxref{Init File}):
1438 @example
1439 (add-to-list 'load-path "/path/to/my/lisp/library")
1440 @end example
1442 @cindex autoload
1443   Some commands are @dfn{autoloaded}; when you run them, Emacs
1444 automatically loads the associated library first.  For instance, the
1445 @kbd{M-x compile} command (@pxref{Compilation}) is autoloaded; if you
1446 call it, Emacs automatically loads the @code{compile} library first.
1447 In contrast, the command @kbd{M-x recompile} is not autoloaded, so it
1448 is unavailable until you load the @code{compile} library.
1450 @vindex help-enable-auto-load
1451   Automatic loading can also occur when you look up the documentation
1452 of an autoloaded command (@pxref{Name Help}), if the documentation
1453 refers to other functions and variables in its library (loading the
1454 library lets Emacs properly set up the hyperlinks in the @file{*Help*}
1455 buffer).  To disable this feature, change the variable
1456 @code{help-enable-auto-load} to @code{nil}.
1458 @vindex load-dangerous-libraries
1459 @cindex Lisp files byte-compiled by XEmacs
1460   By default, Emacs refuses to load compiled Lisp files which were
1461 compiled with XEmacs, a modified version of Emacs---they can cause
1462 Emacs to crash.  Set the variable @code{load-dangerous-libraries} to
1463 @code{t} if you want to try loading them.
1465 @node Lisp Eval
1466 @section Evaluating Emacs Lisp Expressions
1467 @cindex Emacs Lisp mode
1468 @cindex mode, Emacs Lisp
1469 @cindex evaluation, Emacs Lisp
1471 @findex emacs-lisp-mode
1472   Emacs Lisp mode is the major mode for editing Emacs Lisp.  Its mode
1473 command is @kbd{M-x emacs-lisp-mode}.
1475   Emacs provides several commands for evaluating Emacs Lisp
1476 expressions.  You can use these commands in Emacs Lisp mode, to test
1477 your Emacs Lisp code as it is being written.  For example, after
1478 re-writing a function, you can evaluate the function definition to
1479 make it take effect for subsequent function calls.  These commands are
1480 also available globally, and can be used outside Emacs Lisp mode.
1482 @table @asis
1483 @item @kbd{M-:}
1484 Read a single Emacs Lisp expression in the minibuffer, evaluate it,
1485 and print the value in the echo area (@code{eval-expression}).
1486 @item @kbd{C-x C-e}
1487 Evaluate the Emacs Lisp expression before point, and print the value
1488 in the echo area (@code{eval-last-sexp}).
1489 @item @kbd{C-M-x} @r{(in Emacs Lisp mode)}
1490 @itemx @kbd{M-x eval-defun}
1491 Evaluate the defun containing or after point, and print the value in
1492 the echo area (@code{eval-defun}).
1493 @item @kbd{M-x eval-region}
1494 Evaluate all the Emacs Lisp expressions in the region.
1495 @item @kbd{M-x eval-buffer}
1496 Evaluate all the Emacs Lisp expressions in the buffer.
1497 @end table
1499 @ifinfo
1500 @c This uses 'colon' instead of a literal ':' because Info cannot
1501 @c cope with a ':' in a menu.
1502 @kindex M-@key{colon}
1503 @end ifinfo
1504 @ifnotinfo
1505 @kindex M-:
1506 @end ifnotinfo
1507 @findex eval-expression
1508   @kbd{M-:} (@code{eval-expression}) reads an expression using the
1509 minibuffer, and evaluates it.  (Before evaluating the expression, the
1510 current buffer switches back to the buffer that was current when you
1511 typed @kbd{M-:}, not the minibuffer into which you typed the
1512 expression.)
1514 @kindex C-x C-e
1515 @findex eval-last-sexp
1516   The command @kbd{C-x C-e} (@code{eval-last-sexp}) evaluates the
1517 Emacs Lisp expression preceding point in the buffer, and displays the
1518 value in the echo area.  When the result of an evaluation is an
1519 integer, it is displayed together with the value in other formats
1520 (octal, hexadecimal, and character if
1521 @code{eval-expression-print-maximum-character}, described below,
1522 allows it).
1524   If @kbd{M-:} or @kbd{C-x C-e} is given a prefix argument, it inserts
1525 the value into the current buffer at point, rather than displaying it
1526 in the echo area.  If the prefix argument is zero, any integer output
1527 is inserted together with its value in other formats (octal,
1528 hexadecimal, and character).  Such a prefix argument also prevents
1529 abbreviation of the output according to the variables
1530 @code{eval-expression-print-level} and
1531 @code{eval-expression-print-length} (see below).  Similarly, a prefix
1532 argument of @code{-1} overrides the effect of
1533 @code{eval-expression-print-length}.
1535 @kindex C-M-x @r{(Emacs Lisp mode)}
1536 @findex eval-defun
1537   The @code{eval-defun} command is bound to @kbd{C-M-x} in Emacs Lisp
1538 mode.  It evaluates the top-level Lisp expression containing or
1539 following point, and prints the value in the echo area.  In this
1540 context, a top-level expression is referred to as a ``defun'', but it
1541 need not be an actual @code{defun} (function definition).  In
1542 particular, this command treats @code{defvar} expressions specially.
1543 Normally, evaluating a @code{defvar} expression does nothing if the
1544 variable it defines already has a value.  But this command
1545 unconditionally resets the variable to the initial value specified by
1546 the @code{defvar}; this is convenient for debugging Emacs Lisp
1547 programs.  @code{defcustom} and @code{defface} expressions are treated
1548 similarly.  Note that the other commands documented in this section do
1549 not have this special feature.
1551   With a prefix argument, @kbd{C-M-x} instruments the function
1552 definition for Edebug, the Emacs Lisp Debugger.  @xref{Instrumenting,
1553 Instrumenting for Edebug,, elisp, the Emacs Lisp Reference Manual}.
1555 @findex eval-region
1556 @findex eval-buffer
1557   The command @kbd{M-x eval-region} parses the text of the region as
1558 one or more Lisp expressions, evaluating them one by one.  @kbd{M-x
1559 eval-buffer} is similar but evaluates the entire buffer.
1561 @vindex eval-expression-print-level
1562 @vindex eval-expression-print-length
1563 @vindex eval-expression-print-maximum-character
1564 @vindex eval-expression-debug-on-error
1565   The options @code{eval-expression-print-level} and
1566 @code{eval-expression-print-length} control the maximum depth and
1567 length of lists to print in the result of the evaluation commands
1568 before abbreviating them.  Supplying a zero prefix argument to
1569 @code{eval-expression} or @code{eval-last-sexp} causes lists to be
1570 printed in full.  @code{eval-expression-debug-on-error} controls
1571 whether evaluation errors invoke the debugger when these commands are
1572 used; its default is @code{t}.
1573 @code{eval-expression-print-maximum-character} prevents integers which
1574 are larger than it from being displayed as characters.
1576 @node Lisp Interaction
1577 @section Lisp Interaction Buffers
1579 @findex lisp-interaction-mode
1580   When Emacs starts up, it contains a buffer named @file{*scratch*},
1581 which is provided for evaluating Emacs Lisp expressions interactively.
1582 Its major mode is Lisp Interaction mode.  You can also enable Lisp
1583 Interaction mode by typing @kbd{M-x lisp-interaction-mode}.
1585 @findex eval-print-last-sexp
1586 @kindex C-j @r{(Lisp Interaction mode)}
1587   In the @file{*scratch*} buffer, and other Lisp Interaction mode
1588 buffers, @kbd{C-j} (@code{eval-print-last-sexp}) evaluates the Lisp
1589 expression before point, and inserts the value at point.  Thus, as you
1590 type expressions into the buffer followed by @kbd{C-j} after each
1591 expression, the buffer records a transcript of the evaluated
1592 expressions and their values.  All other commands in Lisp Interaction
1593 mode are the same as in Emacs Lisp mode.
1595 @vindex initial-scratch-message
1596   At startup, the @file{*scratch*} buffer contains a short message, in
1597 the form of a Lisp comment, that explains what it is for.  This
1598 message is controlled by the variable @code{initial-scratch-message},
1599 which should be either a documentation string, or @code{nil} (which means to
1600 suppress the message).
1602 @findex ielm
1603   An alternative way of evaluating Emacs Lisp expressions
1604 interactively is to use Inferior Emacs Lisp mode, which provides an
1605 interface rather like Shell mode (@pxref{Shell Mode}) for evaluating
1606 Emacs Lisp expressions.  Type @kbd{M-x ielm} to create an
1607 @file{*ielm*} buffer which uses this mode.  For more information, see
1608 that command's documentation.
1610 @node External Lisp
1611 @section Running an External Lisp
1612 @cindex Lisp mode
1613 @cindex mode, Lisp
1614 @cindex Common Lisp
1616   Lisp mode is the major mode for editing programs written in
1617 general-purpose Lisp dialects, such as Common Lisp.  Its mode command
1618 is @kbd{M-x lisp-mode}.  Emacs uses Lisp mode automatically for files
1619 whose names end in @file{.l}, @file{.lsp}, or @file{.lisp}.
1621 @findex run-lisp
1622 @vindex inferior-lisp-program
1623 @kindex C-x C-z
1624   You can run an external Lisp session as a subprocess or
1625 @dfn{inferior process} of Emacs, and pass expressions to it to be
1626 evaluated.  To begin an external Lisp session, type @kbd{M-x
1627 run-lisp}.  This runs the program named @command{lisp}, and sets it up
1628 so that both input and output go through an Emacs buffer named
1629 @file{*inferior-lisp*}.  To change the name of the Lisp program run by
1630 @kbd{M-x run-lisp}, change the variable @code{inferior-lisp-program}.
1632   The major mode for the @file{*lisp*} buffer is Inferior Lisp mode,
1633 which combines the characteristics of Lisp mode and Shell mode
1634 (@pxref{Shell Mode}).  To send input to the Lisp session, go to the
1635 end of the @file{*lisp*} buffer and type the input, followed by
1636 @key{RET}.  Terminal output from the Lisp session is automatically
1637 inserted in the buffer.
1639 @kindex C-M-x @r{(Lisp mode)}
1640 @findex lisp-eval-defun
1641   When you edit a Lisp program in Lisp mode, you can type @kbd{C-M-x}
1642 (@code{lisp-eval-defun}) to send an expression from the Lisp mode
1643 buffer to a Lisp session that you had started with @kbd{M-x run-lisp}.
1644 The expression sent is the top-level Lisp expression at or following
1645 point.  The resulting value goes as usual into the
1646 @file{*inferior-lisp*} buffer.  Note that the effect of @kbd{C-M-x} in
1647 Lisp mode is thus very similar to its effect in Emacs Lisp mode
1648 (@pxref{Lisp Eval}), except that the expression is sent to a different
1649 Lisp environment instead of being evaluated in Emacs.
1651 @findex scheme-mode
1652 @findex run-scheme
1653 @cindex Scheme mode
1654 @cindex mode, Scheme
1655 @kindex C-M-x @r{(Scheme mode)}
1656   The facilities for editing Scheme code, and for sending expressions
1657 to a Scheme subprocess, are very similar.  Scheme source files are
1658 edited in Scheme mode, which can be explicitly enabled with @kbd{M-x
1659 scheme-mode}.  You can initiate a Scheme session by typing @kbd{M-x
1660 run-scheme} (the buffer for interacting with Scheme is named
1661 @file{*scheme*}), and send expressions to it by typing @kbd{C-M-x}.