x86-nat.c revision 1.3
1/* Native-dependent code for x86 (i386 and x86-64). 2 3 Copyright (C) 2001-2015 Free Software Foundation, Inc. 4 5 This file is part of GDB. 6 7 This program is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 3 of the License, or 10 (at your option) any later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19 20#include "defs.h" 21#include "x86-nat.h" 22#include "gdbcmd.h" 23#include "inferior.h" 24 25/* Support for hardware watchpoints and breakpoints using the x86 26 debug registers. 27 28 This provides several functions for inserting and removing 29 hardware-assisted breakpoints and watchpoints, testing if one or 30 more of the watchpoints triggered and at what address, checking 31 whether a given region can be watched, etc. 32 33 The functions below implement debug registers sharing by reference 34 counts, and allow to watch regions up to 16 bytes long. */ 35 36/* Low-level function vector. */ 37struct x86_dr_low_type x86_dr_low; 38 39/* Per-process data. We don't bind this to a per-inferior registry 40 because of targets like x86 GNU/Linux that need to keep track of 41 processes that aren't bound to any inferior (e.g., fork children, 42 checkpoints). */ 43 44struct x86_process_info 45{ 46 /* Linked list. */ 47 struct x86_process_info *next; 48 49 /* The process identifier. */ 50 pid_t pid; 51 52 /* Copy of x86 hardware debug registers. */ 53 struct x86_debug_reg_state state; 54}; 55 56static struct x86_process_info *x86_process_list = NULL; 57 58/* Find process data for process PID. */ 59 60static struct x86_process_info * 61x86_find_process_pid (pid_t pid) 62{ 63 struct x86_process_info *proc; 64 65 for (proc = x86_process_list; proc; proc = proc->next) 66 if (proc->pid == pid) 67 return proc; 68 69 return NULL; 70} 71 72/* Add process data for process PID. Returns newly allocated info 73 object. */ 74 75static struct x86_process_info * 76x86_add_process (pid_t pid) 77{ 78 struct x86_process_info *proc; 79 80 proc = xcalloc (1, sizeof (*proc)); 81 proc->pid = pid; 82 83 proc->next = x86_process_list; 84 x86_process_list = proc; 85 86 return proc; 87} 88 89/* Get data specific info for process PID, creating it if necessary. 90 Never returns NULL. */ 91 92static struct x86_process_info * 93x86_process_info_get (pid_t pid) 94{ 95 struct x86_process_info *proc; 96 97 proc = x86_find_process_pid (pid); 98 if (proc == NULL) 99 proc = x86_add_process (pid); 100 101 return proc; 102} 103 104/* Get debug registers state for process PID. */ 105 106struct x86_debug_reg_state * 107x86_debug_reg_state (pid_t pid) 108{ 109 return &x86_process_info_get (pid)->state; 110} 111 112/* See declaration in i386-nat.h. */ 113 114void 115x86_forget_process (pid_t pid) 116{ 117 struct x86_process_info *proc, **proc_link; 118 119 proc = x86_process_list; 120 proc_link = &x86_process_list; 121 122 while (proc != NULL) 123 { 124 if (proc->pid == pid) 125 { 126 *proc_link = proc->next; 127 128 xfree (proc); 129 return; 130 } 131 132 proc_link = &proc->next; 133 proc = *proc_link; 134 } 135} 136 137/* Clear the reference counts and forget everything we knew about the 138 debug registers. */ 139 140void 141x86_cleanup_dregs (void) 142{ 143 /* Starting from scratch has the same effect. */ 144 x86_forget_process (ptid_get_pid (inferior_ptid)); 145} 146 147/* Insert a watchpoint to watch a memory region which starts at 148 address ADDR and whose length is LEN bytes. Watch memory accesses 149 of the type TYPE. Return 0 on success, -1 on failure. */ 150 151static int 152x86_insert_watchpoint (struct target_ops *self, 153 CORE_ADDR addr, int len, int type, 154 struct expression *cond) 155{ 156 struct x86_debug_reg_state *state 157 = x86_debug_reg_state (ptid_get_pid (inferior_ptid)); 158 159 return x86_dr_insert_watchpoint (state, type, addr, len); 160} 161 162/* Remove a watchpoint that watched the memory region which starts at 163 address ADDR, whose length is LEN bytes, and for accesses of the 164 type TYPE. Return 0 on success, -1 on failure. */ 165static int 166x86_remove_watchpoint (struct target_ops *self, 167 CORE_ADDR addr, int len, int type, 168 struct expression *cond) 169{ 170 struct x86_debug_reg_state *state 171 = x86_debug_reg_state (ptid_get_pid (inferior_ptid)); 172 173 return x86_dr_remove_watchpoint (state, type, addr, len); 174} 175 176/* Return non-zero if we can watch a memory region that starts at 177 address ADDR and whose length is LEN bytes. */ 178 179static int 180x86_region_ok_for_watchpoint (struct target_ops *self, 181 CORE_ADDR addr, int len) 182{ 183 struct x86_debug_reg_state *state 184 = x86_debug_reg_state (ptid_get_pid (inferior_ptid)); 185 186 return x86_dr_region_ok_for_watchpoint (state, addr, len); 187} 188 189/* If the inferior has some break/watchpoint that triggered, set the 190 address associated with that break/watchpoint and return non-zero. 191 Otherwise, return zero. */ 192 193static int 194x86_stopped_data_address (struct target_ops *ops, CORE_ADDR *addr_p) 195{ 196 struct x86_debug_reg_state *state 197 = x86_debug_reg_state (ptid_get_pid (inferior_ptid)); 198 199 return x86_dr_stopped_data_address (state, addr_p); 200} 201 202/* Return non-zero if the inferior has some watchpoint that triggered. 203 Otherwise return zero. */ 204 205static int 206x86_stopped_by_watchpoint (struct target_ops *ops) 207{ 208 struct x86_debug_reg_state *state 209 = x86_debug_reg_state (ptid_get_pid (inferior_ptid)); 210 211 return x86_dr_stopped_by_watchpoint (state); 212} 213 214/* Insert a hardware-assisted breakpoint at BP_TGT->reqstd_address. 215 Return 0 on success, EBUSY on failure. */ 216 217static int 218x86_insert_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch, 219 struct bp_target_info *bp_tgt) 220{ 221 struct x86_debug_reg_state *state 222 = x86_debug_reg_state (ptid_get_pid (inferior_ptid)); 223 224 bp_tgt->placed_address = bp_tgt->reqstd_address; 225 return x86_dr_insert_watchpoint (state, hw_execute, 226 bp_tgt->placed_address, 1) ? EBUSY : 0; 227} 228 229/* Remove a hardware-assisted breakpoint at BP_TGT->placed_address. 230 Return 0 on success, -1 on failure. */ 231 232static int 233x86_remove_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch, 234 struct bp_target_info *bp_tgt) 235{ 236 struct x86_debug_reg_state *state 237 = x86_debug_reg_state (ptid_get_pid (inferior_ptid)); 238 239 return x86_dr_remove_watchpoint (state, hw_execute, 240 bp_tgt->placed_address, 1); 241} 242 243/* Returns the number of hardware watchpoints of type TYPE that we can 244 set. Value is positive if we can set CNT watchpoints, zero if 245 setting watchpoints of type TYPE is not supported, and negative if 246 CNT is more than the maximum number of watchpoints of type TYPE 247 that we can support. TYPE is one of bp_hardware_watchpoint, 248 bp_read_watchpoint, bp_write_watchpoint, or bp_hardware_breakpoint. 249 CNT is the number of such watchpoints used so far (including this 250 one). OTHERTYPE is non-zero if other types of watchpoints are 251 currently enabled. 252 253 We always return 1 here because we don't have enough information 254 about possible overlap of addresses that they want to watch. As an 255 extreme example, consider the case where all the watchpoints watch 256 the same address and the same region length: then we can handle a 257 virtually unlimited number of watchpoints, due to debug register 258 sharing implemented via reference counts in i386-nat.c. */ 259 260static int 261x86_can_use_hw_breakpoint (struct target_ops *self, 262 int type, int cnt, int othertype) 263{ 264 return 1; 265} 266 267static void 268add_show_debug_regs_command (void) 269{ 270 /* A maintenance command to enable printing the internal DRi mirror 271 variables. */ 272 add_setshow_boolean_cmd ("show-debug-regs", class_maintenance, 273 &show_debug_regs, _("\ 274Set whether to show variables that mirror the x86 debug registers."), _("\ 275Show whether to show variables that mirror the x86 debug registers."), _("\ 276Use \"on\" to enable, \"off\" to disable.\n\ 277If enabled, the debug registers values are shown when GDB inserts\n\ 278or removes a hardware breakpoint or watchpoint, and when the inferior\n\ 279triggers a breakpoint or watchpoint."), 280 NULL, 281 NULL, 282 &maintenance_set_cmdlist, 283 &maintenance_show_cmdlist); 284} 285 286/* There are only two global functions left. */ 287 288void 289x86_use_watchpoints (struct target_ops *t) 290{ 291 /* After a watchpoint trap, the PC points to the instruction after the 292 one that caused the trap. Therefore we don't need to step over it. 293 But we do need to reset the status register to avoid another trap. */ 294 t->to_have_continuable_watchpoint = 1; 295 296 t->to_can_use_hw_breakpoint = x86_can_use_hw_breakpoint; 297 t->to_region_ok_for_hw_watchpoint = x86_region_ok_for_watchpoint; 298 t->to_stopped_by_watchpoint = x86_stopped_by_watchpoint; 299 t->to_stopped_data_address = x86_stopped_data_address; 300 t->to_insert_watchpoint = x86_insert_watchpoint; 301 t->to_remove_watchpoint = x86_remove_watchpoint; 302 t->to_insert_hw_breakpoint = x86_insert_hw_breakpoint; 303 t->to_remove_hw_breakpoint = x86_remove_hw_breakpoint; 304} 305 306void 307x86_set_debug_register_length (int len) 308{ 309 /* This function should be called only once for each native target. */ 310 gdb_assert (x86_dr_low.debug_register_length == 0); 311 gdb_assert (len == 4 || len == 8); 312 x86_dr_low.debug_register_length = len; 313 add_show_debug_regs_command (); 314} 315