選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

320 行
14KB

  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 6.5, http://www.gnu.org/software/texinfo/ -->
  14. <head>
  15. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  16. <title>Convenience Vars (Debugging with GDB)</title>
  17. <meta name="description" content="Convenience Vars (Debugging with GDB)">
  18. <meta name="keywords" content="Convenience Vars (Debugging with GDB)">
  19. <meta name="resource-type" content="document">
  20. <meta name="distribution" content="global">
  21. <meta name="Generator" content="makeinfo">
  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="Convenience-Funs.html#Convenience-Funs" rel="next" title="Convenience Funs">
  27. <link href="Value-History.html#Value-History" rel="prev" title="Value History">
  28. <style type="text/css">
  29. <!--
  30. a.summary-letter {text-decoration: none}
  31. blockquote.indentedblock {margin-right: 0em}
  32. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  33. blockquote.smallquotation {font-size: smaller}
  34. div.display {margin-left: 3.2em}
  35. div.example {margin-left: 3.2em}
  36. div.lisp {margin-left: 3.2em}
  37. div.smalldisplay {margin-left: 3.2em}
  38. div.smallexample {margin-left: 3.2em}
  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.nolinebreak {white-space: nowrap}
  50. span.roman {font-family: initial; font-weight: normal}
  51. span.sansserif {font-family: sans-serif; font-weight: normal}
  52. ul.no-bullet {list-style: none}
  53. -->
  54. </style>
  55. </head>
  56. <body lang="en">
  57. <a name="Convenience-Vars"></a>
  58. <div class="header">
  59. <p>
  60. Next: <a href="Convenience-Funs.html#Convenience-Funs" accesskey="n" rel="next">Convenience Funs</a>, Previous: <a href="Value-History.html#Value-History" accesskey="p" rel="prev">Value History</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>
  61. </div>
  62. <hr>
  63. <a name="Convenience-Variables"></a>
  64. <h3 class="section">10.11 Convenience Variables</h3>
  65. <a name="index-convenience-variables"></a>
  66. <a name="index-user_002ddefined-variables"></a>
  67. <p><small>GDB</small> provides <em>convenience variables</em> that you can use within
  68. <small>GDB</small> to hold on to a value and refer to it later. These variables
  69. exist entirely within <small>GDB</small>; they are not part of your program, and
  70. setting a convenience variable has no direct effect on further execution
  71. of your program. That is why you can use them freely.
  72. </p>
  73. <p>Convenience variables are prefixed with &lsquo;<samp>$</samp>&rsquo;. Any name preceded by
  74. &lsquo;<samp>$</samp>&rsquo; can be used for a convenience variable, unless it is one of
  75. the predefined machine-specific register names (see <a href="Registers.html#Registers">Registers</a>).
  76. (Value history references, in contrast, are <em>numbers</em> preceded
  77. by &lsquo;<samp>$</samp>&rsquo;. See <a href="Value-History.html#Value-History">Value History</a>.)
  78. </p>
  79. <p>You can save a value in a convenience variable with an assignment
  80. expression, just as you would set a variable in your program.
  81. For example:
  82. </p>
  83. <div class="smallexample">
  84. <pre class="smallexample">set $foo = *object_ptr
  85. </pre></div>
  86. <p>would save in <code>$foo</code> the value contained in the object pointed to by
  87. <code>object_ptr</code>.
  88. </p>
  89. <p>Using a convenience variable for the first time creates it, but its
  90. value is <code>void</code> until you assign a new value. You can alter the
  91. value with another assignment at any time.
  92. </p>
  93. <p>Convenience variables have no fixed types. You can assign a convenience
  94. variable any type of value, including structures and arrays, even if
  95. that variable already has a value of a different type. The convenience
  96. variable, when used as an expression, has the type of its current value.
  97. </p>
  98. <dl compact="compact">
  99. <dd><a name="index-show-convenience"></a>
  100. <a name="index-show-all-user-variables-and-functions"></a>
  101. </dd>
  102. <dt><code>show convenience</code></dt>
  103. <dd><p>Print a list of convenience variables used so far, and their values,
  104. as well as a list of the convenience functions.
  105. Abbreviated <code>show conv</code>.
  106. </p>
  107. <a name="index-init_002dif_002dundefined"></a>
  108. <a name="index-convenience-variables_002c-initializing"></a>
  109. </dd>
  110. <dt><code>init-if-undefined $<var>variable</var> = <var>expression</var></code></dt>
  111. <dd><p>Set a convenience variable if it has not already been set. This is useful
  112. for user-defined commands that keep some state. It is similar, in concept,
  113. to using local static variables with initializers in C (except that
  114. convenience variables are global). It can also be used to allow users to
  115. override default values used in a command script.
  116. </p>
  117. <p>If the variable is already defined then the expression is not evaluated so
  118. any side-effects do not occur.
  119. </p></dd>
  120. </dl>
  121. <p>One of the ways to use a convenience variable is as a counter to be
  122. incremented or a pointer to be advanced. For example, to print
  123. a field from successive elements of an array of structures:
  124. </p>
  125. <div class="smallexample">
  126. <pre class="smallexample">set $i = 0
  127. print bar[$i++]-&gt;contents
  128. </pre></div>
  129. <p>Repeat that command by typing <tt class="key">RET</tt>.
  130. </p>
  131. <p>Some convenience variables are created automatically by <small>GDB</small> and given
  132. values likely to be useful.
  133. </p>
  134. <dl compact="compact">
  135. <dd><a name="index-_0024_005f_002c-convenience-variable"></a>
  136. </dd>
  137. <dt><code>$_</code></dt>
  138. <dd><p>The variable <code>$_</code> is automatically set by the <code>x</code> command to
  139. the last address examined (see <a href="Memory.html#Memory">Examining Memory</a>). Other
  140. commands which provide a default address for <code>x</code> to examine also
  141. set <code>$_</code> to that address; these commands include <code>info line</code>
  142. and <code>info breakpoint</code>. The type of <code>$_</code> is <code>void *</code>
  143. except when set by the <code>x</code> command, in which case it is a pointer
  144. to the type of <code>$__</code>.
  145. </p>
  146. <a name="index-_0024_005f_005f_002c-convenience-variable"></a>
  147. </dd>
  148. <dt><code>$__</code></dt>
  149. <dd><p>The variable <code>$__</code> is automatically set by the <code>x</code> command
  150. to the value found in the last address examined. Its type is chosen
  151. to match the format in which the data was printed.
  152. </p>
  153. </dd>
  154. <dt><code>$_exitcode</code></dt>
  155. <dd><a name="index-_0024_005fexitcode_002c-convenience-variable"></a>
  156. <p>When the program being debugged terminates normally, <small>GDB</small>
  157. automatically sets this variable to the exit code of the program, and
  158. resets <code>$_exitsignal</code> to <code>void</code>.
  159. </p>
  160. </dd>
  161. <dt><code>$_exitsignal</code></dt>
  162. <dd><a name="index-_0024_005fexitsignal_002c-convenience-variable"></a>
  163. <p>When the program being debugged dies due to an uncaught signal,
  164. <small>GDB</small> automatically sets this variable to that signal&rsquo;s number,
  165. and resets <code>$_exitcode</code> to <code>void</code>.
  166. </p>
  167. <p>To distinguish between whether the program being debugged has exited
  168. (i.e., <code>$_exitcode</code> is not <code>void</code>) or signalled (i.e.,
  169. <code>$_exitsignal</code> is not <code>void</code>), the convenience function
  170. <code>$_isvoid</code> can be used (see <a href="Convenience-Funs.html#Convenience-Funs">Convenience
  171. Functions</a>). For example, considering the following source code:
  172. </p>
  173. <div class="smallexample">
  174. <pre class="smallexample">#include &lt;signal.h&gt;
  175. int
  176. main (int argc, char *argv[])
  177. {
  178. raise (SIGALRM);
  179. return 0;
  180. }
  181. </pre></div>
  182. <p>A valid way of telling whether the program being debugged has exited
  183. or signalled would be:
  184. </p>
  185. <div class="smallexample">
  186. <pre class="smallexample">(gdb) define has_exited_or_signalled
  187. Type commands for definition of ``has_exited_or_signalled''.
  188. End with a line saying just ``end''.
  189. &gt;if $_isvoid ($_exitsignal)
  190. &gt;echo The program has exited\n
  191. &gt;else
  192. &gt;echo The program has signalled\n
  193. &gt;end
  194. &gt;end
  195. (gdb) run
  196. Starting program:
  197. Program terminated with signal SIGALRM, Alarm clock.
  198. The program no longer exists.
  199. (gdb) has_exited_or_signalled
  200. The program has signalled
  201. </pre></div>
  202. <p>As can be seen, <small>GDB</small> correctly informs that the program being
  203. debugged has signalled, since it calls <code>raise</code> and raises a
  204. <code>SIGALRM</code> signal. If the program being debugged had not called
  205. <code>raise</code>, then <small>GDB</small> would report a normal exit:
  206. </p>
  207. <div class="smallexample">
  208. <pre class="smallexample">(gdb) has_exited_or_signalled
  209. The program has exited
  210. </pre></div>
  211. </dd>
  212. <dt><code>$_exception</code></dt>
  213. <dd><p>The variable <code>$_exception</code> is set to the exception object being
  214. thrown at an exception-related catchpoint. See <a href="Set-Catchpoints.html#Set-Catchpoints">Set Catchpoints</a>.
  215. </p>
  216. </dd>
  217. <dt><code>$_ada_exception</code></dt>
  218. <dd><p>The variable <code>$_ada_exception</code> is set to the address of the
  219. exception being caught or thrown at an Ada exception-related
  220. catchpoint. See <a href="Set-Catchpoints.html#Set-Catchpoints">Set Catchpoints</a>.
  221. </p>
  222. </dd>
  223. <dt><code>$_probe_argc</code></dt>
  224. <dt><code>$_probe_arg0&hellip;$_probe_arg11</code></dt>
  225. <dd><p>Arguments to a static probe. See <a href="Static-Probe-Points.html#Static-Probe-Points">Static Probe Points</a>.
  226. </p>
  227. </dd>
  228. <dt><code>$_sdata</code></dt>
  229. <dd><a name="index-_0024_005fsdata_002c-inspect_002c-convenience-variable"></a>
  230. <p>The variable <code>$_sdata</code> contains extra collected static tracepoint
  231. data. See <a href="Tracepoint-Actions.html#Tracepoint-Actions">Tracepoint Action Lists</a>. Note that
  232. <code>$_sdata</code> could be empty, if not inspecting a trace buffer, or
  233. if extra static tracepoint data has not been collected.
  234. </p>
  235. </dd>
  236. <dt><code>$_siginfo</code></dt>
  237. <dd><a name="index-_0024_005fsiginfo_002c-convenience-variable"></a>
  238. <p>The variable <code>$_siginfo</code> contains extra signal information
  239. (see <a href="Signals.html#extra-signal-information">extra signal information</a>). Note that <code>$_siginfo</code>
  240. could be empty, if the application has not yet received any signals.
  241. For example, it will be empty before you execute the <code>run</code> command.
  242. </p>
  243. </dd>
  244. <dt><code>$_tlb</code></dt>
  245. <dd><a name="index-_0024_005ftlb_002c-convenience-variable"></a>
  246. <p>The variable <code>$_tlb</code> is automatically set when debugging
  247. applications running on MS-Windows in native mode or connected to
  248. gdbserver that supports the <code>qGetTIBAddr</code> request.
  249. See <a href="General-Query-Packets.html#General-Query-Packets">General Query Packets</a>.
  250. This variable contains the address of the thread information block.
  251. </p>
  252. </dd>
  253. <dt><code>$_inferior</code></dt>
  254. <dd><p>The number of the current inferior. See <a href="Inferiors-Connections-and-Programs.html#Inferiors-Connections-and-Programs">Debugging Multiple Inferiors Connections and Programs</a>.
  255. </p>
  256. </dd>
  257. <dt><code>$_thread</code></dt>
  258. <dd><p>The thread number of the current thread. See <a href="Threads.html#thread-numbers">thread numbers</a>.
  259. </p>
  260. </dd>
  261. <dt><code>$_gthread</code></dt>
  262. <dd><p>The global number of the current thread. See <a href="Threads.html#global-thread-numbers">global thread numbers</a>.
  263. </p>
  264. </dd>
  265. <dt><code>$_gdb_major</code></dt>
  266. <dt><code>$_gdb_minor</code></dt>
  267. <dd><a name="index-_0024_005fgdb_005fmajor_002c-convenience-variable"></a>
  268. <a name="index-_0024_005fgdb_005fminor_002c-convenience-variable"></a>
  269. <p>The major and minor version numbers of the running <small>GDB</small>.
  270. Development snapshots and pretest versions have their minor version
  271. incremented by one; thus, <small>GDB</small> pretest 9.11.90 will produce
  272. the value 12 for <code>$_gdb_minor</code>. These variables allow you to
  273. write scripts that work with different versions of <small>GDB</small>
  274. without errors caused by features unavailable in some of those
  275. versions.
  276. </p>
  277. </dd>
  278. <dt><code>$_shell_exitcode</code></dt>
  279. <dt><code>$_shell_exitsignal</code></dt>
  280. <dd><a name="index-_0024_005fshell_005fexitcode_002c-convenience-variable"></a>
  281. <a name="index-_0024_005fshell_005fexitsignal_002c-convenience-variable"></a>
  282. <a name="index-shell-command_002c-exit-code"></a>
  283. <a name="index-shell-command_002c-exit-signal"></a>
  284. <a name="index-exit-status-of-shell-commands"></a>
  285. <p><small>GDB</small> commands such as <code>shell</code> and <code>|</code> are launching
  286. shell commands. When a launched command terminates, <small>GDB</small>
  287. automatically maintains the variables <code>$_shell_exitcode</code>
  288. and <code>$_shell_exitsignal</code> according to the exit status of the last
  289. launched command. These variables are set and used similarly to
  290. the variables <code>$_exitcode</code> and <code>$_exitsignal</code>.
  291. </p>
  292. </dd>
  293. </dl>
  294. <hr>
  295. <div class="header">
  296. <p>
  297. Next: <a href="Convenience-Funs.html#Convenience-Funs" accesskey="n" rel="next">Convenience Funs</a>, Previous: <a href="Value-History.html#Value-History" accesskey="p" rel="prev">Value History</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>
  298. </div>
  299. </body>
  300. </html>