86 #pragma comment(lib, "version.lib") // for "VerQueryValue"
87 #pragma warning(disable:4826)
89 #include "StackWalker.hh"
127 CHAR LoadedImageName[256];
150 DWORD ThCallbackStack;
151 DWORD ThCallbackBStore;
154 DWORD64 KiCallUserMode;
155 DWORD64 KeUserCallbackDispatcher;
156 DWORD64 SystemRangeStart;
165 PVOID FuncTableEntry;
174 (__stdcall *PREAD_PROCESS_MEMORY_ROUTINE64)(
176 DWORD64 qwBaseAddress,
179 LPDWORD lpNumberOfBytesRead
183 (__stdcall *PFUNCTION_TABLE_ACCESS_ROUTINE64)(
189 (__stdcall *PGET_MODULE_BASE_ROUTINE64)(
195 (__stdcall *PTRANSLATE_ADDRESS_ROUTINE64)(
200 #define SYMOPT_CASE_INSENSITIVE 0x00000001
201 #define SYMOPT_UNDNAME 0x00000002
202 #define SYMOPT_DEFERRED_LOADS 0x00000004
203 #define SYMOPT_NO_CPP 0x00000008
204 #define SYMOPT_LOAD_LINES 0x00000010
205 #define SYMOPT_OMAP_FIND_NEAREST 0x00000020
206 #define SYMOPT_LOAD_ANYTHING 0x00000040
207 #define SYMOPT_IGNORE_CVREC 0x00000080
208 #define SYMOPT_NO_UNQUALIFIED_LOADS 0x00000100
209 #define SYMOPT_FAIL_CRITICAL_ERRORS 0x00000200
210 #define SYMOPT_EXACT_SYMBOLS 0x00000400
211 #define SYMOPT_ALLOW_ABSOLUTE_SYMBOLS 0x00000800
212 #define SYMOPT_IGNORE_NT_SYMPATH 0x00001000
213 #define SYMOPT_INCLUDE_32BIT_MODULES 0x00002000
214 #define SYMOPT_PUBLICS_ONLY 0x00004000
215 #define SYMOPT_NO_PUBLICS 0x00008000
216 #define SYMOPT_AUTO_PUBLICS 0x00010000
217 #define SYMOPT_NO_IMAGE_SEARCH 0x00020000
218 #define SYMOPT_SECURE 0x00040000
219 #define SYMOPT_DEBUG 0x80000000
220 #define UNDNAME_COMPLETE (0x0000) // Enable full undecoration
221 #define UNDNAME_NAME_ONLY (0x1000) // Crack only the name for primary declaration;
222 #endif // _MSC_VER < 1300
226 #ifndef INVALID_FILE_ATTRIBUTES
227 #define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
233 #define strcpy_s(dst, len, src) strcpy(dst, src)
234 #define strncpy_s(dst, len, src, maxLen) strncpy(dst, len, src)
235 #define strcat_s(dst, len, src) strcat(dst, src)
236 #define _snprintf_s _snprintf
237 #define _tcscat_s _tcscat
240 static void MyStrCpy(
char* szDest,
size_t nMaxDestSize,
const char* szSrc)
242 if (strlen(szSrc) < nMaxDestSize)
244 strcpy_s(szDest, nMaxDestSize, szSrc);
248 strncpy_s(szDest, nMaxDestSize, szSrc, nMaxDestSize);
249 szDest[nMaxDestSize-1] = 0;
254 #define USED_CONTEXT_FLAGS CONTEXT_FULL
265 m_hProcess = hProcess;
284 if (m_hDbhHelp != NULL)
285 FreeLibrary(m_hDbhHelp);
288 if(m_szSymPath != NULL)
292 BOOL Init(LPCSTR szSymPath)
294 if (m_parent == NULL)
300 if (GetModuleFileName(NULL, szTemp, 4096) > 0)
302 _tcscat_s(szTemp, _T(
".local"));
303 if (GetFileAttributes(szTemp) == INVALID_FILE_ATTRIBUTES)
308 if ( (m_hDbhHelp == NULL) && (GetEnvironmentVariable(_T(
"ProgramFiles"), szTemp, 4096) > 0) )
310 _tcscat_s(szTemp, _T(
"\\Debugging Tools for Windows (x86)\\dbghelp.dll"));
312 if (GetFileAttributes(szTemp) != INVALID_FILE_ATTRIBUTES)
314 m_hDbhHelp = LoadLibrary(szTemp);
318 if ( (m_hDbhHelp == NULL) && (GetEnvironmentVariable(_T(
"ProgramFiles"), szTemp, 4096) > 0) )
320 _tcscat_s(szTemp, _T(
"\\Debugging Tools for Windows (x64)\\dbghelp.dll"));
322 if (GetFileAttributes(szTemp) != INVALID_FILE_ATTRIBUTES)
324 m_hDbhHelp = LoadLibrary(szTemp);
328 if ( (m_hDbhHelp == NULL) && (GetEnvironmentVariable(_T(
"ProgramFiles"), szTemp, 4096) > 0) )
330 _tcscat_s(szTemp, _T(
"\\Debugging Tools for Windows (ia64)\\dbghelp.dll"));
332 if (GetFileAttributes(szTemp) != INVALID_FILE_ATTRIBUTES)
334 m_hDbhHelp = LoadLibrary(szTemp);
339 if ( (m_hDbhHelp == NULL) && (GetEnvironmentVariable(_T(
"ProgramFiles"), szTemp, 4096) > 0) )
341 _tcscat_s(szTemp, _T(
"\\Debugging Tools for Windows\\dbghelp.dll"));
343 if (GetFileAttributes(szTemp) != INVALID_FILE_ATTRIBUTES)
345 m_hDbhHelp = LoadLibrary(szTemp);
348 #if defined _M_X64 || defined _M_IA64
350 if ( (m_hDbhHelp == NULL) && (GetEnvironmentVariable(_T(
"ProgramFiles"), szTemp, 4096) > 0) )
352 _tcscat_s(szTemp, _T(
"\\Debugging Tools for Windows 64-Bit\\dbghelp.dll"));
353 if (GetFileAttributes(szTemp) != INVALID_FILE_ATTRIBUTES)
355 m_hDbhHelp = LoadLibrary(szTemp);
361 if (m_hDbhHelp == NULL)
362 m_hDbhHelp = LoadLibrary( _T(
"dbghelp.dll") );
363 if (m_hDbhHelp == NULL)
365 pSI = (tSI) GetProcAddress(m_hDbhHelp,
"SymInitialize" );
366 pSC = (tSC) GetProcAddress(m_hDbhHelp,
"SymCleanup" );
368 pSW = (tSW) GetProcAddress(m_hDbhHelp,
"StackWalk64" );
369 pSGO = (tSGO) GetProcAddress(m_hDbhHelp,
"SymGetOptions" );
370 pSSO = (tSSO) GetProcAddress(m_hDbhHelp,
"SymSetOptions" );
372 pSFTA = (tSFTA) GetProcAddress(m_hDbhHelp,
"SymFunctionTableAccess64" );
373 pSGLFA = (tSGLFA) GetProcAddress(m_hDbhHelp,
"SymGetLineFromAddr64" );
374 pSGMB = (tSGMB) GetProcAddress(m_hDbhHelp,
"SymGetModuleBase64" );
375 pSGMI = (tSGMI) GetProcAddress(m_hDbhHelp,
"SymGetModuleInfo64" );
376 pSGSFA = (tSGSFA) GetProcAddress(m_hDbhHelp,
"SymGetSymFromAddr64" );
377 pUDSN = (tUDSN) GetProcAddress(m_hDbhHelp,
"UnDecorateSymbolName" );
378 pSLM = (tSLM) GetProcAddress(m_hDbhHelp,
"SymLoadModule64" );
379 pSGSP =(tSGSP) GetProcAddress(m_hDbhHelp,
"SymGetSearchPath" );
381 if ( pSC == NULL || pSFTA == NULL || pSGMB == NULL || pSGMI == NULL ||
382 pSGO == NULL || pSGSFA == NULL || pSI == NULL || pSSO == NULL ||
383 pSW == NULL || pUDSN == NULL || pSLM == NULL )
385 FreeLibrary(m_hDbhHelp);
392 if (szSymPath != NULL)
393 m_szSymPath = _strdup(szSymPath);
394 if (this->pSI(m_hProcess, m_szSymPath, FALSE) == FALSE)
395 this->m_parent->OnDbgHelpErr(
"SymInitialize", GetLastError(), 0);
397 DWORD symOptions = this->pSGO();
398 symOptions |= SYMOPT_LOAD_LINES;
399 symOptions |= SYMOPT_FAIL_CRITICAL_ERRORS;
402 symOptions = this->pSSO(symOptions);
404 char buf[StackWalker::STACKWALK_MAX_NAMELEN] = {0};
405 if (this->pSGSP != NULL)
407 if (this->pSGSP(m_hProcess, buf, StackWalker::STACKWALK_MAX_NAMELEN) == FALSE)
408 this->m_parent->OnDbgHelpErr(
"SymGetSearchPath", GetLastError(), 0);
410 char szUserName[1024] = {0};
412 GetUserNameA(szUserName, &dwSize);
413 this->m_parent->OnSymInit(buf, symOptions, szUserName);
435 CHAR LoadedImageName[256];
437 CHAR LoadedPdbName[256];
439 CHAR CVData[MAX_PATH * 3];
463 CHAR LoadedImageName[256];
469 typedef BOOL (__stdcall *tSC)( IN HANDLE hProcess );
473 typedef PVOID (__stdcall *tSFTA)( HANDLE hProcess, DWORD64 AddrBase );
477 typedef BOOL (__stdcall *tSGLFA)( IN HANDLE hProcess, IN DWORD64 dwAddr,
478 OUT PDWORD pdwDisplacement, OUT PIMAGEHLP_LINE64 Line );
482 typedef DWORD64 (__stdcall *tSGMB)( IN HANDLE hProcess, IN DWORD64 dwAddr );
486 typedef BOOL (__stdcall *tSGMI)( IN HANDLE hProcess, IN DWORD64 dwAddr, OUT
IMAGEHLP_MODULE64_V3 *ModuleInfo );
490 typedef DWORD (__stdcall *tSGO)( VOID );
494 typedef BOOL (__stdcall *tSGSFA)( IN HANDLE hProcess, IN DWORD64 dwAddr,
495 OUT PDWORD64 pdwDisplacement, OUT PIMAGEHLP_SYMBOL64 Symbol );
499 typedef BOOL (__stdcall *tSI)( IN HANDLE hProcess, IN PSTR UserSearchPath, IN BOOL fInvadeProcess );
503 typedef DWORD64 (__stdcall *tSLM)( IN HANDLE hProcess, IN HANDLE hFile,
504 IN PSTR ImageName, IN PSTR ModuleName, IN DWORD64 BaseOfDll, IN DWORD SizeOfDll );
508 typedef DWORD (__stdcall *tSSO)( IN DWORD SymOptions );
512 typedef BOOL (__stdcall *tSW)(
516 LPSTACKFRAME64 StackFrame,
518 PREAD_PROCESS_MEMORY_ROUTINE64 ReadMemoryRoutine,
519 PFUNCTION_TABLE_ACCESS_ROUTINE64 FunctionTableAccessRoutine,
520 PGET_MODULE_BASE_ROUTINE64 GetModuleBaseRoutine,
521 PTRANSLATE_ADDRESS_ROUTINE64 TranslateAddress );
525 typedef DWORD (__stdcall WINAPI *tUDSN)( PCSTR DecoratedName, PSTR UnDecoratedName,
526 DWORD UndecoratedLength, DWORD Flags );
529 typedef BOOL (__stdcall WINAPI *tSGSP)(HANDLE hProcess, PSTR SearchPath, DWORD SearchPathLength);
535 #define MAX_MODULE_NAME32 255
536 #define TH32CS_SNAPMODULE 0x00000008
537 #pragma pack( push, 8 )
548 char szModule[MAX_MODULE_NAME32 + 1];
549 char szExePath[MAX_PATH];
555 BOOL GetModuleListTH32(HANDLE hProcess, DWORD pid)
558 typedef HANDLE (__stdcall *tCT32S)(DWORD dwFlags, DWORD th32ProcessID);
560 typedef BOOL (__stdcall *tM32F)(HANDLE hSnapshot, LPMODULEENTRY32 lpme);
562 typedef BOOL (__stdcall *tM32N)(HANDLE hSnapshot, LPMODULEENTRY32 lpme);
565 const TCHAR *dllname[] = { _T(
"kernel32.dll"), _T(
"tlhelp32.dll") };
566 HINSTANCE hToolhelp = NULL;
567 tCT32S pCT32S = NULL;
573 me.dwSize =
sizeof(me);
577 for (i = 0; i<(
sizeof(dllname) /
sizeof(dllname[0])); i++ )
579 hToolhelp = LoadLibrary( dllname[i] );
580 if (hToolhelp == NULL)
582 pCT32S = (tCT32S) GetProcAddress(hToolhelp,
"CreateToolhelp32Snapshot");
583 pM32F = (tM32F) GetProcAddress(hToolhelp,
"Module32First");
584 pM32N = (tM32N) GetProcAddress(hToolhelp,
"Module32Next");
585 if ( (pCT32S != NULL) && (pM32F != NULL) && (pM32N != NULL) )
587 FreeLibrary(hToolhelp);
591 if (hToolhelp == NULL)
594 hSnap = pCT32S( TH32CS_SNAPMODULE, pid );
595 if (hSnap == (HANDLE) -1)
597 FreeLibrary(hToolhelp);
601 keepGoing = !!pM32F( hSnap, &me );
605 this->LoadModule(hProcess, me.szExePath, me.szModule, (DWORD64) me.modBaseAddr, me.modBaseSize);
607 keepGoing = !!pM32N( hSnap, &me );
610 FreeLibrary(hToolhelp);
623 BOOL GetModuleListPSAPI(HANDLE hProcess)
626 typedef BOOL (__stdcall *tEPM)(HANDLE hProcess, HMODULE *lphModule, DWORD cb, LPDWORD lpcbNeeded );
628 typedef DWORD (__stdcall *tGMFNE)(HANDLE hProcess, HMODULE hModule, LPSTR lpFilename, DWORD nSize );
630 typedef DWORD (__stdcall *tGMBN)(HANDLE hProcess, HMODULE hModule, LPSTR lpFilename, DWORD nSize );
632 typedef BOOL (__stdcall *tGMI)(HANDLE hProcess, HMODULE hModule, LPMODULEINFO pmi, DWORD nSize );
647 const SIZE_T TTBUFLEN = 8096;
650 hPsapi = LoadLibrary( _T(
"psapi.dll") );
654 pEPM = (tEPM) GetProcAddress( hPsapi,
"EnumProcessModules" );
655 pGMFNE = (tGMFNE) GetProcAddress( hPsapi,
"GetModuleFileNameExA" );
656 pGMBN = (tGMFNE) GetProcAddress( hPsapi,
"GetModuleBaseNameA" );
657 pGMI = (tGMI) GetProcAddress( hPsapi,
"GetModuleInformation" );
658 if ( (pEPM == NULL) || (pGMFNE == NULL) || (pGMBN == NULL) || (pGMI == NULL) )
665 hMods = (HMODULE*) malloc(
sizeof(HMODULE) * (TTBUFLEN /
sizeof HMODULE));
666 tt = (
char*) malloc(
sizeof(
char) * TTBUFLEN);
667 tt2 = (
char*) malloc(
sizeof(
char) * TTBUFLEN);
668 if ( (hMods == NULL) || (tt == NULL) || (tt2 == NULL) )
671 if ( ! pEPM( hProcess, hMods, TTBUFLEN, &cbNeeded ) )
677 if ( cbNeeded > TTBUFLEN )
683 for ( i = 0; i < cbNeeded /
sizeof hMods[0]; i++ )
686 pGMI(hProcess, hMods[i], &mi,
sizeof mi );
689 pGMFNE(hProcess, hMods[i], tt, TTBUFLEN );
692 pGMBN(hProcess, hMods[i], tt2, TTBUFLEN );
694 DWORD dwRes = this->LoadModule(hProcess, tt, tt2, (DWORD64) mi.lpBaseOfDll, mi.SizeOfImage);
695 if (dwRes != ERROR_SUCCESS)
696 this->m_parent->OnDbgHelpErr(
"LoadModule", dwRes, 0);
701 if (hPsapi != NULL) FreeLibrary(hPsapi);
702 if (tt2 != NULL) free(tt2);
703 if (tt != NULL) free(tt);
704 if (hMods != NULL) free(hMods);
709 DWORD LoadModule(HANDLE hProcess, LPCSTR img, LPCSTR mod, DWORD64 baseAddr, DWORD size)
711 CHAR *szImg = _strdup(img);
712 CHAR *szMod = _strdup(mod);
713 DWORD result = ERROR_SUCCESS;
714 if ( (szImg == NULL) || (szMod == NULL) )
715 result = ERROR_NOT_ENOUGH_MEMORY;
718 if (pSLM(hProcess, 0, szImg, szMod, baseAddr, size) == 0)
719 result = GetLastError();
721 ULONGLONG fileVersion = 0;
722 if ( (m_parent != NULL) && (szImg != NULL) )
725 if ( (this->m_parent->m_options & StackWalker::RetrieveFileVersion) != 0)
727 VS_FIXEDFILEINFO *fInfo = NULL;
729 DWORD dwSize = GetFileVersionInfoSizeA(szImg, &dwHandle);
732 LPVOID vData = malloc(dwSize);
735 if (GetFileVersionInfoA(szImg, dwHandle, dwSize, vData) != 0)
738 TCHAR szSubBlock[] = _T(
"\\");
739 if (VerQueryValue(vData, szSubBlock, (LPVOID*) &fInfo, &len) == 0)
743 fileVersion = ((ULONGLONG)fInfo->dwFileVersionLS) + ((ULONGLONG)fInfo->dwFileVersionMS << 32);
752 IMAGEHLP_MODULE64_V3 Module;
753 const char *szSymType =
"-unknown-";
754 if (this->GetModuleInfo(hProcess, baseAddr, &Module) != FALSE)
756 switch(Module.SymType)
759 szSymType =
"-nosymbols-";
771 szSymType =
"-exported-";
774 szSymType =
"-deferred-";
783 szSymType =
"Virtual";
787 LPCSTR pdbName = Module.LoadedImageName;
788 if (Module.LoadedPdbName[0] != 0)
789 pdbName = Module.LoadedPdbName;
790 this->m_parent->OnLoadModule(img, mod, baseAddr, size, result, szSymType, pdbName, fileVersion);
792 if (szImg != NULL) free(szImg);
793 if (szMod != NULL) free(szMod);
797 BOOL LoadModules(HANDLE hProcess, DWORD dwProcessId)
800 if (GetModuleListTH32(hProcess, dwProcessId))
803 return GetModuleListPSAPI(hProcess);
807 BOOL GetModuleInfo(HANDLE hProcess, DWORD64 baseAddr, IMAGEHLP_MODULE64_V3 *pModuleInfo)
809 memset(pModuleInfo, 0,
sizeof(IMAGEHLP_MODULE64_V3));
810 if(this->pSGMI == NULL)
812 SetLastError(ERROR_DLL_INIT_FAILED);
816 pModuleInfo->SizeOfStruct =
sizeof(IMAGEHLP_MODULE64_V3);
817 void *pData = malloc(4096);
820 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
823 memcpy(pData, pModuleInfo,
sizeof(IMAGEHLP_MODULE64_V3));
824 static bool s_useV3Version =
true;
827 if (this->pSGMI(hProcess, baseAddr, reinterpret_cast<IMAGEHLP_MODULE64_V3*>(pData) ) != FALSE)
830 memcpy(pModuleInfo, pData,
sizeof(IMAGEHLP_MODULE64_V3));
831 pModuleInfo->SizeOfStruct =
sizeof(IMAGEHLP_MODULE64_V3);
835 s_useV3Version =
false;
839 pModuleInfo->SizeOfStruct =
sizeof(IMAGEHLP_MODULE64_V2);
840 memcpy(pData, pModuleInfo,
sizeof(IMAGEHLP_MODULE64_V2));
841 if (this->pSGMI(hProcess, baseAddr, reinterpret_cast<IMAGEHLP_MODULE64_V3*>(pData) ) != FALSE)
844 memcpy(pModuleInfo, pData,
sizeof(IMAGEHLP_MODULE64_V2));
845 pModuleInfo->SizeOfStruct =
sizeof(IMAGEHLP_MODULE64_V2);
850 SetLastError(ERROR_DLL_INIT_FAILED);
856 StackWalker::StackWalker(DWORD dwProcessId, HANDLE hProcess)
858 this->m_options = OptionsAll;
859 this->m_modulesLoaded = FALSE;
860 this->m_hProcess = hProcess;
862 this->m_dwProcessId = dwProcessId;
863 this->m_szSymPath = NULL;
864 this->m_MaxRecursionCount = 1000;
866 StackWalker::StackWalker(
int options, LPCSTR szSymPath, DWORD dwProcessId, HANDLE hProcess)
868 this->m_options = options;
869 this->m_modulesLoaded = FALSE;
870 this->m_hProcess = hProcess;
872 this->m_dwProcessId = dwProcessId;
873 if (szSymPath != NULL)
875 this->m_szSymPath = _strdup(szSymPath);
876 this->m_options |= SymBuildPath;
879 this->m_szSymPath = NULL;
880 this->m_MaxRecursionCount = 1000;
883 StackWalker::~StackWalker()
885 if (m_szSymPath != NULL)
888 if (this->m_sw != NULL)
893 BOOL StackWalker::LoadModules()
895 if (this->m_sw == NULL)
897 SetLastError(ERROR_DLL_INIT_FAILED);
900 if (m_modulesLoaded != FALSE)
904 char *szSymPath = NULL;
905 if ( (this->m_options & SymBuildPath) != 0)
907 const size_t nSymPathLen = 4096;
908 szSymPath = (
char*) malloc(nSymPathLen);
909 if (szSymPath == NULL)
911 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
916 if (this->m_szSymPath != NULL)
918 strcat_s(szSymPath, nSymPathLen, this->m_szSymPath);
919 strcat_s(szSymPath, nSymPathLen,
";");
922 strcat_s(szSymPath, nSymPathLen,
".;");
924 const size_t nTempLen = 1024;
925 char szTemp[nTempLen];
927 if (GetCurrentDirectoryA(nTempLen, szTemp) > 0)
929 szTemp[nTempLen-1] = 0;
930 strcat_s(szSymPath, nSymPathLen, szTemp);
931 strcat_s(szSymPath, nSymPathLen,
";");
935 if (GetModuleFileNameA(NULL, szTemp, nTempLen) > 0)
937 szTemp[nTempLen-1] = 0;
938 for (
char *p = (szTemp+strlen(szTemp)-1); p >= szTemp; --p)
941 if ( (*p ==
'\\') || (*p ==
'/') || (*p ==
':') )
947 if (strlen(szTemp) > 0)
949 strcat_s(szSymPath, nSymPathLen, szTemp);
950 strcat_s(szSymPath, nSymPathLen,
";");
953 if (GetEnvironmentVariableA(
"_NT_SYMBOL_PATH", szTemp, nTempLen) > 0)
955 szTemp[nTempLen-1] = 0;
956 strcat_s(szSymPath, nSymPathLen, szTemp);
957 strcat_s(szSymPath, nSymPathLen,
";");
959 if (GetEnvironmentVariableA(
"_NT_ALTERNATE_SYMBOL_PATH", szTemp, nTempLen) > 0)
961 szTemp[nTempLen-1] = 0;
962 strcat_s(szSymPath, nSymPathLen, szTemp);
963 strcat_s(szSymPath, nSymPathLen,
";");
965 if (GetEnvironmentVariableA(
"SYSTEMROOT", szTemp, nTempLen) > 0)
967 szTemp[nTempLen-1] = 0;
968 strcat_s(szSymPath, nSymPathLen, szTemp);
969 strcat_s(szSymPath, nSymPathLen,
";");
971 strcat_s(szTemp, nTempLen,
"\\system32");
972 strcat_s(szSymPath, nSymPathLen, szTemp);
973 strcat_s(szSymPath, nSymPathLen,
";");
976 if ( (this->m_options & SymUseSymSrv) != 0)
978 if (GetEnvironmentVariableA(
"SYSTEMDRIVE", szTemp, nTempLen) > 0)
980 szTemp[nTempLen-1] = 0;
981 strcat_s(szSymPath, nSymPathLen,
"SRV*");
982 strcat_s(szSymPath, nSymPathLen, szTemp);
983 strcat_s(szSymPath, nSymPathLen,
"\\websymbols");
984 strcat_s(szSymPath, nSymPathLen,
"*http://msdl.microsoft.com/download/symbols;");
987 strcat_s(szSymPath, nSymPathLen,
"SRV*c:\\websymbols*http://msdl.microsoft.com/download/symbols;");
992 BOOL bRet = this->m_sw->Init(szSymPath);
993 if (szSymPath != NULL) free(szSymPath); szSymPath = NULL;
996 this->OnDbgHelpErr(
"Error while initializing dbghelp.dll", 0, 0);
997 SetLastError(ERROR_DLL_INIT_FAILED);
1001 bRet = this->m_sw->LoadModules(this->m_hProcess, this->m_dwProcessId);
1003 m_modulesLoaded = TRUE;
1012 static StackWalker::PReadProcessMemoryRoutine s_readMemoryFunction = NULL;
1013 static LPVOID s_readMemoryFunction_UserData = NULL;
1015 BOOL StackWalker::ShowCallstack(HANDLE hThread,
const CONTEXT *context, PReadProcessMemoryRoutine readMemoryFunction, LPVOID pUserData)
1018 CallstackEntry csEntry;
1023 bool bLastEntryCalled =
true;
1024 int curRecursionCount = 0;
1026 if (m_modulesLoaded == FALSE)
1027 this->LoadModules();
1029 if (this->m_sw->m_hDbhHelp == NULL)
1031 SetLastError(ERROR_DLL_INIT_FAILED);
1035 s_readMemoryFunction = readMemoryFunction;
1036 s_readMemoryFunction_UserData = pUserData;
1038 if (context == NULL)
1041 if (hThread == GetCurrentThread())
1043 GET_CURRENT_CONTEXT(c, USED_CONTEXT_FLAGS);
1047 SuspendThread(hThread);
1048 memset(&c, 0,
sizeof(CONTEXT));
1049 c.ContextFlags = USED_CONTEXT_FLAGS;
1050 if (GetThreadContext(hThread, &c) == FALSE)
1052 ResumeThread(hThread);
1062 memset(&s, 0,
sizeof(s));
1066 imageType = IMAGE_FILE_MACHINE_I386;
1067 s.AddrPC.Offset = c.Eip;
1068 s.AddrPC.Mode = AddrModeFlat;
1069 s.AddrFrame.Offset = c.Ebp;
1070 s.AddrFrame.Mode = AddrModeFlat;
1071 s.AddrStack.Offset = c.Esp;
1072 s.AddrStack.Mode = AddrModeFlat;
1074 imageType = IMAGE_FILE_MACHINE_AMD64;
1075 s.AddrPC.Offset = c.Rip;
1076 s.AddrPC.Mode = AddrModeFlat;
1077 s.AddrFrame.Offset = c.Rsp;
1078 s.AddrFrame.Mode = AddrModeFlat;
1079 s.AddrStack.Offset = c.Rsp;
1080 s.AddrStack.Mode = AddrModeFlat;
1082 imageType = IMAGE_FILE_MACHINE_IA64;
1083 s.AddrPC.Offset = c.StIIP;
1084 s.AddrPC.Mode = AddrModeFlat;
1085 s.AddrFrame.Offset = c.IntSp;
1086 s.AddrFrame.Mode = AddrModeFlat;
1087 s.AddrBStore.Offset = c.RsBSP;
1088 s.AddrBStore.Mode = AddrModeFlat;
1089 s.AddrStack.Offset = c.IntSp;
1090 s.AddrStack.Mode = AddrModeFlat;
1092 #error "Platform not supported!"
1096 if (!pSym)
goto cleanup;
1099 pSym->MaxNameLength = STACKWALK_MAX_NAMELEN;
1101 memset(&Line, 0,
sizeof(Line));
1102 Line.SizeOfStruct =
sizeof(Line);
1104 memset(&Module, 0,
sizeof(Module));
1105 Module.SizeOfStruct =
sizeof(Module);
1107 for (frameNum = 0; ; ++frameNum )
1114 if ( ! this->m_sw->pSW(imageType, this->m_hProcess, hThread, &s, &c, myReadProcMem, this->m_sw->pSFTA, this->m_sw->pSGMB, NULL) )
1117 this->OnDbgHelpErr(
"StackWalk64", 0, s.AddrPC.Offset);
1121 csEntry.offset = s.AddrPC.Offset;
1122 csEntry.name[0] = 0;
1123 csEntry.undName[0] = 0;
1124 csEntry.undFullName[0] = 0;
1125 csEntry.offsetFromSmybol = 0;
1126 csEntry.offsetFromLine = 0;
1127 csEntry.lineFileName[0] = 0;
1128 csEntry.lineNumber = 0;
1129 csEntry.loadedImageName[0] = 0;
1130 csEntry.moduleName[0] = 0;
1131 if (s.AddrPC.Offset == s.AddrReturn.Offset)
1133 if ( (this->m_MaxRecursionCount > 0) && (curRecursionCount > m_MaxRecursionCount) )
1135 this->OnDbgHelpErr(
"StackWalk64-Endless-Callstack!", 0, s.AddrPC.Offset);
1138 curRecursionCount++;
1141 curRecursionCount = 0;
1142 if (s.AddrPC.Offset != 0)
1146 if (this->m_sw->pSGSFA(this->m_hProcess, s.AddrPC.Offset, &(csEntry.offsetFromSmybol), pSym) != FALSE)
1148 MyStrCpy(csEntry.name, STACKWALK_MAX_NAMELEN, pSym->Name);
1150 this->m_sw->pUDSN( pSym->Name, csEntry.undName, STACKWALK_MAX_NAMELEN, UNDNAME_NAME_ONLY );
1151 this->m_sw->pUDSN( pSym->Name, csEntry.undFullName, STACKWALK_MAX_NAMELEN, UNDNAME_COMPLETE );
1155 this->OnDbgHelpErr(
"SymGetSymFromAddr64", GetLastError(), s.AddrPC.Offset);
1159 if (this->m_sw->pSGLFA != NULL )
1161 if (this->m_sw->pSGLFA(this->m_hProcess, s.AddrPC.Offset, &(csEntry.offsetFromLine), &Line) != FALSE)
1163 csEntry.lineNumber = Line.LineNumber;
1164 MyStrCpy(csEntry.lineFileName, STACKWALK_MAX_NAMELEN, Line.FileName);
1168 this->OnDbgHelpErr(
"SymGetLineFromAddr64", GetLastError(), s.AddrPC.Offset);
1173 if (this->m_sw->GetModuleInfo(this->m_hProcess, s.AddrPC.Offset, &Module ) != FALSE)
1175 switch ( Module.SymType )
1178 csEntry.symTypeString =
"-nosymbols-";
1181 csEntry.symTypeString =
"COFF";
1184 csEntry.symTypeString =
"CV";
1187 csEntry.symTypeString =
"PDB";
1190 csEntry.symTypeString =
"-exported-";
1193 csEntry.symTypeString =
"-deferred-";
1196 csEntry.symTypeString =
"SYM";
1198 #if API_VERSION_NUMBER >= 9
1200 csEntry.symTypeString =
"DIA";
1204 csEntry.symTypeString =
"Virtual";
1208 csEntry.symTypeString = NULL;
1212 MyStrCpy(csEntry.moduleName, STACKWALK_MAX_NAMELEN, Module.ModuleName);
1213 csEntry.baseOfImage = Module.BaseOfImage;
1214 MyStrCpy(csEntry.loadedImageName, STACKWALK_MAX_NAMELEN, Module.LoadedImageName);
1218 this->OnDbgHelpErr(
"SymGetModuleInfo64", GetLastError(), s.AddrPC.Offset);
1222 CallstackEntryType et = nextEntry;
1225 bLastEntryCalled =
false;
1226 this->OnCallstackEntry(et, csEntry);
1228 if (s.AddrReturn.Offset == 0)
1230 bLastEntryCalled =
true;
1231 this->OnCallstackEntry(lastEntry, csEntry);
1232 SetLastError(ERROR_SUCCESS);
1238 if (pSym) free( pSym );
1240 if (bLastEntryCalled ==
false)
1241 this->OnCallstackEntry(lastEntry, csEntry);
1243 if (context == NULL)
1244 ResumeThread(hThread);
1249 BOOL __stdcall StackWalker::myReadProcMem(
1251 DWORD64 qwBaseAddress,
1254 LPDWORD lpNumberOfBytesRead
1257 if (s_readMemoryFunction == NULL)
1260 BOOL bRet = ReadProcessMemory(hProcess, (LPVOID) qwBaseAddress, lpBuffer, nSize, &st);
1261 *lpNumberOfBytesRead = (DWORD) st;
1267 return s_readMemoryFunction(hProcess, qwBaseAddress, lpBuffer, nSize, lpNumberOfBytesRead, s_readMemoryFunction_UserData);
1271 void StackWalker::OnLoadModule(LPCSTR img, LPCSTR mod, DWORD64 baseAddr, DWORD size, DWORD result, LPCSTR symType, LPCSTR pdbName, ULONGLONG fileVersion)
1273 CHAR buffer[STACKWALK_MAX_NAMELEN];
1274 if (fileVersion == 0)
1275 _snprintf_s(buffer, STACKWALK_MAX_NAMELEN,
"%s:%s (%p), size: %d (result: %d), SymType: '%s', PDB: '%s'\n", img, mod, (LPVOID) baseAddr, size, result, symType, pdbName);
1278 DWORD v4 = (DWORD) (fileVersion & 0xFFFF);
1279 DWORD v3 = (DWORD) ((fileVersion>>16) & 0xFFFF);
1280 DWORD v2 = (DWORD) ((fileVersion>>32) & 0xFFFF);
1281 DWORD v1 = (DWORD) ((fileVersion>>48) & 0xFFFF);
1282 _snprintf_s(buffer, STACKWALK_MAX_NAMELEN,
"%s:%s (%p), size: %d (result: %d), SymType: '%s', PDB: '%s', fileVersion: %d.%d.%d.%d\n", img, mod, (LPVOID) baseAddr, size, result, symType, pdbName, v1, v2, v3, v4);
1287 void StackWalker::OnCallstackEntry(CallstackEntryType eType, CallstackEntry &entry)
1289 CHAR buffer[STACKWALK_MAX_NAMELEN];
1290 if ( (eType != lastEntry) && (entry.offset != 0) )
1292 if (entry.name[0] == 0)
1293 MyStrCpy(entry.name, STACKWALK_MAX_NAMELEN,
"(function-name not available)");
1294 if (entry.undName[0] != 0)
1295 MyStrCpy(entry.name, STACKWALK_MAX_NAMELEN, entry.undName);
1296 if (entry.undFullName[0] != 0)
1297 MyStrCpy(entry.name, STACKWALK_MAX_NAMELEN, entry.undFullName);
1298 if (entry.lineFileName[0] == 0)
1300 MyStrCpy(entry.lineFileName, STACKWALK_MAX_NAMELEN,
"(filename not available)");
1301 if (entry.moduleName[0] == 0)
1302 MyStrCpy(entry.moduleName, STACKWALK_MAX_NAMELEN,
"(module-name not available)");
1303 _snprintf_s(buffer, STACKWALK_MAX_NAMELEN,
"%p (%s): %s: %s\n", (LPVOID) entry.offset, entry.moduleName, entry.lineFileName, entry.name);
1306 _snprintf_s(buffer, STACKWALK_MAX_NAMELEN,
"%s (%d): %s\n", entry.lineFileName, entry.lineNumber, entry.name);
1307 buffer[STACKWALK_MAX_NAMELEN-1] = 0;
1312 void StackWalker::OnDbgHelpErr(LPCSTR szFuncName, DWORD gle, DWORD64 addr)
1314 CHAR buffer[STACKWALK_MAX_NAMELEN];
1315 _snprintf_s(buffer, STACKWALK_MAX_NAMELEN,
"ERROR: %s, GetLastError: %d (Address: %p)\n", szFuncName, gle, (LPVOID) addr);
1319 void StackWalker::OnSymInit(LPCSTR szSearchPath, DWORD symOptions, LPCSTR szUserName)
1321 CHAR buffer[STACKWALK_MAX_NAMELEN];
1322 _snprintf_s(buffer, STACKWALK_MAX_NAMELEN,
"SymInit: Symbol-SearchPath: '%s', symOptions: %d, UserName: '%s'\n", szSearchPath, symOptions, szUserName);
1325 #if _MSC_VER <= 1200
1327 ZeroMemory(&ver,
sizeof(OSVERSIONINFOA));
1328 ver.dwOSVersionInfoSize =
sizeof(ver);
1329 if (GetVersionExA(&ver) != FALSE)
1331 _snprintf_s(buffer, STACKWALK_MAX_NAMELEN,
"OS-Version: %d.%d.%d (%s)\n",
1332 ver.dwMajorVersion, ver.dwMinorVersion, ver.dwBuildNumber,
1337 OSVERSIONINFOEXA ver;
1338 ZeroMemory(&ver,
sizeof(OSVERSIONINFOEXA));
1339 ver.dwOSVersionInfoSize =
sizeof(ver);
1340 if (GetVersionExA( (OSVERSIONINFOA*) &ver) != FALSE)
1342 _snprintf_s(buffer, STACKWALK_MAX_NAMELEN,
"OS-Version: %d.%d.%d (%s) 0x%x-0x%x\n",
1343 ver.dwMajorVersion, ver.dwMinorVersion, ver.dwBuildNumber,
1344 ver.szCSDVersion, ver.wSuiteMask, ver.wProductType);
1350 void StackWalker::OnOutput(LPCSTR buffer)
1352 OutputDebugStringA(buffer);