1 /* Internal interfaces for the Windows code
2 Copyright (C) 1995-2021 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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/>. */
19 #ifndef NAT_WINDOWS_NAT_H
20 #define NAT_WINDOWS_NAT_H
26 #include "gdbsupport/gdb_optional.h"
27 #include "target/waitstatus.h"
29 #define STATUS_WX86_BREAKPOINT 0x4000001F
30 #define STATUS_WX86_SINGLE_STEP 0x4000001E
35 /* Thread information structure used to track extra information about
37 struct windows_thread_info
39 windows_thread_info (DWORD tid_
, HANDLE h_
, CORE_ADDR tlb
)
42 thread_local_base (tlb
)
46 ~windows_thread_info ();
48 DISABLE_COPY_AND_ASSIGN (windows_thread_info
);
50 /* Ensure that this thread has been suspended. */
53 /* Resume the thread if it has been suspended. */
56 /* The Win32 thread identifier. */
59 /* The handle to the thread. */
62 /* Thread Information Block address. */
63 CORE_ADDR thread_local_base
;
65 /* This keeps track of whether SuspendThread was called on this
66 thread. -1 means there was a failure or that the thread was
67 explicitly not suspended, 1 means it was called, and 0 means it
71 /* The context of the thread, including any manipulations. */
76 WOW64_CONTEXT wow64_context
;
80 /* Whether debug registers changed since we last set CONTEXT back to
82 bool debug_registers_changed
= false;
84 /* Nonzero if CONTEXT is invalidated and must be re-read from the
86 bool reload_context
= false;
88 /* True if this thread is currently stopped at a software
89 breakpoint. This is used to offset the PC when needed. */
90 bool stopped_at_software_breakpoint
= false;
92 /* True if we've adjusted the PC after hitting a software
93 breakpoint, false otherwise. This lets us avoid multiple
94 adjustments if the registers are read multiple times. */
95 bool pc_adjusted
= false;
97 /* The name of the thread, allocated by xmalloc. */
98 gdb::unique_xmalloc_ptr
<char> name
;
102 /* Possible values to pass to 'thread_rec'. */
103 enum thread_disposition_type
105 /* Do not invalidate the thread's context, and do not suspend the
107 DONT_INVALIDATE_CONTEXT
,
108 /* Invalidate the context, but do not suspend the thread. */
110 /* Invalidate the context and suspend the thread. */
114 /* Find a thread record given a thread id. THREAD_DISPOSITION
115 controls whether the thread is suspended, and whether the context
118 This function must be supplied by the embedding application. */
119 extern windows_thread_info
*thread_rec (ptid_t ptid
,
120 thread_disposition_type disposition
);
123 /* Handle OUTPUT_DEBUG_STRING_EVENT from child process. Updates
124 OURSTATUS and returns the thread id if this represents a thread
125 change (this is specific to Cygwin), otherwise 0.
127 Cygwin prepends its messages with a "cygwin:". Interpret this as
128 a Cygwin signal. Otherwise just print the string as a warning.
130 This function must be supplied by the embedding application. */
131 extern int handle_output_debug_string (struct target_waitstatus
*ourstatus
);
133 /* Handle a DLL load event.
135 This function assumes that the current event did not occur during
136 inferior initialization.
138 DLL_NAME is the name of the library. BASE is the base load
141 This function must be supplied by the embedding application. */
143 extern void handle_load_dll (const char *dll_name
, LPVOID base
);
145 /* Handle a DLL unload event.
147 This function assumes that this event did not occur during inferior
150 This function must be supplied by the embedding application. */
152 extern void handle_unload_dll ();
154 /* Handle MS_VC_EXCEPTION when processing a stop. MS_VC_EXCEPTION is
155 somewhat undocumented but is used to tell the debugger the name of
158 Return true if the exception was handled; return false otherwise.
160 This function must be supplied by the embedding application. */
162 extern bool handle_ms_vc_exception (const EXCEPTION_RECORD
*rec
);
164 /* When EXCEPTION_ACCESS_VIOLATION is processed, we give the embedding
165 application a chance to change it to be considered "unhandled".
166 This function must be supplied by the embedding application. If it
167 returns true, then the exception is "unhandled". */
169 extern bool handle_access_violation (const EXCEPTION_RECORD
*rec
);
172 /* Currently executing process */
173 extern HANDLE current_process_handle
;
174 extern DWORD current_process_id
;
175 extern DWORD main_thread_id
;
176 extern enum gdb_signal last_sig
;
178 /* The current debug event from WaitForDebugEvent or from a pending
180 extern DEBUG_EVENT current_event
;
182 /* The ID of the thread for which we anticipate a stop event.
183 Normally this is -1, meaning we'll accept an event in any
185 extern DWORD desired_stop_thread_id
;
187 /* A single pending stop. See "pending_stops" for more
194 /* The target waitstatus we computed. */
195 target_waitstatus status
;
197 /* The event. A few fields of this can be referenced after a stop,
198 and it seemed simplest to store the entire event. */
202 /* A vector of pending stops. Sometimes, Windows will report a stop
203 on a thread that has been ostensibly suspended. We believe what
204 happens here is that two threads hit a breakpoint simultaneously,
205 and the Windows kernel queues the stop events. However, this can
206 result in the strange effect of trying to single step thread A --
207 leaving all other threads suspended -- and then seeing a stop in
208 thread B. To handle this scenario, we queue all such "pending"
209 stops here, and then process them once the step has completed. See
211 extern std::vector
<pending_stop
> pending_stops
;
213 /* Contents of $_siginfo */
214 extern EXCEPTION_RECORD siginfo_er
;
217 /* The target is a WOW64 process */
218 extern bool wow64_process
;
219 /* Ignore first breakpoint exception of WOW64 process */
220 extern bool ignore_first_breakpoint
;
225 HANDLE_EXCEPTION_UNHANDLED
= 0,
226 HANDLE_EXCEPTION_HANDLED
,
227 HANDLE_EXCEPTION_IGNORED
228 } handle_exception_result
;
230 extern handle_exception_result handle_exception
231 (struct target_waitstatus
*ourstatus
, bool debug_exceptions
);
233 /* Call to indicate that a DLL was loaded. */
235 extern void dll_loaded_event ();
237 /* Iterate over all DLLs currently mapped by our inferior, and
238 add them to our list of solibs. */
240 extern void windows_add_all_dlls ();
242 /* Return true if there is a pending stop matching
243 desired_stop_thread_id. If DEBUG_EVENTS is true, logging will be
246 extern bool matching_pending_stop (bool debug_events
);
248 /* See if a pending stop matches DESIRED_STOP_THREAD_ID. If so,
249 remove it from the list of pending stops, set 'current_event', and
250 return it. Otherwise, return an empty optional. */
252 extern gdb::optional
<pending_stop
> fetch_pending_stop (bool debug_events
);
254 /* A simple wrapper for ContinueDebugEvent that continues the last
255 waited-for event. If DEBUG_EVENTS is true, logging will be
258 extern BOOL
continue_last_debug_event (DWORD continue_status
,
261 /* A simple wrapper for WaitForDebugEvent that also sets the internal
262 'last_wait_event' on success. */
264 extern BOOL
wait_for_debug_event (DEBUG_EVENT
*event
, DWORD timeout
);
266 #define AdjustTokenPrivileges dyn_AdjustTokenPrivileges
267 #define DebugActiveProcessStop dyn_DebugActiveProcessStop
268 #define DebugBreakProcess dyn_DebugBreakProcess
269 #define DebugSetProcessKillOnExit dyn_DebugSetProcessKillOnExit
270 #define EnumProcessModules dyn_EnumProcessModules
271 #define EnumProcessModulesEx dyn_EnumProcessModulesEx
272 #define GetModuleInformation dyn_GetModuleInformation
273 #define GetModuleFileNameExA dyn_GetModuleFileNameExA
274 #define GetModuleFileNameExW dyn_GetModuleFileNameExW
275 #define LookupPrivilegeValueA dyn_LookupPrivilegeValueA
276 #define OpenProcessToken dyn_OpenProcessToken
277 #define GetConsoleFontSize dyn_GetConsoleFontSize
278 #define GetCurrentConsoleFont dyn_GetCurrentConsoleFont
279 #define Wow64SuspendThread dyn_Wow64SuspendThread
280 #define Wow64GetThreadContext dyn_Wow64GetThreadContext
281 #define Wow64SetThreadContext dyn_Wow64SetThreadContext
282 #define Wow64GetThreadSelectorEntry dyn_Wow64GetThreadSelectorEntry
283 #define GenerateConsoleCtrlEvent dyn_GenerateConsoleCtrlEvent
285 typedef BOOL
WINAPI (AdjustTokenPrivileges_ftype
) (HANDLE
, BOOL
,
287 DWORD
, PTOKEN_PRIVILEGES
,
289 extern AdjustTokenPrivileges_ftype
*AdjustTokenPrivileges
;
291 typedef BOOL
WINAPI (DebugActiveProcessStop_ftype
) (DWORD
);
292 extern DebugActiveProcessStop_ftype
*DebugActiveProcessStop
;
294 typedef BOOL
WINAPI (DebugBreakProcess_ftype
) (HANDLE
);
295 extern DebugBreakProcess_ftype
*DebugBreakProcess
;
297 typedef BOOL
WINAPI (DebugSetProcessKillOnExit_ftype
) (BOOL
);
298 extern DebugSetProcessKillOnExit_ftype
*DebugSetProcessKillOnExit
;
300 typedef BOOL
WINAPI (EnumProcessModules_ftype
) (HANDLE
, HMODULE
*, DWORD
,
302 extern EnumProcessModules_ftype
*EnumProcessModules
;
305 typedef BOOL
WINAPI (EnumProcessModulesEx_ftype
) (HANDLE
, HMODULE
*, DWORD
,
307 extern EnumProcessModulesEx_ftype
*EnumProcessModulesEx
;
310 typedef BOOL
WINAPI (GetModuleInformation_ftype
) (HANDLE
, HMODULE
,
311 LPMODULEINFO
, DWORD
);
312 extern GetModuleInformation_ftype
*GetModuleInformation
;
314 typedef DWORD
WINAPI (GetModuleFileNameExA_ftype
) (HANDLE
, HMODULE
, LPSTR
,
316 extern GetModuleFileNameExA_ftype
*GetModuleFileNameExA
;
318 typedef DWORD
WINAPI (GetModuleFileNameExW_ftype
) (HANDLE
, HMODULE
,
320 extern GetModuleFileNameExW_ftype
*GetModuleFileNameExW
;
322 typedef BOOL
WINAPI (LookupPrivilegeValueA_ftype
) (LPCSTR
, LPCSTR
, PLUID
);
323 extern LookupPrivilegeValueA_ftype
*LookupPrivilegeValueA
;
325 typedef BOOL
WINAPI (OpenProcessToken_ftype
) (HANDLE
, DWORD
, PHANDLE
);
326 extern OpenProcessToken_ftype
*OpenProcessToken
;
328 typedef BOOL
WINAPI (GetCurrentConsoleFont_ftype
) (HANDLE
, BOOL
,
329 CONSOLE_FONT_INFO
*);
330 extern GetCurrentConsoleFont_ftype
*GetCurrentConsoleFont
;
332 typedef COORD
WINAPI (GetConsoleFontSize_ftype
) (HANDLE
, DWORD
);
333 extern GetConsoleFontSize_ftype
*GetConsoleFontSize
;
336 typedef DWORD
WINAPI (Wow64SuspendThread_ftype
) (HANDLE
);
337 extern Wow64SuspendThread_ftype
*Wow64SuspendThread
;
339 typedef BOOL
WINAPI (Wow64GetThreadContext_ftype
) (HANDLE
, PWOW64_CONTEXT
);
340 extern Wow64GetThreadContext_ftype
*Wow64GetThreadContext
;
342 typedef BOOL
WINAPI (Wow64SetThreadContext_ftype
) (HANDLE
,
343 const WOW64_CONTEXT
*);
344 extern Wow64SetThreadContext_ftype
*Wow64SetThreadContext
;
346 typedef BOOL
WINAPI (Wow64GetThreadSelectorEntry_ftype
) (HANDLE
, DWORD
,
348 extern Wow64GetThreadSelectorEntry_ftype
*Wow64GetThreadSelectorEntry
;
351 typedef BOOL
WINAPI (GenerateConsoleCtrlEvent_ftype
) (DWORD
, DWORD
);
352 extern GenerateConsoleCtrlEvent_ftype
*GenerateConsoleCtrlEvent
;
354 /* Load any functions which may not be available in ancient versions
357 extern bool initialize_loadable ();