]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/i386v-nat.c
Initial creation of sourceware repository
[thirdparty/binutils-gdb.git] / gdb / i386v-nat.c
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
4 This file is part of GDB.
5
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.
10
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.
15
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. */
19
20 #include "defs.h"
21
22 #ifdef HAVE_PTRACE_H
23 # include <ptrace.h>
24 #else
25 # ifdef HAVE_SYS_PTRACE_H
26 # include <sys/ptrace.h>
27 # endif
28 #endif
29
30 #include "frame.h"
31 #include "inferior.h"
32 #include "language.h"
33 #include "gdbcore.h"
34
35 #ifdef USG
36 #include <sys/types.h>
37 #endif
38
39 #include <sys/param.h>
40 #include <sys/dir.h>
41 #include <signal.h>
42 #include <sys/user.h>
43 #include <sys/ioctl.h>
44 #include <fcntl.h>
45
46
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.
51 * --jsm 1998-10-21
52 */
53
54 #ifdef TARGET_HAS_HARDWARE_WATCHPOINTS
55 # ifdef HAVE_ASM_DEBUGREG_H
56 # include <asm/debugreg.h>
57 # else
58 # include <sys/debugreg.h>
59 # endif
60 #endif
61
62 #include <sys/file.h>
63 #include "gdb_stat.h"
64
65 #ifdef HAVE_SYS_REG_H
66 #include <sys/reg.h>
67 #endif
68
69 #include "floatformat.h"
70
71 #include "target.h"
72
73 \f
74 /* this table must line up with REGISTER_NAMES in tm-i386v.h */
75 /* symbols like 'EAX' come from <sys/reg.h> */
76 static int regmap[] =
77 {
78 EAX, ECX, EDX, EBX,
79 UESP, EBP, ESI, EDI,
80 EIP, EFL, CS, SS,
81 DS, ES, FS, GS,
82 };
83
84 /* blockend is the value of u.u_ar0, and points to the
85 * place where GS is stored
86 */
87
88 int
89 i386_register_u_addr (blockend, regnum)
90 int blockend;
91 int regnum;
92 {
93 struct user u;
94 int fpstate;
95 int ubase;
96
97 ubase = blockend;
98 /* FIXME: Should have better way to test floating point range */
99 if (regnum >= FP0_REGNUM && regnum <= (FP0_REGNUM + 7))
100 {
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));
105 #else
106 fpstate = ubase + ((char *)&u.i387.st_space - (char *)&u);
107 return (fpstate + 10 * (regnum - FP0_REGNUM));
108 #endif
109 }
110 else
111 {
112 return (ubase + 4 * regmap[regnum]);
113 }
114
115 }
116 \f
117 int
118 kernel_u_size ()
119 {
120 return (sizeof (struct user));
121 }
122 \f
123 #ifdef TARGET_HAS_HARDWARE_WATCHPOINTS
124
125 #if !defined (offsetof)
126 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
127 #endif
128
129 /* Record the value of the debug control register. */
130 static int debug_control_mirror;
131
132 /* Record which address associates with which register. */
133 static CORE_ADDR address_lookup[DR_LASTADDR - DR_FIRSTADDR + 1];
134
135 static int
136 i386_insert_aligned_watchpoint PARAMS ((int, CORE_ADDR, CORE_ADDR, int,
137 int));
138
139 static int
140 i386_insert_nonaligned_watchpoint PARAMS ((int, CORE_ADDR, CORE_ADDR, int,
141 int));
142
143 /* Insert a watchpoint. */
144
145 int
146 i386_insert_watchpoint (pid, addr, len, rw)
147 int pid;
148 CORE_ADDR addr;
149 int len;
150 int rw;
151 {
152 return i386_insert_aligned_watchpoint (pid, addr, addr, len, rw);
153 }
154
155 static int
156 i386_insert_aligned_watchpoint (pid, waddr, addr, len, rw)
157 int pid;
158 CORE_ADDR waddr;
159 CORE_ADDR addr;
160 int len;
161 int rw;
162 {
163 int i;
164 int read_write_bits, len_bits;
165 int free_debug_register;
166 int register_number;
167
168 /* Look for a free debug register. */
169 for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
170 {
171 if (address_lookup[i - DR_FIRSTADDR] == 0)
172 break;
173 }
174
175 /* No more debug registers! */
176 if (i > DR_LASTADDR)
177 return -1;
178
179 read_write_bits = (rw & 1) ? DR_RW_READ : DR_RW_WRITE;
180
181 if (len == 1)
182 len_bits = DR_LEN_1;
183 else if (len == 2)
184 {
185 if (addr % 2)
186 return i386_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw);
187 len_bits = DR_LEN_2;
188 }
189
190 else if (len == 4)
191 {
192 if (addr % 4)
193 return i386_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw);
194 len_bits = DR_LEN_4;
195 }
196 else
197 return i386_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw);
198
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;
208
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]),
212 addr);
213
214 /* Record where we came from. */
215 address_lookup[register_number] = addr;
216 return 0;
217 }
218
219 static int
220 i386_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw)
221 int pid;
222 CORE_ADDR waddr;
223 CORE_ADDR addr;
224 int len;
225 int rw;
226 {
227 int align;
228 int size;
229 int rv;
230
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 */
236 };
237
238 rv = 0;
239 while (len > 0)
240 {
241 align = addr % 4;
242 /* Four is the maximum length for 386. */
243 size = (len > 4) ? 3 : len - 1;
244 size = size_try_array[size * 4 + align];
245
246 rv = i386_insert_aligned_watchpoint (pid, waddr, addr, size, rw);
247 if (rv)
248 {
249 i386_remove_watchpoint (pid, waddr, size);
250 return rv;
251 }
252 addr += size;
253 len -= size;
254 }
255 return rv;
256 }
257
258 /* Remove a watchpoint. */
259
260 int
261 i386_remove_watchpoint (pid, addr, len)
262 int pid;
263 CORE_ADDR addr;
264 int len;
265 {
266 int i;
267 int register_number;
268
269 for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
270 {
271 register_number = i - DR_FIRSTADDR;
272 if (address_lookup[register_number] == addr)
273 {
274 debug_control_mirror &=
275 ~(1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * register_number));
276 address_lookup[register_number] = 0;
277 }
278 }
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);
282
283 return 0;
284 }
285
286 /* Check if stopped by a watchpoint. */
287
288 CORE_ADDR
289 i386_stopped_by_watchpoint (pid)
290 int pid;
291 {
292 int i;
293 int status;
294
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);
297
298 for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
299 {
300 if (status & (1 << (i - DR_FIRSTADDR)))
301 return address_lookup[i - DR_FIRSTADDR];
302 }
303
304 return 0;
305 }
306
307 #endif /* TARGET_HAS_HARDWARE_WATCHPOINTS */
308
309 #if 0
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.
314
315 A fix might be to move this code into the "info registers" command.
316 rich@cygnus.com 15 Sept 92. */
317 i386_float_info ()
318 {
319 struct user u; /* just for address computations */
320 int i;
321 /* fpstate defined in <sys/user.h> */
322 struct fpstate *fpstatep;
323 char buf[sizeof (struct fpstate) + 2 * sizeof (int)];
324 unsigned int uaddr;
325 char fpvalid = 0;
326 unsigned int rounded_addr;
327 unsigned int rounded_size;
328 extern int corechan;
329 int skip;
330
331 uaddr = (char *)&u.u_fpvalid - (char *)&u;
332 if (target_has_execution)
333 {
334 unsigned int data;
335 unsigned int mask;
336
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);
340
341 fpvalid = ((data & mask) != 0);
342 }
343 #if 0
344 else
345 {
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");
350
351 }
352 #endif /* no core support yet */
353
354 if (fpvalid == 0)
355 {
356 printf_unfiltered ("no floating point status saved\n");
357 return;
358 }
359
360 uaddr = (char *)&U_FPSTATE(u) - (char *)&u;
361 if (target_has_execution)
362 {
363 int *ip;
364
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;
369
370 ip = (int *)buf;
371 for (i = 0; i < rounded_size; i++)
372 {
373 *ip++ = ptrace (3, inferior_pid, (PTRACE_ARG3_TYPE) rounded_addr, 0);
374 rounded_addr += sizeof (int);
375 }
376 }
377 #if 0
378 else
379 {
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");
384 skip = 0;
385 }
386 #endif /* 0 */
387
388 fpstatep = (struct fpstate *)(buf + skip);
389 print_387_status (fpstatep->status, (struct env387 *)fpstatep->state);
390 }
391
392 #endif /* never */