]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/gdbserver/win32-i386-low.c
*** empty log message ***
[thirdparty/binutils-gdb.git] / gdb / gdbserver / win32-i386-low.c
CommitLineData
0fb0cc75 1/* Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
68070c10
PA
2
3 This file is part of GDB.
4
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
a9762ec7 7 the Free Software Foundation; either version 3 of the License, or
68070c10
PA
8 (at your option) any later version.
9
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.
14
15 You should have received a copy of the GNU General Public License
a9762ec7 16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
68070c10
PA
17
18#include "server.h"
19#include "win32-low.h"
aa5ca48f 20#include "i386-low.h"
68070c10
PA
21
22#define FCS_REGNUM 27
23#define FOP_REGNUM 31
24
25#define FLAG_TRACE_BIT 0x100
26
d05b4ac3
UW
27/* Defined in auto-generated file reg-i386.c. */
28void init_registers_i386 (void);
29
aa5ca48f 30static struct i386_debug_reg_state debug_reg_state;
68070c10 31
34b34921
PA
32static int debug_registers_changed = 0;
33static int debug_registers_used = 0;
34
aa5ca48f
DE
35/* Update the inferior's debug register REGNUM from STATE. */
36
37void
38i386_dr_low_set_addr (const struct i386_debug_reg_state *state, int regnum)
39{
40 if (! (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR))
41 fatal ("Invalid debug register %d", regnum);
42
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;
48}
49
50/* Update the inferior's DR7 debug control register from STATE. */
51
52void
53i386_dr_low_set_control (const struct i386_debug_reg_state *state)
54{
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;
60}
61
62/* Get the value of the DR6 debug status register from the inferior
63 and record it in STATE. */
64
65void
66i386_dr_low_get_status (struct i386_debug_reg_state *state)
67{
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. */
70}
71
72/* Watchpoint support. */
73
74static int
75i386_insert_point (char type, CORE_ADDR addr, int len)
76{
77 switch (type)
78 {
79 case '2':
80 case '3':
81 case '4':
82 return i386_low_insert_watchpoint (&debug_reg_state,
83 type, addr, len);
84 default:
85 /* Unsupported. */
86 return 1;
87 }
88}
89
90static int
91i386_remove_point (char type, CORE_ADDR addr, int len)
92{
93 switch (type)
94 {
95 case '2':
96 case '3':
97 case '4':
98 return i386_low_remove_watchpoint (&debug_reg_state,
99 type, addr, len);
100 default:
101 /* Unsupported. */
102 return 1;
103 }
104}
105
106static int
107i386_stopped_by_watchpoint (void)
108{
109 return i386_low_stopped_by_watchpoint (&debug_reg_state);
110}
111
112static CORE_ADDR
113i386_stopped_data_address (void)
114{
115 CORE_ADDR addr;
116 if (i386_low_stopped_data_address (&debug_reg_state, &addr))
117 return addr;
118 return 0;
119}
120
68070c10 121static void
34b34921 122i386_initial_stuff (void)
68070c10 123{
aa5ca48f 124 i386_low_init_dregs (&debug_reg_state);
34b34921
PA
125 debug_registers_changed = 0;
126 debug_registers_used = 0;
68070c10
PA
127}
128
129static void
34b34921 130i386_get_thread_context (win32_thread_info *th, DEBUG_EVENT* current_event)
68070c10 131{
34b34921
PA
132 th->context.ContextFlags = \
133 CONTEXT_FULL | \
134 CONTEXT_FLOATING_POINT | \
135 CONTEXT_EXTENDED_REGISTERS | \
136 CONTEXT_DEBUG_REGISTERS;
137
138 GetThreadContext (th->h, &th->context);
139
140 debug_registers_changed = 0;
141
142 if (th->tid == current_event->dwThreadId)
143 {
144 /* Copy dr values from the current thread. */
aa5ca48f
DE
145 struct i386_debug_reg_state *dr = &debug_reg_state;
146 dr->dr_mirror[0] = th->context.Dr0;
147 dr->dr_mirror[1] = th->context.Dr1;
148 dr->dr_mirror[2] = th->context.Dr2;
149 dr->dr_mirror[3] = th->context.Dr3;
150 dr->dr_status_mirror = th->context.Dr6;
151 dr->dr_control_mirror = th->context.Dr7;
34b34921 152 }
68070c10
PA
153}
154
155static void
34b34921 156i386_set_thread_context (win32_thread_info *th, DEBUG_EVENT* current_event)
68070c10 157{
34b34921
PA
158 if (debug_registers_changed)
159 {
aa5ca48f
DE
160 struct i386_debug_reg_state *dr = &debug_reg_state;
161 th->context.Dr0 = dr->dr_mirror[0];
162 th->context.Dr1 = dr->dr_mirror[1];
163 th->context.Dr2 = dr->dr_mirror[2];
164 th->context.Dr3 = dr->dr_mirror[3];
165 /* th->context.Dr6 = dr->dr_status_mirror;
34b34921 166 FIXME: should we set dr6 also ?? */
aa5ca48f 167 th->context.Dr7 = dr->dr_control_mirror;
34b34921
PA
168 }
169
170 SetThreadContext (th->h, &th->context);
68070c10
PA
171}
172
68070c10 173static void
34b34921 174i386_thread_added (win32_thread_info *th)
68070c10 175{
34b34921
PA
176 /* Set the debug registers for the new thread if they are used. */
177 if (debug_registers_used)
68070c10 178 {
aa5ca48f 179 struct i386_debug_reg_state *dr = &debug_reg_state;
34b34921
PA
180 th->context.ContextFlags = CONTEXT_DEBUG_REGISTERS;
181 GetThreadContext (th->h, &th->context);
182
aa5ca48f
DE
183 th->context.Dr0 = dr->dr_mirror[0];
184 th->context.Dr1 = dr->dr_mirror[1];
185 th->context.Dr2 = dr->dr_mirror[2];
186 th->context.Dr3 = dr->dr_mirror[3];
187 /* th->context.Dr6 = dr->dr_status_mirror;
34b34921 188 FIXME: should we set dr6 also ?? */
aa5ca48f 189 th->context.Dr7 = dr->dr_control_mirror;
34b34921
PA
190
191 SetThreadContext (th->h, &th->context);
192 th->context.ContextFlags = 0;
68070c10 193 }
68070c10
PA
194}
195
196static void
34b34921 197i386_single_step (win32_thread_info *th)
68070c10
PA
198{
199 th->context.EFlags |= FLAG_TRACE_BIT;
200}
201
202/* An array of offset mappings into a Win32 Context structure.
203 This is a one-to-one mapping which is indexed by gdb's register
204 numbers. It retrieves an offset into the context structure where
205 the 4 byte register is located.
206 An offset value of -1 indicates that Win32 does not provide this
207 register in it's CONTEXT structure. In this case regptr will return
208 a pointer into a dummy register. */
209#define context_offset(x) ((int)&(((CONTEXT *)NULL)->x))
210static const int mappings[] = {
211 context_offset (Eax),
212 context_offset (Ecx),
213 context_offset (Edx),
214 context_offset (Ebx),
215 context_offset (Esp),
216 context_offset (Ebp),
217 context_offset (Esi),
218 context_offset (Edi),
219 context_offset (Eip),
220 context_offset (EFlags),
221 context_offset (SegCs),
222 context_offset (SegSs),
223 context_offset (SegDs),
224 context_offset (SegEs),
225 context_offset (SegFs),
226 context_offset (SegGs),
227 context_offset (FloatSave.RegisterArea[0 * 10]),
228 context_offset (FloatSave.RegisterArea[1 * 10]),
229 context_offset (FloatSave.RegisterArea[2 * 10]),
230 context_offset (FloatSave.RegisterArea[3 * 10]),
231 context_offset (FloatSave.RegisterArea[4 * 10]),
232 context_offset (FloatSave.RegisterArea[5 * 10]),
233 context_offset (FloatSave.RegisterArea[6 * 10]),
234 context_offset (FloatSave.RegisterArea[7 * 10]),
235 context_offset (FloatSave.ControlWord),
236 context_offset (FloatSave.StatusWord),
237 context_offset (FloatSave.TagWord),
238 context_offset (FloatSave.ErrorSelector),
239 context_offset (FloatSave.ErrorOffset),
240 context_offset (FloatSave.DataSelector),
241 context_offset (FloatSave.DataOffset),
242 context_offset (FloatSave.ErrorSelector),
243 /* XMM0-7 */
244 context_offset (ExtendedRegisters[10 * 16]),
245 context_offset (ExtendedRegisters[11 * 16]),
246 context_offset (ExtendedRegisters[12 * 16]),
247 context_offset (ExtendedRegisters[13 * 16]),
248 context_offset (ExtendedRegisters[14 * 16]),
249 context_offset (ExtendedRegisters[15 * 16]),
250 context_offset (ExtendedRegisters[16 * 16]),
251 context_offset (ExtendedRegisters[17 * 16]),
252 /* MXCSR */
253 context_offset (ExtendedRegisters[24])
254};
255#undef context_offset
256
34b34921
PA
257/* Fetch register from gdbserver regcache data. */
258static void
259i386_fetch_inferior_register (win32_thread_info *th, int r)
260{
261 char *context_offset = (char *) &th->context + mappings[r];
262
263 long l;
264 if (r == FCS_REGNUM)
265 {
266 l = *((long *) context_offset) & 0xffff;
267 supply_register (r, (char *) &l);
268 }
269 else if (r == FOP_REGNUM)
270 {
271 l = (*((long *) context_offset) >> 16) & ((1 << 11) - 1);
272 supply_register (r, (char *) &l);
273 }
274 else
275 supply_register (r, context_offset);
276}
277
278/* Store a new register value into the thread context of TH. */
279static void
280i386_store_inferior_register (win32_thread_info *th, int r)
281{
282 char *context_offset = (char *) &th->context + mappings[r];
283 collect_register (r, context_offset);
284}
285
68070c10 286struct win32_target_ops the_low_target = {
d05b4ac3 287 init_registers_i386,
68070c10 288 sizeof (mappings) / sizeof (mappings[0]),
34b34921
PA
289 i386_initial_stuff,
290 i386_get_thread_context,
291 i386_set_thread_context,
292 i386_thread_added,
293 i386_fetch_inferior_register,
294 i386_store_inferior_register,
295 i386_single_step,
296 NULL, /* breakpoint */
68070c10 297 0, /* breakpoint_len */
aa5ca48f
DE
298 i386_insert_point,
299 i386_remove_point,
300 i386_stopped_by_watchpoint,
301 i386_stopped_data_address
68070c10 302};