DebugServer2
Loading...
Searching...
No Matches
ExtraWrappers.h
1//
2// Copyright (c) 2014-present, Facebook, Inc.
3// All rights reserved.
4//
5// This source code is licensed under the University of Illinois/NCSA Open
6// Source License found in the LICENSE file in the root directory of this
7// source tree. An additional grant of patent rights can be found in the
8// PATENTS file in the same directory.
9//
10
11#pragma once
12
13#include "DebugServer2/Base.h"
14
15#include <cstdarg>
16#include <cstdio>
17#include <windows.h>
18
19#define DS2_EXCEPTION_UNCAUGHT_COM 0x800706BA
20#define DS2_EXCEPTION_UNCAUGHT_USER 0xE06D7363
21#define DS2_EXCEPTION_UNCAUGHT_WINRT 0x40080201
22#define DS2_EXCEPTION_VC_THREAD_NAME_SET 0x406D1388
23
24#if !defined(HAVE_WaitForDebugEventEx)
25#define WaitForDebugEventEx WaitForDebugEvent
26#endif
27
28// Some APIs are not exposed when building for UAP.
29#if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
30
31#if !defined(DOXYGEN)
32
33// clang-format off
34extern "C" {
35
36#if !defined(UNLEN)
37#define UNLEN 256
38#endif
39
40#if !defined(HAVE_GetWindowsDirectoryW)
41WINBASEAPI UINT WINAPI GetWindowsDirectoryW(
42 _Out_ LPWSTR lpBuffer,
43 _In_ UINT uSize
44);
45#endif
46
47#if !defined(HAVE_GetModuleHandleA)
48WINBASEAPI HMODULE WINAPI GetModuleHandleA(
49 _In_opt_ LPCSTR lpModuleName
50);
51#endif
52
53#if !defined(HAVE_GetModuleHandleW)
54WINBASEAPI HMODULE WINAPI GetModuleHandleW(
55 _In_opt_ LPCWSTR lpModuleName
56);
57#endif
58
59#if !defined(HAVE_struct__STARTUPINFOW)
60typedef struct _STARTUPINFOW {
61 DWORD cb;
62 LPWSTR lpReserved;
63 LPWSTR lpDesktop;
64 LPWSTR lpTitle;
65 DWORD dwX;
66 DWORD dwY;
67 DWORD dwXSize;
68 DWORD dwYSize;
69 DWORD dwXCountChars;
70 DWORD dwYCountChars;
71 DWORD dwFillAttribute;
72 DWORD dwFlags;
73 WORD wShowWindow;
74 WORD cbReserved2;
75 LPBYTE lpReserved2;
76 HANDLE hStdInput;
77 HANDLE hStdOutput;
78 HANDLE hStdError;
79} STARTUPINFOW, *LPSTARTUPINFOW;
80#endif
81
82#if !defined(HAVE_struct__PROCESS_INFORMATION)
83typedef struct _PROCESS_INFORMATION {
84 HANDLE hProcess;
85 HANDLE hThread;
86 DWORD dwProcessId;
87 DWORD dwThreadId;
88} PROCESS_INFORMATION, *LPPROCESS_INFORMATION;
89#endif
90
91#if !defined(HAVE_HANDLE_FLAG_INHERIT)
92#define HANDLE_FLAG_INHERIT 0x00000001
93#endif
94
95// InitializeProcThreadAttributeList/UpdateProcThreadAttribute and the
96// PROC_THREAD_ATTRIBUTE_LIST type are available in this partition already
97// (they back ConPTY support); only the STARTUPINFOEXW struct and the
98// PROC_THREAD_ATTRIBUTE_HANDLE_LIST attribute constant are missing.
99#if !defined(HAVE_PROC_THREAD_ATTRIBUTE_HANDLE_LIST)
100#if !defined(ProcThreadAttributeValue)
101#define ProcThreadAttributeValue(Number, Thread, Input, Additive) \
102 ((Number) | ((Thread) ? 0x00010000 : 0) | ((Input) ? 0x00020000 : 0) | \
103 ((Additive) ? 0x00040000 : 0))
104#endif
105// ProcThreadAttributeHandleList == 2 in the PROC_THREAD_ATTRIBUTE_NUM enum.
106#define PROC_THREAD_ATTRIBUTE_HANDLE_LIST \
107 ProcThreadAttributeValue(2, FALSE, TRUE, FALSE)
108#endif
109
110#if !defined(HAVE_STARTUPINFOEXW)
111typedef struct _STARTUPINFOEXW {
112 STARTUPINFOW StartupInfo;
113 PPROC_THREAD_ATTRIBUTE_LIST lpAttributeList;
114} STARTUPINFOEXW, *LPSTARTUPINFOEXW;
115#endif
116
117#if !defined(HAVE_STARTF_USESTDHANDLES)
118#define STARTF_USESTDHANDLES 0x00000100
119#endif
120
121#if !defined(HAVE_CreateFileW)
122WINBASEAPI HANDLE WINAPI CreateFileW(
123 _In_ LPCWSTR lpFileName,
124 _In_ DWORD dwDesiredAccess,
125 _In_ DWORD dwShareMode,
126 _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes,
127 _In_ DWORD dwCreationDisposition,
128 _In_ DWORD dwFlagsAndAttributes,
129 _In_opt_ HANDLE hTemplateFile
130);
131#endif
132
133#if !defined(HAVE_TerminateThread)
134WINBASEAPI BOOL WINAPI TerminateThread(
135 _Inout_ HANDLE hThread,
136 _In_ DWORD dwExitCode
137);
138#endif
139
140#if !defined(HAVE_SetThreadContext)
141WINBASEAPI BOOL WINAPI SetThreadContext(
142 _In_ HANDLE hThread,
143 _In_ const CONTEXT *lpContext
144);
145#endif
146
147#if !defined(HAVE_ReadProcessMemory)
148WINBASEAPI BOOL WINAPI ReadProcessMemory(
149 _In_ HANDLE hProcess,
150 _In_ LPCVOID lpBaseAddress,
151 _Out_ LPVOID lpBuffer,
152 _In_ SIZE_T nSize,
153 _Out_ SIZE_T *lpNumberOfBytesRead
154);
155#endif
156
157#if !defined(HAVE_WriteProcessMemory)
158WINBASEAPI BOOL WINAPI WriteProcessMemory(
159 _In_ HANDLE hProcess,
160 _In_ LPVOID lpBaseAddress,
161 _In_ LPCVOID lpBuffer,
162 _In_ SIZE_T nSize,
163 _Out_ SIZE_T *lpNumberOfBytesWritten
164);
165#endif
166
167#if !defined(HAVE_WaitForDebugEvent)
168WINBASEAPI BOOL APIENTRY WaitForDebugEvent(
169 _Out_ LPDEBUG_EVENT lpDebugEvent,
170 _In_ DWORD dwMilliseconds
171);
172#endif
173
174#if !defined(HAVE_ContinueDebugEvent)
175WINBASEAPI BOOL WINAPI ContinueDebugEvent(
176 _In_ DWORD dwProcessId,
177 _In_ DWORD dwThreadId,
178 _In_ DWORD dwContinueStatus
179);
180#endif
181
182#if !defined(HAVE_DebugActiveProcess)
183WINBASEAPI BOOL WINAPI DebugActiveProcess(
184 _In_ DWORD dwProcessId
185);
186#endif
187
188#if !defined(HAVE_DebugActiveProcessStop)
189WINBASEAPI BOOL WINAPI DebugActiveProcessStop(
190 _In_ DWORD dwProcessId
191);
192#endif
193
194#if !defined(HAVE_VirtualAllocEx)
195WINBASEAPI LPVOID WINAPI VirtualAllocEx(
196 _In_ HANDLE hProcess,
197 _In_opt_ LPVOID lpAddress,
198 _In_ SIZE_T dwSize,
199 _In_ DWORD flAllocationType,
200 _In_ DWORD flProtect
201);
202#endif
203
204#if !defined(HAVE_VirtualFreeEx)
205WINBASEAPI BOOL WINAPI VirtualFreeEx(
206 _In_ HANDLE hProcess,
207 _In_ LPVOID lpAddress,
208 _In_ SIZE_T dwSize,
209 _In_ DWORD dwFreeType
210);
211#endif
212
213#if !defined(HAVE_VirtualQueryEx)
214WINBASEAPI SIZE_T WINAPI VirtualQueryEx(
215 _In_ HANDLE hProcess,
216 _In_opt_ LPCVOID lpAddress,
217 _Out_ PMEMORY_BASIC_INFORMATION lpBuffer,
218 _In_ SIZE_T dwLength
219);
220#endif
221
222#if !defined(HAVE_EnumProcessModules)
223// psapi.h's #define EnumProcessModules -> K32EnumProcessModules (PSAPI_VERSION
224// > 1) is itself nested inside the same DESKTOP|SYSTEM|GAMES guard as the
225// rest of the header, so it's absent here too, and callers reference the
226// literal name "EnumProcessModules" directly, not the K32-prefixed one.
227BOOL WINAPI EnumProcessModules(
228 _In_ HANDLE hProcess,
229 _Out_writes_bytes_(cb) HMODULE *lphModule,
230 _In_ DWORD cb,
231 _Out_ LPDWORD lpcbNeeded
232);
233#endif
234
235#if !defined(TH32CS_SNAPTHREAD)
236#define TH32CS_SNAPTHREAD 0x00000004
237#endif
238
239#if !defined(HAVE_PTOP_LEVEL_EXCEPTION_FILTER)
240typedef LONG (WINAPI *PTOP_LEVEL_EXCEPTION_FILTER)(
241 _In_ struct _EXCEPTION_POINTERS *ExceptionInfo
242 );
243#endif
244
245#if !defined(HAVE_LPTOP_LEVEL_EXCEPTION_FILTER)
246typedef PTOP_LEVEL_EXCEPTION_FILTER LPTOP_LEVEL_EXCEPTION_FILTER;
247#endif
248
249// dbghelp.h's symbol handler: the whole header is unavailable outside the
250// Desktop/WER/Games partitions, so its types need re-declaring here too, not
251// just the functions that use them.
252#if !defined(HAVE_SymInitialize) || !defined(HAVE_SymFromAddr) || \
253 !defined(HAVE_SymGetLineFromAddr64)
254
255#if !defined(MAX_SYM_NAME)
256#define MAX_SYM_NAME 2000
257#endif
258
259typedef struct _SYMBOL_INFO {
260 ULONG SizeOfStruct;
261 ULONG TypeIndex;
262 ULONG64 Reserved[2];
263 ULONG Index;
264 ULONG Size;
265 ULONG64 ModBase;
266 ULONG Flags;
267 ULONG64 Value;
268 ULONG64 Address;
269 ULONG Register;
270 ULONG Scope;
271 ULONG Tag;
272 ULONG NameLen;
273 ULONG MaxNameLen;
274 CHAR Name[1];
275} SYMBOL_INFO, *PSYMBOL_INFO;
276
277typedef struct _IMAGEHLP_LINE64 {
278 DWORD SizeOfStruct;
279 PVOID Key;
280 DWORD LineNumber;
281 PCHAR FileName;
282 DWORD64 Address;
283} IMAGEHLP_LINE64, *PIMAGEHLP_LINE64;
284
285#endif
286
287#if !defined(HAVE_SymInitialize)
288WINBASEAPI BOOL WINAPI SymInitialize(
289 _In_ HANDLE hProcess,
290 _In_opt_ PCSTR UserSearchPath,
291 _In_ BOOL fInvadeProcess
292);
293#endif
294
295#if !defined(HAVE_SymFromAddr)
296WINBASEAPI BOOL WINAPI SymFromAddr(
297 _In_ HANDLE hProcess,
298 _In_ DWORD64 Address,
299 _Out_opt_ PDWORD64 Displacement,
300 _Inout_ PSYMBOL_INFO Symbol
301);
302#endif
303
304#if !defined(HAVE_SymGetLineFromAddr64)
305WINBASEAPI BOOL WINAPI SymGetLineFromAddr64(
306 _In_ HANDLE hProcess,
307 _In_ DWORD64 qwAddr,
308 _Out_ PDWORD pdwDisplacement,
309 _Out_ PIMAGEHLP_LINE64 Line64
310);
311#endif
312
313} // extern "C"
314// clang-format on
315
316#endif // !DOXYGEN
317
318#endif