1 /* Low level interface to Windows debugging, for gdbserver.
2 Copyright (C) 2006-2012 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 #define NUM_REGS (the_low_target.num_regs)
92 typedef BOOL
WINAPI (*winapi_DebugActiveProcessStop
) (DWORD dwProcessId
);
93 typedef BOOL
WINAPI (*winapi_DebugSetProcessKillOnExit
) (BOOL KillOnExit
);
94 typedef BOOL
WINAPI (*winapi_DebugBreakProcess
) (HANDLE
);
95 typedef BOOL
WINAPI (*winapi_GenerateConsoleCtrlEvent
) (DWORD
, DWORD
);
97 static void win32_resume (struct thread_resume
*resume_info
, size_t n
);
99 /* Get the thread ID from the current selected inferior (the current
102 current_inferior_ptid (void)
104 return ((struct inferior_list_entry
*) current_inferior
)->id
;
107 /* The current debug event from WaitForDebugEvent. */
109 debug_event_ptid (DEBUG_EVENT
*event
)
111 return ptid_build (event
->dwProcessId
, event
->dwThreadId
, 0);
114 /* Get the thread context of the thread associated with TH. */
117 win32_get_thread_context (win32_thread_info
*th
)
119 memset (&th
->context
, 0, sizeof (CONTEXT
));
120 (*the_low_target
.get_thread_context
) (th
, ¤t_event
);
122 memcpy (&th
->base_context
, &th
->context
, sizeof (CONTEXT
));
126 /* Set the thread context of the thread associated with TH. */
129 win32_set_thread_context (win32_thread_info
*th
)
132 /* Calling SuspendThread on a thread that is running kernel code
133 will report that the suspending was successful, but in fact, that
134 will often not be true. In those cases, the context returned by
135 GetThreadContext will not be correct by the time the thread
136 stops, hence we can't set that context back into the thread when
137 resuming - it will most likelly crash the inferior.
138 Unfortunately, there is no way to know when the thread will
139 really stop. To work around it, we'll only write the context
140 back to the thread when either the user or GDB explicitly change
141 it between stopping and resuming. */
142 if (memcmp (&th
->context
, &th
->base_context
, sizeof (CONTEXT
)) != 0)
144 (*the_low_target
.set_thread_context
) (th
, ¤t_event
);
147 /* Find a thread record given a thread id. If GET_CONTEXT is set then
148 also retrieve the context for this thread. */
149 static win32_thread_info
*
150 thread_rec (ptid_t ptid
, int get_context
)
152 struct thread_info
*thread
;
153 win32_thread_info
*th
;
155 thread
= (struct thread_info
*) find_inferior_id (&all_threads
, ptid
);
159 th
= inferior_target_data (thread
);
160 if (get_context
&& th
->context
.ContextFlags
== 0)
164 if (SuspendThread (th
->h
) == (DWORD
) -1)
166 DWORD err
= GetLastError ();
167 OUTMSG (("warning: SuspendThread failed in thread_rec, "
168 "(error %d): %s\n", (int) err
, strwinerror (err
)));
174 win32_get_thread_context (th
);
180 /* Add a thread to the thread list. */
181 static win32_thread_info
*
182 child_add_thread (DWORD pid
, DWORD tid
, HANDLE h
, void *tlb
)
184 win32_thread_info
*th
;
185 ptid_t ptid
= ptid_build (pid
, tid
, 0);
187 if ((th
= thread_rec (ptid
, FALSE
)))
190 th
= xcalloc (1, sizeof (*th
));
193 th
->thread_local_base
= (CORE_ADDR
) (uintptr_t) tlb
;
195 add_thread (ptid
, th
);
196 set_inferior_regcache_data ((struct thread_info
*)
197 find_inferior_id (&all_threads
, ptid
),
198 new_register_cache ());
200 if (the_low_target
.thread_added
!= NULL
)
201 (*the_low_target
.thread_added
) (th
);
206 /* Delete a thread from the list of threads. */
208 delete_thread_info (struct inferior_list_entry
*thread
)
210 win32_thread_info
*th
= inferior_target_data ((struct thread_info
*) thread
);
212 remove_thread ((struct thread_info
*) thread
);
217 /* Delete a thread from the list of threads. */
219 child_delete_thread (DWORD pid
, DWORD tid
)
221 struct inferior_list_entry
*thread
;
224 /* If the last thread is exiting, just return. */
225 if (all_threads
.head
== all_threads
.tail
)
228 ptid
= ptid_build (pid
, tid
, 0);
229 thread
= find_inferior_id (&all_threads
, ptid
);
233 delete_thread_info (thread
);
236 /* These watchpoint related wrapper functions simply pass on the function call
237 if the low target has registered a corresponding function. */
240 win32_insert_point (char type
, CORE_ADDR addr
, int len
)
242 if (the_low_target
.insert_point
!= NULL
)
243 return the_low_target
.insert_point (type
, addr
, len
);
245 /* Unsupported (see target.h). */
250 win32_remove_point (char type
, CORE_ADDR addr
, int len
)
252 if (the_low_target
.remove_point
!= NULL
)
253 return the_low_target
.remove_point (type
, addr
, len
);
255 /* Unsupported (see target.h). */
260 win32_stopped_by_watchpoint (void)
262 if (the_low_target
.stopped_by_watchpoint
!= NULL
)
263 return the_low_target
.stopped_by_watchpoint ();
269 win32_stopped_data_address (void)
271 if (the_low_target
.stopped_data_address
!= NULL
)
272 return the_low_target
.stopped_data_address ();
278 /* Transfer memory from/to the debugged process. */
280 child_xfer_memory (CORE_ADDR memaddr
, char *our
, int len
,
281 int write
, struct target_ops
*target
)
284 uintptr_t addr
= (uintptr_t) memaddr
;
288 WriteProcessMemory (current_process_handle
, (LPVOID
) addr
,
289 (LPCVOID
) our
, len
, &done
);
290 FlushInstructionCache (current_process_handle
, (LPCVOID
) addr
, len
);
294 ReadProcessMemory (current_process_handle
, (LPCVOID
) addr
, (LPVOID
) our
,
300 /* Clear out any old thread list and reinitialize it to a pristine
303 child_init_thread_list (void)
305 for_each_inferior (&all_threads
, delete_thread_info
);
309 do_initial_child_stuff (HANDLE proch
, DWORD pid
, int attached
)
311 last_sig
= GDB_SIGNAL_0
;
313 current_process_handle
= proch
;
314 current_process_id
= pid
;
317 soft_interrupt_requested
= 0;
318 faked_breakpoint
= 0;
320 memset (¤t_event
, 0, sizeof (current_event
));
322 add_process (pid
, attached
);
323 child_init_thread_list ();
325 if (the_low_target
.initial_stuff
!= NULL
)
326 (*the_low_target
.initial_stuff
) ();
329 /* Resume all artificially suspended threads if we are continuing
332 continue_one_thread (struct inferior_list_entry
*this_thread
, void *id_ptr
)
334 struct thread_info
*thread
= (struct thread_info
*) this_thread
;
335 int thread_id
= * (int *) id_ptr
;
336 win32_thread_info
*th
= inferior_target_data (thread
);
338 if ((thread_id
== -1 || thread_id
== th
->tid
)
341 if (th
->context
.ContextFlags
)
343 win32_set_thread_context (th
);
344 th
->context
.ContextFlags
= 0;
347 if (ResumeThread (th
->h
) == (DWORD
) -1)
349 DWORD err
= GetLastError ();
350 OUTMSG (("warning: ResumeThread failed in continue_one_thread, "
351 "(error %d): %s\n", (int) err
, strwinerror (err
)));
360 child_continue (DWORD continue_status
, int thread_id
)
362 /* The inferior will only continue after the ContinueDebugEvent
364 find_inferior (&all_threads
, continue_one_thread
, &thread_id
);
365 faked_breakpoint
= 0;
367 if (!ContinueDebugEvent (current_event
.dwProcessId
,
368 current_event
.dwThreadId
,
375 /* Fetch register(s) from the current thread context. */
377 child_fetch_inferior_registers (struct regcache
*regcache
, int r
)
380 win32_thread_info
*th
= thread_rec (current_inferior_ptid (), TRUE
);
381 if (r
== -1 || r
> NUM_REGS
)
382 child_fetch_inferior_registers (regcache
, NUM_REGS
);
384 for (regno
= 0; regno
< r
; regno
++)
385 (*the_low_target
.fetch_inferior_register
) (regcache
, th
, regno
);
388 /* Store a new register value into the current thread context. We don't
389 change the program's context until later, when we resume it. */
391 child_store_inferior_registers (struct regcache
*regcache
, int r
)
394 win32_thread_info
*th
= thread_rec (current_inferior_ptid (), TRUE
);
395 if (r
== -1 || r
== 0 || r
> NUM_REGS
)
396 child_store_inferior_registers (regcache
, NUM_REGS
);
398 for (regno
= 0; regno
< r
; regno
++)
399 (*the_low_target
.store_inferior_register
) (regcache
, th
, regno
);
402 /* Map the Windows error number in ERROR to a locale-dependent error
403 message string and return a pointer to it. Typically, the values
404 for ERROR come from GetLastError.
406 The string pointed to shall not be modified by the application,
407 but may be overwritten by a subsequent call to strwinerror
409 The strwinerror function does not change the current setting
413 strwinerror (DWORD error
)
415 static char buf
[1024];
417 DWORD lasterr
= GetLastError ();
418 DWORD chars
= FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
419 | FORMAT_MESSAGE_ALLOCATE_BUFFER
,
422 0, /* Default language */
428 /* If there is an \r\n appended, zap it. */
430 && msgbuf
[chars
- 2] == '\r'
431 && msgbuf
[chars
- 1] == '\n')
437 if (chars
> ((COUNTOF (buf
)) - 1))
439 chars
= COUNTOF (buf
) - 1;
444 wcstombs (buf
, msgbuf
, chars
+ 1);
446 strncpy (buf
, msgbuf
, chars
+ 1);
451 sprintf (buf
, "unknown win32 error (%ld)", error
);
453 SetLastError (lasterr
);
458 create_process (const char *program
, char *args
,
459 DWORD flags
, PROCESS_INFORMATION
*pi
)
464 wchar_t *p
, *wprogram
, *wargs
;
467 wprogram
= alloca ((strlen (program
) + 1) * sizeof (wchar_t));
468 mbstowcs (wprogram
, program
, strlen (program
) + 1);
470 for (p
= wprogram
; *p
; ++p
)
474 argslen
= strlen (args
);
475 wargs
= alloca ((argslen
+ 1) * sizeof (wchar_t));
476 mbstowcs (wargs
, args
, argslen
+ 1);
478 ret
= CreateProcessW (wprogram
, /* image name */
479 wargs
, /* command line */
480 NULL
, /* security, not supported */
481 NULL
, /* thread, not supported */
482 FALSE
, /* inherit handles, not supported */
483 flags
, /* start flags */
484 NULL
, /* environment, not supported */
485 NULL
, /* current directory, not supported */
486 NULL
, /* start info, not supported */
489 STARTUPINFOA si
= { sizeof (STARTUPINFOA
) };
491 ret
= CreateProcessA (program
, /* image name */
492 args
, /* command line */
495 TRUE
, /* inherit handles */
496 flags
, /* start flags */
497 NULL
, /* environment */
498 NULL
, /* current directory */
499 &si
, /* start info */
506 /* Start a new process.
507 PROGRAM is a path to the program to execute.
508 ARGS is a standard NULL-terminated array of arguments,
509 to be passed to the inferior as ``argv''.
510 Returns the new PID on success, -1 on failure. Registers the new
511 process with the process list. */
513 win32_create_inferior (char *program
, char **program_args
)
516 char real_path
[MAXPATHLEN
];
517 char *orig_path
, *new_path
, *path_ptr
;
524 PROCESS_INFORMATION pi
;
527 /* win32_wait needs to know we're not attaching. */
531 error ("No executable specified, specify executable to debug.\n");
533 flags
= DEBUG_PROCESS
| DEBUG_ONLY_THIS_PROCESS
;
537 path_ptr
= getenv ("PATH");
540 int size
= cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, NULL
, 0);
541 orig_path
= alloca (strlen (path_ptr
) + 1);
542 new_path
= alloca (size
);
543 strcpy (orig_path
, path_ptr
);
544 cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, new_path
, size
);
545 setenv ("PATH", new_path
, 1);
547 cygwin_conv_path (CCP_POSIX_TO_WIN_A
, program
, real_path
,
553 for (argc
= 1; program_args
[argc
]; argc
++)
554 argslen
+= strlen (program_args
[argc
]) + 1;
555 args
= alloca (argslen
);
557 for (argc
= 1; program_args
[argc
]; argc
++)
559 /* FIXME: Can we do better about quoting? How does Cygwin
562 strcat (args
, program_args
[argc
]);
564 OUTMSG2 (("Command line is \"%s\"\n", args
));
566 #ifdef CREATE_NEW_PROCESS_GROUP
567 flags
|= CREATE_NEW_PROCESS_GROUP
;
570 ret
= create_process (program
, args
, flags
, &pi
);
571 err
= GetLastError ();
572 if (!ret
&& err
== ERROR_FILE_NOT_FOUND
)
574 char *exename
= alloca (strlen (program
) + 5);
575 strcat (strcpy (exename
, program
), ".exe");
576 ret
= create_process (exename
, args
, flags
, &pi
);
577 err
= GetLastError ();
582 setenv ("PATH", orig_path
, 1);
587 error ("Error creating process \"%s%s\", (error %d): %s\n",
588 program
, args
, (int) err
, strwinerror (err
));
592 OUTMSG2 (("Process created: %s\n", (char *) args
));
596 /* On Windows CE this handle can't be closed. The OS reuses
597 it in the debug events, while the 9x/NT versions of Windows
598 probably use a DuplicateHandle'd one. */
599 CloseHandle (pi
.hThread
);
602 do_initial_child_stuff (pi
.hProcess
, pi
.dwProcessId
, 0);
604 return current_process_id
;
607 /* Attach to a running process.
608 PID is the process ID to attach to, specified by the user
609 or a higher layer. */
611 win32_attach (unsigned long pid
)
614 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
617 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
619 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
621 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
623 h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
626 if (DebugActiveProcess (pid
))
628 if (DebugSetProcessKillOnExit
!= NULL
)
629 DebugSetProcessKillOnExit (FALSE
);
631 /* win32_wait needs to know we're attaching. */
633 do_initial_child_stuff (h
, pid
, 1);
640 err
= GetLastError ();
641 error ("Attach to process failed (error %d): %s\n",
642 (int) err
, strwinerror (err
));
645 /* Handle OUTPUT_DEBUG_STRING_EVENT from child process. */
647 handle_output_debug_string (struct target_waitstatus
*ourstatus
)
649 #define READ_BUFFER_LEN 1024
651 char s
[READ_BUFFER_LEN
+ 1] = { 0 };
652 DWORD nbytes
= current_event
.u
.DebugString
.nDebugStringLength
;
657 if (nbytes
> READ_BUFFER_LEN
)
658 nbytes
= READ_BUFFER_LEN
;
660 addr
= (CORE_ADDR
) (size_t) current_event
.u
.DebugString
.lpDebugStringData
;
662 if (current_event
.u
.DebugString
.fUnicode
)
664 /* The event tells us how many bytes, not chars, even
666 WCHAR buffer
[(READ_BUFFER_LEN
+ 1) / sizeof (WCHAR
)] = { 0 };
667 if (read_inferior_memory (addr
, (unsigned char *) buffer
, nbytes
) != 0)
669 wcstombs (s
, buffer
, (nbytes
+ 1) / sizeof (WCHAR
));
673 if (read_inferior_memory (addr
, (unsigned char *) s
, nbytes
) != 0)
677 if (strncmp (s
, "cYg", 3) != 0)
687 #undef READ_BUFFER_LEN
691 win32_clear_inferiors (void)
693 if (current_process_handle
!= NULL
)
694 CloseHandle (current_process_handle
);
696 for_each_inferior (&all_threads
, delete_thread_info
);
700 /* Kill all inferiors. */
704 struct process_info
*process
;
706 if (current_process_handle
== NULL
)
709 TerminateProcess (current_process_handle
, 0);
712 if (!child_continue (DBG_CONTINUE
, -1))
714 if (!WaitForDebugEvent (¤t_event
, INFINITE
))
716 if (current_event
.dwDebugEventCode
== EXIT_PROCESS_DEBUG_EVENT
)
718 else if (current_event
.dwDebugEventCode
== OUTPUT_DEBUG_STRING_EVENT
)
720 struct target_waitstatus our_status
= { 0 };
721 handle_output_debug_string (&our_status
);
725 win32_clear_inferiors ();
727 process
= find_process_pid (pid
);
728 remove_process (process
);
732 /* Detach from inferior PID. */
734 win32_detach (int pid
)
736 struct process_info
*process
;
737 winapi_DebugActiveProcessStop DebugActiveProcessStop
= NULL
;
738 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
740 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
742 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
744 DebugActiveProcessStop
= GETPROCADDRESS (dll
, DebugActiveProcessStop
);
745 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
747 if (DebugSetProcessKillOnExit
== NULL
748 || DebugActiveProcessStop
== NULL
)
752 struct thread_resume resume
;
753 resume
.thread
= minus_one_ptid
;
754 resume
.kind
= resume_continue
;
756 win32_resume (&resume
, 1);
759 if (!DebugActiveProcessStop (current_process_id
))
762 DebugSetProcessKillOnExit (FALSE
);
763 process
= find_process_pid (pid
);
764 remove_process (process
);
766 win32_clear_inferiors ();
771 win32_mourn (struct process_info
*process
)
773 remove_process (process
);
776 /* Wait for inferiors to end. */
780 HANDLE h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
783 WaitForSingleObject (h
, INFINITE
);
788 /* Return 1 iff the thread with thread ID TID is alive. */
790 win32_thread_alive (ptid_t ptid
)
794 /* Our thread list is reliable; don't bother to poll target
796 if (find_inferior_id (&all_threads
, ptid
) != NULL
)
803 /* Resume the inferior process. RESUME_INFO describes how we want
806 win32_resume (struct thread_resume
*resume_info
, size_t n
)
811 win32_thread_info
*th
;
812 DWORD continue_status
= DBG_CONTINUE
;
815 /* This handles the very limited set of resume packets that GDB can
816 currently produce. */
818 if (n
== 1 && ptid_equal (resume_info
[0].thread
, minus_one_ptid
))
823 /* Yes, we're ignoring resume_info[0].thread. It'd be tricky to make
824 the Windows resume code do the right thing for thread switching. */
825 tid
= current_event
.dwThreadId
;
827 if (!ptid_equal (resume_info
[0].thread
, minus_one_ptid
))
829 sig
= resume_info
[0].sig
;
830 step
= resume_info
[0].kind
== resume_step
;
838 if (sig
!= GDB_SIGNAL_0
)
840 if (current_event
.dwDebugEventCode
!= EXCEPTION_DEBUG_EVENT
)
842 OUTMSG (("Cannot continue with signal %d here.\n", sig
));
844 else if (sig
== last_sig
)
845 continue_status
= DBG_EXCEPTION_NOT_HANDLED
;
847 OUTMSG (("Can only continue with recieved signal %d.\n", last_sig
));
850 last_sig
= GDB_SIGNAL_0
;
852 /* Get context for the currently selected thread. */
853 ptid
= debug_event_ptid (¤t_event
);
854 th
= thread_rec (ptid
, FALSE
);
857 if (th
->context
.ContextFlags
)
859 /* Move register values from the inferior into the thread
860 context structure. */
861 regcache_invalidate ();
865 if (the_low_target
.single_step
!= NULL
)
866 (*the_low_target
.single_step
) (th
);
868 error ("Single stepping is not supported "
869 "in this configuration.\n");
872 win32_set_thread_context (th
);
873 th
->context
.ContextFlags
= 0;
877 /* Allow continuing with the same signal that interrupted us.
878 Otherwise complain. */
880 child_continue (continue_status
, tid
);
884 win32_add_one_solib (const char *name
, CORE_ADDR load_addr
)
886 char buf
[MAX_PATH
+ 1];
887 char buf2
[MAX_PATH
+ 1];
890 WIN32_FIND_DATA w32_fd
;
891 WCHAR wname
[MAX_PATH
+ 1];
892 mbstowcs (wname
, name
, MAX_PATH
);
893 HANDLE h
= FindFirstFile (wname
, &w32_fd
);
895 WIN32_FIND_DATAA w32_fd
;
896 HANDLE h
= FindFirstFileA (name
, &w32_fd
);
899 if (h
== INVALID_HANDLE_VALUE
)
907 char cwd
[MAX_PATH
+ 1];
909 if (GetCurrentDirectoryA (MAX_PATH
+ 1, cwd
))
911 p
= strrchr (buf
, '\\');
914 SetCurrentDirectoryA (buf
);
915 GetFullPathNameA (w32_fd
.cFileName
, MAX_PATH
, buf
, &p
);
916 SetCurrentDirectoryA (cwd
);
923 if (strcasecmp (buf
, "ntdll.dll") == 0)
925 GetSystemDirectoryA (buf
, sizeof (buf
));
926 strcat (buf
, "\\ntdll.dll");
931 cygwin_conv_path (CCP_WIN_A_TO_POSIX
, buf
, buf2
, sizeof (buf2
));
936 loaded_dll (buf2
, load_addr
);
940 get_image_name (HANDLE h
, void *address
, int unicode
)
942 static char buf
[(2 * MAX_PATH
) + 1];
943 DWORD size
= unicode
? sizeof (WCHAR
) : sizeof (char);
949 /* Attempt to read the name of the dll that was detected.
950 This is documented to work only when actively debugging
951 a program. It will not work for attached processes. */
956 /* Windows CE reports the address of the image name,
957 instead of an address of a pointer into the image name. */
958 address_ptr
= address
;
960 /* See if we could read the address of a string, and that the
961 address isn't null. */
962 if (!ReadProcessMemory (h
, address
, &address_ptr
,
963 sizeof (address_ptr
), &done
)
964 || done
!= sizeof (address_ptr
)
969 /* Find the length of the string */
970 while (ReadProcessMemory (h
, address_ptr
+ len
++ * size
, &b
, size
, &done
)
971 && (b
[0] != 0 || b
[size
- 1] != 0) && done
== size
)
975 ReadProcessMemory (h
, address_ptr
, buf
, len
, &done
);
978 WCHAR
*unicode_address
= (WCHAR
*) alloca (len
* sizeof (WCHAR
));
979 ReadProcessMemory (h
, address_ptr
, unicode_address
, len
* sizeof (WCHAR
),
982 WideCharToMultiByte (CP_ACP
, 0, unicode_address
, len
, buf
, len
, 0, 0);
988 typedef BOOL (WINAPI
*winapi_EnumProcessModules
) (HANDLE
, HMODULE
*,
990 typedef BOOL (WINAPI
*winapi_GetModuleInformation
) (HANDLE
, HMODULE
,
991 LPMODULEINFO
, DWORD
);
992 typedef DWORD (WINAPI
*winapi_GetModuleFileNameExA
) (HANDLE
, HMODULE
,
995 static winapi_EnumProcessModules win32_EnumProcessModules
;
996 static winapi_GetModuleInformation win32_GetModuleInformation
;
997 static winapi_GetModuleFileNameExA win32_GetModuleFileNameExA
;
1002 static int psapi_loaded
= 0;
1003 static HMODULE dll
= NULL
;
1008 dll
= LoadLibrary (TEXT("psapi.dll"));
1011 win32_EnumProcessModules
=
1012 GETPROCADDRESS (dll
, EnumProcessModules
);
1013 win32_GetModuleInformation
=
1014 GETPROCADDRESS (dll
, GetModuleInformation
);
1015 win32_GetModuleFileNameExA
=
1016 GETPROCADDRESS (dll
, GetModuleFileNameExA
);
1019 return (win32_EnumProcessModules
!= NULL
1020 && win32_GetModuleInformation
!= NULL
1021 && win32_GetModuleFileNameExA
!= NULL
);
1025 psapi_get_dll_name (LPVOID BaseAddress
, char *dll_name_ret
)
1031 HMODULE
*DllHandle
= dh_buf
;
1039 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1044 if (!ok
|| !cbNeeded
)
1047 DllHandle
= (HMODULE
*) alloca (cbNeeded
);
1051 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1058 for (i
= 0; i
< ((size_t) cbNeeded
/ sizeof (HMODULE
)); i
++)
1060 if (!(*win32_GetModuleInformation
) (current_process_handle
,
1065 DWORD err
= GetLastError ();
1066 error ("Can't get module info: (error %d): %s\n",
1067 (int) err
, strwinerror (err
));
1070 if (mi
.lpBaseOfDll
== BaseAddress
)
1072 len
= (*win32_GetModuleFileNameExA
) (current_process_handle
,
1078 DWORD err
= GetLastError ();
1079 error ("Error getting dll name: (error %d): %s\n",
1080 (int) err
, strwinerror (err
));
1087 dll_name_ret
[0] = '\0';
1091 typedef HANDLE (WINAPI
*winapi_CreateToolhelp32Snapshot
) (DWORD
, DWORD
);
1092 typedef BOOL (WINAPI
*winapi_Module32First
) (HANDLE
, LPMODULEENTRY32
);
1093 typedef BOOL (WINAPI
*winapi_Module32Next
) (HANDLE
, LPMODULEENTRY32
);
1095 static winapi_CreateToolhelp32Snapshot win32_CreateToolhelp32Snapshot
;
1096 static winapi_Module32First win32_Module32First
;
1097 static winapi_Module32Next win32_Module32Next
;
1099 typedef BOOL (WINAPI
*winapi_CloseToolhelp32Snapshot
) (HANDLE
);
1100 static winapi_CloseToolhelp32Snapshot win32_CloseToolhelp32Snapshot
;
1104 load_toolhelp (void)
1106 static int toolhelp_loaded
= 0;
1107 static HMODULE dll
= NULL
;
1109 if (!toolhelp_loaded
)
1111 toolhelp_loaded
= 1;
1113 dll
= GetModuleHandle (_T("KERNEL32.DLL"));
1115 dll
= LoadLibrary (L
"TOOLHELP.DLL");
1120 win32_CreateToolhelp32Snapshot
=
1121 GETPROCADDRESS (dll
, CreateToolhelp32Snapshot
);
1122 win32_Module32First
= GETPROCADDRESS (dll
, Module32First
);
1123 win32_Module32Next
= GETPROCADDRESS (dll
, Module32Next
);
1125 win32_CloseToolhelp32Snapshot
=
1126 GETPROCADDRESS (dll
, CloseToolhelp32Snapshot
);
1130 return (win32_CreateToolhelp32Snapshot
!= NULL
1131 && win32_Module32First
!= NULL
1132 && win32_Module32Next
!= NULL
1134 && win32_CloseToolhelp32Snapshot
!= NULL
1140 toolhelp_get_dll_name (LPVOID BaseAddress
, char *dll_name_ret
)
1142 HANDLE snapshot_module
;
1143 MODULEENTRY32 modEntry
= { sizeof (MODULEENTRY32
) };
1146 if (!load_toolhelp ())
1149 snapshot_module
= win32_CreateToolhelp32Snapshot (TH32CS_SNAPMODULE
,
1150 current_event
.dwProcessId
);
1151 if (snapshot_module
== INVALID_HANDLE_VALUE
)
1154 /* Ignore the first module, which is the exe. */
1155 if (win32_Module32First (snapshot_module
, &modEntry
))
1156 while (win32_Module32Next (snapshot_module
, &modEntry
))
1157 if (modEntry
.modBaseAddr
== BaseAddress
)
1160 wcstombs (dll_name_ret
, modEntry
.szExePath
, MAX_PATH
+ 1);
1162 strcpy (dll_name_ret
, modEntry
.szExePath
);
1169 win32_CloseToolhelp32Snapshot (snapshot_module
);
1171 CloseHandle (snapshot_module
);
1177 handle_load_dll (void)
1179 LOAD_DLL_DEBUG_INFO
*event
= ¤t_event
.u
.LoadDll
;
1180 char dll_buf
[MAX_PATH
+ 1];
1181 char *dll_name
= NULL
;
1182 CORE_ADDR load_addr
;
1184 dll_buf
[0] = dll_buf
[sizeof (dll_buf
) - 1] = '\0';
1186 /* Windows does not report the image name of the dlls in the debug
1187 event on attaches. We resort to iterating over the list of
1188 loaded dlls looking for a match by image base. */
1189 if (!psapi_get_dll_name (event
->lpBaseOfDll
, dll_buf
))
1191 if (!server_waiting
)
1192 /* On some versions of Windows and Windows CE, we can't create
1193 toolhelp snapshots while the inferior is stopped in a
1194 LOAD_DLL_DEBUG_EVENT due to a dll load, but we can while
1195 Windows is reporting the already loaded dlls. */
1196 toolhelp_get_dll_name (event
->lpBaseOfDll
, dll_buf
);
1201 if (*dll_name
== '\0')
1202 dll_name
= get_image_name (current_process_handle
,
1203 event
->lpImageName
, event
->fUnicode
);
1207 /* The symbols in a dll are offset by 0x1000, which is the
1208 offset from 0 of the first byte in an image - because
1209 of the file header and the section alignment. */
1211 load_addr
= (CORE_ADDR
) (uintptr_t) event
->lpBaseOfDll
+ 0x1000;
1212 win32_add_one_solib (dll_name
, load_addr
);
1216 handle_unload_dll (void)
1218 CORE_ADDR load_addr
=
1219 (CORE_ADDR
) (uintptr_t) current_event
.u
.UnloadDll
.lpBaseOfDll
;
1220 load_addr
+= 0x1000;
1221 unloaded_dll (NULL
, load_addr
);
1225 handle_exception (struct target_waitstatus
*ourstatus
)
1227 DWORD code
= current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
;
1229 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
1233 case EXCEPTION_ACCESS_VIOLATION
:
1234 OUTMSG2 (("EXCEPTION_ACCESS_VIOLATION"));
1235 ourstatus
->value
.sig
= GDB_SIGNAL_SEGV
;
1237 case STATUS_STACK_OVERFLOW
:
1238 OUTMSG2 (("STATUS_STACK_OVERFLOW"));
1239 ourstatus
->value
.sig
= GDB_SIGNAL_SEGV
;
1241 case STATUS_FLOAT_DENORMAL_OPERAND
:
1242 OUTMSG2 (("STATUS_FLOAT_DENORMAL_OPERAND"));
1243 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1245 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
1246 OUTMSG2 (("EXCEPTION_ARRAY_BOUNDS_EXCEEDED"));
1247 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1249 case STATUS_FLOAT_INEXACT_RESULT
:
1250 OUTMSG2 (("STATUS_FLOAT_INEXACT_RESULT"));
1251 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1253 case STATUS_FLOAT_INVALID_OPERATION
:
1254 OUTMSG2 (("STATUS_FLOAT_INVALID_OPERATION"));
1255 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1257 case STATUS_FLOAT_OVERFLOW
:
1258 OUTMSG2 (("STATUS_FLOAT_OVERFLOW"));
1259 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1261 case STATUS_FLOAT_STACK_CHECK
:
1262 OUTMSG2 (("STATUS_FLOAT_STACK_CHECK"));
1263 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1265 case STATUS_FLOAT_UNDERFLOW
:
1266 OUTMSG2 (("STATUS_FLOAT_UNDERFLOW"));
1267 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1269 case STATUS_FLOAT_DIVIDE_BY_ZERO
:
1270 OUTMSG2 (("STATUS_FLOAT_DIVIDE_BY_ZERO"));
1271 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1273 case STATUS_INTEGER_DIVIDE_BY_ZERO
:
1274 OUTMSG2 (("STATUS_INTEGER_DIVIDE_BY_ZERO"));
1275 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1277 case STATUS_INTEGER_OVERFLOW
:
1278 OUTMSG2 (("STATUS_INTEGER_OVERFLOW"));
1279 ourstatus
->value
.sig
= GDB_SIGNAL_FPE
;
1281 case EXCEPTION_BREAKPOINT
:
1282 OUTMSG2 (("EXCEPTION_BREAKPOINT"));
1283 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1285 /* Remove the initial breakpoint. */
1286 check_breakpoints ((CORE_ADDR
) (long) current_event
1287 .u
.Exception
.ExceptionRecord
.ExceptionAddress
);
1291 OUTMSG2 (("DBG_CONTROL_C"));
1292 ourstatus
->value
.sig
= GDB_SIGNAL_INT
;
1294 case DBG_CONTROL_BREAK
:
1295 OUTMSG2 (("DBG_CONTROL_BREAK"));
1296 ourstatus
->value
.sig
= GDB_SIGNAL_INT
;
1298 case EXCEPTION_SINGLE_STEP
:
1299 OUTMSG2 (("EXCEPTION_SINGLE_STEP"));
1300 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1302 case EXCEPTION_ILLEGAL_INSTRUCTION
:
1303 OUTMSG2 (("EXCEPTION_ILLEGAL_INSTRUCTION"));
1304 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1306 case EXCEPTION_PRIV_INSTRUCTION
:
1307 OUTMSG2 (("EXCEPTION_PRIV_INSTRUCTION"));
1308 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1310 case EXCEPTION_NONCONTINUABLE_EXCEPTION
:
1311 OUTMSG2 (("EXCEPTION_NONCONTINUABLE_EXCEPTION"));
1312 ourstatus
->value
.sig
= GDB_SIGNAL_ILL
;
1315 if (current_event
.u
.Exception
.dwFirstChance
)
1317 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1320 OUTMSG2 (("gdbserver: unknown target exception 0x%08lx at 0x%s",
1321 current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
,
1322 phex_nz ((uintptr_t) current_event
.u
.Exception
.ExceptionRecord
.
1323 ExceptionAddress
, sizeof (uintptr_t))));
1324 ourstatus
->value
.sig
= GDB_SIGNAL_UNKNOWN
;
1328 last_sig
= ourstatus
->value
.sig
;
1333 suspend_one_thread (struct inferior_list_entry
*entry
)
1335 struct thread_info
*thread
= (struct thread_info
*) entry
;
1336 win32_thread_info
*th
= inferior_target_data (thread
);
1340 if (SuspendThread (th
->h
) == (DWORD
) -1)
1342 DWORD err
= GetLastError ();
1343 OUTMSG (("warning: SuspendThread failed in suspend_one_thread, "
1344 "(error %d): %s\n", (int) err
, strwinerror (err
)));
1352 fake_breakpoint_event (void)
1354 OUTMSG2(("fake_breakpoint_event\n"));
1356 faked_breakpoint
= 1;
1358 memset (¤t_event
, 0, sizeof (current_event
));
1359 current_event
.dwThreadId
= main_thread_id
;
1360 current_event
.dwDebugEventCode
= EXCEPTION_DEBUG_EVENT
;
1361 current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
1362 = EXCEPTION_BREAKPOINT
;
1364 for_each_inferior (&all_threads
, suspend_one_thread
);
1369 auto_delete_breakpoint (CORE_ADDR stop_pc
)
1375 /* Get the next event from the child. */
1378 get_child_debug_event (struct target_waitstatus
*ourstatus
)
1382 last_sig
= GDB_SIGNAL_0
;
1383 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1385 /* Check if GDB sent us an interrupt request. */
1386 check_remote_input_interrupt_request ();
1388 if (soft_interrupt_requested
)
1390 soft_interrupt_requested
= 0;
1391 fake_breakpoint_event ();
1400 /* WinCE doesn't set an initial breakpoint automatically. To
1401 stop the inferior, we flush all currently pending debug
1402 events -- the thread list and the dll list are always
1403 reported immediatelly without delay, then, we suspend all
1404 threads and pretend we saw a trap at the current PC of the
1407 Contrary to desktop Windows, Windows CE *does* report the dll
1408 names on LOAD_DLL_DEBUG_EVENTs resulting from a
1409 DebugActiveProcess call. This limits the way we can detect
1410 if all the dlls have already been reported. If we get a real
1411 debug event before leaving attaching, the worst that will
1412 happen is the user will see a spurious breakpoint. */
1414 current_event
.dwDebugEventCode
= 0;
1415 if (!WaitForDebugEvent (¤t_event
, 0))
1417 OUTMSG2(("no attach events left\n"));
1418 fake_breakpoint_event ();
1422 OUTMSG2(("got attach event\n"));
1427 /* Keep the wait time low enough for confortable remote
1428 interruption, but high enough so gdbserver doesn't become a
1430 if (!WaitForDebugEvent (¤t_event
, 250))
1432 DWORD e
= GetLastError();
1434 if (e
== ERROR_PIPE_NOT_CONNECTED
)
1436 /* This will happen if the loader fails to succesfully
1437 load the application, e.g., if the main executable
1438 tries to pull in a non-existing export from a
1440 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1441 ourstatus
->value
.integer
= 1;
1451 switch (current_event
.dwDebugEventCode
)
1453 case CREATE_THREAD_DEBUG_EVENT
:
1454 OUTMSG2 (("gdbserver: kernel event CREATE_THREAD_DEBUG_EVENT "
1455 "for pid=%d tid=%x)\n",
1456 (unsigned) current_event
.dwProcessId
,
1457 (unsigned) current_event
.dwThreadId
));
1459 /* Record the existence of this thread. */
1460 child_add_thread (current_event
.dwProcessId
,
1461 current_event
.dwThreadId
,
1462 current_event
.u
.CreateThread
.hThread
,
1463 current_event
.u
.CreateThread
.lpThreadLocalBase
);
1466 case EXIT_THREAD_DEBUG_EVENT
:
1467 OUTMSG2 (("gdbserver: kernel event EXIT_THREAD_DEBUG_EVENT "
1468 "for pid=%d tid=%x\n",
1469 (unsigned) current_event
.dwProcessId
,
1470 (unsigned) current_event
.dwThreadId
));
1471 child_delete_thread (current_event
.dwProcessId
,
1472 current_event
.dwThreadId
);
1474 current_inferior
= (struct thread_info
*) all_threads
.head
;
1477 case CREATE_PROCESS_DEBUG_EVENT
:
1478 OUTMSG2 (("gdbserver: kernel event CREATE_PROCESS_DEBUG_EVENT "
1479 "for pid=%d tid=%x\n",
1480 (unsigned) current_event
.dwProcessId
,
1481 (unsigned) current_event
.dwThreadId
));
1482 CloseHandle (current_event
.u
.CreateProcessInfo
.hFile
);
1484 current_process_handle
= current_event
.u
.CreateProcessInfo
.hProcess
;
1485 main_thread_id
= current_event
.dwThreadId
;
1487 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
1488 ourstatus
->value
.execd_pathname
= "Main executable";
1490 /* Add the main thread. */
1491 child_add_thread (current_event
.dwProcessId
,
1493 current_event
.u
.CreateProcessInfo
.hThread
,
1494 current_event
.u
.CreateProcessInfo
.lpThreadLocalBase
);
1496 ourstatus
->value
.related_pid
= debug_event_ptid (¤t_event
);
1500 /* Windows CE doesn't set the initial breakpoint
1501 automatically like the desktop versions of Windows do.
1502 We add it explicitly here. It will be removed as soon as
1504 set_breakpoint_at ((CORE_ADDR
) (long) current_event
.u
1505 .CreateProcessInfo
.lpStartAddress
,
1506 auto_delete_breakpoint
);
1511 case EXIT_PROCESS_DEBUG_EVENT
:
1512 OUTMSG2 (("gdbserver: kernel event EXIT_PROCESS_DEBUG_EVENT "
1513 "for pid=%d tid=%x\n",
1514 (unsigned) current_event
.dwProcessId
,
1515 (unsigned) current_event
.dwThreadId
));
1516 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1517 ourstatus
->value
.integer
= current_event
.u
.ExitProcess
.dwExitCode
;
1518 child_continue (DBG_CONTINUE
, -1);
1519 CloseHandle (current_process_handle
);
1520 current_process_handle
= NULL
;
1523 case LOAD_DLL_DEBUG_EVENT
:
1524 OUTMSG2 (("gdbserver: kernel event LOAD_DLL_DEBUG_EVENT "
1525 "for pid=%d tid=%x\n",
1526 (unsigned) current_event
.dwProcessId
,
1527 (unsigned) current_event
.dwThreadId
));
1528 CloseHandle (current_event
.u
.LoadDll
.hFile
);
1531 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1532 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1535 case UNLOAD_DLL_DEBUG_EVENT
:
1536 OUTMSG2 (("gdbserver: kernel event UNLOAD_DLL_DEBUG_EVENT "
1537 "for pid=%d tid=%x\n",
1538 (unsigned) current_event
.dwProcessId
,
1539 (unsigned) current_event
.dwThreadId
));
1540 handle_unload_dll ();
1541 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1542 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
1545 case EXCEPTION_DEBUG_EVENT
:
1546 OUTMSG2 (("gdbserver: kernel event EXCEPTION_DEBUG_EVENT "
1547 "for pid=%d tid=%x\n",
1548 (unsigned) current_event
.dwProcessId
,
1549 (unsigned) current_event
.dwThreadId
));
1550 handle_exception (ourstatus
);
1553 case OUTPUT_DEBUG_STRING_EVENT
:
1554 /* A message from the kernel (or Cygwin). */
1555 OUTMSG2 (("gdbserver: kernel event OUTPUT_DEBUG_STRING_EVENT "
1556 "for pid=%d tid=%x\n",
1557 (unsigned) current_event
.dwProcessId
,
1558 (unsigned) current_event
.dwThreadId
));
1559 handle_output_debug_string (ourstatus
);
1563 OUTMSG2 (("gdbserver: kernel event unknown "
1564 "for pid=%d tid=%x code=%ld\n",
1565 (unsigned) current_event
.dwProcessId
,
1566 (unsigned) current_event
.dwThreadId
,
1567 current_event
.dwDebugEventCode
));
1571 ptid
= debug_event_ptid (¤t_event
);
1573 (struct thread_info
*) find_inferior_id (&all_threads
, ptid
);
1577 /* Wait for the inferior process to change state.
1578 STATUS will be filled in with a response code to send to GDB.
1579 Returns the signal which caused the process to stop. */
1581 win32_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
, int options
)
1583 struct regcache
*regcache
;
1587 if (!get_child_debug_event (ourstatus
))
1590 switch (ourstatus
->kind
)
1592 case TARGET_WAITKIND_EXITED
:
1593 OUTMSG2 (("Child exited with retcode = %x\n",
1594 ourstatus
->value
.integer
));
1595 win32_clear_inferiors ();
1596 return pid_to_ptid (current_event
.dwProcessId
);
1597 case TARGET_WAITKIND_STOPPED
:
1598 case TARGET_WAITKIND_LOADED
:
1599 OUTMSG2 (("Child Stopped with signal = %d \n",
1600 ourstatus
->value
.sig
));
1602 regcache
= get_thread_regcache (current_inferior
, 1);
1603 child_fetch_inferior_registers (regcache
, -1);
1605 if (ourstatus
->kind
== TARGET_WAITKIND_LOADED
1608 /* When gdb connects, we want to be stopped at the
1609 initial breakpoint, not in some dll load event. */
1610 child_continue (DBG_CONTINUE
, -1);
1614 /* We don't expose _LOADED events to gdbserver core. See
1615 the `dlls_changed' global. */
1616 if (ourstatus
->kind
== TARGET_WAITKIND_LOADED
)
1617 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
1619 return debug_event_ptid (¤t_event
);
1621 OUTMSG (("Ignoring unknown internal event, %d\n", ourstatus
->kind
));
1623 case TARGET_WAITKIND_SPURIOUS
:
1624 case TARGET_WAITKIND_EXECD
:
1625 /* do nothing, just continue */
1626 child_continue (DBG_CONTINUE
, -1);
1632 /* Fetch registers from the inferior process.
1633 If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO. */
1635 win32_fetch_inferior_registers (struct regcache
*regcache
, int regno
)
1637 child_fetch_inferior_registers (regcache
, regno
);
1640 /* Store registers to the inferior process.
1641 If REGNO is -1, store all registers; otherwise, store at least REGNO. */
1643 win32_store_inferior_registers (struct regcache
*regcache
, int regno
)
1645 child_store_inferior_registers (regcache
, regno
);
1648 /* Read memory from the inferior process. This should generally be
1649 called through read_inferior_memory, which handles breakpoint shadowing.
1650 Read LEN bytes at MEMADDR into a buffer at MYADDR. */
1652 win32_read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
1654 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 0, 0) != len
;
1657 /* Write memory to the inferior process. This should generally be
1658 called through write_inferior_memory, which handles breakpoint shadowing.
1659 Write LEN bytes from the buffer at MYADDR to MEMADDR.
1660 Returns 0 on success and errno on failure. */
1662 win32_write_inferior_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
,
1665 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 1, 0) != len
;
1668 /* Send an interrupt request to the inferior process. */
1670 win32_request_interrupt (void)
1672 winapi_DebugBreakProcess DebugBreakProcess
;
1673 winapi_GenerateConsoleCtrlEvent GenerateConsoleCtrlEvent
;
1676 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
1678 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
1681 GenerateConsoleCtrlEvent
= GETPROCADDRESS (dll
, GenerateConsoleCtrlEvent
);
1683 if (GenerateConsoleCtrlEvent
!= NULL
1684 && GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT
, current_process_id
))
1687 /* GenerateConsoleCtrlEvent can fail if process id being debugged is
1688 not a process group id.
1689 Fallback to XP/Vista 'DebugBreakProcess', which generates a
1690 breakpoint exception in the interior process. */
1692 DebugBreakProcess
= GETPROCADDRESS (dll
, DebugBreakProcess
);
1694 if (DebugBreakProcess
!= NULL
1695 && DebugBreakProcess (current_process_handle
))
1698 /* Last resort, suspend all threads manually. */
1699 soft_interrupt_requested
= 1;
1704 win32_error_to_fileio_error (DWORD err
)
1708 case ERROR_BAD_PATHNAME
:
1709 case ERROR_FILE_NOT_FOUND
:
1710 case ERROR_INVALID_NAME
:
1711 case ERROR_PATH_NOT_FOUND
:
1712 return FILEIO_ENOENT
;
1714 case ERROR_IO_DEVICE
:
1715 case ERROR_OPEN_FAILED
:
1717 case ERROR_INVALID_HANDLE
:
1718 return FILEIO_EBADF
;
1719 case ERROR_ACCESS_DENIED
:
1720 case ERROR_SHARING_VIOLATION
:
1721 return FILEIO_EACCES
;
1722 case ERROR_NOACCESS
:
1723 return FILEIO_EFAULT
;
1725 return FILEIO_EBUSY
;
1726 case ERROR_ALREADY_EXISTS
:
1727 case ERROR_FILE_EXISTS
:
1728 return FILEIO_EEXIST
;
1729 case ERROR_BAD_DEVICE
:
1730 return FILEIO_ENODEV
;
1731 case ERROR_DIRECTORY
:
1732 return FILEIO_ENOTDIR
;
1733 case ERROR_FILENAME_EXCED_RANGE
:
1734 case ERROR_INVALID_DATA
:
1735 case ERROR_INVALID_PARAMETER
:
1736 case ERROR_NEGATIVE_SEEK
:
1737 return FILEIO_EINVAL
;
1738 case ERROR_TOO_MANY_OPEN_FILES
:
1739 return FILEIO_EMFILE
;
1740 case ERROR_HANDLE_DISK_FULL
:
1741 case ERROR_DISK_FULL
:
1742 return FILEIO_ENOSPC
;
1743 case ERROR_WRITE_PROTECT
:
1744 return FILEIO_EROFS
;
1745 case ERROR_NOT_SUPPORTED
:
1746 return FILEIO_ENOSYS
;
1749 return FILEIO_EUNKNOWN
;
1753 wince_hostio_last_error (char *buf
)
1755 DWORD winerr
= GetLastError ();
1756 int fileio_err
= win32_error_to_fileio_error (winerr
);
1757 sprintf (buf
, "F-1,%x", fileio_err
);
1761 /* Write Windows OS Thread Information Block address. */
1764 win32_get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
)
1766 win32_thread_info
*th
;
1767 th
= thread_rec (ptid
, 0);
1771 *addr
= th
->thread_local_base
;
1775 static struct target_ops win32_target_ops
= {
1776 win32_create_inferior
,
1785 win32_fetch_inferior_registers
,
1786 win32_store_inferior_registers
,
1787 NULL
, /* prepare_to_access_memory */
1788 NULL
, /* done_accessing_memory */
1789 win32_read_inferior_memory
,
1790 win32_write_inferior_memory
,
1791 NULL
, /* lookup_symbols */
1792 win32_request_interrupt
,
1793 NULL
, /* read_auxv */
1796 win32_stopped_by_watchpoint
,
1797 win32_stopped_data_address
,
1798 NULL
, /* read_offsets */
1799 NULL
, /* get_tls_address */
1800 NULL
, /* qxfer_spu */
1802 wince_hostio_last_error
,
1804 hostio_last_error_from_errno
,
1806 NULL
, /* qxfer_osdata */
1807 NULL
, /* qxfer_siginfo */
1808 NULL
, /* supports_non_stop */
1810 NULL
, /* start_non_stop */
1811 NULL
, /* supports_multi_process */
1812 NULL
, /* handle_monitor_command */
1813 NULL
, /* core_of_thread */
1814 NULL
, /* read_loadmap */
1815 NULL
, /* process_qsupported */
1816 NULL
, /* supports_tracepoints */
1818 NULL
, /* write_pc */
1819 NULL
, /* thread_stopped */
1820 win32_get_tib_address
1823 /* Initialize the Win32 backend. */
1825 initialize_low (void)
1827 set_target_ops (&win32_target_ops
);
1828 if (the_low_target
.breakpoint
!= NULL
)
1829 set_breakpoint_data (the_low_target
.breakpoint
,
1830 the_low_target
.breakpoint_len
);
1831 the_low_target
.arch_setup ();