Skip to content

Commit

Permalink
Added checks for scripts stuck in dead loop.
Browse files Browse the repository at this point in the history
  • Loading branch information
MiranDMC committed Dec 2, 2024
1 parent e173c63 commit e8173b3
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
38 changes: 38 additions & 0 deletions cleo_plugins/DebugUtils/DebugUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "CLEO.h"
#include "CLEO_Utils.h"
#include "ScreenLog.h"
#include "ScriptLog.h"

using namespace CLEO;

Expand All @@ -25,6 +26,12 @@ class DebugUtils
};
static std::deque<PausedScriptInfo> pausedScripts;

static ScriptLog currScript;

// limits for processing after which script is considered hanging
static size_t configLimitCommand;
static size_t configLimitJump;

// breakpoint continue keys
static const int KeyFirst = VK_F5;
static const size_t KeyCount = 8; // F5 to F12
Expand All @@ -43,6 +50,8 @@ class DebugUtils
}

auto config = GetConfigFilename();
configLimitCommand = GetPrivateProfileInt("Limits", "Command", 100000, config.c_str());
configLimitJump = GetPrivateProfileInt("Limits", "Jump", 20000, config.c_str());

// register opcodes
CLEO_RegisterOpcode(0x00C3, Opcode_DebugOn);
Expand All @@ -64,6 +73,7 @@ class DebugUtils
CLEO_RegisterCallback(eCallbackId::Log, OnLog);
CLEO_RegisterCallback(eCallbackId::DrawingFinished, OnDrawingFinished);
CLEO_RegisterCallback(eCallbackId::ScriptProcess, OnScriptProcess);
CLEO_RegisterCallback(eCallbackId::ScriptOpcodeProcess, OnScriptOpcodeProcess);
CLEO_RegisterCallback(eCallbackId::ScriptsFinalize, OnScriptsFinalize);
}

Expand Down Expand Up @@ -172,6 +182,8 @@ class DebugUtils

static bool WINAPI OnScriptProcess(CScriptThread* thread)
{
currScript.Reset();

for (size_t i = 0; i < pausedScripts.size(); i++)
{
if (pausedScripts[i].ptr == thread)
Expand All @@ -183,6 +195,29 @@ class DebugUtils
return true;
}

static OpcodeResult WINAPI OnScriptOpcodeProcess(CRunningScript* pScript, DWORD opcode)
{
currScript.commandCounter++;
if (currScript.commandCounter > configLimitCommand)
{
SHOW_ERROR("More than %d commands executed in single processing of script %s \nScript considered stuck and was suspended.\n\nTo ignore this error, increase 'command' property in %s.ini file.", configLimitCommand, ScriptInfoStr(pScript).c_str(), TARGET_NAME);
return pScript->Suspend();
}

if (opcode == 0x0002 || opcode == 0x004D) // goto, goto_if_false
{
currScript.jumpCounter++;

if (currScript.jumpCounter > configLimitJump)
{
SHOW_ERROR("More than %d jump commands executed in single processing of script %s \nScript considered stuck and was suspended.\n\nTo ignore this error, increase 'jump' property in %s.ini file.", configLimitJump, ScriptInfoStr(pScript).c_str(), TARGET_NAME);
return pScript->Suspend();
}
}

return OR_NONE;
}

static void WINAPI OnLog(eLogLevel level, const char* msg)
{
screenLog.Add(level, msg);
Expand Down Expand Up @@ -378,6 +413,9 @@ class DebugUtils

ScreenLog DebugUtils::screenLog = {};
std::deque<DebugUtils::PausedScriptInfo> DebugUtils::pausedScripts;
ScriptLog DebugUtils::currScript = {};
size_t DebugUtils::configLimitCommand;
size_t DebugUtils::configLimitJump;
bool DebugUtils::keysReleased = true;
std::map<std::string, std::ofstream> DebugUtils::logFiles;

1 change: 1 addition & 0 deletions cleo_plugins/DebugUtils/DebugUtils.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ xcopy /Y "$(OutDir)$(TargetName).*" "$(GTA_SA_DIR)\cleo\cleo_plugins\"
<ClInclude Include="..\..\cleo_sdk\CLEO.h" />
<ClInclude Include="..\..\cleo_sdk\CLEO_Utils.h" />
<ClInclude Include="ScreenLog.h" />
<ClInclude Include="ScriptLog.h" />
</ItemGroup>
<ItemGroup>
<None Include="SA.DebugUtils.ini" />
Expand Down
1 change: 1 addition & 0 deletions cleo_plugins/DebugUtils/DebugUtils.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<ClInclude Include="..\..\cleo_sdk\CLEO_Utils.h">
<Filter>sdk</Filter>
</ClInclude>
<ClInclude Include="ScriptLog.h" />
</ItemGroup>
<ItemGroup>
<Filter Include="sdk">
Expand Down
5 changes: 5 additions & 0 deletions cleo_plugins/DebugUtils/SA.DebugUtils.ini
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@ FontStyle = 1
ColorError = "FF30EEFF"
ColorDebug = "FFEE30FF"
ColorSystem = "DDDDDDFF"

; limits between 'wait' command in script after which it is considered hanging
[Limits]
command = 100000
jump = 20000
15 changes: 15 additions & 0 deletions cleo_plugins/DebugUtils/ScriptLog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once
#include "CLEO.h"

struct ScriptLog
{
size_t commandCounter = 0;
size_t jumpCounter = 0;

void Reset()
{
commandCounter = 0;
jumpCounter = 0;
}
};

0 comments on commit e8173b3

Please sign in to comment.