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