telnetDebugger.cc 26 KB

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