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