Disable displaced stepping if trying it fails
[deliverable/binutils-gdb.git] / gdb / x86-nat.c
CommitLineData
df7e5265 1/* Native-dependent code for x86 (i386 and x86-64).
7fa2737c 2
32d0add0 3 Copyright (C) 2001-2015 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 37struct 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 44struct 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 56static struct x86_process_info *x86_process_list = NULL;
d0d8b0c6 57
26cb8b7c
PA
58/* Find process data for process PID. */
59
df7e5265
GB
60static struct x86_process_info *
61x86_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
75static struct x86_process_info *
76x86_add_process (pid_t pid)
4403d8e9 77{
df7e5265 78 struct x86_process_info *proc;
d0d8b0c6 79
26cb8b7c
PA
80 proc = xcalloc (1, sizeof (*proc));
81 proc->pid = pid;
4403d8e9 82
df7e5265
GB
83 proc->next = x86_process_list;
84 x86_process_list = proc;
4403d8e9 85
26cb8b7c
PA
86 return proc;
87}
4403d8e9 88
26cb8b7c
PA
89/* Get data specific info for process PID, creating it if necessary.
90 Never returns NULL. */
4403d8e9 91
df7e5265
GB
92static struct x86_process_info *
93x86_process_info_get (pid_t pid)
26cb8b7c 94{
df7e5265 95 struct x86_process_info *proc;
26cb8b7c 96
df7e5265 97 proc = x86_find_process_pid (pid);
26cb8b7c 98 if (proc == NULL)
df7e5265 99 proc = x86_add_process (pid);
4403d8e9 100
26cb8b7c 101 return proc;
4403d8e9
JK
102}
103
26cb8b7c 104/* Get debug registers state for process PID. */
52b98211 105
df7e5265
GB
106struct x86_debug_reg_state *
107x86_debug_reg_state (pid_t pid)
7b50312a 108{
df7e5265 109 return &x86_process_info_get (pid)->state;
26cb8b7c
PA
110}
111
112/* See declaration in i386-nat.h. */
113
114void
df7e5265 115x86_forget_process (pid_t pid)
26cb8b7c 116{
df7e5265 117 struct x86_process_info *proc, **proc_link;
26cb8b7c 118
df7e5265
GB
119 proc = x86_process_list;
120 proc_link = &x86_process_list;
26cb8b7c
PA
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 }
7b50312a
PA
135}
136
7fa2737c
MK
137/* Clear the reference counts and forget everything we knew about the
138 debug registers. */
139
52b98211 140void
df7e5265 141x86_cleanup_dregs (void)
52b98211 142{
26cb8b7c 143 /* Starting from scratch has the same effect. */
df7e5265 144 x86_forget_process (ptid_get_pid (inferior_ptid));
52b98211
EZ
145}
146
52b98211
EZ
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. */
7fa2737c 150
9bb9e8ad 151static int
f486487f
SM
152x86_insert_watchpoint (struct target_ops *self, CORE_ADDR addr, int len,
153 enum target_hw_bp_type type, struct expression *cond)
52b98211 154{
df7e5265
GB
155 struct x86_debug_reg_state *state
156 = x86_debug_reg_state (ptid_get_pid (inferior_ptid));
52b98211 157
df7e5265 158 return x86_dr_insert_watchpoint (state, type, addr, len);
52b98211
EZ
159}
160
161/* Remove a watchpoint that watched the memory region which starts at
162 address ADDR, whose length is LEN bytes, and for accesses of the
163 type TYPE. Return 0 on success, -1 on failure. */
9bb9e8ad 164static int
f486487f
SM
165x86_remove_watchpoint (struct target_ops *self, CORE_ADDR addr, int len,
166 enum target_hw_bp_type type, struct expression *cond)
52b98211 167{
df7e5265
GB
168 struct x86_debug_reg_state *state
169 = x86_debug_reg_state (ptid_get_pid (inferior_ptid));
1ced966e 170
df7e5265 171 return x86_dr_remove_watchpoint (state, type, addr, len);
52b98211
EZ
172}
173
174/* Return non-zero if we can watch a memory region that starts at
175 address ADDR and whose length is LEN bytes. */
7fa2737c 176
9bb9e8ad 177static int
df7e5265
GB
178x86_region_ok_for_watchpoint (struct target_ops *self,
179 CORE_ADDR addr, int len)
52b98211 180{
df7e5265
GB
181 struct x86_debug_reg_state *state
182 = x86_debug_reg_state (ptid_get_pid (inferior_ptid));
7fa2737c 183
df7e5265 184 return x86_dr_region_ok_for_watchpoint (state, addr, len);
52b98211
EZ
185}
186
6e62758f
GB
187/* If the inferior has some break/watchpoint that triggered, set the
188 address associated with that break/watchpoint and return non-zero.
4aa7a7f5 189 Otherwise, return zero. */
7fa2737c 190
9bb9e8ad 191static int
df7e5265 192x86_stopped_data_address (struct target_ops *ops, CORE_ADDR *addr_p)
52b98211 193{
df7e5265
GB
194 struct x86_debug_reg_state *state
195 = x86_debug_reg_state (ptid_get_pid (inferior_ptid));
52b98211 196
df7e5265 197 return x86_dr_stopped_data_address (state, addr_p);
4aa7a7f5
JJ
198}
199
6e62758f
GB
200/* Return non-zero if the inferior has some watchpoint that triggered.
201 Otherwise return zero. */
202
9bb9e8ad 203static int
df7e5265 204x86_stopped_by_watchpoint (struct target_ops *ops)
4aa7a7f5 205{
df7e5265
GB
206 struct x86_debug_reg_state *state
207 = x86_debug_reg_state (ptid_get_pid (inferior_ptid));
46e33252 208
df7e5265 209 return x86_dr_stopped_by_watchpoint (state);
52b98211
EZ
210}
211
0d5ed153 212/* Insert a hardware-assisted breakpoint at BP_TGT->reqstd_address.
8181d85f 213 Return 0 on success, EBUSY on failure. */
322a8e06 214
9bb9e8ad 215static int
df7e5265
GB
216x86_insert_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
217 struct bp_target_info *bp_tgt)
52b98211 218{
df7e5265
GB
219 struct x86_debug_reg_state *state
220 = x86_debug_reg_state (ptid_get_pid (inferior_ptid));
46e33252 221
0d5ed153 222 bp_tgt->placed_address = bp_tgt->reqstd_address;
df7e5265
GB
223 return x86_dr_insert_watchpoint (state, hw_execute,
224 bp_tgt->placed_address, 1) ? EBUSY : 0;
52b98211
EZ
225}
226
8181d85f
DJ
227/* Remove a hardware-assisted breakpoint at BP_TGT->placed_address.
228 Return 0 on success, -1 on failure. */
7fa2737c 229
9bb9e8ad 230static int
df7e5265
GB
231x86_remove_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
232 struct bp_target_info *bp_tgt)
52b98211 233{
df7e5265
GB
234 struct x86_debug_reg_state *state
235 = x86_debug_reg_state (ptid_get_pid (inferior_ptid));
46e33252 236
df7e5265
GB
237 return x86_dr_remove_watchpoint (state, hw_execute,
238 bp_tgt->placed_address, 1);
52b98211
EZ
239}
240
c03374d5
DJ
241/* Returns the number of hardware watchpoints of type TYPE that we can
242 set. Value is positive if we can set CNT watchpoints, zero if
243 setting watchpoints of type TYPE is not supported, and negative if
244 CNT is more than the maximum number of watchpoints of type TYPE
245 that we can support. TYPE is one of bp_hardware_watchpoint,
246 bp_read_watchpoint, bp_write_watchpoint, or bp_hardware_breakpoint.
247 CNT is the number of such watchpoints used so far (including this
248 one). OTHERTYPE is non-zero if other types of watchpoints are
249 currently enabled.
250
251 We always return 1 here because we don't have enough information
252 about possible overlap of addresses that they want to watch. As an
253 extreme example, consider the case where all the watchpoints watch
254 the same address and the same region length: then we can handle a
255 virtually unlimited number of watchpoints, due to debug register
256 sharing implemented via reference counts in i386-nat.c. */
257
258static int
df7e5265 259x86_can_use_hw_breakpoint (struct target_ops *self,
f486487f 260 enum bptype type, int cnt, int othertype)
c03374d5
DJ
261{
262 return 1;
263}
264
9bb9e8ad
PM
265static void
266add_show_debug_regs_command (void)
267{
268 /* A maintenance command to enable printing the internal DRi mirror
269 variables. */
270 add_setshow_boolean_cmd ("show-debug-regs", class_maintenance,
c5e92cca 271 &show_debug_regs, _("\
9bb9e8ad
PM
272Set whether to show variables that mirror the x86 debug registers."), _("\
273Show whether to show variables that mirror the x86 debug registers."), _("\
274Use \"on\" to enable, \"off\" to disable.\n\
275If enabled, the debug registers values are shown when GDB inserts\n\
276or removes a hardware breakpoint or watchpoint, and when the inferior\n\
277triggers a breakpoint or watchpoint."),
278 NULL,
279 NULL,
280 &maintenance_set_cmdlist,
281 &maintenance_show_cmdlist);
282}
283
284/* There are only two global functions left. */
285
c03374d5 286void
df7e5265 287x86_use_watchpoints (struct target_ops *t)
c03374d5
DJ
288{
289 /* After a watchpoint trap, the PC points to the instruction after the
290 one that caused the trap. Therefore we don't need to step over it.
291 But we do need to reset the status register to avoid another trap. */
292 t->to_have_continuable_watchpoint = 1;
293
df7e5265
GB
294 t->to_can_use_hw_breakpoint = x86_can_use_hw_breakpoint;
295 t->to_region_ok_for_hw_watchpoint = x86_region_ok_for_watchpoint;
296 t->to_stopped_by_watchpoint = x86_stopped_by_watchpoint;
297 t->to_stopped_data_address = x86_stopped_data_address;
298 t->to_insert_watchpoint = x86_insert_watchpoint;
299 t->to_remove_watchpoint = x86_remove_watchpoint;
300 t->to_insert_hw_breakpoint = x86_insert_hw_breakpoint;
301 t->to_remove_hw_breakpoint = x86_remove_hw_breakpoint;
c03374d5
DJ
302}
303
52b98211 304void
df7e5265 305x86_set_debug_register_length (int len)
52b98211 306{
9bb9e8ad 307 /* This function should be called only once for each native target. */
df7e5265 308 gdb_assert (x86_dr_low.debug_register_length == 0);
9bb9e8ad 309 gdb_assert (len == 4 || len == 8);
df7e5265 310 x86_dr_low.debug_register_length = len;
9bb9e8ad 311 add_show_debug_regs_command ();
52b98211 312}
This page took 1.204881 seconds and 4 git commands to generate.