ProcessUtils.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. #include "Mutex.h"
  25. #include "ProcessUtils.h"
  26. #include "StringUtils.h"
  27. #include <cstdio>
  28. #include <list>
  29. #include <io.h>
  30. #include <fcntl.h>
  31. #include <time.h>
  32. #include <Windows.h>
  33. // Enable SHGetSpecialFolderPath on MinGW
  34. #ifndef _MSC_VER
  35. #define _WIN32_IE 0x0400
  36. #endif
  37. #include <shlobj.h>
  38. #ifdef ENABLE_MINIDUMPS
  39. #include <dbghelp.h>
  40. #endif
  41. #include "DebugNew.h"
  42. static bool miniDumpWritten = false;
  43. static bool consoleOpened = false;
  44. static std::string currentLine;
  45. static std::vector<std::string> arguments;
  46. static Mutex staticMutex;
  47. void ErrorDialog(const char* title, const char* message)
  48. {
  49. MessageBox(0, message, title, 0);
  50. }
  51. void OpenConsoleWindow()
  52. {
  53. if (consoleOpened)
  54. return;
  55. AllocConsole();
  56. HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  57. int hCrt = _open_osfhandle((long)hOut, _O_TEXT);
  58. FILE* outFile = _fdopen(hCrt, "w");
  59. setvbuf(outFile, NULL, _IONBF, 1);
  60. *stdout = *outFile;
  61. HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
  62. hCrt = _open_osfhandle((long)hIn, _O_TEXT);
  63. FILE* inFile = _fdopen(hCrt, "r");
  64. setvbuf(inFile, NULL, _IONBF, 128);
  65. *stdin = *inFile;
  66. consoleOpened = true;
  67. }
  68. Mutex& GetStaticMutex()
  69. {
  70. return staticMutex;
  71. }
  72. const std::vector<std::string>& ParseArguments(const char* cmdLine)
  73. {
  74. arguments.clear();
  75. if (!cmdLine)
  76. return arguments;
  77. std::string cmdStr(cmdLine);
  78. unsigned cmdStart, cmdEnd;
  79. bool inCmd = false;
  80. bool inQuote = false;
  81. for (unsigned i = 0; i < cmdStr.length(); ++i)
  82. {
  83. if (cmdStr[i] == '\"')
  84. inQuote = !inQuote;
  85. if ((cmdStr[i] == ' ') && (!inQuote))
  86. {
  87. if (inCmd)
  88. {
  89. inCmd = false;
  90. cmdEnd = i;
  91. arguments.push_back(cmdStr.substr(cmdStart, cmdEnd - cmdStart));
  92. }
  93. }
  94. else
  95. {
  96. if (!inCmd)
  97. {
  98. inCmd = true;
  99. cmdStart = i;
  100. }
  101. }
  102. }
  103. if (inCmd)
  104. {
  105. cmdEnd = cmdStr.length();
  106. arguments.push_back(cmdStr.substr(cmdStart, cmdEnd - cmdStart));
  107. }
  108. return arguments;
  109. }
  110. const std::vector<std::string>& GetArguments()
  111. {
  112. return arguments;
  113. }
  114. std::string GetConsoleInput()
  115. {
  116. std::string ret;
  117. HANDLE input = GetStdHandle(STD_INPUT_HANDLE);
  118. if (input == INVALID_HANDLE_VALUE)
  119. return ret;
  120. // Use char-based input
  121. SetConsoleMode(input, ENABLE_PROCESSED_INPUT);
  122. INPUT_RECORD record;
  123. DWORD events = 0;
  124. DWORD readEvents = 0;
  125. if (!GetNumberOfConsoleInputEvents(input, &events))
  126. return ret;
  127. while (events--)
  128. {
  129. ReadConsoleInput(input, &record, 1, &readEvents);
  130. if ((record.EventType == KEY_EVENT) && (record.Event.KeyEvent.bKeyDown))
  131. {
  132. char c = record.Event.KeyEvent.uChar.AsciiChar;
  133. if (c)
  134. {
  135. if (c == '\b')
  136. {
  137. printf("\b \b");
  138. int length = currentLine.length();
  139. if (length)
  140. currentLine.resize(length - 1);
  141. }
  142. else if (c == '\r')
  143. {
  144. printf("\n");
  145. ret = currentLine;
  146. currentLine.clear();
  147. return ret;
  148. }
  149. else
  150. {
  151. // We have disabled echo, so echo manually
  152. printf("%c", c);
  153. currentLine += c;
  154. }
  155. }
  156. }
  157. }
  158. return ret;
  159. }
  160. unsigned GetNumCPUCores()
  161. {
  162. SYSTEM_INFO info;
  163. GetSystemInfo(&info);
  164. return info.dwNumberOfProcessors;
  165. }
  166. #ifdef ENABLE_MINIDUMPS
  167. int WriteMiniDump(const char* applicationName, void* exceptionPointers)
  168. {
  169. // In case of recursive or repeating exceptions, only write the dump once
  170. if (miniDumpWritten)
  171. return EXCEPTION_EXECUTE_HANDLER;
  172. miniDumpWritten = true;
  173. MINIDUMP_EXCEPTION_INFORMATION info;
  174. info.ThreadId = GetCurrentThreadId();
  175. info.ExceptionPointers = (EXCEPTION_POINTERS*)exceptionPointers;
  176. info.ClientPointers = TRUE;
  177. static time_t sysTime;
  178. time(&sysTime);
  179. const char* dateTime = ctime(&sysTime);
  180. std::string dateTimeStr = std::string(dateTime);
  181. ReplaceInPlace(dateTimeStr, "\n", "");
  182. ReplaceInPlace(dateTimeStr, ":", "");
  183. ReplaceInPlace(dateTimeStr, "/", "");
  184. ReplaceInPlace(dateTimeStr, ' ', '_');
  185. char pathName[MAX_PATH];
  186. pathName[0] = 0;
  187. SHGetSpecialFolderPath(0, pathName, CSIDL_PERSONAL, 0);
  188. std::string applicationNameStr(applicationName);
  189. std::string miniDumpDir = std::string(pathName) + "\\" + applicationNameStr;
  190. std::string miniDumpName = miniDumpDir + "\\" + applicationNameStr + "_" + dateTimeStr + ".dmp";
  191. CreateDirectory(miniDumpDir.c_str(), 0);
  192. HANDLE file = CreateFile(miniDumpName.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ,
  193. 0, CREATE_ALWAYS, 0, 0);
  194. BOOL success = MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), file, MiniDumpWithDataSegs, &info, 0, 0);
  195. CloseHandle(file);
  196. if (success)
  197. ErrorDialog(applicationName, std::string("An unexpected error occurred. A minidump was generated to " + miniDumpName).c_str());
  198. else
  199. ErrorDialog(applicationName, "An unexpected error occurred. Could not write minidump.");
  200. return EXCEPTION_EXECUTE_HANDLER;
  201. }
  202. #endif