Ignore:
Timestamp:
Jan 22, 2014, 11:39:58 PM (11 years ago)
Author:
[email protected]
Message:

Poor man's fast breakpoints for a 2.3x debugger speedup.
<https://webkit.org/b/122836>

Reviewed by Geoffrey Garen.

Previously we gained back some performance (run at baseline JIT speeds)
when the WebInspector is opened provided no breakpoints are set. This
was achieved by simply skipping all op_debug callbacks to the debugger
if no breakpoints are set. If any breakpoints are set, the debugger will
set a m_needsOpDebugCallbacks flag which causes the callbacks to be
called, and we don't get the baseline JIT speeds anymore.

With this patch, we will now track the number of breakpoints set in the
CodeBlock that they are set in. The LLINT and baseline JIT code will
check CodeBlock::m_numBreakpoints to determine if the op_debug callbacks
need to be called. With this, we will only enable op_debug callbacks for
CodeBlocks that need it i.e. those with breakpoints set in them.

Debugger::m_needsOpDebugCallbacks is now obsoleted. The LLINT and baseline
JIT code still needs to check Debugger::m_shouldPause to determine if the
debugger is in stepping mode and hence, needs op_debug callbacks enabled
for everything until the debugger "continues" the run and exit stepping
mode.

Also in this patch, I fixed a regression in DOM breakpoints which relies
Debugger::breakProgram() to pause the debugger.

  • bytecode/CodeBlock.cpp:

(JSC::CodeBlock::dumpBytecode):

  • Missed accounting for op_debug's new hasBreakpointFlag operand here when it was added.

(JSC::CodeBlock::CodeBlock):
(JSC::CodeBlock::hasOpDebugForLineAndColumn):

  • This is needed in Debugger::toggleBreakpoint() to determine if a breakpoint falls within a CodeBlock or not. Simply checking the bounds of the CodeBlock is insufficient. For example, let's say we have the following JS code:

begin global scope
function f1() {

function f2() {

... set breakpoint here.

}

}
end global scope

