Skip to content

Commit

Permalink
Add plotting for pool allocators
Browse files Browse the repository at this point in the history
  • Loading branch information
dedmen committed Feb 27, 2019
1 parent 8952324 commit b17fcaf
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 3 deletions.
2 changes: 1 addition & 1 deletion intercept
4 changes: 4 additions & 0 deletions src/AdapterTracy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ void AdapterTracy::setCounter(intercept::types::r_string name, float val) {
tracy::Profiler::PlotData(name.c_str(), val);
}

void AdapterTracy::setCounter(const char* name, float val) const {
tracy::Profiler::PlotData(name, val);
}

std::shared_ptr<ScopeInfo> AdapterTracy::createScopeStatic(const char* name, const char* filename, uint32_t fileline) const {
auto info = std::make_shared<ScopeInfoTracy>();
info->info = tracy::SourceLocationData{nullptr, name, filename,fileline, 0};
Expand Down
3 changes: 2 additions & 1 deletion src/AdapterTracy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class AdapterTracy final : public ProfilerAdapter
void setName(std::shared_ptr<ScopeTempStorage> tempStorage, const intercept::types::r_string& name) override;
void setDescription(std::shared_ptr<ScopeTempStorage> tempStorage, const intercept::types::r_string& descr) override;
void addLog(intercept::types::r_string message) override;
void setCounter(intercept::types::r_string name, float val) override;
void setCounter(intercept::types::r_string name, float val) override;
void setCounter(const char* name, float val) const;

std::shared_ptr<ScopeInfo> createScopeStatic(const char* name, const char* filename, uint32_t fileline) const;
static bool isConnected();
Expand Down
3 changes: 3 additions & 0 deletions src/ArmaScriptProfiler.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
Expand All @@ -169,6 +170,7 @@
<ClInclude Include="AdapterChrome.hpp" />
<ClInclude Include="AdapterTracy.hpp" />
<ClInclude Include="EngineProfiling.h" />
<ClInclude Include="FAllocHook.h" />
<ClInclude Include="HookManager.hpp" />
<ClInclude Include="ProfilerAdapter.hpp" />
<ClInclude Include="scriptProfiler.hpp" />
Expand Down Expand Up @@ -245,6 +247,7 @@
</ClCompile>
<ClCompile Include="AdapterTracy.cpp" />
<ClCompile Include="EngineProfiling.cpp" />
<ClCompile Include="FAllocHook.cpp" />
<ClCompile Include="HookManager.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="scriptProfiler.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions src/ArmaScriptProfiler.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@
<ClInclude Include="SignalSlot.hpp">
<Filter>Quelldateien</Filter>
</ClInclude>
<ClInclude Include="FAllocHook.h">
<Filter>Headerdateien</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="scriptProfiler.cpp">
Expand Down Expand Up @@ -272,6 +275,9 @@
<ClCompile Include="HookManager.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
<ClCompile Include="FAllocHook.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<MASM Include="hooks.asm">
Expand Down
85 changes: 85 additions & 0 deletions src/FAllocHook.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include "FAllocHook.h"
#include <containers.hpp>
#include "AdapterTracy.hpp"
extern std::shared_ptr<ProfilerAdapter> GProfilerAdapter;
#include <Tracy.hpp>
using namespace std::string_view_literals;

FAllocHook::FAllocHook()
{
}


FAllocHook::~FAllocHook()
{
}


extern "C" {
uintptr_t engineAlloc;
uintptr_t engineFree;
intercept::types::rv_pool_allocator* freealloctmp;
intercept::types::rv_pool_allocator* allocalloctmp;

void afterAlloc() {
auto tracyProf = std::reinterpret_pointer_cast<AdapterTracy>(GProfilerAdapter);
tracyProf->setCounter(allocalloctmp->_allocName, allocalloctmp->allocated_count);
}

void afterFree() {
auto tracyProf = std::reinterpret_pointer_cast<AdapterTracy>(GProfilerAdapter);
tracyProf->setCounter(freealloctmp->_allocName, freealloctmp->allocated_count);
}


void engineAllocRedir();
void engineFreeRedir();
}


HookManager::Pattern pat_allocReg{ // "Out of FastCAlloc slots"
"xxxxxxxxxxxxxxx?????x????xxx????xx????xxxxx????xxxxxxxxxxx????xxxxxxxxx????x????xxxxxxxxx"sv,
"\x40\x53\x48\x83\xEC\x30\x45\x33\xC9\x48\x8B\xD9\xC7\x44\x24\x00\x00\x00\x00\x00\xE8\x00\x00\x00\x00\x48\x63\x15\x00\x00\x00\x00\x81\xFA\x00\x00\x00\x00\x7D\x1C\x48\x8D\x0D\x00\x00\x00\x00\x48\x8B\xC3\x48\x89\x1C\xD1\xFF\xC2\x89\x15\x00\x00\x00\x00\x48\x83\xC4\x30\x5B\xC3\x48\x8D\x0D\x00\x00\x00\x00\xE8\x00\x00\x00\x00\x48\x8B\xC3\x48\x83\xC4\x30\x5B\xC3"sv
};

HookManager::Pattern pat_allocC{
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx????xxxx????xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx????xxxxxxxxxxx"sv,
"\x40\x53\x48\x83\xEC\x20\xFF\x41\x60\x48\x8B\x41\x08\x48\x8B\xD9\x48\x3B\xC1\x74\x0B\x48\x85\xC0\x74\x06\x48\x83\xC0\xE0\x75\x2B\x48\x8D\x41\x18\x48\x8B\x49\x20\x48\x3B\xC8\x74\x0E\x48\x85\xC9\x74\x09\x48\x8D\x41\xE0\x48\x85\xC0\x75\x10\x48\x8B\xCB\xE8\x00\x00\x00\x00\x84\xC0\x0F\x84\x00\x00\x00\x00\x4C\x8B\x43\x08\x32\xC9\x45\x33\xD2\x4C\x3B\xC3\x74\x0B\x4D\x85\xC0\x74\x06\x49\x83\xC0\xE0\x75\x2A\x4C\x8B\x43\x20\x48\x8D\x43\x18\x4C\x3B\xC0\x0F\x84\x00\x00\x00\x00\x4D\x85\xC0\x74\x06\x49\x83\xC0\xE0\xEB\x03"sv
};

HookManager::Pattern pat_freeC{
"xxxxx????xxxxxxxxxxxxx?xxxxxxxxxxxxxxxxxxxxxxx????x????xxxxxx????xxxxxxx?xxxxxx????xxxxxxxxxx??xxxxxxxxxxx"sv,
"\x48\x85\xD2\x0F\x84\x00\x00\x00\x00\x53\x48\x83\xEC\x20\x48\x63\x41\x58\x48\x89\x7C\x24\x00\x48\x8B\xFA\x48\xFF\xC8\x48\x8B\xD9\x48\x23\xC2\x48\x2B\xF8\x83\x3F\x00\x74\x28\x48\x8D\x0D\x00\x00\x00\x00\xE8\x00\x00\x00\x00\x44\x8B\x07\x48\x8D\x0D\x00\x00\x00\x00\x48\x8B\xD7\x48\x8B\x7C\x24\x00\x48\x83\xC4\x20\x5B\xE9\x00\x00\x00\x00\x48\x8B\x47\x18\x48\x89\x02\x48\x83\x7F\x00\x00\x48\x89\x57\x18\x0F\x94\xC0\x48\x89\x7A\x08"sv
};

void FAllocHook::init() {

auto found = hooks.findPattern(pat_allocReg, 0x0);



auto aicp = *reinterpret_cast<uint32_t*>(found + 0x1C);
auto rip = found + 0x20;
auto aic = *reinterpret_cast<int32_t*>(rip + aicp);

//+1C
//70 C4 4C 01
//014cc470
//0002932C90


auto aidp = *reinterpret_cast<uint32_t*>(found + 0x2B);
rip = found + 0x2F;
auto aid = reinterpret_cast<intercept::types::rv_pool_allocator**>(rip + aidp);
//71 C4 4C 01


auto allocF = hooks.findPattern(pat_allocC);
engineAlloc = hooks.placeHookTotalOffs(allocF, reinterpret_cast<uintptr_t>(engineAllocRedir))+2;

auto FreeF = hooks.findPattern(pat_freeC);
engineFree = hooks.placeHookTotalOffs(FreeF + 0x9, reinterpret_cast<uintptr_t>(engineFreeRedir));
//
//__debugbreak();

}
12 changes: 12 additions & 0 deletions src/FAllocHook.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once
#include "HookManager.hpp"

class FAllocHook
{
public:
void init();
FAllocHook();
~FAllocHook();
HookManager hooks;
};

1 change: 1 addition & 0 deletions src/HookManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <utility>
#include <vector>
#include <functional>
#include <string_view>

//From ArmaDebugEngine

Expand Down
81 changes: 81 additions & 0 deletions src/hooks.asm
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,87 @@ _TEXT SEGMENT
compileCacheIns ENDP


;FAllocHook


EXTERN afterAlloc: PROC; ArmaProf::frameEnd
EXTERN afterFree: PROC;
;JmpBacks
EXTERN engineAlloc: qword
EXTERN engineFree: qword



EXTERN freealloctmp: qword
EXTERN allocalloctmp: qword

doEngineAlloc PROC

;fixup
push rbx
sub rsp, 20h
inc dword ptr [rcx+60h]
mov rax, [rcx+8]
mov rbx, rcx
jmp engineAlloc;

doEngineAlloc ENDP

doEngineFree PROC

;fixup
push rbx
sub rsp, 20h
movsxd rax, dword ptr [rcx+58h]
mov [rsp+30h], rdi
jmp engineFree;
doEngineFree ENDP


;##########
PUBLIC engineAllocRedir
engineAllocRedir PROC
mov allocalloctmp, rcx; get rid of this and just keep rcx on stack
call doEngineAlloc;
push rax;
push rcx;
push rdx;
push r8;
push r9;

call afterAlloc;

pop r9;
pop r8;
pop rdx;
pop rcx;
pop rax;

ret
engineAllocRedir ENDP

;##########
PUBLIC engineFreeRedir
engineFreeRedir PROC
mov freealloctmp, rcx; get rid of this, I keep track of rcx now anyway
push rax;
push r8;
push rcx;
push rdx;
push rcx;
call doEngineFree;
call afterFree;
pop rcx;
pop rdx;
pop rcx;
pop r8;
pop rax;
ret
engineFreeRedir ENDP






Expand Down
6 changes: 6 additions & 0 deletions src/scriptProfiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,12 @@ void scriptProfiler::preStart() {
}
}

if (getCommandLineParam("-profilerEnableFAlloc")) {
allocHook = std::make_shared<FAllocHook>();
diag_log("ASP: FAlloc instrumentation enabled"sv);
allocHook->init();
}

if (getCommandLineParam("-profilerNoPaths"sv)) {
diag_log("ASP: Omitting file paths"sv);
GProfilerAdapter->setOmitFilePaths();
Expand Down
2 changes: 2 additions & 0 deletions src/scriptProfiler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <types.hpp>
#include "ProfilerAdapter.hpp"
#include "EngineProfiling.h"
#include "FAllocHook.h"

class scriptProfiler {
public:
Expand All @@ -21,6 +22,7 @@ class scriptProfiler {
std::shared_ptr<ScopeInfo> preprocFileScope;
intercept::types::r_string waitForAdapter;
std::shared_ptr<EngineProfiling> engineProf;
std::shared_ptr<FAllocHook> allocHook;
bool engineFrameEnd = false;
};

Expand Down
2 changes: 1 addition & 1 deletion tracy
Submodule tracy updated from 49a1a2 to 455850

0 comments on commit b17fcaf

Please sign in to comment.