+ m_breakpointIDToBreakpoint.remove(idIt);
+ breakpoints.remove(breakpoint);
+ delete breakpoint;
+
+ if (breakpoints.isEmpty()) {
+ it->value.remove(breaksIt);
+ if (it->value.isEmpty())
+ m_sourceIDToBreakpoints.remove(it);
+ }
+}
+
+bool Debugger::hasBreakpoint(SourceID sourceID, const TextPosition& position, Breakpoint *hitBreakpoint)
+{
+ if (!m_breakpointsActivated)
+ return false;
+
+ SourceIDToBreakpointsMap::const_iterator it = m_sourceIDToBreakpoints.find(sourceID);
+ if (it == m_sourceIDToBreakpoints.end())
+ return false;
+
+ unsigned line = position.m_line.zeroBasedInt();
+ unsigned column = position.m_column.zeroBasedInt();
+
+ LineToBreakpointsMap::const_iterator breaksIt = it->value.find(line);
+ if (breaksIt == it->value.end())
+ return false;
+
+ bool hit = false;
+ const BreakpointsList& breakpoints = *breaksIt->value;
+ Breakpoint* breakpoint;
+ for (breakpoint = breakpoints.head(); breakpoint; breakpoint = breakpoint->next()) {
+ unsigned breakLine = breakpoint->line;
+ unsigned breakColumn = breakpoint->column;
+ // Since frontend truncates the indent, the first statement in a line must match the breakpoint (line,0).
+ ASSERT(this == m_currentCallFrame->codeBlock()->globalObject()->debugger());
+ if ((line != m_lastExecutedLine && line == breakLine && !breakColumn)
+ || (line == breakLine && column == breakColumn)) {
+ hit = true;
+ break;
+ }
+ }
+ if (!hit)
+ return false;
+
+ if (hitBreakpoint)
+ *hitBreakpoint = *breakpoint;
+
+ if (breakpoint->condition.isEmpty())
+ return true;
+
+ // We cannot stop in the debugger while executing condition code,
+ // so make it looks like the debugger is already paused.
+ TemporaryPausedState pausedState(*this);
+
+ NakedPtr<Exception> exception;
+ DebuggerCallFrame* debuggerCallFrame = currentDebuggerCallFrame();
+ JSValue result = debuggerCallFrame->evaluate(breakpoint->condition, exception);
+
+ // We can lose the debugger while executing JavaScript.
+ if (!m_currentCallFrame)
+ return false;
+
+ if (exception) {
+ // An erroneous condition counts as "false".
+ handleExceptionInBreakpointCondition(m_currentCallFrame, exception);
+ return false;
+ }
+
+ return result.toBoolean(m_currentCallFrame);
+}
+
+class Debugger::ClearCodeBlockDebuggerRequestsFunctor {
+public:
+ ClearCodeBlockDebuggerRequestsFunctor(Debugger* debugger)
+ : m_debugger(debugger)
+ {
+ }
+
+ bool operator()(CodeBlock* codeBlock)
+ {
+ if (codeBlock->hasDebuggerRequests() && m_debugger == codeBlock->globalObject()->debugger())
+ codeBlock->clearDebuggerRequests();
+ return false;
+ }
+
+private:
+ Debugger* m_debugger;
+};
+
+void Debugger::clearBreakpoints()
+{
+ m_topBreakpointID = noBreakpointID;
+ m_breakpointIDToBreakpoint.clear();
+ m_sourceIDToBreakpoints.clear();
+
+ if (!m_vm)
+ return;
+ ClearCodeBlockDebuggerRequestsFunctor functor(this);
+ forEachCodeBlock(functor);
+}
+
+class Debugger::ClearDebuggerRequestsFunctor {
+public:
+ ClearDebuggerRequestsFunctor(JSGlobalObject* globalObject)
+ : m_globalObject(globalObject)
+ {
+ }
+
+ bool operator()(CodeBlock* codeBlock)
+ {
+ if (codeBlock->hasDebuggerRequests() && m_globalObject == codeBlock->globalObject())
+ codeBlock->clearDebuggerRequests();
+ return false;
+ }
+
+private:
+ JSGlobalObject* m_globalObject;
+};
+
+void Debugger::clearDebuggerRequests(JSGlobalObject* globalObject)
+{
+ ASSERT(m_vm);
+ ClearDebuggerRequestsFunctor functor(globalObject);
+ forEachCodeBlock(functor);
+}
+
+void Debugger::setBreakpointsActivated(bool activated)
+{
+ m_breakpointsActivated = activated;
+}
+
+void Debugger::setPauseOnExceptionsState(PauseOnExceptionsState pause)
+{
+ m_pauseOnExceptionsState = pause;
+}
+
+void Debugger::setPauseOnNextStatement(bool pause)
+{
+ m_pauseOnNextStatement = pause;
+ if (pause)
+ setSteppingMode(SteppingModeEnabled);
+}
+
+void Debugger::breakProgram()
+{
+ if (m_isPaused)
+ return;
+
+ m_pauseOnNextStatement = true;
+ setSteppingMode(SteppingModeEnabled);
+ m_currentCallFrame = m_vm->topCallFrame;
+ ASSERT(m_currentCallFrame);
+ pauseIfNeeded(m_currentCallFrame);
+}
+
+void Debugger::continueProgram()
+{
+ if (!m_isPaused)
+ return;
+
+ m_pauseOnNextStatement = false;
+ notifyDoneProcessingDebuggerEvents();
+}
+
+void Debugger::stepIntoStatement()
+{
+ if (!m_isPaused)
+ return;
+
+ m_pauseOnNextStatement = true;
+ setSteppingMode(SteppingModeEnabled);
+ notifyDoneProcessingDebuggerEvents();
+}
+
+void Debugger::stepOverStatement()
+{
+ if (!m_isPaused)
+ return;
+
+ m_pauseOnCallFrame = m_currentCallFrame;
+ notifyDoneProcessingDebuggerEvents();
+}
+
+void Debugger::stepOutOfFunction()
+{
+ if (!m_isPaused)
+ return;
+
+ VMEntryFrame* topVMEntryFrame = m_vm->topVMEntryFrame;
+ m_pauseOnCallFrame = m_currentCallFrame ? m_currentCallFrame->callerFrame(topVMEntryFrame) : 0;
+ notifyDoneProcessingDebuggerEvents();
+}
+
+void Debugger::updateCallFrame(CallFrame* callFrame)
+{
+ m_currentCallFrame = callFrame;
+ SourceID sourceID = DebuggerCallFrame::sourceIDForCallFrame(callFrame);
+ if (m_lastExecutedSourceID != sourceID) {
+ m_lastExecutedLine = UINT_MAX;
+ m_lastExecutedSourceID = sourceID;
+ }
+}
+
+void Debugger::updateCallFrameAndPauseIfNeeded(CallFrame* callFrame)
+{
+ updateCallFrame(callFrame);
+ pauseIfNeeded(callFrame);
+ if (!isStepping())
+ m_currentCallFrame = 0;
+}
+
+void Debugger::pauseIfNeeded(CallFrame* callFrame)
+{
+ if (m_isPaused)
+ return;
+
+ JSGlobalObject* vmEntryGlobalObject = callFrame->vmEntryGlobalObject();
+ if (!needPauseHandling(vmEntryGlobalObject))
+ return;
+
+ Breakpoint breakpoint;
+ bool didHitBreakpoint = false;
+ bool pauseNow = m_pauseOnNextStatement;
+ pauseNow |= (m_pauseOnCallFrame == m_currentCallFrame);
+
+ DebuggerPausedScope debuggerPausedScope(*this);
+
+ intptr_t sourceID = DebuggerCallFrame::sourceIDForCallFrame(m_currentCallFrame);
+ TextPosition position = DebuggerCallFrame::positionForCallFrame(m_currentCallFrame);
+ pauseNow |= didHitBreakpoint = hasBreakpoint(sourceID, position, &breakpoint);
+ m_lastExecutedLine = position.m_line.zeroBasedInt();
+ if (!pauseNow)
+ return;
+
+ // Make sure we are not going to pause again on breakpoint actions by
+ // reseting the pause state before executing any breakpoint actions.
+ TemporaryPausedState pausedState(*this);
+ m_pauseOnCallFrame = 0;
+ m_pauseOnNextStatement = false;
+
+ if (didHitBreakpoint) {
+ handleBreakpointHit(vmEntryGlobalObject, breakpoint);
+ // Note that the actions can potentially stop the debugger, so we need to check that
+ // we still have a current call frame when we get back.
+ if (breakpoint.autoContinue || !m_currentCallFrame)
+ return;
+ m_pausingBreakpointID = breakpoint.id;
+ }
+
+ {
+ PauseReasonDeclaration reason(*this, didHitBreakpoint ? PausedForBreakpoint : m_reasonForPause);
+ handlePause(vmEntryGlobalObject, m_reasonForPause);
+ }
+
+ m_pausingBreakpointID = noBreakpointID;
+
+ if (!m_pauseOnNextStatement && !m_pauseOnCallFrame) {
+ setSteppingMode(SteppingModeDisabled);
+ m_currentCallFrame = nullptr;
+ }
+}
+
+void Debugger::exception(CallFrame* callFrame, JSValue exception, bool hasCatchHandler)
+{
+ if (m_isPaused)
+ return;
+
+ PauseReasonDeclaration reason(*this, PausedForException);
+ if (m_pauseOnExceptionsState == PauseOnAllExceptions || (m_pauseOnExceptionsState == PauseOnUncaughtExceptions && !hasCatchHandler)) {
+ m_pauseOnNextStatement = true;
+ setSteppingMode(SteppingModeEnabled);
+ }
+
+ m_hasHandlerForExceptionCallback = true;
+ m_currentException = exception;
+ updateCallFrameAndPauseIfNeeded(callFrame);
+ m_currentException = JSValue();
+ m_hasHandlerForExceptionCallback = false;
+}
+
+void Debugger::atStatement(CallFrame* callFrame)
+{
+ if (m_isPaused)
+ return;
+
+ PauseReasonDeclaration reason(*this, PausedAtStatement);
+ updateCallFrameAndPauseIfNeeded(callFrame);