Fix building Loongarch BFD with a 32-bit compiler
[binutils-gdb.git] / gdb / x86-nat.c
blob84c835f8a30b79d12e223c0f5799b3ea52fca0d2
1 /* Native-dependent code for x86 (i386 and x86-64).
3 Copyright (C) 2001-2024 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/>. */
20 #include "x86-nat.h"
21 #include "cli/cli-cmds.h"
22 #include "inferior.h"
24 #include <unordered_map>
26 /* Support for hardware watchpoints and breakpoints using the x86
27 debug registers.
29 This provides several functions for inserting and removing
30 hardware-assisted breakpoints and watchpoints, testing if one or
31 more of the watchpoints triggered and at what address, checking
32 whether a given region can be watched, etc.
34 The functions below implement debug registers sharing by reference
35 counts, and allow to watch regions up to 16 bytes long. */
37 /* Low-level function vector. */
38 struct x86_dr_low_type x86_dr_low;
40 /* Hash table storing per-process data. We don't bind this to a
41 per-inferior registry because of targets like x86 GNU/Linux that
42 need to keep track of processes that aren't bound to any inferior
43 (e.g., fork children, checkpoints). */
45 static std::unordered_map<pid_t,
46 struct x86_debug_reg_state> x86_debug_process_state;
48 /* See x86-nat.h. */
50 struct x86_debug_reg_state *
51 x86_lookup_debug_reg_state (pid_t pid)
53 auto it = x86_debug_process_state.find (pid);
54 if (it != x86_debug_process_state.end ())
55 return &it->second;
57 return nullptr;
60 /* Get debug registers state for process PID. */
62 struct x86_debug_reg_state *
63 x86_debug_reg_state (pid_t pid)
65 return &x86_debug_process_state[pid];
68 /* See declaration in x86-nat.h. */
70 void
71 x86_forget_process (pid_t pid)
73 x86_debug_process_state.erase (pid);
76 /* Clear the reference counts and forget everything we knew about the
77 debug registers. */
79 void
80 x86_cleanup_dregs (void)
82 /* Starting from scratch has the same effect. */
83 x86_forget_process (inferior_ptid.pid ());
86 /* Insert a watchpoint to watch a memory region which starts at
87 address ADDR and whose length is LEN bytes. Watch memory accesses
88 of the type TYPE. Return 0 on success, -1 on failure. */
90 int
91 x86_insert_watchpoint (CORE_ADDR addr, int len,
92 enum target_hw_bp_type type, struct expression *cond)
94 struct x86_debug_reg_state *state
95 = x86_debug_reg_state (inferior_ptid.pid ());
97 return x86_dr_insert_watchpoint (state, type, addr, len);
100 /* Remove a watchpoint that watched the memory region which starts at
101 address ADDR, whose length is LEN bytes, and for accesses of the
102 type TYPE. Return 0 on success, -1 on failure. */
104 x86_remove_watchpoint (CORE_ADDR addr, int len,
105 enum target_hw_bp_type type, struct expression *cond)
107 struct x86_debug_reg_state *state
108 = x86_debug_reg_state (inferior_ptid.pid ());
110 return x86_dr_remove_watchpoint (state, type, addr, len);
113 /* Return non-zero if we can watch a memory region that starts at
114 address ADDR and whose length is LEN bytes. */
117 x86_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
119 struct x86_debug_reg_state *state
120 = x86_debug_reg_state (inferior_ptid.pid ());
122 return x86_dr_region_ok_for_watchpoint (state, addr, len);
125 /* If the inferior has some break/watchpoint that triggered, set the
126 address associated with that break/watchpoint and return non-zero.
127 Otherwise, return zero. */
130 x86_stopped_data_address (CORE_ADDR *addr_p)
132 struct x86_debug_reg_state *state
133 = x86_debug_reg_state (inferior_ptid.pid ());
135 return x86_dr_stopped_data_address (state, addr_p);
138 /* Return non-zero if the inferior has some watchpoint that triggered.
139 Otherwise return zero. */
142 x86_stopped_by_watchpoint ()
144 struct x86_debug_reg_state *state
145 = x86_debug_reg_state (inferior_ptid.pid ());
147 return x86_dr_stopped_by_watchpoint (state);
150 /* Insert a hardware-assisted breakpoint at BP_TGT->reqstd_address.
151 Return 0 on success, EBUSY on failure. */
154 x86_insert_hw_breakpoint (struct gdbarch *gdbarch, struct bp_target_info *bp_tgt)
156 struct x86_debug_reg_state *state
157 = x86_debug_reg_state (inferior_ptid.pid ());
159 bp_tgt->placed_address = bp_tgt->reqstd_address;
160 return x86_dr_insert_watchpoint (state, hw_execute,
161 bp_tgt->placed_address, 1) ? EBUSY : 0;
164 /* Remove a hardware-assisted breakpoint at BP_TGT->placed_address.
165 Return 0 on success, -1 on failure. */
168 x86_remove_hw_breakpoint (struct gdbarch *gdbarch,
169 struct bp_target_info *bp_tgt)
171 struct x86_debug_reg_state *state
172 = x86_debug_reg_state (inferior_ptid.pid ());
174 return x86_dr_remove_watchpoint (state, hw_execute,
175 bp_tgt->placed_address, 1);
178 /* Returns the number of hardware watchpoints of type TYPE that we can
179 set. Value is positive if we can set CNT watchpoints, zero if
180 setting watchpoints of type TYPE is not supported, and negative if
181 CNT is more than the maximum number of watchpoints of type TYPE
182 that we can support. TYPE is one of bp_hardware_watchpoint,
183 bp_read_watchpoint, bp_write_watchpoint, or bp_hardware_breakpoint.
184 CNT is the number of such watchpoints used so far (including this
185 one). OTHERTYPE is non-zero if other types of watchpoints are
186 currently enabled.
188 We always return 1 here because we don't have enough information
189 about possible overlap of addresses that they want to watch. As an
190 extreme example, consider the case where all the watchpoints watch
191 the same address and the same region length: then we can handle a
192 virtually unlimited number of watchpoints, due to debug register
193 sharing implemented via reference counts in x86-nat.c. */
196 x86_can_use_hw_breakpoint (enum bptype type, int cnt, int othertype)
198 return 1;
201 /* Return non-zero if the inferior has some breakpoint that triggered.
202 Otherwise return zero. */
205 x86_stopped_by_hw_breakpoint ()
207 struct x86_debug_reg_state *state
208 = x86_debug_reg_state (inferior_ptid.pid ());
210 return x86_dr_stopped_by_hw_breakpoint (state);
213 static void
214 add_show_debug_regs_command (void)
216 /* A maintenance command to enable printing the internal DRi mirror
217 variables. */
218 add_setshow_boolean_cmd ("show-debug-regs", class_maintenance,
219 &show_debug_regs, _("\
220 Set whether to show variables that mirror the x86 debug registers."), _("\
221 Show whether to show variables that mirror the x86 debug registers."), _("\
222 Use \"on\" to enable, \"off\" to disable.\n\
223 If enabled, the debug registers values are shown when GDB inserts\n\
224 or removes a hardware breakpoint or watchpoint, and when the inferior\n\
225 triggers a breakpoint or watchpoint."),
226 NULL,
227 NULL,
228 &maintenance_set_cmdlist,
229 &maintenance_show_cmdlist);
232 /* See x86-nat.h. */
234 void
235 x86_set_debug_register_length (int len)
237 /* This function should be called only once for each native target. */
238 gdb_assert (x86_dr_low.debug_register_length == 0);
239 gdb_assert (len == 4 || len == 8);
240 x86_dr_low.debug_register_length = len;
241 add_show_debug_regs_command ();