Move putchar_filtered() to utils.c.
[platform/upstream/binutils.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,
19    Boston, MA 02111-1307, USA.  */
20
21 #include "defs.h"
22
23 #ifdef HAVE_PTRACE_H
24 #include <ptrace.h>
25 #else
26 #ifdef HAVE_SYS_PTRACE_H
27 #include <sys/ptrace.h>
28 #endif
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
56 #ifdef HAVE_ASM_DEBUGREG_H
57 #include <asm/debugreg.h>
58 #else
59 #include <sys/debugreg.h>
60 #endif
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"
73 \f
74
75 /* this table must line up with REGISTER_NAMES in tm-i386v.h */
76 /* symbols like 'EAX' come from <sys/reg.h> */
77 static int regmap[] =
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
89 int
90 i386_register_u_addr (int blockend, int regnum)
91 {
92   struct user u;
93   int fpstate;
94   int ubase;
95
96   ubase = blockend;
97   /* FIXME:  Should have better way to test floating point range */
98   if (regnum >= FP0_REGNUM && regnum <= (FP0_REGNUM + 7))
99     {
100 #ifdef KSTKSZ                   /* SCO, and others? */
101       ubase += 4 * (SS + 1) - KSTKSZ;
102       fpstate = ubase + ((char *) &u.u_fps.u_fpstate - (char *) &u);
103       return (fpstate + 0x1c + 10 * (regnum - FP0_REGNUM));
104 #else
105       fpstate = ubase + ((char *) &u.i387.st_space - (char *) &u);
106       return (fpstate + 10 * (regnum - FP0_REGNUM));
107 #endif
108     }
109   else
110     {
111       return (ubase + 4 * regmap[regnum]);
112     }
113
114 }
115 \f
116 int
117 kernel_u_size (void)
118 {
119   return (sizeof (struct user));
120 }
121 \f
122 #ifdef TARGET_HAS_HARDWARE_WATCHPOINTS
123
124 #if !defined (offsetof)
125 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
126 #endif
127
128 /* Record the value of the debug control register.  */
129 static int debug_control_mirror;
130
131 /* Record which address associates with which register.  */
132 static CORE_ADDR address_lookup[DR_LASTADDR - DR_FIRSTADDR + 1];
133
134 static int
135 i386_insert_aligned_watchpoint (int, CORE_ADDR, CORE_ADDR, int, int);
136
137 static int
138 i386_insert_nonaligned_watchpoint (int, CORE_ADDR, CORE_ADDR, int, int);
139
140 /* Insert a watchpoint.  */
141
142 int
143 i386_insert_watchpoint (int pid, CORE_ADDR addr, int len, int rw)
144 {
145   return i386_insert_aligned_watchpoint (pid, addr, addr, len, rw);
146 }
147
148 static int
149 i386_insert_aligned_watchpoint (int pid, CORE_ADDR waddr, CORE_ADDR addr,
150                                 int len, int rw)
151 {
152   int i;
153   int read_write_bits, len_bits;
154   int free_debug_register;
155   int register_number;
156
157   /* Look for a free debug register.  */
158   for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
159     {
160       if (address_lookup[i - DR_FIRSTADDR] == 0)
161         break;
162     }
163
164   /* No more debug registers!  */
165   if (i > DR_LASTADDR)
166     return -1;
167
168   read_write_bits = (rw & 1) ? DR_RW_READ : DR_RW_WRITE;
169
170   if (len == 1)
171     len_bits = DR_LEN_1;
172   else if (len == 2)
173     {
174       if (addr % 2)
175         return i386_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw);
176       len_bits = DR_LEN_2;
177     }
178
179   else if (len == 4)
180     {
181       if (addr % 4)
182         return i386_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw);
183       len_bits = DR_LEN_4;
184     }
185   else
186     return i386_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw);
187
188   free_debug_register = i;
189   register_number = free_debug_register - DR_FIRSTADDR;
190   debug_control_mirror |=
191     ((read_write_bits | len_bits)
192      << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * register_number));
193   debug_control_mirror |=
194     (1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * register_number));
195   debug_control_mirror |= DR_LOCAL_SLOWDOWN;
196   debug_control_mirror &= ~DR_CONTROL_RESERVED;
197
198   ptrace (6, pid, offsetof (struct user, u_debugreg[DR_CONTROL]),
199           debug_control_mirror);
200   ptrace (6, pid, offsetof (struct user, u_debugreg[free_debug_register]),
201           addr);
202
203   /* Record where we came from.  */
204   address_lookup[register_number] = addr;
205   return 0;
206 }
207
208 static int
209 i386_insert_nonaligned_watchpoint (int pid, CORE_ADDR waddr, CORE_ADDR addr,
210                                    int len, int rw)
211 {
212   int align;
213   int size;
214   int rv;
215
216   static int size_try_array[4][4] =
217   {
218     { 1, 1, 1, 1 },             /* trying size one */
219     { 2, 1, 2, 1 },             /* trying size two */
220     { 2, 1, 2, 1 },             /* trying size three */
221     { 4, 1, 2, 1 }              /* trying size four */
222   };
223
224   rv = 0;
225   while (len > 0)
226     {
227       align = addr % 4;
228       /* Four is the maximum length for 386.  */
229       size = size_try_array[len > 4 ? 3 : len - 1][align];
230
231       rv = i386_insert_aligned_watchpoint (pid, waddr, addr, size, rw);
232       if (rv)
233         {
234           i386_remove_watchpoint (pid, waddr, size);
235           return rv;
236         }
237       addr += size;
238       len -= size;
239     }
240   return rv;
241 }
242
243 /* Remove a watchpoint.  */
244
245 int
246 i386_remove_watchpoint (int pid, CORE_ADDR addr, int len)
247 {
248   int i;
249   int register_number;
250
251   for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
252     {
253       register_number = i - DR_FIRSTADDR;
254       if (address_lookup[register_number] == addr)
255         {
256           debug_control_mirror &=
257             ~(1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * register_number));
258           address_lookup[register_number] = 0;
259         }
260     }
261   ptrace (6, pid, offsetof (struct user, u_debugreg[DR_CONTROL]),
262           debug_control_mirror);
263   ptrace (6, pid, offsetof (struct user, u_debugreg[DR_STATUS]), 0);
264
265   return 0;
266 }
267
268 /* Check if stopped by a watchpoint.  */
269
270 CORE_ADDR
271 i386_stopped_by_watchpoint (int pid)
272 {
273   int i;
274   int status;
275
276   status = ptrace (3, pid, offsetof (struct user, u_debugreg[DR_STATUS]), 0);
277   ptrace (6, pid, offsetof (struct user, u_debugreg[DR_STATUS]), 0);
278
279   for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
280     {
281       if (status & (1 << (i - DR_FIRSTADDR)))
282         return address_lookup[i - DR_FIRSTADDR];
283     }
284
285   return 0;
286 }
287
288 #endif /* TARGET_HAS_HARDWARE_WATCHPOINTS */