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