1 /* Low level interface to Windows debugging, for gdbserver.
2 Copyright (C) 2006-2015 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"
37 #include <sys/cygwin.h>
40 #define OUTMSG(X) do { printf X; fflush (stderr); } while (0)
53 #define _T(x) TEXT (x)
57 #define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0]))
61 # define GETPROCADDRESS(DLL, PROC) \
62 ((winapi_ ## PROC) GetProcAddress (DLL, TEXT (#PROC)))
64 # define GETPROCADDRESS(DLL, PROC) \
65 ((winapi_ ## PROC) GetProcAddress (DLL, #PROC))
68 int using_threads
= 1;
71 static int attaching
= 0;
72 static HANDLE current_process_handle
= NULL
;
73 static DWORD current_process_id
= 0;
74 static DWORD main_thread_id
= 0;
75 static enum gdb_signal last_sig
= GDB_SIGNAL_0
;
77 /* The current debug event from WaitForDebugEvent. */
78 static DEBUG_EVENT current_event
;
80 /* A status that hasn't been reported to the core yet, and so
81 win32_wait should return it next, instead of fetching the next
82 debug event off the win32 API. */
83 static struct target_waitstatus cached_status
;
85 /* Non zero if an interrupt request is to be satisfied by suspending
87 static int soft_interrupt_requested
= 0;
89 /* Non zero if the inferior is stopped in a simulated breakpoint done
90 by suspending all the threads. */
91 static int faked_breakpoint
= 0;
93 const struct target_desc
*win32_tdesc
;
95 #define NUM_REGS (the_low_target.num_regs)
97 typedef BOOL (WINAPI
*winapi_DebugActiveProcessStop
) (DWORD dwProcessId
);
98 typedef BOOL (WINAPI
*winapi_DebugSetProcessKillOnExit
) (BOOL KillOnExit
);
99 typedef BOOL (WINAPI
*winapi_DebugBreakProcess
) (HANDLE
);
100 typedef BOOL (WINAPI
*winapi_GenerateConsoleCtrlEvent
) (DWORD
, DWORD
);
102 static ptid_t
win32_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
,
104 static void win32_resume (struct thread_resume
*resume_info
, size_t n
);
106 static void win32_add_all_dlls (void);
109 /* Get the thread ID from the current selected inferior (the current
112 current_thread_ptid (void)
117 /* The current debug event from WaitForDebugEvent. */
119 debug_event_ptid (DEBUG_EVENT
*event
)
121 return ptid_build (event
->dwProcessId
, event
->dwThreadId
, 0);
124 /* Get the thread context of the thread associated with TH. */
127 win32_get_thread_context (win32_thread_info
*th
)
129 memset (&th
->context
, 0, sizeof (CONTEXT
));
130 (*the_low_target
.get_thread_context
) (th
);
132 memcpy (&th
->base_context
, &th
->context
, sizeof (CONTEXT
));
136 /* Set the thread context of the thread associated with TH. */
139 win32_set_thread_context (win32_thread_info
*th
)
142 /* Calling SuspendThread on a thread that is running kernel code
143 will report that the suspending was successful, but in fact, that
144 will often not be true. In those cases, the context returned by
145 GetThreadContext will not be correct by the time the thread
146 stops, hence we can't set that context back into the thread when
147 resuming - it will most likelly crash the inferior.
148 Unfortunately, there is no way to know when the thread will
149 really stop. To work around it, we'll only write the context
150 back to the thread when either the user or GDB explicitly change
151 it between stopping and resuming. */
152 if (memcmp (&th
->context
, &th
->base_context
, sizeof (CONTEXT
)) != 0)
154 SetThreadContext (th
->h
, &th
->context
);
157 /* Set the thread context of the thread associated with TH. */
160 win32_prepare_to_resume (win32_thread_info
*th
)
162 if (the_low_target
.prepare_to_resume
!= NULL
)
163 (*the_low_target
.prepare_to_resume
) (th
);
166 /* See win32-low.h. */
169 win32_require_context (win32_thread_info
*th
)
171 if (th
->context
.ContextFlags
== 0)
175 if (SuspendThread (th
->h
) == (DWORD
) -1)
177 DWORD err
= GetLastError ();
178 OUTMSG (("warning: SuspendThread failed in thread_rec, "
179 "(error %d): %s\n", (int) err
, strwinerror (err
)));
185 win32_get_thread_context (th
);
189 /* Find a thread record given a thread id. If GET_CONTEXT is set then
190 also retrieve the context for this thread. */
191 static win32_thread_info
*
192 thread_rec (ptid_t ptid
, int get_context
)
194 struct thread_info
*thread
;
195 win32_thread_info
*th
;
197 thread
= (struct thread_info
*) find_inferior_id (&all_threads
, ptid
);
201 th
= (win32_thread_info
*) inferior_target_data (thread
);
203 win32_require_context (th
);
207 /* Add a thread to the thread list. */
208 static win32_thread_info
*
209 child_add_thread (DWORD pid
, DWORD tid
, HANDLE h
, void *tlb
)
211 win32_thread_info
*th
;
212 ptid_t ptid
= ptid_build (pid
, tid
, 0);
214 if ((th
= thread_rec (ptid
, FALSE
)))
217 th
= XCNEW (win32_thread_info
);
220 th
->thread_local_base
= (CORE_ADDR
) (uintptr_t) tlb
;
222 add_thread (ptid
, th
);
224 if (the_low_target
.thread_added
!= NULL
)
225 (*the_low_target
.thread_added
) (th
);
230 /* Delete a thread from the list of threads. */
232 delete_thread_info (struct inferior_list_entry
*entry
)
234 struct thread_info
*thread
= (struct thread_info
*) entry
;
235 win32_thread_info
*th
= (win32_thread_info
*) inferior_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 struct inferior_list_entry
*thread
;
249 /* If the last thread is exiting, just return. */
250 if (one_inferior_p (&all_threads
))
253 ptid
= ptid_build (pid
, tid
, 0);
254 thread
= find_inferior_id (&all_threads
, ptid
);
258 delete_thread_info (thread
);
261 /* These watchpoint related wrapper functions simply pass on the function call
262 if the low target has registered a corresponding function. */
265 win32_supports_z_point_type (char z_type
)
267 return (the_low_target
.supports_z_point_type
!= NULL
268 && the_low_target
.supports_z_point_type (z_type
));
272 win32_insert_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
273 int size
, struct raw_breakpoint
*bp
)
275 if (the_low_target
.insert_point
!= NULL
)
276 return the_low_target
.insert_point (type
, addr
, size
, bp
);
278 /* Unsupported (see target.h). */
283 win32_remove_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
284 int size
, struct raw_breakpoint
*bp
)
286 if (the_low_target
.remove_point
!= NULL
)
287 return the_low_target
.remove_point (type
, addr
, size
, bp
);
289 /* Unsupported (see target.h). */
294 win32_stopped_by_watchpoint (void)
296 if (the_low_target
.stopped_by_watchpoint
!= NULL
)
297 return the_low_target
.stopped_by_watchpoint ();
303 win32_stopped_data_address (void)
305 if (the_low_target
.stopped_data_address
!= NULL
)
306 return the_low_target
.stopped_data_address ();
312 /* Transfer memory from/to the debugged process. */
314 child_xfer_memory (CORE_ADDR memaddr
, char *our
, int len
,
315 int write
, struct target_ops
*target
)
320 uintptr_t addr
= (uintptr_t) memaddr
;
324 success
= WriteProcessMemory (current_process_handle
, (LPVOID
) addr
,
325 (LPCVOID
) our
, len
, &done
);
327 lasterror
= GetLastError ();
328 FlushInstructionCache (current_process_handle
, (LPCVOID
) addr
, len
);
332 success
= ReadProcessMemory (current_process_handle
, (LPCVOID
) addr
,
333 (LPVOID
) our
, len
, &done
);
335 lasterror
= GetLastError ();
337 if (!success
&& lasterror
== ERROR_PARTIAL_COPY
&& done
> 0)
340 return success
? done
: -1;
343 /* Clear out any old thread list and reinitialize it to a pristine
346 child_init_thread_list (void)
348 for_each_inferior (&all_threads
, delete_thread_info
);
351 /* Zero during the child initialization phase, and nonzero otherwise. */
353 static int child_initialization_done
= 0;
356 do_initial_child_stuff (HANDLE proch
, DWORD pid
, int attached
)
358 struct process_info
*proc
;
360 last_sig
= GDB_SIGNAL_0
;
362 current_process_handle
= proch
;
363 current_process_id
= pid
;
366 soft_interrupt_requested
= 0;
367 faked_breakpoint
= 0;
369 memset (¤t_event
, 0, sizeof (current_event
));
371 proc
= add_process (pid
, attached
);
372 proc
->tdesc
= win32_tdesc
;
373 child_init_thread_list ();
374 child_initialization_done
= 0;
376 if (the_low_target
.initial_stuff
!= NULL
)
377 (*the_low_target
.initial_stuff
) ();
379 cached_status
.kind
= TARGET_WAITKIND_IGNORE
;
381 /* Flush all currently pending debug events (thread and dll list) up
382 to the initial breakpoint. */
385 struct target_waitstatus status
;
387 win32_wait (minus_one_ptid
, &status
, 0);
389 /* Note win32_wait doesn't return thread events. */
390 if (status
.kind
!= TARGET_WAITKIND_LOADED
)
392 cached_status
= status
;
397 struct thread_resume resume
;
399 resume
.thread
= minus_one_ptid
;
400 resume
.kind
= resume_continue
;
403 win32_resume (&resume
, 1);
408 /* Now that the inferior has been started and all DLLs have been mapped,
409 we can iterate over all DLLs and load them in.
411 We avoid doing it any earlier because, on certain versions of Windows,
412 LOAD_DLL_DEBUG_EVENTs are sometimes not complete. In particular,
413 we have seen on Windows 8.1 that the ntdll.dll load event does not
414 include the DLL name, preventing us from creating an associated SO.
415 A possible explanation is that ntdll.dll might be mapped before
416 the SO info gets created by the Windows system -- ntdll.dll is
417 the first DLL to be reported via LOAD_DLL_DEBUG_EVENT and other DLLs
418 do not seem to suffer from that problem.
420 Rather than try to work around this sort of issue, it is much
421 simpler to just ignore DLL load/unload events during the startup
422 phase, and then process them all in one batch now. */
423 win32_add_all_dlls ();
426 child_initialization_done
= 1;
429 /* Resume all artificially suspended threads if we are continuing
432 continue_one_thread (struct inferior_list_entry
*this_thread
, void *id_ptr
)
434 struct thread_info
*thread
= (struct thread_info
*) this_thread
;
435 int thread_id
= * (int *) id_ptr
;
436 win32_thread_info
*th
= (win32_thread_info
*) inferior_target_data (thread
);
438 if (thread_id
== -1 || thread_id
== th
->tid
)
440 win32_prepare_to_resume (th
);
444 if (th
->context
.ContextFlags
)
446 win32_set_thread_context (th
);
447 th
->context
.ContextFlags
= 0;
450 if (ResumeThread (th
->h
) == (DWORD
) -1)
452 DWORD err
= GetLastError ();
453 OUTMSG (("warning: ResumeThread failed in continue_one_thread, "
454 "(error %d): %s\n", (int) err
, strwinerror (err
)));
464 child_continue (DWORD continue_status
, int thread_id
)
466 /* The inferior will only continue after the ContinueDebugEvent
468 find_inferior (&all_threads
, continue_one_thread
, &thread_id
);
469 faked_breakpoint
= 0;
471 if (!ContinueDebugEvent (current_event
.dwProcessId
,
472 current_event
.dwThreadId
,
479 /* Fetch register(s) from the current thread context. */
481 child_fetch_inferior_registers (struct regcache
*regcache
, int r
)
484 win32_thread_info
*th
= thread_rec (current_thread_ptid (), TRUE
);
485 if (r
== -1 || r
> NUM_REGS
)
486 child_fetch_inferior_registers (regcache
, NUM_REGS
);
488 for (regno
= 0; regno
< r
; regno
++)
489 (*the_low_target
.fetch_inferior_register
) (regcache
, th
, regno
);
492 /* Store a new register value into the current thread context. We don't
493 change the program's context until later, when we resume it. */
495 child_store_inferior_registers (struct regcache
*regcache
, int r
)
498 win32_thread_info
*th
= thread_rec (current_thread_ptid (), TRUE
);
499 if (r
== -1 || r
== 0 || r
> NUM_REGS
)
500 child_store_inferior_registers (regcache
, NUM_REGS
);
502 for (regno
= 0; regno
< r
; regno
++)
503 (*the_low_target
.store_inferior_register
) (regcache
, th
, regno
);
506 /* Map the Windows error number in ERROR to a locale-dependent error
507 message string and return a pointer to it. Typically, the values
508 for ERROR come from GetLastError.
510 The string pointed to shall not be modified by the application,
511 but may be overwritten by a subsequent call to strwinerror
513 The strwinerror function does not change the current setting
517 strwinerror (DWORD error
)
519 static char buf
[1024];
521 DWORD lasterr
= GetLastError ();
522 DWORD chars
= FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
523 | FORMAT_MESSAGE_ALLOCATE_BUFFER
,
526 0, /* Default language */
532 /* If there is an \r\n appended, zap it. */
534 && msgbuf
[chars
- 2] == '\r'
535 && msgbuf
[chars
- 1] == '\n')
541 if (chars
> ((COUNTOF (buf
)) - 1))
543 chars
= COUNTOF (buf
) - 1;
548 wcstombs (buf
, msgbuf
, chars
+ 1);
550 strncpy (buf
, msgbuf
, chars
+ 1);
555 sprintf (buf
, "unknown win32 error (%u)", (unsigned) error
);
557 SetLastError (lasterr
);
562 create_process (const char *program
, char *args
,
563 DWORD flags
, PROCESS_INFORMATION
*pi
)
568 wchar_t *p
, *wprogram
, *wargs
;
571 wprogram
= alloca ((strlen (program
) + 1) * sizeof (wchar_t));
572 mbstowcs (wprogram
, program
, strlen (program
) + 1);
574 for (p
= wprogram
; *p
; ++p
)
578 argslen
= strlen (args
);
579 wargs
= alloca ((argslen
+ 1) * sizeof (wchar_t));
580 mbstowcs (wargs
, args
, argslen
+ 1);
582 ret
= CreateProcessW (wprogram
, /* image name */
583 wargs
, /* command line */
584 NULL
, /* security, not supported */
585 NULL
, /* thread, not supported */
586 FALSE
, /* inherit handles, not supported */
587 flags
, /* start flags */
588 NULL
, /* environment, not supported */
589 NULL
, /* current directory, not supported */
590 NULL
, /* start info, not supported */
593 STARTUPINFOA si
= { sizeof (STARTUPINFOA
) };
595 ret
= CreateProcessA (program
, /* image name */
596 args
, /* command line */
599 TRUE
, /* inherit handles */
600 flags
, /* start flags */
601 NULL
, /* environment */
602 NULL
, /* current directory */
603 &si
, /* start info */
610 /* Start a new process.
611 PROGRAM is a path to the program to execute.
612 ARGS is a standard NULL-terminated array of arguments,
613 to be passed to the inferior as ``argv''.
614 Returns the new PID on success, -1 on failure. Registers the new
615 process with the process list. */
617 win32_create_inferior (char *program
, char **program_args
)
620 char real_path
[PATH_MAX
];
621 char *orig_path
, *new_path
, *path_ptr
;
628 PROCESS_INFORMATION pi
;
631 /* win32_wait needs to know we're not attaching. */
635 error ("No executable specified, specify executable to debug.\n");
637 flags
= DEBUG_PROCESS
| DEBUG_ONLY_THIS_PROCESS
;
641 path_ptr
= getenv ("PATH");
644 int size
= cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, NULL
, 0);
645 orig_path
= alloca (strlen (path_ptr
) + 1);
646 new_path
= alloca (size
);
647 strcpy (orig_path
, path_ptr
);
648 cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, new_path
, size
);
649 setenv ("PATH", new_path
, 1);
651 cygwin_conv_path (CCP_POSIX_TO_WIN_A
, program
, real_path
, PATH_MAX
);
656 for (argc
= 1; program_args
[argc
]; argc
++)
657 argslen
+= strlen (program_args
[argc
]) + 1;
658 args
= (char *) alloca (argslen
);
660 for (argc
= 1; program_args
[argc
]; argc
++)
662 /* FIXME: Can we do better about quoting? How does Cygwin
665 strcat (args
, program_args
[argc
]);
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 return current_process_id
;
710 /* Attach to a running process.
711 PID is the process ID to attach to, specified by the user
712 or a higher layer. */
714 win32_attach (unsigned long pid
)
717 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
720 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
722 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
724 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
726 h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
729 if (DebugActiveProcess (pid
))
731 if (DebugSetProcessKillOnExit
!= NULL
)
732 DebugSetProcessKillOnExit (FALSE
);
734 /* win32_wait needs to know we're attaching. */
736 do_initial_child_stuff (h
, pid
, 1);
743 err
= GetLastError ();
744 error ("Attach to process failed (error %d): %s\n",
745 (int) err
, strwinerror (err
));
748 /* Handle OUTPUT_DEBUG_STRING_EVENT from child process. */
750 handle_output_debug_string (struct target_waitstatus
*ourstatus
)
752 #define READ_BUFFER_LEN 1024
754 char s
[READ_BUFFER_LEN
+ 1] = { 0 };
755 DWORD nbytes
= current_event
.u
.DebugString
.nDebugStringLength
;
760 if (nbytes
> READ_BUFFER_LEN
)
761 nbytes
= READ_BUFFER_LEN
;
763 addr
= (CORE_ADDR
) (size_t) current_event
.u
.DebugString
.lpDebugStringData
;
765 if (current_event
.u
.DebugString
.fUnicode
)
767 /* The event tells us how many bytes, not chars, even
769 WCHAR buffer
[(READ_BUFFER_LEN
+ 1) / sizeof (WCHAR
)] = { 0 };
770 if (read_inferior_memory (addr
, (unsigned char *) buffer
, nbytes
) != 0)
772 wcstombs (s
, buffer
, (nbytes
+ 1) / sizeof (WCHAR
));
776 if (read_inferior_memory (addr
, (unsigned char *) s
, nbytes
) != 0)
780 if (!startswith (s
, "cYg"))
790 #undef READ_BUFFER_LEN
794 win32_clear_inferiors (void)
796 if (current_process_handle
!= NULL
)
797 CloseHandle (current_process_handle
);
799 for_each_inferior (&all_threads
, delete_thread_info
);
803 /* Kill all inferiors. */
807 struct process_info
*process
;
809 if (current_process_handle
== NULL
)
812 TerminateProcess (current_process_handle
, 0);
815 if (!child_continue (DBG_CONTINUE
, -1))
817 if (!WaitForDebugEvent (¤t_event
, INFINITE
))
819 if (current_event
.dwDebugEventCode
== EXIT_PROCESS_DEBUG_EVENT
)
821 else if (current_event
.dwDebugEventCode
== OUTPUT_DEBUG_STRING_EVENT
)
823 struct target_waitstatus our_status
= { 0 };
824 handle_output_debug_string (&our_status
);
828 win32_clear_inferiors ();
830 process
= find_process_pid (pid
);
831 remove_process (process
);
835 /* Detach from inferior PID. */
837 win32_detach (int pid
)
839 struct process_info
*process
;
840 winapi_DebugActiveProcessStop DebugActiveProcessStop
= NULL
;
841 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
843 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
845 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
847 DebugActiveProcessStop
= GETPROCADDRESS (dll
, DebugActiveProcessStop
);
848 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
850 if (DebugSetProcessKillOnExit
== NULL
851 || DebugActiveProcessStop
== NULL
)
855 struct thread_resume resume
;
856 resume
.thread
= minus_one_ptid
;
857 resume
.kind
= resume_continue
;
859 win32_resume (&resume
, 1);
862 if (!DebugActiveProcessStop (current_process_id
))
865 DebugSetProcessKillOnExit (FALSE
);
866 process
= find_process_pid (pid
);
867 remove_process (process
);
869 win32_clear_inferiors ();
874 win32_mourn (struct process_info
*process
)
876 remove_process (process
);
879 /* Wait for inferiors to end. */
883 HANDLE h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
886 WaitForSingleObject (h
, INFINITE
);
891 /* Return 1 iff the thread with thread ID TID is alive. */
893 win32_thread_alive (ptid_t ptid
)
897 /* Our thread list is reliable; don't bother to poll target
899 if (find_inferior_id (&all_threads
, ptid
) != NULL
)
906 /* Resume the inferior process. RESUME_INFO describes how we want
909 win32_resume (struct thread_resume
*resume_info
, size_t n
)
914 win32_thread_info
*th
;
915 DWORD continue_status
= DBG_CONTINUE
;
918 /* This handles the very limited set of resume packets that GDB can
919 currently produce. */
921 if (n
== 1 && ptid_equal (resume_info
[0].thread
, minus_one_ptid
))
926 /* Yes, we're ignoring resume_info[0].thread. It'd be tricky to make
927 the Windows resume code do the right thing for thread switching. */
928 tid
= current_event
.dwThreadId
;
930 if (!ptid_equal (resume_info
[0].thread
, minus_one_ptid
))
932 sig
= resume_info
[0].sig
;
933 step
= resume_info
[0].kind
== resume_step
;
941 if (sig
!= GDB_SIGNAL_0
)
943 if (current_event
.dwDebugEventCode
!= EXCEPTION_DEBUG_EVENT
)
945 OUTMSG (("Cannot continue with signal %d here.\n", sig
));
947 else if (sig
== last_sig
)
948 continue_status
= DBG_EXCEPTION_NOT_HANDLED
;
950 OUTMSG (("Can only continue with recieved signal %d.\n", last_sig
));
953 last_sig
= GDB_SIGNAL_0
;
955 /* Get context for the currently selected thread. */
956 ptid
= debug_event_ptid (¤t_event
);
957 th
= thread_rec (ptid
, FALSE
);
960 win32_prepare_to_resume (th
);
962 if (th
->context
.ContextFlags
)
964 /* Move register values from the inferior into the thread
965 context structure. */
966 regcache_invalidate ();
970 if (the_low_target
.single_step
!= NULL
)
971 (*the_low_target
.single_step
) (th
);
973 error ("Single stepping is not supported "
974 "in this configuration.\n");
977 win32_set_thread_context (th
);
978 th
->context
.ContextFlags
= 0;
982 /* Allow continuing with the same signal that interrupted us.
983 Otherwise complain. */
985 child_continue (continue_status
, tid
);
989 win32_add_one_solib (const char *name
, CORE_ADDR load_addr
)
991 char buf
[MAX_PATH
+ 1];
992 char buf2
[MAX_PATH
+ 1];
995 WIN32_FIND_DATA w32_fd
;
996 WCHAR wname
[MAX_PATH
+ 1];
997 mbstowcs (wname
, name
, MAX_PATH
);
998 HANDLE h
= FindFirstFile (wname
, &w32_fd
);
1000 WIN32_FIND_DATAA w32_fd
;
1001 HANDLE h
= FindFirstFileA (name
, &w32_fd
);
1004 /* The symbols in a dll are offset by 0x1000, which is the
1005 offset from 0 of the first byte in an image - because
1006 of the file header and the section alignment. */
1007 load_addr
+= 0x1000;
1009 if (h
== INVALID_HANDLE_VALUE
)
1017 char cwd
[MAX_PATH
+ 1];
1019 if (GetCurrentDirectoryA (MAX_PATH
+ 1, cwd
))
1021 p
= strrchr (buf
, '\\');
1024 SetCurrentDirectoryA (buf
);
1025 GetFullPathNameA (w32_fd
.cFileName
, MAX_PATH
, buf
, &p
);
1026 SetCurrentDirectoryA (cwd
);
1033 if (strcasecmp (buf
, "ntdll.dll") == 0)
1035 GetSystemDirectoryA (buf
, sizeof (buf
));
1036 strcat (buf
, "\\ntdll.dll");
1041 cygwin_conv_path (CCP_WIN_A_TO_POSIX
, buf
, buf2
, sizeof (buf2
));
1046 loaded_dll (buf2
, load_addr
);
1050 get_image_name (HANDLE h
, void *address
, int unicode
)
1052 static char buf
[(2 * MAX_PATH
) + 1];
1053 DWORD size
= unicode
? sizeof (WCHAR
) : sizeof (char);
1059 /* Attempt to read the name of the dll that was detected.
1060 This is documented to work only when actively debugging
1061 a program. It will not work for attached processes. */
1062 if (address
== NULL
)
1066 /* Windows CE reports the address of the image name,
1067 instead of an address of a pointer into the image name. */
1068 address_ptr
= address
;
1070 /* See if we could read the address of a string, and that the
1071 address isn't null. */
1072 if (!ReadProcessMemory (h
, address
, &address_ptr
,
1073 sizeof (address_ptr
), &done
)
1074 || done
!= sizeof (address_ptr
)
1079 /* Find the length of the string */
1080 while (ReadProcessMemory (h
, address_ptr
+ len
++ * size
, &b
, size
, &done
)
1081 && (b
[0] != 0 || b
[size
- 1] != 0) && done
== size
)
1085 ReadProcessMemory (h
, address_ptr
, buf
, len
, &done
);
1088 WCHAR
*unicode_address
= XALLOCAVEC (WCHAR
, len
);
1089 ReadProcessMemory (h
, address_ptr
, unicode_address
, len
* sizeof (WCHAR
),
1092 WideCharToMultiByte (CP_ACP
, 0, unicode_address
, len
, buf
, len
, 0, 0);
1098 typedef BOOL (WINAPI
*winapi_EnumProcessModules
) (HANDLE
, HMODULE
*,
1100 typedef BOOL (WINAPI
*winapi_GetModuleInformation
) (HANDLE
, HMODULE
,
1101 LPMODULEINFO
, DWORD
);
1102 typedef DWORD (WINAPI
*winapi_GetModuleFileNameExA
) (HANDLE
, HMODULE
,
1105 static winapi_EnumProcessModules win32_EnumProcessModules
;
1106 static winapi_GetModuleInformation win32_GetModuleInformation
;
1107 static winapi_GetModuleFileNameExA win32_GetModuleFileNameExA
;
1112 static int psapi_loaded
= 0;
1113 static HMODULE dll
= NULL
;
1118 dll
= LoadLibrary (TEXT("psapi.dll"));
1121 win32_EnumProcessModules
=
1122 GETPROCADDRESS (dll
, EnumProcessModules
);
1123 win32_GetModuleInformation
=
1124 GETPROCADDRESS (dll
, GetModuleInformation
);
1125 win32_GetModuleFileNameExA
=
1126 GETPROCADDRESS (dll
, GetModuleFileNameExA
);
1129 return (win32_EnumProcessModules
!= NULL
1130 && win32_GetModuleInformation
!= NULL
1131 && win32_GetModuleFileNameExA
!= NULL
);
1136 /* Iterate over all DLLs currently mapped by our inferior, and
1137 add them to our list of solibs. */
1140 win32_add_all_dlls (void)
1144 HMODULE
*DllHandle
= dh_buf
;
1152 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1157 if (!ok
|| !cbNeeded
)
1160 DllHandle
= (HMODULE
*) alloca (cbNeeded
);
1164 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1171 for (i
= 1; i
< ((size_t) cbNeeded
/ sizeof (HMODULE
)); i
++)
1174 char dll_name
[MAX_PATH
];
1176 if (!(*win32_GetModuleInformation
) (current_process_handle
,
1181 if ((*win32_GetModuleFileNameExA
) (current_process_handle
,
1186 win32_add_one_solib (dll_name
, (CORE_ADDR
) (uintptr_t) mi
.lpBaseOfDll
);
1191 typedef HANDLE (WINAPI
*winapi_CreateToolhelp32Snapshot
) (DWORD
, DWORD
);
1192 typedef BOOL (WINAPI
*winapi_Module32First
) (HANDLE
, LPMODULEENTRY32
);
1193 typedef BOOL (WINAPI
*winapi_Module32Next
) (HANDLE
, LPMODULEENTRY32
);
1195 /* Handle a DLL load event.
1197 This function assumes that this event did not occur during inferior
1198 initialization, where their event info may be incomplete (see
1199 do_initial_child_stuff and win32_add_all_dlls for more info on
1200 how we handle DLL loading during that phase). */
1203 handle_load_dll (void)
1205 LOAD_DLL_DEBUG_INFO
*event
= ¤t_event
.u
.LoadDll
;
1208 dll_name
= get_image_name (current_process_handle
,
1209 event
->lpImageName
, event
->fUnicode
);
1213 win32_add_one_solib (dll_name
, (CORE_ADDR
) (uintptr_t) event
->lpBaseOfDll
);
1216 /* Handle a DLL unload event.
1218 This function assumes that this event did not occur during inferior
1219 initialization, where their event info may be incomplete (see
1220 do_initial_child_stuff and win32_add_one_solib for more info
1221 on how we handle DLL loading during that phase). */
1224 handle_unload_dll (void)
1226 CORE_ADDR load_addr
=
1227 (CORE_ADDR
) (uintptr_t) current_event
.u
.UnloadDll
.lpBaseOfDll
;
1229 /* The symbols in a dll are offset by 0x1000, which is the
1230 offset from 0 of the first byte in an image - because
1231 of the file header and the section alignment. */
1232 load_addr
+= 0x1000;
1233 unloaded_dll (NULL
, load_addr
);
1237 handle_exception (struct target_waitstatus
*ourstatus
)
1239 DWORD code
= current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
;
1241 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
1245 case EXCEPTION_ACCESS_VIOLATION
:
1246 OUTMSG2 (("EXCEPTION_ACCESS_VIOLATION"));
1247 ourstatus
->value
.sig
= GDB_SIGNAL_SEGV
;
1249 case STATUS_STACK_OVERFLOW
:
1250 OUTMSG2 (("STATUS_STACK_OVERFLOW"));
1251 ourstatus
->value
.sig
= GDB_SIGNAL_SEGV
;
1253 case STATUS_FLOAT_DENORMAL_OPERAND
:
1254 OUTMSG2 (("STATUS_FLOAT_DENORMAL_OPERAND"));
1255 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1257 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
1258 OUTMSG2 (("EXCEPTION_ARRAY_BOUNDS_EXCEEDED"));
1259 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1261 case STATUS_FLOAT_INEXACT_RESULT
:
1262 OUTMSG2 (("STATUS_FLOAT_INEXACT_RESULT"));
1263 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1265 case STATUS_FLOAT_INVALID_OPERATION
:
1266 OUTMSG2 (("STATUS_FLOAT_INVALID_OPERATION"));
1267 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1269 case STATUS_FLOAT_OVERFLOW
:
1270 OUTMSG2 (("STATUS_FLOAT_OVERFLOW"));
1271 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1273 case STATUS_FLOAT_STACK_CHECK
:
1274 OUTMSG2 (("STATUS_FLOAT_STACK_CHECK"));
1275 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1277 case STATUS_FLOAT_UNDERFLOW
:
1278 OUTMSG2 (("STATUS_FLOAT_UNDERFLOW"));
1279 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1281 case STATUS_FLOAT_DIVIDE_BY_ZERO
:
1282 OUTMSG2 (("STATUS_FLOAT_DIVIDE_BY_ZERO"));
1283 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1285 case STATUS_INTEGER_DIVIDE_BY_ZERO
:
1286 OUTMSG2 (("STATUS_INTEGER_DIVIDE_BY_ZERO"));
1287 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1289 case STATUS_INTEGER_OVERFLOW
:
1290 OUTMSG2 (("STATUS_INTEGER_OVERFLOW"));
1291 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1293 case EXCEPTION_BREAKPOINT
:
1294 OUTMSG2 (("EXCEPTION_BREAKPOINT"));
1295 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1297 /* Remove the initial breakpoint. */
1298 check_breakpoints ((CORE_ADDR
) (long) current_event
1299 .u
.Exception
.ExceptionRecord
.ExceptionAddress
);
1303 OUTMSG2 (("DBG_CONTROL_C"));
1304 ourstatus
->value
.sig
= GDB_SIGNAL_INT
;
1306 case DBG_CONTROL_BREAK
:
1307 OUTMSG2 (("DBG_CONTROL_BREAK"));
1308 ourstatus
->value
.sig
= GDB_SIGNAL_INT
;
1310 case EXCEPTION_SINGLE_STEP
:
1311 OUTMSG2 (("EXCEPTION_SINGLE_STEP"));
1312 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1314 case EXCEPTION_ILLEGAL_INSTRUCTION
:
1315 OUTMSG2 (("EXCEPTION_ILLEGAL_INSTRUCTION"));
1316 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1318 case EXCEPTION_PRIV_INSTRUCTION
:
1319 OUTMSG2 (("EXCEPTION_PRIV_INSTRUCTION"));
1320 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1322 case EXCEPTION_NONCONTINUABLE_EXCEPTION
:
1323 OUTMSG2 (("EXCEPTION_NONCONTINUABLE_EXCEPTION"));
1324 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1327 if (current_event
.u
.Exception
.dwFirstChance
)
1329 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1332 OUTMSG2 (("gdbserver: unknown target exception 0x%08x at 0x%s",
1333 (unsigned) current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
,
1334 phex_nz ((uintptr_t) current_event
.u
.Exception
.ExceptionRecord
.
1335 ExceptionAddress
, sizeof (uintptr_t))));
1336 ourstatus
->value
.sig
= GDB_SIGNAL_UNKNOWN
;
1340 last_sig
= ourstatus
->value
.sig
;
1345 suspend_one_thread (struct inferior_list_entry
*entry
)
1347 struct thread_info
*thread
= (struct thread_info
*) entry
;
1348 win32_thread_info
*th
= (win32_thread_info
*) inferior_target_data (thread
);
1352 if (SuspendThread (th
->h
) == (DWORD
) -1)
1354 DWORD err
= GetLastError ();
1355 OUTMSG (("warning: SuspendThread failed in suspend_one_thread, "
1356 "(error %d): %s\n", (int) err
, strwinerror (err
)));
1364 fake_breakpoint_event (void)
1366 OUTMSG2(("fake_breakpoint_event\n"));
1368 faked_breakpoint
= 1;
1370 memset (¤t_event
, 0, sizeof (current_event
));
1371 current_event
.dwThreadId
= main_thread_id
;
1372 current_event
.dwDebugEventCode
= EXCEPTION_DEBUG_EVENT
;
1373 current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
1374 = EXCEPTION_BREAKPOINT
;
1376 for_each_inferior (&all_threads
, suspend_one_thread
);
1381 auto_delete_breakpoint (CORE_ADDR stop_pc
)
1387 /* Get the next event from the child. */
1390 get_child_debug_event (struct target_waitstatus
*ourstatus
)
1394 last_sig
= GDB_SIGNAL_0
;
1395 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1397 /* Check if GDB sent us an interrupt request. */
1398 check_remote_input_interrupt_request ();
1400 if (soft_interrupt_requested
)
1402 soft_interrupt_requested
= 0;
1403 fake_breakpoint_event ();
1412 /* WinCE doesn't set an initial breakpoint automatically. To
1413 stop the inferior, we flush all currently pending debug
1414 events -- the thread list and the dll list are always
1415 reported immediatelly without delay, then, we suspend all
1416 threads and pretend we saw a trap at the current PC of the
1419 Contrary to desktop Windows, Windows CE *does* report the dll
1420 names on LOAD_DLL_DEBUG_EVENTs resulting from a
1421 DebugActiveProcess call. This limits the way we can detect
1422 if all the dlls have already been reported. If we get a real
1423 debug event before leaving attaching, the worst that will
1424 happen is the user will see a spurious breakpoint. */
1426 current_event
.dwDebugEventCode
= 0;
1427 if (!WaitForDebugEvent (¤t_event
, 0))
1429 OUTMSG2(("no attach events left\n"));
1430 fake_breakpoint_event ();
1434 OUTMSG2(("got attach event\n"));
1439 /* Keep the wait time low enough for confortable remote
1440 interruption, but high enough so gdbserver doesn't become a
1442 if (!WaitForDebugEvent (¤t_event
, 250))
1444 DWORD e
= GetLastError();
1446 if (e
== ERROR_PIPE_NOT_CONNECTED
)
1448 /* This will happen if the loader fails to succesfully
1449 load the application, e.g., if the main executable
1450 tries to pull in a non-existing export from a
1452 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1453 ourstatus
->value
.integer
= 1;
1463 switch (current_event
.dwDebugEventCode
)
1465 case CREATE_THREAD_DEBUG_EVENT
:
1466 OUTMSG2 (("gdbserver: kernel event CREATE_THREAD_DEBUG_EVENT "
1467 "for pid=%u tid=%x)\n",
1468 (unsigned) current_event
.dwProcessId
,
1469 (unsigned) current_event
.dwThreadId
));
1471 /* Record the existence of this thread. */
1472 child_add_thread (current_event
.dwProcessId
,
1473 current_event
.dwThreadId
,
1474 current_event
.u
.CreateThread
.hThread
,
1475 current_event
.u
.CreateThread
.lpThreadLocalBase
);
1478 case EXIT_THREAD_DEBUG_EVENT
:
1479 OUTMSG2 (("gdbserver: kernel event EXIT_THREAD_DEBUG_EVENT "
1480 "for pid=%u tid=%x\n",
1481 (unsigned) current_event
.dwProcessId
,
1482 (unsigned) current_event
.dwThreadId
));
1483 child_delete_thread (current_event
.dwProcessId
,
1484 current_event
.dwThreadId
);
1486 current_thread
= (struct thread_info
*) all_threads
.head
;
1489 case CREATE_PROCESS_DEBUG_EVENT
:
1490 OUTMSG2 (("gdbserver: kernel event CREATE_PROCESS_DEBUG_EVENT "
1491 "for pid=%u tid=%x\n",
1492 (unsigned) current_event
.dwProcessId
,
1493 (unsigned) current_event
.dwThreadId
));
1494 CloseHandle (current_event
.u
.CreateProcessInfo
.hFile
);
1496 current_process_handle
= current_event
.u
.CreateProcessInfo
.hProcess
;
1497 main_thread_id
= current_event
.dwThreadId
;
1499 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
1500 ourstatus
->value
.execd_pathname
= "Main executable";
1502 /* Add the main thread. */
1503 child_add_thread (current_event
.dwProcessId
,
1505 current_event
.u
.CreateProcessInfo
.hThread
,
1506 current_event
.u
.CreateProcessInfo
.lpThreadLocalBase
);
1508 ourstatus
->value
.related_pid
= debug_event_ptid (¤t_event
);
1512 /* Windows CE doesn't set the initial breakpoint
1513 automatically like the desktop versions of Windows do.
1514 We add it explicitly here. It will be removed as soon as
1516 set_breakpoint_at ((CORE_ADDR
) (long) current_event
.u
1517 .CreateProcessInfo
.lpStartAddress
,
1518 auto_delete_breakpoint
);
1523 case EXIT_PROCESS_DEBUG_EVENT
:
1524 OUTMSG2 (("gdbserver: kernel event EXIT_PROCESS_DEBUG_EVENT "
1525 "for pid=%u tid=%x\n",
1526 (unsigned) current_event
.dwProcessId
,
1527 (unsigned) current_event
.dwThreadId
));
1528 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1529 ourstatus
->value
.integer
= current_event
.u
.ExitProcess
.dwExitCode
;
1530 child_continue (DBG_CONTINUE
, -1);
1531 CloseHandle (current_process_handle
);
1532 current_process_handle
= NULL
;
1535 case LOAD_DLL_DEBUG_EVENT
:
1536 OUTMSG2 (("gdbserver: kernel event LOAD_DLL_DEBUG_EVENT "
1537 "for pid=%u tid=%x\n",
1538 (unsigned) current_event
.dwProcessId
,
1539 (unsigned) current_event
.dwThreadId
));
1540 CloseHandle (current_event
.u
.LoadDll
.hFile
);
1541 if (! child_initialization_done
)
1545 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1546 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1549 case UNLOAD_DLL_DEBUG_EVENT
:
1550 OUTMSG2 (("gdbserver: kernel event UNLOAD_DLL_DEBUG_EVENT "
1551 "for pid=%u tid=%x\n",
1552 (unsigned) current_event
.dwProcessId
,
1553 (unsigned) current_event
.dwThreadId
));
1554 if (! child_initialization_done
)
1556 handle_unload_dll ();
1557 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1558 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1561 case EXCEPTION_DEBUG_EVENT
:
1562 OUTMSG2 (("gdbserver: kernel event EXCEPTION_DEBUG_EVENT "
1563 "for pid=%u tid=%x\n",
1564 (unsigned) current_event
.dwProcessId
,
1565 (unsigned) current_event
.dwThreadId
));
1566 handle_exception (ourstatus
);
1569 case OUTPUT_DEBUG_STRING_EVENT
:
1570 /* A message from the kernel (or Cygwin). */
1571 OUTMSG2 (("gdbserver: kernel event OUTPUT_DEBUG_STRING_EVENT "
1572 "for pid=%u tid=%x\n",
1573 (unsigned) current_event
.dwProcessId
,
1574 (unsigned) current_event
.dwThreadId
));
1575 handle_output_debug_string (ourstatus
);
1579 OUTMSG2 (("gdbserver: kernel event unknown "
1580 "for pid=%u tid=%x code=%x\n",
1581 (unsigned) current_event
.dwProcessId
,
1582 (unsigned) current_event
.dwThreadId
,
1583 (unsigned) current_event
.dwDebugEventCode
));
1587 ptid
= debug_event_ptid (¤t_event
);
1589 (struct thread_info
*) find_inferior_id (&all_threads
, ptid
);
1593 /* Wait for the inferior process to change state.
1594 STATUS will be filled in with a response code to send to GDB.
1595 Returns the signal which caused the process to stop. */
1597 win32_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
, int options
)
1599 struct regcache
*regcache
;
1601 if (cached_status
.kind
!= TARGET_WAITKIND_IGNORE
)
1603 /* The core always does a wait after creating the inferior, and
1604 do_initial_child_stuff already ran the inferior to the
1605 initial breakpoint (or an exit, if creating the process
1606 fails). Report it now. */
1607 *ourstatus
= cached_status
;
1608 cached_status
.kind
= TARGET_WAITKIND_IGNORE
;
1609 return debug_event_ptid (¤t_event
);
1614 if (!get_child_debug_event (ourstatus
))
1617 switch (ourstatus
->kind
)
1619 case TARGET_WAITKIND_EXITED
:
1620 OUTMSG2 (("Child exited with retcode = %x\n",
1621 ourstatus
->value
.integer
));
1622 win32_clear_inferiors ();
1623 return pid_to_ptid (current_event
.dwProcessId
);
1624 case TARGET_WAITKIND_STOPPED
:
1625 case TARGET_WAITKIND_LOADED
:
1626 OUTMSG2 (("Child Stopped with signal = %d \n",
1627 ourstatus
->value
.sig
));
1629 regcache
= get_thread_regcache (current_thread
, 1);
1630 child_fetch_inferior_registers (regcache
, -1);
1631 return debug_event_ptid (¤t_event
);
1633 OUTMSG (("Ignoring unknown internal event, %d\n", ourstatus
->kind
));
1635 case TARGET_WAITKIND_SPURIOUS
:
1636 case TARGET_WAITKIND_EXECD
:
1637 /* do nothing, just continue */
1638 child_continue (DBG_CONTINUE
, -1);
1644 /* Fetch registers from the inferior process.
1645 If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO. */
1647 win32_fetch_inferior_registers (struct regcache
*regcache
, int regno
)
1649 child_fetch_inferior_registers (regcache
, regno
);
1652 /* Store registers to the inferior process.
1653 If REGNO is -1, store all registers; otherwise, store at least REGNO. */
1655 win32_store_inferior_registers (struct regcache
*regcache
, int regno
)
1657 child_store_inferior_registers (regcache
, regno
);
1660 /* Read memory from the inferior process. This should generally be
1661 called through read_inferior_memory, which handles breakpoint shadowing.
1662 Read LEN bytes at MEMADDR into a buffer at MYADDR. */
1664 win32_read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
1666 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 0, 0) != len
;
1669 /* Write memory to the inferior process. This should generally be
1670 called through write_inferior_memory, which handles breakpoint shadowing.
1671 Write LEN bytes from the buffer at MYADDR to MEMADDR.
1672 Returns 0 on success and errno on failure. */
1674 win32_write_inferior_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
,
1677 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 1, 0) != len
;
1680 /* Send an interrupt request to the inferior process. */
1682 win32_request_interrupt (void)
1684 winapi_DebugBreakProcess DebugBreakProcess
;
1685 winapi_GenerateConsoleCtrlEvent GenerateConsoleCtrlEvent
;
1688 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
1690 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
1693 GenerateConsoleCtrlEvent
= GETPROCADDRESS (dll
, GenerateConsoleCtrlEvent
);
1695 if (GenerateConsoleCtrlEvent
!= NULL
1696 && GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT
, current_process_id
))
1699 /* GenerateConsoleCtrlEvent can fail if process id being debugged is
1700 not a process group id.
1701 Fallback to XP/Vista 'DebugBreakProcess', which generates a
1702 breakpoint exception in the interior process. */
1704 DebugBreakProcess
= GETPROCADDRESS (dll
, DebugBreakProcess
);
1706 if (DebugBreakProcess
!= NULL
1707 && DebugBreakProcess (current_process_handle
))
1710 /* Last resort, suspend all threads manually. */
1711 soft_interrupt_requested
= 1;
1716 win32_error_to_fileio_error (DWORD err
)
1720 case ERROR_BAD_PATHNAME
:
1721 case ERROR_FILE_NOT_FOUND
:
1722 case ERROR_INVALID_NAME
:
1723 case ERROR_PATH_NOT_FOUND
:
1724 return FILEIO_ENOENT
;
1726 case ERROR_IO_DEVICE
:
1727 case ERROR_OPEN_FAILED
:
1729 case ERROR_INVALID_HANDLE
:
1730 return FILEIO_EBADF
;
1731 case ERROR_ACCESS_DENIED
:
1732 case ERROR_SHARING_VIOLATION
:
1733 return FILEIO_EACCES
;
1734 case ERROR_NOACCESS
:
1735 return FILEIO_EFAULT
;
1737 return FILEIO_EBUSY
;
1738 case ERROR_ALREADY_EXISTS
:
1739 case ERROR_FILE_EXISTS
:
1740 return FILEIO_EEXIST
;
1741 case ERROR_BAD_DEVICE
:
1742 return FILEIO_ENODEV
;
1743 case ERROR_DIRECTORY
:
1744 return FILEIO_ENOTDIR
;
1745 case ERROR_FILENAME_EXCED_RANGE
:
1746 case ERROR_INVALID_DATA
:
1747 case ERROR_INVALID_PARAMETER
:
1748 case ERROR_NEGATIVE_SEEK
:
1749 return FILEIO_EINVAL
;
1750 case ERROR_TOO_MANY_OPEN_FILES
:
1751 return FILEIO_EMFILE
;
1752 case ERROR_HANDLE_DISK_FULL
:
1753 case ERROR_DISK_FULL
:
1754 return FILEIO_ENOSPC
;
1755 case ERROR_WRITE_PROTECT
:
1756 return FILEIO_EROFS
;
1757 case ERROR_NOT_SUPPORTED
:
1758 return FILEIO_ENOSYS
;
1761 return FILEIO_EUNKNOWN
;
1765 wince_hostio_last_error (char *buf
)
1767 DWORD winerr
= GetLastError ();
1768 int fileio_err
= win32_error_to_fileio_error (winerr
);
1769 sprintf (buf
, "F-1,%x", fileio_err
);
1773 /* Write Windows OS Thread Information Block address. */
1776 win32_get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
)
1778 win32_thread_info
*th
;
1779 th
= thread_rec (ptid
, 0);
1783 *addr
= th
->thread_local_base
;
1787 /* Implementation of the target_ops method "sw_breakpoint_from_kind". */
1789 static const gdb_byte
*
1790 win32_sw_breakpoint_from_kind (int kind
, int *size
)
1792 *size
= the_low_target
.breakpoint_len
;
1793 return the_low_target
.breakpoint
;
1796 static struct target_ops win32_target_ops
= {
1797 win32_create_inferior
,
1798 NULL
, /* arch_setup */
1807 win32_fetch_inferior_registers
,
1808 win32_store_inferior_registers
,
1809 NULL
, /* prepare_to_access_memory */
1810 NULL
, /* done_accessing_memory */
1811 win32_read_inferior_memory
,
1812 win32_write_inferior_memory
,
1813 NULL
, /* lookup_symbols */
1814 win32_request_interrupt
,
1815 NULL
, /* read_auxv */
1816 win32_supports_z_point_type
,
1819 NULL
, /* stopped_by_sw_breakpoint */
1820 NULL
, /* supports_stopped_by_sw_breakpoint */
1821 NULL
, /* stopped_by_hw_breakpoint */
1822 NULL
, /* supports_stopped_by_hw_breakpoint */
1823 target_can_do_hardware_single_step
,
1824 win32_stopped_by_watchpoint
,
1825 win32_stopped_data_address
,
1826 NULL
, /* read_offsets */
1827 NULL
, /* get_tls_address */
1828 NULL
, /* qxfer_spu */
1830 wince_hostio_last_error
,
1832 hostio_last_error_from_errno
,
1834 NULL
, /* qxfer_osdata */
1835 NULL
, /* qxfer_siginfo */
1836 NULL
, /* supports_non_stop */
1838 NULL
, /* start_non_stop */
1839 NULL
, /* supports_multi_process */
1840 NULL
, /* supports_fork_events */
1841 NULL
, /* supports_vfork_events */
1842 NULL
, /* supports_exec_events */
1843 NULL
, /* handle_new_gdb_connection */
1844 NULL
, /* handle_monitor_command */
1845 NULL
, /* core_of_thread */
1846 NULL
, /* read_loadmap */
1847 NULL
, /* process_qsupported */
1848 NULL
, /* supports_tracepoints */
1850 NULL
, /* write_pc */
1851 NULL
, /* thread_stopped */
1852 win32_get_tib_address
,
1853 NULL
, /* pause_all */
1854 NULL
, /* unpause_all */
1855 NULL
, /* stabilize_threads */
1856 NULL
, /* install_fast_tracepoint_jump_pad */
1857 NULL
, /* emit_ops */
1858 NULL
, /* supports_disable_randomization */
1859 NULL
, /* get_min_fast_tracepoint_insn_len */
1860 NULL
, /* qxfer_libraries_svr4 */
1861 NULL
, /* support_agent */
1862 NULL
, /* support_btrace */
1863 NULL
, /* enable_btrace */
1864 NULL
, /* disable_btrace */
1865 NULL
, /* read_btrace */
1866 NULL
, /* read_btrace_conf */
1867 NULL
, /* supports_range_stepping */
1868 NULL
, /* pid_to_exec_file */
1869 NULL
, /* multifs_open */
1870 NULL
, /* multifs_unlink */
1871 NULL
, /* multifs_readlink */
1872 NULL
, /* breakpoint_kind_from_pc */
1873 win32_sw_breakpoint_from_kind
,
1876 /* Initialize the Win32 backend. */
1878 initialize_low (void)
1880 set_target_ops (&win32_target_ops
);
1881 the_low_target
.arch_setup ();