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