OSDN Git Service

* i386bsd-nat.c: Include "gdb_assert.h".
[pf3gnuchains/pf3gnuchains3x.git] / gdb / i386bsd-nat.c
1 /* Native-dependent code for modern i386 BSD's.
2    Copyright 2000, 2001 Free Software Foundation, Inc.
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"
23 #include "regcache.h"
24
25 #include "gdb_assert.h"
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
36 typedef struct reg gregset_t;
37 #endif
38
39 #ifndef HAVE_FPREGSET_T
40 typedef struct fpreg fpregset_t;
41 #endif
42
43 #include "gregset.h"
44 \f
45
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)
58 #define CANNOT_STORE_REGISTER(regno) cannot_fetch_register (regno)
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'.  */
67 static 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),
83 #ifdef HAVE_STRUCT_REG_R_FS
84   REG_OFFSET (r_fs),
85 #else
86   -1,
87 #endif
88 #ifdef HAVE_STRUCT_REG_R_GS
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
99 static int
100 cannot_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
108 /* Fill GDB's register array with the general-purpose register values
109    in *GREGSETP.  */
110
111 void
112 supply_gregset (gregset_t *gregsetp)
113 {
114   int i;
115
116   for (i = 0; i < NUM_GREGS; i++)
117     {
118       if (CANNOT_FETCH_REGISTER (i))
119         supply_register (i, NULL);
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
129 void
130 fill_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))
136       memcpy (REG_ADDR (gregsetp, i), &registers[REGISTER_BYTE (i)],
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
145 void
146 supply_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
155 void
156 fill_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
164 void
165 fetch_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     }
184 }
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
189 void
190 store_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
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
229 static void
230 i386bsd_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
248 void
249 i386bsd_dr_set_control (unsigned long control)
250 {
251   i386bsd_dr_set (7, control);
252 }
253
254 void
255 i386bsd_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
262 void
263 i386bsd_dr_reset_addr (int regnum)
264 {
265   gdb_assert (regnum >= 0 && regnum <= 4);
266
267   i386bsd_dr_set (regnum, 0);
268 }
269
270 unsigned long
271 i386bsd_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
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
298 CORE_ADDR
299 register_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
309 int
310 kernel_u_size (void)
311 {
312   return (sizeof (struct user));
313 }