MiniDump.cpp 3.2 KB

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