1 /* Intel 386 native support for SYSV systems (pre-SVR4).
3 Copyright 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1998,
4 1999, 2000, 2002 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
28 #ifdef HAVE_SYS_PTRACE_H
29 #include <sys/ptrace.h>
39 #include <sys/types.h>
42 #include <sys/param.h>
46 #include <sys/ioctl.h>
49 #ifdef TARGET_HAS_HARDWARE_WATCHPOINTS
50 #include <sys/debugreg.h>
60 #include "floatformat.h"
65 /* this table must line up with REGISTER_NAMES in tm-i386v.h */
66 /* symbols like 'EAX' come from <sys/reg.h> */
75 /* blockend is the value of u.u_ar0, and points to the
76 * place where GS is stored
80 i386_register_u_addr (int blockend, int regnum)
87 /* FIXME: Should have better way to test floating point range */
88 if (regnum >= FP0_REGNUM && regnum <= (FP0_REGNUM + 7))
90 #ifdef KSTKSZ /* SCO, and others? */
91 ubase += 4 * (SS + 1) - KSTKSZ;
92 fpstate = ubase + ((char *) &u.u_fps.u_fpstate - (char *) &u);
93 return (fpstate + 0x1c + 10 * (regnum - FP0_REGNUM));
95 fpstate = ubase + ((char *) &u.i387.st_space - (char *) &u);
96 return (fpstate + 10 * (regnum - FP0_REGNUM));
101 return (ubase + 4 * regmap[regnum]);
109 return (sizeof (struct user));
112 #ifdef TARGET_HAS_HARDWARE_WATCHPOINTS
114 #if !defined (offsetof)
115 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
118 /* Record the value of the debug control register. */
119 static int debug_control_mirror;
121 /* Record which address associates with which register. */
122 static CORE_ADDR address_lookup[DR_LASTADDR - DR_FIRSTADDR + 1];
125 i386_insert_aligned_watchpoint (int, CORE_ADDR, CORE_ADDR, int, int);
128 i386_insert_nonaligned_watchpoint (int, CORE_ADDR, CORE_ADDR, int, int);
130 /* Insert a watchpoint. */
133 i386_insert_watchpoint (int pid, CORE_ADDR addr, int len, int rw)
135 return i386_insert_aligned_watchpoint (pid, addr, addr, len, rw);
139 i386_insert_aligned_watchpoint (int pid, CORE_ADDR waddr, CORE_ADDR addr,
143 int read_write_bits, len_bits;
144 int free_debug_register;
147 /* Look for a free debug register. */
148 for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
150 if (address_lookup[i - DR_FIRSTADDR] == 0)
154 /* No more debug registers! */
158 read_write_bits = (rw & 1) ? DR_RW_READ : DR_RW_WRITE;
165 return i386_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw);
172 return i386_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw);
176 return i386_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw);
178 free_debug_register = i;
179 register_number = free_debug_register - DR_FIRSTADDR;
180 debug_control_mirror |=
181 ((read_write_bits | len_bits)
182 << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * register_number));
183 debug_control_mirror |=
184 (1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * register_number));
185 debug_control_mirror |= DR_LOCAL_SLOWDOWN;
186 debug_control_mirror &= ~DR_CONTROL_RESERVED;
188 ptrace (6, pid, offsetof (struct user, u_debugreg[DR_CONTROL]),
189 debug_control_mirror);
190 ptrace (6, pid, offsetof (struct user, u_debugreg[free_debug_register]),
193 /* Record where we came from. */
194 address_lookup[register_number] = addr;
199 i386_insert_nonaligned_watchpoint (int pid, CORE_ADDR waddr, CORE_ADDR addr,
206 static int size_try_array[4][4] =
208 { 1, 1, 1, 1 }, /* trying size one */
209 { 2, 1, 2, 1 }, /* trying size two */
210 { 2, 1, 2, 1 }, /* trying size three */
211 { 4, 1, 2, 1 } /* trying size four */
218 /* Four is the maximum length for 386. */
219 size = size_try_array[len > 4 ? 3 : len - 1][align];
221 rv = i386_insert_aligned_watchpoint (pid, waddr, addr, size, rw);
224 i386_remove_watchpoint (pid, waddr, size);
233 /* Remove a watchpoint. */
236 i386_remove_watchpoint (int pid, CORE_ADDR addr, int len)
241 for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
243 register_number = i - DR_FIRSTADDR;
244 if (address_lookup[register_number] == addr)
246 debug_control_mirror &=
247 ~(1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * register_number));
248 address_lookup[register_number] = 0;
251 ptrace (6, pid, offsetof (struct user, u_debugreg[DR_CONTROL]),
252 debug_control_mirror);
253 ptrace (6, pid, offsetof (struct user, u_debugreg[DR_STATUS]), 0);
258 /* Check if stopped by a watchpoint. */
261 i386_stopped_by_watchpoint (int pid)
266 status = ptrace (3, pid, offsetof (struct user, u_debugreg[DR_STATUS]), 0);
267 ptrace (6, pid, offsetof (struct user, u_debugreg[DR_STATUS]), 0);
269 for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
271 if (status & (1 << (i - DR_FIRSTADDR)))
272 return address_lookup[i - DR_FIRSTADDR];
278 #endif /* TARGET_HAS_HARDWARE_WATCHPOINTS */