ProcessUtils.cpp 7.1 KB

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