telnetDebugger.cpp 25 KB

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