2001-04-17 Michael Snyder <msnyder@redhat.com>
[deliverable/binutils-gdb.git] / gdb / i386bsd-nat.c
CommitLineData
e6031aeb 1/* Native-dependent code for modern i386 BSD's.
4e052eda 2 Copyright 2000, 2001 Free Software Foundation, Inc.
e6031aeb
MK
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"
4e052eda 23#include "regcache.h"
e6031aeb 24
0afdd437 25#include "gdb_assert.h"
e6031aeb
MK
26#include <sys/types.h>
27#include <sys/ptrace.h>
28#include <machine/reg.h>
29#include <machine/frame.h>
30
31#ifdef HAVE_SYS_PROCFS_H
32#include <sys/procfs.h>
33#endif
34
35#ifndef HAVE_GREGSET_T
36typedef struct reg gregset_t;
37#endif
38
39#ifndef HAVE_FPREGSET_T
40typedef struct fpreg fpregset_t;
41#endif
42
b051bfa4
MK
43#include "gregset.h"
44\f
45
e6031aeb
MK
46/* In older BSD versions we cannot get at some of the segment
47 registers. FreeBSD for example didn't support the %fs and %gs
48 registers until the 3.0 release. We have autoconf checks for their
49 presence, and deal gracefully with their absence. */
50
51/* Registers we shouldn't try to fetch. */
52#if !defined (CANNOT_FETCH_REGISTER)
53#define CANNOT_FETCH_REGISTER(regno) cannot_fetch_register (regno)
54#endif
55
56/* Registers we shouldn't try to store. */
57#if !defined (CANNOT_STORE_REGISTER)
b051bfa4 58#define CANNOT_STORE_REGISTER(regno) cannot_fetch_register (regno)
e6031aeb
MK
59#endif
60
61/* Offset to the gregset_t location where REG is stored. */
62#define REG_OFFSET(reg) offsetof (gregset_t, reg)
63
64/* At reg_offset[REGNO] you'll find the offset to the gregset_t
65 location where the GDB register REGNO is stored. Unsupported
66 registers are marked with `-1'. */
67static int reg_offset[] =
68{
69 REG_OFFSET (r_eax),
70 REG_OFFSET (r_ecx),
71 REG_OFFSET (r_edx),
72 REG_OFFSET (r_edx),
73 REG_OFFSET (r_esp),
74 REG_OFFSET (r_ebp),
75 REG_OFFSET (r_esi),
76 REG_OFFSET (r_edi),
77 REG_OFFSET (r_eip),
78 REG_OFFSET (r_eflags),
79 REG_OFFSET (r_cs),
80 REG_OFFSET (r_ss),
81 REG_OFFSET (r_ds),
82 REG_OFFSET (r_es),
422ea4b8 83#ifdef HAVE_STRUCT_REG_R_FS
e6031aeb
MK
84 REG_OFFSET (r_fs),
85#else
86 -1,
87#endif
422ea4b8 88#ifdef HAVE_STRUCT_REG_R_GS
e6031aeb
MK
89 REG_OFFSET (r_gs)
90#else
91 -1
92#endif
93};
94
95#define REG_ADDR(regset, regno) ((char *) (regset) + reg_offset[regno])
96
97/* Return nonzero if we shouldn't try to fetch register REGNO. */
98
99static int
100cannot_fetch_register (int regno)
101{
102 return (reg_offset[regno] == -1);
103}
104\f
105
106/* Transfering the registers between GDB, inferiors and core files. */
107
ad2a4d09 108/* Fill GDB's register array with the general-purpose register values
e6031aeb
MK
109 in *GREGSETP. */
110
111void
112supply_gregset (gregset_t *gregsetp)
113{
e6031aeb
MK
114 int i;
115
116 for (i = 0; i < NUM_GREGS; i++)
117 {
118 if (CANNOT_FETCH_REGISTER (i))
b051bfa4 119 supply_register (i, NULL);
e6031aeb
MK
120 else
121 supply_register (i, REG_ADDR (gregsetp, i));
122 }
123}
124
125/* Fill register REGNO (if it is a general-purpose register) in
126 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
127 do this for all registers. */
128
129void
130fill_gregset (gregset_t *gregsetp, int regno)
131{
132 int i;
133
134 for (i = 0; i < NUM_GREGS; i++)
135 if ((regno == -1 || regno == i) && ! CANNOT_STORE_REGISTER (i))
b051bfa4 136 memcpy (REG_ADDR (gregsetp, i), &registers[REGISTER_BYTE (i)],
e6031aeb
MK
137 REGISTER_RAW_SIZE (i));
138}
139
140#include "i387-nat.h"
141
142/* Fill GDB's register array with the floating-point register values
143 in *FPREGSETP. */
144
145void
146supply_fpregset (fpregset_t *fpregsetp)
147{
148 i387_supply_fsave ((char *) fpregsetp);
149}
150
151/* Fill register REGNO (if it is a floating-point register) in
152 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
153 do this for all registers. */
154
155void
156fill_fpregset (fpregset_t *fpregsetp, int regno)
157{
158 i387_fill_fsave ((char *) fpregsetp, regno);
159}
160
161/* Fetch register REGNO from the inferior. If REGNO is -1, do this
162 for all registers (including the floating point registers). */
163
164void
165fetch_inferior_registers (int regno)
166{
167 gregset_t gregs;
168
169 if (ptrace (PT_GETREGS, inferior_pid, (PTRACE_ARG3_TYPE) &gregs, 0) == -1)
170 perror_with_name ("Couldn't get registers");
171
172 supply_gregset (&gregs);
173
174 if (regno == -1 || regno >= FP0_REGNUM)
175 {
176 fpregset_t fpregs;
177
178 if (ptrace (PT_GETFPREGS, inferior_pid,
179 (PTRACE_ARG3_TYPE) &fpregs, 0) == -1)
180 perror_with_name ("Couldn't get floating point status");
181
182 supply_fpregset (&fpregs);
183 }
b051bfa4 184}
e6031aeb
MK
185
186/* Store register REGNO back into the inferior. If REGNO is -1, do
187 this for all registers (including the floating point registers). */
188
189void
190store_inferior_registers (int regno)
191{
192 gregset_t gregs;
193
194 if (ptrace (PT_GETREGS, inferior_pid, (PTRACE_ARG3_TYPE) &gregs, 0) == -1)
195 perror_with_name ("Couldn't get registers");
196
197 fill_gregset (&gregs, regno);
198
199 if (ptrace (PT_SETREGS, inferior_pid, (PTRACE_ARG3_TYPE) &gregs, 0) == -1)
200 perror_with_name ("Couldn't write registers");
201
202 if (regno == -1 || regno >= FP0_REGNUM)
203 {
204 fpregset_t fpregs;
205
206 if (ptrace (PT_GETFPREGS, inferior_pid,
207 (PTRACE_ARG3_TYPE) &fpregs, 0) == -1)
208 perror_with_name ("Couldn't get floating point status");
209
210 fill_fpregset (&fpregs, regno);
211
212 if (ptrace (PT_SETFPREGS, inferior_pid,
213 (PTRACE_ARG3_TYPE) &fpregs, 0) == -1)
214 perror_with_name ("Couldn't write floating point status");
215 }
216}
217\f
218
0afdd437
MK
219/* Support for debug registers. */
220
221#ifdef HAVE_PT_GETDBREGS
222
223/* Not all versions of FreeBSD/i386 that support the debug registers
224 have this macro. */
225#ifndef DBREG_DRX
226#define DBREG_DRX(d, x) ((&d->dr0)[x])
227#endif
228
229static void
230i386bsd_dr_set (int regnum, unsigned int value)
231{
232 struct dbreg dbregs;
233
234 if (ptrace (PT_GETDBREGS, inferior_pid, (PTRACE_ARG3_TYPE) &dbregs, 0) == -1)
235 perror_with_name ("Couldn't get debug registers");
236
237 /* For some mysterious reason, some of the reserved bits in the
238 debug control register get set. Mask these off, otherwise the
239 ptrace call below will fail. */
240 dbregs.dr7 &= ~(0x0000fc00);
241
242 DBREG_DRX ((&dbregs), regnum) = value;
243
244 if (ptrace (PT_SETDBREGS, inferior_pid, (PTRACE_ARG3_TYPE) &dbregs, 0) == -1)
245 perror_with_name ("Couldn't write debug registers");
246}
247
248void
249i386bsd_dr_set_control (unsigned long control)
250{
251 i386bsd_dr_set (7, control);
252}
253
254void
255i386bsd_dr_set_addr (int regnum, CORE_ADDR addr)
256{
257 gdb_assert (regnum >= 0 && regnum <= 4);
258
259 i386bsd_dr_set (regnum, addr);
260}
261
262void
263i386bsd_dr_reset_addr (int regnum)
264{
265 gdb_assert (regnum >= 0 && regnum <= 4);
266
267 i386bsd_dr_set (regnum, 0);
268}
269
270unsigned long
271i386bsd_dr_get_status (void)
272{
273 struct dbreg dbregs;
274
275 /* FIXME: kettenis/2001-03-31: Calling perror_with_name if the
276 ptrace call fails breaks debugging remote targets. The correct
277 way to fix this is to add the hardware breakpoint and watchpoint
278 stuff to the target vectore. For now, just return zero if the
279 ptrace call fails. */
280 if (ptrace (PT_GETDBREGS, inferior_pid, (PTRACE_ARG3_TYPE) &dbregs, 0) == -1)
281#if 0
282 perror_with_name ("Couldn't read debug registers");
283#else
284 return 0;
285#endif
286
287 return dbregs.dr6;
288}
289
290#endif /* PT_GETDBREGS */
291\f
292
e6031aeb
MK
293/* Support for the user struct. */
294
295/* Return the address register REGNO. BLOCKEND is the value of
296 u.u_ar0, which should point to the registers. */
297
298CORE_ADDR
299register_u_addr (CORE_ADDR blockend, int regno)
300{
301 return (CORE_ADDR) REG_ADDR (blockend, regno);
302}
303
304#include <sys/param.h>
305#include <sys/user.h>
306
307/* Return the size of the user struct. */
308
309int
310kernel_u_size (void)
311{
312 return (sizeof (struct user));
313}
This page took 0.070673 seconds and 4 git commands to generate.