Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

312 lines
16KB

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