* Makefile.in (gencode): Provide explicit path to gencode.c.
[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
39f77062
KB
169 if (ptrace (PT_GETREGS, PIDGET (inferior_ptid),
170 (PTRACE_ARG3_TYPE) &gregs, 0) == -1)
e6031aeb
MK
171 perror_with_name ("Couldn't get registers");
172
173 supply_gregset (&gregs);
174
175 if (regno == -1 || regno >= FP0_REGNUM)
176 {
177 fpregset_t fpregs;
178
39f77062 179 if (ptrace (PT_GETFPREGS, PIDGET (inferior_ptid),
e6031aeb
MK
180 (PTRACE_ARG3_TYPE) &fpregs, 0) == -1)
181 perror_with_name ("Couldn't get floating point status");
182
183 supply_fpregset (&fpregs);
184 }
b051bfa4 185}
e6031aeb
MK
186
187/* Store register REGNO back into the inferior. If REGNO is -1, do
188 this for all registers (including the floating point registers). */
189
190void
191store_inferior_registers (int regno)
192{
193 gregset_t gregs;
194
39f77062
KB
195 if (ptrace (PT_GETREGS, PIDGET (inferior_ptid),
196 (PTRACE_ARG3_TYPE) &gregs, 0) == -1)
e6031aeb
MK
197 perror_with_name ("Couldn't get registers");
198
199 fill_gregset (&gregs, regno);
200
39f77062 201 if (ptrace (PT_SETREGS, PIDGET (inferior_ptid), (PTRACE_ARG3_TYPE) &gregs, 0) == -1)
e6031aeb
MK
202 perror_with_name ("Couldn't write registers");
203
204 if (regno == -1 || regno >= FP0_REGNUM)
205 {
206 fpregset_t fpregs;
207
39f77062 208 if (ptrace (PT_GETFPREGS, PIDGET (inferior_ptid),
e6031aeb
MK
209 (PTRACE_ARG3_TYPE) &fpregs, 0) == -1)
210 perror_with_name ("Couldn't get floating point status");
211
212 fill_fpregset (&fpregs, regno);
213
39f77062 214 if (ptrace (PT_SETFPREGS, PIDGET (inferior_ptid),
e6031aeb
MK
215 (PTRACE_ARG3_TYPE) &fpregs, 0) == -1)
216 perror_with_name ("Couldn't write floating point status");
217 }
218}
219\f
220
0afdd437
MK
221/* Support for debug registers. */
222
223#ifdef HAVE_PT_GETDBREGS
224
225/* Not all versions of FreeBSD/i386 that support the debug registers
226 have this macro. */
227#ifndef DBREG_DRX
228#define DBREG_DRX(d, x) ((&d->dr0)[x])
229#endif
230
231static void
232i386bsd_dr_set (int regnum, unsigned int value)
233{
234 struct dbreg dbregs;
235
39f77062
KB
236 if (ptrace (PT_GETDBREGS, PIDGET (inferior_ptid),
237 (PTRACE_ARG3_TYPE) &dbregs, 0) == -1)
0afdd437
MK
238 perror_with_name ("Couldn't get debug registers");
239
240 /* For some mysterious reason, some of the reserved bits in the
241 debug control register get set. Mask these off, otherwise the
242 ptrace call below will fail. */
243 dbregs.dr7 &= ~(0x0000fc00);
244
245 DBREG_DRX ((&dbregs), regnum) = value;
246
39f77062
KB
247 if (ptrace (PT_SETDBREGS, PIDGET (inferior_ptid),
248 (PTRACE_ARG3_TYPE) &dbregs, 0) == -1)
0afdd437
MK
249 perror_with_name ("Couldn't write debug registers");
250}
251
252void
253i386bsd_dr_set_control (unsigned long control)
254{
255 i386bsd_dr_set (7, control);
256}
257
258void
259i386bsd_dr_set_addr (int regnum, CORE_ADDR addr)
260{
261 gdb_assert (regnum >= 0 && regnum <= 4);
262
263 i386bsd_dr_set (regnum, addr);
264}
265
266void
267i386bsd_dr_reset_addr (int regnum)
268{
269 gdb_assert (regnum >= 0 && regnum <= 4);
270
271 i386bsd_dr_set (regnum, 0);
272}
273
274unsigned long
275i386bsd_dr_get_status (void)
276{
277 struct dbreg dbregs;
278
279 /* FIXME: kettenis/2001-03-31: Calling perror_with_name if the
280 ptrace call fails breaks debugging remote targets. The correct
281 way to fix this is to add the hardware breakpoint and watchpoint
282 stuff to the target vectore. For now, just return zero if the
283 ptrace call fails. */
39f77062
KB
284 if (ptrace (PT_GETDBREGS, PIDGET (inferior_ptid),
285 (PTRACE_ARG3_TYPE) & dbregs, 0) == -1)
0afdd437
MK
286#if 0
287 perror_with_name ("Couldn't read debug registers");
288#else
289 return 0;
290#endif
291
292 return dbregs.dr6;
293}
294
295#endif /* PT_GETDBREGS */
296\f
297
e6031aeb
MK
298/* Support for the user struct. */
299
300/* Return the address register REGNO. BLOCKEND is the value of
301 u.u_ar0, which should point to the registers. */
302
303CORE_ADDR
304register_u_addr (CORE_ADDR blockend, int regno)
305{
306 return (CORE_ADDR) REG_ADDR (blockend, regno);
307}
308
309#include <sys/param.h>
310#include <sys/user.h>
311
312/* Return the size of the user struct. */
313
314int
315kernel_u_size (void)
316{
317 return (sizeof (struct user));
318}
This page took 0.088968 seconds and 4 git commands to generate.