1 /* Native-dependent code for the i386.
3 Copyright (C) 2001-2014 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
25 /* Support for hardware watchpoints and breakpoints using the i386
28 This provides several functions for inserting and removing
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.
33 The functions below implement debug registers sharing by reference
34 counts, and allow to watch regions up to 16 bytes long. */
36 /* Whether or not to print the mirrored debug registers. */
39 /* Low-level function vector. */
40 struct i386_dr_low_type i386_dr_low
;
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,
47 struct i386_process_info
50 struct i386_process_info
*next
;
52 /* The process identifier. */
55 /* Copy of i386 hardware debug registers. */
56 struct i386_debug_reg_state state
;
59 static struct i386_process_info
*i386_process_list
= NULL
;
61 /* Find process data for process PID. */
63 static struct i386_process_info
*
64 i386_find_process_pid (pid_t pid
)
66 struct i386_process_info
*proc
;
68 for (proc
= i386_process_list
; proc
; proc
= proc
->next
)
75 /* Add process data for process PID. Returns newly allocated info
78 static struct i386_process_info
*
79 i386_add_process (pid_t pid
)
81 struct i386_process_info
*proc
;
83 proc
= xcalloc (1, sizeof (*proc
));
86 proc
->next
= i386_process_list
;
87 i386_process_list
= proc
;
92 /* Get data specific info for process PID, creating it if necessary.
93 Never returns NULL. */
95 static struct i386_process_info
*
96 i386_process_info_get (pid_t pid
)
98 struct i386_process_info
*proc
;
100 proc
= i386_find_process_pid (pid
);
102 proc
= i386_add_process (pid
);
107 /* Get debug registers state for process PID. */
109 struct i386_debug_reg_state
*
110 i386_debug_reg_state (pid_t pid
)
112 return &i386_process_info_get (pid
)->state
;
115 /* See declaration in i386-nat.h. */
118 i386_forget_process (pid_t pid
)
120 struct i386_process_info
*proc
, **proc_link
;
122 proc
= i386_process_list
;
123 proc_link
= &i386_process_list
;
127 if (proc
->pid
== pid
)
129 *proc_link
= proc
->next
;
135 proc_link
= &proc
->next
;
140 /* Clear the reference counts and forget everything we knew about the
144 i386_cleanup_dregs (void)
146 /* Starting from scratch has the same effect. */
147 i386_forget_process (ptid_get_pid (inferior_ptid
));
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. */
155 i386_insert_watchpoint (struct target_ops
*self
,
156 CORE_ADDR addr
, int len
, int type
,
157 struct expression
*cond
)
159 struct i386_debug_reg_state
*state
160 = i386_debug_reg_state (ptid_get_pid (inferior_ptid
));
162 return i386_dr_insert_watchpoint (state
, type
, addr
, len
);
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. */
169 i386_remove_watchpoint (struct target_ops
*self
,
170 CORE_ADDR addr
, int len
, int type
,
171 struct expression
*cond
)
173 struct i386_debug_reg_state
*state
174 = i386_debug_reg_state (ptid_get_pid (inferior_ptid
));
176 return i386_dr_remove_watchpoint (state
, type
, addr
, len
);
179 /* Return non-zero if we can watch a memory region that starts at
180 address ADDR and whose length is LEN bytes. */
183 i386_region_ok_for_watchpoint (struct target_ops
*self
,
184 CORE_ADDR addr
, int len
)
186 struct i386_debug_reg_state
*state
187 = i386_debug_reg_state (ptid_get_pid (inferior_ptid
));
189 return i386_dr_region_ok_for_watchpoint (state
, addr
, len
);
192 /* If the inferior has some break/watchpoint that triggered, set the
193 address associated with that break/watchpoint and return non-zero.
194 Otherwise, return zero. */
197 i386_stopped_data_address (struct target_ops
*ops
, CORE_ADDR
*addr_p
)
199 struct i386_debug_reg_state
*state
200 = i386_debug_reg_state (ptid_get_pid (inferior_ptid
));
202 return i386_dr_stopped_data_address (state
, addr_p
);
205 /* Return non-zero if the inferior has some watchpoint that triggered.
206 Otherwise return zero. */
209 i386_stopped_by_watchpoint (struct target_ops
*ops
)
212 return i386_stopped_data_address (ops
, &addr
);
215 /* Insert a hardware-assisted breakpoint at BP_TGT->placed_address.
216 Return 0 on success, EBUSY on failure. */
219 i386_insert_hw_breakpoint (struct target_ops
*self
, struct gdbarch
*gdbarch
,
220 struct bp_target_info
*bp_tgt
)
222 return i386_insert_watchpoint (self
, bp_tgt
->placed_address
, 1,
223 hw_execute
, NULL
) ? EBUSY
: 0;
226 /* Remove a hardware-assisted breakpoint at BP_TGT->placed_address.
227 Return 0 on success, -1 on failure. */
230 i386_remove_hw_breakpoint (struct target_ops
*self
, struct gdbarch
*gdbarch
,
231 struct bp_target_info
*bp_tgt
)
233 return i386_remove_watchpoint (self
, bp_tgt
->placed_address
, 1,
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
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
252 sharing implemented via reference counts in i386-nat.c. */
255 i386_can_use_hw_breakpoint (struct target_ops
*self
,
256 int type
, int cnt
, int othertype
)
262 add_show_debug_regs_command (void)
264 /* A maintenance command to enable printing the internal DRi mirror
266 add_setshow_boolean_cmd ("show-debug-regs", class_maintenance
,
267 &debug_hw_points
, _("\
268 Set whether to show variables that mirror the x86 debug registers."), _("\
269 Show whether to show variables that mirror the x86 debug registers."), _("\
270 Use \"on\" to enable, \"off\" to disable.\n\
271 If enabled, the debug registers values are shown when GDB inserts\n\
272 or removes a hardware breakpoint or watchpoint, and when the inferior\n\
273 triggers a breakpoint or watchpoint."),
276 &maintenance_set_cmdlist
,
277 &maintenance_show_cmdlist
);
280 /* There are only two global functions left. */
283 i386_use_watchpoints (struct target_ops
*t
)
285 /* After a watchpoint trap, the PC points to the instruction after the
286 one that caused the trap. Therefore we don't need to step over it.
287 But we do need to reset the status register to avoid another trap. */
288 t
->to_have_continuable_watchpoint
= 1;
290 t
->to_can_use_hw_breakpoint
= i386_can_use_hw_breakpoint
;
291 t
->to_region_ok_for_hw_watchpoint
= i386_region_ok_for_watchpoint
;
292 t
->to_stopped_by_watchpoint
= i386_stopped_by_watchpoint
;
293 t
->to_stopped_data_address
= i386_stopped_data_address
;
294 t
->to_insert_watchpoint
= i386_insert_watchpoint
;
295 t
->to_remove_watchpoint
= i386_remove_watchpoint
;
296 t
->to_insert_hw_breakpoint
= i386_insert_hw_breakpoint
;
297 t
->to_remove_hw_breakpoint
= i386_remove_hw_breakpoint
;
301 i386_set_debug_register_length (int len
)
303 /* This function should be called only once for each native target. */
304 gdb_assert (i386_dr_low
.debug_register_length
== 0);
305 gdb_assert (len
== 4 || len
== 8);
306 i386_dr_low
.debug_register_length
= len
;
307 add_show_debug_regs_command ();