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