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