gdb/ChangeLog:
[external/binutils.git] / gdb / gdbserver / win32-i386-low.c
1 /* Copyright (C) 2007, 2008 Free Software Foundation, Inc.
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
7    the Free Software Foundation; either version 3 of the License, or
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
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 #include "server.h"
19 #include "win32-low.h"
20
21 #define FCS_REGNUM 27
22 #define FOP_REGNUM 31
23
24 #define FLAG_TRACE_BIT 0x100
25
26 /* Defined in auto-generated file reg-i386.c.  */
27 void init_registers_i386 (void);
28
29 static unsigned dr[8];
30
31 static int debug_registers_changed = 0;
32 static int debug_registers_used = 0;
33
34 static void
35 i386_initial_stuff (void)
36 {
37   memset (&dr, 0, sizeof (dr));
38   debug_registers_changed = 0;
39   debug_registers_used = 0;
40 }
41
42 static void
43 i386_get_thread_context (win32_thread_info *th, DEBUG_EVENT* current_event)
44 {
45   th->context.ContextFlags = \
46     CONTEXT_FULL | \
47     CONTEXT_FLOATING_POINT | \
48     CONTEXT_EXTENDED_REGISTERS | \
49     CONTEXT_DEBUG_REGISTERS;
50
51   GetThreadContext (th->h, &th->context);
52
53   debug_registers_changed = 0;
54
55   if (th->tid == current_event->dwThreadId)
56     {
57       /* Copy dr values from the current thread.  */
58       dr[0] = th->context.Dr0;
59       dr[1] = th->context.Dr1;
60       dr[2] = th->context.Dr2;
61       dr[3] = th->context.Dr3;
62       dr[6] = th->context.Dr6;
63       dr[7] = th->context.Dr7;
64     }
65 }
66
67 static void
68 i386_set_thread_context (win32_thread_info *th, DEBUG_EVENT* current_event)
69 {
70   if (debug_registers_changed)
71     {
72       th->context.Dr0 = dr[0];
73       th->context.Dr1 = dr[1];
74       th->context.Dr2 = dr[2];
75       th->context.Dr3 = dr[3];
76       /* th->context.Dr6 = dr[6];
77          FIXME: should we set dr6 also ?? */
78       th->context.Dr7 = dr[7];
79     }
80
81   SetThreadContext (th->h, &th->context);
82 }
83
84 static void
85 i386_thread_added (win32_thread_info *th)
86 {
87   /* Set the debug registers for the new thread if they are used.  */
88   if (debug_registers_used)
89     {
90       th->context.ContextFlags = CONTEXT_DEBUG_REGISTERS;
91       GetThreadContext (th->h, &th->context);
92
93       th->context.Dr0 = dr[0];
94       th->context.Dr1 = dr[1];
95       th->context.Dr2 = dr[2];
96       th->context.Dr3 = dr[3];
97       /* th->context.Dr6 = dr[6];
98          FIXME: should we set dr6 also ?? */
99       th->context.Dr7 = dr[7];
100
101       SetThreadContext (th->h, &th->context);
102       th->context.ContextFlags = 0;
103     }
104 }
105
106 static void
107 i386_single_step (win32_thread_info *th)
108 {
109   th->context.EFlags |= FLAG_TRACE_BIT;
110 }
111
112 /* An array of offset mappings into a Win32 Context structure.
113    This is a one-to-one mapping which is indexed by gdb's register
114    numbers.  It retrieves an offset into the context structure where
115    the 4 byte register is located.
116    An offset value of -1 indicates that Win32 does not provide this
117    register in it's CONTEXT structure.  In this case regptr will return
118    a pointer into a dummy register.  */
119 #define context_offset(x) ((int)&(((CONTEXT *)NULL)->x))
120 static const int mappings[] = {
121   context_offset (Eax),
122   context_offset (Ecx),
123   context_offset (Edx),
124   context_offset (Ebx),
125   context_offset (Esp),
126   context_offset (Ebp),
127   context_offset (Esi),
128   context_offset (Edi),
129   context_offset (Eip),
130   context_offset (EFlags),
131   context_offset (SegCs),
132   context_offset (SegSs),
133   context_offset (SegDs),
134   context_offset (SegEs),
135   context_offset (SegFs),
136   context_offset (SegGs),
137   context_offset (FloatSave.RegisterArea[0 * 10]),
138   context_offset (FloatSave.RegisterArea[1 * 10]),
139   context_offset (FloatSave.RegisterArea[2 * 10]),
140   context_offset (FloatSave.RegisterArea[3 * 10]),
141   context_offset (FloatSave.RegisterArea[4 * 10]),
142   context_offset (FloatSave.RegisterArea[5 * 10]),
143   context_offset (FloatSave.RegisterArea[6 * 10]),
144   context_offset (FloatSave.RegisterArea[7 * 10]),
145   context_offset (FloatSave.ControlWord),
146   context_offset (FloatSave.StatusWord),
147   context_offset (FloatSave.TagWord),
148   context_offset (FloatSave.ErrorSelector),
149   context_offset (FloatSave.ErrorOffset),
150   context_offset (FloatSave.DataSelector),
151   context_offset (FloatSave.DataOffset),
152   context_offset (FloatSave.ErrorSelector),
153   /* XMM0-7 */
154   context_offset (ExtendedRegisters[10 * 16]),
155   context_offset (ExtendedRegisters[11 * 16]),
156   context_offset (ExtendedRegisters[12 * 16]),
157   context_offset (ExtendedRegisters[13 * 16]),
158   context_offset (ExtendedRegisters[14 * 16]),
159   context_offset (ExtendedRegisters[15 * 16]),
160   context_offset (ExtendedRegisters[16 * 16]),
161   context_offset (ExtendedRegisters[17 * 16]),
162   /* MXCSR */
163   context_offset (ExtendedRegisters[24])
164 };
165 #undef context_offset
166
167 /* Fetch register from gdbserver regcache data.  */
168 static void
169 i386_fetch_inferior_register (win32_thread_info *th, int r)
170 {
171   char *context_offset = (char *) &th->context + mappings[r];
172
173   long l;
174   if (r == FCS_REGNUM)
175     {
176       l = *((long *) context_offset) & 0xffff;
177       supply_register (r, (char *) &l);
178     }
179   else if (r == FOP_REGNUM)
180     {
181       l = (*((long *) context_offset) >> 16) & ((1 << 11) - 1);
182       supply_register (r, (char *) &l);
183     }
184   else
185     supply_register (r, context_offset);
186 }
187
188 /* Store a new register value into the thread context of TH.  */
189 static void
190 i386_store_inferior_register (win32_thread_info *th, int r)
191 {
192   char *context_offset = (char *) &th->context + mappings[r];
193   collect_register (r, context_offset);
194 }
195
196 struct win32_target_ops the_low_target = {
197   init_registers_i386,
198   sizeof (mappings) / sizeof (mappings[0]),
199   i386_initial_stuff,
200   i386_get_thread_context,
201   i386_set_thread_context,
202   i386_thread_added,
203   i386_fetch_inferior_register,
204   i386_store_inferior_register,
205   i386_single_step,
206   NULL, /* breakpoint */
207   0, /* breakpoint_len */
208   "i386" /* arch_string */
209 };