blob: 316747584411d65c63bda75f34888fb102df1650 [file] [log] [blame]
<html lang="en">
<head>
<title>Conditions - Debugging with GDB</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Debugging with GDB">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Breakpoints.html#Breakpoints" title="Breakpoints">
<link rel="prev" href="Disabling.html#Disabling" title="Disabling">
<link rel="next" href="Break-Commands.html#Break-Commands" title="Break Commands">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<!--
Copyright (C) 1988-2019 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.''
-->
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
</head>
<body>
<div class="node">
<a name="Conditions"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="Break-Commands.html#Break-Commands">Break Commands</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Disabling.html#Disabling">Disabling</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Breakpoints.html#Breakpoints">Breakpoints</a>
<hr>
</div>
<h4 class="subsection">5.1.6 Break Conditions</h4>
<p><a name="index-conditional-breakpoints-321"></a><a name="index-breakpoint-conditions-322"></a>
<!-- FIXME what is scope of break condition expr? Context where wanted? -->
<!-- in particular for a watchpoint? -->
The simplest sort of breakpoint breaks every time your program reaches a
specified place. You can also specify a <dfn>condition</dfn> for a
breakpoint. A condition is just a Boolean expression in your
programming language (see <a href="Expressions.html#Expressions">Expressions</a>). A breakpoint with
a condition evaluates the expression each time your program reaches it,
and your program stops only if the condition is <em>true</em>.
<p>This is the converse of using assertions for program validation; in that
situation, you want to stop when the assertion is violated&mdash;that is,
when the condition is false. In C, if you want to test an assertion expressed
by the condition <var>assert</var>, you should set the condition
&lsquo;<samp><span class="samp">! </span><var>assert</var></samp>&rsquo; on the appropriate breakpoint.
<p>Conditions are also accepted for watchpoints; you may not need them,
since a watchpoint is inspecting the value of an expression anyhow&mdash;but
it might be simpler, say, to just set a watchpoint on a variable name,
and specify a condition that tests whether the new value is an interesting
one.
<p>Break conditions can have side effects, and may even call functions in
your program. This can be useful, for example, to activate functions
that log program progress, or to use your own print functions to
format special data structures. The effects are completely predictable
unless there is another enabled breakpoint at the same address. (In
that case, <span class="sc">gdb</span> might see the other breakpoint first and stop your
program without checking the condition of this one.) Note that
breakpoint commands are usually more convenient and flexible than break
conditions for the
purpose of performing side effects when a breakpoint is reached
(see <a href="Break-Commands.html#Break-Commands">Breakpoint Command Lists</a>).
<p>Breakpoint conditions can also be evaluated on the target's side if
the target supports it. Instead of evaluating the conditions locally,
<span class="sc">gdb</span> encodes the expression into an agent expression
(see <a href="Agent-Expressions.html#Agent-Expressions">Agent Expressions</a>) suitable for execution on the target,
independently of <span class="sc">gdb</span>. Global variables become raw memory
locations, locals become stack accesses, and so forth.
<p>In this case, <span class="sc">gdb</span> will only be notified of a breakpoint trigger
when its condition evaluates to true. This mechanism may provide faster
response times depending on the performance characteristics of the target
since it does not need to keep <span class="sc">gdb</span> informed about
every breakpoint trigger, even those with false conditions.
<p>Break conditions can be specified when a breakpoint is set, by using
&lsquo;<samp><span class="samp">if</span></samp>&rsquo; in the arguments to the <code>break</code> command. See <a href="Set-Breaks.html#Set-Breaks">Setting Breakpoints</a>. They can also be changed at any time
with the <code>condition</code> command.
<p>You can also use the <code>if</code> keyword with the <code>watch</code> command.
The <code>catch</code> command does not recognize the <code>if</code> keyword;
<code>condition</code> is the only way to impose a further condition on a
catchpoint.
<a name="index-condition-323"></a>
<dl><dt><code>condition </code><var>bnum</var> <var>expression</var><dd>Specify <var>expression</var> as the break condition for breakpoint,
watchpoint, or catchpoint number <var>bnum</var>. After you set a condition,
breakpoint <var>bnum</var> stops your program only if the value of
<var>expression</var> is true (nonzero, in C). When you use
<code>condition</code>, <span class="sc">gdb</span> checks <var>expression</var> immediately for
syntactic correctness, and to determine whether symbols in it have
referents in the context of your breakpoint. If <var>expression</var> uses
symbols not referenced in the context of the breakpoint, <span class="sc">gdb</span>
prints an error message:
<pre class="smallexample"> No symbol "foo" in current context.
</pre>
<p class="noindent"><span class="sc">gdb</span> does
not actually evaluate <var>expression</var> at the time the <code>condition</code>
command (or a command that sets a breakpoint with a condition, like
<code>break if ...</code>) is given, however. See <a href="Expressions.html#Expressions">Expressions</a>.
<br><dt><code>condition </code><var>bnum</var><dd>Remove the condition from breakpoint number <var>bnum</var>. It becomes
an ordinary unconditional breakpoint.
</dl>
<p><a name="index-ignore-count-_0028of-breakpoint_0029-324"></a>A special case of a breakpoint condition is to stop only when the
breakpoint has been reached a certain number of times. This is so
useful that there is a special way to do it, using the <dfn>ignore
count</dfn> of the breakpoint. Every breakpoint has an ignore count, which
is an integer. Most of the time, the ignore count is zero, and
therefore has no effect. But if your program reaches a breakpoint whose
ignore count is positive, then instead of stopping, it just decrements
the ignore count by one and continues. As a result, if the ignore count
value is <var>n</var>, the breakpoint does not stop the next <var>n</var> times
your program reaches it.
<a name="index-ignore-325"></a>
<dl><dt><code>ignore </code><var>bnum</var> <var>count</var><dd>Set the ignore count of breakpoint number <var>bnum</var> to <var>count</var>.
The next <var>count</var> times the breakpoint is reached, your program's
execution does not stop; other than to decrement the ignore count, <span class="sc">gdb</span>
takes no action.
<p>To make the breakpoint stop the next time it is reached, specify
a count of zero.
<p>When you use <code>continue</code> to resume execution of your program from a
breakpoint, you can specify an ignore count directly as an argument to
<code>continue</code>, rather than using <code>ignore</code>. See <a href="Continuing-and-Stepping.html#Continuing-and-Stepping">Continuing and Stepping</a>.
<p>If a breakpoint has a positive ignore count and a condition, the
condition is not checked. Once the ignore count reaches zero,
<span class="sc">gdb</span> resumes checking the condition.
<p>You could achieve the effect of the ignore count with a condition such
as &lsquo;<samp><span class="samp">$foo--&nbsp;&lt;=&nbsp;0</span></samp>&rsquo;<!-- /@w --> using a debugger convenience variable that
is decremented each time. See <a href="Convenience-Vars.html#Convenience-Vars">Convenience Variables</a>.
</dl>
<p>Ignore counts apply to breakpoints, watchpoints, and catchpoints.
</body></html>