blob: b5ae8f977f8f45b2301ff0be301972aa2ae8fee8 [file] [log] [blame] [edit]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Copyright (C) 1988-2015 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "Free Software" and "Free Software Needs
Free Documentation", with the Front-Cover Texts being "A GNU Manual,"
and with the Back-Cover Texts as in (a) below.
(a) The FSF's Back-Cover Text is: "You are free to copy and modify
this GNU Manual. Buying copies from GNU Press supports the FSF in
developing GNU and promoting software freedom." -->
<!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
<head>
<title>Debugging with GDB: Convenience Vars</title>
<meta name="description" content="Debugging with GDB: Convenience Vars">
<meta name="keywords" content="Debugging with GDB: Convenience Vars">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Data.html#Data" rel="up" title="Data">
<link href="Convenience-Funs.html#Convenience-Funs" rel="next" title="Convenience Funs">
<link href="Value-History.html#Value-History" rel="prev" title="Value History">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {margin-left: 3.2em}
kbd {font-style:oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Convenience-Vars"></a>
<div class="header">
<p>
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>
</div>
<hr>
<a name="Convenience-Variables"></a>
<h3 class="section">10.11 Convenience Variables</h3>
<a name="index-convenience-variables"></a>
<a name="index-user_002ddefined-variables"></a>
<p><small>GDB</small> provides <em>convenience variables</em> that you can use within
<small>GDB</small> to hold on to a value and refer to it later. These variables
exist entirely within <small>GDB</small>; they are not part of your program, and
setting a convenience variable has no direct effect on further execution
of your program. That is why you can use them freely.
</p>
<p>Convenience variables are prefixed with &lsquo;<samp>$</samp>&rsquo;. Any name preceded by
&lsquo;<samp>$</samp>&rsquo; can be used for a convenience variable, unless it is one of
the predefined machine-specific register names (see <a href="Registers.html#Registers">Registers</a>).
(Value history references, in contrast, are <em>numbers</em> preceded
by &lsquo;<samp>$</samp>&rsquo;. See <a href="Value-History.html#Value-History">Value History</a>.)
</p>
<p>You can save a value in a convenience variable with an assignment
expression, just as you would set a variable in your program.
For example:
</p>
<div class="smallexample">
<pre class="smallexample">set $foo = *object_ptr
</pre></div>
<p>would save in <code>$foo</code> the value contained in the object pointed to by
<code>object_ptr</code>.
</p>
<p>Using a convenience variable for the first time creates it, but its
value is <code>void</code> until you assign a new value. You can alter the
value with another assignment at any time.
</p>
<p>Convenience variables have no fixed types. You can assign a convenience
variable any type of value, including structures and arrays, even if
that variable already has a value of a different type. The convenience
variable, when used as an expression, has the type of its current value.
</p>
<dl compact="compact">
<dd><a name="index-show-convenience"></a>
<a name="index-show-all-user-variables-and-functions"></a>
</dd>
<dt><code>show convenience</code></dt>
<dd><p>Print a list of convenience variables used so far, and their values,
as well as a list of the convenience functions.
Abbreviated <code>show conv</code>.
</p>
<a name="index-init_002dif_002dundefined"></a>
<a name="index-convenience-variables_002c-initializing"></a>
</dd>
<dt><code>init-if-undefined $<var>variable</var> = <var>expression</var></code></dt>
<dd><p>Set a convenience variable if it has not already been set. This is useful
for user-defined commands that keep some state. It is similar, in concept,
to using local static variables with initializers in C (except that
convenience variables are global). It can also be used to allow users to
override default values used in a command script.
</p>
<p>If the variable is already defined then the expression is not evaluated so
any side-effects do not occur.
</p></dd>
</dl>
<p>One of the ways to use a convenience variable is as a counter to be
incremented or a pointer to be advanced. For example, to print
a field from successive elements of an array of structures:
</p>
<div class="smallexample">
<pre class="smallexample">set $i = 0
print bar[$i++]-&gt;contents
</pre></div>
<p>Repeat that command by typing <tt class="key">RET</tt>.
</p>
<p>Some convenience variables are created automatically by <small>GDB</small> and given
values likely to be useful.
</p>
<dl compact="compact">
<dd><a name="index-_0024_005f_002c-convenience-variable"></a>
</dd>
<dt><code>$_</code></dt>
<dd><p>The variable <code>$_</code> is automatically set by the <code>x</code> command to
the last address examined (see <a href="Memory.html#Memory">Examining Memory</a>). Other
commands which provide a default address for <code>x</code> to examine also
set <code>$_</code> to that address; these commands include <code>info line</code>
and <code>info breakpoint</code>. The type of <code>$_</code> is <code>void *</code>
except when set by the <code>x</code> command, in which case it is a pointer
to the type of <code>$__</code>.
</p>
<a name="index-_0024_005f_005f_002c-convenience-variable"></a>
</dd>
<dt><code>$__</code></dt>
<dd><p>The variable <code>$__</code> is automatically set by the <code>x</code> command
to the value found in the last address examined. Its type is chosen
to match the format in which the data was printed.
</p>
</dd>
<dt><code>$_exitcode</code></dt>
<dd><a name="index-_0024_005fexitcode_002c-convenience-variable"></a>
<p>When the program being debugged terminates normally, <small>GDB</small>
automatically sets this variable to the exit code of the program, and
resets <code>$_exitsignal</code> to <code>void</code>.
</p>
</dd>
<dt><code>$_exitsignal</code></dt>
<dd><a name="index-_0024_005fexitsignal_002c-convenience-variable"></a>
<p>When the program being debugged dies due to an uncaught signal,
<small>GDB</small> automatically sets this variable to that signal&rsquo;s number,
and resets <code>$_exitcode</code> to <code>void</code>.
</p>
<p>To distinguish between whether the program being debugged has exited
(i.e., <code>$_exitcode</code> is not <code>void</code>) or signalled (i.e.,
<code>$_exitsignal</code> is not <code>void</code>), the convenience function
<code>$_isvoid</code> can be used (see <a href="Convenience-Funs.html#Convenience-Funs">Convenience
Functions</a>). For example, considering the following source code:
</p>
<div class="smallexample">
<pre class="smallexample">#include &lt;signal.h&gt;
int
main (int argc, char *argv[])
{
raise (SIGALRM);
return 0;
}
</pre></div>
<p>A valid way of telling whether the program being debugged has exited
or signalled would be:
</p>
<div class="smallexample">
<pre class="smallexample">(gdb) define has_exited_or_signalled
Type commands for definition of ``has_exited_or_signalled''.
End with a line saying just ``end''.
&gt;if $_isvoid ($_exitsignal)
&gt;echo The program has exited\n
&gt;else
&gt;echo The program has signalled\n
&gt;end
&gt;end
(gdb) run
Starting program:
Program terminated with signal SIGALRM, Alarm clock.
The program no longer exists.
(gdb) has_exited_or_signalled
The program has signalled
</pre></div>
<p>As can be seen, <small>GDB</small> correctly informs that the program being
debugged has signalled, since it calls <code>raise</code> and raises a
<code>SIGALRM</code> signal. If the program being debugged had not called
<code>raise</code>, then <small>GDB</small> would report a normal exit:
</p>
<div class="smallexample">
<pre class="smallexample">(gdb) has_exited_or_signalled
The program has exited
</pre></div>
</dd>
<dt><code>$_exception</code></dt>
<dd><p>The variable <code>$_exception</code> is set to the exception object being
thrown at an exception-related catchpoint. See <a href="Set-Catchpoints.html#Set-Catchpoints">Set Catchpoints</a>.
</p>
</dd>
<dt><code>$_probe_argc</code></dt>
<dt><code>$_probe_arg0&hellip;$_probe_arg11</code></dt>
<dd><p>Arguments to a static probe. See <a href="Static-Probe-Points.html#Static-Probe-Points">Static Probe Points</a>.
</p>
</dd>
<dt><code>$_sdata</code></dt>
<dd><a name="index-_0024_005fsdata_002c-inspect_002c-convenience-variable"></a>
<p>The variable <code>$_sdata</code> contains extra collected static tracepoint
data. See <a href="Tracepoint-Actions.html#Tracepoint-Actions">Tracepoint Action Lists</a>. Note that
<code>$_sdata</code> could be empty, if not inspecting a trace buffer, or
if extra static tracepoint data has not been collected.
</p>
</dd>
<dt><code>$_siginfo</code></dt>
<dd><a name="index-_0024_005fsiginfo_002c-convenience-variable"></a>
<p>The variable <code>$_siginfo</code> contains extra signal information
(see <a href="Signals.html#extra-signal-information">extra signal information</a>). Note that <code>$_siginfo</code>
could be empty, if the application has not yet received any signals.
For example, it will be empty before you execute the <code>run</code> command.
</p>
</dd>
<dt><code>$_tlb</code></dt>
<dd><a name="index-_0024_005ftlb_002c-convenience-variable"></a>
<p>The variable <code>$_tlb</code> is automatically set when debugging
applications running on MS-Windows in native mode or connected to
gdbserver that supports the <code>qGetTIBAddr</code> request.
See <a href="General-Query-Packets.html#General-Query-Packets">General Query Packets</a>.
This variable contains the address of the thread information block.
</p>
</dd>
</dl>
<p>On HP-UX systems, if you refer to a function or variable name that
begins with a dollar sign, <small>GDB</small> searches for a user or system
name first, before it searches for a convenience variable.
</p>
<hr>
<div class="header">
<p>
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>
</div>
</body>
</html>