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