Conditions.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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-2020 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Free Software" and "Free Software Needs
  8. Free Documentation", with the Front-Cover Texts being "A GNU Manual,"
  9. and with the Back-Cover Texts as in (a) below.
  10. (a) The FSF's Back-Cover Text is: "You are free to copy and modify
  11. this GNU Manual. Buying copies from GNU Press supports the FSF in
  12. developing GNU and promoting software freedom." -->
  13. <!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ -->
  14. <head>
  15. <title>Debugging with GDB: Conditions</title>
  16. <meta name="description" content="Debugging with GDB: Conditions">
  17. <meta name="keywords" content="Debugging with GDB: Conditions">
  18. <meta name="resource-type" content="document">
  19. <meta name="distribution" content="global">
  20. <meta name="Generator" content="makeinfo">
  21. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  22. <link href="index.html#Top" rel="start" title="Top">
  23. <link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
  24. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  25. <link href="Breakpoints.html#Breakpoints" rel="up" title="Breakpoints">
  26. <link href="Break-Commands.html#Break-Commands" rel="next" title="Break Commands">
  27. <link href="Disabling.html#Disabling" rel="previous" title="Disabling">
  28. <style type="text/css">
  29. <!--
  30. a.summary-letter {text-decoration: none}
  31. blockquote.smallquotation {font-size: smaller}
  32. div.display {margin-left: 3.2em}
  33. div.example {margin-left: 3.2em}
  34. div.indentedblock {margin-left: 3.2em}
  35. div.lisp {margin-left: 3.2em}
  36. div.smalldisplay {margin-left: 3.2em}
  37. div.smallexample {margin-left: 3.2em}
  38. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  39. div.smalllisp {margin-left: 3.2em}
  40. kbd {font-style:oblique}
  41. pre.display {font-family: inherit}
  42. pre.format {font-family: inherit}
  43. pre.menu-comment {font-family: serif}
  44. pre.menu-preformatted {font-family: serif}
  45. pre.smalldisplay {font-family: inherit; font-size: smaller}
  46. pre.smallexample {font-size: smaller}
  47. pre.smallformat {font-family: inherit; font-size: smaller}
  48. pre.smalllisp {font-size: smaller}
  49. span.nocodebreak {white-space:nowrap}
  50. span.nolinebreak {white-space:nowrap}
  51. span.roman {font-family:serif; font-weight:normal}
  52. span.sansserif {font-family:sans-serif; font-weight:normal}
  53. ul.no-bullet {list-style: none}
  54. -->
  55. </style>
  56. </head>
  57. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  58. <a name="Conditions"></a>
  59. <div class="header">
  60. <p>
  61. Next: <a href="Break-Commands.html#Break-Commands" accesskey="n" rel="next">Break Commands</a>, Previous: <a href="Disabling.html#Disabling" accesskey="p" rel="previous">Disabling</a>, Up: <a href="Breakpoints.html#Breakpoints" accesskey="u" rel="up">Breakpoints</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>
  62. </div>
  63. <hr>
  64. <a name="Break-Conditions"></a>
  65. <h4 class="subsection">5.1.6 Break Conditions</h4>
  66. <a name="index-conditional-breakpoints"></a>
  67. <a name="index-breakpoint-conditions"></a>
  68. <p>The simplest sort of breakpoint breaks every time your program reaches a
  69. specified place. You can also specify a <em>condition</em> for a
  70. breakpoint. A condition is just a Boolean expression in your
  71. programming language (see <a href="Expressions.html#Expressions">Expressions</a>). A breakpoint with
  72. a condition evaluates the expression each time your program reaches it,
  73. and your program stops only if the condition is <em>true</em>.
  74. </p>
  75. <p>This is the converse of using assertions for program validation; in that
  76. situation, you want to stop when the assertion is violated&mdash;that is,
  77. when the condition is false. In C, if you want to test an assertion expressed
  78. by the condition <var>assert</var>, you should set the condition
  79. &lsquo;<samp>! <var>assert</var></samp>&rsquo; on the appropriate breakpoint.
  80. </p>
  81. <p>Conditions are also accepted for watchpoints; you may not need them,
  82. since a watchpoint is inspecting the value of an expression anyhow&mdash;but
  83. it might be simpler, say, to just set a watchpoint on a variable name,
  84. and specify a condition that tests whether the new value is an interesting
  85. one.
  86. </p>
  87. <p>Break conditions can have side effects, and may even call functions in
  88. your program. This can be useful, for example, to activate functions
  89. that log program progress, or to use your own print functions to
  90. format special data structures. The effects are completely predictable
  91. unless there is another enabled breakpoint at the same address. (In
  92. that case, <small>GDB</small> might see the other breakpoint first and stop your
  93. program without checking the condition of this one.) Note that
  94. breakpoint commands are usually more convenient and flexible than break
  95. conditions for the
  96. purpose of performing side effects when a breakpoint is reached
  97. (see <a href="Break-Commands.html#Break-Commands">Breakpoint Command Lists</a>).
  98. </p>
  99. <p>Breakpoint conditions can also be evaluated on the target&rsquo;s side if
  100. the target supports it. Instead of evaluating the conditions locally,
  101. <small>GDB</small> encodes the expression into an agent expression
  102. (see <a href="Agent-Expressions.html#Agent-Expressions">Agent Expressions</a>) suitable for execution on the target,
  103. independently of <small>GDB</small>. Global variables become raw memory
  104. locations, locals become stack accesses, and so forth.
  105. </p>
  106. <p>In this case, <small>GDB</small> will only be notified of a breakpoint trigger
  107. when its condition evaluates to true. This mechanism may provide faster
  108. response times depending on the performance characteristics of the target
  109. since it does not need to keep <small>GDB</small> informed about
  110. every breakpoint trigger, even those with false conditions.
  111. </p>
  112. <p>Break conditions can be specified when a breakpoint is set, by using
  113. &lsquo;<samp>if</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
  114. with the <code>condition</code> command.
  115. </p>
  116. <p>You can also use the <code>if</code> keyword with the <code>watch</code> command.
  117. The <code>catch</code> command does not recognize the <code>if</code> keyword;
  118. <code>condition</code> is the only way to impose a further condition on a
  119. catchpoint.
  120. </p>
  121. <dl compact="compact">
  122. <dd><a name="index-condition"></a>
  123. </dd>
  124. <dt><code>condition <var>bnum</var> <var>expression</var></code></dt>
  125. <dd><p>Specify <var>expression</var> as the break condition for breakpoint,
  126. watchpoint, or catchpoint number <var>bnum</var>. After you set a condition,
  127. breakpoint <var>bnum</var> stops your program only if the value of
  128. <var>expression</var> is true (nonzero, in C). When you use
  129. <code>condition</code>, <small>GDB</small> checks <var>expression</var> immediately for
  130. syntactic correctness, and to determine whether symbols in it have
  131. referents in the context of your breakpoint. If <var>expression</var> uses
  132. symbols not referenced in the context of the breakpoint, <small>GDB</small>
  133. prints an error message:
  134. </p>
  135. <div class="smallexample">
  136. <pre class="smallexample">No symbol &quot;foo&quot; in current context.
  137. </pre></div>
  138. <p><small>GDB</small> does
  139. not actually evaluate <var>expression</var> at the time the <code>condition</code>
  140. command (or a command that sets a breakpoint with a condition, like
  141. <code>break if &hellip;</code>) is given, however. See <a href="Expressions.html#Expressions">Expressions</a>.
  142. </p>
  143. </dd>
  144. <dt><code>condition <var>bnum</var></code></dt>
  145. <dd><p>Remove the condition from breakpoint number <var>bnum</var>. It becomes
  146. an ordinary unconditional breakpoint.
  147. </p></dd>
  148. </dl>
  149. <a name="index-ignore-count-_0028of-breakpoint_0029"></a>
  150. <p>A special case of a breakpoint condition is to stop only when the
  151. breakpoint has been reached a certain number of times. This is so
  152. useful that there is a special way to do it, using the <em>ignore
  153. count</em> of the breakpoint. Every breakpoint has an ignore count, which
  154. is an integer. Most of the time, the ignore count is zero, and
  155. therefore has no effect. But if your program reaches a breakpoint whose
  156. ignore count is positive, then instead of stopping, it just decrements
  157. the ignore count by one and continues. As a result, if the ignore count
  158. value is <var>n</var>, the breakpoint does not stop the next <var>n</var> times
  159. your program reaches it.
  160. </p>
  161. <dl compact="compact">
  162. <dd><a name="index-ignore"></a>
  163. </dd>
  164. <dt><code>ignore <var>bnum</var> <var>count</var></code></dt>
  165. <dd><p>Set the ignore count of breakpoint number <var>bnum</var> to <var>count</var>.
  166. The next <var>count</var> times the breakpoint is reached, your program&rsquo;s
  167. execution does not stop; other than to decrement the ignore count, <small>GDB</small>
  168. takes no action.
  169. </p>
  170. <p>To make the breakpoint stop the next time it is reached, specify
  171. a count of zero.
  172. </p>
  173. <p>When you use <code>continue</code> to resume execution of your program from a
  174. breakpoint, you can specify an ignore count directly as an argument to
  175. <code>continue</code>, rather than using <code>ignore</code>. See <a href="Continuing-and-Stepping.html#Continuing-and-Stepping">Continuing and Stepping</a>.
  176. </p>
  177. <p>If a breakpoint has a positive ignore count and a condition, the
  178. condition is not checked. Once the ignore count reaches zero,
  179. <small>GDB</small> resumes checking the condition.
  180. </p>
  181. <p>You could achieve the effect of the ignore count with a condition such
  182. as &lsquo;<samp><span class="nolinebreak">$foo--</span>&nbsp;&lt;=&nbsp;0</samp>&rsquo;<!-- /@w --> using a debugger convenience variable that
  183. is decremented each time. See <a href="Convenience-Vars.html#Convenience-Vars">Convenience
  184. Variables</a>.
  185. </p></dd>
  186. </dl>
  187. <p>Ignore counts apply to breakpoints, watchpoints, and catchpoints.
  188. </p>
  189. <hr>
  190. <div class="header">
  191. <p>
  192. Next: <a href="Break-Commands.html#Break-Commands" accesskey="n" rel="next">Break Commands</a>, Previous: <a href="Disabling.html#Disabling" accesskey="p" rel="previous">Disabling</a>, Up: <a href="Breakpoints.html#Breakpoints" accesskey="u" rel="up">Breakpoints</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>
  193. </div>
  194. </body>
  195. </html>