1/* Internal interfaces for the Windows code
2   Copyright (C) 1995-2020 Free Software Foundation, Inc.
3
4   This file is part of GDB.
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 3 of the License, or
9   (at your option) any later version.
10
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19#ifndef NAT_WINDOWS_NAT_H
20#define NAT_WINDOWS_NAT_H
21
22#include <windows.h>
23#include <vector>
24
25#include "gdbsupport/gdb_optional.h"
26#include "target/waitstatus.h"
27
28#define STATUS_WX86_BREAKPOINT 0x4000001F
29#define STATUS_WX86_SINGLE_STEP 0x4000001E
30
31namespace windows_nat
32{
33
34/* Thread information structure used to track extra information about
35   each thread.  */
36struct windows_thread_info
37{
38  windows_thread_info (DWORD tid_, HANDLE h_, CORE_ADDR tlb)
39    : tid (tid_),
40      h (h_),
41      thread_local_base (tlb)
42  {
43  }
44
45  ~windows_thread_info ();
46
47  DISABLE_COPY_AND_ASSIGN (windows_thread_info);
48
49  /* Ensure that this thread has been suspended.  */
50  void suspend ();
51
52  /* Resume the thread if it has been suspended.  */
53  void resume ();
54
55  /* The Win32 thread identifier.  */
56  DWORD tid;
57
58  /* The handle to the thread.  */
59  HANDLE h;
60
61  /* Thread Information Block address.  */
62  CORE_ADDR thread_local_base;
63
64  /* This keeps track of whether SuspendThread was called on this
65     thread.  -1 means there was a failure or that the thread was
66     explicitly not suspended, 1 means it was called, and 0 means it
67     was not.  */
68  int suspended = 0;
69
70#ifdef _WIN32_WCE
71  /* The context as retrieved right after suspending the thread. */
72  CONTEXT base_context {};
73#endif
74
75  /* The context of the thread, including any manipulations.  */
76  union
77  {
78    CONTEXT context {};
79#ifdef __x86_64__
80    WOW64_CONTEXT wow64_context;
81#endif
82  };
83
84  /* Whether debug registers changed since we last set CONTEXT back to
85     the thread.  */
86  bool debug_registers_changed = false;
87
88  /* Nonzero if CONTEXT is invalidated and must be re-read from the
89     inferior thread.  */
90  bool reload_context = false;
91
92  /* True if this thread is currently stopped at a software
93     breakpoint.  This is used to offset the PC when needed.  */
94  bool stopped_at_software_breakpoint = false;
95
96  /* True if we've adjusted the PC after hitting a software
97     breakpoint, false otherwise.  This lets us avoid multiple
98     adjustments if the registers are read multiple times.  */
99  bool pc_adjusted = false;
100
101  /* The name of the thread, allocated by xmalloc.  */
102  gdb::unique_xmalloc_ptr<char> name;
103};
104
105
106/* Possible values to pass to 'thread_rec'.  */
107enum thread_disposition_type
108{
109  /* Do not invalidate the thread's context, and do not suspend the
110     thread.  */
111  DONT_INVALIDATE_CONTEXT,
112  /* Invalidate the context, but do not suspend the thread.  */
113  DONT_SUSPEND,
114  /* Invalidate the context and suspend the thread.  */
115  INVALIDATE_CONTEXT
116};
117
118/* Find a thread record given a thread id.  THREAD_DISPOSITION
119   controls whether the thread is suspended, and whether the context
120   is invalidated.
121
122   This function must be supplied by the embedding application.  */
123extern windows_thread_info *thread_rec (ptid_t ptid,
124					thread_disposition_type disposition);
125
126
127/* Handle OUTPUT_DEBUG_STRING_EVENT from child process.  Updates
128   OURSTATUS and returns the thread id if this represents a thread
129   change (this is specific to Cygwin), otherwise 0.
130
131   Cygwin prepends its messages with a "cygwin:".  Interpret this as
132   a Cygwin signal.  Otherwise just print the string as a warning.
133
134   This function must be supplied by the embedding application.  */
135extern int handle_output_debug_string (struct target_waitstatus *ourstatus);
136
137/* Handle a DLL load event.
138
139   This function assumes that the current event did not occur during
140   inferior initialization.
141
142   This function must be supplied by the embedding application.  */
143
144extern void handle_load_dll ();
145
146/* Handle a DLL unload event.
147
148   This function assumes that this event did not occur during inferior
149   initialization.
150
151   This function must be supplied by the embedding application.  */
152
153extern void handle_unload_dll ();
154
155/* Handle MS_VC_EXCEPTION when processing a stop.  MS_VC_EXCEPTION is
156   somewhat undocumented but is used to tell the debugger the name of
157   a thread.
158
159   Return true if the exception was handled; return false otherwise.
160
161   This function must be supplied by the embedding application.  */
162
163extern bool handle_ms_vc_exception (const EXCEPTION_RECORD *rec);
164
165/* When EXCEPTION_ACCESS_VIOLATION is processed, we give the embedding
166   application a chance to change it to be considered "unhandled".
167   This function must be supplied by the embedding application.  If it
168   returns true, then the exception is "unhandled".  */
169
170extern bool handle_access_violation (const EXCEPTION_RECORD *rec);
171
172
173/* Currently executing process */
174extern HANDLE current_process_handle;
175extern DWORD current_process_id;
176extern DWORD main_thread_id;
177extern enum gdb_signal last_sig;
178
179/* The current debug event from WaitForDebugEvent or from a pending
180   stop.  */
181extern DEBUG_EVENT current_event;
182
183/* The ID of the thread for which we anticipate a stop event.
184   Normally this is -1, meaning we'll accept an event in any
185   thread.  */
186extern DWORD desired_stop_thread_id;
187
188/* A single pending stop.  See "pending_stops" for more
189   information.  */
190struct pending_stop
191{
192  /* The thread id.  */
193  DWORD thread_id;
194
195  /* The target waitstatus we computed.  */
196  target_waitstatus status;
197
198  /* The event.  A few fields of this can be referenced after a stop,
199     and it seemed simplest to store the entire event.  */
200  DEBUG_EVENT event;
201};
202
203/* A vector of pending stops.  Sometimes, Windows will report a stop
204   on a thread that has been ostensibly suspended.  We believe what
205   happens here is that two threads hit a breakpoint simultaneously,
206   and the Windows kernel queues the stop events.  However, this can
207   result in the strange effect of trying to single step thread A --
208   leaving all other threads suspended -- and then seeing a stop in
209   thread B.  To handle this scenario, we queue all such "pending"
210   stops here, and then process them once the step has completed.  See
211   PR gdb/22992.  */
212extern std::vector<pending_stop> pending_stops;
213
214/* Contents of $_siginfo */
215extern EXCEPTION_RECORD siginfo_er;
216
217#ifdef __x86_64__
218/* Ignore first breakpoint exception of WOW64 process */
219extern bool ignore_first_breakpoint;
220#endif
221
222/* Return the name of the DLL referenced by H at ADDRESS.  UNICODE
223   determines what sort of string is read from the inferior.  Returns
224   the name of the DLL, or NULL on error.  If a name is returned, it
225   is stored in a static buffer which is valid until the next call to
226   get_image_name.  */
227extern const char *get_image_name (HANDLE h, void *address, int unicode);
228
229typedef enum
230{
231  HANDLE_EXCEPTION_UNHANDLED = 0,
232  HANDLE_EXCEPTION_HANDLED,
233  HANDLE_EXCEPTION_IGNORED
234} handle_exception_result;
235
236extern handle_exception_result handle_exception
237  (struct target_waitstatus *ourstatus, bool debug_exceptions);
238
239/* Return true if there is a pending stop matching
240   desired_stop_thread_id.  If DEBUG_EVENTS is true, logging will be
241   enabled.  */
242
243extern bool matching_pending_stop (bool debug_events);
244
245/* See if a pending stop matches DESIRED_STOP_THREAD_ID.  If so,
246   remove it from the list of pending stops, set 'current_event', and
247   return it.  Otherwise, return an empty optional.  */
248
249extern gdb::optional<pending_stop> fetch_pending_stop (bool debug_events);
250
251/* A simple wrapper for ContinueDebugEvent that continues the last
252   waited-for event.  If DEBUG_EVENTS is true, logging will be
253   enabled.  */
254
255extern BOOL continue_last_debug_event (DWORD continue_status,
256				       bool debug_events);
257
258/* A simple wrapper for WaitForDebugEvent that also sets the internal
259   'last_wait_event' on success.  */
260
261extern BOOL wait_for_debug_event (DEBUG_EVENT *event, DWORD timeout);
262
263}
264
265#endif
266