1 /* Low level interface to Windows debugging, for gdbserver.
2 Copyright (C) 2006-2018 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 "gdb_tilde_expand.h"
36 #include "common-inferior.h"
39 #include <sys/cygwin.h>
42 #define OUTMSG(X) do { printf X; fflush (stderr); } while (0)
55 #define _T(x) TEXT (x)
59 #define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0]))
63 # define GETPROCADDRESS(DLL, PROC) \
64 ((winapi_ ## PROC) GetProcAddress (DLL, TEXT (#PROC)))
66 # define GETPROCADDRESS(DLL, PROC) \
67 ((winapi_ ## PROC) GetProcAddress (DLL, #PROC))
70 int using_threads
= 1;
73 static int attaching
= 0;
74 static HANDLE current_process_handle
= NULL
;
75 static DWORD current_process_id
= 0;
76 static DWORD main_thread_id
= 0;
77 static enum gdb_signal last_sig
= GDB_SIGNAL_0
;
79 /* The current debug event from WaitForDebugEvent. */
80 static DEBUG_EVENT current_event
;
82 /* A status that hasn't been reported to the core yet, and so
83 win32_wait should return it next, instead of fetching the next
84 debug event off the win32 API. */
85 static struct target_waitstatus cached_status
;
87 /* Non zero if an interrupt request is to be satisfied by suspending
89 static int soft_interrupt_requested
= 0;
91 /* Non zero if the inferior is stopped in a simulated breakpoint done
92 by suspending all the threads. */
93 static int faked_breakpoint
= 0;
95 const struct target_desc
*win32_tdesc
;
97 #define NUM_REGS (the_low_target.num_regs)
99 typedef BOOL (WINAPI
*winapi_DebugActiveProcessStop
) (DWORD dwProcessId
);
100 typedef BOOL (WINAPI
*winapi_DebugSetProcessKillOnExit
) (BOOL KillOnExit
);
101 typedef BOOL (WINAPI
*winapi_DebugBreakProcess
) (HANDLE
);
102 typedef BOOL (WINAPI
*winapi_GenerateConsoleCtrlEvent
) (DWORD
, DWORD
);
104 static ptid_t
win32_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
,
106 static void win32_resume (struct thread_resume
*resume_info
, size_t n
);
108 static void win32_add_all_dlls (void);
111 /* Get the thread ID from the current selected inferior (the current
114 current_thread_ptid (void)
119 /* The current debug event from WaitForDebugEvent. */
121 debug_event_ptid (DEBUG_EVENT
*event
)
123 return ptid_build (event
->dwProcessId
, event
->dwThreadId
, 0);
126 /* Get the thread context of the thread associated with TH. */
129 win32_get_thread_context (win32_thread_info
*th
)
131 memset (&th
->context
, 0, sizeof (CONTEXT
));
132 (*the_low_target
.get_thread_context
) (th
);
134 memcpy (&th
->base_context
, &th
->context
, sizeof (CONTEXT
));
138 /* Set the thread context of the thread associated with TH. */
141 win32_set_thread_context (win32_thread_info
*th
)
144 /* Calling SuspendThread on a thread that is running kernel code
145 will report that the suspending was successful, but in fact, that
146 will often not be true. In those cases, the context returned by
147 GetThreadContext will not be correct by the time the thread
148 stops, hence we can't set that context back into the thread when
149 resuming - it will most likelly crash the inferior.
150 Unfortunately, there is no way to know when the thread will
151 really stop. To work around it, we'll only write the context
152 back to the thread when either the user or GDB explicitly change
153 it between stopping and resuming. */
154 if (memcmp (&th
->context
, &th
->base_context
, sizeof (CONTEXT
)) != 0)
156 SetThreadContext (th
->h
, &th
->context
);
159 /* Set the thread context of the thread associated with TH. */
162 win32_prepare_to_resume (win32_thread_info
*th
)
164 if (the_low_target
.prepare_to_resume
!= NULL
)
165 (*the_low_target
.prepare_to_resume
) (th
);
168 /* See win32-low.h. */
171 win32_require_context (win32_thread_info
*th
)
173 if (th
->context
.ContextFlags
== 0)
177 if (SuspendThread (th
->h
) == (DWORD
) -1)
179 DWORD err
= GetLastError ();
180 OUTMSG (("warning: SuspendThread failed in thread_rec, "
181 "(error %d): %s\n", (int) err
, strwinerror (err
)));
187 win32_get_thread_context (th
);
191 /* Find a thread record given a thread id. If GET_CONTEXT is set then
192 also retrieve the context for this thread. */
193 static win32_thread_info
*
194 thread_rec (ptid_t ptid
, int get_context
)
196 thread_info
*thread
= find_thread_ptid (ptid
);
200 win32_thread_info
*th
= (win32_thread_info
*) thread_target_data (thread
);
202 win32_require_context (th
);
206 /* Add a thread to the thread list. */
207 static win32_thread_info
*
208 child_add_thread (DWORD pid
, DWORD tid
, HANDLE h
, void *tlb
)
210 win32_thread_info
*th
;
211 ptid_t ptid
= ptid_build (pid
, tid
, 0);
213 if ((th
= thread_rec (ptid
, FALSE
)))
216 th
= XCNEW (win32_thread_info
);
219 th
->thread_local_base
= (CORE_ADDR
) (uintptr_t) tlb
;
221 add_thread (ptid
, th
);
223 if (the_low_target
.thread_added
!= NULL
)
224 (*the_low_target
.thread_added
) (th
);
229 /* Delete a thread from the list of threads. */
231 delete_thread_info (thread_info
*thread
)
233 win32_thread_info
*th
= (win32_thread_info
*) thread_target_data (thread
);
235 remove_thread (thread
);
240 /* Delete a thread from the list of threads. */
242 child_delete_thread (DWORD pid
, DWORD tid
)
244 /* If the last thread is exiting, just return. */
245 if (all_threads
.size () == 1)
248 thread_info
*thread
= find_thread_ptid (ptid_t (pid
, tid
));
252 delete_thread_info (thread
);
255 /* These watchpoint related wrapper functions simply pass on the function call
256 if the low target has registered a corresponding function. */
259 win32_supports_z_point_type (char z_type
)
261 return (the_low_target
.supports_z_point_type
!= NULL
262 && the_low_target
.supports_z_point_type (z_type
));
266 win32_insert_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
267 int size
, struct raw_breakpoint
*bp
)
269 if (the_low_target
.insert_point
!= NULL
)
270 return the_low_target
.insert_point (type
, addr
, size
, bp
);
272 /* Unsupported (see target.h). */
277 win32_remove_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
278 int size
, struct raw_breakpoint
*bp
)
280 if (the_low_target
.remove_point
!= NULL
)
281 return the_low_target
.remove_point (type
, addr
, size
, bp
);
283 /* Unsupported (see target.h). */
288 win32_stopped_by_watchpoint (void)
290 if (the_low_target
.stopped_by_watchpoint
!= NULL
)
291 return the_low_target
.stopped_by_watchpoint ();
297 win32_stopped_data_address (void)
299 if (the_low_target
.stopped_data_address
!= NULL
)
300 return the_low_target
.stopped_data_address ();
306 /* Transfer memory from/to the debugged process. */
308 child_xfer_memory (CORE_ADDR memaddr
, char *our
, int len
,
309 int write
, struct target_ops
*target
)
314 uintptr_t addr
= (uintptr_t) memaddr
;
318 success
= WriteProcessMemory (current_process_handle
, (LPVOID
) addr
,
319 (LPCVOID
) our
, len
, &done
);
321 lasterror
= GetLastError ();
322 FlushInstructionCache (current_process_handle
, (LPCVOID
) addr
, len
);
326 success
= ReadProcessMemory (current_process_handle
, (LPCVOID
) addr
,
327 (LPVOID
) our
, len
, &done
);
329 lasterror
= GetLastError ();
331 if (!success
&& lasterror
== ERROR_PARTIAL_COPY
&& done
> 0)
334 return success
? done
: -1;
337 /* Clear out any old thread list and reinitialize it to a pristine
340 child_init_thread_list (void)
342 for_each_thread (delete_thread_info
);
345 /* Zero during the child initialization phase, and nonzero otherwise. */
347 static int child_initialization_done
= 0;
350 do_initial_child_stuff (HANDLE proch
, DWORD pid
, int attached
)
352 struct process_info
*proc
;
354 last_sig
= GDB_SIGNAL_0
;
356 current_process_handle
= proch
;
357 current_process_id
= pid
;
360 soft_interrupt_requested
= 0;
361 faked_breakpoint
= 0;
363 memset (¤t_event
, 0, sizeof (current_event
));
365 proc
= add_process (pid
, attached
);
366 proc
->tdesc
= win32_tdesc
;
367 child_init_thread_list ();
368 child_initialization_done
= 0;
370 if (the_low_target
.initial_stuff
!= NULL
)
371 (*the_low_target
.initial_stuff
) ();
373 cached_status
.kind
= TARGET_WAITKIND_IGNORE
;
375 /* Flush all currently pending debug events (thread and dll list) up
376 to the initial breakpoint. */
379 struct target_waitstatus status
;
381 win32_wait (minus_one_ptid
, &status
, 0);
383 /* Note win32_wait doesn't return thread events. */
384 if (status
.kind
!= TARGET_WAITKIND_LOADED
)
386 cached_status
= status
;
391 struct thread_resume resume
;
393 resume
.thread
= minus_one_ptid
;
394 resume
.kind
= resume_continue
;
397 win32_resume (&resume
, 1);
402 /* Now that the inferior has been started and all DLLs have been mapped,
403 we can iterate over all DLLs and load them in.
405 We avoid doing it any earlier because, on certain versions of Windows,
406 LOAD_DLL_DEBUG_EVENTs are sometimes not complete. In particular,
407 we have seen on Windows 8.1 that the ntdll.dll load event does not
408 include the DLL name, preventing us from creating an associated SO.
409 A possible explanation is that ntdll.dll might be mapped before
410 the SO info gets created by the Windows system -- ntdll.dll is
411 the first DLL to be reported via LOAD_DLL_DEBUG_EVENT and other DLLs
412 do not seem to suffer from that problem.
414 Rather than try to work around this sort of issue, it is much
415 simpler to just ignore DLL load/unload events during the startup
416 phase, and then process them all in one batch now. */
417 win32_add_all_dlls ();
420 child_initialization_done
= 1;
423 /* Resume all artificially suspended threads if we are continuing
426 continue_one_thread (thread_info
*thread
, int thread_id
)
428 win32_thread_info
*th
= (win32_thread_info
*) thread_target_data (thread
);
430 if (thread_id
== -1 || thread_id
== th
->tid
)
432 win32_prepare_to_resume (th
);
436 if (th
->context
.ContextFlags
)
438 win32_set_thread_context (th
);
439 th
->context
.ContextFlags
= 0;
442 if (ResumeThread (th
->h
) == (DWORD
) -1)
444 DWORD err
= GetLastError ();
445 OUTMSG (("warning: ResumeThread failed in continue_one_thread, "
446 "(error %d): %s\n", (int) err
, strwinerror (err
)));
454 child_continue (DWORD continue_status
, int thread_id
)
456 /* The inferior will only continue after the ContinueDebugEvent
458 for_each_thread ([&] (thread_info
*thread
)
460 continue_one_thread (thread
, thread_id
);
462 faked_breakpoint
= 0;
464 if (!ContinueDebugEvent (current_event
.dwProcessId
,
465 current_event
.dwThreadId
,
472 /* Fetch register(s) from the current thread context. */
474 child_fetch_inferior_registers (struct regcache
*regcache
, int r
)
477 win32_thread_info
*th
= thread_rec (current_thread_ptid (), TRUE
);
478 if (r
== -1 || r
> NUM_REGS
)
479 child_fetch_inferior_registers (regcache
, NUM_REGS
);
481 for (regno
= 0; regno
< r
; regno
++)
482 (*the_low_target
.fetch_inferior_register
) (regcache
, th
, regno
);
485 /* Store a new register value into the current thread context. We don't
486 change the program's context until later, when we resume it. */
488 child_store_inferior_registers (struct regcache
*regcache
, int r
)
491 win32_thread_info
*th
= thread_rec (current_thread_ptid (), TRUE
);
492 if (r
== -1 || r
== 0 || r
> NUM_REGS
)
493 child_store_inferior_registers (regcache
, NUM_REGS
);
495 for (regno
= 0; regno
< r
; regno
++)
496 (*the_low_target
.store_inferior_register
) (regcache
, th
, regno
);
499 /* Map the Windows error number in ERROR to a locale-dependent error
500 message string and return a pointer to it. Typically, the values
501 for ERROR come from GetLastError.
503 The string pointed to shall not be modified by the application,
504 but may be overwritten by a subsequent call to strwinerror
506 The strwinerror function does not change the current setting
510 strwinerror (DWORD error
)
512 static char buf
[1024];
514 DWORD lasterr
= GetLastError ();
515 DWORD chars
= FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
516 | FORMAT_MESSAGE_ALLOCATE_BUFFER
,
519 0, /* Default language */
525 /* If there is an \r\n appended, zap it. */
527 && msgbuf
[chars
- 2] == '\r'
528 && msgbuf
[chars
- 1] == '\n')
534 if (chars
> ((COUNTOF (buf
)) - 1))
536 chars
= COUNTOF (buf
) - 1;
541 wcstombs (buf
, msgbuf
, chars
+ 1);
543 strncpy (buf
, msgbuf
, chars
+ 1);
548 sprintf (buf
, "unknown win32 error (%u)", (unsigned) error
);
550 SetLastError (lasterr
);
555 create_process (const char *program
, char *args
,
556 DWORD flags
, PROCESS_INFORMATION
*pi
)
558 const char *inferior_cwd
= get_inferior_cwd ();
562 wchar_t *p
, *wprogram
, *wargs
, *wcwd
= NULL
;
565 wprogram
= alloca ((strlen (program
) + 1) * sizeof (wchar_t));
566 mbstowcs (wprogram
, program
, strlen (program
) + 1);
568 for (p
= wprogram
; *p
; ++p
)
572 argslen
= strlen (args
);
573 wargs
= alloca ((argslen
+ 1) * sizeof (wchar_t));
574 mbstowcs (wargs
, args
, argslen
+ 1);
576 if (inferior_cwd
!= NULL
)
578 std::string expanded_infcwd
= gdb_tilde_expand (inferior_cwd
);
579 std::replace (expanded_infcwd
.begin (), expanded_infcwd
.end (),
581 wcwd
= alloca ((expanded_infcwd
.size () + 1) * sizeof (wchar_t));
582 if (mbstowcs (wcwd
, expanded_infcwd
.c_str (),
583 expanded_infcwd
.size () + 1) == NULL
)
586 Could not convert the expanded inferior cwd to wide-char."));
590 ret
= CreateProcessW (wprogram
, /* image name */
591 wargs
, /* command line */
592 NULL
, /* security, not supported */
593 NULL
, /* thread, not supported */
594 FALSE
, /* inherit handles, not supported */
595 flags
, /* start flags */
596 NULL
, /* environment, not supported */
597 wcwd
, /* current directory */
598 NULL
, /* start info, not supported */
601 STARTUPINFOA si
= { sizeof (STARTUPINFOA
) };
603 ret
= CreateProcessA (program
, /* image name */
604 args
, /* command line */
607 TRUE
, /* inherit handles */
608 flags
, /* start flags */
609 NULL
, /* environment */
610 /* current directory */
611 (inferior_cwd
== NULL
613 : gdb_tilde_expand (inferior_cwd
).c_str()),
614 &si
, /* start info */
621 /* Start a new process.
622 PROGRAM is the program name.
623 PROGRAM_ARGS is the vector containing the inferior's args.
624 Returns the new PID on success, -1 on failure. Registers the new
625 process with the process list. */
627 win32_create_inferior (const char *program
,
628 const std::vector
<char *> &program_args
)
631 char real_path
[PATH_MAX
];
632 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 last_ptid
= win32_wait (pid_to_ptid (current_process_id
), &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 /* Kill all inferiors. */
811 struct process_info
*process
;
813 if (current_process_handle
== NULL
)
816 TerminateProcess (current_process_handle
, 0);
819 if (!child_continue (DBG_CONTINUE
, -1))
821 if (!WaitForDebugEvent (¤t_event
, INFINITE
))
823 if (current_event
.dwDebugEventCode
== EXIT_PROCESS_DEBUG_EVENT
)
825 else if (current_event
.dwDebugEventCode
== OUTPUT_DEBUG_STRING_EVENT
)
826 handle_output_debug_string ();
829 win32_clear_inferiors ();
831 process
= find_process_pid (pid
);
832 remove_process (process
);
836 /* Detach from inferior PID. */
838 win32_detach (int pid
)
840 struct process_info
*process
;
841 winapi_DebugActiveProcessStop DebugActiveProcessStop
= NULL
;
842 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
844 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
846 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
848 DebugActiveProcessStop
= GETPROCADDRESS (dll
, DebugActiveProcessStop
);
849 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
851 if (DebugSetProcessKillOnExit
== NULL
852 || DebugActiveProcessStop
== NULL
)
856 struct thread_resume resume
;
857 resume
.thread
= minus_one_ptid
;
858 resume
.kind
= resume_continue
;
860 win32_resume (&resume
, 1);
863 if (!DebugActiveProcessStop (current_process_id
))
866 DebugSetProcessKillOnExit (FALSE
);
867 process
= find_process_pid (pid
);
868 remove_process (process
);
870 win32_clear_inferiors ();
875 win32_mourn (struct process_info
*process
)
877 remove_process (process
);
880 /* Wait for inferiors to end. */
884 HANDLE h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
887 WaitForSingleObject (h
, INFINITE
);
892 /* Return 1 iff the thread with thread ID TID is alive. */
894 win32_thread_alive (ptid_t ptid
)
896 /* Our thread list is reliable; don't bother to poll target
898 return find_thread_ptid (ptid
) != NULL
;
901 /* Resume the inferior process. RESUME_INFO describes how we want
904 win32_resume (struct thread_resume
*resume_info
, size_t n
)
909 win32_thread_info
*th
;
910 DWORD continue_status
= DBG_CONTINUE
;
913 /* This handles the very limited set of resume packets that GDB can
914 currently produce. */
916 if (n
== 1 && ptid_equal (resume_info
[0].thread
, minus_one_ptid
))
921 /* Yes, we're ignoring resume_info[0].thread. It'd be tricky to make
922 the Windows resume code do the right thing for thread switching. */
923 tid
= current_event
.dwThreadId
;
925 if (!ptid_equal (resume_info
[0].thread
, minus_one_ptid
))
927 sig
= gdb_signal_from_host (resume_info
[0].sig
);
928 step
= resume_info
[0].kind
== resume_step
;
936 if (sig
!= GDB_SIGNAL_0
)
938 if (current_event
.dwDebugEventCode
!= EXCEPTION_DEBUG_EVENT
)
940 OUTMSG (("Cannot continue with signal %s here.\n",
941 gdb_signal_to_string (sig
)));
943 else if (sig
== last_sig
)
944 continue_status
= DBG_EXCEPTION_NOT_HANDLED
;
946 OUTMSG (("Can only continue with received signal %s.\n",
947 gdb_signal_to_string (last_sig
)));
950 last_sig
= GDB_SIGNAL_0
;
952 /* Get context for the currently selected thread. */
953 ptid
= debug_event_ptid (¤t_event
);
954 th
= thread_rec (ptid
, FALSE
);
957 win32_prepare_to_resume (th
);
959 if (th
->context
.ContextFlags
)
961 /* Move register values from the inferior into the thread
962 context structure. */
963 regcache_invalidate ();
967 if (the_low_target
.single_step
!= NULL
)
968 (*the_low_target
.single_step
) (th
);
970 error ("Single stepping is not supported "
971 "in this configuration.\n");
974 win32_set_thread_context (th
);
975 th
->context
.ContextFlags
= 0;
979 /* Allow continuing with the same signal that interrupted us.
980 Otherwise complain. */
982 child_continue (continue_status
, tid
);
986 win32_add_one_solib (const char *name
, CORE_ADDR load_addr
)
988 char buf
[MAX_PATH
+ 1];
989 char buf2
[MAX_PATH
+ 1];
992 WIN32_FIND_DATA w32_fd
;
993 WCHAR wname
[MAX_PATH
+ 1];
994 mbstowcs (wname
, name
, MAX_PATH
);
995 HANDLE h
= FindFirstFile (wname
, &w32_fd
);
997 WIN32_FIND_DATAA w32_fd
;
998 HANDLE h
= FindFirstFileA (name
, &w32_fd
);
1001 /* The symbols in a dll are offset by 0x1000, which is the
1002 offset from 0 of the first byte in an image - because
1003 of the file header and the section alignment. */
1004 load_addr
+= 0x1000;
1006 if (h
== INVALID_HANDLE_VALUE
)
1014 char cwd
[MAX_PATH
+ 1];
1016 if (GetCurrentDirectoryA (MAX_PATH
+ 1, cwd
))
1018 p
= strrchr (buf
, '\\');
1021 SetCurrentDirectoryA (buf
);
1022 GetFullPathNameA (w32_fd
.cFileName
, MAX_PATH
, buf
, &p
);
1023 SetCurrentDirectoryA (cwd
);
1030 if (strcasecmp (buf
, "ntdll.dll") == 0)
1032 GetSystemDirectoryA (buf
, sizeof (buf
));
1033 strcat (buf
, "\\ntdll.dll");
1038 cygwin_conv_path (CCP_WIN_A_TO_POSIX
, buf
, buf2
, sizeof (buf2
));
1043 loaded_dll (buf2
, load_addr
);
1047 get_image_name (HANDLE h
, void *address
, int unicode
)
1049 static char buf
[(2 * MAX_PATH
) + 1];
1050 DWORD size
= unicode
? sizeof (WCHAR
) : sizeof (char);
1056 /* Attempt to read the name of the dll that was detected.
1057 This is documented to work only when actively debugging
1058 a program. It will not work for attached processes. */
1059 if (address
== NULL
)
1063 /* Windows CE reports the address of the image name,
1064 instead of an address of a pointer into the image name. */
1065 address_ptr
= address
;
1067 /* See if we could read the address of a string, and that the
1068 address isn't null. */
1069 if (!ReadProcessMemory (h
, address
, &address_ptr
,
1070 sizeof (address_ptr
), &done
)
1071 || done
!= sizeof (address_ptr
)
1076 /* Find the length of the string */
1077 while (ReadProcessMemory (h
, address_ptr
+ len
++ * size
, &b
, size
, &done
)
1078 && (b
[0] != 0 || b
[size
- 1] != 0) && done
== size
)
1082 ReadProcessMemory (h
, address_ptr
, buf
, len
, &done
);
1085 WCHAR
*unicode_address
= XALLOCAVEC (WCHAR
, len
);
1086 ReadProcessMemory (h
, address_ptr
, unicode_address
, len
* sizeof (WCHAR
),
1089 WideCharToMultiByte (CP_ACP
, 0, unicode_address
, len
, buf
, len
, 0, 0);
1095 typedef BOOL (WINAPI
*winapi_EnumProcessModules
) (HANDLE
, HMODULE
*,
1097 typedef BOOL (WINAPI
*winapi_GetModuleInformation
) (HANDLE
, HMODULE
,
1098 LPMODULEINFO
, DWORD
);
1099 typedef DWORD (WINAPI
*winapi_GetModuleFileNameExA
) (HANDLE
, HMODULE
,
1102 static winapi_EnumProcessModules win32_EnumProcessModules
;
1103 static winapi_GetModuleInformation win32_GetModuleInformation
;
1104 static winapi_GetModuleFileNameExA win32_GetModuleFileNameExA
;
1109 static int psapi_loaded
= 0;
1110 static HMODULE dll
= NULL
;
1115 dll
= LoadLibrary (TEXT("psapi.dll"));
1118 win32_EnumProcessModules
=
1119 GETPROCADDRESS (dll
, EnumProcessModules
);
1120 win32_GetModuleInformation
=
1121 GETPROCADDRESS (dll
, GetModuleInformation
);
1122 win32_GetModuleFileNameExA
=
1123 GETPROCADDRESS (dll
, GetModuleFileNameExA
);
1126 return (win32_EnumProcessModules
!= NULL
1127 && win32_GetModuleInformation
!= NULL
1128 && win32_GetModuleFileNameExA
!= NULL
);
1133 /* Iterate over all DLLs currently mapped by our inferior, and
1134 add them to our list of solibs. */
1137 win32_add_all_dlls (void)
1141 HMODULE
*DllHandle
= dh_buf
;
1149 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1154 if (!ok
|| !cbNeeded
)
1157 DllHandle
= (HMODULE
*) alloca (cbNeeded
);
1161 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1168 for (i
= 1; i
< ((size_t) cbNeeded
/ sizeof (HMODULE
)); i
++)
1171 char dll_name
[MAX_PATH
];
1173 if (!(*win32_GetModuleInformation
) (current_process_handle
,
1178 if ((*win32_GetModuleFileNameExA
) (current_process_handle
,
1183 win32_add_one_solib (dll_name
, (CORE_ADDR
) (uintptr_t) mi
.lpBaseOfDll
);
1188 typedef HANDLE (WINAPI
*winapi_CreateToolhelp32Snapshot
) (DWORD
, DWORD
);
1189 typedef BOOL (WINAPI
*winapi_Module32First
) (HANDLE
, LPMODULEENTRY32
);
1190 typedef BOOL (WINAPI
*winapi_Module32Next
) (HANDLE
, LPMODULEENTRY32
);
1192 /* Handle a DLL load event.
1194 This function assumes that this event did not occur during inferior
1195 initialization, where their event info may be incomplete (see
1196 do_initial_child_stuff and win32_add_all_dlls for more info on
1197 how we handle DLL loading during that phase). */
1200 handle_load_dll (void)
1202 LOAD_DLL_DEBUG_INFO
*event
= ¤t_event
.u
.LoadDll
;
1205 dll_name
= get_image_name (current_process_handle
,
1206 event
->lpImageName
, event
->fUnicode
);
1210 win32_add_one_solib (dll_name
, (CORE_ADDR
) (uintptr_t) event
->lpBaseOfDll
);
1213 /* Handle a DLL unload event.
1215 This function assumes that this event did not occur during inferior
1216 initialization, where their event info may be incomplete (see
1217 do_initial_child_stuff and win32_add_one_solib for more info
1218 on how we handle DLL loading during that phase). */
1221 handle_unload_dll (void)
1223 CORE_ADDR load_addr
=
1224 (CORE_ADDR
) (uintptr_t) current_event
.u
.UnloadDll
.lpBaseOfDll
;
1226 /* The symbols in a dll are offset by 0x1000, which is the
1227 offset from 0 of the first byte in an image - because
1228 of the file header and the section alignment. */
1229 load_addr
+= 0x1000;
1230 unloaded_dll (NULL
, load_addr
);
1234 handle_exception (struct target_waitstatus
*ourstatus
)
1236 DWORD code
= current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
;
1238 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
1242 case EXCEPTION_ACCESS_VIOLATION
:
1243 OUTMSG2 (("EXCEPTION_ACCESS_VIOLATION"));
1244 ourstatus
->value
.sig
= GDB_SIGNAL_SEGV
;
1246 case STATUS_STACK_OVERFLOW
:
1247 OUTMSG2 (("STATUS_STACK_OVERFLOW"));
1248 ourstatus
->value
.sig
= GDB_SIGNAL_SEGV
;
1250 case STATUS_FLOAT_DENORMAL_OPERAND
:
1251 OUTMSG2 (("STATUS_FLOAT_DENORMAL_OPERAND"));
1252 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1254 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
1255 OUTMSG2 (("EXCEPTION_ARRAY_BOUNDS_EXCEEDED"));
1256 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1258 case STATUS_FLOAT_INEXACT_RESULT
:
1259 OUTMSG2 (("STATUS_FLOAT_INEXACT_RESULT"));
1260 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1262 case STATUS_FLOAT_INVALID_OPERATION
:
1263 OUTMSG2 (("STATUS_FLOAT_INVALID_OPERATION"));
1264 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1266 case STATUS_FLOAT_OVERFLOW
:
1267 OUTMSG2 (("STATUS_FLOAT_OVERFLOW"));
1268 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1270 case STATUS_FLOAT_STACK_CHECK
:
1271 OUTMSG2 (("STATUS_FLOAT_STACK_CHECK"));
1272 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1274 case STATUS_FLOAT_UNDERFLOW
:
1275 OUTMSG2 (("STATUS_FLOAT_UNDERFLOW"));
1276 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1278 case STATUS_FLOAT_DIVIDE_BY_ZERO
:
1279 OUTMSG2 (("STATUS_FLOAT_DIVIDE_BY_ZERO"));
1280 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1282 case STATUS_INTEGER_DIVIDE_BY_ZERO
:
1283 OUTMSG2 (("STATUS_INTEGER_DIVIDE_BY_ZERO"));
1284 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1286 case STATUS_INTEGER_OVERFLOW
:
1287 OUTMSG2 (("STATUS_INTEGER_OVERFLOW"));
1288 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1290 case EXCEPTION_BREAKPOINT
:
1291 OUTMSG2 (("EXCEPTION_BREAKPOINT"));
1292 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1294 /* Remove the initial breakpoint. */
1295 check_breakpoints ((CORE_ADDR
) (long) current_event
1296 .u
.Exception
.ExceptionRecord
.ExceptionAddress
);
1300 OUTMSG2 (("DBG_CONTROL_C"));
1301 ourstatus
->value
.sig
= GDB_SIGNAL_INT
;
1303 case DBG_CONTROL_BREAK
:
1304 OUTMSG2 (("DBG_CONTROL_BREAK"));
1305 ourstatus
->value
.sig
= GDB_SIGNAL_INT
;
1307 case EXCEPTION_SINGLE_STEP
:
1308 OUTMSG2 (("EXCEPTION_SINGLE_STEP"));
1309 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1311 case EXCEPTION_ILLEGAL_INSTRUCTION
:
1312 OUTMSG2 (("EXCEPTION_ILLEGAL_INSTRUCTION"));
1313 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1315 case EXCEPTION_PRIV_INSTRUCTION
:
1316 OUTMSG2 (("EXCEPTION_PRIV_INSTRUCTION"));
1317 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1319 case EXCEPTION_NONCONTINUABLE_EXCEPTION
:
1320 OUTMSG2 (("EXCEPTION_NONCONTINUABLE_EXCEPTION"));
1321 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1324 if (current_event
.u
.Exception
.dwFirstChance
)
1326 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1329 OUTMSG2 (("gdbserver: unknown target exception 0x%08x at 0x%s",
1330 (unsigned) current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
,
1331 phex_nz ((uintptr_t) current_event
.u
.Exception
.ExceptionRecord
.
1332 ExceptionAddress
, sizeof (uintptr_t))));
1333 ourstatus
->value
.sig
= GDB_SIGNAL_UNKNOWN
;
1337 last_sig
= ourstatus
->value
.sig
;
1342 suspend_one_thread (thread_info
*thread
)
1344 win32_thread_info
*th
= (win32_thread_info
*) thread_target_data (thread
);
1348 if (SuspendThread (th
->h
) == (DWORD
) -1)
1350 DWORD err
= GetLastError ();
1351 OUTMSG (("warning: SuspendThread failed in suspend_one_thread, "
1352 "(error %d): %s\n", (int) err
, strwinerror (err
)));
1360 fake_breakpoint_event (void)
1362 OUTMSG2(("fake_breakpoint_event\n"));
1364 faked_breakpoint
= 1;
1366 memset (¤t_event
, 0, sizeof (current_event
));
1367 current_event
.dwThreadId
= main_thread_id
;
1368 current_event
.dwDebugEventCode
= EXCEPTION_DEBUG_EVENT
;
1369 current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
1370 = EXCEPTION_BREAKPOINT
;
1372 for_each_thread (suspend_one_thread
);
1377 auto_delete_breakpoint (CORE_ADDR stop_pc
)
1383 /* Get the next event from the child. */
1386 get_child_debug_event (struct target_waitstatus
*ourstatus
)
1390 last_sig
= GDB_SIGNAL_0
;
1391 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1393 /* Check if GDB sent us an interrupt request. */
1394 check_remote_input_interrupt_request ();
1396 if (soft_interrupt_requested
)
1398 soft_interrupt_requested
= 0;
1399 fake_breakpoint_event ();
1408 /* WinCE doesn't set an initial breakpoint automatically. To
1409 stop the inferior, we flush all currently pending debug
1410 events -- the thread list and the dll list are always
1411 reported immediatelly without delay, then, we suspend all
1412 threads and pretend we saw a trap at the current PC of the
1415 Contrary to desktop Windows, Windows CE *does* report the dll
1416 names on LOAD_DLL_DEBUG_EVENTs resulting from a
1417 DebugActiveProcess call. This limits the way we can detect
1418 if all the dlls have already been reported. If we get a real
1419 debug event before leaving attaching, the worst that will
1420 happen is the user will see a spurious breakpoint. */
1422 current_event
.dwDebugEventCode
= 0;
1423 if (!WaitForDebugEvent (¤t_event
, 0))
1425 OUTMSG2(("no attach events left\n"));
1426 fake_breakpoint_event ();
1430 OUTMSG2(("got attach event\n"));
1435 /* Keep the wait time low enough for confortable remote
1436 interruption, but high enough so gdbserver doesn't become a
1438 if (!WaitForDebugEvent (¤t_event
, 250))
1440 DWORD e
= GetLastError();
1442 if (e
== ERROR_PIPE_NOT_CONNECTED
)
1444 /* This will happen if the loader fails to succesfully
1445 load the application, e.g., if the main executable
1446 tries to pull in a non-existing export from a
1448 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1449 ourstatus
->value
.integer
= 1;
1459 switch (current_event
.dwDebugEventCode
)
1461 case CREATE_THREAD_DEBUG_EVENT
:
1462 OUTMSG2 (("gdbserver: kernel event CREATE_THREAD_DEBUG_EVENT "
1463 "for pid=%u tid=%x)\n",
1464 (unsigned) current_event
.dwProcessId
,
1465 (unsigned) current_event
.dwThreadId
));
1467 /* Record the existence of this thread. */
1468 child_add_thread (current_event
.dwProcessId
,
1469 current_event
.dwThreadId
,
1470 current_event
.u
.CreateThread
.hThread
,
1471 current_event
.u
.CreateThread
.lpThreadLocalBase
);
1474 case EXIT_THREAD_DEBUG_EVENT
:
1475 OUTMSG2 (("gdbserver: kernel event EXIT_THREAD_DEBUG_EVENT "
1476 "for pid=%u tid=%x\n",
1477 (unsigned) current_event
.dwProcessId
,
1478 (unsigned) current_event
.dwThreadId
));
1479 child_delete_thread (current_event
.dwProcessId
,
1480 current_event
.dwThreadId
);
1482 current_thread
= get_first_thread ();
1485 case CREATE_PROCESS_DEBUG_EVENT
:
1486 OUTMSG2 (("gdbserver: kernel event CREATE_PROCESS_DEBUG_EVENT "
1487 "for pid=%u tid=%x\n",
1488 (unsigned) current_event
.dwProcessId
,
1489 (unsigned) current_event
.dwThreadId
));
1490 CloseHandle (current_event
.u
.CreateProcessInfo
.hFile
);
1492 current_process_handle
= current_event
.u
.CreateProcessInfo
.hProcess
;
1493 main_thread_id
= current_event
.dwThreadId
;
1495 /* Add the main thread. */
1496 child_add_thread (current_event
.dwProcessId
,
1498 current_event
.u
.CreateProcessInfo
.hThread
,
1499 current_event
.u
.CreateProcessInfo
.lpThreadLocalBase
);
1504 /* Windows CE doesn't set the initial breakpoint
1505 automatically like the desktop versions of Windows do.
1506 We add it explicitly here. It will be removed as soon as
1508 set_breakpoint_at ((CORE_ADDR
) (long) current_event
.u
1509 .CreateProcessInfo
.lpStartAddress
,
1510 auto_delete_breakpoint
);
1515 case EXIT_PROCESS_DEBUG_EVENT
:
1516 OUTMSG2 (("gdbserver: kernel event EXIT_PROCESS_DEBUG_EVENT "
1517 "for pid=%u tid=%x\n",
1518 (unsigned) current_event
.dwProcessId
,
1519 (unsigned) current_event
.dwThreadId
));
1520 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1521 ourstatus
->value
.integer
= current_event
.u
.ExitProcess
.dwExitCode
;
1522 child_continue (DBG_CONTINUE
, -1);
1523 CloseHandle (current_process_handle
);
1524 current_process_handle
= NULL
;
1527 case LOAD_DLL_DEBUG_EVENT
:
1528 OUTMSG2 (("gdbserver: kernel event LOAD_DLL_DEBUG_EVENT "
1529 "for pid=%u tid=%x\n",
1530 (unsigned) current_event
.dwProcessId
,
1531 (unsigned) current_event
.dwThreadId
));
1532 CloseHandle (current_event
.u
.LoadDll
.hFile
);
1533 if (! child_initialization_done
)
1537 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1538 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1541 case UNLOAD_DLL_DEBUG_EVENT
:
1542 OUTMSG2 (("gdbserver: kernel event UNLOAD_DLL_DEBUG_EVENT "
1543 "for pid=%u tid=%x\n",
1544 (unsigned) current_event
.dwProcessId
,
1545 (unsigned) current_event
.dwThreadId
));
1546 if (! child_initialization_done
)
1548 handle_unload_dll ();
1549 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1550 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1553 case EXCEPTION_DEBUG_EVENT
:
1554 OUTMSG2 (("gdbserver: kernel event EXCEPTION_DEBUG_EVENT "
1555 "for pid=%u tid=%x\n",
1556 (unsigned) current_event
.dwProcessId
,
1557 (unsigned) current_event
.dwThreadId
));
1558 handle_exception (ourstatus
);
1561 case OUTPUT_DEBUG_STRING_EVENT
:
1562 /* A message from the kernel (or Cygwin). */
1563 OUTMSG2 (("gdbserver: kernel event OUTPUT_DEBUG_STRING_EVENT "
1564 "for pid=%u tid=%x\n",
1565 (unsigned) current_event
.dwProcessId
,
1566 (unsigned) current_event
.dwThreadId
));
1567 handle_output_debug_string ();
1571 OUTMSG2 (("gdbserver: kernel event unknown "
1572 "for pid=%u tid=%x code=%x\n",
1573 (unsigned) current_event
.dwProcessId
,
1574 (unsigned) current_event
.dwThreadId
,
1575 (unsigned) current_event
.dwDebugEventCode
));
1579 ptid
= debug_event_ptid (¤t_event
);
1580 current_thread
= find_thread_ptid (ptid
);
1584 /* Wait for the inferior process to change state.
1585 STATUS will be filled in with a response code to send to GDB.
1586 Returns the signal which caused the process to stop. */
1588 win32_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
, int options
)
1590 struct regcache
*regcache
;
1592 if (cached_status
.kind
!= TARGET_WAITKIND_IGNORE
)
1594 /* The core always does a wait after creating the inferior, and
1595 do_initial_child_stuff already ran the inferior to the
1596 initial breakpoint (or an exit, if creating the process
1597 fails). Report it now. */
1598 *ourstatus
= cached_status
;
1599 cached_status
.kind
= TARGET_WAITKIND_IGNORE
;
1600 return debug_event_ptid (¤t_event
);
1605 if (!get_child_debug_event (ourstatus
))
1608 switch (ourstatus
->kind
)
1610 case TARGET_WAITKIND_EXITED
:
1611 OUTMSG2 (("Child exited with retcode = %x\n",
1612 ourstatus
->value
.integer
));
1613 win32_clear_inferiors ();
1614 return pid_to_ptid (current_event
.dwProcessId
);
1615 case TARGET_WAITKIND_STOPPED
:
1616 case TARGET_WAITKIND_LOADED
:
1617 OUTMSG2 (("Child Stopped with signal = %d \n",
1618 ourstatus
->value
.sig
));
1620 regcache
= get_thread_regcache (current_thread
, 1);
1621 child_fetch_inferior_registers (regcache
, -1);
1622 return debug_event_ptid (¤t_event
);
1624 OUTMSG (("Ignoring unknown internal event, %d\n", ourstatus
->kind
));
1626 case TARGET_WAITKIND_SPURIOUS
:
1627 /* do nothing, just continue */
1628 child_continue (DBG_CONTINUE
, -1);
1634 /* Fetch registers from the inferior process.
1635 If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO. */
1637 win32_fetch_inferior_registers (struct regcache
*regcache
, int regno
)
1639 child_fetch_inferior_registers (regcache
, regno
);
1642 /* Store registers to the inferior process.
1643 If REGNO is -1, store all registers; otherwise, store at least REGNO. */
1645 win32_store_inferior_registers (struct regcache
*regcache
, int regno
)
1647 child_store_inferior_registers (regcache
, regno
);
1650 /* Read memory from the inferior process. This should generally be
1651 called through read_inferior_memory, which handles breakpoint shadowing.
1652 Read LEN bytes at MEMADDR into a buffer at MYADDR. */
1654 win32_read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
1656 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 0, 0) != len
;
1659 /* Write memory to the inferior process. This should generally be
1660 called through write_inferior_memory, which handles breakpoint shadowing.
1661 Write LEN bytes from the buffer at MYADDR to MEMADDR.
1662 Returns 0 on success and errno on failure. */
1664 win32_write_inferior_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
,
1667 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 1, 0) != len
;
1670 /* Send an interrupt request to the inferior process. */
1672 win32_request_interrupt (void)
1674 winapi_DebugBreakProcess DebugBreakProcess
;
1675 winapi_GenerateConsoleCtrlEvent GenerateConsoleCtrlEvent
;
1678 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
1680 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
1683 GenerateConsoleCtrlEvent
= GETPROCADDRESS (dll
, GenerateConsoleCtrlEvent
);
1685 if (GenerateConsoleCtrlEvent
!= NULL
1686 && GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT
, current_process_id
))
1689 /* GenerateConsoleCtrlEvent can fail if process id being debugged is
1690 not a process group id.
1691 Fallback to XP/Vista 'DebugBreakProcess', which generates a
1692 breakpoint exception in the interior process. */
1694 DebugBreakProcess
= GETPROCADDRESS (dll
, DebugBreakProcess
);
1696 if (DebugBreakProcess
!= NULL
1697 && DebugBreakProcess (current_process_handle
))
1700 /* Last resort, suspend all threads manually. */
1701 soft_interrupt_requested
= 1;
1706 win32_error_to_fileio_error (DWORD err
)
1710 case ERROR_BAD_PATHNAME
:
1711 case ERROR_FILE_NOT_FOUND
:
1712 case ERROR_INVALID_NAME
:
1713 case ERROR_PATH_NOT_FOUND
:
1714 return FILEIO_ENOENT
;
1716 case ERROR_IO_DEVICE
:
1717 case ERROR_OPEN_FAILED
:
1719 case ERROR_INVALID_HANDLE
:
1720 return FILEIO_EBADF
;
1721 case ERROR_ACCESS_DENIED
:
1722 case ERROR_SHARING_VIOLATION
:
1723 return FILEIO_EACCES
;
1724 case ERROR_NOACCESS
:
1725 return FILEIO_EFAULT
;
1727 return FILEIO_EBUSY
;
1728 case ERROR_ALREADY_EXISTS
:
1729 case ERROR_FILE_EXISTS
:
1730 return FILEIO_EEXIST
;
1731 case ERROR_BAD_DEVICE
:
1732 return FILEIO_ENODEV
;
1733 case ERROR_DIRECTORY
:
1734 return FILEIO_ENOTDIR
;
1735 case ERROR_FILENAME_EXCED_RANGE
:
1736 case ERROR_INVALID_DATA
:
1737 case ERROR_INVALID_PARAMETER
:
1738 case ERROR_NEGATIVE_SEEK
:
1739 return FILEIO_EINVAL
;
1740 case ERROR_TOO_MANY_OPEN_FILES
:
1741 return FILEIO_EMFILE
;
1742 case ERROR_HANDLE_DISK_FULL
:
1743 case ERROR_DISK_FULL
:
1744 return FILEIO_ENOSPC
;
1745 case ERROR_WRITE_PROTECT
:
1746 return FILEIO_EROFS
;
1747 case ERROR_NOT_SUPPORTED
:
1748 return FILEIO_ENOSYS
;
1751 return FILEIO_EUNKNOWN
;
1755 wince_hostio_last_error (char *buf
)
1757 DWORD winerr
= GetLastError ();
1758 int fileio_err
= win32_error_to_fileio_error (winerr
);
1759 sprintf (buf
, "F-1,%x", fileio_err
);
1763 /* Write Windows OS Thread Information Block address. */
1766 win32_get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
)
1768 win32_thread_info
*th
;
1769 th
= thread_rec (ptid
, 0);
1773 *addr
= th
->thread_local_base
;
1777 /* Implementation of the target_ops method "sw_breakpoint_from_kind". */
1779 static const gdb_byte
*
1780 win32_sw_breakpoint_from_kind (int kind
, int *size
)
1782 *size
= the_low_target
.breakpoint_len
;
1783 return the_low_target
.breakpoint
;
1786 static struct target_ops win32_target_ops
= {
1787 win32_create_inferior
,
1788 NULL
, /* post_create_inferior */
1797 win32_fetch_inferior_registers
,
1798 win32_store_inferior_registers
,
1799 NULL
, /* prepare_to_access_memory */
1800 NULL
, /* done_accessing_memory */
1801 win32_read_inferior_memory
,
1802 win32_write_inferior_memory
,
1803 NULL
, /* lookup_symbols */
1804 win32_request_interrupt
,
1805 NULL
, /* read_auxv */
1806 win32_supports_z_point_type
,
1809 NULL
, /* stopped_by_sw_breakpoint */
1810 NULL
, /* supports_stopped_by_sw_breakpoint */
1811 NULL
, /* stopped_by_hw_breakpoint */
1812 NULL
, /* supports_stopped_by_hw_breakpoint */
1813 target_can_do_hardware_single_step
,
1814 win32_stopped_by_watchpoint
,
1815 win32_stopped_data_address
,
1816 NULL
, /* read_offsets */
1817 NULL
, /* get_tls_address */
1818 NULL
, /* qxfer_spu */
1820 wince_hostio_last_error
,
1822 hostio_last_error_from_errno
,
1824 NULL
, /* qxfer_osdata */
1825 NULL
, /* qxfer_siginfo */
1826 NULL
, /* supports_non_stop */
1828 NULL
, /* start_non_stop */
1829 NULL
, /* supports_multi_process */
1830 NULL
, /* supports_fork_events */
1831 NULL
, /* supports_vfork_events */
1832 NULL
, /* supports_exec_events */
1833 NULL
, /* handle_new_gdb_connection */
1834 NULL
, /* handle_monitor_command */
1835 NULL
, /* core_of_thread */
1836 NULL
, /* read_loadmap */
1837 NULL
, /* process_qsupported */
1838 NULL
, /* supports_tracepoints */
1840 NULL
, /* write_pc */
1841 NULL
, /* thread_stopped */
1842 win32_get_tib_address
,
1843 NULL
, /* pause_all */
1844 NULL
, /* unpause_all */
1845 NULL
, /* stabilize_threads */
1846 NULL
, /* install_fast_tracepoint_jump_pad */
1847 NULL
, /* emit_ops */
1848 NULL
, /* supports_disable_randomization */
1849 NULL
, /* get_min_fast_tracepoint_insn_len */
1850 NULL
, /* qxfer_libraries_svr4 */
1851 NULL
, /* support_agent */
1852 NULL
, /* enable_btrace */
1853 NULL
, /* disable_btrace */
1854 NULL
, /* read_btrace */
1855 NULL
, /* read_btrace_conf */
1856 NULL
, /* supports_range_stepping */
1857 NULL
, /* pid_to_exec_file */
1858 NULL
, /* multifs_open */
1859 NULL
, /* multifs_unlink */
1860 NULL
, /* multifs_readlink */
1861 NULL
, /* breakpoint_kind_from_pc */
1862 win32_sw_breakpoint_from_kind
,
1865 /* Initialize the Win32 backend. */
1867 initialize_low (void)
1869 set_target_ops (&win32_target_ops
);
1870 the_low_target
.arch_setup ();