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"
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;
95 bool wow64_process
= false;
98 const struct target_desc
*win32_tdesc
;
100 const struct target_desc
*wow64_win32_tdesc
;
103 #define NUM_REGS (the_low_target.num_regs ())
105 typedef BOOL (WINAPI
*winapi_DebugActiveProcessStop
) (DWORD dwProcessId
);
106 typedef BOOL (WINAPI
*winapi_DebugSetProcessKillOnExit
) (BOOL KillOnExit
);
107 typedef BOOL (WINAPI
*winapi_DebugBreakProcess
) (HANDLE
);
108 typedef BOOL (WINAPI
*winapi_GenerateConsoleCtrlEvent
) (DWORD
, DWORD
);
111 typedef BOOL (WINAPI
*winapi_Wow64SetThreadContext
) (HANDLE
,
112 const WOW64_CONTEXT
*);
114 winapi_Wow64GetThreadContext win32_Wow64GetThreadContext
;
115 static winapi_Wow64SetThreadContext win32_Wow64SetThreadContext
;
119 static void win32_add_all_dlls (void);
122 /* Get the thread ID from the current selected inferior (the current
125 current_thread_ptid (void)
130 /* The current debug event from WaitForDebugEvent. */
132 debug_event_ptid (DEBUG_EVENT
*event
)
134 return ptid_t (event
->dwProcessId
, event
->dwThreadId
, 0);
137 /* Get the thread context of the thread associated with TH. */
140 win32_get_thread_context (windows_thread_info
*th
)
144 memset (&th
->wow64_context
, 0, sizeof (WOW64_CONTEXT
));
147 memset (&th
->context
, 0, sizeof (CONTEXT
));
148 (*the_low_target
.get_thread_context
) (th
);
150 memcpy (&th
->base_context
, &th
->context
, sizeof (CONTEXT
));
154 /* Set the thread context of the thread associated with TH. */
157 win32_set_thread_context (windows_thread_info
*th
)
160 /* Calling SuspendThread on a thread that is running kernel code
161 will report that the suspending was successful, but in fact, that
162 will often not be true. In those cases, the context returned by
163 GetThreadContext will not be correct by the time the thread
164 stops, hence we can't set that context back into the thread when
165 resuming - it will most likely crash the inferior.
166 Unfortunately, there is no way to know when the thread will
167 really stop. To work around it, we'll only write the context
168 back to the thread when either the user or GDB explicitly change
169 it between stopping and resuming. */
170 if (memcmp (&th
->context
, &th
->base_context
, sizeof (CONTEXT
)) != 0)
175 win32_Wow64SetThreadContext (th
->h
, &th
->wow64_context
);
178 SetThreadContext (th
->h
, &th
->context
);
182 /* Set the thread context of the thread associated with TH. */
185 win32_prepare_to_resume (windows_thread_info
*th
)
187 if (the_low_target
.prepare_to_resume
!= NULL
)
188 (*the_low_target
.prepare_to_resume
) (th
);
191 /* See win32-low.h. */
194 win32_require_context (windows_thread_info
*th
)
199 context_flags
= th
->wow64_context
.ContextFlags
;
202 context_flags
= th
->context
.ContextFlags
;
203 if (context_flags
== 0)
206 win32_get_thread_context (th
);
210 /* See nat/windows-nat.h. */
212 windows_thread_info
*
213 windows_nat::thread_rec (ptid_t ptid
, thread_disposition_type disposition
)
215 thread_info
*thread
= find_thread_ptid (ptid
);
219 windows_thread_info
*th
= (windows_thread_info
*) thread_target_data (thread
);
220 if (disposition
!= DONT_INVALIDATE_CONTEXT
)
221 win32_require_context (th
);
225 /* Add a thread to the thread list. */
226 static windows_thread_info
*
227 child_add_thread (DWORD pid
, DWORD tid
, HANDLE h
, void *tlb
)
229 windows_thread_info
*th
;
230 ptid_t ptid
= ptid_t (pid
, tid
, 0);
232 if ((th
= thread_rec (ptid
, DONT_INVALIDATE_CONTEXT
)))
235 CORE_ADDR base
= (CORE_ADDR
) (uintptr_t) tlb
;
237 /* For WOW64 processes, this is actually the pointer to the 64bit TIB,
238 and the 32bit TIB is exactly 2 pages after it. */
240 base
+= 2 * 4096; /* page size = 4096 */
242 th
= new windows_thread_info (tid
, h
, base
);
244 add_thread (ptid
, th
);
246 if (the_low_target
.thread_added
!= NULL
)
247 (*the_low_target
.thread_added
) (th
);
252 /* Delete a thread from the list of threads. */
254 delete_thread_info (thread_info
*thread
)
256 windows_thread_info
*th
= (windows_thread_info
*) thread_target_data (thread
);
258 remove_thread (thread
);
262 /* Delete a thread from the list of threads. */
264 child_delete_thread (DWORD pid
, DWORD tid
)
266 /* If the last thread is exiting, just return. */
267 if (all_threads
.size () == 1)
270 thread_info
*thread
= find_thread_ptid (ptid_t (pid
, tid
));
274 delete_thread_info (thread
);
277 /* These watchpoint related wrapper functions simply pass on the function call
278 if the low target has registered a corresponding function. */
281 win32_process_target::supports_z_point_type (char z_type
)
283 return (z_type
== Z_PACKET_SW_BP
284 || (the_low_target
.supports_z_point_type
!= NULL
285 && the_low_target
.supports_z_point_type (z_type
)));
289 win32_process_target::insert_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
290 int size
, raw_breakpoint
*bp
)
292 if (type
== raw_bkpt_type_sw
)
293 return insert_memory_breakpoint (bp
);
294 else if (the_low_target
.insert_point
!= NULL
)
295 return the_low_target
.insert_point (type
, addr
, size
, bp
);
297 /* Unsupported (see target.h). */
302 win32_process_target::remove_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
303 int size
, raw_breakpoint
*bp
)
305 if (type
== raw_bkpt_type_sw
)
306 return remove_memory_breakpoint (bp
);
307 else if (the_low_target
.remove_point
!= NULL
)
308 return the_low_target
.remove_point (type
, addr
, size
, bp
);
310 /* Unsupported (see target.h). */
315 win32_process_target::stopped_by_watchpoint ()
317 if (the_low_target
.stopped_by_watchpoint
!= NULL
)
318 return the_low_target
.stopped_by_watchpoint ();
324 win32_process_target::stopped_data_address ()
326 if (the_low_target
.stopped_data_address
!= NULL
)
327 return the_low_target
.stopped_data_address ();
333 /* Transfer memory from/to the debugged process. */
335 child_xfer_memory (CORE_ADDR memaddr
, char *our
, int len
,
336 int write
, process_stratum_target
*target
)
341 uintptr_t addr
= (uintptr_t) memaddr
;
345 success
= WriteProcessMemory (current_process_handle
, (LPVOID
) addr
,
346 (LPCVOID
) our
, len
, &done
);
348 lasterror
= GetLastError ();
349 FlushInstructionCache (current_process_handle
, (LPCVOID
) addr
, len
);
353 success
= ReadProcessMemory (current_process_handle
, (LPCVOID
) addr
,
354 (LPVOID
) our
, len
, &done
);
356 lasterror
= GetLastError ();
358 if (!success
&& lasterror
== ERROR_PARTIAL_COPY
&& done
> 0)
361 return success
? done
: -1;
364 /* Clear out any old thread list and reinitialize it to a pristine
367 child_init_thread_list (void)
369 for_each_thread (delete_thread_info
);
372 /* Zero during the child initialization phase, and nonzero otherwise. */
374 static int child_initialization_done
= 0;
377 do_initial_child_stuff (HANDLE proch
, DWORD pid
, int attached
)
379 struct process_info
*proc
;
381 last_sig
= GDB_SIGNAL_0
;
383 current_process_handle
= proch
;
384 current_process_id
= pid
;
387 soft_interrupt_requested
= 0;
388 faked_breakpoint
= 0;
389 open_process_used
= true;
391 memset (¤t_event
, 0, sizeof (current_event
));
395 if (!IsWow64Process (proch
, &wow64
))
397 DWORD err
= GetLastError ();
398 error ("Check if WOW64 process failed (error %d): %s\n",
399 (int) err
, strwinerror (err
));
401 wow64_process
= wow64
;
404 && (win32_Wow64GetThreadContext
== nullptr
405 || win32_Wow64SetThreadContext
== nullptr))
406 error ("WOW64 debugging is not supported on this system.\n");
408 ignore_first_breakpoint
= !attached
&& wow64_process
;
411 proc
= add_process (pid
, attached
);
414 proc
->tdesc
= wow64_win32_tdesc
;
417 proc
->tdesc
= win32_tdesc
;
418 child_init_thread_list ();
419 child_initialization_done
= 0;
421 if (the_low_target
.initial_stuff
!= NULL
)
422 (*the_low_target
.initial_stuff
) ();
424 cached_status
.kind
= TARGET_WAITKIND_IGNORE
;
426 /* Flush all currently pending debug events (thread and dll list) up
427 to the initial breakpoint. */
430 struct target_waitstatus status
;
432 the_target
->wait (minus_one_ptid
, &status
, 0);
434 /* Note win32_wait doesn't return thread events. */
435 if (status
.kind
!= TARGET_WAITKIND_LOADED
)
437 cached_status
= status
;
442 struct thread_resume resume
;
444 resume
.thread
= minus_one_ptid
;
445 resume
.kind
= resume_continue
;
448 the_target
->resume (&resume
, 1);
453 /* Now that the inferior has been started and all DLLs have been mapped,
454 we can iterate over all DLLs and load them in.
456 We avoid doing it any earlier because, on certain versions of Windows,
457 LOAD_DLL_DEBUG_EVENTs are sometimes not complete. In particular,
458 we have seen on Windows 8.1 that the ntdll.dll load event does not
459 include the DLL name, preventing us from creating an associated SO.
460 A possible explanation is that ntdll.dll might be mapped before
461 the SO info gets created by the Windows system -- ntdll.dll is
462 the first DLL to be reported via LOAD_DLL_DEBUG_EVENT and other DLLs
463 do not seem to suffer from that problem.
465 Rather than try to work around this sort of issue, it is much
466 simpler to just ignore DLL load/unload events during the startup
467 phase, and then process them all in one batch now. */
468 win32_add_all_dlls ();
471 child_initialization_done
= 1;
474 /* Resume all artificially suspended threads if we are continuing
477 continue_one_thread (thread_info
*thread
, int thread_id
)
479 windows_thread_info
*th
= (windows_thread_info
*) thread_target_data (thread
);
481 if (thread_id
== -1 || thread_id
== th
->tid
)
483 win32_prepare_to_resume (th
);
487 DWORD
*context_flags
;
490 context_flags
= &th
->wow64_context
.ContextFlags
;
493 context_flags
= &th
->context
.ContextFlags
;
496 win32_set_thread_context (th
);
506 child_continue (DWORD continue_status
, int thread_id
)
508 desired_stop_thread_id
= thread_id
;
509 if (matching_pending_stop (debug_threads
))
512 /* The inferior will only continue after the ContinueDebugEvent
514 for_each_thread ([&] (thread_info
*thread
)
516 continue_one_thread (thread
, thread_id
);
518 faked_breakpoint
= 0;
520 return continue_last_debug_event (continue_status
, debug_threads
);
523 /* Fetch register(s) from the current thread context. */
525 child_fetch_inferior_registers (struct regcache
*regcache
, int r
)
528 windows_thread_info
*th
= thread_rec (current_thread_ptid (),
530 if (r
== -1 || r
> NUM_REGS
)
531 child_fetch_inferior_registers (regcache
, NUM_REGS
);
533 for (regno
= 0; regno
< r
; regno
++)
534 (*the_low_target
.fetch_inferior_register
) (regcache
, th
, regno
);
537 /* Store a new register value into the current thread context. We don't
538 change the program's context until later, when we resume it. */
540 child_store_inferior_registers (struct regcache
*regcache
, int r
)
543 windows_thread_info
*th
= thread_rec (current_thread_ptid (),
545 if (r
== -1 || r
== 0 || r
> NUM_REGS
)
546 child_store_inferior_registers (regcache
, NUM_REGS
);
548 for (regno
= 0; regno
< r
; regno
++)
549 (*the_low_target
.store_inferior_register
) (regcache
, th
, regno
);
552 /* Map the Windows error number in ERROR to a locale-dependent error
553 message string and return a pointer to it. Typically, the values
554 for ERROR come from GetLastError.
556 The string pointed to shall not be modified by the application,
557 but may be overwritten by a subsequent call to strwinerror
559 The strwinerror function does not change the current setting
563 strwinerror (DWORD error
)
565 static char buf
[1024];
567 DWORD lasterr
= GetLastError ();
568 DWORD chars
= FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
569 | FORMAT_MESSAGE_ALLOCATE_BUFFER
,
572 0, /* Default language */
578 /* If there is an \r\n appended, zap it. */
580 && msgbuf
[chars
- 2] == '\r'
581 && msgbuf
[chars
- 1] == '\n')
587 if (chars
> ((COUNTOF (buf
)) - 1))
589 chars
= COUNTOF (buf
) - 1;
594 wcstombs (buf
, msgbuf
, chars
+ 1);
596 strncpy (buf
, msgbuf
, chars
+ 1);
601 sprintf (buf
, "unknown win32 error (%u)", (unsigned) error
);
603 SetLastError (lasterr
);
608 create_process (const char *program
, char *args
,
609 DWORD flags
, PROCESS_INFORMATION
*pi
)
611 const char *inferior_cwd
= get_inferior_cwd ();
613 size_t argslen
, proglen
;
615 proglen
= strlen (program
) + 1;
616 argslen
= strlen (args
) + proglen
;
619 wchar_t *p
, *wprogram
, *wargs
, *wcwd
= NULL
;
621 wprogram
= (wchar_t *) alloca (proglen
* sizeof (wchar_t));
622 mbstowcs (wprogram
, program
, proglen
);
624 for (p
= wprogram
; *p
; ++p
)
628 wargs
= alloca ((argslen
+ 1) * sizeof (wchar_t));
629 wcscpy (wargs
, wprogram
);
630 wcscat (wargs
, L
" ");
631 mbstowcs (wargs
+ proglen
, args
, argslen
+ 1 - proglen
);
633 if (inferior_cwd
!= NULL
)
635 std::string expanded_infcwd
= gdb_tilde_expand (inferior_cwd
);
636 std::replace (expanded_infcwd
.begin (), expanded_infcwd
.end (),
638 wcwd
= alloca ((expanded_infcwd
.size () + 1) * sizeof (wchar_t));
639 if (mbstowcs (wcwd
, expanded_infcwd
.c_str (),
640 expanded_infcwd
.size () + 1) == NULL
)
643 Could not convert the expanded inferior cwd to wide-char."));
647 ret
= CreateProcessW (wprogram
, /* image name */
648 wargs
, /* command line */
649 NULL
, /* security, not supported */
650 NULL
, /* thread, not supported */
651 FALSE
, /* inherit handles, not supported */
652 flags
, /* start flags */
653 NULL
, /* environment, not supported */
654 wcwd
, /* current directory */
655 NULL
, /* start info, not supported */
658 STARTUPINFOA si
= { sizeof (STARTUPINFOA
) };
659 char *program_and_args
= (char *) alloca (argslen
+ 1);
661 strcpy (program_and_args
, program
);
662 strcat (program_and_args
, " ");
663 strcat (program_and_args
, args
);
664 ret
= CreateProcessA (program
, /* image name */
665 program_and_args
, /* command line */
668 TRUE
, /* inherit handles */
669 flags
, /* start flags */
670 NULL
, /* environment */
671 /* current directory */
672 (inferior_cwd
== NULL
674 : gdb_tilde_expand (inferior_cwd
).c_str()),
675 &si
, /* start info */
682 /* Start a new process.
683 PROGRAM is the program name.
684 PROGRAM_ARGS is the vector containing the inferior's args.
685 Returns the new PID on success, -1 on failure. Registers the new
686 process with the process list. */
688 win32_process_target::create_inferior (const char *program
,
689 const std::vector
<char *> &program_args
)
691 client_state
&cs
= get_client_state ();
693 char real_path
[PATH_MAX
];
694 char *orig_path
, *new_path
, *path_ptr
;
698 PROCESS_INFORMATION pi
;
700 std::string str_program_args
= construct_inferior_arguments (program_args
);
701 char *args
= (char *) str_program_args
.c_str ();
703 /* win32_wait needs to know we're not attaching. */
707 error ("No executable specified, specify executable to debug.\n");
709 flags
= DEBUG_PROCESS
| DEBUG_ONLY_THIS_PROCESS
;
713 path_ptr
= getenv ("PATH");
716 int size
= cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, NULL
, 0);
717 orig_path
= (char *) alloca (strlen (path_ptr
) + 1);
718 new_path
= (char *) alloca (size
);
719 strcpy (orig_path
, path_ptr
);
720 cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, new_path
, size
);
721 setenv ("PATH", new_path
, 1);
723 cygwin_conv_path (CCP_POSIX_TO_WIN_A
, program
, real_path
, PATH_MAX
);
727 OUTMSG2 (("Command line is \"%s %s\"\n", program
, args
));
729 #ifdef CREATE_NEW_PROCESS_GROUP
730 flags
|= CREATE_NEW_PROCESS_GROUP
;
733 ret
= create_process (program
, args
, flags
, &pi
);
734 err
= GetLastError ();
735 if (!ret
&& err
== ERROR_FILE_NOT_FOUND
)
737 char *exename
= (char *) alloca (strlen (program
) + 5);
738 strcat (strcpy (exename
, program
), ".exe");
739 ret
= create_process (exename
, args
, flags
, &pi
);
740 err
= GetLastError ();
745 setenv ("PATH", orig_path
, 1);
750 error ("Error creating process \"%s %s\", (error %d): %s\n",
751 program
, args
, (int) err
, strwinerror (err
));
755 OUTMSG2 (("Process created: %s %s\n", program
, (char *) args
));
759 /* On Windows CE this handle can't be closed. The OS reuses
760 it in the debug events, while the 9x/NT versions of Windows
761 probably use a DuplicateHandle'd one. */
762 CloseHandle (pi
.hThread
);
765 do_initial_child_stuff (pi
.hProcess
, pi
.dwProcessId
, 0);
767 /* Wait till we are at 1st instruction in program, return new pid
768 (assuming success). */
769 cs
.last_ptid
= wait (ptid_t (current_process_id
), &cs
.last_status
, 0);
771 /* Necessary for handle_v_kill. */
772 signal_pid
= current_process_id
;
774 return current_process_id
;
777 /* Attach to a running process.
778 PID is the process ID to attach to, specified by the user
779 or a higher layer. */
781 win32_process_target::attach (unsigned long pid
)
784 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
787 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
789 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
791 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
793 h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
796 if (DebugActiveProcess (pid
))
798 if (DebugSetProcessKillOnExit
!= NULL
)
799 DebugSetProcessKillOnExit (FALSE
);
801 /* win32_wait needs to know we're attaching. */
803 do_initial_child_stuff (h
, pid
, 1);
810 err
= GetLastError ();
811 error ("Attach to process failed (error %d): %s\n",
812 (int) err
, strwinerror (err
));
815 /* See nat/windows-nat.h. */
818 windows_nat::handle_output_debug_string (struct target_waitstatus
*ourstatus
)
820 #define READ_BUFFER_LEN 1024
822 char s
[READ_BUFFER_LEN
+ 1] = { 0 };
823 DWORD nbytes
= current_event
.u
.DebugString
.nDebugStringLength
;
828 if (nbytes
> READ_BUFFER_LEN
)
829 nbytes
= READ_BUFFER_LEN
;
831 addr
= (CORE_ADDR
) (size_t) current_event
.u
.DebugString
.lpDebugStringData
;
833 if (current_event
.u
.DebugString
.fUnicode
)
835 /* The event tells us how many bytes, not chars, even
837 WCHAR buffer
[(READ_BUFFER_LEN
+ 1) / sizeof (WCHAR
)] = { 0 };
838 if (read_inferior_memory (addr
, (unsigned char *) buffer
, nbytes
) != 0)
840 wcstombs (s
, buffer
, (nbytes
+ 1) / sizeof (WCHAR
));
844 if (read_inferior_memory (addr
, (unsigned char *) s
, nbytes
) != 0)
848 if (!startswith (s
, "cYg"))
858 #undef READ_BUFFER_LEN
864 win32_clear_inferiors (void)
866 if (open_process_used
)
868 CloseHandle (current_process_handle
);
869 open_process_used
= false;
872 for_each_thread (delete_thread_info
);
873 siginfo_er
.ExceptionCode
= 0;
877 /* Implementation of target_ops::kill. */
880 win32_process_target::kill (process_info
*process
)
882 TerminateProcess (current_process_handle
, 0);
885 if (!child_continue (DBG_CONTINUE
, -1))
887 if (!wait_for_debug_event (¤t_event
, INFINITE
))
889 if (current_event
.dwDebugEventCode
== EXIT_PROCESS_DEBUG_EVENT
)
891 else if (current_event
.dwDebugEventCode
== OUTPUT_DEBUG_STRING_EVENT
)
892 handle_output_debug_string (nullptr);
895 win32_clear_inferiors ();
897 remove_process (process
);
901 /* Implementation of target_ops::detach. */
904 win32_process_target::detach (process_info
*process
)
906 winapi_DebugActiveProcessStop DebugActiveProcessStop
= NULL
;
907 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
909 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
911 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
913 DebugActiveProcessStop
= GETPROCADDRESS (dll
, DebugActiveProcessStop
);
914 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
916 if (DebugSetProcessKillOnExit
== NULL
917 || DebugActiveProcessStop
== NULL
)
921 struct thread_resume resume
;
922 resume
.thread
= minus_one_ptid
;
923 resume
.kind
= resume_continue
;
925 this->resume (&resume
, 1);
928 if (!DebugActiveProcessStop (current_process_id
))
931 DebugSetProcessKillOnExit (FALSE
);
932 remove_process (process
);
934 win32_clear_inferiors ();
939 win32_process_target::mourn (struct process_info
*process
)
941 remove_process (process
);
944 /* Implementation of target_ops::join. */
947 win32_process_target::join (int pid
)
949 HANDLE h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
952 WaitForSingleObject (h
, INFINITE
);
957 /* Return true iff the thread with thread ID TID is alive. */
959 win32_process_target::thread_alive (ptid_t ptid
)
961 /* Our thread list is reliable; don't bother to poll target
963 return find_thread_ptid (ptid
) != NULL
;
966 /* Resume the inferior process. RESUME_INFO describes how we want
969 win32_process_target::resume (thread_resume
*resume_info
, size_t n
)
974 windows_thread_info
*th
;
975 DWORD continue_status
= DBG_CONTINUE
;
978 /* This handles the very limited set of resume packets that GDB can
979 currently produce. */
981 if (n
== 1 && resume_info
[0].thread
== minus_one_ptid
)
986 /* Yes, we're ignoring resume_info[0].thread. It'd be tricky to make
987 the Windows resume code do the right thing for thread switching. */
988 tid
= current_event
.dwThreadId
;
990 if (resume_info
[0].thread
!= minus_one_ptid
)
992 sig
= gdb_signal_from_host (resume_info
[0].sig
);
993 step
= resume_info
[0].kind
== resume_step
;
1001 if (sig
!= GDB_SIGNAL_0
)
1003 if (current_event
.dwDebugEventCode
!= EXCEPTION_DEBUG_EVENT
)
1005 OUTMSG (("Cannot continue with signal %s here.\n",
1006 gdb_signal_to_string (sig
)));
1008 else if (sig
== last_sig
)
1009 continue_status
= DBG_EXCEPTION_NOT_HANDLED
;
1011 OUTMSG (("Can only continue with received signal %s.\n",
1012 gdb_signal_to_string (last_sig
)));
1015 last_sig
= GDB_SIGNAL_0
;
1017 /* Get context for the currently selected thread. */
1018 ptid
= debug_event_ptid (¤t_event
);
1019 th
= thread_rec (ptid
, DONT_INVALIDATE_CONTEXT
);
1022 win32_prepare_to_resume (th
);
1024 DWORD
*context_flags
;
1027 context_flags
= &th
->wow64_context
.ContextFlags
;
1030 context_flags
= &th
->context
.ContextFlags
;
1033 /* Move register values from the inferior into the thread
1034 context structure. */
1035 regcache_invalidate ();
1039 if (the_low_target
.single_step
!= NULL
)
1040 (*the_low_target
.single_step
) (th
);
1042 error ("Single stepping is not supported "
1043 "in this configuration.\n");
1046 win32_set_thread_context (th
);
1051 /* Allow continuing with the same signal that interrupted us.
1052 Otherwise complain. */
1054 child_continue (continue_status
, tid
);
1058 win32_add_one_solib (const char *name
, CORE_ADDR load_addr
)
1060 char buf
[MAX_PATH
+ 1];
1061 char buf2
[MAX_PATH
+ 1];
1064 WIN32_FIND_DATA w32_fd
;
1065 WCHAR wname
[MAX_PATH
+ 1];
1066 mbstowcs (wname
, name
, MAX_PATH
);
1067 HANDLE h
= FindFirstFile (wname
, &w32_fd
);
1069 WIN32_FIND_DATAA w32_fd
;
1070 HANDLE h
= FindFirstFileA (name
, &w32_fd
);
1073 /* The symbols in a dll are offset by 0x1000, which is the
1074 offset from 0 of the first byte in an image - because
1075 of the file header and the section alignment. */
1076 load_addr
+= 0x1000;
1078 if (h
== INVALID_HANDLE_VALUE
)
1086 char cwd
[MAX_PATH
+ 1];
1088 if (GetCurrentDirectoryA (MAX_PATH
+ 1, cwd
))
1090 p
= strrchr (buf
, '\\');
1093 SetCurrentDirectoryA (buf
);
1094 GetFullPathNameA (w32_fd
.cFileName
, MAX_PATH
, buf
, &p
);
1095 SetCurrentDirectoryA (cwd
);
1102 if (strcasecmp (buf
, "ntdll.dll") == 0)
1104 GetSystemDirectoryA (buf
, sizeof (buf
));
1105 strcat (buf
, "\\ntdll.dll");
1110 cygwin_conv_path (CCP_WIN_A_TO_POSIX
, buf
, buf2
, sizeof (buf2
));
1115 loaded_dll (buf2
, load_addr
);
1118 typedef BOOL (WINAPI
*winapi_EnumProcessModules
) (HANDLE
, HMODULE
*,
1121 typedef BOOL (WINAPI
*winapi_EnumProcessModulesEx
) (HANDLE
, HMODULE
*, DWORD
,
1124 typedef BOOL (WINAPI
*winapi_GetModuleInformation
) (HANDLE
, HMODULE
,
1125 LPMODULEINFO
, DWORD
);
1126 typedef DWORD (WINAPI
*winapi_GetModuleFileNameExA
) (HANDLE
, HMODULE
,
1129 static winapi_EnumProcessModules win32_EnumProcessModules
;
1131 static winapi_EnumProcessModulesEx win32_EnumProcessModulesEx
;
1133 static winapi_GetModuleInformation win32_GetModuleInformation
;
1134 static winapi_GetModuleFileNameExA win32_GetModuleFileNameExA
;
1139 static int psapi_loaded
= 0;
1140 static HMODULE dll
= NULL
;
1145 dll
= LoadLibrary (TEXT("psapi.dll"));
1148 win32_EnumProcessModules
=
1149 GETPROCADDRESS (dll
, EnumProcessModules
);
1151 win32_EnumProcessModulesEx
=
1152 GETPROCADDRESS (dll
, EnumProcessModulesEx
);
1154 win32_GetModuleInformation
=
1155 GETPROCADDRESS (dll
, GetModuleInformation
);
1156 win32_GetModuleFileNameExA
=
1157 GETPROCADDRESS (dll
, GetModuleFileNameExA
);
1161 if (wow64_process
&& win32_EnumProcessModulesEx
== nullptr)
1165 return (win32_EnumProcessModules
!= NULL
1166 && win32_GetModuleInformation
!= NULL
1167 && win32_GetModuleFileNameExA
!= NULL
);
1172 /* Iterate over all DLLs currently mapped by our inferior, and
1173 add them to our list of solibs. */
1176 win32_add_all_dlls (void)
1180 HMODULE
*DllHandle
= dh_buf
;
1190 ok
= (*win32_EnumProcessModulesEx
) (current_process_handle
,
1194 LIST_MODULES_32BIT
);
1197 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1202 if (!ok
|| !cbNeeded
)
1205 DllHandle
= (HMODULE
*) alloca (cbNeeded
);
1211 ok
= (*win32_EnumProcessModulesEx
) (current_process_handle
,
1215 LIST_MODULES_32BIT
);
1218 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1225 char system_dir
[MAX_PATH
];
1226 char syswow_dir
[MAX_PATH
];
1227 size_t system_dir_len
= 0;
1228 bool convert_syswow_dir
= false;
1233 /* This fails on 32bit Windows because it has no SysWOW64 directory,
1234 and in this case a path conversion isn't necessary. */
1235 UINT len
= GetSystemWow64DirectoryA (syswow_dir
, sizeof (syswow_dir
));
1238 /* Check that we have passed a large enough buffer. */
1239 gdb_assert (len
< sizeof (syswow_dir
));
1241 len
= GetSystemDirectoryA (system_dir
, sizeof (system_dir
));
1243 gdb_assert (len
!= 0);
1244 /* Check that we have passed a large enough buffer. */
1245 gdb_assert (len
< sizeof (system_dir
));
1247 strcat (system_dir
, "\\");
1248 strcat (syswow_dir
, "\\");
1249 system_dir_len
= strlen (system_dir
);
1251 convert_syswow_dir
= true;
1256 for (i
= 1; i
< ((size_t) cbNeeded
/ sizeof (HMODULE
)); i
++)
1259 char dll_name
[MAX_PATH
];
1261 if (!(*win32_GetModuleInformation
) (current_process_handle
,
1266 if ((*win32_GetModuleFileNameExA
) (current_process_handle
,
1272 const char *name
= dll_name
;
1273 /* Convert the DLL path of 32bit processes returned by
1274 GetModuleFileNameEx from the 64bit system directory to the
1275 32bit syswow64 directory if necessary. */
1276 std::string syswow_dll_path
;
1277 if (convert_syswow_dir
1278 && strncasecmp (dll_name
, system_dir
, system_dir_len
) == 0
1279 && strchr (dll_name
+ system_dir_len
, '\\') == nullptr)
1281 syswow_dll_path
= syswow_dir
;
1282 syswow_dll_path
+= dll_name
+ system_dir_len
;
1283 name
= syswow_dll_path
.c_str();
1286 win32_add_one_solib (name
, (CORE_ADDR
) (uintptr_t) mi
.lpBaseOfDll
);
1291 typedef HANDLE (WINAPI
*winapi_CreateToolhelp32Snapshot
) (DWORD
, DWORD
);
1292 typedef BOOL (WINAPI
*winapi_Module32First
) (HANDLE
, LPMODULEENTRY32
);
1293 typedef BOOL (WINAPI
*winapi_Module32Next
) (HANDLE
, LPMODULEENTRY32
);
1295 /* See nat/windows-nat.h. */
1298 windows_nat::handle_load_dll ()
1300 LOAD_DLL_DEBUG_INFO
*event
= ¤t_event
.u
.LoadDll
;
1301 const char *dll_name
;
1303 dll_name
= get_image_name (current_process_handle
,
1304 event
->lpImageName
, event
->fUnicode
);
1308 win32_add_one_solib (dll_name
, (CORE_ADDR
) (uintptr_t) event
->lpBaseOfDll
);
1311 /* See nat/windows-nat.h. */
1314 windows_nat::handle_unload_dll ()
1316 CORE_ADDR load_addr
=
1317 (CORE_ADDR
) (uintptr_t) current_event
.u
.UnloadDll
.lpBaseOfDll
;
1319 /* The symbols in a dll are offset by 0x1000, which is the
1320 offset from 0 of the first byte in an image - because
1321 of the file header and the section alignment. */
1322 load_addr
+= 0x1000;
1323 unloaded_dll (NULL
, load_addr
);
1327 suspend_one_thread (thread_info
*thread
)
1329 windows_thread_info
*th
= (windows_thread_info
*) thread_target_data (thread
);
1335 fake_breakpoint_event (void)
1337 OUTMSG2(("fake_breakpoint_event\n"));
1339 faked_breakpoint
= 1;
1341 memset (¤t_event
, 0, sizeof (current_event
));
1342 current_event
.dwThreadId
= main_thread_id
;
1343 current_event
.dwDebugEventCode
= EXCEPTION_DEBUG_EVENT
;
1344 current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
1345 = EXCEPTION_BREAKPOINT
;
1347 for_each_thread (suspend_one_thread
);
1352 auto_delete_breakpoint (CORE_ADDR stop_pc
)
1358 /* See nat/windows-nat.h. */
1361 windows_nat::handle_ms_vc_exception (const EXCEPTION_RECORD
*rec
)
1366 /* See nat/windows-nat.h. */
1369 windows_nat::handle_access_violation (const EXCEPTION_RECORD
*rec
)
1374 /* A helper function that will, if needed, set
1375 'stopped_at_software_breakpoint' on the thread and adjust the
1381 struct regcache
*regcache
= get_thread_regcache (current_thread
, 1);
1382 child_fetch_inferior_registers (regcache
, -1);
1384 windows_thread_info
*th
= thread_rec (current_thread_ptid (),
1385 DONT_INVALIDATE_CONTEXT
);
1386 th
->stopped_at_software_breakpoint
= false;
1388 if (current_event
.dwDebugEventCode
== EXCEPTION_DEBUG_EVENT
1389 && ((current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
1390 == EXCEPTION_BREAKPOINT
)
1391 || (current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
1392 == STATUS_WX86_BREAKPOINT
))
1393 && child_initialization_done
)
1395 th
->stopped_at_software_breakpoint
= true;
1396 CORE_ADDR pc
= regcache_read_pc (regcache
);
1397 CORE_ADDR sw_breakpoint_pc
= pc
- the_low_target
.decr_pc_after_break
;
1398 regcache_write_pc (regcache
, sw_breakpoint_pc
);
1402 /* Get the next event from the child. */
1405 get_child_debug_event (DWORD
*continue_status
,
1406 struct target_waitstatus
*ourstatus
)
1410 last_sig
= GDB_SIGNAL_0
;
1411 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1412 *continue_status
= DBG_CONTINUE
;
1414 /* Check if GDB sent us an interrupt request. */
1415 check_remote_input_interrupt_request ();
1417 if (soft_interrupt_requested
)
1419 soft_interrupt_requested
= 0;
1420 fake_breakpoint_event ();
1426 gdb::optional
<pending_stop
> stop
= fetch_pending_stop (debug_threads
);
1427 if (stop
.has_value ())
1429 *ourstatus
= stop
->status
;
1430 current_event
= stop
->event
;
1431 ptid
= debug_event_ptid (¤t_event
);
1432 current_thread
= find_thread_ptid (ptid
);
1436 /* Keep the wait time low enough for comfortable remote
1437 interruption, but high enough so gdbserver doesn't become a
1439 if (!wait_for_debug_event (¤t_event
, 250))
1441 DWORD e
= GetLastError();
1443 if (e
== ERROR_PIPE_NOT_CONNECTED
)
1445 /* This will happen if the loader fails to succesfully
1446 load the application, e.g., if the main executable
1447 tries to pull in a non-existing export from a
1449 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1450 ourstatus
->value
.integer
= 1;
1460 switch (current_event
.dwDebugEventCode
)
1462 case CREATE_THREAD_DEBUG_EVENT
:
1463 OUTMSG2 (("gdbserver: kernel event CREATE_THREAD_DEBUG_EVENT "
1464 "for pid=%u tid=%x)\n",
1465 (unsigned) current_event
.dwProcessId
,
1466 (unsigned) current_event
.dwThreadId
));
1468 /* Record the existence of this thread. */
1469 child_add_thread (current_event
.dwProcessId
,
1470 current_event
.dwThreadId
,
1471 current_event
.u
.CreateThread
.hThread
,
1472 current_event
.u
.CreateThread
.lpThreadLocalBase
);
1475 case EXIT_THREAD_DEBUG_EVENT
:
1476 OUTMSG2 (("gdbserver: kernel event EXIT_THREAD_DEBUG_EVENT "
1477 "for pid=%u tid=%x\n",
1478 (unsigned) current_event
.dwProcessId
,
1479 (unsigned) current_event
.dwThreadId
));
1480 child_delete_thread (current_event
.dwProcessId
,
1481 current_event
.dwThreadId
);
1483 current_thread
= get_first_thread ();
1486 case CREATE_PROCESS_DEBUG_EVENT
:
1487 OUTMSG2 (("gdbserver: kernel event CREATE_PROCESS_DEBUG_EVENT "
1488 "for pid=%u tid=%x\n",
1489 (unsigned) current_event
.dwProcessId
,
1490 (unsigned) current_event
.dwThreadId
));
1491 CloseHandle (current_event
.u
.CreateProcessInfo
.hFile
);
1493 if (open_process_used
)
1495 CloseHandle (current_process_handle
);
1496 open_process_used
= false;
1499 current_process_handle
= current_event
.u
.CreateProcessInfo
.hProcess
;
1500 main_thread_id
= current_event
.dwThreadId
;
1502 /* Add the main thread. */
1503 child_add_thread (current_event
.dwProcessId
,
1505 current_event
.u
.CreateProcessInfo
.hThread
,
1506 current_event
.u
.CreateProcessInfo
.lpThreadLocalBase
);
1509 case EXIT_PROCESS_DEBUG_EVENT
:
1510 OUTMSG2 (("gdbserver: kernel event EXIT_PROCESS_DEBUG_EVENT "
1511 "for pid=%u tid=%x\n",
1512 (unsigned) current_event
.dwProcessId
,
1513 (unsigned) current_event
.dwThreadId
));
1515 DWORD exit_status
= current_event
.u
.ExitProcess
.dwExitCode
;
1516 /* If the exit status looks like a fatal exception, but we
1517 don't recognize the exception's code, make the original
1518 exit status value available, to avoid losing information. */
1520 = WIFSIGNALED (exit_status
) ? WTERMSIG (exit_status
) : -1;
1521 if (exit_signal
== -1)
1523 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1524 ourstatus
->value
.integer
= exit_status
;
1528 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
1529 ourstatus
->value
.sig
= gdb_signal_from_host (exit_signal
);
1532 child_continue (DBG_CONTINUE
, desired_stop_thread_id
);
1535 case LOAD_DLL_DEBUG_EVENT
:
1536 OUTMSG2 (("gdbserver: kernel event LOAD_DLL_DEBUG_EVENT "
1537 "for pid=%u tid=%x\n",
1538 (unsigned) current_event
.dwProcessId
,
1539 (unsigned) current_event
.dwThreadId
));
1540 CloseHandle (current_event
.u
.LoadDll
.hFile
);
1541 if (! child_initialization_done
)
1545 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1546 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1549 case UNLOAD_DLL_DEBUG_EVENT
:
1550 OUTMSG2 (("gdbserver: kernel event UNLOAD_DLL_DEBUG_EVENT "
1551 "for pid=%u tid=%x\n",
1552 (unsigned) current_event
.dwProcessId
,
1553 (unsigned) current_event
.dwThreadId
));
1554 if (! child_initialization_done
)
1556 handle_unload_dll ();
1557 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1558 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1561 case EXCEPTION_DEBUG_EVENT
:
1562 OUTMSG2 (("gdbserver: kernel event EXCEPTION_DEBUG_EVENT "
1563 "for pid=%u tid=%x\n",
1564 (unsigned) current_event
.dwProcessId
,
1565 (unsigned) current_event
.dwThreadId
));
1566 if (handle_exception (ourstatus
, debug_threads
)
1567 == HANDLE_EXCEPTION_UNHANDLED
)
1568 *continue_status
= DBG_EXCEPTION_NOT_HANDLED
;
1571 case OUTPUT_DEBUG_STRING_EVENT
:
1572 /* A message from the kernel (or Cygwin). */
1573 OUTMSG2 (("gdbserver: kernel event OUTPUT_DEBUG_STRING_EVENT "
1574 "for pid=%u tid=%x\n",
1575 (unsigned) current_event
.dwProcessId
,
1576 (unsigned) current_event
.dwThreadId
));
1577 handle_output_debug_string (nullptr);
1581 OUTMSG2 (("gdbserver: kernel event unknown "
1582 "for pid=%u tid=%x code=%x\n",
1583 (unsigned) current_event
.dwProcessId
,
1584 (unsigned) current_event
.dwThreadId
,
1585 (unsigned) current_event
.dwDebugEventCode
));
1589 ptid
= debug_event_ptid (¤t_event
);
1591 if (desired_stop_thread_id
!= -1 && desired_stop_thread_id
!= ptid
.lwp ())
1593 /* Pending stop. See the comment by the definition of
1594 "pending_stops" for details on why this is needed. */
1595 OUTMSG2 (("get_windows_debug_event - "
1596 "unexpected stop in 0x%lx (expecting 0x%x)\n",
1597 ptid
.lwp (), desired_stop_thread_id
));
1599 pending_stops
.push_back ({(DWORD
) ptid
.lwp (), *ourstatus
, current_event
});
1600 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1603 current_thread
= find_thread_ptid (ptid
);
1608 /* Wait for the inferior process to change state.
1609 STATUS will be filled in with a response code to send to GDB.
1610 Returns the signal which caused the process to stop. */
1612 win32_process_target::wait (ptid_t ptid
, target_waitstatus
*ourstatus
,
1615 if (cached_status
.kind
!= TARGET_WAITKIND_IGNORE
)
1617 /* The core always does a wait after creating the inferior, and
1618 do_initial_child_stuff already ran the inferior to the
1619 initial breakpoint (or an exit, if creating the process
1620 fails). Report it now. */
1621 *ourstatus
= cached_status
;
1622 cached_status
.kind
= TARGET_WAITKIND_IGNORE
;
1623 return debug_event_ptid (¤t_event
);
1628 DWORD continue_status
;
1629 if (!get_child_debug_event (&continue_status
, ourstatus
))
1632 switch (ourstatus
->kind
)
1634 case TARGET_WAITKIND_EXITED
:
1635 OUTMSG2 (("Child exited with retcode = %x\n",
1636 ourstatus
->value
.integer
));
1637 win32_clear_inferiors ();
1638 return ptid_t (current_event
.dwProcessId
);
1639 case TARGET_WAITKIND_STOPPED
:
1640 case TARGET_WAITKIND_SIGNALLED
:
1641 case TARGET_WAITKIND_LOADED
:
1643 OUTMSG2 (("Child Stopped with signal = %d \n",
1644 ourstatus
->value
.sig
));
1646 return debug_event_ptid (¤t_event
);
1649 OUTMSG (("Ignoring unknown internal event, %d\n", ourstatus
->kind
));
1651 case TARGET_WAITKIND_SPURIOUS
:
1652 /* do nothing, just continue */
1653 child_continue (continue_status
, desired_stop_thread_id
);
1659 /* Fetch registers from the inferior process.
1660 If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO. */
1662 win32_process_target::fetch_registers (regcache
*regcache
, int regno
)
1664 child_fetch_inferior_registers (regcache
, regno
);
1667 /* Store registers to the inferior process.
1668 If REGNO is -1, store all registers; otherwise, store at least REGNO. */
1670 win32_process_target::store_registers (regcache
*regcache
, int regno
)
1672 child_store_inferior_registers (regcache
, regno
);
1675 /* Read memory from the inferior process. This should generally be
1676 called through read_inferior_memory, which handles breakpoint shadowing.
1677 Read LEN bytes at MEMADDR into a buffer at MYADDR. */
1679 win32_process_target::read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
,
1682 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 0, 0) != len
;
1685 /* Write memory to the inferior process. This should generally be
1686 called through write_inferior_memory, which handles breakpoint shadowing.
1687 Write LEN bytes from the buffer at MYADDR to MEMADDR.
1688 Returns 0 on success and errno on failure. */
1690 win32_process_target::write_memory (CORE_ADDR memaddr
,
1691 const unsigned char *myaddr
, int len
)
1693 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 1, 0) != len
;
1696 /* Send an interrupt request to the inferior process. */
1698 win32_process_target::request_interrupt ()
1700 winapi_DebugBreakProcess DebugBreakProcess
;
1701 winapi_GenerateConsoleCtrlEvent GenerateConsoleCtrlEvent
;
1704 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
1706 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
1709 GenerateConsoleCtrlEvent
= GETPROCADDRESS (dll
, GenerateConsoleCtrlEvent
);
1711 if (GenerateConsoleCtrlEvent
!= NULL
1712 && GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT
, current_process_id
))
1715 /* GenerateConsoleCtrlEvent can fail if process id being debugged is
1716 not a process group id.
1717 Fallback to XP/Vista 'DebugBreakProcess', which generates a
1718 breakpoint exception in the interior process. */
1720 DebugBreakProcess
= GETPROCADDRESS (dll
, DebugBreakProcess
);
1722 if (DebugBreakProcess
!= NULL
1723 && DebugBreakProcess (current_process_handle
))
1726 /* Last resort, suspend all threads manually. */
1727 soft_interrupt_requested
= 1;
1731 win32_process_target::supports_hardware_single_step ()
1738 win32_error_to_fileio_error (DWORD err
)
1742 case ERROR_BAD_PATHNAME
:
1743 case ERROR_FILE_NOT_FOUND
:
1744 case ERROR_INVALID_NAME
:
1745 case ERROR_PATH_NOT_FOUND
:
1746 return FILEIO_ENOENT
;
1748 case ERROR_IO_DEVICE
:
1749 case ERROR_OPEN_FAILED
:
1751 case ERROR_INVALID_HANDLE
:
1752 return FILEIO_EBADF
;
1753 case ERROR_ACCESS_DENIED
:
1754 case ERROR_SHARING_VIOLATION
:
1755 return FILEIO_EACCES
;
1756 case ERROR_NOACCESS
:
1757 return FILEIO_EFAULT
;
1759 return FILEIO_EBUSY
;
1760 case ERROR_ALREADY_EXISTS
:
1761 case ERROR_FILE_EXISTS
:
1762 return FILEIO_EEXIST
;
1763 case ERROR_BAD_DEVICE
:
1764 return FILEIO_ENODEV
;
1765 case ERROR_DIRECTORY
:
1766 return FILEIO_ENOTDIR
;
1767 case ERROR_FILENAME_EXCED_RANGE
:
1768 case ERROR_INVALID_DATA
:
1769 case ERROR_INVALID_PARAMETER
:
1770 case ERROR_NEGATIVE_SEEK
:
1771 return FILEIO_EINVAL
;
1772 case ERROR_TOO_MANY_OPEN_FILES
:
1773 return FILEIO_EMFILE
;
1774 case ERROR_HANDLE_DISK_FULL
:
1775 case ERROR_DISK_FULL
:
1776 return FILEIO_ENOSPC
;
1777 case ERROR_WRITE_PROTECT
:
1778 return FILEIO_EROFS
;
1779 case ERROR_NOT_SUPPORTED
:
1780 return FILEIO_ENOSYS
;
1783 return FILEIO_EUNKNOWN
;
1787 win32_process_target::hostio_last_error (char *buf
)
1789 DWORD winerr
= GetLastError ();
1790 int fileio_err
= win32_error_to_fileio_error (winerr
);
1791 sprintf (buf
, "F-1,%x", fileio_err
);
1796 win32_process_target::supports_qxfer_siginfo ()
1801 /* Write Windows signal info. */
1804 win32_process_target::qxfer_siginfo (const char *annex
,
1805 unsigned char *readbuf
,
1806 unsigned const char *writebuf
,
1807 CORE_ADDR offset
, int len
)
1809 if (siginfo_er
.ExceptionCode
== 0)
1812 if (readbuf
== nullptr)
1815 char *buf
= (char *) &siginfo_er
;
1816 size_t bufsize
= sizeof (siginfo_er
);
1819 EXCEPTION_RECORD32 er32
;
1822 buf
= (char *) &er32
;
1823 bufsize
= sizeof (er32
);
1825 er32
.ExceptionCode
= siginfo_er
.ExceptionCode
;
1826 er32
.ExceptionFlags
= siginfo_er
.ExceptionFlags
;
1827 er32
.ExceptionRecord
= (uintptr_t) siginfo_er
.ExceptionRecord
;
1828 er32
.ExceptionAddress
= (uintptr_t) siginfo_er
.ExceptionAddress
;
1829 er32
.NumberParameters
= siginfo_er
.NumberParameters
;
1831 for (i
= 0; i
< EXCEPTION_MAXIMUM_PARAMETERS
; i
++)
1832 er32
.ExceptionInformation
[i
] = siginfo_er
.ExceptionInformation
[i
];
1836 if (offset
> bufsize
)
1839 if (offset
+ len
> bufsize
)
1840 len
= bufsize
- offset
;
1842 memcpy (readbuf
, buf
+ offset
, len
);
1848 win32_process_target::supports_get_tib_address ()
1853 /* Write Windows OS Thread Information Block address. */
1856 win32_process_target::get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
)
1858 windows_thread_info
*th
;
1859 th
= thread_rec (ptid
, DONT_INVALIDATE_CONTEXT
);
1863 *addr
= th
->thread_local_base
;
1867 /* Implementation of the target_ops method "sw_breakpoint_from_kind". */
1870 win32_process_target::sw_breakpoint_from_kind (int kind
, int *size
)
1872 *size
= the_low_target
.breakpoint_len
;
1873 return the_low_target
.breakpoint
;
1877 win32_process_target::stopped_by_sw_breakpoint ()
1879 windows_thread_info
*th
= thread_rec (current_thread_ptid (),
1880 DONT_INVALIDATE_CONTEXT
);
1881 return th
== nullptr ? false : th
->stopped_at_software_breakpoint
;
1885 win32_process_target::supports_stopped_by_sw_breakpoint ()
1891 win32_process_target::read_pc (struct regcache
*regcache
)
1893 return (*the_low_target
.get_pc
) (regcache
);
1897 win32_process_target::write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
1899 return (*the_low_target
.set_pc
) (regcache
, pc
);
1902 /* The win32 target ops object. */
1904 static win32_process_target the_win32_target
;
1906 /* Initialize the Win32 backend. */
1908 initialize_low (void)
1910 set_target_ops (&the_win32_target
);
1911 the_low_target
.arch_setup ();
1914 /* These functions are loaded dynamically, because they are not available
1916 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
1917 win32_Wow64GetThreadContext
= GETPROCADDRESS (dll
, Wow64GetThreadContext
);
1918 win32_Wow64SetThreadContext
= GETPROCADDRESS (dll
, Wow64SetThreadContext
);