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: Starting</title> 18 19<meta name="description" content="Debugging with GDB: Starting"> 20<meta name="keywords" content="Debugging with GDB: Starting"> 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="Running.html#Running" rel="up" title="Running"> 29<link href="Arguments.html#Arguments" rel="next" title="Arguments"> 30<link href="Compilation.html#Compilation" rel="previous" title="Compilation"> 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="Starting"></a> 65<div class="header"> 66<p> 67Next: <a href="Arguments.html#Arguments" accesskey="n" rel="next">Arguments</a>, Previous: <a href="Compilation.html#Compilation" accesskey="p" rel="previous">Compilation</a>, Up: <a href="Running.html#Running" accesskey="u" rel="up">Running</a> [<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="Starting-Your-Program"></a> 71<h3 class="section">4.2 Starting Your Program</h3> 72<a name="index-starting"></a> 73<a name="index-running"></a> 74 75<dl compact="compact"> 76<dd><a name="index-run"></a> 77<a name="index-r-_0028run_0029"></a> 78</dd> 79<dt><code>run</code></dt> 80<dt><code>r</code></dt> 81<dd><p>Use the <code>run</code> command to start your program under <small>GDB</small>. 82You must first specify the program name with an argument to 83<small>GDB</small> (see <a href="Invocation.html#Invocation">Getting In and Out of 84<small>GDB</small></a>), or by using the <code>file</code> or <code>exec-file</code> 85command (see <a href="Files.html#Files">Commands to Specify Files</a>). 86</p> 87</dd> 88</dl> 89 90<p>If you are running your program in an execution environment that 91supports processes, <code>run</code> creates an inferior process and makes 92that process run your program. In some environments without processes, 93<code>run</code> jumps to the start of your program. Other targets, 94like ‘<samp>remote</samp>’, are always running. If you get an error 95message like this one: 96</p> 97<div class="smallexample"> 98<pre class="smallexample">The "remote" target does not support "run". 99Try "help target" or "continue". 100</pre></div> 101 102<p>then use <code>continue</code> to run your program. You may need <code>load</code> 103first (see <a href="Target-Commands.html#load">load</a>). 104</p> 105<p>The execution of a program is affected by certain information it 106receives from its superior. <small>GDB</small> provides ways to specify this 107information, which you must do <em>before</em> starting your program. (You 108can change it after starting your program, but such changes only affect 109your program the next time you start it.) This information may be 110divided into four categories: 111</p> 112<dl compact="compact"> 113<dt>The <em>arguments.</em></dt> 114<dd><p>Specify the arguments to give your program as the arguments of the 115<code>run</code> command. If a shell is available on your target, the shell 116is used to pass the arguments, so that you may use normal conventions 117(such as wildcard expansion or variable substitution) in describing 118the arguments. 119In Unix systems, you can control which shell is used with the 120<code>SHELL</code> environment variable. If you do not define <code>SHELL</code>, 121<small>GDB</small> uses the default shell (<samp>/bin/sh</samp>). You can disable 122use of any shell with the <code>set startup-with-shell</code> command (see 123below for details). 124</p> 125</dd> 126<dt>The <em>environment.</em></dt> 127<dd><p>Your program normally inherits its environment from <small>GDB</small>, but you can 128use the <small>GDB</small> commands <code>set environment</code> and <code>unset 129environment</code> to change parts of the environment that affect 130your program. See <a href="Environment.html#Environment">Your Program’s Environment</a>. 131</p> 132</dd> 133<dt>The <em>working directory.</em></dt> 134<dd><p>You can set your program’s working directory with the command 135<kbd>set cwd</kbd>. If you do not set any working directory with this 136command, your program will inherit <small>GDB</small>’s working directory if 137native debugging, or the remote server’s working directory if remote 138debugging. See <a href="Working-Directory.html#Working-Directory">Your Program’s Working 139Directory</a>. 140</p> 141</dd> 142<dt>The <em>standard input and output.</em></dt> 143<dd><p>Your program normally uses the same device for standard input and 144standard output as <small>GDB</small> is using. You can redirect input and output 145in the <code>run</code> command line, or you can use the <code>tty</code> command to 146set a different device for your program. 147See <a href="Input_002fOutput.html#Input_002fOutput">Your Program’s Input and Output</a>. 148</p> 149<a name="index-pipes"></a> 150<p><em>Warning:</em> While input and output redirection work, you cannot use 151pipes to pass the output of the program you are debugging to another 152program; if you attempt this, <small>GDB</small> is likely to wind up debugging the 153wrong program. 154</p></dd> 155</dl> 156 157<p>When you issue the <code>run</code> command, your program begins to execute 158immediately. See <a href="Stopping.html#Stopping">Stopping and Continuing</a>, for discussion 159of how to arrange for your program to stop. Once your program has 160stopped, you may call functions in your program, using the <code>print</code> 161or <code>call</code> commands. See <a href="Data.html#Data">Examining Data</a>. 162</p> 163<p>If the modification time of your symbol file has changed since the last 164time <small>GDB</small> read its symbols, <small>GDB</small> discards its symbol 165table, and reads it again. When it does this, <small>GDB</small> tries to retain 166your current breakpoints. 167</p> 168<dl compact="compact"> 169<dd><a name="index-start"></a> 170</dd> 171<dt><code>start</code></dt> 172<dd><a name="index-run-to-main-procedure"></a> 173<p>The name of the main procedure can vary from language to language. 174With C or C<tt>++</tt>, the main procedure name is always <code>main</code>, but 175other languages such as Ada do not require a specific name for their 176main procedure. The debugger provides a convenient way to start the 177execution of the program and to stop at the beginning of the main 178procedure, depending on the language used. 179</p> 180<p>The ‘<samp>start</samp>’ command does the equivalent of setting a temporary 181breakpoint at the beginning of the main procedure and then invoking 182the ‘<samp>run</samp>’ command. 183</p> 184<a name="index-elaboration-phase"></a> 185<p>Some programs contain an <em>elaboration</em> phase where some startup code is 186executed before the main procedure is called. This depends on the 187languages used to write your program. In C<tt>++</tt>, for instance, 188constructors for static and global objects are executed before 189<code>main</code> is called. It is therefore possible that the debugger stops 190before reaching the main procedure. However, the temporary breakpoint 191will remain to halt execution. 192</p> 193<p>Specify the arguments to give to your program as arguments to the 194‘<samp>start</samp>’ command. These arguments will be given verbatim to the 195underlying ‘<samp>run</samp>’ command. Note that the same arguments will be 196reused if no argument is provided during subsequent calls to 197‘<samp>start</samp>’ or ‘<samp>run</samp>’. 198</p> 199<p>It is sometimes necessary to debug the program during elaboration. In 200these cases, using the <code>start</code> command would stop the execution 201of your program too late, as the program would have already completed 202the elaboration phase. Under these circumstances, either insert 203breakpoints in your elaboration code before running your program or 204use the <code>starti</code> command. 205</p> 206<a name="index-starti"></a> 207</dd> 208<dt><code>starti</code></dt> 209<dd><a name="index-run-to-first-instruction"></a> 210<p>The ‘<samp>starti</samp>’ command does the equivalent of setting a temporary 211breakpoint at the first instruction of a program’s execution and then 212invoking the ‘<samp>run</samp>’ command. For programs containing an 213elaboration phase, the <code>starti</code> command will stop execution at 214the start of the elaboration phase. 215</p> 216<a name="set-exec_002dwrapper"></a><a name="index-set-exec_002dwrapper"></a> 217</dd> 218<dt><code>set exec-wrapper <var>wrapper</var></code></dt> 219<dt><code>show exec-wrapper</code></dt> 220<dt><code>unset exec-wrapper</code></dt> 221<dd><p>When ‘<samp>exec-wrapper</samp>’ is set, the specified wrapper is used to 222launch programs for debugging. <small>GDB</small> starts your program 223with a shell command of the form <kbd>exec <var>wrapper</var> 224<var>program</var></kbd>. Quoting is added to <var>program</var> and its 225arguments, but not to <var>wrapper</var>, so you should add quotes if 226appropriate for your shell. The wrapper runs until it executes 227your program, and then <small>GDB</small> takes control. 228</p> 229<p>You can use any program that eventually calls <code>execve</code> with 230its arguments as a wrapper. Several standard Unix utilities do 231this, e.g. <code>env</code> and <code>nohup</code>. Any Unix shell script ending 232with <code>exec "$@"</code> will also work. 233</p> 234<p>For example, you can use <code>env</code> to pass an environment variable to 235the debugged program, without setting the variable in your shell’s 236environment: 237</p> 238<div class="smallexample"> 239<pre class="smallexample">(gdb) set exec-wrapper env 'LD_PRELOAD=libtest.so' 240(gdb) run 241</pre></div> 242 243<p>This command is available when debugging locally on most targets, excluding 244<small>DJGPP</small>, Cygwin, MS Windows, and QNX Neutrino. 245</p> 246<a name="index-set-startup_002dwith_002dshell"></a> 247<a name="set-startup_002dwith_002dshell"></a></dd> 248<dt><code>set startup-with-shell</code></dt> 249<dt><code>set startup-with-shell on</code></dt> 250<dt><code>set startup-with-shell off</code></dt> 251<dt><code>show startup-with-shell</code></dt> 252<dd><p>On Unix systems, by default, if a shell is available on your target, 253<small>GDB</small>) uses it to start your program. Arguments of the 254<code>run</code> command are passed to the shell, which does variable 255substitution, expands wildcard characters and performs redirection of 256I/O. In some circumstances, it may be useful to disable such use of a 257shell, for example, when debugging the shell itself or diagnosing 258startup failures such as: 259</p> 260<div class="smallexample"> 261<pre class="smallexample">(gdb) run 262Starting program: ./a.out 263During startup program terminated with signal SIGSEGV, Segmentation fault. 264</pre></div> 265 266<p>which indicates the shell or the wrapper specified with 267‘<samp>exec-wrapper</samp>’ crashed, not your program. Most often, this is 268caused by something odd in your shell’s non-interactive mode 269initialization file—such as <samp>.cshrc</samp> for C-shell, 270$<samp>.zshenv</samp> for the Z shell, or the file specified in the 271‘<samp>BASH_ENV</samp>’ environment variable for BASH. 272</p> 273<a name="set-auto_002dconnect_002dnative_002dtarget"></a><a name="index-set-auto_002dconnect_002dnative_002dtarget"></a> 274</dd> 275<dt><code>set auto-connect-native-target</code></dt> 276<dt><code>set auto-connect-native-target on</code></dt> 277<dt><code>set auto-connect-native-target off</code></dt> 278<dt><code>show auto-connect-native-target</code></dt> 279<dd> 280<p>By default, if the current inferior is not connected to any target yet 281(e.g., with <code>target remote</code>), the <code>run</code> command starts your 282program as a native process under <small>GDB</small>, on your local machine. 283If you’re sure you don’t want to debug programs on your local machine, 284you can tell <small>GDB</small> to not connect to the native target 285automatically with the <code>set auto-connect-native-target off</code> 286command. 287</p> 288<p>If <code>on</code>, which is the default, and if the current inferior is not 289connected to a target already, the <code>run</code> command automaticaly 290connects to the native target, if one is available. 291</p> 292<p>If <code>off</code>, and if the current inferior is not connected to a 293target already, the <code>run</code> command fails with an error: 294</p> 295<div class="smallexample"> 296<pre class="smallexample">(gdb) run 297Don't know how to run. Try "help target". 298</pre></div> 299 300<p>If the current inferior is already connected to a target, <small>GDB</small> 301always uses it with the <code>run</code> command. 302</p> 303<p>In any case, you can explicitly connect to the native target with the 304<code>target native</code> command. For example, 305</p> 306<div class="smallexample"> 307<pre class="smallexample">(gdb) set auto-connect-native-target off 308(gdb) run 309Don't know how to run. Try "help target". 310(gdb) target native 311(gdb) run 312Starting program: ./a.out 313[Inferior 1 (process 10421) exited normally] 314</pre></div> 315 316<p>In case you connected explicitly to the <code>native</code> target, 317<small>GDB</small> remains connected even if all inferiors exit, ready for 318the next <code>run</code> command. Use the <code>disconnect</code> command to 319disconnect. 320</p> 321<p>Examples of other commands that likewise respect the 322<code>auto-connect-native-target</code> setting: <code>attach</code>, <code>info 323proc</code>, <code>info os</code>. 324</p> 325<a name="index-set-disable_002drandomization"></a> 326</dd> 327<dt><code>set disable-randomization</code></dt> 328<dt><code>set disable-randomization on</code></dt> 329<dd><p>This option (enabled by default in <small>GDB</small>) will turn off the native 330randomization of the virtual address space of the started program. This option 331is useful for multiple debugging sessions to make the execution better 332reproducible and memory addresses reusable across debugging sessions. 333</p> 334<p>This feature is implemented only on certain targets, including <small>GNU</small>/Linux. 335On <small>GNU</small>/Linux you can get the same behavior using 336</p> 337<div class="smallexample"> 338<pre class="smallexample">(gdb) set exec-wrapper setarch `uname -m` -R 339</pre></div> 340 341</dd> 342<dt><code>set disable-randomization off</code></dt> 343<dd><p>Leave the behavior of the started executable unchanged. Some bugs rear their 344ugly heads only when the program is loaded at certain addresses. If your bug 345disappears when you run the program under <small>GDB</small>, that might be because 346<small>GDB</small> by default disables the address randomization on platforms, such 347as <small>GNU</small>/Linux, which do that for stand-alone programs. Use <kbd>set 348disable-randomization off</kbd> to try to reproduce such elusive bugs. 349</p> 350<p>On targets where it is available, virtual address space randomization 351protects the programs against certain kinds of security attacks. In these 352cases the attacker needs to know the exact location of a concrete executable 353code. Randomizing its location makes it impossible to inject jumps misusing 354a code at its expected addresses. 355</p> 356<p>Prelinking shared libraries provides a startup performance advantage but it 357makes addresses in these libraries predictable for privileged processes by 358having just unprivileged access at the target system. Reading the shared 359library binary gives enough information for assembling the malicious code 360misusing it. Still even a prelinked shared library can get loaded at a new 361random address just requiring the regular relocation process during the 362startup. Shared libraries not already prelinked are always loaded at 363a randomly chosen address. 364</p> 365<p>Position independent executables (PIE) contain position independent code 366similar to the shared libraries and therefore such executables get loaded at 367a randomly chosen address upon startup. PIE executables always load even 368already prelinked shared libraries at a random address. You can build such 369executable using <code>gcc -fPIE -pie</code>. 370</p> 371<p>Heap (malloc storage), stack and custom mmap areas are always placed randomly 372(as long as the randomization is enabled). 373</p> 374</dd> 375<dt><code>show disable-randomization</code></dt> 376<dd><p>Show the current setting of the explicit disable of the native randomization of 377the virtual address space of the started program. 378</p> 379</dd> 380</dl> 381 382<hr> 383<div class="header"> 384<p> 385Next: <a href="Arguments.html#Arguments" accesskey="n" rel="next">Arguments</a>, Previous: <a href="Compilation.html#Compilation" accesskey="p" rel="previous">Compilation</a>, Up: <a href="Running.html#Running" accesskey="u" rel="up">Running</a> [<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> 386</div> 387 388 389 390</body> 391</html> 392