Commit | Line | Data |
---|---|---|
e6f9de87 DE |
1 | /* Debug register code for the i386. |
2 | ||
ecd75fc8 | 3 | Copyright (C) 2009-2014 Free Software Foundation, Inc. |
e6f9de87 DE |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #include "server.h" | |
21 | #include "target.h" | |
22 | #include "i386-low.h" | |
23 | ||
6e62758f GB |
24 | /* Support for hardware watchpoints and breakpoints using the i386 |
25 | debug registers. | |
26 | ||
27 | This provides several functions for inserting and removing | |
28 | hardware-assisted breakpoints and watchpoints, testing if one or | |
29 | more of the watchpoints triggered and at what address, checking | |
30 | whether a given region can be watched, etc. | |
31 | ||
32 | The functions below implement debug registers sharing by reference | |
33 | counts, and allow to watch regions up to 16 bytes long. */ | |
34 | ||
e6f9de87 DE |
35 | /* Support for 8-byte wide hw watchpoints. */ |
36 | #ifndef TARGET_HAS_DR_LEN_8 | |
37 | /* NOTE: sizeof (long) == 4 on win64. */ | |
38 | #define TARGET_HAS_DR_LEN_8 (sizeof (void *) == 8) | |
39 | #endif | |
40 | ||
e6f9de87 DE |
41 | /* DR7 Debug Control register fields. */ |
42 | ||
43 | /* How many bits to skip in DR7 to get to R/W and LEN fields. */ | |
44 | #define DR_CONTROL_SHIFT 16 | |
45 | /* How many bits in DR7 per R/W and LEN field for each watchpoint. */ | |
46 | #define DR_CONTROL_SIZE 4 | |
47 | ||
48 | /* Watchpoint/breakpoint read/write fields in DR7. */ | |
49 | #define DR_RW_EXECUTE (0x0) /* Break on instruction execution. */ | |
50 | #define DR_RW_WRITE (0x1) /* Break on data writes. */ | |
51 | #define DR_RW_READ (0x3) /* Break on data reads or writes. */ | |
52 | ||
53 | /* This is here for completeness. No platform supports this | |
54 | functionality yet (as of March 2001). Note that the DE flag in the | |
55 | CR4 register needs to be set to support this. */ | |
56 | #ifndef DR_RW_IORW | |
57 | #define DR_RW_IORW (0x2) /* Break on I/O reads or writes. */ | |
58 | #endif | |
59 | ||
60 | /* Watchpoint/breakpoint length fields in DR7. The 2-bit left shift | |
61 | is so we could OR this with the read/write field defined above. */ | |
62 | #define DR_LEN_1 (0x0 << 2) /* 1-byte region watch or breakpoint. */ | |
63 | #define DR_LEN_2 (0x1 << 2) /* 2-byte region watch. */ | |
64 | #define DR_LEN_4 (0x3 << 2) /* 4-byte region watch. */ | |
65 | #define DR_LEN_8 (0x2 << 2) /* 8-byte region watch (AMD64). */ | |
66 | ||
67 | /* Local and Global Enable flags in DR7. | |
68 | ||
69 | When the Local Enable flag is set, the breakpoint/watchpoint is | |
70 | enabled only for the current task; the processor automatically | |
71 | clears this flag on every task switch. When the Global Enable flag | |
72 | is set, the breakpoint/watchpoint is enabled for all tasks; the | |
73 | processor never clears this flag. | |
74 | ||
75 | Currently, all watchpoint are locally enabled. If you need to | |
76 | enable them globally, read the comment which pertains to this in | |
77 | i386_insert_aligned_watchpoint below. */ | |
78 | #define DR_LOCAL_ENABLE_SHIFT 0 /* Extra shift to the local enable bit. */ | |
79 | #define DR_GLOBAL_ENABLE_SHIFT 1 /* Extra shift to the global enable bit. */ | |
80 | #define DR_ENABLE_SIZE 2 /* Two enable bits per debug register. */ | |
81 | ||
82 | /* Local and global exact breakpoint enable flags (a.k.a. slowdown | |
83 | flags). These are only required on i386, to allow detection of the | |
84 | exact instruction which caused a watchpoint to break; i486 and | |
85 | later processors do that automatically. We set these flags for | |
86 | backwards compatibility. */ | |
87 | #define DR_LOCAL_SLOWDOWN (0x100) | |
88 | #define DR_GLOBAL_SLOWDOWN (0x200) | |
89 | ||
90 | /* Fields reserved by Intel. This includes the GD (General Detect | |
91 | Enable) flag, which causes a debug exception to be generated when a | |
92 | MOV instruction accesses one of the debug registers. | |
93 | ||
94 | FIXME: My Intel manual says we should use 0xF800, not 0xFC00. */ | |
95 | #define DR_CONTROL_RESERVED (0xFC00) | |
96 | ||
97 | /* Auxiliary helper macros. */ | |
98 | ||
99 | /* A value that masks all fields in DR7 that are reserved by Intel. */ | |
100 | #define I386_DR_CONTROL_MASK (~DR_CONTROL_RESERVED) | |
101 | ||
102 | /* The I'th debug register is vacant if its Local and Global Enable | |
103 | bits are reset in the Debug Control register. */ | |
104 | #define I386_DR_VACANT(state, i) \ | |
105 | (((state)->dr_control_mirror & (3 << (DR_ENABLE_SIZE * (i)))) == 0) | |
106 | ||
107 | /* Locally enable the break/watchpoint in the I'th debug register. */ | |
108 | #define I386_DR_LOCAL_ENABLE(state, i) \ | |
109 | do { \ | |
110 | (state)->dr_control_mirror |= \ | |
111 | (1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (i))); \ | |
112 | } while (0) | |
113 | ||
114 | /* Globally enable the break/watchpoint in the I'th debug register. */ | |
115 | #define I386_DR_GLOBAL_ENABLE(state, i) \ | |
116 | do { \ | |
117 | (state)->dr_control_mirror |= \ | |
118 | (1 << (DR_GLOBAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (i))); \ | |
119 | } while (0) | |
120 | ||
121 | /* Disable the break/watchpoint in the I'th debug register. */ | |
122 | #define I386_DR_DISABLE(state, i) \ | |
123 | do { \ | |
124 | (state)->dr_control_mirror &= \ | |
125 | ~(3 << (DR_ENABLE_SIZE * (i))); \ | |
126 | } while (0) | |
127 | ||
128 | /* Set in DR7 the RW and LEN fields for the I'th debug register. */ | |
fc6e2f03 | 129 | #define I386_DR_SET_RW_LEN(state, i, rwlen) \ |
e6f9de87 DE |
130 | do { \ |
131 | (state)->dr_control_mirror &= \ | |
132 | ~(0x0f << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (i))); \ | |
133 | (state)->dr_control_mirror |= \ | |
134 | ((rwlen) << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (i))); \ | |
135 | } while (0) | |
136 | ||
137 | /* Get from DR7 the RW and LEN fields for the I'th debug register. */ | |
964e4306 PA |
138 | #define I386_DR_GET_RW_LEN(dr7, i) \ |
139 | (((dr7) \ | |
e6f9de87 DE |
140 | >> (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (i))) & 0x0f) |
141 | ||
142 | /* Did the watchpoint whose address is in the I'th register break? */ | |
964e4306 | 143 | #define I386_DR_WATCH_HIT(dr6, i) ((dr6) & (1 << (i))) |
e6f9de87 DE |
144 | |
145 | /* A macro to loop over all debug registers. */ | |
146 | #define ALL_DEBUG_REGISTERS(i) for (i = 0; i < DR_NADDR; i++) | |
147 | ||
148 | /* Types of operations supported by i386_handle_nonaligned_watchpoint. */ | |
149 | typedef enum { WP_INSERT, WP_REMOVE, WP_COUNT } i386_wp_op_t; | |
fc6e2f03 | 150 | |
e6f9de87 DE |
151 | /* Implementation. */ |
152 | ||
153 | /* Clear the reference counts and forget everything we knew about the | |
154 | debug registers. */ | |
155 | ||
156 | void | |
157 | i386_low_init_dregs (struct i386_debug_reg_state *state) | |
158 | { | |
159 | int i; | |
160 | ||
161 | ALL_DEBUG_REGISTERS (i) | |
162 | { | |
163 | state->dr_mirror[i] = 0; | |
164 | state->dr_ref_count[i] = 0; | |
165 | } | |
166 | state->dr_control_mirror = 0; | |
167 | state->dr_status_mirror = 0; | |
168 | } | |
169 | ||
6e62758f | 170 | /* Print the values of the mirrored debug registers. */ |
e6f9de87 DE |
171 | |
172 | static void | |
173 | i386_show_dr (struct i386_debug_reg_state *state, | |
174 | const char *func, CORE_ADDR addr, | |
175 | int len, enum target_hw_bp_type type) | |
176 | { | |
177 | int i; | |
178 | ||
179 | fprintf (stderr, "%s", func); | |
180 | if (addr || len) | |
181 | fprintf (stderr, " (addr=%lx, len=%d, type=%s)", | |
182 | (unsigned long) addr, len, | |
183 | type == hw_write ? "data-write" | |
184 | : (type == hw_read ? "data-read" | |
185 | : (type == hw_access ? "data-read/write" | |
186 | : (type == hw_execute ? "instruction-execute" | |
187 | /* FIXME: if/when I/O read/write | |
188 | watchpoints are supported, add them | |
189 | here. */ | |
190 | : "??unknown??")))); | |
191 | fprintf (stderr, ":\n"); | |
192 | fprintf (stderr, "\tCONTROL (DR7): %08x STATUS (DR6): %08x\n", | |
193 | state->dr_control_mirror, state->dr_status_mirror); | |
194 | ALL_DEBUG_REGISTERS (i) | |
195 | { | |
196 | fprintf (stderr, "\ | |
197 | \tDR%d: addr=0x%s, ref.count=%d DR%d: addr=0x%s, ref.count=%d\n", | |
198 | i, paddress (state->dr_mirror[i]), | |
199 | state->dr_ref_count[i], | |
200 | i + 1, paddress (state->dr_mirror[i + 1]), | |
201 | state->dr_ref_count[i + 1]); | |
202 | i++; | |
203 | } | |
204 | } | |
205 | ||
206 | /* Return the value of a 4-bit field for DR7 suitable for watching a | |
207 | region of LEN bytes for accesses of type TYPE. LEN is assumed to | |
208 | have the value of 1, 2, or 4. */ | |
209 | ||
210 | static unsigned | |
211 | i386_length_and_rw_bits (int len, enum target_hw_bp_type type) | |
212 | { | |
213 | unsigned rw; | |
214 | ||
215 | switch (type) | |
216 | { | |
217 | case hw_execute: | |
218 | rw = DR_RW_EXECUTE; | |
219 | break; | |
220 | case hw_write: | |
221 | rw = DR_RW_WRITE; | |
222 | break; | |
223 | case hw_read: | |
85d721b8 | 224 | fatal ("The i386 doesn't support data-read watchpoints.\n"); |
e6f9de87 DE |
225 | case hw_access: |
226 | rw = DR_RW_READ; | |
227 | break; | |
228 | #if 0 | |
229 | /* Not yet supported. */ | |
230 | case hw_io_access: | |
231 | rw = DR_RW_IORW; | |
232 | break; | |
233 | #endif | |
234 | default: | |
235 | error ("\ | |
236 | Invalid hardware breakpoint type %d in i386_length_and_rw_bits.\n", | |
237 | (int) type); | |
238 | } | |
239 | ||
240 | switch (len) | |
241 | { | |
242 | case 1: | |
243 | return (DR_LEN_1 | rw); | |
244 | case 2: | |
245 | return (DR_LEN_2 | rw); | |
246 | case 4: | |
247 | return (DR_LEN_4 | rw); | |
248 | case 8: | |
249 | if (TARGET_HAS_DR_LEN_8) | |
250 | return (DR_LEN_8 | rw); | |
96f7a20f | 251 | /* ELSE FALL THROUGH */ |
e6f9de87 DE |
252 | default: |
253 | error ("\ | |
254 | Invalid hardware breakpoint length %d in i386_length_and_rw_bits.\n", len); | |
255 | } | |
256 | } | |
257 | ||
258 | /* Insert a watchpoint at address ADDR, which is assumed to be aligned | |
259 | according to the length of the region to watch. LEN_RW_BITS is the | |
260 | value of the bits from DR7 which describes the length and access | |
261 | type of the region to be watched by this watchpoint. Return 0 on | |
262 | success, -1 on failure. */ | |
263 | ||
264 | static int | |
265 | i386_insert_aligned_watchpoint (struct i386_debug_reg_state *state, | |
266 | CORE_ADDR addr, unsigned len_rw_bits) | |
267 | { | |
268 | int i; | |
269 | ||
270 | /* First, look for an occupied debug register with the same address | |
271 | and the same RW and LEN definitions. If we find one, we can | |
272 | reuse it for this watchpoint as well (and save a register). */ | |
273 | ALL_DEBUG_REGISTERS (i) | |
274 | { | |
275 | if (!I386_DR_VACANT (state, i) | |
276 | && state->dr_mirror[i] == addr | |
964e4306 | 277 | && I386_DR_GET_RW_LEN (state->dr_control_mirror, i) == len_rw_bits) |
e6f9de87 DE |
278 | { |
279 | state->dr_ref_count[i]++; | |
280 | return 0; | |
281 | } | |
282 | } | |
283 | ||
284 | /* Next, look for a vacant debug register. */ | |
285 | ALL_DEBUG_REGISTERS (i) | |
286 | { | |
287 | if (I386_DR_VACANT (state, i)) | |
288 | break; | |
289 | } | |
290 | ||
291 | /* No more debug registers! */ | |
292 | if (i >= DR_NADDR) | |
293 | return -1; | |
294 | ||
295 | /* Now set up the register I to watch our region. */ | |
296 | ||
297 | /* Record the info in our local mirrored array. */ | |
298 | state->dr_mirror[i] = addr; | |
299 | state->dr_ref_count[i] = 1; | |
300 | I386_DR_SET_RW_LEN (state, i, len_rw_bits); | |
301 | /* Note: we only enable the watchpoint locally, i.e. in the current | |
302 | task. Currently, no i386 target allows or supports global | |
303 | watchpoints; however, if any target would want that in the | |
304 | future, GDB should probably provide a command to control whether | |
305 | to enable watchpoints globally or locally, and the code below | |
306 | should use global or local enable and slow-down flags as | |
307 | appropriate. */ | |
308 | I386_DR_LOCAL_ENABLE (state, i); | |
309 | state->dr_control_mirror |= DR_LOCAL_SLOWDOWN; | |
310 | state->dr_control_mirror &= I386_DR_CONTROL_MASK; | |
311 | ||
e6f9de87 DE |
312 | return 0; |
313 | } | |
314 | ||
315 | /* Remove a watchpoint at address ADDR, which is assumed to be aligned | |
316 | according to the length of the region to watch. LEN_RW_BITS is the | |
317 | value of the bits from DR7 which describes the length and access | |
318 | type of the region watched by this watchpoint. Return 0 on | |
319 | success, -1 on failure. */ | |
320 | ||
321 | static int | |
322 | i386_remove_aligned_watchpoint (struct i386_debug_reg_state *state, | |
323 | CORE_ADDR addr, unsigned len_rw_bits) | |
324 | { | |
325 | int i, retval = -1; | |
326 | ||
327 | ALL_DEBUG_REGISTERS (i) | |
328 | { | |
329 | if (!I386_DR_VACANT (state, i) | |
330 | && state->dr_mirror[i] == addr | |
964e4306 | 331 | && I386_DR_GET_RW_LEN (state->dr_control_mirror, i) == len_rw_bits) |
e6f9de87 DE |
332 | { |
333 | if (--state->dr_ref_count[i] == 0) /* No longer in use? */ | |
334 | { | |
335 | /* Reset our mirror. */ | |
336 | state->dr_mirror[i] = 0; | |
337 | I386_DR_DISABLE (state, i); | |
e6f9de87 DE |
338 | } |
339 | retval = 0; | |
340 | } | |
341 | } | |
342 | ||
343 | return retval; | |
344 | } | |
345 | ||
346 | /* Insert or remove a (possibly non-aligned) watchpoint, or count the | |
347 | number of debug registers required to watch a region at address | |
348 | ADDR whose length is LEN for accesses of type TYPE. Return 0 on | |
349 | successful insertion or removal, a positive number when queried | |
350 | about the number of registers, or -1 on failure. If WHAT is not a | |
351 | valid value, bombs through internal_error. */ | |
352 | ||
353 | static int | |
354 | i386_handle_nonaligned_watchpoint (struct i386_debug_reg_state *state, | |
355 | i386_wp_op_t what, CORE_ADDR addr, int len, | |
356 | enum target_hw_bp_type type) | |
357 | { | |
1ced966e | 358 | int retval = 0; |
e6f9de87 DE |
359 | int max_wp_len = TARGET_HAS_DR_LEN_8 ? 8 : 4; |
360 | ||
361 | static const int size_try_array[8][8] = | |
362 | { | |
363 | {1, 1, 1, 1, 1, 1, 1, 1}, /* Trying size one. */ | |
364 | {2, 1, 2, 1, 2, 1, 2, 1}, /* Trying size two. */ | |
365 | {2, 1, 2, 1, 2, 1, 2, 1}, /* Trying size three. */ | |
366 | {4, 1, 2, 1, 4, 1, 2, 1}, /* Trying size four. */ | |
367 | {4, 1, 2, 1, 4, 1, 2, 1}, /* Trying size five. */ | |
368 | {4, 1, 2, 1, 4, 1, 2, 1}, /* Trying size six. */ | |
369 | {4, 1, 2, 1, 4, 1, 2, 1}, /* Trying size seven. */ | |
370 | {8, 1, 2, 1, 4, 1, 2, 1}, /* Trying size eight. */ | |
371 | }; | |
372 | ||
373 | while (len > 0) | |
374 | { | |
375 | int align = addr % max_wp_len; | |
376 | /* Four (eight on AMD64) is the maximum length a debug register | |
377 | can watch. */ | |
378 | int try = (len > max_wp_len ? (max_wp_len - 1) : len - 1); | |
379 | int size = size_try_array[try][align]; | |
380 | ||
381 | if (what == WP_COUNT) | |
382 | { | |
383 | /* size_try_array[] is defined such that each iteration | |
384 | through the loop is guaranteed to produce an address and a | |
385 | size that can be watched with a single debug register. | |
386 | Thus, for counting the registers required to watch a | |
387 | region, we simply need to increment the count on each | |
388 | iteration. */ | |
389 | retval++; | |
390 | } | |
391 | else | |
392 | { | |
393 | unsigned len_rw = i386_length_and_rw_bits (size, type); | |
394 | ||
395 | if (what == WP_INSERT) | |
1ced966e | 396 | retval = i386_insert_aligned_watchpoint (state, addr, len_rw); |
e6f9de87 | 397 | else if (what == WP_REMOVE) |
1ced966e | 398 | retval = i386_remove_aligned_watchpoint (state, addr, len_rw); |
e6f9de87 DE |
399 | else |
400 | fatal ("\ | |
401 | Invalid value %d of operation in i386_handle_nonaligned_watchpoint.\n", | |
402 | (int) what); | |
403 | ||
1ced966e PA |
404 | if (retval) |
405 | break; | |
e6f9de87 DE |
406 | } |
407 | ||
408 | addr += size; | |
409 | len -= size; | |
410 | } | |
411 | ||
412 | return retval; | |
413 | } | |
414 | ||
1ced966e PA |
415 | /* Update the inferior debug registers state, in INF_STATE, with the |
416 | new debug registers state, in NEW_STATE. */ | |
417 | ||
418 | static void | |
419 | i386_update_inferior_debug_regs (struct i386_debug_reg_state *inf_state, | |
420 | struct i386_debug_reg_state *new_state) | |
421 | { | |
422 | int i; | |
423 | ||
424 | ALL_DEBUG_REGISTERS (i) | |
425 | { | |
426 | if (I386_DR_VACANT (new_state, i) != I386_DR_VACANT (inf_state, i)) | |
427 | i386_dr_low_set_addr (new_state, i); | |
428 | else | |
429 | gdb_assert (new_state->dr_mirror[i] == inf_state->dr_mirror[i]); | |
430 | } | |
431 | ||
432 | if (new_state->dr_control_mirror != inf_state->dr_control_mirror) | |
433 | i386_dr_low_set_control (new_state); | |
434 | ||
435 | *inf_state = *new_state; | |
436 | } | |
437 | ||
e6f9de87 DE |
438 | /* Insert a watchpoint to watch a memory region which starts at |
439 | address ADDR and whose length is LEN bytes. Watch memory accesses | |
6e62758f | 440 | of the type TYPE. Return 0 on success, -1 on failure. */ |
e6f9de87 DE |
441 | |
442 | int | |
443 | i386_low_insert_watchpoint (struct i386_debug_reg_state *state, | |
a4165e94 PA |
444 | enum target_hw_bp_type type, |
445 | CORE_ADDR addr, int len) | |
e6f9de87 DE |
446 | { |
447 | int retval; | |
1ced966e PA |
448 | /* Work on a local copy of the debug registers, and on success, |
449 | commit the change back to the inferior. */ | |
450 | struct i386_debug_reg_state local_state = *state; | |
e6f9de87 | 451 | |
85d721b8 PA |
452 | if (type == hw_read) |
453 | return 1; /* unsupported */ | |
454 | ||
e6f9de87 DE |
455 | if (((len != 1 && len != 2 && len != 4) |
456 | && !(TARGET_HAS_DR_LEN_8 && len == 8)) | |
457 | || addr % len != 0) | |
458 | { | |
fc6e2f03 GB |
459 | retval = i386_handle_nonaligned_watchpoint (&local_state, |
460 | WP_INSERT, | |
e6f9de87 DE |
461 | addr, len, type); |
462 | } | |
463 | else | |
464 | { | |
465 | unsigned len_rw = i386_length_and_rw_bits (len, type); | |
466 | ||
fc6e2f03 GB |
467 | retval = i386_insert_aligned_watchpoint (&local_state, |
468 | addr, len_rw); | |
e6f9de87 DE |
469 | } |
470 | ||
1ced966e PA |
471 | if (retval == 0) |
472 | i386_update_inferior_debug_regs (state, &local_state); | |
473 | ||
e6f9de87 DE |
474 | if (debug_hw_points) |
475 | i386_show_dr (state, "insert_watchpoint", addr, len, type); | |
476 | ||
477 | return retval; | |
478 | } | |
479 | ||
480 | /* Remove a watchpoint that watched the memory region which starts at | |
481 | address ADDR, whose length is LEN bytes, and for accesses of the | |
a4165e94 | 482 | type TYPE. Return 0 on success, -1 on failure. */ |
e6f9de87 DE |
483 | |
484 | int | |
485 | i386_low_remove_watchpoint (struct i386_debug_reg_state *state, | |
a4165e94 PA |
486 | enum target_hw_bp_type type, |
487 | CORE_ADDR addr, int len) | |
e6f9de87 DE |
488 | { |
489 | int retval; | |
1ced966e PA |
490 | /* Work on a local copy of the debug registers, and on success, |
491 | commit the change back to the inferior. */ | |
492 | struct i386_debug_reg_state local_state = *state; | |
e6f9de87 DE |
493 | |
494 | if (((len != 1 && len != 2 && len != 4) | |
495 | && !(TARGET_HAS_DR_LEN_8 && len == 8)) | |
496 | || addr % len != 0) | |
497 | { | |
fc6e2f03 GB |
498 | retval = i386_handle_nonaligned_watchpoint (&local_state, |
499 | WP_REMOVE, | |
e6f9de87 DE |
500 | addr, len, type); |
501 | } | |
502 | else | |
503 | { | |
504 | unsigned len_rw = i386_length_and_rw_bits (len, type); | |
505 | ||
fc6e2f03 GB |
506 | retval = i386_remove_aligned_watchpoint (&local_state, |
507 | addr, len_rw); | |
e6f9de87 DE |
508 | } |
509 | ||
1ced966e PA |
510 | if (retval == 0) |
511 | i386_update_inferior_debug_regs (state, &local_state); | |
512 | ||
e6f9de87 DE |
513 | if (debug_hw_points) |
514 | i386_show_dr (state, "remove_watchpoint", addr, len, type); | |
515 | ||
516 | return retval; | |
517 | } | |
518 | ||
519 | /* Return non-zero if we can watch a memory region that starts at | |
520 | address ADDR and whose length is LEN bytes. */ | |
521 | ||
522 | int | |
523 | i386_low_region_ok_for_watchpoint (struct i386_debug_reg_state *state, | |
524 | CORE_ADDR addr, int len) | |
525 | { | |
526 | int nregs; | |
527 | ||
528 | /* Compute how many aligned watchpoints we would need to cover this | |
529 | region. */ | |
530 | nregs = i386_handle_nonaligned_watchpoint (state, WP_COUNT, | |
531 | addr, len, hw_write); | |
532 | return nregs <= DR_NADDR ? 1 : 0; | |
533 | } | |
534 | ||
535 | /* If the inferior has some break/watchpoint that triggered, set the | |
6e62758f GB |
536 | address associated with that break/watchpoint and return non-zero. |
537 | Otherwise, return zero. */ | |
e6f9de87 DE |
538 | |
539 | int | |
540 | i386_low_stopped_data_address (struct i386_debug_reg_state *state, | |
541 | CORE_ADDR *addr_p) | |
542 | { | |
543 | CORE_ADDR addr = 0; | |
544 | int i; | |
545 | int rc = 0; | |
6210a125 PA |
546 | /* The current thread's DR_STATUS. We always need to read this to |
547 | check whether some watchpoint caused the trap. */ | |
964e4306 | 548 | unsigned status; |
6210a125 PA |
549 | /* We need DR_CONTROL as well, but only iff DR_STATUS indicates a |
550 | data breakpoint trap. Only fetch it when necessary, to avoid an | |
551 | unnecessary extra syscall when no watchpoint triggered. */ | |
552 | int control_p = 0; | |
d54d1edf | 553 | unsigned control = 0; |
e6f9de87 | 554 | |
6210a125 PA |
555 | /* In non-stop/async, threads can be running while we change the |
556 | global dr_mirror (and friends). Say, we set a watchpoint, and | |
557 | let threads resume. Now, say you delete the watchpoint, or | |
558 | add/remove watchpoints such that dr_mirror changes while threads | |
559 | are running. On targets that support non-stop, | |
560 | inserting/deleting watchpoints updates the global dr_mirror only. | |
561 | It does not update the real thread's debug registers; that's only | |
562 | done prior to resume. Instead, if threads are running when the | |
563 | mirror changes, a temporary and transparent stop on all threads | |
564 | is forced so they can get their copy of the debug registers | |
565 | updated on re-resume. Now, say, a thread hit a watchpoint before | |
566 | having been updated with the new dr_mirror contents, and we | |
567 | haven't yet handled the corresponding SIGTRAP. If we trusted | |
568 | dr_mirror below, we'd mistake the real trapped address (from the | |
569 | last time we had updated debug registers in the thread) with | |
570 | whatever was currently in dr_mirror. So to fix this, dr_mirror | |
571 | always represents intention, what we _want_ threads to have in | |
572 | debug registers. To get at the address and cause of the trap, we | |
573 | need to read the state the thread still has in its debug | |
574 | registers. | |
575 | ||
576 | In sum, always get the current debug register values the current | |
577 | thread has, instead of trusting the global mirror. If the thread | |
578 | was running when we last changed watchpoints, the mirror no | |
579 | longer represents what was set in this thread's debug | |
580 | registers. */ | |
964e4306 | 581 | status = i386_dr_low_get_status (); |
e6f9de87 DE |
582 | |
583 | ALL_DEBUG_REGISTERS (i) | |
584 | { | |
6210a125 PA |
585 | if (!I386_DR_WATCH_HIT (status, i)) |
586 | continue; | |
587 | ||
588 | if (!control_p) | |
589 | { | |
590 | control = i386_dr_low_get_control (); | |
591 | control_p = 1; | |
592 | } | |
593 | ||
594 | /* This second condition makes sure DRi is set up for a data | |
595 | watchpoint, not a hardware breakpoint. The reason is that | |
596 | GDB doesn't call the target_stopped_data_address method | |
597 | except for data watchpoints. In other words, I'm being | |
598 | paranoiac. */ | |
599 | if (I386_DR_GET_RW_LEN (control, i) != 0) | |
e6f9de87 | 600 | { |
964e4306 | 601 | addr = i386_dr_low_get_addr (i); |
e6f9de87 DE |
602 | rc = 1; |
603 | if (debug_hw_points) | |
604 | i386_show_dr (state, "watchpoint_hit", addr, -1, hw_write); | |
605 | } | |
606 | } | |
607 | ||
608 | if (debug_hw_points && addr == 0) | |
609 | i386_show_dr (state, "stopped_data_addr", 0, 0, hw_write); | |
610 | ||
611 | if (rc) | |
612 | *addr_p = addr; | |
613 | return rc; | |
614 | } | |
615 | ||
6e62758f GB |
616 | /* Return non-zero if the inferior has some watchpoint that triggered. |
617 | Otherwise return zero. */ | |
e6f9de87 DE |
618 | |
619 | int | |
620 | i386_low_stopped_by_watchpoint (struct i386_debug_reg_state *state) | |
621 | { | |
622 | CORE_ADDR addr = 0; | |
623 | return i386_low_stopped_data_address (state, &addr); | |
624 | } |