1 /* Low level interface to Windows debugging, for gdbserver.
2 Copyright (C) 2006-2014 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 static void win32_add_all_dlls (void);
112 /* Get the thread ID from the current selected inferior (the current
115 current_inferior_ptid (void)
120 /* The current debug event from WaitForDebugEvent. */
122 debug_event_ptid (DEBUG_EVENT
*event
)
124 return ptid_build (event
->dwProcessId
, event
->dwThreadId
, 0);
127 /* Get the thread context of the thread associated with TH. */
130 win32_get_thread_context (win32_thread_info
*th
)
132 memset (&th
->context
, 0, sizeof (CONTEXT
));
133 (*the_low_target
.get_thread_context
) (th
, ¤t_event
);
135 memcpy (&th
->base_context
, &th
->context
, sizeof (CONTEXT
));
139 /* Set the thread context of the thread associated with TH. */
142 win32_set_thread_context (win32_thread_info
*th
)
145 /* Calling SuspendThread on a thread that is running kernel code
146 will report that the suspending was successful, but in fact, that
147 will often not be true. In those cases, the context returned by
148 GetThreadContext will not be correct by the time the thread
149 stops, hence we can't set that context back into the thread when
150 resuming - it will most likelly crash the inferior.
151 Unfortunately, there is no way to know when the thread will
152 really stop. To work around it, we'll only write the context
153 back to the thread when either the user or GDB explicitly change
154 it between stopping and resuming. */
155 if (memcmp (&th
->context
, &th
->base_context
, sizeof (CONTEXT
)) != 0)
157 (*the_low_target
.set_thread_context
) (th
, ¤t_event
);
160 /* Find a thread record given a thread id. If GET_CONTEXT is set then
161 also retrieve the context for this thread. */
162 static win32_thread_info
*
163 thread_rec (ptid_t ptid
, int get_context
)
165 struct thread_info
*thread
;
166 win32_thread_info
*th
;
168 thread
= (struct thread_info
*) find_inferior_id (&all_threads
, ptid
);
172 th
= inferior_target_data (thread
);
173 if (get_context
&& th
->context
.ContextFlags
== 0)
177 if (SuspendThread (th
->h
) == (DWORD
) -1)
179 DWORD err
= GetLastError ();
180 OUTMSG (("warning: SuspendThread failed in thread_rec, "
181 "(error %d): %s\n", (int) err
, strwinerror (err
)));
187 win32_get_thread_context (th
);
193 /* Add a thread to the thread list. */
194 static win32_thread_info
*
195 child_add_thread (DWORD pid
, DWORD tid
, HANDLE h
, void *tlb
)
197 win32_thread_info
*th
;
198 ptid_t ptid
= ptid_build (pid
, tid
, 0);
200 if ((th
= thread_rec (ptid
, FALSE
)))
203 th
= xcalloc (1, sizeof (*th
));
206 th
->thread_local_base
= (CORE_ADDR
) (uintptr_t) tlb
;
208 add_thread (ptid
, th
);
210 if (the_low_target
.thread_added
!= NULL
)
211 (*the_low_target
.thread_added
) (th
);
216 /* Delete a thread from the list of threads. */
218 delete_thread_info (struct inferior_list_entry
*thread
)
220 win32_thread_info
*th
= inferior_target_data ((struct thread_info
*) thread
);
222 remove_thread ((struct thread_info
*) thread
);
227 /* Delete a thread from the list of threads. */
229 child_delete_thread (DWORD pid
, DWORD tid
)
231 struct inferior_list_entry
*thread
;
234 /* If the last thread is exiting, just return. */
235 if (one_inferior_p (&all_threads
))
238 ptid
= ptid_build (pid
, tid
, 0);
239 thread
= find_inferior_id (&all_threads
, ptid
);
243 delete_thread_info (thread
);
246 /* These watchpoint related wrapper functions simply pass on the function call
247 if the low target has registered a corresponding function. */
250 win32_insert_point (char type
, CORE_ADDR addr
, int len
)
252 if (the_low_target
.insert_point
!= NULL
)
253 return the_low_target
.insert_point (type
, addr
, len
);
255 /* Unsupported (see target.h). */
260 win32_remove_point (char type
, CORE_ADDR addr
, int len
)
262 if (the_low_target
.remove_point
!= NULL
)
263 return the_low_target
.remove_point (type
, addr
, len
);
265 /* Unsupported (see target.h). */
270 win32_stopped_by_watchpoint (void)
272 if (the_low_target
.stopped_by_watchpoint
!= NULL
)
273 return the_low_target
.stopped_by_watchpoint ();
279 win32_stopped_data_address (void)
281 if (the_low_target
.stopped_data_address
!= NULL
)
282 return the_low_target
.stopped_data_address ();
288 /* Transfer memory from/to the debugged process. */
290 child_xfer_memory (CORE_ADDR memaddr
, char *our
, int len
,
291 int write
, struct target_ops
*target
)
296 uintptr_t addr
= (uintptr_t) memaddr
;
300 success
= WriteProcessMemory (current_process_handle
, (LPVOID
) addr
,
301 (LPCVOID
) our
, len
, &done
);
303 lasterror
= GetLastError ();
304 FlushInstructionCache (current_process_handle
, (LPCVOID
) addr
, len
);
308 success
= ReadProcessMemory (current_process_handle
, (LPCVOID
) addr
,
309 (LPVOID
) our
, len
, &done
);
311 lasterror
= GetLastError ();
313 if (!success
&& lasterror
== ERROR_PARTIAL_COPY
&& done
> 0)
316 return success
? done
: -1;
319 /* Clear out any old thread list and reinitialize it to a pristine
322 child_init_thread_list (void)
324 for_each_inferior (&all_threads
, delete_thread_info
);
327 /* Zero during the child initialization phase, and nonzero otherwise. */
329 static int child_initialization_done
= 0;
332 do_initial_child_stuff (HANDLE proch
, DWORD pid
, int attached
)
334 struct process_info
*proc
;
336 last_sig
= GDB_SIGNAL_0
;
338 current_process_handle
= proch
;
339 current_process_id
= pid
;
342 soft_interrupt_requested
= 0;
343 faked_breakpoint
= 0;
345 memset (¤t_event
, 0, sizeof (current_event
));
347 proc
= add_process (pid
, attached
);
348 proc
->tdesc
= win32_tdesc
;
349 child_init_thread_list ();
350 child_initialization_done
= 0;
352 if (the_low_target
.initial_stuff
!= NULL
)
353 (*the_low_target
.initial_stuff
) ();
355 cached_status
.kind
= TARGET_WAITKIND_IGNORE
;
357 /* Flush all currently pending debug events (thread and dll list) up
358 to the initial breakpoint. */
361 struct target_waitstatus status
;
363 win32_wait (minus_one_ptid
, &status
, 0);
365 /* Note win32_wait doesn't return thread events. */
366 if (status
.kind
!= TARGET_WAITKIND_LOADED
)
368 cached_status
= status
;
373 struct thread_resume resume
;
375 resume
.thread
= minus_one_ptid
;
376 resume
.kind
= resume_continue
;
379 win32_resume (&resume
, 1);
384 /* Now that the inferior has been started and all DLLs have been mapped,
385 we can iterate over all DLLs and load them in.
387 We avoid doing it any earlier because, on certain versions of Windows,
388 LOAD_DLL_DEBUG_EVENTs are sometimes not complete. In particular,
389 we have seen on Windows 8.1 that the ntdll.dll load event does not
390 include the DLL name, preventing us from creating an associated SO.
391 A possible explanation is that ntdll.dll might be mapped before
392 the SO info gets created by the Windows system -- ntdll.dll is
393 the first DLL to be reported via LOAD_DLL_DEBUG_EVENT and other DLLs
394 do not seem to suffer from that problem.
396 Rather than try to work around this sort of issue, it is much
397 simpler to just ignore DLL load/unload events during the startup
398 phase, and then process them all in one batch now. */
399 win32_add_all_dlls ();
402 child_initialization_done
= 1;
405 /* Resume all artificially suspended threads if we are continuing
408 continue_one_thread (struct inferior_list_entry
*this_thread
, void *id_ptr
)
410 struct thread_info
*thread
= (struct thread_info
*) this_thread
;
411 int thread_id
= * (int *) id_ptr
;
412 win32_thread_info
*th
= inferior_target_data (thread
);
414 if ((thread_id
== -1 || thread_id
== th
->tid
)
417 if (th
->context
.ContextFlags
)
419 win32_set_thread_context (th
);
420 th
->context
.ContextFlags
= 0;
423 if (ResumeThread (th
->h
) == (DWORD
) -1)
425 DWORD err
= GetLastError ();
426 OUTMSG (("warning: ResumeThread failed in continue_one_thread, "
427 "(error %d): %s\n", (int) err
, strwinerror (err
)));
436 child_continue (DWORD continue_status
, int thread_id
)
438 /* The inferior will only continue after the ContinueDebugEvent
440 find_inferior (&all_threads
, continue_one_thread
, &thread_id
);
441 faked_breakpoint
= 0;
443 if (!ContinueDebugEvent (current_event
.dwProcessId
,
444 current_event
.dwThreadId
,
451 /* Fetch register(s) from the current thread context. */
453 child_fetch_inferior_registers (struct regcache
*regcache
, int r
)
456 win32_thread_info
*th
= thread_rec (current_inferior_ptid (), TRUE
);
457 if (r
== -1 || r
> NUM_REGS
)
458 child_fetch_inferior_registers (regcache
, NUM_REGS
);
460 for (regno
= 0; regno
< r
; regno
++)
461 (*the_low_target
.fetch_inferior_register
) (regcache
, th
, regno
);
464 /* Store a new register value into the current thread context. We don't
465 change the program's context until later, when we resume it. */
467 child_store_inferior_registers (struct regcache
*regcache
, int r
)
470 win32_thread_info
*th
= thread_rec (current_inferior_ptid (), TRUE
);
471 if (r
== -1 || r
== 0 || r
> NUM_REGS
)
472 child_store_inferior_registers (regcache
, NUM_REGS
);
474 for (regno
= 0; regno
< r
; regno
++)
475 (*the_low_target
.store_inferior_register
) (regcache
, th
, regno
);
478 /* Map the Windows error number in ERROR to a locale-dependent error
479 message string and return a pointer to it. Typically, the values
480 for ERROR come from GetLastError.
482 The string pointed to shall not be modified by the application,
483 but may be overwritten by a subsequent call to strwinerror
485 The strwinerror function does not change the current setting
489 strwinerror (DWORD error
)
491 static char buf
[1024];
493 DWORD lasterr
= GetLastError ();
494 DWORD chars
= FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
495 | FORMAT_MESSAGE_ALLOCATE_BUFFER
,
498 0, /* Default language */
504 /* If there is an \r\n appended, zap it. */
506 && msgbuf
[chars
- 2] == '\r'
507 && msgbuf
[chars
- 1] == '\n')
513 if (chars
> ((COUNTOF (buf
)) - 1))
515 chars
= COUNTOF (buf
) - 1;
520 wcstombs (buf
, msgbuf
, chars
+ 1);
522 strncpy (buf
, msgbuf
, chars
+ 1);
527 sprintf (buf
, "unknown win32 error (%u)", (unsigned) error
);
529 SetLastError (lasterr
);
534 create_process (const char *program
, char *args
,
535 DWORD flags
, PROCESS_INFORMATION
*pi
)
540 wchar_t *p
, *wprogram
, *wargs
;
543 wprogram
= alloca ((strlen (program
) + 1) * sizeof (wchar_t));
544 mbstowcs (wprogram
, program
, strlen (program
) + 1);
546 for (p
= wprogram
; *p
; ++p
)
550 argslen
= strlen (args
);
551 wargs
= alloca ((argslen
+ 1) * sizeof (wchar_t));
552 mbstowcs (wargs
, args
, argslen
+ 1);
554 ret
= CreateProcessW (wprogram
, /* image name */
555 wargs
, /* command line */
556 NULL
, /* security, not supported */
557 NULL
, /* thread, not supported */
558 FALSE
, /* inherit handles, not supported */
559 flags
, /* start flags */
560 NULL
, /* environment, not supported */
561 NULL
, /* current directory, not supported */
562 NULL
, /* start info, not supported */
565 STARTUPINFOA si
= { sizeof (STARTUPINFOA
) };
567 ret
= CreateProcessA (program
, /* image name */
568 args
, /* command line */
571 TRUE
, /* inherit handles */
572 flags
, /* start flags */
573 NULL
, /* environment */
574 NULL
, /* current directory */
575 &si
, /* start info */
582 /* Start a new process.
583 PROGRAM is a path to the program to execute.
584 ARGS is a standard NULL-terminated array of arguments,
585 to be passed to the inferior as ``argv''.
586 Returns the new PID on success, -1 on failure. Registers the new
587 process with the process list. */
589 win32_create_inferior (char *program
, char **program_args
)
592 char real_path
[PATH_MAX
];
593 char *orig_path
, *new_path
, *path_ptr
;
600 PROCESS_INFORMATION pi
;
603 /* win32_wait needs to know we're not attaching. */
607 error ("No executable specified, specify executable to debug.\n");
609 flags
= DEBUG_PROCESS
| DEBUG_ONLY_THIS_PROCESS
;
613 path_ptr
= getenv ("PATH");
616 int size
= cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, NULL
, 0);
617 orig_path
= alloca (strlen (path_ptr
) + 1);
618 new_path
= alloca (size
);
619 strcpy (orig_path
, path_ptr
);
620 cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, new_path
, size
);
621 setenv ("PATH", new_path
, 1);
623 cygwin_conv_path (CCP_POSIX_TO_WIN_A
, program
, real_path
, PATH_MAX
);
628 for (argc
= 1; program_args
[argc
]; argc
++)
629 argslen
+= strlen (program_args
[argc
]) + 1;
630 args
= alloca (argslen
);
632 for (argc
= 1; program_args
[argc
]; argc
++)
634 /* FIXME: Can we do better about quoting? How does Cygwin
637 strcat (args
, program_args
[argc
]);
639 OUTMSG2 (("Command line is \"%s\"\n", args
));
641 #ifdef CREATE_NEW_PROCESS_GROUP
642 flags
|= CREATE_NEW_PROCESS_GROUP
;
645 ret
= create_process (program
, args
, flags
, &pi
);
646 err
= GetLastError ();
647 if (!ret
&& err
== ERROR_FILE_NOT_FOUND
)
649 char *exename
= alloca (strlen (program
) + 5);
650 strcat (strcpy (exename
, program
), ".exe");
651 ret
= create_process (exename
, args
, flags
, &pi
);
652 err
= GetLastError ();
657 setenv ("PATH", orig_path
, 1);
662 error ("Error creating process \"%s%s\", (error %d): %s\n",
663 program
, args
, (int) err
, strwinerror (err
));
667 OUTMSG2 (("Process created: %s\n", (char *) args
));
671 /* On Windows CE this handle can't be closed. The OS reuses
672 it in the debug events, while the 9x/NT versions of Windows
673 probably use a DuplicateHandle'd one. */
674 CloseHandle (pi
.hThread
);
677 do_initial_child_stuff (pi
.hProcess
, pi
.dwProcessId
, 0);
679 return current_process_id
;
682 /* Attach to a running process.
683 PID is the process ID to attach to, specified by the user
684 or a higher layer. */
686 win32_attach (unsigned long pid
)
689 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
692 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
694 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
696 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
698 h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
701 if (DebugActiveProcess (pid
))
703 if (DebugSetProcessKillOnExit
!= NULL
)
704 DebugSetProcessKillOnExit (FALSE
);
706 /* win32_wait needs to know we're attaching. */
708 do_initial_child_stuff (h
, pid
, 1);
715 err
= GetLastError ();
716 error ("Attach to process failed (error %d): %s\n",
717 (int) err
, strwinerror (err
));
720 /* Handle OUTPUT_DEBUG_STRING_EVENT from child process. */
722 handle_output_debug_string (struct target_waitstatus
*ourstatus
)
724 #define READ_BUFFER_LEN 1024
726 char s
[READ_BUFFER_LEN
+ 1] = { 0 };
727 DWORD nbytes
= current_event
.u
.DebugString
.nDebugStringLength
;
732 if (nbytes
> READ_BUFFER_LEN
)
733 nbytes
= READ_BUFFER_LEN
;
735 addr
= (CORE_ADDR
) (size_t) current_event
.u
.DebugString
.lpDebugStringData
;
737 if (current_event
.u
.DebugString
.fUnicode
)
739 /* The event tells us how many bytes, not chars, even
741 WCHAR buffer
[(READ_BUFFER_LEN
+ 1) / sizeof (WCHAR
)] = { 0 };
742 if (read_inferior_memory (addr
, (unsigned char *) buffer
, nbytes
) != 0)
744 wcstombs (s
, buffer
, (nbytes
+ 1) / sizeof (WCHAR
));
748 if (read_inferior_memory (addr
, (unsigned char *) s
, nbytes
) != 0)
752 if (strncmp (s
, "cYg", 3) != 0)
762 #undef READ_BUFFER_LEN
766 win32_clear_inferiors (void)
768 if (current_process_handle
!= NULL
)
769 CloseHandle (current_process_handle
);
771 for_each_inferior (&all_threads
, delete_thread_info
);
775 /* Kill all inferiors. */
779 struct process_info
*process
;
781 if (current_process_handle
== NULL
)
784 TerminateProcess (current_process_handle
, 0);
787 if (!child_continue (DBG_CONTINUE
, -1))
789 if (!WaitForDebugEvent (¤t_event
, INFINITE
))
791 if (current_event
.dwDebugEventCode
== EXIT_PROCESS_DEBUG_EVENT
)
793 else if (current_event
.dwDebugEventCode
== OUTPUT_DEBUG_STRING_EVENT
)
795 struct target_waitstatus our_status
= { 0 };
796 handle_output_debug_string (&our_status
);
800 win32_clear_inferiors ();
802 process
= find_process_pid (pid
);
803 remove_process (process
);
807 /* Detach from inferior PID. */
809 win32_detach (int pid
)
811 struct process_info
*process
;
812 winapi_DebugActiveProcessStop DebugActiveProcessStop
= NULL
;
813 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
815 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
817 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
819 DebugActiveProcessStop
= GETPROCADDRESS (dll
, DebugActiveProcessStop
);
820 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
822 if (DebugSetProcessKillOnExit
== NULL
823 || DebugActiveProcessStop
== NULL
)
827 struct thread_resume resume
;
828 resume
.thread
= minus_one_ptid
;
829 resume
.kind
= resume_continue
;
831 win32_resume (&resume
, 1);
834 if (!DebugActiveProcessStop (current_process_id
))
837 DebugSetProcessKillOnExit (FALSE
);
838 process
= find_process_pid (pid
);
839 remove_process (process
);
841 win32_clear_inferiors ();
846 win32_mourn (struct process_info
*process
)
848 remove_process (process
);
851 /* Wait for inferiors to end. */
855 HANDLE h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
858 WaitForSingleObject (h
, INFINITE
);
863 /* Return 1 iff the thread with thread ID TID is alive. */
865 win32_thread_alive (ptid_t ptid
)
869 /* Our thread list is reliable; don't bother to poll target
871 if (find_inferior_id (&all_threads
, ptid
) != NULL
)
878 /* Resume the inferior process. RESUME_INFO describes how we want
881 win32_resume (struct thread_resume
*resume_info
, size_t n
)
886 win32_thread_info
*th
;
887 DWORD continue_status
= DBG_CONTINUE
;
890 /* This handles the very limited set of resume packets that GDB can
891 currently produce. */
893 if (n
== 1 && ptid_equal (resume_info
[0].thread
, minus_one_ptid
))
898 /* Yes, we're ignoring resume_info[0].thread. It'd be tricky to make
899 the Windows resume code do the right thing for thread switching. */
900 tid
= current_event
.dwThreadId
;
902 if (!ptid_equal (resume_info
[0].thread
, minus_one_ptid
))
904 sig
= resume_info
[0].sig
;
905 step
= resume_info
[0].kind
== resume_step
;
913 if (sig
!= GDB_SIGNAL_0
)
915 if (current_event
.dwDebugEventCode
!= EXCEPTION_DEBUG_EVENT
)
917 OUTMSG (("Cannot continue with signal %d here.\n", sig
));
919 else if (sig
== last_sig
)
920 continue_status
= DBG_EXCEPTION_NOT_HANDLED
;
922 OUTMSG (("Can only continue with recieved signal %d.\n", last_sig
));
925 last_sig
= GDB_SIGNAL_0
;
927 /* Get context for the currently selected thread. */
928 ptid
= debug_event_ptid (¤t_event
);
929 th
= thread_rec (ptid
, FALSE
);
932 if (th
->context
.ContextFlags
)
934 /* Move register values from the inferior into the thread
935 context structure. */
936 regcache_invalidate ();
940 if (the_low_target
.single_step
!= NULL
)
941 (*the_low_target
.single_step
) (th
);
943 error ("Single stepping is not supported "
944 "in this configuration.\n");
947 win32_set_thread_context (th
);
948 th
->context
.ContextFlags
= 0;
952 /* Allow continuing with the same signal that interrupted us.
953 Otherwise complain. */
955 child_continue (continue_status
, tid
);
959 win32_add_one_solib (const char *name
, CORE_ADDR load_addr
)
961 char buf
[MAX_PATH
+ 1];
962 char buf2
[MAX_PATH
+ 1];
965 WIN32_FIND_DATA w32_fd
;
966 WCHAR wname
[MAX_PATH
+ 1];
967 mbstowcs (wname
, name
, MAX_PATH
);
968 HANDLE h
= FindFirstFile (wname
, &w32_fd
);
970 WIN32_FIND_DATAA w32_fd
;
971 HANDLE h
= FindFirstFileA (name
, &w32_fd
);
974 if (h
== INVALID_HANDLE_VALUE
)
982 char cwd
[MAX_PATH
+ 1];
984 if (GetCurrentDirectoryA (MAX_PATH
+ 1, cwd
))
986 p
= strrchr (buf
, '\\');
989 SetCurrentDirectoryA (buf
);
990 GetFullPathNameA (w32_fd
.cFileName
, MAX_PATH
, buf
, &p
);
991 SetCurrentDirectoryA (cwd
);
998 if (strcasecmp (buf
, "ntdll.dll") == 0)
1000 GetSystemDirectoryA (buf
, sizeof (buf
));
1001 strcat (buf
, "\\ntdll.dll");
1006 cygwin_conv_path (CCP_WIN_A_TO_POSIX
, buf
, buf2
, sizeof (buf2
));
1011 loaded_dll (buf2
, load_addr
);
1015 get_image_name (HANDLE h
, void *address
, int unicode
)
1017 static char buf
[(2 * MAX_PATH
) + 1];
1018 DWORD size
= unicode
? sizeof (WCHAR
) : sizeof (char);
1024 /* Attempt to read the name of the dll that was detected.
1025 This is documented to work only when actively debugging
1026 a program. It will not work for attached processes. */
1027 if (address
== NULL
)
1031 /* Windows CE reports the address of the image name,
1032 instead of an address of a pointer into the image name. */
1033 address_ptr
= address
;
1035 /* See if we could read the address of a string, and that the
1036 address isn't null. */
1037 if (!ReadProcessMemory (h
, address
, &address_ptr
,
1038 sizeof (address_ptr
), &done
)
1039 || done
!= sizeof (address_ptr
)
1044 /* Find the length of the string */
1045 while (ReadProcessMemory (h
, address_ptr
+ len
++ * size
, &b
, size
, &done
)
1046 && (b
[0] != 0 || b
[size
- 1] != 0) && done
== size
)
1050 ReadProcessMemory (h
, address_ptr
, buf
, len
, &done
);
1053 WCHAR
*unicode_address
= (WCHAR
*) alloca (len
* sizeof (WCHAR
));
1054 ReadProcessMemory (h
, address_ptr
, unicode_address
, len
* sizeof (WCHAR
),
1057 WideCharToMultiByte (CP_ACP
, 0, unicode_address
, len
, buf
, len
, 0, 0);
1063 typedef BOOL (WINAPI
*winapi_EnumProcessModules
) (HANDLE
, HMODULE
*,
1065 typedef BOOL (WINAPI
*winapi_GetModuleInformation
) (HANDLE
, HMODULE
,
1066 LPMODULEINFO
, DWORD
);
1067 typedef DWORD (WINAPI
*winapi_GetModuleFileNameExA
) (HANDLE
, HMODULE
,
1070 static winapi_EnumProcessModules win32_EnumProcessModules
;
1071 static winapi_GetModuleInformation win32_GetModuleInformation
;
1072 static winapi_GetModuleFileNameExA win32_GetModuleFileNameExA
;
1077 static int psapi_loaded
= 0;
1078 static HMODULE dll
= NULL
;
1083 dll
= LoadLibrary (TEXT("psapi.dll"));
1086 win32_EnumProcessModules
=
1087 GETPROCADDRESS (dll
, EnumProcessModules
);
1088 win32_GetModuleInformation
=
1089 GETPROCADDRESS (dll
, GetModuleInformation
);
1090 win32_GetModuleFileNameExA
=
1091 GETPROCADDRESS (dll
, GetModuleFileNameExA
);
1094 return (win32_EnumProcessModules
!= NULL
1095 && win32_GetModuleInformation
!= NULL
1096 && win32_GetModuleFileNameExA
!= NULL
);
1100 psapi_get_dll_name (LPVOID BaseAddress
, char *dll_name_ret
)
1106 HMODULE
*DllHandle
= dh_buf
;
1114 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1119 if (!ok
|| !cbNeeded
)
1122 DllHandle
= (HMODULE
*) alloca (cbNeeded
);
1126 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1133 for (i
= 0; i
< ((size_t) cbNeeded
/ sizeof (HMODULE
)); i
++)
1135 if (!(*win32_GetModuleInformation
) (current_process_handle
,
1140 DWORD err
= GetLastError ();
1141 error ("Can't get module info: (error %d): %s\n",
1142 (int) err
, strwinerror (err
));
1145 if (mi
.lpBaseOfDll
== BaseAddress
)
1147 len
= (*win32_GetModuleFileNameExA
) (current_process_handle
,
1153 DWORD err
= GetLastError ();
1154 error ("Error getting dll name: (error %d): %s\n",
1155 (int) err
, strwinerror (err
));
1162 dll_name_ret
[0] = '\0';
1168 /* Iterate over all DLLs currently mapped by our inferior, and
1169 add them to our list of solibs. */
1172 win32_add_all_dlls (void)
1176 HMODULE
*DllHandle
= dh_buf
;
1184 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1189 if (!ok
|| !cbNeeded
)
1192 DllHandle
= (HMODULE
*) alloca (cbNeeded
);
1196 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1203 for (i
= 1; i
< ((size_t) cbNeeded
/ sizeof (HMODULE
)); i
++)
1206 char dll_name
[MAX_PATH
];
1208 if (!(*win32_GetModuleInformation
) (current_process_handle
,
1213 if ((*win32_GetModuleFileNameExA
) (current_process_handle
,
1218 /* The symbols in a dll are offset by 0x1000, which is the
1219 offset from 0 of the first byte in an image - because
1220 of the file header and the section alignment. */
1221 win32_add_one_solib (dll_name
,
1222 (CORE_ADDR
) (uintptr_t) mi
.lpBaseOfDll
+ 0x1000);
1227 typedef HANDLE (WINAPI
*winapi_CreateToolhelp32Snapshot
) (DWORD
, DWORD
);
1228 typedef BOOL (WINAPI
*winapi_Module32First
) (HANDLE
, LPMODULEENTRY32
);
1229 typedef BOOL (WINAPI
*winapi_Module32Next
) (HANDLE
, LPMODULEENTRY32
);
1231 static winapi_CreateToolhelp32Snapshot win32_CreateToolhelp32Snapshot
;
1232 static winapi_Module32First win32_Module32First
;
1233 static winapi_Module32Next win32_Module32Next
;
1235 typedef BOOL (WINAPI
*winapi_CloseToolhelp32Snapshot
) (HANDLE
);
1236 static winapi_CloseToolhelp32Snapshot win32_CloseToolhelp32Snapshot
;
1240 load_toolhelp (void)
1242 static int toolhelp_loaded
= 0;
1243 static HMODULE dll
= NULL
;
1245 if (!toolhelp_loaded
)
1247 toolhelp_loaded
= 1;
1249 dll
= GetModuleHandle (_T("KERNEL32.DLL"));
1251 dll
= LoadLibrary (L
"TOOLHELP.DLL");
1256 win32_CreateToolhelp32Snapshot
=
1257 GETPROCADDRESS (dll
, CreateToolhelp32Snapshot
);
1258 win32_Module32First
= GETPROCADDRESS (dll
, Module32First
);
1259 win32_Module32Next
= GETPROCADDRESS (dll
, Module32Next
);
1261 win32_CloseToolhelp32Snapshot
=
1262 GETPROCADDRESS (dll
, CloseToolhelp32Snapshot
);
1266 return (win32_CreateToolhelp32Snapshot
!= NULL
1267 && win32_Module32First
!= NULL
1268 && win32_Module32Next
!= NULL
1270 && win32_CloseToolhelp32Snapshot
!= NULL
1276 toolhelp_get_dll_name (LPVOID BaseAddress
, char *dll_name_ret
)
1278 HANDLE snapshot_module
;
1279 MODULEENTRY32 modEntry
= { sizeof (MODULEENTRY32
) };
1282 if (!load_toolhelp ())
1285 snapshot_module
= win32_CreateToolhelp32Snapshot (TH32CS_SNAPMODULE
,
1286 current_event
.dwProcessId
);
1287 if (snapshot_module
== INVALID_HANDLE_VALUE
)
1290 /* Ignore the first module, which is the exe. */
1291 if (win32_Module32First (snapshot_module
, &modEntry
))
1292 while (win32_Module32Next (snapshot_module
, &modEntry
))
1293 if (modEntry
.modBaseAddr
== BaseAddress
)
1296 wcstombs (dll_name_ret
, modEntry
.szExePath
, MAX_PATH
+ 1);
1298 strcpy (dll_name_ret
, modEntry
.szExePath
);
1305 win32_CloseToolhelp32Snapshot (snapshot_module
);
1307 CloseHandle (snapshot_module
);
1313 handle_load_dll (void)
1315 LOAD_DLL_DEBUG_INFO
*event
= ¤t_event
.u
.LoadDll
;
1316 char dll_buf
[MAX_PATH
+ 1];
1317 char *dll_name
= NULL
;
1318 CORE_ADDR load_addr
;
1320 dll_buf
[0] = dll_buf
[sizeof (dll_buf
) - 1] = '\0';
1322 /* Windows does not report the image name of the dlls in the debug
1323 event on attaches. We resort to iterating over the list of
1324 loaded dlls looking for a match by image base. */
1325 if (!psapi_get_dll_name (event
->lpBaseOfDll
, dll_buf
))
1327 if (!server_waiting
)
1328 /* On some versions of Windows and Windows CE, we can't create
1329 toolhelp snapshots while the inferior is stopped in a
1330 LOAD_DLL_DEBUG_EVENT due to a dll load, but we can while
1331 Windows is reporting the already loaded dlls. */
1332 toolhelp_get_dll_name (event
->lpBaseOfDll
, dll_buf
);
1337 if (*dll_name
== '\0')
1338 dll_name
= get_image_name (current_process_handle
,
1339 event
->lpImageName
, event
->fUnicode
);
1343 /* The symbols in a dll are offset by 0x1000, which is the
1344 offset from 0 of the first byte in an image - because
1345 of the file header and the section alignment. */
1347 load_addr
= (CORE_ADDR
) (uintptr_t) event
->lpBaseOfDll
+ 0x1000;
1348 win32_add_one_solib (dll_name
, load_addr
);
1351 /* Handle a DLL unload event.
1353 This function assumes that this event did not occur during inferior
1354 initialization, where their event info may be incomplete (see
1355 do_initial_child_stuff and win32_add_one_solib for more info
1356 on how we handle DLL loading during that phase). */
1359 handle_unload_dll (void)
1361 CORE_ADDR load_addr
=
1362 (CORE_ADDR
) (uintptr_t) current_event
.u
.UnloadDll
.lpBaseOfDll
;
1363 load_addr
+= 0x1000;
1364 unloaded_dll (NULL
, load_addr
);
1368 handle_exception (struct target_waitstatus
*ourstatus
)
1370 DWORD code
= current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
;
1372 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
1376 case EXCEPTION_ACCESS_VIOLATION
:
1377 OUTMSG2 (("EXCEPTION_ACCESS_VIOLATION"));
1378 ourstatus
->value
.sig
= GDB_SIGNAL_SEGV
;
1380 case STATUS_STACK_OVERFLOW
:
1381 OUTMSG2 (("STATUS_STACK_OVERFLOW"));
1382 ourstatus
->value
.sig
= GDB_SIGNAL_SEGV
;
1384 case STATUS_FLOAT_DENORMAL_OPERAND
:
1385 OUTMSG2 (("STATUS_FLOAT_DENORMAL_OPERAND"));
1386 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1388 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
1389 OUTMSG2 (("EXCEPTION_ARRAY_BOUNDS_EXCEEDED"));
1390 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1392 case STATUS_FLOAT_INEXACT_RESULT
:
1393 OUTMSG2 (("STATUS_FLOAT_INEXACT_RESULT"));
1394 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1396 case STATUS_FLOAT_INVALID_OPERATION
:
1397 OUTMSG2 (("STATUS_FLOAT_INVALID_OPERATION"));
1398 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1400 case STATUS_FLOAT_OVERFLOW
:
1401 OUTMSG2 (("STATUS_FLOAT_OVERFLOW"));
1402 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1404 case STATUS_FLOAT_STACK_CHECK
:
1405 OUTMSG2 (("STATUS_FLOAT_STACK_CHECK"));
1406 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1408 case STATUS_FLOAT_UNDERFLOW
:
1409 OUTMSG2 (("STATUS_FLOAT_UNDERFLOW"));
1410 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1412 case STATUS_FLOAT_DIVIDE_BY_ZERO
:
1413 OUTMSG2 (("STATUS_FLOAT_DIVIDE_BY_ZERO"));
1414 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1416 case STATUS_INTEGER_DIVIDE_BY_ZERO
:
1417 OUTMSG2 (("STATUS_INTEGER_DIVIDE_BY_ZERO"));
1418 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1420 case STATUS_INTEGER_OVERFLOW
:
1421 OUTMSG2 (("STATUS_INTEGER_OVERFLOW"));
1422 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1424 case EXCEPTION_BREAKPOINT
:
1425 OUTMSG2 (("EXCEPTION_BREAKPOINT"));
1426 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1428 /* Remove the initial breakpoint. */
1429 check_breakpoints ((CORE_ADDR
) (long) current_event
1430 .u
.Exception
.ExceptionRecord
.ExceptionAddress
);
1434 OUTMSG2 (("DBG_CONTROL_C"));
1435 ourstatus
->value
.sig
= GDB_SIGNAL_INT
;
1437 case DBG_CONTROL_BREAK
:
1438 OUTMSG2 (("DBG_CONTROL_BREAK"));
1439 ourstatus
->value
.sig
= GDB_SIGNAL_INT
;
1441 case EXCEPTION_SINGLE_STEP
:
1442 OUTMSG2 (("EXCEPTION_SINGLE_STEP"));
1443 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1445 case EXCEPTION_ILLEGAL_INSTRUCTION
:
1446 OUTMSG2 (("EXCEPTION_ILLEGAL_INSTRUCTION"));
1447 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1449 case EXCEPTION_PRIV_INSTRUCTION
:
1450 OUTMSG2 (("EXCEPTION_PRIV_INSTRUCTION"));
1451 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1453 case EXCEPTION_NONCONTINUABLE_EXCEPTION
:
1454 OUTMSG2 (("EXCEPTION_NONCONTINUABLE_EXCEPTION"));
1455 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1458 if (current_event
.u
.Exception
.dwFirstChance
)
1460 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1463 OUTMSG2 (("gdbserver: unknown target exception 0x%08x at 0x%s",
1464 (unsigned) current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
,
1465 phex_nz ((uintptr_t) current_event
.u
.Exception
.ExceptionRecord
.
1466 ExceptionAddress
, sizeof (uintptr_t))));
1467 ourstatus
->value
.sig
= GDB_SIGNAL_UNKNOWN
;
1471 last_sig
= ourstatus
->value
.sig
;
1476 suspend_one_thread (struct inferior_list_entry
*entry
)
1478 struct thread_info
*thread
= (struct thread_info
*) entry
;
1479 win32_thread_info
*th
= inferior_target_data (thread
);
1483 if (SuspendThread (th
->h
) == (DWORD
) -1)
1485 DWORD err
= GetLastError ();
1486 OUTMSG (("warning: SuspendThread failed in suspend_one_thread, "
1487 "(error %d): %s\n", (int) err
, strwinerror (err
)));
1495 fake_breakpoint_event (void)
1497 OUTMSG2(("fake_breakpoint_event\n"));
1499 faked_breakpoint
= 1;
1501 memset (¤t_event
, 0, sizeof (current_event
));
1502 current_event
.dwThreadId
= main_thread_id
;
1503 current_event
.dwDebugEventCode
= EXCEPTION_DEBUG_EVENT
;
1504 current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
1505 = EXCEPTION_BREAKPOINT
;
1507 for_each_inferior (&all_threads
, suspend_one_thread
);
1512 auto_delete_breakpoint (CORE_ADDR stop_pc
)
1518 /* Get the next event from the child. */
1521 get_child_debug_event (struct target_waitstatus
*ourstatus
)
1525 last_sig
= GDB_SIGNAL_0
;
1526 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1528 /* Check if GDB sent us an interrupt request. */
1529 check_remote_input_interrupt_request ();
1531 if (soft_interrupt_requested
)
1533 soft_interrupt_requested
= 0;
1534 fake_breakpoint_event ();
1543 /* WinCE doesn't set an initial breakpoint automatically. To
1544 stop the inferior, we flush all currently pending debug
1545 events -- the thread list and the dll list are always
1546 reported immediatelly without delay, then, we suspend all
1547 threads and pretend we saw a trap at the current PC of the
1550 Contrary to desktop Windows, Windows CE *does* report the dll
1551 names on LOAD_DLL_DEBUG_EVENTs resulting from a
1552 DebugActiveProcess call. This limits the way we can detect
1553 if all the dlls have already been reported. If we get a real
1554 debug event before leaving attaching, the worst that will
1555 happen is the user will see a spurious breakpoint. */
1557 current_event
.dwDebugEventCode
= 0;
1558 if (!WaitForDebugEvent (¤t_event
, 0))
1560 OUTMSG2(("no attach events left\n"));
1561 fake_breakpoint_event ();
1565 OUTMSG2(("got attach event\n"));
1570 /* Keep the wait time low enough for confortable remote
1571 interruption, but high enough so gdbserver doesn't become a
1573 if (!WaitForDebugEvent (¤t_event
, 250))
1575 DWORD e
= GetLastError();
1577 if (e
== ERROR_PIPE_NOT_CONNECTED
)
1579 /* This will happen if the loader fails to succesfully
1580 load the application, e.g., if the main executable
1581 tries to pull in a non-existing export from a
1583 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1584 ourstatus
->value
.integer
= 1;
1594 switch (current_event
.dwDebugEventCode
)
1596 case CREATE_THREAD_DEBUG_EVENT
:
1597 OUTMSG2 (("gdbserver: kernel event CREATE_THREAD_DEBUG_EVENT "
1598 "for pid=%u tid=%x)\n",
1599 (unsigned) current_event
.dwProcessId
,
1600 (unsigned) current_event
.dwThreadId
));
1602 /* Record the existence of this thread. */
1603 child_add_thread (current_event
.dwProcessId
,
1604 current_event
.dwThreadId
,
1605 current_event
.u
.CreateThread
.hThread
,
1606 current_event
.u
.CreateThread
.lpThreadLocalBase
);
1609 case EXIT_THREAD_DEBUG_EVENT
:
1610 OUTMSG2 (("gdbserver: kernel event EXIT_THREAD_DEBUG_EVENT "
1611 "for pid=%u tid=%x\n",
1612 (unsigned) current_event
.dwProcessId
,
1613 (unsigned) current_event
.dwThreadId
));
1614 child_delete_thread (current_event
.dwProcessId
,
1615 current_event
.dwThreadId
);
1617 current_inferior
= (struct thread_info
*) all_threads
.head
;
1620 case CREATE_PROCESS_DEBUG_EVENT
:
1621 OUTMSG2 (("gdbserver: kernel event CREATE_PROCESS_DEBUG_EVENT "
1622 "for pid=%u tid=%x\n",
1623 (unsigned) current_event
.dwProcessId
,
1624 (unsigned) current_event
.dwThreadId
));
1625 CloseHandle (current_event
.u
.CreateProcessInfo
.hFile
);
1627 current_process_handle
= current_event
.u
.CreateProcessInfo
.hProcess
;
1628 main_thread_id
= current_event
.dwThreadId
;
1630 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
1631 ourstatus
->value
.execd_pathname
= "Main executable";
1633 /* Add the main thread. */
1634 child_add_thread (current_event
.dwProcessId
,
1636 current_event
.u
.CreateProcessInfo
.hThread
,
1637 current_event
.u
.CreateProcessInfo
.lpThreadLocalBase
);
1639 ourstatus
->value
.related_pid
= debug_event_ptid (¤t_event
);
1643 /* Windows CE doesn't set the initial breakpoint
1644 automatically like the desktop versions of Windows do.
1645 We add it explicitly here. It will be removed as soon as
1647 set_breakpoint_at ((CORE_ADDR
) (long) current_event
.u
1648 .CreateProcessInfo
.lpStartAddress
,
1649 auto_delete_breakpoint
);
1654 case EXIT_PROCESS_DEBUG_EVENT
:
1655 OUTMSG2 (("gdbserver: kernel event EXIT_PROCESS_DEBUG_EVENT "
1656 "for pid=%u tid=%x\n",
1657 (unsigned) current_event
.dwProcessId
,
1658 (unsigned) current_event
.dwThreadId
));
1659 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1660 ourstatus
->value
.integer
= current_event
.u
.ExitProcess
.dwExitCode
;
1661 child_continue (DBG_CONTINUE
, -1);
1662 CloseHandle (current_process_handle
);
1663 current_process_handle
= NULL
;
1666 case LOAD_DLL_DEBUG_EVENT
:
1667 OUTMSG2 (("gdbserver: kernel event LOAD_DLL_DEBUG_EVENT "
1668 "for pid=%u tid=%x\n",
1669 (unsigned) current_event
.dwProcessId
,
1670 (unsigned) current_event
.dwThreadId
));
1671 CloseHandle (current_event
.u
.LoadDll
.hFile
);
1672 if (! child_initialization_done
)
1676 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1677 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1680 case UNLOAD_DLL_DEBUG_EVENT
:
1681 OUTMSG2 (("gdbserver: kernel event UNLOAD_DLL_DEBUG_EVENT "
1682 "for pid=%u tid=%x\n",
1683 (unsigned) current_event
.dwProcessId
,
1684 (unsigned) current_event
.dwThreadId
));
1685 if (! child_initialization_done
)
1687 handle_unload_dll ();
1688 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1689 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1692 case EXCEPTION_DEBUG_EVENT
:
1693 OUTMSG2 (("gdbserver: kernel event EXCEPTION_DEBUG_EVENT "
1694 "for pid=%u tid=%x\n",
1695 (unsigned) current_event
.dwProcessId
,
1696 (unsigned) current_event
.dwThreadId
));
1697 handle_exception (ourstatus
);
1700 case OUTPUT_DEBUG_STRING_EVENT
:
1701 /* A message from the kernel (or Cygwin). */
1702 OUTMSG2 (("gdbserver: kernel event OUTPUT_DEBUG_STRING_EVENT "
1703 "for pid=%u tid=%x\n",
1704 (unsigned) current_event
.dwProcessId
,
1705 (unsigned) current_event
.dwThreadId
));
1706 handle_output_debug_string (ourstatus
);
1710 OUTMSG2 (("gdbserver: kernel event unknown "
1711 "for pid=%u tid=%x code=%x\n",
1712 (unsigned) current_event
.dwProcessId
,
1713 (unsigned) current_event
.dwThreadId
,
1714 (unsigned) current_event
.dwDebugEventCode
));
1718 ptid
= debug_event_ptid (¤t_event
);
1720 (struct thread_info
*) find_inferior_id (&all_threads
, ptid
);
1724 /* Wait for the inferior process to change state.
1725 STATUS will be filled in with a response code to send to GDB.
1726 Returns the signal which caused the process to stop. */
1728 win32_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
, int options
)
1730 struct regcache
*regcache
;
1732 if (cached_status
.kind
!= TARGET_WAITKIND_IGNORE
)
1734 /* The core always does a wait after creating the inferior, and
1735 do_initial_child_stuff already ran the inferior to the
1736 initial breakpoint (or an exit, if creating the process
1737 fails). Report it now. */
1738 *ourstatus
= cached_status
;
1739 cached_status
.kind
= TARGET_WAITKIND_IGNORE
;
1740 return debug_event_ptid (¤t_event
);
1745 if (!get_child_debug_event (ourstatus
))
1748 switch (ourstatus
->kind
)
1750 case TARGET_WAITKIND_EXITED
:
1751 OUTMSG2 (("Child exited with retcode = %x\n",
1752 ourstatus
->value
.integer
));
1753 win32_clear_inferiors ();
1754 return pid_to_ptid (current_event
.dwProcessId
);
1755 case TARGET_WAITKIND_STOPPED
:
1756 case TARGET_WAITKIND_LOADED
:
1757 OUTMSG2 (("Child Stopped with signal = %d \n",
1758 ourstatus
->value
.sig
));
1760 regcache
= get_thread_regcache (current_inferior
, 1);
1761 child_fetch_inferior_registers (regcache
, -1);
1762 return debug_event_ptid (¤t_event
);
1764 OUTMSG (("Ignoring unknown internal event, %d\n", ourstatus
->kind
));
1766 case TARGET_WAITKIND_SPURIOUS
:
1767 case TARGET_WAITKIND_EXECD
:
1768 /* do nothing, just continue */
1769 child_continue (DBG_CONTINUE
, -1);
1775 /* Fetch registers from the inferior process.
1776 If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO. */
1778 win32_fetch_inferior_registers (struct regcache
*regcache
, int regno
)
1780 child_fetch_inferior_registers (regcache
, regno
);
1783 /* Store registers to the inferior process.
1784 If REGNO is -1, store all registers; otherwise, store at least REGNO. */
1786 win32_store_inferior_registers (struct regcache
*regcache
, int regno
)
1788 child_store_inferior_registers (regcache
, regno
);
1791 /* Read memory from the inferior process. This should generally be
1792 called through read_inferior_memory, which handles breakpoint shadowing.
1793 Read LEN bytes at MEMADDR into a buffer at MYADDR. */
1795 win32_read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
1797 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 0, 0) != len
;
1800 /* Write memory to the inferior process. This should generally be
1801 called through write_inferior_memory, which handles breakpoint shadowing.
1802 Write LEN bytes from the buffer at MYADDR to MEMADDR.
1803 Returns 0 on success and errno on failure. */
1805 win32_write_inferior_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
,
1808 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 1, 0) != len
;
1811 /* Send an interrupt request to the inferior process. */
1813 win32_request_interrupt (void)
1815 winapi_DebugBreakProcess DebugBreakProcess
;
1816 winapi_GenerateConsoleCtrlEvent GenerateConsoleCtrlEvent
;
1819 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
1821 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
1824 GenerateConsoleCtrlEvent
= GETPROCADDRESS (dll
, GenerateConsoleCtrlEvent
);
1826 if (GenerateConsoleCtrlEvent
!= NULL
1827 && GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT
, current_process_id
))
1830 /* GenerateConsoleCtrlEvent can fail if process id being debugged is
1831 not a process group id.
1832 Fallback to XP/Vista 'DebugBreakProcess', which generates a
1833 breakpoint exception in the interior process. */
1835 DebugBreakProcess
= GETPROCADDRESS (dll
, DebugBreakProcess
);
1837 if (DebugBreakProcess
!= NULL
1838 && DebugBreakProcess (current_process_handle
))
1841 /* Last resort, suspend all threads manually. */
1842 soft_interrupt_requested
= 1;
1847 win32_error_to_fileio_error (DWORD err
)
1851 case ERROR_BAD_PATHNAME
:
1852 case ERROR_FILE_NOT_FOUND
:
1853 case ERROR_INVALID_NAME
:
1854 case ERROR_PATH_NOT_FOUND
:
1855 return FILEIO_ENOENT
;
1857 case ERROR_IO_DEVICE
:
1858 case ERROR_OPEN_FAILED
:
1860 case ERROR_INVALID_HANDLE
:
1861 return FILEIO_EBADF
;
1862 case ERROR_ACCESS_DENIED
:
1863 case ERROR_SHARING_VIOLATION
:
1864 return FILEIO_EACCES
;
1865 case ERROR_NOACCESS
:
1866 return FILEIO_EFAULT
;
1868 return FILEIO_EBUSY
;
1869 case ERROR_ALREADY_EXISTS
:
1870 case ERROR_FILE_EXISTS
:
1871 return FILEIO_EEXIST
;
1872 case ERROR_BAD_DEVICE
:
1873 return FILEIO_ENODEV
;
1874 case ERROR_DIRECTORY
:
1875 return FILEIO_ENOTDIR
;
1876 case ERROR_FILENAME_EXCED_RANGE
:
1877 case ERROR_INVALID_DATA
:
1878 case ERROR_INVALID_PARAMETER
:
1879 case ERROR_NEGATIVE_SEEK
:
1880 return FILEIO_EINVAL
;
1881 case ERROR_TOO_MANY_OPEN_FILES
:
1882 return FILEIO_EMFILE
;
1883 case ERROR_HANDLE_DISK_FULL
:
1884 case ERROR_DISK_FULL
:
1885 return FILEIO_ENOSPC
;
1886 case ERROR_WRITE_PROTECT
:
1887 return FILEIO_EROFS
;
1888 case ERROR_NOT_SUPPORTED
:
1889 return FILEIO_ENOSYS
;
1892 return FILEIO_EUNKNOWN
;
1896 wince_hostio_last_error (char *buf
)
1898 DWORD winerr
= GetLastError ();
1899 int fileio_err
= win32_error_to_fileio_error (winerr
);
1900 sprintf (buf
, "F-1,%x", fileio_err
);
1904 /* Write Windows OS Thread Information Block address. */
1907 win32_get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
)
1909 win32_thread_info
*th
;
1910 th
= thread_rec (ptid
, 0);
1914 *addr
= th
->thread_local_base
;
1918 static struct target_ops win32_target_ops
= {
1919 win32_create_inferior
,
1928 win32_fetch_inferior_registers
,
1929 win32_store_inferior_registers
,
1930 NULL
, /* prepare_to_access_memory */
1931 NULL
, /* done_accessing_memory */
1932 win32_read_inferior_memory
,
1933 win32_write_inferior_memory
,
1934 NULL
, /* lookup_symbols */
1935 win32_request_interrupt
,
1936 NULL
, /* read_auxv */
1939 win32_stopped_by_watchpoint
,
1940 win32_stopped_data_address
,
1941 NULL
, /* read_offsets */
1942 NULL
, /* get_tls_address */
1943 NULL
, /* qxfer_spu */
1945 wince_hostio_last_error
,
1947 hostio_last_error_from_errno
,
1949 NULL
, /* qxfer_osdata */
1950 NULL
, /* qxfer_siginfo */
1951 NULL
, /* supports_non_stop */
1953 NULL
, /* start_non_stop */
1954 NULL
, /* supports_multi_process */
1955 NULL
, /* handle_monitor_command */
1956 NULL
, /* core_of_thread */
1957 NULL
, /* read_loadmap */
1958 NULL
, /* process_qsupported */
1959 NULL
, /* supports_tracepoints */
1961 NULL
, /* write_pc */
1962 NULL
, /* thread_stopped */
1963 win32_get_tib_address
1966 /* Initialize the Win32 backend. */
1968 initialize_low (void)
1970 set_target_ops (&win32_target_ops
);
1971 if (the_low_target
.breakpoint
!= NULL
)
1972 set_breakpoint_data (the_low_target
.breakpoint
,
1973 the_low_target
.breakpoint_len
);
1974 the_low_target
.arch_setup ();