MiniDump.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #if defined(_MSC_VER) && defined(ENABLE_MINIDUMPS)
  25. #include "ProcessUtils.h"
  26. #include <cstdio>
  27. #include <io.h>
  28. #include <fcntl.h>
  29. #include <time.h>
  30. #include <windows.h>
  31. #include <dbghelp.h>
  32. #include <shlObj.h>
  33. static bool miniDumpWritten = false;
  34. int WriteMiniDump(const char* applicationName, void* exceptionPointers)
  35. {
  36. // In case of recursive or repeating exceptions, only write the dump once
  37. /// \todo This function should not allocate any dynamic memory
  38. if (miniDumpWritten)
  39. return EXCEPTION_EXECUTE_HANDLER;
  40. miniDumpWritten = true;
  41. MINIDUMP_EXCEPTION_INFORMATION info;
  42. info.ThreadId = GetCurrentThreadId();
  43. info.ExceptionPointers = (EXCEPTION_POINTERS*)exceptionPointers;
  44. info.ClientPointers = TRUE;
  45. static time_t sysTime;
  46. time(&sysTime);
  47. const char* dateTime = ctime(&sysTime);
  48. String dateTimeStr = String(dateTime);
  49. dateTimeStr.Replace("\n", "");
  50. dateTimeStr.Replace(":", "");
  51. dateTimeStr.Replace("/", "");
  52. dateTimeStr.Replace(' ', '_');
  53. wchar_t pathName[MAX_PATH];
  54. pathName[0] = 0;
  55. SHGetSpecialFolderPathW(0, pathName, CSIDL_PERSONAL, 0);
  56. String applicationNameStr(applicationName);
  57. String miniDumpDir = String(pathName) + "\\" + applicationNameStr;
  58. String miniDumpName = miniDumpDir + "\\" + applicationNameStr + "_" + dateTimeStr + ".dmp";
  59. CreateDirectoryW(WString(miniDumpDir).CString(), 0);
  60. HANDLE file = CreateFileW(WString(miniDumpName).CString(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ,
  61. 0, CREATE_ALWAYS, 0, 0);
  62. BOOL success = MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), file, MiniDumpWithDataSegs, &info, 0, 0);
  63. CloseHandle(file);
  64. if (success)
  65. ErrorDialog(applicationName, "An unexpected error occurred. A minidump was generated to " + miniDumpName);
  66. else
  67. ErrorDialog(applicationName, "An unexpected error occurred. Could not write minidump.");
  68. return EXCEPTION_EXECUTE_HANDLER;
  69. }
  70. #endif