MiniDump.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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. #ifdef ENABLE_MINIDUMPS
  25. #include "ProcessUtils.h"
  26. #include <cstdio>
  27. #include <list>
  28. #include <io.h>
  29. #include <fcntl.h>
  30. #include <time.h>
  31. #include <Windows.h>
  32. #include <DbgHelp.h>
  33. // Enable SHGetSpecialFolderPath on MinGW
  34. #ifndef _MSC_VER
  35. #define _WIN32_IE 0x0400
  36. #endif
  37. #include <ShlObj.h>
  38. static bool miniDumpWritten = false;
  39. int WriteMiniDump(const char* applicationName, void* exceptionPointers)
  40. {
  41. // In case of recursive or repeating exceptions, only write the dump once
  42. if (miniDumpWritten)
  43. return EXCEPTION_EXECUTE_HANDLER;
  44. miniDumpWritten = true;
  45. MINIDUMP_EXCEPTION_INFORMATION info;
  46. info.ThreadId = GetCurrentThreadId();
  47. info.ExceptionPointers = (EXCEPTION_POINTERS*)exceptionPointers;
  48. info.ClientPointers = TRUE;
  49. static time_t sysTime;
  50. time(&sysTime);
  51. const char* dateTime = ctime(&sysTime);
  52. String dateTimeStr = String(dateTime);
  53. dateTimeStr.Replace("\n", "");
  54. dateTimeStr.Replace(":", "");
  55. dateTimeStr.Replace("/", "");
  56. dateTimeStr.Replace(' ', '_');
  57. char pathName[MAX_PATH];
  58. pathName[0] = 0;
  59. SHGetSpecialFolderPath(0, pathName, CSIDL_PERSONAL, 0);
  60. String applicationNameStr(applicationName);
  61. String miniDumpDir = String(pathName) + "\\" + applicationNameStr;
  62. String miniDumpName = miniDumpDir + "\\" + applicationNameStr + "_" + dateTimeStr + ".dmp";
  63. CreateDirectory(miniDumpDir.CString(), 0);
  64. HANDLE file = CreateFile(miniDumpName.CString(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ,
  65. 0, CREATE_ALWAYS, 0, 0);
  66. BOOL success = MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), file, MiniDumpWithDataSegs, &info, 0, 0);
  67. CloseHandle(file);
  68. if (success)
  69. ErrorDialog(applicationName, String("An unexpected error occurred. A minidump was generated to " + miniDumpName).CString());
  70. else
  71. ErrorDialog(applicationName, "An unexpected error occurred. Could not write minidump.");
  72. return EXCEPTION_EXECUTE_HANDLER;
  73. }
  74. #endif