Windows minor fixes, renamed w32 function names, added dbg.bpsysign option (#8356)

This commit is contained in:
josediazfer 2017-08-28 10:19:57 +02:00 committed by radare
parent f91a3bba6f
commit f11fea1e2b
5 changed files with 99 additions and 98 deletions

View File

@ -2385,6 +2385,7 @@ R_API int r_core_config_init(RCore *core) {
#else
SETICB ("dbg.bpsize", 1, &cb_dbgbpsize, "Size of software breakpoints");
#endif
SETPREF ("dbg.bpsysign", "false", "Ignore system breakpoints");
SETICB ("dbg.btdepth", 128, &cb_dbgbtdepth, "Depth of backtrace");
SETCB ("dbg.trace", "false", &cb_trace, "Trace program execution (see asm.trace)");
SETICB ("dbg.trace.tag", 0, &cb_tracetag, "Trace tag");

View File

@ -1044,8 +1044,9 @@ repeat:
}
}
}
if (reason == R_DEBUG_REASON_BREAKPOINT && bp && !bp->enabled) {
if (reason == R_DEBUG_REASON_BREAKPOINT &&
((bp && !bp->enabled) || (!bp && !r_cons_is_breaked () && dbg->corebind.core &&
dbg->corebind.cfggeti (dbg->corebind.core, "dbg.bpsysign")))) {
goto repeat;
}

View File

@ -168,7 +168,7 @@ static int r_debug_native_attach (RDebug *dbg, int pid) {
return linux_attach (dbg, pid);
#elif __WINDOWS__ && !__CYGWIN__
int ret;
HANDLE process = w32_open_process (PROCESS_ALL_ACCESS, FALSE, pid);
HANDLE process = w32_OpenProcess (PROCESS_ALL_ACCESS, FALSE, pid);
if (process != (HANDLE)NULL && DebugActiveProcess (pid)) {
ret = w32_first_thread (pid);
} else {
@ -196,7 +196,7 @@ static int r_debug_native_attach (RDebug *dbg, int pid) {
static int r_debug_native_detach (RDebug *dbg, int pid) {
#if __WINDOWS__ && !__CYGWIN__
return w32_detach (pid)? 0 : -1;
return w32_DebugActiveProcessStop (pid)? 0 : -1;
#elif __CYGWIN__
#warning "r_debug_native_detach not supported on this platform"
return -1;
@ -940,7 +940,7 @@ static RDebugMap* r_debug_native_map_alloc (RDebug *dbg, ut64 addr, int size) {
#elif __WINDOWS__ && !__CYGWIN__
RDebugMap *map = NULL;
LPVOID base = NULL;
HANDLE process = w32_open_process (PROCESS_ALL_ACCESS, FALSE, dbg->pid);
HANDLE process = w32_OpenProcess (PROCESS_ALL_ACCESS, FALSE, dbg->pid);
if (process == INVALID_HANDLE_VALUE) {
return map;
}
@ -966,7 +966,7 @@ static int r_debug_native_map_dealloc (RDebug *dbg, ut64 addr, int size) {
return xnu_map_dealloc (dbg, addr, size);
#elif __WINDOWS__ && !__CYGWIN__
HANDLE process = w32_open_process (PROCESS_ALL_ACCESS, FALSE, dbg->tid);
HANDLE process = w32_OpenProcess (PROCESS_ALL_ACCESS, FALSE, dbg->tid);
if (process == INVALID_HANDLE_VALUE) {
return false;
}
@ -1395,7 +1395,7 @@ static RList *win_desc_list (int pid) {
NTSTATUS status;
ULONG handleInfoSize = 0x10000;
LPVOID buff;
if (!(processHandle = w32_openprocess (0x0040, FALSE, pid))) {
if (!(processHandle = w32_OpenProcess (0x0040, FALSE, pid))) {
eprintf ("win_desc_list: Error opening process.\n");
return NULL;
}
@ -1560,7 +1560,7 @@ static RList *r_debug_desc_native_list (int pid) {
static int r_debug_native_map_protect (RDebug *dbg, ut64 addr, int size, int perms) {
#if __WINDOWS__ && !__CYGWIN__
DWORD old;
HANDLE process = w32_open_process (PROCESS_ALL_ACCESS, FALSE, dbg->pid);
HANDLE process = w32_OpenProcess (PROCESS_ALL_ACCESS, FALSE, dbg->pid);
// TODO: align pointers
BOOL ret = VirtualProtectEx (WIN32_PI (process), (LPVOID)(UINT)addr,
size, perms, &old);

View File

@ -66,9 +66,7 @@ static RDebugMap *add_map(RList *list, const char *name, ut64 addr, ut64 len, ME
r_list_append (list, mr);
}
err_add_map:
if (map_name) {
free (map_name);
}
free (map_name);
return mr;
}
@ -77,14 +75,10 @@ static RDebugMap *add_map_reg(RList *list, const char *name, MEMORY_BASIC_INFORM
}
static RList *w32_dbg_modules(RDebug *dbg) {
MODULEENTRY32 me32;
RDebugMap *mr;
RList *list = r_list_new ();
#if MINGW32
HANDLE h_mod_snap = NULL;
#else
HANDLE h_mod_snap = CreateToolhelp32Snapshot ( TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, dbg->pid );
#endif
MODULEENTRY32 me32;
RDebugMap *mr;
RList *list = r_list_new ();
HANDLE h_mod_snap = w32_CreateToolhelp32Snapshot (TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, dbg->pid);
if (!h_mod_snap) {
r_sys_perror ("w32_dbg_modules/CreateToolhelp32Snapshot");
@ -141,21 +135,17 @@ static int set_mod_inf(HANDLE h_proc, RDebugMap *map, RWinModInfo *mod) {
mod->sect_count = nt_hdrs->FileHeader.NumberOfSections;
sect_hdr = (IMAGE_SECTION_HEADER *)((char *)nt_hdrs + sizeof (IMAGE_NT_HEADERS));
}
if (sect_hdr) {
mod->sect_hdr = (IMAGE_SECTION_HEADER *)malloc (sizeof (IMAGE_SECTION_HEADER) * mod->sect_count);
if (!mod->sect_hdr) {
perror ("malloc set_mod_inf()");
goto err_set_mod_info;
}
memcpy (mod->sect_hdr, sect_hdr, sizeof (IMAGE_SECTION_HEADER) * mod->sect_count);
mod_inf_fill = 0;
mod->sect_hdr = (IMAGE_SECTION_HEADER *)malloc (sizeof (IMAGE_SECTION_HEADER) * mod->sect_count);
if (!mod->sect_hdr) {
perror ("malloc set_mod_inf()");
goto err_set_mod_info;
}
memcpy (mod->sect_hdr, sect_hdr, sizeof (IMAGE_SECTION_HEADER) * mod->sect_count);
mod_inf_fill = 0;
}
err_set_mod_info:
if (mod_inf_fill == -1) {
if (mod->sect_hdr) {
free (mod->sect_hdr);
}
R_FREE (mod->sect_hdr);
}
return mod_inf_fill;
}
@ -167,9 +157,7 @@ static void proc_mem_img(HANDLE h_proc, RList *map_list, RList *mod_list, RWinMo
RListIter *iter;
RDebugMap *map;
if (mod->sect_hdr) {
free (mod->sect_hdr);
}
free (mod->sect_hdr);
memset (mod, 0, sizeof (RWinModInfo));
r_list_foreach (mod_list, iter, map) {
if (addr >= map->addr && addr <= map->addr_end) {
@ -246,9 +234,9 @@ static RList *w32_dbg_maps(RDebug *dbg) {
RList *map_list = r_list_new(), *mod_list = NULL;
GetSystemInfo (&si);
h_proc = w32_openprocess (PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dbg->pid);
h_proc = w32_OpenProcess (PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dbg->pid);
if (!h_proc) {
r_sys_perror ("w32_dbg_maps/w32_openprocess");
r_sys_perror ("w32_dbg_maps/w32_OpenProcess");
goto err_w32_dbg_maps;
}
cur_addr = si.lpMinimumApplicationAddress;
@ -272,9 +260,7 @@ static RList *w32_dbg_maps(RDebug *dbg) {
cur_addr = (LPVOID)(size_t)((ut64)(size_t)mbi.BaseAddress + mbi.RegionSize);
}
err_w32_dbg_maps:
if (mod_inf.sect_hdr) {
free (mod_inf.sect_hdr);
}
free (mod_inf.sect_hdr);
r_list_free (mod_list);
return map_list;
}

View File

@ -155,12 +155,12 @@ typedef struct _OBJECT_TYPE_INFORMATION
static void (*w32_GetModuleBaseName)(HANDLE, HMODULE, LPCSTR, int) = NULL;
static int (*w32_GetModuleInformation)(HANDLE, HMODULE, LPMODULEINFO, int) = NULL;
static BOOL (WINAPI *w32_detach)(DWORD) = NULL;
static BOOL (WINAPI *w32_DebugActiveProcessStop)(DWORD) = NULL;
static HANDLE (WINAPI *w32_openthread)(DWORD, BOOL, DWORD) = NULL;
static BOOL (WINAPI *w32_dbgbreak)(HANDLE) = NULL;
static BOOL (WINAPI *w32_DebugBreakProcess)(HANDLE) = NULL;
static DWORD (WINAPI *w32_getthreadid)(HANDLE) = NULL; // Vista
static DWORD (WINAPI *w32_getprocessid)(HANDLE) = NULL; // XP
static HANDLE (WINAPI *w32_openprocess)(DWORD, BOOL, DWORD) = NULL;
static HANDLE (WINAPI *w32_OpenProcess)(DWORD, BOOL, DWORD) = NULL;
static BOOL (WINAPI *w32_queryfullprocessimagename)(HANDLE, DWORD, LPCSTR, PDWORD) = NULL;
static DWORD (WINAPI *w32_GetMappedFileName)(HANDLE, LPVOID, LPCSTR, DWORD) = NULL;
static NTSTATUS (WINAPI *w32_ntquerysysteminformation)(ULONG, PVOID, ULONG, PULONG) = NULL;
@ -174,6 +174,7 @@ static BOOL (WINAPI *w32_GetXStateFeaturesMask)(PCONTEXT Context, PDWORD64) = NU
static PVOID(WINAPI *w32_LocateXStateFeature)(PCONTEXT Context, DWORD, PDWORD) = NULL;
static BOOL (WINAPI *w32_SetXStateFeaturesMask)(PCONTEXT Context, DWORD64) = NULL;
static DWORD (WINAPI *w32_GetModuleFileNameEx)(HANDLE, HMODULE, LPTSTR, DWORD) = NULL;
static HANDLE (WINAPI *w32_CreateToolhelp32Snapshot)(DWORD, DWORD) = NULL;
#ifndef XSTATE_GSSE
#define XSTATE_GSSE 2
@ -243,14 +244,14 @@ static int w32_dbg_init() {
/* escalate privs (required for win7/vista) */
w32dbg_SeDebugPrivilege ();
/* lookup function pointers for portability */
w32_detach = (BOOL (WINAPI *)(DWORD))
w32_DebugActiveProcessStop = (BOOL (WINAPI *)(DWORD))
GetProcAddress (GetModuleHandleA ("kernel32"),
"DebugActiveProcessStop");
w32_openthread = (HANDLE (WINAPI *)(DWORD, BOOL, DWORD))
GetProcAddress (GetModuleHandleA ("kernel32"), "OpenThread");
w32_openprocess = (HANDLE (WINAPI *)(DWORD, BOOL, DWORD))
w32_OpenProcess = (HANDLE (WINAPI *)(DWORD, BOOL, DWORD))
GetProcAddress (GetModuleHandleA ("kernel32"), "OpenProcess");
w32_dbgbreak = (BOOL (WINAPI *)(HANDLE))
w32_DebugBreakProcess = (BOOL (WINAPI *)(HANDLE))
GetProcAddress (GetModuleHandleA ("kernel32"),
"DebugBreakProcess");
// only windows vista :(
@ -295,7 +296,9 @@ static int w32_dbg_init() {
GetProcAddress(lib,"NtQueryObject");
w32_ntqueryinformationthread = (NTSTATUS (WINAPI *)(HANDLE, ULONG, PVOID, ULONG, PULONG))
GetProcAddress (lib, "NtQueryInformationThread");
if (!w32_detach || !w32_openthread || !w32_dbgbreak ||
w32_CreateToolhelp32Snapshot = (HANDLE (WINAPI *)(DWORD, DWORD))
GetProcAddress (lib, "CreateToolhelp32Snapshot");
if (!w32_DebugActiveProcessStop || !w32_openthread || !w32_DebugBreakProcess ||
!w32_GetModuleBaseName || !w32_GetModuleInformation) {
// OOPS!
eprintf ("debug_init_calls:\n"
@ -303,20 +306,12 @@ static int w32_dbg_init() {
"OpenThread: 0x%p\n"
"DebugBreakProcess: 0x%p\n"
"GetThreadId: 0x%p\n",
w32_detach, w32_openthread, w32_dbgbreak, w32_getthreadid);
w32_DebugActiveProcessStop, w32_openthread, w32_DebugBreakProcess, w32_getthreadid);
return false;
}
return true;
}
static HANDLE w32_open_process (DWORD access, BOOL inherit, DWORD pid) {
HANDLE h = w32_openprocess(access, inherit, pid);
if (h == INVALID_HANDLE_VALUE) {
r_sys_perror ("w32_open_process/OpenProcess");
}
return h;
}
#if 0
static HANDLE w32_t2h(pid_t tid) {
TH_INFO *th = get_th (tid);
@ -382,6 +377,32 @@ err_load_th:
return pid;
}
static char *get_w32_excep_name(unsigned long code) {
char *desc;
switch (code) {
/* fatal exceptions */
case EXCEPTION_ACCESS_VIOLATION:
desc = "access violation";
break;
case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
desc = "array bounds exceeded";
break;
case EXCEPTION_ILLEGAL_INSTRUCTION:
desc = "illegal instruction";
break;
case EXCEPTION_INT_DIVIDE_BY_ZERO:
desc = "divide by zero";
break;
case EXCEPTION_STACK_OVERFLOW:
desc = "stack overflow";
break;
default:
desc = "unknown";
}
return desc;
}
static int debug_exception_event (DEBUG_EVENT *de) {
unsigned long code = de->u.Exception.ExceptionRecord.ExceptionCode;
switch (code) {
@ -395,8 +416,10 @@ static int debug_exception_event (DEBUG_EVENT *de) {
case EXCEPTION_ILLEGAL_INSTRUCTION:
case EXCEPTION_INT_DIVIDE_BY_ZERO:
case EXCEPTION_STACK_OVERFLOW:
eprintf ("(%d) Fatal exception in thread %d\n",
(int)de->dwProcessId, (int)de->dwThreadId);
eprintf ("(%d) Fatal exception (%s) in thread %d\n",
(int)de->dwProcessId,
get_w32_excep_name(code),
(int)de->dwThreadId);
break;
#if __MINGW64__ || _WIN64
/* STATUS_WX86_BREAKPOINT */
@ -801,10 +824,10 @@ err_load_th:
}
static RDebugPid *build_debug_pid(PROCESSENTRY32 *pe) {
HANDLE process = w32_open_process (0x1000, //PROCESS_QUERY_LIMITED_INFORMATION,
HANDLE process = w32_OpenProcess (0x1000, //PROCESS_QUERY_LIMITED_INFORMATION,
FALSE, pe->th32ProcessID);
if (process == INVALID_HANDLE_VALUE || !w32_queryfullprocessimagename) {
if (!process || !w32_queryfullprocessimagename) {
return r_debug_pid_new (pe->szExeFile, pe->th32ProcessID, 0, 's', 0);
}
@ -854,65 +877,55 @@ RList *w32_pids (int pid, RList *list) {
return list;
}
int w32_terminate_process (RDebug *dbg, int pid) {
HANDLE process = w32_open_process(PROCESS_TERMINATE | SYNCHRONIZE , FALSE, pid);
if (process == INVALID_HANDLE_VALUE) {
return false;
bool w32_terminate_process (RDebug *dbg, int pid) {
HANDLE h_proc = w32_OpenProcess(PROCESS_TERMINATE | SYNCHRONIZE , FALSE, pid);
bool ret = false;
if (!h_proc) {
r_sys_perror ("w32_terminate_process/OpenProcess");
goto err_w32_terminate_process;
}
/* stop debugging if we are still attached */
if (w32_detach) {
w32_detach (pid); //DebugActiveProcessStop (pid);
if (w32_DebugActiveProcessStop) {
w32_DebugActiveProcessStop (pid); //DebugActiveProcessStop (pid);
}
if (TerminateProcess (process, 1) == 0) {
r_sys_perror ("w32_terminate_process/TerminateProcess");
CloseHandle (process);
return false;
if (TerminateProcess (h_proc, 1) == 0) {
r_sys_perror ("e32_terminate_process/TerminateProcess");
goto err_w32_terminate_process;
}
DWORD ret_wait;
/* wait up to one second to give the process some time to exit */
ret_wait = WaitForSingleObject (process, 1000);
DWORD ret_wait = WaitForSingleObject (h_proc, 1000);
if (ret_wait == WAIT_FAILED) {
r_sys_perror ("w32_terminate_process/WaitForSingleObject");
CloseHandle (process);
return false;
goto err_w32_terminate_process;
}
if (ret_wait == WAIT_TIMEOUT) {
eprintf ("(%d) Waiting for process to terminate timed out.\n", pid);
CloseHandle (process);
return false;
goto err_w32_terminate_process;
}
return true;
ret = true;
err_w32_terminate_process:
if (h_proc) {
CloseHandle (h_proc);
}
return ret;
}
void w32_break_process (void *d) {
static HANDLE (WINAPI *w32_dbgbreak)(HANDLE) = NULL;
RDebug *dbg = (RDebug *)d;
HANDLE lib;
HANDLE process = w32_open_process (PROCESS_ALL_ACCESS, FALSE, dbg->pid);
if (process == INVALID_HANDLE_VALUE) {
return;
HANDLE h_proc = w32_OpenProcess (PROCESS_ALL_ACCESS, FALSE, dbg->pid);
if (!h_proc) {
r_sys_perror ("w32_break_process/w32_OpenProcess");
goto err_w32_break_process;
}
lib = LoadLibraryA ("kernel32.dll");
if (!lib) {
r_sys_perror ("w32_break_process/LoadLibrary");
CloseHandle (process);
return;
if (!w32_DebugBreakProcess (h_proc)) {
r_sys_perror ("w32_break_process/w32_DebugBreakProcess");
goto err_w32_break_process;
}
if (!w32_dbgbreak) {
w32_dbgbreak = (HANDLE (WINAPI *)(HANDLE))
GetProcAddress (GetModuleHandleA ("kernel32"),
"DebugBreakProcess");
err_w32_break_process:
if (h_proc) {
CloseHandle (h_proc);
}
if (process != INVALID_HANDLE_VALUE && w32_dbgbreak != NULL) {
if (!w32_dbgbreak (process)) {
r_sys_perror ("w32_break_process/DebugBreakProcess");
}
}
CloseHandle (process);
CloseHandle (lib);
}
static int GetAVX (HANDLE hThread, ut128 * xmm, ut128 * ymm) {