#include <stdarg.h>
#include <windows.h>
#include <winsock.h>
#include "wince-stub.h"
#define MALLOC(n) (void *) LocalAlloc (LMEM_MOVEABLE | LMEM_ZEROINIT, (UINT)(n))
#define REALLOC(s, n) (void *) LocalReAlloc ((HLOCAL)(s), (UINT)(n), LMEM_MOVEABLE)
#define FREE(s) LocalFree ((HLOCAL)(s))
static int skip_next_id = 0;
static void
vstub_error (LPCWSTR fmt, va_list args)
{
WCHAR buf[4096];
wvsprintfW (buf, fmt, args);
MessageBoxW (NULL, buf, L"GDB", MB_ICONERROR);
WSACleanup ();
ExitThread (1);
}
static void
stub_error (LPCWSTR fmt, ...)
{
va_list args;
va_start (args, fmt);
vstub_error (fmt, args);
}
static LPVOID
mempool (unsigned int len)
{
static int outn = -1;
static LPWSTR outs[4] = {NULL, NULL, NULL, NULL};
if (++outn >= (sizeof (outs) / sizeof (outs[0])))
outn = 0;
if (outs[outn])
FREE (outs[outn]);
outs[outn] = (LPWSTR) MALLOC (len);
return outs[outn];
}
static void
attempt_resync (LPCWSTR huh, int s)
{
stub_error (L"lost synchronization with host attempting %s. Error %d", huh, WSAGetLastError ());
}
static int
sockread (LPCWSTR huh, int s, void *str, size_t n)
{
for (;;)
{
if (recv (s, str, n, 0) == (int) n)
return n;
attempt_resync (huh, s);
}
}
static int
sockwrite (LPCWSTR huh, int s, const void *str, size_t n)
{
for (;;)
{
if (send (s, str, n, 0) == (int) n)
return n;
attempt_resync (huh, s);
}
}
static DWORD
getdword (LPCWSTR huh, int s, gdb_wince_id what_this)
{
DWORD n;
gdb_wince_id what;
if (skip_next_id)
skip_next_id = 0;
else
do
if (sockread (huh, s, &what, sizeof (what)) != sizeof (what))
stub_error (L"error getting record type from host - %s.", huh);
while (what_this != what);
if (sockread (huh, s, &n, sizeof (n)) != sizeof (n))
stub_error (L"error getting %s from host.", huh);
return n;
}
static WORD
getword (LPCWSTR huh, int s, gdb_wince_id what_this)
{
WORD n;
gdb_wince_id what;
if (skip_next_id)
skip_next_id = 0;
else
do
if (sockread (huh, s, &what, sizeof (what)) != sizeof (what))
stub_error (L"error getting record type from host - %s.", huh);
while (what_this != what);
if (sockread (huh, s, &n, sizeof (n)) != sizeof (n))
stub_error (L"error getting %s from host.", huh);
return n;
}
#define gethandle(huh, s, what) (HANDLE) getdword ((huh), (s), (what))
#define getpvoid(huh, s, what) (LPVOID) getdword ((huh), (s), (what))
#define getlen(huh, s, what) (gdb_wince_len) getword ((huh), (s), (what))
static LPWSTR
getmemory (LPCWSTR huh, int s, gdb_wince_id what, gdb_wince_len *inlen)
{
LPVOID p;
gdb_wince_len dummy;
if (!inlen)
inlen = &dummy;
*inlen = getlen (huh, s, what);
p = mempool ((unsigned int) *inlen);
if ((gdb_wince_len) sockread (huh, s, p, *inlen) != *inlen)
stub_error (L"error getting string from host.");
return p;
}
static void
putdword (LPCWSTR huh, int s, gdb_wince_id what, DWORD n)
{
if (sockwrite (huh, s, &what, sizeof (what)) != sizeof (what))
stub_error (L"error writing record id for %s to host.", huh);
if (sockwrite (huh, s, &n, sizeof (n)) != sizeof (n))
stub_error (L"error writing %s to host.", huh);
}
static void
putword (LPCWSTR huh, int s, gdb_wince_id what, WORD n)
{
if (sockwrite (huh, s, &what, sizeof (what)) != sizeof (what))
stub_error (L"error writing record id for %s to host.", huh);
if (sockwrite (huh, s, &n, sizeof (n)) != sizeof (n))
stub_error (L"error writing %s to host.", huh);
}
#define putlen(huh, s, what, n) putword ((huh), (s), (what), (gdb_wince_len) (n))
static void
putmemory (LPCWSTR huh, int s, gdb_wince_id what, const void *mem, gdb_wince_len len)
{
putlen (huh, s, what, len);
if (((short) len > 0) && (gdb_wince_len) sockwrite (huh, s, mem, len) != len)
stub_error (L"error writing memory to host.");
}
static void
putresult (LPCWSTR huh, gdb_wince_result res, int s, gdb_wince_id what, const void *mem, gdb_wince_len len)
{
if (!res)
len = -(int) GetLastError ();
putmemory (huh, s, what, mem, len);
}
static HANDLE curproc;
static void
create_process (int s)
{
LPWSTR exec_file = getmemory (L"CreateProcess exec_file", s, GDB_CREATEPROCESS, NULL);
LPWSTR args = getmemory (L"CreateProcess args", s, GDB_CREATEPROCESS, NULL);
DWORD flags = getdword (L"CreateProcess flags", s, GDB_CREATEPROCESS);
PROCESS_INFORMATION pi;
gdb_wince_result res;
res = CreateProcessW (exec_file,
args,
NULL,
NULL,
FALSE,
flags,
NULL,
NULL,
NULL,
&pi);
putresult (L"CreateProcess", res, s, GDB_CREATEPROCESS, &pi, sizeof (pi));
curproc = pi.hProcess;
}
static void
terminate_process (int s)
{
gdb_wince_result res;
HANDLE h = gethandle (L"TerminateProcess handle", s, GDB_TERMINATEPROCESS);
res = TerminateProcess (h, 0) || 1;
putresult (L"Terminate process result", res, s, GDB_TERMINATEPROCESS,
&res, sizeof (res));
}
static int stepped = 0;
static void
flag_single_step (int s)
{
stepped = 1;
skip_next_id = 0;
}
struct skipper
{
wchar_t *s;
int nskip;
} skippy[] =
{
{L"Undefined Instruction:", 1},
{L"Data Abort:", 2},
{NULL, 0}
};
static int
skip_message (DEBUG_EVENT *ev)
{
char s[80];
DWORD nread;
struct skipper *skp;
int nbytes = ev->u.DebugString.nDebugStringLength;
if (nbytes > sizeof(s))
nbytes = sizeof(s);
memset (s, 0, sizeof (s));
if (!ReadProcessMemory (curproc, ev->u.DebugString.lpDebugStringData,
s, nbytes, &nread))
return 0;
for (skp = skippy; skp->s != NULL; skp++)
if (wcsncmp ((wchar_t *) s, skp->s, wcslen (skp->s)) == 0)
return skp->nskip;
return 0;
}
static void
wait_for_debug_event (int s)
{
DWORD ms = getdword (L"WaitForDebugEvent ms", s, GDB_WAITFORDEBUGEVENT);
gdb_wince_result res;
DEBUG_EVENT ev;
static int skip_next = 0;
for (;;)
{
res = WaitForDebugEvent (&ev, ms);
if (ev.dwDebugEventCode == OUTPUT_DEBUG_STRING_EVENT)
{
if (skip_next)
{
skip_next--;
goto ignore;
}
if (skip_next = skip_message (&ev))
goto ignore;
}
putresult (L"WaitForDebugEvent event", res, s, GDB_WAITFORDEBUGEVENT,
&ev, sizeof (ev));
break;
ignore:
ContinueDebugEvent (ev.dwProcessId, ev.dwThreadId, DBG_CONTINUE);
}
return;
}
static void
get_thread_context (int s)
{
CONTEXT c;
HANDLE h = gethandle (L"GetThreadContext handle", s, GDB_GETTHREADCONTEXT);
gdb_wince_result res;
memset (&c, 0, sizeof (c));
c.ContextFlags = getdword (L"GetThreadContext flags", s, GDB_GETTHREADCONTEXT);
res = (gdb_wince_result) GetThreadContext (h, &c);
putresult (L"GetThreadContext data", res, s, GDB_GETTHREADCONTEXT,
&c, sizeof (c));
}
static void
set_thread_context (int s)
{
gdb_wince_result res;
HANDLE h = gethandle (L"SetThreadContext handle", s, GDB_SETTHREADCONTEXT);
LPCONTEXT pc = (LPCONTEXT) getmemory (L"SetThreadContext context", s,
GDB_SETTHREADCONTEXT, NULL);
res = SetThreadContext (h, pc);
putresult (L"SetThreadContext result", res, s, GDB_SETTHREADCONTEXT,
&res, sizeof (res));
}
static void
read_process_memory (int s)
{
HANDLE h = gethandle (L"ReadProcessMemory handle", s, GDB_READPROCESSMEMORY);
LPVOID p = getpvoid (L"ReadProcessMemory base", s, GDB_READPROCESSMEMORY);
gdb_wince_len len = getlen (L"ReadProcessMemory size", s, GDB_READPROCESSMEMORY);
LPVOID buf = mempool ((unsigned int) len);
DWORD outlen;
gdb_wince_result res;
outlen = 0;
res = (gdb_wince_result) ReadProcessMemory (h, p, buf, len, &outlen);
putresult (L"ReadProcessMemory data", res, s, GDB_READPROCESSMEMORY,
buf, (gdb_wince_len) outlen);
}
static void
write_process_memory (int s)
{
HANDLE h = gethandle (L"WriteProcessMemory handle", s, GDB_WRITEPROCESSMEMORY);
LPVOID p = getpvoid (L"WriteProcessMemory base", s, GDB_WRITEPROCESSMEMORY);
gdb_wince_len len;
LPVOID buf = getmemory (L"WriteProcessMemory buf", s, GDB_WRITEPROCESSMEMORY, &len);
DWORD outlen;
gdb_wince_result res;
outlen = 0;
res = WriteProcessMemory (h, p, buf, (DWORD) len, &outlen);
putresult (L"WriteProcessMemory data", res, s, GDB_WRITEPROCESSMEMORY,
(gdb_wince_len *) & outlen, sizeof (gdb_wince_len));
}
static void
thread_alive (int s)
{
HANDLE h = gethandle (L"ThreadAlive handle", s, GDB_THREADALIVE);
gdb_wince_result res;
res = WaitForSingleObject (h, 0) == WAIT_OBJECT_0 ? 1 : 0;
putresult (L"WriteProcessMemory data", res, s, GDB_THREADALIVE,
&res, sizeof (res));
}
static void
suspend_thread (int s)
{
DWORD res;
HANDLE h = gethandle (L"SuspendThread handle", s, GDB_SUSPENDTHREAD);
res = SuspendThread (h);
putdword (L"SuspendThread result", s, GDB_SUSPENDTHREAD, res);
}
static void
resume_thread (int s)
{
DWORD res;
HANDLE h = gethandle (L"ResumeThread handle", s, GDB_RESUMETHREAD);
res = ResumeThread (h);
putdword (L"ResumeThread result", s, GDB_RESUMETHREAD, res);
}
static void
continue_debug_event (int s)
{
gdb_wince_result res;
DWORD pid = getdword (L"ContinueDebugEvent pid", s, GDB_CONTINUEDEBUGEVENT);
DWORD tid = getdword (L"ContinueDebugEvent tid", s, GDB_CONTINUEDEBUGEVENT);
DWORD status = getdword (L"ContinueDebugEvent status", s, GDB_CONTINUEDEBUGEVENT);
res = (gdb_wince_result) ContinueDebugEvent (pid, tid, status);
putresult (L"ContinueDebugEvent result", res, s, GDB_CONTINUEDEBUGEVENT, &res, sizeof (res));
}
static void
close_handle (int s)
{
gdb_wince_result res;
HANDLE h = gethandle (L"CloseHandle handle", s, GDB_CLOSEHANDLE);
res = (gdb_wince_result) CloseHandle (h);
putresult (L"CloseHandle result", res, s, GDB_CLOSEHANDLE, &res, sizeof (res));
}
static void
dispatch (int s)
{
gdb_wince_id id;
while (sockread (L"Dispatch", s, &id, sizeof (id)) > 0)
{
skip_next_id = 1;
switch (id)
{
case GDB_CREATEPROCESS:
create_process (s);
break;
case GDB_TERMINATEPROCESS:
terminate_process (s);
break;
case GDB_WAITFORDEBUGEVENT:
wait_for_debug_event (s);
break;
case GDB_GETTHREADCONTEXT:
get_thread_context (s);
break;
case GDB_SETTHREADCONTEXT:
set_thread_context (s);
break;
case GDB_READPROCESSMEMORY:
read_process_memory (s);
break;
case GDB_WRITEPROCESSMEMORY:
write_process_memory (s);
break;
case GDB_THREADALIVE:
thread_alive (s);
break;
case GDB_SUSPENDTHREAD:
suspend_thread (s);
break;
case GDB_RESUMETHREAD:
resume_thread (s);
break;
case GDB_CONTINUEDEBUGEVENT:
continue_debug_event (s);
break;
case GDB_CLOSEHANDLE:
close_handle (s);
break;
case GDB_STOPSTUB:
terminate_process (s);
return;
case GDB_SINGLESTEP:
flag_single_step (s);
break;
default:
{
WCHAR buf[80];
wsprintfW (buf, L"Invalid command id received: %d", id);
MessageBoxW (NULL, buf, L"GDB", MB_ICONERROR);
skip_next_id = 0;
}
}
}
}
int WINAPI
WinMain (HINSTANCE hi, HINSTANCE hp, LPWSTR cmd, int show)
{
struct hostent *h;
int s;
struct WSAData wd;
struct sockaddr_in sin;
int tmp;
LPWSTR whost;
char host[80];
whost = wcschr (cmd, L' ');
if (whost)
{
while (*whost == L' ' || *whost == L'\t')
whost++;
wcstombs (host, whost, 80);
}
if (WSAStartup (MAKEWORD (1, 1), &wd))
stub_error (L"Couldn't initialize WINSOCK.");
if (whost && *whost && (h = gethostbyname (host)) != NULL)
;
else if ((h = gethostbyname ("ppp_peer")) == NULL)
stub_error (L"Couldn't get IP address of host system. Error %d", WSAGetLastError ());
if ((s = socket (AF_INET, SOCK_STREAM, 0)) < 0)
stub_error (L"Couldn't connect to host system. Error %d", WSAGetLastError ());
tmp = 1;
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp, sizeof (tmp));
memset (&sin, 0, sizeof (sin));
sin.sin_family = h->h_addrtype;
memcpy (&sin.sin_addr, h->h_addr, h->h_length);
sin.sin_port = htons (7000);
if (connect (s, (struct sockaddr *) &sin, sizeof (sin)) < 0)
stub_error (L"Couldn't connect to host gdb.");
dispatch (s);
WSACleanup ();
return 0;
}