| OLD | NEW |
| 1 /* Native-dependent code for the i386. | 1 /* Native-dependent code for the i386. |
| 2 | 2 |
| 3 Copyright (C) 2001, 2004-2005, 2007-2012 Free Software Foundation, | 3 Copyright (C) 2001, 2004-2005, 2007-2012 Free Software Foundation, |
| 4 Inc. | 4 Inc. |
| 5 | 5 |
| 6 This file is part of GDB. | 6 This file is part of GDB. |
| 7 | 7 |
| 8 This program is free software; you can redistribute it and/or modify | 8 This program is free software; you can redistribute it and/or modify |
| 9 it under the terms of the GNU General Public License as published by | 9 it under the terms of the GNU General Public License as published by |
| 10 the Free Software Foundation; either version 3 of the License, or | 10 the Free Software Foundation; either version 3 of the License, or |
| 11 (at your option) any later version. | 11 (at your option) any later version. |
| 12 | 12 |
| 13 This program is distributed in the hope that it will be useful, | 13 This program is distributed in the hope that it will be useful, |
| 14 but WITHOUT ANY WARRANTY; without even the implied warranty of | 14 but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 GNU General Public License for more details. | 16 GNU General Public License for more details. |
| 17 | 17 |
| 18 You should have received a copy of the GNU General Public License | 18 You should have received a copy of the GNU General Public License |
| 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */ | 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 20 | 20 |
| 21 #include "i386-nat.h" | 21 #include "i386-nat.h" |
| 22 #include "defs.h" | 22 #include "defs.h" |
| 23 #include "breakpoint.h" | 23 #include "breakpoint.h" |
| 24 #include "command.h" | 24 #include "command.h" |
| 25 #include "gdbcmd.h" | 25 #include "gdbcmd.h" |
| 26 #include "target.h" | 26 #include "target.h" |
| 27 #include "gdb_assert.h" | 27 #include "gdb_assert.h" |
| 28 #include "inferior.h" |
| 28 | 29 |
| 29 /* Support for hardware watchpoints and breakpoints using the i386 | 30 /* Support for hardware watchpoints and breakpoints using the i386 |
| 30 debug registers. | 31 debug registers. |
| 31 | 32 |
| 32 This provides several functions for inserting and removing | 33 This provides several functions for inserting and removing |
| 33 hardware-assisted breakpoints and watchpoints, testing if one or | 34 hardware-assisted breakpoints and watchpoints, testing if one or |
| 34 more of the watchpoints triggered and at what address, checking | 35 more of the watchpoints triggered and at what address, checking |
| 35 whether a given region can be watched, etc. | 36 whether a given region can be watched, etc. |
| 36 | 37 |
| 37 The functions below implement debug registers sharing by reference | 38 The functions below implement debug registers sharing by reference |
| 38 counts, and allow to watch regions up to 16 bytes long. */ | 39 counts, and allow to watch regions up to 16 bytes long. */ |
| 39 | 40 |
| 40 struct i386_dr_low_type i386_dr_low; | 41 struct i386_dr_low_type i386_dr_low; |
| 41 | 42 |
| 42 | 43 |
| 43 /* Support for 8-byte wide hw watchpoints. */ | 44 /* Support for 8-byte wide hw watchpoints. */ |
| 44 #define TARGET_HAS_DR_LEN_8 (i386_dr_low.debug_register_length == 8) | 45 #define TARGET_HAS_DR_LEN_8 (i386_dr_low.debug_register_length == 8) |
| 45 | 46 |
| 46 /* Debug registers' indices. */ | |
| 47 #define DR_NADDR 4 /* The number of debug address registers. */ | |
| 48 #define DR_STATUS 6 /* Index of debug status register (DR6). */ | |
| 49 #define DR_CONTROL 7 /* Index of debug control register (DR7). */ | |
| 50 | |
| 51 /* DR7 Debug Control register fields. */ | 47 /* DR7 Debug Control register fields. */ |
| 52 | 48 |
| 53 /* How many bits to skip in DR7 to get to R/W and LEN fields. */ | 49 /* How many bits to skip in DR7 to get to R/W and LEN fields. */ |
| 54 #define DR_CONTROL_SHIFT 16 | 50 #define DR_CONTROL_SHIFT 16 |
| 55 /* How many bits in DR7 per R/W and LEN field for each watchpoint. */ | 51 /* How many bits in DR7 per R/W and LEN field for each watchpoint. */ |
| 56 #define DR_CONTROL_SIZE 4 | 52 #define DR_CONTROL_SIZE 4 |
| 57 | 53 |
| 58 /* Watchpoint/breakpoint read/write fields in DR7. */ | 54 /* Watchpoint/breakpoint read/write fields in DR7. */ |
| 59 #define DR_RW_EXECUTE (0x0) /* Break on instruction execution. */ | 55 #define DR_RW_EXECUTE (0x0) /* Break on instruction execution. */ |
| 60 #define DR_RW_WRITE (0x1) /* Break on data writes. */ | 56 #define DR_RW_WRITE (0x1) /* Break on data writes. */ |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 | 147 |
| 152 /* Mask that this I'th watchpoint has triggered. */ | 148 /* Mask that this I'th watchpoint has triggered. */ |
| 153 #define I386_DR_WATCH_MASK(i) (1 << (i)) | 149 #define I386_DR_WATCH_MASK(i) (1 << (i)) |
| 154 | 150 |
| 155 /* Did the watchpoint whose address is in the I'th register break? */ | 151 /* Did the watchpoint whose address is in the I'th register break? */ |
| 156 #define I386_DR_WATCH_HIT(dr6, i) ((dr6) & (1 << (i))) | 152 #define I386_DR_WATCH_HIT(dr6, i) ((dr6) & (1 << (i))) |
| 157 | 153 |
| 158 /* A macro to loop over all debug registers. */ | 154 /* A macro to loop over all debug registers. */ |
| 159 #define ALL_DEBUG_REGISTERS(i) for (i = 0; i < DR_NADDR; i++) | 155 #define ALL_DEBUG_REGISTERS(i) for (i = 0; i < DR_NADDR; i++) |
| 160 | 156 |
| 161 | |
| 162 /* Global state needed to track h/w watchpoints. */ | |
| 163 | |
| 164 struct i386_debug_reg_state | |
| 165 { | |
| 166 /* Mirror the inferior's DRi registers. We keep the status and | |
| 167 control registers separated because they don't hold addresses. | |
| 168 Note that since we can change these mirrors while threads are | |
| 169 running, we never trust them to explain a cause of a trap. | |
| 170 For that, we need to peek directly in the inferior registers. */ | |
| 171 CORE_ADDR dr_mirror[DR_NADDR]; | |
| 172 unsigned dr_status_mirror, dr_control_mirror; | |
| 173 | |
| 174 /* Reference counts for each debug register. */ | |
| 175 int dr_ref_count[DR_NADDR]; | |
| 176 }; | |
| 177 | |
| 178 /* Clear the reference counts and forget everything we knew about the | 157 /* Clear the reference counts and forget everything we knew about the |
| 179 debug registers. */ | 158 debug registers. */ |
| 180 | 159 |
| 181 static void | 160 static void |
| 182 i386_init_dregs (struct i386_debug_reg_state *state) | 161 i386_init_dregs (struct i386_debug_reg_state *state) |
| 183 { | 162 { |
| 184 int i; | 163 int i; |
| 185 | 164 |
| 186 ALL_DEBUG_REGISTERS (i) | 165 ALL_DEBUG_REGISTERS (i) |
| 187 { | 166 { |
| 188 state->dr_mirror[i] = 0; | 167 state->dr_mirror[i] = 0; |
| 189 state->dr_ref_count[i] = 0; | 168 state->dr_ref_count[i] = 0; |
| 190 } | 169 } |
| 191 state->dr_control_mirror = 0; | 170 state->dr_control_mirror = 0; |
| 192 state->dr_status_mirror = 0; | 171 state->dr_status_mirror = 0; |
| 193 } | 172 } |
| 194 | 173 |
| 195 static struct i386_debug_reg_state dr_mirror; | 174 /* Per-inferior data key. */ |
| 175 static const struct inferior_data *i386_inferior_data; |
| 176 |
| 177 /* Per-inferior data. */ |
| 178 struct i386_inferior_data |
| 179 { |
| 180 /* Copy of i386 hardware debug registers for performance reasons. */ |
| 181 struct i386_debug_reg_state state; |
| 182 }; |
| 183 |
| 184 /* Per-inferior hook for register_inferior_data_with_cleanup. */ |
| 185 |
| 186 static void |
| 187 i386_inferior_data_cleanup (struct inferior *inf, void *arg) |
| 188 { |
| 189 struct i386_inferior_data *inf_data = arg; |
| 190 |
| 191 xfree (inf_data); |
| 192 } |
| 193 |
| 194 /* Get data specific for INFERIOR_PTID LWP. Return special data area |
| 195 for processes being detached. */ |
| 196 |
| 197 static struct i386_inferior_data * |
| 198 i386_inferior_data_get (void) |
| 199 { |
| 200 struct inferior *inf = current_inferior (); |
| 201 struct i386_inferior_data *inf_data; |
| 202 |
| 203 inf_data = inferior_data (inf, i386_inferior_data); |
| 204 if (inf_data == NULL) |
| 205 { |
| 206 inf_data = xzalloc (sizeof (*inf_data)); |
| 207 set_inferior_data (current_inferior (), i386_inferior_data, inf_data); |
| 208 } |
| 209 |
| 210 if (inf->pid != ptid_get_pid (inferior_ptid)) |
| 211 { |
| 212 /* INFERIOR_PTID is being detached from the inferior INF. |
| 213 » Provide local cache specific for the detached LWP. */ |
| 214 |
| 215 static struct i386_inferior_data detached_inf_data_local; |
| 216 static int detached_inf_pid = -1; |
| 217 |
| 218 if (detached_inf_pid != ptid_get_pid (inferior_ptid)) |
| 219 » { |
| 220 » /* Reinitialize the local cache if INFERIOR_PTID is |
| 221 » different from the LWP last detached. |
| 222 |
| 223 » Linux kernel before 2.6.33 commit |
| 224 » 72f674d203cd230426437cdcf7dd6f681dad8b0d |
| 225 » will inherit hardware debug registers from parent |
| 226 » on fork/vfork/clone. Newer Linux kernels create such tasks with |
| 227 » zeroed debug registers. |
| 228 |
| 229 » GDB will remove all breakpoints (and watchpoints) from the forked |
| 230 » off process. We also need to reset the debug registers in that |
| 231 » process to be compatible with the older Linux kernels. |
| 232 |
| 233 » Copy the debug registers mirrors into the new process so that all |
| 234 » breakpoints and watchpoints can be removed together. The debug |
| 235 » registers mirror will become zeroed in the end before detaching |
| 236 » the forked off process. */ |
| 237 |
| 238 » detached_inf_pid = ptid_get_pid (inferior_ptid); |
| 239 » detached_inf_data_local = *inf_data; |
| 240 » } |
| 241 |
| 242 return &detached_inf_data_local; |
| 243 } |
| 244 |
| 245 return inf_data; |
| 246 } |
| 247 |
| 248 /* Get debug registers state for INFERIOR_PTID, see |
| 249 i386_inferior_data_get. */ |
| 250 |
| 251 struct i386_debug_reg_state * |
| 252 i386_debug_reg_state (void) |
| 253 { |
| 254 return &i386_inferior_data_get ()->state; |
| 255 } |
| 196 | 256 |
| 197 /* Whether or not to print the mirrored debug registers. */ | 257 /* Whether or not to print the mirrored debug registers. */ |
| 198 static int maint_show_dr; | 258 static int maint_show_dr; |
| 199 | 259 |
| 200 /* Types of operations supported by i386_handle_nonaligned_watchpoint. */ | 260 /* Types of operations supported by i386_handle_nonaligned_watchpoint. */ |
| 201 typedef enum { WP_INSERT, WP_REMOVE, WP_COUNT } i386_wp_op_t; | 261 typedef enum { WP_INSERT, WP_REMOVE, WP_COUNT } i386_wp_op_t; |
| 202 | 262 |
| 203 /* Internal functions. */ | 263 /* Internal functions. */ |
| 204 | 264 |
| 205 /* Return the value of a 4-bit field for DR7 suitable for watching a | 265 /* Return the value of a 4-bit field for DR7 suitable for watching a |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 enum target_hw_bp_type type); | 297 enum target_hw_bp_type type); |
| 238 | 298 |
| 239 /* Implementation. */ | 299 /* Implementation. */ |
| 240 | 300 |
| 241 /* Clear the reference counts and forget everything we knew about the | 301 /* Clear the reference counts and forget everything we knew about the |
| 242 debug registers. */ | 302 debug registers. */ |
| 243 | 303 |
| 244 void | 304 void |
| 245 i386_cleanup_dregs (void) | 305 i386_cleanup_dregs (void) |
| 246 { | 306 { |
| 247 i386_init_dregs (&dr_mirror); | 307 struct i386_debug_reg_state *state = i386_debug_reg_state (); |
| 308 |
| 309 i386_init_dregs (state); |
| 248 } | 310 } |
| 249 | 311 |
| 250 /* Print the values of the mirrored debug registers. This is called | 312 /* Print the values of the mirrored debug registers. This is called |
| 251 when maint_show_dr is non-zero. To set that up, type "maint | 313 when maint_show_dr is non-zero. To set that up, type "maint |
| 252 show-debug-regs" at GDB's prompt. */ | 314 show-debug-regs" at GDB's prompt. */ |
| 253 | 315 |
| 254 static void | 316 static void |
| 255 i386_show_dr (struct i386_debug_reg_state *state, | 317 i386_show_dr (struct i386_debug_reg_state *state, |
| 256 const char *func, CORE_ADDR addr, | 318 const char *func, CORE_ADDR addr, |
| 257 int len, enum target_hw_bp_type type) | 319 int len, enum target_hw_bp_type type) |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 501 | 563 |
| 502 return retval; | 564 return retval; |
| 503 } | 565 } |
| 504 | 566 |
| 505 /* Update the inferior's debug registers with the new debug registers | 567 /* Update the inferior's debug registers with the new debug registers |
| 506 state, in NEW_STATE, and then update our local mirror to match. */ | 568 state, in NEW_STATE, and then update our local mirror to match. */ |
| 507 | 569 |
| 508 static void | 570 static void |
| 509 i386_update_inferior_debug_regs (struct i386_debug_reg_state *new_state) | 571 i386_update_inferior_debug_regs (struct i386_debug_reg_state *new_state) |
| 510 { | 572 { |
| 573 struct i386_debug_reg_state *state = i386_debug_reg_state (); |
| 511 int i; | 574 int i; |
| 512 | 575 |
| 513 ALL_DEBUG_REGISTERS (i) | 576 ALL_DEBUG_REGISTERS (i) |
| 514 { | 577 { |
| 515 if (I386_DR_VACANT (new_state, i) != I386_DR_VACANT (&dr_mirror, i)) | 578 if (I386_DR_VACANT (new_state, i) != I386_DR_VACANT (state, i)) |
| 516 » { | 579 » i386_dr_low.set_addr (i, new_state->dr_mirror[i]); |
| 517 » if (!I386_DR_VACANT (new_state, i)) | |
| 518 » { | |
| 519 » i386_dr_low.set_addr (i, new_state->dr_mirror[i]); | |
| 520 | |
| 521 » /* Only a sanity check for leftover bits (set possibly only | |
| 522 » » by inferior). */ | |
| 523 » if (i386_dr_low.unset_status) | |
| 524 » » i386_dr_low.unset_status (I386_DR_WATCH_MASK (i)); | |
| 525 » } | |
| 526 » else | |
| 527 » { | |
| 528 » if (i386_dr_low.reset_addr) | |
| 529 » » i386_dr_low.reset_addr (i); | |
| 530 » } | |
| 531 » } | |
| 532 else | 580 else |
| 533 » gdb_assert (new_state->dr_mirror[i] == dr_mirror.dr_mirror[i]); | 581 » gdb_assert (new_state->dr_mirror[i] == state->dr_mirror[i]); |
| 534 } | 582 } |
| 535 | 583 |
| 536 if (new_state->dr_control_mirror != dr_mirror.dr_control_mirror) | 584 if (new_state->dr_control_mirror != state->dr_control_mirror) |
| 537 i386_dr_low.set_control (new_state->dr_control_mirror); | 585 i386_dr_low.set_control (new_state->dr_control_mirror); |
| 538 | 586 |
| 539 dr_mirror = *new_state; | 587 *state = *new_state; |
| 540 } | 588 } |
| 541 | 589 |
| 542 /* Insert a watchpoint to watch a memory region which starts at | 590 /* Insert a watchpoint to watch a memory region which starts at |
| 543 address ADDR and whose length is LEN bytes. Watch memory accesses | 591 address ADDR and whose length is LEN bytes. Watch memory accesses |
| 544 of the type TYPE. Return 0 on success, -1 on failure. */ | 592 of the type TYPE. Return 0 on success, -1 on failure. */ |
| 545 | 593 |
| 546 static int | 594 static int |
| 547 i386_insert_watchpoint (CORE_ADDR addr, int len, int type, | 595 i386_insert_watchpoint (CORE_ADDR addr, int len, int type, |
| 548 struct expression *cond) | 596 struct expression *cond) |
| 549 { | 597 { |
| 598 struct i386_debug_reg_state *state = i386_debug_reg_state (); |
| 550 int retval; | 599 int retval; |
| 551 /* Work on a local copy of the debug registers, and on success, | 600 /* Work on a local copy of the debug registers, and on success, |
| 552 commit the change back to the inferior. */ | 601 commit the change back to the inferior. */ |
| 553 struct i386_debug_reg_state local_state = dr_mirror; | 602 struct i386_debug_reg_state local_state = *state; |
| 554 | 603 |
| 555 if (type == hw_read) | 604 if (type == hw_read) |
| 556 return 1; /* unsupported */ | 605 return 1; /* unsupported */ |
| 557 | 606 |
| 558 if (((len != 1 && len !=2 && len !=4) && !(TARGET_HAS_DR_LEN_8 && len == 8)) | 607 if (((len != 1 && len !=2 && len !=4) && !(TARGET_HAS_DR_LEN_8 && len == 8)) |
| 559 || addr % len != 0) | 608 || addr % len != 0) |
| 560 retval = i386_handle_nonaligned_watchpoint (&local_state, | 609 retval = i386_handle_nonaligned_watchpoint (&local_state, |
| 561 WP_INSERT, addr, len, type); | 610 WP_INSERT, addr, len, type); |
| 562 else | 611 else |
| 563 { | 612 { |
| 564 unsigned len_rw = i386_length_and_rw_bits (len, type); | 613 unsigned len_rw = i386_length_and_rw_bits (len, type); |
| 565 | 614 |
| 566 retval = i386_insert_aligned_watchpoint (&local_state, | 615 retval = i386_insert_aligned_watchpoint (&local_state, |
| 567 addr, len_rw); | 616 addr, len_rw); |
| 568 } | 617 } |
| 569 | 618 |
| 570 if (retval == 0) | 619 if (retval == 0) |
| 571 i386_update_inferior_debug_regs (&local_state); | 620 i386_update_inferior_debug_regs (&local_state); |
| 572 | 621 |
| 573 if (maint_show_dr) | 622 if (maint_show_dr) |
| 574 i386_show_dr (&dr_mirror, "insert_watchpoint", addr, len, type); | 623 i386_show_dr (state, "insert_watchpoint", addr, len, type); |
| 575 | 624 |
| 576 return retval; | 625 return retval; |
| 577 } | 626 } |
| 578 | 627 |
| 579 /* Remove a watchpoint that watched the memory region which starts at | 628 /* Remove a watchpoint that watched the memory region which starts at |
| 580 address ADDR, whose length is LEN bytes, and for accesses of the | 629 address ADDR, whose length is LEN bytes, and for accesses of the |
| 581 type TYPE. Return 0 on success, -1 on failure. */ | 630 type TYPE. Return 0 on success, -1 on failure. */ |
| 582 static int | 631 static int |
| 583 i386_remove_watchpoint (CORE_ADDR addr, int len, int type, | 632 i386_remove_watchpoint (CORE_ADDR addr, int len, int type, |
| 584 struct expression *cond) | 633 struct expression *cond) |
| 585 { | 634 { |
| 635 struct i386_debug_reg_state *state = i386_debug_reg_state (); |
| 586 int retval; | 636 int retval; |
| 587 /* Work on a local copy of the debug registers, and on success, | 637 /* Work on a local copy of the debug registers, and on success, |
| 588 commit the change back to the inferior. */ | 638 commit the change back to the inferior. */ |
| 589 struct i386_debug_reg_state local_state = dr_mirror; | 639 struct i386_debug_reg_state local_state = *state; |
| 590 | 640 |
| 591 if (((len != 1 && len !=2 && len !=4) && !(TARGET_HAS_DR_LEN_8 && len == 8)) | 641 if (((len != 1 && len !=2 && len !=4) && !(TARGET_HAS_DR_LEN_8 && len == 8)) |
| 592 || addr % len != 0) | 642 || addr % len != 0) |
| 593 retval = i386_handle_nonaligned_watchpoint (&local_state, | 643 retval = i386_handle_nonaligned_watchpoint (&local_state, |
| 594 WP_REMOVE, addr, len, type); | 644 WP_REMOVE, addr, len, type); |
| 595 else | 645 else |
| 596 { | 646 { |
| 597 unsigned len_rw = i386_length_and_rw_bits (len, type); | 647 unsigned len_rw = i386_length_and_rw_bits (len, type); |
| 598 | 648 |
| 599 retval = i386_remove_aligned_watchpoint (&local_state, | 649 retval = i386_remove_aligned_watchpoint (&local_state, |
| 600 addr, len_rw); | 650 addr, len_rw); |
| 601 } | 651 } |
| 602 | 652 |
| 603 if (retval == 0) | 653 if (retval == 0) |
| 604 i386_update_inferior_debug_regs (&local_state); | 654 i386_update_inferior_debug_regs (&local_state); |
| 605 | 655 |
| 606 if (maint_show_dr) | 656 if (maint_show_dr) |
| 607 i386_show_dr (&dr_mirror, "remove_watchpoint", addr, len, type); | 657 i386_show_dr (state, "remove_watchpoint", addr, len, type); |
| 608 | 658 |
| 609 return retval; | 659 return retval; |
| 610 } | 660 } |
| 611 | 661 |
| 612 /* Return non-zero if we can watch a memory region that starts at | 662 /* Return non-zero if we can watch a memory region that starts at |
| 613 address ADDR and whose length is LEN bytes. */ | 663 address ADDR and whose length is LEN bytes. */ |
| 614 | 664 |
| 615 static int | 665 static int |
| 616 i386_region_ok_for_watchpoint (CORE_ADDR addr, int len) | 666 i386_region_ok_for_watchpoint (CORE_ADDR addr, int len) |
| 617 { | 667 { |
| 668 struct i386_debug_reg_state *state = i386_debug_reg_state (); |
| 618 int nregs; | 669 int nregs; |
| 619 | 670 |
| 620 /* Compute how many aligned watchpoints we would need to cover this | 671 /* Compute how many aligned watchpoints we would need to cover this |
| 621 region. */ | 672 region. */ |
| 622 nregs = i386_handle_nonaligned_watchpoint (&dr_mirror, | 673 nregs = i386_handle_nonaligned_watchpoint (state, |
| 623 WP_COUNT, addr, len, hw_write); | 674 WP_COUNT, addr, len, hw_write); |
| 624 return nregs <= DR_NADDR ? 1 : 0; | 675 return nregs <= DR_NADDR ? 1 : 0; |
| 625 } | 676 } |
| 626 | 677 |
| 627 /* If the inferior has some watchpoint that triggered, set the | 678 /* If the inferior has some watchpoint that triggered, set the |
| 628 address associated with that watchpoint and return non-zero. | 679 address associated with that watchpoint and return non-zero. |
| 629 Otherwise, return zero. */ | 680 Otherwise, return zero. */ |
| 630 | 681 |
| 631 static int | 682 static int |
| 632 i386_stopped_data_address (struct target_ops *ops, CORE_ADDR *addr_p) | 683 i386_stopped_data_address (struct target_ops *ops, CORE_ADDR *addr_p) |
| 633 { | 684 { |
| 685 struct i386_debug_reg_state *state = i386_debug_reg_state (); |
| 634 CORE_ADDR addr = 0; | 686 CORE_ADDR addr = 0; |
| 635 int i; | 687 int i; |
| 636 int rc = 0; | 688 int rc = 0; |
| 689 /* The current thread's DR_STATUS. We always need to read this to |
| 690 check whether some watchpoint caused the trap. */ |
| 637 unsigned status; | 691 unsigned status; |
| 638 unsigned control; | 692 /* We need DR_CONTROL as well, but only iff DR_STATUS indicates a |
| 639 struct i386_debug_reg_state *state = &dr_mirror; | 693 data breakpoint trap. Only fetch it when necessary, to avoid an |
| 694 unnecessary extra syscall when no watchpoint triggered. */ |
| 695 int control_p = 0; |
| 696 unsigned control = 0; |
| 640 | 697 |
| 641 dr_mirror.dr_status_mirror = i386_dr_low.get_status (); | 698 /* In non-stop/async, threads can be running while we change the |
| 642 status = dr_mirror.dr_status_mirror; | 699 STATE (and friends). Say, we set a watchpoint, and let threads |
| 643 control = dr_mirror.dr_control_mirror; | 700 resume. Now, say you delete the watchpoint, or add/remove |
| 701 watchpoints such that STATE changes while threads are running. |
| 702 On targets that support non-stop, inserting/deleting watchpoints |
| 703 updates the STATE only. It does not update the real thread's |
| 704 debug registers; that's only done prior to resume. Instead, if |
| 705 threads are running when the mirror changes, a temporary and |
| 706 transparent stop on all threads is forced so they can get their |
| 707 copy of the debug registers updated on re-resume. Now, say, |
| 708 a thread hit a watchpoint before having been updated with the new |
| 709 STATE contents, and we haven't yet handled the corresponding |
| 710 SIGTRAP. If we trusted STATE below, we'd mistake the real |
| 711 trapped address (from the last time we had updated debug |
| 712 registers in the thread) with whatever was currently in STATE. |
| 713 So to fix this, STATE always represents intention, what we _want_ |
| 714 threads to have in debug registers. To get at the address and |
| 715 cause of the trap, we need to read the state the thread still has |
| 716 in its debug registers. |
| 717 |
| 718 In sum, always get the current debug register values the current |
| 719 thread has, instead of trusting the global mirror. If the thread |
| 720 was running when we last changed watchpoints, the mirror no |
| 721 longer represents what was set in this thread's debug |
| 722 registers. */ |
| 723 status = i386_dr_low.get_status (); |
| 644 | 724 |
| 645 ALL_DEBUG_REGISTERS(i) | 725 ALL_DEBUG_REGISTERS(i) |
| 646 { | 726 { |
| 647 if (I386_DR_WATCH_HIT (status, i) | 727 if (!I386_DR_WATCH_HIT (status, i)) |
| 648 » /* This second condition makes sure DRi is set up for a data | 728 » continue; |
| 649 » watchpoint, not a hardware breakpoint. The reason is | 729 |
| 650 » that GDB doesn't call the target_stopped_data_address | 730 if (!control_p) |
| 651 » method except for data watchpoints. In other words, I'm | |
| 652 » being paranoiac. */ | |
| 653 » && I386_DR_GET_RW_LEN (control, i) != 0 | |
| 654 » /* This third condition makes sure DRi is not vacant, this | |
| 655 » avoids false positives in windows-nat.c. */ | |
| 656 » && !I386_DR_VACANT (state, i)) | |
| 657 { | 731 { |
| 658 » addr = state->dr_mirror[i]; | 732 » control = i386_dr_low.get_control (); |
| 733 » control_p = 1; |
| 734 » } |
| 735 |
| 736 /* This second condition makes sure DRi is set up for a data |
| 737 » watchpoint, not a hardware breakpoint. The reason is that |
| 738 » GDB doesn't call the target_stopped_data_address method |
| 739 » except for data watchpoints. In other words, I'm being |
| 740 » paranoiac. */ |
| 741 if (I386_DR_GET_RW_LEN (control, i) != 0) |
| 742 » { |
| 743 » addr = i386_dr_low.get_addr (i); |
| 659 rc = 1; | 744 rc = 1; |
| 660 if (maint_show_dr) | 745 if (maint_show_dr) |
| 661 » i386_show_dr (&dr_mirror, "watchpoint_hit", addr, -1, hw_write); | 746 » i386_show_dr (state, "watchpoint_hit", addr, -1, hw_write); |
| 662 } | 747 } |
| 663 } | 748 } |
| 664 if (maint_show_dr && addr == 0) | 749 if (maint_show_dr && addr == 0) |
| 665 i386_show_dr (&dr_mirror, "stopped_data_addr", 0, 0, hw_write); | 750 i386_show_dr (state, "stopped_data_addr", 0, 0, hw_write); |
| 666 | 751 |
| 667 if (rc) | 752 if (rc) |
| 668 *addr_p = addr; | 753 *addr_p = addr; |
| 669 return rc; | 754 return rc; |
| 670 } | 755 } |
| 671 | 756 |
| 672 static int | 757 static int |
| 673 i386_stopped_by_watchpoint (void) | 758 i386_stopped_by_watchpoint (void) |
| 674 { | 759 { |
| 675 CORE_ADDR addr = 0; | 760 CORE_ADDR addr = 0; |
| 676 return i386_stopped_data_address (¤t_target, &addr); | 761 return i386_stopped_data_address (¤t_target, &addr); |
| 677 } | 762 } |
| 678 | 763 |
| 679 /* Insert a hardware-assisted breakpoint at BP_TGT->placed_address. | 764 /* Insert a hardware-assisted breakpoint at BP_TGT->placed_address. |
| 680 Return 0 on success, EBUSY on failure. */ | 765 Return 0 on success, EBUSY on failure. */ |
| 681 static int | 766 static int |
| 682 i386_insert_hw_breakpoint (struct gdbarch *gdbarch, | 767 i386_insert_hw_breakpoint (struct gdbarch *gdbarch, |
| 683 struct bp_target_info *bp_tgt) | 768 struct bp_target_info *bp_tgt) |
| 684 { | 769 { |
| 770 struct i386_debug_reg_state *state = i386_debug_reg_state (); |
| 685 unsigned len_rw = i386_length_and_rw_bits (1, hw_execute); | 771 unsigned len_rw = i386_length_and_rw_bits (1, hw_execute); |
| 686 CORE_ADDR addr = bp_tgt->placed_address; | 772 CORE_ADDR addr = bp_tgt->placed_address; |
| 687 /* Work on a local copy of the debug registers, and on success, | 773 /* Work on a local copy of the debug registers, and on success, |
| 688 commit the change back to the inferior. */ | 774 commit the change back to the inferior. */ |
| 689 struct i386_debug_reg_state local_state = dr_mirror; | 775 struct i386_debug_reg_state local_state = *state; |
| 690 int retval = i386_insert_aligned_watchpoint (&local_state, | 776 int retval = i386_insert_aligned_watchpoint (&local_state, |
| 691 addr, len_rw) ? EBUSY : 0; | 777 addr, len_rw) ? EBUSY : 0; |
| 692 | 778 |
| 693 if (retval == 0) | 779 if (retval == 0) |
| 694 i386_update_inferior_debug_regs (&local_state); | 780 i386_update_inferior_debug_regs (&local_state); |
| 695 | 781 |
| 696 if (maint_show_dr) | 782 if (maint_show_dr) |
| 697 i386_show_dr (&dr_mirror, "insert_hwbp", addr, 1, hw_execute); | 783 i386_show_dr (state, "insert_hwbp", addr, 1, hw_execute); |
| 698 | 784 |
| 699 return retval; | 785 return retval; |
| 700 } | 786 } |
| 701 | 787 |
| 702 /* Remove a hardware-assisted breakpoint at BP_TGT->placed_address. | 788 /* Remove a hardware-assisted breakpoint at BP_TGT->placed_address. |
| 703 Return 0 on success, -1 on failure. */ | 789 Return 0 on success, -1 on failure. */ |
| 704 | 790 |
| 705 static int | 791 static int |
| 706 i386_remove_hw_breakpoint (struct gdbarch *gdbarch, | 792 i386_remove_hw_breakpoint (struct gdbarch *gdbarch, |
| 707 struct bp_target_info *bp_tgt) | 793 struct bp_target_info *bp_tgt) |
| 708 { | 794 { |
| 795 struct i386_debug_reg_state *state = i386_debug_reg_state (); |
| 709 unsigned len_rw = i386_length_and_rw_bits (1, hw_execute); | 796 unsigned len_rw = i386_length_and_rw_bits (1, hw_execute); |
| 710 CORE_ADDR addr = bp_tgt->placed_address; | 797 CORE_ADDR addr = bp_tgt->placed_address; |
| 711 /* Work on a local copy of the debug registers, and on success, | 798 /* Work on a local copy of the debug registers, and on success, |
| 712 commit the change back to the inferior. */ | 799 commit the change back to the inferior. */ |
| 713 struct i386_debug_reg_state local_state = dr_mirror; | 800 struct i386_debug_reg_state local_state = *state; |
| 714 int retval = i386_remove_aligned_watchpoint (&local_state, | 801 int retval = i386_remove_aligned_watchpoint (&local_state, |
| 715 addr, len_rw); | 802 addr, len_rw); |
| 716 | 803 |
| 717 if (retval == 0) | 804 if (retval == 0) |
| 718 i386_update_inferior_debug_regs (&local_state); | 805 i386_update_inferior_debug_regs (&local_state); |
| 719 | 806 |
| 720 if (maint_show_dr) | 807 if (maint_show_dr) |
| 721 i386_show_dr (&dr_mirror, "remove_hwbp", addr, 1, hw_execute); | 808 i386_show_dr (state, "remove_hwbp", addr, 1, hw_execute); |
| 722 | 809 |
| 723 return retval; | 810 return retval; |
| 724 } | 811 } |
| 725 | 812 |
| 726 /* Returns the number of hardware watchpoints of type TYPE that we can | 813 /* Returns the number of hardware watchpoints of type TYPE that we can |
| 727 set. Value is positive if we can set CNT watchpoints, zero if | 814 set. Value is positive if we can set CNT watchpoints, zero if |
| 728 setting watchpoints of type TYPE is not supported, and negative if | 815 setting watchpoints of type TYPE is not supported, and negative if |
| 729 CNT is more than the maximum number of watchpoints of type TYPE | 816 CNT is more than the maximum number of watchpoints of type TYPE |
| 730 that we can support. TYPE is one of bp_hardware_watchpoint, | 817 that we can support. TYPE is one of bp_hardware_watchpoint, |
| 731 bp_read_watchpoint, bp_write_watchpoint, or bp_hardware_breakpoint. | 818 bp_read_watchpoint, bp_write_watchpoint, or bp_hardware_breakpoint. |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 776 t->to_have_continuable_watchpoint = 1; | 863 t->to_have_continuable_watchpoint = 1; |
| 777 | 864 |
| 778 t->to_can_use_hw_breakpoint = i386_can_use_hw_breakpoint; | 865 t->to_can_use_hw_breakpoint = i386_can_use_hw_breakpoint; |
| 779 t->to_region_ok_for_hw_watchpoint = i386_region_ok_for_watchpoint; | 866 t->to_region_ok_for_hw_watchpoint = i386_region_ok_for_watchpoint; |
| 780 t->to_stopped_by_watchpoint = i386_stopped_by_watchpoint; | 867 t->to_stopped_by_watchpoint = i386_stopped_by_watchpoint; |
| 781 t->to_stopped_data_address = i386_stopped_data_address; | 868 t->to_stopped_data_address = i386_stopped_data_address; |
| 782 t->to_insert_watchpoint = i386_insert_watchpoint; | 869 t->to_insert_watchpoint = i386_insert_watchpoint; |
| 783 t->to_remove_watchpoint = i386_remove_watchpoint; | 870 t->to_remove_watchpoint = i386_remove_watchpoint; |
| 784 t->to_insert_hw_breakpoint = i386_insert_hw_breakpoint; | 871 t->to_insert_hw_breakpoint = i386_insert_hw_breakpoint; |
| 785 t->to_remove_hw_breakpoint = i386_remove_hw_breakpoint; | 872 t->to_remove_hw_breakpoint = i386_remove_hw_breakpoint; |
| 873 |
| 874 if (i386_inferior_data == NULL) |
| 875 i386_inferior_data |
| 876 = register_inferior_data_with_cleanup (i386_inferior_data_cleanup); |
| 786 } | 877 } |
| 787 | 878 |
| 788 void | 879 void |
| 789 i386_set_debug_register_length (int len) | 880 i386_set_debug_register_length (int len) |
| 790 { | 881 { |
| 791 /* This function should be called only once for each native target. */ | 882 /* This function should be called only once for each native target. */ |
| 792 gdb_assert (i386_dr_low.debug_register_length == 0); | 883 gdb_assert (i386_dr_low.debug_register_length == 0); |
| 793 gdb_assert (len == 4 || len == 8); | 884 gdb_assert (len == 4 || len == 8); |
| 794 i386_dr_low.debug_register_length = len; | 885 i386_dr_low.debug_register_length = len; |
| 795 add_show_debug_regs_command (); | 886 add_show_debug_regs_command (); |
| 796 } | 887 } |
| OLD | NEW |