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;
{
SIZE_T numBytes = 0;
uint pageStart = 0;
uint allocationBase = 0;
do do
{ {
// Query memory attributes
MEMORY_BASIC_INFORMATION mbi;
memset(&mbi, 0, sizeof(mbi)); memset(&mbi, 0, sizeof(mbi));
numBytes = VirtualQueryEx(hProcess, (LPCVOID)MyAddress, &mbi, 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) if(mbi.State == MEM_COMMIT)
{ {
if(bListAllPages || curAllocationBase != (uint)mbi.AllocationBase) //only list allocation bases // Only list allocation bases, unless if forced to list all
if(bListAllPages || allocationBase != (uint)mbi.AllocationBase)
{ {
curAllocationBase = (uint)mbi.AllocationBase; // Set the new allocation base page
allocationBase = (uint)mbi.AllocationBase;
MEMPAGE curPage; MEMPAGE curPage;
*curPage.info = 0; memset(&curPage, 0, sizeof(MEMPAGE));
ModNameFromAddr(MyAddress, curPage.info, true);
memcpy(&curPage.mbi, &mbi, sizeof(mbi)); memcpy(&curPage.mbi, &mbi, sizeof(mbi));
ModNameFromAddr(pageStart, curPage.info, true);
pageVector.push_back(curPage); pageVector.push_back(curPage);
} }
else else
pageVector.at(pageVector.size() - 1).mbi.RegionSize += mbi.RegionSize; {
// Otherwise append the page to the last created entry
pageVector.back().mbi.RegionSize += mbi.RegionSize;
} }
newAddress = (uint)mbi.BaseAddress + mbi.RegionSize; }
if(newAddress <= MyAddress)
numBytes = 0; // Calculate the next page start
else uint newAddress = (uint)mbi.BaseAddress + mbi.RegionSize;
MyAddress = newAddress;
if(newAddress <= pageStart)
break;
pageStart = newAddress;
} }
while(numBytes); 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));
} }
} }