2002-06-14 Chris Demetriou <cgd@broadcom.com>
[deliverable/binutils-gdb.git] / gdb / i386bsd-nat.c
CommitLineData
e6031aeb 1/* Native-dependent code for modern i386 BSD's.
7e89e357 2 Copyright 2000, 2001, 2002 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"
b7247919
MK
26#include <signal.h>
27#include <stddef.h>
e6031aeb
MK
28#include <sys/types.h>
29#include <sys/ptrace.h>
30#include <machine/reg.h>
31#include <machine/frame.h>
32
33#ifdef HAVE_SYS_PROCFS_H
34#include <sys/procfs.h>
35#endif
36
37#ifndef HAVE_GREGSET_T
38typedef struct reg gregset_t;
39#endif
40
41#ifndef HAVE_FPREGSET_T
42typedef struct fpreg fpregset_t;
43#endif
44
b051bfa4
MK
45#include "gregset.h"
46\f
47
e6031aeb
MK
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. */
52
53/* Registers we shouldn't try to fetch. */
54#if !defined (CANNOT_FETCH_REGISTER)
55#define CANNOT_FETCH_REGISTER(regno) cannot_fetch_register (regno)
56#endif
57
58/* Registers we shouldn't try to store. */
59#if !defined (CANNOT_STORE_REGISTER)
b051bfa4 60#define CANNOT_STORE_REGISTER(regno) cannot_fetch_register (regno)
e6031aeb
MK
61#endif
62
63/* Offset to the gregset_t location where REG is stored. */
64#define REG_OFFSET(reg) offsetof (gregset_t, reg)
65
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'. */
69static int reg_offset[] =
70{
71 REG_OFFSET (r_eax),
72 REG_OFFSET (r_ecx),
73 REG_OFFSET (r_edx),
2c48bda3 74 REG_OFFSET (r_ebx),
e6031aeb
MK
75 REG_OFFSET (r_esp),
76 REG_OFFSET (r_ebp),
77 REG_OFFSET (r_esi),
78 REG_OFFSET (r_edi),
79 REG_OFFSET (r_eip),
80 REG_OFFSET (r_eflags),
81 REG_OFFSET (r_cs),
82 REG_OFFSET (r_ss),
83 REG_OFFSET (r_ds),
84 REG_OFFSET (r_es),
422ea4b8 85#ifdef HAVE_STRUCT_REG_R_FS
e6031aeb
MK
86 REG_OFFSET (r_fs),
87#else
88 -1,
89#endif
422ea4b8 90#ifdef HAVE_STRUCT_REG_R_GS
e6031aeb
MK
91 REG_OFFSET (r_gs)
92#else
93 -1
94#endif
95};
96
97#define REG_ADDR(regset, regno) ((char *) (regset) + reg_offset[regno])
98
7e89e357
JT
99/* Macro to determine if a register is fetched with PT_GETREGS. */
100#define GETREGS_SUPPLIES(regno) \
101 ((0 <= (regno) && (regno) <= 15))
102
103#ifdef HAVE_PT_GETXMMREGS
104/* Set to 1 if the kernel supports PT_GETXMMREGS. Initialized to -1
105 so that we try PT_GETXMMREGS the first time around. */
106static int have_ptrace_xmmregs = -1;
107#endif
108
e6031aeb
MK
109/* Return nonzero if we shouldn't try to fetch register REGNO. */
110
111static int
112cannot_fetch_register (int regno)
113{
114 return (reg_offset[regno] == -1);
115}
116\f
117
118/* Transfering the registers between GDB, inferiors and core files. */
119
ad2a4d09 120/* Fill GDB's register array with the general-purpose register values
e6031aeb
MK
121 in *GREGSETP. */
122
123void
124supply_gregset (gregset_t *gregsetp)
125{
e6031aeb
MK
126 int i;
127
128 for (i = 0; i < NUM_GREGS; i++)
129 {
130 if (CANNOT_FETCH_REGISTER (i))
b051bfa4 131 supply_register (i, NULL);
e6031aeb
MK
132 else
133 supply_register (i, REG_ADDR (gregsetp, i));
134 }
135}
136
137/* Fill register REGNO (if it is a general-purpose register) in
138 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
139 do this for all registers. */
140
141void
142fill_gregset (gregset_t *gregsetp, int regno)
143{
144 int i;
145
146 for (i = 0; i < NUM_GREGS; i++)
147 if ((regno == -1 || regno == i) && ! CANNOT_STORE_REGISTER (i))
7e89e357 148 regcache_collect (i, REG_ADDR (gregsetp, i));
e6031aeb
MK
149}
150
e750d25e 151#include "i387-tdep.h"
e6031aeb
MK
152
153/* Fill GDB's register array with the floating-point register values
154 in *FPREGSETP. */
155
156void
157supply_fpregset (fpregset_t *fpregsetp)
158{
159 i387_supply_fsave ((char *) fpregsetp);
160}
161
162/* Fill register REGNO (if it is a floating-point register) in
163 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
164 do this for all registers. */
165
166void
167fill_fpregset (fpregset_t *fpregsetp, int regno)
168{
169 i387_fill_fsave ((char *) fpregsetp, regno);
170}
171
172/* Fetch register REGNO from the inferior. If REGNO is -1, do this
173 for all registers (including the floating point registers). */
174
175void
176fetch_inferior_registers (int regno)
177{
e6031aeb 178
7e89e357
JT
179 if (regno == -1 || GETREGS_SUPPLIES (regno))
180 {
181 gregset_t gregs;
182
183 if (ptrace (PT_GETREGS, PIDGET (inferior_ptid),
184 (PTRACE_ARG3_TYPE) &gregs, 0) == -1)
185 perror_with_name ("Couldn't get registers");
e6031aeb 186
7e89e357
JT
187 supply_gregset (&gregs);
188 if (regno != -1)
189 return;
190 }
e6031aeb
MK
191
192 if (regno == -1 || regno >= FP0_REGNUM)
193 {
194 fpregset_t fpregs;
7e89e357
JT
195#ifdef HAVE_PT_GETXMMREGS
196 char xmmregs[512];
197
198 if (have_ptrace_xmmregs != 0 &&
199 ptrace(PT_GETXMMREGS, PIDGET (inferior_ptid),
200 (PTRACE_ARG3_TYPE) xmmregs, 0) == 0)
201 {
202 have_ptrace_xmmregs = 1;
203 i387_supply_fxsave (xmmregs);
204 }
205 else
206 {
207 if (ptrace (PT_GETFPREGS, PIDGET (inferior_ptid),
208 (PTRACE_ARG3_TYPE) &fpregs, 0) == -1)
209 perror_with_name ("Couldn't get floating point status");
e6031aeb 210
7e89e357
JT
211 supply_fpregset (&fpregs);
212 }
213#else
39f77062 214 if (ptrace (PT_GETFPREGS, PIDGET (inferior_ptid),
e6031aeb
MK
215 (PTRACE_ARG3_TYPE) &fpregs, 0) == -1)
216 perror_with_name ("Couldn't get floating point status");
217
218 supply_fpregset (&fpregs);
7e89e357 219#endif
e6031aeb 220 }
b051bfa4 221}
e6031aeb
MK
222
223/* Store register REGNO back into the inferior. If REGNO is -1, do
224 this for all registers (including the floating point registers). */
225
226void
227store_inferior_registers (int regno)
228{
e6031aeb 229
7e89e357
JT
230 if (regno == -1 || GETREGS_SUPPLIES (regno))
231 {
232 gregset_t gregs;
233
234 if (ptrace (PT_GETREGS, PIDGET (inferior_ptid),
235 (PTRACE_ARG3_TYPE) &gregs, 0) == -1)
236 perror_with_name ("Couldn't get registers");
e6031aeb 237
7e89e357 238 fill_gregset (&gregs, regno);
e6031aeb 239
7e89e357
JT
240 if (ptrace (PT_SETREGS, PIDGET (inferior_ptid),
241 (PTRACE_ARG3_TYPE) &gregs, 0) == -1)
242 perror_with_name ("Couldn't write registers");
243
244 if (regno != -1)
245 return;
246 }
e6031aeb
MK
247
248 if (regno == -1 || regno >= FP0_REGNUM)
249 {
250 fpregset_t fpregs;
7e89e357
JT
251#ifdef HAVE_PT_GETXMMREGS
252 char xmmregs[512];
e6031aeb 253
7e89e357
JT
254 if (have_ptrace_xmmregs != 0 &&
255 ptrace(PT_GETXMMREGS, PIDGET (inferior_ptid),
256 (PTRACE_ARG3_TYPE) xmmregs, 0) == 0)
257 {
258 have_ptrace_xmmregs = 1;
259
260 i387_fill_fxsave (xmmregs, regno);
e6031aeb 261
7e89e357
JT
262 if (ptrace (PT_SETXMMREGS, PIDGET (inferior_ptid),
263 (PTRACE_ARG3_TYPE) xmmregs, 0) == -1)
264 perror_with_name ("Couldn't write XMM registers");
265 }
266 else
267 {
268 have_ptrace_xmmregs = 0;
269#endif
270 if (ptrace (PT_GETFPREGS, PIDGET (inferior_ptid),
271 (PTRACE_ARG3_TYPE) &fpregs, 0) == -1)
272 perror_with_name ("Couldn't get floating point status");
273
274 fill_fpregset (&fpregs, regno);
e6031aeb 275
7e89e357
JT
276 if (ptrace (PT_SETFPREGS, PIDGET (inferior_ptid),
277 (PTRACE_ARG3_TYPE) &fpregs, 0) == -1)
278 perror_with_name ("Couldn't write floating point status");
279#ifdef HAVE_PT_GETXMMREGS
280 }
281#endif
e6031aeb
MK
282 }
283}
284\f
285
0afdd437
MK
286/* Support for debug registers. */
287
288#ifdef HAVE_PT_GETDBREGS
289
290/* Not all versions of FreeBSD/i386 that support the debug registers
291 have this macro. */
292#ifndef DBREG_DRX
293#define DBREG_DRX(d, x) ((&d->dr0)[x])
294#endif
295
296static void
297i386bsd_dr_set (int regnum, unsigned int value)
298{
299 struct dbreg dbregs;
300
39f77062
KB
301 if (ptrace (PT_GETDBREGS, PIDGET (inferior_ptid),
302 (PTRACE_ARG3_TYPE) &dbregs, 0) == -1)
0afdd437
MK
303 perror_with_name ("Couldn't get debug registers");
304
305 /* For some mysterious reason, some of the reserved bits in the
306 debug control register get set. Mask these off, otherwise the
307 ptrace call below will fail. */
308 dbregs.dr7 &= ~(0x0000fc00);
309
310 DBREG_DRX ((&dbregs), regnum) = value;
311
39f77062
KB
312 if (ptrace (PT_SETDBREGS, PIDGET (inferior_ptid),
313 (PTRACE_ARG3_TYPE) &dbregs, 0) == -1)
0afdd437
MK
314 perror_with_name ("Couldn't write debug registers");
315}
316
317void
318i386bsd_dr_set_control (unsigned long control)
319{
320 i386bsd_dr_set (7, control);
321}
322
323void
324i386bsd_dr_set_addr (int regnum, CORE_ADDR addr)
325{
326 gdb_assert (regnum >= 0 && regnum <= 4);
327
328 i386bsd_dr_set (regnum, addr);
329}
330
331void
332i386bsd_dr_reset_addr (int regnum)
333{
334 gdb_assert (regnum >= 0 && regnum <= 4);
335
336 i386bsd_dr_set (regnum, 0);
337}
338
339unsigned long
340i386bsd_dr_get_status (void)
341{
342 struct dbreg dbregs;
343
344 /* FIXME: kettenis/2001-03-31: Calling perror_with_name if the
345 ptrace call fails breaks debugging remote targets. The correct
346 way to fix this is to add the hardware breakpoint and watchpoint
b7247919 347 stuff to the target vector. For now, just return zero if the
0afdd437 348 ptrace call fails. */
39f77062
KB
349 if (ptrace (PT_GETDBREGS, PIDGET (inferior_ptid),
350 (PTRACE_ARG3_TYPE) & dbregs, 0) == -1)
0afdd437
MK
351#if 0
352 perror_with_name ("Couldn't read debug registers");
353#else
354 return 0;
355#endif
356
357 return dbregs.dr6;
358}
359
360#endif /* PT_GETDBREGS */
361\f
362
e6031aeb
MK
363/* Support for the user struct. */
364
365/* Return the address register REGNO. BLOCKEND is the value of
366 u.u_ar0, which should point to the registers. */
367
368CORE_ADDR
369register_u_addr (CORE_ADDR blockend, int regno)
370{
371 return (CORE_ADDR) REG_ADDR (blockend, regno);
372}
373
374#include <sys/param.h>
375#include <sys/user.h>
376
377/* Return the size of the user struct. */
378
379int
380kernel_u_size (void)
381{
382 return (sizeof (struct user));
383}
b7247919
MK
384\f
385/* See i386bsd-tdep.c. */
386extern int i386bsd_sigcontext_pc_offset;
387
b7247919
MK
388void
389_initialize_i386bsd_nat (void)
390{
391 /* To support the recognition of signal handlers, i386bsd-tdep.c
392 hardcodes some constants. Inclusion of this file means that we
393 are compiling a native debugger, which means that we can use the
394 system header files and sysctl(3) to get at the relevant
395 information. */
396
397 /* Override the default value for the offset of the program counter
398 in the sigcontext structure. */
399 i386bsd_sigcontext_pc_offset = offsetof (struct sigcontext, sc_pc);
b7247919 400}
This page took 0.179089 seconds and 4 git commands to generate.