Convenience-Funs.html 15 KB

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