1 /* Native-dependent code for FreeBSD/amd64.
2 Copyright 2003 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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. */
25 #include "gdb_assert.h"
28 #include <sys/types.h>
29 #include <sys/ptrace.h>
30 #include <sys/sysctl.h>
31 #include <machine/reg.h>
33 #ifdef HAVE_SYS_PROCFS_H
34 #include <sys/procfs.h>
37 #ifndef HAVE_GREGSET_T
38 typedef struct reg gregset_t
;
41 #ifndef HAVE_FPREGSET_T
42 typedef struct fpreg fpregset_t
;
46 #include "x86-64-tdep.h"
49 /* Offset to the gregset_t location where REG is stored. */
50 #define REG_OFFSET(reg) offsetof (gregset_t, reg)
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
[] =
74 REG_OFFSET (r_rflags
),
81 #define REG_ADDR(regset, regno) ((char *) (regset) + reg_offset[regno])
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))
88 /* Transfering the registers between GDB, inferiors and core files. */
90 /* Fill GDB's register array with the general-purpose register values
94 supply_gregset (gregset_t
*gregsetp
)
98 for (i
= 0; i
< X86_64_NUM_GREGS
; i
++)
100 if (reg_offset
[i
] == -1)
101 supply_register (i
, NULL
);
103 supply_register (i
, REG_ADDR (gregsetp
, i
));
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. */
112 fill_gregset (gregset_t
*gregsetp
, int regno
)
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
));
121 /* Fill GDB's register array with the floating-point register values
125 supply_fpregset (fpregset_t
*fpregsetp
)
127 x86_64_supply_fxsave ((char *) fpregsetp
);
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. */
135 fill_fpregset (fpregset_t
*fpregsetp
, int regno
)
137 x86_64_fill_fxsave ((char *) fpregsetp
, regno
);
140 /* Fetch register REGNO from the inferior. If REGNO is -1, do this
141 for all registers (including the floating point registers). */
144 fetch_inferior_registers (int regno
)
146 if (regno
== -1 || GETREGS_SUPPLIES (regno
))
150 if (ptrace (PT_GETREGS
, PIDGET (inferior_ptid
),
151 (PTRACE_ARG3_TYPE
) &gregs
, 0) == -1)
152 perror_with_name ("Couldn't get registers");
154 supply_gregset (&gregs
);
159 if (regno
== -1 || regno
>= FP0_REGNUM
)
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");
167 supply_fpregset (&fpregs
);
171 /* Store register REGNO back into the inferior. If REGNO is -1, do
172 this for all registers (including the floating point registers). */
175 store_inferior_registers (int regno
)
177 if (regno
== -1 || GETREGS_SUPPLIES (regno
))
181 if (ptrace (PT_GETREGS
, PIDGET (inferior_ptid
),
182 (PTRACE_ARG3_TYPE
) &gregs
, 0) == -1)
183 perror_with_name ("Couldn't get registers");
185 fill_gregset (&gregs
, regno
);
187 if (ptrace (PT_SETREGS
, PIDGET (inferior_ptid
),
188 (PTRACE_ARG3_TYPE
) &gregs
, 0) == -1)
189 perror_with_name ("Couldn't write registers");
195 if (regno
== -1 || regno
>= FP0_REGNUM
)
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");
203 fill_fpregset (&fpregs
, regno
);
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");
212 /* Provide a prototype to silence -Wmissing-prototypes. */
213 void _initialize_amd64fbsd_nat (void);
216 _initialize_am64fbsd_nat (void)
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
226 extern int amd64fbsd_sc_reg_offset
[];
227 #define SC_REG_OFFSET amd64fbsd_sc_reg_offset
229 /* We only check the program counter, stack pointer and frame
230 pointer since these members of `struct sigcontext' are essential
231 for providing backtraces. */
233 #define SC_RIP_OFFSET SC_REG_OFFSET[X86_64_RIP_REGNUM]
234 #define SC_RSP_OFFSET SC_REG_OFFSET[X86_64_RSP_REGNUM]
235 #define SC_RBP_OFFSET SC_REG_OFFSET[X86_64_RBP_REGNUM]
237 /* Override the default value for the offset of the program counter
238 in the sigcontext structure. */
239 offset
= offsetof (struct sigcontext
, sc_rip
);
241 if (SC_RIP_OFFSET
!= offset
)
244 offsetof (struct sigcontext, sc_rip) yields %d instead of %d.\n\
245 Please report this to <bug-gdb@gnu.org>.",
246 offset
, SC_RIP_OFFSET
);
249 SC_RIP_OFFSET
= offset
;
251 /* Likewise for the stack pointer. */
252 offset
= offsetof (struct sigcontext
, sc_rsp
);
254 if (SC_RSP_OFFSET
!= offset
)
257 offsetof (struct sigcontext, sc_rsp) yields %d instead of %d.\n\
258 Please report this to <bug-gdb@gnu.org>.",
259 offset
, SC_RSP_OFFSET
);
262 SC_RSP_OFFSET
= offset
;
264 /* And the frame pointer. */
265 offset
= offsetof (struct sigcontext
, sc_rbp
);
267 if (SC_RBP_OFFSET
!= offset
)
270 offsetof (struct sigcontext, sc_rbp) yields %d instead of %d.\n\
271 Please report this to <bug-gdb@gnu.org>.",
272 offset
, SC_RBP_OFFSET
);
275 SC_RBP_OFFSET
= offset
;
277 /* FreeBSD provides a kern.ps_strings sysctl that we can use to
278 locate the sigtramp. That way we can still recognize a sigtramp
279 if its location is changed in a new kernel. Of course this is
280 still based on the assumption that the sigtramp is placed
281 directly under the location where the program arguments and
282 environment can be found. */
288 extern CORE_ADDR amd64fbsd_sigtramp_start
;
289 extern CORE_ADDR amd64fbsd_sigtramp_end
;
292 mib
[1] = KERN_PS_STRINGS
;
293 len
= sizeof (ps_strings
);
294 if (sysctl (mib
, 2, &ps_strings
, &len
, NULL
, 0) == 0)
296 amd64fbsd_sigtramp_start
= ps_strings
- 32;
297 amd64fbsd_sigtramp_end
= ps_strings
;