Using the CodeBlock bounds alone, the breakpoint above will to appear
to be in the global program CodeBlock, and the CodeBlocks for function
f1() and f2(). With CodeBlock::hasOpDebugForLineAndColumn() we can
rule out the global program CodeBlock and f1(), and only apply the
breakpoint to f2(0 where it belongs.

CodeBlock::hasOpDebugForLineAndColumn() works by iterating over all
the opcodes in the CodeBlock to look for op_debug's. For each op_debug,
it calls CodeBlock::expressionRangeForBytecodeOffset() to do a binary
seach to get the line and column info for that op_debug. This is a
N * log(N) algorithm. However, a quick hands on test using the
WebInspector (with this patch applied) to exercise setting, breaking
on, and clearing breakpoints, as well as stepping through some code
shows no noticeable degradation of the user experience compared to the
baseline without this patch.

  • bytecode/CodeBlock.h:

(JSC::CodeBlock::numBreakpoints):
(JSC::CodeBlock::numBreakpointsOffset):
(JSC::CodeBlock::addBreakpoint):
(JSC::CodeBlock::removeBreakpoint):
(JSC::CodeBlock::clearAllBreakpoints):

  • debugger/Breakpoint.h:
  • defined Breakpoint::unspecifiedColumn so that we can explicitly indicate when the WebInspector was setting a line breakpoint and did not provide a column value. CodeBlock::hasOpDebugForLineAndColumn() needs this information in order to loosen its matching criteria for op_debug bytecodes for the specified breakpoint line and column values provided by the debugger.

Previously, we just hijack a 0 value column as an unspecified column.
However, the WebInspector operates on 0-based ints for column values.
Hence, 0 should be a valid column value and should not be hijacked to
mean an unspecified column.

  • debugger/Debugger.cpp:

(JSC::Debugger::Debugger):

  • added tracking of the VM that the debugger is used with. This is needed by Debugger::breakProgram().

The VM pointer is attained from the first JSGlobalObject that the debugger
attaches to. When the debugger detaches from the last JSGlobalObject, it
will nullify its VM pointer to allow a new one to be set on the next
attach.

We were always only using each debugger instance with one VM. This change
makes it explicit with an assert to ensure that all globalObjects that
the debugger attaches to beongs to the same VM.

(JSC::Debugger::attach):
(JSC::Debugger::detach):
(JSC::Debugger::setShouldPause):

(JSC::Debugger::registerCodeBlock):
(JSC::Debugger::unregisterCodeBlock):

  • registerCodeBlock() is responsible for applying pre-existing breakpoints to new CodeBlocks being installed. Similarly, unregisterCodeBlock() clears the breakpoints.

(JSC::Debugger::toggleBreakpoint):

  • This is the workhorse function that checks if a breakpoint falls within a CodeBlock or not. If it does, then it can either enable or disable said breakpoint in the CodeBlock. In the current implementation, enabling/disabling the breakpoint simply means incrementing/decrementing the CodeBlock's m_numBreakpoints.

(JSC::Debugger::applyBreakpoints):

(JSC::Debugger::ToggleBreakpointFunctor::ToggleBreakpointFunctor):
(JSC::Debugger::ToggleBreakpointFunctor::operator()):
(JSC::Debugger::toggleBreakpoint):

  • Iterates all relevant CodeBlocks and apply the specified breakpoint if appropriate. This is called when a new breakpoint is being defined by the WebInspector and needs to be applied to an already installed CodeBlock.

(JSC::Debugger::setBreakpoint):
(JSC::Debugger::removeBreakpoint):
(JSC::Debugger::hasBreakpoint):
(JSC::Debugger::ClearBreakpointsFunctor::ClearBreakpointsFunctor):
(JSC::Debugger::ClearBreakpointsFunctor::operator()):
(JSC::Debugger::clearBreakpoints):

(JSC::Debugger::breakProgram):

  • Fixed a regression that broke DOM breakpoints. The issue is that with the skipping of op_debug callbacks, we don't always have an updated m_currentCallFrame. Normally, m_currentCallFrame is provided as arg in the op_debug callback. In this case, we can get the CallFrame* from m_vm->topCallFrame.

(JSC::Debugger::updateCallFrameAndPauseIfNeeded):
(JSC::Debugger::pauseIfNeeded):
(JSC::Debugger::willExecuteProgram):

  • debugger/Debugger.h:

(JSC::Debugger::Debugger):
(JSC::Debugger::shouldPause):

  • heap/CodeBlockSet.h:

(JSC::CodeBlockSet::iterate):

  • heap/Heap.h:

(JSC::Heap::forEachCodeBlock):

  • Added utility to iterate all CodeBlocks in the heap / VM.
  • interpreter/Interpreter.cpp:

(JSC::Interpreter::debug):

  • jit/JITOpcodes.cpp:

(JSC::JIT::emit_op_debug):

  • jit/JITOpcodes32_64.cpp:

(JSC::JIT::emit_op_debug):

  • llint/LowLevelInterpreter.asm:
  • These now checks CodeBlock::m_numBreakpoints and Debugger::m_shouldPause instead of Debugger::m_needsOpDebugCallbacks.
  • runtime/Executable.cpp:

(JSC::ScriptExecutable::installCode):

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/debugger/Debugger.cpp

    r160082 r162598  
    11/*
    2  *  Copyright (C) 2008, 2013 Apple Inc. All rights reserved.
     2 *  Copyright (C) 2008, 2013, 2014 Apple Inc. All rights reserved.
    33 *  Copyright (C) 1999-2001 Harri Porten ([email protected])
    44 *  Copyright (C) 2001 Peter Kelly ([email protected])
     
    2525#include "Debugger.h"
    2626
     27#include "CodeBlock.h"
    2728#include "DebuggerCallFrame.h"
    2829#include "Error.h"
     30
    2931#include "HeapIterationScope.h"
    3032#include "Interpreter.h"
     
    140142
    141143Debugger::Debugger(bool isInWorkerThread)
    142     : m_pauseOnExceptionsState(DontPauseOnExceptions)
     144    : m_vm(nullptr)
     145    , m_pauseOnExceptionsState(DontPauseOnExceptions)
    143146    , m_pauseOnNextStatement(false)
    144147    , m_isPaused(false)
     
    152155    , m_lastExecutedSourceID(noSourceID)
    153156    , m_topBreakpointID(noBreakpointID)
    154     , m_needsOpDebugCallbacks(false)
    155157    , m_shouldPause(false)
    156158{
     
    167169{
    168170    ASSERT(!globalObject->debugger());
     171    if (!m_vm)
     172        m_vm = &globalObject->vm();
     173    else
     174        ASSERT(m_vm == &globalObject->vm());
    169175    globalObject->setDebugger(this);
    170176    m_globalObjects.add(globalObject);
     
    185191    m_globalObjects.remove(globalObject);
    186192    globalObject->setDebugger(0);
     193    if (!m_globalObjects.size())
     194        m_vm = nullptr;
    187195}
    188196
     
    190198{
    191199    m_shouldPause = value;
    192     updateNeedForOpDebugCallbacks();
     200}
     201
     202void Debugger::registerCodeBlock(CodeBlock* codeBlock)
     203{
     204    applyBreakpoints(codeBlock);
     205}
     206
     207void Debugger::unregisterCodeBlock(CodeBlock* codeBlock)
     208{
     209    codeBlock->clearAllBreakpoints();
     210}
     211
     212void Debugger::toggleBreakpoint(CodeBlock* codeBlock, Breakpoint& breakpoint, BreakpointState enabledOrNot)
     213{
     214    ASSERT(codeBlock->jitCode()->jitType() == JITCode::InterpreterThunk
     215        || codeBlock->jitCode()->jitType() == JITCode::BaselineJIT);
     216
     217    ScriptExecutable* executable = codeBlock->ownerExecutable();
     218
     219    SourceID sourceID = static_cast<SourceID>(executable->sourceID());
     220    if (breakpoint.sourceID != sourceID)
     221        return;
     222
     223    unsigned line = breakpoint.line;
     224    unsigned column = breakpoint.column;
     225
     226    unsigned startLine = executable->lineNo();
     227    unsigned startColumn = executable->startColumn();
     228    unsigned endLine = executable->lastLine();
     229    unsigned endColumn = executable->endColumn();
     230
     231    // Inspector breakpoint line and column values are zero-based but the executable
     232    // and CodeBlock line and column values are one-based.
     233    line += 1;
     234    column = column ? column + 1 : Breakpoint::unspecifiedColumn;
     235
     236    if (line < startLine || line > endLine)
     237        return;
     238    if (column != Breakpoint::unspecifiedColumn) {
     239        if (line == startLine && column < startColumn)
     240            return;
     241        if (line == endLine && column > endColumn)
     242            return;
     243    }
     244    if (!codeBlock->hasOpDebugForLineAndColumn(line, column))
     245        return;
     246
     247    if (enabledOrNot == BreakpointEnabled)
     248        codeBlock->addBreakpoint(1);
     249    else
     250        codeBlock->removeBreakpoint(1);
     251}
     252
     253void Debugger::applyBreakpoints(CodeBlock* codeBlock)
     254{
     255    BreakpointIDToBreakpointMap& breakpoints = m_breakpointIDToBreakpoint;
     256    for (auto it = breakpoints.begin(); it != breakpoints.end(); ++it) {
     257        Breakpoint& breakpoint = *it->value;
     258        toggleBreakpoint(codeBlock, breakpoint, BreakpointEnabled);
     259    }
     260}
     261
     262class Debugger::ToggleBreakpointFunctor {
     263public:
     264    ToggleBreakpointFunctor(Debugger* debugger, Breakpoint& breakpoint, BreakpointState enabledOrNot)
     265        : m_debugger(debugger)
     266        , m_breakpoint(breakpoint)
     267        , m_enabledOrNot(enabledOrNot)
     268    {
     269    }
     270
     271    bool operator()(CodeBlock* codeBlock)
     272    {
     273        if (m_debugger == codeBlock->globalObject()->debugger())
     274            m_debugger->toggleBreakpoint(codeBlock, m_breakpoint, m_enabledOrNot);
     275        return false;
     276    }
     277
     278private:
     279    Debugger* m_debugger;
     280    Breakpoint& m_breakpoint;
     281    BreakpointState m_enabledOrNot;
     282};
     283
     284void Debugger::toggleBreakpoint(Breakpoint& breakpoint, Debugger::BreakpointState enabledOrNot)
     285{
     286    if (!m_vm)
     287        return;
     288    HeapIterationScope iterationScope(m_vm->heap);
     289    ToggleBreakpointFunctor functor(this, breakpoint, enabledOrNot);
     290    m_vm->heap.forEachCodeBlock(functor);
    193291}
    194292
     
    206304    HeapIterationScope iterationScope(vm->heap);
    207305    vm->heap.objectSpace().forEachLiveCell(iterationScope, recompiler);
    208 }
    209 
    210 void Debugger::updateNeedForOpDebugCallbacks()
    211 {
    212     size_t numberOfBreakpoints = m_breakpointIDToBreakpoint.size();
    213     m_needsOpDebugCallbacks = m_shouldPause || numberOfBreakpoints;
    214306}
    215307
     
    248340    m_breakpointIDToBreakpoint.set(id, &breakpoints.last());
    249341
    250     updateNeedForOpDebugCallbacks();
     342    toggleBreakpoint(breakpoint, BreakpointEnabled);
    251343
    252344    return id;
     
    267359    LineToBreakpointsMap::iterator breaksIt = it->value.find(breakpoint.line);
    268360    ASSERT(breaksIt != it->value.end());
     361
     362    toggleBreakpoint(breakpoint, BreakpointDisabled);
    269363
    270364    BreakpointsInLine& breakpoints = breaksIt->value;
     
    283377        }
    284378    }
    285 
    286     updateNeedForOpDebugCallbacks();
    287379}
    288380
     
    311403        unsigned breakColumn = breakpoints[i].column;
    312404        // Since frontend truncates the indent, the first statement in a line must match the breakpoint (line,0).
     405        ASSERT(this == m_currentCallFrame->codeBlock()->globalObject()->debugger());
    313406        if ((line != m_lastExecutedLine && line == breakLine && !breakColumn)
    314407            || (line == breakLine && column == breakColumn)) {
     
    346439}
    347440
     441class Debugger::ClearBreakpointsFunctor {
     442public:
     443    ClearBreakpointsFunctor(Debugger* debugger)
     444        : m_debugger(debugger)
     445    {
     446    }
     447
     448    bool operator()(CodeBlock* codeBlock)
     449    {
     450        if (codeBlock->numBreakpoints() && m_debugger == codeBlock->globalObject()->debugger())
     451            codeBlock->clearAllBreakpoints();
     452        return false;
     453    }
     454
     455private:
     456    Debugger* m_debugger;
     457};
     458
    348459void Debugger::clearBreakpoints()
    349460{
     
    352463    m_sourceIDToBreakpoints.clear();
    353464
    354     updateNeedForOpDebugCallbacks();
     465    if (!m_vm)
     466        return;
     467    HeapIterationScope iterationScope(m_vm->heap);
     468    ClearBreakpointsFunctor functor(this);
     469    m_vm->heap.forEachCodeBlock(functor);
    355470}
    356471
     
    374489void Debugger::breakProgram()
    375490{
    376     if (m_isPaused || !m_currentCallFrame)
     491    if (m_isPaused)
    377492        return;
    378493
    379494    m_pauseOnNextStatement = true;
    380495    setShouldPause(true);
     496    m_currentCallFrame = m_vm->topCallFrame;
     497    ASSERT(m_currentCallFrame);
    381498    pauseIfNeeded(m_currentCallFrame);
    382499}
     
    433550    updateCallFrame(callFrame);
    434551    pauseIfNeeded(callFrame);
    435     if (!needsOpDebugCallbacks())
     552    if (!shouldPause())
    436553        m_currentCallFrame = 0;
    437554}
     
    478595    if (!m_pauseOnNextStatement && !m_pauseOnCallFrame) {
    479596        setShouldPause(false);
    480         if (!needsOpDebugCallbacks())
    481             m_currentCallFrame = 0;
     597        m_currentCallFrame = nullptr;
    482598    }
    483599}
     
    549665    if (!m_isInWorkerThread)
    550666        updateCallFrameAndPauseIfNeeded(callFrame);
    551     else if (needsOpDebugCallbacks())
     667    else if (shouldPause())
    552668        updateCallFrame(callFrame);
    553669}
Note: See TracChangeset for help on using the changeset viewer.