ScriptDebugServer.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /*
  2. * Copyright (C) 2008, 2009 Apple Inc. All rights reserved.
  3. * Copyright (C) 2010-2011 Google Inc. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  15. * its contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include "config.h"
  30. #if ENABLE(JAVASCRIPT_DEBUGGER)
  31. #include "ScriptDebugServer.h"
  32. #include "ContentSearchUtils.h"
  33. #include "Frame.h"
  34. #include "JSJavaScriptCallFrame.h"
  35. #include "JavaScriptCallFrame.h"
  36. #include "ScriptBreakpoint.h"
  37. #include "ScriptDebugListener.h"
  38. #include "ScriptValue.h"
  39. #include <debugger/DebuggerCallFrame.h>
  40. #include <parser/SourceProvider.h>
  41. #include <runtime/JSLock.h>
  42. #include <wtf/MainThread.h>
  43. #include <wtf/text/WTFString.h>
  44. using namespace JSC;
  45. namespace WebCore {
  46. ScriptDebugServer::ScriptDebugServer()
  47. : m_callingListeners(false)
  48. , m_pauseOnExceptionsState(DontPauseOnExceptions)
  49. , m_pauseOnNextStatement(false)
  50. , m_paused(false)
  51. , m_runningNestedMessageLoop(false)
  52. , m_doneProcessingDebuggerEvents(true)
  53. , m_breakpointsActivated(true)
  54. , m_pauseOnCallFrame(0)
  55. , m_recompileTimer(this, &ScriptDebugServer::recompileAllJSFunctions)
  56. , m_lastExecutedLine(-1)
  57. , m_lastExecutedSourceId(-1)
  58. {
  59. }
  60. ScriptDebugServer::~ScriptDebugServer()
  61. {
  62. }
  63. String ScriptDebugServer::setBreakpoint(const String& sourceID, const ScriptBreakpoint& scriptBreakpoint, int* actualLineNumber, int* actualColumnNumber)
  64. {
  65. intptr_t sourceIDValue = sourceID.toIntPtr();
  66. if (!sourceIDValue)
  67. return "";
  68. SourceIdToBreakpointsMap::iterator it = m_sourceIdToBreakpoints.find(sourceIDValue);
  69. if (it == m_sourceIdToBreakpoints.end())
  70. it = m_sourceIdToBreakpoints.set(sourceIDValue, LineToBreakpointMap()).iterator;
  71. LineToBreakpointMap::iterator breaksIt = it->value.find(scriptBreakpoint.lineNumber + 1);
  72. if (breaksIt == it->value.end())
  73. breaksIt = it->value.set(scriptBreakpoint.lineNumber + 1, BreakpointsInLine()).iterator;
  74. BreakpointsInLine& breaksVector = breaksIt->value;
  75. unsigned breaksCount = breaksVector.size();
  76. for (unsigned i = 0; i < breaksCount; i++) {
  77. if (breaksVector.at(i).columnNumber == scriptBreakpoint.columnNumber)
  78. return "";
  79. }
  80. breaksVector.append(scriptBreakpoint);
  81. *actualLineNumber = scriptBreakpoint.lineNumber;
  82. *actualColumnNumber = scriptBreakpoint.columnNumber;
  83. return sourceID + ":" + String::number(scriptBreakpoint.lineNumber) + ":" + String::number(scriptBreakpoint.columnNumber);
  84. }
  85. void ScriptDebugServer::removeBreakpoint(const String& breakpointId)
  86. {
  87. Vector<String> tokens;
  88. breakpointId.split(":", tokens);
  89. if (tokens.size() != 3)
  90. return;
  91. bool success;
  92. intptr_t sourceIDValue = tokens[0].toIntPtr(&success);
  93. if (!success)
  94. return;
  95. unsigned lineNumber = tokens[1].toUInt(&success);
  96. if (!success)
  97. return;
  98. unsigned columnNumber = tokens[2].toUInt(&success);
  99. if (!success)
  100. return;
  101. SourceIdToBreakpointsMap::iterator it = m_sourceIdToBreakpoints.find(sourceIDValue);
  102. if (it == m_sourceIdToBreakpoints.end())
  103. return;
  104. LineToBreakpointMap::iterator breaksIt = it->value.find(lineNumber + 1);
  105. if (breaksIt == it->value.end())
  106. return;
  107. BreakpointsInLine& breaksVector = breaksIt->value;
  108. unsigned breaksCount = breaksVector.size();
  109. for (unsigned i = 0; i < breaksCount; i++) {
  110. if (breaksVector.at(i).columnNumber == static_cast<int>(columnNumber)) {
  111. breaksVector.remove(i);
  112. break;
  113. }
  114. }
  115. }
  116. bool ScriptDebugServer::hasBreakpoint(intptr_t sourceID, const TextPosition& position) const
  117. {
  118. if (!m_breakpointsActivated)
  119. return false;
  120. SourceIdToBreakpointsMap::const_iterator it = m_sourceIdToBreakpoints.find(sourceID);
  121. if (it == m_sourceIdToBreakpoints.end())
  122. return false;
  123. int lineNumber = position.m_line.zeroBasedInt();
  124. int columnNumber = position.m_column.zeroBasedInt();
  125. if (lineNumber < 0 || columnNumber < 0)
  126. return false;
  127. LineToBreakpointMap::const_iterator breaksIt = it->value.find(lineNumber + 1);
  128. if (breaksIt == it->value.end())
  129. return false;
  130. bool hit = false;
  131. const BreakpointsInLine& breaksVector = breaksIt->value;
  132. unsigned breaksCount = breaksVector.size();
  133. unsigned i;
  134. for (i = 0; i < breaksCount; i++) {
  135. int breakLine = breaksVector.at(i).lineNumber;
  136. int breakColumn = breaksVector.at(i).columnNumber;
  137. // Since frontend truncates the indent, the first statement in a line must match the breakpoint (line,0).
  138. if ((lineNumber != m_lastExecutedLine && lineNumber == breakLine && !breakColumn)
  139. || (lineNumber == breakLine && columnNumber == breakColumn)) {
  140. hit = true;
  141. break;
  142. }
  143. }
  144. if (!hit)
  145. return false;
  146. // An empty condition counts as no condition which is equivalent to "true".
  147. if (breaksVector.at(i).condition.isEmpty())
  148. return true;
  149. JSValue exception;
  150. JSValue result = m_currentCallFrame->evaluate(breaksVector.at(i).condition, exception);
  151. if (exception) {
  152. // An erroneous condition counts as "false".
  153. return false;
  154. }
  155. return result.toBoolean(m_currentCallFrame->exec());
  156. }
  157. void ScriptDebugServer::clearBreakpoints()
  158. {
  159. m_sourceIdToBreakpoints.clear();
  160. }
  161. void ScriptDebugServer::setBreakpointsActivated(bool activated)
  162. {
  163. m_breakpointsActivated = activated;
  164. }
  165. void ScriptDebugServer::setPauseOnExceptionsState(PauseOnExceptionsState pause)
  166. {
  167. m_pauseOnExceptionsState = pause;
  168. }
  169. void ScriptDebugServer::setPauseOnNextStatement(bool pause)
  170. {
  171. m_pauseOnNextStatement = pause;
  172. }
  173. void ScriptDebugServer::breakProgram()
  174. {
  175. if (m_paused || !m_currentCallFrame)
  176. return;
  177. m_pauseOnNextStatement = true;
  178. pauseIfNeeded(m_currentCallFrame->dynamicGlobalObject());
  179. }
  180. void ScriptDebugServer::continueProgram()
  181. {
  182. if (!m_paused)
  183. return;
  184. m_pauseOnNextStatement = false;
  185. m_doneProcessingDebuggerEvents = true;
  186. }
  187. void ScriptDebugServer::stepIntoStatement()
  188. {
  189. if (!m_paused)
  190. return;
  191. m_pauseOnNextStatement = true;
  192. m_doneProcessingDebuggerEvents = true;
  193. }
  194. void ScriptDebugServer::stepOverStatement()
  195. {
  196. if (!m_paused)
  197. return;
  198. m_pauseOnCallFrame = m_currentCallFrame.get();
  199. m_doneProcessingDebuggerEvents = true;
  200. }
  201. void ScriptDebugServer::stepOutOfFunction()
  202. {
  203. if (!m_paused)
  204. return;
  205. m_pauseOnCallFrame = m_currentCallFrame ? m_currentCallFrame->caller() : 0;
  206. m_doneProcessingDebuggerEvents = true;
  207. }
  208. bool ScriptDebugServer::canSetScriptSource()
  209. {
  210. return false;
  211. }
  212. bool ScriptDebugServer::setScriptSource(const String&, const String&, bool, String*, ScriptValue*, ScriptObject*)
  213. {
  214. // FIXME(40300): implement this.
  215. return false;
  216. }
  217. void ScriptDebugServer::updateCallStack(ScriptValue*)
  218. {
  219. // This method is used for restart frame feature that is not implemented yet.
  220. // FIXME(40300): implement this.
  221. }
  222. void ScriptDebugServer::dispatchDidPause(ScriptDebugListener* listener)
  223. {
  224. ASSERT(m_paused);
  225. JSGlobalObject* globalObject = m_currentCallFrame->scopeChain()->globalObject();
  226. ScriptState* state = globalObject->globalExec();
  227. JSValue jsCallFrame;
  228. {
  229. if (m_currentCallFrame->isValid() && globalObject->inherits(&JSDOMGlobalObject::s_info)) {
  230. JSDOMGlobalObject* domGlobalObject = jsCast<JSDOMGlobalObject*>(globalObject);
  231. JSLockHolder lock(state);
  232. jsCallFrame = toJS(state, domGlobalObject, m_currentCallFrame.get());
  233. } else
  234. jsCallFrame = jsUndefined();
  235. }
  236. listener->didPause(state, ScriptValue(state->vm(), jsCallFrame), ScriptValue());
  237. }
  238. void ScriptDebugServer::dispatchDidContinue(ScriptDebugListener* listener)
  239. {
  240. listener->didContinue();
  241. }
  242. void ScriptDebugServer::dispatchDidParseSource(const ListenerSet& listeners, SourceProvider* sourceProvider, bool isContentScript)
  243. {
  244. String sourceID = String::number(sourceProvider->asID());
  245. ScriptDebugListener::Script script;
  246. script.url = sourceProvider->url();
  247. script.source = sourceProvider->source();
  248. script.startLine = sourceProvider->startPosition().m_line.zeroBasedInt();
  249. script.startColumn = sourceProvider->startPosition().m_column.zeroBasedInt();
  250. script.isContentScript = isContentScript;
  251. int sourceLength = script.source.length();
  252. int lineCount = 1;
  253. int lastLineStart = 0;
  254. for (int i = 0; i < sourceLength; ++i) {
  255. if (script.source[i] == '\n') {
  256. lineCount += 1;
  257. lastLineStart = i + 1;
  258. }
  259. }
  260. script.endLine = script.startLine + lineCount - 1;
  261. if (lineCount == 1)
  262. script.endColumn = script.startColumn + sourceLength;
  263. else
  264. script.endColumn = sourceLength - lastLineStart;
  265. Vector<ScriptDebugListener*> copy;
  266. copyToVector(listeners, copy);
  267. for (size_t i = 0; i < copy.size(); ++i)
  268. copy[i]->didParseSource(sourceID, script);
  269. }
  270. void ScriptDebugServer::dispatchFailedToParseSource(const ListenerSet& listeners, SourceProvider* sourceProvider, int errorLine, const String& errorMessage)
  271. {
  272. String url = sourceProvider->url();
  273. const String& data = sourceProvider->source();
  274. int firstLine = sourceProvider->startPosition().m_line.oneBasedInt();
  275. Vector<ScriptDebugListener*> copy;
  276. copyToVector(listeners, copy);
  277. for (size_t i = 0; i < copy.size(); ++i)
  278. copy[i]->failedToParseSource(url, data, firstLine, errorLine, errorMessage);
  279. }
  280. bool ScriptDebugServer::isContentScript(ExecState* exec)
  281. {
  282. return currentWorld(exec) != mainThreadNormalWorld();
  283. }
  284. void ScriptDebugServer::detach(JSGlobalObject* globalObject)
  285. {
  286. // If we're detaching from the currently executing global object, manually tear down our
  287. // stack, since we won't get further debugger callbacks to do so. Also, resume execution,
  288. // since there's no point in staying paused once a window closes.
  289. if (m_currentCallFrame && m_currentCallFrame->dynamicGlobalObject() == globalObject) {
  290. m_currentCallFrame = 0;
  291. m_pauseOnCallFrame = 0;
  292. continueProgram();
  293. }
  294. Debugger::detach(globalObject);
  295. }
  296. void ScriptDebugServer::sourceParsed(ExecState* exec, SourceProvider* sourceProvider, int errorLine, const String& errorMessage)
  297. {
  298. if (m_callingListeners)
  299. return;
  300. ListenerSet* listeners = getListenersForGlobalObject(exec->lexicalGlobalObject());
  301. if (!listeners)
  302. return;
  303. ASSERT(!listeners->isEmpty());
  304. m_callingListeners = true;
  305. bool isError = errorLine != -1;
  306. if (isError)
  307. dispatchFailedToParseSource(*listeners, sourceProvider, errorLine, errorMessage);
  308. else
  309. dispatchDidParseSource(*listeners, sourceProvider, isContentScript(exec));
  310. m_callingListeners = false;
  311. }
  312. void ScriptDebugServer::dispatchFunctionToListeners(const ListenerSet& listeners, JavaScriptExecutionCallback callback)
  313. {
  314. Vector<ScriptDebugListener*> copy;
  315. copyToVector(listeners, copy);
  316. for (size_t i = 0; i < copy.size(); ++i)
  317. (this->*callback)(copy[i]);
  318. }
  319. void ScriptDebugServer::dispatchFunctionToListeners(JavaScriptExecutionCallback callback, JSGlobalObject* globalObject)
  320. {
  321. if (m_callingListeners)
  322. return;
  323. m_callingListeners = true;
  324. if (ListenerSet* listeners = getListenersForGlobalObject(globalObject)) {
  325. ASSERT(!listeners->isEmpty());
  326. dispatchFunctionToListeners(*listeners, callback);
  327. }
  328. m_callingListeners = false;
  329. }
  330. void ScriptDebugServer::createCallFrame(const DebuggerCallFrame& debuggerCallFrame, intptr_t sourceID, int lineNumber, int columnNumber)
  331. {
  332. TextPosition textPosition(OrdinalNumber::fromOneBasedInt(lineNumber), OrdinalNumber::fromZeroBasedInt(columnNumber));
  333. m_currentCallFrame = JavaScriptCallFrame::create(debuggerCallFrame, m_currentCallFrame, sourceID, textPosition);
  334. if (m_lastExecutedSourceId != sourceID) {
  335. m_lastExecutedLine = -1;
  336. m_lastExecutedSourceId = sourceID;
  337. }
  338. }
  339. void ScriptDebugServer::updateCallFrameAndPauseIfNeeded(const DebuggerCallFrame& debuggerCallFrame, intptr_t sourceID, int lineNumber, int columnNumber)
  340. {
  341. ASSERT(m_currentCallFrame);
  342. if (!m_currentCallFrame)
  343. return;
  344. TextPosition textPosition(OrdinalNumber::fromOneBasedInt(lineNumber), OrdinalNumber::fromZeroBasedInt(columnNumber));
  345. m_currentCallFrame->update(debuggerCallFrame, sourceID, textPosition);
  346. pauseIfNeeded(debuggerCallFrame.dynamicGlobalObject());
  347. }
  348. void ScriptDebugServer::pauseIfNeeded(JSGlobalObject* dynamicGlobalObject)
  349. {
  350. if (m_paused)
  351. return;
  352. if (!getListenersForGlobalObject(dynamicGlobalObject))
  353. return;
  354. bool pauseNow = m_pauseOnNextStatement;
  355. pauseNow |= (m_pauseOnCallFrame == m_currentCallFrame);
  356. pauseNow |= hasBreakpoint(m_currentCallFrame->sourceID(), m_currentCallFrame->position());
  357. m_lastExecutedLine = m_currentCallFrame->position().m_line.zeroBasedInt();
  358. if (!pauseNow)
  359. return;
  360. m_pauseOnCallFrame = 0;
  361. m_pauseOnNextStatement = false;
  362. m_paused = true;
  363. dispatchFunctionToListeners(&ScriptDebugServer::dispatchDidPause, dynamicGlobalObject);
  364. didPause(dynamicGlobalObject);
  365. TimerBase::fireTimersInNestedEventLoop();
  366. m_runningNestedMessageLoop = true;
  367. m_doneProcessingDebuggerEvents = false;
  368. runEventLoopWhilePaused();
  369. m_runningNestedMessageLoop = false;
  370. didContinue(dynamicGlobalObject);
  371. dispatchFunctionToListeners(&ScriptDebugServer::dispatchDidContinue, dynamicGlobalObject);
  372. m_paused = false;
  373. }
  374. void ScriptDebugServer::callEvent(const DebuggerCallFrame& debuggerCallFrame, intptr_t sourceID, int lineNumber, int columnNumber)
  375. {
  376. if (!m_paused) {
  377. createCallFrame(debuggerCallFrame, sourceID, lineNumber, columnNumber);
  378. pauseIfNeeded(debuggerCallFrame.dynamicGlobalObject());
  379. }
  380. }
  381. void ScriptDebugServer::atStatement(const DebuggerCallFrame& debuggerCallFrame, intptr_t sourceID, int lineNumber, int columnNumber)
  382. {
  383. if (!m_paused)
  384. updateCallFrameAndPauseIfNeeded(debuggerCallFrame, sourceID, lineNumber, columnNumber);
  385. }
  386. void ScriptDebugServer::returnEvent(const DebuggerCallFrame& debuggerCallFrame, intptr_t sourceID, int lineNumber, int columnNumber)
  387. {
  388. if (m_paused)
  389. return;
  390. updateCallFrameAndPauseIfNeeded(debuggerCallFrame, sourceID, lineNumber, columnNumber);
  391. // detach may have been called during pauseIfNeeded
  392. if (!m_currentCallFrame)
  393. return;
  394. // Treat stepping over a return statement like stepping out.
  395. if (m_currentCallFrame == m_pauseOnCallFrame)
  396. m_pauseOnCallFrame = m_currentCallFrame->caller();
  397. m_currentCallFrame = m_currentCallFrame->caller();
  398. }
  399. void ScriptDebugServer::exception(const DebuggerCallFrame& debuggerCallFrame, intptr_t sourceID, int lineNumber, int columnNumber, bool hasHandler)
  400. {
  401. if (m_paused)
  402. return;
  403. if (m_pauseOnExceptionsState == PauseOnAllExceptions || (m_pauseOnExceptionsState == PauseOnUncaughtExceptions && !hasHandler))
  404. m_pauseOnNextStatement = true;
  405. updateCallFrameAndPauseIfNeeded(debuggerCallFrame, sourceID, lineNumber, columnNumber);
  406. }
  407. void ScriptDebugServer::willExecuteProgram(const DebuggerCallFrame& debuggerCallFrame, intptr_t sourceID, int lineNumber, int columnNumber)
  408. {
  409. if (!m_paused) {
  410. createCallFrame(debuggerCallFrame, sourceID, lineNumber, columnNumber);
  411. pauseIfNeeded(debuggerCallFrame.dynamicGlobalObject());
  412. }
  413. }
  414. void ScriptDebugServer::didExecuteProgram(const DebuggerCallFrame& debuggerCallFrame, intptr_t sourceID, int lineNumber, int columnNumber)
  415. {
  416. if (m_paused)
  417. return;
  418. updateCallFrameAndPauseIfNeeded(debuggerCallFrame, sourceID, lineNumber, columnNumber);
  419. // Treat stepping over the end of a program like stepping out.
  420. if (!m_currentCallFrame)
  421. return;
  422. if (m_currentCallFrame == m_pauseOnCallFrame) {
  423. m_pauseOnCallFrame = m_currentCallFrame->caller();
  424. if (!m_currentCallFrame)
  425. return;
  426. }
  427. m_currentCallFrame = m_currentCallFrame->caller();
  428. }
  429. void ScriptDebugServer::didReachBreakpoint(const DebuggerCallFrame& debuggerCallFrame, intptr_t sourceID, int lineNumber, int columnNumber)
  430. {
  431. if (m_paused)
  432. return;
  433. m_pauseOnNextStatement = true;
  434. updateCallFrameAndPauseIfNeeded(debuggerCallFrame, sourceID, lineNumber, columnNumber);
  435. }
  436. void ScriptDebugServer::recompileAllJSFunctionsSoon()
  437. {
  438. m_recompileTimer.startOneShot(0);
  439. }
  440. void ScriptDebugServer::compileScript(ScriptState*, const String&, const String&, String*, String*)
  441. {
  442. // FIXME(89652): implement this.
  443. }
  444. void ScriptDebugServer::clearCompiledScripts()
  445. {
  446. // FIXME(89652): implement this.
  447. }
  448. void ScriptDebugServer::runScript(ScriptState*, const String&, ScriptValue*, bool*, String*)
  449. {
  450. // FIXME(89652): implement this.
  451. }
  452. } // namespace WebCore
  453. #endif // ENABLE(JAVASCRIPT_DEBUGGER)