1 /* Low level interface to Windows debugging, for gdbserver.
2 Copyright (C) 2006-2013 Free Software Foundation, Inc.
4 Contributed by Leo Zayas. Based on "win32-nat.c" from GDB.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "gdb/signals.h"
24 #include "gdb/fileio.h"
25 #include "mem-break.h"
26 #include "win32-low.h"
27 #include "gdbthread.h"
40 #include <sys/cygwin.h>
43 #define OUTMSG(X) do { printf X; fflush (stderr); } while (0)
56 #define _T(x) TEXT (x)
60 #define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0]))
64 # define GETPROCADDRESS(DLL, PROC) \
65 ((winapi_ ## PROC) GetProcAddress (DLL, TEXT (#PROC)))
67 # define GETPROCADDRESS(DLL, PROC) \
68 ((winapi_ ## PROC) GetProcAddress (DLL, #PROC))
71 int using_threads = 1;
74 static int attaching = 0;
75 static HANDLE current_process_handle = NULL;
76 static DWORD current_process_id = 0;
77 static DWORD main_thread_id = 0;
78 static enum gdb_signal last_sig = GDB_SIGNAL_0;
80 /* The current debug event from WaitForDebugEvent. */
81 static DEBUG_EVENT current_event;
83 /* A status that hasn't been reported to the core yet, and so
84 win32_wait should return it next, instead of fetching the next
85 debug event off the win32 API. */
86 static struct target_waitstatus cached_status;
88 /* Non zero if an interrupt request is to be satisfied by suspending
90 static int soft_interrupt_requested = 0;
92 /* Non zero if the inferior is stopped in a simulated breakpoint done
93 by suspending all the threads. */
94 static int faked_breakpoint = 0;
96 const struct target_desc *win32_tdesc;
98 #define NUM_REGS (the_low_target.num_regs)
100 typedef BOOL (WINAPI *winapi_DebugActiveProcessStop) (DWORD dwProcessId);
101 typedef BOOL (WINAPI *winapi_DebugSetProcessKillOnExit) (BOOL KillOnExit);
102 typedef BOOL (WINAPI *winapi_DebugBreakProcess) (HANDLE);
103 typedef BOOL (WINAPI *winapi_GenerateConsoleCtrlEvent) (DWORD, DWORD);
105 static ptid_t win32_wait (ptid_t ptid, struct target_waitstatus *ourstatus,
107 static void win32_resume (struct thread_resume *resume_info, size_t n);
109 static void win32_ensure_ntdll_loaded (void);
112 /* Get the thread ID from the current selected inferior (the current
115 current_inferior_ptid (void)
117 return ((struct inferior_list_entry*) current_inferior)->id;
120 /* The current debug event from WaitForDebugEvent. */
122 debug_event_ptid (DEBUG_EVENT *event)
124 return ptid_build (event->dwProcessId, event->dwThreadId, 0);
127 /* Get the thread context of the thread associated with TH. */
130 win32_get_thread_context (win32_thread_info *th)
132 memset (&th->context, 0, sizeof (CONTEXT));
133 (*the_low_target.get_thread_context) (th, ¤t_event);
135 memcpy (&th->base_context, &th->context, sizeof (CONTEXT));
139 /* Set the thread context of the thread associated with TH. */
142 win32_set_thread_context (win32_thread_info *th)
145 /* Calling SuspendThread on a thread that is running kernel code
146 will report that the suspending was successful, but in fact, that
147 will often not be true. In those cases, the context returned by
148 GetThreadContext will not be correct by the time the thread
149 stops, hence we can't set that context back into the thread when
150 resuming - it will most likelly crash the inferior.
151 Unfortunately, there is no way to know when the thread will
152 really stop. To work around it, we'll only write the context
153 back to the thread when either the user or GDB explicitly change
154 it between stopping and resuming. */
155 if (memcmp (&th->context, &th->base_context, sizeof (CONTEXT)) != 0)
157 (*the_low_target.set_thread_context) (th, ¤t_event);
160 /* Find a thread record given a thread id. If GET_CONTEXT is set then
161 also retrieve the context for this thread. */
162 static win32_thread_info *
163 thread_rec (ptid_t ptid, int get_context)
165 struct thread_info *thread;
166 win32_thread_info *th;
168 thread = (struct thread_info *) find_inferior_id (&all_threads, ptid);
172 th = inferior_target_data (thread);
173 if (get_context && th->context.ContextFlags == 0)
177 if (SuspendThread (th->h) == (DWORD) -1)
179 DWORD err = GetLastError ();
180 OUTMSG (("warning: SuspendThread failed in thread_rec, "
181 "(error %d): %s\n", (int) err, strwinerror (err)));
187 win32_get_thread_context (th);
193 /* Add a thread to the thread list. */
194 static win32_thread_info *
195 child_add_thread (DWORD pid, DWORD tid, HANDLE h, void *tlb)
197 win32_thread_info *th;
198 ptid_t ptid = ptid_build (pid, tid, 0);
200 if ((th = thread_rec (ptid, FALSE)))
203 th = xcalloc (1, sizeof (*th));
206 th->thread_local_base = (CORE_ADDR) (uintptr_t) tlb;
208 add_thread (ptid, th);
210 if (the_low_target.thread_added != NULL)
211 (*the_low_target.thread_added) (th);
216 /* Delete a thread from the list of threads. */
218 delete_thread_info (struct inferior_list_entry *thread)
220 win32_thread_info *th = inferior_target_data ((struct thread_info *) thread);
222 remove_thread ((struct thread_info *) thread);
227 /* Delete a thread from the list of threads. */
229 child_delete_thread (DWORD pid, DWORD tid)
231 struct inferior_list_entry *thread;
234 /* If the last thread is exiting, just return. */
235 if (all_threads.head == all_threads.tail)
238 ptid = ptid_build (pid, tid, 0);
239 thread = find_inferior_id (&all_threads, ptid);
243 delete_thread_info (thread);
246 /* These watchpoint related wrapper functions simply pass on the function call
247 if the low target has registered a corresponding function. */
250 win32_insert_point (char type, CORE_ADDR addr, int len)
252 if (the_low_target.insert_point != NULL)
253 return the_low_target.insert_point (type, addr, len);
255 /* Unsupported (see target.h). */
260 win32_remove_point (char type, CORE_ADDR addr, int len)
262 if (the_low_target.remove_point != NULL)
263 return the_low_target.remove_point (type, addr, len);
265 /* Unsupported (see target.h). */
270 win32_stopped_by_watchpoint (void)
272 if (the_low_target.stopped_by_watchpoint != NULL)
273 return the_low_target.stopped_by_watchpoint ();
279 win32_stopped_data_address (void)
281 if (the_low_target.stopped_data_address != NULL)
282 return the_low_target.stopped_data_address ();
288 /* Transfer memory from/to the debugged process. */
290 child_xfer_memory (CORE_ADDR memaddr, char *our, int len,
291 int write, struct target_ops *target)
296 uintptr_t addr = (uintptr_t) memaddr;
300 success = WriteProcessMemory (current_process_handle, (LPVOID) addr,
301 (LPCVOID) our, len, &done);
303 lasterror = GetLastError ();
304 FlushInstructionCache (current_process_handle, (LPCVOID) addr, len);
308 success = ReadProcessMemory (current_process_handle, (LPCVOID) addr,
309 (LPVOID) our, len, &done);
311 lasterror = GetLastError ();
313 if (!success && lasterror == ERROR_PARTIAL_COPY && done > 0)
316 return success ? done : -1;
319 /* Clear out any old thread list and reinitialize it to a pristine
322 child_init_thread_list (void)
324 for_each_inferior (&all_threads, delete_thread_info);
328 do_initial_child_stuff (HANDLE proch, DWORD pid, int attached)
330 struct process_info *proc;
332 last_sig = GDB_SIGNAL_0;
334 current_process_handle = proch;
335 current_process_id = pid;
338 soft_interrupt_requested = 0;
339 faked_breakpoint = 0;
341 memset (¤t_event, 0, sizeof (current_event));
343 proc = add_process (pid, attached);
344 proc->tdesc = win32_tdesc;
345 child_init_thread_list ();
347 if (the_low_target.initial_stuff != NULL)
348 (*the_low_target.initial_stuff) ();
350 cached_status.kind = TARGET_WAITKIND_IGNORE;
352 /* Flush all currently pending debug events (thread and dll list) up
353 to the initial breakpoint. */
356 struct target_waitstatus status;
358 win32_wait (minus_one_ptid, &status, 0);
360 /* Note win32_wait doesn't return thread events. */
361 if (status.kind != TARGET_WAITKIND_LOADED)
363 cached_status = status;
368 struct thread_resume resume;
370 resume.thread = minus_one_ptid;
371 resume.kind = resume_continue;
374 win32_resume (&resume, 1);
379 win32_ensure_ntdll_loaded ();
383 /* Resume all artificially suspended threads if we are continuing
386 continue_one_thread (struct inferior_list_entry *this_thread, void *id_ptr)
388 struct thread_info *thread = (struct thread_info *) this_thread;
389 int thread_id = * (int *) id_ptr;
390 win32_thread_info *th = inferior_target_data (thread);
392 if ((thread_id == -1 || thread_id == th->tid)
395 if (th->context.ContextFlags)
397 win32_set_thread_context (th);
398 th->context.ContextFlags = 0;
401 if (ResumeThread (th->h) == (DWORD) -1)
403 DWORD err = GetLastError ();
404 OUTMSG (("warning: ResumeThread failed in continue_one_thread, "
405 "(error %d): %s\n", (int) err, strwinerror (err)));
414 child_continue (DWORD continue_status, int thread_id)
416 /* The inferior will only continue after the ContinueDebugEvent
418 find_inferior (&all_threads, continue_one_thread, &thread_id);
419 faked_breakpoint = 0;
421 if (!ContinueDebugEvent (current_event.dwProcessId,
422 current_event.dwThreadId,
429 /* Fetch register(s) from the current thread context. */
431 child_fetch_inferior_registers (struct regcache *regcache, int r)
434 win32_thread_info *th = thread_rec (current_inferior_ptid (), TRUE);
435 if (r == -1 || r > NUM_REGS)
436 child_fetch_inferior_registers (regcache, NUM_REGS);
438 for (regno = 0; regno < r; regno++)
439 (*the_low_target.fetch_inferior_register) (regcache, th, regno);
442 /* Store a new register value into the current thread context. We don't
443 change the program's context until later, when we resume it. */
445 child_store_inferior_registers (struct regcache *regcache, int r)
448 win32_thread_info *th = thread_rec (current_inferior_ptid (), TRUE);
449 if (r == -1 || r == 0 || r > NUM_REGS)
450 child_store_inferior_registers (regcache, NUM_REGS);
452 for (regno = 0; regno < r; regno++)
453 (*the_low_target.store_inferior_register) (regcache, th, regno);
456 /* Map the Windows error number in ERROR to a locale-dependent error
457 message string and return a pointer to it. Typically, the values
458 for ERROR come from GetLastError.
460 The string pointed to shall not be modified by the application,
461 but may be overwritten by a subsequent call to strwinerror
463 The strwinerror function does not change the current setting
467 strwinerror (DWORD error)
469 static char buf[1024];
471 DWORD lasterr = GetLastError ();
472 DWORD chars = FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
473 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
476 0, /* Default language */
482 /* If there is an \r\n appended, zap it. */
484 && msgbuf[chars - 2] == '\r'
485 && msgbuf[chars - 1] == '\n')
491 if (chars > ((COUNTOF (buf)) - 1))
493 chars = COUNTOF (buf) - 1;
498 wcstombs (buf, msgbuf, chars + 1);
500 strncpy (buf, msgbuf, chars + 1);
505 sprintf (buf, "unknown win32 error (%u)", (unsigned) error);
507 SetLastError (lasterr);
512 create_process (const char *program, char *args,
513 DWORD flags, PROCESS_INFORMATION *pi)
518 wchar_t *p, *wprogram, *wargs;
521 wprogram = alloca ((strlen (program) + 1) * sizeof (wchar_t));
522 mbstowcs (wprogram, program, strlen (program) + 1);
524 for (p = wprogram; *p; ++p)
528 argslen = strlen (args);
529 wargs = alloca ((argslen + 1) * sizeof (wchar_t));
530 mbstowcs (wargs, args, argslen + 1);
532 ret = CreateProcessW (wprogram, /* image name */
533 wargs, /* command line */
534 NULL, /* security, not supported */
535 NULL, /* thread, not supported */
536 FALSE, /* inherit handles, not supported */
537 flags, /* start flags */
538 NULL, /* environment, not supported */
539 NULL, /* current directory, not supported */
540 NULL, /* start info, not supported */
543 STARTUPINFOA si = { sizeof (STARTUPINFOA) };
545 ret = CreateProcessA (program, /* image name */
546 args, /* command line */
549 TRUE, /* inherit handles */
550 flags, /* start flags */
551 NULL, /* environment */
552 NULL, /* current directory */
553 &si, /* start info */
560 /* Start a new process.
561 PROGRAM is a path to the program to execute.
562 ARGS is a standard NULL-terminated array of arguments,
563 to be passed to the inferior as ``argv''.
564 Returns the new PID on success, -1 on failure. Registers the new
565 process with the process list. */
567 win32_create_inferior (char *program, char **program_args)
570 char real_path[PATH_MAX];
571 char *orig_path, *new_path, *path_ptr;
578 PROCESS_INFORMATION pi;
581 /* win32_wait needs to know we're not attaching. */
585 error ("No executable specified, specify executable to debug.\n");
587 flags = DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS;
591 path_ptr = getenv ("PATH");
594 int size = cygwin_conv_path_list (CCP_POSIX_TO_WIN_A, path_ptr, NULL, 0);
595 orig_path = alloca (strlen (path_ptr) + 1);
596 new_path = alloca (size);
597 strcpy (orig_path, path_ptr);
598 cygwin_conv_path_list (CCP_POSIX_TO_WIN_A, path_ptr, new_path, size);
599 setenv ("PATH", new_path, 1);
601 cygwin_conv_path (CCP_POSIX_TO_WIN_A, program, real_path, PATH_MAX);
606 for (argc = 1; program_args[argc]; argc++)
607 argslen += strlen (program_args[argc]) + 1;
608 args = alloca (argslen);
610 for (argc = 1; program_args[argc]; argc++)
612 /* FIXME: Can we do better about quoting? How does Cygwin
615 strcat (args, program_args[argc]);
617 OUTMSG2 (("Command line is \"%s\"\n", args));
619 #ifdef CREATE_NEW_PROCESS_GROUP
620 flags |= CREATE_NEW_PROCESS_GROUP;
623 ret = create_process (program, args, flags, &pi);
624 err = GetLastError ();
625 if (!ret && err == ERROR_FILE_NOT_FOUND)
627 char *exename = alloca (strlen (program) + 5);
628 strcat (strcpy (exename, program), ".exe");
629 ret = create_process (exename, args, flags, &pi);
630 err = GetLastError ();
635 setenv ("PATH", orig_path, 1);
640 error ("Error creating process \"%s%s\", (error %d): %s\n",
641 program, args, (int) err, strwinerror (err));
645 OUTMSG2 (("Process created: %s\n", (char *) args));
649 /* On Windows CE this handle can't be closed. The OS reuses
650 it in the debug events, while the 9x/NT versions of Windows
651 probably use a DuplicateHandle'd one. */
652 CloseHandle (pi.hThread);
655 do_initial_child_stuff (pi.hProcess, pi.dwProcessId, 0);
657 return current_process_id;
660 /* Attach to a running process.
661 PID is the process ID to attach to, specified by the user
662 or a higher layer. */
664 win32_attach (unsigned long pid)
667 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit = NULL;
670 HMODULE dll = GetModuleHandle (_T("COREDLL.DLL"));
672 HMODULE dll = GetModuleHandle (_T("KERNEL32.DLL"));
674 DebugSetProcessKillOnExit = GETPROCADDRESS (dll, DebugSetProcessKillOnExit);
676 h = OpenProcess (PROCESS_ALL_ACCESS, FALSE, pid);
679 if (DebugActiveProcess (pid))
681 if (DebugSetProcessKillOnExit != NULL)
682 DebugSetProcessKillOnExit (FALSE);
684 /* win32_wait needs to know we're attaching. */
686 do_initial_child_stuff (h, pid, 1);
693 err = GetLastError ();
694 error ("Attach to process failed (error %d): %s\n",
695 (int) err, strwinerror (err));
698 /* Handle OUTPUT_DEBUG_STRING_EVENT from child process. */
700 handle_output_debug_string (struct target_waitstatus *ourstatus)
702 #define READ_BUFFER_LEN 1024
704 char s[READ_BUFFER_LEN + 1] = { 0 };
705 DWORD nbytes = current_event.u.DebugString.nDebugStringLength;
710 if (nbytes > READ_BUFFER_LEN)
711 nbytes = READ_BUFFER_LEN;
713 addr = (CORE_ADDR) (size_t) current_event.u.DebugString.lpDebugStringData;
715 if (current_event.u.DebugString.fUnicode)
717 /* The event tells us how many bytes, not chars, even
719 WCHAR buffer[(READ_BUFFER_LEN + 1) / sizeof (WCHAR)] = { 0 };
720 if (read_inferior_memory (addr, (unsigned char *) buffer, nbytes) != 0)
722 wcstombs (s, buffer, (nbytes + 1) / sizeof (WCHAR));
726 if (read_inferior_memory (addr, (unsigned char *) s, nbytes) != 0)
730 if (strncmp (s, "cYg", 3) != 0)
740 #undef READ_BUFFER_LEN
744 win32_clear_inferiors (void)
746 if (current_process_handle != NULL)
747 CloseHandle (current_process_handle);
749 for_each_inferior (&all_threads, delete_thread_info);
753 /* Kill all inferiors. */
757 struct process_info *process;
759 if (current_process_handle == NULL)
762 TerminateProcess (current_process_handle, 0);
765 if (!child_continue (DBG_CONTINUE, -1))
767 if (!WaitForDebugEvent (¤t_event, INFINITE))
769 if (current_event.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT)
771 else if (current_event.dwDebugEventCode == OUTPUT_DEBUG_STRING_EVENT)
773 struct target_waitstatus our_status = { 0 };
774 handle_output_debug_string (&our_status);
778 win32_clear_inferiors ();
780 process = find_process_pid (pid);
781 remove_process (process);
785 /* Detach from inferior PID. */
787 win32_detach (int pid)
789 struct process_info *process;
790 winapi_DebugActiveProcessStop DebugActiveProcessStop = NULL;
791 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit = NULL;
793 HMODULE dll = GetModuleHandle (_T("COREDLL.DLL"));
795 HMODULE dll = GetModuleHandle (_T("KERNEL32.DLL"));
797 DebugActiveProcessStop = GETPROCADDRESS (dll, DebugActiveProcessStop);
798 DebugSetProcessKillOnExit = GETPROCADDRESS (dll, DebugSetProcessKillOnExit);
800 if (DebugSetProcessKillOnExit == NULL
801 || DebugActiveProcessStop == NULL)
805 struct thread_resume resume;
806 resume.thread = minus_one_ptid;
807 resume.kind = resume_continue;
809 win32_resume (&resume, 1);
812 if (!DebugActiveProcessStop (current_process_id))
815 DebugSetProcessKillOnExit (FALSE);
816 process = find_process_pid (pid);
817 remove_process (process);
819 win32_clear_inferiors ();
824 win32_mourn (struct process_info *process)
826 remove_process (process);
829 /* Wait for inferiors to end. */
833 HANDLE h = OpenProcess (PROCESS_ALL_ACCESS, FALSE, pid);
836 WaitForSingleObject (h, INFINITE);
841 /* Return 1 iff the thread with thread ID TID is alive. */
843 win32_thread_alive (ptid_t ptid)
847 /* Our thread list is reliable; don't bother to poll target
849 if (find_inferior_id (&all_threads, ptid) != NULL)
856 /* Resume the inferior process. RESUME_INFO describes how we want
859 win32_resume (struct thread_resume *resume_info, size_t n)
864 win32_thread_info *th;
865 DWORD continue_status = DBG_CONTINUE;
868 /* This handles the very limited set of resume packets that GDB can
869 currently produce. */
871 if (n == 1 && ptid_equal (resume_info[0].thread, minus_one_ptid))
876 /* Yes, we're ignoring resume_info[0].thread. It'd be tricky to make
877 the Windows resume code do the right thing for thread switching. */
878 tid = current_event.dwThreadId;
880 if (!ptid_equal (resume_info[0].thread, minus_one_ptid))
882 sig = resume_info[0].sig;
883 step = resume_info[0].kind == resume_step;
891 if (sig != GDB_SIGNAL_0)
893 if (current_event.dwDebugEventCode != EXCEPTION_DEBUG_EVENT)
895 OUTMSG (("Cannot continue with signal %d here.\n", sig));
897 else if (sig == last_sig)
898 continue_status = DBG_EXCEPTION_NOT_HANDLED;
900 OUTMSG (("Can only continue with recieved signal %d.\n", last_sig));
903 last_sig = GDB_SIGNAL_0;
905 /* Get context for the currently selected thread. */
906 ptid = debug_event_ptid (¤t_event);
907 th = thread_rec (ptid, FALSE);
910 if (th->context.ContextFlags)
912 /* Move register values from the inferior into the thread
913 context structure. */
914 regcache_invalidate ();
918 if (the_low_target.single_step != NULL)
919 (*the_low_target.single_step) (th);
921 error ("Single stepping is not supported "
922 "in this configuration.\n");
925 win32_set_thread_context (th);
926 th->context.ContextFlags = 0;
930 /* Allow continuing with the same signal that interrupted us.
931 Otherwise complain. */
933 child_continue (continue_status, tid);
937 win32_add_one_solib (const char *name, CORE_ADDR load_addr)
939 char buf[MAX_PATH + 1];
940 char buf2[MAX_PATH + 1];
943 WIN32_FIND_DATA w32_fd;
944 WCHAR wname[MAX_PATH + 1];
945 mbstowcs (wname, name, MAX_PATH);
946 HANDLE h = FindFirstFile (wname, &w32_fd);
948 WIN32_FIND_DATAA w32_fd;
949 HANDLE h = FindFirstFileA (name, &w32_fd);
952 if (h == INVALID_HANDLE_VALUE)
960 char cwd[MAX_PATH + 1];
962 if (GetCurrentDirectoryA (MAX_PATH + 1, cwd))
964 p = strrchr (buf, '\\');
967 SetCurrentDirectoryA (buf);
968 GetFullPathNameA (w32_fd.cFileName, MAX_PATH, buf, &p);
969 SetCurrentDirectoryA (cwd);
976 if (strcasecmp (buf, "ntdll.dll") == 0)
978 GetSystemDirectoryA (buf, sizeof (buf));
979 strcat (buf, "\\ntdll.dll");
984 cygwin_conv_path (CCP_WIN_A_TO_POSIX, buf, buf2, sizeof (buf2));
989 loaded_dll (buf2, load_addr);
993 get_image_name (HANDLE h, void *address, int unicode)
995 static char buf[(2 * MAX_PATH) + 1];
996 DWORD size = unicode ? sizeof (WCHAR) : sizeof (char);
1002 /* Attempt to read the name of the dll that was detected.
1003 This is documented to work only when actively debugging
1004 a program. It will not work for attached processes. */
1005 if (address == NULL)
1009 /* Windows CE reports the address of the image name,
1010 instead of an address of a pointer into the image name. */
1011 address_ptr = address;
1013 /* See if we could read the address of a string, and that the
1014 address isn't null. */
1015 if (!ReadProcessMemory (h, address, &address_ptr,
1016 sizeof (address_ptr), &done)
1017 || done != sizeof (address_ptr)
1022 /* Find the length of the string */
1023 while (ReadProcessMemory (h, address_ptr + len++ * size, &b, size, &done)
1024 && (b[0] != 0 || b[size - 1] != 0) && done == size)
1028 ReadProcessMemory (h, address_ptr, buf, len, &done);
1031 WCHAR *unicode_address = (WCHAR *) alloca (len * sizeof (WCHAR));
1032 ReadProcessMemory (h, address_ptr, unicode_address, len * sizeof (WCHAR),
1035 WideCharToMultiByte (CP_ACP, 0, unicode_address, len, buf, len, 0, 0);
1041 typedef BOOL (WINAPI *winapi_EnumProcessModules) (HANDLE, HMODULE *,
1043 typedef BOOL (WINAPI *winapi_GetModuleInformation) (HANDLE, HMODULE,
1044 LPMODULEINFO, DWORD);
1045 typedef DWORD (WINAPI *winapi_GetModuleFileNameExA) (HANDLE, HMODULE,
1048 static winapi_EnumProcessModules win32_EnumProcessModules;
1049 static winapi_GetModuleInformation win32_GetModuleInformation;
1050 static winapi_GetModuleFileNameExA win32_GetModuleFileNameExA;
1055 static int psapi_loaded = 0;
1056 static HMODULE dll = NULL;
1061 dll = LoadLibrary (TEXT("psapi.dll"));
1064 win32_EnumProcessModules =
1065 GETPROCADDRESS (dll, EnumProcessModules);
1066 win32_GetModuleInformation =
1067 GETPROCADDRESS (dll, GetModuleInformation);
1068 win32_GetModuleFileNameExA =
1069 GETPROCADDRESS (dll, GetModuleFileNameExA);
1072 return (win32_EnumProcessModules != NULL
1073 && win32_GetModuleInformation != NULL
1074 && win32_GetModuleFileNameExA != NULL);
1078 psapi_get_dll_name (LPVOID BaseAddress, char *dll_name_ret)
1084 HMODULE *DllHandle = dh_buf;
1092 ok = (*win32_EnumProcessModules) (current_process_handle,
1097 if (!ok || !cbNeeded)
1100 DllHandle = (HMODULE *) alloca (cbNeeded);
1104 ok = (*win32_EnumProcessModules) (current_process_handle,
1111 for (i = 0; i < ((size_t) cbNeeded / sizeof (HMODULE)); i++)
1113 if (!(*win32_GetModuleInformation) (current_process_handle,
1118 DWORD err = GetLastError ();
1119 error ("Can't get module info: (error %d): %s\n",
1120 (int) err, strwinerror (err));
1123 if (mi.lpBaseOfDll == BaseAddress)
1125 len = (*win32_GetModuleFileNameExA) (current_process_handle,
1131 DWORD err = GetLastError ();
1132 error ("Error getting dll name: (error %d): %s\n",
1133 (int) err, strwinerror (err));
1140 dll_name_ret[0] = '\0';
1145 /* On certain versions of Windows, the information about ntdll.dll
1146 is not available yet at the time we get the LOAD_DLL_DEBUG_EVENT,
1147 thus preventing us from reporting this DLL as an SO. This has been
1148 witnessed on Windows 8.1, for instance. A possible explanation
1149 is that ntdll.dll might be mapped before the SO info gets created
1150 by the Windows system -- ntdll.dll is the first DLL to be reported
1151 via LOAD_DLL_DEBUG_EVENT and other DLLs do not seem to suffer from
1154 If we indeed are missing ntdll.dll, this function tries to recover
1155 from this issue, after the fact. Do nothing if we encounter any
1156 issue trying to locate that DLL. */
1159 win32_ensure_ntdll_loaded (void)
1161 struct inferior_list_entry *dll_e;
1164 HMODULE *DllHandle = dh_buf;
1168 for (dll_e = all_dlls.head; dll_e != NULL; dll_e = dll_e->next)
1170 struct dll_info *dll = (struct dll_info *) dll_e;
1172 if (strcasecmp (lbasename (dll->name), "ntdll.dll") == 0)
1180 ok = (*win32_EnumProcessModules) (current_process_handle,
1185 if (!ok || !cbNeeded)
1188 DllHandle = (HMODULE *) alloca (cbNeeded);
1192 ok = (*win32_EnumProcessModules) (current_process_handle,
1199 for (i = 0; i < ((size_t) cbNeeded / sizeof (HMODULE)); i++)
1202 char dll_name[MAX_PATH];
1204 if (!(*win32_GetModuleInformation) (current_process_handle,
1209 if ((*win32_GetModuleFileNameExA) (current_process_handle,
1214 if (strcasecmp (lbasename (dll_name), "ntdll.dll") == 0)
1216 win32_add_one_solib (dll_name,
1217 (CORE_ADDR) (uintptr_t) mi.lpBaseOfDll);
1224 typedef HANDLE (WINAPI *winapi_CreateToolhelp32Snapshot) (DWORD, DWORD);
1225 typedef BOOL (WINAPI *winapi_Module32First) (HANDLE, LPMODULEENTRY32);
1226 typedef BOOL (WINAPI *winapi_Module32Next) (HANDLE, LPMODULEENTRY32);
1228 static winapi_CreateToolhelp32Snapshot win32_CreateToolhelp32Snapshot;
1229 static winapi_Module32First win32_Module32First;
1230 static winapi_Module32Next win32_Module32Next;
1232 typedef BOOL (WINAPI *winapi_CloseToolhelp32Snapshot) (HANDLE);
1233 static winapi_CloseToolhelp32Snapshot win32_CloseToolhelp32Snapshot;
1237 load_toolhelp (void)
1239 static int toolhelp_loaded = 0;
1240 static HMODULE dll = NULL;
1242 if (!toolhelp_loaded)
1244 toolhelp_loaded = 1;
1246 dll = GetModuleHandle (_T("KERNEL32.DLL"));
1248 dll = LoadLibrary (L"TOOLHELP.DLL");
1253 win32_CreateToolhelp32Snapshot =
1254 GETPROCADDRESS (dll, CreateToolhelp32Snapshot);
1255 win32_Module32First = GETPROCADDRESS (dll, Module32First);
1256 win32_Module32Next = GETPROCADDRESS (dll, Module32Next);
1258 win32_CloseToolhelp32Snapshot =
1259 GETPROCADDRESS (dll, CloseToolhelp32Snapshot);
1263 return (win32_CreateToolhelp32Snapshot != NULL
1264 && win32_Module32First != NULL
1265 && win32_Module32Next != NULL
1267 && win32_CloseToolhelp32Snapshot != NULL
1273 toolhelp_get_dll_name (LPVOID BaseAddress, char *dll_name_ret)
1275 HANDLE snapshot_module;
1276 MODULEENTRY32 modEntry = { sizeof (MODULEENTRY32) };
1279 if (!load_toolhelp ())
1282 snapshot_module = win32_CreateToolhelp32Snapshot (TH32CS_SNAPMODULE,
1283 current_event.dwProcessId);
1284 if (snapshot_module == INVALID_HANDLE_VALUE)
1287 /* Ignore the first module, which is the exe. */
1288 if (win32_Module32First (snapshot_module, &modEntry))
1289 while (win32_Module32Next (snapshot_module, &modEntry))
1290 if (modEntry.modBaseAddr == BaseAddress)
1293 wcstombs (dll_name_ret, modEntry.szExePath, MAX_PATH + 1);
1295 strcpy (dll_name_ret, modEntry.szExePath);
1302 win32_CloseToolhelp32Snapshot (snapshot_module);
1304 CloseHandle (snapshot_module);
1310 handle_load_dll (void)
1312 LOAD_DLL_DEBUG_INFO *event = ¤t_event.u.LoadDll;
1313 char dll_buf[MAX_PATH + 1];
1314 char *dll_name = NULL;
1315 CORE_ADDR load_addr;
1317 dll_buf[0] = dll_buf[sizeof (dll_buf) - 1] = '\0';
1319 /* Windows does not report the image name of the dlls in the debug
1320 event on attaches. We resort to iterating over the list of
1321 loaded dlls looking for a match by image base. */
1322 if (!psapi_get_dll_name (event->lpBaseOfDll, dll_buf))
1324 if (!server_waiting)
1325 /* On some versions of Windows and Windows CE, we can't create
1326 toolhelp snapshots while the inferior is stopped in a
1327 LOAD_DLL_DEBUG_EVENT due to a dll load, but we can while
1328 Windows is reporting the already loaded dlls. */
1329 toolhelp_get_dll_name (event->lpBaseOfDll, dll_buf);
1334 if (*dll_name == '\0')
1335 dll_name = get_image_name (current_process_handle,
1336 event->lpImageName, event->fUnicode);
1340 /* The symbols in a dll are offset by 0x1000, which is the
1341 offset from 0 of the first byte in an image - because
1342 of the file header and the section alignment. */
1344 load_addr = (CORE_ADDR) (uintptr_t) event->lpBaseOfDll + 0x1000;
1345 win32_add_one_solib (dll_name, load_addr);
1349 handle_unload_dll (void)
1351 CORE_ADDR load_addr =
1352 (CORE_ADDR) (uintptr_t) current_event.u.UnloadDll.lpBaseOfDll;
1353 load_addr += 0x1000;
1354 unloaded_dll (NULL, load_addr);
1358 handle_exception (struct target_waitstatus *ourstatus)
1360 DWORD code = current_event.u.Exception.ExceptionRecord.ExceptionCode;
1362 ourstatus->kind = TARGET_WAITKIND_STOPPED;
1366 case EXCEPTION_ACCESS_VIOLATION:
1367 OUTMSG2 (("EXCEPTION_ACCESS_VIOLATION"));
1368 ourstatus->value.sig = GDB_SIGNAL_SEGV;
1370 case STATUS_STACK_OVERFLOW:
1371 OUTMSG2 (("STATUS_STACK_OVERFLOW"));
1372 ourstatus->value.sig = GDB_SIGNAL_SEGV;
1374 case STATUS_FLOAT_DENORMAL_OPERAND:
1375 OUTMSG2 (("STATUS_FLOAT_DENORMAL_OPERAND"));
1376 ourstatus->value.sig = GDB_SIGNAL_FPE;
1378 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
1379 OUTMSG2 (("EXCEPTION_ARRAY_BOUNDS_EXCEEDED"));
1380 ourstatus->value.sig = GDB_SIGNAL_FPE;
1382 case STATUS_FLOAT_INEXACT_RESULT:
1383 OUTMSG2 (("STATUS_FLOAT_INEXACT_RESULT"));
1384 ourstatus->value.sig = GDB_SIGNAL_FPE;
1386 case STATUS_FLOAT_INVALID_OPERATION:
1387 OUTMSG2 (("STATUS_FLOAT_INVALID_OPERATION"));
1388 ourstatus->value.sig = GDB_SIGNAL_FPE;
1390 case STATUS_FLOAT_OVERFLOW:
1391 OUTMSG2 (("STATUS_FLOAT_OVERFLOW"));
1392 ourstatus->value.sig = GDB_SIGNAL_FPE;
1394 case STATUS_FLOAT_STACK_CHECK:
1395 OUTMSG2 (("STATUS_FLOAT_STACK_CHECK"));
1396 ourstatus->value.sig = GDB_SIGNAL_FPE;
1398 case STATUS_FLOAT_UNDERFLOW:
1399 OUTMSG2 (("STATUS_FLOAT_UNDERFLOW"));
1400 ourstatus->value.sig = GDB_SIGNAL_FPE;
1402 case STATUS_FLOAT_DIVIDE_BY_ZERO:
1403 OUTMSG2 (("STATUS_FLOAT_DIVIDE_BY_ZERO"));
1404 ourstatus->value.sig = GDB_SIGNAL_FPE;
1406 case STATUS_INTEGER_DIVIDE_BY_ZERO:
1407 OUTMSG2 (("STATUS_INTEGER_DIVIDE_BY_ZERO"));
1408 ourstatus->value.sig = GDB_SIGNAL_FPE;
1410 case STATUS_INTEGER_OVERFLOW:
1411 OUTMSG2 (("STATUS_INTEGER_OVERFLOW"));
1412 ourstatus->value.sig = GDB_SIGNAL_FPE;
1414 case EXCEPTION_BREAKPOINT:
1415 OUTMSG2 (("EXCEPTION_BREAKPOINT"));
1416 ourstatus->value.sig = GDB_SIGNAL_TRAP;
1418 /* Remove the initial breakpoint. */
1419 check_breakpoints ((CORE_ADDR) (long) current_event
1420 .u.Exception.ExceptionRecord.ExceptionAddress);
1424 OUTMSG2 (("DBG_CONTROL_C"));
1425 ourstatus->value.sig = GDB_SIGNAL_INT;
1427 case DBG_CONTROL_BREAK:
1428 OUTMSG2 (("DBG_CONTROL_BREAK"));
1429 ourstatus->value.sig = GDB_SIGNAL_INT;
1431 case EXCEPTION_SINGLE_STEP:
1432 OUTMSG2 (("EXCEPTION_SINGLE_STEP"));
1433 ourstatus->value.sig = GDB_SIGNAL_TRAP;
1435 case EXCEPTION_ILLEGAL_INSTRUCTION:
1436 OUTMSG2 (("EXCEPTION_ILLEGAL_INSTRUCTION"));
1437 ourstatus->value.sig = GDB_SIGNAL_ILL;
1439 case EXCEPTION_PRIV_INSTRUCTION:
1440 OUTMSG2 (("EXCEPTION_PRIV_INSTRUCTION"));
1441 ourstatus->value.sig = GDB_SIGNAL_ILL;
1443 case EXCEPTION_NONCONTINUABLE_EXCEPTION:
1444 OUTMSG2 (("EXCEPTION_NONCONTINUABLE_EXCEPTION"));
1445 ourstatus->value.sig = GDB_SIGNAL_ILL;
1448 if (current_event.u.Exception.dwFirstChance)
1450 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
1453 OUTMSG2 (("gdbserver: unknown target exception 0x%08x at 0x%s",
1454 (unsigned) current_event.u.Exception.ExceptionRecord.ExceptionCode,
1455 phex_nz ((uintptr_t) current_event.u.Exception.ExceptionRecord.
1456 ExceptionAddress, sizeof (uintptr_t))));
1457 ourstatus->value.sig = GDB_SIGNAL_UNKNOWN;
1461 last_sig = ourstatus->value.sig;
1466 suspend_one_thread (struct inferior_list_entry *entry)
1468 struct thread_info *thread = (struct thread_info *) entry;
1469 win32_thread_info *th = inferior_target_data (thread);
1473 if (SuspendThread (th->h) == (DWORD) -1)
1475 DWORD err = GetLastError ();
1476 OUTMSG (("warning: SuspendThread failed in suspend_one_thread, "
1477 "(error %d): %s\n", (int) err, strwinerror (err)));
1485 fake_breakpoint_event (void)
1487 OUTMSG2(("fake_breakpoint_event\n"));
1489 faked_breakpoint = 1;
1491 memset (¤t_event, 0, sizeof (current_event));
1492 current_event.dwThreadId = main_thread_id;
1493 current_event.dwDebugEventCode = EXCEPTION_DEBUG_EVENT;
1494 current_event.u.Exception.ExceptionRecord.ExceptionCode
1495 = EXCEPTION_BREAKPOINT;
1497 for_each_inferior (&all_threads, suspend_one_thread);
1502 auto_delete_breakpoint (CORE_ADDR stop_pc)
1508 /* Get the next event from the child. */
1511 get_child_debug_event (struct target_waitstatus *ourstatus)
1515 last_sig = GDB_SIGNAL_0;
1516 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
1518 /* Check if GDB sent us an interrupt request. */
1519 check_remote_input_interrupt_request ();
1521 if (soft_interrupt_requested)
1523 soft_interrupt_requested = 0;
1524 fake_breakpoint_event ();
1533 /* WinCE doesn't set an initial breakpoint automatically. To
1534 stop the inferior, we flush all currently pending debug
1535 events -- the thread list and the dll list are always
1536 reported immediatelly without delay, then, we suspend all
1537 threads and pretend we saw a trap at the current PC of the
1540 Contrary to desktop Windows, Windows CE *does* report the dll
1541 names on LOAD_DLL_DEBUG_EVENTs resulting from a
1542 DebugActiveProcess call. This limits the way we can detect
1543 if all the dlls have already been reported. If we get a real
1544 debug event before leaving attaching, the worst that will
1545 happen is the user will see a spurious breakpoint. */
1547 current_event.dwDebugEventCode = 0;
1548 if (!WaitForDebugEvent (¤t_event, 0))
1550 OUTMSG2(("no attach events left\n"));
1551 fake_breakpoint_event ();
1555 OUTMSG2(("got attach event\n"));
1560 /* Keep the wait time low enough for confortable remote
1561 interruption, but high enough so gdbserver doesn't become a
1563 if (!WaitForDebugEvent (¤t_event, 250))
1565 DWORD e = GetLastError();
1567 if (e == ERROR_PIPE_NOT_CONNECTED)
1569 /* This will happen if the loader fails to succesfully
1570 load the application, e.g., if the main executable
1571 tries to pull in a non-existing export from a
1573 ourstatus->kind = TARGET_WAITKIND_EXITED;
1574 ourstatus->value.integer = 1;
1584 switch (current_event.dwDebugEventCode)
1586 case CREATE_THREAD_DEBUG_EVENT:
1587 OUTMSG2 (("gdbserver: kernel event CREATE_THREAD_DEBUG_EVENT "
1588 "for pid=%u tid=%x)\n",
1589 (unsigned) current_event.dwProcessId,
1590 (unsigned) current_event.dwThreadId));
1592 /* Record the existence of this thread. */
1593 child_add_thread (current_event.dwProcessId,
1594 current_event.dwThreadId,
1595 current_event.u.CreateThread.hThread,
1596 current_event.u.CreateThread.lpThreadLocalBase);
1599 case EXIT_THREAD_DEBUG_EVENT:
1600 OUTMSG2 (("gdbserver: kernel event EXIT_THREAD_DEBUG_EVENT "
1601 "for pid=%u tid=%x\n",
1602 (unsigned) current_event.dwProcessId,
1603 (unsigned) current_event.dwThreadId));
1604 child_delete_thread (current_event.dwProcessId,
1605 current_event.dwThreadId);
1607 current_inferior = (struct thread_info *) all_threads.head;
1610 case CREATE_PROCESS_DEBUG_EVENT:
1611 OUTMSG2 (("gdbserver: kernel event CREATE_PROCESS_DEBUG_EVENT "
1612 "for pid=%u tid=%x\n",
1613 (unsigned) current_event.dwProcessId,
1614 (unsigned) current_event.dwThreadId));
1615 CloseHandle (current_event.u.CreateProcessInfo.hFile);
1617 current_process_handle = current_event.u.CreateProcessInfo.hProcess;
1618 main_thread_id = current_event.dwThreadId;
1620 ourstatus->kind = TARGET_WAITKIND_EXECD;
1621 ourstatus->value.execd_pathname = "Main executable";
1623 /* Add the main thread. */
1624 child_add_thread (current_event.dwProcessId,
1626 current_event.u.CreateProcessInfo.hThread,
1627 current_event.u.CreateProcessInfo.lpThreadLocalBase);
1629 ourstatus->value.related_pid = debug_event_ptid (¤t_event);
1633 /* Windows CE doesn't set the initial breakpoint
1634 automatically like the desktop versions of Windows do.
1635 We add it explicitly here. It will be removed as soon as
1637 set_breakpoint_at ((CORE_ADDR) (long) current_event.u
1638 .CreateProcessInfo.lpStartAddress,
1639 auto_delete_breakpoint);
1644 case EXIT_PROCESS_DEBUG_EVENT:
1645 OUTMSG2 (("gdbserver: kernel event EXIT_PROCESS_DEBUG_EVENT "
1646 "for pid=%u tid=%x\n",
1647 (unsigned) current_event.dwProcessId,
1648 (unsigned) current_event.dwThreadId));
1649 ourstatus->kind = TARGET_WAITKIND_EXITED;
1650 ourstatus->value.integer = current_event.u.ExitProcess.dwExitCode;
1651 child_continue (DBG_CONTINUE, -1);
1652 CloseHandle (current_process_handle);
1653 current_process_handle = NULL;
1656 case LOAD_DLL_DEBUG_EVENT:
1657 OUTMSG2 (("gdbserver: kernel event LOAD_DLL_DEBUG_EVENT "
1658 "for pid=%u tid=%x\n",
1659 (unsigned) current_event.dwProcessId,
1660 (unsigned) current_event.dwThreadId));
1661 CloseHandle (current_event.u.LoadDll.hFile);
1664 ourstatus->kind = TARGET_WAITKIND_LOADED;
1665 ourstatus->value.sig = GDB_SIGNAL_TRAP;
1668 case UNLOAD_DLL_DEBUG_EVENT:
1669 OUTMSG2 (("gdbserver: kernel event UNLOAD_DLL_DEBUG_EVENT "
1670 "for pid=%u tid=%x\n",
1671 (unsigned) current_event.dwProcessId,
1672 (unsigned) current_event.dwThreadId));
1673 handle_unload_dll ();
1674 ourstatus->kind = TARGET_WAITKIND_LOADED;
1675 ourstatus->value.sig = GDB_SIGNAL_TRAP;
1678 case EXCEPTION_DEBUG_EVENT:
1679 OUTMSG2 (("gdbserver: kernel event EXCEPTION_DEBUG_EVENT "
1680 "for pid=%u tid=%x\n",
1681 (unsigned) current_event.dwProcessId,
1682 (unsigned) current_event.dwThreadId));
1683 handle_exception (ourstatus);
1686 case OUTPUT_DEBUG_STRING_EVENT:
1687 /* A message from the kernel (or Cygwin). */
1688 OUTMSG2 (("gdbserver: kernel event OUTPUT_DEBUG_STRING_EVENT "
1689 "for pid=%u tid=%x\n",
1690 (unsigned) current_event.dwProcessId,
1691 (unsigned) current_event.dwThreadId));
1692 handle_output_debug_string (ourstatus);
1696 OUTMSG2 (("gdbserver: kernel event unknown "
1697 "for pid=%u tid=%x code=%x\n",
1698 (unsigned) current_event.dwProcessId,
1699 (unsigned) current_event.dwThreadId,
1700 (unsigned) current_event.dwDebugEventCode));
1704 ptid = debug_event_ptid (¤t_event);
1706 (struct thread_info *) find_inferior_id (&all_threads, ptid);
1710 /* Wait for the inferior process to change state.
1711 STATUS will be filled in with a response code to send to GDB.
1712 Returns the signal which caused the process to stop. */
1714 win32_wait (ptid_t ptid, struct target_waitstatus *ourstatus, int options)
1716 struct regcache *regcache;
1718 if (cached_status.kind != TARGET_WAITKIND_IGNORE)
1720 /* The core always does a wait after creating the inferior, and
1721 do_initial_child_stuff already ran the inferior to the
1722 initial breakpoint (or an exit, if creating the process
1723 fails). Report it now. */
1724 *ourstatus = cached_status;
1725 cached_status.kind = TARGET_WAITKIND_IGNORE;
1726 return debug_event_ptid (¤t_event);
1731 if (!get_child_debug_event (ourstatus))
1734 switch (ourstatus->kind)
1736 case TARGET_WAITKIND_EXITED:
1737 OUTMSG2 (("Child exited with retcode = %x\n",
1738 ourstatus->value.integer));
1739 win32_clear_inferiors ();
1740 return pid_to_ptid (current_event.dwProcessId);
1741 case TARGET_WAITKIND_STOPPED:
1742 case TARGET_WAITKIND_LOADED:
1743 OUTMSG2 (("Child Stopped with signal = %d \n",
1744 ourstatus->value.sig));
1746 regcache = get_thread_regcache (current_inferior, 1);
1747 child_fetch_inferior_registers (regcache, -1);
1748 return debug_event_ptid (¤t_event);
1750 OUTMSG (("Ignoring unknown internal event, %d\n", ourstatus->kind));
1752 case TARGET_WAITKIND_SPURIOUS:
1753 case TARGET_WAITKIND_EXECD:
1754 /* do nothing, just continue */
1755 child_continue (DBG_CONTINUE, -1);
1761 /* Fetch registers from the inferior process.
1762 If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO. */
1764 win32_fetch_inferior_registers (struct regcache *regcache, int regno)
1766 child_fetch_inferior_registers (regcache, regno);
1769 /* Store registers to the inferior process.
1770 If REGNO is -1, store all registers; otherwise, store at least REGNO. */
1772 win32_store_inferior_registers (struct regcache *regcache, int regno)
1774 child_store_inferior_registers (regcache, regno);
1777 /* Read memory from the inferior process. This should generally be
1778 called through read_inferior_memory, which handles breakpoint shadowing.
1779 Read LEN bytes at MEMADDR into a buffer at MYADDR. */
1781 win32_read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
1783 return child_xfer_memory (memaddr, (char *) myaddr, len, 0, 0) != len;
1786 /* Write memory to the inferior process. This should generally be
1787 called through write_inferior_memory, which handles breakpoint shadowing.
1788 Write LEN bytes from the buffer at MYADDR to MEMADDR.
1789 Returns 0 on success and errno on failure. */
1791 win32_write_inferior_memory (CORE_ADDR memaddr, const unsigned char *myaddr,
1794 return child_xfer_memory (memaddr, (char *) myaddr, len, 1, 0) != len;
1797 /* Send an interrupt request to the inferior process. */
1799 win32_request_interrupt (void)
1801 winapi_DebugBreakProcess DebugBreakProcess;
1802 winapi_GenerateConsoleCtrlEvent GenerateConsoleCtrlEvent;
1805 HMODULE dll = GetModuleHandle (_T("COREDLL.DLL"));
1807 HMODULE dll = GetModuleHandle (_T("KERNEL32.DLL"));
1810 GenerateConsoleCtrlEvent = GETPROCADDRESS (dll, GenerateConsoleCtrlEvent);
1812 if (GenerateConsoleCtrlEvent != NULL
1813 && GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT, current_process_id))
1816 /* GenerateConsoleCtrlEvent can fail if process id being debugged is
1817 not a process group id.
1818 Fallback to XP/Vista 'DebugBreakProcess', which generates a
1819 breakpoint exception in the interior process. */
1821 DebugBreakProcess = GETPROCADDRESS (dll, DebugBreakProcess);
1823 if (DebugBreakProcess != NULL
1824 && DebugBreakProcess (current_process_handle))
1827 /* Last resort, suspend all threads manually. */
1828 soft_interrupt_requested = 1;
1833 win32_error_to_fileio_error (DWORD err)
1837 case ERROR_BAD_PATHNAME:
1838 case ERROR_FILE_NOT_FOUND:
1839 case ERROR_INVALID_NAME:
1840 case ERROR_PATH_NOT_FOUND:
1841 return FILEIO_ENOENT;
1843 case ERROR_IO_DEVICE:
1844 case ERROR_OPEN_FAILED:
1846 case ERROR_INVALID_HANDLE:
1847 return FILEIO_EBADF;
1848 case ERROR_ACCESS_DENIED:
1849 case ERROR_SHARING_VIOLATION:
1850 return FILEIO_EACCES;
1851 case ERROR_NOACCESS:
1852 return FILEIO_EFAULT;
1854 return FILEIO_EBUSY;
1855 case ERROR_ALREADY_EXISTS:
1856 case ERROR_FILE_EXISTS:
1857 return FILEIO_EEXIST;
1858 case ERROR_BAD_DEVICE:
1859 return FILEIO_ENODEV;
1860 case ERROR_DIRECTORY:
1861 return FILEIO_ENOTDIR;
1862 case ERROR_FILENAME_EXCED_RANGE:
1863 case ERROR_INVALID_DATA:
1864 case ERROR_INVALID_PARAMETER:
1865 case ERROR_NEGATIVE_SEEK:
1866 return FILEIO_EINVAL;
1867 case ERROR_TOO_MANY_OPEN_FILES:
1868 return FILEIO_EMFILE;
1869 case ERROR_HANDLE_DISK_FULL:
1870 case ERROR_DISK_FULL:
1871 return FILEIO_ENOSPC;
1872 case ERROR_WRITE_PROTECT:
1873 return FILEIO_EROFS;
1874 case ERROR_NOT_SUPPORTED:
1875 return FILEIO_ENOSYS;
1878 return FILEIO_EUNKNOWN;
1882 wince_hostio_last_error (char *buf)
1884 DWORD winerr = GetLastError ();
1885 int fileio_err = win32_error_to_fileio_error (winerr);
1886 sprintf (buf, "F-1,%x", fileio_err);
1890 /* Write Windows OS Thread Information Block address. */
1893 win32_get_tib_address (ptid_t ptid, CORE_ADDR *addr)
1895 win32_thread_info *th;
1896 th = thread_rec (ptid, 0);
1900 *addr = th->thread_local_base;
1904 static struct target_ops win32_target_ops = {
1905 win32_create_inferior,
1914 win32_fetch_inferior_registers,
1915 win32_store_inferior_registers,
1916 NULL, /* prepare_to_access_memory */
1917 NULL, /* done_accessing_memory */
1918 win32_read_inferior_memory,
1919 win32_write_inferior_memory,
1920 NULL, /* lookup_symbols */
1921 win32_request_interrupt,
1922 NULL, /* read_auxv */
1925 win32_stopped_by_watchpoint,
1926 win32_stopped_data_address,
1927 NULL, /* read_offsets */
1928 NULL, /* get_tls_address */
1929 NULL, /* qxfer_spu */
1931 wince_hostio_last_error,
1933 hostio_last_error_from_errno,
1935 NULL, /* qxfer_osdata */
1936 NULL, /* qxfer_siginfo */
1937 NULL, /* supports_non_stop */
1939 NULL, /* start_non_stop */
1940 NULL, /* supports_multi_process */
1941 NULL, /* handle_monitor_command */
1942 NULL, /* core_of_thread */
1943 NULL, /* read_loadmap */
1944 NULL, /* process_qsupported */
1945 NULL, /* supports_tracepoints */
1947 NULL, /* write_pc */
1948 NULL, /* thread_stopped */
1949 win32_get_tib_address
1952 /* Initialize the Win32 backend. */
1954 initialize_low (void)
1956 set_target_ops (&win32_target_ops);
1957 if (the_low_target.breakpoint != NULL)
1958 set_breakpoint_data (the_low_target.breakpoint,
1959 the_low_target.breakpoint_len);
1960 the_low_target.arch_setup ();