1 /* wince-stub.c -- debugging stub for a Windows CE device
3 Copyright 1999, 2000 Free Software Foundation, Inc.
4 Contributed by Cygnus Solutions, A Red Hat Company.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without eve nthe implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.
24 /* by Christopher Faylor (cgf@cygnus.com) */
29 #include "wince-stub.h"
31 #define MALLOC(n) (void *) LocalAlloc (LMEM_MOVEABLE | LMEM_ZEROINIT, (UINT)(n))
32 #define REALLOC(s, n) (void *) LocalReAlloc ((HLOCAL)(s), (UINT)(n), LMEM_MOVEABLE)
33 #define FREE(s) LocalFree ((HLOCAL)(s))
35 static int skip_next_id = 0; /* Don't read next API code from socket */
37 /* v-style interface for handling varying argument list error messages.
38 Displays the error message in a dialog box and exits when user clicks
41 vstub_error (LPCWSTR fmt, va_list args)
44 wvsprintfW (buf, fmt, args);
46 MessageBoxW (NULL, buf, L"GDB", MB_ICONERROR);
51 /* The standard way to display an error message and exit. */
53 stub_error (LPCWSTR fmt, ...)
57 vstub_error (fmt, args);
60 /* Allocate a limited pool of memory, reallocating over unused
61 buffers. This assumes that there will never be more than four
62 "buffers" required which, so far, is a safe assumption. */
64 mempool (unsigned int len)
67 static LPWSTR outs[4] = {NULL, NULL, NULL, NULL};
69 if (++outn >= (sizeof (outs) / sizeof (outs[0])))
72 /* Allocate space for the converted string, reusing any previously allocated
73 space, if applicable. */
76 outs[outn] = (LPWSTR) MALLOC (len);
81 /* Standard "oh well" can't communicate error. Someday this might attempt
84 attempt_resync (LPCWSTR huh, int s)
86 stub_error (L"lost synchronization with host attempting %s. Error %d", huh, WSAGetLastError ());
89 /* Read arbitrary stuff from a socket. */
91 sockread (LPCWSTR huh, int s, void *str, size_t n)
95 if (recv (s, str, n, 0) == (int) n)
97 attempt_resync (huh, s);
101 /* Write arbitrary stuff to a socket. */
103 sockwrite (LPCWSTR huh, int s, const void *str, size_t n)
107 if (send (s, str, n, 0) == (int) n)
109 attempt_resync (huh, s);
113 /* Get a an ID (possibly) and a DWORD from the host gdb.
114 Don't bother with the id if the main loop has already
117 getdword (LPCWSTR huh, int s, gdb_wince_id what_this)
126 if (sockread (huh, s, &what, sizeof (what)) != sizeof (what))
127 stub_error (L"error getting record type from host - %s.", huh);
128 while (what_this != what);
130 if (sockread (huh, s, &n, sizeof (n)) != sizeof (n))
131 stub_error (L"error getting %s from host.", huh);
136 /* Get a an ID (possibly) and a WORD from the host gdb.
137 Don't bother with the id if the main loop has already
140 getword (LPCWSTR huh, int s, gdb_wince_id what_this)
149 if (sockread (huh, s, &what, sizeof (what)) != sizeof (what))
150 stub_error (L"error getting record type from host - %s.", huh);
151 while (what_this != what);
153 if (sockread (huh, s, &n, sizeof (n)) != sizeof (n))
154 stub_error (L"error getting %s from host.", huh);
159 /* Handy defines for getting various types of values. */
160 #define gethandle(huh, s, what) (HANDLE) getdword ((huh), (s), (what))
161 #define getpvoid(huh, s, what) (LPVOID) getdword ((huh), (s), (what))
162 #define getlen(huh, s, what) (gdb_wince_len) getword ((huh), (s), (what))
164 /* Get an arbitrary block of memory from the gdb host. This comes in
165 two chunks an id/dword representing the length and the stream of memory
166 itself. Returns a pointer, allocated via mempool, to a memory buffer. */
168 getmemory (LPCWSTR huh, int s, gdb_wince_id what, gdb_wince_len *inlen)
176 *inlen = getlen (huh, s, what);
178 p = mempool ((unsigned int) *inlen); /* FIXME: check for error */
180 if ((gdb_wince_len) sockread (huh, s, p, *inlen) != *inlen)
181 stub_error (L"error getting string from host.");
186 /* Output an id/dword to the host */
188 putdword (LPCWSTR huh, int s, gdb_wince_id what, DWORD n)
190 if (sockwrite (huh, s, &what, sizeof (what)) != sizeof (what))
191 stub_error (L"error writing record id for %s to host.", huh);
192 if (sockwrite (huh, s, &n, sizeof (n)) != sizeof (n))
193 stub_error (L"error writing %s to host.", huh);
196 /* Output an id/word to the host */
198 putword (LPCWSTR huh, int s, gdb_wince_id what, WORD n)
200 if (sockwrite (huh, s, &what, sizeof (what)) != sizeof (what))
201 stub_error (L"error writing record id for %s to host.", huh);
202 if (sockwrite (huh, s, &n, sizeof (n)) != sizeof (n))
203 stub_error (L"error writing %s to host.", huh);
206 /* Convenience define for outputting a "gdb_wince_len" type. */
207 #define putlen(huh, s, what, n) putword ((huh), (s), (what), (gdb_wince_len) (n))
209 /* Put an arbitrary block of memory to the gdb host. This comes in
210 two chunks an id/dword representing the length and the stream of memory
213 putmemory (LPCWSTR huh, int s, gdb_wince_id what, const void *mem, gdb_wince_len len)
215 putlen (huh, s, what, len);
216 if (((short) len > 0) && (gdb_wince_len) sockwrite (huh, s, mem, len) != len)
217 stub_error (L"error writing memory to host.");
220 /* Output the result of an operation to the host. If res != 0, sends a block of
221 memory starting at mem of len bytes. If res == 0, sends -GetLastError () and
222 avoids sending the mem. */
224 putresult (LPCWSTR huh, gdb_wince_result res, int s, gdb_wince_id what, const void *mem, gdb_wince_len len)
227 len = -(int) GetLastError ();
228 putmemory (huh, s, what, mem, len);
231 static HANDLE curproc; /* Currently unused, but nice for debugging */
233 /* Emulate CreateProcess. Returns &pi if no error. */
235 create_process (int s)
237 LPWSTR exec_file = getmemory (L"CreateProcess exec_file", s, GDB_CREATEPROCESS, NULL);
238 LPWSTR args = getmemory (L"CreateProcess args", s, GDB_CREATEPROCESS, NULL);
239 DWORD flags = getdword (L"CreateProcess flags", s, GDB_CREATEPROCESS);
240 PROCESS_INFORMATION pi;
241 gdb_wince_result res;
243 res = CreateProcessW (exec_file,
244 args, /* command line */
247 FALSE, /* inherit handles */
248 flags, /* start flags */
250 NULL, /* current directory */
253 putresult (L"CreateProcess", res, s, GDB_CREATEPROCESS, &pi, sizeof (pi));
254 curproc = pi.hProcess;
257 /* Emulate TerminateProcess. Returns return value of TerminateProcess if
259 *** NOTE: For some unknown reason, TerminateProcess seems to always return
260 an ACCESS_DENIED (on Windows CE???) error. So, force a TRUE value for now. */
262 terminate_process (int s)
264 gdb_wince_result res;
265 HANDLE h = gethandle (L"TerminateProcess handle", s, GDB_TERMINATEPROCESS);
267 res = TerminateProcess (h, 0) || 1; /* Doesn't seem to work on SH so default to TRUE */
268 putresult (L"Terminate process result", res, s, GDB_TERMINATEPROCESS,
272 static int stepped = 0;
273 /* Handle single step instruction. FIXME: unneded? */
275 flag_single_step (int s)
287 {L"Undefined Instruction:", 1},
293 skip_message (DEBUG_EVENT *ev)
298 int nbytes = ev->u.DebugString.nDebugStringLength;
300 if (nbytes > sizeof(s))
303 memset (s, 0, sizeof (s));
304 if (!ReadProcessMemory (curproc, ev->u.DebugString.lpDebugStringData,
308 for (skp = skippy; skp->s != NULL; skp++)
309 if (wcsncmp ((wchar_t *) s, skp->s, wcslen (skp->s)) == 0)
315 /* Emulate WaitForDebugEvent. Returns the debug event on success. */
317 wait_for_debug_event (int s)
319 DWORD ms = getdword (L"WaitForDebugEvent ms", s, GDB_WAITFORDEBUGEVENT);
320 gdb_wince_result res;
322 static int skip_next = 0;
326 res = WaitForDebugEvent (&ev, ms);
328 if (ev.dwDebugEventCode == OUTPUT_DEBUG_STRING_EVENT)
335 if (skip_next = skip_message (&ev))
339 putresult (L"WaitForDebugEvent event", res, s, GDB_WAITFORDEBUGEVENT,
344 ContinueDebugEvent (ev.dwProcessId, ev.dwThreadId, DBG_CONTINUE);
350 /* Emulate GetThreadContext. Returns CONTEXT structure on success. */
352 get_thread_context (int s)
355 HANDLE h = gethandle (L"GetThreadContext handle", s, GDB_GETTHREADCONTEXT);
356 gdb_wince_result res;
358 memset (&c, 0, sizeof (c));
359 c.ContextFlags = getdword (L"GetThreadContext flags", s, GDB_GETTHREADCONTEXT);
361 res = (gdb_wince_result) GetThreadContext (h, &c);
362 putresult (L"GetThreadContext data", res, s, GDB_GETTHREADCONTEXT,
366 /* Emulate GetThreadContext. Returns success of SetThreadContext. */
368 set_thread_context (int s)
370 gdb_wince_result res;
371 HANDLE h = gethandle (L"SetThreadContext handle", s, GDB_SETTHREADCONTEXT);
372 LPCONTEXT pc = (LPCONTEXT) getmemory (L"SetThreadContext context", s,
373 GDB_SETTHREADCONTEXT, NULL);
375 res = SetThreadContext (h, pc);
376 putresult (L"SetThreadContext result", res, s, GDB_SETTHREADCONTEXT,
380 /* Emulate ReadProcessMemory. Returns memory read on success. */
382 read_process_memory (int s)
384 HANDLE h = gethandle (L"ReadProcessMemory handle", s, GDB_READPROCESSMEMORY);
385 LPVOID p = getpvoid (L"ReadProcessMemory base", s, GDB_READPROCESSMEMORY);
386 gdb_wince_len len = getlen (L"ReadProcessMemory size", s, GDB_READPROCESSMEMORY);
387 LPVOID buf = mempool ((unsigned int) len);
389 gdb_wince_result res;
392 res = (gdb_wince_result) ReadProcessMemory (h, p, buf, len, &outlen);
393 putresult (L"ReadProcessMemory data", res, s, GDB_READPROCESSMEMORY,
394 buf, (gdb_wince_len) outlen);
397 /* Emulate WriteProcessMemory. Returns WriteProcessMemory success. */
399 write_process_memory (int s)
401 HANDLE h = gethandle (L"WriteProcessMemory handle", s, GDB_WRITEPROCESSMEMORY);
402 LPVOID p = getpvoid (L"WriteProcessMemory base", s, GDB_WRITEPROCESSMEMORY);
404 LPVOID buf = getmemory (L"WriteProcessMemory buf", s, GDB_WRITEPROCESSMEMORY, &len);
406 gdb_wince_result res;
409 res = WriteProcessMemory (h, p, buf, (DWORD) len, &outlen);
410 putresult (L"WriteProcessMemory data", res, s, GDB_WRITEPROCESSMEMORY,
411 (gdb_wince_len *) & outlen, sizeof (gdb_wince_len));
414 /* Return non-zero to gdb host if given thread is alive. */
418 HANDLE h = gethandle (L"ThreadAlive handle", s, GDB_THREADALIVE);
419 gdb_wince_result res;
421 res = WaitForSingleObject (h, 0) == WAIT_OBJECT_0 ? 1 : 0;
422 putresult (L"WriteProcessMemory data", res, s, GDB_THREADALIVE,
426 /* Emulate SuspendThread. Returns value returned from SuspendThread. */
428 suspend_thread (int s)
431 HANDLE h = gethandle (L"SuspendThread handle", s, GDB_SUSPENDTHREAD);
432 res = SuspendThread (h);
433 putdword (L"SuspendThread result", s, GDB_SUSPENDTHREAD, res);
436 /* Emulate ResumeThread. Returns value returned from ResumeThread. */
438 resume_thread (int s)
441 HANDLE h = gethandle (L"ResumeThread handle", s, GDB_RESUMETHREAD);
442 res = ResumeThread (h);
443 putdword (L"ResumeThread result", s, GDB_RESUMETHREAD, res);
446 /* Emulate ContinueDebugEvent. Returns ContinueDebugEvent success. */
448 continue_debug_event (int s)
450 gdb_wince_result res;
451 DWORD pid = getdword (L"ContinueDebugEvent pid", s, GDB_CONTINUEDEBUGEVENT);
452 DWORD tid = getdword (L"ContinueDebugEvent tid", s, GDB_CONTINUEDEBUGEVENT);
453 DWORD status = getdword (L"ContinueDebugEvent status", s, GDB_CONTINUEDEBUGEVENT);
454 res = (gdb_wince_result) ContinueDebugEvent (pid, tid, status);
455 putresult (L"ContinueDebugEvent result", res, s, GDB_CONTINUEDEBUGEVENT, &res, sizeof (res));
458 /* Emulate CloseHandle. Returns CloseHandle success. */
462 gdb_wince_result res;
463 HANDLE h = gethandle (L"CloseHandle handle", s, GDB_CLOSEHANDLE);
464 res = (gdb_wince_result) CloseHandle (h);
465 putresult (L"CloseHandle result", res, s, GDB_CLOSEHANDLE, &res, sizeof (res));
468 /* Main loop for reading requests from gdb host on the socket. */
474 /* Continue reading from socket until receive a GDB_STOPSUB. */
475 while (sockread (L"Dispatch", s, &id, sizeof (id)) > 0)
480 case GDB_CREATEPROCESS:
483 case GDB_TERMINATEPROCESS:
484 terminate_process (s);
486 case GDB_WAITFORDEBUGEVENT:
487 wait_for_debug_event (s);
489 case GDB_GETTHREADCONTEXT:
490 get_thread_context (s);
492 case GDB_SETTHREADCONTEXT:
493 set_thread_context (s);
495 case GDB_READPROCESSMEMORY:
496 read_process_memory (s);
498 case GDB_WRITEPROCESSMEMORY:
499 write_process_memory (s);
501 case GDB_THREADALIVE:
504 case GDB_SUSPENDTHREAD:
507 case GDB_RESUMETHREAD:
510 case GDB_CONTINUEDEBUGEVENT:
511 continue_debug_event (s);
513 case GDB_CLOSEHANDLE:
517 terminate_process (s);
520 flag_single_step (s);
525 wsprintfW (buf, L"Invalid command id received: %d", id);
526 MessageBoxW (NULL, buf, L"GDB", MB_ICONERROR);
533 /* The Windows Main entry point */
535 WinMain (HINSTANCE hi, HINSTANCE hp, LPWSTR cmd, int show)
540 struct sockaddr_in sin;
545 whost = wcschr (cmd, L' '); /* Look for argument. */
547 /* If no host is specified, just use default */
550 /* Eat any spaces. */
551 while (*whost == L' ' || *whost == L'\t')
554 wcstombs (host, whost, 80); /* Convert from UNICODE to ascii */
557 /* Winsock initialization. */
558 if (WSAStartup (MAKEWORD (1, 1), &wd))
559 stub_error (L"Couldn't initialize WINSOCK.");
561 /* If whost was specified, first try it. If it was not specified or the
562 host lookup failed, try the Windows CE magic ppp_peer lookup. ppp_peer
563 is supposed to be the Windows host sitting on the other end of the
565 if (whost && *whost && (h = gethostbyname (host)) != NULL)
566 /* nothing to do */ ;
567 else if ((h = gethostbyname ("ppp_peer")) == NULL)
568 stub_error (L"Couldn't get IP address of host system. Error %d", WSAGetLastError ());
571 if ((s = socket (AF_INET, SOCK_STREAM, 0)) < 0)
572 stub_error (L"Couldn't connect to host system. Error %d", WSAGetLastError ());
574 /* Allow rapid reuse of the port. */
576 setsockopt (s, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp, sizeof (tmp));
578 /* Set up the information for connecting to the host gdb process. */
579 memset (&sin, 0, sizeof (sin));
580 sin.sin_family = h->h_addrtype;
581 memcpy (&sin.sin_addr, h->h_addr, h->h_length);
582 sin.sin_port = htons (7000); /* FIXME: This should be configurable */
584 /* Connect to host */
585 if (connect (s, (struct sockaddr *) &sin, sizeof (sin)) < 0)
586 stub_error (L"Couldn't connect to host gdb.");
588 /* Read from socket until told to exit. */