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-2021 Free Software Foundation, Inc. 4 5Permission is granted to copy, distribute and/or modify this document 6under the terms of the GNU Free Documentation License, Version 1.3 or 7any later version published by the Free Software Foundation; with the 8Invariant Sections being "Free Software" and "Free Software Needs 9Free Documentation", with the Front-Cover Texts being "A GNU Manual," 10and with the Back-Cover Texts as in (a) below. 11 12(a) The FSF's Back-Cover Text is: "You are free to copy and modify 13this GNU Manual. Buying copies from GNU Press supports the FSF in 14developing GNU and promoting software freedom." --> 15<!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ --> 16<head> 17<title>Debugging with GDB: Set Watchpoints</title> 18 19<meta name="description" content="Debugging with GDB: Set Watchpoints"> 20<meta name="keywords" content="Debugging with GDB: Set Watchpoints"> 21<meta name="resource-type" content="document"> 22<meta name="distribution" content="global"> 23<meta name="Generator" content="makeinfo"> 24<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 25<link href="index.html#Top" rel="start" title="Top"> 26<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index"> 27<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents"> 28<link href="Breakpoints.html#Breakpoints" rel="up" title="Breakpoints"> 29<link href="Set-Catchpoints.html#Set-Catchpoints" rel="next" title="Set Catchpoints"> 30<link href="Set-Breaks.html#Set-Breaks" rel="previous" title="Set Breaks"> 31<style type="text/css"> 32<!-- 33a.summary-letter {text-decoration: none} 34blockquote.smallquotation {font-size: smaller} 35div.display {margin-left: 3.2em} 36div.example {margin-left: 3.2em} 37div.indentedblock {margin-left: 3.2em} 38div.lisp {margin-left: 3.2em} 39div.smalldisplay {margin-left: 3.2em} 40div.smallexample {margin-left: 3.2em} 41div.smallindentedblock {margin-left: 3.2em; font-size: smaller} 42div.smalllisp {margin-left: 3.2em} 43kbd {font-style:oblique} 44pre.display {font-family: inherit} 45pre.format {font-family: inherit} 46pre.menu-comment {font-family: serif} 47pre.menu-preformatted {font-family: serif} 48pre.smalldisplay {font-family: inherit; font-size: smaller} 49pre.smallexample {font-size: smaller} 50pre.smallformat {font-family: inherit; font-size: smaller} 51pre.smalllisp {font-size: smaller} 52span.nocodebreak {white-space:nowrap} 53span.nolinebreak {white-space:nowrap} 54span.roman {font-family:serif; font-weight:normal} 55span.sansserif {font-family:sans-serif; font-weight:normal} 56ul.no-bullet {list-style: none} 57--> 58</style> 59 60 61</head> 62 63<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000"> 64<a name="Set-Watchpoints"></a> 65<div class="header"> 66<p> 67Next: <a href="Set-Catchpoints.html#Set-Catchpoints" accesskey="n" rel="next">Set Catchpoints</a>, Previous: <a href="Set-Breaks.html#Set-Breaks" accesskey="p" rel="previous">Set Breaks</a>, Up: <a href="Breakpoints.html#Breakpoints" accesskey="u" rel="up">Breakpoints</a> [<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> 68</div> 69<hr> 70<a name="Setting-Watchpoints"></a> 71<h4 class="subsection">5.1.2 Setting Watchpoints</h4> 72 73<a name="index-setting-watchpoints"></a> 74<p>You can use a watchpoint to stop execution whenever the value of an 75expression changes, without having to predict a particular place where 76this may happen. (This is sometimes called a <em>data breakpoint</em>.) 77The expression may be as simple as the value of a single variable, or 78as complex as many variables combined by operators. Examples include: 79</p> 80<ul> 81<li> A reference to the value of a single variable. 82 83</li><li> An address cast to an appropriate data type. For example, 84‘<samp>*(int *)0x12345678</samp>’ will watch a 4-byte region at the specified 85address (assuming an <code>int</code> occupies 4 bytes). 86 87</li><li> An arbitrarily complex expression, such as ‘<samp>a*b + c/d</samp>’. The 88expression can use any operators valid in the program’s native 89language (see <a href="Languages.html#Languages">Languages</a>). 90</li></ul> 91 92<p>You can set a watchpoint on an expression even if the expression can 93not be evaluated yet. For instance, you can set a watchpoint on 94‘<samp>*global_ptr</samp>’ before ‘<samp>global_ptr</samp>’ is initialized. 95<small>GDB</small> will stop when your program sets ‘<samp>global_ptr</samp>’ and 96the expression produces a valid value. If the expression becomes 97valid in some other way than changing a variable (e.g. if the memory 98pointed to by ‘<samp>*global_ptr</samp>’ becomes readable as the result of a 99<code>malloc</code> call), <small>GDB</small> may not stop until the next time 100the expression changes. 101</p> 102<a name="index-software-watchpoints"></a> 103<a name="index-hardware-watchpoints"></a> 104<p>Depending on your system, watchpoints may be implemented in software or 105hardware. <small>GDB</small> does software watchpointing by single-stepping your 106program and testing the variable’s value each time, which is hundreds of 107times slower than normal execution. (But this may still be worth it, to 108catch errors where you have no clue what part of your program is the 109culprit.) 110</p> 111<p>On some systems, such as most PowerPC or x86-based targets, 112<small>GDB</small> includes support for hardware watchpoints, which do not 113slow down the running of your program. 114</p> 115<dl compact="compact"> 116<dd><a name="index-watch"></a> 117</dd> 118<dt><code>watch <span class="roman">[</span>-l<span class="roman">|</span>-location<span class="roman">]</span> <var>expr</var> <span class="roman">[</span>thread <var>thread-id</var><span class="roman">]</span> <span class="roman">[</span>mask <var>maskvalue</var><span class="roman">]</span></code></dt> 119<dd><p>Set a watchpoint for an expression. <small>GDB</small> will break when the 120expression <var>expr</var> is written into by the program and its value 121changes. The simplest (and the most popular) use of this command is 122to watch the value of a single variable: 123</p> 124<div class="smallexample"> 125<pre class="smallexample">(gdb) watch foo 126</pre></div> 127 128<p>If the command includes a <code><span class="roman">[</span>thread <var>thread-id</var><span class="roman">]</span></code> 129argument, <small>GDB</small> breaks only when the thread identified by 130<var>thread-id</var> changes the value of <var>expr</var>. If any other threads 131change the value of <var>expr</var>, <small>GDB</small> will not break. Note 132that watchpoints restricted to a single thread in this way only work 133with Hardware Watchpoints. 134</p> 135<p>Ordinarily a watchpoint respects the scope of variables in <var>expr</var> 136(see below). The <code>-location</code> argument tells <small>GDB</small> to 137instead watch the memory referred to by <var>expr</var>. In this case, 138<small>GDB</small> will evaluate <var>expr</var>, take the address of the result, 139and watch the memory at that address. The type of the result is used 140to determine the size of the watched memory. If the expression’s 141result does not have an address, then <small>GDB</small> will print an 142error. 143</p> 144<p>The <code><span class="roman">[</span>mask <var>maskvalue</var><span class="roman">]</span></code> argument allows creation 145of masked watchpoints, if the current architecture supports this 146feature (e.g., PowerPC Embedded architecture, see <a href="PowerPC-Embedded.html#PowerPC-Embedded">PowerPC Embedded</a>.) A <em>masked watchpoint</em> specifies a mask in addition 147to an address to watch. The mask specifies that some bits of an address 148(the bits which are reset in the mask) should be ignored when matching 149the address accessed by the inferior against the watchpoint address. 150Thus, a masked watchpoint watches many addresses simultaneously—those 151addresses whose unmasked bits are identical to the unmasked bits in the 152watchpoint address. The <code>mask</code> argument implies <code>-location</code>. 153Examples: 154</p> 155<div class="smallexample"> 156<pre class="smallexample">(gdb) watch foo mask 0xffff00ff 157(gdb) watch *0xdeadbeef mask 0xffffff00 158</pre></div> 159 160<a name="index-rwatch"></a> 161</dd> 162<dt><code>rwatch <span class="roman">[</span>-l<span class="roman">|</span>-location<span class="roman">]</span> <var>expr</var> <span class="roman">[</span>thread <var>thread-id</var><span class="roman">]</span> <span class="roman">[</span>mask <var>maskvalue</var><span class="roman">]</span></code></dt> 163<dd><p>Set a watchpoint that will break when the value of <var>expr</var> is read 164by the program. 165</p> 166<a name="index-awatch"></a> 167</dd> 168<dt><code>awatch <span class="roman">[</span>-l<span class="roman">|</span>-location<span class="roman">]</span> <var>expr</var> <span class="roman">[</span>thread <var>thread-id</var><span class="roman">]</span> <span class="roman">[</span>mask <var>maskvalue</var><span class="roman">]</span></code></dt> 169<dd><p>Set a watchpoint that will break when <var>expr</var> is either read from 170or written into by the program. 171</p> 172<a name="index-info-watchpoints-_005blist_2026_005d"></a> 173</dd> 174<dt><code>info watchpoints <span class="roman">[</span><var>list</var>…<span class="roman">]</span></code></dt> 175<dd><p>This command prints a list of watchpoints, using the same format as 176<code>info break</code> (see <a href="Set-Breaks.html#Set-Breaks">Set Breaks</a>). 177</p></dd> 178</dl> 179 180<p>If you watch for a change in a numerically entered address you need to 181dereference it, as the address itself is just a constant number which will 182never change. <small>GDB</small> refuses to create a watchpoint that watches 183a never-changing value: 184</p> 185<div class="smallexample"> 186<pre class="smallexample">(gdb) watch 0x600850 187Cannot watch constant value 0x600850. 188(gdb) watch *(int *) 0x600850 189Watchpoint 1: *(int *) 6293584 190</pre></div> 191 192<p><small>GDB</small> sets a <em>hardware watchpoint</em> if possible. Hardware 193watchpoints execute very quickly, and the debugger reports a change in 194value at the exact instruction where the change occurs. If <small>GDB</small> 195cannot set a hardware watchpoint, it sets a software watchpoint, which 196executes more slowly and reports the change in value at the next 197<em>statement</em>, not the instruction, after the change occurs. 198</p> 199<a name="index-use-only-software-watchpoints"></a> 200<p>You can force <small>GDB</small> to use only software watchpoints with the 201<kbd>set can-use-hw-watchpoints 0</kbd> command. With this variable set to 202zero, <small>GDB</small> will never try to use hardware watchpoints, even if 203the underlying system supports them. (Note that hardware-assisted 204watchpoints that were set <em>before</em> setting 205<code>can-use-hw-watchpoints</code> to zero will still use the hardware 206mechanism of watching expression values.) 207</p> 208<dl compact="compact"> 209<dt><code>set can-use-hw-watchpoints</code></dt> 210<dd><a name="index-set-can_002duse_002dhw_002dwatchpoints"></a> 211<p>Set whether or not to use hardware watchpoints. 212</p> 213</dd> 214<dt><code>show can-use-hw-watchpoints</code></dt> 215<dd><a name="index-show-can_002duse_002dhw_002dwatchpoints"></a> 216<p>Show the current mode of using hardware watchpoints. 217</p></dd> 218</dl> 219 220<p>For remote targets, you can restrict the number of hardware 221watchpoints <small>GDB</small> will use, see <a href="Remote-Configuration.html#set-remote-hardware_002dbreakpoint_002dlimit">set remote hardware-breakpoint-limit</a>. 222</p> 223<p>When you issue the <code>watch</code> command, <small>GDB</small> reports 224</p> 225<div class="smallexample"> 226<pre class="smallexample">Hardware watchpoint <var>num</var>: <var>expr</var> 227</pre></div> 228 229<p>if it was able to set a hardware watchpoint. 230</p> 231<p>Currently, the <code>awatch</code> and <code>rwatch</code> commands can only set 232hardware watchpoints, because accesses to data that don’t change the 233value of the watched expression cannot be detected without examining 234every instruction as it is being executed, and <small>GDB</small> does not do 235that currently. If <small>GDB</small> finds that it is unable to set a 236hardware breakpoint with the <code>awatch</code> or <code>rwatch</code> command, it 237will print a message like this: 238</p> 239<div class="smallexample"> 240<pre class="smallexample">Expression cannot be implemented with read/access watchpoint. 241</pre></div> 242 243<p>Sometimes, <small>GDB</small> cannot set a hardware watchpoint because the 244data type of the watched expression is wider than what a hardware 245watchpoint on the target machine can handle. For example, some systems 246can only watch regions that are up to 4 bytes wide; on such systems you 247cannot set hardware watchpoints for an expression that yields a 248double-precision floating-point number (which is typically 8 bytes 249wide). As a work-around, it might be possible to break the large region 250into a series of smaller ones and watch them with separate watchpoints. 251</p> 252<p>If you set too many hardware watchpoints, <small>GDB</small> might be unable 253to insert all of them when you resume the execution of your program. 254Since the precise number of active watchpoints is unknown until such 255time as the program is about to be resumed, <small>GDB</small> might not be 256able to warn you about this when you set the watchpoints, and the 257warning will be printed only when the program is resumed: 258</p> 259<div class="smallexample"> 260<pre class="smallexample">Hardware watchpoint <var>num</var>: Could not insert watchpoint 261</pre></div> 262 263<p>If this happens, delete or disable some of the watchpoints. 264</p> 265<p>Watching complex expressions that reference many variables can also 266exhaust the resources available for hardware-assisted watchpoints. 267That’s because <small>GDB</small> needs to watch every variable in the 268expression with separately allocated resources. 269</p> 270<p>If you call a function interactively using <code>print</code> or <code>call</code>, 271any watchpoints you have set will be inactive until <small>GDB</small> reaches another 272kind of breakpoint or the call completes. 273</p> 274<p><small>GDB</small> automatically deletes watchpoints that watch local 275(automatic) variables, or expressions that involve such variables, when 276they go out of scope, that is, when the execution leaves the block in 277which these variables were defined. In particular, when the program 278being debugged terminates, <em>all</em> local variables go out of scope, 279and so only watchpoints that watch global variables remain set. If you 280rerun the program, you will need to set all such watchpoints again. One 281way of doing that would be to set a code breakpoint at the entry to the 282<code>main</code> function and when it breaks, set all the watchpoints. 283</p> 284<a name="index-watchpoints-and-threads"></a> 285<a name="index-threads-and-watchpoints"></a> 286<p>In multi-threaded programs, watchpoints will detect changes to the 287watched expression from every thread. 288</p> 289<blockquote> 290<p><em>Warning:</em> In multi-threaded programs, software watchpoints 291have only limited usefulness. If <small>GDB</small> creates a software 292watchpoint, it can only watch the value of an expression <em>in a 293single thread</em>. If you are confident that the expression can only 294change due to the current thread’s activity (and if you are also 295confident that no other thread can become current), then you can use 296software watchpoints as usual. However, <small>GDB</small> may not notice 297when a non-current thread’s activity changes the expression. (Hardware 298watchpoints, in contrast, watch an expression in all threads.) 299</p></blockquote> 300 301<p>See <a href="Remote-Configuration.html#set-remote-hardware_002dwatchpoint_002dlimit">set remote hardware-watchpoint-limit</a>. 302</p> 303<hr> 304<div class="header"> 305<p> 306Next: <a href="Set-Catchpoints.html#Set-Catchpoints" accesskey="n" rel="next">Set Catchpoints</a>, Previous: <a href="Set-Breaks.html#Set-Breaks" accesskey="p" rel="previous">Set Breaks</a>, Up: <a href="Breakpoints.html#Breakpoints" accesskey="u" rel="up">Breakpoints</a> [<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> 307</div> 308 309 310 311</body> 312</html> 313