📄 dbgmain.c
字号:
//-------------------------------------------------------------------------
void ClearBreakPoints(int procid)
{
BREAKPOINT *p = &DebugProcess.breakpoints;
THREAD *th = DebugProcess.threads;
#ifdef HBREAK
hbpResetBP();
#endif
while (p)
{
freeBreakPoint(DebugProcess.hProcess, p);
p = p->next;
}
while (th)
{
freeBreakPoint(DebugProcess.hProcess, &th->breakpoint);
th = th->next;
}
}
//-------------------------------------------------------------------------
int isBreakPoint(int addr)
{
BREAKPOINT *p = DebugProcess.breakpoints.next;
THREAD *th = DebugProcess.threads;
while (p)
{
if (addr == p->address)
return TRUE;
p = p->next;
}
return FALSE;
}
//-------------------------------------------------------------------------
int isLocalBreakPoint(int addr)
{
THREAD *th = DebugProcess.threads;
while (th)
{
if (addr == th->breakpoint.address)
return TRUE;
th = th->next;
}
return isBreakPoint(addr);
}
//-------------------------------------------------------------------------
#ifdef XXXXX
int IsBreakpointLine(char *module, int line)
{
BREAKPOINT *p = DebugProcess.breakpoints.next;
while (p)
{
if (p->linenum)
if (p->linenum == line && !xstricmp(module, p->module))
return TRUE;
p = p->next;
}
return FALSE;
}
#endif
int StepOverIncrement(DEBUG_EVENT *dbe)
{
unsigned char buf[16];
int word = 0;
int address;
int skiplen = 0;
ReadProcessMemory(DebugProcess.hProcess, (LPVOID)(address = dbe
->u.Exception.ExceptionRecord.ExceptionAddress), (LPVOID)buf, 16, 0);
switch (buf[0])
{
case 0xCE:
skiplen = 1;
break;
case 0xCD:
skiplen = 2;
break;
case 0xE8:
skiplen = 5;
break;
default:
word = (*(short*)buf) &0x38ff;
if (word == 0x10ff || word == 0x18ff)
{
skiplen = 2;
if ((word = (buf[1] &0xc7)) < 0xc0)
{
// not indirect through reg
if (word == 6)
// Offset
skiplen += 4;
else
{
if (word >= 8)
if (word >= 0x80)
skiplen += 4;
else
skiplen += 1;
word &= 7;
if (word == 4)
{
skiplen++; /* has a SIB */
if (*(buf + 1) < 0xc0)
{
word = *(buf + 2);
if ((word &7) == 5)
{
/* EBP special cases */
if (*(buf + 1) &0x40)
skiplen += 4;
else
skiplen += 1;
}
}
}
}
}
}
break;
}
if (skiplen)
return address + skiplen;
return 0;
}
//-------------------------------------------------------------------------
int DoStepOver(DEBUG_EVENT *dbe)
{
int skipaddr = StepOverIncrement(dbe);
if (skipaddr)
{
SetTempBreakPoint(dbe->dwProcessId, dbe->dwThreadId, skipaddr);
}
else
{
SingleStep(dbe->dwProcessId, dbe->dwThreadId);
}
}
//-------------------------------------------------------------------------
int DoStepIn(DEBUG_EVENT *dbe)
{
LastSkipAddr = StepOverIncrement(dbe);
SingleStep(dbe->dwProcessId, dbe->dwThreadId);
}
//-------------------------------------------------------------------------
int IsStepping(DEBUG_EVENT *dbe)
{
char module[256];
int line;
if (uState == SteppingOver)
{
int v;
if ((v = GetBreakpointLine((int)dbe
->u.Exception.ExceptionRecord.ExceptionAddress, module, &line)) !=
dbe->u.Exception.ExceptionRecord.ExceptionAddress)
{
DoStepOver(dbe);
return TRUE;
}
else
{
uState = Running;
return FALSE;
}
}
else if (uState == SteppingIn)
{
int addr = GetBreakpointLine((int)dbe
->u.Exception.ExceptionRecord.ExceptionAddress, module, &line);
if (addr == dbe->u.Exception.ExceptionRecord.ExceptionAddress)
{
uState = Running;
return FALSE;
}
else if (LastSkipAddr)
{
if (dbe->u.Exception.ExceptionRecord.ExceptionAddress !=
LastSkipAddr)
{
if (addr)
{
uState = SteppingOver;
DoStepOver(dbe);
return TRUE;
}
else
{
SetTempBreakPoint(dbe->dwProcessId, dbe->dwThreadId,
LastSkipAddr);
uState = StepInOut;
return TRUE;
}
}
else
{
DoStepIn(dbe);
return TRUE;
}
}
else
{
DoStepIn(dbe);
return TRUE;
}
}
else if (uState == StepInOut)
{
int addr = GetBreakpointLine((int)dbe
->u.Exception.ExceptionRecord.ExceptionAddress, module, &line);
if (addr == dbe->u.Exception.ExceptionRecord.ExceptionAddress)
{
uState = Running;
return FALSE;
}
else
{
uState = SteppingIn;
DoStepIn(dbe);
return TRUE;
}
}
else if (uState == FinishStepOut)
{
int addr = GetBreakpointLine((int)dbe
->u.Exception.ExceptionRecord.ExceptionAddress, module, &line);
if (!addr || addr == dbe->u.Exception.ExceptionRecord.ExceptionAddress)
{
uState = Running;
return FALSE;
}
else
{
uState = SteppingIn;
DoStepIn(dbe);
return TRUE;
}
}
return FALSE;
}
//-------------------------------------------------------------------------
void StepOver(DEBUG_EVENT *dbe)
{
if (GetFocus() != hwndASM)
uState = SteppingOver;
SaveRegisterContext();
DoStepOver(dbe);
ReleaseSemaphore(BreakpointSem, 1, 0);
}
//-------------------------------------------------------------------------
void StepOut(DEBUG_EVENT *dbe)
{
uState = SteppingOut;
SaveRegisterContext();
ReleaseSemaphore(BreakpointSem, 1, 0);
}
//-------------------------------------------------------------------------
void StepIn(DEBUG_EVENT *dbe)
{
if (GetFocus() != hwndASM)
uState = SteppingIn;
SaveRegisterContext();
DoStepIn(dbe);
ReleaseSemaphore(BreakpointSem, 1, 0);
}
//-------------------------------------------------------------------------
int RunTo(DEBUG_EVENT *dbe)
{
int addr;
HWND wnd = GetFocus();
if (wnd == hwndASM)
{
if (!(addr = SendMessage(hwndASM, WM_GETCURSORADDRESS, 0, 0)))
return FALSE;
SetTempBreakPoint(dbe->dwProcessId, dbe->dwThreadId, addr);
SaveRegisterContext();
ReleaseSemaphore(BreakpointSem, 1, 0);
}
else
{
wnd = GetParent(wnd);
if (!IsSpecialWindow(wnd))
{
DWINFO *ptr = (DWINFO*)GetWindowLong(wnd, 0);
int linenum;
SendMessage(ptr->dwHandle, EM_GETSEL, (WPARAM) &linenum, 0);
linenum = SendMessage(ptr->dwHandle, EM_EXLINEFROMCHAR, 0, linenum)
+ 1;
linenum = TagOldLine(ptr->dwName, linenum);
addr = GetBreakpointAddress(ptr->dwName, &linenum, FALSE);
if (addr)
{
SetTempBreakPoint(dbe->dwProcessId, dbe->dwThreadId, addr);
SaveRegisterContext();
ReleaseSemaphore(BreakpointSem, 1, 0);
}
else
return FALSE;
}
else
return FALSE;
}
return TRUE;
}
//-------------------------------------------------------------------------
int dbgSetBreakPoint(char *name, int linenum, char *extra)
{
BREAKPOINT **p = &DebugProcess.breakpoints.next;
if (uState == notDebugging)
return 1;
// will be checked when entering debugger
if (!name)
{
// it was from the assembly window
int addr = linenum;
while (*p)
{
if ((*p)->address == addr)
return 1 ;
p = &(*p)->next;
}
*p = malloc(sizeof(BREAKPOINT));
if (*p)
{
memset(*p, 0, sizeof(BREAKPOINT));
(*p)->address = addr;
(*p)->extra = extra;
GetBreakpointLine(addr, &(*p)->module, &(*p)->linenum);
if (uState == Running)
allocBreakPoint(DebugProcess.hProcess, *p);
}
return 1;
}
else
{
// it was from a source module
int addr = GetBreakpointAddress(name, &linenum, FALSE);
if (addr)
{
while (*p)
{
if ((*p)->address == addr)
return 1 ;
p = &(*p)->next;
}
*p = malloc(sizeof(BREAKPOINT));
if (*p)
{
memset(*p, 0, sizeof(BREAKPOINT));
(*p)->address = addr;
(*p)->extra = extra;
strcpy((*p)->module, name);
(*p)->linenum = linenum;
if (hwndASM)
InvalidateRect(hwndASM, 0, 0);
if (uState == Running)
allocBreakPoint(DebugProcess.hProcess, *p);
}
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -