1 /* Low level interface to Windows debugging, for gdbserver.
2 Copyright (C) 2006-2020 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/fileio.h"
24 #include "mem-break.h"
25 #include "win32-low.h"
26 #include "gdbthread.h"
35 #include "gdbsupport/gdb_tilde_expand.h"
36 #include "gdbsupport/common-inferior.h"
37 #include "gdbsupport/gdb_wait.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 EXCEPTION_RECORD siginfo_er
; /* Contents of $_siginfo */
79 static enum gdb_signal last_sig
= GDB_SIGNAL_0
;
81 /* The current debug event from WaitForDebugEvent. */
82 static DEBUG_EVENT current_event
;
84 /* A status that hasn't been reported to the core yet, and so
85 win32_wait should return it next, instead of fetching the next
86 debug event off the win32 API. */
87 static struct target_waitstatus cached_status
;
89 /* Non zero if an interrupt request is to be satisfied by suspending
91 static int soft_interrupt_requested
= 0;
93 /* Non zero if the inferior is stopped in a simulated breakpoint done
94 by suspending all the threads. */
95 static int faked_breakpoint
= 0;
97 const struct target_desc
*win32_tdesc
;
99 #define NUM_REGS (the_low_target.num_regs)
101 typedef BOOL (WINAPI
*winapi_DebugActiveProcessStop
) (DWORD dwProcessId
);
102 typedef BOOL (WINAPI
*winapi_DebugSetProcessKillOnExit
) (BOOL KillOnExit
);
103 typedef BOOL (WINAPI
*winapi_DebugBreakProcess
) (HANDLE
);
104 typedef BOOL (WINAPI
*winapi_GenerateConsoleCtrlEvent
) (DWORD
, DWORD
);
106 static ptid_t
win32_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
,
108 static void win32_resume (struct thread_resume
*resume_info
, size_t n
);
110 static void win32_add_all_dlls (void);
113 /* Get the thread ID from the current selected inferior (the current
116 current_thread_ptid (void)
121 /* The current debug event from WaitForDebugEvent. */
123 debug_event_ptid (DEBUG_EVENT
*event
)
125 return ptid_t (event
->dwProcessId
, event
->dwThreadId
, 0);
128 /* Get the thread context of the thread associated with TH. */
131 win32_get_thread_context (win32_thread_info
*th
)
133 memset (&th
->context
, 0, sizeof (CONTEXT
));
134 (*the_low_target
.get_thread_context
) (th
);
136 memcpy (&th
->base_context
, &th
->context
, sizeof (CONTEXT
));
140 /* Set the thread context of the thread associated with TH. */
143 win32_set_thread_context (win32_thread_info
*th
)
146 /* Calling SuspendThread on a thread that is running kernel code
147 will report that the suspending was successful, but in fact, that
148 will often not be true. In those cases, the context returned by
149 GetThreadContext will not be correct by the time the thread
150 stops, hence we can't set that context back into the thread when
151 resuming - it will most likely crash the inferior.
152 Unfortunately, there is no way to know when the thread will
153 really stop. To work around it, we'll only write the context
154 back to the thread when either the user or GDB explicitly change
155 it between stopping and resuming. */
156 if (memcmp (&th
->context
, &th
->base_context
, sizeof (CONTEXT
)) != 0)
158 SetThreadContext (th
->h
, &th
->context
);
161 /* Set the thread context of the thread associated with TH. */
164 win32_prepare_to_resume (win32_thread_info
*th
)
166 if (the_low_target
.prepare_to_resume
!= NULL
)
167 (*the_low_target
.prepare_to_resume
) (th
);
170 /* See win32-low.h. */
173 win32_require_context (win32_thread_info
*th
)
175 if (th
->context
.ContextFlags
== 0)
179 if (SuspendThread (th
->h
) == (DWORD
) -1)
181 DWORD err
= GetLastError ();
182 OUTMSG (("warning: SuspendThread failed in thread_rec, "
183 "(error %d): %s\n", (int) err
, strwinerror (err
)));
189 win32_get_thread_context (th
);
193 /* Find a thread record given a thread id. If GET_CONTEXT is set then
194 also retrieve the context for this thread. */
195 static win32_thread_info
*
196 thread_rec (ptid_t ptid
, int get_context
)
198 thread_info
*thread
= find_thread_ptid (ptid
);
202 win32_thread_info
*th
= (win32_thread_info
*) thread_target_data (thread
);
204 win32_require_context (th
);
208 /* Add a thread to the thread list. */
209 static win32_thread_info
*
210 child_add_thread (DWORD pid
, DWORD tid
, HANDLE h
, void *tlb
)
212 win32_thread_info
*th
;
213 ptid_t ptid
= ptid_t (pid
, tid
, 0);
215 if ((th
= thread_rec (ptid
, FALSE
)))
218 th
= XCNEW (win32_thread_info
);
221 th
->thread_local_base
= (CORE_ADDR
) (uintptr_t) tlb
;
223 add_thread (ptid
, th
);
225 if (the_low_target
.thread_added
!= NULL
)
226 (*the_low_target
.thread_added
) (th
);
231 /* Delete a thread from the list of threads. */
233 delete_thread_info (thread_info
*thread
)
235 win32_thread_info
*th
= (win32_thread_info
*) thread_target_data (thread
);
237 remove_thread (thread
);
242 /* Delete a thread from the list of threads. */
244 child_delete_thread (DWORD pid
, DWORD tid
)
246 /* If the last thread is exiting, just return. */
247 if (all_threads
.size () == 1)
250 thread_info
*thread
= find_thread_ptid (ptid_t (pid
, tid
));
254 delete_thread_info (thread
);
257 /* These watchpoint related wrapper functions simply pass on the function call
258 if the low target has registered a corresponding function. */
261 win32_supports_z_point_type (char z_type
)
263 return (the_low_target
.supports_z_point_type
!= NULL
264 && the_low_target
.supports_z_point_type (z_type
));
268 win32_insert_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
269 int size
, struct raw_breakpoint
*bp
)
271 if (the_low_target
.insert_point
!= NULL
)
272 return the_low_target
.insert_point (type
, addr
, size
, bp
);
274 /* Unsupported (see target.h). */
279 win32_remove_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
280 int size
, struct raw_breakpoint
*bp
)
282 if (the_low_target
.remove_point
!= NULL
)
283 return the_low_target
.remove_point (type
, addr
, size
, bp
);
285 /* Unsupported (see target.h). */
290 win32_stopped_by_watchpoint (void)
292 if (the_low_target
.stopped_by_watchpoint
!= NULL
)
293 return the_low_target
.stopped_by_watchpoint ();
299 win32_stopped_data_address (void)
301 if (the_low_target
.stopped_data_address
!= NULL
)
302 return the_low_target
.stopped_data_address ();
308 /* Transfer memory from/to the debugged process. */
310 child_xfer_memory (CORE_ADDR memaddr
, char *our
, int len
,
311 int write
, process_stratum_target
*target
)
316 uintptr_t addr
= (uintptr_t) memaddr
;
320 success
= WriteProcessMemory (current_process_handle
, (LPVOID
) addr
,
321 (LPCVOID
) our
, len
, &done
);
323 lasterror
= GetLastError ();
324 FlushInstructionCache (current_process_handle
, (LPCVOID
) addr
, len
);
328 success
= ReadProcessMemory (current_process_handle
, (LPCVOID
) addr
,
329 (LPVOID
) our
, len
, &done
);
331 lasterror
= GetLastError ();
333 if (!success
&& lasterror
== ERROR_PARTIAL_COPY
&& done
> 0)
336 return success
? done
: -1;
339 /* Clear out any old thread list and reinitialize it to a pristine
342 child_init_thread_list (void)
344 for_each_thread (delete_thread_info
);
347 /* Zero during the child initialization phase, and nonzero otherwise. */
349 static int child_initialization_done
= 0;
352 do_initial_child_stuff (HANDLE proch
, DWORD pid
, int attached
)
354 struct process_info
*proc
;
356 last_sig
= GDB_SIGNAL_0
;
358 current_process_handle
= proch
;
359 current_process_id
= pid
;
362 soft_interrupt_requested
= 0;
363 faked_breakpoint
= 0;
365 memset (¤t_event
, 0, sizeof (current_event
));
367 proc
= add_process (pid
, attached
);
368 proc
->tdesc
= win32_tdesc
;
369 child_init_thread_list ();
370 child_initialization_done
= 0;
372 if (the_low_target
.initial_stuff
!= NULL
)
373 (*the_low_target
.initial_stuff
) ();
375 cached_status
.kind
= TARGET_WAITKIND_IGNORE
;
377 /* Flush all currently pending debug events (thread and dll list) up
378 to the initial breakpoint. */
381 struct target_waitstatus status
;
383 win32_wait (minus_one_ptid
, &status
, 0);
385 /* Note win32_wait doesn't return thread events. */
386 if (status
.kind
!= TARGET_WAITKIND_LOADED
)
388 cached_status
= status
;
393 struct thread_resume resume
;
395 resume
.thread
= minus_one_ptid
;
396 resume
.kind
= resume_continue
;
399 win32_resume (&resume
, 1);
404 /* Now that the inferior has been started and all DLLs have been mapped,
405 we can iterate over all DLLs and load them in.
407 We avoid doing it any earlier because, on certain versions of Windows,
408 LOAD_DLL_DEBUG_EVENTs are sometimes not complete. In particular,
409 we have seen on Windows 8.1 that the ntdll.dll load event does not
410 include the DLL name, preventing us from creating an associated SO.
411 A possible explanation is that ntdll.dll might be mapped before
412 the SO info gets created by the Windows system -- ntdll.dll is
413 the first DLL to be reported via LOAD_DLL_DEBUG_EVENT and other DLLs
414 do not seem to suffer from that problem.
416 Rather than try to work around this sort of issue, it is much
417 simpler to just ignore DLL load/unload events during the startup
418 phase, and then process them all in one batch now. */
419 win32_add_all_dlls ();
422 child_initialization_done
= 1;
425 /* Resume all artificially suspended threads if we are continuing
428 continue_one_thread (thread_info
*thread
, int thread_id
)
430 win32_thread_info
*th
= (win32_thread_info
*) thread_target_data (thread
);
432 if (thread_id
== -1 || thread_id
== th
->tid
)
434 win32_prepare_to_resume (th
);
438 if (th
->context
.ContextFlags
)
440 win32_set_thread_context (th
);
441 th
->context
.ContextFlags
= 0;
444 if (ResumeThread (th
->h
) == (DWORD
) -1)
446 DWORD err
= GetLastError ();
447 OUTMSG (("warning: ResumeThread failed in continue_one_thread, "
448 "(error %d): %s\n", (int) err
, strwinerror (err
)));
456 child_continue (DWORD continue_status
, int thread_id
)
458 /* The inferior will only continue after the ContinueDebugEvent
460 for_each_thread ([&] (thread_info
*thread
)
462 continue_one_thread (thread
, thread_id
);
464 faked_breakpoint
= 0;
466 if (!ContinueDebugEvent (current_event
.dwProcessId
,
467 current_event
.dwThreadId
,
474 /* Fetch register(s) from the current thread context. */
476 child_fetch_inferior_registers (struct regcache
*regcache
, int r
)
479 win32_thread_info
*th
= thread_rec (current_thread_ptid (), TRUE
);
480 if (r
== -1 || r
> NUM_REGS
)
481 child_fetch_inferior_registers (regcache
, NUM_REGS
);
483 for (regno
= 0; regno
< r
; regno
++)
484 (*the_low_target
.fetch_inferior_register
) (regcache
, th
, regno
);
487 /* Store a new register value into the current thread context. We don't
488 change the program's context until later, when we resume it. */
490 child_store_inferior_registers (struct regcache
*regcache
, int r
)
493 win32_thread_info
*th
= thread_rec (current_thread_ptid (), TRUE
);
494 if (r
== -1 || r
== 0 || r
> NUM_REGS
)
495 child_store_inferior_registers (regcache
, NUM_REGS
);
497 for (regno
= 0; regno
< r
; regno
++)
498 (*the_low_target
.store_inferior_register
) (regcache
, th
, regno
);
501 /* Map the Windows error number in ERROR to a locale-dependent error
502 message string and return a pointer to it. Typically, the values
503 for ERROR come from GetLastError.
505 The string pointed to shall not be modified by the application,
506 but may be overwritten by a subsequent call to strwinerror
508 The strwinerror function does not change the current setting
512 strwinerror (DWORD error
)
514 static char buf
[1024];
516 DWORD lasterr
= GetLastError ();
517 DWORD chars
= FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
518 | FORMAT_MESSAGE_ALLOCATE_BUFFER
,
521 0, /* Default language */
527 /* If there is an \r\n appended, zap it. */
529 && msgbuf
[chars
- 2] == '\r'
530 && msgbuf
[chars
- 1] == '\n')
536 if (chars
> ((COUNTOF (buf
)) - 1))
538 chars
= COUNTOF (buf
) - 1;
543 wcstombs (buf
, msgbuf
, chars
+ 1);
545 strncpy (buf
, msgbuf
, chars
+ 1);
550 sprintf (buf
, "unknown win32 error (%u)", (unsigned) error
);
552 SetLastError (lasterr
);
557 create_process (const char *program
, char *args
,
558 DWORD flags
, PROCESS_INFORMATION
*pi
)
560 const char *inferior_cwd
= get_inferior_cwd ();
564 wchar_t *p
, *wprogram
, *wargs
, *wcwd
= NULL
;
567 wprogram
= alloca ((strlen (program
) + 1) * sizeof (wchar_t));
568 mbstowcs (wprogram
, program
, strlen (program
) + 1);
570 for (p
= wprogram
; *p
; ++p
)
574 argslen
= strlen (args
);
575 wargs
= alloca ((argslen
+ 1) * sizeof (wchar_t));
576 mbstowcs (wargs
, args
, argslen
+ 1);
578 if (inferior_cwd
!= NULL
)
580 std::string expanded_infcwd
= gdb_tilde_expand (inferior_cwd
);
581 std::replace (expanded_infcwd
.begin (), expanded_infcwd
.end (),
583 wcwd
= alloca ((expanded_infcwd
.size () + 1) * sizeof (wchar_t));
584 if (mbstowcs (wcwd
, expanded_infcwd
.c_str (),
585 expanded_infcwd
.size () + 1) == NULL
)
588 Could not convert the expanded inferior cwd to wide-char."));
592 ret
= CreateProcessW (wprogram
, /* image name */
593 wargs
, /* command line */
594 NULL
, /* security, not supported */
595 NULL
, /* thread, not supported */
596 FALSE
, /* inherit handles, not supported */
597 flags
, /* start flags */
598 NULL
, /* environment, not supported */
599 wcwd
, /* current directory */
600 NULL
, /* start info, not supported */
603 STARTUPINFOA si
= { sizeof (STARTUPINFOA
) };
605 ret
= CreateProcessA (program
, /* image name */
606 args
, /* command line */
609 TRUE
, /* inherit handles */
610 flags
, /* start flags */
611 NULL
, /* environment */
612 /* current directory */
613 (inferior_cwd
== NULL
615 : gdb_tilde_expand (inferior_cwd
).c_str()),
616 &si
, /* start info */
623 /* Start a new process.
624 PROGRAM is the program name.
625 PROGRAM_ARGS is the vector containing the inferior's args.
626 Returns the new PID on success, -1 on failure. Registers the new
627 process with the process list. */
629 win32_create_inferior (const char *program
,
630 const std::vector
<char *> &program_args
)
632 client_state
&cs
= get_client_state ();
634 char real_path
[PATH_MAX
];
635 char *orig_path
, *new_path
, *path_ptr
;
639 PROCESS_INFORMATION pi
;
641 std::string str_program_args
= stringify_argv (program_args
);
642 char *args
= (char *) str_program_args
.c_str ();
644 /* win32_wait needs to know we're not attaching. */
648 error ("No executable specified, specify executable to debug.\n");
650 flags
= DEBUG_PROCESS
| DEBUG_ONLY_THIS_PROCESS
;
654 path_ptr
= getenv ("PATH");
657 int size
= cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, NULL
, 0);
658 orig_path
= (char *) alloca (strlen (path_ptr
) + 1);
659 new_path
= (char *) alloca (size
);
660 strcpy (orig_path
, path_ptr
);
661 cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, new_path
, size
);
662 setenv ("PATH", new_path
, 1);
664 cygwin_conv_path (CCP_POSIX_TO_WIN_A
, program
, real_path
, PATH_MAX
);
668 OUTMSG2 (("Command line is \"%s\"\n", args
));
670 #ifdef CREATE_NEW_PROCESS_GROUP
671 flags
|= CREATE_NEW_PROCESS_GROUP
;
674 ret
= create_process (program
, args
, flags
, &pi
);
675 err
= GetLastError ();
676 if (!ret
&& err
== ERROR_FILE_NOT_FOUND
)
678 char *exename
= (char *) alloca (strlen (program
) + 5);
679 strcat (strcpy (exename
, program
), ".exe");
680 ret
= create_process (exename
, args
, flags
, &pi
);
681 err
= GetLastError ();
686 setenv ("PATH", orig_path
, 1);
691 error ("Error creating process \"%s%s\", (error %d): %s\n",
692 program
, args
, (int) err
, strwinerror (err
));
696 OUTMSG2 (("Process created: %s\n", (char *) args
));
700 /* On Windows CE this handle can't be closed. The OS reuses
701 it in the debug events, while the 9x/NT versions of Windows
702 probably use a DuplicateHandle'd one. */
703 CloseHandle (pi
.hThread
);
706 do_initial_child_stuff (pi
.hProcess
, pi
.dwProcessId
, 0);
708 /* Wait till we are at 1st instruction in program, return new pid
709 (assuming success). */
710 cs
.last_ptid
= win32_wait (ptid_t (current_process_id
), &cs
.last_status
, 0);
712 /* Necessary for handle_v_kill. */
713 signal_pid
= current_process_id
;
715 return current_process_id
;
718 /* Attach to a running process.
719 PID is the process ID to attach to, specified by the user
720 or a higher layer. */
722 win32_attach (unsigned long pid
)
725 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
728 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
730 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
732 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
734 h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
737 if (DebugActiveProcess (pid
))
739 if (DebugSetProcessKillOnExit
!= NULL
)
740 DebugSetProcessKillOnExit (FALSE
);
742 /* win32_wait needs to know we're attaching. */
744 do_initial_child_stuff (h
, pid
, 1);
751 err
= GetLastError ();
752 error ("Attach to process failed (error %d): %s\n",
753 (int) err
, strwinerror (err
));
756 /* Handle OUTPUT_DEBUG_STRING_EVENT from child process. */
758 handle_output_debug_string (void)
760 #define READ_BUFFER_LEN 1024
762 char s
[READ_BUFFER_LEN
+ 1] = { 0 };
763 DWORD nbytes
= current_event
.u
.DebugString
.nDebugStringLength
;
768 if (nbytes
> READ_BUFFER_LEN
)
769 nbytes
= READ_BUFFER_LEN
;
771 addr
= (CORE_ADDR
) (size_t) current_event
.u
.DebugString
.lpDebugStringData
;
773 if (current_event
.u
.DebugString
.fUnicode
)
775 /* The event tells us how many bytes, not chars, even
777 WCHAR buffer
[(READ_BUFFER_LEN
+ 1) / sizeof (WCHAR
)] = { 0 };
778 if (read_inferior_memory (addr
, (unsigned char *) buffer
, nbytes
) != 0)
780 wcstombs (s
, buffer
, (nbytes
+ 1) / sizeof (WCHAR
));
784 if (read_inferior_memory (addr
, (unsigned char *) s
, nbytes
) != 0)
788 if (!startswith (s
, "cYg"))
798 #undef READ_BUFFER_LEN
802 win32_clear_inferiors (void)
804 if (current_process_handle
!= NULL
)
805 CloseHandle (current_process_handle
);
807 for_each_thread (delete_thread_info
);
808 siginfo_er
.ExceptionCode
= 0;
812 /* Implementation of target_ops::kill. */
815 win32_kill (process_info
*process
)
817 TerminateProcess (current_process_handle
, 0);
820 if (!child_continue (DBG_CONTINUE
, -1))
822 if (!WaitForDebugEvent (¤t_event
, INFINITE
))
824 if (current_event
.dwDebugEventCode
== EXIT_PROCESS_DEBUG_EVENT
)
826 else if (current_event
.dwDebugEventCode
== OUTPUT_DEBUG_STRING_EVENT
)
827 handle_output_debug_string ();
830 win32_clear_inferiors ();
832 remove_process (process
);
836 /* Implementation of target_ops::detach. */
839 win32_detach (process_info
*process
)
841 winapi_DebugActiveProcessStop DebugActiveProcessStop
= NULL
;
842 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
844 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
846 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
848 DebugActiveProcessStop
= GETPROCADDRESS (dll
, DebugActiveProcessStop
);
849 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
851 if (DebugSetProcessKillOnExit
== NULL
852 || DebugActiveProcessStop
== NULL
)
856 struct thread_resume resume
;
857 resume
.thread
= minus_one_ptid
;
858 resume
.kind
= resume_continue
;
860 win32_resume (&resume
, 1);
863 if (!DebugActiveProcessStop (current_process_id
))
866 DebugSetProcessKillOnExit (FALSE
);
867 remove_process (process
);
869 win32_clear_inferiors ();
874 win32_mourn (struct process_info
*process
)
876 remove_process (process
);
879 /* Implementation of target_ops::join. */
884 HANDLE h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
887 WaitForSingleObject (h
, INFINITE
);
892 /* Return 1 iff the thread with thread ID TID is alive. */
894 win32_thread_alive (ptid_t ptid
)
896 /* Our thread list is reliable; don't bother to poll target
898 return find_thread_ptid (ptid
) != NULL
;
901 /* Resume the inferior process. RESUME_INFO describes how we want
904 win32_resume (struct thread_resume
*resume_info
, size_t n
)
909 win32_thread_info
*th
;
910 DWORD continue_status
= DBG_CONTINUE
;
913 /* This handles the very limited set of resume packets that GDB can
914 currently produce. */
916 if (n
== 1 && resume_info
[0].thread
== minus_one_ptid
)
921 /* Yes, we're ignoring resume_info[0].thread. It'd be tricky to make
922 the Windows resume code do the right thing for thread switching. */
923 tid
= current_event
.dwThreadId
;
925 if (resume_info
[0].thread
!= minus_one_ptid
)
927 sig
= gdb_signal_from_host (resume_info
[0].sig
);
928 step
= resume_info
[0].kind
== resume_step
;
936 if (sig
!= GDB_SIGNAL_0
)
938 if (current_event
.dwDebugEventCode
!= EXCEPTION_DEBUG_EVENT
)
940 OUTMSG (("Cannot continue with signal %s here.\n",
941 gdb_signal_to_string (sig
)));
943 else if (sig
== last_sig
)
944 continue_status
= DBG_EXCEPTION_NOT_HANDLED
;
946 OUTMSG (("Can only continue with received signal %s.\n",
947 gdb_signal_to_string (last_sig
)));
950 last_sig
= GDB_SIGNAL_0
;
952 /* Get context for the currently selected thread. */
953 ptid
= debug_event_ptid (¤t_event
);
954 th
= thread_rec (ptid
, FALSE
);
957 win32_prepare_to_resume (th
);
959 if (th
->context
.ContextFlags
)
961 /* Move register values from the inferior into the thread
962 context structure. */
963 regcache_invalidate ();
967 if (the_low_target
.single_step
!= NULL
)
968 (*the_low_target
.single_step
) (th
);
970 error ("Single stepping is not supported "
971 "in this configuration.\n");
974 win32_set_thread_context (th
);
975 th
->context
.ContextFlags
= 0;
979 /* Allow continuing with the same signal that interrupted us.
980 Otherwise complain. */
982 child_continue (continue_status
, tid
);
986 win32_add_one_solib (const char *name
, CORE_ADDR load_addr
)
988 char buf
[MAX_PATH
+ 1];
989 char buf2
[MAX_PATH
+ 1];
992 WIN32_FIND_DATA w32_fd
;
993 WCHAR wname
[MAX_PATH
+ 1];
994 mbstowcs (wname
, name
, MAX_PATH
);
995 HANDLE h
= FindFirstFile (wname
, &w32_fd
);
997 WIN32_FIND_DATAA w32_fd
;
998 HANDLE h
= FindFirstFileA (name
, &w32_fd
);
1001 /* The symbols in a dll are offset by 0x1000, which is the
1002 offset from 0 of the first byte in an image - because
1003 of the file header and the section alignment. */
1004 load_addr
+= 0x1000;
1006 if (h
== INVALID_HANDLE_VALUE
)
1014 char cwd
[MAX_PATH
+ 1];
1016 if (GetCurrentDirectoryA (MAX_PATH
+ 1, cwd
))
1018 p
= strrchr (buf
, '\\');
1021 SetCurrentDirectoryA (buf
);
1022 GetFullPathNameA (w32_fd
.cFileName
, MAX_PATH
, buf
, &p
);
1023 SetCurrentDirectoryA (cwd
);
1030 if (strcasecmp (buf
, "ntdll.dll") == 0)
1032 GetSystemDirectoryA (buf
, sizeof (buf
));
1033 strcat (buf
, "\\ntdll.dll");
1038 cygwin_conv_path (CCP_WIN_A_TO_POSIX
, buf
, buf2
, sizeof (buf2
));
1043 loaded_dll (buf2
, load_addr
);
1047 get_image_name (HANDLE h
, void *address
, int unicode
)
1049 static char buf
[(2 * MAX_PATH
) + 1];
1050 DWORD size
= unicode
? sizeof (WCHAR
) : sizeof (char);
1056 /* Attempt to read the name of the dll that was detected.
1057 This is documented to work only when actively debugging
1058 a program. It will not work for attached processes. */
1059 if (address
== NULL
)
1063 /* Windows CE reports the address of the image name,
1064 instead of an address of a pointer into the image name. */
1065 address_ptr
= address
;
1067 /* See if we could read the address of a string, and that the
1068 address isn't null. */
1069 if (!ReadProcessMemory (h
, address
, &address_ptr
,
1070 sizeof (address_ptr
), &done
)
1071 || done
!= sizeof (address_ptr
)
1076 /* Find the length of the string */
1077 while (ReadProcessMemory (h
, address_ptr
+ len
++ * size
, &b
, size
, &done
)
1078 && (b
[0] != 0 || b
[size
- 1] != 0) && done
== size
)
1082 ReadProcessMemory (h
, address_ptr
, buf
, len
, &done
);
1085 WCHAR
*unicode_address
= XALLOCAVEC (WCHAR
, len
);
1086 ReadProcessMemory (h
, address_ptr
, unicode_address
, len
* sizeof (WCHAR
),
1089 WideCharToMultiByte (CP_ACP
, 0, unicode_address
, len
, buf
, len
, 0, 0);
1095 typedef BOOL (WINAPI
*winapi_EnumProcessModules
) (HANDLE
, HMODULE
*,
1097 typedef BOOL (WINAPI
*winapi_GetModuleInformation
) (HANDLE
, HMODULE
,
1098 LPMODULEINFO
, DWORD
);
1099 typedef DWORD (WINAPI
*winapi_GetModuleFileNameExA
) (HANDLE
, HMODULE
,
1102 static winapi_EnumProcessModules win32_EnumProcessModules
;
1103 static winapi_GetModuleInformation win32_GetModuleInformation
;
1104 static winapi_GetModuleFileNameExA win32_GetModuleFileNameExA
;
1109 static int psapi_loaded
= 0;
1110 static HMODULE dll
= NULL
;
1115 dll
= LoadLibrary (TEXT("psapi.dll"));
1118 win32_EnumProcessModules
=
1119 GETPROCADDRESS (dll
, EnumProcessModules
);
1120 win32_GetModuleInformation
=
1121 GETPROCADDRESS (dll
, GetModuleInformation
);
1122 win32_GetModuleFileNameExA
=
1123 GETPROCADDRESS (dll
, GetModuleFileNameExA
);
1126 return (win32_EnumProcessModules
!= NULL
1127 && win32_GetModuleInformation
!= NULL
1128 && win32_GetModuleFileNameExA
!= NULL
);
1133 /* Iterate over all DLLs currently mapped by our inferior, and
1134 add them to our list of solibs. */
1137 win32_add_all_dlls (void)
1141 HMODULE
*DllHandle
= dh_buf
;
1149 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1154 if (!ok
|| !cbNeeded
)
1157 DllHandle
= (HMODULE
*) alloca (cbNeeded
);
1161 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1168 for (i
= 1; i
< ((size_t) cbNeeded
/ sizeof (HMODULE
)); i
++)
1171 char dll_name
[MAX_PATH
];
1173 if (!(*win32_GetModuleInformation
) (current_process_handle
,
1178 if ((*win32_GetModuleFileNameExA
) (current_process_handle
,
1183 win32_add_one_solib (dll_name
, (CORE_ADDR
) (uintptr_t) mi
.lpBaseOfDll
);
1188 typedef HANDLE (WINAPI
*winapi_CreateToolhelp32Snapshot
) (DWORD
, DWORD
);
1189 typedef BOOL (WINAPI
*winapi_Module32First
) (HANDLE
, LPMODULEENTRY32
);
1190 typedef BOOL (WINAPI
*winapi_Module32Next
) (HANDLE
, LPMODULEENTRY32
);
1192 /* Handle a DLL load event.
1194 This function assumes that this event did not occur during inferior
1195 initialization, where their event info may be incomplete (see
1196 do_initial_child_stuff and win32_add_all_dlls for more info on
1197 how we handle DLL loading during that phase). */
1200 handle_load_dll (void)
1202 LOAD_DLL_DEBUG_INFO
*event
= ¤t_event
.u
.LoadDll
;
1205 dll_name
= get_image_name (current_process_handle
,
1206 event
->lpImageName
, event
->fUnicode
);
1210 win32_add_one_solib (dll_name
, (CORE_ADDR
) (uintptr_t) event
->lpBaseOfDll
);
1213 /* Handle a DLL unload event.
1215 This function assumes that this event did not occur during inferior
1216 initialization, where their event info may be incomplete (see
1217 do_initial_child_stuff and win32_add_one_solib for more info
1218 on how we handle DLL loading during that phase). */
1221 handle_unload_dll (void)
1223 CORE_ADDR load_addr
=
1224 (CORE_ADDR
) (uintptr_t) current_event
.u
.UnloadDll
.lpBaseOfDll
;
1226 /* The symbols in a dll are offset by 0x1000, which is the
1227 offset from 0 of the first byte in an image - because
1228 of the file header and the section alignment. */
1229 load_addr
+= 0x1000;
1230 unloaded_dll (NULL
, load_addr
);
1234 handle_exception (struct target_waitstatus
*ourstatus
)
1236 DWORD code
= current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
;
1238 memcpy (&siginfo_er
, ¤t_event
.u
.Exception
.ExceptionRecord
,
1241 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
1245 case EXCEPTION_ACCESS_VIOLATION
:
1246 OUTMSG2 (("EXCEPTION_ACCESS_VIOLATION"));
1247 ourstatus
->value
.sig
= GDB_SIGNAL_SEGV
;
1249 case STATUS_STACK_OVERFLOW
:
1250 OUTMSG2 (("STATUS_STACK_OVERFLOW"));
1251 ourstatus
->value
.sig
= GDB_SIGNAL_SEGV
;
1253 case STATUS_FLOAT_DENORMAL_OPERAND
:
1254 OUTMSG2 (("STATUS_FLOAT_DENORMAL_OPERAND"));
1255 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1257 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
1258 OUTMSG2 (("EXCEPTION_ARRAY_BOUNDS_EXCEEDED"));
1259 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1261 case STATUS_FLOAT_INEXACT_RESULT
:
1262 OUTMSG2 (("STATUS_FLOAT_INEXACT_RESULT"));
1263 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1265 case STATUS_FLOAT_INVALID_OPERATION
:
1266 OUTMSG2 (("STATUS_FLOAT_INVALID_OPERATION"));
1267 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1269 case STATUS_FLOAT_OVERFLOW
:
1270 OUTMSG2 (("STATUS_FLOAT_OVERFLOW"));
1271 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1273 case STATUS_FLOAT_STACK_CHECK
:
1274 OUTMSG2 (("STATUS_FLOAT_STACK_CHECK"));
1275 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1277 case STATUS_FLOAT_UNDERFLOW
:
1278 OUTMSG2 (("STATUS_FLOAT_UNDERFLOW"));
1279 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1281 case STATUS_FLOAT_DIVIDE_BY_ZERO
:
1282 OUTMSG2 (("STATUS_FLOAT_DIVIDE_BY_ZERO"));
1283 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1285 case STATUS_INTEGER_DIVIDE_BY_ZERO
:
1286 OUTMSG2 (("STATUS_INTEGER_DIVIDE_BY_ZERO"));
1287 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1289 case STATUS_INTEGER_OVERFLOW
:
1290 OUTMSG2 (("STATUS_INTEGER_OVERFLOW"));
1291 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1293 case EXCEPTION_BREAKPOINT
:
1294 OUTMSG2 (("EXCEPTION_BREAKPOINT"));
1295 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1297 /* Remove the initial breakpoint. */
1298 check_breakpoints ((CORE_ADDR
) (long) current_event
1299 .u
.Exception
.ExceptionRecord
.ExceptionAddress
);
1303 OUTMSG2 (("DBG_CONTROL_C"));
1304 ourstatus
->value
.sig
= GDB_SIGNAL_INT
;
1306 case DBG_CONTROL_BREAK
:
1307 OUTMSG2 (("DBG_CONTROL_BREAK"));
1308 ourstatus
->value
.sig
= GDB_SIGNAL_INT
;
1310 case EXCEPTION_SINGLE_STEP
:
1311 OUTMSG2 (("EXCEPTION_SINGLE_STEP"));
1312 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1314 case EXCEPTION_ILLEGAL_INSTRUCTION
:
1315 OUTMSG2 (("EXCEPTION_ILLEGAL_INSTRUCTION"));
1316 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1318 case EXCEPTION_PRIV_INSTRUCTION
:
1319 OUTMSG2 (("EXCEPTION_PRIV_INSTRUCTION"));
1320 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1322 case EXCEPTION_NONCONTINUABLE_EXCEPTION
:
1323 OUTMSG2 (("EXCEPTION_NONCONTINUABLE_EXCEPTION"));
1324 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1327 if (current_event
.u
.Exception
.dwFirstChance
)
1329 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1332 OUTMSG2 (("gdbserver: unknown target exception 0x%08x at 0x%s",
1333 (unsigned) current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
,
1334 phex_nz ((uintptr_t) current_event
.u
.Exception
.ExceptionRecord
.
1335 ExceptionAddress
, sizeof (uintptr_t))));
1336 ourstatus
->value
.sig
= GDB_SIGNAL_UNKNOWN
;
1340 last_sig
= ourstatus
->value
.sig
;
1345 suspend_one_thread (thread_info
*thread
)
1347 win32_thread_info
*th
= (win32_thread_info
*) thread_target_data (thread
);
1351 if (SuspendThread (th
->h
) == (DWORD
) -1)
1353 DWORD err
= GetLastError ();
1354 OUTMSG (("warning: SuspendThread failed in suspend_one_thread, "
1355 "(error %d): %s\n", (int) err
, strwinerror (err
)));
1363 fake_breakpoint_event (void)
1365 OUTMSG2(("fake_breakpoint_event\n"));
1367 faked_breakpoint
= 1;
1369 memset (¤t_event
, 0, sizeof (current_event
));
1370 current_event
.dwThreadId
= main_thread_id
;
1371 current_event
.dwDebugEventCode
= EXCEPTION_DEBUG_EVENT
;
1372 current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
1373 = EXCEPTION_BREAKPOINT
;
1375 for_each_thread (suspend_one_thread
);
1380 auto_delete_breakpoint (CORE_ADDR stop_pc
)
1386 /* Get the next event from the child. */
1389 get_child_debug_event (struct target_waitstatus
*ourstatus
)
1393 last_sig
= GDB_SIGNAL_0
;
1394 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1396 /* Check if GDB sent us an interrupt request. */
1397 check_remote_input_interrupt_request ();
1399 if (soft_interrupt_requested
)
1401 soft_interrupt_requested
= 0;
1402 fake_breakpoint_event ();
1411 /* WinCE doesn't set an initial breakpoint automatically. To
1412 stop the inferior, we flush all currently pending debug
1413 events -- the thread list and the dll list are always
1414 reported immediatelly without delay, then, we suspend all
1415 threads and pretend we saw a trap at the current PC of the
1418 Contrary to desktop Windows, Windows CE *does* report the dll
1419 names on LOAD_DLL_DEBUG_EVENTs resulting from a
1420 DebugActiveProcess call. This limits the way we can detect
1421 if all the dlls have already been reported. If we get a real
1422 debug event before leaving attaching, the worst that will
1423 happen is the user will see a spurious breakpoint. */
1425 current_event
.dwDebugEventCode
= 0;
1426 if (!WaitForDebugEvent (¤t_event
, 0))
1428 OUTMSG2(("no attach events left\n"));
1429 fake_breakpoint_event ();
1433 OUTMSG2(("got attach event\n"));
1438 /* Keep the wait time low enough for comfortable remote
1439 interruption, but high enough so gdbserver doesn't become a
1441 if (!WaitForDebugEvent (¤t_event
, 250))
1443 DWORD e
= GetLastError();
1445 if (e
== ERROR_PIPE_NOT_CONNECTED
)
1447 /* This will happen if the loader fails to succesfully
1448 load the application, e.g., if the main executable
1449 tries to pull in a non-existing export from a
1451 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1452 ourstatus
->value
.integer
= 1;
1462 switch (current_event
.dwDebugEventCode
)
1464 case CREATE_THREAD_DEBUG_EVENT
:
1465 OUTMSG2 (("gdbserver: kernel event CREATE_THREAD_DEBUG_EVENT "
1466 "for pid=%u tid=%x)\n",
1467 (unsigned) current_event
.dwProcessId
,
1468 (unsigned) current_event
.dwThreadId
));
1470 /* Record the existence of this thread. */
1471 child_add_thread (current_event
.dwProcessId
,
1472 current_event
.dwThreadId
,
1473 current_event
.u
.CreateThread
.hThread
,
1474 current_event
.u
.CreateThread
.lpThreadLocalBase
);
1477 case EXIT_THREAD_DEBUG_EVENT
:
1478 OUTMSG2 (("gdbserver: kernel event EXIT_THREAD_DEBUG_EVENT "
1479 "for pid=%u tid=%x\n",
1480 (unsigned) current_event
.dwProcessId
,
1481 (unsigned) current_event
.dwThreadId
));
1482 child_delete_thread (current_event
.dwProcessId
,
1483 current_event
.dwThreadId
);
1485 current_thread
= get_first_thread ();
1488 case CREATE_PROCESS_DEBUG_EVENT
:
1489 OUTMSG2 (("gdbserver: kernel event CREATE_PROCESS_DEBUG_EVENT "
1490 "for pid=%u tid=%x\n",
1491 (unsigned) current_event
.dwProcessId
,
1492 (unsigned) current_event
.dwThreadId
));
1493 CloseHandle (current_event
.u
.CreateProcessInfo
.hFile
);
1495 current_process_handle
= current_event
.u
.CreateProcessInfo
.hProcess
;
1496 main_thread_id
= current_event
.dwThreadId
;
1498 /* Add the main thread. */
1499 child_add_thread (current_event
.dwProcessId
,
1501 current_event
.u
.CreateProcessInfo
.hThread
,
1502 current_event
.u
.CreateProcessInfo
.lpThreadLocalBase
);
1507 /* Windows CE doesn't set the initial breakpoint
1508 automatically like the desktop versions of Windows do.
1509 We add it explicitly here. It will be removed as soon as
1511 set_breakpoint_at ((CORE_ADDR
) (long) current_event
.u
1512 .CreateProcessInfo
.lpStartAddress
,
1513 auto_delete_breakpoint
);
1518 case EXIT_PROCESS_DEBUG_EVENT
:
1519 OUTMSG2 (("gdbserver: kernel event EXIT_PROCESS_DEBUG_EVENT "
1520 "for pid=%u tid=%x\n",
1521 (unsigned) current_event
.dwProcessId
,
1522 (unsigned) current_event
.dwThreadId
));
1524 DWORD exit_status
= current_event
.u
.ExitProcess
.dwExitCode
;
1525 /* If the exit status looks like a fatal exception, but we
1526 don't recognize the exception's code, make the original
1527 exit status value available, to avoid losing information. */
1529 = WIFSIGNALED (exit_status
) ? WTERMSIG (exit_status
) : -1;
1530 if (exit_signal
== -1)
1532 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1533 ourstatus
->value
.integer
= exit_status
;
1537 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
1538 ourstatus
->value
.sig
= gdb_signal_from_host (exit_signal
);
1541 child_continue (DBG_CONTINUE
, -1);
1542 CloseHandle (current_process_handle
);
1543 current_process_handle
= NULL
;
1546 case LOAD_DLL_DEBUG_EVENT
:
1547 OUTMSG2 (("gdbserver: kernel event LOAD_DLL_DEBUG_EVENT "
1548 "for pid=%u tid=%x\n",
1549 (unsigned) current_event
.dwProcessId
,
1550 (unsigned) current_event
.dwThreadId
));
1551 CloseHandle (current_event
.u
.LoadDll
.hFile
);
1552 if (! child_initialization_done
)
1556 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1557 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1560 case UNLOAD_DLL_DEBUG_EVENT
:
1561 OUTMSG2 (("gdbserver: kernel event UNLOAD_DLL_DEBUG_EVENT "
1562 "for pid=%u tid=%x\n",
1563 (unsigned) current_event
.dwProcessId
,
1564 (unsigned) current_event
.dwThreadId
));
1565 if (! child_initialization_done
)
1567 handle_unload_dll ();
1568 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1569 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1572 case EXCEPTION_DEBUG_EVENT
:
1573 OUTMSG2 (("gdbserver: kernel event EXCEPTION_DEBUG_EVENT "
1574 "for pid=%u tid=%x\n",
1575 (unsigned) current_event
.dwProcessId
,
1576 (unsigned) current_event
.dwThreadId
));
1577 handle_exception (ourstatus
);
1580 case OUTPUT_DEBUG_STRING_EVENT
:
1581 /* A message from the kernel (or Cygwin). */
1582 OUTMSG2 (("gdbserver: kernel event OUTPUT_DEBUG_STRING_EVENT "
1583 "for pid=%u tid=%x\n",
1584 (unsigned) current_event
.dwProcessId
,
1585 (unsigned) current_event
.dwThreadId
));
1586 handle_output_debug_string ();
1590 OUTMSG2 (("gdbserver: kernel event unknown "
1591 "for pid=%u tid=%x code=%x\n",
1592 (unsigned) current_event
.dwProcessId
,
1593 (unsigned) current_event
.dwThreadId
,
1594 (unsigned) current_event
.dwDebugEventCode
));
1598 ptid
= debug_event_ptid (¤t_event
);
1599 current_thread
= find_thread_ptid (ptid
);
1603 /* Wait for the inferior process to change state.
1604 STATUS will be filled in with a response code to send to GDB.
1605 Returns the signal which caused the process to stop. */
1607 win32_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
, int options
)
1609 struct regcache
*regcache
;
1611 if (cached_status
.kind
!= TARGET_WAITKIND_IGNORE
)
1613 /* The core always does a wait after creating the inferior, and
1614 do_initial_child_stuff already ran the inferior to the
1615 initial breakpoint (or an exit, if creating the process
1616 fails). Report it now. */
1617 *ourstatus
= cached_status
;
1618 cached_status
.kind
= TARGET_WAITKIND_IGNORE
;
1619 return debug_event_ptid (¤t_event
);
1624 if (!get_child_debug_event (ourstatus
))
1627 switch (ourstatus
->kind
)
1629 case TARGET_WAITKIND_EXITED
:
1630 OUTMSG2 (("Child exited with retcode = %x\n",
1631 ourstatus
->value
.integer
));
1632 win32_clear_inferiors ();
1633 return ptid_t (current_event
.dwProcessId
);
1634 case TARGET_WAITKIND_STOPPED
:
1635 case TARGET_WAITKIND_SIGNALLED
:
1636 case TARGET_WAITKIND_LOADED
:
1637 OUTMSG2 (("Child Stopped with signal = %d \n",
1638 ourstatus
->value
.sig
));
1640 regcache
= get_thread_regcache (current_thread
, 1);
1641 child_fetch_inferior_registers (regcache
, -1);
1642 return debug_event_ptid (¤t_event
);
1644 OUTMSG (("Ignoring unknown internal event, %d\n", ourstatus
->kind
));
1646 case TARGET_WAITKIND_SPURIOUS
:
1647 /* do nothing, just continue */
1648 child_continue (DBG_CONTINUE
, -1);
1654 /* Fetch registers from the inferior process.
1655 If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO. */
1657 win32_fetch_inferior_registers (struct regcache
*regcache
, int regno
)
1659 child_fetch_inferior_registers (regcache
, regno
);
1662 /* Store registers to the inferior process.
1663 If REGNO is -1, store all registers; otherwise, store at least REGNO. */
1665 win32_store_inferior_registers (struct regcache
*regcache
, int regno
)
1667 child_store_inferior_registers (regcache
, regno
);
1670 /* Read memory from the inferior process. This should generally be
1671 called through read_inferior_memory, which handles breakpoint shadowing.
1672 Read LEN bytes at MEMADDR into a buffer at MYADDR. */
1674 win32_read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
1676 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 0, 0) != len
;
1679 /* Write memory to the inferior process. This should generally be
1680 called through write_inferior_memory, which handles breakpoint shadowing.
1681 Write LEN bytes from the buffer at MYADDR to MEMADDR.
1682 Returns 0 on success and errno on failure. */
1684 win32_write_inferior_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
,
1687 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 1, 0) != len
;
1690 /* Send an interrupt request to the inferior process. */
1692 win32_request_interrupt (void)
1694 winapi_DebugBreakProcess DebugBreakProcess
;
1695 winapi_GenerateConsoleCtrlEvent GenerateConsoleCtrlEvent
;
1698 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
1700 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
1703 GenerateConsoleCtrlEvent
= GETPROCADDRESS (dll
, GenerateConsoleCtrlEvent
);
1705 if (GenerateConsoleCtrlEvent
!= NULL
1706 && GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT
, current_process_id
))
1709 /* GenerateConsoleCtrlEvent can fail if process id being debugged is
1710 not a process group id.
1711 Fallback to XP/Vista 'DebugBreakProcess', which generates a
1712 breakpoint exception in the interior process. */
1714 DebugBreakProcess
= GETPROCADDRESS (dll
, DebugBreakProcess
);
1716 if (DebugBreakProcess
!= NULL
1717 && DebugBreakProcess (current_process_handle
))
1720 /* Last resort, suspend all threads manually. */
1721 soft_interrupt_requested
= 1;
1726 win32_error_to_fileio_error (DWORD err
)
1730 case ERROR_BAD_PATHNAME
:
1731 case ERROR_FILE_NOT_FOUND
:
1732 case ERROR_INVALID_NAME
:
1733 case ERROR_PATH_NOT_FOUND
:
1734 return FILEIO_ENOENT
;
1736 case ERROR_IO_DEVICE
:
1737 case ERROR_OPEN_FAILED
:
1739 case ERROR_INVALID_HANDLE
:
1740 return FILEIO_EBADF
;
1741 case ERROR_ACCESS_DENIED
:
1742 case ERROR_SHARING_VIOLATION
:
1743 return FILEIO_EACCES
;
1744 case ERROR_NOACCESS
:
1745 return FILEIO_EFAULT
;
1747 return FILEIO_EBUSY
;
1748 case ERROR_ALREADY_EXISTS
:
1749 case ERROR_FILE_EXISTS
:
1750 return FILEIO_EEXIST
;
1751 case ERROR_BAD_DEVICE
:
1752 return FILEIO_ENODEV
;
1753 case ERROR_DIRECTORY
:
1754 return FILEIO_ENOTDIR
;
1755 case ERROR_FILENAME_EXCED_RANGE
:
1756 case ERROR_INVALID_DATA
:
1757 case ERROR_INVALID_PARAMETER
:
1758 case ERROR_NEGATIVE_SEEK
:
1759 return FILEIO_EINVAL
;
1760 case ERROR_TOO_MANY_OPEN_FILES
:
1761 return FILEIO_EMFILE
;
1762 case ERROR_HANDLE_DISK_FULL
:
1763 case ERROR_DISK_FULL
:
1764 return FILEIO_ENOSPC
;
1765 case ERROR_WRITE_PROTECT
:
1766 return FILEIO_EROFS
;
1767 case ERROR_NOT_SUPPORTED
:
1768 return FILEIO_ENOSYS
;
1771 return FILEIO_EUNKNOWN
;
1775 wince_hostio_last_error (char *buf
)
1777 DWORD winerr
= GetLastError ();
1778 int fileio_err
= win32_error_to_fileio_error (winerr
);
1779 sprintf (buf
, "F-1,%x", fileio_err
);
1783 /* Write Windows signal info. */
1786 win32_xfer_siginfo (const char *annex
, unsigned char *readbuf
,
1787 unsigned const char *writebuf
, CORE_ADDR offset
, int len
)
1789 if (siginfo_er
.ExceptionCode
== 0)
1792 if (readbuf
== nullptr)
1795 if (offset
> sizeof (siginfo_er
))
1798 if (offset
+ len
> sizeof (siginfo_er
))
1799 len
= sizeof (siginfo_er
) - offset
;
1801 memcpy (readbuf
, (char *) &siginfo_er
+ offset
, len
);
1806 /* Write Windows OS Thread Information Block address. */
1809 win32_get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
)
1811 win32_thread_info
*th
;
1812 th
= thread_rec (ptid
, 0);
1816 *addr
= th
->thread_local_base
;
1820 /* Implementation of the target_ops method "sw_breakpoint_from_kind". */
1822 static const gdb_byte
*
1823 win32_sw_breakpoint_from_kind (int kind
, int *size
)
1825 *size
= the_low_target
.breakpoint_len
;
1826 return the_low_target
.breakpoint
;
1829 static process_stratum_target win32_target_ops
= {
1830 win32_create_inferior
,
1831 NULL
, /* post_create_inferior */
1840 win32_fetch_inferior_registers
,
1841 win32_store_inferior_registers
,
1842 NULL
, /* prepare_to_access_memory */
1843 NULL
, /* done_accessing_memory */
1844 win32_read_inferior_memory
,
1845 win32_write_inferior_memory
,
1846 NULL
, /* lookup_symbols */
1847 win32_request_interrupt
,
1848 NULL
, /* read_auxv */
1849 win32_supports_z_point_type
,
1852 NULL
, /* stopped_by_sw_breakpoint */
1853 NULL
, /* supports_stopped_by_sw_breakpoint */
1854 NULL
, /* stopped_by_hw_breakpoint */
1855 NULL
, /* supports_stopped_by_hw_breakpoint */
1856 target_can_do_hardware_single_step
,
1857 win32_stopped_by_watchpoint
,
1858 win32_stopped_data_address
,
1859 NULL
, /* read_offsets */
1860 NULL
, /* get_tls_address */
1862 wince_hostio_last_error
,
1864 hostio_last_error_from_errno
,
1866 NULL
, /* qxfer_osdata */
1868 NULL
, /* supports_non_stop */
1870 NULL
, /* start_non_stop */
1871 NULL
, /* supports_multi_process */
1872 NULL
, /* supports_fork_events */
1873 NULL
, /* supports_vfork_events */
1874 NULL
, /* supports_exec_events */
1875 NULL
, /* handle_new_gdb_connection */
1876 NULL
, /* handle_monitor_command */
1877 NULL
, /* core_of_thread */
1878 NULL
, /* read_loadmap */
1879 NULL
, /* process_qsupported */
1880 NULL
, /* supports_tracepoints */
1882 NULL
, /* write_pc */
1883 NULL
, /* thread_stopped */
1884 win32_get_tib_address
,
1885 NULL
, /* pause_all */
1886 NULL
, /* unpause_all */
1887 NULL
, /* stabilize_threads */
1888 NULL
, /* install_fast_tracepoint_jump_pad */
1889 NULL
, /* emit_ops */
1890 NULL
, /* supports_disable_randomization */
1891 NULL
, /* get_min_fast_tracepoint_insn_len */
1892 NULL
, /* qxfer_libraries_svr4 */
1893 NULL
, /* support_agent */
1894 NULL
, /* enable_btrace */
1895 NULL
, /* disable_btrace */
1896 NULL
, /* read_btrace */
1897 NULL
, /* read_btrace_conf */
1898 NULL
, /* supports_range_stepping */
1899 NULL
, /* pid_to_exec_file */
1900 NULL
, /* multifs_open */
1901 NULL
, /* multifs_unlink */
1902 NULL
, /* multifs_readlink */
1903 NULL
, /* breakpoint_kind_from_pc */
1904 win32_sw_breakpoint_from_kind
,
1907 /* Initialize the Win32 backend. */
1909 initialize_low (void)
1911 set_target_ops (&win32_target_ops
);
1912 the_low_target
.arch_setup ();