1 /* Low level interface to Windows debugging, for gdbserver.
2 Copyright (C) 2006, 2007, 2008, 2009 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"
33 #include <sys/param.h>
38 #include <sys/cygwin.h>
43 #define OUTMSG(X) do { printf X; fflush (stdout); } while (0)
45 #define OUTMSG2(X) do { printf X; fflush (stdout); } while (0)
47 #define OUTMSG2(X) do ; while (0)
51 #define _T(x) TEXT (x)
55 #define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0]))
59 # define GETPROCADDRESS(DLL, PROC) \
60 ((winapi_ ## PROC) GetProcAddress (DLL, TEXT (#PROC)))
62 # define GETPROCADDRESS(DLL, PROC) \
63 ((winapi_ ## PROC) GetProcAddress (DLL, #PROC))
66 int using_threads
= 1;
69 static int attaching
= 0;
70 static HANDLE current_process_handle
= NULL
;
71 static DWORD current_process_id
= 0;
72 static DWORD main_thread_id
= 0;
73 static enum target_signal last_sig
= TARGET_SIGNAL_0
;
75 /* The current debug event from WaitForDebugEvent. */
76 static DEBUG_EVENT current_event
;
78 /* Non zero if an interrupt request is to be satisfied by suspending
80 static int soft_interrupt_requested
= 0;
82 /* Non zero if the inferior is stopped in a simulated breakpoint done
83 by suspending all the threads. */
84 static int faked_breakpoint
= 0;
86 #define NUM_REGS (the_low_target.num_regs)
88 typedef BOOL
WINAPI (*winapi_DebugActiveProcessStop
) (DWORD dwProcessId
);
89 typedef BOOL
WINAPI (*winapi_DebugSetProcessKillOnExit
) (BOOL KillOnExit
);
90 typedef BOOL
WINAPI (*winapi_DebugBreakProcess
) (HANDLE
);
91 typedef BOOL
WINAPI (*winapi_GenerateConsoleCtrlEvent
) (DWORD
, DWORD
);
93 static void win32_resume (struct thread_resume
*resume_info
, size_t n
);
95 /* Get the thread ID from the current selected inferior (the current
98 current_inferior_ptid (void)
100 return ((struct inferior_list_entry
*) current_inferior
)->id
;
103 /* The current debug event from WaitForDebugEvent. */
105 debug_event_ptid (DEBUG_EVENT
*event
)
107 return ptid_build (event
->dwProcessId
, event
->dwThreadId
, 0);
110 /* Get the thread context of the thread associated with TH. */
113 win32_get_thread_context (win32_thread_info
*th
)
115 memset (&th
->context
, 0, sizeof (CONTEXT
));
116 (*the_low_target
.get_thread_context
) (th
, ¤t_event
);
118 memcpy (&th
->base_context
, &th
->context
, sizeof (CONTEXT
));
122 /* Set the thread context of the thread associated with TH. */
125 win32_set_thread_context (win32_thread_info
*th
)
128 /* Calling SuspendThread on a thread that is running kernel code
129 will report that the suspending was successful, but in fact, that
130 will often not be true. In those cases, the context returned by
131 GetThreadContext will not be correct by the time the thread
132 stops, hence we can't set that context back into the thread when
133 resuming - it will most likelly crash the inferior.
134 Unfortunately, there is no way to know when the thread will
135 really stop. To work around it, we'll only write the context
136 back to the thread when either the user or GDB explicitly change
137 it between stopping and resuming. */
138 if (memcmp (&th
->context
, &th
->base_context
, sizeof (CONTEXT
)) != 0)
140 (*the_low_target
.set_thread_context
) (th
, ¤t_event
);
143 /* Find a thread record given a thread id. If GET_CONTEXT is set then
144 also retrieve the context for this thread. */
145 static win32_thread_info
*
146 thread_rec (ptid_t ptid
, int get_context
)
148 struct thread_info
*thread
;
149 win32_thread_info
*th
;
151 thread
= (struct thread_info
*) find_inferior_id (&all_threads
, ptid
);
155 th
= inferior_target_data (thread
);
156 if (get_context
&& th
->context
.ContextFlags
== 0)
160 if (SuspendThread (th
->h
) == (DWORD
) -1)
162 DWORD err
= GetLastError ();
163 OUTMSG (("warning: SuspendThread failed in thread_rec, "
164 "(error %d): %s\n", (int) err
, strwinerror (err
)));
170 win32_get_thread_context (th
);
176 /* Add a thread to the thread list. */
177 static win32_thread_info
*
178 child_add_thread (DWORD pid
, DWORD tid
, HANDLE h
)
180 win32_thread_info
*th
;
181 ptid_t ptid
= ptid_build (pid
, tid
, 0);
183 if ((th
= thread_rec (ptid
, FALSE
)))
186 th
= xcalloc (1, sizeof (*th
));
190 add_thread (ptid
, th
);
191 set_inferior_regcache_data ((struct thread_info
*)
192 find_inferior_id (&all_threads
, ptid
),
193 new_register_cache ());
195 if (the_low_target
.thread_added
!= NULL
)
196 (*the_low_target
.thread_added
) (th
);
201 /* Delete a thread from the list of threads. */
203 delete_thread_info (struct inferior_list_entry
*thread
)
205 win32_thread_info
*th
= inferior_target_data ((struct thread_info
*) thread
);
207 remove_thread ((struct thread_info
*) thread
);
212 /* Delete a thread from the list of threads. */
214 child_delete_thread (DWORD pid
, DWORD tid
)
216 struct inferior_list_entry
*thread
;
219 /* If the last thread is exiting, just return. */
220 if (all_threads
.head
== all_threads
.tail
)
223 ptid
= ptid_build (pid
, tid
, 0);
224 thread
= find_inferior_id (&all_threads
, ptid
);
228 delete_thread_info (thread
);
231 /* These watchpoint related wrapper functions simply pass on the function call
232 if the low target has registered a corresponding function. */
235 win32_insert_point (char type
, CORE_ADDR addr
, int len
)
237 if (the_low_target
.insert_point
!= NULL
)
238 return the_low_target
.insert_point (type
, addr
, len
);
240 /* Unsupported (see target.h). */
245 win32_remove_point (char type
, CORE_ADDR addr
, int len
)
247 if (the_low_target
.remove_point
!= NULL
)
248 return the_low_target
.remove_point (type
, addr
, len
);
250 /* Unsupported (see target.h). */
255 win32_stopped_by_watchpoint (void)
257 if (the_low_target
.stopped_by_watchpoint
!= NULL
)
258 return the_low_target
.stopped_by_watchpoint ();
264 win32_stopped_data_address (void)
266 if (the_low_target
.stopped_data_address
!= NULL
)
267 return the_low_target
.stopped_data_address ();
273 /* Transfer memory from/to the debugged process. */
275 child_xfer_memory (CORE_ADDR memaddr
, char *our
, int len
,
276 int write
, struct target_ops
*target
)
279 long addr
= (long) memaddr
;
283 WriteProcessMemory (current_process_handle
, (LPVOID
) addr
,
284 (LPCVOID
) our
, len
, &done
);
285 FlushInstructionCache (current_process_handle
, (LPCVOID
) addr
, len
);
289 ReadProcessMemory (current_process_handle
, (LPCVOID
) addr
, (LPVOID
) our
,
295 /* Clear out any old thread list and reinitialize it to a pristine
298 child_init_thread_list (void)
300 for_each_inferior (&all_threads
, delete_thread_info
);
304 do_initial_child_stuff (HANDLE proch
, DWORD pid
, int attached
)
306 last_sig
= TARGET_SIGNAL_0
;
308 current_process_handle
= proch
;
309 current_process_id
= pid
;
312 soft_interrupt_requested
= 0;
313 faked_breakpoint
= 0;
315 memset (¤t_event
, 0, sizeof (current_event
));
317 add_process (pid
, attached
);
318 child_init_thread_list ();
320 if (the_low_target
.initial_stuff
!= NULL
)
321 (*the_low_target
.initial_stuff
) ();
324 /* Resume all artificially suspended threads if we are continuing
327 continue_one_thread (struct inferior_list_entry
*this_thread
, void *id_ptr
)
329 struct thread_info
*thread
= (struct thread_info
*) this_thread
;
330 int thread_id
= * (int *) id_ptr
;
331 win32_thread_info
*th
= inferior_target_data (thread
);
333 if ((thread_id
== -1 || thread_id
== th
->tid
)
336 if (th
->context
.ContextFlags
)
338 win32_set_thread_context (th
);
339 th
->context
.ContextFlags
= 0;
342 if (ResumeThread (th
->h
) == (DWORD
) -1)
344 DWORD err
= GetLastError ();
345 OUTMSG (("warning: ResumeThread failed in continue_one_thread, "
346 "(error %d): %s\n", (int) err
, strwinerror (err
)));
355 child_continue (DWORD continue_status
, int thread_id
)
357 /* The inferior will only continue after the ContinueDebugEvent
359 find_inferior (&all_threads
, continue_one_thread
, &thread_id
);
360 faked_breakpoint
= 0;
362 if (!ContinueDebugEvent (current_event
.dwProcessId
,
363 current_event
.dwThreadId
,
370 /* Fetch register(s) from the current thread context. */
372 child_fetch_inferior_registers (int r
)
375 win32_thread_info
*th
= thread_rec (current_inferior_ptid (), TRUE
);
376 if (r
== -1 || r
> NUM_REGS
)
377 child_fetch_inferior_registers (NUM_REGS
);
379 for (regno
= 0; regno
< r
; regno
++)
380 (*the_low_target
.fetch_inferior_register
) (th
, regno
);
383 /* Store a new register value into the current thread context. We don't
384 change the program's context until later, when we resume it. */
386 child_store_inferior_registers (int r
)
389 win32_thread_info
*th
= thread_rec (current_inferior_ptid (), TRUE
);
390 if (r
== -1 || r
== 0 || r
> NUM_REGS
)
391 child_store_inferior_registers (NUM_REGS
);
393 for (regno
= 0; regno
< r
; regno
++)
394 (*the_low_target
.store_inferior_register
) (th
, regno
);
397 /* Map the Windows error number in ERROR to a locale-dependent error
398 message string and return a pointer to it. Typically, the values
399 for ERROR come from GetLastError.
401 The string pointed to shall not be modified by the application,
402 but may be overwritten by a subsequent call to strwinerror
404 The strwinerror function does not change the current setting
408 strwinerror (DWORD error
)
410 static char buf
[1024];
412 DWORD lasterr
= GetLastError ();
413 DWORD chars
= FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
414 | FORMAT_MESSAGE_ALLOCATE_BUFFER
,
417 0, /* Default language */
423 /* If there is an \r\n appended, zap it. */
425 && msgbuf
[chars
- 2] == '\r'
426 && msgbuf
[chars
- 1] == '\n')
432 if (chars
> ((COUNTOF (buf
)) - 1))
434 chars
= COUNTOF (buf
) - 1;
439 wcstombs (buf
, msgbuf
, chars
+ 1);
441 strncpy (buf
, msgbuf
, chars
+ 1);
446 sprintf (buf
, "unknown win32 error (%ld)", error
);
448 SetLastError (lasterr
);
453 create_process (const char *program
, char *args
,
454 DWORD flags
, PROCESS_INFORMATION
*pi
)
459 wchar_t *p
, *wprogram
, *wargs
;
462 wprogram
= alloca ((strlen (program
) + 1) * sizeof (wchar_t));
463 mbstowcs (wprogram
, program
, strlen (program
) + 1);
465 for (p
= wprogram
; *p
; ++p
)
469 argslen
= strlen (args
);
470 wargs
= alloca ((argslen
+ 1) * sizeof (wchar_t));
471 mbstowcs (wargs
, args
, argslen
+ 1);
473 ret
= CreateProcessW (wprogram
, /* image name */
474 wargs
, /* command line */
475 NULL
, /* security, not supported */
476 NULL
, /* thread, not supported */
477 FALSE
, /* inherit handles, not supported */
478 flags
, /* start flags */
479 NULL
, /* environment, not supported */
480 NULL
, /* current directory, not supported */
481 NULL
, /* start info, not supported */
484 STARTUPINFOA si
= { sizeof (STARTUPINFOA
) };
486 ret
= CreateProcessA (program
, /* image name */
487 args
, /* command line */
490 TRUE
, /* inherit handles */
491 flags
, /* start flags */
492 NULL
, /* environment */
493 NULL
, /* current directory */
494 &si
, /* start info */
501 /* Start a new process.
502 PROGRAM is a path to the program to execute.
503 ARGS is a standard NULL-terminated array of arguments,
504 to be passed to the inferior as ``argv''.
505 Returns the new PID on success, -1 on failure. Registers the new
506 process with the process list. */
508 win32_create_inferior (char *program
, char **program_args
)
511 char real_path
[MAXPATHLEN
];
512 char *orig_path
, *new_path
, *path_ptr
;
519 PROCESS_INFORMATION pi
;
522 /* win32_wait needs to know we're not attaching. */
526 error ("No executable specified, specify executable to debug.\n");
528 flags
= DEBUG_PROCESS
| DEBUG_ONLY_THIS_PROCESS
;
532 path_ptr
= getenv ("PATH");
535 orig_path
= alloca (strlen (path_ptr
) + 1);
536 new_path
= alloca (cygwin_posix_to_win32_path_list_buf_size (path_ptr
));
537 strcpy (orig_path
, path_ptr
);
538 cygwin_posix_to_win32_path_list (path_ptr
, new_path
);
539 setenv ("PATH", new_path
, 1);
541 cygwin_conv_to_win32_path (program
, real_path
);
546 for (argc
= 1; program_args
[argc
]; argc
++)
547 argslen
+= strlen (program_args
[argc
]) + 1;
548 args
= alloca (argslen
);
550 for (argc
= 1; program_args
[argc
]; argc
++)
552 /* FIXME: Can we do better about quoting? How does Cygwin
555 strcat (args
, program_args
[argc
]);
557 OUTMSG2 (("Command line is \"%s\"\n", args
));
559 #ifdef CREATE_NEW_PROCESS_GROUP
560 flags
|= CREATE_NEW_PROCESS_GROUP
;
563 ret
= create_process (program
, args
, flags
, &pi
);
564 err
= GetLastError ();
565 if (!ret
&& err
== ERROR_FILE_NOT_FOUND
)
567 char *exename
= alloca (strlen (program
) + 5);
568 strcat (strcpy (exename
, program
), ".exe");
569 ret
= create_process (exename
, args
, flags
, &pi
);
570 err
= GetLastError ();
575 setenv ("PATH", orig_path
, 1);
580 error ("Error creating process \"%s%s\", (error %d): %s\n",
581 program
, args
, (int) err
, strwinerror (err
));
585 OUTMSG2 (("Process created: %s\n", (char *) args
));
589 /* On Windows CE this handle can't be closed. The OS reuses
590 it in the debug events, while the 9x/NT versions of Windows
591 probably use a DuplicateHandle'd one. */
592 CloseHandle (pi
.hThread
);
595 do_initial_child_stuff (pi
.hProcess
, pi
.dwProcessId
, 0);
597 return current_process_id
;
600 /* Attach to a running process.
601 PID is the process ID to attach to, specified by the user
602 or a higher layer. */
604 win32_attach (unsigned long pid
)
607 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
610 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
612 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
614 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
616 h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
619 if (DebugActiveProcess (pid
))
621 if (DebugSetProcessKillOnExit
!= NULL
)
622 DebugSetProcessKillOnExit (FALSE
);
624 /* win32_wait needs to know we're attaching. */
626 do_initial_child_stuff (h
, pid
, 1);
633 err
= GetLastError ();
634 error ("Attach to process failed (error %d): %s\n",
635 (int) err
, strwinerror (err
));
638 /* Handle OUTPUT_DEBUG_STRING_EVENT from child process. */
640 handle_output_debug_string (struct target_waitstatus
*ourstatus
)
642 #define READ_BUFFER_LEN 1024
644 char s
[READ_BUFFER_LEN
+ 1] = { 0 };
645 DWORD nbytes
= current_event
.u
.DebugString
.nDebugStringLength
;
650 if (nbytes
> READ_BUFFER_LEN
)
651 nbytes
= READ_BUFFER_LEN
;
653 addr
= (CORE_ADDR
) (size_t) current_event
.u
.DebugString
.lpDebugStringData
;
655 if (current_event
.u
.DebugString
.fUnicode
)
657 /* The event tells us how many bytes, not chars, even
659 WCHAR buffer
[(READ_BUFFER_LEN
+ 1) / sizeof (WCHAR
)] = { 0 };
660 if (read_inferior_memory (addr
, (unsigned char *) buffer
, nbytes
) != 0)
662 wcstombs (s
, buffer
, (nbytes
+ 1) / sizeof (WCHAR
));
666 if (read_inferior_memory (addr
, (unsigned char *) s
, nbytes
) != 0)
670 if (strncmp (s
, "cYg", 3) != 0)
680 #undef READ_BUFFER_LEN
684 win32_clear_inferiors (void)
686 if (current_process_handle
!= NULL
)
687 CloseHandle (current_process_handle
);
689 for_each_inferior (&all_threads
, delete_thread_info
);
693 /* Kill all inferiors. */
697 struct process_info
*process
;
699 if (current_process_handle
== NULL
)
702 TerminateProcess (current_process_handle
, 0);
705 if (!child_continue (DBG_CONTINUE
, -1))
707 if (!WaitForDebugEvent (¤t_event
, INFINITE
))
709 if (current_event
.dwDebugEventCode
== EXIT_PROCESS_DEBUG_EVENT
)
711 else if (current_event
.dwDebugEventCode
== OUTPUT_DEBUG_STRING_EVENT
)
713 struct target_waitstatus our_status
= { 0 };
714 handle_output_debug_string (&our_status
);
718 win32_clear_inferiors ();
720 process
= find_process_pid (pid
);
721 remove_process (process
);
725 /* Detach from inferior PID. */
727 win32_detach (int pid
)
729 struct process_info
*process
;
730 winapi_DebugActiveProcessStop DebugActiveProcessStop
= NULL
;
731 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
733 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
735 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
737 DebugActiveProcessStop
= GETPROCADDRESS (dll
, DebugActiveProcessStop
);
738 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
740 if (DebugSetProcessKillOnExit
== NULL
741 || DebugActiveProcessStop
== NULL
)
745 struct thread_resume resume
;
746 resume
.thread
= minus_one_ptid
;
747 resume
.kind
= resume_continue
;
749 win32_resume (&resume
, 1);
752 if (!DebugActiveProcessStop (current_process_id
))
755 DebugSetProcessKillOnExit (FALSE
);
756 process
= find_process_pid (pid
);
757 remove_process (process
);
759 win32_clear_inferiors ();
763 /* Wait for inferiors to end. */
767 HANDLE h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
770 WaitForSingleObject (h
, INFINITE
);
775 /* Return 1 iff the thread with thread ID TID is alive. */
777 win32_thread_alive (ptid_t ptid
)
781 /* Our thread list is reliable; don't bother to poll target
783 if (find_inferior_id (&all_threads
, ptid
) != NULL
)
790 /* Resume the inferior process. RESUME_INFO describes how we want
793 win32_resume (struct thread_resume
*resume_info
, size_t n
)
796 enum target_signal sig
;
798 win32_thread_info
*th
;
799 DWORD continue_status
= DBG_CONTINUE
;
802 /* This handles the very limited set of resume packets that GDB can
803 currently produce. */
805 if (n
== 1 && ptid_equal (resume_info
[0].thread
, minus_one_ptid
))
810 /* Yes, we're ignoring resume_info[0].thread. It'd be tricky to make
811 the Windows resume code do the right thing for thread switching. */
812 tid
= current_event
.dwThreadId
;
814 if (!ptid_equal (resume_info
[0].thread
, minus_one_ptid
))
816 sig
= resume_info
[0].sig
;
817 step
= resume_info
[0].kind
== resume_step
;
825 if (sig
!= TARGET_SIGNAL_0
)
827 if (current_event
.dwDebugEventCode
!= EXCEPTION_DEBUG_EVENT
)
829 OUTMSG (("Cannot continue with signal %d here.\n", sig
));
831 else if (sig
== last_sig
)
832 continue_status
= DBG_EXCEPTION_NOT_HANDLED
;
834 OUTMSG (("Can only continue with recieved signal %d.\n", last_sig
));
837 last_sig
= TARGET_SIGNAL_0
;
839 /* Get context for the currently selected thread. */
840 ptid
= debug_event_ptid (¤t_event
);
841 th
= thread_rec (ptid
, FALSE
);
844 if (th
->context
.ContextFlags
)
846 /* Move register values from the inferior into the thread
847 context structure. */
848 regcache_invalidate ();
852 if (the_low_target
.single_step
!= NULL
)
853 (*the_low_target
.single_step
) (th
);
855 error ("Single stepping is not supported "
856 "in this configuration.\n");
859 win32_set_thread_context (th
);
860 th
->context
.ContextFlags
= 0;
864 /* Allow continuing with the same signal that interrupted us.
865 Otherwise complain. */
867 child_continue (continue_status
, tid
);
871 win32_add_one_solib (const char *name
, CORE_ADDR load_addr
)
873 char buf
[MAX_PATH
+ 1];
874 char buf2
[MAX_PATH
+ 1];
877 WIN32_FIND_DATA w32_fd
;
878 WCHAR wname
[MAX_PATH
+ 1];
879 mbstowcs (wname
, name
, MAX_PATH
);
880 HANDLE h
= FindFirstFile (wname
, &w32_fd
);
882 WIN32_FIND_DATAA w32_fd
;
883 HANDLE h
= FindFirstFileA (name
, &w32_fd
);
886 if (h
== INVALID_HANDLE_VALUE
)
894 char cwd
[MAX_PATH
+ 1];
896 if (GetCurrentDirectoryA (MAX_PATH
+ 1, cwd
))
898 p
= strrchr (buf
, '\\');
901 SetCurrentDirectoryA (buf
);
902 GetFullPathNameA (w32_fd
.cFileName
, MAX_PATH
, buf
, &p
);
903 SetCurrentDirectoryA (cwd
);
910 cygwin_conv_to_posix_path (buf
, buf2
);
915 loaded_dll (buf2
, load_addr
);
919 get_image_name (HANDLE h
, void *address
, int unicode
)
921 static char buf
[(2 * MAX_PATH
) + 1];
922 DWORD size
= unicode
? sizeof (WCHAR
) : sizeof (char);
928 /* Attempt to read the name of the dll that was detected.
929 This is documented to work only when actively debugging
930 a program. It will not work for attached processes. */
935 /* Windows CE reports the address of the image name,
936 instead of an address of a pointer into the image name. */
937 address_ptr
= address
;
939 /* See if we could read the address of a string, and that the
940 address isn't null. */
941 if (!ReadProcessMemory (h
, address
, &address_ptr
,
942 sizeof (address_ptr
), &done
)
943 || done
!= sizeof (address_ptr
)
948 /* Find the length of the string */
949 while (ReadProcessMemory (h
, address_ptr
+ len
++ * size
, &b
, size
, &done
)
950 && (b
[0] != 0 || b
[size
- 1] != 0) && done
== size
)
954 ReadProcessMemory (h
, address_ptr
, buf
, len
, &done
);
957 WCHAR
*unicode_address
= (WCHAR
*) alloca (len
* sizeof (WCHAR
));
958 ReadProcessMemory (h
, address_ptr
, unicode_address
, len
* sizeof (WCHAR
),
961 WideCharToMultiByte (CP_ACP
, 0, unicode_address
, len
, buf
, len
, 0, 0);
967 typedef BOOL (WINAPI
*winapi_EnumProcessModules
) (HANDLE
, HMODULE
*,
969 typedef BOOL (WINAPI
*winapi_GetModuleInformation
) (HANDLE
, HMODULE
,
970 LPMODULEINFO
, DWORD
);
971 typedef DWORD (WINAPI
*winapi_GetModuleFileNameExA
) (HANDLE
, HMODULE
,
974 static winapi_EnumProcessModules win32_EnumProcessModules
;
975 static winapi_GetModuleInformation win32_GetModuleInformation
;
976 static winapi_GetModuleFileNameExA win32_GetModuleFileNameExA
;
981 static int psapi_loaded
= 0;
982 static HMODULE dll
= NULL
;
987 dll
= LoadLibrary (TEXT("psapi.dll"));
990 win32_EnumProcessModules
=
991 GETPROCADDRESS (dll
, EnumProcessModules
);
992 win32_GetModuleInformation
=
993 GETPROCADDRESS (dll
, GetModuleInformation
);
994 win32_GetModuleFileNameExA
=
995 GETPROCADDRESS (dll
, GetModuleFileNameExA
);
998 return (win32_EnumProcessModules
!= NULL
999 && win32_GetModuleInformation
!= NULL
1000 && win32_GetModuleFileNameExA
!= NULL
);
1004 psapi_get_dll_name (DWORD BaseAddress
, char *dll_name_ret
)
1010 HMODULE
*DllHandle
= dh_buf
;
1018 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1023 if (!ok
|| !cbNeeded
)
1026 DllHandle
= (HMODULE
*) alloca (cbNeeded
);
1030 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1037 for (i
= 0; i
< ((size_t) cbNeeded
/ sizeof (HMODULE
)); i
++)
1039 if (!(*win32_GetModuleInformation
) (current_process_handle
,
1044 DWORD err
= GetLastError ();
1045 error ("Can't get module info: (error %d): %s\n",
1046 (int) err
, strwinerror (err
));
1049 if ((DWORD
) (mi
.lpBaseOfDll
) == BaseAddress
)
1051 len
= (*win32_GetModuleFileNameExA
) (current_process_handle
,
1057 DWORD err
= GetLastError ();
1058 error ("Error getting dll name: (error %d): %s\n",
1059 (int) err
, strwinerror (err
));
1066 dll_name_ret
[0] = '\0';
1070 typedef HANDLE (WINAPI
*winapi_CreateToolhelp32Snapshot
) (DWORD
, DWORD
);
1071 typedef BOOL (WINAPI
*winapi_Module32First
) (HANDLE
, LPMODULEENTRY32
);
1072 typedef BOOL (WINAPI
*winapi_Module32Next
) (HANDLE
, LPMODULEENTRY32
);
1074 static winapi_CreateToolhelp32Snapshot win32_CreateToolhelp32Snapshot
;
1075 static winapi_Module32First win32_Module32First
;
1076 static winapi_Module32Next win32_Module32Next
;
1078 typedef BOOL (WINAPI
*winapi_CloseToolhelp32Snapshot
) (HANDLE
);
1079 static winapi_CloseToolhelp32Snapshot win32_CloseToolhelp32Snapshot
;
1083 load_toolhelp (void)
1085 static int toolhelp_loaded
= 0;
1086 static HMODULE dll
= NULL
;
1088 if (!toolhelp_loaded
)
1090 toolhelp_loaded
= 1;
1092 dll
= GetModuleHandle (_T("KERNEL32.DLL"));
1094 dll
= LoadLibrary (L
"TOOLHELP.DLL");
1099 win32_CreateToolhelp32Snapshot
=
1100 GETPROCADDRESS (dll
, CreateToolhelp32Snapshot
);
1101 win32_Module32First
= GETPROCADDRESS (dll
, Module32First
);
1102 win32_Module32Next
= GETPROCADDRESS (dll
, Module32Next
);
1104 win32_CloseToolhelp32Snapshot
=
1105 GETPROCADDRESS (dll
, CloseToolhelp32Snapshot
);
1109 return (win32_CreateToolhelp32Snapshot
!= NULL
1110 && win32_Module32First
!= NULL
1111 && win32_Module32Next
!= NULL
1113 && win32_CloseToolhelp32Snapshot
!= NULL
1119 toolhelp_get_dll_name (DWORD BaseAddress
, char *dll_name_ret
)
1121 HANDLE snapshot_module
;
1122 MODULEENTRY32 modEntry
= { sizeof (MODULEENTRY32
) };
1125 if (!load_toolhelp ())
1128 snapshot_module
= win32_CreateToolhelp32Snapshot (TH32CS_SNAPMODULE
,
1129 current_event
.dwProcessId
);
1130 if (snapshot_module
== INVALID_HANDLE_VALUE
)
1133 /* Ignore the first module, which is the exe. */
1134 if (win32_Module32First (snapshot_module
, &modEntry
))
1135 while (win32_Module32Next (snapshot_module
, &modEntry
))
1136 if ((DWORD
) modEntry
.modBaseAddr
== BaseAddress
)
1139 wcstombs (dll_name_ret
, modEntry
.szExePath
, MAX_PATH
+ 1);
1141 strcpy (dll_name_ret
, modEntry
.szExePath
);
1148 win32_CloseToolhelp32Snapshot (snapshot_module
);
1150 CloseHandle (snapshot_module
);
1156 handle_load_dll (void)
1158 LOAD_DLL_DEBUG_INFO
*event
= ¤t_event
.u
.LoadDll
;
1159 char dll_buf
[MAX_PATH
+ 1];
1160 char *dll_name
= NULL
;
1163 dll_buf
[0] = dll_buf
[sizeof (dll_buf
) - 1] = '\0';
1165 /* Windows does not report the image name of the dlls in the debug
1166 event on attaches. We resort to iterating over the list of
1167 loaded dlls looking for a match by image base. */
1168 if (!psapi_get_dll_name ((DWORD
) event
->lpBaseOfDll
, dll_buf
))
1170 if (!server_waiting
)
1171 /* On some versions of Windows and Windows CE, we can't create
1172 toolhelp snapshots while the inferior is stopped in a
1173 LOAD_DLL_DEBUG_EVENT due to a dll load, but we can while
1174 Windows is reporting the already loaded dlls. */
1175 toolhelp_get_dll_name ((DWORD
) event
->lpBaseOfDll
, dll_buf
);
1180 if (*dll_name
== '\0')
1181 dll_name
= get_image_name (current_process_handle
,
1182 event
->lpImageName
, event
->fUnicode
);
1186 /* The symbols in a dll are offset by 0x1000, which is the
1187 the offset from 0 of the first byte in an image - because
1188 of the file header and the section alignment. */
1190 load_addr
= (DWORD
) event
->lpBaseOfDll
+ 0x1000;
1191 win32_add_one_solib (dll_name
, load_addr
);
1195 handle_unload_dll (void)
1197 CORE_ADDR load_addr
=
1198 (CORE_ADDR
) (DWORD
) current_event
.u
.UnloadDll
.lpBaseOfDll
;
1199 load_addr
+= 0x1000;
1200 unloaded_dll (NULL
, load_addr
);
1204 handle_exception (struct target_waitstatus
*ourstatus
)
1206 DWORD code
= current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
;
1208 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
1212 case EXCEPTION_ACCESS_VIOLATION
:
1213 OUTMSG2 (("EXCEPTION_ACCESS_VIOLATION"));
1214 ourstatus
->value
.sig
= TARGET_SIGNAL_SEGV
;
1216 case STATUS_STACK_OVERFLOW
:
1217 OUTMSG2 (("STATUS_STACK_OVERFLOW"));
1218 ourstatus
->value
.sig
= TARGET_SIGNAL_SEGV
;
1220 case STATUS_FLOAT_DENORMAL_OPERAND
:
1221 OUTMSG2 (("STATUS_FLOAT_DENORMAL_OPERAND"));
1222 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1224 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
1225 OUTMSG2 (("EXCEPTION_ARRAY_BOUNDS_EXCEEDED"));
1226 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1228 case STATUS_FLOAT_INEXACT_RESULT
:
1229 OUTMSG2 (("STATUS_FLOAT_INEXACT_RESULT"));
1230 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1232 case STATUS_FLOAT_INVALID_OPERATION
:
1233 OUTMSG2 (("STATUS_FLOAT_INVALID_OPERATION"));
1234 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1236 case STATUS_FLOAT_OVERFLOW
:
1237 OUTMSG2 (("STATUS_FLOAT_OVERFLOW"));
1238 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1240 case STATUS_FLOAT_STACK_CHECK
:
1241 OUTMSG2 (("STATUS_FLOAT_STACK_CHECK"));
1242 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1244 case STATUS_FLOAT_UNDERFLOW
:
1245 OUTMSG2 (("STATUS_FLOAT_UNDERFLOW"));
1246 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1248 case STATUS_FLOAT_DIVIDE_BY_ZERO
:
1249 OUTMSG2 (("STATUS_FLOAT_DIVIDE_BY_ZERO"));
1250 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1252 case STATUS_INTEGER_DIVIDE_BY_ZERO
:
1253 OUTMSG2 (("STATUS_INTEGER_DIVIDE_BY_ZERO"));
1254 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1256 case STATUS_INTEGER_OVERFLOW
:
1257 OUTMSG2 (("STATUS_INTEGER_OVERFLOW"));
1258 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1260 case EXCEPTION_BREAKPOINT
:
1261 OUTMSG2 (("EXCEPTION_BREAKPOINT"));
1262 ourstatus
->value
.sig
= TARGET_SIGNAL_TRAP
;
1264 /* Remove the initial breakpoint. */
1265 check_breakpoints ((CORE_ADDR
) (long) current_event
1266 .u
.Exception
.ExceptionRecord
.ExceptionAddress
);
1270 OUTMSG2 (("DBG_CONTROL_C"));
1271 ourstatus
->value
.sig
= TARGET_SIGNAL_INT
;
1273 case DBG_CONTROL_BREAK
:
1274 OUTMSG2 (("DBG_CONTROL_BREAK"));
1275 ourstatus
->value
.sig
= TARGET_SIGNAL_INT
;
1277 case EXCEPTION_SINGLE_STEP
:
1278 OUTMSG2 (("EXCEPTION_SINGLE_STEP"));
1279 ourstatus
->value
.sig
= TARGET_SIGNAL_TRAP
;
1281 case EXCEPTION_ILLEGAL_INSTRUCTION
:
1282 OUTMSG2 (("EXCEPTION_ILLEGAL_INSTRUCTION"));
1283 ourstatus
->value
.sig
= TARGET_SIGNAL_ILL
;
1285 case EXCEPTION_PRIV_INSTRUCTION
:
1286 OUTMSG2 (("EXCEPTION_PRIV_INSTRUCTION"));
1287 ourstatus
->value
.sig
= TARGET_SIGNAL_ILL
;
1289 case EXCEPTION_NONCONTINUABLE_EXCEPTION
:
1290 OUTMSG2 (("EXCEPTION_NONCONTINUABLE_EXCEPTION"));
1291 ourstatus
->value
.sig
= TARGET_SIGNAL_ILL
;
1294 if (current_event
.u
.Exception
.dwFirstChance
)
1296 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1299 OUTMSG2 (("gdbserver: unknown target exception 0x%08lx at 0x%08lx",
1300 current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
,
1301 (DWORD
) current_event
.u
.Exception
.ExceptionRecord
.
1303 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
1307 last_sig
= ourstatus
->value
.sig
;
1312 suspend_one_thread (struct inferior_list_entry
*entry
)
1314 struct thread_info
*thread
= (struct thread_info
*) entry
;
1315 win32_thread_info
*th
= inferior_target_data (thread
);
1319 if (SuspendThread (th
->h
) == (DWORD
) -1)
1321 DWORD err
= GetLastError ();
1322 OUTMSG (("warning: SuspendThread failed in suspend_one_thread, "
1323 "(error %d): %s\n", (int) err
, strwinerror (err
)));
1331 fake_breakpoint_event (void)
1333 OUTMSG2(("fake_breakpoint_event\n"));
1335 faked_breakpoint
= 1;
1337 memset (¤t_event
, 0, sizeof (current_event
));
1338 current_event
.dwThreadId
= main_thread_id
;
1339 current_event
.dwDebugEventCode
= EXCEPTION_DEBUG_EVENT
;
1340 current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
1341 = EXCEPTION_BREAKPOINT
;
1343 for_each_inferior (&all_threads
, suspend_one_thread
);
1348 auto_delete_breakpoint (CORE_ADDR stop_pc
)
1354 /* Get the next event from the child. */
1357 get_child_debug_event (struct target_waitstatus
*ourstatus
)
1361 last_sig
= TARGET_SIGNAL_0
;
1362 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1364 /* Check if GDB sent us an interrupt request. */
1365 check_remote_input_interrupt_request ();
1367 if (soft_interrupt_requested
)
1369 soft_interrupt_requested
= 0;
1370 fake_breakpoint_event ();
1379 /* WinCE doesn't set an initial breakpoint automatically. To
1380 stop the inferior, we flush all currently pending debug
1381 events -- the thread list and the dll list are always
1382 reported immediatelly without delay, then, we suspend all
1383 threads and pretend we saw a trap at the current PC of the
1386 Contrary to desktop Windows, Windows CE *does* report the dll
1387 names on LOAD_DLL_DEBUG_EVENTs resulting from a
1388 DebugActiveProcess call. This limits the way we can detect
1389 if all the dlls have already been reported. If we get a real
1390 debug event before leaving attaching, the worst that will
1391 happen is the user will see a spurious breakpoint. */
1393 current_event
.dwDebugEventCode
= 0;
1394 if (!WaitForDebugEvent (¤t_event
, 0))
1396 OUTMSG2(("no attach events left\n"));
1397 fake_breakpoint_event ();
1401 OUTMSG2(("got attach event\n"));
1406 /* Keep the wait time low enough for confortable remote
1407 interruption, but high enough so gdbserver doesn't become a
1409 if (!WaitForDebugEvent (¤t_event
, 250))
1415 ptid
= debug_event_ptid (¤t_event
);
1417 (struct thread_info
*) find_inferior_id (&all_threads
, ptid
);
1419 switch (current_event
.dwDebugEventCode
)
1421 case CREATE_THREAD_DEBUG_EVENT
:
1422 OUTMSG2 (("gdbserver: kernel event CREATE_THREAD_DEBUG_EVENT "
1423 "for pid=%d tid=%x)\n",
1424 (unsigned) current_event
.dwProcessId
,
1425 (unsigned) current_event
.dwThreadId
));
1427 /* Record the existence of this thread. */
1428 child_add_thread (current_event
.dwProcessId
,
1429 current_event
.dwThreadId
,
1430 current_event
.u
.CreateThread
.hThread
);
1433 case EXIT_THREAD_DEBUG_EVENT
:
1434 OUTMSG2 (("gdbserver: kernel event EXIT_THREAD_DEBUG_EVENT "
1435 "for pid=%d tid=%x\n",
1436 (unsigned) current_event
.dwProcessId
,
1437 (unsigned) current_event
.dwThreadId
));
1438 child_delete_thread (current_event
.dwProcessId
,
1439 current_event
.dwThreadId
);
1442 case CREATE_PROCESS_DEBUG_EVENT
:
1443 OUTMSG2 (("gdbserver: kernel event CREATE_PROCESS_DEBUG_EVENT "
1444 "for pid=%d tid=%x\n",
1445 (unsigned) current_event
.dwProcessId
,
1446 (unsigned) current_event
.dwThreadId
));
1447 CloseHandle (current_event
.u
.CreateProcessInfo
.hFile
);
1449 current_process_handle
= current_event
.u
.CreateProcessInfo
.hProcess
;
1450 main_thread_id
= current_event
.dwThreadId
;
1452 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
1453 ourstatus
->value
.execd_pathname
= "Main executable";
1455 /* Add the main thread. */
1456 child_add_thread (current_event
.dwProcessId
,
1458 current_event
.u
.CreateProcessInfo
.hThread
);
1460 ourstatus
->value
.related_pid
= debug_event_ptid (¤t_event
);
1464 /* Windows CE doesn't set the initial breakpoint
1465 automatically like the desktop versions of Windows do.
1466 We add it explicitly here. It will be removed as soon as
1468 set_breakpoint_at ((CORE_ADDR
) (long) current_event
.u
1469 .CreateProcessInfo
.lpStartAddress
,
1470 auto_delete_breakpoint
);
1475 case EXIT_PROCESS_DEBUG_EVENT
:
1476 OUTMSG2 (("gdbserver: kernel event EXIT_PROCESS_DEBUG_EVENT "
1477 "for pid=%d tid=%x\n",
1478 (unsigned) current_event
.dwProcessId
,
1479 (unsigned) current_event
.dwThreadId
));
1480 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1481 ourstatus
->value
.integer
= current_event
.u
.ExitProcess
.dwExitCode
;
1482 child_continue (DBG_CONTINUE
, -1);
1483 CloseHandle (current_process_handle
);
1484 current_process_handle
= NULL
;
1487 case LOAD_DLL_DEBUG_EVENT
:
1488 OUTMSG2 (("gdbserver: kernel event LOAD_DLL_DEBUG_EVENT "
1489 "for pid=%d tid=%x\n",
1490 (unsigned) current_event
.dwProcessId
,
1491 (unsigned) current_event
.dwThreadId
));
1492 CloseHandle (current_event
.u
.LoadDll
.hFile
);
1495 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1496 ourstatus
->value
.sig
= TARGET_SIGNAL_TRAP
;
1499 case UNLOAD_DLL_DEBUG_EVENT
:
1500 OUTMSG2 (("gdbserver: kernel event UNLOAD_DLL_DEBUG_EVENT "
1501 "for pid=%d tid=%x\n",
1502 (unsigned) current_event
.dwProcessId
,
1503 (unsigned) current_event
.dwThreadId
));
1504 handle_unload_dll ();
1505 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1506 ourstatus
->value
.sig
= TARGET_SIGNAL_TRAP
;
1509 case EXCEPTION_DEBUG_EVENT
:
1510 OUTMSG2 (("gdbserver: kernel event EXCEPTION_DEBUG_EVENT "
1511 "for pid=%d tid=%x\n",
1512 (unsigned) current_event
.dwProcessId
,
1513 (unsigned) current_event
.dwThreadId
));
1514 handle_exception (ourstatus
);
1517 case OUTPUT_DEBUG_STRING_EVENT
:
1518 /* A message from the kernel (or Cygwin). */
1519 OUTMSG2 (("gdbserver: kernel event OUTPUT_DEBUG_STRING_EVENT "
1520 "for pid=%d tid=%x\n",
1521 (unsigned) current_event
.dwProcessId
,
1522 (unsigned) current_event
.dwThreadId
));
1523 handle_output_debug_string (ourstatus
);
1527 OUTMSG2 (("gdbserver: kernel event unknown "
1528 "for pid=%d tid=%x code=%ld\n",
1529 (unsigned) current_event
.dwProcessId
,
1530 (unsigned) current_event
.dwThreadId
,
1531 current_event
.dwDebugEventCode
));
1536 (struct thread_info
*) find_inferior_id (&all_threads
, ptid
);
1540 /* Wait for the inferior process to change state.
1541 STATUS will be filled in with a response code to send to GDB.
1542 Returns the signal which caused the process to stop. */
1544 win32_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
, int options
)
1546 struct process_info
*process
;
1550 if (!get_child_debug_event (ourstatus
))
1553 switch (ourstatus
->kind
)
1555 case TARGET_WAITKIND_EXITED
:
1556 OUTMSG2 (("Child exited with retcode = %x\n",
1557 ourstatus
->value
.integer
));
1559 process
= find_process_pid (current_process_id
);
1560 remove_process (process
);
1561 win32_clear_inferiors ();
1562 return pid_to_ptid (current_event
.dwProcessId
);
1563 case TARGET_WAITKIND_STOPPED
:
1564 case TARGET_WAITKIND_LOADED
:
1565 OUTMSG2 (("Child Stopped with signal = %d \n",
1566 our_status
.value
.sig
));
1568 child_fetch_inferior_registers (-1);
1570 if (ourstatus
->kind
== TARGET_WAITKIND_LOADED
1573 /* When gdb connects, we want to be stopped at the
1574 initial breakpoint, not in some dll load event. */
1575 child_continue (DBG_CONTINUE
, -1);
1579 /* We don't expose _LOADED events to gdbserver core. See
1580 the `dlls_changed' global. */
1581 if (ourstatus
->kind
== TARGET_WAITKIND_LOADED
)
1582 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
1584 return debug_event_ptid (¤t_event
);
1586 OUTMSG (("Ignoring unknown internal event, %d\n", ourstatus
->kind
));
1588 case TARGET_WAITKIND_SPURIOUS
:
1589 case TARGET_WAITKIND_EXECD
:
1590 /* do nothing, just continue */
1591 child_continue (DBG_CONTINUE
, -1);
1597 /* Fetch registers from the inferior process.
1598 If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO. */
1600 win32_fetch_inferior_registers (int regno
)
1602 child_fetch_inferior_registers (regno
);
1605 /* Store registers to the inferior process.
1606 If REGNO is -1, store all registers; otherwise, store at least REGNO. */
1608 win32_store_inferior_registers (int regno
)
1610 child_store_inferior_registers (regno
);
1613 /* Read memory from the inferior process. This should generally be
1614 called through read_inferior_memory, which handles breakpoint shadowing.
1615 Read LEN bytes at MEMADDR into a buffer at MYADDR. */
1617 win32_read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
1619 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 0, 0) != len
;
1622 /* Write memory to the inferior process. This should generally be
1623 called through write_inferior_memory, which handles breakpoint shadowing.
1624 Write LEN bytes from the buffer at MYADDR to MEMADDR.
1625 Returns 0 on success and errno on failure. */
1627 win32_write_inferior_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
,
1630 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 1, 0) != len
;
1633 /* Send an interrupt request to the inferior process. */
1635 win32_request_interrupt (void)
1637 winapi_DebugBreakProcess DebugBreakProcess
;
1638 winapi_GenerateConsoleCtrlEvent GenerateConsoleCtrlEvent
;
1641 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
1643 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
1646 GenerateConsoleCtrlEvent
= GETPROCADDRESS (dll
, GenerateConsoleCtrlEvent
);
1648 if (GenerateConsoleCtrlEvent
!= NULL
1649 && GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT
, current_process_id
))
1652 /* GenerateConsoleCtrlEvent can fail if process id being debugged is
1653 not a process group id.
1654 Fallback to XP/Vista 'DebugBreakProcess', which generates a
1655 breakpoint exception in the interior process. */
1657 DebugBreakProcess
= GETPROCADDRESS (dll
, DebugBreakProcess
);
1659 if (DebugBreakProcess
!= NULL
1660 && DebugBreakProcess (current_process_handle
))
1663 /* Last resort, suspend all threads manually. */
1664 soft_interrupt_requested
= 1;
1669 win32_error_to_fileio_error (DWORD err
)
1673 case ERROR_BAD_PATHNAME
:
1674 case ERROR_FILE_NOT_FOUND
:
1675 case ERROR_INVALID_NAME
:
1676 case ERROR_PATH_NOT_FOUND
:
1677 return FILEIO_ENOENT
;
1679 case ERROR_IO_DEVICE
:
1680 case ERROR_OPEN_FAILED
:
1682 case ERROR_INVALID_HANDLE
:
1683 return FILEIO_EBADF
;
1684 case ERROR_ACCESS_DENIED
:
1685 case ERROR_SHARING_VIOLATION
:
1686 return FILEIO_EACCES
;
1687 case ERROR_NOACCESS
:
1688 return FILEIO_EFAULT
;
1690 return FILEIO_EBUSY
;
1691 case ERROR_ALREADY_EXISTS
:
1692 case ERROR_FILE_EXISTS
:
1693 return FILEIO_EEXIST
;
1694 case ERROR_BAD_DEVICE
:
1695 return FILEIO_ENODEV
;
1696 case ERROR_DIRECTORY
:
1697 return FILEIO_ENOTDIR
;
1698 case ERROR_FILENAME_EXCED_RANGE
:
1699 case ERROR_INVALID_DATA
:
1700 case ERROR_INVALID_PARAMETER
:
1701 case ERROR_NEGATIVE_SEEK
:
1702 return FILEIO_EINVAL
;
1703 case ERROR_TOO_MANY_OPEN_FILES
:
1704 return FILEIO_EMFILE
;
1705 case ERROR_HANDLE_DISK_FULL
:
1706 case ERROR_DISK_FULL
:
1707 return FILEIO_ENOSPC
;
1708 case ERROR_WRITE_PROTECT
:
1709 return FILEIO_EROFS
;
1710 case ERROR_NOT_SUPPORTED
:
1711 return FILEIO_ENOSYS
;
1714 return FILEIO_EUNKNOWN
;
1718 wince_hostio_last_error (char *buf
)
1720 DWORD winerr
= GetLastError ();
1721 int fileio_err
= win32_error_to_fileio_error (winerr
);
1722 sprintf (buf
, "F-1,%x", fileio_err
);
1726 static struct target_ops win32_target_ops
= {
1727 win32_create_inferior
,
1735 win32_fetch_inferior_registers
,
1736 win32_store_inferior_registers
,
1737 win32_read_inferior_memory
,
1738 win32_write_inferior_memory
,
1740 win32_request_interrupt
,
1744 win32_stopped_by_watchpoint
,
1745 win32_stopped_data_address
,
1750 wince_hostio_last_error
,
1752 hostio_last_error_from_errno
,
1756 /* Initialize the Win32 backend. */
1758 initialize_low (void)
1760 set_target_ops (&win32_target_ops
);
1761 if (the_low_target
.breakpoint
!= NULL
)
1762 set_breakpoint_data (the_low_target
.breakpoint
,
1763 the_low_target
.breakpoint_len
);
1764 the_low_target
.arch_setup ();