1 /* Intel 386 native support for System V 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"
64 #include "i386-tdep.h"
67 /* Mapping between the general-purpose registers in `struct user'
68 format and GDB's register array layout. */
77 /* Support for the user struct. */
79 /* Return the address of register REGNUM. BLOCKEND is the value of
80 u.u_ar0, and points to the place where GS is stored. */
83 register_u_addr (CORE_ADDR blockend, int regnum)
88 if (i386_fp_regnum_p (regnum))
90 #ifdef KSTKSZ /* SCO, and others? */
91 blockend += 4 * (SS + 1) - KSTKSZ;
92 fpstate = blockend + ((char *) &u.u_fps.u_fpstate - (char *) &u);
93 return (fpstate + 0x1c + 10 * (regnum - FP0_REGNUM));
95 fpstate = blockend + ((char *) &u.i387.st_space - (char *) &u);
96 return (fpstate + 10 * (regnum - FP0_REGNUM));
100 return (blockend + 4 * regmap[regnum]);
103 /* Return the size of the user struct. */
108 return (sizeof (struct user));
111 #ifdef TARGET_HAS_HARDWARE_WATCHPOINTS
113 #if !defined (offsetof)
114 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
117 /* Record the value of the debug control register. */
118 static int debug_control_mirror;
120 /* Record which address associates with which register. */
121 static CORE_ADDR address_lookup[DR_LASTADDR - DR_FIRSTADDR + 1];
123 static int i386_insert_aligned_watchpoint (int, CORE_ADDR, CORE_ADDR, int,
126 static int i386_insert_nonaligned_watchpoint (int, CORE_ADDR, CORE_ADDR, int,
129 /* Insert a watchpoint. */
132 i386_insert_watchpoint (int pid, CORE_ADDR addr, int len, int rw)
134 return i386_insert_aligned_watchpoint (pid, addr, addr, len, rw);
138 i386_insert_aligned_watchpoint (int pid, CORE_ADDR waddr, CORE_ADDR addr,
142 int read_write_bits, len_bits;
143 int free_debug_register;
146 /* Look for a free debug register. */
147 for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
149 if (address_lookup[i - DR_FIRSTADDR] == 0)
153 /* No more debug registers! */
157 read_write_bits = (rw & 1) ? DR_RW_READ : DR_RW_WRITE;
164 return i386_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw);
171 return i386_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw);
175 return i386_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw);
177 free_debug_register = i;
178 register_number = free_debug_register - DR_FIRSTADDR;
179 debug_control_mirror |=
180 ((read_write_bits | len_bits)
181 << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * register_number));
182 debug_control_mirror |=
183 (1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * register_number));
184 debug_control_mirror |= DR_LOCAL_SLOWDOWN;
185 debug_control_mirror &= ~DR_CONTROL_RESERVED;
187 ptrace (6, pid, offsetof (struct user, u_debugreg[DR_CONTROL]),
188 debug_control_mirror);
189 ptrace (6, pid, offsetof (struct user, u_debugreg[free_debug_register]),
192 /* Record where we came from. */
193 address_lookup[register_number] = addr;
198 i386_insert_nonaligned_watchpoint (int pid, CORE_ADDR waddr, CORE_ADDR addr,
205 static int size_try_array[4][4] =
207 { 1, 1, 1, 1 }, /* trying size one */
208 { 2, 1, 2, 1 }, /* trying size two */
209 { 2, 1, 2, 1 }, /* trying size three */
210 { 4, 1, 2, 1 } /* trying size four */
217 /* Four is the maximum length for 386. */
218 size = size_try_array[len > 4 ? 3 : len - 1][align];
220 rv = i386_insert_aligned_watchpoint (pid, waddr, addr, size, rw);
223 i386_remove_watchpoint (pid, waddr, size);
232 /* Remove a watchpoint. */
235 i386_remove_watchpoint (int pid, CORE_ADDR addr, int len)
240 for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
242 register_number = i - DR_FIRSTADDR;
243 if (address_lookup[register_number] == addr)
245 debug_control_mirror &=
246 ~(1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * register_number));
247 address_lookup[register_number] = 0;
250 ptrace (6, pid, offsetof (struct user, u_debugreg[DR_CONTROL]),
251 debug_control_mirror);
252 ptrace (6, pid, offsetof (struct user, u_debugreg[DR_STATUS]), 0);
257 /* Check if stopped by a watchpoint. */
260 i386_stopped_by_watchpoint (int pid)
265 status = ptrace (3, pid, offsetof (struct user, u_debugreg[DR_STATUS]), 0);
266 ptrace (6, pid, offsetof (struct user, u_debugreg[DR_STATUS]), 0);
268 for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
270 if (status & (1 << (i - DR_FIRSTADDR)))
271 return address_lookup[i - DR_FIRSTADDR];
277 #endif /* TARGET_HAS_HARDWARE_WATCHPOINTS */