1 /* Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
3 This file is part of GDB.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "win32-low.h"
25 #define FLAG_TRACE_BIT 0x100
27 /* Defined in auto-generated file reg-i386.c. */
28 void init_registers_i386 (void);
30 static struct i386_debug_reg_state debug_reg_state
;
32 static int debug_registers_changed
= 0;
33 static int debug_registers_used
= 0;
35 /* Update the inferior's debug register REGNUM from STATE. */
38 i386_dr_low_set_addr (const struct i386_debug_reg_state
*state
, int regnum
)
40 if (! (regnum
>= 0 && regnum
<= DR_LASTADDR
- DR_FIRSTADDR
))
41 fatal ("Invalid debug register %d", regnum
);
43 /* debug_reg_state.dr_mirror is already set.
44 Just notify i386_set_thread_context, i386_thread_added
45 that the registers need to be updated. */
46 debug_registers_changed
= 1;
47 debug_registers_used
= 1;
50 /* Update the inferior's DR7 debug control register from STATE. */
53 i386_dr_low_set_control (const struct i386_debug_reg_state
*state
)
55 /* debug_reg_state.dr_control_mirror is already set.
56 Just notify i386_set_thread_context, i386_thread_added
57 that the registers need to be updated. */
58 debug_registers_changed
= 1;
59 debug_registers_used
= 1;
62 /* Get the value of the DR6 debug status register from the inferior
63 and record it in STATE. */
66 i386_dr_low_get_status (struct i386_debug_reg_state
*state
)
68 /* We don't need to do anything here, the last call to thread_rec for
69 current_event.dwThreadId id has already set it. */
72 /* Watchpoint support. */
75 i386_insert_point (char type
, CORE_ADDR addr
, int len
)
82 return i386_low_insert_watchpoint (&debug_reg_state
,
91 i386_remove_point (char type
, CORE_ADDR addr
, int len
)
98 return i386_low_remove_watchpoint (&debug_reg_state
,
107 i386_stopped_by_watchpoint (void)
109 return i386_low_stopped_by_watchpoint (&debug_reg_state
);
113 i386_stopped_data_address (void)
116 if (i386_low_stopped_data_address (&debug_reg_state
, &addr
))
122 i386_initial_stuff (void)
124 i386_low_init_dregs (&debug_reg_state
);
125 debug_registers_changed
= 0;
126 debug_registers_used
= 0;
130 i386_get_thread_context (win32_thread_info
*th
, DEBUG_EVENT
* current_event
)
132 /* Requesting the CONTEXT_EXTENDED_REGISTERS register set fails if
133 the system doesn't support extended registers. */
134 static DWORD extended_registers
= CONTEXT_EXTENDED_REGISTERS
;
137 th
->context
.ContextFlags
= (CONTEXT_FULL
138 | CONTEXT_FLOATING_POINT
139 | CONTEXT_DEBUG_REGISTERS
140 | extended_registers
);
142 if (!GetThreadContext (th
->h
, &th
->context
))
144 DWORD e
= GetLastError ();
146 if (extended_registers
&& e
== ERROR_INVALID_PARAMETER
)
148 extended_registers
= 0;
152 error ("GetThreadContext failure %ld\n", (long) e
);
155 debug_registers_changed
= 0;
157 if (th
->tid
== current_event
->dwThreadId
)
159 /* Copy dr values from the current thread. */
160 struct i386_debug_reg_state
*dr
= &debug_reg_state
;
161 dr
->dr_mirror
[0] = th
->context
.Dr0
;
162 dr
->dr_mirror
[1] = th
->context
.Dr1
;
163 dr
->dr_mirror
[2] = th
->context
.Dr2
;
164 dr
->dr_mirror
[3] = th
->context
.Dr3
;
165 dr
->dr_status_mirror
= th
->context
.Dr6
;
166 dr
->dr_control_mirror
= th
->context
.Dr7
;
171 i386_set_thread_context (win32_thread_info
*th
, DEBUG_EVENT
* current_event
)
173 if (debug_registers_changed
)
175 struct i386_debug_reg_state
*dr
= &debug_reg_state
;
176 th
->context
.Dr0
= dr
->dr_mirror
[0];
177 th
->context
.Dr1
= dr
->dr_mirror
[1];
178 th
->context
.Dr2
= dr
->dr_mirror
[2];
179 th
->context
.Dr3
= dr
->dr_mirror
[3];
180 /* th->context.Dr6 = dr->dr_status_mirror;
181 FIXME: should we set dr6 also ?? */
182 th
->context
.Dr7
= dr
->dr_control_mirror
;
185 SetThreadContext (th
->h
, &th
->context
);
189 i386_thread_added (win32_thread_info
*th
)
191 /* Set the debug registers for the new thread if they are used. */
192 if (debug_registers_used
)
194 struct i386_debug_reg_state
*dr
= &debug_reg_state
;
195 th
->context
.ContextFlags
= CONTEXT_DEBUG_REGISTERS
;
196 GetThreadContext (th
->h
, &th
->context
);
198 th
->context
.Dr0
= dr
->dr_mirror
[0];
199 th
->context
.Dr1
= dr
->dr_mirror
[1];
200 th
->context
.Dr2
= dr
->dr_mirror
[2];
201 th
->context
.Dr3
= dr
->dr_mirror
[3];
202 /* th->context.Dr6 = dr->dr_status_mirror;
203 FIXME: should we set dr6 also ?? */
204 th
->context
.Dr7
= dr
->dr_control_mirror
;
206 SetThreadContext (th
->h
, &th
->context
);
207 th
->context
.ContextFlags
= 0;
212 i386_single_step (win32_thread_info
*th
)
214 th
->context
.EFlags
|= FLAG_TRACE_BIT
;
217 /* An array of offset mappings into a Win32 Context structure.
218 This is a one-to-one mapping which is indexed by gdb's register
219 numbers. It retrieves an offset into the context structure where
220 the 4 byte register is located.
221 An offset value of -1 indicates that Win32 does not provide this
222 register in it's CONTEXT structure. In this case regptr will return
223 a pointer into a dummy register. */
224 #define context_offset(x) ((int)&(((CONTEXT *)NULL)->x))
225 static const int mappings
[] = {
226 context_offset (Eax
),
227 context_offset (Ecx
),
228 context_offset (Edx
),
229 context_offset (Ebx
),
230 context_offset (Esp
),
231 context_offset (Ebp
),
232 context_offset (Esi
),
233 context_offset (Edi
),
234 context_offset (Eip
),
235 context_offset (EFlags
),
236 context_offset (SegCs
),
237 context_offset (SegSs
),
238 context_offset (SegDs
),
239 context_offset (SegEs
),
240 context_offset (SegFs
),
241 context_offset (SegGs
),
242 context_offset (FloatSave
.RegisterArea
[0 * 10]),
243 context_offset (FloatSave
.RegisterArea
[1 * 10]),
244 context_offset (FloatSave
.RegisterArea
[2 * 10]),
245 context_offset (FloatSave
.RegisterArea
[3 * 10]),
246 context_offset (FloatSave
.RegisterArea
[4 * 10]),
247 context_offset (FloatSave
.RegisterArea
[5 * 10]),
248 context_offset (FloatSave
.RegisterArea
[6 * 10]),
249 context_offset (FloatSave
.RegisterArea
[7 * 10]),
250 context_offset (FloatSave
.ControlWord
),
251 context_offset (FloatSave
.StatusWord
),
252 context_offset (FloatSave
.TagWord
),
253 context_offset (FloatSave
.ErrorSelector
),
254 context_offset (FloatSave
.ErrorOffset
),
255 context_offset (FloatSave
.DataSelector
),
256 context_offset (FloatSave
.DataOffset
),
257 context_offset (FloatSave
.ErrorSelector
),
259 context_offset (ExtendedRegisters
[10 * 16]),
260 context_offset (ExtendedRegisters
[11 * 16]),
261 context_offset (ExtendedRegisters
[12 * 16]),
262 context_offset (ExtendedRegisters
[13 * 16]),
263 context_offset (ExtendedRegisters
[14 * 16]),
264 context_offset (ExtendedRegisters
[15 * 16]),
265 context_offset (ExtendedRegisters
[16 * 16]),
266 context_offset (ExtendedRegisters
[17 * 16]),
268 context_offset (ExtendedRegisters
[24])
270 #undef context_offset
272 /* Fetch register from gdbserver regcache data. */
274 i386_fetch_inferior_register (win32_thread_info
*th
, int r
)
276 char *context_offset
= (char *) &th
->context
+ mappings
[r
];
281 l
= *((long *) context_offset
) & 0xffff;
282 supply_register (r
, (char *) &l
);
284 else if (r
== FOP_REGNUM
)
286 l
= (*((long *) context_offset
) >> 16) & ((1 << 11) - 1);
287 supply_register (r
, (char *) &l
);
290 supply_register (r
, context_offset
);
293 /* Store a new register value into the thread context of TH. */
295 i386_store_inferior_register (win32_thread_info
*th
, int r
)
297 char *context_offset
= (char *) &th
->context
+ mappings
[r
];
298 collect_register (r
, context_offset
);
301 static const unsigned char i386_win32_breakpoint
= 0xcc;
302 #define i386_win32_breakpoint_len 1
304 struct win32_target_ops the_low_target
= {
306 sizeof (mappings
) / sizeof (mappings
[0]),
308 i386_get_thread_context
,
309 i386_set_thread_context
,
311 i386_fetch_inferior_register
,
312 i386_store_inferior_register
,
314 &i386_win32_breakpoint
,
315 i386_win32_breakpoint_len
,
318 i386_stopped_by_watchpoint
,
319 i386_stopped_data_address