1
0
Fork 0

DbgFunctions()->GetJit export

This commit is contained in:
Mr. eXoDia 2014-08-04 22:23:49 +02:00
parent 0e7c61b24f
commit e14884443f
8 changed files with 151 additions and 137 deletions

View File

@ -107,6 +107,24 @@ static void _getcallstack(DBGCALLSTACK* callstack)
stackgetcallstack(GetContextDataEx(hActiveThread, UE_CSP), (CALLSTACK*)callstack);
}
static bool _getjit(char* jit, bool jit64)
{
char* currentJit = 0;
arch dummy;
if(!dbggetjit(&currentJit, jit64 ? x64 : x32, &dummy))
return false;
//get out the actual jit path
char* copyJit=currentJit;
if(*copyJit=='\"')
copyJit++;
char* last=strchr(copyJit, '\"');
if(last)
*last=0;
strcpy_s(jit, MAX_SETTING_SIZE, copyJit);
efree(currentJit, "dbggetjit:*jit_entry_out");
return true;
}
void dbgfunctionsinit()
{
_dbgfunctions.AssembleAtEx=_assembleatex;
@ -129,4 +147,5 @@ void dbgfunctionsinit()
_dbgfunctions.MemUpdateMap=memupdatemap;
_dbgfunctions.GetCallStack=_getcallstack;
_dbgfunctions.SymbolDownloadAllSymbols=symdownloadallsymbols;
_dbgfunctions.GetJit=_getjit;
}

View File

@ -47,6 +47,7 @@ typedef bool (*DISASMFAST)(unsigned char* data, duint addr, BASIC_INSTRUCTION_IN
typedef void (*MEMUPDATEMAP)(HANDLE hProcess);
typedef void (*GETCALLSTACK)(DBGCALLSTACK* callstack);
typedef void (*SYMBOLDOWNLOADALLSYMBOLS)(const char* szSymbolStore);
typedef bool (*GETJIT)(char* jit, bool x64);
typedef struct DBGFUNCTIONS_
{
@ -70,6 +71,7 @@ typedef struct DBGFUNCTIONS_
MEMUPDATEMAP MemUpdateMap;
GETCALLSTACK GetCallStack;
SYMBOLDOWNLOADALLSYMBOLS SymbolDownloadAllSymbols;
GETJIT GetJit;
} DBGFUNCTIONS;
#ifdef BUILD_DBG

View File

@ -175,4 +175,12 @@ arch GetFileArchitecture(const char* szFileName)
CloseHandle(hFile);
}
return retval;
}
bool IsWow64()
{
BOOL bIsWow64Process = FALSE;
//x64_dbg supports WinXP SP3 and later only, so ignore the GetProcAddress crap :D
IsWow64Process(GetCurrentProcess(), &bIsWow64Process);
return !!bIsWow64Process;
}

View File

@ -119,5 +119,6 @@ bool DirExists(const char* dir);
bool GetFileNameFromHandle(HANDLE hFile, char* szFileName);
bool settingboolget(const char* section, const char* name);
arch GetFileArchitecture(const char* szFileName);
bool IsWow64();
#endif // _GLOBAL_H

View File

