MiniDump.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  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. namespace Urho3D
  34. {
  35. static bool miniDumpWritten = false;
  36. int WriteMiniDump(const char* applicationName, void* exceptionPointers)
  37. {
  38. // In case of recursive or repeating exceptions, only write the dump once
  39. /// \todo This function should not allocate any dynamic memory
  40. if (miniDumpWritten)
  41. return EXCEPTION_EXECUTE_HANDLER;
  42. miniDumpWritten = true;
  43. MINIDUMP_EXCEPTION_INFORMATION info;
  44. info.ThreadId = GetCurrentThreadId();
  45. info.ExceptionPointers = (EXCEPTION_POINTERS*)exceptionPointers;
  46. info.ClientPointers = TRUE;
  47. static time_t sysTime;
  48. time(&sysTime);
  49. const char* dateTime = ctime(&sysTime);
  50. String dateTimeStr = String(dateTime);
  51. dateTimeStr.Replace("\n", "");
  52. dateTimeStr.Replace(":", "");
  53. dateTimeStr.Replace("/", "");
  54. dateTimeStr.Replace(' ', '_');
  55. wchar_t pathName[MAX_PATH];
  56. pathName[0] = 0;
  57. SHGetSpecialFolderPathW(0, pathName, CSIDL_PERSONAL, 0);
  58. String applicationNameStr(applicationName);
  59. String miniDumpDir = String(pathName) + "\\" + applicationNameStr;
  60. String miniDumpName = miniDumpDir + "\\" + applicationNameStr + "_" + dateTimeStr + ".dmp";
  61. CreateDirectoryW(WString(miniDumpDir).CString(), 0);
  62. HANDLE file = CreateFileW(WString(miniDumpName).CString(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ,
  63. 0, CREATE_ALWAYS, 0, 0);
  64. BOOL success = MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), file, MiniDumpWithDataSegs, &info, 0, 0);
  65. CloseHandle(file);
  66. if (success)
  67. ErrorDialog(applicationName, "An unexpected error occurred. A minidump was generated to " + miniDumpName);
  68. else
  69. ErrorDialog(applicationName, "An unexpected error occurred. Could not write minidump.");
  70. return EXCEPTION_EXECUTE_HANDLER;
  71. }
  72. }
  73. #endif