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 const struct target_desc
*win32_tdesc
;
93 #define NUM_REGS (the_low_target.num_regs)
95 typedef BOOL (WINAPI
*winapi_DebugActiveProcessStop
) (DWORD dwProcessId
);
96 typedef BOOL (WINAPI
*winapi_DebugSetProcessKillOnExit
) (BOOL KillOnExit
);
97 typedef BOOL (WINAPI
*winapi_DebugBreakProcess
) (HANDLE
);
98 typedef BOOL (WINAPI
*winapi_GenerateConsoleCtrlEvent
) (DWORD
, DWORD
);
101 static void win32_add_all_dlls (void);
104 /* Get the thread ID from the current selected inferior (the current
107 current_thread_ptid (void)
112 /* The current debug event from WaitForDebugEvent. */
114 debug_event_ptid (DEBUG_EVENT
*event
)
116 return ptid_t (event
->dwProcessId
, event
->dwThreadId
, 0);
119 /* Get the thread context of the thread associated with TH. */
122 win32_get_thread_context (windows_thread_info
*th
)
124 memset (&th
->context
, 0, sizeof (CONTEXT
));
125 (*the_low_target
.get_thread_context
) (th
);
127 memcpy (&th
->base_context
, &th
->context
, sizeof (CONTEXT
));
131 /* Set the thread context of the thread associated with TH. */
134 win32_set_thread_context (windows_thread_info
*th
)
137 /* Calling SuspendThread on a thread that is running kernel code
138 will report that the suspending was successful, but in fact, that
139 will often not be true. In those cases, the context returned by
140 GetThreadContext will not be correct by the time the thread
141 stops, hence we can't set that context back into the thread when
142 resuming - it will most likely crash the inferior.
143 Unfortunately, there is no way to know when the thread will
144 really stop. To work around it, we'll only write the context
145 back to the thread when either the user or GDB explicitly change
146 it between stopping and resuming. */
147 if (memcmp (&th
->context
, &th
->base_context
, sizeof (CONTEXT
)) != 0)
149 SetThreadContext (th
->h
, &th
->context
);
152 /* Set the thread context of the thread associated with TH. */
155 win32_prepare_to_resume (windows_thread_info
*th
)
157 if (the_low_target
.prepare_to_resume
!= NULL
)
158 (*the_low_target
.prepare_to_resume
) (th
);
161 /* See win32-low.h. */
164 win32_require_context (windows_thread_info
*th
)
166 if (th
->context
.ContextFlags
== 0)
169 win32_get_thread_context (th
);
173 /* See nat/windows-nat.h. */
175 windows_thread_info
*
176 windows_nat::thread_rec (ptid_t ptid
, thread_disposition_type disposition
)
178 thread_info
*thread
= find_thread_ptid (ptid
);
182 windows_thread_info
*th
= (windows_thread_info
*) thread_target_data (thread
);
183 if (disposition
!= DONT_INVALIDATE_CONTEXT
)
184 win32_require_context (th
);
188 /* Add a thread to the thread list. */
189 static windows_thread_info
*
190 child_add_thread (DWORD pid
, DWORD tid
, HANDLE h
, void *tlb
)
192 windows_thread_info
*th
;
193 ptid_t ptid
= ptid_t (pid
, tid
, 0);
195 if ((th
= thread_rec (ptid
, DONT_INVALIDATE_CONTEXT
)))
198 th
= new windows_thread_info (tid
, h
, (CORE_ADDR
) (uintptr_t) tlb
);
200 add_thread (ptid
, th
);
202 if (the_low_target
.thread_added
!= NULL
)
203 (*the_low_target
.thread_added
) (th
);
208 /* Delete a thread from the list of threads. */
210 delete_thread_info (thread_info
*thread
)
212 windows_thread_info
*th
= (windows_thread_info
*) thread_target_data (thread
);
214 remove_thread (thread
);
218 /* Delete a thread from the list of threads. */
220 child_delete_thread (DWORD pid
, DWORD tid
)
222 /* If the last thread is exiting, just return. */
223 if (all_threads
.size () == 1)
226 thread_info
*thread
= find_thread_ptid (ptid_t (pid
, tid
));
230 delete_thread_info (thread
);
233 /* These watchpoint related wrapper functions simply pass on the function call
234 if the low target has registered a corresponding function. */
237 win32_process_target::supports_z_point_type (char z_type
)
239 return (the_low_target
.supports_z_point_type
!= NULL
240 && the_low_target
.supports_z_point_type (z_type
));
244 win32_process_target::insert_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
245 int size
, raw_breakpoint
*bp
)
247 if (the_low_target
.insert_point
!= NULL
)
248 return the_low_target
.insert_point (type
, addr
, size
, bp
);
250 /* Unsupported (see target.h). */
255 win32_process_target::remove_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
256 int size
, raw_breakpoint
*bp
)
258 if (the_low_target
.remove_point
!= NULL
)
259 return the_low_target
.remove_point (type
, addr
, size
, bp
);
261 /* Unsupported (see target.h). */
266 win32_process_target::stopped_by_watchpoint ()
268 if (the_low_target
.stopped_by_watchpoint
!= NULL
)
269 return the_low_target
.stopped_by_watchpoint ();
275 win32_process_target::stopped_data_address ()
277 if (the_low_target
.stopped_data_address
!= NULL
)
278 return the_low_target
.stopped_data_address ();
284 /* Transfer memory from/to the debugged process. */
286 child_xfer_memory (CORE_ADDR memaddr
, char *our
, int len
,
287 int write
, process_stratum_target
*target
)
292 uintptr_t addr
= (uintptr_t) memaddr
;
296 success
= WriteProcessMemory (current_process_handle
, (LPVOID
) addr
,
297 (LPCVOID
) our
, len
, &done
);
299 lasterror
= GetLastError ();
300 FlushInstructionCache (current_process_handle
, (LPCVOID
) addr
, len
);
304 success
= ReadProcessMemory (current_process_handle
, (LPCVOID
) addr
,
305 (LPVOID
) our
, len
, &done
);
307 lasterror
= GetLastError ();
309 if (!success
&& lasterror
== ERROR_PARTIAL_COPY
&& done
> 0)
312 return success
? done
: -1;
315 /* Clear out any old thread list and reinitialize it to a pristine
318 child_init_thread_list (void)
320 for_each_thread (delete_thread_info
);
323 /* Zero during the child initialization phase, and nonzero otherwise. */
325 static int child_initialization_done
= 0;
328 do_initial_child_stuff (HANDLE proch
, DWORD pid
, int attached
)
330 struct process_info
*proc
;
332 last_sig
= GDB_SIGNAL_0
;
334 current_process_handle
= proch
;
335 current_process_id
= pid
;
338 soft_interrupt_requested
= 0;
339 faked_breakpoint
= 0;
341 memset (¤t_event
, 0, sizeof (current_event
));
343 proc
= add_process (pid
, attached
);
344 proc
->tdesc
= win32_tdesc
;
345 child_init_thread_list ();
346 child_initialization_done
= 0;
348 if (the_low_target
.initial_stuff
!= NULL
)
349 (*the_low_target
.initial_stuff
) ();
351 cached_status
.kind
= TARGET_WAITKIND_IGNORE
;
353 /* Flush all currently pending debug events (thread and dll list) up
354 to the initial breakpoint. */
357 struct target_waitstatus status
;
359 the_target
->wait (minus_one_ptid
, &status
, 0);
361 /* Note win32_wait doesn't return thread events. */
362 if (status
.kind
!= TARGET_WAITKIND_LOADED
)
364 cached_status
= status
;
369 struct thread_resume resume
;
371 resume
.thread
= minus_one_ptid
;
372 resume
.kind
= resume_continue
;
375 the_target
->resume (&resume
, 1);
380 /* Now that the inferior has been started and all DLLs have been mapped,
381 we can iterate over all DLLs and load them in.
383 We avoid doing it any earlier because, on certain versions of Windows,
384 LOAD_DLL_DEBUG_EVENTs are sometimes not complete. In particular,
385 we have seen on Windows 8.1 that the ntdll.dll load event does not
386 include the DLL name, preventing us from creating an associated SO.
387 A possible explanation is that ntdll.dll might be mapped before
388 the SO info gets created by the Windows system -- ntdll.dll is
389 the first DLL to be reported via LOAD_DLL_DEBUG_EVENT and other DLLs
390 do not seem to suffer from that problem.
392 Rather than try to work around this sort of issue, it is much
393 simpler to just ignore DLL load/unload events during the startup
394 phase, and then process them all in one batch now. */
395 win32_add_all_dlls ();
398 child_initialization_done
= 1;
401 /* Resume all artificially suspended threads if we are continuing
404 continue_one_thread (thread_info
*thread
, int thread_id
)
406 windows_thread_info
*th
= (windows_thread_info
*) thread_target_data (thread
);
408 if (thread_id
== -1 || thread_id
== th
->tid
)
410 win32_prepare_to_resume (th
);
414 if (th
->context
.ContextFlags
)
416 win32_set_thread_context (th
);
417 th
->context
.ContextFlags
= 0;
426 child_continue (DWORD continue_status
, int thread_id
)
428 /* The inferior will only continue after the ContinueDebugEvent
430 for_each_thread ([&] (thread_info
*thread
)
432 continue_one_thread (thread
, thread_id
);
434 faked_breakpoint
= 0;
436 if (!ContinueDebugEvent (current_event
.dwProcessId
,
437 current_event
.dwThreadId
,
444 /* Fetch register(s) from the current thread context. */
446 child_fetch_inferior_registers (struct regcache
*regcache
, int r
)
449 windows_thread_info
*th
= thread_rec (current_thread_ptid (),
451 if (r
== -1 || r
> NUM_REGS
)
452 child_fetch_inferior_registers (regcache
, NUM_REGS
);
454 for (regno
= 0; regno
< r
; regno
++)
455 (*the_low_target
.fetch_inferior_register
) (regcache
, th
, regno
);
458 /* Store a new register value into the current thread context. We don't
459 change the program's context until later, when we resume it. */
461 child_store_inferior_registers (struct regcache
*regcache
, int r
)
464 windows_thread_info
*th
= thread_rec (current_thread_ptid (),
466 if (r
== -1 || r
== 0 || r
> NUM_REGS
)
467 child_store_inferior_registers (regcache
, NUM_REGS
);
469 for (regno
= 0; regno
< r
; regno
++)
470 (*the_low_target
.store_inferior_register
) (regcache
, th
, regno
);
473 /* Map the Windows error number in ERROR to a locale-dependent error
474 message string and return a pointer to it. Typically, the values
475 for ERROR come from GetLastError.
477 The string pointed to shall not be modified by the application,
478 but may be overwritten by a subsequent call to strwinerror
480 The strwinerror function does not change the current setting
484 strwinerror (DWORD error
)
486 static char buf
[1024];
488 DWORD lasterr
= GetLastError ();
489 DWORD chars
= FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
490 | FORMAT_MESSAGE_ALLOCATE_BUFFER
,
493 0, /* Default language */
499 /* If there is an \r\n appended, zap it. */
501 && msgbuf
[chars
- 2] == '\r'
502 && msgbuf
[chars
- 1] == '\n')
508 if (chars
> ((COUNTOF (buf
)) - 1))
510 chars
= COUNTOF (buf
) - 1;
515 wcstombs (buf
, msgbuf
, chars
+ 1);
517 strncpy (buf
, msgbuf
, chars
+ 1);
522 sprintf (buf
, "unknown win32 error (%u)", (unsigned) error
);
524 SetLastError (lasterr
);
529 create_process (const char *program
, char *args
,
530 DWORD flags
, PROCESS_INFORMATION
*pi
)
532 const char *inferior_cwd
= get_inferior_cwd ();
534 size_t argslen
, proglen
;
536 proglen
= strlen (program
) + 1;
537 argslen
= strlen (args
) + proglen
;
540 wchar_t *p
, *wprogram
, *wargs
, *wcwd
= NULL
;
542 wprogram
= (wchar_t *) alloca (proglen
* sizeof (wchar_t));
543 mbstowcs (wprogram
, program
, proglen
);
545 for (p
= wprogram
; *p
; ++p
)
549 wargs
= alloca ((argslen
+ 1) * sizeof (wchar_t));
550 wcscpy (wargs
, wprogram
);
551 wcscat (wargs
, L
" ");
552 mbstowcs (wargs
+ proglen
, args
, argslen
+ 1 - proglen
);
554 if (inferior_cwd
!= NULL
)
556 std::string expanded_infcwd
= gdb_tilde_expand (inferior_cwd
);
557 std::replace (expanded_infcwd
.begin (), expanded_infcwd
.end (),
559 wcwd
= alloca ((expanded_infcwd
.size () + 1) * sizeof (wchar_t));
560 if (mbstowcs (wcwd
, expanded_infcwd
.c_str (),
561 expanded_infcwd
.size () + 1) == NULL
)
564 Could not convert the expanded inferior cwd to wide-char."));
568 ret
= CreateProcessW (wprogram
, /* image name */
569 wargs
, /* command line */
570 NULL
, /* security, not supported */
571 NULL
, /* thread, not supported */
572 FALSE
, /* inherit handles, not supported */
573 flags
, /* start flags */
574 NULL
, /* environment, not supported */
575 wcwd
, /* current directory */
576 NULL
, /* start info, not supported */
579 STARTUPINFOA si
= { sizeof (STARTUPINFOA
) };
580 char *program_and_args
= (char *) alloca (argslen
+ 1);
582 strcpy (program_and_args
, program
);
583 strcat (program_and_args
, " ");
584 strcat (program_and_args
, args
);
585 ret
= CreateProcessA (program
, /* image name */
586 program_and_args
, /* command line */
589 TRUE
, /* inherit handles */
590 flags
, /* start flags */
591 NULL
, /* environment */
592 /* current directory */
593 (inferior_cwd
== NULL
595 : gdb_tilde_expand (inferior_cwd
).c_str()),
596 &si
, /* start info */
603 /* Start a new process.
604 PROGRAM is the program name.
605 PROGRAM_ARGS is the vector containing the inferior's args.
606 Returns the new PID on success, -1 on failure. Registers the new
607 process with the process list. */
609 win32_process_target::create_inferior (const char *program
,
610 const std::vector
<char *> &program_args
)
612 client_state
&cs
= get_client_state ();
614 char real_path
[PATH_MAX
];
615 char *orig_path
, *new_path
, *path_ptr
;
619 PROCESS_INFORMATION pi
;
621 std::string str_program_args
= stringify_argv (program_args
);
622 char *args
= (char *) str_program_args
.c_str ();
624 /* win32_wait needs to know we're not attaching. */
628 error ("No executable specified, specify executable to debug.\n");
630 flags
= DEBUG_PROCESS
| DEBUG_ONLY_THIS_PROCESS
;
634 path_ptr
= getenv ("PATH");
637 int size
= cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, NULL
, 0);
638 orig_path
= (char *) alloca (strlen (path_ptr
) + 1);
639 new_path
= (char *) alloca (size
);
640 strcpy (orig_path
, path_ptr
);
641 cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, new_path
, size
);
642 setenv ("PATH", new_path
, 1);
644 cygwin_conv_path (CCP_POSIX_TO_WIN_A
, program
, real_path
, PATH_MAX
);
648 OUTMSG2 (("Command line is \"%s %s\"\n", program
, args
));
650 #ifdef CREATE_NEW_PROCESS_GROUP
651 flags
|= CREATE_NEW_PROCESS_GROUP
;
654 ret
= create_process (program
, args
, flags
, &pi
);
655 err
= GetLastError ();
656 if (!ret
&& err
== ERROR_FILE_NOT_FOUND
)
658 char *exename
= (char *) alloca (strlen (program
) + 5);
659 strcat (strcpy (exename
, program
), ".exe");
660 ret
= create_process (exename
, args
, flags
, &pi
);
661 err
= GetLastError ();
666 setenv ("PATH", orig_path
, 1);
671 error ("Error creating process \"%s %s\", (error %d): %s\n",
672 program
, args
, (int) err
, strwinerror (err
));
676 OUTMSG2 (("Process created: %s %s\n", program
, (char *) args
));
680 /* On Windows CE this handle can't be closed. The OS reuses
681 it in the debug events, while the 9x/NT versions of Windows
682 probably use a DuplicateHandle'd one. */
683 CloseHandle (pi
.hThread
);
686 do_initial_child_stuff (pi
.hProcess
, pi
.dwProcessId
, 0);
688 /* Wait till we are at 1st instruction in program, return new pid
689 (assuming success). */
690 cs
.last_ptid
= wait (ptid_t (current_process_id
), &cs
.last_status
, 0);
692 /* Necessary for handle_v_kill. */
693 signal_pid
= current_process_id
;
695 return current_process_id
;
698 /* Attach to a running process.
699 PID is the process ID to attach to, specified by the user
700 or a higher layer. */
702 win32_process_target::attach (unsigned long pid
)
705 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
708 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
710 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
712 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
714 h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
717 if (DebugActiveProcess (pid
))
719 if (DebugSetProcessKillOnExit
!= NULL
)
720 DebugSetProcessKillOnExit (FALSE
);
722 /* win32_wait needs to know we're attaching. */
724 do_initial_child_stuff (h
, pid
, 1);
731 err
= GetLastError ();
732 error ("Attach to process failed (error %d): %s\n",
733 (int) err
, strwinerror (err
));
736 /* See nat/windows-nat.h. */
739 windows_nat::handle_output_debug_string (struct target_waitstatus
*ourstatus
)
741 #define READ_BUFFER_LEN 1024
743 char s
[READ_BUFFER_LEN
+ 1] = { 0 };
744 DWORD nbytes
= current_event
.u
.DebugString
.nDebugStringLength
;
749 if (nbytes
> READ_BUFFER_LEN
)
750 nbytes
= READ_BUFFER_LEN
;
752 addr
= (CORE_ADDR
) (size_t) current_event
.u
.DebugString
.lpDebugStringData
;
754 if (current_event
.u
.DebugString
.fUnicode
)
756 /* The event tells us how many bytes, not chars, even
758 WCHAR buffer
[(READ_BUFFER_LEN
+ 1) / sizeof (WCHAR
)] = { 0 };
759 if (read_inferior_memory (addr
, (unsigned char *) buffer
, nbytes
) != 0)
761 wcstombs (s
, buffer
, (nbytes
+ 1) / sizeof (WCHAR
));
765 if (read_inferior_memory (addr
, (unsigned char *) s
, nbytes
) != 0)
769 if (!startswith (s
, "cYg"))
779 #undef READ_BUFFER_LEN
785 win32_clear_inferiors (void)
787 if (current_process_handle
!= NULL
)
788 CloseHandle (current_process_handle
);
790 for_each_thread (delete_thread_info
);
791 siginfo_er
.ExceptionCode
= 0;
795 /* Implementation of target_ops::kill. */
798 win32_process_target::kill (process_info
*process
)
800 TerminateProcess (current_process_handle
, 0);
803 if (!child_continue (DBG_CONTINUE
, -1))
805 if (!WaitForDebugEvent (¤t_event
, INFINITE
))
807 if (current_event
.dwDebugEventCode
== EXIT_PROCESS_DEBUG_EVENT
)
809 else if (current_event
.dwDebugEventCode
== OUTPUT_DEBUG_STRING_EVENT
)
810 handle_output_debug_string (nullptr);
813 win32_clear_inferiors ();
815 remove_process (process
);
819 /* Implementation of target_ops::detach. */
822 win32_process_target::detach (process_info
*process
)
824 winapi_DebugActiveProcessStop DebugActiveProcessStop
= NULL
;
825 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
827 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
829 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
831 DebugActiveProcessStop
= GETPROCADDRESS (dll
, DebugActiveProcessStop
);
832 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
834 if (DebugSetProcessKillOnExit
== NULL
835 || DebugActiveProcessStop
== NULL
)
839 struct thread_resume resume
;
840 resume
.thread
= minus_one_ptid
;
841 resume
.kind
= resume_continue
;
843 this->resume (&resume
, 1);
846 if (!DebugActiveProcessStop (current_process_id
))
849 DebugSetProcessKillOnExit (FALSE
);
850 remove_process (process
);
852 win32_clear_inferiors ();
857 win32_process_target::mourn (struct process_info
*process
)
859 remove_process (process
);
862 /* Implementation of target_ops::join. */
865 win32_process_target::join (int pid
)
867 HANDLE h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
870 WaitForSingleObject (h
, INFINITE
);
875 /* Return true iff the thread with thread ID TID is alive. */
877 win32_process_target::thread_alive (ptid_t ptid
)
879 /* Our thread list is reliable; don't bother to poll target
881 return find_thread_ptid (ptid
) != NULL
;
884 /* Resume the inferior process. RESUME_INFO describes how we want
887 win32_process_target::resume (thread_resume
*resume_info
, size_t n
)
892 windows_thread_info
*th
;
893 DWORD continue_status
= DBG_CONTINUE
;
896 /* This handles the very limited set of resume packets that GDB can
897 currently produce. */
899 if (n
== 1 && resume_info
[0].thread
== minus_one_ptid
)
904 /* Yes, we're ignoring resume_info[0].thread. It'd be tricky to make
905 the Windows resume code do the right thing for thread switching. */
906 tid
= current_event
.dwThreadId
;
908 if (resume_info
[0].thread
!= minus_one_ptid
)
910 sig
= gdb_signal_from_host (resume_info
[0].sig
);
911 step
= resume_info
[0].kind
== resume_step
;
919 if (sig
!= GDB_SIGNAL_0
)
921 if (current_event
.dwDebugEventCode
!= EXCEPTION_DEBUG_EVENT
)
923 OUTMSG (("Cannot continue with signal %s here.\n",
924 gdb_signal_to_string (sig
)));
926 else if (sig
== last_sig
)
927 continue_status
= DBG_EXCEPTION_NOT_HANDLED
;
929 OUTMSG (("Can only continue with received signal %s.\n",
930 gdb_signal_to_string (last_sig
)));
933 last_sig
= GDB_SIGNAL_0
;
935 /* Get context for the currently selected thread. */
936 ptid
= debug_event_ptid (¤t_event
);
937 th
= thread_rec (ptid
, DONT_INVALIDATE_CONTEXT
);
940 win32_prepare_to_resume (th
);
942 if (th
->context
.ContextFlags
)
944 /* Move register values from the inferior into the thread
945 context structure. */
946 regcache_invalidate ();
950 if (the_low_target
.single_step
!= NULL
)
951 (*the_low_target
.single_step
) (th
);
953 error ("Single stepping is not supported "
954 "in this configuration.\n");
957 win32_set_thread_context (th
);
958 th
->context
.ContextFlags
= 0;
962 /* Allow continuing with the same signal that interrupted us.
963 Otherwise complain. */
965 child_continue (continue_status
, tid
);
969 win32_add_one_solib (const char *name
, CORE_ADDR load_addr
)
971 char buf
[MAX_PATH
+ 1];
972 char buf2
[MAX_PATH
+ 1];
975 WIN32_FIND_DATA w32_fd
;
976 WCHAR wname
[MAX_PATH
+ 1];
977 mbstowcs (wname
, name
, MAX_PATH
);
978 HANDLE h
= FindFirstFile (wname
, &w32_fd
);
980 WIN32_FIND_DATAA w32_fd
;
981 HANDLE h
= FindFirstFileA (name
, &w32_fd
);
984 /* The symbols in a dll are offset by 0x1000, which is the
985 offset from 0 of the first byte in an image - because
986 of the file header and the section alignment. */
989 if (h
== INVALID_HANDLE_VALUE
)
997 char cwd
[MAX_PATH
+ 1];
999 if (GetCurrentDirectoryA (MAX_PATH
+ 1, cwd
))
1001 p
= strrchr (buf
, '\\');
1004 SetCurrentDirectoryA (buf
);
1005 GetFullPathNameA (w32_fd
.cFileName
, MAX_PATH
, buf
, &p
);
1006 SetCurrentDirectoryA (cwd
);
1013 if (strcasecmp (buf
, "ntdll.dll") == 0)
1015 GetSystemDirectoryA (buf
, sizeof (buf
));
1016 strcat (buf
, "\\ntdll.dll");
1021 cygwin_conv_path (CCP_WIN_A_TO_POSIX
, buf
, buf2
, sizeof (buf2
));
1026 loaded_dll (buf2
, load_addr
);
1029 typedef BOOL (WINAPI
*winapi_EnumProcessModules
) (HANDLE
, HMODULE
*,
1031 typedef BOOL (WINAPI
*winapi_GetModuleInformation
) (HANDLE
, HMODULE
,
1032 LPMODULEINFO
, DWORD
);
1033 typedef DWORD (WINAPI
*winapi_GetModuleFileNameExA
) (HANDLE
, HMODULE
,
1036 static winapi_EnumProcessModules win32_EnumProcessModules
;
1037 static winapi_GetModuleInformation win32_GetModuleInformation
;
1038 static winapi_GetModuleFileNameExA win32_GetModuleFileNameExA
;
1043 static int psapi_loaded
= 0;
1044 static HMODULE dll
= NULL
;
1049 dll
= LoadLibrary (TEXT("psapi.dll"));
1052 win32_EnumProcessModules
=
1053 GETPROCADDRESS (dll
, EnumProcessModules
);
1054 win32_GetModuleInformation
=
1055 GETPROCADDRESS (dll
, GetModuleInformation
);
1056 win32_GetModuleFileNameExA
=
1057 GETPROCADDRESS (dll
, GetModuleFileNameExA
);
1060 return (win32_EnumProcessModules
!= NULL
1061 && win32_GetModuleInformation
!= NULL
1062 && win32_GetModuleFileNameExA
!= NULL
);
1067 /* Iterate over all DLLs currently mapped by our inferior, and
1068 add them to our list of solibs. */
1071 win32_add_all_dlls (void)
1075 HMODULE
*DllHandle
= dh_buf
;
1083 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1088 if (!ok
|| !cbNeeded
)
1091 DllHandle
= (HMODULE
*) alloca (cbNeeded
);
1095 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1102 for (i
= 1; i
< ((size_t) cbNeeded
/ sizeof (HMODULE
)); i
++)
1105 char dll_name
[MAX_PATH
];
1107 if (!(*win32_GetModuleInformation
) (current_process_handle
,
1112 if ((*win32_GetModuleFileNameExA
) (current_process_handle
,
1117 win32_add_one_solib (dll_name
, (CORE_ADDR
) (uintptr_t) mi
.lpBaseOfDll
);
1122 typedef HANDLE (WINAPI
*winapi_CreateToolhelp32Snapshot
) (DWORD
, DWORD
);
1123 typedef BOOL (WINAPI
*winapi_Module32First
) (HANDLE
, LPMODULEENTRY32
);
1124 typedef BOOL (WINAPI
*winapi_Module32Next
) (HANDLE
, LPMODULEENTRY32
);
1126 /* Handle a DLL load event.
1128 This function assumes that this event did not occur during inferior
1129 initialization, where their event info may be incomplete (see
1130 do_initial_child_stuff and win32_add_all_dlls for more info on
1131 how we handle DLL loading during that phase). */
1134 handle_load_dll (void)
1136 LOAD_DLL_DEBUG_INFO
*event
= ¤t_event
.u
.LoadDll
;
1137 const char *dll_name
;
1139 dll_name
= get_image_name (current_process_handle
,
1140 event
->lpImageName
, event
->fUnicode
);
1144 win32_add_one_solib (dll_name
, (CORE_ADDR
) (uintptr_t) event
->lpBaseOfDll
);
1147 /* Handle a DLL unload event.
1149 This function assumes that this event did not occur during inferior
1150 initialization, where their event info may be incomplete (see
1151 do_initial_child_stuff and win32_add_one_solib for more info
1152 on how we handle DLL loading during that phase). */
1155 handle_unload_dll (void)
1157 CORE_ADDR load_addr
=
1158 (CORE_ADDR
) (uintptr_t) current_event
.u
.UnloadDll
.lpBaseOfDll
;
1160 /* The symbols in a dll are offset by 0x1000, which is the
1161 offset from 0 of the first byte in an image - because
1162 of the file header and the section alignment. */
1163 load_addr
+= 0x1000;
1164 unloaded_dll (NULL
, load_addr
);
1168 handle_exception (struct target_waitstatus
*ourstatus
)
1170 DWORD code
= current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
;
1172 memcpy (&siginfo_er
, ¤t_event
.u
.Exception
.ExceptionRecord
,
1175 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
1179 case EXCEPTION_ACCESS_VIOLATION
:
1180 OUTMSG2 (("EXCEPTION_ACCESS_VIOLATION"));
1181 ourstatus
->value
.sig
= GDB_SIGNAL_SEGV
;
1183 case STATUS_STACK_OVERFLOW
:
1184 OUTMSG2 (("STATUS_STACK_OVERFLOW"));
1185 ourstatus
->value
.sig
= GDB_SIGNAL_SEGV
;
1187 case STATUS_FLOAT_DENORMAL_OPERAND
:
1188 OUTMSG2 (("STATUS_FLOAT_DENORMAL_OPERAND"));
1189 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1191 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
1192 OUTMSG2 (("EXCEPTION_ARRAY_BOUNDS_EXCEEDED"));
1193 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1195 case STATUS_FLOAT_INEXACT_RESULT
:
1196 OUTMSG2 (("STATUS_FLOAT_INEXACT_RESULT"));
1197 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1199 case STATUS_FLOAT_INVALID_OPERATION
:
1200 OUTMSG2 (("STATUS_FLOAT_INVALID_OPERATION"));
1201 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1203 case STATUS_FLOAT_OVERFLOW
:
1204 OUTMSG2 (("STATUS_FLOAT_OVERFLOW"));
1205 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1207 case STATUS_FLOAT_STACK_CHECK
:
1208 OUTMSG2 (("STATUS_FLOAT_STACK_CHECK"));
1209 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1211 case STATUS_FLOAT_UNDERFLOW
:
1212 OUTMSG2 (("STATUS_FLOAT_UNDERFLOW"));
1213 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1215 case STATUS_FLOAT_DIVIDE_BY_ZERO
:
1216 OUTMSG2 (("STATUS_FLOAT_DIVIDE_BY_ZERO"));
1217 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1219 case STATUS_INTEGER_DIVIDE_BY_ZERO
:
1220 OUTMSG2 (("STATUS_INTEGER_DIVIDE_BY_ZERO"));
1221 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1223 case STATUS_INTEGER_OVERFLOW
:
1224 OUTMSG2 (("STATUS_INTEGER_OVERFLOW"));
1225 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1227 case EXCEPTION_BREAKPOINT
:
1228 OUTMSG2 (("EXCEPTION_BREAKPOINT"));
1229 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1231 /* Remove the initial breakpoint. */
1232 check_breakpoints ((CORE_ADDR
) (long) current_event
1233 .u
.Exception
.ExceptionRecord
.ExceptionAddress
);
1237 OUTMSG2 (("DBG_CONTROL_C"));
1238 ourstatus
->value
.sig
= GDB_SIGNAL_INT
;
1240 case DBG_CONTROL_BREAK
:
1241 OUTMSG2 (("DBG_CONTROL_BREAK"));
1242 ourstatus
->value
.sig
= GDB_SIGNAL_INT
;
1244 case EXCEPTION_SINGLE_STEP
:
1245 OUTMSG2 (("EXCEPTION_SINGLE_STEP"));
1246 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1248 case EXCEPTION_ILLEGAL_INSTRUCTION
:
1249 OUTMSG2 (("EXCEPTION_ILLEGAL_INSTRUCTION"));
1250 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1252 case EXCEPTION_PRIV_INSTRUCTION
:
1253 OUTMSG2 (("EXCEPTION_PRIV_INSTRUCTION"));
1254 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1256 case EXCEPTION_NONCONTINUABLE_EXCEPTION
:
1257 OUTMSG2 (("EXCEPTION_NONCONTINUABLE_EXCEPTION"));
1258 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1261 if (current_event
.u
.Exception
.dwFirstChance
)
1263 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1266 OUTMSG2 (("gdbserver: unknown target exception 0x%08x at 0x%s",
1267 (unsigned) current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
,
1268 phex_nz ((uintptr_t) current_event
.u
.Exception
.ExceptionRecord
.
1269 ExceptionAddress
, sizeof (uintptr_t))));
1270 ourstatus
->value
.sig
= GDB_SIGNAL_UNKNOWN
;
1274 last_sig
= ourstatus
->value
.sig
;
1279 suspend_one_thread (thread_info
*thread
)
1281 windows_thread_info
*th
= (windows_thread_info
*) thread_target_data (thread
);
1287 fake_breakpoint_event (void)
1289 OUTMSG2(("fake_breakpoint_event\n"));
1291 faked_breakpoint
= 1;
1293 memset (¤t_event
, 0, sizeof (current_event
));
1294 current_event
.dwThreadId
= main_thread_id
;
1295 current_event
.dwDebugEventCode
= EXCEPTION_DEBUG_EVENT
;
1296 current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
1297 = EXCEPTION_BREAKPOINT
;
1299 for_each_thread (suspend_one_thread
);
1304 auto_delete_breakpoint (CORE_ADDR stop_pc
)
1310 /* Get the next event from the child. */
1313 get_child_debug_event (struct target_waitstatus
*ourstatus
)
1317 last_sig
= GDB_SIGNAL_0
;
1318 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1320 /* Check if GDB sent us an interrupt request. */
1321 check_remote_input_interrupt_request ();
1323 if (soft_interrupt_requested
)
1325 soft_interrupt_requested
= 0;
1326 fake_breakpoint_event ();
1335 /* WinCE doesn't set an initial breakpoint automatically. To
1336 stop the inferior, we flush all currently pending debug
1337 events -- the thread list and the dll list are always
1338 reported immediatelly without delay, then, we suspend all
1339 threads and pretend we saw a trap at the current PC of the
1342 Contrary to desktop Windows, Windows CE *does* report the dll
1343 names on LOAD_DLL_DEBUG_EVENTs resulting from a
1344 DebugActiveProcess call. This limits the way we can detect
1345 if all the dlls have already been reported. If we get a real
1346 debug event before leaving attaching, the worst that will
1347 happen is the user will see a spurious breakpoint. */
1349 current_event
.dwDebugEventCode
= 0;
1350 if (!WaitForDebugEvent (¤t_event
, 0))
1352 OUTMSG2(("no attach events left\n"));
1353 fake_breakpoint_event ();
1357 OUTMSG2(("got attach event\n"));
1362 /* Keep the wait time low enough for comfortable remote
1363 interruption, but high enough so gdbserver doesn't become a
1365 if (!WaitForDebugEvent (¤t_event
, 250))
1367 DWORD e
= GetLastError();
1369 if (e
== ERROR_PIPE_NOT_CONNECTED
)
1371 /* This will happen if the loader fails to succesfully
1372 load the application, e.g., if the main executable
1373 tries to pull in a non-existing export from a
1375 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1376 ourstatus
->value
.integer
= 1;
1386 switch (current_event
.dwDebugEventCode
)
1388 case CREATE_THREAD_DEBUG_EVENT
:
1389 OUTMSG2 (("gdbserver: kernel event CREATE_THREAD_DEBUG_EVENT "
1390 "for pid=%u tid=%x)\n",
1391 (unsigned) current_event
.dwProcessId
,
1392 (unsigned) current_event
.dwThreadId
));
1394 /* Record the existence of this thread. */
1395 child_add_thread (current_event
.dwProcessId
,
1396 current_event
.dwThreadId
,
1397 current_event
.u
.CreateThread
.hThread
,
1398 current_event
.u
.CreateThread
.lpThreadLocalBase
);
1401 case EXIT_THREAD_DEBUG_EVENT
:
1402 OUTMSG2 (("gdbserver: kernel event EXIT_THREAD_DEBUG_EVENT "
1403 "for pid=%u tid=%x\n",
1404 (unsigned) current_event
.dwProcessId
,
1405 (unsigned) current_event
.dwThreadId
));
1406 child_delete_thread (current_event
.dwProcessId
,
1407 current_event
.dwThreadId
);
1409 current_thread
= get_first_thread ();
1412 case CREATE_PROCESS_DEBUG_EVENT
:
1413 OUTMSG2 (("gdbserver: kernel event CREATE_PROCESS_DEBUG_EVENT "
1414 "for pid=%u tid=%x\n",
1415 (unsigned) current_event
.dwProcessId
,
1416 (unsigned) current_event
.dwThreadId
));
1417 CloseHandle (current_event
.u
.CreateProcessInfo
.hFile
);
1419 current_process_handle
= current_event
.u
.CreateProcessInfo
.hProcess
;
1420 main_thread_id
= current_event
.dwThreadId
;
1422 /* Add the main thread. */
1423 child_add_thread (current_event
.dwProcessId
,
1425 current_event
.u
.CreateProcessInfo
.hThread
,
1426 current_event
.u
.CreateProcessInfo
.lpThreadLocalBase
);
1431 /* Windows CE doesn't set the initial breakpoint
1432 automatically like the desktop versions of Windows do.
1433 We add it explicitly here. It will be removed as soon as
1435 set_breakpoint_at ((CORE_ADDR
) (long) current_event
.u
1436 .CreateProcessInfo
.lpStartAddress
,
1437 auto_delete_breakpoint
);
1442 case EXIT_PROCESS_DEBUG_EVENT
:
1443 OUTMSG2 (("gdbserver: kernel event EXIT_PROCESS_DEBUG_EVENT "
1444 "for pid=%u tid=%x\n",
1445 (unsigned) current_event
.dwProcessId
,
1446 (unsigned) current_event
.dwThreadId
));
1448 DWORD exit_status
= current_event
.u
.ExitProcess
.dwExitCode
;
1449 /* If the exit status looks like a fatal exception, but we
1450 don't recognize the exception's code, make the original
1451 exit status value available, to avoid losing information. */
1453 = WIFSIGNALED (exit_status
) ? WTERMSIG (exit_status
) : -1;
1454 if (exit_signal
== -1)
1456 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1457 ourstatus
->value
.integer
= exit_status
;
1461 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
1462 ourstatus
->value
.sig
= gdb_signal_from_host (exit_signal
);
1465 child_continue (DBG_CONTINUE
, -1);
1466 CloseHandle (current_process_handle
);
1467 current_process_handle
= NULL
;
1470 case LOAD_DLL_DEBUG_EVENT
:
1471 OUTMSG2 (("gdbserver: kernel event LOAD_DLL_DEBUG_EVENT "
1472 "for pid=%u tid=%x\n",
1473 (unsigned) current_event
.dwProcessId
,
1474 (unsigned) current_event
.dwThreadId
));
1475 CloseHandle (current_event
.u
.LoadDll
.hFile
);
1476 if (! child_initialization_done
)
1480 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1481 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1484 case UNLOAD_DLL_DEBUG_EVENT
:
1485 OUTMSG2 (("gdbserver: kernel event UNLOAD_DLL_DEBUG_EVENT "
1486 "for pid=%u tid=%x\n",
1487 (unsigned) current_event
.dwProcessId
,
1488 (unsigned) current_event
.dwThreadId
));
1489 if (! child_initialization_done
)
1491 handle_unload_dll ();
1492 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1493 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1496 case EXCEPTION_DEBUG_EVENT
:
1497 OUTMSG2 (("gdbserver: kernel event EXCEPTION_DEBUG_EVENT "
1498 "for pid=%u tid=%x\n",
1499 (unsigned) current_event
.dwProcessId
,
1500 (unsigned) current_event
.dwThreadId
));
1501 handle_exception (ourstatus
);
1504 case OUTPUT_DEBUG_STRING_EVENT
:
1505 /* A message from the kernel (or Cygwin). */
1506 OUTMSG2 (("gdbserver: kernel event OUTPUT_DEBUG_STRING_EVENT "
1507 "for pid=%u tid=%x\n",
1508 (unsigned) current_event
.dwProcessId
,
1509 (unsigned) current_event
.dwThreadId
));
1510 handle_output_debug_string (nullptr);
1514 OUTMSG2 (("gdbserver: kernel event unknown "
1515 "for pid=%u tid=%x code=%x\n",
1516 (unsigned) current_event
.dwProcessId
,
1517 (unsigned) current_event
.dwThreadId
,
1518 (unsigned) current_event
.dwDebugEventCode
));
1522 ptid
= debug_event_ptid (¤t_event
);
1523 current_thread
= find_thread_ptid (ptid
);
1527 /* Wait for the inferior process to change state.
1528 STATUS will be filled in with a response code to send to GDB.
1529 Returns the signal which caused the process to stop. */
1531 win32_process_target::wait (ptid_t ptid
, target_waitstatus
*ourstatus
,
1534 struct regcache
*regcache
;
1536 if (cached_status
.kind
!= TARGET_WAITKIND_IGNORE
)
1538 /* The core always does a wait after creating the inferior, and
1539 do_initial_child_stuff already ran the inferior to the
1540 initial breakpoint (or an exit, if creating the process
1541 fails). Report it now. */
1542 *ourstatus
= cached_status
;
1543 cached_status
.kind
= TARGET_WAITKIND_IGNORE
;
1544 return debug_event_ptid (¤t_event
);
1549 if (!get_child_debug_event (ourstatus
))
1552 switch (ourstatus
->kind
)
1554 case TARGET_WAITKIND_EXITED
:
1555 OUTMSG2 (("Child exited with retcode = %x\n",
1556 ourstatus
->value
.integer
));
1557 win32_clear_inferiors ();
1558 return ptid_t (current_event
.dwProcessId
);
1559 case TARGET_WAITKIND_STOPPED
:
1560 case TARGET_WAITKIND_SIGNALLED
:
1561 case TARGET_WAITKIND_LOADED
:
1562 OUTMSG2 (("Child Stopped with signal = %d \n",
1563 ourstatus
->value
.sig
));
1565 regcache
= get_thread_regcache (current_thread
, 1);
1566 child_fetch_inferior_registers (regcache
, -1);
1567 return debug_event_ptid (¤t_event
);
1569 OUTMSG (("Ignoring unknown internal event, %d\n", ourstatus
->kind
));
1571 case TARGET_WAITKIND_SPURIOUS
:
1572 /* do nothing, just continue */
1573 child_continue (DBG_CONTINUE
, -1);
1579 /* Fetch registers from the inferior process.
1580 If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO. */
1582 win32_process_target::fetch_registers (regcache
*regcache
, int regno
)
1584 child_fetch_inferior_registers (regcache
, regno
);
1587 /* Store registers to the inferior process.
1588 If REGNO is -1, store all registers; otherwise, store at least REGNO. */
1590 win32_process_target::store_registers (regcache
*regcache
, int regno
)
1592 child_store_inferior_registers (regcache
, regno
);
1595 /* Read memory from the inferior process. This should generally be
1596 called through read_inferior_memory, which handles breakpoint shadowing.
1597 Read LEN bytes at MEMADDR into a buffer at MYADDR. */
1599 win32_process_target::read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
,
1602 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 0, 0) != len
;
1605 /* Write memory to the inferior process. This should generally be
1606 called through write_inferior_memory, which handles breakpoint shadowing.
1607 Write LEN bytes from the buffer at MYADDR to MEMADDR.
1608 Returns 0 on success and errno on failure. */
1610 win32_process_target::write_memory (CORE_ADDR memaddr
,
1611 const unsigned char *myaddr
, int len
)
1613 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 1, 0) != len
;
1616 /* Send an interrupt request to the inferior process. */
1618 win32_process_target::request_interrupt ()
1620 winapi_DebugBreakProcess DebugBreakProcess
;
1621 winapi_GenerateConsoleCtrlEvent GenerateConsoleCtrlEvent
;
1624 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
1626 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
1629 GenerateConsoleCtrlEvent
= GETPROCADDRESS (dll
, GenerateConsoleCtrlEvent
);
1631 if (GenerateConsoleCtrlEvent
!= NULL
1632 && GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT
, current_process_id
))
1635 /* GenerateConsoleCtrlEvent can fail if process id being debugged is
1636 not a process group id.
1637 Fallback to XP/Vista 'DebugBreakProcess', which generates a
1638 breakpoint exception in the interior process. */
1640 DebugBreakProcess
= GETPROCADDRESS (dll
, DebugBreakProcess
);
1642 if (DebugBreakProcess
!= NULL
1643 && DebugBreakProcess (current_process_handle
))
1646 /* Last resort, suspend all threads manually. */
1647 soft_interrupt_requested
= 1;
1651 win32_process_target::supports_hardware_single_step ()
1658 win32_error_to_fileio_error (DWORD err
)
1662 case ERROR_BAD_PATHNAME
:
1663 case ERROR_FILE_NOT_FOUND
:
1664 case ERROR_INVALID_NAME
:
1665 case ERROR_PATH_NOT_FOUND
:
1666 return FILEIO_ENOENT
;
1668 case ERROR_IO_DEVICE
:
1669 case ERROR_OPEN_FAILED
:
1671 case ERROR_INVALID_HANDLE
:
1672 return FILEIO_EBADF
;
1673 case ERROR_ACCESS_DENIED
:
1674 case ERROR_SHARING_VIOLATION
:
1675 return FILEIO_EACCES
;
1676 case ERROR_NOACCESS
:
1677 return FILEIO_EFAULT
;
1679 return FILEIO_EBUSY
;
1680 case ERROR_ALREADY_EXISTS
:
1681 case ERROR_FILE_EXISTS
:
1682 return FILEIO_EEXIST
;
1683 case ERROR_BAD_DEVICE
:
1684 return FILEIO_ENODEV
;
1685 case ERROR_DIRECTORY
:
1686 return FILEIO_ENOTDIR
;
1687 case ERROR_FILENAME_EXCED_RANGE
:
1688 case ERROR_INVALID_DATA
:
1689 case ERROR_INVALID_PARAMETER
:
1690 case ERROR_NEGATIVE_SEEK
:
1691 return FILEIO_EINVAL
;
1692 case ERROR_TOO_MANY_OPEN_FILES
:
1693 return FILEIO_EMFILE
;
1694 case ERROR_HANDLE_DISK_FULL
:
1695 case ERROR_DISK_FULL
:
1696 return FILEIO_ENOSPC
;
1697 case ERROR_WRITE_PROTECT
:
1698 return FILEIO_EROFS
;
1699 case ERROR_NOT_SUPPORTED
:
1700 return FILEIO_ENOSYS
;
1703 return FILEIO_EUNKNOWN
;
1707 win32_process_target::hostio_last_error (char *buf
)
1709 DWORD winerr
= GetLastError ();
1710 int fileio_err
= win32_error_to_fileio_error (winerr
);
1711 sprintf (buf
, "F-1,%x", fileio_err
);
1716 win32_process_target::supports_qxfer_siginfo ()
1721 /* Write Windows signal info. */
1724 win32_process_target::qxfer_siginfo (const char *annex
,
1725 unsigned char *readbuf
,
1726 unsigned const char *writebuf
,
1727 CORE_ADDR offset
, int len
)
1729 if (siginfo_er
.ExceptionCode
== 0)
1732 if (readbuf
== nullptr)
1735 if (offset
> sizeof (siginfo_er
))
1738 if (offset
+ len
> sizeof (siginfo_er
))
1739 len
= sizeof (siginfo_er
) - offset
;
1741 memcpy (readbuf
, (char *) &siginfo_er
+ offset
, len
);
1747 win32_process_target::supports_get_tib_address ()
1752 /* Write Windows OS Thread Information Block address. */
1755 win32_process_target::get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
)
1757 windows_thread_info
*th
;
1758 th
= thread_rec (ptid
, DONT_INVALIDATE_CONTEXT
);
1762 *addr
= th
->thread_local_base
;
1766 /* Implementation of the target_ops method "sw_breakpoint_from_kind". */
1769 win32_process_target::sw_breakpoint_from_kind (int kind
, int *size
)
1771 *size
= the_low_target
.breakpoint_len
;
1772 return the_low_target
.breakpoint
;
1775 /* The win32 target ops object. */
1777 static win32_process_target the_win32_target
;
1779 /* Initialize the Win32 backend. */
1781 initialize_low (void)
1783 set_target_ops (&the_win32_target
);
1784 the_low_target
.arch_setup ();