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