telnetDebugger.cpp 25 KB

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