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