1 /* Intel 386 native support for SYSV systems (pre-SVR4).
2 Copyright 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999,
3 2000 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
27 #ifdef HAVE_SYS_PTRACE_H
28 #include <sys/ptrace.h>
38 #include <sys/types.h>
41 #include <sys/param.h>
45 #include <sys/ioctl.h>
49 /* FIXME: The following used to be just "#include <sys/debugreg.h>", but
50 * the the Linux 2.1.x kernel and glibc 2.0.x are not in sync; including
51 * <sys/debugreg.h> will result in an error. With luck, these losers
52 * will get their act together and we can trash this hack in the near future.
56 #ifdef TARGET_HAS_HARDWARE_WATCHPOINTS
57 #ifdef HAVE_ASM_DEBUGREG_H
58 #include <asm/debugreg.h>
60 #include <sys/debugreg.h>
71 #include "floatformat.h"
76 /* this table must line up with REGISTER_NAMES in tm-i386v.h */
77 /* symbols like 'EAX' come from <sys/reg.h> */
86 /* blockend is the value of u.u_ar0, and points to the
87 * place where GS is stored
91 i386_register_u_addr (int blockend, int 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 (int, CORE_ADDR, CORE_ADDR, int, int);
139 i386_insert_nonaligned_watchpoint (int, CORE_ADDR, CORE_ADDR, int, int);
141 /* Insert a watchpoint. */
144 i386_insert_watchpoint (int pid, CORE_ADDR addr, int len, int rw)
146 return i386_insert_aligned_watchpoint (pid, addr, addr, len, rw);
150 i386_insert_aligned_watchpoint (int pid, CORE_ADDR waddr, CORE_ADDR addr,
154 int read_write_bits, len_bits;
155 int free_debug_register;
158 /* Look for a free debug register. */
159 for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
161 if (address_lookup[i - DR_FIRSTADDR] == 0)
165 /* No more debug registers! */
169 read_write_bits = (rw & 1) ? DR_RW_READ : DR_RW_WRITE;
176 return i386_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw);
183 return i386_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw);
187 return i386_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw);
189 free_debug_register = i;
190 register_number = free_debug_register - DR_FIRSTADDR;
191 debug_control_mirror |=
192 ((read_write_bits | len_bits)
193 << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * register_number));
194 debug_control_mirror |=
195 (1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * register_number));
196 debug_control_mirror |= DR_LOCAL_SLOWDOWN;
197 debug_control_mirror &= ~DR_CONTROL_RESERVED;
199 ptrace (6, pid, offsetof (struct user, u_debugreg[DR_CONTROL]),
200 debug_control_mirror);
201 ptrace (6, pid, offsetof (struct user, u_debugreg[free_debug_register]),
204 /* Record where we came from. */
205 address_lookup[register_number] = addr;
210 i386_insert_nonaligned_watchpoint (int pid, CORE_ADDR waddr, CORE_ADDR addr,
217 static int size_try_array[4][4] =
219 { 1, 1, 1, 1 }, /* trying size one */
220 { 2, 1, 2, 1 }, /* trying size two */
221 { 2, 1, 2, 1 }, /* trying size three */
222 { 4, 1, 2, 1 } /* trying size four */
229 /* Four is the maximum length for 386. */
230 size = size_try_array[len > 4 ? 3 : len - 1][align];
232 rv = i386_insert_aligned_watchpoint (pid, waddr, addr, size, rw);
235 i386_remove_watchpoint (pid, waddr, size);
244 /* Remove a watchpoint. */
247 i386_remove_watchpoint (int pid, CORE_ADDR addr, int len)
252 for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
254 register_number = i - DR_FIRSTADDR;
255 if (address_lookup[register_number] == addr)
257 debug_control_mirror &=
258 ~(1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * register_number));
259 address_lookup[register_number] = 0;
262 ptrace (6, pid, offsetof (struct user, u_debugreg[DR_CONTROL]),
263 debug_control_mirror);
264 ptrace (6, pid, offsetof (struct user, u_debugreg[DR_STATUS]), 0);
269 /* Check if stopped by a watchpoint. */
272 i386_stopped_by_watchpoint (int pid)
277 status = ptrace (3, pid, offsetof (struct user, u_debugreg[DR_STATUS]), 0);
278 ptrace (6, pid, offsetof (struct user, u_debugreg[DR_STATUS]), 0);
280 for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
282 if (status & (1 << (i - DR_FIRSTADDR)))
283 return address_lookup[i - DR_FIRSTADDR];
289 #endif /* TARGET_HAS_HARDWARE_WATCHPOINTS */