PARAMS removal.
[deliverable/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
4e326852 4 This file is part of GDB.
c906108c 5
4e326852
JB
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
4e326852
JB
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
4e326852
JB
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
4e326852 24#include <ptrace.h>
c906108c 25#else
4e326852
JB
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
4e326852
JB
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"
77d2aaae 73\f
4e326852 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> */
4e326852 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 */
4e326852 100 if (regnum >= FP0_REGNUM && regnum <= (FP0_REGNUM + 7))
c906108c 101 {
4e326852 102#ifdef KSTKSZ /* SCO, and others? */
c906108c 103 ubase += 4 * (SS + 1) - KSTKSZ;
4e326852 104 fpstate = ubase + ((char *) &u.u_fps.u_fpstate - (char *) &u);
c906108c
SS
105 return (fpstate + 0x1c + 10 * (regnum - FP0_REGNUM));
106#else
4e326852 107 fpstate = ubase + ((char *) &u.i387.st_space - (char *) &u);
c906108c
SS
108 return (fpstate + 10 * (regnum - FP0_REGNUM));
109#endif
4e326852 110 }
c906108c
SS
111 else
112 {
113 return (ubase + 4 * regmap[regnum]);
114 }
4e326852 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
a14ed312 137i386_insert_aligned_watchpoint (int, CORE_ADDR, CORE_ADDR, int, int);
c906108c
SS
138
139static int
a14ed312 140i386_insert_nonaligned_watchpoint (int, CORE_ADDR, CORE_ADDR, int, int);
c906108c
SS
141
142/* Insert a watchpoint. */
143
144int
145i386_insert_watchpoint (pid, addr, len, rw)
146 int pid;
147 CORE_ADDR addr;
148 int len;
149 int rw;
150{
151 return i386_insert_aligned_watchpoint (pid, addr, addr, len, rw);
152}
153
154static int
155i386_insert_aligned_watchpoint (pid, waddr, addr, len, rw)
156 int pid;
157 CORE_ADDR waddr;
158 CORE_ADDR addr;
159 int len;
160 int rw;
161{
162 int i;
163 int read_write_bits, len_bits;
164 int free_debug_register;
165 int register_number;
4e326852 166
c906108c
SS
167 /* Look for a free debug register. */
168 for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
169 {
170 if (address_lookup[i - DR_FIRSTADDR] == 0)
171 break;
172 }
173
174 /* No more debug registers! */
175 if (i > DR_LASTADDR)
176 return -1;
177
178 read_write_bits = (rw & 1) ? DR_RW_READ : DR_RW_WRITE;
179
180 if (len == 1)
181 len_bits = DR_LEN_1;
182 else if (len == 2)
183 {
184 if (addr % 2)
185 return i386_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw);
186 len_bits = DR_LEN_2;
187 }
188
189 else if (len == 4)
190 {
191 if (addr % 4)
192 return i386_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw);
193 len_bits = DR_LEN_4;
194 }
195 else
196 return i386_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw);
4e326852 197
c906108c
SS
198 free_debug_register = i;
199 register_number = free_debug_register - DR_FIRSTADDR;
200 debug_control_mirror |=
201 ((read_write_bits | len_bits)
202 << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * register_number));
203 debug_control_mirror |=
204 (1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * register_number));
205 debug_control_mirror |= DR_LOCAL_SLOWDOWN;
206 debug_control_mirror &= ~DR_CONTROL_RESERVED;
4e326852 207
c906108c
SS
208 ptrace (6, pid, offsetof (struct user, u_debugreg[DR_CONTROL]),
209 debug_control_mirror);
210 ptrace (6, pid, offsetof (struct user, u_debugreg[free_debug_register]),
211 addr);
212
213 /* Record where we came from. */
214 address_lookup[register_number] = addr;
215 return 0;
216}
217
218static int
219i386_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw)
220 int pid;
221 CORE_ADDR waddr;
222 CORE_ADDR addr;
223 int len;
224 int rw;
225{
226 int align;
227 int size;
228 int rv;
229
ceef0e30 230 static int size_try_array[4][4] =
4e326852 231 {
ceef0e30
JB
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 */
c906108c
SS
236 };
237
238 rv = 0;
239 while (len > 0)
240 {
241 align = addr % 4;
242 /* Four is the maximum length for 386. */
ceef0e30 243 size = size_try_array[len > 4 ? 3 : len - 1][align];
c906108c
SS
244
245 rv = i386_insert_aligned_watchpoint (pid, waddr, addr, size, rw);
246 if (rv)
247 {
248 i386_remove_watchpoint (pid, waddr, size);
249 return rv;
250 }
251 addr += size;
252 len -= size;
253 }
254 return rv;
255}
256
257/* Remove a watchpoint. */
258
259int
260i386_remove_watchpoint (pid, addr, len)
261 int pid;
262 CORE_ADDR addr;
263 int len;
264{
265 int i;
266 int register_number;
267
268 for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
269 {
270 register_number = i - DR_FIRSTADDR;
271 if (address_lookup[register_number] == addr)
272 {
273 debug_control_mirror &=
274 ~(1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * register_number));
275 address_lookup[register_number] = 0;
276 }
277 }
278 ptrace (6, pid, offsetof (struct user, u_debugreg[DR_CONTROL]),
279 debug_control_mirror);
280 ptrace (6, pid, offsetof (struct user, u_debugreg[DR_STATUS]), 0);
281
282 return 0;
283}
284
285/* Check if stopped by a watchpoint. */
286
287CORE_ADDR
288i386_stopped_by_watchpoint (pid)
4e326852 289 int pid;
c906108c
SS
290{
291 int i;
292 int status;
293
294 status = ptrace (3, pid, offsetof (struct user, u_debugreg[DR_STATUS]), 0);
295 ptrace (6, pid, offsetof (struct user, u_debugreg[DR_STATUS]), 0);
296
297 for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
298 {
299 if (status & (1 << (i - DR_FIRSTADDR)))
300 return address_lookup[i - DR_FIRSTADDR];
301 }
302
303 return 0;
304}
305
306#endif /* TARGET_HAS_HARDWARE_WATCHPOINTS */
This page took 0.065491 seconds and 4 git commands to generate.