]>
Commit | Line | Data |
---|---|---|
7fa2737c MK |
1 | /* Native-dependent code for the i386. |
2 | ||
6aba47ca | 3 | Copyright (C) 2001, 2004, 2005, 2007 Free Software Foundation, Inc. |
52b98211 EZ |
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 2 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, write to the Free Software | |
197e01b6 EZ |
19 | Foundation, Inc., 51 Franklin Street, Fifth Floor, |
20 | Boston, MA 02110-1301, USA. */ | |
52b98211 EZ |
21 | |
22 | #include "defs.h" | |
23 | #include "breakpoint.h" | |
24 | #include "command.h" | |
25 | #include "gdbcmd.h" | |
26 | ||
7fa2737c | 27 | /* Support for hardware watchpoints and breakpoints using the i386 |
52b98211 EZ |
28 | debug registers. |
29 | ||
30 | This provides several functions for inserting and removing | |
7fa2737c MK |
31 | hardware-assisted breakpoints and watchpoints, testing if one or |
32 | more of the watchpoints triggered and at what address, checking | |
33 | whether a given region can be watched, etc. | |
34 | ||
35 | A target which wants to use these functions should define several | |
36 | macros, such as `target_insert_watchpoint' and | |
37 | `target_stopped_data_address', listed in target.h, to call the | |
38 | appropriate functions below. It should also define | |
52b98211 EZ |
39 | I386_USE_GENERIC_WATCHPOINTS in its tm.h file. |
40 | ||
7fa2737c MK |
41 | In addition, each target should provide several low-level macros |
42 | that will be called to insert watchpoints and hardware breakpoints | |
43 | into the inferior, remove them, and check their status. These | |
44 | macros are: | |
52b98211 EZ |
45 | |
46 | I386_DR_LOW_SET_CONTROL -- set the debug control (DR7) | |
47 | register to a given value | |
48 | ||
49 | I386_DR_LOW_SET_ADDR -- put an address into one debug | |
50 | register | |
51 | ||
52 | I386_DR_LOW_RESET_ADDR -- reset the address stored in | |
53 | one debug register | |
54 | ||
55 | I386_DR_LOW_GET_STATUS -- return the value of the debug | |
56 | status (DR6) register. | |
57 | ||
7fa2737c MK |
58 | The functions below implement debug registers sharing by reference |
59 | counts, and allow to watch regions up to 16 bytes long. */ | |
52b98211 EZ |
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 | |
7fa2737c | 65 | #define TARGET_HAS_DR_LEN_8 0 |
e906b9a3 JS |
66 | #endif |
67 | ||
52b98211 | 68 | /* Debug registers' indices. */ |
7fa2737c MK |
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). */ | |
52b98211 EZ |
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. */ | |
7fa2737c MK |
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. */ | |
52b98211 EZ |
84 | |
85 | /* This is here for completeness. No platform supports this | |
7fa2737c | 86 | functionality yet (as of March 2001). Note that the DE flag in the |
52b98211 EZ |
87 | CR4 register needs to be set to support this. */ |
88 | #ifndef DR_RW_IORW | |
7fa2737c | 89 | #define DR_RW_IORW (0x2) /* Break on I/O reads or writes. */ |
52b98211 EZ |
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. */ | |
7fa2737c MK |
94 | #define DR_LEN_1 (0x0 << 2) /* 1-byte region watch or breakpoint. */ |
95 | #define DR_LEN_2 (0x1 << 2) /* 2-byte region watch. */ | |
96 | #define DR_LEN_4 (0x3 << 2) /* 4-byte region watch. */ | |
97 | #define DR_LEN_8 (0x2 << 2) /* 8-byte region watch (AMD64). */ | |
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 | |
7fa2737c MK |
103 | clears this flag on every task switch. When the Global Enable flag |
104 | is set, the breakpoint/watchpoint is enabled for all tasks; the | |
105 | processor never clears this flag. | |
52b98211 EZ |
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. */ | |
7fa2737c MK |
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 /* Two enable bits per debug register. */ | |
52b98211 EZ |
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 | |
7fa2737c | 118 | backwards compatibility. */ |
52b98211 | 119 | #define DR_LOCAL_SLOWDOWN (0x100) |
7fa2737c | 120 | #define DR_GLOBAL_SLOWDOWN (0x200) |
52b98211 EZ |
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. */ | |
7fa2737c | 132 | #define I386_DR_CONTROL_MASK (~DR_CONTROL_RESERVED) |
52b98211 EZ |
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]; | |
7fa2737c | 171 | static unsigned dr_status_mirror, dr_control_mirror; |
52b98211 EZ |
172 | |
173 | /* Reference counts for each debug register. */ | |
7fa2737c | 174 | static int dr_ref_count[DR_NADDR]; |
52b98211 EZ |
175 | |
176 | /* Whether or not to print the mirrored debug registers. */ | |
7fa2737c | 177 | static int maint_show_dr; |
52b98211 EZ |
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 | |
7fa2737c MK |
185 | region of LEN bytes for accesses of type TYPE. LEN is assumed to |
186 | have the value of 1, 2, or 4. */ | |
52b98211 EZ |
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 | |
7fa2737c MK |
209 | about the number of registers, or -1 on failure. If WHAT is not a |
210 | valid value, bombs through internal_error. */ | |
52b98211 EZ |
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 | ||
7fa2737c MK |
217 | /* Clear the reference counts and forget everything we knew about the |
218 | debug registers. */ | |
219 | ||
52b98211 EZ |
220 | void |
221 | i386_cleanup_dregs (void) | |
222 | { | |
223 | int i; | |
224 | ||
225 | ALL_DEBUG_REGISTERS(i) | |
226 | { | |
227 | dr_mirror[i] = 0; | |
228 | dr_ref_count[i] = 0; | |
229 | } | |
230 | dr_control_mirror = 0; | |
231 | dr_status_mirror = 0; | |
232 | } | |
233 | ||
7fa2737c MK |
234 | /* Reset all debug registers at each new startup to avoid missing |
235 | watchpoints after restart. */ | |
236 | ||
9b4f1ba7 PM |
237 | void |
238 | child_post_startup_inferior (ptid_t ptid) | |
239 | { | |
240 | i386_cleanup_dregs (); | |
241 | } | |
7fa2737c | 242 | |
7fa2737c MK |
243 | /* Print the values of the mirrored debug registers. This is called |
244 | when maint_show_dr is non-zero. To set that up, type "maint | |
245 | show-debug-regs" at GDB's prompt. */ | |
246 | ||
52b98211 EZ |
247 | static void |
248 | i386_show_dr (const char *func, CORE_ADDR addr, | |
249 | int len, enum target_hw_bp_type type) | |
250 | { | |
251 | int i; | |
252 | ||
253 | puts_unfiltered (func); | |
254 | if (addr || len) | |
255 | printf_unfiltered (" (addr=%lx, len=%d, type=%s)", | |
256 | /* This code is for ia32, so casting CORE_ADDR | |
257 | to unsigned long should be okay. */ | |
258 | (unsigned long)addr, len, | |
259 | type == hw_write ? "data-write" | |
260 | : (type == hw_read ? "data-read" | |
261 | : (type == hw_access ? "data-read/write" | |
262 | : (type == hw_execute ? "instruction-execute" | |
263 | /* FIXME: if/when I/O read/write | |
264 | watchpoints are supported, add them | |
265 | here. */ | |
266 | : "??unknown??")))); | |
267 | puts_unfiltered (":\n"); | |
268 | printf_unfiltered ("\tCONTROL (DR7): %08x STATUS (DR6): %08x\n", | |
269 | dr_control_mirror, dr_status_mirror); | |
270 | ALL_DEBUG_REGISTERS(i) | |
271 | { | |
7fa2737c MK |
272 | printf_unfiltered ("\ |
273 | \tDR%d: addr=0x%s, ref.count=%d DR%d: addr=0x%s, ref.count=%d\n", | |
e906b9a3 JS |
274 | i, paddr(dr_mirror[i]), dr_ref_count[i], |
275 | i+1, paddr(dr_mirror[i+1]), dr_ref_count[i+1]); | |
52b98211 EZ |
276 | i++; |
277 | } | |
278 | } | |
279 | ||
280 | /* Return the value of a 4-bit field for DR7 suitable for watching a | |
7fa2737c MK |
281 | region of LEN bytes for accesses of type TYPE. LEN is assumed to |
282 | have the value of 1, 2, or 4. */ | |
283 | ||
52b98211 EZ |
284 | static unsigned |
285 | i386_length_and_rw_bits (int len, enum target_hw_bp_type type) | |
286 | { | |
287 | unsigned rw; | |
288 | ||
289 | switch (type) | |
290 | { | |
291 | case hw_execute: | |
292 | rw = DR_RW_EXECUTE; | |
293 | break; | |
294 | case hw_write: | |
295 | rw = DR_RW_WRITE; | |
296 | break; | |
7fa2737c MK |
297 | case hw_read: |
298 | /* The i386 doesn't support data-read watchpoints. */ | |
52b98211 EZ |
299 | case hw_access: |
300 | rw = DR_RW_READ; | |
301 | break; | |
302 | #if 0 | |
7fa2737c MK |
303 | /* Not yet supported. */ |
304 | case hw_io_access: | |
52b98211 EZ |
305 | rw = DR_RW_IORW; |
306 | break; | |
307 | #endif | |
308 | default: | |
e2e0b3e5 AC |
309 | internal_error (__FILE__, __LINE__, _("\ |
310 | Invalid hardware breakpoint type %d in i386_length_and_rw_bits.\n"), | |
7fa2737c | 311 | (int) type); |
52b98211 EZ |
312 | } |
313 | ||
314 | switch (len) | |
315 | { | |
52b98211 EZ |
316 | case 1: |
317 | return (DR_LEN_1 | rw); | |
e906b9a3 JS |
318 | case 2: |
319 | return (DR_LEN_2 | rw); | |
320 | case 4: | |
321 | return (DR_LEN_4 | rw); | |
322 | case 8: | |
323 | if (TARGET_HAS_DR_LEN_8) | |
324 | return (DR_LEN_8 | rw); | |
52b98211 | 325 | default: |
e2e0b3e5 AC |
326 | internal_error (__FILE__, __LINE__, _("\ |
327 | Invalid hardware breakpoint length %d in i386_length_and_rw_bits.\n"), len); | |
52b98211 EZ |
328 | } |
329 | } | |
330 | ||
331 | /* Insert a watchpoint at address ADDR, which is assumed to be aligned | |
332 | according to the length of the region to watch. LEN_RW_BITS is the | |
333 | value of the bits from DR7 which describes the length and access | |
334 | type of the region to be watched by this watchpoint. Return 0 on | |
335 | success, -1 on failure. */ | |
7fa2737c | 336 | |
52b98211 EZ |
337 | static int |
338 | i386_insert_aligned_watchpoint (CORE_ADDR addr, unsigned len_rw_bits) | |
339 | { | |
340 | int i; | |
341 | ||
342 | /* First, look for an occupied debug register with the same address | |
343 | and the same RW and LEN definitions. If we find one, we can | |
344 | reuse it for this watchpoint as well (and save a register). */ | |
345 | ALL_DEBUG_REGISTERS(i) | |
346 | { | |
347 | if (!I386_DR_VACANT (i) | |
348 | && dr_mirror[i] == addr | |
349 | && I386_DR_GET_RW_LEN (i) == len_rw_bits) | |
350 | { | |
351 | dr_ref_count[i]++; | |
352 | return 0; | |
353 | } | |
354 | } | |
355 | ||
356 | /* Next, look for a vacant debug register. */ | |
357 | ALL_DEBUG_REGISTERS(i) | |
358 | { | |
359 | if (I386_DR_VACANT (i)) | |
360 | break; | |
361 | } | |
362 | ||
363 | /* No more debug registers! */ | |
364 | if (i >= DR_NADDR) | |
365 | return -1; | |
366 | ||
367 | /* Now set up the register I to watch our region. */ | |
368 | ||
369 | /* Record the info in our local mirrored array. */ | |
370 | dr_mirror[i] = addr; | |
371 | dr_ref_count[i] = 1; | |
372 | I386_DR_SET_RW_LEN (i, len_rw_bits); | |
373 | /* Note: we only enable the watchpoint locally, i.e. in the current | |
7fa2737c | 374 | task. Currently, no i386 target allows or supports global |
52b98211 EZ |
375 | watchpoints; however, if any target would want that in the |
376 | future, GDB should probably provide a command to control whether | |
377 | to enable watchpoints globally or locally, and the code below | |
378 | should use global or local enable and slow-down flags as | |
379 | appropriate. */ | |
380 | I386_DR_LOCAL_ENABLE (i); | |
381 | dr_control_mirror |= DR_LOCAL_SLOWDOWN; | |
382 | dr_control_mirror &= I386_DR_CONTROL_MASK; | |
383 | ||
384 | /* Finally, actually pass the info to the inferior. */ | |
52b98211 | 385 | I386_DR_LOW_SET_ADDR (i, addr); |
4bcc3944 | 386 | I386_DR_LOW_SET_CONTROL (dr_control_mirror); |
52b98211 EZ |
387 | |
388 | return 0; | |
389 | } | |
390 | ||
391 | /* Remove a watchpoint at address ADDR, which is assumed to be aligned | |
392 | according to the length of the region to watch. LEN_RW_BITS is the | |
393 | value of the bits from DR7 which describes the length and access | |
394 | type of the region watched by this watchpoint. Return 0 on | |
395 | success, -1 on failure. */ | |
7fa2737c | 396 | |
52b98211 EZ |
397 | static int |
398 | i386_remove_aligned_watchpoint (CORE_ADDR addr, unsigned len_rw_bits) | |
399 | { | |
400 | int i, retval = -1; | |
401 | ||
402 | ALL_DEBUG_REGISTERS(i) | |
403 | { | |
404 | if (!I386_DR_VACANT (i) | |
405 | && dr_mirror[i] == addr | |
406 | && I386_DR_GET_RW_LEN (i) == len_rw_bits) | |
407 | { | |
408 | if (--dr_ref_count[i] == 0) /* no longer in use? */ | |
409 | { | |
410 | /* Reset our mirror. */ | |
411 | dr_mirror[i] = 0; | |
412 | I386_DR_DISABLE (i); | |
413 | /* Reset it in the inferior. */ | |
52b98211 | 414 | I386_DR_LOW_SET_CONTROL (dr_control_mirror); |
4bcc3944 | 415 | I386_DR_LOW_RESET_ADDR (i); |
52b98211 EZ |
416 | } |
417 | retval = 0; | |
418 | } | |
419 | } | |
420 | ||
421 | return retval; | |
422 | } | |
423 | ||
424 | /* Insert or remove a (possibly non-aligned) watchpoint, or count the | |
425 | number of debug registers required to watch a region at address | |
426 | ADDR whose length is LEN for accesses of type TYPE. Return 0 on | |
427 | successful insertion or removal, a positive number when queried | |
7fa2737c MK |
428 | about the number of registers, or -1 on failure. If WHAT is not a |
429 | valid value, bombs through internal_error. */ | |
430 | ||
52b98211 EZ |
431 | static int |
432 | i386_handle_nonaligned_watchpoint (i386_wp_op_t what, CORE_ADDR addr, int len, | |
433 | enum target_hw_bp_type type) | |
434 | { | |
7fa2737c | 435 | int retval = 0, status = 0; |
e906b9a3 | 436 | int max_wp_len = TARGET_HAS_DR_LEN_8 ? 8 : 4; |
52b98211 | 437 | |
e906b9a3 | 438 | static int size_try_array[8][8] = |
52b98211 | 439 | { |
7fa2737c MK |
440 | {1, 1, 1, 1, 1, 1, 1, 1}, /* Trying size one. */ |
441 | {2, 1, 2, 1, 2, 1, 2, 1}, /* Trying size two. */ | |
442 | {2, 1, 2, 1, 2, 1, 2, 1}, /* Trying size three. */ | |
443 | {4, 1, 2, 1, 4, 1, 2, 1}, /* Trying size four. */ | |
444 | {4, 1, 2, 1, 4, 1, 2, 1}, /* Trying size five. */ | |
445 | {4, 1, 2, 1, 4, 1, 2, 1}, /* Trying size six. */ | |
446 | {4, 1, 2, 1, 4, 1, 2, 1}, /* Trying size seven. */ | |
447 | {8, 1, 2, 1, 4, 1, 2, 1}, /* Trying size eight. */ | |
52b98211 EZ |
448 | }; |
449 | ||
450 | while (len > 0) | |
451 | { | |
7fa2737c | 452 | int align = addr % max_wp_len; |
f2e7c15d | 453 | /* Four (eight on AMD64) is the maximum length a debug register |
e906b9a3 | 454 | can watch. */ |
7fa2737c MK |
455 | int try = (len > max_wp_len ? (max_wp_len - 1) : len - 1); |
456 | int size = size_try_array[try][align]; | |
457 | ||
52b98211 | 458 | if (what == WP_COUNT) |
7fa2737c MK |
459 | { |
460 | /* size_try_array[] is defined such that each iteration | |
461 | through the loop is guaranteed to produce an address and a | |
462 | size that can be watched with a single debug register. | |
463 | Thus, for counting the registers required to watch a | |
464 | region, we simply need to increment the count on each | |
465 | iteration. */ | |
466 | retval++; | |
467 | } | |
52b98211 EZ |
468 | else |
469 | { | |
470 | unsigned len_rw = i386_length_and_rw_bits (size, type); | |
471 | ||
472 | if (what == WP_INSERT) | |
473 | status = i386_insert_aligned_watchpoint (addr, len_rw); | |
474 | else if (what == WP_REMOVE) | |
475 | status = i386_remove_aligned_watchpoint (addr, len_rw); | |
476 | else | |
e2e0b3e5 AC |
477 | internal_error (__FILE__, __LINE__, _("\ |
478 | Invalid value %d of operation in i386_handle_nonaligned_watchpoint.\n"), | |
52b98211 EZ |
479 | (int)what); |
480 | /* We keep the loop going even after a failure, because some | |
481 | of the other aligned watchpoints might still succeed | |
482 | (e.g. if they watch addresses that are already watched, | |
483 | in which case we just increment the reference counts of | |
484 | occupied debug registers). If we break out of the loop | |
485 | too early, we could cause those addresses watched by | |
486 | other watchpoints to be disabled when breakpoint.c reacts | |
487 | to our failure to insert this watchpoint and tries to | |
488 | remove it. */ | |
489 | if (status) | |
7fa2737c | 490 | retval = status; |
52b98211 | 491 | } |
7fa2737c | 492 | |
52b98211 EZ |
493 | addr += size; |
494 | len -= size; | |
495 | } | |
7fa2737c MK |
496 | |
497 | return retval; | |
52b98211 EZ |
498 | } |
499 | ||
500 | /* Insert a watchpoint to watch a memory region which starts at | |
501 | address ADDR and whose length is LEN bytes. Watch memory accesses | |
502 | of the type TYPE. Return 0 on success, -1 on failure. */ | |
7fa2737c | 503 | |
52b98211 EZ |
504 | int |
505 | i386_insert_watchpoint (CORE_ADDR addr, int len, int type) | |
506 | { | |
507 | int retval; | |
508 | ||
e906b9a3 JS |
509 | if (((len != 1 && len !=2 && len !=4) && !(TARGET_HAS_DR_LEN_8 && len == 8)) |
510 | || addr % len != 0) | |
52b98211 EZ |
511 | retval = i386_handle_nonaligned_watchpoint (WP_INSERT, addr, len, type); |
512 | else | |
513 | { | |
514 | unsigned len_rw = i386_length_and_rw_bits (len, type); | |
515 | ||
516 | retval = i386_insert_aligned_watchpoint (addr, len_rw); | |
517 | } | |
518 | ||
519 | if (maint_show_dr) | |
520 | i386_show_dr ("insert_watchpoint", addr, len, type); | |
521 | ||
522 | return retval; | |
523 | } | |
524 | ||
525 | /* Remove a watchpoint that watched the memory region which starts at | |
526 | address ADDR, whose length is LEN bytes, and for accesses of the | |
527 | type TYPE. Return 0 on success, -1 on failure. */ | |
528 | int | |
529 | i386_remove_watchpoint (CORE_ADDR addr, int len, int type) | |
530 | { | |
531 | int retval; | |
532 | ||
e906b9a3 JS |
533 | if (((len != 1 && len !=2 && len !=4) && !(TARGET_HAS_DR_LEN_8 && len == 8)) |
534 | || addr % len != 0) | |
52b98211 EZ |
535 | retval = i386_handle_nonaligned_watchpoint (WP_REMOVE, addr, len, type); |
536 | else | |
537 | { | |
538 | unsigned len_rw = i386_length_and_rw_bits (len, type); | |
539 | ||
540 | retval = i386_remove_aligned_watchpoint (addr, len_rw); | |
541 | } | |
542 | ||
543 | if (maint_show_dr) | |
544 | i386_show_dr ("remove_watchpoint", addr, len, type); | |
545 | ||
546 | return retval; | |
547 | } | |
548 | ||
549 | /* Return non-zero if we can watch a memory region that starts at | |
550 | address ADDR and whose length is LEN bytes. */ | |
7fa2737c | 551 | |
52b98211 EZ |
552 | int |
553 | i386_region_ok_for_watchpoint (CORE_ADDR addr, int len) | |
554 | { | |
7fa2737c MK |
555 | int nregs; |
556 | ||
52b98211 EZ |
557 | /* Compute how many aligned watchpoints we would need to cover this |
558 | region. */ | |
7fa2737c | 559 | nregs = i386_handle_nonaligned_watchpoint (WP_COUNT, addr, len, hw_write); |
52b98211 EZ |
560 | return nregs <= DR_NADDR ? 1 : 0; |
561 | } | |
562 | ||
4aa7a7f5 JJ |
563 | /* If the inferior has some watchpoint that triggered, set the |
564 | address associated with that watchpoint and return non-zero. | |
565 | Otherwise, return zero. */ | |
7fa2737c | 566 | |
4aa7a7f5 JJ |
567 | int |
568 | i386_stopped_data_address (CORE_ADDR *addr_p) | |
52b98211 | 569 | { |
7fa2737c | 570 | CORE_ADDR addr = 0; |
52b98211 | 571 | int i; |
4aa7a7f5 | 572 | int rc = 0; |
52b98211 EZ |
573 | |
574 | dr_status_mirror = I386_DR_LOW_GET_STATUS (); | |
575 | ||
576 | ALL_DEBUG_REGISTERS(i) | |
577 | { | |
578 | if (I386_DR_WATCH_HIT (i) | |
579 | /* This second condition makes sure DRi is set up for a data | |
580 | watchpoint, not a hardware breakpoint. The reason is | |
581 | that GDB doesn't call the target_stopped_data_address | |
582 | method except for data watchpoints. In other words, I'm | |
f2e7c15d | 583 | being paranoiac. */ |
52b98211 EZ |
584 | && I386_DR_GET_RW_LEN (i) != 0) |
585 | { | |
7fa2737c | 586 | addr = dr_mirror[i]; |
4aa7a7f5 | 587 | rc = 1; |
52b98211 | 588 | if (maint_show_dr) |
7fa2737c | 589 | i386_show_dr ("watchpoint_hit", addr, -1, hw_write); |
52b98211 EZ |
590 | } |
591 | } | |
7fa2737c | 592 | if (maint_show_dr && addr == 0) |
52b98211 EZ |
593 | i386_show_dr ("stopped_data_addr", 0, 0, hw_write); |
594 | ||
4aa7a7f5 JJ |
595 | if (rc) |
596 | *addr_p = addr; | |
597 | return rc; | |
598 | } | |
599 | ||
600 | int | |
601 | i386_stopped_by_watchpoint (void) | |
602 | { | |
603 | CORE_ADDR addr = 0; | |
604 | return i386_stopped_data_address (&addr); | |
52b98211 EZ |
605 | } |
606 | ||
607 | /* Return non-zero if the inferior has some break/watchpoint that | |
608 | triggered. */ | |
7fa2737c | 609 | |
52b98211 EZ |
610 | int |
611 | i386_stopped_by_hwbp (void) | |
612 | { | |
613 | int i; | |
614 | ||
615 | dr_status_mirror = I386_DR_LOW_GET_STATUS (); | |
616 | if (maint_show_dr) | |
617 | i386_show_dr ("stopped_by_hwbp", 0, 0, hw_execute); | |
618 | ||
619 | ALL_DEBUG_REGISTERS(i) | |
620 | { | |
621 | if (I386_DR_WATCH_HIT (i)) | |
622 | return 1; | |
623 | } | |
624 | ||
625 | return 0; | |
626 | } | |
627 | ||
8181d85f DJ |
628 | /* Insert a hardware-assisted breakpoint at BP_TGT->placed_address. |
629 | Return 0 on success, EBUSY on failure. */ | |
52b98211 | 630 | int |
8181d85f | 631 | i386_insert_hw_breakpoint (struct bp_target_info *bp_tgt) |
52b98211 EZ |
632 | { |
633 | unsigned len_rw = i386_length_and_rw_bits (1, hw_execute); | |
8181d85f | 634 | CORE_ADDR addr = bp_tgt->placed_address; |
52b98211 EZ |
635 | int retval = i386_insert_aligned_watchpoint (addr, len_rw) ? EBUSY : 0; |
636 | ||
637 | if (maint_show_dr) | |
638 | i386_show_dr ("insert_hwbp", addr, 1, hw_execute); | |
639 | ||
640 | return retval; | |
641 | } | |
642 | ||
8181d85f DJ |
643 | /* Remove a hardware-assisted breakpoint at BP_TGT->placed_address. |
644 | Return 0 on success, -1 on failure. */ | |
7fa2737c | 645 | |
52b98211 | 646 | int |
8181d85f | 647 | i386_remove_hw_breakpoint (struct bp_target_info *bp_tgt) |
52b98211 EZ |
648 | { |
649 | unsigned len_rw = i386_length_and_rw_bits (1, hw_execute); | |
8181d85f | 650 | CORE_ADDR addr = bp_tgt->placed_address; |
52b98211 EZ |
651 | int retval = i386_remove_aligned_watchpoint (addr, len_rw); |
652 | ||
653 | if (maint_show_dr) | |
654 | i386_show_dr ("remove_hwbp", addr, 1, hw_execute); | |
655 | ||
656 | return retval; | |
657 | } | |
658 | ||
659 | #endif /* I386_USE_GENERIC_WATCHPOINTS */ | |
52b98211 | 660 | \f |
7fa2737c MK |
661 | |
662 | /* Provide a prototype to silence -Wmissing-prototypes. */ | |
663 | void _initialize_i386_nat (void); | |
664 | ||
52b98211 EZ |
665 | void |
666 | _initialize_i386_nat (void) | |
667 | { | |
668 | #ifdef I386_USE_GENERIC_WATCHPOINTS | |
669 | /* A maintenance command to enable printing the internal DRi mirror | |
670 | variables. */ | |
b66df561 AC |
671 | deprecated_add_set_cmd ("show-debug-regs", class_maintenance, |
672 | var_boolean, (char *) &maint_show_dr, _("\ | |
52b98211 EZ |
673 | Set whether to show variables that mirror the x86 debug registers.\n\ |
674 | Use \"on\" to enable, \"off\" to disable.\n\ | |
675 | If enabled, the debug registers values are shown when GDB inserts\n\ | |
676 | or removes a hardware breakpoint or watchpoint, and when the inferior\n\ | |
b66df561 AC |
677 | triggers a breakpoint or watchpoint."), |
678 | &maintenancelist); | |
52b98211 EZ |
679 | #endif |
680 | } |