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