1 /* Low level interface to Windows debugging, for gdbserver.
2 Copyright (C) 2006-2021 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"
39 using namespace windows_nat
;
42 #include <sys/cygwin.h>
45 #define OUTMSG(X) do { printf X; fflush (stderr); } while (0)
58 #define _T(x) TEXT (x)
62 #define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0]))
66 # define GETPROCADDRESS(DLL, PROC) \
67 ((winapi_ ## PROC) GetProcAddress (DLL, TEXT (#PROC)))
69 # define GETPROCADDRESS(DLL, PROC) \
70 ((winapi_ ## PROC) GetProcAddress (DLL, #PROC))
73 int using_threads
= 1;
76 static int attaching
= 0;
78 /* A status that hasn't been reported to the core yet, and so
79 win32_wait should return it next, instead of fetching the next
80 debug event off the win32 API. */
81 static struct target_waitstatus cached_status
;
83 /* Non zero if an interrupt request is to be satisfied by suspending
85 static int soft_interrupt_requested
= 0;
87 /* Non zero if the inferior is stopped in a simulated breakpoint done
88 by suspending all the threads. */
89 static int faked_breakpoint
= 0;
91 /* True if current_process_handle needs to be closed. */
92 static bool open_process_used
= false;
94 const struct target_desc
*win32_tdesc
;
96 const struct target_desc
*wow64_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
);
107 typedef BOOL (WINAPI
*winapi_Wow64SetThreadContext
) (HANDLE
,
108 const WOW64_CONTEXT
*);
110 winapi_Wow64GetThreadContext win32_Wow64GetThreadContext
;
111 static winapi_Wow64SetThreadContext win32_Wow64SetThreadContext
;
115 static void win32_add_all_dlls (void);
118 /* Get the thread ID from the current selected inferior (the current
121 current_thread_ptid (void)
126 /* The current debug event from WaitForDebugEvent. */
128 debug_event_ptid (DEBUG_EVENT
*event
)
130 return ptid_t (event
->dwProcessId
, event
->dwThreadId
, 0);
133 /* Get the thread context of the thread associated with TH. */
136 win32_get_thread_context (windows_thread_info
*th
)
140 memset (&th
->wow64_context
, 0, sizeof (WOW64_CONTEXT
));
143 memset (&th
->context
, 0, sizeof (CONTEXT
));
144 (*the_low_target
.get_thread_context
) (th
);
146 memcpy (&th
->base_context
, &th
->context
, sizeof (CONTEXT
));
150 /* Set the thread context of the thread associated with TH. */
153 win32_set_thread_context (windows_thread_info
*th
)
156 /* Calling SuspendThread on a thread that is running kernel code
157 will report that the suspending was successful, but in fact, that
158 will often not be true. In those cases, the context returned by
159 GetThreadContext will not be correct by the time the thread
160 stops, hence we can't set that context back into the thread when
161 resuming - it will most likely crash the inferior.
162 Unfortunately, there is no way to know when the thread will
163 really stop. To work around it, we'll only write the context
164 back to the thread when either the user or GDB explicitly change
165 it between stopping and resuming. */
166 if (memcmp (&th
->context
, &th
->base_context
, sizeof (CONTEXT
)) != 0)
171 win32_Wow64SetThreadContext (th
->h
, &th
->wow64_context
);
174 SetThreadContext (th
->h
, &th
->context
);
178 /* Set the thread context of the thread associated with TH. */
181 win32_prepare_to_resume (windows_thread_info
*th
)
183 if (the_low_target
.prepare_to_resume
!= NULL
)
184 (*the_low_target
.prepare_to_resume
) (th
);
187 /* See win32-low.h. */
190 win32_require_context (windows_thread_info
*th
)
195 context_flags
= th
->wow64_context
.ContextFlags
;
198 context_flags
= th
->context
.ContextFlags
;
199 if (context_flags
== 0)
202 win32_get_thread_context (th
);
206 /* See nat/windows-nat.h. */
208 windows_thread_info
*
209 windows_nat::thread_rec (ptid_t ptid
, thread_disposition_type disposition
)
211 thread_info
*thread
= find_thread_ptid (ptid
);
215 windows_thread_info
*th
= (windows_thread_info
*) thread_target_data (thread
);
216 if (disposition
!= DONT_INVALIDATE_CONTEXT
)
217 win32_require_context (th
);
221 /* Add a thread to the thread list. */
222 static windows_thread_info
*
223 child_add_thread (DWORD pid
, DWORD tid
, HANDLE h
, void *tlb
)
225 windows_thread_info
*th
;
226 ptid_t ptid
= ptid_t (pid
, tid
, 0);
228 if ((th
= thread_rec (ptid
, DONT_INVALIDATE_CONTEXT
)))
231 CORE_ADDR base
= (CORE_ADDR
) (uintptr_t) tlb
;
233 /* For WOW64 processes, this is actually the pointer to the 64bit TIB,
234 and the 32bit TIB is exactly 2 pages after it. */
236 base
+= 2 * 4096; /* page size = 4096 */
238 th
= new windows_thread_info (tid
, h
, base
);
240 add_thread (ptid
, th
);
242 if (the_low_target
.thread_added
!= NULL
)
243 (*the_low_target
.thread_added
) (th
);
248 /* Delete a thread from the list of threads. */
250 delete_thread_info (thread_info
*thread
)
252 windows_thread_info
*th
= (windows_thread_info
*) thread_target_data (thread
);
254 remove_thread (thread
);
258 /* Delete a thread from the list of threads. */
260 child_delete_thread (DWORD pid
, DWORD tid
)
262 /* If the last thread is exiting, just return. */
263 if (all_threads
.size () == 1)
266 thread_info
*thread
= find_thread_ptid (ptid_t (pid
, tid
));
270 delete_thread_info (thread
);
273 /* These watchpoint related wrapper functions simply pass on the function call
274 if the low target has registered a corresponding function. */
277 win32_process_target::supports_z_point_type (char z_type
)
279 return (z_type
== Z_PACKET_SW_BP
280 || (the_low_target
.supports_z_point_type
!= NULL
281 && the_low_target
.supports_z_point_type (z_type
)));
285 win32_process_target::insert_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
286 int size
, raw_breakpoint
*bp
)
288 if (type
== raw_bkpt_type_sw
)
289 return insert_memory_breakpoint (bp
);
290 else if (the_low_target
.insert_point
!= NULL
)
291 return the_low_target
.insert_point (type
, addr
, size
, bp
);
293 /* Unsupported (see target.h). */
298 win32_process_target::remove_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
299 int size
, raw_breakpoint
*bp
)
301 if (type
== raw_bkpt_type_sw
)
302 return remove_memory_breakpoint (bp
);
303 else if (the_low_target
.remove_point
!= NULL
)
304 return the_low_target
.remove_point (type
, addr
, size
, bp
);
306 /* Unsupported (see target.h). */
311 win32_process_target::stopped_by_watchpoint ()
313 if (the_low_target
.stopped_by_watchpoint
!= NULL
)
314 return the_low_target
.stopped_by_watchpoint ();
320 win32_process_target::stopped_data_address ()
322 if (the_low_target
.stopped_data_address
!= NULL
)
323 return the_low_target
.stopped_data_address ();
329 /* Transfer memory from/to the debugged process. */
331 child_xfer_memory (CORE_ADDR memaddr
, char *our
, int len
,
332 int write
, process_stratum_target
*target
)
337 uintptr_t addr
= (uintptr_t) memaddr
;
341 success
= WriteProcessMemory (current_process_handle
, (LPVOID
) addr
,
342 (LPCVOID
) our
, len
, &done
);
344 lasterror
= GetLastError ();
345 FlushInstructionCache (current_process_handle
, (LPCVOID
) addr
, len
);
349 success
= ReadProcessMemory (current_process_handle
, (LPCVOID
) addr
,
350 (LPVOID
) our
, len
, &done
);
352 lasterror
= GetLastError ();
354 if (!success
&& lasterror
== ERROR_PARTIAL_COPY
&& done
> 0)
357 return success
? done
: -1;
360 /* Clear out any old thread list and reinitialize it to a pristine
363 child_init_thread_list (void)
365 for_each_thread (delete_thread_info
);
368 /* Zero during the child initialization phase, and nonzero otherwise. */
370 static int child_initialization_done
= 0;
373 do_initial_child_stuff (HANDLE proch
, DWORD pid
, int attached
)
375 struct process_info
*proc
;
377 last_sig
= GDB_SIGNAL_0
;
379 current_process_handle
= proch
;
380 current_process_id
= pid
;
383 soft_interrupt_requested
= 0;
384 faked_breakpoint
= 0;
385 open_process_used
= true;
387 memset (¤t_event
, 0, sizeof (current_event
));
391 if (!IsWow64Process (proch
, &wow64
))
393 DWORD err
= GetLastError ();
394 error ("Check if WOW64 process failed (error %d): %s\n",
395 (int) err
, strwinerror (err
));
397 wow64_process
= wow64
;
400 && (win32_Wow64GetThreadContext
== nullptr
401 || win32_Wow64SetThreadContext
== nullptr))
402 error ("WOW64 debugging is not supported on this system.\n");
404 ignore_first_breakpoint
= !attached
&& wow64_process
;
407 proc
= add_process (pid
, attached
);
410 proc
->tdesc
= wow64_win32_tdesc
;
413 proc
->tdesc
= win32_tdesc
;
414 child_init_thread_list ();
415 child_initialization_done
= 0;
417 if (the_low_target
.initial_stuff
!= NULL
)
418 (*the_low_target
.initial_stuff
) ();
420 cached_status
.kind
= TARGET_WAITKIND_IGNORE
;
422 /* Flush all currently pending debug events (thread and dll list) up
423 to the initial breakpoint. */
426 struct target_waitstatus status
;
428 the_target
->wait (minus_one_ptid
, &status
, 0);
430 /* Note win32_wait doesn't return thread events. */
431 if (status
.kind
!= TARGET_WAITKIND_LOADED
)
433 cached_status
= status
;
438 struct thread_resume resume
;
440 resume
.thread
= minus_one_ptid
;
441 resume
.kind
= resume_continue
;
444 the_target
->resume (&resume
, 1);
449 /* Now that the inferior has been started and all DLLs have been mapped,
450 we can iterate over all DLLs and load them in.
452 We avoid doing it any earlier because, on certain versions of Windows,
453 LOAD_DLL_DEBUG_EVENTs are sometimes not complete. In particular,
454 we have seen on Windows 8.1 that the ntdll.dll load event does not
455 include the DLL name, preventing us from creating an associated SO.
456 A possible explanation is that ntdll.dll might be mapped before
457 the SO info gets created by the Windows system -- ntdll.dll is
458 the first DLL to be reported via LOAD_DLL_DEBUG_EVENT and other DLLs
459 do not seem to suffer from that problem.
461 Rather than try to work around this sort of issue, it is much
462 simpler to just ignore DLL load/unload events during the startup
463 phase, and then process them all in one batch now. */
464 win32_add_all_dlls ();
467 child_initialization_done
= 1;
470 /* Resume all artificially suspended threads if we are continuing
473 continue_one_thread (thread_info
*thread
, int thread_id
)
475 windows_thread_info
*th
= (windows_thread_info
*) thread_target_data (thread
);
477 if (thread_id
== -1 || thread_id
== th
->tid
)
479 win32_prepare_to_resume (th
);
483 DWORD
*context_flags
;
486 context_flags
= &th
->wow64_context
.ContextFlags
;
489 context_flags
= &th
->context
.ContextFlags
;
492 win32_set_thread_context (th
);
502 child_continue (DWORD continue_status
, int thread_id
)
504 desired_stop_thread_id
= thread_id
;
505 if (matching_pending_stop (debug_threads
))
508 /* The inferior will only continue after the ContinueDebugEvent
510 for_each_thread ([&] (thread_info
*thread
)
512 continue_one_thread (thread
, thread_id
);
514 faked_breakpoint
= 0;
516 return continue_last_debug_event (continue_status
, debug_threads
);
519 /* Fetch register(s) from the current thread context. */
521 child_fetch_inferior_registers (struct regcache
*regcache
, int r
)
524 windows_thread_info
*th
= thread_rec (current_thread_ptid (),
526 if (r
== -1 || r
> NUM_REGS
)
527 child_fetch_inferior_registers (regcache
, NUM_REGS
);
529 for (regno
= 0; regno
< r
; regno
++)
530 (*the_low_target
.fetch_inferior_register
) (regcache
, th
, regno
);
533 /* Store a new register value into the current thread context. We don't
534 change the program's context until later, when we resume it. */
536 child_store_inferior_registers (struct regcache
*regcache
, int r
)
539 windows_thread_info
*th
= thread_rec (current_thread_ptid (),
541 if (r
== -1 || r
== 0 || r
> NUM_REGS
)
542 child_store_inferior_registers (regcache
, NUM_REGS
);
544 for (regno
= 0; regno
< r
; regno
++)
545 (*the_low_target
.store_inferior_register
) (regcache
, th
, regno
);
548 /* Map the Windows error number in ERROR to a locale-dependent error
549 message string and return a pointer to it. Typically, the values
550 for ERROR come from GetLastError.
552 The string pointed to shall not be modified by the application,
553 but may be overwritten by a subsequent call to strwinerror
555 The strwinerror function does not change the current setting
559 strwinerror (DWORD error
)
561 static char buf
[1024];
563 DWORD lasterr
= GetLastError ();
564 DWORD chars
= FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
565 | FORMAT_MESSAGE_ALLOCATE_BUFFER
,
568 0, /* Default language */
574 /* If there is an \r\n appended, zap it. */
576 && msgbuf
[chars
- 2] == '\r'
577 && msgbuf
[chars
- 1] == '\n')
583 if (chars
> ((COUNTOF (buf
)) - 1))
585 chars
= COUNTOF (buf
) - 1;
590 wcstombs (buf
, msgbuf
, chars
+ 1);
592 strncpy (buf
, msgbuf
, chars
+ 1);
597 sprintf (buf
, "unknown win32 error (%u)", (unsigned) error
);
599 SetLastError (lasterr
);
604 create_process (const char *program
, char *args
,
605 DWORD flags
, PROCESS_INFORMATION
*pi
)
607 const char *inferior_cwd
= get_inferior_cwd ();
609 size_t argslen
, proglen
;
611 proglen
= strlen (program
) + 1;
612 argslen
= strlen (args
) + proglen
;
615 wchar_t *p
, *wprogram
, *wargs
, *wcwd
= NULL
;
617 wprogram
= (wchar_t *) alloca (proglen
* sizeof (wchar_t));
618 mbstowcs (wprogram
, program
, proglen
);
620 for (p
= wprogram
; *p
; ++p
)
624 wargs
= alloca ((argslen
+ 1) * sizeof (wchar_t));
625 wcscpy (wargs
, wprogram
);
626 wcscat (wargs
, L
" ");
627 mbstowcs (wargs
+ proglen
, args
, argslen
+ 1 - proglen
);
629 if (inferior_cwd
!= NULL
)
631 std::string expanded_infcwd
= gdb_tilde_expand (inferior_cwd
);
632 std::replace (expanded_infcwd
.begin (), expanded_infcwd
.end (),
634 wcwd
= alloca ((expanded_infcwd
.size () + 1) * sizeof (wchar_t));
635 if (mbstowcs (wcwd
, expanded_infcwd
.c_str (),
636 expanded_infcwd
.size () + 1) == NULL
)
639 Could not convert the expanded inferior cwd to wide-char."));
643 ret
= CreateProcessW (wprogram
, /* image name */
644 wargs
, /* command line */
645 NULL
, /* security, not supported */
646 NULL
, /* thread, not supported */
647 FALSE
, /* inherit handles, not supported */
648 flags
, /* start flags */
649 NULL
, /* environment, not supported */
650 wcwd
, /* current directory */
651 NULL
, /* start info, not supported */
654 STARTUPINFOA si
= { sizeof (STARTUPINFOA
) };
655 char *program_and_args
= (char *) alloca (argslen
+ 1);
657 strcpy (program_and_args
, program
);
658 strcat (program_and_args
, " ");
659 strcat (program_and_args
, args
);
660 ret
= CreateProcessA (program
, /* image name */
661 program_and_args
, /* command line */
664 TRUE
, /* inherit handles */
665 flags
, /* start flags */
666 NULL
, /* environment */
667 /* current directory */
668 (inferior_cwd
== NULL
670 : gdb_tilde_expand (inferior_cwd
).c_str()),
671 &si
, /* start info */
678 /* Start a new process.
679 PROGRAM is the program name.
680 PROGRAM_ARGS is the vector containing the inferior's args.
681 Returns the new PID on success, -1 on failure. Registers the new
682 process with the process list. */
684 win32_process_target::create_inferior (const char *program
,
685 const std::vector
<char *> &program_args
)
687 client_state
&cs
= get_client_state ();
689 char real_path
[PATH_MAX
];
690 char *orig_path
, *new_path
, *path_ptr
;
694 PROCESS_INFORMATION pi
;
696 std::string str_program_args
= construct_inferior_arguments (program_args
);
697 char *args
= (char *) str_program_args
.c_str ();
699 /* win32_wait needs to know we're not attaching. */
703 error ("No executable specified, specify executable to debug.\n");
705 flags
= DEBUG_PROCESS
| DEBUG_ONLY_THIS_PROCESS
;
709 path_ptr
= getenv ("PATH");
712 int size
= cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, NULL
, 0);
713 orig_path
= (char *) alloca (strlen (path_ptr
) + 1);
714 new_path
= (char *) alloca (size
);
715 strcpy (orig_path
, path_ptr
);
716 cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, new_path
, size
);
717 setenv ("PATH", new_path
, 1);
719 cygwin_conv_path (CCP_POSIX_TO_WIN_A
, program
, real_path
, PATH_MAX
);
723 OUTMSG2 (("Command line is \"%s %s\"\n", program
, args
));
725 #ifdef CREATE_NEW_PROCESS_GROUP
726 flags
|= CREATE_NEW_PROCESS_GROUP
;
729 ret
= create_process (program
, args
, flags
, &pi
);
730 err
= GetLastError ();
731 if (!ret
&& err
== ERROR_FILE_NOT_FOUND
)
733 char *exename
= (char *) alloca (strlen (program
) + 5);
734 strcat (strcpy (exename
, program
), ".exe");
735 ret
= create_process (exename
, args
, flags
, &pi
);
736 err
= GetLastError ();
741 setenv ("PATH", orig_path
, 1);
746 error ("Error creating process \"%s %s\", (error %d): %s\n",
747 program
, args
, (int) err
, strwinerror (err
));
751 OUTMSG2 (("Process created: %s %s\n", program
, (char *) args
));
755 /* On Windows CE this handle can't be closed. The OS reuses
756 it in the debug events, while the 9x/NT versions of Windows
757 probably use a DuplicateHandle'd one. */
758 CloseHandle (pi
.hThread
);
761 do_initial_child_stuff (pi
.hProcess
, pi
.dwProcessId
, 0);
763 /* Wait till we are at 1st instruction in program, return new pid
764 (assuming success). */
765 cs
.last_ptid
= wait (ptid_t (current_process_id
), &cs
.last_status
, 0);
767 /* Necessary for handle_v_kill. */
768 signal_pid
= current_process_id
;
770 return current_process_id
;
773 /* Attach to a running process.
774 PID is the process ID to attach to, specified by the user
775 or a higher layer. */
777 win32_process_target::attach (unsigned long pid
)
780 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
783 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
785 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
787 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
789 h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
792 if (DebugActiveProcess (pid
))
794 if (DebugSetProcessKillOnExit
!= NULL
)
795 DebugSetProcessKillOnExit (FALSE
);
797 /* win32_wait needs to know we're attaching. */
799 do_initial_child_stuff (h
, pid
, 1);
806 err
= GetLastError ();
807 error ("Attach to process failed (error %d): %s\n",
808 (int) err
, strwinerror (err
));
811 /* See nat/windows-nat.h. */
814 windows_nat::handle_output_debug_string (struct target_waitstatus
*ourstatus
)
816 #define READ_BUFFER_LEN 1024
818 char s
[READ_BUFFER_LEN
+ 1] = { 0 };
819 DWORD nbytes
= current_event
.u
.DebugString
.nDebugStringLength
;
824 if (nbytes
> READ_BUFFER_LEN
)
825 nbytes
= READ_BUFFER_LEN
;
827 addr
= (CORE_ADDR
) (size_t) current_event
.u
.DebugString
.lpDebugStringData
;
829 if (current_event
.u
.DebugString
.fUnicode
)
831 /* The event tells us how many bytes, not chars, even
833 WCHAR buffer
[(READ_BUFFER_LEN
+ 1) / sizeof (WCHAR
)] = { 0 };
834 if (read_inferior_memory (addr
, (unsigned char *) buffer
, nbytes
) != 0)
836 wcstombs (s
, buffer
, (nbytes
+ 1) / sizeof (WCHAR
));
840 if (read_inferior_memory (addr
, (unsigned char *) s
, nbytes
) != 0)
844 if (!startswith (s
, "cYg"))
854 #undef READ_BUFFER_LEN
860 win32_clear_inferiors (void)
862 if (open_process_used
)
864 CloseHandle (current_process_handle
);
865 open_process_used
= false;
868 for_each_thread (delete_thread_info
);
869 siginfo_er
.ExceptionCode
= 0;
873 /* Implementation of target_ops::kill. */
876 win32_process_target::kill (process_info
*process
)
878 TerminateProcess (current_process_handle
, 0);
881 if (!child_continue (DBG_CONTINUE
, -1))
883 if (!wait_for_debug_event (¤t_event
, INFINITE
))
885 if (current_event
.dwDebugEventCode
== EXIT_PROCESS_DEBUG_EVENT
)
887 else if (current_event
.dwDebugEventCode
== OUTPUT_DEBUG_STRING_EVENT
)
888 handle_output_debug_string (nullptr);
891 win32_clear_inferiors ();
893 remove_process (process
);
897 /* Implementation of target_ops::detach. */
900 win32_process_target::detach (process_info
*process
)
902 winapi_DebugActiveProcessStop DebugActiveProcessStop
= NULL
;
903 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
905 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
907 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
909 DebugActiveProcessStop
= GETPROCADDRESS (dll
, DebugActiveProcessStop
);
910 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
912 if (DebugSetProcessKillOnExit
== NULL
913 || DebugActiveProcessStop
== NULL
)
917 struct thread_resume resume
;
918 resume
.thread
= minus_one_ptid
;
919 resume
.kind
= resume_continue
;
921 this->resume (&resume
, 1);
924 if (!DebugActiveProcessStop (current_process_id
))
927 DebugSetProcessKillOnExit (FALSE
);
928 remove_process (process
);
930 win32_clear_inferiors ();
935 win32_process_target::mourn (struct process_info
*process
)
937 remove_process (process
);
940 /* Implementation of target_ops::join. */
943 win32_process_target::join (int pid
)
945 HANDLE h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
948 WaitForSingleObject (h
, INFINITE
);
953 /* Return true iff the thread with thread ID TID is alive. */
955 win32_process_target::thread_alive (ptid_t ptid
)
957 /* Our thread list is reliable; don't bother to poll target
959 return find_thread_ptid (ptid
) != NULL
;
962 /* Resume the inferior process. RESUME_INFO describes how we want
965 win32_process_target::resume (thread_resume
*resume_info
, size_t n
)
970 windows_thread_info
*th
;
971 DWORD continue_status
= DBG_CONTINUE
;
974 /* This handles the very limited set of resume packets that GDB can
975 currently produce. */
977 if (n
== 1 && resume_info
[0].thread
== minus_one_ptid
)
982 /* Yes, we're ignoring resume_info[0].thread. It'd be tricky to make
983 the Windows resume code do the right thing for thread switching. */
984 tid
= current_event
.dwThreadId
;
986 if (resume_info
[0].thread
!= minus_one_ptid
)
988 sig
= gdb_signal_from_host (resume_info
[0].sig
);
989 step
= resume_info
[0].kind
== resume_step
;
997 if (sig
!= GDB_SIGNAL_0
)
999 if (current_event
.dwDebugEventCode
!= EXCEPTION_DEBUG_EVENT
)
1001 OUTMSG (("Cannot continue with signal %s here.\n",
1002 gdb_signal_to_string (sig
)));
1004 else if (sig
== last_sig
)
1005 continue_status
= DBG_EXCEPTION_NOT_HANDLED
;
1007 OUTMSG (("Can only continue with received signal %s.\n",
1008 gdb_signal_to_string (last_sig
)));
1011 last_sig
= GDB_SIGNAL_0
;
1013 /* Get context for the currently selected thread. */
1014 ptid
= debug_event_ptid (¤t_event
);
1015 th
= thread_rec (ptid
, DONT_INVALIDATE_CONTEXT
);
1018 win32_prepare_to_resume (th
);
1020 DWORD
*context_flags
;
1023 context_flags
= &th
->wow64_context
.ContextFlags
;
1026 context_flags
= &th
->context
.ContextFlags
;
1029 /* Move register values from the inferior into the thread
1030 context structure. */
1031 regcache_invalidate ();
1035 if (the_low_target
.single_step
!= NULL
)
1036 (*the_low_target
.single_step
) (th
);
1038 error ("Single stepping is not supported "
1039 "in this configuration.\n");
1042 win32_set_thread_context (th
);
1047 /* Allow continuing with the same signal that interrupted us.
1048 Otherwise complain. */
1050 child_continue (continue_status
, tid
);
1054 win32_add_one_solib (const char *name
, CORE_ADDR load_addr
)
1056 char buf
[MAX_PATH
+ 1];
1057 char buf2
[MAX_PATH
+ 1];
1060 WIN32_FIND_DATA w32_fd
;
1061 WCHAR wname
[MAX_PATH
+ 1];
1062 mbstowcs (wname
, name
, MAX_PATH
);
1063 HANDLE h
= FindFirstFile (wname
, &w32_fd
);
1065 WIN32_FIND_DATAA w32_fd
;
1066 HANDLE h
= FindFirstFileA (name
, &w32_fd
);
1069 /* The symbols in a dll are offset by 0x1000, which is the
1070 offset from 0 of the first byte in an image - because
1071 of the file header and the section alignment. */
1072 load_addr
+= 0x1000;
1074 if (h
== INVALID_HANDLE_VALUE
)
1082 char cwd
[MAX_PATH
+ 1];
1084 if (GetCurrentDirectoryA (MAX_PATH
+ 1, cwd
))
1086 p
= strrchr (buf
, '\\');
1089 SetCurrentDirectoryA (buf
);
1090 GetFullPathNameA (w32_fd
.cFileName
, MAX_PATH
, buf
, &p
);
1091 SetCurrentDirectoryA (cwd
);
1098 if (strcasecmp (buf
, "ntdll.dll") == 0)
1100 GetSystemDirectoryA (buf
, sizeof (buf
));
1101 strcat (buf
, "\\ntdll.dll");
1106 cygwin_conv_path (CCP_WIN_A_TO_POSIX
, buf
, buf2
, sizeof (buf2
));
1111 loaded_dll (buf2
, load_addr
);
1114 typedef BOOL (WINAPI
*winapi_EnumProcessModules
) (HANDLE
, HMODULE
*,
1117 typedef BOOL (WINAPI
*winapi_EnumProcessModulesEx
) (HANDLE
, HMODULE
*, DWORD
,
1120 typedef BOOL (WINAPI
*winapi_GetModuleInformation
) (HANDLE
, HMODULE
,
1121 LPMODULEINFO
, DWORD
);
1122 typedef DWORD (WINAPI
*winapi_GetModuleFileNameExA
) (HANDLE
, HMODULE
,
1125 static winapi_EnumProcessModules win32_EnumProcessModules
;
1127 static winapi_EnumProcessModulesEx win32_EnumProcessModulesEx
;
1129 static winapi_GetModuleInformation win32_GetModuleInformation
;
1130 static winapi_GetModuleFileNameExA win32_GetModuleFileNameExA
;
1135 static int psapi_loaded
= 0;
1136 static HMODULE dll
= NULL
;
1141 dll
= LoadLibrary (TEXT("psapi.dll"));
1144 win32_EnumProcessModules
=
1145 GETPROCADDRESS (dll
, EnumProcessModules
);
1147 win32_EnumProcessModulesEx
=
1148 GETPROCADDRESS (dll
, EnumProcessModulesEx
);
1150 win32_GetModuleInformation
=
1151 GETPROCADDRESS (dll
, GetModuleInformation
);
1152 win32_GetModuleFileNameExA
=
1153 GETPROCADDRESS (dll
, GetModuleFileNameExA
);
1157 if (wow64_process
&& win32_EnumProcessModulesEx
== nullptr)
1161 return (win32_EnumProcessModules
!= NULL
1162 && win32_GetModuleInformation
!= NULL
1163 && win32_GetModuleFileNameExA
!= NULL
);
1168 /* Iterate over all DLLs currently mapped by our inferior, looking for
1169 a DLL loaded at LOAD_ADDR; if found, return its file name,
1170 otherwise return NULL. If LOAD_ADDR is NULL, add all mapped DLLs
1171 to our list of solibs. */
1174 win32_add_dll (LPVOID load_addr
)
1178 HMODULE
*DllHandle
= dh_buf
;
1188 ok
= (*win32_EnumProcessModulesEx
) (current_process_handle
,
1192 LIST_MODULES_32BIT
);
1195 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1200 if (!ok
|| !cbNeeded
)
1203 DllHandle
= (HMODULE
*) alloca (cbNeeded
);
1209 ok
= (*win32_EnumProcessModulesEx
) (current_process_handle
,
1213 LIST_MODULES_32BIT
);
1216 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1223 char system_dir
[MAX_PATH
];
1224 char syswow_dir
[MAX_PATH
];
1225 size_t system_dir_len
= 0;
1226 bool convert_syswow_dir
= false;
1231 /* This fails on 32bit Windows because it has no SysWOW64 directory,
1232 and in this case a path conversion isn't necessary. */
1233 UINT len
= GetSystemWow64DirectoryA (syswow_dir
, sizeof (syswow_dir
));
1236 /* Check that we have passed a large enough buffer. */
1237 gdb_assert (len
< sizeof (syswow_dir
));
1239 len
= GetSystemDirectoryA (system_dir
, sizeof (system_dir
));
1241 gdb_assert (len
!= 0);
1242 /* Check that we have passed a large enough buffer. */
1243 gdb_assert (len
< sizeof (system_dir
));
1245 strcat (system_dir
, "\\");
1246 strcat (syswow_dir
, "\\");
1247 system_dir_len
= strlen (system_dir
);
1249 convert_syswow_dir
= true;
1254 for (i
= 1; i
< ((size_t) cbNeeded
/ sizeof (HMODULE
)); i
++)
1257 static char dll_name
[MAX_PATH
];
1259 if (!(*win32_GetModuleInformation
) (current_process_handle
,
1264 if ((*win32_GetModuleFileNameExA
) (current_process_handle
,
1270 if (load_addr
!= nullptr && mi
.lpBaseOfDll
!= load_addr
)
1273 const char *name
= dll_name
;
1274 /* Convert the DLL path of 32bit processes returned by
1275 GetModuleFileNameEx from the 64bit system directory to the
1276 32bit syswow64 directory if necessary. */
1277 std::string syswow_dll_path
;
1278 if (convert_syswow_dir
1279 && strncasecmp (dll_name
, system_dir
, system_dir_len
) == 0
1280 && strchr (dll_name
+ system_dir_len
, '\\') == nullptr)
1282 syswow_dll_path
= syswow_dir
;
1283 syswow_dll_path
+= dll_name
+ system_dir_len
;
1284 name
= syswow_dll_path
.c_str();
1287 if (load_addr
!= nullptr)
1289 if (name
!= dll_name
)
1290 strcpy (dll_name
, name
);
1294 win32_add_one_solib (name
, (CORE_ADDR
) (uintptr_t) mi
.lpBaseOfDll
);
1299 /* Iterate over all DLLs currently mapped by our inferior, and
1300 add them to our list of solibs. */
1303 win32_add_all_dlls (void)
1305 win32_add_dll (NULL
);
1307 #endif /* !_WIN32_WCE */
1309 typedef HANDLE (WINAPI
*winapi_CreateToolhelp32Snapshot
) (DWORD
, DWORD
);
1310 typedef BOOL (WINAPI
*winapi_Module32First
) (HANDLE
, LPMODULEENTRY32
);
1311 typedef BOOL (WINAPI
*winapi_Module32Next
) (HANDLE
, LPMODULEENTRY32
);
1313 /* See nat/windows-nat.h. */
1316 windows_nat::handle_load_dll ()
1318 LOAD_DLL_DEBUG_INFO
*event
= ¤t_event
.u
.LoadDll
;
1319 const char *dll_name
;
1321 dll_name
= get_image_name (current_process_handle
,
1322 event
->lpImageName
, event
->fUnicode
);
1324 if (dll_name
== nullptr
1325 && event
->lpBaseOfDll
!= nullptr)
1326 dll_name
= win32_add_dll (event
->lpBaseOfDll
);
1328 if (dll_name
== nullptr)
1331 win32_add_one_solib (dll_name
, (CORE_ADDR
) (uintptr_t) event
->lpBaseOfDll
);
1334 /* See nat/windows-nat.h. */
1337 windows_nat::handle_unload_dll ()
1339 CORE_ADDR load_addr
=
1340 (CORE_ADDR
) (uintptr_t) current_event
.u
.UnloadDll
.lpBaseOfDll
;
1342 /* The symbols in a dll are offset by 0x1000, which is the
1343 offset from 0 of the first byte in an image - because
1344 of the file header and the section alignment. */
1345 load_addr
+= 0x1000;
1346 unloaded_dll (NULL
, load_addr
);
1350 suspend_one_thread (thread_info
*thread
)
1352 windows_thread_info
*th
= (windows_thread_info
*) thread_target_data (thread
);
1358 fake_breakpoint_event (void)
1360 OUTMSG2(("fake_breakpoint_event\n"));
1362 faked_breakpoint
= 1;
1364 memset (¤t_event
, 0, sizeof (current_event
));
1365 current_event
.dwThreadId
= main_thread_id
;
1366 current_event
.dwDebugEventCode
= EXCEPTION_DEBUG_EVENT
;
1367 current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
1368 = EXCEPTION_BREAKPOINT
;
1370 for_each_thread (suspend_one_thread
);
1375 auto_delete_breakpoint (CORE_ADDR stop_pc
)
1381 /* See nat/windows-nat.h. */
1384 windows_nat::handle_ms_vc_exception (const EXCEPTION_RECORD
*rec
)
1389 /* See nat/windows-nat.h. */
1392 windows_nat::handle_access_violation (const EXCEPTION_RECORD
*rec
)
1397 /* A helper function that will, if needed, set
1398 'stopped_at_software_breakpoint' on the thread and adjust the
1404 struct regcache
*regcache
= get_thread_regcache (current_thread
, 1);
1405 child_fetch_inferior_registers (regcache
, -1);
1407 windows_thread_info
*th
= thread_rec (current_thread_ptid (),
1408 DONT_INVALIDATE_CONTEXT
);
1409 th
->stopped_at_software_breakpoint
= false;
1411 if (current_event
.dwDebugEventCode
== EXCEPTION_DEBUG_EVENT
1412 && ((current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
1413 == EXCEPTION_BREAKPOINT
)
1414 || (current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
1415 == STATUS_WX86_BREAKPOINT
))
1416 && child_initialization_done
)
1418 th
->stopped_at_software_breakpoint
= true;
1419 CORE_ADDR pc
= regcache_read_pc (regcache
);
1420 CORE_ADDR sw_breakpoint_pc
= pc
- the_low_target
.decr_pc_after_break
;
1421 regcache_write_pc (regcache
, sw_breakpoint_pc
);
1425 /* Get the next event from the child. */
1428 get_child_debug_event (DWORD
*continue_status
,
1429 struct target_waitstatus
*ourstatus
)
1433 last_sig
= GDB_SIGNAL_0
;
1434 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1435 *continue_status
= DBG_CONTINUE
;
1437 /* Check if GDB sent us an interrupt request. */
1438 check_remote_input_interrupt_request ();
1440 if (soft_interrupt_requested
)
1442 soft_interrupt_requested
= 0;
1443 fake_breakpoint_event ();
1449 gdb::optional
<pending_stop
> stop
= fetch_pending_stop (debug_threads
);
1450 if (stop
.has_value ())
1452 *ourstatus
= stop
->status
;
1453 current_event
= stop
->event
;
1454 ptid
= debug_event_ptid (¤t_event
);
1455 current_thread
= find_thread_ptid (ptid
);
1459 /* Keep the wait time low enough for comfortable remote
1460 interruption, but high enough so gdbserver doesn't become a
1462 if (!wait_for_debug_event (¤t_event
, 250))
1464 DWORD e
= GetLastError();
1466 if (e
== ERROR_PIPE_NOT_CONNECTED
)
1468 /* This will happen if the loader fails to succesfully
1469 load the application, e.g., if the main executable
1470 tries to pull in a non-existing export from a
1472 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1473 ourstatus
->value
.integer
= 1;
1483 switch (current_event
.dwDebugEventCode
)
1485 case CREATE_THREAD_DEBUG_EVENT
:
1486 OUTMSG2 (("gdbserver: kernel event CREATE_THREAD_DEBUG_EVENT "
1487 "for pid=%u tid=%x)\n",
1488 (unsigned) current_event
.dwProcessId
,
1489 (unsigned) current_event
.dwThreadId
));
1491 /* Record the existence of this thread. */
1492 child_add_thread (current_event
.dwProcessId
,
1493 current_event
.dwThreadId
,
1494 current_event
.u
.CreateThread
.hThread
,
1495 current_event
.u
.CreateThread
.lpThreadLocalBase
);
1498 case EXIT_THREAD_DEBUG_EVENT
:
1499 OUTMSG2 (("gdbserver: kernel event EXIT_THREAD_DEBUG_EVENT "
1500 "for pid=%u tid=%x\n",
1501 (unsigned) current_event
.dwProcessId
,
1502 (unsigned) current_event
.dwThreadId
));
1503 child_delete_thread (current_event
.dwProcessId
,
1504 current_event
.dwThreadId
);
1506 current_thread
= get_first_thread ();
1509 case CREATE_PROCESS_DEBUG_EVENT
:
1510 OUTMSG2 (("gdbserver: kernel event CREATE_PROCESS_DEBUG_EVENT "
1511 "for pid=%u tid=%x\n",
1512 (unsigned) current_event
.dwProcessId
,
1513 (unsigned) current_event
.dwThreadId
));
1514 CloseHandle (current_event
.u
.CreateProcessInfo
.hFile
);
1516 if (open_process_used
)
1518 CloseHandle (current_process_handle
);
1519 open_process_used
= false;
1522 current_process_handle
= current_event
.u
.CreateProcessInfo
.hProcess
;
1523 main_thread_id
= current_event
.dwThreadId
;
1525 /* Add the main thread. */
1526 child_add_thread (current_event
.dwProcessId
,
1528 current_event
.u
.CreateProcessInfo
.hThread
,
1529 current_event
.u
.CreateProcessInfo
.lpThreadLocalBase
);
1532 case EXIT_PROCESS_DEBUG_EVENT
:
1533 OUTMSG2 (("gdbserver: kernel event EXIT_PROCESS_DEBUG_EVENT "
1534 "for pid=%u tid=%x\n",
1535 (unsigned) current_event
.dwProcessId
,
1536 (unsigned) current_event
.dwThreadId
));
1538 DWORD exit_status
= current_event
.u
.ExitProcess
.dwExitCode
;
1539 /* If the exit status looks like a fatal exception, but we
1540 don't recognize the exception's code, make the original
1541 exit status value available, to avoid losing information. */
1543 = WIFSIGNALED (exit_status
) ? WTERMSIG (exit_status
) : -1;
1544 if (exit_signal
== -1)
1546 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1547 ourstatus
->value
.integer
= exit_status
;
1551 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
1552 ourstatus
->value
.sig
= gdb_signal_from_host (exit_signal
);
1555 child_continue (DBG_CONTINUE
, desired_stop_thread_id
);
1558 case LOAD_DLL_DEBUG_EVENT
:
1559 OUTMSG2 (("gdbserver: kernel event LOAD_DLL_DEBUG_EVENT "
1560 "for pid=%u tid=%x\n",
1561 (unsigned) current_event
.dwProcessId
,
1562 (unsigned) current_event
.dwThreadId
));
1563 CloseHandle (current_event
.u
.LoadDll
.hFile
);
1564 if (! child_initialization_done
)
1568 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1569 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1572 case UNLOAD_DLL_DEBUG_EVENT
:
1573 OUTMSG2 (("gdbserver: kernel event UNLOAD_DLL_DEBUG_EVENT "
1574 "for pid=%u tid=%x\n",
1575 (unsigned) current_event
.dwProcessId
,
1576 (unsigned) current_event
.dwThreadId
));
1577 if (! child_initialization_done
)
1579 handle_unload_dll ();
1580 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1581 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1584 case EXCEPTION_DEBUG_EVENT
:
1585 OUTMSG2 (("gdbserver: kernel event EXCEPTION_DEBUG_EVENT "
1586 "for pid=%u tid=%x\n",
1587 (unsigned) current_event
.dwProcessId
,
1588 (unsigned) current_event
.dwThreadId
));
1589 if (handle_exception (ourstatus
, debug_threads
)
1590 == HANDLE_EXCEPTION_UNHANDLED
)
1591 *continue_status
= DBG_EXCEPTION_NOT_HANDLED
;
1594 case OUTPUT_DEBUG_STRING_EVENT
:
1595 /* A message from the kernel (or Cygwin). */
1596 OUTMSG2 (("gdbserver: kernel event OUTPUT_DEBUG_STRING_EVENT "
1597 "for pid=%u tid=%x\n",
1598 (unsigned) current_event
.dwProcessId
,
1599 (unsigned) current_event
.dwThreadId
));
1600 handle_output_debug_string (nullptr);
1604 OUTMSG2 (("gdbserver: kernel event unknown "
1605 "for pid=%u tid=%x code=%x\n",
1606 (unsigned) current_event
.dwProcessId
,
1607 (unsigned) current_event
.dwThreadId
,
1608 (unsigned) current_event
.dwDebugEventCode
));
1612 ptid
= debug_event_ptid (¤t_event
);
1614 if (desired_stop_thread_id
!= -1 && desired_stop_thread_id
!= ptid
.lwp ())
1616 /* Pending stop. See the comment by the definition of
1617 "pending_stops" for details on why this is needed. */
1618 OUTMSG2 (("get_windows_debug_event - "
1619 "unexpected stop in 0x%lx (expecting 0x%x)\n",
1620 ptid
.lwp (), desired_stop_thread_id
));
1622 pending_stops
.push_back ({(DWORD
) ptid
.lwp (), *ourstatus
, current_event
});
1623 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1626 current_thread
= find_thread_ptid (ptid
);
1631 /* Wait for the inferior process to change state.
1632 STATUS will be filled in with a response code to send to GDB.
1633 Returns the signal which caused the process to stop. */
1635 win32_process_target::wait (ptid_t ptid
, target_waitstatus
*ourstatus
,
1636 target_wait_flags options
)
1638 if (cached_status
.kind
!= TARGET_WAITKIND_IGNORE
)
1640 /* The core always does a wait after creating the inferior, and
1641 do_initial_child_stuff already ran the inferior to the
1642 initial breakpoint (or an exit, if creating the process
1643 fails). Report it now. */
1644 *ourstatus
= cached_status
;
1645 cached_status
.kind
= TARGET_WAITKIND_IGNORE
;
1646 return debug_event_ptid (¤t_event
);
1651 DWORD continue_status
;
1652 if (!get_child_debug_event (&continue_status
, ourstatus
))
1655 switch (ourstatus
->kind
)
1657 case TARGET_WAITKIND_EXITED
:
1658 OUTMSG2 (("Child exited with retcode = %x\n",
1659 ourstatus
->value
.integer
));
1660 win32_clear_inferiors ();
1661 return ptid_t (current_event
.dwProcessId
);
1662 case TARGET_WAITKIND_STOPPED
:
1663 case TARGET_WAITKIND_SIGNALLED
:
1664 case TARGET_WAITKIND_LOADED
:
1666 OUTMSG2 (("Child Stopped with signal = %d \n",
1667 ourstatus
->value
.sig
));
1669 return debug_event_ptid (¤t_event
);
1672 OUTMSG (("Ignoring unknown internal event, %d\n", ourstatus
->kind
));
1674 case TARGET_WAITKIND_SPURIOUS
:
1675 /* do nothing, just continue */
1676 child_continue (continue_status
, desired_stop_thread_id
);
1682 /* Fetch registers from the inferior process.
1683 If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO. */
1685 win32_process_target::fetch_registers (regcache
*regcache
, int regno
)
1687 child_fetch_inferior_registers (regcache
, regno
);
1690 /* Store registers to the inferior process.
1691 If REGNO is -1, store all registers; otherwise, store at least REGNO. */
1693 win32_process_target::store_registers (regcache
*regcache
, int regno
)
1695 child_store_inferior_registers (regcache
, regno
);
1698 /* Read memory from the inferior process. This should generally be
1699 called through read_inferior_memory, which handles breakpoint shadowing.
1700 Read LEN bytes at MEMADDR into a buffer at MYADDR. */
1702 win32_process_target::read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
,
1705 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 0, 0) != len
;
1708 /* Write memory to the inferior process. This should generally be
1709 called through write_inferior_memory, which handles breakpoint shadowing.
1710 Write LEN bytes from the buffer at MYADDR to MEMADDR.
1711 Returns 0 on success and errno on failure. */
1713 win32_process_target::write_memory (CORE_ADDR memaddr
,
1714 const unsigned char *myaddr
, int len
)
1716 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 1, 0) != len
;
1719 /* Send an interrupt request to the inferior process. */
1721 win32_process_target::request_interrupt ()
1723 winapi_DebugBreakProcess DebugBreakProcess
;
1724 winapi_GenerateConsoleCtrlEvent GenerateConsoleCtrlEvent
;
1727 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
1729 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
1732 GenerateConsoleCtrlEvent
= GETPROCADDRESS (dll
, GenerateConsoleCtrlEvent
);
1734 if (GenerateConsoleCtrlEvent
!= NULL
1735 && GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT
, current_process_id
))
1738 /* GenerateConsoleCtrlEvent can fail if process id being debugged is
1739 not a process group id.
1740 Fallback to XP/Vista 'DebugBreakProcess', which generates a
1741 breakpoint exception in the interior process. */
1743 DebugBreakProcess
= GETPROCADDRESS (dll
, DebugBreakProcess
);
1745 if (DebugBreakProcess
!= NULL
1746 && DebugBreakProcess (current_process_handle
))
1749 /* Last resort, suspend all threads manually. */
1750 soft_interrupt_requested
= 1;
1754 win32_process_target::supports_hardware_single_step ()
1761 win32_error_to_fileio_error (DWORD err
)
1765 case ERROR_BAD_PATHNAME
:
1766 case ERROR_FILE_NOT_FOUND
:
1767 case ERROR_INVALID_NAME
:
1768 case ERROR_PATH_NOT_FOUND
:
1769 return FILEIO_ENOENT
;
1771 case ERROR_IO_DEVICE
:
1772 case ERROR_OPEN_FAILED
:
1774 case ERROR_INVALID_HANDLE
:
1775 return FILEIO_EBADF
;
1776 case ERROR_ACCESS_DENIED
:
1777 case ERROR_SHARING_VIOLATION
:
1778 return FILEIO_EACCES
;
1779 case ERROR_NOACCESS
:
1780 return FILEIO_EFAULT
;
1782 return FILEIO_EBUSY
;
1783 case ERROR_ALREADY_EXISTS
:
1784 case ERROR_FILE_EXISTS
:
1785 return FILEIO_EEXIST
;
1786 case ERROR_BAD_DEVICE
:
1787 return FILEIO_ENODEV
;
1788 case ERROR_DIRECTORY
:
1789 return FILEIO_ENOTDIR
;
1790 case ERROR_FILENAME_EXCED_RANGE
:
1791 case ERROR_INVALID_DATA
:
1792 case ERROR_INVALID_PARAMETER
:
1793 case ERROR_NEGATIVE_SEEK
:
1794 return FILEIO_EINVAL
;
1795 case ERROR_TOO_MANY_OPEN_FILES
:
1796 return FILEIO_EMFILE
;
1797 case ERROR_HANDLE_DISK_FULL
:
1798 case ERROR_DISK_FULL
:
1799 return FILEIO_ENOSPC
;
1800 case ERROR_WRITE_PROTECT
:
1801 return FILEIO_EROFS
;
1802 case ERROR_NOT_SUPPORTED
:
1803 return FILEIO_ENOSYS
;
1806 return FILEIO_EUNKNOWN
;
1810 win32_process_target::hostio_last_error (char *buf
)
1812 DWORD winerr
= GetLastError ();
1813 int fileio_err
= win32_error_to_fileio_error (winerr
);
1814 sprintf (buf
, "F-1,%x", fileio_err
);
1819 win32_process_target::supports_qxfer_siginfo ()
1824 /* Write Windows signal info. */
1827 win32_process_target::qxfer_siginfo (const char *annex
,
1828 unsigned char *readbuf
,
1829 unsigned const char *writebuf
,
1830 CORE_ADDR offset
, int len
)
1832 if (siginfo_er
.ExceptionCode
== 0)
1835 if (readbuf
== nullptr)
1838 char *buf
= (char *) &siginfo_er
;
1839 size_t bufsize
= sizeof (siginfo_er
);
1842 EXCEPTION_RECORD32 er32
;
1845 buf
= (char *) &er32
;
1846 bufsize
= sizeof (er32
);
1848 er32
.ExceptionCode
= siginfo_er
.ExceptionCode
;
1849 er32
.ExceptionFlags
= siginfo_er
.ExceptionFlags
;
1850 er32
.ExceptionRecord
= (uintptr_t) siginfo_er
.ExceptionRecord
;
1851 er32
.ExceptionAddress
= (uintptr_t) siginfo_er
.ExceptionAddress
;
1852 er32
.NumberParameters
= siginfo_er
.NumberParameters
;
1854 for (i
= 0; i
< EXCEPTION_MAXIMUM_PARAMETERS
; i
++)
1855 er32
.ExceptionInformation
[i
] = siginfo_er
.ExceptionInformation
[i
];
1859 if (offset
> bufsize
)
1862 if (offset
+ len
> bufsize
)
1863 len
= bufsize
- offset
;
1865 memcpy (readbuf
, buf
+ offset
, len
);
1871 win32_process_target::supports_get_tib_address ()
1876 /* Write Windows OS Thread Information Block address. */
1879 win32_process_target::get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
)
1881 windows_thread_info
*th
;
1882 th
= thread_rec (ptid
, DONT_INVALIDATE_CONTEXT
);
1886 *addr
= th
->thread_local_base
;
1890 /* Implementation of the target_ops method "sw_breakpoint_from_kind". */
1893 win32_process_target::sw_breakpoint_from_kind (int kind
, int *size
)
1895 *size
= the_low_target
.breakpoint_len
;
1896 return the_low_target
.breakpoint
;
1900 win32_process_target::stopped_by_sw_breakpoint ()
1902 windows_thread_info
*th
= thread_rec (current_thread_ptid (),
1903 DONT_INVALIDATE_CONTEXT
);
1904 return th
== nullptr ? false : th
->stopped_at_software_breakpoint
;
1908 win32_process_target::supports_stopped_by_sw_breakpoint ()
1914 win32_process_target::read_pc (struct regcache
*regcache
)
1916 return (*the_low_target
.get_pc
) (regcache
);
1920 win32_process_target::write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
1922 return (*the_low_target
.set_pc
) (regcache
, pc
);
1925 /* The win32 target ops object. */
1927 static win32_process_target the_win32_target
;
1929 /* Initialize the Win32 backend. */
1931 initialize_low (void)
1933 set_target_ops (&the_win32_target
);
1934 the_low_target
.arch_setup ();
1937 /* These functions are loaded dynamically, because they are not available
1939 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
1940 win32_Wow64GetThreadContext
= GETPROCADDRESS (dll
, Wow64GetThreadContext
);
1941 win32_Wow64SetThreadContext
= GETPROCADDRESS (dll
, Wow64SetThreadContext
);