ProcessUtils.cpp 6.8 KB

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