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