1 /* Intel 386 native support for SYSV systems (pre-SVR4).
2 Copyright (C) 1988, 89, 91, 92, 94, 96, 1998 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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, Boston, MA 02111-1307, USA. */
25 # ifdef HAVE_SYS_PTRACE_H
26 # include <sys/ptrace.h>
36 #include <sys/types.h>
39 #include <sys/param.h>
43 #include <sys/ioctl.h>
47 /* FIXME: The following used to be just "#include <sys/debugreg.h>", but
48 * the the Linux 2.1.x kernel and glibc 2.0.x are not in sync; including
49 * <sys/debugreg.h> will result in an error. With luck, these losers
50 * will get their act together and we can trash this hack in the near future.
54 #ifdef TARGET_HAS_HARDWARE_WATCHPOINTS
55 # ifdef HAVE_ASM_DEBUGREG_H
56 # include <asm/debugreg.h>
58 # include <sys/debugreg.h>
69 #include "floatformat.h"
74 /* this table must line up with REGISTER_NAMES in tm-i386v.h */
75 /* symbols like 'EAX' come from <sys/reg.h> */
84 /* blockend is the value of u.u_ar0, and points to the
85 * place where GS is stored
89 i386_register_u_addr (blockend
, regnum
)
98 /* FIXME: Should have better way to test floating point range */
99 if (regnum
>= FP0_REGNUM
&& regnum
<= (FP0_REGNUM
+ 7))
101 #ifdef KSTKSZ /* SCO, and others? */
102 ubase
+= 4 * (SS
+ 1) - KSTKSZ
;
103 fpstate
= ubase
+ ((char *)&u
.u_fps
.u_fpstate
- (char *)&u
);
104 return (fpstate
+ 0x1c + 10 * (regnum
- FP0_REGNUM
));
106 fpstate
= ubase
+ ((char *)&u
.i387
.st_space
- (char *)&u
);
107 return (fpstate
+ 10 * (regnum
- FP0_REGNUM
));
112 return (ubase
+ 4 * regmap
[regnum
]);
120 return (sizeof (struct user
));
123 #ifdef TARGET_HAS_HARDWARE_WATCHPOINTS
125 #if !defined (offsetof)
126 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
129 /* Record the value of the debug control register. */
130 static int debug_control_mirror
;
132 /* Record which address associates with which register. */
133 static CORE_ADDR address_lookup
[DR_LASTADDR
- DR_FIRSTADDR
+ 1];
136 i386_insert_aligned_watchpoint
PARAMS ((int, CORE_ADDR
, CORE_ADDR
, int,
140 i386_insert_nonaligned_watchpoint
PARAMS ((int, CORE_ADDR
, CORE_ADDR
, int,
143 /* Insert a watchpoint. */
146 i386_insert_watchpoint (pid
, addr
, len
, rw
)
152 return i386_insert_aligned_watchpoint (pid
, addr
, addr
, len
, rw
);
156 i386_insert_aligned_watchpoint (pid
, waddr
, addr
, len
, rw
)
164 int read_write_bits
, len_bits
;
165 int free_debug_register
;
168 /* Look for a free debug register. */
169 for (i
= DR_FIRSTADDR
; i
<= DR_LASTADDR
; i
++)
171 if (address_lookup
[i
- DR_FIRSTADDR
] == 0)
175 /* No more debug registers! */
179 read_write_bits
= (rw
& 1) ? DR_RW_READ
: DR_RW_WRITE
;
186 return i386_insert_nonaligned_watchpoint (pid
, waddr
, addr
, len
, rw
);
193 return i386_insert_nonaligned_watchpoint (pid
, waddr
, addr
, len
, rw
);
197 return i386_insert_nonaligned_watchpoint (pid
, waddr
, addr
, len
, rw
);
199 free_debug_register
= i
;
200 register_number
= free_debug_register
- DR_FIRSTADDR
;
201 debug_control_mirror
|=
202 ((read_write_bits
| len_bits
)
203 << (DR_CONTROL_SHIFT
+ DR_CONTROL_SIZE
* register_number
));
204 debug_control_mirror
|=
205 (1 << (DR_LOCAL_ENABLE_SHIFT
+ DR_ENABLE_SIZE
* register_number
));
206 debug_control_mirror
|= DR_LOCAL_SLOWDOWN
;
207 debug_control_mirror
&= ~DR_CONTROL_RESERVED
;
209 ptrace (6, pid
, offsetof (struct user
, u_debugreg
[DR_CONTROL
]),
210 debug_control_mirror
);
211 ptrace (6, pid
, offsetof (struct user
, u_debugreg
[free_debug_register
]),
214 /* Record where we came from. */
215 address_lookup
[register_number
] = addr
;
220 i386_insert_nonaligned_watchpoint (pid
, waddr
, addr
, len
, rw
)
231 static int size_try_array
[16] = {
232 1, 1, 1, 1, /* trying size one */
233 2, 1, 2, 1, /* trying size two */
234 2, 1, 2, 1, /* trying size three */
235 4, 1, 2, 1 /* trying size four */
242 /* Four is the maximum length for 386. */
243 size
= (len
> 4) ? 3 : len
- 1;
244 size
= size_try_array
[size
* 4 + align
];
246 rv
= i386_insert_aligned_watchpoint (pid
, waddr
, addr
, size
, rw
);
249 i386_remove_watchpoint (pid
, waddr
, size
);
258 /* Remove a watchpoint. */
261 i386_remove_watchpoint (pid
, addr
, len
)
269 for (i
= DR_FIRSTADDR
; i
<= DR_LASTADDR
; i
++)
271 register_number
= i
- DR_FIRSTADDR
;
272 if (address_lookup
[register_number
] == addr
)
274 debug_control_mirror
&=
275 ~(1 << (DR_LOCAL_ENABLE_SHIFT
+ DR_ENABLE_SIZE
* register_number
));
276 address_lookup
[register_number
] = 0;
279 ptrace (6, pid
, offsetof (struct user
, u_debugreg
[DR_CONTROL
]),
280 debug_control_mirror
);
281 ptrace (6, pid
, offsetof (struct user
, u_debugreg
[DR_STATUS
]), 0);
286 /* Check if stopped by a watchpoint. */
289 i386_stopped_by_watchpoint (pid
)
295 status
= ptrace (3, pid
, offsetof (struct user
, u_debugreg
[DR_STATUS
]), 0);
296 ptrace (6, pid
, offsetof (struct user
, u_debugreg
[DR_STATUS
]), 0);
298 for (i
= DR_FIRSTADDR
; i
<= DR_LASTADDR
; i
++)
300 if (status
& (1 << (i
- DR_FIRSTADDR
)))
301 return address_lookup
[i
- DR_FIRSTADDR
];
307 #endif /* TARGET_HAS_HARDWARE_WATCHPOINTS */
310 /* using FLOAT_INFO as is would be a problem. FLOAT_INFO is called
311 via a command xxx and eventually calls ptrace without ever having
312 traversed the target vector. This would be terribly impolite
313 behaviour for a sun4 hosted remote gdb.
315 A fix might be to move this code into the "info registers" command.
316 rich@cygnus.com 15 Sept 92. */
319 struct user u
; /* just for address computations */
321 /* fpstate defined in <sys/user.h> */
322 struct fpstate
*fpstatep
;
323 char buf
[sizeof (struct fpstate
) + 2 * sizeof (int)];
326 unsigned int rounded_addr
;
327 unsigned int rounded_size
;
331 uaddr
= (char *)&u
.u_fpvalid
- (char *)&u
;
332 if (target_has_execution
)
337 rounded_addr
= uaddr
& -sizeof (int);
338 data
= ptrace (3, inferior_pid
, (PTRACE_ARG3_TYPE
) rounded_addr
, 0);
339 mask
= 0xff << ((uaddr
- rounded_addr
) * 8);
341 fpvalid
= ((data
& mask
) != 0);
346 if (lseek (corechan
, uaddr
, 0) < 0)
347 perror ("seek on core file");
348 if (myread (corechan
, &fpvalid
, 1) < 0)
349 perror ("read on core file");
352 #endif /* no core support yet */
356 printf_unfiltered ("no floating point status saved\n");
360 uaddr
= (char *)&U_FPSTATE(u
) - (char *)&u
;
361 if (target_has_execution
)
365 rounded_addr
= uaddr
& -sizeof (int);
366 rounded_size
= (((uaddr
+ sizeof (struct fpstate
)) - uaddr
) +
367 sizeof (int) - 1) / sizeof (int);
368 skip
= uaddr
- rounded_addr
;
371 for (i
= 0; i
< rounded_size
; i
++)
373 *ip
++ = ptrace (3, inferior_pid
, (PTRACE_ARG3_TYPE
) rounded_addr
, 0);
374 rounded_addr
+= sizeof (int);
380 if (lseek (corechan
, uaddr
, 0) < 0)
381 perror_with_name ("seek on core file");
382 if (myread (corechan
, buf
, sizeof (struct fpstate
)) < 0)
383 perror_with_name ("read from core file");
388 fpstatep
= (struct fpstate
*)(buf
+ skip
);
389 print_387_status (fpstatep
->status
, (struct env387
*)fpstatep
->state
);