Skip to content

Commit

Permalink
windbg ^c support under linux+ ^c support for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
skuater@hotmail.com authored and radare committed May 5, 2015
1 parent eabfaaf commit ccfae11
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 23 deletions.
1 change: 1 addition & 0 deletions libr/cons/cons.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ static HANDLE h;
static BOOL __w32_control(DWORD type) {
if (type == CTRL_C_EVENT) {
break_signal (2); // SIGINT
eprintf("{ctrl+c} pressed.\n");
return R_TRUE;
}
return R_FALSE;
Expand Down
41 changes: 41 additions & 0 deletions libr/debug/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -454,12 +454,45 @@ R_API int r_debug_step_over(RDebug *dbg, int steps) {

return i;
}
#if __WINDOWS__
static int winbreak=0;
static void static_debug_native_break(void *d) {
static BOOL WINAPI (*w32_dbgbreak)(HANDLE) = NULL;
static HANDLE WINAPI (*w32_openprocess)(DWORD, BOOL, DWORD) = NULL;
static void WINAPI (*w32_dbgbreaksimple)(void) = NULL;
int ret=0;
RDebug *dbg = (RDebug *)d;
HANDLE lib;
HANDLE hProcess;
lib = LoadLibrary ("kernel32.dll");
if (!w32_dbgbreak) {
w32_dbgbreak = (HANDLE WINAPI (*)(HANDLE))
GetProcAddress (GetModuleHandle ("kernel32"),
"DebugBreakProcess");
}
if (!w32_openprocess) {
w32_openprocess=(HANDLE WINAPI (*)(DWORD, BOOL, DWORD))
GetProcAddress (GetModuleHandle ("kernel32"),
"OpenProcess");
}
if (w32_dbgbreak!=NULL && w32_openprocess!=NULL) {
hProcess=w32_openprocess(PROCESS_ALL_ACCESS,FALSE, dbg->pid );
winbreak=1;
w32_dbgbreak(hProcess);
CloseHandle(lib);
CloseHandle(hProcess);
}
}
#endif

R_API int r_debug_continue_kill(RDebug *dbg, int sig) {
ut64 pc;
int retwait, ret = R_FALSE;
if (!dbg)
return R_FALSE;
#if __WINDOWS__
r_cons_break(static_debug_native_break,dbg);
#endif
repeat:
if (r_debug_is_dead (dbg))
return R_FALSE;
Expand All @@ -468,6 +501,14 @@ R_API int r_debug_continue_kill(RDebug *dbg, int sig) {
ret = dbg->h->cont (dbg, dbg->pid, dbg->tid, sig);
dbg->signum = 0;
retwait = r_debug_wait (dbg);
#if __WINDOWS__
if (winbreak) {
int tmp=ret;
ret=dbg->tid;
dbg->tid=tmp;
winbreak=0;
}
#endif
r_bp_restore (dbg->bp, R_FALSE); // unset sw breakpoints
//r_debug_recoil (dbg);
if (r_debug_recoil (dbg) || dbg->reason == R_DBG_REASON_BP) {
Expand Down
2 changes: 2 additions & 0 deletions libr/debug/p/debug_native.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <sys/param.h>
#include "native/drx.c" // x86 specific
#include "native/reg.c" // x86 specific
#include "r_cons.h"

#if DEBUGGER

Expand Down Expand Up @@ -473,6 +474,7 @@ static int r_debug_native_continue_syscall(RDebug *dbg, int pid, int num) {
static int r_debug_native_continue(RDebug *dbg, int pid, int tid, int sig) {
void *data = (void*)(size_t)((sig != -1)?sig: dbg->signum);
#if __WINDOWS__ && !__CYGWIN__
eprintf("r_debug_native_continue: pid=%08x tid=%08x\n",pid,tid);
if (ContinueDebugEvent (pid, tid, DBG_CONTINUE) == 0) {
print_lasterr ((char *)__FUNCTION__);
eprintf ("debug_contp: error\n");
Expand Down
4 changes: 3 additions & 1 deletion libr/debug/p/debug_wind.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ static void wstatic_debug_break(void *u) {
dbreak=1;
wind_break_read(wctx);
}

static int r_debug_wind_wait (RDebug *dbg, int pid) {
#define STATE_EXCEPTION 0x3030
kd_packet_t *pkt;
kd_stc_64 *stc;
int ret;
Expand All @@ -89,7 +91,7 @@ static int r_debug_wind_wait (RDebug *dbg, int pid) {

stc = (kd_stc_64 *)pkt->data;
// Handle exceptions only
if (stc->state == 0x3030) {
if (stc->state == STATE_EXCEPTION) {
wind_set_cpu (wctx, stc->cpu);
free (pkt);
dbg->reason = R_DBG_REASON_INT;
Expand Down
24 changes: 10 additions & 14 deletions libr/debug/p/native/w32.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,15 @@ return (0);
#endif

//BOOL WINAPI DebugActiveProcessStop(DWORD dwProcessId);

BOOL WINAPI DebugBreakProcess(
_In_ HANDLE Process
);
static void (*gmbn)(HANDLE, HMODULE, LPTSTR, int) = NULL;
static int (*gmi)(HANDLE, HMODULE, LPMODULEINFO, int) = NULL;
static BOOL WINAPI (*w32_detach)(DWORD) = NULL;
static HANDLE WINAPI (*w32_openthread)(DWORD, BOOL, DWORD) = NULL;
static HANDLE WINAPI (*w32_dbgbreak)(HANDLE) = NULL;
static BOOL WINAPI (*w32_dbgbreak)(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;
Expand Down Expand Up @@ -328,23 +332,16 @@ static int w32_dbg_wait(RDebug *dbg, int pid) {
}
/* save thread id */
tid = de.dwThreadId;
//pid = de.dwProcessId;
dbg->tid=tid;
/* get exception code */
code = de.dwDebugEventCode;
//eprintf("code: %x pid=%08x tid=%08x\n",code,pid,tid);
/* Ctrl-C? */
//if (code == 0x2) {
// TODO: interrupted
//WS(event) = INT_EVENT;
//break;
//}
/* set state */
//WS(event) = UNKNOWN_EVENT;
/* get kind of event */
switch (code) {
case CREATE_PROCESS_DEBUG_EVENT:
eprintf ("(%d) created process (%d:%p)\n",
pid, w32_h2t (de.u.CreateProcessInfo.
hProcess),
de.u.CreateProcessInfo.lpStartAddress);
eprintf ("(%d) created process (%d:%p)\n", pid, w32_h2t (de.u.CreateProcessInfo.hProcess), de.u.CreateProcessInfo.lpStartAddress);
r_debug_native_continue (dbg, pid, tid, -1);
next_event = 1;
ret = R_DBG_REASON_NEW_PID;
Expand All @@ -368,8 +365,7 @@ static int w32_dbg_wait(RDebug *dbg, int pid) {
ret = R_DBG_REASON_EXIT_TID;
break;
case LOAD_DLL_DEBUG_EVENT:
eprintf ("(%d) Loading %s library at %p\n",
pid, "", de.u.LoadDll.lpBaseOfDll);
eprintf ("(%d) Loading %s library at %p\n",pid, "", de.u.LoadDll.lpBaseOfDll);
r_debug_native_continue (dbg, pid, tid, -1);
next_event = 1;
ret = R_DBG_REASON_NEW_LIB;
Expand Down
23 changes: 22 additions & 1 deletion shlr/wind/iob_pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,28 @@ static int iob_pipe_close (void *p) {
}

static int iob_pipe_read (void *p, uint8_t *buf, const uint64_t count, const int timeout) {
return recv((int)(size_t)p, buf, count, 0);
//return recv((int)(size_t)p, buf, count, 0);
int result;
fd_set readset;
int fd=(int)(size_t)p;
do {
FD_ZERO(&readset);
FD_SET(fd, &readset);
result = select(fd + 1, &readset, NULL, NULL, NULL);
if (result == 0) { // pipe closed
return -1;
}
else if (result > 0) { // data to read
if (FD_ISSET(fd, &readset)) {
return recv((int)(size_t)p, buf, count, 0);
}
}
else {
//eprintf("Error on select()");//: %s\", strerror(errno));
return -1;
}
} while (result == -1 && errno == EINTR);
return EINTR;
}

static int iob_pipe_write (void *p, const uint8_t *buf, const uint64_t count, const int timeout) {
Expand Down
17 changes: 10 additions & 7 deletions shlr/wind/wind.c
Original file line number Diff line number Diff line change
Expand Up @@ -1116,16 +1116,19 @@ wind_break (WindCtx *ctx) {
return 1;
}

#if __WINDOWS__
static BOOL WINAPI (*w32_CancelIoEx)(HANDLE, LPOVERLAPPED) = NULL;
#endif
int
wind_break_read (WindCtx *ctx) {
#if __WINDOWS__
HANDLE lib;
lib = LoadLibrary ("psapi.dll");
w32_CancelIoEx = (BOOL WINAPI (*)(HANDLE, LPOVERLAPPED))GetProcAddress (GetModuleHandle ("kernel32"),"CancelIoEx");
w32_CancelIoEx(ctx->io_ptr,NULL);
static BOOL WINAPI (*w32_CancelIoEx)(HANDLE, LPOVERLAPPED) = NULL;
if (!w32_CancelIoEx) {
HANDLE lib;
lib = LoadLibrary ("psapi.dll");
w32_CancelIoEx = (BOOL WINAPI (*)(HANDLE, LPOVERLAPPED))
GetProcAddress (GetModuleHandle ("kernel32"),
"CancelIoEx");
}
if (w32_CancelIoEx)
w32_CancelIoEx(ctx->io_ptr,NULL);
#endif
return 1;
}

0 comments on commit ccfae11

Please sign in to comment.