1 /* Low level interface to Windows debugging, for gdbserver.
2 Copyright (C) 2006, 2007 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 2 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, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
25 #include "gdb/signals.h"
26 #include "mem-break.h"
27 #include "win32-low.h"
33 #include <sys/param.h>
38 #include <sys/cygwin.h>
43 #define OUTMSG(X) do { printf X; fflush (stdout); } while (0)
45 #define OUTMSG2(X) do { printf X; fflush (stdout); } while (0)
47 #define OUTMSG2(X) do ; while (0)
51 #define _T(x) TEXT (x)
55 #define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0]))
59 # define GETPROCADDRESS(DLL, PROC) \
60 ((winapi_ ## PROC) GetProcAddress (DLL, TEXT (#PROC)))
62 # define GETPROCADDRESS(DLL, PROC) \
63 ((winapi_ ## PROC) GetProcAddress (DLL, #PROC))
66 int using_threads
= 1;
69 static HANDLE current_process_handle
= NULL
;
70 static DWORD current_process_id
= 0;
71 static enum target_signal last_sig
= TARGET_SIGNAL_0
;
73 /* The current debug event from WaitForDebugEvent. */
74 static DEBUG_EVENT current_event
;
76 #define NUM_REGS (the_low_target.num_regs)
78 typedef BOOL
WINAPI (*winapi_DebugActiveProcessStop
) (DWORD dwProcessId
);
79 typedef BOOL
WINAPI (*winapi_DebugSetProcessKillOnExit
) (BOOL KillOnExit
);
80 typedef BOOL
WINAPI (*winapi_DebugBreakProcess
) (HANDLE
);
81 typedef BOOL
WINAPI (*winapi_GenerateConsoleCtrlEvent
) (DWORD
, DWORD
);
83 static DWORD main_thread_id
= 0;
85 static void win32_resume (struct thread_resume
*resume_info
);
87 /* Get the thread ID from the current selected inferior (the current
90 current_inferior_tid (void)
92 win32_thread_info
*th
= inferior_target_data (current_inferior
);
96 /* Find a thread record given a thread id. If GET_CONTEXT is set then
97 also retrieve the context for this thread. */
98 static win32_thread_info
*
99 thread_rec (DWORD id
, int get_context
)
101 struct thread_info
*thread
;
102 win32_thread_info
*th
;
104 thread
= (struct thread_info
*) find_inferior_id (&all_threads
, id
);
108 th
= inferior_target_data (thread
);
109 if (!th
->suspend_count
&& get_context
)
111 if (id
!= current_event
.dwThreadId
)
112 th
->suspend_count
= SuspendThread (th
->h
) + 1;
114 (*the_low_target
.get_thread_context
) (th
, ¤t_event
);
120 /* Add a thread to the thread list. */
121 static win32_thread_info
*
122 child_add_thread (DWORD tid
, HANDLE h
)
124 win32_thread_info
*th
;
126 if ((th
= thread_rec (tid
, FALSE
)))
129 th
= (win32_thread_info
*) malloc (sizeof (*th
));
130 memset (th
, 0, sizeof (*th
));
134 add_thread (tid
, th
, (unsigned int) tid
);
135 set_inferior_regcache_data ((struct thread_info
*)
136 find_inferior_id (&all_threads
, tid
),
137 new_register_cache ());
139 if (the_low_target
.thread_added
!= NULL
)
140 (*the_low_target
.thread_added
) (th
);
145 /* Delete a thread from the list of threads. */
147 delete_thread_info (struct inferior_list_entry
*thread
)
149 win32_thread_info
*th
= inferior_target_data ((struct thread_info
*) thread
);
151 remove_thread ((struct thread_info
*) thread
);
156 /* Delete a thread from the list of threads. */
158 child_delete_thread (DWORD id
)
160 struct inferior_list_entry
*thread
;
162 /* If the last thread is exiting, just return. */
163 if (all_threads
.head
== all_threads
.tail
)
166 thread
= find_inferior_id (&all_threads
, id
);
170 delete_thread_info (thread
);
173 /* Transfer memory from/to the debugged process. */
175 child_xfer_memory (CORE_ADDR memaddr
, char *our
, int len
,
176 int write
, struct target_ops
*target
)
179 long addr
= (long) memaddr
;
183 WriteProcessMemory (current_process_handle
, (LPVOID
) addr
,
184 (LPCVOID
) our
, len
, &done
);
185 FlushInstructionCache (current_process_handle
, (LPCVOID
) addr
, len
);
189 ReadProcessMemory (current_process_handle
, (LPCVOID
) addr
, (LPVOID
) our
,
195 /* Generally, what has the program done? */
198 /* The program has exited. The exit status is in value.integer. */
199 TARGET_WAITKIND_EXITED
,
201 /* The program has stopped with a signal. Which signal is in
203 TARGET_WAITKIND_STOPPED
,
205 /* The program is letting us know that it dynamically loaded something
206 (e.g. it called load(2) on AIX). */
207 TARGET_WAITKIND_LOADED
,
209 /* The program has exec'ed a new executable file. The new file's
210 pathname is pointed to by value.execd_pathname. */
211 TARGET_WAITKIND_EXECD
,
213 /* Nothing interesting happened, but we stopped anyway. We take the
214 chance to check if GDB requested an interrupt. */
215 TARGET_WAITKIND_SPURIOUS
,
218 struct target_waitstatus
220 enum target_waitkind kind
;
222 /* Forked child pid, execd pathname, exit status or signal number. */
226 enum target_signal sig
;
228 char *execd_pathname
;
234 /* Clear out any old thread list and reinitialize it to a pristine
237 child_init_thread_list (void)
239 for_each_inferior (&all_threads
, delete_thread_info
);
243 do_initial_child_stuff (DWORD pid
)
245 last_sig
= TARGET_SIGNAL_0
;
247 memset (¤t_event
, 0, sizeof (current_event
));
249 child_init_thread_list ();
251 if (the_low_target
.initial_stuff
!= NULL
)
252 (*the_low_target
.initial_stuff
) ();
255 /* Resume all artificially suspended threads if we are continuing
258 continue_one_thread (struct inferior_list_entry
*this_thread
, void *id_ptr
)
260 struct thread_info
*thread
= (struct thread_info
*) this_thread
;
261 int thread_id
= * (int *) id_ptr
;
262 win32_thread_info
*th
= inferior_target_data (thread
);
265 if ((thread_id
== -1 || thread_id
== th
->tid
)
266 && th
->suspend_count
)
268 if (th
->context
.ContextFlags
)
270 (*the_low_target
.set_thread_context
) (th
, ¤t_event
);
271 th
->context
.ContextFlags
= 0;
274 for (i
= 0; i
< th
->suspend_count
; i
++)
275 (void) ResumeThread (th
->h
);
276 th
->suspend_count
= 0;
283 child_continue (DWORD continue_status
, int thread_id
)
287 res
= ContinueDebugEvent (current_event
.dwProcessId
,
288 current_event
.dwThreadId
, continue_status
);
290 find_inferior (&all_threads
, continue_one_thread
, &thread_id
);
295 /* Fetch register(s) from the current thread context. */
297 child_fetch_inferior_registers (int r
)
300 win32_thread_info
*th
= thread_rec (current_inferior_tid (), TRUE
);
301 if (r
== -1 || r
== 0 || r
> NUM_REGS
)
302 child_fetch_inferior_registers (NUM_REGS
);
304 for (regno
= 0; regno
< r
; regno
++)
305 (*the_low_target
.fetch_inferior_register
) (th
, regno
);
308 /* Store a new register value into the current thread context. We don't
309 change the program's context until later, when we resume it. */
311 child_store_inferior_registers (int r
)
314 win32_thread_info
*th
= thread_rec (current_inferior_tid (), TRUE
);
315 if (r
== -1 || r
== 0 || r
> NUM_REGS
)
316 child_store_inferior_registers (NUM_REGS
);
318 for (regno
= 0; regno
< r
; regno
++)
319 (*the_low_target
.store_inferior_register
) (th
, regno
);
322 /* Map the Windows error number in ERROR to a locale-dependent error
323 message string and return a pointer to it. Typically, the values
324 for ERROR come from GetLastError.
326 The string pointed to shall not be modified by the application,
327 but may be overwritten by a subsequent call to strwinerror
329 The strwinerror function does not change the current setting
333 strwinerror (DWORD error
)
335 static char buf
[1024];
337 DWORD lasterr
= GetLastError ();
338 DWORD chars
= FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
339 | FORMAT_MESSAGE_ALLOCATE_BUFFER
,
342 0, /* Default language */
348 /* If there is an \r\n appended, zap it. */
350 && msgbuf
[chars
- 2] == '\r'
351 && msgbuf
[chars
- 1] == '\n')
357 if (chars
> ((COUNTOF (buf
)) - 1))
359 chars
= COUNTOF (buf
) - 1;
364 wcstombs (buf
, msgbuf
, chars
+ 1);
366 strncpy (buf
, msgbuf
, chars
+ 1);
371 sprintf (buf
, "unknown win32 error (%ld)", error
);
373 SetLastError (lasterr
);
377 /* Start a new process.
378 PROGRAM is a path to the program to execute.
379 ARGS is a standard NULL-terminated array of arguments,
380 to be passed to the inferior as ``argv''.
381 Returns the new PID on success, -1 on failure. Registers the new
382 process with the process list. */
384 win32_create_inferior (char *program
, char **program_args
)
387 char real_path
[MAXPATHLEN
];
388 char *orig_path
, *new_path
, *path_ptr
;
395 PROCESS_INFORMATION pi
;
396 #ifndef __MINGW32CE__
397 STARTUPINFOA si
= { sizeof (STARTUPINFOA
) };
400 wchar_t *wargs
, *wprogram
;
404 error ("No executable specified, specify executable to debug.\n");
406 flags
= DEBUG_PROCESS
| DEBUG_ONLY_THIS_PROCESS
;
410 path_ptr
= getenv ("PATH");
413 orig_path
= alloca (strlen (path_ptr
) + 1);
414 new_path
= alloca (cygwin_posix_to_win32_path_list_buf_size (path_ptr
));
415 strcpy (orig_path
, path_ptr
);
416 cygwin_posix_to_win32_path_list (path_ptr
, new_path
);
417 setenv ("PATH", new_path
, 1);
419 cygwin_conv_to_win32_path (program
, real_path
);
424 for (argc
= 1; program_args
[argc
]; argc
++)
425 argslen
+= strlen (program_args
[argc
]) + 1;
426 args
= alloca (argslen
);
428 for (argc
= 1; program_args
[argc
]; argc
++)
430 /* FIXME: Can we do better about quoting? How does Cygwin
433 strcat (args
, program_args
[argc
]);
435 OUTMSG2 (("Command line is \"%s\"\n", args
));
437 #ifdef CREATE_NEW_PROCESS_GROUP
438 flags
|= CREATE_NEW_PROCESS_GROUP
;
442 to_back_slashes (program
);
443 wargs
= alloca (argslen
* sizeof (wchar_t));
444 mbstowcs (wargs
, args
, argslen
);
445 wprogram
= alloca ((strlen (program
) + 1) * sizeof (wchar_t));
446 mbstowcs (wprogram
, program
, strlen (program
) + 1);
447 ret
= CreateProcessW (wprogram
, /* image name */
448 wargs
, /* command line */
449 NULL
, /* security, not supported */
450 NULL
, /* thread, not supported */
451 FALSE
, /* inherit handles, not supported */
452 flags
, /* start flags */
453 NULL
, /* environment, not supported */
454 NULL
, /* current directory, not supported */
455 NULL
, /* start info, not supported */
456 &pi
); /* proc info */
458 ret
= CreateProcessA (program
, /* image name */
459 args
, /* command line */
462 TRUE
, /* inherit handles */
463 flags
, /* start flags */
464 winenv
, /* environment */
465 NULL
, /* current directory */
466 &si
, /* start info */
467 &pi
); /* proc info */
472 setenv ("PATH", orig_path
, 1);
477 DWORD err
= GetLastError ();
478 error ("Error creating process \"%s%s\", (error %d): %s\n",
479 program
, args
, (int) err
, strwinerror (err
));
483 OUTMSG2 (("Process created: %s\n", (char *) args
));
487 /* On Windows CE this handle can't be closed. The OS reuses
488 it in the debug events, while the 9x/NT versions of Windows
489 probably use a DuplicateHandle'd one. */
490 CloseHandle (pi
.hThread
);
493 current_process_handle
= pi
.hProcess
;
494 current_process_id
= pi
.dwProcessId
;
496 do_initial_child_stuff (current_process_id
);
498 return current_process_id
;
501 /* Attach to a running process.
502 PID is the process ID to attach to, specified by the user
503 or a higher layer. */
505 win32_attach (unsigned long pid
)
507 winapi_DebugActiveProcessStop DebugActiveProcessStop
= NULL
;
508 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
510 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
512 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
514 DebugActiveProcessStop
= GETPROCADDRESS (dll
, DebugActiveProcessStop
);
515 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
517 if (DebugActiveProcess (pid
))
519 if (DebugSetProcessKillOnExit
!= NULL
)
520 DebugSetProcessKillOnExit (FALSE
);
522 current_process_handle
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
524 if (current_process_handle
!= NULL
)
526 current_process_id
= pid
;
527 do_initial_child_stuff (pid
);
530 if (DebugActiveProcessStop
!= NULL
)
531 DebugActiveProcessStop (current_process_id
);
534 error ("Attach to process failed.");
537 /* Handle OUTPUT_DEBUG_STRING_EVENT from child process. */
539 handle_output_debug_string (struct target_waitstatus
*ourstatus
)
541 #define READ_BUFFER_LEN 1024
543 char s
[READ_BUFFER_LEN
+ 1] = { 0 };
544 DWORD nbytes
= current_event
.u
.DebugString
.nDebugStringLength
;
549 if (nbytes
> READ_BUFFER_LEN
)
550 nbytes
= READ_BUFFER_LEN
;
552 addr
= (CORE_ADDR
) (size_t) current_event
.u
.DebugString
.lpDebugStringData
;
554 if (current_event
.u
.DebugString
.fUnicode
)
556 /* The event tells us how many bytes, not chars, even
558 WCHAR buffer
[(READ_BUFFER_LEN
+ 1) / sizeof (WCHAR
)] = { 0 };
559 if (read_inferior_memory (addr
, (unsigned char *) buffer
, nbytes
) != 0)
561 wcstombs (s
, buffer
, (nbytes
+ 1) / sizeof (WCHAR
));
565 if (read_inferior_memory (addr
, (unsigned char *) s
, nbytes
) != 0)
569 if (strncmp (s
, "cYg", 3) != 0)
579 #undef READ_BUFFER_LEN
582 /* Kill all inferiors. */
586 win32_thread_info
*current_thread
;
588 if (current_process_handle
== NULL
)
591 TerminateProcess (current_process_handle
, 0);
594 if (!child_continue (DBG_CONTINUE
, -1))
596 if (!WaitForDebugEvent (¤t_event
, INFINITE
))
598 if (current_event
.dwDebugEventCode
== EXIT_PROCESS_DEBUG_EVENT
)
600 else if (current_event
.dwDebugEventCode
== OUTPUT_DEBUG_STRING_EVENT
)
602 struct target_waitstatus our_status
= { 0 };
603 handle_output_debug_string (&our_status
);
607 CloseHandle (current_process_handle
);
609 current_thread
= inferior_target_data (current_inferior
);
610 if (current_thread
&& current_thread
->h
)
612 /* This may fail in an attached process, so don't check. */
613 (void) CloseHandle (current_thread
->h
);
617 /* Detach from all inferiors. */
623 winapi_DebugActiveProcessStop DebugActiveProcessStop
= NULL
;
624 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit
= NULL
;
626 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
628 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
630 DebugActiveProcessStop
= GETPROCADDRESS (dll
, DebugActiveProcessStop
);
631 DebugSetProcessKillOnExit
= GETPROCADDRESS (dll
, DebugSetProcessKillOnExit
);
633 if (DebugSetProcessKillOnExit
== NULL
634 || DebugActiveProcessStop
== NULL
)
637 /* We need a new handle, since DebugActiveProcessStop
638 closes all the ones that came through the events. */
639 if ((h
= OpenProcess (PROCESS_ALL_ACCESS
,
641 current_process_id
)) == NULL
)
643 /* The process died. */
648 struct thread_resume resume
;
652 resume
.leave_stopped
= 0;
653 win32_resume (&resume
);
656 if (!DebugActiveProcessStop (current_process_id
))
661 DebugSetProcessKillOnExit (FALSE
);
663 current_process_handle
= h
;
667 /* Wait for inferiors to end. */
671 if (current_process_id
== 0
672 || current_process_handle
== NULL
)
675 WaitForSingleObject (current_process_handle
, INFINITE
);
676 CloseHandle (current_process_handle
);
678 current_process_handle
= NULL
;
679 current_process_id
= 0;
682 /* Return 1 iff the thread with thread ID TID is alive. */
684 win32_thread_alive (unsigned long tid
)
688 /* Our thread list is reliable; don't bother to poll target
690 if (find_inferior_id (&all_threads
, tid
) != NULL
)
697 /* Resume the inferior process. RESUME_INFO describes how we want
700 win32_resume (struct thread_resume
*resume_info
)
703 enum target_signal sig
;
705 win32_thread_info
*th
;
706 DWORD continue_status
= DBG_CONTINUE
;
708 /* This handles the very limited set of resume packets that GDB can
709 currently produce. */
711 if (resume_info
[0].thread
== -1)
713 else if (resume_info
[1].thread
== -1 && !resume_info
[1].leave_stopped
)
716 /* Yes, we're ignoring resume_info[0].thread. It'd be tricky to make
717 the Windows resume code do the right thing for thread switching. */
718 tid
= current_event
.dwThreadId
;
720 if (resume_info
[0].thread
!= -1)
722 sig
= resume_info
[0].sig
;
723 step
= resume_info
[0].step
;
731 if (sig
!= TARGET_SIGNAL_0
)
733 if (current_event
.dwDebugEventCode
!= EXCEPTION_DEBUG_EVENT
)
735 OUTMSG (("Cannot continue with signal %d here.\n", sig
));
737 else if (sig
== last_sig
)
738 continue_status
= DBG_EXCEPTION_NOT_HANDLED
;
740 OUTMSG (("Can only continue with recieved signal %d.\n", last_sig
));
743 last_sig
= TARGET_SIGNAL_0
;
745 /* Get context for the currently selected thread. */
746 th
= thread_rec (current_event
.dwThreadId
, FALSE
);
749 if (th
->context
.ContextFlags
)
751 /* Move register values from the inferior into the thread
752 context structure. */
753 regcache_invalidate ();
757 if (the_low_target
.single_step
!= NULL
)
758 (*the_low_target
.single_step
) (th
);
760 error ("Single stepping is not supported "
761 "in this configuration.\n");
764 (*the_low_target
.set_thread_context
) (th
, ¤t_event
);
765 th
->context
.ContextFlags
= 0;
769 /* Allow continuing with the same signal that interrupted us.
770 Otherwise complain. */
772 child_continue (continue_status
, tid
);
776 handle_exception (struct target_waitstatus
*ourstatus
)
778 DWORD code
= current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
;
780 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
784 case EXCEPTION_ACCESS_VIOLATION
:
785 OUTMSG2 (("EXCEPTION_ACCESS_VIOLATION"));
786 ourstatus
->value
.sig
= TARGET_SIGNAL_SEGV
;
788 case STATUS_STACK_OVERFLOW
:
789 OUTMSG2 (("STATUS_STACK_OVERFLOW"));
790 ourstatus
->value
.sig
= TARGET_SIGNAL_SEGV
;
792 case STATUS_FLOAT_DENORMAL_OPERAND
:
793 OUTMSG2 (("STATUS_FLOAT_DENORMAL_OPERAND"));
794 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
796 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
797 OUTMSG2 (("EXCEPTION_ARRAY_BOUNDS_EXCEEDED"));
798 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
800 case STATUS_FLOAT_INEXACT_RESULT
:
801 OUTMSG2 (("STATUS_FLOAT_INEXACT_RESULT"));
802 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
804 case STATUS_FLOAT_INVALID_OPERATION
:
805 OUTMSG2 (("STATUS_FLOAT_INVALID_OPERATION"));
806 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
808 case STATUS_FLOAT_OVERFLOW
:
809 OUTMSG2 (("STATUS_FLOAT_OVERFLOW"));
810 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
812 case STATUS_FLOAT_STACK_CHECK
:
813 OUTMSG2 (("STATUS_FLOAT_STACK_CHECK"));
814 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
816 case STATUS_FLOAT_UNDERFLOW
:
817 OUTMSG2 (("STATUS_FLOAT_UNDERFLOW"));
818 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
820 case STATUS_FLOAT_DIVIDE_BY_ZERO
:
821 OUTMSG2 (("STATUS_FLOAT_DIVIDE_BY_ZERO"));
822 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
824 case STATUS_INTEGER_DIVIDE_BY_ZERO
:
825 OUTMSG2 (("STATUS_INTEGER_DIVIDE_BY_ZERO"));
826 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
828 case STATUS_INTEGER_OVERFLOW
:
829 OUTMSG2 (("STATUS_INTEGER_OVERFLOW"));
830 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
832 case EXCEPTION_BREAKPOINT
:
833 OUTMSG2 (("EXCEPTION_BREAKPOINT"));
834 ourstatus
->value
.sig
= TARGET_SIGNAL_TRAP
;
836 /* Remove the initial breakpoint. */
837 check_breakpoints ((CORE_ADDR
) (long) current_event
838 .u
.Exception
.ExceptionRecord
.ExceptionAddress
);
842 OUTMSG2 (("DBG_CONTROL_C"));
843 ourstatus
->value
.sig
= TARGET_SIGNAL_INT
;
845 case DBG_CONTROL_BREAK
:
846 OUTMSG2 (("DBG_CONTROL_BREAK"));
847 ourstatus
->value
.sig
= TARGET_SIGNAL_INT
;
849 case EXCEPTION_SINGLE_STEP
:
850 OUTMSG2 (("EXCEPTION_SINGLE_STEP"));
851 ourstatus
->value
.sig
= TARGET_SIGNAL_TRAP
;
853 case EXCEPTION_ILLEGAL_INSTRUCTION
:
854 OUTMSG2 (("EXCEPTION_ILLEGAL_INSTRUCTION"));
855 ourstatus
->value
.sig
= TARGET_SIGNAL_ILL
;
857 case EXCEPTION_PRIV_INSTRUCTION
:
858 OUTMSG2 (("EXCEPTION_PRIV_INSTRUCTION"));
859 ourstatus
->value
.sig
= TARGET_SIGNAL_ILL
;
861 case EXCEPTION_NONCONTINUABLE_EXCEPTION
:
862 OUTMSG2 (("EXCEPTION_NONCONTINUABLE_EXCEPTION"));
863 ourstatus
->value
.sig
= TARGET_SIGNAL_ILL
;
866 if (current_event
.u
.Exception
.dwFirstChance
)
868 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
871 OUTMSG2 (("gdbserver: unknown target exception 0x%08lx at 0x%08lx",
872 current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
,
873 (DWORD
) current_event
.u
.Exception
.ExceptionRecord
.
875 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
879 last_sig
= ourstatus
->value
.sig
;
882 /* Get the next event from the child. */
884 get_child_debug_event (struct target_waitstatus
*ourstatus
)
888 last_sig
= TARGET_SIGNAL_0
;
889 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
891 /* Keep the wait time low enough for confortable remote interruption,
892 but high enough so gdbserver doesn't become a bottleneck. */
893 if (!(debug_event
= WaitForDebugEvent (¤t_event
, 250)))
897 (struct thread_info
*) find_inferior_id (&all_threads
,
898 current_event
.dwThreadId
);
900 switch (current_event
.dwDebugEventCode
)
902 case CREATE_THREAD_DEBUG_EVENT
:
903 OUTMSG2 (("gdbserver: kernel event CREATE_THREAD_DEBUG_EVENT "
904 "for pid=%d tid=%x)\n",
905 (unsigned) current_event
.dwProcessId
,
906 (unsigned) current_event
.dwThreadId
));
908 /* Record the existence of this thread. */
909 child_add_thread (current_event
.dwThreadId
,
910 current_event
.u
.CreateThread
.hThread
);
913 case EXIT_THREAD_DEBUG_EVENT
:
914 OUTMSG2 (("gdbserver: kernel event EXIT_THREAD_DEBUG_EVENT "
915 "for pid=%d tid=%x\n",
916 (unsigned) current_event
.dwProcessId
,
917 (unsigned) current_event
.dwThreadId
));
918 child_delete_thread (current_event
.dwThreadId
);
921 case CREATE_PROCESS_DEBUG_EVENT
:
922 OUTMSG2 (("gdbserver: kernel event CREATE_PROCESS_DEBUG_EVENT "
923 "for pid=%d tid=%x\n",
924 (unsigned) current_event
.dwProcessId
,
925 (unsigned) current_event
.dwThreadId
));
926 CloseHandle (current_event
.u
.CreateProcessInfo
.hFile
);
928 current_process_handle
= current_event
.u
.CreateProcessInfo
.hProcess
;
929 main_thread_id
= current_event
.dwThreadId
;
931 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
932 ourstatus
->value
.execd_pathname
= "Main executable";
934 /* Add the main thread. */
935 child_add_thread (main_thread_id
,
936 current_event
.u
.CreateProcessInfo
.hThread
);
938 ourstatus
->value
.related_pid
= current_event
.dwThreadId
;
940 /* Windows CE doesn't set the initial breakpoint automatically
941 like the desktop versions of Windows do. We add it explicitly
942 here. It will be removed as soon as it is hit. */
943 set_breakpoint_at ((CORE_ADDR
) (long) current_event
.u
944 .CreateProcessInfo
.lpStartAddress
,
945 delete_breakpoint_at
);
949 case EXIT_PROCESS_DEBUG_EVENT
:
950 OUTMSG2 (("gdbserver: kernel event EXIT_PROCESS_DEBUG_EVENT "
951 "for pid=%d tid=%x\n",
952 (unsigned) current_event
.dwProcessId
,
953 (unsigned) current_event
.dwThreadId
));
954 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
955 ourstatus
->value
.integer
= current_event
.u
.ExitProcess
.dwExitCode
;
956 CloseHandle (current_process_handle
);
957 current_process_handle
= NULL
;
960 case LOAD_DLL_DEBUG_EVENT
:
961 OUTMSG2 (("gdbserver: kernel event LOAD_DLL_DEBUG_EVENT "
962 "for pid=%d tid=%x\n",
963 (unsigned) current_event
.dwProcessId
,
964 (unsigned) current_event
.dwThreadId
));
965 CloseHandle (current_event
.u
.LoadDll
.hFile
);
967 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
968 ourstatus
->value
.integer
= 0;
971 case UNLOAD_DLL_DEBUG_EVENT
:
972 OUTMSG2 (("gdbserver: kernel event UNLOAD_DLL_DEBUG_EVENT "
973 "for pid=%d tid=%x\n",
974 (unsigned) current_event
.dwProcessId
,
975 (unsigned) current_event
.dwThreadId
));
978 case EXCEPTION_DEBUG_EVENT
:
979 OUTMSG2 (("gdbserver: kernel event EXCEPTION_DEBUG_EVENT "
980 "for pid=%d tid=%x\n",
981 (unsigned) current_event
.dwProcessId
,
982 (unsigned) current_event
.dwThreadId
));
983 handle_exception (ourstatus
);
986 case OUTPUT_DEBUG_STRING_EVENT
:
987 /* A message from the kernel (or Cygwin). */
988 OUTMSG2 (("gdbserver: kernel event OUTPUT_DEBUG_STRING_EVENT "
989 "for pid=%d tid=%x\n",
990 (unsigned) current_event
.dwProcessId
,
991 (unsigned) current_event
.dwThreadId
));
992 handle_output_debug_string (ourstatus
);
996 OUTMSG2 (("gdbserver: kernel event unknown "
997 "for pid=%d tid=%x code=%ld\n",
998 (unsigned) current_event
.dwProcessId
,
999 (unsigned) current_event
.dwThreadId
,
1000 current_event
.dwDebugEventCode
));
1005 (struct thread_info
*) find_inferior_id (&all_threads
,
1006 current_event
.dwThreadId
);
1009 /* Wait for the inferior process to change state.
1010 STATUS will be filled in with a response code to send to GDB.
1011 Returns the signal which caused the process to stop. */
1012 static unsigned char
1013 win32_wait (char *status
)
1015 struct target_waitstatus our_status
;
1021 /* Check if GDB sent us an interrupt request. */
1022 check_remote_input_interrupt_request ();
1024 get_child_debug_event (&our_status
);
1026 switch (our_status
.kind
)
1028 case TARGET_WAITKIND_EXITED
:
1029 OUTMSG2 (("Child exited with retcode = %x\n",
1030 our_status
.value
.integer
));
1034 child_fetch_inferior_registers (-1);
1036 return our_status
.value
.integer
;
1037 case TARGET_WAITKIND_STOPPED
:
1038 OUTMSG2 (("Child Stopped with signal = %d \n",
1039 our_status
.value
.sig
));
1043 child_fetch_inferior_registers (-1);
1045 return our_status
.value
.sig
;
1047 OUTMSG (("Ignoring unknown internal event, %d\n", our_status
.kind
));
1049 case TARGET_WAITKIND_SPURIOUS
:
1050 case TARGET_WAITKIND_LOADED
:
1051 case TARGET_WAITKIND_EXECD
:
1052 /* do nothing, just continue */
1053 child_continue (DBG_CONTINUE
, -1);
1059 /* Fetch registers from the inferior process.
1060 If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO. */
1062 win32_fetch_inferior_registers (int regno
)
1064 child_fetch_inferior_registers (regno
);
1067 /* Store registers to the inferior process.
1068 If REGNO is -1, store all registers; otherwise, store at least REGNO. */
1070 win32_store_inferior_registers (int regno
)
1072 child_store_inferior_registers (regno
);
1075 /* Read memory from the inferior process. This should generally be
1076 called through read_inferior_memory, which handles breakpoint shadowing.
1077 Read LEN bytes at MEMADDR into a buffer at MYADDR. */
1079 win32_read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
1081 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 0, 0) != len
;
1084 /* Write memory to the inferior process. This should generally be
1085 called through write_inferior_memory, which handles breakpoint shadowing.
1086 Write LEN bytes from the buffer at MYADDR to MEMADDR.
1087 Returns 0 on success and errno on failure. */
1089 win32_write_inferior_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
,
1092 return child_xfer_memory (memaddr
, (char *) myaddr
, len
, 1, 0) != len
;
1095 /* Send an interrupt request to the inferior process. */
1097 win32_request_interrupt (void)
1099 winapi_DebugBreakProcess DebugBreakProcess
;
1100 winapi_GenerateConsoleCtrlEvent GenerateConsoleCtrlEvent
;
1103 HMODULE dll
= GetModuleHandle (_T("COREDLL.DLL"));
1105 HMODULE dll
= GetModuleHandle (_T("KERNEL32.DLL"));
1108 GenerateConsoleCtrlEvent
= GETPROCADDRESS (dll
, GenerateConsoleCtrlEvent
);
1110 if (GenerateConsoleCtrlEvent
!= NULL
1111 && GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT
, current_process_id
))
1114 /* GenerateConsoleCtrlEvent can fail if process id being debugged is
1115 not a process group id.
1116 Fallback to XP/Vista 'DebugBreakProcess', which generates a
1117 breakpoint exception in the interior process. */
1119 DebugBreakProcess
= GETPROCADDRESS (dll
, DebugBreakProcess
);
1121 if (DebugBreakProcess
!= NULL
1122 && DebugBreakProcess (current_process_handle
))
1125 OUTMSG (("Could not interrupt process.\n"));
1129 win32_arch_string (void)
1131 return the_low_target
.arch_string
;
1134 static struct target_ops win32_target_ops
= {
1135 win32_create_inferior
,
1143 win32_fetch_inferior_registers
,
1144 win32_store_inferior_registers
,
1145 win32_read_inferior_memory
,
1146 win32_write_inferior_memory
,
1148 win32_request_interrupt
,
1159 /* Initialize the Win32 backend. */
1161 initialize_low (void)
1163 set_target_ops (&win32_target_ops
);
1164 if (the_low_target
.breakpoint
!= NULL
)
1165 set_breakpoint_data (the_low_target
.breakpoint
,
1166 the_low_target
.breakpoint_len
);