RemoteDebuggerBase.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _REMOTE_DEBUGGER_BASE_H_
  23. #include "debug/remote/RemoteDebuggerBase.h"
  24. #endif
  25. #ifndef _REMOTE_DEBUGGER_BRIDGE_H_
  26. #include "debug/remote/RemoteDebuggerBridge.h"
  27. #endif
  28. #ifndef _FRAMEALLOCATOR_H_
  29. #include "memory/frameAllocator.h"
  30. #endif
  31. #ifndef _SIMBASE_H_
  32. #include "sim/simBase.h"
  33. #endif
  34. #ifndef _CODEBLOCK_H_
  35. #include "console/codeBlock.h"
  36. #endif
  37. // Script bindings.
  38. #include "debug/remote/RemoteDebuggerBase_ScriptBinding.h"
  39. //-----------------------------------------------------------------------------
  40. IMPLEMENT_CONOBJECT(RemoteDebuggerBase);
  41. //-----------------------------------------------------------------------------
  42. RemoteDebuggerBase::RemoteDebuggerBase() :
  43. mClientSocket( NetSocket::INVALID ),
  44. mClientAuthenticated( false ),
  45. mReceiveCommandCursor( 0 )
  46. {
  47. // Turn-on tick processing.
  48. setProcessTicks( true );
  49. }
  50. //-----------------------------------------------------------------------------
  51. RemoteDebuggerBase::~RemoteDebuggerBase()
  52. {
  53. }
  54. //-----------------------------------------------------------------------------
  55. bool RemoteDebuggerBase::login( const char* pPassword )
  56. {
  57. // Set client authentication.
  58. mClientAuthenticated = ( dStrcmp( RemoteDebuggerBridge::getConnectionPassword(), pPassword ) == 0 );
  59. // Was the client authenticated?
  60. if ( mClientAuthenticated )
  61. {
  62. // Yes, so perform the client log-in callback.
  63. onClientLogin();
  64. // Info.
  65. Con::printf( "Client authenticated on remote debugger." );
  66. }
  67. else
  68. {
  69. // No, so warn.
  70. Con::warnf( "Client failed authentication on remote debugger." );
  71. }
  72. return mClientAuthenticated;
  73. }
  74. //-----------------------------------------------------------------------------
  75. bool RemoteDebuggerBase::addCodeBlock( CodeBlock* pCodeBlock )
  76. {
  77. // Finish if client it not authenticated.
  78. if ( !isClientAuthenticated() )
  79. return false;
  80. #if 1
  81. Con::printf( "+ AddCodeBlock: [%s] %s", pCodeBlock->name, pCodeBlock->fullPath );
  82. //Con::printSeparator();
  83. //for( U32 breakEntry = 0, breakIndex = 0; breakEntry < pCodeBlock->lineBreakPairCount; breakEntry++, breakIndex += 2 )
  84. //{
  85. // Con::printf( "Line: %d, IP: %d", pCodeBlock->lineBreakPairs[breakIndex] >> 8, pCodeBlock->lineBreakPairs[breakIndex+1] );
  86. //}
  87. #else
  88. for( CodeBlock* pCodeBlock = CodeBlock::getCodeBlockList(); pCodeBlock != NULL; pCodeBlock = pCodeBlock->nextFile )
  89. {
  90. Con::printf( "%s", pCodeBlock->fullPath );
  91. Con::printSeparator();
  92. for( U32 breakEntry = 0, breakIndex = 0; breakEntry < pCodeBlock->lineBreakPairCount; breakEntry++, breakIndex += 2 )
  93. {
  94. Con::printf( "Line: %d, IP: %d", pCodeBlock->lineBreakPairs[breakIndex] >> 8, pCodeBlock->lineBreakPairs[breakIndex+1] );
  95. }
  96. Con::printSeparator();
  97. }
  98. #endif
  99. return true;
  100. }
  101. //-----------------------------------------------------------------------------
  102. bool RemoteDebuggerBase::removeCodeBlock( CodeBlock* pCodeBlock )
  103. {
  104. // Finish if client it not authenticated.
  105. if ( !isClientAuthenticated() )
  106. return false;
  107. Con::printf( "- RemoveCodeBlock: [%s] %s", pCodeBlock->name, pCodeBlock->fullPath );
  108. return true;
  109. }
  110. //-----------------------------------------------------------------------------
  111. bool RemoteDebuggerBase::pushStackFrame( void )
  112. {
  113. // Finish if client it not authenticated.
  114. if ( !isClientAuthenticated() )
  115. return false;
  116. return true;
  117. }
  118. //-----------------------------------------------------------------------------
  119. bool RemoteDebuggerBase::popStackFrame( void )
  120. {
  121. // Finish if client it not authenticated.
  122. if ( !isClientAuthenticated() )
  123. return false;
  124. return true;
  125. }
  126. //-----------------------------------------------------------------------------
  127. bool RemoteDebuggerBase::executionStopped( CodeBlock *code, U32 lineNumber )
  128. {
  129. // Finish if client it not authenticated.
  130. if ( !isClientAuthenticated() )
  131. return false;
  132. return true;
  133. }
  134. //-----------------------------------------------------------------------------
  135. RemoteDebuggerBase* RemoteDebuggerBase::getRemoteDebugger( void )
  136. {
  137. return Sim::findObject<RemoteDebuggerBase>( REMOTE_DEBUGGER_NAME );
  138. }
  139. //-----------------------------------------------------------------------------
  140. void RemoteDebuggerBase::processTick( void )
  141. {
  142. // Finish if the client socket is invalid.
  143. if ( mClientSocket == NetSocket::INVALID )
  144. return;
  145. // Calculate read point.
  146. char* pReadPoint = (mReceiveCommandBuffer + mReceiveCommandCursor);
  147. // Read from the socket.
  148. S32 readCount;
  149. Net::Error readStatus = Net::recv(mClientSocket, (U8*)pReadPoint, sizeof(mReceiveCommandBuffer)-mReceiveCommandCursor, &readCount);
  150. // Is the connection invalid?
  151. if ( readCount == 0 || (readStatus != Net::NoError && readStatus != Net::WouldBlock) )
  152. {
  153. // Yes, so terminate it.
  154. setProcessTicks(false);
  155. RemoteDebuggerBridge::close();
  156. return;
  157. }
  158. // Finish if the read would've blocked.
  159. if ( readStatus == Net::WouldBlock )
  160. return;
  161. // Process last read segment.
  162. for( S32 index = 0; index < readCount; ++index )
  163. {
  164. // Fetch character.
  165. const char character = pReadPoint[index];
  166. // Skip if this is not a command termination character.
  167. if ( character != '\r' && character != '\n' )
  168. continue;
  169. // Yes, so terminate command.
  170. pReadPoint[index] = 0;
  171. // Receive the command.
  172. receiveCommand( mReceiveCommandBuffer );
  173. // Search for any trailing characters after the command.
  174. for ( S32 trailIndex = index+1; trailIndex < readCount; ++trailIndex )
  175. {
  176. // Fetch trail character.
  177. const char trailCharacter = pReadPoint[trailIndex];
  178. // Skip if this is a command termination character.
  179. if ( trailCharacter == '\r' || trailCharacter == '\n' )
  180. continue;
  181. // Calculate remaining command characters.
  182. mReceiveCommandCursor = readCount-trailIndex;
  183. // Move the trailing characters to the start of the command buffer.
  184. dMemmove( mReceiveCommandBuffer, pReadPoint+trailIndex, mReceiveCommandCursor );
  185. // Finish!
  186. return;
  187. }
  188. // Reset receive command cursor.
  189. mReceiveCommandCursor = 0;
  190. // Finish!
  191. return;
  192. }
  193. // Move receive cursor.
  194. mReceiveCommandCursor += readCount;
  195. // Is the receive buffer full?
  196. if ( mReceiveCommandCursor >= sizeof(mReceiveCommandBuffer) )
  197. {
  198. // Yes, so warn.
  199. Con::warnf( "%s - Command receive buffer is full! Resetting buffer.", getClassName() );
  200. mReceiveCommandCursor = 0;
  201. }
  202. }
  203. //-----------------------------------------------------------------------------
  204. void RemoteDebuggerBase::receiveCommand( const char* pCommand )
  205. {
  206. // Sanity!
  207. AssertFatal( pCommand != NULL, "Remote debugger command cannot be NULL." );
  208. // Is the client authenticated?
  209. if ( mClientAuthenticated )
  210. {
  211. // Yes, so finish if no command available.
  212. if ( dStrlen(pCommand) == 0 )
  213. return;
  214. // Evaluate the command.
  215. const char* pReturnValue = Con::evaluatef( pCommand );
  216. // Send the return value if it exists.
  217. sendCommand( pReturnValue );
  218. return;
  219. }
  220. // Attempt authentication with the received command.
  221. sendCommand( login( pCommand ) ? "1" : "0" );
  222. }
  223. //-----------------------------------------------------------------------------
  224. bool RemoteDebuggerBase::sendCommand( const char* pCommand )
  225. {
  226. // Is the client socket valid?
  227. if ( mClientSocket == NetSocket::INVALID )
  228. {
  229. // No, so warn.
  230. Con::warnf( "Cannot send command with invalid client socket." );
  231. return false;
  232. }
  233. // Fetch command length.
  234. const S32 commandLength = dStrlen(pCommand);
  235. // Calculate required send response size.
  236. // This size is the original command response plus termination null plus an extra for the newline command termination.
  237. const S32 requiredSendResponseBufferSize = commandLength+3;
  238. // Create response buffer.
  239. FrameTemp<char> sendResponseBuffer( requiredSendResponseBufferSize );
  240. // Append carriage-return to send command.
  241. dSprintf( sendResponseBuffer, requiredSendResponseBufferSize, "%s\n\r", pCommand );
  242. // Send the command.
  243. Net::send( mClientSocket, (const U8*)REMOTE_DEBUGGER_RESPONSE_START, dStrlen(REMOTE_DEBUGGER_RESPONSE_START) );
  244. Net::send( mClientSocket, (const U8*)~sendResponseBuffer, requiredSendResponseBufferSize-1 );
  245. Net::send( mClientSocket, (const U8*)REMOTE_DEBUGGER_RESPONSE_END, dStrlen(REMOTE_DEBUGGER_RESPONSE_END) );
  246. return true;
  247. }