void readRegister(int fd, int flag) { if ( flag ) { requestReg_ReadAll(fd); PrintRegisters(fd); } else { if (rnumber == REGISTER_ADMODE_ID) { gADMode = getADMode(fd); #ifdef DEBUG printf("Mode : %d\n", gADMode); #else printf("%d", gADMode); #endif } else { requestReg_Read(fd, rnumber); if (rnumber == 0) printf("%d\n", REGDATA_inner[0]); else PrintRegister(fd, rnumber); } } }
DWORD TraceBpHandler( PPROCESS_INFO ThisProcess, PTHREAD_INFO ThisThread, PEXCEPTION_RECORD ExceptionRecord, PBREAKPOINT_INFO BreakpointInfo ) { CONTEXT Context; #ifdef _M_IX86 if (BreakpointInfo->Flags & BPF_TRACE) { CurrContext.EFlags &= ~0x100; SetRegContext( ThisThread->hThread, &CurrContext ); } #endif if (BreakpointInfo->LastBp) { WriteMemory( ThisProcess->hProcess, (PVOID) BreakpointInfo->LastBp->Address, &BpInstr, BpSize ); BreakpointInfo->LastBp = NULL; } // // clear the trace breakpoint // ClearBreakpoint( ThisProcess, BreakpointInfo ); // // print the registers // if (PrintRegistersFlag) { PrintRegisters(); } // // print the code // PrintOneInstruction( ThisProcess->hProcess, (ULONG)ExceptionRecord->ExceptionAddress ); // // enter the debugger // ULONG ContinueStatus = ConsoleDebugger( ThisThread->hProcess, ThisThread->hThread, ExceptionRecord, FALSE, BreakpointInfo->Command ); // // continue the debuggee // return ContinueStatus; }
BOOL CmdDisplayRegisters( LPSTR CmdBuf, HANDLE hProcess, HANDLE hThread, PEXCEPTION_RECORD ExceptionRecord ) { PrintRegisters(); return TRUE; }
DWORD UserBpHandler( PPROCESS_INFO ThisProcess, PTHREAD_INFO ThisThread, PEXCEPTION_RECORD ExceptionRecord, PBREAKPOINT_INFO BreakpointInfo ) { if (BreakpointInfo->Number) { printf( "Breakpoint #%d hit\n", BreakpointInfo->Number ); } else { printf( "Hardcoded breakpoint hit\n" ); } if (PrintRegistersFlag) PrintRegisters(); PrintOneInstruction( ThisProcess->hProcess, (ULONG)ExceptionRecord->ExceptionAddress ); ULONG ContinueStatus = ConsoleDebugger( ThisThread->hProcess, ThisThread->hThread, ExceptionRecord, FALSE, BreakpointInfo->Command ); if (BreakpointInfo->Address && (!Stepped)) { // // the bp is still present so we must step off it // SuspendAllThreads( ThisProcess, ThisThread ); ULONG Address = GetNextOffset( ThisProcess->hProcess, (ULONG)ExceptionRecord->ExceptionAddress, TRUE ); if (Address != (ULONG)-1) { PBREAKPOINT_INFO bp = SetBreakpoint( ThisProcess, Address, 0, NULL, UserBpStepHandler ); if (bp) { bp->LastBp = BreakpointInfo; } else { printf( "could not set off of the breakpoint\n" ); } } else { #ifdef _M_IX86 CurrContext.EFlags |= 0x100; SetRegContext( ThisThread->hThread, &CurrContext ); PBREAKPOINT_INFO bp = GetAvailBreakpoint( ThisProcess ); if (bp) { bp->Flags |= BPF_TRACE; bp->Address = (ULONG)ExceptionRecord->ExceptionAddress; bp->Handler = TraceBpHandler; bp->LastBp = BreakpointInfo; } else { printf( "could not step off of the breakpoint\n" ); } #else printf( "could not step off of the breakpoint\n" ); #endif } } return ContinueStatus; }
DWORD ConsoleDebugger( HANDLE hProcess, HANDLE hThread, PEXCEPTION_RECORD ExceptionRecord, BOOL UnexpectedException, LPSTR InitialCommand ) { PPROCESS_INFO ThisProcess; DWORD ContinueStatus = DBG_EXCEPTION_NOT_HANDLED; static CHAR CmdBuf[512]; Stepped = FALSE; if (!ConsoleCreated) { CmdBuf[0] = 0; if (!CreateDebuggerConsole()) { return ContinueStatus; } } ThisProcess = GetProcessInfo( hProcess ); if (!ThisProcess) { printf( "could not get process information\n" ); } if (UnexpectedException) { printf( "\n" ); printf( "*---------------------------------------\n" ); printf( "An unexpected error has occurred\n" ); printf( "Address: 0x%08x\n", ExceptionRecord->ExceptionAddress ); printf( "Error code: 0x%08x\n", ExceptionRecord->ExceptionCode ); printf( "*---------------------------------------\n" ); PrintRegisters(); PrintOneInstruction( hProcess, (ULONG)ExceptionRecord->ExceptionAddress ); } if (BreakInNow) { BreakInNow = FALSE; printf( "*** Initial breakpoint\n\n" ); } // // check to see if any modules need symbols loading // for (ULONG i=0; i<MAX_DLLS; i++) { if (DllList[i].BaseAddress && !DllList[i].Unloaded) { IMAGEHLP_MODULE ModuleInfo; if (!SymGetModuleInfo( hProcess, DllList[i].BaseAddress, &ModuleInfo )) { if (ThisProcess) { printf( "loading 0x%08x %s\n", DllList[i].BaseAddress, DllList[i].Name ); LoadSymbols( ThisProcess, &DllList[i], NULL ); } } } } CurrProcess = hProcess; if (InitialCommand) { strcpy( CmdBuf, InitialCommand ); } while( TRUE ) { if (!InitialCommand) { printf( "ApiMon> " ); scanf( "%[^\n]", CmdBuf ); getchar(); } LPSTR p = CmdBuf; while (p[0]) { LPSTR s = p; while (*s) { if (*s == '\"') { s += 1; while (*s && *s != '\"') { s += 1; } if (*s == '\"') { s += 1; } } if (*s == ';') { break; } s += 1; } if (*s == ';') { s[0] = 0; } else { s = NULL; } switch( tolower(p[0]) ) { case 'q': ExitProcess( 0 ); break; case 'g': ContinueStatus = DBG_CONTINUE; goto exit; case 'k': CmdStackTrace( p, hProcess, hThread, ExceptionRecord ); break; case 'd': CmdDisplayMemory( p, hProcess, hThread, ExceptionRecord ); break; case 'r': if (p[1] == 't') { PrintRegistersFlag = !PrintRegistersFlag; } CmdDisplayRegisters( p, hProcess, hThread, ExceptionRecord ); break; case 'u': CmdDisplayCode( p, hProcess, hThread, ExceptionRecord ); break; case 'b': CmdBreakPoint( p, hProcess, hThread, ExceptionRecord ); break; case 'l': if (tolower(p[1]) == 'm') { CmdDisplayModules( p, hProcess, hThread, ExceptionRecord ); } else if (tolower(p[1]) == 'n') { CmdListNear( p, hProcess, hThread, ExceptionRecord ); } else { goto invalid_command; } break; case 't': if (p[1] == 'r') { PrintRegistersFlag = !PrintRegistersFlag; } if (CmdTrace( p, hProcess, hThread, ExceptionRecord )) { ContinueStatus = DBG_CONTINUE; Stepped = TRUE; goto exit; } break; case 'p': if (p[1] == 'r') { PrintRegistersFlag = !PrintRegistersFlag; } if (CmdStep( p, hProcess, hThread, ExceptionRecord )) { ContinueStatus = DBG_CONTINUE; Stepped = TRUE; goto exit; } break; case 'h': if (tolower(p[1]) == 'e' && tolower(p[2]) == 'l' && tolower(p[3]) == 'p') { CmdDisplayHelp( p, hProcess, hThread, ExceptionRecord ); } break; case '?': { ULONG val = GetExpression( p+1 ); if (!ExprError) { printf( "Evaluate expression: %d = 0x%08x\n", val, val ); } } break; default: invalid_command: printf( "****>>> invalid command\n" ); break; } if (s) { p = s + 1; } else { p += strlen(p); } } } exit: return ContinueStatus; }