Fix potentially uninitialised variables in the Windows tools
[binutils-gdb.git] / gdb / aarch64-fbsd-nat.c
1 /* Native-dependent code for FreeBSD/aarch64.
2
3 Copyright (C) 2017-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 "arch-utils.h"
22 #include "inferior.h"
23 #include "regcache.h"
24 #include "target.h"
25 #include "nat/aarch64-hw-point.h"
26
27 #include "elf/common.h"
28
29 #include <sys/param.h>
30 #include <sys/ptrace.h>
31 #include <machine/armreg.h>
32 #include <machine/reg.h>
33
34 #include "fbsd-nat.h"
35 #include "aarch64-tdep.h"
36 #include "aarch64-fbsd-tdep.h"
37 #include "aarch64-nat.h"
38 #include "inf-ptrace.h"
39
40 #if __FreeBSD_version >= 1400005
41 #define HAVE_DBREG
42
43 #include <unordered_set>
44 #endif
45
46 #ifdef HAVE_DBREG
47 struct aarch64_fbsd_nat_target final
48 : public aarch64_nat_target<fbsd_nat_target>
49 #else
50 struct aarch64_fbsd_nat_target final : public fbsd_nat_target
51 #endif
52 {
53 void fetch_registers (struct regcache *, int) override;
54 void store_registers (struct regcache *, int) override;
55
56 const struct target_desc *read_description () override;
57
58 #ifdef HAVE_DBREG
59 /* Hardware breakpoints and watchpoints. */
60 bool stopped_by_watchpoint () override;
61 bool stopped_data_address (CORE_ADDR *) override;
62 bool stopped_by_hw_breakpoint () override;
63 bool supports_stopped_by_hw_breakpoint () override;
64
65 void post_startup_inferior (ptid_t) override;
66 void post_attach (int pid) override;
67
68 void low_new_fork (ptid_t parent, pid_t child) override;
69 void low_delete_thread (thread_info *) override;
70 void low_prepare_to_resume (thread_info *) override;
71
72 private:
73 void probe_debug_regs (int pid);
74 static bool debug_regs_probed;
75 #endif
76 };
77
78 static aarch64_fbsd_nat_target the_aarch64_fbsd_nat_target;
79 bool aarch64_fbsd_nat_target::debug_regs_probed;
80
81 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
82 for all registers. */
83
84 void
85 aarch64_fbsd_nat_target::fetch_registers (struct regcache *regcache,
86 int regnum)
87 {
88 fetch_register_set<struct reg> (regcache, regnum, PT_GETREGS,
89 &aarch64_fbsd_gregset);
90 fetch_register_set<struct fpreg> (regcache, regnum, PT_GETFPREGS,
91 &aarch64_fbsd_fpregset);
92
93 gdbarch *gdbarch = regcache->arch ();
94 aarch64_gdbarch_tdep *tdep = (aarch64_gdbarch_tdep *) gdbarch_tdep (gdbarch);
95 if (tdep->has_tls ())
96 {
97 const struct regcache_map_entry aarch64_fbsd_tls_regmap[] =
98 {
99 { 1, tdep->tls_regnum, 8 },
100 { 0 }
101 };
102
103 const struct regset aarch64_fbsd_tls_regset =
104 {
105 aarch64_fbsd_tls_regmap,
106 regcache_supply_regset, regcache_collect_regset
107 };
108
109 fetch_regset<uint64_t> (regcache, regnum, NT_ARM_TLS,
110 &aarch64_fbsd_tls_regset);
111 }
112 }
113
114 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
115 this for all registers. */
116
117 void
118 aarch64_fbsd_nat_target::store_registers (struct regcache *regcache,
119 int regnum)
120 {
121 store_register_set<struct reg> (regcache, regnum, PT_GETREGS, PT_SETREGS,
122 &aarch64_fbsd_gregset);
123 store_register_set<struct fpreg> (regcache, regnum, PT_GETFPREGS,
124 PT_SETFPREGS, &aarch64_fbsd_fpregset);
125
126 gdbarch *gdbarch = regcache->arch ();
127 aarch64_gdbarch_tdep *tdep = (aarch64_gdbarch_tdep *) gdbarch_tdep (gdbarch);
128 if (tdep->has_tls ())
129 {
130 const struct regcache_map_entry aarch64_fbsd_tls_regmap[] =
131 {
132 { 1, tdep->tls_regnum, 8 },
133 { 0 }
134 };
135
136 const struct regset aarch64_fbsd_tls_regset =
137 {
138 aarch64_fbsd_tls_regmap,
139 regcache_supply_regset, regcache_collect_regset
140 };
141
142 store_regset<uint64_t> (regcache, regnum, NT_ARM_TLS,
143 &aarch64_fbsd_tls_regset);
144 }
145 }
146
147 /* Implement the target read_description method. */
148
149 const struct target_desc *
150 aarch64_fbsd_nat_target::read_description ()
151 {
152 aarch64_features features;
153 features.tls = have_regset (inferior_ptid, NT_ARM_TLS) != 0;
154 return aarch64_read_description (features);
155 }
156
157 #ifdef HAVE_DBREG
158 /* Set of threads which need to update debug registers on next resume. */
159
160 static std::unordered_set<lwpid_t> aarch64_debug_pending_threads;
161
162 /* Implement the "stopped_data_address" target_ops method. */
163
164 bool
165 aarch64_fbsd_nat_target::stopped_data_address (CORE_ADDR *addr_p)
166 {
167 siginfo_t siginfo;
168 struct aarch64_debug_reg_state *state;
169
170 if (!fbsd_nat_get_siginfo (inferior_ptid, &siginfo))
171 return false;
172
173 /* This must be a hardware breakpoint. */
174 if (siginfo.si_signo != SIGTRAP
175 || siginfo.si_code != TRAP_TRACE
176 || siginfo.si_trapno != EXCP_WATCHPT_EL0)
177 return false;
178
179 const CORE_ADDR addr_trap = (CORE_ADDR) siginfo.si_addr;
180
181 /* Check if the address matches any watched address. */
182 state = aarch64_get_debug_reg_state (inferior_ptid.pid ());
183 return aarch64_stopped_data_address (state, addr_trap, addr_p);
184 }
185
186 /* Implement the "stopped_by_watchpoint" target_ops method. */
187
188 bool
189 aarch64_fbsd_nat_target::stopped_by_watchpoint ()
190 {
191 CORE_ADDR addr;
192
193 return stopped_data_address (&addr);
194 }
195
196 /* Implement the "stopped_by_hw_breakpoint" target_ops method. */
197
198 bool
199 aarch64_fbsd_nat_target::stopped_by_hw_breakpoint ()
200 {
201 siginfo_t siginfo;
202 struct aarch64_debug_reg_state *state;
203
204 if (!fbsd_nat_get_siginfo (inferior_ptid, &siginfo))
205 return false;
206
207 /* This must be a hardware breakpoint. */
208 if (siginfo.si_signo != SIGTRAP
209 || siginfo.si_code != TRAP_TRACE
210 || siginfo.si_trapno != EXCP_WATCHPT_EL0)
211 return false;
212
213 return !stopped_by_watchpoint();
214 }
215
216 /* Implement the "supports_stopped_by_hw_breakpoint" target_ops method. */
217
218 bool
219 aarch64_fbsd_nat_target::supports_stopped_by_hw_breakpoint ()
220 {
221 return true;
222 }
223
224 /* Fetch the hardware debug register capability information. */
225
226 void
227 aarch64_fbsd_nat_target::probe_debug_regs (int pid)
228 {
229 if (!debug_regs_probed)
230 {
231 struct dbreg reg;
232
233 debug_regs_probed = true;
234 aarch64_num_bp_regs = 0;
235 aarch64_num_wp_regs = 0;
236
237 if (ptrace(PT_GETDBREGS, pid, (PTRACE_TYPE_ARG3) &reg, 0) == 0)
238 {
239 switch (reg.db_debug_ver)
240 {
241 case AARCH64_DEBUG_ARCH_V8:
242 case AARCH64_DEBUG_ARCH_V8_1:
243 case AARCH64_DEBUG_ARCH_V8_2:
244 case AARCH64_DEBUG_ARCH_V8_4:
245 break;
246 default:
247 return;
248 }
249
250 aarch64_num_bp_regs = reg.db_nbkpts;
251 if (aarch64_num_bp_regs > AARCH64_HBP_MAX_NUM)
252 {
253 warning (_("Unexpected number of hardware breakpoint registers"
254 " reported by ptrace, got %d, expected %d."),
255 aarch64_num_bp_regs, AARCH64_HBP_MAX_NUM);
256 aarch64_num_bp_regs = AARCH64_HBP_MAX_NUM;
257 }
258 aarch64_num_wp_regs = reg.db_nwtpts;
259 if (aarch64_num_wp_regs > AARCH64_HWP_MAX_NUM)
260 {
261 warning (_("Unexpected number of hardware watchpoint registers"
262 " reported by ptrace, got %d, expected %d."),
263 aarch64_num_wp_regs, AARCH64_HWP_MAX_NUM);
264 aarch64_num_wp_regs = AARCH64_HWP_MAX_NUM;
265 }
266 }
267 }
268 }
269
270 /* Implement the virtual inf_ptrace_target::post_startup_inferior method. */
271
272 void
273 aarch64_fbsd_nat_target::post_startup_inferior (ptid_t ptid)
274 {
275 aarch64_remove_debug_reg_state (ptid.pid ());
276 probe_debug_regs (ptid.pid ());
277 fbsd_nat_target::post_startup_inferior (ptid);
278 }
279
280 /* Implement the "post_attach" target_ops method. */
281
282 void
283 aarch64_fbsd_nat_target::post_attach (int pid)
284 {
285 aarch64_remove_debug_reg_state (pid);
286 probe_debug_regs (pid);
287 fbsd_nat_target::post_attach (pid);
288 }
289
290 /* Implement the virtual fbsd_nat_target::low_new_fork method. */
291
292 void
293 aarch64_fbsd_nat_target::low_new_fork (ptid_t parent, pid_t child)
294 {
295 struct aarch64_debug_reg_state *parent_state, *child_state;
296
297 /* If there is no parent state, no watchpoints nor breakpoints have
298 been set, so there is nothing to do. */
299 parent_state = aarch64_lookup_debug_reg_state (parent.pid ());
300 if (parent_state == nullptr)
301 return;
302
303 /* The kernel clears debug registers in the new child process after
304 fork, but GDB core assumes the child inherits the watchpoints/hw
305 breakpoints of the parent, and will remove them all from the
306 forked off process. Copy the debug registers mirrors into the
307 new process so that all breakpoints and watchpoints can be
308 removed together. */
309
310 child_state = aarch64_get_debug_reg_state (child);
311 *child_state = *parent_state;
312 }
313
314 /* Mark debug register state "dirty" for all threads belonging to the
315 current inferior. */
316
317 void
318 aarch64_notify_debug_reg_change (ptid_t ptid,
319 int is_watchpoint, unsigned int idx)
320 {
321 for (thread_info *tp : current_inferior ()->non_exited_threads ())
322 {
323 if (tp->ptid.lwp_p ())
324 aarch64_debug_pending_threads.emplace (tp->ptid.lwp ());
325 }
326 }
327
328 /* Implement the virtual fbsd_nat_target::low_delete_thread method. */
329
330 void
331 aarch64_fbsd_nat_target::low_delete_thread (thread_info *tp)
332 {
333 gdb_assert(tp->ptid.lwp_p ());
334 aarch64_debug_pending_threads.erase (tp->ptid.lwp ());
335 }
336
337 /* Implement the virtual fbsd_nat_target::low_prepare_to_resume method. */
338
339 void
340 aarch64_fbsd_nat_target::low_prepare_to_resume (thread_info *tp)
341 {
342 gdb_assert(tp->ptid.lwp_p ());
343
344 if (aarch64_debug_pending_threads.erase (tp->ptid.lwp ()) == 0)
345 return;
346
347 struct aarch64_debug_reg_state *state =
348 aarch64_lookup_debug_reg_state (tp->ptid.pid ());
349 gdb_assert(state != nullptr);
350
351 struct dbreg reg;
352 memset (&reg, 0, sizeof(reg));
353 for (int i = 0; i < aarch64_num_bp_regs; i++)
354 {
355 reg.db_breakregs[i].dbr_addr = state->dr_addr_bp[i];
356 reg.db_breakregs[i].dbr_ctrl = state->dr_ctrl_bp[i];
357 }
358 for (int i = 0; i < aarch64_num_wp_regs; i++)
359 {
360 reg.db_watchregs[i].dbw_addr = state->dr_addr_wp[i];
361 reg.db_watchregs[i].dbw_ctrl = state->dr_ctrl_wp[i];
362 }
363 if (ptrace(PT_SETDBREGS, tp->ptid.lwp (), (PTRACE_TYPE_ARG3) &reg, 0) != 0)
364 error (_("Failed to set hardware debug registers"));
365 }
366 #else
367 /* A stub that should never be called. */
368 void
369 aarch64_notify_debug_reg_change (ptid_t ptid,
370 int is_watchpoint, unsigned int idx)
371 {
372 gdb_assert (true);
373 }
374 #endif
375
376 void _initialize_aarch64_fbsd_nat ();
377 void
378 _initialize_aarch64_fbsd_nat ()
379 {
380 #ifdef HAVE_DBREG
381 aarch64_initialize_hw_point ();
382 #endif
383 add_inf_child_target (&the_aarch64_fbsd_nat_target);
384 }