DBG: fixed all memory leaks (from emalloc&efree)
This commit is contained in:
parent
d80b5cf1c3
commit
da3fdf53a3
|
@ -4,6 +4,7 @@
|
|||
#include "value.h"
|
||||
#include "addrinfo.h"
|
||||
#include "console.h"
|
||||
#include "threading.h"
|
||||
|
||||
extern "C" DLL_EXPORT duint _dbg_memfindbaseaddr(duint addr, duint* size)
|
||||
{
|
||||
|
@ -62,14 +63,6 @@ extern "C" DLL_EXPORT bool _dbg_memisvalidreadptr(duint addr)
|
|||
return memisvalidreadptr(fdProcessInfo->hProcess, addr);
|
||||
}
|
||||
|
||||
extern "C" DLL_EXPORT void _dbg_dbgexitsignal()
|
||||
{
|
||||
//TODO: handle exit signal
|
||||
cbStopDebug("");
|
||||
Sleep(200);
|
||||
DeleteFileA("DLLLoader.exe");
|
||||
}
|
||||
|
||||
extern "C" DLL_EXPORT bool _dbg_valfromstring(const char* string, duint* value)
|
||||
{
|
||||
return valfromstring(string, value, 0, 0, true, 0);
|
||||
|
|
|
@ -11,8 +11,7 @@ extern "C"
|
|||
DLL_EXPORT duint _dbg_memfindbaseaddr(duint addr, duint* size);
|
||||
DLL_EXPORT bool _dbg_memread(duint addr, unsigned char* dest, duint size, duint* read);
|
||||
DLL_EXPORT bool _dbg_memmap(MEMMAP* memmap);
|
||||
extern "C" DLL_EXPORT bool _dbg_memisvalidreadptr(duint addr);
|
||||
DLL_EXPORT void _dbg_dbgexitsignal();
|
||||
DLL_EXPORT bool _dbg_memisvalidreadptr(duint addr);
|
||||
DLL_EXPORT bool _dbg_valfromstring(const char* string, duint* value);
|
||||
DLL_EXPORT bool _dbg_isdebugging();
|
||||
DLL_EXPORT bool _dbg_isjumpgoingtoexecute(duint addr);
|
||||
|
|
|
@ -21,6 +21,29 @@ void efree(void* ptr)
|
|||
delete[] (unsigned char*)ptr;
|
||||
}
|
||||
|
||||
static int emalloc_count=0;
|
||||
|
||||
void* emalloc(size_t size, const char* reason)
|
||||
{
|
||||
unsigned char* a=new unsigned char[size+0x1000];
|
||||
if(!a)
|
||||
{
|
||||
MessageBoxA(0, "Could not allocate memory", "Error", MB_ICONERROR);
|
||||
ExitProcess(1);
|
||||
}
|
||||
memset(a, 0, size);
|
||||
emalloc_count++;
|
||||
//printf("DBG%.5d:alloc:"fhex":%s:"fhex"\n", emalloc_count, a, reason, size);
|
||||
return a;
|
||||
}
|
||||
|
||||
void efree(void* ptr, const char* reason)
|
||||
{
|
||||
emalloc_count--;
|
||||
//printf("DBG%.5d:efree:"fhex":%s\n", emalloc_count, ptr, reason);
|
||||
delete[] (unsigned char*)ptr;
|
||||
}
|
||||
|
||||
bool arraycontains(const char* cmd_list, const char* cmd)
|
||||
{
|
||||
if(!cmd_list or !cmd)
|
||||
|
@ -56,26 +79,26 @@ void formathex(char* string)
|
|||
{
|
||||
int len=strlen(string);
|
||||
_strupr(string);
|
||||
char* new_string=(char*)emalloc(len+1);
|
||||
char* new_string=(char*)emalloc(len+1, "formathex:new_string");
|
||||
memset(new_string, 0, len+1);
|
||||
for(int i=0,j=0; i<len; i++)
|
||||
if(isxdigit(string[i]))
|
||||
j+=sprintf(new_string+j, "%c", string[i]);
|
||||
strcpy(string, new_string);
|
||||
efree(new_string);
|
||||
efree(new_string, "formathex:new_string");
|
||||
}
|
||||
|
||||
void formatdec(char* string)
|
||||
{
|
||||
int len=strlen(string);
|
||||
_strupr(string);
|
||||
char* new_string=(char*)emalloc(len+1);
|
||||
char* new_string=(char*)emalloc(len+1, "formatdec:new_string");
|
||||
memset(new_string, 0, len+1);
|
||||
for(int i=0,j=0; i<len; i++)
|
||||
if(isdigit(string[i]))
|
||||
j+=sprintf(new_string+j, "%c", string[i]);
|
||||
strcpy(string, new_string);
|
||||
efree(new_string);
|
||||
efree(new_string, "formatdec:new_string");
|
||||
}
|
||||
|
||||
bool FileExists(const char* file)
|
||||
|
|
|
@ -97,6 +97,8 @@ extern char dbpath[deflen];
|
|||
//functions
|
||||
void* emalloc(size_t size);
|
||||
void efree(void* ptr);
|
||||
void* emalloc(size_t size, const char* reason);
|
||||
void efree(void* ptr, const char* reason);
|
||||
bool arraycontains(const char* cmd_list, const char* cmd);
|
||||
bool scmp(const char* a, const char* b);
|
||||
void formathex(char* string);
|
||||
|
|
|
@ -90,7 +90,9 @@ bool dbsave()
|
|||
void dbclose()
|
||||
{
|
||||
dbsave();
|
||||
sqlite3_db_release_memory(db);
|
||||
sqlite3_close(db); //close program database
|
||||
sqlite3_db_release_memory(internaldb);
|
||||
sqlite3_close(internaldb); //close internal database
|
||||
}
|
||||
|
||||
|
@ -167,22 +169,27 @@ bool modunload(uint base)
|
|||
return false;
|
||||
}
|
||||
|
||||
void modclear()
|
||||
{
|
||||
modinfo.clear();
|
||||
}
|
||||
|
||||
///api functions
|
||||
bool apienumexports(uint base, EXPORTENUMCALLBACK cbEnum)
|
||||
{
|
||||
MEMORY_BASIC_INFORMATION mbi;
|
||||
VirtualQueryEx(fdProcessInfo->hProcess, (const void*)base, &mbi, sizeof(mbi));
|
||||
uint size=mbi.RegionSize;
|
||||
void* buffer=emalloc(size);
|
||||
void* buffer=emalloc(size, "apienumexports:buffer");
|
||||
if(!memread(fdProcessInfo->hProcess, (const void*)base, buffer, size, 0))
|
||||
{
|
||||
efree(buffer);
|
||||
efree(buffer, "apienumexports:buffer");
|
||||
return false;
|
||||
}
|
||||
IMAGE_NT_HEADERS* pnth=(IMAGE_NT_HEADERS*)((uint)buffer+GetPE32DataFromMappedFile((ULONG_PTR)buffer, 0, UE_PE_OFFSET));
|
||||
uint export_dir_rva=pnth->OptionalHeader.DataDirectory[0].VirtualAddress;
|
||||
uint export_dir_size=pnth->OptionalHeader.DataDirectory[0].Size;
|
||||
efree(buffer);
|
||||
efree(buffer, "apienumexports:buffer");
|
||||
IMAGE_EXPORT_DIRECTORY export_dir;
|
||||
memset(&export_dir, 0, sizeof(export_dir));
|
||||
memread(fdProcessInfo->hProcess, (const void*)(export_dir_rva+base), &export_dir, sizeof(export_dir), 0);
|
||||
|
@ -251,7 +258,7 @@ bool commentset(uint addr, const char* text)
|
|||
if(!*text) //NOTE: delete when there is no text
|
||||
return commentdel(addr);
|
||||
int len=strlen(text);
|
||||
char* newtext=(char*)emalloc(len+1);
|
||||
char* newtext=(char*)emalloc(len+1, "commentset:newtext");
|
||||
*newtext=0;
|
||||
for(int i=0,j=0; i<len; i++)
|
||||
{
|
||||
|
@ -269,7 +276,7 @@ bool commentset(uint addr, const char* text)
|
|||
if(sqlite3_prepare_v2(db, sql, -1, &stmt, 0)!=SQLITE_OK)
|
||||
{
|
||||
sqlite3_finalize(stmt);
|
||||
efree(newtext);
|
||||
efree(newtext, "commentset:newtext");
|
||||
return false;
|
||||
}
|
||||
if(sqlite3_step(stmt)==SQLITE_ROW) //there is a comment already
|
||||
|
@ -285,7 +292,7 @@ bool commentset(uint addr, const char* text)
|
|||
if(sqlite3_prepare_v2(db, sql, -1, &stmt, 0)!=SQLITE_OK)
|
||||
{
|
||||
sqlite3_finalize(stmt);
|
||||
efree(newtext);
|
||||
efree(newtext, "commentset:newtext");
|
||||
return false;
|
||||
}
|
||||
if(sqlite3_step(stmt)==SQLITE_ROW) //there is a comment already
|
||||
|
@ -294,7 +301,7 @@ bool commentset(uint addr, const char* text)
|
|||
sprintf(sql, "INSERT INTO comments (mod,addr,text) VALUES ('%s',%"fext"d,'%s')", modname, rva, newtext);
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
efree(newtext);
|
||||
efree(newtext, "commentset:newtext");
|
||||
char* errorText=0;
|
||||
if(sqlite3_exec(db, sql, 0, 0, &errorText)!=SQLITE_OK) //error
|
||||
{
|
||||
|
@ -381,7 +388,7 @@ bool labelset(uint addr, const char* text)
|
|||
if(!*text) //NOTE: delete when there is no text
|
||||
return labeldel(addr);
|
||||
int len=strlen(text);
|
||||
char* newtext=(char*)emalloc(len+1);
|
||||
char* newtext=(char*)emalloc(len+1, "labelset:newtext");
|
||||
*newtext=0;
|
||||
for(int i=0,j=0; i<len; i++)
|
||||
{
|
||||
|
@ -399,6 +406,7 @@ bool labelset(uint addr, const char* text)
|
|||
if(sqlite3_prepare_v2(db, sql, -1, &stmt, 0)!=SQLITE_OK)
|
||||
{
|
||||
sqlite3_finalize(stmt);
|
||||
efree(newtext, "labelset:newtext");
|
||||
return false;
|
||||
}
|
||||
if(sqlite3_step(stmt)==SQLITE_ROW) //there is a label already
|
||||
|
@ -414,6 +422,7 @@ bool labelset(uint addr, const char* text)
|
|||
if(sqlite3_prepare_v2(db, sql, -1, &stmt, 0)!=SQLITE_OK)
|
||||
{
|
||||
sqlite3_finalize(stmt);
|
||||
efree(newtext, "labelset:newtext");
|
||||
return false;
|
||||
}
|
||||
if(sqlite3_step(stmt)==SQLITE_ROW) //there is a label already
|
||||
|
@ -422,7 +431,7 @@ bool labelset(uint addr, const char* text)
|
|||
sprintf(sql, "INSERT INTO labels (mod,addr,text) VALUES ('%s',%"fext"d,'%s')", modname, rva, newtext);
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
efree(newtext);
|
||||
efree(newtext, "labelset:newtext");
|
||||
char* errorText=0;
|
||||
if(sqlite3_exec(db, sql, 0, 0, &errorText)!=SQLITE_OK) //error
|
||||
{
|
||||
|
|
|
@ -22,6 +22,7 @@ bool modnamefromaddr(uint addr, char* modname);
|
|||
uint modbasefromaddr(uint addr);
|
||||
bool modload(uint base, uint size, const char* name);
|
||||
bool modunload(uint base);
|
||||
void modclear();
|
||||
bool apienumexports(uint base, EXPORTENUMCALLBACK cbEnum);
|
||||
bool commentset(uint addr, const char* text);
|
||||
bool commentget(uint addr, char* text);
|
||||
|
|
|
@ -1,29 +1,26 @@
|
|||
#include "breakpoint.h"
|
||||
#include "debugger.h"
|
||||
|
||||
BREAKPOINT* bpinit(BREAKPOINT* breakpoint_list)
|
||||
BREAKPOINT* bpinit()
|
||||
{
|
||||
bool bNext=true;
|
||||
if(!breakpoint_list)
|
||||
bNext=false;
|
||||
BREAKPOINT* cur=breakpoint_list;
|
||||
while(bNext)
|
||||
{
|
||||
BREAKPOINT* next=cur->next;
|
||||
bpdel(breakpoint_list, 0, cur->addr, BPNORMAL);
|
||||
cur=next;
|
||||
if(!cur)
|
||||
bNext=false;
|
||||
}
|
||||
BREAKPOINT* bp;
|
||||
if(!breakpoint_list)
|
||||
bp=(BREAKPOINT*)emalloc(sizeof(BREAKPOINT));
|
||||
else
|
||||
bp=breakpoint_list;
|
||||
BREAKPOINT* bp=(BREAKPOINT*)emalloc(sizeof(BREAKPOINT), "bpinit:bp");
|
||||
memset(bp, 0, sizeof(BREAKPOINT));
|
||||
return bp;
|
||||
}
|
||||
|
||||
void bpfree(BREAKPOINT* breakpoint_list)
|
||||
{
|
||||
BREAKPOINT* cur=breakpoint_list;
|
||||
while(cur)
|
||||
{
|
||||
if(cur->name)
|
||||
efree(cur->name, "bpfree:cur->name");
|
||||
BREAKPOINT* next=cur->next;
|
||||
efree(cur, "bpfree:cur");
|
||||
cur=next;
|
||||
}
|
||||
}
|
||||
|
||||
BREAKPOINT* bpfind(BREAKPOINT* breakpoint_list, const char* name, uint addr, BREAKPOINT** link, BP_TYPE type)
|
||||
{
|
||||
BREAKPOINT* cur=breakpoint_list;
|
||||
|
@ -62,11 +59,11 @@ bool bpnew(BREAKPOINT* breakpoint_list, const char* name, uint addr, short oldby
|
|||
nonext=true;
|
||||
}
|
||||
else
|
||||
bp=(BREAKPOINT*)emalloc(sizeof(BREAKPOINT));
|
||||
bp=(BREAKPOINT*)emalloc(sizeof(BREAKPOINT), "bpnew:bp");
|
||||
memset(bp, 0, sizeof(BREAKPOINT));
|
||||
if(name and *name)
|
||||
{
|
||||
bp->name=(char*)emalloc(strlen(name)+1);
|
||||
bp->name=(char*)emalloc(strlen(name)+1, "bpnew:bp->name");
|
||||
strcpy(bp->name, name);
|
||||
}
|
||||
bp->addr=addr;
|
||||
|
@ -91,8 +88,8 @@ bool bpsetname(BREAKPOINT* breakpoint_list, uint addr, const char* name)
|
|||
BREAKPOINT* found=bpfind(breakpoint_list, 0, addr, 0, BPNOTYPE);
|
||||
if(!found)
|
||||
return false;
|
||||
efree(found->name); //free previous name
|
||||
found->name=(char*)emalloc(strlen(name)+1);
|
||||
efree(found->name, "bpsetname:found->name"); //free previous name
|
||||
found->name=(char*)emalloc(strlen(name)+1, "bpsetname:found->name");
|
||||
strcpy(found->name, name);
|
||||
return true;
|
||||
}
|
||||
|
@ -104,7 +101,7 @@ bool bpdel(BREAKPOINT* breakpoint_list, const char* name, uint addr, BP_TYPE typ
|
|||
if(!found)
|
||||
return false;
|
||||
if(found->name)
|
||||
efree(found->name);
|
||||
efree(found->name, "bpdel:found->name");
|
||||
if(found==breakpoint_list)
|
||||
{
|
||||
BREAKPOINT* next=breakpoint_list->next;
|
||||
|
@ -112,7 +109,7 @@ bool bpdel(BREAKPOINT* breakpoint_list, const char* name, uint addr, BP_TYPE typ
|
|||
{
|
||||
memcpy(breakpoint_list, breakpoint_list->next, sizeof(BREAKPOINT));
|
||||
breakpoint_list->next=next->next;
|
||||
efree(next);
|
||||
efree(next, "bpdel:next");
|
||||
}
|
||||
else
|
||||
memset(breakpoint_list, 0, sizeof(BREAKPOINT));
|
||||
|
@ -120,7 +117,7 @@ bool bpdel(BREAKPOINT* breakpoint_list, const char* name, uint addr, BP_TYPE typ
|
|||
else
|
||||
{
|
||||
prev->next=found->next;
|
||||
efree(found);
|
||||
efree(found, "bpdel:found");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -26,7 +26,8 @@ struct BREAKPOINT
|
|||
};
|
||||
|
||||
//functions
|
||||
BREAKPOINT* bpinit(BREAKPOINT* breakpoint_list);
|
||||
BREAKPOINT* bpinit();
|
||||
void bpfree(BREAKPOINT* breakpoint_list);
|
||||
BREAKPOINT* bpfind(BREAKPOINT* breakpoint_list, const char* name, uint addr, BREAKPOINT** link, BP_TYPE type);
|
||||
bool bpnew(BREAKPOINT* breakpoint_list, const char* name, uint addr, short oldbytes, BP_TYPE type);
|
||||
bool bpsetname(BREAKPOINT* breakpoint_list, uint addr, const char* name);
|
||||
|
|
|
@ -26,7 +26,7 @@ COMMAND* cmdfind(COMMAND* command_list, const char* name, COMMAND** link)
|
|||
|
||||
COMMAND* cmdinit()
|
||||
{
|
||||
COMMAND* cmd=(COMMAND*)emalloc(sizeof(COMMAND));
|
||||
COMMAND* cmd=(COMMAND*)emalloc(sizeof(COMMAND), "cmdinit:cmd");
|
||||
memset(cmd, 0, sizeof(COMMAND));
|
||||
return cmd;
|
||||
}
|
||||
|
@ -36,9 +36,9 @@ void cmdfree(COMMAND* cmd_list)
|
|||
COMMAND* cur=cmd_list;
|
||||
while(cur)
|
||||
{
|
||||
efree(cur->name);
|
||||
efree(cur->name, "cmdfree:cur->name");
|
||||
COMMAND* next=cur->next;
|
||||
efree(cur);
|
||||
efree(cur, "cmdfree:cur");
|
||||
cur=next;
|
||||
}
|
||||
}
|
||||
|
@ -55,9 +55,9 @@ bool cmdnew(COMMAND* command_list, const char* name, CBCOMMAND cbCommand, bool d
|
|||
nonext=true;
|
||||
}
|
||||
else
|
||||
cmd=(COMMAND*)emalloc(sizeof(COMMAND));
|
||||
cmd=(COMMAND*)emalloc(sizeof(COMMAND), "cmdnew:cmd");
|
||||
memset(cmd, 0, sizeof(COMMAND));
|
||||
cmd->name=(char*)emalloc(strlen(name)+1);
|
||||
cmd->name=(char*)emalloc(strlen(name)+1, "cmdnew:cmd->name");
|
||||
strcpy(cmd->name, name);
|
||||
cmd->cbCommand=cbCommand;
|
||||
cmd->debugonly=debugonly;
|
||||
|
@ -105,7 +105,7 @@ bool cmddel(COMMAND* command_list, const char* name)
|
|||
COMMAND* found=cmdfind(command_list, name, &prev);
|
||||
if(!found)
|
||||
return false;
|
||||
efree(found->name);
|
||||
efree(found->name, "cmddel:found->name");
|
||||
if(found==command_list)
|
||||
{
|
||||
COMMAND* next=command_list->next;
|
||||
|
@ -113,7 +113,7 @@ bool cmddel(COMMAND* command_list, const char* name)
|
|||
{
|
||||
memcpy(command_list, command_list->next, sizeof(COMMAND));
|
||||
command_list->next=next->next;
|
||||
efree(next);
|
||||
efree(next, "cmddel:next");
|
||||
}
|
||||
else
|
||||
memset(command_list, 0, sizeof(COMMAND));
|
||||
|
@ -121,7 +121,7 @@ bool cmddel(COMMAND* command_list, const char* name)
|
|||
else
|
||||
{
|
||||
prev->next=found->next;
|
||||
efree(found);
|
||||
efree(found, "cmddel:found");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -137,8 +137,7 @@ CMDRESULT cmdloop(COMMAND* command_list, CBCOMMAND cbUnknownCommand, CBCOMMANDPR
|
|||
{
|
||||
if(!cbUnknownCommand or !cbCommandProvider)
|
||||
return STATUS_ERROR;
|
||||
char* command=(char*)emalloc(deflen);
|
||||
memset(command, 0, deflen);
|
||||
char command[deflen]="";
|
||||
bool bLoop=true;
|
||||
while(bLoop)
|
||||
{
|
||||
|
@ -176,7 +175,6 @@ CMDRESULT cmdloop(COMMAND* command_list, CBCOMMAND cbUnknownCommand, CBCOMMANDPR
|
|||
}
|
||||
}
|
||||
}
|
||||
efree(command);
|
||||
return STATUS_EXIT;
|
||||
}
|
||||
|
||||
|
@ -187,7 +185,7 @@ static void specialformat(char* string)
|
|||
{
|
||||
int len=strlen(string);
|
||||
char* found=strstr(string, "=");
|
||||
char* str=(char*)emalloc(len*2);
|
||||
char* str=(char*)emalloc(len*2, "specialformat:str");
|
||||
memset(str, 0, len*2);
|
||||
if(found) //contains =
|
||||
{
|
||||
|
@ -197,7 +195,7 @@ static void specialformat(char* string)
|
|||
if(!*found)
|
||||
{
|
||||
*found='=';
|
||||
efree(str);
|
||||
efree(str, "specialformat:str");
|
||||
return;
|
||||
}
|
||||
int flen=strlen(found); //n(+)=n++
|
||||
|
@ -225,7 +223,7 @@ static void specialformat(char* string)
|
|||
sprintf(str, "mov %s,%s%c1", string, string, op);
|
||||
strcpy(string, str);
|
||||
}
|
||||
efree(str);
|
||||
efree(str, "specialformat:str");
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -411,12 +411,13 @@ static DWORD WINAPI threadDebugLoop(void* lpParameter)
|
|||
unlock(WAITID_SYSBREAK);
|
||||
return 0;
|
||||
}
|
||||
lock(WAITID_STOP);
|
||||
strcpy(szFileName, init->exe);
|
||||
efree(init); //free init struct
|
||||
efree(init, "threadDebugLoop:init"); //free init struct
|
||||
varset("$hp", (uint)fdProcessInfo->hProcess, true);
|
||||
varset("$pid", fdProcessInfo->dwProcessId, true);
|
||||
ecount=0;
|
||||
bplist=bpinit(bplist);
|
||||
bplist=bpinit();
|
||||
//NOTE: set custom handlers
|
||||
SetCustomHandler(UE_CH_CREATEPROCESS, (void*)cbCreateProcess);
|
||||
SetCustomHandler(UE_CH_SYSTEMBREAKPOINT, (void*)cbSystemBreakpoint);
|
||||
|
@ -429,11 +430,14 @@ static DWORD WINAPI threadDebugLoop(void* lpParameter)
|
|||
DebugLoop();
|
||||
DeleteFileA("DLLLoader.exe");
|
||||
//message the user/do final stuff
|
||||
SymCleanup(fdProcessInfo->hProcess);
|
||||
dbclose();
|
||||
GuiSetDebugState(stopped);
|
||||
bpfree(bplist);
|
||||
dputs("debugging stopped!");
|
||||
varset("$hp", 0, true);
|
||||
varset("$pid", 0, true);
|
||||
unlock(WAITID_STOP);
|
||||
waitclear();
|
||||
return 0;
|
||||
}
|
||||
|
@ -472,7 +476,7 @@ CMDRESULT cbDebugInit(const char* cmd)
|
|||
currentfolder[len]=0;
|
||||
if(DirExists(arg3))
|
||||
strcpy(currentfolder, arg3);
|
||||
INIT_STRUCT* init=(INIT_STRUCT*)emalloc(sizeof(INIT_STRUCT));
|
||||
INIT_STRUCT* init=(INIT_STRUCT*)emalloc(sizeof(INIT_STRUCT), "cbDebugInit:init");
|
||||
memset(init, 0, sizeof(INIT_STRUCT));
|
||||
init->exe=arg1;
|
||||
init->commandline=commandline;
|
||||
|
@ -1182,7 +1186,7 @@ CMDRESULT cbMemWrite(const char* cmd)
|
|||
uint addr=0;
|
||||
if(!valfromstring(arg1, &addr, 0, 0, false, 0))
|
||||
return STATUS_ERROR;
|
||||
unsigned char* blub=(unsigned char*)emalloc(0x2123);
|
||||
unsigned char* blub=(unsigned char*)emalloc(0x2123, "cbMemWrite:blub");
|
||||
memread(fdProcessInfo->hProcess, (const void*)addr, blub, 0x2123, 0);
|
||||
//memwrite(fdProcessInfo->hProcess, (void*)addr, blub, 0x2123, 0);
|
||||
return STATUS_CONTINUE;
|
||||
|
|
|
@ -56,13 +56,13 @@ mathformat:
|
|||
void mathformat(char* text)
|
||||
{
|
||||
int len=strlen(text);
|
||||
char* temp=(char*)emalloc(len+1);
|
||||
char* temp=(char*)emalloc(len+1, "mathformat:temp");
|
||||
memset(temp, 0, len+1);
|
||||
for(int i=0,j=0; i<len; i++)
|
||||
if(mathisoperator(text[i])<3 or text[i]!=text[i+1])
|
||||
j+=sprintf(temp+j, "%c", text[i]);
|
||||
strcpy(text, temp);
|
||||
efree(temp);
|
||||
efree(temp, "mathformat:temp");
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -336,7 +336,7 @@ bool mathhandlebrackets(char* expression)
|
|||
return true;
|
||||
expstruct.total_pairs=total_pairs;
|
||||
|
||||
expstruct.pairs=(BRACKET_PAIR*)emalloc(expstruct.total_pairs*sizeof(BRACKET_PAIR));
|
||||
expstruct.pairs=(BRACKET_PAIR*)emalloc(expstruct.total_pairs*sizeof(BRACKET_PAIR), "mathhandlebrackets:expstruct.pairs");
|
||||
memset(expstruct.pairs, 0, expstruct.total_pairs*sizeof(BRACKET_PAIR));
|
||||
matchpairs(&expstruct, expression, 0);
|
||||
int deepest=0;
|
||||
|
@ -348,7 +348,7 @@ bool mathhandlebrackets(char* expression)
|
|||
if(!printlayer(expression, &expstruct, i))
|
||||
return false;
|
||||
|
||||
efree(expstruct.pairs);
|
||||
efree(expstruct.pairs, "mathhandlebrackets:expstruct.pairs");
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -375,8 +375,8 @@ bool mathfromstring(const char* string, uint* value, int* value_size, bool* isva
|
|||
return false;
|
||||
return true;
|
||||
}
|
||||
char* strleft=(char*)emalloc(len+1);
|
||||
char* strright=(char*)emalloc(len+1);
|
||||
char* strleft=(char*)emalloc(len+1, "mathfromstring:strleft");
|
||||
char* strright=(char*)emalloc(len+1, "mathfromstring:strright");
|
||||
memset(strleft, 0, len+1);
|
||||
memset(strright, 0, len+1);
|
||||
strncpy(strleft, string, highestop_pos);
|
||||
|
@ -384,15 +384,15 @@ bool mathfromstring(const char* string, uint* value, int* value_size, bool* isva
|
|||
//dprintf("left: %s, right: %s, op: %c\n", strleft, strright, string[highestop_pos]);
|
||||
if(!*strright)
|
||||
{
|
||||
efree(strleft);
|
||||
efree(strright);
|
||||
efree(strleft, "mathfromstring:strleft");
|
||||
efree(strright, "mathfromstring:strright");
|
||||
return false;
|
||||
}
|
||||
uint right=0;
|
||||
if(!valfromstring(strright, &right, 0, 0, false, 0))
|
||||
{
|
||||
efree(strleft);
|
||||
efree(strright);
|
||||
efree(strleft, "mathfromstring:strleft");
|
||||
efree(strright, "mathfromstring:strright");
|
||||
return false;
|
||||
}
|
||||
if(string[highestop_pos]=='~')
|
||||
|
@ -401,16 +401,16 @@ bool mathfromstring(const char* string, uint* value, int* value_size, bool* isva
|
|||
if(!strlen(strleft))
|
||||
{
|
||||
*value=right;
|
||||
efree(strleft);
|
||||
efree(strright);
|
||||
efree(strleft, "mathfromstring:strleft");
|
||||
efree(strright, "mathfromstring:strright");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
uint left=0;
|
||||
if(!valfromstring(strleft, &left, 0, 0, false, 0))
|
||||
{
|
||||
efree(strleft);
|
||||
efree(strright);
|
||||
efree(strleft, "mathfromstring:strleft");
|
||||
efree(strright, "mathfromstring:strright");
|
||||
return false;
|
||||
}
|
||||
bool math_ok;
|
||||
|
@ -418,8 +418,8 @@ bool mathfromstring(const char* string, uint* value, int* value_size, bool* isva
|
|||
math_ok=mathdosignedoperation(string[highestop_pos], left, right, (sint*)value);
|
||||
else
|
||||
math_ok=mathdounsignedoperation(string[highestop_pos], left, right, value);
|
||||
efree(strleft);
|
||||
efree(strright);
|
||||
efree(strleft, "mathfromstring:strleft");
|
||||
efree(strright, "mathfromstring:strright");
|
||||
return math_ok;
|
||||
}
|
||||
|
||||
|
|
|
@ -26,52 +26,38 @@ uint memfindbaseaddr(HANDLE hProcess, uint addr, uint* size)
|
|||
|
||||
bool memread(HANDLE hProcess, const void* lpBaseAddress, void* lpBuffer, SIZE_T nSize, SIZE_T* lpNumberOfBytesRead)
|
||||
{
|
||||
//TODO: there are bugs in this function
|
||||
if(!hProcess or !lpBaseAddress or !lpBuffer or !nSize)
|
||||
if(!hProcess or !lpBaseAddress or !lpBuffer or !nSize) //generic failures
|
||||
return false;
|
||||
|
||||
SIZE_T read1=0;
|
||||
SIZE_T read=0;
|
||||
DWORD oldprotect=0;
|
||||
VirtualProtectEx(hProcess, (void*)lpBaseAddress, nSize, PAGE_EXECUTE_READWRITE, &oldprotect);
|
||||
bool test=ReadProcessMemory(hProcess, (void*)lpBaseAddress, lpBuffer, nSize, &read1);
|
||||
VirtualProtectEx(hProcess, (void*)lpBaseAddress, nSize, oldprotect, &oldprotect);
|
||||
if(test and read1==nSize)
|
||||
bool ret=ReadProcessMemory(hProcess, (void*)lpBaseAddress, lpBuffer, nSize, &read); //try 'normal' RPM
|
||||
if(!ret or read!=nSize) //failed
|
||||
{
|
||||
VirtualProtectEx(hProcess, (void*)lpBaseAddress, nSize, PAGE_EXECUTE_READWRITE, &oldprotect); //change page protection
|
||||
ret=ReadProcessMemory(hProcess, (void*)lpBaseAddress, lpBuffer, nSize, &read); //try 'normal' RPM again
|
||||
VirtualProtectEx(hProcess, (void*)lpBaseAddress, nSize, oldprotect, &oldprotect); //restore page protection
|
||||
}
|
||||
if(ret and read==nSize) //'normal' RPM worked!
|
||||
{
|
||||
if(lpNumberOfBytesRead)
|
||||
*lpNumberOfBytesRead=read1;
|
||||
*lpNumberOfBytesRead=read;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
uint addr=(uint)lpBaseAddress;
|
||||
uint startRva=addr&(PAGE_SIZE-1); //get start rva
|
||||
uint addrStart=addr-startRva; //round down one page
|
||||
uint pages=nSize/PAGE_SIZE+1;
|
||||
SIZE_T sizeRead=0;
|
||||
unsigned char curPage[PAGE_SIZE]; //current page memory
|
||||
unsigned char* destBuffer=(unsigned char*)lpBuffer;
|
||||
|
||||
for(uint i=0; i<pages; i++)
|
||||
for(uint i=0; i<nSize; i++) //read byte-per-byte
|
||||
{
|
||||
SIZE_T readBytes=0;
|
||||
void* curAddr=(void*)(addrStart+i*PAGE_SIZE);
|
||||
bool ret=ReadProcessMemory(hProcess, curAddr, curPage, PAGE_SIZE, &readBytes);
|
||||
if(!ret or readBytes!=PAGE_SIZE)
|
||||
unsigned char* curaddr=(unsigned char*)lpBaseAddress+i;
|
||||
unsigned char* curbuf=(unsigned char*)lpBuffer+i;
|
||||
ret=ReadProcessMemory(hProcess, curaddr, curbuf, 1, 0); //try 'normal' RPM
|
||||
if(!ret) //we failed
|
||||
{
|
||||
VirtualProtectEx(hProcess, curAddr, PAGE_SIZE, PAGE_EXECUTE_READWRITE, &oldprotect);
|
||||
ret=ReadProcessMemory(hProcess, curAddr, curPage, PAGE_SIZE, &readBytes);
|
||||
VirtualProtectEx(hProcess, curAddr, PAGE_SIZE, oldprotect, &oldprotect);
|
||||
if(!ret or readBytes!=PAGE_SIZE)
|
||||
VirtualProtectEx(hProcess, curaddr, 1, PAGE_EXECUTE_READWRITE, &oldprotect); //change page protection
|
||||
ret=ReadProcessMemory(hProcess, curaddr, curbuf, PAGE_SIZE, 0); //try 'normal' RPM again
|
||||
VirtualProtectEx(hProcess, curaddr, 1, oldprotect, &oldprotect); //restore page protection
|
||||
if(!ret) //complete failure
|
||||
return false;
|
||||
}
|
||||
if(sizeRead+PAGE_SIZE>nSize) //do not overflow the buffer
|
||||
memcpy(destBuffer, curPage+startRva, nSize-sizeRead);
|
||||
else //default case
|
||||
memcpy(destBuffer, curPage+startRva, PAGE_SIZE-startRva);
|
||||
sizeRead+=(PAGE_SIZE-startRva);
|
||||
destBuffer+=(PAGE_SIZE-startRva);
|
||||
if(!i)
|
||||
startRva=0;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -4,19 +4,19 @@
|
|||
//allocate a message (internal)
|
||||
static MESSAGE* msgalloc()
|
||||
{
|
||||
return (MESSAGE*)emalloc(sizeof(MESSAGE));
|
||||
return (MESSAGE*)emalloc(sizeof(MESSAGE), "msgalloc:msg");
|
||||
}
|
||||
|
||||
//free a message (internal)
|
||||
static void msgfree(MESSAGE* msg)
|
||||
{
|
||||
efree(msg);
|
||||
efree(msg, "msgfree:msg");
|
||||
}
|
||||
|
||||
//allocate a message stack
|
||||
MESSAGE_STACK* msgallocstack()
|
||||
{
|
||||
MESSAGE_STACK* msgstack=(MESSAGE_STACK*)emalloc(sizeof(MESSAGE_STACK));
|
||||
MESSAGE_STACK* msgstack=(MESSAGE_STACK*)emalloc(sizeof(MESSAGE_STACK), "msgallocstack:msgstack");
|
||||
if(!msgstack)
|
||||
return 0;
|
||||
memset(msgstack, 0, sizeof(MESSAGE_STACK));
|
||||
|
@ -31,7 +31,7 @@ void msgfreestack(MESSAGE_STACK* msgstack)
|
|||
int stackpos=msgstack->stackpos;
|
||||
for(int i=0; i<stackpos; i++) //free all messages left in stack
|
||||
msgfree(msgstack->msg[i]);
|
||||
efree(msgstack);
|
||||
efree(msgstack, "msgfreestack:msgstack");
|
||||
}
|
||||
|
||||
//add a message to the stack
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
#include "_global.h"
|
||||
#include <windows.h>
|
||||
|
||||
#define MAX_MESSAGES 4096
|
||||
#define MAX_MESSAGES 256
|
||||
|
||||
//message structure
|
||||
struct MESSAGE
|
||||
|
|
|
@ -6,8 +6,9 @@
|
|||
//enums
|
||||
enum WAIT_ID
|
||||
{
|
||||
WAITID_SYSBREAK=0,
|
||||
WAITID_RUN=1
|
||||
WAITID_SYSBREAK,
|
||||
WAITID_RUN,
|
||||
WAITID_STOP
|
||||
};
|
||||
|
||||
//functions
|
||||
|
|
|
@ -1103,18 +1103,18 @@ bool valfromstring(const char* string, uint* value, int* value_size, bool* isvar
|
|||
else if(mathcontains(string)) //handle math
|
||||
{
|
||||
int len=strlen(string);
|
||||
char* string_=(char*)emalloc(len+256);
|
||||
char* string_=(char*)emalloc(len+256, "valfromstring:string_");
|
||||
strcpy(string_, string);
|
||||
int add=0;
|
||||
while(mathisoperator(string_[add])>2)
|
||||
add++;
|
||||
if(!mathhandlebrackets(string_+add))
|
||||
{
|
||||
efree(string_);
|
||||
efree(string_, "valfromstring:string_");
|
||||
return false;
|
||||
}
|
||||
bool ret=mathfromstring(string_+add, value, value_size, isvar);
|
||||
efree(string_);
|
||||
efree(string_, "valfromstring:string_");
|
||||
return ret;
|
||||
}
|
||||
else if(*string=='@') //memory location
|
||||
|
|
|
@ -24,7 +24,7 @@ static VAR* varfind(const char* name, VAR** link)
|
|||
|
||||
void varinit()
|
||||
{
|
||||
vars=(VAR*)emalloc(sizeof(VAR));
|
||||
vars=(VAR*)emalloc(sizeof(VAR), "varinit:vars");
|
||||
memset(vars, 0, sizeof(VAR));
|
||||
//General variables
|
||||
varnew("$res\1$result", 0, VAR_SYSTEM);
|
||||
|
@ -41,6 +41,18 @@ void varinit()
|
|||
varnew("$lastalloc", 0, VAR_READONLY);
|
||||
}
|
||||
|
||||
void varfree()
|
||||
{
|
||||
VAR* cur=vars;
|
||||
while(cur)
|
||||
{
|
||||
efree(cur->name, "varfree:cur->name");
|
||||
VAR* next=cur->next;
|
||||
efree(cur, "varfree:cur");
|
||||
cur=next;
|
||||
}
|
||||
}
|
||||
|
||||
VAR* vargetptr()
|
||||
{
|
||||
return vars;
|
||||
|
@ -50,7 +62,7 @@ bool varnew(const char* name_, uint value, VAR_TYPE type)
|
|||
{
|
||||
if(!name_)
|
||||
return false;
|
||||
char* name=(char*)emalloc(strlen(name_)+2);
|
||||
char* name=(char*)emalloc(strlen(name_)+2, "varnew:name");
|
||||
if(*name_!='$')
|
||||
{
|
||||
*name='$';
|
||||
|
@ -60,12 +72,12 @@ bool varnew(const char* name_, uint value, VAR_TYPE type)
|
|||
strcpy(name, name_);
|
||||
if(!name[1])
|
||||
{
|
||||
efree(name);
|
||||
efree(name, "varnew:name");
|
||||
return false;
|
||||
}
|
||||
if(varfind(name, 0))
|
||||
{
|
||||
efree(name);
|
||||
efree(name, "varnew:name");
|
||||
return false;
|
||||
}
|
||||
VAR* var;
|
||||
|
@ -76,7 +88,7 @@ bool varnew(const char* name_, uint value, VAR_TYPE type)
|
|||
var=vars;
|
||||
}
|
||||
else
|
||||
var=(VAR*)emalloc(sizeof(VAR));
|
||||
var=(VAR*)emalloc(sizeof(VAR), "varnew:var");
|
||||
memset(var, 0, sizeof(VAR));
|
||||
var->name=name;
|
||||
var->type=type;
|
||||
|
@ -127,7 +139,7 @@ bool varset(const char* name, uint value, bool setreadonly)
|
|||
|
||||
bool vardel(const char* name_, bool delsystem)
|
||||
{
|
||||
char* name=(char*)emalloc(strlen(name_)+2);
|
||||
char* name=(char*)emalloc(strlen(name_)+2, "vardel:name");
|
||||
if(*name_!='$')
|
||||
{
|
||||
*name='$';
|
||||
|
@ -137,7 +149,7 @@ bool vardel(const char* name_, bool delsystem)
|
|||
strcpy(name, name_);
|
||||
VAR* prev=0;
|
||||
VAR* found=varfind(name, &prev);
|
||||
efree(name);
|
||||
efree(name, "vardel:name");
|
||||
if(!found)
|
||||
return false;
|
||||
VAR_TYPE type=found->type;
|
||||
|
@ -145,7 +157,7 @@ bool vardel(const char* name_, bool delsystem)
|
|||
return false;
|
||||
if(type==VAR_HIDDEN)
|
||||
return false;
|
||||
efree(found->name);
|
||||
efree(found->name, "vardel:found->name");
|
||||
if(found==vars)
|
||||
{
|
||||
VAR* next=vars->next;
|
||||
|
@ -153,7 +165,7 @@ bool vardel(const char* name_, bool delsystem)
|
|||
{
|
||||
memcpy(vars, vars->next, sizeof(VAR));
|
||||
vars->next=next->next;
|
||||
efree(next);
|
||||
efree(next, "vardel:next");
|
||||
}
|
||||
else
|
||||
memset(vars, 0, sizeof(VAR));
|
||||
|
@ -161,7 +173,7 @@ bool vardel(const char* name_, bool delsystem)
|
|||
else
|
||||
{
|
||||
prev->next=found->next;
|
||||
efree(found);
|
||||
efree(found, "vardel:found");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -37,6 +37,7 @@ struct VAR
|
|||
|
||||
//functions
|
||||
void varinit();
|
||||
void varfree();
|
||||
VAR* vargetptr();
|
||||
bool varnew(const char* name, uint value, VAR_TYPE type);
|
||||
bool varget(const char* name, uint* value, int* size, VAR_TYPE* type);
|
||||
|
|
|
@ -11,8 +11,10 @@
|
|||
#include "x64_dbg.h"
|
||||
#include "msgqueue.h"
|
||||
#include "addrinfo.h"
|
||||
#include "threading.h"
|
||||
|
||||
static MESSAGE_STACK* gMsgStack;
|
||||
static MESSAGE_STACK* gMsgStack=0;
|
||||
static COMMAND* command_list=0;
|
||||
|
||||
static CMDRESULT cbStrLen(const char* cmd)
|
||||
{
|
||||
|
@ -33,8 +35,6 @@ static CMDRESULT cbCls(const char* cmd)
|
|||
return STATUS_CONTINUE;
|
||||
}
|
||||
|
||||
static COMMAND* command_list=0;
|
||||
|
||||
static void registercommands()
|
||||
{
|
||||
COMMAND* cmd=command_list=cmdinit();
|
||||
|
@ -88,14 +88,14 @@ static bool cbCommandProvider(char* cmd, int maxlen)
|
|||
if(strlen(newcmd)>=deflen)
|
||||
newcmd[deflen-1]=0;
|
||||
strcpy(cmd, newcmd);
|
||||
efree(newcmd); //free allocated command
|
||||
efree(newcmd, "cbCommandProvider:newcmd"); //free allocated command
|
||||
return true;
|
||||
}
|
||||
|
||||
extern "C" DLL_EXPORT bool _dbg_dbgcmdexec(const char* cmd)
|
||||
{
|
||||
int len=strlen(cmd);
|
||||
char* newcmd=(char*)emalloc((len+1)*sizeof(char));
|
||||
char* newcmd=(char*)emalloc((len+1)*sizeof(char), "_dbg_dbgcmdexec:newcmd");
|
||||
strcpy(newcmd, cmd);
|
||||
return msgsend(gMsgStack, 0, (uint)newcmd, 0);
|
||||
}
|
||||
|
@ -141,3 +141,14 @@ extern "C" DLL_EXPORT const char* _dbg_dbginit()
|
|||
//CreateThread(0, 0, ConsoleReadLoopThread, 0, 0, 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
extern "C" DLL_EXPORT void _dbg_dbgexitsignal()
|
||||
{
|
||||
//TODO: handle exit signal
|
||||
cbStopDebug("");
|
||||
wait(WAITID_STOP); //after this, debugging stopped
|
||||
DeleteFileA("DLLLoader.exe");
|
||||
cmdfree(command_list);
|
||||
varfree();
|
||||
msgfreestack(gMsgStack);
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@ extern "C"
|
|||
|
||||
DLL_EXPORT const char* _dbg_dbginit();
|
||||
DLL_EXPORT bool _dbg_dbgcmdexec(const char* cmd);
|
||||
DLL_EXPORT void _dbg_dbgexitsignal();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@ Disassembly::Disassembly(MemoryPage* parMemPage, QWidget *parent) : AbstractTabl
|
|||
|
||||
setRowCount(parMemPage->getSize());
|
||||
|
||||
qDebug() << "size" << parMemPage->getSize();
|
||||
//qDebug() << "size" << parMemPage->getSize();
|
||||
|
||||
int charwidth=QFontMetrics(this->font()).width(QChar(' '));
|
||||
|
||||
|
|
Loading…
Reference in New Issue