binutils-gdb/gdb/wince-stub.c

593 lines
17 KiB
C
Raw Normal View History

2000-02-25 05:39:15 +08:00
/* wince-stub.c -- debugging stub for a Windows CE device
2007-01-10 01:59:20 +08:00
Copyright (C) 1999, 2000, 2007 Free Software Foundation, Inc.
2000-02-25 05:39:15 +08:00
Contributed by Cygnus Solutions, A Red Hat Company.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without eve nthe implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
* breakpoint.c: * arm-tdep.c: * ia64-tdep.c: * i386-tdep.c: * hpread.c: * hppa-tdep.c: * hppa-hpux-tdep.c: * gnu-nat.c: * gdbtypes.c: * gdbarch.h: * gdbarch.c: * eval.c: * dwarf2read.c: * dbxread.c: * copying: * symfile.c: * stabsread.c: * sh64-tdep.c: * sh-tdep.c: * s390-tdep.c: * rs6000-tdep.c: * remote.c: * remote-mips.c: * mips-tdep.c: * mdebugread.c: * linux-nat.c: * infrun.c: * xcoffread.c: * win32-nat.c: * valops.c: * utils.c: * tracepoint.c: * target.c: * symtab.c: * c-exp.y: * ada-valprint.c: * ada-typeprint.c: * ada-lex.l: * ada-lang.h: * ada-lang.c: * ada-exp.y: * alphafbsd-tdep.c: * alphabsd-tdep.h: * alphabsd-tdep.c: * alphabsd-nat.c: * alpha-tdep.h: * alpha-tdep.c: * alpha-osf1-tdep.c: * alpha-nat.c: * alpha-mdebug-tdep.c: * alpha-linux-tdep.c: * alpha-linux-nat.c: * aix-thread.c: * abug-rom.c: * arch-utils.c: * annotate.h: * annotate.c: * amd64obsd-tdep.c: * amd64obsd-nat.c: * amd64nbsd-tdep.c: * amd64nbsd-nat.c: * amd64fbsd-tdep.c: * amd64fbsd-nat.c: * amd64bsd-nat.c: * amd64-tdep.h: * amd64-tdep.c: * amd64-sol2-tdep.c: * amd64-nat.h: * amd64-nat.c: * amd64-linux-tdep.c: * amd64-linux-nat.c: * alphanbsd-tdep.c: * block.h: * block.c: * bfd-target.h: * bfd-target.c: * bcache.h: * bcache.c: * ax.h: * ax-general.c: * ax-gdb.h: * ax-gdb.c: * avr-tdep.c: * auxv.h: * auxv.c: * armnbsd-tdep.c: * armnbsd-nat.c: * arm-tdep.h: * arm-linux-nat.c: * arch-utils.h: * charset.c: * call-cmds.h: * c-valprint.c: * c-typeprint.c: * c-lang.h: * c-lang.c: * buildsym.h: * buildsym.c: * bsd-uthread.h: * bsd-uthread.c: * bsd-kvm.h: * bsd-kvm.c: * breakpoint.h: * core-regset.c: * core-aout.c: * completer.h: * completer.c: * complaints.h: * complaints.c: * command.h: * coffread.c: * coff-solib.h: * coff-solib.c: * coff-pe-read.h: * coff-pe-read.c: * cli-out.h: * cli-out.c: * charset.h: * dink32-rom.c: * dictionary.h: * dictionary.c: * demangle.c: * defs.h: * dcache.h: * dcache.c: * d10v-tdep.c: * cpu32bug-rom.c: * cp-valprint.c: * cp-support.h: * cp-support.c: * cp-namespace.c: * cp-abi.h: * cp-abi.c: * corelow.c: * corefile.c: * environ.c: * elfread.c: * dwarfread.c: * dwarf2loc.c: * dwarf2expr.h: * dwarf2expr.c: * dwarf2-frame.h: * dwarf2-frame.c: * dve3900-rom.c: * dummy-frame.h: * dummy-frame.c: * dsrec.c: * doublest.h: * doublest.c: * disasm.h: * disasm.c: * fork-child.c: * findvar.c: * fbsd-nat.h: * fbsd-nat.c: * f-valprint.c: * f-typeprint.c: * f-lang.h: * f-lang.c: * expression.h: * expprint.c: * exec.h: * exec.c: * exceptions.h: * exceptions.c: * event-top.h: * event-top.c: * event-loop.h: * event-loop.c: * gdb.c: * gdb-stabs.h: * gdb-events.h: * gdb-events.c: * gcore.c: * frv-tdep.h: * frv-tdep.c: * frv-linux-tdep.c: * frame.h: * frame.c: * frame-unwind.h: * frame-unwind.c: * frame-base.h: * frame-base.c: * gdb_vfork.h: * gdb_thread_db.h: * gdb_string.h: * gdb_stat.h: * gdb_regex.h: * gdb_ptrace.h: * gdb_proc_service.h: * gdb_obstack.h: * gdb_locale.h: * gdb_dirent.h: * gdb_curses.h: * gdb_assert.h: * gdbarch.sh: * gdb.h: * hpux-thread.c: * hppabsd-nat.c: * hppa-tdep.h: * hpacc-abi.c: * h8300-tdep.c: * gregset.h: * go32-nat.c: * gnu-v3-abi.c: * gnu-v2-abi.h: * gnu-v2-abi.c: * gnu-nat.h: * glibc-tdep.c: * gdbtypes.h: * gdbcore.h: * gdbcmd.h: * i386nbsd-tdep.c: * i386nbsd-nat.c: * i386gnu-tdep.c: * i386gnu-nat.c: * i386fbsd-tdep.c: * i386fbsd-nat.c: * i386bsd-tdep.c: * i386bsd-nat.h: * i386bsd-nat.c: * i386-tdep.h: * i386-sol2-nat.c: * i386-nto-tdep.c: * i386-nat.c: * i386-linux-tdep.h: * i386-linux-tdep.c: * i386-linux-nat.c: * i386-cygwin-tdep.c: * inf-ttrace.c: * inf-ptrace.h: * inf-ptrace.c: * inf-loop.h: * inf-loop.c: * inf-child.h: * inf-child.c: * ia64-tdep.h: * ia64-linux-nat.c: * i387-tdep.h: * i387-tdep.c: * i386v4-nat.c: * i386v-nat.c: * i386obsd-tdep.c: * i386obsd-nat.c: * kod.c: * jv-valprint.c: * jv-typeprint.c: * jv-lang.h: * jv-lang.c: * irix5-nat.c: * iq2000-tdep.c: * interps.h: * interps.c: * inftarg.c: * inflow.h: * inflow.c: * inferior.h: * infcmd.c: * infcall.h: * infcall.c: * inf-ttrace.h: * m32r-tdep.h: * m32r-tdep.c: * m32r-rom.c: * m32r-linux-tdep.c: * m32r-linux-nat.c: * m2-valprint.c: * m2-typeprint.c: * m2-lang.h: * m2-lang.c: * lynx-nat.c: * linux-thread-db.c: * linux-nat.h: * linespec.c: * libunwind-frame.h: * libunwind-frame.c: * language.h: * language.c: * macroexp.c: * macrocmd.c: * m88kbsd-nat.c: * m88k-tdep.h: * m88k-tdep.c: * m68klinux-tdep.c: * m68klinux-nat.c: * m68kbsd-tdep.c: * m68kbsd-nat.c: * m68k-tdep.h: * m68k-tdep.c: * mips-linux-nat.c: * mips-irix-tdep.c: * minsyms.c: * memattr.h: * memattr.c: * mem-break.c: * mdebugread.h: * main.h: * main.c: * macrotab.h: * macrotab.c: * macroscope.h: * macroscope.c: * macroexp.h: * nbsd-tdep.c: * mt-tdep.c: * monitor.h: * monitor.c: * mn10300-tdep.h: * mn10300-tdep.c: * mn10300-linux-tdep.c: * mipsv4-nat.c: * mipsread.c: * mipsnbsd-tdep.h: * mipsnbsd-tdep.c: * mipsnbsd-nat.c: * mips64obsd-tdep.c: * mips64obsd-nat.c: * mips-tdep.h: * mips-mdebug-tdep.c: * mips-linux-tdep.c: * osabi.h: * osabi.c: * ocd.h: * ocd.c: * observer.c: * objfiles.h: * objfiles.c: * objc-lang.h: * objc-lang.c: * objc-exp.y: * nto-tdep.h: * nto-tdep.c: * nto-procfs.c: * nlmread.c: * nbsd-tdep.h: * ppcobsd-tdep.c: * ppcobsd-nat.c: * ppcnbsd-tdep.h: * ppcnbsd-tdep.c: * ppcnbsd-nat.c: * ppcbug-rom.c: * ppc-tdep.h: * ppc-sysv-tdep.c: * ppc-linux-tdep.c: * ppc-linux-nat.c: * ppc-bdm.c: * parser-defs.h: * parse.c: * p-valprint.c: * p-typeprint.c: * p-lang.h: * p-lang.c: * remote-fileio.h: * remote-fileio.c: * remote-est.c: * remote-e7000.c: * regset.h: * regset.c: * reggroups.h: * reggroups.c: * regcache.h: * regcache.c: * proc-why.c: * proc-service.c: * proc-events.c: * printcmd.c: * ppcobsd-tdep.h: * sentinel-frame.h: * sentinel-frame.c: * scm-valprint.c: * scm-tags.h: * scm-lang.h: * scm-lang.c: * scm-exp.c: * s390-tdep.h: * rom68k-rom.c: * remote.h: * remote-utils.c: * remote-st.c: * remote-sim.c: * remote-sds.c: * remote-rdp.c: * remote-rdi.c: * remote-hms.c: * sim-regno.h: * shnbsd-tdep.h: * shnbsd-tdep.c: * shnbsd-nat.c: * sh-tdep.h: * serial.h: * serial.c: * ser-unix.h: * ser-unix.c: * ser-tcp.c: * ser-pipe.c: * ser-go32.c: * ser-e7kpc.c: * ser-base.h: * ser-base.c: * solib.c: * solib-svr4.h: * solib-svr4.c: * solib-sunos.c: * solib-som.h: * solib-som.c: * solib-pa64.h: * solib-pa64.c: * solib-osf.c: * solib-null.c: * solib-legacy.c: * solib-irix.c: * solib-frv.c: * solib-aix5.c: * sol-thread.c: * sparc64-linux-tdep.c: * sparc64-linux-nat.c: * sparc-tdep.h: * sparc-tdep.c: * sparc-sol2-tdep.c: * sparc-sol2-nat.c: * sparc-nat.h: * sparc-nat.c: * sparc-linux-tdep.c: * sparc-linux-nat.c: * source.h: * source.c: * somread.c: * solist.h: * solib.h: * std-regs.c: * stack.h: * stack.c: * stabsread.h: * sparcobsd-tdep.c: * sparcnbsd-tdep.c: * sparcnbsd-nat.c: * sparc64obsd-tdep.c: * sparc64nbsd-tdep.c: * sparc64nbsd-nat.c: * sparc64fbsd-tdep.c: * sparc64fbsd-nat.c: * sparc64-tdep.h: * sparc64-tdep.c: * sparc64-sol2-tdep.c: * sparc64-nat.c: * ui-file.c: * typeprint.h: * typeprint.c: * tramp-frame.h: * tramp-frame.c: * trad-frame.h: * trad-frame.c: * tracepoint.h: * top.c: * tobs.inc: * thread.c: * terminal.h: * target.h: * symfile.h: * stop-gdb.c: * vaxbsd-nat.c: * vax-tdep.h: * vax-tdep.c: * vax-nat.c: * varobj.h: * varobj.c: * value.h: * value.c: * valprint.h: * valprint.c: * v850-tdep.c: * uw-thread.c: * user-regs.c: * ui-out.h: * ui-out.c: * ui-file.h: * xcoffsolib.h: * xcoffsolib.c: * wrapper.c: * wince.c: * wince-stub.h: * wince-stub.c: * vaxobsd-tdep.c: * vaxnbsd-tdep.c: * gdb_gcore.sh: * copying.c: * configure.ac: * aclocal.m4: * acinclude.m4: * reply_mig_hack.awk: * observer.sh: * gdb_mbuild.sh: * arm-linux-tdep.c: * blockframe.c: * dbug-rom.c: * environ.h: * dwarf2loc.h: * gdb-events.sh: * glibc-tdep.h: * gdb_wait.h: * gdbthread.h: * i386-sol2-tdep.c: * hppabsd-tdep.c: * hppa-linux-nat.c: * hppa-hpux-nat.c: * ia64-linux-tdep.c: * infptrace.c: * linespec.h: * maint.c: * mips-mdebug-tdep.h: * remote-m32r-sdi.c: * s390-nat.c: * rs6000-nat.c: * remote-utils.h: * sh3-rom.c: * sh-linux-tdep.c: * top.h: * symtab.h: * symmisc.c: * symfile-mem.c: * srec.h: * user-regs.h: * version.h: * valarith.c: * xstormy16-tdep.c: * wrapper.h: * Makefile.in: * f-exp.y: * cris-tdep.c: * cp-name-parser.y: * procfs.c: * proc-utils.h: * proc-flags.c: * proc-api.c: * p-exp.y: * m68hc11-tdep.c: * m2-exp.y: * kod.h: * kod-cisco.c: * jv-exp.y: * hppa-linux-tdep.c: Add (c) after Copyright. Update the FSF address.
2005-12-18 06:34:03 +08:00
Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
2000-02-25 05:39:15 +08:00
*/
/* by Christopher Faylor (cgf@cygnus.com) */
#include <stdarg.h>
#include <windows.h>
#include <winsock.h>
#include "wince-stub.h"
#define MALLOC(n) (void *) LocalAlloc (LMEM_MOVEABLE | LMEM_ZEROINIT, (UINT)(n))
2000-02-25 05:39:15 +08:00
#define REALLOC(s, n) (void *) LocalReAlloc ((HLOCAL)(s), (UINT)(n), LMEM_MOVEABLE)
#define FREE(s) LocalFree ((HLOCAL)(s))
2000-02-25 05:39:15 +08:00
static int skip_next_id = 0; /* Don't read next API code from socket */
/* v-style interface for handling varying argument list error messages.
2000-02-25 05:39:15 +08:00
Displays the error message in a dialog box and exits when user clicks
on OK. */
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);
}
/* The standard way to display an error message and exit. */
static void
stub_error (LPCWSTR fmt, ...)
{
va_list args;
va_start (args, fmt);
vstub_error (fmt, args);
}
/* Allocate a limited pool of memory, reallocating over unused
buffers. This assumes that there will never be more than four
"buffers" required which, so far, is a safe assumption. */
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;
/* Allocate space for the converted string, reusing any previously allocated
space, if applicable. */
if (outs[outn])
FREE (outs[outn]);
outs[outn] = (LPWSTR) MALLOC (len);
return outs[outn];
}
2000-02-25 05:39:15 +08:00
/* Standard "oh well" can't communicate error. Someday this might attempt
synchronization. */
static void
attempt_resync (LPCWSTR huh, int s)
{
stub_error (L"lost synchronization with host attempting %s. Error %d", huh, WSAGetLastError ());
}
/* Read arbitrary stuff from a socket. */
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);
}
}
/* Write arbitrary stuff to a socket. */
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);
}
}
/* Get a an ID (possibly) and a DWORD from the host gdb.
Don't bother with the id if the main loop has already
read it. */
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;
}
/* Get a an ID (possibly) and a WORD from the host gdb.
Don't bother with the id if the main loop has already
read it. */
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;
}
/* Handy defines for getting various types of values. */
#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))
/* Get an arbitrary block of memory from the gdb host. This comes in
two chunks an id/dword representing the length and the stream of memory
itself. Returns a pointer, allocated via mempool, to a memory buffer. */
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); /* FIXME: check for error */
2000-02-25 05:39:15 +08:00
if ((gdb_wince_len) sockread (huh, s, p, *inlen) != *inlen)
stub_error (L"error getting string from host.");
return p;
}
/* Output an id/dword to the host */
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);
}
/* Output an id/word to the host */
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);
}
/* Convenience define for outputting a "gdb_wince_len" type. */
#define putlen(huh, s, what, n) putword ((huh), (s), (what), (gdb_wince_len) (n))
/* Put an arbitrary block of memory to the gdb host. This comes in
two chunks an id/dword representing the length and the stream of memory
itself. */
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.");
}
/* Output the result of an operation to the host. If res != 0, sends a block of
memory starting at mem of len bytes. If res == 0, sends -GetLastError () and
avoids sending the mem. */
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; /* Currently unused, but nice for debugging */
/* Emulate CreateProcess. Returns &pi if no error. */
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, /* command line */
NULL, /* Security */
NULL, /* thread */
FALSE, /* inherit handles */
flags, /* start flags */
NULL,
NULL, /* current directory */
NULL,
&pi);
putresult (L"CreateProcess", res, s, GDB_CREATEPROCESS, &pi, sizeof (pi));
curproc = pi.hProcess;
}
/* Emulate TerminateProcess. Returns return value of TerminateProcess if
no error.
*** NOTE: For some unknown reason, TerminateProcess seems to always return
an ACCESS_DENIED (on Windows CE???) error. So, force a TRUE value for now. */
static void
terminate_process (int s)
{
gdb_wince_result res;
HANDLE h = gethandle (L"TerminateProcess handle", s, GDB_TERMINATEPROCESS);
res = TerminateProcess (h, 0) || 1; /* Doesn't seem to work on SH so default to TRUE */
putresult (L"Terminate process result", res, s, GDB_TERMINATEPROCESS,
&res, sizeof (res));
}
static int stepped = 0;
/* Handle single step instruction. FIXME: unneded? */
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;
}
2000-02-25 05:39:15 +08:00
/* Emulate WaitForDebugEvent. Returns the debug event on success. */
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);
}
2000-02-25 05:39:15 +08:00
return;
2000-02-25 05:39:15 +08:00
}
/* Emulate GetThreadContext. Returns CONTEXT structure on success. */
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);
2000-02-25 05:39:15 +08:00
res = (gdb_wince_result) GetThreadContext (h, &c);
putresult (L"GetThreadContext data", res, s, GDB_GETTHREADCONTEXT,
&c, sizeof (c));
}
/* Emulate GetThreadContext. Returns success of SetThreadContext. */
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));
}
/* Emulate ReadProcessMemory. Returns memory read on success. */
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);
2000-02-25 05:39:15 +08:00
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);
}
/* Emulate WriteProcessMemory. Returns WriteProcessMemory success. */
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));
}
/* Return non-zero to gdb host if given thread is alive. */
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));
}
/* Emulate SuspendThread. Returns value returned from SuspendThread. */
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);
}
/* Emulate ResumeThread. Returns value returned from ResumeThread. */
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);
}
/* Emulate ContinueDebugEvent. Returns ContinueDebugEvent success. */
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));
}
/* Emulate CloseHandle. Returns CloseHandle success. */
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));
}
/* Main loop for reading requests from gdb host on the socket. */
static void
dispatch (int s)
{
gdb_wince_id id;
/* Continue reading from socket until receive a GDB_STOPSUB. */
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;
2000-02-25 05:39:15 +08:00
default:
{
WCHAR buf[80];
wsprintfW (buf, L"Invalid command id received: %d", id);
MessageBoxW (NULL, buf, L"GDB", MB_ICONERROR);
skip_next_id = 0;
}
}
}
}
/* The Windows Main entry point */
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' '); /* Look for argument. */
/* If no host is specified, just use default */
if (whost)
{
/* Eat any spaces. */
while (*whost == L' ' || *whost == L'\t')
whost++;
wcstombs (host, whost, 80); /* Convert from UNICODE to ascii */
}
/* Winsock initialization. */
if (WSAStartup (MAKEWORD (1, 1), &wd))
stub_error (L"Couldn't initialize WINSOCK.");
/* If whost was specified, first try it. If it was not specified or the
host lookup failed, try the Windows CE magic ppp_peer lookup. ppp_peer
is supposed to be the Windows host sitting on the other end of the
serial cable. */
if (whost && *whost && (h = gethostbyname (host)) != NULL)
/* nothing to do */ ;
else if ((h = gethostbyname ("ppp_peer")) == NULL)
stub_error (L"Couldn't get IP address of host system. Error %d", WSAGetLastError ());
/* Get a socket. */
if ((s = socket (AF_INET, SOCK_STREAM, 0)) < 0)
stub_error (L"Couldn't connect to host system. Error %d", WSAGetLastError ());
/* Allow rapid reuse of the port. */
tmp = 1;
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp, sizeof (tmp));
/* Set up the information for connecting to the host gdb process. */
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); /* FIXME: This should be configurable */
/* Connect to host */
if (connect (s, (struct sockaddr *) &sin, sizeof (sin)) < 0)
stub_error (L"Couldn't connect to host gdb.");
/* Read from socket until told to exit. */
dispatch (s);
WSACleanup ();
return 0;
}