WIP, trying to figure out why minidumps doesn't work.

This commit is contained in:
William Moberg
2016-02-23 18:19:48 +01:00
parent bac23f6e8c
commit a36deb2cf9
3 changed files with 84 additions and 32 deletions
+1 -1
View File
@@ -3,6 +3,6 @@
#include <Windows.h> #include <Windows.h>
void WINAPI Create_Dump(PEXCEPTION_POINTERS pException, BOOL File_Flag, BOOL Show_Flag); void WINAPI Create_Dump(PEXCEPTION_POINTERS pException);
#endif #endif
+58 -26
View File
@@ -9,6 +9,7 @@
#include <ctime> #include <ctime>
#include <windows.h> #include <windows.h>
#include <comdef.h>
#include <tlhelp32.h> #include <tlhelp32.h>
//#include "dbghelp.h" //#include "dbghelp.h"
@@ -28,9 +29,24 @@ typedef struct _MINIDUMP_EXCEPTION_INFORMATION {
BOOL ClientPointers; BOOL ClientPointers;
} MINIDUMP_EXCEPTION_INFORMATION, *PMINIDUMP_EXCEPTION_INFORMATION; } MINIDUMP_EXCEPTION_INFORMATION, *PMINIDUMP_EXCEPTION_INFORMATION;
typedef enum _MINIDUMP_TYPE { typedef enum _MINIDUMP_TYPE
MiniDumpNormal = 0x00000000, {
MiniDumpWithDataSegs = 0x00000001, MiniDumpNormal = 0x00000000,
MiniDumpWithDataSegs = 0x00000001,
MiniDumpWithFullMemory = 0x00000002,
MiniDumpWithHandleData = 0x00000004,
MiniDumpFilterMemory = 0x00000008,
MiniDumpScanMemory = 0x00000010,
MiniDumpWithUnloadedModules = 0x00000020,
MiniDumpWithIndirectlyReferencedMemory = 0x00000040,
MiniDumpFilterModulePaths = 0x00000080,
MiniDumpWithProcessThreadData = 0x00000100,
MiniDumpWithPrivateReadWriteMemory = 0x00000200,
MiniDumpWithoutOptionalData = 0x00000400,
MiniDumpWithFullMemoryInfo = 0x00000800,
MiniDumpWithThreadInfo = 0x00001000,
MiniDumpWithCodeSegs = 0x00002000,
MiniDumpWithoutManagedState = 0x00004000,
} MINIDUMP_TYPE; } MINIDUMP_TYPE;
typedef BOOL (WINAPI * MINIDUMP_WRITE_DUMP)( typedef BOOL (WINAPI * MINIDUMP_WRITE_DUMP)(
@@ -63,48 +79,64 @@ MINIDUMP_WRITE_DUMP MiniDumpWriteDump_;
typedef HANDLE (WINAPI * CREATE_TOOL_HELP32_SNAPSHOT)(DWORD dwFlags, DWORD th32ProcessID); typedef HANDLE (WINAPI * CREATE_TOOL_HELP32_SNAPSHOT)(DWORD dwFlags, DWORD th32ProcessID);
//************************************************************************************* //*************************************************************************************
void WINAPI Create_Dump(PEXCEPTION_POINTERS pException, BOOL File_Flag, BOOL Show_Flag) void WINAPI Create_Dump(PEXCEPTION_POINTERS pException)
//************************************************************************************* //*************************************************************************************
// Create dump. // Create dump.
// pException can be either GetExceptionInformation() or NULL. // pException can be either GetExceptionInformation() or NULL.
// If File_Flag = TRUE - write dump files (.dmz and .dmp) with the name of the current process.
// If Show_Flag = TRUE - show message with Get_Exception_Info() dump.
{ {
// Try to get MiniDumpWriteDump() address. // Try to get MiniDumpWriteDump() address.
hDbgHelp = LoadLibrary("DBGHELP.DLL"); hDbgHelp = LoadLibrary("DBGHELP.DLL");
MiniDumpWriteDump_ = (MINIDUMP_WRITE_DUMP)GetProcAddress(hDbgHelp, "MiniDumpWriteDump"); MiniDumpWriteDump_ = (MINIDUMP_WRITE_DUMP)GetProcAddress(hDbgHelp, "MiniDumpWriteDump");
// If MiniDumpWriteDump() of DbgHelp.dll available. // If MiniDumpWriteDump() of DbgHelp.dll available.
if (MiniDumpWriteDump_) if (MiniDumpWriteDump_)
{ {
HANDLE hDump_File; //get absolute path of current process: C:/ ... /name.exe
CHAR Dump_Path[MAX_PATH]; CHAR Dump_Path[MAX_PATH];
GetModuleFileName(NULL, Dump_Path, sizeof(Dump_Path));
std::string path(Dump_Path);
GetModuleFileName(NULL, Dump_Path, sizeof(Dump_Path)); //path of current process //Get current time in a string.
std::time_t t = std::time(NULL); std::time_t t = std::time(NULL);
char tStr[16]; char tStr[16];
std::strftime(tStr, 32, " %a %H-%M-%S", std::localtime(&t)); std::strftime(tStr, ARRAYSIZE(tStr), " %a %H-%M-%S", std::localtime(&t));
std::string time(tStr); std::string time(tStr);
std::string path(Dump_Path); // Remove the .exe from path
path = path.substr(0, path.length() - 4); path = path.substr(0, path.length() - 4);
// Add the current time and .dmp
path += time + ".dmp"; path += time + ".dmp";
MINIDUMP_EXCEPTION_INFORMATION M; MINIDUMP_EXCEPTION_INFORMATION M;
M.ThreadId = GetCurrentThreadId(); M.ThreadId = GetCurrentThreadId();
M.ExceptionPointers = pException; M.ExceptionPointers = pException;
M.ClientPointers = 0; M.ClientPointers = TRUE;
hDump_File = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); HANDLE hDump_File = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD curProId = GetCurrentProcessId();
//HANDLE curPro = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ | PROCESS_DUP_HANDLE, FALSE, curProId);
HANDLE curPro = GetCurrentProcess();
MiniDumpWriteDump_(GetCurrentProcess(), GetCurrentProcessId(), hDump_File, BOOL result = MiniDumpWriteDump_(curPro, curProId, hDump_File,
MiniDumpNormal, (pException) ? &M : NULL, NULL, NULL); MiniDumpNormal, (pException) ? &M : NULL, NULL, NULL);
HRESULT error = (HRESULT)GetLastError();
CloseHandle(hDump_File); CloseHandle(hDump_File);
if (!result) {
std::cout << "Memory dumped to: \"" << path.c_str() << "\""; _com_error cErr(error);
MessageBox(NULL, ("Application crashed, memory dumped to: " + path).c_str(), "MiniDump", MB_ICONHAND | MB_OK); HRESULT actualErrorCode = error & 0xFFFF;
char eBuf[16];
std::sprintf(eBuf, "0x%x", actualErrorCode);
std::cout << "Bad memory dump at: \"" << path.c_str() << "\"" << std::endl
<< "because MiniDumpWriteDump failed with error #" << actualErrorCode << " (" << eBuf << "): \"" << cErr.ErrorMessage() << "\"" << std::endl;
MessageBox(NULL, "Application crashed, memory dump failed, but file was created.", "MiniDump", MB_ICONHAND | MB_OK);
} else {
std::cout << "Memory dumped to: \"" << path.c_str() << "\"";
MessageBox(NULL, ("Application crashed, memory dumped to: " + path).c_str(), "MiniDump", MB_ICONHAND | MB_OK);
}
} else { } else {
MessageBox(NULL, "Application crashed, memory dump failed.", "MiniDump", MB_ICONHAND | MB_OK); std::cout << "Memory dump failed because MiniDumpWriteDump is not available.";
MessageBox(NULL, "Application crashed, could not create a memory dump.", "MiniDump", MB_ICONHAND | MB_OK);
} }
} }
+25 -5
View File
@@ -1,25 +1,45 @@
#include "Game.h" //#include "Game.h"
#include "MiniDump.h" #include "MiniDump.h"
LONG WINAPI CrashHandler(EXCEPTION_POINTERS* pException); LONG WINAPI CrashHandler(EXCEPTION_POINTERS* pException);
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
::SetUnhandledExceptionFilter(CrashHandler); //::SetUnhandledExceptionFilter(CrashHandler);
//EXCEPTION_POINTERS* pException = new EXCEPTION_POINTERS();
//pException->ContextRecord = new _CONTEXT();
//pException->ExceptionRecord = new EXCEPTION_RECORD();
//CrashHandler(pException);
//return 0;
__try {
int* ii = nullptr;
*ii = 17;
//int x = asd[4];
//std::cout << "crash before we get here, don't optimize x. " << x;
//throw std::exception();
} __except (CrashHandler(GetExceptionInformation())) {
system("pause");
return 0;
}
system("pause");
/*
Game game(argc, argv); Game game(argc, argv);
while (game.Running()) { while (game.Running()) {
game.Tick(); game.Tick();
} }
*/
return 0; return 0;
} }
LONG WINAPI CrashHandler(EXCEPTION_POINTERS* pException) LONG WINAPI CrashHandler(EXCEPTION_POINTERS* pException)
{ {
//Take minidump. path should be bin/TacticalZ.dmp //Take minidump. path should be bin/TacticalZ.dmp
//Then show MessageBox, and exit application. //Then show MessageBox, and exit application.
Create_Dump(pException, 1, 1); Create_Dump(pException);
return EXCEPTION_EXECUTE_HANDLER;// EXCEPTION_CONTINUE_SEARCH return EXCEPTION_EXECUTE_HANDLER;// EXCEPTION_CONTINUE_SEARCH
} }