1
0
Fork 0

DBG: small refactoring + removed some unused variables

This commit is contained in:
Mr. eXoDia 2015-07-11 03:09:21 +02:00
parent e1bd4a288c
commit e8ee2430c6
20 changed files with 102 additions and 91 deletions

View File

@ -80,18 +80,19 @@ void BookmarkDelRange(uint Start, uint End)
EXCLUSIVE_ACQUIRE(LockBookmarks);
for(auto itr = bookmarks.begin(); itr != bookmarks.end();)
{
const auto & currentBookmark = itr->second;
// Ignore manually set entries
if(itr->second.manual)
if(currentBookmark.manual)
{
itr++;
++itr;
continue;
}
// [Start, End)
if(itr->second.addr >= Start && itr->second.addr < End)
if(currentBookmark.addr >= Start && currentBookmark.addr < End)
itr = bookmarks.erase(itr);
else
itr++;
++itr;
}
}
}

View File

@ -109,6 +109,6 @@ void Command::dataFinish()
if(_data.length())
{
_tokens.push_back(_data);
_data = "";
_data.clear();
}
}

View File

@ -104,18 +104,19 @@ void CommentDelRange(uint Start, uint End)
EXCLUSIVE_ACQUIRE(LockComments);
for(auto itr = comments.begin(); itr != comments.end();)
{
const auto & currentComment = itr->second;
// Ignore manually set entries
if(itr->second.manual)
if(currentComment.manual)
{
itr++;
++itr;
continue;
}
// [Start, End)
if(itr->second.addr >= Start && itr->second.addr < End)
if(currentComment.addr >= Start && currentComment.addr < End)
itr = comments.erase(itr);
else
itr++;
++itr;
}
}
}

View File

@ -113,7 +113,7 @@ void ControlFlowAnalysis::BasicBlockStarts()
void ControlFlowAnalysis::BasicBlocks()
{
for(auto i = _blockStarts.begin(); i != _blockStarts.end(); i++)
for(auto i = _blockStarts.begin(); i != _blockStarts.end(); ++i)
{
uint start = *i;
if(!IsValidAddress(start))

View File

@ -968,14 +968,14 @@ static void cbOutputDebugString(OUTPUT_DEBUG_STRING_INFO* DebugString)
if(MemRead(DebugString->lpDebugStringData, DebugText, DebugString->nDebugStringLength, 0))
{
String str = String(DebugText);
if(str != lastDebugText) //fix for every string being printed twice
if(str != lastDebugText) //fix for every string being printed twice
{
if(str != "\n")
dprintf("DebugString: \"%s\"\n", StringUtils::Escape(str).c_str());
lastDebugText = str;
}
else
lastDebugText = "";
lastDebugText.clear();
}
}

View File

@ -875,7 +875,6 @@ CMDRESULT cbDebugBenchmark(int argc, char* argv[])
{
uint addr = MemFindBaseAddr(GetContextDataEx(hActiveThread, UE_CIP), 0);
DWORD ticks = GetTickCount();
char comment[MAX_COMMENT_SIZE] = "";
for(uint i = addr; i < addr + 100000; i++)
{
CommentSet(i, "test", false);

View File

@ -201,12 +201,13 @@ void disasmget(unsigned char* buffer, uint addr, DISASM_INSTR* instr)
instr->argcount = 0;
return;
}
sprintf_s(instr->instruction, "%s %s", cp.GetInstr()->mnemonic, cp.GetInstr()->op_str);
const cs_x86 & x86 = cp.GetInstr()->detail->x86;
instr->instr_size = cp.GetInstr()->size;
const cs_insn* cpInstr = cp.GetInstr();
sprintf_s(instr->instruction, "%s %s", cpInstr->mnemonic, cpInstr->op_str);
const cs_x86 & x86 = cpInstr->detail->x86;
instr->instr_size = cpInstr->size;
if(cp.InGroup(CS_GRP_JUMP) || cp.IsLoop() || cp.InGroup(CS_GRP_RET) || cp.InGroup(CS_GRP_CALL))
instr->type = instr_branch;
else if(strstr(cp.GetInstr()->op_str, "sp") || strstr(cp.GetInstr()->op_str, "bp"))
else if(strstr(cpInstr->op_str, "sp") || strstr(cpInstr->op_str, "bp"))
instr->type = instr_stack;
else
instr->type = instr_normal;

View File

@ -199,7 +199,7 @@ void ExpressionParser::addOperatorToken(const char ch, const Token::Type type)
if(_curToken.length()) //add a new data token when there is data in the buffer
{
_tokens.push_back(Token(_curToken, Token::Type::Data));
_curToken = "";
_curToken.clear();
}
String data;
data += ch;

View File

@ -8,7 +8,7 @@ bool FileReader::ReadAllText(const String & fileName, String & content)
unsigned int filesize = GetFileSize(hFile, 0);
if(!filesize)
{
content = "";
content.clear();
return true;
}
Memory<char*> filedata(filesize + 1, "FileReader::ReadAllText:filedata");

View File

@ -120,18 +120,19 @@ void FunctionDelRange(uint Start, uint End)
EXCLUSIVE_ACQUIRE(LockFunctions);
for(auto itr = functions.begin(); itr != functions.end();)
{
const auto & currentFunction = itr->second;
// Ignore manually set entries
if(itr->second.manual)
if(currentFunction.manual)
{
itr++;
++itr;
continue;
}
// [Start, End]
if(itr->second.end >= Start && itr->second.start <= End)
if(currentFunction.end >= Start && currentFunction.start <= End)
itr = functions.erase(itr);
else
itr++;
++itr;
}
}
}

