1 /* Native-dependent code for modern i386 BSD's.
2 Copyright 2000, 2001 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 /* Return nonzero if we shouldn't try to fetch register REGNO. */
102 cannot_fetch_register (int regno
)
104 return (reg_offset
[regno
] == -1);
108 /* Transfering the registers between GDB, inferiors and core files. */
110 /* Fill GDB's register array with the general-purpose register values
114 supply_gregset (gregset_t
*gregsetp
)
118 for (i
= 0; i
< NUM_GREGS
; i
++)
120 if (CANNOT_FETCH_REGISTER (i
))
121 supply_register (i
, NULL
);
123 supply_register (i
, REG_ADDR (gregsetp
, i
));
127 /* Fill register REGNO (if it is a general-purpose register) in
128 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
129 do this for all registers. */
132 fill_gregset (gregset_t
*gregsetp
, int regno
)
136 for (i
= 0; i
< NUM_GREGS
; i
++)
137 if ((regno
== -1 || regno
== i
) && ! CANNOT_STORE_REGISTER (i
))
138 memcpy (REG_ADDR (gregsetp
, i
), ®isters
[REGISTER_BYTE (i
)],
139 REGISTER_RAW_SIZE (i
));
142 #include "i387-nat.h"
144 /* Fill GDB's register array with the floating-point register values
148 supply_fpregset (fpregset_t
*fpregsetp
)
150 i387_supply_fsave ((char *) fpregsetp
);
153 /* Fill register REGNO (if it is a floating-point register) in
154 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
155 do this for all registers. */
158 fill_fpregset (fpregset_t
*fpregsetp
, int regno
)
160 i387_fill_fsave ((char *) fpregsetp
, regno
);
163 /* Fetch register REGNO from the inferior. If REGNO is -1, do this
164 for all registers (including the floating point registers). */
167 fetch_inferior_registers (int regno
)
171 if (ptrace (PT_GETREGS
, PIDGET (inferior_ptid
),
172 (PTRACE_ARG3_TYPE
) &gregs
, 0) == -1)
173 perror_with_name ("Couldn't get registers");
175 supply_gregset (&gregs
);
177 if (regno
== -1 || regno
>= FP0_REGNUM
)
181 if (ptrace (PT_GETFPREGS
, PIDGET (inferior_ptid
),
182 (PTRACE_ARG3_TYPE
) &fpregs
, 0) == -1)
183 perror_with_name ("Couldn't get floating point status");
185 supply_fpregset (&fpregs
);
189 /* Store register REGNO back into the inferior. If REGNO is -1, do
190 this for all registers (including the floating point registers). */
193 store_inferior_registers (int regno
)
197 if (ptrace (PT_GETREGS
, PIDGET (inferior_ptid
),
198 (PTRACE_ARG3_TYPE
) &gregs
, 0) == -1)
199 perror_with_name ("Couldn't get registers");
201 fill_gregset (&gregs
, regno
);
203 if (ptrace (PT_SETREGS
, PIDGET (inferior_ptid
),
204 (PTRACE_ARG3_TYPE
) &gregs
, 0) == -1)
205 perror_with_name ("Couldn't write registers");
207 if (regno
== -1 || regno
>= FP0_REGNUM
)
211 if (ptrace (PT_GETFPREGS
, PIDGET (inferior_ptid
),
212 (PTRACE_ARG3_TYPE
) &fpregs
, 0) == -1)
213 perror_with_name ("Couldn't get floating point status");
215 fill_fpregset (&fpregs
, regno
);
217 if (ptrace (PT_SETFPREGS
, PIDGET (inferior_ptid
),
218 (PTRACE_ARG3_TYPE
) &fpregs
, 0) == -1)
219 perror_with_name ("Couldn't write floating point status");
224 /* Support for debug registers. */
226 #ifdef HAVE_PT_GETDBREGS
228 /* Not all versions of FreeBSD/i386 that support the debug registers
231 #define DBREG_DRX(d, x) ((&d->dr0)[x])
235 i386bsd_dr_set (int regnum
, unsigned int value
)
239 if (ptrace (PT_GETDBREGS
, PIDGET (inferior_ptid
),
240 (PTRACE_ARG3_TYPE
) &dbregs
, 0) == -1)
241 perror_with_name ("Couldn't get debug registers");
243 /* For some mysterious reason, some of the reserved bits in the
244 debug control register get set. Mask these off, otherwise the
245 ptrace call below will fail. */
246 dbregs
.dr7
&= ~(0x0000fc00);
248 DBREG_DRX ((&dbregs
), regnum
) = value
;
250 if (ptrace (PT_SETDBREGS
, PIDGET (inferior_ptid
),
251 (PTRACE_ARG3_TYPE
) &dbregs
, 0) == -1)
252 perror_with_name ("Couldn't write debug registers");
256 i386bsd_dr_set_control (unsigned long control
)
258 i386bsd_dr_set (7, control
);
262 i386bsd_dr_set_addr (int regnum
, CORE_ADDR addr
)
264 gdb_assert (regnum
>= 0 && regnum
<= 4);
266 i386bsd_dr_set (regnum
, addr
);
270 i386bsd_dr_reset_addr (int regnum
)
272 gdb_assert (regnum
>= 0 && regnum
<= 4);
274 i386bsd_dr_set (regnum
, 0);
278 i386bsd_dr_get_status (void)
282 /* FIXME: kettenis/2001-03-31: Calling perror_with_name if the
283 ptrace call fails breaks debugging remote targets. The correct
284 way to fix this is to add the hardware breakpoint and watchpoint
285 stuff to the target vector. For now, just return zero if the
286 ptrace call fails. */
287 if (ptrace (PT_GETDBREGS
, PIDGET (inferior_ptid
),
288 (PTRACE_ARG3_TYPE
) & dbregs
, 0) == -1)
290 perror_with_name ("Couldn't read debug registers");
298 #endif /* PT_GETDBREGS */
301 /* Support for the user struct. */
303 /* Return the address register REGNO. BLOCKEND is the value of
304 u.u_ar0, which should point to the registers. */
307 register_u_addr (CORE_ADDR blockend
, int regno
)
309 return (CORE_ADDR
) REG_ADDR (blockend
, regno
);
312 #include <sys/param.h>
313 #include <sys/user.h>
315 /* Return the size of the user struct. */
320 return (sizeof (struct user
));
323 /* See i386bsd-tdep.c. */
324 extern int i386bsd_sigcontext_pc_offset
;
326 #include <sys/sysctl.h>
329 _initialize_i386bsd_nat (void)
331 /* To support the recognition of signal handlers, i386bsd-tdep.c
332 hardcodes some constants. Inclusion of this file means that we
333 are compiling a native debugger, which means that we can use the
334 system header files and sysctl(3) to get at the relevant
337 /* Override the default value for the offset of the program counter
338 in the sigcontext structure. */
339 i386bsd_sigcontext_pc_offset
= offsetof (struct sigcontext
, sc_pc
);
341 /* FreeBSD provides a kern.ps_strings sysctl that we can use to
342 locate the sigtramp. That way we can still recognize a sigtramp
343 if it's location is changed in a new kernel. Of course this is
344 still based on the assumption that the sigtramp is placed
345 directly under the location where the program arguments and
346 environment can be found. */
347 #ifdef KERN_PS_STRINGS
354 mib
[1] = KERN_PS_STRINGS
;
355 len
= sizeof (ps_strings
);
356 if (sysctl (mib
, 2, &ps_strings
, &len
, NULL
, 0) == 0)
358 i386bsd_sigtramp_start
= ps_strings
- 128;
359 i386bsd_sigtramp_end
= ps_strings
;