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