You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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