1 /* Low level interface to Windows debugging, for gdbserver.
2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
5 Contributed by Leo Zayas. Based on "win32-nat.c" from GDB.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "gdb/signals.h"
25 #include "gdb/fileio.h"
26 #include "mem-break.h"
27 #include "win32-low.h"
34 #include <sys/param.h>
38 #include <sys/cygwin.h>
41 #define OUTMSG(X) do { printf X; fflush (stderr); } while (0)
54 #define _T(x) TEXT (x)
58 #define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0]))
62 # define GETPROCADDRESS(DLL, PROC) \
63 ((winapi_ ## PROC) GetProcAddress (DLL, TEXT (#PROC)))
65 # define GETPROCADDRESS(DLL, PROC) \
66 ((winapi_ ## PROC) GetProcAddress (DLL, #PROC))
69 int using_threads = 1;
72 static int attaching = 0;
73 static HANDLE current_process_handle = NULL;
74 static DWORD current_process_id = 0;
75 static DWORD main_thread_id = 0;
76 static enum target_signal last_sig = TARGET_SIGNAL_0;
78 /* The current debug event from WaitForDebugEvent. */
79 static DEBUG_EVENT current_event;
81 /* Non zero if an interrupt request is to be satisfied by suspending
83 static int soft_interrupt_requested = 0;
85 /* Non zero if the inferior is stopped in a simulated breakpoint done
86 by suspending all the threads. */
87 static int faked_breakpoint = 0;
89 #define NUM_REGS (the_low_target.num_regs)
91 typedef BOOL WINAPI (*winapi_DebugActiveProcessStop) (DWORD dwProcessId);
92 typedef BOOL WINAPI (*winapi_DebugSetProcessKillOnExit) (BOOL KillOnExit);
93 typedef BOOL WINAPI (*winapi_DebugBreakProcess) (HANDLE);
94 typedef BOOL WINAPI (*winapi_GenerateConsoleCtrlEvent) (DWORD, DWORD);
96 static void win32_resume (struct thread_resume *resume_info, size_t n);
98 /* Get the thread ID from the current selected inferior (the current
101 current_inferior_ptid (void)
103 return ((struct inferior_list_entry*) current_inferior)->id;
106 /* The current debug event from WaitForDebugEvent. */
108 debug_event_ptid (DEBUG_EVENT *event)
110 return ptid_build (event->dwProcessId, event->dwThreadId, 0);
113 /* Get the thread context of the thread associated with TH. */
116 win32_get_thread_context (win32_thread_info *th)
118 memset (&th->context, 0, sizeof (CONTEXT));
119 (*the_low_target.get_thread_context) (th, ¤t_event);
121 memcpy (&th->base_context, &th->context, sizeof (CONTEXT));
125 /* Set the thread context of the thread associated with TH. */
128 win32_set_thread_context (win32_thread_info *th)
131 /* Calling SuspendThread on a thread that is running kernel code
132 will report that the suspending was successful, but in fact, that
133 will often not be true. In those cases, the context returned by
134 GetThreadContext will not be correct by the time the thread
135 stops, hence we can't set that context back into the thread when
136 resuming - it will most likelly crash the inferior.
137 Unfortunately, there is no way to know when the thread will
138 really stop. To work around it, we'll only write the context
139 back to the thread when either the user or GDB explicitly change
140 it between stopping and resuming. */
141 if (memcmp (&th->context, &th->base_context, sizeof (CONTEXT)) != 0)
143 (*the_low_target.set_thread_context) (th, ¤t_event);
146 /* Find a thread record given a thread id. If GET_CONTEXT is set then
147 also retrieve the context for this thread. */
148 static win32_thread_info *
149 thread_rec (ptid_t ptid, int get_context)
151 struct thread_info *thread;
152 win32_thread_info *th;
154 thread = (struct thread_info *) find_inferior_id (&all_threads, ptid);
158 th = inferior_target_data (thread);
159 if (get_context && th->context.ContextFlags == 0)
163 if (SuspendThread (th->h) == (DWORD) -1)
165 DWORD err = GetLastError ();
166 OUTMSG (("warning: SuspendThread failed in thread_rec, "
167 "(error %d): %s\n", (int) err, strwinerror (err)));
173 win32_get_thread_context (th);
179 /* Add a thread to the thread list. */
180 static win32_thread_info *
181 child_add_thread (DWORD pid, DWORD tid, HANDLE h, void *tlb)
183 win32_thread_info *th;
184 ptid_t ptid = ptid_build (pid, tid, 0);
186 if ((th = thread_rec (ptid, FALSE)))
189 th = xcalloc (1, sizeof (*th));
192 th->thread_local_base = (CORE_ADDR) (uintptr_t) tlb;
194 add_thread (ptid, th);
195 set_inferior_regcache_data ((struct thread_info *)
196 find_inferior_id (&all_threads, ptid),
197 new_register_cache ());
199 if (the_low_target.thread_added != NULL)
200 (*the_low_target.thread_added) (th);
205 /* Delete a thread from the list of threads. */
207 delete_thread_info (struct inferior_list_entry *thread)
209 win32_thread_info *th = inferior_target_data ((struct thread_info *) thread);
211 remove_thread ((struct thread_info *) thread);
216 /* Delete a thread from the list of threads. */
218 child_delete_thread (DWORD pid, DWORD tid)
220 struct inferior_list_entry *thread;
223 /* If the last thread is exiting, just return. */
224 if (all_threads.head == all_threads.tail)
227 ptid = ptid_build (pid, tid, 0);
228 thread = find_inferior_id (&all_threads, ptid);
232 delete_thread_info (thread);
235 /* These watchpoint related wrapper functions simply pass on the function call
236 if the low target has registered a corresponding function. */
239 win32_insert_point (char type, CORE_ADDR addr, int len)
241 if (the_low_target.insert_point != NULL)
242 return the_low_target.insert_point (type, addr, len);
244 /* Unsupported (see target.h). */
249 win32_remove_point (char type, CORE_ADDR addr, int len)
251 if (the_low_target.remove_point != NULL)
252 return the_low_target.remove_point (type, addr, len);
254 /* Unsupported (see target.h). */
259 win32_stopped_by_watchpoint (void)
261 if (the_low_target.stopped_by_watchpoint != NULL)
262 return the_low_target.stopped_by_watchpoint ();
268 win32_stopped_data_address (void)
270 if (the_low_target.stopped_data_address != NULL)
271 return the_low_target.stopped_data_address ();
277 /* Transfer memory from/to the debugged process. */
279 child_xfer_memory (CORE_ADDR memaddr, char *our, int len,
280 int write, struct target_ops *target)
283 uintptr_t addr = (uintptr_t) memaddr;
287 WriteProcessMemory (current_process_handle, (LPVOID) addr,
288 (LPCVOID) our, len, &done);
289 FlushInstructionCache (current_process_handle, (LPCVOID) addr, len);
293 ReadProcessMemory (current_process_handle, (LPCVOID) addr, (LPVOID) our,
299 /* Clear out any old thread list and reinitialize it to a pristine
302 child_init_thread_list (void)
304 for_each_inferior (&all_threads, delete_thread_info);
308 do_initial_child_stuff (HANDLE proch, DWORD pid, int attached)
310 last_sig = TARGET_SIGNAL_0;
312 current_process_handle = proch;
313 current_process_id = pid;
316 soft_interrupt_requested = 0;
317 faked_breakpoint = 0;
319 memset (¤t_event, 0, sizeof (current_event));
321 add_process (pid, attached);
322 child_init_thread_list ();
324 if (the_low_target.initial_stuff != NULL)
325 (*the_low_target.initial_stuff) ();
328 /* Resume all artificially suspended threads if we are continuing
331 continue_one_thread (struct inferior_list_entry *this_thread, void *id_ptr)
333 struct thread_info *thread = (struct thread_info *) this_thread;
334 int thread_id = * (int *) id_ptr;
335 win32_thread_info *th = inferior_target_data (thread);
337 if ((thread_id == -1 || thread_id == th->tid)
340 if (th->context.ContextFlags)
342 win32_set_thread_context (th);
343 th->context.ContextFlags = 0;
346 if (ResumeThread (th->h) == (DWORD) -1)
348 DWORD err = GetLastError ();
349 OUTMSG (("warning: ResumeThread failed in continue_one_thread, "
350 "(error %d): %s\n", (int) err, strwinerror (err)));
359 child_continue (DWORD continue_status, int thread_id)
361 /* The inferior will only continue after the ContinueDebugEvent
363 find_inferior (&all_threads, continue_one_thread, &thread_id);
364 faked_breakpoint = 0;
366 if (!ContinueDebugEvent (current_event.dwProcessId,
367 current_event.dwThreadId,
374 /* Fetch register(s) from the current thread context. */
376 child_fetch_inferior_registers (struct regcache *regcache, int r)
379 win32_thread_info *th = thread_rec (current_inferior_ptid (), TRUE);
380 if (r == -1 || r > NUM_REGS)
381 child_fetch_inferior_registers (regcache, NUM_REGS);
383 for (regno = 0; regno < r; regno++)
384 (*the_low_target.fetch_inferior_register) (regcache, th, regno);
387 /* Store a new register value into the current thread context. We don't
388 change the program's context until later, when we resume it. */
390 child_store_inferior_registers (struct regcache *regcache, int r)
393 win32_thread_info *th = thread_rec (current_inferior_ptid (), TRUE);
394 if (r == -1 || r == 0 || r > NUM_REGS)
395 child_store_inferior_registers (regcache, NUM_REGS);
397 for (regno = 0; regno < r; regno++)
398 (*the_low_target.store_inferior_register) (regcache, th, regno);
401 /* Map the Windows error number in ERROR to a locale-dependent error
402 message string and return a pointer to it. Typically, the values
403 for ERROR come from GetLastError.
405 The string pointed to shall not be modified by the application,
406 but may be overwritten by a subsequent call to strwinerror
408 The strwinerror function does not change the current setting
412 strwinerror (DWORD error)
414 static char buf[1024];
416 DWORD lasterr = GetLastError ();
417 DWORD chars = FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
418 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
421 0, /* Default language */
427 /* If there is an \r\n appended, zap it. */
429 && msgbuf[chars - 2] == '\r'
430 && msgbuf[chars - 1] == '\n')
436 if (chars > ((COUNTOF (buf)) - 1))
438 chars = COUNTOF (buf) - 1;
443 wcstombs (buf, msgbuf, chars + 1);
445 strncpy (buf, msgbuf, chars + 1);
450 sprintf (buf, "unknown win32 error (%ld)", error);
452 SetLastError (lasterr);
457 create_process (const char *program, char *args,
458 DWORD flags, PROCESS_INFORMATION *pi)
463 wchar_t *p, *wprogram, *wargs;
466 wprogram = alloca ((strlen (program) + 1) * sizeof (wchar_t));
467 mbstowcs (wprogram, program, strlen (program) + 1);
469 for (p = wprogram; *p; ++p)
473 argslen = strlen (args);
474 wargs = alloca ((argslen + 1) * sizeof (wchar_t));
475 mbstowcs (wargs, args, argslen + 1);
477 ret = CreateProcessW (wprogram, /* image name */
478 wargs, /* command line */
479 NULL, /* security, not supported */
480 NULL, /* thread, not supported */
481 FALSE, /* inherit handles, not supported */
482 flags, /* start flags */
483 NULL, /* environment, not supported */
484 NULL, /* current directory, not supported */
485 NULL, /* start info, not supported */
488 STARTUPINFOA si = { sizeof (STARTUPINFOA) };
490 ret = CreateProcessA (program, /* image name */
491 args, /* command line */
494 TRUE, /* inherit handles */
495 flags, /* start flags */
496 NULL, /* environment */
497 NULL, /* current directory */
498 &si, /* start info */
505 /* Start a new process.
506 PROGRAM is a path to the program to execute.
507 ARGS is a standard NULL-terminated array of arguments,
508 to be passed to the inferior as ``argv''.
509 Returns the new PID on success, -1 on failure. Registers the new
510 process with the process list. */
512 win32_create_inferior (char *program, char **program_args)
515 char real_path[MAXPATHLEN];
516 char *orig_path, *new_path, *path_ptr;
523 PROCESS_INFORMATION pi;
526 /* win32_wait needs to know we're not attaching. */
530 error ("No executable specified, specify executable to debug.\n");
532 flags = DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS;
536 path_ptr = getenv ("PATH");
539 orig_path = alloca (strlen (path_ptr) + 1);
540 new_path = alloca (cygwin_posix_to_win32_path_list_buf_size (path_ptr));
541 strcpy (orig_path, path_ptr);
542 cygwin_posix_to_win32_path_list (path_ptr, new_path);
543 setenv ("PATH", new_path, 1);
545 cygwin_conv_to_win32_path (program, real_path);
550 for (argc = 1; program_args[argc]; argc++)
551 argslen += strlen (program_args[argc]) + 1;
552 args = alloca (argslen);
554 for (argc = 1; program_args[argc]; argc++)
556 /* FIXME: Can we do better about quoting? How does Cygwin
559 strcat (args, program_args[argc]);
561 OUTMSG2 (("Command line is \"%s\"\n", args));
563 #ifdef CREATE_NEW_PROCESS_GROUP
564 flags |= CREATE_NEW_PROCESS_GROUP;
567 ret = create_process (program, args, flags, &pi);
568 err = GetLastError ();
569 if (!ret && err == ERROR_FILE_NOT_FOUND)
571 char *exename = alloca (strlen (program) + 5);
572 strcat (strcpy (exename, program), ".exe");
573 ret = create_process (exename, args, flags, &pi);
574 err = GetLastError ();
579 setenv ("PATH", orig_path, 1);
584 error ("Error creating process \"%s%s\", (error %d): %s\n",
585 program, args, (int) err, strwinerror (err));
589 OUTMSG2 (("Process created: %s\n", (char *) args));
593 /* On Windows CE this handle can't be closed. The OS reuses
594 it in the debug events, while the 9x/NT versions of Windows
595 probably use a DuplicateHandle'd one. */
596 CloseHandle (pi.hThread);
599 do_initial_child_stuff (pi.hProcess, pi.dwProcessId, 0);
601 return current_process_id;
604 /* Attach to a running process.
605 PID is the process ID to attach to, specified by the user
606 or a higher layer. */
608 win32_attach (unsigned long pid)
611 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit = NULL;
614 HMODULE dll = GetModuleHandle (_T("COREDLL.DLL"));
616 HMODULE dll = GetModuleHandle (_T("KERNEL32.DLL"));
618 DebugSetProcessKillOnExit = GETPROCADDRESS (dll, DebugSetProcessKillOnExit);
620 h = OpenProcess (PROCESS_ALL_ACCESS, FALSE, pid);
623 if (DebugActiveProcess (pid))
625 if (DebugSetProcessKillOnExit != NULL)
626 DebugSetProcessKillOnExit (FALSE);
628 /* win32_wait needs to know we're attaching. */
630 do_initial_child_stuff (h, pid, 1);
637 err = GetLastError ();
638 error ("Attach to process failed (error %d): %s\n",
639 (int) err, strwinerror (err));
642 /* Handle OUTPUT_DEBUG_STRING_EVENT from child process. */
644 handle_output_debug_string (struct target_waitstatus *ourstatus)
646 #define READ_BUFFER_LEN 1024
648 char s[READ_BUFFER_LEN + 1] = { 0 };
649 DWORD nbytes = current_event.u.DebugString.nDebugStringLength;
654 if (nbytes > READ_BUFFER_LEN)
655 nbytes = READ_BUFFER_LEN;
657 addr = (CORE_ADDR) (size_t) current_event.u.DebugString.lpDebugStringData;
659 if (current_event.u.DebugString.fUnicode)
661 /* The event tells us how many bytes, not chars, even
663 WCHAR buffer[(READ_BUFFER_LEN + 1) / sizeof (WCHAR)] = { 0 };
664 if (read_inferior_memory (addr, (unsigned char *) buffer, nbytes) != 0)
666 wcstombs (s, buffer, (nbytes + 1) / sizeof (WCHAR));
670 if (read_inferior_memory (addr, (unsigned char *) s, nbytes) != 0)
674 if (strncmp (s, "cYg", 3) != 0)
684 #undef READ_BUFFER_LEN
688 win32_clear_inferiors (void)
690 if (current_process_handle != NULL)
691 CloseHandle (current_process_handle);
693 for_each_inferior (&all_threads, delete_thread_info);
697 /* Kill all inferiors. */
701 struct process_info *process;
703 if (current_process_handle == NULL)
706 TerminateProcess (current_process_handle, 0);
709 if (!child_continue (DBG_CONTINUE, -1))
711 if (!WaitForDebugEvent (¤t_event, INFINITE))
713 if (current_event.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT)
715 else if (current_event.dwDebugEventCode == OUTPUT_DEBUG_STRING_EVENT)
717 struct target_waitstatus our_status = { 0 };
718 handle_output_debug_string (&our_status);
722 win32_clear_inferiors ();
724 process = find_process_pid (pid);
725 remove_process (process);
729 /* Detach from inferior PID. */
731 win32_detach (int pid)
733 struct process_info *process;
734 winapi_DebugActiveProcessStop DebugActiveProcessStop = NULL;
735 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit = NULL;
737 HMODULE dll = GetModuleHandle (_T("COREDLL.DLL"));
739 HMODULE dll = GetModuleHandle (_T("KERNEL32.DLL"));
741 DebugActiveProcessStop = GETPROCADDRESS (dll, DebugActiveProcessStop);
742 DebugSetProcessKillOnExit = GETPROCADDRESS (dll, DebugSetProcessKillOnExit);
744 if (DebugSetProcessKillOnExit == NULL
745 || DebugActiveProcessStop == NULL)
749 struct thread_resume resume;
750 resume.thread = minus_one_ptid;
751 resume.kind = resume_continue;
753 win32_resume (&resume, 1);
756 if (!DebugActiveProcessStop (current_process_id))
759 DebugSetProcessKillOnExit (FALSE);
760 process = find_process_pid (pid);
761 remove_process (process);
763 win32_clear_inferiors ();
768 win32_mourn (struct process_info *process)
770 remove_process (process);
773 /* Wait for inferiors to end. */
777 HANDLE h = OpenProcess (PROCESS_ALL_ACCESS, FALSE, pid);
780 WaitForSingleObject (h, INFINITE);
785 /* Return 1 iff the thread with thread ID TID is alive. */
787 win32_thread_alive (ptid_t ptid)
791 /* Our thread list is reliable; don't bother to poll target
793 if (find_inferior_id (&all_threads, ptid) != NULL)
800 /* Resume the inferior process. RESUME_INFO describes how we want
803 win32_resume (struct thread_resume *resume_info, size_t n)
806 enum target_signal sig;
808 win32_thread_info *th;
809 DWORD continue_status = DBG_CONTINUE;
812 /* This handles the very limited set of resume packets that GDB can
813 currently produce. */
815 if (n == 1 && ptid_equal (resume_info[0].thread, minus_one_ptid))
820 /* Yes, we're ignoring resume_info[0].thread. It'd be tricky to make
821 the Windows resume code do the right thing for thread switching. */
822 tid = current_event.dwThreadId;
824 if (!ptid_equal (resume_info[0].thread, minus_one_ptid))
826 sig = resume_info[0].sig;
827 step = resume_info[0].kind == resume_step;
835 if (sig != TARGET_SIGNAL_0)
837 if (current_event.dwDebugEventCode != EXCEPTION_DEBUG_EVENT)
839 OUTMSG (("Cannot continue with signal %d here.\n", sig));
841 else if (sig == last_sig)
842 continue_status = DBG_EXCEPTION_NOT_HANDLED;
844 OUTMSG (("Can only continue with recieved signal %d.\n", last_sig));
847 last_sig = TARGET_SIGNAL_0;
849 /* Get context for the currently selected thread. */
850 ptid = debug_event_ptid (¤t_event);
851 th = thread_rec (ptid, FALSE);
854 if (th->context.ContextFlags)
856 /* Move register values from the inferior into the thread
857 context structure. */
858 regcache_invalidate ();
862 if (the_low_target.single_step != NULL)
863 (*the_low_target.single_step) (th);
865 error ("Single stepping is not supported "
866 "in this configuration.\n");
869 win32_set_thread_context (th);
870 th->context.ContextFlags = 0;
874 /* Allow continuing with the same signal that interrupted us.
875 Otherwise complain. */
877 child_continue (continue_status, tid);
881 win32_add_one_solib (const char *name, CORE_ADDR load_addr)
883 char buf[MAX_PATH + 1];
884 char buf2[MAX_PATH + 1];
887 WIN32_FIND_DATA w32_fd;
888 WCHAR wname[MAX_PATH + 1];
889 mbstowcs (wname, name, MAX_PATH);
890 HANDLE h = FindFirstFile (wname, &w32_fd);
892 WIN32_FIND_DATAA w32_fd;
893 HANDLE h = FindFirstFileA (name, &w32_fd);
896 if (h == INVALID_HANDLE_VALUE)
904 char cwd[MAX_PATH + 1];
906 if (GetCurrentDirectoryA (MAX_PATH + 1, cwd))
908 p = strrchr (buf, '\\');
911 SetCurrentDirectoryA (buf);
912 GetFullPathNameA (w32_fd.cFileName, MAX_PATH, buf, &p);
913 SetCurrentDirectoryA (cwd);
920 if (strcasecmp (buf, "ntdll.dll") == 0)
922 GetSystemDirectoryA (buf, sizeof (buf));
923 strcat (buf, "\\ntdll.dll");
928 cygwin_conv_to_posix_path (buf, buf2);
933 loaded_dll (buf2, load_addr);
937 get_image_name (HANDLE h, void *address, int unicode)
939 static char buf[(2 * MAX_PATH) + 1];
940 DWORD size = unicode ? sizeof (WCHAR) : sizeof (char);
946 /* Attempt to read the name of the dll that was detected.
947 This is documented to work only when actively debugging
948 a program. It will not work for attached processes. */
953 /* Windows CE reports the address of the image name,
954 instead of an address of a pointer into the image name. */
955 address_ptr = address;
957 /* See if we could read the address of a string, and that the
958 address isn't null. */
959 if (!ReadProcessMemory (h, address, &address_ptr,
960 sizeof (address_ptr), &done)
961 || done != sizeof (address_ptr)
966 /* Find the length of the string */
967 while (ReadProcessMemory (h, address_ptr + len++ * size, &b, size, &done)
968 && (b[0] != 0 || b[size - 1] != 0) && done == size)
972 ReadProcessMemory (h, address_ptr, buf, len, &done);
975 WCHAR *unicode_address = (WCHAR *) alloca (len * sizeof (WCHAR));
976 ReadProcessMemory (h, address_ptr, unicode_address, len * sizeof (WCHAR),
979 WideCharToMultiByte (CP_ACP, 0, unicode_address, len, buf, len, 0, 0);
985 typedef BOOL (WINAPI *winapi_EnumProcessModules) (HANDLE, HMODULE *,
987 typedef BOOL (WINAPI *winapi_GetModuleInformation) (HANDLE, HMODULE,
988 LPMODULEINFO, DWORD);
989 typedef DWORD (WINAPI *winapi_GetModuleFileNameExA) (HANDLE, HMODULE,
992 static winapi_EnumProcessModules win32_EnumProcessModules;
993 static winapi_GetModuleInformation win32_GetModuleInformation;
994 static winapi_GetModuleFileNameExA win32_GetModuleFileNameExA;
999 static int psapi_loaded = 0;
1000 static HMODULE dll = NULL;
1005 dll = LoadLibrary (TEXT("psapi.dll"));
1008 win32_EnumProcessModules =
1009 GETPROCADDRESS (dll, EnumProcessModules);
1010 win32_GetModuleInformation =
1011 GETPROCADDRESS (dll, GetModuleInformation);
1012 win32_GetModuleFileNameExA =
1013 GETPROCADDRESS (dll, GetModuleFileNameExA);
1016 return (win32_EnumProcessModules != NULL
1017 && win32_GetModuleInformation != NULL
1018 && win32_GetModuleFileNameExA != NULL);
1022 psapi_get_dll_name (LPVOID BaseAddress, char *dll_name_ret)
1028 HMODULE *DllHandle = dh_buf;
1036 ok = (*win32_EnumProcessModules) (current_process_handle,
1041 if (!ok || !cbNeeded)
1044 DllHandle = (HMODULE *) alloca (cbNeeded);
1048 ok = (*win32_EnumProcessModules) (current_process_handle,
1055 for (i = 0; i < ((size_t) cbNeeded / sizeof (HMODULE)); i++)
1057 if (!(*win32_GetModuleInformation) (current_process_handle,
1062 DWORD err = GetLastError ();
1063 error ("Can't get module info: (error %d): %s\n",
1064 (int) err, strwinerror (err));
1067 if (mi.lpBaseOfDll == BaseAddress)
1069 len = (*win32_GetModuleFileNameExA) (current_process_handle,
1075 DWORD err = GetLastError ();
1076 error ("Error getting dll name: (error %d): %s\n",
1077 (int) err, strwinerror (err));
1084 dll_name_ret[0] = '\0';
1088 typedef HANDLE (WINAPI *winapi_CreateToolhelp32Snapshot) (DWORD, DWORD);
1089 typedef BOOL (WINAPI *winapi_Module32First) (HANDLE, LPMODULEENTRY32);
1090 typedef BOOL (WINAPI *winapi_Module32Next) (HANDLE, LPMODULEENTRY32);
1092 static winapi_CreateToolhelp32Snapshot win32_CreateToolhelp32Snapshot;
1093 static winapi_Module32First win32_Module32First;
1094 static winapi_Module32Next win32_Module32Next;
1096 typedef BOOL (WINAPI *winapi_CloseToolhelp32Snapshot) (HANDLE);
1097 static winapi_CloseToolhelp32Snapshot win32_CloseToolhelp32Snapshot;
1101 load_toolhelp (void)
1103 static int toolhelp_loaded = 0;
1104 static HMODULE dll = NULL;
1106 if (!toolhelp_loaded)
1108 toolhelp_loaded = 1;
1110 dll = GetModuleHandle (_T("KERNEL32.DLL"));
1112 dll = LoadLibrary (L"TOOLHELP.DLL");
1117 win32_CreateToolhelp32Snapshot =
1118 GETPROCADDRESS (dll, CreateToolhelp32Snapshot);
1119 win32_Module32First = GETPROCADDRESS (dll, Module32First);
1120 win32_Module32Next = GETPROCADDRESS (dll, Module32Next);
1122 win32_CloseToolhelp32Snapshot =
1123 GETPROCADDRESS (dll, CloseToolhelp32Snapshot);
1127 return (win32_CreateToolhelp32Snapshot != NULL
1128 && win32_Module32First != NULL
1129 && win32_Module32Next != NULL
1131 && win32_CloseToolhelp32Snapshot != NULL
1137 toolhelp_get_dll_name (LPVOID BaseAddress, char *dll_name_ret)
1139 HANDLE snapshot_module;
1140 MODULEENTRY32 modEntry = { sizeof (MODULEENTRY32) };
1143 if (!load_toolhelp ())
1146 snapshot_module = win32_CreateToolhelp32Snapshot (TH32CS_SNAPMODULE,
1147 current_event.dwProcessId);
1148 if (snapshot_module == INVALID_HANDLE_VALUE)
1151 /* Ignore the first module, which is the exe. */
1152 if (win32_Module32First (snapshot_module, &modEntry))
1153 while (win32_Module32Next (snapshot_module, &modEntry))
1154 if (modEntry.modBaseAddr == BaseAddress)
1157 wcstombs (dll_name_ret, modEntry.szExePath, MAX_PATH + 1);
1159 strcpy (dll_name_ret, modEntry.szExePath);
1166 win32_CloseToolhelp32Snapshot (snapshot_module);
1168 CloseHandle (snapshot_module);
1174 handle_load_dll (void)
1176 LOAD_DLL_DEBUG_INFO *event = ¤t_event.u.LoadDll;
1177 char dll_buf[MAX_PATH + 1];
1178 char *dll_name = NULL;
1179 CORE_ADDR load_addr;
1181 dll_buf[0] = dll_buf[sizeof (dll_buf) - 1] = '\0';
1183 /* Windows does not report the image name of the dlls in the debug
1184 event on attaches. We resort to iterating over the list of
1185 loaded dlls looking for a match by image base. */
1186 if (!psapi_get_dll_name (event->lpBaseOfDll, dll_buf))
1188 if (!server_waiting)
1189 /* On some versions of Windows and Windows CE, we can't create
1190 toolhelp snapshots while the inferior is stopped in a
1191 LOAD_DLL_DEBUG_EVENT due to a dll load, but we can while
1192 Windows is reporting the already loaded dlls. */
1193 toolhelp_get_dll_name (event->lpBaseOfDll, dll_buf);
1198 if (*dll_name == '\0')
1199 dll_name = get_image_name (current_process_handle,
1200 event->lpImageName, event->fUnicode);
1204 /* The symbols in a dll are offset by 0x1000, which is the
1205 the offset from 0 of the first byte in an image - because
1206 of the file header and the section alignment. */
1208 load_addr = (CORE_ADDR) (uintptr_t) event->lpBaseOfDll + 0x1000;
1209 win32_add_one_solib (dll_name, load_addr);
1213 handle_unload_dll (void)
1215 CORE_ADDR load_addr =
1216 (CORE_ADDR) (uintptr_t) current_event.u.UnloadDll.lpBaseOfDll;
1217 load_addr += 0x1000;
1218 unloaded_dll (NULL, load_addr);
1222 handle_exception (struct target_waitstatus *ourstatus)
1224 DWORD code = current_event.u.Exception.ExceptionRecord.ExceptionCode;
1226 ourstatus->kind = TARGET_WAITKIND_STOPPED;
1230 case EXCEPTION_ACCESS_VIOLATION:
1231 OUTMSG2 (("EXCEPTION_ACCESS_VIOLATION"));
1232 ourstatus->value.sig = TARGET_SIGNAL_SEGV;
1234 case STATUS_STACK_OVERFLOW:
1235 OUTMSG2 (("STATUS_STACK_OVERFLOW"));
1236 ourstatus->value.sig = TARGET_SIGNAL_SEGV;
1238 case STATUS_FLOAT_DENORMAL_OPERAND:
1239 OUTMSG2 (("STATUS_FLOAT_DENORMAL_OPERAND"));
1240 ourstatus->value.sig = TARGET_SIGNAL_FPE;
1242 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
1243 OUTMSG2 (("EXCEPTION_ARRAY_BOUNDS_EXCEEDED"));
1244 ourstatus->value.sig = TARGET_SIGNAL_FPE;
1246 case STATUS_FLOAT_INEXACT_RESULT:
1247 OUTMSG2 (("STATUS_FLOAT_INEXACT_RESULT"));
1248 ourstatus->value.sig = TARGET_SIGNAL_FPE;
1250 case STATUS_FLOAT_INVALID_OPERATION:
1251 OUTMSG2 (("STATUS_FLOAT_INVALID_OPERATION"));
1252 ourstatus->value.sig = TARGET_SIGNAL_FPE;
1254 case STATUS_FLOAT_OVERFLOW:
1255 OUTMSG2 (("STATUS_FLOAT_OVERFLOW"));
1256 ourstatus->value.sig = TARGET_SIGNAL_FPE;
1258 case STATUS_FLOAT_STACK_CHECK:
1259 OUTMSG2 (("STATUS_FLOAT_STACK_CHECK"));
1260 ourstatus->value.sig = TARGET_SIGNAL_FPE;
1262 case STATUS_FLOAT_UNDERFLOW:
1263 OUTMSG2 (("STATUS_FLOAT_UNDERFLOW"));
1264 ourstatus->value.sig = TARGET_SIGNAL_FPE;
1266 case STATUS_FLOAT_DIVIDE_BY_ZERO:
1267 OUTMSG2 (("STATUS_FLOAT_DIVIDE_BY_ZERO"));
1268 ourstatus->value.sig = TARGET_SIGNAL_FPE;
1270 case STATUS_INTEGER_DIVIDE_BY_ZERO:
1271 OUTMSG2 (("STATUS_INTEGER_DIVIDE_BY_ZERO"));
1272 ourstatus->value.sig = TARGET_SIGNAL_FPE;
1274 case STATUS_INTEGER_OVERFLOW:
1275 OUTMSG2 (("STATUS_INTEGER_OVERFLOW"));
1276 ourstatus->value.sig = TARGET_SIGNAL_FPE;
1278 case EXCEPTION_BREAKPOINT:
1279 OUTMSG2 (("EXCEPTION_BREAKPOINT"));
1280 ourstatus->value.sig = TARGET_SIGNAL_TRAP;
1282 /* Remove the initial breakpoint. */
1283 check_breakpoints ((CORE_ADDR) (long) current_event
1284 .u.Exception.ExceptionRecord.ExceptionAddress);
1288 OUTMSG2 (("DBG_CONTROL_C"));
1289 ourstatus->value.sig = TARGET_SIGNAL_INT;
1291 case DBG_CONTROL_BREAK:
1292 OUTMSG2 (("DBG_CONTROL_BREAK"));
1293 ourstatus->value.sig = TARGET_SIGNAL_INT;
1295 case EXCEPTION_SINGLE_STEP:
1296 OUTMSG2 (("EXCEPTION_SINGLE_STEP"));
1297 ourstatus->value.sig = TARGET_SIGNAL_TRAP;
1299 case EXCEPTION_ILLEGAL_INSTRUCTION:
1300 OUTMSG2 (("EXCEPTION_ILLEGAL_INSTRUCTION"));
1301 ourstatus->value.sig = TARGET_SIGNAL_ILL;
1303 case EXCEPTION_PRIV_INSTRUCTION:
1304 OUTMSG2 (("EXCEPTION_PRIV_INSTRUCTION"));
1305 ourstatus->value.sig = TARGET_SIGNAL_ILL;
1307 case EXCEPTION_NONCONTINUABLE_EXCEPTION:
1308 OUTMSG2 (("EXCEPTION_NONCONTINUABLE_EXCEPTION"));
1309 ourstatus->value.sig = TARGET_SIGNAL_ILL;
1312 if (current_event.u.Exception.dwFirstChance)
1314 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
1317 OUTMSG2 (("gdbserver: unknown target exception 0x%08lx at 0x%s",
1318 current_event.u.Exception.ExceptionRecord.ExceptionCode,
1319 phex_nz ((uintptr_t) current_event.u.Exception.ExceptionRecord.
1320 ExceptionAddress, sizeof (uintptr_t))));
1321 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
1325 last_sig = ourstatus->value.sig;
1330 suspend_one_thread (struct inferior_list_entry *entry)
1332 struct thread_info *thread = (struct thread_info *) entry;
1333 win32_thread_info *th = inferior_target_data (thread);
1337 if (SuspendThread (th->h) == (DWORD) -1)
1339 DWORD err = GetLastError ();
1340 OUTMSG (("warning: SuspendThread failed in suspend_one_thread, "
1341 "(error %d): %s\n", (int) err, strwinerror (err)));
1349 fake_breakpoint_event (void)
1351 OUTMSG2(("fake_breakpoint_event\n"));
1353 faked_breakpoint = 1;
1355 memset (¤t_event, 0, sizeof (current_event));
1356 current_event.dwThreadId = main_thread_id;
1357 current_event.dwDebugEventCode = EXCEPTION_DEBUG_EVENT;
1358 current_event.u.Exception.ExceptionRecord.ExceptionCode
1359 = EXCEPTION_BREAKPOINT;
1361 for_each_inferior (&all_threads, suspend_one_thread);
1366 auto_delete_breakpoint (CORE_ADDR stop_pc)
1372 /* Get the next event from the child. */
1375 get_child_debug_event (struct target_waitstatus *ourstatus)
1379 last_sig = TARGET_SIGNAL_0;
1380 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
1382 /* Check if GDB sent us an interrupt request. */
1383 check_remote_input_interrupt_request ();
1385 if (soft_interrupt_requested)
1387 soft_interrupt_requested = 0;
1388 fake_breakpoint_event ();
1397 /* WinCE doesn't set an initial breakpoint automatically. To
1398 stop the inferior, we flush all currently pending debug
1399 events -- the thread list and the dll list are always
1400 reported immediatelly without delay, then, we suspend all
1401 threads and pretend we saw a trap at the current PC of the
1404 Contrary to desktop Windows, Windows CE *does* report the dll
1405 names on LOAD_DLL_DEBUG_EVENTs resulting from a
1406 DebugActiveProcess call. This limits the way we can detect
1407 if all the dlls have already been reported. If we get a real
1408 debug event before leaving attaching, the worst that will
1409 happen is the user will see a spurious breakpoint. */
1411 current_event.dwDebugEventCode = 0;
1412 if (!WaitForDebugEvent (¤t_event, 0))
1414 OUTMSG2(("no attach events left\n"));
1415 fake_breakpoint_event ();
1419 OUTMSG2(("got attach event\n"));
1424 /* Keep the wait time low enough for confortable remote
1425 interruption, but high enough so gdbserver doesn't become a
1427 if (!WaitForDebugEvent (¤t_event, 250))
1429 DWORD e = GetLastError();
1431 if (e == ERROR_PIPE_NOT_CONNECTED)
1433 /* This will happen if the loader fails to succesfully
1434 load the application, e.g., if the main executable
1435 tries to pull in a non-existing export from a
1437 ourstatus->kind = TARGET_WAITKIND_EXITED;
1438 ourstatus->value.integer = 1;
1448 switch (current_event.dwDebugEventCode)
1450 case CREATE_THREAD_DEBUG_EVENT:
1451 OUTMSG2 (("gdbserver: kernel event CREATE_THREAD_DEBUG_EVENT "
1452 "for pid=%d tid=%x)\n",
1453 (unsigned) current_event.dwProcessId,
1454 (unsigned) current_event.dwThreadId));
1456 /* Record the existence of this thread. */
1457 child_add_thread (current_event.dwProcessId,
1458 current_event.dwThreadId,
1459 current_event.u.CreateThread.hThread,
1460 current_event.u.CreateThread.lpThreadLocalBase);
1463 case EXIT_THREAD_DEBUG_EVENT:
1464 OUTMSG2 (("gdbserver: kernel event EXIT_THREAD_DEBUG_EVENT "
1465 "for pid=%d tid=%x\n",
1466 (unsigned) current_event.dwProcessId,
1467 (unsigned) current_event.dwThreadId));
1468 child_delete_thread (current_event.dwProcessId,
1469 current_event.dwThreadId);
1471 current_inferior = (struct thread_info *) all_threads.head;
1474 case CREATE_PROCESS_DEBUG_EVENT:
1475 OUTMSG2 (("gdbserver: kernel event CREATE_PROCESS_DEBUG_EVENT "
1476 "for pid=%d tid=%x\n",
1477 (unsigned) current_event.dwProcessId,
1478 (unsigned) current_event.dwThreadId));
1479 CloseHandle (current_event.u.CreateProcessInfo.hFile);
1481 current_process_handle = current_event.u.CreateProcessInfo.hProcess;
1482 main_thread_id = current_event.dwThreadId;
1484 ourstatus->kind = TARGET_WAITKIND_EXECD;
1485 ourstatus->value.execd_pathname = "Main executable";
1487 /* Add the main thread. */
1488 child_add_thread (current_event.dwProcessId,
1490 current_event.u.CreateProcessInfo.hThread,
1491 current_event.u.CreateProcessInfo.lpThreadLocalBase);
1493 ourstatus->value.related_pid = debug_event_ptid (¤t_event);
1497 /* Windows CE doesn't set the initial breakpoint
1498 automatically like the desktop versions of Windows do.
1499 We add it explicitly here. It will be removed as soon as
1501 set_breakpoint_at ((CORE_ADDR) (long) current_event.u
1502 .CreateProcessInfo.lpStartAddress,
1503 auto_delete_breakpoint);
1508 case EXIT_PROCESS_DEBUG_EVENT:
1509 OUTMSG2 (("gdbserver: kernel event EXIT_PROCESS_DEBUG_EVENT "
1510 "for pid=%d tid=%x\n",
1511 (unsigned) current_event.dwProcessId,
1512 (unsigned) current_event.dwThreadId));
1513 ourstatus->kind = TARGET_WAITKIND_EXITED;
1514 ourstatus->value.integer = current_event.u.ExitProcess.dwExitCode;
1515 child_continue (DBG_CONTINUE, -1);
1516 CloseHandle (current_process_handle);
1517 current_process_handle = NULL;
1520 case LOAD_DLL_DEBUG_EVENT:
1521 OUTMSG2 (("gdbserver: kernel event LOAD_DLL_DEBUG_EVENT "
1522 "for pid=%d tid=%x\n",
1523 (unsigned) current_event.dwProcessId,
1524 (unsigned) current_event.dwThreadId));
1525 CloseHandle (current_event.u.LoadDll.hFile);
1528 ourstatus->kind = TARGET_WAITKIND_LOADED;
1529 ourstatus->value.sig = TARGET_SIGNAL_TRAP;
1532 case UNLOAD_DLL_DEBUG_EVENT:
1533 OUTMSG2 (("gdbserver: kernel event UNLOAD_DLL_DEBUG_EVENT "
1534 "for pid=%d tid=%x\n",
1535 (unsigned) current_event.dwProcessId,
1536 (unsigned) current_event.dwThreadId));
1537 handle_unload_dll ();
1538 ourstatus->kind = TARGET_WAITKIND_LOADED;
1539 ourstatus->value.sig = TARGET_SIGNAL_TRAP;
1542 case EXCEPTION_DEBUG_EVENT:
1543 OUTMSG2 (("gdbserver: kernel event EXCEPTION_DEBUG_EVENT "
1544 "for pid=%d tid=%x\n",
1545 (unsigned) current_event.dwProcessId,
1546 (unsigned) current_event.dwThreadId));
1547 handle_exception (ourstatus);
1550 case OUTPUT_DEBUG_STRING_EVENT:
1551 /* A message from the kernel (or Cygwin). */
1552 OUTMSG2 (("gdbserver: kernel event OUTPUT_DEBUG_STRING_EVENT "
1553 "for pid=%d tid=%x\n",
1554 (unsigned) current_event.dwProcessId,
1555 (unsigned) current_event.dwThreadId));
1556 handle_output_debug_string (ourstatus);
1560 OUTMSG2 (("gdbserver: kernel event unknown "
1561 "for pid=%d tid=%x code=%ld\n",
1562 (unsigned) current_event.dwProcessId,
1563 (unsigned) current_event.dwThreadId,
1564 current_event.dwDebugEventCode));
1568 ptid = debug_event_ptid (¤t_event);
1570 (struct thread_info *) find_inferior_id (&all_threads, ptid);
1574 /* Wait for the inferior process to change state.
1575 STATUS will be filled in with a response code to send to GDB.
1576 Returns the signal which caused the process to stop. */
1578 win32_wait (ptid_t ptid, struct target_waitstatus *ourstatus, int options)
1580 struct regcache *regcache;
1584 if (!get_child_debug_event (ourstatus))
1587 switch (ourstatus->kind)
1589 case TARGET_WAITKIND_EXITED:
1590 OUTMSG2 (("Child exited with retcode = %x\n",
1591 ourstatus->value.integer));
1592 win32_clear_inferiors ();
1593 return pid_to_ptid (current_event.dwProcessId);
1594 case TARGET_WAITKIND_STOPPED:
1595 case TARGET_WAITKIND_LOADED:
1596 OUTMSG2 (("Child Stopped with signal = %d \n",
1597 ourstatus->value.sig));
1599 regcache = get_thread_regcache (current_inferior, 1);
1600 child_fetch_inferior_registers (regcache, -1);
1602 if (ourstatus->kind == TARGET_WAITKIND_LOADED
1605 /* When gdb connects, we want to be stopped at the
1606 initial breakpoint, not in some dll load event. */
1607 child_continue (DBG_CONTINUE, -1);
1611 /* We don't expose _LOADED events to gdbserver core. See
1612 the `dlls_changed' global. */
1613 if (ourstatus->kind == TARGET_WAITKIND_LOADED)
1614 ourstatus->kind = TARGET_WAITKIND_STOPPED;
1616 return debug_event_ptid (¤t_event);
1618 OUTMSG (("Ignoring unknown internal event, %d\n", ourstatus->kind));
1620 case TARGET_WAITKIND_SPURIOUS:
1621 case TARGET_WAITKIND_EXECD:
1622 /* do nothing, just continue */
1623 child_continue (DBG_CONTINUE, -1);
1629 /* Fetch registers from the inferior process.
1630 If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO. */
1632 win32_fetch_inferior_registers (struct regcache *regcache, int regno)
1634 child_fetch_inferior_registers (regcache, regno);
1637 /* Store registers to the inferior process.
1638 If REGNO is -1, store all registers; otherwise, store at least REGNO. */
1640 win32_store_inferior_registers (struct regcache *regcache, int regno)
1642 child_store_inferior_registers (regcache, regno);
1645 /* Read memory from the inferior process. This should generally be
1646 called through read_inferior_memory, which handles breakpoint shadowing.
1647 Read LEN bytes at MEMADDR into a buffer at MYADDR. */
1649 win32_read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
1651 return child_xfer_memory (memaddr, (char *) myaddr, len, 0, 0) != len;
1654 /* Write memory to the inferior process. This should generally be
1655 called through write_inferior_memory, which handles breakpoint shadowing.
1656 Write LEN bytes from the buffer at MYADDR to MEMADDR.
1657 Returns 0 on success and errno on failure. */
1659 win32_write_inferior_memory (CORE_ADDR memaddr, const unsigned char *myaddr,
1662 return child_xfer_memory (memaddr, (char *) myaddr, len, 1, 0) != len;
1665 /* Send an interrupt request to the inferior process. */
1667 win32_request_interrupt (void)
1669 winapi_DebugBreakProcess DebugBreakProcess;
1670 winapi_GenerateConsoleCtrlEvent GenerateConsoleCtrlEvent;
1673 HMODULE dll = GetModuleHandle (_T("COREDLL.DLL"));
1675 HMODULE dll = GetModuleHandle (_T("KERNEL32.DLL"));
1678 GenerateConsoleCtrlEvent = GETPROCADDRESS (dll, GenerateConsoleCtrlEvent);
1680 if (GenerateConsoleCtrlEvent != NULL
1681 && GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT, current_process_id))
1684 /* GenerateConsoleCtrlEvent can fail if process id being debugged is
1685 not a process group id.
1686 Fallback to XP/Vista 'DebugBreakProcess', which generates a
1687 breakpoint exception in the interior process. */
1689 DebugBreakProcess = GETPROCADDRESS (dll, DebugBreakProcess);
1691 if (DebugBreakProcess != NULL
1692 && DebugBreakProcess (current_process_handle))
1695 /* Last resort, suspend all threads manually. */
1696 soft_interrupt_requested = 1;
1701 win32_error_to_fileio_error (DWORD err)
1705 case ERROR_BAD_PATHNAME:
1706 case ERROR_FILE_NOT_FOUND:
1707 case ERROR_INVALID_NAME:
1708 case ERROR_PATH_NOT_FOUND:
1709 return FILEIO_ENOENT;
1711 case ERROR_IO_DEVICE:
1712 case ERROR_OPEN_FAILED:
1714 case ERROR_INVALID_HANDLE:
1715 return FILEIO_EBADF;
1716 case ERROR_ACCESS_DENIED:
1717 case ERROR_SHARING_VIOLATION:
1718 return FILEIO_EACCES;
1719 case ERROR_NOACCESS:
1720 return FILEIO_EFAULT;
1722 return FILEIO_EBUSY;
1723 case ERROR_ALREADY_EXISTS:
1724 case ERROR_FILE_EXISTS:
1725 return FILEIO_EEXIST;
1726 case ERROR_BAD_DEVICE:
1727 return FILEIO_ENODEV;
1728 case ERROR_DIRECTORY:
1729 return FILEIO_ENOTDIR;
1730 case ERROR_FILENAME_EXCED_RANGE:
1731 case ERROR_INVALID_DATA:
1732 case ERROR_INVALID_PARAMETER:
1733 case ERROR_NEGATIVE_SEEK:
1734 return FILEIO_EINVAL;
1735 case ERROR_TOO_MANY_OPEN_FILES:
1736 return FILEIO_EMFILE;
1737 case ERROR_HANDLE_DISK_FULL:
1738 case ERROR_DISK_FULL:
1739 return FILEIO_ENOSPC;
1740 case ERROR_WRITE_PROTECT:
1741 return FILEIO_EROFS;
1742 case ERROR_NOT_SUPPORTED:
1743 return FILEIO_ENOSYS;
1746 return FILEIO_EUNKNOWN;
1750 wince_hostio_last_error (char *buf)
1752 DWORD winerr = GetLastError ();
1753 int fileio_err = win32_error_to_fileio_error (winerr);
1754 sprintf (buf, "F-1,%x", fileio_err);
1758 /* Write Windows OS Thread Information Block address. */
1761 win32_get_tib_address (ptid_t ptid, CORE_ADDR *addr)
1763 win32_thread_info *th;
1764 th = thread_rec (ptid, 0);
1768 *addr = th->thread_local_base;
1772 static struct target_ops win32_target_ops = {
1773 win32_create_inferior,
1782 win32_fetch_inferior_registers,
1783 win32_store_inferior_registers,
1784 NULL, /* prepare_to_access_memory */
1785 NULL, /* done_accessing_memory */
1786 win32_read_inferior_memory,
1787 win32_write_inferior_memory,
1788 NULL, /* lookup_symbols */
1789 win32_request_interrupt,
1790 NULL, /* read_auxv */
1793 win32_stopped_by_watchpoint,
1794 win32_stopped_data_address,
1795 NULL, /* read_offsets */
1796 NULL, /* get_tls_address */
1797 NULL, /* qxfer_spu */
1799 wince_hostio_last_error,
1801 hostio_last_error_from_errno,
1803 NULL, /* qxfer_osdata */
1804 NULL, /* qxfer_siginfo */
1805 NULL, /* supports_non_stop */
1807 NULL, /* start_non_stop */
1808 NULL, /* supports_multi_process */
1809 NULL, /* handle_monitor_command */
1810 NULL, /* core_of_thread */
1811 NULL, /* process_qsupported */
1812 NULL, /* supports_tracepoints */
1814 NULL, /* write_pc */
1815 NULL, /* thread_stopped */
1816 win32_get_tib_address
1819 /* Initialize the Win32 backend. */
1821 initialize_low (void)
1823 set_target_ops (&win32_target_ops);
1824 if (the_low_target.breakpoint != NULL)
1825 set_breakpoint_data (the_low_target.breakpoint,
1826 the_low_target.breakpoint_len);
1827 the_low_target.arch_setup ();