]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdbserver/win32-low.cc
Allow ASLR to be disabled on Windows
[thirdparty/binutils-gdb.git] / gdbserver / win32-low.cc
1 /* Low level interface to Windows debugging, for gdbserver.
2 Copyright (C) 2006-2022 Free Software Foundation, Inc.
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
10 the Free Software Foundation; either version 3 of the License, or
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
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include "server.h"
22 #include "regcache.h"
23 #include "gdb/fileio.h"
24 #include "mem-break.h"
25 #include "win32-low.h"
26 #include "gdbthread.h"
27 #include "dll.h"
28 #include "hostio.h"
29 #include <windows.h>
30 #include <winnt.h>
31 #include <imagehlp.h>
32 #include <tlhelp32.h>
33 #include <psapi.h>
34 #include <process.h>
35 #include "gdbsupport/gdb_tilde_expand.h"
36 #include "gdbsupport/common-inferior.h"
37 #include "gdbsupport/gdb_wait.h"
38
39 using namespace windows_nat;
40
41 /* See win32-low.h. */
42 windows_process_info windows_process;
43
44 #ifndef USE_WIN32API
45 #include <sys/cygwin.h>
46 #endif
47
48 #define OUTMSG(X) do { printf X; fflush (stderr); } while (0)
49
50 #define OUTMSG2(X) \
51 do \
52 { \
53 if (debug_threads) \
54 { \
55 printf X; \
56 fflush (stderr); \
57 } \
58 } while (0)
59
60 #ifndef _T
61 #define _T(x) TEXT (x)
62 #endif
63
64 #ifndef COUNTOF
65 #define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0]))
66 #endif
67
68 int using_threads = 1;
69
70 /* Globals. */
71 static int attaching = 0;
72
73 /* A status that hasn't been reported to the core yet, and so
74 win32_wait should return it next, instead of fetching the next
75 debug event off the win32 API. */
76 static struct target_waitstatus cached_status;
77
78 /* Non zero if an interrupt request is to be satisfied by suspending
79 all threads. */
80 static int soft_interrupt_requested = 0;
81
82 /* Non zero if the inferior is stopped in a simulated breakpoint done
83 by suspending all the threads. */
84 static int faked_breakpoint = 0;
85
86 /* True if current_process_handle needs to be closed. */
87 static bool open_process_used = false;
88
89 const struct target_desc *win32_tdesc;
90 #ifdef __x86_64__
91 const struct target_desc *wow64_win32_tdesc;
92 #endif
93
94 #define NUM_REGS (the_low_target.num_regs ())
95
96 /* Get the thread ID from the current selected inferior (the current
97 thread). */
98 static ptid_t
99 current_thread_ptid (void)
100 {
101 return current_ptid;
102 }
103
104 /* The current debug event from WaitForDebugEvent. */
105 static ptid_t
106 debug_event_ptid (DEBUG_EVENT *event)
107 {
108 return ptid_t (event->dwProcessId, event->dwThreadId, 0);
109 }
110
111 /* Get the thread context of the thread associated with TH. */
112
113 static void
114 win32_get_thread_context (windows_thread_info *th)
115 {
116 #ifdef __x86_64__
117 if (windows_process.wow64_process)
118 memset (&th->wow64_context, 0, sizeof (WOW64_CONTEXT));
119 else
120 #endif
121 memset (&th->context, 0, sizeof (CONTEXT));
122 (*the_low_target.get_thread_context) (th);
123 }
124
125 /* Set the thread context of the thread associated with TH. */
126
127 static void
128 win32_set_thread_context (windows_thread_info *th)
129 {
130 #ifdef __x86_64__
131 if (windows_process.wow64_process)
132 Wow64SetThreadContext (th->h, &th->wow64_context);
133 else
134 #endif
135 SetThreadContext (th->h, &th->context);
136 }
137
138 /* Set the thread context of the thread associated with TH. */
139
140 static void
141 win32_prepare_to_resume (windows_thread_info *th)
142 {
143 if (the_low_target.prepare_to_resume != NULL)
144 (*the_low_target.prepare_to_resume) (th);
145 }
146
147 /* See win32-low.h. */
148
149 void
150 win32_require_context (windows_thread_info *th)
151 {
152 DWORD context_flags;
153 #ifdef __x86_64__
154 if (windows_process.wow64_process)
155 context_flags = th->wow64_context.ContextFlags;
156 else
157 #endif
158 context_flags = th->context.ContextFlags;
159 if (context_flags == 0)
160 {
161 th->suspend ();
162 win32_get_thread_context (th);
163 }
164 }
165
166 /* See nat/windows-nat.h. */
167
168 windows_thread_info *
169 windows_nat::windows_process_info::thread_rec
170 (ptid_t ptid, thread_disposition_type disposition)
171 {
172 thread_info *thread = find_thread_ptid (ptid);
173 if (thread == NULL)
174 return NULL;
175
176 windows_thread_info *th = (windows_thread_info *) thread_target_data (thread);
177 if (disposition != DONT_INVALIDATE_CONTEXT)
178 win32_require_context (th);
179 return th;
180 }
181
182 /* Add a thread to the thread list. */
183 static windows_thread_info *
184 child_add_thread (DWORD pid, DWORD tid, HANDLE h, void *tlb)
185 {
186 windows_thread_info *th;
187 ptid_t ptid = ptid_t (pid, tid, 0);
188
189 if ((th = windows_process.thread_rec (ptid, DONT_INVALIDATE_CONTEXT)))
190 return th;
191
192 CORE_ADDR base = (CORE_ADDR) (uintptr_t) tlb;
193 #ifdef __x86_64__
194 /* For WOW64 processes, this is actually the pointer to the 64bit TIB,
195 and the 32bit TIB is exactly 2 pages after it. */
196 if (windows_process.wow64_process)
197 base += 2 * 4096; /* page size = 4096 */
198 #endif
199 th = new windows_thread_info (tid, h, base);
200
201 add_thread (ptid, th);
202
203 if (the_low_target.thread_added != NULL)
204 (*the_low_target.thread_added) (th);
205
206 return th;
207 }
208
209 /* Delete a thread from the list of threads. */
210 static void
211 delete_thread_info (thread_info *thread)
212 {
213 windows_thread_info *th = (windows_thread_info *) thread_target_data (thread);
214
215 remove_thread (thread);
216 delete th;
217 }
218
219 /* Delete a thread from the list of threads. */
220 static void
221 child_delete_thread (DWORD pid, DWORD tid)
222 {
223 /* If the last thread is exiting, just return. */
224 if (all_threads.size () == 1)
225 return;
226
227 thread_info *thread = find_thread_ptid (ptid_t (pid, tid));
228 if (thread == NULL)
229 return;
230
231 delete_thread_info (thread);
232 }
233
234 /* These watchpoint related wrapper functions simply pass on the function call
235 if the low target has registered a corresponding function. */
236
237 bool
238 win32_process_target::supports_z_point_type (char z_type)
239 {
240 return (z_type == Z_PACKET_SW_BP
241 || (the_low_target.supports_z_point_type != NULL
242 && the_low_target.supports_z_point_type (z_type)));
243 }
244
245 int
246 win32_process_target::insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
247 int size, raw_breakpoint *bp)
248 {
249 if (type == raw_bkpt_type_sw)
250 return insert_memory_breakpoint (bp);
251 else if (the_low_target.insert_point != NULL)
252 return the_low_target.insert_point (type, addr, size, bp);
253 else
254 /* Unsupported (see target.h). */
255 return 1;
256 }
257
258 int
259 win32_process_target::remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
260 int size, raw_breakpoint *bp)
261 {
262 if (type == raw_bkpt_type_sw)
263 return remove_memory_breakpoint (bp);
264 else if (the_low_target.remove_point != NULL)
265 return the_low_target.remove_point (type, addr, size, bp);
266 else
267 /* Unsupported (see target.h). */
268 return 1;
269 }
270
271 bool
272 win32_process_target::stopped_by_watchpoint ()
273 {
274 if (the_low_target.stopped_by_watchpoint != NULL)
275 return the_low_target.stopped_by_watchpoint ();
276 else
277 return false;
278 }
279
280 CORE_ADDR
281 win32_process_target::stopped_data_address ()
282 {
283 if (the_low_target.stopped_data_address != NULL)
284 return the_low_target.stopped_data_address ();
285 else
286 return 0;
287 }
288
289
290 /* Transfer memory from/to the debugged process. */
291 static int
292 child_xfer_memory (CORE_ADDR memaddr, char *our, int len,
293 int write, process_stratum_target *target)
294 {
295 BOOL success;
296 SIZE_T done = 0;
297 DWORD lasterror = 0;
298 uintptr_t addr = (uintptr_t) memaddr;
299
300 if (write)
301 {
302 success = WriteProcessMemory (windows_process.handle, (LPVOID) addr,
303 (LPCVOID) our, len, &done);
304 if (!success)
305 lasterror = GetLastError ();
306 FlushInstructionCache (windows_process.handle, (LPCVOID) addr, len);
307 }
308 else
309 {
310 success = ReadProcessMemory (windows_process.handle, (LPCVOID) addr,
311 (LPVOID) our, len, &done);
312 if (!success)
313 lasterror = GetLastError ();
314 }
315 if (!success && lasterror == ERROR_PARTIAL_COPY && done > 0)
316 return done;
317 else
318 return success ? done : -1;
319 }
320
321 /* Clear out any old thread list and reinitialize it to a pristine
322 state. */
323 static void
324 child_init_thread_list (void)
325 {
326 for_each_thread (delete_thread_info);
327 }
328
329 /* Zero during the child initialization phase, and nonzero otherwise. */
330
331 static int child_initialization_done = 0;
332
333 static void
334 do_initial_child_stuff (HANDLE proch, DWORD pid, int attached)
335 {
336 struct process_info *proc;
337
338 windows_process.last_sig = GDB_SIGNAL_0;
339 windows_process.handle = proch;
340 windows_process.main_thread_id = 0;
341
342 soft_interrupt_requested = 0;
343 faked_breakpoint = 0;
344 open_process_used = true;
345
346 memset (&windows_process.current_event, 0,
347 sizeof (windows_process.current_event));
348
349 #ifdef __x86_64__
350 BOOL wow64;
351 if (!IsWow64Process (proch, &wow64))
352 {
353 DWORD err = GetLastError ();
354 error ("Check if WOW64 process failed (error %d): %s\n",
355 (int) err, strwinerror (err));
356 }
357 windows_process.wow64_process = wow64;
358
359 if (windows_process.wow64_process
360 && (Wow64GetThreadContext == nullptr
361 || Wow64SetThreadContext == nullptr))
362 error ("WOW64 debugging is not supported on this system.\n");
363
364 windows_process.ignore_first_breakpoint
365 = !attached && windows_process.wow64_process;
366 #endif
367
368 proc = add_process (pid, attached);
369 #ifdef __x86_64__
370 if (windows_process.wow64_process)
371 proc->tdesc = wow64_win32_tdesc;
372 else
373 #endif
374 proc->tdesc = win32_tdesc;
375 child_init_thread_list ();
376 child_initialization_done = 0;
377
378 if (the_low_target.initial_stuff != NULL)
379 (*the_low_target.initial_stuff) ();
380
381 cached_status.set_ignore ();
382
383 /* Flush all currently pending debug events (thread and dll list) up
384 to the initial breakpoint. */
385 while (1)
386 {
387 struct target_waitstatus status;
388
389 the_target->wait (minus_one_ptid, &status, 0);
390
391 /* Note win32_wait doesn't return thread events. */
392 if (status.kind () != TARGET_WAITKIND_LOADED)
393 {
394 cached_status = status;
395 break;
396 }
397
398 {
399 struct thread_resume resume;
400
401 resume.thread = minus_one_ptid;
402 resume.kind = resume_continue;
403 resume.sig = 0;
404
405 the_target->resume (&resume, 1);
406 }
407 }
408
409 /* Now that the inferior has been started and all DLLs have been mapped,
410 we can iterate over all DLLs and load them in.
411
412 We avoid doing it any earlier because, on certain versions of Windows,
413 LOAD_DLL_DEBUG_EVENTs are sometimes not complete. In particular,
414 we have seen on Windows 8.1 that the ntdll.dll load event does not
415 include the DLL name, preventing us from creating an associated SO.
416 A possible explanation is that ntdll.dll might be mapped before
417 the SO info gets created by the Windows system -- ntdll.dll is
418 the first DLL to be reported via LOAD_DLL_DEBUG_EVENT and other DLLs
419 do not seem to suffer from that problem.
420
421 Rather than try to work around this sort of issue, it is much
422 simpler to just ignore DLL load/unload events during the startup
423 phase, and then process them all in one batch now. */
424 windows_process.add_all_dlls ();
425
426 child_initialization_done = 1;
427 }
428
429 /* Resume all artificially suspended threads if we are continuing
430 execution. */
431 static void
432 continue_one_thread (thread_info *thread, int thread_id)
433 {
434 windows_thread_info *th = (windows_thread_info *) thread_target_data (thread);
435
436 if (thread_id == -1 || thread_id == th->tid)
437 {
438 win32_prepare_to_resume (th);
439
440 if (th->suspended)
441 {
442 DWORD *context_flags;
443 #ifdef __x86_64__
444 if (windows_process.wow64_process)
445 context_flags = &th->wow64_context.ContextFlags;
446 else
447 #endif
448 context_flags = &th->context.ContextFlags;
449 if (*context_flags)
450 {
451 win32_set_thread_context (th);
452 *context_flags = 0;
453 }
454
455 th->resume ();
456 }
457 }
458 }
459
460 static BOOL
461 child_continue (DWORD continue_status, int thread_id)
462 {
463 windows_process.desired_stop_thread_id = thread_id;
464 if (windows_process.matching_pending_stop (debug_threads))
465 return TRUE;
466
467 /* The inferior will only continue after the ContinueDebugEvent
468 call. */
469 for_each_thread ([&] (thread_info *thread)
470 {
471 continue_one_thread (thread, thread_id);
472 });
473 faked_breakpoint = 0;
474
475 return continue_last_debug_event (continue_status, debug_threads);
476 }
477
478 /* Fetch register(s) from the current thread context. */
479 static void
480 child_fetch_inferior_registers (struct regcache *regcache, int r)
481 {
482 int regno;
483 windows_thread_info *th
484 = windows_process.thread_rec (current_thread_ptid (),
485 INVALIDATE_CONTEXT);
486 if (r == -1 || r > NUM_REGS)
487 child_fetch_inferior_registers (regcache, NUM_REGS);
488 else
489 for (regno = 0; regno < r; regno++)
490 (*the_low_target.fetch_inferior_register) (regcache, th, regno);
491 }
492
493 /* Store a new register value into the current thread context. We don't
494 change the program's context until later, when we resume it. */
495 static void
496 child_store_inferior_registers (struct regcache *regcache, int r)
497 {
498 int regno;
499 windows_thread_info *th
500 = windows_process.thread_rec (current_thread_ptid (),
501 INVALIDATE_CONTEXT);
502 if (r == -1 || r == 0 || r > NUM_REGS)
503 child_store_inferior_registers (regcache, NUM_REGS);
504 else
505 for (regno = 0; regno < r; regno++)
506 (*the_low_target.store_inferior_register) (regcache, th, regno);
507 }
508
509 /* Map the Windows error number in ERROR to a locale-dependent error
510 message string and return a pointer to it. Typically, the values
511 for ERROR come from GetLastError.
512
513 The string pointed to shall not be modified by the application,
514 but may be overwritten by a subsequent call to strwinerror
515
516 The strwinerror function does not change the current setting
517 of GetLastError. */
518
519 char *
520 strwinerror (DWORD error)
521 {
522 static char buf[1024];
523 TCHAR *msgbuf;
524 DWORD lasterr = GetLastError ();
525 DWORD chars = FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
526 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
527 NULL,
528 error,
529 0, /* Default language */
530 (LPTSTR) &msgbuf,
531 0,
532 NULL);
533 if (chars != 0)
534 {
535 /* If there is an \r\n appended, zap it. */
536 if (chars >= 2
537 && msgbuf[chars - 2] == '\r'
538 && msgbuf[chars - 1] == '\n')
539 {
540 chars -= 2;
541 msgbuf[chars] = 0;
542 }
543
544 if (chars > ((COUNTOF (buf)) - 1))
545 {
546 chars = COUNTOF (buf) - 1;
547 msgbuf [chars] = 0;
548 }
549
550 #ifdef UNICODE
551 wcstombs (buf, msgbuf, chars + 1);
552 #else
553 strncpy (buf, msgbuf, chars + 1);
554 #endif
555 LocalFree (msgbuf);
556 }
557 else
558 sprintf (buf, "unknown win32 error (%u)", (unsigned) error);
559
560 SetLastError (lasterr);
561 return buf;
562 }
563
564 static BOOL
565 create_process (const char *program, char *args,
566 DWORD flags, PROCESS_INFORMATION *pi)
567 {
568 const std::string &inferior_cwd = get_inferior_cwd ();
569 BOOL ret;
570 size_t argslen, proglen;
571
572 proglen = strlen (program) + 1;
573 argslen = strlen (args) + proglen;
574
575 STARTUPINFOA si = { sizeof (STARTUPINFOA) };
576 char *program_and_args = (char *) alloca (argslen + 1);
577
578 strcpy (program_and_args, program);
579 strcat (program_and_args, " ");
580 strcat (program_and_args, args);
581 ret = create_process (program, /* image name */
582 program_and_args, /* command line */
583 flags, /* start flags */
584 NULL, /* environment */
585 /* current directory */
586 (inferior_cwd.empty ()
587 ? NULL
588 : gdb_tilde_expand (inferior_cwd.c_str ()).c_str()),
589 get_client_state ().disable_randomization,
590 &si, /* start info */
591 pi); /* proc info */
592
593 return ret;
594 }
595
596 /* Start a new process.
597 PROGRAM is the program name.
598 PROGRAM_ARGS is the vector containing the inferior's args.
599 Returns the new PID on success, -1 on failure. Registers the new
600 process with the process list. */
601 int
602 win32_process_target::create_inferior (const char *program,
603 const std::vector<char *> &program_args)
604 {
605 client_state &cs = get_client_state ();
606 #ifndef USE_WIN32API
607 char real_path[PATH_MAX];
608 char *orig_path, *new_path, *path_ptr;
609 #endif
610 BOOL ret;
611 DWORD flags;
612 PROCESS_INFORMATION pi;
613 DWORD err;
614 std::string str_program_args = construct_inferior_arguments (program_args);
615 char *args = (char *) str_program_args.c_str ();
616
617 /* win32_wait needs to know we're not attaching. */
618 attaching = 0;
619
620 if (!program)
621 error ("No executable specified, specify executable to debug.\n");
622
623 flags = DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS;
624
625 #ifndef USE_WIN32API
626 orig_path = NULL;
627 path_ptr = getenv ("PATH");
628 if (path_ptr)
629 {
630 int size = cygwin_conv_path_list (CCP_POSIX_TO_WIN_A, path_ptr, NULL, 0);
631 orig_path = (char *) alloca (strlen (path_ptr) + 1);
632 new_path = (char *) alloca (size);
633 strcpy (orig_path, path_ptr);
634 cygwin_conv_path_list (CCP_POSIX_TO_WIN_A, path_ptr, new_path, size);
635 setenv ("PATH", new_path, 1);
636 }
637 cygwin_conv_path (CCP_POSIX_TO_WIN_A, program, real_path, PATH_MAX);
638 program = real_path;
639 #endif
640
641 OUTMSG2 (("Command line is \"%s %s\"\n", program, args));
642
643 #ifdef CREATE_NEW_PROCESS_GROUP
644 flags |= CREATE_NEW_PROCESS_GROUP;
645 #endif
646
647 ret = create_process (program, args, flags, &pi);
648 err = GetLastError ();
649 if (!ret && err == ERROR_FILE_NOT_FOUND)
650 {
651 char *exename = (char *) alloca (strlen (program) + 5);
652 strcat (strcpy (exename, program), ".exe");
653 ret = create_process (exename, args, flags, &pi);
654 err = GetLastError ();
655 }
656
657 #ifndef USE_WIN32API
658 if (orig_path)
659 setenv ("PATH", orig_path, 1);
660 #endif
661
662 if (!ret)
663 {
664 error ("Error creating process \"%s %s\", (error %d): %s\n",
665 program, args, (int) err, strwinerror (err));
666 }
667 else
668 {
669 OUTMSG2 (("Process created: %s %s\n", program, (char *) args));
670 }
671
672 CloseHandle (pi.hThread);
673
674 do_initial_child_stuff (pi.hProcess, pi.dwProcessId, 0);
675
676 /* Wait till we are at 1st instruction in program, return new pid
677 (assuming success). */
678 cs.last_ptid = wait (ptid_t (pi.dwProcessId), &cs.last_status, 0);
679
680 /* Necessary for handle_v_kill. */
681 signal_pid = pi.dwProcessId;
682
683 return pi.dwProcessId;
684 }
685
686 /* Attach to a running process.
687 PID is the process ID to attach to, specified by the user
688 or a higher layer. */
689 int
690 win32_process_target::attach (unsigned long pid)
691 {
692 HANDLE h;
693 DWORD err;
694
695 h = OpenProcess (PROCESS_ALL_ACCESS, FALSE, pid);
696 if (h != NULL)
697 {
698 if (DebugActiveProcess (pid))
699 {
700 DebugSetProcessKillOnExit (FALSE);
701
702 /* win32_wait needs to know we're attaching. */
703 attaching = 1;
704 do_initial_child_stuff (h, pid, 1);
705 return 0;
706 }
707
708 CloseHandle (h);
709 }
710
711 err = GetLastError ();
712 error ("Attach to process failed (error %d): %s\n",
713 (int) err, strwinerror (err));
714 }
715
716 /* See nat/windows-nat.h. */
717
718 int
719 windows_nat::windows_process_info::handle_output_debug_string
720 (struct target_waitstatus *ourstatus)
721 {
722 #define READ_BUFFER_LEN 1024
723 CORE_ADDR addr;
724 char s[READ_BUFFER_LEN + 1] = { 0 };
725 DWORD nbytes = current_event.u.DebugString.nDebugStringLength;
726
727 if (nbytes == 0)
728 return 0;
729
730 if (nbytes > READ_BUFFER_LEN)
731 nbytes = READ_BUFFER_LEN;
732
733 addr = (CORE_ADDR) (size_t) current_event.u.DebugString.lpDebugStringData;
734
735 if (current_event.u.DebugString.fUnicode)
736 {
737 /* The event tells us how many bytes, not chars, even
738 in Unicode. */
739 WCHAR buffer[(READ_BUFFER_LEN + 1) / sizeof (WCHAR)] = { 0 };
740 if (read_inferior_memory (addr, (unsigned char *) buffer, nbytes) != 0)
741 return 0;
742 wcstombs (s, buffer, (nbytes + 1) / sizeof (WCHAR));
743 }
744 else
745 {
746 if (read_inferior_memory (addr, (unsigned char *) s, nbytes) != 0)
747 return 0;
748 }
749
750 if (!startswith (s, "cYg"))
751 {
752 if (!server_waiting)
753 {
754 OUTMSG2(("%s", s));
755 return 0;
756 }
757
758 monitor_output (s);
759 }
760 #undef READ_BUFFER_LEN
761
762 return 0;
763 }
764
765 static void
766 win32_clear_inferiors (void)
767 {
768 if (open_process_used)
769 {
770 CloseHandle (windows_process.handle);
771 open_process_used = false;
772 }
773
774 for_each_thread (delete_thread_info);
775 windows_process.siginfo_er.ExceptionCode = 0;
776 clear_inferiors ();
777 }
778
779 /* Implementation of target_ops::kill. */
780
781 int
782 win32_process_target::kill (process_info *process)
783 {
784 TerminateProcess (windows_process.handle, 0);
785 for (;;)
786 {
787 if (!child_continue (DBG_CONTINUE, -1))
788 break;
789 if (!wait_for_debug_event (&windows_process.current_event, INFINITE))
790 break;
791 if (windows_process.current_event.dwDebugEventCode
792 == EXIT_PROCESS_DEBUG_EVENT)
793 break;
794 else if (windows_process.current_event.dwDebugEventCode
795 == OUTPUT_DEBUG_STRING_EVENT)
796 windows_process.handle_output_debug_string (nullptr);
797 }
798
799 win32_clear_inferiors ();
800
801 remove_process (process);
802 return 0;
803 }
804
805 /* Implementation of target_ops::detach. */
806
807 int
808 win32_process_target::detach (process_info *process)
809 {
810 struct thread_resume resume;
811 resume.thread = minus_one_ptid;
812 resume.kind = resume_continue;
813 resume.sig = 0;
814 this->resume (&resume, 1);
815
816 if (!DebugActiveProcessStop (process->pid))
817 return -1;
818
819 DebugSetProcessKillOnExit (FALSE);
820 remove_process (process);
821
822 win32_clear_inferiors ();
823 return 0;
824 }
825
826 void
827 win32_process_target::mourn (struct process_info *process)
828 {
829 remove_process (process);
830 }
831
832 /* Implementation of target_ops::join. */
833
834 void
835 win32_process_target::join (int pid)
836 {
837 HANDLE h = OpenProcess (PROCESS_ALL_ACCESS, FALSE, pid);
838 if (h != NULL)
839 {
840 WaitForSingleObject (h, INFINITE);
841 CloseHandle (h);
842 }
843 }
844
845 /* Return true iff the thread with thread ID TID is alive. */
846 bool
847 win32_process_target::thread_alive (ptid_t ptid)
848 {
849 /* Our thread list is reliable; don't bother to poll target
850 threads. */
851 return find_thread_ptid (ptid) != NULL;
852 }
853
854 /* Resume the inferior process. RESUME_INFO describes how we want
855 to resume. */
856 void
857 win32_process_target::resume (thread_resume *resume_info, size_t n)
858 {
859 DWORD tid;
860 enum gdb_signal sig;
861 int step;
862 windows_thread_info *th;
863 DWORD continue_status = DBG_CONTINUE;
864 ptid_t ptid;
865
866 /* This handles the very limited set of resume packets that GDB can
867 currently produce. */
868
869 if (n == 1 && resume_info[0].thread == minus_one_ptid)
870 tid = -1;
871 else if (n > 1)
872 tid = -1;
873 else
874 /* Yes, we're ignoring resume_info[0].thread. It'd be tricky to make
875 the Windows resume code do the right thing for thread switching. */
876 tid = windows_process.current_event.dwThreadId;
877
878 if (resume_info[0].thread != minus_one_ptid)
879 {
880 sig = gdb_signal_from_host (resume_info[0].sig);
881 step = resume_info[0].kind == resume_step;
882 }
883 else
884 {
885 sig = GDB_SIGNAL_0;
886 step = 0;
887 }
888
889 if (sig != GDB_SIGNAL_0)
890 {
891 if (windows_process.current_event.dwDebugEventCode
892 != EXCEPTION_DEBUG_EVENT)
893 {
894 OUTMSG (("Cannot continue with signal %s here.\n",
895 gdb_signal_to_string (sig)));
896 }
897 else if (sig == windows_process.last_sig)
898 continue_status = DBG_EXCEPTION_NOT_HANDLED;
899 else
900 OUTMSG (("Can only continue with received signal %s.\n",
901 gdb_signal_to_string (windows_process.last_sig)));
902 }
903
904 windows_process.last_sig = GDB_SIGNAL_0;
905
906 /* Get context for the currently selected thread. */
907 ptid = debug_event_ptid (&windows_process.current_event);
908 th = windows_process.thread_rec (ptid, DONT_INVALIDATE_CONTEXT);
909 if (th)
910 {
911 win32_prepare_to_resume (th);
912
913 DWORD *context_flags;
914 #ifdef __x86_64__
915 if (windows_process.wow64_process)
916 context_flags = &th->wow64_context.ContextFlags;
917 else
918 #endif
919 context_flags = &th->context.ContextFlags;
920 if (*context_flags)
921 {
922 /* Move register values from the inferior into the thread
923 context structure. */
924 regcache_invalidate ();
925
926 if (step)
927 {
928 if (the_low_target.single_step != NULL)
929 (*the_low_target.single_step) (th);
930 else
931 error ("Single stepping is not supported "
932 "in this configuration.\n");
933 }
934
935 win32_set_thread_context (th);
936 *context_flags = 0;
937 }
938 }
939
940 /* Allow continuing with the same signal that interrupted us.
941 Otherwise complain. */
942
943 child_continue (continue_status, tid);
944 }
945
946 /* See nat/windows-nat.h. */
947
948 void
949 windows_nat::windows_process_info::handle_load_dll (const char *name,
950 LPVOID base)
951 {
952 CORE_ADDR load_addr = (CORE_ADDR) (uintptr_t) base;
953
954 char buf[MAX_PATH + 1];
955 char buf2[MAX_PATH + 1];
956
957 WIN32_FIND_DATAA w32_fd;
958 HANDLE h = FindFirstFileA (name, &w32_fd);
959
960 /* The symbols in a dll are offset by 0x1000, which is the
961 offset from 0 of the first byte in an image - because
962 of the file header and the section alignment. */
963 load_addr += 0x1000;
964
965 if (h == INVALID_HANDLE_VALUE)
966 strcpy (buf, name);
967 else
968 {
969 FindClose (h);
970 strcpy (buf, name);
971 {
972 char cwd[MAX_PATH + 1];
973 char *p;
974 if (GetCurrentDirectoryA (MAX_PATH + 1, cwd))
975 {
976 p = strrchr (buf, '\\');
977 if (p)
978 p[1] = '\0';
979 SetCurrentDirectoryA (buf);
980 GetFullPathNameA (w32_fd.cFileName, MAX_PATH, buf, &p);
981 SetCurrentDirectoryA (cwd);
982 }
983 }
984 }
985
986 if (strcasecmp (buf, "ntdll.dll") == 0)
987 {
988 GetSystemDirectoryA (buf, sizeof (buf));
989 strcat (buf, "\\ntdll.dll");
990 }
991
992 #ifdef __CYGWIN__
993 cygwin_conv_path (CCP_WIN_A_TO_POSIX, buf, buf2, sizeof (buf2));
994 #else
995 strcpy (buf2, buf);
996 #endif
997
998 loaded_dll (buf2, load_addr);
999 }
1000
1001 /* See nat/windows-nat.h. */
1002
1003 void
1004 windows_nat::windows_process_info::handle_unload_dll ()
1005 {
1006 CORE_ADDR load_addr =
1007 (CORE_ADDR) (uintptr_t) current_event.u.UnloadDll.lpBaseOfDll;
1008
1009 /* The symbols in a dll are offset by 0x1000, which is the
1010 offset from 0 of the first byte in an image - because
1011 of the file header and the section alignment. */
1012 load_addr += 0x1000;
1013 unloaded_dll (NULL, load_addr);
1014 }
1015
1016 static void
1017 suspend_one_thread (thread_info *thread)
1018 {
1019 windows_thread_info *th = (windows_thread_info *) thread_target_data (thread);
1020
1021 th->suspend ();
1022 }
1023
1024 static void
1025 fake_breakpoint_event (void)
1026 {
1027 OUTMSG2(("fake_breakpoint_event\n"));
1028
1029 faked_breakpoint = 1;
1030
1031 memset (&windows_process.current_event, 0,
1032 sizeof (windows_process.current_event));
1033 windows_process.current_event.dwThreadId = windows_process.main_thread_id;
1034 windows_process.current_event.dwDebugEventCode = EXCEPTION_DEBUG_EVENT;
1035 windows_process.current_event.u.Exception.ExceptionRecord.ExceptionCode
1036 = EXCEPTION_BREAKPOINT;
1037
1038 for_each_thread (suspend_one_thread);
1039 }
1040
1041 /* See nat/windows-nat.h. */
1042
1043 bool
1044 windows_nat::windows_process_info::handle_access_violation
1045 (const EXCEPTION_RECORD *rec)
1046 {
1047 return false;
1048 }
1049
1050 /* A helper function that will, if needed, set
1051 'stopped_at_software_breakpoint' on the thread and adjust the
1052 PC. */
1053
1054 static void
1055 maybe_adjust_pc ()
1056 {
1057 struct regcache *regcache = get_thread_regcache (current_thread, 1);
1058 child_fetch_inferior_registers (regcache, -1);
1059
1060 windows_thread_info *th
1061 = windows_process.thread_rec (current_thread_ptid (),
1062 DONT_INVALIDATE_CONTEXT);
1063 th->stopped_at_software_breakpoint = false;
1064
1065 if (windows_process.current_event.dwDebugEventCode == EXCEPTION_DEBUG_EVENT
1066 && ((windows_process.current_event.u.Exception.ExceptionRecord.ExceptionCode
1067 == EXCEPTION_BREAKPOINT)
1068 || (windows_process.current_event.u.Exception.ExceptionRecord.ExceptionCode
1069 == STATUS_WX86_BREAKPOINT))
1070 && child_initialization_done)
1071 {
1072 th->stopped_at_software_breakpoint = true;
1073 CORE_ADDR pc = regcache_read_pc (regcache);
1074 CORE_ADDR sw_breakpoint_pc = pc - the_low_target.decr_pc_after_break;
1075 regcache_write_pc (regcache, sw_breakpoint_pc);
1076 }
1077 }
1078
1079 /* Get the next event from the child. */
1080
1081 static int
1082 get_child_debug_event (DWORD *continue_status,
1083 struct target_waitstatus *ourstatus)
1084 {
1085 ptid_t ptid;
1086
1087 windows_process.last_sig = GDB_SIGNAL_0;
1088 ourstatus->set_spurious ();
1089 *continue_status = DBG_CONTINUE;
1090
1091 /* Check if GDB sent us an interrupt request. */
1092 check_remote_input_interrupt_request ();
1093
1094 DEBUG_EVENT *current_event = &windows_process.current_event;
1095
1096 if (soft_interrupt_requested)
1097 {
1098 soft_interrupt_requested = 0;
1099 fake_breakpoint_event ();
1100 goto gotevent;
1101 }
1102
1103 attaching = 0;
1104 {
1105 gdb::optional<pending_stop> stop
1106 = windows_process.fetch_pending_stop (debug_threads);
1107 if (stop.has_value ())
1108 {
1109 *ourstatus = stop->status;
1110 windows_process.current_event = stop->event;
1111 ptid = debug_event_ptid (&windows_process.current_event);
1112 switch_to_thread (find_thread_ptid (ptid));
1113 return 1;
1114 }
1115
1116 /* Keep the wait time low enough for comfortable remote
1117 interruption, but high enough so gdbserver doesn't become a
1118 bottleneck. */
1119 if (!wait_for_debug_event (&windows_process.current_event, 250))
1120 {
1121 DWORD e = GetLastError();
1122
1123 if (e == ERROR_PIPE_NOT_CONNECTED)
1124 {
1125 /* This will happen if the loader fails to succesfully
1126 load the application, e.g., if the main executable
1127 tries to pull in a non-existing export from a
1128 DLL. */
1129 ourstatus->set_exited (1);
1130 return 1;
1131 }
1132
1133 return 0;
1134 }
1135 }
1136
1137 gotevent:
1138
1139 switch (current_event->dwDebugEventCode)
1140 {
1141 case CREATE_THREAD_DEBUG_EVENT:
1142 OUTMSG2 (("gdbserver: kernel event CREATE_THREAD_DEBUG_EVENT "
1143 "for pid=%u tid=%x)\n",
1144 (unsigned) current_event->dwProcessId,
1145 (unsigned) current_event->dwThreadId));
1146
1147 /* Record the existence of this thread. */
1148 child_add_thread (current_event->dwProcessId,
1149 current_event->dwThreadId,
1150 current_event->u.CreateThread.hThread,
1151 current_event->u.CreateThread.lpThreadLocalBase);
1152 break;
1153
1154 case EXIT_THREAD_DEBUG_EVENT:
1155 OUTMSG2 (("gdbserver: kernel event EXIT_THREAD_DEBUG_EVENT "
1156 "for pid=%u tid=%x\n",
1157 (unsigned) current_event->dwProcessId,
1158 (unsigned) current_event->dwThreadId));
1159 child_delete_thread (current_event->dwProcessId,
1160 current_event->dwThreadId);
1161
1162 switch_to_thread (get_first_thread ());
1163 return 1;
1164
1165 case CREATE_PROCESS_DEBUG_EVENT:
1166 OUTMSG2 (("gdbserver: kernel event CREATE_PROCESS_DEBUG_EVENT "
1167 "for pid=%u tid=%x\n",
1168 (unsigned) current_event->dwProcessId,
1169 (unsigned) current_event->dwThreadId));
1170 CloseHandle (current_event->u.CreateProcessInfo.hFile);
1171
1172 if (open_process_used)
1173 {
1174 CloseHandle (windows_process.handle);
1175 open_process_used = false;
1176 }
1177
1178 windows_process.handle = current_event->u.CreateProcessInfo.hProcess;
1179 windows_process.main_thread_id = current_event->dwThreadId;
1180
1181 /* Add the main thread. */
1182 child_add_thread (current_event->dwProcessId,
1183 windows_process.main_thread_id,
1184 current_event->u.CreateProcessInfo.hThread,
1185 current_event->u.CreateProcessInfo.lpThreadLocalBase);
1186 break;
1187
1188 case EXIT_PROCESS_DEBUG_EVENT:
1189 OUTMSG2 (("gdbserver: kernel event EXIT_PROCESS_DEBUG_EVENT "
1190 "for pid=%u tid=%x\n",
1191 (unsigned) current_event->dwProcessId,
1192 (unsigned) current_event->dwThreadId));
1193 {
1194 DWORD exit_status = current_event->u.ExitProcess.dwExitCode;
1195 /* If the exit status looks like a fatal exception, but we
1196 don't recognize the exception's code, make the original
1197 exit status value available, to avoid losing information. */
1198 int exit_signal
1199 = WIFSIGNALED (exit_status) ? WTERMSIG (exit_status) : -1;
1200 if (exit_signal == -1)
1201 ourstatus->set_exited (exit_status);
1202 else
1203 ourstatus->set_signalled (gdb_signal_from_host (exit_signal));
1204 }
1205 child_continue (DBG_CONTINUE, windows_process.desired_stop_thread_id);
1206 break;
1207
1208 case LOAD_DLL_DEBUG_EVENT:
1209 OUTMSG2 (("gdbserver: kernel event LOAD_DLL_DEBUG_EVENT "
1210 "for pid=%u tid=%x\n",
1211 (unsigned) current_event->dwProcessId,
1212 (unsigned) current_event->dwThreadId));
1213 CloseHandle (current_event->u.LoadDll.hFile);
1214 if (! child_initialization_done)
1215 break;
1216 windows_process.dll_loaded_event ();
1217
1218 ourstatus->set_loaded ();
1219 break;
1220
1221 case UNLOAD_DLL_DEBUG_EVENT:
1222 OUTMSG2 (("gdbserver: kernel event UNLOAD_DLL_DEBUG_EVENT "
1223 "for pid=%u tid=%x\n",
1224 (unsigned) current_event->dwProcessId,
1225 (unsigned) current_event->dwThreadId));
1226 if (! child_initialization_done)
1227 break;
1228 windows_process.handle_unload_dll ();
1229 ourstatus->set_loaded ();
1230 break;
1231
1232 case EXCEPTION_DEBUG_EVENT:
1233 OUTMSG2 (("gdbserver: kernel event EXCEPTION_DEBUG_EVENT "
1234 "for pid=%u tid=%x\n",
1235 (unsigned) current_event->dwProcessId,
1236 (unsigned) current_event->dwThreadId));
1237 if (windows_process.handle_exception (ourstatus, debug_threads)
1238 == HANDLE_EXCEPTION_UNHANDLED)
1239 *continue_status = DBG_EXCEPTION_NOT_HANDLED;
1240 break;
1241
1242 case OUTPUT_DEBUG_STRING_EVENT:
1243 /* A message from the kernel (or Cygwin). */
1244 OUTMSG2 (("gdbserver: kernel event OUTPUT_DEBUG_STRING_EVENT "
1245 "for pid=%u tid=%x\n",
1246 (unsigned) current_event->dwProcessId,
1247 (unsigned) current_event->dwThreadId));
1248 windows_process.handle_output_debug_string (nullptr);
1249 break;
1250
1251 default:
1252 OUTMSG2 (("gdbserver: kernel event unknown "
1253 "for pid=%u tid=%x code=%x\n",
1254 (unsigned) current_event->dwProcessId,
1255 (unsigned) current_event->dwThreadId,
1256 (unsigned) current_event->dwDebugEventCode));
1257 break;
1258 }
1259
1260 ptid = debug_event_ptid (&windows_process.current_event);
1261
1262 if (windows_process.desired_stop_thread_id != -1
1263 && windows_process.desired_stop_thread_id != ptid.lwp ())
1264 {
1265 /* Pending stop. See the comment by the definition of
1266 "pending_stops" for details on why this is needed. */
1267 OUTMSG2 (("get_windows_debug_event - "
1268 "unexpected stop in 0x%lx (expecting 0x%x)\n",
1269 ptid.lwp (), windows_process.desired_stop_thread_id));
1270 maybe_adjust_pc ();
1271 windows_process.pending_stops.push_back
1272 ({(DWORD) ptid.lwp (), *ourstatus, *current_event});
1273 ourstatus->set_spurious ();
1274 }
1275 else
1276 switch_to_thread (find_thread_ptid (ptid));
1277
1278 return 1;
1279 }
1280
1281 /* Wait for the inferior process to change state.
1282 STATUS will be filled in with a response code to send to GDB.
1283 Returns the signal which caused the process to stop. */
1284 ptid_t
1285 win32_process_target::wait (ptid_t ptid, target_waitstatus *ourstatus,
1286 target_wait_flags options)
1287 {
1288 if (cached_status.kind () != TARGET_WAITKIND_IGNORE)
1289 {
1290 /* The core always does a wait after creating the inferior, and
1291 do_initial_child_stuff already ran the inferior to the
1292 initial breakpoint (or an exit, if creating the process
1293 fails). Report it now. */
1294 *ourstatus = cached_status;
1295 cached_status.set_ignore ();
1296 return debug_event_ptid (&windows_process.current_event);
1297 }
1298
1299 while (1)
1300 {
1301 DWORD continue_status;
1302 if (!get_child_debug_event (&continue_status, ourstatus))
1303 continue;
1304
1305 switch (ourstatus->kind ())
1306 {
1307 case TARGET_WAITKIND_EXITED:
1308 OUTMSG2 (("Child exited with retcode = %x\n",
1309 ourstatus->exit_status ()));
1310 win32_clear_inferiors ();
1311 return ptid_t (windows_process.current_event.dwProcessId);
1312 case TARGET_WAITKIND_STOPPED:
1313 case TARGET_WAITKIND_SIGNALLED:
1314 case TARGET_WAITKIND_LOADED:
1315 {
1316 OUTMSG2 (("Child Stopped with signal = %d \n",
1317 ourstatus->sig ()));
1318 maybe_adjust_pc ();
1319 return debug_event_ptid (&windows_process.current_event);
1320 }
1321 default:
1322 OUTMSG (("Ignoring unknown internal event, %d\n",
1323 ourstatus->kind ()));
1324 /* fall-through */
1325 case TARGET_WAITKIND_SPURIOUS:
1326 /* do nothing, just continue */
1327 child_continue (continue_status,
1328 windows_process.desired_stop_thread_id);
1329 break;
1330 }
1331 }
1332 }
1333
1334 /* Fetch registers from the inferior process.
1335 If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO. */
1336 void
1337 win32_process_target::fetch_registers (regcache *regcache, int regno)
1338 {
1339 child_fetch_inferior_registers (regcache, regno);
1340 }
1341
1342 /* Store registers to the inferior process.
1343 If REGNO is -1, store all registers; otherwise, store at least REGNO. */
1344 void
1345 win32_process_target::store_registers (regcache *regcache, int regno)
1346 {
1347 child_store_inferior_registers (regcache, regno);
1348 }
1349
1350 /* Read memory from the inferior process. This should generally be
1351 called through read_inferior_memory, which handles breakpoint shadowing.
1352 Read LEN bytes at MEMADDR into a buffer at MYADDR. */
1353 int
1354 win32_process_target::read_memory (CORE_ADDR memaddr, unsigned char *myaddr,
1355 int len)
1356 {
1357 return child_xfer_memory (memaddr, (char *) myaddr, len, 0, 0) != len;
1358 }
1359
1360 /* Write memory to the inferior process. This should generally be
1361 called through write_inferior_memory, which handles breakpoint shadowing.
1362 Write LEN bytes from the buffer at MYADDR to MEMADDR.
1363 Returns 0 on success and errno on failure. */
1364 int
1365 win32_process_target::write_memory (CORE_ADDR memaddr,
1366 const unsigned char *myaddr, int len)
1367 {
1368 return child_xfer_memory (memaddr, (char *) myaddr, len, 1, 0) != len;
1369 }
1370
1371 /* Send an interrupt request to the inferior process. */
1372 void
1373 win32_process_target::request_interrupt ()
1374 {
1375 if (GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT, signal_pid))
1376 return;
1377
1378 /* GenerateConsoleCtrlEvent can fail if process id being debugged is
1379 not a process group id.
1380 Fallback to XP/Vista 'DebugBreakProcess', which generates a
1381 breakpoint exception in the interior process. */
1382
1383 if (DebugBreakProcess (windows_process.handle))
1384 return;
1385
1386 /* Last resort, suspend all threads manually. */
1387 soft_interrupt_requested = 1;
1388 }
1389
1390 bool
1391 win32_process_target::supports_hardware_single_step ()
1392 {
1393 return true;
1394 }
1395
1396 bool
1397 win32_process_target::supports_qxfer_siginfo ()
1398 {
1399 return true;
1400 }
1401
1402 /* Write Windows signal info. */
1403
1404 int
1405 win32_process_target::qxfer_siginfo (const char *annex,
1406 unsigned char *readbuf,
1407 unsigned const char *writebuf,
1408 CORE_ADDR offset, int len)
1409 {
1410 if (windows_process.siginfo_er.ExceptionCode == 0)
1411 return -1;
1412
1413 if (readbuf == nullptr)
1414 return -1;
1415
1416 char *buf = (char *) &windows_process.siginfo_er;
1417 size_t bufsize = sizeof (windows_process.siginfo_er);
1418
1419 #ifdef __x86_64__
1420 EXCEPTION_RECORD32 er32;
1421 if (windows_process.wow64_process)
1422 {
1423 buf = (char *) &er32;
1424 bufsize = sizeof (er32);
1425
1426 er32.ExceptionCode = windows_process.siginfo_er.ExceptionCode;
1427 er32.ExceptionFlags = windows_process.siginfo_er.ExceptionFlags;
1428 er32.ExceptionRecord
1429 = (uintptr_t) windows_process.siginfo_er.ExceptionRecord;
1430 er32.ExceptionAddress
1431 = (uintptr_t) windows_process.siginfo_er.ExceptionAddress;
1432 er32.NumberParameters = windows_process.siginfo_er.NumberParameters;
1433 int i;
1434 for (i = 0; i < EXCEPTION_MAXIMUM_PARAMETERS; i++)
1435 er32.ExceptionInformation[i]
1436 = windows_process.siginfo_er.ExceptionInformation[i];
1437 }
1438 #endif
1439
1440 if (offset > bufsize)
1441 return -1;
1442
1443 if (offset + len > bufsize)
1444 len = bufsize - offset;
1445
1446 memcpy (readbuf, buf + offset, len);
1447
1448 return len;
1449 }
1450
1451 bool
1452 win32_process_target::supports_get_tib_address ()
1453 {
1454 return true;
1455 }
1456
1457 /* Write Windows OS Thread Information Block address. */
1458
1459 int
1460 win32_process_target::get_tib_address (ptid_t ptid, CORE_ADDR *addr)
1461 {
1462 windows_thread_info *th;
1463 th = windows_process.thread_rec (ptid, DONT_INVALIDATE_CONTEXT);
1464 if (th == NULL)
1465 return 0;
1466 if (addr != NULL)
1467 *addr = th->thread_local_base;
1468 return 1;
1469 }
1470
1471 /* Implementation of the target_ops method "sw_breakpoint_from_kind". */
1472
1473 const gdb_byte *
1474 win32_process_target::sw_breakpoint_from_kind (int kind, int *size)
1475 {
1476 *size = the_low_target.breakpoint_len;
1477 return the_low_target.breakpoint;
1478 }
1479
1480 bool
1481 win32_process_target::stopped_by_sw_breakpoint ()
1482 {
1483 windows_thread_info *th
1484 = windows_process.thread_rec (current_thread_ptid (),
1485 DONT_INVALIDATE_CONTEXT);
1486 return th == nullptr ? false : th->stopped_at_software_breakpoint;
1487 }
1488
1489 bool
1490 win32_process_target::supports_stopped_by_sw_breakpoint ()
1491 {
1492 return true;
1493 }
1494
1495 CORE_ADDR
1496 win32_process_target::read_pc (struct regcache *regcache)
1497 {
1498 return (*the_low_target.get_pc) (regcache);
1499 }
1500
1501 void
1502 win32_process_target::write_pc (struct regcache *regcache, CORE_ADDR pc)
1503 {
1504 return (*the_low_target.set_pc) (regcache, pc);
1505 }
1506
1507 const char *
1508 win32_process_target::thread_name (ptid_t thread)
1509 {
1510 windows_thread_info *th
1511 = windows_process.thread_rec (current_thread_ptid (),
1512 DONT_INVALIDATE_CONTEXT);
1513 return th->thread_name ();
1514 }
1515
1516 const char *
1517 win32_process_target::pid_to_exec_file (int pid)
1518 {
1519 return windows_process.pid_to_exec_file (pid);
1520 }
1521
1522 /* The win32 target ops object. */
1523
1524 static win32_process_target the_win32_target;
1525
1526 /* Initialize the Win32 backend. */
1527 void
1528 initialize_low (void)
1529 {
1530 set_target_ops (&the_win32_target);
1531 the_low_target.arch_setup ();
1532
1533 initialize_loadable ();
1534 }