GDB (xrefs)
Loading...
Searching...
No Matches
aarch64-fbsd-nat.c
Go to the documentation of this file.
1/* Native-dependent code for FreeBSD/aarch64.
2
3 Copyright (C) 2017-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 "arch-utils.h"
22#include "inferior.h"
23#include "regcache.h"
24#include "target.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
48 : public aarch64_nat_target<fbsd_nat_target>
49#else
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
72private:
73 void probe_debug_regs (int pid);
74 static bool debug_regs_probed;
75#endif
76};
77
79
80/* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
81 for all registers. */
82
83void
98
99/* Store register REGNUM back into the inferior. If REGNUM is -1, do
100 this for all registers. */
101
102void
117
118/* Implement the target read_description method. */
119
120const struct target_desc *
122{
124 return this->beneath ()->read_description ();
125
126 aarch64_features features;
127 features.tls = have_regset (inferior_ptid, NT_ARM_TLS)? 1 : 0;
128 return aarch64_read_description (features);
129}
130
131#ifdef HAVE_DBREG
133
134/* Set of threads which need to update debug registers on next resume. */
135
136static std::unordered_set<lwpid_t> aarch64_debug_pending_threads;
137
138/* Implement the "stopped_data_address" target_ops method. */
139
140bool
142{
144 struct aarch64_debug_reg_state *state;
145
147 return false;
148
149 /* This must be a hardware breakpoint. */
150 if (siginfo.si_signo != SIGTRAP
151 || siginfo.si_code != TRAP_TRACE
152 || siginfo.si_trapno != EXCP_WATCHPT_EL0)
153 return false;
154
155 const CORE_ADDR addr_trap = (CORE_ADDR) siginfo.si_addr;
156
157 /* Check if the address matches any watched address. */
160}
161
162/* Implement the "stopped_by_watchpoint" target_ops method. */
163
164bool
166{
167 CORE_ADDR addr;
168
169 return stopped_data_address (&addr);
170}
171
172/* Implement the "stopped_by_hw_breakpoint" target_ops method. */
173
174bool
176{
178 struct aarch64_debug_reg_state *state;
179
181 return false;
182
183 /* This must be a hardware breakpoint. */
184 if (siginfo.si_signo != SIGTRAP
185 || siginfo.si_code != TRAP_TRACE
186 || siginfo.si_trapno != EXCP_WATCHPT_EL0)
187 return false;
188
189 return !stopped_by_watchpoint();
190}
191
192/* Implement the "supports_stopped_by_hw_breakpoint" target_ops method. */
193
194bool
196{
197 return true;
198}
199
200/* Fetch the hardware debug register capability information. */
201
202void
204{
206 {
207 struct dbreg reg;
208
209 debug_regs_probed = true;
212
213 if (ptrace(PT_GETDBREGS, pid, (PTRACE_TYPE_ARG3) &reg, 0) == 0)
214 {
215 switch (reg.db_debug_ver)
216 {
223 break;
224 default:
225 return;
226 }
227
228 aarch64_num_bp_regs = reg.db_nbkpts;
230 {
231 warning (_("Unexpected number of hardware breakpoint registers"
232 " reported by ptrace, got %d, expected %d."),
235 }
236 aarch64_num_wp_regs = reg.db_nwtpts;
238 {
239 warning (_("Unexpected number of hardware watchpoint registers"
240 " reported by ptrace, got %d, expected %d."),
243 }
244 }
245 }
246}
247
248/* Implement the virtual inf_ptrace_target::post_startup_inferior method. */
249
250void
252{
253 aarch64_remove_debug_reg_state (ptid.pid ());
254 probe_debug_regs (ptid.pid ());
256}
257
258/* Implement the "post_attach" target_ops method. */
259
260void
262{
266}
267
268/* Implement the virtual fbsd_nat_target::low_new_fork method. */
269
270void
271aarch64_fbsd_nat_target::low_new_fork (ptid_t parent, pid_t child)
272{
274
275 /* If there is no parent state, no watchpoints nor breakpoints have
276 been set, so there is nothing to do. */
278 if (parent_state == nullptr)
279 return;
280
281 /* The kernel clears debug registers in the new child process after
282 fork, but GDB core assumes the child inherits the watchpoints/hw
283 breakpoints of the parent, and will remove them all from the
284 forked off process. Copy the debug registers mirrors into the
285 new process so that all breakpoints and watchpoints can be
286 removed together. */
287
290}
291
292/* Mark debug register state "dirty" for all threads belonging to the
293 current inferior. */
294
295void
297 int is_watchpoint, unsigned int idx)
298{
299 for (thread_info *tp : current_inferior ()->non_exited_threads ())
300 {
301 if (tp->ptid.lwp_p ())
302 aarch64_debug_pending_threads.emplace (tp->ptid.lwp ());
303 }
304}
305
306/* Implement the virtual fbsd_nat_target::low_delete_thread method. */
307
308void
310{
311 gdb_assert(tp->ptid.lwp_p ());
312 aarch64_debug_pending_threads.erase (tp->ptid.lwp ());
313}
314
315/* Implement the virtual fbsd_nat_target::low_prepare_to_resume method. */
316
317void
319{
320 gdb_assert(tp->ptid.lwp_p ());
321
322 if (aarch64_debug_pending_threads.erase (tp->ptid.lwp ()) == 0)
323 return;
324
325 struct aarch64_debug_reg_state *state =
327 gdb_assert(state != nullptr);
328
329 struct dbreg reg;
330 memset (&reg, 0, sizeof(reg));
331 for (int i = 0; i < aarch64_num_bp_regs; i++)
332 {
333 reg.db_breakregs[i].dbr_addr = state->dr_addr_bp[i];
334 reg.db_breakregs[i].dbr_ctrl = state->dr_ctrl_bp[i];
335 }
336 for (int i = 0; i < aarch64_num_wp_regs; i++)
337 {
338 reg.db_watchregs[i].dbw_addr = state->dr_addr_wp[i];
339 reg.db_watchregs[i].dbw_ctrl = state->dr_ctrl_wp[i];
340 }
341 if (ptrace(PT_SETDBREGS, tp->ptid.lwp (), (PTRACE_TYPE_ARG3) &reg, 0) != 0)
342 error (_("Failed to set hardware debug registers"));
343}
344#else
345/* A stub that should never be called. */
346void
348 int is_watchpoint, unsigned int idx)
349{
350 gdb_assert (true);
351}
352#endif
353
355void
static aarch64_fbsd_nat_target the_aarch64_fbsd_nat_target
void aarch64_notify_debug_reg_change(ptid_t ptid, int is_watchpoint, unsigned int idx)
void _initialize_aarch64_fbsd_nat()
const struct regset aarch64_fbsd_fpregset
const struct regset aarch64_fbsd_gregset
const struct regset aarch64_fbsd_tls_regset
int aarch64_num_bp_regs
int aarch64_num_wp_regs
#define AARCH64_HBP_MAX_NUM
#define AARCH64_DEBUG_ARCH_V8_1
#define AARCH64_DEBUG_ARCH_V8_4
#define AARCH64_DEBUG_ARCH_V8
#define AARCH64_HWP_MAX_NUM
#define AARCH64_DEBUG_ARCH_V8_9
#define AARCH64_DEBUG_ARCH_V8_2
#define AARCH64_DEBUG_ARCH_V8_8
void aarch64_initialize_hw_point()
struct aarch64_debug_reg_state * aarch64_lookup_debug_reg_state(pid_t pid)
Definition aarch64-nat.c:39
struct aarch64_debug_reg_state * aarch64_get_debug_reg_state(pid_t pid)
Definition aarch64-nat.c:51
bool aarch64_stopped_data_address(const struct aarch64_debug_reg_state *state, CORE_ADDR addr_trap, CORE_ADDR *addr_p)
void aarch64_remove_debug_reg_state(pid_t pid)
Definition aarch64-nat.c:59
int regnum
const target_desc * aarch64_read_description(const aarch64_features &features)
bool is_watchpoint(const struct breakpoint *bpt)
size_t have_regset(ptid_t ptid, int note)
Definition fbsd-nat.c:2388
bool fetch_register_set(struct regcache *regcache, int regnum, int fetch_op, const struct regset *regset, int regbase, void *regs, size_t size)
Definition fbsd-nat.c:2336
virtual void low_new_fork(ptid_t parent, pid_t child)
Definition fbsd-nat.h:128
void post_startup_inferior(ptid_t) override
Definition fbsd-nat.c:2275
virtual void low_delete_thread(thread_info *)
Definition fbsd-nat.h:132
virtual void low_prepare_to_resume(thread_info *)
Definition fbsd-nat.h:136
void post_attach(int) override
Definition fbsd-nat.c:2283
gdbarch * arch() const
Definition regcache.c:231
ptid_t ptid
Definition gdbthread.h:259
bool fbsd_nat_get_siginfo(ptid_t ptid, siginfo_t *siginfo)
Definition fbsd-nat.c:2460
#define ptrace(request, pid, addr, data)
Definition gdb_ptrace.h:141
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
void add_inf_child_target(inf_child_target *target)
Definition inf-child.c:418
ptid_t inferior_ptid
Definition infcmd.c:74
struct inferior * current_inferior(void)
Definition inferior.c:55
#define PTRACE_TYPE_ARG3
CORE_ADDR dr_addr_bp[AARCH64_HBP_MAX_NUM]
unsigned int dr_ctrl_wp[AARCH64_HWP_MAX_NUM]
CORE_ADDR dr_addr_wp[AARCH64_HWP_MAX_NUM]
unsigned int dr_ctrl_bp[AARCH64_HBP_MAX_NUM]
void store_registers(struct regcache *, int) override
void fetch_registers(struct regcache *, int) override
const struct target_desc * read_description() override
uint8_t tls
Definition aarch64.h:39
virtual bool stopped_by_watchpoint() TARGET_DEFAULT_RETURN(false)
target_ops * beneath() const
Definition target.c:3041
virtual bool stopped_data_address(CORE_ADDR *) TARGET_DEFAULT_RETURN(false)
virtual bool stopped_by_hw_breakpoint() TARGET_DEFAULT_RETURN(false)
virtual bool supports_stopped_by_hw_breakpoint() TARGET_DEFAULT_RETURN(false)
virtual const struct target_desc * read_description() TARGET_DEFAULT_RETURN(NULL)