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
);
106 static ptid_t
win32_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
,
108 static void win32_resume (struct thread_resume
*resume_info
, size_t n
);
110 static void win32_add_all_dlls (void);
113 /* Get the thread ID from the current selected inferior (the current
116 current_thread_ptid (void)
121 /* The current debug event from WaitForDebugEvent. */
123 debug_event_ptid (DEBUG_EVENT
*event
)
125 return ptid_t (event
->dwProcessId
, event
->dwThreadId
, 0);
128 /* Get the thread context of the thread associated with TH. */
131 win32_get_thread_context (win32_thread_info
*th
)
133 memset (&th
->context
, 0, sizeof (CONTEXT
));
134 (*the_low_target
.get_thread_context
) (th
);
136 memcpy (&th
->base_context
, &th
->context
, sizeof (CONTEXT
));
140 /* Set the thread context of the thread associated with TH. */
143 win32_set_thread_context (win32_thread_info
*th
)
146 /* Calling SuspendThread on a thread that is running kernel code
147 will report that the suspending was successful, but in fact, that
148 will often not be true. In those cases, the context returned by
149 GetThreadContext will not be correct by the time the thread
150 stops, hence we can't set that context back into the thread when
151 resuming - it will most likely crash the inferior.
152 Unfortunately, there is no way to know when the thread will
153 really stop. To work around it, we'll only write the context
154 back to the thread when either the user or GDB explicitly change
155 it between stopping and resuming. */
156 if (memcmp (&th
->context
, &th
->base_context
, sizeof (CONTEXT
)) != 0)
158 SetThreadContext (th
->h
, &th
->context
);
161 /* Set the thread context of the thread associated with TH. */
164 win32_prepare_to_resume (win32_thread_info
*th
)
166 if (the_low_target
.prepare_to_resume
!= NULL
)
167 (*the_low_target
.prepare_to_resume
) (th
);
170 /* See win32-low.h. */
173 win32_require_context (win32_thread_info
*th
)
175 if (th
->context
.ContextFlags
== 0)
179 if (SuspendThread (th
->h
) == (DWORD
) -1)
181 DWORD err
= GetLastError ();
182 OUTMSG (("warning: SuspendThread failed in thread_rec, "
183 "(error %d): %s\n", (int) err
, strwinerror (err
)));
189 win32_get_thread_context (th
);
193 /* Find a thread record given a thread id. If GET_CONTEXT is set then
194 also retrieve the context for this thread. */
195 static win32_thread_info
*
196 thread_rec (ptid_t ptid
, int get_context
)
198 thread_info
*thread
= find_thread_ptid (ptid
);
202 win32_thread_info
*th
= (win32_thread_info
*) thread_target_data (thread
);
204 win32_require_context (th
);
208 /* Add a thread to the thread list. */
209 static win32_thread_info
*
210 child_add_thread (DWORD pid
, DWORD tid
, HANDLE h
, void *tlb
)
212 win32_thread_info
*th
;
213 ptid_t ptid
= ptid_t (pid
, tid
, 0);
215 if ((th
= thread_rec (ptid
, FALSE
)))
218 th
= XCNEW (win32_thread_info
);
221 th
->thread_local_base
= (CORE_ADDR
) (uintptr_t) tlb
;
223 add_thread (ptid
, th
);
225 if (the_low_target
.thread_added
!= NULL
)
226 (*the_low_target
.thread_added
) (th
);
231 /* Delete a thread from the list of threads. */
233 delete_thread_info (thread_info
*thread
)
235 win32_thread_info
*th
= (win32_thread_info
*) thread_target_data (thread
);
237 remove_thread (thread
);
242 /* Delete a thread from the list of threads. */
244 child_delete_thread (DWORD pid
, DWORD tid
)
246 /* If the last thread is exiting, just return. */
247 if (all_threads
.size () == 1)
250 thread_info
*thread
= find_thread_ptid (ptid_t (pid
, tid
));
254 delete_thread_info (thread
);
257 /* These watchpoint related wrapper functions simply pass on the function call
258 if the low target has registered a corresponding function. */
261 win32_supports_z_point_type (char z_type
)
263 return (the_low_target
.supports_z_point_type
!= NULL
264 && the_low_target
.supports_z_point_type (z_type
));
268 win32_insert_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
269 int size
, struct raw_breakpoint
*bp
)
271 if (the_low_target
.insert_point
!= NULL
)
272 return the_low_target
.insert_point (type
, addr
, size
, bp
);
274 /* Unsupported (see target.h). */
279 win32_remove_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
280 int size
, struct raw_breakpoint
*bp
)
282 if (the_low_target
.remove_point
!= NULL
)
283 return the_low_target
.remove_point (type
, addr
, size
, bp
);
285 /* Unsupported (see target.h). */
290 win32_stopped_by_watchpoint (void)
292 if (the_low_target
.stopped_by_watchpoint
!= NULL
)
293 return the_low_target
.stopped_by_watchpoint ();
299 win32_stopped_data_address (void)
301 if (the_low_target
.stopped_data_address
!= NULL
)
302 return the_low_target
.stopped_data_address ();
308 /* Transfer memory from/to the debugged process. */
310 child_xfer_memory (CORE_ADDR memaddr
, char *our
, int len
,
311 int write
, process_stratum_target
*target
)
316 uintptr_t addr
= (uintptr_t) memaddr
;
320 success
= WriteProcessMemory (current_process_handle
, (LPVOID
) addr
,
321 (LPCVOID
) our
, len
, &done
);
323 lasterror
= GetLastError ();
324 FlushInstructionCache (current_process_handle
, (LPCVOID
) addr
, len
);
328 success
= ReadProcessMemory (current_process_handle
, (LPCVOID
) addr
,
329 (LPVOID
) our
, len
, &done
);
331 lasterror
= GetLastError ();
333 if (!success
&& lasterror
== ERROR_PARTIAL_COPY
&& done
> 0)
336 return success
? done
: -1;
339 /* Clear out any old thread list and reinitialize it to a pristine
342 child_init_thread_list (void)
344 for_each_thread (delete_thread_info
);
347 /* Zero during the child initialization phase, and nonzero otherwise. */
349 static int child_initialization_done
= 0;
352 do_initial_child_stuff (HANDLE proch
, DWORD pid
, int attached
)
354 struct process_info
*proc
;
356 last_sig
= GDB_SIGNAL_0
;
358 current_process_handle
= proch
;
359 current_process_id
= pid
;
362 soft_interrupt_requested
= 0;
363 faked_breakpoint
= 0;
365 memset (¤t_event
, 0, sizeof (current_event
));
367 proc
= add_process (pid
, attached
);
368 proc
->tdesc
= win32_tdesc
;
369 child_init_thread_list ();
370 child_initialization_done
= 0;
372 if (the_low_target
.initial_stuff
!= NULL
)
373 (*the_low_target
.initial_stuff
) ();
375 cached_status
.kind
= TARGET_WAITKIND_IGNORE
;
377 /* Flush all currently pending debug events (thread and dll list) up
378 to the initial breakpoint. */
381 struct target_waitstatus status
;
383 win32_wait (minus_one_ptid
, &status
, 0);
385 /* Note win32_wait doesn't return thread events. */
386 if (status
.kind
!= TARGET_WAITKIND_LOADED
)
388 cached_status
= status
;
393 struct thread_resume resume
;
395 resume
.thread
= minus_one_ptid
;
396 resume
.kind
= resume_continue
;
399 win32_resume (&resume
, 1);
404 /* Now that the inferior has been started and all DLLs have been mapped,
405 we can iterate over all DLLs and load them in.
407 We avoid doing it any earlier because, on certain versions of Windows,
408 LOAD_DLL_DEBUG_EVENTs are sometimes not complete. In particular,
409 we have seen on Windows 8.1 that the ntdll.dll load event does not
410 include the DLL name, preventing us from creating an associated SO.
411 A possible explanation is that ntdll.dll might be mapped before
412 the SO info gets created by the Windows system -- ntdll.dll is
413 the first DLL to be reported via LOAD_DLL_DEBUG_EVENT and other DLLs
414 do not seem to suffer from that problem.
416 Rather than try to work around this sort of issue, it is much
417 simpler to just ignore DLL load/unload events during the startup
418 phase, and then process them all in one batch now. */
419 win32_add_all_dlls ();
422 child_initialization_done
= 1;
425 /* Resume all artificially suspended threads if we are continuing
428 continue_one_thread (thread_info
*thread
, int thread_id
)
430 win32_thread_info
*th
= (win32_thread_info
*) thread_target_data (thread
);
432 if (thread_id
== -1 || thread_id
== th
->tid
)
434 win32_prepare_to_resume (th
);
438 if (th
->context
.ContextFlags
)
440 win32_set_thread_context (th
);
441 th
->context
.ContextFlags
= 0;
444 if (ResumeThread (th
->h
) == (DWORD
) -1)
446 DWORD err
= GetLastError ();
447 OUTMSG (("warning: ResumeThread failed in continue_one_thread, "
448 "(error %d): %s\n", (int) err
, strwinerror (err
)));
456 child_continue (DWORD continue_status
, int thread_id
)
458 /* The inferior will only continue after the ContinueDebugEvent
460 for_each_thread ([&] (thread_info
*thread
)
462 continue_one_thread (thread
, thread_id
);
464 faked_breakpoint
= 0;
466 if (!ContinueDebugEvent (current_event
.dwProcessId
,
467 current_event
.dwThreadId
,
474 /* Fetch register(s) from the current thread context. */
476 child_fetch_inferior_registers (struct regcache
*regcache
, int r
)
479 win32_thread_info
*th
= thread_rec (current_thread_ptid (), TRUE
);
480 if (r
== -1 || r
> NUM_REGS
)
481 child_fetch_inferior_registers (regcache
, NUM_REGS
);
483 for (regno
= 0; regno
< r
; regno
++)
484 (*the_low_target
.fetch_inferior_register
) (regcache
, th
, regno
);
487 /* Store a new register value into the current thread context. We don't
488 change the program's context until later, when we resume it. */
490 child_store_inferior_registers (struct regcache
*regcache
, int r
)
493 win32_thread_info
*th
= thread_rec (current_thread_ptid (), TRUE
);
494 if (r
== -1 || r
== 0 || r
> NUM_REGS
)
495 child_store_inferior_registers (regcache
, NUM_REGS
);
497 for (regno
= 0; regno
< r
; regno
++)
498 (*the_low_target
.store_inferior_register
) (regcache
, th
, regno
);
501 /* Map the Windows error number in ERROR to a locale-dependent error
502 message string and return a pointer to it. Typically, the values
503 for ERROR come from GetLastError.
505 The string pointed to shall not be modified by the application,
506 but may be overwritten by a subsequent call to strwinerror
508 The strwinerror function does not change the current setting
512 strwinerror (DWORD error
)
514 static char buf
[1024];
516 DWORD lasterr
= GetLastError ();
517 DWORD chars
= FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
518 | FORMAT_MESSAGE_ALLOCATE_BUFFER
,
521 0, /* Default language */
527 /* If there is an \r\n appended, zap it. */
529 && msgbuf
[chars
- 2] == '\r'
530 && msgbuf
[chars
- 1] == '\n')
536 if (chars
> ((COUNTOF (buf
)) - 1))
538 chars
= COUNTOF (buf
) - 1;
543 wcstombs (buf
, msgbuf
, chars
+ 1);
545 strncpy (buf
, msgbuf
, chars
+ 1);
550 sprintf (buf
, "unknown win32 error (%u)", (unsigned) error
);
552 SetLastError (lasterr
);
557 create_process (const char *program
, char *args
,
558 DWORD flags
, PROCESS_INFORMATION
*pi
)
560 const char *inferior_cwd
= get_inferior_cwd ();
562 size_t argslen
, proglen
;
564 proglen
= strlen (program
) + 1;
565 argslen
= strlen (args
) + proglen
;
568 wchar_t *p
, *wprogram
, *wargs
, *wcwd
= NULL
;
570 wprogram
= (wchar_t *) alloca (proglen
* sizeof (wchar_t));
571 mbstowcs (wprogram
, program
, proglen
);
573 for (p
= wprogram
; *p
; ++p
)
577 wargs
= alloca ((argslen
+ 1) * sizeof (wchar_t));
578 wcscpy (wargs
, wprogram
);
579 wcscat (wargs
, L
" ");
580 mbstowcs (wargs
+ proglen
, args
, argslen
+ 1 - proglen
);
582 if (inferior_cwd
!= NULL
)
584 std::string expanded_infcwd
= gdb_tilde_expand (inferior_cwd
);
585 std::replace (expanded_infcwd
.begin (), expanded_infcwd
.end (),
587 wcwd
= alloca ((expanded_infcwd
.size () + 1) * sizeof (wchar_t));
588 if (mbstowcs (wcwd
, expanded_infcwd
.c_str (),
589 expanded_infcwd
.size () + 1) == NULL
)
592 Could not convert the expanded inferior cwd to wide-char."));
596 ret
= CreateProcessW (wprogram
, /* image name */
597 wargs
, /* command line */
598 NULL
, /* security, not supported */
599 NULL
, /* thread, not supported */
600 FALSE
, /* inherit handles, not supported */
601 flags
, /* start flags */
602 NULL
, /* environment, not supported */
603 wcwd
, /* current directory */
604 NULL
, /* start info, not supported */
607 STARTUPINFOA si
= { sizeof (STARTUPINFOA
) };
608 char *program_and_args
= (char *) alloca (argslen
+ 1);
610 strcpy (program_and_args
, program
);
611 strcat (program_and_args
, " ");
612 strcat (program_and_args
, args
);
613 ret
= CreateProcessA (program
, /* image name */
614 program_and_args
, /* command line */
617 TRUE
, /* inherit handles */
618 flags
, /* start flags */
619 NULL
, /* environment */
620 /* current directory */
621 (inferior_cwd
== NULL
623 : gdb_tilde_expand (inferior_cwd
).c_str()),
624 &si
, /* start info */
631 /* Start a new process.
632 PROGRAM is the program name.
633 PROGRAM_ARGS is the vector containing the inferior's args.
634 Returns the new PID on success, -1 on failure. Registers the new
635 process with the process list. */
637 win32_process_target::create_inferior (const char *program
,
638 const std::vector
<char *> &program_args
)
640 client_state
&cs
= get_client_state ();
642 char real_path
[PATH_MAX
];
643 char *orig_path
, *new_path
, *path_ptr
;
647 PROCESS_INFORMATION pi
;
649 std::string str_program_args
= stringify_argv (program_args
);
650 char *args
= (char *) str_program_args
.c_str ();
652 /* win32_wait needs to know we're not attaching. */
656 error ("No executable specified, specify executable to debug.\n");
658 flags
= DEBUG_PROCESS
| DEBUG_ONLY_THIS_PROCESS
;
662 path_ptr
= getenv ("PATH");
665 int size
= cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, NULL
, 0);
666 orig_path
= (char *) alloca (strlen (path_ptr
) + 1);
667 new_path
= (char *) alloca (size
);
668 strcpy (orig_path
, path_ptr
);
669 cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, new_path
, size
);
670 setenv ("PATH", new_path
, 1);
672 cygwin_conv_path (CCP_POSIX_TO_WIN_A
, program
, real_path
, PATH_MAX
);
676 OUTMSG2 (("Command line is \"%s %s\"\n", program
, args
));
678 #ifdef CREATE_NEW_PROCESS_GROUP
679 flags
|= CREATE_NEW_PROCESS_GROUP
;
682 ret
= create_process (program
, args
, flags
, &pi
);
683 err
= GetLastError ();
684 if (!ret
&& err
== ERROR_FILE_NOT_FOUND
)
686 char *exename
= (char *) alloca (strlen (program
) + 5);
687 strcat (strcpy (exename
, program
), ".exe");
688 ret
= create_process (exename
, args
, flags
, &pi
);
689 err
= GetLastError ();
694 setenv ("PATH", orig_path
, 1);
699 error ("Error creating process \"%s %s\", (error %d): %s\n",
700 program
, args
, (int) err
, strwinerror (err
));
704 OUTMSG2 (("Process created: %s %s\n", program
, (char *) args
));
708 /* On Windows CE this handle can't be closed. The OS reuses
709 it in the debug events, while the 9x/NT versions of Windows
710 probably use a DuplicateHandle'd one. */
711 CloseHandle (pi
.hThread
);
714 do_initial_child_stuff (pi
.hProcess
, pi
.dwProcessId
, 0);
716 /* Wait till we are at 1st instruction in program, return new pid
717 (assuming success). */
718 cs
.last_ptid
= win32_wait (ptid_t (current_process_id
), &cs
.last_status
, 0);
720 /* Necessary for handle_v_kill. */
721 signal_pid
= current_process_id
;
723 return current_process_id
;
726 /* Attach to a running process.
727 PID is the process ID to attach to, specified by the user
728 or a higher layer. */
730 win32_process_target::attach (unsigned long pid
)
733 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
736 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
738 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
740 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
742 h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
745 if (DebugActiveProcess (pid
))
747 if (DebugSetProcessKillOnExit
!= NULL
)
748 DebugSetProcessKillOnExit (FALSE
);
750 /* win32_wait needs to know we're attaching. */
752 do_initial_child_stuff (h
, pid
, 1);
759 err
= GetLastError ();
760 error ("Attach to process failed (error %d): %s\n",
761 (int) err
, strwinerror (err
));
764 /* Handle OUTPUT_DEBUG_STRING_EVENT from child process. */
766 handle_output_debug_string (void)
768 #define READ_BUFFER_LEN 1024
770 char s
[READ_BUFFER_LEN
+ 1] = { 0 };
771 DWORD nbytes
= current_event
.u
.DebugString
.nDebugStringLength
;
776 if (nbytes
> READ_BUFFER_LEN
)
777 nbytes
= READ_BUFFER_LEN
;
779 addr
= (CORE_ADDR
) (size_t) current_event
.u
.DebugString
.lpDebugStringData
;
781 if (current_event
.u
.DebugString
.fUnicode
)
783 /* The event tells us how many bytes, not chars, even
785 WCHAR buffer
[(READ_BUFFER_LEN
+ 1) / sizeof (WCHAR
)] = { 0 };
786 if (read_inferior_memory (addr
, (unsigned char *) buffer
, nbytes
) != 0)
788 wcstombs (s
, buffer
, (nbytes
+ 1) / sizeof (WCHAR
));
792 if (read_inferior_memory (addr
, (unsigned char *) s
, nbytes
) != 0)
796 if (!startswith (s
, "cYg"))
806 #undef READ_BUFFER_LEN
810 win32_clear_inferiors (void)
812 if (current_process_handle
!= NULL
)
813 CloseHandle (current_process_handle
);
815 for_each_thread (delete_thread_info
);
816 siginfo_er
.ExceptionCode
= 0;
820 /* Implementation of target_ops::kill. */
823 win32_process_target::kill (process_info
*process
)
825 TerminateProcess (current_process_handle
, 0);
828 if (!child_continue (DBG_CONTINUE
, -1))
830 if (!WaitForDebugEvent (¤t_event
, INFINITE
))
832 if (current_event
.dwDebugEventCode
== EXIT_PROCESS_DEBUG_EVENT
)
834 else if (current_event
.dwDebugEventCode
== OUTPUT_DEBUG_STRING_EVENT
)
835 handle_output_debug_string ();
838 win32_clear_inferiors ();
840 remove_process (process
);
844 /* Implementation of target_ops::detach. */
847 win32_process_target::detach (process_info
*process
)
849 winapi_DebugActiveProcessStop DebugActiveProcessStop
= NULL
;
850 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
852 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
854 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
856 DebugActiveProcessStop
= GETPROCADDRESS (dll
, DebugActiveProcessStop
);
857 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
859 if (DebugSetProcessKillOnExit
== NULL
860 || DebugActiveProcessStop
== NULL
)
864 struct thread_resume resume
;
865 resume
.thread
= minus_one_ptid
;
866 resume
.kind
= resume_continue
;
868 win32_resume (&resume
, 1);
871 if (!DebugActiveProcessStop (current_process_id
))
874 DebugSetProcessKillOnExit (FALSE
);
875 remove_process (process
);
877 win32_clear_inferiors ();
882 win32_mourn (struct process_info
*process
)
884 remove_process (process
);
887 /* Implementation of target_ops::join. */
892 HANDLE h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
895 WaitForSingleObject (h
, INFINITE
);
900 /* Return 1 iff the thread with thread ID TID is alive. */
902 win32_thread_alive (ptid_t ptid
)
904 /* Our thread list is reliable; don't bother to poll target
906 return find_thread_ptid (ptid
) != NULL
;
909 /* Resume the inferior process. RESUME_INFO describes how we want
912 win32_resume (struct thread_resume
*resume_info
, size_t n
)
917 win32_thread_info
*th
;
918 DWORD continue_status
= DBG_CONTINUE
;
921 /* This handles the very limited set of resume packets that GDB can
922 currently produce. */
924 if (n
== 1 && resume_info
[0].thread
== minus_one_ptid
)
929 /* Yes, we're ignoring resume_info[0].thread. It'd be tricky to make
930 the Windows resume code do the right thing for thread switching. */
931 tid
= current_event
.dwThreadId
;
933 if (resume_info
[0].thread
!= minus_one_ptid
)
935 sig
= gdb_signal_from_host (resume_info
[0].sig
);
936 step
= resume_info
[0].kind
== resume_step
;
944 if (sig
!= GDB_SIGNAL_0
)
946 if (current_event
.dwDebugEventCode
!= EXCEPTION_DEBUG_EVENT
)
948 OUTMSG (("Cannot continue with signal %s here.\n",
949 gdb_signal_to_string (sig
)));
951 else if (sig
== last_sig
)
952 continue_status
= DBG_EXCEPTION_NOT_HANDLED
;
954 OUTMSG (("Can only continue with received signal %s.\n",
955 gdb_signal_to_string (last_sig
)));
958 last_sig
= GDB_SIGNAL_0
;
960 /* Get context for the currently selected thread. */
961 ptid
= debug_event_ptid (¤t_event
);
962 th
= thread_rec (ptid
, FALSE
);
965 win32_prepare_to_resume (th
);
967 if (th
->context
.ContextFlags
)
969 /* Move register values from the inferior into the thread
970 context structure. */
971 regcache_invalidate ();
975 if (the_low_target
.single_step
!= NULL
)
976 (*the_low_target
.single_step
) (th
);
978 error ("Single stepping is not supported "
979 "in this configuration.\n");
982 win32_set_thread_context (th
);
983 th
->context
.ContextFlags
= 0;
987 /* Allow continuing with the same signal that interrupted us.
988 Otherwise complain. */
990 child_continue (continue_status
, tid
);
994 win32_add_one_solib (const char *name
, CORE_ADDR load_addr
)
996 char buf
[MAX_PATH
+ 1];
997 char buf2
[MAX_PATH
+ 1];
1000 WIN32_FIND_DATA w32_fd
;
1001 WCHAR wname
[MAX_PATH
+ 1];
1002 mbstowcs (wname
, name
, MAX_PATH
);
1003 HANDLE h
= FindFirstFile (wname
, &w32_fd
);
1005 WIN32_FIND_DATAA w32_fd
;
1006 HANDLE h
= FindFirstFileA (name
, &w32_fd
);
1009 /* The symbols in a dll are offset by 0x1000, which is the
1010 offset from 0 of the first byte in an image - because
1011 of the file header and the section alignment. */
1012 load_addr
+= 0x1000;
1014 if (h
== INVALID_HANDLE_VALUE
)
1022 char cwd
[MAX_PATH
+ 1];
1024 if (GetCurrentDirectoryA (MAX_PATH
+ 1, cwd
))
1026 p
= strrchr (buf
, '\\');
1029 SetCurrentDirectoryA (buf
);
1030 GetFullPathNameA (w32_fd
.cFileName
, MAX_PATH
, buf
, &p
);
1031 SetCurrentDirectoryA (cwd
);
1038 if (strcasecmp (buf
, "ntdll.dll") == 0)
1040 GetSystemDirectoryA (buf
, sizeof (buf
));
1041 strcat (buf
, "\\ntdll.dll");
1046 cygwin_conv_path (CCP_WIN_A_TO_POSIX
, buf
, buf2
, sizeof (buf2
));
1051 loaded_dll (buf2
, load_addr
);
1055 get_image_name (HANDLE h
, void *address
, int unicode
)
1057 static char buf
[(2 * MAX_PATH
) + 1];
1058 DWORD size
= unicode
? sizeof (WCHAR
) : sizeof (char);
1064 /* Attempt to read the name of the dll that was detected.
1065 This is documented to work only when actively debugging
1066 a program. It will not work for attached processes. */
1067 if (address
== NULL
)
1071 /* Windows CE reports the address of the image name,
1072 instead of an address of a pointer into the image name. */
1073 address_ptr
= address
;
1075 /* See if we could read the address of a string, and that the
1076 address isn't null. */
1077 if (!ReadProcessMemory (h
, address
, &address_ptr
,
1078 sizeof (address_ptr
), &done
)
1079 || done
!= sizeof (address_ptr
)
1084 /* Find the length of the string */
1085 while (ReadProcessMemory (h
, address_ptr
+ len
++ * size
, &b
, size
, &done
)
1086 && (b
[0] != 0 || b
[size
- 1] != 0) && done
== size
)
1090 ReadProcessMemory (h
, address_ptr
, buf
, len
, &done
);
1093 WCHAR
*unicode_address
= XALLOCAVEC (WCHAR
, len
);
1094 ReadProcessMemory (h
, address_ptr
, unicode_address
, len
* sizeof (WCHAR
),
1097 WideCharToMultiByte (CP_ACP
, 0, unicode_address
, len
, buf
, len
, 0, 0);
1103 typedef BOOL (WINAPI
*winapi_EnumProcessModules
) (HANDLE
, HMODULE
*,
1105 typedef BOOL (WINAPI
*winapi_GetModuleInformation
) (HANDLE
, HMODULE
,
1106 LPMODULEINFO
, DWORD
);
1107 typedef DWORD (WINAPI
*winapi_GetModuleFileNameExA
) (HANDLE
, HMODULE
,
1110 static winapi_EnumProcessModules win32_EnumProcessModules
;
1111 static winapi_GetModuleInformation win32_GetModuleInformation
;
1112 static winapi_GetModuleFileNameExA win32_GetModuleFileNameExA
;
1117 static int psapi_loaded
= 0;
1118 static HMODULE dll
= NULL
;
1123 dll
= LoadLibrary (TEXT("psapi.dll"));
1126 win32_EnumProcessModules
=
1127 GETPROCADDRESS (dll
, EnumProcessModules
);
1128 win32_GetModuleInformation
=
1129 GETPROCADDRESS (dll
, GetModuleInformation
);
1130 win32_GetModuleFileNameExA
=
1131 GETPROCADDRESS (dll
, GetModuleFileNameExA
);
1134 return (win32_EnumProcessModules
!= NULL
1135 && win32_GetModuleInformation
!= NULL
1136 && win32_GetModuleFileNameExA
!= NULL
);
1141 /* Iterate over all DLLs currently mapped by our inferior, and
1142 add them to our list of solibs. */
1145 win32_add_all_dlls (void)
1149 HMODULE
*DllHandle
= dh_buf
;
1157 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1162 if (!ok
|| !cbNeeded
)
1165 DllHandle
= (HMODULE
*) alloca (cbNeeded
);
1169 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1176 for (i
= 1; i
< ((size_t) cbNeeded
/ sizeof (HMODULE
)); i
++)
1179 char dll_name
[MAX_PATH
];
1181 if (!(*win32_GetModuleInformation
) (current_process_handle
,
1186 if ((*win32_GetModuleFileNameExA
) (current_process_handle
,
1191 win32_add_one_solib (dll_name
, (CORE_ADDR
) (uintptr_t) mi
.lpBaseOfDll
);
1196 typedef HANDLE (WINAPI
*winapi_CreateToolhelp32Snapshot
) (DWORD
, DWORD
);
1197 typedef BOOL (WINAPI
*winapi_Module32First
) (HANDLE
, LPMODULEENTRY32
);
1198 typedef BOOL (WINAPI
*winapi_Module32Next
) (HANDLE
, LPMODULEENTRY32
);
1200 /* Handle a DLL load event.
1202 This function assumes that this event did not occur during inferior
1203 initialization, where their event info may be incomplete (see
1204 do_initial_child_stuff and win32_add_all_dlls for more info on
1205 how we handle DLL loading during that phase). */
1208 handle_load_dll (void)
1210 LOAD_DLL_DEBUG_INFO
*event
= ¤t_event
.u
.LoadDll
;
1213 dll_name
= get_image_name (current_process_handle
,
1214 event
->lpImageName
, event
->fUnicode
);
1218 win32_add_one_solib (dll_name
, (CORE_ADDR
) (uintptr_t) event
->lpBaseOfDll
);
1221 /* Handle a DLL unload event.
1223 This function assumes that this event did not occur during inferior
1224 initialization, where their event info may be incomplete (see
1225 do_initial_child_stuff and win32_add_one_solib for more info
1226 on how we handle DLL loading during that phase). */
1229 handle_unload_dll (void)
1231 CORE_ADDR load_addr
=
1232 (CORE_ADDR
) (uintptr_t) current_event
.u
.UnloadDll
.lpBaseOfDll
;
1234 /* The symbols in a dll are offset by 0x1000, which is the
1235 offset from 0 of the first byte in an image - because
1236 of the file header and the section alignment. */
1237 load_addr
+= 0x1000;
1238 unloaded_dll (NULL
, load_addr
);
1242 handle_exception (struct target_waitstatus
*ourstatus
)
1244 DWORD code
= current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
;
1246 memcpy (&siginfo_er
, ¤t_event
.u
.Exception
.ExceptionRecord
,
1249 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
1253 case EXCEPTION_ACCESS_VIOLATION
:
1254 OUTMSG2 (("EXCEPTION_ACCESS_VIOLATION"));
1255 ourstatus
->value
.sig
= GDB_SIGNAL_SEGV
;
1257 case STATUS_STACK_OVERFLOW
:
1258 OUTMSG2 (("STATUS_STACK_OVERFLOW"));
1259 ourstatus
->value
.sig
= GDB_SIGNAL_SEGV
;
1261 case STATUS_FLOAT_DENORMAL_OPERAND
:
1262 OUTMSG2 (("STATUS_FLOAT_DENORMAL_OPERAND"));
1263 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1265 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
1266 OUTMSG2 (("EXCEPTION_ARRAY_BOUNDS_EXCEEDED"));
1267 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1269 case STATUS_FLOAT_INEXACT_RESULT
:
1270 OUTMSG2 (("STATUS_FLOAT_INEXACT_RESULT"));
1271 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1273 case STATUS_FLOAT_INVALID_OPERATION
:
1274 OUTMSG2 (("STATUS_FLOAT_INVALID_OPERATION"));
1275 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1277 case STATUS_FLOAT_OVERFLOW
:
1278 OUTMSG2 (("STATUS_FLOAT_OVERFLOW"));
1279 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1281 case STATUS_FLOAT_STACK_CHECK
:
1282 OUTMSG2 (("STATUS_FLOAT_STACK_CHECK"));
1283 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1285 case STATUS_FLOAT_UNDERFLOW
:
1286 OUTMSG2 (("STATUS_FLOAT_UNDERFLOW"));
1287 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1289 case STATUS_FLOAT_DIVIDE_BY_ZERO
:
1290 OUTMSG2 (("STATUS_FLOAT_DIVIDE_BY_ZERO"));
1291 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1293 case STATUS_INTEGER_DIVIDE_BY_ZERO
:
1294 OUTMSG2 (("STATUS_INTEGER_DIVIDE_BY_ZERO"));
1295 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1297 case STATUS_INTEGER_OVERFLOW
:
1298 OUTMSG2 (("STATUS_INTEGER_OVERFLOW"));
1299 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1301 case EXCEPTION_BREAKPOINT
:
1302 OUTMSG2 (("EXCEPTION_BREAKPOINT"));
1303 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1305 /* Remove the initial breakpoint. */
1306 check_breakpoints ((CORE_ADDR
) (long) current_event
1307 .u
.Exception
.ExceptionRecord
.ExceptionAddress
);
1311 OUTMSG2 (("DBG_CONTROL_C"));
1312 ourstatus
->value
.sig
= GDB_SIGNAL_INT
;
1314 case DBG_CONTROL_BREAK
:
1315 OUTMSG2 (("DBG_CONTROL_BREAK"));
1316 ourstatus
->value
.sig
= GDB_SIGNAL_INT
;
1318 case EXCEPTION_SINGLE_STEP
:
1319 OUTMSG2 (("EXCEPTION_SINGLE_STEP"));
1320 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1322 case EXCEPTION_ILLEGAL_INSTRUCTION
:
1323 OUTMSG2 (("EXCEPTION_ILLEGAL_INSTRUCTION"));
1324 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1326 case EXCEPTION_PRIV_INSTRUCTION
:
1327 OUTMSG2 (("EXCEPTION_PRIV_INSTRUCTION"));
1328 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1330 case EXCEPTION_NONCONTINUABLE_EXCEPTION
:
1331 OUTMSG2 (("EXCEPTION_NONCONTINUABLE_EXCEPTION"));
1332 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1335 if (current_event
.u
.Exception
.dwFirstChance
)
1337 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1340 OUTMSG2 (("gdbserver: unknown target exception 0x%08x at 0x%s",
1341 (unsigned) current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
,
1342 phex_nz ((uintptr_t) current_event
.u
.Exception
.ExceptionRecord
.
1343 ExceptionAddress
, sizeof (uintptr_t))));
1344 ourstatus
->value
.sig
= GDB_SIGNAL_UNKNOWN
;
1348 last_sig
= ourstatus
->value
.sig
;
1353 suspend_one_thread (thread_info
*thread
)
1355 win32_thread_info
*th
= (win32_thread_info
*) thread_target_data (thread
);
1359 if (SuspendThread (th
->h
) == (DWORD
) -1)
1361 DWORD err
= GetLastError ();
1362 OUTMSG (("warning: SuspendThread failed in suspend_one_thread, "
1363 "(error %d): %s\n", (int) err
, strwinerror (err
)));
1371 fake_breakpoint_event (void)
1373 OUTMSG2(("fake_breakpoint_event\n"));
1375 faked_breakpoint
= 1;
1377 memset (¤t_event
, 0, sizeof (current_event
));
1378 current_event
.dwThreadId
= main_thread_id
;
1379 current_event
.dwDebugEventCode
= EXCEPTION_DEBUG_EVENT
;
1380 current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
1381 = EXCEPTION_BREAKPOINT
;
1383 for_each_thread (suspend_one_thread
);
1388 auto_delete_breakpoint (CORE_ADDR stop_pc
)
1394 /* Get the next event from the child. */
1397 get_child_debug_event (struct target_waitstatus
*ourstatus
)
1401 last_sig
= GDB_SIGNAL_0
;
1402 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1404 /* Check if GDB sent us an interrupt request. */
1405 check_remote_input_interrupt_request ();
1407 if (soft_interrupt_requested
)
1409 soft_interrupt_requested
= 0;
1410 fake_breakpoint_event ();
1419 /* WinCE doesn't set an initial breakpoint automatically. To
1420 stop the inferior, we flush all currently pending debug
1421 events -- the thread list and the dll list are always
1422 reported immediatelly without delay, then, we suspend all
1423 threads and pretend we saw a trap at the current PC of the
1426 Contrary to desktop Windows, Windows CE *does* report the dll
1427 names on LOAD_DLL_DEBUG_EVENTs resulting from a
1428 DebugActiveProcess call. This limits the way we can detect
1429 if all the dlls have already been reported. If we get a real
1430 debug event before leaving attaching, the worst that will
1431 happen is the user will see a spurious breakpoint. */
1433 current_event
.dwDebugEventCode
= 0;
1434 if (!WaitForDebugEvent (¤t_event
, 0))
1436 OUTMSG2(("no attach events left\n"));
1437 fake_breakpoint_event ();
1441 OUTMSG2(("got attach event\n"));
1446 /* Keep the wait time low enough for comfortable remote
1447 interruption, but high enough so gdbserver doesn't become a
1449 if (!WaitForDebugEvent (¤t_event
, 250))
1451 DWORD e
= GetLastError();
1453 if (e
== ERROR_PIPE_NOT_CONNECTED
)
1455 /* This will happen if the loader fails to succesfully
1456 load the application, e.g., if the main executable
1457 tries to pull in a non-existing export from a
1459 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1460 ourstatus
->value
.integer
= 1;
1470 switch (current_event
.dwDebugEventCode
)
1472 case CREATE_THREAD_DEBUG_EVENT
:
1473 OUTMSG2 (("gdbserver: kernel event CREATE_THREAD_DEBUG_EVENT "
1474 "for pid=%u tid=%x)\n",
1475 (unsigned) current_event
.dwProcessId
,
1476 (unsigned) current_event
.dwThreadId
));
1478 /* Record the existence of this thread. */
1479 child_add_thread (current_event
.dwProcessId
,
1480 current_event
.dwThreadId
,
1481 current_event
.u
.CreateThread
.hThread
,
1482 current_event
.u
.CreateThread
.lpThreadLocalBase
);
1485 case EXIT_THREAD_DEBUG_EVENT
:
1486 OUTMSG2 (("gdbserver: kernel event EXIT_THREAD_DEBUG_EVENT "
1487 "for pid=%u tid=%x\n",
1488 (unsigned) current_event
.dwProcessId
,
1489 (unsigned) current_event
.dwThreadId
));
1490 child_delete_thread (current_event
.dwProcessId
,
1491 current_event
.dwThreadId
);
1493 current_thread
= get_first_thread ();
1496 case CREATE_PROCESS_DEBUG_EVENT
:
1497 OUTMSG2 (("gdbserver: kernel event CREATE_PROCESS_DEBUG_EVENT "
1498 "for pid=%u tid=%x\n",
1499 (unsigned) current_event
.dwProcessId
,
1500 (unsigned) current_event
.dwThreadId
));
1501 CloseHandle (current_event
.u
.CreateProcessInfo
.hFile
);
1503 current_process_handle
= current_event
.u
.CreateProcessInfo
.hProcess
;
1504 main_thread_id
= current_event
.dwThreadId
;
1506 /* Add the main thread. */
1507 child_add_thread (current_event
.dwProcessId
,
1509 current_event
.u
.CreateProcessInfo
.hThread
,
1510 current_event
.u
.CreateProcessInfo
.lpThreadLocalBase
);
1515 /* Windows CE doesn't set the initial breakpoint
1516 automatically like the desktop versions of Windows do.
1517 We add it explicitly here. It will be removed as soon as
1519 set_breakpoint_at ((CORE_ADDR
) (long) current_event
.u
1520 .CreateProcessInfo
.lpStartAddress
,
1521 auto_delete_breakpoint
);
1526 case EXIT_PROCESS_DEBUG_EVENT
:
1527 OUTMSG2 (("gdbserver: kernel event EXIT_PROCESS_DEBUG_EVENT "
1528 "for pid=%u tid=%x\n",
1529 (unsigned) current_event
.dwProcessId
,
1530 (unsigned) current_event
.dwThreadId
));
1532 DWORD exit_status
= current_event
.u
.ExitProcess
.dwExitCode
;
1533 /* If the exit status looks like a fatal exception, but we
1534 don't recognize the exception's code, make the original
1535 exit status value available, to avoid losing information. */
1537 = WIFSIGNALED (exit_status
) ? WTERMSIG (exit_status
) : -1;
1538 if (exit_signal
== -1)
1540 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1541 ourstatus
->value
.integer
= exit_status
;
1545 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
1546 ourstatus
->value
.sig
= gdb_signal_from_host (exit_signal
);
1549 child_continue (DBG_CONTINUE
, -1);
1550 CloseHandle (current_process_handle
);
1551 current_process_handle
= NULL
;
1554 case LOAD_DLL_DEBUG_EVENT
:
1555 OUTMSG2 (("gdbserver: kernel event LOAD_DLL_DEBUG_EVENT "
1556 "for pid=%u tid=%x\n",
1557 (unsigned) current_event
.dwProcessId
,
1558 (unsigned) current_event
.dwThreadId
));
1559 CloseHandle (current_event
.u
.LoadDll
.hFile
);
1560 if (! child_initialization_done
)
1564 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1565 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1568 case UNLOAD_DLL_DEBUG_EVENT
:
1569 OUTMSG2 (("gdbserver: kernel event UNLOAD_DLL_DEBUG_EVENT "
1570 "for pid=%u tid=%x\n",
1571 (unsigned) current_event
.dwProcessId
,
1572 (unsigned) current_event
.dwThreadId
));
1573 if (! child_initialization_done
)
1575 handle_unload_dll ();
1576 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1577 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1580 case EXCEPTION_DEBUG_EVENT
:
1581 OUTMSG2 (("gdbserver: kernel event EXCEPTION_DEBUG_EVENT "
1582 "for pid=%u tid=%x\n",
1583 (unsigned) current_event
.dwProcessId
,
1584 (unsigned) current_event
.dwThreadId
));
1585 handle_exception (ourstatus
);
1588 case OUTPUT_DEBUG_STRING_EVENT
:
1589 /* A message from the kernel (or Cygwin). */
1590 OUTMSG2 (("gdbserver: kernel event OUTPUT_DEBUG_STRING_EVENT "
1591 "for pid=%u tid=%x\n",
1592 (unsigned) current_event
.dwProcessId
,
1593 (unsigned) current_event
.dwThreadId
));
1594 handle_output_debug_string ();
1598 OUTMSG2 (("gdbserver: kernel event unknown "
1599 "for pid=%u tid=%x code=%x\n",
1600 (unsigned) current_event
.dwProcessId
,
1601 (unsigned) current_event
.dwThreadId
,
1602 (unsigned) current_event
.dwDebugEventCode
));
1606 ptid
= debug_event_ptid (¤t_event
);
1607 current_thread
= find_thread_ptid (ptid
);
1611 /* Wait for the inferior process to change state.
1612 STATUS will be filled in with a response code to send to GDB.
1613 Returns the signal which caused the process to stop. */
1615 win32_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
, int options
)
1617 struct regcache
*regcache
;
1619 if (cached_status
.kind
!= TARGET_WAITKIND_IGNORE
)
1621 /* The core always does a wait after creating the inferior, and
1622 do_initial_child_stuff already ran the inferior to the
1623 initial breakpoint (or an exit, if creating the process
1624 fails). Report it now. */
1625 *ourstatus
= cached_status
;
1626 cached_status
.kind
= TARGET_WAITKIND_IGNORE
;
1627 return debug_event_ptid (¤t_event
);
1632 if (!get_child_debug_event (ourstatus
))
1635 switch (ourstatus
->kind
)
1637 case TARGET_WAITKIND_EXITED
:
1638 OUTMSG2 (("Child exited with retcode = %x\n",
1639 ourstatus
->value
.integer
));
1640 win32_clear_inferiors ();
1641 return ptid_t (current_event
.dwProcessId
);
1642 case TARGET_WAITKIND_STOPPED
:
1643 case TARGET_WAITKIND_SIGNALLED
:
1644 case TARGET_WAITKIND_LOADED
:
1645 OUTMSG2 (("Child Stopped with signal = %d \n",
1646 ourstatus
->value
.sig
));
1648 regcache
= get_thread_regcache (current_thread
, 1);
1649 child_fetch_inferior_registers (regcache
, -1);
1650 return debug_event_ptid (¤t_event
);
1652 OUTMSG (("Ignoring unknown internal event, %d\n", ourstatus
->kind
));
1654 case TARGET_WAITKIND_SPURIOUS
:
1655 /* do nothing, just continue */
1656 child_continue (DBG_CONTINUE
, -1);
1662 /* Fetch registers from the inferior process.
1663 If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO. */
1665 win32_fetch_inferior_registers (struct regcache
*regcache
, int regno
)
1667 child_fetch_inferior_registers (regcache
, regno
);
1670 /* Store registers to the inferior process.
1671 If REGNO is -1, store all registers; otherwise, store at least REGNO. */
1673 win32_store_inferior_registers (struct regcache
*regcache
, int regno
)
1675 child_store_inferior_registers (regcache
, regno
);
1678 /* Read memory from the inferior process. This should generally be
1679 called through read_inferior_memory, which handles breakpoint shadowing.
1680 Read LEN bytes at MEMADDR into a buffer at MYADDR. */
1682 win32_read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
1684 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 0, 0) != len
;
1687 /* Write memory to the inferior process. This should generally be
1688 called through write_inferior_memory, which handles breakpoint shadowing.
1689 Write LEN bytes from the buffer at MYADDR to MEMADDR.
1690 Returns 0 on success and errno on failure. */
1692 win32_write_inferior_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
,
1695 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 1, 0) != len
;
1698 /* Send an interrupt request to the inferior process. */
1700 win32_request_interrupt (void)
1702 winapi_DebugBreakProcess DebugBreakProcess
;
1703 winapi_GenerateConsoleCtrlEvent GenerateConsoleCtrlEvent
;
1706 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
1708 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
1711 GenerateConsoleCtrlEvent
= GETPROCADDRESS (dll
, GenerateConsoleCtrlEvent
);
1713 if (GenerateConsoleCtrlEvent
!= NULL
1714 && GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT
, current_process_id
))
1717 /* GenerateConsoleCtrlEvent can fail if process id being debugged is
1718 not a process group id.
1719 Fallback to XP/Vista 'DebugBreakProcess', which generates a
1720 breakpoint exception in the interior process. */
1722 DebugBreakProcess
= GETPROCADDRESS (dll
, DebugBreakProcess
);
1724 if (DebugBreakProcess
!= NULL
1725 && DebugBreakProcess (current_process_handle
))
1728 /* Last resort, suspend all threads manually. */
1729 soft_interrupt_requested
= 1;
1734 win32_error_to_fileio_error (DWORD err
)
1738 case ERROR_BAD_PATHNAME
:
1739 case ERROR_FILE_NOT_FOUND
:
1740 case ERROR_INVALID_NAME
:
1741 case ERROR_PATH_NOT_FOUND
:
1742 return FILEIO_ENOENT
;
1744 case ERROR_IO_DEVICE
:
1745 case ERROR_OPEN_FAILED
:
1747 case ERROR_INVALID_HANDLE
:
1748 return FILEIO_EBADF
;
1749 case ERROR_ACCESS_DENIED
:
1750 case ERROR_SHARING_VIOLATION
:
1751 return FILEIO_EACCES
;
1752 case ERROR_NOACCESS
:
1753 return FILEIO_EFAULT
;
1755 return FILEIO_EBUSY
;
1756 case ERROR_ALREADY_EXISTS
:
1757 case ERROR_FILE_EXISTS
:
1758 return FILEIO_EEXIST
;
1759 case ERROR_BAD_DEVICE
:
1760 return FILEIO_ENODEV
;
1761 case ERROR_DIRECTORY
:
1762 return FILEIO_ENOTDIR
;
1763 case ERROR_FILENAME_EXCED_RANGE
:
1764 case ERROR_INVALID_DATA
:
1765 case ERROR_INVALID_PARAMETER
:
1766 case ERROR_NEGATIVE_SEEK
:
1767 return FILEIO_EINVAL
;
1768 case ERROR_TOO_MANY_OPEN_FILES
:
1769 return FILEIO_EMFILE
;
1770 case ERROR_HANDLE_DISK_FULL
:
1771 case ERROR_DISK_FULL
:
1772 return FILEIO_ENOSPC
;
1773 case ERROR_WRITE_PROTECT
:
1774 return FILEIO_EROFS
;
1775 case ERROR_NOT_SUPPORTED
:
1776 return FILEIO_ENOSYS
;
1779 return FILEIO_EUNKNOWN
;
1783 wince_hostio_last_error (char *buf
)
1785 DWORD winerr
= GetLastError ();
1786 int fileio_err
= win32_error_to_fileio_error (winerr
);
1787 sprintf (buf
, "F-1,%x", fileio_err
);
1791 /* Write Windows signal info. */
1794 win32_xfer_siginfo (const char *annex
, unsigned char *readbuf
,
1795 unsigned const char *writebuf
, CORE_ADDR offset
, int len
)
1797 if (siginfo_er
.ExceptionCode
== 0)
1800 if (readbuf
== nullptr)
1803 if (offset
> sizeof (siginfo_er
))
1806 if (offset
+ len
> sizeof (siginfo_er
))
1807 len
= sizeof (siginfo_er
) - offset
;
1809 memcpy (readbuf
, (char *) &siginfo_er
+ offset
, len
);
1814 /* Write Windows OS Thread Information Block address. */
1817 win32_get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
)
1819 win32_thread_info
*th
;
1820 th
= thread_rec (ptid
, 0);
1824 *addr
= th
->thread_local_base
;
1828 /* Implementation of the target_ops method "sw_breakpoint_from_kind". */
1830 static const gdb_byte
*
1831 win32_sw_breakpoint_from_kind (int kind
, int *size
)
1833 *size
= the_low_target
.breakpoint_len
;
1834 return the_low_target
.breakpoint
;
1837 /* The win32 target ops object. */
1839 static win32_process_target the_win32_target
;
1841 static process_stratum_target win32_target_ops
= {
1847 win32_fetch_inferior_registers
,
1848 win32_store_inferior_registers
,
1849 NULL
, /* prepare_to_access_memory */
1850 NULL
, /* done_accessing_memory */
1851 win32_read_inferior_memory
,
1852 win32_write_inferior_memory
,
1853 NULL
, /* lookup_symbols */
1854 win32_request_interrupt
,
1855 NULL
, /* read_auxv */
1856 win32_supports_z_point_type
,
1859 NULL
, /* stopped_by_sw_breakpoint */
1860 NULL
, /* supports_stopped_by_sw_breakpoint */
1861 NULL
, /* stopped_by_hw_breakpoint */
1862 NULL
, /* supports_stopped_by_hw_breakpoint */
1863 target_can_do_hardware_single_step
,
1864 win32_stopped_by_watchpoint
,
1865 win32_stopped_data_address
,
1866 NULL
, /* read_offsets */
1867 NULL
, /* get_tls_address */
1869 wince_hostio_last_error
,
1871 hostio_last_error_from_errno
,
1873 NULL
, /* qxfer_osdata */
1875 NULL
, /* supports_non_stop */
1877 NULL
, /* start_non_stop */
1878 NULL
, /* supports_multi_process */
1879 NULL
, /* supports_fork_events */
1880 NULL
, /* supports_vfork_events */
1881 NULL
, /* supports_exec_events */
1882 NULL
, /* handle_new_gdb_connection */
1883 NULL
, /* handle_monitor_command */
1884 NULL
, /* core_of_thread */
1885 NULL
, /* read_loadmap */
1886 NULL
, /* process_qsupported */
1887 NULL
, /* supports_tracepoints */
1889 NULL
, /* write_pc */
1890 NULL
, /* thread_stopped */
1891 win32_get_tib_address
,
1892 NULL
, /* pause_all */
1893 NULL
, /* unpause_all */
1894 NULL
, /* stabilize_threads */
1895 NULL
, /* install_fast_tracepoint_jump_pad */
1896 NULL
, /* emit_ops */
1897 NULL
, /* supports_disable_randomization */
1898 NULL
, /* get_min_fast_tracepoint_insn_len */
1899 NULL
, /* qxfer_libraries_svr4 */
1900 NULL
, /* support_agent */
1901 NULL
, /* enable_btrace */
1902 NULL
, /* disable_btrace */
1903 NULL
, /* read_btrace */
1904 NULL
, /* read_btrace_conf */
1905 NULL
, /* supports_range_stepping */
1906 NULL
, /* pid_to_exec_file */
1907 NULL
, /* multifs_open */
1908 NULL
, /* multifs_unlink */
1909 NULL
, /* multifs_readlink */
1910 NULL
, /* breakpoint_kind_from_pc */
1911 win32_sw_breakpoint_from_kind
,
1912 NULL
, /* thread_name */
1913 NULL
, /* breakpoint_kind_from_current_state */
1914 NULL
, /* supports_software_single_step */
1915 NULL
, /* supports_catch_syscall */
1916 NULL
, /* get_ipa_tdesc_idx */
1917 NULL
, /* thread_handle */
1921 /* Initialize the Win32 backend. */
1923 initialize_low (void)
1925 set_target_ops (&win32_target_ops
);
1926 the_low_target
.arch_setup ();