]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/i386v-nat.c
import gdb-1999-07-07 post reformat
[thirdparty/binutils-gdb.git] / gdb / i386v-nat.c
CommitLineData
c906108c
SS
1/* Intel 386 native support for SYSV systems (pre-SVR4).
2 Copyright (C) 1988, 89, 91, 92, 94, 96, 1998 Free Software Foundation, Inc.
3
c5aa993b 4 This file is part of GDB.
c906108c 5
c5aa993b
JM
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.
c906108c 10
c5aa993b
JM
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.
c906108c 15
c5aa993b
JM
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. */
c906108c
SS
20
21#include "defs.h"
22
23#ifdef HAVE_PTRACE_H
c5aa993b 24#include <ptrace.h>
c906108c 25#else
c5aa993b
JM
26#ifdef HAVE_SYS_PTRACE_H
27#include <sys/ptrace.h>
28#endif
c906108c
SS
29#endif
30
31#include "frame.h"
32#include "inferior.h"
33#include "language.h"
34#include "gdbcore.h"
35
36#ifdef USG
37#include <sys/types.h>
38#endif
39
40#include <sys/param.h>
41#include <sys/dir.h>
42#include <signal.h>
43#include <sys/user.h>
44#include <sys/ioctl.h>
45#include <fcntl.h>
46
47
48/* FIXME: The following used to be just "#include <sys/debugreg.h>", but
49 * the the Linux 2.1.x kernel and glibc 2.0.x are not in sync; including
50 * <sys/debugreg.h> will result in an error. With luck, these losers
51 * will get their act together and we can trash this hack in the near future.
52 * --jsm 1998-10-21
53 */
54
55#ifdef TARGET_HAS_HARDWARE_WATCHPOINTS
c5aa993b
JM
56#ifdef HAVE_ASM_DEBUGREG_H
57#include <asm/debugreg.h>
58#else
59#include <sys/debugreg.h>
60#endif
c906108c
SS
61#endif
62
63#include <sys/file.h>
64#include "gdb_stat.h"
65
66#ifdef HAVE_SYS_REG_H
67#include <sys/reg.h>
68#endif
69
70#include "floatformat.h"
71
72#include "target.h"
c906108c 73\f
c5aa993b 74
c906108c
SS
75/* this table must line up with REGISTER_NAMES in tm-i386v.h */
76/* symbols like 'EAX' come from <sys/reg.h> */
c5aa993b 77static int regmap[] =
c906108c
SS
78{
79 EAX, ECX, EDX, EBX,
80 UESP, EBP, ESI, EDI,
81 EIP, EFL, CS, SS,
82 DS, ES, FS, GS,
83};
84
85/* blockend is the value of u.u_ar0, and points to the
86 * place where GS is stored
87 */
88
89int
90i386_register_u_addr (blockend, regnum)
91 int blockend;
92 int regnum;
93{
94 struct user u;
95 int fpstate;
96 int ubase;
97
98 ubase = blockend;
99 /* FIXME: Should have better way to test floating point range */
c5aa993b 100 if (regnum >= FP0_REGNUM && regnum <= (FP0_REGNUM + 7))
c906108c 101 {
c5aa993b 102#ifdef KSTKSZ /* SCO, and others? */
c906108c 103 ubase += 4 * (SS + 1) - KSTKSZ;
c5aa993b 104 fpstate = ubase + ((char *) &u.u_fps.u_fpstate - (char *) &u);
c906108c
SS
105 return (fpstate + 0x1c + 10 * (regnum - FP0_REGNUM));
106#else
c5aa993b 107 fpstate = ubase + ((char *) &u.i387.st_space - (char *) &u);
c906108c
SS
108 return (fpstate + 10 * (regnum - FP0_REGNUM));
109#endif
c5aa993b 110 }
c906108c
SS
111 else
112 {
113 return (ubase + 4 * regmap[regnum]);
114 }
c5aa993b 115
c906108c
SS
116}
117\f
118int
119kernel_u_size ()
120{
121 return (sizeof (struct user));
122}
123\f
124#ifdef TARGET_HAS_HARDWARE_WATCHPOINTS
125
126#if !defined (offsetof)
127#define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
128#endif
129
130/* Record the value of the debug control register. */
131static int debug_control_mirror;
132
133/* Record which address associates with which register. */
134static CORE_ADDR address_lookup[DR_LASTADDR - DR_FIRSTADDR + 1];
135
136static int
137i386_insert_aligned_watchpoint PARAMS ((int, CORE_ADDR, CORE_ADDR, int,
c5aa993b 138 int));
c906108c
SS
139
140static int
141i386_insert_nonaligned_watchpoint PARAMS ((int, CORE_ADDR, CORE_ADDR, int,
142 int));
143
144/* Insert a watchpoint. */
145
146int
147i386_insert_watchpoint (pid, addr, len, rw)
148 int pid;
149 CORE_ADDR addr;
150 int len;
151 int rw;
152{
153 return i386_insert_aligned_watchpoint (pid, addr, addr, len, rw);
154}
155
156static int
157i386_insert_aligned_watchpoint (pid, waddr, addr, len, rw)
158 int pid;
159 CORE_ADDR waddr;
160 CORE_ADDR addr;
161 int len;
162 int rw;
163{
164 int i;
165 int read_write_bits, len_bits;
166 int free_debug_register;
167 int register_number;
c5aa993b 168
c906108c
SS
169 /* Look for a free debug register. */
170 for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
171 {
172 if (address_lookup[i - DR_FIRSTADDR] == 0)
173 break;
174 }
175
176 /* No more debug registers! */
177 if (i > DR_LASTADDR)
178 return -1;
179
180 read_write_bits = (rw & 1) ? DR_RW_READ : DR_RW_WRITE;
181
182 if (len == 1)
183 len_bits = DR_LEN_1;
184 else if (len == 2)
185 {
186 if (addr % 2)
187 return i386_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw);
188 len_bits = DR_LEN_2;
189 }
190
191 else if (len == 4)
192 {
193 if (addr % 4)
194 return i386_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw);
195 len_bits = DR_LEN_4;
196 }
197 else
198 return i386_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw);
c5aa993b 199
c906108c
SS
200 free_debug_register = i;
201 register_number = free_debug_register - DR_FIRSTADDR;
202 debug_control_mirror |=
203 ((read_write_bits | len_bits)
204 << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * register_number));
205 debug_control_mirror |=
206 (1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * register_number));
207 debug_control_mirror |= DR_LOCAL_SLOWDOWN;
208 debug_control_mirror &= ~DR_CONTROL_RESERVED;
c5aa993b 209
c906108c
SS
210 ptrace (6, pid, offsetof (struct user, u_debugreg[DR_CONTROL]),
211 debug_control_mirror);
212 ptrace (6, pid, offsetof (struct user, u_debugreg[free_debug_register]),
213 addr);
214
215 /* Record where we came from. */
216 address_lookup[register_number] = addr;
217 return 0;
218}
219
220static int
221i386_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw)
222 int pid;
223 CORE_ADDR waddr;
224 CORE_ADDR addr;
225 int len;
226 int rw;
227{
228 int align;
229 int size;
230 int rv;
231
c5aa993b
JM
232 static int size_try_array[16] =
233 {
c906108c
SS
234 1, 1, 1, 1, /* trying size one */
235 2, 1, 2, 1, /* trying size two */
236 2, 1, 2, 1, /* trying size three */
237 4, 1, 2, 1 /* trying size four */
238 };
239
240 rv = 0;
241 while (len > 0)
242 {
243 align = addr % 4;
244 /* Four is the maximum length for 386. */
245 size = (len > 4) ? 3 : len - 1;
246 size = size_try_array[size * 4 + align];
247
248 rv = i386_insert_aligned_watchpoint (pid, waddr, addr, size, rw);
249 if (rv)
250 {
251 i386_remove_watchpoint (pid, waddr, size);
252 return rv;
253 }
254 addr += size;
255 len -= size;
256 }
257 return rv;
258}
259
260/* Remove a watchpoint. */
261
262int
263i386_remove_watchpoint (pid, addr, len)
264 int pid;
265 CORE_ADDR addr;
266 int len;
267{
268 int i;
269 int register_number;
270
271 for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
272 {
273 register_number = i - DR_FIRSTADDR;
274 if (address_lookup[register_number] == addr)
275 {
276 debug_control_mirror &=
277 ~(1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * register_number));
278 address_lookup[register_number] = 0;
279 }
280 }
281 ptrace (6, pid, offsetof (struct user, u_debugreg[DR_CONTROL]),
282 debug_control_mirror);
283 ptrace (6, pid, offsetof (struct user, u_debugreg[DR_STATUS]), 0);
284
285 return 0;
286}
287
288/* Check if stopped by a watchpoint. */
289
290CORE_ADDR
291i386_stopped_by_watchpoint (pid)
c5aa993b 292 int pid;
c906108c
SS
293{
294 int i;
295 int status;
296
297 status = ptrace (3, pid, offsetof (struct user, u_debugreg[DR_STATUS]), 0);
298 ptrace (6, pid, offsetof (struct user, u_debugreg[DR_STATUS]), 0);
299
300 for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
301 {
302 if (status & (1 << (i - DR_FIRSTADDR)))
303 return address_lookup[i - DR_FIRSTADDR];
304 }
305
306 return 0;
307}
308
309#endif /* TARGET_HAS_HARDWARE_WATCHPOINTS */
310
311#if 0
312/* using FLOAT_INFO as is would be a problem. FLOAT_INFO is called
313 via a command xxx and eventually calls ptrace without ever having
314 traversed the target vector. This would be terribly impolite
315 behaviour for a sun4 hosted remote gdb.
316
317 A fix might be to move this code into the "info registers" command.
318 rich@cygnus.com 15 Sept 92. */
319i386_float_info ()
320{
c5aa993b 321 struct user u; /* just for address computations */
c906108c
SS
322 int i;
323 /* fpstate defined in <sys/user.h> */
324 struct fpstate *fpstatep;
325 char buf[sizeof (struct fpstate) + 2 * sizeof (int)];
326 unsigned int uaddr;
327 char fpvalid = 0;
328 unsigned int rounded_addr;
329 unsigned int rounded_size;
330 extern int corechan;
331 int skip;
c5aa993b
JM
332
333 uaddr = (char *) &u.u_fpvalid - (char *) &u;
c906108c
SS
334 if (target_has_execution)
335 {
336 unsigned int data;
337 unsigned int mask;
c5aa993b 338
c906108c
SS
339 rounded_addr = uaddr & -sizeof (int);
340 data = ptrace (3, inferior_pid, (PTRACE_ARG3_TYPE) rounded_addr, 0);
341 mask = 0xff << ((uaddr - rounded_addr) * 8);
c5aa993b 342
c906108c 343 fpvalid = ((data & mask) != 0);
c5aa993b 344 }
c906108c 345#if 0
c5aa993b 346 else
c906108c
SS
347 {
348 if (lseek (corechan, uaddr, 0) < 0)
349 perror ("seek on core file");
c5aa993b 350 if (myread (corechan, &fpvalid, 1) < 0)
c906108c 351 perror ("read on core file");
c5aa993b 352
c906108c 353 }
c5aa993b
JM
354#endif /* no core support yet */
355
356 if (fpvalid == 0)
c906108c
SS
357 {
358 printf_unfiltered ("no floating point status saved\n");
359 return;
360 }
c5aa993b
JM
361
362 uaddr = (char *) &U_FPSTATE (u) - (char *) &u;
c906108c
SS
363 if (target_has_execution)
364 {
365 int *ip;
c5aa993b 366
c906108c
SS
367 rounded_addr = uaddr & -sizeof (int);
368 rounded_size = (((uaddr + sizeof (struct fpstate)) - uaddr) +
369 sizeof (int) - 1) / sizeof (int);
370 skip = uaddr - rounded_addr;
c5aa993b
JM
371
372 ip = (int *) buf;
373 for (i = 0; i < rounded_size; i++)
c906108c
SS
374 {
375 *ip++ = ptrace (3, inferior_pid, (PTRACE_ARG3_TYPE) rounded_addr, 0);
376 rounded_addr += sizeof (int);
377 }
c5aa993b 378 }
c906108c 379#if 0
c5aa993b 380 else
c906108c
SS
381 {
382 if (lseek (corechan, uaddr, 0) < 0)
383 perror_with_name ("seek on core file");
c5aa993b
JM
384 if (myread (corechan, buf, sizeof (struct fpstate)) < 0)
385 perror_with_name ("read from core file");
c906108c
SS
386 skip = 0;
387 }
c5aa993b 388#endif /* 0 */
c906108c 389
c5aa993b
JM
390 fpstatep = (struct fpstate *) (buf + skip);
391 print_387_status (fpstatep->status, (struct env387 *) fpstatep->state);
c906108c
SS
392}
393
394#endif /* never */