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