ProcessUtils.cpp 7.0 KB

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