2002-06-14 Chris Demetriou <cgd@broadcom.com>
[deliverable/binutils-gdb.git] / gdb / i386bsd-nat.c
1 /* Native-dependent code for modern i386 BSD's.
2 Copyright 2000, 2001, 2002 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 <signal.h>
27 #include <stddef.h>
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
38 typedef struct reg gregset_t;
39 #endif
40
41 #ifndef HAVE_FPREGSET_T
42 typedef struct fpreg fpregset_t;
43 #endif
44
45 #include "gregset.h"
46 \f
47
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)
60 #define CANNOT_STORE_REGISTER(regno) cannot_fetch_register (regno)
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'. */
69 static int reg_offset[] =
70 {
71 REG_OFFSET (r_eax),
72 REG_OFFSET (r_ecx),
73 REG_OFFSET (r_edx),
74 REG_OFFSET (r_ebx),
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),
85 #ifdef HAVE_STRUCT_REG_R_FS
86 REG_OFFSET (r_fs),
87 #else
88 -1,
89 #endif
90 #ifdef HAVE_STRUCT_REG_R_GS
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
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. */
106 static int have_ptrace_xmmregs = -1;
107 #endif
108
109 /* Return nonzero if we shouldn't try to fetch register REGNO. */
110
111 static int
112 cannot_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
120 /* Fill GDB's register array with the general-purpose register values
121 in *GREGSETP. */
122
123 void
124 supply_gregset (gregset_t *gregsetp)
125 {
126 int i;
127
128 for (i = 0; i < NUM_GREGS; i++)
129 {
130 if (CANNOT_FETCH_REGISTER (i))
131 supply_register (i, NULL);
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
141 void
142 fill_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))
148 regcache_collect (i, REG_ADDR (gregsetp, i));
149 }
150
151 #include "i387-tdep.h"
152
153 /* Fill GDB's register array with the floating-point register values
154 in *FPREGSETP. */
155
156 void
157 supply_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
166 void
167 fill_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
175 void
176 fetch_inferior_registers (int regno)
177 {
178
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");
186
187 supply_gregset (&gregs);
188 if (regno != -1)
189 return;
190 }
191
192 if (regno == -1 || regno >= FP0_REGNUM)
193 {
194 fpregset_t fpregs;
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");
210
211 supply_fpregset (&fpregs);
212 }
213 #else
214 if (ptrace (PT_GETFPREGS, PIDGET (inferior_ptid),
215 (PTRACE_ARG3_TYPE) &fpregs, 0) == -1)
216 perror_with_name ("Couldn't get floating point status");
217
218 supply_fpregset (&fpregs);
219 #endif
220 }
221 }
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
226 void
227 store_inferior_registers (int regno)
228 {
229
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");
237
238 fill_gregset (&gregs, regno);
239
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 }
247
248 if (regno == -1 || regno >= FP0_REGNUM)
249 {
250 fpregset_t fpregs;
251 #ifdef HAVE_PT_GETXMMREGS
252 char xmmregs[512];
253
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);
261
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);
275
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
282 }
283 }
284 \f
285
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
296 static void
297 i386bsd_dr_set (int regnum, unsigned int value)
298 {
299 struct dbreg dbregs;
300
301 if (ptrace (PT_GETDBREGS, PIDGET (inferior_ptid),
302 (PTRACE_ARG3_TYPE) &dbregs, 0) == -1)
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
312 if (ptrace (PT_SETDBREGS, PIDGET (inferior_ptid),
313 (PTRACE_ARG3_TYPE) &dbregs, 0) == -1)
314 perror_with_name ("Couldn't write debug registers");
315 }
316
317 void
318 i386bsd_dr_set_control (unsigned long control)
319 {
320 i386bsd_dr_set (7, control);
321 }
322
323 void
324 i386bsd_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
331 void
332 i386bsd_dr_reset_addr (int regnum)
333 {
334 gdb_assert (regnum >= 0 && regnum <= 4);
335
336 i386bsd_dr_set (regnum, 0);
337 }
338
339 unsigned long
340 i386bsd_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
347 stuff to the target vector. For now, just return zero if the
348 ptrace call fails. */
349 if (ptrace (PT_GETDBREGS, PIDGET (inferior_ptid),
350 (PTRACE_ARG3_TYPE) & dbregs, 0) == -1)
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
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
368 CORE_ADDR
369 register_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
379 int
380 kernel_u_size (void)
381 {
382 return (sizeof (struct user));
383 }
384 \f
385 /* See i386bsd-tdep.c. */
386 extern int i386bsd_sigcontext_pc_offset;
387
388 void
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);
400 }
This page took 0.057549 seconds and 5 git commands to generate.