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;
77 static HANDLE current_process_handle
= NULL
;
78 static DWORD current_process_id
= 0;
79 static DWORD main_thread_id
= 0;
80 static EXCEPTION_RECORD siginfo_er
; /* Contents of $_siginfo */
81 static enum gdb_signal last_sig
= GDB_SIGNAL_0
;
83 /* The current debug event from WaitForDebugEvent. */
84 static DEBUG_EVENT current_event
;
86 /* A status that hasn't been reported to the core yet, and so
87 win32_wait should return it next, instead of fetching the next
88 debug event off the win32 API. */
89 static struct target_waitstatus cached_status
;
91 /* Non zero if an interrupt request is to be satisfied by suspending
93 static int soft_interrupt_requested
= 0;
95 /* Non zero if the inferior is stopped in a simulated breakpoint done
96 by suspending all the threads. */
97 static int faked_breakpoint
= 0;
99 const struct target_desc
*win32_tdesc
;
101 #define NUM_REGS (the_low_target.num_regs)
103 typedef BOOL (WINAPI
*winapi_DebugActiveProcessStop
) (DWORD dwProcessId
);
104 typedef BOOL (WINAPI
*winapi_DebugSetProcessKillOnExit
) (BOOL KillOnExit
);
105 typedef BOOL (WINAPI
*winapi_DebugBreakProcess
) (HANDLE
);
106 typedef BOOL (WINAPI
*winapi_GenerateConsoleCtrlEvent
) (DWORD
, DWORD
);
109 static void win32_add_all_dlls (void);
112 /* Get the thread ID from the current selected inferior (the current
115 current_thread_ptid (void)
120 /* The current debug event from WaitForDebugEvent. */
122 debug_event_ptid (DEBUG_EVENT
*event
)
124 return ptid_t (event
->dwProcessId
, event
->dwThreadId
, 0);
127 /* Get the thread context of the thread associated with TH. */
130 win32_get_thread_context (windows_thread_info
*th
)
132 memset (&th
->context
, 0, sizeof (CONTEXT
));
133 (*the_low_target
.get_thread_context
) (th
);
135 memcpy (&th
->base_context
, &th
->context
, sizeof (CONTEXT
));
139 /* Set the thread context of the thread associated with TH. */
142 win32_set_thread_context (windows_thread_info
*th
)
145 /* Calling SuspendThread on a thread that is running kernel code
146 will report that the suspending was successful, but in fact, that
147 will often not be true. In those cases, the context returned by
148 GetThreadContext will not be correct by the time the thread
149 stops, hence we can't set that context back into the thread when
150 resuming - it will most likely crash the inferior.
151 Unfortunately, there is no way to know when the thread will
152 really stop. To work around it, we'll only write the context
153 back to the thread when either the user or GDB explicitly change
154 it between stopping and resuming. */
155 if (memcmp (&th
->context
, &th
->base_context
, sizeof (CONTEXT
)) != 0)
157 SetThreadContext (th
->h
, &th
->context
);
160 /* Set the thread context of the thread associated with TH. */
163 win32_prepare_to_resume (windows_thread_info
*th
)
165 if (the_low_target
.prepare_to_resume
!= NULL
)
166 (*the_low_target
.prepare_to_resume
) (th
);
169 /* See win32-low.h. */
172 win32_require_context (windows_thread_info
*th
)
174 if (th
->context
.ContextFlags
== 0)
177 win32_get_thread_context (th
);
181 /* See nat/windows-nat.h. */
183 windows_thread_info
*
184 windows_nat::thread_rec (ptid_t ptid
, thread_disposition_type disposition
)
186 thread_info
*thread
= find_thread_ptid (ptid
);
190 windows_thread_info
*th
= (windows_thread_info
*) thread_target_data (thread
);
191 if (disposition
!= DONT_INVALIDATE_CONTEXT
)
192 win32_require_context (th
);
196 /* Add a thread to the thread list. */
197 static windows_thread_info
*
198 child_add_thread (DWORD pid
, DWORD tid
, HANDLE h
, void *tlb
)
200 windows_thread_info
*th
;
201 ptid_t ptid
= ptid_t (pid
, tid
, 0);
203 if ((th
= thread_rec (ptid
, DONT_INVALIDATE_CONTEXT
)))
206 th
= new windows_thread_info (tid
, h
, (CORE_ADDR
) (uintptr_t) tlb
);
208 add_thread (ptid
, th
);
210 if (the_low_target
.thread_added
!= NULL
)
211 (*the_low_target
.thread_added
) (th
);
216 /* Delete a thread from the list of threads. */
218 delete_thread_info (thread_info
*thread
)
220 windows_thread_info
*th
= (windows_thread_info
*) thread_target_data (thread
);
222 remove_thread (thread
);
226 /* Delete a thread from the list of threads. */
228 child_delete_thread (DWORD pid
, DWORD tid
)
230 /* If the last thread is exiting, just return. */
231 if (all_threads
.size () == 1)
234 thread_info
*thread
= find_thread_ptid (ptid_t (pid
, tid
));
238 delete_thread_info (thread
);
241 /* These watchpoint related wrapper functions simply pass on the function call
242 if the low target has registered a corresponding function. */
245 win32_process_target::supports_z_point_type (char z_type
)
247 return (the_low_target
.supports_z_point_type
!= NULL
248 && the_low_target
.supports_z_point_type (z_type
));
252 win32_process_target::insert_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
253 int size
, raw_breakpoint
*bp
)
255 if (the_low_target
.insert_point
!= NULL
)
256 return the_low_target
.insert_point (type
, addr
, size
, bp
);
258 /* Unsupported (see target.h). */
263 win32_process_target::remove_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
264 int size
, raw_breakpoint
*bp
)
266 if (the_low_target
.remove_point
!= NULL
)
267 return the_low_target
.remove_point (type
, addr
, size
, bp
);
269 /* Unsupported (see target.h). */
274 win32_process_target::stopped_by_watchpoint ()
276 if (the_low_target
.stopped_by_watchpoint
!= NULL
)
277 return the_low_target
.stopped_by_watchpoint ();
283 win32_process_target::stopped_data_address ()
285 if (the_low_target
.stopped_data_address
!= NULL
)
286 return the_low_target
.stopped_data_address ();
292 /* Transfer memory from/to the debugged process. */
294 child_xfer_memory (CORE_ADDR memaddr
, char *our
, int len
,
295 int write
, process_stratum_target
*target
)
300 uintptr_t addr
= (uintptr_t) memaddr
;
304 success
= WriteProcessMemory (current_process_handle
, (LPVOID
) addr
,
305 (LPCVOID
) our
, len
, &done
);
307 lasterror
= GetLastError ();
308 FlushInstructionCache (current_process_handle
, (LPCVOID
) addr
, len
);
312 success
= ReadProcessMemory (current_process_handle
, (LPCVOID
) addr
,
313 (LPVOID
) our
, len
, &done
);
315 lasterror
= GetLastError ();
317 if (!success
&& lasterror
== ERROR_PARTIAL_COPY
&& done
> 0)
320 return success
? done
: -1;
323 /* Clear out any old thread list and reinitialize it to a pristine
326 child_init_thread_list (void)
328 for_each_thread (delete_thread_info
);
331 /* Zero during the child initialization phase, and nonzero otherwise. */
333 static int child_initialization_done
= 0;
336 do_initial_child_stuff (HANDLE proch
, DWORD pid
, int attached
)
338 struct process_info
*proc
;
340 last_sig
= GDB_SIGNAL_0
;
342 current_process_handle
= proch
;
343 current_process_id
= pid
;
346 soft_interrupt_requested
= 0;
347 faked_breakpoint
= 0;
349 memset (¤t_event
, 0, sizeof (current_event
));
351 proc
= add_process (pid
, attached
);
352 proc
->tdesc
= win32_tdesc
;
353 child_init_thread_list ();
354 child_initialization_done
= 0;
356 if (the_low_target
.initial_stuff
!= NULL
)
357 (*the_low_target
.initial_stuff
) ();
359 cached_status
.kind
= TARGET_WAITKIND_IGNORE
;
361 /* Flush all currently pending debug events (thread and dll list) up
362 to the initial breakpoint. */
365 struct target_waitstatus status
;
367 the_target
->wait (minus_one_ptid
, &status
, 0);
369 /* Note win32_wait doesn't return thread events. */
370 if (status
.kind
!= TARGET_WAITKIND_LOADED
)
372 cached_status
= status
;
377 struct thread_resume resume
;
379 resume
.thread
= minus_one_ptid
;
380 resume
.kind
= resume_continue
;
383 the_target
->resume (&resume
, 1);
388 /* Now that the inferior has been started and all DLLs have been mapped,
389 we can iterate over all DLLs and load them in.
391 We avoid doing it any earlier because, on certain versions of Windows,
392 LOAD_DLL_DEBUG_EVENTs are sometimes not complete. In particular,
393 we have seen on Windows 8.1 that the ntdll.dll load event does not
394 include the DLL name, preventing us from creating an associated SO.
395 A possible explanation is that ntdll.dll might be mapped before
396 the SO info gets created by the Windows system -- ntdll.dll is
397 the first DLL to be reported via LOAD_DLL_DEBUG_EVENT and other DLLs
398 do not seem to suffer from that problem.
400 Rather than try to work around this sort of issue, it is much
401 simpler to just ignore DLL load/unload events during the startup
402 phase, and then process them all in one batch now. */
403 win32_add_all_dlls ();
406 child_initialization_done
= 1;
409 /* Resume all artificially suspended threads if we are continuing
412 continue_one_thread (thread_info
*thread
, int thread_id
)
414 windows_thread_info
*th
= (windows_thread_info
*) thread_target_data (thread
);
416 if (thread_id
== -1 || thread_id
== th
->tid
)
418 win32_prepare_to_resume (th
);
422 if (th
->context
.ContextFlags
)
424 win32_set_thread_context (th
);
425 th
->context
.ContextFlags
= 0;
434 child_continue (DWORD continue_status
, int thread_id
)
436 /* The inferior will only continue after the ContinueDebugEvent
438 for_each_thread ([&] (thread_info
*thread
)
440 continue_one_thread (thread
, thread_id
);
442 faked_breakpoint
= 0;
444 if (!ContinueDebugEvent (current_event
.dwProcessId
,
445 current_event
.dwThreadId
,
452 /* Fetch register(s) from the current thread context. */
454 child_fetch_inferior_registers (struct regcache
*regcache
, int r
)
457 windows_thread_info
*th
= thread_rec (current_thread_ptid (),
459 if (r
== -1 || r
> NUM_REGS
)
460 child_fetch_inferior_registers (regcache
, NUM_REGS
);
462 for (regno
= 0; regno
< r
; regno
++)
463 (*the_low_target
.fetch_inferior_register
) (regcache
, th
, regno
);
466 /* Store a new register value into the current thread context. We don't
467 change the program's context until later, when we resume it. */
469 child_store_inferior_registers (struct regcache
*regcache
, int r
)
472 windows_thread_info
*th
= thread_rec (current_thread_ptid (),
474 if (r
== -1 || r
== 0 || r
> NUM_REGS
)
475 child_store_inferior_registers (regcache
, NUM_REGS
);
477 for (regno
= 0; regno
< r
; regno
++)
478 (*the_low_target
.store_inferior_register
) (regcache
, th
, regno
);
481 /* Map the Windows error number in ERROR to a locale-dependent error
482 message string and return a pointer to it. Typically, the values
483 for ERROR come from GetLastError.
485 The string pointed to shall not be modified by the application,
486 but may be overwritten by a subsequent call to strwinerror
488 The strwinerror function does not change the current setting
492 strwinerror (DWORD error
)
494 static char buf
[1024];
496 DWORD lasterr
= GetLastError ();
497 DWORD chars
= FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
498 | FORMAT_MESSAGE_ALLOCATE_BUFFER
,
501 0, /* Default language */
507 /* If there is an \r\n appended, zap it. */
509 && msgbuf
[chars
- 2] == '\r'
510 && msgbuf
[chars
- 1] == '\n')
516 if (chars
> ((COUNTOF (buf
)) - 1))
518 chars
= COUNTOF (buf
) - 1;
523 wcstombs (buf
, msgbuf
, chars
+ 1);
525 strncpy (buf
, msgbuf
, chars
+ 1);
530 sprintf (buf
, "unknown win32 error (%u)", (unsigned) error
);
532 SetLastError (lasterr
);
537 create_process (const char *program
, char *args
,
538 DWORD flags
, PROCESS_INFORMATION
*pi
)
540 const char *inferior_cwd
= get_inferior_cwd ();
542 size_t argslen
, proglen
;
544 proglen
= strlen (program
) + 1;
545 argslen
= strlen (args
) + proglen
;
548 wchar_t *p
, *wprogram
, *wargs
, *wcwd
= NULL
;
550 wprogram
= (wchar_t *) alloca (proglen
* sizeof (wchar_t));
551 mbstowcs (wprogram
, program
, proglen
);
553 for (p
= wprogram
; *p
; ++p
)
557 wargs
= alloca ((argslen
+ 1) * sizeof (wchar_t));
558 wcscpy (wargs
, wprogram
);
559 wcscat (wargs
, L
" ");
560 mbstowcs (wargs
+ proglen
, args
, argslen
+ 1 - proglen
);
562 if (inferior_cwd
!= NULL
)
564 std::string expanded_infcwd
= gdb_tilde_expand (inferior_cwd
);
565 std::replace (expanded_infcwd
.begin (), expanded_infcwd
.end (),
567 wcwd
= alloca ((expanded_infcwd
.size () + 1) * sizeof (wchar_t));
568 if (mbstowcs (wcwd
, expanded_infcwd
.c_str (),
569 expanded_infcwd
.size () + 1) == NULL
)
572 Could not convert the expanded inferior cwd to wide-char."));
576 ret
= CreateProcessW (wprogram
, /* image name */
577 wargs
, /* command line */
578 NULL
, /* security, not supported */
579 NULL
, /* thread, not supported */
580 FALSE
, /* inherit handles, not supported */
581 flags
, /* start flags */
582 NULL
, /* environment, not supported */
583 wcwd
, /* current directory */
584 NULL
, /* start info, not supported */
587 STARTUPINFOA si
= { sizeof (STARTUPINFOA
) };
588 char *program_and_args
= (char *) alloca (argslen
+ 1);
590 strcpy (program_and_args
, program
);
591 strcat (program_and_args
, " ");
592 strcat (program_and_args
, args
);
593 ret
= CreateProcessA (program
, /* image name */
594 program_and_args
, /* command line */
597 TRUE
, /* inherit handles */
598 flags
, /* start flags */
599 NULL
, /* environment */
600 /* current directory */
601 (inferior_cwd
== NULL
603 : gdb_tilde_expand (inferior_cwd
).c_str()),
604 &si
, /* start info */
611 /* Start a new process.
612 PROGRAM is the program name.
613 PROGRAM_ARGS is the vector containing the inferior's args.
614 Returns the new PID on success, -1 on failure. Registers the new
615 process with the process list. */
617 win32_process_target::create_inferior (const char *program
,
618 const std::vector
<char *> &program_args
)
620 client_state
&cs
= get_client_state ();
622 char real_path
[PATH_MAX
];
623 char *orig_path
, *new_path
, *path_ptr
;
627 PROCESS_INFORMATION pi
;
629 std::string str_program_args
= stringify_argv (program_args
);
630 char *args
= (char *) str_program_args
.c_str ();
632 /* win32_wait needs to know we're not attaching. */
636 error ("No executable specified, specify executable to debug.\n");
638 flags
= DEBUG_PROCESS
| DEBUG_ONLY_THIS_PROCESS
;
642 path_ptr
= getenv ("PATH");
645 int size
= cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, NULL
, 0);
646 orig_path
= (char *) alloca (strlen (path_ptr
) + 1);
647 new_path
= (char *) alloca (size
);
648 strcpy (orig_path
, path_ptr
);
649 cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, new_path
, size
);
650 setenv ("PATH", new_path
, 1);
652 cygwin_conv_path (CCP_POSIX_TO_WIN_A
, program
, real_path
, PATH_MAX
);
656 OUTMSG2 (("Command line is \"%s %s\"\n", program
, args
));
658 #ifdef CREATE_NEW_PROCESS_GROUP
659 flags
|= CREATE_NEW_PROCESS_GROUP
;
662 ret
= create_process (program
, args
, flags
, &pi
);
663 err
= GetLastError ();
664 if (!ret
&& err
== ERROR_FILE_NOT_FOUND
)
666 char *exename
= (char *) alloca (strlen (program
) + 5);
667 strcat (strcpy (exename
, program
), ".exe");
668 ret
= create_process (exename
, args
, flags
, &pi
);
669 err
= GetLastError ();
674 setenv ("PATH", orig_path
, 1);
679 error ("Error creating process \"%s %s\", (error %d): %s\n",
680 program
, args
, (int) err
, strwinerror (err
));
684 OUTMSG2 (("Process created: %s %s\n", program
, (char *) args
));
688 /* On Windows CE this handle can't be closed. The OS reuses
689 it in the debug events, while the 9x/NT versions of Windows
690 probably use a DuplicateHandle'd one. */
691 CloseHandle (pi
.hThread
);
694 do_initial_child_stuff (pi
.hProcess
, pi
.dwProcessId
, 0);
696 /* Wait till we are at 1st instruction in program, return new pid
697 (assuming success). */
698 cs
.last_ptid
= wait (ptid_t (current_process_id
), &cs
.last_status
, 0);
700 /* Necessary for handle_v_kill. */
701 signal_pid
= current_process_id
;
703 return current_process_id
;
706 /* Attach to a running process.
707 PID is the process ID to attach to, specified by the user
708 or a higher layer. */
710 win32_process_target::attach (unsigned long pid
)
713 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
716 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
718 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
720 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
722 h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
725 if (DebugActiveProcess (pid
))
727 if (DebugSetProcessKillOnExit
!= NULL
)
728 DebugSetProcessKillOnExit (FALSE
);
730 /* win32_wait needs to know we're attaching. */
732 do_initial_child_stuff (h
, pid
, 1);
739 err
= GetLastError ();
740 error ("Attach to process failed (error %d): %s\n",
741 (int) err
, strwinerror (err
));
744 /* Handle OUTPUT_DEBUG_STRING_EVENT from child process. */
746 handle_output_debug_string (void)
748 #define READ_BUFFER_LEN 1024
750 char s
[READ_BUFFER_LEN
+ 1] = { 0 };
751 DWORD nbytes
= current_event
.u
.DebugString
.nDebugStringLength
;
756 if (nbytes
> READ_BUFFER_LEN
)
757 nbytes
= READ_BUFFER_LEN
;
759 addr
= (CORE_ADDR
) (size_t) current_event
.u
.DebugString
.lpDebugStringData
;
761 if (current_event
.u
.DebugString
.fUnicode
)
763 /* The event tells us how many bytes, not chars, even
765 WCHAR buffer
[(READ_BUFFER_LEN
+ 1) / sizeof (WCHAR
)] = { 0 };
766 if (read_inferior_memory (addr
, (unsigned char *) buffer
, nbytes
) != 0)
768 wcstombs (s
, buffer
, (nbytes
+ 1) / sizeof (WCHAR
));
772 if (read_inferior_memory (addr
, (unsigned char *) s
, nbytes
) != 0)
776 if (!startswith (s
, "cYg"))
786 #undef READ_BUFFER_LEN
790 win32_clear_inferiors (void)
792 if (current_process_handle
!= NULL
)
793 CloseHandle (current_process_handle
);
795 for_each_thread (delete_thread_info
);
796 siginfo_er
.ExceptionCode
= 0;
800 /* Implementation of target_ops::kill. */
803 win32_process_target::kill (process_info
*process
)
805 TerminateProcess (current_process_handle
, 0);
808 if (!child_continue (DBG_CONTINUE
, -1))
810 if (!WaitForDebugEvent (¤t_event
, INFINITE
))
812 if (current_event
.dwDebugEventCode
== EXIT_PROCESS_DEBUG_EVENT
)
814 else if (current_event
.dwDebugEventCode
== OUTPUT_DEBUG_STRING_EVENT
)
815 handle_output_debug_string ();
818 win32_clear_inferiors ();
820 remove_process (process
);
824 /* Implementation of target_ops::detach. */
827 win32_process_target::detach (process_info
*process
)
829 winapi_DebugActiveProcessStop DebugActiveProcessStop
= NULL
;
830 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
832 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
834 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
836 DebugActiveProcessStop
= GETPROCADDRESS (dll
, DebugActiveProcessStop
);
837 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
839 if (DebugSetProcessKillOnExit
== NULL
840 || DebugActiveProcessStop
== NULL
)
844 struct thread_resume resume
;
845 resume
.thread
= minus_one_ptid
;
846 resume
.kind
= resume_continue
;
848 this->resume (&resume
, 1);
851 if (!DebugActiveProcessStop (current_process_id
))
854 DebugSetProcessKillOnExit (FALSE
);
855 remove_process (process
);
857 win32_clear_inferiors ();
862 win32_process_target::mourn (struct process_info
*process
)
864 remove_process (process
);
867 /* Implementation of target_ops::join. */
870 win32_process_target::join (int pid
)
872 HANDLE h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
875 WaitForSingleObject (h
, INFINITE
);
880 /* Return true iff the thread with thread ID TID is alive. */
882 win32_process_target::thread_alive (ptid_t ptid
)
884 /* Our thread list is reliable; don't bother to poll target
886 return find_thread_ptid (ptid
) != NULL
;
889 /* Resume the inferior process. RESUME_INFO describes how we want
892 win32_process_target::resume (thread_resume
*resume_info
, size_t n
)
897 windows_thread_info
*th
;
898 DWORD continue_status
= DBG_CONTINUE
;
901 /* This handles the very limited set of resume packets that GDB can
902 currently produce. */
904 if (n
== 1 && resume_info
[0].thread
== minus_one_ptid
)
909 /* Yes, we're ignoring resume_info[0].thread. It'd be tricky to make
910 the Windows resume code do the right thing for thread switching. */
911 tid
= current_event
.dwThreadId
;
913 if (resume_info
[0].thread
!= minus_one_ptid
)
915 sig
= gdb_signal_from_host (resume_info
[0].sig
);
916 step
= resume_info
[0].kind
== resume_step
;
924 if (sig
!= GDB_SIGNAL_0
)
926 if (current_event
.dwDebugEventCode
!= EXCEPTION_DEBUG_EVENT
)
928 OUTMSG (("Cannot continue with signal %s here.\n",
929 gdb_signal_to_string (sig
)));
931 else if (sig
== last_sig
)
932 continue_status
= DBG_EXCEPTION_NOT_HANDLED
;
934 OUTMSG (("Can only continue with received signal %s.\n",
935 gdb_signal_to_string (last_sig
)));
938 last_sig
= GDB_SIGNAL_0
;
940 /* Get context for the currently selected thread. */
941 ptid
= debug_event_ptid (¤t_event
);
942 th
= thread_rec (ptid
, DONT_INVALIDATE_CONTEXT
);
945 win32_prepare_to_resume (th
);
947 if (th
->context
.ContextFlags
)
949 /* Move register values from the inferior into the thread
950 context structure. */
951 regcache_invalidate ();
955 if (the_low_target
.single_step
!= NULL
)
956 (*the_low_target
.single_step
) (th
);
958 error ("Single stepping is not supported "
959 "in this configuration.\n");
962 win32_set_thread_context (th
);
963 th
->context
.ContextFlags
= 0;
967 /* Allow continuing with the same signal that interrupted us.
968 Otherwise complain. */
970 child_continue (continue_status
, tid
);
974 win32_add_one_solib (const char *name
, CORE_ADDR load_addr
)
976 char buf
[MAX_PATH
+ 1];
977 char buf2
[MAX_PATH
+ 1];
980 WIN32_FIND_DATA w32_fd
;
981 WCHAR wname
[MAX_PATH
+ 1];
982 mbstowcs (wname
, name
, MAX_PATH
);
983 HANDLE h
= FindFirstFile (wname
, &w32_fd
);
985 WIN32_FIND_DATAA w32_fd
;
986 HANDLE h
= FindFirstFileA (name
, &w32_fd
);
989 /* The symbols in a dll are offset by 0x1000, which is the
990 offset from 0 of the first byte in an image - because
991 of the file header and the section alignment. */
994 if (h
== INVALID_HANDLE_VALUE
)
1002 char cwd
[MAX_PATH
+ 1];
1004 if (GetCurrentDirectoryA (MAX_PATH
+ 1, cwd
))
1006 p
= strrchr (buf
, '\\');
1009 SetCurrentDirectoryA (buf
);
1010 GetFullPathNameA (w32_fd
.cFileName
, MAX_PATH
, buf
, &p
);
1011 SetCurrentDirectoryA (cwd
);
1018 if (strcasecmp (buf
, "ntdll.dll") == 0)
1020 GetSystemDirectoryA (buf
, sizeof (buf
));
1021 strcat (buf
, "\\ntdll.dll");
1026 cygwin_conv_path (CCP_WIN_A_TO_POSIX
, buf
, buf2
, sizeof (buf2
));
1031 loaded_dll (buf2
, load_addr
);
1035 get_image_name (HANDLE h
, void *address
, int unicode
)
1037 static char buf
[(2 * MAX_PATH
) + 1];
1038 DWORD size
= unicode
? sizeof (WCHAR
) : sizeof (char);
1044 /* Attempt to read the name of the dll that was detected.
1045 This is documented to work only when actively debugging
1046 a program. It will not work for attached processes. */
1047 if (address
== NULL
)
1051 /* Windows CE reports the address of the image name,
1052 instead of an address of a pointer into the image name. */
1053 address_ptr
= address
;
1055 /* See if we could read the address of a string, and that the
1056 address isn't null. */
1057 if (!ReadProcessMemory (h
, address
, &address_ptr
,
1058 sizeof (address_ptr
), &done
)
1059 || done
!= sizeof (address_ptr
)
1064 /* Find the length of the string */
1065 while (ReadProcessMemory (h
, address_ptr
+ len
++ * size
, &b
, size
, &done
)
1066 && (b
[0] != 0 || b
[size
- 1] != 0) && done
== size
)
1070 ReadProcessMemory (h
, address_ptr
, buf
, len
, &done
);
1073 WCHAR
*unicode_address
= XALLOCAVEC (WCHAR
, len
);
1074 ReadProcessMemory (h
, address_ptr
, unicode_address
, len
* sizeof (WCHAR
),
1077 WideCharToMultiByte (CP_ACP
, 0, unicode_address
, len
, buf
, len
, 0, 0);
1083 typedef BOOL (WINAPI
*winapi_EnumProcessModules
) (HANDLE
, HMODULE
*,
1085 typedef BOOL (WINAPI
*winapi_GetModuleInformation
) (HANDLE
, HMODULE
,
1086 LPMODULEINFO
, DWORD
);
1087 typedef DWORD (WINAPI
*winapi_GetModuleFileNameExA
) (HANDLE
, HMODULE
,
1090 static winapi_EnumProcessModules win32_EnumProcessModules
;
1091 static winapi_GetModuleInformation win32_GetModuleInformation
;
1092 static winapi_GetModuleFileNameExA win32_GetModuleFileNameExA
;
1097 static int psapi_loaded
= 0;
1098 static HMODULE dll
= NULL
;
1103 dll
= LoadLibrary (TEXT("psapi.dll"));
1106 win32_EnumProcessModules
=
1107 GETPROCADDRESS (dll
, EnumProcessModules
);
1108 win32_GetModuleInformation
=
1109 GETPROCADDRESS (dll
, GetModuleInformation
);
1110 win32_GetModuleFileNameExA
=
1111 GETPROCADDRESS (dll
, GetModuleFileNameExA
);
1114 return (win32_EnumProcessModules
!= NULL
1115 && win32_GetModuleInformation
!= NULL
1116 && win32_GetModuleFileNameExA
!= NULL
);
1121 /* Iterate over all DLLs currently mapped by our inferior, and
1122 add them to our list of solibs. */
1125 win32_add_all_dlls (void)
1129 HMODULE
*DllHandle
= dh_buf
;
1137 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1142 if (!ok
|| !cbNeeded
)
1145 DllHandle
= (HMODULE
*) alloca (cbNeeded
);
1149 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1156 for (i
= 1; i
< ((size_t) cbNeeded
/ sizeof (HMODULE
)); i
++)
1159 char dll_name
[MAX_PATH
];
1161 if (!(*win32_GetModuleInformation
) (current_process_handle
,
1166 if ((*win32_GetModuleFileNameExA
) (current_process_handle
,
1171 win32_add_one_solib (dll_name
, (CORE_ADDR
) (uintptr_t) mi
.lpBaseOfDll
);
1176 typedef HANDLE (WINAPI
*winapi_CreateToolhelp32Snapshot
) (DWORD
, DWORD
);
1177 typedef BOOL (WINAPI
*winapi_Module32First
) (HANDLE
, LPMODULEENTRY32
);
1178 typedef BOOL (WINAPI
*winapi_Module32Next
) (HANDLE
, LPMODULEENTRY32
);
1180 /* Handle a DLL load event.
1182 This function assumes that this event did not occur during inferior
1183 initialization, where their event info may be incomplete (see
1184 do_initial_child_stuff and win32_add_all_dlls for more info on
1185 how we handle DLL loading during that phase). */
1188 handle_load_dll (void)
1190 LOAD_DLL_DEBUG_INFO
*event
= ¤t_event
.u
.LoadDll
;
1193 dll_name
= get_image_name (current_process_handle
,
1194 event
->lpImageName
, event
->fUnicode
);
1198 win32_add_one_solib (dll_name
, (CORE_ADDR
) (uintptr_t) event
->lpBaseOfDll
);
1201 /* Handle a DLL unload event.
1203 This function assumes that this event did not occur during inferior
1204 initialization, where their event info may be incomplete (see
1205 do_initial_child_stuff and win32_add_one_solib for more info
1206 on how we handle DLL loading during that phase). */
1209 handle_unload_dll (void)
1211 CORE_ADDR load_addr
=
1212 (CORE_ADDR
) (uintptr_t) current_event
.u
.UnloadDll
.lpBaseOfDll
;
1214 /* The symbols in a dll are offset by 0x1000, which is the
1215 offset from 0 of the first byte in an image - because
1216 of the file header and the section alignment. */
1217 load_addr
+= 0x1000;
1218 unloaded_dll (NULL
, load_addr
);
1222 handle_exception (struct target_waitstatus
*ourstatus
)
1224 DWORD code
= current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
;
1226 memcpy (&siginfo_er
, ¤t_event
.u
.Exception
.ExceptionRecord
,
1229 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
1233 case EXCEPTION_ACCESS_VIOLATION
:
1234 OUTMSG2 (("EXCEPTION_ACCESS_VIOLATION"));
1235 ourstatus
->value
.sig
= GDB_SIGNAL_SEGV
;
1237 case STATUS_STACK_OVERFLOW
:
1238 OUTMSG2 (("STATUS_STACK_OVERFLOW"));
1239 ourstatus
->value
.sig
= GDB_SIGNAL_SEGV
;
1241 case STATUS_FLOAT_DENORMAL_OPERAND
:
1242 OUTMSG2 (("STATUS_FLOAT_DENORMAL_OPERAND"));
1243 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1245 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
1246 OUTMSG2 (("EXCEPTION_ARRAY_BOUNDS_EXCEEDED"));
1247 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1249 case STATUS_FLOAT_INEXACT_RESULT
:
1250 OUTMSG2 (("STATUS_FLOAT_INEXACT_RESULT"));
1251 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1253 case STATUS_FLOAT_INVALID_OPERATION
:
1254 OUTMSG2 (("STATUS_FLOAT_INVALID_OPERATION"));
1255 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1257 case STATUS_FLOAT_OVERFLOW
:
1258 OUTMSG2 (("STATUS_FLOAT_OVERFLOW"));
1259 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1261 case STATUS_FLOAT_STACK_CHECK
:
1262 OUTMSG2 (("STATUS_FLOAT_STACK_CHECK"));
1263 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1265 case STATUS_FLOAT_UNDERFLOW
:
1266 OUTMSG2 (("STATUS_FLOAT_UNDERFLOW"));
1267 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1269 case STATUS_FLOAT_DIVIDE_BY_ZERO
:
1270 OUTMSG2 (("STATUS_FLOAT_DIVIDE_BY_ZERO"));
1271 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1273 case STATUS_INTEGER_DIVIDE_BY_ZERO
:
1274 OUTMSG2 (("STATUS_INTEGER_DIVIDE_BY_ZERO"));
1275 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1277 case STATUS_INTEGER_OVERFLOW
:
1278 OUTMSG2 (("STATUS_INTEGER_OVERFLOW"));
1279 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1281 case EXCEPTION_BREAKPOINT
:
1282 OUTMSG2 (("EXCEPTION_BREAKPOINT"));
1283 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1285 /* Remove the initial breakpoint. */
1286 check_breakpoints ((CORE_ADDR
) (long) current_event
1287 .u
.Exception
.ExceptionRecord
.ExceptionAddress
);
1291 OUTMSG2 (("DBG_CONTROL_C"));
1292 ourstatus
->value
.sig
= GDB_SIGNAL_INT
;
1294 case DBG_CONTROL_BREAK
:
1295 OUTMSG2 (("DBG_CONTROL_BREAK"));
1296 ourstatus
->value
.sig
= GDB_SIGNAL_INT
;
1298 case EXCEPTION_SINGLE_STEP
:
1299 OUTMSG2 (("EXCEPTION_SINGLE_STEP"));
1300 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1302 case EXCEPTION_ILLEGAL_INSTRUCTION
:
1303 OUTMSG2 (("EXCEPTION_ILLEGAL_INSTRUCTION"));
1304 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1306 case EXCEPTION_PRIV_INSTRUCTION
:
1307 OUTMSG2 (("EXCEPTION_PRIV_INSTRUCTION"));
1308 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1310 case EXCEPTION_NONCONTINUABLE_EXCEPTION
:
1311 OUTMSG2 (("EXCEPTION_NONCONTINUABLE_EXCEPTION"));
1312 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1315 if (current_event
.u
.Exception
.dwFirstChance
)
1317 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1320 OUTMSG2 (("gdbserver: unknown target exception 0x%08x at 0x%s",
1321 (unsigned) current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
,
1322 phex_nz ((uintptr_t) current_event
.u
.Exception
.ExceptionRecord
.
1323 ExceptionAddress
, sizeof (uintptr_t))));
1324 ourstatus
->value
.sig
= GDB_SIGNAL_UNKNOWN
;
1328 last_sig
= ourstatus
->value
.sig
;
1333 suspend_one_thread (thread_info
*thread
)
1335 windows_thread_info
*th
= (windows_thread_info
*) thread_target_data (thread
);
1341 fake_breakpoint_event (void)
1343 OUTMSG2(("fake_breakpoint_event\n"));
1345 faked_breakpoint
= 1;
1347 memset (¤t_event
, 0, sizeof (current_event
));
1348 current_event
.dwThreadId
= main_thread_id
;
1349 current_event
.dwDebugEventCode
= EXCEPTION_DEBUG_EVENT
;
1350 current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
1351 = EXCEPTION_BREAKPOINT
;
1353 for_each_thread (suspend_one_thread
);
1358 auto_delete_breakpoint (CORE_ADDR stop_pc
)
1364 /* Get the next event from the child. */
1367 get_child_debug_event (struct target_waitstatus
*ourstatus
)
1371 last_sig
= GDB_SIGNAL_0
;
1372 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1374 /* Check if GDB sent us an interrupt request. */
1375 check_remote_input_interrupt_request ();
1377 if (soft_interrupt_requested
)
1379 soft_interrupt_requested
= 0;
1380 fake_breakpoint_event ();
1389 /* WinCE doesn't set an initial breakpoint automatically. To
1390 stop the inferior, we flush all currently pending debug
1391 events -- the thread list and the dll list are always
1392 reported immediatelly without delay, then, we suspend all
1393 threads and pretend we saw a trap at the current PC of the
1396 Contrary to desktop Windows, Windows CE *does* report the dll
1397 names on LOAD_DLL_DEBUG_EVENTs resulting from a
1398 DebugActiveProcess call. This limits the way we can detect
1399 if all the dlls have already been reported. If we get a real
1400 debug event before leaving attaching, the worst that will
1401 happen is the user will see a spurious breakpoint. */
1403 current_event
.dwDebugEventCode
= 0;
1404 if (!WaitForDebugEvent (¤t_event
, 0))
1406 OUTMSG2(("no attach events left\n"));
1407 fake_breakpoint_event ();
1411 OUTMSG2(("got attach event\n"));
1416 /* Keep the wait time low enough for comfortable remote
1417 interruption, but high enough so gdbserver doesn't become a
1419 if (!WaitForDebugEvent (¤t_event
, 250))
1421 DWORD e
= GetLastError();
1423 if (e
== ERROR_PIPE_NOT_CONNECTED
)
1425 /* This will happen if the loader fails to succesfully
1426 load the application, e.g., if the main executable
1427 tries to pull in a non-existing export from a
1429 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1430 ourstatus
->value
.integer
= 1;
1440 switch (current_event
.dwDebugEventCode
)
1442 case CREATE_THREAD_DEBUG_EVENT
:
1443 OUTMSG2 (("gdbserver: kernel event CREATE_THREAD_DEBUG_EVENT "
1444 "for pid=%u tid=%x)\n",
1445 (unsigned) current_event
.dwProcessId
,
1446 (unsigned) current_event
.dwThreadId
));
1448 /* Record the existence of this thread. */
1449 child_add_thread (current_event
.dwProcessId
,
1450 current_event
.dwThreadId
,
1451 current_event
.u
.CreateThread
.hThread
,
1452 current_event
.u
.CreateThread
.lpThreadLocalBase
);
1455 case EXIT_THREAD_DEBUG_EVENT
:
1456 OUTMSG2 (("gdbserver: kernel event EXIT_THREAD_DEBUG_EVENT "
1457 "for pid=%u tid=%x\n",
1458 (unsigned) current_event
.dwProcessId
,
1459 (unsigned) current_event
.dwThreadId
));
1460 child_delete_thread (current_event
.dwProcessId
,
1461 current_event
.dwThreadId
);
1463 current_thread
= get_first_thread ();
1466 case CREATE_PROCESS_DEBUG_EVENT
:
1467 OUTMSG2 (("gdbserver: kernel event CREATE_PROCESS_DEBUG_EVENT "
1468 "for pid=%u tid=%x\n",
1469 (unsigned) current_event
.dwProcessId
,
1470 (unsigned) current_event
.dwThreadId
));
1471 CloseHandle (current_event
.u
.CreateProcessInfo
.hFile
);
1473 current_process_handle
= current_event
.u
.CreateProcessInfo
.hProcess
;
1474 main_thread_id
= current_event
.dwThreadId
;
1476 /* Add the main thread. */
1477 child_add_thread (current_event
.dwProcessId
,
1479 current_event
.u
.CreateProcessInfo
.hThread
,
1480 current_event
.u
.CreateProcessInfo
.lpThreadLocalBase
);
1485 /* Windows CE doesn't set the initial breakpoint
1486 automatically like the desktop versions of Windows do.
1487 We add it explicitly here. It will be removed as soon as
1489 set_breakpoint_at ((CORE_ADDR
) (long) current_event
.u
1490 .CreateProcessInfo
.lpStartAddress
,
1491 auto_delete_breakpoint
);
1496 case EXIT_PROCESS_DEBUG_EVENT
:
1497 OUTMSG2 (("gdbserver: kernel event EXIT_PROCESS_DEBUG_EVENT "
1498 "for pid=%u tid=%x\n",
1499 (unsigned) current_event
.dwProcessId
,
1500 (unsigned) current_event
.dwThreadId
));
1502 DWORD exit_status
= current_event
.u
.ExitProcess
.dwExitCode
;
1503 /* If the exit status looks like a fatal exception, but we
1504 don't recognize the exception's code, make the original
1505 exit status value available, to avoid losing information. */
1507 = WIFSIGNALED (exit_status
) ? WTERMSIG (exit_status
) : -1;
1508 if (exit_signal
== -1)
1510 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1511 ourstatus
->value
.integer
= exit_status
;
1515 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
1516 ourstatus
->value
.sig
= gdb_signal_from_host (exit_signal
);
1519 child_continue (DBG_CONTINUE
, -1);
1520 CloseHandle (current_process_handle
);
1521 current_process_handle
= NULL
;
1524 case LOAD_DLL_DEBUG_EVENT
:
1525 OUTMSG2 (("gdbserver: kernel event LOAD_DLL_DEBUG_EVENT "
1526 "for pid=%u tid=%x\n",
1527 (unsigned) current_event
.dwProcessId
,
1528 (unsigned) current_event
.dwThreadId
));
1529 CloseHandle (current_event
.u
.LoadDll
.hFile
);
1530 if (! child_initialization_done
)
1534 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1535 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1538 case UNLOAD_DLL_DEBUG_EVENT
:
1539 OUTMSG2 (("gdbserver: kernel event UNLOAD_DLL_DEBUG_EVENT "
1540 "for pid=%u tid=%x\n",
1541 (unsigned) current_event
.dwProcessId
,
1542 (unsigned) current_event
.dwThreadId
));
1543 if (! child_initialization_done
)
1545 handle_unload_dll ();
1546 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1547 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1550 case EXCEPTION_DEBUG_EVENT
:
1551 OUTMSG2 (("gdbserver: kernel event EXCEPTION_DEBUG_EVENT "
1552 "for pid=%u tid=%x\n",
1553 (unsigned) current_event
.dwProcessId
,
1554 (unsigned) current_event
.dwThreadId
));
1555 handle_exception (ourstatus
);
1558 case OUTPUT_DEBUG_STRING_EVENT
:
1559 /* A message from the kernel (or Cygwin). */
1560 OUTMSG2 (("gdbserver: kernel event OUTPUT_DEBUG_STRING_EVENT "
1561 "for pid=%u tid=%x\n",
1562 (unsigned) current_event
.dwProcessId
,
1563 (unsigned) current_event
.dwThreadId
));
1564 handle_output_debug_string ();
1568 OUTMSG2 (("gdbserver: kernel event unknown "
1569 "for pid=%u tid=%x code=%x\n",
1570 (unsigned) current_event
.dwProcessId
,
1571 (unsigned) current_event
.dwThreadId
,
1572 (unsigned) current_event
.dwDebugEventCode
));
1576 ptid
= debug_event_ptid (¤t_event
);
1577 current_thread
= find_thread_ptid (ptid
);
1581 /* Wait for the inferior process to change state.
1582 STATUS will be filled in with a response code to send to GDB.
1583 Returns the signal which caused the process to stop. */
1585 win32_process_target::wait (ptid_t ptid
, target_waitstatus
*ourstatus
,
1588 struct regcache
*regcache
;
1590 if (cached_status
.kind
!= TARGET_WAITKIND_IGNORE
)
1592 /* The core always does a wait after creating the inferior, and
1593 do_initial_child_stuff already ran the inferior to the
1594 initial breakpoint (or an exit, if creating the process
1595 fails). Report it now. */
1596 *ourstatus
= cached_status
;
1597 cached_status
.kind
= TARGET_WAITKIND_IGNORE
;
1598 return debug_event_ptid (¤t_event
);
1603 if (!get_child_debug_event (ourstatus
))
1606 switch (ourstatus
->kind
)
1608 case TARGET_WAITKIND_EXITED
:
1609 OUTMSG2 (("Child exited with retcode = %x\n",
1610 ourstatus
->value
.integer
));
1611 win32_clear_inferiors ();
1612 return ptid_t (current_event
.dwProcessId
);
1613 case TARGET_WAITKIND_STOPPED
:
1614 case TARGET_WAITKIND_SIGNALLED
:
1615 case TARGET_WAITKIND_LOADED
:
1616 OUTMSG2 (("Child Stopped with signal = %d \n",
1617 ourstatus
->value
.sig
));
1619 regcache
= get_thread_regcache (current_thread
, 1);
1620 child_fetch_inferior_registers (regcache
, -1);
1621 return debug_event_ptid (¤t_event
);
1623 OUTMSG (("Ignoring unknown internal event, %d\n", ourstatus
->kind
));
1625 case TARGET_WAITKIND_SPURIOUS
:
1626 /* do nothing, just continue */
1627 child_continue (DBG_CONTINUE
, -1);
1633 /* Fetch registers from the inferior process.
1634 If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO. */
1636 win32_process_target::fetch_registers (regcache
*regcache
, int regno
)
1638 child_fetch_inferior_registers (regcache
, regno
);
1641 /* Store registers to the inferior process.
1642 If REGNO is -1, store all registers; otherwise, store at least REGNO. */
1644 win32_process_target::store_registers (regcache
*regcache
, int regno
)
1646 child_store_inferior_registers (regcache
, regno
);
1649 /* Read memory from the inferior process. This should generally be
1650 called through read_inferior_memory, which handles breakpoint shadowing.
1651 Read LEN bytes at MEMADDR into a buffer at MYADDR. */
1653 win32_process_target::read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
,
1656 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 0, 0) != len
;
1659 /* Write memory to the inferior process. This should generally be
1660 called through write_inferior_memory, which handles breakpoint shadowing.
1661 Write LEN bytes from the buffer at MYADDR to MEMADDR.
1662 Returns 0 on success and errno on failure. */
1664 win32_process_target::write_memory (CORE_ADDR memaddr
,
1665 const unsigned char *myaddr
, int len
)
1667 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 1, 0) != len
;
1670 /* Send an interrupt request to the inferior process. */
1672 win32_process_target::request_interrupt ()
1674 winapi_DebugBreakProcess DebugBreakProcess
;
1675 winapi_GenerateConsoleCtrlEvent GenerateConsoleCtrlEvent
;
1678 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
1680 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
1683 GenerateConsoleCtrlEvent
= GETPROCADDRESS (dll
, GenerateConsoleCtrlEvent
);
1685 if (GenerateConsoleCtrlEvent
!= NULL
1686 && GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT
, current_process_id
))
1689 /* GenerateConsoleCtrlEvent can fail if process id being debugged is
1690 not a process group id.
1691 Fallback to XP/Vista 'DebugBreakProcess', which generates a
1692 breakpoint exception in the interior process. */
1694 DebugBreakProcess
= GETPROCADDRESS (dll
, DebugBreakProcess
);
1696 if (DebugBreakProcess
!= NULL
1697 && DebugBreakProcess (current_process_handle
))
1700 /* Last resort, suspend all threads manually. */
1701 soft_interrupt_requested
= 1;
1705 win32_process_target::supports_hardware_single_step ()
1712 win32_error_to_fileio_error (DWORD err
)
1716 case ERROR_BAD_PATHNAME
:
1717 case ERROR_FILE_NOT_FOUND
:
1718 case ERROR_INVALID_NAME
:
1719 case ERROR_PATH_NOT_FOUND
:
1720 return FILEIO_ENOENT
;
1722 case ERROR_IO_DEVICE
:
1723 case ERROR_OPEN_FAILED
:
1725 case ERROR_INVALID_HANDLE
:
1726 return FILEIO_EBADF
;
1727 case ERROR_ACCESS_DENIED
:
1728 case ERROR_SHARING_VIOLATION
:
1729 return FILEIO_EACCES
;
1730 case ERROR_NOACCESS
:
1731 return FILEIO_EFAULT
;
1733 return FILEIO_EBUSY
;
1734 case ERROR_ALREADY_EXISTS
:
1735 case ERROR_FILE_EXISTS
:
1736 return FILEIO_EEXIST
;
1737 case ERROR_BAD_DEVICE
:
1738 return FILEIO_ENODEV
;
1739 case ERROR_DIRECTORY
:
1740 return FILEIO_ENOTDIR
;
1741 case ERROR_FILENAME_EXCED_RANGE
:
1742 case ERROR_INVALID_DATA
:
1743 case ERROR_INVALID_PARAMETER
:
1744 case ERROR_NEGATIVE_SEEK
:
1745 return FILEIO_EINVAL
;
1746 case ERROR_TOO_MANY_OPEN_FILES
:
1747 return FILEIO_EMFILE
;
1748 case ERROR_HANDLE_DISK_FULL
:
1749 case ERROR_DISK_FULL
:
1750 return FILEIO_ENOSPC
;
1751 case ERROR_WRITE_PROTECT
:
1752 return FILEIO_EROFS
;
1753 case ERROR_NOT_SUPPORTED
:
1754 return FILEIO_ENOSYS
;
1757 return FILEIO_EUNKNOWN
;
1761 win32_process_target::hostio_last_error (char *buf
)
1763 DWORD winerr
= GetLastError ();
1764 int fileio_err
= win32_error_to_fileio_error (winerr
);
1765 sprintf (buf
, "F-1,%x", fileio_err
);
1770 win32_process_target::supports_qxfer_siginfo ()
1775 /* Write Windows signal info. */
1778 win32_process_target::qxfer_siginfo (const char *annex
,
1779 unsigned char *readbuf
,
1780 unsigned const char *writebuf
,
1781 CORE_ADDR offset
, int len
)
1783 if (siginfo_er
.ExceptionCode
== 0)
1786 if (readbuf
== nullptr)
1789 if (offset
> sizeof (siginfo_er
))
1792 if (offset
+ len
> sizeof (siginfo_er
))
1793 len
= sizeof (siginfo_er
) - offset
;
1795 memcpy (readbuf
, (char *) &siginfo_er
+ offset
, len
);
1801 win32_process_target::supports_get_tib_address ()
1806 /* Write Windows OS Thread Information Block address. */
1809 win32_process_target::get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
)
1811 windows_thread_info
*th
;
1812 th
= thread_rec (ptid
, DONT_INVALIDATE_CONTEXT
);
1816 *addr
= th
->thread_local_base
;
1820 /* Implementation of the target_ops method "sw_breakpoint_from_kind". */
1823 win32_process_target::sw_breakpoint_from_kind (int kind
, int *size
)
1825 *size
= the_low_target
.breakpoint_len
;
1826 return the_low_target
.breakpoint
;
1829 /* The win32 target ops object. */
1831 static win32_process_target the_win32_target
;
1833 /* Initialize the Win32 backend. */
1835 initialize_low (void)
1837 set_target_ops (&the_win32_target
);
1838 the_low_target
.arch_setup ();