DBG: removed internal SQLite DB

DBG: now 100% supports debug symbols + gets API names etc
DBG: improved performance of _dbg_addrinfoget
This commit is contained in:
mr.exodia 2013-11-17 16:39:47 +01:00
parent da3fdf53a3
commit e1a6b26c8e
4 changed files with 47 additions and 152 deletions

View File

@ -103,36 +103,34 @@ extern "C" DLL_EXPORT bool _dbg_addrinfoget(duint addr, SEGMENTREG segment, ADDR
{ {
if(labelget(addr, addrinfo->label)) if(labelget(addr, addrinfo->label))
retval=true; retval=true;
else if(!retval) //no user labels
{ {
//TODO: auto-labels //TODO: auto-labels
/*const char* apiname=(const char*)ImporterGetAPINameFromDebugee(fdProcessInfo->hProcess, addr); DWORD64 displacement=0;
if(apiname) char buffer[sizeof(SYMBOL_INFO) + MAX_LABEL_SIZE * sizeof(char)];
PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)buffer;
pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO);
pSymbol->MaxNameLen = MAX_LABEL_SIZE;
if(SymFromAddr(fdProcessInfo->hProcess, (DWORD64)addr, &displacement, pSymbol) and !displacement)
{ {
strcpy(addrinfo->label, apiname); strcpy(addrinfo->label, pSymbol->Name);
retval=true; retval=true;
} }
uint addr_dw=0;
if(memread(fdProcessInfo->hProcess, (const void*)addr, &addr_dw, sizeof(uint), 0))
{
const char* apiname=(const char*)ImporterGetAPINameFromDebugee(fdProcessInfo->hProcess, addr_dw);
if(apiname)
{
strcpy(addrinfo->label, apiname);
retval=true;
}
}*/
if(!retval) if(!retval)
{ {
DWORD64 displacement=0; uint addr_=0;
char buffer[sizeof(SYMBOL_INFO) + MAX_LABEL_SIZE * sizeof(char)]; if(memread(fdProcessInfo->hProcess, (const void*)addr, &addr_, sizeof(uint), 0))
PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)buffer;
pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO);
pSymbol->MaxNameLen = MAX_LABEL_SIZE;
if(SymFromAddr(fdProcessInfo->hProcess, (DWORD64)addr, &displacement, pSymbol) and !displacement)
{ {
strcpy(addrinfo->label, pSymbol->Name); DWORD64 displacement=0;
retval=true; char buffer[sizeof(SYMBOL_INFO) + MAX_LABEL_SIZE * sizeof(char)];
PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)buffer;
pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO);
pSymbol->MaxNameLen = MAX_LABEL_SIZE;
if(SymFromAddr(fdProcessInfo->hProcess, (DWORD64)addr_, &displacement, pSymbol) and !displacement)
{
strcpy(addrinfo->label, pSymbol->Name);
retval=true;
}
} }
} }
} }
@ -142,18 +140,22 @@ extern "C" DLL_EXPORT bool _dbg_addrinfoget(duint addr, SEGMENTREG segment, ADDR
if(commentget(addr, addrinfo->comment)) if(commentget(addr, addrinfo->comment))
retval=true; retval=true;
//TODO: auto-comments //TODO: auto-comments
else if(!retval)
{ {
if(!retval) DWORD dwDisplacement;
IMAGEHLP_LINE64 line;
line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
if(SymGetLineFromAddr64(fdProcessInfo->hProcess, (DWORD64)addr, &dwDisplacement, &line) and !dwDisplacement)
{ {
DWORD dwDisplacement; char filename[deflen]="";
IMAGEHLP_LINE64 line; strcpy(filename, line.FileName);
line.SizeOfStruct = sizeof(IMAGEHLP_LINE64); int len=strlen(filename);
if(SymGetLineFromAddr64(fdProcessInfo->hProcess, (DWORD64)addr, &dwDisplacement, &line) and !dwDisplacement) while(filename[len]!='\\' and len!=0)
{ len--;
sprintf(addrinfo->comment, "line: %u", line.LineNumber); if(len)
retval=true; len++;
} sprintf(addrinfo->comment, "%s:%u", filename+len, line.LineNumber);
retval=true;
} }
} }
} }

View File

