Core/CrashHandler: Remove logging function variation depending on crash reason and print directly to file, not to temporary buffer

(cherry picked from commit c5e1b49e8c)
This commit is contained in:
Shauren
2020-08-07 17:57:44 +02:00
parent 259bbf1e04
commit cc0affa523
2 changed files with 9 additions and 62 deletions

View File

@@ -10,7 +10,6 @@
#include <algorithm>
#include <ehdata.h>
#include <rttidata.h>
#include <cstdio>
#include <tlhelp32.h>
#include <tchar.h>
@@ -51,12 +50,11 @@ TCHAR WheatyExceptionReport::m_szLogFileName[MAX_PATH];
TCHAR WheatyExceptionReport::m_szDumpFileName[MAX_PATH];
LPTOP_LEVEL_EXCEPTION_FILTER WheatyExceptionReport::m_previousFilter;
_invalid_parameter_handler WheatyExceptionReport::m_previousCrtHandler;
HANDLE WheatyExceptionReport::m_hReportFile;
FILE* WheatyExceptionReport::m_hReportFile;
HANDLE WheatyExceptionReport::m_hDumpFile;
HANDLE WheatyExceptionReport::m_hProcess;
SymbolPairs WheatyExceptionReport::symbols;
std::stack<SymbolDetail> WheatyExceptionReport::symbolDetails;
bool WheatyExceptionReport::stackOverflowException;
bool WheatyExceptionReport::alreadyCrashed;
std::mutex WheatyExceptionReport::alreadyCrashedLock;
WheatyExceptionReport::pRtlGetVersion WheatyExceptionReport::RtlGetVersion;
@@ -73,7 +71,6 @@ WheatyExceptionReport::WheatyExceptionReport() // Constructor
m_previousFilter = SetUnhandledExceptionFilter(WheatyUnhandledExceptionFilter);
m_previousCrtHandler = _set_invalid_parameter_handler(WheatyCrtHandler);
m_hProcess = GetCurrentProcess();
stackOverflowException = false;
alreadyCrashed = false;
RtlGetVersion = (pRtlGetVersion)GetProcAddress(GetModuleHandle(_T("ntdll.dll")), "RtlGetVersion");
if (!IsDebuggerPresent())
@@ -110,9 +107,6 @@ PEXCEPTION_POINTERS pExceptionInfo)
alreadyCrashed = true;
if (pExceptionInfo->ExceptionRecord->ExceptionCode == STATUS_STACK_OVERFLOW)
stackOverflowException = true;
TCHAR module_folder_name[MAX_PATH];
GetModuleFileName(nullptr, module_folder_name, MAX_PATH);
TCHAR* pos = _tcsrchr(module_folder_name, '\\');
@@ -134,7 +128,7 @@ PEXCEPTION_POINTERS pExceptionInfo)
sprintf(m_szDumpFileName, "%s\\%s_%s_[%u-%u_%u-%u-%u].dmp",
crash_folder_path, GitRevision::GetHash(), pos, systime.wDay, systime.wMonth, systime.wHour, systime.wMinute, systime.wSecond);
sprintf(m_szLogFileName, "%s\\%s_%s_[%u-%u_%u-%u-%u].txt",
_stprintf(m_szLogFileName, _T("%s\\%s_%s_[%u-%u_%u-%u-%u].txt"),
crash_folder_path, GitRevision::GetHash(), pos, systime.wDay, systime.wMonth, systime.wHour, systime.wMinute, systime.wSecond);
m_hDumpFile = CreateFile(m_szDumpFileName,
@@ -145,14 +139,6 @@ PEXCEPTION_POINTERS pExceptionInfo)
FILE_FLAG_WRITE_THROUGH,
nullptr);
m_hReportFile = CreateFile(m_szLogFileName,
GENERIC_WRITE,
0,
nullptr,
OPEN_ALWAYS,
FILE_FLAG_WRITE_THROUGH,
nullptr);
if (m_hDumpFile)
{
MINIDUMP_EXCEPTION_INFORMATION info;
@@ -179,13 +165,13 @@ PEXCEPTION_POINTERS pExceptionInfo)
CloseHandle(m_hDumpFile);
}
m_hReportFile = _tfopen(m_szLogFileName, _T("wb"));
if (m_hReportFile)
{
SetFilePointer(m_hReportFile, 0, nullptr, FILE_END);
GenerateExceptionReport(pExceptionInfo);
CloseHandle(m_hReportFile);
fclose(m_hReportFile);
m_hReportFile = nullptr;
}
@@ -1468,47 +1454,10 @@ DWORD_PTR WheatyExceptionReport::DereferenceUnsafePointer(DWORD_PTR address)
//============================================================================
int __cdecl WheatyExceptionReport::Log(const TCHAR * format, ...)
{
int retValue;
va_list argptr;
va_start(argptr, format);
if (stackOverflowException)
{
retValue = HeapLog(format, argptr);
va_end(argptr);
}
else
{
retValue = StackLog(format, argptr);
va_end(argptr);
}
return retValue;
}
int __cdecl WheatyExceptionReport::StackLog(const TCHAR * format, va_list argptr)
{
int retValue;
DWORD cbWritten;
TCHAR szBuff[WER_LARGE_BUFFER_SIZE];
retValue = vsprintf(szBuff, format, argptr);
WriteFile(m_hReportFile, szBuff, retValue * sizeof(TCHAR), &cbWritten, nullptr);
return retValue;
}
int __cdecl WheatyExceptionReport::HeapLog(const TCHAR * format, va_list argptr)
{
int retValue = 0;
DWORD cbWritten;
TCHAR* szBuff = (TCHAR*)malloc(sizeof(TCHAR) * WER_LARGE_BUFFER_SIZE);
if (szBuff != nullptr)
{
retValue = vsprintf(szBuff, format, argptr);
WriteFile(m_hReportFile, szBuff, retValue * sizeof(TCHAR), &cbWritten, nullptr);
free(szBuff);
}
int retValue = _vftprintf(m_hReportFile, format, argptr);
va_end(argptr);
return retValue;
}

View File

@@ -9,6 +9,7 @@
#include <dbghelp.h>
#include <set>
#include <cstdlib>
#include <cstdio>
#include <stack>
#include <mutex>
@@ -171,8 +172,6 @@ class WheatyExceptionReport
static DWORD_PTR DereferenceUnsafePointer(DWORD_PTR address);
static int __cdecl Log(const TCHAR * format, ...);
static int __cdecl StackLog(const TCHAR * format, va_list argptr);
static int __cdecl HeapLog(const TCHAR * format, va_list argptr);
static bool StoreSymbol(DWORD type , DWORD_PTR offset);
static void ClearSymbols();
@@ -182,12 +181,11 @@ class WheatyExceptionReport
static TCHAR m_szDumpFileName[MAX_PATH];
static LPTOP_LEVEL_EXCEPTION_FILTER m_previousFilter;
static _invalid_parameter_handler m_previousCrtHandler;
static HANDLE m_hReportFile;
static FILE* m_hReportFile;
static HANDLE m_hDumpFile;
static HANDLE m_hProcess;
static SymbolPairs symbols;
static std::stack<SymbolDetail> symbolDetails;
static bool stackOverflowException;
static bool alreadyCrashed;
static std::mutex alreadyCrashedLock;
typedef NTSTATUS(NTAPI* pRtlGetVersion)(PRTL_OSVERSIONINFOW lpVersionInformation);