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