@ -4,7 +4,6 @@
#include "memory.h" #include "memory.h"
static sqlite3* db; static sqlite3* db;
static sqlite3* internaldb;
///basic database functions ///basic database functions
void dbinit() void dbinit()
@ -30,18 +29,6 @@ void dbinit()
dprintf("SQL Error: %s\n", errorText); dprintf("SQL Error: %s\n", errorText);
sqlite3_free(errorText); sqlite3_free(errorText);
} }
//initialize internal database
if(sqlite3_open(":memory:", &internaldb))
{
dputs("failed to open database!");
return;
}
strcpy(sql, "CREATE TABLE IF NOT EXISTS exports (id INTEGER PRIMARY KEY AUTOINCREMENT, base INT64 NOT NULL, mod TEXT, name TEXT NOT NULL, addr INT64 NOT NULL)");
if(sqlite3_exec(internaldb, sql, 0, 0, &errorText)!=SQLITE_OK) //error
{
dprintf("SQL Error: %s\n", errorText);
sqlite3_free(errorText);
}
} }
static int loadOrSaveDb(sqlite3* memory, const char* file, bool save) static int loadOrSaveDb(sqlite3* memory, const char* file, bool save)
@ -78,9 +65,6 @@ bool dbload()
bool dbsave() bool dbsave()
{ {
DeleteFileA("internal.db");
loadOrSaveDb(internaldb, "internal.db", true);
CreateDirectoryA(sqlitedb_basedir, 0); //create database directory CreateDirectoryA(sqlitedb_basedir, 0); //create database directory
if(loadOrSaveDb(db, dbpath, true)!=SQLITE_OK) if(loadOrSaveDb(db, dbpath, true)!=SQLITE_OK)
return false; return false;
@ -92,86 +76,29 @@ void dbclose()
dbsave(); dbsave();
sqlite3_db_release_memory(db); sqlite3_db_release_memory(db);
sqlite3_close(db); //close program database sqlite3_close(db); //close program database
sqlite3_db_release_memory(internaldb);
sqlite3_close(internaldb); //close internal database
} }
///module functions ///module functions
static std::vector<MODINFO> modinfo;
bool modnamefromaddr(uint addr, char* modname) bool modnamefromaddr(uint addr, char* modname)
{ {
int total=modinfo.size(); IMAGEHLP_MODULE64 modInfo;
for(int i=0; i<total; i++) memset(&modInfo, 0, sizeof(modInfo));
{ modInfo.SizeOfStruct=sizeof(IMAGEHLP_MODULE64);
if(addr>=modinfo.at(i).start and addr<modinfo.at(i).end) if(!SymGetModuleInfo64(fdProcessInfo->hProcess, (DWORD64)addr, &modInfo))
{ return false;
strcpy(modname, modinfo.at(i).name); _strlwr(modInfo.ModuleName);
return true; strcpy(modname, modInfo.ModuleName);
} return true;
}
return false;
} }
uint modbasefromaddr(uint addr) uint modbasefromaddr(uint addr)
{ {
int total=modinfo.size(); IMAGEHLP_MODULE64 modInfo;
for(int i=0; i<total; i++) memset(&modInfo, 0, sizeof(modInfo));
{ modInfo.SizeOfStruct=sizeof(IMAGEHLP_MODULE64);
if(addr>=modinfo.at(i).start and addr<modinfo.at(i).end) if(!SymGetModuleInfo64(fdProcessInfo->hProcess, (DWORD64)addr, &modInfo))
{
return modinfo.at(i).start;
}
}
return 0;
}
static void cbExport(uint base, const char* mod, const char* name, uint addr)
{
char sql[deflen]="";
sprintf(sql, "INSERT INTO exports (base,mod,name,addr) VALUES (%"fext"d,'%s','%s',%"fext"d)", base, mod, name, addr);
char* errorText=0;
if(sqlite3_exec(internaldb, sql, 0, 0, &errorText)!=SQLITE_OK) //error
{
dprintf("SQL Error: %s\n", errorText);
sqlite3_free(errorText);
}
}
bool modload(uint base, uint size, const char* name)
{
if(!base or !size or !name or strlen(name)>=31)
return false; return false;
MODINFO info; return modInfo.BaseOfImage;
info.start=base;
info.end=base+size;
strcpy(info.name, name);
_strlwr(info.name);
modinfo.push_back(info);
apienumexports(base, cbExport);
return true;
}
bool modunload(uint base)
{
if(!base)
return false;
int total=modinfo.size();
for(int i=0; i<total; i++)
{
if(modinfo.at(i).start==base)
{
modinfo.erase(modinfo.begin()+i);
return true;
}
}
return false;
}
void modclear()
{
modinfo.clear();
} }
///api functions ///api functions

View File

@ -20,9 +20,6 @@ bool dbload();
void dbclose(); void dbclose();
bool modnamefromaddr(uint addr, char* modname); bool modnamefromaddr(uint addr, char* modname);
uint modbasefromaddr(uint addr); 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 apienumexports(uint base, EXPORTENUMCALLBACK cbEnum);
bool commentset(uint addr, const char* text); bool commentset(uint addr, const char* text);
bool commentget(uint addr, char* text); bool commentget(uint addr, char* text);

