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"
40 #include <sys/cygwin.h>
43 #define OUTMSG(X) do { printf X; fflush (stderr); } while (0)
56 #define _T(x) TEXT (x)
60 #define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0]))
64 # define GETPROCADDRESS(DLL, PROC) \
65 ((winapi_ ## PROC) GetProcAddress (DLL, TEXT (#PROC)))
67 # define GETPROCADDRESS(DLL, PROC) \
68 ((winapi_ ## PROC) GetProcAddress (DLL, #PROC))
71 int using_threads
= 1;
74 static int attaching
= 0;
75 static HANDLE current_process_handle
= NULL
;
76 static DWORD current_process_id
= 0;
77 static DWORD main_thread_id
= 0;
78 static EXCEPTION_RECORD siginfo_er
; /* Contents of $_siginfo */
79 static enum gdb_signal last_sig
= GDB_SIGNAL_0
;
81 /* The current debug event from WaitForDebugEvent. */
82 static DEBUG_EVENT current_event
;
84 /* A status that hasn't been reported to the core yet, and so
85 win32_wait should return it next, instead of fetching the next
86 debug event off the win32 API. */
87 static struct target_waitstatus cached_status
;
89 /* Non zero if an interrupt request is to be satisfied by suspending
91 static int soft_interrupt_requested
= 0;
93 /* Non zero if the inferior is stopped in a simulated breakpoint done
94 by suspending all the threads. */
95 static int faked_breakpoint
= 0;
97 const struct target_desc
*win32_tdesc
;
99 #define NUM_REGS (the_low_target.num_regs)
101 typedef BOOL (WINAPI
*winapi_DebugActiveProcessStop
) (DWORD dwProcessId
);
102 typedef BOOL (WINAPI
*winapi_DebugSetProcessKillOnExit
) (BOOL KillOnExit
);
103 typedef BOOL (WINAPI
*winapi_DebugBreakProcess
) (HANDLE
);
104 typedef BOOL (WINAPI
*winapi_GenerateConsoleCtrlEvent
) (DWORD
, DWORD
);
107 static void win32_add_all_dlls (void);
110 /* Get the thread ID from the current selected inferior (the current
113 current_thread_ptid (void)
118 /* The current debug event from WaitForDebugEvent. */
120 debug_event_ptid (DEBUG_EVENT
*event
)
122 return ptid_t (event
->dwProcessId
, event
->dwThreadId
, 0);
125 /* Get the thread context of the thread associated with TH. */
128 win32_get_thread_context (windows_thread_info
*th
)
130 memset (&th
->context
, 0, sizeof (CONTEXT
));
131 (*the_low_target
.get_thread_context
) (th
);
133 memcpy (&th
->base_context
, &th
->context
, sizeof (CONTEXT
));
137 /* Set the thread context of the thread associated with TH. */
140 win32_set_thread_context (windows_thread_info
*th
)
143 /* Calling SuspendThread on a thread that is running kernel code
144 will report that the suspending was successful, but in fact, that
145 will often not be true. In those cases, the context returned by
146 GetThreadContext will not be correct by the time the thread
147 stops, hence we can't set that context back into the thread when
148 resuming - it will most likely crash the inferior.
149 Unfortunately, there is no way to know when the thread will
150 really stop. To work around it, we'll only write the context
151 back to the thread when either the user or GDB explicitly change
152 it between stopping and resuming. */
153 if (memcmp (&th
->context
, &th
->base_context
, sizeof (CONTEXT
)) != 0)
155 SetThreadContext (th
->h
, &th
->context
);
158 /* Set the thread context of the thread associated with TH. */
161 win32_prepare_to_resume (windows_thread_info
*th
)
163 if (the_low_target
.prepare_to_resume
!= NULL
)
164 (*the_low_target
.prepare_to_resume
) (th
);
167 /* See win32-low.h. */
170 win32_require_context (windows_thread_info
*th
)
172 if (th
->context
.ContextFlags
== 0)
175 win32_get_thread_context (th
);
179 /* Find a thread record given a thread id. If GET_CONTEXT is set then
180 also retrieve the context for this thread. */
181 static windows_thread_info
*
182 thread_rec (ptid_t ptid
, int get_context
)
184 thread_info
*thread
= find_thread_ptid (ptid
);
188 windows_thread_info
*th
= (windows_thread_info
*) thread_target_data (thread
);
190 win32_require_context (th
);
194 /* Add a thread to the thread list. */
195 static windows_thread_info
*
196 child_add_thread (DWORD pid
, DWORD tid
, HANDLE h
, void *tlb
)
198 windows_thread_info
*th
;
199 ptid_t ptid
= ptid_t (pid
, tid
, 0);
201 if ((th
= thread_rec (ptid
, FALSE
)))
204 th
= new windows_thread_info (tid
, h
, (CORE_ADDR
) (uintptr_t) tlb
);
206 add_thread (ptid
, th
);
208 if (the_low_target
.thread_added
!= NULL
)
209 (*the_low_target
.thread_added
) (th
);
214 /* Delete a thread from the list of threads. */
216 delete_thread_info (thread_info
*thread
)
218 windows_thread_info
*th
= (windows_thread_info
*) thread_target_data (thread
);
220 remove_thread (thread
);
224 /* Delete a thread from the list of threads. */
226 child_delete_thread (DWORD pid
, DWORD tid
)
228 /* If the last thread is exiting, just return. */
229 if (all_threads
.size () == 1)
232 thread_info
*thread
= find_thread_ptid (ptid_t (pid
, tid
));
236 delete_thread_info (thread
);
239 /* These watchpoint related wrapper functions simply pass on the function call
240 if the low target has registered a corresponding function. */
243 win32_process_target::supports_z_point_type (char z_type
)
245 return (the_low_target
.supports_z_point_type
!= NULL
246 && the_low_target
.supports_z_point_type (z_type
));
250 win32_process_target::insert_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
251 int size
, raw_breakpoint
*bp
)
253 if (the_low_target
.insert_point
!= NULL
)
254 return the_low_target
.insert_point (type
, addr
, size
, bp
);
256 /* Unsupported (see target.h). */
261 win32_process_target::remove_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
262 int size
, raw_breakpoint
*bp
)
264 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 (current_process_handle
, (LPVOID
) addr
,
303 (LPCVOID
) our
, len
, &done
);
305 lasterror
= GetLastError ();
306 FlushInstructionCache (current_process_handle
, (LPCVOID
) addr
, len
);
310 success
= ReadProcessMemory (current_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 last_sig
= GDB_SIGNAL_0
;
340 current_process_handle
= proch
;
341 current_process_id
= pid
;
344 soft_interrupt_requested
= 0;
345 faked_breakpoint
= 0;
347 memset (¤t_event
, 0, sizeof (current_event
));
349 proc
= add_process (pid
, attached
);
350 proc
->tdesc
= win32_tdesc
;
351 child_init_thread_list ();
352 child_initialization_done
= 0;
354 if (the_low_target
.initial_stuff
!= NULL
)
355 (*the_low_target
.initial_stuff
) ();
357 cached_status
.kind
= TARGET_WAITKIND_IGNORE
;
359 /* Flush all currently pending debug events (thread and dll list) up
360 to the initial breakpoint. */
363 struct target_waitstatus status
;
365 the_target
->wait (minus_one_ptid
, &status
, 0);
367 /* Note win32_wait doesn't return thread events. */
368 if (status
.kind
!= TARGET_WAITKIND_LOADED
)
370 cached_status
= status
;
375 struct thread_resume resume
;
377 resume
.thread
= minus_one_ptid
;
378 resume
.kind
= resume_continue
;
381 the_target
->resume (&resume
, 1);
386 /* Now that the inferior has been started and all DLLs have been mapped,
387 we can iterate over all DLLs and load them in.
389 We avoid doing it any earlier because, on certain versions of Windows,
390 LOAD_DLL_DEBUG_EVENTs are sometimes not complete. In particular,
391 we have seen on Windows 8.1 that the ntdll.dll load event does not
392 include the DLL name, preventing us from creating an associated SO.
393 A possible explanation is that ntdll.dll might be mapped before
394 the SO info gets created by the Windows system -- ntdll.dll is
395 the first DLL to be reported via LOAD_DLL_DEBUG_EVENT and other DLLs
396 do not seem to suffer from that problem.
398 Rather than try to work around this sort of issue, it is much
399 simpler to just ignore DLL load/unload events during the startup
400 phase, and then process them all in one batch now. */
401 win32_add_all_dlls ();
404 child_initialization_done
= 1;
407 /* Resume all artificially suspended threads if we are continuing
410 continue_one_thread (thread_info
*thread
, int thread_id
)
412 windows_thread_info
*th
= (windows_thread_info
*) thread_target_data (thread
);
414 if (thread_id
== -1 || thread_id
== th
->tid
)
416 win32_prepare_to_resume (th
);
420 if (th
->context
.ContextFlags
)
422 win32_set_thread_context (th
);
423 th
->context
.ContextFlags
= 0;
432 child_continue (DWORD continue_status
, int thread_id
)
434 /* The inferior will only continue after the ContinueDebugEvent
436 for_each_thread ([&] (thread_info
*thread
)
438 continue_one_thread (thread
, thread_id
);
440 faked_breakpoint
= 0;
442 if (!ContinueDebugEvent (current_event
.dwProcessId
,
443 current_event
.dwThreadId
,
450 /* Fetch register(s) from the current thread context. */
452 child_fetch_inferior_registers (struct regcache
*regcache
, int r
)
455 windows_thread_info
*th
= thread_rec (current_thread_ptid (), TRUE
);
456 if (r
== -1 || r
> NUM_REGS
)
457 child_fetch_inferior_registers (regcache
, NUM_REGS
);
459 for (regno
= 0; regno
< r
; regno
++)
460 (*the_low_target
.fetch_inferior_register
) (regcache
, th
, regno
);
463 /* Store a new register value into the current thread context. We don't
464 change the program's context until later, when we resume it. */
466 child_store_inferior_registers (struct regcache
*regcache
, int r
)
469 windows_thread_info
*th
= thread_rec (current_thread_ptid (), TRUE
);
470 if (r
== -1 || r
== 0 || r
> NUM_REGS
)
471 child_store_inferior_registers (regcache
, NUM_REGS
);
473 for (regno
= 0; regno
< r
; regno
++)
474 (*the_low_target
.store_inferior_register
) (regcache
, th
, regno
);
477 /* Map the Windows error number in ERROR to a locale-dependent error
478 message string and return a pointer to it. Typically, the values
479 for ERROR come from GetLastError.
481 The string pointed to shall not be modified by the application,
482 but may be overwritten by a subsequent call to strwinerror
484 The strwinerror function does not change the current setting
488 strwinerror (DWORD error
)
490 static char buf
[1024];
492 DWORD lasterr
= GetLastError ();
493 DWORD chars
= FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
494 | FORMAT_MESSAGE_ALLOCATE_BUFFER
,
497 0, /* Default language */
503 /* If there is an \r\n appended, zap it. */
505 && msgbuf
[chars
- 2] == '\r'
506 && msgbuf
[chars
- 1] == '\n')
512 if (chars
> ((COUNTOF (buf
)) - 1))
514 chars
= COUNTOF (buf
) - 1;
519 wcstombs (buf
, msgbuf
, chars
+ 1);
521 strncpy (buf
, msgbuf
, chars
+ 1);
526 sprintf (buf
, "unknown win32 error (%u)", (unsigned) error
);
528 SetLastError (lasterr
);
533 create_process (const char *program
, char *args
,
534 DWORD flags
, PROCESS_INFORMATION
*pi
)
536 const char *inferior_cwd
= get_inferior_cwd ();
538 size_t argslen
, proglen
;
540 proglen
= strlen (program
) + 1;
541 argslen
= strlen (args
) + proglen
;
544 wchar_t *p
, *wprogram
, *wargs
, *wcwd
= NULL
;
546 wprogram
= (wchar_t *) alloca (proglen
* sizeof (wchar_t));
547 mbstowcs (wprogram
, program
, proglen
);
549 for (p
= wprogram
; *p
; ++p
)
553 wargs
= alloca ((argslen
+ 1) * sizeof (wchar_t));
554 wcscpy (wargs
, wprogram
);
555 wcscat (wargs
, L
" ");
556 mbstowcs (wargs
+ proglen
, args
, argslen
+ 1 - proglen
);
558 if (inferior_cwd
!= NULL
)
560 std::string expanded_infcwd
= gdb_tilde_expand (inferior_cwd
);
561 std::replace (expanded_infcwd
.begin (), expanded_infcwd
.end (),
563 wcwd
= alloca ((expanded_infcwd
.size () + 1) * sizeof (wchar_t));
564 if (mbstowcs (wcwd
, expanded_infcwd
.c_str (),
565 expanded_infcwd
.size () + 1) == NULL
)
568 Could not convert the expanded inferior cwd to wide-char."));
572 ret
= CreateProcessW (wprogram
, /* image name */
573 wargs
, /* command line */
574 NULL
, /* security, not supported */
575 NULL
, /* thread, not supported */
576 FALSE
, /* inherit handles, not supported */
577 flags
, /* start flags */
578 NULL
, /* environment, not supported */
579 wcwd
, /* current directory */
580 NULL
, /* start info, not supported */
583 STARTUPINFOA si
= { sizeof (STARTUPINFOA
) };
584 char *program_and_args
= (char *) alloca (argslen
+ 1);
586 strcpy (program_and_args
, program
);
587 strcat (program_and_args
, " ");
588 strcat (program_and_args
, args
);
589 ret
= CreateProcessA (program
, /* image name */
590 program_and_args
, /* command line */
593 TRUE
, /* inherit handles */
594 flags
, /* start flags */
595 NULL
, /* environment */
596 /* current directory */
597 (inferior_cwd
== NULL
599 : gdb_tilde_expand (inferior_cwd
).c_str()),
600 &si
, /* start info */
607 /* Start a new process.
608 PROGRAM is the program name.
609 PROGRAM_ARGS is the vector containing the inferior's args.
610 Returns the new PID on success, -1 on failure. Registers the new
611 process with the process list. */
613 win32_process_target::create_inferior (const char *program
,
614 const std::vector
<char *> &program_args
)
616 client_state
&cs
= get_client_state ();
618 char real_path
[PATH_MAX
];
619 char *orig_path
, *new_path
, *path_ptr
;
623 PROCESS_INFORMATION pi
;
625 std::string str_program_args
= stringify_argv (program_args
);
626 char *args
= (char *) str_program_args
.c_str ();
628 /* win32_wait needs to know we're not attaching. */
632 error ("No executable specified, specify executable to debug.\n");
634 flags
= DEBUG_PROCESS
| DEBUG_ONLY_THIS_PROCESS
;
638 path_ptr
= getenv ("PATH");
641 int size
= cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, NULL
, 0);
642 orig_path
= (char *) alloca (strlen (path_ptr
) + 1);
643 new_path
= (char *) alloca (size
);
644 strcpy (orig_path
, path_ptr
);
645 cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, new_path
, size
);
646 setenv ("PATH", new_path
, 1);
648 cygwin_conv_path (CCP_POSIX_TO_WIN_A
, program
, real_path
, PATH_MAX
);
652 OUTMSG2 (("Command line is \"%s %s\"\n", program
, args
));
654 #ifdef CREATE_NEW_PROCESS_GROUP
655 flags
|= CREATE_NEW_PROCESS_GROUP
;
658 ret
= create_process (program
, args
, flags
, &pi
);
659 err
= GetLastError ();
660 if (!ret
&& err
== ERROR_FILE_NOT_FOUND
)
662 char *exename
= (char *) alloca (strlen (program
) + 5);
663 strcat (strcpy (exename
, program
), ".exe");
664 ret
= create_process (exename
, args
, flags
, &pi
);
665 err
= GetLastError ();
670 setenv ("PATH", orig_path
, 1);
675 error ("Error creating process \"%s %s\", (error %d): %s\n",
676 program
, args
, (int) err
, strwinerror (err
));
680 OUTMSG2 (("Process created: %s %s\n", program
, (char *) args
));
684 /* On Windows CE this handle can't be closed. The OS reuses
685 it in the debug events, while the 9x/NT versions of Windows
686 probably use a DuplicateHandle'd one. */
687 CloseHandle (pi
.hThread
);
690 do_initial_child_stuff (pi
.hProcess
, pi
.dwProcessId
, 0);
692 /* Wait till we are at 1st instruction in program, return new pid
693 (assuming success). */
694 cs
.last_ptid
= wait (ptid_t (current_process_id
), &cs
.last_status
, 0);
696 /* Necessary for handle_v_kill. */
697 signal_pid
= current_process_id
;
699 return current_process_id
;
702 /* Attach to a running process.
703 PID is the process ID to attach to, specified by the user
704 or a higher layer. */
706 win32_process_target::attach (unsigned long pid
)
709 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
712 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
714 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
716 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
718 h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
721 if (DebugActiveProcess (pid
))
723 if (DebugSetProcessKillOnExit
!= NULL
)
724 DebugSetProcessKillOnExit (FALSE
);
726 /* win32_wait needs to know we're attaching. */
728 do_initial_child_stuff (h
, pid
, 1);
735 err
= GetLastError ();
736 error ("Attach to process failed (error %d): %s\n",
737 (int) err
, strwinerror (err
));
740 /* Handle OUTPUT_DEBUG_STRING_EVENT from child process. */
742 handle_output_debug_string (void)
744 #define READ_BUFFER_LEN 1024
746 char s
[READ_BUFFER_LEN
+ 1] = { 0 };
747 DWORD nbytes
= current_event
.u
.DebugString
.nDebugStringLength
;
752 if (nbytes
> READ_BUFFER_LEN
)
753 nbytes
= READ_BUFFER_LEN
;
755 addr
= (CORE_ADDR
) (size_t) current_event
.u
.DebugString
.lpDebugStringData
;
757 if (current_event
.u
.DebugString
.fUnicode
)
759 /* The event tells us how many bytes, not chars, even
761 WCHAR buffer
[(READ_BUFFER_LEN
+ 1) / sizeof (WCHAR
)] = { 0 };
762 if (read_inferior_memory (addr
, (unsigned char *) buffer
, nbytes
) != 0)
764 wcstombs (s
, buffer
, (nbytes
+ 1) / sizeof (WCHAR
));
768 if (read_inferior_memory (addr
, (unsigned char *) s
, nbytes
) != 0)
772 if (!startswith (s
, "cYg"))
782 #undef READ_BUFFER_LEN
786 win32_clear_inferiors (void)
788 if (current_process_handle
!= NULL
)
789 CloseHandle (current_process_handle
);
791 for_each_thread (delete_thread_info
);
792 siginfo_er
.ExceptionCode
= 0;
796 /* Implementation of target_ops::kill. */
799 win32_process_target::kill (process_info
*process
)
801 TerminateProcess (current_process_handle
, 0);
804 if (!child_continue (DBG_CONTINUE
, -1))
806 if (!WaitForDebugEvent (¤t_event
, INFINITE
))
808 if (current_event
.dwDebugEventCode
== EXIT_PROCESS_DEBUG_EVENT
)
810 else if (current_event
.dwDebugEventCode
== OUTPUT_DEBUG_STRING_EVENT
)
811 handle_output_debug_string ();
814 win32_clear_inferiors ();
816 remove_process (process
);
820 /* Implementation of target_ops::detach. */
823 win32_process_target::detach (process_info
*process
)
825 winapi_DebugActiveProcessStop DebugActiveProcessStop
= NULL
;
826 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
828 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
830 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
832 DebugActiveProcessStop
= GETPROCADDRESS (dll
, DebugActiveProcessStop
);
833 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
835 if (DebugSetProcessKillOnExit
== NULL
836 || DebugActiveProcessStop
== NULL
)
840 struct thread_resume resume
;
841 resume
.thread
= minus_one_ptid
;
842 resume
.kind
= resume_continue
;
844 this->resume (&resume
, 1);
847 if (!DebugActiveProcessStop (current_process_id
))
850 DebugSetProcessKillOnExit (FALSE
);
851 remove_process (process
);
853 win32_clear_inferiors ();
858 win32_process_target::mourn (struct process_info
*process
)
860 remove_process (process
);
863 /* Implementation of target_ops::join. */
866 win32_process_target::join (int pid
)
868 HANDLE h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
871 WaitForSingleObject (h
, INFINITE
);
876 /* Return true iff the thread with thread ID TID is alive. */
878 win32_process_target::thread_alive (ptid_t ptid
)
880 /* Our thread list is reliable; don't bother to poll target
882 return find_thread_ptid (ptid
) != NULL
;
885 /* Resume the inferior process. RESUME_INFO describes how we want
888 win32_process_target::resume (thread_resume
*resume_info
, size_t n
)
893 windows_thread_info
*th
;
894 DWORD continue_status
= DBG_CONTINUE
;
897 /* This handles the very limited set of resume packets that GDB can
898 currently produce. */
900 if (n
== 1 && resume_info
[0].thread
== minus_one_ptid
)
905 /* Yes, we're ignoring resume_info[0].thread. It'd be tricky to make
906 the Windows resume code do the right thing for thread switching. */
907 tid
= current_event
.dwThreadId
;
909 if (resume_info
[0].thread
!= minus_one_ptid
)
911 sig
= gdb_signal_from_host (resume_info
[0].sig
);
912 step
= resume_info
[0].kind
== resume_step
;
920 if (sig
!= GDB_SIGNAL_0
)
922 if (current_event
.dwDebugEventCode
!= EXCEPTION_DEBUG_EVENT
)
924 OUTMSG (("Cannot continue with signal %s here.\n",
925 gdb_signal_to_string (sig
)));
927 else if (sig
== last_sig
)
928 continue_status
= DBG_EXCEPTION_NOT_HANDLED
;
930 OUTMSG (("Can only continue with received signal %s.\n",
931 gdb_signal_to_string (last_sig
)));
934 last_sig
= GDB_SIGNAL_0
;
936 /* Get context for the currently selected thread. */
937 ptid
= debug_event_ptid (¤t_event
);
938 th
= thread_rec (ptid
, FALSE
);
941 win32_prepare_to_resume (th
);
943 if (th
->context
.ContextFlags
)
945 /* Move register values from the inferior into the thread
946 context structure. */
947 regcache_invalidate ();
951 if (the_low_target
.single_step
!= NULL
)
952 (*the_low_target
.single_step
) (th
);
954 error ("Single stepping is not supported "
955 "in this configuration.\n");
958 win32_set_thread_context (th
);
959 th
->context
.ContextFlags
= 0;
963 /* Allow continuing with the same signal that interrupted us.
964 Otherwise complain. */
966 child_continue (continue_status
, tid
);
970 win32_add_one_solib (const char *name
, CORE_ADDR load_addr
)
972 char buf
[MAX_PATH
+ 1];
973 char buf2
[MAX_PATH
+ 1];
976 WIN32_FIND_DATA w32_fd
;
977 WCHAR wname
[MAX_PATH
+ 1];
978 mbstowcs (wname
, name
, MAX_PATH
);
979 HANDLE h
= FindFirstFile (wname
, &w32_fd
);
981 WIN32_FIND_DATAA w32_fd
;
982 HANDLE h
= FindFirstFileA (name
, &w32_fd
);
985 /* The symbols in a dll are offset by 0x1000, which is the
986 offset from 0 of the first byte in an image - because
987 of the file header and the section alignment. */
990 if (h
== INVALID_HANDLE_VALUE
)
998 char cwd
[MAX_PATH
+ 1];
1000 if (GetCurrentDirectoryA (MAX_PATH
+ 1, cwd
))
1002 p
= strrchr (buf
, '\\');
1005 SetCurrentDirectoryA (buf
);
1006 GetFullPathNameA (w32_fd
.cFileName
, MAX_PATH
, buf
, &p
);
1007 SetCurrentDirectoryA (cwd
);
1014 if (strcasecmp (buf
, "ntdll.dll") == 0)
1016 GetSystemDirectoryA (buf
, sizeof (buf
));
1017 strcat (buf
, "\\ntdll.dll");
1022 cygwin_conv_path (CCP_WIN_A_TO_POSIX
, buf
, buf2
, sizeof (buf2
));
1027 loaded_dll (buf2
, load_addr
);
1031 get_image_name (HANDLE h
, void *address
, int unicode
)
1033 static char buf
[(2 * MAX_PATH
) + 1];
1034 DWORD size
= unicode
? sizeof (WCHAR
) : sizeof (char);
1040 /* Attempt to read the name of the dll that was detected.
1041 This is documented to work only when actively debugging
1042 a program. It will not work for attached processes. */
1043 if (address
== NULL
)
1047 /* Windows CE reports the address of the image name,
1048 instead of an address of a pointer into the image name. */
1049 address_ptr
= address
;
1051 /* See if we could read the address of a string, and that the
1052 address isn't null. */
1053 if (!ReadProcessMemory (h
, address
, &address_ptr
,
1054 sizeof (address_ptr
), &done
)
1055 || done
!= sizeof (address_ptr
)
1060 /* Find the length of the string */
1061 while (ReadProcessMemory (h
, address_ptr
+ len
++ * size
, &b
, size
, &done
)
1062 && (b
[0] != 0 || b
[size
- 1] != 0) && done
== size
)
1066 ReadProcessMemory (h
, address_ptr
, buf
, len
, &done
);
1069 WCHAR
*unicode_address
= XALLOCAVEC (WCHAR
, len
);
1070 ReadProcessMemory (h
, address_ptr
, unicode_address
, len
* sizeof (WCHAR
),
1073 WideCharToMultiByte (CP_ACP
, 0, unicode_address
, len
, buf
, len
, 0, 0);
1079 typedef BOOL (WINAPI
*winapi_EnumProcessModules
) (HANDLE
, HMODULE
*,
1081 typedef BOOL (WINAPI
*winapi_GetModuleInformation
) (HANDLE
, HMODULE
,
1082 LPMODULEINFO
, DWORD
);
1083 typedef DWORD (WINAPI
*winapi_GetModuleFileNameExA
) (HANDLE
, HMODULE
,
1086 static winapi_EnumProcessModules win32_EnumProcessModules
;
1087 static winapi_GetModuleInformation win32_GetModuleInformation
;
1088 static winapi_GetModuleFileNameExA win32_GetModuleFileNameExA
;
1093 static int psapi_loaded
= 0;
1094 static HMODULE dll
= NULL
;
1099 dll
= LoadLibrary (TEXT("psapi.dll"));
1102 win32_EnumProcessModules
=
1103 GETPROCADDRESS (dll
, EnumProcessModules
);
1104 win32_GetModuleInformation
=
1105 GETPROCADDRESS (dll
, GetModuleInformation
);
1106 win32_GetModuleFileNameExA
=
1107 GETPROCADDRESS (dll
, GetModuleFileNameExA
);
1110 return (win32_EnumProcessModules
!= NULL
1111 && win32_GetModuleInformation
!= NULL
1112 && win32_GetModuleFileNameExA
!= NULL
);
1117 /* Iterate over all DLLs currently mapped by our inferior, and
1118 add them to our list of solibs. */
1121 win32_add_all_dlls (void)
1125 HMODULE
*DllHandle
= dh_buf
;
1133 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1138 if (!ok
|| !cbNeeded
)
1141 DllHandle
= (HMODULE
*) alloca (cbNeeded
);
1145 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1152 for (i
= 1; i
< ((size_t) cbNeeded
/ sizeof (HMODULE
)); i
++)
1155 char dll_name
[MAX_PATH
];
1157 if (!(*win32_GetModuleInformation
) (current_process_handle
,
1162 if ((*win32_GetModuleFileNameExA
) (current_process_handle
,
1167 win32_add_one_solib (dll_name
, (CORE_ADDR
) (uintptr_t) mi
.lpBaseOfDll
);
1172 typedef HANDLE (WINAPI
*winapi_CreateToolhelp32Snapshot
) (DWORD
, DWORD
);
1173 typedef BOOL (WINAPI
*winapi_Module32First
) (HANDLE
, LPMODULEENTRY32
);
1174 typedef BOOL (WINAPI
*winapi_Module32Next
) (HANDLE
, LPMODULEENTRY32
);
1176 /* Handle a DLL load event.
1178 This function assumes that this event did not occur during inferior
1179 initialization, where their event info may be incomplete (see
1180 do_initial_child_stuff and win32_add_all_dlls for more info on
1181 how we handle DLL loading during that phase). */
1184 handle_load_dll (void)
1186 LOAD_DLL_DEBUG_INFO
*event
= ¤t_event
.u
.LoadDll
;
1189 dll_name
= get_image_name (current_process_handle
,
1190 event
->lpImageName
, event
->fUnicode
);
1194 win32_add_one_solib (dll_name
, (CORE_ADDR
) (uintptr_t) event
->lpBaseOfDll
);
1197 /* Handle a DLL unload event.
1199 This function assumes that this event did not occur during inferior
1200 initialization, where their event info may be incomplete (see
1201 do_initial_child_stuff and win32_add_one_solib for more info
1202 on how we handle DLL loading during that phase). */
1205 handle_unload_dll (void)
1207 CORE_ADDR load_addr
=
1208 (CORE_ADDR
) (uintptr_t) current_event
.u
.UnloadDll
.lpBaseOfDll
;
1210 /* The symbols in a dll are offset by 0x1000, which is the
1211 offset from 0 of the first byte in an image - because
1212 of the file header and the section alignment. */
1213 load_addr
+= 0x1000;
1214 unloaded_dll (NULL
, load_addr
);
1218 handle_exception (struct target_waitstatus
*ourstatus
)
1220 DWORD code
= current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
;
1222 memcpy (&siginfo_er
, ¤t_event
.u
.Exception
.ExceptionRecord
,
1225 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
1229 case EXCEPTION_ACCESS_VIOLATION
:
1230 OUTMSG2 (("EXCEPTION_ACCESS_VIOLATION"));
1231 ourstatus
->value
.sig
= GDB_SIGNAL_SEGV
;
1233 case STATUS_STACK_OVERFLOW
:
1234 OUTMSG2 (("STATUS_STACK_OVERFLOW"));
1235 ourstatus
->value
.sig
= GDB_SIGNAL_SEGV
;
1237 case STATUS_FLOAT_DENORMAL_OPERAND
:
1238 OUTMSG2 (("STATUS_FLOAT_DENORMAL_OPERAND"));
1239 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1241 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
1242 OUTMSG2 (("EXCEPTION_ARRAY_BOUNDS_EXCEEDED"));
1243 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1245 case STATUS_FLOAT_INEXACT_RESULT
:
1246 OUTMSG2 (("STATUS_FLOAT_INEXACT_RESULT"));
1247 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1249 case STATUS_FLOAT_INVALID_OPERATION
:
1250 OUTMSG2 (("STATUS_FLOAT_INVALID_OPERATION"));
1251 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1253 case STATUS_FLOAT_OVERFLOW
:
1254 OUTMSG2 (("STATUS_FLOAT_OVERFLOW"));
1255 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1257 case STATUS_FLOAT_STACK_CHECK
:
1258 OUTMSG2 (("STATUS_FLOAT_STACK_CHECK"));
1259 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1261 case STATUS_FLOAT_UNDERFLOW
:
1262 OUTMSG2 (("STATUS_FLOAT_UNDERFLOW"));
1263 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1265 case STATUS_FLOAT_DIVIDE_BY_ZERO
:
1266 OUTMSG2 (("STATUS_FLOAT_DIVIDE_BY_ZERO"));
1267 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1269 case STATUS_INTEGER_DIVIDE_BY_ZERO
:
1270 OUTMSG2 (("STATUS_INTEGER_DIVIDE_BY_ZERO"));
1271 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1273 case STATUS_INTEGER_OVERFLOW
:
1274 OUTMSG2 (("STATUS_INTEGER_OVERFLOW"));
1275 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1277 case EXCEPTION_BREAKPOINT
:
1278 OUTMSG2 (("EXCEPTION_BREAKPOINT"));
1279 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1281 /* Remove the initial breakpoint. */
1282 check_breakpoints ((CORE_ADDR
) (long) current_event
1283 .u
.Exception
.ExceptionRecord
.ExceptionAddress
);
1287 OUTMSG2 (("DBG_CONTROL_C"));
1288 ourstatus
->value
.sig
= GDB_SIGNAL_INT
;
1290 case DBG_CONTROL_BREAK
:
1291 OUTMSG2 (("DBG_CONTROL_BREAK"));
1292 ourstatus
->value
.sig
= GDB_SIGNAL_INT
;
1294 case EXCEPTION_SINGLE_STEP
:
1295 OUTMSG2 (("EXCEPTION_SINGLE_STEP"));
1296 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1298 case EXCEPTION_ILLEGAL_INSTRUCTION
:
1299 OUTMSG2 (("EXCEPTION_ILLEGAL_INSTRUCTION"));
1300 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1302 case EXCEPTION_PRIV_INSTRUCTION
:
1303 OUTMSG2 (("EXCEPTION_PRIV_INSTRUCTION"));
1304 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1306 case EXCEPTION_NONCONTINUABLE_EXCEPTION
:
1307 OUTMSG2 (("EXCEPTION_NONCONTINUABLE_EXCEPTION"));
1308 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1311 if (current_event
.u
.Exception
.dwFirstChance
)
1313 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1316 OUTMSG2 (("gdbserver: unknown target exception 0x%08x at 0x%s",
1317 (unsigned) current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
,
1318 phex_nz ((uintptr_t) current_event
.u
.Exception
.ExceptionRecord
.
1319 ExceptionAddress
, sizeof (uintptr_t))));
1320 ourstatus
->value
.sig
= GDB_SIGNAL_UNKNOWN
;
1324 last_sig
= ourstatus
->value
.sig
;
1329 suspend_one_thread (thread_info
*thread
)
1331 windows_thread_info
*th
= (windows_thread_info
*) thread_target_data (thread
);
1337 fake_breakpoint_event (void)
1339 OUTMSG2(("fake_breakpoint_event\n"));
1341 faked_breakpoint
= 1;
1343 memset (¤t_event
, 0, sizeof (current_event
));
1344 current_event
.dwThreadId
= main_thread_id
;
1345 current_event
.dwDebugEventCode
= EXCEPTION_DEBUG_EVENT
;
1346 current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
1347 = EXCEPTION_BREAKPOINT
;
1349 for_each_thread (suspend_one_thread
);
1354 auto_delete_breakpoint (CORE_ADDR stop_pc
)
1360 /* Get the next event from the child. */
1363 get_child_debug_event (struct target_waitstatus
*ourstatus
)
1367 last_sig
= GDB_SIGNAL_0
;
1368 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1370 /* Check if GDB sent us an interrupt request. */
1371 check_remote_input_interrupt_request ();
1373 if (soft_interrupt_requested
)
1375 soft_interrupt_requested
= 0;
1376 fake_breakpoint_event ();
1385 /* WinCE doesn't set an initial breakpoint automatically. To
1386 stop the inferior, we flush all currently pending debug
1387 events -- the thread list and the dll list are always
1388 reported immediatelly without delay, then, we suspend all
1389 threads and pretend we saw a trap at the current PC of the
1392 Contrary to desktop Windows, Windows CE *does* report the dll
1393 names on LOAD_DLL_DEBUG_EVENTs resulting from a
1394 DebugActiveProcess call. This limits the way we can detect
1395 if all the dlls have already been reported. If we get a real
1396 debug event before leaving attaching, the worst that will
1397 happen is the user will see a spurious breakpoint. */
1399 current_event
.dwDebugEventCode
= 0;
1400 if (!WaitForDebugEvent (¤t_event
, 0))
1402 OUTMSG2(("no attach events left\n"));
1403 fake_breakpoint_event ();
1407 OUTMSG2(("got attach event\n"));
1412 /* Keep the wait time low enough for comfortable remote
1413 interruption, but high enough so gdbserver doesn't become a
1415 if (!WaitForDebugEvent (¤t_event
, 250))
1417 DWORD e
= GetLastError();
1419 if (e
== ERROR_PIPE_NOT_CONNECTED
)
1421 /* This will happen if the loader fails to succesfully
1422 load the application, e.g., if the main executable
1423 tries to pull in a non-existing export from a
1425 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1426 ourstatus
->value
.integer
= 1;
1436 switch (current_event
.dwDebugEventCode
)
1438 case CREATE_THREAD_DEBUG_EVENT
:
1439 OUTMSG2 (("gdbserver: kernel event CREATE_THREAD_DEBUG_EVENT "
1440 "for pid=%u tid=%x)\n",
1441 (unsigned) current_event
.dwProcessId
,
1442 (unsigned) current_event
.dwThreadId
));
1444 /* Record the existence of this thread. */
1445 child_add_thread (current_event
.dwProcessId
,
1446 current_event
.dwThreadId
,
1447 current_event
.u
.CreateThread
.hThread
,
1448 current_event
.u
.CreateThread
.lpThreadLocalBase
);
1451 case EXIT_THREAD_DEBUG_EVENT
:
1452 OUTMSG2 (("gdbserver: kernel event EXIT_THREAD_DEBUG_EVENT "
1453 "for pid=%u tid=%x\n",
1454 (unsigned) current_event
.dwProcessId
,
1455 (unsigned) current_event
.dwThreadId
));
1456 child_delete_thread (current_event
.dwProcessId
,
1457 current_event
.dwThreadId
);
1459 current_thread
= get_first_thread ();
1462 case CREATE_PROCESS_DEBUG_EVENT
:
1463 OUTMSG2 (("gdbserver: kernel event CREATE_PROCESS_DEBUG_EVENT "
1464 "for pid=%u tid=%x\n",
1465 (unsigned) current_event
.dwProcessId
,
1466 (unsigned) current_event
.dwThreadId
));
1467 CloseHandle (current_event
.u
.CreateProcessInfo
.hFile
);
1469 current_process_handle
= current_event
.u
.CreateProcessInfo
.hProcess
;
1470 main_thread_id
= current_event
.dwThreadId
;
1472 /* Add the main thread. */
1473 child_add_thread (current_event
.dwProcessId
,
1475 current_event
.u
.CreateProcessInfo
.hThread
,
1476 current_event
.u
.CreateProcessInfo
.lpThreadLocalBase
);
1481 /* Windows CE doesn't set the initial breakpoint
1482 automatically like the desktop versions of Windows do.
1483 We add it explicitly here. It will be removed as soon as
1485 set_breakpoint_at ((CORE_ADDR
) (long) current_event
.u
1486 .CreateProcessInfo
.lpStartAddress
,
1487 auto_delete_breakpoint
);
1492 case EXIT_PROCESS_DEBUG_EVENT
:
1493 OUTMSG2 (("gdbserver: kernel event EXIT_PROCESS_DEBUG_EVENT "
1494 "for pid=%u tid=%x\n",
1495 (unsigned) current_event
.dwProcessId
,
1496 (unsigned) current_event
.dwThreadId
));
1498 DWORD exit_status
= current_event
.u
.ExitProcess
.dwExitCode
;
1499 /* If the exit status looks like a fatal exception, but we
1500 don't recognize the exception's code, make the original
1501 exit status value available, to avoid losing information. */
1503 = WIFSIGNALED (exit_status
) ? WTERMSIG (exit_status
) : -1;
1504 if (exit_signal
== -1)
1506 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1507 ourstatus
->value
.integer
= exit_status
;
1511 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
1512 ourstatus
->value
.sig
= gdb_signal_from_host (exit_signal
);
1515 child_continue (DBG_CONTINUE
, -1);
1516 CloseHandle (current_process_handle
);
1517 current_process_handle
= NULL
;
1520 case LOAD_DLL_DEBUG_EVENT
:
1521 OUTMSG2 (("gdbserver: kernel event LOAD_DLL_DEBUG_EVENT "
1522 "for pid=%u tid=%x\n",
1523 (unsigned) current_event
.dwProcessId
,
1524 (unsigned) current_event
.dwThreadId
));
1525 CloseHandle (current_event
.u
.LoadDll
.hFile
);
1526 if (! child_initialization_done
)
1530 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1531 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1534 case UNLOAD_DLL_DEBUG_EVENT
:
1535 OUTMSG2 (("gdbserver: kernel event UNLOAD_DLL_DEBUG_EVENT "
1536 "for pid=%u tid=%x\n",
1537 (unsigned) current_event
.dwProcessId
,
1538 (unsigned) current_event
.dwThreadId
));
1539 if (! child_initialization_done
)
1541 handle_unload_dll ();
1542 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1543 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1546 case EXCEPTION_DEBUG_EVENT
:
1547 OUTMSG2 (("gdbserver: kernel event EXCEPTION_DEBUG_EVENT "
1548 "for pid=%u tid=%x\n",
1549 (unsigned) current_event
.dwProcessId
,
1550 (unsigned) current_event
.dwThreadId
));
1551 handle_exception (ourstatus
);
1554 case OUTPUT_DEBUG_STRING_EVENT
:
1555 /* A message from the kernel (or Cygwin). */
1556 OUTMSG2 (("gdbserver: kernel event OUTPUT_DEBUG_STRING_EVENT "
1557 "for pid=%u tid=%x\n",
1558 (unsigned) current_event
.dwProcessId
,
1559 (unsigned) current_event
.dwThreadId
));
1560 handle_output_debug_string ();
1564 OUTMSG2 (("gdbserver: kernel event unknown "
1565 "for pid=%u tid=%x code=%x\n",
1566 (unsigned) current_event
.dwProcessId
,
1567 (unsigned) current_event
.dwThreadId
,
1568 (unsigned) current_event
.dwDebugEventCode
));
1572 ptid
= debug_event_ptid (¤t_event
);
1573 current_thread
= find_thread_ptid (ptid
);
1577 /* Wait for the inferior process to change state.
1578 STATUS will be filled in with a response code to send to GDB.
1579 Returns the signal which caused the process to stop. */
1581 win32_process_target::wait (ptid_t ptid
, target_waitstatus
*ourstatus
,
1584 struct regcache
*regcache
;
1586 if (cached_status
.kind
!= TARGET_WAITKIND_IGNORE
)
1588 /* The core always does a wait after creating the inferior, and
1589 do_initial_child_stuff already ran the inferior to the
1590 initial breakpoint (or an exit, if creating the process
1591 fails). Report it now. */
1592 *ourstatus
= cached_status
;
1593 cached_status
.kind
= TARGET_WAITKIND_IGNORE
;
1594 return debug_event_ptid (¤t_event
);
1599 if (!get_child_debug_event (ourstatus
))
1602 switch (ourstatus
->kind
)
1604 case TARGET_WAITKIND_EXITED
:
1605 OUTMSG2 (("Child exited with retcode = %x\n",
1606 ourstatus
->value
.integer
));
1607 win32_clear_inferiors ();
1608 return ptid_t (current_event
.dwProcessId
);
1609 case TARGET_WAITKIND_STOPPED
:
1610 case TARGET_WAITKIND_SIGNALLED
:
1611 case TARGET_WAITKIND_LOADED
:
1612 OUTMSG2 (("Child Stopped with signal = %d \n",
1613 ourstatus
->value
.sig
));
1615 regcache
= get_thread_regcache (current_thread
, 1);
1616 child_fetch_inferior_registers (regcache
, -1);
1617 return debug_event_ptid (¤t_event
);
1619 OUTMSG (("Ignoring unknown internal event, %d\n", ourstatus
->kind
));
1621 case TARGET_WAITKIND_SPURIOUS
:
1622 /* do nothing, just continue */
1623 child_continue (DBG_CONTINUE
, -1);
1629 /* Fetch registers from the inferior process.
1630 If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO. */
1632 win32_process_target::fetch_registers (regcache
*regcache
, int regno
)
1634 child_fetch_inferior_registers (regcache
, regno
);
1637 /* Store registers to the inferior process.
1638 If REGNO is -1, store all registers; otherwise, store at least REGNO. */
1640 win32_process_target::store_registers (regcache
*regcache
, int regno
)
1642 child_store_inferior_registers (regcache
, regno
);
1645 /* Read memory from the inferior process. This should generally be
1646 called through read_inferior_memory, which handles breakpoint shadowing.
1647 Read LEN bytes at MEMADDR into a buffer at MYADDR. */
1649 win32_process_target::read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
,
1652 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 0, 0) != len
;
1655 /* Write memory to the inferior process. This should generally be
1656 called through write_inferior_memory, which handles breakpoint shadowing.
1657 Write LEN bytes from the buffer at MYADDR to MEMADDR.
1658 Returns 0 on success and errno on failure. */
1660 win32_process_target::write_memory (CORE_ADDR memaddr
,
1661 const unsigned char *myaddr
, int len
)
1663 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 1, 0) != len
;
1666 /* Send an interrupt request to the inferior process. */
1668 win32_process_target::request_interrupt ()
1670 winapi_DebugBreakProcess DebugBreakProcess
;
1671 winapi_GenerateConsoleCtrlEvent GenerateConsoleCtrlEvent
;
1674 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
1676 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
1679 GenerateConsoleCtrlEvent
= GETPROCADDRESS (dll
, GenerateConsoleCtrlEvent
);
1681 if (GenerateConsoleCtrlEvent
!= NULL
1682 && GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT
, current_process_id
))
1685 /* GenerateConsoleCtrlEvent can fail if process id being debugged is
1686 not a process group id.
1687 Fallback to XP/Vista 'DebugBreakProcess', which generates a
1688 breakpoint exception in the interior process. */
1690 DebugBreakProcess
= GETPROCADDRESS (dll
, DebugBreakProcess
);
1692 if (DebugBreakProcess
!= NULL
1693 && DebugBreakProcess (current_process_handle
))
1696 /* Last resort, suspend all threads manually. */
1697 soft_interrupt_requested
= 1;
1701 win32_process_target::supports_hardware_single_step ()
1708 win32_error_to_fileio_error (DWORD err
)
1712 case ERROR_BAD_PATHNAME
:
1713 case ERROR_FILE_NOT_FOUND
:
1714 case ERROR_INVALID_NAME
:
1715 case ERROR_PATH_NOT_FOUND
:
1716 return FILEIO_ENOENT
;
1718 case ERROR_IO_DEVICE
:
1719 case ERROR_OPEN_FAILED
:
1721 case ERROR_INVALID_HANDLE
:
1722 return FILEIO_EBADF
;
1723 case ERROR_ACCESS_DENIED
:
1724 case ERROR_SHARING_VIOLATION
:
1725 return FILEIO_EACCES
;
1726 case ERROR_NOACCESS
:
1727 return FILEIO_EFAULT
;
1729 return FILEIO_EBUSY
;
1730 case ERROR_ALREADY_EXISTS
:
1731 case ERROR_FILE_EXISTS
:
1732 return FILEIO_EEXIST
;
1733 case ERROR_BAD_DEVICE
:
1734 return FILEIO_ENODEV
;
1735 case ERROR_DIRECTORY
:
1736 return FILEIO_ENOTDIR
;
1737 case ERROR_FILENAME_EXCED_RANGE
:
1738 case ERROR_INVALID_DATA
:
1739 case ERROR_INVALID_PARAMETER
:
1740 case ERROR_NEGATIVE_SEEK
:
1741 return FILEIO_EINVAL
;
1742 case ERROR_TOO_MANY_OPEN_FILES
:
1743 return FILEIO_EMFILE
;
1744 case ERROR_HANDLE_DISK_FULL
:
1745 case ERROR_DISK_FULL
:
1746 return FILEIO_ENOSPC
;
1747 case ERROR_WRITE_PROTECT
:
1748 return FILEIO_EROFS
;
1749 case ERROR_NOT_SUPPORTED
:
1750 return FILEIO_ENOSYS
;
1753 return FILEIO_EUNKNOWN
;
1757 win32_process_target::hostio_last_error (char *buf
)
1759 DWORD winerr
= GetLastError ();
1760 int fileio_err
= win32_error_to_fileio_error (winerr
);
1761 sprintf (buf
, "F-1,%x", fileio_err
);
1766 win32_process_target::supports_qxfer_siginfo ()
1771 /* Write Windows signal info. */
1774 win32_process_target::qxfer_siginfo (const char *annex
,
1775 unsigned char *readbuf
,
1776 unsigned const char *writebuf
,
1777 CORE_ADDR offset
, int len
)
1779 if (siginfo_er
.ExceptionCode
== 0)
1782 if (readbuf
== nullptr)
1785 if (offset
> sizeof (siginfo_er
))
1788 if (offset
+ len
> sizeof (siginfo_er
))
1789 len
= sizeof (siginfo_er
) - offset
;
1791 memcpy (readbuf
, (char *) &siginfo_er
+ offset
, len
);
1797 win32_process_target::supports_get_tib_address ()
1802 /* Write Windows OS Thread Information Block address. */
1805 win32_process_target::get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
)
1807 windows_thread_info
*th
;
1808 th
= thread_rec (ptid
, 0);
1812 *addr
= th
->thread_local_base
;
1816 /* Implementation of the target_ops method "sw_breakpoint_from_kind". */
1819 win32_process_target::sw_breakpoint_from_kind (int kind
, int *size
)
1821 *size
= the_low_target
.breakpoint_len
;
1822 return the_low_target
.breakpoint
;
1825 /* The win32 target ops object. */
1827 static win32_process_target the_win32_target
;
1829 /* Initialize the Win32 backend. */
1831 initialize_low (void)
1833 set_target_ops (&the_win32_target
);
1834 the_low_target
.arch_setup ();