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/fileio.h"
24 #include "mem-break.h"
25 #include "win32-low.h"
26 #include "gdbthread.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
, ¤t_event
);
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 (*the_low_target
.set_thread_context
) (th
, ¤t_event
);
159 /* Find a thread record given a thread id. If GET_CONTEXT is set then
160 also retrieve the context for this thread. */
161 static win32_thread_info
*
162 thread_rec (ptid_t ptid
, int get_context
)
164 struct thread_info
*thread
;
165 win32_thread_info
*th
;
167 thread
= (struct thread_info
*) find_inferior_id (&all_threads
, ptid
);
171 th
= inferior_target_data (thread
);
172 if (get_context
&& th
->context
.ContextFlags
== 0)
176 if (SuspendThread (th
->h
) == (DWORD
) -1)
178 DWORD err
= GetLastError ();
179 OUTMSG (("warning: SuspendThread failed in thread_rec, "
180 "(error %d): %s\n", (int) err
, strwinerror (err
)));
186 win32_get_thread_context (th
);
192 /* Add a thread to the thread list. */
193 static win32_thread_info
*
194 child_add_thread (DWORD pid
, DWORD tid
, HANDLE h
, void *tlb
)
196 win32_thread_info
*th
;
197 ptid_t ptid
= ptid_build (pid
, tid
, 0);
199 if ((th
= thread_rec (ptid
, FALSE
)))
202 th
= xcalloc (1, sizeof (*th
));
205 th
->thread_local_base
= (CORE_ADDR
) (uintptr_t) tlb
;
207 add_thread (ptid
, th
);
209 if (the_low_target
.thread_added
!= NULL
)
210 (*the_low_target
.thread_added
) (th
);
215 /* Delete a thread from the list of threads. */
217 delete_thread_info (struct inferior_list_entry
*thread
)
219 win32_thread_info
*th
= inferior_target_data ((struct thread_info
*) thread
);
221 remove_thread ((struct thread_info
*) thread
);
226 /* Delete a thread from the list of threads. */
228 child_delete_thread (DWORD pid
, DWORD tid
)
230 struct inferior_list_entry
*thread
;
233 /* If the last thread is exiting, just return. */
234 if (one_inferior_p (&all_threads
))
237 ptid
= ptid_build (pid
, tid
, 0);
238 thread
= find_inferior_id (&all_threads
, ptid
);
242 delete_thread_info (thread
);
245 /* These watchpoint related wrapper functions simply pass on the function call
246 if the low target has registered a corresponding function. */
249 win32_supports_z_point_type (char z_type
)
251 return (the_low_target
.supports_z_point_type
!= NULL
252 && the_low_target
.supports_z_point_type (z_type
));
256 win32_insert_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
257 int size
, struct raw_breakpoint
*bp
)
259 if (the_low_target
.insert_point
!= NULL
)
260 return the_low_target
.insert_point (type
, addr
, size
, bp
);
262 /* Unsupported (see target.h). */
267 win32_remove_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
268 int size
, struct raw_breakpoint
*bp
)
270 if (the_low_target
.remove_point
!= NULL
)
271 return the_low_target
.remove_point (type
, addr
, size
, bp
);
273 /* Unsupported (see target.h). */
278 win32_stopped_by_watchpoint (void)
280 if (the_low_target
.stopped_by_watchpoint
!= NULL
)
281 return the_low_target
.stopped_by_watchpoint ();
287 win32_stopped_data_address (void)
289 if (the_low_target
.stopped_data_address
!= NULL
)
290 return the_low_target
.stopped_data_address ();
296 /* Transfer memory from/to the debugged process. */
298 child_xfer_memory (CORE_ADDR memaddr
, char *our
, int len
,
299 int write
, struct target_ops
*target
)
304 uintptr_t addr
= (uintptr_t) memaddr
;
308 success
= WriteProcessMemory (current_process_handle
, (LPVOID
) addr
,
309 (LPCVOID
) our
, len
, &done
);
311 lasterror
= GetLastError ();
312 FlushInstructionCache (current_process_handle
, (LPCVOID
) addr
, len
);
316 success
= ReadProcessMemory (current_process_handle
, (LPCVOID
) addr
,
317 (LPVOID
) our
, len
, &done
);
319 lasterror
= GetLastError ();
321 if (!success
&& lasterror
== ERROR_PARTIAL_COPY
&& done
> 0)
324 return success
? done
: -1;
327 /* Clear out any old thread list and reinitialize it to a pristine
330 child_init_thread_list (void)
332 for_each_inferior (&all_threads
, delete_thread_info
);
335 /* Zero during the child initialization phase, and nonzero otherwise. */
337 static int child_initialization_done
= 0;
340 do_initial_child_stuff (HANDLE proch
, DWORD pid
, int attached
)
342 struct process_info
*proc
;
344 last_sig
= GDB_SIGNAL_0
;
346 current_process_handle
= proch
;
347 current_process_id
= pid
;
350 soft_interrupt_requested
= 0;
351 faked_breakpoint
= 0;
353 memset (¤t_event
, 0, sizeof (current_event
));
355 proc
= add_process (pid
, attached
);
356 proc
->tdesc
= win32_tdesc
;
357 child_init_thread_list ();
358 child_initialization_done
= 0;
360 if (the_low_target
.initial_stuff
!= NULL
)
361 (*the_low_target
.initial_stuff
) ();
363 cached_status
.kind
= TARGET_WAITKIND_IGNORE
;
365 /* Flush all currently pending debug events (thread and dll list) up
366 to the initial breakpoint. */
369 struct target_waitstatus status
;
371 win32_wait (minus_one_ptid
, &status
, 0);
373 /* Note win32_wait doesn't return thread events. */
374 if (status
.kind
!= TARGET_WAITKIND_LOADED
)
376 cached_status
= status
;
381 struct thread_resume resume
;
383 resume
.thread
= minus_one_ptid
;
384 resume
.kind
= resume_continue
;
387 win32_resume (&resume
, 1);
392 /* Now that the inferior has been started and all DLLs have been mapped,
393 we can iterate over all DLLs and load them in.
395 We avoid doing it any earlier because, on certain versions of Windows,
396 LOAD_DLL_DEBUG_EVENTs are sometimes not complete. In particular,
397 we have seen on Windows 8.1 that the ntdll.dll load event does not
398 include the DLL name, preventing us from creating an associated SO.
399 A possible explanation is that ntdll.dll might be mapped before
400 the SO info gets created by the Windows system -- ntdll.dll is
401 the first DLL to be reported via LOAD_DLL_DEBUG_EVENT and other DLLs
402 do not seem to suffer from that problem.
404 Rather than try to work around this sort of issue, it is much
405 simpler to just ignore DLL load/unload events during the startup
406 phase, and then process them all in one batch now. */
407 win32_add_all_dlls ();
410 child_initialization_done
= 1;
413 /* Resume all artificially suspended threads if we are continuing
416 continue_one_thread (struct inferior_list_entry
*this_thread
, void *id_ptr
)
418 struct thread_info
*thread
= (struct thread_info
*) this_thread
;
419 int thread_id
= * (int *) id_ptr
;
420 win32_thread_info
*th
= inferior_target_data (thread
);
422 if ((thread_id
== -1 || thread_id
== th
->tid
)
425 if (th
->context
.ContextFlags
)
427 win32_set_thread_context (th
);
428 th
->context
.ContextFlags
= 0;
431 if (ResumeThread (th
->h
) == (DWORD
) -1)
433 DWORD err
= GetLastError ();
434 OUTMSG (("warning: ResumeThread failed in continue_one_thread, "
435 "(error %d): %s\n", (int) err
, strwinerror (err
)));
444 child_continue (DWORD continue_status
, int thread_id
)
446 /* The inferior will only continue after the ContinueDebugEvent
448 find_inferior (&all_threads
, continue_one_thread
, &thread_id
);
449 faked_breakpoint
= 0;
451 if (!ContinueDebugEvent (current_event
.dwProcessId
,
452 current_event
.dwThreadId
,
459 /* Fetch register(s) from the current thread context. */
461 child_fetch_inferior_registers (struct regcache
*regcache
, int r
)
464 win32_thread_info
*th
= thread_rec (current_thread_ptid (), TRUE
);
465 if (r
== -1 || r
> NUM_REGS
)
466 child_fetch_inferior_registers (regcache
, NUM_REGS
);
468 for (regno
= 0; regno
< r
; regno
++)
469 (*the_low_target
.fetch_inferior_register
) (regcache
, th
, regno
);
472 /* Store a new register value into the current thread context. We don't
473 change the program's context until later, when we resume it. */
475 child_store_inferior_registers (struct regcache
*regcache
, int r
)
478 win32_thread_info
*th
= thread_rec (current_thread_ptid (), TRUE
);
479 if (r
== -1 || r
== 0 || r
> NUM_REGS
)
480 child_store_inferior_registers (regcache
, NUM_REGS
);
482 for (regno
= 0; regno
< r
; regno
++)
483 (*the_low_target
.store_inferior_register
) (regcache
, th
, regno
);
486 /* Map the Windows error number in ERROR to a locale-dependent error
487 message string and return a pointer to it. Typically, the values
488 for ERROR come from GetLastError.
490 The string pointed to shall not be modified by the application,
491 but may be overwritten by a subsequent call to strwinerror
493 The strwinerror function does not change the current setting
497 strwinerror (DWORD error
)
499 static char buf
[1024];
501 DWORD lasterr
= GetLastError ();
502 DWORD chars
= FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
503 | FORMAT_MESSAGE_ALLOCATE_BUFFER
,
506 0, /* Default language */
512 /* If there is an \r\n appended, zap it. */
514 && msgbuf
[chars
- 2] == '\r'
515 && msgbuf
[chars
- 1] == '\n')
521 if (chars
> ((COUNTOF (buf
)) - 1))
523 chars
= COUNTOF (buf
) - 1;
528 wcstombs (buf
, msgbuf
, chars
+ 1);
530 strncpy (buf
, msgbuf
, chars
+ 1);
535 sprintf (buf
, "unknown win32 error (%u)", (unsigned) error
);
537 SetLastError (lasterr
);
542 create_process (const char *program
, char *args
,
543 DWORD flags
, PROCESS_INFORMATION
*pi
)
548 wchar_t *p
, *wprogram
, *wargs
;
551 wprogram
= alloca ((strlen (program
) + 1) * sizeof (wchar_t));
552 mbstowcs (wprogram
, program
, strlen (program
) + 1);
554 for (p
= wprogram
; *p
; ++p
)
558 argslen
= strlen (args
);
559 wargs
= alloca ((argslen
+ 1) * sizeof (wchar_t));
560 mbstowcs (wargs
, args
, argslen
+ 1);
562 ret
= CreateProcessW (wprogram
, /* image name */
563 wargs
, /* command line */
564 NULL
, /* security, not supported */
565 NULL
, /* thread, not supported */
566 FALSE
, /* inherit handles, not supported */
567 flags
, /* start flags */
568 NULL
, /* environment, not supported */
569 NULL
, /* current directory, not supported */
570 NULL
, /* start info, not supported */
573 STARTUPINFOA si
= { sizeof (STARTUPINFOA
) };
575 ret
= CreateProcessA (program
, /* image name */
576 args
, /* command line */
579 TRUE
, /* inherit handles */
580 flags
, /* start flags */
581 NULL
, /* environment */
582 NULL
, /* current directory */
583 &si
, /* start info */
590 /* Start a new process.
591 PROGRAM is a path to the program to execute.
592 ARGS is a standard NULL-terminated array of arguments,
593 to be passed to the inferior as ``argv''.
594 Returns the new PID on success, -1 on failure. Registers the new
595 process with the process list. */
597 win32_create_inferior (char *program
, char **program_args
)
600 char real_path
[PATH_MAX
];
601 char *orig_path
, *new_path
, *path_ptr
;
608 PROCESS_INFORMATION pi
;
611 /* win32_wait needs to know we're not attaching. */
615 error ("No executable specified, specify executable to debug.\n");
617 flags
= DEBUG_PROCESS
| DEBUG_ONLY_THIS_PROCESS
;
621 path_ptr
= getenv ("PATH");
624 int size
= cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, NULL
, 0);
625 orig_path
= alloca (strlen (path_ptr
) + 1);
626 new_path
= alloca (size
);
627 strcpy (orig_path
, path_ptr
);
628 cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, new_path
, size
);
629 setenv ("PATH", new_path
, 1);
631 cygwin_conv_path (CCP_POSIX_TO_WIN_A
, program
, real_path
, PATH_MAX
);
636 for (argc
= 1; program_args
[argc
]; argc
++)
637 argslen
+= strlen (program_args
[argc
]) + 1;
638 args
= alloca (argslen
);
640 for (argc
= 1; program_args
[argc
]; argc
++)
642 /* FIXME: Can we do better about quoting? How does Cygwin
645 strcat (args
, program_args
[argc
]);
647 OUTMSG2 (("Command line is \"%s\"\n", args
));
649 #ifdef CREATE_NEW_PROCESS_GROUP
650 flags
|= CREATE_NEW_PROCESS_GROUP
;
653 ret
= create_process (program
, args
, flags
, &pi
);
654 err
= GetLastError ();
655 if (!ret
&& err
== ERROR_FILE_NOT_FOUND
)
657 char *exename
= alloca (strlen (program
) + 5);
658 strcat (strcpy (exename
, program
), ".exe");
659 ret
= create_process (exename
, args
, flags
, &pi
);
660 err
= GetLastError ();
665 setenv ("PATH", orig_path
, 1);
670 error ("Error creating process \"%s%s\", (error %d): %s\n",
671 program
, args
, (int) err
, strwinerror (err
));
675 OUTMSG2 (("Process created: %s\n", (char *) args
));
679 /* On Windows CE this handle can't be closed. The OS reuses
680 it in the debug events, while the 9x/NT versions of Windows
681 probably use a DuplicateHandle'd one. */
682 CloseHandle (pi
.hThread
);
685 do_initial_child_stuff (pi
.hProcess
, pi
.dwProcessId
, 0);
687 return current_process_id
;
690 /* Attach to a running process.
691 PID is the process ID to attach to, specified by the user
692 or a higher layer. */
694 win32_attach (unsigned long pid
)
697 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
700 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
702 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
704 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
706 h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
709 if (DebugActiveProcess (pid
))
711 if (DebugSetProcessKillOnExit
!= NULL
)
712 DebugSetProcessKillOnExit (FALSE
);
714 /* win32_wait needs to know we're attaching. */
716 do_initial_child_stuff (h
, pid
, 1);
723 err
= GetLastError ();
724 error ("Attach to process failed (error %d): %s\n",
725 (int) err
, strwinerror (err
));
728 /* Handle OUTPUT_DEBUG_STRING_EVENT from child process. */
730 handle_output_debug_string (struct target_waitstatus
*ourstatus
)
732 #define READ_BUFFER_LEN 1024
734 char s
[READ_BUFFER_LEN
+ 1] = { 0 };
735 DWORD nbytes
= current_event
.u
.DebugString
.nDebugStringLength
;
740 if (nbytes
> READ_BUFFER_LEN
)
741 nbytes
= READ_BUFFER_LEN
;
743 addr
= (CORE_ADDR
) (size_t) current_event
.u
.DebugString
.lpDebugStringData
;
745 if (current_event
.u
.DebugString
.fUnicode
)
747 /* The event tells us how many bytes, not chars, even
749 WCHAR buffer
[(READ_BUFFER_LEN
+ 1) / sizeof (WCHAR
)] = { 0 };
750 if (read_inferior_memory (addr
, (unsigned char *) buffer
, nbytes
) != 0)
752 wcstombs (s
, buffer
, (nbytes
+ 1) / sizeof (WCHAR
));
756 if (read_inferior_memory (addr
, (unsigned char *) s
, nbytes
) != 0)
760 if (strncmp (s
, "cYg", 3) != 0)
770 #undef READ_BUFFER_LEN
774 win32_clear_inferiors (void)
776 if (current_process_handle
!= NULL
)
777 CloseHandle (current_process_handle
);
779 for_each_inferior (&all_threads
, delete_thread_info
);
783 /* Kill all inferiors. */
787 struct process_info
*process
;
789 if (current_process_handle
== NULL
)
792 TerminateProcess (current_process_handle
, 0);
795 if (!child_continue (DBG_CONTINUE
, -1))
797 if (!WaitForDebugEvent (¤t_event
, INFINITE
))
799 if (current_event
.dwDebugEventCode
== EXIT_PROCESS_DEBUG_EVENT
)
801 else if (current_event
.dwDebugEventCode
== OUTPUT_DEBUG_STRING_EVENT
)
803 struct target_waitstatus our_status
= { 0 };
804 handle_output_debug_string (&our_status
);
808 win32_clear_inferiors ();
810 process
= find_process_pid (pid
);
811 remove_process (process
);
815 /* Detach from inferior PID. */
817 win32_detach (int pid
)
819 struct process_info
*process
;
820 winapi_DebugActiveProcessStop DebugActiveProcessStop
= NULL
;
821 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
823 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
825 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
827 DebugActiveProcessStop
= GETPROCADDRESS (dll
, DebugActiveProcessStop
);
828 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
830 if (DebugSetProcessKillOnExit
== NULL
831 || DebugActiveProcessStop
== NULL
)
835 struct thread_resume resume
;
836 resume
.thread
= minus_one_ptid
;
837 resume
.kind
= resume_continue
;
839 win32_resume (&resume
, 1);
842 if (!DebugActiveProcessStop (current_process_id
))
845 DebugSetProcessKillOnExit (FALSE
);
846 process
= find_process_pid (pid
);
847 remove_process (process
);
849 win32_clear_inferiors ();
854 win32_mourn (struct process_info
*process
)
856 remove_process (process
);
859 /* Wait for inferiors to end. */
863 HANDLE h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
866 WaitForSingleObject (h
, INFINITE
);
871 /* Return 1 iff the thread with thread ID TID is alive. */
873 win32_thread_alive (ptid_t ptid
)
877 /* Our thread list is reliable; don't bother to poll target
879 if (find_inferior_id (&all_threads
, ptid
) != NULL
)
886 /* Resume the inferior process. RESUME_INFO describes how we want
889 win32_resume (struct thread_resume
*resume_info
, size_t n
)
894 win32_thread_info
*th
;
895 DWORD continue_status
= DBG_CONTINUE
;
898 /* This handles the very limited set of resume packets that GDB can
899 currently produce. */
901 if (n
== 1 && ptid_equal (resume_info
[0].thread
, minus_one_ptid
))
906 /* Yes, we're ignoring resume_info[0].thread. It'd be tricky to make
907 the Windows resume code do the right thing for thread switching. */
908 tid
= current_event
.dwThreadId
;
910 if (!ptid_equal (resume_info
[0].thread
, minus_one_ptid
))
912 sig
= resume_info
[0].sig
;
913 step
= resume_info
[0].kind
== resume_step
;
921 if (sig
!= GDB_SIGNAL_0
)
923 if (current_event
.dwDebugEventCode
!= EXCEPTION_DEBUG_EVENT
)
925 OUTMSG (("Cannot continue with signal %d here.\n", sig
));
927 else if (sig
== last_sig
)
928 continue_status
= DBG_EXCEPTION_NOT_HANDLED
;
930 OUTMSG (("Can only continue with recieved signal %d.\n", last_sig
));
933 last_sig
= GDB_SIGNAL_0
;
935 /* Get context for the currently selected thread. */
936 ptid
= debug_event_ptid (¤t_event
);
937 th
= thread_rec (ptid
, FALSE
);
940 if (th
->context
.ContextFlags
)
942 /* Move register values from the inferior into the thread
943 context structure. */
944 regcache_invalidate ();
948 if (the_low_target
.single_step
!= NULL
)
949 (*the_low_target
.single_step
) (th
);
951 error ("Single stepping is not supported "
952 "in this configuration.\n");
955 win32_set_thread_context (th
);
956 th
->context
.ContextFlags
= 0;
960 /* Allow continuing with the same signal that interrupted us.
961 Otherwise complain. */
963 child_continue (continue_status
, tid
);
967 win32_add_one_solib (const char *name
, CORE_ADDR load_addr
)
969 char buf
[MAX_PATH
+ 1];
970 char buf2
[MAX_PATH
+ 1];
973 WIN32_FIND_DATA w32_fd
;
974 WCHAR wname
[MAX_PATH
+ 1];
975 mbstowcs (wname
, name
, MAX_PATH
);
976 HANDLE h
= FindFirstFile (wname
, &w32_fd
);
978 WIN32_FIND_DATAA w32_fd
;
979 HANDLE h
= FindFirstFileA (name
, &w32_fd
);
982 /* The symbols in a dll are offset by 0x1000, which is the
983 offset from 0 of the first byte in an image - because
984 of the file header and the section alignment. */
987 if (h
== INVALID_HANDLE_VALUE
)
995 char cwd
[MAX_PATH
+ 1];
997 if (GetCurrentDirectoryA (MAX_PATH
+ 1, cwd
))
999 p
= strrchr (buf
, '\\');
1002 SetCurrentDirectoryA (buf
);
1003 GetFullPathNameA (w32_fd
.cFileName
, MAX_PATH
, buf
, &p
);
1004 SetCurrentDirectoryA (cwd
);
1011 if (strcasecmp (buf
, "ntdll.dll") == 0)
1013 GetSystemDirectoryA (buf
, sizeof (buf
));
1014 strcat (buf
, "\\ntdll.dll");
1019 cygwin_conv_path (CCP_WIN_A_TO_POSIX
, buf
, buf2
, sizeof (buf2
));
1024 loaded_dll (buf2
, load_addr
);
1028 get_image_name (HANDLE h
, void *address
, int unicode
)
1030 static char buf
[(2 * MAX_PATH
) + 1];
1031 DWORD size
= unicode
? sizeof (WCHAR
) : sizeof (char);
1037 /* Attempt to read the name of the dll that was detected.
1038 This is documented to work only when actively debugging
1039 a program. It will not work for attached processes. */
1040 if (address
== NULL
)
1044 /* Windows CE reports the address of the image name,
1045 instead of an address of a pointer into the image name. */
1046 address_ptr
= address
;
1048 /* See if we could read the address of a string, and that the
1049 address isn't null. */
1050 if (!ReadProcessMemory (h
, address
, &address_ptr
,
1051 sizeof (address_ptr
), &done
)
1052 || done
!= sizeof (address_ptr
)
1057 /* Find the length of the string */
1058 while (ReadProcessMemory (h
, address_ptr
+ len
++ * size
, &b
, size
, &done
)
1059 && (b
[0] != 0 || b
[size
- 1] != 0) && done
== size
)
1063 ReadProcessMemory (h
, address_ptr
, buf
, len
, &done
);
1066 WCHAR
*unicode_address
= (WCHAR
*) alloca (len
* sizeof (WCHAR
));
1067 ReadProcessMemory (h
, address_ptr
, unicode_address
, len
* sizeof (WCHAR
),
1070 WideCharToMultiByte (CP_ACP
, 0, unicode_address
, len
, buf
, len
, 0, 0);
1076 typedef BOOL (WINAPI
*winapi_EnumProcessModules
) (HANDLE
, HMODULE
*,
1078 typedef BOOL (WINAPI
*winapi_GetModuleInformation
) (HANDLE
, HMODULE
,
1079 LPMODULEINFO
, DWORD
);
1080 typedef DWORD (WINAPI
*winapi_GetModuleFileNameExA
) (HANDLE
, HMODULE
,
1083 static winapi_EnumProcessModules win32_EnumProcessModules
;
1084 static winapi_GetModuleInformation win32_GetModuleInformation
;
1085 static winapi_GetModuleFileNameExA win32_GetModuleFileNameExA
;
1090 static int psapi_loaded
= 0;
1091 static HMODULE dll
= NULL
;
1096 dll
= LoadLibrary (TEXT("psapi.dll"));
1099 win32_EnumProcessModules
=
1100 GETPROCADDRESS (dll
, EnumProcessModules
);
1101 win32_GetModuleInformation
=
1102 GETPROCADDRESS (dll
, GetModuleInformation
);
1103 win32_GetModuleFileNameExA
=
1104 GETPROCADDRESS (dll
, GetModuleFileNameExA
);
1107 return (win32_EnumProcessModules
!= NULL
1108 && win32_GetModuleInformation
!= NULL
1109 && win32_GetModuleFileNameExA
!= NULL
);
1114 /* Iterate over all DLLs currently mapped by our inferior, and
1115 add them to our list of solibs. */
1118 win32_add_all_dlls (void)
1122 HMODULE
*DllHandle
= dh_buf
;
1130 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1135 if (!ok
|| !cbNeeded
)
1138 DllHandle
= (HMODULE
*) alloca (cbNeeded
);
1142 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1149 for (i
= 1; i
< ((size_t) cbNeeded
/ sizeof (HMODULE
)); i
++)
1152 char dll_name
[MAX_PATH
];
1154 if (!(*win32_GetModuleInformation
) (current_process_handle
,
1159 if ((*win32_GetModuleFileNameExA
) (current_process_handle
,
1164 win32_add_one_solib (dll_name
, (CORE_ADDR
) (uintptr_t) mi
.lpBaseOfDll
);
1169 typedef HANDLE (WINAPI
*winapi_CreateToolhelp32Snapshot
) (DWORD
, DWORD
);
1170 typedef BOOL (WINAPI
*winapi_Module32First
) (HANDLE
, LPMODULEENTRY32
);
1171 typedef BOOL (WINAPI
*winapi_Module32Next
) (HANDLE
, LPMODULEENTRY32
);
1173 /* Handle a DLL load event.
1175 This function assumes that this event did not occur during inferior
1176 initialization, where their event info may be incomplete (see
1177 do_initial_child_stuff and win32_add_all_dlls for more info on
1178 how we handle DLL loading during that phase). */
1181 handle_load_dll (void)
1183 LOAD_DLL_DEBUG_INFO
*event
= ¤t_event
.u
.LoadDll
;
1186 dll_name
= get_image_name (current_process_handle
,
1187 event
->lpImageName
, event
->fUnicode
);
1191 win32_add_one_solib (dll_name
, (CORE_ADDR
) (uintptr_t) event
->lpBaseOfDll
);
1194 /* Handle a DLL unload event.
1196 This function assumes that this event did not occur during inferior
1197 initialization, where their event info may be incomplete (see
1198 do_initial_child_stuff and win32_add_one_solib for more info
1199 on how we handle DLL loading during that phase). */
1202 handle_unload_dll (void)
1204 CORE_ADDR load_addr
=
1205 (CORE_ADDR
) (uintptr_t) current_event
.u
.UnloadDll
.lpBaseOfDll
;
1207 /* The symbols in a dll are offset by 0x1000, which is the
1208 offset from 0 of the first byte in an image - because
1209 of the file header and the section alignment. */
1210 load_addr
+= 0x1000;
1211 unloaded_dll (NULL
, load_addr
);
1215 handle_exception (struct target_waitstatus
*ourstatus
)
1217 DWORD code
= current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
;
1219 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
1223 case EXCEPTION_ACCESS_VIOLATION
:
1224 OUTMSG2 (("EXCEPTION_ACCESS_VIOLATION"));
1225 ourstatus
->value
.sig
= GDB_SIGNAL_SEGV
;
1227 case STATUS_STACK_OVERFLOW
:
1228 OUTMSG2 (("STATUS_STACK_OVERFLOW"));
1229 ourstatus
->value
.sig
= GDB_SIGNAL_SEGV
;
1231 case STATUS_FLOAT_DENORMAL_OPERAND
:
1232 OUTMSG2 (("STATUS_FLOAT_DENORMAL_OPERAND"));
1233 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1235 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
1236 OUTMSG2 (("EXCEPTION_ARRAY_BOUNDS_EXCEEDED"));
1237 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1239 case STATUS_FLOAT_INEXACT_RESULT
:
1240 OUTMSG2 (("STATUS_FLOAT_INEXACT_RESULT"));
1241 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1243 case STATUS_FLOAT_INVALID_OPERATION
:
1244 OUTMSG2 (("STATUS_FLOAT_INVALID_OPERATION"));
1245 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1247 case STATUS_FLOAT_OVERFLOW
:
1248 OUTMSG2 (("STATUS_FLOAT_OVERFLOW"));
1249 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1251 case STATUS_FLOAT_STACK_CHECK
:
1252 OUTMSG2 (("STATUS_FLOAT_STACK_CHECK"));
1253 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1255 case STATUS_FLOAT_UNDERFLOW
:
1256 OUTMSG2 (("STATUS_FLOAT_UNDERFLOW"));
1257 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1259 case STATUS_FLOAT_DIVIDE_BY_ZERO
:
1260 OUTMSG2 (("STATUS_FLOAT_DIVIDE_BY_ZERO"));
1261 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1263 case STATUS_INTEGER_DIVIDE_BY_ZERO
:
1264 OUTMSG2 (("STATUS_INTEGER_DIVIDE_BY_ZERO"));
1265 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1267 case STATUS_INTEGER_OVERFLOW
:
1268 OUTMSG2 (("STATUS_INTEGER_OVERFLOW"));
1269 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1271 case EXCEPTION_BREAKPOINT
:
1272 OUTMSG2 (("EXCEPTION_BREAKPOINT"));
1273 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1275 /* Remove the initial breakpoint. */
1276 check_breakpoints ((CORE_ADDR
) (long) current_event
1277 .u
.Exception
.ExceptionRecord
.ExceptionAddress
);
1281 OUTMSG2 (("DBG_CONTROL_C"));
1282 ourstatus
->value
.sig
= GDB_SIGNAL_INT
;
1284 case DBG_CONTROL_BREAK
:
1285 OUTMSG2 (("DBG_CONTROL_BREAK"));
1286 ourstatus
->value
.sig
= GDB_SIGNAL_INT
;
1288 case EXCEPTION_SINGLE_STEP
:
1289 OUTMSG2 (("EXCEPTION_SINGLE_STEP"));
1290 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1292 case EXCEPTION_ILLEGAL_INSTRUCTION
:
1293 OUTMSG2 (("EXCEPTION_ILLEGAL_INSTRUCTION"));
1294 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1296 case EXCEPTION_PRIV_INSTRUCTION
:
1297 OUTMSG2 (("EXCEPTION_PRIV_INSTRUCTION"));
1298 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1300 case EXCEPTION_NONCONTINUABLE_EXCEPTION
:
1301 OUTMSG2 (("EXCEPTION_NONCONTINUABLE_EXCEPTION"));
1302 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1305 if (current_event
.u
.Exception
.dwFirstChance
)
1307 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1310 OUTMSG2 (("gdbserver: unknown target exception 0x%08x at 0x%s",
1311 (unsigned) current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
,
1312 phex_nz ((uintptr_t) current_event
.u
.Exception
.ExceptionRecord
.
1313 ExceptionAddress
, sizeof (uintptr_t))));
1314 ourstatus
->value
.sig
= GDB_SIGNAL_UNKNOWN
;
1318 last_sig
= ourstatus
->value
.sig
;
1323 suspend_one_thread (struct inferior_list_entry
*entry
)
1325 struct thread_info
*thread
= (struct thread_info
*) entry
;
1326 win32_thread_info
*th
= inferior_target_data (thread
);
1330 if (SuspendThread (th
->h
) == (DWORD
) -1)
1332 DWORD err
= GetLastError ();
1333 OUTMSG (("warning: SuspendThread failed in suspend_one_thread, "
1334 "(error %d): %s\n", (int) err
, strwinerror (err
)));
1342 fake_breakpoint_event (void)
1344 OUTMSG2(("fake_breakpoint_event\n"));
1346 faked_breakpoint
= 1;
1348 memset (¤t_event
, 0, sizeof (current_event
));
1349 current_event
.dwThreadId
= main_thread_id
;
1350 current_event
.dwDebugEventCode
= EXCEPTION_DEBUG_EVENT
;
1351 current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
1352 = EXCEPTION_BREAKPOINT
;
1354 for_each_inferior (&all_threads
, suspend_one_thread
);
1359 auto_delete_breakpoint (CORE_ADDR stop_pc
)
1365 /* Get the next event from the child. */
1368 get_child_debug_event (struct target_waitstatus
*ourstatus
)
1372 last_sig
= GDB_SIGNAL_0
;
1373 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1375 /* Check if GDB sent us an interrupt request. */
1376 check_remote_input_interrupt_request ();
1378 if (soft_interrupt_requested
)
1380 soft_interrupt_requested
= 0;
1381 fake_breakpoint_event ();
1390 /* WinCE doesn't set an initial breakpoint automatically. To
1391 stop the inferior, we flush all currently pending debug
1392 events -- the thread list and the dll list are always
1393 reported immediatelly without delay, then, we suspend all
1394 threads and pretend we saw a trap at the current PC of the
1397 Contrary to desktop Windows, Windows CE *does* report the dll
1398 names on LOAD_DLL_DEBUG_EVENTs resulting from a
1399 DebugActiveProcess call. This limits the way we can detect
1400 if all the dlls have already been reported. If we get a real
1401 debug event before leaving attaching, the worst that will
1402 happen is the user will see a spurious breakpoint. */
1404 current_event
.dwDebugEventCode
= 0;
1405 if (!WaitForDebugEvent (¤t_event
, 0))
1407 OUTMSG2(("no attach events left\n"));
1408 fake_breakpoint_event ();
1412 OUTMSG2(("got attach event\n"));
1417 /* Keep the wait time low enough for confortable remote
1418 interruption, but high enough so gdbserver doesn't become a
1420 if (!WaitForDebugEvent (¤t_event
, 250))
1422 DWORD e
= GetLastError();
1424 if (e
== ERROR_PIPE_NOT_CONNECTED
)
1426 /* This will happen if the loader fails to succesfully
1427 load the application, e.g., if the main executable
1428 tries to pull in a non-existing export from a
1430 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1431 ourstatus
->value
.integer
= 1;
1441 switch (current_event
.dwDebugEventCode
)
1443 case CREATE_THREAD_DEBUG_EVENT
:
1444 OUTMSG2 (("gdbserver: kernel event CREATE_THREAD_DEBUG_EVENT "
1445 "for pid=%u tid=%x)\n",
1446 (unsigned) current_event
.dwProcessId
,
1447 (unsigned) current_event
.dwThreadId
));
1449 /* Record the existence of this thread. */
1450 child_add_thread (current_event
.dwProcessId
,
1451 current_event
.dwThreadId
,
1452 current_event
.u
.CreateThread
.hThread
,
1453 current_event
.u
.CreateThread
.lpThreadLocalBase
);
1456 case EXIT_THREAD_DEBUG_EVENT
:
1457 OUTMSG2 (("gdbserver: kernel event EXIT_THREAD_DEBUG_EVENT "
1458 "for pid=%u tid=%x\n",
1459 (unsigned) current_event
.dwProcessId
,
1460 (unsigned) current_event
.dwThreadId
));
1461 child_delete_thread (current_event
.dwProcessId
,
1462 current_event
.dwThreadId
);
1464 current_thread
= (struct thread_info
*) all_threads
.head
;
1467 case CREATE_PROCESS_DEBUG_EVENT
:
1468 OUTMSG2 (("gdbserver: kernel event CREATE_PROCESS_DEBUG_EVENT "
1469 "for pid=%u tid=%x\n",
1470 (unsigned) current_event
.dwProcessId
,
1471 (unsigned) current_event
.dwThreadId
));
1472 CloseHandle (current_event
.u
.CreateProcessInfo
.hFile
);
1474 current_process_handle
= current_event
.u
.CreateProcessInfo
.hProcess
;
1475 main_thread_id
= current_event
.dwThreadId
;
1477 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
1478 ourstatus
->value
.execd_pathname
= "Main executable";
1480 /* Add the main thread. */
1481 child_add_thread (current_event
.dwProcessId
,
1483 current_event
.u
.CreateProcessInfo
.hThread
,
1484 current_event
.u
.CreateProcessInfo
.lpThreadLocalBase
);
1486 ourstatus
->value
.related_pid
= debug_event_ptid (¤t_event
);
1490 /* Windows CE doesn't set the initial breakpoint
1491 automatically like the desktop versions of Windows do.
1492 We add it explicitly here. It will be removed as soon as
1494 set_breakpoint_at ((CORE_ADDR
) (long) current_event
.u
1495 .CreateProcessInfo
.lpStartAddress
,
1496 auto_delete_breakpoint
);
1501 case EXIT_PROCESS_DEBUG_EVENT
:
1502 OUTMSG2 (("gdbserver: kernel event EXIT_PROCESS_DEBUG_EVENT "
1503 "for pid=%u tid=%x\n",
1504 (unsigned) current_event
.dwProcessId
,
1505 (unsigned) current_event
.dwThreadId
));
1506 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1507 ourstatus
->value
.integer
= current_event
.u
.ExitProcess
.dwExitCode
;
1508 child_continue (DBG_CONTINUE
, -1);
1509 CloseHandle (current_process_handle
);
1510 current_process_handle
= NULL
;
1513 case LOAD_DLL_DEBUG_EVENT
:
1514 OUTMSG2 (("gdbserver: kernel event LOAD_DLL_DEBUG_EVENT "
1515 "for pid=%u tid=%x\n",
1516 (unsigned) current_event
.dwProcessId
,
1517 (unsigned) current_event
.dwThreadId
));
1518 CloseHandle (current_event
.u
.LoadDll
.hFile
);
1519 if (! child_initialization_done
)
1523 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1524 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1527 case UNLOAD_DLL_DEBUG_EVENT
:
1528 OUTMSG2 (("gdbserver: kernel event UNLOAD_DLL_DEBUG_EVENT "
1529 "for pid=%u tid=%x\n",
1530 (unsigned) current_event
.dwProcessId
,
1531 (unsigned) current_event
.dwThreadId
));
1532 if (! child_initialization_done
)
1534 handle_unload_dll ();
1535 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1536 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1539 case EXCEPTION_DEBUG_EVENT
:
1540 OUTMSG2 (("gdbserver: kernel event EXCEPTION_DEBUG_EVENT "
1541 "for pid=%u tid=%x\n",
1542 (unsigned) current_event
.dwProcessId
,
1543 (unsigned) current_event
.dwThreadId
));
1544 handle_exception (ourstatus
);
1547 case OUTPUT_DEBUG_STRING_EVENT
:
1548 /* A message from the kernel (or Cygwin). */
1549 OUTMSG2 (("gdbserver: kernel event OUTPUT_DEBUG_STRING_EVENT "
1550 "for pid=%u tid=%x\n",
1551 (unsigned) current_event
.dwProcessId
,
1552 (unsigned) current_event
.dwThreadId
));
1553 handle_output_debug_string (ourstatus
);
1557 OUTMSG2 (("gdbserver: kernel event unknown "
1558 "for pid=%u tid=%x code=%x\n",
1559 (unsigned) current_event
.dwProcessId
,
1560 (unsigned) current_event
.dwThreadId
,
1561 (unsigned) current_event
.dwDebugEventCode
));
1565 ptid
= debug_event_ptid (¤t_event
);
1567 (struct thread_info
*) find_inferior_id (&all_threads
, ptid
);
1571 /* Wait for the inferior process to change state.
1572 STATUS will be filled in with a response code to send to GDB.
1573 Returns the signal which caused the process to stop. */
1575 win32_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
, int options
)
1577 struct regcache
*regcache
;
1579 if (cached_status
.kind
!= TARGET_WAITKIND_IGNORE
)
1581 /* The core always does a wait after creating the inferior, and
1582 do_initial_child_stuff already ran the inferior to the
1583 initial breakpoint (or an exit, if creating the process
1584 fails). Report it now. */
1585 *ourstatus
= cached_status
;
1586 cached_status
.kind
= TARGET_WAITKIND_IGNORE
;
1587 return debug_event_ptid (¤t_event
);
1592 if (!get_child_debug_event (ourstatus
))
1595 switch (ourstatus
->kind
)
1597 case TARGET_WAITKIND_EXITED
:
1598 OUTMSG2 (("Child exited with retcode = %x\n",
1599 ourstatus
->value
.integer
));
1600 win32_clear_inferiors ();
1601 return pid_to_ptid (current_event
.dwProcessId
);
1602 case TARGET_WAITKIND_STOPPED
:
1603 case TARGET_WAITKIND_LOADED
:
1604 OUTMSG2 (("Child Stopped with signal = %d \n",
1605 ourstatus
->value
.sig
));
1607 regcache
= get_thread_regcache (current_thread
, 1);
1608 child_fetch_inferior_registers (regcache
, -1);
1609 return debug_event_ptid (¤t_event
);
1611 OUTMSG (("Ignoring unknown internal event, %d\n", ourstatus
->kind
));
1613 case TARGET_WAITKIND_SPURIOUS
:
1614 case TARGET_WAITKIND_EXECD
:
1615 /* do nothing, just continue */
1616 child_continue (DBG_CONTINUE
, -1);
1622 /* Fetch registers from the inferior process.
1623 If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO. */
1625 win32_fetch_inferior_registers (struct regcache
*regcache
, int regno
)
1627 child_fetch_inferior_registers (regcache
, regno
);
1630 /* Store registers to the inferior process.
1631 If REGNO is -1, store all registers; otherwise, store at least REGNO. */
1633 win32_store_inferior_registers (struct regcache
*regcache
, int regno
)
1635 child_store_inferior_registers (regcache
, regno
);
1638 /* Read memory from the inferior process. This should generally be
1639 called through read_inferior_memory, which handles breakpoint shadowing.
1640 Read LEN bytes at MEMADDR into a buffer at MYADDR. */
1642 win32_read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
1644 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 0, 0) != len
;
1647 /* Write memory to the inferior process. This should generally be
1648 called through write_inferior_memory, which handles breakpoint shadowing.
1649 Write LEN bytes from the buffer at MYADDR to MEMADDR.
1650 Returns 0 on success and errno on failure. */
1652 win32_write_inferior_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
,
1655 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 1, 0) != len
;
1658 /* Send an interrupt request to the inferior process. */
1660 win32_request_interrupt (void)
1662 winapi_DebugBreakProcess DebugBreakProcess
;
1663 winapi_GenerateConsoleCtrlEvent GenerateConsoleCtrlEvent
;
1666 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
1668 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
1671 GenerateConsoleCtrlEvent
= GETPROCADDRESS (dll
, GenerateConsoleCtrlEvent
);
1673 if (GenerateConsoleCtrlEvent
!= NULL
1674 && GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT
, current_process_id
))
1677 /* GenerateConsoleCtrlEvent can fail if process id being debugged is
1678 not a process group id.
1679 Fallback to XP/Vista 'DebugBreakProcess', which generates a
1680 breakpoint exception in the interior process. */
1682 DebugBreakProcess
= GETPROCADDRESS (dll
, DebugBreakProcess
);
1684 if (DebugBreakProcess
!= NULL
1685 && DebugBreakProcess (current_process_handle
))
1688 /* Last resort, suspend all threads manually. */
1689 soft_interrupt_requested
= 1;
1694 win32_error_to_fileio_error (DWORD err
)
1698 case ERROR_BAD_PATHNAME
:
1699 case ERROR_FILE_NOT_FOUND
:
1700 case ERROR_INVALID_NAME
:
1701 case ERROR_PATH_NOT_FOUND
:
1702 return FILEIO_ENOENT
;
1704 case ERROR_IO_DEVICE
:
1705 case ERROR_OPEN_FAILED
:
1707 case ERROR_INVALID_HANDLE
:
1708 return FILEIO_EBADF
;
1709 case ERROR_ACCESS_DENIED
:
1710 case ERROR_SHARING_VIOLATION
:
1711 return FILEIO_EACCES
;
1712 case ERROR_NOACCESS
:
1713 return FILEIO_EFAULT
;
1715 return FILEIO_EBUSY
;
1716 case ERROR_ALREADY_EXISTS
:
1717 case ERROR_FILE_EXISTS
:
1718 return FILEIO_EEXIST
;
1719 case ERROR_BAD_DEVICE
:
1720 return FILEIO_ENODEV
;
1721 case ERROR_DIRECTORY
:
1722 return FILEIO_ENOTDIR
;
1723 case ERROR_FILENAME_EXCED_RANGE
:
1724 case ERROR_INVALID_DATA
:
1725 case ERROR_INVALID_PARAMETER
:
1726 case ERROR_NEGATIVE_SEEK
:
1727 return FILEIO_EINVAL
;
1728 case ERROR_TOO_MANY_OPEN_FILES
:
1729 return FILEIO_EMFILE
;
1730 case ERROR_HANDLE_DISK_FULL
:
1731 case ERROR_DISK_FULL
:
1732 return FILEIO_ENOSPC
;
1733 case ERROR_WRITE_PROTECT
:
1734 return FILEIO_EROFS
;
1735 case ERROR_NOT_SUPPORTED
:
1736 return FILEIO_ENOSYS
;
1739 return FILEIO_EUNKNOWN
;
1743 wince_hostio_last_error (char *buf
)
1745 DWORD winerr
= GetLastError ();
1746 int fileio_err
= win32_error_to_fileio_error (winerr
);
1747 sprintf (buf
, "F-1,%x", fileio_err
);
1751 /* Write Windows OS Thread Information Block address. */
1754 win32_get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
)
1756 win32_thread_info
*th
;
1757 th
= thread_rec (ptid
, 0);
1761 *addr
= th
->thread_local_base
;
1765 static struct target_ops win32_target_ops
= {
1766 win32_create_inferior
,
1775 win32_fetch_inferior_registers
,
1776 win32_store_inferior_registers
,
1777 NULL
, /* prepare_to_access_memory */
1778 NULL
, /* done_accessing_memory */
1779 win32_read_inferior_memory
,
1780 win32_write_inferior_memory
,
1781 NULL
, /* lookup_symbols */
1782 win32_request_interrupt
,
1783 NULL
, /* read_auxv */
1784 win32_supports_z_point_type
,
1787 win32_stopped_by_watchpoint
,
1788 win32_stopped_data_address
,
1789 NULL
, /* read_offsets */
1790 NULL
, /* get_tls_address */
1791 NULL
, /* qxfer_spu */
1793 wince_hostio_last_error
,
1795 hostio_last_error_from_errno
,
1797 NULL
, /* qxfer_osdata */
1798 NULL
, /* qxfer_siginfo */
1799 NULL
, /* supports_non_stop */
1801 NULL
, /* start_non_stop */
1802 NULL
, /* supports_multi_process */
1803 NULL
, /* handle_monitor_command */
1804 NULL
, /* core_of_thread */
1805 NULL
, /* read_loadmap */
1806 NULL
, /* process_qsupported */
1807 NULL
, /* supports_tracepoints */
1809 NULL
, /* write_pc */
1810 NULL
, /* thread_stopped */
1811 win32_get_tib_address
1814 /* Initialize the Win32 backend. */
1816 initialize_low (void)
1818 set_target_ops (&win32_target_ops
);
1819 if (the_low_target
.breakpoint
!= NULL
)
1820 set_breakpoint_data (the_low_target
.breakpoint
,
1821 the_low_target
.breakpoint_len
);
1822 the_low_target
.arch_setup ();