1
0

WriteMemoryDump.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * WriteMemoryDump.h
  3. * -----------------
  4. * Purpose: Code for writing memory dumps to a file.
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #pragma once
  10. #include "openmpt/all/BuildSettings.hpp"
  11. #if MPT_COMPILER_MSVC
  12. #pragma warning(push)
  13. #pragma warning(disable:4091) // 'typedef ': ignored on left of '' when no variable is declared
  14. #endif // MPT_COMPILER_MSVC
  15. #include <dbghelp.h>
  16. #if MPT_COMPILER_MSVC
  17. #pragma warning(pop)
  18. #endif // MPT_COMPILER_MSVC
  19. OPENMPT_NAMESPACE_BEGIN
  20. typedef BOOL (WINAPI *MINIDUMPWRITEDUMP)(HANDLE hProcess, DWORD dwPid, HANDLE hFile, MINIDUMP_TYPE DumpType,
  21. CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
  22. CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
  23. CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam
  24. );
  25. static bool WriteMemoryDump(_EXCEPTION_POINTERS *pExceptionInfo, const TCHAR *filename, bool fullMemDump)
  26. {
  27. bool result = false;
  28. HMODULE hDll = ::LoadLibrary(_T("DBGHELP.DLL"));
  29. if (hDll)
  30. {
  31. MINIDUMPWRITEDUMP pDump = (MINIDUMPWRITEDUMP)::GetProcAddress(hDll, "MiniDumpWriteDump");
  32. if (pDump)
  33. {
  34. HANDLE hFile = ::CreateFile(filename, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  35. if (hFile != INVALID_HANDLE_VALUE)
  36. {
  37. _MINIDUMP_EXCEPTION_INFORMATION ExInfo;
  38. if(pExceptionInfo)
  39. {
  40. ExInfo.ThreadId = ::GetCurrentThreadId();
  41. ExInfo.ExceptionPointers = pExceptionInfo;
  42. ExInfo.ClientPointers = NULL;
  43. }
  44. pDump(GetCurrentProcess(), GetCurrentProcessId(), hFile,
  45. fullMemDump ?
  46. (MINIDUMP_TYPE)(MiniDumpWithFullMemory | MiniDumpWithHandleData | MiniDumpWithThreadInfo | MiniDumpWithProcessThreadData | MiniDumpWithFullMemoryInfo
  47. #if MPT_COMPILER_MSVC
  48. | MiniDumpIgnoreInaccessibleMemory | MiniDumpWithTokenInformation
  49. #endif
  50. )
  51. :
  52. MiniDumpNormal,
  53. pExceptionInfo ? &ExInfo : NULL, NULL, NULL);
  54. ::CloseHandle(hFile);
  55. result = true;
  56. }
  57. }
  58. ::FreeLibrary(hDll);
  59. }
  60. return result;
  61. }
  62. OPENMPT_NAMESPACE_END