1 /* Low level interface to Windows debugging, for gdbserver.
2 Copyright (C) 2006-2024 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/>. */
22 #include "gdbsupport/fileio.h"
23 #include "mem-break.h"
24 #include "win32-low.h"
25 #include "gdbthread.h"
34 #include "gdbsupport/gdb_tilde_expand.h"
35 #include "gdbsupport/common-inferior.h"
36 #include "gdbsupport/gdb_wait.h"
38 using namespace windows_nat
;
40 /* See win32-low.h. */
41 gdbserver_windows_process windows_process
;
44 #include <sys/cygwin.h>
47 #define OUTMSG(X) do { printf X; fflush (stderr); } while (0)
60 #define _T(x) TEXT (x)
63 int using_threads
= 1;
65 const struct target_desc
*win32_tdesc
;
67 const struct target_desc
*wow64_win32_tdesc
;
70 #define NUM_REGS (the_low_target.num_regs ())
72 /* The current debug event from WaitForDebugEvent. */
74 debug_event_ptid (DEBUG_EVENT
*event
)
76 return ptid_t (event
->dwProcessId
, event
->dwThreadId
, 0);
79 /* Get the thread context of the thread associated with TH. */
82 win32_get_thread_context (windows_thread_info
*th
)
84 windows_process
.with_context (th
, [] (auto *context
)
86 memset (context
, 0, sizeof (*context
));
88 (*the_low_target
.get_thread_context
) (th
);
91 /* Set the thread context of the thread associated with TH. */
94 win32_set_thread_context (windows_thread_info
*th
)
96 windows_process
.with_context (th
, [&] (auto *context
)
98 set_thread_context (th
->h
, context
);
102 /* Set the thread context of the thread associated with TH. */
105 win32_prepare_to_resume (windows_thread_info
*th
)
107 if (the_low_target
.prepare_to_resume
!= NULL
)
108 (*the_low_target
.prepare_to_resume
) (th
);
111 /* See win32-low.h. */
114 win32_require_context (windows_thread_info
*th
)
116 DWORD context_flags
= *windows_process
.context_flags_ptr (th
);
117 if (context_flags
== 0)
120 win32_get_thread_context (th
);
124 /* See nat/windows-nat.h. */
126 windows_thread_info
*
127 gdbserver_windows_process::thread_rec
128 (ptid_t ptid
, thread_disposition_type disposition
)
130 thread_info
*thread
= find_thread_ptid (ptid
);
134 auto th
= static_cast<windows_thread_info
*> (thread
->target_data ());
135 if (disposition
!= DONT_INVALIDATE_CONTEXT
)
136 win32_require_context (th
);
140 /* Add a thread to the thread list. */
141 static windows_thread_info
*
142 child_add_thread (DWORD pid
, DWORD tid
, HANDLE h
, void *tlb
)
144 windows_thread_info
*th
;
145 ptid_t ptid
= ptid_t (pid
, tid
, 0);
147 if ((th
= windows_process
.thread_rec (ptid
, DONT_INVALIDATE_CONTEXT
)))
150 CORE_ADDR base
= (CORE_ADDR
) (uintptr_t) tlb
;
152 /* For WOW64 processes, this is actually the pointer to the 64bit TIB,
153 and the 32bit TIB is exactly 2 pages after it. */
154 if (windows_process
.wow64_process
)
155 base
+= 2 * 4096; /* page size = 4096 */
157 th
= new windows_thread_info (tid
, h
, base
);
159 find_process_pid (pid
)->add_thread (ptid
, th
);
161 if (the_low_target
.thread_added
!= NULL
)
162 (*the_low_target
.thread_added
) (th
);
167 /* Delete a thread from the list of threads. */
169 delete_thread_info (thread_info
*thread
)
171 auto th
= static_cast<windows_thread_info
*> (thread
->target_data ());
173 thread
->process ()->remove_thread (thread
);
177 /* Delete a thread from the list of threads. */
179 child_delete_thread (DWORD pid
, DWORD tid
)
181 process_info
*process
= find_process_pid (pid
);
183 if (process
== nullptr)
186 /* If the last thread is exiting, just return. */
187 if (process
->thread_count () == 1)
190 thread_info
*thread
= process
->find_thread (ptid_t (pid
, tid
));
191 if (thread
== nullptr)
194 delete_thread_info (thread
);
197 /* These watchpoint related wrapper functions simply pass on the function call
198 if the low target has registered a corresponding function. */
201 win32_process_target::supports_z_point_type (char z_type
)
203 return (z_type
== Z_PACKET_SW_BP
204 || (the_low_target
.supports_z_point_type
!= NULL
205 && the_low_target
.supports_z_point_type (z_type
)));
209 win32_process_target::insert_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
210 int size
, raw_breakpoint
*bp
)
212 if (type
== raw_bkpt_type_sw
)
213 return insert_memory_breakpoint (bp
);
214 else if (the_low_target
.insert_point
!= NULL
)
215 return the_low_target
.insert_point (type
, addr
, size
, bp
);
217 /* Unsupported (see target.h). */
222 win32_process_target::remove_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
223 int size
, raw_breakpoint
*bp
)
225 if (type
== raw_bkpt_type_sw
)
226 return remove_memory_breakpoint (bp
);
227 else if (the_low_target
.remove_point
!= NULL
)
228 return the_low_target
.remove_point (type
, addr
, size
, bp
);
230 /* Unsupported (see target.h). */
235 win32_process_target::stopped_by_watchpoint ()
237 if (the_low_target
.stopped_by_watchpoint
!= NULL
)
238 return the_low_target
.stopped_by_watchpoint ();
244 win32_process_target::stopped_data_address ()
246 if (the_low_target
.stopped_data_address
!= NULL
)
247 return the_low_target
.stopped_data_address ();
253 /* Transfer memory from/to the debugged process. */
255 child_xfer_memory (CORE_ADDR memaddr
, char *our
, int len
,
256 int write
, process_stratum_target
*target
)
261 uintptr_t addr
= (uintptr_t) memaddr
;
265 success
= WriteProcessMemory (windows_process
.handle
, (LPVOID
) addr
,
266 (LPCVOID
) our
, len
, &done
);
268 lasterror
= GetLastError ();
269 FlushInstructionCache (windows_process
.handle
, (LPCVOID
) addr
, len
);
273 success
= ReadProcessMemory (windows_process
.handle
, (LPCVOID
) addr
,
274 (LPVOID
) our
, len
, &done
);
276 lasterror
= GetLastError ();
278 if (!success
&& lasterror
== ERROR_PARTIAL_COPY
&& done
> 0)
281 return success
? done
: -1;
284 /* Clear out any old thread list and reinitialize it to a pristine
287 child_init_thread_list (void)
289 for_each_thread (delete_thread_info
);
293 do_initial_child_stuff (HANDLE proch
, DWORD pid
, int attached
)
295 struct process_info
*proc
;
297 windows_process
.last_sig
= GDB_SIGNAL_0
;
298 windows_process
.handle
= proch
;
299 windows_process
.main_thread_id
= 0;
301 windows_process
.soft_interrupt_requested
= 0;
302 windows_process
.faked_breakpoint
= 0;
303 windows_process
.open_process_used
= true;
305 memset (&windows_process
.current_event
, 0,
306 sizeof (windows_process
.current_event
));
310 if (!IsWow64Process (proch
, &wow64
))
312 DWORD err
= GetLastError ();
313 throw_winerror_with_name ("Check if WOW64 process failed", err
);
315 windows_process
.wow64_process
= wow64
;
317 if (windows_process
.wow64_process
318 && (Wow64GetThreadContext
== nullptr
319 || Wow64SetThreadContext
== nullptr))
320 error ("WOW64 debugging is not supported on this system.\n");
322 windows_process
.ignore_first_breakpoint
323 = !attached
&& windows_process
.wow64_process
;
326 proc
= add_process (pid
, attached
);
328 if (windows_process
.wow64_process
)
329 proc
->tdesc
= wow64_win32_tdesc
;
332 proc
->tdesc
= win32_tdesc
;
333 child_init_thread_list ();
334 windows_process
.child_initialization_done
= 0;
336 if (the_low_target
.initial_stuff
!= NULL
)
337 (*the_low_target
.initial_stuff
) ();
339 windows_process
.cached_status
.set_ignore ();
341 /* Flush all currently pending debug events (thread and dll list) up
342 to the initial breakpoint. */
345 struct target_waitstatus status
;
347 the_target
->wait (minus_one_ptid
, &status
, 0);
349 /* Note win32_wait doesn't return thread events. */
350 if (status
.kind () != TARGET_WAITKIND_LOADED
)
352 windows_process
.cached_status
= status
;
357 struct thread_resume resume
;
359 resume
.thread
= minus_one_ptid
;
360 resume
.kind
= resume_continue
;
363 the_target
->resume (&resume
, 1);
367 /* Now that the inferior has been started and all DLLs have been mapped,
368 we can iterate over all DLLs and load them in.
370 We avoid doing it any earlier because, on certain versions of Windows,
371 LOAD_DLL_DEBUG_EVENTs are sometimes not complete. In particular,
372 we have seen on Windows 8.1 that the ntdll.dll load event does not
373 include the DLL name, preventing us from creating an associated SO.
374 A possible explanation is that ntdll.dll might be mapped before
375 the SO info gets created by the Windows system -- ntdll.dll is
376 the first DLL to be reported via LOAD_DLL_DEBUG_EVENT and other DLLs
377 do not seem to suffer from that problem.
379 Rather than try to work around this sort of issue, it is much
380 simpler to just ignore DLL load/unload events during the startup
381 phase, and then process them all in one batch now. */
382 windows_process
.add_all_dlls ();
384 windows_process
.child_initialization_done
= 1;
387 /* Resume all artificially suspended threads if we are continuing
390 continue_one_thread (thread_info
*thread
, int thread_id
)
392 auto th
= static_cast<windows_thread_info
*> (thread
->target_data ());
394 if (thread_id
== -1 || thread_id
== th
->tid
)
396 win32_prepare_to_resume (th
);
400 DWORD
*context_flags
= windows_process
.context_flags_ptr (th
);
403 win32_set_thread_context (th
);
413 child_continue (DWORD continue_status
, int thread_id
)
415 windows_process
.desired_stop_thread_id
= thread_id
;
416 if (windows_process
.matching_pending_stop (debug_threads
))
419 /* The inferior will only continue after the ContinueDebugEvent
421 for_each_thread ([&] (thread_info
*thread
)
423 continue_one_thread (thread
, thread_id
);
425 windows_process
.faked_breakpoint
= 0;
427 return continue_last_debug_event (continue_status
, debug_threads
);
430 /* Fetch register(s) from the current thread context. */
432 child_fetch_inferior_registers (struct regcache
*regcache
, int r
)
435 windows_thread_info
*th
436 = windows_process
.thread_rec (current_thread
->id
,
438 if (r
== -1 || r
> NUM_REGS
)
439 child_fetch_inferior_registers (regcache
, NUM_REGS
);
441 for (regno
= 0; regno
< r
; regno
++)
442 (*the_low_target
.fetch_inferior_register
) (regcache
, th
, regno
);
445 /* Store a new register value into the current thread context. We don't
446 change the program's context until later, when we resume it. */
448 child_store_inferior_registers (struct regcache
*regcache
, int r
)
451 windows_thread_info
*th
452 = windows_process
.thread_rec (current_thread
->id
,
454 if (r
== -1 || r
== 0 || r
> NUM_REGS
)
455 child_store_inferior_registers (regcache
, NUM_REGS
);
457 for (regno
= 0; regno
< r
; regno
++)
458 (*the_low_target
.store_inferior_register
) (regcache
, th
, regno
);
462 create_process (const char *program
, char *args
,
463 DWORD flags
, PROCESS_INFORMATION
*pi
)
465 const std::string
&inferior_cwd
= get_inferior_cwd ();
467 size_t argslen
, proglen
;
469 proglen
= strlen (program
) + 1;
470 argslen
= strlen (args
) + proglen
;
472 STARTUPINFOA si
= { sizeof (STARTUPINFOA
) };
473 char *program_and_args
= (char *) alloca (argslen
+ 1);
475 strcpy (program_and_args
, program
);
476 strcat (program_and_args
, " ");
477 strcat (program_and_args
, args
);
478 ret
= create_process (program
, /* image name */
479 program_and_args
, /* command line */
480 flags
, /* start flags */
481 NULL
, /* environment */
482 /* current directory */
483 (inferior_cwd
.empty ()
485 : gdb_tilde_expand (inferior_cwd
).c_str()),
486 get_client_state ().disable_randomization
,
487 &si
, /* start info */
496 win32_process_target::create_inferior (const char *program
,
497 const std::string
&program_args
)
499 client_state
&cs
= get_client_state ();
501 char real_path
[PATH_MAX
];
502 char *orig_path
, *new_path
, *path_ptr
;
506 PROCESS_INFORMATION pi
;
508 char *args
= (char *) program_args
.c_str ();
510 /* win32_wait needs to know we're not attaching. */
511 windows_process
.attaching
= 0;
514 error ("No executable specified, specify executable to debug.\n");
516 flags
= DEBUG_PROCESS
| DEBUG_ONLY_THIS_PROCESS
;
520 path_ptr
= getenv ("PATH");
523 int size
= cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, NULL
, 0);
524 orig_path
= (char *) alloca (strlen (path_ptr
) + 1);
525 new_path
= (char *) alloca (size
);
526 strcpy (orig_path
, path_ptr
);
527 cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, new_path
, size
);
528 setenv ("PATH", new_path
, 1);
530 cygwin_conv_path (CCP_POSIX_TO_WIN_A
, program
, real_path
, PATH_MAX
);
534 OUTMSG2 (("Command line is \"%s %s\"\n", program
, args
));
536 #ifdef CREATE_NEW_PROCESS_GROUP
537 flags
|= CREATE_NEW_PROCESS_GROUP
;
540 ret
= create_process (program
, args
, flags
, &pi
);
541 err
= GetLastError ();
542 if (!ret
&& err
== ERROR_FILE_NOT_FOUND
)
544 char *exename
= (char *) alloca (strlen (program
) + 5);
545 strcat (strcpy (exename
, program
), ".exe");
546 ret
= create_process (exename
, args
, flags
, &pi
);
547 err
= GetLastError ();
552 setenv ("PATH", orig_path
, 1);
557 std::string msg
= string_printf (_("Error creating process \"%s %s\""),
559 throw_winerror_with_name (msg
.c_str (), err
);
563 OUTMSG2 (("Process created: %s %s\n", program
, (char *) args
));
566 CloseHandle (pi
.hThread
);
568 do_initial_child_stuff (pi
.hProcess
, pi
.dwProcessId
, 0);
570 /* Wait till we are at 1st instruction in program, return new pid
571 (assuming success). */
572 cs
.last_ptid
= wait (ptid_t (pi
.dwProcessId
), &cs
.last_status
, 0);
574 /* Necessary for handle_v_kill. */
575 signal_pid
= pi
.dwProcessId
;
577 return pi
.dwProcessId
;
580 /* Attach to a running process.
581 PID is the process ID to attach to, specified by the user
582 or a higher layer. */
584 win32_process_target::attach (unsigned long pid
)
589 h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
592 if (DebugActiveProcess (pid
))
594 DebugSetProcessKillOnExit (FALSE
);
596 /* win32_wait needs to know we're attaching. */
597 windows_process
.attaching
= 1;
598 do_initial_child_stuff (h
, pid
, 1);
605 err
= GetLastError ();
606 throw_winerror_with_name ("Attach to process failed", err
);
609 /* See nat/windows-nat.h. */
612 gdbserver_windows_process::handle_output_debug_string
613 (struct target_waitstatus
*ourstatus
)
615 #define READ_BUFFER_LEN 1024
617 char s
[READ_BUFFER_LEN
+ 1] = { 0 };
618 DWORD nbytes
= current_event
.u
.DebugString
.nDebugStringLength
;
623 if (nbytes
> READ_BUFFER_LEN
)
624 nbytes
= READ_BUFFER_LEN
;
626 addr
= (CORE_ADDR
) (size_t) current_event
.u
.DebugString
.lpDebugStringData
;
628 if (current_event
.u
.DebugString
.fUnicode
)
630 /* The event tells us how many bytes, not chars, even
632 WCHAR buffer
[(READ_BUFFER_LEN
+ 1) / sizeof (WCHAR
)] = { 0 };
633 if (read_inferior_memory (addr
, (unsigned char *) buffer
, nbytes
) != 0)
635 wcstombs (s
, buffer
, (nbytes
+ 1) / sizeof (WCHAR
));
639 if (read_inferior_memory (addr
, (unsigned char *) s
, nbytes
) != 0)
643 if (!startswith (s
, "cYg"))
653 #undef READ_BUFFER_LEN
659 win32_clear_process ()
661 if (windows_process
.open_process_used
)
663 CloseHandle (windows_process
.handle
);
664 windows_process
.open_process_used
= false;
667 for_each_thread (delete_thread_info
);
668 windows_process
.siginfo_er
.ExceptionCode
= 0;
671 /* Implementation of target_ops::kill. */
674 win32_process_target::kill (process_info
*process
)
676 TerminateProcess (windows_process
.handle
, 0);
679 if (!child_continue (DBG_CONTINUE
, -1))
681 if (!wait_for_debug_event (&windows_process
.current_event
, INFINITE
))
683 if (windows_process
.current_event
.dwDebugEventCode
684 == EXIT_PROCESS_DEBUG_EVENT
)
686 else if (windows_process
.current_event
.dwDebugEventCode
687 == OUTPUT_DEBUG_STRING_EVENT
)
688 windows_process
.handle_output_debug_string (nullptr);
691 win32_clear_process ();
692 remove_process (process
);
697 /* Implementation of target_ops::detach. */
700 win32_process_target::detach (process_info
*process
)
702 struct thread_resume resume
;
703 resume
.thread
= minus_one_ptid
;
704 resume
.kind
= resume_continue
;
706 this->resume (&resume
, 1);
708 if (!DebugActiveProcessStop (process
->pid
))
711 DebugSetProcessKillOnExit (FALSE
);
712 win32_clear_process ();
713 remove_process (process
);
719 win32_process_target::mourn (struct process_info
*process
)
721 remove_process (process
);
724 /* Implementation of target_ops::join. */
727 win32_process_target::join (int pid
)
729 HANDLE h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
732 WaitForSingleObject (h
, INFINITE
);
737 /* Return true iff the thread with thread ID TID is alive. */
739 win32_process_target::thread_alive (ptid_t ptid
)
741 /* Our thread list is reliable; don't bother to poll target
743 return find_thread_ptid (ptid
) != NULL
;
746 /* Resume the inferior process. RESUME_INFO describes how we want
749 win32_process_target::resume (thread_resume
*resume_info
, size_t n
)
754 windows_thread_info
*th
;
755 DWORD continue_status
= DBG_CONTINUE
;
758 /* This handles the very limited set of resume packets that GDB can
759 currently produce. */
761 if (n
== 1 && resume_info
[0].thread
== minus_one_ptid
)
766 /* Yes, we're ignoring resume_info[0].thread. It'd be tricky to make
767 the Windows resume code do the right thing for thread switching. */
768 tid
= windows_process
.current_event
.dwThreadId
;
770 if (resume_info
[0].thread
!= minus_one_ptid
)
772 sig
= gdb_signal_from_host (resume_info
[0].sig
);
773 step
= resume_info
[0].kind
== resume_step
;
781 if (sig
!= GDB_SIGNAL_0
)
783 if (windows_process
.current_event
.dwDebugEventCode
784 != EXCEPTION_DEBUG_EVENT
)
786 OUTMSG (("Cannot continue with signal %s here.\n",
787 gdb_signal_to_string (sig
)));
789 else if (sig
== windows_process
.last_sig
)
790 continue_status
= DBG_EXCEPTION_NOT_HANDLED
;
792 OUTMSG (("Can only continue with received signal %s.\n",
793 gdb_signal_to_string (windows_process
.last_sig
)));
796 windows_process
.last_sig
= GDB_SIGNAL_0
;
798 /* Get context for the currently selected thread. */
799 ptid
= debug_event_ptid (&windows_process
.current_event
);
800 th
= windows_process
.thread_rec (ptid
, DONT_INVALIDATE_CONTEXT
);
803 win32_prepare_to_resume (th
);
805 DWORD
*context_flags
= windows_process
.context_flags_ptr (th
);
808 /* Move register values from the inferior into the thread
809 context structure. */
810 regcache_invalidate ();
814 if (the_low_target
.single_step
!= NULL
)
815 (*the_low_target
.single_step
) (th
);
817 error ("Single stepping is not supported "
818 "in this configuration.\n");
821 win32_set_thread_context (th
);
826 /* Allow continuing with the same signal that interrupted us.
827 Otherwise complain. */
829 child_continue (continue_status
, tid
);
832 /* See nat/windows-nat.h. */
835 gdbserver_windows_process::handle_load_dll (const char *name
, LPVOID base
)
837 CORE_ADDR load_addr
= (CORE_ADDR
) (uintptr_t) base
;
839 char buf
[MAX_PATH
+ 1];
840 char buf2
[MAX_PATH
+ 1];
842 WIN32_FIND_DATAA w32_fd
;
843 HANDLE h
= FindFirstFileA (name
, &w32_fd
);
845 /* The symbols in a dll are offset by 0x1000, which is the
846 offset from 0 of the first byte in an image - because
847 of the file header and the section alignment. */
850 if (h
== INVALID_HANDLE_VALUE
)
857 char cwd
[MAX_PATH
+ 1];
859 if (GetCurrentDirectoryA (MAX_PATH
+ 1, cwd
))
861 p
= strrchr (buf
, '\\');
864 SetCurrentDirectoryA (buf
);
865 GetFullPathNameA (w32_fd
.cFileName
, MAX_PATH
, buf
, &p
);
866 SetCurrentDirectoryA (cwd
);
871 if (strcasecmp (buf
, "ntdll.dll") == 0)
873 GetSystemDirectoryA (buf
, sizeof (buf
));
874 strcat (buf
, "\\ntdll.dll");
878 cygwin_conv_path (CCP_WIN_A_TO_POSIX
, buf
, buf2
, sizeof (buf2
));
883 loaded_dll (buf2
, load_addr
);
886 /* See nat/windows-nat.h. */
889 gdbserver_windows_process::handle_unload_dll ()
891 CORE_ADDR load_addr
=
892 (CORE_ADDR
) (uintptr_t) current_event
.u
.UnloadDll
.lpBaseOfDll
;
894 /* The symbols in a dll are offset by 0x1000, which is the
895 offset from 0 of the first byte in an image - because
896 of the file header and the section alignment. */
898 unloaded_dll (NULL
, load_addr
);
902 suspend_one_thread (thread_info
*thread
)
904 auto th
= static_cast<windows_thread_info
*> (thread
->target_data ());
910 fake_breakpoint_event (void)
912 OUTMSG2(("fake_breakpoint_event\n"));
914 windows_process
.faked_breakpoint
= 1;
916 memset (&windows_process
.current_event
, 0,
917 sizeof (windows_process
.current_event
));
918 windows_process
.current_event
.dwThreadId
= windows_process
.main_thread_id
;
919 windows_process
.current_event
.dwDebugEventCode
= EXCEPTION_DEBUG_EVENT
;
920 windows_process
.current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
921 = EXCEPTION_BREAKPOINT
;
923 for_each_thread (suspend_one_thread
);
926 /* See nat/windows-nat.h. */
929 gdbserver_windows_process::handle_access_violation
930 (const EXCEPTION_RECORD
*rec
)
935 /* A helper function that will, if needed, set
936 'stopped_at_software_breakpoint' on the thread and adjust the
942 regcache
*regcache
= get_thread_regcache (current_thread
);
943 child_fetch_inferior_registers (regcache
, -1);
945 windows_thread_info
*th
946 = windows_process
.thread_rec (current_thread
->id
,
947 DONT_INVALIDATE_CONTEXT
);
948 th
->stopped_at_software_breakpoint
= false;
950 if (windows_process
.current_event
.dwDebugEventCode
== EXCEPTION_DEBUG_EVENT
951 && ((windows_process
.current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
952 == EXCEPTION_BREAKPOINT
)
953 || (windows_process
.current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
954 == STATUS_WX86_BREAKPOINT
))
955 && windows_process
.child_initialization_done
)
957 th
->stopped_at_software_breakpoint
= true;
958 CORE_ADDR pc
= regcache_read_pc (regcache
);
959 CORE_ADDR sw_breakpoint_pc
= pc
- the_low_target
.decr_pc_after_break
;
960 regcache_write_pc (regcache
, sw_breakpoint_pc
);
964 /* Get the next event from the child. */
967 get_child_debug_event (DWORD
*continue_status
,
968 struct target_waitstatus
*ourstatus
)
972 windows_process
.last_sig
= GDB_SIGNAL_0
;
973 ourstatus
->set_spurious ();
974 *continue_status
= DBG_CONTINUE
;
976 /* Check if GDB sent us an interrupt request. */
977 check_remote_input_interrupt_request ();
979 DEBUG_EVENT
*current_event
= &windows_process
.current_event
;
981 if (windows_process
.soft_interrupt_requested
)
983 windows_process
.soft_interrupt_requested
= 0;
984 fake_breakpoint_event ();
988 windows_process
.attaching
= 0;
990 std::optional
<pending_stop
> stop
991 = windows_process
.fetch_pending_stop (debug_threads
);
992 if (stop
.has_value ())
994 *ourstatus
= stop
->status
;
995 windows_process
.current_event
= stop
->event
;
996 ptid
= debug_event_ptid (&windows_process
.current_event
);
997 switch_to_thread (find_thread_ptid (ptid
));
1001 /* Keep the wait time low enough for comfortable remote
1002 interruption, but high enough so gdbserver doesn't become a
1004 if (!wait_for_debug_event (&windows_process
.current_event
, 250))
1006 DWORD e
= GetLastError();
1008 if (e
== ERROR_PIPE_NOT_CONNECTED
)
1010 /* This will happen if the loader fails to successfully
1011 load the application, e.g., if the main executable
1012 tries to pull in a non-existing export from a
1014 ourstatus
->set_exited (1);
1024 switch (current_event
->dwDebugEventCode
)
1026 case CREATE_THREAD_DEBUG_EVENT
:
1027 OUTMSG2 (("gdbserver: kernel event CREATE_THREAD_DEBUG_EVENT "
1028 "for pid=%u tid=%x)\n",
1029 (unsigned) current_event
->dwProcessId
,
1030 (unsigned) current_event
->dwThreadId
));
1032 /* Record the existence of this thread. */
1033 child_add_thread (current_event
->dwProcessId
,
1034 current_event
->dwThreadId
,
1035 current_event
->u
.CreateThread
.hThread
,
1036 current_event
->u
.CreateThread
.lpThreadLocalBase
);
1039 case EXIT_THREAD_DEBUG_EVENT
:
1040 OUTMSG2 (("gdbserver: kernel event EXIT_THREAD_DEBUG_EVENT "
1041 "for pid=%u tid=%x\n",
1042 (unsigned) current_event
->dwProcessId
,
1043 (unsigned) current_event
->dwThreadId
));
1044 child_delete_thread (current_event
->dwProcessId
,
1045 current_event
->dwThreadId
);
1047 switch_to_thread (get_first_thread ());
1050 case CREATE_PROCESS_DEBUG_EVENT
:
1051 OUTMSG2 (("gdbserver: kernel event CREATE_PROCESS_DEBUG_EVENT "
1052 "for pid=%u tid=%x\n",
1053 (unsigned) current_event
->dwProcessId
,
1054 (unsigned) current_event
->dwThreadId
));
1055 CloseHandle (current_event
->u
.CreateProcessInfo
.hFile
);
1057 if (windows_process
.open_process_used
)
1059 CloseHandle (windows_process
.handle
);
1060 windows_process
.open_process_used
= false;
1063 windows_process
.handle
= current_event
->u
.CreateProcessInfo
.hProcess
;
1064 windows_process
.main_thread_id
= current_event
->dwThreadId
;
1066 /* Add the main thread. */
1067 child_add_thread (current_event
->dwProcessId
,
1068 windows_process
.main_thread_id
,
1069 current_event
->u
.CreateProcessInfo
.hThread
,
1070 current_event
->u
.CreateProcessInfo
.lpThreadLocalBase
);
1073 case EXIT_PROCESS_DEBUG_EVENT
:
1074 OUTMSG2 (("gdbserver: kernel event EXIT_PROCESS_DEBUG_EVENT "
1075 "for pid=%u tid=%x\n",
1076 (unsigned) current_event
->dwProcessId
,
1077 (unsigned) current_event
->dwThreadId
));
1079 DWORD exit_status
= current_event
->u
.ExitProcess
.dwExitCode
;
1080 /* If the exit status looks like a fatal exception, but we
1081 don't recognize the exception's code, make the original
1082 exit status value available, to avoid losing information. */
1084 = WIFSIGNALED (exit_status
) ? WTERMSIG (exit_status
) : -1;
1085 if (exit_signal
== -1)
1086 ourstatus
->set_exited (exit_status
);
1088 ourstatus
->set_signalled (gdb_signal_from_host (exit_signal
));
1090 child_continue (DBG_CONTINUE
, windows_process
.desired_stop_thread_id
);
1093 case LOAD_DLL_DEBUG_EVENT
:
1094 OUTMSG2 (("gdbserver: kernel event LOAD_DLL_DEBUG_EVENT "
1095 "for pid=%u tid=%x\n",
1096 (unsigned) current_event
->dwProcessId
,
1097 (unsigned) current_event
->dwThreadId
));
1098 CloseHandle (current_event
->u
.LoadDll
.hFile
);
1099 if (! windows_process
.child_initialization_done
)
1101 windows_process
.dll_loaded_event ();
1103 ourstatus
->set_loaded ();
1106 case UNLOAD_DLL_DEBUG_EVENT
:
1107 OUTMSG2 (("gdbserver: kernel event UNLOAD_DLL_DEBUG_EVENT "
1108 "for pid=%u tid=%x\n",
1109 (unsigned) current_event
->dwProcessId
,
1110 (unsigned) current_event
->dwThreadId
));
1111 if (! windows_process
.child_initialization_done
)
1113 windows_process
.handle_unload_dll ();
1114 ourstatus
->set_loaded ();
1117 case EXCEPTION_DEBUG_EVENT
:
1118 OUTMSG2 (("gdbserver: kernel event EXCEPTION_DEBUG_EVENT "
1119 "for pid=%u tid=%x\n",
1120 (unsigned) current_event
->dwProcessId
,
1121 (unsigned) current_event
->dwThreadId
));
1122 if (windows_process
.handle_exception (ourstatus
, debug_threads
)
1123 == HANDLE_EXCEPTION_UNHANDLED
)
1124 *continue_status
= DBG_EXCEPTION_NOT_HANDLED
;
1127 case OUTPUT_DEBUG_STRING_EVENT
:
1128 /* A message from the kernel (or Cygwin). */
1129 OUTMSG2 (("gdbserver: kernel event OUTPUT_DEBUG_STRING_EVENT "
1130 "for pid=%u tid=%x\n",
1131 (unsigned) current_event
->dwProcessId
,
1132 (unsigned) current_event
->dwThreadId
));
1133 windows_process
.handle_output_debug_string (nullptr);
1137 OUTMSG2 (("gdbserver: kernel event unknown "
1138 "for pid=%u tid=%x code=%x\n",
1139 (unsigned) current_event
->dwProcessId
,
1140 (unsigned) current_event
->dwThreadId
,
1141 (unsigned) current_event
->dwDebugEventCode
));
1145 ptid
= debug_event_ptid (&windows_process
.current_event
);
1147 if (windows_process
.desired_stop_thread_id
!= -1
1148 && windows_process
.desired_stop_thread_id
!= ptid
.lwp ())
1150 /* Pending stop. See the comment by the definition of
1151 "pending_stops" for details on why this is needed. */
1152 OUTMSG2 (("get_windows_debug_event - "
1153 "unexpected stop in 0x%lx (expecting 0x%x)\n",
1154 ptid
.lwp (), windows_process
.desired_stop_thread_id
));
1156 windows_process
.pending_stops
.push_back
1157 ({(DWORD
) ptid
.lwp (), *ourstatus
, *current_event
});
1158 ourstatus
->set_spurious ();
1161 switch_to_thread (find_thread_ptid (ptid
));
1166 /* Wait for the inferior process to change state.
1167 STATUS will be filled in with a response code to send to GDB.
1168 Returns the signal which caused the process to stop. */
1170 win32_process_target::wait (ptid_t ptid
, target_waitstatus
*ourstatus
,
1171 target_wait_flags options
)
1173 if (windows_process
.cached_status
.kind () != TARGET_WAITKIND_IGNORE
)
1175 /* The core always does a wait after creating the inferior, and
1176 do_initial_child_stuff already ran the inferior to the
1177 initial breakpoint (or an exit, if creating the process
1178 fails). Report it now. */
1179 *ourstatus
= windows_process
.cached_status
;
1180 windows_process
.cached_status
.set_ignore ();
1181 return debug_event_ptid (&windows_process
.current_event
);
1186 DWORD continue_status
;
1187 if (!get_child_debug_event (&continue_status
, ourstatus
))
1190 switch (ourstatus
->kind ())
1192 case TARGET_WAITKIND_EXITED
:
1193 OUTMSG2 (("Child exited with retcode = %x\n",
1194 ourstatus
->exit_status ()));
1195 win32_clear_process ();
1196 return ptid_t (windows_process
.current_event
.dwProcessId
);
1197 case TARGET_WAITKIND_STOPPED
:
1198 case TARGET_WAITKIND_SIGNALLED
:
1199 case TARGET_WAITKIND_LOADED
:
1201 OUTMSG2 (("Child Stopped with signal = %d \n",
1202 ourstatus
->sig ()));
1204 return debug_event_ptid (&windows_process
.current_event
);
1207 OUTMSG (("Ignoring unknown internal event, %d\n",
1208 ourstatus
->kind ()));
1210 case TARGET_WAITKIND_SPURIOUS
:
1211 /* do nothing, just continue */
1212 child_continue (continue_status
,
1213 windows_process
.desired_stop_thread_id
);
1219 /* Fetch registers from the inferior process.
1220 If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO. */
1222 win32_process_target::fetch_registers (regcache
*regcache
, int regno
)
1224 child_fetch_inferior_registers (regcache
, regno
);
1227 /* Store registers to the inferior process.
1228 If REGNO is -1, store all registers; otherwise, store at least REGNO. */
1230 win32_process_target::store_registers (regcache
*regcache
, int regno
)
1232 child_store_inferior_registers (regcache
, regno
);
1235 /* Read memory from the inferior process. This should generally be
1236 called through read_inferior_memory, which handles breakpoint shadowing.
1237 Read LEN bytes at MEMADDR into a buffer at MYADDR. */
1239 win32_process_target::read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
,
1242 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 0, 0) != len
;
1245 /* Write memory to the inferior process. This should generally be
1246 called through write_inferior_memory, which handles breakpoint shadowing.
1247 Write LEN bytes from the buffer at MYADDR to MEMADDR.
1248 Returns 0 on success and errno on failure. */
1250 win32_process_target::write_memory (CORE_ADDR memaddr
,
1251 const unsigned char *myaddr
, int len
)
1253 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 1, 0) != len
;
1256 /* Send an interrupt request to the inferior process. */
1258 win32_process_target::request_interrupt ()
1260 if (GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT
, signal_pid
))
1263 /* GenerateConsoleCtrlEvent can fail if process id being debugged is
1264 not a process group id.
1265 Fallback to XP/Vista 'DebugBreakProcess', which generates a
1266 breakpoint exception in the interior process. */
1268 if (DebugBreakProcess (windows_process
.handle
))
1271 /* Last resort, suspend all threads manually. */
1272 windows_process
.soft_interrupt_requested
= 1;
1276 win32_process_target::supports_hardware_single_step ()
1282 win32_process_target::supports_qxfer_siginfo ()
1287 /* Write Windows signal info. */
1290 win32_process_target::qxfer_siginfo (const char *annex
,
1291 unsigned char *readbuf
,
1292 unsigned const char *writebuf
,
1293 CORE_ADDR offset
, int len
)
1295 if (windows_process
.siginfo_er
.ExceptionCode
== 0)
1298 if (readbuf
== nullptr)
1301 char *buf
= (char *) &windows_process
.siginfo_er
;
1302 size_t bufsize
= sizeof (windows_process
.siginfo_er
);
1305 EXCEPTION_RECORD32 er32
;
1306 if (windows_process
.wow64_process
)
1308 buf
= (char *) &er32
;
1309 bufsize
= sizeof (er32
);
1311 er32
.ExceptionCode
= windows_process
.siginfo_er
.ExceptionCode
;
1312 er32
.ExceptionFlags
= windows_process
.siginfo_er
.ExceptionFlags
;
1313 er32
.ExceptionRecord
1314 = (uintptr_t) windows_process
.siginfo_er
.ExceptionRecord
;
1315 er32
.ExceptionAddress
1316 = (uintptr_t) windows_process
.siginfo_er
.ExceptionAddress
;
1317 er32
.NumberParameters
= windows_process
.siginfo_er
.NumberParameters
;
1319 for (i
= 0; i
< EXCEPTION_MAXIMUM_PARAMETERS
; i
++)
1320 er32
.ExceptionInformation
[i
]
1321 = windows_process
.siginfo_er
.ExceptionInformation
[i
];
1325 if (offset
> bufsize
)
1328 if (offset
+ len
> bufsize
)
1329 len
= bufsize
- offset
;
1331 memcpy (readbuf
, buf
+ offset
, len
);
1337 win32_process_target::supports_get_tib_address ()
1342 /* Write Windows OS Thread Information Block address. */
1345 win32_process_target::get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
)
1347 windows_thread_info
*th
;
1348 th
= windows_process
.thread_rec (ptid
, DONT_INVALIDATE_CONTEXT
);
1352 *addr
= th
->thread_local_base
;
1356 /* Implementation of the target_ops method "sw_breakpoint_from_kind". */
1359 win32_process_target::sw_breakpoint_from_kind (int kind
, int *size
)
1361 *size
= the_low_target
.breakpoint_len
;
1362 return the_low_target
.breakpoint
;
1366 win32_process_target::stopped_by_sw_breakpoint ()
1368 windows_thread_info
*th
1369 = windows_process
.thread_rec (current_thread
->id
,
1370 DONT_INVALIDATE_CONTEXT
);
1371 return th
== nullptr ? false : th
->stopped_at_software_breakpoint
;
1375 win32_process_target::supports_stopped_by_sw_breakpoint ()
1381 win32_process_target::read_pc (struct regcache
*regcache
)
1383 return (*the_low_target
.get_pc
) (regcache
);
1387 win32_process_target::write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
1389 return (*the_low_target
.set_pc
) (regcache
, pc
);
1393 win32_process_target::thread_name (ptid_t thread
)
1395 windows_thread_info
*th
1396 = windows_process
.thread_rec (current_thread
->id
,
1397 DONT_INVALIDATE_CONTEXT
);
1398 return th
->thread_name ();
1402 win32_process_target::pid_to_exec_file (int pid
)
1404 return windows_process
.pid_to_exec_file (pid
);
1407 /* The win32 target ops object. */
1409 static win32_process_target the_win32_target
;
1411 /* Initialize the Win32 backend. */
1413 initialize_low (void)
1415 set_target_ops (&the_win32_target
);
1416 the_low_target
.arch_setup ();
1418 initialize_loadable ();