d3d8: Display FIXME only once in ValidatePixelShader and ValidateVertexShader.
[wine/multimedia.git] / programs / winedbg / be_i386.c
blob6ab5970d5c4325a6990806b73f647b4d609351ab
1 /*
2 * Debugger i386 specific functions
4 * Copyright 2004 Eric Pouech
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "debugger.h"
22 #include "wine/debug.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
26 #ifdef __i386__
28 /* debugger/db_disasm.c */
29 extern void be_i386_disasm_one_insn(ADDRESS64* addr, int display);
31 #define STEP_FLAG 0x00000100 /* single step flag */
32 #define V86_FLAG 0x00020000
34 #define IS_VM86_MODE(ctx) (ctx->EFlags & V86_FLAG)
36 static ADDRESS_MODE get_selector_type(HANDLE hThread, const CONTEXT* ctx, WORD sel)
38 LDT_ENTRY le;
40 if (IS_VM86_MODE(ctx)) return AddrModeReal;
41 /* null or system selector */
42 if (!(sel & 4) || ((sel >> 3) < 17)) return AddrModeFlat;
43 if (dbg_curr_process->process_io->get_selector(hThread, sel, &le))
44 return le.HighWord.Bits.Default_Big ? AddrMode1632 : AddrMode1616;
45 /* selector doesn't exist */
46 return -1;
49 static void* be_i386_linearize(HANDLE hThread, const ADDRESS64* addr)
51 LDT_ENTRY le;
53 switch (addr->Mode)
55 case AddrModeReal:
56 return (void*)((DWORD)(LOWORD(addr->Segment) << 4) + (DWORD)addr->Offset);
57 case AddrMode1632:
58 if (!(addr->Segment & 4) || ((addr->Segment >> 3) < 17))
59 return (void*)(DWORD)addr->Offset;
60 /* fall through */
61 case AddrMode1616:
62 if (!dbg_curr_process->process_io->get_selector(hThread, addr->Segment, &le)) return NULL;
63 return (void*)((le.HighWord.Bits.BaseHi << 24) +
64 (le.HighWord.Bits.BaseMid << 16) + le.BaseLow +
65 (DWORD)addr->Offset);
66 case AddrModeFlat:
67 return (void*)(DWORD)addr->Offset;
69 return NULL;
72 static unsigned be_i386_build_addr(HANDLE hThread, const CONTEXT* ctx, ADDRESS64* addr,
73 unsigned seg, unsigned long offset)
75 addr->Mode = AddrModeFlat;
76 addr->Segment = seg;
77 addr->Offset = offset;
78 if (seg)
80 addr->Mode = get_selector_type(hThread, ctx, seg);
81 switch (addr->Mode)
83 case AddrModeReal:
84 case AddrMode1616:
85 addr->Offset &= 0xffff;
86 break;
87 case AddrModeFlat:
88 case AddrMode1632:
89 break;
90 default:
91 addr->Mode = -1;
92 return FALSE;
95 return TRUE;
98 static unsigned be_i386_get_addr(HANDLE hThread, const CONTEXT* ctx,
99 enum be_cpu_addr bca, ADDRESS64* addr)
101 switch (bca)
103 case be_cpu_addr_pc:
104 return be_i386_build_addr(hThread, ctx, addr, ctx->SegCs, ctx->Eip);
105 case be_cpu_addr_stack:
106 return be_i386_build_addr(hThread, ctx, addr, ctx->SegSs, ctx->Esp);
107 case be_cpu_addr_frame:
108 return be_i386_build_addr(hThread, ctx, addr, ctx->SegSs, ctx->Ebp);
110 return FALSE;
113 static unsigned be_i386_get_register_info(int regno, enum be_cpu_addr* kind)
115 switch (regno)
117 case CV_REG_EIP: *kind = be_cpu_addr_pc; return TRUE;
118 case CV_REG_EBP: *kind = be_cpu_addr_frame; return TRUE;
119 case CV_REG_ESP: *kind = be_cpu_addr_stack; return TRUE;
121 return FALSE;
124 static void be_i386_single_step(CONTEXT* ctx, unsigned enable)
126 if (enable) ctx->EFlags |= STEP_FLAG;
127 else ctx->EFlags &= ~STEP_FLAG;
130 static void be_i386_all_print_context(HANDLE hThread, const CONTEXT* ctx)
132 long double ST[8]; /* These are for floating regs */
133 int cnt;
135 /* Break out the FPU state and the floating point registers */
136 dbg_printf("Floating Point Unit status:\n");
137 dbg_printf(" FLCW:%04x ", LOWORD(ctx->FloatSave.ControlWord));
138 dbg_printf(" FLTW:%04x ", LOWORD(ctx->FloatSave.TagWord));
139 dbg_printf(" FLEO:%08x ", (unsigned int) ctx->FloatSave.ErrorOffset);
140 dbg_printf(" FLSW:%04x", LOWORD(ctx->FloatSave.StatusWord));
142 /* Isolate the condition code bits - note they are not contiguous */
143 dbg_printf("(CC:%d%d%d%d", (ctx->FloatSave.StatusWord & 0x00004000) >> 14,
144 (ctx->FloatSave.StatusWord & 0x00000400) >> 10,
145 (ctx->FloatSave.StatusWord & 0x00000200) >> 9,
146 (ctx->FloatSave.StatusWord & 0x00000100) >> 8);
148 /* Now pull out hte 3 bit of the TOP stack pointer */
149 dbg_printf(" TOP:%01x", (unsigned int) (ctx->FloatSave.StatusWord & 0x00003800) >> 11);
151 /* Lets analyse the error bits and indicate the status
152 * the Invalid Op flag has sub status which is tested as follows */
153 if (ctx->FloatSave.StatusWord & 0x00000001) { /* Invalid Fl OP */
154 if (ctx->FloatSave.StatusWord & 0x00000040) { /* Stack Fault */
155 if (ctx->FloatSave.StatusWord & 0x00000200) /* C1 says Overflow */
156 dbg_printf(" #IE(Stack Overflow)");
157 else
158 dbg_printf(" #IE(Stack Underflow)"); /* Underflow */
160 else dbg_printf(" #IE(Arthimetic error)"); /* Invalid Fl OP */
163 if (ctx->FloatSave.StatusWord & 0x00000002) dbg_printf(" #DE"); /* Denormalised OP */
164 if (ctx->FloatSave.StatusWord & 0x00000004) dbg_printf(" #ZE"); /* Zero Divide */
165 if (ctx->FloatSave.StatusWord & 0x00000008) dbg_printf(" #OE"); /* Overflow */
166 if (ctx->FloatSave.StatusWord & 0x00000010) dbg_printf(" #UE"); /* Underflow */
167 if (ctx->FloatSave.StatusWord & 0x00000020) dbg_printf(" #PE"); /* Precision error */
168 if (ctx->FloatSave.StatusWord & 0x00000040)
169 if (!(ctx->FloatSave.StatusWord & 0x00000001))
170 dbg_printf(" #SE"); /* Stack Fault (don't think this can occur) */
171 if (ctx->FloatSave.StatusWord & 0x00000080) dbg_printf(" #ES"); /* Error Summary */
172 if (ctx->FloatSave.StatusWord & 0x00008000) dbg_printf(" #FB"); /* FPU Busy */
173 dbg_printf(")\n");
175 /* Here are the rest of the registers */
176 dbg_printf(" FLES:%08x FLDO:%08x FLDS:%08x FLCNS:%08x\n",
177 ctx->FloatSave.ErrorSelector,
178 ctx->FloatSave.DataOffset,
179 ctx->FloatSave.DataSelector,
180 ctx->FloatSave.Cr0NpxState);
182 /* Now for the floating point registers */
183 dbg_printf("Floating Point Registers:\n");
184 for (cnt = 0; cnt < 4; cnt++)
186 memcpy(&ST[cnt], &ctx->FloatSave.RegisterArea[cnt * 10], 10);
187 dbg_printf(" ST%d:%Lf ", cnt, ST[cnt]);
189 dbg_printf("\n");
190 for (cnt = 4; cnt < 8; cnt++)
192 memcpy(&ST[cnt], &ctx->FloatSave.RegisterArea[cnt * 10], 10);
193 dbg_printf(" ST%d:%Lf ", cnt, ST[cnt]);
195 dbg_printf("\n");
198 static void be_i386_print_context(HANDLE hThread, const CONTEXT* ctx, int all_regs)
200 static const char flags[] = "aVR-N--ODITSZ-A-P-C";
201 int i;
202 char buf[33];
204 dbg_printf("Register dump:\n");
206 /* First get the segment registers out of the way */
207 dbg_printf(" CS:%04x SS:%04x DS:%04x ES:%04x FS:%04x GS:%04x",
208 (WORD)ctx->SegCs, (WORD)ctx->SegSs,
209 (WORD)ctx->SegDs, (WORD)ctx->SegEs,
210 (WORD)ctx->SegFs, (WORD)ctx->SegGs);
212 strcpy(buf, flags);
213 for (i = 0; buf[i]; i++)
214 if (buf[i] != '-' && !(ctx->EFlags & (1 << (sizeof(flags) - 2 - i))))
215 buf[i] = ' ';
217 switch (get_selector_type(hThread, ctx, ctx->SegCs))
219 case AddrMode1616:
220 case AddrModeReal:
221 dbg_printf("\n IP:%04x SP:%04x BP:%04x FLAGS:%04x(%s)\n",
222 LOWORD(ctx->Eip), LOWORD(ctx->Esp),
223 LOWORD(ctx->Ebp), LOWORD(ctx->EFlags), buf);
224 dbg_printf(" AX:%04x BX:%04x CX:%04x DX:%04x SI:%04x DI:%04x\n",
225 LOWORD(ctx->Eax), LOWORD(ctx->Ebx),
226 LOWORD(ctx->Ecx), LOWORD(ctx->Edx),
227 LOWORD(ctx->Esi), LOWORD(ctx->Edi));
228 break;
229 case AddrModeFlat:
230 case AddrMode1632:
231 dbg_printf("\n EIP:%08x ESP:%08x EBP:%08x EFLAGS:%08x(%s)\n",
232 ctx->Eip, ctx->Esp, ctx->Ebp, ctx->EFlags, buf);
233 dbg_printf(" EAX:%08x EBX:%08x ECX:%08x EDX:%08x\n",
234 ctx->Eax, ctx->Ebx, ctx->Ecx, ctx->Edx);
235 dbg_printf(" ESI:%08x EDI:%08x\n",
236 ctx->Esi, ctx->Edi);
237 break;
240 if (all_regs) be_i386_all_print_context(hThread, ctx); /* print floating regs */
244 static void be_i386_print_segment_info(HANDLE hThread, const CONTEXT* ctx)
246 if (get_selector_type(hThread, ctx, ctx->SegCs) == AddrMode1616)
248 info_win32_segments(ctx->SegDs >> 3, 1);
249 if (ctx->SegEs != ctx->SegDs) info_win32_segments(ctx->SegEs >> 3, 1);
251 info_win32_segments(ctx->SegFs >> 3, 1);
254 static struct dbg_internal_var be_i386_ctx[] =
256 {CV_REG_AL, "AL", (DWORD_PTR*)FIELD_OFFSET(CONTEXT, Eax), dbg_itype_unsigned_char_int},
257 {CV_REG_CL, "CL", (DWORD_PTR*)FIELD_OFFSET(CONTEXT, Ecx), dbg_itype_unsigned_char_int},
258 {CV_REG_DL, "DL", (DWORD_PTR*)FIELD_OFFSET(CONTEXT, Edx), dbg_itype_unsigned_char_int},
259 {CV_REG_BL, "BL", (DWORD_PTR*)FIELD_OFFSET(CONTEXT, Ebx), dbg_itype_unsigned_char_int},
260 {CV_REG_AH, "AH", (DWORD_PTR*)(FIELD_OFFSET(CONTEXT, Eax)+1), dbg_itype_unsigned_char_int},
261 {CV_REG_CH, "CH", (DWORD_PTR*)(FIELD_OFFSET(CONTEXT, Ecx)+1), dbg_itype_unsigned_char_int},
262 {CV_REG_DH, "DH", (DWORD_PTR*)(FIELD_OFFSET(CONTEXT, Edx)+1), dbg_itype_unsigned_char_int},
263 {CV_REG_BH, "BH", (DWORD_PTR*)(FIELD_OFFSET(CONTEXT, Ebx)+1), dbg_itype_unsigned_char_int},
264 {CV_REG_AX, "AX", (DWORD_PTR*)FIELD_OFFSET(CONTEXT, Eax), dbg_itype_unsigned_short_int},
265 {CV_REG_CX, "CX", (DWORD_PTR*)FIELD_OFFSET(CONTEXT, Ecx), dbg_itype_unsigned_short_int},
266 {CV_REG_DX, "DX", (DWORD_PTR*)FIELD_OFFSET(CONTEXT, Edx), dbg_itype_unsigned_short_int},
267 {CV_REG_BX, "BX", (DWORD_PTR*)FIELD_OFFSET(CONTEXT, Ebx), dbg_itype_unsigned_short_int},
268 {CV_REG_SP, "SP", (DWORD_PTR*)FIELD_OFFSET(CONTEXT, Esp), dbg_itype_unsigned_short_int},
269 {CV_REG_BP, "BP", (DWORD_PTR*)FIELD_OFFSET(CONTEXT, Ebp), dbg_itype_unsigned_short_int},
270 {CV_REG_SI, "SI", (DWORD_PTR*)FIELD_OFFSET(CONTEXT, Esi), dbg_itype_unsigned_short_int},
271 {CV_REG_DI, "DI", (DWORD_PTR*)FIELD_OFFSET(CONTEXT, Edi), dbg_itype_unsigned_short_int},
272 {CV_REG_EAX, "EAX", (DWORD_PTR*)FIELD_OFFSET(CONTEXT, Eax), dbg_itype_unsigned_int},
273 {CV_REG_ECX, "ECX", (DWORD_PTR*)FIELD_OFFSET(CONTEXT, Ecx), dbg_itype_unsigned_int},
274 {CV_REG_EDX, "EDX", (DWORD_PTR*)FIELD_OFFSET(CONTEXT, Edx), dbg_itype_unsigned_int},
275 {CV_REG_EBX, "EBX", (DWORD_PTR*)FIELD_OFFSET(CONTEXT, Ebx), dbg_itype_unsigned_int},
276 {CV_REG_ESP, "ESP", (DWORD_PTR*)FIELD_OFFSET(CONTEXT, Esp), dbg_itype_unsigned_int},
277 {CV_REG_EBP, "EBP", (DWORD_PTR*)FIELD_OFFSET(CONTEXT, Ebp), dbg_itype_unsigned_int},
278 {CV_REG_ESI, "ESI", (DWORD_PTR*)FIELD_OFFSET(CONTEXT, Esi), dbg_itype_unsigned_int},
279 {CV_REG_EDI, "EDI", (DWORD_PTR*)FIELD_OFFSET(CONTEXT, Edi), dbg_itype_unsigned_int},
280 {CV_REG_ES, "ES", (DWORD_PTR*)FIELD_OFFSET(CONTEXT, SegEs), dbg_itype_unsigned_short_int},
281 {CV_REG_CS, "CS", (DWORD_PTR*)FIELD_OFFSET(CONTEXT, SegCs), dbg_itype_unsigned_short_int},
282 {CV_REG_SS, "SS", (DWORD_PTR*)FIELD_OFFSET(CONTEXT, SegSs), dbg_itype_unsigned_short_int},
283 {CV_REG_DS, "DS", (DWORD_PTR*)FIELD_OFFSET(CONTEXT, SegDs), dbg_itype_unsigned_short_int},
284 {CV_REG_FS, "FS", (DWORD_PTR*)FIELD_OFFSET(CONTEXT, SegFs), dbg_itype_unsigned_short_int},
285 {CV_REG_GS, "GS", (DWORD_PTR*)FIELD_OFFSET(CONTEXT, SegGs), dbg_itype_unsigned_short_int},
286 {CV_REG_IP, "IP", (DWORD_PTR*)FIELD_OFFSET(CONTEXT, Eip), dbg_itype_unsigned_short_int},
287 {CV_REG_FLAGS, "FLAGS", (DWORD_PTR*)FIELD_OFFSET(CONTEXT, EFlags), dbg_itype_unsigned_short_int},
288 {CV_REG_EIP, "EIP", (DWORD_PTR*)FIELD_OFFSET(CONTEXT, Eip), dbg_itype_unsigned_int},
289 {CV_REG_EFLAGS, "EFLAGS", (DWORD_PTR*)FIELD_OFFSET(CONTEXT, EFlags), dbg_itype_unsigned_int},
290 {0, NULL, 0, dbg_itype_none}
293 static const struct dbg_internal_var* be_i386_init_registers(CONTEXT* ctx)
295 struct dbg_internal_var* div;
297 for (div = be_i386_ctx; div->name; div++)
298 div->pval = (DWORD_PTR*)((char*)ctx + (DWORD)div->pval);
299 return be_i386_ctx;
302 static unsigned be_i386_is_step_over_insn(const void* insn)
304 BYTE ch;
306 for (;;)
308 if (!dbg_read_memory(insn, &ch, sizeof(ch))) return FALSE;
310 switch (ch)
312 /* Skip all prefixes */
313 case 0x2e: /* cs: */
314 case 0x36: /* ss: */
315 case 0x3e: /* ds: */
316 case 0x26: /* es: */
317 case 0x64: /* fs: */
318 case 0x65: /* gs: */
319 case 0x66: /* opcode size prefix */
320 case 0x67: /* addr size prefix */
321 case 0xf0: /* lock */
322 case 0xf2: /* repne */
323 case 0xf3: /* repe */
324 insn = (const char*)insn + 1;
325 continue;
327 /* Handle call instructions */
328 case 0xcd: /* int <intno> */
329 case 0xe8: /* call <offset> */
330 case 0x9a: /* lcall <seg>:<off> */
331 return TRUE;
333 case 0xff: /* call <regmodrm> */
334 if (!dbg_read_memory((const char*)insn + 1, &ch, sizeof(ch)))
335 return FALSE;
336 return (((ch & 0x38) == 0x10) || ((ch & 0x38) == 0x18));
338 /* Handle string instructions */
339 case 0x6c: /* insb */
340 case 0x6d: /* insw */
341 case 0x6e: /* outsb */
342 case 0x6f: /* outsw */
343 case 0xa4: /* movsb */
344 case 0xa5: /* movsw */
345 case 0xa6: /* cmpsb */
346 case 0xa7: /* cmpsw */
347 case 0xaa: /* stosb */
348 case 0xab: /* stosw */
349 case 0xac: /* lodsb */
350 case 0xad: /* lodsw */
351 case 0xae: /* scasb */
352 case 0xaf: /* scasw */
353 return TRUE;
355 default:
356 return FALSE;
361 static unsigned be_i386_is_function_return(const void* insn)
363 BYTE ch;
365 if (!dbg_read_memory(insn, &ch, sizeof(ch))) return FALSE;
366 return (ch == 0xC2) || (ch == 0xC3);
369 static unsigned be_i386_is_break_insn(const void* insn)
371 BYTE c;
373 if (!dbg_read_memory(insn, &c, sizeof(c))) return FALSE;
374 return c == 0xCC;
377 static unsigned get_size(ADDRESS_MODE am)
379 if (am == AddrModeReal || am == AddrMode1616) return 16;
380 return 32;
383 static BOOL fetch_value(const char* addr, unsigned sz, int* value)
385 char value8;
386 short value16;
388 switch (sz)
390 case 8:
391 if (!dbg_read_memory(addr, &value8, sizeof(value8)))
392 return FALSE;
393 *value = value8;
394 break;
395 case 16:
396 if (!dbg_read_memory(addr, &value16, sizeof(value16)))
397 return FALSE;
398 *value = value16;
399 case 32:
400 if (!dbg_read_memory(addr, value, sizeof(*value)))
401 return FALSE;
402 break;
403 default: return FALSE;
405 return TRUE;
408 static unsigned be_i386_is_func_call(const void* insn, ADDRESS64* callee)
410 BYTE ch;
411 int delta;
412 short segment;
413 unsigned dst = 0;
414 unsigned operand_size;
415 ADDRESS_MODE cs_addr_mode;
417 cs_addr_mode = get_selector_type(dbg_curr_thread->handle, &dbg_context,
418 dbg_context.SegCs);
419 operand_size = get_size(cs_addr_mode);
421 /* get operand_size (also getting rid of the various prefixes */
424 if (!dbg_read_memory(insn, &ch, sizeof(ch))) return FALSE;
425 if (ch == 0x66)
427 operand_size = 48 - operand_size; /* 16 => 32, 32 => 16 */
428 insn = (const char*)insn + 1;
430 } while (ch == 0x66 || ch == 0x67);
432 switch (ch)
434 case 0xe8: /* relative near call */
435 callee->Mode = cs_addr_mode;
436 if (!fetch_value((const char*)insn + 1, operand_size, &delta))
437 return FALSE;
438 callee->Segment = dbg_context.SegCs;
439 callee->Offset = (DWORD)insn + 1 + (operand_size / 8) + delta;
440 return TRUE;
442 case 0x9a: /* absolute far call */
443 if (!dbg_read_memory((const char*)insn + 1 + operand_size / 8,
444 &segment, sizeof(segment)))
445 return FALSE;
446 callee->Mode = get_selector_type(dbg_curr_thread->handle, &dbg_context,
447 segment);
448 if (!fetch_value((const char*)insn + 1, operand_size, &delta))
449 return FALSE;
450 callee->Segment = segment;
451 callee->Offset = delta;
452 return TRUE;
454 case 0xff:
455 if (!dbg_read_memory((const char*)insn + 1, &ch, sizeof(ch)))
456 return FALSE;
457 /* keep only the CALL and LCALL insn:s */
458 switch ((ch >> 3) & 0x07)
460 case 0x02:
461 segment = dbg_context.SegCs;
462 break;
463 case 0x03:
464 if (!dbg_read_memory((const char*)insn + 1 + operand_size / 8,
465 &segment, sizeof(segment)))
466 return FALSE;
467 break;
468 default: return FALSE;
470 /* FIXME: we only support the 32 bit far calls for now */
471 if (operand_size != 32)
473 WINE_FIXME("Unsupported yet call insn (0xFF 0x%02x) with 16 bit operand-size at %p\n", ch, insn);
474 return FALSE;
476 switch (ch & 0xC7) /* keep Mod R/M only (skip reg) */
478 case 0x04:
479 case 0x44:
480 case 0x84:
481 WINE_FIXME("Unsupported yet call insn (0xFF 0x%02x) (SIB bytes) at %p\n", ch, insn);
482 return FALSE;
483 case 0x05: /* addr32 */
484 if ((ch & 0x38) == 0x10 || /* call */
485 (ch & 0x38) == 0x18) /* lcall */
487 void *addr;
488 if (!dbg_read_memory((const char *)insn + 2, &addr, sizeof(addr)))
489 return FALSE;
490 if ((ch & 0x38) == 0x18) /* lcall */
492 if (!dbg_read_memory((const char*)addr + operand_size, &segment, sizeof(segment)))
493 return FALSE;
495 else segment = dbg_context.SegCs;
496 if (!dbg_read_memory((const char*)addr, &dst, sizeof(dst)))
497 return FALSE;
498 callee->Mode = get_selector_type(dbg_curr_thread->handle, &dbg_context, segment);
499 callee->Segment = segment;
500 callee->Offset = dst;
501 return TRUE;
503 return FALSE;
504 default:
505 switch (ch & 0x07)
507 case 0x00: dst = dbg_context.Eax; break;
508 case 0x01: dst = dbg_context.Ecx; break;
509 case 0x02: dst = dbg_context.Edx; break;
510 case 0x03: dst = dbg_context.Ebx; break;
511 case 0x04: dst = dbg_context.Esp; break;
512 case 0x05: dst = dbg_context.Ebp; break;
513 case 0x06: dst = dbg_context.Esi; break;
514 case 0x07: dst = dbg_context.Edi; break;
516 if ((ch >> 6) != 0x03) /* indirect address */
518 if (ch >> 6) /* we got a displacement */
520 if (!fetch_value((const char*)insn + 2, (ch >> 6) == 0x01 ? 8 : 32, &delta))
521 return FALSE;
522 dst += delta;
524 if (((ch >> 3) & 0x07) == 0x03) /* LCALL */
526 if (!dbg_read_memory((const char*)dst + operand_size, &segment, sizeof(segment)))
527 return FALSE;
529 else segment = dbg_context.SegCs;
530 if (!dbg_read_memory((const char*)dst, &delta, sizeof(delta)))
531 return FALSE;
532 callee->Mode = get_selector_type(dbg_curr_thread->handle, &dbg_context,
533 segment);
534 callee->Segment = segment;
535 callee->Offset = delta;
537 else
539 callee->Mode = cs_addr_mode;
540 callee->Segment = dbg_context.SegCs;
541 callee->Offset = dst;
544 return TRUE;
546 default:
547 return FALSE;
551 #define DR7_CONTROL_SHIFT 16
552 #define DR7_CONTROL_SIZE 4
554 #define DR7_RW_EXECUTE (0x0)
555 #define DR7_RW_WRITE (0x1)
556 #define DR7_RW_READ (0x3)
558 #define DR7_LEN_1 (0x0)
559 #define DR7_LEN_2 (0x4)
560 #define DR7_LEN_4 (0xC)
562 #define DR7_LOCAL_ENABLE_SHIFT 0
563 #define DR7_GLOBAL_ENABLE_SHIFT 1
564 #define DR7_ENABLE_SIZE 2
566 #define DR7_LOCAL_ENABLE_MASK (0x55)
567 #define DR7_GLOBAL_ENABLE_MASK (0xAA)
569 #define DR7_CONTROL_RESERVED (0xFC00)
570 #define DR7_LOCAL_SLOWDOWN (0x100)
571 #define DR7_GLOBAL_SLOWDOWN (0x200)
573 #define DR7_ENABLE_MASK(dr) (1<<(DR7_LOCAL_ENABLE_SHIFT+DR7_ENABLE_SIZE*(dr)))
574 #define IS_DR7_SET(ctrl,dr) ((ctrl)&DR7_ENABLE_MASK(dr))
576 static inline int be_i386_get_unused_DR(CONTEXT* ctx, DWORD** r)
578 if (!IS_DR7_SET(ctx->Dr7, 0))
580 *r = &ctx->Dr0;
581 return 0;
583 if (!IS_DR7_SET(ctx->Dr7, 1))
585 *r = &ctx->Dr1;
586 return 1;
588 if (!IS_DR7_SET(ctx->Dr7, 2))
590 *r = &ctx->Dr2;
591 return 2;
593 if (!IS_DR7_SET(ctx->Dr7, 3))
595 *r = &ctx->Dr3;
596 return 3;
598 dbg_printf("All hardware registers have been used\n");
600 return -1;
603 static unsigned be_i386_insert_Xpoint(HANDLE hProcess, const struct be_process_io* pio,
604 CONTEXT* ctx, enum be_xpoint_type type,
605 void* addr, unsigned long* val, unsigned size)
607 unsigned char ch;
608 SIZE_T sz;
609 DWORD *pr;
610 int reg;
611 unsigned long bits;
613 switch (type)
615 case be_xpoint_break:
616 if (size != 0) return 0;
617 if (!pio->read(hProcess, addr, &ch, 1, &sz) || sz != 1) return 0;
618 *val = ch;
619 ch = 0xcc;
620 if (!pio->write(hProcess, addr, &ch, 1, &sz) || sz != 1) return 0;
621 break;
622 case be_xpoint_watch_exec:
623 bits = DR7_RW_EXECUTE;
624 goto hw_bp;
625 case be_xpoint_watch_read:
626 bits = DR7_RW_READ;
627 goto hw_bp;
628 case be_xpoint_watch_write:
629 bits = DR7_RW_WRITE;
630 hw_bp:
631 if ((reg = be_i386_get_unused_DR(ctx, &pr)) == -1) return 0;
632 *pr = (DWORD)addr;
633 if (type != be_xpoint_watch_exec) switch (size)
635 case 4: bits |= DR7_LEN_4; break;
636 case 2: bits |= DR7_LEN_2; break;
637 case 1: bits |= DR7_LEN_1; break;
638 default: return 0;
640 *val = reg;
641 /* clear old values */
642 ctx->Dr7 &= ~(0x0F << (DR7_CONTROL_SHIFT + DR7_CONTROL_SIZE * reg));
643 /* set the correct ones */
644 ctx->Dr7 |= bits << (DR7_CONTROL_SHIFT + DR7_CONTROL_SIZE * reg);
645 ctx->Dr7 |= DR7_ENABLE_MASK(reg) | DR7_LOCAL_SLOWDOWN;
646 break;
647 default:
648 dbg_printf("Unknown bp type %c\n", type);
649 return 0;
651 return 1;
654 static unsigned be_i386_remove_Xpoint(HANDLE hProcess, const struct be_process_io* pio,
655 CONTEXT* ctx, enum be_xpoint_type type,
656 void* addr, unsigned long val, unsigned size)
658 SIZE_T sz;
659 unsigned char ch;
661 switch (type)
663 case be_xpoint_break:
664 if (size != 0) return 0;
665 if (!pio->read(hProcess, addr, &ch, 1, &sz) || sz != 1) return 0;
666 if (ch != (unsigned char)0xCC)
667 WINE_FIXME("Cannot get back %02x instead of 0xCC at %08lx\n",
668 ch, (unsigned long)addr);
669 ch = (unsigned char)val;
670 if (!pio->write(hProcess, addr, &ch, 1, &sz) || sz != 1) return 0;
671 break;
672 case be_xpoint_watch_exec:
673 case be_xpoint_watch_read:
674 case be_xpoint_watch_write:
675 /* simply disable the entry */
676 ctx->Dr7 &= ~DR7_ENABLE_MASK(val);
677 break;
678 default:
679 dbg_printf("Unknown bp type %c\n", type);
680 return 0;
682 return 1;
685 static unsigned be_i386_is_watchpoint_set(const CONTEXT* ctx, unsigned idx)
687 return ctx->Dr6 & (1 << idx);
690 static void be_i386_clear_watchpoint(CONTEXT* ctx, unsigned idx)
692 ctx->Dr6 &= ~(1 << idx);
695 static int be_i386_adjust_pc_for_break(CONTEXT* ctx, BOOL way)
697 if (way)
699 ctx->Eip--;
700 return -1;
702 ctx->Eip++;
703 return 1;
706 static int be_i386_fetch_integer(const struct dbg_lvalue* lvalue, unsigned size,
707 unsigned ext_sign, LONGLONG* ret)
709 if (size != 1 && size != 2 && size != 4 && size != 8) return FALSE;
711 memset(ret, 0, sizeof(*ret)); /* clear unread bytes */
712 /* FIXME: this assumes that debuggee and debugger use the same
713 * integral representation
715 if (!memory_read_value(lvalue, size, ret)) return FALSE;
717 /* propagate sign information */
718 if (ext_sign && size < 8 && (*ret >> (size * 8 - 1)) != 0)
720 ULONGLONG neg = -1;
721 *ret |= neg << (size * 8);
723 return TRUE;
726 static int be_i386_fetch_float(const struct dbg_lvalue* lvalue, unsigned size,
727 long double* ret)
729 char tmp[sizeof(long double)];
731 /* FIXME: this assumes that debuggee and debugger use the same
732 * representation for reals
734 if (!memory_read_value(lvalue, size, tmp)) return FALSE;
736 /* float & double types have to be promoted to a long double */
737 switch (size)
739 case sizeof(float): *ret = *(float*)tmp; break;
740 case sizeof(double): *ret = *(double*)tmp; break;
741 case sizeof(long double): *ret = *(long double*)tmp; break;
742 default: return FALSE;
744 return TRUE;
747 struct backend_cpu be_i386 =
749 IMAGE_FILE_MACHINE_I386,
750 be_i386_linearize,
751 be_i386_build_addr,
752 be_i386_get_addr,
753 be_i386_get_register_info,
754 be_i386_single_step,
755 be_i386_print_context,
756 be_i386_print_segment_info,
757 be_i386_init_registers,
758 be_i386_is_step_over_insn,
759 be_i386_is_function_return,
760 be_i386_is_break_insn,
761 be_i386_is_func_call,
762 be_i386_disasm_one_insn,
763 be_i386_insert_Xpoint,
764 be_i386_remove_Xpoint,
765 be_i386_is_watchpoint_set,
766 be_i386_clear_watchpoint,
767 be_i386_adjust_pc_for_break,
768 be_i386_fetch_integer,
769 be_i386_fetch_float,
771 #endif