Define.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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: Define</title>
  16. <meta name="description" content="Debugging with GDB: Define">
  17. <meta name="keywords" content="Debugging with GDB: Define">
  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="Hooks.html#Hooks" rel="next" title="Hooks">
  27. <link href="Sequences.html#Sequences" rel="previous" title="Sequences">
  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="Define"></a>
  59. <div class="header">
  60. <p>
  61. Next: <a href="Hooks.html#Hooks" accesskey="n" rel="next">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="User_002ddefined-Commands"></a>
  65. <h4 class="subsection">23.1.1 User-defined Commands</h4>
  66. <a name="index-user_002ddefined-command"></a>
  67. <a name="index-arguments_002c-to-user_002ddefined-commands"></a>
  68. <p>A <em>user-defined command</em> is a sequence of <small>GDB</small> commands to
  69. which you assign a new name as a command. This is done with the
  70. <code>define</code> command. User commands may accept an unlimited number of arguments
  71. separated by whitespace. Arguments are accessed within the user command
  72. via <code>$arg0&hellip;$argN</code>. A trivial example:
  73. </p>
  74. <div class="smallexample">
  75. <pre class="smallexample">define adder
  76. print $arg0 + $arg1 + $arg2
  77. end
  78. </pre></div>
  79. <p>To execute the command use:
  80. </p>
  81. <div class="smallexample">
  82. <pre class="smallexample">adder 1 2 3
  83. </pre></div>
  84. <p>This defines the command <code>adder</code>, which prints the sum of
  85. its three arguments. Note the arguments are text substitutions, so they may
  86. reference variables, use complex expressions, or even perform inferior
  87. functions calls.
  88. </p>
  89. <a name="index-argument-count-in-user_002ddefined-commands"></a>
  90. <a name="index-how-many-arguments-_0028user_002ddefined-commands_0029"></a>
  91. <p>In addition, <code>$argc</code> may be used to find out how many arguments have
  92. been passed.
  93. </p>
  94. <div class="smallexample">
  95. <pre class="smallexample">define adder
  96. if $argc == 2
  97. print $arg0 + $arg1
  98. end
  99. if $argc == 3
  100. print $arg0 + $arg1 + $arg2
  101. end
  102. end
  103. </pre></div>
  104. <p>Combining with the <code>eval</code> command (see <a href="Output.html#eval">eval</a>) makes it easier
  105. to process a variable number of arguments:
  106. </p>
  107. <div class="smallexample">
  108. <pre class="smallexample">define adder
  109. set $i = 0
  110. set $sum = 0
  111. while $i &lt; $argc
  112. eval &quot;set $sum = $sum + $arg%d&quot;, $i
  113. set $i = $i + 1
  114. end
  115. print $sum
  116. end
  117. </pre></div>
  118. <dl compact="compact">
  119. <dd>
  120. <a name="index-define"></a>
  121. </dd>
  122. <dt><code>define <var>commandname</var></code></dt>
  123. <dd><p>Define a command named <var>commandname</var>. If there is already a command
  124. by that name, you are asked to confirm that you want to redefine it.
  125. The argument <var>commandname</var> may be a bare command name consisting of letters,
  126. numbers, dashes, dots, and underscores. It may also start with any
  127. predefined or user-defined prefix command.
  128. For example, &lsquo;<samp>define target my-target</samp>&rsquo; creates
  129. a user-defined &lsquo;<samp>target my-target</samp>&rsquo; command.
  130. </p>
  131. <p>The definition of the command is made up of other <small>GDB</small> command lines,
  132. which are given following the <code>define</code> command. The end of these
  133. commands is marked by a line containing <code>end</code>.
  134. </p>
  135. <a name="index-document"></a>
  136. <a name="index-end-_0028user_002ddefined-commands_0029"></a>
  137. </dd>
  138. <dt><code>document <var>commandname</var></code></dt>
  139. <dd><p>Document the user-defined command <var>commandname</var>, so that it can be
  140. accessed by <code>help</code>. The command <var>commandname</var> must already be
  141. defined. This command reads lines of documentation just as <code>define</code>
  142. reads the lines of the command definition, ending with <code>end</code>.
  143. After the <code>document</code> command is finished, <code>help</code> on command
  144. <var>commandname</var> displays the documentation you have written.
  145. </p>
  146. <p>You may use the <code>document</code> command again to change the
  147. documentation of a command. Redefining the command with <code>define</code>
  148. does not change the documentation.
  149. </p>
  150. <a name="index-define_002dprefix"></a>
  151. </dd>
  152. <dt><code>define-prefix <var>commandname</var></code></dt>
  153. <dd><p>Define or mark the command <var>commandname</var> as a user-defined prefix
  154. command. Once marked, <var>commandname</var> can be used as prefix command
  155. by the <code>define</code> command.
  156. Note that <code>define-prefix</code> can be used with a not yet defined
  157. <var>commandname</var>. In such a case, <var>commandname</var> is defined as
  158. an empty user-defined command.
  159. In case you redefine a command that was marked as a user-defined
  160. prefix command, the subcommands of the redefined command are kept
  161. (and <small>GDB</small> indicates so to the user).
  162. </p>
  163. <p>Example:
  164. </p><div class="example">
  165. <pre class="example">(gdb) define-prefix abc
  166. (gdb) define-prefix abc def
  167. (gdb) define abc def
  168. Type commands for definition of &quot;abc def&quot;.
  169. End with a line saying just &quot;end&quot;.
  170. &gt;echo command initial def\n
  171. &gt;end
  172. (gdb) define abc def ghi
  173. Type commands for definition of &quot;abc def ghi&quot;.
  174. End with a line saying just &quot;end&quot;.
  175. &gt;echo command ghi\n
  176. &gt;end
  177. (gdb) define abc def
  178. Keeping subcommands of prefix command &quot;def&quot;.
  179. Redefine command &quot;def&quot;? (y or n) y
  180. Type commands for definition of &quot;abc def&quot;.
  181. End with a line saying just &quot;end&quot;.
  182. &gt;echo command def\n
  183. &gt;end
  184. (gdb) abc def ghi
  185. command ghi
  186. (gdb) abc def
  187. command def
  188. (gdb)
  189. </pre></div>
  190. <a name="index-dont_002drepeat-1"></a>
  191. <a name="index-don_0027t-repeat-command"></a>
  192. </dd>
  193. <dt><code>dont-repeat</code></dt>
  194. <dd><p>Used inside a user-defined command, this tells <small>GDB</small> that this
  195. command should not be repeated when the user hits <tt class="key">RET</tt>
  196. (see <a href="Command-Syntax.html#Command-Syntax">repeat last command</a>).
  197. </p>
  198. <a name="index-help-user_002ddefined"></a>
  199. </dd>
  200. <dt><code>help user-defined</code></dt>
  201. <dd><p>List all user-defined commands and all python commands defined in class
  202. COMMAND_USER. The first line of the documentation or docstring is
  203. included (if any).
  204. </p>
  205. <a name="index-show-user"></a>
  206. </dd>
  207. <dt><code>show user</code></dt>
  208. <dt><code>show user <var>commandname</var></code></dt>
  209. <dd><p>Display the <small>GDB</small> commands used to define <var>commandname</var> (but
  210. not its documentation). If no <var>commandname</var> is given, display the
  211. definitions for all user-defined commands.
  212. This does not work for user-defined python commands.
  213. </p>
  214. <a name="index-infinite-recursion-in-user_002ddefined-commands"></a>
  215. <a name="index-show-max_002duser_002dcall_002ddepth"></a>
  216. <a name="index-set-max_002duser_002dcall_002ddepth"></a>
  217. </dd>
  218. <dt><code>show max-user-call-depth</code></dt>
  219. <dt><code>set max-user-call-depth</code></dt>
  220. <dd><p>The value of <code>max-user-call-depth</code> controls how many recursion
  221. levels are allowed in user-defined commands before <small>GDB</small> suspects an
  222. infinite recursion and aborts the command.
  223. This does not apply to user-defined python commands.
  224. </p></dd>
  225. </dl>
  226. <p>In addition to the above commands, user-defined commands frequently
  227. use control flow commands, described in <a href="Command-Files.html#Command-Files">Command Files</a>.
  228. </p>
  229. <p>When user-defined commands are executed, the
  230. commands of the definition are not printed. An error in any command
  231. stops execution of the user-defined command.
  232. </p>
  233. <p>If used interactively, commands that would ask for confirmation proceed
  234. without asking when used inside a user-defined command. Many <small>GDB</small>
  235. commands that normally print messages to say what they are doing omit the
  236. messages when used in a user-defined command.
  237. </p>
  238. <hr>
  239. <div class="header">
  240. <p>
  241. Next: <a href="Hooks.html#Hooks" accesskey="n" rel="next">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>
  242. </div>
  243. </body>
  244. </html>