Initial revision
[deliverable/binutils-gdb.git] / gdb / doc / gdb.texinfo
1 \input texinfo
2 @setfilename gdb.info
3 @settitle GDB, The GNU Debugger
4 @synindex ky cp
5 @ifinfo
6 This file documents the GNU debugger GDB.
7
8 Copyright (C) 1988, 1989 Free Software Foundation, Inc.
9
10 Permission is granted to make and distribute verbatim copies of
11 this manual provided the copyright notice and this permission notice
12 are preserved on all copies.
13
14 @ignore
15 Permission is granted to process this file through Tex and print the
16 results, provided the printed document carries copying permission
17 notice identical to this one except for the removal of this paragraph
18 (this paragraph not being relevant to the printed manual).
19
20 @end ignore
21 Permission is granted to copy and distribute modified versions of this
22 manual under the conditions for verbatim copying, provided also that the
23 section entitled ``GNU General Public License'' is included exactly as
24 in the original, and provided that the entire resulting derived work is
25 distributed under the terms of a permission notice identical to this
26 one.
27
28 Permission is granted to copy and distribute translations of this manual
29 into another language, under the above conditions for modified versions,
30 except that the section entitled ``GNU General Public License'' may be
31 included in a translation approved by the author instead of in the
32 original English.
33 @end ifinfo
34
35 @setchapternewpage odd
36 @settitle GDB Manual
37 @titlepage
38 @sp 6
39 @center @titlefont{GDB Manual}
40 @sp 1
41 @center The GNU Source-Level Debugger
42 @sp 4
43 @center Third Edition, GDB version 3.4
44 @sp 1
45 @center October 1989
46 @sp 5
47 @center Richard M. Stallman
48 @page
49 @vskip 0pt plus 1filll
50 Copyright @copyright{} 1988, 1989 Free Software Foundation, Inc.
51
52 Permission is granted to make and distribute verbatim copies of
53 this manual provided the copyright notice and this permission notice
54 are preserved on all copies.
55
56 Permission is granted to copy and distribute modified versions of this
57 manual under the conditions for verbatim copying, provided also that the
58 section entitled ``GNU General Public License'' is included exactly as
59 in the original, and provided that the entire resulting derived work is
60 distributed under the terms of a permission notice identical to this
61 one.
62
63 Permission is granted to copy and distribute translations of this manual
64 into another language, under the above conditions for modified versions,
65 except that the section entitled ``GNU General Public License'' may be
66 included in a translation approved by the author instead of in the
67 original English.
68 @end titlepage
69 @page
70
71 @node Top, Top, Top, (DIR)
72 @unnumbered Summary of GDB
73
74 The purpose of a debugger such as GDB is to allow you to execute another
75 program while examining what is going on inside it. We call the other
76 program ``your program'' or ``the program being debugged''.
77
78 GDB can do four kinds of things (plus other things in support of these):
79
80 @enumerate
81 @item
82 Start the program, specifying anything that might affect its behavior.
83
84 @item
85 Make the program stop on specified conditions.
86
87 @item
88 Examine what has happened, when the program has stopped, so that you
89 can see bugs happen.
90
91 @item
92 Change things in the program, so you can correct the effects of one bug
93 and go on to learn about another without having to recompile first.
94 @end enumerate
95
96 GDB can be used to debug programs written in C and C++. Pascal support
97 is being implemented, and Fortran support will be added when a GNU
98 Fortran compiler is written.
99
100 @menu
101 * License:: The GNU General Public License gives you permission
102 to redistribute GDB on certain terms; and also
103 explains that there is no warranty.
104 * User Interface:: GDB command syntax and input and output conventions.
105 * Files:: Specifying files for GDB to operate on.
106 * Options:: GDB arguments and options.
107 * Compilation::Compiling your program so you can debug it.
108 * Running:: Running your program under GDB.
109 * Stopping:: Making your program stop. Why it may stop. What to do then.
110 * Stack:: Examining your program's stack.
111 * Source:: Examining your program's source files.
112 * Data:: Examining data in your program.
113 * Symbols:: Examining the debugger's symbol table.
114 * Altering:: Altering things in your program.
115 * Sequences:: Canned command sequences for repeated use.
116 * Emacs:: Using GDB through GNU Emacs.
117 * Remote:: Remote kernel debugging across a serial line.
118 * Commands:: Index of GDB commands.
119 * Concepts:: Index of GDB concepts.
120 @end menu
121
122 @node License, User Interface, Top, Top
123 @unnumbered GNU GENERAL PUBLIC LICENSE
124 @center Version 1, February 1989
125
126 @display
127 Copyright @copyright{} 1989 Free Software Foundation, Inc.
128 675 Mass Ave, Cambridge, MA 02139, USA
129
130 Everyone is permitted to copy and distribute verbatim copies
131 of this license document, but changing it is not allowed.
132 @end display
133
134 @unnumberedsec Preamble
135
136 The license agreements of most software companies try to keep users
137 at the mercy of those companies. By contrast, our General Public
138 License is intended to guarantee your freedom to share and change free
139 software---to make sure the software is free for all its users. The
140 General Public License applies to the Free Software Foundation's
141 software and to any other program whose authors commit to using it.
142 You can use it for your programs, too.
143
144 When we speak of free software, we are referring to freedom, not
145 price. Specifically, the General Public License is designed to make
146 sure that you have the freedom to give away or sell copies of free
147 software, that you receive source code or can get it if you want it,
148 that you can change the software or use pieces of it in new free
149 programs; and that you know you can do these things.
150
151 To protect your rights, we need to make restrictions that forbid
152 anyone to deny you these rights or to ask you to surrender the rights.
153 These restrictions translate to certain responsibilities for you if you
154 distribute copies of the software, or if you modify it.
155
156 For example, if you distribute copies of a such a program, whether
157 gratis or for a fee, you must give the recipients all the rights that
158 you have. You must make sure that they, too, receive or can get the
159 source code. And you must tell them their rights.
160
161 We protect your rights with two steps: (1) copyright the software, and
162 (2) offer you this license which gives you legal permission to copy,
163 distribute and/or modify the software.
164
165 Also, for each author's protection and ours, we want to make certain
166 that everyone understands that there is no warranty for this free
167 software. If the software is modified by someone else and passed on, we
168 want its recipients to know that what they have is not the original, so
169 that any problems introduced by others will not reflect on the original
170 authors' reputations.
171
172 The precise terms and conditions for copying, distribution and
173 modification follow.
174
175 @iftex
176 @unnumberedsec TERMS AND CONDITIONS
177 @end iftex
178 @ifinfo
179 @center TERMS AND CONDITIONS
180 @end ifinfo
181
182 @enumerate
183 @item
184 This License Agreement applies to any program or other work which
185 contains a notice placed by the copyright holder saying it may be
186 distributed under the terms of this General Public License. The
187 ``Program'', below, refers to any such program or work, and a ``work based
188 on the Program'' means either the Program or any work containing the
189 Program or a portion of it, either verbatim or with modifications. Each
190 licensee is addressed as ``you''.
191
192 @item
193 You may copy and distribute verbatim copies of the Program's source
194 code as you receive it, in any medium, provided that you conspicuously and
195 appropriately publish on each copy an appropriate copyright notice and
196 disclaimer of warranty; keep intact all the notices that refer to this
197 General Public License and to the absence of any warranty; and give any
198 other recipients of the Program a copy of this General Public License
199 along with the Program. You may charge a fee for the physical act of
200 transferring a copy.
201
202 @item
203 You may modify your copy or copies of the Program or any portion of
204 it, and copy and distribute such modifications under the terms of Paragraph
205 1 above, provided that you also do the following:
206
207 @itemize @bullet
208 @item
209 cause the modified files to carry prominent notices stating that
210 you changed the files and the date of any change; and
211
212 @item
213 cause the whole of any work that you distribute or publish, that
214 in whole or in part contains the Program or any part thereof, either
215 with or without modifications, to be licensed at no charge to all
216 third parties under the terms of this General Public License (except
217 that you may choose to grant warranty protection to some or all
218 third parties, at your option).
219
220 @item
221 If the modified program normally reads commands interactively when
222 run, you must cause it, when started running for such interactive use
223 in the simplest and most usual way, to print or display an
224 announcement including an appropriate copyright notice and a notice
225 that there is no warranty (or else, saying that you provide a
226 warranty) and that users may redistribute the program under these
227 conditions, and telling the user how to view a copy of this General
228 Public License.
229
230 @item
231 You may charge a fee for the physical act of transferring a
232 copy, and you may at your option offer warranty protection in
233 exchange for a fee.
234 @end itemize
235
236 Mere aggregation of another independent work with the Program (or its
237 derivative) on a volume of a storage or distribution medium does not bring
238 the other work under the scope of these terms.
239
240 @item
241 You may copy and distribute the Program (or a portion or derivative of
242 it, under Paragraph 2) in object code or executable form under the terms of
243 Paragraphs 1 and 2 above provided that you also do one of the following:
244
245 @itemize @bullet
246 @item
247 accompany it with the complete corresponding machine-readable
248 source code, which must be distributed under the terms of
249 Paragraphs 1 and 2 above; or,
250
251 @item
252 accompany it with a written offer, valid for at least three
253 years, to give any third party free (except for a nominal charge
254 for the cost of distribution) a complete machine-readable copy of the
255 corresponding source code, to be distributed under the terms of
256 Paragraphs 1 and 2 above; or,
257
258 @item
259 accompany it with the information you received as to where the
260 corresponding source code may be obtained. (This alternative is
261 allowed only for noncommercial distribution and only if you
262 received the program in object code or executable form alone.)
263 @end itemize
264
265 Source code for a work means the preferred form of the work for making
266 modifications to it. For an executable file, complete source code means
267 all the source code for all modules it contains; but, as a special
268 exception, it need not include source code for modules which are standard
269 libraries that accompany the operating system on which the executable
270 file runs, or for standard header files or definitions files that
271 accompany that operating system.
272
273 @item
274 You may not copy, modify, sublicense, distribute or transfer the
275 Program except as expressly provided under this General Public License.
276 Any attempt otherwise to copy, modify, sublicense, distribute or transfer
277 the Program is void, and will automatically terminate your rights to use
278 the Program under this License. However, parties who have received
279 copies, or rights to use copies, from you under this General Public
280 License will not have their licenses terminated so long as such parties
281 remain in full compliance.
282
283 @item
284 By copying, distributing or modifying the Program (or any work based
285 on the Program) you indicate your acceptance of this license to do so,
286 and all its terms and conditions.
287
288 @item
289 Each time you redistribute the Program (or any work based on the
290 Program), the recipient automatically receives a license from the original
291 licensor to copy, distribute or modify the Program subject to these
292 terms and conditions. You may not impose any further restrictions on the
293 recipients' exercise of the rights granted herein.
294
295 @item
296 The Free Software Foundation may publish revised and/or new versions
297 of the General Public License from time to time. Such new versions will
298 be similar in spirit to the present version, but may differ in detail to
299 address new problems or concerns.
300
301 Each version is given a distinguishing version number. If the Program
302 specifies a version number of the license which applies to it and ``any
303 later version'', you have the option of following the terms and conditions
304 either of that version or of any later version published by the Free
305 Software Foundation. If the Program does not specify a version number of
306 the license, you may choose any version ever published by the Free Software
307 Foundation.
308
309 @item
310 If you wish to incorporate parts of the Program into other free
311 programs whose distribution conditions are different, write to the author
312 to ask for permission. For software which is copyrighted by the Free
313 Software Foundation, write to the Free Software Foundation; we sometimes
314 make exceptions for this. Our decision will be guided by the two goals
315 of preserving the free status of all derivatives of our free software and
316 of promoting the sharing and reuse of software generally.
317
318 @iftex
319 @heading NO WARRANTY
320 @end iftex
321 @ifinfo
322 @center NO WARRANTY
323 @end ifinfo
324
325 @item
326 BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
327 FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
328 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
329 PROVIDE THE PROGRAM ``AS IS'' WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
330 OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
331 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
332 TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
333 PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
334 REPAIR OR CORRECTION.
335
336 @item
337 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL
338 ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
339 REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
340 INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES
341 ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT
342 LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES
343 SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE
344 WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN
345 ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
346 @end enumerate
347
348 @iftex
349 @heading END OF TERMS AND CONDITIONS
350 @end iftex
351 @ifinfo
352 @center END OF TERMS AND CONDITIONS
353 @end ifinfo
354
355 @page
356 @unnumberedsec Appendix: How to Apply These Terms to Your New Programs
357
358 If you develop a new program, and you want it to be of the greatest
359 possible use to humanity, the best way to achieve this is to make it
360 free software which everyone can redistribute and change under these
361 terms.
362
363 To do so, attach the following notices to the program. It is safest to
364 attach them to the start of each source file to most effectively convey
365 the exclusion of warranty; and each file should have at least the
366 ``copyright'' line and a pointer to where the full notice is found.
367
368 @smallexample
369 @var{one line to give the program's name and a brief idea of what it does.}
370 Copyright (C) 19@var{yy} @var{name of author}
371
372 This program is free software; you can redistribute it and/or modify
373 it under the terms of the GNU General Public License as published by
374 the Free Software Foundation; either version 1, or (at your option)
375 any later version.
376
377 This program is distributed in the hope that it will be useful,
378 but WITHOUT ANY WARRANTY; without even the implied warranty of
379 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
380 GNU General Public License for more details.
381
382 You should have received a copy of the GNU General Public License
383 along with this program; if not, write to the Free Software
384 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
385 @end smallexample
386
387 Also add information on how to contact you by electronic and paper mail.
388
389 If the program is interactive, make it output a short notice like this
390 when it starts in an interactive mode:
391
392 @smallexample
393 Gnomovision version 69, Copyright (C) 19@var{yy} @var{name of author}
394 Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
395 This is free software, and you are welcome to redistribute it
396 under certain conditions; type `show c' for details.
397 @end smallexample
398
399 The hypothetical commands `show w' and `show c' should show the
400 appropriate parts of the General Public License. Of course, the
401 commands you use may be called something other than `show w' and `show
402 c'; they could even be mouse-clicks or menu items---whatever suits your
403 program.
404
405 You should also get your employer (if you work as a programmer) or your
406 school, if any, to sign a ``copyright disclaimer'' for the program, if
407 necessary. Here a sample; alter the names:
408
409 @example
410 Yoyodyne, Inc., hereby disclaims all copyright interest in the
411 program `Gnomovision' (a program to direct compilers to make passes
412 at assemblers) written by James Hacker.
413
414 @var{signature of Ty Coon}, 1 April 1989
415 Ty Coon, President of Vice
416 @end example
417
418 That's all there is to it!
419
420 @node User Interface, Files, License, Top
421 @chapter GDB Input and Output Conventions
422
423 GDB is invoked with the shell command @samp{gdb}. Once started, it reads
424 commands from the terminal until you tell it to exit.
425
426 A GDB command is a single line of input. There is no limit on how long
427 it can be. It starts with a command name, which is followed by arguments
428 whose meaning depends on the command name. For example, the command
429 @samp{step} accepts an argument which is the number of times to step,
430 as in @samp{step 5}. You can also use the @samp{step} command with
431 no arguments. Some command names do not allow any arguments.
432
433 @cindex abbreviation
434 GDB command names may always be abbreviated if the abbreviation is
435 unambiguous. Sometimes even ambiguous abbreviations are allowed; for
436 example, @samp{s} is specially defined as equivalent to @samp{step}
437 even though there are other commands whose names start with @samp{s}.
438 Possible command abbreviations are often stated in the documentation
439 of the individual commands.
440
441 @cindex repeating commands
442 A blank line as input to GDB means to repeat the previous command verbatim.
443 Certain commands do not allow themselves to be repeated this way; these are
444 commands for which unintentional repetition might cause trouble and which
445 you are unlikely to want to repeat. Certain others (@samp{list} and
446 @samp{x}) act differently when repeated because that is more useful.
447
448 A line of input starting with @samp{#} is a comment; it does nothing.
449 This is useful mainly in command files (@xref{Command Files}).
450
451 @cindex prompt
452 GDB indicates its readiness to read a command by printing a string
453 called the @dfn{prompt}. This string is normally @samp{(gdb)}. You can
454 change the prompt string with the @samp{set prompt} command. For
455 instance, when debugging GDB with GDB, it is useful to change the prompt
456 in one of the GDBs so that you tell which one you are talking to.
457
458 @table @code
459 @item set prompt @var{newprompt}
460 @kindex set prompt
461 Directs GDB to use @var{newprompt} as its prompt string henceforth.
462 @end table
463
464 @cindex exiting GDB
465 @kindex quit
466 To exit GDB, use the @samp{quit} command (abbreviated @samp{q}).
467 @kbd{Ctrl-c} will not exit from GDB, but rather will terminate the action
468 of any GDB command that is in progress and return to GDB command level.
469 It is safe to type @kbd{Ctrl-c} at any time because GDB does not allow
470 it to take effect until a time when it is safe.
471
472 @cindex screen size
473 @cindex pauses in output
474 Certain commands to GDB may produce large amounts of information output
475 to the screen. To help you read all of it, GDB pauses and asks you for
476 input at the end of each page of output. Type @key{RET} when you want
477 to continue the output. Normally GDB knows the size of the screen from
478 on the termcap data base together with the value of the @code{TERM}
479 environment variable; if this is not correct, you can override it with
480 the @samp{set screensize} command:
481
482 @table @code
483 @item set screensize @var{lpp}
484 @itemx set screensize @var{lpp} @var{cpl}
485 @kindex set screensize
486 Specify a screen height of @var{lpp} lines and (optionally) a width of
487 @var{cpl} characters. If you omit @var{cpl}, the width does not change.
488
489 If you specify a height of zero lines, GDB will not pause during output
490 no matter how long the output is. This is useful if output is to a file
491 or to an editor buffer.
492 @end table
493
494 Also, GDB may at times produce more information about its own workings
495 than is of interest to the user. Some of these informational messages
496 can be turned on and off with the @samp{set verbose} command:
497
498 @table @code
499 @kindex set verbose
500 @item set verbose off
501 Disables GDB's output of certain informational messages.
502
503 @item set verbose on
504 Re-enables GDB's output of certain informational messages.
505 @end table
506
507 Currently, the messages controlled by @samp{set verbose} are those which
508 announce that the symbol table for a source file is being read
509 (@pxref{File Commands}, in the description of the command
510 @samp{symbol-file}).
511 @c The following is the right way to do it, but emacs 18.55 doesn't support
512 @c @ref, and neither the emacs lisp manual version of texinfmt or makeinfo
513 @c is released.
514 @ignore
515 see @samp{symbol-file} in @ref{File Commands}).
516 @end ignore
517
518 @node Files, Compilation, User Interface, Top
519 @chapter Specifying GDB's Files
520
521 @cindex core dump file
522 @cindex executable file
523 @cindex symbol table
524 GDB needs to know the file name of the program to be debugged, both in
525 order to read its symbol table and in order to start the program. To
526 debug a core dump of a previous run, GDB must be told the file name of
527 the core dump.
528
529 @menu
530 * Arguments: File Arguments. Specifying files with arguments
531 (when you start GDB).
532 * Commands: File Commands. Specifying files with GDB commands.
533 @end menu
534
535 @node File Arguments, File Commands, Files, Files
536 @section Specifying Files with Arguments
537
538 The usual way to specify the executable and core dump file names is with
539 two command arguments given when you start GDB. The first argument is used
540 as the file for execution and symbols, and the second argument (if any) is
541 used as the core dump file name. Thus,
542
543 @example
544 gdb progm core
545 @end example
546
547 @noindent
548 specifies @file{progm} as the executable program and @file{core} as a core
549 dump file to examine. (You do not need to have a core dump file if what
550 you plan to do is debug the program interactively.)
551
552 @xref{Options}, for full information on options and arguments for
553 invoking GDB.
554
555 @node File Commands,, File Arguments, Files
556 @section Specifying Files with Commands
557
558 Usually you specify the files for GDB to work with by giving arguments when
559 you invoke GDB. But occasionally it is necessary to change to a different
560 file during a GDB session. Or you may run GDB and forget to specify the
561 files you want to use. In these situations the GDB commands to specify new
562 files are useful.
563
564 @table @code
565 @item exec-file @var{filename}
566 @kindex exec-file
567 Specify that the program to be run is found in @var{filename}. If you
568 do not specify a directory and the file is not found in GDB's working
569 directory, GDB will use the environment variable @code{PATH} as a list
570 of directories to search, just as the shell does when looking for a
571 program to run.
572
573 @item symbol-file @var{filename}
574 @kindex symbol-file
575 Read symbol table information from file @var{filename}. @code{PATH}
576 is searched when necessary. Most of the time you will use both the
577 @samp{exec-file} and @samp{symbol-file} commands on the same file.
578
579 @samp{symbol-file} with no argument clears out GDB's symbol table.
580
581 The @samp{symbol-file} command does not actually read the symbol table in
582 full right away. Instead, it scans the symbol table quickly to find
583 which source files and which symbols are present. The details are read
584 later, one source file at a time, when they are needed.
585
586 The purpose of this two-stage reading strategy is to make GDB start up
587 faster. For the most part, it is invisible except for occasional
588 messages telling you that the symbol table details for a particular
589 source file are being read. (The @samp{set verbose} command controls
590 whether these messages are printed; @pxref{User Interface}).
591
592 However, you will sometimes see in backtraces lines for functions in
593 source files whose data has not been read in; these lines omit some of
594 the information, such as argument values, which cannot be printed
595 without full details of the symbol table.
596
597 When the symbol table is stored in COFF format, @samp{symbol-file} does
598 read the symbol table data in full right away. We haven't bothered to
599 implement the two-stage strategy for COFF.
600
601 @item core-file @var{filename}
602 @kindex core-file
603 Specify the whereabouts of a core dump file to be used as the
604 ``contents of memory''. Note that the core dump contains only the
605 writable parts of memory; the read-only parts must come from the
606 executable file.
607
608 @samp{core-file} with no argument specifies that no core file is
609 to be used.
610
611 Note that the core file is ignored when your program is actually running
612 under GDB. So, if you have been running the program and you wish to
613 debug a core file instead, you must kill the subprocess in which the
614 program is running. To do this, use the @samp{kill} command
615 (@pxref{Kill Process}).
616
617 @item add-file @var{filename} @var{address}
618 @kindex add-file
619 @cindex dynamic linking
620 The @samp{add-file} command reads additional symbol table information
621 from the file @var{filename}. You would use this when that file has
622 been dynamically loaded into the program that is running. @var{address}
623 should be the memory address at which the file has been loaded; GDB cannot
624 figure this out for itself.
625
626 The symbol table of the file @var{filename} is added to the symbol table
627 originally read with the @samp{symbol-file} command. You can use the
628 @samp{add-file} command any number of times; the new symbol data thus
629 read keeps adding to the old. The @samp{symbol-file} command forgets
630 all the symbol data GDB has read; that is the only time symbol data is
631 forgotten in GDB.
632
633 @item info files
634 @kindex info files
635 Print the names of the executable and core dump files currently in
636 use by GDB, and the file from which symbols were loaded.
637 @end table
638
639 While all three file-specifying commands allow both absolute and relative
640 file names as arguments, GDB always converts the file name to an absolute
641 one and remembers it that way.
642
643 The @samp{symbol-file} command causes GDB to forget the contents of its
644 convenience variables, the value history, and all breakpoints and
645 auto-display expressions. This is because they may contain pointers to the
646 internal data recording symbols and data types, which are part of the old
647 symbol table data being discarded inside GDB.
648
649 @node Compilation, Running, Files, Top
650 @chapter Compiling Your Program for Debugging
651
652 In order to debug a program effectively, you need to ask for debugging
653 information when you compile it. This information in the object file
654 describes the data type of each variable or function and the correspondence
655 between source line numbers and addresses in the executable code.
656
657 To request debugging information, specify the @samp{-g} option when you run
658 the compiler.
659
660 The Unix C compiler is unable to handle the @samp{-g} and @samp{-O} options
661 together. This means that you cannot ask for optimization if you ask for
662 debugger information.
663
664 The GNU C compiler supports @samp{-g} with or without @samp{-O}, making it
665 possible to debug optimized code. We recommend that you @emph{always} use
666 @samp{-g} whenever you compile a program. You may think the program is
667 correct, but there's no sense in pushing your luck.
668
669 GDB no longer supports the debugging information produced by giving the
670 GNU C compiler the @samp{-gg} option, so do not use this option.
671
672 @ignore
673 @comment As far as I know, there are no cases in which GDB will
674 @comment produce strange output in this case. (but no promises).
675 If your program includes archives made with the @code{ar} program, and
676 if the object files used as input to @code{ar} were compiled without the
677 @samp{-g} option and have names longer than 15 characters, GDB will get
678 confused reading the program's symbol table. No error message will be
679 given, but GDB may behave strangely. The reason for this problem is a
680 deficiency in the Unix archive file format, which cannot represent file
681 names longer than 15 characters.
682
683 To avoid this problem, compile the archive members with the @samp{-g}
684 option or use shorter file names. Alternatively, use a version of GNU
685 @code{ar} dated more recently than August 1989.
686 @end ignore
687
688 @node Running, Stopping, Compilation, Top
689 @chapter Running Your Program Under GDB
690
691 @cindex running
692 @kindex run
693 To start your program under GDB, use the @samp{run} command. The program
694 must already have been specified using the @samp{exec-file} command or with
695 an argument to GDB (@pxref{Files}); what @samp{run} does is create an
696 inferior process, load the program into it, and set it in motion.
697
698 The execution of a program is affected by certain information it
699 receives from its superior. GDB provides ways to specify this
700 information, which you must do @i{before} starting the program. (You
701 can change it after starting the program, but such changes do not affect
702 the program unless you start it over again.) This information may be
703 divided into three categories:
704
705 @table @asis
706 @item The @i{arguments.}
707 You specify the arguments to give the program as the arguments of the
708 @samp{run} command.
709
710 @item The @i{environment.}
711 The program normally inherits its environment from GDB, but you can
712 use the GDB commands @samp{set environment} and
713 @samp{unset environment} to change parts of the environment that will
714 be given to the program.@refill
715
716 @item The @i{working directory.}
717 The program inherits its working directory from GDB. You can set GDB's
718 working directory with the @samp{cd} command in GDB.
719 @end table
720
721 After the @samp{run} command, the debugger does nothing but wait for your
722 program to stop. @xref{Stopping}.
723
724 Note that once your program has been started by the @samp{run} command,
725 you may evaluate expressions that involve calls to functions in the
726 inferior. @xref{Expressions}. If you wish to evaluate a function
727 simply for its side affects, you may use the @samp{set} command.
728 @xref{Assignment}.
729
730 @menu
731 * Arguments:: Specifying the arguments for your program.
732 * Environment:: Specifying the environment for your program.
733 * Working Directory:: Specifying the working directory for giving
734 to your program when it is run.
735 * Input/Output:: Specifying the program's standard input and output.
736 * Attach:: Debugging a process started outside GDB.
737 * Kill Process:: Getting rid of the child process running your program.
738 @end menu
739
740 @node Arguments, Environment, Running, Running
741 @section Your Program's Arguments
742
743 @cindex arguments (to your program)
744 The arguments to your program are specified by the arguments of the
745 @samp{run} command. They are passed to a shell, which expands wildcard
746 characters and performs redirection of I/O, and thence to the program.
747
748 @samp{run} with no arguments uses the same arguments used by the previous
749 @samp{run}.
750
751 @kindex set args
752 The command @samp{set args} can be used to specify the arguments to be used
753 the next time the program is run. If @samp{set args} has no arguments, it
754 means to use no arguments the next time the program is run. If you have
755 run your program with arguments and want to run it again with no arguments,
756 this is the only way to do so.
757
758 @node Environment, Working Directory, Arguments, Running
759 @section Your Program's Environment
760
761 @cindex environment (of your program)
762 The @dfn{environment} consists of a set of @dfn{environment variables} and
763 their values. Environment variables conventionally record such things as
764 your user name, your home directory, your terminal type, and your search
765 path for programs to run. Usually you set up environment variables with
766 the shell and they are inherited by all the other programs you run. When
767 debugging, it can be useful to try running the program with different
768 environments without having to start the debugger over again.
769
770 @table @code
771 @item info environment @var{varname}
772 @kindex info environment
773 Print the value of environment variable @var{varname} to be given to
774 your program when it is started. This command can be abbreviated
775 @samp{i env @var{varname}}.
776
777 @item info environment
778 Print the names and values of all environment variables to be given to
779 your program when it is started. This command can be abbreviated
780 @samp{i env}.
781
782 @item set environment @var{varname} @var{value}
783 @itemx set environment @var{varname} = @var{value}
784 @kindex set environment
785 Sets environment variable @var{varname} to @var{value}, for your program
786 only, not for GDB itself. @var{value} may be any string; the values of
787 environment variables are just strings, and any interpretation is
788 supplied by your program itself. The @var{value} parameter is optional;
789 if it is eliminated, the variable is set to a null value. This command
790 can be abbreviated as short as @samp{set e}.
791
792 For example, this command:
793
794 @example
795 set env USER = foo
796 @end example
797
798 @noindent
799 tells the program, when subsequently run, to assume it is being run
800 on behalf of the user named @samp{foo}.
801
802 @item delete environment @var{varname}
803 @itemx unset environment @var{varname}
804 @kindex delete environment
805 @kindex unset environment
806 Remove variable @var{varname} from the environment to be passed to your
807 program. This is different from @samp{set env @var{varname}@ =} because
808 @samp{delete environment} leaves the variable with no value, which is
809 distinguishable from an empty value. This command can be abbreviated
810 @samp{d e}.
811 @end table
812
813 @node Working Directory, Input/Output, Environment, Running
814 @section Your Program's Working Directory
815
816 @cindex working directory (of your program)
817 Each time you start your program with @samp{run}, it inherits its
818 working directory from the current working directory of GDB. GDB's
819 working directory is initially whatever it inherited from its parent
820 process (typically the shell), but you can specify a new working
821 directory in GDB with the @samp{cd} command.
822
823 The GDB working directory also serves as a default for the commands
824 that specify files for GDB to operate on. @xref{Files}.
825
826 @table @code
827 @item cd @var{directory}
828 @kindex cd
829 Set GDB's working directory to @var{directory}.
830
831 @item pwd
832 @kindex pwd
833 Print GDB's working directory.
834 @end table
835
836 @node Input/Output, Attach, Working Directory, Running
837 @section Your Program's Input and Output
838
839 @cindex redirection
840 @cindex controlling terminal
841 By default, the program you run under GDB does input and output to the same
842 terminal that GDB uses.
843
844 You can redirect the program's input and/or output using @samp{sh}-style
845 redirection commands in the @samp{run} command. For example,
846
847 @example
848 run > outfile
849 @end example
850
851 @noindent
852 starts the program, diverting its output to the file @file{outfile}.
853
854 @kindex tty
855 Another way to specify where the program should do input and output is
856 with the @samp{tty} command. This command accepts a file name as
857 argument, and causes this file to be the default for future @samp{run}
858 commands. It also resets the controlling terminal for the child
859 process, for future @samp{run} commands. For example,
860
861 @example
862 tty /dev/ttyb
863 @end example
864
865 @noindent
866 directs that processes started with subsequent @samp{run} commands
867 default to do input and output on the terminal @file{/dev/ttyb} and have
868 that as their controlling terminal.
869
870 An explicit redirection in @samp{run} overrides the @samp{tty} command's
871 effect on input/output redirection, but not its effect on the
872 controlling terminal.
873
874 When you use the @samp{tty} command or redirect input in the @samp{run}
875 command, only the @emph{input for your program} is affected. The input
876 for GDB still comes from your terminal.
877
878 @node Attach, Kill Process, Input/Output, Running
879 @section Debugging an Already-Running Process
880 @kindex detach
881 @kindex attach
882 @cindex attach
883
884 Some operating systems allow GDB to debug an already-running process
885 that was started outside of GDB. To do this, you use the @samp{attach}
886 command instead of the @samp{run} command.
887
888 The @samp{attach} command requires one argument, which is the process-id
889 of the process you want to debug. (The usual way to find out the
890 process-id of the process is with the @code{ps} utility.)
891
892 The first thing GDB does after arranging to debug the process is to stop
893 it. You can examine and modify an attached process with all the GDB
894 commands that ordinarily available when you start processes with
895 @samp{run}. You can insert breakpoints; you can step and continue; you
896 can modify storage. If you would rather the process continue running,
897 you may use the @samp{continue} command after attaching GDB to the
898 process.
899
900 When you have finished debugging the attached process, you can use the
901 @samp{detach} command to release it from GDB's control. Detaching
902 the process continues its execution. After the @samp{detach} command,
903 that process and GDB become completely independent once more, and you
904 are ready to @samp{attach} another process or start one with @samp{run}.
905
906 If you exit GDB or use the @samp{run} command while you have an attached
907 process, you kill that process. You will be asked for confirmation if you
908 try to do either of these things.
909
910 The @samp{attach} command is also used to debug a remote machine via a
911 serial connection. @xref{Remote}, for more info.
912
913 @node Kill Process,, Attach, Running
914 @section Killing the Child Process
915
916 @table @code
917 @item kill
918 @kindex kill
919 Kill the child process in which the program being debugged is running
920 under GDB.
921
922 This command is useful if you wish to debug a core dump instead. GDB
923 ignores any core dump file if it is actually running the program, so the
924 @samp{kill} command is the only sure way to make sure the core dump file
925 is used once again.
926
927 It is also useful if you wish to run the program outside the debugger
928 for once and then go back to debugging it.
929
930 The @samp{kill} command is also useful if you wish to recompile and
931 relink the program, since on many systems it is impossible to modify an
932 executable file which is running in a process. But, in this case, it is
933 just as good to exit GDB, since you will need to read a new symbol table
934 after the program is recompiled if you wish to debug the new version,
935 and restarting GDB is the easiest way to do that.
936 @end table
937
938 @node Stopping, Stack, Running, Top
939 @chapter Stopping and Continuing
940
941 When you run a program normally, it runs until it terminates. The
942 principal purpose of using a debugger is so that you can stop it before
943 that point; or so that if the program runs into trouble you can
944 investigate and find out why.
945
946 @menu
947 * Signals:: Fatal signals in your program just stop it;
948 then you can use GDB to see what is going on.
949 * Breakpoints:: Breakpoints let you stop your program when it
950 reaches a specified point in the code.
951 * Continuing:: Resuming execution until the next signal or breakpoint.
952 * Stepping:: Stepping runs the program a short distance and
953 then stops it wherever it has come to.
954 @end menu
955
956 @node Signals, Breakpoints, Stopping, Stopping
957 @section Signals
958 @cindex signals
959
960 A signal is an asynchronous event that can happen in a program. The
961 operating system defines the possible kinds of signals, and gives each kind
962 a name and a number. For example, @code{SIGINT} is the signal a program
963 gets when you type @kbd{Ctrl-c}; @code{SIGSEGV} is the signal a program
964 gets from referencing a place in memory far away from all the areas in use;
965 @code{SIGALRM} occurs when the alarm clock timer goes off (which happens
966 only if the program has requested an alarm).
967
968 @cindex fatal signals
969 Some signals, including @code{SIGALRM}, are a normal part of the
970 functioning of the program. Others, such as @code{SIGSEGV}, indicate
971 errors; these signals are @dfn{fatal} (kill the program immediately) if the
972 program has not specified in advance some other way to handle the signal.
973 @code{SIGINT} does not indicate an error in the program, but it is normally
974 fatal so it can carry out the purpose of @kbd{Ctrl-c}: to kill the program.
975
976 GDB has the ability to detect any occurrence of a signal in the program
977 running under GDB's control. You can tell GDB in advance what to do for
978 each kind of signal.
979
980 @cindex handling signals
981 Normally, GDB is set up to ignore non-erroneous signals like @code{SIGALRM}
982 (so as not to interfere with their role in the functioning of the program)
983 but to stop the program immediately whenever an error signal happens.
984 You can change these settings with the @samp{handle} command.
985
986 @table @code
987 @item info signal
988 @kindex info signal
989 Print a table of all the kinds of signals and how GDB has been told to
990 handle each one. You can use this to see the signal numbers of all
991 the defined types of signals.
992
993 @item handle @var{signal} @var{keywords}@dots{}
994 @kindex handle
995 Change the way GDB handles signal @var{signal}. @var{signal} can be the
996 number of a signal or its name (with or without the @samp{SIG} at the
997 beginning). The @var{keywords} say what change to make.
998 @end table
999
1000 The keywords allowed by the handle command can be abbreviated. Their full
1001 names are
1002
1003 @table @code
1004 @item stop
1005 GDB should stop the program when this signal happens. This implies
1006 the @samp{print} keyword as well.
1007
1008 @item print
1009 GDB should print a message when this signal happens.
1010
1011 @item nostop
1012 GDB should not stop the program when this signal happens. It may
1013 still print a message telling you that the signal has come in.
1014
1015 @item noprint
1016 GDB should not mention the occurrence of the signal at all. This
1017 implies the @samp{nostop} keyword as well.
1018
1019 @item pass
1020 GDB should allow the program to see this signal; the program will be
1021 able to handle the signal, or may be terminated if the signal is fatal
1022 and not handled.
1023
1024 @item nopass
1025 GDB should not allow the program to see this signal.
1026 @end table
1027
1028 When a signal has been set to stop the program, the program cannot see the
1029 signal until you continue. It will see the signal then, if @samp{pass} is
1030 in effect for the signal in question @i{at that time}. In other words,
1031 after GDB reports a signal, you can use the @samp{handle} command with
1032 @samp{pass} or @samp{nopass} to control whether that signal will be seen by
1033 the program when you later continue it.
1034
1035 You can also use the @samp{signal} command to prevent the program from
1036 seeing a signal, or cause it to see a signal it normally would not see,
1037 or to give it any signal at any time. @xref{Signaling}.
1038
1039 @node Breakpoints, Continuing, Signals, Stopping
1040 @section Breakpoints
1041
1042 @cindex breakpoints
1043 A @dfn{breakpoint} makes your program stop whenever a certain point in the
1044 program is reached. You set breakpoints explicitly with GDB commands,
1045 specifying the place where the program should stop by line number, function
1046 name or exact address in the program. You can add various other conditions
1047 to control whether the program will stop.
1048
1049 Each breakpoint is assigned a number when it is created; these numbers are
1050 successive integers starting with 1. In many of the commands for controlling
1051 various features of breakpoints you use the breakpoint number to say which
1052 breakpoint you want to change. Each breakpoint may be @dfn{enabled} or
1053 @dfn{disabled}; if disabled, it has no effect on the program until you
1054 enable it again.
1055
1056 @kindex info break
1057 @kindex $_
1058 The command @samp{info break} prints a list of all breakpoints set and not
1059 deleted, showing their numbers, where in the program they are, and any
1060 special features in use for them. Disabled breakpoints are included in the
1061 list, but marked as disabled. @samp{info break} with a breakpoint number
1062 as argument lists only that breakpoint. The convenience variable @code{$_}
1063 and the default examining-address for the @samp{x} command are set to the
1064 address of the last breakpoint listed (@pxref{Memory}).
1065
1066 @menu
1067 * Set Breaks:: How to establish breakpoints.
1068 * Delete Breaks:: How to remove breakpoints no longer needed.
1069 * Disabling:: How to disable breakpoints (turn them off temporarily).
1070 * Conditions:: Making extra conditions on whether to stop.
1071 * Break Commands:: Commands to be executed at a breakpoint.
1072 * Error in Breakpoints:: "Cannot insert breakpoints" error--why, what to do.
1073 @end menu
1074
1075 @node Set Breaks, Delete Breaks, Breakpoints, Breakpoints
1076 @subsection Setting Breakpoints
1077
1078 @kindex break
1079 Breakpoints are set with the @samp{break} command (abbreviated @samp{b}).
1080 You have several ways to say where the breakpoint should go.
1081
1082 @table @code
1083 @item break @var{function}
1084 Set a breakpoint at entry to function @var{function}.
1085
1086 @item break @var{+offset}
1087 @itemx break @var{-offset}
1088 Set a breakpoint some number of lines forward or back from the position
1089 at which execution stopped in the currently selected frame.
1090
1091 @item break @var{linenum}
1092 Set a breakpoint at line @var{linenum} in the current source file.
1093 That file is the last file whose source text was printed. This
1094 breakpoint will stop the program just before it executes any of the
1095 code on that line.
1096
1097 @item break @var{filename}:@var{linenum}
1098 Set a breakpoint at line @var{linenum} in source file @var{filename}.
1099
1100 @item break @var{filename}:@var{function}
1101 Set a breakpoint at entry to function @var{function} found in file
1102 @var{filename}. Specifying a file name as well as a function name is
1103 superfluous except when multiple files contain similarly named
1104 functions.
1105
1106 @item break *@var{address}
1107 Set a breakpoint at address @var{address}. You can use this to set
1108 breakpoints in parts of the program which do not have debugging
1109 information or source files.
1110
1111 @item break
1112 Set a breakpoint at the next instruction to be executed in the selected
1113 stack frame (@pxref{Stack}). In any selected frame but the innermost,
1114 this will cause the program to stop as soon as control returns to that
1115 frame. This is equivalent to a @samp{finish} command in the frame
1116 inside the selected frame. If this is done in the innermost frame, GDB
1117 will stop the next time it reaches the current location; this may be
1118 useful inside of loops.
1119
1120 GDB normally ignores breakpoints when it resumes execution, until at
1121 least one instruction has been executed. If it did not do this, you
1122 would be unable to proceed past a breakpoint without first disabling the
1123 breakpoint. This rule applies whether or not the breakpoint already
1124 existed when the program stopped.
1125
1126 @item break @dots{} if @var{cond}
1127 Set a breakpoint with condition @var{cond}; evaluate the expression
1128 @var{cond} each time the breakpoint is reached, and stop only if the
1129 value is nonzero. @samp{@dots{}} stands for one of the possible
1130 arguments described above (or no argument) specifying where to break.
1131 @xref{Conditions}, for more information on breakpoint conditions.
1132
1133 @item tbreak @var{args}
1134 @kindex tbreak
1135 Set a breakpoint enabled only for one stop. @var{args} are the
1136 same as in the @samp{break} command, and the breakpoint is set in the same
1137 way, but the breakpoint is automatically disabled the first time it
1138 is hit. @xref{Disabling}.
1139 @end table
1140
1141 GDB allows you to set any number of breakpoints at the same place in the
1142 program. There is nothing silly or meaningless about this. When the
1143 breakpoints are conditional, this is even useful (@pxref{Conditions}).
1144
1145 @node Delete Breaks, Disabling, Set Breaks, Breakpoints
1146 @subsection Deleting Breakpoints
1147
1148 @cindex clearing breakpoint
1149 @cindex deleting breakpoints
1150 It is often necessary to eliminate a breakpoint once it has done its job
1151 and you no longer want the program to stop there. This is called
1152 @dfn{deleting} the breakpoint. A breakpoint that has been deleted no
1153 longer exists in any sense; it is forgotten.
1154
1155 With the @samp{clear} command you can delete breakpoints according to where
1156 they are in the program. With the @samp{delete} command you can delete
1157 individual breakpoints by specifying their breakpoint numbers.
1158
1159 @b{It is not necessary to delete a breakpoint to proceed past it.} GDB
1160 automatically ignores breakpoints in the first instruction to be executed
1161 when you continue execution without changing the execution address.
1162
1163 @table @code
1164 @item clear
1165 @kindex clear
1166 Delete any breakpoints at the next instruction to be executed in the
1167 selected stack frame (@pxref{Selection}). When the innermost frame
1168 is selected, this is a good way to delete a breakpoint that the program
1169 just stopped at.
1170
1171 @item clear @var{function}
1172 @itemx clear @var{filename}:@var{function}
1173 Delete any breakpoints set at entry to the function @var{function}.
1174
1175 @item clear @var{linenum}
1176 @itemx clear @var{filename}:@var{linenum}
1177 Delete any breakpoints set at or within the code of the specified line.
1178
1179 @item delete @var{bnums}@dots{}
1180 @kindex delete
1181 Delete the breakpoints of the numbers specified as arguments.
1182 @end table
1183
1184 @node Disabling, Conditions, Delete Breaks, Breakpoints
1185 @subsection Disabling Breakpoints
1186
1187 @cindex disabled breakpoints
1188 @cindex enabled breakpoints
1189 Rather than deleting a breakpoint, you might prefer to @dfn{disable} it.
1190 This makes the breakpoint inoperative as if it had been deleted, but
1191 remembers the information on the breakpoint so that you can @dfn{enable}
1192 it again later.
1193
1194 You disable and enable breakpoints with the @samp{enable} and
1195 @samp{disable} commands, specifying one or more breakpoint numbers as
1196 arguments. Use @samp{info break} to print a list of breakpoints if you
1197 don't know which breakpoint numbers to use.
1198
1199 A breakpoint can have any of four different states of enablement:
1200
1201 @itemize @bullet
1202 @item
1203 Enabled. The breakpoint will stop the program. A breakpoint made
1204 with the @samp{break} command starts out in this state.
1205 @item
1206 Disabled. The breakpoint has no effect on the program.
1207 @item
1208 Enabled once. The breakpoint will stop the program, but
1209 when it does so it will become disabled. A breakpoint made
1210 with the @samp{tbreak} command starts out in this state.
1211 @item
1212 Enabled for deletion. The breakpoint will stop the program, but
1213 immediately after it does so it will be deleted permanently.
1214 @end itemize
1215
1216 You change the state of enablement of a breakpoint with the following
1217 commands:
1218
1219 @table @code
1220 @item disable breakpoints @var{bnums}@dots{}
1221 @itemx disable @var{bnums}@dots{}
1222 @kindex disable breakpoints
1223 @kindex disable
1224 Disable the specified breakpoints. A disabled breakpoint has no
1225 effect but is not forgotten. All options such as ignore-counts,
1226 conditions and commands are remembered in case the breakpoint is
1227 enabled again later.
1228
1229 @item enable breakpoints @var{bnums}@dots{}
1230 @itemx enable @var{bnums}@dots{}
1231 @kindex enable breakpoints
1232 @kindex enable
1233 Enable the specified breakpoints. They become effective once again in
1234 stopping the program, until you specify otherwise.
1235
1236 @item enable breakpoints once @var{bnums}@dots{}
1237 @itemx enable once @var{bnums}@dots{}
1238 Enable the specified breakpoints temporarily. Each will be disabled
1239 again the next time it stops the program (unless you have used one of
1240 these commands to specify a different state before that time comes).
1241
1242 @item enable breakpoints delete @var{bnums}@dots{}
1243 @itemx enable delete @var{bnums}@dots{}
1244 Enable the specified breakpoints to work once and then die. Each of
1245 the breakpoints will be deleted the next time it stops the program
1246 (unless you have used one of these commands to specify a different
1247 state before that time comes).
1248 @end table
1249
1250 Aside from the automatic disablement or deletion of a breakpoint when it
1251 stops the program, which happens only in certain states, the state of
1252 enablement of a breakpoint changes only when one of the commands above
1253 is used.
1254
1255 @node Conditions, Break Commands, Disabling, Breakpoints
1256 @subsection Break Conditions
1257 @cindex conditional breakpoints
1258 @cindex breakpoint conditions
1259
1260 The simplest sort of breakpoint breaks every time the program reaches a
1261 specified place. You can also specify a @dfn{condition} for a
1262 breakpoint. A condition is just a boolean expression in your
1263 programming language. (@xref{Expressions}). A breakpoint with a
1264 condition evaluates the expression each time the program reaches it, and
1265 the program stops only if the condition is true.
1266
1267 Break conditions may have side effects, and may even call functions in your
1268 program. These may sound like strange things to do, but their effects are
1269 completely predictable unless there is another enabled breakpoint at the
1270 same address. (In that case, GDB might see the other breakpoint first and
1271 stop the program without checking the condition of this one.) Note that
1272 breakpoint commands are usually more convenient and flexible for the
1273 purpose of performing side effects when a breakpoint is reached
1274 (@pxref{Break Commands}).
1275
1276 Break conditions can be specified when a breakpoint is set, by using
1277 @samp{if} in the arguments to the @samp{break} command. @xref{Set Breaks}.
1278 They can also be changed at any time with the @samp{condition} command:
1279
1280 @table @code
1281 @item condition @var{bnum} @var{expression}
1282 @kindex condition
1283 Specify @var{expression} as the break condition for breakpoint number
1284 @var{bnum}. From now on, this breakpoint will stop the program only if
1285 the value of @var{expression} is true (nonzero, in C). @var{expression}
1286 is not evaluated at the time the @samp{condition} command is given.
1287 @xref{Expressions}.
1288
1289 @item condition @var{bnum}
1290 Remove the condition from breakpoint number @var{bnum}. It becomes
1291 an ordinary unconditional breakpoint.
1292 @end table
1293
1294 @cindex ignore count (of breakpoint)
1295 A special case of a breakpoint condition is to stop only when the
1296 breakpoint has been reached a certain number of times. This is so
1297 useful that there is a special way to do it, using the @dfn{ignore
1298 count} of the breakpoint. Every breakpoint has an ignore count, which
1299 is an integer. Most of the time, the ignore count is zero, and
1300 therefore has no effect. But if the program reaches a breakpoint whose
1301 ignore count is positive, then instead of stopping, it just decrements
1302 the ignore count by one and continues. As a result, if the ignore count
1303 value is @var{n}, the breakpoint will not stop the next @var{n} times it
1304 is reached.
1305
1306 @table @code
1307 @item ignore @var{bnum} @var{count}
1308 @kindex ignore
1309 Set the ignore count of breakpoint number @var{bnum} to @var{count}.
1310 The next @var{count} times the breakpoint is reached, it will not stop.
1311
1312 To make the breakpoint stop the next time it is reached, specify
1313 a count of zero.
1314
1315 @item cont @var{count}
1316 Continue execution of the program, setting the ignore count of the
1317 breakpoint that the program stopped at to @var{count} minus one.
1318 Thus, the program will not stop at this breakpoint until the
1319 @var{count}'th time it is reached.
1320
1321 This command is allowed only when the program stopped due to a
1322 breakpoint. At other times, the argument to @samp{cont} is ignored.
1323 @end table
1324
1325 If a breakpoint has a positive ignore count and a condition, the condition
1326 is not checked. Once the ignore count reaches zero, the condition will
1327 start to be checked.
1328
1329 Note that you could achieve the effect of the ignore count with a
1330 condition such as @w{@samp{$foo-- <= 0}} using a debugger convenience
1331 variable that is decremented each time. @xref{Convenience Vars}.
1332
1333 @node Break Commands, Error in Breakpoints, Conditions, Breakpoints
1334 @subsection Commands Executed on Breaking
1335
1336 @cindex breakpoint commands
1337 You can give any breakpoint a series of commands to execute when the
1338 program stops due to that breakpoint. For example, you might want to
1339 print the values of certain expressions, or enable other breakpoints.
1340
1341 @table @code
1342 @item commands @var{bnum}
1343 Specify commands for breakpoint number @var{bnum}. The commands
1344 themselves appear on the following lines. Type a line containing just
1345 @samp{end} to terminate the commands.
1346
1347 To remove all commands from a breakpoint, use the command
1348 @samp{commands} and follow it immediately by @samp{end}; that is, give
1349 no commands.
1350
1351 With no arguments, @samp{commands} refers to the last breakpoint set.
1352 @end table
1353
1354 It is possible for breakpoint commands to start the program up again.
1355 Simply use the @samp{cont} command, or @samp{step}, or any other command
1356 to resume execution. However, any remaining breakpoint commands are
1357 ignored. When the program stops again, GDB will act according to the
1358 cause of that stop.
1359
1360 @kindex silent
1361 If the first command specified is @samp{silent}, the usual message about
1362 stopping at a breakpoint is not printed. This may be desirable for
1363 breakpoints that are to print a specific message and then continue.
1364 If the remaining commands too print nothing, you will see no sign that
1365 the breakpoint was reached at all. @samp{silent} is not really a command;
1366 it is meaningful only at the beginning of the commands for a breakpoint.
1367
1368 The commands @samp{echo} and @samp{output} that allow you to print precisely
1369 controlled output are often useful in silent breakpoints. @xref{Output}.
1370
1371 For example, here is how you could use breakpoint commands to print the
1372 value of @code{x} at entry to @code{foo} whenever it is positive.
1373
1374 @example
1375 break foo if x>0
1376 commands
1377 silent
1378 echo x is\040
1379 output x
1380 echo \n
1381 cont
1382 end
1383 @end example
1384
1385 One application for breakpoint commands is to correct one bug so you can
1386 test another. Put a breakpoint just after the erroneous line of code, give
1387 it a condition to detect the case in which something erroneous has been
1388 done, and give it commands to assign correct values to any variables that
1389 need them. End with the @samp{cont} command so that the program does not
1390 stop, and start with the @samp{silent} command so that no output is
1391 produced. Here is an example:
1392
1393 @example
1394 break 403
1395 commands
1396 silent
1397 set x = y + 4
1398 cont
1399 end
1400 @end example
1401
1402 One deficiency in the operation of automatically continuing breakpoints
1403 under Unix appears when your program uses raw mode for the terminal.
1404 GDB switches back to its own terminal modes (not raw) before executing
1405 commands, and then must switch back to raw mode when your program is
1406 continued. This causes any pending terminal input to be lost.
1407
1408 In the GNU system, this will be fixed by changing the behavior of
1409 terminal modes.
1410
1411 Under Unix, when you have this problem, you might be able to get around
1412 it by putting your actions into the breakpoint condition instead of
1413 commands. For example
1414
1415 @example
1416 condition 5 (x = y + 4), 0
1417 @end example
1418
1419 @noindent
1420 specifies a condition expression (@xref{Expressions}) that will change
1421 @code{x} as needed, then always have the value 0 so the program will not
1422 stop. Loss of input is avoided here because break conditions are
1423 evaluated without changing the terminal modes. When you want to have
1424 nontrivial conditions for performing the side effects, the operators
1425 @samp{&&}, @samp{||} and @samp{?@dots{}:} may be useful.
1426
1427 @node Error in Breakpoints,, Break Commands, Breakpoints
1428 @subsection ``Cannot Insert Breakpoints'' Error
1429
1430 Under some operating systems, breakpoints cannot be used in a program if
1431 any other process is running that program. Attempting to run or
1432 continue the program with a breakpoint in this case will cause GDB to
1433 stop it.
1434
1435 When this happens, you have three ways to proceed:
1436
1437 @enumerate
1438 @item
1439 Remove or disable the breakpoints, then continue.
1440
1441 @item
1442 Suspend GDB, and copy the file containing the program to a new name.
1443 Resume GDB and use the @samp{exec-file} command to specify that GDB
1444 should run the program under that name. Then start the program again.
1445
1446 @item
1447 Relink the program so that the text segment is nonsharable, using the
1448 linker option @samp{-N}. The operating system limitation may not apply
1449 to nonsharable executables.
1450 @end enumerate
1451
1452 @node Continuing, Stepping, Breakpoints, Stopping
1453 @section Continuing
1454
1455 After your program stops, most likely you will want it to run some more if
1456 the bug you are looking for has not happened yet.
1457
1458 @table @code
1459 @item cont
1460 @kindex cont
1461 Continue running the program at the place where it stopped.
1462 @end table
1463
1464 If the program stopped at a breakpoint, the place to continue running
1465 is the address of the breakpoint. You might expect that continuing would
1466 just stop at the same breakpoint immediately. In fact, @samp{cont}
1467 takes special care to prevent that from happening. You do not need
1468 to delete the breakpoint to proceed through it after stopping at it.
1469
1470 You can, however, specify an ignore-count for the breakpoint that the
1471 program stopped at, by means of an argument to the @samp{cont} command.
1472 @xref{Conditions}.
1473
1474 If the program stopped because of a signal other than @code{SIGINT} or
1475 @code{SIGTRAP}, continuing will cause the program to see that signal.
1476 You may not want this to happen. For example, if the program stopped
1477 due to some sort of memory reference error, you might store correct
1478 values into the erroneous variables and continue, hoping to see more
1479 execution; but the program would probably terminate immediately as
1480 a result of the fatal signal once it sees the signal. To prevent this,
1481 you can continue with @samp{signal 0}. @xref{Signaling}. You can
1482 also act in advance to prevent the program from seeing certain kinds
1483 of signals, using the @samp{handle} command (@pxref{Signals}).
1484
1485 @node Stepping,, Continuing, Stopping
1486 @section Stepping
1487
1488 @cindex stepping
1489 @dfn{Stepping} means setting your program in motion for a limited time, so
1490 that control will return automatically to the debugger after one line of
1491 code or one machine instruction. Breakpoints are active during stepping
1492 and the program will stop for them even if it has not gone as far as the
1493 stepping command specifies.
1494
1495 @table @code
1496 @item step
1497 @kindex step
1498 Continue running the program until control reaches a different line,
1499 then stop it and return control to the debugger. This command is
1500 abbreviated @samp{s}.
1501
1502 This command may be given when control is within a function for which
1503 there is no debugging information. In that case, execution will proceed
1504 until control reaches a different function, or is about to return from
1505 this function. An argument repeats this action.
1506
1507 @item step @var{count}
1508 Continue running as in @samp{step}, but do so @var{count} times. If a
1509 breakpoint is reached or a signal not related to stepping occurs before
1510 @var{count} steps, stepping stops right away.
1511
1512 @item next
1513 @kindex next
1514 Similar to @samp{step}, but any function calls appearing within the line of
1515 code are executed without stopping. Execution stops when control reaches a
1516 different line of code at the stack level which was executing when the
1517 @samp{next} command was given. This command is abbreviated @samp{n}.
1518
1519 An argument is a repeat count, as in @samp{step}.
1520
1521 @samp{next} within a function without debugging information acts as does
1522 @samp{step}, but any function calls appearing within the code of the
1523 function are executed without stopping.
1524
1525 @item finish
1526 @kindex finish
1527 Continue running until just after the selected stack frame returns (or
1528 until there is some other reason to stop, such as a fatal signal or a
1529 breakpoint). Print value returned by the selected stack frame (if any).
1530
1531 Contrast this with the @samp{return} command (@pxref{Returning}).
1532
1533 @item until
1534 @kindex until
1535 This command is used to avoid single stepping through a loop more than
1536 once. It is like the @samp{next} command, except that when @samp{until}
1537 encounters a jump, it automatically continues execution until the
1538 program counter is greater than the address of the jump.
1539
1540 This means that when you reach the end of a loop after single stepping
1541 though it, @samp{until} will cause the program to continue execution
1542 until the loop is exited. In contrast, a @samp{next} command at the end
1543 of a loop will simply step back to the beginning of the loop, which
1544 would force you to step through the next iteration.
1545
1546 @samp{until} always stops the program if it attempts to exit the current
1547 stack frame.
1548
1549 @samp{until} may produce somewhat counterintuitive results if the order
1550 of the source lines does not match the actual order of execution. For
1551 example, in a typical C @code{for}-loop, the third expression in the
1552 @code{for}-statement (the loop-step expression) is executed after the
1553 statements in the body of the loop, but is written before them.
1554 Therefore, the @samp{until} command would appear to step back to the
1555 beginning of the loop when it advances to this expression. However, it
1556 has not really done so, not in terms of the actual machine code.
1557
1558 Note that @samp{until} with no argument works by means of single
1559 instruction stepping, and hence is slower than @samp{until} with an
1560 argument.
1561
1562 @item until @var{location}
1563 Continue running the program until either the specified location is
1564 reached, or the current (innermost) stack frame returns. This form of
1565 the command uses breakpoints, and hence is quicker than @samp{until}
1566 without an argument.
1567
1568 @item stepi
1569 @itemx si
1570 @kindex stepi
1571 @kindex si
1572 Execute one machine instruction, then stop and return to the debugger.
1573
1574 It is often useful to do @samp{display/i $pc} when stepping by machine
1575 instructions. This will cause the next instruction to be executed to
1576 be displayed automatically at each stop. @xref{Auto Display}.
1577
1578 An argument is a repeat count, as in @samp{step}.
1579
1580 @item nexti
1581 @itemx ni
1582 @kindex nexti
1583 @kindex ni
1584 Execute one machine instruction, but if it is a subroutine call,
1585 proceed until the subroutine returns.
1586
1587 An argument is a repeat count, as in @samp{next}.
1588 @end table
1589
1590 A typical technique for using stepping is to put a breakpoint
1591 (@pxref{Breakpoints}) at the beginning of the function or the section of
1592 the program in which a problem is believed to lie, and then step through
1593 the suspect area, examining the variables that are interesting, until the
1594 problem happens.
1595
1596 The @samp{cont} command can be used after stepping to resume execution
1597 until the next breakpoint or signal.
1598
1599 @node Stack, Source, Stopping, Top
1600 @chapter Examining the Stack
1601
1602 When your program has stopped, the first thing you need to know is where it
1603 stopped and how it got there.
1604
1605 @cindex call stack
1606 Each time your program performs a function call, the information about
1607 where in the program the call was made from is saved in a block of data
1608 called a @dfn{stack frame}. The frame also contains the arguments of the
1609 call and the local variables of the function that was called. All the
1610 stack frames are allocated in a region of memory called the @dfn{call
1611 stack}.
1612
1613 When your program stops, the GDB commands for examining the stack allow you
1614 to see all of this information.
1615
1616 One of the stack frames is @dfn{selected} by GDB and many GDB commands
1617 refer implicitly to the selected frame. In particular, whenever you ask
1618 GDB for the value of a variable in the program, the value is found in the
1619 selected frame. There are special GDB commands to select whichever frame
1620 you are interested in.
1621
1622 When the program stops, GDB automatically selects the currently executing
1623 frame and describes it briefly as the @samp{frame} command does
1624 (@pxref{Frame Info, Info}).
1625
1626 @menu
1627 * Frames:: Explanation of stack frames and terminology.
1628 * Backtrace:: Summarizing many frames at once.
1629 * Selection:: How to select a stack frame.
1630 * Info: Frame Info, Commands to print information on stack frames.
1631 @end menu
1632
1633 @node Frames, Backtrace, Stack, Stack
1634 @section Stack Frames
1635
1636 @cindex frame
1637 @cindex stack frame
1638 The call stack is divided up into contiguous pieces called @dfn{stack
1639 frames}, or @dfn{frames} for short; each frame is the data associated
1640 with one call to one function. The frame contains the arguments given
1641 to the function, the function's local variables, and the address at
1642 which the function is executing.
1643
1644 @cindex initial frame
1645 @cindex outermost frame
1646 @cindex innermost frame
1647 When your program is started, the stack has only one frame, that of the
1648 function @code{main}. This is called the @dfn{initial} frame or the
1649 @dfn{outermost} frame. Each time a function is called, a new frame is
1650 made. Each time a function returns, the frame for that function invocation
1651 is eliminated. If a function is recursive, there can be many frames for
1652 the same function. The frame for the function in which execution is
1653 actually occurring is called the @dfn{innermost} frame. This is the most
1654 recently created of all the stack frames that still exist.
1655
1656 @cindex frame pointer
1657 Inside your program, stack frames are identified by their addresses. A
1658 stack frame consists of many bytes, each of which has its own address; each
1659 kind of computer has a convention for choosing one of those bytes whose
1660 address serves as the address of the frame. Usually this address is kept
1661 in a register called the @dfn{frame pointer register} while execution is
1662 going on in that frame.
1663
1664 @cindex frame number
1665 GDB assigns numbers to all existing stack frames, starting with zero for
1666 the innermost frame, one for the frame that called it, and so on upward.
1667 These numbers do not really exist in your program; they are to give you a
1668 way of talking about stack frames in GDB commands.
1669
1670 @cindex selected frame
1671 Many GDB commands refer implicitly to one stack frame. GDB records a stack
1672 frame that is called the @dfn{selected} stack frame; you can select any
1673 frame using one set of GDB commands, and then other commands will operate
1674 on that frame. When your program stops, GDB automatically selects the
1675 innermost frame.
1676
1677 @cindex frameless execution
1678 Some functions can be compiled to run without a frame reserved for them
1679 on the stack. This is occasionally done with heavily used library
1680 functions to save the frame setup time. GDB has limited facilities for
1681 dealing with these function invocations; if the innermost function
1682 invocation has no stack frame, GDB will give it a virtual stack frame of
1683 0 and correctly allow tracing of the function call chain. Results are
1684 undefined if a function invocation besides the innermost one is
1685 frameless.
1686
1687 @node Backtrace, Selection, Frames, Stack
1688 @section Backtraces
1689
1690 A backtrace is a summary of how the program got where it is. It shows one
1691 line per frame, for many frames, starting with the currently executing
1692 frame (frame zero), followed by its caller (frame one), and on up the
1693 stack.
1694
1695 @table @code
1696 @item backtrace
1697 @itemx bt
1698 @kindex backtrace
1699 @kindex bt
1700 Print a backtrace of the entire stack: one line per frame for all
1701 frames in the stack.
1702
1703 You can stop the backtrace at any time by typing the system interrupt
1704 character, normally @kbd{Control-C}.
1705
1706 @item backtrace @var{n}
1707 @itemx bt @var{n}
1708 Similar, but print only the innermost @var{n} frames.
1709
1710 @item backtrace @var{-n}
1711 @itemx bt @var{-n}
1712 Similar, but print only the outermost @var{n} frames.
1713 @end table
1714
1715 @kindex where
1716 @kindex info stack
1717 The names @samp{where} and @samp{info stack} are additional aliases
1718 for @samp{backtrace}.
1719
1720 Every line in the backtrace shows the frame number, the function name
1721 and the program counter value.
1722
1723 If the function is in a source file whose symbol table data has been
1724 fully read, the backtrace shows the source file name and line number, as
1725 well as the arguments to the function. (The program counter value is
1726 omitted if it is at the beginning of the code for that line number.)
1727
1728 If the source file's symbol data has not been fully read, just scanned,
1729 this extra information is replaced with an ellipsis. You can force the
1730 symbol data for that frame's source file to be read by selecting the
1731 frame. (@xref{Selection}).
1732
1733 Here is an example of a backtrace. It was made with the command
1734 @samp{bt 3}, so it shows the innermost three frames.
1735
1736 @example
1737 #0 rtx_equal_p (x=(rtx) 0x8e58c, y=(rtx) 0x1086c4) (/gp/rms/cc/rtlanal.c line 337)
1738 #1 0x246b0 in expand_call (...) (...)
1739 #2 0x21cfc in expand_expr (...) (...)
1740 (More stack frames follow...)
1741 @end example
1742
1743 @noindent
1744 The functions @code{expand_call} and @code{expand_expr} are in a file
1745 whose symbol details have not been fully read. Full detail is available
1746 for the function @code{rtx_equal_p}, which is in the file
1747 @file{rtlanal.c}. Its arguments, named @code{x} and @code{y}, are shown
1748 with their typed values.
1749
1750 @node Selection, Frame Info, Backtrace, Stack
1751 @section Selecting a Frame
1752
1753 Most commands for examining the stack and other data in the program work on
1754 whichever stack frame is selected at the moment. Here are the commands for
1755 selecting a stack frame; all of them finish by printing a brief description
1756 of the stack frame just selected.
1757
1758 @table @code
1759 @item frame @var{n}
1760 @kindex frame
1761 Select frame number @var{n}. Recall that frame zero is the innermost
1762 (currently executing) frame, frame one is the frame that called the
1763 innermost one, and so on. The highest-numbered frame is @code{main}'s
1764 frame.
1765
1766 @item frame @var{addr}
1767 Select the frame at address @var{addr}. This is useful mainly if the
1768 chaining of stack frames has been damaged by a bug, making it
1769 impossible for GDB to assign numbers properly to all frames. In
1770 addition, this can be useful when the program has multiple stacks and
1771 switches between them.
1772
1773 @item up @var{n}
1774 @kindex up
1775 Select the frame @var{n} frames up from the frame previously selected.
1776 For positive numbers @var{n}, this advances toward the outermost
1777 frame, to higher frame numbers, to frames that have existed longer.
1778 @var{n} defaults to one.
1779
1780 @item down @var{n}
1781 @kindex down
1782 Select the frame @var{n} frames down from the frame previously
1783 selected. For positive numbers @var{n}, this advances toward the
1784 innermost frame, to lower frame numbers, to frames that were created
1785 more recently. @var{n} defaults to one.
1786 @end table
1787
1788 All of these commands end by printing some information on the frame that
1789 has been selected: the frame number, the function name, the arguments, the
1790 source file and line number of execution in that frame, and the text of
1791 that source line. For example:
1792
1793 @example
1794 #3 main (argc=3, argv=??, env=??) at main.c, line 67
1795 67 read_input_file (argv[i]);
1796 @end example
1797
1798 After such a printout, the @samp{list} command with no arguments will print
1799 ten lines centered on the point of execution in the frame. @xref{List}.
1800
1801 @node Frame Info,, Selection, Stack
1802 @section Information on a Frame
1803
1804 There are several other commands to print information about the selected
1805 stack frame.
1806
1807 @table @code
1808 @item frame
1809 This command prints a brief description of the selected stack frame.
1810 It can be abbreviated @samp{f}. With an argument, this command is
1811 used to select a stack frame; with no argument, it does not change
1812 which frame is selected, but still prints the same information.
1813
1814 @item info frame
1815 @kindex info frame
1816 This command prints a verbose description of the selected stack frame,
1817 including the address of the frame, the addresses of the next frame in
1818 (called by this frame) and the next frame out (caller of this frame),
1819 the address of the frame's arguments, the program counter saved in it
1820 (the address of execution in the caller frame), and which registers
1821 were saved in the frame. The verbose description is useful when
1822 something has gone wrong that has made the stack format fail to fit
1823 the usual conventions.
1824
1825 @item info frame @var{addr}
1826 Print a verbose description of the frame at address @var{addr},
1827 without selecting that frame. The selected frame remains unchanged by
1828 this command.
1829
1830 @item info args
1831 @kindex info args
1832 Print the arguments of the selected frame, each on a separate line.
1833
1834 @item info locals
1835 @kindex info locals
1836 Print the local variables of the selected frame, each on a separate
1837 line. These are all variables declared static or automatic within all
1838 program blocks that execution in this frame is currently inside of.
1839 @end table
1840
1841 @node Source, Data, Stack, Top
1842 @chapter Examining Source Files
1843
1844 GDB knows which source files your program was compiled from, and
1845 can print parts of their text. When your program stops, GDB
1846 spontaneously prints the line it stopped in. Likewise, when you
1847 select a stack frame (@pxref{Selection}), GDB prints the line
1848 which execution in that frame has stopped in. You can also
1849 print parts of source files by explicit command.
1850
1851 @menu
1852 * List:: Using the @samp{list} command to print source files.
1853 * Search:: Commands for searching source files.
1854 * Source Path:: Specifying the directories to search for source files.
1855 @end menu
1856
1857 @node List, Search, Source, Source
1858 @section Printing Source Lines
1859
1860 @kindex list
1861 To print lines from a source file, use the @samp{list} command
1862 (abbreviated @samp{l}). There are several ways to specify what part
1863 of the file you want to print.
1864
1865 Here are the forms of the @samp{list} command most commonly used:
1866
1867 @table @code
1868 @item list @var{linenum}
1869 Print ten lines centered around line number @var{linenum} in the
1870 current source file.
1871
1872 @item list @var{function}
1873 Print ten lines centered around the beginning of function
1874 @var{function}.
1875
1876 @item list
1877 Print ten more lines. If the last lines printed were printed with a
1878 @samp{list} command, this prints ten lines following the last lines
1879 printed; however, if the last line printed was a solitary line printed
1880 as part of displaying a stack frame (@pxref{Stack}), this prints ten
1881 lines centered around that line.
1882
1883 @item list -
1884 Print ten lines just before the lines last printed.
1885 @end table
1886
1887 Repeating a @samp{list} command with @key{RET} discards the argument,
1888 so it is equivalent to typing just @samp{list}. This is more useful
1889 than listing the same lines again. An exception is made for an
1890 argument of @samp{-}; that argument is preserved in repetition so that
1891 each repetition moves up in the file.
1892
1893 @cindex linespec
1894 In general, the @samp{list} command expects you to supply zero, one or two
1895 @dfn{linespecs}. Linespecs specify source lines; there are several ways
1896 of writing them but the effect is always to specify some source line.
1897 Here is a complete description of the possible arguments for @samp{list}:
1898
1899 @table @code
1900 @item list @var{linespec}
1901 Print ten lines centered around the line specified by @var{linespec}.
1902
1903 @item list @var{first},@var{last}
1904 Print lines from @var{first} to @var{last}. Both arguments are
1905 linespecs.
1906
1907 @item list ,@var{last}
1908 Print ten lines ending with @var{last}.
1909
1910 @item list @var{first},
1911 Print ten lines starting with @var{first}.
1912
1913 @item list +
1914 Print ten lines just after the lines last printed.
1915
1916 @item list -
1917 Print ten lines just before the lines last printed.
1918
1919 @item list
1920 As described in the preceding table.
1921 @end table
1922
1923 Here are the ways of specifying a single source line---all the
1924 kinds of linespec.
1925
1926 @table @code
1927 @item @var{linenum}
1928 Specifies line @var{linenum} of the current source file.
1929 When a @samp{list} command has two linespecs, this refers to
1930 the same source file as the first linespec.
1931
1932 @item +@var{offset}
1933 Specifies the line @var{offset} lines after the last line printed.
1934 When used as the second linespec in a @samp{list} command that has
1935 two, this specifies the line @var{offset} lines down from the
1936 first linespec.
1937
1938 @item -@var{offset}
1939 Specifies the line @var{offset} lines before the last line printed.
1940
1941 @item @var{filename}:@var{linenum}
1942 Specifies line @var{linenum} in the source file @var{filename}.
1943
1944 @item @var{function}
1945 Specifies the line of the open-brace that begins the body of the
1946 function @var{function}.
1947
1948 @item @var{filename}:@var{function}
1949 Specifies the line of the open-brace that begins the body of the
1950 function @var{function} in the file @var{filename}. The file name is
1951 needed with a function name only for disambiguation of identically
1952 named functions in different source files.
1953
1954 @item *@var{address}
1955 Specifies the line containing the program address @var{address}.
1956 @var{address} may be any expression.
1957 @end table
1958
1959 One other command is used to map source lines to program addresses.
1960
1961 @table @code
1962 @item info line @var{linenum}
1963 @kindex info line
1964 Print the starting and ending addresses of the compiled code for
1965 source line @var{linenum}.
1966
1967 @kindex $_
1968 The default examine address for the @samp{x} command is changed to the
1969 starting address of the line, so that @samp{x/i} is sufficient to
1970 begin examining the machine code (@pxref{Memory}). Also, this address
1971 is saved as the value of the convenience variable @code{$_}
1972 (@pxref{Convenience Vars}).
1973 @end table
1974
1975 @node Search, Source Path, List, Source
1976 @section Searching Source Files
1977 @cindex searching
1978 @kindex forward-search
1979 @kindex reverse-search
1980
1981 There are two commands for searching through the current source file for a
1982 regular expression.
1983
1984 The command @samp{forward-search @var{regexp}} checks each line, starting
1985 with the one following the last line listed, for a match for @var{regexp}.
1986 It lists the line that is found. You can abbreviate the command name
1987 as @samp{fo}.
1988
1989 The command @samp{reverse-search @var{regexp}} checks each line, starting
1990 with the one before the last line listed and going backward, for a match
1991 for @var{regexp}. It lists the line that is found. You can abbreviate
1992 this command with as little as @samp{rev}.
1993
1994 @node Source Path,, Search, Source
1995 @section Specifying Source Directories
1996
1997 @cindex source path
1998 @cindex directories for source files
1999 Executable programs do not record the directories of the source files
2000 from which they were compiled, just the names. GDB remembers a list of
2001 directories to search for source files; this is called the @dfn{source
2002 path}. Each time GDB wants a source file, it tries all the directories
2003 in the list, in the order they are present in the list, until it finds a
2004 file with the desired name. @b{Note that the executable search path is
2005 @i{not} used for this purpose. Neither is the current working
2006 directory, unless it happens to be in the source path.}
2007
2008 @kindex directory
2009 When you start GDB, its source path contains just the current working
2010 directory. To add other directories, use the @samp{directory} command.
2011
2012 @table @code
2013 @item directory @var{dirnames...}
2014 Add directory @var{dirname} to the end of the source path. Several
2015 directory names may be given to this command, separated by whitespace or
2016 @samp{:}.
2017
2018 @item directory
2019 Reset the source path to just the current working directory of GDB.
2020 This requires confirmation.
2021
2022 Since this command deletes directories from the search path, it may
2023 change the directory in which a previously read source file will be
2024 discovered. To make this work correctly, this command also clears out
2025 the tables GDB maintains about the source files it has already found.
2026
2027 @item info directories
2028 @kindex info directories
2029 Print the source path: show which directories it contains.
2030 @end table
2031
2032 Because the @samp{directory} command adds to the end of the source path,
2033 it does not affect any file that GDB has already found. If the source
2034 path contains directories that you do not want, and these directories
2035 contain misleading files with names matching your source files, the
2036 way to correct the situation is as follows:
2037
2038 @enumerate
2039 @item
2040 Choose the directory you want at the beginning of the source path.
2041 Use the @samp{cd} command to make that the current working directory.
2042
2043 @item
2044 Use @samp{directory} with no argument to reset the source path to just
2045 that directory.
2046
2047 @item
2048 Use @samp{directory} with suitable arguments to add any other
2049 directories you want in the source path.
2050 @end enumerate
2051
2052 @node Data, Symbols, Source, Top
2053 @chapter Examining Data
2054
2055 @cindex printing data
2056 @cindex examining data
2057 @kindex print
2058 The usual way to examine data in your program is with the @samp{print}
2059 command (abbreviated @samp{p}). It evaluates and prints the value of any
2060 valid expression of the language the program is written in (for now, C).
2061 You type
2062
2063 @example
2064 print @var{exp}
2065 @end example
2066
2067 @noindent
2068 where @var{exp} is any valid expression, and the value of @var{exp}
2069 is printed in a format appropriate to its data type.
2070
2071 A more low-level way of examining data is with the @samp{x} command.
2072 It examines data in memory at a specified address and prints it in a
2073 specified format.
2074
2075 @menu
2076 * Expressions:: Expressions that can be computed and printed.
2077 * Variables:: Using your program's variables in expressions.
2078 * Assignment:: Setting your program's variables.
2079 * Arrays:: Examining part of memory as an array.
2080 * Format Options:: Controlling how structures and arrays are printed.
2081 * Output formats:: Specifying formats for printing values.
2082 * Memory:: Examining memory explicitly.
2083 * Auto Display:: Printing certain expressions whenever program stops.
2084 * Value History:: Referring to values previously printed.
2085 * Convenience Vars:: Giving names to values for future reference.
2086 * Registers:: Referring to and storing in machine registers.
2087 @end menu
2088
2089 @node Expressions, Variables, Data, Data
2090 @section Expressions
2091
2092 @cindex expressions
2093 Many different GDB commands accept an expression and compute its value.
2094 Any kind of constant, variable or operator defined by the programming
2095 language you are using is legal in an expression in GDB. This includes
2096 conditional expressions, function calls, casts and string constants.
2097 It unfortunately does not include symbols defined by preprocessor
2098 @code{#define} commands.
2099
2100 Casts are supported in all languages, not just in C, because it is so
2101 useful to cast a number into a pointer so as to examine a structure
2102 at that address in memory.
2103
2104 GDB supports three kinds of operator in addition to those of programming
2105 languages:
2106
2107 @table @code
2108 @item @@
2109 @samp{@@} is a binary operator for treating parts of memory as arrays.
2110 @xref{Arrays}, for more information.
2111
2112 @item ::
2113 @samp{::} allows you to specify a variable in terms of the file or
2114 function it is defined in. @xref{Variables}.
2115
2116 @item @{@var{type}@} @var{addr}
2117 Refers to an object of type @var{type} stored at address @var{addr} in
2118 memory. @var{addr} may be any expression whose value is an integer or
2119 pointer (but parentheses are required around nonunary operators, just as in
2120 a cast). This construct is allowed regardless of what kind of data is
2121 officially supposed to reside at @var{addr}.@refill
2122 @end table
2123
2124 @node Variables, Arrays, Expressions, Data
2125 @section Program Variables
2126
2127 The most common kind of expression to use is the name of a variable
2128 in your program.
2129
2130 Variables in expressions are understood in the selected stack frame
2131 (@pxref{Selection}); they must either be global (or static) or be visible
2132 according to the scope rules of the programming language from the point of
2133 execution in that frame. This means that in the function
2134
2135 @example
2136 foo (a)
2137 int a;
2138 @{
2139 bar (a);
2140 @{
2141 int b = test ();
2142 bar (b);
2143 @}
2144 @}
2145 @end example
2146
2147 @noindent
2148 the variable @code{a} is usable whenever the program is executing
2149 within the function @code{foo}, but the variable @code{b} is visible
2150 only while the program is executing inside the block in which @code{b}
2151 is declared.
2152
2153 As a special exception, you can refer to a variable or function whose
2154 scope is a single source file even if the current execution point is not
2155 in this file. But it is possible to have more than one such variable
2156 or function with the same name (if they are in different source files).
2157 In such a case, it is not defined which one you will get. If you wish,
2158 you can specify any one of them using the colon-colon construct:
2159
2160 @example
2161 @var{block}::@var{variable}
2162 @end example
2163
2164 @noindent
2165 Here @var{block} is the name of the source file whose variable you want.
2166
2167 @node Arrays, Format options, Variables, Data
2168 @section Artificial Arrays
2169
2170 @cindex artificial array
2171 It is often useful to print out several successive objects of the
2172 same type in memory; a section of an array, or an array of
2173 dynamically determined size for which only a pointer exists in the
2174 program.
2175
2176 This can be done by constructing an @dfn{artificial array} with the
2177 binary operator @samp{@@}. The left operand of @samp{@@} should be
2178 the first element of the desired array, as an individual object.
2179 The right operand should be the length of the array. The result is
2180 an array value whose elements are all of the type of the left argument.
2181 The first element is actually the left argument; the second element
2182 comes from bytes of memory immediately following those that hold the
2183 first element, and so on. Here is an example. If a program says
2184
2185 @example
2186 int *array = (int *) malloc (len * sizeof (int));
2187 @end example
2188
2189 @noindent
2190 you can print the contents of @code{array} with
2191
2192 @example
2193 p *array@@len
2194 @end example
2195
2196 The left operand of @samp{@@} must reside in memory. Array values made
2197 with @samp{@@} in this way behave just like other arrays in terms of
2198 subscripting, and are coerced to pointers when used in expressions.
2199 (It would probably appear in an expression via the value history,
2200 after you had printed it out.)
2201
2202 @node Format options, Output formats, Arrays, Data
2203 @section Format options
2204
2205 @cindex format options
2206 GDB provides a few ways to control how arrays and structures are
2207 printed.
2208
2209 @table @code
2210 @item info format
2211 @kindex info format
2212 Display the current settings for the format options.
2213
2214 @item set array-max @var{number-of-elements}
2215 @kindex set array-max
2216 If GDB is printing a large array, it will stop printing after it has
2217 printed the number of elements set by the @samp{set array-max} command.
2218 This limit also applies to the display of strings.
2219
2220 @item set prettyprint on
2221 @kindex set prettyprint
2222 Cause GDB to print structures in an indented format with one member per
2223 line, like this:
2224
2225 @example
2226 $1 = @{
2227 next = 0x0,
2228 flags = @{
2229 sweet = 1,
2230 sour = 1
2231 @},
2232 meat = 0x54 "Pork"
2233 @}
2234 @end example
2235
2236 @item set prettyprint off
2237 Cause GDB to print structures in a compact format, like this:
2238
2239 @example
2240 $1 = @{next = 0x0, flags = @{sweet = 1, sour = 1@}, meat = 0x54 "Pork"@}
2241 @end example
2242
2243 This is the default format.
2244
2245 @item set unionprint on
2246 @kindex set unionprint
2247 Tell GDB to print unions which are contained in structures. This is the
2248 default setting.
2249 @item set unionprint off
2250 Tell GDB not to print unions which are contained in structures.
2251
2252 For example, given the declarations
2253
2254 @example
2255 typedef enum @{Tree, Bug@} Species;
2256 typedef enum @{Big_tree, Acorn, Seedling@} Tree_forms;
2257 typedef enum @{Caterpiller, Cocoon, Butterfly@} Bug_forms;
2258
2259 struct thing @{
2260 Species it;
2261 union @{
2262 Tree_forms tree;
2263 Bug_forms bug;
2264 @} form;
2265 @};
2266
2267 struct thing foo = @{Tree, @{Acorn@}@};
2268 @end example
2269
2270 @noindent
2271 with @samp{set unionprint on} in effect @samp{p foo} would print
2272
2273 @example
2274 $1 = @{it = Tree, form = @{tree = Acorn, bug = Cocoon@}@}
2275 @end example
2276
2277 @noindent
2278 and with @samp{set unionprint off} in effect it would print
2279
2280 @example
2281 $1 = @{it = Tree, form = @{...@}@}
2282 @end example
2283 @end table
2284
2285 @node Output formats, Memory, Format options, Data
2286 @section Output formats
2287
2288 @cindex formatted output
2289 @cindex output formats
2290 GDB normally prints all values according to their data types. Sometimes
2291 this is not what you want. For example, you might want to print a number
2292 in hex, or a pointer in decimal. Or you might want to view data in memory
2293 at a certain address as a character string or an instruction. These things
2294 can be done with @dfn{output formats}.
2295
2296 The simplest use of output formats is to say how to print a value
2297 already computed. This is done by starting the arguments of the
2298 @samp{print} command with a slash and a format letter. The format
2299 letters supported are:
2300
2301 @table @samp
2302 @item x
2303 Regard the bits of the value as an integer, and print the integer in
2304 hexadecimal.
2305
2306 @item d
2307 Print as integer in signed decimal.
2308
2309 @item u
2310 Print as integer in unsigned decimal.
2311
2312 @item o
2313 Print as integer in octal.
2314
2315 @item a
2316 Print as an address, both absolute in hex and then relative
2317 to a symbol defined as an address below it.
2318
2319 @item c
2320 Regard as an integer and print it as a character constant.
2321
2322 @item f
2323 Regard the bits of the value as a floating point number and print
2324 using typical floating point syntax.
2325 @end table
2326
2327 For example, to print the program counter in hex (@pxref{Registers}), type
2328
2329 @example
2330 p/x $pc
2331 @end example
2332
2333 @noindent
2334 Note that no space is required before the slash; this is because command
2335 names in GDB cannot contain a slash.
2336
2337 To reprint the last value in the value history with a different format,
2338 you can use the @samp{print} command with just a format and no
2339 expression. For example, @samp{p/x} reprints the last value in hex.
2340
2341 @node Memory, Auto Display, Output formats, Data
2342 @subsection Examining Memory
2343
2344 @cindex examining memory
2345 @kindex x
2346 The command @samp{x} (for `examine') can be used to examine memory
2347 without reference to the program's data types. The format in which you
2348 wish to examine memory is instead explicitly specified. The allowable
2349 formats are a superset of the formats described in the previous section.
2350
2351 @samp{x} is followed by a slash and an output format specification,
2352 followed by an expression for an address. The expression need not have
2353 a pointer value (though it may); it is used as an integer, as the
2354 address of a byte of memory. @xref{Expressions} for more information on
2355 expressions. For example, @samp{x/4xw $sp} prints the four words of
2356 memory above the stack pointer in hexadecimal.
2357
2358 The output format in this case specifies both how big a unit of memory
2359 to examine and how to print the contents of that unit. It is done
2360 with one or two of the following letters:
2361
2362 These letters specify just the size of unit to examine:
2363
2364 @table @samp
2365 @item b
2366 Examine individual bytes.
2367
2368 @item h
2369 Examine halfwords (two bytes each).
2370
2371 @item w
2372 Examine words (four bytes each).
2373
2374 @cindex word
2375 Many assemblers and cpu designers still use `word' for a 16-bit quantity,
2376 as a holdover from specific predecessor machines of the 1970's that really
2377 did use two-byte words. But more generally the term `word' has always
2378 referred to the size of quantity that a machine normally operates on and
2379 stores in its registers. This is 32 bits for all the machines that GDB
2380 runs on.
2381
2382 @item g
2383 Examine giant words (8 bytes).
2384 @end table
2385
2386 These letters specify just the way to print the contents:
2387
2388 @table @samp
2389 @item x
2390 Print as integers in unsigned hexadecimal.
2391
2392 @item d
2393 Print as integers in signed decimal.
2394
2395 @item u
2396 Print as integers in unsigned decimal.
2397
2398 @item o
2399 Print as integers in unsigned octal.
2400
2401 @item a
2402 Print as an address, both absolute in hex and then relative
2403 to a symbol defined as an address below it.
2404
2405 @item c
2406 Print as character constants.
2407
2408 @item f
2409 Print as floating point. This works only with sizes @samp{w} and
2410 @samp{g}.
2411
2412 @item s
2413 Print a null-terminated string of characters. The specified unit size
2414 is ignored; instead, the unit is however many bytes it takes to reach
2415 a null character (including the null character).
2416
2417 @item i
2418 Print a machine instruction in assembler syntax (or nearly). The
2419 specified unit size is ignored; the number of bytes in an instruction
2420 varies depending on the type of machine, the opcode and the addressing
2421 modes used.
2422 @end table
2423
2424 If either the manner of printing or the size of unit fails to be specified,
2425 the default is to use the same one that was used last. If you don't want
2426 to use any letters after the slash, you can omit the slash as well.
2427
2428 You can also omit the address to examine. Then the address used is
2429 just after the last unit examined. This is why string and instruction
2430 formats actually compute a unit-size based on the data: so that the
2431 next string or instruction examined will start in the right place.
2432 The @samp{print} command sometimes sets the default address for
2433 the @samp{x} command; when the value printed resides in memory, the
2434 default is set to examine the same location. @samp{info line} also
2435 sets the default for @samp{x}, to the address of the start of the
2436 machine code for the specified line and @samp{info breakpoints} sets
2437 it to the address of the last breakpoint listed.
2438
2439 When you use @key{RET} to repeat an @samp{x} command, it does not repeat
2440 exactly the same: the address specified previously (if any) is ignored, so
2441 that the repeated command examines the successive locations in memory
2442 rather than the same ones.
2443
2444 You can examine several consecutive units of memory with one command by
2445 writing a repeat-count after the slash (before the format letters, if any).
2446 The repeat count must be a decimal integer. It has the same effect as
2447 repeating the @samp{x} command that many times except that the output may
2448 be more compact with several units per line. For example,
2449
2450 @example
2451 x/10i $pc
2452 @end example
2453
2454 @noindent
2455 prints ten instructions starting with the one to be executed next in the
2456 selected frame. After doing this, you could print another ten following
2457 instructions with
2458
2459 @example
2460 x/10
2461 @end example
2462
2463 @noindent
2464 in which the format and address are allowed to default.
2465
2466 @kindex $_
2467 @kindex $__
2468 The addresses and contents printed by the @samp{x} command are not put in
2469 the value history because there is often too much of them and they would
2470 get in the way. Instead, GDB makes these values available for subsequent
2471 use in expressions as values of the convenience variables @code{$_} and
2472 @code{$__}.
2473
2474 After an @samp{x} command, the last address examined is available for use
2475 in expressions in the convenience variable @code{$_}. The contents of that
2476 address, as examined, are available in the convenience variable @code{$__}.
2477
2478 If the @samp{x} command has a repeat count, the address and contents saved
2479 are from the last memory unit printed; this is not the same as the last
2480 address printed if several units were printed on the last line of output.
2481
2482 @kindex disassemble
2483 The specialized command @samp{disassemble} is also provided to dump a
2484 range of memory as machine instructions. The default memory range is
2485 the function surrounding the program counter of the selected frame. A
2486 single argument to this command is a program counter value; the function
2487 surrounding this value will be dumped. Two arguments (separated by one
2488 or more spaces) specify a range of addresss (first inclusive, second
2489 exclusive) to be dumped.
2490
2491 @node Auto Display, Value History, Memory, Data
2492 @section Automatic Display
2493 @cindex automatic display
2494 @cindex display of expressions
2495
2496 If you find that you want to print the value of an expression frequently
2497 (to see how it changes), you might want to add it to the @dfn{automatic
2498 display list} so that GDB will print its value each time the program stops.
2499 Each expression added to the list is given a number to identify it;
2500 to remove an expression from the list, you specify that number.
2501 The automatic display looks like this:
2502
2503 @example
2504 2: foo = 38
2505 3: bar[5] = (struct hack *) 0x3804
2506 @end example
2507
2508 @noindent
2509 showing item numbers, expressions and their current values.
2510
2511 If the expression refers to local variables, then it does not make sense
2512 outside the lexical context for which it was set up. Such an expression
2513 is printed only when execution is inside that lexical context. For
2514 example, if you give the command @samp{display name} while inside a
2515 function with an argument @code{name}, then this argument will be
2516 displayed whenever the program stops inside that function, but not when
2517 it stops elsewhere (since this argument doesn't exist elsewhere).
2518
2519 @table @code
2520 @item display @var{exp}
2521 @kindex display
2522 Add the expression @var{exp} to the list of expressions to display
2523 each time the program stops. @xref{Expressions}.
2524
2525 @item display/@var{fmt} @var{exp}
2526 For @var{fmt} specifying only a display format and not a size or
2527 count, add the expression @var{exp} to the auto-display list but
2528 arranges to display it each time in the specified format @var{fmt}.
2529
2530 @item display/@var{fmt} @var{addr}
2531 For @var{fmt} @samp{i} or @samp{s}, or including a unit-size or a
2532 number of units, add the expression @var{addr} as a memory address to
2533 be examined each time the program stops. Examining means in effect
2534 doing @samp{x/@var{fmt} @var{addr}}. @xref{Memory}.
2535
2536 @item undisplay @var{dnums}@dots{}
2537 @itemx delete display @var{dnums}@dots{}
2538 @kindex delete display
2539 @kindex undisplay
2540 Remove item numbers @var{dnums} from the list of expressions to display.
2541
2542 @item disable display @var{dnums}@dots{}
2543 @kindex disable display
2544 Disable the display of item numbers @var{dnums}. A disabled display
2545 item is not printed automatically, but is not forgotten. It may be
2546 reenabled later.
2547
2548 @item enable display @var{dnums}@dots{}
2549 @kindex enable display
2550 Enable display of item numbers @var{dnums}. It becomes effective once
2551 again in auto display of its expression, until you specify otherwise.
2552
2553 @item display
2554 Display the current values of the expressions on the list, just as is
2555 done when the program stops.
2556
2557 @item info display
2558 @kindex info display
2559 Print the list of expressions previously set up to display
2560 automatically, each one with its item number, but without showing the
2561 values. This includes disabled expressions, which are marked as such.
2562 It also includes expressions which would not be displayed right now
2563 because they refer to automatic variables not currently available.
2564 @end table
2565
2566 @node Value History, Convenience Vars, Auto Display, Data
2567 @section Value History
2568
2569 @cindex value history
2570 Every value printed by the @samp{print} command is saved for the entire
2571 session in GDB's @dfn{value history} so that you can refer to it in
2572 other expressions.
2573
2574 @cindex @code{$}
2575 @cindex @code{$$}
2576 @cindex history number
2577 The values printed are given @dfn{history numbers} for you to refer to them
2578 by. These are successive integers starting with 1. @samp{print} shows you
2579 the history number assigned to a value by printing @samp{$@var{num} = }
2580 before the value; here @var{num} is the history number.
2581
2582 To refer to any previous value, use @samp{$} followed by the value's
2583 history number. The output printed by @samp{print} is designed to remind
2584 you of this. Just @code{$} refers to the most recent value in the history,
2585 and @code{$$} refers to the value before that.
2586
2587 For example, suppose you have just printed a pointer to a structure and
2588 want to see the contents of the structure. It suffices to type
2589
2590 @example
2591 p *$
2592 @end example
2593
2594 If you have a chain of structures where the component @samp{next} points
2595 to the next one, you can print the contents of the next one with this:
2596
2597 @example
2598 p *$.next
2599 @end example
2600
2601 @noindent
2602 It might be useful to repeat this command many times by typing @key{RET}.
2603
2604 Note that the history records values, not expressions. If the value of
2605 @code{x} is 4 and you type this command:
2606
2607 @example
2608 print x
2609 set x=5
2610 @end example
2611
2612 @noindent
2613 then the value recorded in the value history by the @samp{print} command
2614 remains 4 even though the value of @code{x} has changed.
2615
2616 @table @code
2617 @item info values
2618 @kindex info values
2619 Print the last ten values in the value history, with their item
2620 numbers. This is like @samp{p $$9} repeated ten times, except that
2621 @samp{info values} does not change the history.
2622
2623 @item info values @var{n}
2624 Print ten history values centered on history item number @var{n}.
2625
2626 @item info values +
2627 Print ten history values just after the values last printed.
2628 @end table
2629
2630 @node Convenience Vars, Registers, Value History, Data
2631 @section Convenience Variables
2632
2633 @cindex convenience variables
2634 GDB provides @dfn{convenience variables} that you can use within GDB to
2635 hold on to a value and refer to it later. These variables exist entirely
2636 within GDB; they are not part of your program, and setting a convenience
2637 variable has no effect on further execution of your program. That's why
2638 you can use them freely.
2639
2640 Convenience variables have names starting with @samp{$}. Any name starting
2641 with @samp{$} can be used for a convenience variable, unless it is one of
2642 the predefined set of register names (@pxref{Registers}).
2643
2644 You can save a value in a convenience variable with an assignment
2645 expression, just as you would set a variable in your program. Example:
2646
2647 @example
2648 set $foo = *object_ptr
2649 @end example
2650
2651 @noindent
2652 would save in @code{$foo} the value contained in the object pointed to by
2653 @code{object_ptr}.
2654
2655 Using a convenience variable for the first time creates it; but its value
2656 is @code{void} until you assign a new value. You can alter the value with
2657 another assignment at any time.
2658
2659 Convenience variables have no fixed types. You can assign a convenience
2660 variable any type of value, even if it already has a value of a different
2661 type. The convenience variable as an expression has whatever type its
2662 current value has.
2663
2664 @table @code
2665 @item info convenience
2666 @kindex info convenience
2667 Print a list of convenience variables used so far, and their values.
2668 Abbreviated @samp{i con}.
2669 @end table
2670
2671 One of the ways to use a convenience variable is as a counter to be
2672 incremented or a pointer to be advanced. For example:
2673
2674 @example
2675 set $i = 0
2676 print bar[$i++]->contents
2677 @i{@dots{}repeat that command by typing @key{RET}.}
2678 @end example
2679
2680 Some convenience variables are created automatically by GDB and given
2681 values likely to be useful.
2682
2683 @table @code
2684 @item $_
2685 The variable @code{$_} is automatically set by the @samp{x} command to
2686 the last address examined (@pxref{Memory}). Other commands which
2687 provide a default address for @samp{x} to examine also set @code{$_}
2688 to that address; these commands include @samp{info line} and @samp{info
2689 breakpoint}.
2690
2691 @item $__
2692 The variable @code{$__} is automatically set by the @samp{x} command
2693 to the value found in the last address examined.
2694 @end table
2695
2696 @node Registers,, Convenience Vars, Data
2697 @section Registers
2698
2699 @cindex registers
2700 Machine register contents can be referred to in expressions as variables
2701 with names starting with @samp{$}. The names of registers are different
2702 for each machine; use @samp{info registers} to see the names used on your
2703 machine. The names @code{$pc} and @code{$sp} are used on all machines for
2704 the program counter register and the stack pointer. Often @code{$fp} is
2705 used for a register that contains a pointer to the current stack frame,
2706 and @code{$ps} is used for a register that contains the processor
2707 status. These standard register names may be available on your machine
2708 even though the @code{info registers} command displays them with a
2709 different name. For example, on the SPARC, @code{info registers}
2710 displays the processor status register as @code{$psr} but you can also
2711 refer to it as @code{$ps}.
2712
2713 GDB always considers the contents of an ordinary register as an integer
2714 when the register is examined in this way. Some machines have special
2715 registers which can hold nothing but floating point; these registers are
2716 considered floating point. There is no way to refer to the contents of an
2717 ordinary register as floating point value (although you can @emph{print}
2718 it as a floating point value with @samp{print/f $@var{regname}}).
2719
2720 Some registers have distinct ``raw'' and ``virtual'' data formats. This
2721 means that the data format in which the register contents are saved by the
2722 operating system is not the same one that your program normally sees. For
2723 example, the registers of the 68881 floating point coprocessor are always
2724 saved in ``extended'' format, but all C programs expect to work with
2725 ``double'' format. In such cases, GDB normally works with the virtual
2726 format only (the format that makes sense for your program), but the
2727 @samp{info registers} command prints the data in both formats.
2728
2729 Register values are relative to the selected stack frame
2730 (@pxref{Selection}). This means that you get the value that the register
2731 would contain if all stack frames farther in were exited and their saved
2732 registers restored. In order to see the real contents of all registers,
2733 you must select the innermost frame (with @samp{frame 0}).
2734
2735 Some registers are never saved (typically those numbered zero or one)
2736 because they are used for returning function values; for these registers,
2737 relativization makes no difference.
2738
2739 @table @code
2740 @item info registers
2741 @kindex info registers
2742 Print the names and relativized values of all registers.
2743
2744 @item info registers @var{regname}
2745 Print the relativized value of register @var{regname}. @var{regname}
2746 may be any register name valid on the machine you are using, with
2747 or without the initial @samp{$}.
2748 @end table
2749
2750 @subsection Examples
2751
2752 You could print the program counter in hex with
2753
2754 @example
2755 p/x $pc
2756 @end example
2757
2758 @noindent
2759 or print the instruction to be executed next with
2760
2761 @example
2762 x/i $pc
2763 @end example
2764
2765 @noindent
2766 or add four to the stack pointer with
2767
2768 @example
2769 set $sp += 4
2770 @end example
2771
2772 @noindent
2773 The last is a way of removing one word from the stack, on machines where
2774 stacks grow downward in memory (most machines, nowadays). This assumes
2775 that the innermost stack frame is selected. Setting @code{$sp} is
2776 not allowed when other stack frames are selected.
2777
2778 @node Symbols, Altering, Data, Top
2779 @chapter Examining the Symbol Table
2780
2781 The commands described in this section allow you to make inquiries for
2782 information about the symbols (names of variables, functions and types)
2783 defined in your program. This information is found by GDB in the symbol
2784 table loaded by the @samp{symbol-file} command; it is inherent in the text
2785 of your program and does not change as the program executes.
2786
2787 @table @code
2788 @item info address @var{symbol}
2789 @kindex info address
2790 Describe where the data for @var{symbol} is stored. For a register
2791 variable, this says which register it is kept in. For a non-register
2792 local variable, this prints the stack-frame offset at which the variable
2793 is always stored.
2794
2795 Note the contrast with @samp{print &@var{symbol}}, which does not work
2796 at all for a register variables, and for a stack local variable prints
2797 the exact address of the current instantiation of the variable.
2798
2799 @item whatis @var{exp}
2800 @kindex whatis
2801 Print the data type of expression @var{exp}. @var{exp} is not
2802 actually evaluated, and any side-effecting operations (such as
2803 assignments or function calls) inside it do not take place.
2804 @xref{Expressions}.
2805
2806 @item whatis
2807 Print the data type of @code{$}, the last value in the value history.
2808
2809 @item ptype @var{typename}
2810 @kindex ptype
2811 Print a description of data type @var{typename}. @var{typename} may be
2812 the name of a type, or for C code it may have the form
2813 @samp{struct @var{struct-tag}}, @samp{union @var{union-tag}} or
2814 @samp{enum @var{enum-tag}}.@refill
2815
2816 @item ptype @var{exp}
2817 Print a description of the type of expression @var{exp}. This is like
2818 @samp{whatis} except it prints a detailed description, instead of just
2819 the name of the type. For example, if the type of a variable is
2820 @samp{struct complex @{double real; double imag;@}}, @samp{whatis} will
2821 print @samp{struct complex} and @samp{ptype} will print @samp{struct
2822 complex @{double real; double imag;@}}
2823
2824 @item info sources
2825 @kindex info sources
2826 Print the names of all source files in the program for which there
2827 is debugging information.
2828
2829 @item info functions
2830 @kindex info functions
2831 Print the names and data types of all defined functions.
2832
2833 @item info functions @var{regexp}
2834 Print the names and data types of all defined functions
2835 whose names contain a match for regular expression @var{regexp}.
2836 Thus, @samp{info fun step} finds all functions whose names
2837 include @samp{step}; @samp{info fun ^step} finds those whose names
2838 start with @samp{step}.
2839
2840 @item info variables
2841 @kindex info variables
2842 Print the names and data types of all variables that are declared
2843 outside of functions (i.e., except for local variables).
2844
2845 @item info variables @var{regexp}
2846 Print the names and data types of all variables (except for local
2847 variables) whose names contain a match for regular expression
2848 @var{regexp}.
2849
2850 @ignore
2851 Uncomment this only if the implementation is fixed.
2852 @item info types
2853 @kindex info types
2854 Print all data types that are defined in the program.
2855
2856 @item info types @var{regexp}
2857 Print all data types that are defined in the program whose names
2858 contain a match for regular expression @var{regexp}.
2859 @end ignore
2860
2861 @ignore
2862 This was never implemented.
2863 @item info methods
2864 @itemx info methods @var{regexp}
2865 @kindex info methods
2866 The @samp{info-methods} command permits the user to examine all defined
2867 methods within C++ program, or (with the @var{regexp} argument) a
2868 specific set of methods found in the various C++ classes. Many
2869 C++ classes provide a large number of methods. Thus, the output
2870 from the @samp{ptype} command can be overwhelming and hard to use. The
2871 @samp{info-methods} command filters the methods, printing only those
2872 which match the regular-expression @var{regexp}.
2873 @end ignore
2874
2875 @item printsyms @var{filename}
2876 @kindex printsyms
2877 Write a complete dump of the debugger's symbol data into the
2878 file @var{filename}.
2879 @end table
2880
2881 @node Altering, Sequences, Symbols, Top
2882 @chapter Altering Execution
2883
2884 Once you think you have find an error in the program, you might want to
2885 find out for certain whether correcting the apparent error would lead to
2886 correct results in the rest of the run. You can find the answer by
2887 experiment, using the GDB features for altering execution of the
2888 program.
2889
2890 For example, you can store new values into variables or memory
2891 locations, give the program a signal, restart it at a different address,
2892 or even return prematurely from a function to its caller.
2893
2894 @menu
2895 * Assignment:: Altering variable values or memory contents.
2896 * Jumping:: Altering control flow.
2897 * Signaling:: Making signals happen in the program.
2898 * Returning:: Making a function return prematurely.
2899 @end menu
2900
2901 @node Assignment, Jumping, Altering, Altering
2902 @section Assignment to Variables
2903
2904 @cindex assignment
2905 @cindex setting variables
2906 To alter the value of a variable, evaluate an assignment expression.
2907 @xref{Expressions}. For example,
2908
2909 @example
2910 print x=4
2911 @end example
2912
2913 @noindent
2914 would store the value 4 into the variable @code{x}, and then print
2915 the value of the assignment expression (which is 4).
2916
2917 All the assignment operators of C are supported, including the
2918 incrementation operators @samp{++} and @samp{--}, and combining
2919 assignments such as @samp{+=} and @samp{<<=}.
2920
2921 @kindex set
2922 @kindex set variable
2923 If you are not interested in seeing the value of the assignment, use the
2924 @samp{set} command instead of the @samp{print} command. @samp{set} is
2925 really the same as @samp{print} except that the expression's value is not
2926 printed and is not put in the value history (@pxref{Value History}). The
2927 expression is evaluated only for side effects.
2928
2929 Note that if the beginning of the argument string of the @samp{set} command
2930 appears identical to a @samp{set} subcommand, it may be necessary to use
2931 the @samp{set variable} command. This command is identical to @samp{set}
2932 except for its lack of subcommands.
2933
2934 GDB allows more implicit conversions in assignments than C does; you can
2935 freely store an integer value into a pointer variable or vice versa, and
2936 any structure can be converted to any other structure that is the same
2937 length or shorter.
2938
2939 To store values into arbitrary places in memory, use the @samp{@{@dots{}@}}
2940 construct to generate a value of specified type at a specified address
2941 (@pxref{Expressions}). For example, @code{@{int@}0x83040} would refer
2942 to memory location 0x83040 as an integer (which implies a certain size
2943 and representation in memory), and
2944
2945 @example
2946 set @{int@}0x83040 = 4
2947 @end example
2948
2949 would store the value 4 into that memory location.
2950
2951 @node Jumping, Signaling, Assignment, Altering
2952 @section Continuing at a Different Address
2953
2954 Ordinarily, when you continue the program, you do so at the place where
2955 it stopped, with the @samp{cont} command. You can instead continue at
2956 an address of your own choosing, with the following commands:
2957
2958 @table @code
2959 @item jump @var{linenum}
2960 @kindex jump
2961 Resume execution at line number @var{linenum}. Execution may stop
2962 immediately if there is a breakpoint there.
2963
2964 The @samp{jump} command does not change the current stack frame, or
2965 the stack pointer, or the contents of any memory location or any
2966 register other than the program counter. If line @var{linenum} is in
2967 a different function from the one currently executing, the results may
2968 be bizarre if the two functions expect different patterns of arguments or
2969 of local variables. For this reason, the @samp{jump} command requests
2970 confirmation if the specified line is not in the function currently
2971 executing. However, even bizarre results are predictable based on
2972 careful study of the machine-language code of the program.
2973
2974 @item jump *@var{address}
2975 Resume execution at the instruction at address @var{address}.
2976 @end table
2977
2978 You can get much the same effect as the @code{jump} command by storing a
2979 new value into the register @code{$pc}. The difference is that this
2980 does not start the program running; it only changes the address where it
2981 @emph{will} run when it is continued. For example,
2982
2983 @example
2984 set $pc = 0x485
2985 @end example
2986
2987 @noindent
2988 causes the next @samp{cont} command or stepping command to execute at
2989 address 0x485, rather than at the address where the program stopped.
2990 @xref{Stepping}.
2991
2992 The most common occasion to use the @samp{jump} command is when you have
2993 stepped across a function call with @code{next}, and found that the
2994 return value is incorrect. If all the relevant data appeared correct
2995 before the function call, the error is probably in the function that
2996 just returned.
2997
2998 In general, your next step would now be to rerun the program and execute
2999 up to this function call, and then step into it to see where it goes
3000 astray. But this may be time consuming. If the function did not have
3001 significant side effects, you could get the same information by resuming
3002 execution just before the function call and stepping through it. To do this,
3003 first put a breakpoint on that function; then, use the @samp{jump} command
3004 to continue on the line with the function call.
3005
3006 @node Signaling, Returning, Jumping, Altering
3007 @section Giving the Program a Signal
3008
3009 @table @code
3010 @item signal @var{signalnum}
3011 @kindex signal
3012 Resume execution where the program stopped, but give it immediately the
3013 signal number @var{signalnum}.
3014
3015 Alternatively, if @var{signalnum} is zero, continue execution without
3016 giving a signal. This is useful when the program stopped on account of
3017 a signal and would ordinary see the signal when resumed with the
3018 @samp{cont} command; @samp{signal 0} causes it to resume without a
3019 signal.
3020 @end table
3021
3022 @node Returning,, Signaling, Altering
3023 @section Returning from a Function
3024
3025 @cindex returning from a function
3026 @kindex return
3027 You can cancel execution of a function call with the @samp{return}
3028 command. This command has the effect of discarding the selected stack
3029 frame (and all frames within it), so that control moves to the caller of
3030 that function. You can think of this as making the discarded frame
3031 return prematurely.
3032
3033 First select the stack frame that you wish to return from
3034 (@pxref{Selection}). Then type the @samp{return} command. If you wish
3035 to specify the value to be returned, give that as an argument.
3036
3037 This pops the selected stack frame (and any other frames inside of it),
3038 leaving its caller as the innermost remaining frame. That frame becomes
3039 selected. The specified value is stored in the registers used for
3040 returning values of functions.
3041
3042 The @samp{return} command does not resume execution; it leaves the
3043 program stopped in the state that would exist if the function had just
3044 returned. Contrast this with the @samp{finish} command
3045 (@pxref{Stepping}), which resumes execution until the selected stack
3046 frame returns @emph{naturally}.
3047
3048 @node Sequences, Options, Altering, Top
3049 @chapter Canned Sequences of Commands
3050
3051 GDB provides two ways to store sequences of commands for execution as a
3052 unit: user-defined commands and command files.
3053
3054 @menu
3055 * Define:: User-defined commands.
3056 * Command Files:: Command files.
3057 * Output:: Controlled output commands useful in
3058 user-defined commands and command files.
3059 @end menu
3060
3061 @node Define, Command Files, Sequences, Sequences
3062 @section User-Defined Commands
3063
3064 @cindex user-defined command
3065 A @dfn{user-defined command} is a sequence of GDB commands to which you
3066 assign a new name as a command. This is done with the @samp{define}
3067 command.
3068
3069 @table @code
3070 @item define @var{commandname}
3071 @kindex define
3072 Define a command named @var{commandname}. If there is already a command
3073 by that name, you are asked to confirm that you want to redefine it.
3074
3075 The definition of the command is made up of other GDB command lines,
3076 which are given following the @samp{define} command. The end of these
3077 commands is marked by a line containing @samp{end}.
3078
3079 @item document @var{commandname}
3080 @kindex document
3081 Give documentation to the user-defined command @var{commandname}. The
3082 command @var{commandname} must already be defined. This command reads
3083 lines of documentation just as @samp{define} reads the lines of the
3084 command definition, ending with @samp{end}. After the @samp{document}
3085 command is finished, @samp{help} on command @var{commandname} will print
3086 the documentation you have specified.
3087
3088 You may use the @samp{document} command again to change the
3089 documentation of a command. Redefining the command with @samp{define}
3090 does not change the documentation.
3091 @end table
3092
3093 User-defined commands do not take arguments. When they are executed, the
3094 commands of the definition are not printed. An error in any command
3095 stops execution of the user-defined command.
3096
3097 Commands that would ask for confirmation if used interactively proceed
3098 without asking when used inside a user-defined command. Many GDB commands
3099 that normally print messages to say what they are doing omit the messages
3100 when used in user-defined command.
3101
3102 @node Command Files, Output, Define, Sequences
3103 @section Command Files
3104
3105 @cindex command files
3106 A command file for GDB is a file of lines that are GDB commands. Comments
3107 (lines starting with @samp{#}) may also be included. An empty line in a
3108 command file does nothing; it does not mean to repeat the last command, as
3109 it would from the terminal.
3110
3111 @cindex init file
3112 @cindex @file{.gdbinit}
3113 When GDB starts, it automatically executes its @dfn{init files}, command
3114 files named @file{.gdbinit}. GDB reads the init file (if any) in your home
3115 directory and then the init file (if any) in the current working
3116 directory. (The init files are not executed if the @samp{-nx} option
3117 is given.) You can also request the execution of a command file with the
3118 @samp{source} command:
3119
3120 @table @code
3121 @item source @var{filename}
3122 @kindex source
3123 Execute the command file @var{filename}.
3124 @end table
3125
3126 The lines in a command file are executed sequentially. They are not
3127 printed as they are executed. An error in any command terminates execution
3128 of the command file.
3129
3130 Commands that would ask for confirmation if used interactively proceed
3131 without asking when used in a command file. Many GDB commands that
3132 normally print messages to say what they are doing omit the messages
3133 when used in a command file.
3134
3135 @node Output,, Command Files, Sequences
3136 @section Commands for Controlled Output
3137
3138 During the execution of a command file or a user-defined command, the only
3139 output that appears is what is explicitly printed by the commands of the
3140 definition. This section describes three commands useful for generating
3141 exactly the output you want.
3142
3143 @table @code
3144 @item echo @var{text}
3145 @kindex echo
3146 @comment I don't consider backslash-space a standard C escape sequence
3147 @comment because it's not in ANSI.
3148 Print @var{text}. Nonprinting characters can be included in @var{text}
3149 using C escape sequences, such as @samp{\n} to print a newline. @b{No
3150 newline will be printed unless you specify one.} In addition to the
3151 standard C escape sequences a backslash followed by a space stands for a
3152 space. This is useful for outputting a string with spaces at the
3153 beginning or the end, since leading and trailing spaces are trimmed from
3154 all arguments. Thus, to print @w{`` and foo = ''}, use the command
3155 @w{``echo \ and foo = \ ''}.
3156 @comment AAARGGG! How am I supposed to do @samp{ and foo = } and not
3157 @comment have the spaces be invisible in TeX?
3158
3159 A backslash at the end of @var{text} can be used, as in C, to continue
3160 the command onto subsequent lines. For example,
3161
3162 @example
3163 echo This is some text\n\
3164 which is continued\n\
3165 onto several lines.\n
3166 @end example
3167
3168 produces the same output as
3169
3170 @example
3171 echo This is some text\n
3172 echo which is continued\n
3173 echo onto several lines.\n
3174 @end example
3175
3176 @item output @var{expression}
3177 @kindex output
3178 Print the value of @var{expression} and nothing but that value: no
3179 newlines, no @samp{$@var{nn} = }. The value is not entered in the
3180 value history either. @xref{Expressions} for more information on
3181 expressions.
3182
3183 @item output/@var{fmt} @var{expression}
3184 Print the value of @var{expression} in format @var{fmt}.
3185 @xref{Output formats}, for more information.
3186
3187 @item printf @var{string}, @var{expressions}@dots{}
3188 @kindex printf
3189 Print the values of the @var{expressions} under the control of
3190 @var{string}. The @var{expressions} are separated by commas and may
3191 be either numbers or pointers. Their values are printed as specified
3192 by @var{string}, exactly as if the program were to execute
3193
3194 @example
3195 printf (@var{string}, @var{expressions}@dots{});
3196 @end example
3197
3198 For example, you can print two values in hex like this:
3199
3200 @example
3201 printf "foo, bar-foo = 0x%x, 0x%x\n", foo, bar-foo
3202 @end example
3203
3204 The only backslash-escape sequences that you can use in the string are
3205 the simple ones that consist of backslash followed by a letter.
3206 @end table
3207
3208 @node Options, Emacs, Sequences, Top
3209 @chapter Options and Arguments for GDB
3210
3211 When you invoke GDB, you can specify arguments telling it what files to
3212 operate on and what other things to do.
3213
3214 @menu
3215 * Mode Options:: Options controlling modes of operation.
3216 * File Options:: Options to specify files (executable, coredump, commands)
3217 * Other Arguments:: Any other arguments without options
3218 also specify files.
3219 @end menu
3220
3221 @node Mode Options, File Options, Options, Options
3222 @section Mode Options
3223
3224 @table @samp
3225 @item -nx
3226 Do not execute commands from the init files @file{.gdbinit}.
3227 Normally, the commands in these files are executed after all the
3228 command options and arguments have been processed. @xref{Command
3229 Files}.
3230
3231 @item -q
3232 ``Quiet''. Do not print the usual introductory messages.
3233
3234 @item -batch
3235 Run in batch mode. Exit with code 0 after processing all the command
3236 files specified with @samp{-x} (and @file{.gdbinit}, if not inhibited).
3237 Exit with nonzero status if an error occurs in executing the GDB
3238 commands in the command files.
3239
3240 @item -fullname
3241 This option is used when Emacs runs GDB as a subprocess. It tells GDB
3242 to output the full file name and line number in a standard,
3243 recognizable fashion each time a stack frame is displayed (which
3244 includes each time the program stops). This recognizable format looks
3245 like two @samp{\032} characters, followed by the file name, line number
3246 and character position separated by colons, and a newline. The
3247 Emacs-to-GDB interface program uses the two @samp{\032} characters as
3248 a signal to display the source code for the frame.
3249 @end table
3250
3251 @node File Options, Other Arguments, Mode Options, Options
3252 @section File-specifying Options
3253
3254 All the options and command line arguments given are processed
3255 in sequential order. The order makes a difference when the
3256 @samp{-x} option is used.
3257
3258 @table @samp
3259 @item -s @var{file}
3260 Read symbol table from file @var{file}.
3261
3262 @item -e @var{file}
3263 Use file @var{file} as the executable file to execute when
3264 appropriate, and for examining pure data in conjunction with a core
3265 dump.
3266
3267 @item -se @var{file}
3268 Read symbol table from file @var{file} and use it as the executable
3269 file.
3270
3271 @item -c @var{file}
3272 Use file @var{file} as a core dump to examine.
3273
3274 @item -x @var{file}
3275 Execute GDB commands from file @var{file}.
3276
3277 @item -d @var{directory}
3278 Add @var{directory} to the path to search for source files.
3279 @end table
3280
3281 @node Other Arguments,, File Options, Options
3282 @section Other Arguments
3283
3284 If there are arguments to GDB that are not options or associated with
3285 options, the first one specifies the symbol table and executable file name
3286 (as if it were preceded by @samp{-se}) and the second one specifies a core
3287 dump file name (as if it were preceded by @samp{-c}).
3288
3289 @node Emacs, Remote, Options, Top
3290 @chapter Using GDB under GNU Emacs
3291
3292 A special interface allows you to use GNU Emacs to view (and
3293 edit) the source files for the program you are debugging with
3294 GDB.
3295
3296 To use this interface, use the command @kbd{M-x gdb} in Emacs. Give the
3297 executable file you want to debug as an argument. This command starts
3298 GDB as a subprocess of Emacs, with input and output through a newly
3299 created Emacs buffer.
3300
3301 Using GDB under Emacs is just like using GDB normally except for two
3302 things:
3303
3304 @itemize @bullet
3305 @item
3306 All ``terminal'' input and output goes through the Emacs buffer. This
3307 applies both to GDB commands and their output, and to the input and
3308 output done by the program you are debugging.
3309
3310 This is useful because it means that you can copy the text of previous
3311 commands and input them again; you can even use parts of the output
3312 in this way.
3313
3314 All the facilities of Emacs's Shell mode are available for this purpose.
3315
3316 @item
3317 GDB displays source code through Emacs. Each time GDB displays a
3318 stack frame, Emacs automatically finds the source file for that frame
3319 and puts an arrow (@samp{=>}) at the left margin of the current line.
3320
3321 Explicit GDB @samp{list} or search commands still produce output as
3322 usual, but you probably will have no reason to use them.
3323 @end itemize
3324
3325 In the GDB I/O buffer, you can use these special Emacs commands:
3326
3327 @table @kbd
3328 @item M-s
3329 Execute to another source line, like the GDB @samp{step} command.
3330
3331 @item M-n
3332 Execute to next source line in this function, skipping all function
3333 calls, like the GDB @samp{next} command.
3334
3335 @item M-i
3336 Execute one instruction, like the GDB @samp{stepi} command.
3337
3338 @item C-c C-f
3339 Execute until exit from the selected stack frame, like the GDB
3340 @samp{finish} command.
3341
3342 @item M-c
3343 @comment C-c C-p in emacs 19
3344 Continue execution of the program, like the GDB @samp{cont} command.
3345
3346 @item M-u
3347 @comment C-c C-u in emacs 19
3348 Go up the number of frames indicated by the numeric argument
3349 (@pxref{Arguments, , Numeric Arguments, emacs, The GNU Emacs Manual}),
3350 like the GDB @samp{up} command.@refill
3351
3352 @item M-d
3353 @comment C-c C-d in emacs 19
3354 Go down the number of frames indicated by the numeric argument, like the
3355 GDB @samp{down} command.
3356 @end table
3357
3358 In any source file, the Emacs command @kbd{C-x SPC} (@code{gdb-break})
3359 tells GDB to set a breakpoint on the source line point is on.
3360
3361 The source files displayed in Emacs are in ordinary Emacs buffers
3362 which are visiting the source files in the usual way. You can edit
3363 the files with these buffers if you wish; but keep in mind that GDB
3364 communicates with Emacs in terms of line numbers. If you add or
3365 delete lines from the text, the line numbers that GDB knows will cease
3366 to correspond properly to the code.
3367
3368 @node Remote, Commands, Emacs, Top
3369 @chapter Remote Kernel Debugging
3370
3371 If you are trying to debug a program running on a machine that can't run
3372 GDB in the usual way, it is often useful to use remote debugging. For
3373 example, you might be debugging an operating system kernel, or debugging
3374 a small system which does not have a general purpose operating system
3375 powerful enough to run a full-featured debugger. Currently GDB supports
3376 remote debugging over a serial connection.
3377
3378 The program to be debugged on the remote machine needs to contain a
3379 debugging device driver which talks to GDB over the serial line using the
3380 protocol described below. The same version of GDB that is used ordinarily
3381 can be used for this. Several sample remote debugging drivers are
3382 distributed with GDB; see the @file{README} file in the GDB distribution for
3383 more information.
3384
3385 @menu
3386 * Remote Commands:: Commands used to start and finish remote debugging.
3387 @end menu
3388
3389 For details of the communication protocol, see the comments in the GDB
3390 source file @file{remote.c}.
3391
3392 @node Remote Commands,, Remote, Remote
3393 @section Commands for Remote Debugging
3394
3395 To start remote debugging, first run GDB and specify as an executable file
3396 the program that is running in the remote machine. This tells GDB how
3397 to find the program's symbols and the contents of its pure text. Then
3398 establish communication using the @samp{attach} command with a device
3399 name rather than a pid as an argument. For example:
3400
3401 @example
3402 attach /dev/ttyd
3403 @end example
3404
3405 @noindent
3406 if the serial line is connected to the device named @file{/dev/ttyd}. This
3407 will stop the remote machine if it is not already stopped.
3408
3409 Now you can use all the usual commands to examine and change data and to
3410 step and continue the remote program.
3411
3412 To resume the remote program and stop debugging it, use the @samp{detach}
3413 command.
3414
3415 @ignore
3416 This material will be merged in when better Readline documentation
3417 is done.
3418
3419 @node GDB Readline, History Top ,Readline Top, Command Editing
3420 @subsection GDB Readline
3421
3422 You may control the behavior of command line editing in GDB with the
3423 following commands:
3424
3425 @table @code
3426 @kindex set editing
3427 @item set editing
3428 @itemx set editing on
3429 Enable command line editing (enabled by default).
3430
3431 @item set editing off
3432 Disable command line editing.
3433
3434 @kindex set history file
3435 @item set history file @var{filename}
3436 Set the name of the GDB command history file to @var{filename}. This is
3437 the file from which GDB will read an initial command history
3438 list or to which it will write this list when it exits. This list is
3439 accessed through history expansion or through the history
3440 command editing characters listed below. This file defaults to the
3441 value of the environmental variable @code{GDBHISTFILE}, or to
3442 @code{./.gdb_history} if this variable is not set.
3443
3444 @kindex set history write
3445 @item set history write
3446 @itemx set history write on
3447 Enable the writing of the command history to the command history file
3448 named above. This is enabled by default.
3449
3450 @item set history write off
3451 Disable the writing of the command history to the command history file.
3452
3453 @kindex set history size
3454 @item set history size @var{size}
3455 Set the number of commands which GDB will keep in its history list.
3456 This defaults to the value of the environmental variable
3457 @code{HISTSIZE}, or to 256 if this variable is not set.
3458
3459 @kindex info editing
3460 @item info editing
3461 Display the current settings relating to command line editing, and also
3462 display the last ten commands in the command history.
3463
3464 @item info editing @var{n}
3465 Print ten commands centered on command number @var{n}.
3466
3467 @item info editing +
3468 Print ten commands just after the commands last printed.
3469 @end table
3470
3471 @node GDB History, , History Top, Command editing
3472 @comment node-name, next, previous, up
3473 Note that because of the additional meaning of @code{!} to GDB (as the
3474 logical not operator in C), history expansion is off by default. If you
3475 decide to enable history expansion with the @samp{set history expansion
3476 on} command, you will need to follow @samp{!} with a space or a tab to
3477 prevent it from being expanded.
3478
3479 The commands to control history expansion are:
3480
3481 @table @code
3482
3483 @kindex set history expansion
3484 @item set history expansion on
3485 @itemx set history expansion
3486 Enable history expansion.
3487
3488 @item set history expansion off
3489 Disable history expansion. History expansion is off by default.
3490
3491 @end table
3492 @end ignore
3493
3494 @node Commands, Concepts, Remote, Top
3495 @unnumbered Command Index
3496
3497 @printindex ky
3498
3499 @node Concepts, , Commands, Top
3500 @unnumbered Concept Index
3501
3502 @printindex cp
3503
3504 @contents
3505 @bye
3506
3507
3508
3509
3510 Occasionally it is useful to execute a shell command from within GDB.
3511 This can be done with the @samp{shell} command.
3512
3513 @table @code
3514 @item shell @var{shell command string}
3515 @kindex shell
3516 @cindex shell escape
3517 Directs GDB to invoke an inferior shell to execute @var{shell command string}.
3518 The environment variable @code{SHELL} is used if it exists, otherwise GDB
3519 uses @samp{/bin/sh}.
3520 @end table
This page took 0.107664 seconds and 4 git commands to generate.