1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
|
/* This file contains debugging utilities used only under VC2005+
* Maintained by: portej05 (please run patches past him before committing here!)
*/
/* Based on the ATL headers, however, updated to fix one or two bugs in the ATL headers
* and moved to the Sym*64 functions
*/
#if defined(PDB_DEBUGGING)
/* Windows */
#include <windows.h>
#include <dbghelp.h>
/* SCP */
#include "globalincs/pstypes.h"
#include "globalincs/mspdb_callstack.h"
/* Link the library that we need */
#pragma comment( lib, "dbghelp.lib" )
HRESULT SCP_DumpStack( SCP_IDumpHandler* pISH );
BOOL SCP_mspdbcs_ResolveSymbol( HANDLE hProcess, UINT_PTR dwAddress, SCP_mspdbcs_SDumpStackSymbolInfo& siSymbol );
LPVOID __stdcall SCP_mspdbcs_FunctionTableAccess( HANDLE hProcess, DWORD64 dwPCAddress );
DWORD64 __stdcall SCP_mspdbcs_GetModuleBase( HANDLE hProcess, DWORD64 returnAddress );
DWORD WINAPI SCP_mspdbcs_DumpStackThread( LPVOID pv );
void SCP_mspdbcs_Initialise( );
void SCP_mspdbcs_Cleanup( );
static bool SCP_mspdbcs_initialised = false;
static CRITICAL_SECTION SCP_mspdbcs_cs;
BOOL SCP_mspdbcs_ResolveSymbol( HANDLE hProcess, UINT_PTR dwAddress, SCP_mspdbcs_SDumpStackSymbolInfo& siSymbol )
{
BOOL retVal = TRUE;
char szUndec[ SCP_MSPDBCS_MAX_SYMBOL_LENGTH ];
char szWithOffset[ SCP_MSPDBCS_MAX_SYMBOL_LENGTH ];
char* pszSymbol = NULL;
IMAGEHLP_MODULE64 mi;
memset( &siSymbol, 0, sizeof( SCP_mspdbcs_SDumpStackSymbolInfo ) );
mi.SizeOfStruct = sizeof( IMAGEHLP_MODULE64 );
siSymbol.dwAddress = dwAddress;
if ( !SymGetModuleInfo64( hProcess, dwAddress, &mi ) )
{
/* Unneeded */
/*printf("Error: %x\n", HRESULT_FROM_WIN32( GetLastError( ) ));*/
strcpy_s( siSymbol.szModule, "<no module>" );
}
else
{
char* pszLastModule = strchr( mi.ImageName, '\\' );
if ( pszLastModule == NULL )
pszLastModule = mi.ImageName;
else
pszLastModule++; /* move off the backslash */
strncpy_s( siSymbol.szModule, pszLastModule, _TRUNCATE );
}
__try
{
union
{
CHAR rgchSymbol[ sizeof(IMAGEHLP_SYMBOL64) + SCP_MSPDBCS_MAX_SYMBOL_LENGTH ];
IMAGEHLP_SYMBOL64 sym;
} sym;
memset( &sym.sym, 0, sizeof( sym.sym ) );
sym.sym.SizeOfStruct = sizeof( IMAGEHLP_SYMBOL64 );
#ifdef _WIN64
sym.sym.Address = dwAddress;
#else
sym.sym.Address = (DWORD)dwAddress;
#endif
sym.sym.MaxNameLength = SCP_MSPDBCS_MAX_SYMBOL_LENGTH;
#ifdef _WIN64
if ( SymGetSymFromAddr64( hProcess, dwAddress, &(siSymbol.dwOffset), &sym.sym ) )
#else
if ( SymGetSymFromAddr64( hProcess, (DWORD)dwAddress, &(siSymbol.dwOffset), &sym.sym ) )
#endif
{
pszSymbol = sym.sym.Name;
if ( UnDecorateSymbolName( sym.sym.Name, szUndec, sizeof( szUndec )/sizeof( szUndec[0] ),
UNDNAME_NO_MS_KEYWORDS | UNDNAME_NO_ACCESS_SPECIFIERS ) )
{
pszSymbol = szUndec;
}
else if ( SymUnDName64( &sym.sym, szUndec, sizeof( szUndec )/sizeof( szUndec[0] ) ) )
{
pszSymbol = szUndec;
}
if ( siSymbol.dwOffset != 0 )
{
sprintf_s( szWithOffset, SCP_MSPDBCS_MAX_SYMBOL_LENGTH, "%s + %d bytes", pszSymbol, siSymbol.dwOffset );
szWithOffset[ SCP_MSPDBCS_MAX_SYMBOL_LENGTH - 1 ] = '\0'; /* Because sprintf doesn't guarantee NULL terminating */
pszSymbol = szWithOffset;
}
}
else
pszSymbol = "<no symbol>";
}
__except( EXCEPTION_ACCESS_VIOLATION == GetExceptionCode( ) )
{
pszSymbol = "<EX: no symbol>";
siSymbol.dwOffset = dwAddress - mi.BaseOfImage;
}
strncpy_s( siSymbol.szSymbol, pszSymbol, _TRUNCATE );
return retVal;
}
LPVOID __stdcall SCP_mspdbcs_FunctionTableAccess( HANDLE hProcess, DWORD64 dwPCAddress )
{
return SymFunctionTableAccess64( hProcess, dwPCAddress );
}
DWORD64 __stdcall SCP_mspdbcs_GetModuleBase( HANDLE hProcess, DWORD64 returnAddress )
{
IMAGEHLP_MODULE moduleInfo;
moduleInfo.SizeOfStruct = sizeof( IMAGEHLP_MODULE );
/* The ATL headers do it this way */
#ifdef _WIN64
if ( SymGetModuleInfo( hProcess, returnAddress, &moduleInfo ) )
#else
if ( SymGetModuleInfo( hProcess, (ULONG)returnAddress, &moduleInfo ) )
#endif
{
return moduleInfo.BaseOfImage;
}
else
{
MEMORY_BASIC_INFORMATION memoryBasicInfo;
if ( VirtualQueryEx( hProcess, (LPVOID)returnAddress,
&memoryBasicInfo, sizeof( MEMORY_BASIC_INFORMATION ) ) )
{
DWORD cch = 0;
char szFile[ _MAX_PATH ] = {0}; /* Initialise the file path */
cch = GetModuleFileNameA( (HINSTANCE)memoryBasicInfo.AllocationBase, szFile, MAX_PATH );
SymLoadModule( hProcess, NULL, ((cch)?szFile:NULL),
#ifdef _WIN64
NULL, (DWORD_PTR)memoryBasicInfo.AllocationBase, 0 );
#else
NULL, (DWORD)(DWORD_PTR)memoryBasicInfo.AllocationBase, 0 );
#endif
return (DWORD_PTR)memoryBasicInfo.AllocationBase;
}
}
return NULL;
}
DWORD WINAPI SCP_mspdbcs_DumpStackThread( LPVOID pv )
{
CONTEXT context;
memset( &context, 0, sizeof( CONTEXT ) );
context.ContextFlags = CONTEXT_FULL;
SCP_mspdbcs_SDumpStackThreadInfo* pdsti =
reinterpret_cast< SCP_mspdbcs_SDumpStackThreadInfo* >( pv );
/* We're going to walk the stack here */
/* Suspend the running thread */
SuspendThread( pdsti->hThread );
pdsti->pIDS->OnBegin( );
/* Retrieve the context (processor state) of the suspended thread */
GetThreadContext( pdsti->hThread, &context );
/* Set name decoration handling */
DWORD dw = SymGetOptions( );
dw = dw & ~SYMOPT_UNDNAME;
SymSetOptions( dw );
/* Initialise the stackframe */
STACKFRAME64 stackFrame;
memset( &stackFrame, 0, sizeof( STACKFRAME64 ) );
stackFrame.AddrPC.Mode = AddrModeFlat;
stackFrame.AddrFrame.Mode = AddrModeFlat;
stackFrame.AddrStack.Mode = AddrModeFlat;
stackFrame.AddrReturn.Mode = AddrModeFlat;
stackFrame.AddrBStore.Mode = AddrModeFlat;
DWORD dwMachType;
/* Determine the machine type based on the compilation.
* Initialise stackFrame accordingly
* We will only handle the types of _M_IX86 or _M_AMD64
* We're not intending to be compatible with IA64 or any other architectures
* under windows
*/
#if defined(_M_IX86)
dwMachType = IMAGE_FILE_MACHINE_I386;
stackFrame.AddrPC.Offset = context.Eip;
stackFrame.AddrStack.Offset = context.Esp;
stackFrame.AddrFrame.Offset = context.Ebp;
#elif defined(_M_AMD64)
dwMachType = IMAGE_FILE_MACHINE_AMD64;
stackFrame.AddrPC.Offset = context.Rip;
stackFrame.AddrStack = context.Rsp;
#else
# error UNKNOWN ARCHITECTURE
#endif
/* All the discovered addresses will be stored in an array */
SCP_vector< void* > addresses;
/* Walk the stack */
for ( int currFrame = 0; currFrame < SCP_MSPDBCS_MAX_STACK_FRAMES; currFrame++ )
{
if ( !StackWalk64( dwMachType, pdsti->hProcess, pdsti->hThread,
&stackFrame, &context, NULL,
SCP_mspdbcs_FunctionTableAccess, SCP_mspdbcs_GetModuleBase, NULL ) )
{
break; /* No more stack frames to walk */
}
/* Found a useful address? */
if ( stackFrame.AddrPC.Offset != 0 )
{
if ( pdsti->pIDS->ResolveSymbols( ) )
addresses.push_back( (void*)(DWORD_PTR)stackFrame.AddrPC.Offset );
else
pdsti->pIDS->OnEntry( (void*)(DWORD_PTR)stackFrame.AddrPC.Offset, NULL, NULL );
}
}
if ( pdsti->pIDS->ResolveSymbols( ) )
{
/* Dump the stack information that we have */
for ( size_t i = 0; i < addresses.size( ); i++ )
{
SCP_mspdbcs_SDumpStackSymbolInfo info;
const char* szModule = NULL;
const char* szSymbol = NULL;
if ( SCP_mspdbcs_ResolveSymbol( pdsti->hProcess, (UINT_PTR)addresses[ i ], info ) )
{
szModule = info.szModule;
szSymbol = info.szSymbol;
}
pdsti->pIDS->OnEntry( addresses[ i ], szModule, szSymbol );
}
}
pdsti->pIDS->OnEnd( );
ResumeThread( pdsti->hThread );
return 0;
}
/* Entry point */
HRESULT SCP_DumpStack( SCP_IDumpHandler* pIDH )
{
if ( !pIDH )
return E_POINTER;
/* Retrieve pseudo handles to the current thread and process */
HANDLE hPseudoThread = GetCurrentThread( );
HANDLE hPseudoProcess = GetCurrentProcess( );
/* Retrieve the real handle of this thread */
HANDLE hThread = NULL;
if ( !DuplicateHandle( hPseudoProcess, hPseudoThread, /* Source process and thread */
hPseudoProcess, &hThread, /* Target process and thread */
0, FALSE, DUPLICATE_SAME_ACCESS ) ) /* Non-Inheritable, same access as current process/thread */
return HRESULT_FROM_WIN32( GetLastError( ) ); /* Bugger */
DWORD dwID;
SCP_mspdbcs_SDumpStackThreadInfo info;
info.hProcess = hPseudoProcess;
info.hThread = hThread;
info.pIDS = pIDH;
/* Calls to dbghelp.dll must be synchronised :( */
EnterCriticalSection( &SCP_mspdbcs_cs );
/* This will fail if SymInitialize hasn't been called, so
* this protects against uninitialised state */
if ( !SCP_mspdbcs_initialised )
{
LeaveCriticalSection( &SCP_mspdbcs_cs );
mprintf( ("Symbols not initialised\n") );
return E_UNEXPECTED;
}
HANDLE workerThread = CreateThread( NULL, 0, SCP_mspdbcs_DumpStackThread,
&info, 0, &dwID );
LeaveCriticalSection( &SCP_mspdbcs_cs );
if ( workerThread == NULL )
return HRESULT_FROM_WIN32( GetLastError( ) ); /* Bugger */
while ( WaitForSingleObject( workerThread, 0 ) != WAIT_OBJECT_0 );
CloseHandle( workerThread );
return S_OK;
}
#endif // PDB_DEBUGGING
void SCP_mspdbcs_Initialise( )
{
#ifdef PDB_DEBUGGING
HANDLE hPseudoProcess = GetCurrentProcess( );
if ( !SymInitialize( hPseudoProcess, NULL, TRUE ) )
{
mprintf( ("Could not initialise symbols - callstacks will fail: %x\n", HRESULT_FROM_WIN32( GetLastError( ) ) ) );
}
else
{
InitializeCriticalSection( &SCP_mspdbcs_cs );
SCP_mspdbcs_initialised = true;
}
#endif
}
void SCP_mspdbcs_Cleanup( )
{
#ifdef PDB_DEBUGGING
/* We enter the critical section to synchronise the check of
* SCP_mspdbcs_initialised. Failure to do that could cause
* SymCleanup to be called before SCP_dump_stack is finished
*/
EnterCriticalSection( &SCP_mspdbcs_cs );
SCP_mspdbcs_initialised = false; /* stop problems at end of execution */
LeaveCriticalSection( &SCP_mspdbcs_cs );
DeleteCriticalSection( &SCP_mspdbcs_cs );
HANDLE hPseudoProcess = GetCurrentProcess( );
SymCleanup( hPseudoProcess );
#endif
}
|