1<html lang="en"> 2<head> 3<title>Convenience Vars - Debugging with GDB</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Debugging with GDB"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="Data.html#Data" title="Data"> 9<link rel="prev" href="Value-History.html#Value-History" title="Value History"> 10<link rel="next" href="Registers.html#Registers" title="Registers"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<!-- 13Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 141998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 15Free Software Foundation, Inc. 16 17Permission is granted to copy, distribute and/or modify this document 18under the terms of the GNU Free Documentation License, Version 1.3 or 19any later version published by the Free Software Foundation; with the 20Invariant Sections being ``Free Software'' and ``Free Software Needs 21Free Documentation'', with the Front-Cover Texts being ``A GNU Manual,'' 22and with the Back-Cover Texts as in (a) below. 23 24(a) The FSF's Back-Cover Text is: ``You are free to copy and modify 25this GNU Manual. Buying copies from GNU Press supports the FSF in 26developing GNU and promoting software freedom.''--> 27<meta http-equiv="Content-Style-Type" content="text/css"> 28<style type="text/css"><!-- 29 pre.display { font-family:inherit } 30 pre.format { font-family:inherit } 31 pre.smalldisplay { font-family:inherit; font-size:smaller } 32 pre.smallformat { font-family:inherit; font-size:smaller } 33 pre.smallexample { font-size:smaller } 34 pre.smalllisp { font-size:smaller } 35 span.sc { font-variant:small-caps } 36 span.roman { font-family:serif; font-weight:normal; } 37 span.sansserif { font-family:sans-serif; font-weight:normal; } 38--></style> 39<link rel="stylesheet" type="text/css" href="../cs.css"> 40</head> 41<body> 42<div class="node"> 43<a name="Convenience-Vars"></a> 44<p> 45Next: <a rel="next" accesskey="n" href="Registers.html#Registers">Registers</a>, 46Previous: <a rel="previous" accesskey="p" href="Value-History.html#Value-History">Value History</a>, 47Up: <a rel="up" accesskey="u" href="Data.html#Data">Data</a> 48<hr> 49</div> 50 51<h3 class="section">10.11 Convenience Variables</h3> 52 53<p><a name="index-convenience-variables-583"></a><a name="index-user_002ddefined-variables-584"></a><span class="sc">gdb</span> provides <dfn>convenience variables</dfn> that you can use within 54<span class="sc">gdb</span> to hold on to a value and refer to it later. These variables 55exist entirely within <span class="sc">gdb</span>; they are not part of your program, and 56setting a convenience variable has no direct effect on further execution 57of your program. That is why you can use them freely. 58 59 <p>Convenience variables are prefixed with ‘<samp><span class="samp">$</span></samp>’. Any name preceded by 60‘<samp><span class="samp">$</span></samp>’ can be used for a convenience variable, unless it is one of 61the predefined machine-specific register names (see <a href="Registers.html#Registers">Registers</a>). 62(Value history references, in contrast, are <em>numbers</em> preceded 63by ‘<samp><span class="samp">$</span></samp>’. See <a href="Value-History.html#Value-History">Value History</a>.) 64 65 <p>You can save a value in a convenience variable with an assignment 66expression, just as you would set a variable in your program. 67For example: 68 69<pre class="smallexample"> set $foo = *object_ptr 70</pre> 71 <p class="noindent">would save in <code>$foo</code> the value contained in the object pointed to by 72<code>object_ptr</code>. 73 74 <p>Using a convenience variable for the first time creates it, but its 75value is <code>void</code> until you assign a new value. You can alter the 76value with another assignment at any time. 77 78 <p>Convenience variables have no fixed types. You can assign a convenience 79variable any type of value, including structures and arrays, even if 80that variable already has a value of a different type. The convenience 81variable, when used as an expression, has the type of its current value. 82 83 84<a name="index-show-convenience-585"></a> 85<a name="index-show-all-user-variables-586"></a> 86<dl><dt><code>show convenience</code><dd>Print a list of convenience variables used so far, and their values. 87Abbreviated <code>show conv</code>. 88 89 <p><a name="index-init_002dif_002dundefined-587"></a><a name="index-convenience-variables_002c-initializing-588"></a><br><dt><code>init-if-undefined $</code><var>variable</var><code> = </code><var>expression</var><dd>Set a convenience variable if it has not already been set. This is useful 90for user-defined commands that keep some state. It is similar, in concept, 91to using local static variables with initializers in C (except that 92convenience variables are global). It can also be used to allow users to 93override default values used in a command script. 94 95 <p>If the variable is already defined then the expression is not evaluated so 96any side-effects do not occur. 97</dl> 98 99 <p>One of the ways to use a convenience variable is as a counter to be 100incremented or a pointer to be advanced. For example, to print 101a field from successive elements of an array of structures: 102 103<pre class="smallexample"> set $i = 0 104 print bar[$i++]->contents 105</pre> 106 <p class="noindent">Repeat that command by typing <RET>. 107 108 <p>Some convenience variables are created automatically by <span class="sc">gdb</span> and given 109values likely to be useful. 110 111 112<a name="index-g_t_0024_005f_0040r_007b_002c-convenience-variable_007d-589"></a> 113<dl><dt><code>$_</code><dd>The variable <code>$_</code> is automatically set by the <code>x</code> command to 114the last address examined (see <a href="Memory.html#Memory">Examining Memory</a>). Other 115commands which provide a default address for <code>x</code> to examine also 116set <code>$_</code> to that address; these commands include <code>info line</code> 117and <code>info breakpoint</code>. The type of <code>$_</code> is <code>void *</code> 118except when set by the <code>x</code> command, in which case it is a pointer 119to the type of <code>$__</code>. 120 121 <p><a name="index-g_t_0024_005f_005f_0040r_007b_002c-convenience-variable_007d-590"></a><br><dt><code>$__</code><dd>The variable <code>$__</code> is automatically set by the <code>x</code> command 122to the value found in the last address examined. Its type is chosen 123to match the format in which the data was printed. 124 125 <br><dt><code>$_exitcode</code><dd><a name="index-g_t_0024_005fexitcode_0040r_007b_002c-convenience-variable_007d-591"></a>The variable <code>$_exitcode</code> is automatically set to the exit code when 126the program being debugged terminates. 127 128 <br><dt><code>$_sdata</code><dd><a name="index-g_t_0024_005fsdata_0040r_007b_002c-inspect_002c-convenience-variable_007d-592"></a>The variable <code>$_sdata</code> contains extra collected static tracepoint 129data. See <a href="Tracepoint-Actions.html#Tracepoint-Actions">Tracepoint Action Lists</a>. Note that 130<code>$_sdata</code> could be empty, if not inspecting a trace buffer, or 131if extra static tracepoint data has not been collected. 132 133 <br><dt><code>$_siginfo</code><dd><a name="index-g_t_0024_005fsiginfo_0040r_007b_002c-convenience-variable_007d-593"></a>The variable <code>$_siginfo</code> contains extra signal information 134(see <a href="extra-signal-information.html#extra-signal-information">extra signal information</a>). Note that <code>$_siginfo</code> 135could be empty, if the application has not yet received any signals. 136For example, it will be empty before you execute the <code>run</code> command. 137 138 <br><dt><code>$_tlb</code><dd><a name="index-g_t_0024_005ftlb_0040r_007b_002c-convenience-variable_007d-594"></a>The variable <code>$_tlb</code> is automatically set when debugging 139applications running on MS-Windows in native mode or connected to 140gdbserver that supports the <code>qGetTIBAddr</code> request. 141See <a href="General-Query-Packets.html#General-Query-Packets">General Query Packets</a>. 142This variable contains the address of the thread information block. 143 144 </dl> 145 146 <p>On HP-UX systems, if you refer to a function or variable name that 147begins with a dollar sign, <span class="sc">gdb</span> searches for a user or system 148name first, before it searches for a convenience variable. 149 150 <p><a name="index-convenience-functions-595"></a><span class="sc">gdb</span> also supplies some <dfn>convenience functions</dfn>. These 151have a syntax similar to convenience variables. A convenience 152function can be used in an expression just like an ordinary function; 153however, a convenience function is implemented internally to 154<span class="sc">gdb</span>. 155 156 <dl> 157<dt><code>help function</code><dd><a name="index-help-function-596"></a><a name="index-show-all-convenience-functions-597"></a>Print a list of all convenience functions. 158</dl> 159 160 </body></html> 161 162