View File

@ -265,11 +265,6 @@ static void cbLoadDll(LOAD_DLL_DEBUG_INFO* LoadDll)
DevicePathToPath(DLLDebugFileName, DLLDebugFileName, deflen); DevicePathToPath(DLLDebugFileName, DLLDebugFileName, deflen);
dprintf("DLL Loaded: "fhex" %s\n", base, DLLDebugFileName); dprintf("DLL Loaded: "fhex" %s\n", base, DLLDebugFileName);
SymLoadModuleEx(fdProcessInfo->hProcess, LoadDll->hFile, DLLDebugFileName, 0, (DWORD64)base, 0, 0, 0); SymLoadModuleEx(fdProcessInfo->hProcess, LoadDll->hFile, DLLDebugFileName, 0, (DWORD64)base, 0, 0, 0);
IMAGEHLP_MODULE64 modInfo;
memset(&modInfo, 0, sizeof(modInfo));
modInfo.SizeOfStruct=sizeof(IMAGEHLP_MODULE64);
if(SymGetModuleInfo64(fdProcessInfo->hProcess, (DWORD64)base, &modInfo))
modload(modInfo.BaseOfImage, modInfo.ImageSize, modInfo.ModuleName);
} }
static void cbUnloadDll(UNLOAD_DLL_DEBUG_INFO* UnloadDll) static void cbUnloadDll(UNLOAD_DLL_DEBUG_INFO* UnloadDll)
@ -282,7 +277,6 @@ static void cbUnloadDll(UNLOAD_DLL_DEBUG_INFO* UnloadDll)
DevicePathToPath(DLLDebugFileName, DLLDebugFileName, deflen); DevicePathToPath(DLLDebugFileName, DLLDebugFileName, deflen);
dprintf("DLL Unloaded: "fhex" %s\n", base, DLLDebugFileName); dprintf("DLL Unloaded: "fhex" %s\n", base, DLLDebugFileName);
SymUnloadModule64(fdProcessInfo->hProcess, (DWORD64)base); SymUnloadModule64(fdProcessInfo->hProcess, (DWORD64)base);
modunload((uint)base);
} }
static void cbCreateProcess(CREATE_PROCESS_DEBUG_INFO* CreateProcessInfo) static void cbCreateProcess(CREATE_PROCESS_DEBUG_INFO* CreateProcessInfo)
@ -315,12 +309,6 @@ static void cbCreateProcess(CREATE_PROCESS_DEBUG_INFO* CreateProcessInfo)
SymInitialize(fdProcessInfo->hProcess, 0, false); //initialize symbols SymInitialize(fdProcessInfo->hProcess, 0, false); //initialize symbols
//SymRegisterCallback64(fdProcessInfo->hProcess, SymRegisterCallbackProc64, 0); //SymRegisterCallback64(fdProcessInfo->hProcess, SymRegisterCallbackProc64, 0);
SymLoadModuleEx(fdProcessInfo->hProcess, CreateProcessInfo->hFile, DebugFileName, 0, (DWORD64)base, 0, 0, 0); SymLoadModuleEx(fdProcessInfo->hProcess, CreateProcessInfo->hFile, DebugFileName, 0, (DWORD64)base, 0, 0, 0);
IMAGEHLP_MODULE64 modInfo;
memset(&modInfo, 0, sizeof(modInfo));
modInfo.SizeOfStruct=sizeof(IMAGEHLP_MODULE64);
if(SymGetModuleInfo64(fdProcessInfo->hProcess, (DWORD64)base, &modInfo))
modload(modInfo.BaseOfImage, modInfo.ImageSize, modInfo.ModuleName);
} }
static void cbSystemBreakpoint(void* ExceptionData) static void cbSystemBreakpoint(void* ExceptionData)
@ -337,25 +325,6 @@ static void cbSystemBreakpoint(void* ExceptionData)
//lock //lock
lock(WAITID_RUN); lock(WAITID_RUN);
wait(WAITID_RUN); wait(WAITID_RUN);
/*//my code
//list memorymap(cbListPage)
ReadMemory(va)
setBP(va, type, callback)
//gui
GuiChangeCIP(va, base, size)
//gui
cbClearMap
cbAddPage(MEMORY_BASIC_INFO, modulename)
cbEndMap
//dbg
MemoryMap(cbClear, cbAddPage, cbEndMap)
*/
} }
static void cbStep() static void cbStep()