1 /* Low level interface to Windows debugging, for gdbserver.
2 Copyright (C) 2006-2014 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/signals.h"
24 #include "gdb/fileio.h"
25 #include "mem-break.h"
26 #include "win32-low.h"
27 #include "gdbthread.h"
40 #include <sys/cygwin.h>
43 #define OUTMSG(X) do { printf X; fflush (stderr); } while (0)
56 #define _T(x) TEXT (x)
60 #define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0]))
64 # define GETPROCADDRESS(DLL, PROC) \
65 ((winapi_ ## PROC) GetProcAddress (DLL, TEXT (#PROC)))
67 # define GETPROCADDRESS(DLL, PROC) \
68 ((winapi_ ## PROC) GetProcAddress (DLL, #PROC))
71 int using_threads
= 1;
74 static int attaching
= 0;
75 static HANDLE current_process_handle
= NULL
;
76 static DWORD current_process_id
= 0;
77 static DWORD main_thread_id
= 0;
78 static enum gdb_signal last_sig
= GDB_SIGNAL_0
;
80 /* The current debug event from WaitForDebugEvent. */
81 static DEBUG_EVENT current_event
;
83 /* A status that hasn't been reported to the core yet, and so
84 win32_wait should return it next, instead of fetching the next
85 debug event off the win32 API. */
86 static struct target_waitstatus cached_status
;
88 /* Non zero if an interrupt request is to be satisfied by suspending
90 static int soft_interrupt_requested
= 0;
92 /* Non zero if the inferior is stopped in a simulated breakpoint done
93 by suspending all the threads. */
94 static int faked_breakpoint
= 0;
96 const struct target_desc
*win32_tdesc
;
98 #define NUM_REGS (the_low_target.num_regs)
100 typedef BOOL (WINAPI
*winapi_DebugActiveProcessStop
) (DWORD dwProcessId
);
101 typedef BOOL (WINAPI
*winapi_DebugSetProcessKillOnExit
) (BOOL KillOnExit
);
102 typedef BOOL (WINAPI
*winapi_DebugBreakProcess
) (HANDLE
);
103 typedef BOOL (WINAPI
*winapi_GenerateConsoleCtrlEvent
) (DWORD
, DWORD
);
105 static ptid_t
win32_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
,
107 static void win32_resume (struct thread_resume
*resume_info
, size_t n
);
109 static void win32_add_all_dlls (void);
112 /* Get the thread ID from the current selected inferior (the current
115 current_inferior_ptid (void)
120 /* The current debug event from WaitForDebugEvent. */
122 debug_event_ptid (DEBUG_EVENT
*event
)
124 return ptid_build (event
->dwProcessId
, event
->dwThreadId
, 0);
127 /* Get the thread context of the thread associated with TH. */
130 win32_get_thread_context (win32_thread_info
*th
)
132 memset (&th
->context
, 0, sizeof (CONTEXT
));
133 (*the_low_target
.get_thread_context
) (th
, ¤t_event
);
135 memcpy (&th
->base_context
, &th
->context
, sizeof (CONTEXT
));
139 /* Set the thread context of the thread associated with TH. */
142 win32_set_thread_context (win32_thread_info
*th
)
145 /* Calling SuspendThread on a thread that is running kernel code
146 will report that the suspending was successful, but in fact, that
147 will often not be true. In those cases, the context returned by
148 GetThreadContext will not be correct by the time the thread
149 stops, hence we can't set that context back into the thread when
150 resuming - it will most likelly crash the inferior.
151 Unfortunately, there is no way to know when the thread will
152 really stop. To work around it, we'll only write the context
153 back to the thread when either the user or GDB explicitly change
154 it between stopping and resuming. */
155 if (memcmp (&th
->context
, &th
->base_context
, sizeof (CONTEXT
)) != 0)
157 (*the_low_target
.set_thread_context
) (th
, ¤t_event
);
160 /* Find a thread record given a thread id. If GET_CONTEXT is set then
161 also retrieve the context for this thread. */
162 static win32_thread_info
*
163 thread_rec (ptid_t ptid
, int get_context
)
165 struct thread_info
*thread
;
166 win32_thread_info
*th
;
168 thread
= (struct thread_info
*) find_inferior_id (&all_threads
, ptid
);
172 th
= inferior_target_data (thread
);
173 if (get_context
&& 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
);
193 /* Add a thread to the thread list. */
194 static win32_thread_info
*
195 child_add_thread (DWORD pid
, DWORD tid
, HANDLE h
, void *tlb
)
197 win32_thread_info
*th
;
198 ptid_t ptid
= ptid_build (pid
, tid
, 0);
200 if ((th
= thread_rec (ptid
, FALSE
)))
203 th
= xcalloc (1, sizeof (*th
));
206 th
->thread_local_base
= (CORE_ADDR
) (uintptr_t) tlb
;
208 add_thread (ptid
, th
);
210 if (the_low_target
.thread_added
!= NULL
)
211 (*the_low_target
.thread_added
) (th
);
216 /* Delete a thread from the list of threads. */
218 delete_thread_info (struct inferior_list_entry
*thread
)
220 win32_thread_info
*th
= inferior_target_data ((struct thread_info
*) thread
);
222 remove_thread ((struct thread_info
*) thread
);
227 /* Delete a thread from the list of threads. */
229 child_delete_thread (DWORD pid
, DWORD tid
)
231 struct inferior_list_entry
*thread
;
234 /* If the last thread is exiting, just return. */
235 if (one_inferior_p (&all_threads
))
238 ptid
= ptid_build (pid
, tid
, 0);
239 thread
= find_inferior_id (&all_threads
, ptid
);
243 delete_thread_info (thread
);
246 /* These watchpoint related wrapper functions simply pass on the function call
247 if the low target has registered a corresponding function. */
250 win32_supports_z_point_type (char z_type
)
252 return (the_low_target
.supports_z_point_type
!= NULL
253 && the_low_target
.supports_z_point_type (z_type
));
257 win32_insert_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
258 int size
, struct raw_breakpoint
*bp
)
260 if (the_low_target
.insert_point
!= NULL
)
261 return the_low_target
.insert_point (type
, addr
, size
, bp
);
263 /* Unsupported (see target.h). */
268 win32_remove_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
269 int size
, struct raw_breakpoint
*bp
)
271 if (the_low_target
.remove_point
!= NULL
)
272 return the_low_target
.remove_point (type
, addr
, size
, bp
);
274 /* Unsupported (see target.h). */
279 win32_stopped_by_watchpoint (void)
281 if (the_low_target
.stopped_by_watchpoint
!= NULL
)
282 return the_low_target
.stopped_by_watchpoint ();
288 win32_stopped_data_address (void)
290 if (the_low_target
.stopped_data_address
!= NULL
)
291 return the_low_target
.stopped_data_address ();
297 /* Transfer memory from/to the debugged process. */
299 child_xfer_memory (CORE_ADDR memaddr
, char *our
, int len
,
300 int write
, struct target_ops
*target
)
305 uintptr_t addr
= (uintptr_t) memaddr
;
309 success
= WriteProcessMemory (current_process_handle
, (LPVOID
) addr
,
310 (LPCVOID
) our
, len
, &done
);
312 lasterror
= GetLastError ();
313 FlushInstructionCache (current_process_handle
, (LPCVOID
) addr
, len
);
317 success
= ReadProcessMemory (current_process_handle
, (LPCVOID
) addr
,
318 (LPVOID
) our
, len
, &done
);
320 lasterror
= GetLastError ();
322 if (!success
&& lasterror
== ERROR_PARTIAL_COPY
&& done
> 0)
325 return success
? done
: -1;
328 /* Clear out any old thread list and reinitialize it to a pristine
331 child_init_thread_list (void)
333 for_each_inferior (&all_threads
, delete_thread_info
);
336 /* Zero during the child initialization phase, and nonzero otherwise. */
338 static int child_initialization_done
= 0;
341 do_initial_child_stuff (HANDLE proch
, DWORD pid
, int attached
)
343 struct process_info
*proc
;
345 last_sig
= GDB_SIGNAL_0
;
347 current_process_handle
= proch
;
348 current_process_id
= pid
;
351 soft_interrupt_requested
= 0;
352 faked_breakpoint
= 0;
354 memset (¤t_event
, 0, sizeof (current_event
));
356 proc
= add_process (pid
, attached
);
357 proc
->tdesc
= win32_tdesc
;
358 child_init_thread_list ();
359 child_initialization_done
= 0;
361 if (the_low_target
.initial_stuff
!= NULL
)
362 (*the_low_target
.initial_stuff
) ();
364 cached_status
.kind
= TARGET_WAITKIND_IGNORE
;
366 /* Flush all currently pending debug events (thread and dll list) up
367 to the initial breakpoint. */
370 struct target_waitstatus status
;
372 win32_wait (minus_one_ptid
, &status
, 0);
374 /* Note win32_wait doesn't return thread events. */
375 if (status
.kind
!= TARGET_WAITKIND_LOADED
)
377 cached_status
= status
;
382 struct thread_resume resume
;
384 resume
.thread
= minus_one_ptid
;
385 resume
.kind
= resume_continue
;
388 win32_resume (&resume
, 1);
393 /* Now that the inferior has been started and all DLLs have been mapped,
394 we can iterate over all DLLs and load them in.
396 We avoid doing it any earlier because, on certain versions of Windows,
397 LOAD_DLL_DEBUG_EVENTs are sometimes not complete. In particular,
398 we have seen on Windows 8.1 that the ntdll.dll load event does not
399 include the DLL name, preventing us from creating an associated SO.
400 A possible explanation is that ntdll.dll might be mapped before
401 the SO info gets created by the Windows system -- ntdll.dll is
402 the first DLL to be reported via LOAD_DLL_DEBUG_EVENT and other DLLs
403 do not seem to suffer from that problem.
405 Rather than try to work around this sort of issue, it is much
406 simpler to just ignore DLL load/unload events during the startup
407 phase, and then process them all in one batch now. */
408 win32_add_all_dlls ();
411 child_initialization_done
= 1;
414 /* Resume all artificially suspended threads if we are continuing
417 continue_one_thread (struct inferior_list_entry
*this_thread
, void *id_ptr
)
419 struct thread_info
*thread
= (struct thread_info
*) this_thread
;
420 int thread_id
= * (int *) id_ptr
;
421 win32_thread_info
*th
= inferior_target_data (thread
);
423 if ((thread_id
== -1 || thread_id
== th
->tid
)
426 if (th
->context
.ContextFlags
)
428 win32_set_thread_context (th
);
429 th
->context
.ContextFlags
= 0;
432 if (ResumeThread (th
->h
) == (DWORD
) -1)
434 DWORD err
= GetLastError ();
435 OUTMSG (("warning: ResumeThread failed in continue_one_thread, "
436 "(error %d): %s\n", (int) err
, strwinerror (err
)));
445 child_continue (DWORD continue_status
, int thread_id
)
447 /* The inferior will only continue after the ContinueDebugEvent
449 find_inferior (&all_threads
, continue_one_thread
, &thread_id
);
450 faked_breakpoint
= 0;
452 if (!ContinueDebugEvent (current_event
.dwProcessId
,
453 current_event
.dwThreadId
,
460 /* Fetch register(s) from the current thread context. */
462 child_fetch_inferior_registers (struct regcache
*regcache
, int r
)
465 win32_thread_info
*th
= thread_rec (current_inferior_ptid (), TRUE
);
466 if (r
== -1 || r
> NUM_REGS
)
467 child_fetch_inferior_registers (regcache
, NUM_REGS
);
469 for (regno
= 0; regno
< r
; regno
++)
470 (*the_low_target
.fetch_inferior_register
) (regcache
, th
, regno
);
473 /* Store a new register value into the current thread context. We don't
474 change the program's context until later, when we resume it. */
476 child_store_inferior_registers (struct regcache
*regcache
, int r
)
479 win32_thread_info
*th
= thread_rec (current_inferior_ptid (), TRUE
);
480 if (r
== -1 || r
== 0 || r
> NUM_REGS
)
481 child_store_inferior_registers (regcache
, NUM_REGS
);
483 for (regno
= 0; regno
< r
; regno
++)
484 (*the_low_target
.store_inferior_register
) (regcache
, th
, regno
);
487 /* Map the Windows error number in ERROR to a locale-dependent error
488 message string and return a pointer to it. Typically, the values
489 for ERROR come from GetLastError.
491 The string pointed to shall not be modified by the application,
492 but may be overwritten by a subsequent call to strwinerror
494 The strwinerror function does not change the current setting
498 strwinerror (DWORD error
)
500 static char buf
[1024];
502 DWORD lasterr
= GetLastError ();
503 DWORD chars
= FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
504 | FORMAT_MESSAGE_ALLOCATE_BUFFER
,
507 0, /* Default language */
513 /* If there is an \r\n appended, zap it. */
515 && msgbuf
[chars
- 2] == '\r'
516 && msgbuf
[chars
- 1] == '\n')
522 if (chars
> ((COUNTOF (buf
)) - 1))
524 chars
= COUNTOF (buf
) - 1;
529 wcstombs (buf
, msgbuf
, chars
+ 1);
531 strncpy (buf
, msgbuf
, chars
+ 1);
536 sprintf (buf
, "unknown win32 error (%u)", (unsigned) error
);
538 SetLastError (lasterr
);
543 create_process (const char *program
, char *args
,
544 DWORD flags
, PROCESS_INFORMATION
*pi
)
549 wchar_t *p
, *wprogram
, *wargs
;
552 wprogram
= alloca ((strlen (program
) + 1) * sizeof (wchar_t));
553 mbstowcs (wprogram
, program
, strlen (program
) + 1);
555 for (p
= wprogram
; *p
; ++p
)
559 argslen
= strlen (args
);
560 wargs
= alloca ((argslen
+ 1) * sizeof (wchar_t));
561 mbstowcs (wargs
, args
, argslen
+ 1);
563 ret
= CreateProcessW (wprogram
, /* image name */
564 wargs
, /* command line */
565 NULL
, /* security, not supported */
566 NULL
, /* thread, not supported */
567 FALSE
, /* inherit handles, not supported */
568 flags
, /* start flags */
569 NULL
, /* environment, not supported */
570 NULL
, /* current directory, not supported */
571 NULL
, /* start info, not supported */
574 STARTUPINFOA si
= { sizeof (STARTUPINFOA
) };
576 ret
= CreateProcessA (program
, /* image name */
577 args
, /* command line */
580 TRUE
, /* inherit handles */
581 flags
, /* start flags */
582 NULL
, /* environment */
583 NULL
, /* current directory */
584 &si
, /* start info */
591 /* Start a new process.
592 PROGRAM is a path to the program to execute.
593 ARGS is a standard NULL-terminated array of arguments,
594 to be passed to the inferior as ``argv''.
595 Returns the new PID on success, -1 on failure. Registers the new
596 process with the process list. */
598 win32_create_inferior (char *program
, char **program_args
)
601 char real_path
[PATH_MAX
];
602 char *orig_path
, *new_path
, *path_ptr
;
609 PROCESS_INFORMATION pi
;
612 /* win32_wait needs to know we're not attaching. */
616 error ("No executable specified, specify executable to debug.\n");
618 flags
= DEBUG_PROCESS
| DEBUG_ONLY_THIS_PROCESS
;
622 path_ptr
= getenv ("PATH");
625 int size
= cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, NULL
, 0);
626 orig_path
= alloca (strlen (path_ptr
) + 1);
627 new_path
= alloca (size
);
628 strcpy (orig_path
, path_ptr
);
629 cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, new_path
, size
);
630 setenv ("PATH", new_path
, 1);
632 cygwin_conv_path (CCP_POSIX_TO_WIN_A
, program
, real_path
, PATH_MAX
);
637 for (argc
= 1; program_args
[argc
]; argc
++)
638 argslen
+= strlen (program_args
[argc
]) + 1;
639 args
= alloca (argslen
);
641 for (argc
= 1; program_args
[argc
]; argc
++)
643 /* FIXME: Can we do better about quoting? How does Cygwin
646 strcat (args
, program_args
[argc
]);
648 OUTMSG2 (("Command line is \"%s\"\n", args
));
650 #ifdef CREATE_NEW_PROCESS_GROUP
651 flags
|= CREATE_NEW_PROCESS_GROUP
;
654 ret
= create_process (program
, args
, flags
, &pi
);
655 err
= GetLastError ();
656 if (!ret
&& err
== ERROR_FILE_NOT_FOUND
)
658 char *exename
= alloca (strlen (program
) + 5);
659 strcat (strcpy (exename
, program
), ".exe");
660 ret
= create_process (exename
, args
, flags
, &pi
);
661 err
= GetLastError ();
666 setenv ("PATH", orig_path
, 1);
671 error ("Error creating process \"%s%s\", (error %d): %s\n",
672 program
, args
, (int) err
, strwinerror (err
));
676 OUTMSG2 (("Process created: %s\n", (char *) args
));
680 /* On Windows CE this handle can't be closed. The OS reuses
681 it in the debug events, while the 9x/NT versions of Windows
682 probably use a DuplicateHandle'd one. */
683 CloseHandle (pi
.hThread
);
686 do_initial_child_stuff (pi
.hProcess
, pi
.dwProcessId
, 0);
688 return current_process_id
;
691 /* Attach to a running process.
692 PID is the process ID to attach to, specified by the user
693 or a higher layer. */
695 win32_attach (unsigned long pid
)
698 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
701 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
703 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
705 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
707 h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
710 if (DebugActiveProcess (pid
))
712 if (DebugSetProcessKillOnExit
!= NULL
)
713 DebugSetProcessKillOnExit (FALSE
);
715 /* win32_wait needs to know we're attaching. */
717 do_initial_child_stuff (h
, pid
, 1);
724 err
= GetLastError ();
725 error ("Attach to process failed (error %d): %s\n",
726 (int) err
, strwinerror (err
));
729 /* Handle OUTPUT_DEBUG_STRING_EVENT from child process. */
731 handle_output_debug_string (struct target_waitstatus
*ourstatus
)
733 #define READ_BUFFER_LEN 1024
735 char s
[READ_BUFFER_LEN
+ 1] = { 0 };
736 DWORD nbytes
= current_event
.u
.DebugString
.nDebugStringLength
;
741 if (nbytes
> READ_BUFFER_LEN
)
742 nbytes
= READ_BUFFER_LEN
;
744 addr
= (CORE_ADDR
) (size_t) current_event
.u
.DebugString
.lpDebugStringData
;
746 if (current_event
.u
.DebugString
.fUnicode
)
748 /* The event tells us how many bytes, not chars, even
750 WCHAR buffer
[(READ_BUFFER_LEN
+ 1) / sizeof (WCHAR
)] = { 0 };
751 if (read_inferior_memory (addr
, (unsigned char *) buffer
, nbytes
) != 0)
753 wcstombs (s
, buffer
, (nbytes
+ 1) / sizeof (WCHAR
));
757 if (read_inferior_memory (addr
, (unsigned char *) s
, nbytes
) != 0)
761 if (strncmp (s
, "cYg", 3) != 0)
771 #undef READ_BUFFER_LEN
775 win32_clear_inferiors (void)
777 if (current_process_handle
!= NULL
)
778 CloseHandle (current_process_handle
);
780 for_each_inferior (&all_threads
, delete_thread_info
);
784 /* Kill all inferiors. */
788 struct process_info
*process
;
790 if (current_process_handle
== NULL
)
793 TerminateProcess (current_process_handle
, 0);
796 if (!child_continue (DBG_CONTINUE
, -1))
798 if (!WaitForDebugEvent (¤t_event
, INFINITE
))
800 if (current_event
.dwDebugEventCode
== EXIT_PROCESS_DEBUG_EVENT
)
802 else if (current_event
.dwDebugEventCode
== OUTPUT_DEBUG_STRING_EVENT
)
804 struct target_waitstatus our_status
= { 0 };
805 handle_output_debug_string (&our_status
);
809 win32_clear_inferiors ();
811 process
= find_process_pid (pid
);
812 remove_process (process
);
816 /* Detach from inferior PID. */
818 win32_detach (int pid
)
820 struct process_info
*process
;
821 winapi_DebugActiveProcessStop DebugActiveProcessStop
= NULL
;
822 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
824 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
826 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
828 DebugActiveProcessStop
= GETPROCADDRESS (dll
, DebugActiveProcessStop
);
829 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
831 if (DebugSetProcessKillOnExit
== NULL
832 || DebugActiveProcessStop
== NULL
)
836 struct thread_resume resume
;
837 resume
.thread
= minus_one_ptid
;
838 resume
.kind
= resume_continue
;
840 win32_resume (&resume
, 1);
843 if (!DebugActiveProcessStop (current_process_id
))
846 DebugSetProcessKillOnExit (FALSE
);
847 process
= find_process_pid (pid
);
848 remove_process (process
);
850 win32_clear_inferiors ();
855 win32_mourn (struct process_info
*process
)
857 remove_process (process
);
860 /* Wait for inferiors to end. */
864 HANDLE h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
867 WaitForSingleObject (h
, INFINITE
);
872 /* Return 1 iff the thread with thread ID TID is alive. */
874 win32_thread_alive (ptid_t ptid
)
878 /* Our thread list is reliable; don't bother to poll target
880 if (find_inferior_id (&all_threads
, ptid
) != NULL
)
887 /* Resume the inferior process. RESUME_INFO describes how we want
890 win32_resume (struct thread_resume
*resume_info
, size_t n
)
895 win32_thread_info
*th
;
896 DWORD continue_status
= DBG_CONTINUE
;
899 /* This handles the very limited set of resume packets that GDB can
900 currently produce. */
902 if (n
== 1 && ptid_equal (resume_info
[0].thread
, minus_one_ptid
))
907 /* Yes, we're ignoring resume_info[0].thread. It'd be tricky to make
908 the Windows resume code do the right thing for thread switching. */
909 tid
= current_event
.dwThreadId
;
911 if (!ptid_equal (resume_info
[0].thread
, minus_one_ptid
))
913 sig
= resume_info
[0].sig
;
914 step
= resume_info
[0].kind
== resume_step
;
922 if (sig
!= GDB_SIGNAL_0
)
924 if (current_event
.dwDebugEventCode
!= EXCEPTION_DEBUG_EVENT
)
926 OUTMSG (("Cannot continue with signal %d here.\n", sig
));
928 else if (sig
== last_sig
)
929 continue_status
= DBG_EXCEPTION_NOT_HANDLED
;
931 OUTMSG (("Can only continue with recieved signal %d.\n", last_sig
));
934 last_sig
= GDB_SIGNAL_0
;
936 /* Get context for the currently selected thread. */
937 ptid
= debug_event_ptid (¤t_event
);
938 th
= thread_rec (ptid
, FALSE
);
941 if (th
->context
.ContextFlags
)
943 /* Move register values from the inferior into the thread
944 context structure. */
945 regcache_invalidate ();
949 if (the_low_target
.single_step
!= NULL
)
950 (*the_low_target
.single_step
) (th
);
952 error ("Single stepping is not supported "
953 "in this configuration.\n");
956 win32_set_thread_context (th
);
957 th
->context
.ContextFlags
= 0;
961 /* Allow continuing with the same signal that interrupted us.
962 Otherwise complain. */
964 child_continue (continue_status
, tid
);
968 win32_add_one_solib (const char *name
, CORE_ADDR load_addr
)
970 char buf
[MAX_PATH
+ 1];
971 char buf2
[MAX_PATH
+ 1];
974 WIN32_FIND_DATA w32_fd
;
975 WCHAR wname
[MAX_PATH
+ 1];
976 mbstowcs (wname
, name
, MAX_PATH
);
977 HANDLE h
= FindFirstFile (wname
, &w32_fd
);
979 WIN32_FIND_DATAA w32_fd
;
980 HANDLE h
= FindFirstFileA (name
, &w32_fd
);
983 /* The symbols in a dll are offset by 0x1000, which is the
984 offset from 0 of the first byte in an image - because
985 of the file header and the section alignment. */
988 if (h
== INVALID_HANDLE_VALUE
)
996 char cwd
[MAX_PATH
+ 1];
998 if (GetCurrentDirectoryA (MAX_PATH
+ 1, cwd
))
1000 p
= strrchr (buf
, '\\');
1003 SetCurrentDirectoryA (buf
);
1004 GetFullPathNameA (w32_fd
.cFileName
, MAX_PATH
, buf
, &p
);
1005 SetCurrentDirectoryA (cwd
);
1012 if (strcasecmp (buf
, "ntdll.dll") == 0)
1014 GetSystemDirectoryA (buf
, sizeof (buf
));
1015 strcat (buf
, "\\ntdll.dll");
1020 cygwin_conv_path (CCP_WIN_A_TO_POSIX
, buf
, buf2
, sizeof (buf2
));
1025 loaded_dll (buf2
, load_addr
);
1029 get_image_name (HANDLE h
, void *address
, int unicode
)
1031 static char buf
[(2 * MAX_PATH
) + 1];
1032 DWORD size
= unicode
? sizeof (WCHAR
) : sizeof (char);
1038 /* Attempt to read the name of the dll that was detected.
1039 This is documented to work only when actively debugging
1040 a program. It will not work for attached processes. */
1041 if (address
== NULL
)
1045 /* Windows CE reports the address of the image name,
1046 instead of an address of a pointer into the image name. */
1047 address_ptr
= address
;
1049 /* See if we could read the address of a string, and that the
1050 address isn't null. */
1051 if (!ReadProcessMemory (h
, address
, &address_ptr
,
1052 sizeof (address_ptr
), &done
)
1053 || done
!= sizeof (address_ptr
)
1058 /* Find the length of the string */
1059 while (ReadProcessMemory (h
, address_ptr
+ len
++ * size
, &b
, size
, &done
)
1060 && (b
[0] != 0 || b
[size
- 1] != 0) && done
== size
)
1064 ReadProcessMemory (h
, address_ptr
, buf
, len
, &done
);
1067 WCHAR
*unicode_address
= (WCHAR
*) alloca (len
* sizeof (WCHAR
));
1068 ReadProcessMemory (h
, address_ptr
, unicode_address
, len
* sizeof (WCHAR
),
1071 WideCharToMultiByte (CP_ACP
, 0, unicode_address
, len
, buf
, len
, 0, 0);
1077 typedef BOOL (WINAPI
*winapi_EnumProcessModules
) (HANDLE
, HMODULE
*,
1079 typedef BOOL (WINAPI
*winapi_GetModuleInformation
) (HANDLE
, HMODULE
,
1080 LPMODULEINFO
, DWORD
);
1081 typedef DWORD (WINAPI
*winapi_GetModuleFileNameExA
) (HANDLE
, HMODULE
,
1084 static winapi_EnumProcessModules win32_EnumProcessModules
;
1085 static winapi_GetModuleInformation win32_GetModuleInformation
;
1086 static winapi_GetModuleFileNameExA win32_GetModuleFileNameExA
;
1091 static int psapi_loaded
= 0;
1092 static HMODULE dll
= NULL
;
1097 dll
= LoadLibrary (TEXT("psapi.dll"));
1100 win32_EnumProcessModules
=
1101 GETPROCADDRESS (dll
, EnumProcessModules
);
1102 win32_GetModuleInformation
=
1103 GETPROCADDRESS (dll
, GetModuleInformation
);
1104 win32_GetModuleFileNameExA
=
1105 GETPROCADDRESS (dll
, GetModuleFileNameExA
);
1108 return (win32_EnumProcessModules
!= NULL
1109 && win32_GetModuleInformation
!= NULL
1110 && win32_GetModuleFileNameExA
!= NULL
);
1115 /* Iterate over all DLLs currently mapped by our inferior, and
1116 add them to our list of solibs. */
1119 win32_add_all_dlls (void)
1123 HMODULE
*DllHandle
= dh_buf
;
1131 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1136 if (!ok
|| !cbNeeded
)
1139 DllHandle
= (HMODULE
*) alloca (cbNeeded
);
1143 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1150 for (i
= 1; i
< ((size_t) cbNeeded
/ sizeof (HMODULE
)); i
++)
1153 char dll_name
[MAX_PATH
];
1155 if (!(*win32_GetModuleInformation
) (current_process_handle
,
1160 if ((*win32_GetModuleFileNameExA
) (current_process_handle
,
1165 win32_add_one_solib (dll_name
, (CORE_ADDR
) (uintptr_t) mi
.lpBaseOfDll
);
1170 typedef HANDLE (WINAPI
*winapi_CreateToolhelp32Snapshot
) (DWORD
, DWORD
);
1171 typedef BOOL (WINAPI
*winapi_Module32First
) (HANDLE
, LPMODULEENTRY32
);
1172 typedef BOOL (WINAPI
*winapi_Module32Next
) (HANDLE
, LPMODULEENTRY32
);
1174 /* Handle a DLL load event.
1176 This function assumes that this event did not occur during inferior
1177 initialization, where their event info may be incomplete (see
1178 do_initial_child_stuff and win32_add_all_dlls for more info on
1179 how we handle DLL loading during that phase). */
1182 handle_load_dll (void)
1184 LOAD_DLL_DEBUG_INFO
*event
= ¤t_event
.u
.LoadDll
;
1187 dll_name
= get_image_name (current_process_handle
,
1188 event
->lpImageName
, event
->fUnicode
);
1192 win32_add_one_solib (dll_name
, (CORE_ADDR
) (uintptr_t) event
->lpBaseOfDll
);
1195 /* Handle a DLL unload 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_one_solib for more info
1200 on how we handle DLL loading during that phase). */
1203 handle_unload_dll (void)
1205 CORE_ADDR load_addr
=
1206 (CORE_ADDR
) (uintptr_t) current_event
.u
.UnloadDll
.lpBaseOfDll
;
1208 /* The symbols in a dll are offset by 0x1000, which is the
1209 offset from 0 of the first byte in an image - because
1210 of the file header and the section alignment. */
1211 load_addr
+= 0x1000;
1212 unloaded_dll (NULL
, load_addr
);
1216 handle_exception (struct target_waitstatus
*ourstatus
)
1218 DWORD code
= current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
;
1220 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
1224 case EXCEPTION_ACCESS_VIOLATION
:
1225 OUTMSG2 (("EXCEPTION_ACCESS_VIOLATION"));
1226 ourstatus
->value
.sig
= GDB_SIGNAL_SEGV
;
1228 case STATUS_STACK_OVERFLOW
:
1229 OUTMSG2 (("STATUS_STACK_OVERFLOW"));
1230 ourstatus
->value
.sig
= GDB_SIGNAL_SEGV
;
1232 case STATUS_FLOAT_DENORMAL_OPERAND
:
1233 OUTMSG2 (("STATUS_FLOAT_DENORMAL_OPERAND"));
1234 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1236 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
1237 OUTMSG2 (("EXCEPTION_ARRAY_BOUNDS_EXCEEDED"));
1238 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1240 case STATUS_FLOAT_INEXACT_RESULT
:
1241 OUTMSG2 (("STATUS_FLOAT_INEXACT_RESULT"));
1242 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1244 case STATUS_FLOAT_INVALID_OPERATION
:
1245 OUTMSG2 (("STATUS_FLOAT_INVALID_OPERATION"));
1246 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1248 case STATUS_FLOAT_OVERFLOW
:
1249 OUTMSG2 (("STATUS_FLOAT_OVERFLOW"));
1250 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1252 case STATUS_FLOAT_STACK_CHECK
:
1253 OUTMSG2 (("STATUS_FLOAT_STACK_CHECK"));
1254 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1256 case STATUS_FLOAT_UNDERFLOW
:
1257 OUTMSG2 (("STATUS_FLOAT_UNDERFLOW"));
1258 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1260 case STATUS_FLOAT_DIVIDE_BY_ZERO
:
1261 OUTMSG2 (("STATUS_FLOAT_DIVIDE_BY_ZERO"));
1262 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1264 case STATUS_INTEGER_DIVIDE_BY_ZERO
:
1265 OUTMSG2 (("STATUS_INTEGER_DIVIDE_BY_ZERO"));
1266 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1268 case STATUS_INTEGER_OVERFLOW
:
1269 OUTMSG2 (("STATUS_INTEGER_OVERFLOW"));
1270 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1272 case EXCEPTION_BREAKPOINT
:
1273 OUTMSG2 (("EXCEPTION_BREAKPOINT"));
1274 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1276 /* Remove the initial breakpoint. */
1277 check_breakpoints ((CORE_ADDR
) (long) current_event
1278 .u
.Exception
.ExceptionRecord
.ExceptionAddress
);
1282 OUTMSG2 (("DBG_CONTROL_C"));
1283 ourstatus
->value
.sig
= GDB_SIGNAL_INT
;
1285 case DBG_CONTROL_BREAK
:
1286 OUTMSG2 (("DBG_CONTROL_BREAK"));
1287 ourstatus
->value
.sig
= GDB_SIGNAL_INT
;
1289 case EXCEPTION_SINGLE_STEP
:
1290 OUTMSG2 (("EXCEPTION_SINGLE_STEP"));
1291 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1293 case EXCEPTION_ILLEGAL_INSTRUCTION
:
1294 OUTMSG2 (("EXCEPTION_ILLEGAL_INSTRUCTION"));
1295 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1297 case EXCEPTION_PRIV_INSTRUCTION
:
1298 OUTMSG2 (("EXCEPTION_PRIV_INSTRUCTION"));
1299 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1301 case EXCEPTION_NONCONTINUABLE_EXCEPTION
:
1302 OUTMSG2 (("EXCEPTION_NONCONTINUABLE_EXCEPTION"));
1303 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1306 if (current_event
.u
.Exception
.dwFirstChance
)
1308 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1311 OUTMSG2 (("gdbserver: unknown target exception 0x%08x at 0x%s",
1312 (unsigned) current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
,
1313 phex_nz ((uintptr_t) current_event
.u
.Exception
.ExceptionRecord
.
1314 ExceptionAddress
, sizeof (uintptr_t))));
1315 ourstatus
->value
.sig
= GDB_SIGNAL_UNKNOWN
;
1319 last_sig
= ourstatus
->value
.sig
;
1324 suspend_one_thread (struct inferior_list_entry
*entry
)
1326 struct thread_info
*thread
= (struct thread_info
*) entry
;
1327 win32_thread_info
*th
= inferior_target_data (thread
);
1331 if (SuspendThread (th
->h
) == (DWORD
) -1)
1333 DWORD err
= GetLastError ();
1334 OUTMSG (("warning: SuspendThread failed in suspend_one_thread, "
1335 "(error %d): %s\n", (int) err
, strwinerror (err
)));
1343 fake_breakpoint_event (void)
1345 OUTMSG2(("fake_breakpoint_event\n"));
1347 faked_breakpoint
= 1;
1349 memset (¤t_event
, 0, sizeof (current_event
));
1350 current_event
.dwThreadId
= main_thread_id
;
1351 current_event
.dwDebugEventCode
= EXCEPTION_DEBUG_EVENT
;
1352 current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
1353 = EXCEPTION_BREAKPOINT
;
1355 for_each_inferior (&all_threads
, suspend_one_thread
);
1360 auto_delete_breakpoint (CORE_ADDR stop_pc
)
1366 /* Get the next event from the child. */
1369 get_child_debug_event (struct target_waitstatus
*ourstatus
)
1373 last_sig
= GDB_SIGNAL_0
;
1374 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1376 /* Check if GDB sent us an interrupt request. */
1377 check_remote_input_interrupt_request ();
1379 if (soft_interrupt_requested
)
1381 soft_interrupt_requested
= 0;
1382 fake_breakpoint_event ();
1391 /* WinCE doesn't set an initial breakpoint automatically. To
1392 stop the inferior, we flush all currently pending debug
1393 events -- the thread list and the dll list are always
1394 reported immediatelly without delay, then, we suspend all
1395 threads and pretend we saw a trap at the current PC of the
1398 Contrary to desktop Windows, Windows CE *does* report the dll
1399 names on LOAD_DLL_DEBUG_EVENTs resulting from a
1400 DebugActiveProcess call. This limits the way we can detect
1401 if all the dlls have already been reported. If we get a real
1402 debug event before leaving attaching, the worst that will
1403 happen is the user will see a spurious breakpoint. */
1405 current_event
.dwDebugEventCode
= 0;
1406 if (!WaitForDebugEvent (¤t_event
, 0))
1408 OUTMSG2(("no attach events left\n"));
1409 fake_breakpoint_event ();
1413 OUTMSG2(("got attach event\n"));
1418 /* Keep the wait time low enough for confortable remote
1419 interruption, but high enough so gdbserver doesn't become a
1421 if (!WaitForDebugEvent (¤t_event
, 250))
1423 DWORD e
= GetLastError();
1425 if (e
== ERROR_PIPE_NOT_CONNECTED
)
1427 /* This will happen if the loader fails to succesfully
1428 load the application, e.g., if the main executable
1429 tries to pull in a non-existing export from a
1431 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1432 ourstatus
->value
.integer
= 1;
1442 switch (current_event
.dwDebugEventCode
)
1444 case CREATE_THREAD_DEBUG_EVENT
:
1445 OUTMSG2 (("gdbserver: kernel event CREATE_THREAD_DEBUG_EVENT "
1446 "for pid=%u tid=%x)\n",
1447 (unsigned) current_event
.dwProcessId
,
1448 (unsigned) current_event
.dwThreadId
));
1450 /* Record the existence of this thread. */
1451 child_add_thread (current_event
.dwProcessId
,
1452 current_event
.dwThreadId
,
1453 current_event
.u
.CreateThread
.hThread
,
1454 current_event
.u
.CreateThread
.lpThreadLocalBase
);
1457 case EXIT_THREAD_DEBUG_EVENT
:
1458 OUTMSG2 (("gdbserver: kernel event EXIT_THREAD_DEBUG_EVENT "
1459 "for pid=%u tid=%x\n",
1460 (unsigned) current_event
.dwProcessId
,
1461 (unsigned) current_event
.dwThreadId
));
1462 child_delete_thread (current_event
.dwProcessId
,
1463 current_event
.dwThreadId
);
1465 current_inferior
= (struct thread_info
*) all_threads
.head
;
1468 case CREATE_PROCESS_DEBUG_EVENT
:
1469 OUTMSG2 (("gdbserver: kernel event CREATE_PROCESS_DEBUG_EVENT "
1470 "for pid=%u tid=%x\n",
1471 (unsigned) current_event
.dwProcessId
,
1472 (unsigned) current_event
.dwThreadId
));
1473 CloseHandle (current_event
.u
.CreateProcessInfo
.hFile
);
1475 current_process_handle
= current_event
.u
.CreateProcessInfo
.hProcess
;
1476 main_thread_id
= current_event
.dwThreadId
;
1478 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
1479 ourstatus
->value
.execd_pathname
= "Main executable";
1481 /* Add the main thread. */
1482 child_add_thread (current_event
.dwProcessId
,
1484 current_event
.u
.CreateProcessInfo
.hThread
,
1485 current_event
.u
.CreateProcessInfo
.lpThreadLocalBase
);
1487 ourstatus
->value
.related_pid
= debug_event_ptid (¤t_event
);
1491 /* Windows CE doesn't set the initial breakpoint
1492 automatically like the desktop versions of Windows do.
1493 We add it explicitly here. It will be removed as soon as
1495 set_breakpoint_at ((CORE_ADDR
) (long) current_event
.u
1496 .CreateProcessInfo
.lpStartAddress
,
1497 auto_delete_breakpoint
);
1502 case EXIT_PROCESS_DEBUG_EVENT
:
1503 OUTMSG2 (("gdbserver: kernel event EXIT_PROCESS_DEBUG_EVENT "
1504 "for pid=%u tid=%x\n",
1505 (unsigned) current_event
.dwProcessId
,
1506 (unsigned) current_event
.dwThreadId
));
1507 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1508 ourstatus
->value
.integer
= current_event
.u
.ExitProcess
.dwExitCode
;
1509 child_continue (DBG_CONTINUE
, -1);
1510 CloseHandle (current_process_handle
);
1511 current_process_handle
= NULL
;
1514 case LOAD_DLL_DEBUG_EVENT
:
1515 OUTMSG2 (("gdbserver: kernel event LOAD_DLL_DEBUG_EVENT "
1516 "for pid=%u tid=%x\n",
1517 (unsigned) current_event
.dwProcessId
,
1518 (unsigned) current_event
.dwThreadId
));
1519 CloseHandle (current_event
.u
.LoadDll
.hFile
);
1520 if (! child_initialization_done
)
1524 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1525 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1528 case UNLOAD_DLL_DEBUG_EVENT
:
1529 OUTMSG2 (("gdbserver: kernel event UNLOAD_DLL_DEBUG_EVENT "
1530 "for pid=%u tid=%x\n",
1531 (unsigned) current_event
.dwProcessId
,
1532 (unsigned) current_event
.dwThreadId
));
1533 if (! child_initialization_done
)
1535 handle_unload_dll ();
1536 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1537 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1540 case EXCEPTION_DEBUG_EVENT
:
1541 OUTMSG2 (("gdbserver: kernel event EXCEPTION_DEBUG_EVENT "
1542 "for pid=%u tid=%x\n",
1543 (unsigned) current_event
.dwProcessId
,
1544 (unsigned) current_event
.dwThreadId
));
1545 handle_exception (ourstatus
);
1548 case OUTPUT_DEBUG_STRING_EVENT
:
1549 /* A message from the kernel (or Cygwin). */
1550 OUTMSG2 (("gdbserver: kernel event OUTPUT_DEBUG_STRING_EVENT "
1551 "for pid=%u tid=%x\n",
1552 (unsigned) current_event
.dwProcessId
,
1553 (unsigned) current_event
.dwThreadId
));
1554 handle_output_debug_string (ourstatus
);
1558 OUTMSG2 (("gdbserver: kernel event unknown "
1559 "for pid=%u tid=%x code=%x\n",
1560 (unsigned) current_event
.dwProcessId
,
1561 (unsigned) current_event
.dwThreadId
,
1562 (unsigned) current_event
.dwDebugEventCode
));
1566 ptid
= debug_event_ptid (¤t_event
);
1568 (struct thread_info
*) find_inferior_id (&all_threads
, ptid
);
1572 /* Wait for the inferior process to change state.
1573 STATUS will be filled in with a response code to send to GDB.
1574 Returns the signal which caused the process to stop. */
1576 win32_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
, int options
)
1578 struct regcache
*regcache
;
1580 if (cached_status
.kind
!= TARGET_WAITKIND_IGNORE
)
1582 /* The core always does a wait after creating the inferior, and
1583 do_initial_child_stuff already ran the inferior to the
1584 initial breakpoint (or an exit, if creating the process
1585 fails). Report it now. */
1586 *ourstatus
= cached_status
;
1587 cached_status
.kind
= TARGET_WAITKIND_IGNORE
;
1588 return debug_event_ptid (¤t_event
);
1593 if (!get_child_debug_event (ourstatus
))
1596 switch (ourstatus
->kind
)
1598 case TARGET_WAITKIND_EXITED
:
1599 OUTMSG2 (("Child exited with retcode = %x\n",
1600 ourstatus
->value
.integer
));
1601 win32_clear_inferiors ();
1602 return pid_to_ptid (current_event
.dwProcessId
);
1603 case TARGET_WAITKIND_STOPPED
:
1604 case TARGET_WAITKIND_LOADED
:
1605 OUTMSG2 (("Child Stopped with signal = %d \n",
1606 ourstatus
->value
.sig
));
1608 regcache
= get_thread_regcache (current_inferior
, 1);
1609 child_fetch_inferior_registers (regcache
, -1);
1610 return debug_event_ptid (¤t_event
);
1612 OUTMSG (("Ignoring unknown internal event, %d\n", ourstatus
->kind
));
1614 case TARGET_WAITKIND_SPURIOUS
:
1615 case TARGET_WAITKIND_EXECD
:
1616 /* do nothing, just continue */
1617 child_continue (DBG_CONTINUE
, -1);
1623 /* Fetch registers from the inferior process.
1624 If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO. */
1626 win32_fetch_inferior_registers (struct regcache
*regcache
, int regno
)
1628 child_fetch_inferior_registers (regcache
, regno
);
1631 /* Store registers to the inferior process.
1632 If REGNO is -1, store all registers; otherwise, store at least REGNO. */
1634 win32_store_inferior_registers (struct regcache
*regcache
, int regno
)
1636 child_store_inferior_registers (regcache
, regno
);
1639 /* Read memory from the inferior process. This should generally be
1640 called through read_inferior_memory, which handles breakpoint shadowing.
1641 Read LEN bytes at MEMADDR into a buffer at MYADDR. */
1643 win32_read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
1645 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 0, 0) != len
;
1648 /* Write memory to the inferior process. This should generally be
1649 called through write_inferior_memory, which handles breakpoint shadowing.
1650 Write LEN bytes from the buffer at MYADDR to MEMADDR.
1651 Returns 0 on success and errno on failure. */
1653 win32_write_inferior_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
,
1656 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 1, 0) != len
;
1659 /* Send an interrupt request to the inferior process. */
1661 win32_request_interrupt (void)
1663 winapi_DebugBreakProcess DebugBreakProcess
;
1664 winapi_GenerateConsoleCtrlEvent GenerateConsoleCtrlEvent
;
1667 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
1669 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
1672 GenerateConsoleCtrlEvent
= GETPROCADDRESS (dll
, GenerateConsoleCtrlEvent
);
1674 if (GenerateConsoleCtrlEvent
!= NULL
1675 && GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT
, current_process_id
))
1678 /* GenerateConsoleCtrlEvent can fail if process id being debugged is
1679 not a process group id.
1680 Fallback to XP/Vista 'DebugBreakProcess', which generates a
1681 breakpoint exception in the interior process. */
1683 DebugBreakProcess
= GETPROCADDRESS (dll
, DebugBreakProcess
);
1685 if (DebugBreakProcess
!= NULL
1686 && DebugBreakProcess (current_process_handle
))
1689 /* Last resort, suspend all threads manually. */
1690 soft_interrupt_requested
= 1;
1695 win32_error_to_fileio_error (DWORD err
)
1699 case ERROR_BAD_PATHNAME
:
1700 case ERROR_FILE_NOT_FOUND
:
1701 case ERROR_INVALID_NAME
:
1702 case ERROR_PATH_NOT_FOUND
:
1703 return FILEIO_ENOENT
;
1705 case ERROR_IO_DEVICE
:
1706 case ERROR_OPEN_FAILED
:
1708 case ERROR_INVALID_HANDLE
:
1709 return FILEIO_EBADF
;
1710 case ERROR_ACCESS_DENIED
:
1711 case ERROR_SHARING_VIOLATION
:
1712 return FILEIO_EACCES
;
1713 case ERROR_NOACCESS
:
1714 return FILEIO_EFAULT
;
1716 return FILEIO_EBUSY
;
1717 case ERROR_ALREADY_EXISTS
:
1718 case ERROR_FILE_EXISTS
:
1719 return FILEIO_EEXIST
;
1720 case ERROR_BAD_DEVICE
:
1721 return FILEIO_ENODEV
;
1722 case ERROR_DIRECTORY
:
1723 return FILEIO_ENOTDIR
;
1724 case ERROR_FILENAME_EXCED_RANGE
:
1725 case ERROR_INVALID_DATA
:
1726 case ERROR_INVALID_PARAMETER
:
1727 case ERROR_NEGATIVE_SEEK
:
1728 return FILEIO_EINVAL
;
1729 case ERROR_TOO_MANY_OPEN_FILES
:
1730 return FILEIO_EMFILE
;
1731 case ERROR_HANDLE_DISK_FULL
:
1732 case ERROR_DISK_FULL
:
1733 return FILEIO_ENOSPC
;
1734 case ERROR_WRITE_PROTECT
:
1735 return FILEIO_EROFS
;
1736 case ERROR_NOT_SUPPORTED
:
1737 return FILEIO_ENOSYS
;
1740 return FILEIO_EUNKNOWN
;
1744 wince_hostio_last_error (char *buf
)
1746 DWORD winerr
= GetLastError ();
1747 int fileio_err
= win32_error_to_fileio_error (winerr
);
1748 sprintf (buf
, "F-1,%x", fileio_err
);
1752 /* Write Windows OS Thread Information Block address. */
1755 win32_get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
)
1757 win32_thread_info
*th
;
1758 th
= thread_rec (ptid
, 0);
1762 *addr
= th
->thread_local_base
;
1766 static struct target_ops win32_target_ops
= {
1767 win32_create_inferior
,
1776 win32_fetch_inferior_registers
,
1777 win32_store_inferior_registers
,
1778 NULL
, /* prepare_to_access_memory */
1779 NULL
, /* done_accessing_memory */
1780 win32_read_inferior_memory
,
1781 win32_write_inferior_memory
,
1782 NULL
, /* lookup_symbols */
1783 win32_request_interrupt
,
1784 NULL
, /* read_auxv */
1785 win32_supports_z_point_type
,
1788 win32_stopped_by_watchpoint
,
1789 win32_stopped_data_address
,
1790 NULL
, /* read_offsets */
1791 NULL
, /* get_tls_address */
1792 NULL
, /* qxfer_spu */
1794 wince_hostio_last_error
,
1796 hostio_last_error_from_errno
,
1798 NULL
, /* qxfer_osdata */
1799 NULL
, /* qxfer_siginfo */
1800 NULL
, /* supports_non_stop */
1802 NULL
, /* start_non_stop */
1803 NULL
, /* supports_multi_process */
1804 NULL
, /* handle_monitor_command */
1805 NULL
, /* core_of_thread */
1806 NULL
, /* read_loadmap */
1807 NULL
, /* process_qsupported */
1808 NULL
, /* supports_tracepoints */
1810 NULL
, /* write_pc */
1811 NULL
, /* thread_stopped */
1812 win32_get_tib_address
1815 /* Initialize the Win32 backend. */
1817 initialize_low (void)
1819 set_target_ops (&win32_target_ops
);
1820 if (the_low_target
.breakpoint
!= NULL
)
1821 set_breakpoint_data (the_low_target
.breakpoint
,
1822 the_low_target
.breakpoint_len
);
1823 the_low_target
.arch_setup ();