--- mstools/samples/deb/debdebug.c 2018/08/09 18:20:39 1.1 +++ mstools/samples/deb/debdebug.c 2018/08/09 18:21:30 1.1.1.2 @@ -1,40 +1,54 @@ // ************************************************************************ -// MODULE : DEBDebug.c +// +// Microsoft Developer Support +// Copyright (c) 1992 Microsoft Corporation +// +// ************************************************************************ +// MODULE : DEBDebug.C // PURPOSE : Debug support functions for the Debug Event Browser // FUNCTIONS : // DbgEventThread() - debug event processing thread // GetModuleFileNameFromHeader() - get the module name -// SkipBreakPoint() - skips over the break point in the debugee +// SkipBreakPoint() - skips over the break point in the +// debugee (R4000 only) // COMMENTS : +// // ************************************************************************ +#define STRICT #include -#include // strcpy(), strcat() -#include // sprintf() #include "LinkList.H" #include "DEBMain.H" #include "DEBMisc.H" #include "DEBDebug.H" +//-- internal fucntion prototypes +DWORD WINAPI GetModuleFileNameFromHeader( HANDLE, HANDLE, LPTSTR, DWORD ); +#if( MIPS == 1 ) + BOOL SkipBreakPoint( DWORD ); +#endif // ************************************************************************ // FUNCTION : DebugEventThread( LPCTSTR ) // PURPOSE : Main debug event processing loop -// COMMENTS : A new debug event thread is created for each debugee process. -// DebugeeFileName may be the process ID of and active process -// or the name of a process to create. -// Note: this build only supports browsing one process at a time. +// COMMENTS : +// A new debug event thread is created for each debugee process. +// DebugeeFileName may be the process ID of an active process or the +// name of a process to create. // ************************************************************************ -DWORD +DWORD WINAPI DebugEventThread( LPCTSTR DebugeeFileName ) { + #define STRING_BUFSIZ 256 + static BOOL fFirstTime = TRUE; + static TCHAR szDebugeeTitle[128]; static PNODE ThreadNode; // used for inserting new thread nodes PNODE TempNode; // used for queries on thread linked list DEBUG_EVENT DebugEvent; - TCHAR szDebugEventBuffer[256]; - TCHAR szTempBuf[256]; + TCHAR szDebugEventBuffer[STRING_BUFSIZ]; + TCHAR szTempBuf[STRING_BUFSIZ]; DWORD NumberOfBytesRead; //-- temporary thread info node used for queries @@ -44,6 +58,29 @@ DebugEventThread( LPCTSTR DebugeeFileNam // DebugeeFileName may be dwProcessId and an actual filename if( !DebugActiveProcess( (DWORD) DebugeeFileName ) ) { + STARTUPINFO StartInfo; + LPSTARTUPINFO lpStartInfo = &StartInfo; + + PROCESS_INFORMATION ProcessInfo; + LPPROCESS_INFORMATION lpProcessInfo = &ProcessInfo; + + if( fFirstTime ) { + LoadString( hInstance, IDS_OFN_DEBUGEE_TITLE, szDebugeeTitle, + sizeof(szDebugeeTitle)/sizeof(TCHAR) ); + } + + lpStartInfo->cb = sizeof( STARTUPINFO ); + lpStartInfo->lpDesktop = NULL; + lpStartInfo->lpTitle = szDebugeeTitle; + lpStartInfo->dwX = 0; + lpStartInfo->dwY = 0; + lpStartInfo->dwXSize = 0; + lpStartInfo->dwYSize = 0; + lpStartInfo->dwFlags = NULL; + lpStartInfo->wShowWindow = SW_SHOWDEFAULT; + + lpProcessInfo->hProcess = NULL; + //-- create the debugee process instead if( !CreateProcess( NULL, @@ -54,16 +91,17 @@ DebugEventThread( LPCTSTR DebugeeFileNam DebugMode | DebugeePriority | CREATE_NEW_CONSOLE, (LPVOID) NULL, (LPTSTR) NULL, - &si, &pi ) ) { + &StartInfo, &ProcessInfo ) ) { - return( FALSE ); + ErrorBox( "Failed", "CreateProcess()", __FILE__, __LINE__ ); + ExitThread( FALSE ); } else { - hProcess = pi.hProcess; - CloseHandle( pi.hProcess ); - CloseHandle( pi.hThread ); + hProcess = ProcessInfo.hProcess; + CloseHandle( ProcessInfo.hProcess ); + CloseHandle( ProcessInfo.hThread ); } } @@ -83,74 +121,74 @@ DebugEventThread( LPCTSTR DebugeeFileNam switch( DebugEvent.u.Exception.ExceptionRecord.ExceptionCode ) { case EXCEPTION_ACCESS_VIOLATION: - strcat(szDebugEventBuffer, "ACCESS_VIOLATION"); + lstrcat(szDebugEventBuffer, "ACCESS_VIOLATION"); break; case EXCEPTION_BREAKPOINT: - strcat(szDebugEventBuffer, "BREAKPOINT"); + lstrcat(szDebugEventBuffer, "BREAKPOINT"); break; case EXCEPTION_DATATYPE_MISALIGNMENT: - strcat(szDebugEventBuffer, "DATATYPE_MISALIGNMENT"); + lstrcat(szDebugEventBuffer, "DATATYPE_MISALIGNMENT"); break; case EXCEPTION_SINGLE_STEP: - strcat(szDebugEventBuffer, "SINGLE_STEP"); + lstrcat(szDebugEventBuffer, "SINGLE_STEP"); break; case EXCEPTION_ARRAY_BOUNDS_EXCEEDED: - strcat(szDebugEventBuffer, "ARRAY_BOUNDS_EXCEEDED"); + lstrcat(szDebugEventBuffer, "ARRAY_BOUNDS_EXCEEDED"); break; case EXCEPTION_FLT_DIVIDE_BY_ZERO: - strcat(szDebugEventBuffer, "FLT_DIVIDE_BY_ZERO"); + lstrcat(szDebugEventBuffer, "FLT_DIVIDE_BY_ZERO"); break; case EXCEPTION_FLT_INVALID_OPERATION: - strcat(szDebugEventBuffer, "FLT_INVALID_OPERATION"); + lstrcat(szDebugEventBuffer, "FLT_INVALID_OPERATION"); break; case EXCEPTION_FLT_OVERFLOW: - strcat(szDebugEventBuffer, "FLT_OVERFLOW"); + lstrcat(szDebugEventBuffer, "FLT_OVERFLOW"); break; case EXCEPTION_FLT_STACK_CHECK: - strcat(szDebugEventBuffer, "FLT_STACK_CHECK"); + lstrcat(szDebugEventBuffer, "FLT_STACK_CHECK"); break; case EXCEPTION_FLT_UNDERFLOW: - strcat(szDebugEventBuffer, "FLT_UNDERFLOW"); + lstrcat(szDebugEventBuffer, "FLT_UNDERFLOW"); break; case EXCEPTION_INT_DIVIDE_BY_ZERO: - strcat(szDebugEventBuffer, "INT_DIVIDE_BY_ZERO"); + lstrcat(szDebugEventBuffer, "INT_DIVIDE_BY_ZERO"); break; case EXCEPTION_INT_OVERFLOW: - strcat(szDebugEventBuffer, "INT_OVERFLOW"); + lstrcat(szDebugEventBuffer, "INT_OVERFLOW"); break; case EXCEPTION_PRIV_INSTRUCTION: - strcat(szDebugEventBuffer, "PRIV_INSTRUCTION"); + lstrcat(szDebugEventBuffer, "PRIV_INSTRUCTION"); break; default: - strcat(szDebugEventBuffer, "Unknown [0x"); - strcat(szDebugEventBuffer, + lstrcat(szDebugEventBuffer, "Unknown [0x"); + lstrcat(szDebugEventBuffer, LongToCharUpper( (LONG) DebugEvent.u.Exception.ExceptionRecord.ExceptionCode, szTempBuf, 16) ); - strcat(szDebugEventBuffer, "]"); + lstrcat(szDebugEventBuffer, "]"); break; } if( fVerbose ) { - strcat(szDebugEventBuffer, " - dwFirstChance:"); - strcat(szDebugEventBuffer, + lstrcat(szDebugEventBuffer, " - dwFirstChance:"); + lstrcat(szDebugEventBuffer, LongToCharUpper( (LONG) DebugEvent.u.Exception.dwFirstChance, szTempBuf, 10) ); } else { if(DebugEvent.u.Exception.dwFirstChance != 0) - strcat(szDebugEventBuffer, " - First Chance"); + lstrcat(szDebugEventBuffer, " - First Chance"); else - strcat(szDebugEventBuffer, " - Second Chance"); + lstrcat(szDebugEventBuffer, " - Second Chance"); } break; @@ -158,11 +196,11 @@ DebugEventThread( LPCTSTR DebugeeFileNam MakeDebugEventString(szDebugEventBuffer, "Create Thread", &DebugEvent); if( fVerbose ) { - strcat(szDebugEventBuffer, " - hThread:0x"); - strcat(szDebugEventBuffer, + lstrcat(szDebugEventBuffer, " - hThread:0x"); + lstrcat(szDebugEventBuffer, LongToCharUpper( (LONG) DebugEvent.u.CreateThread.hThread, szTempBuf, 16) ); - strcat(szDebugEventBuffer, " - lpStartAddress:0x"); - strcat(szDebugEventBuffer, + lstrcat(szDebugEventBuffer, " - lpStartAddress:0x"); + lstrcat(szDebugEventBuffer, LongToCharUpper( (LONG) DebugEvent.u.CreateThread.lpStartAddress, szTempBuf, 16) ); } break; @@ -171,26 +209,26 @@ DebugEventThread( LPCTSTR DebugeeFileNam MakeDebugEventString(szDebugEventBuffer, "Create Process", &DebugEvent); if( fVerbose ) { - strcat(szDebugEventBuffer, " - hFile:0x"); - strcat(szDebugEventBuffer, + lstrcat(szDebugEventBuffer, " - hFile:0x"); + lstrcat(szDebugEventBuffer, LongToCharUpper( (LONG) DebugEvent.u.CreateProcessInfo.hFile, szTempBuf, 16) ); - strcat(szDebugEventBuffer, " - hProcess:0x"); - strcat(szDebugEventBuffer, + lstrcat(szDebugEventBuffer, " - hProcess:0x"); + lstrcat(szDebugEventBuffer, LongToCharUpper( (LONG) DebugEvent.u.CreateProcessInfo.hProcess, szTempBuf, 16) ); - strcat(szDebugEventBuffer, " - hThread:0x"); - strcat(szDebugEventBuffer, + lstrcat(szDebugEventBuffer, " - hThread:0x"); + lstrcat(szDebugEventBuffer, LongToCharUpper( (LONG) DebugEvent.u.CreateProcessInfo.hThread, szTempBuf, 16) ); - strcat(szDebugEventBuffer, " - lpBaseOfImage:0x"); - strcat(szDebugEventBuffer, + lstrcat(szDebugEventBuffer, " - lpBaseOfImage:0x"); + lstrcat(szDebugEventBuffer, LongToCharUpper( (LONG) DebugEvent.u.CreateProcessInfo.lpBaseOfImage, szTempBuf, 16) ); - strcat(szDebugEventBuffer, " - dwDebugInfoFileOffset:"); - strcat(szDebugEventBuffer, + lstrcat(szDebugEventBuffer, " - dwDebugInfoFileOffset:"); + lstrcat(szDebugEventBuffer, LongToCharUpper( (LONG) DebugEvent.u.CreateProcessInfo.dwDebugInfoFileOffset, szTempBuf, 10) ); - strcat(szDebugEventBuffer, " - nDebugInfoSize:"); - strcat(szDebugEventBuffer, + lstrcat(szDebugEventBuffer, " - nDebugInfoSize:"); + lstrcat(szDebugEventBuffer, LongToCharUpper( (LONG) DebugEvent.u.CreateProcessInfo.nDebugInfoSize, szTempBuf, 10) ); - strcat(szDebugEventBuffer, " - lpStartAddress:0x"); - strcat(szDebugEventBuffer, + lstrcat(szDebugEventBuffer, " - lpStartAddress:0x"); + lstrcat(szDebugEventBuffer, LongToCharUpper( (LONG) DebugEvent.u.CreateProcessInfo.lpStartAddress, szTempBuf, 16) ); } break; @@ -200,12 +238,12 @@ DebugEventThread( LPCTSTR DebugeeFileNam &DebugEvent); if( fVerbose ) { - strcat(szDebugEventBuffer, " - dwExitCode:"); + lstrcat(szDebugEventBuffer, " - dwExitCode:"); } else { - strcat(szDebugEventBuffer, " - Returned:"); + lstrcat(szDebugEventBuffer, " - Returned:"); } - strcat(szDebugEventBuffer, + lstrcat(szDebugEventBuffer, LongToCharUpper( (LONG) DebugEvent.u.ExitThread.dwExitCode, szTempBuf, 10) ); break; @@ -213,12 +251,12 @@ DebugEventThread( LPCTSTR DebugeeFileNam MakeDebugEventString(szDebugEventBuffer, "Exit Process", &DebugEvent); if( fVerbose ) { - strcat(szDebugEventBuffer, " - dwExitCode:"); + lstrcat(szDebugEventBuffer, " - dwExitCode:"); } else { - strcat(szDebugEventBuffer, " - Returned:"); + lstrcat(szDebugEventBuffer, " - Returned:"); } - strcat(szDebugEventBuffer, + lstrcat(szDebugEventBuffer, LongToCharUpper( (LONG) DebugEvent.u.ExitProcess.dwExitCode, szTempBuf, 10) ); break; @@ -228,20 +266,20 @@ DebugEventThread( LPCTSTR DebugeeFileNam GetModuleFileNameFromHeader( hProcess, DebugEvent.u.LoadDll.hFile, - szTempBuf, 256 ); - strcat( szDebugEventBuffer, szTempBuf ); + szTempBuf, STRING_BUFSIZ ); + lstrcat( szDebugEventBuffer, szTempBuf ); if( fVerbose ) { - strcat(szDebugEventBuffer, " - hFile:0x"); - strcat(szDebugEventBuffer, + lstrcat(szDebugEventBuffer, " - hFile:0x"); + lstrcat(szDebugEventBuffer, LongToCharUpper( (LONG) DebugEvent.u.LoadDll.hFile, szTempBuf, 16) ); - strcat(szDebugEventBuffer, " - lpBaseOfDll:0x"); - strcat(szDebugEventBuffer, + lstrcat(szDebugEventBuffer, " - lpBaseOfDll:0x"); + lstrcat(szDebugEventBuffer, LongToCharUpper( (LONG) DebugEvent.u.LoadDll.lpBaseOfDll, szTempBuf, 16) ); - strcat(szDebugEventBuffer, " - dwDebugInfoFileOffset:"); - strcat(szDebugEventBuffer, + lstrcat(szDebugEventBuffer, " - dwDebugInfoFileOffset:"); + lstrcat(szDebugEventBuffer, LongToCharUpper( (LONG) DebugEvent.u.LoadDll.dwDebugInfoFileOffset, szTempBuf, 10) ); - strcat(szDebugEventBuffer, " - nDebugInfoSize:"); - strcat(szDebugEventBuffer, + lstrcat(szDebugEventBuffer, " - nDebugInfoSize:"); + lstrcat(szDebugEventBuffer, LongToCharUpper( (LONG) DebugEvent.u.LoadDll.nDebugInfoSize, szTempBuf, 10) ); } break; @@ -250,8 +288,8 @@ DebugEventThread( LPCTSTR DebugeeFileNam MakeDebugEventString(szDebugEventBuffer, "Unload DLL", &DebugEvent); if( fVerbose ) { - strcat(szDebugEventBuffer, " - lpBaseOfDLL:0x"); - strcat(szDebugEventBuffer, + lstrcat(szDebugEventBuffer, " - lpBaseOfDLL:0x"); + lstrcat(szDebugEventBuffer, LongToCharUpper( (LONG) DebugEvent.u.UnloadDll.lpBaseOfDll, szTempBuf, 16) ); } break; @@ -259,7 +297,7 @@ DebugEventThread( LPCTSTR DebugeeFileNam case OUTPUT_DEBUG_STRING_EVENT: MakeDebugEventString(szDebugEventBuffer, "Output Debug String", &DebugEvent); - strcat(szDebugEventBuffer, ":\""); + lstrcat(szDebugEventBuffer, ":\""); //-- According to Win32 Specs Rev3, I need to use ReadVirtualMemory // however this incorrect. ReadProcessMemory is the correct API @@ -267,14 +305,14 @@ DebugEventThread( LPCTSTR DebugeeFileNam DebugEvent.u.DebugString.lpDebugStringData, szTempBuf, DebugEvent.u.DebugString.nDebugStringLength, &NumberOfBytesRead ); - strcat(szDebugEventBuffer, szTempBuf ); - strcat(szDebugEventBuffer, "\"" ); + lstrcat(szDebugEventBuffer, szTempBuf ); + lstrcat(szDebugEventBuffer, "\"" ); if( fVerbose ) { - strcat(szDebugEventBuffer, " - fUnicode:"); - strcat(szDebugEventBuffer, + lstrcat(szDebugEventBuffer, " - fUnicode:"); + lstrcat(szDebugEventBuffer, LongToCharUpper( (LONG) DebugEvent.u.DebugString.fUnicode, szTempBuf, 10) ); - strcat(szDebugEventBuffer, " - nDebugStringLength:"); - strcat(szDebugEventBuffer, + lstrcat(szDebugEventBuffer, " - nDebugStringLength:"); + lstrcat(szDebugEventBuffer, LongToCharUpper( (LONG) DebugEvent.u.DebugString.nDebugStringLength, szTempBuf, 10) ); } break; @@ -282,10 +320,10 @@ DebugEventThread( LPCTSTR DebugeeFileNam default: MakeDebugEventString(szDebugEventBuffer, "Debug Event:", &DebugEvent); - strcat(szDebugEventBuffer, "Unknown [0x"); - strcat(szDebugEventBuffer, + lstrcat(szDebugEventBuffer, "Unknown [0x"); + lstrcat(szDebugEventBuffer, LongToCharUpper( (LONG) DebugEvent.dwDebugEventCode, szTempBuf, 16) ); - strcat(szDebugEventBuffer, "]"); + lstrcat(szDebugEventBuffer, "]"); break; } @@ -356,6 +394,7 @@ DebugEventThread( LPCTSTR DebugeeFileNam DeleteCurrentNode( &ProcessList ); DestroyNode( TempNode ); if( --ActiveProcesses == 0 ) { + // return( TRUE ); ExitThread( TRUE ); } break; @@ -366,7 +405,9 @@ DebugEventThread( LPCTSTR DebugeeFileNam DBG_CONTINUE ); } - return( FALSE ); + ExitThread( FALSE ); + + return( FALSE ); // avoid the "no return value" warning } @@ -374,9 +415,10 @@ DebugEventThread( LPCTSTR DebugeeFileNam // FUNCTION : GetModuleFileNameFromHeader( HANDLE, HANDLE, LPTSTR, DWORD ) // PURPOSE : Retrieves the DLL module name for a given file handle of a // the module. Reads the module name from the EXE header. -// COMMENTS : Retrieves only the module name and not the pathname +// COMMENTS : +// Retrieves only the module name and not the pathname. // ************************************************************************ -DWORD APIENTRY +DWORD WINAPI GetModuleFileNameFromHeader( HANDLE hProcess, HANDLE hFile, LPTSTR lpszPath, DWORD cbPath ) { @@ -397,13 +439,13 @@ GetModuleFileNameFromHeader( HANDLE hPro //-- verify that the handle is not NULL if( !hFile ) { - strcpy( lpszPath, "hFile is NULL!" ); + lstrcpy( lpszPath, "hFile is NULL!" ); return( 0 ); } //-- verify that the handle is for a disk file if( GetFileType(hFile) != FILE_TYPE_DISK ) { - strcpy( lpszPath, "Incorrect hFile Type!" ); + lstrcpy( lpszPath, "Incorrect hFile Type!" ); return( 0 ); } @@ -447,11 +489,11 @@ GetModuleFileNameFromHeader( HANDLE hPro return( NumberOfBytesRead ); } else - sprintf( lpszPath, "NtSignature is 0x%x!", NtSignature ); + wsprintf( lpszPath, "NtSignature is 0x%x!", NtSignature ); } else - sprintf( lpszPath, "DosSignature is 0x%x!", DosSignature ); + wsprintf( lpszPath, "DosSignature is 0x%x!", DosSignature ); return( 0 ); } @@ -461,8 +503,9 @@ GetModuleFileNameFromHeader( HANDLE hPro // ************************************************************************ // FUNCTION : SkipThreadBreakPoint( DWORD ); // PURPOSE : Skip over the break point instruction belonging to -// dwThreadId -// COMMENTS : Only the MIPS R4000 requires this +// dwThreadId. +// COMMENTS : +// Only the MIPS R4000 requires this. // ************************************************************************ BOOL SkipBreakPoint( DWORD dwThreadId ) @@ -485,9 +528,9 @@ SkipBreakPoint( DWORD dwThreadId ) if( !GetThreadContext( (ThreadNode->NodeData).hThread, &Context ) ) { char szTempBuf[64]; - sprintf(szTempBuf, "--> Error: dwThreadId = 0x%8.8lx", dwThreadId ); + wsprintf(szTempBuf, "--> Error: dwThreadId = 0x%8.8lx", dwThreadId ); ListBoxInsert( hWndDebugList, &MaxStrLen, szTempBuf ); - sprintf(szTempBuf, "--> Error: GetThreadContext( %lx ) failed, error 0x%8.8lx", + wsprintf(szTempBuf, "--> Error: GetThreadContext( %lx ) failed, error 0x%8.8lx", (DWORD) (ThreadNode->NodeData).hThread, (DWORD) GetLastError() ); ListBoxInsert( hWndDebugList, &MaxStrLen, szTempBuf);