[DRWTSN32] Add code to write a minidump
authorWilliam Kent <wjk011@gmail.com>
Thu, 8 Mar 2018 19:51:15 +0000 (20:51 +0100)
committerMark Jansen <mark.jansen@reactos.org>
Fri, 9 Mar 2018 23:04:57 +0000 (00:04 +0100)
base/applications/drwtsn32/main.cpp

index 1224b75..436a18b 100644 (file)
@@ -178,6 +178,62 @@ std::wstring Settings_GetOutputPath(void)
     return std::wstring(Buffer);
 }
 
+BOOL Settings_GetShouldWriteDump(void)
+{
+    CRegKey key;
+    if (key.Open(HKEY_CURRENT_USER, L"SOFTWARE\\ReactOS\\Crash Reporter", KEY_READ) != ERROR_SUCCESS)
+    {
+        return FALSE;
+    }
+
+    DWORD Value;
+    if (key.QueryDWORDValue(L"Minidump", Value) != ERROR_SUCCESS)
+    {
+        return FALSE;
+    }
+
+    return (Value != 0);
+}
+
+HRESULT WriteMinidump(LPCWSTR LogFilePath, DumpData& data)
+{
+    HRESULT hr = S_OK;
+
+    WCHAR DumpFilePath[MAX_PATH] = L"";
+    StringCchCopyW(DumpFilePath, _countof(DumpFilePath), LogFilePath);
+    PathRemoveExtensionW(DumpFilePath);
+    PathAddExtensionW(DumpFilePath, L".dmp");
+
+    HANDLE hDumpFile = CreateFileW(DumpFilePath, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
+    if (hDumpFile == INVALID_HANDLE_VALUE)
+    {
+        return HRESULT_FROM_WIN32(GetLastError());
+    }
+
+    ThreadData& Thread = data.Threads[data.ThreadID];
+    Thread.Update();
+    PCONTEXT ContextPointer = &Thread.Context;
+
+    MINIDUMP_EXCEPTION_INFORMATION DumpExceptionInfo = {0};
+    EXCEPTION_POINTERS ExceptionPointers = {0};
+    ExceptionPointers.ExceptionRecord = &data.ExceptionInfo.ExceptionRecord;
+    ExceptionPointers.ContextRecord = ContextPointer;
+
+    DumpExceptionInfo.ThreadId = data.ThreadID;
+    DumpExceptionInfo.ExceptionPointers = &ExceptionPointers;
+    DumpExceptionInfo.ClientPointers = FALSE;
+
+    BOOL DumpSucceeded = MiniDumpWriteDump(data.ProcessHandle, data.ProcessID, hDumpFile, MiniDumpNormal, &DumpExceptionInfo, NULL, NULL);
+    if (!DumpSucceeded)
+    {
+        // According to MSDN, this value is already an HRESULT, so don't convert it again.
+        hr = GetLastError();
+    }
+
+    CloseHandle(hDumpFile);
+    return hr;
+}
+
 int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, LPWSTR cmdLine, INT)
 {
     int argc;
@@ -284,6 +340,10 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, LPWSTR cmdLine, INT)
     }
 
     PrintBugreport(output, data);
+    if (Settings_GetShouldWriteDump() && HasPath)
+    {
+        WriteMinidump(OutputPath.c_str(), data);
+    }
 
     TerminateProcess(data.ProcessHandle, data.ExceptionInfo.ExceptionRecord.ExceptionCode);