Import the "pathmax" gnulib module.
[deliverable/binutils-gdb.git] / gdb / gdbserver / win32-low.c
1 /* Low level interface to Windows debugging, for gdbserver.
2 Copyright (C) 2006-2013 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 3 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, see <http://www.gnu.org/licenses/>. */
20
21 #include "server.h"
22 #include "regcache.h"
23 #include "gdb/signals.h"
24 #include "gdb/fileio.h"
25 #include "mem-break.h"
26 #include "win32-low.h"
27 #include "gdbthread.h"
28
29 #include <stdint.h>
30 #include <windows.h>
31 #include <winnt.h>
32 #include <imagehlp.h>
33 #include <tlhelp32.h>
34 #include <psapi.h>
35 #include <sys/param.h>
36 #include <process.h>
37
38 #ifndef USE_WIN32API
39 #include <sys/cygwin.h>
40 #endif
41
42 #define OUTMSG(X) do { printf X; fflush (stderr); } while (0)
43
44 #define OUTMSG2(X) \
45 do \
46 { \
47 if (debug_threads) \
48 { \
49 printf X; \
50 fflush (stderr); \
51 } \
52 } while (0)
53
54 #ifndef _T
55 #define _T(x) TEXT (x)
56 #endif
57
58 #ifndef COUNTOF
59 #define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0]))
60 #endif
61
62 #ifdef _WIN32_WCE
63 # define GETPROCADDRESS(DLL, PROC) \
64 ((winapi_ ## PROC) GetProcAddress (DLL, TEXT (#PROC)))
65 #else
66 # define GETPROCADDRESS(DLL, PROC) \
67 ((winapi_ ## PROC) GetProcAddress (DLL, #PROC))
68 #endif
69
70 int using_threads = 1;
71
72 /* Globals. */
73 static int attaching = 0;
74 static HANDLE current_process_handle = NULL;
75 static DWORD current_process_id = 0;
76 static DWORD main_thread_id = 0;
77 static enum gdb_signal last_sig = GDB_SIGNAL_0;
78
79 /* The current debug event from WaitForDebugEvent. */
80 static DEBUG_EVENT current_event;
81
82 /* Non zero if an interrupt request is to be satisfied by suspending
83 all threads. */
84 static int soft_interrupt_requested = 0;
85
86 /* Non zero if the inferior is stopped in a simulated breakpoint done
87 by suspending all the threads. */
88 static int faked_breakpoint = 0;
89
90 const struct target_desc *win32_tdesc;
91
92 #define NUM_REGS (the_low_target.num_regs)
93
94 typedef BOOL WINAPI (*winapi_DebugActiveProcessStop) (DWORD dwProcessId);
95 typedef BOOL WINAPI (*winapi_DebugSetProcessKillOnExit) (BOOL KillOnExit);
96 typedef BOOL WINAPI (*winapi_DebugBreakProcess) (HANDLE);
97 typedef BOOL WINAPI (*winapi_GenerateConsoleCtrlEvent) (DWORD, DWORD);
98
99 static void win32_resume (struct thread_resume *resume_info, size_t n);
100
101 /* Get the thread ID from the current selected inferior (the current
102 thread). */
103 static ptid_t
104 current_inferior_ptid (void)
105 {
106 return ((struct inferior_list_entry*) current_inferior)->id;
107 }
108
109 /* The current debug event from WaitForDebugEvent. */
110 static ptid_t
111 debug_event_ptid (DEBUG_EVENT *event)
112 {
113 return ptid_build (event->dwProcessId, event->dwThreadId, 0);
114 }
115
116 /* Get the thread context of the thread associated with TH. */
117
118 static void
119 win32_get_thread_context (win32_thread_info *th)
120 {
121 memset (&th->context, 0, sizeof (CONTEXT));
122 (*the_low_target.get_thread_context) (th, &current_event);
123 #ifdef _WIN32_WCE
124 memcpy (&th->base_context, &th->context, sizeof (CONTEXT));
125 #endif
126 }
127
128 /* Set the thread context of the thread associated with TH. */
129
130 static void
131 win32_set_thread_context (win32_thread_info *th)
132 {
133 #ifdef _WIN32_WCE
134 /* Calling SuspendThread on a thread that is running kernel code
135 will report that the suspending was successful, but in fact, that
136 will often not be true. In those cases, the context returned by
137 GetThreadContext will not be correct by the time the thread
138 stops, hence we can't set that context back into the thread when
139 resuming - it will most likelly crash the inferior.
140 Unfortunately, there is no way to know when the thread will
141 really stop. To work around it, we'll only write the context
142 back to the thread when either the user or GDB explicitly change
143 it between stopping and resuming. */
144 if (memcmp (&th->context, &th->base_context, sizeof (CONTEXT)) != 0)
145 #endif
146 (*the_low_target.set_thread_context) (th, &current_event);
147 }
148
149 /* Find a thread record given a thread id. If GET_CONTEXT is set then
150 also retrieve the context for this thread. */
151 static win32_thread_info *
152 thread_rec (ptid_t ptid, int get_context)
153 {
154 struct thread_info *thread;
155 win32_thread_info *th;
156
157 thread = (struct thread_info *) find_inferior_id (&all_threads, ptid);
158 if (thread == NULL)
159 return NULL;
160
161 th = inferior_target_data (thread);
162 if (get_context && th->context.ContextFlags == 0)
163 {
164 if (!th->suspended)
165 {
166 if (SuspendThread (th->h) == (DWORD) -1)
167 {
168 DWORD err = GetLastError ();
169 OUTMSG (("warning: SuspendThread failed in thread_rec, "
170 "(error %d): %s\n", (int) err, strwinerror (err)));
171 }
172 else
173 th->suspended = 1;
174 }
175
176 win32_get_thread_context (th);
177 }
178
179 return th;
180 }
181
182 /* Add a thread to the thread list. */
183 static win32_thread_info *
184 child_add_thread (DWORD pid, DWORD tid, HANDLE h, void *tlb)
185 {
186 win32_thread_info *th;
187 ptid_t ptid = ptid_build (pid, tid, 0);
188
189 if ((th = thread_rec (ptid, FALSE)))
190 return th;
191
192 th = xcalloc (1, sizeof (*th));
193 th->tid = tid;
194 th->h = h;
195 th->thread_local_base = (CORE_ADDR) (uintptr_t) tlb;
196
197 add_thread (ptid, th);
198
199 if (the_low_target.thread_added != NULL)
200 (*the_low_target.thread_added) (th);
201
202 return th;
203 }
204
205 /* Delete a thread from the list of threads. */
206 static void
207 delete_thread_info (struct inferior_list_entry *thread)
208 {
209 win32_thread_info *th = inferior_target_data ((struct thread_info *) thread);
210
211 remove_thread ((struct thread_info *) thread);
212 CloseHandle (th->h);
213 free (th);
214 }
215
216 /* Delete a thread from the list of threads. */
217 static void
218 child_delete_thread (DWORD pid, DWORD tid)
219 {
220 struct inferior_list_entry *thread;
221 ptid_t ptid;
222
223 /* If the last thread is exiting, just return. */
224 if (all_threads.head == all_threads.tail)
225 return;
226
227 ptid = ptid_build (pid, tid, 0);
228 thread = find_inferior_id (&all_threads, ptid);
229 if (thread == NULL)
230 return;
231
232 delete_thread_info (thread);
233 }
234
235 /* These watchpoint related wrapper functions simply pass on the function call
236 if the low target has registered a corresponding function. */
237
238 static int
239 win32_insert_point (char type, CORE_ADDR addr, int len)
240 {
241 if (the_low_target.insert_point != NULL)
242 return the_low_target.insert_point (type, addr, len);
243 else
244 /* Unsupported (see target.h). */
245 return 1;
246 }
247
248 static int
249 win32_remove_point (char type, CORE_ADDR addr, int len)
250 {
251 if (the_low_target.remove_point != NULL)
252 return the_low_target.remove_point (type, addr, len);
253 else
254 /* Unsupported (see target.h). */
255 return 1;
256 }
257
258 static int
259 win32_stopped_by_watchpoint (void)
260 {
261 if (the_low_target.stopped_by_watchpoint != NULL)
262 return the_low_target.stopped_by_watchpoint ();
263 else
264 return 0;
265 }
266
267 static CORE_ADDR
268 win32_stopped_data_address (void)
269 {
270 if (the_low_target.stopped_data_address != NULL)
271 return the_low_target.stopped_data_address ();
272 else
273 return 0;
274 }
275
276
277 /* Transfer memory from/to the debugged process. */
278 static int
279 child_xfer_memory (CORE_ADDR memaddr, char *our, int len,
280 int write, struct target_ops *target)
281 {
282 SIZE_T done;
283 uintptr_t addr = (uintptr_t) memaddr;
284
285 if (write)
286 {
287 WriteProcessMemory (current_process_handle, (LPVOID) addr,
288 (LPCVOID) our, len, &done);
289 FlushInstructionCache (current_process_handle, (LPCVOID) addr, len);
290 }
291 else
292 {
293 ReadProcessMemory (current_process_handle, (LPCVOID) addr, (LPVOID) our,
294 len, &done);
295 }
296 return done;
297 }
298
299 /* Clear out any old thread list and reinitialize it to a pristine
300 state. */
301 static void
302 child_init_thread_list (void)
303 {
304 for_each_inferior (&all_threads, delete_thread_info);
305 }
306
307 static void
308 do_initial_child_stuff (HANDLE proch, DWORD pid, int attached)
309 {
310 struct process_info *proc;
311
312 last_sig = GDB_SIGNAL_0;
313
314 current_process_handle = proch;
315 current_process_id = pid;
316 main_thread_id = 0;
317
318 soft_interrupt_requested = 0;
319 faked_breakpoint = 0;
320
321 memset (&current_event, 0, sizeof (current_event));
322
323 proc = add_process (pid, attached);
324 proc->tdesc = win32_tdesc;
325 child_init_thread_list ();
326
327 if (the_low_target.initial_stuff != NULL)
328 (*the_low_target.initial_stuff) ();
329 }
330
331 /* Resume all artificially suspended threads if we are continuing
332 execution. */
333 static int
334 continue_one_thread (struct inferior_list_entry *this_thread, void *id_ptr)
335 {
336 struct thread_info *thread = (struct thread_info *) this_thread;
337 int thread_id = * (int *) id_ptr;
338 win32_thread_info *th = inferior_target_data (thread);
339
340 if ((thread_id == -1 || thread_id == th->tid)
341 && th->suspended)
342 {
343 if (th->context.ContextFlags)
344 {
345 win32_set_thread_context (th);
346 th->context.ContextFlags = 0;
347 }
348
349 if (ResumeThread (th->h) == (DWORD) -1)
350 {
351 DWORD err = GetLastError ();
352 OUTMSG (("warning: ResumeThread failed in continue_one_thread, "
353 "(error %d): %s\n", (int) err, strwinerror (err)));
354 }
355 th->suspended = 0;
356 }
357
358 return 0;
359 }
360
361 static BOOL
362 child_continue (DWORD continue_status, int thread_id)
363 {
364 /* The inferior will only continue after the ContinueDebugEvent
365 call. */
366 find_inferior (&all_threads, continue_one_thread, &thread_id);
367 faked_breakpoint = 0;
368
369 if (!ContinueDebugEvent (current_event.dwProcessId,
370 current_event.dwThreadId,
371 continue_status))
372 return FALSE;
373
374 return TRUE;
375 }
376
377 /* Fetch register(s) from the current thread context. */
378 static void
379 child_fetch_inferior_registers (struct regcache *regcache, int r)
380 {
381 int regno;
382 win32_thread_info *th = thread_rec (current_inferior_ptid (), TRUE);
383 if (r == -1 || r > NUM_REGS)
384 child_fetch_inferior_registers (regcache, NUM_REGS);
385 else
386 for (regno = 0; regno < r; regno++)
387 (*the_low_target.fetch_inferior_register) (regcache, th, regno);
388 }
389
390 /* Store a new register value into the current thread context. We don't
391 change the program's context until later, when we resume it. */
392 static void
393 child_store_inferior_registers (struct regcache *regcache, int r)
394 {
395 int regno;
396 win32_thread_info *th = thread_rec (current_inferior_ptid (), TRUE);
397 if (r == -1 || r == 0 || r > NUM_REGS)
398 child_store_inferior_registers (regcache, NUM_REGS);
399 else
400 for (regno = 0; regno < r; regno++)
401 (*the_low_target.store_inferior_register) (regcache, th, regno);
402 }
403
404 /* Map the Windows error number in ERROR to a locale-dependent error
405 message string and return a pointer to it. Typically, the values
406 for ERROR come from GetLastError.
407
408 The string pointed to shall not be modified by the application,
409 but may be overwritten by a subsequent call to strwinerror
410
411 The strwinerror function does not change the current setting
412 of GetLastError. */
413
414 char *
415 strwinerror (DWORD error)
416 {
417 static char buf[1024];
418 TCHAR *msgbuf;
419 DWORD lasterr = GetLastError ();
420 DWORD chars = FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
421 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
422 NULL,
423 error,
424 0, /* Default language */
425 (LPVOID)&msgbuf,
426 0,
427 NULL);
428 if (chars != 0)
429 {
430 /* If there is an \r\n appended, zap it. */
431 if (chars >= 2
432 && msgbuf[chars - 2] == '\r'
433 && msgbuf[chars - 1] == '\n')
434 {
435 chars -= 2;
436 msgbuf[chars] = 0;
437 }
438
439 if (chars > ((COUNTOF (buf)) - 1))
440 {
441 chars = COUNTOF (buf) - 1;
442 msgbuf [chars] = 0;
443 }
444
445 #ifdef UNICODE
446 wcstombs (buf, msgbuf, chars + 1);
447 #else
448 strncpy (buf, msgbuf, chars + 1);
449 #endif
450 LocalFree (msgbuf);
451 }
452 else
453 sprintf (buf, "unknown win32 error (%u)", (unsigned) error);
454
455 SetLastError (lasterr);
456 return buf;
457 }
458
459 static BOOL
460 create_process (const char *program, char *args,
461 DWORD flags, PROCESS_INFORMATION *pi)
462 {
463 BOOL ret;
464
465 #ifdef _WIN32_WCE
466 wchar_t *p, *wprogram, *wargs;
467 size_t argslen;
468
469 wprogram = alloca ((strlen (program) + 1) * sizeof (wchar_t));
470 mbstowcs (wprogram, program, strlen (program) + 1);
471
472 for (p = wprogram; *p; ++p)
473 if (L'/' == *p)
474 *p = L'\\';
475
476 argslen = strlen (args);
477 wargs = alloca ((argslen + 1) * sizeof (wchar_t));
478 mbstowcs (wargs, args, argslen + 1);
479
480 ret = CreateProcessW (wprogram, /* image name */
481 wargs, /* command line */
482 NULL, /* security, not supported */
483 NULL, /* thread, not supported */
484 FALSE, /* inherit handles, not supported */
485 flags, /* start flags */
486 NULL, /* environment, not supported */
487 NULL, /* current directory, not supported */
488 NULL, /* start info, not supported */
489 pi); /* proc info */
490 #else
491 STARTUPINFOA si = { sizeof (STARTUPINFOA) };
492
493 ret = CreateProcessA (program, /* image name */
494 args, /* command line */
495 NULL, /* security */
496 NULL, /* thread */
497 TRUE, /* inherit handles */
498 flags, /* start flags */
499 NULL, /* environment */
500 NULL, /* current directory */
501 &si, /* start info */
502 pi); /* proc info */
503 #endif
504
505 return ret;
506 }
507
508 /* Start a new process.
509 PROGRAM is a path to the program to execute.
510 ARGS is a standard NULL-terminated array of arguments,
511 to be passed to the inferior as ``argv''.
512 Returns the new PID on success, -1 on failure. Registers the new
513 process with the process list. */
514 static int
515 win32_create_inferior (char *program, char **program_args)
516 {
517 #ifndef USE_WIN32API
518 char real_path[MAXPATHLEN];
519 char *orig_path, *new_path, *path_ptr;
520 #endif
521 BOOL ret;
522 DWORD flags;
523 char *args;
524 int argslen;
525 int argc;
526 PROCESS_INFORMATION pi;
527 DWORD err;
528
529 /* win32_wait needs to know we're not attaching. */
530 attaching = 0;
531
532 if (!program)
533 error ("No executable specified, specify executable to debug.\n");
534
535 flags = DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS;
536
537 #ifndef USE_WIN32API
538 orig_path = NULL;
539 path_ptr = getenv ("PATH");
540 if (path_ptr)
541 {
542 int size = cygwin_conv_path_list (CCP_POSIX_TO_WIN_A, path_ptr, NULL, 0);
543 orig_path = alloca (strlen (path_ptr) + 1);
544 new_path = alloca (size);
545 strcpy (orig_path, path_ptr);
546 cygwin_conv_path_list (CCP_POSIX_TO_WIN_A, path_ptr, new_path, size);
547 setenv ("PATH", new_path, 1);
548 }
549 cygwin_conv_path (CCP_POSIX_TO_WIN_A, program, real_path,
550 MAXPATHLEN);
551 program = real_path;
552 #endif
553
554 argslen = 1;
555 for (argc = 1; program_args[argc]; argc++)
556 argslen += strlen (program_args[argc]) + 1;
557 args = alloca (argslen);
558 args[0] = '\0';
559 for (argc = 1; program_args[argc]; argc++)
560 {
561 /* FIXME: Can we do better about quoting? How does Cygwin
562 handle this? */
563 strcat (args, " ");
564 strcat (args, program_args[argc]);
565 }
566 OUTMSG2 (("Command line is \"%s\"\n", args));
567
568 #ifdef CREATE_NEW_PROCESS_GROUP
569 flags |= CREATE_NEW_PROCESS_GROUP;
570 #endif
571
572 ret = create_process (program, args, flags, &pi);
573 err = GetLastError ();
574 if (!ret && err == ERROR_FILE_NOT_FOUND)
575 {
576 char *exename = alloca (strlen (program) + 5);
577 strcat (strcpy (exename, program), ".exe");
578 ret = create_process (exename, args, flags, &pi);
579 err = GetLastError ();
580 }
581
582 #ifndef USE_WIN32API
583 if (orig_path)
584 setenv ("PATH", orig_path, 1);
585 #endif
586
587 if (!ret)
588 {
589 error ("Error creating process \"%s%s\", (error %d): %s\n",
590 program, args, (int) err, strwinerror (err));
591 }
592 else
593 {
594 OUTMSG2 (("Process created: %s\n", (char *) args));
595 }
596
597 #ifndef _WIN32_WCE
598 /* On Windows CE this handle can't be closed. The OS reuses
599 it in the debug events, while the 9x/NT versions of Windows
600 probably use a DuplicateHandle'd one. */
601 CloseHandle (pi.hThread);
602 #endif
603
604 do_initial_child_stuff (pi.hProcess, pi.dwProcessId, 0);
605
606 return current_process_id;
607 }
608
609 /* Attach to a running process.
610 PID is the process ID to attach to, specified by the user
611 or a higher layer. */
612 static int
613 win32_attach (unsigned long pid)
614 {
615 HANDLE h;
616 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit = NULL;
617 DWORD err;
618 #ifdef _WIN32_WCE
619 HMODULE dll = GetModuleHandle (_T("COREDLL.DLL"));
620 #else
621 HMODULE dll = GetModuleHandle (_T("KERNEL32.DLL"));
622 #endif
623 DebugSetProcessKillOnExit = GETPROCADDRESS (dll, DebugSetProcessKillOnExit);
624
625 h = OpenProcess (PROCESS_ALL_ACCESS, FALSE, pid);
626 if (h != NULL)
627 {
628 if (DebugActiveProcess (pid))
629 {
630 if (DebugSetProcessKillOnExit != NULL)
631 DebugSetProcessKillOnExit (FALSE);
632
633 /* win32_wait needs to know we're attaching. */
634 attaching = 1;
635 do_initial_child_stuff (h, pid, 1);
636 return 0;
637 }
638
639 CloseHandle (h);
640 }
641
642 err = GetLastError ();
643 error ("Attach to process failed (error %d): %s\n",
644 (int) err, strwinerror (err));
645 }
646
647 /* Handle OUTPUT_DEBUG_STRING_EVENT from child process. */
648 static void
649 handle_output_debug_string (struct target_waitstatus *ourstatus)
650 {
651 #define READ_BUFFER_LEN 1024
652 CORE_ADDR addr;
653 char s[READ_BUFFER_LEN + 1] = { 0 };
654 DWORD nbytes = current_event.u.DebugString.nDebugStringLength;
655
656 if (nbytes == 0)
657 return;
658
659 if (nbytes > READ_BUFFER_LEN)
660 nbytes = READ_BUFFER_LEN;
661
662 addr = (CORE_ADDR) (size_t) current_event.u.DebugString.lpDebugStringData;
663
664 if (current_event.u.DebugString.fUnicode)
665 {
666 /* The event tells us how many bytes, not chars, even
667 in Unicode. */
668 WCHAR buffer[(READ_BUFFER_LEN + 1) / sizeof (WCHAR)] = { 0 };
669 if (read_inferior_memory (addr, (unsigned char *) buffer, nbytes) != 0)
670 return;
671 wcstombs (s, buffer, (nbytes + 1) / sizeof (WCHAR));
672 }
673 else
674 {
675 if (read_inferior_memory (addr, (unsigned char *) s, nbytes) != 0)
676 return;
677 }
678
679 if (strncmp (s, "cYg", 3) != 0)
680 {
681 if (!server_waiting)
682 {
683 OUTMSG2(("%s", s));
684 return;
685 }
686
687 monitor_output (s);
688 }
689 #undef READ_BUFFER_LEN
690 }
691
692 static void
693 win32_clear_inferiors (void)
694 {
695 if (current_process_handle != NULL)
696 CloseHandle (current_process_handle);
697
698 for_each_inferior (&all_threads, delete_thread_info);
699 clear_inferiors ();
700 }
701
702 /* Kill all inferiors. */
703 static int
704 win32_kill (int pid)
705 {
706 struct process_info *process;
707
708 if (current_process_handle == NULL)
709 return -1;
710
711 TerminateProcess (current_process_handle, 0);
712 for (;;)
713 {
714 if (!child_continue (DBG_CONTINUE, -1))
715 break;
716 if (!WaitForDebugEvent (&current_event, INFINITE))
717 break;
718 if (current_event.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT)
719 break;
720 else if (current_event.dwDebugEventCode == OUTPUT_DEBUG_STRING_EVENT)
721 {
722 struct target_waitstatus our_status = { 0 };
723 handle_output_debug_string (&our_status);
724 }
725 }
726
727 win32_clear_inferiors ();
728
729 process = find_process_pid (pid);
730 remove_process (process);
731 return 0;
732 }
733
734 /* Detach from inferior PID. */
735 static int
736 win32_detach (int pid)
737 {
738 struct process_info *process;
739 winapi_DebugActiveProcessStop DebugActiveProcessStop = NULL;
740 winapi_DebugSetProcessKillOnExit DebugSetProcessKillOnExit = NULL;
741 #ifdef _WIN32_WCE
742 HMODULE dll = GetModuleHandle (_T("COREDLL.DLL"));
743 #else
744 HMODULE dll = GetModuleHandle (_T("KERNEL32.DLL"));
745 #endif
746 DebugActiveProcessStop = GETPROCADDRESS (dll, DebugActiveProcessStop);
747 DebugSetProcessKillOnExit = GETPROCADDRESS (dll, DebugSetProcessKillOnExit);
748
749 if (DebugSetProcessKillOnExit == NULL
750 || DebugActiveProcessStop == NULL)
751 return -1;
752
753 {
754 struct thread_resume resume;
755 resume.thread = minus_one_ptid;
756 resume.kind = resume_continue;
757 resume.sig = 0;
758 win32_resume (&resume, 1);
759 }
760
761 if (!DebugActiveProcessStop (current_process_id))
762 return -1;
763
764 DebugSetProcessKillOnExit (FALSE);
765 process = find_process_pid (pid);
766 remove_process (process);
767
768 win32_clear_inferiors ();
769 return 0;
770 }
771
772 static void
773 win32_mourn (struct process_info *process)
774 {
775 remove_process (process);
776 }
777
778 /* Wait for inferiors to end. */
779 static void
780 win32_join (int pid)
781 {
782 HANDLE h = OpenProcess (PROCESS_ALL_ACCESS, FALSE, pid);
783 if (h != NULL)
784 {
785 WaitForSingleObject (h, INFINITE);
786 CloseHandle (h);
787 }
788 }
789
790 /* Return 1 iff the thread with thread ID TID is alive. */
791 static int
792 win32_thread_alive (ptid_t ptid)
793 {
794 int res;
795
796 /* Our thread list is reliable; don't bother to poll target
797 threads. */
798 if (find_inferior_id (&all_threads, ptid) != NULL)
799 res = 1;
800 else
801 res = 0;
802 return res;
803 }
804
805 /* Resume the inferior process. RESUME_INFO describes how we want
806 to resume. */
807 static void
808 win32_resume (struct thread_resume *resume_info, size_t n)
809 {
810 DWORD tid;
811 enum gdb_signal sig;
812 int step;
813 win32_thread_info *th;
814 DWORD continue_status = DBG_CONTINUE;
815 ptid_t ptid;
816
817 /* This handles the very limited set of resume packets that GDB can
818 currently produce. */
819
820 if (n == 1 && ptid_equal (resume_info[0].thread, minus_one_ptid))
821 tid = -1;
822 else if (n > 1)
823 tid = -1;
824 else
825 /* Yes, we're ignoring resume_info[0].thread. It'd be tricky to make
826 the Windows resume code do the right thing for thread switching. */
827 tid = current_event.dwThreadId;
828
829 if (!ptid_equal (resume_info[0].thread, minus_one_ptid))
830 {
831 sig = resume_info[0].sig;
832 step = resume_info[0].kind == resume_step;
833 }
834 else
835 {
836 sig = 0;
837 step = 0;
838 }
839
840 if (sig != GDB_SIGNAL_0)
841 {
842 if (current_event.dwDebugEventCode != EXCEPTION_DEBUG_EVENT)
843 {
844 OUTMSG (("Cannot continue with signal %d here.\n", sig));
845 }
846 else if (sig == last_sig)
847 continue_status = DBG_EXCEPTION_NOT_HANDLED;
848 else
849 OUTMSG (("Can only continue with recieved signal %d.\n", last_sig));
850 }
851
852 last_sig = GDB_SIGNAL_0;
853
854 /* Get context for the currently selected thread. */
855 ptid = debug_event_ptid (&current_event);
856 th = thread_rec (ptid, FALSE);
857 if (th)
858 {
859 if (th->context.ContextFlags)
860 {
861 /* Move register values from the inferior into the thread
862 context structure. */
863 regcache_invalidate ();
864
865 if (step)
866 {
867 if (the_low_target.single_step != NULL)
868 (*the_low_target.single_step) (th);
869 else
870 error ("Single stepping is not supported "
871 "in this configuration.\n");
872 }
873
874 win32_set_thread_context (th);
875 th->context.ContextFlags = 0;
876 }
877 }
878
879 /* Allow continuing with the same signal that interrupted us.
880 Otherwise complain. */
881
882 child_continue (continue_status, tid);
883 }
884
885 static void
886 win32_add_one_solib (const char *name, CORE_ADDR load_addr)
887 {
888 char buf[MAX_PATH + 1];
889 char buf2[MAX_PATH + 1];
890
891 #ifdef _WIN32_WCE
892 WIN32_FIND_DATA w32_fd;
893 WCHAR wname[MAX_PATH + 1];
894 mbstowcs (wname, name, MAX_PATH);
895 HANDLE h = FindFirstFile (wname, &w32_fd);
896 #else
897 WIN32_FIND_DATAA w32_fd;
898 HANDLE h = FindFirstFileA (name, &w32_fd);
899 #endif
900
901 if (h == INVALID_HANDLE_VALUE)
902 strcpy (buf, name);
903 else
904 {
905 FindClose (h);
906 strcpy (buf, name);
907 #ifndef _WIN32_WCE
908 {
909 char cwd[MAX_PATH + 1];
910 char *p;
911 if (GetCurrentDirectoryA (MAX_PATH + 1, cwd))
912 {
913 p = strrchr (buf, '\\');
914 if (p)
915 p[1] = '\0';
916 SetCurrentDirectoryA (buf);
917 GetFullPathNameA (w32_fd.cFileName, MAX_PATH, buf, &p);
918 SetCurrentDirectoryA (cwd);
919 }
920 }
921 #endif
922 }
923
924 #ifndef _WIN32_WCE
925 if (strcasecmp (buf, "ntdll.dll") == 0)
926 {
927 GetSystemDirectoryA (buf, sizeof (buf));
928 strcat (buf, "\\ntdll.dll");
929 }
930 #endif
931
932 #ifdef __CYGWIN__
933 cygwin_conv_path (CCP_WIN_A_TO_POSIX, buf, buf2, sizeof (buf2));
934 #else
935 strcpy (buf2, buf);
936 #endif
937
938 loaded_dll (buf2, load_addr);
939 }
940
941 static char *
942 get_image_name (HANDLE h, void *address, int unicode)
943 {
944 static char buf[(2 * MAX_PATH) + 1];
945 DWORD size = unicode ? sizeof (WCHAR) : sizeof (char);
946 char *address_ptr;
947 int len = 0;
948 char b[2];
949 SIZE_T done;
950
951 /* Attempt to read the name of the dll that was detected.
952 This is documented to work only when actively debugging
953 a program. It will not work for attached processes. */
954 if (address == NULL)
955 return NULL;
956
957 #ifdef _WIN32_WCE
958 /* Windows CE reports the address of the image name,
959 instead of an address of a pointer into the image name. */
960 address_ptr = address;
961 #else
962 /* See if we could read the address of a string, and that the
963 address isn't null. */
964 if (!ReadProcessMemory (h, address, &address_ptr,
965 sizeof (address_ptr), &done)
966 || done != sizeof (address_ptr)
967 || !address_ptr)
968 return NULL;
969 #endif
970
971 /* Find the length of the string */
972 while (ReadProcessMemory (h, address_ptr + len++ * size, &b, size, &done)
973 && (b[0] != 0 || b[size - 1] != 0) && done == size)
974 continue;
975
976 if (!unicode)
977 ReadProcessMemory (h, address_ptr, buf, len, &done);
978 else
979 {
980 WCHAR *unicode_address = (WCHAR *) alloca (len * sizeof (WCHAR));
981 ReadProcessMemory (h, address_ptr, unicode_address, len * sizeof (WCHAR),
982 &done);
983
984 WideCharToMultiByte (CP_ACP, 0, unicode_address, len, buf, len, 0, 0);
985 }
986
987 return buf;
988 }
989
990 typedef BOOL (WINAPI *winapi_EnumProcessModules) (HANDLE, HMODULE *,
991 DWORD, LPDWORD);
992 typedef BOOL (WINAPI *winapi_GetModuleInformation) (HANDLE, HMODULE,
993 LPMODULEINFO, DWORD);
994 typedef DWORD (WINAPI *winapi_GetModuleFileNameExA) (HANDLE, HMODULE,
995 LPSTR, DWORD);
996
997 static winapi_EnumProcessModules win32_EnumProcessModules;
998 static winapi_GetModuleInformation win32_GetModuleInformation;
999 static winapi_GetModuleFileNameExA win32_GetModuleFileNameExA;
1000
1001 static BOOL
1002 load_psapi (void)
1003 {
1004 static int psapi_loaded = 0;
1005 static HMODULE dll = NULL;
1006
1007 if (!psapi_loaded)
1008 {
1009 psapi_loaded = 1;
1010 dll = LoadLibrary (TEXT("psapi.dll"));
1011 if (!dll)
1012 return FALSE;
1013 win32_EnumProcessModules =
1014 GETPROCADDRESS (dll, EnumProcessModules);
1015 win32_GetModuleInformation =
1016 GETPROCADDRESS (dll, GetModuleInformation);
1017 win32_GetModuleFileNameExA =
1018 GETPROCADDRESS (dll, GetModuleFileNameExA);
1019 }
1020
1021 return (win32_EnumProcessModules != NULL
1022 && win32_GetModuleInformation != NULL
1023 && win32_GetModuleFileNameExA != NULL);
1024 }
1025
1026 static int
1027 psapi_get_dll_name (LPVOID BaseAddress, char *dll_name_ret)
1028 {
1029 DWORD len;
1030 MODULEINFO mi;
1031 size_t i;
1032 HMODULE dh_buf[1];
1033 HMODULE *DllHandle = dh_buf;
1034 DWORD cbNeeded;
1035 BOOL ok;
1036
1037 if (!load_psapi ())
1038 goto failed;
1039
1040 cbNeeded = 0;
1041 ok = (*win32_EnumProcessModules) (current_process_handle,
1042 DllHandle,
1043 sizeof (HMODULE),
1044 &cbNeeded);
1045
1046 if (!ok || !cbNeeded)
1047 goto failed;
1048
1049 DllHandle = (HMODULE *) alloca (cbNeeded);
1050 if (!DllHandle)
1051 goto failed;
1052
1053 ok = (*win32_EnumProcessModules) (current_process_handle,
1054 DllHandle,
1055 cbNeeded,
1056 &cbNeeded);
1057 if (!ok)
1058 goto failed;
1059
1060 for (i = 0; i < ((size_t) cbNeeded / sizeof (HMODULE)); i++)
1061 {
1062 if (!(*win32_GetModuleInformation) (current_process_handle,
1063 DllHandle[i],
1064 &mi,
1065 sizeof (mi)))
1066 {
1067 DWORD err = GetLastError ();
1068 error ("Can't get module info: (error %d): %s\n",
1069 (int) err, strwinerror (err));
1070 }
1071
1072 if (mi.lpBaseOfDll == BaseAddress)
1073 {
1074 len = (*win32_GetModuleFileNameExA) (current_process_handle,
1075 DllHandle[i],
1076 dll_name_ret,
1077 MAX_PATH);
1078 if (len == 0)
1079 {
1080 DWORD err = GetLastError ();
1081 error ("Error getting dll name: (error %d): %s\n",
1082 (int) err, strwinerror (err));
1083 }
1084 return 1;
1085 }
1086 }
1087
1088 failed:
1089 dll_name_ret[0] = '\0';
1090 return 0;
1091 }
1092
1093 typedef HANDLE (WINAPI *winapi_CreateToolhelp32Snapshot) (DWORD, DWORD);
1094 typedef BOOL (WINAPI *winapi_Module32First) (HANDLE, LPMODULEENTRY32);
1095 typedef BOOL (WINAPI *winapi_Module32Next) (HANDLE, LPMODULEENTRY32);
1096
1097 static winapi_CreateToolhelp32Snapshot win32_CreateToolhelp32Snapshot;
1098 static winapi_Module32First win32_Module32First;
1099 static winapi_Module32Next win32_Module32Next;
1100 #ifdef _WIN32_WCE
1101 typedef BOOL (WINAPI *winapi_CloseToolhelp32Snapshot) (HANDLE);
1102 static winapi_CloseToolhelp32Snapshot win32_CloseToolhelp32Snapshot;
1103 #endif
1104
1105 static BOOL
1106 load_toolhelp (void)
1107 {
1108 static int toolhelp_loaded = 0;
1109 static HMODULE dll = NULL;
1110
1111 if (!toolhelp_loaded)
1112 {
1113 toolhelp_loaded = 1;
1114 #ifndef _WIN32_WCE
1115 dll = GetModuleHandle (_T("KERNEL32.DLL"));
1116 #else
1117 dll = LoadLibrary (L"TOOLHELP.DLL");
1118 #endif
1119 if (!dll)
1120 return FALSE;
1121
1122 win32_CreateToolhelp32Snapshot =
1123 GETPROCADDRESS (dll, CreateToolhelp32Snapshot);
1124 win32_Module32First = GETPROCADDRESS (dll, Module32First);
1125 win32_Module32Next = GETPROCADDRESS (dll, Module32Next);
1126 #ifdef _WIN32_WCE
1127 win32_CloseToolhelp32Snapshot =
1128 GETPROCADDRESS (dll, CloseToolhelp32Snapshot);
1129 #endif
1130 }
1131
1132 return (win32_CreateToolhelp32Snapshot != NULL
1133 && win32_Module32First != NULL
1134 && win32_Module32Next != NULL
1135 #ifdef _WIN32_WCE
1136 && win32_CloseToolhelp32Snapshot != NULL
1137 #endif
1138 );
1139 }
1140
1141 static int
1142 toolhelp_get_dll_name (LPVOID BaseAddress, char *dll_name_ret)
1143 {
1144 HANDLE snapshot_module;
1145 MODULEENTRY32 modEntry = { sizeof (MODULEENTRY32) };
1146 int found = 0;
1147
1148 if (!load_toolhelp ())
1149 return 0;
1150
1151 snapshot_module = win32_CreateToolhelp32Snapshot (TH32CS_SNAPMODULE,
1152 current_event.dwProcessId);
1153 if (snapshot_module == INVALID_HANDLE_VALUE)
1154 return 0;
1155
1156 /* Ignore the first module, which is the exe. */
1157 if (win32_Module32First (snapshot_module, &modEntry))
1158 while (win32_Module32Next (snapshot_module, &modEntry))
1159 if (modEntry.modBaseAddr == BaseAddress)
1160 {
1161 #ifdef UNICODE
1162 wcstombs (dll_name_ret, modEntry.szExePath, MAX_PATH + 1);
1163 #else
1164 strcpy (dll_name_ret, modEntry.szExePath);
1165 #endif
1166 found = 1;
1167 break;
1168 }
1169
1170 #ifdef _WIN32_WCE
1171 win32_CloseToolhelp32Snapshot (snapshot_module);
1172 #else
1173 CloseHandle (snapshot_module);
1174 #endif
1175 return found;
1176 }
1177
1178 static void
1179 handle_load_dll (void)
1180 {
1181 LOAD_DLL_DEBUG_INFO *event = &current_event.u.LoadDll;
1182 char dll_buf[MAX_PATH + 1];
1183 char *dll_name = NULL;
1184 CORE_ADDR load_addr;
1185
1186 dll_buf[0] = dll_buf[sizeof (dll_buf) - 1] = '\0';
1187
1188 /* Windows does not report the image name of the dlls in the debug
1189 event on attaches. We resort to iterating over the list of
1190 loaded dlls looking for a match by image base. */
1191 if (!psapi_get_dll_name (event->lpBaseOfDll, dll_buf))
1192 {
1193 if (!server_waiting)
1194 /* On some versions of Windows and Windows CE, we can't create
1195 toolhelp snapshots while the inferior is stopped in a
1196 LOAD_DLL_DEBUG_EVENT due to a dll load, but we can while
1197 Windows is reporting the already loaded dlls. */
1198 toolhelp_get_dll_name (event->lpBaseOfDll, dll_buf);
1199 }
1200
1201 dll_name = dll_buf;
1202
1203 if (*dll_name == '\0')
1204 dll_name = get_image_name (current_process_handle,
1205 event->lpImageName, event->fUnicode);
1206 if (!dll_name)
1207 return;
1208
1209 /* The symbols in a dll are offset by 0x1000, which is the
1210 offset from 0 of the first byte in an image - because
1211 of the file header and the section alignment. */
1212
1213 load_addr = (CORE_ADDR) (uintptr_t) event->lpBaseOfDll + 0x1000;
1214 win32_add_one_solib (dll_name, load_addr);
1215 }
1216
1217 static void
1218 handle_unload_dll (void)
1219 {
1220 CORE_ADDR load_addr =
1221 (CORE_ADDR) (uintptr_t) current_event.u.UnloadDll.lpBaseOfDll;
1222 load_addr += 0x1000;
1223 unloaded_dll (NULL, load_addr);
1224 }
1225
1226 static void
1227 handle_exception (struct target_waitstatus *ourstatus)
1228 {
1229 DWORD code = current_event.u.Exception.ExceptionRecord.ExceptionCode;
1230
1231 ourstatus->kind = TARGET_WAITKIND_STOPPED;
1232
1233 switch (code)
1234 {
1235 case EXCEPTION_ACCESS_VIOLATION:
1236 OUTMSG2 (("EXCEPTION_ACCESS_VIOLATION"));
1237 ourstatus->value.sig = GDB_SIGNAL_SEGV;
1238 break;
1239 case STATUS_STACK_OVERFLOW:
1240 OUTMSG2 (("STATUS_STACK_OVERFLOW"));
1241 ourstatus->value.sig = GDB_SIGNAL_SEGV;
1242 break;
1243 case STATUS_FLOAT_DENORMAL_OPERAND:
1244 OUTMSG2 (("STATUS_FLOAT_DENORMAL_OPERAND"));
1245 ourstatus->value.sig = GDB_SIGNAL_FPE;
1246 break;
1247 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
1248 OUTMSG2 (("EXCEPTION_ARRAY_BOUNDS_EXCEEDED"));
1249 ourstatus->value.sig = GDB_SIGNAL_FPE;
1250 break;
1251 case STATUS_FLOAT_INEXACT_RESULT:
1252 OUTMSG2 (("STATUS_FLOAT_INEXACT_RESULT"));
1253 ourstatus->value.sig = GDB_SIGNAL_FPE;
1254 break;
1255 case STATUS_FLOAT_INVALID_OPERATION:
1256 OUTMSG2 (("STATUS_FLOAT_INVALID_OPERATION"));
1257 ourstatus->value.sig = GDB_SIGNAL_FPE;
1258 break;
1259 case STATUS_FLOAT_OVERFLOW:
1260 OUTMSG2 (("STATUS_FLOAT_OVERFLOW"));
1261 ourstatus->value.sig = GDB_SIGNAL_FPE;
1262 break;
1263 case STATUS_FLOAT_STACK_CHECK:
1264 OUTMSG2 (("STATUS_FLOAT_STACK_CHECK"));
1265 ourstatus->value.sig = GDB_SIGNAL_FPE;
1266 break;
1267 case STATUS_FLOAT_UNDERFLOW:
1268 OUTMSG2 (("STATUS_FLOAT_UNDERFLOW"));
1269 ourstatus->value.sig = GDB_SIGNAL_FPE;
1270 break;
1271 case STATUS_FLOAT_DIVIDE_BY_ZERO:
1272 OUTMSG2 (("STATUS_FLOAT_DIVIDE_BY_ZERO"));
1273 ourstatus->value.sig = GDB_SIGNAL_FPE;
1274 break;
1275 case STATUS_INTEGER_DIVIDE_BY_ZERO:
1276 OUTMSG2 (("STATUS_INTEGER_DIVIDE_BY_ZERO"));
1277 ourstatus->value.sig = GDB_SIGNAL_FPE;
1278 break;
1279 case STATUS_INTEGER_OVERFLOW:
1280 OUTMSG2 (("STATUS_INTEGER_OVERFLOW"));
1281 ourstatus->value.sig = GDB_SIGNAL_FPE;
1282 break;
1283 case EXCEPTION_BREAKPOINT:
1284 OUTMSG2 (("EXCEPTION_BREAKPOINT"));
1285 ourstatus->value.sig = GDB_SIGNAL_TRAP;
1286 #ifdef _WIN32_WCE
1287 /* Remove the initial breakpoint. */
1288 check_breakpoints ((CORE_ADDR) (long) current_event
1289 .u.Exception.ExceptionRecord.ExceptionAddress);
1290 #endif
1291 break;
1292 case DBG_CONTROL_C:
1293 OUTMSG2 (("DBG_CONTROL_C"));
1294 ourstatus->value.sig = GDB_SIGNAL_INT;
1295 break;
1296 case DBG_CONTROL_BREAK:
1297 OUTMSG2 (("DBG_CONTROL_BREAK"));
1298 ourstatus->value.sig = GDB_SIGNAL_INT;
1299 break;
1300 case EXCEPTION_SINGLE_STEP:
1301 OUTMSG2 (("EXCEPTION_SINGLE_STEP"));
1302 ourstatus->value.sig = GDB_SIGNAL_TRAP;
1303 break;
1304 case EXCEPTION_ILLEGAL_INSTRUCTION:
1305 OUTMSG2 (("EXCEPTION_ILLEGAL_INSTRUCTION"));
1306 ourstatus->value.sig = GDB_SIGNAL_ILL;
1307 break;
1308 case EXCEPTION_PRIV_INSTRUCTION:
1309 OUTMSG2 (("EXCEPTION_PRIV_INSTRUCTION"));
1310 ourstatus->value.sig = GDB_SIGNAL_ILL;
1311 break;
1312 case EXCEPTION_NONCONTINUABLE_EXCEPTION:
1313 OUTMSG2 (("EXCEPTION_NONCONTINUABLE_EXCEPTION"));
1314 ourstatus->value.sig = GDB_SIGNAL_ILL;
1315 break;
1316 default:
1317 if (current_event.u.Exception.dwFirstChance)
1318 {
1319 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
1320 return;
1321 }
1322 OUTMSG2 (("gdbserver: unknown target exception 0x%08x at 0x%s",
1323 (unsigned) current_event.u.Exception.ExceptionRecord.ExceptionCode,
1324 phex_nz ((uintptr_t) current_event.u.Exception.ExceptionRecord.
1325 ExceptionAddress, sizeof (uintptr_t))));
1326 ourstatus->value.sig = GDB_SIGNAL_UNKNOWN;
1327 break;
1328 }
1329 OUTMSG2 (("\n"));
1330 last_sig = ourstatus->value.sig;
1331 }
1332
1333
1334 static void
1335 suspend_one_thread (struct inferior_list_entry *entry)
1336 {
1337 struct thread_info *thread = (struct thread_info *) entry;
1338 win32_thread_info *th = inferior_target_data (thread);
1339
1340 if (!th->suspended)
1341 {
1342 if (SuspendThread (th->h) == (DWORD) -1)
1343 {
1344 DWORD err = GetLastError ();
1345 OUTMSG (("warning: SuspendThread failed in suspend_one_thread, "
1346 "(error %d): %s\n", (int) err, strwinerror (err)));
1347 }
1348 else
1349 th->suspended = 1;
1350 }
1351 }
1352
1353 static void
1354 fake_breakpoint_event (void)
1355 {
1356 OUTMSG2(("fake_breakpoint_event\n"));
1357
1358 faked_breakpoint = 1;
1359
1360 memset (&current_event, 0, sizeof (current_event));
1361 current_event.dwThreadId = main_thread_id;
1362 current_event.dwDebugEventCode = EXCEPTION_DEBUG_EVENT;
1363 current_event.u.Exception.ExceptionRecord.ExceptionCode
1364 = EXCEPTION_BREAKPOINT;
1365
1366 for_each_inferior (&all_threads, suspend_one_thread);
1367 }
1368
1369 #ifdef _WIN32_WCE
1370 static int
1371 auto_delete_breakpoint (CORE_ADDR stop_pc)
1372 {
1373 return 1;
1374 }
1375 #endif
1376
1377 /* Get the next event from the child. */
1378
1379 static int
1380 get_child_debug_event (struct target_waitstatus *ourstatus)
1381 {
1382 ptid_t ptid;
1383
1384 last_sig = GDB_SIGNAL_0;
1385 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
1386
1387 /* Check if GDB sent us an interrupt request. */
1388 check_remote_input_interrupt_request ();
1389
1390 if (soft_interrupt_requested)
1391 {
1392 soft_interrupt_requested = 0;
1393 fake_breakpoint_event ();
1394 goto gotevent;
1395 }
1396
1397 #ifndef _WIN32_WCE
1398 attaching = 0;
1399 #else
1400 if (attaching)
1401 {
1402 /* WinCE doesn't set an initial breakpoint automatically. To
1403 stop the inferior, we flush all currently pending debug
1404 events -- the thread list and the dll list are always
1405 reported immediatelly without delay, then, we suspend all
1406 threads and pretend we saw a trap at the current PC of the
1407 main thread.
1408
1409 Contrary to desktop Windows, Windows CE *does* report the dll
1410 names on LOAD_DLL_DEBUG_EVENTs resulting from a
1411 DebugActiveProcess call. This limits the way we can detect
1412 if all the dlls have already been reported. If we get a real
1413 debug event before leaving attaching, the worst that will
1414 happen is the user will see a spurious breakpoint. */
1415
1416 current_event.dwDebugEventCode = 0;
1417 if (!WaitForDebugEvent (&current_event, 0))
1418 {
1419 OUTMSG2(("no attach events left\n"));
1420 fake_breakpoint_event ();
1421 attaching = 0;
1422 }
1423 else
1424 OUTMSG2(("got attach event\n"));
1425 }
1426 else
1427 #endif
1428 {
1429 /* Keep the wait time low enough for confortable remote
1430 interruption, but high enough so gdbserver doesn't become a
1431 bottleneck. */
1432 if (!WaitForDebugEvent (&current_event, 250))
1433 {
1434 DWORD e = GetLastError();
1435
1436 if (e == ERROR_PIPE_NOT_CONNECTED)
1437 {
1438 /* This will happen if the loader fails to succesfully
1439 load the application, e.g., if the main executable
1440 tries to pull in a non-existing export from a
1441 DLL. */
1442 ourstatus->kind = TARGET_WAITKIND_EXITED;
1443 ourstatus->value.integer = 1;
1444 return 1;
1445 }
1446
1447 return 0;
1448 }
1449 }
1450
1451 gotevent:
1452
1453 switch (current_event.dwDebugEventCode)
1454 {
1455 case CREATE_THREAD_DEBUG_EVENT:
1456 OUTMSG2 (("gdbserver: kernel event CREATE_THREAD_DEBUG_EVENT "
1457 "for pid=%u tid=%x)\n",
1458 (unsigned) current_event.dwProcessId,
1459 (unsigned) current_event.dwThreadId));
1460
1461 /* Record the existence of this thread. */
1462 child_add_thread (current_event.dwProcessId,
1463 current_event.dwThreadId,
1464 current_event.u.CreateThread.hThread,
1465 current_event.u.CreateThread.lpThreadLocalBase);
1466 break;
1467
1468 case EXIT_THREAD_DEBUG_EVENT:
1469 OUTMSG2 (("gdbserver: kernel event EXIT_THREAD_DEBUG_EVENT "
1470 "for pid=%u tid=%x\n",
1471 (unsigned) current_event.dwProcessId,
1472 (unsigned) current_event.dwThreadId));
1473 child_delete_thread (current_event.dwProcessId,
1474 current_event.dwThreadId);
1475
1476 current_inferior = (struct thread_info *) all_threads.head;
1477 return 1;
1478
1479 case CREATE_PROCESS_DEBUG_EVENT:
1480 OUTMSG2 (("gdbserver: kernel event CREATE_PROCESS_DEBUG_EVENT "
1481 "for pid=%u tid=%x\n",
1482 (unsigned) current_event.dwProcessId,
1483 (unsigned) current_event.dwThreadId));
1484 CloseHandle (current_event.u.CreateProcessInfo.hFile);
1485
1486 current_process_handle = current_event.u.CreateProcessInfo.hProcess;
1487 main_thread_id = current_event.dwThreadId;
1488
1489 ourstatus->kind = TARGET_WAITKIND_EXECD;
1490 ourstatus->value.execd_pathname = "Main executable";
1491
1492 /* Add the main thread. */
1493 child_add_thread (current_event.dwProcessId,
1494 main_thread_id,
1495 current_event.u.CreateProcessInfo.hThread,
1496 current_event.u.CreateProcessInfo.lpThreadLocalBase);
1497
1498 ourstatus->value.related_pid = debug_event_ptid (&current_event);
1499 #ifdef _WIN32_WCE
1500 if (!attaching)
1501 {
1502 /* Windows CE doesn't set the initial breakpoint
1503 automatically like the desktop versions of Windows do.
1504 We add it explicitly here. It will be removed as soon as
1505 it is hit. */
1506 set_breakpoint_at ((CORE_ADDR) (long) current_event.u
1507 .CreateProcessInfo.lpStartAddress,
1508 auto_delete_breakpoint);
1509 }
1510 #endif
1511 break;
1512
1513 case EXIT_PROCESS_DEBUG_EVENT:
1514 OUTMSG2 (("gdbserver: kernel event EXIT_PROCESS_DEBUG_EVENT "
1515 "for pid=%u tid=%x\n",
1516 (unsigned) current_event.dwProcessId,
1517 (unsigned) current_event.dwThreadId));
1518 ourstatus->kind = TARGET_WAITKIND_EXITED;
1519 ourstatus->value.integer = current_event.u.ExitProcess.dwExitCode;
1520 child_continue (DBG_CONTINUE, -1);
1521 CloseHandle (current_process_handle);
1522 current_process_handle = NULL;
1523 break;
1524
1525 case LOAD_DLL_DEBUG_EVENT:
1526 OUTMSG2 (("gdbserver: kernel event LOAD_DLL_DEBUG_EVENT "
1527 "for pid=%u tid=%x\n",
1528 (unsigned) current_event.dwProcessId,
1529 (unsigned) current_event.dwThreadId));
1530 CloseHandle (current_event.u.LoadDll.hFile);
1531 handle_load_dll ();
1532
1533 ourstatus->kind = TARGET_WAITKIND_LOADED;
1534 ourstatus->value.sig = GDB_SIGNAL_TRAP;
1535 break;
1536
1537 case UNLOAD_DLL_DEBUG_EVENT:
1538 OUTMSG2 (("gdbserver: kernel event UNLOAD_DLL_DEBUG_EVENT "
1539 "for pid=%u tid=%x\n",
1540 (unsigned) current_event.dwProcessId,
1541 (unsigned) current_event.dwThreadId));
1542 handle_unload_dll ();
1543 ourstatus->kind = TARGET_WAITKIND_LOADED;
1544 ourstatus->value.sig = GDB_SIGNAL_TRAP;
1545 break;
1546
1547 case EXCEPTION_DEBUG_EVENT:
1548 OUTMSG2 (("gdbserver: kernel event EXCEPTION_DEBUG_EVENT "
1549 "for pid=%u tid=%x\n",
1550 (unsigned) current_event.dwProcessId,
1551 (unsigned) current_event.dwThreadId));
1552 handle_exception (ourstatus);
1553 break;
1554
1555 case OUTPUT_DEBUG_STRING_EVENT:
1556 /* A message from the kernel (or Cygwin). */
1557 OUTMSG2 (("gdbserver: kernel event OUTPUT_DEBUG_STRING_EVENT "
1558 "for pid=%u tid=%x\n",
1559 (unsigned) current_event.dwProcessId,
1560 (unsigned) current_event.dwThreadId));
1561 handle_output_debug_string (ourstatus);
1562 break;
1563
1564 default:
1565 OUTMSG2 (("gdbserver: kernel event unknown "
1566 "for pid=%u tid=%x code=%x\n",
1567 (unsigned) current_event.dwProcessId,
1568 (unsigned) current_event.dwThreadId,
1569 (unsigned) current_event.dwDebugEventCode));
1570 break;
1571 }
1572
1573 ptid = debug_event_ptid (&current_event);
1574 current_inferior =
1575 (struct thread_info *) find_inferior_id (&all_threads, ptid);
1576 return 1;
1577 }
1578
1579 /* Wait for the inferior process to change state.
1580 STATUS will be filled in with a response code to send to GDB.
1581 Returns the signal which caused the process to stop. */
1582 static ptid_t
1583 win32_wait (ptid_t ptid, struct target_waitstatus *ourstatus, int options)
1584 {
1585 struct regcache *regcache;
1586
1587 while (1)
1588 {
1589 if (!get_child_debug_event (ourstatus))
1590 continue;
1591
1592 switch (ourstatus->kind)
1593 {
1594 case TARGET_WAITKIND_EXITED:
1595 OUTMSG2 (("Child exited with retcode = %x\n",
1596 ourstatus->value.integer));
1597 win32_clear_inferiors ();
1598 return pid_to_ptid (current_event.dwProcessId);
1599 case TARGET_WAITKIND_STOPPED:
1600 case TARGET_WAITKIND_LOADED:
1601 OUTMSG2 (("Child Stopped with signal = %d \n",
1602 ourstatus->value.sig));
1603
1604 regcache = get_thread_regcache (current_inferior, 1);
1605 child_fetch_inferior_registers (regcache, -1);
1606
1607 if (ourstatus->kind == TARGET_WAITKIND_LOADED
1608 && !server_waiting)
1609 {
1610 /* When gdb connects, we want to be stopped at the
1611 initial breakpoint, not in some dll load event. */
1612 child_continue (DBG_CONTINUE, -1);
1613 break;
1614 }
1615
1616 /* We don't expose _LOADED events to gdbserver core. See
1617 the `dlls_changed' global. */
1618 if (ourstatus->kind == TARGET_WAITKIND_LOADED)
1619 ourstatus->kind = TARGET_WAITKIND_STOPPED;
1620
1621 return debug_event_ptid (&current_event);
1622 default:
1623 OUTMSG (("Ignoring unknown internal event, %d\n", ourstatus->kind));
1624 /* fall-through */
1625 case TARGET_WAITKIND_SPURIOUS:
1626 case TARGET_WAITKIND_EXECD:
1627 /* do nothing, just continue */
1628 child_continue (DBG_CONTINUE, -1);
1629 break;
1630 }
1631 }
1632 }
1633
1634 /* Fetch registers from the inferior process.
1635 If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO. */
1636 static void
1637 win32_fetch_inferior_registers (struct regcache *regcache, int regno)
1638 {
1639 child_fetch_inferior_registers (regcache, regno);
1640 }
1641
1642 /* Store registers to the inferior process.
1643 If REGNO is -1, store all registers; otherwise, store at least REGNO. */
1644 static void
1645 win32_store_inferior_registers (struct regcache *regcache, int regno)
1646 {
1647 child_store_inferior_registers (regcache, regno);
1648 }
1649
1650 /* Read memory from the inferior process. This should generally be
1651 called through read_inferior_memory, which handles breakpoint shadowing.
1652 Read LEN bytes at MEMADDR into a buffer at MYADDR. */
1653 static int
1654 win32_read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
1655 {
1656 return child_xfer_memory (memaddr, (char *) myaddr, len, 0, 0) != len;
1657 }
1658
1659 /* Write memory to the inferior process. This should generally be
1660 called through write_inferior_memory, which handles breakpoint shadowing.
1661 Write LEN bytes from the buffer at MYADDR to MEMADDR.
1662 Returns 0 on success and errno on failure. */
1663 static int
1664 win32_write_inferior_memory (CORE_ADDR memaddr, const unsigned char *myaddr,
1665 int len)
1666 {
1667 return child_xfer_memory (memaddr, (char *) myaddr, len, 1, 0) != len;
1668 }
1669
1670 /* Send an interrupt request to the inferior process. */
1671 static void
1672 win32_request_interrupt (void)
1673 {
1674 winapi_DebugBreakProcess DebugBreakProcess;
1675 winapi_GenerateConsoleCtrlEvent GenerateConsoleCtrlEvent;
1676
1677 #ifdef _WIN32_WCE
1678 HMODULE dll = GetModuleHandle (_T("COREDLL.DLL"));
1679 #else
1680 HMODULE dll = GetModuleHandle (_T("KERNEL32.DLL"));
1681 #endif
1682
1683 GenerateConsoleCtrlEvent = GETPROCADDRESS (dll, GenerateConsoleCtrlEvent);
1684
1685 if (GenerateConsoleCtrlEvent != NULL
1686 && GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT, current_process_id))
1687 return;
1688
1689 /* GenerateConsoleCtrlEvent can fail if process id being debugged is
1690 not a process group id.
1691 Fallback to XP/Vista 'DebugBreakProcess', which generates a
1692 breakpoint exception in the interior process. */
1693
1694 DebugBreakProcess = GETPROCADDRESS (dll, DebugBreakProcess);
1695
1696 if (DebugBreakProcess != NULL
1697 && DebugBreakProcess (current_process_handle))
1698 return;
1699
1700 /* Last resort, suspend all threads manually. */
1701 soft_interrupt_requested = 1;
1702 }
1703
1704 #ifdef _WIN32_WCE
1705 int
1706 win32_error_to_fileio_error (DWORD err)
1707 {
1708 switch (err)
1709 {
1710 case ERROR_BAD_PATHNAME:
1711 case ERROR_FILE_NOT_FOUND:
1712 case ERROR_INVALID_NAME:
1713 case ERROR_PATH_NOT_FOUND:
1714 return FILEIO_ENOENT;
1715 case ERROR_CRC:
1716 case ERROR_IO_DEVICE:
1717 case ERROR_OPEN_FAILED:
1718 return FILEIO_EIO;
1719 case ERROR_INVALID_HANDLE:
1720 return FILEIO_EBADF;
1721 case ERROR_ACCESS_DENIED:
1722 case ERROR_SHARING_VIOLATION:
1723 return FILEIO_EACCES;
1724 case ERROR_NOACCESS:
1725 return FILEIO_EFAULT;
1726 case ERROR_BUSY:
1727 return FILEIO_EBUSY;
1728 case ERROR_ALREADY_EXISTS:
1729 case ERROR_FILE_EXISTS:
1730 return FILEIO_EEXIST;
1731 case ERROR_BAD_DEVICE:
1732 return FILEIO_ENODEV;
1733 case ERROR_DIRECTORY:
1734 return FILEIO_ENOTDIR;
1735 case ERROR_FILENAME_EXCED_RANGE:
1736 case ERROR_INVALID_DATA:
1737 case ERROR_INVALID_PARAMETER:
1738 case ERROR_NEGATIVE_SEEK:
1739 return FILEIO_EINVAL;
1740 case ERROR_TOO_MANY_OPEN_FILES:
1741 return FILEIO_EMFILE;
1742 case ERROR_HANDLE_DISK_FULL:
1743 case ERROR_DISK_FULL:
1744 return FILEIO_ENOSPC;
1745 case ERROR_WRITE_PROTECT:
1746 return FILEIO_EROFS;
1747 case ERROR_NOT_SUPPORTED:
1748 return FILEIO_ENOSYS;
1749 }
1750
1751 return FILEIO_EUNKNOWN;
1752 }
1753
1754 static void
1755 wince_hostio_last_error (char *buf)
1756 {
1757 DWORD winerr = GetLastError ();
1758 int fileio_err = win32_error_to_fileio_error (winerr);
1759 sprintf (buf, "F-1,%x", fileio_err);
1760 }
1761 #endif
1762
1763 /* Write Windows OS Thread Information Block address. */
1764
1765 static int
1766 win32_get_tib_address (ptid_t ptid, CORE_ADDR *addr)
1767 {
1768 win32_thread_info *th;
1769 th = thread_rec (ptid, 0);
1770 if (th == NULL)
1771 return 0;
1772 if (addr != NULL)
1773 *addr = th->thread_local_base;
1774 return 1;
1775 }
1776
1777 static struct target_ops win32_target_ops = {
1778 win32_create_inferior,
1779 win32_attach,
1780 win32_kill,
1781 win32_detach,
1782 win32_mourn,
1783 win32_join,
1784 win32_thread_alive,
1785 win32_resume,
1786 win32_wait,
1787 win32_fetch_inferior_registers,
1788 win32_store_inferior_registers,
1789 NULL, /* prepare_to_access_memory */
1790 NULL, /* done_accessing_memory */
1791 win32_read_inferior_memory,
1792 win32_write_inferior_memory,
1793 NULL, /* lookup_symbols */
1794 win32_request_interrupt,
1795 NULL, /* read_auxv */
1796 win32_insert_point,
1797 win32_remove_point,
1798 win32_stopped_by_watchpoint,
1799 win32_stopped_data_address,
1800 NULL, /* read_offsets */
1801 NULL, /* get_tls_address */
1802 NULL, /* qxfer_spu */
1803 #ifdef _WIN32_WCE
1804 wince_hostio_last_error,
1805 #else
1806 hostio_last_error_from_errno,
1807 #endif
1808 NULL, /* qxfer_osdata */
1809 NULL, /* qxfer_siginfo */
1810 NULL, /* supports_non_stop */
1811 NULL, /* async */
1812 NULL, /* start_non_stop */
1813 NULL, /* supports_multi_process */
1814 NULL, /* handle_monitor_command */
1815 NULL, /* core_of_thread */
1816 NULL, /* read_loadmap */
1817 NULL, /* process_qsupported */
1818 NULL, /* supports_tracepoints */
1819 NULL, /* read_pc */
1820 NULL, /* write_pc */
1821 NULL, /* thread_stopped */
1822 win32_get_tib_address
1823 };
1824
1825 /* Initialize the Win32 backend. */
1826 void
1827 initialize_low (void)
1828 {
1829 set_target_ops (&win32_target_ops);
1830 if (the_low_target.breakpoint != NULL)
1831 set_breakpoint_data (the_low_target.breakpoint,
1832 the_low_target.breakpoint_len);
1833 the_low_target.arch_setup ();
1834 }
This page took 0.065824 seconds and 5 git commands to generate.