telnetDebugger.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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. #include "platform/platform.h"
  23. #include "console/telnetDebugger.h"
  24. #include "core/frameAllocator.h"
  25. #include "console/console.h"
  26. #include "console/engineApi.h"
  27. #include "core/stringTable.h"
  28. #include "console/consoleInternal.h"
  29. #include "console/ast.h"
  30. #include "console/compiler.h"
  31. #include "core/util/journal/process.h"
  32. #include "core/module.h"
  33. MODULE_BEGIN( TelnetDebugger )
  34. MODULE_INIT
  35. {
  36. TelnetDebugger::create();
  37. }
  38. MODULE_SHUTDOWN
  39. {
  40. TelnetDebugger::destroy();
  41. }
  42. MODULE_END;
  43. //
  44. // Enhanced TelnetDebugger for Torsion
  45. // http://www.sickheadgames.com/torsion
  46. //
  47. //
  48. // Debugger commands:
  49. //
  50. // CEVAL console line - evaluate the console line
  51. // output: none
  52. //
  53. // BRKVARSET varName passct expr - NOT IMPLEMENTED!
  54. // output: none
  55. //
  56. // BRKVARCLR varName - NOT IMPLEMENTED!
  57. // output: none
  58. //
  59. // BRKSET file line clear passct expr - set a breakpoint on the file,line
  60. // it must pass passct times for it to break and if clear is true, it
  61. // clears when hit
  62. // output:
  63. //
  64. // BRKNEXT - stop execution at the next breakable line.
  65. // output: none
  66. //
  67. // BRKCLR file line - clear a breakpoint on the file,line
  68. // output: none
  69. //
  70. // BRKCLRALL - clear all breakpoints
  71. // output: none
  72. //
  73. // CONTINUE - continue execution
  74. // output: RUNNING
  75. //
  76. // STEPIN - run until next statement
  77. // output: RUNNING
  78. //
  79. // STEPOVER - run until next break <= current frame
  80. // output: RUNNING
  81. //
  82. // STEPOUT - run until next break <= current frame - 1
  83. // output: RUNNING
  84. //
  85. // EVAL tag frame expr - evaluate the expr in the console, on the frame'th stack frame
  86. // output: EVALOUT tag exprResult
  87. //
  88. // FILELIST - list script files loaded
  89. // output: FILELISTOUT file1 file2 file3 file4 ...
  90. //
  91. // BREAKLIST file - get a list of breakpoint-able lines in the file
  92. // output: BREAKLISTOUT file skipBreakPairs skiplinecount breaklinecount skiplinecount breaklinecount ...
  93. //
  94. //
  95. // Other output:
  96. //
  97. // BREAK file1 line1 function1 file2 line2 function2 ... - Sent when the debugger hits a
  98. // breakpoint. It lists out one file/line/function triplet for each stack level.
  99. // The first one is the top of the stack.
  100. //
  101. // COUT console-output - echo of console output from engine
  102. //
  103. // BRKMOV file line newline - sent when a breakpoint is moved to a breakable line.
  104. //
  105. // BRKCLR file line - sent when a breakpoint cannot be moved to a breakable line on the client.
  106. //
  107. DefineConsoleFunction( dbgSetParameters, void, (S32 port, const char * password, bool waitForClient ), (false), "( int port, string password, bool waitForClient )"
  108. "Open a debug server port on the specified port, requiring the specified password, "
  109. "and optionally waiting for the debug client to connect.\n"
  110. "@internal Primarily used for Torsion and other debugging tools")
  111. {
  112. if (TelDebugger)
  113. {
  114. TelDebugger->setDebugParameters(port, password, waitForClient );
  115. }
  116. }
  117. DefineConsoleFunction( dbgIsConnected, bool, (), , "()"
  118. "Returns true if a script debugging client is connected else return false.\n"
  119. "@internal Primarily used for Torsion and other debugging tools")
  120. {
  121. return TelDebugger && TelDebugger->isConnected();
  122. }
  123. DefineConsoleFunction( dbgDisconnect, void, (), , "()"
  124. "Forcibly disconnects any attached script debugging client.\n"
  125. "@internal Primarily used for Torsion and other debugging tools")
  126. {
  127. if (TelDebugger)
  128. TelDebugger->disconnect();
  129. }
  130. static void debuggerConsumer(U32 level, const char *line)
  131. {
  132. TORQUE_UNUSED(level);
  133. if (TelDebugger)
  134. TelDebugger->processConsoleLine(line);
  135. }
  136. TelnetDebugger::TelnetDebugger()
  137. {
  138. Con::addConsumer(debuggerConsumer);
  139. mAcceptPort = -1;
  140. mAcceptSocket = InvalidSocket;
  141. mDebugSocket = InvalidSocket;
  142. mState = NotConnected;
  143. mCurPos = 0;
  144. mBreakpoints = NULL;
  145. mBreakOnNextStatement = false;
  146. mStackPopBreakIndex = -1;
  147. mProgramPaused = false;
  148. mWaitForClient = false;
  149. // Add the version number in a global so that
  150. // scripts can detect the presence of the
  151. // "enhanced" debugger features.
  152. Con::evaluatef( "$dbgVersion = %d;", Version );
  153. }
  154. TelnetDebugger::Breakpoint **TelnetDebugger::findBreakpoint(StringTableEntry fileName, S32 lineNumber)
  155. {
  156. Breakpoint **walk = &mBreakpoints;
  157. Breakpoint *cur;
  158. while((cur = *walk) != NULL)
  159. {
  160. // TODO: This assumes that the OS file names are case
  161. // insensitive... Torque needs a dFilenameCmp() function.
  162. if( dStricmp( cur->fileName, fileName ) == 0 && cur->lineNumber == U32(lineNumber))
  163. return walk;
  164. walk = &cur->next;
  165. }
  166. return NULL;
  167. }
  168. TelnetDebugger::~TelnetDebugger()
  169. {
  170. Con::removeConsumer(debuggerConsumer);
  171. if(mAcceptSocket != InvalidSocket)
  172. Net::closeSocket(mAcceptSocket);
  173. if(mDebugSocket != InvalidSocket)
  174. Net::closeSocket(mDebugSocket);
  175. }
  176. TelnetDebugger *TelDebugger = NULL;
  177. void TelnetDebugger::create()
  178. {
  179. TelDebugger = new TelnetDebugger;
  180. Process::notify(TelDebugger, &TelnetDebugger::process, PROCESS_FIRST_ORDER);
  181. }
  182. void TelnetDebugger::destroy()
  183. {
  184. Process::remove(TelDebugger, &TelnetDebugger::process);
  185. delete TelDebugger;
  186. TelDebugger = NULL;
  187. }
  188. void TelnetDebugger::send(const char *str)
  189. {
  190. Net::send(mDebugSocket, (const unsigned char*)str, dStrlen(str));
  191. }
  192. void TelnetDebugger::disconnect()
  193. {
  194. if ( mDebugSocket != InvalidSocket )
  195. {
  196. Net::closeSocket(mDebugSocket);
  197. mDebugSocket = InvalidSocket;
  198. }
  199. removeAllBreakpoints();
  200. mState = NotConnected;
  201. mProgramPaused = false;
  202. }
  203. void TelnetDebugger::setDebugParameters(S32 port, const char *password, bool waitForClient)
  204. {
  205. // Don't bail if same port... we might just be wanting to change
  206. // the password.
  207. // if(port == mAcceptPort)
  208. // return;
  209. if(mAcceptSocket != InvalidSocket)
  210. {
  211. Net::closeSocket(mAcceptSocket);
  212. mAcceptSocket = InvalidSocket;
  213. }
  214. mAcceptPort = port;
  215. if(mAcceptPort != -1 && mAcceptPort != 0)
  216. {
  217. mAcceptSocket = Net::openSocket();
  218. Net::bind(mAcceptSocket, mAcceptPort);
  219. Net::listen(mAcceptSocket, 4);
  220. Net::setBlocking(mAcceptSocket, false);
  221. }
  222. dStrncpy(mDebuggerPassword, password, PasswordMaxLength);
  223. mWaitForClient = waitForClient;
  224. if ( !mWaitForClient )
  225. return;
  226. // Wait for the client to fully connect.
  227. while ( mState != Connected )
  228. {
  229. Platform::sleep(10);
  230. process();
  231. }
  232. }
  233. void TelnetDebugger::processConsoleLine(const char *consoleLine)
  234. {
  235. if(mState != NotConnected)
  236. {
  237. send("COUT ");
  238. send(consoleLine);
  239. send("\r\n");
  240. }
  241. }
  242. void TelnetDebugger::process()
  243. {
  244. NetAddress address;
  245. if(mAcceptSocket != InvalidSocket)
  246. {
  247. // ok, see if we have any new connections:
  248. NetSocket newConnection;
  249. newConnection = Net::accept(mAcceptSocket, &address);
  250. if(newConnection != InvalidSocket && mDebugSocket == InvalidSocket)
  251. {
  252. Con::printf ("Debugger connection from %i.%i.%i.%i",
  253. address.netNum[0], address.netNum[1], address.netNum[2], address.netNum[3]);
  254. mState = PasswordTry;
  255. mDebugSocket = newConnection;
  256. Net::setBlocking(newConnection, false);
  257. }
  258. else if(newConnection != InvalidSocket)
  259. Net::closeSocket(newConnection);
  260. }
  261. // see if we have any input to process...
  262. if(mDebugSocket == InvalidSocket)
  263. return;
  264. checkDebugRecv();
  265. if(mDebugSocket == InvalidSocket)
  266. removeAllBreakpoints();
  267. }
  268. void TelnetDebugger::checkDebugRecv()
  269. {
  270. for (;;)
  271. {
  272. // Process all the complete commands in the buffer.
  273. while ( mCurPos > 0 )
  274. {
  275. // Remove leading whitespace.
  276. while ( mCurPos > 0 && ( mLineBuffer[0] == 0 || mLineBuffer[0] == '\r' || mLineBuffer[0] == '\n' ) )
  277. {
  278. mCurPos--;
  279. dMemmove(mLineBuffer, mLineBuffer + 1, mCurPos);
  280. }
  281. // Look for a complete command.
  282. bool gotCmd = false;
  283. for(S32 i = 0; i < mCurPos; i++)
  284. {
  285. if( mLineBuffer[i] == 0 )
  286. mLineBuffer[i] = '_';
  287. else if ( mLineBuffer[i] == '\r' || mLineBuffer[i] == '\n' )
  288. {
  289. // Send this command to be processed.
  290. mLineBuffer[i] = '\n';
  291. processLineBuffer(i+1);
  292. // Remove the command from the buffer.
  293. mCurPos -= i + 1;
  294. dMemmove(mLineBuffer, mLineBuffer + i + 1, mCurPos);
  295. gotCmd = true;
  296. break;
  297. }
  298. }
  299. // If we didn't find a command in this pass
  300. // then we have an incomplete buffer.
  301. if ( !gotCmd )
  302. break;
  303. }
  304. // found no <CR> or <LF>
  305. if(mCurPos == MaxCommandSize) // this shouldn't happen
  306. {
  307. disconnect();
  308. return;
  309. }
  310. S32 numBytes;
  311. Net::Error err = Net::recv(mDebugSocket, (unsigned char*)(mLineBuffer + mCurPos), MaxCommandSize - mCurPos, &numBytes);
  312. if((err != Net::NoError && err != Net::WouldBlock) || numBytes == 0)
  313. {
  314. disconnect();
  315. return;
  316. }
  317. if(err == Net::WouldBlock)
  318. return;
  319. mCurPos += numBytes;
  320. }
  321. }
  322. void TelnetDebugger::executionStopped(CodeBlock *code, U32 lineNumber)
  323. {
  324. if(mProgramPaused)
  325. return;
  326. if(mBreakOnNextStatement)
  327. {
  328. setBreakOnNextStatement( false );
  329. breakProcess();
  330. return;
  331. }
  332. Breakpoint **bp = findBreakpoint(code->name, lineNumber);
  333. if(!bp)
  334. return;
  335. Breakpoint *brk = *bp;
  336. mProgramPaused = true;
  337. Con::evaluatef("$Debug::result = %s;", brk->testExpression);
  338. if(Con::getBoolVariable("$Debug::result"))
  339. {
  340. brk->curCount++;
  341. if(brk->curCount >= brk->passCount)
  342. {
  343. brk->curCount = 0;
  344. if(brk->clearOnHit)
  345. removeBreakpoint(code->name, lineNumber);
  346. breakProcess();
  347. }
  348. }
  349. mProgramPaused = false;
  350. }
  351. void TelnetDebugger::pushStackFrame()
  352. {
  353. if(mState == NotConnected)
  354. return;
  355. if(mBreakOnNextStatement && mStackPopBreakIndex > -1 &&
  356. gEvalState.getStackDepth() > mStackPopBreakIndex)
  357. setBreakOnNextStatement( false );
  358. }
  359. void TelnetDebugger::popStackFrame()
  360. {
  361. if(mState == NotConnected)
  362. return;
  363. if(mStackPopBreakIndex > -1 && gEvalState.getStackDepth()-1 <= mStackPopBreakIndex)
  364. setBreakOnNextStatement( true );
  365. }
  366. void TelnetDebugger::breakProcess()
  367. {
  368. // Send out a break with the full stack.
  369. sendBreak();
  370. mProgramPaused = true;
  371. while(mProgramPaused)
  372. {
  373. Platform::sleep(10);
  374. checkDebugRecv();
  375. if(mDebugSocket == InvalidSocket)
  376. {
  377. mProgramPaused = false;
  378. removeAllBreakpoints();
  379. debugContinue();
  380. return;
  381. }
  382. }
  383. }
  384. void TelnetDebugger::sendBreak()
  385. {
  386. // echo out the break
  387. send("BREAK");
  388. char buffer[MaxCommandSize];
  389. char scope[MaxCommandSize];
  390. S32 last = 0;
  391. for(S32 i = (S32) gEvalState.getStackDepth() - 1; i >= last; i--)
  392. {
  393. CodeBlock *code = gEvalState.stack[i]->code;
  394. const char *file = "<none>";
  395. if (code && code->name && code->name[0])
  396. file = code->name;
  397. Namespace *ns = gEvalState.stack[i]->scopeNamespace;
  398. scope[0] = 0;
  399. if ( ns ) {
  400. if ( ns->mParent && ns->mParent->mPackage && ns->mParent->mPackage[0] ) {
  401. dStrcat( scope, ns->mParent->mPackage );
  402. dStrcat( scope, "::" );
  403. }
  404. if ( ns->mName && ns->mName[0] ) {
  405. dStrcat( scope, ns->mName );
  406. dStrcat( scope, "::" );
  407. }
  408. }
  409. const char *function = gEvalState.stack[i]->scopeName;
  410. if ((!function) || (!function[0]))
  411. function = "<none>";
  412. dStrcat( scope, function );
  413. U32 line=0, inst;
  414. U32 ip = gEvalState.stack[i]->ip;
  415. if (code)
  416. code->findBreakLine(ip, line, inst);
  417. dSprintf(buffer, MaxCommandSize, " %s %d %s", file, line, scope);
  418. send(buffer);
  419. }
  420. send("\r\n");
  421. }
  422. void TelnetDebugger::processLineBuffer(S32 cmdLen)
  423. {
  424. if (mState == PasswordTry)
  425. {
  426. if(dStrncmp(mLineBuffer, mDebuggerPassword, cmdLen-1))
  427. {
  428. // failed password:
  429. send("PASS WrongPassword.\r\n");
  430. disconnect();
  431. }
  432. else
  433. {
  434. send("PASS Connected.\r\n");
  435. mState = mWaitForClient ? Initialize : Connected;
  436. }
  437. return;
  438. }
  439. else
  440. {
  441. char evalBuffer[MaxCommandSize];
  442. char varBuffer[MaxCommandSize];
  443. char fileBuffer[MaxCommandSize];
  444. char clear[MaxCommandSize];
  445. S32 passCount, line, frame;
  446. if(dSscanf(mLineBuffer, "CEVAL %[^\n]", evalBuffer) == 1)
  447. {
  448. RawData rd;
  449. rd.size = dStrlen(evalBuffer) + 1;
  450. rd.data = ( S8* ) evalBuffer;
  451. Con::smConsoleInput.trigger(rd);
  452. }
  453. else if(dSscanf(mLineBuffer, "BRKVARSET %s %d %[^\n]", varBuffer, &passCount, evalBuffer) == 3)
  454. addVariableBreakpoint(varBuffer, passCount, evalBuffer);
  455. else if(dSscanf(mLineBuffer, "BRKVARCLR %s", varBuffer) == 1)
  456. removeVariableBreakpoint(varBuffer);
  457. else if(dSscanf(mLineBuffer, "BRKSET %s %d %s %d %[^\n]", fileBuffer,&line,&clear,&passCount,evalBuffer) == 5)
  458. addBreakpoint(fileBuffer, line, dAtob(clear), passCount, evalBuffer);
  459. else if(dSscanf(mLineBuffer, "BRKCLR %s %d", fileBuffer, &line) == 2)
  460. removeBreakpoint(fileBuffer, line);
  461. else if(!dStrncmp(mLineBuffer, "BRKCLRALL\n", cmdLen))
  462. removeAllBreakpoints();
  463. else if(!dStrncmp(mLineBuffer, "BRKNEXT\n", cmdLen))
  464. debugBreakNext();
  465. else if(!dStrncmp(mLineBuffer, "CONTINUE\n", cmdLen))
  466. debugContinue();
  467. else if(!dStrncmp(mLineBuffer, "STEPIN\n", cmdLen))
  468. debugStepIn();
  469. else if(!dStrncmp(mLineBuffer, "STEPOVER\n", cmdLen))
  470. debugStepOver();
  471. else if(!dStrncmp(mLineBuffer, "STEPOUT\n", cmdLen))
  472. debugStepOut();
  473. else if(dSscanf(mLineBuffer, "EVAL %s %d %[^\n]", varBuffer, &frame, evalBuffer) == 3)
  474. evaluateExpression(varBuffer, frame, evalBuffer);
  475. else if(!dStrncmp(mLineBuffer, "FILELIST\n", cmdLen))
  476. dumpFileList();
  477. else if(dSscanf(mLineBuffer, "BREAKLIST %s", fileBuffer) == 1)
  478. dumpBreakableList(fileBuffer);
  479. else
  480. {
  481. S32 errorLen = dStrlen(mLineBuffer) + 32; // ~25 in error message, plus buffer
  482. FrameTemp<char> errorBuffer(errorLen);
  483. dSprintf( errorBuffer, errorLen, "DBGERR Invalid command(%s)!\r\n", mLineBuffer );
  484. // invalid stuff.
  485. send( errorBuffer );
  486. }
  487. }
  488. }
  489. void TelnetDebugger::addVariableBreakpoint(const char*, S32, const char*)
  490. {
  491. send("addVariableBreakpoint\r\n");
  492. }
  493. void TelnetDebugger::removeVariableBreakpoint(const char*)
  494. {
  495. send("removeVariableBreakpoint\r\n");
  496. }
  497. void TelnetDebugger::addAllBreakpoints(CodeBlock *code)
  498. {
  499. if(mState == NotConnected)
  500. return;
  501. // Find the breakpoints for this code block and attach them.
  502. Breakpoint *cur = mBreakpoints;
  503. while( cur != NULL )
  504. {
  505. // TODO: This assumes that the OS file names are case
  506. // insensitive... Torque needs a dFilenameCmp() function.
  507. if( dStricmp( cur->fileName, code->name ) == 0 )
  508. {
  509. cur->code = code;
  510. // Find the fist breakline starting from and
  511. // including the requested breakline.
  512. S32 newLine = code->findFirstBreakLine(cur->lineNumber);
  513. if (newLine <= 0)
  514. {
  515. char buffer[MaxCommandSize];
  516. dSprintf(buffer, MaxCommandSize, "BRKCLR %s %d\r\n", cur->fileName, cur->lineNumber);
  517. send(buffer);
  518. Breakpoint *next = cur->next;
  519. removeBreakpoint(cur->fileName, cur->lineNumber);
  520. cur = next;
  521. continue;
  522. }
  523. // If the requested breakline does not match
  524. // the actual break line we need to inform
  525. // the client.
  526. if (newLine != cur->lineNumber)
  527. {
  528. char buffer[MaxCommandSize];
  529. // If we already have a line at this breapoint then
  530. // tell the client to clear the breakpoint.
  531. if ( findBreakpoint(cur->fileName, newLine) ) {
  532. dSprintf(buffer, MaxCommandSize, "BRKCLR %s %d\r\n", cur->fileName, cur->lineNumber);
  533. send(buffer);
  534. Breakpoint *next = cur->next;
  535. removeBreakpoint(cur->fileName, cur->lineNumber);
  536. cur = next;
  537. continue;
  538. }
  539. // We're moving the breakpoint to new line... inform the
  540. // client so it can update it's view.
  541. dSprintf(buffer, MaxCommandSize, "BRKMOV %s %d %d\r\n", cur->fileName, cur->lineNumber, newLine);
  542. send(buffer);
  543. cur->lineNumber = newLine;
  544. }
  545. code->setBreakpoint(cur->lineNumber);
  546. }
  547. cur = cur->next;
  548. }
  549. // Enable all breaks if a break next was set.
  550. if (mBreakOnNextStatement)
  551. code->setAllBreaks();
  552. }
  553. void TelnetDebugger::addBreakpoint(const char *fileName, S32 line, bool clear, S32 passCount, const char *evalString)
  554. {
  555. fileName = StringTable->insert(fileName);
  556. Breakpoint **bp = findBreakpoint(fileName, line);
  557. if(bp)
  558. {
  559. // trying to add the same breakpoint...
  560. Breakpoint *brk = *bp;
  561. dFree(brk->testExpression);
  562. brk->testExpression = dStrdup(evalString);
  563. brk->passCount = passCount;
  564. brk->clearOnHit = clear;
  565. brk->curCount = 0;
  566. }
  567. else
  568. {
  569. // Note that if the code block is not already
  570. // loaded it is handled by addAllBreakpoints.
  571. CodeBlock* code = CodeBlock::find(fileName);
  572. if (code)
  573. {
  574. // Find the fist breakline starting from and
  575. // including the requested breakline.
  576. S32 newLine = code->findFirstBreakLine(line);
  577. if (newLine <= 0)
  578. {
  579. char buffer[MaxCommandSize];
  580. dSprintf(buffer, MaxCommandSize, "BRKCLR %s %d\r\n", fileName, line);
  581. send(buffer);
  582. return;
  583. }
  584. // If the requested breakline does not match
  585. // the actual break line we need to inform
  586. // the client.
  587. if (newLine != line)
  588. {
  589. char buffer[MaxCommandSize];
  590. // If we already have a line at this breapoint then
  591. // tell the client to clear the breakpoint.
  592. if ( findBreakpoint(fileName, newLine) ) {
  593. dSprintf(buffer, MaxCommandSize, "BRKCLR %s %d\r\n", fileName, line);
  594. send(buffer);
  595. return;
  596. }
  597. // We're moving the breakpoint to new line... inform the client.
  598. dSprintf(buffer, MaxCommandSize, "BRKMOV %s %d %d\r\n", fileName, line, newLine);
  599. send(buffer);
  600. line = newLine;
  601. }
  602. code->setBreakpoint(line);
  603. }
  604. Breakpoint *brk = new Breakpoint;
  605. brk->code = code;
  606. brk->fileName = fileName;
  607. brk->lineNumber = line;
  608. brk->passCount = passCount;
  609. brk->clearOnHit = clear;
  610. brk->curCount = 0;
  611. brk->testExpression = dStrdup(evalString);
  612. brk->next = mBreakpoints;
  613. mBreakpoints = brk;
  614. }
  615. }
  616. void TelnetDebugger::removeBreakpointsFromCode(CodeBlock *code)
  617. {
  618. Breakpoint **walk = &mBreakpoints;
  619. Breakpoint *cur;
  620. while((cur = *walk) != NULL)
  621. {
  622. if(cur->code == code)
  623. {
  624. dFree(cur->testExpression);
  625. *walk = cur->next;
  626. delete walk;
  627. }
  628. else
  629. walk = &cur->next;
  630. }
  631. }
  632. void TelnetDebugger::removeBreakpoint(const char *fileName, S32 line)
  633. {
  634. fileName = StringTable->insert(fileName);
  635. Breakpoint **bp = findBreakpoint(fileName, line);
  636. if(bp)
  637. {
  638. Breakpoint *brk = *bp;
  639. *bp = brk->next;
  640. if ( brk->code )
  641. brk->code->clearBreakpoint(brk->lineNumber);
  642. dFree(brk->testExpression);
  643. delete brk;
  644. }
  645. }
  646. void TelnetDebugger::removeAllBreakpoints()
  647. {
  648. Breakpoint *walk = mBreakpoints;
  649. while(walk)
  650. {
  651. Breakpoint *temp = walk->next;
  652. if ( walk->code )
  653. walk->code->clearBreakpoint(walk->lineNumber);
  654. dFree(walk->testExpression);
  655. delete walk;
  656. walk = temp;
  657. }
  658. mBreakpoints = NULL;
  659. }
  660. void TelnetDebugger::debugContinue()
  661. {
  662. if (mState == Initialize) {
  663. mState = Connected;
  664. return;
  665. }
  666. setBreakOnNextStatement( false );
  667. mStackPopBreakIndex = -1;
  668. mProgramPaused = false;
  669. send("RUNNING\r\n");
  670. }
  671. void TelnetDebugger::setBreakOnNextStatement( bool enabled )
  672. {
  673. if ( enabled )
  674. {
  675. // Apply breaks on all the code blocks.
  676. for(CodeBlock *walk = CodeBlock::getCodeBlockList(); walk; walk = walk->nextFile)
  677. walk->setAllBreaks();
  678. mBreakOnNextStatement = true;
  679. }
  680. else if ( !enabled )
  681. {
  682. // Clear all the breaks on the codeblocks
  683. // then go reapply the breakpoints.
  684. for(CodeBlock *walk = CodeBlock::getCodeBlockList(); walk; walk = walk->nextFile)
  685. walk->clearAllBreaks();
  686. for(Breakpoint *w = mBreakpoints; w; w = w->next)
  687. {
  688. if ( w->code )
  689. w->code->setBreakpoint(w->lineNumber);
  690. }
  691. mBreakOnNextStatement = false;
  692. }
  693. }
  694. void TelnetDebugger::debugBreakNext()
  695. {
  696. if (mState != Connected)
  697. return;
  698. if ( !mProgramPaused )
  699. setBreakOnNextStatement( true );
  700. }
  701. void TelnetDebugger::debugStepIn()
  702. {
  703. // Note that step in is allowed during
  704. // the initialize state, so that we can
  705. // break on the first script line executed.
  706. setBreakOnNextStatement( true );
  707. mStackPopBreakIndex = -1;
  708. mProgramPaused = false;
  709. // Don't bother sending this to the client
  710. // if it's in the initialize state. It will
  711. // just be ignored as the client knows it
  712. // is in a running state when it connects.
  713. if (mState != Initialize)
  714. send("RUNNING\r\n");
  715. else
  716. mState = Connected;
  717. }
  718. void TelnetDebugger::debugStepOver()
  719. {
  720. if (mState != Connected)
  721. return;
  722. setBreakOnNextStatement( true );
  723. mStackPopBreakIndex = gEvalState.getStackDepth();
  724. mProgramPaused = false;
  725. send("RUNNING\r\n");
  726. }
  727. void TelnetDebugger::debugStepOut()
  728. {
  729. if (mState != Connected)
  730. return;
  731. setBreakOnNextStatement( false );
  732. mStackPopBreakIndex = gEvalState.getStackDepth() - 1;
  733. if ( mStackPopBreakIndex == 0 )
  734. mStackPopBreakIndex = -1;
  735. mProgramPaused = false;
  736. send("RUNNING\r\n");
  737. }
  738. void TelnetDebugger::evaluateExpression(const char *tag, S32 frame, const char *evalBuffer)
  739. {
  740. // Make sure we're passing a valid frame to the eval.
  741. if ( frame > gEvalState.getStackDepth() )
  742. frame = gEvalState.getStackDepth() - 1;
  743. if ( frame < 0 )
  744. frame = 0;
  745. // Build a buffer just big enough for this eval.
  746. const char* format = "return %s;";
  747. dsize_t len = dStrlen( format ) + dStrlen( evalBuffer );
  748. char* buffer = new char[ len ];
  749. dSprintf( buffer, len, format, evalBuffer );
  750. // Execute the eval.
  751. CodeBlock *newCodeBlock = new CodeBlock();
  752. const char* result = newCodeBlock->compileExec( NULL, buffer, false, frame );
  753. delete [] buffer;
  754. // Create a new buffer that fits the result.
  755. format = "EVALOUT %s %s\r\n";
  756. len = dStrlen( format ) + dStrlen( tag ) + dStrlen( result );
  757. buffer = new char[ len ];
  758. dSprintf( buffer, len, format, tag, result[0] ? result : "\"\"" );
  759. send( buffer );
  760. delete [] buffer;
  761. }
  762. void TelnetDebugger::dumpFileList()
  763. {
  764. send("FILELISTOUT ");
  765. for(CodeBlock *walk = CodeBlock::getCodeBlockList(); walk; walk = walk->nextFile)
  766. {
  767. send(walk->name);
  768. if(walk->nextFile)
  769. send(" ");
  770. }
  771. send("\r\n");
  772. }
  773. void TelnetDebugger::dumpBreakableList(const char *fileName)
  774. {
  775. fileName = StringTable->insert(fileName);
  776. CodeBlock *file = CodeBlock::find(fileName);
  777. char buffer[MaxCommandSize];
  778. if(file)
  779. {
  780. dSprintf(buffer, MaxCommandSize, "BREAKLISTOUT %s %d", fileName, file->breakListSize >> 1);
  781. send(buffer);
  782. for(U32 i = 0; i < file->breakListSize; i += 2)
  783. {
  784. dSprintf(buffer, MaxCommandSize, " %d %d", file->breakList[i], file->breakList[i+1]);
  785. send(buffer);
  786. }
  787. send("\r\n");
  788. }
  789. else
  790. send("DBGERR No such file!\r\n");
  791. }
  792. void TelnetDebugger::clearCodeBlockPointers(CodeBlock *code)
  793. {
  794. Breakpoint **walk = &mBreakpoints;
  795. Breakpoint *cur;
  796. while((cur = *walk) != NULL)
  797. {
  798. if(cur->code == code)
  799. cur->code = NULL;
  800. walk = &cur->next;
  801. }
  802. }