Authors: Chris Morgan <cmorgan@wpi.edu>, James Abbatiello <abbejy@wpi.edu>
[wine/multimedia.git] / debugger / registers.c
blobd0eef7775af50fa21f760c2f0b08b8c5a92e8739
1 /*
2 * Debugger register handling
4 * Copyright 1995 Alexandre Julliard
5 */
7 #include "config.h"
8 #include <stdio.h>
9 #include <string.h>
10 #include "selectors.h"
11 #include "debugger.h"
13 CONTEXT DEBUG_context;
15 /***********************************************************************
16 * DEBUG_SetRegister
18 * Set a register value.
20 void DEBUG_SetRegister( enum debug_regs reg, int val )
22 switch(reg)
24 case REG_EAX: EAX_reg(&DEBUG_context) = val; break;
25 case REG_EBX: EBX_reg(&DEBUG_context) = val; break;
26 case REG_ECX: ECX_reg(&DEBUG_context) = val; break;
27 case REG_EDX: EDX_reg(&DEBUG_context) = val; break;
28 case REG_ESI: ESI_reg(&DEBUG_context) = val; break;
29 case REG_EDI: EDI_reg(&DEBUG_context) = val; break;
30 case REG_EBP: EBP_reg(&DEBUG_context) = val; break;
31 case REG_EFL: EFL_reg(&DEBUG_context) = val; break;
32 case REG_EIP: EIP_reg(&DEBUG_context) = val; break;
33 case REG_ESP: ESP_reg(&DEBUG_context) = val; break;
34 case REG_AX: AX_reg(&DEBUG_context) = val; break;
35 case REG_BX: BX_reg(&DEBUG_context) = val; break;
36 case REG_CX: CX_reg(&DEBUG_context) = val; break;
37 case REG_DX: DX_reg(&DEBUG_context) = val; break;
38 case REG_SI: SI_reg(&DEBUG_context) = val; break;
39 case REG_DI: DI_reg(&DEBUG_context) = val; break;
40 case REG_BP: BP_reg(&DEBUG_context) = val; break;
41 case REG_FL: FL_reg(&DEBUG_context) = val; break;
42 case REG_IP: IP_reg(&DEBUG_context) = val; break;
43 case REG_SP: SP_reg(&DEBUG_context) = val; break;
44 case REG_CS: CS_reg(&DEBUG_context) = val; break;
45 case REG_DS: DS_reg(&DEBUG_context) = val; break;
46 case REG_ES: ES_reg(&DEBUG_context) = val; break;
47 case REG_SS: SS_reg(&DEBUG_context) = val; break;
48 case REG_FS: FS_reg(&DEBUG_context) = val; break;
49 case REG_GS: GS_reg(&DEBUG_context) = val; break;
54 int
55 DEBUG_PrintRegister(enum debug_regs reg)
57 switch(reg)
59 case REG_EAX: fprintf(stderr, "%%eax"); break;
60 case REG_EBX: fprintf(stderr, "%%ebx"); break;
61 case REG_ECX: fprintf(stderr, "%%ecx"); break;
62 case REG_EDX: fprintf(stderr, "%%edx"); break;
63 case REG_ESI: fprintf(stderr, "%%esi"); break;
64 case REG_EDI: fprintf(stderr, "%%edi"); break;
65 case REG_EBP: fprintf(stderr, "%%ebp"); break;
66 case REG_EFL: fprintf(stderr, "%%efl"); break;
67 case REG_EIP: fprintf(stderr, "%%eip"); break;
68 case REG_ESP: fprintf(stderr, "%%esp"); break;
69 case REG_AX: fprintf(stderr, "%%ax"); break;
70 case REG_BX: fprintf(stderr, "%%bx"); break;
71 case REG_CX: fprintf(stderr, "%%cx"); break;
72 case REG_DX: fprintf(stderr, "%%dx"); break;
73 case REG_SI: fprintf(stderr, "%%si"); break;
74 case REG_DI: fprintf(stderr, "%%di"); break;
75 case REG_BP: fprintf(stderr, "%%bp"); break;
76 case REG_FL: fprintf(stderr, "%%fl"); break;
77 case REG_IP: fprintf(stderr, "%%ip"); break;
78 case REG_SP: fprintf(stderr, "%%sp"); break;
79 case REG_CS: fprintf(stderr, "%%cs"); break;
80 case REG_DS: fprintf(stderr, "%%ds"); break;
81 case REG_ES: fprintf(stderr, "%%es"); break;
82 case REG_SS: fprintf(stderr, "%%ss"); break;
83 case REG_FS: fprintf(stderr, "%%fs"); break;
84 case REG_GS: fprintf(stderr, "%%gs"); break;
86 return TRUE;
89 /***********************************************************************
90 * DEBUG_GetRegister
92 * Get a register value.
94 int DEBUG_GetRegister( enum debug_regs reg )
96 switch(reg)
98 case REG_EAX: return EAX_reg(&DEBUG_context);
99 case REG_EBX: return EBX_reg(&DEBUG_context);
100 case REG_ECX: return ECX_reg(&DEBUG_context);
101 case REG_EDX: return EDX_reg(&DEBUG_context);
102 case REG_ESI: return ESI_reg(&DEBUG_context);
103 case REG_EDI: return EDI_reg(&DEBUG_context);
104 case REG_EBP: return EBP_reg(&DEBUG_context);
105 case REG_EFL: return EFL_reg(&DEBUG_context);
106 case REG_EIP: return EIP_reg(&DEBUG_context);
107 case REG_ESP: return ESP_reg(&DEBUG_context);
108 case REG_AX: return AX_reg(&DEBUG_context);
109 case REG_BX: return BX_reg(&DEBUG_context);
110 case REG_CX: return CX_reg(&DEBUG_context);
111 case REG_DX: return DX_reg(&DEBUG_context);
112 case REG_SI: return SI_reg(&DEBUG_context);
113 case REG_DI: return DI_reg(&DEBUG_context);
114 case REG_BP: return BP_reg(&DEBUG_context);
115 case REG_FL: return FL_reg(&DEBUG_context);
116 case REG_IP: return IP_reg(&DEBUG_context);
117 case REG_SP: return SP_reg(&DEBUG_context);
118 case REG_CS: return CS_reg(&DEBUG_context);
119 case REG_DS: return DS_reg(&DEBUG_context);
120 case REG_ES: return ES_reg(&DEBUG_context);
121 case REG_SS: return SS_reg(&DEBUG_context);
122 case REG_FS: return FS_reg(&DEBUG_context);
123 case REG_GS: return GS_reg(&DEBUG_context);
125 return 0; /* should not happen */
129 /***********************************************************************
130 * DEBUG_SetSigContext
132 * Set the register values from a sigcontext.
134 void DEBUG_SetSigContext( const SIGCONTEXT *sigcontext )
136 EAX_reg(&DEBUG_context) = EAX_sig(sigcontext);
137 EBX_reg(&DEBUG_context) = EBX_sig(sigcontext);
138 ECX_reg(&DEBUG_context) = ECX_sig(sigcontext);
139 EDX_reg(&DEBUG_context) = EDX_sig(sigcontext);
140 ESI_reg(&DEBUG_context) = ESI_sig(sigcontext);
141 EDI_reg(&DEBUG_context) = EDI_sig(sigcontext);
142 EBP_reg(&DEBUG_context) = EBP_sig(sigcontext);
143 EFL_reg(&DEBUG_context) = EFL_sig(sigcontext);
144 EIP_reg(&DEBUG_context) = EIP_sig(sigcontext);
145 ESP_reg(&DEBUG_context) = ESP_sig(sigcontext);
146 CS_reg(&DEBUG_context) = LOWORD(CS_sig(sigcontext));
147 DS_reg(&DEBUG_context) = LOWORD(DS_sig(sigcontext));
148 ES_reg(&DEBUG_context) = LOWORD(ES_sig(sigcontext));
149 SS_reg(&DEBUG_context) = LOWORD(SS_sig(sigcontext));
150 #ifdef FS_sig
151 FS_reg(&DEBUG_context) = LOWORD(FS_sig(sigcontext));
152 #else
153 GET_FS( FS_reg(&DEBUG_context) );
154 FS_reg(&DEBUG_context) &= 0xffff;
155 #endif
156 #ifdef GS_sig
157 GS_reg(&DEBUG_context) = LOWORD(GS_sig(sigcontext));
158 #else
159 GET_GS( GS_reg(&DEBUG_context) );
160 GS_reg(&DEBUG_context) &= 0xffff;
161 #endif
162 if (ISV86(&DEBUG_context))
163 (char*)V86BASE(&DEBUG_context) = DOSMEM_MemoryBase(0);
167 /***********************************************************************
168 * DEBUG_GetSigContext
170 * Build a sigcontext from the register values.
172 void DEBUG_GetSigContext( SIGCONTEXT *sigcontext )
174 EAX_sig(sigcontext) = EAX_reg(&DEBUG_context);
175 EBX_sig(sigcontext) = EBX_reg(&DEBUG_context);
176 ECX_sig(sigcontext) = ECX_reg(&DEBUG_context);
177 EDX_sig(sigcontext) = EDX_reg(&DEBUG_context);
178 ESI_sig(sigcontext) = ESI_reg(&DEBUG_context);
179 EDI_sig(sigcontext) = EDI_reg(&DEBUG_context);
180 EBP_sig(sigcontext) = EBP_reg(&DEBUG_context);
181 EFL_sig(sigcontext) = EFL_reg(&DEBUG_context);
182 EIP_sig(sigcontext) = EIP_reg(&DEBUG_context);
183 ESP_sig(sigcontext) = ESP_reg(&DEBUG_context);
184 CS_sig(sigcontext) = CS_reg(&DEBUG_context);
185 DS_sig(sigcontext) = DS_reg(&DEBUG_context);
186 ES_sig(sigcontext) = ES_reg(&DEBUG_context);
187 SS_sig(sigcontext) = SS_reg(&DEBUG_context);
188 #ifdef FS_sig
189 FS_sig(sigcontext) = FS_reg(&DEBUG_context);
190 #else
191 SET_FS( FS_reg(&DEBUG_context) );
192 #endif
193 #ifdef GS_sig
194 GS_sig(sigcontext) = GS_reg(&DEBUG_context);
195 #else
196 SET_GS( GS_reg(&DEBUG_context) );
197 #endif
200 /***********************************************************************
201 * DEBUG_Flags
203 * Return Flag String.
205 char *DEBUG_Flags( DWORD flag, char *buf )
207 char *pt;
209 strcpy( buf, " - 00 - - - " );
210 pt = buf + strlen( buf );
211 if ( buf >= pt-- ) return( buf );
212 if ( flag & 0x00000001 ) *pt = 'C'; /* Carry Falg */
213 if ( buf >= pt-- ) return( buf );
214 if ( flag & 0x00000002 ) *pt = '1';
215 if ( buf >= pt-- ) return( buf );
216 if ( flag & 0x00000004 ) *pt = 'P'; /* Parity Flag */
217 if ( buf >= pt-- ) return( buf );
218 if ( flag & 0x00000008 ) *pt = '-';
219 if ( buf >= pt-- ) return( buf );
220 if ( flag & 0x00000010 ) *pt = 'A'; /* Auxiliary Carry Flag */
221 if ( buf >= pt-- ) return( buf );
222 if ( flag & 0x00000020 ) *pt = '-';
223 if ( buf >= pt-- ) return( buf );
224 if ( flag & 0x00000040 ) *pt = 'Z'; /* Zero Flag */
225 if ( buf >= pt-- ) return( buf );
226 if ( flag & 0x00000080 ) *pt = 'S'; /* Sign Flag */
227 if ( buf >= pt-- ) return( buf );
228 if ( flag & 0x00000100 ) *pt = 'T'; /* Trap/Trace Flag */
229 if ( buf >= pt-- ) return( buf );
230 if ( flag & 0x00000200 ) *pt = 'I'; /* Interupt Enable Flag */
231 if ( buf >= pt-- ) return( buf );
232 if ( flag & 0x00000400 ) *pt = 'D'; /* Direction Indicator */
233 if ( buf >= pt-- ) return( buf );
234 if ( flag & 0x00000800 ) *pt = 'O'; /* Overflow Flag */
235 if ( buf >= pt-- ) return( buf );
236 if ( flag & 0x00001000 ) *pt = '1'; /* I/O Privilage Level */
237 if ( buf >= pt-- ) return( buf );
238 if ( flag & 0x00002000 ) *pt = '1'; /* I/O Privilage Level */
239 if ( buf >= pt-- ) return( buf );
240 if ( flag & 0x00004000 ) *pt = 'N'; /* Nested Task Flag */
241 if ( buf >= pt-- ) return( buf );
242 if ( flag & 0x00008000 ) *pt = '-';
243 if ( buf >= pt-- ) return( buf );
244 if ( flag & 0x00010000 ) *pt = 'R'; /* Resume Flag */
245 if ( buf >= pt-- ) return( buf );
246 if ( flag & 0x00020000 ) *pt = 'V'; /* Vritual Mode Flag */
247 if ( buf >= pt-- ) return( buf );
248 if ( flag & 0x00040000 ) *pt = 'a'; /* Alignment Check Flag */
249 if ( buf >= pt-- ) return( buf );
250 return( buf );
254 /***********************************************************************
255 * DEBUG_InfoRegisters
257 * Display registers information.
259 void DEBUG_InfoRegisters(void)
261 char flag[33];
263 fprintf(stderr,"Register dump:\n");
265 /* First get the segment registers out of the way */
266 fprintf( stderr," CS:%04x SS:%04x DS:%04x ES:%04x FS:%04x GS:%04x",
267 (WORD)CS_reg(&DEBUG_context), (WORD)SS_reg(&DEBUG_context),
268 (WORD)DS_reg(&DEBUG_context), (WORD)ES_reg(&DEBUG_context),
269 (WORD)FS_reg(&DEBUG_context), (WORD)GS_reg(&DEBUG_context) );
270 if (dbg_mode == 16)
272 fprintf( stderr,"\n IP:%04x SP:%04x BP:%04x FLAGS:%04x(%s)\n",
273 IP_reg(&DEBUG_context), SP_reg(&DEBUG_context),
274 BP_reg(&DEBUG_context), FL_reg(&DEBUG_context),
275 DEBUG_Flags(FL_reg(&DEBUG_context), flag));
276 fprintf( stderr," AX:%04x BX:%04x CX:%04x DX:%04x SI:%04x DI:%04x\n",
277 AX_reg(&DEBUG_context), BX_reg(&DEBUG_context),
278 CX_reg(&DEBUG_context), DX_reg(&DEBUG_context),
279 SI_reg(&DEBUG_context), DI_reg(&DEBUG_context) );
281 else /* 32-bit mode */
283 fprintf( stderr, "\n EIP:%08lx ESP:%08lx EBP:%08lx EFLAGS:%08lx(%s)\n",
284 EIP_reg(&DEBUG_context), ESP_reg(&DEBUG_context),
285 EBP_reg(&DEBUG_context), EFL_reg(&DEBUG_context),
286 DEBUG_Flags(EFL_reg(&DEBUG_context), flag));
287 fprintf( stderr, " EAX:%08lx EBX:%08lx ECX:%08lx EDX:%08lx\n",
288 EAX_reg(&DEBUG_context), EBX_reg(&DEBUG_context),
289 ECX_reg(&DEBUG_context), EDX_reg(&DEBUG_context) );
290 fprintf( stderr, " ESI:%08lx EDI:%08lx\n",
291 ESI_reg(&DEBUG_context), EDI_reg(&DEBUG_context) );
296 /***********************************************************************
297 * DEBUG_ValidateRegisters
299 * Make sure all registers have a correct value for returning from
300 * the signal handler.
302 BOOL DEBUG_ValidateRegisters(void)
304 WORD cs, ds;
306 if (ISV86(&DEBUG_context)) return TRUE;
308 /* Check that a selector is a valid ring-3 LDT selector, or a NULL selector */
309 #define CHECK_SEG(seg,name) \
310 if (((seg) & ~3) && \
311 ((((seg) & 7) != 7) || IS_LDT_ENTRY_FREE(SELECTOR_TO_ENTRY(seg)))) \
313 fprintf( stderr, "*** Invalid value for %s register: %04x\n", \
314 (name), (WORD)(seg) ); \
315 return FALSE; \
318 GET_CS(cs);
319 GET_DS(ds);
320 if (CS_reg(&DEBUG_context) != cs) CHECK_SEG(CS_reg(&DEBUG_context), "CS");
321 if (SS_reg(&DEBUG_context) != ds) CHECK_SEG(SS_reg(&DEBUG_context), "SS");
322 if (DS_reg(&DEBUG_context) != ds) CHECK_SEG(DS_reg(&DEBUG_context), "DS");
323 if (ES_reg(&DEBUG_context) != ds) CHECK_SEG(ES_reg(&DEBUG_context), "ES");
324 if (FS_reg(&DEBUG_context) != ds) CHECK_SEG(FS_reg(&DEBUG_context), "FS");
325 if (GS_reg(&DEBUG_context) != ds) CHECK_SEG(GS_reg(&DEBUG_context), "GS");
327 /* Check that CS and SS are not NULL */
329 if (!ISV86(&DEBUG_context))
330 if (!(CS_reg(&DEBUG_context) & ~3))
332 fprintf( stderr, "*** Invalid value for CS register: %04x\n",
333 (WORD)CS_reg(&DEBUG_context) );
334 return FALSE;
336 if (!(SS_reg(&DEBUG_context) & ~3))
338 fprintf( stderr, "*** Invalid value for SS register: %04x\n",
339 (WORD)SS_reg(&DEBUG_context) );
340 return FALSE;
342 return TRUE;
343 #undef CHECK_SEG