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: Convenience Funs</title>
18
19<meta name="description" content="Debugging with GDB: Convenience Funs">
20<meta name="keywords" content="Debugging with GDB: Convenience Funs">
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="Data.html#Data" rel="up" title="Data">
29<link href="Registers.html#Registers" rel="next" title="Registers">
30<link href="Convenience-Vars.html#Convenience-Vars" rel="previous" title="Convenience Vars">
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="Convenience-Funs"></a>
65<div class="header">
66<p>
67Next: <a href="Registers.html#Registers" accesskey="n" rel="next">Registers</a>, Previous: <a href="Convenience-Vars.html#Convenience-Vars" accesskey="p" rel="previous">Convenience Vars</a>, Up: <a href="Data.html#Data" accesskey="u" rel="up">Data</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="Convenience-Functions"></a>
71<h3 class="section">10.12 Convenience Functions</h3>
72
73<a name="index-convenience-functions"></a>
74<p><small>GDB</small> also supplies some <em>convenience functions</em>.  These
75have a syntax similar to convenience variables.  A convenience
76function can be used in an expression just like an ordinary function;
77however, a convenience function is implemented internally to
78<small>GDB</small>.
79</p>
80<p>These functions do not require <small>GDB</small> to be configured with
81<code>Python</code> support, which means that they are always available.
82</p>
83<dl compact="compact">
84<dt><code>$_isvoid (<var>expr</var>)</code></dt>
85<dd><a name="index-_0024_005fisvoid_002c-convenience-function"></a>
86<p>Return one if the expression <var>expr</var> is <code>void</code>.  Otherwise it
87returns zero.
88</p>
89<p>A <code>void</code> expression is an expression where the type of the result
90is <code>void</code>.  For example, you can examine a convenience variable
91(see <a href="Convenience-Vars.html#Convenience-Vars">Convenience Variables</a>) to check whether
92it is <code>void</code>:
93</p>
94<div class="smallexample">
95<pre class="smallexample">(gdb) print $_exitcode
96$1 = void
97(gdb) print $_isvoid ($_exitcode)
98$2 = 1
99(gdb) run
100Starting program: ./a.out
101[Inferior 1 (process 29572) exited normally]
102(gdb) print $_exitcode
103$3 = 0
104(gdb) print $_isvoid ($_exitcode)
105$4 = 0
106</pre></div>
107
108<p>In the example above, we used <code>$_isvoid</code> to check whether
109<code>$_exitcode</code> is <code>void</code> before and after the execution of the
110program being debugged.  Before the execution there is no exit code to
111be examined, therefore <code>$_exitcode</code> is <code>void</code>.  After the
112execution the program being debugged returned zero, therefore
113<code>$_exitcode</code> is zero, which means that it is not <code>void</code>
114anymore.
115</p>
116<p>The <code>void</code> expression can also be a call of a function from the
117program being debugged.  For example, given the following function:
118</p>
119<div class="smallexample">
120<pre class="smallexample">void
121foo (void)
122{
123}
124</pre></div>
125
126<p>The result of calling it inside <small>GDB</small> is <code>void</code>:
127</p>
128<div class="smallexample">
129<pre class="smallexample">(gdb) print foo ()
130$1 = void
131(gdb) print $_isvoid (foo ())
132$2 = 1
133(gdb) set $v = foo ()
134(gdb) print $v
135$3 = void
136(gdb) print $_isvoid ($v)
137$4 = 1
138</pre></div>
139
140</dd>
141<dt><code>$_gdb_setting_str (<var>setting</var>)</code></dt>
142<dd><a name="index-_0024_005fgdb_005fsetting_005fstr_002c-convenience-function"></a>
143<p>Return the value of the <small>GDB</small> <var>setting</var> as a string.
144<var>setting</var> is any setting that can be used in a <code>set</code> or
145<code>show</code> command (see <a href="Controlling-GDB.html#Controlling-GDB">Controlling GDB</a>).
146</p>
147<div class="smallexample">
148<pre class="smallexample">(gdb) show print frame-arguments
149Printing of non-scalar frame arguments is &quot;scalars&quot;.
150(gdb) p $_gdb_setting_str(&quot;print frame-arguments&quot;)
151$1 = &quot;scalars&quot;
152(gdb) p $_gdb_setting_str(&quot;height&quot;)
153$2 = &quot;30&quot;
154(gdb)
155</pre></div>
156
157</dd>
158<dt><code>$_gdb_setting (<var>setting</var>)</code></dt>
159<dd><a name="index-_0024_005fgdb_005fsetting_002c-convenience-function"></a>
160<p>Return the value of the <small>GDB</small> <var>setting</var>.
161The type of the returned value depends on the setting.
162</p>
163<p>The value type for boolean and auto boolean settings is <code>int</code>.
164The boolean values <code>off</code> and <code>on</code> are converted to
165the integer values <code>0</code> and <code>1</code>.  The value <code>auto</code> is
166converted to the value <code>-1</code>.
167</p>
168<p>The value type for integer settings is either <code>unsigned int</code>
169or <code>int</code>, depending on the setting.
170</p>
171<p>Some integer settings accept an <code>unlimited</code> value.
172Depending on the setting, the <code>set</code> command also accepts
173the value <code>0</code> or the value <code>-1</code> as a synonym for
174<code>unlimited</code>.
175For example, <code>set height unlimited</code> is equivalent to
176<code>set height 0</code>.
177</p>
178<p>Some other settings that accept the <code>unlimited</code> value
179use the value <code>0</code> to literally mean zero.
180For example, <code>set history size 0</code> indicates to not
181record any <small>GDB</small> commands in the command history.
182For such settings, <code>-1</code> is the synonym
183for <code>unlimited</code>.
184</p>
185<p>See the documentation of the corresponding <code>set</code> command for
186the numerical value equivalent to <code>unlimited</code>.
187</p>
188<p>The <code>$_gdb_setting</code> function converts the unlimited value
189to a <code>0</code> or a <code>-1</code> value according to what the
190<code>set</code> command uses.
191</p>
192<div class="smallexample">
193<pre class="smallexample">(gdb) p $_gdb_setting_str(&quot;height&quot;)
194$1 = &quot;30&quot;
195(gdb) p $_gdb_setting(&quot;height&quot;)
196$2 = 30
197(gdb) set height unlimited
198(gdb) p $_gdb_setting_str(&quot;height&quot;)
199$3 = &quot;unlimited&quot;
200(gdb) p $_gdb_setting(&quot;height&quot;)
201$4 = 0
202</pre><pre class="smallexample">(gdb) p $_gdb_setting_str(&quot;history size&quot;)
203$5 = &quot;unlimited&quot;
204(gdb) p $_gdb_setting(&quot;history size&quot;)
205$6 = -1
206(gdb) p $_gdb_setting_str(&quot;disassemble-next-line&quot;)
207$7 = &quot;auto&quot;
208(gdb) p $_gdb_setting(&quot;disassemble-next-line&quot;)
209$8 = -1
210(gdb)
211</pre></div>
212
213<p>Other setting types (enum, filename, optional filename, string, string noescape)
214are returned as string values.
215</p>
216
217</dd>
218<dt><code>$_gdb_maint_setting_str (<var>setting</var>)</code></dt>
219<dd><a name="index-_0024_005fgdb_005fmaint_005fsetting_005fstr_002c-convenience-function"></a>
220<p>Like the <code>$_gdb_setting_str</code> function, but works with
221<code>maintenance set</code> variables.
222</p>
223</dd>
224<dt><code>$_gdb_maint_setting (<var>setting</var>)</code></dt>
225<dd><a name="index-_0024_005fgdb_005fmaint_005fsetting_002c-convenience-function"></a>
226<p>Like the <code>$_gdb_setting</code> function, but works with
227<code>maintenance set</code> variables.
228</p>
229</dd>
230</dl>
231
232<p>The following functions require <small>GDB</small> to be configured with
233<code>Python</code> support.
234</p>
235<dl compact="compact">
236<dt><code>$_memeq(<var>buf1</var>, <var>buf2</var>, <var>length</var>)</code></dt>
237<dd><a name="index-_0024_005fmemeq_002c-convenience-function"></a>
238<p>Returns one if the <var>length</var> bytes at the addresses given by
239<var>buf1</var> and <var>buf2</var> are equal.
240Otherwise it returns zero.
241</p>
242</dd>
243<dt><code>$_regex(<var>str</var>, <var>regex</var>)</code></dt>
244<dd><a name="index-_0024_005fregex_002c-convenience-function"></a>
245<p>Returns one if the string <var>str</var> matches the regular expression
246<var>regex</var>.  Otherwise it returns zero.
247The syntax of the regular expression is that specified by <code>Python</code>&rsquo;s
248regular expression support.
249</p>
250</dd>
251<dt><code>$_streq(<var>str1</var>, <var>str2</var>)</code></dt>
252<dd><a name="index-_0024_005fstreq_002c-convenience-function"></a>
253<p>Returns one if the strings <var>str1</var> and <var>str2</var> are equal.
254Otherwise it returns zero.
255</p>
256</dd>
257<dt><code>$_strlen(<var>str</var>)</code></dt>
258<dd><a name="index-_0024_005fstrlen_002c-convenience-function"></a>
259<p>Returns the length of string <var>str</var>.
260</p>
261</dd>
262<dt><code>$_caller_is(<var>name</var><span class="roman">[</span>, <var>number_of_frames</var><span class="roman">]</span>)</code></dt>
263<dd><a name="index-_0024_005fcaller_005fis_002c-convenience-function"></a>
264<p>Returns one if the calling function&rsquo;s name is equal to <var>name</var>.
265Otherwise it returns zero.
266</p>
267<p>If the optional argument <var>number_of_frames</var> is provided,
268it is the number of frames up in the stack to look.
269The default is 1.
270</p>
271<p>Example:
272</p>
273<div class="smallexample">
274<pre class="smallexample">(gdb) backtrace
275#0  bottom_func ()
276    at testsuite/gdb.python/py-caller-is.c:21
277#1  0x00000000004005a0 in middle_func ()
278    at testsuite/gdb.python/py-caller-is.c:27
279#2  0x00000000004005ab in top_func ()
280    at testsuite/gdb.python/py-caller-is.c:33
281#3  0x00000000004005b6 in main ()
282    at testsuite/gdb.python/py-caller-is.c:39
283(gdb) print $_caller_is (&quot;middle_func&quot;)
284$1 = 1
285(gdb) print $_caller_is (&quot;top_func&quot;, 2)
286$1 = 1
287</pre></div>
288
289</dd>
290<dt><code>$_caller_matches(<var>regexp</var><span class="roman">[</span>, <var>number_of_frames</var><span class="roman">]</span>)</code></dt>
291<dd><a name="index-_0024_005fcaller_005fmatches_002c-convenience-function"></a>
292<p>Returns one if the calling function&rsquo;s name matches the regular expression
293<var>regexp</var>.  Otherwise it returns zero.
294</p>
295<p>If the optional argument <var>number_of_frames</var> is provided,
296it is the number of frames up in the stack to look.
297The default is 1.
298</p>
299</dd>
300<dt><code>$_any_caller_is(<var>name</var><span class="roman">[</span>, <var>number_of_frames</var><span class="roman">]</span>)</code></dt>
301<dd><a name="index-_0024_005fany_005fcaller_005fis_002c-convenience-function"></a>
302<p>Returns one if any calling function&rsquo;s name is equal to <var>name</var>.
303Otherwise it returns zero.
304</p>
305<p>If the optional argument <var>number_of_frames</var> is provided,
306it is the number of frames up in the stack to look.
307The default is 1.
308</p>
309<p>This function differs from <code>$_caller_is</code> in that this function
310checks all stack frames from the immediate caller to the frame specified
311by <var>number_of_frames</var>, whereas <code>$_caller_is</code> only checks the
312frame specified by <var>number_of_frames</var>.
313</p>
314</dd>
315<dt><code>$_any_caller_matches(<var>regexp</var><span class="roman">[</span>, <var>number_of_frames</var><span class="roman">]</span>)</code></dt>
316<dd><a name="index-_0024_005fany_005fcaller_005fmatches_002c-convenience-function"></a>
317<p>Returns one if any calling function&rsquo;s name matches the regular expression
318<var>regexp</var>.  Otherwise it returns zero.
319</p>
320<p>If the optional argument <var>number_of_frames</var> is provided,
321it is the number of frames up in the stack to look.
322The default is 1.
323</p>
324<p>This function differs from <code>$_caller_matches</code> in that this function
325checks all stack frames from the immediate caller to the frame specified
326by <var>number_of_frames</var>, whereas <code>$_caller_matches</code> only checks the
327frame specified by <var>number_of_frames</var>.
328</p>
329</dd>
330<dt><code>$_as_string(<var>value</var>)</code></dt>
331<dd><a name="index-_0024_005fas_005fstring_002c-convenience-function"></a>
332<p>Return the string representation of <var>value</var>.
333</p>
334<p>This function is useful to obtain the textual label (enumerator) of an
335enumeration value.  For example, assuming the variable <var>node</var> is of
336an enumerated type:
337</p>
338<div class="smallexample">
339<pre class="smallexample">(gdb) printf &quot;Visiting node of type %s\n&quot;, $_as_string(node)
340Visiting node of type NODE_INTEGER
341</pre></div>
342
343</dd>
344<dt><code>$_cimag(<var>value</var>)</code></dt>
345<dt><code>$_creal(<var>value</var>)</code></dt>
346<dd><a name="index-_0024_005fcimag_002c-convenience-function"></a>
347<a name="index-_0024_005fcreal_002c-convenience-function"></a>
348<p>Return the imaginary (<code>$_cimag</code>) or real (<code>$_creal</code>) part of
349the complex number <var>value</var>.
350</p>
351<p>The type of the imaginary or real part depends on the type of the
352complex number, e.g., using <code>$_cimag</code> on a <code>float complex</code>
353will return an imaginary part of type <code>float</code>.
354</p>
355</dd>
356</dl>
357
358<p><small>GDB</small> provides the ability to list and get help on
359convenience functions.
360</p>
361<dl compact="compact">
362<dt><code>help function</code></dt>
363<dd><a name="index-help-function"></a>
364<a name="index-show-all-convenience-functions"></a>
365<p>Print a list of all convenience functions.
366</p></dd>
367</dl>
368
369<hr>
370<div class="header">
371<p>
372Next: <a href="Registers.html#Registers" accesskey="n" rel="next">Registers</a>, Previous: <a href="Convenience-Vars.html#Convenience-Vars" accesskey="p" rel="previous">Convenience Vars</a>, Up: <a href="Data.html#Data" accesskey="u" rel="up">Data</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>
373</div>
374
375
376
377</body>
378</html>
379