Arquivos
hhvm/hphp/runtime/debugger/cmd/cmd_flow_control.cpp
T
Owen Yamauchi 739450013f Move runtime/eval/debugger to runtime/debugger
runtime/eval is now gone. Woooo

This is just a git mv + `codemod runtime/eval/debugger runtime/debugger`
2013-06-03 23:54:35 -07:00

193 linhas
7.4 KiB
C++

/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-2013 Facebook, Inc. (http://www.facebook.com) |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#include "hphp/runtime/debugger/cmd/cmd_flow_control.h"
#include "hphp/runtime/vm/debugger_hook.h"
namespace HPHP { namespace Eval {
///////////////////////////////////////////////////////////////////////////////
TRACE_SET_MOD(debuggerflow);
CmdFlowControl::~CmdFlowControl() {
// Remove any location filter that may have been setup by this cmd.
removeLocationFilter();
}
void CmdFlowControl::sendImpl(DebuggerThriftBuffer &thrift) {
DebuggerCommand::sendImpl(thrift);
thrift.write(m_count);
thrift.write(m_smallStep);
}
void CmdFlowControl::recvImpl(DebuggerThriftBuffer &thrift) {
DebuggerCommand::recvImpl(thrift);
thrift.read(m_count);
thrift.read(m_smallStep);
}
void CmdFlowControl::onClientImpl(DebuggerClient &client) {
if (DebuggerCommand::displayedHelp(client)) return;
client.setFrame(0);
if (client.argCount() > 1) {
help(client);
return;
}
if (client.argCount() == 1) {
string snum = client.argValue(1);
if (!DebuggerClient::IsValidNumber(snum)) {
client.error("Count needs to be a number.");
return;
}
m_count = atoi(snum.c_str());
if (m_count < 1) {
client.error("Count needs to be a positive number.");
return;
}
}
m_smallStep = client.getDebuggerSmallStep();
client.sendToServer(this);
throw DebuggerConsoleExitException();
}
bool CmdFlowControl::onServer(DebuggerProxy &proxy) {
// Flow control cmds do their work in onSetup() and onBeginInterrupt(), so
// there is no real work to do in here.
return true;
}
void CmdFlowControl::onSetup(DebuggerProxy &proxy, CmdInterrupt &interrupt) {
// Should only do setting and nothing else
g_context->setDebuggerSmallStep(m_smallStep);
}
// Setup the last location filter on the VM context for all offsets covered by
// the current source line. This will short-circuit the work done in
// phpDebuggerOpcodeHook() and ensure we don't interrupt on this source line.
void CmdFlowControl::installLocationFilterForLine(InterruptSite *site) {
if (!site) return; // We may be stopped at a place with no source info.
if (g_vmContext->m_lastLocFilter) {
g_vmContext->m_lastLocFilter->clear();
} else {
g_vmContext->m_lastLocFilter = new PCFilter();
}
TRACE(3, "Prepare location filter for %s:%d, unit %p:\n",
site->getFile(), site->getLine0(), site->getUnit());
g_vmContext->m_lastLocFilter->addRanges(site->getUnit(),
site->getCurOffsetRange());
}
void CmdFlowControl::removeLocationFilter() {
if (g_vmContext->m_lastLocFilter) {
delete g_vmContext->m_lastLocFilter;
g_vmContext->m_lastLocFilter = nullptr;
}
}
bool CmdFlowControl::atStepOutOffset(Offset o) {
return ((o == m_stepOutOffset1) || (o == m_stepOutOffset2));
}
bool CmdFlowControl::hasStepOuts() {
return m_stepOutUnit != nullptr;
}
// Place internal breakpoints to get out of the current function. This may place
// multiple internal breakpoints, and it may place them more than one frame up.
// Some instructions can cause PHP to be invoked without an explicit call. A set
// which causes a destructor to run, a iteration init which causes an object's
// next() method to run, a RetC which causes destructors to run, etc. This
// recgonizes such cases and ensures we have internal breakpoints to cover the
// destination(s) of such instructions.
void CmdFlowControl::setupStepOuts() {
// Existing step outs should be cleaned up before making new ones.
assert(!hasStepOuts());
ActRec* fp = g_vmContext->getFP();
assert(fp);
Offset returnOffset;
bool fromVMEntry;
while (!hasStepOuts()) {
fp = g_vmContext->getPrevVMState(fp, &returnOffset, nullptr, &fromVMEntry);
// If we've run off the top of the stack, just return having setup no
// step outs. This will cause cmds like Next and Out to just let the program
// run, which is appropriate.
if (!fp) break;
Unit* returnUnit = fp->m_func->unit();
PC returnPC = returnUnit->at(returnOffset);
TRACE(2, "CmdFlowControl::setupStepOuts: at '%s' offset %d opcode %s\n",
fp->m_func->fullName()->data(), returnOffset,
opcodeToName(*returnPC));
// Don't step out to generated functions, keep looking.
if (fp->m_func->line1() == 0) continue;
if (fromVMEntry) {
TRACE(2, "CmdFlowControl::setupStepOuts: VM entry\n");
// We only execute this for opcodes which invoke more PHP, and that does
// not include switches. Thus, we'll have at most two destinations.
assert(!isSwitch(*returnPC) && (numSuccs(returnPC) <= 2));
// Set an internal breakpoint after the instruction if it can fall thru.
if (instrAllowsFallThru(*returnPC)) {
m_stepOutUnit = returnUnit;
m_stepOutOffset1 = returnOffset + instrLen(returnPC);
TRACE(2, "CmdFlowControl: step out to '%s' offset %d (fall-thru)\n",
fp->m_func->fullName()->data(), m_stepOutOffset1);
phpAddBreakPoint(m_stepOutUnit, m_stepOutOffset1);
}
// Set an internal breakpoint at the target of a control flow instruction.
// A good example of a control flow op that invokes PHP is IterNext.
if (instrIsControlFlow(*returnPC)) {
Offset target = instrJumpTarget(returnPC, 0);
if (target != InvalidAbsoluteOffset) {
m_stepOutUnit = returnUnit;
m_stepOutOffset2 = returnOffset + target;
TRACE(2, "CmdFlowControl: step out to '%s' offset %d (jump target)\n",
fp->m_func->fullName()->data(), m_stepOutOffset2);
phpAddBreakPoint(m_stepOutUnit, m_stepOutOffset2);
}
}
// If we have no place to step out to, then unwind another frame and try
// again. The most common case that leads here is Ret*, which does not
// fall-thru and has no encoded target.
} else {
m_stepOutUnit = returnUnit;
m_stepOutOffset1 = returnOffset;
TRACE(2, "CmdFlowControl: step out to '%s' offset %d\n",
fp->m_func->fullName()->data(), m_stepOutOffset1);
phpAddBreakPoint(m_stepOutUnit, m_stepOutOffset1);
}
}
}
void CmdFlowControl::cleanupStepOuts() {
if (m_stepOutUnit) {
if (m_stepOutOffset1 != InvalidAbsoluteOffset) {
phpRemoveBreakPoint(m_stepOutUnit, m_stepOutOffset1);
m_stepOutOffset1 = InvalidAbsoluteOffset;
}
if (m_stepOutOffset2 != InvalidAbsoluteOffset) {
phpRemoveBreakPoint(m_stepOutUnit, m_stepOutOffset2);
m_stepOutOffset2 = InvalidAbsoluteOffset;
}
m_stepOutUnit = nullptr;
}
}
///////////////////////////////////////////////////////////////////////////////
}}