View File

@ -124,18 +124,19 @@ void LabelDelRange(uint Start, uint End)
EXCLUSIVE_ACQUIRE(LockLabels);
for(auto itr = labels.begin(); itr != labels.end();)
{
const auto & currentLabel = itr->second;
// Ignore manually set entries
if(itr->second.manual)
if(currentLabel.manual)
{
itr++;
++itr;
continue;
}
// [Start, End)
if(itr->second.addr >= Start && itr->second.addr < End)
if(currentLabel.addr >= Start && currentLabel.addr < End)
itr = labels.erase(itr);
else
itr++;
++itr;
}
}
}

View File

@ -60,10 +60,11 @@ void MemUpdateMap(HANDLE hProcess)
char curMod[MAX_MODULE_SIZE] = "";
for(int i = pagecount - 1; i > -1; i--)
{
if(!pageVector.at(i).info[0] || (scmp(curMod, pageVector.at(i).info) && !bListAllPages)) //there is a module
auto & currentPage = pageVector.at(i);
if(!currentPage.info[0] || (scmp(curMod, currentPage.info) && !bListAllPages)) //there is a module
continue; //skip non-modules
strcpy(curMod, pageVector.at(i).info);
uint base = ModBaseFromName(pageVector.at(i).info);
uint base = ModBaseFromName(currentPage.info);
if(!base)
continue;
std::vector<MODSECTIONINFO> sections;
@ -79,14 +80,15 @@ void MemUpdateMap(HANDLE hProcess)
pageVector.erase(pageVector.begin() + i); //remove the SizeOfImage page
for(int j = SectionNumber - 1; j > -1; j--)
{
const auto & currentSection = sections.at(j);
memset(&newPage, 0, sizeof(MEMPAGE));
VirtualQueryEx(hProcess, (LPCVOID)sections.at(j).addr, &newPage.mbi, sizeof(MEMORY_BASIC_INFORMATION));
uint SectionSize = sections.at(j).size;
VirtualQueryEx(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
if(SectionSize)
newPage.mbi.RegionSize = SectionSize;
sprintf_s(newPage.info, " \"%s\"", sections.at(j).name);
sprintf_s(newPage.info, " \"%s\"", currentSection.name);
pageVector.insert(pageVector.begin() + i, newPage);
}
//insert the module itself (the module header)
@ -97,26 +99,27 @@ void MemUpdateMap(HANDLE hProcess)
}
else //list all pages
{
uint start = (uint)pageVector.at(i).mbi.BaseAddress;
uint end = start + pageVector.at(i).mbi.RegionSize;
uint start = (uint)currentPage.mbi.BaseAddress;
uint end = start + currentPage.mbi.RegionSize;
for(int j = 0, k = 0; j < SectionNumber; j++)
{
uint secStart = sections.at(j).addr;
uint SectionSize = sections.at(j).size;
const auto & currentSection = sections.at(j);
uint secStart = currentSection.addr;
uint SectionSize = currentSection.size;
if(SectionSize % PAGE_SIZE) //unaligned page size
SectionSize += PAGE_SIZE - (SectionSize % PAGE_SIZE); //fix this
uint secEnd = secStart + SectionSize;
if(secStart >= start && secEnd <= end) //section is inside the memory page
{
if(k)
k += sprintf_s(pageVector.at(i).info + k, MAX_MODULE_SIZE - k, ",");
k += sprintf_s(pageVector.at(i).info + k, MAX_MODULE_SIZE - k, " \"%s\"", sections.at(j).name);
k += sprintf_s(currentPage.info + k, MAX_MODULE_SIZE - k, ",");
k += sprintf_s(currentPage.info + k, MAX_MODULE_SIZE - k, " \"%s\"", currentSection.name);
}
else if(start >= secStart && end <= secEnd) //memory page is inside the section
{
if(k)
k += sprintf_s(pageVector.at(i).info + k, MAX_MODULE_SIZE - k, ",");
k += sprintf_s(pageVector.at(i).info + k, MAX_MODULE_SIZE - k, " \"%s\"", sections.at(j).name);
k += sprintf_s(currentPage.info + k, MAX_MODULE_SIZE - k, ",");
k += sprintf_s(currentPage.info + k, MAX_MODULE_SIZE - k, " \"%s\"", currentSection.name);
}
}
}
@ -155,7 +158,7 @@ uint MemFindBaseAddr(uint Address, uint* Size, bool Refresh)
return found->first.first;
}
bool MemRead(void* BaseAddress, void* Buffer, SIZE_T Size, SIZE_T* NumberOfBytesRead)
bool MemRead(const void* BaseAddress, void* Buffer, SIZE_T Size, SIZE_T* NumberOfBytesRead)
{
// Fast fail if address is invalid
if(!MemIsCanonicalAddress((uint)BaseAddress))
@ -172,7 +175,7 @@ bool MemRead(void* BaseAddress, void* Buffer, SIZE_T Size, SIZE_T* NumberOfBytes
NumberOfBytesRead = &bytesReadTemp;
// Normal single-call read
bool ret = MemoryReadSafe(fdProcessInfo->hProcess, BaseAddress, Buffer, Size, NumberOfBytesRead);
bool ret = MemoryReadSafe(fdProcessInfo->hProcess, (LPVOID)BaseAddress, Buffer, Size, NumberOfBytesRead);
if(ret && *NumberOfBytesRead == Size)
return true;
@ -210,7 +213,7 @@ bool MemRead(void* BaseAddress, void* Buffer, SIZE_T Size, SIZE_T* NumberOfBytes
return (*NumberOfBytesRead > 0);
}
bool MemWrite(void* BaseAddress, void* Buffer, SIZE_T Size, SIZE_T* NumberOfBytesWritten)
bool MemWrite(void* BaseAddress, const void* Buffer, SIZE_T Size, SIZE_T* NumberOfBytesWritten)
{
// Fast fail if address is invalid
if(!MemIsCanonicalAddress((uint)BaseAddress))
@ -265,7 +268,7 @@ bool MemWrite(void* BaseAddress, void* Buffer, SIZE_T Size, SIZE_T* NumberOfByte
return (*NumberOfBytesWritten > 0);
}
bool MemPatch(void* BaseAddress, void* Buffer, SIZE_T Size, SIZE_T* NumberOfBytesWritten)
bool MemPatch(void* BaseAddress, const void* Buffer, SIZE_T Size, SIZE_T* NumberOfBytesWritten)
{
// Buffer and size must be valid
if(!Buffer || Size <= 0)
@ -274,7 +277,7 @@ bool MemPatch(void* BaseAddress, void* Buffer, SIZE_T Size, SIZE_T* NumberOfByte
// Allocate the memory
Memory<unsigned char*> oldData(Size, "mempatch:oldData");
if(!MemRead(BaseAddress, oldData, Size, nullptr))
if(!MemRead(BaseAddress, oldData(), Size, nullptr))
{
// If no memory can be read, no memory can be written. Fail out
// of this function.
@ -282,7 +285,7 @@ bool MemPatch(void* BaseAddress, void* Buffer, SIZE_T Size, SIZE_T* NumberOfByte
}
for(SIZE_T i = 0; i < Size; i++)
PatchSet((uint)BaseAddress + i, oldData[i], ((unsigned char*)Buffer)[i]);
PatchSet((uint)BaseAddress + i, oldData()[i], ((const unsigned char*)Buffer)[i]);
return MemWrite(BaseAddress, Buffer, Size, NumberOfBytesWritten);
}
@ -290,7 +293,7 @@ bool MemPatch(void* BaseAddress, void* Buffer, SIZE_T Size, SIZE_T* NumberOfByte
bool MemIsValidReadPtr(uint Address)
{
unsigned char a = 0;
return MemRead((void*)Address, &a, sizeof(unsigned char), nullptr);
return MemRead((const void*)Address, &a, sizeof(unsigned char), nullptr);
}
bool MemIsCanonicalAddress(uint Address)

View File

@ -8,9 +8,9 @@ extern bool bListAllPages;
void MemUpdateMap(HANDLE hProcess);
uint MemFindBaseAddr(uint Address, uint* Size, bool Refresh = false);
bool MemRead(void* BaseAddress, void* Buffer, SIZE_T Size, SIZE_T* NumberOfBytesRead);
bool MemWrite(void* BaseAddress, void* Buffer, SIZE_T Size, SIZE_T* NumberOfBytesWritten);
bool MemPatch(void* BaseAddress, void* Buffer, SIZE_T Size, SIZE_T* NumberOfBytesWritten);
bool MemRead(const void* BaseAddress, void* Buffer, SIZE_T Size, SIZE_T* NumberOfBytesRead);
bool MemWrite(void* BaseAddress, const void* Buffer, SIZE_T Size, SIZE_T* NumberOfBytesWritten);
bool MemPatch(void* 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);

View File

@ -200,15 +200,16 @@ uint ModBaseFromName(const char* Module)
SHARED_ACQUIRE(LockModules);
for(auto itr = modinfo.begin(); itr != modinfo.end(); itr++)
for(const auto & i : modinfo)
{
char currentModule[MAX_MODULE_SIZE];
strcpy_s(currentModule, itr->second.name);
strcat_s(currentModule, itr->second.extension);
const auto & currentModule = i.second;
char currentModuleName[MAX_MODULE_SIZE];
strcpy_s(currentModuleName, currentModule.name);
strcat_s(currentModuleName, currentModule.extension);
// Test with and without extension
if(!_stricmp(currentModule, Module) || !_stricmp(itr->second.name, Module))
return itr->second.base;
if(!_stricmp(currentModuleName, Module) || !_stricmp(currentModule.name, Module))
return currentModule.base;
}
return 0;

View File

@ -142,17 +142,18 @@ void PatchDelRange(uint Start, uint End, bool Restore)
EXCLUSIVE_ACQUIRE(LockPatches);
for(auto itr = patches.begin(); itr != patches.end();)
{
const auto & currentPatch = itr->second;
// [Start, End)
if(itr->second.addr >= Start && itr->second.addr < End)
if(currentPatch.addr >= Start && currentPatch.addr < End)
{
// Restore the original byte if necessary
if(Restore)
MemWrite((void*)(itr->second.addr + moduleBase), &itr->second.oldbyte, sizeof(char), nullptr);
MemWrite((void*)(currentPatch.addr + moduleBase), &currentPatch.oldbyte, sizeof(char), nullptr);
itr = patches.erase(itr);
}
else
itr++;
++itr;
}
}
}
@ -313,7 +314,7 @@ void PatchClear(const char* Module)
if(!_stricmp(itr->second.mod, Module))
itr = patches.erase(itr);
else
itr++;
++itr;
}
}
}

View File

@ -293,11 +293,12 @@ void pluginunload()
int pluginCount = (int)pluginList.size();
for(int i = pluginCount - 1; i > -1; i--)
{
PLUGSTOP stop = pluginList.at(i).plugstop;
const auto & currentPlugin = pluginList.at(i);
PLUGSTOP stop = currentPlugin.plugstop;
if(stop)
stop();
plugincmdunregisterall(pluginList.at(i).initStruct.pluginHandle);
FreeLibrary(pluginList.at(i).hPlugin);
plugincmdunregisterall(currentPlugin.initStruct.pluginHandle);
FreeLibrary(currentPlugin.hPlugin);
pluginList.erase(pluginList.begin() + i);
}
pluginCallbackList.clear(); //remove all callbacks
@ -445,15 +446,15 @@ int pluginmenuadd(int hMenu, const char* title)
*/
bool pluginmenuaddentry(int hMenu, int hEntry, const char* title)
{
if(!title or !strlen(title) or hEntry == -1)
if(!title || !strlen(title) or hEntry == -1)
return false;
int pluginHandle = -1;
//find plugin handle
for(unsigned int i = 0; i < pluginMenuList.size(); i++)
for(const auto & currentMenu : pluginMenuList)
{
if(pluginMenuList.at(i).hEntryMenu == hMenu and pluginMenuList.at(i).hEntryPlugin == -1)
if(currentMenu.hEntryMenu == hMenu && currentMenu.hEntryPlugin == -1)
{
pluginHandle = pluginMenuList.at(i).pluginHandle;
pluginHandle = currentMenu.pluginHandle;
break;
}
}
@ -526,20 +527,17 @@ void pluginmenucall(int hEntry)
{
if(hEntry == -1)
return;
for(unsigned int i = 0; i < pluginMenuList.size(); i++)
for(const auto & currentMenu : pluginMenuList)
{
if(pluginMenuList.at(i).hEntryMenu == hEntry && pluginMenuList.at(i).hEntryPlugin != -1)
if(currentMenu.hEntryMenu == hEntry && currentMenu.hEntryPlugin != -1)
{
PLUG_CB_MENUENTRY menuEntryInfo;
menuEntryInfo.hEntry = pluginMenuList.at(i).hEntryPlugin;
int pluginCallbackCount = (int)pluginCallbackList.size();
int pluginHandle = pluginMenuList.at(i).pluginHandle;
for(int j = 0; j < pluginCallbackCount; j++)
menuEntryInfo.hEntry = currentMenu.hEntryPlugin;
for(const auto & currentCallback : pluginCallbackList)
{
if(pluginCallbackList.at(j).pluginHandle == pluginHandle and pluginCallbackList.at(j).cbType == CB_MENUENTRY)
if(currentCallback.pluginHandle == currentMenu.pluginHandle && currentCallback.cbType == CB_MENUENTRY)
{
//TODO: handle exceptions
pluginCallbackList.at(j).cbPlugin(CB_MENUENTRY, &menuEntryInfo);
currentCallback.cbPlugin(CB_MENUENTRY, &menuEntryInfo);
return;
}
}
@ -605,12 +603,11 @@ void pluginmenuentryseticon(int pluginHandle, int hEntry, const ICONDATA* icon)
{
if(hEntry == -1)
return;
bool bFound = false;
for(unsigned int i = 0; i < pluginMenuList.size(); i++)
for(const auto & currentMenu : pluginMenuList)
{
if(pluginMenuList.at(i).pluginHandle == pluginHandle && pluginMenuList.at(i).hEntryPlugin == hEntry)
if(currentMenu.pluginHandle == pluginHandle && currentMenu.hEntryPlugin == hEntry)
{
GuiMenuSetEntryIcon(pluginMenuList.at(i).hEntryMenu, icon);
GuiMenuSetEntryIcon(currentMenu.hEntryMenu, icon);
break;
}
}

View File

@ -59,12 +59,17 @@ static int scriptlabelfind(const char* labelname)
return 0;
}
static inline bool isEmptyLine(SCRIPTLINETYPE type)
{
return type == lineempty || type == linecomment || type == linelabel;
}
static int scriptinternalstep(int fromIp) //internal step routine
{
int maxIp = (int)linemap.size(); //maximum ip
if(fromIp >= maxIp) //script end
return fromIp;
while((linemap.at(fromIp).type == lineempty or linemap.at(fromIp).type == linecomment or linemap.at(fromIp).type == linelabel) and fromIp < maxIp) //skip empty lines
while(isEmptyLine(linemap.at(fromIp).type) && fromIp < maxIp) //skip empty lines
fromIp++;
fromIp++;
return fromIp;
@ -143,13 +148,13 @@ static bool scriptcreatelinemap(const char* filename)
strcpy_s(entry.raw, temp);
linemap.push_back(entry);
}
unsigned int linemapsize = (unsigned int)linemap.size();
int linemapsize = (int)linemap.size();
while(!*linemap.at(linemapsize - 1).raw) //remove empty lines from the end
{
linemapsize--;
linemap.pop_back();
}
for(unsigned int i = 0; i < linemapsize; i++)
for(int i = 0; i < linemapsize; i++)
{
LINEMAPENTRY cur = linemap.at(i);
@ -232,21 +237,22 @@ static bool scriptcreatelinemap(const char* filename)
linemap.at(i) = cur;
}
linemapsize = (int)linemap.size();
for(unsigned int i = 0; i < linemapsize; i++)
for(int i = 0; i < linemapsize; i++)
{
if(linemap.at(i).type == linebranch) //invalid branch label
auto & currentLine = linemap.at(i);
if(currentLine.type == linebranch) //invalid branch label
{
int labelline = scriptlabelfind(linemap.at(i).u.branch.branchlabel);
int labelline = scriptlabelfind(currentLine.u.branch.branchlabel);
if(!labelline) //invalid branch label
{
char message[256] = "";
sprintf(message, "Invalid branch label \"%s\" detected on line %d!", linemap.at(i).u.branch.branchlabel, i + 1);
sprintf(message, "Invalid branch label \"%s\" detected on line %d!", currentLine.u.branch.branchlabel, i + 1);
GuiScriptError(0, message);
std::vector<LINEMAPENTRY>().swap(linemap);
return false;
}
else //set the branch destination line
linemap.at(i).u.branch.dest = scriptinternalstep(labelline);
currentLine.u.branch.dest = scriptinternalstep(labelline);
}
}
if(linemap.at(linemapsize - 1).type == linecomment or linemap.at(linemapsize - 1).type == linelabel) //label/comment on the end

View File

@ -92,7 +92,7 @@ bool stackcommentget(uint addr, STACK_COMMENT* comment)
strcpy_s(label, addrinfo.label);
char module[MAX_MODULE_SIZE] = "";
ModNameFromAddr(data, module, false);
char addrInfo[MAX_COMMENT_SIZE] = "";
if(*module) //module
{
if(*label) //+label

View File

@ -86,7 +86,6 @@ static unsigned int getArgNumType(const String & formatString, ValueType::ValueT
static String handleFormatString(const String & formatString, const FormatValueVector & values)
{
String result;
ValueType::ValueType type = ValueType::Unknown;
unsigned int argnum = getArgNumType(formatString, type);
if(argnum < values.size())

View File

@ -41,7 +41,7 @@ void ThreadExit(DWORD ThreadId)
EXCLUSIVE_ACQUIRE(LockThreads);
// Don't use a foreach loop here because of the erase() call
for(auto itr = threadList.begin(); itr != threadList.end(); itr++)
for(auto itr = threadList.begin(); itr != threadList.end(); ++itr)
{
if(itr->ThreadId == ThreadId)
{