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"
22 #ifndef CONTEXT_EXTENDED_REGISTERS
23 #define CONTEXT_EXTENDED_REGISTERS 0
29 #define FLAG_TRACE_BIT 0x100
32 /* Defined in auto-generated file reg-amd64.c. */
33 void init_registers_amd64 (void);
35 /* Defined in auto-generated file reg-i386.c. */
36 void init_registers_i386 (void);
39 static struct i386_debug_reg_state debug_reg_state;
41 static int debug_registers_changed = 0;
42 static int debug_registers_used = 0;
44 /* Update the inferior's debug register REGNUM from STATE. */
47 i386_dr_low_set_addr (const struct i386_debug_reg_state *state, int regnum)
49 if (! (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR))
50 fatal ("Invalid debug register %d", regnum);
52 /* debug_reg_state.dr_mirror is already set.
53 Just notify i386_set_thread_context, i386_thread_added
54 that the registers need to be updated. */
55 debug_registers_changed = 1;
56 debug_registers_used = 1;
59 /* Update the inferior's DR7 debug control register from STATE. */
62 i386_dr_low_set_control (const struct i386_debug_reg_state *state)
64 /* debug_reg_state.dr_control_mirror is already set.
65 Just notify i386_set_thread_context, i386_thread_added
66 that the registers need to be updated. */
67 debug_registers_changed = 1;
68 debug_registers_used = 1;
71 /* Get the value of the DR6 debug status register from the inferior
72 and record it in STATE. */
75 i386_dr_low_get_status (struct i386_debug_reg_state *state)
77 /* We don't need to do anything here, the last call to thread_rec for
78 current_event.dwThreadId id has already set it. */
81 /* Watchpoint support. */
84 i386_insert_point (char type, CORE_ADDR addr, int len)
91 return i386_low_insert_watchpoint (&debug_reg_state,
100 i386_remove_point (char type, CORE_ADDR addr, int len)
107 return i386_low_remove_watchpoint (&debug_reg_state,
116 i386_stopped_by_watchpoint (void)
118 return i386_low_stopped_by_watchpoint (&debug_reg_state);
122 i386_stopped_data_address (void)
125 if (i386_low_stopped_data_address (&debug_reg_state, &addr))
131 i386_initial_stuff (void)
133 i386_low_init_dregs (&debug_reg_state);
134 debug_registers_changed = 0;
135 debug_registers_used = 0;
139 i386_get_thread_context (win32_thread_info *th, DEBUG_EVENT* current_event)
141 /* Requesting the CONTEXT_EXTENDED_REGISTERS register set fails if
142 the system doesn't support extended registers. */
143 static DWORD extended_registers = CONTEXT_EXTENDED_REGISTERS;
146 th->context.ContextFlags = (CONTEXT_FULL
147 | CONTEXT_FLOATING_POINT
148 | CONTEXT_DEBUG_REGISTERS
149 | extended_registers);
151 if (!GetThreadContext (th->h, &th->context))
153 DWORD e = GetLastError ();
155 if (extended_registers && e == ERROR_INVALID_PARAMETER)
157 extended_registers = 0;
161 error ("GetThreadContext failure %ld\n", (long) e);
164 debug_registers_changed = 0;
166 if (th->tid == current_event->dwThreadId)
168 /* Copy dr values from the current thread. */
169 struct i386_debug_reg_state *dr = &debug_reg_state;
170 dr->dr_mirror[0] = th->context.Dr0;
171 dr->dr_mirror[1] = th->context.Dr1;
172 dr->dr_mirror[2] = th->context.Dr2;
173 dr->dr_mirror[3] = th->context.Dr3;
174 dr->dr_status_mirror = th->context.Dr6;
175 dr->dr_control_mirror = th->context.Dr7;
180 i386_set_thread_context (win32_thread_info *th, DEBUG_EVENT* current_event)
182 if (debug_registers_changed)
184 struct i386_debug_reg_state *dr = &debug_reg_state;
185 th->context.Dr0 = dr->dr_mirror[0];
186 th->context.Dr1 = dr->dr_mirror[1];
187 th->context.Dr2 = dr->dr_mirror[2];
188 th->context.Dr3 = dr->dr_mirror[3];
189 /* th->context.Dr6 = dr->dr_status_mirror;
190 FIXME: should we set dr6 also ?? */
191 th->context.Dr7 = dr->dr_control_mirror;
194 SetThreadContext (th->h, &th->context);
198 i386_thread_added (win32_thread_info *th)
200 /* Set the debug registers for the new thread if they are used. */
201 if (debug_registers_used)
203 struct i386_debug_reg_state *dr = &debug_reg_state;
204 th->context.ContextFlags = CONTEXT_DEBUG_REGISTERS;
205 GetThreadContext (th->h, &th->context);
207 th->context.Dr0 = dr->dr_mirror[0];
208 th->context.Dr1 = dr->dr_mirror[1];
209 th->context.Dr2 = dr->dr_mirror[2];
210 th->context.Dr3 = dr->dr_mirror[3];
211 /* th->context.Dr6 = dr->dr_status_mirror;
212 FIXME: should we set dr6 also ?? */
213 th->context.Dr7 = dr->dr_control_mirror;
215 SetThreadContext (th->h, &th->context);
216 th->context.ContextFlags = 0;
221 i386_single_step (win32_thread_info *th)
223 th->context.EFlags |= FLAG_TRACE_BIT;
228 /* An array of offset mappings into a Win32 Context structure.
229 This is a one-to-one mapping which is indexed by gdb's register
230 numbers. It retrieves an offset into the context structure where
231 the 4 byte register is located.
232 An offset value of -1 indicates that Win32 does not provide this
233 register in it's CONTEXT structure. In this case regptr will return
234 a pointer into a dummy register. */
235 #define context_offset(x) ((int)&(((CONTEXT *)NULL)->x))
236 static const int mappings[] = {
237 context_offset (Eax),
238 context_offset (Ecx),
239 context_offset (Edx),
240 context_offset (Ebx),
241 context_offset (Esp),
242 context_offset (Ebp),
243 context_offset (Esi),
244 context_offset (Edi),
245 context_offset (Eip),
246 context_offset (EFlags),
247 context_offset (SegCs),
248 context_offset (SegSs),
249 context_offset (SegDs),
250 context_offset (SegEs),
251 context_offset (SegFs),
252 context_offset (SegGs),
253 context_offset (FloatSave.RegisterArea[0 * 10]),
254 context_offset (FloatSave.RegisterArea[1 * 10]),
255 context_offset (FloatSave.RegisterArea[2 * 10]),
256 context_offset (FloatSave.RegisterArea[3 * 10]),
257 context_offset (FloatSave.RegisterArea[4 * 10]),
258 context_offset (FloatSave.RegisterArea[5 * 10]),
259 context_offset (FloatSave.RegisterArea[6 * 10]),
260 context_offset (FloatSave.RegisterArea[7 * 10]),
261 context_offset (FloatSave.ControlWord),
262 context_offset (FloatSave.StatusWord),
263 context_offset (FloatSave.TagWord),
264 context_offset (FloatSave.ErrorSelector),
265 context_offset (FloatSave.ErrorOffset),
266 context_offset (FloatSave.DataSelector),
267 context_offset (FloatSave.DataOffset),
268 context_offset (FloatSave.ErrorSelector),
270 context_offset (ExtendedRegisters[10 * 16]),
271 context_offset (ExtendedRegisters[11 * 16]),
272 context_offset (ExtendedRegisters[12 * 16]),
273 context_offset (ExtendedRegisters[13 * 16]),
274 context_offset (ExtendedRegisters[14 * 16]),
275 context_offset (ExtendedRegisters[15 * 16]),
276 context_offset (ExtendedRegisters[16 * 16]),
277 context_offset (ExtendedRegisters[17 * 16]),
279 context_offset (ExtendedRegisters[24])
281 #undef context_offset
283 #else /* __x86_64__ */
285 #define context_offset(x) (offsetof (CONTEXT, x))
286 static const int mappings[] =
288 context_offset (Rax),
289 context_offset (Rbx),
290 context_offset (Rcx),
291 context_offset (Rdx),
292 context_offset (Rsi),
293 context_offset (Rdi),
294 context_offset (Rbp),
295 context_offset (Rsp),
298 context_offset (R10),
299 context_offset (R11),
300 context_offset (R12),
301 context_offset (R13),
302 context_offset (R14),
303 context_offset (R15),
304 context_offset (Rip),
305 context_offset (EFlags),
306 context_offset (SegCs),
307 context_offset (SegSs),
308 context_offset (SegDs),
309 context_offset (SegEs),
310 context_offset (SegFs),
311 context_offset (SegGs),
312 context_offset (FloatSave.FloatRegisters[0]),
313 context_offset (FloatSave.FloatRegisters[1]),
314 context_offset (FloatSave.FloatRegisters[2]),
315 context_offset (FloatSave.FloatRegisters[3]),
316 context_offset (FloatSave.FloatRegisters[4]),
317 context_offset (FloatSave.FloatRegisters[5]),
318 context_offset (FloatSave.FloatRegisters[6]),
319 context_offset (FloatSave.FloatRegisters[7]),
320 context_offset (FloatSave.ControlWord),
321 context_offset (FloatSave.StatusWord),
322 context_offset (FloatSave.TagWord),
323 context_offset (FloatSave.ErrorSelector),
324 context_offset (FloatSave.ErrorOffset),
325 context_offset (FloatSave.DataSelector),
326 context_offset (FloatSave.DataOffset),
327 context_offset (FloatSave.ErrorSelector)
329 context_offset (Xmm0),
330 context_offset (Xmm1),
331 context_offset (Xmm2),
332 context_offset (Xmm3),
333 context_offset (Xmm4),
334 context_offset (Xmm5),
335 context_offset (Xmm6),
336 context_offset (Xmm7),
337 context_offset (Xmm8),
338 context_offset (Xmm9),
339 context_offset (Xmm10),
340 context_offset (Xmm11),
341 context_offset (Xmm12),
342 context_offset (Xmm13),
343 context_offset (Xmm14),
344 context_offset (Xmm15),
346 context_offset (FloatSave.MxCsr)
348 #undef context_offset
350 #endif /* __x86_64__ */
352 /* Fetch register from gdbserver regcache data. */
354 i386_fetch_inferior_register (struct regcache *regcache,
355 win32_thread_info *th, int r)
357 char *context_offset = (char *) &th->context + mappings[r];
362 l = *((long *) context_offset) & 0xffff;
363 supply_register (regcache, r, (char *) &l);
365 else if (r == FOP_REGNUM)
367 l = (*((long *) context_offset) >> 16) & ((1 << 11) - 1);
368 supply_register (regcache, r, (char *) &l);
371 supply_register (regcache, r, context_offset);
374 /* Store a new register value into the thread context of TH. */
376 i386_store_inferior_register (struct regcache *regcache,
377 win32_thread_info *th, int r)
379 char *context_offset = (char *) &th->context + mappings[r];
380 collect_register (regcache, r, context_offset);
383 static const unsigned char i386_win32_breakpoint = 0xcc;
384 #define i386_win32_breakpoint_len 1
387 init_windows_x86 (void)
390 init_registers_amd64 ();
392 init_registers_i386 ();
396 struct win32_target_ops the_low_target = {
398 sizeof (mappings) / sizeof (mappings[0]),
400 i386_get_thread_context,
401 i386_set_thread_context,
403 i386_fetch_inferior_register,
404 i386_store_inferior_register,
406 &i386_win32_breakpoint,
407 i386_win32_breakpoint_len,
410 i386_stopped_by_watchpoint,
411 i386_stopped_data_address