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