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