1 /* Low level interface to Windows debugging, for gdbserver.
2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
5 Contributed by Leo Zayas. Based on "win32-nat.c" from GDB.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "gdb/signals.h"
25 #include "gdb/fileio.h"
26 #include "mem-break.h"
27 #include "win32-low.h"
34 #include <sys/param.h>
38 #include <sys/cygwin.h>
41 #define OUTMSG(X) do { printf X; fflush (stderr); } while (0)
54 #define _T(x) TEXT (x)
58 #define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0]))
62 # define GETPROCADDRESS(DLL, PROC) \
63 ((winapi_ ## PROC) GetProcAddress (DLL, TEXT (#PROC)))
65 # define GETPROCADDRESS(DLL, PROC) \
66 ((winapi_ ## PROC) GetProcAddress (DLL, #PROC))
69 int using_threads
= 1;
72 static int attaching
= 0;
73 static HANDLE current_process_handle
= NULL
;
74 static DWORD current_process_id
= 0;
75 static DWORD main_thread_id
= 0;
76 static enum target_signal last_sig
= TARGET_SIGNAL_0
;
78 /* The current debug event from WaitForDebugEvent. */
79 static DEBUG_EVENT current_event
;
81 /* Non zero if an interrupt request is to be satisfied by suspending
83 static int soft_interrupt_requested
= 0;
85 /* Non zero if the inferior is stopped in a simulated breakpoint done
86 by suspending all the threads. */
87 static int faked_breakpoint
= 0;
89 #define NUM_REGS (the_low_target.num_regs)
91 typedef BOOL
WINAPI (*winapi_DebugActiveProcessStop
) (DWORD dwProcessId
);
92 typedef BOOL
WINAPI (*winapi_DebugSetProcessKillOnExit
) (BOOL KillOnExit
);
93 typedef BOOL
WINAPI (*winapi_DebugBreakProcess
) (HANDLE
);
94 typedef BOOL
WINAPI (*winapi_GenerateConsoleCtrlEvent
) (DWORD
, DWORD
);
96 static void win32_resume (struct thread_resume
*resume_info
, size_t n
);
98 /* Get the thread ID from the current selected inferior (the current
101 current_inferior_ptid (void)
103 return ((struct inferior_list_entry
*) current_inferior
)->id
;
106 /* The current debug event from WaitForDebugEvent. */
108 debug_event_ptid (DEBUG_EVENT
*event
)
110 return ptid_build (event
->dwProcessId
, event
->dwThreadId
, 0);
113 /* Get the thread context of the thread associated with TH. */
116 win32_get_thread_context (win32_thread_info
*th
)
118 memset (&th
->context
, 0, sizeof (CONTEXT
));
119 (*the_low_target
.get_thread_context
) (th
, ¤t_event
);
121 memcpy (&th
->base_context
, &th
->context
, sizeof (CONTEXT
));
125 /* Set the thread context of the thread associated with TH. */
128 win32_set_thread_context (win32_thread_info
*th
)
131 /* Calling SuspendThread on a thread that is running kernel code
132 will report that the suspending was successful, but in fact, that
133 will often not be true. In those cases, the context returned by
134 GetThreadContext will not be correct by the time the thread
135 stops, hence we can't set that context back into the thread when
136 resuming - it will most likelly crash the inferior.
137 Unfortunately, there is no way to know when the thread will
138 really stop. To work around it, we'll only write the context
139 back to the thread when either the user or GDB explicitly change
140 it between stopping and resuming. */
141 if (memcmp (&th
->context
, &th
->base_context
, sizeof (CONTEXT
)) != 0)
143 (*the_low_target
.set_thread_context
) (th
, ¤t_event
);
146 /* Find a thread record given a thread id. If GET_CONTEXT is set then
147 also retrieve the context for this thread. */
148 static win32_thread_info
*
149 thread_rec (ptid_t ptid
, int get_context
)
151 struct thread_info
*thread
;
152 win32_thread_info
*th
;
154 thread
= (struct thread_info
*) find_inferior_id (&all_threads
, ptid
);
158 th
= inferior_target_data (thread
);
159 if (get_context
&& th
->context
.ContextFlags
== 0)
163 if (SuspendThread (th
->h
) == (DWORD
) -1)
165 DWORD err
= GetLastError ();
166 OUTMSG (("warning: SuspendThread failed in thread_rec, "
167 "(error %d): %s\n", (int) err
, strwinerror (err
)));
173 win32_get_thread_context (th
);
179 /* Add a thread to the thread list. */
180 static win32_thread_info
*
181 child_add_thread (DWORD pid
, DWORD tid
, HANDLE h
, void *tlb
)
183 win32_thread_info
*th
;
184 ptid_t ptid
= ptid_build (pid
, tid
, 0);
186 if ((th
= thread_rec (ptid
, FALSE
)))
189 th
= xcalloc (1, sizeof (*th
));
192 th
->thread_local_base
= (CORE_ADDR
) (uintptr_t) tlb
;
194 add_thread (ptid
, th
);
195 set_inferior_regcache_data ((struct thread_info
*)
196 find_inferior_id (&all_threads
, ptid
),
197 new_register_cache ());
199 if (the_low_target
.thread_added
!= NULL
)
200 (*the_low_target
.thread_added
) (th
);
205 /* Delete a thread from the list of threads. */
207 delete_thread_info (struct inferior_list_entry
*thread
)
209 win32_thread_info
*th
= inferior_target_data ((struct thread_info
*) thread
);
211 remove_thread ((struct thread_info
*) thread
);
216 /* Delete a thread from the list of threads. */
218 child_delete_thread (DWORD pid
, DWORD tid
)
220 struct inferior_list_entry
*thread
;
223 /* If the last thread is exiting, just return. */
224 if (all_threads
.head
== all_threads
.tail
)
227 ptid
= ptid_build (pid
, tid
, 0);
228 thread
= find_inferior_id (&all_threads
, ptid
);
232 delete_thread_info (thread
);
235 /* These watchpoint related wrapper functions simply pass on the function call
236 if the low target has registered a corresponding function. */
239 win32_insert_point (char type
, CORE_ADDR addr
, int len
)
241 if (the_low_target
.insert_point
!= NULL
)
242 return the_low_target
.insert_point (type
, addr
, len
);
244 /* Unsupported (see target.h). */
249 win32_remove_point (char type
, CORE_ADDR addr
, int len
)
251 if (the_low_target
.remove_point
!= NULL
)
252 return the_low_target
.remove_point (type
, addr
, len
);
254 /* Unsupported (see target.h). */
259 win32_stopped_by_watchpoint (void)
261 if (the_low_target
.stopped_by_watchpoint
!= NULL
)
262 return the_low_target
.stopped_by_watchpoint ();
268 win32_stopped_data_address (void)
270 if (the_low_target
.stopped_data_address
!= NULL
)
271 return the_low_target
.stopped_data_address ();
277 /* Transfer memory from/to the debugged process. */
279 child_xfer_memory (CORE_ADDR memaddr
, char *our
, int len
,
280 int write
, struct target_ops
*target
)
283 uintptr_t addr
= (uintptr_t) memaddr
;
287 WriteProcessMemory (current_process_handle
, (LPVOID
) addr
,
288 (LPCVOID
) our
, len
, &done
);
289 FlushInstructionCache (current_process_handle
, (LPCVOID
) addr
, len
);
293 ReadProcessMemory (current_process_handle
, (LPCVOID
) addr
, (LPVOID
) our
,
299 /* Clear out any old thread list and reinitialize it to a pristine
302 child_init_thread_list (void)
304 for_each_inferior (&all_threads
, delete_thread_info
);
308 do_initial_child_stuff (HANDLE proch
, DWORD pid
, int attached
)
310 last_sig
= TARGET_SIGNAL_0
;
312 current_process_handle
= proch
;
313 current_process_id
= pid
;
316 soft_interrupt_requested
= 0;
317 faked_breakpoint
= 0;
319 memset (¤t_event
, 0, sizeof (current_event
));
321 add_process (pid
, attached
);
322 child_init_thread_list ();
324 if (the_low_target
.initial_stuff
!= NULL
)
325 (*the_low_target
.initial_stuff
) ();
328 /* Resume all artificially suspended threads if we are continuing
331 continue_one_thread (struct inferior_list_entry
*this_thread
, void *id_ptr
)
333 struct thread_info
*thread
= (struct thread_info
*) this_thread
;
334 int thread_id
= * (int *) id_ptr
;
335 win32_thread_info
*th
= inferior_target_data (thread
);
337 if ((thread_id
== -1 || thread_id
== th
->tid
)
340 if (th
->context
.ContextFlags
)
342 win32_set_thread_context (th
);
343 th
->context
.ContextFlags
= 0;
346 if (ResumeThread (th
->h
) == (DWORD
) -1)
348 DWORD err
= GetLastError ();
349 OUTMSG (("warning: ResumeThread failed in continue_one_thread, "
350 "(error %d): %s\n", (int) err
, strwinerror (err
)));
359 child_continue (DWORD continue_status
, int thread_id
)
361 /* The inferior will only continue after the ContinueDebugEvent
363 find_inferior (&all_threads
, continue_one_thread
, &thread_id
);
364 faked_breakpoint
= 0;
366 if (!ContinueDebugEvent (current_event
.dwProcessId
,
367 current_event
.dwThreadId
,
374 /* Fetch register(s) from the current thread context. */
376 child_fetch_inferior_registers (struct regcache
*regcache
, int r
)
379 win32_thread_info
*th
= thread_rec (current_inferior_ptid (), TRUE
);
380 if (r
== -1 || r
> NUM_REGS
)
381 child_fetch_inferior_registers (regcache
, NUM_REGS
);
383 for (regno
= 0; regno
< r
; regno
++)
384 (*the_low_target
.fetch_inferior_register
) (regcache
, th
, regno
);
387 /* Store a new register value into the current thread context. We don't
388 change the program's context until later, when we resume it. */
390 child_store_inferior_registers (struct regcache
*regcache
, int r
)
393 win32_thread_info
*th
= thread_rec (current_inferior_ptid (), TRUE
);
394 if (r
== -1 || r
== 0 || r
> NUM_REGS
)
395 child_store_inferior_registers (regcache
, NUM_REGS
);
397 for (regno
= 0; regno
< r
; regno
++)
398 (*the_low_target
.store_inferior_register
) (regcache
, th
, regno
);
401 /* Map the Windows error number in ERROR to a locale-dependent error
402 message string and return a pointer to it. Typically, the values
403 for ERROR come from GetLastError.
405 The string pointed to shall not be modified by the application,
406 but may be overwritten by a subsequent call to strwinerror
408 The strwinerror function does not change the current setting
412 strwinerror (DWORD error
)
414 static char buf
[1024];
416 DWORD lasterr
= GetLastError ();
417 DWORD chars
= FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
418 | FORMAT_MESSAGE_ALLOCATE_BUFFER
,
421 0, /* Default language */
427 /* If there is an \r\n appended, zap it. */
429 && msgbuf
[chars
- 2] == '\r'
430 && msgbuf
[chars
- 1] == '\n')
436 if (chars
> ((COUNTOF (buf
)) - 1))
438 chars
= COUNTOF (buf
) - 1;
443 wcstombs (buf
, msgbuf
, chars
+ 1);
445 strncpy (buf
, msgbuf
, chars
+ 1);
450 sprintf (buf
, "unknown win32 error (%ld)", error
);
452 SetLastError (lasterr
);
457 create_process (const char *program
, char *args
,
458 DWORD flags
, PROCESS_INFORMATION
*pi
)
463 wchar_t *p
, *wprogram
, *wargs
;
466 wprogram
= alloca ((strlen (program
) + 1) * sizeof (wchar_t));
467 mbstowcs (wprogram
, program
, strlen (program
) + 1);
469 for (p
= wprogram
; *p
; ++p
)
473 argslen
= strlen (args
);
474 wargs
= alloca ((argslen
+ 1) * sizeof (wchar_t));
475 mbstowcs (wargs
, args
, argslen
+ 1);
477 ret
= CreateProcessW (wprogram
, /* image name */
478 wargs
, /* command line */
479 NULL
, /* security, not supported */
480 NULL
, /* thread, not supported */
481 FALSE
, /* inherit handles, not supported */
482 flags
, /* start flags */
483 NULL
, /* environment, not supported */
484 NULL
, /* current directory, not supported */
485 NULL
, /* start info, not supported */
488 STARTUPINFOA si
= { sizeof (STARTUPINFOA
) };
490 ret
= CreateProcessA (program
, /* image name */
491 args
, /* command line */
494 TRUE
, /* inherit handles */
495 flags
, /* start flags */
496 NULL
, /* environment */
497 NULL
, /* current directory */
498 &si
, /* start info */
505 /* Start a new process.
506 PROGRAM is a path to the program to execute.
507 ARGS is a standard NULL-terminated array of arguments,
508 to be passed to the inferior as ``argv''.
509 Returns the new PID on success, -1 on failure. Registers the new
510 process with the process list. */
512 win32_create_inferior (char *program
, char **program_args
)
515 char real_path
[MAXPATHLEN
];
516 char *orig_path
, *new_path
, *path_ptr
;
523 PROCESS_INFORMATION pi
;
526 /* win32_wait needs to know we're not attaching. */
530 error ("No executable specified, specify executable to debug.\n");
532 flags
= DEBUG_PROCESS
| DEBUG_ONLY_THIS_PROCESS
;
536 path_ptr
= getenv ("PATH");
539 int size
= cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, NULL
, 0);
540 orig_path
= alloca (strlen (path_ptr
) + 1);
541 new_path
= alloca (size
);
542 strcpy (orig_path
, path_ptr
);
543 cygwin_conv_path_list (CCP_POSIX_TO_WIN_A
, path_ptr
, new_path
, size
);
544 setenv ("PATH", new_path
, 1);
546 cygwin_conv_path (CCP_POSIX_TO_WIN_A
, program
, real_path
,
552 for (argc
= 1; program_args
[argc
]; argc
++)
553 argslen
+= strlen (program_args
[argc
]) + 1;
554 args
= alloca (argslen
);
556 for (argc
= 1; program_args
[argc
]; argc
++)
558 /* FIXME: Can we do better about quoting? How does Cygwin
561 strcat (args
, program_args
[argc
]);
563 OUTMSG2 (("Command line is \"%s\"\n", args
));
565 #ifdef CREATE_NEW_PROCESS_GROUP
566 flags
|= CREATE_NEW_PROCESS_GROUP
;
569 ret
= create_process (program
, args
, flags
, &pi
);
570 err
= GetLastError ();
571 if (!ret
&& err
== ERROR_FILE_NOT_FOUND
)
573 char *exename
= alloca (strlen (program
) + 5);
574 strcat (strcpy (exename
, program
), ".exe");
575 ret
= create_process (exename
, args
, flags
, &pi
);
576 err
= GetLastError ();
581 setenv ("PATH", orig_path
, 1);
586 error ("Error creating process \"%s%s\", (error %d): %s\n",
587 program
, args
, (int) err
, strwinerror (err
));
591 OUTMSG2 (("Process created: %s\n", (char *) args
));
595 /* On Windows CE this handle can't be closed. The OS reuses
596 it in the debug events, while the 9x/NT versions of Windows
597 probably use a DuplicateHandle'd one. */
598 CloseHandle (pi
.hThread
);
601 do_initial_child_stuff (pi
.hProcess
, pi
.dwProcessId
, 0);
603 return current_process_id
;
606 /* Attach to a running process.
607 PID is the process ID to attach to, specified by the user
608 or a higher layer. */
610 win32_attach (unsigned long pid
)
613 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
616 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
618 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
620 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
622 h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
625 if (DebugActiveProcess (pid
))
627 if (DebugSetProcessKillOnExit
!= NULL
)
628 DebugSetProcessKillOnExit (FALSE
);
630 /* win32_wait needs to know we're attaching. */
632 do_initial_child_stuff (h
, pid
, 1);
639 err
= GetLastError ();
640 error ("Attach to process failed (error %d): %s\n",
641 (int) err
, strwinerror (err
));
644 /* Handle OUTPUT_DEBUG_STRING_EVENT from child process. */
646 handle_output_debug_string (struct target_waitstatus
*ourstatus
)
648 #define READ_BUFFER_LEN 1024
650 char s
[READ_BUFFER_LEN
+ 1] = { 0 };
651 DWORD nbytes
= current_event
.u
.DebugString
.nDebugStringLength
;
656 if (nbytes
> READ_BUFFER_LEN
)
657 nbytes
= READ_BUFFER_LEN
;
659 addr
= (CORE_ADDR
) (size_t) current_event
.u
.DebugString
.lpDebugStringData
;
661 if (current_event
.u
.DebugString
.fUnicode
)
663 /* The event tells us how many bytes, not chars, even
665 WCHAR buffer
[(READ_BUFFER_LEN
+ 1) / sizeof (WCHAR
)] = { 0 };
666 if (read_inferior_memory (addr
, (unsigned char *) buffer
, nbytes
) != 0)
668 wcstombs (s
, buffer
, (nbytes
+ 1) / sizeof (WCHAR
));
672 if (read_inferior_memory (addr
, (unsigned char *) s
, nbytes
) != 0)
676 if (strncmp (s
, "cYg", 3) != 0)
686 #undef READ_BUFFER_LEN
690 win32_clear_inferiors (void)
692 if (current_process_handle
!= NULL
)
693 CloseHandle (current_process_handle
);
695 for_each_inferior (&all_threads
, delete_thread_info
);
699 /* Kill all inferiors. */
703 struct process_info
*process
;
705 if (current_process_handle
== NULL
)
708 TerminateProcess (current_process_handle
, 0);
711 if (!child_continue (DBG_CONTINUE
, -1))
713 if (!WaitForDebugEvent (¤t_event
, INFINITE
))
715 if (current_event
.dwDebugEventCode
== EXIT_PROCESS_DEBUG_EVENT
)
717 else if (current_event
.dwDebugEventCode
== OUTPUT_DEBUG_STRING_EVENT
)
719 struct target_waitstatus our_status
= { 0 };
720 handle_output_debug_string (&our_status
);
724 win32_clear_inferiors ();
726 process
= find_process_pid (pid
);
727 remove_process (process
);
731 /* Detach from inferior PID. */
733 win32_detach (int pid
)
735 struct process_info
*process
;
736 winapi_DebugActiveProcessStop DebugActiveProcessStop
= NULL
;
737 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
739 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
741 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
743 DebugActiveProcessStop
= GETPROCADDRESS (dll
, DebugActiveProcessStop
);
744 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
746 if (DebugSetProcessKillOnExit
== NULL
747 || DebugActiveProcessStop
== NULL
)
751 struct thread_resume resume
;
752 resume
.thread
= minus_one_ptid
;
753 resume
.kind
= resume_continue
;
755 win32_resume (&resume
, 1);
758 if (!DebugActiveProcessStop (current_process_id
))
761 DebugSetProcessKillOnExit (FALSE
);
762 process
= find_process_pid (pid
);
763 remove_process (process
);
765 win32_clear_inferiors ();
770 win32_mourn (struct process_info
*process
)
772 remove_process (process
);
775 /* Wait for inferiors to end. */
779 HANDLE h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
782 WaitForSingleObject (h
, INFINITE
);
787 /* Return 1 iff the thread with thread ID TID is alive. */
789 win32_thread_alive (ptid_t ptid
)
793 /* Our thread list is reliable; don't bother to poll target
795 if (find_inferior_id (&all_threads
, ptid
) != NULL
)
802 /* Resume the inferior process. RESUME_INFO describes how we want
805 win32_resume (struct thread_resume
*resume_info
, size_t n
)
808 enum target_signal sig
;
810 win32_thread_info
*th
;
811 DWORD continue_status
= DBG_CONTINUE
;
814 /* This handles the very limited set of resume packets that GDB can
815 currently produce. */
817 if (n
== 1 && ptid_equal (resume_info
[0].thread
, minus_one_ptid
))
822 /* Yes, we're ignoring resume_info[0].thread. It'd be tricky to make
823 the Windows resume code do the right thing for thread switching. */
824 tid
= current_event
.dwThreadId
;
826 if (!ptid_equal (resume_info
[0].thread
, minus_one_ptid
))
828 sig
= resume_info
[0].sig
;
829 step
= resume_info
[0].kind
== resume_step
;
837 if (sig
!= TARGET_SIGNAL_0
)
839 if (current_event
.dwDebugEventCode
!= EXCEPTION_DEBUG_EVENT
)
841 OUTMSG (("Cannot continue with signal %d here.\n", sig
));
843 else if (sig
== last_sig
)
844 continue_status
= DBG_EXCEPTION_NOT_HANDLED
;
846 OUTMSG (("Can only continue with recieved signal %d.\n", last_sig
));
849 last_sig
= TARGET_SIGNAL_0
;
851 /* Get context for the currently selected thread. */
852 ptid
= debug_event_ptid (¤t_event
);
853 th
= thread_rec (ptid
, FALSE
);
856 if (th
->context
.ContextFlags
)
858 /* Move register values from the inferior into the thread
859 context structure. */
860 regcache_invalidate ();
864 if (the_low_target
.single_step
!= NULL
)
865 (*the_low_target
.single_step
) (th
);
867 error ("Single stepping is not supported "
868 "in this configuration.\n");
871 win32_set_thread_context (th
);
872 th
->context
.ContextFlags
= 0;
876 /* Allow continuing with the same signal that interrupted us.
877 Otherwise complain. */
879 child_continue (continue_status
, tid
);
883 win32_add_one_solib (const char *name
, CORE_ADDR load_addr
)
885 char buf
[MAX_PATH
+ 1];
886 char buf2
[MAX_PATH
+ 1];
889 WIN32_FIND_DATA w32_fd
;
890 WCHAR wname
[MAX_PATH
+ 1];
891 mbstowcs (wname
, name
, MAX_PATH
);
892 HANDLE h
= FindFirstFile (wname
, &w32_fd
);
894 WIN32_FIND_DATAA w32_fd
;
895 HANDLE h
= FindFirstFileA (name
, &w32_fd
);
898 if (h
== INVALID_HANDLE_VALUE
)
906 char cwd
[MAX_PATH
+ 1];
908 if (GetCurrentDirectoryA (MAX_PATH
+ 1, cwd
))
910 p
= strrchr (buf
, '\\');
913 SetCurrentDirectoryA (buf
);
914 GetFullPathNameA (w32_fd
.cFileName
, MAX_PATH
, buf
, &p
);
915 SetCurrentDirectoryA (cwd
);
922 if (strcasecmp (buf
, "ntdll.dll") == 0)
924 GetSystemDirectoryA (buf
, sizeof (buf
));
925 strcat (buf
, "\\ntdll.dll");
930 cygwin_conv_path (CCP_WIN_A_TO_POSIX
, buf
, buf2
, sizeof (buf2
));
935 loaded_dll (buf2
, load_addr
);
939 get_image_name (HANDLE h
, void *address
, int unicode
)
941 static char buf
[(2 * MAX_PATH
) + 1];
942 DWORD size
= unicode
? sizeof (WCHAR
) : sizeof (char);
948 /* Attempt to read the name of the dll that was detected.
949 This is documented to work only when actively debugging
950 a program. It will not work for attached processes. */
955 /* Windows CE reports the address of the image name,
956 instead of an address of a pointer into the image name. */
957 address_ptr
= address
;
959 /* See if we could read the address of a string, and that the
960 address isn't null. */
961 if (!ReadProcessMemory (h
, address
, &address_ptr
,
962 sizeof (address_ptr
), &done
)
963 || done
!= sizeof (address_ptr
)
968 /* Find the length of the string */
969 while (ReadProcessMemory (h
, address_ptr
+ len
++ * size
, &b
, size
, &done
)
970 && (b
[0] != 0 || b
[size
- 1] != 0) && done
== size
)
974 ReadProcessMemory (h
, address_ptr
, buf
, len
, &done
);
977 WCHAR
*unicode_address
= (WCHAR
*) alloca (len
* sizeof (WCHAR
));
978 ReadProcessMemory (h
, address_ptr
, unicode_address
, len
* sizeof (WCHAR
),
981 WideCharToMultiByte (CP_ACP
, 0, unicode_address
, len
, buf
, len
, 0, 0);
987 typedef BOOL (WINAPI
*winapi_EnumProcessModules
) (HANDLE
, HMODULE
*,
989 typedef BOOL (WINAPI
*winapi_GetModuleInformation
) (HANDLE
, HMODULE
,
990 LPMODULEINFO
, DWORD
);
991 typedef DWORD (WINAPI
*winapi_GetModuleFileNameExA
) (HANDLE
, HMODULE
,
994 static winapi_EnumProcessModules win32_EnumProcessModules
;
995 static winapi_GetModuleInformation win32_GetModuleInformation
;
996 static winapi_GetModuleFileNameExA win32_GetModuleFileNameExA
;
1001 static int psapi_loaded
= 0;
1002 static HMODULE dll
= NULL
;
1007 dll
= LoadLibrary (TEXT("psapi.dll"));
1010 win32_EnumProcessModules
=
1011 GETPROCADDRESS (dll
, EnumProcessModules
);
1012 win32_GetModuleInformation
=
1013 GETPROCADDRESS (dll
, GetModuleInformation
);
1014 win32_GetModuleFileNameExA
=
1015 GETPROCADDRESS (dll
, GetModuleFileNameExA
);
1018 return (win32_EnumProcessModules
!= NULL
1019 && win32_GetModuleInformation
!= NULL
1020 && win32_GetModuleFileNameExA
!= NULL
);
1024 psapi_get_dll_name (LPVOID BaseAddress
, char *dll_name_ret
)
1030 HMODULE
*DllHandle
= dh_buf
;
1038 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1043 if (!ok
|| !cbNeeded
)
1046 DllHandle
= (HMODULE
*) alloca (cbNeeded
);
1050 ok
= (*win32_EnumProcessModules
) (current_process_handle
,
1057 for (i
= 0; i
< ((size_t) cbNeeded
/ sizeof (HMODULE
)); i
++)
1059 if (!(*win32_GetModuleInformation
) (current_process_handle
,
1064 DWORD err
= GetLastError ();
1065 error ("Can't get module info: (error %d): %s\n",
1066 (int) err
, strwinerror (err
));
1069 if (mi
.lpBaseOfDll
== BaseAddress
)
1071 len
= (*win32_GetModuleFileNameExA
) (current_process_handle
,
1077 DWORD err
= GetLastError ();
1078 error ("Error getting dll name: (error %d): %s\n",
1079 (int) err
, strwinerror (err
));
1086 dll_name_ret
[0] = '\0';
1090 typedef HANDLE (WINAPI
*winapi_CreateToolhelp32Snapshot
) (DWORD
, DWORD
);
1091 typedef BOOL (WINAPI
*winapi_Module32First
) (HANDLE
, LPMODULEENTRY32
);
1092 typedef BOOL (WINAPI
*winapi_Module32Next
) (HANDLE
, LPMODULEENTRY32
);
1094 static winapi_CreateToolhelp32Snapshot win32_CreateToolhelp32Snapshot
;
1095 static winapi_Module32First win32_Module32First
;
1096 static winapi_Module32Next win32_Module32Next
;
1098 typedef BOOL (WINAPI
*winapi_CloseToolhelp32Snapshot
) (HANDLE
);
1099 static winapi_CloseToolhelp32Snapshot win32_CloseToolhelp32Snapshot
;
1103 load_toolhelp (void)
1105 static int toolhelp_loaded
= 0;
1106 static HMODULE dll
= NULL
;
1108 if (!toolhelp_loaded
)
1110 toolhelp_loaded
= 1;
1112 dll
= GetModuleHandle (_T("KERNEL32.DLL"));
1114 dll
= LoadLibrary (L
"TOOLHELP.DLL");
1119 win32_CreateToolhelp32Snapshot
=
1120 GETPROCADDRESS (dll
, CreateToolhelp32Snapshot
);
1121 win32_Module32First
= GETPROCADDRESS (dll
, Module32First
);
1122 win32_Module32Next
= GETPROCADDRESS (dll
, Module32Next
);
1124 win32_CloseToolhelp32Snapshot
=
1125 GETPROCADDRESS (dll
, CloseToolhelp32Snapshot
);
1129 return (win32_CreateToolhelp32Snapshot
!= NULL
1130 && win32_Module32First
!= NULL
1131 && win32_Module32Next
!= NULL
1133 && win32_CloseToolhelp32Snapshot
!= NULL
1139 toolhelp_get_dll_name (LPVOID BaseAddress
, char *dll_name_ret
)
1141 HANDLE snapshot_module
;
1142 MODULEENTRY32 modEntry
= { sizeof (MODULEENTRY32
) };
1145 if (!load_toolhelp ())
1148 snapshot_module
= win32_CreateToolhelp32Snapshot (TH32CS_SNAPMODULE
,
1149 current_event
.dwProcessId
);
1150 if (snapshot_module
== INVALID_HANDLE_VALUE
)
1153 /* Ignore the first module, which is the exe. */
1154 if (win32_Module32First (snapshot_module
, &modEntry
))
1155 while (win32_Module32Next (snapshot_module
, &modEntry
))
1156 if (modEntry
.modBaseAddr
== BaseAddress
)
1159 wcstombs (dll_name_ret
, modEntry
.szExePath
, MAX_PATH
+ 1);
1161 strcpy (dll_name_ret
, modEntry
.szExePath
);
1168 win32_CloseToolhelp32Snapshot (snapshot_module
);
1170 CloseHandle (snapshot_module
);
1176 handle_load_dll (void)
1178 LOAD_DLL_DEBUG_INFO
*event
= ¤t_event
.u
.LoadDll
;
1179 char dll_buf
[MAX_PATH
+ 1];
1180 char *dll_name
= NULL
;
1181 CORE_ADDR load_addr
;
1183 dll_buf
[0] = dll_buf
[sizeof (dll_buf
) - 1] = '\0';
1185 /* Windows does not report the image name of the dlls in the debug
1186 event on attaches. We resort to iterating over the list of
1187 loaded dlls looking for a match by image base. */
1188 if (!psapi_get_dll_name (event
->lpBaseOfDll
, dll_buf
))
1190 if (!server_waiting
)
1191 /* On some versions of Windows and Windows CE, we can't create
1192 toolhelp snapshots while the inferior is stopped in a
1193 LOAD_DLL_DEBUG_EVENT due to a dll load, but we can while
1194 Windows is reporting the already loaded dlls. */
1195 toolhelp_get_dll_name (event
->lpBaseOfDll
, dll_buf
);
1200 if (*dll_name
== '\0')
1201 dll_name
= get_image_name (current_process_handle
,
1202 event
->lpImageName
, event
->fUnicode
);
1206 /* The symbols in a dll are offset by 0x1000, which is the
1207 offset from 0 of the first byte in an image - because
1208 of the file header and the section alignment. */
1210 load_addr
= (CORE_ADDR
) (uintptr_t) event
->lpBaseOfDll
+ 0x1000;
1211 win32_add_one_solib (dll_name
, load_addr
);
1215 handle_unload_dll (void)
1217 CORE_ADDR load_addr
=
1218 (CORE_ADDR
) (uintptr_t) current_event
.u
.UnloadDll
.lpBaseOfDll
;
1219 load_addr
+= 0x1000;
1220 unloaded_dll (NULL
, load_addr
);
1224 handle_exception (struct target_waitstatus
*ourstatus
)
1226 DWORD code
= current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
;
1228 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
1232 case EXCEPTION_ACCESS_VIOLATION
:
1233 OUTMSG2 (("EXCEPTION_ACCESS_VIOLATION"));
1234 ourstatus
->value
.sig
= TARGET_SIGNAL_SEGV
;
1236 case STATUS_STACK_OVERFLOW
:
1237 OUTMSG2 (("STATUS_STACK_OVERFLOW"));
1238 ourstatus
->value
.sig
= TARGET_SIGNAL_SEGV
;
1240 case STATUS_FLOAT_DENORMAL_OPERAND
:
1241 OUTMSG2 (("STATUS_FLOAT_DENORMAL_OPERAND"));
1242 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1244 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
1245 OUTMSG2 (("EXCEPTION_ARRAY_BOUNDS_EXCEEDED"));
1246 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1248 case STATUS_FLOAT_INEXACT_RESULT
:
1249 OUTMSG2 (("STATUS_FLOAT_INEXACT_RESULT"));
1250 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1252 case STATUS_FLOAT_INVALID_OPERATION
:
1253 OUTMSG2 (("STATUS_FLOAT_INVALID_OPERATION"));
1254 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1256 case STATUS_FLOAT_OVERFLOW
:
1257 OUTMSG2 (("STATUS_FLOAT_OVERFLOW"));
1258 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1260 case STATUS_FLOAT_STACK_CHECK
:
1261 OUTMSG2 (("STATUS_FLOAT_STACK_CHECK"));
1262 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1264 case STATUS_FLOAT_UNDERFLOW
:
1265 OUTMSG2 (("STATUS_FLOAT_UNDERFLOW"));
1266 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1268 case STATUS_FLOAT_DIVIDE_BY_ZERO
:
1269 OUTMSG2 (("STATUS_FLOAT_DIVIDE_BY_ZERO"));
1270 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1272 case STATUS_INTEGER_DIVIDE_BY_ZERO
:
1273 OUTMSG2 (("STATUS_INTEGER_DIVIDE_BY_ZERO"));
1274 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1276 case STATUS_INTEGER_OVERFLOW
:
1277 OUTMSG2 (("STATUS_INTEGER_OVERFLOW"));
1278 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1280 case EXCEPTION_BREAKPOINT
:
1281 OUTMSG2 (("EXCEPTION_BREAKPOINT"));
1282 ourstatus
->value
.sig
= TARGET_SIGNAL_TRAP
;
1284 /* Remove the initial breakpoint. */
1285 check_breakpoints ((CORE_ADDR
) (long) current_event
1286 .u
.Exception
.ExceptionRecord
.ExceptionAddress
);
1290 OUTMSG2 (("DBG_CONTROL_C"));
1291 ourstatus
->value
.sig
= TARGET_SIGNAL_INT
;
1293 case DBG_CONTROL_BREAK
:
1294 OUTMSG2 (("DBG_CONTROL_BREAK"));
1295 ourstatus
->value
.sig
= TARGET_SIGNAL_INT
;
1297 case EXCEPTION_SINGLE_STEP
:
1298 OUTMSG2 (("EXCEPTION_SINGLE_STEP"));
1299 ourstatus
->value
.sig
= TARGET_SIGNAL_TRAP
;
1301 case EXCEPTION_ILLEGAL_INSTRUCTION
:
1302 OUTMSG2 (("EXCEPTION_ILLEGAL_INSTRUCTION"));
1303 ourstatus
->value
.sig
= TARGET_SIGNAL_ILL
;
1305 case EXCEPTION_PRIV_INSTRUCTION
:
1306 OUTMSG2 (("EXCEPTION_PRIV_INSTRUCTION"));
1307 ourstatus
->value
.sig
= TARGET_SIGNAL_ILL
;
1309 case EXCEPTION_NONCONTINUABLE_EXCEPTION
:
1310 OUTMSG2 (("EXCEPTION_NONCONTINUABLE_EXCEPTION"));
1311 ourstatus
->value
.sig
= TARGET_SIGNAL_ILL
;
1314 if (current_event
.u
.Exception
.dwFirstChance
)
1316 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1319 OUTMSG2 (("gdbserver: unknown target exception 0x%08lx at 0x%s",
1320 current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
,
1321 phex_nz ((uintptr_t) current_event
.u
.Exception
.ExceptionRecord
.
1322 ExceptionAddress
, sizeof (uintptr_t))));
1323 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
1327 last_sig
= ourstatus
->value
.sig
;
1332 suspend_one_thread (struct inferior_list_entry
*entry
)
1334 struct thread_info
*thread
= (struct thread_info
*) entry
;
1335 win32_thread_info
*th
= inferior_target_data (thread
);
1339 if (SuspendThread (th
->h
) == (DWORD
) -1)
1341 DWORD err
= GetLastError ();
1342 OUTMSG (("warning: SuspendThread failed in suspend_one_thread, "
1343 "(error %d): %s\n", (int) err
, strwinerror (err
)));
1351 fake_breakpoint_event (void)
1353 OUTMSG2(("fake_breakpoint_event\n"));
1355 faked_breakpoint
= 1;
1357 memset (¤t_event
, 0, sizeof (current_event
));
1358 current_event
.dwThreadId
= main_thread_id
;
1359 current_event
.dwDebugEventCode
= EXCEPTION_DEBUG_EVENT
;
1360 current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
1361 = EXCEPTION_BREAKPOINT
;
1363 for_each_inferior (&all_threads
, suspend_one_thread
);
1368 auto_delete_breakpoint (CORE_ADDR stop_pc
)
1374 /* Get the next event from the child. */
1377 get_child_debug_event (struct target_waitstatus
*ourstatus
)
1381 last_sig
= TARGET_SIGNAL_0
;
1382 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1384 /* Check if GDB sent us an interrupt request. */
1385 check_remote_input_interrupt_request ();
1387 if (soft_interrupt_requested
)
1389 soft_interrupt_requested
= 0;
1390 fake_breakpoint_event ();
1399 /* WinCE doesn't set an initial breakpoint automatically. To
1400 stop the inferior, we flush all currently pending debug
1401 events -- the thread list and the dll list are always
1402 reported immediatelly without delay, then, we suspend all
1403 threads and pretend we saw a trap at the current PC of the
1406 Contrary to desktop Windows, Windows CE *does* report the dll
1407 names on LOAD_DLL_DEBUG_EVENTs resulting from a
1408 DebugActiveProcess call. This limits the way we can detect
1409 if all the dlls have already been reported. If we get a real
1410 debug event before leaving attaching, the worst that will
1411 happen is the user will see a spurious breakpoint. */
1413 current_event
.dwDebugEventCode
= 0;
1414 if (!WaitForDebugEvent (¤t_event
, 0))
1416 OUTMSG2(("no attach events left\n"));
1417 fake_breakpoint_event ();
1421 OUTMSG2(("got attach event\n"));
1426 /* Keep the wait time low enough for confortable remote
1427 interruption, but high enough so gdbserver doesn't become a
1429 if (!WaitForDebugEvent (¤t_event
, 250))
1431 DWORD e
= GetLastError();
1433 if (e
== ERROR_PIPE_NOT_CONNECTED
)
1435 /* This will happen if the loader fails to succesfully
1436 load the application, e.g., if the main executable
1437 tries to pull in a non-existing export from a
1439 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1440 ourstatus
->value
.integer
= 1;
1450 switch (current_event
.dwDebugEventCode
)
1452 case CREATE_THREAD_DEBUG_EVENT
:
1453 OUTMSG2 (("gdbserver: kernel event CREATE_THREAD_DEBUG_EVENT "
1454 "for pid=%d tid=%x)\n",
1455 (unsigned) current_event
.dwProcessId
,
1456 (unsigned) current_event
.dwThreadId
));
1458 /* Record the existence of this thread. */
1459 child_add_thread (current_event
.dwProcessId
,
1460 current_event
.dwThreadId
,
1461 current_event
.u
.CreateThread
.hThread
,
1462 current_event
.u
.CreateThread
.lpThreadLocalBase
);
1465 case EXIT_THREAD_DEBUG_EVENT
:
1466 OUTMSG2 (("gdbserver: kernel event EXIT_THREAD_DEBUG_EVENT "
1467 "for pid=%d tid=%x\n",
1468 (unsigned) current_event
.dwProcessId
,
1469 (unsigned) current_event
.dwThreadId
));
1470 child_delete_thread (current_event
.dwProcessId
,
1471 current_event
.dwThreadId
);
1473 current_inferior
= (struct thread_info
*) all_threads
.head
;
1476 case CREATE_PROCESS_DEBUG_EVENT
:
1477 OUTMSG2 (("gdbserver: kernel event CREATE_PROCESS_DEBUG_EVENT "
1478 "for pid=%d tid=%x\n",
1479 (unsigned) current_event
.dwProcessId
,
1480 (unsigned) current_event
.dwThreadId
));
1481 CloseHandle (current_event
.u
.CreateProcessInfo
.hFile
);
1483 current_process_handle
= current_event
.u
.CreateProcessInfo
.hProcess
;
1484 main_thread_id
= current_event
.dwThreadId
;
1486 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
1487 ourstatus
->value
.execd_pathname
= "Main executable";
1489 /* Add the main thread. */
1490 child_add_thread (current_event
.dwProcessId
,
1492 current_event
.u
.CreateProcessInfo
.hThread
,
1493 current_event
.u
.CreateProcessInfo
.lpThreadLocalBase
);
1495 ourstatus
->value
.related_pid
= debug_event_ptid (¤t_event
);
1499 /* Windows CE doesn't set the initial breakpoint
1500 automatically like the desktop versions of Windows do.
1501 We add it explicitly here. It will be removed as soon as
1503 set_breakpoint_at ((CORE_ADDR
) (long) current_event
.u
1504 .CreateProcessInfo
.lpStartAddress
,
1505 auto_delete_breakpoint
);
1510 case EXIT_PROCESS_DEBUG_EVENT
:
1511 OUTMSG2 (("gdbserver: kernel event EXIT_PROCESS_DEBUG_EVENT "
1512 "for pid=%d tid=%x\n",
1513 (unsigned) current_event
.dwProcessId
,
1514 (unsigned) current_event
.dwThreadId
));
1515 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1516 ourstatus
->value
.integer
= current_event
.u
.ExitProcess
.dwExitCode
;
1517 child_continue (DBG_CONTINUE
, -1);
1518 CloseHandle (current_process_handle
);
1519 current_process_handle
= NULL
;
1522 case LOAD_DLL_DEBUG_EVENT
:
1523 OUTMSG2 (("gdbserver: kernel event LOAD_DLL_DEBUG_EVENT "
1524 "for pid=%d tid=%x\n",
1525 (unsigned) current_event
.dwProcessId
,
1526 (unsigned) current_event
.dwThreadId
));
1527 CloseHandle (current_event
.u
.LoadDll
.hFile
);
1530 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1531 ourstatus
->value
.sig
= TARGET_SIGNAL_TRAP
;
1534 case UNLOAD_DLL_DEBUG_EVENT
:
1535 OUTMSG2 (("gdbserver: kernel event UNLOAD_DLL_DEBUG_EVENT "
1536 "for pid=%d tid=%x\n",
1537 (unsigned) current_event
.dwProcessId
,
1538 (unsigned) current_event
.dwThreadId
));
1539 handle_unload_dll ();
1540 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1541 ourstatus
->value
.sig
= TARGET_SIGNAL_TRAP
;
1544 case EXCEPTION_DEBUG_EVENT
:
1545 OUTMSG2 (("gdbserver: kernel event EXCEPTION_DEBUG_EVENT "
1546 "for pid=%d tid=%x\n",
1547 (unsigned) current_event
.dwProcessId
,
1548 (unsigned) current_event
.dwThreadId
));
1549 handle_exception (ourstatus
);
1552 case OUTPUT_DEBUG_STRING_EVENT
:
1553 /* A message from the kernel (or Cygwin). */
1554 OUTMSG2 (("gdbserver: kernel event OUTPUT_DEBUG_STRING_EVENT "
1555 "for pid=%d tid=%x\n",
1556 (unsigned) current_event
.dwProcessId
,
1557 (unsigned) current_event
.dwThreadId
));
1558 handle_output_debug_string (ourstatus
);
1562 OUTMSG2 (("gdbserver: kernel event unknown "
1563 "for pid=%d tid=%x code=%ld\n",
1564 (unsigned) current_event
.dwProcessId
,
1565 (unsigned) current_event
.dwThreadId
,
1566 current_event
.dwDebugEventCode
));
1570 ptid
= debug_event_ptid (¤t_event
);
1572 (struct thread_info
*) find_inferior_id (&all_threads
, ptid
);
1576 /* Wait for the inferior process to change state.
1577 STATUS will be filled in with a response code to send to GDB.
1578 Returns the signal which caused the process to stop. */
1580 win32_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
, int options
)
1582 struct regcache
*regcache
;
1586 if (!get_child_debug_event (ourstatus
))
1589 switch (ourstatus
->kind
)
1591 case TARGET_WAITKIND_EXITED
:
1592 OUTMSG2 (("Child exited with retcode = %x\n",
1593 ourstatus
->value
.integer
));
1594 win32_clear_inferiors ();
1595 return pid_to_ptid (current_event
.dwProcessId
);
1596 case TARGET_WAITKIND_STOPPED
:
1597 case TARGET_WAITKIND_LOADED
:
1598 OUTMSG2 (("Child Stopped with signal = %d \n",
1599 ourstatus
->value
.sig
));
1601 regcache
= get_thread_regcache (current_inferior
, 1);
1602 child_fetch_inferior_registers (regcache
, -1);
1604 if (ourstatus
->kind
== TARGET_WAITKIND_LOADED
1607 /* When gdb connects, we want to be stopped at the
1608 initial breakpoint, not in some dll load event. */
1609 child_continue (DBG_CONTINUE
, -1);
1613 /* We don't expose _LOADED events to gdbserver core. See
1614 the `dlls_changed' global. */
1615 if (ourstatus
->kind
== TARGET_WAITKIND_LOADED
)
1616 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
1618 return debug_event_ptid (¤t_event
);
1620 OUTMSG (("Ignoring unknown internal event, %d\n", ourstatus
->kind
));
1622 case TARGET_WAITKIND_SPURIOUS
:
1623 case TARGET_WAITKIND_EXECD
:
1624 /* do nothing, just continue */
1625 child_continue (DBG_CONTINUE
, -1);
1631 /* Fetch registers from the inferior process.
1632 If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO. */
1634 win32_fetch_inferior_registers (struct regcache
*regcache
, int regno
)
1636 child_fetch_inferior_registers (regcache
, regno
);
1639 /* Store registers to the inferior process.
1640 If REGNO is -1, store all registers; otherwise, store at least REGNO. */
1642 win32_store_inferior_registers (struct regcache
*regcache
, int regno
)
1644 child_store_inferior_registers (regcache
, regno
);
1647 /* Read memory from the inferior process. This should generally be
1648 called through read_inferior_memory, which handles breakpoint shadowing.
1649 Read LEN bytes at MEMADDR into a buffer at MYADDR. */
1651 win32_read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
1653 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 0, 0) != len
;
1656 /* Write memory to the inferior process. This should generally be
1657 called through write_inferior_memory, which handles breakpoint shadowing.
1658 Write LEN bytes from the buffer at MYADDR to MEMADDR.
1659 Returns 0 on success and errno on failure. */
1661 win32_write_inferior_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
,
1664 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 1, 0) != len
;
1667 /* Send an interrupt request to the inferior process. */
1669 win32_request_interrupt (void)
1671 winapi_DebugBreakProcess DebugBreakProcess
;
1672 winapi_GenerateConsoleCtrlEvent GenerateConsoleCtrlEvent
;
1675 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
1677 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
1680 GenerateConsoleCtrlEvent
= GETPROCADDRESS (dll
, GenerateConsoleCtrlEvent
);
1682 if (GenerateConsoleCtrlEvent
!= NULL
1683 && GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT
, current_process_id
))
1686 /* GenerateConsoleCtrlEvent can fail if process id being debugged is
1687 not a process group id.
1688 Fallback to XP/Vista 'DebugBreakProcess', which generates a
1689 breakpoint exception in the interior process. */
1691 DebugBreakProcess
= GETPROCADDRESS (dll
, DebugBreakProcess
);
1693 if (DebugBreakProcess
!= NULL
1694 && DebugBreakProcess (current_process_handle
))
1697 /* Last resort, suspend all threads manually. */
1698 soft_interrupt_requested
= 1;
1703 win32_error_to_fileio_error (DWORD err
)
1707 case ERROR_BAD_PATHNAME
:
1708 case ERROR_FILE_NOT_FOUND
:
1709 case ERROR_INVALID_NAME
:
1710 case ERROR_PATH_NOT_FOUND
:
1711 return FILEIO_ENOENT
;
1713 case ERROR_IO_DEVICE
:
1714 case ERROR_OPEN_FAILED
:
1716 case ERROR_INVALID_HANDLE
:
1717 return FILEIO_EBADF
;
1718 case ERROR_ACCESS_DENIED
:
1719 case ERROR_SHARING_VIOLATION
:
1720 return FILEIO_EACCES
;
1721 case ERROR_NOACCESS
:
1722 return FILEIO_EFAULT
;
1724 return FILEIO_EBUSY
;
1725 case ERROR_ALREADY_EXISTS
:
1726 case ERROR_FILE_EXISTS
:
1727 return FILEIO_EEXIST
;
1728 case ERROR_BAD_DEVICE
:
1729 return FILEIO_ENODEV
;
1730 case ERROR_DIRECTORY
:
1731 return FILEIO_ENOTDIR
;
1732 case ERROR_FILENAME_EXCED_RANGE
:
1733 case ERROR_INVALID_DATA
:
1734 case ERROR_INVALID_PARAMETER
:
1735 case ERROR_NEGATIVE_SEEK
:
1736 return FILEIO_EINVAL
;
1737 case ERROR_TOO_MANY_OPEN_FILES
:
1738 return FILEIO_EMFILE
;
1739 case ERROR_HANDLE_DISK_FULL
:
1740 case ERROR_DISK_FULL
:
1741 return FILEIO_ENOSPC
;
1742 case ERROR_WRITE_PROTECT
:
1743 return FILEIO_EROFS
;
1744 case ERROR_NOT_SUPPORTED
:
1745 return FILEIO_ENOSYS
;
1748 return FILEIO_EUNKNOWN
;
1752 wince_hostio_last_error (char *buf
)
1754 DWORD winerr
= GetLastError ();
1755 int fileio_err
= win32_error_to_fileio_error (winerr
);
1756 sprintf (buf
, "F-1,%x", fileio_err
);
1760 /* Write Windows OS Thread Information Block address. */
1763 win32_get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
)
1765 win32_thread_info
*th
;
1766 th
= thread_rec (ptid
, 0);
1770 *addr
= th
->thread_local_base
;
1774 static struct target_ops win32_target_ops
= {
1775 win32_create_inferior
,
1784 win32_fetch_inferior_registers
,
1785 win32_store_inferior_registers
,
1786 NULL
, /* prepare_to_access_memory */
1787 NULL
, /* done_accessing_memory */
1788 win32_read_inferior_memory
,
1789 win32_write_inferior_memory
,
1790 NULL
, /* lookup_symbols */
1791 win32_request_interrupt
,
1792 NULL
, /* read_auxv */
1795 win32_stopped_by_watchpoint
,
1796 win32_stopped_data_address
,
1797 NULL
, /* read_offsets */
1798 NULL
, /* get_tls_address */
1799 NULL
, /* qxfer_spu */
1801 wince_hostio_last_error
,
1803 hostio_last_error_from_errno
,
1805 NULL
, /* qxfer_osdata */
1806 NULL
, /* qxfer_siginfo */
1807 NULL
, /* supports_non_stop */
1809 NULL
, /* start_non_stop */
1810 NULL
, /* supports_multi_process */
1811 NULL
, /* handle_monitor_command */
1812 NULL
, /* core_of_thread */
1813 NULL
, /* process_qsupported */
1814 NULL
, /* supports_tracepoints */
1816 NULL
, /* write_pc */
1817 NULL
, /* thread_stopped */
1818 win32_get_tib_address
1821 /* Initialize the Win32 backend. */
1823 initialize_low (void)
1825 set_target_ops (&win32_target_ops
);
1826 if (the_low_target
.breakpoint
!= NULL
)
1827 set_breakpoint_data (the_low_target
.breakpoint
,
1828 the_low_target
.breakpoint_len
);
1829 the_low_target
.arch_setup ();