2019-09-21 18:59:54 +00:00
|
|
|
#include "ns.h" // Must come before everything else!
|
2017-02-19 22:15:44 +00:00
|
|
|
|
2017-02-25 08:15:36 +00:00
|
|
|
#include "compat.h"
|
2008-07-12 14:09:52 +00:00
|
|
|
#include "mdump.h"
|
2017-02-19 22:15:44 +00:00
|
|
|
|
2008-08-06 11:50:34 +00:00
|
|
|
#include <tchar.h>
|
2019-09-21 20:53:00 +00:00
|
|
|
|
|
|
|
BEGIN_DUKE_NS
|
|
|
|
|
2008-07-12 14:09:52 +00:00
|
|
|
LPCSTR MiniDumper::m_szAppName;
|
|
|
|
|
|
|
|
MiniDumper g_dumper("eduke32");
|
|
|
|
|
2018-11-18 18:06:38 +00:00
|
|
|
MiniDumper::MiniDumper(LPCSTR szAppName)
|
2008-07-12 14:09:52 +00:00
|
|
|
{
|
2018-10-25 23:30:46 +00:00
|
|
|
// if this assert fires then you have two instances of MiniDumper
|
|
|
|
// which is not allowed
|
2018-11-18 18:06:38 +00:00
|
|
|
assert(m_szAppName == NULL);
|
2008-07-12 14:09:52 +00:00
|
|
|
|
2018-10-25 23:30:46 +00:00
|
|
|
m_szAppName = szAppName ? _strdup(szAppName) : "Application";
|
2008-07-12 14:09:52 +00:00
|
|
|
|
2018-11-18 18:06:38 +00:00
|
|
|
::SetUnhandledExceptionFilter(TopLevelFilter);
|
2008-07-12 14:09:52 +00:00
|
|
|
}
|
|
|
|
|
2018-11-18 18:06:38 +00:00
|
|
|
LONG MiniDumper::TopLevelFilter(struct _EXCEPTION_POINTERS *pExceptionInfo)
|
2008-07-12 14:09:52 +00:00
|
|
|
{
|
2018-10-25 23:30:46 +00:00
|
|
|
HMODULE hDll = NULL;
|
2018-11-18 18:06:38 +00:00
|
|
|
char szDbgHelpPath[_MAX_PATH];
|
2018-10-25 23:30:46 +00:00
|
|
|
|
2018-11-18 18:06:38 +00:00
|
|
|
if (GetModuleFileName(NULL, szDbgHelpPath, _MAX_PATH))
|
2018-10-25 23:30:46 +00:00
|
|
|
{
|
2018-11-18 18:06:38 +00:00
|
|
|
auto pSlash = _tcsrchr(szDbgHelpPath, '\\');
|
|
|
|
|
2018-10-25 23:30:46 +00:00
|
|
|
if (pSlash)
|
|
|
|
{
|
2018-11-18 18:06:38 +00:00
|
|
|
_tcscpy(pSlash + 1, "DBGHELP.DLL");
|
|
|
|
hDll = ::LoadLibrary(szDbgHelpPath);
|
2018-10-25 23:30:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 18:06:38 +00:00
|
|
|
if (hDll == NULL)
|
2018-10-25 23:30:46 +00:00
|
|
|
{
|
|
|
|
// load any version we can
|
2018-11-18 18:06:38 +00:00
|
|
|
hDll = ::LoadLibrary("DBGHELP.DLL");
|
2018-10-25 23:30:46 +00:00
|
|
|
}
|
|
|
|
|
2018-11-18 18:06:38 +00:00
|
|
|
LONG retval = EXCEPTION_CONTINUE_SEARCH;
|
|
|
|
LPCTSTR szResult = "DBGHELP.DLL not found";
|
|
|
|
char szScratch[_MAX_PATH];
|
2018-10-25 23:30:46 +00:00
|
|
|
|
|
|
|
if (hDll)
|
|
|
|
{
|
2018-11-18 18:06:38 +00:00
|
|
|
auto pDump = MINIDUMPWRITEDUMP(::GetProcAddress(hDll, "MiniDumpWriteDump"));
|
|
|
|
|
2018-10-25 23:30:46 +00:00
|
|
|
if (pDump)
|
|
|
|
{
|
|
|
|
char szDumpPath[_MAX_PATH];
|
|
|
|
|
2018-11-18 18:06:38 +00:00
|
|
|
sprintf(szDumpPath, "%s_%u.dmp", m_szAppName, timeGetTime());
|
2018-10-25 23:30:46 +00:00
|
|
|
|
2018-11-18 18:06:38 +00:00
|
|
|
HANDLE hFile = ::CreateFile(szDumpPath, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, NULL);
|
|
|
|
|
|
|
|
if (hFile != INVALID_HANDLE_VALUE)
|
2018-10-25 23:30:46 +00:00
|
|
|
{
|
2018-11-18 18:06:38 +00:00
|
|
|
_MINIDUMP_EXCEPTION_INFORMATION ExInfo = { ::GetCurrentThreadId(), pExceptionInfo, NULL };
|
|
|
|
|
|
|
|
// take a dump
|
|
|
|
BOOL bOK = pDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, MiniDumpNormal, &ExInfo, NULL, NULL);
|
2018-10-25 23:30:46 +00:00
|
|
|
|
2018-11-18 18:06:38 +00:00
|
|
|
if (bOK)
|
2018-10-25 23:30:46 +00:00
|
|
|
{
|
2018-11-18 18:06:38 +00:00
|
|
|
sprintf(szScratch, "Saved dump file to \"%s\"", szDumpPath);
|
|
|
|
szResult = szScratch;
|
|
|
|
retval = EXCEPTION_EXECUTE_HANDLER;
|
2018-10-25 23:30:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-11-18 18:06:38 +00:00
|
|
|
sprintf(szScratch, "Failed to save dump file to \"%s\" (error %d)", szDumpPath, GetLastError());
|
2018-10-25 23:30:46 +00:00
|
|
|
szResult = szScratch;
|
|
|
|
}
|
2018-11-18 18:06:38 +00:00
|
|
|
|
|
|
|
::CloseHandle(hFile);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sprintf(szScratch, "Failed to create dump file \"%s\" (error %d)", szDumpPath, GetLastError());
|
|
|
|
szResult = szScratch;
|
2018-10-25 23:30:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
szResult = "DBGHELP.DLL too old";
|
|
|
|
}
|
2018-10-25 23:30:37 +00:00
|
|
|
|
|
|
|
::FreeLibrary(hDll);
|
2018-10-25 23:30:46 +00:00
|
|
|
}
|
2008-07-12 14:09:52 +00:00
|
|
|
|
2018-11-18 18:06:38 +00:00
|
|
|
::MessageBox(NULL, szResult, m_szAppName, MB_OK);
|
2008-07-12 14:09:52 +00:00
|
|
|
|
2018-10-25 23:30:46 +00:00
|
|
|
return retval;
|
2018-11-18 18:06:38 +00:00
|
|
|
}
|
2019-09-21 20:53:00 +00:00
|
|
|
|
|
|
|
END_DUKE_NS
|