1 /* Low level interface to Windows debugging, for gdbserver.
2 Copyright (C) 2006-2013 Free Software Foundation, Inc.
4 Contributed by Leo Zayas. Based on "win32-nat.c" from GDB.
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 3 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 even the 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, see <http://www.gnu.org/licenses/>. */
23 #include "gdb/signals.h"
24 #include "gdb/fileio.h"
25 #include "mem-break.h"
26 #include "win32-low.h"
27 #include "gdbthread.h"
40 #include <sys/cygwin.h>
43 #define OUTMSG(X) do { printf X; fflush (stderr); } while (0)
56 #define _T(x) TEXT (x)
60 #define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0]))
64 # define GETPROCADDRESS(DLL, PROC) \
65 ((winapi_ ## PROC) GetProcAddress (DLL, TEXT (#PROC)))
67 # define GETPROCADDRESS(DLL, PROC) \
68 ((winapi_ ## PROC) GetProcAddress (DLL, #PROC))
71 int using_threads
= 1;
74 static int attaching
= 0;
75 static HANDLE current_process_handle
= NULL
;
76 static DWORD current_process_id
= 0;
77 static DWORD main_thread_id
= 0;
78 static enum gdb_signal last_sig
= GDB_SIGNAL_0
;
80 /* The current debug event from WaitForDebugEvent. */
81 static DEBUG_EVENT current_event
;
83 /* A status that hasn't been reported to the core yet, and so
84 win32_wait should return it next, instead of fetching the next
85 debug event off the win32 API. */
86 static struct target_waitstatus cached_status
;
88 /* Non zero if an interrupt request is to be satisfied by suspending
90 static int soft_interrupt_requested
= 0;
92 /* Non zero if the inferior is stopped in a simulated breakpoint done
93 by suspending all the threads. */
94 static int faked_breakpoint
= 0;
96 const struct target_desc
*win32_tdesc
;
98 #define NUM_REGS (the_low_target.num_regs)
100 typedef BOOL (WINAPI
*winapi_DebugActiveProcessStop
) (DWORD dwProcessId
);
101 typedef BOOL (WINAPI
*winapi_DebugSetProcessKillOnExit
) (BOOL KillOnExit
);
102 typedef BOOL (WINAPI
*winapi_DebugBreakProcess
) (HANDLE
);
103 typedef BOOL (WINAPI
*winapi_GenerateConsoleCtrlEvent
) (DWORD
, DWORD
);
105 static ptid_t
win32_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
,
107 static void win32_resume (struct thread_resume
*resume_info
, size_t n
);
109 /* Get the thread ID from the current selected inferior (the current
112 current_inferior_ptid (void)
114 return ((struct inferior_list_entry
*) current_inferior
)->id
;
117 /* The current debug event from WaitForDebugEvent. */
119 debug_event_ptid (DEBUG_EVENT
*event
)
121 return ptid_build (event
->dwProcessId
, event
->dwThreadId
, 0);
124 /* Get the thread context of the thread associated with TH. */
127 win32_get_thread_context (win32_thread_info
*th
)
129 memset (&th
->context
, 0, sizeof (CONTEXT
));
130 (*the_low_target
.get_thread_context
) (th
, ¤t_event
);
132 memcpy (&th
->base_context
, &th
->context
, sizeof (CONTEXT
));
136 /* Set the thread context of the thread associated with TH. */
139 win32_set_thread_context (win32_thread_info
*th
)
142 /* Calling SuspendThread on a thread that is running kernel code
143 will report that the suspending was successful, but in fact, that
144 will often not be true. In those cases, the context returned by
145 GetThreadContext will not be correct by the time the thread
146 stops, hence we can't set that context back into the thread when
147 resuming - it will most likelly crash the inferior.
148 Unfortunately, there is no way to know when the thread will
149 really stop. To work around it, we'll only write the context
150 back to the thread when either the user or GDB explicitly change
151 it between stopping and resuming. */
152 if (memcmp (&th
->context
, &th
->base_context
, sizeof (CONTEXT
)) != 0)
154 (*the_low_target
.set_thread_context
) (th
, ¤t_event
);
157 /* Find a thread record given a thread id. If GET_CONTEXT is set then
158 also retrieve the context for this thread. */
159 static win32_thread_info
*
160 thread_rec (ptid_t ptid
, int get_context
)
162 struct thread_info
*thread
;
163 win32_thread_info
*th
;
165 thread
= (struct thread_info
*) find_inferior_id (&all_threads
, ptid
);
169 th
= inferior_target_data (thread
);
170 if (get_context
&& th
->context
.ContextFlags
== 0)
174 if (SuspendThread (th
->h
) == (DWORD
) -1)
176 DWORD err
= GetLastError ();
177 OUTMSG (("warning: SuspendThread failed in thread_rec, "
178 "(error %d): %s\n", (int) err
, strwinerror (err
)));
184 win32_get_thread_context (th
);
190 /* Add a thread to the thread list. */
191 static win32_thread_info
*
192 child_add_thread (DWORD pid
, DWORD tid
, HANDLE h
, void *tlb
)
194 win32_thread_info
*th
;
195 ptid_t ptid
= ptid_build (pid
, tid
, 0);
197 if ((th
= thread_rec (ptid
, FALSE
)))
200 th
= xcalloc (1, sizeof (*th
));
203 th
->thread_local_base
= (CORE_ADDR
) (uintptr_t) tlb
;
205 add_thread (ptid
, th
);
207 if (the_low_target
.thread_added
!= NULL
)
208 (*the_low_target
.thread_added
) (th
);
213 /* Delete a thread from the list of threads. */
215 delete_thread_info (struct inferior_list_entry
*thread
)
217 win32_thread_info
*th
= inferior_target_data ((struct thread_info
*) thread
);
219 remove_thread ((struct thread_info
*) thread
);
224 /* Delete a thread from the list of threads. */
226 child_delete_thread (DWORD pid
, DWORD tid
)
228 struct inferior_list_entry
*thread
;
231 /* If the last thread is exiting, just return. */
232 if (all_threads
.head
== all_threads
.tail
)
235 ptid
= ptid_build (pid
, tid
, 0);
236 thread
= find_inferior_id (&all_threads
, ptid
);
240 delete_thread_info (thread
);
243 /* These watchpoint related wrapper functions simply pass on the function call
244 if the low target has registered a corresponding function. */
247 win32_insert_point (char type
, CORE_ADDR addr
, int len
)
249 if (the_low_target
.insert_point
!= NULL
)
250 return the_low_target
.insert_point (type
, addr
, len
);
252 /* Unsupported (see target.h). */
257 win32_remove_point (char type
, CORE_ADDR addr
, int len
)
259 if (the_low_target
.remove_point
!= NULL
)
260 return the_low_target
.remove_point (type
, addr
, len
);
262 /* Unsupported (see target.h). */
267 win32_stopped_by_watchpoint (void)
269 if (the_low_target
.stopped_by_watchpoint
!= NULL
)
270 return the_low_target
.stopped_by_watchpoint ();
276 win32_stopped_data_address (void)
278 if (the_low_target
.stopped_data_address
!= NULL
)
279 return the_low_target
.stopped_data_address ();
285 /* Transfer memory from/to the debugged process. */
287 child_xfer_memory (CORE_ADDR memaddr
, char *our
, int len
,
288 int write
, struct target_ops
*target
)
293 uintptr_t addr
= (uintptr_t) memaddr
;
297 success
= WriteProcessMemory (current_process_handle
, (LPVOID
) addr
,
298 (LPCVOID
) our
, len
, &done
);
300 lasterror
= GetLastError ();
301 FlushInstructionCache (current_process_handle
, (LPCVOID
) addr
, len
);
305 success
= ReadProcessMemory (current_process_handle
, (LPCVOID
) addr
,
306 (LPVOID
) our
, len
, &done
);
308 lasterror
= GetLastError ();
310 if (!success
&& lasterror
== ERROR_PARTIAL_COPY
&& done
> 0)
313 return success
? done
: -1;
316 /* Clear out any old thread list and reinitialize it to a pristine
319 child_init_thread_list (void)
321 for_each_inferior (&all_threads
, delete_thread_info
);
325 do_initial_child_stuff (HANDLE proch
, DWORD pid
, int attached
)
327 struct process_info
*proc
;
329 last_sig
= GDB_SIGNAL_0
;
331 current_process_handle
= proch
;
332 current_process_id
= pid
;
335 soft_interrupt_requested
= 0;
336 faked_breakpoint
= 0;
338 memset (¤t_event
, 0, sizeof (current_event
));
340 proc
= add_process (pid
, attached
);
341 proc
->tdesc
= win32_tdesc
;
342 child_init_thread_list ();
344 if (the_low_target
.initial_stuff
!= NULL
)
345 (*the_low_target
.initial_stuff
) ();
347 cached_status
.kind
= TARGET_WAITKIND_IGNORE
;
349 /* Flush all currently pending debug events (thread and dll list) up
350 to the initial breakpoint. */
353 struct target_waitstatus status
;
355 win32_wait (minus_one_ptid
, &status
, 0);
357 /* Note win32_wait doesn't return thread events. */
358 if (status
.kind
!= TARGET_WAITKIND_LOADED
)
360 cached_status
= status
;
365 struct thread_resume resume
;
367 resume
.thread
= minus_one_ptid
;
368 resume
.kind
= resume_continue
;
371 win32_resume (&resume
, 1);
376 /* Resume all artificially suspended threads if we are continuing
379 continue_one_thread (struct inferior_list_entry
*this_thread
, void *id_ptr
)
381 struct thread_info
*thread
= (struct thread_info
*) this_thread
;
382 int thread_id
= * (int *) id_ptr
;
383 win32_thread_info
*th
= inferior_target_data (thread
);
385 if ((thread_id
== -1 || thread_id
== th
->tid
)
388 if (th
->context
.ContextFlags
)
390 win32_set_thread_context (th
);
391 th
->context
.ContextFlags
= 0;
394 if (ResumeThread (th
->h
) == (DWORD
) -1)
396 DWORD err
= GetLastError ();
397 OUTMSG (("warning: ResumeThread failed in continue_one_thread, "
398 "(error %d): %s\n", (int) err
, strwinerror (err
)));
407 child_continue (DWORD continue_status
, int thread_id
)
409 /* The inferior will only continue after the ContinueDebugEvent
411 find_inferior (&all_threads
, continue_one_thread
, &thread_id
);
412 faked_breakpoint
= 0;
414 if (!ContinueDebugEvent (current_event
.dwProcessId
,
415 current_event
.dwThreadId
,
422 /* Fetch register(s) from the current thread context. */
424 child_fetch_inferior_registers (struct regcache
*regcache
, int r
)
427 win32_thread_info
*th
= thread_rec (current_inferior_ptid (), TRUE
);
428 if (r
== -1 || r
> NUM_REGS
)
429 child_fetch_inferior_registers (regcache
, NUM_REGS
);
431 for (regno
= 0; regno
< r
; regno
++)
432 (*the_low_target
.fetch_inferior_register
) (regcache
, th
, regno
);
435 /* Store a new register value into the current thread context. We don't
436 change the program's context until later, when we resume it. */
438 child_store_inferior_registers (struct regcache
*regcache
, int r
)
441 win32_thread_info
*th
= thread_rec (current_inferior_ptid (), TRUE
);
442 if (r
== -1 || r
== 0 || r
> NUM_REGS
)
443 child_store_inferior_registers (regcache
, NUM_REGS
);
445 for (regno
= 0; regno
< r
; regno
++)
446 (*the_low_target
.store_inferior_register
) (regcache
, th
, regno
);
449 /* Map the Windows error number in ERROR to a locale-dependent error
450 message string and return a pointer to it. Typically, the values
451 for ERROR come from GetLastError.
453 The string pointed to shall not be modified by the application,
454 but may be overwritten by a subsequent call to strwinerror
456 The strwinerror function does not change the current setting
460 strwinerror (DWORD error
)
462 static char buf
[1024];
464 DWORD lasterr
= GetLastError ();
465 DWORD chars
= FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
466 | FORMAT_MESSAGE_ALLOCATE_BUFFER
,
469 0, /* Default language */
475 /* If there is an \r\n appended, zap it. */
477 && msgbuf
[chars
- 2] == '\r'
478 && msgbuf
[chars
- 1] == '\n')
484 if (chars
> ((COUNTOF (buf
)) - 1))
486 chars
= COUNTOF (buf
) - 1;
491 wcstombs (buf
, msgbuf
, chars
+ 1);
493 strncpy (buf
, msgbuf
, chars
+ 1);
498 sprintf (buf
, "unknown win32 error (%u)", (unsigned) error
);
500 SetLastError (lasterr
);
505 create_process (const char *program
, char *args
,
506 DWORD flags
, PROCESS_INFORMATION
*pi
)
511 wchar_t *p
, *wprogram
, *wargs
;
514 wprogram
= alloca ((strlen (program
) + 1) * sizeof (wchar_t));
515 mbstowcs (wprogram
, program
, strlen (program
) + 1);
517 for (p
= wprogram
; *p
; ++p
)
521 argslen
= strlen (args
);
522 wargs
= alloca ((argslen
+ 1) * sizeof (wchar_t));
523 mbstowcs (wargs
, args
, argslen
+ 1);
525 ret
= CreateProcessW (wprogram
, /* image name */
526 wargs
, /* command line */
527 NULL
, /* security, not supported */
528 NULL
, /* thread, not supported */
529 FALSE
, /* inherit handles, not supported */
530 flags
, /* start flags */
531 NULL
, /* environment, not supported */
532 NULL
, /* current directory, not supported */
533 NULL
, /* start info, not supported */
536 STARTUPINFOA si
= { sizeof (STARTUPINFOA
) };
538 ret
= CreateProcessA (program
, /* image name */
539 args
, /* command line */
542 TRUE
, /* inherit handles */
543 flags
, /* start flags */
544 NULL
, /* environment */
545 NULL
, /* current directory */
546 &si
, /* start info */
553 /* Start a new process.
554 PROGRAM is a path to the program to execute.
555 ARGS is a standard NULL-terminated array of arguments,
556 to be passed to the inferior as ``argv''.
557 Returns the new PID on success, -1 on failure. Registers the new
558 process with the process list. */
560 win32_create_inferior (char *program
, char **program_args
)
563 char real_path
[PATH_MAX
];
564 char *orig_path
, *new_path
, *path_ptr
;
571 PROCESS_INFORMATION pi
;
574 /* win32_wait needs to know we're not attaching. */
578 error ("No executable specified, specify executable to debug.\n");
580 flags
= DEBUG_PROCESS
| DEBUG_ONLY_THIS_PROCESS
;
584 path_ptr
= getenv ("PATH");
587 int size
= cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, NULL
, 0);
588 orig_path
= alloca (strlen (path_ptr
) + 1);
589 new_path
= alloca (size
);
590 strcpy (orig_path
, path_ptr
);
591 cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, new_path
, size
);
592 setenv ("PATH", new_path
, 1);
594 cygwin_conv_path (CCP_POSIX_TO_WIN_A
, program
, real_path
, PATH_MAX
);
599 for (argc
= 1; program_args
[argc
]; argc
++)
600 argslen
+= strlen (program_args
[argc
]) + 1;
601 args
= alloca (argslen
);
603 for (argc
= 1; program_args
[argc
]; argc
++)
605 /* FIXME: Can we do better about quoting? How does Cygwin
608 strcat (args
, program_args
[argc
]);
610 OUTMSG2 (("Command line is \"%s\"\n", args
));
612 #ifdef CREATE_NEW_PROCESS_GROUP
613 flags
|= CREATE_NEW_PROCESS_GROUP
;
616 ret
= create_process (program
, args
, flags
, &pi
);
617 err
= GetLastError ();
618 if (!ret
&& err
== ERROR_FILE_NOT_FOUND
)
620 char *exename
= alloca (strlen (program
) + 5);
621 strcat (strcpy (exename
, program
), ".exe");
622 ret
= create_process (exename
, args
, flags
, &pi
);
623 err
= GetLastError ();
628 setenv ("PATH", orig_path
, 1);
633 error ("Error creating process \"%s%s\", (error %d): %s\n",
634 program
, args
, (int) err
, strwinerror (err
));
638 OUTMSG2 (("Process created: %s\n", (char *) args
));
642 /* On Windows CE this handle can't be closed. The OS reuses
643 it in the debug events, while the 9x/NT versions of Windows
644 probably use a DuplicateHandle'd one. */
645 CloseHandle (pi
.hThread
);
648 do_initial_child_stuff (pi
.hProcess
, pi
.dwProcessId
, 0);
650 return current_process_id
;
653 /* Attach to a running process.
654 PID is the process ID to attach to, specified by the user
655 or a higher layer. */
657 win32_attach (unsigned long pid
)
660 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
663 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
665 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
667 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
669 h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
672 if (DebugActiveProcess (pid
))
674 if (DebugSetProcessKillOnExit
!= NULL
)
675 DebugSetProcessKillOnExit (FALSE
);
677 /* win32_wait needs to know we're attaching. */
679 do_initial_child_stuff (h
, pid
, 1);
686 err
= GetLastError ();
687 error ("Attach to process failed (error %d): %s\n",
688 (int) err
, strwinerror (err
));
691 /* Handle OUTPUT_DEBUG_STRING_EVENT from child process. */
693 handle_output_debug_string (struct target_waitstatus
*ourstatus
)
695 #define READ_BUFFER_LEN 1024
697 char s
[READ_BUFFER_LEN
+ 1] = { 0 };
698 DWORD nbytes
= current_event
.u
.DebugString
.nDebugStringLength
;
703 if (nbytes
> READ_BUFFER_LEN
)
704 nbytes
= READ_BUFFER_LEN
;
706 addr
= (CORE_ADDR
) (size_t) current_event
.u
.DebugString
.lpDebugStringData
;
708 if (current_event
.u
.DebugString
.fUnicode
)
710 /* The event tells us how many bytes, not chars, even
712 WCHAR buffer
[(READ_BUFFER_LEN
+ 1) / sizeof (WCHAR
)] = { 0 };
713 if (read_inferior_memory (addr
, (unsigned char *) buffer
, nbytes
) != 0)
715 wcstombs (s
, buffer
, (nbytes
+ 1) / sizeof (WCHAR
));
719 if (read_inferior_memory (addr
, (unsigned char *) s
, nbytes
) != 0)
723 if (strncmp (s
, "cYg", 3) != 0)
733 #undef READ_BUFFER_LEN
737 win32_clear_inferiors (void)
739 if (current_process_handle
!= NULL
)
740 CloseHandle (current_process_handle
);
742 for_each_inferior (&all_threads
, delete_thread_info
);
746 /* Kill all inferiors. */
750 struct process_info
*process
;
752 if (current_process_handle
== NULL
)
755 TerminateProcess (current_process_handle
, 0);
758 if (!child_continue (DBG_CONTINUE
, -1))
760 if (!WaitForDebugEvent (¤t_event
, INFINITE
))
762 if (current_event
.dwDebugEventCode
== EXIT_PROCESS_DEBUG_EVENT
)
764 else if (current_event
.dwDebugEventCode
== OUTPUT_DEBUG_STRING_EVENT
)
766 struct target_waitstatus our_status
= { 0 };
767 handle_output_debug_string (&our_status
);
771 win32_clear_inferiors ();
773 process
= find_process_pid (pid
);
774 remove_process (process
);
778 /* Detach from inferior PID. */
780 win32_detach (int pid
)
782 struct process_info
*process
;
783 winapi_DebugActiveProcessStop DebugActiveProcessStop
= NULL
;
784 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
786 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
788 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
790 DebugActiveProcessStop
= GETPROCADDRESS (dll
, DebugActiveProcessStop
);
791 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
793 if (DebugSetProcessKillOnExit
== NULL
794 || DebugActiveProcessStop
== NULL
)
798 struct thread_resume resume
;
799 resume
.thread
= minus_one_ptid
;
800 resume
.kind
= resume_continue
;
802 win32_resume (&resume
, 1);
805 if (!DebugActiveProcessStop (current_process_id
))
808 DebugSetProcessKillOnExit (FALSE
);
809 process
= find_process_pid (pid
);
810 remove_process (process
);
812 win32_clear_inferiors ();
817 win32_mourn (struct process_info
*process
)
819 remove_process (process
);
822 /* Wait for inferiors to end. */
826 HANDLE h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
829 WaitForSingleObject (h
, INFINITE
);
834 /* Return 1 iff the thread with thread ID TID is alive. */
836 win32_thread_alive (ptid_t ptid
)
840 /* Our thread list is reliable; don't bother to poll target
842 if (find_inferior_id (&all_threads
, ptid
) != NULL
)
849 /* Resume the inferior process. RESUME_INFO describes how we want
852 win32_resume (struct thread_resume
*resume_info
, size_t n
)
857 win32_thread_info
*th
;
858 DWORD continue_status
= DBG_CONTINUE
;
861 /* This handles the very limited set of resume packets that GDB can
862 currently produce. */
864 if (n
== 1 && ptid_equal (resume_info
[0].thread
, minus_one_ptid
))
869 /* Yes, we're ignoring resume_info[0].thread. It'd be tricky to make
870 the Windows resume code do the right thing for thread switching. */
871 tid
= current_event
.dwThreadId
;
873 if (!ptid_equal (resume_info
[0].thread
, minus_one_ptid
))
875 sig
= resume_info
[0].sig
;
876 step
= resume_info
[0].kind
== resume_step
;
884 if (sig
!= GDB_SIGNAL_0
)
886 if (current_event
.dwDebugEventCode
!= EXCEPTION_DEBUG_EVENT
)
888 OUTMSG (("Cannot continue with signal %d here.\n", sig
));
890 else if (sig
== last_sig
)
891 continue_status
= DBG_EXCEPTION_NOT_HANDLED
;
893 OUTMSG (("Can only continue with recieved signal %d.\n", last_sig
));
896 last_sig
= GDB_SIGNAL_0
;
898 /* Get context for the currently selected thread. */
899 ptid
= debug_event_ptid (¤t_event
);
900 th
= thread_rec (ptid
, FALSE
);
903 if (th
->context
.ContextFlags
)
905 /* Move register values from the inferior into the thread
906 context structure. */
907 regcache_invalidate ();
911 if (the_low_target
.single_step
!= NULL
)
912 (*the_low_target
.single_step
) (th
);
914 error ("Single stepping is not supported "
915 "in this configuration.\n");
918 win32_set_thread_context (th
);
919 th
->context
.ContextFlags
= 0;
923 /* Allow continuing with the same signal that interrupted us.
924 Otherwise complain. */
926 child_continue (continue_status
, tid
);
930 win32_add_one_solib (const char *name
, CORE_ADDR load_addr
)
932 char buf
[MAX_PATH
+ 1];
933 char buf2
[MAX_PATH
+ 1];
936 WIN32_FIND_DATA w32_fd
;
937 WCHAR wname
[MAX_PATH
+ 1];
938 mbstowcs (wname
, name
, MAX_PATH
);
939 HANDLE h
= FindFirstFile (wname
, &w32_fd
);
941 WIN32_FIND_DATAA w32_fd
;
942 HANDLE h
= FindFirstFileA (name
, &w32_fd
);
945 if (h
== INVALID_HANDLE_VALUE
)
953 char cwd
[MAX_PATH
+ 1];
955 if (GetCurrentDirectoryA (MAX_PATH
+ 1, cwd
))
957 p
= strrchr (buf
, '\\');
960 SetCurrentDirectoryA (buf
);
961 GetFullPathNameA (w32_fd
.cFileName
, MAX_PATH
, buf
, &p
);
962 SetCurrentDirectoryA (cwd
);
969 if (strcasecmp (buf
, "ntdll.dll") == 0)
971 GetSystemDirectoryA (buf
, sizeof (buf
));
972 strcat (buf
, "\\ntdll.dll");
977 cygwin_conv_path (CCP_WIN_A_TO_POSIX
, buf
, buf2
, sizeof (buf2
));
982 loaded_dll (buf2
, load_addr
);
986 get_image_name (HANDLE h
, void *address
, int unicode
)
988 static char buf
[(2 * MAX_PATH
) + 1];
989 DWORD size
= unicode
? sizeof (WCHAR
) : sizeof (char);
995 /* Attempt to read the name of the dll that was detected.
996 This is documented to work only when actively debugging
997 a program. It will not work for attached processes. */
1002 /* Windows CE reports the address of the image name,
1003 instead of an address of a pointer into the image name. */
1004 address_ptr
= address
;
1006 /* See if we could read the address of a string, and that the
1007 address isn't null. */
1008 if (!ReadProcessMemory (h
, address
, &address_ptr
,
1009 sizeof (address_ptr
), &done
)
1010 || done
!= sizeof (address_ptr
)
1015 /* Find the length of the string */
1016 while (ReadProcessMemory (h
, address_ptr
+ len
++ * size
, &b
, size
, &done
)
1017 && (b
[0] != 0 || b
[size
- 1] != 0) && done
== size
)
1021 ReadProcessMemory (h
, address_ptr
, buf
, len
, &done
);
1024 WCHAR
*unicode_address
= (WCHAR
*) alloca (len
* sizeof (WCHAR
));
1025 ReadProcessMemory (h
, address_ptr
, unicode_address
, len
* sizeof (WCHAR
),
1028 WideCharToMultiByte (CP_ACP
, 0, unicode_address
, len
, buf
, len
, 0, 0);
1034 typedef BOOL (WINAPI
*winapi_EnumProcessModules
) (HANDLE
, HMODULE
*,
1036 typedef BOOL (WINAPI
*winapi_GetModuleInformation
) (HANDLE
, HMODULE
,
1037 LPMODULEINFO
, DWORD
);
1038 typedef DWORD (WINAPI
*winapi_GetModuleFileNameExA
) (HANDLE
, HMODULE
,
1041 static winapi_EnumProcessModules win32_EnumProcessModules
;
1042 static winapi_GetModuleInformation win32_GetModuleInformation
;
1043 static winapi_GetModuleFileNameExA win32_GetModuleFileNameExA
;
1048 static int psapi_loaded
= 0;
1049 static HMODULE dll
= NULL
;
1054 dll
= LoadLibrary (TEXT("psapi.dll"));
1057 win32_EnumProcessModules
=
1058 GETPROCADDRESS (dll
, EnumProcessModules
);
1059 win32_GetModuleInformation
=
1060 GETPROCADDRESS (dll
, GetModuleInformation
);
1061 win32_GetModuleFileNameExA
=
1062 GETPROCADDRESS (dll
, GetModuleFileNameExA
);
1065 return (win32_EnumProcessModules
!= NULL
1066 && win32_GetModuleInformation
!= NULL
1067 && win32_GetModuleFileNameExA
!= NULL
);
1071 psapi_get_dll_name (LPVOID BaseAddress
, char *dll_name_ret
)
1077 HMODULE
*DllHandle
= dh_buf
;
1085 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1090 if (!ok
|| !cbNeeded
)
1093 DllHandle
= (HMODULE
*) alloca (cbNeeded
);
1097 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1104 for (i
= 0; i
< ((size_t) cbNeeded
/ sizeof (HMODULE
)); i
++)
1106 if (!(*win32_GetModuleInformation
) (current_process_handle
,
1111 DWORD err
= GetLastError ();
1112 error ("Can't get module info: (error %d): %s\n",
1113 (int) err
, strwinerror (err
));
1116 if (mi
.lpBaseOfDll
== BaseAddress
)
1118 len
= (*win32_GetModuleFileNameExA
) (current_process_handle
,
1124 DWORD err
= GetLastError ();
1125 error ("Error getting dll name: (error %d): %s\n",
1126 (int) err
, strwinerror (err
));
1133 dll_name_ret
[0] = '\0';
1137 typedef HANDLE (WINAPI
*winapi_CreateToolhelp32Snapshot
) (DWORD
, DWORD
);
1138 typedef BOOL (WINAPI
*winapi_Module32First
) (HANDLE
, LPMODULEENTRY32
);
1139 typedef BOOL (WINAPI
*winapi_Module32Next
) (HANDLE
, LPMODULEENTRY32
);
1141 static winapi_CreateToolhelp32Snapshot win32_CreateToolhelp32Snapshot
;
1142 static winapi_Module32First win32_Module32First
;
1143 static winapi_Module32Next win32_Module32Next
;
1145 typedef BOOL (WINAPI
*winapi_CloseToolhelp32Snapshot
) (HANDLE
);
1146 static winapi_CloseToolhelp32Snapshot win32_CloseToolhelp32Snapshot
;
1150 load_toolhelp (void)
1152 static int toolhelp_loaded
= 0;
1153 static HMODULE dll
= NULL
;
1155 if (!toolhelp_loaded
)
1157 toolhelp_loaded
= 1;
1159 dll
= GetModuleHandle (_T("KERNEL32.DLL"));
1161 dll
= LoadLibrary (L
"TOOLHELP.DLL");
1166 win32_CreateToolhelp32Snapshot
=
1167 GETPROCADDRESS (dll
, CreateToolhelp32Snapshot
);
1168 win32_Module32First
= GETPROCADDRESS (dll
, Module32First
);
1169 win32_Module32Next
= GETPROCADDRESS (dll
, Module32Next
);
1171 win32_CloseToolhelp32Snapshot
=
1172 GETPROCADDRESS (dll
, CloseToolhelp32Snapshot
);
1176 return (win32_CreateToolhelp32Snapshot
!= NULL
1177 && win32_Module32First
!= NULL
1178 && win32_Module32Next
!= NULL
1180 && win32_CloseToolhelp32Snapshot
!= NULL
1186 toolhelp_get_dll_name (LPVOID BaseAddress
, char *dll_name_ret
)
1188 HANDLE snapshot_module
;
1189 MODULEENTRY32 modEntry
= { sizeof (MODULEENTRY32
) };
1192 if (!load_toolhelp ())
1195 snapshot_module
= win32_CreateToolhelp32Snapshot (TH32CS_SNAPMODULE
,
1196 current_event
.dwProcessId
);
1197 if (snapshot_module
== INVALID_HANDLE_VALUE
)
1200 /* Ignore the first module, which is the exe. */
1201 if (win32_Module32First (snapshot_module
, &modEntry
))
1202 while (win32_Module32Next (snapshot_module
, &modEntry
))
1203 if (modEntry
.modBaseAddr
== BaseAddress
)
1206 wcstombs (dll_name_ret
, modEntry
.szExePath
, MAX_PATH
+ 1);
1208 strcpy (dll_name_ret
, modEntry
.szExePath
);
1215 win32_CloseToolhelp32Snapshot (snapshot_module
);
1217 CloseHandle (snapshot_module
);
1223 handle_load_dll (void)
1225 LOAD_DLL_DEBUG_INFO
*event
= ¤t_event
.u
.LoadDll
;
1226 char dll_buf
[MAX_PATH
+ 1];
1227 char *dll_name
= NULL
;
1228 CORE_ADDR load_addr
;
1230 dll_buf
[0] = dll_buf
[sizeof (dll_buf
) - 1] = '\0';
1232 /* Windows does not report the image name of the dlls in the debug
1233 event on attaches. We resort to iterating over the list of
1234 loaded dlls looking for a match by image base. */
1235 if (!psapi_get_dll_name (event
->lpBaseOfDll
, dll_buf
))
1237 if (!server_waiting
)
1238 /* On some versions of Windows and Windows CE, we can't create
1239 toolhelp snapshots while the inferior is stopped in a
1240 LOAD_DLL_DEBUG_EVENT due to a dll load, but we can while
1241 Windows is reporting the already loaded dlls. */
1242 toolhelp_get_dll_name (event
->lpBaseOfDll
, dll_buf
);
1247 if (*dll_name
== '\0')
1248 dll_name
= get_image_name (current_process_handle
,
1249 event
->lpImageName
, event
->fUnicode
);
1253 /* The symbols in a dll are offset by 0x1000, which is the
1254 offset from 0 of the first byte in an image - because
1255 of the file header and the section alignment. */
1257 load_addr
= (CORE_ADDR
) (uintptr_t) event
->lpBaseOfDll
+ 0x1000;
1258 win32_add_one_solib (dll_name
, load_addr
);
1262 handle_unload_dll (void)
1264 CORE_ADDR load_addr
=
1265 (CORE_ADDR
) (uintptr_t) current_event
.u
.UnloadDll
.lpBaseOfDll
;
1266 load_addr
+= 0x1000;
1267 unloaded_dll (NULL
, load_addr
);
1271 handle_exception (struct target_waitstatus
*ourstatus
)
1273 DWORD code
= current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
;
1275 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
1279 case EXCEPTION_ACCESS_VIOLATION
:
1280 OUTMSG2 (("EXCEPTION_ACCESS_VIOLATION"));
1281 ourstatus
->value
.sig
= GDB_SIGNAL_SEGV
;
1283 case STATUS_STACK_OVERFLOW
:
1284 OUTMSG2 (("STATUS_STACK_OVERFLOW"));
1285 ourstatus
->value
.sig
= GDB_SIGNAL_SEGV
;
1287 case STATUS_FLOAT_DENORMAL_OPERAND
:
1288 OUTMSG2 (("STATUS_FLOAT_DENORMAL_OPERAND"));
1289 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1291 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
1292 OUTMSG2 (("EXCEPTION_ARRAY_BOUNDS_EXCEEDED"));
1293 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1295 case STATUS_FLOAT_INEXACT_RESULT
:
1296 OUTMSG2 (("STATUS_FLOAT_INEXACT_RESULT"));
1297 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1299 case STATUS_FLOAT_INVALID_OPERATION
:
1300 OUTMSG2 (("STATUS_FLOAT_INVALID_OPERATION"));
1301 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1303 case STATUS_FLOAT_OVERFLOW
:
1304 OUTMSG2 (("STATUS_FLOAT_OVERFLOW"));
1305 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1307 case STATUS_FLOAT_STACK_CHECK
:
1308 OUTMSG2 (("STATUS_FLOAT_STACK_CHECK"));
1309 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1311 case STATUS_FLOAT_UNDERFLOW
:
1312 OUTMSG2 (("STATUS_FLOAT_UNDERFLOW"));
1313 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1315 case STATUS_FLOAT_DIVIDE_BY_ZERO
:
1316 OUTMSG2 (("STATUS_FLOAT_DIVIDE_BY_ZERO"));
1317 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1319 case STATUS_INTEGER_DIVIDE_BY_ZERO
:
1320 OUTMSG2 (("STATUS_INTEGER_DIVIDE_BY_ZERO"));
1321 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1323 case STATUS_INTEGER_OVERFLOW
:
1324 OUTMSG2 (("STATUS_INTEGER_OVERFLOW"));
1325 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1327 case EXCEPTION_BREAKPOINT
:
1328 OUTMSG2 (("EXCEPTION_BREAKPOINT"));
1329 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1331 /* Remove the initial breakpoint. */
1332 check_breakpoints ((CORE_ADDR
) (long) current_event
1333 .u
.Exception
.ExceptionRecord
.ExceptionAddress
);
1337 OUTMSG2 (("DBG_CONTROL_C"));
1338 ourstatus
->value
.sig
= GDB_SIGNAL_INT
;
1340 case DBG_CONTROL_BREAK
:
1341 OUTMSG2 (("DBG_CONTROL_BREAK"));
1342 ourstatus
->value
.sig
= GDB_SIGNAL_INT
;
1344 case EXCEPTION_SINGLE_STEP
:
1345 OUTMSG2 (("EXCEPTION_SINGLE_STEP"));
1346 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1348 case EXCEPTION_ILLEGAL_INSTRUCTION
:
1349 OUTMSG2 (("EXCEPTION_ILLEGAL_INSTRUCTION"));
1350 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1352 case EXCEPTION_PRIV_INSTRUCTION
:
1353 OUTMSG2 (("EXCEPTION_PRIV_INSTRUCTION"));
1354 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1356 case EXCEPTION_NONCONTINUABLE_EXCEPTION
:
1357 OUTMSG2 (("EXCEPTION_NONCONTINUABLE_EXCEPTION"));
1358 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1361 if (current_event
.u
.Exception
.dwFirstChance
)
1363 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1366 OUTMSG2 (("gdbserver: unknown target exception 0x%08x at 0x%s",
1367 (unsigned) current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
,
1368 phex_nz ((uintptr_t) current_event
.u
.Exception
.ExceptionRecord
.
1369 ExceptionAddress
, sizeof (uintptr_t))));
1370 ourstatus
->value
.sig
= GDB_SIGNAL_UNKNOWN
;
1374 last_sig
= ourstatus
->value
.sig
;
1379 suspend_one_thread (struct inferior_list_entry
*entry
)
1381 struct thread_info
*thread
= (struct thread_info
*) entry
;
1382 win32_thread_info
*th
= inferior_target_data (thread
);
1386 if (SuspendThread (th
->h
) == (DWORD
) -1)
1388 DWORD err
= GetLastError ();
1389 OUTMSG (("warning: SuspendThread failed in suspend_one_thread, "
1390 "(error %d): %s\n", (int) err
, strwinerror (err
)));
1398 fake_breakpoint_event (void)
1400 OUTMSG2(("fake_breakpoint_event\n"));
1402 faked_breakpoint
= 1;
1404 memset (¤t_event
, 0, sizeof (current_event
));
1405 current_event
.dwThreadId
= main_thread_id
;
1406 current_event
.dwDebugEventCode
= EXCEPTION_DEBUG_EVENT
;
1407 current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
1408 = EXCEPTION_BREAKPOINT
;
1410 for_each_inferior (&all_threads
, suspend_one_thread
);
1415 auto_delete_breakpoint (CORE_ADDR stop_pc
)
1421 /* Get the next event from the child. */
1424 get_child_debug_event (struct target_waitstatus
*ourstatus
)
1428 last_sig
= GDB_SIGNAL_0
;
1429 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1431 /* Check if GDB sent us an interrupt request. */
1432 check_remote_input_interrupt_request ();
1434 if (soft_interrupt_requested
)
1436 soft_interrupt_requested
= 0;
1437 fake_breakpoint_event ();
1446 /* WinCE doesn't set an initial breakpoint automatically. To
1447 stop the inferior, we flush all currently pending debug
1448 events -- the thread list and the dll list are always
1449 reported immediatelly without delay, then, we suspend all
1450 threads and pretend we saw a trap at the current PC of the
1453 Contrary to desktop Windows, Windows CE *does* report the dll
1454 names on LOAD_DLL_DEBUG_EVENTs resulting from a
1455 DebugActiveProcess call. This limits the way we can detect
1456 if all the dlls have already been reported. If we get a real
1457 debug event before leaving attaching, the worst that will
1458 happen is the user will see a spurious breakpoint. */
1460 current_event
.dwDebugEventCode
= 0;
1461 if (!WaitForDebugEvent (¤t_event
, 0))
1463 OUTMSG2(("no attach events left\n"));
1464 fake_breakpoint_event ();
1468 OUTMSG2(("got attach event\n"));
1473 /* Keep the wait time low enough for confortable remote
1474 interruption, but high enough so gdbserver doesn't become a
1476 if (!WaitForDebugEvent (¤t_event
, 250))
1478 DWORD e
= GetLastError();
1480 if (e
== ERROR_PIPE_NOT_CONNECTED
)
1482 /* This will happen if the loader fails to succesfully
1483 load the application, e.g., if the main executable
1484 tries to pull in a non-existing export from a
1486 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1487 ourstatus
->value
.integer
= 1;
1497 switch (current_event
.dwDebugEventCode
)
1499 case CREATE_THREAD_DEBUG_EVENT
:
1500 OUTMSG2 (("gdbserver: kernel event CREATE_THREAD_DEBUG_EVENT "
1501 "for pid=%u tid=%x)\n",
1502 (unsigned) current_event
.dwProcessId
,
1503 (unsigned) current_event
.dwThreadId
));
1505 /* Record the existence of this thread. */
1506 child_add_thread (current_event
.dwProcessId
,
1507 current_event
.dwThreadId
,
1508 current_event
.u
.CreateThread
.hThread
,
1509 current_event
.u
.CreateThread
.lpThreadLocalBase
);
1512 case EXIT_THREAD_DEBUG_EVENT
:
1513 OUTMSG2 (("gdbserver: kernel event EXIT_THREAD_DEBUG_EVENT "
1514 "for pid=%u tid=%x\n",
1515 (unsigned) current_event
.dwProcessId
,
1516 (unsigned) current_event
.dwThreadId
));
1517 child_delete_thread (current_event
.dwProcessId
,
1518 current_event
.dwThreadId
);
1520 current_inferior
= (struct thread_info
*) all_threads
.head
;
1523 case CREATE_PROCESS_DEBUG_EVENT
:
1524 OUTMSG2 (("gdbserver: kernel event CREATE_PROCESS_DEBUG_EVENT "
1525 "for pid=%u tid=%x\n",
1526 (unsigned) current_event
.dwProcessId
,
1527 (unsigned) current_event
.dwThreadId
));
1528 CloseHandle (current_event
.u
.CreateProcessInfo
.hFile
);
1530 current_process_handle
= current_event
.u
.CreateProcessInfo
.hProcess
;
1531 main_thread_id
= current_event
.dwThreadId
;
1533 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
1534 ourstatus
->value
.execd_pathname
= "Main executable";
1536 /* Add the main thread. */
1537 child_add_thread (current_event
.dwProcessId
,
1539 current_event
.u
.CreateProcessInfo
.hThread
,
1540 current_event
.u
.CreateProcessInfo
.lpThreadLocalBase
);
1542 ourstatus
->value
.related_pid
= debug_event_ptid (¤t_event
);
1546 /* Windows CE doesn't set the initial breakpoint
1547 automatically like the desktop versions of Windows do.
1548 We add it explicitly here. It will be removed as soon as
1550 set_breakpoint_at ((CORE_ADDR
) (long) current_event
.u
1551 .CreateProcessInfo
.lpStartAddress
,
1552 auto_delete_breakpoint
);
1557 case EXIT_PROCESS_DEBUG_EVENT
:
1558 OUTMSG2 (("gdbserver: kernel event EXIT_PROCESS_DEBUG_EVENT "
1559 "for pid=%u tid=%x\n",
1560 (unsigned) current_event
.dwProcessId
,
1561 (unsigned) current_event
.dwThreadId
));
1562 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1563 ourstatus
->value
.integer
= current_event
.u
.ExitProcess
.dwExitCode
;
1564 child_continue (DBG_CONTINUE
, -1);
1565 CloseHandle (current_process_handle
);
1566 current_process_handle
= NULL
;
1569 case LOAD_DLL_DEBUG_EVENT
:
1570 OUTMSG2 (("gdbserver: kernel event LOAD_DLL_DEBUG_EVENT "
1571 "for pid=%u tid=%x\n",
1572 (unsigned) current_event
.dwProcessId
,
1573 (unsigned) current_event
.dwThreadId
));
1574 CloseHandle (current_event
.u
.LoadDll
.hFile
);
1577 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1578 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1581 case UNLOAD_DLL_DEBUG_EVENT
:
1582 OUTMSG2 (("gdbserver: kernel event UNLOAD_DLL_DEBUG_EVENT "
1583 "for pid=%u tid=%x\n",
1584 (unsigned) current_event
.dwProcessId
,
1585 (unsigned) current_event
.dwThreadId
));
1586 handle_unload_dll ();
1587 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1588 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1591 case EXCEPTION_DEBUG_EVENT
:
1592 OUTMSG2 (("gdbserver: kernel event EXCEPTION_DEBUG_EVENT "
1593 "for pid=%u tid=%x\n",
1594 (unsigned) current_event
.dwProcessId
,
1595 (unsigned) current_event
.dwThreadId
));
1596 handle_exception (ourstatus
);
1599 case OUTPUT_DEBUG_STRING_EVENT
:
1600 /* A message from the kernel (or Cygwin). */
1601 OUTMSG2 (("gdbserver: kernel event OUTPUT_DEBUG_STRING_EVENT "
1602 "for pid=%u tid=%x\n",
1603 (unsigned) current_event
.dwProcessId
,
1604 (unsigned) current_event
.dwThreadId
));
1605 handle_output_debug_string (ourstatus
);
1609 OUTMSG2 (("gdbserver: kernel event unknown "
1610 "for pid=%u tid=%x code=%x\n",
1611 (unsigned) current_event
.dwProcessId
,
1612 (unsigned) current_event
.dwThreadId
,
1613 (unsigned) current_event
.dwDebugEventCode
));
1617 ptid
= debug_event_ptid (¤t_event
);
1619 (struct thread_info
*) find_inferior_id (&all_threads
, ptid
);
1623 /* Wait for the inferior process to change state.
1624 STATUS will be filled in with a response code to send to GDB.
1625 Returns the signal which caused the process to stop. */
1627 win32_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
, int options
)
1629 struct regcache
*regcache
;
1631 if (cached_status
.kind
!= TARGET_WAITKIND_IGNORE
)
1633 /* The core always does a wait after creating the inferior, and
1634 do_initial_child_stuff already ran the inferior to the
1635 initial breakpoint (or an exit, if creating the process
1636 fails). Report it now. */
1637 *ourstatus
= cached_status
;
1638 cached_status
.kind
= TARGET_WAITKIND_IGNORE
;
1639 return debug_event_ptid (¤t_event
);
1644 if (!get_child_debug_event (ourstatus
))
1647 switch (ourstatus
->kind
)
1649 case TARGET_WAITKIND_EXITED
:
1650 OUTMSG2 (("Child exited with retcode = %x\n",
1651 ourstatus
->value
.integer
));
1652 win32_clear_inferiors ();
1653 return pid_to_ptid (current_event
.dwProcessId
);
1654 case TARGET_WAITKIND_STOPPED
:
1655 case TARGET_WAITKIND_LOADED
:
1656 OUTMSG2 (("Child Stopped with signal = %d \n",
1657 ourstatus
->value
.sig
));
1659 regcache
= get_thread_regcache (current_inferior
, 1);
1660 child_fetch_inferior_registers (regcache
, -1);
1661 return debug_event_ptid (¤t_event
);
1663 OUTMSG (("Ignoring unknown internal event, %d\n", ourstatus
->kind
));
1665 case TARGET_WAITKIND_SPURIOUS
:
1666 case TARGET_WAITKIND_EXECD
:
1667 /* do nothing, just continue */
1668 child_continue (DBG_CONTINUE
, -1);
1674 /* Fetch registers from the inferior process.
1675 If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO. */
1677 win32_fetch_inferior_registers (struct regcache
*regcache
, int regno
)
1679 child_fetch_inferior_registers (regcache
, regno
);
1682 /* Store registers to the inferior process.
1683 If REGNO is -1, store all registers; otherwise, store at least REGNO. */
1685 win32_store_inferior_registers (struct regcache
*regcache
, int regno
)
1687 child_store_inferior_registers (regcache
, regno
);
1690 /* Read memory from the inferior process. This should generally be
1691 called through read_inferior_memory, which handles breakpoint shadowing.
1692 Read LEN bytes at MEMADDR into a buffer at MYADDR. */
1694 win32_read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
1696 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 0, 0) != len
;
1699 /* Write memory to the inferior process. This should generally be
1700 called through write_inferior_memory, which handles breakpoint shadowing.
1701 Write LEN bytes from the buffer at MYADDR to MEMADDR.
1702 Returns 0 on success and errno on failure. */
1704 win32_write_inferior_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
,
1707 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 1, 0) != len
;
1710 /* Send an interrupt request to the inferior process. */
1712 win32_request_interrupt (void)
1714 winapi_DebugBreakProcess DebugBreakProcess
;
1715 winapi_GenerateConsoleCtrlEvent GenerateConsoleCtrlEvent
;
1718 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
1720 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
1723 GenerateConsoleCtrlEvent
= GETPROCADDRESS (dll
, GenerateConsoleCtrlEvent
);
1725 if (GenerateConsoleCtrlEvent
!= NULL
1726 && GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT
, current_process_id
))
1729 /* GenerateConsoleCtrlEvent can fail if process id being debugged is
1730 not a process group id.
1731 Fallback to XP/Vista 'DebugBreakProcess', which generates a
1732 breakpoint exception in the interior process. */
1734 DebugBreakProcess
= GETPROCADDRESS (dll
, DebugBreakProcess
);
1736 if (DebugBreakProcess
!= NULL
1737 && DebugBreakProcess (current_process_handle
))
1740 /* Last resort, suspend all threads manually. */
1741 soft_interrupt_requested
= 1;
1746 win32_error_to_fileio_error (DWORD err
)
1750 case ERROR_BAD_PATHNAME
:
1751 case ERROR_FILE_NOT_FOUND
:
1752 case ERROR_INVALID_NAME
:
1753 case ERROR_PATH_NOT_FOUND
:
1754 return FILEIO_ENOENT
;
1756 case ERROR_IO_DEVICE
:
1757 case ERROR_OPEN_FAILED
:
1759 case ERROR_INVALID_HANDLE
:
1760 return FILEIO_EBADF
;
1761 case ERROR_ACCESS_DENIED
:
1762 case ERROR_SHARING_VIOLATION
:
1763 return FILEIO_EACCES
;
1764 case ERROR_NOACCESS
:
1765 return FILEIO_EFAULT
;
1767 return FILEIO_EBUSY
;
1768 case ERROR_ALREADY_EXISTS
:
1769 case ERROR_FILE_EXISTS
:
1770 return FILEIO_EEXIST
;
1771 case ERROR_BAD_DEVICE
:
1772 return FILEIO_ENODEV
;
1773 case ERROR_DIRECTORY
:
1774 return FILEIO_ENOTDIR
;
1775 case ERROR_FILENAME_EXCED_RANGE
:
1776 case ERROR_INVALID_DATA
:
1777 case ERROR_INVALID_PARAMETER
:
1778 case ERROR_NEGATIVE_SEEK
:
1779 return FILEIO_EINVAL
;
1780 case ERROR_TOO_MANY_OPEN_FILES
:
1781 return FILEIO_EMFILE
;
1782 case ERROR_HANDLE_DISK_FULL
:
1783 case ERROR_DISK_FULL
:
1784 return FILEIO_ENOSPC
;
1785 case ERROR_WRITE_PROTECT
:
1786 return FILEIO_EROFS
;
1787 case ERROR_NOT_SUPPORTED
:
1788 return FILEIO_ENOSYS
;
1791 return FILEIO_EUNKNOWN
;
1795 wince_hostio_last_error (char *buf
)
1797 DWORD winerr
= GetLastError ();
1798 int fileio_err
= win32_error_to_fileio_error (winerr
);
1799 sprintf (buf
, "F-1,%x", fileio_err
);
1803 /* Write Windows OS Thread Information Block address. */
1806 win32_get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
)
1808 win32_thread_info
*th
;
1809 th
= thread_rec (ptid
, 0);
1813 *addr
= th
->thread_local_base
;
1817 static struct target_ops win32_target_ops
= {
1818 win32_create_inferior
,
1827 win32_fetch_inferior_registers
,
1828 win32_store_inferior_registers
,
1829 NULL
, /* prepare_to_access_memory */
1830 NULL
, /* done_accessing_memory */
1831 win32_read_inferior_memory
,
1832 win32_write_inferior_memory
,
1833 NULL
, /* lookup_symbols */
1834 win32_request_interrupt
,
1835 NULL
, /* read_auxv */
1838 win32_stopped_by_watchpoint
,
1839 win32_stopped_data_address
,
1840 NULL
, /* read_offsets */
1841 NULL
, /* get_tls_address */
1842 NULL
, /* qxfer_spu */
1844 wince_hostio_last_error
,
1846 hostio_last_error_from_errno
,
1848 NULL
, /* qxfer_osdata */
1849 NULL
, /* qxfer_siginfo */
1850 NULL
, /* supports_non_stop */
1852 NULL
, /* start_non_stop */
1853 NULL
, /* supports_multi_process */
1854 NULL
, /* handle_monitor_command */
1855 NULL
, /* core_of_thread */
1856 NULL
, /* read_loadmap */
1857 NULL
, /* process_qsupported */
1858 NULL
, /* supports_tracepoints */
1860 NULL
, /* write_pc */
1861 NULL
, /* thread_stopped */
1862 win32_get_tib_address
1865 /* Initialize the Win32 backend. */
1867 initialize_low (void)
1869 set_target_ops (&win32_target_ops
);
1870 if (the_low_target
.breakpoint
!= NULL
)
1871 set_breakpoint_data (the_low_target
.breakpoint
,
1872 the_low_target
.breakpoint_len
);
1873 the_low_target
.arch_setup ();