Starting.html 18 KB

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