1 /* Native-dependent code for x86 (i386 and x86-64). 2 3 Copyright (C) 2001-2024 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 "x86-nat.h" 21 #include "cli/cli-cmds.h" 22 #include "inferior.h" 23 24 #include <unordered_map> 25 26 /* Support for hardware watchpoints and breakpoints using the x86 27 debug registers. 28 29 This provides several functions for inserting and removing 30 hardware-assisted breakpoints and watchpoints, testing if one or 31 more of the watchpoints triggered and at what address, checking 32 whether a given region can be watched, etc. 33 34 The functions below implement debug registers sharing by reference 35 counts, and allow to watch regions up to 16 bytes long. */ 36 37 /* Low-level function vector. */ 38 struct x86_dr_low_type x86_dr_low; 39 40 /* Hash table storing per-process data. We don't bind this to a 41 per-inferior registry because of targets like x86 GNU/Linux that 42 need to keep track of processes that aren't bound to any inferior 43 (e.g., fork children, checkpoints). */ 44 45 static std::unordered_map<pid_t, 46 struct x86_debug_reg_state> x86_debug_process_state; 47 48 /* See x86-nat.h. */ 49 50 struct x86_debug_reg_state * 51 x86_lookup_debug_reg_state (pid_t pid) 52 { 53 auto it = x86_debug_process_state.find (pid); 54 if (it != x86_debug_process_state.end ()) 55 return &it->second; 56 57 return nullptr; 58 } 59 60 /* Get debug registers state for process PID. */ 61 62 struct x86_debug_reg_state * 63 x86_debug_reg_state (pid_t pid) 64 { 65 return &x86_debug_process_state[pid]; 66 } 67 68 /* See declaration in x86-nat.h. */ 69 70 void 71 x86_forget_process (pid_t pid) 72 { 73 x86_debug_process_state.erase (pid); 74 } 75 76 /* Clear the reference counts and forget everything we knew about the 77 debug registers. */ 78 79 void 80 x86_cleanup_dregs (void) 81 { 82 /* Starting from scratch has the same effect. */ 83 x86_forget_process (inferior_ptid.pid ()); 84 } 85 86 /* Insert a watchpoint to watch a memory region which starts at 87 address ADDR and whose length is LEN bytes. Watch memory accesses 88 of the type TYPE. Return 0 on success, -1 on failure. */ 89 90 int 91 x86_insert_watchpoint (CORE_ADDR addr, int len, 92 enum target_hw_bp_type type, struct expression *cond) 93 { 94 struct x86_debug_reg_state *state 95 = x86_debug_reg_state (inferior_ptid.pid ()); 96 97 return x86_dr_insert_watchpoint (state, type, addr, len); 98 } 99 100 /* Remove a watchpoint that watched the memory region which starts at 101 address ADDR, whose length is LEN bytes, and for accesses of the 102 type TYPE. Return 0 on success, -1 on failure. */ 103 int 104 x86_remove_watchpoint (CORE_ADDR addr, int len, 105 enum target_hw_bp_type type, struct expression *cond) 106 { 107 struct x86_debug_reg_state *state 108 = x86_debug_reg_state (inferior_ptid.pid ()); 109 110 return x86_dr_remove_watchpoint (state, type, addr, len); 111 } 112 113 /* Return non-zero if we can watch a memory region that starts at 114 address ADDR and whose length is LEN bytes. */ 115 116 int 117 x86_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len) 118 { 119 struct x86_debug_reg_state *state 120 = x86_debug_reg_state (inferior_ptid.pid ()); 121 122 return x86_dr_region_ok_for_watchpoint (state, addr, len); 123 } 124 125 /* If the inferior has some break/watchpoint that triggered, set the 126 address associated with that break/watchpoint and return non-zero. 127 Otherwise, return zero. */ 128 129 int 130 x86_stopped_data_address (CORE_ADDR *addr_p) 131 { 132 struct x86_debug_reg_state *state 133 = x86_debug_reg_state (inferior_ptid.pid ()); 134 135 return x86_dr_stopped_data_address (state, addr_p); 136 } 137 138 /* Return non-zero if the inferior has some watchpoint that triggered. 139 Otherwise return zero. */ 140 141 int 142 x86_stopped_by_watchpoint () 143 { 144 struct x86_debug_reg_state *state 145 = x86_debug_reg_state (inferior_ptid.pid ()); 146 147 return x86_dr_stopped_by_watchpoint (state); 148 } 149 150 /* Insert a hardware-assisted breakpoint at BP_TGT->reqstd_address. 151 Return 0 on success, EBUSY on failure. */ 152 153 int 154 x86_insert_hw_breakpoint (struct gdbarch *gdbarch, struct bp_target_info *bp_tgt) 155 { 156 struct x86_debug_reg_state *state 157 = x86_debug_reg_state (inferior_ptid.pid ()); 158 159 bp_tgt->placed_address = bp_tgt->reqstd_address; 160 return x86_dr_insert_watchpoint (state, hw_execute, 161 bp_tgt->placed_address, 1) ? EBUSY : 0; 162 } 163 164 /* Remove a hardware-assisted breakpoint at BP_TGT->placed_address. 165 Return 0 on success, -1 on failure. */ 166 167 int 168 x86_remove_hw_breakpoint (struct gdbarch *gdbarch, 169 struct bp_target_info *bp_tgt) 170 { 171 struct x86_debug_reg_state *state 172 = x86_debug_reg_state (inferior_ptid.pid ()); 173 174 return x86_dr_remove_watchpoint (state, hw_execute, 175 bp_tgt->placed_address, 1); 176 } 177 178 /* Returns the number of hardware watchpoints of type TYPE that we can 179 set. Value is positive if we can set CNT watchpoints, zero if 180 setting watchpoints of type TYPE is not supported, and negative if 181 CNT is more than the maximum number of watchpoints of type TYPE 182 that we can support. TYPE is one of bp_hardware_watchpoint, 183 bp_read_watchpoint, bp_write_watchpoint, or bp_hardware_breakpoint. 184 CNT is the number of such watchpoints used so far (including this 185 one). OTHERTYPE is non-zero if other types of watchpoints are 186 currently enabled. 187 188 We always return 1 here because we don't have enough information 189 about possible overlap of addresses that they want to watch. As an 190 extreme example, consider the case where all the watchpoints watch 191 the same address and the same region length: then we can handle a 192 virtually unlimited number of watchpoints, due to debug register 193 sharing implemented via reference counts in x86-nat.c. */ 194 195 int 196 x86_can_use_hw_breakpoint (enum bptype type, int cnt, int othertype) 197 { 198 return 1; 199 } 200 201 /* Return non-zero if the inferior has some breakpoint that triggered. 202 Otherwise return zero. */ 203 204 int 205 x86_stopped_by_hw_breakpoint () 206 { 207 struct x86_debug_reg_state *state 208 = x86_debug_reg_state (inferior_ptid.pid ()); 209 210 return x86_dr_stopped_by_hw_breakpoint (state); 211 } 212 213 static void 214 add_show_debug_regs_command (void) 215 { 216 /* A maintenance command to enable printing the internal DRi mirror 217 variables. */ 218 add_setshow_boolean_cmd ("show-debug-regs", class_maintenance, 219 &show_debug_regs, _("\ 220 Set whether to show variables that mirror the x86 debug registers."), _("\ 221 Show whether to show variables that mirror the x86 debug registers."), _("\ 222 Use \"on\" to enable, \"off\" to disable.\n\ 223 If enabled, the debug registers values are shown when GDB inserts\n\ 224 or removes a hardware breakpoint or watchpoint, and when the inferior\n\ 225 triggers a breakpoint or watchpoint."), 226 NULL, 227 NULL, 228 &maintenance_set_cmdlist, 229 &maintenance_show_cmdlist); 230 } 231 232 /* See x86-nat.h. */ 233 234 void 235 x86_set_debug_register_length (int len) 236 { 237 /* This function should be called only once for each native target. */ 238 gdb_assert (x86_dr_low.debug_register_length == 0); 239 gdb_assert (len == 4 || len == 8); 240 x86_dr_low.debug_register_length = len; 241 add_show_debug_regs_command (); 242 } 243