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, (UINT)(n))
32 #define REALLOC(s, n) (void *) LocalReAlloc ((HLOCAL)(s), (UINT)(n), LMEM_MOVEABLE)
34 static int skip_next_id
= 0; /* Don't read next API code from socket */
36 /* v-style interface for handling varying argyment list error messages.
37 Displays the error message in a dialog box and exits when user clicks
40 vstub_error (LPCWSTR fmt
, va_list args
)
43 wvsprintfW (buf
, fmt
, args
);
45 MessageBoxW (NULL
, buf
, L
"GDB", MB_ICONERROR
);
50 /* The standard way to display an error message and exit. */
52 stub_error (LPCWSTR fmt
, ...)
56 vstub_error (fmt
, args
);
59 /* Standard "oh well" can't communicate error. Someday this might attempt
62 attempt_resync (LPCWSTR huh
, int s
)
64 stub_error (L
"lost synchronization with host attempting %s. Error %d", huh
, WSAGetLastError ());
67 /* Read arbitrary stuff from a socket. */
69 sockread (LPCWSTR huh
, int s
, void *str
, size_t n
)
73 if (recv (s
, str
, n
, 0) == (int) n
)
75 attempt_resync (huh
, s
);
79 /* Write arbitrary stuff to a socket. */
81 sockwrite (LPCWSTR huh
, int s
, const void *str
, size_t n
)
85 if (send (s
, str
, n
, 0) == (int) n
)
87 attempt_resync (huh
, s
);
91 /* Allocate a limited pool of memory, reallocating over unused
92 buffers. This assumes that there will never be more than four
93 "buffers" required which, so far, is a safe assumption. */
95 mempool (gdb_wince_len len
)
98 static LPWSTR outs
[4] = {NULL
/*, NULL, etc. */};
100 if (++n
>= (sizeof (outs
) / sizeof (outs
[0])))
103 /* Allocate space for the converted string, reusing any previously allocated
104 space, if applicable. */
106 outs
[n
] = (LPWSTR
) REALLOC (outs
[n
], len
);
108 outs
[n
] = (LPWSTR
) MALLOC (len
);
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 (*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 /* Emulate WaitForDebugEvent. Returns the debug event on success. */
274 wait_for_debug_event (int s
)
276 DWORD ms
= getdword (L
"WaitForDebugEvent ms", s
, GDB_WAITFORDEBUGEVENT
);
277 gdb_wince_result res
;
280 res
= WaitForDebugEvent (&ev
, ms
);
281 putresult (L
"WaitForDebugEvent event", res
, s
, GDB_WAITFORDEBUGEVENT
,
285 /* Emulate GetThreadContext. Returns CONTEXT structure on success. */
287 get_thread_context (int s
)
290 HANDLE h
= gethandle (L
"GetThreadContext handle", s
, GDB_GETTHREADCONTEXT
);
291 gdb_wince_result res
;
293 memset (&c
, 0, sizeof (c
));
294 c
.ContextFlags
= getdword (L
"GetThreadContext handle", s
, GDB_GETTHREADCONTEXT
);
296 res
= (gdb_wince_result
) GetThreadContext (h
, &c
);
297 putresult (L
"GetThreadContext data", res
, s
, GDB_GETTHREADCONTEXT
,
301 /* Emulate GetThreadContext. Returns success of SetThreadContext. */
303 set_thread_context (int s
)
305 gdb_wince_result res
;
306 HANDLE h
= gethandle (L
"SetThreadContext handle", s
, GDB_SETTHREADCONTEXT
);
307 LPCONTEXT pc
= (LPCONTEXT
) getmemory (L
"SetThreadContext context", s
,
308 GDB_SETTHREADCONTEXT
, NULL
);
310 res
= SetThreadContext (h
, pc
);
311 putresult (L
"SetThreadContext result", res
, s
, GDB_SETTHREADCONTEXT
,
315 /* Emulate ReadProcessMemory. Returns memory read on success. */
317 read_process_memory (int s
)
319 HANDLE h
= gethandle (L
"ReadProcessMemory handle", s
, GDB_READPROCESSMEMORY
);
320 LPVOID p
= getpvoid (L
"ReadProcessMemory base", s
, GDB_READPROCESSMEMORY
);
321 gdb_wince_len len
= getlen (L
"ReadProcessMemory size", s
, GDB_READPROCESSMEMORY
);
322 LPVOID buf
= mempool ((gdb_wince_len
) len
);
324 gdb_wince_result res
;
327 res
= (gdb_wince_result
) ReadProcessMemory (h
, p
, buf
, len
, &outlen
);
328 putresult (L
"ReadProcessMemory data", res
, s
, GDB_READPROCESSMEMORY
,
329 buf
, (gdb_wince_len
) outlen
);
332 /* Emulate WriteProcessMemory. Returns WriteProcessMemory success. */
334 write_process_memory (int s
)
336 HANDLE h
= gethandle (L
"WriteProcessMemory handle", s
, GDB_WRITEPROCESSMEMORY
);
337 LPVOID p
= getpvoid (L
"WriteProcessMemory base", s
, GDB_WRITEPROCESSMEMORY
);
339 LPVOID buf
= getmemory (L
"WriteProcessMemory buf", s
, GDB_WRITEPROCESSMEMORY
, &len
);
341 gdb_wince_result res
;
344 res
= WriteProcessMemory (h
, p
, buf
, (DWORD
) len
, &outlen
);
345 putresult (L
"WriteProcessMemory data", res
, s
, GDB_WRITEPROCESSMEMORY
,
346 (gdb_wince_len
*) & outlen
, sizeof (gdb_wince_len
));
349 /* Return non-zero to gdb host if given thread is alive. */
353 HANDLE h
= gethandle (L
"ThreadAlive handle", s
, GDB_THREADALIVE
);
354 gdb_wince_result res
;
356 res
= WaitForSingleObject (h
, 0) == WAIT_OBJECT_0
? 1 : 0;
357 putresult (L
"WriteProcessMemory data", res
, s
, GDB_THREADALIVE
,
361 /* Emulate SuspendThread. Returns value returned from SuspendThread. */
363 suspend_thread (int s
)
366 HANDLE h
= gethandle (L
"SuspendThread handle", s
, GDB_SUSPENDTHREAD
);
367 res
= SuspendThread (h
);
368 putdword (L
"SuspendThread result", s
, GDB_SUSPENDTHREAD
, res
);
371 /* Emulate ResumeThread. Returns value returned from ResumeThread. */
373 resume_thread (int s
)
376 HANDLE h
= gethandle (L
"ResumeThread handle", s
, GDB_RESUMETHREAD
);
377 res
= ResumeThread (h
);
378 putdword (L
"ResumeThread result", s
, GDB_RESUMETHREAD
, res
);
381 /* Emulate ContinueDebugEvent. Returns ContinueDebugEvent success. */
383 continue_debug_event (int s
)
385 gdb_wince_result res
;
386 DWORD pid
= getdword (L
"ContinueDebugEvent pid", s
, GDB_CONTINUEDEBUGEVENT
);
387 DWORD tid
= getdword (L
"ContinueDebugEvent tid", s
, GDB_CONTINUEDEBUGEVENT
);
388 DWORD status
= getdword (L
"ContinueDebugEvent status", s
, GDB_CONTINUEDEBUGEVENT
);
389 res
= (gdb_wince_result
) ContinueDebugEvent (pid
, tid
, status
);
390 putresult (L
"ContinueDebugEvent result", res
, s
, GDB_CONTINUEDEBUGEVENT
, &res
, sizeof (res
));
393 /* Emulate CloseHandle. Returns CloseHandle success. */
397 gdb_wince_result res
;
398 HANDLE h
= gethandle (L
"CloseHandle handle", s
, GDB_CLOSEHANDLE
);
399 res
= (gdb_wince_result
) CloseHandle (h
);
400 putresult (L
"CloseHandle result", res
, s
, GDB_CLOSEHANDLE
, &res
, sizeof (res
));
403 /* Handle single step instruction */
409 /* Main loop for reading requests from gdb host on the socket. */
415 /* Continue reading from socket until receive a GDB_STOPSUB. */
416 while (sockread (L
"Dispatch", s
, &id
, sizeof (id
)) > 0)
421 case GDB_CREATEPROCESS
:
424 case GDB_TERMINATEPROCESS
:
425 terminate_process (s
);
427 case GDB_WAITFORDEBUGEVENT
:
428 wait_for_debug_event (s
);
430 case GDB_GETTHREADCONTEXT
:
431 get_thread_context (s
);
433 case GDB_SETTHREADCONTEXT
:
434 set_thread_context (s
);
436 case GDB_READPROCESSMEMORY
:
437 read_process_memory (s
);
439 case GDB_WRITEPROCESSMEMORY
:
440 write_process_memory (s
);
442 case GDB_THREADALIVE
:
445 case GDB_SUSPENDTHREAD
:
448 case GDB_RESUMETHREAD
:
451 case GDB_CONTINUEDEBUGEVENT
:
452 continue_debug_event (s
);
454 case GDB_CLOSEHANDLE
:
458 terminate_process (s
);
466 wsprintfW (buf
, L
"Invalid command id received: %d", id
);
467 MessageBoxW (NULL
, buf
, L
"GDB", MB_ICONERROR
);
474 /* The Windows Main entry point */
476 WinMain (HINSTANCE hi
, HINSTANCE hp
, LPWSTR cmd
, int show
)
481 struct sockaddr_in sin
;
486 whost
= wcschr (cmd
, L
' '); /* Look for argument. */
488 /* If no host is specified, just use default */
491 /* Eat any spaces. */
492 while (*whost
== L
' ' || *whost
== L
'\t')
495 wcstombs (host
, whost
, 80); /* Convert from UNICODE to ascii */
498 MessageBoxW (NULL
, whost
, L
"GDB", MB_ICONERROR
);
500 /* Winsock initialization. */
501 if (WSAStartup (MAKEWORD (1, 1), &wd
))
502 stub_error (L
"Couldn't initialize WINSOCK.");
504 /* If whost was specified, first try it. If it was not specified or the
505 host lookup failed, try the Windows CE magic ppp_peer lookup. ppp_peer
506 is supposed to be the Windows host sitting on the other end of the
508 if (whost
&& *whost
&& (h
= gethostbyname (host
)) != NULL
)
509 /* nothing to do */ ;
510 else if ((h
= gethostbyname ("ppp_peer")) == NULL
)
511 stub_error (L
"Couldn't get IP address of host system. Error %d", WSAGetLastError ());
514 if ((s
= socket (AF_INET
, SOCK_STREAM
, 0)) < 0)
515 stub_error (L
"Couldn't connect to host system. Error %d", WSAGetLastError ());
517 /* Allow rapid reuse of the port. */
519 setsockopt (s
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
, sizeof (tmp
));
521 /* Set up the information for connecting to the host gdb process. */
522 memset (&sin
, 0, sizeof (sin
));
523 sin
.sin_family
= h
->h_addrtype
;
524 memcpy (&sin
.sin_addr
, h
->h_addr
, h
->h_length
);
525 sin
.sin_port
= htons (7000); /* FIXME: This should be configurable */
527 /* Connect to host */
528 if (connect (s
, (struct sockaddr
*) &sin
, sizeof (sin
)) < 0)
529 stub_error (L
"Couldn't connect to host gdb.");
531 /* Read from socket until told to exit. */
This page took 0.041109 seconds and 4 git commands to generate.