1 /* Low level interface to i386 running the GNU Hurd.
2 Copyright 1992, 1995, 1996, 1998, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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. */
24 #include "floatformat.h"
27 #include "gdb_assert.h"
32 #include <mach_error.h>
33 #include <mach/message.h>
34 #include <mach/exception.h>
40 /* Offset to the thread_state_t location where REG is stored. */
41 #define REG_OFFSET(reg) offsetof (struct i386_thread_state, reg)
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
[] =
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
)
53 #define REG_ADDR(state, regnum) ((char *)(state) + reg_offset[regnum])
56 /* Get the whole floating-point state of THREAD and record the
57 values of the corresponding (pseudo) registers. */
59 fetch_fpregs (struct proc
*thread
)
61 mach_msg_type_number_t count
= i386_FLOAT_STATE_COUNT
;
62 struct i386_float_state state
;
65 err
= thread_get_state (thread
->port
, i386_FLOAT_STATE
,
66 (thread_state_t
) &state
, &count
);
69 warning ("Couldn't fetch floating-point state from %s",
70 proc_string (thread
));
74 if (!state
.initialized
)
75 /* The floating-point state isn't initialized. */
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
);
87 /* Supply the floating-point registers. */
88 i387_supply_fsave (state
.hw_state
);
91 /* Fetch register REGNO, or all regs if REGNO is -1. */
93 gnu_fetch_registers (int regno
)
97 /* Make sure we know about new threads. */
98 inf_update_procs (current_inferior
);
100 thread
= inf_tid_to_thread (current_inferior
, PIDGET (inferior_ptid
));
102 error ("Can't fetch registers from thread %d: No such thread",
103 PIDGET (inferior_ptid
));
105 if (regno
< NUM_GREGS
|| regno
== -1)
107 thread_state_t state
;
109 /* This does the dirty work for us. */
110 state
= proc_get_state (thread
, 0);
113 warning ("Couldn't fetch registers from %s",
114 proc_string (thread
));
122 proc_debug (thread
, "fetching all register");
124 for (i
= 0; i
< NUM_GREGS
; i
++)
125 supply_register (i
, REG_ADDR (state
, i
));
126 thread
->fetched_regs
= ~0;
130 proc_debug (thread
, "fetching register %s", REGISTER_NAME (regno
));
132 supply_register (regno
, REG_ADDR (state
, regno
));
133 thread
->fetched_regs
|= (1 << regno
);
137 if (regno
>= NUM_GREGS
|| regno
== -1)
139 proc_debug (thread
, "fetching floating-point registers");
141 fetch_fpregs (thread
);
146 /* Store the whole floating-point state into THREAD using information
147 from the corresponding (pseudo) registers. */
149 store_fpregs (struct proc
*thread
, int regno
)
151 mach_msg_type_number_t count
= i386_FLOAT_STATE_COUNT
;
152 struct i386_float_state state
;
155 err
= thread_get_state (thread
->port
, i386_FLOAT_STATE
,
156 (thread_state_t
) &state
, &count
);
159 warning ("Couldn't fetch floating-point state from %s",
160 proc_string (thread
));
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
);
168 err
= thread_set_state (thread
->port
, i386_FLOAT_STATE
,
169 (thread_state_t
) &state
, i386_FLOAT_STATE_COUNT
);
172 warning ("Couldn't store floating-point state into %s",
173 proc_string (thread
));
178 /* Store at least register REGNO, or all regs if REGNO == -1. */
180 gnu_store_registers (int regno
)
184 /* Make sure we know about new threads. */
185 inf_update_procs (current_inferior
);
187 thread
= inf_tid_to_thread (current_inferior
, PIDGET (inferior_ptid
));
189 error ("Couldn't store registers into thread %d: No such thread",
190 PIDGET (inferior_ptid
));
192 if (regno
< NUM_GREGS
|| regno
== -1)
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
;
200 if (!was_aborted
&& was_valid
)
201 memcpy (&old_state
, &thread
->state
, sizeof (old_state
));
203 state
= proc_get_state (thread
, 1);
206 warning ("Couldn't store registers into %s", proc_string (thread
));
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;
214 if (!was_aborted
&& was_valid
)
215 /* See which registers have changed after aborting the thread. */
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! */
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
));
232 warning ("... also writing this register! Suspicious...");
236 #define fill(state, regno) \
237 memcpy (REG_ADDR(state, regno), ®isters[REGISTER_BYTE (regno)], \
238 REGISTER_RAW_SIZE (regno))
244 proc_debug (thread
, "storing all registers");
246 for (i
= 0; i
< NUM_GREGS
; i
++)
247 if (register_valid
[i
])
252 proc_debug (thread
, "storing register %s", REGISTER_NAME (regno
));
254 gdb_assert (register_valid
[regno
]);
258 /* Restore the T bit. */
259 ((struct i386_thread_state
*)state
)->efl
&= ~0x100;
260 ((struct i386_thread_state
*)state
)->efl
|= trace
;
265 if (regno
>= NUM_GREGS
|| regno
== -1)
267 proc_debug (thread
, "storing floating-point registers");
269 store_fpregs (thread
, regno
);