Commit | Line | Data |
---|---|---|
df7e5265 | 1 | /* Native-dependent code for x86 (i386 and x86-64). |
7fa2737c | 2 | |
b811d2c2 | 3 | Copyright (C) 2001-2020 Free Software Foundation, Inc. |
52b98211 EZ |
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 | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
52b98211 EZ |
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 | |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
52b98211 EZ |
19 | |
20 | #include "defs.h" | |
df7e5265 | 21 | #include "x86-nat.h" |
52b98211 | 22 | #include "gdbcmd.h" |
4403d8e9 | 23 | #include "inferior.h" |
52b98211 | 24 | |
df7e5265 | 25 | /* Support for hardware watchpoints and breakpoints using the x86 |
52b98211 EZ |
26 | debug registers. |
27 | ||
28 | This provides several functions for inserting and removing | |
7fa2737c MK |
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 | ||
7fa2737c MK |
33 | The functions below implement debug registers sharing by reference |
34 | counts, and allow to watch regions up to 16 bytes long. */ | |
52b98211 | 35 | |
6e62758f | 36 | /* Low-level function vector. */ |
df7e5265 | 37 | struct x86_dr_low_type x86_dr_low; |
9bb9e8ad | 38 | |
26cb8b7c PA |
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). */ | |
1ced966e | 43 | |
df7e5265 | 44 | struct x86_process_info |
1ced966e | 45 | { |
26cb8b7c | 46 | /* Linked list. */ |
df7e5265 | 47 | struct x86_process_info *next; |
1ced966e | 48 | |
26cb8b7c PA |
49 | /* The process identifier. */ |
50 | pid_t pid; | |
4403d8e9 | 51 | |
df7e5265 GB |
52 | /* Copy of x86 hardware debug registers. */ |
53 | struct x86_debug_reg_state state; | |
4403d8e9 JK |
54 | }; |
55 | ||
df7e5265 | 56 | static struct x86_process_info *x86_process_list = NULL; |
d0d8b0c6 | 57 | |
26cb8b7c PA |
58 | /* Find process data for process PID. */ |
59 | ||
df7e5265 GB |
60 | static struct x86_process_info * |
61 | x86_find_process_pid (pid_t pid) | |
d0d8b0c6 | 62 | { |
df7e5265 | 63 | struct x86_process_info *proc; |
26cb8b7c | 64 | |
df7e5265 | 65 | for (proc = x86_process_list; proc; proc = proc->next) |
26cb8b7c PA |
66 | if (proc->pid == pid) |
67 | return proc; | |
d0d8b0c6 | 68 | |
26cb8b7c | 69 | return NULL; |
d0d8b0c6 JK |
70 | } |
71 | ||
26cb8b7c PA |
72 | /* Add process data for process PID. Returns newly allocated info |
73 | object. */ | |
4403d8e9 | 74 | |
df7e5265 GB |
75 | static struct x86_process_info * |
76 | x86_add_process (pid_t pid) | |
4403d8e9 | 77 | { |
8d749320 | 78 | struct x86_process_info *proc = XCNEW (struct x86_process_info); |
d0d8b0c6 | 79 | |
26cb8b7c | 80 | proc->pid = pid; |
df7e5265 GB |
81 | proc->next = x86_process_list; |
82 | x86_process_list = proc; | |
4403d8e9 | 83 | |
26cb8b7c PA |
84 | return proc; |
85 | } | |
4403d8e9 | 86 | |
26cb8b7c PA |
87 | /* Get data specific info for process PID, creating it if necessary. |
88 | Never returns NULL. */ | |
4403d8e9 | 89 | |
df7e5265 GB |
90 | static struct x86_process_info * |
91 | x86_process_info_get (pid_t pid) | |
26cb8b7c | 92 | { |
df7e5265 | 93 | struct x86_process_info *proc; |
26cb8b7c | 94 | |
df7e5265 | 95 | proc = x86_find_process_pid (pid); |
26cb8b7c | 96 | if (proc == NULL) |
df7e5265 | 97 | proc = x86_add_process (pid); |
4403d8e9 | 98 | |
26cb8b7c | 99 | return proc; |
4403d8e9 JK |
100 | } |
101 | ||
26cb8b7c | 102 | /* Get debug registers state for process PID. */ |
52b98211 | 103 | |
df7e5265 GB |
104 | struct x86_debug_reg_state * |
105 | x86_debug_reg_state (pid_t pid) | |
7b50312a | 106 | { |
df7e5265 | 107 | return &x86_process_info_get (pid)->state; |
26cb8b7c PA |
108 | } |
109 | ||
030f17b5 | 110 | /* See declaration in x86-nat.h. */ |
26cb8b7c PA |
111 | |
112 | void | |
df7e5265 | 113 | x86_forget_process (pid_t pid) |
26cb8b7c | 114 | { |
df7e5265 | 115 | struct x86_process_info *proc, **proc_link; |
26cb8b7c | 116 | |
df7e5265 GB |
117 | proc = x86_process_list; |
118 | proc_link = &x86_process_list; | |
26cb8b7c PA |
119 | |
120 | while (proc != NULL) | |
121 | { | |
122 | if (proc->pid == pid) | |
123 | { | |
124 | *proc_link = proc->next; | |
125 | ||
126 | xfree (proc); | |
127 | return; | |
128 | } | |
129 | ||
130 | proc_link = &proc->next; | |
131 | proc = *proc_link; | |
132 | } | |
7b50312a PA |
133 | } |
134 | ||
7fa2737c MK |
135 | /* Clear the reference counts and forget everything we knew about the |
136 | debug registers. */ | |
137 | ||
52b98211 | 138 | void |
df7e5265 | 139 | x86_cleanup_dregs (void) |
52b98211 | 140 | { |
26cb8b7c | 141 | /* Starting from scratch has the same effect. */ |
e99b03dc | 142 | x86_forget_process (inferior_ptid.pid ()); |
52b98211 EZ |
143 | } |
144 | ||
52b98211 EZ |
145 | /* Insert a watchpoint to watch a memory region which starts at |
146 | address ADDR and whose length is LEN bytes. Watch memory accesses | |
147 | of the type TYPE. Return 0 on success, -1 on failure. */ | |
7fa2737c | 148 | |
f6ac5f3d PA |
149 | int |
150 | x86_insert_watchpoint (CORE_ADDR addr, int len, | |
f486487f | 151 | enum target_hw_bp_type type, struct expression *cond) |
52b98211 | 152 | { |
df7e5265 | 153 | struct x86_debug_reg_state *state |
e99b03dc | 154 | = x86_debug_reg_state (inferior_ptid.pid ()); |
52b98211 | 155 | |
df7e5265 | 156 | return x86_dr_insert_watchpoint (state, type, addr, len); |
52b98211 EZ |
157 | } |
158 | ||
159 | /* Remove a watchpoint that watched the memory region which starts at | |
160 | address ADDR, whose length is LEN bytes, and for accesses of the | |
161 | type TYPE. Return 0 on success, -1 on failure. */ | |
f6ac5f3d PA |
162 | int |
163 | x86_remove_watchpoint (CORE_ADDR addr, int len, | |
f486487f | 164 | enum target_hw_bp_type type, struct expression *cond) |
52b98211 | 165 | { |
df7e5265 | 166 | struct x86_debug_reg_state *state |
e99b03dc | 167 | = x86_debug_reg_state (inferior_ptid.pid ()); |
1ced966e | 168 | |
df7e5265 | 169 | return x86_dr_remove_watchpoint (state, type, addr, len); |
52b98211 EZ |
170 | } |
171 | ||
172 | /* Return non-zero if we can watch a memory region that starts at | |
173 | address ADDR and whose length is LEN bytes. */ | |
7fa2737c | 174 | |
f6ac5f3d PA |
175 | int |
176 | x86_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len) | |
52b98211 | 177 | { |
df7e5265 | 178 | struct x86_debug_reg_state *state |
e99b03dc | 179 | = x86_debug_reg_state (inferior_ptid.pid ()); |
7fa2737c | 180 | |
df7e5265 | 181 | return x86_dr_region_ok_for_watchpoint (state, addr, len); |
52b98211 EZ |
182 | } |
183 | ||
6e62758f GB |
184 | /* If the inferior has some break/watchpoint that triggered, set the |
185 | address associated with that break/watchpoint and return non-zero. | |
4aa7a7f5 | 186 | Otherwise, return zero. */ |
7fa2737c | 187 | |
f6ac5f3d PA |
188 | int |
189 | x86_stopped_data_address (CORE_ADDR *addr_p) | |
52b98211 | 190 | { |
df7e5265 | 191 | struct x86_debug_reg_state *state |
e99b03dc | 192 | = x86_debug_reg_state (inferior_ptid.pid ()); |
52b98211 | 193 | |
df7e5265 | 194 | return x86_dr_stopped_data_address (state, addr_p); |
4aa7a7f5 JJ |
195 | } |
196 | ||
6e62758f GB |
197 | /* Return non-zero if the inferior has some watchpoint that triggered. |
198 | Otherwise return zero. */ | |
199 | ||
f6ac5f3d PA |
200 | int |
201 | x86_stopped_by_watchpoint () | |
4aa7a7f5 | 202 | { |
df7e5265 | 203 | struct x86_debug_reg_state *state |
e99b03dc | 204 | = x86_debug_reg_state (inferior_ptid.pid ()); |
46e33252 | 205 | |
df7e5265 | 206 | return x86_dr_stopped_by_watchpoint (state); |
52b98211 EZ |
207 | } |
208 | ||
0d5ed153 | 209 | /* Insert a hardware-assisted breakpoint at BP_TGT->reqstd_address. |
8181d85f | 210 | Return 0 on success, EBUSY on failure. */ |
322a8e06 | 211 | |
f6ac5f3d PA |
212 | int |
213 | x86_insert_hw_breakpoint (struct gdbarch *gdbarch, struct bp_target_info *bp_tgt) | |
52b98211 | 214 | { |
df7e5265 | 215 | struct x86_debug_reg_state *state |
e99b03dc | 216 | = x86_debug_reg_state (inferior_ptid.pid ()); |
46e33252 | 217 | |
0d5ed153 | 218 | bp_tgt->placed_address = bp_tgt->reqstd_address; |
df7e5265 GB |
219 | return x86_dr_insert_watchpoint (state, hw_execute, |
220 | bp_tgt->placed_address, 1) ? EBUSY : 0; | |
52b98211 EZ |
221 | } |
222 | ||
8181d85f DJ |
223 | /* Remove a hardware-assisted breakpoint at BP_TGT->placed_address. |
224 | Return 0 on success, -1 on failure. */ | |
7fa2737c | 225 | |
f6ac5f3d PA |
226 | int |
227 | x86_remove_hw_breakpoint (struct gdbarch *gdbarch, | |
df7e5265 | 228 | struct bp_target_info *bp_tgt) |
52b98211 | 229 | { |
df7e5265 | 230 | struct x86_debug_reg_state *state |
e99b03dc | 231 | = x86_debug_reg_state (inferior_ptid.pid ()); |
46e33252 | 232 | |
df7e5265 GB |
233 | return x86_dr_remove_watchpoint (state, hw_execute, |
234 | bp_tgt->placed_address, 1); | |
52b98211 EZ |
235 | } |
236 | ||
c03374d5 DJ |
237 | /* Returns the number of hardware watchpoints of type TYPE that we can |
238 | set. Value is positive if we can set CNT watchpoints, zero if | |
239 | setting watchpoints of type TYPE is not supported, and negative if | |
240 | CNT is more than the maximum number of watchpoints of type TYPE | |
241 | that we can support. TYPE is one of bp_hardware_watchpoint, | |
242 | bp_read_watchpoint, bp_write_watchpoint, or bp_hardware_breakpoint. | |
243 | CNT is the number of such watchpoints used so far (including this | |
244 | one). OTHERTYPE is non-zero if other types of watchpoints are | |
245 | currently enabled. | |
246 | ||
247 | We always return 1 here because we don't have enough information | |
248 | about possible overlap of addresses that they want to watch. As an | |
249 | extreme example, consider the case where all the watchpoints watch | |
250 | the same address and the same region length: then we can handle a | |
251 | virtually unlimited number of watchpoints, due to debug register | |
030f17b5 | 252 | sharing implemented via reference counts in x86-nat.c. */ |
c03374d5 | 253 | |
f6ac5f3d PA |
254 | int |
255 | x86_can_use_hw_breakpoint (enum bptype type, int cnt, int othertype) | |
c03374d5 DJ |
256 | { |
257 | return 1; | |
258 | } | |
259 | ||
12279366 JB |
260 | /* Return non-zero if the inferior has some breakpoint that triggered. |
261 | Otherwise return zero. */ | |
262 | ||
f6ac5f3d PA |
263 | int |
264 | x86_stopped_by_hw_breakpoint () | |
12279366 JB |
265 | { |
266 | struct x86_debug_reg_state *state | |
e99b03dc | 267 | = x86_debug_reg_state (inferior_ptid.pid ()); |
12279366 JB |
268 | |
269 | return x86_dr_stopped_by_hw_breakpoint (state); | |
270 | } | |
271 | ||
9bb9e8ad PM |
272 | static void |
273 | add_show_debug_regs_command (void) | |
274 | { | |
275 | /* A maintenance command to enable printing the internal DRi mirror | |
276 | variables. */ | |
277 | add_setshow_boolean_cmd ("show-debug-regs", class_maintenance, | |
c5e92cca | 278 | &show_debug_regs, _("\ |
9bb9e8ad PM |
279 | Set whether to show variables that mirror the x86 debug registers."), _("\ |
280 | Show whether to show variables that mirror the x86 debug registers."), _("\ | |
281 | Use \"on\" to enable, \"off\" to disable.\n\ | |
282 | If enabled, the debug registers values are shown when GDB inserts\n\ | |
283 | or removes a hardware breakpoint or watchpoint, and when the inferior\n\ | |
284 | triggers a breakpoint or watchpoint."), | |
285 | NULL, | |
286 | NULL, | |
287 | &maintenance_set_cmdlist, | |
288 | &maintenance_show_cmdlist); | |
289 | } | |
290 | ||
f6ac5f3d | 291 | /* See x86-nat.h. */ |
c03374d5 | 292 | |
52b98211 | 293 | void |
df7e5265 | 294 | x86_set_debug_register_length (int len) |
52b98211 | 295 | { |
9bb9e8ad | 296 | /* This function should be called only once for each native target. */ |
df7e5265 | 297 | gdb_assert (x86_dr_low.debug_register_length == 0); |
9bb9e8ad | 298 | gdb_assert (len == 4 || len == 8); |
df7e5265 | 299 | x86_dr_low.debug_register_length = len; |
9bb9e8ad | 300 | add_show_debug_regs_command (); |
52b98211 | 301 | } |