@ -1474,4 +1474,110 @@ void cbDetach()
else
dputs("detached!");
return;
}
#define JIT_REG_KEY TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug")
bool dbggetjit(char** jit_entry_out, arch arch_in, arch* arch_out)
{
DWORD key_flags = KEY_READ;
DWORD lRv;
HKEY hKey;
if (arch_out != NULL )
{
if ( arch_in != x64 && arch_in != x32 )
{
#ifdef _WIN32
* arch_out = x32;
#endif
#ifdef _WIN64
* arch_out = x64;
#endif
}
else
* arch_out = arch_in;
}
if ( arch_in == x64 )
{
if (!IsWow64())
return false;
#ifdef _WIN32
key_flags |= KEY_WOW64_64KEY;
#endif
}
else if ( arch_in == x32 )
{
#ifdef _WIN64
key_flags |= KEY_WOW64_32KEY;
#endif
}
lRv = RegOpenKeyEx(HKEY_LOCAL_MACHINE, JIT_REG_KEY, 0, key_flags,&hKey);
if (lRv != ERROR_SUCCESS)
return false;
char jit_entry[512];
DWORD jit_entry_size = sizeof( jit_entry );
lRv = RegQueryValueExA(hKey, "Debugger", 0, NULL, (LPBYTE)jit_entry, & jit_entry_size);
if (lRv != ERROR_SUCCESS)
return false;
* jit_entry_out = (char *) emalloc( jit_entry_size, "dbggetjit:*jit_entry_out");
//internal allocation functions cannot fail (application will exit with an error)
//if ( * jit_entry_out == NULL )
//return false;
strcpy( * jit_entry_out, jit_entry );
return true;
}
bool dbgsetjit(char* jit_cmd, arch arch_in, arch* arch_out)
{
DWORD key_flags = KEY_WRITE;
DWORD lRv;
HKEY hKey;
DWORD dwDisposition;
if (arch_out != NULL )
{
if ( arch_in != x64 && arch_in != x32 )
{
#ifdef _WIN32
* arch_out = x32;
#endif
#ifdef _WIN64
* arch_out = x64;
#endif
}
else
* arch_out = arch_in;
}
if ( arch_in == x64 )
{
if (!IsWow64())
return false;
#ifdef _WIN32
key_flags |= KEY_WOW64_64KEY;
#endif
}
else if ( arch_in == x32 )
{
#ifdef _WIN64
key_flags |= KEY_WOW64_32KEY;
#endif
}
lRv = RegCreateKeyEx(HKEY_LOCAL_MACHINE, JIT_REG_KEY, 0, NULL, REG_OPTION_NON_VOLATILE, key_flags, NULL, &hKey, &dwDisposition);
if (lRv != ERROR_SUCCESS)
return false;
lRv = RegSetValueExA(hKey, "Debugger", 0, REG_SZ, (BYTE *) jit_cmd, strlen(jit_cmd) + 1 );
RegCloseKey(hKey);
return (lRv == ERROR_SUCCESS);
}

View File

@ -48,6 +48,8 @@ void dbgaddignoredexception(ExceptionRange range);
bool dbgisignoredexception(unsigned int exception);
bool dbgcmdnew(const char* name, CBCOMMAND cbCommand, bool debugonly);
bool dbgcmddel(const char* name);
bool dbggetjit(char** jit_entry_out, arch arch_in, arch* arch_out);
bool dbgsetjit(char* jit_cmd, arch arch_in, arch* arch_out);
void cbStep();
void cbRtrStep();

View File

