Commit | Line | Data |
---|---|---|
68cc0bfb MK |
1 | /* Native-dependent code for FreeBSD/amd64. |
2 | Copyright 2003 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of GDB. | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
18 | Foundation, Inc., 59 Temple Place - Suite 330, | |
19 | Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | #include "defs.h" | |
22 | #include "inferior.h" | |
23 | #include "regcache.h" | |
24 | ||
25 | #include "gdb_assert.h" | |
26 | #include <signal.h> | |
27 | #include <stddef.h> | |
28 | #include <sys/types.h> | |
29 | #include <sys/ptrace.h> | |
30 | #include <sys/sysctl.h> | |
31 | #include <machine/reg.h> | |
32 | ||
33 | #ifdef HAVE_SYS_PROCFS_H | |
34 | #include <sys/procfs.h> | |
35 | #endif | |
36 | ||
37 | #ifndef HAVE_GREGSET_T | |
38 | typedef struct reg gregset_t; | |
39 | #endif | |
40 | ||
41 | #ifndef HAVE_FPREGSET_T | |
42 | typedef struct fpreg fpregset_t; | |
43 | #endif | |
44 | ||
45 | #include "gregset.h" | |
46 | #include "x86-64-tdep.h" | |
47 | \f | |
48 | ||
49 | /* Offset to the gregset_t location where REG is stored. */ | |
50 | #define REG_OFFSET(reg) offsetof (gregset_t, reg) | |
51 | ||
52 | /* At reg_offset[REGNO] you'll find the offset to the gregset_t | |
53 | location where the GDB register REGNO is stored. Unsupported | |
54 | registers are marked with `-1'. */ | |
55 | static int reg_offset[] = | |
56 | { | |
57 | REG_OFFSET (r_rax), | |
58 | REG_OFFSET (r_rbx), | |
59 | REG_OFFSET (r_rcx), | |
60 | REG_OFFSET (r_rdx), | |
61 | REG_OFFSET (r_rsi), | |
62 | REG_OFFSET (r_rdi), | |
63 | REG_OFFSET (r_rbp), | |
64 | REG_OFFSET (r_rsp), | |
65 | REG_OFFSET (r_r8), | |
66 | REG_OFFSET (r_r9), | |
67 | REG_OFFSET (r_r10), | |
68 | REG_OFFSET (r_r11), | |
69 | REG_OFFSET (r_r12), | |
70 | REG_OFFSET (r_r13), | |
71 | REG_OFFSET (r_r14), | |
72 | REG_OFFSET (r_r15), | |
73 | REG_OFFSET (r_rip), | |
74 | REG_OFFSET (r_rflags), | |
75 | -1, | |
76 | -1, | |
77 | -1, | |
78 | -1 | |
79 | }; | |
80 | ||
81 | #define REG_ADDR(regset, regno) ((char *) (regset) + reg_offset[regno]) | |
82 | ||
83 | /* Macro to determine if a register is fetched with PT_GETREGS. */ | |
84 | #define GETREGS_SUPPLIES(regno) \ | |
85 | ((0 <= (regno) && (regno) < X86_64_NUM_GREGS)) | |
86 | \f | |
87 | ||
88 | /* Transfering the registers between GDB, inferiors and core files. */ | |
89 | ||
90 | /* Fill GDB's register array with the general-purpose register values | |
91 | in *GREGSETP. */ | |
92 | ||
93 | void | |
94 | supply_gregset (gregset_t *gregsetp) | |
95 | { | |
96 | int i; | |
97 | ||
98 | for (i = 0; i < X86_64_NUM_GREGS; i++) | |
99 | { | |
100 | if (reg_offset[i] == -1) | |
101 | supply_register (i, NULL); | |
102 | else | |
103 | supply_register (i, REG_ADDR (gregsetp, i)); | |
104 | } | |
105 | } | |
106 | ||
107 | /* Fill register REGNO (if it is a general-purpose register) in | |
108 | *GREGSETPS with the value in GDB's register array. If REGNO is -1, | |
109 | do this for all registers. */ | |
110 | ||
111 | void | |
112 | fill_gregset (gregset_t *gregsetp, int regno) | |
113 | { | |
114 | int i; | |
115 | ||
116 | for (i = 0; i < X86_64_NUM_GREGS; i++) | |
117 | if ((regno == -1 || regno == i) && reg_offset[i] != -1) | |
118 | regcache_collect (i, REG_ADDR (gregsetp, i)); | |
119 | } | |
120 | ||
121 | /* Fill GDB's register array with the floating-point register values | |
122 | in *FPREGSETP. */ | |
123 | ||
124 | void | |
125 | supply_fpregset (fpregset_t *fpregsetp) | |
126 | { | |
127 | x86_64_supply_fxsave ((char *) fpregsetp); | |
128 | } | |
129 | ||
130 | /* Fill register REGNO (if it is a floating-point register) in | |
131 | *FPREGSETP with the value in GDB's register array. If REGNO is -1, | |
132 | do this for all registers. */ | |
133 | ||
134 | void | |
135 | fill_fpregset (fpregset_t *fpregsetp, int regno) | |
136 | { | |
137 | x86_64_fill_fxsave ((char *) fpregsetp, regno); | |
138 | } | |
139 | ||
140 | /* Fetch register REGNO from the inferior. If REGNO is -1, do this | |
141 | for all registers (including the floating point registers). */ | |
142 | ||
143 | void | |
144 | fetch_inferior_registers (int regno) | |
145 | { | |
146 | if (regno == -1 || GETREGS_SUPPLIES (regno)) | |
147 | { | |
148 | gregset_t gregs; | |
149 | ||
150 | if (ptrace (PT_GETREGS, PIDGET (inferior_ptid), | |
151 | (PTRACE_ARG3_TYPE) &gregs, 0) == -1) | |
152 | perror_with_name ("Couldn't get registers"); | |
153 | ||
154 | supply_gregset (&gregs); | |
155 | if (regno != -1) | |
156 | return; | |
157 | } | |
158 | ||
159 | if (regno == -1 || regno >= FP0_REGNUM) | |
160 | { | |
161 | fpregset_t fpregs; | |
162 | ||
163 | if (ptrace (PT_GETFPREGS, PIDGET (inferior_ptid), | |
164 | (PTRACE_ARG3_TYPE) &fpregs, 0) == -1) | |
165 | perror_with_name ("Couldn't get floating point status"); | |
166 | ||
167 | supply_fpregset (&fpregs); | |
168 | } | |
169 | } | |
170 | ||
171 | /* Store register REGNO back into the inferior. If REGNO is -1, do | |
172 | this for all registers (including the floating point registers). */ | |
173 | ||
174 | void | |
175 | store_inferior_registers (int regno) | |
176 | { | |
177 | if (regno == -1 || GETREGS_SUPPLIES (regno)) | |
178 | { | |
179 | gregset_t gregs; | |
180 | ||
181 | if (ptrace (PT_GETREGS, PIDGET (inferior_ptid), | |
182 | (PTRACE_ARG3_TYPE) &gregs, 0) == -1) | |
183 | perror_with_name ("Couldn't get registers"); | |
184 | ||
185 | fill_gregset (&gregs, regno); | |
186 | ||
187 | if (ptrace (PT_SETREGS, PIDGET (inferior_ptid), | |
188 | (PTRACE_ARG3_TYPE) &gregs, 0) == -1) | |
189 | perror_with_name ("Couldn't write registers"); | |
190 | ||
191 | if (regno != -1) | |
192 | return; | |
193 | } | |
194 | ||
195 | if (regno == -1 || regno >= FP0_REGNUM) | |
196 | { | |
197 | fpregset_t fpregs; | |
198 | ||
199 | if (ptrace (PT_GETFPREGS, PIDGET (inferior_ptid), | |
200 | (PTRACE_ARG3_TYPE) &fpregs, 0) == -1) | |
201 | perror_with_name ("Couldn't get floating point status"); | |
202 | ||
203 | fill_fpregset (&fpregs, regno); | |
204 | ||
205 | if (ptrace (PT_SETFPREGS, PIDGET (inferior_ptid), | |
206 | (PTRACE_ARG3_TYPE) &fpregs, 0) == -1) | |
207 | perror_with_name ("Couldn't write floating point status"); | |
208 | } | |
209 | } | |
210 | \f | |
211 | ||
212 | /* Provide a prototype to silence -Wmissing-prototypes. */ | |
213 | void _initialize_amd64fbsd_nat (void); | |
214 | ||
215 | void | |
216 | _initialize_am64fbsd_nat (void) | |
217 | { | |
218 | int offset; | |
219 | ||
220 | /* To support the recognition of signal handlers, i386bsd-tdep.c | |
221 | hardcodes some constants. Inclusion of this file means that we | |
222 | are compiling a native debugger, which means that we can use the | |
223 | system header files and sysctl(3) to get at the relevant | |
224 | information. */ | |
225 | ||
68cc0bfb MK |
226 | #define SC_REG_OFFSET amd64fbsd_sc_reg_offset |
227 | ||
228 | /* We only check the program counter, stack pointer and frame | |
229 | pointer since these members of `struct sigcontext' are essential | |
230 | for providing backtraces. */ | |
231 | ||
232 | #define SC_RIP_OFFSET SC_REG_OFFSET[X86_64_RIP_REGNUM] | |
233 | #define SC_RSP_OFFSET SC_REG_OFFSET[X86_64_RSP_REGNUM] | |
234 | #define SC_RBP_OFFSET SC_REG_OFFSET[X86_64_RBP_REGNUM] | |
235 | ||
236 | /* Override the default value for the offset of the program counter | |
237 | in the sigcontext structure. */ | |
238 | offset = offsetof (struct sigcontext, sc_rip); | |
239 | ||
240 | if (SC_RIP_OFFSET != offset) | |
241 | { | |
242 | warning ("\ | |
243 | offsetof (struct sigcontext, sc_rip) yields %d instead of %d.\n\ | |
244 | Please report this to <bug-gdb@gnu.org>.", | |
245 | offset, SC_RIP_OFFSET); | |
246 | } | |
247 | ||
248 | SC_RIP_OFFSET = offset; | |
249 | ||
250 | /* Likewise for the stack pointer. */ | |
251 | offset = offsetof (struct sigcontext, sc_rsp); | |
252 | ||
253 | if (SC_RSP_OFFSET != offset) | |
254 | { | |
255 | warning ("\ | |
256 | offsetof (struct sigcontext, sc_rsp) yields %d instead of %d.\n\ | |
257 | Please report this to <bug-gdb@gnu.org>.", | |
258 | offset, SC_RSP_OFFSET); | |
259 | } | |
260 | ||
261 | SC_RSP_OFFSET = offset; | |
262 | ||
263 | /* And the frame pointer. */ | |
264 | offset = offsetof (struct sigcontext, sc_rbp); | |
265 | ||
266 | if (SC_RBP_OFFSET != offset) | |
267 | { | |
268 | warning ("\ | |
269 | offsetof (struct sigcontext, sc_rbp) yields %d instead of %d.\n\ | |
270 | Please report this to <bug-gdb@gnu.org>.", | |
271 | offset, SC_RBP_OFFSET); | |
272 | } | |
273 | ||
274 | SC_RBP_OFFSET = offset; | |
275 | ||
276 | /* FreeBSD provides a kern.ps_strings sysctl that we can use to | |
277 | locate the sigtramp. That way we can still recognize a sigtramp | |
278 | if its location is changed in a new kernel. Of course this is | |
279 | still based on the assumption that the sigtramp is placed | |
280 | directly under the location where the program arguments and | |
281 | environment can be found. */ | |
282 | { | |
283 | int mib[2]; | |
284 | int ps_strings; | |
285 | size_t len; | |
286 | ||
68cc0bfb MK |
287 | mib[0] = CTL_KERN; |
288 | mib[1] = KERN_PS_STRINGS; | |
289 | len = sizeof (ps_strings); | |
290 | if (sysctl (mib, 2, &ps_strings, &len, NULL, 0) == 0) | |
291 | { | |
292 | amd64fbsd_sigtramp_start = ps_strings - 32; | |
293 | amd64fbsd_sigtramp_end = ps_strings; | |
294 | } | |
295 | } | |
296 | } |