hc
2024-03-22 f63cd4c03ea42695d5f9b0e1798edd196923aae6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Copyright (C) 1988-2021 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.1, 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="previous" 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="previous">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>$_ada_exception</code></dt>
<dd><p>The variable <code>$_ada_exception</code> is set to the address of the
exception being caught or thrown at an Ada 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>
<dt><code>$_inferior</code></dt>
<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>.
</p>
</dd>
<dt><code>$_thread</code></dt>
<dd><p>The thread number of the current thread.  See <a href="Threads.html#thread-numbers">thread numbers</a>.
</p>
</dd>
<dt><code>$_gthread</code></dt>
<dd><p>The global number of the current thread.  See <a href="Threads.html#global-thread-numbers">global thread numbers</a>.
</p>
</dd>
<dt><code>$_gdb_major</code></dt>
<dt><code>$_gdb_minor</code></dt>
<dd><a name="index-_0024_005fgdb_005fmajor_002c-convenience-variable"></a>
<a name="index-_0024_005fgdb_005fminor_002c-convenience-variable"></a>
<p>The major and minor version numbers of the running <small>GDB</small>.
Development snapshots and pretest versions have their minor version
incremented by one; thus, <small>GDB</small> pretest 9.11.90 will produce
the value 12 for <code>$_gdb_minor</code>.  These variables allow you to
write scripts that work with different versions of <small>GDB</small>
without errors caused by features unavailable in some of those
versions.
</p>
</dd>
<dt><code>$_shell_exitcode</code></dt>
<dt><code>$_shell_exitsignal</code></dt>
<dd><a name="index-_0024_005fshell_005fexitcode_002c-convenience-variable"></a>
<a name="index-_0024_005fshell_005fexitsignal_002c-convenience-variable"></a>
<a name="index-shell-command_002c-exit-code"></a>
<a name="index-shell-command_002c-exit-signal"></a>
<a name="index-exit-status-of-shell-commands"></a>
<p><small>GDB</small> commands such as <code>shell</code> and <code>|</code> are launching
shell commands.  When a launched command terminates, <small>GDB</small>
automatically maintains the variables <code>$_shell_exitcode</code>
and <code>$_shell_exitsignal</code> according to the exit status of the last
launched command.  These variables are set and used similarly to
the variables <code>$_exitcode</code> and <code>$_exitsignal</code>.
</p>
</dd>
</dl>
 
<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="previous">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>