DBG: Further refactoring in memory.cpp functions
This commit is contained in:
parent
5c4bda6c93
commit
bcc488f93e
|
@ -166,7 +166,7 @@ bool _getprocesslist(DBGPROCESSINFO** entries, int* count)
|
|||
static void _memupdatemap()
|
||||
{
|
||||
dbggetprivateusage(fdProcessInfo->hProcess, true);
|
||||
MemUpdateMap(fdProcessInfo->hProcess);
|
||||
MemUpdateMap();
|
||||
GuiUpdateMemoryView();
|
||||
}
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@ SCRIPT_EXPORT bool Script::Memory::IsValidPtr(duint addr)
|
|||
|
||||
SCRIPT_EXPORT duint Script::Memory::RemoteAlloc(duint addr, duint size)
|
||||
{
|
||||
return (duint)MemAllocRemote(addr, size, PAGE_EXECUTE_READWRITE);
|
||||
return (duint)MemAllocRemote(addr, size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
|
||||
}
|
||||
|
||||
SCRIPT_EXPORT bool Script::Memory::RemoteFree(duint addr)
|
||||
|
|
|
@ -80,6 +80,7 @@ void BookmarkDelRange(uint Start, uint End)
|
|||
for(auto itr = bookmarks.begin(); itr != bookmarks.end();)
|
||||
{
|
||||
const auto & currentBookmark = itr->second;
|
||||
|
||||
// Ignore manually set entries
|
||||
if(currentBookmark.manual)
|
||||
{
|
||||
|
|
|
@ -55,7 +55,7 @@ static DWORD WINAPI memMapThread(void* ptr)
|
|||
if(cachePrivateUsage != PrivateUsage && !dbgisrunning()) //update the memory map when the memory usage changed
|
||||
{
|
||||
cachePrivateUsage = PrivateUsage;
|
||||
MemUpdateMap(fdProcessInfo->hProcess);
|
||||
MemUpdateMap();
|
||||
GuiUpdateMemoryView();
|
||||
}
|
||||
Sleep(1000);
|
||||
|
@ -599,7 +599,7 @@ static void cbCreateProcess(CREATE_PROCESS_DEBUG_INFO* CreateProcessInfo)
|
|||
|
||||
//update memory map
|
||||
dbggetprivateusage(fdProcessInfo->hProcess, true);
|
||||
MemUpdateMap(fdProcessInfo->hProcess);
|
||||
MemUpdateMap();
|
||||
GuiUpdateMemoryView();
|
||||
|
||||
GuiDumpAt(MemFindBaseAddr(GetContextData(UE_CIP), 0) + PAGE_SIZE); //dump somewhere
|
||||
|
@ -733,7 +733,7 @@ static void cbCreateThread(CREATE_THREAD_DEBUG_INFO* CreateThread)
|
|||
{
|
||||
//update memory map
|
||||
dbggetprivateusage(fdProcessInfo->hProcess, true);
|
||||
MemUpdateMap(fdProcessInfo->hProcess);
|
||||
MemUpdateMap();
|
||||
//update GUI
|
||||
GuiSetDebugState(paused);
|
||||
DebugUpdateGui(GetContextDataEx(hActiveThread, UE_CIP), true);
|
||||
|
@ -831,7 +831,7 @@ static void cbLoadDll(LOAD_DLL_DEBUG_INFO* LoadDll)
|
|||
|
||||
//update memory map
|
||||
dbggetprivateusage(fdProcessInfo->hProcess, true);
|
||||
MemUpdateMap(fdProcessInfo->hProcess);
|
||||
MemUpdateMap();
|
||||
GuiUpdateMemoryView();
|
||||
|
||||
char modname[256] = "";
|
||||
|
@ -950,7 +950,7 @@ static void cbUnloadDll(UNLOAD_DLL_DEBUG_INFO* UnloadDll)
|
|||
|
||||
//update memory map
|
||||
dbggetprivateusage(fdProcessInfo->hProcess, true);
|
||||
MemUpdateMap(fdProcessInfo->hProcess);
|
||||
MemUpdateMap();
|
||||
GuiUpdateMemoryView();
|
||||
}
|
||||
|
||||
|
@ -1023,7 +1023,7 @@ static void cbException(EXCEPTION_DEBUG_INFO* ExceptionData)
|
|||
SetNextDbgContinueStatus(DBG_CONTINUE);
|
||||
GuiSetDebugState(paused);
|
||||
dbggetprivateusage(fdProcessInfo->hProcess, true);
|
||||
MemUpdateMap(fdProcessInfo->hProcess);
|
||||
MemUpdateMap();
|
||||
DebugUpdateGui(GetContextDataEx(hActiveThread, UE_CIP), true);
|
||||
//lock
|
||||
lock(WAITID_RUN);
|
||||
|
@ -1939,7 +1939,7 @@ bool dbgsetcmdline(const char* cmd_line, cmdline_error_t* cmd_line_error)
|
|||
|
||||
new_command_line.Buffer = command_linewstr();
|
||||
|
||||
uint mem = (uint)MemAllocRemote(0, new_command_line.Length * 2, PAGE_READWRITE);
|
||||
uint mem = (uint)MemAllocRemote(0, new_command_line.Length * 2, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
|
||||
if(!mem)
|
||||
{
|
||||
cmd_line_error->type = CMDL_ERR_ALLOC_UNICODEANSI_COMMANDLINE;
|
||||
|
@ -1980,7 +1980,7 @@ bool dbggetcmdline(char** cmd_line, cmdline_error_t* cmd_line_error)
|
|||
UNICODE_STRING CommandLine;
|
||||
cmdline_error_t cmd_line_error_aux;
|
||||
|
||||
if(cmd_line_error == NULL)
|
||||
if(!cmd_line_error)
|
||||
cmd_line_error = &cmd_line_error_aux;
|
||||
|
||||
if(!getcommandlineaddr(&cmd_line_error->addr, cmd_line_error))
|
||||
|
|
|
@ -789,7 +789,7 @@ CMDRESULT cbDebugAlloc(int argc, char* argv[])
|
|||
if(argc > 1)
|
||||
if(!valfromstring(argv[1], &size, false))
|
||||
return STATUS_ERROR;
|
||||
uint mem = (uint)MemAllocRemote(0, size, PAGE_EXECUTE_READWRITE);
|
||||
uint mem = (uint)MemAllocRemote(0, size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
|
||||
if(!mem)
|
||||
dputs("VirtualAllocEx failed");
|
||||
else
|
||||
|
@ -798,7 +798,7 @@ CMDRESULT cbDebugAlloc(int argc, char* argv[])
|
|||
varset("$lastalloc", mem, true);
|
||||
//update memory map
|
||||
dbggetprivateusage(fdProcessInfo->hProcess, true);
|
||||
MemUpdateMap(fdProcessInfo->hProcess);
|
||||
MemUpdateMap();
|
||||
GuiUpdateMemoryView();
|
||||
|
||||
varset("$res", mem, false);
|
||||
|
@ -827,7 +827,7 @@ CMDRESULT cbDebugFree(int argc, char* argv[])
|
|||
dputs("VirtualFreeEx failed");
|
||||
//update memory map
|
||||
dbggetprivateusage(fdProcessInfo->hProcess, true);
|
||||
MemUpdateMap(fdProcessInfo->hProcess);
|
||||
MemUpdateMap();
|
||||
GuiUpdateMemoryView();
|
||||
|
||||
varset("$res", ok, false);
|
||||
|
@ -1851,7 +1851,7 @@ CMDRESULT cbDebugSetPageRights(int argc, char* argv[])
|
|||
|
||||
if(argc < 3 || !valfromstring(argv[1], &addr))
|
||||
{
|
||||
dprintf("Error: using an address as arg1 and as arg2: Execute, ExecuteRead, ExecuteReadWrite, ExecuteWriteCopy, NoAccess, ReadOnly, ReadWrite, WriteCopy. You can add a G at first for add PAGE GUARD, example: GReadOnly\n");
|
||||
dprintf("Error: Using an address as arg1 and as arg2: Execute, ExecuteRead, ExecuteReadWrite, ExecuteWriteCopy, NoAccess, ReadOnly, ReadWrite, WriteCopy. You can add a G at first for add PAGE GUARD, example: GReadOnly\n");
|
||||
return STATUS_ERROR;
|
||||
}
|
||||
|
||||
|
@ -1869,7 +1869,7 @@ CMDRESULT cbDebugSetPageRights(int argc, char* argv[])
|
|||
|
||||
//update the memory map
|
||||
dbggetprivateusage(fdProcessInfo->hProcess, true);
|
||||
MemUpdateMap(fdProcessInfo->hProcess);
|
||||
MemUpdateMap();
|
||||
GuiUpdateMemoryView();
|
||||
|
||||
dprintf("New rights of "fhex": %s\n", addr, rights);
|
||||
|
@ -1888,8 +1888,8 @@ CMDRESULT cbDebugLoadLib(int argc, char* argv[])
|
|||
LoadLibThreadID = fdProcessInfo->dwThreadId;
|
||||
HANDLE LoadLibThread = ThreadGetHandle((DWORD)LoadLibThreadID);
|
||||
|
||||
DLLNameMem = (uint)VirtualAllocEx(fdProcessInfo->hProcess, NULL, strlen(argv[1]) + 1, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
|
||||
ASMAddr = (uint)VirtualAllocEx(fdProcessInfo->hProcess, NULL, 0x1000, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
|
||||
DLLNameMem = MemAllocRemote(0, strlen(argv[1]) + 1, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
|
||||
ASMAddr = MemAllocRemote(0, 0x1000, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
|
||||
|
||||
if(!DLLNameMem || !ASMAddr)
|
||||
{
|
||||
|
@ -1962,8 +1962,8 @@ void cbDebugLoadLibBPX()
|
|||
varset("$result", LibAddr, false);
|
||||
backupctx.eflags &= ~0x100;
|
||||
SetFullContextDataEx(LoadLibThread, &backupctx);
|
||||
VirtualFreeEx(fdProcessInfo->hProcess, (LPVOID)DLLNameMem, 0, MEM_RELEASE);
|
||||
VirtualFreeEx(fdProcessInfo->hProcess, (LPVOID)ASMAddr, 0, MEM_RELEASE);
|
||||
MemFreeRemote(DLLNameMem);
|
||||
MemFreeRemote(ASMAddr);
|
||||
ThreadResumeAll();
|
||||
//update GUI
|
||||
GuiSetDebugState(paused);
|
||||
|
@ -2075,7 +2075,7 @@ CMDRESULT cbDebugSetCmdline(int argc, char* argv[])
|
|||
|
||||
//update the memory map
|
||||
dbggetprivateusage(fdProcessInfo->hProcess, true);
|
||||
MemUpdateMap(fdProcessInfo->hProcess);
|
||||
MemUpdateMap();
|
||||
GuiUpdateMemoryView();
|
||||
|
||||
dprintf("New command line: %s\n", argv[1]);
|
||||
|
|
|
@ -120,6 +120,7 @@ void FunctionDelRange(uint Start, uint End)
|
|||
for(auto itr = functions.begin(); itr != functions.end();)
|
||||
{
|
||||
const auto & currentFunction = itr->second;
|
||||
|
||||
// Ignore manually set entries
|
||||
if(currentFunction.manual)
|
||||
{
|
||||
|
|
|
@ -2058,7 +2058,7 @@ CMDRESULT cbInstrMeminfo(int argc, char* argv[])
|
|||
else if(argv[1][0] == 'r')
|
||||
{
|
||||
dbggetprivateusage(fdProcessInfo->hProcess, true);
|
||||
MemUpdateMap(fdProcessInfo->hProcess);
|
||||
MemUpdateMap();
|
||||
GuiUpdateMemoryView();
|
||||
dputs("memory map updated!");
|
||||
}
|
||||
|
|
|
@ -19,10 +19,8 @@
|
|||
std::map<Range, MEMPAGE, RangeCompare> memoryPages;
|
||||
bool bListAllPages = false;
|
||||
|
||||
void MemUpdateMap(HANDLE hProcess)
|
||||
void MemUpdateMap()
|
||||
{
|
||||
EXCLUSIVE_ACQUIRE(LockMemoryPages);
|
||||
|
||||
// First gather all possible pages in the memory range
|
||||
std::vector<MEMPAGE> pageVector;
|
||||
{
|
||||
|
@ -36,7 +34,7 @@ void MemUpdateMap(HANDLE hProcess)
|
|||
MEMORY_BASIC_INFORMATION mbi;
|
||||
memset(&mbi, 0, sizeof(mbi));
|
||||
|
||||
numBytes = VirtualQueryEx(hProcess, (LPVOID)pageStart, &mbi, sizeof(mbi));
|
||||
numBytes = VirtualQueryEx(fdProcessInfo->hProcess, (LPVOID)pageStart, &mbi, sizeof(mbi));
|
||||
|
||||
// Only allow pages that are committed to memory (exclude reserved/mapped)
|
||||
if(mbi.State == MEM_COMMIT)
|
||||
|
@ -99,7 +97,7 @@ void MemUpdateMap(HANDLE hProcess)
|
|||
{
|
||||
const auto & currentSection = sections.at(j);
|
||||
memset(&newPage, 0, sizeof(MEMPAGE));
|
||||
VirtualQueryEx(hProcess, (LPCVOID)currentSection.addr, &newPage.mbi, sizeof(MEMORY_BASIC_INFORMATION));
|
||||
VirtualQueryEx(fdProcessInfo->hProcess, (LPCVOID)currentSection.addr, &newPage.mbi, sizeof(MEMORY_BASIC_INFORMATION));
|
||||
uint SectionSize = currentSection.size;
|
||||
if(SectionSize % PAGE_SIZE) //unaligned page size
|
||||
SectionSize += PAGE_SIZE - (SectionSize % PAGE_SIZE); //fix this
|
||||
|
@ -110,7 +108,7 @@ void MemUpdateMap(HANDLE hProcess)
|
|||
}
|
||||
//insert the module itself (the module header)
|
||||
memset(&newPage, 0, sizeof(MEMPAGE));
|
||||
VirtualQueryEx(hProcess, (LPCVOID)base, &newPage.mbi, sizeof(MEMORY_BASIC_INFORMATION));
|
||||
VirtualQueryEx(fdProcessInfo->hProcess, (LPCVOID)base, &newPage.mbi, sizeof(MEMORY_BASIC_INFORMATION));
|
||||
strcpy_s(newPage.info, curMod);
|
||||
pageVector.insert(pageVector.begin() + i, newPage);
|
||||
}
|
||||
|
@ -143,6 +141,7 @@ void MemUpdateMap(HANDLE hProcess)
|
|||
}
|
||||
|
||||
// Convert the vector to a map
|
||||
EXCLUSIVE_ACQUIRE(LockMemoryPages);
|
||||
memoryPages.clear();
|
||||
|
||||
for(auto & page : pageVector)
|
||||
|
@ -157,7 +156,7 @@ uint MemFindBaseAddr(uint Address, uint* Size, bool Refresh)
|
|||
{
|
||||
// Update the memory map if needed
|
||||
if(Refresh)
|
||||
MemUpdateMap(fdProcessInfo->hProcess);
|
||||
MemUpdateMap();
|
||||
|
||||
SHARED_ACQUIRE(LockMemoryPages);
|
||||
|
||||
|
@ -326,12 +325,12 @@ bool MemIsCanonicalAddress(uint Address)
|
|||
#endif // ndef _WIN64
|
||||
}
|
||||
|
||||
void* MemAllocRemote(uint Address, SIZE_T Size, DWORD Protect)
|
||||
uint MemAllocRemote(uint Address, uint Size, DWORD Type, DWORD Protect)
|
||||
{
|
||||
return VirtualAllocEx(fdProcessInfo->hProcess, (void*)Address, Size, MEM_RESERVE | MEM_COMMIT, Protect);
|
||||
return (uint)VirtualAllocEx(fdProcessInfo->hProcess, (LPVOID)Address, Size, Type, Protect);
|
||||
}
|
||||
|
||||
bool MemFreeRemote(uint Address)
|
||||
{
|
||||
return !!VirtualFreeEx(fdProcessInfo->hProcess, (void*)Address, 0, MEM_RELEASE);
|
||||
return !!VirtualFreeEx(fdProcessInfo->hProcess, (LPVOID)Address, 0, MEM_RELEASE);
|
||||
}
|
|
@ -6,12 +6,12 @@
|
|||
extern std::map<Range, MEMPAGE, RangeCompare> memoryPages;
|
||||
extern bool bListAllPages;
|
||||
|
||||
void MemUpdateMap(HANDLE hProcess);
|
||||
void MemUpdateMap();
|
||||
uint MemFindBaseAddr(uint Address, uint* Size, bool Refresh = false);
|
||||
bool MemRead(uint BaseAddress, void* Buffer, SIZE_T Size, SIZE_T* NumberOfBytesRead);
|
||||
bool MemWrite(uint BaseAddress, const void* Buffer, SIZE_T Size, SIZE_T* NumberOfBytesWritten);
|
||||
bool MemPatch(uint BaseAddress, const void* Buffer, SIZE_T Size, SIZE_T* NumberOfBytesWritten);
|
||||
bool MemIsValidReadPtr(uint Address);
|
||||
bool MemIsCanonicalAddress(uint Address);
|
||||
void* MemAllocRemote(uint Address, SIZE_T Size, DWORD Protect);
|
||||
uint MemAllocRemote(uint Address, uint Size, DWORD Type, DWORD Protect);
|
||||
bool MemFreeRemote(uint Address);
|
Loading…
Reference in New Issue