GDB (API)
|
00001 /* Native-dependent code for the i386. 00002 00003 Copyright (C) 2001-2013 Free Software Foundation, Inc. 00004 00005 This file is part of GDB. 00006 00007 This program is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; either version 3 of the License, or 00010 (at your option) any later version. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 00019 00020 #include "defs.h" 00021 #include "i386-nat.h" 00022 #include "breakpoint.h" 00023 #include "command.h" 00024 #include "gdbcmd.h" 00025 #include "target.h" 00026 #include "gdb_assert.h" 00027 #include "inferior.h" 00028 00029 /* Support for hardware watchpoints and breakpoints using the i386 00030 debug registers. 00031 00032 This provides several functions for inserting and removing 00033 hardware-assisted breakpoints and watchpoints, testing if one or 00034 more of the watchpoints triggered and at what address, checking 00035 whether a given region can be watched, etc. 00036 00037 The functions below implement debug registers sharing by reference 00038 counts, and allow to watch regions up to 16 bytes long. */ 00039 00040 struct i386_dr_low_type i386_dr_low; 00041 00042 00043 /* Support for 8-byte wide hw watchpoints. */ 00044 #define TARGET_HAS_DR_LEN_8 (i386_dr_low.debug_register_length == 8) 00045 00046 /* DR7 Debug Control register fields. */ 00047 00048 /* How many bits to skip in DR7 to get to R/W and LEN fields. */ 00049 #define DR_CONTROL_SHIFT 16 00050 /* How many bits in DR7 per R/W and LEN field for each watchpoint. */ 00051 #define DR_CONTROL_SIZE 4 00052 00053 /* Watchpoint/breakpoint read/write fields in DR7. */ 00054 #define DR_RW_EXECUTE (0x0) /* Break on instruction execution. */ 00055 #define DR_RW_WRITE (0x1) /* Break on data writes. */ 00056 #define DR_RW_READ (0x3) /* Break on data reads or writes. */ 00057 00058 /* This is here for completeness. No platform supports this 00059 functionality yet (as of March 2001). Note that the DE flag in the 00060 CR4 register needs to be set to support this. */ 00061 #ifndef DR_RW_IORW 00062 #define DR_RW_IORW (0x2) /* Break on I/O reads or writes. */ 00063 #endif 00064 00065 /* Watchpoint/breakpoint length fields in DR7. The 2-bit left shift 00066 is so we could OR this with the read/write field defined above. */ 00067 #define DR_LEN_1 (0x0 << 2) /* 1-byte region watch or breakpoint. */ 00068 #define DR_LEN_2 (0x1 << 2) /* 2-byte region watch. */ 00069 #define DR_LEN_4 (0x3 << 2) /* 4-byte region watch. */ 00070 #define DR_LEN_8 (0x2 << 2) /* 8-byte region watch (AMD64). */ 00071 00072 /* Local and Global Enable flags in DR7. 00073 00074 When the Local Enable flag is set, the breakpoint/watchpoint is 00075 enabled only for the current task; the processor automatically 00076 clears this flag on every task switch. When the Global Enable flag 00077 is set, the breakpoint/watchpoint is enabled for all tasks; the 00078 processor never clears this flag. 00079 00080 Currently, all watchpoint are locally enabled. If you need to 00081 enable them globally, read the comment which pertains to this in 00082 i386_insert_aligned_watchpoint below. */ 00083 #define DR_LOCAL_ENABLE_SHIFT 0 /* Extra shift to the local enable bit. */ 00084 #define DR_GLOBAL_ENABLE_SHIFT 1 /* Extra shift to the global enable bit. */ 00085 #define DR_ENABLE_SIZE 2 /* Two enable bits per debug register. */ 00086 00087 /* Local and global exact breakpoint enable flags (a.k.a. slowdown 00088 flags). These are only required on i386, to allow detection of the 00089 exact instruction which caused a watchpoint to break; i486 and 00090 later processors do that automatically. We set these flags for 00091 backwards compatibility. */ 00092 #define DR_LOCAL_SLOWDOWN (0x100) 00093 #define DR_GLOBAL_SLOWDOWN (0x200) 00094 00095 /* Fields reserved by Intel. This includes the GD (General Detect 00096 Enable) flag, which causes a debug exception to be generated when a 00097 MOV instruction accesses one of the debug registers. 00098 00099 FIXME: My Intel manual says we should use 0xF800, not 0xFC00. */ 00100 #define DR_CONTROL_RESERVED (0xFC00) 00101 00102 /* Auxiliary helper macros. */ 00103 00104 /* A value that masks all fields in DR7 that are reserved by Intel. */ 00105 #define I386_DR_CONTROL_MASK (~DR_CONTROL_RESERVED) 00106 00107 /* The I'th debug register is vacant if its Local and Global Enable 00108 bits are reset in the Debug Control register. */ 00109 #define I386_DR_VACANT(state, i) \ 00110 (((state)->dr_control_mirror & (3 << (DR_ENABLE_SIZE * (i)))) == 0) 00111 00112 /* Locally enable the break/watchpoint in the I'th debug register. */ 00113 #define I386_DR_LOCAL_ENABLE(state, i) \ 00114 do { \ 00115 (state)->dr_control_mirror |= \ 00116 (1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (i))); \ 00117 } while (0) 00118 00119 /* Globally enable the break/watchpoint in the I'th debug register. */ 00120 #define I386_DR_GLOBAL_ENABLE(state, i) \ 00121 do { \ 00122 (state)->dr_control_mirror |= \ 00123 (1 << (DR_GLOBAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (i))); \ 00124 } while (0) 00125 00126 /* Disable the break/watchpoint in the I'th debug register. */ 00127 #define I386_DR_DISABLE(state, i) \ 00128 do { \ 00129 (state)->dr_control_mirror &= \ 00130 ~(3 << (DR_ENABLE_SIZE * (i))); \ 00131 } while (0) 00132 00133 /* Set in DR7 the RW and LEN fields for the I'th debug register. */ 00134 #define I386_DR_SET_RW_LEN(state, i, rwlen) \ 00135 do { \ 00136 (state)->dr_control_mirror &= \ 00137 ~(0x0f << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (i))); \ 00138 (state)->dr_control_mirror |= \ 00139 ((rwlen) << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (i))); \ 00140 } while (0) 00141 00142 /* Get from DR7 the RW and LEN fields for the I'th debug register. */ 00143 #define I386_DR_GET_RW_LEN(dr7, i) \ 00144 (((dr7) \ 00145 >> (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (i))) & 0x0f) 00146 00147 /* Mask that this I'th watchpoint has triggered. */ 00148 #define I386_DR_WATCH_MASK(i) (1 << (i)) 00149 00150 /* Did the watchpoint whose address is in the I'th register break? */ 00151 #define I386_DR_WATCH_HIT(dr6, i) ((dr6) & (1 << (i))) 00152 00153 /* A macro to loop over all debug registers. */ 00154 #define ALL_DEBUG_REGISTERS(i) for (i = 0; i < DR_NADDR; i++) 00155 00156 /* Per-process data. We don't bind this to a per-inferior registry 00157 because of targets like x86 GNU/Linux that need to keep track of 00158 processes that aren't bound to any inferior (e.g., fork children, 00159 checkpoints). */ 00160 00161 struct i386_process_info 00162 { 00163 /* Linked list. */ 00164 struct i386_process_info *next; 00165 00166 /* The process identifier. */ 00167 pid_t pid; 00168 00169 /* Copy of i386 hardware debug registers. */ 00170 struct i386_debug_reg_state state; 00171 }; 00172 00173 static struct i386_process_info *i386_process_list = NULL; 00174 00175 /* Find process data for process PID. */ 00176 00177 static struct i386_process_info * 00178 i386_find_process_pid (pid_t pid) 00179 { 00180 struct i386_process_info *proc; 00181 00182 for (proc = i386_process_list; proc; proc = proc->next) 00183 if (proc->pid == pid) 00184 return proc; 00185 00186 return NULL; 00187 } 00188 00189 /* Add process data for process PID. Returns newly allocated info 00190 object. */ 00191 00192 static struct i386_process_info * 00193 i386_add_process (pid_t pid) 00194 { 00195 struct i386_process_info *proc; 00196 00197 proc = xcalloc (1, sizeof (*proc)); 00198 proc->pid = pid; 00199 00200 proc->next = i386_process_list; 00201 i386_process_list = proc; 00202 00203 return proc; 00204 } 00205 00206 /* Get data specific info for process PID, creating it if necessary. 00207 Never returns NULL. */ 00208 00209 static struct i386_process_info * 00210 i386_process_info_get (pid_t pid) 00211 { 00212 struct i386_process_info *proc; 00213 00214 proc = i386_find_process_pid (pid); 00215 if (proc == NULL) 00216 proc = i386_add_process (pid); 00217 00218 return proc; 00219 } 00220 00221 /* Get debug registers state for process PID. */ 00222 00223 struct i386_debug_reg_state * 00224 i386_debug_reg_state (pid_t pid) 00225 { 00226 return &i386_process_info_get (pid)->state; 00227 } 00228 00229 /* See declaration in i386-nat.h. */ 00230 00231 void 00232 i386_forget_process (pid_t pid) 00233 { 00234 struct i386_process_info *proc, **proc_link; 00235 00236 proc = i386_process_list; 00237 proc_link = &i386_process_list; 00238 00239 while (proc != NULL) 00240 { 00241 if (proc->pid == pid) 00242 { 00243 *proc_link = proc->next; 00244 00245 xfree (proc); 00246 return; 00247 } 00248 00249 proc_link = &proc->next; 00250 proc = *proc_link; 00251 } 00252 } 00253 00254 /* Whether or not to print the mirrored debug registers. */ 00255 static int maint_show_dr; 00256 00257 /* Types of operations supported by i386_handle_nonaligned_watchpoint. */ 00258 typedef enum { WP_INSERT, WP_REMOVE, WP_COUNT } i386_wp_op_t; 00259 00260 /* Internal functions. */ 00261 00262 /* Return the value of a 4-bit field for DR7 suitable for watching a 00263 region of LEN bytes for accesses of type TYPE. LEN is assumed to 00264 have the value of 1, 2, or 4. */ 00265 static unsigned i386_length_and_rw_bits (int len, enum target_hw_bp_type type); 00266 00267 /* Insert a watchpoint at address ADDR, which is assumed to be aligned 00268 according to the length of the region to watch. LEN_RW_BITS is the 00269 value of the bit-field from DR7 which describes the length and 00270 access type of the region to be watched by this watchpoint. Return 00271 0 on success, -1 on failure. */ 00272 static int i386_insert_aligned_watchpoint (struct i386_debug_reg_state *state, 00273 CORE_ADDR addr, 00274 unsigned len_rw_bits); 00275 00276 /* Remove a watchpoint at address ADDR, which is assumed to be aligned 00277 according to the length of the region to watch. LEN_RW_BITS is the 00278 value of the bits from DR7 which describes the length and access 00279 type of the region watched by this watchpoint. Return 0 on 00280 success, -1 on failure. */ 00281 static int i386_remove_aligned_watchpoint (struct i386_debug_reg_state *state, 00282 CORE_ADDR addr, 00283 unsigned len_rw_bits); 00284 00285 /* Insert or remove a (possibly non-aligned) watchpoint, or count the 00286 number of debug registers required to watch a region at address 00287 ADDR whose length is LEN for accesses of type TYPE. Return 0 on 00288 successful insertion or removal, a positive number when queried 00289 about the number of registers, or -1 on failure. If WHAT is not a 00290 valid value, bombs through internal_error. */ 00291 static int i386_handle_nonaligned_watchpoint (struct i386_debug_reg_state *state, 00292 i386_wp_op_t what, 00293 CORE_ADDR addr, int len, 00294 enum target_hw_bp_type type); 00295 00296 /* Implementation. */ 00297 00298 /* Clear the reference counts and forget everything we knew about the 00299 debug registers. */ 00300 00301 void 00302 i386_cleanup_dregs (void) 00303 { 00304 /* Starting from scratch has the same effect. */ 00305 i386_forget_process (ptid_get_pid (inferior_ptid)); 00306 } 00307 00308 /* Print the values of the mirrored debug registers. This is called 00309 when maint_show_dr is non-zero. To set that up, type "maint 00310 show-debug-regs" at GDB's prompt. */ 00311 00312 static void 00313 i386_show_dr (struct i386_debug_reg_state *state, 00314 const char *func, CORE_ADDR addr, 00315 int len, enum target_hw_bp_type type) 00316 { 00317 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8; 00318 int i; 00319 00320 puts_unfiltered (func); 00321 if (addr || len) 00322 printf_unfiltered (" (addr=%lx, len=%d, type=%s)", 00323 /* This code is for ia32, so casting CORE_ADDR 00324 to unsigned long should be okay. */ 00325 (unsigned long)addr, len, 00326 type == hw_write ? "data-write" 00327 : (type == hw_read ? "data-read" 00328 : (type == hw_access ? "data-read/write" 00329 : (type == hw_execute ? "instruction-execute" 00330 /* FIXME: if/when I/O read/write 00331 watchpoints are supported, add them 00332 here. */ 00333 : "??unknown??")))); 00334 puts_unfiltered (":\n"); 00335 printf_unfiltered ("\tCONTROL (DR7): %s STATUS (DR6): %s\n", 00336 phex (state->dr_control_mirror, 8), 00337 phex (state->dr_status_mirror, 8)); 00338 ALL_DEBUG_REGISTERS(i) 00339 { 00340 printf_unfiltered ("\ 00341 \tDR%d: addr=0x%s, ref.count=%d DR%d: addr=0x%s, ref.count=%d\n", 00342 i, phex (state->dr_mirror[i], addr_size), 00343 state->dr_ref_count[i], 00344 i + 1, phex (state->dr_mirror[i + 1], addr_size), 00345 state->dr_ref_count[i+1]); 00346 i++; 00347 } 00348 } 00349 00350 /* Return the value of a 4-bit field for DR7 suitable for watching a 00351 region of LEN bytes for accesses of type TYPE. LEN is assumed to 00352 have the value of 1, 2, or 4. */ 00353 00354 static unsigned 00355 i386_length_and_rw_bits (int len, enum target_hw_bp_type type) 00356 { 00357 unsigned rw; 00358 00359 switch (type) 00360 { 00361 case hw_execute: 00362 rw = DR_RW_EXECUTE; 00363 break; 00364 case hw_write: 00365 rw = DR_RW_WRITE; 00366 break; 00367 case hw_read: 00368 internal_error (__FILE__, __LINE__, 00369 _("The i386 doesn't support " 00370 "data-read watchpoints.\n")); 00371 case hw_access: 00372 rw = DR_RW_READ; 00373 break; 00374 #if 0 00375 /* Not yet supported. */ 00376 case hw_io_access: 00377 rw = DR_RW_IORW; 00378 break; 00379 #endif 00380 default: 00381 internal_error (__FILE__, __LINE__, _("\ 00382 Invalid hardware breakpoint type %d in i386_length_and_rw_bits.\n"), 00383 (int) type); 00384 } 00385 00386 switch (len) 00387 { 00388 case 1: 00389 return (DR_LEN_1 | rw); 00390 case 2: 00391 return (DR_LEN_2 | rw); 00392 case 4: 00393 return (DR_LEN_4 | rw); 00394 case 8: 00395 if (TARGET_HAS_DR_LEN_8) 00396 return (DR_LEN_8 | rw); 00397 /* ELSE FALL THROUGH */ 00398 default: 00399 internal_error (__FILE__, __LINE__, _("\ 00400 Invalid hardware breakpoint length %d in i386_length_and_rw_bits.\n"), len); 00401 } 00402 } 00403 00404 /* Insert a watchpoint at address ADDR, which is assumed to be aligned 00405 according to the length of the region to watch. LEN_RW_BITS is the 00406 value of the bits from DR7 which describes the length and access 00407 type of the region to be watched by this watchpoint. Return 0 on 00408 success, -1 on failure. */ 00409 00410 static int 00411 i386_insert_aligned_watchpoint (struct i386_debug_reg_state *state, 00412 CORE_ADDR addr, unsigned len_rw_bits) 00413 { 00414 int i; 00415 00416 if (!i386_dr_low.set_addr || !i386_dr_low.set_control) 00417 return -1; 00418 00419 /* First, look for an occupied debug register with the same address 00420 and the same RW and LEN definitions. If we find one, we can 00421 reuse it for this watchpoint as well (and save a register). */ 00422 ALL_DEBUG_REGISTERS(i) 00423 { 00424 if (!I386_DR_VACANT (state, i) 00425 && state->dr_mirror[i] == addr 00426 && I386_DR_GET_RW_LEN (state->dr_control_mirror, i) == len_rw_bits) 00427 { 00428 state->dr_ref_count[i]++; 00429 return 0; 00430 } 00431 } 00432 00433 /* Next, look for a vacant debug register. */ 00434 ALL_DEBUG_REGISTERS(i) 00435 { 00436 if (I386_DR_VACANT (state, i)) 00437 break; 00438 } 00439 00440 /* No more debug registers! */ 00441 if (i >= DR_NADDR) 00442 return -1; 00443 00444 /* Now set up the register I to watch our region. */ 00445 00446 /* Record the info in our local mirrored array. */ 00447 state->dr_mirror[i] = addr; 00448 state->dr_ref_count[i] = 1; 00449 I386_DR_SET_RW_LEN (state, i, len_rw_bits); 00450 /* Note: we only enable the watchpoint locally, i.e. in the current 00451 task. Currently, no i386 target allows or supports global 00452 watchpoints; however, if any target would want that in the 00453 future, GDB should probably provide a command to control whether 00454 to enable watchpoints globally or locally, and the code below 00455 should use global or local enable and slow-down flags as 00456 appropriate. */ 00457 I386_DR_LOCAL_ENABLE (state, i); 00458 state->dr_control_mirror |= DR_LOCAL_SLOWDOWN; 00459 state->dr_control_mirror &= I386_DR_CONTROL_MASK; 00460 00461 return 0; 00462 } 00463 00464 /* Remove a watchpoint at address ADDR, which is assumed to be aligned 00465 according to the length of the region to watch. LEN_RW_BITS is the 00466 value of the bits from DR7 which describes the length and access 00467 type of the region watched by this watchpoint. Return 0 on 00468 success, -1 on failure. */ 00469 00470 static int 00471 i386_remove_aligned_watchpoint (struct i386_debug_reg_state *state, 00472 CORE_ADDR addr, unsigned len_rw_bits) 00473 { 00474 int i, retval = -1; 00475 00476 ALL_DEBUG_REGISTERS(i) 00477 { 00478 if (!I386_DR_VACANT (state, i) 00479 && state->dr_mirror[i] == addr 00480 && I386_DR_GET_RW_LEN (state->dr_control_mirror, i) == len_rw_bits) 00481 { 00482 if (--state->dr_ref_count[i] == 0) /* no longer in use? */ 00483 { 00484 /* Reset our mirror. */ 00485 state->dr_mirror[i] = 0; 00486 I386_DR_DISABLE (state, i); 00487 } 00488 retval = 0; 00489 } 00490 } 00491 00492 return retval; 00493 } 00494 00495 /* Insert or remove a (possibly non-aligned) watchpoint, or count the 00496 number of debug registers required to watch a region at address 00497 ADDR whose length is LEN for accesses of type TYPE. Return 0 on 00498 successful insertion or removal, a positive number when queried 00499 about the number of registers, or -1 on failure. If WHAT is not a 00500 valid value, bombs through internal_error. */ 00501 00502 static int 00503 i386_handle_nonaligned_watchpoint (struct i386_debug_reg_state *state, 00504 i386_wp_op_t what, CORE_ADDR addr, int len, 00505 enum target_hw_bp_type type) 00506 { 00507 int retval = 0; 00508 int max_wp_len = TARGET_HAS_DR_LEN_8 ? 8 : 4; 00509 00510 static int size_try_array[8][8] = 00511 { 00512 {1, 1, 1, 1, 1, 1, 1, 1}, /* Trying size one. */ 00513 {2, 1, 2, 1, 2, 1, 2, 1}, /* Trying size two. */ 00514 {2, 1, 2, 1, 2, 1, 2, 1}, /* Trying size three. */ 00515 {4, 1, 2, 1, 4, 1, 2, 1}, /* Trying size four. */ 00516 {4, 1, 2, 1, 4, 1, 2, 1}, /* Trying size five. */ 00517 {4, 1, 2, 1, 4, 1, 2, 1}, /* Trying size six. */ 00518 {4, 1, 2, 1, 4, 1, 2, 1}, /* Trying size seven. */ 00519 {8, 1, 2, 1, 4, 1, 2, 1}, /* Trying size eight. */ 00520 }; 00521 00522 while (len > 0) 00523 { 00524 int align = addr % max_wp_len; 00525 /* Four (eight on AMD64) is the maximum length a debug register 00526 can watch. */ 00527 int try = (len > max_wp_len ? (max_wp_len - 1) : len - 1); 00528 int size = size_try_array[try][align]; 00529 00530 if (what == WP_COUNT) 00531 { 00532 /* size_try_array[] is defined such that each iteration 00533 through the loop is guaranteed to produce an address and a 00534 size that can be watched with a single debug register. 00535 Thus, for counting the registers required to watch a 00536 region, we simply need to increment the count on each 00537 iteration. */ 00538 retval++; 00539 } 00540 else 00541 { 00542 unsigned len_rw = i386_length_and_rw_bits (size, type); 00543 00544 if (what == WP_INSERT) 00545 retval = i386_insert_aligned_watchpoint (state, addr, len_rw); 00546 else if (what == WP_REMOVE) 00547 retval = i386_remove_aligned_watchpoint (state, addr, len_rw); 00548 else 00549 internal_error (__FILE__, __LINE__, _("\ 00550 Invalid value %d of operation in i386_handle_nonaligned_watchpoint.\n"), 00551 (int)what); 00552 if (retval) 00553 break; 00554 } 00555 00556 addr += size; 00557 len -= size; 00558 } 00559 00560 return retval; 00561 } 00562 00563 /* Update the inferior's debug registers with the new debug registers 00564 state, in NEW_STATE, and then update our local mirror to match. */ 00565 00566 static void 00567 i386_update_inferior_debug_regs (struct i386_debug_reg_state *new_state) 00568 { 00569 struct i386_debug_reg_state *state 00570 = i386_debug_reg_state (ptid_get_pid (inferior_ptid)); 00571 int i; 00572 00573 ALL_DEBUG_REGISTERS (i) 00574 { 00575 if (I386_DR_VACANT (new_state, i) != I386_DR_VACANT (state, i)) 00576 i386_dr_low.set_addr (i, new_state->dr_mirror[i]); 00577 else 00578 gdb_assert (new_state->dr_mirror[i] == state->dr_mirror[i]); 00579 } 00580 00581 if (new_state->dr_control_mirror != state->dr_control_mirror) 00582 i386_dr_low.set_control (new_state->dr_control_mirror); 00583 00584 *state = *new_state; 00585 } 00586 00587 /* Insert a watchpoint to watch a memory region which starts at 00588 address ADDR and whose length is LEN bytes. Watch memory accesses 00589 of the type TYPE. Return 0 on success, -1 on failure. */ 00590 00591 static int 00592 i386_insert_watchpoint (CORE_ADDR addr, int len, int type, 00593 struct expression *cond) 00594 { 00595 struct i386_debug_reg_state *state 00596 = i386_debug_reg_state (ptid_get_pid (inferior_ptid)); 00597 int retval; 00598 /* Work on a local copy of the debug registers, and on success, 00599 commit the change back to the inferior. */ 00600 struct i386_debug_reg_state local_state = *state; 00601 00602 if (type == hw_read) 00603 return 1; /* unsupported */ 00604 00605 if (((len != 1 && len !=2 && len !=4) && !(TARGET_HAS_DR_LEN_8 && len == 8)) 00606 || addr % len != 0) 00607 retval = i386_handle_nonaligned_watchpoint (&local_state, 00608 WP_INSERT, addr, len, type); 00609 else 00610 { 00611 unsigned len_rw = i386_length_and_rw_bits (len, type); 00612 00613 retval = i386_insert_aligned_watchpoint (&local_state, 00614 addr, len_rw); 00615 } 00616 00617 if (retval == 0) 00618 i386_update_inferior_debug_regs (&local_state); 00619 00620 if (maint_show_dr) 00621 i386_show_dr (state, "insert_watchpoint", addr, len, type); 00622 00623 return retval; 00624 } 00625 00626 /* Remove a watchpoint that watched the memory region which starts at 00627 address ADDR, whose length is LEN bytes, and for accesses of the 00628 type TYPE. Return 0 on success, -1 on failure. */ 00629 static int 00630 i386_remove_watchpoint (CORE_ADDR addr, int len, int type, 00631 struct expression *cond) 00632 { 00633 struct i386_debug_reg_state *state 00634 = i386_debug_reg_state (ptid_get_pid (inferior_ptid)); 00635 int retval; 00636 /* Work on a local copy of the debug registers, and on success, 00637 commit the change back to the inferior. */ 00638 struct i386_debug_reg_state local_state = *state; 00639 00640 if (((len != 1 && len !=2 && len !=4) && !(TARGET_HAS_DR_LEN_8 && len == 8)) 00641 || addr % len != 0) 00642 retval = i386_handle_nonaligned_watchpoint (&local_state, 00643 WP_REMOVE, addr, len, type); 00644 else 00645 { 00646 unsigned len_rw = i386_length_and_rw_bits (len, type); 00647 00648 retval = i386_remove_aligned_watchpoint (&local_state, 00649 addr, len_rw); 00650 } 00651 00652 if (retval == 0) 00653 i386_update_inferior_debug_regs (&local_state); 00654 00655 if (maint_show_dr) 00656 i386_show_dr (state, "remove_watchpoint", addr, len, type); 00657 00658 return retval; 00659 } 00660 00661 /* Return non-zero if we can watch a memory region that starts at 00662 address ADDR and whose length is LEN bytes. */ 00663 00664 static int 00665 i386_region_ok_for_watchpoint (CORE_ADDR addr, int len) 00666 { 00667 struct i386_debug_reg_state *state 00668 = i386_debug_reg_state (ptid_get_pid (inferior_ptid)); 00669 int nregs; 00670 00671 /* Compute how many aligned watchpoints we would need to cover this 00672 region. */ 00673 nregs = i386_handle_nonaligned_watchpoint (state, 00674 WP_COUNT, addr, len, hw_write); 00675 return nregs <= DR_NADDR ? 1 : 0; 00676 } 00677 00678 /* If the inferior has some watchpoint that triggered, set the 00679 address associated with that watchpoint and return non-zero. 00680 Otherwise, return zero. */ 00681 00682 static int 00683 i386_stopped_data_address (struct target_ops *ops, CORE_ADDR *addr_p) 00684 { 00685 struct i386_debug_reg_state *state 00686 = i386_debug_reg_state (ptid_get_pid (inferior_ptid)); 00687 CORE_ADDR addr = 0; 00688 int i; 00689 int rc = 0; 00690 /* The current thread's DR_STATUS. We always need to read this to 00691 check whether some watchpoint caused the trap. */ 00692 unsigned status; 00693 /* We need DR_CONTROL as well, but only iff DR_STATUS indicates a 00694 data breakpoint trap. Only fetch it when necessary, to avoid an 00695 unnecessary extra syscall when no watchpoint triggered. */ 00696 int control_p = 0; 00697 unsigned control = 0; 00698 00699 /* In non-stop/async, threads can be running while we change the 00700 STATE (and friends). Say, we set a watchpoint, and let threads 00701 resume. Now, say you delete the watchpoint, or add/remove 00702 watchpoints such that STATE changes while threads are running. 00703 On targets that support non-stop, inserting/deleting watchpoints 00704 updates the STATE only. It does not update the real thread's 00705 debug registers; that's only done prior to resume. Instead, if 00706 threads are running when the mirror changes, a temporary and 00707 transparent stop on all threads is forced so they can get their 00708 copy of the debug registers updated on re-resume. Now, say, 00709 a thread hit a watchpoint before having been updated with the new 00710 STATE contents, and we haven't yet handled the corresponding 00711 SIGTRAP. If we trusted STATE below, we'd mistake the real 00712 trapped address (from the last time we had updated debug 00713 registers in the thread) with whatever was currently in STATE. 00714 So to fix this, STATE always represents intention, what we _want_ 00715 threads to have in debug registers. To get at the address and 00716 cause of the trap, we need to read the state the thread still has 00717 in its debug registers. 00718 00719 In sum, always get the current debug register values the current 00720 thread has, instead of trusting the global mirror. If the thread 00721 was running when we last changed watchpoints, the mirror no 00722 longer represents what was set in this thread's debug 00723 registers. */ 00724 status = i386_dr_low.get_status (); 00725 00726 ALL_DEBUG_REGISTERS(i) 00727 { 00728 if (!I386_DR_WATCH_HIT (status, i)) 00729 continue; 00730 00731 if (!control_p) 00732 { 00733 control = i386_dr_low.get_control (); 00734 control_p = 1; 00735 } 00736 00737 /* This second condition makes sure DRi is set up for a data 00738 watchpoint, not a hardware breakpoint. The reason is that 00739 GDB doesn't call the target_stopped_data_address method 00740 except for data watchpoints. In other words, I'm being 00741 paranoiac. */ 00742 if (I386_DR_GET_RW_LEN (control, i) != 0) 00743 { 00744 addr = i386_dr_low.get_addr (i); 00745 rc = 1; 00746 if (maint_show_dr) 00747 i386_show_dr (state, "watchpoint_hit", addr, -1, hw_write); 00748 } 00749 } 00750 if (maint_show_dr && addr == 0) 00751 i386_show_dr (state, "stopped_data_addr", 0, 0, hw_write); 00752 00753 if (rc) 00754 *addr_p = addr; 00755 return rc; 00756 } 00757 00758 static int 00759 i386_stopped_by_watchpoint (void) 00760 { 00761 CORE_ADDR addr = 0; 00762 return i386_stopped_data_address (¤t_target, &addr); 00763 } 00764 00765 /* Insert a hardware-assisted breakpoint at BP_TGT->placed_address. 00766 Return 0 on success, EBUSY on failure. */ 00767 static int 00768 i386_insert_hw_breakpoint (struct gdbarch *gdbarch, 00769 struct bp_target_info *bp_tgt) 00770 { 00771 struct i386_debug_reg_state *state 00772 = i386_debug_reg_state (ptid_get_pid (inferior_ptid)); 00773 unsigned len_rw = i386_length_and_rw_bits (1, hw_execute); 00774 CORE_ADDR addr = bp_tgt->placed_address; 00775 /* Work on a local copy of the debug registers, and on success, 00776 commit the change back to the inferior. */ 00777 struct i386_debug_reg_state local_state = *state; 00778 int retval = i386_insert_aligned_watchpoint (&local_state, 00779 addr, len_rw) ? EBUSY : 0; 00780 00781 if (retval == 0) 00782 i386_update_inferior_debug_regs (&local_state); 00783 00784 if (maint_show_dr) 00785 i386_show_dr (state, "insert_hwbp", addr, 1, hw_execute); 00786 00787 return retval; 00788 } 00789 00790 /* Remove a hardware-assisted breakpoint at BP_TGT->placed_address. 00791 Return 0 on success, -1 on failure. */ 00792 00793 static int 00794 i386_remove_hw_breakpoint (struct gdbarch *gdbarch, 00795 struct bp_target_info *bp_tgt) 00796 { 00797 struct i386_debug_reg_state *state 00798 = i386_debug_reg_state (ptid_get_pid (inferior_ptid)); 00799 unsigned len_rw = i386_length_and_rw_bits (1, hw_execute); 00800 CORE_ADDR addr = bp_tgt->placed_address; 00801 /* Work on a local copy of the debug registers, and on success, 00802 commit the change back to the inferior. */ 00803 struct i386_debug_reg_state local_state = *state; 00804 int retval = i386_remove_aligned_watchpoint (&local_state, 00805 addr, len_rw); 00806 00807 if (retval == 0) 00808 i386_update_inferior_debug_regs (&local_state); 00809 00810 if (maint_show_dr) 00811 i386_show_dr (state, "remove_hwbp", addr, 1, hw_execute); 00812 00813 return retval; 00814 } 00815 00816 /* Returns the number of hardware watchpoints of type TYPE that we can 00817 set. Value is positive if we can set CNT watchpoints, zero if 00818 setting watchpoints of type TYPE is not supported, and negative if 00819 CNT is more than the maximum number of watchpoints of type TYPE 00820 that we can support. TYPE is one of bp_hardware_watchpoint, 00821 bp_read_watchpoint, bp_write_watchpoint, or bp_hardware_breakpoint. 00822 CNT is the number of such watchpoints used so far (including this 00823 one). OTHERTYPE is non-zero if other types of watchpoints are 00824 currently enabled. 00825 00826 We always return 1 here because we don't have enough information 00827 about possible overlap of addresses that they want to watch. As an 00828 extreme example, consider the case where all the watchpoints watch 00829 the same address and the same region length: then we can handle a 00830 virtually unlimited number of watchpoints, due to debug register 00831 sharing implemented via reference counts in i386-nat.c. */ 00832 00833 static int 00834 i386_can_use_hw_breakpoint (int type, int cnt, int othertype) 00835 { 00836 return 1; 00837 } 00838 00839 static void 00840 add_show_debug_regs_command (void) 00841 { 00842 /* A maintenance command to enable printing the internal DRi mirror 00843 variables. */ 00844 add_setshow_boolean_cmd ("show-debug-regs", class_maintenance, 00845 &maint_show_dr, _("\ 00846 Set whether to show variables that mirror the x86 debug registers."), _("\ 00847 Show whether to show variables that mirror the x86 debug registers."), _("\ 00848 Use \"on\" to enable, \"off\" to disable.\n\ 00849 If enabled, the debug registers values are shown when GDB inserts\n\ 00850 or removes a hardware breakpoint or watchpoint, and when the inferior\n\ 00851 triggers a breakpoint or watchpoint."), 00852 NULL, 00853 NULL, 00854 &maintenance_set_cmdlist, 00855 &maintenance_show_cmdlist); 00856 } 00857 00858 /* There are only two global functions left. */ 00859 00860 void 00861 i386_use_watchpoints (struct target_ops *t) 00862 { 00863 /* After a watchpoint trap, the PC points to the instruction after the 00864 one that caused the trap. Therefore we don't need to step over it. 00865 But we do need to reset the status register to avoid another trap. */ 00866 t->to_have_continuable_watchpoint = 1; 00867 00868 t->to_can_use_hw_breakpoint = i386_can_use_hw_breakpoint; 00869 t->to_region_ok_for_hw_watchpoint = i386_region_ok_for_watchpoint; 00870 t->to_stopped_by_watchpoint = i386_stopped_by_watchpoint; 00871 t->to_stopped_data_address = i386_stopped_data_address; 00872 t->to_insert_watchpoint = i386_insert_watchpoint; 00873 t->to_remove_watchpoint = i386_remove_watchpoint; 00874 t->to_insert_hw_breakpoint = i386_insert_hw_breakpoint; 00875 t->to_remove_hw_breakpoint = i386_remove_hw_breakpoint; 00876 } 00877 00878 void 00879 i386_set_debug_register_length (int len) 00880 { 00881 /* This function should be called only once for each native target. */ 00882 gdb_assert (i386_dr_low.debug_register_length == 0); 00883 gdb_assert (len == 4 || len == 8); 00884 i386_dr_low.debug_register_length = len; 00885 add_show_debug_regs_command (); 00886 }