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