GDB (xrefs)
Loading...
Searching...
No Matches
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-2023 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#include <unordered_map>
26
27/* Support for hardware watchpoints and breakpoints using the x86
28 debug registers.
29
30 This provides several functions for inserting and removing
31 hardware-assisted breakpoints and watchpoints, testing if one or
32 more of the watchpoints triggered and at what address, checking
33 whether a given region can be watched, etc.
34
35 The functions below implement debug registers sharing by reference
36 counts, and allow to watch regions up to 16 bytes long. */
37
38/* Low-level function vector. */
40
41/* Hash table storing per-process data. We don't bind this to a
42 per-inferior registry because of targets like x86 GNU/Linux that
43 need to keep track of processes that aren't bound to any inferior
44 (e.g., fork children, checkpoints). */
45
46static std::unordered_map<pid_t,
48
49/* See x86-nat.h. */
50
53{
54 auto it = x86_debug_process_state.find (pid);
55 if (it != x86_debug_process_state.end ())
56 return &it->second;
57
58 return nullptr;
59}
60
61/* Get debug registers state for process PID. */
62
68
69/* See declaration in x86-nat.h. */
70
71void
76
77/* Clear the reference counts and forget everything we knew about the
78 debug registers. */
79
80void
82{
83 /* Starting from scratch has the same effect. */
85}
86
87/* Insert a watchpoint to watch a memory region which starts at
88 address ADDR and whose length is LEN bytes. Watch memory accesses
89 of the type TYPE. Return 0 on success, -1 on failure. */
90
91int
92x86_insert_watchpoint (CORE_ADDR addr, int len,
93 enum target_hw_bp_type type, struct expression *cond)
94{
95 struct x86_debug_reg_state *state
97
98 return x86_dr_insert_watchpoint (state, type, addr, len);
99}
100
101/* Remove a watchpoint that watched the memory region which starts at
102 address ADDR, whose length is LEN bytes, and for accesses of the
103 type TYPE. Return 0 on success, -1 on failure. */
104int
105x86_remove_watchpoint (CORE_ADDR addr, int len,
106 enum target_hw_bp_type type, struct expression *cond)
107{
108 struct x86_debug_reg_state *state
110
111 return x86_dr_remove_watchpoint (state, type, addr, len);
112}
113
114/* Return non-zero if we can watch a memory region that starts at
115 address ADDR and whose length is LEN bytes. */
116
117int
118x86_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
119{
120 struct x86_debug_reg_state *state
122
123 return x86_dr_region_ok_for_watchpoint (state, addr, len);
124}
125
126/* If the inferior has some break/watchpoint that triggered, set the
127 address associated with that break/watchpoint and return non-zero.
128 Otherwise, return zero. */
129
130int
131x86_stopped_data_address (CORE_ADDR *addr_p)
132{
133 struct x86_debug_reg_state *state
135
136 return x86_dr_stopped_data_address (state, addr_p);
137}
138
139/* Return non-zero if the inferior has some watchpoint that triggered.
140 Otherwise return zero. */
141
142int
144{
145 struct x86_debug_reg_state *state
147
148 return x86_dr_stopped_by_watchpoint (state);
149}
150
151/* Insert a hardware-assisted breakpoint at BP_TGT->reqstd_address.
152 Return 0 on success, EBUSY on failure. */
153
154int
156{
157 struct x86_debug_reg_state *state
159
160 bp_tgt->placed_address = bp_tgt->reqstd_address;
161 return x86_dr_insert_watchpoint (state, hw_execute,
162 bp_tgt->placed_address, 1) ? EBUSY : 0;
163}
164
165/* Remove a hardware-assisted breakpoint at BP_TGT->placed_address.
166 Return 0 on success, -1 on failure. */
167
168int
170 struct bp_target_info *bp_tgt)
171{
172 struct x86_debug_reg_state *state
174
175 return x86_dr_remove_watchpoint (state, hw_execute,
176 bp_tgt->placed_address, 1);
177}
178
179/* Returns the number of hardware watchpoints of type TYPE that we can
180 set. Value is positive if we can set CNT watchpoints, zero if
181 setting watchpoints of type TYPE is not supported, and negative if
182 CNT is more than the maximum number of watchpoints of type TYPE
183 that we can support. TYPE is one of bp_hardware_watchpoint,
184 bp_read_watchpoint, bp_write_watchpoint, or bp_hardware_breakpoint.
185 CNT is the number of such watchpoints used so far (including this
186 one). OTHERTYPE is non-zero if other types of watchpoints are
187 currently enabled.
188
189 We always return 1 here because we don't have enough information
190 about possible overlap of addresses that they want to watch. As an
191 extreme example, consider the case where all the watchpoints watch
192 the same address and the same region length: then we can handle a
193 virtually unlimited number of watchpoints, due to debug register
194 sharing implemented via reference counts in x86-nat.c. */
195
196int
197x86_can_use_hw_breakpoint (enum bptype type, int cnt, int othertype)
198{
199 return 1;
200}
201
202/* Return non-zero if the inferior has some breakpoint that triggered.
203 Otherwise return zero. */
204
205int
207{
208 struct x86_debug_reg_state *state
210
211 return x86_dr_stopped_by_hw_breakpoint (state);
212}
213
214static void
216{
217 /* A maintenance command to enable printing the internal DRi mirror
218 variables. */
219 add_setshow_boolean_cmd ("show-debug-regs", class_maintenance,
220 &show_debug_regs, _("\
221Set whether to show variables that mirror the x86 debug registers."), _("\
222Show whether to show variables that mirror the x86 debug registers."), _("\
223Use \"on\" to enable, \"off\" to disable.\n\
224If enabled, the debug registers values are shown when GDB inserts\n\
225or removes a hardware breakpoint or watchpoint, and when the inferior\n\
226triggers a breakpoint or watchpoint."),
227 NULL,
228 NULL,
231}
232
233/* See x86-nat.h. */
234
235void
237{
238 /* This function should be called only once for each native target. */
239 gdb_assert (x86_dr_low.debug_register_length == 0);
240 gdb_assert (len == 4 || len == 8);
243}
bptype
Definition breakpoint.h:84
struct cmd_list_element * maintenance_show_cmdlist
Definition maint.c:752
struct cmd_list_element * maintenance_set_cmdlist
Definition maint.c:751
set_show_commands add_setshow_boolean_cmd(const char *name, enum command_class theclass, bool *var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_func_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
Definition cli-decode.c:809
@ class_maintenance
Definition command.h:65
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:1791
ptid_t inferior_ptid
Definition infcmd.c:74
CORE_ADDR placed_address
Definition breakpoint.h:266
CORE_ADDR reqstd_address
Definition breakpoint.h:269
int debug_register_length
Definition x86-dregs.h:64
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:548
int x86_dr_stopped_by_hw_breakpoint(struct x86_debug_reg_state *state)
Definition x86-dregs.c:693
int x86_dr_stopped_by_watchpoint(struct x86_debug_reg_state *state)
Definition x86-dregs.c:683
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:506
int x86_dr_region_ok_for_watchpoint(struct x86_debug_reg_state *state, CORE_ADDR addr, int len)
Definition x86-dregs.c:586
int x86_dr_stopped_data_address(struct x86_debug_reg_state *state, CORE_ADDR *addr_p)
Definition x86-dregs.c:603
int x86_stopped_by_hw_breakpoint()
Definition x86-nat.c:206
static void add_show_debug_regs_command(void)
Definition x86-nat.c:215
int x86_insert_watchpoint(CORE_ADDR addr, int len, enum target_hw_bp_type type, struct expression *cond)
Definition x86-nat.c:92
static std::unordered_map< pid_t, struct x86_debug_reg_state > x86_debug_process_state
Definition x86-nat.c:47
struct x86_debug_reg_state * x86_debug_reg_state(pid_t pid)
Definition x86-nat.c:64
int x86_stopped_data_address(CORE_ADDR *addr_p)
Definition x86-nat.c:131
int x86_remove_hw_breakpoint(struct gdbarch *gdbarch, struct bp_target_info *bp_tgt)
Definition x86-nat.c:169
void x86_cleanup_dregs(void)
Definition x86-nat.c:81
void x86_forget_process(pid_t pid)
Definition x86-nat.c:72
struct x86_dr_low_type x86_dr_low
Definition x86-nat.c:39
int x86_stopped_by_watchpoint()
Definition x86-nat.c:143
int x86_insert_hw_breakpoint(struct gdbarch *gdbarch, struct bp_target_info *bp_tgt)
Definition x86-nat.c:155
int x86_can_use_hw_breakpoint(enum bptype type, int cnt, int othertype)
Definition x86-nat.c:197
int x86_region_ok_for_hw_watchpoint(CORE_ADDR addr, int len)
Definition x86-nat.c:118
void x86_set_debug_register_length(int len)
Definition x86-nat.c:236
struct x86_debug_reg_state * x86_lookup_debug_reg_state(pid_t pid)
Definition x86-nat.c:52
int x86_remove_watchpoint(CORE_ADDR addr, int len, enum target_hw_bp_type type, struct expression *cond)
Definition x86-nat.c:105