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