openclonk/src/script/C4AulDebug.cpp

425 lines
10 KiB
C++
Raw Normal View History

#include <C4Include.h>
#include <C4Version.h>
#include <C4GameControl.h>
#include <C4Game.h>
#include <C4MessageInput.h>
#include <C4Log.h>
#include "C4AulDebug.h"
#include "C4AulExec.h"
// *** C4AulDebug
C4AulDebug::C4AulDebug()
: fInit(false), fConnected(false)
{
ZeroMem(&PeerAddr, sizeof PeerAddr);
}
C4AulDebug::~C4AulDebug()
{
for (std::list<StdStrBuf*>::iterator it = StackTrace.begin(); it != StackTrace.end(); it++)
{delete *it;}
}
void C4AulDebug::PackPacket(const C4NetIOPacket &rPacket, StdBuf &rOutBuf)
{
// Enlarge buffer
int iSize = rPacket.getSize(),
iPos = rOutBuf.getSize();
rOutBuf.Grow(iSize + 2);
// Write packet
rOutBuf.Write(rPacket, iPos);
// Terminate
uint8_t *pPos = getMBufPtr<uint8_t>(rOutBuf, iPos + iSize);
*pPos = '\r'; *(pPos + 1) = '\n';
}
size_t C4AulDebug::UnpackPacket(const StdBuf &rInBuf, const C4NetIO::addr_t &addr)
{
// Find line seperation
const char *pSep = reinterpret_cast<const char *>(memchr(rInBuf.getData(), '\n', rInBuf.getSize()));
if(!pSep)
return 0;
// Check if it's windows-style seperation
int iSize = pSep - getBufPtr<char>(rInBuf) + 1,
iLength = iSize - 1;
if(iLength && *(pSep - 1) == '\r')
iLength--;
// Copy the line
StdStrBuf Buf; Buf.Copy(getBufPtr<char>(rInBuf), iLength);
// Password line?
if(fConnected)
ProcessLine(Buf);
else if(!Password.getSize() || Password == Buf)
{
fConnected = true;
SendLine("HLO", "This is " C4ENGINEINFOLONG ", " C4VERSION);
Log("C4Aul debugger connected successfully!");
}
else
C4NetIOTCP::Close(PeerAddr);
// Consume line
return iSize;
}
bool C4AulDebug::OnConn(const C4NetIO::addr_t &AddrPeer, const C4NetIO::addr_t &AddrConnect, const addr_t *pOwnAddr, C4NetIO *pNetIO)
{
assert(pNetIO == this);
// Already have a connection?
if(fConnected) return false;
// Check address
if(AllowedAddr.sin_addr.s_addr)
if(AllowedAddr.sin_addr.s_addr != AddrPeer.sin_addr.s_addr ||
(AllowedAddr.sin_port && AllowedAddr.sin_port != AddrPeer.sin_port))
{
LogF("C4AulDebug blocked connection from %s:%d", inet_ntoa(AddrPeer.sin_addr), htons(AddrPeer.sin_port));
return false;
}
// Log
LogF("C4AulDebug got connection from %s:%d", inet_ntoa(AddrPeer.sin_addr), htons(AddrPeer.sin_port));
// Accept connection
PeerAddr = AddrPeer;
return true;
}
void C4AulDebug::OnDisconn(const C4NetIO::addr_t &AddrPeer, C4NetIO *pNetIO, const char *szReason)
{
LogF("C4AulDebug lost connection (%s)", szReason);
fConnected = false;
eState = DS_Go;
ZeroMem(&PeerAddr, sizeof PeerAddr);
}
void C4AulDebug::OnPacket(const class C4NetIOPacket &rPacket, C4NetIO *pNetIO)
{
// Won't get called
}
bool C4AulDebug::SetAllowed(const char *szHost)
{
// Clear
ZeroMem(&AllowedAddr, sizeof(AllowedAddr));
// No host?
if(!szHost || !*szHost) return true;
// Resolve the address
return ResolveAddress(szHost, &AllowedAddr, 0);
}
bool C4AulDebug::Init(uint16_t iPort)
{
if(fInit) Close();
if(iPort == P_NONE) return false;
// Register self as callback for network events
C4NetIOTCP::SetCallback(this);
// Start listening
if(!C4NetIOTCP::Init(iPort))
return false;
// Okay
fInit = true;
eState = DS_Go;
return true;
}
bool C4AulDebug::Close()
{
if(!fInit) return true;
fInit = fConnected = false;
return C4NetIOTCP::Close();
}
void C4AulDebug::OnLog(const char *szLine)
{
if(!fConnected) return;
SendLine("LOG", szLine);
}
void C4AulDebug::ProcessLine(const StdStrBuf &Line)
{
// Get command
StdStrBuf Cmd;
Cmd.CopyUntil(Line.getData(), ' ');
// Get data
const char *szData = Line.getPtr(Cmd.getLength());
if(*szData) szData++;
// Identify command
const char *szCmd = Cmd.getData();
bool fOkay = true;
const char *szAnswer = NULL;
if(SEqualNoCase(szCmd, "HELP"))
{
fOkay = false; szAnswer = "Yeah, like I'm going to explain that /here/";
}
else if(SEqualNoCase(szCmd, "BYE") || SEqualNoCase(szCmd, "QUIT"))
C4NetIOTCP::Close(PeerAddr);
else if(SEqualNoCase(szCmd, "SAY"))
::Control.DoInput(CID_Message, new C4ControlMessage(C4CMT_Normal, szData), CDT_Direct);
else if(SEqualNoCase(szCmd, "CMD"))
::MessageInput.ProcessCommand(szData);
else if(SEqualNoCase(szCmd, "STP") || SEqualNoCase(szCmd, "S"))
eState = DS_Step;
else if(SEqualNoCase(szCmd, "GO") || SEqualNoCase(szCmd, "G"))
eState = DS_Go;
else if(SEqualNoCase(szCmd, "STO") || SEqualNoCase(szCmd, "O"))
eState = DS_StepOver;
else if(SEqualNoCase(szCmd, "STR") || SEqualNoCase(szCmd, "R"))
eState = DS_StepOut;
else if(SEqualNoCase(szCmd, "EXC") || SEqualNoCase(szCmd, "E"))
::Control.DoInput(CID_Script, new C4ControlScript(szData, C4ControlScript::SCOPE_Console, false), CDT_Decide);
else if(SEqualNoCase(szCmd, "PSE"))
if(Game.IsPaused())
{
Game.Unpause();
szAnswer = "Game unpaused.";
}
else
{
Game.Pause();
szAnswer = "Game paused.";
}
else if (SEqualNoCase(szCmd, "LST"))
{
for (C4AulScript* script = ScriptEngine.Child0; script; script = script->Next) {
SendLine(Config.AtRelativePath(script->ScriptName.getData()));
}
}
// toggle breakpoint
else if (SEqualNoCase(szCmd, "TBR"))
{
StdStrBuf scriptPath;
scriptPath.CopyUntil(szData, ':');
const char* lineStart = szData+1+scriptPath.getLength();
int line = strtol(szData+1+scriptPath.getLength(), const_cast<char**>(&lineStart), 10);
C4AulScript* script;
for (script = ScriptEngine.Child0; script; script = script->Next)
{
if (SEqualNoCase(Config.AtRelativePath(script->ScriptName.getData()), scriptPath.getData()))
break;
}
if (script)
{
C4AulBCC* foundDebugChunk = NULL;
const char* scriptText = script->GetScript();
for (C4AulBCC* chunk = script->Code; chunk; chunk++)
{
switch (chunk->bccType) {
case AB_DEBUG:
int lineOfThisOne;
if ((lineOfThisOne = SGetLine(scriptText, chunk->SPos)) == line)
{
foundDebugChunk = chunk;
goto Done;
}
/*else {
DebugLogF("Debug chunk at %d", lineOfThisOne);
}*/
break;
case AB_EOF:
goto Done;
default:
break;
}
}
Done:
if (foundDebugChunk)
{
foundDebugChunk->Par.i = !foundDebugChunk->Par.i; // activate breakpoint
}
else
{
szAnswer = "Can't set breakpoint (wrong line?)";
fOkay = false;
}
}
else
{
fOkay = false;
szAnswer = "Can't find script";
}
}
else if (SEqualNoCase(szCmd, "SST"))
{
std::list<StdStrBuf*>::iterator it = StackTrace.begin();
for (it++; it != StackTrace.end(); it++)
{
SendLine("AT", (*it)->getData());
}
SendLine("EST");
}
else
{
fOkay = false;
szAnswer = "Can't do that";
}
// Send answer
SendLine(fOkay ? "OK" : "ERR", szAnswer);
}
bool C4AulDebug::SendLine(const char *szType, const char *szData)
{
StdStrBuf Line = szData ? FormatString("%s %s", szType, szData) : StdStrBuf(szType);
return Send(C4NetIOPacket(Line.getData(), Line.getSize(), false, PeerAddr));
}
void C4AulDebug::DebugStep(C4AulBCC *pCPos)
{
// Get top context
C4AulScriptContext *pCtx = pExec->GetContext(pExec->GetContextDepth() - 1);
// Already stopped? Ignore.
// This means we are doing some calculation with suspended script engine.
// We do /not/ want to have recursive suspensions...
if(eState == DS_Stop)
return;
// Have break point?
if(pCPos->Par.i)
eState = DS_Step;
StepPoint(pCPos);
}
void C4AulDebug::DebugStepIn(C4AulBCC *pCPos)
{
}
void C4AulDebug::DebugStepOut(C4AulBCC *pCPos, C4AulScriptContext *pRetCtx, C4Value *pRVal)
{
// Ignore if already suspended, see above.
if(eState == DS_Stop)
return;
// This counts as a regular step point
StepPoint(pCPos, pRetCtx, pRVal);
}
void C4AulDebug::StepPoint(C4AulBCC *pCPos, C4AulScriptContext *pRetCtx, C4Value *pRVal)
{
// Maybe got a command in the meantime?
Execute(0);
// Get current script context
int iCallDepth = pExec->GetContextDepth();
C4AulScriptContext *pCtx = pExec->GetContext(iCallDepth-1);
// When we're stepping out of a script function, the context of the returning
// function hasn't been removed yet.
if(pCtx == pRetCtx)
{
iCallDepth--;
// Check if we are returning to a script function
if(pCtx->Return)
pCtx--;
else
pCtx = NULL;
}
// Stepped out?
if(pRVal && eState != DS_Go && iCallDepth <= iStepCallDepth)
{
StdStrBuf FuncDump = pRetCtx->ReturnDump();
StdStrBuf ReturnDump = pRVal->GetDataString();
SendLine("RET", FormatString("%s = %s", FuncDump.getData(), ReturnDump.getData()).getData());
// Ignore as step point if we didn't "really" step out
if(iCallDepth >= iStepCallDepth) return;
}
// Stop?
switch(eState)
{
// Continue normally
case DS_Go: return;
// Always stop
case DS_Stop: break;
case DS_Step: break;
// Only stop for same level or above
case DS_StepOver:
if(iCallDepth > iStepCallDepth)
return;
break;
// Only stop above
case DS_StepOut:
if(iCallDepth >= iStepCallDepth)
return;
break;
}
// Let's stop here
eState = DS_Stop;
iStepCallDepth = iCallDepth;
Game.HaltCount++;
// No valid stop position? Just continue
if(!pCtx)
{
Game.HaltCount--;
eState = DS_Step;
return;
}
// Signal
if(pCPos && pCPos->bccType == AB_DEBUG && pCPos->Par.i)
SendLine("STP", FormatString("Stopped on breakpoint %d", pCPos->Par.i).getData());
else
SendLine("STP", "Stepped");
// Position
ObtainStackTrace(pCtx, pCPos);
SendLine("POS", StackTrace.front()->getData());
// Suspend until we get some command
while(eState == DS_Stop)
if(!Application.ScheduleProcs())
{
Close();
return;
}
// Do whatever we've been told.
Game.HaltCount--;
}
void C4AulDebug::ObtainStackTrace(C4AulScriptContext* pCtx, C4AulBCC* pCPos)
{
for (std::list<StdStrBuf*>::iterator it = StackTrace.begin(); it != StackTrace.end(); it++)
{delete *it;}
StackTrace.clear();
for (int ctxNum = pExec->GetContextDepth()-1; ctxNum >= 0; ctxNum--)
{
C4AulScriptContext* c = pExec->GetContext(ctxNum);
C4AulBCC* _cpos = c == pCtx ? pCPos : c->CPos;
if (_cpos)
{
StdStrBuf* format = new StdStrBuf(FormatCodePos(c, _cpos));
StackTrace.push_back(format);
}
}
}
StdStrBuf C4AulDebug::FormatCodePos(C4AulScriptContext *pCtx, C4AulBCC *pCPos)
{
// Get position in script
const char *szScript = pCtx->Func->pOrgScript->GetScript();
int iLine = SGetLine(szScript, pCPos->SPos);
// Format
return FormatString("%s:%d", Config.AtRelativePath(pCtx->Func->pOrgScript->ScriptName.getData()), iLine);
}