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: Command Files</title>
18
19<meta name="description" content="Debugging with GDB: Command Files">
20<meta name="keywords" content="Debugging with GDB: Command Files">
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="Sequences.html#Sequences" rel="up" title="Sequences">
29<link href="Output.html#Output" rel="next" title="Output">
30<link href="Hooks.html#Hooks" rel="previous" title="Hooks">
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="Command-Files"></a>
65<div class="header">
66<p>
67Next: <a href="Output.html#Output" accesskey="n" rel="next">Output</a>, Previous: <a href="Hooks.html#Hooks" accesskey="p" rel="previous">Hooks</a>, Up: <a href="Sequences.html#Sequences" accesskey="u" rel="up">Sequences</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<a name="Command-Files-1"></a>
71<h4 class="subsection">23.1.3 Command Files</h4>
72
73<a name="index-command-files"></a>
74<a name="index-scripting-commands"></a>
75<p>A command file for <small>GDB</small> is a text file made of lines that are
76<small>GDB</small> commands.  Comments (lines starting with <kbd>#</kbd>) may
77also be included.  An empty line in a command file does nothing; it
78does not mean to repeat the last command, as it would from the
79terminal.
80</p>
81<p>You can request the execution of a command file with the <code>source</code>
82command.  Note that the <code>source</code> command is also used to evaluate
83scripts that are not Command Files.  The exact behavior can be configured
84using the <code>script-extension</code> setting.
85See <a href="Extending-GDB.html#Extending-GDB">Extending GDB</a>.
86</p>
87<dl compact="compact">
88<dd><a name="index-source"></a>
89<a name="index-execute-commands-from-a-file"></a>
90</dd>
91<dt><code>source [-s] [-v] <var>filename</var></code></dt>
92<dd><p>Execute the command file <var>filename</var>.
93</p></dd>
94</dl>
95
96<p>The lines in a command file are generally executed sequentially,
97unless the order of execution is changed by one of the
98<em>flow-control commands</em> described below.  The commands are not
99printed as they are executed.  An error in any command terminates
100execution of the command file and control is returned to the console.
101</p>
102<p><small>GDB</small> first searches for <var>filename</var> in the current directory.
103If the file is not found there, and <var>filename</var> does not specify a
104directory, then <small>GDB</small> also looks for the file on the source search path
105(specified with the &lsquo;<samp>directory</samp>&rsquo; command);
106except that <samp>$cdir</samp> is not searched because the compilation directory
107is not relevant to scripts.
108</p>
109<p>If <code>-s</code> is specified, then <small>GDB</small> searches for <var>filename</var>
110on the search path even if <var>filename</var> specifies a directory.
111The search is done by appending <var>filename</var> to each element of the
112search path.  So, for example, if <var>filename</var> is <samp>mylib/myscript</samp>
113and the search path contains <samp>/home/user</samp> then <small>GDB</small> will
114look for the script <samp>/home/user/mylib/myscript</samp>.
115The search is also done if <var>filename</var> is an absolute path.
116For example, if <var>filename</var> is <samp>/tmp/myscript</samp> and
117the search path contains <samp>/home/user</samp> then <small>GDB</small> will
118look for the script <samp>/home/user/tmp/myscript</samp>.
119For DOS-like systems, if <var>filename</var> contains a drive specification,
120it is stripped before concatenation.  For example, if <var>filename</var> is
121<samp>d:myscript</samp> and the search path contains <samp>c:/tmp</samp> then <small>GDB</small>
122will look for the script <samp>c:/tmp/myscript</samp>.
123</p>
124<p>If <code>-v</code>, for verbose mode, is given then <small>GDB</small> displays
125each command as it is executed.  The option must be given before
126<var>filename</var>, and is interpreted as part of the filename anywhere else.
127</p>
128<p>Commands that would ask for confirmation if used interactively proceed
129without asking when used in a command file.  Many <small>GDB</small> commands that
130normally print messages to say what they are doing omit the messages
131when called from command files.
132</p>
133<p><small>GDB</small> also accepts command input from standard input.  In this
134mode, normal output goes to standard output and error output goes to
135standard error.  Errors in a command file supplied on standard input do
136not terminate execution of the command file&mdash;execution continues with
137the next command.
138</p>
139<div class="smallexample">
140<pre class="smallexample">gdb &lt; cmds &gt; log 2&gt;&amp;1
141</pre></div>
142
143<p>(The syntax above will vary depending on the shell used.) This example
144will execute commands from the file <samp>cmds</samp>. All output and errors
145would be directed to <samp>log</samp>.
146</p>
147<p>Since commands stored on command files tend to be more general than
148commands typed interactively, they frequently need to deal with
149complicated situations, such as different or unexpected values of
150variables and symbols, changes in how the program being debugged is
151built, etc.  <small>GDB</small> provides a set of flow-control commands to
152deal with these complexities.  Using these commands, you can write
153complex scripts that loop over data structures, execute commands
154conditionally, etc.
155</p>
156<dl compact="compact">
157<dd><a name="index-if"></a>
158<a name="index-else"></a>
159</dd>
160<dt><code>if</code></dt>
161<dt><code>else</code></dt>
162<dd><p>This command allows to include in your script conditionally executed
163commands. The <code>if</code> command takes a single argument, which is an
164expression to evaluate.  It is followed by a series of commands that
165are executed only if the expression is true (its value is nonzero).
166There can then optionally be an <code>else</code> line, followed by a series
167of commands that are only executed if the expression was false.  The
168end of the list is marked by a line containing <code>end</code>.
169</p>
170<a name="index-while"></a>
171</dd>
172<dt><code>while</code></dt>
173<dd><p>This command allows to write loops.  Its syntax is similar to
174<code>if</code>: the command takes a single argument, which is an expression
175to evaluate, and must be followed by the commands to execute, one per
176line, terminated by an <code>end</code>.  These commands are called the
177<em>body</em> of the loop.  The commands in the body of <code>while</code> are
178executed repeatedly as long as the expression evaluates to true.
179</p>
180<a name="index-loop_005fbreak"></a>
181</dd>
182<dt><code>loop_break</code></dt>
183<dd><p>This command exits the <code>while</code> loop in whose body it is included.
184Execution of the script continues after that <code>while</code>s <code>end</code>
185line.
186</p>
187<a name="index-loop_005fcontinue"></a>
188</dd>
189<dt><code>loop_continue</code></dt>
190<dd><p>This command skips the execution of the rest of the body of commands
191in the <code>while</code> loop in whose body it is included.  Execution
192branches to the beginning of the <code>while</code> loop, where it evaluates
193the controlling expression.
194</p>
195<a name="index-end-_0028if_002felse_002fwhile-commands_0029"></a>
196</dd>
197<dt><code>end</code></dt>
198<dd><p>Terminate the block of commands that are the body of <code>if</code>,
199<code>else</code>, or <code>while</code> flow-control commands.
200</p></dd>
201</dl>
202
203
204<hr>
205<div class="header">
206<p>
207Next: <a href="Output.html#Output" accesskey="n" rel="next">Output</a>, Previous: <a href="Hooks.html#Hooks" accesskey="p" rel="previous">Hooks</a>, Up: <a href="Sequences.html#Sequences" accesskey="u" rel="up">Sequences</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>
208</div>
209
210
211
212</body>
213</html>
214