mirror of
https://github.com/TorqueGameEngines/Torque3D.git
synced 2026-07-13 07:34:45 +00:00
Engine directory for ticket #1
This commit is contained in:
parent
352279af7a
commit
7dbfe6994d
3795 changed files with 1363358 additions and 0 deletions
337
Engine/source/platformWin32/minidump/winMiniDump.cpp
Normal file
337
Engine/source/platformWin32/minidump/winMiniDump.cpp
Normal file
|
|
@ -0,0 +1,337 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// Copyright (c) 2012 GarageGames, LLC
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to
|
||||
// deal in the Software without restriction, including without limitation the
|
||||
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
// sell copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
// IN THE SOFTWARE.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#if defined( TORQUE_MINIDUMP ) && defined( TORQUE_RELEASE )
|
||||
|
||||
#include "platformWin32/platformWin32.h"
|
||||
#include "platformWin32/minidump/winStackWalker.h"
|
||||
#include "core/fileio.h"
|
||||
#include "core/strings/stringFunctions.h"
|
||||
#include "console/console.h"
|
||||
#include "app/net/serverQuery.h"
|
||||
|
||||
#pragma pack(push,8)
|
||||
#include <DbgHelp.h>
|
||||
#include <time.h>
|
||||
#pragma pack(pop)
|
||||
|
||||
#pragma comment(lib, "dbghelp.lib")
|
||||
|
||||
extern Win32PlatState winState;
|
||||
|
||||
//Forward declarations for the dialog functions
|
||||
BOOL CALLBACK MiniDumpDialogProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam);
|
||||
LRESULT DisplayMiniDumpDialog(HINSTANCE hinst, HWND hwndOwner);
|
||||
LPWORD lpwAlign(LPWORD lpIn);
|
||||
char gUserInput[4096];
|
||||
|
||||
//Console variables
|
||||
extern StringTableEntry gMiniDumpDir;
|
||||
extern StringTableEntry gMiniDumpUser;
|
||||
extern StringTableEntry gMiniDumpExec;
|
||||
extern StringTableEntry gMiniDumpParams;
|
||||
extern StringTableEntry gMiniDumpExecDir;
|
||||
|
||||
|
||||
char* dStrrstr(char* dst, const char* src, const char* findStr, char* replaceStr)
|
||||
{
|
||||
//see if str contains findStr, if not then return
|
||||
const char* findpos = strstr(src, findStr);
|
||||
if(!findpos)
|
||||
{
|
||||
strcpy(dst, src);
|
||||
}
|
||||
else
|
||||
{
|
||||
//copy the new string to the buffer
|
||||
dst[0]='\0';
|
||||
strncat(dst, src, findpos-src);
|
||||
strcat(dst, replaceStr);
|
||||
const char* cur = findpos + strlen(findStr);
|
||||
strcat(dst, cur);
|
||||
}
|
||||
|
||||
return dst;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
// CreateMiniDump()
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
INT CreateMiniDump( LPEXCEPTION_POINTERS ExceptionInfo)
|
||||
{
|
||||
//Get any information we can from the user and store it in gUserInput
|
||||
try
|
||||
{
|
||||
while(ShowCursor(TRUE) < 0);
|
||||
DisplayMiniDumpDialog(winState.appInstance, winState.appWindow);
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
//dSprintf(gUserInput, 4096, "The user could not enter a description of what was occurring.\n\n\n");
|
||||
}
|
||||
|
||||
//Build a Game, Date and Time stamped MiniDump folder
|
||||
time_t theTime;
|
||||
time(&theTime);
|
||||
tm* pLocalTime = localtime(&theTime);
|
||||
char crashFolder[2048];
|
||||
dSprintf(crashFolder, 2048, "%s_%02d.%02d_%02d.%02d.%02d",
|
||||
Platform::getExecutableName(),
|
||||
pLocalTime->tm_mon+1, pLocalTime->tm_mday,
|
||||
pLocalTime->tm_hour, pLocalTime->tm_min, pLocalTime->tm_sec);
|
||||
|
||||
//Builed the fully qualified MiniDump path
|
||||
char crashPath[2048];
|
||||
char fileName[2048];
|
||||
if(gMiniDumpDir==NULL)
|
||||
{
|
||||
dSprintf(crashPath, 2048, "%s/MiniDump/%s", Platform::getCurrentDirectory(), crashFolder);
|
||||
}
|
||||
else
|
||||
{
|
||||
dSprintf(crashPath, 2048, "%s/%s", gMiniDumpDir, crashFolder);
|
||||
}
|
||||
|
||||
dSprintf(fileName, 2048, "%s/Minidump.dmp",crashPath);
|
||||
if (!Platform::createPath (fileName))return false; //create the directory
|
||||
|
||||
//Save the minidump
|
||||
File fileObject;
|
||||
if(fileObject.open(fileName, File::Write) == File::Ok)
|
||||
{
|
||||
MINIDUMP_EXCEPTION_INFORMATION DumpExceptionInfo;
|
||||
DumpExceptionInfo.ThreadId = GetCurrentThreadId();
|
||||
DumpExceptionInfo.ExceptionPointers = ExceptionInfo;
|
||||
DumpExceptionInfo.ClientPointers = true;
|
||||
MiniDumpWriteDump( GetCurrentProcess(), GetCurrentProcessId(), (HANDLE)fileObject.getHandle(), MiniDumpNormal, &DumpExceptionInfo, NULL, NULL );
|
||||
fileObject.close();
|
||||
}
|
||||
|
||||
//copy over the log file
|
||||
char fromFile[2048];
|
||||
dSprintf(fromFile, 2048, "%s/%s", Platform::getCurrentDirectory(), "console.log" );
|
||||
dSprintf(fileName, 2048, "%s/console.log", crashPath);
|
||||
Con::setLogMode(3); //ensure that the log file is closed (so it can be copied)
|
||||
dPathCopy(fromFile, fileName, true);
|
||||
|
||||
//copy over the exe file
|
||||
char exeName[1024];
|
||||
dSprintf(exeName, 1024, Platform::getExecutableName());
|
||||
exeName[dStrlen(exeName)-4]=0;
|
||||
dSprintf(fromFile, 2048, "%s/%s.dll", Platform::getCurrentDirectory(), exeName );
|
||||
dSprintf(fileName, 2048, "%s/%s.dll", crashPath, exeName );
|
||||
dPathCopy(fromFile, fileName, true);
|
||||
|
||||
//copy over the pdb file
|
||||
char pdbName[1024];
|
||||
dStrcpy(pdbName, exeName);
|
||||
dStrncat(pdbName, ".pdb", 4);
|
||||
dSprintf(fromFile, 2048, "%s/%s", Platform::getCurrentDirectory(), pdbName );
|
||||
dSprintf(fileName, 2048, "%s/%s", crashPath, pdbName );
|
||||
dPathCopy(fromFile, fileName, true);
|
||||
|
||||
//save the call stack
|
||||
char traceBuffer[65536];
|
||||
traceBuffer[0] = 0;
|
||||
dGetStackTrace( traceBuffer, *static_cast<const CONTEXT *>(ExceptionInfo->ContextRecord) );
|
||||
|
||||
//save the user input and the call stack to a file
|
||||
char crashlogFile[2048];
|
||||
dSprintf(crashlogFile, 2048, "%s/crash.log", crashPath);
|
||||
if(fileObject.open(crashlogFile, File::Write) == File::Ok)
|
||||
{
|
||||
fileObject.write(strlen(gUserInput), gUserInput);
|
||||
fileObject.write(strlen(traceBuffer), traceBuffer);
|
||||
fileObject.close();
|
||||
}
|
||||
|
||||
//call the external program indicated in script
|
||||
if(gMiniDumpExec!= NULL)
|
||||
{
|
||||
//replace special variables in gMiniDumpParams
|
||||
if(gMiniDumpParams)
|
||||
{
|
||||
char updateParams[4096];
|
||||
char finalParams[4096];
|
||||
dStrrstr(finalParams, gMiniDumpParams, "%crashpath%", crashPath);
|
||||
dStrrstr(updateParams, finalParams, "%crashfolder%", crashFolder);
|
||||
dStrrstr(finalParams, updateParams, "%crashlog%", crashlogFile);
|
||||
ShellExecuteA(NULL, "", gMiniDumpExec, finalParams, gMiniDumpExecDir ? gMiniDumpExecDir : "", SW_SHOWNORMAL);
|
||||
}
|
||||
else
|
||||
{
|
||||
ShellExecuteA(NULL, "", gMiniDumpExec, "", gMiniDumpExecDir ? gMiniDumpExecDir : "", SW_SHOWNORMAL);
|
||||
}
|
||||
}
|
||||
|
||||
return EXCEPTION_EXECUTE_HANDLER;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
// MiniDumpDialogProc - Used By DisplayMiniDumpDialog
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
const int ID_TEXT=200;
|
||||
const int ID_USERTEXT=300;
|
||||
const int ID_DONE=400;
|
||||
BOOL CALLBACK MiniDumpDialogProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam)
|
||||
{
|
||||
char text[128]= "";
|
||||
|
||||
switch (message)
|
||||
{
|
||||
case WM_INITDIALOG :
|
||||
SetDlgItemTextA ( hwndDlg, ID_USERTEXT, text );
|
||||
return TRUE ;
|
||||
|
||||
case WM_COMMAND:
|
||||
switch (LOWORD(wParam))
|
||||
{
|
||||
case ID_DONE:
|
||||
if( !GetDlgItemTextA(hwndDlg, ID_USERTEXT, gUserInput, 4096) ) gUserInput[0]='\0';
|
||||
strcat(gUserInput, "\n\n\n");
|
||||
EndDialog(hwndDlg, wParam);
|
||||
return TRUE;
|
||||
default:
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
// Helper function to DWORD align the Dialog Box components (Used in DisplayMiniDumpDialog()
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
LPWORD lpwAlign(LPWORD lpIn)
|
||||
{
|
||||
ULONG ul;
|
||||
|
||||
ul = (ULONG)lpIn;
|
||||
ul ++;
|
||||
ul >>=1;
|
||||
ul <<=1;
|
||||
return (LPWORD)ul;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
// Create the Dialog Box to get input from the user
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
LRESULT DisplayMiniDumpDialog(HINSTANCE hinst, HWND hwndOwner)
|
||||
{
|
||||
HGLOBAL hgbl = GlobalAlloc(GMEM_ZEROINIT, 1024);
|
||||
if (!hgbl) return -1;
|
||||
|
||||
//-----------------------------------------------------------------
|
||||
// Define the dialog box
|
||||
//-----------------------------------------------------------------
|
||||
LPDLGTEMPLATE lpdt = (LPDLGTEMPLATE)GlobalLock(hgbl);
|
||||
lpdt->style = WS_POPUP | WS_BORDER | DS_MODALFRAME | WS_CAPTION;
|
||||
lpdt->cdit = 3; // Number of controls
|
||||
lpdt->x = 100;
|
||||
lpdt->y = 100;
|
||||
lpdt->cx = 300;
|
||||
lpdt->cy = 90;
|
||||
|
||||
LPWORD lpw = (LPWORD)(lpdt + 1);
|
||||
*lpw++ = 0; // No menu
|
||||
*lpw++ = 0; // Predefined dialog box class (by default)
|
||||
|
||||
LPWSTR lpwsz = (LPWSTR)lpw;
|
||||
int nchar = 1 + MultiByteToWideChar(CP_ACP, 0, "MiniDump Crash Report", -1, lpwsz, 50);
|
||||
lpw += nchar;
|
||||
|
||||
//-----------------------------------------------------------------
|
||||
// Define a static text message
|
||||
//-----------------------------------------------------------------
|
||||
lpw = lpwAlign(lpw); // Align DLGITEMTEMPLATE on DWORD boundary
|
||||
LPDLGITEMTEMPLATE lpdit = (LPDLGITEMTEMPLATE)lpw;
|
||||
lpdit->x = 10;
|
||||
lpdit->y = 10;
|
||||
lpdit->cx = 290;
|
||||
lpdit->cy = 10;
|
||||
lpdit->id = ID_TEXT; // Text identifier
|
||||
lpdit->style = WS_CHILD | WS_VISIBLE | SS_LEFT;
|
||||
|
||||
lpw = (LPWORD)(lpdit + 1);
|
||||
*lpw++ = 0xFFFF;
|
||||
*lpw++ = 0x0082; // Static class
|
||||
|
||||
LPSTR msg = "The program has crashed. Please describe what was happening:";
|
||||
for (lpwsz = (LPWSTR)lpw; *lpwsz++ = (WCHAR)*msg++;);
|
||||
lpw = (LPWORD)lpwsz;
|
||||
|
||||
*lpw++ = 0; // No creation data
|
||||
|
||||
//-----------------------------------------------------------------
|
||||
// Define a DONE button
|
||||
//-----------------------------------------------------------------
|
||||
lpw = lpwAlign(lpw); // Align DLGITEMTEMPLATE on DWORD boundary
|
||||
lpdit = (LPDLGITEMTEMPLATE)lpw;
|
||||
lpdit->x = 265;
|
||||
lpdit->y = 75;
|
||||
lpdit->cx = 25;
|
||||
lpdit->cy = 12;
|
||||
lpdit->id = ID_DONE; // OK button identifier
|
||||
lpdit->style = WS_CHILD | WS_VISIBLE | WS_TABSTOP;// | BS_DEFPUSHBUTTON;
|
||||
|
||||
lpw = (LPWORD)(lpdit + 1);
|
||||
*lpw++ = 0xFFFF;
|
||||
*lpw++ = 0x0080; // Button class
|
||||
|
||||
lpwsz = (LPWSTR)lpw;
|
||||
nchar = 1 + MultiByteToWideChar(CP_ACP, 0, "Done", -1, lpwsz, 50);
|
||||
lpw += nchar;
|
||||
*lpw++ = 0; // No creation data
|
||||
|
||||
//-----------------------------------------------------------------
|
||||
// Define a text entry message
|
||||
//-----------------------------------------------------------------
|
||||
lpw = lpwAlign(lpw); // Align DLGITEMTEMPLATE on DWORD boundary
|
||||
lpdit = (LPDLGITEMTEMPLATE)lpw;
|
||||
lpdit->x = 10;
|
||||
lpdit->y = 22;
|
||||
lpdit->cx = 280;
|
||||
lpdit->cy = 50;
|
||||
lpdit->id = ID_USERTEXT; // Text identifier
|
||||
lpdit->style = ES_LEFT | WS_BORDER | WS_TABSTOP | WS_CHILD | WS_VISIBLE;
|
||||
|
||||
lpw = (LPWORD)(lpdit + 1);
|
||||
*lpw++ = 0xFFFF;
|
||||
*lpw++ = 0x0081; // Text edit class
|
||||
|
||||
*lpw++ = 0; // No creation data
|
||||
|
||||
|
||||
|
||||
GlobalUnlock(hgbl);
|
||||
LRESULT ret = DialogBoxIndirect( hinst,
|
||||
(LPDLGTEMPLATE)hgbl,
|
||||
hwndOwner,
|
||||
(DLGPROC)MiniDumpDialogProc);
|
||||
GlobalFree(hgbl);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif
|
||||
825
Engine/source/platformWin32/minidump/winStackWalker.cpp
Normal file
825
Engine/source/platformWin32/minidump/winStackWalker.cpp
Normal file
|
|
@ -0,0 +1,825 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// Copyright (c) 2012 GarageGames, LLC
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to
|
||||
// deal in the Software without restriction, including without limitation the
|
||||
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
// sell copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
// IN THE SOFTWARE.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#if defined( TORQUE_MINIDUMP ) && defined( TORQUE_RELEASE )
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
// Sourced from http://www.codeproject.com/threads/StackWalker.asp
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
#include "winStackWalker.h"
|
||||
|
||||
#undef UNICODE
|
||||
|
||||
#include <tchar.h>
|
||||
#include <Tlhelp32.h>
|
||||
#include <stdio.h>
|
||||
#include <psapi.h>
|
||||
|
||||
#pragma comment(lib, "version.lib") // for "VerQueryValue"
|
||||
#pragma comment(lib, "dbghelp.lib")
|
||||
#pragma comment(lib, "psapi.lib")
|
||||
|
||||
|
||||
// secure-CRT_functions are only available starting with VC8
|
||||
#if _MSC_VER < 1400
|
||||
#define strcpy_s strcpy
|
||||
#define strcat_s(dst, len, src) strcat(dst, src)
|
||||
#define _snprintf_s _snprintf
|
||||
#define _tcscat_s _tcscat
|
||||
#endif
|
||||
|
||||
// Entry for each Callstack-Entry
|
||||
const int STACKWALK_MAX_NAMELEN = 1024; // max name length for symbols
|
||||
struct CallstackEntry
|
||||
{
|
||||
DWORD64 offset; // if 0, we have no valid entry
|
||||
char name[STACKWALK_MAX_NAMELEN];
|
||||
char undName[STACKWALK_MAX_NAMELEN];
|
||||
char undFullName[STACKWALK_MAX_NAMELEN];
|
||||
DWORD64 offsetFromSmybol;
|
||||
DWORD64 offsetFromLine;
|
||||
DWORD lineNumber;
|
||||
char lineFileName[STACKWALK_MAX_NAMELEN];
|
||||
DWORD symType;
|
||||
const char * symTypeString;
|
||||
char moduleName[STACKWALK_MAX_NAMELEN];
|
||||
DWORD64 baseOfImage;
|
||||
char loadedImageName[STACKWALK_MAX_NAMELEN];
|
||||
};
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
// Implementation of platform functions
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
void dGetStackTrace(char * traceBuffer, CONTEXT const & ContextRecord)
|
||||
{
|
||||
StackWalker sw;
|
||||
sw.setOutputBuffer(traceBuffer);
|
||||
sw.ShowCallstack(GetCurrentThread(), ContextRecord);
|
||||
sw.setOutputBuffer(NULL);
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
//Constructor
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
StackWalker::StackWalker(DWORD options, LPCSTR szSymPath)
|
||||
: m_dwProcessId(GetCurrentProcessId()),
|
||||
m_hProcess(GetCurrentProcess()),
|
||||
m_options(options),
|
||||
m_modulesLoaded(false),
|
||||
m_pOutputBuffer(NULL)
|
||||
{
|
||||
if (szSymPath != NULL)
|
||||
{
|
||||
m_szSymPath = _strdup(szSymPath);
|
||||
m_options |= SymBuildPath;
|
||||
}
|
||||
else
|
||||
m_szSymPath = NULL;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
//Destructor
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
StackWalker::~StackWalker()
|
||||
{
|
||||
SymCleanup(m_hProcess);
|
||||
if (m_szSymPath != NULL) free(m_szSymPath);
|
||||
m_szSymPath = NULL;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
//setOutputBuffer - Points the StackWalker at a buffer where it will write out any output. If this is set to NULL then the output
|
||||
// will only be sent to DebugString
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
void StackWalker::setOutputBuffer(char * buffer)
|
||||
{
|
||||
m_pOutputBuffer = buffer;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
//OnOutput
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
void StackWalker::OnOutput(LPCSTR buffer)
|
||||
{
|
||||
OutputDebugStringA(buffer);
|
||||
if(m_pOutputBuffer) strcat(m_pOutputBuffer, buffer);
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
//Init
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
bool StackWalker::Init(LPCSTR szSymPath)
|
||||
{
|
||||
// SymInitialize
|
||||
if (szSymPath != NULL) m_szSymPath = _strdup(szSymPath);
|
||||
if (SymInitialize(m_hProcess, m_szSymPath, FALSE) == FALSE)
|
||||
{
|
||||
this->OnDbgHelpErr("SymInitialize", GetLastError(), 0);
|
||||
}
|
||||
|
||||
DWORD symOptions = SymGetOptions();
|
||||
symOptions |= SYMOPT_LOAD_LINES;
|
||||
symOptions |= SYMOPT_FAIL_CRITICAL_ERRORS;
|
||||
symOptions = SymSetOptions(symOptions);
|
||||
|
||||
char buf[STACKWALK_MAX_NAMELEN] = {0};
|
||||
if(SymGetSearchPath(m_hProcess, buf, STACKWALK_MAX_NAMELEN) == FALSE)
|
||||
{
|
||||
this->OnDbgHelpErr("SymGetSearchPath", GetLastError(), 0);
|
||||
}
|
||||
|
||||
char szUserName[1024] = {0};
|
||||
DWORD dwSize = 1024;
|
||||
GetUserNameA(szUserName, &dwSize);
|
||||
this->OnSymInit(buf, symOptions, szUserName);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
//LoadModules
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
bool StackWalker::LoadModules()
|
||||
{
|
||||
if (m_modulesLoaded) return true;
|
||||
|
||||
// Build the sym-path:
|
||||
char *szSymPath = NULL;
|
||||
if ( (m_options & SymBuildPath) != 0)
|
||||
{
|
||||
const size_t nSymPathLen = 4096;
|
||||
szSymPath = (char*) malloc(nSymPathLen);
|
||||
if (szSymPath == NULL)
|
||||
{
|
||||
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
||||
return false;
|
||||
}
|
||||
szSymPath[0] = 0;
|
||||
// Now first add the (optional) provided sympath:
|
||||
if (m_szSymPath != NULL)
|
||||
{
|
||||
strcat_s(szSymPath, nSymPathLen, m_szSymPath);
|
||||
strcat_s(szSymPath, nSymPathLen, ";");
|
||||
}
|
||||
|
||||
strcat_s(szSymPath, nSymPathLen, ".;");
|
||||
|
||||
const size_t nTempLen = 1024;
|
||||
CHAR szTemp[nTempLen];
|
||||
// Now add the current directory:
|
||||
if (GetCurrentDirectoryA(nTempLen, szTemp) > 0)
|
||||
{
|
||||
szTemp[nTempLen-1] = 0;
|
||||
strcat_s(szSymPath, nSymPathLen, szTemp);
|
||||
strcat_s(szSymPath, nSymPathLen, ";");
|
||||
}
|
||||
|
||||
// Now add the path for the main-module:
|
||||
if (GetModuleFileNameA(NULL, szTemp, nTempLen) > 0)
|
||||
{
|
||||
szTemp[nTempLen-1] = 0;
|
||||
for (char *p = (szTemp+strlen(szTemp)-1); p >= szTemp; --p)
|
||||
{
|
||||
// locate the rightmost path separator
|
||||
if ( (*p == '\\') || (*p == '/') || (*p == ':') )
|
||||
{
|
||||
*p = 0;
|
||||
break;
|
||||
}
|
||||
} // for (search for path separator...)
|
||||
if (strlen(szTemp) > 0)
|
||||
{
|
||||
strcat_s(szSymPath, nSymPathLen, szTemp);
|
||||
strcat_s(szSymPath, nSymPathLen, ";");
|
||||
}
|
||||
}
|
||||
if (GetEnvironmentVariableA("_NT_SYMBOL_PATH", szTemp, nTempLen) > 0)
|
||||
{
|
||||
szTemp[nTempLen-1] = 0;
|
||||
strcat_s(szSymPath, nSymPathLen, szTemp);
|
||||
strcat_s(szSymPath, nSymPathLen, ";");
|
||||
}
|
||||
if (GetEnvironmentVariableA("_NT_ALTERNATE_SYMBOL_PATH", szTemp, nTempLen) > 0)
|
||||
{
|
||||
szTemp[nTempLen-1] = 0;
|
||||
strcat_s(szSymPath, nSymPathLen, szTemp);
|
||||
strcat_s(szSymPath, nSymPathLen, ";");
|
||||
}
|
||||
if (GetEnvironmentVariableA("SYSTEMROOT", szTemp, nTempLen) > 0)
|
||||
{
|
||||
szTemp[nTempLen-1] = 0;
|
||||
strcat_s(szSymPath, nSymPathLen, szTemp);
|
||||
strcat_s(szSymPath, nSymPathLen, ";");
|
||||
// also add the "system32"-directory:
|
||||
strcat_s(szTemp, nTempLen, "\\system32");
|
||||
strcat_s(szSymPath, nSymPathLen, szTemp);
|
||||
strcat_s(szSymPath, nSymPathLen, ";");
|
||||
}
|
||||
|
||||
if ( (m_options & SymBuildPath) != 0 )
|
||||
{
|
||||
if (GetEnvironmentVariableA("SYSTEMDRIVE", szTemp, nTempLen) > 0)
|
||||
{
|
||||
szTemp[nTempLen-1] = 0;
|
||||
strcat_s(szSymPath, nSymPathLen, "SRV*");
|
||||
strcat_s(szSymPath, nSymPathLen, szTemp);
|
||||
strcat_s(szSymPath, nSymPathLen, "\\websymbols");
|
||||
strcat_s(szSymPath, nSymPathLen, "*http://msdl.microsoft.com/download/symbols;");
|
||||
}
|
||||
else
|
||||
strcat_s(szSymPath, nSymPathLen, "SRV*c:\\websymbols*http://msdl.microsoft.com/download/symbols;");
|
||||
}
|
||||
}
|
||||
|
||||
// First Init the whole stuff...
|
||||
bool bRet = Init(szSymPath);
|
||||
if (szSymPath != NULL)
|
||||
{
|
||||
free(szSymPath);
|
||||
szSymPath = NULL;
|
||||
}
|
||||
if (bRet == false)
|
||||
{
|
||||
this->OnDbgHelpErr("Error while initializing dbghelp.dll", 0, 0);
|
||||
SetLastError(ERROR_DLL_INIT_FAILED);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(GetModuleListTH32(m_hProcess, m_dwProcessId))
|
||||
{
|
||||
m_modulesLoaded = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
// then try psapi
|
||||
if(GetModuleListPSAPI(m_hProcess))
|
||||
{
|
||||
m_modulesLoaded = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
//LoadModule
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
DWORD StackWalker::LoadModule(HANDLE hProcess, LPCSTR img, LPCSTR mod, DWORD64 baseAddr, DWORD size)
|
||||
{
|
||||
CHAR *szImg = _strdup(img);
|
||||
CHAR *szMod = _strdup(mod);
|
||||
DWORD result = ERROR_SUCCESS;
|
||||
if ( (szImg == NULL) || (szMod == NULL) )
|
||||
result = ERROR_NOT_ENOUGH_MEMORY;
|
||||
else
|
||||
{
|
||||
if (SymLoadModule64(hProcess, 0, szImg, szMod, baseAddr, size) == 0)
|
||||
result = GetLastError();
|
||||
}
|
||||
ULONGLONG fileVersion = 0;
|
||||
if(szImg != NULL)
|
||||
{
|
||||
// try to retrieve the file-version:
|
||||
if ( (m_options & StackWalker::RetrieveFileVersion) != 0)
|
||||
{
|
||||
VS_FIXEDFILEINFO *fInfo = NULL;
|
||||
DWORD dwHandle;
|
||||
DWORD dwSize = GetFileVersionInfoSizeA(szImg, &dwHandle);
|
||||
if (dwSize > 0)
|
||||
{
|
||||
LPVOID vData = malloc(dwSize);
|
||||
if (vData != NULL)
|
||||
{
|
||||
if (GetFileVersionInfoA(szImg, dwHandle, dwSize, vData) != 0)
|
||||
{
|
||||
UINT len;
|
||||
char szSubBlock[] = _T("\\");
|
||||
if (VerQueryValueA(vData, szSubBlock, (LPVOID*) &fInfo, &len) == 0)
|
||||
fInfo = NULL;
|
||||
else
|
||||
{
|
||||
fileVersion = ((ULONGLONG)fInfo->dwFileVersionLS) + ((ULONGLONG)fInfo->dwFileVersionMS << 32);
|
||||
}
|
||||
}
|
||||
free(vData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Retrive some additional-infos about the module
|
||||
IMAGEHLP_MODULE64 Module;
|
||||
const char *szSymType = "-unknown-";
|
||||
if (this->GetModuleInfo(hProcess, baseAddr, &Module) != FALSE)
|
||||
{
|
||||
switch(Module.SymType)
|
||||
{
|
||||
case SymNone:
|
||||
szSymType = "-nosymbols-";
|
||||
break;
|
||||
case SymCoff:
|
||||
szSymType = "COFF";
|
||||
break;
|
||||
case SymCv:
|
||||
szSymType = "CV";
|
||||
break;
|
||||
case SymPdb:
|
||||
szSymType = "PDB";
|
||||
break;
|
||||
case SymExport:
|
||||
szSymType = "-exported-";
|
||||
break;
|
||||
case SymDeferred:
|
||||
szSymType = "-deferred-";
|
||||
break;
|
||||
case SymSym:
|
||||
szSymType = "SYM";
|
||||
break;
|
||||
case 8: //SymVirtual:
|
||||
szSymType = "Virtual";
|
||||
break;
|
||||
case 9: // SymDia:
|
||||
szSymType = "DIA";
|
||||
break;
|
||||
}
|
||||
}
|
||||
this->OnLoadModule(img, mod, baseAddr, size, result, szSymType, Module.LoadedImageName, fileVersion);
|
||||
}
|
||||
if (szImg != NULL) free(szImg);
|
||||
if (szMod != NULL) free(szMod);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// The following is used to pass the "userData"-Pointer to the user-provided readMemoryFunction
|
||||
// This has to be done due to a problem with the "hProcess"-parameter in x64...
|
||||
// Because this class is in no case multi-threading-enabled (because of the limitations of dbghelp.dll) it is "safe" to use a static-variable
|
||||
static StackWalker::PReadProcessMemoryRoutine s_readMemoryFunction = NULL;
|
||||
static LPVOID s_readMemoryFunction_UserData = NULL;
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
//ShowCallstack
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
bool StackWalker::ShowCallstack(HANDLE hThread, CONTEXT const & context, PReadProcessMemoryRoutine readMemoryFunction, LPVOID pUserData)
|
||||
{
|
||||
CONTEXT c = context;
|
||||
IMAGEHLP_SYMBOL64 *pSym = NULL;
|
||||
IMAGEHLP_MODULE64 Module;
|
||||
IMAGEHLP_LINE64 Line;
|
||||
int frameNum;
|
||||
|
||||
if (!m_modulesLoaded) LoadModules();
|
||||
|
||||
|
||||
s_readMemoryFunction = readMemoryFunction;
|
||||
s_readMemoryFunction_UserData = pUserData;
|
||||
|
||||
|
||||
|
||||
// init STACKFRAME for first call
|
||||
STACKFRAME64 s; // in/out stackframe
|
||||
memset(&s, 0, sizeof(s));
|
||||
DWORD imageType;
|
||||
#ifdef _M_IX86
|
||||
// normally, call ImageNtHeader() and use machine info from PE header
|
||||
imageType = IMAGE_FILE_MACHINE_I386;
|
||||
s.AddrPC.Offset = c.Eip;
|
||||
s.AddrPC.Mode = AddrModeFlat;
|
||||
s.AddrFrame.Offset = c.Ebp;
|
||||
s.AddrFrame.Mode = AddrModeFlat;
|
||||
s.AddrStack.Offset = c.Esp;
|
||||
s.AddrStack.Mode = AddrModeFlat;
|
||||
#elif _M_X64
|
||||
imageType = IMAGE_FILE_MACHINE_AMD64;
|
||||
s.AddrPC.Offset = c.Rip;
|
||||
s.AddrPC.Mode = AddrModeFlat;
|
||||
s.AddrFrame.Offset = c.Rsp;
|
||||
s.AddrFrame.Mode = AddrModeFlat;
|
||||
s.AddrStack.Offset = c.Rsp;
|
||||
s.AddrStack.Mode = AddrModeFlat;
|
||||
#elif _M_IA64
|
||||
imageType = IMAGE_FILE_MACHINE_IA64;
|
||||
s.AddrPC.Offset = c.StIIP;
|
||||
s.AddrPC.Mode = AddrModeFlat;
|
||||
s.AddrFrame.Offset = c.IntSp;
|
||||
s.AddrFrame.Mode = AddrModeFlat;
|
||||
s.AddrBStore.Offset = c.RsBSP;
|
||||
s.AddrBStore.Mode = AddrModeFlat;
|
||||
s.AddrStack.Offset = c.IntSp;
|
||||
s.AddrStack.Mode = AddrModeFlat;
|
||||
#else
|
||||
#error "Platform not supported!"
|
||||
#endif
|
||||
|
||||
pSym = (IMAGEHLP_SYMBOL64 *) malloc(sizeof(IMAGEHLP_SYMBOL64) + STACKWALK_MAX_NAMELEN);
|
||||
if (!pSym) goto cleanup; // not enough memory...
|
||||
memset(pSym, 0, sizeof(IMAGEHLP_SYMBOL64) + STACKWALK_MAX_NAMELEN);
|
||||
pSym->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL64);
|
||||
pSym->MaxNameLength = STACKWALK_MAX_NAMELEN;
|
||||
|
||||
memset(&Line, 0, sizeof(Line));
|
||||
Line.SizeOfStruct = sizeof(Line);
|
||||
|
||||
memset(&Module, 0, sizeof(Module));
|
||||
Module.SizeOfStruct = sizeof(Module);
|
||||
|
||||
for (frameNum = 0; ; ++frameNum )
|
||||
{
|
||||
// get next stack frame (StackWalk64(), SymFunctionTableAccess64(), SymGetModuleBase64())
|
||||
// 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.
|
||||
// CONTEXT need not to be suplied if imageTyp is IMAGE_FILE_MACHINE_I386!
|
||||
if ( ! StackWalk64(imageType, this->m_hProcess, hThread, &s, &c, myReadProcMem, SymFunctionTableAccess64, SymGetModuleBase64, NULL) )
|
||||
{
|
||||
this->OnDbgHelpErr("StackWalk64", GetLastError(), s.AddrPC.Offset);
|
||||
break;
|
||||
}
|
||||
|
||||
CallstackEntry csEntry;
|
||||
csEntry.offset = s.AddrPC.Offset;
|
||||
csEntry.name[0] = 0;
|
||||
csEntry.undName[0] = 0;
|
||||
csEntry.undFullName[0] = 0;
|
||||
csEntry.offsetFromSmybol = 0;
|
||||
csEntry.offsetFromLine = 0;
|
||||
csEntry.lineFileName[0] = 0;
|
||||
csEntry.lineNumber = 0;
|
||||
csEntry.loadedImageName[0] = 0;
|
||||
csEntry.moduleName[0] = 0;
|
||||
if (s.AddrPC.Offset == s.AddrReturn.Offset)
|
||||
{
|
||||
OnDbgHelpErr("StackWalk64-Endless-Callstack!", 0, s.AddrPC.Offset);
|
||||
break;
|
||||
}
|
||||
if (s.AddrPC.Offset != 0)
|
||||
{
|
||||
// we seem to have a valid PC, show procedure info
|
||||
if (SymGetSymFromAddr64(m_hProcess, s.AddrPC.Offset, &(csEntry.offsetFromSmybol), pSym) != FALSE)
|
||||
{
|
||||
// TODO: Mache dies sicher...!
|
||||
strcpy_s(csEntry.name, pSym->Name);
|
||||
UnDecorateSymbolName( pSym->Name, csEntry.undName, STACKWALK_MAX_NAMELEN, UNDNAME_NAME_ONLY );
|
||||
UnDecorateSymbolName( pSym->Name, csEntry.undFullName, STACKWALK_MAX_NAMELEN, UNDNAME_COMPLETE );
|
||||
}
|
||||
else
|
||||
{
|
||||
this->OnDbgHelpErr("SymGetSymFromAddr64", GetLastError(), s.AddrPC.Offset);
|
||||
}
|
||||
|
||||
// show line number info, NT5.0-method
|
||||
if (SymGetLineFromAddr64(this->m_hProcess, s.AddrPC.Offset, (PDWORD)&(csEntry.offsetFromLine), &Line) != FALSE)
|
||||
{
|
||||
csEntry.lineNumber = Line.LineNumber;
|
||||
// TODO: Mache dies sicher...!
|
||||
strcpy_s(csEntry.lineFileName, Line.FileName);
|
||||
}
|
||||
else
|
||||
{
|
||||
this->OnDbgHelpErr("SymGetLineFromAddr64", GetLastError(), s.AddrPC.Offset);
|
||||
}
|
||||
|
||||
// show module info
|
||||
if( GetModuleInfo(this->m_hProcess, s.AddrPC.Offset, &Module) )
|
||||
{
|
||||
switch ( Module.SymType )
|
||||
{
|
||||
case SymNone:
|
||||
csEntry.symTypeString = "-nosymbols-";
|
||||
break;
|
||||
case SymCoff:
|
||||
csEntry.symTypeString = "COFF";
|
||||
break;
|
||||
case SymCv:
|
||||
csEntry.symTypeString = "CV";
|
||||
break;
|
||||
case SymPdb:
|
||||
csEntry.symTypeString = "PDB";
|
||||
break;
|
||||
case SymExport:
|
||||
csEntry.symTypeString = "-exported-";
|
||||
break;
|
||||
case SymDeferred:
|
||||
csEntry.symTypeString = "-deferred-";
|
||||
break;
|
||||
case SymSym:
|
||||
csEntry.symTypeString = "SYM";
|
||||
break;
|
||||
case SymDia:
|
||||
csEntry.symTypeString = "DIA";
|
||||
break;
|
||||
case SymVirtual:
|
||||
csEntry.symTypeString = "Virtual";
|
||||
break;
|
||||
default:
|
||||
//_snprintf( ty, sizeof ty, "symtype=%ld", (long) Module.SymType );
|
||||
csEntry.symTypeString = NULL;
|
||||
break;
|
||||
}
|
||||
|
||||
// TODO: Mache dies sicher...!
|
||||
strcpy_s(csEntry.moduleName, Module.ModuleName);
|
||||
csEntry.baseOfImage = Module.BaseOfImage;
|
||||
strcpy_s(csEntry.loadedImageName, Module.LoadedImageName);
|
||||
}
|
||||
else
|
||||
{
|
||||
OnDbgHelpErr("SymGetModuleInfo64", GetLastError(), s.AddrPC.Offset);
|
||||
}
|
||||
}
|
||||
|
||||
CallstackEntryType et = nextEntry;
|
||||
if (frameNum == 0) et = firstEntry;
|
||||
OnCallstackEntry(et, csEntry);
|
||||
|
||||
if (s.AddrReturn.Offset == 0)
|
||||
{
|
||||
OnCallstackEntry(lastEntry, csEntry);
|
||||
SetLastError(ERROR_SUCCESS);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
cleanup:
|
||||
if (pSym) free( pSym );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
BOOL __stdcall StackWalker::myReadProcMem(HANDLE hProcess, DWORD64 qwBaseAddress, PVOID lpBuffer, DWORD nSize, LPDWORD lpNumberOfBytesRead)
|
||||
{
|
||||
if (s_readMemoryFunction == NULL)
|
||||
{
|
||||
SIZE_T st;
|
||||
BOOL bRet = ReadProcessMemory(hProcess, (LPVOID) qwBaseAddress, lpBuffer, nSize, &st);
|
||||
*lpNumberOfBytesRead = (DWORD) st;
|
||||
//printf("ReadMemory: hProcess: %p, baseAddr: %p, buffer: %p, size: %d, read: %d, result: %d\n", hProcess, (LPVOID) qwBaseAddress, lpBuffer, nSize, (DWORD) st, (DWORD) bRet);
|
||||
return bRet;
|
||||
}
|
||||
else
|
||||
{
|
||||
return s_readMemoryFunction(hProcess, qwBaseAddress, lpBuffer, nSize, lpNumberOfBytesRead, s_readMemoryFunction_UserData);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
//OnLoadModule
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
void StackWalker::OnLoadModule(LPCSTR img, LPCSTR mod, DWORD64 baseAddr, DWORD size, DWORD result, LPCSTR symType, LPCSTR pdbName, ULONGLONG fileVersion)
|
||||
{
|
||||
CHAR buffer[STACKWALK_MAX_NAMELEN];
|
||||
if (fileVersion == 0)
|
||||
_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);
|
||||
else
|
||||
{
|
||||
DWORD v4 = (DWORD) fileVersion & 0xFFFF;
|
||||
DWORD v3 = (DWORD) (fileVersion>>16) & 0xFFFF;
|
||||
DWORD v2 = (DWORD) (fileVersion>>32) & 0xFFFF;
|
||||
DWORD v1 = (DWORD) (fileVersion>>48) & 0xFFFF;
|
||||
_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);
|
||||
}
|
||||
if(OutputModules & m_options) OnOutput(buffer);
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
//GetModuleInfo
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
bool StackWalker::GetModuleInfo(HANDLE hProcess, DWORD64 baseAddr, IMAGEHLP_MODULE64 *pModuleInfo)
|
||||
{
|
||||
|
||||
pModuleInfo->SizeOfStruct = sizeof(IMAGEHLP_MODULE64);
|
||||
void *pData = malloc(4096); // reserve enough memory, so the bug in v6.3.5.1 does not lead to memory-overwrites...
|
||||
if (pData == NULL)
|
||||
{
|
||||
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
||||
return false;
|
||||
}
|
||||
memcpy(pData, pModuleInfo, sizeof(IMAGEHLP_MODULE64));
|
||||
if (SymGetModuleInfo64(hProcess, baseAddr, (IMAGEHLP_MODULE64*) pData) != FALSE)
|
||||
{
|
||||
// only copy as much memory as is reserved...
|
||||
memcpy(pModuleInfo, pData, sizeof(IMAGEHLP_MODULE64));
|
||||
pModuleInfo->SizeOfStruct = sizeof(IMAGEHLP_MODULE64);
|
||||
free(pData);
|
||||
return true;
|
||||
}
|
||||
free(pData);
|
||||
SetLastError(ERROR_DLL_INIT_FAILED);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
//GetModuleListTH32
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
bool StackWalker::GetModuleListTH32(HANDLE hProcess, DWORD pid)
|
||||
{
|
||||
// CreateToolhelp32Snapshot()
|
||||
typedef HANDLE (__stdcall *tCT32S)(DWORD dwFlags, DWORD th32ProcessID);
|
||||
// Module32First()
|
||||
typedef BOOL (__stdcall *tM32F)(HANDLE hSnapshot, LPMODULEENTRY32 lpme);
|
||||
// Module32Next()
|
||||
typedef BOOL (__stdcall *tM32N)(HANDLE hSnapshot, LPMODULEENTRY32 lpme);
|
||||
|
||||
// try both dlls...
|
||||
const char *dllname[] = { "kernel32.dll", "tlhelp32.dll" };
|
||||
HINSTANCE hToolhelp = NULL;
|
||||
tCT32S pCT32S = NULL;
|
||||
tM32F pM32F = NULL;
|
||||
tM32N pM32N = NULL;
|
||||
|
||||
HANDLE hSnap;
|
||||
MODULEENTRY32 me;
|
||||
me.dwSize = sizeof(me);
|
||||
BOOL keepGoing;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i<(sizeof(dllname) / sizeof(dllname[0])); i++ )
|
||||
{
|
||||
hToolhelp = LoadLibraryA( dllname[i] );
|
||||
if (hToolhelp == NULL)
|
||||
continue;
|
||||
pCT32S = (tCT32S) GetProcAddress(hToolhelp, "CreateToolhelp32Snapshot");
|
||||
pM32F = (tM32F) GetProcAddress(hToolhelp, "Module32First");
|
||||
pM32N = (tM32N) GetProcAddress(hToolhelp, "Module32Next");
|
||||
if ( (pCT32S != NULL) && (pM32F != NULL) && (pM32N != NULL) )
|
||||
break; // found the functions!
|
||||
FreeLibrary(hToolhelp);
|
||||
hToolhelp = NULL;
|
||||
}
|
||||
|
||||
if (hToolhelp == NULL)
|
||||
return false;
|
||||
|
||||
hSnap = pCT32S( TH32CS_SNAPMODULE, pid );
|
||||
if (hSnap == (HANDLE) -1)
|
||||
return false;
|
||||
|
||||
keepGoing = !!pM32F( hSnap, &me );
|
||||
int cnt = 0;
|
||||
while (keepGoing)
|
||||
{
|
||||
this->LoadModule(hProcess, me.szExePath, me.szModule, (DWORD64) me.modBaseAddr, me.modBaseSize);
|
||||
cnt++;
|
||||
keepGoing = !!pM32N( hSnap, &me );
|
||||
}
|
||||
CloseHandle(hSnap);
|
||||
FreeLibrary(hToolhelp);
|
||||
if (cnt <= 0) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
//GetModuleListPSAPI
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
bool StackWalker::GetModuleListPSAPI(HANDLE hProcess)
|
||||
{
|
||||
DWORD i;
|
||||
//ModuleEntry e;
|
||||
DWORD cbNeeded;
|
||||
MODULEINFO mi;
|
||||
HMODULE *hMods = 0;
|
||||
char *tt = NULL;
|
||||
char *tt2 = NULL;
|
||||
const SIZE_T TTBUFLEN = 8096;
|
||||
int cnt = 0;
|
||||
|
||||
hMods = (HMODULE*) malloc(sizeof(HMODULE) * (TTBUFLEN / sizeof HMODULE));
|
||||
tt = (char*) malloc(sizeof(char) * TTBUFLEN);
|
||||
tt2 = (char*) malloc(sizeof(char) * TTBUFLEN);
|
||||
if ( (hMods == NULL) || (tt == NULL) || (tt2 == NULL) )
|
||||
goto cleanup;
|
||||
|
||||
if ( !EnumProcessModules(hProcess, hMods, TTBUFLEN, &cbNeeded) )
|
||||
{
|
||||
//_ftprintf(fLogFile, _T("%lu: EPM failed, GetLastError = %lu\n"), g_dwShowCount, gle );
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if ( cbNeeded > TTBUFLEN )
|
||||
{
|
||||
//_ftprintf(fLogFile, _T("%lu: More than %lu module handles. Huh?\n"), g_dwShowCount, lenof( hMods ) );
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
for ( i = 0; i < cbNeeded / sizeof hMods[0]; i++ )
|
||||
{
|
||||
// base address, size
|
||||
GetModuleInformation(hProcess, hMods[i], &mi, sizeof mi );
|
||||
// image file name
|
||||
tt[0] = 0;
|
||||
GetModuleFileNameExA(hProcess, hMods[i], tt, TTBUFLEN );
|
||||
// module name
|
||||
tt2[0] = 0;
|
||||
GetModuleBaseNameA(hProcess, hMods[i], tt2, TTBUFLEN );
|
||||
|
||||
DWORD dwRes = this->LoadModule(hProcess, tt, tt2, (DWORD64) mi.lpBaseOfDll, mi.SizeOfImage);
|
||||
if (dwRes != ERROR_SUCCESS)
|
||||
this->OnDbgHelpErr("LoadModule", dwRes, 0);
|
||||
cnt++;
|
||||
}
|
||||
|
||||
cleanup:
|
||||
if (tt2 != NULL) free(tt2);
|
||||
if (tt != NULL) free(tt);
|
||||
if (hMods != NULL) free(hMods);
|
||||
|
||||
return cnt != 0;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
//OnCallstackEntry
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
void StackWalker::OnCallstackEntry(CallstackEntryType eType, CallstackEntry &entry)
|
||||
{
|
||||
CHAR buffer[STACKWALK_MAX_NAMELEN];
|
||||
if ( (eType != lastEntry) && (entry.offset != 0) )
|
||||
{
|
||||
if (entry.name[0] == 0) strcpy_s(entry.name, "(function-name not available)");
|
||||
if (entry.undName[0] != 0) strcpy_s(entry.name, entry.undName);
|
||||
if (entry.undFullName[0] != 0) strcpy_s(entry.name, entry.undFullName);
|
||||
if (entry.lineFileName[0] == 0)
|
||||
{
|
||||
strcpy_s(entry.lineFileName, "(filename not available)");
|
||||
if (entry.moduleName[0] == 0) strcpy_s(entry.moduleName, "(module-name not available)");
|
||||
_snprintf_s(buffer, STACKWALK_MAX_NAMELEN, "%p (%s): %s: %s\n", (LPVOID) entry.offset, entry.moduleName, entry.lineFileName, entry.name);
|
||||
}
|
||||
else
|
||||
{
|
||||
_snprintf_s(buffer, STACKWALK_MAX_NAMELEN, "%s (%d): %s\n", entry.lineFileName, entry.lineNumber, entry.name);
|
||||
}
|
||||
OnOutput(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
//OnDbgHelpErr
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
void StackWalker::OnDbgHelpErr(LPCSTR szFuncName, DWORD gle, DWORD64 addr)
|
||||
{
|
||||
CHAR buffer[STACKWALK_MAX_NAMELEN];
|
||||
_snprintf_s(buffer, STACKWALK_MAX_NAMELEN, "ERROR: %s, GetLastError: %d (Address: %p)\n", szFuncName, gle, (LPVOID) addr);
|
||||
OnOutput(buffer);
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
//OnSymInit
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------
|
||||
void StackWalker::OnSymInit(LPCSTR szSearchPath, DWORD symOptions, LPCSTR szUserName)
|
||||
{
|
||||
//Symbol Search path
|
||||
CHAR buffer[STACKWALK_MAX_NAMELEN];
|
||||
_snprintf_s(buffer, STACKWALK_MAX_NAMELEN, "SymInit: Symbol-SearchPath: '%s', symOptions: %d, UserName: '%s'\n", szSearchPath, symOptions, szUserName);
|
||||
if(OutputSymPath & m_options) OnOutput(buffer);
|
||||
|
||||
//OS-version
|
||||
OSVERSIONINFOEX ver;
|
||||
ZeroMemory(&ver, sizeof(OSVERSIONINFOEX));
|
||||
ver.dwOSVersionInfoSize = sizeof(ver);
|
||||
if (GetVersionEx( (OSVERSIONINFO*) &ver) != FALSE)
|
||||
{
|
||||
_snprintf_s(buffer, STACKWALK_MAX_NAMELEN, "OS-Version: %d.%d.%d (%s) 0x%x-0x%x\n",
|
||||
ver.dwMajorVersion, ver.dwMinorVersion, ver.dwBuildNumber,
|
||||
ver.szCSDVersion, ver.wSuiteMask, ver.wProductType);
|
||||
if(OutputOS & m_options) OnOutput(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
102
Engine/source/platformWin32/minidump/winStackWalker.h
Normal file
102
Engine/source/platformWin32/minidump/winStackWalker.h
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// Copyright (c) 2012 GarageGames, LLC
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to
|
||||
// deal in the Software without restriction, including without limitation the
|
||||
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
// sell copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
// IN THE SOFTWARE.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef __WIN_PLATFORM_STACKWALKER__
|
||||
#define __WIN_PLATFORM_STACKWALKER__
|
||||
|
||||
#if defined( TORQUE_MINIDUMP ) && defined( TORQUE_RELEASE )
|
||||
|
||||
#include <windows.h>
|
||||
#include <dbghelp.h>
|
||||
|
||||
class StackWalker
|
||||
{
|
||||
public:
|
||||
|
||||
typedef enum StackWalkOptions
|
||||
{
|
||||
// RetrieveNone = 0, // No additional info will be retrieved (only the address is available)
|
||||
// RetrieveSymbol = 1, // Try to get the symbol-name
|
||||
// RetrieveLine = 2, // Try to get the line for this symbol
|
||||
// RetrieveModuleInfo = 4, // Try to retrieve the module-infos
|
||||
RetrieveFileVersion = 8, // Also retrieve the version for the DLL/EXE
|
||||
RetrieveVerbose = 0xF, // Contains all of the above retrieve options
|
||||
|
||||
SymBuildPath = 0x10, // Generate a "good" symbol-search-path
|
||||
SymUseSymSrv = 0x20, // Also use a public Symbol Server
|
||||
SymAll = 0x30, // Contains all of the above Symbol options
|
||||
|
||||
OutputSymPath = 0x80, //print out the symbol path
|
||||
OutputOS = 0x100, //print out the OS path
|
||||
OutputModules = 0x200, //print out the Modules
|
||||
|
||||
OptionsDefault = 0x3F, // Less verbose output (default)
|
||||
OptionsAll = 0x2FF // Contains all options
|
||||
};
|
||||
|
||||
StackWalker(DWORD optionFlags = OptionsDefault, LPCSTR szSymPath = NULL);
|
||||
virtual ~StackWalker();
|
||||
|
||||
typedef BOOL (__stdcall *PReadProcessMemoryRoutine)(HANDLE hProcess,
|
||||
DWORD64 qwBaseAddress,
|
||||
PVOID lpBuffer,
|
||||
DWORD nSize,
|
||||
LPDWORD lpNumberOfBytesRead,
|
||||
LPVOID pUserData // optional data, which was passed in "ShowCallstack"
|
||||
);
|
||||
|
||||
// pUserData is optional to identify some data in the 'readMemoryFunction'-callback
|
||||
bool ShowCallstack(HANDLE hThread, CONTEXT const & Context, PReadProcessMemoryRoutine readMemoryFunction = NULL, LPVOID pUserData = NULL);
|
||||
|
||||
void setOutputBuffer(char * buffer);
|
||||
|
||||
private:
|
||||
typedef enum CallstackEntryType {firstEntry, nextEntry, lastEntry};
|
||||
|
||||
HANDLE m_hProcess;
|
||||
DWORD m_dwProcessId;
|
||||
bool m_modulesLoaded;
|
||||
LPSTR m_szSymPath;
|
||||
int m_options;
|
||||
char * m_pOutputBuffer;
|
||||
|
||||
static BOOL __stdcall myReadProcMem(HANDLE hProcess, DWORD64 qwBaseAddress, PVOID lpBuffer, DWORD nSize, LPDWORD lpNumberOfBytesRead);
|
||||
|
||||
bool Init(LPCSTR szSymPath);
|
||||
|
||||
virtual void OnSymInit(LPCSTR szSearchPath, DWORD symOptions, LPCSTR szUserName);
|
||||
virtual void OnLoadModule(LPCSTR img, LPCSTR mod, DWORD64 baseAddr, DWORD size, DWORD result, LPCSTR symType, LPCSTR pdbName, ULONGLONG fileVersion);
|
||||
virtual void OnCallstackEntry(CallstackEntryType eType, struct CallstackEntry &entry);
|
||||
virtual void OnDbgHelpErr(LPCSTR szFuncName, DWORD gle, DWORD64 addr);
|
||||
virtual void OnOutput(LPCSTR szText);
|
||||
|
||||
bool LoadModules();
|
||||
DWORD LoadModule(HANDLE hProcess, LPCSTR img, LPCSTR mod, DWORD64 baseAddr, DWORD size);
|
||||
bool GetModuleListTH32(HANDLE hProcess, DWORD pid);
|
||||
bool GetModuleListPSAPI(HANDLE hProcess);
|
||||
bool GetModuleInfo(HANDLE hProcess, DWORD64 baseAddr, IMAGEHLP_MODULE64 *pModuleInfo);
|
||||
};
|
||||
|
||||
void dGetStackTrace(char * traceBuffer, CONTEXT const & ContextRecord);
|
||||
|
||||
#endif
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue