1 /* Native-dependent code for FreeBSD/amd64.
3 Copyright (C) 2003-2019 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/>. */
22 /* Standard C includes. */
23 #include <machine/reg.h>
25 #include <sys/ptrace.h>
26 #include <sys/sysctl.h>
27 #include <sys/types.h>
30 /* Local non-gdb includes. */
31 #include "amd64-bsd-nat.h"
32 #include "amd64-nat.h"
33 #include "amd64-tdep.h"
34 #include "common/x86-xstate.h"
43 class amd64_fbsd_nat_target final
44 : public amd64_bsd_nat_target
<fbsd_nat_target
>
47 /* Add some extra features to the common *BSD/amd64 target. */
48 const struct target_desc
*read_description () override
;
50 #if defined(HAVE_PT_GETDBREGS) && defined(USE_SIGTRAP_SIGINFO)
51 bool supports_stopped_by_hw_breakpoint () override
;
55 static amd64_fbsd_nat_target the_amd64_fbsd_nat_target
;
57 /* Offset in `struct reg' where MEMBER is stored. */
58 #define REG_OFFSET(member) offsetof (struct reg, member)
60 /* At amd64fbsd64_r_reg_offset[REGNUM] you'll find the offset in
61 `struct reg' location where the GDB register REGNUM is stored.
62 Unsupported registers are marked with `-1'. */
63 static int amd64fbsd64_r_reg_offset
[] =
82 REG_OFFSET (r_rflags
),
92 /* Mapping between the general-purpose registers in FreeBSD/amd64
93 `struct reg' format and GDB's register cache layout for
96 Note that most FreeBSD/amd64 registers are 64-bit, while the
97 FreeBSD/i386 registers are all 32-bit, but since we're
98 little-endian we get away with that. */
100 /* From <machine/reg.h>. */
101 static int amd64fbsd32_r_reg_offset
[I386_NUM_GREGS
] =
103 14 * 8, 13 * 8, /* %eax, %ecx */
104 12 * 8, 11 * 8, /* %edx, %ebx */
105 20 * 8, 10 * 8, /* %esp, %ebp */
106 9 * 8, 8 * 8, /* %esi, %edi */
107 17 * 8, 19 * 8, /* %eip, %eflags */
108 18 * 8, 21 * 8, /* %cs, %ss */
109 -1, -1, -1, -1 /* %ds, %es, %fs, %gs */
113 /* Support for debugging kernel virtual memory images. */
115 #include <machine/pcb.h>
116 #include <osreldate.h>
121 amd64fbsd_supply_pcb (struct regcache
*regcache
, struct pcb
*pcb
)
123 /* The following is true for FreeBSD 5.2:
125 The pcb contains %rip, %rbx, %rsp, %rbp, %r12, %r13, %r14, %r15,
126 %ds, %es, %fs and %gs. This accounts for all callee-saved
127 registers specified by the psABI and then some. Here %esp
128 contains the stack pointer at the point just after the call to
129 cpu_switch(). From this information we reconstruct the register
130 state as it would like when we just returned from cpu_switch(). */
132 /* The stack pointer shouldn't be zero. */
133 if (pcb
->pcb_rsp
== 0)
137 regcache
->raw_supply (AMD64_RIP_REGNUM
, &pcb
->pcb_rip
);
138 regcache
->raw_supply (AMD64_RBX_REGNUM
, &pcb
->pcb_rbx
);
139 regcache
->raw_supply (AMD64_RSP_REGNUM
, &pcb
->pcb_rsp
);
140 regcache
->raw_supply (AMD64_RBP_REGNUM
, &pcb
->pcb_rbp
);
141 regcache
->raw_supply (12, &pcb
->pcb_r12
);
142 regcache
->raw_supply (13, &pcb
->pcb_r13
);
143 regcache
->raw_supply (14, &pcb
->pcb_r14
);
144 regcache
->raw_supply (15, &pcb
->pcb_r15
);
145 #if (__FreeBSD_version < 800075) && (__FreeBSD_kernel_version < 800075)
146 /* struct pcb provides the pcb_ds/pcb_es/pcb_fs/pcb_gs fields only
147 up until __FreeBSD_version 800074: The removal of these fields
148 occurred on 2009-04-01 while the __FreeBSD_version number was
149 bumped to 800075 on 2009-04-06. So 800075 is the closest version
150 number where we should not try to access these fields. */
151 regcache
->raw_supply (AMD64_DS_REGNUM
, &pcb
->pcb_ds
);
152 regcache
->raw_supply (AMD64_ES_REGNUM
, &pcb
->pcb_es
);
153 regcache
->raw_supply (AMD64_FS_REGNUM
, &pcb
->pcb_fs
);
154 regcache
->raw_supply (AMD64_GS_REGNUM
, &pcb
->pcb_gs
);
161 /* Implement the read_description method. */
163 const struct target_desc
*
164 amd64_fbsd_nat_target::read_description ()
166 #ifdef PT_GETXSTATE_INFO
167 static int xsave_probed
;
168 static uint64_t xcr0
;
173 if (ptrace (PT_GETREGS
, inferior_ptid
.pid (),
174 (PTRACE_TYPE_ARG3
) ®s
, 0) == -1)
175 perror_with_name (_("Couldn't get registers"));
176 is64
= (regs
.r_cs
== GSEL (GUCODE_SEL
, SEL_UPL
));
177 #ifdef PT_GETXSTATE_INFO
180 struct ptrace_xstate_info info
;
182 if (ptrace (PT_GETXSTATE_INFO
, inferior_ptid
.pid (),
183 (PTRACE_TYPE_ARG3
) &info
, sizeof (info
)) == 0)
185 x86bsd_xsave_len
= info
.xsave_len
;
186 xcr0
= info
.xsave_mask
;
191 if (x86bsd_xsave_len
!= 0)
194 return amd64_target_description (xcr0
, true);
196 return i386_target_description (xcr0
, true);
200 return amd64_target_description (X86_XSTATE_SSE_MASK
, true);
202 return i386_target_description (X86_XSTATE_SSE_MASK
, true);
205 #if defined(HAVE_PT_GETDBREGS) && defined(USE_SIGTRAP_SIGINFO)
206 /* Implement the supports_stopped_by_hw_breakpoints method. */
209 amd64_fbsd_nat_target::supports_stopped_by_hw_breakpoint ()
216 _initialize_amd64fbsd_nat (void)
220 amd64_native_gregset32_reg_offset
= amd64fbsd32_r_reg_offset
;
221 amd64_native_gregset64_reg_offset
= amd64fbsd64_r_reg_offset
;
223 add_inf_child_target (&the_amd64_fbsd_nat_target
);
225 /* Support debugging kernel virtual memory images. */
226 bsd_kvm_add_target (amd64fbsd_supply_pcb
);
228 /* To support the recognition of signal handlers, i386-bsd-tdep.c
229 hardcodes some constants. Inclusion of this file means that we
230 are compiling a native debugger, which means that we can use the
231 system header files and sysctl(3) to get at the relevant
234 #define SC_REG_OFFSET amd64fbsd_sc_reg_offset
236 /* We only check the program counter, stack pointer and frame
237 pointer since these members of `struct sigcontext' are essential
238 for providing backtraces. */
240 #define SC_RIP_OFFSET SC_REG_OFFSET[AMD64_RIP_REGNUM]
241 #define SC_RSP_OFFSET SC_REG_OFFSET[AMD64_RSP_REGNUM]
242 #define SC_RBP_OFFSET SC_REG_OFFSET[AMD64_RBP_REGNUM]
244 /* Override the default value for the offset of the program counter
245 in the sigcontext structure. */
246 offset
= offsetof (struct sigcontext
, sc_rip
);
248 if (SC_RIP_OFFSET
!= offset
)
251 offsetof (struct sigcontext, sc_rip) yields %d instead of %d.\n\
252 Please report this to <bug-gdb@gnu.org>."),
253 offset
, SC_RIP_OFFSET
);
256 SC_RIP_OFFSET
= offset
;
258 /* Likewise for the stack pointer. */
259 offset
= offsetof (struct sigcontext
, sc_rsp
);
261 if (SC_RSP_OFFSET
!= offset
)
264 offsetof (struct sigcontext, sc_rsp) yields %d instead of %d.\n\
265 Please report this to <bug-gdb@gnu.org>."),
266 offset
, SC_RSP_OFFSET
);
269 SC_RSP_OFFSET
= offset
;
271 /* And the frame pointer. */
272 offset
= offsetof (struct sigcontext
, sc_rbp
);
274 if (SC_RBP_OFFSET
!= offset
)
277 offsetof (struct sigcontext, sc_rbp) yields %d instead of %d.\n\
278 Please report this to <bug-gdb@gnu.org>."),
279 offset
, SC_RBP_OFFSET
);
282 SC_RBP_OFFSET
= offset
;
284 #ifdef KERN_PROC_SIGTRAMP
285 /* Normally signal frames are detected via amd64fbsd_sigtramp_p.
286 However, FreeBSD 9.2 through 10.1 do not include the page holding
287 the signal code in core dumps. These releases do provide a
288 kern.proc.sigtramp.<pid> sysctl that returns the location of the
289 signal trampoline for a running process. We fetch the location
290 of the current (gdb) process and use this to identify signal
291 frames in core dumps from these releases. Note that this only
292 works for core dumps of 64-bit (FreeBSD/amd64) processes and does
293 not handle core dumps of 32-bit (FreeBSD/i386) processes. */
296 struct kinfo_sigtramp kst
;
301 mib
[2] = KERN_PROC_SIGTRAMP
;
304 if (sysctl (mib
, 4, &kst
, &len
, NULL
, 0) == 0)
306 amd64fbsd_sigtramp_start_addr
= (uintptr_t) kst
.ksigtramp_start
;
307 amd64fbsd_sigtramp_end_addr
= (uintptr_t) kst
.ksigtramp_end
;