2014-03-17 19:44:11 +00:00
|
|
|
// $Id$
|
2004-11-14 08:29:32 +00:00
|
|
|
//
|
2006-04-08 20:22:08 +00:00
|
|
|
|
2004-02-06 03:12:30 +00:00
|
|
|
/*////////////////////////////////////////////////////////////////////////////
|
|
|
|
* Project:
|
|
|
|
* Memory_and_Exception_Trace
|
|
|
|
*
|
|
|
|
* ///////////////////////////////////////////////////////////////////////////
|
|
|
|
* File:
|
|
|
|
* Stackwalker.cpp
|
|
|
|
*
|
|
|
|
* Remarks:
|
2021-10-01 07:36:00 +00:00
|
|
|
* Dumps the stack of an thread if an exception occurs
|
2004-02-06 03:12:30 +00:00
|
|
|
*
|
|
|
|
* Author:
|
|
|
|
* Jochen Kalmbach, Germany
|
2004-03-06 09:45:25 +00:00
|
|
|
* (c) 2002-2003 (Freeware)
|
|
|
|
* http://www.codeproject.com/tools/leakfinder.asp
|
|
|
|
*
|
|
|
|
* License (The zlib/libpng License, http://www.opensource.org/licenses/zlib-license.php):
|
|
|
|
*
|
|
|
|
* Copyright (c) 2003 Jochen Kalmbach
|
|
|
|
*
|
|
|
|
* This software is provided 'as-is', without any express or implied warranty.
|
|
|
|
* In no event will the authors be held liable for any damages arising from the
|
|
|
|
* use of this software.
|
|
|
|
*
|
|
|
|
* Permission is granted to anyone to use this software for any purpose, including
|
|
|
|
* commercial applications, and to alter it and redistribute it freely, subject to
|
|
|
|
* the following restrictions:
|
|
|
|
*
|
|
|
|
* 1. The origin of this software must not be misrepresented; you must not claim
|
|
|
|
* that you wrote the original software. If you use this software in a product,
|
|
|
|
* an acknowledgment in the product documentation would be appreciated but is
|
|
|
|
* not required.
|
|
|
|
*
|
|
|
|
* 2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
* misrepresented as being the original software.
|
|
|
|
*
|
|
|
|
* 3. This notice may not be removed or altered from any source distribution.
|
2004-02-06 03:12:30 +00:00
|
|
|
*
|
|
|
|
*//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2005-08-03 00:14:26 +00:00
|
|
|
|
2020-06-04 07:48:21 +00:00
|
|
|
#if defined(_WIN32)
|
2005-08-03 00:14:26 +00:00
|
|
|
#include "boinc_win.h"
|
|
|
|
#endif
|
|
|
|
|
2007-01-04 17:46:38 +00:00
|
|
|
#include "diagnostics.h"
|
2016-02-15 22:54:13 +00:00
|
|
|
#include "str_replace.h"
|
2016-09-09 16:46:04 +00:00
|
|
|
#include "str_util.h"
|
2006-04-08 20:22:08 +00:00
|
|
|
#include "stackwalker_win.h"
|
2006-05-01 21:36:38 +00:00
|
|
|
#include "stackwalker_imports.h"
|
|
|
|
|
|
|
|
|
|
|
|
// Link to dbghelp.dll and version.dll dynamically at runtime so we
|
|
|
|
// can be specific about which version we are getting and where
|
|
|
|
// we are getting it from
|
|
|
|
static tIAV pIAV = NULL; // ImagehlpApiVersion()
|
|
|
|
static tSC pSC = NULL; // SymCleanup()
|
|
|
|
static tSEM pSEM = NULL; // SymEnumerateModules64()
|
|
|
|
static tSFTA pSFTA = NULL; // SymFunctionTableAccess64()
|
|
|
|
static tSGLFA pSGLFA = NULL; // SymGetLineFromAddr64()
|
|
|
|
static tSGMB pSGMB = NULL; // SymGetModuleBase64()
|
|
|
|
static tSGMI pSGMI = NULL; // SymGetModuleInfo64()
|
|
|
|
static tSGO pSGO = NULL; // SymGetOptions()
|
|
|
|
static tSGSP pSGSP = NULL; // SymGetSearchPath()
|
|
|
|
static tSFA pSFA = NULL; // SymFromAddr()
|
|
|
|
static tSI pSI = NULL; // SymInitialize()
|
|
|
|
static tSLM pSLM = NULL; // SymLoadModuleEx()
|
|
|
|
static tSRC pSRC = NULL; // SymRegisterCallback64()
|
|
|
|
static tSSO pSSO = NULL; // SymSetOptions()
|
|
|
|
static tSW pSW = NULL; // StackWalk()
|
|
|
|
static tUDSN pUDSN = NULL; // UnDecorateSymbolName()
|
|
|
|
static tSSSO pSSSO = NULL; // SymbolServerSetOptions
|
|
|
|
static tSDD pSDD = NULL; // SetDllDirectory
|
|
|
|
static tGFVIS pGFVIS = NULL; // GetFileVersionInfoSize
|
|
|
|
static tGFVI pGFVI = NULL; // GetFileVersionInfo
|
|
|
|
static tVQV pVQV = NULL; // VerQueryValue
|
2006-04-15 22:45:02 +00:00
|
|
|
|
|
|
|
|
2006-04-08 20:22:08 +00:00
|
|
|
// Forward definitions of functions:
|
2006-04-15 22:45:02 +00:00
|
|
|
static void ShowStackRM(HANDLE hThread, CONTEXT& c);
|
2004-02-06 03:12:30 +00:00
|
|
|
|
2006-04-08 20:22:08 +00:00
|
|
|
// Global data:
|
|
|
|
static BOOL g_bInitialized = FALSE;
|
2006-04-15 22:45:02 +00:00
|
|
|
static HANDLE g_hProcess = NULL;
|
2006-04-17 06:46:48 +00:00
|
|
|
static HMODULE g_hDbgHelpDll = NULL;
|
|
|
|
static HMODULE g_hSymSrvDll = NULL;
|
|
|
|
static HMODULE g_hSrcSrvDll = NULL;
|
|
|
|
static HMODULE g_hVersionDll = NULL;
|
2006-04-08 20:22:08 +00:00
|
|
|
static CRITICAL_SECTION g_csFileOpenClose = {0};
|
|
|
|
|
|
|
|
|
|
|
|
// ##########################################################################################
|
|
|
|
// ##########################################################################################
|
|
|
|
// ##########################################################################################
|
|
|
|
// ##########################################################################################
|
|
|
|
|
|
|
|
|
2006-04-10 06:20:02 +00:00
|
|
|
bool DebuggerLoadLibrary(
|
|
|
|
HINSTANCE* lphInstance, const std::string strBOINCLocation, const std::string strLibrary
|
|
|
|
)
|
|
|
|
{
|
|
|
|
std::string strTargetLibrary;
|
|
|
|
|
|
|
|
if (strBOINCLocation.empty()) {
|
|
|
|
strTargetLibrary = strLibrary;
|
|
|
|
} else {
|
|
|
|
strTargetLibrary = strBOINCLocation + "\\" + strLibrary;
|
|
|
|
}
|
|
|
|
|
|
|
|
*lphInstance = LoadLibraryA( strTargetLibrary.c_str() );
|
|
|
|
if ( *lphInstance == NULL )
|
|
|
|
{
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "LoadLibraryA( %s ): GetLastError = %lu\n", strTargetLibrary.c_str(), gle);
|
2008-05-09 09:23:24 +00:00
|
|
|
|
2006-04-10 06:20:02 +00:00
|
|
|
strTargetLibrary = strLibrary;
|
|
|
|
*lphInstance = LoadLibraryA( strTargetLibrary.c_str() );
|
|
|
|
if ( *lphInstance == NULL )
|
|
|
|
{
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "LoadLibraryA( %s ): GetLastError = %lu\n", strTargetLibrary.c_str(), gle);
|
2006-04-10 06:20:02 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "Loaded Library : %s\n", strTargetLibrary.c_str());
|
2006-04-10 06:20:02 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-10-23 03:02:16 +00:00
|
|
|
BOOL CALLBACK SymbolServerCallbackProc64(UINT_PTR ActionCode, ULONG64 CallbackData, ULONG64 /* UserContext */)
|
2006-04-15 22:45:02 +00:00
|
|
|
{
|
|
|
|
BOOL bRetVal = FALSE;
|
|
|
|
PIMAGEHLP_CBA_EVENT pEvent = NULL;
|
|
|
|
|
|
|
|
switch(ActionCode) {
|
|
|
|
case SSRVACTION_EVENT:
|
|
|
|
pEvent = (PIMAGEHLP_CBA_EVENT)CallbackData;
|
|
|
|
switch(pEvent->severity) {
|
|
|
|
case sevInfo:
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "SSRVINFO: %s\n", pEvent->desc);
|
2006-04-15 22:45:02 +00:00
|
|
|
break;
|
|
|
|
case sevProblem:
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "SSRVPROB: %s\n", pEvent->desc);
|
2006-04-15 22:45:02 +00:00
|
|
|
break;
|
|
|
|
case sevAttn:
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "SSRVATTN: %s\n", pEvent->desc);
|
2006-04-15 22:45:02 +00:00
|
|
|
break;
|
|
|
|
case sevFatal:
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "SSRVFATAL: %s\n", pEvent->desc);
|
2006-04-15 22:45:02 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
bRetVal = TRUE;
|
|
|
|
break;
|
|
|
|
case SSRVACTION_TRACE:
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "SSRVDEBUG: %s\n", (PCTSTR)CallbackData);
|
2006-04-15 22:45:02 +00:00
|
|
|
bRetVal = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bRetVal;
|
|
|
|
}
|
2006-04-10 06:20:02 +00:00
|
|
|
|
2008-10-23 03:02:16 +00:00
|
|
|
BOOL CALLBACK SymRegisterCallbackProc64(HANDLE /* hProcess */, ULONG ActionCode, ULONG64 CallbackData, ULONG64 /* UserContext */)
|
2004-02-06 03:12:30 +00:00
|
|
|
{
|
2006-04-08 20:22:08 +00:00
|
|
|
BOOL bRetVal = FALSE;
|
2006-04-15 22:45:02 +00:00
|
|
|
PIMAGEHLP_CBA_EVENT pEvent = NULL;
|
2006-04-08 20:22:08 +00:00
|
|
|
|
|
|
|
switch(ActionCode) {
|
2006-04-15 22:45:02 +00:00
|
|
|
case CBA_EVENT:
|
|
|
|
pEvent = (PIMAGEHLP_CBA_EVENT)CallbackData;
|
|
|
|
switch(pEvent->severity) {
|
|
|
|
case sevInfo:
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "INFO: %s\n", pEvent->desc);
|
2006-04-15 22:45:02 +00:00
|
|
|
break;
|
|
|
|
case sevProblem:
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "PROB: %s\n", pEvent->desc);
|
2006-04-15 22:45:02 +00:00
|
|
|
break;
|
|
|
|
case sevAttn:
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "ATTN: %s\n", pEvent->desc);
|
2006-04-15 22:45:02 +00:00
|
|
|
break;
|
|
|
|
case sevFatal:
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "FATAL: %s\n", pEvent->desc);
|
2006-04-15 22:45:02 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-04-08 20:22:08 +00:00
|
|
|
bRetVal = TRUE;
|
|
|
|
break;
|
2006-04-15 22:45:02 +00:00
|
|
|
case CBA_DEBUG_INFO:
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "DEBUG: %s\n", (PCTSTR)CallbackData);
|
2006-04-08 20:22:08 +00:00
|
|
|
bRetVal = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bRetVal;
|
|
|
|
}
|
2004-02-06 03:12:30 +00:00
|
|
|
|
2008-10-23 03:02:16 +00:00
|
|
|
BOOL CALLBACK SymEnumerateModulesProc64(LPCSTR /* ModuleName */, DWORD64 BaseOfDll, PVOID /* UserContext */)
|
2004-02-06 03:12:30 +00:00
|
|
|
{
|
2006-04-17 06:46:48 +00:00
|
|
|
IMAGEHLP_MODULE64 Module;
|
|
|
|
char szSymbolType[32];
|
|
|
|
DWORD dwHandle;
|
|
|
|
LPVOID lpData;
|
|
|
|
DWORD dwSize;
|
|
|
|
char szQuery[256];
|
|
|
|
LPVOID lpVar;
|
|
|
|
UINT uiVarSize;
|
|
|
|
VS_FIXEDFILEINFO* pFileInfo;
|
|
|
|
char szVersionInfo[24];
|
|
|
|
char szCompanyName[256];
|
|
|
|
char szProductName[256];
|
|
|
|
char szFileVersion[256];
|
|
|
|
char szProductVersion[256];
|
|
|
|
bool bFileVersionSupported = false;
|
|
|
|
bool bFileVersionRetrieved = false;
|
|
|
|
|
|
|
|
struct LANGANDCODEPAGE {
|
|
|
|
WORD wLanguage;
|
|
|
|
WORD wCodePage;
|
|
|
|
} *lpTranslate;
|
|
|
|
|
2004-02-06 03:12:30 +00:00
|
|
|
|
2006-04-08 20:22:08 +00:00
|
|
|
memset( &Module, '\0', sizeof(IMAGEHLP_MODULE64) );
|
|
|
|
Module.SizeOfStruct = sizeof(IMAGEHLP_MODULE64);
|
2004-02-06 03:12:30 +00:00
|
|
|
|
2006-04-17 06:46:48 +00:00
|
|
|
memset( &szSymbolType, '\0', sizeof(szSymbolType) );
|
|
|
|
memset( &szQuery, '\0', sizeof(szQuery) );
|
|
|
|
memset( &szVersionInfo, '\0', sizeof(szVersionInfo) );
|
|
|
|
memset( &szCompanyName, '\0', sizeof(szCompanyName) );
|
|
|
|
memset( &szProductName, '\0', sizeof(szProductName) );
|
|
|
|
memset( &szFileVersion, '\0', sizeof(szFileVersion) );
|
|
|
|
memset( &szProductVersion, '\0', sizeof(szProductVersion) );
|
2004-02-06 03:12:30 +00:00
|
|
|
|
2006-04-15 22:45:02 +00:00
|
|
|
if ( !pSGMI( g_hProcess, BaseOfDll, &Module ) )
|
2006-04-08 20:22:08 +00:00
|
|
|
{
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "SymGetModuleInfo(): GetLastError = %lu\n", gle);
|
2006-04-08 20:22:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch ( Module.SymType )
|
|
|
|
{
|
|
|
|
case SymNone:
|
2016-02-15 22:54:13 +00:00
|
|
|
safe_strcpy( szSymbolType, "-nosymbols-" );
|
2006-04-08 20:22:08 +00:00
|
|
|
break;
|
|
|
|
case SymCoff:
|
2016-02-15 22:54:13 +00:00
|
|
|
safe_strcpy( szSymbolType, "COFF" );
|
2006-04-08 20:22:08 +00:00
|
|
|
break;
|
|
|
|
case SymCv:
|
2016-02-15 22:54:13 +00:00
|
|
|
safe_strcpy( szSymbolType, "CV" );
|
2006-04-08 20:22:08 +00:00
|
|
|
break;
|
|
|
|
case SymPdb:
|
2016-02-15 22:54:13 +00:00
|
|
|
safe_strcpy( szSymbolType, "PDB" );
|
2006-04-08 20:22:08 +00:00
|
|
|
break;
|
|
|
|
case SymExport:
|
2016-02-15 22:54:13 +00:00
|
|
|
safe_strcpy( szSymbolType, "-exported-" );
|
2006-04-08 20:22:08 +00:00
|
|
|
break;
|
|
|
|
case SymDeferred:
|
2016-02-15 22:54:13 +00:00
|
|
|
safe_strcpy( szSymbolType, "-deferred-" );
|
2006-04-08 20:22:08 +00:00
|
|
|
break;
|
|
|
|
case SymSym:
|
2016-02-15 22:54:13 +00:00
|
|
|
safe_strcpy( szSymbolType, "SYM" );
|
2006-04-08 20:22:08 +00:00
|
|
|
break;
|
|
|
|
default:
|
2011-07-14 15:54:52 +00:00
|
|
|
snprintf( szSymbolType, sizeof(szSymbolType), "symtype=%ld", (long) Module.SymType );
|
2006-04-08 20:22:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-02-06 03:12:30 +00:00
|
|
|
|
2006-04-17 06:46:48 +00:00
|
|
|
// Get File Version Information
|
|
|
|
//
|
|
|
|
bFileVersionSupported = (NULL != pGFVIS) && (NULL != pGFVI) && (NULL != pVQV);
|
|
|
|
if (bFileVersionSupported) {
|
|
|
|
|
|
|
|
dwSize = pGFVIS(Module.LoadedImageName, &dwHandle);
|
|
|
|
if (dwSize) {
|
|
|
|
lpData = (LPVOID)malloc(dwSize);
|
|
|
|
if(pGFVI(Module.LoadedImageName, dwHandle, dwSize, lpData)) {
|
|
|
|
bFileVersionRetrieved = true;
|
|
|
|
|
|
|
|
// Which language should be used to lookup the structure?
|
2016-02-15 22:54:13 +00:00
|
|
|
safe_strcpy(szQuery, "\\VarFileInfo\\Translation");
|
2006-04-17 06:46:48 +00:00
|
|
|
pVQV(lpData, szQuery, (LPVOID*)&lpTranslate, &uiVarSize);
|
|
|
|
|
|
|
|
|
|
|
|
// Version specified as part of the root record.
|
|
|
|
if (pVQV(lpData, "\\", (LPVOID*)&pFileInfo, &uiVarSize)) {
|
|
|
|
snprintf(szVersionInfo, sizeof(szVersionInfo), "%d.%d.%d.%d",
|
|
|
|
HIWORD(pFileInfo->dwFileVersionMS),
|
|
|
|
LOWORD(pFileInfo->dwFileVersionMS),
|
|
|
|
HIWORD(pFileInfo->dwFileVersionLS),
|
|
|
|
LOWORD(pFileInfo->dwFileVersionLS)
|
|
|
|
); }
|
|
|
|
|
|
|
|
// Company Name.
|
2022-08-03 09:21:59 +00:00
|
|
|
snprintf(szQuery, sizeof(szQuery), "\\StringFileInfo\\%04x%04x\\CompanyName",
|
2006-04-17 06:46:48 +00:00
|
|
|
lpTranslate[0].wLanguage,
|
|
|
|
lpTranslate[0].wCodePage
|
|
|
|
);
|
|
|
|
if (pVQV(lpData, szQuery, &lpVar, &uiVarSize)) {
|
|
|
|
uiVarSize = snprintf(szCompanyName, sizeof(szCompanyName), "%s", lpVar);
|
|
|
|
if ((sizeof(szCompanyName) == uiVarSize) || (-1 == uiVarSize)) {
|
|
|
|
szCompanyName[255] = '\0';
|
|
|
|
}
|
|
|
|
} else {
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "Get Company Name Failed.\n");
|
2006-04-17 06:46:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Product Name.
|
2022-08-03 09:21:59 +00:00
|
|
|
snprintf(szQuery, sizeof(szQuery), "\\StringFileInfo\\%04x%04x\\ProductName",
|
2006-04-17 06:46:48 +00:00
|
|
|
lpTranslate[0].wLanguage,
|
|
|
|
lpTranslate[0].wCodePage
|
|
|
|
);
|
|
|
|
if (pVQV(lpData, szQuery, &lpVar, &uiVarSize)) {
|
|
|
|
uiVarSize = snprintf(szProductName, sizeof(szProductName), "%s", lpVar);
|
|
|
|
if ((sizeof(szProductName) == uiVarSize) || (-1 == uiVarSize)) {
|
|
|
|
szProductName[255] = '\0';
|
|
|
|
}
|
|
|
|
} else {
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "Get Product Name Failed.\n");
|
2006-04-17 06:46:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// File Version.
|
2022-08-03 09:21:59 +00:00
|
|
|
snprintf(szQuery, sizeof(szQuery), "\\StringFileInfo\\%04x%04x\\FileVersion",
|
2006-04-17 06:46:48 +00:00
|
|
|
lpTranslate[0].wLanguage,
|
|
|
|
lpTranslate[0].wCodePage
|
|
|
|
);
|
|
|
|
if (pVQV(lpData, szQuery, &lpVar, &uiVarSize)) {
|
|
|
|
uiVarSize = snprintf(szFileVersion, sizeof(szFileVersion), "%s", lpVar);
|
|
|
|
if ((sizeof(szFileVersion) == uiVarSize) || (-1 == uiVarSize)) {
|
|
|
|
szFileVersion[255] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Product Version.
|
2022-08-03 09:21:59 +00:00
|
|
|
snprintf(szQuery, sizeof(szQuery), "\\StringFileInfo\\%04x%04x\\ProductVersion",
|
2006-04-17 06:46:48 +00:00
|
|
|
lpTranslate[0].wLanguage,
|
|
|
|
lpTranslate[0].wCodePage
|
|
|
|
);
|
|
|
|
if (pVQV(lpData, szQuery, &lpVar, &uiVarSize)) {
|
|
|
|
uiVarSize = snprintf(szProductVersion, sizeof(szProductVersion), "%s", lpVar);
|
|
|
|
if ((sizeof(szProductVersion) == uiVarSize) || (-1 == uiVarSize)) {
|
|
|
|
szProductVersion[255] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(lpData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "ModLoad: ");
|
|
|
|
fprintf(stderr, "%.16x " , Module.BaseOfImage);
|
|
|
|
fprintf(stderr, "%.16x " , Module.ImageSize);
|
|
|
|
fprintf(stderr, "%s " , Module.LoadedImageName);
|
2006-04-17 06:46:48 +00:00
|
|
|
if (bFileVersionSupported && bFileVersionRetrieved) {
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "(%s) " , szVersionInfo);
|
2006-04-17 06:46:48 +00:00
|
|
|
}
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "(%s Symbols Loaded)" , szSymbolType);
|
|
|
|
fprintf(stderr, "\n");
|
2008-06-17 16:19:36 +00:00
|
|
|
#ifndef __MINGW32__
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, " Linked PDB Filename : %s\n" , Module.CVData);
|
2008-06-17 16:19:36 +00:00
|
|
|
#endif
|
2006-04-17 06:46:48 +00:00
|
|
|
if (bFileVersionSupported && bFileVersionRetrieved) {
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, " File Version : %s\n" , szFileVersion);
|
|
|
|
fprintf(stderr, " Company Name : %s\n" , szCompanyName);
|
|
|
|
fprintf(stderr, " Product Name : %s\n" , szProductName);
|
|
|
|
fprintf(stderr, " Product Version : %s\n" , szProductVersion);
|
2006-04-17 06:46:48 +00:00
|
|
|
}
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "\n");
|
2004-02-06 03:12:30 +00:00
|
|
|
|
2006-04-08 20:22:08 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-04-15 22:45:02 +00:00
|
|
|
int DebuggerInitialize( LPCSTR pszBOINCLocation, LPCSTR pszSymbolStore, BOOL bProxyEnabled, LPCSTR pszProxyServer )
|
2004-02-06 03:12:30 +00:00
|
|
|
{
|
2006-04-08 20:22:08 +00:00
|
|
|
if (g_bInitialized != FALSE)
|
|
|
|
return 0;
|
|
|
|
|
2006-04-15 22:45:02 +00:00
|
|
|
// Get a real handle to the current process and store it for future use.
|
|
|
|
DuplicateHandle(
|
|
|
|
GetCurrentProcess(),
|
|
|
|
GetCurrentProcess(),
|
|
|
|
GetCurrentProcess(),
|
|
|
|
&g_hProcess,
|
|
|
|
0,
|
|
|
|
false,
|
|
|
|
DUPLICATE_SAME_ACCESS
|
|
|
|
);
|
|
|
|
|
2011-07-14 15:54:52 +00:00
|
|
|
if (!DebuggerLoadLibrary(&g_hDbgHelpDll, pszBOINCLocation, "dbghelp.dll")) {
|
|
|
|
g_bInitialized = FALSE;
|
|
|
|
return 1;
|
|
|
|
}
|
2006-04-17 06:46:48 +00:00
|
|
|
|
2011-07-14 15:54:52 +00:00
|
|
|
DebuggerLoadLibrary(&g_hSymSrvDll, pszBOINCLocation, "symsrv.dll");
|
|
|
|
DebuggerLoadLibrary(&g_hSrcSrvDll, pszBOINCLocation, "srcsrv.dll");
|
|
|
|
DebuggerLoadLibrary(&g_hVersionDll, pszBOINCLocation, "version.dll");
|
2008-04-30 05:42:06 +00:00
|
|
|
|
2011-07-14 15:54:52 +00:00
|
|
|
if (g_hSymSrvDll) {
|
|
|
|
pSSSO = (tSSSO)GetProcAddress(g_hSymSrvDll, "SymbolServerSetOptions");
|
|
|
|
if (pSSSO) {
|
|
|
|
if (!pSSSO(SSRVOPT_TRACE, (ULONG64)TRUE)) {
|
|
|
|
fprintf(stderr, "SymbolServerSetOptions(): Register Trace Failed, GetLastError = %lu\n", gle);
|
|
|
|
}
|
|
|
|
if (!pSSSO(SSRVOPT_CALLBACK, (ULONG64)SymbolServerCallbackProc64)) {
|
|
|
|
fprintf(stderr, "SymbolServerSetOptions(): Register Callback Failed, GetLastError = %lu\n", gle);
|
|
|
|
}
|
|
|
|
if (!pSSSO(SSRVOPT_UNATTENDED, (ULONG64)TRUE)) {
|
|
|
|
fprintf(stderr, "SymbolServerSetOptions(): Register Unattended Failed, GetLastError = %lu\n", gle);
|
|
|
|
}
|
|
|
|
if (bProxyEnabled) {
|
|
|
|
if (!pSSSO(SSRVOPT_PROXY, (ULONG64)pszProxyServer)) {
|
|
|
|
fprintf(stderr, "SymbolServerSetOptions(): Register Proxy Failed, GetLastError = %lu\n", gle);
|
2006-04-15 22:45:02 +00:00
|
|
|
}
|
2011-07-14 15:54:52 +00:00
|
|
|
} else {
|
|
|
|
if (!pSSSO(SSRVOPT_PROXY, (ULONG64)NULL)) {
|
|
|
|
fprintf(stderr, "SymbolServerSetOptions(): Register Proxy Failed, GetLastError = %lu\n", gle);
|
2006-04-15 22:45:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-07-14 15:54:52 +00:00
|
|
|
}
|
2006-04-17 06:46:48 +00:00
|
|
|
|
2011-07-14 15:54:52 +00:00
|
|
|
|
|
|
|
if (g_hVersionDll) {
|
|
|
|
pGFVIS = (tGFVIS)GetProcAddress(g_hVersionDll, "GetFileVersionInfoSizeA");
|
|
|
|
pGFVI = (tGFVI)GetProcAddress(g_hVersionDll, "GetFileVersionInfoA");
|
|
|
|
pVQV = (tVQV)GetProcAddress(g_hVersionDll, "VerQueryValueA");
|
2006-04-08 20:22:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pIAV = (tIAV) GetProcAddress( g_hDbgHelpDll, "ImagehlpApiVersion" );
|
|
|
|
pSC = (tSC) GetProcAddress( g_hDbgHelpDll, "SymCleanup" );
|
|
|
|
pSEM = (tSEM) GetProcAddress( g_hDbgHelpDll, "SymEnumerateModules64" );
|
|
|
|
pSFTA = (tSFTA) GetProcAddress( g_hDbgHelpDll, "SymFunctionTableAccess64" );
|
|
|
|
pSGLFA = (tSGLFA) GetProcAddress( g_hDbgHelpDll, "SymGetLineFromAddr64" );
|
|
|
|
pSGMB = (tSGMB) GetProcAddress( g_hDbgHelpDll, "SymGetModuleBase64" );
|
|
|
|
pSGMI = (tSGMI) GetProcAddress( g_hDbgHelpDll, "SymGetModuleInfo64" );
|
|
|
|
pSGO = (tSGO) GetProcAddress( g_hDbgHelpDll, "SymGetOptions" );
|
|
|
|
pSGSP = (tSGSP) GetProcAddress( g_hDbgHelpDll, "SymGetSearchPath" );
|
|
|
|
pSFA = (tSFA) GetProcAddress( g_hDbgHelpDll, "SymFromAddr" );
|
|
|
|
pSI = (tSI) GetProcAddress( g_hDbgHelpDll, "SymInitialize" );
|
|
|
|
pSRC = (tSRC) GetProcAddress( g_hDbgHelpDll, "SymRegisterCallback64" );
|
|
|
|
pSSO = (tSSO) GetProcAddress( g_hDbgHelpDll, "SymSetOptions" );
|
|
|
|
pSW = (tSW) GetProcAddress( g_hDbgHelpDll, "StackWalk64" );
|
|
|
|
pUDSN = (tUDSN) GetProcAddress( g_hDbgHelpDll, "UnDecorateSymbolName" );
|
|
|
|
pSLM = (tSLM) GetProcAddress( g_hDbgHelpDll, "SymLoadModuleEx" );
|
|
|
|
|
|
|
|
if ( pIAV == NULL || pSC == NULL || pSEM == NULL || pSFTA == NULL ||
|
|
|
|
pSGMB == NULL || pSGMI == NULL || pSGO == NULL || pSFA == NULL ||
|
|
|
|
pSI == NULL || pSRC == NULL || pSSO == NULL || pSW == NULL ||
|
|
|
|
pUDSN == NULL || pSLM == NULL )
|
2006-03-17 09:03:29 +00:00
|
|
|
{
|
2006-06-08 19:40:14 +00:00
|
|
|
if (!pIAV) fprintf( stderr, "GetProcAddress(): ImagehlpApiVersion missing.\n" );
|
|
|
|
if (!pSC) fprintf( stderr, "GetProcAddress(): SymCleanup missing.\n" );
|
|
|
|
if (!pSEM) fprintf( stderr, "GetProcAddress(): SymEnumerateModules64 missing.\n" );
|
|
|
|
if (!pSFTA) fprintf( stderr, "GetProcAddress(): SymFunctionTableAccess64 missing.\n" );
|
|
|
|
if (!pSGLFA) fprintf( stderr, "GetProcAddress(): SymGetLineFromAddr64 missing.\n" );
|
|
|
|
if (!pSGMB) fprintf( stderr, "GetProcAddress(): SymGetModuleBase64 missing.\n" );
|
|
|
|
if (!pSGMI) fprintf( stderr, "GetProcAddress(): SymGetModuleInfo64 missing.\n" );
|
|
|
|
if (!pSGO) fprintf( stderr, "GetProcAddress(): SymGetOptions missing.\n" );
|
|
|
|
if (!pSGSP) fprintf( stderr, "GetProcAddress(): SymGetSearchPath missing.\n" );
|
|
|
|
if (!pSFA) fprintf( stderr, "GetProcAddress(): SymFromAddr missing.\n" );
|
|
|
|
if (!pSI) fprintf( stderr, "GetProcAddress(): SymInitialize missing.\n" );
|
|
|
|
if (!pSRC) fprintf( stderr, "GetProcAddress(): SymRegisterCallback64 missing.\n" );
|
|
|
|
if (!pSSO) fprintf( stderr, "GetProcAddress(): SymSetOptions missing.\n" );
|
|
|
|
if (!pSW) fprintf( stderr, "GetProcAddress(): StackWalk64 missing.\n" );
|
|
|
|
if (!pUDSN) fprintf( stderr, "GetProcAddress(): UnDecorateSymbolName missing.\n" );
|
|
|
|
if (!pSLM) fprintf( stderr, "GetProcAddress(): SymLoadModuleEx missing.\n" );
|
|
|
|
|
2006-04-08 20:22:08 +00:00
|
|
|
FreeLibrary( g_hDbgHelpDll );
|
2006-03-17 09:03:29 +00:00
|
|
|
g_bInitialized = FALSE;
|
|
|
|
return 1;
|
|
|
|
}
|
2006-04-08 20:22:08 +00:00
|
|
|
|
|
|
|
g_bInitialized = TRUE;
|
|
|
|
|
|
|
|
InitializeCriticalSection(&g_csFileOpenClose);
|
|
|
|
EnterCriticalSection(&g_csFileOpenClose);
|
|
|
|
|
|
|
|
CHAR* tt;
|
|
|
|
CHAR* p;
|
|
|
|
DWORD symOptions; // symbol handler settings
|
2008-11-24 21:19:42 +00:00
|
|
|
std::string strCurrentDirectory;
|
|
|
|
std::string strExecutableDirectory;
|
2006-04-15 22:45:02 +00:00
|
|
|
std::string strLocalSymbolStore;
|
|
|
|
std::string strSymbolSearchPath;
|
2006-04-08 20:22:08 +00:00
|
|
|
|
|
|
|
tt = (CHAR*) malloc(sizeof(CHAR) * TTBUFLEN); // Get the temporary buffer
|
2020-05-06 20:01:05 +00:00
|
|
|
if (!tt) goto error; // not enough memory...
|
2006-04-08 20:22:08 +00:00
|
|
|
|
|
|
|
// build symbol search path from:
|
2008-11-24 21:19:42 +00:00
|
|
|
strCurrentDirectory = "";
|
|
|
|
strExecutableDirectory = "";
|
2006-04-10 06:20:02 +00:00
|
|
|
strLocalSymbolStore = "";
|
|
|
|
strSymbolSearchPath = "";
|
2006-04-08 20:22:08 +00:00
|
|
|
|
2008-11-24 21:19:42 +00:00
|
|
|
// Detect Current Directory
|
|
|
|
if ( GetCurrentDirectoryA( TTBUFLEN, tt ) ) {
|
|
|
|
strCurrentDirectory = tt;
|
|
|
|
}
|
2006-04-08 20:22:08 +00:00
|
|
|
|
2008-11-24 21:19:42 +00:00
|
|
|
// Detect Executable Directory
|
2006-04-08 20:22:08 +00:00
|
|
|
if ( GetModuleFileNameA( 0, tt, TTBUFLEN ) )
|
|
|
|
{
|
|
|
|
for ( p = tt + strlen( tt ) - 1; p >= tt; -- p )
|
|
|
|
{
|
|
|
|
// locate the rightmost path separator
|
2008-11-24 21:19:42 +00:00
|
|
|
if ( *p == '\\' || *p == '/' || *p == ':' ) {
|
2006-04-08 20:22:08 +00:00
|
|
|
break;
|
2008-11-24 21:19:42 +00:00
|
|
|
}
|
2006-04-08 20:22:08 +00:00
|
|
|
}
|
2008-11-24 21:19:42 +00:00
|
|
|
|
2006-04-08 20:22:08 +00:00
|
|
|
// if we found one, p is pointing at it; if not, tt only contains
|
|
|
|
// an exe name (no path), and p points before its first byte
|
|
|
|
if ( p != tt ) // path sep found?
|
|
|
|
{
|
2008-11-24 21:19:42 +00:00
|
|
|
if ( *p == ':' ) { // we leave colons in place
|
2006-04-08 20:22:08 +00:00
|
|
|
++p;
|
2008-11-24 21:19:42 +00:00
|
|
|
}
|
|
|
|
|
2006-04-08 20:22:08 +00:00
|
|
|
*p = '\0'; // eliminate the exe name and last path sep
|
2008-11-24 21:19:42 +00:00
|
|
|
strExecutableDirectory += tt;
|
2006-04-08 20:22:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-24 21:19:42 +00:00
|
|
|
// Current Directory
|
|
|
|
if (!strCurrentDirectory.empty()) {
|
|
|
|
strSymbolSearchPath += strCurrentDirectory + std::string( ";" );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Executable Directory
|
|
|
|
if (!strExecutableDirectory.empty()) {
|
|
|
|
strSymbolSearchPath += strExecutableDirectory + std::string( ";" );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Environment Variable _NT_SYMBOL_PATH
|
|
|
|
if ( GetEnvironmentVariableA( "_NT_SYMBOL_PATH", tt, TTBUFLEN ) ) {
|
2006-04-15 22:45:02 +00:00
|
|
|
strSymbolSearchPath += tt + std::string( ";" );
|
2008-11-24 21:19:42 +00:00
|
|
|
}
|
2006-04-10 06:20:02 +00:00
|
|
|
|
2008-11-24 21:19:42 +00:00
|
|
|
// Environment Variable _NT_ALTERNATE_SYMBOL_PATH
|
|
|
|
if ( GetEnvironmentVariableA( "_NT_ALT_SYMBOL_PATH", tt, TTBUFLEN ) ) {
|
|
|
|
strSymbolSearchPath += tt + std::string( ";" );
|
|
|
|
}
|
2006-04-08 20:22:08 +00:00
|
|
|
|
2009-06-09 17:30:39 +00:00
|
|
|
// Depending on if we are a BOINC application or a project application
|
|
|
|
// we'll need to store our symbol files in two different locations.
|
|
|
|
//
|
|
|
|
// BOINC:
|
|
|
|
// [DATADIR]\symbols
|
|
|
|
// Project:
|
|
|
|
// [DATADIR]\projects\project_dir\symbols
|
|
|
|
//
|
|
|
|
if (!diagnostics_is_flag_set(BOINC_DIAG_BOINCAPPLICATION)) {
|
|
|
|
strLocalSymbolStore += strCurrentDirectory + std::string("\\symbols");
|
|
|
|
} else {
|
|
|
|
strLocalSymbolStore += strExecutableDirectory + std::string("\\symbols");
|
2006-04-10 06:20:02 +00:00
|
|
|
}
|
2006-04-08 20:22:08 +00:00
|
|
|
|
2009-06-09 17:30:39 +00:00
|
|
|
// Microsoft Public Symbol Server
|
2009-06-11 02:14:36 +00:00
|
|
|
if (!diagnostics_is_flag_set(BOINC_DIAG_BOINCAPPLICATION) || (0 < strlen(pszSymbolStore))) {
|
2009-06-09 17:30:39 +00:00
|
|
|
if (std::string::npos == strSymbolSearchPath.find("http://msdl.microsoft.com/download/symbols")) {
|
|
|
|
strSymbolSearchPath +=
|
|
|
|
std::string( "srv*" ) + strLocalSymbolStore +
|
|
|
|
std::string( "*http://msdl.microsoft.com/download/symbols;" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Project Symbol Server
|
2009-06-11 02:14:36 +00:00
|
|
|
if (diagnostics_is_flag_set(BOINC_DIAG_BOINCAPPLICATION) && (0 < strlen(pszSymbolStore))) {
|
2020-05-06 20:25:50 +00:00
|
|
|
if (std::string::npos == strSymbolSearchPath.find(pszSymbolStore)) {
|
2009-06-09 17:30:39 +00:00
|
|
|
strSymbolSearchPath +=
|
|
|
|
std::string( "srv*" ) + strLocalSymbolStore + std::string( "*" ) +
|
|
|
|
std::string( pszSymbolStore ) + std::string( ";" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BOINC Symbol Server
|
|
|
|
if (!diagnostics_is_flag_set(BOINC_DIAG_BOINCAPPLICATION)) {
|
|
|
|
if (std::string::npos == strSymbolSearchPath.find("http://boinc.berkeley.edu/symstore")) {
|
|
|
|
strSymbolSearchPath +=
|
|
|
|
std::string( "srv*" ) + strLocalSymbolStore +
|
|
|
|
std::string( "*http://boinc.berkeley.edu/symstore;" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-10 06:20:02 +00:00
|
|
|
if ( strSymbolSearchPath.size() > 0 ) // if we added anything, we have a trailing semicolon
|
|
|
|
strSymbolSearchPath = strSymbolSearchPath.substr( 0, strSymbolSearchPath.size() - 1 );
|
2006-04-08 20:22:08 +00:00
|
|
|
|
2020-05-06 19:52:18 +00:00
|
|
|
free( tt );
|
2006-04-15 22:45:02 +00:00
|
|
|
|
2006-04-20 20:16:50 +00:00
|
|
|
// Setting symbol options to the WinDbg defaults.
|
2008-01-31 18:34:51 +00:00
|
|
|
symOptions = (DWORD)NULL;
|
2006-04-20 21:21:08 +00:00
|
|
|
symOptions |= SYMOPT_CASE_INSENSITIVE;
|
|
|
|
symOptions |= SYMOPT_LOAD_LINES;
|
|
|
|
symOptions |= SYMOPT_OMAP_FIND_NEAREST;
|
|
|
|
symOptions |= SYMOPT_FAIL_CRITICAL_ERRORS;
|
|
|
|
symOptions |= SYMOPT_AUTO_PUBLICS;
|
|
|
|
symOptions |= SYMOPT_NO_IMAGE_SEARCH;
|
2006-04-15 22:45:02 +00:00
|
|
|
symOptions |= SYMOPT_DEBUG;
|
2006-04-21 06:37:33 +00:00
|
|
|
symOptions |= SYMOPT_NO_PROMPTS;
|
2006-04-08 20:22:08 +00:00
|
|
|
pSSO( symOptions ); // SymSetOptions()
|
|
|
|
|
2006-04-15 22:45:02 +00:00
|
|
|
// init symbol handler stuff (SymInitialize())
|
|
|
|
if (!pSI(g_hProcess, strSymbolSearchPath.c_str(), TRUE))
|
|
|
|
{
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "SymInitialize(): GetLastError = %lu\n", gle);
|
2020-05-06 20:01:05 +00:00
|
|
|
goto error;
|
2006-04-15 22:45:02 +00:00
|
|
|
}
|
|
|
|
|
2008-10-23 03:02:16 +00:00
|
|
|
if (!pSRC(g_hProcess, SymRegisterCallbackProc64, (ULONG64)g_hProcess))
|
2006-04-15 22:45:02 +00:00
|
|
|
{
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "SymRegisterCallback64(): GetLastError = %lu\n", gle);
|
2006-04-15 22:45:02 +00:00
|
|
|
}
|
|
|
|
|
2006-04-08 20:22:08 +00:00
|
|
|
LeaveCriticalSection(&g_csFileOpenClose);
|
|
|
|
return 0;
|
2020-05-06 20:01:05 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
LeaveCriticalSection(&g_csFileOpenClose);
|
|
|
|
return 1;
|
2004-02-06 03:12:30 +00:00
|
|
|
}
|
|
|
|
|
2006-04-08 20:22:08 +00:00
|
|
|
int DebuggerDisplayDiagnostics()
|
2004-02-06 03:12:30 +00:00
|
|
|
{
|
2006-04-08 20:22:08 +00:00
|
|
|
LPAPI_VERSION lpDV = NULL;
|
2011-07-14 15:54:52 +00:00
|
|
|
char buf[TTBUFLEN];
|
2004-02-06 03:12:30 +00:00
|
|
|
|
2006-06-09 06:45:28 +00:00
|
|
|
if (g_bInitialized == FALSE)
|
|
|
|
{
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "Stackwalker not initialized (or was not able to initialize)!\n");
|
2006-06-09 06:45:28 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
EnterCriticalSection(&g_csFileOpenClose);
|
|
|
|
|
2006-04-08 20:22:08 +00:00
|
|
|
lpDV = pIAV();
|
2011-07-14 15:54:52 +00:00
|
|
|
pSGSP(g_hProcess, (PTSTR)&buf, TTBUFLEN);
|
2006-04-08 20:22:08 +00:00
|
|
|
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "Debugger Engine : %d.%d.%d.%d\n", lpDV->MajorVersion, lpDV->MinorVersion, lpDV->Revision, lpDV->Reserved);
|
|
|
|
fprintf(stderr, "Symbol Search Path: %s\n", buf);
|
|
|
|
fprintf(stderr, "\n\n");
|
2006-04-08 20:22:08 +00:00
|
|
|
|
2008-10-23 03:02:16 +00:00
|
|
|
if (!pSEM(g_hProcess, (PSYM_ENUMMODULES_CALLBACK64)SymEnumerateModulesProc64, NULL))
|
2004-02-06 03:12:30 +00:00
|
|
|
{
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "SymEnumerateModules64(): GetLastError = %lu\n", gle );
|
2004-02-06 03:12:30 +00:00
|
|
|
}
|
2006-04-08 20:22:08 +00:00
|
|
|
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "\n\n");
|
2006-04-08 20:22:08 +00:00
|
|
|
|
|
|
|
LeaveCriticalSection(&g_csFileOpenClose);
|
|
|
|
|
|
|
|
return 0;
|
2004-02-06 03:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// #################################################################################
|
|
|
|
// #################################################################################
|
|
|
|
// Here the Stackwalk-Part begins.
|
|
|
|
// Some of the code is from an example from a book
|
2021-10-08 22:00:01 +00:00
|
|
|
// But I couldn't find the reference anymore... sorry...
|
2004-02-06 03:12:30 +00:00
|
|
|
// If someone knowns, please let me know...
|
|
|
|
// #################################################################################
|
|
|
|
// #################################################################################
|
|
|
|
|
|
|
|
|
|
|
|
// if you use C++ exception handling: install a translator function
|
|
|
|
// with set_se_translator(). In the context of that function (but *not*
|
|
|
|
// afterwards), you can either do your stack dump, or save the CONTEXT
|
|
|
|
// record as a local copy. Note that you must do the stack sump at the
|
|
|
|
// earliest opportunity, to avoid the interesting stackframes being gone
|
|
|
|
// by the time you do the dump.
|
|
|
|
|
|
|
|
// status:
|
|
|
|
// - EXCEPTION_CONTINUE_SEARCH: exception wird weitergereicht
|
|
|
|
// - EXCEPTION_CONTINUE_EXECUTION:
|
|
|
|
// - EXCEPTION_EXECUTE_HANDLER:
|
2006-04-08 20:22:08 +00:00
|
|
|
DWORD StackwalkFilter(EXCEPTION_POINTERS *ep, DWORD status)
|
2004-02-06 03:12:30 +00:00
|
|
|
{
|
|
|
|
HANDLE hThread;
|
|
|
|
|
|
|
|
DuplicateHandle( GetCurrentProcess(), GetCurrentThread(),
|
|
|
|
GetCurrentProcess(), &hThread, 0, false, DUPLICATE_SAME_ACCESS );
|
|
|
|
|
2006-04-15 22:45:02 +00:00
|
|
|
StackwalkThread( hThread, ep->ContextRecord );
|
2004-02-06 03:12:30 +00:00
|
|
|
|
2006-04-08 20:22:08 +00:00
|
|
|
CloseHandle( hThread );
|
2004-02-06 03:12:30 +00:00
|
|
|
|
|
|
|
return status;
|
|
|
|
} // StackwalkFilter
|
|
|
|
|
2006-04-08 20:22:08 +00:00
|
|
|
void StackwalkThread(HANDLE hThread, CONTEXT* c)
|
2006-03-21 12:04:14 +00:00
|
|
|
{
|
2006-04-15 22:45:02 +00:00
|
|
|
ShowStackRM(hThread, *c);
|
2006-03-21 12:04:14 +00:00
|
|
|
}
|
|
|
|
|
2006-04-15 22:45:02 +00:00
|
|
|
static void ShowStackRM(HANDLE hThread, CONTEXT& Context)
|
2004-02-06 03:12:30 +00:00
|
|
|
{
|
2006-04-08 20:22:08 +00:00
|
|
|
BOOL bRetVal = FALSE;
|
2004-02-06 03:12:30 +00:00
|
|
|
|
2006-08-24 07:02:52 +00:00
|
|
|
int frameNum = 0;
|
|
|
|
DWORD64 offsetFromSymbol = 0;
|
|
|
|
DWORD offsetFromLine = 0;
|
2006-06-09 06:45:28 +00:00
|
|
|
char undName[MAX_SYM_NAME];
|
|
|
|
|
|
|
|
char szMsgSymFromAddr[256];
|
|
|
|
char szMsgSymGetLineFromAddr[256];
|
|
|
|
char szMsgSymGetModuleInfo[256];
|
2004-02-06 03:12:30 +00:00
|
|
|
|
2006-04-08 20:22:08 +00:00
|
|
|
IMAGEHLP_MODULE64 Module;
|
|
|
|
IMAGEHLP_LINE64 Line;
|
|
|
|
STACKFRAME64 StackFrame;
|
2004-02-06 03:12:30 +00:00
|
|
|
|
2011-07-14 15:54:52 +00:00
|
|
|
ULONG64 SymbolBuffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME + 1];
|
2006-04-09 23:06:18 +00:00
|
|
|
PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)SymbolBuffer;
|
|
|
|
|
|
|
|
|
2006-04-08 20:22:08 +00:00
|
|
|
if (g_bInitialized == FALSE)
|
2004-02-06 03:12:30 +00:00
|
|
|
{
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "Stackwalker not initialized (or was not able to initialize)!\n");
|
2006-04-08 20:22:08 +00:00
|
|
|
return;
|
2004-02-06 03:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-04-08 20:22:08 +00:00
|
|
|
// Critical section begin...
|
|
|
|
EnterCriticalSection(&g_csFileOpenClose);
|
|
|
|
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "- Registers -\n");
|
2006-04-08 20:22:08 +00:00
|
|
|
|
|
|
|
// Dump the Context data
|
2007-04-03 14:33:17 +00:00
|
|
|
#if defined(_WIN64) && defined(_M_X64)
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"rax=%.16x rbx=%.16x rcx=%.16x rdx=%.16x rsi=%.16x rdi=%.16x\n",
|
2007-04-03 14:33:17 +00:00
|
|
|
Context.Rax, Context.Rbx, Context.Rcx, Context.Rdx, Context.Rsi, Context.Rdi
|
|
|
|
);
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"r8=%.16x r9=%.16x r10=%.16x r11=%.16x r12=%.16x r13=%.16x\n",
|
2007-04-03 14:33:17 +00:00
|
|
|
Context.R8, Context.R9, Context.R10, Context.R11, Context.R12, Context.R13
|
|
|
|
);
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"r14=%.16x r15=%.16x rip=%.16x rsp=%.16x rbp=%.16x\n",
|
2007-04-03 14:33:17 +00:00
|
|
|
Context.R14, Context.R15, Context.Rip, Context.Rsp, Context.Rbp
|
|
|
|
);
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"cs=%.4x ss=%.4x ds=%.4x es=%.4x fs=%.4x gs=%.4x efl=%.8x\n\n",
|
2007-04-03 14:33:17 +00:00
|
|
|
Context.SegCs, Context.SegSs, Context.SegDs, Context.SegEs, Context.SegFs, Context.SegGs, Context.EFlags
|
|
|
|
);
|
|
|
|
#else
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"eax=%.8x ebx=%.8x ecx=%.8x edx=%.8x esi=%.8x edi=%.8x\n",
|
2006-04-08 20:22:08 +00:00
|
|
|
Context.Eax, Context.Ebx, Context.Ecx, Context.Edx, Context.Esi, Context.Edi
|
|
|
|
);
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"eip=%.8x esp=%.8x ebp=%.8x\n",
|
2006-04-08 20:22:08 +00:00
|
|
|
Context.Eip, Context.Esp, Context.Ebp
|
|
|
|
);
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"cs=%.4x ss=%.4x ds=%.4x es=%.4x fs=%.4x gs=%.4x efl=%.8x\n\n",
|
2006-04-08 20:22:08 +00:00
|
|
|
Context.SegCs, Context.SegSs, Context.SegDs, Context.SegEs, Context.SegFs, Context.SegGs, Context.EFlags
|
|
|
|
);
|
2007-04-03 14:33:17 +00:00
|
|
|
#endif
|
2006-04-08 20:22:08 +00:00
|
|
|
|
|
|
|
// Stack Header
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "- Callstack -\n");
|
|
|
|
fprintf(stderr, "ChildEBP RetAddr Args to Child\n");
|
2006-04-08 20:22:08 +00:00
|
|
|
fflush( stderr );
|
|
|
|
|
|
|
|
|
|
|
|
// init STACKFRAME for first call
|
|
|
|
// Notes: AddrModeFlat is just an assumption. I hate VDM debugging.
|
|
|
|
// Notes: will have to be #ifdef-ed for Alphas; MIPSes are dead anyway,
|
|
|
|
// and good riddance.
|
|
|
|
memset( &StackFrame, '\0', sizeof(STACKFRAME64) );
|
2007-04-13 06:13:46 +00:00
|
|
|
#if defined(_WIN64) && defined(_M_X64)
|
|
|
|
StackFrame.AddrPC.Offset = Context.Rip;
|
|
|
|
StackFrame.AddrPC.Mode = AddrModeFlat;
|
|
|
|
StackFrame.AddrFrame.Offset = Context.Rbp;
|
|
|
|
StackFrame.AddrFrame.Mode = AddrModeFlat;
|
|
|
|
#else
|
|
|
|
StackFrame.AddrPC.Offset = Context.Eip;
|
2006-04-08 20:22:08 +00:00
|
|
|
StackFrame.AddrPC.Mode = AddrModeFlat;
|
|
|
|
StackFrame.AddrFrame.Offset = Context.Ebp;
|
|
|
|
StackFrame.AddrFrame.Mode = AddrModeFlat;
|
2007-04-13 06:13:46 +00:00
|
|
|
#endif
|
2006-04-08 20:22:08 +00:00
|
|
|
|
2007-04-13 06:13:46 +00:00
|
|
|
memset( pSymbol, '\0', sizeof(SymbolBuffer) );
|
2006-04-09 23:06:18 +00:00
|
|
|
pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO);
|
|
|
|
pSymbol->MaxNameLen = MAX_SYM_NAME;
|
2006-04-08 20:22:08 +00:00
|
|
|
|
|
|
|
memset( &Line, '\0', sizeof(IMAGEHLP_LINE64) );
|
|
|
|
Line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
|
|
|
|
|
|
|
|
memset( &Module, '\0', sizeof(IMAGEHLP_MODULE64) );
|
|
|
|
Module.SizeOfStruct = sizeof(IMAGEHLP_MODULE64);
|
|
|
|
|
2016-02-15 22:54:13 +00:00
|
|
|
safe_strcpy(szMsgSymFromAddr, "");
|
|
|
|
safe_strcpy(szMsgSymGetLineFromAddr, "");
|
|
|
|
safe_strcpy(szMsgSymGetModuleInfo, "");
|
2006-06-09 06:45:28 +00:00
|
|
|
|
|
|
|
|
2006-04-08 20:22:08 +00:00
|
|
|
for ( frameNum = 0; ; ++frameNum )
|
2004-02-06 03:12:30 +00:00
|
|
|
{
|
2006-04-08 20:22:08 +00:00
|
|
|
// get next stack frame (StackWalk(), SymFunctionTableAccess(), SymGetModuleBase())
|
|
|
|
// if this returns ERROR_INVALID_ADDRESS (487) or ERROR_NOACCESS (998), you can
|
|
|
|
// assume that either you are done, or that the stack is so hosed that the next
|
|
|
|
// deeper frame could not be found.
|
2007-04-13 06:13:46 +00:00
|
|
|
bRetVal = pSW(
|
2011-07-14 15:54:52 +00:00
|
|
|
#if defined(_WIN64) && defined(_M_X64)
|
2007-04-13 06:13:46 +00:00
|
|
|
IMAGE_FILE_MACHINE_AMD64,
|
|
|
|
#else
|
2006-04-08 20:22:08 +00:00
|
|
|
IMAGE_FILE_MACHINE_I386,
|
2011-07-14 15:54:52 +00:00
|
|
|
#endif
|
2006-04-15 22:45:02 +00:00
|
|
|
g_hProcess,
|
2006-04-08 20:22:08 +00:00
|
|
|
hThread,
|
|
|
|
&StackFrame,
|
|
|
|
&Context,
|
|
|
|
NULL,
|
|
|
|
(PFUNCTION_TABLE_ACCESS_ROUTINE64)pSFTA,
|
|
|
|
(PGET_MODULE_BASE_ROUTINE64)pSGMB,
|
|
|
|
NULL
|
|
|
|
);
|
2011-07-14 15:54:52 +00:00
|
|
|
if (!bRetVal) {
|
2006-04-08 20:22:08 +00:00
|
|
|
break;
|
2011-07-14 15:54:52 +00:00
|
|
|
}
|
2004-02-06 03:12:30 +00:00
|
|
|
|
2006-04-08 20:22:08 +00:00
|
|
|
if ( StackFrame.AddrPC.Offset == 0 )
|
2004-02-06 03:12:30 +00:00
|
|
|
{
|
2006-04-08 20:22:08 +00:00
|
|
|
// Special case: If we are here, we have no valid callstack entry!
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "(-nosymbols- PC == 0)\n");
|
2004-02-06 03:12:30 +00:00
|
|
|
}
|
2006-04-08 20:22:08 +00:00
|
|
|
else
|
2004-02-06 03:12:30 +00:00
|
|
|
{
|
2006-04-08 20:22:08 +00:00
|
|
|
// show procedure info (SymFromAddr())
|
|
|
|
undName[0] = 0;
|
|
|
|
offsetFromSymbol = 0;
|
2006-04-15 22:45:02 +00:00
|
|
|
if ( !pSFA( g_hProcess, StackFrame.AddrPC.Offset, &offsetFromSymbol, pSymbol ) )
|
2004-02-06 03:12:30 +00:00
|
|
|
{
|
2006-04-08 20:22:08 +00:00
|
|
|
if ( gle != 487 )
|
2006-04-09 23:06:18 +00:00
|
|
|
{
|
2006-06-09 06:45:28 +00:00
|
|
|
snprintf(
|
|
|
|
szMsgSymFromAddr,
|
|
|
|
sizeof(szMsgSymFromAddr),
|
|
|
|
"SymFromAddr(): GetLastError = '%lu'",
|
|
|
|
gle
|
|
|
|
);
|
2006-04-09 23:06:18 +00:00
|
|
|
}
|
2004-02-06 03:12:30 +00:00
|
|
|
}
|
|
|
|
else
|
2006-04-08 20:22:08 +00:00
|
|
|
{
|
|
|
|
// UnDecorateSymbolName()
|
2006-04-09 23:06:18 +00:00
|
|
|
pUDSN( pSymbol->Name, undName, MAX_SYM_NAME, UNDNAME_NAME_ONLY );
|
2006-04-08 20:22:08 +00:00
|
|
|
}
|
2004-02-06 03:12:30 +00:00
|
|
|
|
2006-04-08 20:22:08 +00:00
|
|
|
// show line number info (SymGetLineFromAddr())
|
|
|
|
offsetFromLine = 0;
|
2006-04-15 22:45:02 +00:00
|
|
|
if ( !pSGLFA( g_hProcess, StackFrame.AddrPC.Offset, &offsetFromLine, &Line ) )
|
2006-04-08 20:22:08 +00:00
|
|
|
{
|
2006-06-09 06:45:28 +00:00
|
|
|
if ( (gle != 487) && (frameNum > 0) )
|
2006-04-08 20:22:08 +00:00
|
|
|
{
|
2006-06-09 06:45:28 +00:00
|
|
|
snprintf(
|
|
|
|
szMsgSymGetLineFromAddr,
|
|
|
|
sizeof(szMsgSymGetLineFromAddr),
|
|
|
|
"SymGetLineFromAddr(): GetLastError = '%lu'",
|
|
|
|
gle
|
|
|
|
);
|
2006-04-08 20:22:08 +00:00
|
|
|
}
|
|
|
|
}
|
2004-02-06 03:12:30 +00:00
|
|
|
|
2006-04-08 20:22:08 +00:00
|
|
|
// show module info (SymGetModuleInfo())
|
2006-04-15 22:45:02 +00:00
|
|
|
if ( !pSGMI( g_hProcess, StackFrame.AddrPC.Offset, &Module ) )
|
2004-02-06 03:12:30 +00:00
|
|
|
{
|
2006-06-09 06:45:28 +00:00
|
|
|
snprintf(
|
|
|
|
szMsgSymGetModuleInfo,
|
|
|
|
sizeof(szMsgSymGetModuleInfo),
|
|
|
|
"SymGetModuleInfo(): GetLastError = '%lu'",
|
|
|
|
gle
|
|
|
|
);
|
2004-02-06 03:12:30 +00:00
|
|
|
}
|
2006-04-08 20:22:08 +00:00
|
|
|
} // we seem to have a valid PC
|
|
|
|
|
|
|
|
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "%.8x ", StackFrame.AddrFrame.Offset);
|
|
|
|
fprintf(stderr, "%.8x ", StackFrame.AddrReturn.Offset);
|
|
|
|
fprintf(stderr, "%.8x ", StackFrame.Params[0]);
|
|
|
|
fprintf(stderr, "%.8x ", StackFrame.Params[1]);
|
|
|
|
fprintf(stderr, "%.8x ", StackFrame.Params[2]);
|
|
|
|
fprintf(stderr, "%.8x ", StackFrame.Params[3]);
|
|
|
|
fprintf(stderr, "%s", Module.ModuleName);
|
|
|
|
fprintf(stderr, "!%s+", undName);
|
|
|
|
fprintf(stderr, "0x%x ", offsetFromLine);
|
2006-04-08 20:22:08 +00:00
|
|
|
|
2011-07-14 15:54:52 +00:00
|
|
|
if (Line.LineNumber) {
|
|
|
|
fprintf(stderr, "(%s:%lu) ", Line.FileName, Line.LineNumber);
|
|
|
|
}
|
2006-04-08 20:22:08 +00:00
|
|
|
|
|
|
|
if (StackFrame.FuncTableEntry) {
|
|
|
|
// FPO Data
|
|
|
|
PFPO_DATA pFPO = (PFPO_DATA)StackFrame.FuncTableEntry;
|
|
|
|
switch(pFPO->cbFrame) {
|
|
|
|
case FRAME_FPO:
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "FPO: [%d,%d,%d] ", pFPO->cdwParams, pFPO->cdwLocals, pFPO->cbRegs);
|
2006-04-08 20:22:08 +00:00
|
|
|
break;
|
|
|
|
case FRAME_TRAP:
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "FPO: [%d,%d] TrapFrame @ 0x%.8x ", pFPO->cdwParams, pFPO->cdwLocals, pFPO->ulOffStart);
|
2006-04-08 20:22:08 +00:00
|
|
|
break;
|
|
|
|
case FRAME_TSS:
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "FPO: TaskGate Segment: 0 ");
|
2006-04-08 20:22:08 +00:00
|
|
|
break;
|
2004-02-06 03:12:30 +00:00
|
|
|
}
|
2006-04-08 20:22:08 +00:00
|
|
|
}
|
|
|
|
|
2006-06-09 06:45:28 +00:00
|
|
|
if (strlen(szMsgSymFromAddr) || strlen(szMsgSymGetLineFromAddr) || strlen(szMsgSymGetModuleInfo)) {
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(
|
2006-06-09 06:45:28 +00:00
|
|
|
stderr,
|
2011-07-14 15:54:52 +00:00
|
|
|
"%s %s %s Address = '%.8x'",
|
2006-06-09 06:45:28 +00:00
|
|
|
szMsgSymFromAddr,
|
|
|
|
szMsgSymGetLineFromAddr,
|
|
|
|
szMsgSymGetModuleInfo,
|
|
|
|
StackFrame.AddrPC.Offset
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "\n");
|
2004-02-06 03:12:30 +00:00
|
|
|
|
|
|
|
|
2006-04-08 20:22:08 +00:00
|
|
|
// Zero out params so we have fresh parameters through the next interation
|
2008-01-31 18:34:51 +00:00
|
|
|
StackFrame.Params[0] = (DWORD64)NULL;
|
|
|
|
StackFrame.Params[1] = (DWORD64)NULL;
|
|
|
|
StackFrame.Params[2] = (DWORD64)NULL;
|
|
|
|
StackFrame.Params[3] = (DWORD64)NULL;
|
2004-02-06 03:12:30 +00:00
|
|
|
|
|
|
|
|
2006-04-08 20:22:08 +00:00
|
|
|
// no return address means no deeper stackframe
|
|
|
|
if ( StackFrame.AddrReturn.Offset == 0 )
|
|
|
|
{
|
|
|
|
// avoid misunderstandings in the printf() following the loop
|
|
|
|
SetLastError( 0 );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} // for ( frameNum )
|
|
|
|
|
2006-04-09 06:50:18 +00:00
|
|
|
switch(gle){
|
|
|
|
case ERROR_SUCCESS:
|
|
|
|
break;
|
|
|
|
case ERROR_INVALID_ADDRESS:
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "\nStackWalk(): ERROR_INVALID_ADDRESS (%lu) - Possible stack corruption.\n", gle );
|
2006-04-09 06:50:18 +00:00
|
|
|
break;
|
|
|
|
case ERROR_NOACCESS:
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "\nStackWalk(): ERROR_NOACCESS (%lu) - Possible stack corruption.\n", gle );
|
2006-04-09 06:50:18 +00:00
|
|
|
break;
|
|
|
|
default:
|
2011-07-14 15:54:52 +00:00
|
|
|
fprintf(stderr, "\nStackWalk(): GetLastError = %lu\n", gle );
|
2006-04-09 06:50:18 +00:00
|
|
|
break;
|
2006-04-08 23:45:57 +00:00
|
|
|
}
|
2006-04-08 20:22:08 +00:00
|
|
|
|
|
|
|
fflush(stderr);
|
|
|
|
|
|
|
|
LeaveCriticalSection(&g_csFileOpenClose);
|
|
|
|
}
|
2004-12-08 00:40:19 +00:00
|
|
|
|