debugger.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "../../sys/win32/rc/debugger_resource.h"
  23. #include "DebuggerApp.h"
  24. #include "DebuggerServer.h"
  25. DWORD CALLBACK DebuggerThread ( LPVOID param );
  26. rvDebuggerApp gDebuggerApp;
  27. HWND gDebuggerWindow = NULL;
  28. bool gDebuggerSuspend = false;
  29. bool gDebuggerConnnected = false;
  30. HANDLE gDebuggerGameThread = NULL;
  31. rvDebuggerServer* gDebuggerServer = NULL;
  32. HANDLE gDebuggerServerThread = NULL;
  33. DWORD gDebuggerServerThreadID = 0;
  34. bool gDebuggerServerQuit = false;
  35. /*
  36. ================
  37. DebuggerMain
  38. Main entry point for the debugger application
  39. ================
  40. */
  41. void DebuggerClientInit( const char *cmdline )
  42. {
  43. // See if the debugger is already running
  44. if ( rvDebuggerWindow::Activate ( ) )
  45. {
  46. goto DebuggerClientInitDone;
  47. }
  48. if ( !gDebuggerApp.Initialize ( win32.hInstance ) )
  49. {
  50. goto DebuggerClientInitDone;
  51. }
  52. gDebuggerApp.Run ( );
  53. DebuggerClientInitDone:
  54. common->Quit();
  55. }
  56. /*
  57. ================
  58. DebuggerLaunch
  59. Launches another instance of the running executable with +debugger appended
  60. to the end to indicate that the debugger should start up.
  61. ================
  62. */
  63. void DebuggerClientLaunch ( void )
  64. {
  65. if ( renderSystem->IsFullScreen() ) {
  66. common->Printf( "Cannot run the script debugger in fullscreen mode.\n"
  67. "Set r_fullscreen to 0 and vid_restart.\n" );
  68. return;
  69. }
  70. // See if the debugger is already running
  71. if ( rvDebuggerWindow::Activate ( ) ) {
  72. return;
  73. }
  74. char exeFile[MAX_PATH];
  75. char curDir[MAX_PATH];
  76. STARTUPINFO startup;
  77. PROCESS_INFORMATION process;
  78. ZeroMemory ( &startup, sizeof(startup) );
  79. startup.cb = sizeof(startup);
  80. GetCurrentDirectory ( MAX_PATH, curDir );
  81. GetModuleFileName ( NULL, exeFile, MAX_PATH );
  82. const char* s = va("%s +set fs_game %s +set fs_cdpath %s +debugger", exeFile, cvarSystem->GetCVarString( "fs_game" ), cvarSystem->GetCVarString( "fs_cdpath" ) );
  83. CreateProcess ( NULL, (LPSTR)s,
  84. NULL, NULL, FALSE, 0, NULL, curDir, &startup, &process );
  85. CloseHandle ( process.hThread );
  86. CloseHandle ( process.hProcess );
  87. }
  88. /*
  89. ================
  90. DebuggerServerThread
  91. Thread proc for the debugger server
  92. ================
  93. */
  94. DWORD CALLBACK DebuggerServerThread ( LPVOID param )
  95. {
  96. assert ( gDebuggerServer );
  97. while ( !gDebuggerServerQuit )
  98. {
  99. gDebuggerServer->ProcessMessages ( );
  100. Sleep ( 1 );
  101. }
  102. return 0;
  103. }
  104. /*
  105. ================
  106. DebuggerServerInit
  107. Starts up the debugger server
  108. ================
  109. */
  110. bool DebuggerServerInit ( void )
  111. {
  112. // Dont do this if we are in the debugger already
  113. if ( com_editors & EDITOR_DEBUGGER )
  114. {
  115. return false;
  116. }
  117. // Allocate the new debugger server
  118. gDebuggerServer = new rvDebuggerServer;
  119. if ( !gDebuggerServer )
  120. {
  121. return false;
  122. }
  123. // Initialize the debugger server
  124. if ( !gDebuggerServer->Initialize ( ) )
  125. {
  126. delete gDebuggerServer;
  127. gDebuggerServer = NULL;
  128. return false;
  129. }
  130. // Start the debugger server thread
  131. gDebuggerServerThread = CreateThread ( NULL, 0, DebuggerServerThread, 0, 0, &gDebuggerServerThreadID );
  132. return true;
  133. }
  134. /*
  135. ================
  136. DebuggerServerShutdown
  137. Shuts down the debugger server
  138. ================
  139. */
  140. void DebuggerServerShutdown ( void )
  141. {
  142. if ( gDebuggerServerThread )
  143. {
  144. // Signal the debugger server to quit
  145. gDebuggerServerQuit = true;
  146. // Wait for the thread to finish
  147. WaitForSingleObject ( gDebuggerServerThread, INFINITE );
  148. // Shutdown the server now
  149. gDebuggerServer->Shutdown();
  150. delete gDebuggerServer;
  151. gDebuggerServer = NULL;
  152. // Cleanup the thread handle
  153. CloseHandle ( gDebuggerServerThread );
  154. gDebuggerServerThread = NULL;
  155. }
  156. }
  157. /*
  158. ================
  159. DebuggerServerCheckBreakpoint
  160. Check to see if there is a breakpoint associtated with this statement
  161. ================
  162. */
  163. void DebuggerServerCheckBreakpoint ( idInterpreter* interpreter, idProgram* program, int instructionPointer )
  164. {
  165. if ( !gDebuggerServer )
  166. {
  167. return;
  168. }
  169. gDebuggerServer->CheckBreakpoints ( interpreter, program, instructionPointer );
  170. }
  171. /*
  172. ================
  173. DebuggerServerPrint
  174. Sends a print message to the debugger client
  175. ================
  176. */
  177. void DebuggerServerPrint ( const char* text )
  178. {
  179. if ( !gDebuggerServer )
  180. {
  181. return;
  182. }
  183. gDebuggerServer->Print ( text );
  184. }