@ -1394,139 +1394,8 @@ CMDRESULT cbDebugDownloadSymbol(int argc, char* argv[])
return STATUS_CONTINUE;
}
bool IsWow64()
{
BOOL bIsWow64 = FALSE;
typedef BOOL (APIENTRY *LPFN_ISWOW64PROCESS)
(HANDLE, PBOOL);
LPFN_ISWOW64PROCESS fnIsWow64Process;
HMODULE module = GetModuleHandle(TEXT("kernel32"));
const char funcName[] = "IsWow64Process";
fnIsWow64Process = (LPFN_ISWOW64PROCESS)
GetProcAddress(module, funcName);
if(NULL != fnIsWow64Process)
{
if (!fnIsWow64Process(GetCurrentProcess(),
&bIsWow64))
return false; //unkown error;
}
return bIsWow64 != FALSE;
}
#define JIT_REG_KEY TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug")
#define ATTACH_CMD_LINE "\" -a %ld"
int GetJIT( char ** jit_entry_out, arch arch_in, arch * arch_out )
{
DWORD key_flags = KEY_READ;
DWORD lRv;
HKEY hKey;
if (arch_out != NULL )
{
if ( arch_in != x64 && arch_in != x32 )
{
#ifdef _WIN32
* arch_out = x32;
#endif
#ifdef _WIN64
* arch_out = x64;
#endif
}
else
* arch_out = arch_in;
}
if ( arch_in == x64 )
{
if (!IsWow64())
return -1;
#ifdef _WIN32
key_flags |= KEY_WOW64_64KEY;
#endif
}
else if ( arch_in == x32 )
{
#ifdef _WIN64
key_flags |= KEY_WOW64_32KEY;
#endif
}
lRv = RegOpenKeyEx(HKEY_LOCAL_MACHINE, JIT_REG_KEY, 0, key_flags,&hKey);
if (lRv != ERROR_SUCCESS)
return -1;
char jit_entry[512];
DWORD jit_entry_size = sizeof( jit_entry );
lRv = RegQueryValueExA(hKey, "Debugger", 0, NULL, (LPBYTE)jit_entry, & jit_entry_size);
if (lRv != ERROR_SUCCESS)
return -1;
* jit_entry_out = (char *) calloc( jit_entry_size, 1 );
if ( * jit_entry_out == NULL )
return -1;
strcpy( * jit_entry_out, jit_entry );
return 0;
}
int SetJIT( char * jit_cmd, arch arch_in, arch * arch_out )
{
DWORD key_flags = KEY_WRITE;
DWORD lRv;
HKEY hKey;
DWORD dwDisposition;
if (arch_out != NULL )
{
if ( arch_in != x64 && arch_in != x32 )
{
#ifdef _WIN32
* arch_out = x32;
#endif
#ifdef _WIN64
* arch_out = x64;
#endif
}
else
* arch_out = arch_in;
}
if ( arch_in == x64 )
{
if (!IsWow64())
return -1;
#ifdef _WIN32
key_flags |= KEY_WOW64_64KEY;
#endif
}
else if ( arch_in == x32 )
{
#ifdef _WIN64
key_flags |= KEY_WOW64_32KEY;
#endif
}
lRv = RegCreateKeyEx(HKEY_LOCAL_MACHINE, JIT_REG_KEY, 0, NULL, REG_OPTION_NON_VOLATILE, key_flags, NULL, &hKey, &dwDisposition);
if (lRv != ERROR_SUCCESS)
return -1;
lRv = RegSetValueExA(hKey, "Debugger", 0, REG_SZ, (BYTE *) jit_cmd, strlen(jit_cmd) + 1 );
RegCloseKey(hKey);
if (lRv != ERROR_SUCCESS )
return -1;
return 0;
}
CMDRESULT cbDebugSetJIT(int argc, char* argv[])
{
arch actual_arch;
@ -1539,7 +1408,7 @@ CMDRESULT cbDebugSetJIT(int argc, char* argv[])
strcat(path, ATTACH_CMD_LINE);
jit_debugger_cmd = path;
if ( SetJIT( jit_debugger_cmd, notfound, & actual_arch ) == -1 )
if (!dbgsetjit( jit_debugger_cmd, notfound, & actual_arch ))
{
dprintf( "Error setting JIT %s\n", (actual_arch == x64) ? "x64" : "x32" );
return STATUS_ERROR;
@ -1548,7 +1417,7 @@ CMDRESULT cbDebugSetJIT(int argc, char* argv[])
else if ( argc == 2 )
{
jit_debugger_cmd = argv[1];
if ( SetJIT( jit_debugger_cmd, notfound, & actual_arch ) == -1 )
if (!dbgsetjit( jit_debugger_cmd, notfound, & actual_arch ))
{
dprintf( "Error setting JIT %s\n", (actual_arch == x64) ? "x64" : "x32" );
return STATUS_ERROR;
@ -1575,7 +1444,7 @@ CMDRESULT cbDebugSetJIT(int argc, char* argv[])
}
jit_debugger_cmd = argv[2];
if ( SetJIT( jit_debugger_cmd, actual_arch, NULL ) == -1 )
if (!dbgsetjit( jit_debugger_cmd, actual_arch, NULL))
{
dprintf( "Error getting JIT %s\n", (actual_arch == x64) ? "x64" : "x32" );
return STATUS_ERROR;
@ -1599,7 +1468,7 @@ CMDRESULT cbDebugGetJIT(int argc, char* argv[])
if(argc < 2)
{
if ( GetJIT( & get_entry, notfound, & actual_arch ) == -1 )
if (!dbggetjit( & get_entry, notfound, & actual_arch ))
{
dprintf( "Error getting JIT %s\n", (actual_arch == x64) ? "x64" : "x32" );
return STATUS_ERROR;
@ -1625,7 +1494,7 @@ CMDRESULT cbDebugGetJIT(int argc, char* argv[])
return STATUS_ERROR;
}
if ( GetJIT( & get_entry, actual_arch, NULL ) == -1 )
if (!dbggetjit( & get_entry, actual_arch, NULL ))
{
dprintf( "Error getting JIT %s\n", argv[1] );
return STATUS_ERROR;
@ -1634,7 +1503,7 @@ CMDRESULT cbDebugGetJIT(int argc, char* argv[])
dprintf( " JIT %s: %s\n", (actual_arch == x64) ? "x64" : "x32", get_entry );
if ( get_entry != NULL )
free(get_entry);
efree(get_entry);
return STATUS_CONTINUE;
}

View File

@ -284,6 +284,13 @@ extern "C" DLL_EXPORT const char* _dbg_dbginit()
DbgCmdExec(str.c_str());
}
commandlinefree(argc, argv);
char jit[MAX_SETTING_SIZE]="";
bool isx64=true;
#ifndef _WIN64
isx64=false;
#endif
DbgFunctions()->GetJit(jit, isx64);
MessageBoxA(0,jit,0,0);
return 0;
}