1 /* Low level interface to Windows debugging, for gdbserver.
2 Copyright (C) 2006-2022 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
;
41 /* See win32-low.h. */
42 windows_process_info windows_process
;
45 #include <sys/cygwin.h>
48 #define OUTMSG(X) do { printf X; fflush (stderr); } while (0)
61 #define _T(x) TEXT (x)
65 #define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0]))
68 int using_threads
= 1;
71 static int attaching
= 0;
73 /* A status that hasn't been reported to the core yet, and so
74 win32_wait should return it next, instead of fetching the next
75 debug event off the win32 API. */
76 static struct target_waitstatus cached_status
;
78 /* Non zero if an interrupt request is to be satisfied by suspending
80 static int soft_interrupt_requested
= 0;
82 /* Non zero if the inferior is stopped in a simulated breakpoint done
83 by suspending all the threads. */
84 static int faked_breakpoint
= 0;
86 /* True if current_process_handle needs to be closed. */
87 static bool open_process_used
= false;
89 const struct target_desc
*win32_tdesc
;
91 const struct target_desc
*wow64_win32_tdesc
;
94 #define NUM_REGS (the_low_target.num_regs ())
96 /* Get the thread ID from the current selected inferior (the current
99 current_thread_ptid (void)
104 /* The current debug event from WaitForDebugEvent. */
106 debug_event_ptid (DEBUG_EVENT
*event
)
108 return ptid_t (event
->dwProcessId
, event
->dwThreadId
, 0);
111 /* Get the thread context of the thread associated with TH. */
114 win32_get_thread_context (windows_thread_info
*th
)
117 if (windows_process
.wow64_process
)
118 memset (&th
->wow64_context
, 0, sizeof (WOW64_CONTEXT
));
121 memset (&th
->context
, 0, sizeof (CONTEXT
));
122 (*the_low_target
.get_thread_context
) (th
);
125 /* Set the thread context of the thread associated with TH. */
128 win32_set_thread_context (windows_thread_info
*th
)
131 if (windows_process
.wow64_process
)
132 Wow64SetThreadContext (th
->h
, &th
->wow64_context
);
135 SetThreadContext (th
->h
, &th
->context
);
138 /* Set the thread context of the thread associated with TH. */
141 win32_prepare_to_resume (windows_thread_info
*th
)
143 if (the_low_target
.prepare_to_resume
!= NULL
)
144 (*the_low_target
.prepare_to_resume
) (th
);
147 /* See win32-low.h. */
150 win32_require_context (windows_thread_info
*th
)
154 if (windows_process
.wow64_process
)
155 context_flags
= th
->wow64_context
.ContextFlags
;
158 context_flags
= th
->context
.ContextFlags
;
159 if (context_flags
== 0)
162 win32_get_thread_context (th
);
166 /* See nat/windows-nat.h. */
168 windows_thread_info
*
169 windows_nat::windows_process_info::thread_rec
170 (ptid_t ptid
, thread_disposition_type disposition
)
172 thread_info
*thread
= find_thread_ptid (ptid
);
176 windows_thread_info
*th
= (windows_thread_info
*) thread_target_data (thread
);
177 if (disposition
!= DONT_INVALIDATE_CONTEXT
)
178 win32_require_context (th
);
182 /* Add a thread to the thread list. */
183 static windows_thread_info
*
184 child_add_thread (DWORD pid
, DWORD tid
, HANDLE h
, void *tlb
)
186 windows_thread_info
*th
;
187 ptid_t ptid
= ptid_t (pid
, tid
, 0);
189 if ((th
= windows_process
.thread_rec (ptid
, DONT_INVALIDATE_CONTEXT
)))
192 CORE_ADDR base
= (CORE_ADDR
) (uintptr_t) tlb
;
194 /* For WOW64 processes, this is actually the pointer to the 64bit TIB,
195 and the 32bit TIB is exactly 2 pages after it. */
196 if (windows_process
.wow64_process
)
197 base
+= 2 * 4096; /* page size = 4096 */
199 th
= new windows_thread_info (tid
, h
, base
);
201 add_thread (ptid
, th
);
203 if (the_low_target
.thread_added
!= NULL
)
204 (*the_low_target
.thread_added
) (th
);
209 /* Delete a thread from the list of threads. */
211 delete_thread_info (thread_info
*thread
)
213 windows_thread_info
*th
= (windows_thread_info
*) thread_target_data (thread
);
215 remove_thread (thread
);
219 /* Delete a thread from the list of threads. */
221 child_delete_thread (DWORD pid
, DWORD tid
)
223 /* If the last thread is exiting, just return. */
224 if (all_threads
.size () == 1)
227 thread_info
*thread
= find_thread_ptid (ptid_t (pid
, tid
));
231 delete_thread_info (thread
);
234 /* These watchpoint related wrapper functions simply pass on the function call
235 if the low target has registered a corresponding function. */
238 win32_process_target::supports_z_point_type (char z_type
)
240 return (z_type
== Z_PACKET_SW_BP
241 || (the_low_target
.supports_z_point_type
!= NULL
242 && the_low_target
.supports_z_point_type (z_type
)));
246 win32_process_target::insert_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
247 int size
, raw_breakpoint
*bp
)
249 if (type
== raw_bkpt_type_sw
)
250 return insert_memory_breakpoint (bp
);
251 else if (the_low_target
.insert_point
!= NULL
)
252 return the_low_target
.insert_point (type
, addr
, size
, bp
);
254 /* Unsupported (see target.h). */
259 win32_process_target::remove_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
260 int size
, raw_breakpoint
*bp
)
262 if (type
== raw_bkpt_type_sw
)
263 return remove_memory_breakpoint (bp
);
264 else if (the_low_target
.remove_point
!= NULL
)
265 return the_low_target
.remove_point (type
, addr
, size
, bp
);
267 /* Unsupported (see target.h). */
272 win32_process_target::stopped_by_watchpoint ()
274 if (the_low_target
.stopped_by_watchpoint
!= NULL
)
275 return the_low_target
.stopped_by_watchpoint ();
281 win32_process_target::stopped_data_address ()
283 if (the_low_target
.stopped_data_address
!= NULL
)
284 return the_low_target
.stopped_data_address ();
290 /* Transfer memory from/to the debugged process. */
292 child_xfer_memory (CORE_ADDR memaddr
, char *our
, int len
,
293 int write
, process_stratum_target
*target
)
298 uintptr_t addr
= (uintptr_t) memaddr
;
302 success
= WriteProcessMemory (windows_process
.handle
, (LPVOID
) addr
,
303 (LPCVOID
) our
, len
, &done
);
305 lasterror
= GetLastError ();
306 FlushInstructionCache (windows_process
.handle
, (LPCVOID
) addr
, len
);
310 success
= ReadProcessMemory (windows_process
.handle
, (LPCVOID
) addr
,
311 (LPVOID
) our
, len
, &done
);
313 lasterror
= GetLastError ();
315 if (!success
&& lasterror
== ERROR_PARTIAL_COPY
&& done
> 0)
318 return success
? done
: -1;
321 /* Clear out any old thread list and reinitialize it to a pristine
324 child_init_thread_list (void)
326 for_each_thread (delete_thread_info
);
329 /* Zero during the child initialization phase, and nonzero otherwise. */
331 static int child_initialization_done
= 0;
334 do_initial_child_stuff (HANDLE proch
, DWORD pid
, int attached
)
336 struct process_info
*proc
;
338 windows_process
.last_sig
= GDB_SIGNAL_0
;
339 windows_process
.handle
= proch
;
340 windows_process
.main_thread_id
= 0;
342 soft_interrupt_requested
= 0;
343 faked_breakpoint
= 0;
344 open_process_used
= true;
346 memset (&windows_process
.current_event
, 0,
347 sizeof (windows_process
.current_event
));
351 if (!IsWow64Process (proch
, &wow64
))
353 DWORD err
= GetLastError ();
354 error ("Check if WOW64 process failed (error %d): %s\n",
355 (int) err
, strwinerror (err
));
357 windows_process
.wow64_process
= wow64
;
359 if (windows_process
.wow64_process
360 && (Wow64GetThreadContext
== nullptr
361 || Wow64SetThreadContext
== nullptr))
362 error ("WOW64 debugging is not supported on this system.\n");
364 windows_process
.ignore_first_breakpoint
365 = !attached
&& windows_process
.wow64_process
;
368 proc
= add_process (pid
, attached
);
370 if (windows_process
.wow64_process
)
371 proc
->tdesc
= wow64_win32_tdesc
;
374 proc
->tdesc
= win32_tdesc
;
375 child_init_thread_list ();
376 child_initialization_done
= 0;
378 if (the_low_target
.initial_stuff
!= NULL
)
379 (*the_low_target
.initial_stuff
) ();
381 cached_status
.set_ignore ();
383 /* Flush all currently pending debug events (thread and dll list) up
384 to the initial breakpoint. */
387 struct target_waitstatus status
;
389 the_target
->wait (minus_one_ptid
, &status
, 0);
391 /* Note win32_wait doesn't return thread events. */
392 if (status
.kind () != TARGET_WAITKIND_LOADED
)
394 cached_status
= status
;
399 struct thread_resume resume
;
401 resume
.thread
= minus_one_ptid
;
402 resume
.kind
= resume_continue
;
405 the_target
->resume (&resume
, 1);
409 /* Now that the inferior has been started and all DLLs have been mapped,
410 we can iterate over all DLLs and load them in.
412 We avoid doing it any earlier because, on certain versions of Windows,
413 LOAD_DLL_DEBUG_EVENTs are sometimes not complete. In particular,
414 we have seen on Windows 8.1 that the ntdll.dll load event does not
415 include the DLL name, preventing us from creating an associated SO.
416 A possible explanation is that ntdll.dll might be mapped before
417 the SO info gets created by the Windows system -- ntdll.dll is
418 the first DLL to be reported via LOAD_DLL_DEBUG_EVENT and other DLLs
419 do not seem to suffer from that problem.
421 Rather than try to work around this sort of issue, it is much
422 simpler to just ignore DLL load/unload events during the startup
423 phase, and then process them all in one batch now. */
424 windows_process
.add_all_dlls ();
426 child_initialization_done
= 1;
429 /* Resume all artificially suspended threads if we are continuing
432 continue_one_thread (thread_info
*thread
, int thread_id
)
434 windows_thread_info
*th
= (windows_thread_info
*) thread_target_data (thread
);
436 if (thread_id
== -1 || thread_id
== th
->tid
)
438 win32_prepare_to_resume (th
);
442 DWORD
*context_flags
;
444 if (windows_process
.wow64_process
)
445 context_flags
= &th
->wow64_context
.ContextFlags
;
448 context_flags
= &th
->context
.ContextFlags
;
451 win32_set_thread_context (th
);
461 child_continue (DWORD continue_status
, int thread_id
)
463 windows_process
.desired_stop_thread_id
= thread_id
;
464 if (windows_process
.matching_pending_stop (debug_threads
))
467 /* The inferior will only continue after the ContinueDebugEvent
469 for_each_thread ([&] (thread_info
*thread
)
471 continue_one_thread (thread
, thread_id
);
473 faked_breakpoint
= 0;
475 return continue_last_debug_event (continue_status
, debug_threads
);
478 /* Fetch register(s) from the current thread context. */
480 child_fetch_inferior_registers (struct regcache
*regcache
, int r
)
483 windows_thread_info
*th
484 = windows_process
.thread_rec (current_thread_ptid (),
486 if (r
== -1 || r
> NUM_REGS
)
487 child_fetch_inferior_registers (regcache
, NUM_REGS
);
489 for (regno
= 0; regno
< r
; regno
++)
490 (*the_low_target
.fetch_inferior_register
) (regcache
, th
, regno
);
493 /* Store a new register value into the current thread context. We don't
494 change the program's context until later, when we resume it. */
496 child_store_inferior_registers (struct regcache
*regcache
, int r
)
499 windows_thread_info
*th
500 = windows_process
.thread_rec (current_thread_ptid (),
502 if (r
== -1 || r
== 0 || r
> NUM_REGS
)
503 child_store_inferior_registers (regcache
, NUM_REGS
);
505 for (regno
= 0; regno
< r
; regno
++)
506 (*the_low_target
.store_inferior_register
) (regcache
, th
, regno
);
509 /* Map the Windows error number in ERROR to a locale-dependent error
510 message string and return a pointer to it. Typically, the values
511 for ERROR come from GetLastError.
513 The string pointed to shall not be modified by the application,
514 but may be overwritten by a subsequent call to strwinerror
516 The strwinerror function does not change the current setting
520 strwinerror (DWORD error
)
522 static char buf
[1024];
524 DWORD lasterr
= GetLastError ();
525 DWORD chars
= FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
526 | FORMAT_MESSAGE_ALLOCATE_BUFFER
,
529 0, /* Default language */
535 /* If there is an \r\n appended, zap it. */
537 && msgbuf
[chars
- 2] == '\r'
538 && msgbuf
[chars
- 1] == '\n')
544 if (chars
> ((COUNTOF (buf
)) - 1))
546 chars
= COUNTOF (buf
) - 1;
551 wcstombs (buf
, msgbuf
, chars
+ 1);
553 strncpy (buf
, msgbuf
, chars
+ 1);
558 sprintf (buf
, "unknown win32 error (%u)", (unsigned) error
);
560 SetLastError (lasterr
);
565 create_process (const char *program
, char *args
,
566 DWORD flags
, PROCESS_INFORMATION
*pi
)
568 const std::string
&inferior_cwd
= get_inferior_cwd ();
570 size_t argslen
, proglen
;
572 proglen
= strlen (program
) + 1;
573 argslen
= strlen (args
) + proglen
;
575 STARTUPINFOA si
= { sizeof (STARTUPINFOA
) };
576 char *program_and_args
= (char *) alloca (argslen
+ 1);
578 strcpy (program_and_args
, program
);
579 strcat (program_and_args
, " ");
580 strcat (program_and_args
, args
);
581 ret
= CreateProcessA (program
, /* image name */
582 program_and_args
, /* command line */
585 TRUE
, /* inherit handles */
586 flags
, /* start flags */
587 NULL
, /* environment */
588 /* current directory */
589 (inferior_cwd
.empty ()
591 : gdb_tilde_expand (inferior_cwd
.c_str ()).c_str()),
592 &si
, /* start info */
598 /* Start a new process.
599 PROGRAM is the program name.
600 PROGRAM_ARGS is the vector containing the inferior's args.
601 Returns the new PID on success, -1 on failure. Registers the new
602 process with the process list. */
604 win32_process_target::create_inferior (const char *program
,
605 const std::vector
<char *> &program_args
)
607 client_state
&cs
= get_client_state ();
609 char real_path
[PATH_MAX
];
610 char *orig_path
, *new_path
, *path_ptr
;
614 PROCESS_INFORMATION pi
;
616 std::string str_program_args
= construct_inferior_arguments (program_args
);
617 char *args
= (char *) str_program_args
.c_str ();
619 /* win32_wait needs to know we're not attaching. */
623 error ("No executable specified, specify executable to debug.\n");
625 flags
= DEBUG_PROCESS
| DEBUG_ONLY_THIS_PROCESS
;
629 path_ptr
= getenv ("PATH");
632 int size
= cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, NULL
, 0);
633 orig_path
= (char *) alloca (strlen (path_ptr
) + 1);
634 new_path
= (char *) alloca (size
);
635 strcpy (orig_path
, path_ptr
);
636 cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, new_path
, size
);
637 setenv ("PATH", new_path
, 1);
639 cygwin_conv_path (CCP_POSIX_TO_WIN_A
, program
, real_path
, PATH_MAX
);
643 OUTMSG2 (("Command line is \"%s %s\"\n", program
, args
));
645 #ifdef CREATE_NEW_PROCESS_GROUP
646 flags
|= CREATE_NEW_PROCESS_GROUP
;
649 ret
= create_process (program
, args
, flags
, &pi
);
650 err
= GetLastError ();
651 if (!ret
&& err
== ERROR_FILE_NOT_FOUND
)
653 char *exename
= (char *) alloca (strlen (program
) + 5);
654 strcat (strcpy (exename
, program
), ".exe");
655 ret
= create_process (exename
, args
, flags
, &pi
);
656 err
= GetLastError ();
661 setenv ("PATH", orig_path
, 1);
666 error ("Error creating process \"%s %s\", (error %d): %s\n",
667 program
, args
, (int) err
, strwinerror (err
));
671 OUTMSG2 (("Process created: %s %s\n", program
, (char *) args
));
674 CloseHandle (pi
.hThread
);
676 do_initial_child_stuff (pi
.hProcess
, pi
.dwProcessId
, 0);
678 /* Wait till we are at 1st instruction in program, return new pid
679 (assuming success). */
680 cs
.last_ptid
= wait (ptid_t (pi
.dwProcessId
), &cs
.last_status
, 0);
682 /* Necessary for handle_v_kill. */
683 signal_pid
= pi
.dwProcessId
;
685 return pi
.dwProcessId
;
688 /* Attach to a running process.
689 PID is the process ID to attach to, specified by the user
690 or a higher layer. */
692 win32_process_target::attach (unsigned long pid
)
697 h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
700 if (DebugActiveProcess (pid
))
702 DebugSetProcessKillOnExit (FALSE
);
704 /* win32_wait needs to know we're attaching. */
706 do_initial_child_stuff (h
, pid
, 1);
713 err
= GetLastError ();
714 error ("Attach to process failed (error %d): %s\n",
715 (int) err
, strwinerror (err
));
718 /* See nat/windows-nat.h. */
721 windows_nat::windows_process_info::handle_output_debug_string
722 (struct target_waitstatus
*ourstatus
)
724 #define READ_BUFFER_LEN 1024
726 char s
[READ_BUFFER_LEN
+ 1] = { 0 };
727 DWORD nbytes
= current_event
.u
.DebugString
.nDebugStringLength
;
732 if (nbytes
> READ_BUFFER_LEN
)
733 nbytes
= READ_BUFFER_LEN
;
735 addr
= (CORE_ADDR
) (size_t) current_event
.u
.DebugString
.lpDebugStringData
;
737 if (current_event
.u
.DebugString
.fUnicode
)
739 /* The event tells us how many bytes, not chars, even
741 WCHAR buffer
[(READ_BUFFER_LEN
+ 1) / sizeof (WCHAR
)] = { 0 };
742 if (read_inferior_memory (addr
, (unsigned char *) buffer
, nbytes
) != 0)
744 wcstombs (s
, buffer
, (nbytes
+ 1) / sizeof (WCHAR
));
748 if (read_inferior_memory (addr
, (unsigned char *) s
, nbytes
) != 0)
752 if (!startswith (s
, "cYg"))
762 #undef READ_BUFFER_LEN
768 win32_clear_inferiors (void)
770 if (open_process_used
)
772 CloseHandle (windows_process
.handle
);
773 open_process_used
= false;
776 for_each_thread (delete_thread_info
);
777 windows_process
.siginfo_er
.ExceptionCode
= 0;
781 /* Implementation of target_ops::kill. */
784 win32_process_target::kill (process_info
*process
)
786 TerminateProcess (windows_process
.handle
, 0);
789 if (!child_continue (DBG_CONTINUE
, -1))
791 if (!wait_for_debug_event (&windows_process
.current_event
, INFINITE
))
793 if (windows_process
.current_event
.dwDebugEventCode
794 == EXIT_PROCESS_DEBUG_EVENT
)
796 else if (windows_process
.current_event
.dwDebugEventCode
797 == OUTPUT_DEBUG_STRING_EVENT
)
798 windows_process
.handle_output_debug_string (nullptr);
801 win32_clear_inferiors ();
803 remove_process (process
);
807 /* Implementation of target_ops::detach. */
810 win32_process_target::detach (process_info
*process
)
812 struct thread_resume resume
;
813 resume
.thread
= minus_one_ptid
;
814 resume
.kind
= resume_continue
;
816 this->resume (&resume
, 1);
818 if (!DebugActiveProcessStop (process
->pid
))
821 DebugSetProcessKillOnExit (FALSE
);
822 remove_process (process
);
824 win32_clear_inferiors ();
829 win32_process_target::mourn (struct process_info
*process
)
831 remove_process (process
);
834 /* Implementation of target_ops::join. */
837 win32_process_target::join (int pid
)
839 HANDLE h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
842 WaitForSingleObject (h
, INFINITE
);
847 /* Return true iff the thread with thread ID TID is alive. */
849 win32_process_target::thread_alive (ptid_t ptid
)
851 /* Our thread list is reliable; don't bother to poll target
853 return find_thread_ptid (ptid
) != NULL
;
856 /* Resume the inferior process. RESUME_INFO describes how we want
859 win32_process_target::resume (thread_resume
*resume_info
, size_t n
)
864 windows_thread_info
*th
;
865 DWORD continue_status
= DBG_CONTINUE
;
868 /* This handles the very limited set of resume packets that GDB can
869 currently produce. */
871 if (n
== 1 && resume_info
[0].thread
== minus_one_ptid
)
876 /* Yes, we're ignoring resume_info[0].thread. It'd be tricky to make
877 the Windows resume code do the right thing for thread switching. */
878 tid
= windows_process
.current_event
.dwThreadId
;
880 if (resume_info
[0].thread
!= minus_one_ptid
)
882 sig
= gdb_signal_from_host (resume_info
[0].sig
);
883 step
= resume_info
[0].kind
== resume_step
;
891 if (sig
!= GDB_SIGNAL_0
)
893 if (windows_process
.current_event
.dwDebugEventCode
894 != EXCEPTION_DEBUG_EVENT
)
896 OUTMSG (("Cannot continue with signal %s here.\n",
897 gdb_signal_to_string (sig
)));
899 else if (sig
== windows_process
.last_sig
)
900 continue_status
= DBG_EXCEPTION_NOT_HANDLED
;
902 OUTMSG (("Can only continue with received signal %s.\n",
903 gdb_signal_to_string (windows_process
.last_sig
)));
906 windows_process
.last_sig
= GDB_SIGNAL_0
;
908 /* Get context for the currently selected thread. */
909 ptid
= debug_event_ptid (&windows_process
.current_event
);
910 th
= windows_process
.thread_rec (ptid
, DONT_INVALIDATE_CONTEXT
);
913 win32_prepare_to_resume (th
);
915 DWORD
*context_flags
;
917 if (windows_process
.wow64_process
)
918 context_flags
= &th
->wow64_context
.ContextFlags
;
921 context_flags
= &th
->context
.ContextFlags
;
924 /* Move register values from the inferior into the thread
925 context structure. */
926 regcache_invalidate ();
930 if (the_low_target
.single_step
!= NULL
)
931 (*the_low_target
.single_step
) (th
);
933 error ("Single stepping is not supported "
934 "in this configuration.\n");
937 win32_set_thread_context (th
);
942 /* Allow continuing with the same signal that interrupted us.
943 Otherwise complain. */
945 child_continue (continue_status
, tid
);
948 /* See nat/windows-nat.h. */
951 windows_nat::windows_process_info::handle_load_dll (const char *name
,
954 CORE_ADDR load_addr
= (CORE_ADDR
) (uintptr_t) base
;
956 char buf
[MAX_PATH
+ 1];
957 char buf2
[MAX_PATH
+ 1];
959 WIN32_FIND_DATAA w32_fd
;
960 HANDLE h
= FindFirstFileA (name
, &w32_fd
);
962 /* The symbols in a dll are offset by 0x1000, which is the
963 offset from 0 of the first byte in an image - because
964 of the file header and the section alignment. */
967 if (h
== INVALID_HANDLE_VALUE
)
974 char cwd
[MAX_PATH
+ 1];
976 if (GetCurrentDirectoryA (MAX_PATH
+ 1, cwd
))
978 p
= strrchr (buf
, '\\');
981 SetCurrentDirectoryA (buf
);
982 GetFullPathNameA (w32_fd
.cFileName
, MAX_PATH
, buf
, &p
);
983 SetCurrentDirectoryA (cwd
);
988 if (strcasecmp (buf
, "ntdll.dll") == 0)
990 GetSystemDirectoryA (buf
, sizeof (buf
));
991 strcat (buf
, "\\ntdll.dll");
995 cygwin_conv_path (CCP_WIN_A_TO_POSIX
, buf
, buf2
, sizeof (buf2
));
1000 loaded_dll (buf2
, load_addr
);
1003 /* See nat/windows-nat.h. */
1006 windows_nat::windows_process_info::handle_unload_dll ()
1008 CORE_ADDR load_addr
=
1009 (CORE_ADDR
) (uintptr_t) current_event
.u
.UnloadDll
.lpBaseOfDll
;
1011 /* The symbols in a dll are offset by 0x1000, which is the
1012 offset from 0 of the first byte in an image - because
1013 of the file header and the section alignment. */
1014 load_addr
+= 0x1000;
1015 unloaded_dll (NULL
, load_addr
);
1019 suspend_one_thread (thread_info
*thread
)
1021 windows_thread_info
*th
= (windows_thread_info
*) thread_target_data (thread
);
1027 fake_breakpoint_event (void)
1029 OUTMSG2(("fake_breakpoint_event\n"));
1031 faked_breakpoint
= 1;
1033 memset (&windows_process
.current_event
, 0,
1034 sizeof (windows_process
.current_event
));
1035 windows_process
.current_event
.dwThreadId
= windows_process
.main_thread_id
;
1036 windows_process
.current_event
.dwDebugEventCode
= EXCEPTION_DEBUG_EVENT
;
1037 windows_process
.current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
1038 = EXCEPTION_BREAKPOINT
;
1040 for_each_thread (suspend_one_thread
);
1043 /* See nat/windows-nat.h. */
1046 windows_nat::windows_process_info::handle_access_violation
1047 (const EXCEPTION_RECORD
*rec
)
1052 /* A helper function that will, if needed, set
1053 'stopped_at_software_breakpoint' on the thread and adjust the
1059 struct regcache
*regcache
= get_thread_regcache (current_thread
, 1);
1060 child_fetch_inferior_registers (regcache
, -1);
1062 windows_thread_info
*th
1063 = windows_process
.thread_rec (current_thread_ptid (),
1064 DONT_INVALIDATE_CONTEXT
);
1065 th
->stopped_at_software_breakpoint
= false;
1067 if (windows_process
.current_event
.dwDebugEventCode
== EXCEPTION_DEBUG_EVENT
1068 && ((windows_process
.current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
1069 == EXCEPTION_BREAKPOINT
)
1070 || (windows_process
.current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
1071 == STATUS_WX86_BREAKPOINT
))
1072 && child_initialization_done
)
1074 th
->stopped_at_software_breakpoint
= true;
1075 CORE_ADDR pc
= regcache_read_pc (regcache
);
1076 CORE_ADDR sw_breakpoint_pc
= pc
- the_low_target
.decr_pc_after_break
;
1077 regcache_write_pc (regcache
, sw_breakpoint_pc
);
1081 /* Get the next event from the child. */
1084 get_child_debug_event (DWORD
*continue_status
,
1085 struct target_waitstatus
*ourstatus
)
1089 windows_process
.last_sig
= GDB_SIGNAL_0
;
1090 ourstatus
->set_spurious ();
1091 *continue_status
= DBG_CONTINUE
;
1093 /* Check if GDB sent us an interrupt request. */
1094 check_remote_input_interrupt_request ();
1096 DEBUG_EVENT
*current_event
= &windows_process
.current_event
;
1098 if (soft_interrupt_requested
)
1100 soft_interrupt_requested
= 0;
1101 fake_breakpoint_event ();
1107 gdb::optional
<pending_stop
> stop
1108 = windows_process
.fetch_pending_stop (debug_threads
);
1109 if (stop
.has_value ())
1111 *ourstatus
= stop
->status
;
1112 windows_process
.current_event
= stop
->event
;
1113 ptid
= debug_event_ptid (&windows_process
.current_event
);
1114 switch_to_thread (find_thread_ptid (ptid
));
1118 /* Keep the wait time low enough for comfortable remote
1119 interruption, but high enough so gdbserver doesn't become a
1121 if (!wait_for_debug_event (&windows_process
.current_event
, 250))
1123 DWORD e
= GetLastError();
1125 if (e
== ERROR_PIPE_NOT_CONNECTED
)
1127 /* This will happen if the loader fails to succesfully
1128 load the application, e.g., if the main executable
1129 tries to pull in a non-existing export from a
1131 ourstatus
->set_exited (1);
1141 switch (current_event
->dwDebugEventCode
)
1143 case CREATE_THREAD_DEBUG_EVENT
:
1144 OUTMSG2 (("gdbserver: kernel event CREATE_THREAD_DEBUG_EVENT "
1145 "for pid=%u tid=%x)\n",
1146 (unsigned) current_event
->dwProcessId
,
1147 (unsigned) current_event
->dwThreadId
));
1149 /* Record the existence of this thread. */
1150 child_add_thread (current_event
->dwProcessId
,
1151 current_event
->dwThreadId
,
1152 current_event
->u
.CreateThread
.hThread
,
1153 current_event
->u
.CreateThread
.lpThreadLocalBase
);
1156 case EXIT_THREAD_DEBUG_EVENT
:
1157 OUTMSG2 (("gdbserver: kernel event EXIT_THREAD_DEBUG_EVENT "
1158 "for pid=%u tid=%x\n",
1159 (unsigned) current_event
->dwProcessId
,
1160 (unsigned) current_event
->dwThreadId
));
1161 child_delete_thread (current_event
->dwProcessId
,
1162 current_event
->dwThreadId
);
1164 switch_to_thread (get_first_thread ());
1167 case CREATE_PROCESS_DEBUG_EVENT
:
1168 OUTMSG2 (("gdbserver: kernel event CREATE_PROCESS_DEBUG_EVENT "
1169 "for pid=%u tid=%x\n",
1170 (unsigned) current_event
->dwProcessId
,
1171 (unsigned) current_event
->dwThreadId
));
1172 CloseHandle (current_event
->u
.CreateProcessInfo
.hFile
);
1174 if (open_process_used
)
1176 CloseHandle (windows_process
.handle
);
1177 open_process_used
= false;
1180 windows_process
.handle
= current_event
->u
.CreateProcessInfo
.hProcess
;
1181 windows_process
.main_thread_id
= current_event
->dwThreadId
;
1183 /* Add the main thread. */
1184 child_add_thread (current_event
->dwProcessId
,
1185 windows_process
.main_thread_id
,
1186 current_event
->u
.CreateProcessInfo
.hThread
,
1187 current_event
->u
.CreateProcessInfo
.lpThreadLocalBase
);
1190 case EXIT_PROCESS_DEBUG_EVENT
:
1191 OUTMSG2 (("gdbserver: kernel event EXIT_PROCESS_DEBUG_EVENT "
1192 "for pid=%u tid=%x\n",
1193 (unsigned) current_event
->dwProcessId
,
1194 (unsigned) current_event
->dwThreadId
));
1196 DWORD exit_status
= current_event
->u
.ExitProcess
.dwExitCode
;
1197 /* If the exit status looks like a fatal exception, but we
1198 don't recognize the exception's code, make the original
1199 exit status value available, to avoid losing information. */
1201 = WIFSIGNALED (exit_status
) ? WTERMSIG (exit_status
) : -1;
1202 if (exit_signal
== -1)
1203 ourstatus
->set_exited (exit_status
);
1205 ourstatus
->set_signalled (gdb_signal_from_host (exit_signal
));
1207 child_continue (DBG_CONTINUE
, windows_process
.desired_stop_thread_id
);
1210 case LOAD_DLL_DEBUG_EVENT
:
1211 OUTMSG2 (("gdbserver: kernel event LOAD_DLL_DEBUG_EVENT "
1212 "for pid=%u tid=%x\n",
1213 (unsigned) current_event
->dwProcessId
,
1214 (unsigned) current_event
->dwThreadId
));
1215 CloseHandle (current_event
->u
.LoadDll
.hFile
);
1216 if (! child_initialization_done
)
1218 windows_process
.dll_loaded_event ();
1220 ourstatus
->set_loaded ();
1223 case UNLOAD_DLL_DEBUG_EVENT
:
1224 OUTMSG2 (("gdbserver: kernel event UNLOAD_DLL_DEBUG_EVENT "
1225 "for pid=%u tid=%x\n",
1226 (unsigned) current_event
->dwProcessId
,
1227 (unsigned) current_event
->dwThreadId
));
1228 if (! child_initialization_done
)
1230 windows_process
.handle_unload_dll ();
1231 ourstatus
->set_loaded ();
1234 case EXCEPTION_DEBUG_EVENT
:
1235 OUTMSG2 (("gdbserver: kernel event EXCEPTION_DEBUG_EVENT "
1236 "for pid=%u tid=%x\n",
1237 (unsigned) current_event
->dwProcessId
,
1238 (unsigned) current_event
->dwThreadId
));
1239 if (windows_process
.handle_exception (ourstatus
, debug_threads
)
1240 == HANDLE_EXCEPTION_UNHANDLED
)
1241 *continue_status
= DBG_EXCEPTION_NOT_HANDLED
;
1244 case OUTPUT_DEBUG_STRING_EVENT
:
1245 /* A message from the kernel (or Cygwin). */
1246 OUTMSG2 (("gdbserver: kernel event OUTPUT_DEBUG_STRING_EVENT "
1247 "for pid=%u tid=%x\n",
1248 (unsigned) current_event
->dwProcessId
,
1249 (unsigned) current_event
->dwThreadId
));
1250 windows_process
.handle_output_debug_string (nullptr);
1254 OUTMSG2 (("gdbserver: kernel event unknown "
1255 "for pid=%u tid=%x code=%x\n",
1256 (unsigned) current_event
->dwProcessId
,
1257 (unsigned) current_event
->dwThreadId
,
1258 (unsigned) current_event
->dwDebugEventCode
));
1262 ptid
= debug_event_ptid (&windows_process
.current_event
);
1264 if (windows_process
.desired_stop_thread_id
!= -1
1265 && windows_process
.desired_stop_thread_id
!= ptid
.lwp ())
1267 /* Pending stop. See the comment by the definition of
1268 "pending_stops" for details on why this is needed. */
1269 OUTMSG2 (("get_windows_debug_event - "
1270 "unexpected stop in 0x%lx (expecting 0x%x)\n",
1271 ptid
.lwp (), windows_process
.desired_stop_thread_id
));
1273 windows_process
.pending_stops
.push_back
1274 ({(DWORD
) ptid
.lwp (), *ourstatus
, *current_event
});
1275 ourstatus
->set_spurious ();
1278 switch_to_thread (find_thread_ptid (ptid
));
1283 /* Wait for the inferior process to change state.
1284 STATUS will be filled in with a response code to send to GDB.
1285 Returns the signal which caused the process to stop. */
1287 win32_process_target::wait (ptid_t ptid
, target_waitstatus
*ourstatus
,
1288 target_wait_flags options
)
1290 if (cached_status
.kind () != TARGET_WAITKIND_IGNORE
)
1292 /* The core always does a wait after creating the inferior, and
1293 do_initial_child_stuff already ran the inferior to the
1294 initial breakpoint (or an exit, if creating the process
1295 fails). Report it now. */
1296 *ourstatus
= cached_status
;
1297 cached_status
.set_ignore ();
1298 return debug_event_ptid (&windows_process
.current_event
);
1303 DWORD continue_status
;
1304 if (!get_child_debug_event (&continue_status
, ourstatus
))
1307 switch (ourstatus
->kind ())
1309 case TARGET_WAITKIND_EXITED
:
1310 OUTMSG2 (("Child exited with retcode = %x\n",
1311 ourstatus
->exit_status ()));
1312 win32_clear_inferiors ();
1313 return ptid_t (windows_process
.current_event
.dwProcessId
);
1314 case TARGET_WAITKIND_STOPPED
:
1315 case TARGET_WAITKIND_SIGNALLED
:
1316 case TARGET_WAITKIND_LOADED
:
1318 OUTMSG2 (("Child Stopped with signal = %d \n",
1319 ourstatus
->sig ()));
1321 return debug_event_ptid (&windows_process
.current_event
);
1324 OUTMSG (("Ignoring unknown internal event, %d\n",
1325 ourstatus
->kind ()));
1327 case TARGET_WAITKIND_SPURIOUS
:
1328 /* do nothing, just continue */
1329 child_continue (continue_status
,
1330 windows_process
.desired_stop_thread_id
);
1336 /* Fetch registers from the inferior process.
1337 If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO. */
1339 win32_process_target::fetch_registers (regcache
*regcache
, int regno
)
1341 child_fetch_inferior_registers (regcache
, regno
);
1344 /* Store registers to the inferior process.
1345 If REGNO is -1, store all registers; otherwise, store at least REGNO. */
1347 win32_process_target::store_registers (regcache
*regcache
, int regno
)
1349 child_store_inferior_registers (regcache
, regno
);
1352 /* Read memory from the inferior process. This should generally be
1353 called through read_inferior_memory, which handles breakpoint shadowing.
1354 Read LEN bytes at MEMADDR into a buffer at MYADDR. */
1356 win32_process_target::read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
,
1359 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 0, 0) != len
;
1362 /* Write memory to the inferior process. This should generally be
1363 called through write_inferior_memory, which handles breakpoint shadowing.
1364 Write LEN bytes from the buffer at MYADDR to MEMADDR.
1365 Returns 0 on success and errno on failure. */
1367 win32_process_target::write_memory (CORE_ADDR memaddr
,
1368 const unsigned char *myaddr
, int len
)
1370 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 1, 0) != len
;
1373 /* Send an interrupt request to the inferior process. */
1375 win32_process_target::request_interrupt ()
1377 if (GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT
, signal_pid
))
1380 /* GenerateConsoleCtrlEvent can fail if process id being debugged is
1381 not a process group id.
1382 Fallback to XP/Vista 'DebugBreakProcess', which generates a
1383 breakpoint exception in the interior process. */
1385 if (DebugBreakProcess (windows_process
.handle
))
1388 /* Last resort, suspend all threads manually. */
1389 soft_interrupt_requested
= 1;
1393 win32_process_target::supports_hardware_single_step ()
1399 win32_process_target::supports_qxfer_siginfo ()
1404 /* Write Windows signal info. */
1407 win32_process_target::qxfer_siginfo (const char *annex
,
1408 unsigned char *readbuf
,
1409 unsigned const char *writebuf
,
1410 CORE_ADDR offset
, int len
)
1412 if (windows_process
.siginfo_er
.ExceptionCode
== 0)
1415 if (readbuf
== nullptr)
1418 char *buf
= (char *) &windows_process
.siginfo_er
;
1419 size_t bufsize
= sizeof (windows_process
.siginfo_er
);
1422 EXCEPTION_RECORD32 er32
;
1423 if (windows_process
.wow64_process
)
1425 buf
= (char *) &er32
;
1426 bufsize
= sizeof (er32
);
1428 er32
.ExceptionCode
= windows_process
.siginfo_er
.ExceptionCode
;
1429 er32
.ExceptionFlags
= windows_process
.siginfo_er
.ExceptionFlags
;
1430 er32
.ExceptionRecord
1431 = (uintptr_t) windows_process
.siginfo_er
.ExceptionRecord
;
1432 er32
.ExceptionAddress
1433 = (uintptr_t) windows_process
.siginfo_er
.ExceptionAddress
;
1434 er32
.NumberParameters
= windows_process
.siginfo_er
.NumberParameters
;
1436 for (i
= 0; i
< EXCEPTION_MAXIMUM_PARAMETERS
; i
++)
1437 er32
.ExceptionInformation
[i
]
1438 = windows_process
.siginfo_er
.ExceptionInformation
[i
];
1442 if (offset
> bufsize
)
1445 if (offset
+ len
> bufsize
)
1446 len
= bufsize
- offset
;
1448 memcpy (readbuf
, buf
+ offset
, len
);
1454 win32_process_target::supports_get_tib_address ()
1459 /* Write Windows OS Thread Information Block address. */
1462 win32_process_target::get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
)
1464 windows_thread_info
*th
;
1465 th
= windows_process
.thread_rec (ptid
, DONT_INVALIDATE_CONTEXT
);
1469 *addr
= th
->thread_local_base
;
1473 /* Implementation of the target_ops method "sw_breakpoint_from_kind". */
1476 win32_process_target::sw_breakpoint_from_kind (int kind
, int *size
)
1478 *size
= the_low_target
.breakpoint_len
;
1479 return the_low_target
.breakpoint
;
1483 win32_process_target::stopped_by_sw_breakpoint ()
1485 windows_thread_info
*th
1486 = windows_process
.thread_rec (current_thread_ptid (),
1487 DONT_INVALIDATE_CONTEXT
);
1488 return th
== nullptr ? false : th
->stopped_at_software_breakpoint
;
1492 win32_process_target::supports_stopped_by_sw_breakpoint ()
1498 win32_process_target::read_pc (struct regcache
*regcache
)
1500 return (*the_low_target
.get_pc
) (regcache
);
1504 win32_process_target::write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
1506 return (*the_low_target
.set_pc
) (regcache
, pc
);
1510 win32_process_target::thread_name (ptid_t thread
)
1512 windows_thread_info
*th
1513 = windows_process
.thread_rec (current_thread_ptid (),
1514 DONT_INVALIDATE_CONTEXT
);
1515 return th
->thread_name ();
1518 /* The win32 target ops object. */
1520 static win32_process_target the_win32_target
;
1522 /* Initialize the Win32 backend. */
1524 initialize_low (void)
1526 set_target_ops (&the_win32_target
);
1527 the_low_target
.arch_setup ();
1529 initialize_loadable ();