Sort includes for files gdb/[a-f]*.[chyl].
[deliverable/binutils-gdb.git] / gdb / amd64-fbsd-nat.c
1 /* Native-dependent code for FreeBSD/amd64.
2
3 Copyright (C) 2003-2019 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
22 /* Standard C includes. */
23 #include <machine/reg.h>
24 #include <signal.h>
25 #include <sys/ptrace.h>
26 #include <sys/sysctl.h>
27 #include <sys/types.h>
28 #include <sys/user.h>
29
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"
35 #include "fbsd-nat.h"
36 #include "inferior.h"
37 #include "regcache.h"
38 #include "target.h"
39 #include "x86-nat.h"
40
41 \f
42
43 class amd64_fbsd_nat_target final
44 : public amd64_bsd_nat_target<fbsd_nat_target>
45 {
46 public:
47 /* Add some extra features to the common *BSD/amd64 target. */
48 const struct target_desc *read_description () override;
49
50 #if defined(HAVE_PT_GETDBREGS) && defined(USE_SIGTRAP_SIGINFO)
51 bool supports_stopped_by_hw_breakpoint () override;
52 #endif
53 };
54
55 static amd64_fbsd_nat_target the_amd64_fbsd_nat_target;
56
57 /* Offset in `struct reg' where MEMBER is stored. */
58 #define REG_OFFSET(member) offsetof (struct reg, member)
59
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[] =
64 {
65 REG_OFFSET (r_rax),
66 REG_OFFSET (r_rbx),
67 REG_OFFSET (r_rcx),
68 REG_OFFSET (r_rdx),
69 REG_OFFSET (r_rsi),
70 REG_OFFSET (r_rdi),
71 REG_OFFSET (r_rbp),
72 REG_OFFSET (r_rsp),
73 REG_OFFSET (r_r8),
74 REG_OFFSET (r_r9),
75 REG_OFFSET (r_r10),
76 REG_OFFSET (r_r11),
77 REG_OFFSET (r_r12),
78 REG_OFFSET (r_r13),
79 REG_OFFSET (r_r14),
80 REG_OFFSET (r_r15),
81 REG_OFFSET (r_rip),
82 REG_OFFSET (r_rflags),
83 REG_OFFSET (r_cs),
84 REG_OFFSET (r_ss),
85 -1,
86 -1,
87 -1,
88 -1
89 };
90 \f
91
92 /* Mapping between the general-purpose registers in FreeBSD/amd64
93 `struct reg' format and GDB's register cache layout for
94 FreeBSD/i386.
95
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. */
99
100 /* From <machine/reg.h>. */
101 static int amd64fbsd32_r_reg_offset[I386_NUM_GREGS] =
102 {
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 */
110 };
111 \f
112
113 /* Support for debugging kernel virtual memory images. */
114
115 #include <machine/pcb.h>
116 #include <osreldate.h>
117
118 #include "bsd-kvm.h"
119
120 static int
121 amd64fbsd_supply_pcb (struct regcache *regcache, struct pcb *pcb)
122 {
123 /* The following is true for FreeBSD 5.2:
124
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(). */
131
132 /* The stack pointer shouldn't be zero. */
133 if (pcb->pcb_rsp == 0)
134 return 0;
135
136 pcb->pcb_rsp += 8;
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);
155 #endif
156
157 return 1;
158 }
159 \f
160
161 /* Implement the read_description method. */
162
163 const struct target_desc *
164 amd64_fbsd_nat_target::read_description ()
165 {
166 #ifdef PT_GETXSTATE_INFO
167 static int xsave_probed;
168 static uint64_t xcr0;
169 #endif
170 struct reg regs;
171 int is64;
172
173 if (ptrace (PT_GETREGS, inferior_ptid.pid (),
174 (PTRACE_TYPE_ARG3) &regs, 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
178 if (!xsave_probed)
179 {
180 struct ptrace_xstate_info info;
181
182 if (ptrace (PT_GETXSTATE_INFO, inferior_ptid.pid (),
183 (PTRACE_TYPE_ARG3) &info, sizeof (info)) == 0)
184 {
185 x86bsd_xsave_len = info.xsave_len;
186 xcr0 = info.xsave_mask;
187 }
188 xsave_probed = 1;
189 }
190
191 if (x86bsd_xsave_len != 0)
192 {
193 if (is64)
194 return amd64_target_description (xcr0, true);
195 else
196 return i386_target_description (xcr0, true);
197 }
198 #endif
199 if (is64)
200 return amd64_target_description (X86_XSTATE_SSE_MASK, true);
201 else
202 return i386_target_description (X86_XSTATE_SSE_MASK, true);
203 }
204
205 #if defined(HAVE_PT_GETDBREGS) && defined(USE_SIGTRAP_SIGINFO)
206 /* Implement the supports_stopped_by_hw_breakpoints method. */
207
208 bool
209 amd64_fbsd_nat_target::supports_stopped_by_hw_breakpoint ()
210 {
211 return true;
212 }
213 #endif
214
215 void
216 _initialize_amd64fbsd_nat (void)
217 {
218 int offset;
219
220 amd64_native_gregset32_reg_offset = amd64fbsd32_r_reg_offset;
221 amd64_native_gregset64_reg_offset = amd64fbsd64_r_reg_offset;
222
223 add_inf_child_target (&the_amd64_fbsd_nat_target);
224
225 /* Support debugging kernel virtual memory images. */
226 bsd_kvm_add_target (amd64fbsd_supply_pcb);
227
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
232 information. */
233
234 #define SC_REG_OFFSET amd64fbsd_sc_reg_offset
235
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. */
239
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]
243
244 /* Override the default value for the offset of the program counter
245 in the sigcontext structure. */
246 offset = offsetof (struct sigcontext, sc_rip);
247
248 if (SC_RIP_OFFSET != offset)
249 {
250 warning (_("\
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);
254 }
255
256 SC_RIP_OFFSET = offset;
257
258 /* Likewise for the stack pointer. */
259 offset = offsetof (struct sigcontext, sc_rsp);
260
261 if (SC_RSP_OFFSET != offset)
262 {
263 warning (_("\
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);
267 }
268
269 SC_RSP_OFFSET = offset;
270
271 /* And the frame pointer. */
272 offset = offsetof (struct sigcontext, sc_rbp);
273
274 if (SC_RBP_OFFSET != offset)
275 {
276 warning (_("\
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);
280 }
281
282 SC_RBP_OFFSET = offset;
283
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. */
294 {
295 int mib[4];
296 struct kinfo_sigtramp kst;
297 size_t len;
298
299 mib[0] = CTL_KERN;
300 mib[1] = KERN_PROC;
301 mib[2] = KERN_PROC_SIGTRAMP;
302 mib[3] = getpid ();
303 len = sizeof (kst);
304 if (sysctl (mib, 4, &kst, &len, NULL, 0) == 0)
305 {
306 amd64fbsd_sigtramp_start_addr = (uintptr_t) kst.ksigtramp_start;
307 amd64fbsd_sigtramp_end_addr = (uintptr_t) kst.ksigtramp_end;
308 }
309 }
310 #endif
311 }
This page took 0.040268 seconds and 4 git commands to generate.