* win32-low.c (handle_output_debug_string): Ignore event if not
[deliverable/binutils-gdb.git] / gdb / gdbserver / win32-low.c
1 /* Low level interface to Windows debugging, for gdbserver.
2 Copyright (C) 2006, 2007 Free Software Foundation, Inc.
3
4 Contributed by Leo Zayas. Based on "win32-nat.c" from GDB.
5
6 This file is part of GDB.
7
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.
12
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.
17
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. */
22
23 #include "server.h"
24 #include "regcache.h"
25 #include "gdb/signals.h"
26 #include "mem-break.h"
27 #include "win32-low.h"
28
29 #include <windows.h>
30 #include <winnt.h>
31 #include <imagehlp.h>
32 #include <psapi.h>
33 #include <sys/param.h>
34 #include <malloc.h>
35 #include <process.h>
36
37 #ifndef USE_WIN32API
38 #include <sys/cygwin.h>
39 #endif
40
41 #define LOG 0
42
43 #define OUTMSG(X) do { printf X; fflush (stdout); } while (0)
44 #if LOG
45 #define OUTMSG2(X) do { printf X; fflush (stdout); } while (0)
46 #else
47 #define OUTMSG2(X) do ; while (0)
48 #endif
49
50 #ifndef _T
51 #define _T(x) TEXT (x)
52 #endif
53
54 #ifndef COUNTOF
55 #define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0]))
56 #endif
57
58 #ifdef _WIN32_WCE
59 # define GETPROCADDRESS(DLL, PROC) \
60 ((winapi_ ## PROC) GetProcAddress (DLL, TEXT (#PROC)))
61 #else
62 # define GETPROCADDRESS(DLL, PROC) \
63 ((winapi_ ## PROC) GetProcAddress (DLL, #PROC))
64 #endif
65
66 int using_threads = 1;
67
68 /* Globals. */
69 static HANDLE current_process_handle = NULL;
70 static DWORD current_process_id = 0;
71 static enum target_signal last_sig = TARGET_SIGNAL_0;
72
73 /* The current debug event from WaitForDebugEvent. */
74 static DEBUG_EVENT current_event;
75
76 #define NUM_REGS (the_low_target.num_regs)
77
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);
82
83 static DWORD main_thread_id = 0;
84
85 static void win32_resume (struct thread_resume *resume_info);
86
87 /* Get the thread ID from the current selected inferior (the current
88 thread). */
89 static DWORD
90 current_inferior_tid (void)
91 {
92 win32_thread_info *th = inferior_target_data (current_inferior);
93 return th->tid;
94 }
95
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)
100 {
101 struct thread_info *thread;
102 win32_thread_info *th;
103
104 thread = (struct thread_info *) find_inferior_id (&all_threads, id);
105 if (thread == NULL)
106 return NULL;
107
108 th = inferior_target_data (thread);
109 if (!th->suspend_count && get_context)
110 {
111 if (id != current_event.dwThreadId)
112 th->suspend_count = SuspendThread (th->h) + 1;
113
114 (*the_low_target.get_thread_context) (th, &current_event);
115 }
116
117 return th;
118 }
119
120 /* Add a thread to the thread list. */
121 static win32_thread_info *
122 child_add_thread (DWORD tid, HANDLE h)
123 {
124 win32_thread_info *th;
125
126 if ((th = thread_rec (tid, FALSE)))
127 return th;
128
129 th = (win32_thread_info *) malloc (sizeof (*th));
130 memset (th, 0, sizeof (*th));
131 th->tid = tid;
132 th->h = h;
133
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 ());
138
139 if (the_low_target.thread_added != NULL)
140 (*the_low_target.thread_added) (th);
141
142 return th;
143 }
144
145 /* Delete a thread from the list of threads. */
146 static void
147 delete_thread_info (struct inferior_list_entry *thread)
148 {
149 win32_thread_info *th = inferior_target_data ((struct thread_info *) thread);
150
151 remove_thread ((struct thread_info *) thread);
152 CloseHandle (th->h);
153 free (th);
154 }
155
156 /* Delete a thread from the list of threads. */
157 static void
158 child_delete_thread (DWORD id)
159 {
160 struct inferior_list_entry *thread;
161
162 /* If the last thread is exiting, just return. */
163 if (all_threads.head == all_threads.tail)
164 return;
165
166 thread = find_inferior_id (&all_threads, id);
167 if (thread == NULL)
168 return;
169
170 delete_thread_info (thread);
171 }
172
173 /* Transfer memory from/to the debugged process. */
174 static int
175 child_xfer_memory (CORE_ADDR memaddr, char *our, int len,
176 int write, struct target_ops *target)
177 {
178 SIZE_T done;
179 long addr = (long) memaddr;
180
181 if (write)
182 {
183 WriteProcessMemory (current_process_handle, (LPVOID) addr,
184 (LPCVOID) our, len, &done);
185 FlushInstructionCache (current_process_handle, (LPCVOID) addr, len);
186 }
187 else
188 {
189 ReadProcessMemory (current_process_handle, (LPCVOID) addr, (LPVOID) our,
190 len, &done);
191 }
192 return done;
193 }
194
195 /* Generally, what has the program done? */
196 enum target_waitkind
197 {
198 /* The program has exited. The exit status is in value.integer. */
199 TARGET_WAITKIND_EXITED,
200
201 /* The program has stopped with a signal. Which signal is in
202 value.sig. */
203 TARGET_WAITKIND_STOPPED,
204
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,
208
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,
212
213 /* Nothing interesting happened, but we stopped anyway. We take the
214 chance to check if GDB requested an interrupt. */
215 TARGET_WAITKIND_SPURIOUS,
216 };
217
218 struct target_waitstatus
219 {
220 enum target_waitkind kind;
221
222 /* Forked child pid, execd pathname, exit status or signal number. */
223 union
224 {
225 int integer;
226 enum target_signal sig;
227 int related_pid;
228 char *execd_pathname;
229 int syscall_id;
230 }
231 value;
232 };
233
234 /* Clear out any old thread list and reinitialize it to a pristine
235 state. */
236 static void
237 child_init_thread_list (void)
238 {
239 for_each_inferior (&all_threads, delete_thread_info);
240 }
241
242 static void
243 do_initial_child_stuff (DWORD pid)
244 {
245 last_sig = TARGET_SIGNAL_0;
246
247 memset (&current_event, 0, sizeof (current_event));
248
249 child_init_thread_list ();
250
251 if (the_low_target.initial_stuff != NULL)
252 (*the_low_target.initial_stuff) ();
253 }
254
255 /* Resume all artificially suspended threads if we are continuing
256 execution. */
257 static int
258 continue_one_thread (struct inferior_list_entry *this_thread, void *id_ptr)
259 {
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);
263 int i;
264
265 if ((thread_id == -1 || thread_id == th->tid)
266 && th->suspend_count)
267 {
268 if (th->context.ContextFlags)
269 {
270 (*the_low_target.set_thread_context) (th, &current_event);
271 th->context.ContextFlags = 0;
272 }
273
274 for (i = 0; i < th->suspend_count; i++)
275 (void) ResumeThread (th->h);
276 th->suspend_count = 0;
277 }
278
279 return 0;
280 }
281
282 static BOOL
283 child_continue (DWORD continue_status, int thread_id)
284 {
285 BOOL res;
286
287 res = ContinueDebugEvent (current_event.dwProcessId,
288 current_event.dwThreadId, continue_status);
289 if (res)
290 find_inferior (&all_threads, continue_one_thread, &thread_id);
291
292 return res;
293 }
294
295 /* Fetch register(s) from the current thread context. */
296 static void
297 child_fetch_inferior_registers (int r)
298 {
299 int regno;
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);
303 else
304 for (regno = 0; regno < r; regno++)
305 (*the_low_target.fetch_inferior_register) (th, regno);
306 }
307
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. */
310 static void
311 child_store_inferior_registers (int r)
312 {
313 int regno;
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);
317 else
318 for (regno = 0; regno < r; regno++)
319 (*the_low_target.store_inferior_register) (th, regno);
320 }
321
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.
325
326 The string pointed to shall not be modified by the application,
327 but may be overwritten by a subsequent call to strwinerror
328
329 The strwinerror function does not change the current setting
330 of GetLastError. */
331
332 char *
333 strwinerror (DWORD error)
334 {
335 static char buf[1024];
336 TCHAR *msgbuf;
337 DWORD lasterr = GetLastError ();
338 DWORD chars = FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
339 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
340 NULL,
341 error,
342 0, /* Default language */
343 (LPVOID)&msgbuf,
344 0,
345 NULL);
346 if (chars != 0)
347 {
348 /* If there is an \r\n appended, zap it. */
349 if (chars >= 2
350 && msgbuf[chars - 2] == '\r'
351 && msgbuf[chars - 1] == '\n')
352 {
353 chars -= 2;
354 msgbuf[chars] = 0;
355 }
356
357 if (chars > ((COUNTOF (buf)) - 1))
358 {
359 chars = COUNTOF (buf) - 1;
360 msgbuf [chars] = 0;
361 }
362
363 #ifdef UNICODE
364 wcstombs (buf, msgbuf, chars + 1);
365 #else
366 strncpy (buf, msgbuf, chars + 1);
367 #endif
368 LocalFree (msgbuf);
369 }
370 else
371 sprintf (buf, "unknown win32 error (%ld)", error);
372
373 SetLastError (lasterr);
374 return buf;
375 }
376
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. */
383 static int
384 win32_create_inferior (char *program, char **program_args)
385 {
386 #ifndef USE_WIN32API
387 char real_path[MAXPATHLEN];
388 char *orig_path, *new_path, *path_ptr;
389 #endif
390 BOOL ret;
391 DWORD flags;
392 char *args;
393 int argslen;
394 int argc;
395 PROCESS_INFORMATION pi;
396 #ifndef __MINGW32CE__
397 STARTUPINFOA si = { sizeof (STARTUPINFOA) };
398 char *winenv = NULL;
399 #else
400 wchar_t *wargs, *wprogram;
401 #endif
402
403 if (!program)
404 error ("No executable specified, specify executable to debug.\n");
405
406 flags = DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS;
407
408 #ifndef USE_WIN32API
409 orig_path = NULL;
410 path_ptr = getenv ("PATH");
411 if (path_ptr)
412 {
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);
418 }
419 cygwin_conv_to_win32_path (program, real_path);
420 program = real_path;
421 #endif
422
423 argslen = 1;
424 for (argc = 1; program_args[argc]; argc++)
425 argslen += strlen (program_args[argc]) + 1;
426 args = alloca (argslen);
427 args[0] = '\0';
428 for (argc = 1; program_args[argc]; argc++)
429 {
430 /* FIXME: Can we do better about quoting? How does Cygwin
431 handle this? */
432 strcat (args, " ");
433 strcat (args, program_args[argc]);
434 }
435 OUTMSG2 (("Command line is \"%s\"\n", args));
436
437 #ifdef CREATE_NEW_PROCESS_GROUP
438 flags |= CREATE_NEW_PROCESS_GROUP;
439 #endif
440
441 #ifdef __MINGW32CE__
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 */
457 #else
458 ret = CreateProcessA (program, /* image name */
459 args, /* command line */
460 NULL, /* security */
461 NULL, /* thread */
462 TRUE, /* inherit handles */
463 flags, /* start flags */
464 winenv, /* environment */
465 NULL, /* current directory */
466 &si, /* start info */
467 &pi); /* proc info */
468 #endif
469
470 #ifndef USE_WIN32API
471 if (orig_path)
472 setenv ("PATH", orig_path, 1);
473 #endif
474
475 if (!ret)
476 {
477 DWORD err = GetLastError ();
478 error ("Error creating process \"%s%s\", (error %d): %s\n",
479 program, args, (int) err, strwinerror (err));
480 }
481 else
482 {
483 OUTMSG2 (("Process created: %s\n", (char *) args));
484 }
485
486 #ifndef _WIN32_WCE
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);
491 #endif
492
493 current_process_handle = pi.hProcess;
494 current_process_id = pi.dwProcessId;
495
496 do_initial_child_stuff (current_process_id);
497
498 return current_process_id;
499 }
500
501 /* Attach to a running process.
502 PID is the process ID to attach to, specified by the user
503 or a higher layer. */
504 static int
505 win32_attach (unsigned long pid)
506 {
507 winapi_DebugActiveProcessStop DebugActiveProcessStop = NULL;
508 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit = NULL;
509 #ifdef _WIN32_WCE
510 HMODULE dll = GetModuleHandle (_T("COREDLL.DLL"));
511 #else
512 HMODULE dll = GetModuleHandle (_T("KERNEL32.DLL"));
513 #endif
514 DebugActiveProcessStop = GETPROCADDRESS (dll, DebugActiveProcessStop);
515 DebugSetProcessKillOnExit = GETPROCADDRESS (dll, DebugSetProcessKillOnExit);
516
517 if (DebugActiveProcess (pid))
518 {
519 if (DebugSetProcessKillOnExit != NULL)
520 DebugSetProcessKillOnExit (FALSE);
521
522 current_process_handle = OpenProcess (PROCESS_ALL_ACCESS, FALSE, pid);
523
524 if (current_process_handle != NULL)
525 {
526 current_process_id = pid;
527 do_initial_child_stuff (pid);
528 return 0;
529 }
530 if (DebugActiveProcessStop != NULL)
531 DebugActiveProcessStop (current_process_id);
532 }
533
534 error ("Attach to process failed.");
535 }
536
537 /* Handle OUTPUT_DEBUG_STRING_EVENT from child process. */
538 static void
539 handle_output_debug_string (struct target_waitstatus *ourstatus)
540 {
541 #define READ_BUFFER_LEN 1024
542 CORE_ADDR addr;
543 char s[READ_BUFFER_LEN + 1] = { 0 };
544 DWORD nbytes = current_event.u.DebugString.nDebugStringLength;
545
546 if (nbytes == 0)
547 return;
548
549 if (nbytes > READ_BUFFER_LEN)
550 nbytes = READ_BUFFER_LEN;
551
552 addr = (CORE_ADDR) (size_t) current_event.u.DebugString.lpDebugStringData;
553
554 if (current_event.u.DebugString.fUnicode)
555 {
556 /* The event tells us how many bytes, not chars, even
557 in Unicode. */
558 WCHAR buffer[(READ_BUFFER_LEN + 1) / sizeof (WCHAR)] = { 0 };
559 if (read_inferior_memory (addr, (unsigned char *) buffer, nbytes) != 0)
560 return;
561 wcstombs (s, buffer, (nbytes + 1) / sizeof (WCHAR));
562 }
563 else
564 {
565 if (read_inferior_memory (addr, (unsigned char *) s, nbytes) != 0)
566 return;
567 }
568
569 if (strncmp (s, "cYg", 3) != 0)
570 {
571 if (!server_waiting)
572 {
573 OUTMSG2(("%s", s));
574 return;
575 }
576
577 monitor_output (s);
578 }
579 #undef READ_BUFFER_LEN
580 }
581
582 /* Kill all inferiors. */
583 static void
584 win32_kill (void)
585 {
586 win32_thread_info *current_thread;
587
588 if (current_process_handle == NULL)
589 return;
590
591 TerminateProcess (current_process_handle, 0);
592 for (;;)
593 {
594 if (!child_continue (DBG_CONTINUE, -1))
595 break;
596 if (!WaitForDebugEvent (&current_event, INFINITE))
597 break;
598 if (current_event.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT)
599 break;
600 else if (current_event.dwDebugEventCode == OUTPUT_DEBUG_STRING_EVENT)
601 {
602 struct target_waitstatus our_status = { 0 };
603 handle_output_debug_string (&our_status);
604 }
605 }
606
607 CloseHandle (current_process_handle);
608
609 current_thread = inferior_target_data (current_inferior);
610 if (current_thread && current_thread->h)
611 {
612 /* This may fail in an attached process, so don't check. */
613 (void) CloseHandle (current_thread->h);
614 }
615 }
616
617 /* Detach from all inferiors. */
618 static int
619 win32_detach (void)
620 {
621 HANDLE h;
622
623 winapi_DebugActiveProcessStop DebugActiveProcessStop = NULL;
624 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit = NULL;
625 #ifdef _WIN32_WCE
626 HMODULE dll = GetModuleHandle (_T("COREDLL.DLL"));
627 #else
628 HMODULE dll = GetModuleHandle (_T("KERNEL32.DLL"));
629 #endif
630 DebugActiveProcessStop = GETPROCADDRESS (dll, DebugActiveProcessStop);
631 DebugSetProcessKillOnExit = GETPROCADDRESS (dll, DebugSetProcessKillOnExit);
632
633 if (DebugSetProcessKillOnExit == NULL
634 || DebugActiveProcessStop == NULL)
635 return -1;
636
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,
640 FALSE,
641 current_process_id)) == NULL)
642 {
643 /* The process died. */
644 return -1;
645 }
646
647 {
648 struct thread_resume resume;
649 resume.thread = -1;
650 resume.step = 0;
651 resume.sig = 0;
652 resume.leave_stopped = 0;
653 win32_resume (&resume);
654 }
655
656 if (!DebugActiveProcessStop (current_process_id))
657 {
658 CloseHandle (h);
659 return -1;
660 }
661 DebugSetProcessKillOnExit (FALSE);
662
663 current_process_handle = h;
664 return 0;
665 }
666
667 /* Wait for inferiors to end. */
668 static void
669 win32_join (void)
670 {
671 if (current_process_id == 0
672 || current_process_handle == NULL)
673 return;
674
675 WaitForSingleObject (current_process_handle, INFINITE);
676 CloseHandle (current_process_handle);
677
678 current_process_handle = NULL;
679 current_process_id = 0;
680 }
681
682 /* Return 1 iff the thread with thread ID TID is alive. */
683 static int
684 win32_thread_alive (unsigned long tid)
685 {
686 int res;
687
688 /* Our thread list is reliable; don't bother to poll target
689 threads. */
690 if (find_inferior_id (&all_threads, tid) != NULL)
691 res = 1;
692 else
693 res = 0;
694 return res;
695 }
696
697 /* Resume the inferior process. RESUME_INFO describes how we want
698 to resume. */
699 static void
700 win32_resume (struct thread_resume *resume_info)
701 {
702 DWORD tid;
703 enum target_signal sig;
704 int step;
705 win32_thread_info *th;
706 DWORD continue_status = DBG_CONTINUE;
707
708 /* This handles the very limited set of resume packets that GDB can
709 currently produce. */
710
711 if (resume_info[0].thread == -1)
712 tid = -1;
713 else if (resume_info[1].thread == -1 && !resume_info[1].leave_stopped)
714 tid = -1;
715 else
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;
719
720 if (resume_info[0].thread != -1)
721 {
722 sig = resume_info[0].sig;
723 step = resume_info[0].step;
724 }
725 else
726 {
727 sig = 0;
728 step = 0;
729 }
730
731 if (sig != TARGET_SIGNAL_0)
732 {
733 if (current_event.dwDebugEventCode != EXCEPTION_DEBUG_EVENT)
734 {
735 OUTMSG (("Cannot continue with signal %d here.\n", sig));
736 }
737 else if (sig == last_sig)
738 continue_status = DBG_EXCEPTION_NOT_HANDLED;
739 else
740 OUTMSG (("Can only continue with recieved signal %d.\n", last_sig));
741 }
742
743 last_sig = TARGET_SIGNAL_0;
744
745 /* Get context for the currently selected thread. */
746 th = thread_rec (current_event.dwThreadId, FALSE);
747 if (th)
748 {
749 if (th->context.ContextFlags)
750 {
751 /* Move register values from the inferior into the thread
752 context structure. */
753 regcache_invalidate ();
754
755 if (step)
756 {
757 if (the_low_target.single_step != NULL)
758 (*the_low_target.single_step) (th);
759 else
760 error ("Single stepping is not supported "
761 "in this configuration.\n");
762 }
763
764 (*the_low_target.set_thread_context) (th, &current_event);
765 th->context.ContextFlags = 0;
766 }
767 }
768
769 /* Allow continuing with the same signal that interrupted us.
770 Otherwise complain. */
771
772 child_continue (continue_status, tid);
773 }
774
775 static void
776 handle_exception (struct target_waitstatus *ourstatus)
777 {
778 DWORD code = current_event.u.Exception.ExceptionRecord.ExceptionCode;
779
780 ourstatus->kind = TARGET_WAITKIND_STOPPED;
781
782 switch (code)
783 {
784 case EXCEPTION_ACCESS_VIOLATION:
785 OUTMSG2 (("EXCEPTION_ACCESS_VIOLATION"));
786 ourstatus->value.sig = TARGET_SIGNAL_SEGV;
787 break;
788 case STATUS_STACK_OVERFLOW:
789 OUTMSG2 (("STATUS_STACK_OVERFLOW"));
790 ourstatus->value.sig = TARGET_SIGNAL_SEGV;
791 break;
792 case STATUS_FLOAT_DENORMAL_OPERAND:
793 OUTMSG2 (("STATUS_FLOAT_DENORMAL_OPERAND"));
794 ourstatus->value.sig = TARGET_SIGNAL_FPE;
795 break;
796 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
797 OUTMSG2 (("EXCEPTION_ARRAY_BOUNDS_EXCEEDED"));
798 ourstatus->value.sig = TARGET_SIGNAL_FPE;
799 break;
800 case STATUS_FLOAT_INEXACT_RESULT:
801 OUTMSG2 (("STATUS_FLOAT_INEXACT_RESULT"));
802 ourstatus->value.sig = TARGET_SIGNAL_FPE;
803 break;
804 case STATUS_FLOAT_INVALID_OPERATION:
805 OUTMSG2 (("STATUS_FLOAT_INVALID_OPERATION"));
806 ourstatus->value.sig = TARGET_SIGNAL_FPE;
807 break;
808 case STATUS_FLOAT_OVERFLOW:
809 OUTMSG2 (("STATUS_FLOAT_OVERFLOW"));
810 ourstatus->value.sig = TARGET_SIGNAL_FPE;
811 break;
812 case STATUS_FLOAT_STACK_CHECK:
813 OUTMSG2 (("STATUS_FLOAT_STACK_CHECK"));
814 ourstatus->value.sig = TARGET_SIGNAL_FPE;
815 break;
816 case STATUS_FLOAT_UNDERFLOW:
817 OUTMSG2 (("STATUS_FLOAT_UNDERFLOW"));
818 ourstatus->value.sig = TARGET_SIGNAL_FPE;
819 break;
820 case STATUS_FLOAT_DIVIDE_BY_ZERO:
821 OUTMSG2 (("STATUS_FLOAT_DIVIDE_BY_ZERO"));
822 ourstatus->value.sig = TARGET_SIGNAL_FPE;
823 break;
824 case STATUS_INTEGER_DIVIDE_BY_ZERO:
825 OUTMSG2 (("STATUS_INTEGER_DIVIDE_BY_ZERO"));
826 ourstatus->value.sig = TARGET_SIGNAL_FPE;
827 break;
828 case STATUS_INTEGER_OVERFLOW:
829 OUTMSG2 (("STATUS_INTEGER_OVERFLOW"));
830 ourstatus->value.sig = TARGET_SIGNAL_FPE;
831 break;
832 case EXCEPTION_BREAKPOINT:
833 OUTMSG2 (("EXCEPTION_BREAKPOINT"));
834 ourstatus->value.sig = TARGET_SIGNAL_TRAP;
835 #ifdef _WIN32_WCE
836 /* Remove the initial breakpoint. */
837 check_breakpoints ((CORE_ADDR) (long) current_event
838 .u.Exception.ExceptionRecord.ExceptionAddress);
839 #endif
840 break;
841 case DBG_CONTROL_C:
842 OUTMSG2 (("DBG_CONTROL_C"));
843 ourstatus->value.sig = TARGET_SIGNAL_INT;
844 break;
845 case DBG_CONTROL_BREAK:
846 OUTMSG2 (("DBG_CONTROL_BREAK"));
847 ourstatus->value.sig = TARGET_SIGNAL_INT;
848 break;
849 case EXCEPTION_SINGLE_STEP:
850 OUTMSG2 (("EXCEPTION_SINGLE_STEP"));
851 ourstatus->value.sig = TARGET_SIGNAL_TRAP;
852 break;
853 case EXCEPTION_ILLEGAL_INSTRUCTION:
854 OUTMSG2 (("EXCEPTION_ILLEGAL_INSTRUCTION"));
855 ourstatus->value.sig = TARGET_SIGNAL_ILL;
856 break;
857 case EXCEPTION_PRIV_INSTRUCTION:
858 OUTMSG2 (("EXCEPTION_PRIV_INSTRUCTION"));
859 ourstatus->value.sig = TARGET_SIGNAL_ILL;
860 break;
861 case EXCEPTION_NONCONTINUABLE_EXCEPTION:
862 OUTMSG2 (("EXCEPTION_NONCONTINUABLE_EXCEPTION"));
863 ourstatus->value.sig = TARGET_SIGNAL_ILL;
864 break;
865 default:
866 if (current_event.u.Exception.dwFirstChance)
867 {
868 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
869 return;
870 }
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.
874 ExceptionAddress));
875 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
876 break;
877 }
878 OUTMSG2 (("\n"));
879 last_sig = ourstatus->value.sig;
880 }
881
882 /* Get the next event from the child. */
883 static void
884 get_child_debug_event (struct target_waitstatus *ourstatus)
885 {
886 BOOL debug_event;
887
888 last_sig = TARGET_SIGNAL_0;
889 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
890
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 (&current_event, 250)))
894 return;
895
896 current_inferior =
897 (struct thread_info *) find_inferior_id (&all_threads,
898 current_event.dwThreadId);
899
900 switch (current_event.dwDebugEventCode)
901 {
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));
907
908 /* Record the existence of this thread. */
909 child_add_thread (current_event.dwThreadId,
910 current_event.u.CreateThread.hThread);
911 break;
912
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);
919 break;
920
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);
927
928 current_process_handle = current_event.u.CreateProcessInfo.hProcess;
929 main_thread_id = current_event.dwThreadId;
930
931 ourstatus->kind = TARGET_WAITKIND_EXECD;
932 ourstatus->value.execd_pathname = "Main executable";
933
934 /* Add the main thread. */
935 child_add_thread (main_thread_id,
936 current_event.u.CreateProcessInfo.hThread);
937
938 ourstatus->value.related_pid = current_event.dwThreadId;
939 #ifdef _WIN32_WCE
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);
946 #endif
947 break;
948
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;
958 break;
959
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);
966
967 ourstatus->kind = TARGET_WAITKIND_LOADED;
968 ourstatus->value.integer = 0;
969 break;
970
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));
976 break;
977
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);
984 break;
985
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);
993 break;
994
995 default:
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));
1001 break;
1002 }
1003
1004 current_inferior =
1005 (struct thread_info *) find_inferior_id (&all_threads,
1006 current_event.dwThreadId);
1007 }
1008
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)
1014 {
1015 struct target_waitstatus our_status;
1016
1017 *status = 'T';
1018
1019 while (1)
1020 {
1021 /* Check if GDB sent us an interrupt request. */
1022 check_remote_input_interrupt_request ();
1023
1024 get_child_debug_event (&our_status);
1025
1026 switch (our_status.kind)
1027 {
1028 case TARGET_WAITKIND_EXITED:
1029 OUTMSG2 (("Child exited with retcode = %x\n",
1030 our_status.value.integer));
1031
1032 *status = 'W';
1033
1034 child_fetch_inferior_registers (-1);
1035
1036 return our_status.value.integer;
1037 case TARGET_WAITKIND_STOPPED:
1038 OUTMSG2 (("Child Stopped with signal = %d \n",
1039 our_status.value.sig));
1040
1041 *status = 'T';
1042
1043 child_fetch_inferior_registers (-1);
1044
1045 return our_status.value.sig;
1046 default:
1047 OUTMSG (("Ignoring unknown internal event, %d\n", our_status.kind));
1048 /* fall-through */
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);
1054 break;
1055 }
1056 }
1057 }
1058
1059 /* Fetch registers from the inferior process.
1060 If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO. */
1061 static void
1062 win32_fetch_inferior_registers (int regno)
1063 {
1064 child_fetch_inferior_registers (regno);
1065 }
1066
1067 /* Store registers to the inferior process.
1068 If REGNO is -1, store all registers; otherwise, store at least REGNO. */
1069 static void
1070 win32_store_inferior_registers (int regno)
1071 {
1072 child_store_inferior_registers (regno);
1073 }
1074
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. */
1078 static int
1079 win32_read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
1080 {
1081 return child_xfer_memory (memaddr, (char *) myaddr, len, 0, 0) != len;
1082 }
1083
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. */
1088 static int
1089 win32_write_inferior_memory (CORE_ADDR memaddr, const unsigned char *myaddr,
1090 int len)
1091 {
1092 return child_xfer_memory (memaddr, (char *) myaddr, len, 1, 0) != len;
1093 }
1094
1095 /* Send an interrupt request to the inferior process. */
1096 static void
1097 win32_request_interrupt (void)
1098 {
1099 winapi_DebugBreakProcess DebugBreakProcess;
1100 winapi_GenerateConsoleCtrlEvent GenerateConsoleCtrlEvent;
1101
1102 #ifdef _WIN32_WCE
1103 HMODULE dll = GetModuleHandle (_T("COREDLL.DLL"));
1104 #else
1105 HMODULE dll = GetModuleHandle (_T("KERNEL32.DLL"));
1106 #endif
1107
1108 GenerateConsoleCtrlEvent = GETPROCADDRESS (dll, GenerateConsoleCtrlEvent);
1109
1110 if (GenerateConsoleCtrlEvent != NULL
1111 && GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT, current_process_id))
1112 return;
1113
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. */
1118
1119 DebugBreakProcess = GETPROCADDRESS (dll, DebugBreakProcess);
1120
1121 if (DebugBreakProcess != NULL
1122 && DebugBreakProcess (current_process_handle))
1123 return;
1124
1125 OUTMSG (("Could not interrupt process.\n"));
1126 }
1127
1128 static const char *
1129 win32_arch_string (void)
1130 {
1131 return the_low_target.arch_string;
1132 }
1133
1134 static struct target_ops win32_target_ops = {
1135 win32_create_inferior,
1136 win32_attach,
1137 win32_kill,
1138 win32_detach,
1139 win32_join,
1140 win32_thread_alive,
1141 win32_resume,
1142 win32_wait,
1143 win32_fetch_inferior_registers,
1144 win32_store_inferior_registers,
1145 win32_read_inferior_memory,
1146 win32_write_inferior_memory,
1147 NULL,
1148 win32_request_interrupt,
1149 NULL,
1150 NULL,
1151 NULL,
1152 NULL,
1153 NULL,
1154 NULL,
1155 NULL,
1156 win32_arch_string
1157 };
1158
1159 /* Initialize the Win32 backend. */
1160 void
1161 initialize_low (void)
1162 {
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);
1167 init_registers ();
1168 }
This page took 0.054384 seconds and 4 git commands to generate.