1 /* Target-vector operations for controlling Windows CE child processes, for GDB.
2 Copyright 1999, 2000 Free Software Foundation, Inc.
3 Contributed by Cygnus Solutions, A Red Hat Company.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without eve nthe implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
23 /* by Christopher Faylor (cgf@cygnus.com) */
25 /* We assume we're being built with and will be used for cygwin. */
29 #define SH4 /* Just to get all of the CONTEXT defines. */
33 #include "frame.h" /* required by inferior.h */
39 #include <sys/types.h>
46 #include <cygwin/in.h>
47 #include <cygwin/socket.h>
52 #include "gdb_string.h"
53 #include "gdbthread.h"
55 #include <sys/param.h>
56 #include "wince-stub.h"
59 /* The ui's event loop. */
60 extern int (*ui_loop_hook) PARAMS ((int signo));
62 /* If we're not using the old Cygwin header file set, define the
63 following which never should have been in the generic Win32 API
64 headers in the first place since they were our own invention... */
65 #ifndef _GNU_H_WINDOWS_H
66 #define FLAG_TRACE_BIT 0x100
67 #ifdef CONTEXT_FLOATING_POINT
68 #define CONTEXT_DEBUGGER0 (CONTEXT_FULL | CONTEXT_FLOATING_POINT)
70 #define CONTEXT_DEBUGGER0 (CONTEXT_FULL)
75 #define CONTEXT_DEBUGGER ((CONTEXT_DEBUGGER0 & ~(CONTEXT_SH4 | CONTEXT_FLOATING_POINT)) | CONTEXT_SH3)
77 #define CONTEXT_DEBUGGER CONTEXT_DEBUGGER0
79 /* The string sent by cygwin when it processes a signal.
80 FIXME: This should be in a cygwin include file. */
81 #define CYGWIN_SIGNAL_STRING "cygwin: signal"
83 #define CHECK(x) check (x, __FILE__,__LINE__)
84 #define DEBUG_EXEC(x) if (debug_exec) printf x
85 #define DEBUG_EVENTS(x) if (debug_events) printf x
86 #define DEBUG_MEM(x) if (debug_memory) printf x
87 #define DEBUG_EXCEPT(x) if (debug_exceptions) printf x
89 static int connection_initialized = 0; /* True if we've initialized a RAPI session. */
91 static DCACHE *remote_dcache;
93 /* The directory where the stub and executable files are uploaded. */
94 static const char *remote_directory = "\\gdb";
96 /* The types automatic upload available. */
103 upload_when = UPLOAD_NEWER;
105 /* Valid options for 'set remoteupload'. Note that options
106 must track upload_when enum. */
127 static char *remote_upload = NULL; /* Set by set remoteupload */
128 static int remote_add_host = 0;
130 /* Forward declaration */
131 extern struct target_ops child_ops;
133 static int win32_child_thread_alive PARAMS ((int));
134 void child_kill_inferior PARAMS ((void));
136 static int last_sig = 0; /* Set if a signal was received from the
139 /* Thread information structure used to track information that is
140 not available in gdb's thread structure. */
141 typedef struct thread_info_struct
143 struct thread_info_struct *next;
148 int stepped; /* True if stepped. */
150 unsigned long step_instr;
151 unsigned long step_prev;
156 static thread_info thread_head =
159 /* The process and thread handles for the above context. */
161 static DEBUG_EVENT current_event; /* The current debug event from
163 static HANDLE current_process_handle; /* Currently executing process */
164 static thread_info *current_thread; /* Info on currently selected thread */
165 static thread_info *this_thread; /* Info on thread returned by wait_for_debug_event */
166 static DWORD main_thread_id; /* Thread ID of the main thread */
168 /* Counts of things. */
169 static int exception_count = 0;
170 static int event_count = 0;
173 static int debug_exec = 0; /* show execution */
174 static int debug_events = 0; /* show events from kernel */
175 static int debug_memory = 0; /* show target memory accesses */
176 static int debug_exceptions = 0; /* show target exceptions */
178 /* An array of offset mappings into a Win32 Context structure.
179 This is a one-to-one mapping which is indexed by gdb's register
180 numbers. It retrieves an offset into the context structure where
181 the 4 byte register is located.
182 An offset value of -1 indicates that Win32 does not provide this
183 register in it's CONTEXT structure. regptr will return zero for this
186 This is used by the regptr function. */
187 #define context_offset(x) ((int)&(((PCONTEXT)NULL)->x))
188 static const int mappings[NUM_REGS + 1] =
191 context_offset (Eax),
192 context_offset (Ecx),
193 context_offset (Edx),
194 context_offset (Ebx),
195 context_offset (Esp),
196 context_offset (Ebp),
197 context_offset (Esi),
198 context_offset (Edi),
199 context_offset (Eip),
200 context_offset (EFlags),
201 context_offset (SegCs),
202 context_offset (SegSs),
203 context_offset (SegDs),
204 context_offset (SegEs),
205 context_offset (SegFs),
206 context_offset (SegGs),
207 context_offset (FloatSave.RegisterArea[0 * 10]),
208 context_offset (FloatSave.RegisterArea[1 * 10]),
209 context_offset (FloatSave.RegisterArea[2 * 10]),
210 context_offset (FloatSave.RegisterArea[3 * 10]),
211 context_offset (FloatSave.RegisterArea[4 * 10]),
212 context_offset (FloatSave.RegisterArea[5 * 10]),
213 context_offset (FloatSave.RegisterArea[6 * 10]),
214 context_offset (FloatSave.RegisterArea[7 * 10]),
226 context_offset (R10),
227 context_offset (R11),
228 context_offset (R12),
229 context_offset (R13),
230 context_offset (R14),
231 context_offset (R15),
232 context_offset (Fir),
233 context_offset (PR), /* Procedure Register */
234 context_offset (GBR), /* Global Base Register */
235 context_offset (MACH), /* Accumulate */
236 context_offset (MACL), /* Multiply */
237 context_offset (Psr),
238 context_offset (Fpul),
239 context_offset (Fpscr),
240 context_offset (FRegs[0]),
241 context_offset (FRegs[1]),
242 context_offset (FRegs[2]),
243 context_offset (FRegs[3]),
244 context_offset (FRegs[4]),
245 context_offset (FRegs[5]),
246 context_offset (FRegs[6]),
247 context_offset (FRegs[7]),
248 context_offset (FRegs[8]),
249 context_offset (FRegs[9]),
250 context_offset (FRegs[10]),
251 context_offset (FRegs[11]),
252 context_offset (FRegs[12]),
253 context_offset (FRegs[13]),
254 context_offset (FRegs[14]),
255 context_offset (FRegs[15]),
256 context_offset (xFRegs[0]),
257 context_offset (xFRegs[1]),
258 context_offset (xFRegs[2]),
259 context_offset (xFRegs[3]),
260 context_offset (xFRegs[4]),
261 context_offset (xFRegs[5]),
262 context_offset (xFRegs[6]),
263 context_offset (xFRegs[7]),
264 context_offset (xFRegs[8]),
265 context_offset (xFRegs[9]),
266 context_offset (xFRegs[10]),
267 context_offset (xFRegs[11]),
268 context_offset (xFRegs[12]),
269 context_offset (xFRegs[13]),
270 context_offset (xFRegs[14]),
271 context_offset (xFRegs[15]),
273 context_offset (IntZero),
274 context_offset (IntAt),
275 context_offset (IntV0),
276 context_offset (IntV1),
277 context_offset (IntA0),
278 context_offset (IntA1),
279 context_offset (IntA2),
280 context_offset (IntA3),
281 context_offset (IntT0),
282 context_offset (IntT1),
283 context_offset (IntT2),
284 context_offset (IntT3),
285 context_offset (IntT4),
286 context_offset (IntT5),
287 context_offset (IntT6),
288 context_offset (IntT7),
289 context_offset (IntS0),
290 context_offset (IntS1),
291 context_offset (IntS2),
292 context_offset (IntS3),
293 context_offset (IntS4),
294 context_offset (IntS5),
295 context_offset (IntS6),
296 context_offset (IntS7),
297 context_offset (IntT8),
298 context_offset (IntT9),
299 context_offset (IntK0),
300 context_offset (IntK1),
301 context_offset (IntGp),
302 context_offset (IntSp),
303 context_offset (IntS8),
304 context_offset (IntRa),
305 context_offset (Psr),
306 context_offset (IntLo),
307 context_offset (IntHi),
310 context_offset (Fir),
311 context_offset (FltF0),
312 context_offset (FltF1),
313 context_offset (FltF2),
314 context_offset (FltF3),
315 context_offset (FltF4),
316 context_offset (FltF5),
317 context_offset (FltF6),
318 context_offset (FltF7),
319 context_offset (FltF8),
320 context_offset (FltF9),
321 context_offset (FltF10),
322 context_offset (FltF11),
323 context_offset (FltF12),
324 context_offset (FltF13),
325 context_offset (FltF14),
326 context_offset (FltF15),
327 context_offset (FltF16),
328 context_offset (FltF17),
329 context_offset (FltF18),
330 context_offset (FltF19),
331 context_offset (FltF20),
332 context_offset (FltF21),
333 context_offset (FltF22),
334 context_offset (FltF23),
335 context_offset (FltF24),
336 context_offset (FltF25),
337 context_offset (FltF26),
338 context_offset (FltF27),
339 context_offset (FltF28),
340 context_offset (FltF29),
341 context_offset (FltF30),
342 context_offset (FltF31),
343 context_offset (Fsr),
344 context_offset (Fir),
357 context_offset (R10),
358 context_offset (R11),
359 context_offset (R12),
372 context_offset (Psr),
377 /* This vector maps the target's idea of an exception (extracted
378 from the DEBUG_EVENT structure) to GDB's idea. */
380 struct xlate_exception
383 enum target_signal us;
386 static const struct xlate_exception
389 {EXCEPTION_ACCESS_VIOLATION, TARGET_SIGNAL_SEGV},
390 {STATUS_STACK_OVERFLOW, TARGET_SIGNAL_SEGV},
391 {EXCEPTION_BREAKPOINT, TARGET_SIGNAL_TRAP},
392 {DBG_CONTROL_C, TARGET_SIGNAL_INT},
393 {EXCEPTION_SINGLE_STEP, TARGET_SIGNAL_TRAP},
396 /******************** Beginning of stub interface ********************/
398 /* Stub interface description:
400 The Windows CE stub implements a crude RPC. The hand-held device
401 connects to gdb using port 7000. gdb and the stub then communicate
404 byte 0: command id (e.g. Create Process)
411 byte 3-n: arbitrary memory.
413 The interface is deterministic, i.e., if the stub expects a DWORD then
414 the gdb server should send a DWORD.
417 /* Note: In the functions below, the `huh' parameter is a string passed from the
418 function containing a descriptive string concerning the current operation.
419 This is used for error reporting.
421 The 'what' parameter is a command id as found in wince-stub.h.
423 Hopefully, the rest of the parameters are self-explanatory.
426 static int s; /* communication socket */
428 /* v-style interface for handling varying argyment list error messages.
429 Displays the error message in a dialog box and exits when user clicks
432 vstub_error (LPCSTR fmt, va_list * args)
435 vsprintf (buf, fmt, args);
440 /* The standard way to display an error message and exit. */
442 stub_error (LPCSTR fmt,...)
445 va_start (args, fmt);
446 vstub_error (fmt, args);
449 /* Standard "oh well" can't communicate error. Someday this might attempt
452 attempt_resync (LPCSTR huh, int s)
454 stub_error ("lost synchronization with target attempting %s", huh);
457 /* Read arbitrary stuff from a socket. */
459 sockread (LPCSTR huh, int s, void *str, size_t n)
463 if (recv (s, str, n, 0) == n)
465 attempt_resync (huh, s);
469 /* Write arbitrary stuff to a socket. */
471 sockwrite (LPCSTR huh, const void *str, size_t n)
475 if (send (s, str, n, 0) == n)
477 attempt_resync (huh, s);
481 /* Output an id/dword to the host */
483 putdword (LPCSTR huh, gdb_wince_id what, DWORD n)
485 if (sockwrite (huh, &what, sizeof (what)) != sizeof (what))
486 stub_error ("error writing record id to host for %s", huh);
487 if (sockwrite (huh, &n, sizeof (n)) != sizeof (n))
488 stub_error ("error writing %s to host.", huh);
491 /* Output an id/word to the host */
493 putword (LPCSTR huh, gdb_wince_id what, WORD n)
495 if (sockwrite (huh, &what, sizeof (what)) != sizeof (what))
496 stub_error ("error writing record id to host for %s", huh);
497 if (sockwrite (huh, &n, sizeof (n)) != sizeof (n))
498 stub_error ("error writing %s host.", huh);
501 /* Convenience define for outputting a "gdb_wince_len" type. */
502 #define putlen(huh, what, n) putword((huh), (what), (gdb_wince_len) (n))
504 /* Put an arbitrary block of memory to the gdb host. This comes in
505 two chunks an id/dword representing the length and the stream of memory
508 putmemory (LPCSTR huh, gdb_wince_id what, const void *mem, gdb_wince_len len)
510 putlen (huh, what, len);
511 if (((short) len > 0) && sockwrite (huh, mem, len) != len)
512 stub_error ("error writing %s to host.", huh);
515 /* Output the result of an operation to the host. If res != 0, sends a block of
516 memory starting at mem of len bytes. If res == 0, sends -GetLastError () and
517 avoids sending the mem. */
519 getdword (LPCSTR huh, gdb_wince_id what_this)
524 if (sockread (huh, s, &what, sizeof (what)) != sizeof (what))
525 stub_error ("error getting record type from host - %s.", huh);
526 while (what_this != what);
528 if (sockread (huh, s, &n, sizeof (n)) != sizeof (n))
529 stub_error ("error getting %s from host.", huh);
534 /* Get a an ID (possibly) and a WORD from the host gdb.
535 Don't bother with the id if the main loop has already
538 getword (LPCSTR huh, gdb_wince_id what_this)
543 if (sockread (huh, s, &what, sizeof (what)) != sizeof (what))
544 stub_error ("error getting record type from host - %s.", huh);
545 while (what_this != what);
547 if (sockread (huh, s, &n, sizeof (n)) != sizeof (n))
548 stub_error ("error getting %s from host.", huh);
553 /* Handy defines for getting/putting various types of values. */
554 #define gethandle(huh, what) (HANDLE) getdword ((huh), (what))
555 #define getpvoid(huh, what) (LPVOID) getdword ((huh), (what))
556 #define getlen(huh, what) (gdb_wince_len) getword ((huh), (what))
557 #define puthandle(huh, what, h) putdword ((huh), (what), (DWORD) (h))
558 #define putpvoid(huh, what, p) putdword ((huh), (what), (DWORD) (p))
560 /* Retrieve the result of an operation from the stub. If nbytes < 0) then nbytes
561 is actually an error and nothing else follows. Use SetLastError to remember this.
562 if nbytes > 0, retrieve a block of *nbytes into buf.
565 getresult (LPCSTR huh, gdb_wince_id what, LPVOID buf, gdb_wince_len * nbytes)
571 *nbytes = getlen (huh, what);
573 if ((short) *nbytes < 0)
575 SetLastError (-(short) *nbytes);
579 if ((gdb_wince_len) sockread (huh, s, buf, *nbytes) != *nbytes)
580 stub_error ("couldn't read information from wince stub - %s", huh);
585 /* Convert "narrow" string to "wide". Manipulates a buffer ring of 8
586 buffers which hold the translated string. This is an arbitrary limit
587 but it is approximately double the current needs of this module.
590 towide (const char *s, gdb_wince_len * out_len)
593 static LPWSTR outs[8] =
594 {NULL /*, NULL, etc. */ };
600 /* First determine the length required to hold the converted string. */
601 *out_len = sizeof (WCHAR) * MultiByteToWideChar (CP_ACP, 0, s, -1, NULL, 0);
603 return NULL; /* The conversion failed */
605 if (++n >= (sizeof (outs) / sizeof (outs[0])))
608 /* Allocate space for the converted string, reusing any previously allocated
609 space, if applicable. Note that if outs[n] is NULL, realloc will act as
610 a malloc (under cygwin, at least).
612 outs[n] = (LPWSTR) realloc (outs[n], *out_len);
613 memset (outs[n], 0, *out_len);
614 (void) MultiByteToWideChar (CP_ACP, 0, s, -1, outs[n], *out_len);
618 /******************** Emulation routines start here. ********************
620 The functions below are modelled after their Win32 counterparts. They are named
621 similarly to Win32 and take exactly the same arguments except where otherwise noted.
622 They communicate with the stub on the hand-held device by sending their arguments
623 over the socket and waiting for results from the socket.
625 There is one universal change. In cases where a length is expected to be returned
626 in a DWORD, we use a gdb_wince_len type instead. Currently this is an unsigned short
627 which is smaller than the standard Win32 DWORD. This is done to minimize unnecessary
628 traffic since the connection to Windows CE can be slow. To change this, modify the
629 typedef in wince-stub.h and change the putlen/getlen macros in this file and in
633 create_process (LPSTR exec_file, LPSTR args, DWORD flags, PROCESS_INFORMATION * pi)
638 buf = towide (exec_file, &len);
639 putmemory ("CreateProcess exec_file", GDB_CREATEPROCESS, buf, len);
640 buf = towide (args, &len);
641 putmemory ("CreateProcess args", GDB_CREATEPROCESS, buf, len);
642 putdword ("CreateProcess flags", GDB_CREATEPROCESS, flags);
643 return getresult ("CreateProcess result", GDB_CREATEPROCESS, pi, NULL);
646 /* Emulate TerminateProcess. Don't bother with the second argument since CE
650 terminate_process (HANDLE h)
652 gdb_wince_result res;
655 puthandle ("TerminateProcess handle", GDB_TERMINATEPROCESS, h);
656 return getresult ("TerminateProcess result", GDB_TERMINATEPROCESS, &res, NULL);
660 wait_for_debug_event (DEBUG_EVENT * ev, DWORD ms)
664 putdword ("WaitForDebugEvent ms", GDB_WAITFORDEBUGEVENT, ms);
665 return getresult ("WaitForDebugEvent event", GDB_WAITFORDEBUGEVENT, ev, NULL);
669 get_thread_context (HANDLE h, CONTEXT * c)
673 puthandle ("GetThreadContext handle", GDB_GETTHREADCONTEXT, h);
674 putdword ("GetThreadContext flags", GDB_GETTHREADCONTEXT, c->ContextFlags);
675 return getresult ("GetThreadContext context", GDB_GETTHREADCONTEXT, c, NULL);
679 set_thread_context (HANDLE h, CONTEXT * c)
681 gdb_wince_result res;
684 puthandle ("SetThreadContext handle", GDB_SETTHREADCONTEXT, h);
685 putmemory ("SetThreadContext context", GDB_SETTHREADCONTEXT, c, sizeof (*c));
686 return getresult ("SetThreadContext context", GDB_SETTHREADCONTEXT, &res, NULL);
690 read_process_memory (HANDLE h, LPCVOID where, LPVOID buf, gdb_wince_len len, gdb_wince_len * nbytes)
694 puthandle ("ReadProcessMemory handle", GDB_READPROCESSMEMORY, h);
695 putpvoid ("ReadProcessMemory location", GDB_READPROCESSMEMORY, where);
696 putlen ("ReadProcessMemory size", GDB_READPROCESSMEMORY, len);
698 return getresult ("ReadProcessMemory buf", GDB_READPROCESSMEMORY, buf, nbytes);
702 write_process_memory (HANDLE h, LPCVOID where, LPCVOID buf, gdb_wince_len len, gdb_wince_len * nbytes)
706 puthandle ("WriteProcessMemory handle", GDB_WRITEPROCESSMEMORY, h);
707 putpvoid ("WriteProcessMemory location", GDB_WRITEPROCESSMEMORY, where);
708 putmemory ("WriteProcProcessMemory buf", GDB_WRITEPROCESSMEMORY, buf, len);
710 return getresult ("WriteProcessMemory result", GDB_WRITEPROCESSMEMORY, nbytes, NULL);
714 remote_read_bytes (CORE_ADDR memaddr, char *myaddr, int len)
716 gdb_wince_len nbytes;
717 if (!read_process_memory (current_process_handle, (LPCVOID) memaddr,
718 (LPVOID) myaddr, len, &nbytes))
724 remote_write_bytes (CORE_ADDR memaddr, char *myaddr, int len)
726 gdb_wince_len nbytes;
727 if (!write_process_memory (current_process_handle, (LPCVOID) memaddr,
728 (LPCVOID) myaddr, len, &nbytes))
733 /* This is not a standard Win32 function. It instructs the stub to return TRUE
734 if the thread referenced by HANDLE h is alive.
737 thread_alive (HANDLE h)
739 gdb_wince_result res;
742 puthandle ("ThreadAlive handle", GDB_THREADALIVE, h);
743 return getresult ("ThreadAlive result", GDB_THREADALIVE, &res, NULL);
747 suspend_thread (HANDLE h)
751 puthandle ("SuspendThread handle", GDB_SUSPENDTHREAD, h);
752 return (int) getdword ("SuspendThread result", GDB_SUSPENDTHREAD);
756 resume_thread (HANDLE h)
760 puthandle ("ResumeThread handle", GDB_RESUMETHREAD, h);
761 return (int) getdword ("SuspendThread result", GDB_RESUMETHREAD);
765 continue_debug_event (DWORD pid, DWORD tid, DWORD status)
767 gdb_wince_result res;
770 putdword ("ContinueDebugEvent pid", GDB_CONTINUEDEBUGEVENT, pid);
771 putdword ("ContinueDebugEvent tid", GDB_CONTINUEDEBUGEVENT, tid);
772 putdword ("ContinueDebugEvent status", GDB_CONTINUEDEBUGEVENT, status);
773 return getresult ("ContinueDebugEvent result", GDB_CONTINUEDEBUGEVENT, &res, NULL);
777 close_handle (HANDLE h)
779 gdb_wince_result res;
782 puthandle ("CloseHandle handle", GDB_CLOSEHANDLE, h);
783 return (int) getresult ("CloseHandle result", GDB_CLOSEHANDLE, &res, NULL);
786 /* This is not a standard Win32 interface. This function tells the stub
794 (void) putdword ("Stopping gdb stub", GDB_STOPSTUB, 0);
798 /******************** End of emulation routines. ********************/
799 /******************** End of stub interface ********************/
801 /* Find a thread record given a thread id.
802 If get_context then also retrieve the context for this
805 thread_rec (DWORD id, int get_context)
809 for (th = &thread_head; (th = th->next) != NULL;)
812 if (!th->suspend_count && get_context)
814 if (get_context > 0 && th != this_thread)
815 th->suspend_count = suspend_thread (th->h) + 1;
816 else if (get_context < 0)
817 th->suspend_count = -1;
819 th->context.ContextFlags = CONTEXT_DEBUGGER;
820 get_thread_context (th->h, &th->context);
828 /* Add a thread to the thread list */
830 child_add_thread (DWORD id, HANDLE h)
834 if ((th = thread_rec (id, FALSE)))
837 th = (thread_info *) xmalloc (sizeof (*th));
838 memset (th, 0, sizeof (*th));
841 th->next = thread_head.next;
842 thread_head.next = th;
847 /* Clear out any old thread list and reintialize it to a
850 child_init_thread_list ()
852 thread_info *th = &thread_head;
854 DEBUG_EVENTS (("gdb: child_init_thread_list\n"));
856 while (th->next != NULL)
858 thread_info *here = th->next;
859 th->next = here->next;
860 (void) close_handle (here->h);
865 /* Delete a thread from the list of threads */
867 child_delete_thread (DWORD id)
872 printf_unfiltered ("[Deleting %s]\n", target_pid_to_str (id));
875 for (th = &thread_head;
876 th->next != NULL && th->next->id != id;
880 if (th->next != NULL)
882 thread_info *here = th->next;
883 th->next = here->next;
884 close_handle (here->h);
890 check (BOOL ok, const char *file, int line)
893 printf_filtered ("error return %s:%d was %d\n", file, line, GetLastError ());
896 /* Return a pointer into a CONTEXT field indexed by gdb register number.
897 Return a pointer to an address pointing to zero if there is no
898 corresponding CONTEXT field for the given register number.
901 regptr (LPCONTEXT c, int r)
903 static ULONG zero = 0;
908 p = (ULONG *) (((char *) c) + mappings[r]);
913 do_child_fetch_inferior_registers (int r)
917 supply_register (r, (char *) regptr (¤t_thread->context, r));
921 for (r = 0; r < NUM_REGS; r++)
922 do_child_fetch_inferior_registers (r);
927 child_fetch_inferior_registers (int r)
929 current_thread = thread_rec (inferior_pid, TRUE);
930 do_child_fetch_inferior_registers (r);
934 do_child_store_inferior_registers (int r)
937 read_register_gen (r, ((char *) ¤t_thread->context) + mappings[r]);
940 for (r = 0; r < NUM_REGS; r++)
941 do_child_store_inferior_registers (r);
945 /* Store a new register value into the current thread context */
947 child_store_inferior_registers (int r)
949 current_thread = thread_rec (inferior_pid, TRUE);
950 do_child_store_inferior_registers (r);
953 /* Wait for child to do something. Return pid of child, or -1 in case
954 of error; store status through argument pointer OURSTATUS. */
957 handle_load_dll (PTR dummy)
959 LOAD_DLL_DEBUG_INFO *event = ¤t_event.u.LoadDll;
960 char dll_buf[MAX_PATH + 1];
961 char *p, *bufp, *imgp, *dll_name, *dll_basename;
964 dll_buf[0] = dll_buf[sizeof (dll_buf) - 1] = '\0';
965 if (!event->lpImageName)
969 for (bufp = dll_buf, imgp = event->lpImageName;
970 bufp < dll_buf + sizeof (dll_buf);
971 bufp += 16, imgp += 16)
973 gdb_wince_len nbytes = 0;
974 (void) read_process_memory (current_process_handle,
975 imgp, bufp, 16, &nbytes);
977 if (!nbytes && bufp == dll_buf)
978 return 1; /* couldn't read it */
979 for (p = bufp; p < bufp + nbytes; p++)
995 dll_name = alloca (len);
1000 if (!event->fUnicode)
1001 memcpy (dll_name, dll_buf, len);
1003 WideCharToMultiByte (CP_ACP, 0, (LPCWSTR) dll_buf, len,
1004 dll_name, len, 0, 0);
1006 while ((p = strchr (dll_name, '\\')))
1009 /* FIXME!! It would be nice to define one symbol which pointed to the
1010 front of the dll if we can't find any symbols. */
1012 if (!(dll_basename = strrchr (dll_name, '/')))
1013 dll_basename = dll_name;
1017 /* The symbols in a dll are offset by 0x1000, which is the
1018 the offset from 0 of the first byte in an image - because
1019 of the file header and the section alignment.
1021 FIXME: Is this the real reason that we need the 0x1000 ? */
1023 printf_unfiltered ("%x:%s", event->lpBaseOfDll, dll_name);
1024 #if 0 /* FIXME: Need to use RAPI stuff to read the file someday. */
1026 struct section_addr_info section_addrs;
1027 memset (§ion_addrs, 0, sizeof (section_addrs));
1028 section_addrs.text_addr = (int) event->lpBaseOfDll + 0x1000;
1029 symbol_file_add (dll_name, 0, §ion_addrs, 0, OBJF_SHARED);
1032 printf_unfiltered ("\n");
1037 /* Handle DEBUG_STRING output from child process.
1038 Cygwin prepends its messages with a "cygwin:". Interpret this as
1039 a Cygwin signal. Otherwise just print the string as a warning. */
1041 handle_output_debug_string (struct target_waitstatus *ourstatus)
1046 gdb_wince_len nbytes_read;
1047 gdb_wince_len nbytes = current_event.u.DebugString.nDebugStringLength;
1052 memset (p, 0, sizeof (p));
1053 if (!read_process_memory (current_process_handle,
1054 current_event.u.DebugString.lpDebugStringData,
1055 &p, nbytes, &nbytes_read)
1059 memset (s, 0, sizeof (s));
1060 WideCharToMultiByte (CP_ACP, 0, (LPCWSTR) p, (int) nbytes_read, s,
1061 sizeof (s) - 1, NULL, NULL);
1062 q = strchr (s, '\n');
1074 /* Handle target exceptions. */
1076 handle_exception (struct target_waitstatus *ourstatus)
1080 if (current_event.u.Exception.dwFirstChance)
1083 ourstatus->kind = TARGET_WAITKIND_STOPPED;
1085 /* Record the context of the current thread */
1086 th = thread_rec (current_event.dwThreadId, -1);
1088 switch (current_event.u.Exception.ExceptionRecord.ExceptionCode)
1090 case EXCEPTION_ACCESS_VIOLATION:
1091 DEBUG_EXCEPT (("gdb: Target exception ACCESS_VIOLATION at 0x%08x\n",
1092 (unsigned) current_event.u.Exception.ExceptionRecord.ExceptionAddress));
1093 ourstatus->value.sig = TARGET_SIGNAL_SEGV;
1095 case STATUS_STACK_OVERFLOW:
1096 DEBUG_EXCEPT (("gdb: Target exception STACK_OVERFLOW at 0x%08x\n",
1097 (unsigned) current_event.u.Exception.ExceptionRecord.ExceptionAddress));
1098 ourstatus->value.sig = TARGET_SIGNAL_SEGV;
1100 case EXCEPTION_BREAKPOINT:
1101 DEBUG_EXCEPT (("gdb: Target exception BREAKPOINT at 0x%08x\n",
1102 (unsigned) current_event.u.Exception.ExceptionRecord.ExceptionAddress));
1103 ourstatus->value.sig = TARGET_SIGNAL_TRAP;
1106 DEBUG_EXCEPT (("gdb: Target exception CONTROL_C at 0x%08x\n",
1107 (unsigned) current_event.u.Exception.ExceptionRecord.ExceptionAddress));
1108 ourstatus->value.sig = TARGET_SIGNAL_INT;
1109 /* User typed CTRL-C. Continue with this status */
1110 last_sig = SIGINT; /* FIXME - should check pass state */
1112 case EXCEPTION_SINGLE_STEP:
1113 DEBUG_EXCEPT (("gdb: Target exception SINGLE_STEP at 0x%08x\n",
1114 (unsigned) current_event.u.Exception.ExceptionRecord.ExceptionAddress));
1115 ourstatus->value.sig = TARGET_SIGNAL_TRAP;
1118 /* This may be a structured exception handling exception. In
1119 that case, we want to let the program try to handle it, and
1120 only break if we see the exception a second time. */
1122 printf_unfiltered ("gdb: unknown target exception 0x%08x at 0x%08x\n",
1123 current_event.u.Exception.ExceptionRecord.ExceptionCode,
1124 current_event.u.Exception.ExceptionRecord.ExceptionAddress);
1125 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
1132 /* Resume all artificially suspended threads if we are continuing
1135 child_continue (DWORD continue_status, int id)
1141 DEBUG_EVENTS (("ContinueDebugEvent (cpid=%d, ctid=%d, DBG_CONTINUE);\n",
1142 (unsigned) current_event.dwProcessId, (unsigned) current_event.dwThreadId));
1143 res = continue_debug_event (current_event.dwProcessId,
1144 current_event.dwThreadId,
1147 for (th = &thread_head; (th = th->next) != NULL;)
1148 if (((id == -1) || (id == th->id)) && th->suspend_count)
1150 for (i = 0; i < th->suspend_count; i++)
1151 (void) resume_thread (th->h);
1152 th->suspend_count = 0;
1158 /* Get the next event from the child. Return 1 if the event requires
1159 handling by WFI (or whatever).
1162 get_child_debug_event (int pid, struct target_waitstatus *ourstatus, DWORD * event_code, int *retval)
1165 DWORD continue_status;
1168 if (!(debug_event = wait_for_debug_event (¤t_event, 1000)))
1170 breakout = *retval = *event_code = 0;
1174 this_thread = thread_rec (current_event.dwThreadId, FALSE);
1176 continue_status = DBG_CONTINUE;
1178 switch (*event_code = current_event.dwDebugEventCode)
1180 case CREATE_THREAD_DEBUG_EVENT:
1181 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%x code=%s)\n",
1182 (unsigned) current_event.dwProcessId,
1183 (unsigned) current_event.dwThreadId,
1184 "CREATE_THREAD_DEBUG_EVENT"));
1185 /* Record the existence of this thread */
1186 child_add_thread (current_event.dwThreadId,
1187 current_event.u.CreateThread.hThread);
1189 printf_unfiltered ("[New %s]\n",
1190 target_pid_to_str (current_event.dwThreadId));
1193 case EXIT_THREAD_DEBUG_EVENT:
1194 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n",
1195 (unsigned) current_event.dwProcessId,
1196 (unsigned) current_event.dwThreadId,
1197 "EXIT_THREAD_DEBUG_EVENT"));
1198 child_delete_thread (current_event.dwThreadId);
1201 case CREATE_PROCESS_DEBUG_EVENT:
1202 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n",
1203 (unsigned) current_event.dwProcessId,
1204 (unsigned) current_event.dwThreadId,
1205 "CREATE_PROCESS_DEBUG_EVENT"));
1206 current_process_handle = current_event.u.CreateProcessInfo.hProcess;
1208 main_thread_id = inferior_pid = current_event.dwThreadId;
1209 /* Add the main thread */
1210 current_thread = child_add_thread (inferior_pid,
1211 current_event.u.CreateProcessInfo.hThread);
1214 case EXIT_PROCESS_DEBUG_EVENT:
1215 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n",
1216 (unsigned) current_event.dwProcessId,
1217 (unsigned) current_event.dwThreadId,
1218 "EXIT_PROCESS_DEBUG_EVENT"));
1219 ourstatus->kind = TARGET_WAITKIND_EXITED;
1220 ourstatus->value.integer = current_event.u.ExitProcess.dwExitCode;
1221 close_handle (current_process_handle);
1222 *retval = current_event.dwProcessId;
1225 case LOAD_DLL_DEBUG_EVENT:
1226 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n",
1227 (unsigned) current_event.dwProcessId,
1228 (unsigned) current_event.dwThreadId,
1229 "LOAD_DLL_DEBUG_EVENT"));
1230 catch_errors (handle_load_dll, NULL, (char *) "", RETURN_MASK_ALL);
1231 registers_changed (); /* mark all regs invalid */
1234 case UNLOAD_DLL_DEBUG_EVENT:
1235 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n",
1236 (unsigned) current_event.dwProcessId,
1237 (unsigned) current_event.dwThreadId,
1238 "UNLOAD_DLL_DEBUG_EVENT"));
1239 break; /* FIXME: don't know what to do here */
1241 case EXCEPTION_DEBUG_EVENT:
1242 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n",
1243 (unsigned) current_event.dwProcessId,
1244 (unsigned) current_event.dwThreadId,
1245 "EXCEPTION_DEBUG_EVENT"));
1246 if (handle_exception (ourstatus))
1249 *retval = current_event.dwThreadId;
1250 remote_read_bytes (read_pc (), buf, sizeof (buf));
1251 dcache_xfer_memory (remote_dcache, read_pc (), buf, sizeof (buf), 0);
1254 continue_status = DBG_EXCEPTION_NOT_HANDLED;
1257 case OUTPUT_DEBUG_STRING_EVENT: /* message from the kernel */
1258 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n",
1259 (unsigned) current_event.dwProcessId,
1260 (unsigned) current_event.dwThreadId,
1261 "OUTPUT_DEBUG_STRING_EVENT"));
1262 handle_output_debug_string (ourstatus);
1265 printf_unfiltered ("gdb: kernel event for pid=%d tid=%d\n",
1266 current_event.dwProcessId,
1267 current_event.dwThreadId);
1268 printf_unfiltered (" unknown event code %d\n",
1269 current_event.dwDebugEventCode);
1274 CHECK (child_continue (continue_status, -1));
1280 /* Wait for interesting events to occur in the target process. */
1282 child_wait (int pid, struct target_waitstatus *ourstatus)
1287 /* We loop when we get a non-standard exception rather than return
1288 with a SPURIOUS because resume can try and step or modify things,
1289 which needs a current_thread->h. But some of these exceptions mark
1290 the birth or death of threads, which mean that the current thread
1291 isn't necessarily what you think it is. */
1294 if (get_child_debug_event (pid, ourstatus, &event_code, &retval))
1300 if (ui_loop_hook != NULL)
1301 detach = ui_loop_hook (0);
1304 child_kill_inferior ();
1308 /* Print status information about what we're accessing. */
1311 child_files_info (ignore)
1312 struct target_ops *ignore;
1314 printf_unfiltered ("\tUsing the running image of child %s.\n",
1315 target_pid_to_str (inferior_pid));
1320 child_open (arg, from_tty)
1324 error ("Use the \"run\" command to start a child process.");
1327 #define FACTOR (0x19db1ded53ea710LL)
1328 #define NSPERSEC 10000000
1330 /* Convert a Win32 time to "UNIX" format. */
1332 to_time_t (FILETIME * ptr)
1334 /* A file time is the number of 100ns since jan 1 1601
1335 stuffed into two long words.
1336 A time_t is the number of seconds since jan 1 1970. */
1339 long long x = ((long long) ptr->dwHighDateTime << 32) + ((unsigned) ptr->dwLowDateTime);
1340 x -= FACTOR; /* number of 100ns between 1601 and 1970 */
1341 rem = x % ((long long) NSPERSEC);
1342 rem += (NSPERSEC / 2);
1343 x /= (long long) NSPERSEC; /* number of 100ns in a second */
1344 x += (long long) (rem / NSPERSEC);
1348 /* Upload a file to the remote device depending on the user's
1349 'set remoteupload' specification. */
1351 upload_to_device (const char *to, const char *from)
1354 const char *dir = remote_directory ? : "\\gdb";
1356 static char *remotefile = NULL;
1360 const char *in_to = to;
1361 FILETIME ctime, atime, wtime;
1365 /* Look for a path separator and only use trailing part. */
1366 while ((p = strpbrk (to, "/\\")) != NULL)
1370 error ("no filename found to upload - %s.", in_to);
1372 len = strlen (dir) + strlen (to) + 2;
1373 remotefile = (char *) realloc (remotefile, len);
1374 strcpy (remotefile, dir);
1375 strcat (remotefile, "\\");
1376 strcat (remotefile, to);
1378 if (upload_when == UPLOAD_NEVER)
1379 return remotefile; /* Don't bother uploading. */
1381 /* Open the source. */
1382 if ((fd = openp (getenv ("PATH"), TRUE, (char *) from, O_RDONLY, 0, NULL)) < 0)
1383 error ("couldn't open %s", from);
1385 /* Get the time for later comparison. */
1386 if (fstat (fd, &st))
1387 st.st_mtime = (time_t) - 1;
1389 /* Always attempt to create the directory on the remote system. */
1390 wstr = towide (dir, NULL);
1391 (void) CeCreateDirectory (wstr, NULL);
1393 /* Attempt to open the remote file, creating it if it doesn't exist. */
1394 wstr = towide (remotefile, NULL);
1395 h = CeCreateFile (wstr, GENERIC_READ | GENERIC_WRITE, 0, NULL,
1396 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1398 /* Some kind of problem? */
1399 err = CeGetLastError ();
1401 error ("error creating file to \"%s\". Windows error %d.",
1404 /* See if we need to upload the file. */
1405 if (upload_when == UPLOAD_ALWAYS ||
1406 err != ERROR_ALREADY_EXISTS ||
1407 !CeGetFileTime (h, &ctime, &atime, &wtime) ||
1408 to_time_t (&wtime) < st.st_mtime)
1414 /* Upload the file. */
1415 while ((n = read (fd, buf, sizeof (buf))) > 0)
1416 if (!CeWriteFile (h, buf, (DWORD) n, &nbytes, NULL))
1417 error ("error writing to remote device - %d.",
1427 /* Initialize the connection to the remote device. */
1434 struct sockaddr_in sin;
1435 char *stub_file_name;
1437 PROCESS_INFORMATION pi;
1439 if (!connection_initialized)
1440 switch (CeRapiInit ())
1443 connection_initialized = 1;
1447 error ("Can't initialize connection to remote device.\n");
1451 /* Upload the stub to the handheld device. */
1452 stub_file_name = upload_to_device ("wince-stub.exe", WINCE_STUB);
1453 strcpy (args, stub_file_name);
1455 if (remote_add_host)
1458 hostname = strchr (args, '\0');
1459 if (gethostname (hostname, sizeof (args) - strlen (args)))
1460 error ("couldn't get hostname of this system.");
1464 if ((s0 = socket (AF_INET, SOCK_STREAM, 0)) < 0)
1465 stub_error ("Couldn't connect to host system.");
1467 /* Allow rapid reuse of the port. */
1469 (void) setsockopt (s0, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp, sizeof (tmp));
1472 /* Set up the information for connecting to the host gdb process. */
1473 memset (&sin, 0, sizeof (sin));
1474 sin.sin_family = AF_INET;
1475 sin.sin_port = htons (7000); /* FIXME: This should be configurable */
1477 if (bind (s0, (struct sockaddr *) &sin, sizeof (sin)))
1478 error ("couldn't bind socket");
1481 error ("Couldn't open socket for listening.\n");
1483 /* Start up the stub on the remote device. */
1484 if (!CeCreateProcess (towide (stub_file_name, NULL), towide (args, NULL),
1485 NULL, NULL, 0, 0, NULL, NULL, NULL, &pi))
1486 error ("Unable to start remote stub '%s'. Windows CE error %d.",
1487 stub_file_name, CeGetLastError ());
1489 /* Wait for a connection */
1491 if ((s = accept (s0, NULL, NULL)) < 0)
1492 error ("couldn't set up server for connection.");
1497 /* Start an inferior win32 child process and sets inferior_pid to its pid.
1498 EXEC_FILE is the file to run.
1499 ALLARGS is a string containing the arguments to the program.
1500 ENV is the environment vector to pass. Errors reported with error(). */
1502 child_create_inferior (char *exec_file, char *args, char **env)
1504 PROCESS_INFORMATION pi;
1505 struct target_waitstatus dummy;
1507 DWORD flags, event_code;
1508 char *exec_and_args;
1511 error ("No executable specified, use `target exec'.\n");
1513 flags = DEBUG_PROCESS;
1515 wince_initialize (); /* Make sure we've got a connection. */
1517 remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes);
1519 dcache_flush (remote_dcache);
1521 exec_file = upload_to_device (exec_file, exec_file);
1523 while (*args == ' ')
1526 /* Allocate space for "command<sp>args" */
1529 exec_and_args = alloca (strlen (exec_file) + 1);
1530 strcpy (exec_and_args, exec_file);
1534 exec_and_args = alloca (strlen (exec_file + strlen (args) + 2));
1535 sprintf (exec_and_args, "%s %s", exec_file, args);
1538 memset (&pi, 0, sizeof (pi));
1539 /* Execute the process */
1540 if (!create_process (exec_file, exec_and_args, flags, &pi))
1541 error ("Error creating process %s, (error %d)\n", exec_file, GetLastError ());
1543 exception_count = 0;
1546 current_process_handle = pi.hProcess;
1547 current_event.dwProcessId = pi.dwProcessId;
1548 memset (¤t_event, 0, sizeof (current_event));
1549 inferior_pid = current_event.dwThreadId = pi.dwThreadId;
1550 push_target (&child_ops);
1551 child_init_thread_list ();
1552 child_add_thread (pi.dwThreadId, pi.hThread);
1553 init_wait_for_inferior ();
1554 clear_proceed_status ();
1555 target_terminal_init ();
1556 target_terminal_inferior ();
1559 /* Run until process and threads are loaded */
1561 get_child_debug_event (inferior_pid, &dummy, &event_code, &ret);
1562 while (event_code != CREATE_PROCESS_DEBUG_EVENT);
1564 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0);
1567 /* Chile has gone bye-bye. */
1569 child_mourn_inferior ()
1571 (void) child_continue (DBG_CONTINUE, -1);
1572 unpush_target (&child_ops);
1575 connection_initialized = 0;
1576 generic_mourn_inferior ();
1579 /* Move memory from child to/from gdb. */
1581 child_xfer_memory (CORE_ADDR memaddr, char *our, int len,
1582 int write, struct target_ops *target)
1586 return dcache_xfer_memory (remote_dcache, memaddr, our, len, write);
1589 /* Terminate the process and wait for child to tell us it has completed. */
1591 child_kill_inferior (void)
1593 CHECK (terminate_process (current_process_handle));
1597 if (!child_continue (DBG_CONTINUE, -1))
1599 if (!wait_for_debug_event (¤t_event, INFINITE))
1601 if (current_event.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT)
1605 CHECK (close_handle (current_process_handle));
1606 close_handle (current_thread->h);
1607 target_mourn_inferior (); /* or just child_mourn_inferior? */
1612 undoSStep (thread_info * th)
1616 memory_remove_breakpoint (th->step_pc, (void *) &th->step_prev);
1622 wince_software_single_step (unsigned int ignore, int insert_breakpoints_p)
1625 thread_info *th = current_thread; /* Info on currently selected thread */
1626 CORE_ADDR mips_next_pc (CORE_ADDR pc);
1628 if (!insert_breakpoints_p)
1635 pc = read_register (PC_REGNUM);
1636 th->step_pc = mips_next_pc (pc);
1638 memory_insert_breakpoint (th->step_pc, (void *) &th->step_prev);
1641 /* Hitachi SH architecture instruction encoding masks */
1643 #define COND_BR_MASK 0xff00
1644 #define UCOND_DBR_MASK 0xe000
1645 #define UCOND_RBR_MASK 0xf0df
1646 #define TRAPA_MASK 0xff00
1648 #define COND_DISP 0x00ff
1649 #define UCOND_DISP 0x0fff
1650 #define UCOND_REG 0x0f00
1652 /* Hitachi SH instruction opcodes */
1654 #define BF_INSTR 0x8b00
1655 #define BT_INSTR 0x8900
1656 #define BRA_INSTR 0xa000
1657 #define BSR_INSTR 0xb000
1658 #define JMP_INSTR 0x402b
1659 #define JSR_INSTR 0x400b
1660 #define RTS_INSTR 0x000b
1661 #define RTE_INSTR 0x002b
1662 #define TRAPA_INSTR 0xc300
1663 #define SSTEP_INSTR 0xc3ff
1666 #define T_BIT_MASK 0x0001
1668 /* Undo the effect of a previous doSStep. If we single stepped,
1669 restore the old instruction. */
1672 undoSStep (thread_info * th)
1677 write_process_memory (current_process_handle, (LPVOID) th->step_pc,
1678 (LPVOID) & th->step_instr, sizeof (short), &done);
1679 if (done != sizeof (short))
1680 error ("error unsetting single step.");
1685 /* Single step (in a painstaking fashion) by inspecting the current
1686 instruction and setting a breakpoint on the "next" instruction
1687 which would be executed. This code hails from sh-stub.c.
1690 wince_software_single_step (unsigned int ignore, int insert_breakpoints_p)
1692 thread_info *th = current_thread; /* Info on currently selected thread */
1694 if (!insert_breakpoints_p)
1701 unsigned short opcode;
1703 LPCONTEXT c = &th->context;
1705 instrMem = (short *) c->Fir;
1707 read_process_memory (current_process_handle, (LPCVOID) c->Fir, &opcode,
1708 sizeof (opcode), &done);
1709 if (done != sizeof (opcode))
1710 error ("couldn't retrieve opcode");
1713 if ((opcode & COND_BR_MASK) == BT_INSTR)
1715 if (c->Psr & T_BIT_MASK)
1717 displacement = (opcode & COND_DISP) << 1;
1718 if (displacement & 0x80)
1719 displacement |= 0xffffff00;
1721 * Remember PC points to second instr.
1722 * after PC of branch ... so add 4
1724 instrMem = (short *) (c->Fir + displacement + 4);
1729 else if ((opcode & COND_BR_MASK) == BF_INSTR)
1731 if (c->Psr & T_BIT_MASK)
1735 displacement = (opcode & COND_DISP) << 1;
1736 if (displacement & 0x80)
1737 displacement |= 0xffffff00;
1739 * Remember PC points to second instr.
1740 * after PC of branch ... so add 4
1742 instrMem = (short *) (c->Fir + displacement + 4);
1745 else if ((opcode & UCOND_DBR_MASK) == BRA_INSTR)
1747 displacement = (opcode & UCOND_DISP) << 1;
1748 if (displacement & 0x0800)
1749 displacement |= 0xfffff000;
1752 * Remember PC points to second instr.
1753 * after PC of branch ... so add 4
1755 instrMem = (short *) (c->Fir + displacement + 4);
1757 else if ((opcode & UCOND_RBR_MASK) == JSR_INSTR)
1759 reg = (char) ((opcode & UCOND_REG) >> 8);
1761 instrMem = (short *) *regptr (c, reg);
1763 else if (opcode == RTS_INSTR)
1764 instrMem = (short *) c->PR;
1765 else if (opcode == RTE_INSTR)
1766 instrMem = (short *) *regptr (c, 15);
1767 else if ((opcode & TRAPA_MASK) == TRAPA_INSTR)
1768 instrMem = (short *) ((opcode & ~TRAPA_MASK) << 2);
1772 th->step_pc = (CORE_ADDR) instrMem;
1774 read_process_memory (current_process_handle, (LPVOID) instrMem,
1775 (LPVOID) & th->step_instr, sizeof (short), &done);
1776 opcode = SSTEP_INSTR;
1777 write_process_memory (current_process_handle, (LPVOID) instrMem,
1778 (LPVOID) & opcode, sizeof (short), &done);
1782 /* Single step (in a painstaking fashion) by inspecting the current
1783 instruction and setting a breakpoint on the "next" instruction
1784 which would be executed. This code hails from sh-stub.c.
1787 undoSStep (thread_info * th)
1791 memory_remove_breakpoint (th->step_pc, (void *) &th->step_prev);
1797 wince_software_single_step (unsigned int ignore, int insert_breakpoints_p)
1800 thread_info *th = current_thread; /* Info on currently selected thread */
1801 CORE_ADDR mips_next_pc (CORE_ADDR pc);
1803 if (!insert_breakpoints_p)
1810 pc = read_register (PC_REGNUM);
1811 th->step_pc = arm_get_next_pc (pc);
1813 memory_insert_breakpoint (th->step_pc, (void *) &th->step_prev);
1817 /* Resume the child after an exception. */
1819 child_resume (int pid, int step, enum target_signal sig)
1822 DWORD continue_status = last_sig > 0 && last_sig < NSIG ?
1823 DBG_EXCEPTION_NOT_HANDLED : DBG_CONTINUE;
1825 DEBUG_EXEC (("gdb: child_resume (pid=%d, step=%d, sig=%d);\n",
1828 /* Get context for currently selected thread */
1829 th = thread_rec (current_event.dwThreadId, FALSE);
1831 if (th->context.ContextFlags)
1833 CHECK (set_thread_context (th->h, &th->context));
1834 th->context.ContextFlags = 0;
1837 dcache_flush (remote_dcache);
1839 /* Allow continuing with the same signal that interrupted us.
1840 Otherwise complain. */
1841 if (sig && sig != last_sig)
1842 fprintf_unfiltered (gdb_stderr, "Can't send signals to the child. signal %d\n", sig);
1845 child_continue (continue_status, pid);
1849 child_prepare_to_store ()
1851 /* Do nothing, since we can store individual regs */
1863 DEBUG_EVENTS (("gdb: child_close, inferior_pid=%d\n", inferior_pid));
1866 /* Explicitly upload file to remotedir */
1869 child_load (char *file, int from_tty)
1871 upload_to_device (file, file);
1874 struct target_ops child_ops;
1877 init_child_ops (void)
1879 memset (&child_ops, 0, sizeof (child_ops));
1880 child_ops.to_shortname = (char *) "child";
1881 child_ops.to_longname = (char *) "Windows CE process";
1882 child_ops.to_doc = (char *) "Windows CE process (started by the \"run\" command).";
1883 child_ops.to_open = child_open;
1884 child_ops.to_close = child_close;
1885 child_ops.to_resume = child_resume;
1886 child_ops.to_wait = child_wait;
1887 child_ops.to_fetch_registers = child_fetch_inferior_registers;
1888 child_ops.to_store_registers = child_store_inferior_registers;
1889 child_ops.to_prepare_to_store = child_prepare_to_store;
1890 child_ops.to_xfer_memory = child_xfer_memory;
1891 child_ops.to_files_info = child_files_info;
1892 child_ops.to_insert_breakpoint = memory_insert_breakpoint;
1893 child_ops.to_remove_breakpoint = memory_remove_breakpoint;
1894 child_ops.to_terminal_init = terminal_init_inferior;
1895 child_ops.to_terminal_inferior = terminal_inferior;
1896 child_ops.to_terminal_ours_for_output = terminal_ours_for_output;
1897 child_ops.to_terminal_ours = terminal_ours;
1898 child_ops.to_terminal_info = child_terminal_info;
1899 child_ops.to_kill = child_kill_inferior;
1900 child_ops.to_load = child_load;
1901 child_ops.to_create_inferior = child_create_inferior;
1902 child_ops.to_mourn_inferior = child_mourn_inferior;
1903 child_ops.to_can_run = child_can_run;
1904 child_ops.to_thread_alive = win32_child_thread_alive;
1905 child_ops.to_stratum = process_stratum;
1906 child_ops.to_has_all_memory = 1;
1907 child_ops.to_has_memory = 1;
1908 child_ops.to_has_stack = 1;
1909 child_ops.to_has_registers = 1;
1910 child_ops.to_has_execution = 1;
1911 child_ops.to_sections = 0;
1912 child_ops.to_sections_end = 0;
1913 child_ops.to_magic = OPS_MAGIC;
1917 /* Handle 'set remoteupload' parameter. */
1919 #define replace_upload(what) \
1920 upload_when = UPLOAD_NEWER; \
1921 remote_upload = realloc (remote_upload, strlen (upload_options[upload_when].name)); \
1922 strcpy (remote_upload, upload_options[upload_when].name);
1925 set_upload_type (char *ignore, int from_tty)
1930 if (!remote_upload || !remote_upload[0])
1932 replace_upload (UPLOAD_NEWER);
1934 printf_unfiltered ("Upload upload_options are: always, newer, never.\n");
1938 len = strlen (remote_upload);
1939 for (i = 0; i < (sizeof (upload_options) / sizeof (upload_options[0])); i++)
1940 if (len >= upload_options[i].abbrev &&
1941 strncasecmp (remote_upload, upload_options[i].name, len) == 0)
1943 remote_upload = (char *) upload_options[i].name;
1948 bad_option = remote_upload;
1949 replace_upload (UPLOAD_NEWER);
1950 error ("Unknown upload type: %s.", bad_option);
1954 _initialize_inftarg ()
1956 struct cmd_list_element *set;
1960 (add_set_cmd ((char *) "remotedirectory", no_class,
1961 var_string_noescape, (char *) &remote_directory,
1962 (char *) "Set directory for remote upload.\n",
1965 remote_directory = xstrdup (remote_directory);
1967 set = add_set_cmd ((char *) "remoteupload", no_class,
1968 var_string_noescape, (char *) &remote_upload,
1969 (char *) "Set how to upload executables to remote device.\n",
1971 add_show_from_set (set, &showlist);
1972 set->function.cfunc = set_upload_type;
1973 set_upload_type (NULL, 0);
1974 set_dcache_state (1);
1977 (add_set_cmd ((char *) "debugexec", class_support, var_boolean,
1978 (char *) &debug_exec,
1979 (char *) "Set whether to display execution in child process.",
1984 (add_set_cmd ((char *) "remoteaddhost", class_support, var_boolean,
1985 (char *) &remote_add_host,
1986 (char *) "Set whether to add this host to remote stub arguments for\n
1987 debugging over a network.", &setlist),
1991 (add_set_cmd ((char *) "debugevents", class_support, var_boolean,
1992 (char *) &debug_events,
1993 (char *) "Set whether to display kernel events in child process.",
1998 (add_set_cmd ((char *) "debugmemory", class_support, var_boolean,
1999 (char *) &debug_memory,
2000 (char *) "Set whether to display memory accesses in child process.",
2005 (add_set_cmd ((char *) "debugexceptions", class_support, var_boolean,
2006 (char *) &debug_exceptions,
2007 (char *) "Set whether to display kernel exceptions in child process.",
2011 add_target (&child_ops);
2014 /* Determine if the thread referenced by "pid" is alive
2015 by "polling" it. If WaitForSingleObject returns WAIT_OBJECT_0
2016 it means that the pid has died. Otherwise it is assumed to be alive. */
2018 win32_child_thread_alive (int pid)
2020 return thread_alive (thread_rec (pid, FALSE)->h);
2023 /* Convert pid to printable format. */
2025 cygwin_pid_to_str (int pid)
2027 static char buf[80];
2028 if (pid == current_event.dwProcessId)
2029 sprintf (buf, "process %d", pid);
2031 sprintf (buf, "thread %d.0x%x", (unsigned) current_event.dwProcessId, pid);