]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/x86-nat.c
x86-nat: Use an unordered_map to store per-pid debug reg state.
[thirdparty/binutils-gdb.git] / gdb / x86-nat.c
1 /* Native-dependent code for x86 (i386 and x86-64).
2
3 Copyright (C) 2001-2022 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. */
39 struct x86_dr_low_type x86_dr_low;
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
46 static std::unordered_map<pid_t,
47 struct x86_debug_reg_state> x86_debug_process_state;
48
49 /* Get debug registers state for process PID. */
50
51 struct x86_debug_reg_state *
52 x86_debug_reg_state (pid_t pid)
53 {
54 return &x86_debug_process_state[pid];
55 }
56
57 /* See declaration in x86-nat.h. */
58
59 void
60 x86_forget_process (pid_t pid)
61 {
62 x86_debug_process_state.erase (pid);
63 }
64
65 /* Clear the reference counts and forget everything we knew about the
66 debug registers. */
67
68 void
69 x86_cleanup_dregs (void)
70 {
71 /* Starting from scratch has the same effect. */
72 x86_forget_process (inferior_ptid.pid ());
73 }
74
75 /* Insert a watchpoint to watch a memory region which starts at
76 address ADDR and whose length is LEN bytes. Watch memory accesses
77 of the type TYPE. Return 0 on success, -1 on failure. */
78
79 int
80 x86_insert_watchpoint (CORE_ADDR addr, int len,
81 enum target_hw_bp_type type, struct expression *cond)
82 {
83 struct x86_debug_reg_state *state
84 = x86_debug_reg_state (inferior_ptid.pid ());
85
86 return x86_dr_insert_watchpoint (state, type, addr, len);
87 }
88
89 /* Remove a watchpoint that watched the memory region which starts at
90 address ADDR, whose length is LEN bytes, and for accesses of the
91 type TYPE. Return 0 on success, -1 on failure. */
92 int
93 x86_remove_watchpoint (CORE_ADDR addr, int len,
94 enum target_hw_bp_type type, struct expression *cond)
95 {
96 struct x86_debug_reg_state *state
97 = x86_debug_reg_state (inferior_ptid.pid ());
98
99 return x86_dr_remove_watchpoint (state, type, addr, len);
100 }
101
102 /* Return non-zero if we can watch a memory region that starts at
103 address ADDR and whose length is LEN bytes. */
104
105 int
106 x86_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
107 {
108 struct x86_debug_reg_state *state
109 = x86_debug_reg_state (inferior_ptid.pid ());
110
111 return x86_dr_region_ok_for_watchpoint (state, addr, len);
112 }
113
114 /* If the inferior has some break/watchpoint that triggered, set the
115 address associated with that break/watchpoint and return non-zero.
116 Otherwise, return zero. */
117
118 int
119 x86_stopped_data_address (CORE_ADDR *addr_p)
120 {
121 struct x86_debug_reg_state *state
122 = x86_debug_reg_state (inferior_ptid.pid ());
123
124 return x86_dr_stopped_data_address (state, addr_p);
125 }
126
127 /* Return non-zero if the inferior has some watchpoint that triggered.
128 Otherwise return zero. */
129
130 int
131 x86_stopped_by_watchpoint ()
132 {
133 struct x86_debug_reg_state *state
134 = x86_debug_reg_state (inferior_ptid.pid ());
135
136 return x86_dr_stopped_by_watchpoint (state);
137 }
138
139 /* Insert a hardware-assisted breakpoint at BP_TGT->reqstd_address.
140 Return 0 on success, EBUSY on failure. */
141
142 int
143 x86_insert_hw_breakpoint (struct gdbarch *gdbarch, struct bp_target_info *bp_tgt)
144 {
145 struct x86_debug_reg_state *state
146 = x86_debug_reg_state (inferior_ptid.pid ());
147
148 bp_tgt->placed_address = bp_tgt->reqstd_address;
149 return x86_dr_insert_watchpoint (state, hw_execute,
150 bp_tgt->placed_address, 1) ? EBUSY : 0;
151 }
152
153 /* Remove a hardware-assisted breakpoint at BP_TGT->placed_address.
154 Return 0 on success, -1 on failure. */
155
156 int
157 x86_remove_hw_breakpoint (struct gdbarch *gdbarch,
158 struct bp_target_info *bp_tgt)
159 {
160 struct x86_debug_reg_state *state
161 = x86_debug_reg_state (inferior_ptid.pid ());
162
163 return x86_dr_remove_watchpoint (state, hw_execute,
164 bp_tgt->placed_address, 1);
165 }
166
167 /* Returns the number of hardware watchpoints of type TYPE that we can
168 set. Value is positive if we can set CNT watchpoints, zero if
169 setting watchpoints of type TYPE is not supported, and negative if
170 CNT is more than the maximum number of watchpoints of type TYPE
171 that we can support. TYPE is one of bp_hardware_watchpoint,
172 bp_read_watchpoint, bp_write_watchpoint, or bp_hardware_breakpoint.
173 CNT is the number of such watchpoints used so far (including this
174 one). OTHERTYPE is non-zero if other types of watchpoints are
175 currently enabled.
176
177 We always return 1 here because we don't have enough information
178 about possible overlap of addresses that they want to watch. As an
179 extreme example, consider the case where all the watchpoints watch
180 the same address and the same region length: then we can handle a
181 virtually unlimited number of watchpoints, due to debug register
182 sharing implemented via reference counts in x86-nat.c. */
183
184 int
185 x86_can_use_hw_breakpoint (enum bptype type, int cnt, int othertype)
186 {
187 return 1;
188 }
189
190 /* Return non-zero if the inferior has some breakpoint that triggered.
191 Otherwise return zero. */
192
193 int
194 x86_stopped_by_hw_breakpoint ()
195 {
196 struct x86_debug_reg_state *state
197 = x86_debug_reg_state (inferior_ptid.pid ());
198
199 return x86_dr_stopped_by_hw_breakpoint (state);
200 }
201
202 static void
203 add_show_debug_regs_command (void)
204 {
205 /* A maintenance command to enable printing the internal DRi mirror
206 variables. */
207 add_setshow_boolean_cmd ("show-debug-regs", class_maintenance,
208 &show_debug_regs, _("\
209 Set whether to show variables that mirror the x86 debug registers."), _("\
210 Show whether to show variables that mirror the x86 debug registers."), _("\
211 Use \"on\" to enable, \"off\" to disable.\n\
212 If enabled, the debug registers values are shown when GDB inserts\n\
213 or removes a hardware breakpoint or watchpoint, and when the inferior\n\
214 triggers a breakpoint or watchpoint."),
215 NULL,
216 NULL,
217 &maintenance_set_cmdlist,
218 &maintenance_show_cmdlist);
219 }
220
221 /* See x86-nat.h. */
222
223 void
224 x86_set_debug_register_length (int len)
225 {
226 /* This function should be called only once for each native target. */
227 gdb_assert (x86_dr_low.debug_register_length == 0);
228 gdb_assert (len == 4 || len == 8);
229 x86_dr_low.debug_register_length = len;
230 add_show_debug_regs_command ();
231 }