Command-Files.html 9.9 KB

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