1 /* Native-dependent code for modern i386 BSD's.
2 Copyright 2000, 2001, 2002 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 <machine/reg.h>
31 #include <machine/frame.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
;
48 /* In older BSD versions we cannot get at some of the segment
49 registers. FreeBSD for example didn't support the %fs and %gs
50 registers until the 3.0 release. We have autoconf checks for their
51 presence, and deal gracefully with their absence. */
53 /* Registers we shouldn't try to fetch. */
54 #if !defined (CANNOT_FETCH_REGISTER)
55 #define CANNOT_FETCH_REGISTER(regno) cannot_fetch_register (regno)
58 /* Registers we shouldn't try to store. */
59 #if !defined (CANNOT_STORE_REGISTER)
60 #define CANNOT_STORE_REGISTER(regno) cannot_fetch_register (regno)
63 /* Offset to the gregset_t location where REG is stored. */
64 #define REG_OFFSET(reg) offsetof (gregset_t, reg)
66 /* At reg_offset[REGNO] you'll find the offset to the gregset_t
67 location where the GDB register REGNO is stored. Unsupported
68 registers are marked with `-1'. */
69 static int reg_offset
[] =
80 REG_OFFSET (r_eflags
),
85 #ifdef HAVE_STRUCT_REG_R_FS
90 #ifdef HAVE_STRUCT_REG_R_GS
97 #define REG_ADDR(regset, regno) ((char *) (regset) + reg_offset[regno])
99 /* Macro to determine if a register is fetched with PT_GETREGS. */
100 #define GETREGS_SUPPLIES(regno) \
101 ((0 <= (regno) && (regno) <= 15))
103 #ifdef HAVE_PT_GETXMMREGS
104 /* Set to 1 if the kernel supports PT_GETXMMREGS. Initialized to -1
105 so that we try PT_GETXMMREGS the first time around. */
106 static int have_ptrace_xmmregs
= -1;
109 /* Return nonzero if we shouldn't try to fetch register REGNO. */
112 cannot_fetch_register (int regno
)
114 return (reg_offset
[regno
] == -1);
118 /* Transfering the registers between GDB, inferiors and core files. */
120 /* Fill GDB's register array with the general-purpose register values
124 supply_gregset (gregset_t
*gregsetp
)
128 for (i
= 0; i
< NUM_GREGS
; i
++)
130 if (CANNOT_FETCH_REGISTER (i
))
131 supply_register (i
, NULL
);
133 supply_register (i
, REG_ADDR (gregsetp
, i
));
137 /* Fill register REGNO (if it is a general-purpose register) in
138 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
139 do this for all registers. */
142 fill_gregset (gregset_t
*gregsetp
, int regno
)
146 for (i
= 0; i
< NUM_GREGS
; i
++)
147 if ((regno
== -1 || regno
== i
) && ! CANNOT_STORE_REGISTER (i
))
148 regcache_collect (i
, REG_ADDR (gregsetp
, i
));
151 #include "i387-tdep.h"
153 /* Fill GDB's register array with the floating-point register values
157 supply_fpregset (fpregset_t
*fpregsetp
)
159 i387_supply_fsave ((char *) fpregsetp
);
162 /* Fill register REGNO (if it is a floating-point register) in
163 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
164 do this for all registers. */
167 fill_fpregset (fpregset_t
*fpregsetp
, int regno
)
169 i387_fill_fsave ((char *) fpregsetp
, regno
);
172 /* Fetch register REGNO from the inferior. If REGNO is -1, do this
173 for all registers (including the floating point registers). */
176 fetch_inferior_registers (int regno
)
179 if (regno
== -1 || GETREGS_SUPPLIES (regno
))
183 if (ptrace (PT_GETREGS
, PIDGET (inferior_ptid
),
184 (PTRACE_ARG3_TYPE
) &gregs
, 0) == -1)
185 perror_with_name ("Couldn't get registers");
187 supply_gregset (&gregs
);
192 if (regno
== -1 || regno
>= FP0_REGNUM
)
195 #ifdef HAVE_PT_GETXMMREGS
198 if (have_ptrace_xmmregs
!= 0 &&
199 ptrace(PT_GETXMMREGS
, PIDGET (inferior_ptid
),
200 (PTRACE_ARG3_TYPE
) xmmregs
, 0) == 0)
202 have_ptrace_xmmregs
= 1;
203 i387_supply_fxsave (xmmregs
);
207 if (ptrace (PT_GETFPREGS
, PIDGET (inferior_ptid
),
208 (PTRACE_ARG3_TYPE
) &fpregs
, 0) == -1)
209 perror_with_name ("Couldn't get floating point status");
211 supply_fpregset (&fpregs
);
214 if (ptrace (PT_GETFPREGS
, PIDGET (inferior_ptid
),
215 (PTRACE_ARG3_TYPE
) &fpregs
, 0) == -1)
216 perror_with_name ("Couldn't get floating point status");
218 supply_fpregset (&fpregs
);
223 /* Store register REGNO back into the inferior. If REGNO is -1, do
224 this for all registers (including the floating point registers). */
227 store_inferior_registers (int regno
)
230 if (regno
== -1 || GETREGS_SUPPLIES (regno
))
234 if (ptrace (PT_GETREGS
, PIDGET (inferior_ptid
),
235 (PTRACE_ARG3_TYPE
) &gregs
, 0) == -1)
236 perror_with_name ("Couldn't get registers");
238 fill_gregset (&gregs
, regno
);
240 if (ptrace (PT_SETREGS
, PIDGET (inferior_ptid
),
241 (PTRACE_ARG3_TYPE
) &gregs
, 0) == -1)
242 perror_with_name ("Couldn't write registers");
248 if (regno
== -1 || regno
>= FP0_REGNUM
)
251 #ifdef HAVE_PT_GETXMMREGS
254 if (have_ptrace_xmmregs
!= 0 &&
255 ptrace(PT_GETXMMREGS
, PIDGET (inferior_ptid
),
256 (PTRACE_ARG3_TYPE
) xmmregs
, 0) == 0)
258 have_ptrace_xmmregs
= 1;
260 i387_fill_fxsave (xmmregs
, regno
);
262 if (ptrace (PT_SETXMMREGS
, PIDGET (inferior_ptid
),
263 (PTRACE_ARG3_TYPE
) xmmregs
, 0) == -1)
264 perror_with_name ("Couldn't write XMM registers");
268 have_ptrace_xmmregs
= 0;
270 if (ptrace (PT_GETFPREGS
, PIDGET (inferior_ptid
),
271 (PTRACE_ARG3_TYPE
) &fpregs
, 0) == -1)
272 perror_with_name ("Couldn't get floating point status");
274 fill_fpregset (&fpregs
, regno
);
276 if (ptrace (PT_SETFPREGS
, PIDGET (inferior_ptid
),
277 (PTRACE_ARG3_TYPE
) &fpregs
, 0) == -1)
278 perror_with_name ("Couldn't write floating point status");
279 #ifdef HAVE_PT_GETXMMREGS
286 /* Support for debug registers. */
288 #ifdef HAVE_PT_GETDBREGS
290 /* Not all versions of FreeBSD/i386 that support the debug registers
293 #define DBREG_DRX(d, x) ((&d->dr0)[x])
297 i386bsd_dr_set (int regnum
, unsigned int value
)
301 if (ptrace (PT_GETDBREGS
, PIDGET (inferior_ptid
),
302 (PTRACE_ARG3_TYPE
) &dbregs
, 0) == -1)
303 perror_with_name ("Couldn't get debug registers");
305 /* For some mysterious reason, some of the reserved bits in the
306 debug control register get set. Mask these off, otherwise the
307 ptrace call below will fail. */
308 dbregs
.dr7
&= ~(0x0000fc00);
310 DBREG_DRX ((&dbregs
), regnum
) = value
;
312 if (ptrace (PT_SETDBREGS
, PIDGET (inferior_ptid
),
313 (PTRACE_ARG3_TYPE
) &dbregs
, 0) == -1)
314 perror_with_name ("Couldn't write debug registers");
318 i386bsd_dr_set_control (unsigned long control
)
320 i386bsd_dr_set (7, control
);
324 i386bsd_dr_set_addr (int regnum
, CORE_ADDR addr
)
326 gdb_assert (regnum
>= 0 && regnum
<= 4);
328 i386bsd_dr_set (regnum
, addr
);
332 i386bsd_dr_reset_addr (int regnum
)
334 gdb_assert (regnum
>= 0 && regnum
<= 4);
336 i386bsd_dr_set (regnum
, 0);
340 i386bsd_dr_get_status (void)
344 /* FIXME: kettenis/2001-03-31: Calling perror_with_name if the
345 ptrace call fails breaks debugging remote targets. The correct
346 way to fix this is to add the hardware breakpoint and watchpoint
347 stuff to the target vector. For now, just return zero if the
348 ptrace call fails. */
349 if (ptrace (PT_GETDBREGS
, PIDGET (inferior_ptid
),
350 (PTRACE_ARG3_TYPE
) & dbregs
, 0) == -1)
352 perror_with_name ("Couldn't read debug registers");
360 #endif /* PT_GETDBREGS */
363 /* Support for the user struct. */
365 /* Return the address register REGNO. BLOCKEND is the value of
366 u.u_ar0, which should point to the registers. */
369 register_u_addr (CORE_ADDR blockend
, int regno
)
371 return (CORE_ADDR
) REG_ADDR (blockend
, regno
);
374 #include <sys/param.h>
375 #include <sys/user.h>
377 /* Return the size of the user struct. */
382 return (sizeof (struct user
));
385 /* See i386bsd-tdep.c. */
386 extern int i386bsd_sigcontext_pc_offset
;
389 _initialize_i386bsd_nat (void)
391 /* To support the recognition of signal handlers, i386bsd-tdep.c
392 hardcodes some constants. Inclusion of this file means that we
393 are compiling a native debugger, which means that we can use the
394 system header files and sysctl(3) to get at the relevant
397 /* Override the default value for the offset of the program counter
398 in the sigcontext structure. */
399 i386bsd_sigcontext_pc_offset
= offsetof (struct sigcontext
, sc_pc
);