[GDBserver] Multi-process + multi-arch
[deliverable/binutils-gdb.git] / gdb / gdbserver / win32-i386-low.c
CommitLineData
28e7fd62 1/* Copyright (C) 2007-2013 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 21
54709339
PM
22#ifndef CONTEXT_EXTENDED_REGISTERS
23#define CONTEXT_EXTENDED_REGISTERS 0
24#endif
25
68070c10
PA
26#define FCS_REGNUM 27
27#define FOP_REGNUM 31
28
29#define FLAG_TRACE_BIT 0x100
30
1c07cc19
PM
31#ifdef __x86_64__
32/* Defined in auto-generated file reg-amd64.c. */
54709339 33void init_registers_amd64 (void);
3aee8918 34extern const struct target_desc *tdesc_amd64;
54709339 35#else
d05b4ac3
UW
36/* Defined in auto-generated file reg-i386.c. */
37void init_registers_i386 (void);
3aee8918 38extern const struct target_desc *tdesc_i386;
54709339 39#endif
d05b4ac3 40
aa5ca48f 41static struct i386_debug_reg_state debug_reg_state;
68070c10 42
34b34921
PA
43static int debug_registers_changed = 0;
44static int debug_registers_used = 0;
45
aa5ca48f
DE
46/* Update the inferior's debug register REGNUM from STATE. */
47
48void
49i386_dr_low_set_addr (const struct i386_debug_reg_state *state, int regnum)
50{
51 if (! (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR))
52 fatal ("Invalid debug register %d", regnum);
53
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;
59}
60
964e4306
PA
61CORE_ADDR
62i386_dr_low_get_addr (int regnum)
63{
0a5b1e09 64 gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR);
964e4306
PA
65
66 return debug_reg_state.dr_mirror[regnum];
67}
68
aa5ca48f
DE
69/* Update the inferior's DR7 debug control register from STATE. */
70
71void
72i386_dr_low_set_control (const struct i386_debug_reg_state *state)
73{
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;
79}
80
964e4306
PA
81unsigned
82i386_dr_low_get_control (void)
83{
8d26e50c 84 return debug_reg_state.dr_control_mirror;
964e4306
PA
85}
86
aa5ca48f
DE
87/* Get the value of the DR6 debug status register from the inferior
88 and record it in STATE. */
89
964e4306
PA
90unsigned
91i386_dr_low_get_status (void)
aa5ca48f
DE
92{
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. */
8d26e50c 95 return debug_reg_state.dr_status_mirror;
aa5ca48f
DE
96}
97
98/* Watchpoint support. */
99
100static int
101i386_insert_point (char type, CORE_ADDR addr, int len)
102{
103 switch (type)
104 {
105 case '2':
106 case '3':
107 case '4':
108 return i386_low_insert_watchpoint (&debug_reg_state,
109 type, addr, len);
110 default:
111 /* Unsupported. */
112 return 1;
113 }
114}
115
116static int
117i386_remove_point (char type, CORE_ADDR addr, int len)
118{
119 switch (type)
120 {
121 case '2':
122 case '3':
123 case '4':
124 return i386_low_remove_watchpoint (&debug_reg_state,
125 type, addr, len);
126 default:
127 /* Unsupported. */
128 return 1;
129 }
130}
131
132static int
133i386_stopped_by_watchpoint (void)
134{
135 return i386_low_stopped_by_watchpoint (&debug_reg_state);
136}
137
138static CORE_ADDR
139i386_stopped_data_address (void)
140{
141 CORE_ADDR addr;
142 if (i386_low_stopped_data_address (&debug_reg_state, &addr))
143 return addr;
144 return 0;
145}
146
68070c10 147static void
34b34921 148i386_initial_stuff (void)
68070c10 149{
aa5ca48f 150 i386_low_init_dregs (&debug_reg_state);
34b34921
PA
151 debug_registers_changed = 0;
152 debug_registers_used = 0;
68070c10
PA
153}
154
155static void
34b34921 156i386_get_thread_context (win32_thread_info *th, DEBUG_EVENT* current_event)
68070c10 157{
912cf4ba
PA
158 /* Requesting the CONTEXT_EXTENDED_REGISTERS register set fails if
159 the system doesn't support extended registers. */
160 static DWORD extended_registers = CONTEXT_EXTENDED_REGISTERS;
34b34921 161
912cf4ba
PA
162 again:
163 th->context.ContextFlags = (CONTEXT_FULL
164 | CONTEXT_FLOATING_POINT
165 | CONTEXT_DEBUG_REGISTERS
166 | extended_registers);
167
168 if (!GetThreadContext (th->h, &th->context))
169 {
170 DWORD e = GetLastError ();
171
172 if (extended_registers && e == ERROR_INVALID_PARAMETER)
173 {
174 extended_registers = 0;
175 goto again;
176 }
177
178 error ("GetThreadContext failure %ld\n", (long) e);
179 }
34b34921
PA
180
181 debug_registers_changed = 0;
182
183 if (th->tid == current_event->dwThreadId)
184 {
185 /* Copy dr values from the current thread. */
aa5ca48f
DE
186 struct i386_debug_reg_state *dr = &debug_reg_state;
187 dr->dr_mirror[0] = th->context.Dr0;
188 dr->dr_mirror[1] = th->context.Dr1;
189 dr->dr_mirror[2] = th->context.Dr2;
190 dr->dr_mirror[3] = th->context.Dr3;
8d26e50c
JB
191 dr->dr_status_mirror = th->context.Dr6;
192 dr->dr_control_mirror = th->context.Dr7;
34b34921 193 }
68070c10
PA
194}
195
196static void
34b34921 197i386_set_thread_context (win32_thread_info *th, DEBUG_EVENT* current_event)
68070c10 198{
34b34921
PA
199 if (debug_registers_changed)
200 {
aa5ca48f
DE
201 struct i386_debug_reg_state *dr = &debug_reg_state;
202 th->context.Dr0 = dr->dr_mirror[0];
203 th->context.Dr1 = dr->dr_mirror[1];
204 th->context.Dr2 = dr->dr_mirror[2];
205 th->context.Dr3 = dr->dr_mirror[3];
8d26e50c 206 /* th->context.Dr6 = dr->dr_status_mirror;
34b34921 207 FIXME: should we set dr6 also ?? */
8d26e50c 208 th->context.Dr7 = dr->dr_control_mirror;
34b34921
PA
209 }
210
211 SetThreadContext (th->h, &th->context);
68070c10
PA
212}
213
68070c10 214static void
34b34921 215i386_thread_added (win32_thread_info *th)
68070c10 216{
34b34921
PA
217 /* Set the debug registers for the new thread if they are used. */
218 if (debug_registers_used)
68070c10 219 {
aa5ca48f 220 struct i386_debug_reg_state *dr = &debug_reg_state;
34b34921
PA
221 th->context.ContextFlags = CONTEXT_DEBUG_REGISTERS;
222 GetThreadContext (th->h, &th->context);
223
aa5ca48f
DE
224 th->context.Dr0 = dr->dr_mirror[0];
225 th->context.Dr1 = dr->dr_mirror[1];
226 th->context.Dr2 = dr->dr_mirror[2];
227 th->context.Dr3 = dr->dr_mirror[3];
8d26e50c 228 /* th->context.Dr6 = dr->dr_status_mirror;
34b34921 229 FIXME: should we set dr6 also ?? */
8d26e50c 230 th->context.Dr7 = dr->dr_control_mirror;
34b34921
PA
231
232 SetThreadContext (th->h, &th->context);
233 th->context.ContextFlags = 0;
68070c10 234 }
68070c10
PA
235}
236
237static void
34b34921 238i386_single_step (win32_thread_info *th)
68070c10
PA
239{
240 th->context.EFlags |= FLAG_TRACE_BIT;
241}
242
1c07cc19 243#ifndef __x86_64__
54709339 244
68070c10
PA
245/* An array of offset mappings into a Win32 Context structure.
246 This is a one-to-one mapping which is indexed by gdb's register
247 numbers. It retrieves an offset into the context structure where
248 the 4 byte register is located.
249 An offset value of -1 indicates that Win32 does not provide this
250 register in it's CONTEXT structure. In this case regptr will return
251 a pointer into a dummy register. */
252#define context_offset(x) ((int)&(((CONTEXT *)NULL)->x))
253static const int mappings[] = {
254 context_offset (Eax),
255 context_offset (Ecx),
256 context_offset (Edx),
257 context_offset (Ebx),
258 context_offset (Esp),
259 context_offset (Ebp),
260 context_offset (Esi),
261 context_offset (Edi),
262 context_offset (Eip),
263 context_offset (EFlags),
264 context_offset (SegCs),
265 context_offset (SegSs),
266 context_offset (SegDs),
267 context_offset (SegEs),
268 context_offset (SegFs),
269 context_offset (SegGs),
270 context_offset (FloatSave.RegisterArea[0 * 10]),
271 context_offset (FloatSave.RegisterArea[1 * 10]),
272 context_offset (FloatSave.RegisterArea[2 * 10]),
273 context_offset (FloatSave.RegisterArea[3 * 10]),
274 context_offset (FloatSave.RegisterArea[4 * 10]),
275 context_offset (FloatSave.RegisterArea[5 * 10]),
276 context_offset (FloatSave.RegisterArea[6 * 10]),
277 context_offset (FloatSave.RegisterArea[7 * 10]),
278 context_offset (FloatSave.ControlWord),
279 context_offset (FloatSave.StatusWord),
280 context_offset (FloatSave.TagWord),
281 context_offset (FloatSave.ErrorSelector),
282 context_offset (FloatSave.ErrorOffset),
283 context_offset (FloatSave.DataSelector),
284 context_offset (FloatSave.DataOffset),
285 context_offset (FloatSave.ErrorSelector),
286 /* XMM0-7 */
287 context_offset (ExtendedRegisters[10 * 16]),
288 context_offset (ExtendedRegisters[11 * 16]),
289 context_offset (ExtendedRegisters[12 * 16]),
290 context_offset (ExtendedRegisters[13 * 16]),
291 context_offset (ExtendedRegisters[14 * 16]),
292 context_offset (ExtendedRegisters[15 * 16]),
293 context_offset (ExtendedRegisters[16 * 16]),
294 context_offset (ExtendedRegisters[17 * 16]),
295 /* MXCSR */
296 context_offset (ExtendedRegisters[24])
297};
298#undef context_offset
299
1c07cc19 300#else /* __x86_64__ */
54709339
PM
301
302#define context_offset(x) (offsetof (CONTEXT, x))
303static const int mappings[] =
304{
305 context_offset (Rax),
306 context_offset (Rbx),
307 context_offset (Rcx),
308 context_offset (Rdx),
309 context_offset (Rsi),
310 context_offset (Rdi),
311 context_offset (Rbp),
312 context_offset (Rsp),
313 context_offset (R8),
314 context_offset (R9),
315 context_offset (R10),
316 context_offset (R11),
317 context_offset (R12),
318 context_offset (R13),
319 context_offset (R14),
320 context_offset (R15),
321 context_offset (Rip),
322 context_offset (EFlags),
323 context_offset (SegCs),
324 context_offset (SegSs),
325 context_offset (SegDs),
326 context_offset (SegEs),
327 context_offset (SegFs),
328 context_offset (SegGs),
329 context_offset (FloatSave.FloatRegisters[0]),
330 context_offset (FloatSave.FloatRegisters[1]),
331 context_offset (FloatSave.FloatRegisters[2]),
332 context_offset (FloatSave.FloatRegisters[3]),
333 context_offset (FloatSave.FloatRegisters[4]),
334 context_offset (FloatSave.FloatRegisters[5]),
335 context_offset (FloatSave.FloatRegisters[6]),
336 context_offset (FloatSave.FloatRegisters[7]),
337 context_offset (FloatSave.ControlWord),
338 context_offset (FloatSave.StatusWord),
339 context_offset (FloatSave.TagWord),
340 context_offset (FloatSave.ErrorSelector),
341 context_offset (FloatSave.ErrorOffset),
342 context_offset (FloatSave.DataSelector),
343 context_offset (FloatSave.DataOffset),
344 context_offset (FloatSave.ErrorSelector)
345 /* XMM0-7 */ ,
346 context_offset (Xmm0),
347 context_offset (Xmm1),
348 context_offset (Xmm2),
349 context_offset (Xmm3),
350 context_offset (Xmm4),
351 context_offset (Xmm5),
352 context_offset (Xmm6),
353 context_offset (Xmm7),
354 context_offset (Xmm8),
355 context_offset (Xmm9),
356 context_offset (Xmm10),
357 context_offset (Xmm11),
358 context_offset (Xmm12),
359 context_offset (Xmm13),
360 context_offset (Xmm14),
361 context_offset (Xmm15),
362 /* MXCSR */
363 context_offset (FloatSave.MxCsr)
364};
365#undef context_offset
366
1c07cc19 367#endif /* __x86_64__ */
54709339 368
34b34921
PA
369/* Fetch register from gdbserver regcache data. */
370static void
442ea881
PA
371i386_fetch_inferior_register (struct regcache *regcache,
372 win32_thread_info *th, int r)
34b34921
PA
373{
374 char *context_offset = (char *) &th->context + mappings[r];
375
376 long l;
377 if (r == FCS_REGNUM)
378 {
379 l = *((long *) context_offset) & 0xffff;
442ea881 380 supply_register (regcache, r, (char *) &l);
34b34921
PA
381 }
382 else if (r == FOP_REGNUM)
383 {
384 l = (*((long *) context_offset) >> 16) & ((1 << 11) - 1);
442ea881 385 supply_register (regcache, r, (char *) &l);
34b34921
PA
386 }
387 else
442ea881 388 supply_register (regcache, r, context_offset);
34b34921
PA
389}
390
391/* Store a new register value into the thread context of TH. */
392static void
442ea881
PA
393i386_store_inferior_register (struct regcache *regcache,
394 win32_thread_info *th, int r)
34b34921
PA
395{
396 char *context_offset = (char *) &th->context + mappings[r];
442ea881 397 collect_register (regcache, r, context_offset);
34b34921
PA
398}
399
912cf4ba
PA
400static const unsigned char i386_win32_breakpoint = 0xcc;
401#define i386_win32_breakpoint_len 1
402
54709339 403static void
3aee8918 404i386_arch_setup (void)
54709339 405{
1c07cc19 406#ifdef __x86_64__
54709339 407 init_registers_amd64 ();
3aee8918 408 win32_tdesc = tdesc_amd64;
54709339
PM
409#else
410 init_registers_i386 ();
3aee8918 411 win32_tdesc = tdesc_i386;
54709339
PM
412#endif
413}
414
68070c10 415struct win32_target_ops the_low_target = {
3aee8918 416 i386_arch_setup,
68070c10 417 sizeof (mappings) / sizeof (mappings[0]),
34b34921
PA
418 i386_initial_stuff,
419 i386_get_thread_context,
420 i386_set_thread_context,
421 i386_thread_added,
422 i386_fetch_inferior_register,
423 i386_store_inferior_register,
424 i386_single_step,
912cf4ba
PA
425 &i386_win32_breakpoint,
426 i386_win32_breakpoint_len,
aa5ca48f
DE
427 i386_insert_point,
428 i386_remove_point,
429 i386_stopped_by_watchpoint,
430 i386_stopped_data_address
68070c10 431};
This page took 0.672414 seconds and 4 git commands to generate.