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 enum gdb_signal last_sig
= GDB_SIGNAL_0
;
80 /* The current debug event from WaitForDebugEvent. */
81 static DEBUG_EVENT current_event
;
83 /* A status that hasn't been reported to the core yet, and so
84 win32_wait should return it next, instead of fetching the next
85 debug event off the win32 API. */
86 static struct target_waitstatus cached_status
;
88 /* Non zero if an interrupt request is to be satisfied by suspending
90 static int soft_interrupt_requested
= 0;
92 /* Non zero if the inferior is stopped in a simulated breakpoint done
93 by suspending all the threads. */
94 static int faked_breakpoint
= 0;
96 const struct target_desc
*win32_tdesc
;
98 #define NUM_REGS (the_low_target.num_regs)
100 typedef BOOL (WINAPI
*winapi_DebugActiveProcessStop
) (DWORD dwProcessId
);
101 typedef BOOL (WINAPI
*winapi_DebugSetProcessKillOnExit
) (BOOL KillOnExit
);
102 typedef BOOL (WINAPI
*winapi_DebugBreakProcess
) (HANDLE
);
103 typedef BOOL (WINAPI
*winapi_GenerateConsoleCtrlEvent
) (DWORD
, DWORD
);
105 static ptid_t
win32_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
,
107 static void win32_resume (struct thread_resume
*resume_info
, size_t n
);
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 (win32_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 (win32_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 (win32_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 (win32_thread_info
*th
)
174 if (th
->context
.ContextFlags
== 0)
178 if (SuspendThread (th
->h
) == (DWORD
) -1)
180 DWORD err
= GetLastError ();
181 OUTMSG (("warning: SuspendThread failed in thread_rec, "
182 "(error %d): %s\n", (int) err
, strwinerror (err
)));
188 win32_get_thread_context (th
);
192 /* Find a thread record given a thread id. If GET_CONTEXT is set then
193 also retrieve the context for this thread. */
194 static win32_thread_info
*
195 thread_rec (ptid_t ptid
, int get_context
)
197 thread_info
*thread
= find_thread_ptid (ptid
);
201 win32_thread_info
*th
= (win32_thread_info
*) thread_target_data (thread
);
203 win32_require_context (th
);
207 /* Add a thread to the thread list. */
208 static win32_thread_info
*
209 child_add_thread (DWORD pid
, DWORD tid
, HANDLE h
, void *tlb
)
211 win32_thread_info
*th
;
212 ptid_t ptid
= ptid_t (pid
, tid
, 0);
214 if ((th
= thread_rec (ptid
, FALSE
)))
217 th
= XCNEW (win32_thread_info
);
220 th
->thread_local_base
= (CORE_ADDR
) (uintptr_t) tlb
;
222 add_thread (ptid
, th
);
224 if (the_low_target
.thread_added
!= NULL
)
225 (*the_low_target
.thread_added
) (th
);
230 /* Delete a thread from the list of threads. */
232 delete_thread_info (thread_info
*thread
)
234 win32_thread_info
*th
= (win32_thread_info
*) thread_target_data (thread
);
236 remove_thread (thread
);
241 /* Delete a thread from the list of threads. */
243 child_delete_thread (DWORD pid
, DWORD tid
)
245 /* If the last thread is exiting, just return. */
246 if (all_threads
.size () == 1)
249 thread_info
*thread
= find_thread_ptid (ptid_t (pid
, tid
));
253 delete_thread_info (thread
);
256 /* These watchpoint related wrapper functions simply pass on the function call
257 if the low target has registered a corresponding function. */
260 win32_supports_z_point_type (char z_type
)
262 return (the_low_target
.supports_z_point_type
!= NULL
263 && the_low_target
.supports_z_point_type (z_type
));
267 win32_insert_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
268 int size
, struct raw_breakpoint
*bp
)
270 if (the_low_target
.insert_point
!= NULL
)
271 return the_low_target
.insert_point (type
, addr
, size
, bp
);
273 /* Unsupported (see target.h). */
278 win32_remove_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
279 int size
, struct raw_breakpoint
*bp
)
281 if (the_low_target
.remove_point
!= NULL
)
282 return the_low_target
.remove_point (type
, addr
, size
, bp
);
284 /* Unsupported (see target.h). */
289 win32_stopped_by_watchpoint (void)
291 if (the_low_target
.stopped_by_watchpoint
!= NULL
)
292 return the_low_target
.stopped_by_watchpoint ();
298 win32_stopped_data_address (void)
300 if (the_low_target
.stopped_data_address
!= NULL
)
301 return the_low_target
.stopped_data_address ();
307 /* Transfer memory from/to the debugged process. */
309 child_xfer_memory (CORE_ADDR memaddr
, char *our
, int len
,
310 int write
, struct target_ops
*target
)
315 uintptr_t addr
= (uintptr_t) memaddr
;
319 success
= WriteProcessMemory (current_process_handle
, (LPVOID
) addr
,
320 (LPCVOID
) our
, len
, &done
);
322 lasterror
= GetLastError ();
323 FlushInstructionCache (current_process_handle
, (LPCVOID
) addr
, len
);
327 success
= ReadProcessMemory (current_process_handle
, (LPCVOID
) addr
,
328 (LPVOID
) our
, len
, &done
);
330 lasterror
= GetLastError ();
332 if (!success
&& lasterror
== ERROR_PARTIAL_COPY
&& done
> 0)
335 return success
? done
: -1;
338 /* Clear out any old thread list and reinitialize it to a pristine
341 child_init_thread_list (void)
343 for_each_thread (delete_thread_info
);
346 /* Zero during the child initialization phase, and nonzero otherwise. */
348 static int child_initialization_done
= 0;
351 do_initial_child_stuff (HANDLE proch
, DWORD pid
, int attached
)
353 struct process_info
*proc
;
355 last_sig
= GDB_SIGNAL_0
;
357 current_process_handle
= proch
;
358 current_process_id
= pid
;
361 soft_interrupt_requested
= 0;
362 faked_breakpoint
= 0;
364 memset (¤t_event
, 0, sizeof (current_event
));
366 proc
= add_process (pid
, attached
);
367 proc
->tdesc
= win32_tdesc
;
368 child_init_thread_list ();
369 child_initialization_done
= 0;
371 if (the_low_target
.initial_stuff
!= NULL
)
372 (*the_low_target
.initial_stuff
) ();
374 cached_status
.kind
= TARGET_WAITKIND_IGNORE
;
376 /* Flush all currently pending debug events (thread and dll list) up
377 to the initial breakpoint. */
380 struct target_waitstatus status
;
382 win32_wait (minus_one_ptid
, &status
, 0);
384 /* Note win32_wait doesn't return thread events. */
385 if (status
.kind
!= TARGET_WAITKIND_LOADED
)
387 cached_status
= status
;
392 struct thread_resume resume
;
394 resume
.thread
= minus_one_ptid
;
395 resume
.kind
= resume_continue
;
398 win32_resume (&resume
, 1);
403 /* Now that the inferior has been started and all DLLs have been mapped,
404 we can iterate over all DLLs and load them in.
406 We avoid doing it any earlier because, on certain versions of Windows,
407 LOAD_DLL_DEBUG_EVENTs are sometimes not complete. In particular,
408 we have seen on Windows 8.1 that the ntdll.dll load event does not
409 include the DLL name, preventing us from creating an associated SO.
410 A possible explanation is that ntdll.dll might be mapped before
411 the SO info gets created by the Windows system -- ntdll.dll is
412 the first DLL to be reported via LOAD_DLL_DEBUG_EVENT and other DLLs
413 do not seem to suffer from that problem.
415 Rather than try to work around this sort of issue, it is much
416 simpler to just ignore DLL load/unload events during the startup
417 phase, and then process them all in one batch now. */
418 win32_add_all_dlls ();
421 child_initialization_done
= 1;
424 /* Resume all artificially suspended threads if we are continuing
427 continue_one_thread (thread_info
*thread
, int thread_id
)
429 win32_thread_info
*th
= (win32_thread_info
*) thread_target_data (thread
);
431 if (thread_id
== -1 || thread_id
== th
->tid
)
433 win32_prepare_to_resume (th
);
437 if (th
->context
.ContextFlags
)
439 win32_set_thread_context (th
);
440 th
->context
.ContextFlags
= 0;
443 if (ResumeThread (th
->h
) == (DWORD
) -1)
445 DWORD err
= GetLastError ();
446 OUTMSG (("warning: ResumeThread failed in continue_one_thread, "
447 "(error %d): %s\n", (int) err
, strwinerror (err
)));
455 child_continue (DWORD continue_status
, int thread_id
)
457 /* The inferior will only continue after the ContinueDebugEvent
459 for_each_thread ([&] (thread_info
*thread
)
461 continue_one_thread (thread
, thread_id
);
463 faked_breakpoint
= 0;
465 if (!ContinueDebugEvent (current_event
.dwProcessId
,
466 current_event
.dwThreadId
,
473 /* Fetch register(s) from the current thread context. */
475 child_fetch_inferior_registers (struct regcache
*regcache
, int r
)
478 win32_thread_info
*th
= thread_rec (current_thread_ptid (), TRUE
);
479 if (r
== -1 || r
> NUM_REGS
)
480 child_fetch_inferior_registers (regcache
, NUM_REGS
);
482 for (regno
= 0; regno
< r
; regno
++)
483 (*the_low_target
.fetch_inferior_register
) (regcache
, th
, regno
);
486 /* Store a new register value into the current thread context. We don't
487 change the program's context until later, when we resume it. */
489 child_store_inferior_registers (struct regcache
*regcache
, int r
)
492 win32_thread_info
*th
= thread_rec (current_thread_ptid (), TRUE
);
493 if (r
== -1 || r
== 0 || r
> NUM_REGS
)
494 child_store_inferior_registers (regcache
, NUM_REGS
);
496 for (regno
= 0; regno
< r
; regno
++)
497 (*the_low_target
.store_inferior_register
) (regcache
, th
, regno
);
500 /* Map the Windows error number in ERROR to a locale-dependent error
501 message string and return a pointer to it. Typically, the values
502 for ERROR come from GetLastError.
504 The string pointed to shall not be modified by the application,
505 but may be overwritten by a subsequent call to strwinerror
507 The strwinerror function does not change the current setting
511 strwinerror (DWORD error
)
513 static char buf
[1024];
515 DWORD lasterr
= GetLastError ();
516 DWORD chars
= FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
517 | FORMAT_MESSAGE_ALLOCATE_BUFFER
,
520 0, /* Default language */
526 /* If there is an \r\n appended, zap it. */
528 && msgbuf
[chars
- 2] == '\r'
529 && msgbuf
[chars
- 1] == '\n')
535 if (chars
> ((COUNTOF (buf
)) - 1))
537 chars
= COUNTOF (buf
) - 1;
542 wcstombs (buf
, msgbuf
, chars
+ 1);
544 strncpy (buf
, msgbuf
, chars
+ 1);
549 sprintf (buf
, "unknown win32 error (%u)", (unsigned) error
);
551 SetLastError (lasterr
);
556 create_process (const char *program
, char *args
,
557 DWORD flags
, PROCESS_INFORMATION
*pi
)
559 const char *inferior_cwd
= get_inferior_cwd ();
563 wchar_t *p
, *wprogram
, *wargs
, *wcwd
= NULL
;
566 wprogram
= alloca ((strlen (program
) + 1) * sizeof (wchar_t));
567 mbstowcs (wprogram
, program
, strlen (program
) + 1);
569 for (p
= wprogram
; *p
; ++p
)
573 argslen
= strlen (args
);
574 wargs
= alloca ((argslen
+ 1) * sizeof (wchar_t));
575 mbstowcs (wargs
, args
, argslen
+ 1);
577 if (inferior_cwd
!= NULL
)
579 std::string expanded_infcwd
= gdb_tilde_expand (inferior_cwd
);
580 std::replace (expanded_infcwd
.begin (), expanded_infcwd
.end (),
582 wcwd
= alloca ((expanded_infcwd
.size () + 1) * sizeof (wchar_t));
583 if (mbstowcs (wcwd
, expanded_infcwd
.c_str (),
584 expanded_infcwd
.size () + 1) == NULL
)
587 Could not convert the expanded inferior cwd to wide-char."));
591 ret
= CreateProcessW (wprogram
, /* image name */
592 wargs
, /* command line */
593 NULL
, /* security, not supported */
594 NULL
, /* thread, not supported */
595 FALSE
, /* inherit handles, not supported */
596 flags
, /* start flags */
597 NULL
, /* environment, not supported */
598 wcwd
, /* current directory */
599 NULL
, /* start info, not supported */
602 STARTUPINFOA si
= { sizeof (STARTUPINFOA
) };
604 ret
= CreateProcessA (program
, /* image name */
605 args
, /* command line */
608 TRUE
, /* inherit handles */
609 flags
, /* start flags */
610 NULL
, /* environment */
611 /* current directory */
612 (inferior_cwd
== NULL
614 : gdb_tilde_expand (inferior_cwd
).c_str()),
615 &si
, /* start info */
622 /* Start a new process.
623 PROGRAM is the program name.
624 PROGRAM_ARGS is the vector containing the inferior's args.
625 Returns the new PID on success, -1 on failure. Registers the new
626 process with the process list. */
628 win32_create_inferior (const char *program
,
629 const std::vector
<char *> &program_args
)
631 client_state
&cs
= get_client_state ();
633 char real_path
[PATH_MAX
];
634 char *orig_path
, *new_path
, *path_ptr
;
638 PROCESS_INFORMATION pi
;
640 std::string str_program_args
= stringify_argv (program_args
);
641 char *args
= (char *) str_program_args
.c_str ();
643 /* win32_wait needs to know we're not attaching. */
647 error ("No executable specified, specify executable to debug.\n");
649 flags
= DEBUG_PROCESS
| DEBUG_ONLY_THIS_PROCESS
;
653 path_ptr
= getenv ("PATH");
656 int size
= cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, NULL
, 0);
657 orig_path
= (char *) alloca (strlen (path_ptr
) + 1);
658 new_path
= (char *) alloca (size
);
659 strcpy (orig_path
, path_ptr
);
660 cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, new_path
, size
);
661 setenv ("PATH", new_path
, 1);
663 cygwin_conv_path (CCP_POSIX_TO_WIN_A
, program
, real_path
, PATH_MAX
);
667 OUTMSG2 (("Command line is \"%s\"\n", args
));
669 #ifdef CREATE_NEW_PROCESS_GROUP
670 flags
|= CREATE_NEW_PROCESS_GROUP
;
673 ret
= create_process (program
, args
, flags
, &pi
);
674 err
= GetLastError ();
675 if (!ret
&& err
== ERROR_FILE_NOT_FOUND
)
677 char *exename
= (char *) alloca (strlen (program
) + 5);
678 strcat (strcpy (exename
, program
), ".exe");
679 ret
= create_process (exename
, args
, flags
, &pi
);
680 err
= GetLastError ();
685 setenv ("PATH", orig_path
, 1);
690 error ("Error creating process \"%s%s\", (error %d): %s\n",
691 program
, args
, (int) err
, strwinerror (err
));
695 OUTMSG2 (("Process created: %s\n", (char *) args
));
699 /* On Windows CE this handle can't be closed. The OS reuses
700 it in the debug events, while the 9x/NT versions of Windows
701 probably use a DuplicateHandle'd one. */
702 CloseHandle (pi
.hThread
);
705 do_initial_child_stuff (pi
.hProcess
, pi
.dwProcessId
, 0);
707 /* Wait till we are at 1st instruction in program, return new pid
708 (assuming success). */
709 cs
.last_ptid
= win32_wait (ptid_t (current_process_id
), &cs
.last_status
, 0);
711 return current_process_id
;
714 /* Attach to a running process.
715 PID is the process ID to attach to, specified by the user
716 or a higher layer. */
718 win32_attach (unsigned long pid
)
721 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
724 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
726 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
728 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
730 h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
733 if (DebugActiveProcess (pid
))
735 if (DebugSetProcessKillOnExit
!= NULL
)
736 DebugSetProcessKillOnExit (FALSE
);
738 /* win32_wait needs to know we're attaching. */
740 do_initial_child_stuff (h
, pid
, 1);
747 err
= GetLastError ();
748 error ("Attach to process failed (error %d): %s\n",
749 (int) err
, strwinerror (err
));
752 /* Handle OUTPUT_DEBUG_STRING_EVENT from child process. */
754 handle_output_debug_string (void)
756 #define READ_BUFFER_LEN 1024
758 char s
[READ_BUFFER_LEN
+ 1] = { 0 };
759 DWORD nbytes
= current_event
.u
.DebugString
.nDebugStringLength
;
764 if (nbytes
> READ_BUFFER_LEN
)
765 nbytes
= READ_BUFFER_LEN
;
767 addr
= (CORE_ADDR
) (size_t) current_event
.u
.DebugString
.lpDebugStringData
;
769 if (current_event
.u
.DebugString
.fUnicode
)
771 /* The event tells us how many bytes, not chars, even
773 WCHAR buffer
[(READ_BUFFER_LEN
+ 1) / sizeof (WCHAR
)] = { 0 };
774 if (read_inferior_memory (addr
, (unsigned char *) buffer
, nbytes
) != 0)
776 wcstombs (s
, buffer
, (nbytes
+ 1) / sizeof (WCHAR
));
780 if (read_inferior_memory (addr
, (unsigned char *) s
, nbytes
) != 0)
784 if (!startswith (s
, "cYg"))
794 #undef READ_BUFFER_LEN
798 win32_clear_inferiors (void)
800 if (current_process_handle
!= NULL
)
801 CloseHandle (current_process_handle
);
803 for_each_thread (delete_thread_info
);
807 /* Implementation of target_ops::kill. */
810 win32_kill (process_info
*process
)
812 TerminateProcess (current_process_handle
, 0);
815 if (!child_continue (DBG_CONTINUE
, -1))
817 if (!WaitForDebugEvent (¤t_event
, INFINITE
))
819 if (current_event
.dwDebugEventCode
== EXIT_PROCESS_DEBUG_EVENT
)
821 else if (current_event
.dwDebugEventCode
== OUTPUT_DEBUG_STRING_EVENT
)
822 handle_output_debug_string ();
825 win32_clear_inferiors ();
827 remove_process (process
);
831 /* Implementation of target_ops::detach. */
834 win32_detach (process_info
*process
)
836 winapi_DebugActiveProcessStop DebugActiveProcessStop
= NULL
;
837 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
839 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
841 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
843 DebugActiveProcessStop
= GETPROCADDRESS (dll
, DebugActiveProcessStop
);
844 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
846 if (DebugSetProcessKillOnExit
== NULL
847 || DebugActiveProcessStop
== NULL
)
851 struct thread_resume resume
;
852 resume
.thread
= minus_one_ptid
;
853 resume
.kind
= resume_continue
;
855 win32_resume (&resume
, 1);
858 if (!DebugActiveProcessStop (current_process_id
))
861 DebugSetProcessKillOnExit (FALSE
);
862 remove_process (process
);
864 win32_clear_inferiors ();
869 win32_mourn (struct process_info
*process
)
871 remove_process (process
);
874 /* Implementation of target_ops::join. */
879 HANDLE h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
882 WaitForSingleObject (h
, INFINITE
);
887 /* Return 1 iff the thread with thread ID TID is alive. */
889 win32_thread_alive (ptid_t ptid
)
891 /* Our thread list is reliable; don't bother to poll target
893 return find_thread_ptid (ptid
) != NULL
;
896 /* Resume the inferior process. RESUME_INFO describes how we want
899 win32_resume (struct thread_resume
*resume_info
, size_t n
)
904 win32_thread_info
*th
;
905 DWORD continue_status
= DBG_CONTINUE
;
908 /* This handles the very limited set of resume packets that GDB can
909 currently produce. */
911 if (n
== 1 && resume_info
[0].thread
== minus_one_ptid
)
916 /* Yes, we're ignoring resume_info[0].thread. It'd be tricky to make
917 the Windows resume code do the right thing for thread switching. */
918 tid
= current_event
.dwThreadId
;
920 if (resume_info
[0].thread
!= minus_one_ptid
)
922 sig
= gdb_signal_from_host (resume_info
[0].sig
);
923 step
= resume_info
[0].kind
== resume_step
;
931 if (sig
!= GDB_SIGNAL_0
)
933 if (current_event
.dwDebugEventCode
!= EXCEPTION_DEBUG_EVENT
)
935 OUTMSG (("Cannot continue with signal %s here.\n",
936 gdb_signal_to_string (sig
)));
938 else if (sig
== last_sig
)
939 continue_status
= DBG_EXCEPTION_NOT_HANDLED
;
941 OUTMSG (("Can only continue with received signal %s.\n",
942 gdb_signal_to_string (last_sig
)));
945 last_sig
= GDB_SIGNAL_0
;
947 /* Get context for the currently selected thread. */
948 ptid
= debug_event_ptid (¤t_event
);
949 th
= thread_rec (ptid
, FALSE
);
952 win32_prepare_to_resume (th
);
954 if (th
->context
.ContextFlags
)
956 /* Move register values from the inferior into the thread
957 context structure. */
958 regcache_invalidate ();
962 if (the_low_target
.single_step
!= NULL
)
963 (*the_low_target
.single_step
) (th
);
965 error ("Single stepping is not supported "
966 "in this configuration.\n");
969 win32_set_thread_context (th
);
970 th
->context
.ContextFlags
= 0;
974 /* Allow continuing with the same signal that interrupted us.
975 Otherwise complain. */
977 child_continue (continue_status
, tid
);
981 win32_add_one_solib (const char *name
, CORE_ADDR load_addr
)
983 char buf
[MAX_PATH
+ 1];
984 char buf2
[MAX_PATH
+ 1];
987 WIN32_FIND_DATA w32_fd
;
988 WCHAR wname
[MAX_PATH
+ 1];
989 mbstowcs (wname
, name
, MAX_PATH
);
990 HANDLE h
= FindFirstFile (wname
, &w32_fd
);
992 WIN32_FIND_DATAA w32_fd
;
993 HANDLE h
= FindFirstFileA (name
, &w32_fd
);
996 /* The symbols in a dll are offset by 0x1000, which is the
997 offset from 0 of the first byte in an image - because
998 of the file header and the section alignment. */
1001 if (h
== INVALID_HANDLE_VALUE
)
1009 char cwd
[MAX_PATH
+ 1];
1011 if (GetCurrentDirectoryA (MAX_PATH
+ 1, cwd
))
1013 p
= strrchr (buf
, '\\');
1016 SetCurrentDirectoryA (buf
);
1017 GetFullPathNameA (w32_fd
.cFileName
, MAX_PATH
, buf
, &p
);
1018 SetCurrentDirectoryA (cwd
);
1025 if (strcasecmp (buf
, "ntdll.dll") == 0)
1027 GetSystemDirectoryA (buf
, sizeof (buf
));
1028 strcat (buf
, "\\ntdll.dll");
1033 cygwin_conv_path (CCP_WIN_A_TO_POSIX
, buf
, buf2
, sizeof (buf2
));
1038 loaded_dll (buf2
, load_addr
);
1042 get_image_name (HANDLE h
, void *address
, int unicode
)
1044 static char buf
[(2 * MAX_PATH
) + 1];
1045 DWORD size
= unicode
? sizeof (WCHAR
) : sizeof (char);
1051 /* Attempt to read the name of the dll that was detected.
1052 This is documented to work only when actively debugging
1053 a program. It will not work for attached processes. */
1054 if (address
== NULL
)
1058 /* Windows CE reports the address of the image name,
1059 instead of an address of a pointer into the image name. */
1060 address_ptr
= address
;
1062 /* See if we could read the address of a string, and that the
1063 address isn't null. */
1064 if (!ReadProcessMemory (h
, address
, &address_ptr
,
1065 sizeof (address_ptr
), &done
)
1066 || done
!= sizeof (address_ptr
)
1071 /* Find the length of the string */
1072 while (ReadProcessMemory (h
, address_ptr
+ len
++ * size
, &b
, size
, &done
)
1073 && (b
[0] != 0 || b
[size
- 1] != 0) && done
== size
)
1077 ReadProcessMemory (h
, address_ptr
, buf
, len
, &done
);
1080 WCHAR
*unicode_address
= XALLOCAVEC (WCHAR
, len
);
1081 ReadProcessMemory (h
, address_ptr
, unicode_address
, len
* sizeof (WCHAR
),
1084 WideCharToMultiByte (CP_ACP
, 0, unicode_address
, len
, buf
, len
, 0, 0);
1090 typedef BOOL (WINAPI
*winapi_EnumProcessModules
) (HANDLE
, HMODULE
*,
1092 typedef BOOL (WINAPI
*winapi_GetModuleInformation
) (HANDLE
, HMODULE
,
1093 LPMODULEINFO
, DWORD
);
1094 typedef DWORD (WINAPI
*winapi_GetModuleFileNameExA
) (HANDLE
, HMODULE
,
1097 static winapi_EnumProcessModules win32_EnumProcessModules
;
1098 static winapi_GetModuleInformation win32_GetModuleInformation
;
1099 static winapi_GetModuleFileNameExA win32_GetModuleFileNameExA
;
1104 static int psapi_loaded
= 0;
1105 static HMODULE dll
= NULL
;
1110 dll
= LoadLibrary (TEXT("psapi.dll"));
1113 win32_EnumProcessModules
=
1114 GETPROCADDRESS (dll
, EnumProcessModules
);
1115 win32_GetModuleInformation
=
1116 GETPROCADDRESS (dll
, GetModuleInformation
);
1117 win32_GetModuleFileNameExA
=
1118 GETPROCADDRESS (dll
, GetModuleFileNameExA
);
1121 return (win32_EnumProcessModules
!= NULL
1122 && win32_GetModuleInformation
!= NULL
1123 && win32_GetModuleFileNameExA
!= NULL
);
1128 /* Iterate over all DLLs currently mapped by our inferior, and
1129 add them to our list of solibs. */
1132 win32_add_all_dlls (void)
1136 HMODULE
*DllHandle
= dh_buf
;
1144 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1149 if (!ok
|| !cbNeeded
)
1152 DllHandle
= (HMODULE
*) alloca (cbNeeded
);
1156 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1163 for (i
= 1; i
< ((size_t) cbNeeded
/ sizeof (HMODULE
)); i
++)
1166 char dll_name
[MAX_PATH
];
1168 if (!(*win32_GetModuleInformation
) (current_process_handle
,
1173 if ((*win32_GetModuleFileNameExA
) (current_process_handle
,
1178 win32_add_one_solib (dll_name
, (CORE_ADDR
) (uintptr_t) mi
.lpBaseOfDll
);
1183 typedef HANDLE (WINAPI
*winapi_CreateToolhelp32Snapshot
) (DWORD
, DWORD
);
1184 typedef BOOL (WINAPI
*winapi_Module32First
) (HANDLE
, LPMODULEENTRY32
);
1185 typedef BOOL (WINAPI
*winapi_Module32Next
) (HANDLE
, LPMODULEENTRY32
);
1187 /* Handle a DLL load event.
1189 This function assumes that this event did not occur during inferior
1190 initialization, where their event info may be incomplete (see
1191 do_initial_child_stuff and win32_add_all_dlls for more info on
1192 how we handle DLL loading during that phase). */
1195 handle_load_dll (void)
1197 LOAD_DLL_DEBUG_INFO
*event
= ¤t_event
.u
.LoadDll
;
1200 dll_name
= get_image_name (current_process_handle
,
1201 event
->lpImageName
, event
->fUnicode
);
1205 win32_add_one_solib (dll_name
, (CORE_ADDR
) (uintptr_t) event
->lpBaseOfDll
);
1208 /* Handle a DLL unload event.
1210 This function assumes that this event did not occur during inferior
1211 initialization, where their event info may be incomplete (see
1212 do_initial_child_stuff and win32_add_one_solib for more info
1213 on how we handle DLL loading during that phase). */
1216 handle_unload_dll (void)
1218 CORE_ADDR load_addr
=
1219 (CORE_ADDR
) (uintptr_t) current_event
.u
.UnloadDll
.lpBaseOfDll
;
1221 /* The symbols in a dll are offset by 0x1000, which is the
1222 offset from 0 of the first byte in an image - because
1223 of the file header and the section alignment. */
1224 load_addr
+= 0x1000;
1225 unloaded_dll (NULL
, load_addr
);
1229 handle_exception (struct target_waitstatus
*ourstatus
)
1231 DWORD code
= current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
;
1233 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
1237 case EXCEPTION_ACCESS_VIOLATION
:
1238 OUTMSG2 (("EXCEPTION_ACCESS_VIOLATION"));
1239 ourstatus
->value
.sig
= GDB_SIGNAL_SEGV
;
1241 case STATUS_STACK_OVERFLOW
:
1242 OUTMSG2 (("STATUS_STACK_OVERFLOW"));
1243 ourstatus
->value
.sig
= GDB_SIGNAL_SEGV
;
1245 case STATUS_FLOAT_DENORMAL_OPERAND
:
1246 OUTMSG2 (("STATUS_FLOAT_DENORMAL_OPERAND"));
1247 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1249 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
1250 OUTMSG2 (("EXCEPTION_ARRAY_BOUNDS_EXCEEDED"));
1251 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1253 case STATUS_FLOAT_INEXACT_RESULT
:
1254 OUTMSG2 (("STATUS_FLOAT_INEXACT_RESULT"));
1255 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1257 case STATUS_FLOAT_INVALID_OPERATION
:
1258 OUTMSG2 (("STATUS_FLOAT_INVALID_OPERATION"));
1259 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1261 case STATUS_FLOAT_OVERFLOW
:
1262 OUTMSG2 (("STATUS_FLOAT_OVERFLOW"));
1263 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1265 case STATUS_FLOAT_STACK_CHECK
:
1266 OUTMSG2 (("STATUS_FLOAT_STACK_CHECK"));
1267 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1269 case STATUS_FLOAT_UNDERFLOW
:
1270 OUTMSG2 (("STATUS_FLOAT_UNDERFLOW"));
1271 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1273 case STATUS_FLOAT_DIVIDE_BY_ZERO
:
1274 OUTMSG2 (("STATUS_FLOAT_DIVIDE_BY_ZERO"));
1275 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1277 case STATUS_INTEGER_DIVIDE_BY_ZERO
:
1278 OUTMSG2 (("STATUS_INTEGER_DIVIDE_BY_ZERO"));
1279 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1281 case STATUS_INTEGER_OVERFLOW
:
1282 OUTMSG2 (("STATUS_INTEGER_OVERFLOW"));
1283 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1285 case EXCEPTION_BREAKPOINT
:
1286 OUTMSG2 (("EXCEPTION_BREAKPOINT"));
1287 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1289 /* Remove the initial breakpoint. */
1290 check_breakpoints ((CORE_ADDR
) (long) current_event
1291 .u
.Exception
.ExceptionRecord
.ExceptionAddress
);
1295 OUTMSG2 (("DBG_CONTROL_C"));
1296 ourstatus
->value
.sig
= GDB_SIGNAL_INT
;
1298 case DBG_CONTROL_BREAK
:
1299 OUTMSG2 (("DBG_CONTROL_BREAK"));
1300 ourstatus
->value
.sig
= GDB_SIGNAL_INT
;
1302 case EXCEPTION_SINGLE_STEP
:
1303 OUTMSG2 (("EXCEPTION_SINGLE_STEP"));
1304 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1306 case EXCEPTION_ILLEGAL_INSTRUCTION
:
1307 OUTMSG2 (("EXCEPTION_ILLEGAL_INSTRUCTION"));
1308 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1310 case EXCEPTION_PRIV_INSTRUCTION
:
1311 OUTMSG2 (("EXCEPTION_PRIV_INSTRUCTION"));
1312 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1314 case EXCEPTION_NONCONTINUABLE_EXCEPTION
:
1315 OUTMSG2 (("EXCEPTION_NONCONTINUABLE_EXCEPTION"));
1316 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1319 if (current_event
.u
.Exception
.dwFirstChance
)
1321 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1324 OUTMSG2 (("gdbserver: unknown target exception 0x%08x at 0x%s",
1325 (unsigned) current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
,
1326 phex_nz ((uintptr_t) current_event
.u
.Exception
.ExceptionRecord
.
1327 ExceptionAddress
, sizeof (uintptr_t))));
1328 ourstatus
->value
.sig
= GDB_SIGNAL_UNKNOWN
;
1332 last_sig
= ourstatus
->value
.sig
;
1337 suspend_one_thread (thread_info
*thread
)
1339 win32_thread_info
*th
= (win32_thread_info
*) thread_target_data (thread
);
1343 if (SuspendThread (th
->h
) == (DWORD
) -1)
1345 DWORD err
= GetLastError ();
1346 OUTMSG (("warning: SuspendThread failed in suspend_one_thread, "
1347 "(error %d): %s\n", (int) err
, strwinerror (err
)));
1355 fake_breakpoint_event (void)
1357 OUTMSG2(("fake_breakpoint_event\n"));
1359 faked_breakpoint
= 1;
1361 memset (¤t_event
, 0, sizeof (current_event
));
1362 current_event
.dwThreadId
= main_thread_id
;
1363 current_event
.dwDebugEventCode
= EXCEPTION_DEBUG_EVENT
;
1364 current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
1365 = EXCEPTION_BREAKPOINT
;
1367 for_each_thread (suspend_one_thread
);
1372 auto_delete_breakpoint (CORE_ADDR stop_pc
)
1378 /* Get the next event from the child. */
1381 get_child_debug_event (struct target_waitstatus
*ourstatus
)
1385 last_sig
= GDB_SIGNAL_0
;
1386 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1388 /* Check if GDB sent us an interrupt request. */
1389 check_remote_input_interrupt_request ();
1391 if (soft_interrupt_requested
)
1393 soft_interrupt_requested
= 0;
1394 fake_breakpoint_event ();
1403 /* WinCE doesn't set an initial breakpoint automatically. To
1404 stop the inferior, we flush all currently pending debug
1405 events -- the thread list and the dll list are always
1406 reported immediatelly without delay, then, we suspend all
1407 threads and pretend we saw a trap at the current PC of the
1410 Contrary to desktop Windows, Windows CE *does* report the dll
1411 names on LOAD_DLL_DEBUG_EVENTs resulting from a
1412 DebugActiveProcess call. This limits the way we can detect
1413 if all the dlls have already been reported. If we get a real
1414 debug event before leaving attaching, the worst that will
1415 happen is the user will see a spurious breakpoint. */
1417 current_event
.dwDebugEventCode
= 0;
1418 if (!WaitForDebugEvent (¤t_event
, 0))
1420 OUTMSG2(("no attach events left\n"));
1421 fake_breakpoint_event ();
1425 OUTMSG2(("got attach event\n"));
1430 /* Keep the wait time low enough for comfortable remote
1431 interruption, but high enough so gdbserver doesn't become a
1433 if (!WaitForDebugEvent (¤t_event
, 250))
1435 DWORD e
= GetLastError();
1437 if (e
== ERROR_PIPE_NOT_CONNECTED
)
1439 /* This will happen if the loader fails to succesfully
1440 load the application, e.g., if the main executable
1441 tries to pull in a non-existing export from a
1443 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1444 ourstatus
->value
.integer
= 1;
1454 switch (current_event
.dwDebugEventCode
)
1456 case CREATE_THREAD_DEBUG_EVENT
:
1457 OUTMSG2 (("gdbserver: kernel event CREATE_THREAD_DEBUG_EVENT "
1458 "for pid=%u tid=%x)\n",
1459 (unsigned) current_event
.dwProcessId
,
1460 (unsigned) current_event
.dwThreadId
));
1462 /* Record the existence of this thread. */
1463 child_add_thread (current_event
.dwProcessId
,
1464 current_event
.dwThreadId
,
1465 current_event
.u
.CreateThread
.hThread
,
1466 current_event
.u
.CreateThread
.lpThreadLocalBase
);
1469 case EXIT_THREAD_DEBUG_EVENT
:
1470 OUTMSG2 (("gdbserver: kernel event EXIT_THREAD_DEBUG_EVENT "
1471 "for pid=%u tid=%x\n",
1472 (unsigned) current_event
.dwProcessId
,
1473 (unsigned) current_event
.dwThreadId
));
1474 child_delete_thread (current_event
.dwProcessId
,
1475 current_event
.dwThreadId
);
1477 current_thread
= get_first_thread ();
1480 case CREATE_PROCESS_DEBUG_EVENT
:
1481 OUTMSG2 (("gdbserver: kernel event CREATE_PROCESS_DEBUG_EVENT "
1482 "for pid=%u tid=%x\n",
1483 (unsigned) current_event
.dwProcessId
,
1484 (unsigned) current_event
.dwThreadId
));
1485 CloseHandle (current_event
.u
.CreateProcessInfo
.hFile
);
1487 current_process_handle
= current_event
.u
.CreateProcessInfo
.hProcess
;
1488 main_thread_id
= current_event
.dwThreadId
;
1490 /* Add the main thread. */
1491 child_add_thread (current_event
.dwProcessId
,
1493 current_event
.u
.CreateProcessInfo
.hThread
,
1494 current_event
.u
.CreateProcessInfo
.lpThreadLocalBase
);
1499 /* Windows CE doesn't set the initial breakpoint
1500 automatically like the desktop versions of Windows do.
1501 We add it explicitly here. It will be removed as soon as
1503 set_breakpoint_at ((CORE_ADDR
) (long) current_event
.u
1504 .CreateProcessInfo
.lpStartAddress
,
1505 auto_delete_breakpoint
);
1510 case EXIT_PROCESS_DEBUG_EVENT
:
1511 OUTMSG2 (("gdbserver: kernel event EXIT_PROCESS_DEBUG_EVENT "
1512 "for pid=%u tid=%x\n",
1513 (unsigned) current_event
.dwProcessId
,
1514 (unsigned) current_event
.dwThreadId
));
1516 DWORD exit_status
= current_event
.u
.ExitProcess
.dwExitCode
;
1517 /* If the exit status looks like a fatal exception, but we
1518 don't recognize the exception's code, make the original
1519 exit status value available, to avoid losing information. */
1521 = WIFSIGNALED (exit_status
) ? WTERMSIG (exit_status
) : -1;
1522 if (exit_signal
== -1)
1524 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1525 ourstatus
->value
.integer
= exit_status
;
1529 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
1530 ourstatus
->value
.sig
= gdb_signal_from_host (exit_signal
);
1533 child_continue (DBG_CONTINUE
, -1);
1534 CloseHandle (current_process_handle
);
1535 current_process_handle
= NULL
;
1538 case LOAD_DLL_DEBUG_EVENT
:
1539 OUTMSG2 (("gdbserver: kernel event LOAD_DLL_DEBUG_EVENT "
1540 "for pid=%u tid=%x\n",
1541 (unsigned) current_event
.dwProcessId
,
1542 (unsigned) current_event
.dwThreadId
));
1543 CloseHandle (current_event
.u
.LoadDll
.hFile
);
1544 if (! child_initialization_done
)
1548 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1549 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1552 case UNLOAD_DLL_DEBUG_EVENT
:
1553 OUTMSG2 (("gdbserver: kernel event UNLOAD_DLL_DEBUG_EVENT "
1554 "for pid=%u tid=%x\n",
1555 (unsigned) current_event
.dwProcessId
,
1556 (unsigned) current_event
.dwThreadId
));
1557 if (! child_initialization_done
)
1559 handle_unload_dll ();
1560 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1561 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1564 case EXCEPTION_DEBUG_EVENT
:
1565 OUTMSG2 (("gdbserver: kernel event EXCEPTION_DEBUG_EVENT "
1566 "for pid=%u tid=%x\n",
1567 (unsigned) current_event
.dwProcessId
,
1568 (unsigned) current_event
.dwThreadId
));
1569 handle_exception (ourstatus
);
1572 case OUTPUT_DEBUG_STRING_EVENT
:
1573 /* A message from the kernel (or Cygwin). */
1574 OUTMSG2 (("gdbserver: kernel event OUTPUT_DEBUG_STRING_EVENT "
1575 "for pid=%u tid=%x\n",
1576 (unsigned) current_event
.dwProcessId
,
1577 (unsigned) current_event
.dwThreadId
));
1578 handle_output_debug_string ();
1582 OUTMSG2 (("gdbserver: kernel event unknown "
1583 "for pid=%u tid=%x code=%x\n",
1584 (unsigned) current_event
.dwProcessId
,
1585 (unsigned) current_event
.dwThreadId
,
1586 (unsigned) current_event
.dwDebugEventCode
));
1590 ptid
= debug_event_ptid (¤t_event
);
1591 current_thread
= find_thread_ptid (ptid
);
1595 /* Wait for the inferior process to change state.
1596 STATUS will be filled in with a response code to send to GDB.
1597 Returns the signal which caused the process to stop. */
1599 win32_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
, int options
)
1601 struct regcache
*regcache
;
1603 if (cached_status
.kind
!= TARGET_WAITKIND_IGNORE
)
1605 /* The core always does a wait after creating the inferior, and
1606 do_initial_child_stuff already ran the inferior to the
1607 initial breakpoint (or an exit, if creating the process
1608 fails). Report it now. */
1609 *ourstatus
= cached_status
;
1610 cached_status
.kind
= TARGET_WAITKIND_IGNORE
;
1611 return debug_event_ptid (¤t_event
);
1616 if (!get_child_debug_event (ourstatus
))
1619 switch (ourstatus
->kind
)
1621 case TARGET_WAITKIND_EXITED
:
1622 OUTMSG2 (("Child exited with retcode = %x\n",
1623 ourstatus
->value
.integer
));
1624 win32_clear_inferiors ();
1625 return ptid_t (current_event
.dwProcessId
);
1626 case TARGET_WAITKIND_STOPPED
:
1627 case TARGET_WAITKIND_SIGNALLED
:
1628 case TARGET_WAITKIND_LOADED
:
1629 OUTMSG2 (("Child Stopped with signal = %d \n",
1630 ourstatus
->value
.sig
));
1632 regcache
= get_thread_regcache (current_thread
, 1);
1633 child_fetch_inferior_registers (regcache
, -1);
1634 return debug_event_ptid (¤t_event
);
1636 OUTMSG (("Ignoring unknown internal event, %d\n", ourstatus
->kind
));
1638 case TARGET_WAITKIND_SPURIOUS
:
1639 /* do nothing, just continue */
1640 child_continue (DBG_CONTINUE
, -1);
1646 /* Fetch registers from the inferior process.
1647 If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO. */
1649 win32_fetch_inferior_registers (struct regcache
*regcache
, int regno
)
1651 child_fetch_inferior_registers (regcache
, regno
);
1654 /* Store registers to the inferior process.
1655 If REGNO is -1, store all registers; otherwise, store at least REGNO. */
1657 win32_store_inferior_registers (struct regcache
*regcache
, int regno
)
1659 child_store_inferior_registers (regcache
, regno
);
1662 /* Read memory from the inferior process. This should generally be
1663 called through read_inferior_memory, which handles breakpoint shadowing.
1664 Read LEN bytes at MEMADDR into a buffer at MYADDR. */
1666 win32_read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
1668 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 0, 0) != len
;
1671 /* Write memory to the inferior process. This should generally be
1672 called through write_inferior_memory, which handles breakpoint shadowing.
1673 Write LEN bytes from the buffer at MYADDR to MEMADDR.
1674 Returns 0 on success and errno on failure. */
1676 win32_write_inferior_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
,
1679 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 1, 0) != len
;
1682 /* Send an interrupt request to the inferior process. */
1684 win32_request_interrupt (void)
1686 winapi_DebugBreakProcess DebugBreakProcess
;
1687 winapi_GenerateConsoleCtrlEvent GenerateConsoleCtrlEvent
;
1690 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
1692 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
1695 GenerateConsoleCtrlEvent
= GETPROCADDRESS (dll
, GenerateConsoleCtrlEvent
);
1697 if (GenerateConsoleCtrlEvent
!= NULL
1698 && GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT
, current_process_id
))
1701 /* GenerateConsoleCtrlEvent can fail if process id being debugged is
1702 not a process group id.
1703 Fallback to XP/Vista 'DebugBreakProcess', which generates a
1704 breakpoint exception in the interior process. */
1706 DebugBreakProcess
= GETPROCADDRESS (dll
, DebugBreakProcess
);
1708 if (DebugBreakProcess
!= NULL
1709 && DebugBreakProcess (current_process_handle
))
1712 /* Last resort, suspend all threads manually. */
1713 soft_interrupt_requested
= 1;
1718 win32_error_to_fileio_error (DWORD err
)
1722 case ERROR_BAD_PATHNAME
:
1723 case ERROR_FILE_NOT_FOUND
:
1724 case ERROR_INVALID_NAME
:
1725 case ERROR_PATH_NOT_FOUND
:
1726 return FILEIO_ENOENT
;
1728 case ERROR_IO_DEVICE
:
1729 case ERROR_OPEN_FAILED
:
1731 case ERROR_INVALID_HANDLE
:
1732 return FILEIO_EBADF
;
1733 case ERROR_ACCESS_DENIED
:
1734 case ERROR_SHARING_VIOLATION
:
1735 return FILEIO_EACCES
;
1736 case ERROR_NOACCESS
:
1737 return FILEIO_EFAULT
;
1739 return FILEIO_EBUSY
;
1740 case ERROR_ALREADY_EXISTS
:
1741 case ERROR_FILE_EXISTS
:
1742 return FILEIO_EEXIST
;
1743 case ERROR_BAD_DEVICE
:
1744 return FILEIO_ENODEV
;
1745 case ERROR_DIRECTORY
:
1746 return FILEIO_ENOTDIR
;
1747 case ERROR_FILENAME_EXCED_RANGE
:
1748 case ERROR_INVALID_DATA
:
1749 case ERROR_INVALID_PARAMETER
:
1750 case ERROR_NEGATIVE_SEEK
:
1751 return FILEIO_EINVAL
;
1752 case ERROR_TOO_MANY_OPEN_FILES
:
1753 return FILEIO_EMFILE
;
1754 case ERROR_HANDLE_DISK_FULL
:
1755 case ERROR_DISK_FULL
:
1756 return FILEIO_ENOSPC
;
1757 case ERROR_WRITE_PROTECT
:
1758 return FILEIO_EROFS
;
1759 case ERROR_NOT_SUPPORTED
:
1760 return FILEIO_ENOSYS
;
1763 return FILEIO_EUNKNOWN
;
1767 wince_hostio_last_error (char *buf
)
1769 DWORD winerr
= GetLastError ();
1770 int fileio_err
= win32_error_to_fileio_error (winerr
);
1771 sprintf (buf
, "F-1,%x", fileio_err
);
1775 /* Write Windows OS Thread Information Block address. */
1778 win32_get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
)
1780 win32_thread_info
*th
;
1781 th
= thread_rec (ptid
, 0);
1785 *addr
= th
->thread_local_base
;
1789 /* Implementation of the target_ops method "sw_breakpoint_from_kind". */
1791 static const gdb_byte
*
1792 win32_sw_breakpoint_from_kind (int kind
, int *size
)
1794 *size
= the_low_target
.breakpoint_len
;
1795 return the_low_target
.breakpoint
;
1798 static struct target_ops win32_target_ops
= {
1799 win32_create_inferior
,
1800 NULL
, /* post_create_inferior */
1809 win32_fetch_inferior_registers
,
1810 win32_store_inferior_registers
,
1811 NULL
, /* prepare_to_access_memory */
1812 NULL
, /* done_accessing_memory */
1813 win32_read_inferior_memory
,
1814 win32_write_inferior_memory
,
1815 NULL
, /* lookup_symbols */
1816 win32_request_interrupt
,
1817 NULL
, /* read_auxv */
1818 win32_supports_z_point_type
,
1821 NULL
, /* stopped_by_sw_breakpoint */
1822 NULL
, /* supports_stopped_by_sw_breakpoint */
1823 NULL
, /* stopped_by_hw_breakpoint */
1824 NULL
, /* supports_stopped_by_hw_breakpoint */
1825 target_can_do_hardware_single_step
,
1826 win32_stopped_by_watchpoint
,
1827 win32_stopped_data_address
,
1828 NULL
, /* read_offsets */
1829 NULL
, /* get_tls_address */
1831 wince_hostio_last_error
,
1833 hostio_last_error_from_errno
,
1835 NULL
, /* qxfer_osdata */
1836 NULL
, /* qxfer_siginfo */
1837 NULL
, /* supports_non_stop */
1839 NULL
, /* start_non_stop */
1840 NULL
, /* supports_multi_process */
1841 NULL
, /* supports_fork_events */
1842 NULL
, /* supports_vfork_events */
1843 NULL
, /* supports_exec_events */
1844 NULL
, /* handle_new_gdb_connection */
1845 NULL
, /* handle_monitor_command */
1846 NULL
, /* core_of_thread */
1847 NULL
, /* read_loadmap */
1848 NULL
, /* process_qsupported */
1849 NULL
, /* supports_tracepoints */
1851 NULL
, /* write_pc */
1852 NULL
, /* thread_stopped */
1853 win32_get_tib_address
,
1854 NULL
, /* pause_all */
1855 NULL
, /* unpause_all */
1856 NULL
, /* stabilize_threads */
1857 NULL
, /* install_fast_tracepoint_jump_pad */
1858 NULL
, /* emit_ops */
1859 NULL
, /* supports_disable_randomization */
1860 NULL
, /* get_min_fast_tracepoint_insn_len */
1861 NULL
, /* qxfer_libraries_svr4 */
1862 NULL
, /* support_agent */
1863 NULL
, /* enable_btrace */
1864 NULL
, /* disable_btrace */
1865 NULL
, /* read_btrace */
1866 NULL
, /* read_btrace_conf */
1867 NULL
, /* supports_range_stepping */
1868 NULL
, /* pid_to_exec_file */
1869 NULL
, /* multifs_open */
1870 NULL
, /* multifs_unlink */
1871 NULL
, /* multifs_readlink */
1872 NULL
, /* breakpoint_kind_from_pc */
1873 win32_sw_breakpoint_from_kind
,
1876 /* Initialize the Win32 backend. */
1878 initialize_low (void)
1880 set_target_ops (&win32_target_ops
);
1881 the_low_target
.arch_setup ();