Imported Upstream version 7.5
[platform/upstream/gdb.git] / gdb / i386gnu-nat.c
1 /* Low level interface to i386 running the GNU Hurd.
2
3    Copyright (C) 1992, 1995-1996, 1998, 2000-2001, 2004, 2007-2012 Free
4    Software Foundation, Inc.
5
6    This file is part of GDB.
7
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 3 of the License, or
11    (at your option) any later version.
12
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.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 #include "defs.h"
22 #include "inferior.h"
23 #include "floatformat.h"
24 #include "regcache.h"
25
26 #include "gdb_assert.h"
27 #include <errno.h>
28 #include <stdio.h>
29 #include "gdb_string.h"
30
31 #include <mach.h>
32 #include <mach_error.h>
33 #include <mach/message.h>
34 #include <mach/exception.h>
35
36 #include "i386-tdep.h"
37
38 #include "gnu-nat.h"
39 #include "i387-tdep.h"
40
41 #ifdef HAVE_SYS_PROCFS_H
42 # include <sys/procfs.h>
43 # include "gregset.h"
44 #endif
45
46 /* Offset to the thread_state_t location where REG is stored.  */
47 #define REG_OFFSET(reg) offsetof (struct i386_thread_state, reg)
48
49 /* At REG_OFFSET[N] is the offset to the thread_state_t location where
50    the GDB register N is stored.  */
51 static int reg_offset[] =
52 {
53   REG_OFFSET (eax), REG_OFFSET (ecx), REG_OFFSET (edx), REG_OFFSET (ebx),
54   REG_OFFSET (uesp), REG_OFFSET (ebp), REG_OFFSET (esi), REG_OFFSET (edi),
55   REG_OFFSET (eip), REG_OFFSET (efl), REG_OFFSET (cs), REG_OFFSET (ss),
56   REG_OFFSET (ds), REG_OFFSET (es), REG_OFFSET (fs), REG_OFFSET (gs)
57 };
58
59 #define REG_ADDR(state, regnum) ((char *)(state) + reg_offset[regnum])
60 #define CREG_ADDR(state, regnum) ((const char *)(state) + reg_offset[regnum])
61
62 \f
63 /* Get the whole floating-point state of THREAD and record the values
64    of the corresponding (pseudo) registers.  */
65
66 static void
67 fetch_fpregs (struct regcache *regcache, struct proc *thread)
68 {
69   mach_msg_type_number_t count = i386_FLOAT_STATE_COUNT;
70   struct i386_float_state state;
71   error_t err;
72
73   err = thread_get_state (thread->port, i386_FLOAT_STATE,
74                           (thread_state_t) &state, &count);
75   if (err)
76     {
77       warning (_("Couldn't fetch floating-point state from %s"),
78                proc_string (thread));
79       return;
80     }
81
82   if (!state.initialized)
83     {
84       /* The floating-point state isn't initialized.  */
85       i387_supply_fsave (regcache, -1, NULL);
86     }
87   else
88     {
89       /* Supply the floating-point registers.  */
90       i387_supply_fsave (regcache, -1, state.hw_state);
91     }
92 }
93
94 #ifdef HAVE_SYS_PROCFS_H
95 /* These two calls are used by the core-regset.c code for
96    reading ELF core files.  */
97 void
98 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregs)
99 {
100   int i;
101   for (i = 0; i < I386_NUM_GREGS; i++)
102     regcache_raw_supply (regcache, i, CREG_ADDR (gregs, i));
103 }
104
105 void
106 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregs)
107 {
108   i387_supply_fsave (regcache, -1, fpregs);
109 }
110 #endif
111
112 /* Fetch register REGNO, or all regs if REGNO is -1.  */
113 static void
114 gnu_fetch_registers (struct target_ops *ops,
115                      struct regcache *regcache, int regno)
116 {
117   struct proc *thread;
118
119   /* Make sure we know about new threads.  */
120   inf_update_procs (gnu_current_inf);
121
122   thread = inf_tid_to_thread (gnu_current_inf,
123                               ptid_get_tid (inferior_ptid));
124   if (!thread)
125     error (_("Can't fetch registers from thread %s: No such thread"),
126            target_pid_to_str (inferior_ptid));
127
128   if (regno < I386_NUM_GREGS || regno == -1)
129     {
130       thread_state_t state;
131
132       /* This does the dirty work for us.  */
133       state = proc_get_state (thread, 0);
134       if (!state)
135         {
136           warning (_("Couldn't fetch registers from %s"),
137                    proc_string (thread));
138           return;
139         }
140
141       if (regno == -1)
142         {
143           int i;
144
145           proc_debug (thread, "fetching all register");
146
147           for (i = 0; i < I386_NUM_GREGS; i++)
148             regcache_raw_supply (regcache, i, REG_ADDR (state, i));
149           thread->fetched_regs = ~0;
150         }
151       else
152         {
153           proc_debug (thread, "fetching register %s",
154                       gdbarch_register_name (get_regcache_arch (regcache),
155                                              regno));
156
157           regcache_raw_supply (regcache, regno,
158                                REG_ADDR (state, regno));
159           thread->fetched_regs |= (1 << regno);
160         }
161     }
162
163   if (regno >= I386_NUM_GREGS || regno == -1)
164     {
165       proc_debug (thread, "fetching floating-point registers");
166
167       fetch_fpregs (regcache, thread);
168     }
169 }
170 \f
171
172 /* Store the whole floating-point state into THREAD using information
173    from the corresponding (pseudo) registers.  */
174 static void
175 store_fpregs (const struct regcache *regcache, struct proc *thread, int regno)
176 {
177   mach_msg_type_number_t count = i386_FLOAT_STATE_COUNT;
178   struct i386_float_state state;
179   error_t err;
180
181   err = thread_get_state (thread->port, i386_FLOAT_STATE,
182                           (thread_state_t) &state, &count);
183   if (err)
184     {
185       warning (_("Couldn't fetch floating-point state from %s"),
186                proc_string (thread));
187       return;
188     }
189
190   /* FIXME: kettenis/2001-07-15: Is this right?  Should we somehow
191      take into account DEPRECATED_REGISTER_VALID like the old code did?  */
192   i387_collect_fsave (regcache, regno, state.hw_state);
193
194   err = thread_set_state (thread->port, i386_FLOAT_STATE,
195                           (thread_state_t) &state, i386_FLOAT_STATE_COUNT);
196   if (err)
197     {
198       warning (_("Couldn't store floating-point state into %s"),
199                proc_string (thread));
200       return;
201     }
202 }
203
204 /* Store at least register REGNO, or all regs if REGNO == -1.  */
205 static void
206 gnu_store_registers (struct target_ops *ops,
207                      struct regcache *regcache, int regno)
208 {
209   struct proc *thread;
210   struct gdbarch *gdbarch = get_regcache_arch (regcache);
211
212   /* Make sure we know about new threads.  */
213   inf_update_procs (gnu_current_inf);
214
215   thread = inf_tid_to_thread (gnu_current_inf,
216                               ptid_get_tid (inferior_ptid));
217   if (!thread)
218     error (_("Couldn't store registers into thread %s: No such thread"),
219            target_pid_to_str (inferior_ptid));
220
221   if (regno < I386_NUM_GREGS || regno == -1)
222     {
223       thread_state_t state;
224       thread_state_data_t old_state;
225       int was_aborted = thread->aborted;
226       int was_valid = thread->state_valid;
227       int trace;
228
229       if (!was_aborted && was_valid)
230         memcpy (&old_state, &thread->state, sizeof (old_state));
231
232       state = proc_get_state (thread, 1);
233       if (!state)
234         {
235           warning (_("Couldn't store registers into %s"),
236                    proc_string (thread));
237           return;
238         }
239
240       /* Save the T bit.  We might try to restore the %eflags register
241          below, but changing the T bit would seriously confuse GDB.  */
242       trace = ((struct i386_thread_state *)state)->efl & 0x100;
243
244       if (!was_aborted && was_valid)
245         /* See which registers have changed after aborting the thread.  */
246         {
247           int check_regno;
248
249           for (check_regno = 0; check_regno < I386_NUM_GREGS; check_regno++)
250             if ((thread->fetched_regs & (1 << check_regno))
251                 && memcpy (REG_ADDR (&old_state, check_regno),
252                            REG_ADDR (state, check_regno),
253                            register_size (gdbarch, check_regno)))
254               /* Register CHECK_REGNO has changed!  Ack!  */
255               {
256                 warning (_("Register %s changed after the thread was aborted"),
257                          gdbarch_register_name (gdbarch, check_regno));
258                 if (regno >= 0 && regno != check_regno)
259                   /* Update GDB's copy of the register.  */
260                   regcache_raw_supply (regcache, check_regno,
261                                        REG_ADDR (state, check_regno));
262                 else
263                   warning (_("... also writing this register!  "
264                              "Suspicious..."));
265               }
266         }
267
268       if (regno == -1)
269         {
270           int i;
271
272           proc_debug (thread, "storing all registers");
273
274           for (i = 0; i < I386_NUM_GREGS; i++)
275             if (REG_VALID == regcache_register_status (regcache, i))
276               regcache_raw_collect (regcache, i, REG_ADDR (state, i));
277         }
278       else
279         {
280           proc_debug (thread, "storing register %s",
281                       gdbarch_register_name (gdbarch, regno));
282
283           gdb_assert (REG_VALID == regcache_register_status (regcache, regno));
284           regcache_raw_collect (regcache, regno, REG_ADDR (state, regno));
285         }
286
287       /* Restore the T bit.  */
288       ((struct i386_thread_state *)state)->efl &= ~0x100;
289       ((struct i386_thread_state *)state)->efl |= trace;
290     }
291
292   if (regno >= I386_NUM_GREGS || regno == -1)
293     {
294       proc_debug (thread, "storing floating-point registers");
295
296       store_fpregs (regcache, thread, regno);
297     }
298 }
299
300 /* Provide a prototype to silence -Wmissing-prototypes.  */
301 extern initialize_file_ftype _initialize_i386gnu_nat;
302
303 void
304 _initialize_i386gnu_nat (void)
305 {
306   struct target_ops *t;
307
308   /* Fill in the generic GNU/Hurd methods.  */
309   t = gnu_target ();
310
311   t->to_fetch_registers = gnu_fetch_registers;
312   t->to_store_registers = gnu_store_registers;
313
314   /* Register the target.  */
315   add_target (t);
316 }