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"
35 #include <sys/param.h>
39 #include <sys/cygwin.h>
42 #define OUTMSG(X) do { printf X; fflush (stderr); } while (0)
55 #define _T(x) TEXT (x)
59 #define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0]))
63 # define GETPROCADDRESS(DLL, PROC) \
64 ((winapi_ ## PROC) GetProcAddress (DLL, TEXT (#PROC)))
66 # define GETPROCADDRESS(DLL, PROC) \
67 ((winapi_ ## PROC) GetProcAddress (DLL, #PROC))
70 int using_threads
= 1;
73 static int attaching
= 0;
74 static HANDLE current_process_handle
= NULL
;
75 static DWORD current_process_id
= 0;
76 static DWORD main_thread_id
= 0;
77 static enum gdb_signal last_sig
= GDB_SIGNAL_0
;
79 /* The current debug event from WaitForDebugEvent. */
80 static DEBUG_EVENT current_event
;
82 /* Non zero if an interrupt request is to be satisfied by suspending
84 static int soft_interrupt_requested
= 0;
86 /* Non zero if the inferior is stopped in a simulated breakpoint done
87 by suspending all the threads. */
88 static int faked_breakpoint
= 0;
90 const struct target_desc
*win32_tdesc
;
92 #define NUM_REGS (the_low_target.num_regs)
94 typedef BOOL
WINAPI (*winapi_DebugActiveProcessStop
) (DWORD dwProcessId
);
95 typedef BOOL
WINAPI (*winapi_DebugSetProcessKillOnExit
) (BOOL KillOnExit
);
96 typedef BOOL
WINAPI (*winapi_DebugBreakProcess
) (HANDLE
);
97 typedef BOOL
WINAPI (*winapi_GenerateConsoleCtrlEvent
) (DWORD
, DWORD
);
99 static void win32_resume (struct thread_resume
*resume_info
, size_t n
);
101 /* Get the thread ID from the current selected inferior (the current
104 current_inferior_ptid (void)
106 return ((struct inferior_list_entry
*) current_inferior
)->id
;
109 /* The current debug event from WaitForDebugEvent. */
111 debug_event_ptid (DEBUG_EVENT
*event
)
113 return ptid_build (event
->dwProcessId
, event
->dwThreadId
, 0);
116 /* Get the thread context of the thread associated with TH. */
119 win32_get_thread_context (win32_thread_info
*th
)
121 memset (&th
->context
, 0, sizeof (CONTEXT
));
122 (*the_low_target
.get_thread_context
) (th
, ¤t_event
);
124 memcpy (&th
->base_context
, &th
->context
, sizeof (CONTEXT
));
128 /* Set the thread context of the thread associated with TH. */
131 win32_set_thread_context (win32_thread_info
*th
)
134 /* Calling SuspendThread on a thread that is running kernel code
135 will report that the suspending was successful, but in fact, that
136 will often not be true. In those cases, the context returned by
137 GetThreadContext will not be correct by the time the thread
138 stops, hence we can't set that context back into the thread when
139 resuming - it will most likelly crash the inferior.
140 Unfortunately, there is no way to know when the thread will
141 really stop. To work around it, we'll only write the context
142 back to the thread when either the user or GDB explicitly change
143 it between stopping and resuming. */
144 if (memcmp (&th
->context
, &th
->base_context
, sizeof (CONTEXT
)) != 0)
146 (*the_low_target
.set_thread_context
) (th
, ¤t_event
);
149 /* Find a thread record given a thread id. If GET_CONTEXT is set then
150 also retrieve the context for this thread. */
151 static win32_thread_info
*
152 thread_rec (ptid_t ptid
, int get_context
)
154 struct thread_info
*thread
;
155 win32_thread_info
*th
;
157 thread
= (struct thread_info
*) find_inferior_id (&all_threads
, ptid
);
161 th
= inferior_target_data (thread
);
162 if (get_context
&& th
->context
.ContextFlags
== 0)
166 if (SuspendThread (th
->h
) == (DWORD
) -1)
168 DWORD err
= GetLastError ();
169 OUTMSG (("warning: SuspendThread failed in thread_rec, "
170 "(error %d): %s\n", (int) err
, strwinerror (err
)));
176 win32_get_thread_context (th
);
182 /* Add a thread to the thread list. */
183 static win32_thread_info
*
184 child_add_thread (DWORD pid
, DWORD tid
, HANDLE h
, void *tlb
)
186 win32_thread_info
*th
;
187 ptid_t ptid
= ptid_build (pid
, tid
, 0);
189 if ((th
= thread_rec (ptid
, FALSE
)))
192 th
= xcalloc (1, sizeof (*th
));
195 th
->thread_local_base
= (CORE_ADDR
) (uintptr_t) tlb
;
197 add_thread (ptid
, th
);
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 struct process_info
*proc
;
312 last_sig
= GDB_SIGNAL_0
;
314 current_process_handle
= proch
;
315 current_process_id
= pid
;
318 soft_interrupt_requested
= 0;
319 faked_breakpoint
= 0;
321 memset (¤t_event
, 0, sizeof (current_event
));
323 proc
= add_process (pid
, attached
);
324 proc
->tdesc
= win32_tdesc
;
325 child_init_thread_list ();
327 if (the_low_target
.initial_stuff
!= NULL
)
328 (*the_low_target
.initial_stuff
) ();
331 /* Resume all artificially suspended threads if we are continuing
334 continue_one_thread (struct inferior_list_entry
*this_thread
, void *id_ptr
)
336 struct thread_info
*thread
= (struct thread_info
*) this_thread
;
337 int thread_id
= * (int *) id_ptr
;
338 win32_thread_info
*th
= inferior_target_data (thread
);
340 if ((thread_id
== -1 || thread_id
== th
->tid
)
343 if (th
->context
.ContextFlags
)
345 win32_set_thread_context (th
);
346 th
->context
.ContextFlags
= 0;
349 if (ResumeThread (th
->h
) == (DWORD
) -1)
351 DWORD err
= GetLastError ();
352 OUTMSG (("warning: ResumeThread failed in continue_one_thread, "
353 "(error %d): %s\n", (int) err
, strwinerror (err
)));
362 child_continue (DWORD continue_status
, int thread_id
)
364 /* The inferior will only continue after the ContinueDebugEvent
366 find_inferior (&all_threads
, continue_one_thread
, &thread_id
);
367 faked_breakpoint
= 0;
369 if (!ContinueDebugEvent (current_event
.dwProcessId
,
370 current_event
.dwThreadId
,
377 /* Fetch register(s) from the current thread context. */
379 child_fetch_inferior_registers (struct regcache
*regcache
, int r
)
382 win32_thread_info
*th
= thread_rec (current_inferior_ptid (), TRUE
);
383 if (r
== -1 || r
> NUM_REGS
)
384 child_fetch_inferior_registers (regcache
, NUM_REGS
);
386 for (regno
= 0; regno
< r
; regno
++)
387 (*the_low_target
.fetch_inferior_register
) (regcache
, th
, regno
);
390 /* Store a new register value into the current thread context. We don't
391 change the program's context until later, when we resume it. */
393 child_store_inferior_registers (struct regcache
*regcache
, int r
)
396 win32_thread_info
*th
= thread_rec (current_inferior_ptid (), TRUE
);
397 if (r
== -1 || r
== 0 || r
> NUM_REGS
)
398 child_store_inferior_registers (regcache
, NUM_REGS
);
400 for (regno
= 0; regno
< r
; regno
++)
401 (*the_low_target
.store_inferior_register
) (regcache
, th
, regno
);
404 /* Map the Windows error number in ERROR to a locale-dependent error
405 message string and return a pointer to it. Typically, the values
406 for ERROR come from GetLastError.
408 The string pointed to shall not be modified by the application,
409 but may be overwritten by a subsequent call to strwinerror
411 The strwinerror function does not change the current setting
415 strwinerror (DWORD error
)
417 static char buf
[1024];
419 DWORD lasterr
= GetLastError ();
420 DWORD chars
= FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
421 | FORMAT_MESSAGE_ALLOCATE_BUFFER
,
424 0, /* Default language */
430 /* If there is an \r\n appended, zap it. */
432 && msgbuf
[chars
- 2] == '\r'
433 && msgbuf
[chars
- 1] == '\n')
439 if (chars
> ((COUNTOF (buf
)) - 1))
441 chars
= COUNTOF (buf
) - 1;
446 wcstombs (buf
, msgbuf
, chars
+ 1);
448 strncpy (buf
, msgbuf
, chars
+ 1);
453 sprintf (buf
, "unknown win32 error (%u)", (unsigned) error
);
455 SetLastError (lasterr
);
460 create_process (const char *program
, char *args
,
461 DWORD flags
, PROCESS_INFORMATION
*pi
)
466 wchar_t *p
, *wprogram
, *wargs
;
469 wprogram
= alloca ((strlen (program
) + 1) * sizeof (wchar_t));
470 mbstowcs (wprogram
, program
, strlen (program
) + 1);
472 for (p
= wprogram
; *p
; ++p
)
476 argslen
= strlen (args
);
477 wargs
= alloca ((argslen
+ 1) * sizeof (wchar_t));
478 mbstowcs (wargs
, args
, argslen
+ 1);
480 ret
= CreateProcessW (wprogram
, /* image name */
481 wargs
, /* command line */
482 NULL
, /* security, not supported */
483 NULL
, /* thread, not supported */
484 FALSE
, /* inherit handles, not supported */
485 flags
, /* start flags */
486 NULL
, /* environment, not supported */
487 NULL
, /* current directory, not supported */
488 NULL
, /* start info, not supported */
491 STARTUPINFOA si
= { sizeof (STARTUPINFOA
) };
493 ret
= CreateProcessA (program
, /* image name */
494 args
, /* command line */
497 TRUE
, /* inherit handles */
498 flags
, /* start flags */
499 NULL
, /* environment */
500 NULL
, /* current directory */
501 &si
, /* start info */
508 /* Start a new process.
509 PROGRAM is a path to the program to execute.
510 ARGS is a standard NULL-terminated array of arguments,
511 to be passed to the inferior as ``argv''.
512 Returns the new PID on success, -1 on failure. Registers the new
513 process with the process list. */
515 win32_create_inferior (char *program
, char **program_args
)
518 char real_path
[MAXPATHLEN
];
519 char *orig_path
, *new_path
, *path_ptr
;
526 PROCESS_INFORMATION pi
;
529 /* win32_wait needs to know we're not attaching. */
533 error ("No executable specified, specify executable to debug.\n");
535 flags
= DEBUG_PROCESS
| DEBUG_ONLY_THIS_PROCESS
;
539 path_ptr
= getenv ("PATH");
542 int size
= cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, NULL
, 0);
543 orig_path
= alloca (strlen (path_ptr
) + 1);
544 new_path
= alloca (size
);
545 strcpy (orig_path
, path_ptr
);
546 cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, new_path
, size
);
547 setenv ("PATH", new_path
, 1);
549 cygwin_conv_path (CCP_POSIX_TO_WIN_A
, program
, real_path
,
555 for (argc
= 1; program_args
[argc
]; argc
++)
556 argslen
+= strlen (program_args
[argc
]) + 1;
557 args
= alloca (argslen
);
559 for (argc
= 1; program_args
[argc
]; argc
++)
561 /* FIXME: Can we do better about quoting? How does Cygwin
564 strcat (args
, program_args
[argc
]);
566 OUTMSG2 (("Command line is \"%s\"\n", args
));
568 #ifdef CREATE_NEW_PROCESS_GROUP
569 flags
|= CREATE_NEW_PROCESS_GROUP
;
572 ret
= create_process (program
, args
, flags
, &pi
);
573 err
= GetLastError ();
574 if (!ret
&& err
== ERROR_FILE_NOT_FOUND
)
576 char *exename
= alloca (strlen (program
) + 5);
577 strcat (strcpy (exename
, program
), ".exe");
578 ret
= create_process (exename
, args
, flags
, &pi
);
579 err
= GetLastError ();
584 setenv ("PATH", orig_path
, 1);
589 error ("Error creating process \"%s%s\", (error %d): %s\n",
590 program
, args
, (int) err
, strwinerror (err
));
594 OUTMSG2 (("Process created: %s\n", (char *) args
));
598 /* On Windows CE this handle can't be closed. The OS reuses
599 it in the debug events, while the 9x/NT versions of Windows
600 probably use a DuplicateHandle'd one. */
601 CloseHandle (pi
.hThread
);
604 do_initial_child_stuff (pi
.hProcess
, pi
.dwProcessId
, 0);
606 return current_process_id
;
609 /* Attach to a running process.
610 PID is the process ID to attach to, specified by the user
611 or a higher layer. */
613 win32_attach (unsigned long pid
)
616 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
619 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
621 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
623 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
625 h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
628 if (DebugActiveProcess (pid
))
630 if (DebugSetProcessKillOnExit
!= NULL
)
631 DebugSetProcessKillOnExit (FALSE
);
633 /* win32_wait needs to know we're attaching. */
635 do_initial_child_stuff (h
, pid
, 1);
642 err
= GetLastError ();
643 error ("Attach to process failed (error %d): %s\n",
644 (int) err
, strwinerror (err
));
647 /* Handle OUTPUT_DEBUG_STRING_EVENT from child process. */
649 handle_output_debug_string (struct target_waitstatus
*ourstatus
)
651 #define READ_BUFFER_LEN 1024
653 char s
[READ_BUFFER_LEN
+ 1] = { 0 };
654 DWORD nbytes
= current_event
.u
.DebugString
.nDebugStringLength
;
659 if (nbytes
> READ_BUFFER_LEN
)
660 nbytes
= READ_BUFFER_LEN
;
662 addr
= (CORE_ADDR
) (size_t) current_event
.u
.DebugString
.lpDebugStringData
;
664 if (current_event
.u
.DebugString
.fUnicode
)
666 /* The event tells us how many bytes, not chars, even
668 WCHAR buffer
[(READ_BUFFER_LEN
+ 1) / sizeof (WCHAR
)] = { 0 };
669 if (read_inferior_memory (addr
, (unsigned char *) buffer
, nbytes
) != 0)
671 wcstombs (s
, buffer
, (nbytes
+ 1) / sizeof (WCHAR
));
675 if (read_inferior_memory (addr
, (unsigned char *) s
, nbytes
) != 0)
679 if (strncmp (s
, "cYg", 3) != 0)
689 #undef READ_BUFFER_LEN
693 win32_clear_inferiors (void)
695 if (current_process_handle
!= NULL
)
696 CloseHandle (current_process_handle
);
698 for_each_inferior (&all_threads
, delete_thread_info
);
702 /* Kill all inferiors. */
706 struct process_info
*process
;
708 if (current_process_handle
== NULL
)
711 TerminateProcess (current_process_handle
, 0);
714 if (!child_continue (DBG_CONTINUE
, -1))
716 if (!WaitForDebugEvent (¤t_event
, INFINITE
))
718 if (current_event
.dwDebugEventCode
== EXIT_PROCESS_DEBUG_EVENT
)
720 else if (current_event
.dwDebugEventCode
== OUTPUT_DEBUG_STRING_EVENT
)
722 struct target_waitstatus our_status
= { 0 };
723 handle_output_debug_string (&our_status
);
727 win32_clear_inferiors ();
729 process
= find_process_pid (pid
);
730 remove_process (process
);
734 /* Detach from inferior PID. */
736 win32_detach (int pid
)
738 struct process_info
*process
;
739 winapi_DebugActiveProcessStop DebugActiveProcessStop
= NULL
;
740 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
742 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
744 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
746 DebugActiveProcessStop
= GETPROCADDRESS (dll
, DebugActiveProcessStop
);
747 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
749 if (DebugSetProcessKillOnExit
== NULL
750 || DebugActiveProcessStop
== NULL
)
754 struct thread_resume resume
;
755 resume
.thread
= minus_one_ptid
;
756 resume
.kind
= resume_continue
;
758 win32_resume (&resume
, 1);
761 if (!DebugActiveProcessStop (current_process_id
))
764 DebugSetProcessKillOnExit (FALSE
);
765 process
= find_process_pid (pid
);
766 remove_process (process
);
768 win32_clear_inferiors ();
773 win32_mourn (struct process_info
*process
)
775 remove_process (process
);
778 /* Wait for inferiors to end. */
782 HANDLE h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
785 WaitForSingleObject (h
, INFINITE
);
790 /* Return 1 iff the thread with thread ID TID is alive. */
792 win32_thread_alive (ptid_t ptid
)
796 /* Our thread list is reliable; don't bother to poll target
798 if (find_inferior_id (&all_threads
, ptid
) != NULL
)
805 /* Resume the inferior process. RESUME_INFO describes how we want
808 win32_resume (struct thread_resume
*resume_info
, size_t n
)
813 win32_thread_info
*th
;
814 DWORD continue_status
= DBG_CONTINUE
;
817 /* This handles the very limited set of resume packets that GDB can
818 currently produce. */
820 if (n
== 1 && ptid_equal (resume_info
[0].thread
, minus_one_ptid
))
825 /* Yes, we're ignoring resume_info[0].thread. It'd be tricky to make
826 the Windows resume code do the right thing for thread switching. */
827 tid
= current_event
.dwThreadId
;
829 if (!ptid_equal (resume_info
[0].thread
, minus_one_ptid
))
831 sig
= resume_info
[0].sig
;
832 step
= resume_info
[0].kind
== resume_step
;
840 if (sig
!= GDB_SIGNAL_0
)
842 if (current_event
.dwDebugEventCode
!= EXCEPTION_DEBUG_EVENT
)
844 OUTMSG (("Cannot continue with signal %d here.\n", sig
));
846 else if (sig
== last_sig
)
847 continue_status
= DBG_EXCEPTION_NOT_HANDLED
;
849 OUTMSG (("Can only continue with recieved signal %d.\n", last_sig
));
852 last_sig
= GDB_SIGNAL_0
;
854 /* Get context for the currently selected thread. */
855 ptid
= debug_event_ptid (¤t_event
);
856 th
= thread_rec (ptid
, FALSE
);
859 if (th
->context
.ContextFlags
)
861 /* Move register values from the inferior into the thread
862 context structure. */
863 regcache_invalidate ();
867 if (the_low_target
.single_step
!= NULL
)
868 (*the_low_target
.single_step
) (th
);
870 error ("Single stepping is not supported "
871 "in this configuration.\n");
874 win32_set_thread_context (th
);
875 th
->context
.ContextFlags
= 0;
879 /* Allow continuing with the same signal that interrupted us.
880 Otherwise complain. */
882 child_continue (continue_status
, tid
);
886 win32_add_one_solib (const char *name
, CORE_ADDR load_addr
)
888 char buf
[MAX_PATH
+ 1];
889 char buf2
[MAX_PATH
+ 1];
892 WIN32_FIND_DATA w32_fd
;
893 WCHAR wname
[MAX_PATH
+ 1];
894 mbstowcs (wname
, name
, MAX_PATH
);
895 HANDLE h
= FindFirstFile (wname
, &w32_fd
);
897 WIN32_FIND_DATAA w32_fd
;
898 HANDLE h
= FindFirstFileA (name
, &w32_fd
);
901 if (h
== INVALID_HANDLE_VALUE
)
909 char cwd
[MAX_PATH
+ 1];
911 if (GetCurrentDirectoryA (MAX_PATH
+ 1, cwd
))
913 p
= strrchr (buf
, '\\');
916 SetCurrentDirectoryA (buf
);
917 GetFullPathNameA (w32_fd
.cFileName
, MAX_PATH
, buf
, &p
);
918 SetCurrentDirectoryA (cwd
);
925 if (strcasecmp (buf
, "ntdll.dll") == 0)
927 GetSystemDirectoryA (buf
, sizeof (buf
));
928 strcat (buf
, "\\ntdll.dll");
933 cygwin_conv_path (CCP_WIN_A_TO_POSIX
, buf
, buf2
, sizeof (buf2
));
938 loaded_dll (buf2
, load_addr
);
942 get_image_name (HANDLE h
, void *address
, int unicode
)
944 static char buf
[(2 * MAX_PATH
) + 1];
945 DWORD size
= unicode
? sizeof (WCHAR
) : sizeof (char);
951 /* Attempt to read the name of the dll that was detected.
952 This is documented to work only when actively debugging
953 a program. It will not work for attached processes. */
958 /* Windows CE reports the address of the image name,
959 instead of an address of a pointer into the image name. */
960 address_ptr
= address
;
962 /* See if we could read the address of a string, and that the
963 address isn't null. */
964 if (!ReadProcessMemory (h
, address
, &address_ptr
,
965 sizeof (address_ptr
), &done
)
966 || done
!= sizeof (address_ptr
)
971 /* Find the length of the string */
972 while (ReadProcessMemory (h
, address_ptr
+ len
++ * size
, &b
, size
, &done
)
973 && (b
[0] != 0 || b
[size
- 1] != 0) && done
== size
)
977 ReadProcessMemory (h
, address_ptr
, buf
, len
, &done
);
980 WCHAR
*unicode_address
= (WCHAR
*) alloca (len
* sizeof (WCHAR
));
981 ReadProcessMemory (h
, address_ptr
, unicode_address
, len
* sizeof (WCHAR
),
984 WideCharToMultiByte (CP_ACP
, 0, unicode_address
, len
, buf
, len
, 0, 0);
990 typedef BOOL (WINAPI
*winapi_EnumProcessModules
) (HANDLE
, HMODULE
*,
992 typedef BOOL (WINAPI
*winapi_GetModuleInformation
) (HANDLE
, HMODULE
,
993 LPMODULEINFO
, DWORD
);
994 typedef DWORD (WINAPI
*winapi_GetModuleFileNameExA
) (HANDLE
, HMODULE
,
997 static winapi_EnumProcessModules win32_EnumProcessModules
;
998 static winapi_GetModuleInformation win32_GetModuleInformation
;
999 static winapi_GetModuleFileNameExA win32_GetModuleFileNameExA
;
1004 static int psapi_loaded
= 0;
1005 static HMODULE dll
= NULL
;
1010 dll
= LoadLibrary (TEXT("psapi.dll"));
1013 win32_EnumProcessModules
=
1014 GETPROCADDRESS (dll
, EnumProcessModules
);
1015 win32_GetModuleInformation
=
1016 GETPROCADDRESS (dll
, GetModuleInformation
);
1017 win32_GetModuleFileNameExA
=
1018 GETPROCADDRESS (dll
, GetModuleFileNameExA
);
1021 return (win32_EnumProcessModules
!= NULL
1022 && win32_GetModuleInformation
!= NULL
1023 && win32_GetModuleFileNameExA
!= NULL
);
1027 psapi_get_dll_name (LPVOID BaseAddress
, char *dll_name_ret
)
1033 HMODULE
*DllHandle
= dh_buf
;
1041 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1046 if (!ok
|| !cbNeeded
)
1049 DllHandle
= (HMODULE
*) alloca (cbNeeded
);
1053 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1060 for (i
= 0; i
< ((size_t) cbNeeded
/ sizeof (HMODULE
)); i
++)
1062 if (!(*win32_GetModuleInformation
) (current_process_handle
,
1067 DWORD err
= GetLastError ();
1068 error ("Can't get module info: (error %d): %s\n",
1069 (int) err
, strwinerror (err
));
1072 if (mi
.lpBaseOfDll
== BaseAddress
)
1074 len
= (*win32_GetModuleFileNameExA
) (current_process_handle
,
1080 DWORD err
= GetLastError ();
1081 error ("Error getting dll name: (error %d): %s\n",
1082 (int) err
, strwinerror (err
));
1089 dll_name_ret
[0] = '\0';
1093 typedef HANDLE (WINAPI
*winapi_CreateToolhelp32Snapshot
) (DWORD
, DWORD
);
1094 typedef BOOL (WINAPI
*winapi_Module32First
) (HANDLE
, LPMODULEENTRY32
);
1095 typedef BOOL (WINAPI
*winapi_Module32Next
) (HANDLE
, LPMODULEENTRY32
);
1097 static winapi_CreateToolhelp32Snapshot win32_CreateToolhelp32Snapshot
;
1098 static winapi_Module32First win32_Module32First
;
1099 static winapi_Module32Next win32_Module32Next
;
1101 typedef BOOL (WINAPI
*winapi_CloseToolhelp32Snapshot
) (HANDLE
);
1102 static winapi_CloseToolhelp32Snapshot win32_CloseToolhelp32Snapshot
;
1106 load_toolhelp (void)
1108 static int toolhelp_loaded
= 0;
1109 static HMODULE dll
= NULL
;
1111 if (!toolhelp_loaded
)
1113 toolhelp_loaded
= 1;
1115 dll
= GetModuleHandle (_T("KERNEL32.DLL"));
1117 dll
= LoadLibrary (L
"TOOLHELP.DLL");
1122 win32_CreateToolhelp32Snapshot
=
1123 GETPROCADDRESS (dll
, CreateToolhelp32Snapshot
);
1124 win32_Module32First
= GETPROCADDRESS (dll
, Module32First
);
1125 win32_Module32Next
= GETPROCADDRESS (dll
, Module32Next
);
1127 win32_CloseToolhelp32Snapshot
=
1128 GETPROCADDRESS (dll
, CloseToolhelp32Snapshot
);
1132 return (win32_CreateToolhelp32Snapshot
!= NULL
1133 && win32_Module32First
!= NULL
1134 && win32_Module32Next
!= NULL
1136 && win32_CloseToolhelp32Snapshot
!= NULL
1142 toolhelp_get_dll_name (LPVOID BaseAddress
, char *dll_name_ret
)
1144 HANDLE snapshot_module
;
1145 MODULEENTRY32 modEntry
= { sizeof (MODULEENTRY32
) };
1148 if (!load_toolhelp ())
1151 snapshot_module
= win32_CreateToolhelp32Snapshot (TH32CS_SNAPMODULE
,
1152 current_event
.dwProcessId
);
1153 if (snapshot_module
== INVALID_HANDLE_VALUE
)
1156 /* Ignore the first module, which is the exe. */
1157 if (win32_Module32First (snapshot_module
, &modEntry
))
1158 while (win32_Module32Next (snapshot_module
, &modEntry
))
1159 if (modEntry
.modBaseAddr
== BaseAddress
)
1162 wcstombs (dll_name_ret
, modEntry
.szExePath
, MAX_PATH
+ 1);
1164 strcpy (dll_name_ret
, modEntry
.szExePath
);
1171 win32_CloseToolhelp32Snapshot (snapshot_module
);
1173 CloseHandle (snapshot_module
);
1179 handle_load_dll (void)
1181 LOAD_DLL_DEBUG_INFO
*event
= ¤t_event
.u
.LoadDll
;
1182 char dll_buf
[MAX_PATH
+ 1];
1183 char *dll_name
= NULL
;
1184 CORE_ADDR load_addr
;
1186 dll_buf
[0] = dll_buf
[sizeof (dll_buf
) - 1] = '\0';
1188 /* Windows does not report the image name of the dlls in the debug
1189 event on attaches. We resort to iterating over the list of
1190 loaded dlls looking for a match by image base. */
1191 if (!psapi_get_dll_name (event
->lpBaseOfDll
, dll_buf
))
1193 if (!server_waiting
)
1194 /* On some versions of Windows and Windows CE, we can't create
1195 toolhelp snapshots while the inferior is stopped in a
1196 LOAD_DLL_DEBUG_EVENT due to a dll load, but we can while
1197 Windows is reporting the already loaded dlls. */
1198 toolhelp_get_dll_name (event
->lpBaseOfDll
, dll_buf
);
1203 if (*dll_name
== '\0')
1204 dll_name
= get_image_name (current_process_handle
,
1205 event
->lpImageName
, event
->fUnicode
);
1209 /* The symbols in a dll are offset by 0x1000, which is the
1210 offset from 0 of the first byte in an image - because
1211 of the file header and the section alignment. */
1213 load_addr
= (CORE_ADDR
) (uintptr_t) event
->lpBaseOfDll
+ 0x1000;
1214 win32_add_one_solib (dll_name
, load_addr
);
1218 handle_unload_dll (void)
1220 CORE_ADDR load_addr
=
1221 (CORE_ADDR
) (uintptr_t) current_event
.u
.UnloadDll
.lpBaseOfDll
;
1222 load_addr
+= 0x1000;
1223 unloaded_dll (NULL
, load_addr
);
1227 handle_exception (struct target_waitstatus
*ourstatus
)
1229 DWORD code
= current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
;
1231 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
1235 case EXCEPTION_ACCESS_VIOLATION
:
1236 OUTMSG2 (("EXCEPTION_ACCESS_VIOLATION"));
1237 ourstatus
->value
.sig
= GDB_SIGNAL_SEGV
;
1239 case STATUS_STACK_OVERFLOW
:
1240 OUTMSG2 (("STATUS_STACK_OVERFLOW"));
1241 ourstatus
->value
.sig
= GDB_SIGNAL_SEGV
;
1243 case STATUS_FLOAT_DENORMAL_OPERAND
:
1244 OUTMSG2 (("STATUS_FLOAT_DENORMAL_OPERAND"));
1245 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1247 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
1248 OUTMSG2 (("EXCEPTION_ARRAY_BOUNDS_EXCEEDED"));
1249 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1251 case STATUS_FLOAT_INEXACT_RESULT
:
1252 OUTMSG2 (("STATUS_FLOAT_INEXACT_RESULT"));
1253 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1255 case STATUS_FLOAT_INVALID_OPERATION
:
1256 OUTMSG2 (("STATUS_FLOAT_INVALID_OPERATION"));
1257 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1259 case STATUS_FLOAT_OVERFLOW
:
1260 OUTMSG2 (("STATUS_FLOAT_OVERFLOW"));
1261 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1263 case STATUS_FLOAT_STACK_CHECK
:
1264 OUTMSG2 (("STATUS_FLOAT_STACK_CHECK"));
1265 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1267 case STATUS_FLOAT_UNDERFLOW
:
1268 OUTMSG2 (("STATUS_FLOAT_UNDERFLOW"));
1269 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1271 case STATUS_FLOAT_DIVIDE_BY_ZERO
:
1272 OUTMSG2 (("STATUS_FLOAT_DIVIDE_BY_ZERO"));
1273 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1275 case STATUS_INTEGER_DIVIDE_BY_ZERO
:
1276 OUTMSG2 (("STATUS_INTEGER_DIVIDE_BY_ZERO"));
1277 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1279 case STATUS_INTEGER_OVERFLOW
:
1280 OUTMSG2 (("STATUS_INTEGER_OVERFLOW"));
1281 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1283 case EXCEPTION_BREAKPOINT
:
1284 OUTMSG2 (("EXCEPTION_BREAKPOINT"));
1285 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1287 /* Remove the initial breakpoint. */
1288 check_breakpoints ((CORE_ADDR
) (long) current_event
1289 .u
.Exception
.ExceptionRecord
.ExceptionAddress
);
1293 OUTMSG2 (("DBG_CONTROL_C"));
1294 ourstatus
->value
.sig
= GDB_SIGNAL_INT
;
1296 case DBG_CONTROL_BREAK
:
1297 OUTMSG2 (("DBG_CONTROL_BREAK"));
1298 ourstatus
->value
.sig
= GDB_SIGNAL_INT
;
1300 case EXCEPTION_SINGLE_STEP
:
1301 OUTMSG2 (("EXCEPTION_SINGLE_STEP"));
1302 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1304 case EXCEPTION_ILLEGAL_INSTRUCTION
:
1305 OUTMSG2 (("EXCEPTION_ILLEGAL_INSTRUCTION"));
1306 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1308 case EXCEPTION_PRIV_INSTRUCTION
:
1309 OUTMSG2 (("EXCEPTION_PRIV_INSTRUCTION"));
1310 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1312 case EXCEPTION_NONCONTINUABLE_EXCEPTION
:
1313 OUTMSG2 (("EXCEPTION_NONCONTINUABLE_EXCEPTION"));
1314 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1317 if (current_event
.u
.Exception
.dwFirstChance
)
1319 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1322 OUTMSG2 (("gdbserver: unknown target exception 0x%08x at 0x%s",
1323 (unsigned) current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
,
1324 phex_nz ((uintptr_t) current_event
.u
.Exception
.ExceptionRecord
.
1325 ExceptionAddress
, sizeof (uintptr_t))));
1326 ourstatus
->value
.sig
= GDB_SIGNAL_UNKNOWN
;
1330 last_sig
= ourstatus
->value
.sig
;
1335 suspend_one_thread (struct inferior_list_entry
*entry
)
1337 struct thread_info
*thread
= (struct thread_info
*) entry
;
1338 win32_thread_info
*th
= inferior_target_data (thread
);
1342 if (SuspendThread (th
->h
) == (DWORD
) -1)
1344 DWORD err
= GetLastError ();
1345 OUTMSG (("warning: SuspendThread failed in suspend_one_thread, "
1346 "(error %d): %s\n", (int) err
, strwinerror (err
)));
1354 fake_breakpoint_event (void)
1356 OUTMSG2(("fake_breakpoint_event\n"));
1358 faked_breakpoint
= 1;
1360 memset (¤t_event
, 0, sizeof (current_event
));
1361 current_event
.dwThreadId
= main_thread_id
;
1362 current_event
.dwDebugEventCode
= EXCEPTION_DEBUG_EVENT
;
1363 current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
1364 = EXCEPTION_BREAKPOINT
;
1366 for_each_inferior (&all_threads
, suspend_one_thread
);
1371 auto_delete_breakpoint (CORE_ADDR stop_pc
)
1377 /* Get the next event from the child. */
1380 get_child_debug_event (struct target_waitstatus
*ourstatus
)
1384 last_sig
= GDB_SIGNAL_0
;
1385 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1387 /* Check if GDB sent us an interrupt request. */
1388 check_remote_input_interrupt_request ();
1390 if (soft_interrupt_requested
)
1392 soft_interrupt_requested
= 0;
1393 fake_breakpoint_event ();
1402 /* WinCE doesn't set an initial breakpoint automatically. To
1403 stop the inferior, we flush all currently pending debug
1404 events -- the thread list and the dll list are always
1405 reported immediatelly without delay, then, we suspend all
1406 threads and pretend we saw a trap at the current PC of the
1409 Contrary to desktop Windows, Windows CE *does* report the dll
1410 names on LOAD_DLL_DEBUG_EVENTs resulting from a
1411 DebugActiveProcess call. This limits the way we can detect
1412 if all the dlls have already been reported. If we get a real
1413 debug event before leaving attaching, the worst that will
1414 happen is the user will see a spurious breakpoint. */
1416 current_event
.dwDebugEventCode
= 0;
1417 if (!WaitForDebugEvent (¤t_event
, 0))
1419 OUTMSG2(("no attach events left\n"));
1420 fake_breakpoint_event ();
1424 OUTMSG2(("got attach event\n"));
1429 /* Keep the wait time low enough for confortable remote
1430 interruption, but high enough so gdbserver doesn't become a
1432 if (!WaitForDebugEvent (¤t_event
, 250))
1434 DWORD e
= GetLastError();
1436 if (e
== ERROR_PIPE_NOT_CONNECTED
)
1438 /* This will happen if the loader fails to succesfully
1439 load the application, e.g., if the main executable
1440 tries to pull in a non-existing export from a
1442 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1443 ourstatus
->value
.integer
= 1;
1453 switch (current_event
.dwDebugEventCode
)
1455 case CREATE_THREAD_DEBUG_EVENT
:
1456 OUTMSG2 (("gdbserver: kernel event CREATE_THREAD_DEBUG_EVENT "
1457 "for pid=%u tid=%x)\n",
1458 (unsigned) current_event
.dwProcessId
,
1459 (unsigned) current_event
.dwThreadId
));
1461 /* Record the existence of this thread. */
1462 child_add_thread (current_event
.dwProcessId
,
1463 current_event
.dwThreadId
,
1464 current_event
.u
.CreateThread
.hThread
,
1465 current_event
.u
.CreateThread
.lpThreadLocalBase
);
1468 case EXIT_THREAD_DEBUG_EVENT
:
1469 OUTMSG2 (("gdbserver: kernel event EXIT_THREAD_DEBUG_EVENT "
1470 "for pid=%u tid=%x\n",
1471 (unsigned) current_event
.dwProcessId
,
1472 (unsigned) current_event
.dwThreadId
));
1473 child_delete_thread (current_event
.dwProcessId
,
1474 current_event
.dwThreadId
);
1476 current_inferior
= (struct thread_info
*) all_threads
.head
;
1479 case CREATE_PROCESS_DEBUG_EVENT
:
1480 OUTMSG2 (("gdbserver: kernel event CREATE_PROCESS_DEBUG_EVENT "
1481 "for pid=%u tid=%x\n",
1482 (unsigned) current_event
.dwProcessId
,
1483 (unsigned) current_event
.dwThreadId
));
1484 CloseHandle (current_event
.u
.CreateProcessInfo
.hFile
);
1486 current_process_handle
= current_event
.u
.CreateProcessInfo
.hProcess
;
1487 main_thread_id
= current_event
.dwThreadId
;
1489 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
1490 ourstatus
->value
.execd_pathname
= "Main executable";
1492 /* Add the main thread. */
1493 child_add_thread (current_event
.dwProcessId
,
1495 current_event
.u
.CreateProcessInfo
.hThread
,
1496 current_event
.u
.CreateProcessInfo
.lpThreadLocalBase
);
1498 ourstatus
->value
.related_pid
= debug_event_ptid (¤t_event
);
1502 /* Windows CE doesn't set the initial breakpoint
1503 automatically like the desktop versions of Windows do.
1504 We add it explicitly here. It will be removed as soon as
1506 set_breakpoint_at ((CORE_ADDR
) (long) current_event
.u
1507 .CreateProcessInfo
.lpStartAddress
,
1508 auto_delete_breakpoint
);
1513 case EXIT_PROCESS_DEBUG_EVENT
:
1514 OUTMSG2 (("gdbserver: kernel event EXIT_PROCESS_DEBUG_EVENT "
1515 "for pid=%u tid=%x\n",
1516 (unsigned) current_event
.dwProcessId
,
1517 (unsigned) current_event
.dwThreadId
));
1518 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1519 ourstatus
->value
.integer
= current_event
.u
.ExitProcess
.dwExitCode
;
1520 child_continue (DBG_CONTINUE
, -1);
1521 CloseHandle (current_process_handle
);
1522 current_process_handle
= NULL
;
1525 case LOAD_DLL_DEBUG_EVENT
:
1526 OUTMSG2 (("gdbserver: kernel event LOAD_DLL_DEBUG_EVENT "
1527 "for pid=%u tid=%x\n",
1528 (unsigned) current_event
.dwProcessId
,
1529 (unsigned) current_event
.dwThreadId
));
1530 CloseHandle (current_event
.u
.LoadDll
.hFile
);
1533 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1534 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1537 case UNLOAD_DLL_DEBUG_EVENT
:
1538 OUTMSG2 (("gdbserver: kernel event UNLOAD_DLL_DEBUG_EVENT "
1539 "for pid=%u tid=%x\n",
1540 (unsigned) current_event
.dwProcessId
,
1541 (unsigned) current_event
.dwThreadId
));
1542 handle_unload_dll ();
1543 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1544 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1547 case EXCEPTION_DEBUG_EVENT
:
1548 OUTMSG2 (("gdbserver: kernel event EXCEPTION_DEBUG_EVENT "
1549 "for pid=%u tid=%x\n",
1550 (unsigned) current_event
.dwProcessId
,
1551 (unsigned) current_event
.dwThreadId
));
1552 handle_exception (ourstatus
);
1555 case OUTPUT_DEBUG_STRING_EVENT
:
1556 /* A message from the kernel (or Cygwin). */
1557 OUTMSG2 (("gdbserver: kernel event OUTPUT_DEBUG_STRING_EVENT "
1558 "for pid=%u tid=%x\n",
1559 (unsigned) current_event
.dwProcessId
,
1560 (unsigned) current_event
.dwThreadId
));
1561 handle_output_debug_string (ourstatus
);
1565 OUTMSG2 (("gdbserver: kernel event unknown "
1566 "for pid=%u tid=%x code=%x\n",
1567 (unsigned) current_event
.dwProcessId
,
1568 (unsigned) current_event
.dwThreadId
,
1569 (unsigned) current_event
.dwDebugEventCode
));
1573 ptid
= debug_event_ptid (¤t_event
);
1575 (struct thread_info
*) find_inferior_id (&all_threads
, ptid
);
1579 /* Wait for the inferior process to change state.
1580 STATUS will be filled in with a response code to send to GDB.
1581 Returns the signal which caused the process to stop. */
1583 win32_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
, int options
)
1585 struct regcache
*regcache
;
1589 if (!get_child_debug_event (ourstatus
))
1592 switch (ourstatus
->kind
)
1594 case TARGET_WAITKIND_EXITED
:
1595 OUTMSG2 (("Child exited with retcode = %x\n",
1596 ourstatus
->value
.integer
));
1597 win32_clear_inferiors ();
1598 return pid_to_ptid (current_event
.dwProcessId
);
1599 case TARGET_WAITKIND_STOPPED
:
1600 case TARGET_WAITKIND_LOADED
:
1601 OUTMSG2 (("Child Stopped with signal = %d \n",
1602 ourstatus
->value
.sig
));
1604 regcache
= get_thread_regcache (current_inferior
, 1);
1605 child_fetch_inferior_registers (regcache
, -1);
1607 if (ourstatus
->kind
== TARGET_WAITKIND_LOADED
1610 /* When gdb connects, we want to be stopped at the
1611 initial breakpoint, not in some dll load event. */
1612 child_continue (DBG_CONTINUE
, -1);
1616 /* We don't expose _LOADED events to gdbserver core. See
1617 the `dlls_changed' global. */
1618 if (ourstatus
->kind
== TARGET_WAITKIND_LOADED
)
1619 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
1621 return debug_event_ptid (¤t_event
);
1623 OUTMSG (("Ignoring unknown internal event, %d\n", ourstatus
->kind
));
1625 case TARGET_WAITKIND_SPURIOUS
:
1626 case TARGET_WAITKIND_EXECD
:
1627 /* do nothing, just continue */
1628 child_continue (DBG_CONTINUE
, -1);
1634 /* Fetch registers from the inferior process.
1635 If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO. */
1637 win32_fetch_inferior_registers (struct regcache
*regcache
, int regno
)
1639 child_fetch_inferior_registers (regcache
, regno
);
1642 /* Store registers to the inferior process.
1643 If REGNO is -1, store all registers; otherwise, store at least REGNO. */
1645 win32_store_inferior_registers (struct regcache
*regcache
, int regno
)
1647 child_store_inferior_registers (regcache
, regno
);
1650 /* Read memory from the inferior process. This should generally be
1651 called through read_inferior_memory, which handles breakpoint shadowing.
1652 Read LEN bytes at MEMADDR into a buffer at MYADDR. */
1654 win32_read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
1656 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 0, 0) != len
;
1659 /* Write memory to the inferior process. This should generally be
1660 called through write_inferior_memory, which handles breakpoint shadowing.
1661 Write LEN bytes from the buffer at MYADDR to MEMADDR.
1662 Returns 0 on success and errno on failure. */
1664 win32_write_inferior_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
,
1667 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 1, 0) != len
;
1670 /* Send an interrupt request to the inferior process. */
1672 win32_request_interrupt (void)
1674 winapi_DebugBreakProcess DebugBreakProcess
;
1675 winapi_GenerateConsoleCtrlEvent GenerateConsoleCtrlEvent
;
1678 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
1680 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
1683 GenerateConsoleCtrlEvent
= GETPROCADDRESS (dll
, GenerateConsoleCtrlEvent
);
1685 if (GenerateConsoleCtrlEvent
!= NULL
1686 && GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT
, current_process_id
))
1689 /* GenerateConsoleCtrlEvent can fail if process id being debugged is
1690 not a process group id.
1691 Fallback to XP/Vista 'DebugBreakProcess', which generates a
1692 breakpoint exception in the interior process. */
1694 DebugBreakProcess
= GETPROCADDRESS (dll
, DebugBreakProcess
);
1696 if (DebugBreakProcess
!= NULL
1697 && DebugBreakProcess (current_process_handle
))
1700 /* Last resort, suspend all threads manually. */
1701 soft_interrupt_requested
= 1;
1706 win32_error_to_fileio_error (DWORD err
)
1710 case ERROR_BAD_PATHNAME
:
1711 case ERROR_FILE_NOT_FOUND
:
1712 case ERROR_INVALID_NAME
:
1713 case ERROR_PATH_NOT_FOUND
:
1714 return FILEIO_ENOENT
;
1716 case ERROR_IO_DEVICE
:
1717 case ERROR_OPEN_FAILED
:
1719 case ERROR_INVALID_HANDLE
:
1720 return FILEIO_EBADF
;
1721 case ERROR_ACCESS_DENIED
:
1722 case ERROR_SHARING_VIOLATION
:
1723 return FILEIO_EACCES
;
1724 case ERROR_NOACCESS
:
1725 return FILEIO_EFAULT
;
1727 return FILEIO_EBUSY
;
1728 case ERROR_ALREADY_EXISTS
:
1729 case ERROR_FILE_EXISTS
:
1730 return FILEIO_EEXIST
;
1731 case ERROR_BAD_DEVICE
:
1732 return FILEIO_ENODEV
;
1733 case ERROR_DIRECTORY
:
1734 return FILEIO_ENOTDIR
;
1735 case ERROR_FILENAME_EXCED_RANGE
:
1736 case ERROR_INVALID_DATA
:
1737 case ERROR_INVALID_PARAMETER
:
1738 case ERROR_NEGATIVE_SEEK
:
1739 return FILEIO_EINVAL
;
1740 case ERROR_TOO_MANY_OPEN_FILES
:
1741 return FILEIO_EMFILE
;
1742 case ERROR_HANDLE_DISK_FULL
:
1743 case ERROR_DISK_FULL
:
1744 return FILEIO_ENOSPC
;
1745 case ERROR_WRITE_PROTECT
:
1746 return FILEIO_EROFS
;
1747 case ERROR_NOT_SUPPORTED
:
1748 return FILEIO_ENOSYS
;
1751 return FILEIO_EUNKNOWN
;
1755 wince_hostio_last_error (char *buf
)
1757 DWORD winerr
= GetLastError ();
1758 int fileio_err
= win32_error_to_fileio_error (winerr
);
1759 sprintf (buf
, "F-1,%x", fileio_err
);
1763 /* Write Windows OS Thread Information Block address. */
1766 win32_get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
)
1768 win32_thread_info
*th
;
1769 th
= thread_rec (ptid
, 0);
1773 *addr
= th
->thread_local_base
;
1777 static struct target_ops win32_target_ops
= {
1778 win32_create_inferior
,
1787 win32_fetch_inferior_registers
,
1788 win32_store_inferior_registers
,
1789 NULL
, /* prepare_to_access_memory */
1790 NULL
, /* done_accessing_memory */
1791 win32_read_inferior_memory
,
1792 win32_write_inferior_memory
,
1793 NULL
, /* lookup_symbols */
1794 win32_request_interrupt
,
1795 NULL
, /* read_auxv */
1798 win32_stopped_by_watchpoint
,
1799 win32_stopped_data_address
,
1800 NULL
, /* read_offsets */
1801 NULL
, /* get_tls_address */
1802 NULL
, /* qxfer_spu */
1804 wince_hostio_last_error
,
1806 hostio_last_error_from_errno
,
1808 NULL
, /* qxfer_osdata */
1809 NULL
, /* qxfer_siginfo */
1810 NULL
, /* supports_non_stop */
1812 NULL
, /* start_non_stop */
1813 NULL
, /* supports_multi_process */
1814 NULL
, /* handle_monitor_command */
1815 NULL
, /* core_of_thread */
1816 NULL
, /* read_loadmap */
1817 NULL
, /* process_qsupported */
1818 NULL
, /* supports_tracepoints */
1820 NULL
, /* write_pc */
1821 NULL
, /* thread_stopped */
1822 win32_get_tib_address
1825 /* Initialize the Win32 backend. */
1827 initialize_low (void)
1829 set_target_ops (&win32_target_ops
);
1830 if (the_low_target
.breakpoint
!= NULL
)
1831 set_breakpoint_data (the_low_target
.breakpoint
,
1832 the_low_target
.breakpoint_len
);
1833 the_low_target
.arch_setup ();