1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<!-- Copyright (C) 1988-2021 Free Software Foundation, Inc.
4
5Permission is granted to copy, distribute and/or modify this document
6under the terms of the GNU Free Documentation License, Version 1.3 or
7any later version published by the Free Software Foundation; with the
8Invariant Sections being "Free Software" and "Free Software Needs
9Free Documentation", with the Front-Cover Texts being "A GNU Manual,"
10and with the Back-Cover Texts as in (a) below.
11
12(a) The FSF's Back-Cover Text is: "You are free to copy and modify
13this GNU Manual.  Buying copies from GNU Press supports the FSF in
14developing GNU and promoting software freedom." -->
15<!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ -->
16<head>
17<title>Debugging with GDB: gdb man</title>
18
19<meta name="description" content="Debugging with GDB: gdb man">
20<meta name="keywords" content="Debugging with GDB: gdb man">
21<meta name="resource-type" content="document">
22<meta name="distribution" content="global">
23<meta name="Generator" content="makeinfo">
24<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
25<link href="index.html#Top" rel="start" title="Top">
26<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
27<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
28<link href="Man-Pages.html#Man-Pages" rel="up" title="Man Pages">
29<link href="gdbserver-man.html#gdbserver-man" rel="next" title="gdbserver man">
30<link href="Man-Pages.html#Man-Pages" rel="previous" title="Man Pages">
31<style type="text/css">
32<!--
33a.summary-letter {text-decoration: none}
34blockquote.smallquotation {font-size: smaller}
35div.display {margin-left: 3.2em}
36div.example {margin-left: 3.2em}
37div.indentedblock {margin-left: 3.2em}
38div.lisp {margin-left: 3.2em}
39div.smalldisplay {margin-left: 3.2em}
40div.smallexample {margin-left: 3.2em}
41div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
42div.smalllisp {margin-left: 3.2em}
43kbd {font-style:oblique}
44pre.display {font-family: inherit}
45pre.format {font-family: inherit}
46pre.menu-comment {font-family: serif}
47pre.menu-preformatted {font-family: serif}
48pre.smalldisplay {font-family: inherit; font-size: smaller}
49pre.smallexample {font-size: smaller}
50pre.smallformat {font-family: inherit; font-size: smaller}
51pre.smalllisp {font-size: smaller}
52span.nocodebreak {white-space:nowrap}
53span.nolinebreak {white-space:nowrap}
54span.roman {font-family:serif; font-weight:normal}
55span.sansserif {font-family:sans-serif; font-weight:normal}
56ul.no-bullet {list-style: none}
57-->
58</style>
59
60
61</head>
62
63<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
64<a name="gdb-man"></a>
65<div class="header">
66<p>
67Next: <a href="gdbserver-man.html#gdbserver-man" accesskey="n" rel="next">gdbserver man</a>, Up: <a href="Man-Pages.html#Man-Pages" accesskey="u" rel="up">Man Pages</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
68</div>
69<hr>
70<h4 class="node-heading">gdb man</h4>
71<a name="gdb-man-1"></a>
72<h3 class="heading">gdb man</h3>
73
74
75<p>gdb [<samp>-help</samp>] [<samp>-nh</samp>] [<samp>-nx</samp>] [<samp>-q</samp>]
76[<samp>-batch</samp>] [<samp>-cd=</samp><var>dir</var>] [<samp>-f</samp>]
77[<samp>-b</samp>&nbsp;<!-- /@w --><var>bps</var>]
78    [<samp>-tty=</samp><var>dev</var>] [<samp>-s</samp> <var>symfile</var>]
79[<samp>-e</samp>&nbsp;<!-- /@w --><var>prog</var>] [<samp>-se</samp>&nbsp;<!-- /@w --><var>prog</var>]
80[<samp>-c</samp>&nbsp;<!-- /@w --><var>core</var>] [<samp>-p</samp>&nbsp;<!-- /@w --><var>procID</var>]
81    [<samp>-x</samp>&nbsp;<!-- /@w --><var>cmds</var>] [<samp>-d</samp>&nbsp;<!-- /@w --><var>dir</var>]
82[<var>prog</var>|<var>prog</var> <var>procID</var>|<var>prog</var> <var>core</var>]
83</p>
84<p>The purpose of a debugger such as <small>GDB</small> is to allow you to see what is
85going on &ldquo;inside&rdquo; another program while it executes &ndash; or what another
86program was doing at the moment it crashed.
87</p>
88<p><small>GDB</small> can do four main kinds of things (plus other things in support of
89these) to help you catch bugs in the act:
90</p>
91<ul>
92<li> Start your program, specifying anything that might affect its behavior.
93
94</li><li> Make your program stop on specified conditions.
95
96</li><li> Examine what has happened, when your program has stopped.
97
98</li><li> Change things in your program, so you can experiment with correcting the
99effects of one bug and go on to learn about another.
100</li></ul>
101
102<p>You can use <small>GDB</small> to debug programs written in C, C<tt>++</tt>, Fortran and
103Modula-2.
104</p>
105<p><small>GDB</small> is invoked with the shell command <code>gdb</code>.  Once started, it reads
106commands from the terminal until you tell it to exit with the <small>GDB</small>
107command <code>quit</code>.  You can get online help from <small>GDB</small> itself
108by using the command <code>help</code>.
109</p>
110<p>You can run <code>gdb</code> with no arguments or options; but the most
111usual way to start <small>GDB</small> is with one argument or two, specifying an
112executable program as the argument:
113</p>
114<div class="smallexample">
115<pre class="smallexample">gdb program
116</pre></div>
117
118<p>You can also start with both an executable program and a core file specified:
119</p>
120<div class="smallexample">
121<pre class="smallexample">gdb program core
122</pre></div>
123
124<p>You can, instead, specify a process ID as a second argument or use option
125<code>-p</code>, if you want to debug a running process:
126</p>
127<div class="smallexample">
128<pre class="smallexample">gdb program 1234
129gdb -p 1234
130</pre></div>
131
132<p>would attach <small>GDB</small> to process <code>1234</code>.  With option <samp>-p</samp> you
133can omit the <var>program</var> filename.
134</p>
135<p>Here are some of the most frequently needed <small>GDB</small> commands:
136</p>
137<dl compact="compact">
138<dt><code>break [<var>file</var>:]<var>function</var></code></dt>
139<dd><p>Set a breakpoint at <var>function</var> (in <var>file</var>).
140</p>
141</dd>
142<dt><code>run [<var>arglist</var>]</code></dt>
143<dd><p>Start your program (with <var>arglist</var>, if specified).
144</p>
145</dd>
146<dt><code>bt</code></dt>
147<dd><p>Backtrace: display the program stack.
148</p>
149</dd>
150<dt><code>print <var>expr</var></code></dt>
151<dd><p>Display the value of an expression.
152</p>
153</dd>
154<dt><code>c</code></dt>
155<dd><p>Continue running your program (after stopping, e.g. at a breakpoint).
156</p>
157</dd>
158<dt><code>next</code></dt>
159<dd><p>Execute next program line (after stopping); step <em>over</em> any
160function calls in the line.
161</p>
162</dd>
163<dt><code>edit [<var>file</var>:]<var>function</var></code></dt>
164<dd><p>look at the program line where it is presently stopped.
165</p>
166</dd>
167<dt><code>list [<var>file</var>:]<var>function</var></code></dt>
168<dd><p>type the text of the program in the vicinity of where it is presently stopped.
169</p>
170</dd>
171<dt><code>step</code></dt>
172<dd><p>Execute next program line (after stopping); step <em>into</em> any
173function calls in the line.
174</p>
175</dd>
176<dt><code>help [<var>name</var>]</code></dt>
177<dd><p>Show information about <small>GDB</small> command <var>name</var>, or general information
178about using <small>GDB</small>.
179</p>
180</dd>
181<dt><code>quit</code></dt>
182<dd><p>Exit from <small>GDB</small>.
183</p></dd>
184</dl>
185
186
187<p>Any arguments other than options specify an executable
188file and core file (or process ID); that is, the first argument
189encountered with no
190associated option flag is equivalent to a <samp>-se</samp> option, and the second,
191if any, is equivalent to a <samp>-c</samp> option if it&rsquo;s the name of a file.
192Many options have
193both long and short forms; both are shown here.  The long forms are also
194recognized if you truncate them, so long as enough of the option is
195present to be unambiguous.  (If you prefer, you can flag option
196arguments with <samp>+</samp> rather than <samp>-</samp>, though we illustrate the
197more usual convention.)
198</p>
199<p>All the options and command line arguments you give are processed
200in sequential order.  The order makes a difference when the <samp>-x</samp>
201option is used.
202</p>
203<dl compact="compact">
204<dt><code>-help</code></dt>
205<dt><code>-h</code></dt>
206<dd><p>List all options, with brief explanations.
207</p>
208</dd>
209<dt><code>-symbols=<var>file</var></code></dt>
210<dt><code>-s <var>file</var></code></dt>
211<dd><p>Read symbol table from file <var>file</var>.
212</p>
213</dd>
214<dt><code>-write</code></dt>
215<dd><p>Enable writing into executable and core files.
216</p>
217</dd>
218<dt><code>-exec=<var>file</var></code></dt>
219<dt><code>-e <var>file</var></code></dt>
220<dd><p>Use file <var>file</var> as the executable file to execute when
221appropriate, and for examining pure data in conjunction with a core
222dump.
223</p>
224</dd>
225<dt><code>-se=<var>file</var></code></dt>
226<dd><p>Read symbol table from file <var>file</var> and use it as the executable
227file.
228</p>
229</dd>
230<dt><code>-core=<var>file</var></code></dt>
231<dt><code>-c <var>file</var></code></dt>
232<dd><p>Use file <var>file</var> as a core dump to examine.
233</p>
234</dd>
235<dt><code>-command=<var>file</var></code></dt>
236<dt><code>-x <var>file</var></code></dt>
237<dd><p>Execute <small>GDB</small> commands from file <var>file</var>.
238</p>
239</dd>
240<dt><code>-ex <var>command</var></code></dt>
241<dd><p>Execute given <small>GDB</small> <var>command</var>.
242</p>
243</dd>
244<dt><code>-directory=<var>directory</var></code></dt>
245<dt><code>-d <var>directory</var></code></dt>
246<dd><p>Add <var>directory</var> to the path to search for source files.
247</p>
248</dd>
249<dt><code>-nh</code></dt>
250<dd><p>Do not execute commands from <samp>~/.gdbinit</samp>.
251</p>
252</dd>
253<dt><code>-nx</code></dt>
254<dt><code>-n</code></dt>
255<dd><p>Do not execute commands from any <samp>.gdbinit</samp> initialization files.
256</p>
257</dd>
258<dt><code>-quiet</code></dt>
259<dt><code>-q</code></dt>
260<dd><p>&ldquo;Quiet&rdquo;.  Do not print the introductory and copyright messages.  These
261messages are also suppressed in batch mode.
262</p>
263</dd>
264<dt><code>-batch</code></dt>
265<dd><p>Run in batch mode.  Exit with status <code>0</code> after processing all the command
266files specified with <samp>-x</samp> (and <samp>.gdbinit</samp>, if not inhibited).
267Exit with nonzero status if an error occurs in executing the <small>GDB</small>
268commands in the command files.
269</p>
270<p>Batch mode may be useful for running <small>GDB</small> as a filter, for example to
271download and run a program on another computer; in order to make this
272more useful, the message
273</p>
274<div class="smallexample">
275<pre class="smallexample">Program exited normally.
276</pre></div>
277
278<p>(which is ordinarily issued whenever a program running under <small>GDB</small> control
279terminates) is not issued when running in batch mode.
280</p>
281</dd>
282<dt><code>-cd=<var>directory</var></code></dt>
283<dd><p>Run <small>GDB</small> using <var>directory</var> as its working directory,
284instead of the current directory.
285</p>
286</dd>
287<dt><code>-fullname</code></dt>
288<dt><code>-f</code></dt>
289<dd><p>Emacs sets this option when it runs <small>GDB</small> as a subprocess.  It tells
290<small>GDB</small> to output the full file name and line number in a standard,
291recognizable fashion each time a stack frame is displayed (which
292includes each time the program stops).  This recognizable format looks
293like two &lsquo;<samp>\032</samp>&rsquo; characters, followed by the file name, line number
294and character position separated by colons, and a newline.  The
295Emacs-to-<small>GDB</small> interface program uses the two &lsquo;<samp>\032</samp>&rsquo;
296characters as a signal to display the source code for the frame.
297</p>
298</dd>
299<dt><code>-b <var>bps</var></code></dt>
300<dd><p>Set the line speed (baud rate or bits per second) of any serial
301interface used by <small>GDB</small> for remote debugging.
302</p>
303</dd>
304<dt><code>-tty=<var>device</var></code></dt>
305<dd><p>Run using <var>device</var> for your program&rsquo;s standard input and output.
306</p></dd>
307</dl>
308
309
310<hr>
311<div class="header">
312<p>
313Next: <a href="gdbserver-man.html#gdbserver-man" accesskey="n" rel="next">gdbserver man</a>, Up: <a href="Man-Pages.html#Man-Pages" accesskey="u" rel="up">Man Pages</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
314</div>
315
316
317
318</body>
319</html>
320