OSDN Git Service

* i386gnu-nat.c: Include "i387-nat.h".
[pf3gnuchains/sourceware.git] / gdb / i386gnu-nat.c
1 /* Low level interface to i386 running the GNU Hurd.
2    Copyright 1992, 1995, 1996, 1998, 2000, 2001
3    Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include "defs.h"
23 #include "inferior.h"
24 #include "floatformat.h"
25 #include "regcache.h"
26
27 #include "gdb_assert.h"
28 #include <errno.h>
29 #include <stdio.h>
30
31 #include <mach.h>
32 #include <mach_error.h>
33 #include <mach/message.h>
34 #include <mach/exception.h>
35
36 #include "gnu-nat.h"
37 #include "i387-nat.h"
38
39
40 /* Offset to the thread_state_t location where REG is stored.  */
41 #define REG_OFFSET(reg) offsetof (struct i386_thread_state, reg)
42
43 /* At REG_OFFSET[N] is the offset to the thread_state_t location where
44    the GDB register N is stored.  */
45 static int reg_offset[] =
46 {
47   REG_OFFSET (eax), REG_OFFSET (ecx), REG_OFFSET (edx), REG_OFFSET (ebx),
48   REG_OFFSET (uesp), REG_OFFSET (ebp), REG_OFFSET (esi), REG_OFFSET (edi),
49   REG_OFFSET (eip), REG_OFFSET (efl), REG_OFFSET (cs), REG_OFFSET (ss),
50   REG_OFFSET (ds), REG_OFFSET (es), REG_OFFSET (fs), REG_OFFSET (gs)
51 };
52
53 #define REG_ADDR(state, regnum) ((char *)(state) + reg_offset[regnum])
54
55 \f
56 /* Get the whole floating-point state of THREAD and record the
57    values of the corresponding (pseudo) registers.  */
58 static void
59 fetch_fpregs (struct proc *thread)
60 {
61   mach_msg_type_number_t count = i386_FLOAT_STATE_COUNT;
62   struct i386_float_state state;
63   error_t err;
64
65   err = thread_get_state (thread->port, i386_FLOAT_STATE,
66                           (thread_state_t) &state, &count);
67   if (err)
68     {
69       warning ("Couldn't fetch floating-point state from %s",
70                proc_string (thread));
71       return;
72     }
73
74   if (!state.initialized)
75     /* The floating-point state isn't initialized.  */
76     {
77       int i;
78
79       for (i = FP0_REGNUM; i <= FP7_REGNUM; i++)
80         supply_register (i, NULL);
81       for (i = FCTRL_REGNUM; i <= FOP_REGNUM; i++)
82         supply_register (i, NULL);
83
84       return;
85     }
86
87   /* Supply the floating-point registers.  */
88   i387_supply_fsave (state.hw_state);
89 }
90
91 /* Fetch register REGNO, or all regs if REGNO is -1.  */
92 void
93 gnu_fetch_registers (int regno)
94 {
95   struct proc *thread;
96
97   /* Make sure we know about new threads.  */
98   inf_update_procs (current_inferior);
99
100   thread = inf_tid_to_thread (current_inferior, PIDGET (inferior_ptid));
101   if (!thread)
102     error ("Can't fetch registers from thread %d: No such thread",
103            PIDGET (inferior_ptid));
104
105   if (regno < NUM_GREGS || regno == -1)
106     {
107       thread_state_t state;
108
109       /* This does the dirty work for us.  */
110       state = proc_get_state (thread, 0);
111       if (!state)
112         {
113           warning ("Couldn't fetch registers from %s",
114                    proc_string (thread));
115           return;
116         }
117
118       if (regno == -1)
119         {
120           int i;
121
122           proc_debug (thread, "fetching all register");
123
124           for (i = 0; i < NUM_GREGS; i++)
125             supply_register (i, REG_ADDR (state, i));
126           thread->fetched_regs = ~0;
127         }
128       else
129         {
130           proc_debug (thread, "fetching register %s", REGISTER_NAME (regno));
131
132           supply_register (regno, REG_ADDR (state, regno));
133           thread->fetched_regs |= (1 << regno);
134         }
135     }
136
137   if (regno >= NUM_GREGS || regno == -1)
138     {
139       proc_debug (thread, "fetching floating-point registers");
140
141       fetch_fpregs (thread);
142     }
143 }
144 \f
145
146 /* Store the whole floating-point state into THREAD using information
147    from the corresponding (pseudo) registers.  */
148 static void
149 store_fpregs (struct proc *thread, int regno)
150 {
151   mach_msg_type_number_t count = i386_FLOAT_STATE_COUNT;
152   struct i386_float_state state;
153   error_t err;
154
155   err = thread_get_state (thread->port, i386_FLOAT_STATE,
156                           (thread_state_t) &state, &count);
157   if (err)
158     {
159       warning ("Couldn't fetch floating-point state from %s",
160                proc_string (thread));
161       return;
162     }
163
164   /* FIXME: kettenis/2001-07-15: Is this right?  Should we somehow
165      take into account REGISTER_VALID like the old code did?  */
166   i387_fill_fsave (state.hw_state, regno);
167
168   err = thread_set_state (thread->port, i386_FLOAT_STATE,
169                           (thread_state_t) &state, i386_FLOAT_STATE_COUNT);
170   if (err)
171     {
172       warning ("Couldn't store floating-point state into %s",
173                proc_string (thread));
174       return;
175     }
176 }
177
178 /* Store at least register REGNO, or all regs if REGNO == -1.  */
179 void
180 gnu_store_registers (int regno)
181 {
182   struct proc *thread;
183
184   /* Make sure we know about new threads.  */
185   inf_update_procs (current_inferior);
186
187   thread = inf_tid_to_thread (current_inferior, PIDGET (inferior_ptid));
188   if (!thread)
189     error ("Couldn't store registers into thread %d: No such thread",
190            PIDGET (inferior_ptid));
191
192   if (regno < NUM_GREGS || regno == -1)
193     {
194       thread_state_t state;
195       thread_state_data_t old_state;
196       int was_aborted = thread->aborted;
197       int was_valid = thread->state_valid;
198       int trace;
199
200       if (!was_aborted && was_valid)
201         memcpy (&old_state, &thread->state, sizeof (old_state));
202
203       state = proc_get_state (thread, 1);
204       if (!state)
205         {
206           warning ("Couldn't store registers into %s", proc_string (thread));
207           return;
208         }
209
210       /* Save the T bit.  We might try to restore the %eflags register
211          below, but changing the T bit would seriously confuse GDB.  */
212       trace = ((struct i386_thread_state *)state)->efl & 0x100;
213
214       if (!was_aborted && was_valid)
215         /* See which registers have changed after aborting the thread.  */
216         {
217           int check_regno;
218
219           for (check_regno = 0; check_regno < NUM_GREGS; check_regno++)
220             if ((thread->fetched_regs & (1 << check_regno))
221                 && memcpy (REG_ADDR (&old_state, check_regno),
222                            REG_ADDR (state, check_regno),
223                            REGISTER_RAW_SIZE (check_regno)))
224               /* Register CHECK_REGNO has changed!  Ack!  */
225               {
226                 warning ("Register %s changed after the thread was aborted",
227                          REGISTER_NAME (check_regno));
228                 if (regno >= 0 && regno != check_regno)
229                   /* Update GDB's copy of the register.  */
230                   supply_register (check_regno, REG_ADDR (state, check_regno));
231                 else
232                   warning ("... also writing this register!  Suspicious...");
233               }
234         }
235
236 #define fill(state, regno)                                               \
237   memcpy (REG_ADDR(state, regno), &registers[REGISTER_BYTE (regno)],     \
238           REGISTER_RAW_SIZE (regno))
239
240       if (regno == -1)
241         {
242           int i;
243
244           proc_debug (thread, "storing all registers");
245
246           for (i = 0; i < NUM_GREGS; i++)
247             if (register_valid[i])
248               fill (state, i);
249         }
250       else
251         {
252           proc_debug (thread, "storing register %s", REGISTER_NAME (regno));
253
254           gdb_assert (register_valid[regno]);
255           fill (state, regno);
256         }
257
258       /* Restore the T bit.  */
259       ((struct i386_thread_state *)state)->efl &= ~0x100;
260       ((struct i386_thread_state *)state)->efl |= trace;
261     }
262
263 #undef fill
264
265   if (regno >= NUM_GREGS || regno == -1)
266     {
267       proc_debug (thread, "storing floating-point registers");
268
269       store_fpregs (thread, regno);
270     }
271 }