GDB (xrefs)

/tmp/gdb-8.1/gdb/x86-nat.c
Go to the documentation of this file.
1 /* Native-dependent code for x86 (i386 and x86-64).
2
3 Copyright (C) 2001-2018 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 /* Support for hardware watchpoints and breakpoints using the x86
26 debug registers.
27
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.
32
33 The functions below implement debug registers sharing by reference
34 counts, and allow to watch regions up to 16 bytes long. */
35
36 /* Low-level function vector. */
37 struct x86_dr_low_type x86_dr_low;
38
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). */
43
44 struct x86_process_info
45 {
46 /* Linked list. */
47 struct x86_process_info *next;
48
49 /* The process identifier. */
50 pid_t pid;
51
52 /* Copy of x86 hardware debug registers. */
53 struct x86_debug_reg_state state;
54 };
55
56 static struct x86_process_info *x86_process_list = NULL;
57
58 /* Find process data for process PID. */
59
60 static struct x86_process_info *
61 x86_find_process_pid (pid_t pid)
62 {
63 struct x86_process_info *proc;
64
65 for (proc = x86_process_list; proc; proc = proc->next)
66 if (proc->pid == pid)
67 return proc;
68
69 return NULL;
70 }
71
72 /* Add process data for process PID. Returns newly allocated info
73 object. */
74
75 static struct x86_process_info *
76 x86_add_process (pid_t pid)
77 {
78 struct x86_process_info *proc = XCNEW (struct x86_process_info);
79
80 proc->pid = pid;
81 proc->next = x86_process_list;
82 x86_process_list = proc;
83
84 return proc;
85 }
86
87 /* Get data specific info for process PID, creating it if necessary.
88 Never returns NULL. */
89
90 static struct x86_process_info *
91 x86_process_info_get (pid_t pid)
92 {
93 struct x86_process_info *proc;
94
95 proc = x86_find_process_pid (pid);
96 if (proc == NULL)
97 proc = x86_add_process (pid);
98
99 return proc;
100 }
101
102 /* Get debug registers state for process PID. */
103
104 struct x86_debug_reg_state *
105 x86_debug_reg_state (pid_t pid)
106 {
107 return &x86_process_info_get (pid)->state;
108 }
109
110 /* See declaration in x86-nat.h. */
111
112 void
113 x86_forget_process (pid_t pid)
114 {
115 struct x86_process_info *proc, **proc_link;
116
117 proc = x86_process_list;
118 proc_link = &x86_process_list;
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 }
133 }
134
135 /* Clear the reference counts and forget everything we knew about the
136 debug registers. */
137
138 void
139 x86_cleanup_dregs (void)
140 {
141 /* Starting from scratch has the same effect. */
142 x86_forget_process (ptid_get_pid (inferior_ptid));
143 }
144
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. */
148
149 static int
150 x86_insert_watchpoint (struct target_ops *self, CORE_ADDR addr, int len,
151 enum target_hw_bp_type type, struct expression *cond)
152 {
153 struct x86_debug_reg_state *state
154 = x86_debug_reg_state (ptid_get_pid (inferior_ptid));
155
156 return x86_dr_insert_watchpoint (state, type, addr, len);
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. */
162 static int
163 x86_remove_watchpoint (struct target_ops *self, CORE_ADDR addr, int len,
164 enum target_hw_bp_type type, struct expression *cond)
165 {
166 struct x86_debug_reg_state *state
167 = x86_debug_reg_state (ptid_get_pid (inferior_ptid));
168
169 return x86_dr_remove_watchpoint (state, type, addr, len);
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. */
174
175 static int
176 x86_region_ok_for_watchpoint (struct target_ops *self,
177 CORE_ADDR addr, int len)
178 {
179 struct x86_debug_reg_state *state
180 = x86_debug_reg_state (ptid_get_pid (inferior_ptid));
181
182 return x86_dr_region_ok_for_watchpoint (state, addr, len);
183 }
184
185 /* If the inferior has some break/watchpoint that triggered, set the
186 address associated with that break/watchpoint and return non-zero.
187 Otherwise, return zero. */
188
189 static int
190 x86_stopped_data_address (struct target_ops *ops, CORE_ADDR *addr_p)
191 {
192 struct x86_debug_reg_state *state
193 = x86_debug_reg_state (ptid_get_pid (inferior_ptid));
194
195 return x86_dr_stopped_data_address (state, addr_p);
196 }
197
198 /* Return non-zero if the inferior has some watchpoint that triggered.
199 Otherwise return zero. */
200
201 static int
202 x86_stopped_by_watchpoint (struct target_ops *ops)
203 {
204 struct x86_debug_reg_state *state
205 = x86_debug_reg_state (ptid_get_pid (inferior_ptid));
206
207 return x86_dr_stopped_by_watchpoint (state);
208 }
209
210 /* Insert a hardware-assisted breakpoint at BP_TGT->reqstd_address.
211 Return 0 on success, EBUSY on failure. */
212
213 static int
214 x86_insert_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
215 struct bp_target_info *bp_tgt)
216 {
217 struct x86_debug_reg_state *state
218 = x86_debug_reg_state (ptid_get_pid (inferior_ptid));
219
220 bp_tgt->placed_address = bp_tgt->reqstd_address;
221 return x86_dr_insert_watchpoint (state, hw_execute,
222 bp_tgt->placed_address, 1) ? EBUSY : 0;
223 }
224
225 /* Remove a hardware-assisted breakpoint at BP_TGT->placed_address.
226 Return 0 on success, -1 on failure. */
227
228 static int
229 x86_remove_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
230 struct bp_target_info *bp_tgt)
231 {
232 struct x86_debug_reg_state *state
233 = x86_debug_reg_state (ptid_get_pid (inferior_ptid));
234
235 return x86_dr_remove_watchpoint (state, hw_execute,
236 bp_tgt->placed_address, 1);
237 }
238
239 /* Returns the number of hardware watchpoints of type TYPE that we can
240 set. Value is positive if we can set CNT watchpoints, zero if
241 setting watchpoints of type TYPE is not supported, and negative if
242 CNT is more than the maximum number of watchpoints of type TYPE
243 that we can support. TYPE is one of bp_hardware_watchpoint,
244 bp_read_watchpoint, bp_write_watchpoint, or bp_hardware_breakpoint.
245 CNT is the number of such watchpoints used so far (including this
246 one). OTHERTYPE is non-zero if other types of watchpoints are
247 currently enabled.
248
249 We always return 1 here because we don't have enough information
250 about possible overlap of addresses that they want to watch. As an
251 extreme example, consider the case where all the watchpoints watch
252 the same address and the same region length: then we can handle a
253 virtually unlimited number of watchpoints, due to debug register
254 sharing implemented via reference counts in x86-nat.c. */
255
256 static int
257 x86_can_use_hw_breakpoint (struct target_ops *self,
258 enum bptype type, int cnt, int othertype)
259 {
260 return 1;
261 }
262
263 static void
264 add_show_debug_regs_command (void)
265 {
266 /* A maintenance command to enable printing the internal DRi mirror
267 variables. */
268 add_setshow_boolean_cmd ("show-debug-regs", class_maintenance,
269 &show_debug_regs, _("\
270 Set whether to show variables that mirror the x86 debug registers."), _("\
271 Show whether to show variables that mirror the x86 debug registers."), _("\
272 Use \"on\" to enable, \"off\" to disable.\n\
273 If enabled, the debug registers values are shown when GDB inserts\n\
274 or removes a hardware breakpoint or watchpoint, and when the inferior\n\
275 triggers a breakpoint or watchpoint."),
276 NULL,
277 NULL,
278 &maintenance_set_cmdlist,
279 &maintenance_show_cmdlist);
280 }
281
282 /* There are only two global functions left. */
283
284 void
285 x86_use_watchpoints (struct target_ops *t)
286 {
287 /* After a watchpoint trap, the PC points to the instruction after the
288 one that caused the trap. Therefore we don't need to step over it.
289 But we do need to reset the status register to avoid another trap. */
290 t->to_have_continuable_watchpoint = 1;
291
292 t->to_can_use_hw_breakpoint = x86_can_use_hw_breakpoint;
293 t->to_region_ok_for_hw_watchpoint = x86_region_ok_for_watchpoint;
294 t->to_stopped_by_watchpoint = x86_stopped_by_watchpoint;
295 t->to_stopped_data_address = x86_stopped_data_address;
296 t->to_insert_watchpoint = x86_insert_watchpoint;
297 t->to_remove_watchpoint = x86_remove_watchpoint;
298 t->to_insert_hw_breakpoint = x86_insert_hw_breakpoint;
299 t->to_remove_hw_breakpoint = x86_remove_hw_breakpoint;
300 }
301
302 void
303 x86_set_debug_register_length (int len)
304 {
305 /* This function should be called only once for each native target. */
306 gdb_assert (x86_dr_low.debug_register_length == 0);
307 gdb_assert (len == 4 || len == 8);
308 x86_dr_low.debug_register_length = len;
309 add_show_debug_regs_command ();
310 }
gdbarch
Definition: gdbarch.c:127
bp_target_info::reqstd_address
CORE_ADDR reqstd_address
Definition: breakpoint.h:251
CORE_ADDR
bfd_vma CORE_ADDR
Definition: common-types.h:41
ptid_get_pid
int ptid_get_pid(const ptid_t &ptid)
Definition: ptid.c:47
x86_find_process_pid
static struct x86_process_info * x86_find_process_pid(pid_t pid)
Definition: x86-nat.c:61
xfree
void xfree(void *)
pid
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t int int rusage_t pid_t pid
Definition: gnu-nat.c:1824
x86_remove_watchpoint
static int x86_remove_watchpoint(struct target_ops *self, CORE_ADDR addr, int len, enum target_hw_bp_type type, struct expression *cond)
Definition: x86-nat.c:163
x86_insert_watchpoint
static int x86_insert_watchpoint(struct target_ops *self, CORE_ADDR addr, int len, enum target_hw_bp_type type, struct expression *cond)
Definition: x86-nat.c:150
x86_process_info::next
struct x86_process_info * next
Definition: x86-nat.c:47
x86_dr_low_type
Definition: x86-dregs.h:39
x86_remove_hw_breakpoint
static int x86_remove_hw_breakpoint(struct target_ops *self, struct gdbarch *gdbarch, struct bp_target_info *bp_tgt)
Definition: x86-nat.c:229
hw_execute
Definition: break-common.h:27
x86_dr_remove_watchpoint
int x86_dr_remove_watchpoint(struct x86_debug_reg_state *state, enum target_hw_bp_type type, CORE_ADDR addr, int len)
Definition: x86-dregs.c:512
target_ops::to_remove_watchpoint
int(* to_remove_watchpoint)(struct target_ops *, CORE_ADDR, int, enum target_hw_bp_type, struct expression *) TARGET_DEFAULT_RETURN(-1)
Definition: target.h:516
target_ops::to_region_ok_for_hw_watchpoint
int(* to_region_ok_for_hw_watchpoint)(struct target_ops *, CORE_ADDR, int) TARGET_DEFAULT_FUNC(default_region_ok_for_hw_watchpoint)
Definition: target.h:543
target_ops::to_can_use_hw_breakpoint
int(* to_can_use_hw_breakpoint)(struct target_ops *, enum bptype, int, int) TARGET_DEFAULT_RETURN(0)
Definition: target.h:502
_
#define _(String)
Definition: gdb_locale.h:35
x86_dr_stopped_data_address
int x86_dr_stopped_data_address(struct x86_debug_reg_state *state, CORE_ADDR *addr_p)
Definition: x86-dregs.c:567
defs.h
maintenance_set_cmdlist
struct cmd_list_element * maintenance_set_cmdlist
Definition: maint.c:634
x86_dr_insert_watchpoint
int x86_dr_insert_watchpoint(struct x86_debug_reg_state *state, enum target_hw_bp_type type, CORE_ADDR addr, int len)
Definition: x86-dregs.c:470
x86_process_info::state
struct x86_debug_reg_state state
Definition: x86-nat.c:53
x86_dr_region_ok_for_watchpoint
int x86_dr_region_ok_for_watchpoint(struct x86_debug_reg_state *state, CORE_ADDR addr, int len)
Definition: x86-dregs.c:550
bp_target_info
Definition: breakpoint.h:238
proc
Definition: gnu-nat.h:42
x86_stopped_by_watchpoint
static int x86_stopped_by_watchpoint(struct target_ops *ops)
Definition: x86-nat.c:202
x86_process_list
static struct x86_process_info * x86_process_list
Definition: x86-nat.c:56
target_ops
Definition: target.h:412
x86_add_process
static struct x86_process_info * x86_add_process(pid_t pid)
Definition: x86-nat.c:76
expression
Definition: expression.h:78
show_debug_regs
int show_debug_regs
Definition: common-debug.c:25
inferior.h
type
Definition: gdbtypes.h:749
x86-nat.h
proc::next
struct proc * next
Definition: gnu-nat.h:73
x86_stopped_data_address
static int x86_stopped_data_address(struct target_ops *ops, CORE_ADDR *addr_p)
Definition: x86-nat.c:190
x86_process_info::pid
pid_t pid
Definition: x86-nat.c:50
target_ops::to_insert_watchpoint
int(* to_insert_watchpoint)(struct target_ops *, CORE_ADDR, int, enum target_hw_bp_type, struct expression *) TARGET_DEFAULT_RETURN(-1)
Definition: target.h:519
x86_debug_reg_state
struct x86_debug_reg_state * x86_debug_reg_state(pid_t pid)
Definition: x86-nat.c:105
bp_target_info::placed_address
CORE_ADDR placed_address
Definition: breakpoint.h:248
x86_dr_stopped_by_watchpoint
int x86_dr_stopped_by_watchpoint(struct x86_debug_reg_state *state)
Definition: x86-dregs.c:647
target_ops::to_insert_hw_breakpoint
int(* to_insert_hw_breakpoint)(struct target_ops *, struct gdbarch *, struct bp_target_info *) TARGET_DEFAULT_RETURN(-1)
Definition: target.h:507
bptype
bptype
Definition: breakpoint.h:67
gdb_assert
#define gdb_assert(expr)
Definition: gdb_assert.h:32
x86_dr_low
struct x86_dr_low_type x86_dr_low
Definition: x86-nat.c:37
x86_process_info_get
static struct x86_process_info * x86_process_info_get(pid_t pid)
Definition: x86-nat.c:91
target_ops::to_remove_hw_breakpoint
int(* to_remove_hw_breakpoint)(struct target_ops *, struct gdbarch *, struct bp_target_info *) TARGET_DEFAULT_RETURN(-1)
Definition: target.h:510
x86_forget_process
void x86_forget_process(pid_t pid)
Definition: x86-nat.c:113
XCNEW
#define XCNEW(T)
Definition: poison.h:121
x86_process_info
Definition: x86-nat.c:44
x86_region_ok_for_watchpoint
static int x86_region_ok_for_watchpoint(struct target_ops *self, CORE_ADDR addr, int len)
Definition: x86-nat.c:176
inferior_ptid
ptid_t inferior_ptid
Definition: infcmd.c:94
add_setshow_boolean_cmd
void add_setshow_boolean_cmd(const char *name, enum command_class theclass, int *var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_const_sfunc_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
Definition: cli-decode.c:569
x86_insert_hw_breakpoint
static int x86_insert_hw_breakpoint(struct target_ops *self, struct gdbarch *gdbarch, struct bp_target_info *bp_tgt)
Definition: x86-nat.c:214
target_ops::to_stopped_by_watchpoint
int(* to_stopped_by_watchpoint)(struct target_ops *) TARGET_DEFAULT_RETURN(0)
Definition: target.h:531
add_show_debug_regs_command
static void add_show_debug_regs_command(void)
Definition: x86-nat.c:264
target_hw_bp_type
target_hw_bp_type
Definition: break-common.h:22
target_ops::to_have_continuable_watchpoint
int to_have_continuable_watchpoint
Definition: target.h:534
x86_debug_reg_state
Definition: x86-dregs.h:77
x86_set_debug_register_length
void x86_set_debug_register_length(int len)
Definition: x86-nat.c:303
x86_dr_low_type::debug_register_length
int debug_register_length
Definition: x86-dregs.h:63
x86_use_watchpoints
void x86_use_watchpoints(struct target_ops *t)
Definition: x86-nat.c:285
class_maintenance
Definition: command.h:43
target_ops::to_stopped_data_address
int(* to_stopped_data_address)(struct target_ops *, CORE_ADDR *) TARGET_DEFAULT_RETURN(0)
Definition: target.h:535
x86_cleanup_dregs
void x86_cleanup_dregs(void)
Definition: x86-nat.c:139
maintenance_show_cmdlist
struct cmd_list_element * maintenance_show_cmdlist
Definition: maint.c:635
gdbcmd.h
x86_can_use_hw_breakpoint
static int x86_can_use_hw_breakpoint(struct target_ops *self, enum bptype type, int cnt, int othertype)
Definition: x86-nat.c:257