telnetDebugger.cc 25 KB

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