1 /* Low level interface to Windows debugging, for gdbserver.
2 Copyright (C) 2006, 2007, 2008 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 enum target_signal last_sig
= TARGET_SIGNAL_0
;
74 /* The current debug event from WaitForDebugEvent. */
75 static DEBUG_EVENT current_event
;
77 /* Non zero if an interrupt request is to be satisfied by suspending
79 static int soft_interrupt_requested
= 0;
81 /* Non zero if the inferior is stopped in a simulated breakpoint done
82 by suspending all the threads. */
83 static int faked_breakpoint
= 0;
85 #define NUM_REGS (the_low_target.num_regs)
87 typedef BOOL
WINAPI (*winapi_DebugActiveProcessStop
) (DWORD dwProcessId
);
88 typedef BOOL
WINAPI (*winapi_DebugSetProcessKillOnExit
) (BOOL KillOnExit
);
89 typedef BOOL
WINAPI (*winapi_DebugBreakProcess
) (HANDLE
);
90 typedef BOOL
WINAPI (*winapi_GenerateConsoleCtrlEvent
) (DWORD
, DWORD
);
92 static DWORD main_thread_id
= 0;
94 static void win32_resume (struct thread_resume
*resume_info
);
96 /* Get the thread ID from the current selected inferior (the current
99 current_inferior_tid (void)
101 win32_thread_info
*th
= inferior_target_data (current_inferior
);
105 /* Get the thread context of the thread associated with TH. */
108 win32_get_thread_context (win32_thread_info
*th
)
110 memset (&th
->context
, 0, sizeof (CONTEXT
));
111 (*the_low_target
.get_thread_context
) (th
, ¤t_event
);
113 memcpy (&th
->base_context
, &th
->context
, sizeof (CONTEXT
));
117 /* Set the thread context of the thread associated with TH. */
120 win32_set_thread_context (win32_thread_info
*th
)
123 /* Calling SuspendThread on a thread that is running kernel code
124 will report that the suspending was successful, but in fact, that
125 will often not be true. In those cases, the context returned by
126 GetThreadContext will not be correct by the time the thread
127 stops, hence we can't set that context back into the thread when
128 resuming - it will most likelly crash the inferior.
129 Unfortunately, there is no way to know when the thread will
130 really stop. To work around it, we'll only write the context
131 back to the thread when either the user or GDB explicitly change
132 it between stopping and resuming. */
133 if (memcmp (&th
->context
, &th
->base_context
, sizeof (CONTEXT
)) != 0)
135 (*the_low_target
.set_thread_context
) (th
, ¤t_event
);
138 /* Find a thread record given a thread id. If GET_CONTEXT is set then
139 also retrieve the context for this thread. */
140 static win32_thread_info
*
141 thread_rec (DWORD id
, int get_context
)
143 struct thread_info
*thread
;
144 win32_thread_info
*th
;
146 thread
= (struct thread_info
*) find_inferior_id (&all_threads
, id
);
150 th
= inferior_target_data (thread
);
151 if (get_context
&& th
->context
.ContextFlags
== 0)
155 if (SuspendThread (th
->h
) == (DWORD
) -1)
157 DWORD err
= GetLastError ();
158 OUTMSG (("warning: SuspendThread failed in thread_rec, "
159 "(error %d): %s\n", (int) err
, strwinerror (err
)));
165 win32_get_thread_context (th
);
171 /* Add a thread to the thread list. */
172 static win32_thread_info
*
173 child_add_thread (DWORD tid
, HANDLE h
)
175 win32_thread_info
*th
;
177 if ((th
= thread_rec (tid
, FALSE
)))
180 th
= calloc (1, sizeof (*th
));
184 add_thread (tid
, th
, (unsigned int) tid
);
185 set_inferior_regcache_data ((struct thread_info
*)
186 find_inferior_id (&all_threads
, tid
),
187 new_register_cache ());
189 if (the_low_target
.thread_added
!= NULL
)
190 (*the_low_target
.thread_added
) (th
);
195 /* Delete a thread from the list of threads. */
197 delete_thread_info (struct inferior_list_entry
*thread
)
199 win32_thread_info
*th
= inferior_target_data ((struct thread_info
*) thread
);
201 remove_thread ((struct thread_info
*) thread
);
206 /* Delete a thread from the list of threads. */
208 child_delete_thread (DWORD id
)
210 struct inferior_list_entry
*thread
;
212 /* If the last thread is exiting, just return. */
213 if (all_threads
.head
== all_threads
.tail
)
216 thread
= find_inferior_id (&all_threads
, id
);
220 delete_thread_info (thread
);
223 /* Transfer memory from/to the debugged process. */
225 child_xfer_memory (CORE_ADDR memaddr
, char *our
, int len
,
226 int write
, struct target_ops
*target
)
229 long addr
= (long) memaddr
;
233 WriteProcessMemory (current_process_handle
, (LPVOID
) addr
,
234 (LPCVOID
) our
, len
, &done
);
235 FlushInstructionCache (current_process_handle
, (LPCVOID
) addr
, len
);
239 ReadProcessMemory (current_process_handle
, (LPCVOID
) addr
, (LPVOID
) our
,
245 /* Generally, what has the program done? */
248 /* The program has exited. The exit status is in value.integer. */
249 TARGET_WAITKIND_EXITED
,
251 /* The program has stopped with a signal. Which signal is in
253 TARGET_WAITKIND_STOPPED
,
255 /* The program is letting us know that it dynamically loaded
256 or unloaded something. */
257 TARGET_WAITKIND_LOADED
,
259 /* The program has exec'ed a new executable file. The new file's
260 pathname is pointed to by value.execd_pathname. */
261 TARGET_WAITKIND_EXECD
,
263 /* Nothing interesting happened, but we stopped anyway. We take the
264 chance to check if GDB requested an interrupt. */
265 TARGET_WAITKIND_SPURIOUS
,
268 struct target_waitstatus
270 enum target_waitkind kind
;
272 /* Forked child pid, execd pathname, exit status or signal number. */
276 enum target_signal sig
;
278 char *execd_pathname
;
284 /* Clear out any old thread list and reinitialize it to a pristine
287 child_init_thread_list (void)
289 for_each_inferior (&all_threads
, delete_thread_info
);
293 do_initial_child_stuff (DWORD pid
)
295 last_sig
= TARGET_SIGNAL_0
;
297 memset (¤t_event
, 0, sizeof (current_event
));
299 child_init_thread_list ();
301 if (the_low_target
.initial_stuff
!= NULL
)
302 (*the_low_target
.initial_stuff
) ();
305 /* Resume all artificially suspended threads if we are continuing
308 continue_one_thread (struct inferior_list_entry
*this_thread
, void *id_ptr
)
310 struct thread_info
*thread
= (struct thread_info
*) this_thread
;
311 int thread_id
= * (int *) id_ptr
;
312 win32_thread_info
*th
= inferior_target_data (thread
);
314 if ((thread_id
== -1 || thread_id
== th
->tid
)
317 if (th
->context
.ContextFlags
)
319 win32_set_thread_context (th
);
320 th
->context
.ContextFlags
= 0;
323 if (ResumeThread (th
->h
) == (DWORD
) -1)
325 DWORD err
= GetLastError ();
326 OUTMSG (("warning: ResumeThread failed in continue_one_thread, "
327 "(error %d): %s\n", (int) err
, strwinerror (err
)));
336 child_continue (DWORD continue_status
, int thread_id
)
338 /* The inferior will only continue after the ContinueDebugEvent
340 find_inferior (&all_threads
, continue_one_thread
, &thread_id
);
341 faked_breakpoint
= 0;
343 if (!ContinueDebugEvent (current_event
.dwProcessId
,
344 current_event
.dwThreadId
,
351 /* Fetch register(s) from the current thread context. */
353 child_fetch_inferior_registers (int r
)
356 win32_thread_info
*th
= thread_rec (current_inferior_tid (), TRUE
);
357 if (r
== -1 || r
== 0 || r
> NUM_REGS
)
358 child_fetch_inferior_registers (NUM_REGS
);
360 for (regno
= 0; regno
< r
; regno
++)
361 (*the_low_target
.fetch_inferior_register
) (th
, regno
);
364 /* Store a new register value into the current thread context. We don't
365 change the program's context until later, when we resume it. */
367 child_store_inferior_registers (int r
)
370 win32_thread_info
*th
= thread_rec (current_inferior_tid (), TRUE
);
371 if (r
== -1 || r
== 0 || r
> NUM_REGS
)
372 child_store_inferior_registers (NUM_REGS
);
374 for (regno
= 0; regno
< r
; regno
++)
375 (*the_low_target
.store_inferior_register
) (th
, regno
);
378 /* Map the Windows error number in ERROR to a locale-dependent error
379 message string and return a pointer to it. Typically, the values
380 for ERROR come from GetLastError.
382 The string pointed to shall not be modified by the application,
383 but may be overwritten by a subsequent call to strwinerror
385 The strwinerror function does not change the current setting
389 strwinerror (DWORD error
)
391 static char buf
[1024];
393 DWORD lasterr
= GetLastError ();
394 DWORD chars
= FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
395 | FORMAT_MESSAGE_ALLOCATE_BUFFER
,
398 0, /* Default language */
404 /* If there is an \r\n appended, zap it. */
406 && msgbuf
[chars
- 2] == '\r'
407 && msgbuf
[chars
- 1] == '\n')
413 if (chars
> ((COUNTOF (buf
)) - 1))
415 chars
= COUNTOF (buf
) - 1;
420 wcstombs (buf
, msgbuf
, chars
+ 1);
422 strncpy (buf
, msgbuf
, chars
+ 1);
427 sprintf (buf
, "unknown win32 error (%ld)", error
);
429 SetLastError (lasterr
);
434 create_process (const char *program
, char *args
,
435 DWORD flags
, PROCESS_INFORMATION
*pi
)
440 wchar_t *p
, *wprogram
, *wargs
;
443 wprogram
= alloca ((strlen (program
) + 1) * sizeof (wchar_t));
444 mbstowcs (wprogram
, program
, strlen (program
) + 1);
446 for (p
= wprogram
; *p
; ++p
)
450 argslen
= strlen (args
);
451 wargs
= alloca ((argslen
+ 1) * sizeof (wchar_t));
452 mbstowcs (wargs
, args
, argslen
+ 1);
454 ret
= CreateProcessW (wprogram
, /* image name */
455 wargs
, /* command line */
456 NULL
, /* security, not supported */
457 NULL
, /* thread, not supported */
458 FALSE
, /* inherit handles, not supported */
459 flags
, /* start flags */
460 NULL
, /* environment, not supported */
461 NULL
, /* current directory, not supported */
462 NULL
, /* start info, not supported */
465 STARTUPINFOA si
= { sizeof (STARTUPINFOA
) };
467 ret
= CreateProcessA (program
, /* image name */
468 args
, /* command line */
471 TRUE
, /* inherit handles */
472 flags
, /* start flags */
473 NULL
, /* environment */
474 NULL
, /* current directory */
475 &si
, /* start info */
482 /* Start a new process.
483 PROGRAM is a path to the program to execute.
484 ARGS is a standard NULL-terminated array of arguments,
485 to be passed to the inferior as ``argv''.
486 Returns the new PID on success, -1 on failure. Registers the new
487 process with the process list. */
489 win32_create_inferior (char *program
, char **program_args
)
492 char real_path
[MAXPATHLEN
];
493 char *orig_path
, *new_path
, *path_ptr
;
500 PROCESS_INFORMATION pi
;
503 /* win32_wait needs to know we're not attaching. */
507 error ("No executable specified, specify executable to debug.\n");
509 flags
= DEBUG_PROCESS
| DEBUG_ONLY_THIS_PROCESS
;
513 path_ptr
= getenv ("PATH");
516 orig_path
= alloca (strlen (path_ptr
) + 1);
517 new_path
= alloca (cygwin_posix_to_win32_path_list_buf_size (path_ptr
));
518 strcpy (orig_path
, path_ptr
);
519 cygwin_posix_to_win32_path_list (path_ptr
, new_path
);
520 setenv ("PATH", new_path
, 1);
522 cygwin_conv_to_win32_path (program
, real_path
);
527 for (argc
= 1; program_args
[argc
]; argc
++)
528 argslen
+= strlen (program_args
[argc
]) + 1;
529 args
= alloca (argslen
);
531 for (argc
= 1; program_args
[argc
]; argc
++)
533 /* FIXME: Can we do better about quoting? How does Cygwin
536 strcat (args
, program_args
[argc
]);
538 OUTMSG2 (("Command line is \"%s\"\n", args
));
540 #ifdef CREATE_NEW_PROCESS_GROUP
541 flags
|= CREATE_NEW_PROCESS_GROUP
;
544 ret
= create_process (program
, args
, flags
, &pi
);
545 err
= GetLastError ();
546 if (!ret
&& err
== ERROR_FILE_NOT_FOUND
)
548 char *exename
= alloca (strlen (program
) + 5);
549 strcat (strcpy (exename
, program
), ".exe");
550 ret
= create_process (exename
, args
, flags
, &pi
);
551 err
= GetLastError ();
556 setenv ("PATH", orig_path
, 1);
561 error ("Error creating process \"%s%s\", (error %d): %s\n",
562 program
, args
, (int) err
, strwinerror (err
));
566 OUTMSG2 (("Process created: %s\n", (char *) args
));
570 /* On Windows CE this handle can't be closed. The OS reuses
571 it in the debug events, while the 9x/NT versions of Windows
572 probably use a DuplicateHandle'd one. */
573 CloseHandle (pi
.hThread
);
576 current_process_handle
= pi
.hProcess
;
577 current_process_id
= pi
.dwProcessId
;
579 do_initial_child_stuff (current_process_id
);
581 return current_process_id
;
584 /* Attach to a running process.
585 PID is the process ID to attach to, specified by the user
586 or a higher layer. */
588 win32_attach (unsigned long pid
)
591 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
594 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
596 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
598 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
600 h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
603 if (DebugActiveProcess (pid
))
605 if (DebugSetProcessKillOnExit
!= NULL
)
606 DebugSetProcessKillOnExit (FALSE
);
608 /* win32_wait needs to know we're attaching. */
610 current_process_handle
= h
;
611 current_process_id
= pid
;
612 do_initial_child_stuff (pid
);
619 err
= GetLastError ();
620 error ("Attach to process failed (error %d): %s\n",
621 (int) err
, strwinerror (err
));
624 /* Handle OUTPUT_DEBUG_STRING_EVENT from child process. */
626 handle_output_debug_string (struct target_waitstatus
*ourstatus
)
628 #define READ_BUFFER_LEN 1024
630 char s
[READ_BUFFER_LEN
+ 1] = { 0 };
631 DWORD nbytes
= current_event
.u
.DebugString
.nDebugStringLength
;
636 if (nbytes
> READ_BUFFER_LEN
)
637 nbytes
= READ_BUFFER_LEN
;
639 addr
= (CORE_ADDR
) (size_t) current_event
.u
.DebugString
.lpDebugStringData
;
641 if (current_event
.u
.DebugString
.fUnicode
)
643 /* The event tells us how many bytes, not chars, even
645 WCHAR buffer
[(READ_BUFFER_LEN
+ 1) / sizeof (WCHAR
)] = { 0 };
646 if (read_inferior_memory (addr
, (unsigned char *) buffer
, nbytes
) != 0)
648 wcstombs (s
, buffer
, (nbytes
+ 1) / sizeof (WCHAR
));
652 if (read_inferior_memory (addr
, (unsigned char *) s
, nbytes
) != 0)
656 if (strncmp (s
, "cYg", 3) != 0)
666 #undef READ_BUFFER_LEN
669 /* Kill all inferiors. */
673 win32_thread_info
*current_thread
;
675 if (current_process_handle
== NULL
)
678 TerminateProcess (current_process_handle
, 0);
681 if (!child_continue (DBG_CONTINUE
, -1))
683 if (!WaitForDebugEvent (¤t_event
, INFINITE
))
685 if (current_event
.dwDebugEventCode
== EXIT_PROCESS_DEBUG_EVENT
)
687 else if (current_event
.dwDebugEventCode
== OUTPUT_DEBUG_STRING_EVENT
)
689 struct target_waitstatus our_status
= { 0 };
690 handle_output_debug_string (&our_status
);
694 CloseHandle (current_process_handle
);
696 current_thread
= inferior_target_data (current_inferior
);
697 if (current_thread
&& current_thread
->h
)
699 /* This may fail in an attached process, so don't check. */
700 (void) CloseHandle (current_thread
->h
);
704 /* Detach from all inferiors. */
710 winapi_DebugActiveProcessStop DebugActiveProcessStop
= NULL
;
711 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
713 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
715 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
717 DebugActiveProcessStop
= GETPROCADDRESS (dll
, DebugActiveProcessStop
);
718 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
720 if (DebugSetProcessKillOnExit
== NULL
721 || DebugActiveProcessStop
== NULL
)
724 /* We need a new handle, since DebugActiveProcessStop
725 closes all the ones that came through the events. */
726 if ((h
= OpenProcess (PROCESS_ALL_ACCESS
,
728 current_process_id
)) == NULL
)
730 /* The process died. */
735 struct thread_resume resume
;
739 resume
.leave_stopped
= 0;
740 win32_resume (&resume
);
743 if (!DebugActiveProcessStop (current_process_id
))
748 DebugSetProcessKillOnExit (FALSE
);
750 current_process_handle
= h
;
754 /* Wait for inferiors to end. */
758 if (current_process_id
== 0
759 || current_process_handle
== NULL
)
762 WaitForSingleObject (current_process_handle
, INFINITE
);
763 CloseHandle (current_process_handle
);
765 current_process_handle
= NULL
;
766 current_process_id
= 0;
769 /* Return 1 iff the thread with thread ID TID is alive. */
771 win32_thread_alive (unsigned long tid
)
775 /* Our thread list is reliable; don't bother to poll target
777 if (find_inferior_id (&all_threads
, tid
) != NULL
)
784 /* Resume the inferior process. RESUME_INFO describes how we want
787 win32_resume (struct thread_resume
*resume_info
)
790 enum target_signal sig
;
792 win32_thread_info
*th
;
793 DWORD continue_status
= DBG_CONTINUE
;
795 /* This handles the very limited set of resume packets that GDB can
796 currently produce. */
798 if (resume_info
[0].thread
== -1)
800 else if (resume_info
[1].thread
== -1 && !resume_info
[1].leave_stopped
)
803 /* Yes, we're ignoring resume_info[0].thread. It'd be tricky to make
804 the Windows resume code do the right thing for thread switching. */
805 tid
= current_event
.dwThreadId
;
807 if (resume_info
[0].thread
!= -1)
809 sig
= resume_info
[0].sig
;
810 step
= resume_info
[0].step
;
818 if (sig
!= TARGET_SIGNAL_0
)
820 if (current_event
.dwDebugEventCode
!= EXCEPTION_DEBUG_EVENT
)
822 OUTMSG (("Cannot continue with signal %d here.\n", sig
));
824 else if (sig
== last_sig
)
825 continue_status
= DBG_EXCEPTION_NOT_HANDLED
;
827 OUTMSG (("Can only continue with recieved signal %d.\n", last_sig
));
830 last_sig
= TARGET_SIGNAL_0
;
832 /* Get context for the currently selected thread. */
833 th
= thread_rec (current_event
.dwThreadId
, FALSE
);
836 if (th
->context
.ContextFlags
)
838 /* Move register values from the inferior into the thread
839 context structure. */
840 regcache_invalidate ();
844 if (the_low_target
.single_step
!= NULL
)
845 (*the_low_target
.single_step
) (th
);
847 error ("Single stepping is not supported "
848 "in this configuration.\n");
851 win32_set_thread_context (th
);
852 th
->context
.ContextFlags
= 0;
856 /* Allow continuing with the same signal that interrupted us.
857 Otherwise complain. */
859 child_continue (continue_status
, tid
);
863 win32_add_one_solib (const char *name
, CORE_ADDR load_addr
)
865 char buf
[MAX_PATH
+ 1];
866 char buf2
[MAX_PATH
+ 1];
869 WIN32_FIND_DATA w32_fd
;
870 WCHAR wname
[MAX_PATH
+ 1];
871 mbstowcs (wname
, name
, MAX_PATH
);
872 HANDLE h
= FindFirstFile (wname
, &w32_fd
);
874 WIN32_FIND_DATAA w32_fd
;
875 HANDLE h
= FindFirstFileA (name
, &w32_fd
);
878 if (h
== INVALID_HANDLE_VALUE
)
886 char cwd
[MAX_PATH
+ 1];
888 if (GetCurrentDirectoryA (MAX_PATH
+ 1, cwd
))
890 p
= strrchr (buf
, '\\');
893 SetCurrentDirectoryA (buf
);
894 GetFullPathNameA (w32_fd
.cFileName
, MAX_PATH
, buf
, &p
);
895 SetCurrentDirectoryA (cwd
);
902 cygwin_conv_to_posix_path (buf
, buf2
);
907 loaded_dll (buf2
, load_addr
);
911 get_image_name (HANDLE h
, void *address
, int unicode
)
913 static char buf
[(2 * MAX_PATH
) + 1];
914 DWORD size
= unicode
? sizeof (WCHAR
) : sizeof (char);
920 /* Attempt to read the name of the dll that was detected.
921 This is documented to work only when actively debugging
922 a program. It will not work for attached processes. */
927 /* Windows CE reports the address of the image name,
928 instead of an address of a pointer into the image name. */
929 address_ptr
= address
;
931 /* See if we could read the address of a string, and that the
932 address isn't null. */
933 if (!ReadProcessMemory (h
, address
, &address_ptr
,
934 sizeof (address_ptr
), &done
)
935 || done
!= sizeof (address_ptr
)
940 /* Find the length of the string */
941 while (ReadProcessMemory (h
, address_ptr
+ len
++ * size
, &b
, size
, &done
)
942 && (b
[0] != 0 || b
[size
- 1] != 0) && done
== size
)
946 ReadProcessMemory (h
, address_ptr
, buf
, len
, &done
);
949 WCHAR
*unicode_address
= (WCHAR
*) alloca (len
* sizeof (WCHAR
));
950 ReadProcessMemory (h
, address_ptr
, unicode_address
, len
* sizeof (WCHAR
),
953 WideCharToMultiByte (CP_ACP
, 0, unicode_address
, len
, buf
, len
, 0, 0);
959 typedef BOOL (WINAPI
*winapi_EnumProcessModules
) (HANDLE
, HMODULE
*,
961 typedef BOOL (WINAPI
*winapi_GetModuleInformation
) (HANDLE
, HMODULE
,
962 LPMODULEINFO
, DWORD
);
963 typedef DWORD (WINAPI
*winapi_GetModuleFileNameExA
) (HANDLE
, HMODULE
,
966 static winapi_EnumProcessModules win32_EnumProcessModules
;
967 static winapi_GetModuleInformation win32_GetModuleInformation
;
968 static winapi_GetModuleFileNameExA win32_GetModuleFileNameExA
;
973 static int psapi_loaded
= 0;
974 static HMODULE dll
= NULL
;
979 dll
= LoadLibrary (TEXT("psapi.dll"));
982 win32_EnumProcessModules
=
983 GETPROCADDRESS (dll
, EnumProcessModules
);
984 win32_GetModuleInformation
=
985 GETPROCADDRESS (dll
, GetModuleInformation
);
986 win32_GetModuleFileNameExA
=
987 GETPROCADDRESS (dll
, GetModuleFileNameExA
);
990 return (win32_EnumProcessModules
!= NULL
991 && win32_GetModuleInformation
!= NULL
992 && win32_GetModuleFileNameExA
!= NULL
);
996 psapi_get_dll_name (DWORD BaseAddress
, char *dll_name_ret
)
1002 HMODULE
*DllHandle
= dh_buf
;
1010 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1015 if (!ok
|| !cbNeeded
)
1018 DllHandle
= (HMODULE
*) alloca (cbNeeded
);
1022 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1029 for (i
= 0; i
< ((size_t) cbNeeded
/ sizeof (HMODULE
)); i
++)
1031 if (!(*win32_GetModuleInformation
) (current_process_handle
,
1036 DWORD err
= GetLastError ();
1037 error ("Can't get module info: (error %d): %s\n",
1038 (int) err
, strwinerror (err
));
1041 if ((DWORD
) (mi
.lpBaseOfDll
) == BaseAddress
)
1043 len
= (*win32_GetModuleFileNameExA
) (current_process_handle
,
1049 DWORD err
= GetLastError ();
1050 error ("Error getting dll name: (error %d): %s\n",
1051 (int) err
, strwinerror (err
));
1058 dll_name_ret
[0] = '\0';
1062 typedef HANDLE (WINAPI
*winapi_CreateToolhelp32Snapshot
) (DWORD
, DWORD
);
1063 typedef BOOL (WINAPI
*winapi_Module32First
) (HANDLE
, LPMODULEENTRY32
);
1064 typedef BOOL (WINAPI
*winapi_Module32Next
) (HANDLE
, LPMODULEENTRY32
);
1066 static winapi_CreateToolhelp32Snapshot win32_CreateToolhelp32Snapshot
;
1067 static winapi_Module32First win32_Module32First
;
1068 static winapi_Module32Next win32_Module32Next
;
1070 typedef BOOL (WINAPI
*winapi_CloseToolhelp32Snapshot
) (HANDLE
);
1071 static winapi_CloseToolhelp32Snapshot win32_CloseToolhelp32Snapshot
;
1075 load_toolhelp (void)
1077 static int toolhelp_loaded
= 0;
1078 static HMODULE dll
= NULL
;
1080 if (!toolhelp_loaded
)
1082 toolhelp_loaded
= 1;
1084 dll
= GetModuleHandle (_T("KERNEL32.DLL"));
1086 dll
= LoadLibrary (L
"TOOLHELP.DLL");
1091 win32_CreateToolhelp32Snapshot
=
1092 GETPROCADDRESS (dll
, CreateToolhelp32Snapshot
);
1093 win32_Module32First
= GETPROCADDRESS (dll
, Module32First
);
1094 win32_Module32Next
= GETPROCADDRESS (dll
, Module32Next
);
1096 win32_CloseToolhelp32Snapshot
=
1097 GETPROCADDRESS (dll
, CloseToolhelp32Snapshot
);
1101 return (win32_CreateToolhelp32Snapshot
!= NULL
1102 && win32_Module32First
!= NULL
1103 && win32_Module32Next
!= NULL
1105 && win32_CloseToolhelp32Snapshot
!= NULL
1111 toolhelp_get_dll_name (DWORD BaseAddress
, char *dll_name_ret
)
1113 HANDLE snapshot_module
;
1114 MODULEENTRY32 modEntry
= { sizeof (MODULEENTRY32
) };
1117 if (!load_toolhelp ())
1120 snapshot_module
= win32_CreateToolhelp32Snapshot (TH32CS_SNAPMODULE
,
1121 current_event
.dwProcessId
);
1122 if (snapshot_module
== INVALID_HANDLE_VALUE
)
1125 /* Ignore the first module, which is the exe. */
1126 if (win32_Module32First (snapshot_module
, &modEntry
))
1127 while (win32_Module32Next (snapshot_module
, &modEntry
))
1128 if ((DWORD
) modEntry
.modBaseAddr
== BaseAddress
)
1131 wcstombs (dll_name_ret
, modEntry
.szExePath
, MAX_PATH
+ 1);
1133 strcpy (dll_name_ret
, modEntry
.szExePath
);
1140 win32_CloseToolhelp32Snapshot (snapshot_module
);
1142 CloseHandle (snapshot_module
);
1148 handle_load_dll (void)
1150 LOAD_DLL_DEBUG_INFO
*event
= ¤t_event
.u
.LoadDll
;
1151 char dll_buf
[MAX_PATH
+ 1];
1152 char *dll_name
= NULL
;
1155 dll_buf
[0] = dll_buf
[sizeof (dll_buf
) - 1] = '\0';
1157 /* Windows does not report the image name of the dlls in the debug
1158 event on attaches. We resort to iterating over the list of
1159 loaded dlls looking for a match by image base. */
1160 if (!psapi_get_dll_name ((DWORD
) event
->lpBaseOfDll
, dll_buf
))
1162 if (!server_waiting
)
1163 /* On some versions of Windows and Windows CE, we can't create
1164 toolhelp snapshots while the inferior is stopped in a
1165 LOAD_DLL_DEBUG_EVENT due to a dll load, but we can while
1166 Windows is reporting the already loaded dlls. */
1167 toolhelp_get_dll_name ((DWORD
) event
->lpBaseOfDll
, dll_buf
);
1172 if (*dll_name
== '\0')
1173 dll_name
= get_image_name (current_process_handle
,
1174 event
->lpImageName
, event
->fUnicode
);
1178 /* The symbols in a dll are offset by 0x1000, which is the
1179 the offset from 0 of the first byte in an image - because
1180 of the file header and the section alignment. */
1182 load_addr
= (DWORD
) event
->lpBaseOfDll
+ 0x1000;
1183 win32_add_one_solib (dll_name
, load_addr
);
1187 handle_unload_dll (void)
1189 CORE_ADDR load_addr
=
1190 (CORE_ADDR
) (DWORD
) current_event
.u
.UnloadDll
.lpBaseOfDll
;
1191 load_addr
+= 0x1000;
1192 unloaded_dll (NULL
, load_addr
);
1196 handle_exception (struct target_waitstatus
*ourstatus
)
1198 DWORD code
= current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
;
1200 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
1204 case EXCEPTION_ACCESS_VIOLATION
:
1205 OUTMSG2 (("EXCEPTION_ACCESS_VIOLATION"));
1206 ourstatus
->value
.sig
= TARGET_SIGNAL_SEGV
;
1208 case STATUS_STACK_OVERFLOW
:
1209 OUTMSG2 (("STATUS_STACK_OVERFLOW"));
1210 ourstatus
->value
.sig
= TARGET_SIGNAL_SEGV
;
1212 case STATUS_FLOAT_DENORMAL_OPERAND
:
1213 OUTMSG2 (("STATUS_FLOAT_DENORMAL_OPERAND"));
1214 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1216 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
1217 OUTMSG2 (("EXCEPTION_ARRAY_BOUNDS_EXCEEDED"));
1218 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1220 case STATUS_FLOAT_INEXACT_RESULT
:
1221 OUTMSG2 (("STATUS_FLOAT_INEXACT_RESULT"));
1222 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1224 case STATUS_FLOAT_INVALID_OPERATION
:
1225 OUTMSG2 (("STATUS_FLOAT_INVALID_OPERATION"));
1226 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1228 case STATUS_FLOAT_OVERFLOW
:
1229 OUTMSG2 (("STATUS_FLOAT_OVERFLOW"));
1230 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1232 case STATUS_FLOAT_STACK_CHECK
:
1233 OUTMSG2 (("STATUS_FLOAT_STACK_CHECK"));
1234 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1236 case STATUS_FLOAT_UNDERFLOW
:
1237 OUTMSG2 (("STATUS_FLOAT_UNDERFLOW"));
1238 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1240 case STATUS_FLOAT_DIVIDE_BY_ZERO
:
1241 OUTMSG2 (("STATUS_FLOAT_DIVIDE_BY_ZERO"));
1242 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1244 case STATUS_INTEGER_DIVIDE_BY_ZERO
:
1245 OUTMSG2 (("STATUS_INTEGER_DIVIDE_BY_ZERO"));
1246 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1248 case STATUS_INTEGER_OVERFLOW
:
1249 OUTMSG2 (("STATUS_INTEGER_OVERFLOW"));
1250 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1252 case EXCEPTION_BREAKPOINT
:
1253 OUTMSG2 (("EXCEPTION_BREAKPOINT"));
1254 ourstatus
->value
.sig
= TARGET_SIGNAL_TRAP
;
1256 /* Remove the initial breakpoint. */
1257 check_breakpoints ((CORE_ADDR
) (long) current_event
1258 .u
.Exception
.ExceptionRecord
.ExceptionAddress
);
1262 OUTMSG2 (("DBG_CONTROL_C"));
1263 ourstatus
->value
.sig
= TARGET_SIGNAL_INT
;
1265 case DBG_CONTROL_BREAK
:
1266 OUTMSG2 (("DBG_CONTROL_BREAK"));
1267 ourstatus
->value
.sig
= TARGET_SIGNAL_INT
;
1269 case EXCEPTION_SINGLE_STEP
:
1270 OUTMSG2 (("EXCEPTION_SINGLE_STEP"));
1271 ourstatus
->value
.sig
= TARGET_SIGNAL_TRAP
;
1273 case EXCEPTION_ILLEGAL_INSTRUCTION
:
1274 OUTMSG2 (("EXCEPTION_ILLEGAL_INSTRUCTION"));
1275 ourstatus
->value
.sig
= TARGET_SIGNAL_ILL
;
1277 case EXCEPTION_PRIV_INSTRUCTION
:
1278 OUTMSG2 (("EXCEPTION_PRIV_INSTRUCTION"));
1279 ourstatus
->value
.sig
= TARGET_SIGNAL_ILL
;
1281 case EXCEPTION_NONCONTINUABLE_EXCEPTION
:
1282 OUTMSG2 (("EXCEPTION_NONCONTINUABLE_EXCEPTION"));
1283 ourstatus
->value
.sig
= TARGET_SIGNAL_ILL
;
1286 if (current_event
.u
.Exception
.dwFirstChance
)
1288 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1291 OUTMSG2 (("gdbserver: unknown target exception 0x%08lx at 0x%08lx",
1292 current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
,
1293 (DWORD
) current_event
.u
.Exception
.ExceptionRecord
.
1295 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
1299 last_sig
= ourstatus
->value
.sig
;
1304 suspend_one_thread (struct inferior_list_entry
*entry
)
1306 struct thread_info
*thread
= (struct thread_info
*) entry
;
1307 win32_thread_info
*th
= inferior_target_data (thread
);
1311 if (SuspendThread (th
->h
) == (DWORD
) -1)
1313 DWORD err
= GetLastError ();
1314 OUTMSG (("warning: SuspendThread failed in suspend_one_thread, "
1315 "(error %d): %s\n", (int) err
, strwinerror (err
)));
1323 fake_breakpoint_event (void)
1325 OUTMSG2(("fake_breakpoint_event\n"));
1327 faked_breakpoint
= 1;
1329 memset (¤t_event
, 0, sizeof (current_event
));
1330 current_event
.dwThreadId
= main_thread_id
;
1331 current_event
.dwDebugEventCode
= EXCEPTION_DEBUG_EVENT
;
1332 current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
1333 = EXCEPTION_BREAKPOINT
;
1335 for_each_inferior (&all_threads
, suspend_one_thread
);
1340 auto_delete_breakpoint (CORE_ADDR stop_pc
)
1346 /* Get the next event from the child. */
1349 get_child_debug_event (struct target_waitstatus
*ourstatus
)
1351 last_sig
= TARGET_SIGNAL_0
;
1352 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1354 /* Check if GDB sent us an interrupt request. */
1355 check_remote_input_interrupt_request ();
1357 if (soft_interrupt_requested
)
1359 soft_interrupt_requested
= 0;
1360 fake_breakpoint_event ();
1369 /* WinCE doesn't set an initial breakpoint automatically. To
1370 stop the inferior, we flush all currently pending debug
1371 events -- the thread list and the dll list are always
1372 reported immediatelly without delay, then, we suspend all
1373 threads and pretend we saw a trap at the current PC of the
1376 Contrary to desktop Windows, Windows CE *does* report the dll
1377 names on LOAD_DLL_DEBUG_EVENTs resulting from a
1378 DebugActiveProcess call. This limits the way we can detect
1379 if all the dlls have already been reported. If we get a real
1380 debug event before leaving attaching, the worst that will
1381 happen is the user will see a spurious breakpoint. */
1383 current_event
.dwDebugEventCode
= 0;
1384 if (!WaitForDebugEvent (¤t_event
, 0))
1386 OUTMSG2(("no attach events left\n"));
1387 fake_breakpoint_event ();
1391 OUTMSG2(("got attach event\n"));
1396 /* Keep the wait time low enough for confortable remote
1397 interruption, but high enough so gdbserver doesn't become a
1399 if (!WaitForDebugEvent (¤t_event
, 250))
1406 (struct thread_info
*) find_inferior_id (&all_threads
,
1407 current_event
.dwThreadId
);
1409 switch (current_event
.dwDebugEventCode
)
1411 case CREATE_THREAD_DEBUG_EVENT
:
1412 OUTMSG2 (("gdbserver: kernel event CREATE_THREAD_DEBUG_EVENT "
1413 "for pid=%d tid=%x)\n",
1414 (unsigned) current_event
.dwProcessId
,
1415 (unsigned) current_event
.dwThreadId
));
1417 /* Record the existence of this thread. */
1418 child_add_thread (current_event
.dwThreadId
,
1419 current_event
.u
.CreateThread
.hThread
);
1422 case EXIT_THREAD_DEBUG_EVENT
:
1423 OUTMSG2 (("gdbserver: kernel event EXIT_THREAD_DEBUG_EVENT "
1424 "for pid=%d tid=%x\n",
1425 (unsigned) current_event
.dwProcessId
,
1426 (unsigned) current_event
.dwThreadId
));
1427 child_delete_thread (current_event
.dwThreadId
);
1430 case CREATE_PROCESS_DEBUG_EVENT
:
1431 OUTMSG2 (("gdbserver: kernel event CREATE_PROCESS_DEBUG_EVENT "
1432 "for pid=%d tid=%x\n",
1433 (unsigned) current_event
.dwProcessId
,
1434 (unsigned) current_event
.dwThreadId
));
1435 CloseHandle (current_event
.u
.CreateProcessInfo
.hFile
);
1437 current_process_handle
= current_event
.u
.CreateProcessInfo
.hProcess
;
1438 main_thread_id
= current_event
.dwThreadId
;
1440 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
1441 ourstatus
->value
.execd_pathname
= "Main executable";
1443 /* Add the main thread. */
1444 child_add_thread (main_thread_id
,
1445 current_event
.u
.CreateProcessInfo
.hThread
);
1447 ourstatus
->value
.related_pid
= current_event
.dwThreadId
;
1451 /* Windows CE doesn't set the initial breakpoint
1452 automatically like the desktop versions of Windows do.
1453 We add it explicitly here. It will be removed as soon as
1455 set_breakpoint_at ((CORE_ADDR
) (long) current_event
.u
1456 .CreateProcessInfo
.lpStartAddress
,
1457 auto_delete_breakpoint
);
1462 case EXIT_PROCESS_DEBUG_EVENT
:
1463 OUTMSG2 (("gdbserver: kernel event EXIT_PROCESS_DEBUG_EVENT "
1464 "for pid=%d tid=%x\n",
1465 (unsigned) current_event
.dwProcessId
,
1466 (unsigned) current_event
.dwThreadId
));
1467 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1468 ourstatus
->value
.integer
= current_event
.u
.ExitProcess
.dwExitCode
;
1469 CloseHandle (current_process_handle
);
1470 current_process_handle
= NULL
;
1473 case LOAD_DLL_DEBUG_EVENT
:
1474 OUTMSG2 (("gdbserver: kernel event LOAD_DLL_DEBUG_EVENT "
1475 "for pid=%d tid=%x\n",
1476 (unsigned) current_event
.dwProcessId
,
1477 (unsigned) current_event
.dwThreadId
));
1478 CloseHandle (current_event
.u
.LoadDll
.hFile
);
1481 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1482 ourstatus
->value
.sig
= TARGET_SIGNAL_TRAP
;
1485 case UNLOAD_DLL_DEBUG_EVENT
:
1486 OUTMSG2 (("gdbserver: kernel event UNLOAD_DLL_DEBUG_EVENT "
1487 "for pid=%d tid=%x\n",
1488 (unsigned) current_event
.dwProcessId
,
1489 (unsigned) current_event
.dwThreadId
));
1490 handle_unload_dll ();
1491 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1492 ourstatus
->value
.sig
= TARGET_SIGNAL_TRAP
;
1495 case EXCEPTION_DEBUG_EVENT
:
1496 OUTMSG2 (("gdbserver: kernel event EXCEPTION_DEBUG_EVENT "
1497 "for pid=%d tid=%x\n",
1498 (unsigned) current_event
.dwProcessId
,
1499 (unsigned) current_event
.dwThreadId
));
1500 handle_exception (ourstatus
);
1503 case OUTPUT_DEBUG_STRING_EVENT
:
1504 /* A message from the kernel (or Cygwin). */
1505 OUTMSG2 (("gdbserver: kernel event OUTPUT_DEBUG_STRING_EVENT "
1506 "for pid=%d tid=%x\n",
1507 (unsigned) current_event
.dwProcessId
,
1508 (unsigned) current_event
.dwThreadId
));
1509 handle_output_debug_string (ourstatus
);
1513 OUTMSG2 (("gdbserver: kernel event unknown "
1514 "for pid=%d tid=%x code=%ld\n",
1515 (unsigned) current_event
.dwProcessId
,
1516 (unsigned) current_event
.dwThreadId
,
1517 current_event
.dwDebugEventCode
));
1522 (struct thread_info
*) find_inferior_id (&all_threads
,
1523 current_event
.dwThreadId
);
1527 /* Wait for the inferior process to change state.
1528 STATUS will be filled in with a response code to send to GDB.
1529 Returns the signal which caused the process to stop. */
1530 static unsigned char
1531 win32_wait (char *status
)
1533 struct target_waitstatus our_status
;
1539 if (!get_child_debug_event (&our_status
))
1542 switch (our_status
.kind
)
1544 case TARGET_WAITKIND_EXITED
:
1545 OUTMSG2 (("Child exited with retcode = %x\n",
1546 our_status
.value
.integer
));
1549 return our_status
.value
.integer
;
1550 case TARGET_WAITKIND_STOPPED
:
1551 case TARGET_WAITKIND_LOADED
:
1552 OUTMSG2 (("Child Stopped with signal = %d \n",
1553 our_status
.value
.sig
));
1557 child_fetch_inferior_registers (-1);
1559 if (our_status
.kind
== TARGET_WAITKIND_LOADED
1562 /* When gdb connects, we want to be stopped at the
1563 initial breakpoint, not in some dll load event. */
1564 child_continue (DBG_CONTINUE
, -1);
1568 return our_status
.value
.sig
;
1570 OUTMSG (("Ignoring unknown internal event, %d\n", our_status
.kind
));
1572 case TARGET_WAITKIND_SPURIOUS
:
1573 case TARGET_WAITKIND_EXECD
:
1574 /* do nothing, just continue */
1575 child_continue (DBG_CONTINUE
, -1);
1581 /* Fetch registers from the inferior process.
1582 If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO. */
1584 win32_fetch_inferior_registers (int regno
)
1586 child_fetch_inferior_registers (regno
);
1589 /* Store registers to the inferior process.
1590 If REGNO is -1, store all registers; otherwise, store at least REGNO. */
1592 win32_store_inferior_registers (int regno
)
1594 child_store_inferior_registers (regno
);
1597 /* Read memory from the inferior process. This should generally be
1598 called through read_inferior_memory, which handles breakpoint shadowing.
1599 Read LEN bytes at MEMADDR into a buffer at MYADDR. */
1601 win32_read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
1603 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 0, 0) != len
;
1606 /* Write memory to the inferior process. This should generally be
1607 called through write_inferior_memory, which handles breakpoint shadowing.
1608 Write LEN bytes from the buffer at MYADDR to MEMADDR.
1609 Returns 0 on success and errno on failure. */
1611 win32_write_inferior_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
,
1614 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 1, 0) != len
;
1617 /* Send an interrupt request to the inferior process. */
1619 win32_request_interrupt (void)
1621 winapi_DebugBreakProcess DebugBreakProcess
;
1622 winapi_GenerateConsoleCtrlEvent GenerateConsoleCtrlEvent
;
1625 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
1627 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
1630 GenerateConsoleCtrlEvent
= GETPROCADDRESS (dll
, GenerateConsoleCtrlEvent
);
1632 if (GenerateConsoleCtrlEvent
!= NULL
1633 && GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT
, current_process_id
))
1636 /* GenerateConsoleCtrlEvent can fail if process id being debugged is
1637 not a process group id.
1638 Fallback to XP/Vista 'DebugBreakProcess', which generates a
1639 breakpoint exception in the interior process. */
1641 DebugBreakProcess
= GETPROCADDRESS (dll
, DebugBreakProcess
);
1643 if (DebugBreakProcess
!= NULL
1644 && DebugBreakProcess (current_process_handle
))
1647 /* Last resort, suspend all threads manually. */
1648 soft_interrupt_requested
= 1;
1652 win32_arch_string (void)
1654 return the_low_target
.arch_string
;
1659 win32_error_to_fileio_error (DWORD err
)
1663 case ERROR_BAD_PATHNAME
:
1664 case ERROR_FILE_NOT_FOUND
:
1665 case ERROR_INVALID_NAME
:
1666 case ERROR_PATH_NOT_FOUND
:
1667 return FILEIO_ENOENT
;
1669 case ERROR_IO_DEVICE
:
1670 case ERROR_OPEN_FAILED
:
1672 case ERROR_INVALID_HANDLE
:
1673 return FILEIO_EBADF
;
1674 case ERROR_ACCESS_DENIED
:
1675 case ERROR_SHARING_VIOLATION
:
1676 return FILEIO_EACCES
;
1677 case ERROR_NOACCESS
:
1678 return FILEIO_EFAULT
;
1680 return FILEIO_EBUSY
;
1681 case ERROR_ALREADY_EXISTS
:
1682 case ERROR_FILE_EXISTS
:
1683 return FILEIO_EEXIST
;
1684 case ERROR_BAD_DEVICE
:
1685 return FILEIO_ENODEV
;
1686 case ERROR_DIRECTORY
:
1687 return FILEIO_ENOTDIR
;
1688 case ERROR_FILENAME_EXCED_RANGE
:
1689 case ERROR_INVALID_DATA
:
1690 case ERROR_INVALID_PARAMETER
:
1691 case ERROR_NEGATIVE_SEEK
:
1692 return FILEIO_EINVAL
;
1693 case ERROR_TOO_MANY_OPEN_FILES
:
1694 return FILEIO_EMFILE
;
1695 case ERROR_HANDLE_DISK_FULL
:
1696 case ERROR_DISK_FULL
:
1697 return FILEIO_ENOSPC
;
1698 case ERROR_WRITE_PROTECT
:
1699 return FILEIO_EROFS
;
1700 case ERROR_NOT_SUPPORTED
:
1701 return FILEIO_ENOSYS
;
1704 return FILEIO_EUNKNOWN
;
1708 wince_hostio_last_error (char *buf
)
1710 DWORD winerr
= GetLastError ();
1711 int fileio_err
= win32_error_to_fileio_error (winerr
);
1712 sprintf (buf
, "F-1,%x", fileio_err
);
1716 static struct target_ops win32_target_ops
= {
1717 win32_create_inferior
,
1725 win32_fetch_inferior_registers
,
1726 win32_store_inferior_registers
,
1727 win32_read_inferior_memory
,
1728 win32_write_inferior_memory
,
1730 win32_request_interrupt
,
1741 wince_hostio_last_error
,
1743 hostio_last_error_from_errno
,
1747 /* Initialize the Win32 backend. */
1749 initialize_low (void)
1751 set_target_ops (&win32_target_ops
);
1752 if (the_low_target
.breakpoint
!= NULL
)
1753 set_breakpoint_data (the_low_target
.breakpoint
,
1754 the_low_target
.breakpoint_len
);