1
0
Fork 0

DBG: Partially refactor MemUpdateMap

This commit is contained in:
Nukem 2015-07-10 02:14:25 -04:00
parent 529e5f1e3a
commit b881e2d134
1 changed files with 47 additions and 30 deletions

View File

@ -22,37 +22,55 @@ bool bListAllPages = false;
void MemUpdateMap(HANDLE hProcess) void MemUpdateMap(HANDLE hProcess)
{ {
EXCLUSIVE_ACQUIRE(LockMemoryPages); EXCLUSIVE_ACQUIRE(LockMemoryPages);
MEMORY_BASIC_INFORMATION mbi;
SIZE_T numBytes;
uint MyAddress = 0, newAddress = 0;
uint curAllocationBase = 0;
// First gather all possible pages in the memory range
std::vector<MEMPAGE> pageVector; std::vector<MEMPAGE> pageVector;
do
{ {
memset(&mbi, 0, sizeof(mbi)); SIZE_T numBytes = 0;
numBytes = VirtualQueryEx(hProcess, (LPCVOID)MyAddress, &mbi, sizeof(mbi)); uint pageStart = 0;
if(mbi.State == MEM_COMMIT) uint allocationBase = 0;
do
{ {
if(bListAllPages || curAllocationBase != (uint)mbi.AllocationBase) //only list allocation bases // Query memory attributes
MEMORY_BASIC_INFORMATION mbi;
memset(&mbi, 0, sizeof(mbi));
numBytes = VirtualQueryEx(hProcess, (LPVOID)pageStart, &mbi, sizeof(mbi));
// Only allow pages that are committed to memory (exclude reserved/mapped)
if(mbi.State == MEM_COMMIT)
{ {
curAllocationBase = (uint)mbi.AllocationBase; // Only list allocation bases, unless if forced to list all
MEMPAGE curPage; if(bListAllPages || allocationBase != (uint)mbi.AllocationBase)
*curPage.info = 0; {
ModNameFromAddr(MyAddress, curPage.info, true); // Set the new allocation base page
memcpy(&curPage.mbi, &mbi, sizeof(mbi)); allocationBase = (uint)mbi.AllocationBase;
pageVector.push_back(curPage);
MEMPAGE curPage;
memset(&curPage, 0, sizeof(MEMPAGE));
memcpy(&curPage.mbi, &mbi, sizeof(mbi));
ModNameFromAddr(pageStart, curPage.info, true);
pageVector.push_back(curPage);
}
else
{
// Otherwise append the page to the last created entry
pageVector.back().mbi.RegionSize += mbi.RegionSize;
}
} }
else
pageVector.at(pageVector.size() - 1).mbi.RegionSize += mbi.RegionSize; // Calculate the next page start
uint newAddress = (uint)mbi.BaseAddress + mbi.RegionSize;
if(newAddress <= pageStart)
break;
pageStart = newAddress;
} }
newAddress = (uint)mbi.BaseAddress + mbi.RegionSize; while(numBytes);
if(newAddress <= MyAddress)
numBytes = 0;
else
MyAddress = newAddress;
} }
while(numBytes);
//process file sections //process file sections
int pagecount = (int)pageVector.size(); int pagecount = (int)pageVector.size();
@ -121,15 +139,14 @@ void MemUpdateMap(HANDLE hProcess)
} }
} }
//convert to memoryPages map // Convert the vector to a map
pagecount = (int)pageVector.size();
memoryPages.clear(); memoryPages.clear();
for(int i = 0; i < pagecount; i++)
for(auto & page : pageVector)
{ {
const MEMPAGE & curPage = pageVector.at(i); uint start = (uint)page.mbi.BaseAddress;
uint start = (uint)curPage.mbi.BaseAddress; uint size = (uint)page.mbi.RegionSize;
uint size = curPage.mbi.RegionSize; memoryPages.insert(std::make_pair(std::make_pair(start, start + size - 1), page));
memoryPages.insert(std::make_pair(std::make_pair(start, start + size - 1), curPage));
} }
} }