* Contribute CGEN simulator build support code.
[binutils-gdb.git] / gdb / i386bsd-nat.c
blobf1182ea4e800b85bac2781e3b9c09a4eb83675dd
1 /* Native-dependent code for modern i386 BSD's.
2 Copyright (C) 2000 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. */
21 #include "defs.h"
22 #include "inferior.h"
24 #include <sys/types.h>
25 #include <sys/ptrace.h>
26 #include <machine/reg.h>
27 #include <machine/frame.h>
29 #ifdef HAVE_SYS_PROCFS_H
30 #include <sys/procfs.h>
31 #endif
33 #ifndef HAVE_GREGSET_T
34 typedef struct reg gregset_t;
35 #endif
37 #ifndef HAVE_FPREGSET_T
38 typedef struct fpreg fpregset_t;
39 #endif
41 #include "gregset.h"
44 /* In older BSD versions we cannot get at some of the segment
45 registers. FreeBSD for example didn't support the %fs and %gs
46 registers until the 3.0 release. We have autoconf checks for their
47 presence, and deal gracefully with their absence. */
49 /* Registers we shouldn't try to fetch. */
50 #if !defined (CANNOT_FETCH_REGISTER)
51 #define CANNOT_FETCH_REGISTER(regno) cannot_fetch_register (regno)
52 #endif
54 /* Registers we shouldn't try to store. */
55 #if !defined (CANNOT_STORE_REGISTER)
56 #define CANNOT_STORE_REGISTER(regno) cannot_fetch_register (regno)
57 #endif
59 /* Offset to the gregset_t location where REG is stored. */
60 #define REG_OFFSET(reg) offsetof (gregset_t, reg)
62 /* At reg_offset[REGNO] you'll find the offset to the gregset_t
63 location where the GDB register REGNO is stored. Unsupported
64 registers are marked with `-1'. */
65 static int reg_offset[] =
67 REG_OFFSET (r_eax),
68 REG_OFFSET (r_ecx),
69 REG_OFFSET (r_edx),
70 REG_OFFSET (r_edx),
71 REG_OFFSET (r_esp),
72 REG_OFFSET (r_ebp),
73 REG_OFFSET (r_esi),
74 REG_OFFSET (r_edi),
75 REG_OFFSET (r_eip),
76 REG_OFFSET (r_eflags),
77 REG_OFFSET (r_cs),
78 REG_OFFSET (r_ss),
79 REG_OFFSET (r_ds),
80 REG_OFFSET (r_es),
81 #ifdef HAVE_STRUCT_REG_R_FS
82 REG_OFFSET (r_fs),
83 #else
84 -1,
85 #endif
86 #ifdef HAVE_STRUCT_REG_R_GS
87 REG_OFFSET (r_gs)
88 #else
90 #endif
93 #define REG_ADDR(regset, regno) ((char *) (regset) + reg_offset[regno])
95 /* Return nonzero if we shouldn't try to fetch register REGNO. */
97 static int
98 cannot_fetch_register (int regno)
100 return (reg_offset[regno] == -1);
104 /* Transfering the registers between GDB, inferiors and core files. */
106 /* Fill GDB's register array with the genereal-purpose register values
107 in *GREGSETP. */
109 void
110 supply_gregset (gregset_t *gregsetp)
112 int i;
114 for (i = 0; i < NUM_GREGS; i++)
116 if (CANNOT_FETCH_REGISTER (i))
117 supply_register (i, NULL);
118 else
119 supply_register (i, REG_ADDR (gregsetp, i));
123 /* Fill register REGNO (if it is a general-purpose register) in
124 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
125 do this for all registers. */
127 void
128 fill_gregset (gregset_t *gregsetp, int regno)
130 int i;
132 for (i = 0; i < NUM_GREGS; i++)
133 if ((regno == -1 || regno == i) && ! CANNOT_STORE_REGISTER (i))
134 memcpy (REG_ADDR (gregsetp, i), &registers[REGISTER_BYTE (i)],
135 REGISTER_RAW_SIZE (i));
138 #include "i387-nat.h"
140 /* Fill GDB's register array with the floating-point register values
141 in *FPREGSETP. */
143 void
144 supply_fpregset (fpregset_t *fpregsetp)
146 i387_supply_fsave ((char *) fpregsetp);
149 /* Fill register REGNO (if it is a floating-point register) in
150 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
151 do this for all registers. */
153 void
154 fill_fpregset (fpregset_t *fpregsetp, int regno)
156 i387_fill_fsave ((char *) fpregsetp, regno);
159 /* Fetch register REGNO from the inferior. If REGNO is -1, do this
160 for all registers (including the floating point registers). */
162 void
163 fetch_inferior_registers (int regno)
165 gregset_t gregs;
167 if (ptrace (PT_GETREGS, inferior_pid, (PTRACE_ARG3_TYPE) &gregs, 0) == -1)
168 perror_with_name ("Couldn't get registers");
170 supply_gregset (&gregs);
172 if (regno == -1 || regno >= FP0_REGNUM)
174 fpregset_t fpregs;
176 if (ptrace (PT_GETFPREGS, inferior_pid,
177 (PTRACE_ARG3_TYPE) &fpregs, 0) == -1)
178 perror_with_name ("Couldn't get floating point status");
180 supply_fpregset (&fpregs);
184 /* Store register REGNO back into the inferior. If REGNO is -1, do
185 this for all registers (including the floating point registers). */
187 void
188 store_inferior_registers (int regno)
190 gregset_t gregs;
192 if (ptrace (PT_GETREGS, inferior_pid, (PTRACE_ARG3_TYPE) &gregs, 0) == -1)
193 perror_with_name ("Couldn't get registers");
195 fill_gregset (&gregs, regno);
197 if (ptrace (PT_SETREGS, inferior_pid, (PTRACE_ARG3_TYPE) &gregs, 0) == -1)
198 perror_with_name ("Couldn't write registers");
200 if (regno == -1 || regno >= FP0_REGNUM)
202 fpregset_t fpregs;
204 if (ptrace (PT_GETFPREGS, inferior_pid,
205 (PTRACE_ARG3_TYPE) &fpregs, 0) == -1)
206 perror_with_name ("Couldn't get floating point status");
208 fill_fpregset (&fpregs, regno);
210 if (ptrace (PT_SETFPREGS, inferior_pid,
211 (PTRACE_ARG3_TYPE) &fpregs, 0) == -1)
212 perror_with_name ("Couldn't write floating point status");
217 /* Support for the user struct. */
219 /* Return the address register REGNO. BLOCKEND is the value of
220 u.u_ar0, which should point to the registers. */
222 CORE_ADDR
223 register_u_addr (CORE_ADDR blockend, int regno)
225 return (CORE_ADDR) REG_ADDR (blockend, regno);
228 #include <sys/param.h>
229 #include <sys/user.h>
231 /* Return the size of the user struct. */
234 kernel_u_size (void)
236 return (sizeof (struct user));