1 /* Copyright (C) 2007-2014 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);
34 extern const struct target_desc
*tdesc_amd64
;
36 /* Defined in auto-generated file reg-i386.c. */
37 void init_registers_i386 (void);
38 extern const struct target_desc
*tdesc_i386
;
41 static struct i386_debug_reg_state debug_reg_state
;
43 static int debug_registers_changed
= 0;
44 static int debug_registers_used
= 0;
46 /* Update the inferior's debug register REGNUM from STATE. */
49 i386_dr_low_set_addr (const struct i386_debug_reg_state
*state
, int regnum
)
51 if (! (regnum
>= 0 && regnum
<= DR_LASTADDR
- DR_FIRSTADDR
))
52 fatal ("Invalid debug register %d", regnum
);
54 /* debug_reg_state.dr_mirror is already set.
55 Just notify i386_set_thread_context, i386_thread_added
56 that the registers need to be updated. */
57 debug_registers_changed
= 1;
58 debug_registers_used
= 1;
62 i386_dr_low_get_addr (int regnum
)
64 gdb_assert (DR_FIRSTADDR
<= regnum
&& regnum
<= DR_LASTADDR
);
66 return debug_reg_state
.dr_mirror
[regnum
];
69 /* Update the inferior's DR7 debug control register from STATE. */
72 i386_dr_low_set_control (const struct i386_debug_reg_state
*state
)
74 /* debug_reg_state.dr_control_mirror is already set.
75 Just notify i386_set_thread_context, i386_thread_added
76 that the registers need to be updated. */
77 debug_registers_changed
= 1;
78 debug_registers_used
= 1;
82 i386_dr_low_get_control (void)
84 return debug_reg_state
.dr_control_mirror
;
87 /* Get the value of the DR6 debug status register from the inferior
88 and record it in STATE. */
91 i386_dr_low_get_status (void)
93 /* We don't need to do anything here, the last call to thread_rec for
94 current_event.dwThreadId id has already set it. */
95 return debug_reg_state
.dr_status_mirror
;
98 /* Breakpoint/watchpoint support. */
101 i386_supports_z_point_type (char z_type
)
105 case Z_PACKET_WRITE_WP
:
106 case Z_PACKET_ACCESS_WP
:
114 i386_insert_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
115 int size
, struct raw_breakpoint
*bp
)
119 case raw_bkpt_type_write_wp
:
120 case raw_bkpt_type_access_wp
:
122 enum target_hw_bp_type hw_type
123 = raw_bkpt_type_to_target_hw_bp_type (type
);
125 return i386_low_insert_watchpoint (&debug_reg_state
,
126 hw_type
, addr
, size
);
135 i386_remove_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
136 int size
, struct raw_breakpoint
*bp
)
140 case raw_bkpt_type_write_wp
:
141 case raw_bkpt_type_access_wp
:
143 enum target_hw_bp_type hw_type
144 = raw_bkpt_type_to_target_hw_bp_type (type
);
146 return i386_low_remove_watchpoint (&debug_reg_state
,
147 hw_type
, addr
, size
);
156 i386_stopped_by_watchpoint (void)
158 return i386_low_stopped_by_watchpoint (&debug_reg_state
);
162 i386_stopped_data_address (void)
165 if (i386_low_stopped_data_address (&debug_reg_state
, &addr
))
171 i386_initial_stuff (void)
173 i386_low_init_dregs (&debug_reg_state
);
174 debug_registers_changed
= 0;
175 debug_registers_used
= 0;
179 i386_get_thread_context (win32_thread_info
*th
, DEBUG_EVENT
* current_event
)
181 /* Requesting the CONTEXT_EXTENDED_REGISTERS register set fails if
182 the system doesn't support extended registers. */
183 static DWORD extended_registers
= CONTEXT_EXTENDED_REGISTERS
;
186 th
->context
.ContextFlags
= (CONTEXT_FULL
187 | CONTEXT_FLOATING_POINT
188 | CONTEXT_DEBUG_REGISTERS
189 | extended_registers
);
191 if (!GetThreadContext (th
->h
, &th
->context
))
193 DWORD e
= GetLastError ();
195 if (extended_registers
&& e
== ERROR_INVALID_PARAMETER
)
197 extended_registers
= 0;
201 error ("GetThreadContext failure %ld\n", (long) e
);
204 debug_registers_changed
= 0;
206 if (th
->tid
== current_event
->dwThreadId
)
208 /* Copy dr values from the current thread. */
209 struct i386_debug_reg_state
*dr
= &debug_reg_state
;
210 dr
->dr_mirror
[0] = th
->context
.Dr0
;
211 dr
->dr_mirror
[1] = th
->context
.Dr1
;
212 dr
->dr_mirror
[2] = th
->context
.Dr2
;
213 dr
->dr_mirror
[3] = th
->context
.Dr3
;
214 dr
->dr_status_mirror
= th
->context
.Dr6
;
215 dr
->dr_control_mirror
= th
->context
.Dr7
;
220 i386_set_thread_context (win32_thread_info
*th
, DEBUG_EVENT
* current_event
)
222 if (debug_registers_changed
)
224 struct i386_debug_reg_state
*dr
= &debug_reg_state
;
225 th
->context
.Dr0
= dr
->dr_mirror
[0];
226 th
->context
.Dr1
= dr
->dr_mirror
[1];
227 th
->context
.Dr2
= dr
->dr_mirror
[2];
228 th
->context
.Dr3
= dr
->dr_mirror
[3];
229 /* th->context.Dr6 = dr->dr_status_mirror;
230 FIXME: should we set dr6 also ?? */
231 th
->context
.Dr7
= dr
->dr_control_mirror
;
234 SetThreadContext (th
->h
, &th
->context
);
238 i386_thread_added (win32_thread_info
*th
)
240 /* Set the debug registers for the new thread if they are used. */
241 if (debug_registers_used
)
243 struct i386_debug_reg_state
*dr
= &debug_reg_state
;
244 th
->context
.ContextFlags
= CONTEXT_DEBUG_REGISTERS
;
245 GetThreadContext (th
->h
, &th
->context
);
247 th
->context
.Dr0
= dr
->dr_mirror
[0];
248 th
->context
.Dr1
= dr
->dr_mirror
[1];
249 th
->context
.Dr2
= dr
->dr_mirror
[2];
250 th
->context
.Dr3
= dr
->dr_mirror
[3];
251 /* th->context.Dr6 = dr->dr_status_mirror;
252 FIXME: should we set dr6 also ?? */
253 th
->context
.Dr7
= dr
->dr_control_mirror
;
255 SetThreadContext (th
->h
, &th
->context
);
256 th
->context
.ContextFlags
= 0;
261 i386_single_step (win32_thread_info
*th
)
263 th
->context
.EFlags
|= FLAG_TRACE_BIT
;
268 /* An array of offset mappings into a Win32 Context structure.
269 This is a one-to-one mapping which is indexed by gdb's register
270 numbers. It retrieves an offset into the context structure where
271 the 4 byte register is located.
272 An offset value of -1 indicates that Win32 does not provide this
273 register in it's CONTEXT structure. In this case regptr will return
274 a pointer into a dummy register. */
275 #define context_offset(x) ((int)&(((CONTEXT *)NULL)->x))
276 static const int mappings
[] = {
277 context_offset (Eax
),
278 context_offset (Ecx
),
279 context_offset (Edx
),
280 context_offset (Ebx
),
281 context_offset (Esp
),
282 context_offset (Ebp
),
283 context_offset (Esi
),
284 context_offset (Edi
),
285 context_offset (Eip
),
286 context_offset (EFlags
),
287 context_offset (SegCs
),
288 context_offset (SegSs
),
289 context_offset (SegDs
),
290 context_offset (SegEs
),
291 context_offset (SegFs
),
292 context_offset (SegGs
),
293 context_offset (FloatSave
.RegisterArea
[0 * 10]),
294 context_offset (FloatSave
.RegisterArea
[1 * 10]),
295 context_offset (FloatSave
.RegisterArea
[2 * 10]),
296 context_offset (FloatSave
.RegisterArea
[3 * 10]),
297 context_offset (FloatSave
.RegisterArea
[4 * 10]),
298 context_offset (FloatSave
.RegisterArea
[5 * 10]),
299 context_offset (FloatSave
.RegisterArea
[6 * 10]),
300 context_offset (FloatSave
.RegisterArea
[7 * 10]),
301 context_offset (FloatSave
.ControlWord
),
302 context_offset (FloatSave
.StatusWord
),
303 context_offset (FloatSave
.TagWord
),
304 context_offset (FloatSave
.ErrorSelector
),
305 context_offset (FloatSave
.ErrorOffset
),
306 context_offset (FloatSave
.DataSelector
),
307 context_offset (FloatSave
.DataOffset
),
308 context_offset (FloatSave
.ErrorSelector
),
310 context_offset (ExtendedRegisters
[10 * 16]),
311 context_offset (ExtendedRegisters
[11 * 16]),
312 context_offset (ExtendedRegisters
[12 * 16]),
313 context_offset (ExtendedRegisters
[13 * 16]),
314 context_offset (ExtendedRegisters
[14 * 16]),
315 context_offset (ExtendedRegisters
[15 * 16]),
316 context_offset (ExtendedRegisters
[16 * 16]),
317 context_offset (ExtendedRegisters
[17 * 16]),
319 context_offset (ExtendedRegisters
[24])
321 #undef context_offset
323 #else /* __x86_64__ */
325 #define context_offset(x) (offsetof (CONTEXT, x))
326 static const int mappings
[] =
328 context_offset (Rax
),
329 context_offset (Rbx
),
330 context_offset (Rcx
),
331 context_offset (Rdx
),
332 context_offset (Rsi
),
333 context_offset (Rdi
),
334 context_offset (Rbp
),
335 context_offset (Rsp
),
338 context_offset (R10
),
339 context_offset (R11
),
340 context_offset (R12
),
341 context_offset (R13
),
342 context_offset (R14
),
343 context_offset (R15
),
344 context_offset (Rip
),
345 context_offset (EFlags
),
346 context_offset (SegCs
),
347 context_offset (SegSs
),
348 context_offset (SegDs
),
349 context_offset (SegEs
),
350 context_offset (SegFs
),
351 context_offset (SegGs
),
352 context_offset (FloatSave
.FloatRegisters
[0]),
353 context_offset (FloatSave
.FloatRegisters
[1]),
354 context_offset (FloatSave
.FloatRegisters
[2]),
355 context_offset (FloatSave
.FloatRegisters
[3]),
356 context_offset (FloatSave
.FloatRegisters
[4]),
357 context_offset (FloatSave
.FloatRegisters
[5]),
358 context_offset (FloatSave
.FloatRegisters
[6]),
359 context_offset (FloatSave
.FloatRegisters
[7]),
360 context_offset (FloatSave
.ControlWord
),
361 context_offset (FloatSave
.StatusWord
),
362 context_offset (FloatSave
.TagWord
),
363 context_offset (FloatSave
.ErrorSelector
),
364 context_offset (FloatSave
.ErrorOffset
),
365 context_offset (FloatSave
.DataSelector
),
366 context_offset (FloatSave
.DataOffset
),
367 context_offset (FloatSave
.ErrorSelector
)
369 context_offset (Xmm0
),
370 context_offset (Xmm1
),
371 context_offset (Xmm2
),
372 context_offset (Xmm3
),
373 context_offset (Xmm4
),
374 context_offset (Xmm5
),
375 context_offset (Xmm6
),
376 context_offset (Xmm7
),
377 context_offset (Xmm8
),
378 context_offset (Xmm9
),
379 context_offset (Xmm10
),
380 context_offset (Xmm11
),
381 context_offset (Xmm12
),
382 context_offset (Xmm13
),
383 context_offset (Xmm14
),
384 context_offset (Xmm15
),
386 context_offset (FloatSave
.MxCsr
)
388 #undef context_offset
390 #endif /* __x86_64__ */
392 /* Fetch register from gdbserver regcache data. */
394 i386_fetch_inferior_register (struct regcache
*regcache
,
395 win32_thread_info
*th
, int r
)
397 char *context_offset
= (char *) &th
->context
+ mappings
[r
];
402 l
= *((long *) context_offset
) & 0xffff;
403 supply_register (regcache
, r
, (char *) &l
);
405 else if (r
== FOP_REGNUM
)
407 l
= (*((long *) context_offset
) >> 16) & ((1 << 11) - 1);
408 supply_register (regcache
, r
, (char *) &l
);
411 supply_register (regcache
, r
, context_offset
);
414 /* Store a new register value into the thread context of TH. */
416 i386_store_inferior_register (struct regcache
*regcache
,
417 win32_thread_info
*th
, int r
)
419 char *context_offset
= (char *) &th
->context
+ mappings
[r
];
420 collect_register (regcache
, r
, context_offset
);
423 static const unsigned char i386_win32_breakpoint
= 0xcc;
424 #define i386_win32_breakpoint_len 1
427 i386_arch_setup (void)
430 init_registers_amd64 ();
431 win32_tdesc
= tdesc_amd64
;
433 init_registers_i386 ();
434 win32_tdesc
= tdesc_i386
;
438 struct win32_target_ops the_low_target
= {
440 sizeof (mappings
) / sizeof (mappings
[0]),
442 i386_get_thread_context
,
443 i386_set_thread_context
,
445 i386_fetch_inferior_register
,
446 i386_store_inferior_register
,
448 &i386_win32_breakpoint
,
449 i386_win32_breakpoint_len
,
450 i386_supports_z_point_type
,
453 i386_stopped_by_watchpoint
,
454 i386_stopped_data_address