MiniDump.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // Copyright (c) 2008-2017 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. #if defined(_MSC_VER) && defined(ATOMIC_MINIDUMPS)
  23. #include "../Precompiled.h"
  24. #include "../Core/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. namespace Atomic
  32. {
  33. ATOMIC_API int WriteMiniDump(const char* applicationName, void* exceptionPointers)
  34. {
  35. static bool miniDumpWritten = false;
  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. String miniDumpDir = GetMiniDumpDir();
  54. String miniDumpName = miniDumpDir + String(applicationName) + "_" + dateTimeStr + ".dmp";
  55. CreateDirectoryW(WString(miniDumpDir).CString(), 0);
  56. HANDLE file = CreateFileW(WString(miniDumpName).CString(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ,
  57. 0, CREATE_ALWAYS, 0, 0);
  58. BOOL success = MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), file, MiniDumpWithDataSegs, &info, 0, 0);
  59. CloseHandle(file);
  60. if (success)
  61. ErrorDialog(applicationName, "An unexpected error occurred. A minidump was generated to " + miniDumpName);
  62. else
  63. ErrorDialog(applicationName, "An unexpected error occurred. Could not write minidump.");
  64. return EXCEPTION_EXECUTE_HANDLER;
  65. }
  66. }
  67. #endif